Skip to content

Commit 0757cf1

Browse files
committed
feat(agent-manager): improve codex process-session matching
1 parent 45a6bbb commit 0757cf1

11 files changed

Lines changed: 862 additions & 30 deletions

File tree

docs/ai/design/feature-codex-adapter-agent-manager-package.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ Responsibilities:
4040
- `id`: deterministic session/process identifier
4141
- `name`: user-facing label derived from `cwd`; fallback to `codex-<session-id-prefix>` when `cwd` is missing
4242
- `cwd`: workspace path (if available)
43+
- `sessionStart`: parsed from `session_meta.timestamp` for process/session time matching
4344
- `status`: computed from recency/activity metadata using the same threshold values already used by existing adapters
44-
- `command`: executable + args required for open/focus flow
45+
- `pid`: matched running Codex process id used by terminal focus flow
4546

4647
## API Design
4748

@@ -80,8 +81,12 @@ Responsibilities:
8081
- Rationale: this feature adds detection capability, not UX changes.
8182
- Decision: Parse the first JSON line (`type=session_meta`) as the authoritative session identity/cwd/timestamp source.
8283
- Rationale: sampled session files consistently include this shape, and it avoids scanning full transcript payloads.
83-
- Decision: Determine end-of-session by reading the last JSON line and checking `payload.type`.
84-
- Rationale: `task_complete`/`turn_aborted` reliably indicate ended runs in sampled data.
84+
- Decision: Treat running `codex` processes as source-of-truth for list membership.
85+
- Rationale: session tail events can represent turn completion while process remains active.
86+
- Decision: Match `pid -> session` by closest process start time (`now - etime`) to `session_meta.timestamp` with tolerance.
87+
- Rationale: improves accuracy when multiple Codex processes share the same project `cwd`.
88+
- Decision: Bound session scanning for performance while including process-start day windows.
89+
- Rationale: keeps list latency low and still supports long-lived process/session mappings.
8590
- Decision: Keep status-threshold values consistent across adapters.
8691
- Rationale: preserves cross-agent behavior consistency and avoids adapter-specific drift.
8792
- Decision: Use `codex-<session-id-prefix>` fallback naming when `cwd` is unavailable.

docs/ai/implementation/feature-codex-adapter-agent-manager-package.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ description: Implementation notes for Codex adapter support in package agent man
3131
## Implementation Notes
3232

3333
### Core Features
34-
- Implement Codex adapter contract (`type`, `name`, `listRunningAgents`) using existing utilities where possible.
34+
- Implement Codex adapter contract (`type`, `canHandle`, `detectAgents`) using existing utilities where possible.
3535
- Normalize Codex metadata into stable `AgentInfo` output.
3636
- 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.
3738

3839
### Patterns & Best Practices
3940
- Follow `ClaudeCodeAdapter` structure for consistency.
@@ -54,11 +55,26 @@ description: Implementation notes for Codex adapter support in package agent man
5455

5556
## Performance Considerations
5657

57-
- Avoid repeated filesystem scans where possible.
58-
- Keep parsing linear to discovered entries.
58+
- Avoid full session-history scans per run; use bounded recent-file selection.
59+
- Include process-start day windows to preserve long-lived session mapping without scanning all days.
60+
- Keep parsing linear to selected entries only.
5961
- Reuse existing async aggregation model.
6062

6163
## Security Notes
6264

6365
- Read only local metadata/process information necessary for agent detection.
6466
- Do not execute arbitrary commands during detection.
67+
68+
## Implementation Status
69+
70+
- Completed:
71+
- Added `packages/agent-manager/src/adapters/CodexAdapter.ts`
72+
- Added package exports in `packages/agent-manager/src/adapters/index.ts` and `packages/agent-manager/src/index.ts`
73+
- Updated `packages/cli/src/commands/agent.ts` to register `CodexAdapter` for `list` and `open`
74+
- Added adapter unit tests and CLI command test mock update for Codex export
75+
- Notes:
76+
- 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.
77+
- Matching/performance constants are defined in `CodexAdapter`:
78+
- `PROCESS_SESSION_TIME_TOLERANCE_MS`
79+
- `PROCESS_START_DAY_WINDOW_DAYS`
80+
- session-scan bound constants (`MIN/MAX/SCAN_MULTIPLIER`)

docs/ai/planning/feature-codex-adapter-agent-manager-package.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,35 @@ description: Task plan for adding Codex adapter support and integrating it into
99

1010
## Milestones
1111

12-
- [ ] Milestone 1: Codex adapter design finalized and scaffolding created
13-
- [ ] Milestone 2: Codex adapter implementation and package exports complete
14-
- [ ] Milestone 3: CLI integration, tests, and verification complete
12+
- [x] Milestone 1: Codex adapter design finalized and scaffolding created
13+
- [x] Milestone 2: Codex adapter implementation and package exports complete
14+
- [x] Milestone 3: CLI integration, tests, and verification complete
1515

1616
## Task Breakdown
1717

1818
### Phase 1: Foundation
19-
- [ ] Task 1.1: Confirm Codex discovery inputs and mapping contract
19+
- [x] Task 1.1: Confirm Codex discovery inputs and mapping contract
2020
- Use `~/.codex/sessions/YYYY/MM/DD/*.jsonl` as the primary source
2121
- Parse line 1 `session_meta` for `id`, `cwd`, `timestamp`
2222
- Parse the last line for terminal event markers (`task_complete`, `turn_aborted`)
2323
- Define normalization rules for `id`, `name`, `cwd`, and `status`
24-
- [ ] Task 1.2: Scaffold package adapter files
24+
- [x] Task 1.2: Scaffold package adapter files
2525
- Add `CodexAdapter.ts` and test file skeleton
2626
- Update adapter/index exports
2727

2828
### Phase 2: Core Features
29-
- [ ] Task 2.1: Implement Codex discovery and mapping logic
29+
- [x] Task 2.1: Implement Codex discovery and mapping logic
3030
- Parse metadata with robust validation/fallback behavior
3131
- Compute status using existing status model
32-
- [ ] Task 2.2: Register Codex adapter in CLI command flow
32+
- [x] Task 2.2: Register Codex adapter in CLI command flow
3333
- Update all manager registration paths in `commands/agent.ts`
3434
- Preserve output structure and errors
3535

3636
### Phase 3: Integration & Polish
37-
- [ ] Task 3.1: Add/extend tests
37+
- [x] Task 3.1: Add/extend tests
3838
- Unit tests for Codex adapter branches and failure handling
3939
- CLI command tests for registration/path coverage
40-
- [ ] Task 3.2: Validate and document
40+
- [x] Task 3.2: Validate and document
4141
- Run lint/build/tests for affected projects
4242
- Record implementation + testing outcomes in docs/ai
4343

@@ -68,3 +68,7 @@ description: Task plan for adding Codex adapter support and integrating it into
6868
- Existing adapter examples (`ClaudeCodeAdapter`) as implementation template
6969
- Maintainer validation for Codex session/source assumptions
7070
- CI runtime for lint/build/test verification
71+
72+
## Progress Summary
73+
74+
Implementation scope is complete. `CodexAdapter` was added to `@ai-devkit/agent-manager`, exported through package entry points, and registered in CLI agent command flows. Follow-up fixes addressed three runtime issues: false-positive process matching, missing long-lived session links, and list latency from broad session scans. Matching now uses `etime`-based process start time with configurable tolerance and process-start day-window session inclusion, while keeping a bounded recent-file scan for performance. Focused adapter and CLI agent-command tests pass, and package lint/build/test checks pass for affected areas. Full `cli:test` includes unrelated pre-existing module-resolution failures (`@ai-devkit/memory` and workspace package mocking) and is tracked as external validation noise rather than a feature regression.

docs/ai/requirements/feature-codex-adapter-agent-manager-package.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ Who is affected:
6464
## Questions & Open Items
6565

6666
- Resolved (2026-02-26): Canonical discovery source is `~/.codex/sessions` JSONL files. In 88/88 sampled files, line 1 is `type=session_meta` with `payload.id`, `payload.cwd`, and `payload.timestamp`.
67-
- Resolved (2026-02-26): Active-vs-ended heuristic should use latest event in session file.
68-
- `payload.type` of `task_complete` or `turn_aborted` => session ended.
69-
- other trailing event/message types + recent timestamp => potentially active.
67+
- Resolved (2026-02-26): Running `codex` process list is the source of truth for whether an agent is listed.
68+
- Session tail events such as `task_complete` and `turn_aborted` do not hide an agent when the process is still running.
69+
- Resolved (2026-02-26): Session matching uses process start time (`now - etime`) against `session_meta.timestamp` with a configurable tolerance window constant.
70+
- Resolved (2026-02-26): For long-lived processes, session scan includes process-start day window in addition to bounded recent-file scanning.
7071
- Resolved (2026-02-26): Use the same status threshold values across all adapters (Codex uses existing shared/Claude-equivalent thresholds).
7172
- Resolved (2026-02-26): If `cwd` is missing, fallback display identifier is `codex-<session-id-prefix>`.

docs/ai/testing/feature-codex-adapter-agent-manager-package.md

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,21 @@ description: Test strategy and coverage plan for Codex adapter integration
1616
## Unit Tests
1717

1818
### `CodexAdapter`
19-
- [ ] Detect and map valid Codex entries into `AgentInfo`
20-
- [ ] Return empty array when no Codex metadata exists
21-
- [ ] Skip malformed entries without failing full result
22-
- [ ] Map status values based on activity thresholds
23-
- [ ] Produce stable name/id collision handling
19+
- [x] Detect and map valid Codex entries into `AgentInfo`
20+
- [x] Return empty array when no Codex metadata exists
21+
- [x] Skip malformed entries without failing full result
22+
- [x] Map status values based on activity thresholds
23+
- [x] Produce stable name/id collision handling
24+
- [x] Match same-cwd sessions by closest process start time
25+
- [x] Keep running processes listed even when session tail is `task_complete`/`turn_aborted`
2426

2527
### `AgentManager` integration seam
26-
- [ ] Aggregates Codex + Claude adapter output
28+
- [x] Aggregates Codex + Claude adapter output
2729
- [ ] Handles Codex adapter errors while preserving other adapter results
2830

2931
## Integration Tests
3032

31-
- [ ] `agent` command registers `CodexAdapter` in manager setup path(s)
33+
- [x] `agent` command registers `CodexAdapter` in manager setup path(s)
3234
- [ ] `agent list --json` includes Codex entries with expected fields
3335
- [ ] `agent open` handles Codex agent command metadata path correctly
3436

@@ -47,12 +49,19 @@ description: Test strategy and coverage plan for Codex adapter integration
4749
## Test Reporting & Coverage
4850

4951
- Commands:
50-
- `npm run lint`
51-
- `npm run build`
52-
- `npm run test -- --coverage`
53-
- project-scoped Nx tests for `agent-manager` and `cli`
52+
- `npx nx run agent-manager:lint`
53+
- `npx nx run agent-manager:build`
54+
- `npx nx run agent-manager:test`
55+
- `npx nx run agent-manager:test -- --runInBand src/__tests__/adapters/CodexAdapter.test.ts`
56+
- `npx nx run cli:test -- --runInBand src/__tests__/commands/agent.test.ts`
57+
- `npx nx run cli:lint` ✅ (warnings only, no errors)
5458
- Capture coverage deltas and list any residual gaps in this doc
5559

60+
Coverage and residual gaps:
61+
- New Codex adapter unit suite (`CodexAdapter.test.ts`) is passing with coverage on detection, filtering, status mapping, fallback naming, and time-based matching.
62+
- Full `npx nx run cli:test` currently fails due to unrelated pre-existing module-resolution issues in `memory.test.ts` and baseline `agent.test.ts` mocking behavior when running the entire suite without focused filtering.
63+
- Runtime validation confirmed targeted mapping: PID `81442` maps to session `019c7024-89e6-7880-81eb-1417bd2177b5` after time-based matching + process-day window logic.
64+
5665
## Manual Testing
5766

5867
- Verify table output readability for mixed Claude/Codex lists

0 commit comments

Comments
 (0)