|
| 1 | +--- |
| 2 | +phase: implementation |
| 3 | +title: "Codex Adapter in @ai-devkit/agent-manager - Implementation" |
| 4 | +feature: codex-adapter-agent-manager-package |
| 5 | +description: Implementation notes for Codex adapter support in package agent manager and CLI integration |
| 6 | +--- |
| 7 | + |
| 8 | +# Implementation Guide: Codex Adapter in @ai-devkit/agent-manager |
| 9 | + |
| 10 | +## Development Setup |
| 11 | + |
| 12 | +- Use branch/worktree: `feature-codex-adapter-agent-manager-package` |
| 13 | +- Install dependencies with `npm ci` |
| 14 | +- Validate docs and feature scope with: |
| 15 | + - `npx ai-devkit@latest lint` |
| 16 | + - `npx ai-devkit@latest lint --feature codex-adapter-agent-manager-package` |
| 17 | + |
| 18 | +## Code Structure |
| 19 | + |
| 20 | +- Package adapter implementation: |
| 21 | + - `packages/agent-manager/src/adapters/CodexAdapter.ts` |
| 22 | +- Package exports: |
| 23 | + - `packages/agent-manager/src/adapters/index.ts` |
| 24 | + - `packages/agent-manager/src/index.ts` |
| 25 | +- CLI wiring: |
| 26 | + - `packages/cli/src/commands/agent.ts` |
| 27 | +- Tests: |
| 28 | + - `packages/agent-manager/src/__tests__/adapters/CodexAdapter.test.ts` |
| 29 | + - CLI tests touching adapter registration/open flow |
| 30 | + |
| 31 | +## Implementation Notes |
| 32 | + |
| 33 | +### Core Features |
| 34 | +- Implement Codex adapter contract (`type`, `canHandle`, `detectAgents`) using existing utilities where possible. |
| 35 | +- Normalize Codex metadata into stable `AgentInfo` output. |
| 36 | +- Register Codex adapter in command paths that instantiate `AgentManager`. |
| 37 | +- Match process/session pairs by `cwd` plus process-start-time proximity (`etime` vs `session_meta.timestamp`) using configurable tolerance. |
| 38 | + |
| 39 | +### Patterns & Best Practices |
| 40 | +- Follow `ClaudeCodeAdapter` structure for consistency. |
| 41 | +- Keep adapter-specific parsing in adapter module; keep formatting in CLI. |
| 42 | +- Fail soft on malformed/partial entries; avoid throwing across adapter boundary. |
| 43 | +- Keep `detectAgents` orchestration readable via small private helpers for each matching stage. |
| 44 | + |
| 45 | +## Integration Points |
| 46 | + |
| 47 | +- `AgentManager` parallel aggregation behavior |
| 48 | +- `TerminalFocusManager` open/focus flow compatibility for Codex command metadata |
| 49 | +- CLI list/json output mapping |
| 50 | + |
| 51 | +## Error Handling |
| 52 | + |
| 53 | +- Handle missing/unreadable Codex source data by returning empty results. |
| 54 | +- Catch parsing errors per-entry and continue processing valid entries. |
| 55 | +- Let manager collect adapter errors without crashing full command. |
| 56 | + |
| 57 | +## Performance Considerations |
| 58 | + |
| 59 | +- Avoid full session-history scans per run; use bounded recent-file selection. |
| 60 | +- Include process-start day windows to preserve long-lived session mapping without scanning all days. |
| 61 | +- Keep parsing linear to selected entries only. |
| 62 | +- Reuse existing async aggregation model. |
| 63 | + |
| 64 | +## Security Notes |
| 65 | + |
| 66 | +- Read only local metadata/process information necessary for agent detection. |
| 67 | +- Do not execute arbitrary commands during detection. |
| 68 | + |
| 69 | +## Implementation Status |
| 70 | + |
| 71 | +- Completed: |
| 72 | + - Added `packages/agent-manager/src/adapters/CodexAdapter.ts` |
| 73 | + - Added package exports in `packages/agent-manager/src/adapters/index.ts` and `packages/agent-manager/src/index.ts` |
| 74 | + - Updated `packages/cli/src/commands/agent.ts` to register `CodexAdapter` for `list` and `open` |
| 75 | + - Added adapter unit tests and CLI command test mock update for Codex export |
| 76 | +- Notes: |
| 77 | + - CLI TypeScript tests resolve workspace package exports from built artifacts; run `npx nx run agent-manager:build` before focused CLI agent-command tests when export surface changes. |
| 78 | + - Matching/performance constants are defined in `CodexAdapter`: |
| 79 | + - `PROCESS_SESSION_TIME_TOLERANCE_MS` |
| 80 | + - `PROCESS_START_DAY_WINDOW_DAYS` |
| 81 | + - session-scan bound constants (`MIN/MAX/SCAN_MULTIPLIER`) |
| 82 | + - Simplification refactor (no behavior change): |
| 83 | + - extracted orchestration helpers: |
| 84 | + - `listCodexProcesses` |
| 85 | + - `calculateSessionScanLimit` |
| 86 | + - `assignSessionsForMode` |
| 87 | + - `addMappedSessionAgent` |
| 88 | + - `addProcessOnlyAgent` |
| 89 | + - `filterCandidateSessions` |
| 90 | + - `rankCandidatesByStartTime` |
| 91 | + - replaced repeated `agents.some(...)` PID checks with `assignedPids` set tracking |
| 92 | + |
| 93 | +## Phase 6 Check Implementation (February 26, 2026) |
| 94 | + |
| 95 | +### Alignment Summary |
| 96 | + |
| 97 | +- Overall status: aligned with requirements and design. |
| 98 | +- Codex adapter implementation remains package-owned and exported through public entry points. |
| 99 | +- CLI registration for `list` and `open` includes `CodexAdapter` and preserves existing command UX boundaries. |
| 100 | + |
| 101 | +### File-by-File Comparison |
| 102 | + |
| 103 | +- `packages/agent-manager/src/adapters/CodexAdapter.ts` |
| 104 | + - Implements required adapter contract and process-first list membership. |
| 105 | + - Uses configured time-based matching (`etime` start time vs `session_meta.timestamp`) with tolerance and day-window file inclusion. |
| 106 | + - Includes simplification refactor helpers and set-based PID/session assignment tracking with no behavior drift. |
| 107 | +- `packages/agent-manager/src/adapters/index.ts` |
| 108 | + - Exports `CodexAdapter` as designed. |
| 109 | +- `packages/agent-manager/src/index.ts` |
| 110 | + - Re-exports `CodexAdapter` from package root as designed. |
| 111 | +- `packages/cli/src/commands/agent.ts` |
| 112 | + - Registers `CodexAdapter` for both list and open manager paths; no CLI presentation logic moved into package. |
| 113 | +- `packages/agent-manager/src/__tests__/adapters/CodexAdapter.test.ts` |
| 114 | + - Covers core mapping/status behavior plus simplified matching-phase behavior (`cwd`, `missing-cwd`, `any`) and no-session-reuse expectations. |
| 115 | + |
| 116 | +### Deviations / Concerns |
| 117 | + |
| 118 | +- No requirement/design deviations found. |
| 119 | +- Residual validation note: full `cli:test` still has known unrelated pre-existing failures outside this feature scope; focused Codex adapter tests pass. |
| 120 | + |
| 121 | +## Phase 8 Code Review (February 26, 2026) |
| 122 | + |
| 123 | +### Findings |
| 124 | + |
| 125 | +1. No blocking correctness, security, or design-alignment issues found in the Codex adapter implementation or CLI integration paths. |
| 126 | +2. Non-blocking performance follow-up: `readFirstLine` currently reads full file content (`fs.readFileSync`) before splitting first line in `CodexAdapter`; this is acceptable for current bounded scan but can be optimized later for very large transcripts. |
| 127 | +3. Test-phase risk remains low for changed paths (focused suites pass), with residual global coverage/flaky-suite signals tracked as pre-existing workspace-level issues. |
| 128 | + |
| 129 | +### Final Checklist |
| 130 | + |
| 131 | +- Design/requirements match: ✅ |
| 132 | +- Logic gaps on changed paths: ✅ none identified |
| 133 | +- Security concerns introduced: ✅ none identified |
| 134 | +- Tests for changed behavior: ✅ focused adapter + CLI command suites pass |
| 135 | +- Docs updated across lifecycle phases: ✅ |
| 136 | + |
| 137 | +### Review Verdict |
| 138 | + |
| 139 | +- Ready for push/PR from a Phase 8 review perspective. |
0 commit comments