<!-- task=m-iop-hot-path-one-shot-execution/13+12_outer_turn_integration plan=1 tag=API milestone-task=terminal-control -->

# Hot Path direct/light outer-turn integration

## For the Implementing Agent

구현 후 `CODE_REVIEW-cloud-G09.md`의 구현 담당 섹션에 실제 변경·검증 출력을 채우고 active 파일을 유지한다. 차단 시 정확한 blocker와 재개 조건만 기록하며 archive/`complete.log` 작성이나 상태 판정은 하지 않는다.

## Background

Child 12의 stage-scoped gate와 HTTP-turn sequencer를 selector/direct/light lifecycle에 연결한다. 한 inbound HTTP 요청마다 새 outer turn을 만들고, local→review처럼 agent roundtrip이 없는 내부 전이는 같은 turn에서 이어가되 tool call HTTP terminal 뒤에는 writer를 보존하지 않는다.

## Archive Evidence Snapshot

- 이전 active plan/review pair는 구현 전에 source reanalysis로 대체됐다. 구현 evidence와 verdict는 없다.

## Analysis

### Files Read

- `agent-roadmap/sdd/knowledge-tool-optimization-extension/iop-hot-path-one-shot-execution/SDD.md`
- `apps/edge/internal/openai/hot_path_dispatch.go`
- `apps/edge/internal/openai/hot_path_direct.go`
- `apps/edge/internal/openai/hot_path_light.go`
- `apps/edge/internal/openai/hot_path_cleanup.go`
- `apps/edge/internal/openai/hot_path_direct_test.go`
- `apps/edge/internal/openai/hot_path_light_test.go`
- `agent-test/local/rules.md`

### SDD Criteria

- 승인 SDD S10: one outer envelope, nested response start/terminal suppression, delta ordering, per-turn usage sum and caller output-cap enforcement.
- direct/tool turn은 endpoint terminal을 commit한다. local completion 뒤 review/repair는 same HTTP turn에서 이어질 수 있다.

### Verification Context

- external runtime 없이 fake provider/stage fixture와 fresh race tests로 닫는다.

### Test Coverage Gaps

- already-dispatched selector result부터 후속 stage까지 한 turn을 공유하는 경로, remaining cap 전달, compatibility response 동등성 test가 없다.

### Symbol References

- public rename/remove 없음. Child 12 내부 contract만 소비한다.

### Split Judgment

- stable contract: child 12 core → selector/direct/light lifecycle. Caller protocol encoding은 child 14/15에 남긴다.

### Scope Rationale

- endpoint native wire/error matrix, observation, smoke는 제외한다.

### Final Routing

- evaluation_mode=isolated-reassessment, finalizer=`finalize-task-policy.sh pair`.
- build scores=2/2/1/1/2, risks=`temporal_state,concurrent_consistency,boundary_contract,variant_product`(4), risk-boundary → `PLAN-cloud-G08.md`.
- review → `CODE_REVIEW-cloud-G09.md`; `large_indivisible_context=false`, recovery=0/false.

## Implementation Checklist

- [ ] [API-1] Wire the already-dispatched selector result and direct/light follow-up stages through one HTTP-turn sequencer while propagating remaining output budget.
- [ ] [API-2] Add compatibility, transition, response-start/terminal, and no-post-terminal regression evidence.
- [ ] Fill implementation-owned sections in CODE_REVIEW-*-G??.md with actual implementation notes and verification output.

### [API-1] Lifecycle integration

**Problem:** current selector/direct/light collectors complete independently, and stage request builders reuse the original output limit rather than an outer-turn remaining budget.

**Solution:** Initialize exactly one outer turn per inbound preset HTTP request. Feed the existing initial `ProviderPoolDispatchResult` into its first stage runtime instead of redispatching. Replace each subsequent provider stage with a new stage runtime while retaining the outer sequencer, propagate normalized remaining output budget through `hotPathDispatchSnapshot` and stage body builders, hold only internal transition terminals, and terminate the HTTP turn on direct/tool/error/cap completion. Keep the compatibility accumulator so existing complete-response behavior remains stable until endpoint codecs are connected.

**Modified Files and Checklist:**

- [ ] Modify `apps/edge/internal/openai/hot_path_dispatch.go` to run initial/subsequent dispatch results through stage-scoped runtimes and carry remaining budget.
- [ ] Modify `apps/edge/internal/openai/hot_path_direct.go` to consume the shared turn/compatibility result without a second collector.
- [ ] Modify `apps/edge/internal/openai/hot_path_light.go` to retain the same turn across local/review/repair and stop after an outer terminal.

**Test Strategy:** direct, tool, local-pass, local-review, repair, cap, and terminal-stop rows with exact dispatch counts.

**Verification:** targeted API-2 command exits 0.

### [API-2] Integration evidence

**Problem:** ordinary direct/light tests do not prove stage replacement inside one HTTP turn.

**Solution:** Extend the core fixture with already-dispatched initial results and multi-stage sequences. Assert no duplicate dispatch, one response-start, ordered deltas, per-stage terminal interception, summed usage, decreasing cap, compatibility output equivalence, and no provider work after outer terminal/cancel.

**Modified Files and Checklist:**

- [ ] Extend `apps/edge/internal/openai/hot_path_terminal_control_test.go` with outer-turn integration and compatibility cases.
- [ ] Record actual output in `agent-task/m-iop-hot-path-one-shot-execution/13+12_outer_turn_integration/CODE_REVIEW-cloud-G09.md`.

**Test Strategy:** preserve existing direct/light/cleanup regressions and compare exact transition trace.

**Verification:** run Final Verification; all commands exit 0 without race.

## Dependencies and Execution Order

1. Directory dependency `12` must produce `agent-task/m-iop-hot-path-one-shot-execution/12+10,11_outer_turn_core/complete.log`.
2. Implement API-1, then API-2.

## Modified Files Summary

| File | Item |
|---|---|
| `apps/edge/internal/openai/hot_path_dispatch.go` | API-1 |
| `apps/edge/internal/openai/hot_path_direct.go` | API-1 |
| `apps/edge/internal/openai/hot_path_light.go` | API-1 |
| `apps/edge/internal/openai/hot_path_terminal_control_test.go` | API-2 |
| `agent-task/m-iop-hot-path-one-shot-execution/13+12_outer_turn_integration/CODE_REVIEW-cloud-G09.md` | API-2 |

## Final Verification

```bash
go test -race -count=1 ./apps/edge/internal/openai -run 'TestHotPath(OuterTurnIntegration|OuterTurnCompatibility|Direct|Light|Cleanup)'
go test -race -count=1 ./packages/go/streamgate ./packages/go/config ./apps/edge/internal/openai ./apps/edge/internal/service
git diff --check
```

Expected: exit 0, one sequencer per HTTP request, no redispatch/nested terminal, stable compatibility output, correct remaining cap, no race.

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