Skip to content

perf(gateway): reuse prepared task parents and attribute row selections - #181732

Merged
steipete merged 2 commits into
mainfrom
steipete/perf-micro-roster-row-selection-prod-20260919
Oct 23, 2026
Merged

steipete merged 2 commits into
mainfrom
steipete/perf-micro-roster-row-selection-prod-20260919

Conversation

@steipete

@steipete steipete commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Task pull-request subscriptions repeatedly resolve parent identities that the session-row owner has already prepared. Production allocation profiles also group several selection paths under the same roster wrapper, obscuring which caller is doing the work.

User Impact

Task descendant selection returns the same results with less allocation. Operators gain bounded selection-kind/caller counters to distinguish broad scans, exact reads, child traversal, and running-candidate lookups. No configuration or schema changes are required.

Why This Change Was Made

Pass the prepared parent row through the existing selector and remove the ordinary-parent physical lookup, which only sentinel parents need. Preserve raw aliases, sentinel behavior, federation, live audience checks, and the existing sharing-revision cache. Ordered broad selections are already retained; unordered catalogs keep their observable resident ordering.

The existing memory sampler carries process-lifetime counters to Prometheus and OpenTelemetry; selections update scalar totals without emitting individual diagnostic events. OTel baselines follow the retained Meter and metric-name prefix, so restarting a service with a preloaded SDK does not double-count prior totals.

Runtime code adds 216 lines and removes 27 (net +189). The selection optimization is net +1 for the typed prepared-row input; the remaining growth implements the requested attribution, exporters, and their lifecycle. No new selection cache or duplicate owner was added.

Evidence

The task traversal regression observed 4 redundant parent resolutions before, 0 after, including after reparenting. All 24 owner tests and 45 focused telemetry tests passed, along with changed-file lint/formatting, the targeted core typecheck, the export collision guard, and 22 import-closure tests. Independent review found no actionable P0–P2 findings. The OTel restart finding from GitHub review was reproduced, fixed, covered with a real-SDK regression (restart, repeated sample, prefix isolation, new provider), and independently reviewed.

Real Gateway measurement

Synthetic fixture: 5,000 sessions, 6,000 retained runs, four tabs watching 50 task keys each. Each side performs 1,000 stored-row updates at a 100 ms interval, 125 sidebar list requests, and 256 subscription refreshes. The driver joins the subscription owner's asynchronous work and validates every watched snapshot. All six runs produced identical initial/final snapshot hashes.

Baseline is 36f471b55b8a plus identical telemetry and harness. The measured candidate owner hashes match final head f9ed0a64ddd9. Pairs 1 and 2 ran concurrently within one exclusive, guarded slot with interleaved CPUs and swapped placement. Pair 3 used another exclusive slot with the same interleaving and guard to resolve the opposite-sign update p99. No pair was interrupted by the guard. A preliminary setup-only failure was discarded: the driver now waits for /readyz before connecting.

Values below are baseline → candidate. Allocation includes sampled collected objects; each update includes its notification work and the stated sidebar mix.

Metric Pair 1 Pair 2, swapped Pair 3
Allocation, MB/update 20.871 → 19.737 20.929 → 19.555 20.981 → 19.732
Main CPU, ms/update 27.856 → 27.406 26.937 → 25.864 31.259 → 29.681
Process CPU, ms/update 44.043 → 47.709 42.794 → 41.170 52.689 → 51.177
Total GC, ms/update 1.745 → 1.857 1.782 → 1.762 2.638 → 2.161
Promotion, MB/update 2.468 → 2.544 2.562 → 2.533 2.610 → 2.589
Update mean, ms 12.004 → 11.288 11.758 → 11.076 15.586 → 14.697
Update p95, ms; n=1,000/side 14.120 → 13.582 14.077 → 13.626 24.245 → 26.517
Update p99, ms; n=1,000/side 17.671 → 24.943 25.588 → 22.363 68.909 → 65.612
Event-loop p99, ms 41.550 → 40.501 41.091 → 38.928 54.034 → 55.443
Event-loop samples 4,473 / 4,627 4,556 / 4,685 4,211 / 4,350
Subscription p95, ms; n=256/side 16.651 → 14.824 16.421 → 16.074 15.985 → 15.825
Maximum GC pause, ms 18.866 → 16.189 29.050 → 19.653 334.935 → 56.922
Minor collections 721 / 683 706 / 686 698 / 677
Minor GC p95, ms 2.638 → 2.830 3.070 → 3.150 4.190 → 3.790
Major collections 9 / 18 8 / 8 18 / 18

The original swapped-pair mean is −6.00% allocation and −2.80% main CPU. Across all three pairs these are −5.98% (−1.252 MB/update) and −3.55% (−1.033 ms/update). Update p99 changed by +7.272, −3.225, and −3.297 ms: the opposite-sign result is decided on its +0.250 ms absolute mean, not a relative mean dominated by the smallest baseline. Other mixed-sign absolute means: process CPU +0.177 ms/update, total GC −0.128 ms/update, promotion +0.0086 MB/update, and event-loop p99 −0.601 ms. None crosses the adverse gate.

Acceptance: reject ≥20% adverse paired mean or ≥10% adverse in both swapped pairs. Opposite signs or >5× baseline spread require absolute deltas and a third pair for a gating metric. p99 requires 1,000 samples/side and p95 requires 200. Minor p95's absolute mean is −0.043 ms, within the sub-millisecond context rule; minor p99 and major pause tails are context-only at these collection counts. Candidate maximum pauses are below max(50 ms, 2 × baseline maximum) in each pair. Sidebar tails have only 125 samples/side and are context: mean latency deltas are −0.588, −0.338, and +0.697 ms.

Attribution: each full run made 430,000 detached parent selections, about 500 additional parent selections attributed to task-PR work, and only one broad sidebar selection, with no broad resets. This reproduces the parent path that a warm sidebar-only workload missed. A separate 100-update qualification profile reduced the selector allocation subtree from 242.4 to 106.5 MB and its parent-resolution subtree from 80.8 to 3.1 MB; those overlapping subtrees must not be added.

Reproduce with the committed scripts/perf/session-pr-subscriptions-measure.ts and fixture/paired-run instructions in scripts/perf/README.md:

node --import ./scripts/tsx.mjs scripts/perf/session-pr-subscriptions-measure.ts \
  --root <fixture> --output <results> --port <loopback-port> \
  --clients 4 --keys 50 --operations 1000 --subscribes 256 \
  --interval-ms 100 --list-every 8 --start-at <shared-future-epoch-ms>

Limits: this is a synthetic caller ratio, not a claim of an exact production speedup. Both sides include identical telemetry, so its overhead is not isolated. No production deployment was performed; PR CI supplies the broad validation.

Production follow-up: compare rate(openclaw_session_row_selections_total[5m]) and rate(openclaw_session_row_selection_rows_total[5m]) grouped by kind,task, alongside rate(openclaw_session_row_selection_resets_total[5m]). Include parent/other: detached work does not necessarily inherit the originating RPC task. A follow-up allocation profile should show the reduced selectSessionRowParent subtree at a comparable selection rate.

@clawsweeper

clawsweeper Bot commented Oct 23, 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 docs Improvements or additions to documentation gateway Gateway runtime extensions: diagnostics-otel Extension: diagnostics-otel scripts Repository scripts extensions: diagnostics-prometheus size: M labels Oct 23, 2026
@steipete
steipete marked this pull request as ready for review October 23, 2026 15:42
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-23T15:57:34.991068Z f9ed0a6 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7c46564fcd

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@steipete
steipete force-pushed the steipete/perf-micro-roster-row-selection-prod-20260919 branch from 7c46564 to f9ed0a6 Compare October 23, 2026 15:53
@steipete

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

The OTel restart finding is fixed and covered with a real-SDK regression. The branch, PR body, and all three paired measurements are current at f9ed0a6.

@clawsweeper

clawsweeper Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:feature Preliminary submission classification; full source review pending. 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. and removed clawsweeper:intake:feature Preliminary submission classification; full source review pending. labels Oct 23, 2026
@clawsweeper

clawsweeper Bot commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR reuses prepared task-parent identities and exports session-row selection counts through Prometheus and OpenTelemetry.

Example: An OTel service samples 4 parent selections, restarts with the same SDK, then samples a cumulative total of 6.

  • Before: The provider exports 10 selections because the restarted recorder recounts the first 4.
  • After: The provider exports 6 selections, and repeated samples add nothing.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A useful, focused optimization with recorded Gateway measurements, meaningful lifecycle coverage, and no actionable findings; measurements isolate the selector change with identical telemetry on both sides.
Proof confidence 🐚 platinum hermit (4/6) This maintainer-authored PR is outside the ordinary contributor proof gate; its supplied real-Gateway measurements exercise stored-row updates, sidebar RPCs, and task PR subscriptions with matching snapshots, while the real-SDK regression covers restart accounting.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Performance · Worth it: Yes · Fix scope: Complete
User problem: Task PR subscriptions allocate unnecessarily while repeatedly resolving known parent identities, and operators cannot distinguish the selection callers responsible.
Reason: The maintainer-authored direction has measured allocation benefits, retains existing session behavior, and adds bounded operational attribution.

Merge readiness

✅ Ready for maintainer review

This PR provides a measured allocation improvement and useful bounded telemetry; no actionable defect remains in the reviewed diff, including the OTel restart repair.

Priority: P2
Reviewed head: f9ed0a64ddd9b9d71b33edb07d6897f8587fad12

Before merge

None.

Findings

None.

Agent review details

How this fits together

The Gateway session-row projection selects resident descendants for task PR subscriptions, while its existing memory sampler carries cumulative selection totals to diagnostic exporters.

flowchart LR
  A[Task PR subscriptions] --> B[Prepared parent row]
  B --> C[Resident parent index]
  C --> D[Live audience checks]
  C --> E[Selection counters]
  E --> F[Memory sampler]
  F --> G[Prometheus and OpenTelemetry]
Loading

Technical review

Best possible solution:

Retain the existing projection and sampler owners, carry prepared parent identities directly, and preserve exporter baselines for the lifetime of their instruments.

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

Current main passes parent keys back through lineage resolution despite already holding prepared rows; the supplied Gateway measurements exercise that path, and the restart regression directly encodes the earlier recounting failure.

Is this the best way to solve the issue?

The change removes repeated work at the existing owner and adds bounded counters through existing exporters without introducing a competing selection cache.

AGENTS.md: found and applied where relevant.

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

Provenance checked

  • Task descendant selection and revision-bound reuse keeps the original intent (bb842bf: Share structural selections while retaining live audience checks, incognito exclusion, and revision freshness.)
  • Parent-index lookup and sentinel handling keeps the original intent (2594ae8: Bound child selection by indexed parent candidates while preserving physical competitors, federation, and absent sentinel parents.)
  • Stored literal parents and alias resolution keeps the original intent (ff27365: Preserve existing stored addresses before applying missing-parent alias fallback; older blob inspection was locally limited, so current-main blame and the original PR supplied the contract.)
  • Cumulative telemetry transport keeps the original intent (6548f17: Carry bounded cumulative work totals through memory samples without emitting an event for every operation.)
  • Benchmark owner substitution and settlement keeps the original intent (bb842bf: Measure the real Gateway subscription owner with verified source substitutions and native asynchronous settlement.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

Likely related people:

  • Vincent Koc: Raw commit ff27365 adds src/gateway/session-row-projection-record.ts:416 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: medium; commits: ff2736533bed; files: src/gateway/session-row-projection-record.ts)

PR surface

Source +189, Tests +124, Docs +24, Other +48. Total +385 across 25 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 12 216 27 +189
Tests 7 129 5 +124
Docs 3 28 4 +24
Config 0 0 0 0
Generated 0 0 0 0
Other 3 53 5 +48
Total 25 426 41 +385

Review metrics

Metric Value Why it matters
Selection metric cardinality At most 99 label sets across three new counter families Seven selection kinds and seven fixed caller buckets bound each labeled family to 49 combinations, with one unlabeled reset series.

Labels

Label changes:

  • add P2: A bounded Gateway performance and observability improvement with measured value and no demonstrated urgent user-facing regression.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Label justifications:

  • P2: A bounded Gateway performance and observability improvement with measured value and no demonstrated urgent user-facing regression.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

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 23, 2026, 12:06 PM ET / 16:06 UTC.

@steipete
steipete merged commit cee236c into main Oct 23, 2026
164 checks passed
@steipete
steipete deleted the steipete/perf-micro-roster-row-selection-prod-20260919 branch October 23, 2026 16:16
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 24, 2026
…ns (openclaw#181732)

* perf(gateway): reuse prepared task parents and attribute row selections

* fix(diagnostics-otel): retain row selection totals across restarts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation extensions: diagnostics-otel Extension: diagnostics-otel extensions: diagnostics-prometheus gateway Gateway runtime P2 Normal backlog priority with limited blast radius. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. scripts Repository scripts size: M 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