Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(agent-manager): improve codex process-session matching
  • Loading branch information
codeaholicguy committed Feb 26, 2026
commit 0757cf17f57d7f3d891f4b7977eba3152a270a84
11 changes: 8 additions & 3 deletions docs/ai/design/feature-codex-adapter-agent-manager-package.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ Responsibilities:
- `id`: deterministic session/process identifier
- `name`: user-facing label derived from `cwd`; fallback to `codex-<session-id-prefix>` when `cwd` is missing
- `cwd`: workspace path (if available)
- `sessionStart`: parsed from `session_meta.timestamp` for process/session time matching
- `status`: computed from recency/activity metadata using the same threshold values already used by existing adapters
- `command`: executable + args required for open/focus flow
- `pid`: matched running Codex process id used by terminal focus flow

## API Design

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

### Core Features
- Implement Codex adapter contract (`type`, `name`, `listRunningAgents`) using existing utilities where possible.
- Implement Codex adapter contract (`type`, `canHandle`, `detectAgents`) using existing utilities where possible.
- Normalize Codex metadata into stable `AgentInfo` output.
- Register Codex adapter in command paths that instantiate `AgentManager`.
- Match process/session pairs by `cwd` plus process-start-time proximity (`etime` vs `session_meta.timestamp`) using configurable tolerance.

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

## Performance Considerations

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

## Security Notes

- Read only local metadata/process information necessary for agent detection.
- Do not execute arbitrary commands during detection.

## Implementation Status

- Completed:
- Added `packages/agent-manager/src/adapters/CodexAdapter.ts`
- Added package exports in `packages/agent-manager/src/adapters/index.ts` and `packages/agent-manager/src/index.ts`
- Updated `packages/cli/src/commands/agent.ts` to register `CodexAdapter` for `list` and `open`
- Added adapter unit tests and CLI command test mock update for Codex export
- Notes:
- 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.
- Matching/performance constants are defined in `CodexAdapter`:
- `PROCESS_SESSION_TIME_TOLERANCE_MS`
- `PROCESS_START_DAY_WINDOW_DAYS`
- session-scan bound constants (`MIN/MAX/SCAN_MULTIPLIER`)
22 changes: 13 additions & 9 deletions docs/ai/planning/feature-codex-adapter-agent-manager-package.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,35 @@ description: Task plan for adding Codex adapter support and integrating it into

## Milestones

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

## Task Breakdown

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

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

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

Expand Down Expand Up @@ -68,3 +68,7 @@ description: Task plan for adding Codex adapter support and integrating it into
- Existing adapter examples (`ClaudeCodeAdapter`) as implementation template
- Maintainer validation for Codex session/source assumptions
- CI runtime for lint/build/test verification

## Progress Summary

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.
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ Who is affected:
## Questions & Open Items

- 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`.
- Resolved (2026-02-26): Active-vs-ended heuristic should use latest event in session file.
- `payload.type` of `task_complete` or `turn_aborted` => session ended.
- other trailing event/message types + recent timestamp => potentially active.
- Resolved (2026-02-26): Running `codex` process list is the source of truth for whether an agent is listed.
- Session tail events such as `task_complete` and `turn_aborted` do not hide an agent when the process is still running.
- Resolved (2026-02-26): Session matching uses process start time (`now - etime`) against `session_meta.timestamp` with a configurable tolerance window constant.
- Resolved (2026-02-26): For long-lived processes, session scan includes process-start day window in addition to bounded recent-file scanning.
- Resolved (2026-02-26): Use the same status threshold values across all adapters (Codex uses existing shared/Claude-equivalent thresholds).
- Resolved (2026-02-26): If `cwd` is missing, fallback display identifier is `codex-<session-id-prefix>`.
31 changes: 20 additions & 11 deletions docs/ai/testing/feature-codex-adapter-agent-manager-package.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ description: Test strategy and coverage plan for Codex adapter integration
## Unit Tests

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

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

## Integration Tests

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

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

- Commands:
- `npm run lint`
- `npm run build`
- `npm run test -- --coverage`
- project-scoped Nx tests for `agent-manager` and `cli`
- `npx nx run agent-manager:lint` ✅
- `npx nx run agent-manager:build` ✅
- `npx nx run agent-manager:test` ✅
- `npx nx run agent-manager:test -- --runInBand src/__tests__/adapters/CodexAdapter.test.ts` ✅
- `npx nx run cli:test -- --runInBand src/__tests__/commands/agent.test.ts` ✅
- `npx nx run cli:lint` ✅ (warnings only, no errors)
- Capture coverage deltas and list any residual gaps in this doc

Coverage and residual gaps:
- New Codex adapter unit suite (`CodexAdapter.test.ts`) is passing with coverage on detection, filtering, status mapping, fallback naming, and time-based matching.
- 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.
- Runtime validation confirmed targeted mapping: PID `81442` maps to session `019c7024-89e6-7880-81eb-1417bd2177b5` after time-based matching + process-day window logic.

## Manual Testing

- Verify table output readability for mixed Claude/Codex lists
Expand Down
Loading