<!-- task=m-iop-hot-path-one-shot-execution/05+02,04_request_coordinator plan=5 tag=REVIEW_API milestone-task=request-identity -->

# Validate Complete Endpoint-Native Committed History

## For the Implementing Agent

Implement the two endpoint history validators, run every command, and fill the implementation-owned sections in `CODE_REVIEW-cloud-G05.md` with actual notes and output. Keep the active files in place and report ready for review; finalization is code-review-skill only. If blocked, record only the exact blocker, attempted commands/output, and resume condition in implementation-owned evidence fields. Do not ask the user, call user-input tools, create control-plane stop files, classify the next state, archive logs, or write `complete.log`.

## Background

The newest Chat and Anthropic result frontier is now fenced, but each parser still trusts tool-call/result structure already present in the committed prefix. A malformed prefix can therefore become the next immutable lineage even though the plan and SDD require validation of the complete endpoint-native continuation before hashing or coordinator consumption.

## Archive Evidence Snapshot

- Closing pair: `agent-task/m-iop-hot-path-one-shot-execution/05+02,04_request_coordinator/plan_cloud_G06_4.log` and `agent-task/m-iop-hot-path-one-shot-execution/05+02,04_request_coordinator/code_review_cloud_G06_4.log`; verdict `FAIL`.
- Required finding: validate every committed Chat and Anthropic turn, including historical issued-ID uniqueness, tool-call/result pairing, and supported Anthropic content blocks, before hashing the prefix or committed lineage.
- Fresh evidence: every planned dependency, focused, race, vet, format, and diff command passed, but one reviewer-only test showed acceptance of duplicate historical issued IDs for both endpoints, an orphan historical Chat tool result, and an unknown historical Anthropic assistant block. The temporary test was removed after capture.
- Dependencies: `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/04+02,03_preset_model_authorization/complete.log` are the exact completed predecessors.
- Roadmap carryover: `milestone-task=request-identity`; approved SDD scenario S05 and its Evidence Map remain the acceptance source.

## Dependencies and Execution Order

- `02+01_preset_generation` is satisfied by `agent-task/archive/2026/08/m-iop-hot-path-one-shot-execution/02+01_preset_generation/complete.log`.
- `04+02,03_preset_model_authorization` is satisfied by `agent-task/archive/2026/08/m-iop-hot-path-one-shot-execution/04+02,03_preset_model_authorization/complete.log`.

## Analysis

### Files Read

- `apps/edge/internal/openai/request_lineage.go`
- `apps/edge/internal/openai/request_coordinator.go`
- `apps/edge/internal/openai/request_coordinator_test.go`
- `apps/edge/internal/openai/chat_types.go`
- `apps/edge/internal/openai/anthropic_types.go`
- `agent-roadmap/phase/knowledge-tool-optimization-extension/PHASE.md`
- `agent-roadmap/phase/knowledge-tool-optimization-extension/milestones/iop-hot-path-one-shot-execution.md`
- `agent-roadmap/sdd/knowledge-tool-optimization-extension/iop-hot-path-one-shot-execution/SDD.md`
- `agent-spec/input/openai-compatible-surface.md`
- `agent-contract/outer/openai-compatible-api.md`
- `agent-contract/outer/anthropic-compatible-api.md`
- `agent-ops/rules/project/domain/edge/rules.md`
- `agent-ops/rules/project/domain/testing/rules.md`
- `agent-test/local/rules.md`
- `agent-test/local/edge-smoke.md`
- `agent-task/m-iop-hot-path-one-shot-execution/05+02,04_request_coordinator/PLAN-cloud-G06.md`
- `agent-task/m-iop-hot-path-one-shot-execution/05+02,04_request_coordinator/CODE_REVIEW-cloud-G06.md`
- `agent-task/m-iop-hot-path-one-shot-execution/05+02,04_request_coordinator/code_review_cloud_G05_3.log`
- `agent-task/archive/2026/08/m-iop-hot-path-one-shot-execution/02+01_preset_generation/complete.log`
- `agent-task/archive/2026/08/m-iop-hot-path-one-shot-execution/04+02,03_preset_model_authorization/complete.log`

### SDD Criteria

- SDD: `agent-roadmap/sdd/knowledge-tool-optimization-extension/iop-hot-path-one-shot-execution/SDD.md`; status `[승인됨]`, lock released, and no `USER_REVIEW.md`.
- Milestone metadata: `milestone-task=request-identity`; target Acceptance Scenario S05.
- S05 and its Evidence Map require a valid full-history/frontier split, immutable endpoint-native lineage and tool binding, mutation rejection, public/provider ID affinity, and exactly-once frontier consumption.
- The checklist therefore validates every historical endpoint-native tool turn before either digest is returned and reruns focused plus race evidence for the same `request-identity` boundary.

### Verification Context

- No verification handoff was supplied. Repository-native evidence is the approved SDD, endpoint contracts, Edge/local test rules, lineage source/tests, the current FAIL result, and the two exact predecessor completion logs.
- Preflight: `/config/.local/bin/go`; resolved path `/config/opt/go/bin/go`; `go version go1.26.2 linux/arm64`; `GOROOT=/config/opt/go`. The current dirty worktree contains the intentional execution-preset task state.
- Fresh dependency checks, focused lineage tests, focused race tests, common race tests including config, `go vet`, `gofmt -d`, and `git diff --check` passed. A focused reviewer-only package test failed all four historical-prefix cases and was removed.
- No remote runner, credential, provider, live smoke, or full-cycle execution is required because the coordinator remains handler-unintegrated and this follow-up changes only deterministic endpoint history validation. Fresh `-count=1` and race output is required; cached success is not accepted. Confidence: high.

### Test Coverage Gaps

- Chat: the matrix covers malformed roles and the newest frontier but not duplicate issued IDs in an earlier assistant turn or an orphan historical `tool` message.
- Anthropic: the matrix covers the newest frontier and role alternation but not duplicate issued IDs in an earlier assistant turn or unsupported content blocks in committed history.
- Both endpoints need a valid multi-turn control proving the stricter scan preserves canonical lineage advancement and large-integer fidelity.

### Symbol References

- No symbol is renamed or removed. `validateChatMessages` and `validateAnthropicMessages` are used only by the request-lineage constructors in `request_lineage.go`; `decodeAnthropicContent` is the existing endpoint content validator available for reuse.

### Split Judgment

Keep one plan. Chat and Anthropic validators are variants of one acceptance invariant: no prefix or committed digest may be returned until every historical tool-call/result turn is structurally valid. Splitting would permit one endpoint to continue accepting malformed immutable lineage.

### Scope Rationale

Change only `request_lineage.go`, its existing coordinator/lineage test file, and the active review evidence file. Do not change the already-correct coordinator fence, integrate handlers, alter public API/config contracts, add stage/artifact behavior, or touch sibling execution-preset work.

### Final Routing

- `evaluation_mode=isolated-reassessment`; `finalizer=finalize-task-policy.sh`, pair mode.
- Build closures are all true: scope, context, verification, trusted evidence, ownership, and decisions are closed by the focused reproducer and repository-native tests. Scores `(1,1,0,2,1)` produce G05 with `local-fit` base.
- `large_indivisible_context=false`; matched loop risks are `boundary_contract`, `structured_interpretation`, and `variant_product` (3). `review_rework_count=4` and `evidence_integrity_failure=true` select `recovery-boundary`; build route is cloud `PLAN-cloud-G05.md`.
- Review closures are all true. Scores `(1,1,0,2,1)` produce official cloud G05 in `CODE_REVIEW-cloud-G05.md` using Codex `gpt-5.6-sol` xhigh.
- Capability gap: none. The exact failure and deterministic verification are available in the current checkout.

## Implementation Checklist

- [ ] Validate every Chat assistant tool-call/result turn before hashing, reject duplicate or replayed issued IDs and orphan/partial/duplicate/unknown tool results throughout committed history, and add valid plus malformed multi-turn regression coverage.
- [ ] Decode and validate every Anthropic message block before hashing, reject duplicate or replayed tool-use IDs and mismatched/partial/duplicate/unsupported tool-result turns throughout committed history, and add valid plus malformed multi-turn regression coverage.
- [ ] Run archived dependency, focused, common race including config, vet, formatting, and diff verification exactly as written.
- [ ] Fill implementation-owned sections in CODE_REVIEW-*-G??.md with actual implementation notes and verification output.

### [REVIEW_API-1] Validate Complete Chat Tool History

#### Problem

`request_lineage.go:87` validates only each Chat message role, while lines 207-215 validate issued IDs only for the newest assistant frontier. Earlier duplicate assistant IDs and orphan `tool` messages are hashed into a trusted committed prefix.

#### Solution

Scan the entire Chat message sequence before splitting the newest frontier. Track issued IDs across assistant tool-call turns, require each non-empty tool-call set to be followed by exactly its unique `tool_call_id` results before another non-tool message, and reject orphan, partial, duplicate, unknown, or replayed IDs while preserving the original `json.RawMessage` values for canonical hashing.

```go
// Before: request_lineage.go:87
for i, rawMsg := range msgList {
    // Role whitelist only.
}

// After: validate the complete sequence without rewriting payloads.
if err := validateChatToolHistory(msgList); err != nil {
    return nil, err
}
```

#### Modified Files and Checklist

- [ ] `apps/edge/internal/openai/request_lineage.go` — validate all Chat tool-call/result turns and globally reject issued-ID replay before returning digests.
- [ ] `apps/edge/internal/openai/request_coordinator_test.go` — add historical duplicate/orphan/partial/unknown cases and a valid multi-turn control.

#### Test Strategy

Extend `TestLogicalRequestEndpointContinuationRejectionMatrix` with the reviewer-reproduced historical duplicate and orphan cases plus historical partial/unknown results. Extend the valid endpoint continuation test with two committed Chat tool turns and adjacent large integers so the stricter validator cannot alter lossless canonicalization.

#### Verification

Run `go test -count=1 ./apps/edge/internal/openai -run 'TestLogicalRequest(Lineage|EndpointContinuation)'`; expect PASS.

### [REVIEW_API-2] Validate Complete Anthropic Tool History

#### Problem

`request_lineage.go:114` validates Anthropic roles and alternation only, while lines 355-373 inspect tool-use blocks only in the newest assistant frontier. Earlier duplicate tool-use IDs and unsupported assistant blocks therefore enter the committed digest.

#### Solution

Decode every message through the existing strict Anthropic content-block validator, enforce role-appropriate tool-use/tool-result placement and exact adjacent ID sets for every assistant/user tool turn, and reject duplicate or replayed issued IDs across the complete sequence before computing prefix or committed hashes.

```go
// Before: request_lineage.go:114
for i, rawMsg := range msgList {
    // Role and alternation checks only.
}

// After: reuse endpoint block validation and validate every tool turn.
blocks, err := decodeAnthropicContent(message.Content)
if err != nil {
    return nil, fmt.Errorf("anthropic message %d: %w", i, err)
}
```

#### Modified Files and Checklist

- [ ] `apps/edge/internal/openai/request_lineage.go` — validate all Anthropic content blocks, tool-use/result adjacency, exact ID sets, and issued-ID replay.
- [ ] `apps/edge/internal/openai/request_coordinator_test.go` — add historical duplicate/unsupported/mismatched cases, update valid tool-use fixtures to the strict endpoint shape, and add a valid multi-turn control.

#### Test Strategy

Extend `TestLogicalRequestEndpointContinuationRejectionMatrix` with the reviewer-reproduced historical duplicate and unsupported-block cases plus historical partial/unknown/duplicate results. Keep valid string/text/image/thinking content accepted where the endpoint decoder permits it, and verify a two-turn Anthropic tool history preserves canonical large integers.

#### Verification

Run `go test -count=1 ./apps/edge/internal/openai -run 'TestLogicalRequest(Lineage|EndpointContinuation)'`; expect PASS.

## Modified Files Summary

| File | Items |
|------|-------|
| `apps/edge/internal/openai/request_lineage.go` | REVIEW_API-1, REVIEW_API-2 |
| `apps/edge/internal/openai/request_coordinator_test.go` | REVIEW_API-1, REVIEW_API-2 |
| `agent-task/m-iop-hot-path-one-shot-execution/05+02,04_request_coordinator/CODE_REVIEW-cloud-G05.md` | REVIEW_API-1, REVIEW_API-2 |

## 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/04+02,03_preset_model_authorization/complete.log
go test -count=1 ./apps/edge/internal/openai -run 'TestLogicalRequest(Lineage|EndpointContinuation)'
go test -race -count=1 ./apps/edge/internal/openai -run 'TestLogicalRequest(MandatoryLineageFence|Continuation|CommittedLineage|ConcurrentFrontier)'
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/request_coordinator.go apps/edge/internal/openai/request_lineage.go apps/edge/internal/openai/request_coordinator_test.go
git diff --check
```

Expected: every command exits 0; both endpoint parsers reject malformed current and historical tool turns without changing canonical JSON fidelity, the coordinator fence and no-mutation/race behavior remain intact, and no handler or external execution path is added.

After completing all code changes, fill implementation-owned sections in `CODE_REVIEW-*-G??.md`.
