perf(core): make Codex guardian discovery linear - #142
Open
kongtou20070406 wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
🟢 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); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What and why
readCodexGuardianThreadInfo()currently stores every parsed record and callscodexIsGuardianThread(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:
session_metarecord;codex-auto-reviewhas 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.
69d9e21)57e8979)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 onto69d9e21npm run typecheck— 0 errors (root + app)npm run lint— 0 errors, 11 pre-existing/generated warningsDeliberately out of scope