Skip to content

perf(core): make Codex guardian discovery linear - #142

Open
kongtou20070406 wants to merge 1 commit into
tommy0103:mainfrom
kongtou20070406:perf/linear-codex-guardian-scan
Open

perf(core): make Codex guardian discovery linear#142
kongtou20070406 wants to merge 1 commit into
tommy0103:mainfrom
kongtou20070406:perf/linear-codex-guardian-scan

Conversation

@kongtou20070406

@kongtou20070406 kongtou20070406 commented Sep 3, 2026

Copy link
Copy Markdown

What and why

readCodexGuardianThreadInfo() currently stores every parsed record and calls codexIsGuardianThread(meta, records) after each line. For a non-guardian subagent transcript, each call scans the growing array again, making discovery O(n²) in transcript length and retaining O(n) records before indexing even begins.

This change keeps two scalar facts while reading once:

  • the latest valid session_meta record;
  • whether codex-auto-review has appeared before or after that metadata.

Direct guardian metadata and non-subagent early exits are unchanged. Legacy guardian detection still works when the model record appears before the session metadata; the new regression pins that ordering.

No schema, cursor, parser output, provider contract, public API, or dependency changes.

Reproduction and ablation

Windows 11, Node 24.16.0. The benchmark generates one 20,001-line non-guardian Codex subagent transcript in a temporary directory, warms discovery once, then reports five discovery samples.

arm samples (ms, sorted) p50 reduction
disabled (69d9e21) 252.49, 261.11, 262.29, 449.62, 463.36 262.29 ms
enabled (57e8979) 6.60, 6.81, 7.15, 7.23, 9.56 7.15 ms 97.3% (36.7x)

Both arms discover the same single non-guardian unit. The regression suite additionally covers direct guardians, legacy model-based guardians, matching five-part cursors, legacy cursors, same-mtime rewrites, exact changed paths, parsing, and incremental persistence.

Verification

  • npm test — 661 pass / 0 fail on Ubuntu WSL2 (Node 24.14.1), current head after rebasing onto 69d9e21
  • npm run typecheck — 0 errors (root + app)
  • npm run lint — 0 errors, 11 pre-existing/generated warnings
  • Focused Codex discovery/parse/index suites — 13 pass / 0 fail on Windows before the unrelated upstream rebase
  • Disabled/enabled benchmark rerun on the current parent/head pair
  • No existing assertion was loosened

Deliberately out of scope

Copilot AI lite review requested due to automatic review settings September 3, 2026 10:58

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The change is a targeted performance refactor with a focused regression test and no apparent contract/schema changes.

Pull request overview

This PR optimizes Codex “guardian” transcript discovery by replacing quadratic scanning over accumulated parsed records with a single linear pass that tracks only the latest session_meta and whether a codex-auto-review model record has been seen, preserving existing direct-guardian and non-subagent early-exit behavior.

Changes:

  • Refactors readCodexGuardianThreadInfo() to perform O(n) guardian detection without retaining all parsed records.
  • Adds a regression test to pin legacy guardian detection when the model record appears before session_meta.
File summaries
File Description
packages/core/src/parsing.ts Reworks guardian discovery to avoid repeated scans and large temporary allocations during transcript reads.
tests/codex-discover.test.mjs Adds coverage for legacy “model-before-metadata” guardian detection ordering.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +93 to +96
const units = discoverWith(rootDir, new Map());
assert.equal(units[0].sessionId, '');
assert.equal(units[0].meta.guardian, true);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants