Skip to content

improve: reduce Code Mode source preparation overhead - #154209

Merged
steipete merged 1 commit into
mainfrom
codex/code-mode-fast-path
Sep 21, 2026
Merged

steipete merged 1 commit into
mainfrom
codex/code-mode-fast-path

Conversation

@steipete

@steipete steipete commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Related: #153889. Follow-up to #154001.

What Problem This Solves

Code Mode still constructs an Acorn AST for ordinary JavaScript cells that contain no possible module loader, adding unnecessary source-preparation cost.

User Impact

JavaScript cells without module-loader syntax reach execution sooner. Typed discovery, tool validation, error locations, and sandbox limits are unchanged; no configuration changes are needed. Whole agent-turn latency remains dominated by other work.

Why This Change Was Made

A conservative text check skips Acorn only when import, require, and Unicode escapes are all absent. Possible loader spellings retain the existing parser and malformed-source checks, and QuickJS still parses every executable cell.

The live-matrix scorer now checks call identity before walking result chains and reuses each collected chain. Two malformed-JavaScript Unicode cases replace 192 repetitive valid-AST assertions, covering the fallback that the old loop never reached.

Evidence

On the same isolated AWS host and pinned baseline, source preparation fell from 16.3 to 0.54 microseconds for an 89-byte cell and from 870 to 5.47 microseconds for an 8.3 KiB cell (40 measured batches). Process peak RSS for these probes fell from 108.9 to 101.5 MiB and 116.9 to 100.4 MiB respectively. These are source-preparation microbenchmarks, not whole-task speedup claims. Cold worker startup and ordinary warm-cell timing were broadly unchanged.

Validation included 938 focused runtime/matrix tests across 28 files and passing changed checks. After removing the metadata-transfer experiment, the final source-preparation/matrix tests and private-QA build passed. Independent review of the final diff found no actionable P0–P2 findings. The real-model JavaScript-contract smoke passed on the exact clean commit: 1/1 task behavior and 1/1 interview consistency, with no failures, unavailable cases, or retries.

Benchmark runs: baseline, selected parser optimization (results-A). Selected build and focused proof.

Test cost: the final src/agents/code-mode-runtime.test.ts project passed 97 tests in 2.89 seconds including transform/import/setup; all final targeted projects passed 437 tests across four files in 19.22 seconds. CI run passed on f024c40ed11c68c093bafebcd4ea4cea79f12d8d: 795 seconds elapsed, including 92 seconds for checks-node-changed and 308 seconds for the build job (jobs overlap). The clean committed private-QA build also passed with source and runtime artifact hashes matching the initial build.

Live JavaScript-contract proof: explicit OpenClaw runtime, openai/gpt-5.6-luna, one trial, 13.107 seconds and five upstream calls. The built runtime recorded clean source at the PR commit.

@clawsweeper

clawsweeper Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added scripts Repository scripts agents Agent runtime and tooling size: S maintainer Maintainer-authored PR labels Sep 21, 2026
@steipete
steipete marked this pull request as ready for review September 21, 2026 00:29
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Sep 21, 2026
@clawsweeper

clawsweeper Bot commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs changes before merge. Reviewed September 20, 2026, 8:37 PM ET / September 21, 2026, 00:37 UTC.

ClawSweeper review

What this changes

Code Mode skips unnecessary JavaScript source parsing, reuses collected results in its QA scorer, and replaces repetitive Unicode tests with focused malformed-source cases.

Merge readiness

⛔ Needs changes before merge - 1 item remains

Keep open: this is a useful, bounded optimization absent from current main and the latest release. No actionable correctness or security defect was found in the introduced changes.

Priority: P2
Reviewed head: f024c40ed11c68c093bafebcd4ea4cea79f12d8d

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused, maintainable optimization with useful performance measurements and no blocking code findings.
Proof confidence 🌊 off-meta tidepool Not applicable: The verified repository-admin author is exempt from the external-contributor proof gate. Reported AWS measurements exercise source preparation; final live JavaScript-contract proof is still described as pending and is not counted as completed.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: The verified repository-admin author is exempt from the external-contributor proof gate. Reported AWS measurements exercise source preparation; final live JavaScript-contract proof is still described as pending and is not counted as completed.
Evidence reviewed 10 items Introduced change and merge ownership: The pinned merge-base-to-head diff introduces only source preparation, QA scorer, and test changes. Raw test-merge parents match the supplied base followed by the exact PR head; its source-preparation delta preserves the same four-line fast path.
Validation remains intact: The fast path requires absence of import, require, and Unicode-escape text. Potential loaders retain the existing AST and malformed-source checks; prepareSource still performs shell detection, and the worker still evaluates the prepared source through QuickJS.
QA scorer preserves its predicates: Outcome helpers now consume the same collected chain directly. Declaration identity and rejected-call identity checks move ahead of chain traversal without changing the acceptance conditions. Existing tests cover fabricated declarations, incorrect validation errors, and resumed outcomes.
Findings None None.
Security None None.

How this fits together

OpenClaw Code Mode prepares agent-written JavaScript before running it in a QuickJS worker with bounded tool access. Its QA scorer checks recorded tool calls and results against expected behavior.

flowchart TD
  A[Agent JavaScript] --> B{Possible module loader text}
  B -->|Yes| C[Existing parser and loader checks]
  B -->|No| D[Shell source check]
  C --> D
  D --> E[QuickJS worker execution]
  E --> F[Recorded calls and results]
  F --> G[QA behavior scorer]
Loading

Before merge

  • Complete next step (P2) - Add the measured wall time for src/agents/code-mode-runtime.test.ts and available CI seconds to the PR evidence, as required by repository test-cost policy.
Agent review details

Security

None.

PR surface

Source +4, Tests -3, Other +8. Total +9 across 3 files.

View PR surface stats
Area Files Added Removed Net
Source 1 4 0 +4
Tests 1 8 11 -3
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 1 48 40 +8
Total 3 60 51 +9

Review metrics

Metric Value Why it matters
Reported source preparation 89 bytes: 16.3 → 0.54 µs; 8.3 KiB: 870 → 5.47 µs The measured benefit is confined to source preparation; the PR does not claim comparable whole-turn acceleration.

Technical review

Best possible solution:

Keep source preparation with its existing owner and retain the conservative fast path without changing guest execution or tool permissions.

Do we have a high-confidence way to reproduce the issue?

Not applicable as a functional bug reproduction: current source confirms unconditional Acorn parsing, and the author supplies before/after performance measurements; those measurements were not rerun here.

Is this the best way to solve the issue?

Yes. The conservative prefilter removes unnecessary work at the existing owner, while the scorer changes preserve the same result-chain and identity checks.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 2969f4becc06.

Labels

Label changes:

  • add P2: This is a bounded runtime performance improvement with reported measurements and no urgent user-facing regression.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: The verified repository-admin author is exempt from the external-contributor proof gate. Reported AWS measurements exercise source preparation; final live JavaScript-contract proof is still described as pending and is not counted as completed.

Label justifications:

  • P2: This is a bounded runtime performance improvement with reported measurements and no urgent user-facing regression.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: The verified repository-admin author is exempt from the external-contributor proof gate. Reported AWS measurements exercise source preparation; final live JavaScript-contract proof is still described as pending and is not counted as completed.

Evidence

What I checked:

  • Introduced change and merge ownership: The pinned merge-base-to-head diff introduces only source preparation, QA scorer, and test changes. Raw test-merge parents match the supplied base followed by the exact PR head; its source-preparation delta preserves the same four-line fast path. (src/agents/code-mode-source.ts:93, f024c40ed11c)
  • Validation remains intact: The fast path requires absence of import, require, and Unicode-escape text. Potential loaders retain the existing AST and malformed-source checks; prepareSource still performs shell detection, and the worker still evaluates the prepared source through QuickJS. (src/agents/code-mode.worker.ts:561, f024c40ed11c)
  • QA scorer preserves its predicates: Outcome helpers now consume the same collected chain directly. Declaration identity and rejected-call identity checks move ahead of chain traversal without changing the acceptance conditions. Existing tests cover fabricated declarations, incorrect validation errors, and resumed outcomes. (scripts/lib/code-mode-matrix-gateway.ts:773, f024c40ed11c)
  • Focused Unicode coverage: The removed loop repeated valid-AST rejection across 96 padding lengths for two loaders. Existing valid Unicode cases remain, while the two added malformed-JavaScript cases exercise the UTF-16 masking fallback. (src/agents/code-mode-runtime.test.ts:410, f024c40ed11c)
  • Still needed on current main: Fetched main still unconditionally invokes parseCodeModeScriptSyntax inside rejectsModuleAccess. The merged JavaScript-only predecessor is related groundwork, not implementation of this fast path. (src/agents/code-mode-source.ts:93, 2969f4becc06)
  • Latest release comparison: The v2026.9.5 source also lacks the fast path and retains the older JavaScript/TypeScript preparation interface. (src/agents/code-mode-source.ts, ec9c1a13db89)

Likely related people:

  • unknown: The claimed source-line change could not be verified from bounded local history. (role: source history unknown; confidence: low)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Record the changed runtime test file’s measured wall time and available CI seconds in the PR evidence.

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@steipete
steipete merged commit 4912a41 into main Sep 21, 2026
183 checks passed
@steipete
steipete deleted the codex/code-mode-fast-path branch September 21, 2026 00:53
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling maintainer Maintainer-authored PR P2 Normal backlog priority with limited blast radius. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. scripts Repository scripts size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant