<!-- task=m-iop-owned-single-request-agent-execution/12+05,06,08,11_internal_tool_loop plan=0 tag=API milestone-task=tool-loop -->

# Code Review Reference - 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.
> Execute the plan's selected root cause, scope, files, and dependency decisions as written. Do not choose another owner, narrow/expand the write boundary, or replace a fix with another verification attempt.
> 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-06
task=m-iop-owned-single-request-agent-execution/12+05,06,08,11_internal_tool_loop, plan=0, tag=API

## 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-G10.md` → `code_review_cloud_G10_0.log` and `PLAN-cloud-G09.md` → `plan_cloud_G09_0.log`.
3. If PASS, write `complete.log` and move active task directory to `agent-task/archive/YYYY/MM/m-iop-owned-single-request-agent-execution/12+05,06,08,11_internal_tool_loop/`. If WARN/FAIL, fully write the next filesystem state required by the code-review skill.
4. If PASS, preserve the first-line `milestone-task=tool-loop` metadata in `complete.log` and report it for runtime aggregation. 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 |
|------|---------|
| API-1 Define canonical internal tool continuation | [x] |
| API-2 Execute and resume the saved stage internally | [x] |
| API-3 Prove no external continuation at the HTTP boundary | [x] |

## Implementation Checklist

- [x] Define closed canonical internal workspace calls/results and strict per-operation decoding independent of caller-facing tool codecs.
- [x] Execute ordered calls through the admitted generation, correlate exactly one pending call/result, resume only the saved stage, and enforce immutable iteration/output/deadline budgets.
- [x] Propagate cancellation and every malformed/stale/denied/exhausted outcome internally with no external continuation or fallback/reselection.
- [x] Prove a real marked Anthropic POST performs multiple Node round trips yet emits no public tool protocol or second ingress, then synchronize contracts/specs.
- [x] Run all dependency, focused race, endpoint, package, vet, documentation, and whitespace verification.
- [x] Fill implementation-owned sections in CODE_REVIEW-*-G??.md with actual implementation notes and verification output.

## Review-Only Checklist

> **[REVIEW AGENT ONLY]** Implementers must not modify/check this section.

- [x] Append verdict, routing signals, dimensions, and findings.
- [x] Archive the active pair to routed suffix `0` logs and verify `.gitignore`.
- [x] On PASS write `complete.log`, preserve/report Milestone metadata, move this directory, and keep the parent while siblings remain.
- [ ] On WARN/FAIL write only the official next loop state.

## Deviations from Plan

None.

## Key Design Decisions

- The continuation remains an optional executor capability. Existing executors that emit no internal tool call remain compatible, while an executor that emits one without implementing the continuation fails before any workspace wire effect.
- The service owns five closed operation schemas and recursively rejects duplicate JSON keys, unknown fields, trailing data, malformed identities, non-canonical paths, executable/argv input, and invalid environment names without importing caller-facing codecs.
- Workspace admission now freezes allowed environment names in addition to operations and command IDs. The loop performs operation, command, environment, write-size, identity, iteration, output, deadline, and request-wall-clock checks against defensive copies; command timeout is the lower of the remaining stage budget and frozen workspace maximum.
- One request-local loop opens the admitted workspace once, preserves the exact connection generation, permits one pending call at a time, rejects repeated tool IDs globally, and resumes only the canonical saved plan, work, or review stage. Repair shares the review-stage budget.
- Continuations receive only deep-copied typed result fields. Raw Node errors, arguments, workspace authority, command details, and internal tool protocol do not enter surface progress or terminal output.
- The real HTTP evidence uses one marked Anthropic POST and an actual in-process workspace wire exchange. No production handler change was required. Provider-specific stage drivers, artifact cleanup, and live Claude qualification remain deferred as planned.

## Reviewer Checkpoints

- Confirm service-owned schemas do not call route-01 caller codecs.
- Confirm packet 06 completed before this packet changed the shared Anthropic contract and input spec; no streaming implementation was pulled into this packet.
- Confirm strict decode/capability checks precede wire effects and tool calls execute in order.
- Confirm exact request/stage/tool/generation correlation, budget enforcement, and one continuation delivery.
- Confirm cancellation sends Node cancel and no tool event reaches surface progress/terminal.
- Confirm the real HTTP assertion proves one ingress, multiple tool round trips, no `tool_use`, and one terminal.

## Verification Results

Paste actual stdout/stderr for every command and record replacements under deviations.

### 1. Packet 05 dependency

`test -f agent-task/m-iop-owned-single-request-agent-execution/05+03_single_ingress/complete.log || test "$(compgen -G 'agent-task/archive/*/*/m-iop-owned-single-request-agent-execution/05+03_single_ingress/complete.log' | wc -l)" -eq 1`

```text
PASS (exit 0; no stdout/stderr).
```

### 2. Packet 06 dependency

`test -f agent-task/m-iop-owned-single-request-agent-execution/06+05_stream_terminal/complete.log || test "$(compgen -G 'agent-task/archive/*/*/m-iop-owned-single-request-agent-execution/06+05_stream_terminal/complete.log' | wc -l)" -eq 1`

```text
PASS (exit 0; no stdout/stderr).
```

### 3. Packet 08 dependency

`test -f agent-task/m-iop-owned-single-request-agent-execution/08+03,07_workspace_admission/complete.log || test "$(compgen -G 'agent-task/archive/*/*/m-iop-owned-single-request-agent-execution/08+03,07_workspace_admission/complete.log' | wc -l)" -eq 1`

```text
PASS (exit 0; no stdout/stderr).
```

### 4. Packet 11 dependency

`test -f agent-task/m-iop-owned-single-request-agent-execution/11+10_workspace_command/complete.log || test "$(compgen -G 'agent-task/archive/*/*/m-iop-owned-single-request-agent-execution/11+10_workspace_command/complete.log' | wc -l)" -eq 1`

```text
PASS (exit 0; no stdout/stderr).
```

### 5. Service race tests

`go test -race ./apps/edge/internal/service -run 'Test(InternalWorkspaceTool|SingleRequestInternalToolLoop)' -count=1`

```text
ok  	iop/apps/edge/internal/service	1.104s
```

### 6. HTTP evidence

`go test ./apps/edge/internal/openai -run 'TestAnthropicSingleRequest(UsesOnePost|InternalToolsStayPrivate)' -count=1`

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

### 7. Package regression

`go test ./apps/edge/internal/service ./apps/edge/internal/openai -count=1`

```text
ok  	iop/apps/edge/internal/service	6.222s
ok  	iop/apps/edge/internal/openai	7.948s
```

### 8. Vet

`go vet ./apps/edge/internal/service ./apps/edge/internal/openai`

```text
PASS (exit 0; no stdout/stderr).
```

### 9. Contract/spec search

`rg --sort path -n 'internal tool|tool_use|second|workspace|defer' agent-contract/outer/anthropic-compatible-api.md agent-spec/input/openai-compatible-surface.md agent-spec/runtime/edge-node-execution.md`

```text
PASS (exit 0; 64 matching lines across all three selected contract/spec files).
```

### 10. Whitespace

`git diff --check`

```text
PASS (exit 0; no stdout/stderr).
```

---

> **[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=0`, `evidence_integrity_failure=false`
- Next Step: Archive the reviewed pair, write `complete.log`, and move the completed task directory to the monthly archive.
