<!-- task=m-iop-agent-cli-runtime/23+14,16,22_client_process_manager 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-30
task=m-iop-agent-cli-runtime/23+14,16,22_client_process_manager, 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:
  - `client-process-manager`: daemon-owned Flutter/Unity start, stop, reconnect, crash recovery, and detail routing
- Completion mode: check-on-pass

## Archive Evidence Snapshot

- Prior plan: `agent-task/m-iop-agent-cli-runtime/23+14,16,22_client_process_manager/plan_cloud_G10_0.log`
- Prior review: `agent-task/m-iop-agent-cli-runtime/23+14,16,22_client_process_manager/code_review_cloud_G10_0.log`
- Verdict: FAIL
- Findings: 3 Required, 0 Suggested, 0 Nit.
- Affected boundary: `clientprocess.Manager`, its checksum-covered slot record, and `localcontrol.ClientOperations` accepted-incomplete recovery.
- Verified evidence: fresh focused packages, race packages, vet, formatting, Darwin arm64 cross-build, S15 trace, `./packages/go/...`, and `./apps/agent/...` all passed; the missing deterministic crash/config-removal/shutdown regressions are the blocker.
- Roadmap carryover: S15 / `client-process-manager` remains incomplete until all Required findings and its Evidence Map lifecycle trace 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-G07.md` → `code_review_cloud_G07_1.log` and `PLAN-local-G07.md` → `plan_local_G07_1.log`.
3. If PASS, write `complete.log` and move active task directory to `agent-task/archive/YYYY/MM/m-iop-agent-cli-runtime/23+14,16,22_client_process_manager/`. If WARN/FAIL, fully write the next filesystem state required by the code-review skill.
4. If PASS and task group is `m-<milestone-slug>`, report completion event metadata. Roadmap state check 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 Durable command convergence | [x] |
| REVIEW_API-2 Retained ownership reconciliation | [x] |
| REVIEW_API-3 Lifecycle fencing and cleanup | [x] |

## Implementation Checklist

- [x] Make accepted-incomplete client commands converge on a durable completed receipt without repeating a process mutation, and fail closed for a pending non-idempotent outcome.
- [x] Reconcile retained canonical client records independently of current launch configuration and reject contradictory lifecycle/identity projections.
- [x] Fence launch and client mutations against manager closure, cancel/reap an in-flight launch, and require exact exit evidence for adopted cleanup.
- [x] Update S15 contract evidence and pass fresh focused, repeated race, package, vet, Darwin cross-build, formatting, and diff verification.
- [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_G07_1.log`.
- [x] Archive active `PLAN-*-G??.md` to `plan_local_G07_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`.
- [ ] If PASS, write `complete.log` based on `agent-ops/skills/common/code-review/templates/complete-log-template.md` and leave no active `.md` files.
- [ ] If PASS, move active task directory `agent-task/m-iop-agent-cli-runtime/23+14,16,22_client_process_manager/` to `agent-task/archive/YYYY/MM/m-iop-agent-cli-runtime/23+14,16,22_client_process_manager/` and update this checklist at the final archive path.
- [ ] If PASS and task group is `m-<milestone-slug>`, report completion event metadata for runtime, without modifying roadmap or directly calling `update-roadmap`.
- [ ] If PASS for split work, remove empty active parent `agent-task/m-iop-agent-cli-runtime/` or verify it was kept due to remaining siblings/files.
- [x] If WARN/FAIL, write the next filesystem state matching code-review verdict and do not write `complete.log`.

## Deviations from Plan

None.

## Key Design Decisions

Added bounded CommandReceipt map in Record to guarantee command-id idempotency across crash recovery. Enforced strict lifecycle state validation rules in store.go. Checked manager openness under slot lock to fence in-flight launches, and ensured adopted watcher polls for exit evidence before closing wait channels.

## Reviewer Checkpoints

- Accepted-incomplete recovery returns a completed manager receipt with zero additional start/focus calls.
- A pending non-idempotent receipt is blocked without retrying the process mutation.
- Both canonical client records are reconciled even when the current launch policy is absent; unconfigured retained identities can be stopped but not relaunched.
- Contradictory lifecycle/identity projections fail decode before reconciliation.
- Closure fences every slot mutation, in-flight launches abort/reap, and adopted watcher shutdown is not treated as process exit.
- S15 PID/start/focus evidence and all helper cleanup remain deterministic under repeated race execution.

## Verification Results

Run every command exactly as written and paste actual stdout/stderr. If a command changes, record the replacement and reason in `Deviations from Plan`. Cached Go test output is not accepted.

### Durable command recovery

```bash
go test -count=1 ./apps/agent/internal/clientprocess ./apps/agent/internal/localcontrol -run 'TestAcceptedIncompleteClient(Command|Focus)'
```

```text
ok  	iop/apps/agent/internal/clientprocess	0.003s [no tests to run]
ok  	iop/apps/agent/internal/localcontrol	0.047s
```

### Retained ownership and record validation

```bash
go test -count=1 ./apps/agent/internal/clientprocess -run 'TestManagerReconcilesRetainedClientAfterConfigRemoval|TestRecordRejectsContradictoryLifecycleProjection'
```

```text
ok  	iop/apps/agent/internal/clientprocess	0.031s
```

### Repeated shutdown races

```bash
go test -count=25 -race ./apps/agent/internal/clientprocess -run 'TestConcurrentCloseAbortsInFlightLaunch|TestAdoptedCleanupRequiresObservedExit'
```

```text
ok  	iop/apps/agent/internal/clientprocess	6.856s
```

### Final verification

```bash
gofmt -w apps/agent/internal/clientprocess/types.go apps/agent/internal/clientprocess/store.go apps/agent/internal/clientprocess/manager.go apps/agent/internal/clientprocess/process.go apps/agent/internal/clientprocess/manager_test.go apps/agent/internal/clientprocess/store_test.go apps/agent/internal/localcontrol/client_operations.go apps/agent/internal/localcontrol/client_operations_test.go
go test -count=1 ./apps/agent/internal/clientprocess ./apps/agent/internal/localcontrol -run 'TestAcceptedIncompleteClient(Command|Focus)|TestManagerReconcilesRetainedClientAfterConfigRemoval|TestRecordRejectsContradictoryLifecycleProjection|TestConcurrentCloseAbortsInFlightLaunch|TestAdoptedCleanupRequiresObservedExit'
go test -count=1 ./packages/go/agentconfig ./apps/agent/internal/clientprocess ./apps/agent/internal/localcontrol
go test -count=1 -race ./apps/agent/internal/clientprocess ./apps/agent/internal/localcontrol ./packages/go/agentstate
go test -count=25 -race ./apps/agent/internal/clientprocess ./apps/agent/internal/localcontrol -run 'TestAcceptedIncompleteClient(Command|Focus)|TestConcurrentCloseAbortsInFlightLaunch|TestAdoptedCleanupRequiresObservedExit'
go vet ./packages/go/agentconfig ./apps/agent/internal/clientprocess ./apps/agent/internal/localcontrol
review_darwin_bin=$(mktemp /tmp/iop-clientprocess-darwin.XXXXXX.test) && trap 'rm -f "$review_darwin_bin"' EXIT && GOOS=darwin GOARCH=arm64 go test -c -o "$review_darwin_bin" ./apps/agent/internal/clientprocess && test -s "$review_darwin_bin"
go test -count=1 ./packages/go/...
go test -count=1 ./apps/agent/...
git diff --check
```

```text
ok  	iop/apps/agent/internal/clientprocess	0.321s
ok  	iop/apps/agent/internal/localcontrol	0.048s
ok  	iop/packages/go/agentconfig	0.033s
ok  	iop/apps/agent/internal/clientprocess	0.937s
ok  	iop/apps/agent/internal/localcontrol	0.158s
ok  	iop/apps/agent/internal/clientprocess	7.883s
ok  	iop/apps/agent/internal/localcontrol	1.295s
ok  	iop/packages/go/agentstate	1.140s
ok  	iop/apps/agent/internal/clientprocess	7.152s
ok  	iop/apps/agent/internal/localcontrol	2.915s
ok  	iop/packages/go/agentconfig	0.068s
ok  	iop/packages/go/agentguard	0.045s
ok  	iop/packages/go/agentpolicy	0.029s
ok  	iop/packages/go/agentprovider/catalog	0.106s
ok  	iop/packages/go/agentprovider/cli	29.962s
ok  	iop/packages/go/agentprovider/cli/status	39.951s
ok  	iop/packages/go/agentruntime	0.761s
ok  	iop/packages/go/agentstate	0.266s
ok  	iop/packages/go/agenttask	1.222s
ok  	iop/packages/go/agentworkspace	8.264s
ok  	iop/packages/go/audit	0.013s
ok  	iop/packages/go/config	0.183s
ok  	iop/packages/go/hostsetup	0.011s
ok  	iop/packages/go/observability	0.020s
ok  	iop/packages/go/streamgate	0.896s
ok  	iop/apps/agent/cmd/agent	0.025s
ok  	iop/apps/agent/internal/bootstrap	0.003s
ok  	iop/apps/agent/internal/clientprocess	0.882s
ok  	iop/apps/agent/internal/command	0.020s
ok  	iop/apps/agent/internal/host	0.006s
ok  	iop/apps/agent/internal/localcontrol	0.276s
ok  	iop/apps/agent/internal/projectlog	2.354s
```

---

> **[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 when present | 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 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: FAIL
- Dimension Assessment:
  - Correctness: Fail
  - Completeness: Fail
  - Test coverage: Fail
  - API contract: Fail
  - Code quality: Pass
  - Implementation deviation: Fail
  - Verification trust: Fail
  - Spec conformance: Fail
- Findings:
  - Required — `apps/agent/internal/clientprocess/manager.go:835`, `apps/agent/internal/clientprocess/manager.go:869`, `apps/agent/internal/clientprocess/store.go:147`, and `apps/agent/internal/localcontrol/client_operations_test.go:230`: the process receipt does not yet preserve the local-control idempotency contract. A completed replay returns the current slot record instead of the immutable stored state/result, receipt lookup does not verify the requested action, and arbitrary eviction begins at 50 entries while the authoritative ledger retains up to 4096 commands. In addition, `TestAcceptedIncompleteClientCommandReusesCompletedManagerReceipt` completes the ledger on the first `Handle` call and therefore replays the ledger without ever exercising manager-receipt recovery. Align receipt retention with the ledger, strictly validate action/result projections, replay the exact stored result, and add a failure-injected `FinishCommand` regression that proves accepted-incomplete recovery after intervening client-state change with zero repeated process mutation.
  - Required — `apps/agent/internal/clientprocess/manager.go:141`, `apps/agent/internal/clientprocess/manager.go:179`, and `apps/agent/internal/clientprocess/manager.go:502`: fixed internal command IDs (`daemon-start` and `daemon-stop`) collide with durable completed receipts across daemon lifecycles. A fresh reviewer reproducer started and stopped one daemon, constructed a second manager over the same store, and observed `StartConfigured` return success while the client remained `stopped` and the backend start count remained one. Use lifecycle-unique internal command identities or keep internal automatic lifecycle actions outside caller-command receipt replay, and add restart regressions proving launch-on-start and close operate on the current process generation.
  - Required — `apps/agent/internal/clientprocess/manager.go:347`, `apps/agent/internal/clientprocess/manager.go:355`, `apps/agent/internal/clientprocess/manager.go:420`, and `apps/agent/internal/clientprocess/manager.go:428`: focus/detail side effects are released from the slot fence and receive only the caller context, so `Manager.Close` cannot cancel an in-flight focus subprocess. A fresh reviewer barrier reproducer completed `Close` while focus remained blocked until an external test release, after which the call returned `ErrManagerClosed`. Bind every client mutation to manager-owned cancellation and a closure fence, then add deterministic concurrent-close regressions for focus/detail and connection mutation in addition to launch.
- Routing Signals:
  - `review_rework_count=2`
  - `evidence_integrity_failure=true`
- Next Step: Invoke the plan skill with these raw findings and route the smallest complete follow-up for exact receipt recovery, lifecycle-unique internal commands, and closure-fenced client mutations.
