Skip to content

fix(sessions): stop decoding the SQLite session row four times per entry patch - #136371

Closed
LiuwqGit wants to merge 1 commit into
openclaw:mainfrom
LiuwqGit:feat/issue-136338-sqlite-entry-decode-once
Closed

LiuwqGit wants to merge 1 commit into
openclaw:mainfrom
LiuwqGit:feat/issue-136338-sqlite-entry-decode-once

Conversation

@LiuwqGit

@LiuwqGit LiuwqGit commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Closes #136338

What Problem This Solves

Resolves a problem where gateways running the SQLite session store lose roughly a third of saturated throughput compared to 2026.7.1-2: every session-entry patch decodes (JSON.parse + participants projection) the same session row four times and deep-compares two decoded copies via JSON.stringify, all synchronously on the gateway event loop. A normal streamed reply turn issues about eleven such patches, so with a production-shaped ~17 KB entry_json the session store consumed ~29% of a saturated gateway main thread.

Why This Change Was Made

The patch path now decodes the session row exactly once at preparation and revalidates cheaply at the commit edge: instead of re-hydrating the row and JSON.stringify-comparing decoded entries, the transaction compares a raw persisted-row fingerprint (session_key, current_session_id, entry_json, updated_at) and keeps the same SqliteSessionMutationConflictError rejection contract when another writer changed the source row. writeSessionEntry reuses the commit-validated previous entry via an explicit opt-in (trustedCanonicalPreviousEntry) instead of re-reading the row, and post-write identity publication reuses the exact row the transaction just persisted instead of a fourth hydrated read. No schema or session-semantics changes; other write paths (lifecycle, recovery, transcript writes) are untouched.

User Impact

Session-entry patches stop paying four full row decodes plus two decoded-entry stringifications per patch, restoring gateway main-thread capacity for multi-agent deployments. Behavior is unchanged: commit-edge conflict detection, creation-stamp preservation, and identity mutation events work exactly as before.

Evidence

Regression test first (RED) — the prepared row was decoded 3 times during one successful patch (preparation, in-transaction revalidation, writeSessionEntry canonical re-read; the 4th decode reads the post-write row):

FAIL session accessor seam > decodes the SQLite session row once across a successful entry patch
AssertionError: expected [ …(3) ] to have a length of 1 but got 3

After the fix, the same test asserts exactly one decode of the prepared row, and the issue's conformance contract ("rejects a prepared SQLite entry patch when its source row changes") still passes:

Test Files  2 passed (2)   # session-accessor.test.ts + session-accessor.conformance.test.ts
     Tests  175 passed (175)

Stability: session-accessor.conformance.test.ts re-run 3 consecutive times after the final refactor, 47/47 each run. validate.sh (oxfmt + oxlint + same-directory tests + pnpm tsgo:core) passes on all changed files. CI lanes (test-projects --changed, full check:test-types) are pending on Actions.

Real environment tested: Node 26.7.0 on the repository worktree (Linux), vitest unit/conformance suites against a real node:sqlite database — not the 16-agent production gateway profile from the issue.

Exact steps or commands run after this patch:

node scripts/run-vitest.mjs run src/config/sessions/session-accessor.test.ts \
  src/config/sessions/session-accessor.conformance.test.ts
skills/fix-pr/scripts/validate.sh src/config/sessions/session-accessor.sqlite-entry-equality.ts \
  src/config/sessions/session-accessor.sqlite-entry-fingerprint.ts \
  src/config/sessions/session-accessor.sqlite-entry-store.ts \
  src/config/sessions/session-accessor.sqlite-entry.ts \
  src/config/sessions/session-accessor.test.ts

Evidence after fix: 175/175 tests pass across both suites; the decode-once regression test asserts JSON.parse of the persisted entry_json happens exactly once per successful patch; 3 consecutive conformance re-runs (47/47) confirm the commit-edge conflict contract is stable.

Observed result after fix: one decode at preparation, cheap raw-column fingerprint comparison at the commit edge, no row re-read inside writeSessionEntry, no post-write identity re-read — while a concurrent replaceSessionEntrySync between preparation and commit still rejects with SqliteSessionMutationConflictError.

What was not tested: the issue's full 16-concurrent-agent gateway throughput/profile scenario (requires containerized gateway images and a mock provider); only source-level decode counts and behavioral conformance were measured.

…try patch

Every session-entry patch hydrated the same session row four times
(preparation, in-transaction revalidation, writeSessionEntry canonical
re-read, and post-write identity snapshot) and deep-compared two decoded
copies via JSON.stringify, so ~11 patches per streamed turn burned roughly
a third of a saturated gateway's main thread.

Decode once at preparation and revalidate cheaply at the commit edge by
comparing the raw persisted row fingerprint (session_key,
current_session_id, entry_json, updated_at), preserving the
SqliteSessionMutationConflictError contract. The write reuses the
commit-validated previous entry instead of re-reading it, and identity
publication reuses the row the transaction just persisted.

Fixes openclaw#136338
@clawsweeper

clawsweeper Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

@LiuwqGit

LiuwqGit commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

Closing as a duplicate of #136361, the issue reporter's own fix PR (the issue body announced "Fix PR follows"). Same defect owner and same goal: decode the session row once at preparation and revalidate cheaply at the commit edge. Deferring to the earlier, author-owned PR.

@LiuwqGit LiuwqGit closed this Sep 2, 2026
@LiuwqGit
LiuwqGit deleted the feat/issue-136338-sqlite-entry-decode-once branch September 2, 2026 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: every SQLite session-entry patch decodes the session row four times, ~11 patches per turn cost a third of gateway throughput vs 2026.7.1-2

1 participant