Skip to content

Commit 2f2f062

Browse files
committed
fix(sessions): keep runtime reads and repairs on SQLite owners
Resolve physical session databases once and carry that identity through startup, search, boards, maintenance, and Doctor. Remove automatic legacy JSON import from Gateway startup while preserving explicit Doctor upgrades. Publish repaired legacy transcripts with strict atomic replacement and preserve the source when backup, write, or rename fails.
1 parent 5c67c71 commit 2f2f062

66 files changed

Lines changed: 2316 additions & 2048 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎config/assertion-safety-baseline.txt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3018,7 +3018,6 @@ src/gateway/server-startup-config-helpers.ts 1
30183018
src/gateway/server-startup-config.ts 1
30193019
src/gateway/server-startup-finish.ts 1
30203020
src/gateway/server-startup-post-attach.ts 1
3021-
src/gateway/server-startup-session-migration.ts 1
30223021
src/gateway/server-worker-placement-startup.ts 2
30233022
src/gateway/server.auth.control-ui.bootstrap-lifecycle.suite.ts 8
30243023
src/gateway/server.auth.control-ui.device-token.suite.ts 2

‎docs/cli/doctor.md‎

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Doctor has five postures:
3434
| Repair | `openclaw doctor --fix` | Applies supported repairs, using prompts unless non-interactive repair is safe. |
3535
| Lint | `openclaw doctor --lint [--json]` | Read-only findings with threshold-based exit codes for CI gates. |
3636
| Shared SQLite maintenance | `openclaw doctor --state-sqlite compact` | Explicitly checkpoints, compacts, and verifies the canonical shared state DB. |
37-
| Session SQLite migration | `openclaw doctor --session-sqlite <mode>` | Inspects, imports, validates, compacts, recovers, or restores session state. |
37+
| Session SQLite tools | `openclaw doctor --session-sqlite <mode>` | Inspects or maintains SQLite sessions and explicitly imports legacy history. |
3838

3939
Use `openclaw doctor --json` when an operator or script wants the advisory Doctor report as JSON. It exits successfully after producing a report; inspect `ok` and `findings` for health state. Use explicit `openclaw doctor --lint --json` when CI should exit nonzero for findings at the selected severity threshold. Prefer `--fix` when a human operator wants Doctor to edit config or state.
4040

@@ -96,8 +96,8 @@ openclaw channels status --probe
9696
| `--lint` | Run modernized health checks in read-only mode and emit diagnostic findings. |
9797
| `--post-upgrade` | Run post-upgrade plugin compatibility probes; findings go to stdout; exit code 1 if any error-level finding is present. |
9898
| `--state-sqlite <mode>` | Run explicit shared state SQLite maintenance. The only mode is `compact`. |
99-
| `--session-sqlite <mode>` | Run the targeted session SQLite migration mode: `inspect`, `dry-run`, `import`, `validate`, `compact`, `recover`, or `restore`. |
100-
| `--session-sqlite-store <path>` | With `--session-sqlite`: select one legacy `sessions.json` store path. |
99+
| `--session-sqlite <mode>` | Run targeted session SQLite maintenance or legacy import: `inspect`, `dry-run`, `import`, `validate`, `compact`, `recover`, or `restore`. |
100+
| `--session-sqlite-store <path>` | With `--session-sqlite`: select a SQLite database or legacy `sessions.json` source, subject to the mode's selection rules below. |
101101
| `--session-sqlite-agent <id>` | With `--session-sqlite`: select one configured agent. |
102102
| `--session-sqlite-all-agents` | With `--session-sqlite`: select configured and discovered agent stores. |
103103
| `--github-issue` | With `--session-sqlite recover`: prepare a sanitized openclaw/openclaw issue report; doctor creates it with `gh` after `--yes` or interactive confirmation. |
@@ -289,16 +289,19 @@ compatible backup or upgrade OpenClaw for a newer schema.
289289

290290
## Session SQLite migration
291291

292-
OpenClaw imports legacy session rows and transcript history into each agent's
293-
SQLite database automatically during gateway startup and during
294-
`openclaw doctor --fix`. `openclaw doctor --session-sqlite <mode>` is the
295-
targeted inspection and validation tool for that migration. Current runtime
296-
session rows live in
297-
`~/.openclaw/agents/<agentId>/agent/openclaw-agent.sqlite`. Legacy
298-
`sessions.json` files are migration sources. Hot transcript JSONL files are
299-
imported and archived out of the active sessions directory after successful
300-
import; archive-tier JSONL files remain support artifacts, not runtime
301-
fallbacks.
292+
Runtime session rows and transcripts live in SQLite, by default at
293+
`~/.openclaw/agents/<agentId>/agent/openclaw-agent.sqlite`. Gateway and local
294+
CLI startup do not import, restore, or rewrite legacy session JSON/JSONL files.
295+
When startup finds a legacy session store, it refuses readiness and prints a
296+
`doctor --fix` command for the active profile instead of serving empty history.
297+
298+
To upgrade history from an older file-backed installation, stop the Gateway,
299+
back up its state, and run `openclaw doctor --fix` before restarting it.
300+
`openclaw doctor --session-sqlite <mode>` provides targeted inspection,
301+
import, validation, and SQLite maintenance. Legacy `sessions.json` files are
302+
migration sources. Hot transcript JSONL files are imported and archived after
303+
successful import; archive-tier JSONL files remain support artifacts, not
304+
runtime fallbacks.
302305

303306
The regular `openclaw doctor` pass also reports canonical SQLite transcripts
304307
whose initial session header was never persisted. `openclaw doctor --fix`
@@ -311,7 +314,7 @@ Modes:
311314

312315
| Mode | Behavior |
313316
| ---------- | ---------------------------------------------------------------------------------------------------------------------- |
314-
| `inspect` | Read legacy and SQLite counts, plus unreferenced JSONL files, without importing. |
317+
| `inspect` | Read SQLite counts and any selected legacy-source diagnostics without importing; legacy files are not required. |
315318
| `dry-run` | Parse legacy entries and transcript JSONL files, count importable rows, and report issues without writing SQLite rows. |
316319
| `import` | Import legacy entries and transcript events into SQLite for the selected targets. |
317320
| `validate` | Compare the selected legacy sources against SQLite rows and transcript event counts. |
@@ -321,34 +324,44 @@ Modes:
321324

322325
Selectors:
323326

324-
- Default: the configured default agent store, when that legacy store file exists.
327+
- Default: the configured default agent store; SQLite inspection does not require a legacy file.
325328
- `--session-sqlite-agent <id>`: one configured agent.
326329
- `--session-sqlite-all-agents`: configured agent stores plus discovered agent stores.
327-
- `--session-sqlite-store <path>`: one explicit legacy `sessions.json` path.
330+
- `--session-sqlite-store <path>`: one explicit `.sqlite` database or legacy `sessions.json` path.
331+
332+
`dry-run`, `import`, and `validate` select existing legacy sources only. An
333+
explicit `.sqlite` path selects no legacy targets in those modes; it is never
334+
parsed or archived as JSON. Use `inspect`, `compact`, or corruption recovery
335+
with `recover` for a SQLite target. Recovering or restoring archived sources
336+
from migration manifests requires the original legacy selector or agent-store
337+
discovery that includes it. Legacy `sessions.json` selector paths remain
338+
supported and resolve to their corresponding SQLite stores for maintenance.
328339

329-
Manual inspection sequence:
340+
With the Gateway stopped and its state backed up, inspect and import legacy
341+
history:
330342

331343
```bash
332344
openclaw doctor --session-sqlite inspect --session-sqlite-all-agents
333345
openclaw doctor --session-sqlite dry-run --session-sqlite-all-agents --json
334346
openclaw doctor --session-sqlite import --session-sqlite-all-agents
335-
openclaw doctor --session-sqlite validate --session-sqlite-all-agents --json
336-
openclaw doctor --session-sqlite compact --session-sqlite-all-agents
337-
openclaw doctor --session-sqlite recover --github-issue
347+
openclaw doctor --session-sqlite inspect --session-sqlite-all-agents --json
338348
```
339349

340-
Back up the OpenClaw state directory before running `import` on an install with
341-
important history. `validate` exits non-zero when a selected legacy entry is
342-
missing from SQLite, a session id differs, or a transcript event count differs.
350+
`import` validates rows and transcript event counts before archiving its
351+
legacy sources. After a successful import, `validate` may select no legacy
352+
targets; use `inspect` to see the current SQLite state. While legacy sources
353+
remain, `validate` exits non-zero when a selected entry is missing from SQLite,
354+
a session id differs, or a transcript event count differs.
343355
When using `--session-sqlite-store <path>`, check that the report contains the
344-
expected target count; a nonexistent explicit store path selects no targets.
356+
expected target count; a nonexistent legacy source selects no targets for
357+
`dry-run`, `import`, or `validate`.
345358

346359
SQLite deletes reclaim pages inside the database first; they do not necessarily
347360
shrink the database file immediately. After deleting or archiving large
348361
transcripts, run `openclaw doctor --session-sqlite compact --session-sqlite-all-agents`
349362
to checkpoint WAL files, run `VACUUM`, and report before/after database and WAL
350-
sizes. Compaction requires a regular file with the current agent schema, the
351-
selected agent's durable owner metadata, and no open handle in the doctor
363+
sizes. Compaction requires a regular file with the current agent schema, its
364+
durable database owner metadata, and no open handle in the doctor
352365
process. The destructive `import`, `compact`, `recover`, and `restore` modes
353366
hold the same state ownership lock as Gateway startup for their full operation;
354367
`inspect`, `dry-run`, and `validate` remain read-only and do not take it. Stop
@@ -362,8 +375,8 @@ checks cover SQLite WAL, shared-memory, and rollback-journal sidecars.
362375

363376
Each import writes a manifest under
364377
`~/.openclaw/session-sqlite-migration-runs/` before moving transcript artifacts
365-
into the archive. If startup reports a failed session SQLite migration after
366-
artifacts moved, run recovery:
378+
into the archive. If an explicit import fails after artifacts moved, keep the
379+
Gateway stopped and run recovery:
367380

368381
```bash
369382
openclaw doctor --session-sqlite recover --github-issue
@@ -373,13 +386,16 @@ Recovery selects the latest failed migration manifest, restores only the
373386
manifest's archived artifacts, validates the affected targets, refreshes the
374387
sanitized `.failure.md` and `.failure.json` reports, and prepares a GitHub issue
375388
body that avoids transcript contents, raw environment, secrets, and unbounded
376-
config. When no failed migration manifest exists but a selected agent SQLite
377-
database is corrupt, not a database, or has journal sidecars without a main
378-
database, recovery copies the complete file set to a temporary inspection
379-
directory. SQLite can roll back a valid hot journal in that disposable copy
389+
config. When no failed migration manifest exists, recovery inspects selected
390+
SQLite databases using temporary copies of their complete file sets. SQLite
391+
can roll back a valid hot journal in that disposable copy
380392
before `quick_check`, `integrity_check`, and `foreign_key_check` run, while the
381-
original forensic files remain untouched. Failed integrity checks or orphaned
382-
sidecars preserve the DB, WAL, SHM, and rollback-journal files by renaming the
393+
original forensic files remain untouched during inspection. Recovery attempts
394+
to repair canonical index corruption in place after schema and owner validation.
395+
Schema, owner, and I/O errors, as well as failed or refused index repairs,
396+
leave the original database in place with a diagnostic. Other confirmed
397+
corruption or orphaned sidecars
398+
preserve the DB, WAL, SHM, and rollback-journal files by renaming the
383399
whole discovered set with one `.corrupt-<timestamp>` suffix. A caught rename
384400
failure rolls already-moved files back before reporting failure, so a
385401
recoverable file set is not silently split. Stop the Gateway before recovery;

‎docs/concepts/session.md‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ Opt into automatic resets globally, then override them per chat type or channel:
184184

185185
## Where state lives
186186

187-
- **Runtime session rows:** `~/.openclaw/agents/<agentId>/agent/openclaw-agent.sqlite`
187+
- **Runtime session rows and transcripts:** `~/.openclaw/agents/<agentId>/agent/openclaw-agent.sqlite` by default
188188
- **Archived transcript files:** `~/.openclaw/agents/<agentId>/sessions/`
189189
- **Legacy row migration source:** `~/.openclaw/agents/<agentId>/sessions/sessions.json`
190190

@@ -196,15 +196,18 @@ timestamps:
196196
- `updatedAt`: last store-row mutation; useful for listing and pruning, but not
197197
authoritative for daily/idle reset freshness.
198198

199-
During migration from older installs, gateway startup and `openclaw doctor
200-
--fix` import legacy `sessions.json` rows and hot transcript JSONL history into
201-
SQLite automatically. Rows without `sessionStartedAt` are resolved from the
199+
To import legacy `sessions.json` rows and hot transcript JSONL history from an
200+
older installation, stop the Gateway, back up its state, and run
201+
`openclaw doctor --fix` before restarting it. Gateway and local CLI startup use
202+
SQLite without importing, restoring, or rewriting legacy session files.
203+
If startup finds a legacy store, it refuses readiness and prints the Doctor
204+
command for the active profile instead of silently starting with empty history.
205+
During Doctor import, rows without `sessionStartedAt` are resolved from the
202206
legacy transcript JSONL session header when available. If an older row also
203207
lacks `lastInteractionAt`, idle freshness falls back to that session start time,
204208
not to later bookkeeping writes. Use `openclaw doctor --session-sqlite inspect
205209
--session-sqlite-all-agents` and the [Doctor migration
206-
sequence](/cli/doctor#session-sqlite-migration) when you want explicit
207-
inspection or validation evidence.
210+
sequence](/cli/doctor#session-sqlite-migration) for inspection and validation.
208211

209212
## Session maintenance
210213

0 commit comments

Comments
 (0)