<!-- task=m-iop-hot-path-one-shot-execution/06+04,05_request_identity_ingress plan=0 tag=API milestone-task=request-identity -->

# Preset Request Identity Endpoint Ingress

## For the Implementing Agent

Start only after predecessors 04 and 05 have `complete.log`. Implement, run every command, and fill `CODE_REVIEW-cloud-G07.md` with actual evidence. Keep active files for official review; finalization is review-agent-only.

## Background

Preset-backed Chat and Anthropic Messages requests must join the Edge-local coordinator without trusting caller metadata and without changing legacy/provider-only ingress behavior.

## Dependencies and Execution Order

- Required predecessors: `04+02,03_preset_model_authorization` and `05+02,04_request_coordinator`.

## Analysis

### Files Read

- `agent-roadmap/sdd/knowledge-tool-optimization-extension/iop-hot-path-one-shot-execution/SDD.md`
- `apps/edge/internal/openai/server.go`
- `apps/edge/internal/openai/chat_handler.go`
- `apps/edge/internal/openai/chat_decode.go`
- `apps/edge/internal/openai/chat_types.go`
- `apps/edge/internal/openai/anthropic_handler.go`
- `apps/edge/internal/openai/anthropic_types.go`
- `apps/edge/internal/openai/stream_gate_ingress_test.go`
- `apps/edge/internal/openai/anthropic_surface_test.go`
- `agent-contract/outer/openai-compatible-api.md`
- `agent-contract/outer/anthropic-compatible-api.md`

### SDD Criteria

SDD scenario S05 requires full-history/frontier acceptance on both endpoints, caller-metadata spoof rejection, internal identity attachment, cross-principal/missing-state rejection, and legacy bypass.

### Verification Context

Deterministic endpoint fixtures and fake dispatch are sufficient; no external provider is required. Fresh/race tests are mandatory. Confidence: high.

### Test Coverage Gaps

No existing handler test spans calls through the new coordinator or proves that rejected preset continuations dispatch zero providers.

### Symbol References

`handleChatCompletions` and `handleAnthropicMessages` become the two ingress callers; provider-only paths remain unchanged.

### Split Judgment

This is the second refined child of the former request-identity pair. It consumes the stable coordinator contract and independently verifies two-protocol ingress integration.

### Scope Rationale

Exclude coordinator internals, mode transitions, workspace/artifact semantics, direct/light execution, cleanup, durable storage, and response-envelope redesign.

### Final Routing

`evaluation_mode=isolated-reassessment`; finalizer pair. Build closures are true; scores `(2,1,2,1,1)` yield G07/local-fit, matched risks `boundary_contract,variant_product` (2), so no risk boundary applies and the build is local `PLAN-local-G07.md`. Review uses the same scores and official cloud G07 in `CODE_REVIEW-cloud-G07.md`. No large context/rework/evidence failure/capability gap.

## Implementation Checklist

- [ ] Join preset-backed Chat and Messages begin/resume ingress to the coordinator.
- [ ] Reject caller identity spoofing, missing/cross-owner state, and mutations before provider dispatch while preserving legacy bypass.
- [x] Run dependency, focused handler, race, vet, and diff verification exactly as written.
- [x] Fill implementation-owned sections in CODE_REVIEW-*-G??.md with actual notes and output.

### [API-2] Join preset-backed endpoint ingress to the coordinator

#### Problem

Chat and Anthropic handlers dispatch routes directly, while caller metadata may contain arbitrary request-id-like values that cannot become authoritative.

#### Solution

At each preset-backed ingress derive the authenticated principal, decode canonical history/tools, and call coordinator Begin or Resume based only on server-issued public tool ids and history correlation. Attach internal request/call/stage ids without overwriting caller metadata and translate coordinator errors through existing endpoint-standard writers.

#### Modified Files and Checklist

- [ ] `apps/edge/internal/openai/server.go` — own/init the coordinator and test injection points.
- [ ] `apps/edge/internal/openai/chat_handler.go` — join preset Chat ingress.
- [ ] `apps/edge/internal/openai/anthropic_handler.go` — join preset Messages ingress.
- [ ] `apps/edge/internal/openai/request_identity_handler_test.go` — endpoint begin/resume/rejection tests.

#### Test Strategy

Write `TestPresetRequestIdentityAcrossChatTurns` and `TestPresetRequestIdentityAcrossAnthropicTurns`, plus cross-principal, missing-store, caller-metadata spoof, and legacy bypass cases. Fake dispatch must remain zero on rejection.

#### Verification

Run `go test -count=1 ./apps/edge/internal/openai -run 'TestPresetRequestIdentity'`; expect PASS.

## Modified Files Summary

| File | Items |
|------|-------|
| `apps/edge/internal/openai/server.go` | API-2 |
| `apps/edge/internal/openai/chat_handler.go` | API-2 |
| `apps/edge/internal/openai/anthropic_handler.go` | API-2 |
| `apps/edge/internal/openai/request_identity_handler_test.go` | API-2 |
| `agent-task/m-iop-hot-path-one-shot-execution/06+04,05_request_identity_ingress/CODE_REVIEW-cloud-G07.md` | API-2 |

## Final Verification

```bash
test -f agent-task/m-iop-hot-path-one-shot-execution/04+02,03_preset_model_authorization/complete.log
test -f agent-task/m-iop-hot-path-one-shot-execution/05+02,04_request_coordinator/complete.log
go test -race -count=1 ./apps/edge/internal/openai -run 'TestPresetRequestIdentity'
go test -race -count=1 ./packages/go/streamgate ./apps/edge/internal/openai ./apps/edge/internal/service
go vet ./apps/edge/internal/openai
git diff --check
```

Expected: all commands exit 0; rejected continuations dispatch zero providers and legacy routes bypass the coordinator.
