<!-- task=m-iop-agent-cli-runtime/15+13_host_lifecycle plan=0 tag=API -->

# Code Review Reference - API

> **[IMPLEMENTING AGENT — READ FIRST] Filling in this file is mandatory.**
> Complete the checklist and implementation-owned evidence, leave active files in place, and report ready for review. Finalization is review-agent-only.

## Overview

date=2026-07-29
task=m-iop-agent-cli-runtime/15+13_host_lifecycle, plan=0, tag=API

## For the Review Agent

> **[REVIEW AGENT ONLY]** Implementing agents must not execute finalization.

Compare lifecycle behavior and actual output to source. Append verdict/signals; archive `CODE_REVIEW-cloud-G06.md` → `code_review_cloud_G06_0.log` and `PLAN-local-G06.md` → `plan_local_G06_0.log`; on PASS write `complete.log` and archive the task; on WARN/FAIL materialize the next state.

## Implementation Item Completion

| Item | Status |
|------|--------|
| API-2 Host lifecycle | [x] |

## Implementation Checklist

- [x] Implement and test the standalone host lifecycle and dependency ports without duplicating shared runtime behavior.
- [x] Run fresh focused, race, vet, 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]** Implementing agents must not modify this checklist.

- [x] Append one verdict and verified routing signals.
- [x] Verify verdict, dimensions, and finding classifications.
- [x] Archive `CODE_REVIEW-cloud-G06.md` as `code_review_cloud_G06_0.log`.
- [x] Archive `PLAN-local-G06.md` as `plan_local_G06_0.log`.
- [x] Verify the Agent-Ops managed `.gitignore` block.
- [ ] If PASS, write canonical `complete.log` and leave no active Markdown files.
- [ ] If PASS, move this task directory to the dated archive and update this checklist there.
- [ ] If PASS, remove an empty split parent or prove remaining siblings/files require it.
- [x] If WARN/FAIL, write the exact next state without `complete.log`.

## Deviations from Plan

None.

## Key Design Decisions

- Lifecycle operations are serialized separately from status snapshots, so component callbacks never run while the status mutex is held.
- `Start` gives components one child context, cancels it before reverse rollback, and uses a cancellation-independent rollback context so cleanup still runs after launch cancellation.
- Launch and rollback/stop error identities are retained with `errors.Join`; repeated `Stop` returns nil after the first completed shutdown.
- `Status` defensively copies the started-component list.

## Reviewer Checkpoints

- Lifecycle logic remains application-owned and does not duplicate shared task runtime algorithms.
- Startup rollback covers only started components and reverse shutdown preserves all errors.
- Cancellation and repeated stop are race-free.

## Verification Results

### Formatting

```bash
gofmt -w apps/agent/internal/host/*.go
```

Output:

```text
(clean exit, code 0, no output)
```

### Focused and race tests

```bash
go test -count=1 ./apps/agent/internal/host
go test -count=1 -race ./apps/agent/internal/host
```

Output:

```text
ok  	iop/apps/agent/internal/host	0.002s
ok  	iop/apps/agent/internal/host	1.006s
```

### Static checks

```bash
go vet ./apps/agent/internal/host
git diff --check
```

Output:

```text
(clean exit, code 0, no output)
```

---

> **[IMPLEMENTING AGENT — BEFORE SAVING]** Fill every implementation-owned section and leave review-only content unchanged.

## Section Ownership

| Section | Owner | Note |
|---------|-------|------|
| Header, Overview, Review Instructions | Fixed | Implementer must not modify |
| Implementation Item Completion, Implementation Checklist | Implementing agent | Check status only |
| Review-Only Checklist | Review agent | Implementer must not modify |
| Deviations, Decisions, Verification Results | Implementing agent | Record actual evidence |
| Reviewer Checkpoints | Fixed | Reviewer verifies them |
| Code Review Result and finalization | Review agent | Append/execute only after review |

## 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: Pass
- Findings:
  - Required — `apps/agent/internal/host/host.go:73`: `Start` holds `lifecycleMu` while each component starts, but `Stop` must acquire the same mutex before it reaches the stored cancel function. A component that blocks in `Start` until its context is cancelled therefore cannot be stopped: a focused reviewer test timed out after 250 ms with `Stop() could not cancel an in-progress component Start`. Publish/call the run cancellation before waiting for serialized teardown (or use an equivalent explicit lifecycle state), then add a deterministic concurrent Start/Stop test that proves prompt cancellation, rollback of only the started prefix, and race-free completion.
- Routing Signals:
  - `review_rework_count=1`
  - `evidence_integrity_failure=false`
- Next Step: Invoke the plan skill with the raw finding and fresh verification evidence, rerun isolated routing, and materialize the smallest follow-up pair.
