Skip to content

perf(sessions): reduce CPU work when listing session previews - #131341

Merged
steipete merged 1 commit into
mainfrom
steipete/gallant-mahavira-3f0182
Aug 28, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/gallant-mahavira-3f0182

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

Resolves unnecessary Gateway CPU work when listing session previews: a readable newest reply still caused older, discarded tail messages to be projected and Markdown-flattened. The ordinary probe contains up to 20 messages, with bounded widening when newer rows are not displayable.

Why This Change Was Made

Use native reverse short-circuit traversal in the existing title-reader selector. Each visited row still passes through the same transcript normalization and canonical display projector; traversal ends as soon as the newest visible text is found. This removes the eager maps and reversed-array allocation without changing visibility, suppression, Markdown flattening, truncation, null results, title selection, probe limits, or cache behavior.

Production LOC: +7/-7 (net 0). Tests: +112/-2 (net +110). No new production API, dependency, configuration, schema, or cache. AI-assisted with Codex.

User Impact

Session lists produce the same titles and previews while avoiding unnecessary work on older messages. Hidden, tool, system, thinking-only, suppressed control replies, and Markdown that flattens to empty still fall through according to the existing display rules. There is no visual or protocol change.

Release-note context: reduce redundant transcript projection when generating session-list last-message previews.

Evidence

Four new regression cases exercise the actual single-session and batched title readers, both with immediate success and bounded widening. They instrument an older nested text accessor at the existing storage-reader seam while retaining real SQLite reads, transcript normalization, and display projection. The observed row is outside the first-user head probe.

Before the production change, all four cases failed because the older text accessor was read three times after a valid newer preview was available; the exact title/preview assertions and all 48 existing tests passed. After the change, all 52 tests pass and the older accessor is untouched.

Focused test command (five files, 52 passed):

node scripts/run-vitest.mjs src/gateway/session-transcript-title-reader.markdown.test.ts src/gateway/session-transcript-title-reader.placeholder.test.ts src/gateway/session-display-projection.test.ts src/gateway/session-transcript-readers.test.ts src/gateway/session-title-inbound-metadata.test.ts

Changed-file validation passed, including core and core-test typechecks, formatting, lint, dead-export and boundary guards, and import-cycle checks:

node scripts/check-changed.mjs -- src/gateway/session-transcript-title-reader.ts src/gateway/session-transcript-title-reader.markdown.test.ts

A local synthetic benchmark using the real display projector and 20 assistant messages (19 older 8,560-byte Markdown messages plus one short newest reply), over 100 iterations, measured 2,000 versus 100 projections and approximately 159.92 ms versus 0.25 ms. Outputs also matched empty, tool-only, hidden-last, suppressed-control, and empty-after-flattening cases. This excludes storage, RPC, and cache costs and is not a measured production speedup.

No production service or live state was accessed or changed. This is a bounded behavior-neutral optimization; deterministic reader-boundary regression proof is used rather than visual evidence. Fresh independent autoreview and exact-head CI are recorded in the landing proof.

@clawsweeper

clawsweeper Bot commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: S maintainer Maintainer-authored PR labels Aug 28, 2026
@steipete
steipete marked this pull request as ready for review August 28, 2026 00:44
@steipete

Copy link
Copy Markdown
Contributor Author

Land-ready owner review for source b10e1dee540c6760fead5fc965be14de9138b36b.

The unnecessary work was owned by findLastMessageText in src/gateway/session-transcript-title-reader.ts:96: both eager maps ran for every tail row before selecting one visible result. Native reverse short-circuit traversal now normalizes and projects only visited rows. The existing display projector still owns visibility, suppression, Markdown flattening, truncation, and null results. Titles, bounded widening, and watermark caching are unchanged.

Best-fix verdict: this repairs the producer selector shared by single and batch reads. Adding consumer guards would leave the work intact; another cache or a database change would introduce unnecessary ownership and lifecycle complexity. Inspected the complete reader/display owner, transcript normalization, SQLite probe ordering, row and batch callers, and relevant sibling tests. Current main still has the eager selector. Production LOC: +7/-7 (net 0); tests: +112/-2 (net +110). No API, config, schema, dependency, or cache additions.

Regression proof: before the production change, the four new single/batch initial/widening cases failed solely because an older nested text accessor was read three times instead of zero. Exact preview/title assertions and the 48 prior tests passed. After the change, all 52 tests pass; the older accessor is untouched. A fresh rerun against the committed source also passed all 52 tests in five files. Real SQLite reads and the canonical projector remain active in these tests.

Verified local commands:

node scripts/run-vitest.mjs src/gateway/session-transcript-title-reader.markdown.test.ts src/gateway/session-transcript-title-reader.placeholder.test.ts src/gateway/session-display-projection.test.ts src/gateway/session-transcript-readers.test.ts src/gateway/session-title-inbound-metadata.test.ts
node scripts/check-changed.mjs -- src/gateway/session-transcript-title-reader.ts src/gateway/session-transcript-title-reader.markdown.test.ts
git diff --check

The complete changed gate passed, including types, formatting, lint, and architectural guards. Fresh independent prepublication autoreview returned no findings; the patch has not changed since that review. Native review artifacts validate with READY FOR /prepare-pr and zero findings.

Proof limits: no live performance measurement or storage/RPC/cache benchmark. This is a bounded behavior-neutral selector optimization, with deterministic reader-boundary regression evidence; synthetic projection timing is not a production speedup claim. No visual output or Codex/provider runtime contract changes.

Exact-head hosted CI: run 33130672002, attempt 1 completed successfully for b10e1dee540c6760fead5fc965be14de9138b36b. The required openclaw/ci-gate is SUCCESS.

Live merge policy inspection found no enforced independent-approval rule for this PR. The active default-branch rules enforce linear history, deletion/non-fast-forward protection, and the Actions-owned CI gate; the CI gate is satisfied. The two changed files are not restricted CODEOWNERS paths. Latest ClawSweeper status is review-started only, with no findings or Rank-up moves published; no review finding is being waived.

Native landing preparation passed: scripts/pr review-init, review-checkout-main, review-checkout-pr, review-guard, review-artifacts-init, review-validate-artifacts, then OPENCLAW_TESTBOX=1 scripts/pr prepare-run 131341. Hosted gates verified the exact reviewed SHA; prepare made no source changes and skipped an unnecessary push because the branch already matched. Native merge guards remain in force.

@steipete
steipete merged commit f40f907 into main Aug 28, 2026
298 checks passed
@steipete
steipete deleted the steipete/gallant-mahavira-3f0182 branch August 28, 2026 01:01
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime maintainer Maintainer-authored PR size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant