> For the complete documentation index, see [llms.txt](https://faction-os.gitbook.io/faction-os-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://faction-os.gitbook.io/faction-os-docs/.spec_system/archive/sessions/phase20-session08-managed-agent-lifecycle-control/code-review.md).

# Code Review and Repair Report

**Session ID**: `phase20-session08-managed-agent-lifecycle-control` **Package**: null **Reviewed**: 2026-06-28 **Scope**: All uncommitted changes in the working tree **Result**: RESOLVED

## Review Surface

**Files reviewed** (all uncommitted changes):

* `.spec_system/state.json` - tracked-modified
* `.spec_system/specs/phase20-session08-managed-agent-lifecycle-control/implementation-notes.md` - untracked
* `.spec_system/specs/phase20-session08-managed-agent-lifecycle-control/spec.md` - untracked
* `.spec_system/specs/phase20-session08-managed-agent-lifecycle-control/tasks.md` - untracked
* `apps/server/README_server.md` - tracked-modified
* `apps/server/src/lib/commandCenterValidation.ts` - tracked-modified
* `apps/server/src/managers/heroLifecycleManager.ts` - tracked-modified
* `apps/server/src/managers/managedAgentSessionManager.ts` - untracked
* `apps/server/src/managers/orchestrationCommandCenter.ts` - tracked-modified
* `apps/server/src/routes/commandCenter.ts` - tracked-modified
* `apps/server/src/server.ts` - tracked-modified
* `apps/server/tests/commandCenterRoutes.test.ts` - tracked-modified
* `apps/server/tests/commandCenterValidation.test.ts` - tracked-modified
* `apps/server/tests/eventIngest.test.ts` - tracked-modified
* `apps/server/tests/heroLifecycleManager.test.ts` - tracked-modified
* `apps/server/tests/managedAgentSessionManager.test.ts` - untracked
* `apps/web/README_web.md` - tracked-modified
* `apps/web/src/components/orchestration/HeroLineageWorkbench.tsx` - tracked-modified
* `apps/web/src/components/orchestration/OrchestrationShell.tsx` - tracked-modified
* `apps/web/src/lib/commandCenterUi.ts` - tracked-modified
* `apps/web/src/lib/orchestrationApi.ts` - tracked-modified
* `apps/web/src/store/useWsClient.ts` - tracked-modified
* `apps/web/tests/CommandCenterPanes.test.tsx` - tracked-modified
* `apps/web/tests/HeroLineageWorkbench.test.tsx` - tracked-modified
* `apps/web/tests/commandCenterStore.test.ts` - tracked-modified
* `apps/web/tests/commandCenterUi.test.ts` - tracked-modified
* `apps/web/tests/orchestrationApi.test.ts` - tracked-modified
* `docs/api/README_api.md` - tracked-modified
* `packages/protocol/src/orchestrationCommandCenter.ts` - tracked-modified
* `packages/protocol/src/rest.ts` - tracked-modified
* `packages/protocol/tests/orchestrationCommandCenter.test.ts` - tracked-modified
* `tests/e2e/orchestration-command-center.e2e.ts` - tracked-modified

**Inventory commands**: `git status`, `git diff HEAD`, `git diff --cached`, `git ls-files --others --exclude-standard`

## Findings by Severity

### Critical

No findings.

### High

* `apps/server/src/managers/orchestrationCommandCenter.ts:646` - Managed lifecycle execution ran before the lifecycle command manager checked duplicate idempotency keys, stale `expectedRevision`, final command state, or execution collection capacity. A stale route request could therefore stop a registered local process and only then return a conflict instead of rejecting before process control. | Fix: added `HeroLifecycleManager.preflight`, call it before managed execution, and check execution collection capacity before invoking `ManagedAgentSessionManager.execute`. Added route regression coverage proving stale managed requests return `409` without signaling the process. | Status: FIXED

### Medium

* `apps/server/src/managers/managedAgentSessionManager.ts:445` - Process-backed stop treated any direct process handle `kill` call as success, including `false` returns from Node-style handles, and did not fall back to tree kill on a rejected or timed-out signal. This could record a managed process as stopped even when the local signal was not accepted. | Fix: direct process signals now use timeout-bounded acceptance checking and escalate to `SIGKILL` through the registered tree-kill path when `SIGTERM` is rejected or times out. Added manager regression coverage for direct kill rejection and escalation. | Status: FIXED

### Low

* `apps/web/src/lib/commandCenterUi.ts:1418` - Observed-only Hero/Lineage rows omitted `Record stop intent`, despite the session contract preserving record-only stop intent for non-managed hook-observed sessions. | Fix: restored `stop` in observed lifecycle actions and added UI helper coverage to keep the observed stop-intent action visible while managed rows still show only supported managed controls. | Status: FIXED

## Assumptions and Deliberate Non-Fixes

* The installed apex-spec analyzer fallback path was missing and this repo has no `.spec_system/scripts/`; I reconstructed the required project state from `.spec_system/state.json`, the active session spec, task checklist, implementation notes, conventions, and the Git inventory. This was a tooling gap, not an external blocker.

## Behavior Changes

* Stale, final-state, duplicate-idempotency, or execution-capacity conflicts are rejected before any managed local process or PTY lifecycle action runs.
* Process-backed managed stop now escalates through the manager-owned tree-kill path when a direct process handle rejects or times out on `SIGTERM`.
* Observed-only Hero/Lineage rows once again expose `Record stop intent`; managed rows still expose only supported ready managed controls.

## Validation Fix Review

* `apps/server/tests/commandCenterRoutes.test.ts:609` - Validation found the duplicate in-flight route test could race HTTP scheduling and occasionally send the duplicate request after the first stop had already completed, producing a valid idempotent `201` instead of the intended in-flight `409`. Fix: the test now waits for the mocked process-kill path to start before sending the duplicate request, making the in-flight assertion deterministic. Status: FIXED.

## Verification

* Analyzer: `if [ -d ".spec_system/scripts" ]; then bash .spec_system/scripts/analyze-project.sh --json; else bash /home/aiwithapex/.codex/plugins/cache/apex-spec-system/apex-spec/2.1.3-codex/scripts/analyze-project.sh --json; fi` - N/A - local and installed analyzer scripts were absent; state was reconstructed manually from `.spec_system/state.json`.
* Focused server regression tests: `source "$HOME/.nvm/nvm.sh" && nvm use 26.2.0 >/dev/null && npx vitest run apps/server/tests/managedAgentSessionManager.test.ts apps/server/tests/commandCenterRoutes.test.ts apps/server/tests/heroLifecycleManager.test.ts` - PASS - 3 files, 30 tests.
* Focused protocol/server/web tests: `source "$HOME/.nvm/nvm.sh" && nvm use 26.2.0 >/dev/null && npx vitest run packages/protocol/tests/orchestrationCommandCenter.test.ts apps/server/tests/managedAgentSessionManager.test.ts apps/server/tests/heroLifecycleManager.test.ts apps/server/tests/commandCenterValidation.test.ts apps/server/tests/commandCenterRoutes.test.ts apps/server/tests/eventIngest.test.ts apps/web/tests/orchestrationApi.test.ts apps/web/tests/commandCenterUi.test.ts apps/web/tests/HeroLineageWorkbench.test.tsx apps/web/tests/CommandCenterPanes.test.tsx apps/web/tests/commandCenterStore.test.ts` - PASS - 11 files, 146 tests.
* Type checker: `source "$HOME/.nvm/nvm.sh" && nvm use 26.2.0 >/dev/null && npm --workspace packages/protocol run typecheck` - PASS.
* Type checker: `source "$HOME/.nvm/nvm.sh" && nvm use 26.2.0 >/dev/null && npm --workspace apps/server run typecheck` - PASS.
* Type checker: `source "$HOME/.nvm/nvm.sh" && nvm use 26.2.0 >/dev/null && npm --workspace apps/web run typecheck` - PASS.
* Formatter: `source "$HOME/.nvm/nvm.sh" && nvm use 26.2.0 >/dev/null && npm run format:check` - PASS.
* Linter: `source "$HOME/.nvm/nvm.sh" && nvm use 26.2.0 >/dev/null && npm run lint` - PASS.
* Aggregate type checker: `source "$HOME/.nvm/nvm.sh" && nvm use 26.2.0 >/dev/null && npm run typecheck --workspaces --if-present` - PASS.
* Tests: `source "$HOME/.nvm/nvm.sh" && nvm use 26.2.0 >/dev/null && npm test` - PASS - 270 files passed, 1 skipped; 3076 tests passed, 1 skipped.
* Browser e2e: `source "$HOME/.nvm/nvm.sh" && nvm use 26.2.0 >/dev/null && npm run test:e2e -- --project=app-desktop tests/e2e/orchestration-command-center.e2e.ts` - PASS - 6 tests.
* Whitespace: `git diff --check` - PASS.
* ASCII scan: `changed=$(git diff --name-only HEAD); if [ -n "$changed" ] && printf '%s\n' "$changed" | xargs -r sh -c 'LC_ALL=C rg -n "[^\x00-\x7F]" "$@"' sh; then exit 1; else exit 0; fi` - PASS.
* Final diff re-read: no remaining issues found.

## Summary

1. Reviewed all uncommitted implementation changes: 32 files across protocol, server, web, docs, e2e, and session artifacts.
2. Findings: 0 critical, 1 high, 1 medium, 1 low; all resolved with targeted tests.
3. No repo-fixable findings remain deliberately unfixed.
4. Formatting, lint, package typechecks, aggregate workspace typecheck, full Vitest, focused Playwright e2e, whitespace, and ASCII checks pass.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://faction-os.gitbook.io/faction-os-docs/.spec_system/archive/sessions/phase20-session08-managed-agent-lifecycle-control/code-review.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
