<!-- task=m-iop-hot-path-one-shot-execution/15+13_chat_gate plan=3 tag=REVIEW_REVIEW_API milestone-task=terminal-control,chat-gate -->

# Code Review Reference - REVIEW_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-03
task=m-iop-hot-path-one-shot-execution/15+13_chat_gate, plan=3, tag=REVIEW_REVIEW_API

## Archive Evidence Snapshot

- The immediately preceding pair is archived as `agent-task/m-iop-hot-path-one-shot-execution/15+13_chat_gate/plan_cloud_G10_2.log` and `agent-task/m-iop-hot-path-one-shot-execution/15+13_chat_gate/code_review_cloud_G10_2.log`.
- Its verdict is `FAIL` with two Required findings, zero Suggested findings, and zero Nit findings.
- Required finding 1: normalized selector and live-stage consumers reuse a cached `openai_response_id` when a later visible or complete `RunEvent` omits its own required metadata value.
- Required finding 2: live OpenAI and Anthropic decoders discard `length`/`max_tokens`, `stageOutput()` defaults to `stop`, and the Light flow can advance instead of ending with one caller-native length terminal.
- Fresh review evidence passed targeted and common race tests, Edge/Node vet, exact formatting checks, and `git diff --check`; `review_rework_count=2` and `evidence_integrity_failure=false`.
- Full-cycle execution and credentialed provider smoke were not run; live Pi smoke remains assigned to the separate `hot-smoke` task and is not completion evidence here.

## 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-G09.md` → `code_review_cloud_G09_3.log` and `PLAN-cloud-G09.md` → `plan_cloud_G09_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/15+13_chat_gate/`. 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_REVIEW_API-1 Require identity on each normalized event | [x] |
| REVIEW_REVIEW_API-2 Preserve and own provider length terminals | [x] |
| REVIEW_REVIEW_API-3 Regression and verification evidence | [x] |

## Implementation Checklist

- [x] [REVIEW_REVIEW_API-1] Require the stable provider response ID on every normalized visible and complete RunEvent and reject later omissions or conflicts in selector and live sources.
- [x] [REVIEW_REVIEW_API-2] Preserve OpenAI `length` and Anthropic `max_tokens` through live-stage terminal evidence, stop Light continuation, and emit one caller-native length terminal.
- [x] [REVIEW_REVIEW_API-3] Add selector/live identity and progressive provider-length regressions, then run and record fresh verification.
- [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_G09_3.log`.
- [x] Archive active `PLAN-*-G??.md` to `plan_cloud_G09_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`.
- [x] If PASS, write `complete.log` based on `agent-ops/skills/common/code-review/templates/complete-log-template.md` and leave no active `.md` files.
- [x] If PASS, move active task directory `agent-task/m-iop-hot-path-one-shot-execution/15+13_chat_gate/` to `agent-task/archive/YYYY/MM/m-iop-hot-path-one-shot-execution/15+13_chat_gate/` and update this checklist at the final archive path.
- [x] 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`.
- [x] 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.
- [ ] If WARN/FAIL, write the next filesystem state matching code-review verdict and do not write `complete.log`.

## Deviations from Plan

- None. The implementation and final verification stayed within the listed files and commands.

## Key Design Decisions

- Added `bindRequired` so each normalized `delta`, `reasoning_delta`, and `complete` validates the identity carried by that exact event while unrelated events may remain identity-free and still participate in conflict detection when they carry a value.
- Added a protocol-neutral stage terminal-reason probe. Normalized complete metadata, OpenAI Chat `finish_reason`, and Anthropic Messages `stop_reason` now flow through held stage terminal evidence into `normalizedStageOutput`; absent reasons default to `stop`, and visible tools remain authoritative as `tool_calls`.
- Treated provider `length` and `max_tokens` as the same outer length outcome in the Light runner. A no-tool provider length terminal commits and renders the single caller-native terminal, removes logical and Light state, and returns before local commit or review dispatch.
- Added deterministic regressions for later normalized identity omissions, normalized/OpenAI/Anthropic terminal-reason projection, and channel-driven progressive Chat length ownership with one role, ordered content, one `length`, one `[DONE]`, no review dispatch, and state removal.

## Reviewer Checkpoints

- Confirm every normalized `delta`, `reasoning_delta`, and `complete` validates the non-empty `openai_response_id` carried by that exact event and never succeeds from cached-only identity.
- Confirm unrelated normalized status/heartbeat events may remain identity-free, while conflicting event identity still fails closed.
- Confirm OpenAI `finish_reason:"length"`, Anthropic `stop_reason:"max_tokens"`, and normalized complete-event finish metadata survive through held stage terminal evidence and `normalizedStageOutput`.
- Confirm a no-tool provider length terminal ends the Light flow before `commitLocal`, review dispatch, or another provider stage, while already flushed content remains in the one public response.
- Confirm progressive Chat output contains one provider-owned response ID, one role, ordered visible deltas, exactly one `finish_reason:"length"`, and exactly one final `[DONE]`.
- Confirm selector classification remains pre-commit and that Node, contract/spec/roadmap, `/v1/responses`, post-commit error expansion, and live Pi smoke stay outside this follow-up.

## Verification Results

> Run each command exactly as written from the repository root after all code changes. Replace each placeholder with actual stdout/stderr and record the exit status. Fresh `-count=1` output is required; summarized or reconstructed results are not acceptable. Any replacement command requires a matching `Deviations from Plan` entry.

### Target reviewed identity and provider-terminal behavior

Command: `go test -race -count=1 ./apps/edge/internal/openai -run 'TestHotPathPresetHandlersDirect|TestHotPathNormalizedStageSourceRequiresIdentity|TestHotPathChatProviderLength|TestHotPathLiveStageTerminalReason'`

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

Exit status: 0

### Full task-targeted Edge surface

Command: `go test -race -count=1 ./apps/edge/internal/openai -run 'TestHotPathChat|TestHotPathDirect|TestHotPathOuterTurn|TestChatStreamSession'`

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

Exit status: 0

### Common producer/consumer race regression

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

```text
ok  	iop/packages/go/streamgate	2.016s
ok  	iop/packages/go/config	1.646s
ok  	iop/apps/node/internal/adapters/openai_compat	1.322s
ok  	iop/apps/edge/internal/openai	13.480s
ok  	iop/apps/edge/internal/service	7.188s
```

Exit status: 0

### Static analysis

Command: `go vet ./apps/node/internal/adapters/openai_compat ./apps/edge/...`

```text
```

Exit status: 0

### Formatting

Command: `gofmt -l apps/edge/internal/openai/hot_path_stage_stream.go apps/edge/internal/openai/hot_path_dispatch.go apps/edge/internal/openai/hot_path_terminal_control.go apps/edge/internal/openai/hot_path_light.go apps/edge/internal/openai/hot_path_chat_gate_test.go apps/edge/internal/openai/hot_path_direct_test.go`

```text
```

Exit status: 0

### Diff integrity

Command: `git diff --check`

```text
```

Exit status: 0

---

> **[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: PASS
- Dimension Assessment:
  - Correctness: Pass
  - Completeness: Pass
  - Test Coverage: Pass
  - API Contract: Pass
  - Code Quality: Pass
  - Implementation Deviation: Pass
  - Verification Trust: Pass
  - Spec Conformance: Pass
- Findings: None
- Routing Signals:
  - `review_rework_count=2`
  - `evidence_integrity_failure=false`
- Next Step: Write `complete.log`, archive the active pair and task directory, and report the milestone completion event metadata without modifying the roadmap.
