Skip to content

Commit c400158

Browse files
authored
Merge pull request #84 from ichinya/feat/codex-session-scan-performance
feat(sessions): enhance session file handling and optimize limit snapshot
2 parents 0e3a459 + 270bccf commit c400158

70 files changed

Lines changed: 6466 additions & 696 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ data/
66
*.sqlite
77
.ai-factory/
88
docs-html/
9+
*SFConflict*
910
playwright-report/
1011
test-results/

AGENTS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Autonomous task management system with Kanban board and AI subagents. Tasks flow
1414
- **Runtime Abstraction:** `@aif/runtime` workspace (runtime/provider contracts + registry)
1515
- **Database:** SQLite (better-sqlite3 + drizzle-orm)
1616
- **Frontend:** React 19 + Vite + TailwindCSS 4
17-
- **Runtime:** Pluggable adapter system (`@aif/runtime`) — built-in Claude (Agent SDK) + Codex (CLI/API) + OpenRouter (API) adapters
17+
- **Runtime:** Pluggable adapter system (`@aif/runtime`) — built-in Claude (Agent SDK) + Codex (SDK/CLI/API) + OpenRouter (API) adapters
1818
- **Agent:** Runtime-neutral coordinator + node-cron
1919
- **Testing:** Vitest
2020

@@ -60,7 +60,7 @@ packages/
6060
│ └── src/
6161
│ ├── index.ts # Server entry point
6262
│ ├── routes/ # tasks.ts, projects.ts, chat.ts, runtimeProfiles.ts
63-
│ ├── services/ # runtime.ts, fastFix.ts, roadmapGeneration.ts
63+
│ ├── services/ # runtime.ts, codexIndex.ts, fastFix.ts, roadmapGeneration.ts
6464
│ ├── middleware/ # logger.ts, rateLimit.ts, zodValidator.ts
6565
│ ├── schemas.ts # Zod request validation
6666
│ └── ws.ts # WebSocket handler

docs/api.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ Used by API/agent services to trigger project-scoped WebSocket broadcasts withou
341341
## Runtime Profiles
342342

343343
Runtime profiles carry non-secret transport/model config plus the latest persisted runtime-limit snapshot used by API, agent, and UI surfaces.
344+
For local Codex runtimes (`runtimeId=codex` with `sdk`/`cli` transport), `/runtime-profiles` and `/runtime-profiles/effective/*` now read limit overlays from the SQLite Codex index (`codex_limit_heads`) maintained by the background API indexer. Request handlers do not perform direct `~/.codex/sessions` scans.
344345

345346
### List Runtime Profiles
346347

@@ -367,6 +368,7 @@ GET /runtime-profiles/effective/chat/:projectId
367368
```
368369

369370
Both responses include the resolved `profile` object (or `null`) plus source metadata. When a profile is present, its payload includes `runtimeLimitSnapshot` and `runtimeLimitUpdatedAt`.
371+
If no indexed Codex head is available for the resolved account/project scope, the response falls back to the persisted profile snapshot.
370372

371373
### Runtime Limit Snapshot Shape
372374

@@ -672,6 +674,7 @@ GET /runtime-profiles
672674

673675
`scope=project` requires `projectId`. `scope=global` returns only reusable profiles (`projectId = null`).
674676
`scope=visible` is the default when omitted.
677+
For local Codex profiles, this endpoint overlays the response from indexed Codex limit heads in SQLite instead of scanning `~/.codex/sessions` during request handling.
675678

676679
### Effective Runtime Resolution
677680

@@ -874,6 +877,7 @@ DELETE /chat/sessions/:id
874877
```
875878

876879
Chat sessions persist the runtime profile chosen when the session starts. This keeps older conversations tied to the runtime they were created with even if the project's current default changes later.
880+
For local Codex runtimes, session discovery uses the indexed `codex_sessions` read-model. Session detail/message reads resolve `sessionId -> filePath` from the same index before compatibility fallback to runtime-adapter lookups.
877881

878882
`POST` and `PUT` accept `runtimeProfileId` as an optional field. The value must be either a global profile or one owned by the same project.
879883

docs/architecture.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ Runtime-limit state is normalized once in `@aif/runtime` and then persisted in S
8888
- adapters translate provider-specific quota signals into a shared `runtimeLimitSnapshot` contract;
8989
- `runtime_profiles.runtime_limit_snapshot_json` stores the latest authoritative profile-level state;
9090
- `tasks.runtime_limit_snapshot_json` stores a task-level copy when work is blocked by quota pressure or hard exhaustion.
91+
- API runs a background Codex indexer that reconciles `~/.codex/sessions` into SQLite read-model tables (`codex_sessions`, `codex_limit_heads`, `codex_limit_history`, `codex_index_cursors`) so hot endpoints (`/chat/sessions`, `/runtime-profiles`) use DB reads instead of request-path filesystem scans.
9192

9293
The source is explicit in the snapshot (`provider_api`, `sdk_event`, `api_headers`, `turn_usage`) so upper layers do not need adapter-specific branching.
9394

@@ -290,6 +291,9 @@ Key tables:
290291
- **runtime_profiles** — project-scoped or global runtime/provider profiles with non-secret transport/model config plus authoritative runtime-limit state (`runtime_limit_snapshot_json`, `runtime_limit_updated_at`)
291292
- **projects** — project metadata plus default runtime profile ids for tasks and chat
292293
- **chat_sessions / chat_messages** — persisted chat state with runtime profile/session linkage
294+
- **codex_sessions** — indexed Codex session metadata keyed by runtime session id and source file state
295+
- **codex_limit_heads / codex_limit_history** — latest and recent normalized Codex limit snapshots keyed by account fingerprint/project scope/limit id
296+
- **codex_index_cursors** — per-indexer progress/watermark state for incremental reconcile
293297
- **task_comments** — human/agent comments with optional attachments
294298

295299
### Indexes
@@ -303,6 +307,9 @@ Runtime index bootstrap creates the following indexes via `CREATE INDEX IF NOT E
303307
- `idx_tasks_project_status` — composite for ordered task-list queries
304308
- `idx_task_comments_task_id` — comment lookups by task
305309
- `idx_tasks_locked` — parallel execution: find unlocked or stale-locked tasks
310+
- `idx_codex_sessions_project_root_updated` / `idx_codex_sessions_file_path` — local Codex session listing and session-id/file-path lookup
311+
- `idx_codex_limit_heads_lookup` — account/project/limit scoped head overlay queries
312+
- `idx_codex_limit_history_head` / `idx_codex_limit_history_account` — bounded recent limit history by head and account scope
306313

307314
## See Also
308315

0 commit comments

Comments
 (0)