Skip to content

perf(sessions): avoid redundant actor entry writes - #175191

Merged
steipete merged 1 commit into
mainfrom
dbasync/entrywrites2
Oct 17, 2026
Merged

steipete merged 1 commit into
mainfrom
dbasync/entrywrites2

Conversation

@steipete

@steipete steipete commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Session run adoption can rewrite the same node and window twice within one actor command, and the first unchanged patch after hydration can rewrite a node unnecessarily.

User Impact

No user-visible behavior or storage contract changes. The session actor performs fewer redundant SQLite writes while preserving its ordering, receipts, row facts, and synchronous SessionManager SDK contract.

Why This Change Was Made

The entry writer already compares persisted columns and skips unchanged nodes and windows. Hydration selected those columns but discarded several before publishing the actor's resident committed row, so its next comparison could falsely detect changes. Retain those existing scalar columns while stripping the aggregate actor projections. No new query or payload projection is introduced.

Defer no-turn run adoption to the existing reducer tail, just as delivery and completion already do, so lifecycle and activity changes persist together once. This removes the intermediate write path without adding another cache or command API. Request fusion remains separate work.

Removed redundant work and accepted edges:

  • Intermediate adoption persistence: observers receive the final committed adoption plus reducer state; the intermediate state is intentionally never published outside the command transaction.
  • Hydrated no-op node rewrite: unchanged persisted columns need no write; a genuinely stale derived column still triggers repair. Foreign live writers remain outside the Gateway's single-owner contract.

No security or lifecycle authority checks were removed. No schema, migration, dialect, SDK, update, or service-lifecycle changes.

Evidence

  • Codex autoreview round 1: no actionable P0–P2 findings.
  • 56 focused tests pass across actor entry residency, worker, memory, and completion suites. The new regression verifies adoption plus activity uses one node/window write, restart hydration preserves committed values, an unchanged hydrated row writes nothing, and a stale stored field still repairs.
  • T1 main-thread SQLite sites in the touched actor scope: 0 → 0. Worker/dialect ratchets pass in exact-head CI.
  • Production diff: +19 / −16 (net +3); test diff: +92 / −0. The small production increase retains existing selected facts and removes the partial resident-row construction.
  • Formatting, whitespace, and scoped type-aware lint pass. Exact-head CI passes production/test types, lint, source contracts, architecture/Madge, boundaries, database ratchets, build, and selected Node tests. PostgreSQL conformance and both required CI gate contexts pass (exact-head run). Completed ClawSweeper and Codex connector reviews report no findings. Knip reports 10 production and eight full-tree unused exports (scripts: zero), exactly matching scheduled main run 39006528379 at bbb775bd011b. Scanner/config/package and affected source blobs match the base and candidate; no candidate-only findings. Recorded with the main-failure owner.

SQL measurement — complete

Three alternating pairs in order M1,C1,M2,C2,M3,C3, with ten warm turns per run (30 per revision). Base 82d97d2fada961b765957fb4ff862e81816d7a6f; candidate 57ec0590b4989c046b56bbb37ca2b3ceedadcf70. Shared harness v3 was unchanged; Node/SQLite, compile-cache setting, source heads, runtime artifact bytes, workload completion, causal attribution, and process cleanup were validated for every run.

No standard-workload saving is demonstrated. All 60 warm turns have the same turn-attributed counts by turn position. The workload does not exercise the combined no-turn adoption/activity case or the unchanged hydrated patch covered by the regression. No latency claim on the loaded host.

Values are median [min–max]; SQL includes every thread. Raw windows also include background work.

Metric Base Candidate Δ median
Warm, turn-attributed: statements 62 [62–63] 62 [62–63] 0
Warm, turn-attributed: requests 18 [18–19] 18 [18–19] 0
Warm, turn-attributed: MAIN statements 0 [0–0] 0 [0–0] 0
Warm, raw window: statements 62 [62–96] 62 [62–85] 0
Warm, raw window: requests 18 [18–23] 18 [18–23] 0
Warm, raw window: MAIN statements 0 [0–13] 0 [0–9] 0
Whole workload: statements 3554 [3552–3593] 3552 [3545–3573] -2
Whole workload: requests 924 [920–926] 922 [920–926] -2
Whole workload: MAIN statements 150 [150–151] 150 [149–150] 0

Whole-workload ranges overlap; the −2 median statements/requests are background timing variation, not a claimed optimization. Raw warm/first-turn MAIN spikes are the same 13 startup-maintenance lease reads moving between phase windows; turn-caused MAIN remains zero. Touched-scope T1 stays 0 → 0, and the full inventory stays 182 → 182 sites in 88 files.

Subsystem attribution

Family Warm SQL, base → candidate Warm linked requests, base → candidate Whole SQL, base → candidate
session-nodes-upsert 5 [5–5] → 5 [5–5] 5 [5–5] → 5 [5–5] 102 [102–103] → 102 [102–102]
session-windows-upsert 2 [2–2] → 2 [2–2] 2 [2–2] → 2 [2–2] 55 [55–55] → 55 [55–55]
session-windows-update 4 [4–4] → 4 [4–4] 4 [4–4] → 4 [4–4] 88 [88–88] → 88 [88–90]
participant-projection 0 [0–0] → 0 [0–0] 0 [0–0] → 0 [0–0] 33 [33–33] → 33 [33–34]
exact-entry-facts 0 [0–0] → 0 [0–0] 0 [0–0] → 0 [0–0] 23 [23–23] → 23 [23–23]
actor-hydration 0 [0–0] → 0 [0–0] 0 [0–0] → 0 [0–0] 10 [10–10] → 10 [10–10]
entry-cache-snapshots 0 [0–0] → 0 [0–0] 0 [0–0] → 0 [0–0] 12 [11–12] → 11 [11–12]
row-facts 1 [1–1] → 1 [1–1] 1 [1–1] → 1 [1–1] 53 [53–53] → 53 [53–53]
watermarks 1 [1–1] → 1 [1–1] 1 [1–1] → 1 [1–1] 28 [28–28] → 28 [28–28]

Every warm subsystem median delta is 0 statements / 0 requests. Family requests are distinct linked wire IDs and overlap across families; they are not additive. Reads are classified from full native execution stacks, not the compact analyzer’s representative stack. No saving is attributed to the variable cache-snapshot count.

The earlier owner reproduction on b87b4590f54cf747472e8ebfef7cc40dcda3b627 observed 2 node + 2 window writes for adoption plus activity and 1 node write for an unchanged hydrated patch. The current base retains those exact decision paths (source verified). This candidate passes 1 node + 1 window, 0 + 0 after hydration, and 1 + 0 when a stale stored column genuinely needs repair. The failing reproduction was not rerun on the current base; it is explicitly earlier-base evidence.

Phase-specific raw counts

Each cell is median [min–max] across three runs. Phase windows include background timers; their shifts do not establish owner savings.

Phase Base SQL Candidate SQL Base requests Candidate requests Base MAIN Candidate MAIN
01.boot 336 [333–346] 335 [333–338] 91 [89–94] 91 [89–91] 63 [63–64] 63 [63–63]
01.connect 77 [63–260] 68 [56–276] 22 [19–31] 21 [19–35] 0 [0–0] 0 [0–0]
02.first-turn 489 [313–490] 512 [328–523] 96 [95–103] 97 [96–101] 0 [0–0] 13 [0–13]
04.second-session 508 [507–511] 508 [506–529] 134 [134–135] 134 [134–143] 0 [0–0] 0 [0–0]
05.control-plane 7 [7–8] 7 [7–8] 1 [1–1] 1 [1–1] 0 [0–0] 0 [0–0]
06.cron 277 [276–278] 276 [275–277] 73 [73–74] 73 [72–73] 0 [0–0] 0 [0–0]
07.compaction 154 [150–249] 154 [154–156] 28 [25–38] 28 [28–30] 0 [0–0] 0 [0–0]
07.idle-connected 142 [142–143] 142 [142–372] 50 [49–51] 50 [50–75] 0 [0–0] 0 [0–0]
07.idle-settle 239 [144–242] 237 [8–238] 27 [20–30] 27 [2–28] 0 [0–0] 0 [0–0]
08.shutdown 26 [26–26] 26 [26–26] 6 [6–6] 6 [6–6] 7 [7–7] 7 [7–7]
09.restart-boot 345 [343–356] 352 [341–353] 103 [102–107] 104 [101–107] 60 [60–60] 60 [59–60]
09.restart-connect 51 [43–57] 54 [40–57] 21 [20–25] 24 [19–26] 0 [0–0] 0 [0–0]
10.restart-first-turn 214 [209–244] 215 [199–218] 70 [69–70] 70 [66–71] 0 [0–0] 0 [0–0]
11.restart-shutdown 30 [26–30] 26 [26–26] 7 [6–7] 6 [6–6] 7 [7–7] 7 [7–7]

@clawsweeper

clawsweeper Bot commented Oct 17, 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.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 17, 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-17T05:51:57.288221Z 57ec059 PR opened
ℹ️ 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.

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 17, 2026
@clawsweeper

clawsweeper Bot commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR retains persisted row columns during actor hydration and combines run adoption with reducer persistence.

Example: Adopt run current-run with startedAt 2 and activity updatedAt 20.

  • Before: Adoption and activity can each write the session node and window.
  • After: The command persists their combined result once, preserving the same final entry.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) A focused, source-supported optimization with meaningful production-owner coverage; application-level savings have not yet been demonstrated.
Proof confidence 🦐 gold shrimp (3/6) The ordinary contributor proof gate does not apply to this maintainer-authored PR; supplied coverage exercises the production actor worker with SQLite, while application-level measurements remain explicitly pending.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Performance · Worth it: Yes · Fix scope: Complete
User problem: Session processing performs unnecessary SQLite writes during run adoption and after actor hydration.
Reason: The maintainer-authored direction removes concrete redundant work without another cache, public API, configuration option, or storage format.

Merge readiness

✅ Ready for maintainer review

Keep open: this maintainer-authored PR removes redundant writes that remain on main, and review found no introduced correctness or security defect.

Priority: P2
Reviewed head: 57ec0590b4989c046b56bbb37ca2b3ceedadcf70

Before merge

None.

Findings

None.

Tests

  • Missing end-to-end proof: The planned Gateway baseline/candidate write trace and an explicit pre-fix failure/post-fix pass for the new regression are not supplied.
Agent review details

How this fits together

The session actor accepts ordered lifecycle commands, hydrates committed session facts, and uses the existing storage writer to persist changes and publish receipts.

flowchart TD
  A[Session callers] --> B[Session actor]
  C[SQLite hydration] --> B
  B --> D[Lifecycle and reducers]
  D --> E[Existing entry writer]
  E --> F[SQLite transaction]
  F --> G[Committed receipt]
Loading

Technical review

Best possible solution:

Retain complete persisted comparison facts and perform one final entry write through the existing transaction owner.

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

Source establishes both redundant-write paths on fetched main: immediate adoption persistence before reducers, and incomplete hydrated columns supplied to exact node comparison; no target code was executed during this review.

Is this the best way to solve the issue?

The patch addresses both causes within existing owners, preserves lifecycle checks and final admission, and leaves genuinely stale persisted columns eligible for repair.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 6485b1906e73.

Provenance checked

  • Hydrated persisted entry rows keeps the original intent (d5173cc: The original actor retained committed transaction facts for later phase cutovers; this patch preserves that ownership while retaining columns needed by the existing comparison.)
  • No-turn adoption persistence in shared phase dispatch changes intended behavior with a stated reason (5bd4f88: The shared phase extraction preserved immediate lifecycle persistence across durable and memory backends; this PR explicitly replaces the intermediate write with final combined persistence inside the same command.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

  • Introduced changes: The pinned introduction diff changes only hydration row retention, deferred no-turn adoption persistence, and its regression coverage; unrelated base movement is excluded. (src/config/sessions/session-actor-phase.ts:234, 57ec0590b498)
  • Current main still performs the intermediate write: Fetched main calls lifecycle without deferral for no-turn adoption, while reducers can subsequently write the entry again. (src/config/sessions/session-actor-phase.ts:234, 6485b1906e73)
  • Existing comparison owner: The entry writer compares generated node columns with retained persisted columns; hydration previously omitted columns such as label, causing false changes. Retaining their actual values also preserves repair when stored columns differ. (src/config/sessions/session-accessor.sqlite-entry-store.ts:533, 57ec0590b498)
  • Production-owner regression coverage: The added test uses the real actor worker and SQLite, checks one combined node/window write, rehydrates committed state, checks zero unchanged writes, and verifies stale-label repair. (src/config/sessions/session-actor-entry-residency.test.ts:44, 57ec0590b498)
  • Accepted transaction tradeoff: The maintainer-authored body explicitly accepts publishing only final adoption-plus-reducer state; inspection confirms the work remains synchronous within the existing transaction and final admission boundary. (src/config/sessions/session-actor.worker.ts:509, 57ec0590b498)
  • Exact-head validation: Changed-test lanes, production types, lint, architecture checks, and security checks passed on the reviewed head; PostgreSQL conformance remained in progress, so CI was not fully complete. (57ec0590b498)

Likely related people:

  • Peter Steinberger: Raw commit 5bd4f88 adds src/config/sessions/session-actor-phase.ts:212 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 5bd4f88535c6; files: src/config/sessions/session-actor-phase.ts)

PR surface

Source +3, Tests +92. Total +95 across 3 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 2 19 16 +3
Tests 1 92 0 +92
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 111 16 +95

Review metrics

Metric Value Why it matters
Production versus test changes Production +19/-16; tests +92/-0 The small production increase replaces partial row construction with retained persisted facts; regression coverage checks write reduction, rehydration, and repair.

Labels

Label changes:

  • add P2: This is a focused internal performance improvement with limited blast radius and no established user-visible outage.
  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp 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: This is a focused internal performance improvement with limited blast radius and no established user-visible outage.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Rank-up moves

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

  • Provide the planned baseline/candidate Gateway trace showing reduced actor writes and unchanged final session facts.

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 17, 2026, 2:04 AM ET / 06:04 UTC.

@steipete
steipete merged commit 852875e into main Oct 17, 2026
142 checks passed
@steipete
steipete deleted the dbasync/entrywrites2 branch October 17, 2026 06:21
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 18, 2026
Retain the already-selected persisted node columns in the session actor's committed row so its existing comparison skips unchanged writes after hydration. Strip aggregate actor projections from that retained row.

Defer no-turn run-adoption persistence to the existing reducer tail, folding lifecycle and activity into one node/window write while preserving actor ordering, write receipts and synchronous SessionManager behavior.

Validated with 56 focused actor tests, scoped type-aware lint and a clean Codex review. No schema or public API changes.

Release-note context: reduce redundant session bookkeeping writes during run adoption and after actor hydration.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. 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