Skip to content

perf(agents): preserve cross-run Code Mode prompt prefixes - #170282

Merged
steipete merged 2 commits into
mainfrom
steipete/perf-micro-cache-cross-run-residual-20260919
Oct 12, 2026
Merged

steipete merged 2 commits into
mainfrom
steipete/perf-micro-cache-cross-run-residual-20260919

Conversation

@steipete

@steipete steipete commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Code Mode rewrites the exec tool description when MCP, plugin, client, or permission-filtered catalog entries change between runs. That invalidates the tool-schema prefix of an otherwise reusable conversation.

User Impact

Consecutive runs retain identical Code Mode control schemas while receiving the current callable index and capability guidance after the stable prompt prefix. Execution still uses the current catalog and permissions. No configuration, schema migration, or operator action is required.

Why This Change Was Made

Move the bounded catalog index and catalog-dependent MCP, skills, nodes, and Swarm guidance into the existing prompt projection, for both embedded and worker harnesses. The existing session prompt owner appends changed sections on supported native Anthropic/Responses routes. Delete the description observer and wrapper synchronization machinery instead of retaining two sources of catalog guidance.

Current main already retains refreshed system/suffix sections and append-only failed-turn replay. Added serialized Anthropic/Responses regressions cover all reported section names, error/aborted assistant turns, staged user input, operator context, and diagnostics staying off-wire; no speculative second replay mechanism is added.

Evidence

Synthetic byte measurement against the original production renderer at 36e78f215f1f, 20 runs with 70 tools and one changing entry: serialized common tool-schema projection changes 19/19 → 0/19; tool-schema size 7,335 → 3,942 bytes (46% smaller). Changing the Code Mode index also changes the original system prefix but leaves the new 4,942-byte prefix identical while retaining current guidance. These measure bytes and prefix stability, not provider cache-hit rates or latency.

Production diff: 80 insertions, 131 deletions (net −51 lines), excluding tests, test support, and docs.

Local proof: changed-file oxfmt, repository oxlint on changed TypeScript files, and git diff --check pass. Independent review is clean through P2.

  • pnpm test src/agents/code-mode.test.ts src/agents/code-mode.catalog-index.test.ts src/agents/system-prompt.test.ts src/agents/embedded-agent-runner/run/attempt.llm-boundary.cache-stability.test.ts --maxWorkers=1: 113 tests passed, 274.44 s cold wall time (catalog shard 110.88 s; serialized-request shard 24.54 s; Code Mode/system-prompt shard 114.79 s). The new schema-equality test took 3 ms; new error/operator cases took 2–7 ms each.
  • src/agents/harness/tool-surface-bridge.test.ts (initial focused run, also using --maxWorkers=1): 33 tests passed, 195.05 s cold shard wall time, dominated by compilation. This was an earlier combined command; its subsequent new replay fixture assertions failed because a low-level adapter was expected to emit the native transport's role. The fixture now uses the native transport and passes above.

Initial overlapping edits caused cold-compiler source-change failures before tests; no such run is counted as proof. Additional focused proof after CI found stale test expectations:

  • pnpm test src/agents/tools/agents-wait-tool.test.ts --maxWorkers=1: 31 passed, 100.79 s cold wall time.
  • pnpm test src/agents/bash-tools.exec-host-contract.test.ts src/agents/system-prompt-report.tool-digest-cache.test.ts --maxWorkers=1: 16 passed, 144.10 s wall time. The test-only follow-up also passed independent P2 review.

193 focused tests pass locally. Initial CI identified remaining collector/shell expectations that read the retired description index, plus a summary fixture that unintentionally invoked shell-description augmentation. Those are corrected without changing production code. Final-head CI run 38315040682 passed on b61dbfce7711a92b537505ca2b5bd40ff1f0a751 in 913 wall seconds; all required checks, including openclaw/ci-gate, are green. Full typecheck, broad suites, and remaining sibling coverage run in PR CI. No Testbox/Crabbox lease was allocated. The Linux benchmark rig was unreachable over SSH; Team was untouched. No live provider latency/cache-hit measurement is claimed.

@clawsweeper

clawsweeper Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

ClawSweeper review in progress

ClawSweeper is reviewing this revision. This supersedes any previous blocked status.

View the workflow run.

@steipete
steipete marked this pull request as ready for review October 12, 2026 21:56
@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation agents Agent runtime and tooling size: L labels Oct 12, 2026
@github-actions

github-actions Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

⚠️ Security sensitive changes

This maintainer PR changes sensitive security components.

No secops approval is required. This comment is informational because the PR author has Maintain or Admin access.

  • Current SHA: b61dbfce7711a92b537505ca2b5bd40ff1f0a751
  • Maintainer: @steipete
  • Repository role: admin

These security sensitive changes were made:

  • src/agents/harness/prompt-tool-policy.ts: Controls sandbox containment or tool execution authority. Check filesystem and network isolation, approval scope, and command allowlists.

Carefully review these changes before merging.

@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Oct 12, 2026
@clawsweeper

clawsweeper Bot commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs changes before merge.

What this changes

This PR moves changing Code Mode catalog guidance from tool descriptions into the prompt after its stable prefix.

Example: A second Code Mode run adds stamp_beta alongside stamp_alpha.

  • Before: The exec description changes, altering provider-visible tool declarations.
  • After: The control declarations stay identical while the prompt supplies the current callable index.

Review scores

Measure Result What it means
Overall readiness 🦪 silver shellfish (2/6) Useful ownership simplification with harness evidence, currently blocked by concrete test regressions.
Proof confidence 🦐 gold shrimp (3/6) The verified maintainer-authored PR is outside the contributor proof gate; supplied evidence covers production renderers and serialized Anthropic/Responses requests, without a shipped Gateway or live-provider performance run.
Patch quality 🦪 silver shellfish (2/6) 2 actionable review findings remain.

Product

Kind: Performance · Worth it: Yes · Fix scope: Complete
User problem: Catalog changes can invalidate reusable provider prompt prefixes in long Code Mode conversations.
Reason: The maintainer-authored direction addresses a source-confirmed problem and removes a competing presentation mechanism.

Merge readiness

⛔ Needs changes before merge - 2 items remain

Keep this PR open: it addresses a current prompt-prefix problem with a simpler ownership model, but two diff-caused test failures and another stale assertion need repair.

Priority: P2
Reviewed head: e34e3c95eb15e3430a5bf8d521cf04a560c3a172

Before merge

  • Migrate the remaining catalog assertions to the prompt projection (P2) - Removing catalog contracts from exec.description leaves this assertion expecting completed: Array< in text that can no longer contain it. CI fails here. The host matrix in src/agents/bash-tools.exec-host-contract.test.ts:65 likewise expects host?: ... in the description. Assert those contracts through buildCodeModeCatalogPrompt using the current catalog, preserving the existing execution and schema checks.
  • Use a neutral tool name in the mutable-description fixture (P2) - The rewritten fixture is an ordinary tool named exec. After its description is manually changed, finalize(tools) invokes applyToolAvailabilityDescriptions, which replaces exec text with the shell description. The resulting summary cannot equal the report captured before finalization, and CI fails at this assertion. Use a neutral probe name so finalization preserves the description being tested.

Findings

  • [P2] Migrate the remaining catalog assertions to the prompt projection — src/agents/tools/agents-wait-tool.test.ts:95
  • [P2] Use a neutral tool name in the mutable-description fixture — src/agents/system-prompt-report.tool-digest-cache.test.ts:72
Agent review details

How this fits together

Agent tool assembly prepares a policy-filtered catalog; prompt projection presents its callable contracts to the model, while Code Mode execution dispatches through the current catalog.

flowchart TD
  A[Enabled tools] --> B[Policy filtered catalog]
  B --> C[Stable control schemas]
  B --> D[Current catalog prompt]
  C --> E[Provider request]
  D --> E
  E --> F[Code Mode execution]
  B --> F
Loading

Technical review

Best possible solution:

Complete the test cutover while retaining stable control schemas, current catalog guidance, and unchanged execution authority.

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

Current-main source establishes catalog-driven declaration changes, and CI annotations directly establish two introduced test failures; no target code was executed during this review.

Is this the best way to solve the issue?

Moving current guidance into the existing prompt owner avoids stale session stamps and duplicate synchronization, but the remaining test consumers must follow that cutover.

Full review comments:

  • [P2] Migrate the remaining catalog assertions to the prompt projection — src/agents/tools/agents-wait-tool.test.ts:95
    Removing catalog contracts from exec.description leaves this assertion expecting completed: Array< in text that can no longer contain it. CI fails here. The host matrix in src/agents/bash-tools.exec-host-contract.test.ts:65 likewise expects host?: ... in the description. Assert those contracts through buildCodeModeCatalogPrompt using the current catalog, preserving the existing execution and schema checks.
    Confidence: 1
  • [P2] Use a neutral tool name in the mutable-description fixture — src/agents/system-prompt-report.tool-digest-cache.test.ts:72
    The rewritten fixture is an ordinary tool named exec. After its description is manually changed, finalize(tools) invokes applyToolAvailabilityDescriptions, which replaces exec text with the shell description. The resulting summary cannot equal the report captured before finalization, and CI fails at this assertion. Use a neutral probe name so finalization preserves the description being tested.
    Confidence: 1

Overall correctness: patch is incorrect
Overall confidence: 0.98

AGENTS.md: found and applied where relevant.

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

Provenance checked

  • Catalog contracts in exec descriptions changes intended behavior with a stated reason (4249df3: One effective catalog owns callable names, provider-visible contracts, and runtime routing.)
  • Description wrapper synchronization changes intended behavior with a stated reason (7ea7ada: Weak references preserved live-wrapper updates without retaining obsolete execution runners.)
  • Presentation versus execution restrictions keeps the original intent (dc66f00: Keep presentation based on the admitted catalog while enforcing execution restrictions separately.)
  • Transcript-backed prompt refresh keeps the original intent (a79e29e: Preserve cached conversation prefixes by appending refreshed instruction sections through the existing session owner.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

Acceptance criteria:

  • pnpm test src/agents/tools/agents-wait-tool.test.ts src/agents/bash-tools.exec-host-contract.test.ts src/agents/system-prompt-report.tool-digest-cache.test.ts --maxWorkers=1
  • pnpm exec oxfmt --check src/agents/tools/agents-wait-tool.test.ts src/agents/bash-tools.exec-host-contract.test.ts src/agents/system-prompt-report.tool-digest-cache.test.ts
  • git diff --check

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit dc66f00 adds src/agents/code-mode.ts:148 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: dc66f009f3c5; files: src/agents/code-mode.ts)
  • Vincent Koc: Raw commit b5b1029 adds src/agents/code-mode.ts:170 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: medium; commits: b5b1029990fd; files: src/agents/code-mode.ts)

PR surface

Source -51, Tests +83, Docs +4. Total +36 across 25 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 8 80 131 -51
Tests 14 302 219 +83
Docs 3 8 4 +4
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 25 390 354 +36

Review metrics

Metric Value Why it matters
Production versus test changes Production +80/-131; tests +300/-202; test support +2/-17. Production shrinks by deleting the superseded observer machinery; added tests cover schema stability and serialized replay.

Labels

Label changes:

  • add P2: This is a bounded prompt-cache improvement with focused test repairs, without evidence of an urgent runtime outage.
  • add rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦐 gold shrimp and patch quality is 🦪 silver shellfish.
  • add status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action.

Label justifications:

  • P2: This is a bounded prompt-cache improvement with focused test repairs, without evidence of an urgent runtime outage.
  • rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦐 gold shrimp and patch quality is 🦪 silver shellfish.
  • status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed October 12, 2026, 6:04 PM ET / 22:04 UTC.

@steipete

Copy link
Copy Markdown
Contributor Author

Both findings are fixed in b61dbfc: collector and shell host-contract assertions now read the current catalog prompt, and the mutable-description fixture uses a neutral tool name. The three affected files pass locally (47 tests), the follow-up independent review is clean, and the exact-head CI run is green: https://github.com/openclaw/openclaw/actions/runs/38315040682.

@steipete
steipete merged commit 0de6366 into main Oct 12, 2026
141 of 142 checks passed
@steipete
steipete deleted the steipete/perf-micro-cache-cross-run-residual-20260919 branch October 12, 2026 22:31
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 13, 2026
…170282)

* perf(agents): preserve cross-run Code Mode prompt prefixes

* test(agents): follow current Code Mode catalog guidance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling docs Improvements or additions to documentation P2 Normal backlog priority with limited blast radius. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. security-sensitive-changed size: L status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant