Skip to content

perf(gateway): stop per-row store and registry rediscovery in sessions.list - #114659

Merged
steipete merged 4 commits into
mainfrom
perf/sessions-list-target-discovery
Jul 27, 2026
Merged

steipete merged 4 commits into
mainfrom
perf/sessions-list-target-discovery

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

sessions.list on team.openclaw.ai (avg 587ms, max 2.8s at ~300 sessions/3 agent stores) spent its dominant flag-independent cost on per-row rediscovery: every returned row re-ran session-store target discovery (readdirSync over the agents root, 3-4 lstatSync/realpathSync.native per agent dir, and three full state-DB registry reads — each re-running schema-migration detection probes), plus a per-row ACP metadata query, a structuredClone of every entry for the sharing lookup, and a rebuild of the live-run registries per row. The whole sharing/membership stage was also the only part of the handler with no diagnostics span, making it invisible in production timelines.

Why This Change Was Made

Five bounded fixes at their natural owners:

  • Target discovery is threaded through a request-scoped cache keyed by agentId (mirroring the existing GatewaySessionStoreCache pattern): at most one discovery per agent per request.
  • listOpenClawRegisteredAgentDatabases gains a single-slot process-level memo keyed by resolved registry path, invalidated by this module's own write paths post-commit — registry metadata is process-stable per repo policy; other-process changes take effect on restart, and the comment names that contract.
  • ACP metadata honors the in-memory entry marker and batches the remaining rows into one lifecycle-validated IN (...) query per request (~300 queries → 1).
  • The sharing lookup passes clone: false (that path reads only createdActor.id/visibility; the cache doc-comment already required read-only use).
  • Live-run registries are captured once per request and indexed by sessionKey instead of rescanned per row; and the sharing stage now has a gateway.sessions.list.sharing diagnostics span.

User Impact

The Control UI's most frequent heavy call drops its dominant per-row costs: roughly 7,500-9,000 SQLite statements and 9,000-18,000 filesystem calls per request reduced to low tens and ~90-180 respectively (>99%). No response-shape or behavior change.

Evidence

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: L maintainer Maintainer-authored PR labels Jul 27, 2026
@steipete
steipete marked this pull request as ready for review July 27, 2026 17:36
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. labels Jul 27, 2026
@clawsweeper

clawsweeper Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed July 27, 2026, 2:33 PM ET / 18:33 UTC.

ClawSweeper review

What this changes

This PR makes sessions.list reuse request-scoped discovery and active-run data, batch ACP metadata reads, and memoize agent-database registry listings to avoid repeated filesystem and SQLite work for every returned session row.

Merge readiness

⛔ Blocked until real behavior proof from a real setup is added - 4 items remain

Keep this PR open for normal maintainer review. It presents a bounded performance refactor across the gateway session-listing path, but the available evidence still contains tests and prior baseline measurements rather than inspectable after-fix behavior from a real deployment; the protected maintainer label also requires explicit human handling.

Priority: P2
Reviewed head: 6eb2f2e8bcc9c76148e1449db8b6a73b1412af5f

Review scores

Measure Result What it means
Overall readiness 🦪 silver shellfish (2/6) The implementation and focused regression coverage are promising, but missing after-fix real behavior proof remains a merge gate.
Proof confidence 🦪 silver shellfish (2/6) Needs real behavior proof before merge: The PR provides tests, CI, and prior baseline measurements, but no inspectable after-fix output from a real session-listing workload; please attach redacted timing or diagnostics evidence before merge. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Needs proof Needs real behavior proof before merge: The PR provides tests, CI, and prior baseline measurements, but no inspectable after-fix output from a real session-listing workload; please attach redacted timing or diagnostics evidence before merge. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Evidence reviewed 4 items Changed runtime path: The branch threads a request-scoped store-target discovery cache through sharing resolution, prepares active-run indexes once, and adds batched ACP metadata lookup for session-list row projection.
Registry invalidation contract: The new listing module memoizes registered agent databases by resolved registry path, while registry write paths invalidate that memo after commit; the branch also adds a regression test for write invalidation.
Regression coverage supplied: The PR context reports focused regressions for single discovery per agent, ACP batch parity, active-run index parity, and registry-memo invalidation, plus successful completed CI boundary and security checks on the branch.
Findings None None.
Security None None.

How this fits together

The gateway’s sessions.list handler gathers session rows for Control UI and other gateway clients, then enriches each row with sharing, ACP, and active-run state. This PR changes the per-request data preparation that feeds those row projections without changing the response shape.

flowchart LR
  A[Control UI or gateway client] --> B[sessions.list handler]
  B --> C[Session store discovery]
  B --> D[ACP metadata lookup]
  B --> E[Active-run registries]
  C --> F[Sharing and row projection]
  D --> F
  E --> F
  F --> G[Session list response]
Loading

Before merge

  • Add real behavior proof - Needs real behavior proof before merge: The PR provides tests, CI, and prior baseline measurements, but no inspectable after-fix output from a real session-listing workload; please attach redacted timing or diagnostics evidence before merge. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
  • Resolve merge risk (P1) - The process-level registry memo intentionally makes registry changes performed by another process visible after gateway restart rather than through request-time polling; that operator contract needs real after-fix evidence before merge.
  • Resolve merge risk (P1) - The branch is behind current main, so maintainers should refresh the merge result before treating the supplied focused validation as final.
  • Complete next step (P2) - A maintainer should review the restart-scoped registry visibility contract after the contributor supplies real after-fix performance evidence; this is not a safe automated repair task.
Agent review details

Security

None.

PR surface

Source +220, Tests +224, Other +1. Total +445 across 17 files.

View PR surface stats
Area Files Added Removed Net
Source 12 407 187 +220
Tests 4 225 1 +224
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 1 1 0 +1
Total 17 633 188 +445

Review metrics

None.

Stored data model

Persistent data-model change detected: migration/backfill/repair: src/state/openclaw-agent-db-registry-listing.ts, migration/backfill/repair: src/state/openclaw-agent-db-registry.ts, persistent cache schema: src/gateway/session-utils-store-lookup.ts, serialized state: src/acp/runtime/session-meta.ts, serialized state: src/gateway/server-methods/session-active-runs.test.ts, serialized state: src/gateway/server-methods/session-active-runs.ts, and 20 more. Confirm migration or upgrade compatibility proof before merge.

Merge-risk options

Maintainer options:

  1. Prove the registry visibility contract before merge (recommended)
    Attach redacted real-run diagnostics that show the optimized request path and demonstrate that same-process registry writes invalidate immediately while externally written registry changes follow the documented restart boundary.
  2. Accept restart-scoped registry visibility
    A maintainer may explicitly accept the process-stable registry contract if restart-only discovery of external registry edits is intentional for all supported deployments.

Technical review

Best possible solution:

Keep the cache and batching changes at their current owner boundaries, then attach redacted before/after diagnostics from a real sessions.list workload that shows the reduced sharing-stage work and confirms the documented registry-update behavior.

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

No high-confidence after-fix reproduction is available in the supplied review evidence. The PR describes a concrete production workload and adds focused tests, but it does not include inspectable post-change timing, diagnostics, or a redacted live trace.

Is this the best way to solve the issue?

Unclear until real behavior proof is attached. The cache placement, batching, and write-side invalidation align with the repository’s prepared-facts and process-stable-metadata guidance, but the user-visible performance and restart-scoped registry behavior still need direct confirmation.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 3923a9c8b9ca.

Labels

Label changes:

  • add rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🐚 platinum hermit.
  • remove rating: 🧂 unranked krab: Current PR rating is rating: 🦪 silver shellfish, so this older rating label is no longer current.

Label justifications:

  • P2: This is a targeted performance improvement to a frequently used gateway path, with no demonstrated functional outage or release-blocking regression.
  • merge-risk: 🚨 compatibility: The registry listing changes from fresh reads to a process-lifetime memo, so external registry edits now rely on the documented restart boundary.
  • rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🐚 platinum hermit.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: The PR provides tests, CI, and prior baseline measurements, but no inspectable after-fix output from a real session-listing workload; please attach redacted timing or diagnostics evidence before merge. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Evidence

What I checked:

  • Changed runtime path: The branch threads a request-scoped store-target discovery cache through sharing resolution, prepares active-run indexes once, and adds batched ACP metadata lookup for session-list row projection. (src/gateway/server-methods/sessions-read.ts:286, 6eb2f2e8bcc9)
  • Registry invalidation contract: The new listing module memoizes registered agent databases by resolved registry path, while registry write paths invalidate that memo after commit; the branch also adds a regression test for write invalidation. (src/state/openclaw-agent-db-registry-listing.ts:15, 6eb2f2e8bcc9)
  • Regression coverage supplied: The PR context reports focused regressions for single discovery per agent, ACP batch parity, active-run index parity, and registry-memo invalidation, plus successful completed CI boundary and security checks on the branch. (src/gateway/session-utils.perf.test.ts:140, 6eb2f2e8bcc9)
  • Proof gate remains unmet: The prior ClawSweeper review requested inspectable after-fix runtime evidence. The current PR body reports tests, CI, and an earlier production cost audit, but does not provide a redacted after-fix trace, timing output, or live artifact demonstrating the changed request behavior. (6eb2f2e8bcc9)

Likely related people:

Rank-up moves

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

  • Attach a redacted before/after sessions.list diagnostic trace or timing transcript from a real installation; remove private endpoints, IPs, tokens, and other sensitive data.
  • Refresh the branch against current main and ensure the attached proof reflects the resulting merge candidate.

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.

History

Review history (1 earlier review cycle)
  • reviewed 2026-07-27T17:51:05.373Z sha 87d05c4 :: needs real behavior proof before merge. :: none

@openclaw-barnacle openclaw-barnacle Bot added the scripts Repository scripts label Jul 27, 2026
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. labels Jul 27, 2026
@steipete
steipete force-pushed the perf/sessions-list-target-discovery branch from 6eb2f2e to e2ce398 Compare July 27, 2026 18:48
@steipete
steipete merged commit ab0b749 into main Jul 27, 2026
120 checks passed
@steipete
steipete deleted the perf/sessions-list-target-discovery branch July 27, 2026 19:04
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 28, 2026
…s.list (openclaw#114659)

* perf(gateway): stop per-row store and registry rediscovery in sessions.list

* fix(gateway): satisfy kysely guardrail and knip on sessions.list rediscovery fix

* refactor(state): split registered agent database listing from the registry

* chore(state): allowlist the split registry listing module for raw sqlite probes
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 merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P2 Normal backlog priority with limited blast radius. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. scripts Repository scripts size: L status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant