Skip to content

perf(sessions): reuse opaque-key matchers and pruning cursors - #133691

Merged
steipete merged 1 commit into
mainfrom
codex/perf-session-key-map
Aug 31, 2026
Merged

steipete merged 1 commit into
mainfrom
codex/perf-session-key-map

Conversation

@steipete

Copy link
Copy Markdown
Contributor

Session-key normalization rebuilt its fixed opaque-peer matchers for every uncached key, while bounded-map pruning restarted a keys iterator for each eviction. Both owners already have the information needed to avoid that work.

This prepares the existing matchers once, flattens their tail collector, removes a redundant normalization helper, and uses one insertion-order cursor for bulk eviction. The public normalizer and ordinary-key path stay unchanged, as do opaque Matrix/Signal bytes, thread suffixes, alias proof, cache bounds, TTL, and caller-owned Map lifecycle. There is no new cache, option, public API, persistence format, or authority policy. Production is +52/−62 lines (net −10); tests are unchanged.

All 176 existing session, routing, store-key, Map, dedupe, and directory-cache tests passed on current main and the candidate. Independent complete-owner review and managed Codex review found no actionable defects. The complete formatted production AST matches the measured proposal. Current-source review found only an unrelated redaction-pattern delimiter change among the 128 historical runtime inputs; package dependencies are unchanged since the previous source review. Historical measurements remain identified with their original base.

The retained differential proof covers 2,229 scenarios / 6,841 comparison units and 43 complete workload projections / 129 comparisons. Eight fresh forward/reverse processes retained all 3,784 timing samples. Uncached opaque-key normalization in an already loaded module improves about 2.1–2.3×, and pruning 2,048 entries while retaining 2,048 improves about 8.8×. Most callers evict only one entry. Costs remain explicit: ordinary mixed-case misses were 35/46 ns slower, empty dedupe insertion 9/24 ns slower, and one-item eviction at cap one 1/4 ns slower. Empty-store controls were order-sensitive. These are scoped owner measurements, not whole-Gateway, channel-delivery, provider, or memory gains.

Verification commands:

pnpm test src/sessions/session-key-case-preservation.test.ts src/routing/session-key.test.ts src/routing/session-key.continuity.test.ts src/config/sessions/session-key.test.ts src/config/sessions/explicit-session-key-normalization.test.ts src/infra/map-size.test.ts src/infra/dedupe.test.ts src/infra/outbound/directory-cache.test.ts -- --maxWorkers=1
pnpm check:changed --base d56aacd6991b4c552fe1bd71b3d061034a70906b

Full changed-file checks passed, including production/test typechecks, lint, unused-export analysis, database guards, and zero runtime import cycles. This internal refactor preserves documented behavior; no new live-channel claim or operator action is introduced.

@clawsweeper

clawsweeper Bot commented Aug 31, 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 size: S maintainer Maintainer-authored PR labels Aug 31, 2026
@steipete
steipete marked this pull request as ready for review August 31, 2026 01:20
@clawsweeper clawsweeper Bot added P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. 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 Aug 31, 2026
@clawsweeper

clawsweeper Bot commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 30, 2026, 9:27 PM ET / August 31, 2026, 01:27 UTC.

ClawSweeper review

What this changes

The PR prepares fixed opaque-peer matchers once for session-key normalization and reuses one insertion-order iterator when pruning bounded maps.

Merge readiness

⚠️ Ready for maintainer review - 1 item remains

Keep open. The reviewed main context still has the pre-optimization implementation, while this small, maintainer-labeled PR makes behavior-preserving changes within the two owning helpers; no blocking correctness or security defect was found from source review.

Priority: P3
Reviewed head: 03d59bb19038fc08225b076ef94ffefbc53a3758

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused, net-negative owner-level refactor with preserved existing coverage and no supported blocking finding.
Proof confidence 🌊 off-meta tidepool Not applicable: This maintainer-authored internal refactor does not trigger the external-contributor proof gate; source review ties the changed owners to existing behavior tests, though no local test execution was performed in this read-only review.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: This maintainer-authored internal refactor does not trigger the external-contributor proof gate; source review ties the changed owners to existing behavior tests, though no local test execution was performed in this read-only review.
Evidence reviewed 5 items Introduced owner changes: The verified PR delta prepares the three fixed descriptor patterns at module initialization and uses them only in the existing span collector; it also creates one iterator only after a map is known to exceed its positive limit.
Owner boundary and callers: The normalizer remains the single path used by routing and session-store canonicalization; the store owner retains its alias-proof safeguards for Matrix and Signal keys.
Existing behavior coverage: Unchanged tests cover opaque Matrix room and thread IDs, Signal segments, nested wrappers, alias safety, and map-limit behavior; no test source changes accompany the refactor.
Findings None None.
Security None None.

How this fits together

Session-key normalization converts incoming channel and peer identifiers into stable storage and routing keys while preserving selected opaque IDs. Bounded-map pruning caps in-memory caches used by session and other runtime owners.

flowchart LR
  A[Channel and peer identifiers] --> B[Session-key normalization]
  B --> C[Opaque peer preservation]
  C --> D[Canonical session key]
  E[In-memory cache entries] --> F[Bounded map pruning]
  F --> G[Newest entries retained]
Loading

Before merge

  • Complete next step (P2) - No discrete repair is indicated; retain this member-authored, maintainer-labeled PR for ordinary review and exact-head CI completion.
Agent review details

Security

None.

PR surface

Source -10. Total -10 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 2 52 62 -10
Tests 0 0 0 0
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 52 62 -10

Review metrics

Metric Value Why it matters
Production versus test delta production +52/-62 (net -10); tests +0/-0 The refactor removes repeated work without expanding public, configuration, persistence, or test-support surfaces.

Technical review

Best possible solution:

Keep the session normalizer and bounded-map helper as the sole owners of these operations, with the existing opaque-ID and cache-bound tests preserving their contracts.

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

Not applicable: this PR refactors existing internal work rather than reporting a user-visible failure.

Is this the best way to solve the issue?

Yes. Preparing the immutable matchers in the normalizer and retaining a cursor inside the map-pruning owner removes duplicate work without adding cache state or a parallel path.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 119720344a87.

Labels

Label changes:

  • add P3: This is a behavior-neutral internal performance cleanup with limited blast radius.
  • 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: This maintainer-authored internal refactor does not trigger the external-contributor proof gate; source review ties the changed owners to existing behavior tests, though no local test execution was performed in this read-only review.

Label justifications:

  • P3: This is a behavior-neutral internal performance cleanup with limited blast radius.
  • 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: This maintainer-authored internal refactor does not trigger the external-contributor proof gate; source review ties the changed owners to existing behavior tests, though no local test execution was performed in this read-only review.

Evidence

What I checked:

  • Introduced owner changes: The verified PR delta prepares the three fixed descriptor patterns at module initialization and uses them only in the existing span collector; it also creates one iterator only after a map is known to exceed its positive limit. (src/sessions/session-key-utils.ts:69, 03d59bb19038)
  • Owner boundary and callers: The normalizer remains the single path used by routing and session-store canonicalization; the store owner retains its alias-proof safeguards for Matrix and Signal keys. (src/config/sessions/store-entry.ts:17, 03d59bb19038)
  • Existing behavior coverage: Unchanged tests cover opaque Matrix room and thread IDs, Signal segments, nested wrappers, alias safety, and map-limit behavior; no test source changes accompany the refactor. (src/sessions/session-key-case-preservation.test.ts:138, 119720344a87)
  • Current-main comparison: The reviewed main context retains the old per-call regular-expression construction and per-eviction iterator creation, so this PR is neither already implemented nor obsolete. (src/infra/map-size.ts:13, 119720344a87)
  • Feature-history routing: Recent history includes prior session-normalization work by Peter Steinberger, including commits 870f936 and b5bd6e8; no source-line introduction claim is made because older promisor objects were unavailable. (src/sessions/session-key-utils.ts:69, 870f936851f9)

Likely related people:

  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

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 c00b5f4 into main Aug 31, 2026
308 checks passed
@steipete
steipete deleted the codex/perf-session-key-map branch August 31, 2026 01:42
@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

maintainer Maintainer-authored PR P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. 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