<!-- task=m-iop-hot-path-one-shot-execution/04+02,03_preset_model_authorization plan=3 tag=REVIEW_API milestone-task=preset-model -->

# Code Review Reference - REVIEW_API

> **[IMPLEMENTING AGENT — READ FIRST] Filling in this file is the mandatory final step of implementation.**
> The task is NOT complete until every implementation-owned section below is filled in.
> Complete the `Implementation Checklist`; the final checklist item is mandatory before saving.
> Fill implementation-owned sections, then stop with active files in place and report ready for review.
> If implementation is blocked, record the exact blocker, attempted commands/output, and resume condition only in implementation-owned evidence fields.
> Do not ask the user directly, present choices, call user-input tools, create control-plane stop files, or classify the next state.
> Finalization (`Code Review Result`, log rename, `complete.log`, archive moves, `Review-Only Checklist`) is review-agent-only, even after compaction/resume.
> Follow the ownership table at the bottom of this file for which sections you own.

## Overview

date=2026-08-02
task=m-iop-hot-path-one-shot-execution/04+02,03_preset_model_authorization, plan=3, tag=REVIEW_API

## Archive Evidence Snapshot

- Current review evidence will be archived as `agent-task/m-iop-hot-path-one-shot-execution/04+02,03_preset_model_authorization/plan_cloud_G08_2.log` and `agent-task/m-iop-hot-path-one-shot-execution/04+02,03_preset_model_authorization/code_review_cloud_G08_2.log`.
- Verdict: FAIL. Findings: 2 Required, 0 Suggested, 0 Nit.
- Required terminal repair: activate preset JSON/SSE response rewriting only after an actual successful `RESPONSE_START`; an `END` without response start must retain the existing 502 provider error instead of returning 200 with an empty body.
- Required contract repair: limit the new external response-model guarantee to authorized virtual presets and describe the existing ordinary native byte-preserving versus Chat-bridge behavior accurately.
- Fresh review evidence: all planned focused, race, vet, format, contract-inspection, and diff commands exited 0. A temporary managed native-preset regression with only an `END` frame failed with `status=200 body="", want 502 provider error` and was removed after capture.
- Roadmap carryover: milestone task `preset-model`, approved SDD scenario S01. Predecessors remain satisfied by `agent-task/archive/2026/08/m-iop-hot-path-one-shot-execution/02+01_preset_generation/complete.log` and `agent-task/archive/2026/08/m-iop-hot-path-one-shot-execution/03+01_preset_model_config/complete.log`.

## For the Review Agent

> **[REVIEW AGENT ONLY]** The finalization steps below are review-agent only. Implementing agents must not execute this section.

Compare implementation of each item against source files and verify that output in `Verification Results` matches code.
Review completion means the following steps are finished:

1. Append verdict and `review_rework_count` / `evidence_integrity_failure` routing signals.
2. Archive `CODE_REVIEW-cloud-G08.md` → `code_review_cloud_G08_3.log` and `PLAN-cloud-G08.md` → `plan_cloud_G08_3.log`.
3. If PASS, write `complete.log` and move active task directory to `agent-task/archive/YYYY/MM/m-iop-hot-path-one-shot-execution/04+02,03_preset_model_authorization/`. If WARN/FAIL, fully write the next filesystem state required by the code-review skill.
4. If PASS and task group is `m-<milestone-slug>`, preserve the first-line `milestone-task` metadata in `complete.log` and report it for the runtime aggregation event. Roadmap state evaluation belongs to `sync-milestone-workstate`.
5. Check applicable `Review-Only Checklist` items at the final `.log` location before reporting.

---

## Implementation Item Completion

| Item | Status |
|------|---------|
| REVIEW_API-1 — Restore the native preset response-start gate | [x] |
| REVIEW_API-2 — Correct the Anthropic response-model contract boundary | [x] |

## Implementation Checklist

- [x] Restore pre-response terminal/error behavior in the native preset relay and add deterministic boundary regressions.
- [x] Correct the Anthropic contract to scope public response identity to virtual presets while preserving ordinary native/bridge semantics.
- [x] Run the focused, race, vet, format, contract-inspection, and diff verification exactly as written.
- [x] Fill implementation-owned sections in CODE_REVIEW-*-G??.md with actual implementation notes and verification output.

## Review-Only Checklist

> **[REVIEW AGENT ONLY]** This checklist is used only by the review agent.
> Implementing agents must not modify or check this section.

- [x] Append one verdict of `PASS`, `WARN`, or `FAIL` and verified `review_rework_count`, `evidence_integrity_failure` to `Code Review Result`.
- [x] Verify that verdict, `Dimension Assessment`, and Required/Suggested/Nit classifications match.
- [x] Archive active `CODE_REVIEW-*-G??.md` to `code_review_cloud_G08_3.log`.
- [x] Archive active `PLAN-*-G??.md` to `plan_cloud_G08_3.log`.
- [x] Verify that the Agent-Ops managed block in `.gitignore` unignores `agent-task/**/*.md` and `agent-task/**/*.log` and ignores `agent-roadmap/current.md`.
- [ ] If PASS, write `complete.log` based on `agent-ops/skills/common/code-review/templates/complete-log-template.md` and leave no active `.md` files.
- [ ] If PASS, move active task directory `agent-task/m-iop-hot-path-one-shot-execution/04+02,03_preset_model_authorization/` to `agent-task/archive/YYYY/MM/m-iop-hot-path-one-shot-execution/04+02,03_preset_model_authorization/` and update this checklist at the final archive path.
- [ ] If PASS and task group is `m-<milestone-slug>`, preserve and report `milestone-task` metadata for runtime aggregation, without modifying roadmap or directly calling `update-roadmap`.
- [ ] If PASS for split work, remove empty active parent `agent-task/m-iop-hot-path-one-shot-execution/` or verify it was kept due to remaining siblings/files.
- [x] If WARN/FAIL, write the next filesystem state matching code-review verdict and do not write `complete.log`.

## Deviations from Plan

None.

## Key Design Decisions

- The native rewrite branch now requires both a received `RESPONSE_START` and a 2xx provider status for BODY and END processing.
- The END-only managed preset regression asserts the pre-existing 502 Anthropic `api_error`; a BODY before response start remains raw baseline relay rather than entering the rewrite buffer.
- The contract limits public response identity rewriting to authorized virtual presets and explicitly distinguishes ordinary native byte preservation from Chat-bridge conversion.

## Reviewer Checkpoints

- BODY and END rewriting for a virtual preset requires both a received `RESPONSE_START` and a successful response status.
- An END-only managed preset tunnel returns the existing 502 Anthropic `api_error`; pre-start frames do not enter the successful rewrite state.
- Successful preset JSON and fragmented SSE still expose the virtual ID, while ordinary native and non-2xx provider responses retain their prior bytes/status/ordering.
- The Anthropic contract guarantees virtual-preset response identity without claiming that ordinary native responses rewrite their provider model; native and Chat-bridge semantics are distinguished consistently.
- SDD S01 authorization, projected selector credential identity, and predecessor evidence remain unchanged.

## Verification Results

### REVIEW_API-1 focused verification

```bash
go test -count=1 ./apps/edge/internal/openai -run 'Test(AnthropicNativeVirtualPresetPreservesPublicModelIdentity|AnthropicNativeProviderFixturesPreserveBytesAndHeaders|AnthropicNativeStreamPreservesFragmentOrderAndSingleTerminal|AnthropicNativeProviderErrorPreservesStatusAndBody)'
```

Exit status: 0

```text
ok  \tiop/apps/edge/internal/openai\t0.040s
```

### REVIEW_API-2 contract verification

```bash
rg --sort path -n 'virtual preset|ordinary native|Chat bridge|response model|provider response' agent-contract/outer/anthropic-compatible-api.md
```

Exit status: 0

```text
27:Routing first resolves the request `model` through the provider pool. An `anthropic_messages` candidate uses a native provider tunnel, while an `openai_chat` candidate uses the Messages-to-Chat bridge over its provider tunnel.
53:virtual preset model IDs for the authenticated principal. Ordinary request model
64:the provider resource and from `credential_slot_ref`. For a virtual preset, the
70:An authorized virtual preset retains its requested virtual ID in successful responses
71:across the native Messages tunnel and Chat bridge. Ordinary native routes preserve the
72:provider response model and body bytes; the Chat bridge emits its converted Anthropic
73:response model semantics.
103:Chat bridge 경로는 `Anthropic-Beta`를 지원하지 않으며, bridge로 라우팅될 때 beta 값이 있으면 `400 invalid_request_error`를 반환한다.
274:authorized virtual preset ID for the authenticated principal. An ordinary route resolves
275:to exactly one internal model group and selector-compatible provider; a virtual preset
278:An authorized virtual preset retains its requested virtual response model identity;
279:ordinary native routes and the Chat bridge retain their distinct response semantics.
282:`models[]` provider mapping은 OpenAI-compatible provider와 normalized-only provider를 같은 model group 안에 둘 수 있다. dispatch는 기존 capacity + priority + availability 기준으로 provider를 한 번 선택하고, client request field가 아니라 selected provider capability로 native Anthropic 또는 Chat bridge execution path를 결정한다.
286:선택된 provider의 `ConcreteProtocolProfile.Driver`가 `anthropic_messages`이면 Edge는 provider raw tunnel을 통해 Anthropic-native request/response를 relay한다. Ordinary native routes preserve provider response model/body bytes, while authorized virtual presets rewrite successful response identity to the requested virtual model.
287:`openai_chat`이면 Edge는 Anthropic Messages request를 Chat Completions request로 bridge하고, Chat bridge 응답을 다시 Anthropic Messages response로 변환한다. Authorized virtual presets retain their requested virtual response model identity through that conversion; ordinary bridge responses use the bridge's converted response model semantics.
295:- Chat bridge: `chat` capability + `chat_completions` operation
304:Chat bridge는 provider profile의 `extensions.thinking` 또는 `extensions.reasoning`이 `true`일 때만 `thinking` block을 지원한다.
309:Anthropic handlers do not currently record the OpenAI canonical usage metric series. Native `USAGE` tunnel frames are ignored by the Anthropic relay; provider-reported usage remains in the native response body or is converted by the Chat bridge response path.
```

### Final verification

```bash
test -f agent-task/archive/2026/08/m-iop-hot-path-one-shot-execution/02+01_preset_generation/complete.log
test -f agent-task/archive/2026/08/m-iop-hot-path-one-shot-execution/03+01_preset_model_config/complete.log
go test -count=1 ./apps/edge/internal/openai -run 'Test(AnthropicNativeVirtualPresetPreservesPublicModelIdentity|AnthropicNativeProviderFixturesPreserveBytesAndHeaders|AnthropicNativeStreamPreservesFragmentOrderAndSingleTerminal|AnthropicNativeProviderErrorPreservesStatusAndBody)'
go test -race -count=1 ./packages/go/streamgate ./packages/go/config ./apps/edge/internal/openai ./apps/edge/internal/service
go vet ./apps/edge/internal/openai
gofmt -d apps/edge/internal/openai/anthropic_native.go apps/edge/internal/openai/anthropic_native_test.go
rg --sort path -n 'virtual preset|ordinary native|Chat bridge|response model|provider response' agent-contract/outer/anthropic-compatible-api.md
git diff --check
```

Exit status: 0

```text
ok  \tiop/apps/edge/internal/openai\t0.040s
ok  \tiop/packages/go/streamgate\t2.024s
ok  \tiop/packages/go/config\t1.521s
ok  \tiop/apps/edge/internal/openai\t8.869s
ok  \tiop/apps/edge/internal/service\t6.980s

go vet ./apps/edge/internal/openai: no stdout/stderr
gofmt -d apps/edge/internal/openai/anthropic_native.go apps/edge/internal/openai/anthropic_native_test.go: no stdout/stderr
git diff --check: no stdout/stderr

Contract inspection output matched the REVIEW_API-2 evidence above.
```

---

> **[IMPLEMENTING AGENT — BEFORE SAVING] Have you filled in every implementation-owned section?**
> If anything is blank, go back and fill it in before saving this file.
> Leave review-agent-only sections unchanged.

## Section Ownership

| Section | Owner | Note |
|---------|-------|------|
| Header comment, Overview, Review Agent Instructions | Fixed at stub creation | Implementing agent must not modify or execute these (archive, complete.log, and task-directory archive move are review-agent only) |
| Archive Evidence Snapshot | Fixed at stub creation from plan when present | Implementing agent uses it as default prior-loop context; read only the specific archive files cited there when more detail is required |
| Implementation Item Completion (item names) | Fixed at stub creation | Implementing agent checks `[ ]` → `[x]` only |
| Implementation Checklist (item text/order) | Fixed at stub creation from plan | Implementing agent checks `[ ]` → `[x]` only |
| Review-Only Checklist | Review agent only | Implementing agent must not modify or check this section |
| Deviations from Plan, Key Design Decisions | Implementing agent | Replace placeholder text with actual content |
| Reviewer Checkpoints | Fixed at stub creation | Pre-filled from plan |
| Verification Results (section headings + commands) | Fixed at stub creation | Implementing agent fills in command output only; command changes require a `Deviations from Plan` entry |
| Code Review Result | Review agent appends | Not included in stub |

## Code Review Result

- Overall Verdict: FAIL
- Dimension Assessment:
  - Correctness: Pass
  - Completeness: Fail
  - Test Coverage: Fail
  - API Contract: Fail
  - Code Quality: Pass
  - Implementation Deviation: Fail
  - Verification Trust: Fail
  - Spec Conformance: Pass
- Findings:
  - Required — `agent-contract/outer/anthropic-compatible-api.md:198`: the general non-streaming response-field contract still states that `model` is always the request-model echo, contradicting the newly documented and executable ordinary-native behavior that preserves the provider response model/body bytes. The active plan explicitly requires the general response `model` description and Native-vs-Bridge section to use the same distinction, but the planned contract inspection does not match this line and therefore reported a false consistency result. Rewrite this field description to distinguish authorized virtual presets, ordinary native responses, and Chat-bridge converted responses, then inspect that exact field together with the existing native/preset regression suite.
- Routing Signals:
  - `review_rework_count=4`
  - `evidence_integrity_failure=true`
- Next Step: Invoke the plan skill in `prepare-follow-up` mode with the raw finding and fresh verification evidence, then archive this pair and materialize the freshly routed follow-up pair.
