Skip to content

fix(gateway): allow first database creation during session discovery - #199862

Merged
steipete merged 1 commit into
mainfrom
steipete/fix-2518-sqlite-identity
Nov 16, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/fix-2518-sqlite-identity

Conversation

@steipete

@steipete steipete commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes chat requests failing with SQLite database path identity changed when an agent's first database is created during session discovery.

User Impact

First-use session discovery can read the newly created database without rejecting its earlier absence. No configuration, schema, migration, or user action is required.

A separate Bun SESSION_MUTATION_AUTHORIZATION_CHANGED failure remains unresolved; this PR does not claim to repair it.

Why This Change Was Made

#197510 moved ordinary Gateway lookup to worker preparation that captures an absent-file identity before the existing worker waits for pending database creation. Legitimate first creation then invalidates that premature identity. Ordinary lookup now selects the existing captureIdentity: false mode, restoring its prior path-custody contract. The actual row reader still owns the opened file's generation; mutation, placement, cold-storage, and retained-plan checks remain intact.

Production code grows by one line to select that existing mode at the lookup boundary. One regression adds 38 test lines and forces real first creation between preparation and discovery.

Evidence

Synthetic owner measurements use a real allocated 35 GiB SQLite database, 5,000 sessions, and 62,000 workspace files totaling 1.116 GB. The existing qualification owner runs five counterbalanced samples per variant, 3,000 operations each, with separate A/A controls and sampled JS allocation. Candidate census stays equal; premature identity captures fall from one to zero per operation.

Per-operation median Before After
APFS wall / CPU 41.87 / 46.59 µs 21.18 / 25.08 µs
APFS sampled JS allocation 18,079 B 12,432 B
Linux btrfs wall / CPU 15.132 / 20.279 µs 8.150 / 12.786 µs
Linux btrfs sampled JS allocation 15,985 B 10,950 B

APFS A/A controls: wall 43.71/41.80 µs, CPU 49.19/46.21 µs, allocation 18,129/18,334 B. Linux A/A spreads: 1.94% wall, 11.94% CPU, 0.15% allocation. Linux uses a task-owned btrfs loop image on ext4, 8 CPUs/16 GiB RAM, Node 24.22.1. These are qualification-owner mode comparisons, not Gateway throughput or native-allocation claims. No production data was copied.

The original Bun xAI case fails before and after this fix with the separate authorization error. An error-path diagnostic traced it to a pending agent-database registry publication during retained authorization; the guard originates in #163030, but the regression's introducing commit and failing publication owner are still unknown. Later diagnostic passes do not establish a fix. All temporary instrumentation was removed. This unresolved local failure is retained as investigated evidence and is not an inherited-CI exception; exact-head CI remains required.

Existing before/after terminal evidence

These are excerpts from the already-completed canonical node scripts/run-vitest.mjs run invocations with explicit OPENCLAW_VITEST_RUNTIME=node or bun and --maxWorkers=1. Temporary synthetic paths are redacted. The committed head is byte-identical to the tested candidate.

Unchanged real-Gateway xAI case, Node, parent:

× |gateway-server-isolated| src/gateway/server.xai-fallback.test.ts > chat.send preserves xAI rejection priority and recovers statusless failures 45543ms
  → Error: SQLite database path identity changed: <synthetic-state>/agents/fallback/agent/openclaw-agent.sqlite
Tests  1 failed (1)

Same unchanged real-Gateway case, Node, candidate:

✓ |gateway-server-isolated| src/gateway/server.xai-fallback.test.ts > chat.send preserves xAI rejection priority and recovers statusless failures 42315ms
Tests  1 passed (1)

Deterministic first-creation regression, parent Node then candidate Node/Bun:

× |gateway-database-workers| src/gateway/session-utils-store-lookup.test.ts > global session lookup ownership > reads a first database created during target discovery 2101ms
  → SQLite database path identity changed: <synthetic-state>/agents/cold/agent/openclaw-agent.sqlite
✓ |gateway-database-workers| src/gateway/session-utils-store-lookup.test.ts > global session lookup ownership > reads a first database created during target discovery 1883ms
✓ |gateway-database-workers| src/gateway/session-utils-store-lookup.test.ts > global session lookup ownership > reads a first database created during target discovery 6555ms

The unchanged Node Gateway case validates the user flow; the deterministic owner regression establishes the specific first-creation interleaving. The original Bun authorization gap described above remains unresolved.

@clawsweeper

clawsweeper Bot commented Nov 16, 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 gateway Gateway runtime size: XS labels Nov 16, 2026
@steipete
steipete marked this pull request as ready for review November 16, 2026 00:42
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-16T00:44:53.027622Z df74012 Draft marked ready
ℹ️ 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. P1 High-priority user-facing bug, regression, or broken workflow. 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. labels Nov 16, 2026
@clawsweeper

clawsweeper Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR lets ordinary Gateway session discovery accept an agent database first created after candidate preparation.

Example: The database for agent cold is created while looking up agent:cold:first-session.

  • Before: Discovery rejects the newly created database with SQLite database path identity changed.
  • After: Discovery reads the created-during-discovery session while retaining path custody and reader authority checks.

Review scores

Measure Result What it means
Overall readiness 🧂 unranked krab (1/6) The focused patch is source-supported and has a meaningful regression, but the required shipped-entry-point evidence is not supplied in inspectable form.
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: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: A chat request can fail when legitimate first database creation occurs during session discovery.
Reason: The maintainer-authored direction restores working first-use lookup through existing owners; the separately reported Bun authorization failure remains explicitly outside scope.

Regression provenance

Possible regression — suspected (reviewed change). No predecessor PR is attributed.

Merge readiness

✅ Ready for maintainer review

This PR addresses a concrete current-main regression with a focused fix; no introduced correctness or security defect was found.

Priority: P1
Reviewed head: df74012e89d73fa194586ae6a9389905a8407657

Before merge

None.

Findings

None.

Tests

  • Missing end-to-end proof: Attach existing redacted Gateway logs or terminal output showing successful chat lookup when first database creation occurs during discovery; remove IPs, API keys, phone numbers, and non-public endpoints.
Agent review details

How this fits together

Gateway session lookup sends configured database candidates through the existing discovery worker, then obtains session rows and membership facts from retained database readers.

flowchart TD
  A[Gateway session request] --> B[Capture configured paths]
  B --> C[Prepare discovery candidates]
  C --> D[Wait for pending database creation]
  D --> E[Discover session stores]
  E --> F[Read retained session facts]
  F --> G[Check authority and consume]
Loading

Technical review

Best possible solution:

Keep asynchronous discovery and use its existing locator-only preparation mode, leaving opened-file generation and mutation authority with their current owners.

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

Source establishes the absent-identity capture, intervening creation wait, and exact rejection; the committed regression forces that sequence, but no target code was executed during this read-only review.

Is this the best way to solve the issue?

This is the smallest complete repair: extend an existing preparation mode to the inventory request and select it at the affected caller, without changing writer or inspection behavior.

AGENTS.md: found and applied where relevant.

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

Provenance checked

  • Gateway inventory identity capture changes intended behavior with a stated reason (2c4413a: Move necessary filesystem qualification into existing workers to reduce Gateway stalls while preserving physical custody and accepted-work settlement.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

PR surface

Source +1, Tests +38. Total +39 across 3 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 2 3 2 +1
Tests 1 38 0 +38
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 41 2 +39

Preflight source

Source unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified.

Review metrics

Metric Value Why it matters
Production versus regression growth Production net +1 line; tests +38 lines The production change reuses an existing mode, and the regression covers the distinct first-creation timing failure.

Labels

Label changes:

  • add P1: The current-main regression can reject chat session discovery when an agent database is first created.
  • 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:

  • P1: The current-main regression can reject chat session discovery when an agent database is first created.
  • 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.

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 November 15, 2026, 7:59 PM ET / November 16, 2026, 00:59 UTC.

@steipete
steipete merged commit c3e767d into main Nov 16, 2026
220 of 222 checks passed
@steipete
steipete deleted the steipete/fix-2518-sqlite-identity branch November 16, 2026 01:11
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

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

Labels

gateway Gateway runtime P1 High-priority user-facing bug, regression, or broken workflow. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: XS 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