Skip to content

fix(sessions): reduce transcript reader memory and startup work - #177242

Open
vincentkoc wants to merge 3 commits into
mainfrom
fix/transcript-reader-imports-20261019
Open

vincentkoc wants to merge 3 commits into
mainfrom
fix/transcript-reader-imports-20261019

Conversation

@vincentkoc

@vincentkoc vincentkoc commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

What Problem This Solves

Fixes unnecessary memory and startup work in session transcript readers. A model-context read imported writable agent/state database initialization and write execution, even when its worker only read an existing transcript.

Why This Change Was Made

Separate read observation from writable database acquisition. Readers observe the existing lifecycle owner without creating it; that owner continues to retain handles and refresh idle custody. Local admission, creation/deletion guards, committed-read companions, and owner inspection behavior remain intact.

Move write execution into the existing write-scope owner and transcript state reads into a read-only module. Update internal callers together. No config, schema-version, protocol, or public Plugin SDK contract changes.

User Impact

Lower per-reader module footprint without disabling session history or changing transcript contents. This is a targeted memory reduction, not a claim that the overall Gateway RSS problem is solved.

Release-note context: session transcript readers avoid loading writable database initialization and preserve existing handle lifecycle behavior. Release-owned changelog files are unchanged.

Evidence

Landing hold: the reader heap target passes, but the complete Gateway series does not satisfy the no-regression rule. Process CPU rose 6.48% (one plugin) / 4.88% (five plugins); additional regressions are listed below. No owner waiver has been requested or granted. Do not merge. Exact-head CI is pending after the wrapper inventory repair.

Completed diagnostic and development checks:

  • A compiled model-context worker returned identical full transcript events/version for 31 reads and an additional bounded read; one worker was reused and cleanup reached zero workers.
  • Instrumented diagnostic: loaded files 1,366 → 719; loaded JavaScript 8,806,529 → 6,516,957 bytes; worker heap at 5 seconds after drain 37,706,592 → 29,467,552 bytes. This is one diagnostic comparison using the existing proof checkout, excluded from the scored series. Process RSS includes the host and all workers.
  • Three initial focused files and eight sibling files passed, including committed reads, idle/borrow custody, reader-before-writer startup, native/transformed module identity, migration ordering, workspace recovery, and affected mocks.
  • Core and all core-test typecheck shards passed on the diagnostic source. Script typecheck and changed-file lint passed after the final narrow fixes. The refreshed independent candidate package build and three overlap tests passed at c180634279453642298f9ffce70fbe4412cd72c7.
  • The earlier diagnostic changed check exposed inherited failures on base b6e0872: unchanged UI max-lines suppressions and test-root shard counts (Gateway 724, plugins/platform 725, over the local 720 guard). No transcript test roots were added or removed. These are prior-base findings, not a claim about the refreshed candidate CI. The dead exports reported on the separate plugin experiment are absent from this independent source.
  • Independent review: no actionable P0–P2 findings in the supplied change and owner context.

Acceptance rule, recorded before scored runs

Baseline: 357be061b3e1894b19410ffd9bf17c77819cbb9e. Candidate: c180634279453642298f9ffce70fbe4412cd72c7. Use the same runtime, dependencies, host, resource limits, and committed workload on both builds. Record actual build identity, harness hashes, host load, and all samples. Use a fresh task-owned temp root for every process and unset NODE_COMPILE_CACHE on both variants; the Gateway fixture also owns its worker bytecode cache.

  1. Reader workload: env -u NODE_COMPILE_CACHE node --import ./scripts/tsx.mjs scripts/bench-transcript-reader.ts --output <new-report.json>. Five serial interleaved baseline/candidate pairs. Each fresh process runs one cold plus 30 warm reads of an 80-message SQLite transcript, one bounded read, and heap samples at 2/5 seconds after drain. Require full output equality, exactly one reused worker, no pending tasks, and zero workers after cleanup. Target: at least 10% lower median worker heap at 5 seconds. Report cold and warm latency, CPU, process RSS, median, p95, and range separately; RSS is process-wide, never summed per worker.
  2. Real Gateway regression workload: the common committed harness at 7d63c2fd41aaf978ecaff8a02cb347518cd2fb4b (scripts/bench-gateway-concurrency.ts, scripts/lib/gateway-bench-runtime.ts, scripts/lib/gateway-bench-probes.ts) applied identically to both source trees after building. Five serial AB pairs for each plugin count 1 and 5:
    node scripts/bench-gateway-concurrency.ts --entry dist/entry.js --gateway-runtime node --provider mock --plugin-count <1-or-5> --concurrency 1 --agent-warmup-turns 1 --turns-per-session 3 --runs 1 --warmup 0 --cadence-ms 100 --stream-chunk-delay-ms 1000 --json --output <new-report.json>.
    Leave session-count/history/profiling options unset. Require matching completed turns, inference payloads, activated plugins, and clean cleanup. Preparation workers retain their existing lifecycle on both sides.
  3. Keep every sample. Separate diagnostics/profiles from the scored series. A measured user-facing regression needs explicit owner acceptance; a 20% regression in any cell stops landing. Inconclusive memory or regression evidence remains a hold, not a passing result.

The reader benchmark is committed here because the existing history benchmark measures source-host query execution and SQL tracing rather than a compiled model-context worker.

Scope accounting: runtime +604/-539 (net +65), tests/support +133/-113 (net +20), benchmark +234. The runtime increase supplies the read-only observation boundary without importing writer initialization; existing write operations and transcript-read implementations move to their canonical owner modules.

Conflict repair: newer main renamed the maintenance test import to runWrite. The rebased patch preserves that alias and redirects only its module path. No runtime patch changed. Both scored build identities were refreshed to the new base before any scored run; the earlier diagnostic comparison remains excluded.

Named follow-up: agent database workers still import the host SQLite broker through the synchronous deleteAgentProvenanceForAgent helper. Its existing kernel module is a possible owner; compiled loading and memory benefit still need measurement. This PR does not change that path.

Repeated reader results

All ten scored reader runs completed: five serial AB pairs, each with 31 full reads and one bounded read, exactly one reused transcript worker, and zero workers after cleanup. Every returned event and context version matched that run's synchronous canonical reader. The fixture hash is identical across all ten runs; whole-response hashes differ because each fresh transcript receives its own generation UUID. No sample was discarded.

Metric Baseline median / p95 [range] Candidate median / p95 [range] Median delta
Worker heap 5s MiB 35.828 / 35.834 [35.827-35.834] 27.999 / 28.000 [27.999-28.000] -21.85%
Process RSS 5s MiB 552.344 / 561.938 [546.266-561.938] 558.828 / 586.375 [545.672-586.375] +1.17%
Cold read ms 260.492 / 305.814 [257.673-305.814] 190.255 / 194.492 [183.043-194.492] -26.96%
Warm read median ms 2.706 / 2.902 [2.681-2.902] 2.721 / 2.857 [2.636-2.857] +0.56%
Warm read p95 ms 3.967 / 6.360 [3.746-6.360] 3.460 / 3.540 [3.340-3.540] -12.79%
Cold process CPU ms 326.268 / 387.772 [321.911-387.772] 252.183 / 313.401 [245.524-313.401] -22.71%
Warm total process CPU ms 137.536 / 142.926 [132.824-142.926] 112.564 / 116.273 [110.749-116.273] -18.16%

Worker heap improved consistently by 7.83 MiB (21.85%). Cold read latency and CPU improved. Warm median latency moved +0.015 ms (+0.56%); process-wide source-host RSS moved +6.48 MiB (+1.17%), so this does not establish a total RSS gain. Gateway regression acceptance fails the stated no-regression rule; see the complete results below. With five samples, nearest-rank p95 is the observed maximum, not a stable population-tail estimate.

Build identities: baseline 357be061b3e1894b19410ffd9bf17c77819cbb9e, output SHA-256 aa79db75ad766a7a21660418f6b6d1e0bfba5ce6bb07bc6cba8ad1804ad6a0d2; candidate c180634279453642298f9ffce70fbe4412cd72c7, output SHA-256 d7db0fc166b3aaf9bd46f5119e0ea6ac4dc6a9db105b00add4a56106ef5e1b89. Both used the package build profile and the same dependency installation. Node 26.7.0, macOS arm64, 18 logical CPUs, 48 GiB physical memory.

Complete Gateway regression results

All 20 runs are included: five serial AB pairs for each plugin count. Every run completed one verified warmup and three verified measured turns, activated every fixture plugin, and exited normally with code 0 after teardown. All readiness, session-list and Control UI probes succeeded. Worker counts were 26 (one plugin) / 30 (five plugins), including 1/5 retained preparation workers on both variants.

Plugins Metric Baseline median / p95 [range] Candidate median / p95 [range] Median delta p95 delta
1 End RSS MiB 2287.672 / 2323.688 [2046.812-2323.688] 2201.391 / 2350.078 [1772.406-2350.078] -3.77% +1.14%
1 Peak RSS MiB 2291.562 / 2395.484 [2072.094-2395.484] 2295.625 / 2464.578 [1864.844-2464.578] +0.18% +2.88%
1 Main heap MiB 321.524 / 332.891 [305.039-332.891] 300.415 / 318.646 [290.152-318.646] -6.57% -4.28%
1 Ready startup ms 4538.500 / 5308.500 [4343.900-5308.500] 4445.800 / 5001.700 [4353.100-5001.700] -2.04% -5.78%
1 Setup ms 3501.898 / 3581.946 [3355.823-3581.946] 3547.390 / 3660.384 [3401.708-3660.384] +1.30% +2.19%
1 Three-turn load ms 3614.230 / 3719.325 [3611.478-3719.325] 3619.711 / 3714.670 [3613.430-3714.670] +0.15% -0.13%
1 Total process CPU ms 2037.296 / 2589.037 [1974.674-2589.037] 2169.240 / 2303.339 [2051.255-2303.339] +6.48% -11.03%
1 Main-thread CPU ms 570.371 / 613.202 [534.754-613.202] 557.652 / 586.470 [537.943-586.470] -2.23% -4.36%
1 readyz per-run p95 ms 3.190 / 31.708 [1.900-31.708] 3.521 / 4.811 [1.917-4.811] +10.38% -84.83%
1 sessionsList per-run p95 ms 4.497 / 31.657 [2.527-31.657] 4.685 / 10.528 [2.782-10.528] +4.20% -66.74%
1 controlUi per-run p95 ms 9.039 / 49.948 [8.391-49.948] 10.459 / 13.180 [6.684-13.180] +15.70% -73.61%
5 End RSS MiB 2505.156 / 2544.984 [1761.156-2544.984] 2490.047 / 2516.688 [2471.109-2516.688] -0.60% -1.11%
5 Peak RSS MiB 2505.531 / 2598.375 [2372.938-2598.375] 2564.234 / 2593.906 [2547.844-2593.906] +2.34% -0.17%
5 Main heap MiB 322.435 / 337.658 [313.688-337.658] 304.781 / 317.542 [297.520-317.542] -5.48% -5.96%
5 Ready startup ms 5126.300 / 5409.100 [4963.800-5409.100] 5205.000 / 6044.900 [4901.800-6044.900] +1.54% +11.75%
5 Setup ms 3575.436 / 4084.886 [3501.410-4084.886] 3476.037 / 3656.945 [3416.290-3656.945] -2.78% -10.48%
5 Three-turn load ms 3615.998 / 3917.048 [3610.178-3917.048] 3619.073 / 3716.836 [3606.126-3716.836] +0.09% -5.11%
5 Total process CPU ms 2120.314 / 2599.229 [2016.004-2599.229] 2223.804 / 2464.116 [2029.293-2464.116] +4.88% -5.20%
5 Main-thread CPU ms 602.630 / 732.217 [554.995-732.217] 566.188 / 655.459 [534.510-655.459] -6.05% -10.48%
5 readyz per-run p95 ms 3.891 / 6.486 [2.464-6.486] 3.732 / 4.567 [2.586-4.567] -4.10% -29.58%
5 sessionsList per-run p95 ms 5.286 / 8.787 [2.424-8.787] 4.532 / 5.277 [3.256-5.277] -14.27% -39.95%
5 controlUi per-run p95 ms 9.189 / 14.360 [6.997-14.360] 7.709 / 10.737 [6.952-10.737] -16.11% -25.23%

With five runs, p95 across runs is the observed maximum. Probe rows summarize each run's p95, not a pooled percentile. The 100 ms adaptive sampler produced 36-39 rounds; counts vary with measured duration. No sample is excluded and no regression is waived as noise.

Work completed is consistent across all runs: warmup sends 3 inference requests (2 recap, 1 turn); load sends 6 inference requests (3 recap, 3 turns) plus 2 embedding requests. Setup and post-load snapshots contain no additional requests; unknown payload bytes are zero. Baseline warmup/load request bytes are 81,076/245,174; candidate 81,116/245,294. The +40 bytes per turn are a remaining fixture-comparability limitation: candidate temp-root labels are five characters longer and fixture paths enter prompts, but raw payload equality was not retained, so this report does not assert byte-identical inference payloads. The stricter payload acceptance condition is therefore unproven.

Decision: hold. Reader heap improved by 7.83 MiB, but this is not a meaningful resolution of the 2.2-2.5 GiB Gateway footprint. Total CPU medians worsen on both Gateway cohorts; one-plugin probe medians and five-plugin startup/peak RSS also worsen. The prewritten rule requires explicit owner acceptance for any measured regression. No merge is authorized by these results.

CI repair at HEAD 480fc26: add the three new runtime module paths to the PR wrapper extraction inventory. The failed closure tests now pass locally under the default runtime and explicit pinned Bun (2 files, 22 tests each); the later current-head CI attribution is recorded below. This metadata-only change does not alter runtime source or compiled output, so scored runs retain candidate identity c180634; they are not relabeled as exact-head CI proof.

Additional sampled diagnostics (median of each per-run percentile):

Plugins Metric Baseline Candidate Delta
1 readyzLatencyMs p50 1.5284 1.4963 -2.10%
1 readyzLatencyMs p95 3.1902 3.5212 +10.38%
1 sessionsListLatencyMs p50 1.1833 1.1434 -3.38%
1 sessionsListLatencyMs p95 4.4965 4.6854 +4.20%
1 controlUiLatencyMs p50 3.3509 3.3105 -1.21%
1 controlUiLatencyMs p95 9.0393 10.4588 +15.70%
1 freshConnectionLatencyMs p50 2.8705 2.6271 -8.48%
1 freshConnectionLatencyMs p95 2.8705 2.6271 -8.48%
1 eventLoopDelayP99Ms p50 23.4000 23.7000 +1.28%
1 eventLoopDelayP99Ms p95 23.8000 27.4000 +15.13%
1 eventLoopDelayMaxMs p50 23.4000 23.7000 +1.28%
1 eventLoopDelayMaxMs p95 23.8000 27.4000 +15.13%
1 eventLoopUtilization p50 0.3330 0.3240 -2.70%
1 eventLoopUtilization p95 0.3990 0.4360 +9.27%
1 cpuCoreRatio p50 0.8350 0.5650 -32.34%
1 cpuCoreRatio p95 1.3380 1.5940 +19.13%
5 readyzLatencyMs p50 1.5573 1.5274 -1.92%
5 readyzLatencyMs p95 3.8909 3.7315 -4.10%
5 sessionsListLatencyMs p50 1.1661 1.2655 +8.52%
5 sessionsListLatencyMs p95 5.2861 4.5315 -14.27%
5 controlUiLatencyMs p50 3.3491 3.3321 -0.51%
5 controlUiLatencyMs p95 9.1886 7.7087 -16.11%
5 freshConnectionLatencyMs p50 2.5888 2.8823 +11.34%
5 freshConnectionLatencyMs p95 2.5888 2.8823 +11.34%
5 eventLoopDelayP99Ms p50 26.8000 21.8000 -18.66%
5 eventLoopDelayP99Ms p95 29.1000 26.3000 -9.62%
5 eventLoopDelayMaxMs p50 26.8000 21.8000 -18.66%
5 eventLoopDelayMaxMs p95 29.1000 26.3000 -9.62%
5 eventLoopUtilization p50 0.3560 0.3330 -6.46%
5 eventLoopUtilization p95 0.4290 0.4440 +3.50%
5 cpuCoreRatio p50 0.4640 0.6480 +39.66%
5 cpuCoreRatio p95 1.4640 1.4970 +2.25%

The sampled five-plugin CPU-core-ratio median rises 39.66%, also exceeding the 20% stop threshold. The integrated process CPU measurements above remain the total-work measure; both are reported. One-minute host load before runs ranged 5.51-8.68 for p1 and 4.07-5.53 for p5. No score is discarded for host load.

CI repair and remaining attribution

Current head c7f91fb repairs the registry-listing spy in targets-dedupe.test.ts; all four focused tests pass. Runtime remains the measured c180634. The performance hold above is unchanged.

The failed CI run 39239246910 tested merge e0b66e08 (head 480fc26 into main b87a318), not the original performance baseline. Its Board test failure is inherited from that main parent: test and lifecycle blobs match, and main commit ae0ff9c fixes the assertion to match accepted reads draining before binding retirement (#177223).

The restored-requester final E2E failure remains unattributed: test/gateway-restored-requester-settle.e2e.test.ts:87 observed zero first provider requests after an agent-request timeout; other three cases passed. Unchanged test/deadline source alone does not prove inheritance. Named follow-up: compare the exact final case on CI main parent and merge candidate with matching builds, retaining the existing 2-second announcement budget. No production workaround or green claim.

Current-head CI attribution: c7f91

Run https://github.com/openclaw/openclaw/actions/runs/39284795736 tested integration 9deecef052ed66948ddf0c6f2aeebbdf322fa39d (c7f91fb into main parent 9acb9a7). Seven failed jobs contain eight failures. Four are inherited; four remain unattributed. No reader-source repair is justified by the evidence yet, and this is not an inherited-only landing claim.

Failure Attribution
Census mock-export ratchet Inherited main mock; fixed by cc942bc.
sessions.abort-yielded.test.ts:314, pending=false child outcome absent Unknown; reproduce baseline and integration.
sessions-create-worktree-base.test.ts:301, rejected recovery changes saved row Unknown; distinguish request mutation from concurrent title publication.
server.chat.gateway-server-chat.test.ts, failed dispatch/restart drain timeout Same 120-second test failure on main job118149896161.
attempt.settled-turn-finalization-context.test.ts:139, two hidden internal prompts retained Inherited main07b59a0976dd intentional runtime persistence; producer/test blobs match integration main parent; same main failure. Main248aa9a5f6781a94a59c6b5103df43025aa3242c repairs the assertion while checking hidden provenance.
server-agent-database-startup.test.ts:693, recover lifecycle chat.send ok:false Same test failure on main job118149896203.
gateway-openai-compaction-replay.e2e.test.ts:102, duplicate continuation text Unknown; new internal-prompt retention is a lead, not causal proof.
gateway-restored-requester-settle.e2e.test.ts, final deadline Unknown; startup readiness passed, subsequent recovery did not.

Independent main failures come from https://github.com/openclaw/openclaw/actions/runs/39278710829 at af49c87, an ancestor of the integration main parent. Current main c8112c7 was checked for both completed assertion/mock repairs.

Named follow-up: run the four unknown cases serially on exact main parent9acb9a74 and integration9deecef0 with qualified matching dependencies, CI runtime, and matching built dist for E2E. Retain existing deadlines and collect original failure evidence; a passing isolated replay alone does not prove a fix. The performance hold and earlier unequal inference-byte evidence remain unchanged.

@clawsweeper

clawsweeper Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@vincentkoc vincentkoc self-assigned this Oct 19, 2026
@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime scripts Repository scripts commands Command implementations agents Agent runtime and tooling size: XL labels Oct 19, 2026
@vincentkoc
vincentkoc force-pushed the fix/transcript-reader-imports-20261019 branch from 76ef73e to c180634 Compare October 19, 2026 10:27
@vincentkoc
vincentkoc marked this pull request as ready for review October 19, 2026 10:27
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 other 🚨 Merging this PR has meaningful risk outside the owned taxonomy. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Oct 19, 2026
@clawsweeper

clawsweeper Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: stale review; fresh review needed.

Summary
The latest durable ClawSweeper review was for head 480fc26a9e1159daef870c6be4a2c9f7dc4ac9c4, but the PR head is now c7f91fb698c40aa055e893762e2662b2a7e29c95. Its old verdict and PR readiness labels are no longer current.

Next step
Run or wait for a fresh ClawSweeper review on the current PR head.

Review history (1 earlier review cycle)
  • reviewed 2026-10-19T11:03:32.831Z sha 480fc26 :: blocked before merge. :: [P2] Update the target-deduplication spy to the new read owner

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling commands Command implementations gateway Gateway runtime P2 Normal backlog priority with limited blast radius. scripts Repository scripts size: XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant