Skip to content

fix(database): make unbounded retention portable to PostgreSQL - #183044

Merged
steipete merged 1 commit into
mainfrom
dbasync/ownerredesign3
Oct 25, 2026
Merged

steipete merged 1 commit into
mainfrom
dbasync/ownerredesign3

Conversation

@steipete

@steipete steipete commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

PostgreSQL rejects three retention queries with SQLSTATE 2201W (LIMIT must not be negative). ACP replay session retention, channel-ingress capacity retention, and resolved session-suggestion pruning all used SQLite's LIMIT -1 spelling. ACP byte trimming also used SQLite's scalar max form.

What Changed

One database-engine helper supplies the unbounded limit expression: -1 on SQLite and ALL on PostgreSQL. The three owners keep their existing OFFSET, ordering, predicates, and batching. ACP uses a Kysely CASE expression for the same zero-clamped subtraction.

No schema, retention policy, transaction, cache, identity, or worker-request change. ACP byte-budget optimization is deliberately separate and will follow in another PR.

Evidence

The synthetic real-owner tests pass 7/7 on SQLite and fail 7/7 on PostgreSQL before the fix, all with SQLSTATE 2201W. With this repair, 14/14 pass on SQLite and real PostgreSQL 17. They exercise:

  • ACP partial-session trimming with accurate and understated byte totals, plus the exact 200-session cap under tied timestamps.
  • Pending, completed, and failed ingress rows, including protected rows retaining their original capacity slots, 500-row batches, repeat pruning, and zero-cap tails.
  • Resolved-suggestion tied ordering while retaining pending suggestions and another session's rows.

Changed-file type-aware lint and git diff --check pass. The independent Codex P0–P2 review is scoped-clean, and the committed tree matches the reviewed tree. PostgreSQL maintainer review is approved for this exact head. Exact-head CI passed, including all three PostgreSQL conformance shards, production/test types, type-aware lint, source contracts, and baseline ratchets.

The local T1 and dialect ratchets, format, mock exports, and other completed contract checks passed. Local typecheck setup rejected the campaign-required external dependency store; CI supplies the passing typecheck and lint evidence. The duplicate local full-core lint was stopped after CI passed. All three local Knip comparisons exactly match the original base: production 72/72, full-tree 86/86, scripts 1,136/1,136, with zero added or removed findings.

Class Audit

The shared helper repairs all three unguarded negative-LIMIT sites: src/acp/event-ledger.worker.ts, src/channels/message/ingress-queue.kernel.ts, and src/config/sessions/session-suggestion-store.kernel.ts (the latter used eb.lit(-1)). The remaining negative sentinel in session-accessor.transcript-range.worker.ts is explicitly inside the SQLite-only !postgres recursive branch; PostgreSQL has its own outer limit. All 13 other OFFSET chains use nonnegative limits.

Open-PR Compatibility

Base-only versus base-plus-candidate merge-tree comparison across 80 overlapping open PRs adds no conflicting paths or conflict markers. Existing conflicts are not counted as new ones.

Compared PRs

#183114, #183069, #182971, #182951, #182329, #182216, #182104, #180832, #180026, #178768, #178124, #178033, #177356, #177351, #175345, #175029, #174317, #174235, #173557, #173026, #170160, #169723, #169504, #169438, #169241, #169081, #168522, #168434, #167221, #167090, #166166, #165710, #165583, #164935, #164843, #164721, #164551, #163835, #163655, #163239, #162989, #162583, #162565, #162530, #161997, #161410, #160908, #160851, #159478, #159326, #159255, #158502, #158244, #157693, #157557, #157427, #156634, #155630, #154904, #153907, #153856, #151648, #150078, #149882, #149241, #148533, #146847, #146326, #145839, #141239, #141004, #140609, #139620, #136474, #129388, #126224, #124337, #121204, #107366, #93952

Portability head: d0ecc5533d4b1f56fedf9db9d030eaad42870ac8. All seven files are byte-identical to the reviewed and tested prerequisite. The three optimization-only proof changes have moved out of this PR.

@clawsweeper

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

@openclaw-barnacle openclaw-barnacle Bot added scripts Repository scripts size: M labels Oct 25, 2026
@steipete
steipete marked this pull request as ready for review October 25, 2026 00:47
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 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-25T01:54:27.579533Z d0ecc55 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.

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. clawsweeper:intake:feature Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. 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-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. clawsweeper:intake:feature Preliminary submission classification; full source review pending. labels Oct 25, 2026
@clawsweeper

clawsweeper Bot commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR reuses ordered sessions and deletion byte totals during ACP replay eviction.

Example: Sessions a, b, and c each contain 130 events with 64 KiB text and tied timestamps.

  • Before: The reported baseline performs 11 budget reads, removes a, retains b sequences 65–130 as incomplete, and preserves c.
  • After: The candidate targets two budget reads with identical retained history; that production-flow result remains unmeasured.

Review scores

Measure Result What it means
Overall readiness 🧂 unranked krab (1/6) The patch is focused and source-consistent, but the performance acceptance evidence lacks the candidate production-flow result.
Proof confidence 🧂 unranked krab (1/6) Real behavior proof is not required for maintainer- or bot-authored pull requests.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Performance · Worth it: Yes · Fix scope: Complete
User problem: ACP replay retention performs avoidable database work when retained history exceeds its byte budget.
Reason: The measured baseline identifies concrete redundant reads, and the maintainer-authored direction preserves existing replay behavior within the current owner.

Merge readiness

✅ Ready for maintainer review

Keep this maintainer-authored PR open: it removes identifiable repeated work that remains on current main, and no definite introduced defect was found.

Priority: P2
Reviewed head: e554c369a88a37963a6ce0d02e1b95a9e4e3e79e

Before merge

None.

Findings

None.

Tests

  • Missing end-to-end proof: The candidate needs the submitted real ACP stdio/Gateway workload's after-fix counts and retained-state comparison; the body reports the native regression red on baseline and green on candidate.
Agent review details

How this fits together

The ACP bridge sends session starts and updates to the shared-state worker, which persists bounded replay history and returns complete replay or an incomplete result that selects transcript fallback.

flowchart LR
  A[ACP stdio client] --> B[Bridge translator]
  B --> C[Replay ledger facade]
  C --> D[Shared state worker]
  D --> E[Transactional retention]
  E --> F[Replay tables]
  F --> G[Replay or transcript fallback]
Loading

Technical review

Best possible solution:

Keep the owner-local optimization and land it once its performance acceptance rule and required database checks are satisfied.

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

Yes: current-main source repeats both reads per eviction batch, and the submitted baseline reports 11 budget reads through real ACP stdio and an isolated Gateway; this review ran no target code.

Is this the best way to solve the issue?

Carrying the existing deletion totals and bounded session order within their owning transaction is the smallest complete optimization, preserving the stored clamp without introducing a cache or another state owner.

AGENTS.md: found and applied where relevant.

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

Provenance checked

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 841272e adds src/acp/event-ledger.worker.ts:355 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 841272e230d0; files: src/acp/event-ledger.worker.ts)
  • coaiMax: Raw commit c4822b8 adds src/acp/event-ledger-bytes.ts:23 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: medium; commits: c4822b82d55c; files: src/acp/event-ledger-bytes.ts)

PR surface

Source +20, Tests +157, Other +188. Total +365 across 6 files. Added test files: 1.

View PR surface stats
Area Files Added Removed Net
Source 1 35 15 +20
Tests 3 157 0 +157
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 2 188 0 +188
Total 6 380 15 +365

Labels

Label changes:

  • add P2: This is a bounded performance improvement to over-budget ACP replay writes, with no demonstrated urgent user-facing regression.
  • add rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab 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 bounded performance improvement to over-budget ACP replay writes, with no demonstrated urgent user-facing regression.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab 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.

  • Complete the registered baseline/candidate production-flow comparison.
  • Resolve attribution and disposition of the failing required PostgreSQL conformance check.

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 24, 2026, 9:17 PM ET / October 25, 2026, 01:17 UTC.

@steipete
steipete force-pushed the dbasync/ownerredesign3 branch from e554c36 to afc4115 Compare October 25, 2026 01:50
@steipete
steipete force-pushed the dbasync/ownerredesign3 branch from 8749adb to afc4115 Compare October 25, 2026 01:52
Use the engine-owned unbounded row limit for ACP, channel ingress, and resolved suggestions. Preserve offset ordering, protected slots, and batch behavior; express ACP byte clamping with CASE. Real SQLite and PostgreSQL regressions cover all three owners.
@steipete
steipete force-pushed the dbasync/ownerredesign3 branch from afc4115 to d0ecc55 Compare October 25, 2026 01:52
@steipete steipete changed the title perf(acp): reduce repeated replay eviction reads fix(database): make unbounded retention portable to PostgreSQL Oct 25, 2026
@steipete
steipete merged commit f64a730 into main Oct 25, 2026
156 of 160 checks passed
@steipete
steipete deleted the dbasync/ownerredesign3 branch October 25, 2026 02:31
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 25, 2026
…law#183044)

Fix PostgreSQL retention failures in ACP replay, channel ingress, and resolved session suggestions by using the database engine-owned unbounded LIMIT expression. Preserve each owner's ordering, offsets, protected rows, and batching. Express ACP byte-accounting subtraction with the same portable zero clamp.

Synthetic owner regressions reproduce seven PostgreSQL failures before the repair and pass all fourteen SQLite/PostgreSQL cases with the fix. No schema, policy, transaction, or worker-request change.
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: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. 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