<!-- task=m-iop-agent-cli-runtime/07+06_target_policy plan=1 tag=REVIEW_API -->

# Code Review Reference - 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-07-29
task=m-iop-agent-cli-runtime/07+06_target_policy, plan=1, tag=REVIEW_API

## Roadmap Targets

- Milestone: `agent-roadmap/phase/automation-runtime-bridge/milestones/iop-agent-cli-runtime.md`
- Milestone link: [Milestone document](agent-roadmap/phase/automation-runtime-bridge/milestones/iop-agent-cli-runtime.md)
- Task ids:
  - `target-policy`: ordered evaluator and durable route history
- Completion mode: check-on-pass

## Archive Evidence Snapshot

- Prior plan: `agent-task/m-iop-agent-cli-runtime/07+06_target_policy/plan_local_G07_0.log`
- Prior review: `agent-task/m-iop-agent-cli-runtime/07+06_target_policy/code_review_cloud_G07_0.log`
- Verdict: `FAIL`
- Findings: 5 Required, 0 Suggested, 0 Nit.
- Affected files: `packages/go/agentpolicy/decision.go`, `packages/go/agentpolicy/evaluator.go`, `packages/go/agentpolicy/evaluator_test.go`, `packages/go/agenttask/ports.go`, `packages/go/agenttask/manager_test.go`.
- Verification evidence: the existing `agentpolicy` suite passed; reviewer-only payload and unresolved-profile reproducers failed as expected; the required combined package command failed; `go test -count=20 -run '^TestIntegrationLeaseRenewsAndFencesLateResult$' ./packages/go/agenttask` also failed.
- Roadmap carryover: `target-policy` remains incomplete until S06 project-aware selection, immutable route replay/history, corruption rejection, and deterministic verification all pass.

## 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_1.log` and `PLAN-cloud-G09.md` → `plan_cloud_G09_1.log`.
3. If PASS, write `complete.log` and move active task directory to `agent-task/archive/YYYY/MM/m-iop-agent-cli-runtime/07+06_target_policy/`. If WARN/FAIL, fully write the next filesystem state required by the code-review skill.
4. If PASS, report the `m-iop-agent-cli-runtime` completion event metadata. Roadmap state checks and `update-roadmap` calls are runtime responsibilities.
5. Check applicable `Review-Only Checklist` items at the final `.log` location before reporting.

---

## Implementation Item Completion

| Item | Status |
|------|---------|
| REVIEW_API-1 Resolve effective project policy and target identity | [x] |
| REVIEW_API-2 Seal and replay durable route evidence | [x] |
| REVIEW_API-3 Make required lease verification deterministic | [x] |

## Implementation Checklist

- [x] Resolve the effective project selection policy and require one catalog-backed provider/model/profile identity with immutable revisions.
- [x] Integrity-seal the complete route decision, preserve ordered candidate/selected reason/used history, and resume a valid persisted route without reevaluation.
- [x] Add the S06 regression matrix for project overrides, unresolved profiles, quota predicates, payload mutations, and stable persisted-route replay.
- [x] Stabilize the integration-lease fencing test and prove it passes repeatedly before the full package verification.
- [x] Run the local platform-common profile and all fresh target verification commands without generated-file drift.
- [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_G10_1.log`.
- [x] Archive active `PLAN-*-G??.md` to `plan_cloud_G09_1.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-agent-cli-runtime/07+06_target_policy/` to `agent-task/archive/YYYY/MM/m-iop-agent-cli-runtime/07+06_target_policy/` and update this checklist at the final archive path.
- [x] If PASS, report completion event metadata for runtime without modifying roadmap or directly calling `update-roadmap`.
- [ ] If PASS, remove the empty active parent only when no sibling tasks remain.
- [ ] If WARN/FAIL, write the next filesystem state matching the verdict and do not write `complete.log`.

## Deviations from Plan

None. The implementation stayed within the five planned files and all verification commands were run unchanged.

## Key Design Decisions

- `SelectionContext.ProjectID` selects either the immutable global policy or the matching effective project policy. Unknown projects fail with `ErrUnknownProject`; all successful targets are resolved against the catalog and receive a config-derived immutable profile revision.
- `RouteDecision` now carries explicit JSON fields for the selected reason, every ordered rule/default candidate, and actual used-route history. Rules after the first match remain present as unevaluated evidence.
- The durable envelope uses strict single-document JSON decoding, rejects unknown fields and non-canonical payloads, and seals length-prefixed version, config revision, selection revision, and canonical decision bytes with SHA-256. Envelope/payload disagreement, structural gaps, and target/history mutations fail as `ErrCorruptDecision`.
- `ResumePolicy` pins both config and effective selection revisions, verifies every candidate and used target against the current immutable catalog/policy identities, and returns the persisted decision without re-running predicates.
- The integration fencing test now waits for one snapshot containing both `integrating` state and the manager-owned integration lease, eliminating the pre-claim `pending_integration` race.

## Reviewer Checkpoints

- An effective project override selects its own first matching target and persists the project selection revision.
- Every successful decision has resolved provider/model/profile identities and an immutable profile/config revision.
- Any mutation of the versioned route payload, revisions, target identity, or history is rejected before resume; valid resume returns the pinned decision without rule reevaluation.
- Candidate evidence records ordered evaluated/rejected/selected status, selected reason, and used-route history.
- The integration-lease fencing test passes 20 fresh repetitions and the complete target suites pass under normal and race execution.

## Verification Results

Replace each instruction line below with the command's exact stdout/stderr. If a command changes, record the replacement and reason in `Deviations from Plan`.

### `command -v go && readlink -f "$(command -v go)" && go version && go env GOROOT`

```text
/config/.local/bin/go
/config/opt/go/bin/go
go version go1.26.2 linux/arm64
/config/opt/go
```

### `make proto`

```text
protoc \
	--go_out=. \
	--go_opt=module=iop \
	--proto_path=. \
	proto/iop/runtime.proto \
	proto/iop/node.proto \
	proto/iop/control.proto \
	proto/iop/job.proto
```

### `go test -count=1 ./apps/edge/internal/transport ./apps/node/internal/transport`

```text
ok  	iop/apps/edge/internal/transport	4.971s
ok  	iop/apps/node/internal/transport	5.870s
```

### `go test -count=1 -run 'TestEvaluator(UsesProjectSelectionOverride|RejectsUnknownProject|RejectsUnresolvedProfile|FirstMatchWins)$' -v ./packages/go/agentpolicy`

```text
=== RUN   TestEvaluatorFirstMatchWins
--- PASS: TestEvaluatorFirstMatchWins (0.00s)
=== RUN   TestEvaluatorUsesProjectSelectionOverride
--- PASS: TestEvaluatorUsesProjectSelectionOverride (0.00s)
=== RUN   TestEvaluatorRejectsUnknownProject
--- PASS: TestEvaluatorRejectsUnknownProject (0.00s)
=== RUN   TestEvaluatorRejectsUnresolvedProfile
--- PASS: TestEvaluatorRejectsUnresolvedProfile (0.00s)
PASS
ok  	iop/packages/go/agentpolicy	0.004s
```

### `go test -count=1 -run 'Test(EncodeDecodeDecision|DecodeDecision|ResumePolicy|EvaluatorQuota|EvaluatorOrderedCandidate)' -v ./packages/go/agentpolicy`

```text
=== RUN   TestEncodeDecodeDecisionRoundTrip
--- PASS: TestEncodeDecodeDecisionRoundTrip (0.00s)
=== RUN   TestDecodeDecisionRejectsCorruptJSON
--- PASS: TestDecodeDecisionRejectsCorruptJSON (0.00s)
=== RUN   TestDecodeDecisionRejectsWrongVersion
--- PASS: TestDecodeDecisionRejectsWrongVersion (0.00s)
=== RUN   TestDecodeDecisionRejectsRevisionMismatch
--- PASS: TestDecodeDecisionRejectsRevisionMismatch (0.00s)
=== RUN   TestDecodeDecisionRequiresCompleteExpectation
--- PASS: TestDecodeDecisionRequiresCompleteExpectation (0.00s)
=== RUN   TestDecodeDecisionRejectsMutationMatrix
=== RUN   TestDecodeDecisionRejectsMutationMatrix/envelope_config_revision
=== RUN   TestDecodeDecisionRejectsMutationMatrix/payload_config_revision
=== RUN   TestDecodeDecisionRejectsMutationMatrix/payload_selection_revision
=== RUN   TestDecodeDecisionRejectsMutationMatrix/provider_identity
=== RUN   TestDecodeDecisionRejectsMutationMatrix/model_identity
=== RUN   TestDecodeDecisionRejectsMutationMatrix/profile_identity
=== RUN   TestDecodeDecisionRejectsMutationMatrix/profile_revision
=== RUN   TestDecodeDecisionRejectsMutationMatrix/candidate_history
=== RUN   TestDecodeDecisionRejectsMutationMatrix/used_route_history
=== RUN   TestDecodeDecisionRejectsMutationMatrix/decision_history_identity
=== RUN   TestDecodeDecisionRejectsMutationMatrix/integrity_value
=== RUN   TestDecodeDecisionRejectsMutationMatrix/unknown_payload_field_with_matching_seal
=== RUN   TestDecodeDecisionRejectsMutationMatrix/envelope_payload_disagreement_with_matching_seal
--- PASS: TestDecodeDecisionRejectsMutationMatrix (0.00s)
    --- PASS: TestDecodeDecisionRejectsMutationMatrix/envelope_config_revision (0.00s)
    --- PASS: TestDecodeDecisionRejectsMutationMatrix/payload_config_revision (0.00s)
    --- PASS: TestDecodeDecisionRejectsMutationMatrix/payload_selection_revision (0.00s)
    --- PASS: TestDecodeDecisionRejectsMutationMatrix/provider_identity (0.00s)
    --- PASS: TestDecodeDecisionRejectsMutationMatrix/model_identity (0.00s)
    --- PASS: TestDecodeDecisionRejectsMutationMatrix/profile_identity (0.00s)
    --- PASS: TestDecodeDecisionRejectsMutationMatrix/profile_revision (0.00s)
    --- PASS: TestDecodeDecisionRejectsMutationMatrix/candidate_history (0.00s)
    --- PASS: TestDecodeDecisionRejectsMutationMatrix/used_route_history (0.00s)
    --- PASS: TestDecodeDecisionRejectsMutationMatrix/decision_history_identity (0.00s)
    --- PASS: TestDecodeDecisionRejectsMutationMatrix/integrity_value (0.00s)
    --- PASS: TestDecodeDecisionRejectsMutationMatrix/unknown_payload_field_with_matching_seal (0.00s)
    --- PASS: TestDecodeDecisionRejectsMutationMatrix/envelope_payload_disagreement_with_matching_seal (0.00s)
=== RUN   TestEvaluatorQuotaAndMinimumTokenOrdering
=== RUN   TestEvaluatorQuotaAndMinimumTokenOrdering/at_minimum_first_match
=== RUN   TestEvaluatorQuotaAndMinimumTokenOrdering/below_minimum_fallback
--- PASS: TestEvaluatorQuotaAndMinimumTokenOrdering (0.00s)
    --- PASS: TestEvaluatorQuotaAndMinimumTokenOrdering/at_minimum_first_match (0.00s)
    --- PASS: TestEvaluatorQuotaAndMinimumTokenOrdering/below_minimum_fallback (0.00s)
=== RUN   TestEvaluatorOrderedCandidateEvidence
--- PASS: TestEvaluatorOrderedCandidateEvidence (0.00s)
=== RUN   TestResumePolicyReturnsPinnedDecisionWithoutReselection
--- PASS: TestResumePolicyReturnsPinnedDecisionWithoutReselection (0.00s)
=== RUN   TestResumePolicyRejectsCorruptOrForeignTarget
--- PASS: TestResumePolicyRejectsCorruptOrForeignTarget (0.00s)
=== RUN   TestResumePolicyRejectsResealedKnownTargetMutation
--- PASS: TestResumePolicyRejectsResealedKnownTargetMutation (0.00s)
PASS
ok  	iop/packages/go/agentpolicy	0.066s
```

### `go test -count=20 -run '^TestIntegrationLeaseRenewsAndFencesLateResult$' ./packages/go/agenttask`

```text
ok  	iop/packages/go/agenttask	0.128s
```

### `go test -count=1 ./packages/go/agentpolicy ./packages/go/agenttask`

```text
ok  	iop/packages/go/agentpolicy	0.016s
ok  	iop/packages/go/agenttask	0.485s
```

### `go test -count=1 -race ./packages/go/agentpolicy ./packages/go/agenttask`

```text
ok  	iop/packages/go/agentpolicy	1.157s
ok  	iop/packages/go/agenttask	1.503s
```

### `go vet ./packages/go/agentpolicy ./packages/go/agenttask`

```text
(no stdout/stderr; exit code 0)
```

### `gofmt -d packages/go/agentpolicy packages/go/agenttask/ports.go packages/go/agenttask/manager_test.go`

```text
(no stdout/stderr; exit code 0)
```

### `git diff --check`

```text
(no stdout/stderr; exit code 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) |
| Roadmap Targets | Fixed at stub creation from plan | Implementing agent must not modify; code-review copies it into `complete.log` as `Roadmap Completion` only on PASS |
| Archive Evidence Snapshot | Fixed at stub creation from plan | Implementing agent uses it as default prior-loop context; read only the specific archive files cited there when more detail is required |
| Agent UI Completion | Mixed | Omitted for this non-UI task |
| 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=1, evidence_integrity_failure=false
- Next Step: Archive the active pair, write `complete.log`, move the task to the completion archive, and report the Milestone completion event metadata.
