<!-- task=m-iop-hot-path-one-shot-execution/14+13_anthropic_gate plan=1 tag=API milestone-task=terminal-control,anthropic-gate -->

# Code Review Reference - API

> **[IMPLEMENTING AGENT — READ FIRST] Filling in this file is mandatory.** Fill implementation-owned evidence and stop with active files. Review finalization is review-agent-only.

## Overview

date=2026-08-03
task=m-iop-hot-path-one-shot-execution/14+13_anthropic_gate, plan=1, tag=API

## Archive Evidence Snapshot

- Plan/review 0 was superseded before implementation; it contains no implementation verdict/evidence.

## For the Review Agent

Verify source/wire output, archive to `code_review_cloud_G10_1.log` and `plan_cloud_G09_1.log`, then finalize by verdict. Preserve `milestone-task=terminal-control,anthropic-gate` on PASS.

## Implementation Item Completion

| Item | Status |
|---|---|
| API-1 Native Messages outer codec | [x] |
| API-2 Anthropic wire evidence | [x] |

## Implementation Checklist

- [x] [API-1] Add a caller-facing Anthropic Messages outer codec and pass the already-dispatched preset result, request identity, stream mode, and `max_tokens` into the shared turn.
- [x] [API-2] Add native streaming/non-streaming, mixed-provider, fragmentation, tool, cap, and baseline error handler fixtures.
- [x] Fill implementation-owned sections in CODE_REVIEW-*-G??.md with actual implementation notes and verification output.

## Review-Only Checklist

- [x] Append verdict/routing signals and verify findings/dimensions.
- [x] Archive review/plan to suffix `1`; verify `.gitignore` managed block.
- [x] On PASS write `complete.log`, preserve metadata, archive child; on WARN/FAIL write directed state without completion.

## Deviations from Plan

- The normalized stage boundary needed an ordered delta log in `hot_path_selector.go`, `hot_path_dispatch.go`, `hot_path_terminal_control.go`, and its cloning path so the caller codec could preserve provider fragmentation without decoding provider wire itself. The Anthropic SSE decoder also needed an explicit `json:"partial_json"` tag; otherwise native tool fragments decoded as an empty object.
- The shared primary-error cleanup path now receives the request-local outer turn. This keeps the stored continuation fingerprint identical to the visible Anthropic/Chat transcript when an earlier same-turn stage released content before cleanup was issued.
- `hot_path_direct.go`, `hot_path_light.go`, and `request_identity_ingress.go` received narrow codec/outer-turn integration changes beyond the original modified-files table. Provider selection, provider dispatch, and wire decoding ownership were not moved into the Anthropic caller codec.

## Key Design Decisions

- The Messages codec is request-local and consumes only the shared outer-turn release log plus the final normalized output. It never selects a provider or parses selected-provider wire.
- The initial preset branch submits exactly once, then passes that existing `ProviderPoolDispatchResult` into `runInitialPresetTurn`; collection and shared-turn execution do not redispatch.
- Required positive `max_tokens` is validated at Messages ingress, copied into trusted Hot Path cap metadata, and used by one outer turn across every same-HTTP internal stage.
- Streaming output owns one `message_start`, monotonic block indices, stable turn-scoped tool ids, preserved thinking/text/tool argument fragments, one aggregate-usage `message_delta`, and one `message_stop`. Non-stream output is rendered from the same blocks and usage.
- Precommit failures remain JSON errors. Once SSE has committed, the codec emits exactly one native `error` event and no trailing `message_stop`.

## Reviewer Checkpoints

- Confirm selected-provider decoding stays in the common predecessor; this child only encodes caller-facing Messages output.
- Confirm full request `max_tokens`, already-dispatched initial result, one native envelope, stable tool ids, aggregate usage, and non-stream behavior.
- Confirm committed error emits Anthropic `error` without a trailing `message_stop` in covered baseline cases.

## Verification Results

### Targeted

Command: `go test -race -count=1 ./apps/edge/internal/openai -run 'TestHotPathAnthropic|TestAnthropic(ChatBridge|Native)'`

```text
ok  	iop/apps/edge/internal/openai	1.175s
```

Exit status: 0

### Common regression

Command: `go test -race -count=1 ./packages/go/streamgate ./packages/go/config ./apps/edge/internal/openai ./apps/edge/internal/service`

```text
ok  	iop/packages/go/streamgate	2.064s
ok  	iop/packages/go/config	1.940s
ok  	iop/apps/edge/internal/openai	14.235s
ok  	iop/apps/edge/internal/service	7.434s
```

Exit status: 0

### Diff

Command: `git diff --check`

```text
(no output)
```

Exit status: 0

## Section Ownership

Implementer owns completion status, deviations, decisions, and outputs. Reviewer alone owns review-only actions and final result.

## Code Review Result

- Overall Verdict: FAIL
- Dimension Assessment:
  - Correctness: Fail — the public response identity and output-cap behavior do not preserve provider semantics.
  - Completeness: Fail — the production handler still buffers the initial provider tunnel through `END` before writing the caller stream.
  - Test coverage: Fail — the cap fixture asserts character truncation, and the post-commit error fixture bypasses the handler and provider tunnel.
  - API contract: Fail — virtual-preset Messages output exposes an IOP logical request ID and treats `max_tokens` as a character/byte budget.
  - Code quality: Pass — the new codec and shared outer-turn code are structured and the implementation deviations are documented.
  - Implementation deviation: Pass — the additional shared files are explained and are relevant to the requested outer-turn integration.
  - Verification trust: Pass — the reviewer reran every claimed command successfully and `git diff --check` is clean.
  - Spec conformance: Fail — S10/S11 require provider identity, live nonterminal release, and endpoint-native terminal behavior across one outer turn.
- Findings:
  - Required — `apps/edge/internal/openai/hot_path_light.go:793`, `apps/edge/internal/openai/anthropic_stream.go:445`, `apps/edge/internal/openai/hot_path_anthropic_gate_test.go:132`: the continuation outer turn is created with `iop_logical_request_id`; after a stage begins or releases a delta, `bindResponseID` cannot replace it, and the test explicitly requires that transport identity in `message_start.message.id`. The Anthropic contract requires the first provider-reported response ID and forbids promoting an IOP transport value. Bind the first validated stage response ID before opening/releasing the outer response, use it for the message and turn-scoped tool namespace, and assert that it equals the provider ID and differs from the logical request ID.
  - Required — `apps/edge/internal/openai/hot_path_terminal_control.go:151`, `apps/edge/internal/openai/hot_path_terminal_control.go:457`, `apps/edge/internal/openai/hot_path_anthropic_gate_test.go:226`: `max_tokens` is converted to four runes per token, visible payload is locally truncated by runes, and UTF-8 byte count can override provider-reported token usage. The current fixture therefore rewrites a provider-compliant `end_turn` response with two reported output tokens into truncated text plus `max_tokens`. Keep the caller cap and inter-stage remaining budget in provider-reported tokens, preserve an already compliant stage payload and terminal, and define deterministic fail-closed behavior for missing usage without substituting character or byte counts for tokens.
  - Required — `apps/edge/internal/openai/hot_path_dispatch.go:195`, `apps/edge/internal/openai/anthropic_stream.go:401`, `apps/edge/internal/openai/hot_path_anthropic_gate_test.go:299`: the initial tunnel collector buffers every body frame until `END`, and only afterward does the Anthropic codec write its complete SSE response. The claimed post-commit error test calls codec methods directly, so it does not prove the handler can flush live deltas or convert a later provider tunnel error. Route the production provider tunnel through the normalized stage source/sink, flush safe deltas before `END` while holding internal terminals, and add a channel-controlled handler test that observes a flushed `message_start`/delta before provider completion and then verifies one native `error` with no `message_stop` after an injected error.
- Routing Signals:
  - `review_rework_count=1`
  - `evidence_integrity_failure=false`
- Next Step: Prepare and route a follow-up plan for all Required findings; do not write `complete.log`.
