# CORI Provider Event Normalization and Bridge Hardening

## Purpose
This document governs the provider-facing bridge layer that sits between raw provider events and the normalized CORI runtime used by the full-page and popup call surfaces.

The goal is simple:

- provider payloads stay inside the adapter layer
- the dispatcher UI reads only normalized CORI runtime state
- duplicate, late, or out-of-order provider events do not create noisy or contradictory call behavior

## Core rule
The provider adapter is not allowed to become the UI state model.

Raw provider events must be translated into the shared normalized CORI event spine before they can affect:

- listening posture
- visible fact state
- advice refresh
- save readiness
- session closeout

## Normalized mapping rules
The bridge should map provider events into the shared CORI runtime event set.

Examples:

- `provider.session.created` -> `SESSION_READY`
- `provider.call.ringing` -> `CALL_DIALING`
- `provider.call.connected` -> `CALL_CONNECTED`
- `provider.media.started` -> `LISTENING_STARTED`
- `provider.transcript.partial` -> `TRANSCRIPT_PARTIAL`
- `provider.fact.upsert` -> `FACT_UPSERTED`
- `provider.advice.refresh` -> `ADVICE_REFRESHED`
- `provider.session.closed` -> `SESSION_ENDED`

A mapping should only be accepted when the bridge rules say the session is in a valid phase for that event.

## Bridge-safe session phases
Recommended bridge phases:

- `NOT_READY`
- `GUARDED`
- `READY_TO_START`
- `STARTING`
- `LIVE`
- `CLOSING`
- `CLOSED`

The bridge owns the transition rules between these phases.
The UI should not infer them directly from raw provider payloads.

## Session start guards
Provider-backed listening should not start unless the following are satisfied:

- policy allows it
- required notice is clear
- a target phone exists
- the dialer is ready
- provider mapping is staged
- webhook posture is ready
- live token readiness is clear

If any required guard fails, the session should remain blocked or waiting.

## Duplicate and out-of-order handling
The bridge must protect runtime state from provider noise.

Rules:

1. Duplicate event ids should be logged and ignored after first acceptance.
2. Out-of-order events should not replay raw intermediate UI states.
3. Late events after terminal closeout must not reopen the session.
4. If a stronger final fact arrives during a controlled closeout window, it may still upgrade the saved result before terminal close.

## Partial transcript normalization
Transcript fragments should be normalized before they affect dispatcher-facing state.

Normalization should:

- strip empty or filler fragments
- drop provider-only metadata
- keep only short readable text
- promote facts only when the new fragment is stronger or more specific than the current best value
- prevent raw partials from crossing the save boundary

## Closeout hardening
The session should have one terminal end-state.

Closeout should:

- accept one final end decision
- freeze the last accepted advice result
- persist only stable or confirmed facts
- discard unresolved raw fragments and superseded counters
- keep full-page and popup behavior aligned after the call ends

## UI consumption rule
The full-page CORI surface and popup CORI surface must read the same normalized runtime truth.
Neither surface should render raw provider payload structure directly.

## Next build implication
The next controlled provider-backed session path should plug into this hardened bridge rather than bypassing it.
