Skip to content

Commit 0a8e360

Browse files
jalehmansteipete
andauthored
refactor: flip sessions and transcripts to sqlite storage (#98236)
* refactor(sessions): migrate runtime storage to sqlite * test(sessions): fix sqlite CI regressions * test(sessions): align remaining sqlite fixtures * fix(codex): require sqlite trajectory recorder * test(sessions): align orphan recovery sqlite fixture * test(sessions): align sqlite rebase fixtures * fix(sessions): finish current-main integration of the sqlite flip Resolve the whole-store SDK removal across its owner boundary: drop the loadSessionStore re-export and the registry whole-store wrappers, wire hasTrackedActiveSessionRun into gateway chat, complete the preserveLockedHarnessIds cleanup contract, flip the codex thread-history import to storePath targets, and port remaining main-side tests from file-store helpers to session accessor reads. * chore: drop committed pebbles log, revert plugin-inspector bump, refresh generated docs Remove the 1.8k-line .pebbles/events.jsonl work log from the branch, restore the plugin-inspector advisory lane to main's pinned 0.3.10 so the supply-chain bump gets its own review, and regenerate docs_map, the plugin SDK API baseline, and the export-surface ratchet for the merged tree. * feat(sessions): keep archived transcripts by default with zstd cold storage Codex-style retention: deleting or resetting a session archives its transcript as a zstd-compressed JSONL artifact (plain when the runtime lacks node:zlib zstd) and keeps it until the disk budget evicts oldest first. resetArchiveRetention now governs both deleted and reset archives and defaults to keep; maxDiskBytes defaults to 2gb so retention stays bounded, with archives evicted before live sessions. The cron reaper follows the same knob instead of deleting archives on its own timer. * fix(state): converge agent DB migration lineages and bound database growth Merge coherence: run both structure-gated legacy memory-schema repairs (flip-lineage drop, main-lineage identity rebuild) before the flip migration so pre-flip v1/v2 and pre-merge flip v1/v4 databases all converge, and hoist foreign_keys=OFF outside the schema transaction where the pragma was silently ignored and the v1 sessions rebuild cascade-deleted session_entries. Growth guards: fresh agent DBs enable auto_vacuum=INCREMENTAL, WAL maintenance releases freed pages in bounded passes (never a blocking full VACUUM), and doctor reports state/agent DB bloat from freelist stats. * fix(codex): resolve the store path for thread-history import via the SDK The supervision catalog passed the legacy sessionFile locator to the storePath-targeted transcript mirror; resolve the agent store path with the session-store SDK helper instead of a runtime-object seam so test fakes and headless callers need no extra surface. Drop the obsolete missing-session-id preprocessing case: sessions rows are NOT NULL on session_id and upsert repairs id-less patches at write time. * fix(sessions): fail safe on malformed disk-budget config and doctor stat errors A malformed explicit maxDiskBytes disables the budget instead of falling back to the destructive 2gb default the user never chose, and the doctor bloat check skips databases whose paths stat-fail instead of aborting doctor. * fix(sessions): complete sqlite conflict translations * test(sqlite): align hardening checks with maintenance * test(sessions): inspect compressed transcript archives * fix(tests): await session seeds and drop unused helpers flagged by CI lint The five unawaited writeSessionStoreSeed calls raced their SQLite seeds against the assertions, failing compact shards; the bloat probe drops a useless initializer and the merged tests drop now-unused helpers. * test(sessions): type legacy proof events directly * test(sessions): align hardening contracts * perf(sessions): read usage transcript sizes from SQL aggregates Usage/cost scans walked every session and materialized every transcript event just to re-stringify it for a byte estimate — the #86718 stall class reborn on the DB. readTranscriptStatsSync sums stored JSON bytes in SQLite without loading a single row. * fix(sessions): re-root foreign-root transcript paths onto the current sessions dir Restored backups, moved OPENCLAW_STATE_DIR, and rehearsal copies carry absolute sessionFile paths from the old root; the containment fallback kept those foreign paths, so migration read (and would archive) files in the original root and reported local copies missing. Re-root the canonical agents/<id>/sessions suffix onto the current dir when the file exists there; genuine cross-root layouts still fall through unchanged. * test(agents): seed harness admission through sqlite * fix(sqlite): close agent db on pragma setup failure * fix(doctor): compact and retrofit incremental auto-vacuum after session import The migration is the sanctioned offline window: post-import compact reclaims import churn and applies auto_vacuum=INCREMENTAL to databases created before the fresh-DB pragma existed, so runtime maintenance can release pages in bounded passes on every install. --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent ca09fca commit 0a8e360

612 files changed

Lines changed: 49700 additions & 20307 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.

‎.github/workflows/ci.yml‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,6 +1580,12 @@ jobs:
15801580
- check_name: check-session-transcript-reader-boundary
15811581
group: session-transcript-reader-boundary
15821582
runner: blacksmith-4vcpu-ubuntu-2404
1583+
- check_name: check-sqlite-session-schema-baseline
1584+
group: sqlite-session-schema-baseline
1585+
runner: blacksmith-4vcpu-ubuntu-2404
1586+
- check_name: check-sqlite-session-flip-proof
1587+
group: sqlite-session-flip-proof
1588+
runner: blacksmith-8vcpu-ubuntu-2404
15831589
- check_name: check-additional-extension-channels
15841590
group: extension-channels
15851591
runner: blacksmith-8vcpu-ubuntu-2404
@@ -1695,6 +1701,13 @@ jobs:
16951701
else
16961702
run_check "lint:tmp:session-accessor-boundary" pnpm run lint:tmp:session-accessor-boundary
16971703
fi
1704+
if [ ! -f scripts/check-sqlite-transaction-boundary.mjs ]; then
1705+
echo "[skip] SQLite transaction boundary check is not present in this checkout"
1706+
elif ! node -e 'const pkg = require("./package.json"); process.exit(pkg.scripts?.["lint:tmp:sqlite-transaction-boundary"] ? 0 : 1);'; then
1707+
echo "[skip] SQLite transaction boundary script is not present in package.json"
1708+
else
1709+
run_check "lint:tmp:sqlite-transaction-boundary" pnpm run lint:tmp:sqlite-transaction-boundary
1710+
fi
16981711
;;
16991712
session-transcript-reader-boundary)
17001713
if [ ! -f scripts/check-session-transcript-reader-boundary.mjs ]; then
@@ -1705,6 +1718,20 @@ jobs:
17051718
run_check "lint:tmp:session-transcript-reader-boundary" pnpm run lint:tmp:session-transcript-reader-boundary
17061719
fi
17071720
;;
1721+
sqlite-session-schema-baseline)
1722+
if ! node -e 'const pkg = require("./package.json"); process.exit(pkg.scripts?.["sqlite:sessions-schema:check"] ? 0 : 1);'; then
1723+
echo "[skip] SQLite sessions/transcripts schema baseline script is not present in package.json"
1724+
else
1725+
run_check "sqlite:sessions-schema:check" pnpm run sqlite:sessions-schema:check
1726+
fi
1727+
;;
1728+
sqlite-session-flip-proof)
1729+
if [ ! -f test/scripts/sqlite-sessions-transcripts-flip-proof.e2e.test.ts ]; then
1730+
echo "[skip] SQLite sessions/transcripts flip proof is not present in this checkout"
1731+
else
1732+
run_check "sqlite sessions/transcripts flip proof" node scripts/run-vitest.mjs run --config test/vitest/vitest.e2e.config.ts test/scripts/sqlite-sessions-transcripts-flip-proof.e2e.test.ts
1733+
fi
1734+
;;
17081735
extension-channels)
17091736
run_check "lint:extensions:channels" pnpm run lint:extensions:channels
17101737
;;

‎.github/workflows/workflow-sanity.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,5 +212,8 @@ jobs:
212212
- name: Check plugin SDK API baseline drift
213213
run: pnpm plugin-sdk:api:check
214214

215+
- name: Check SQLite sessions/transcripts schema baseline drift
216+
run: pnpm sqlite:sessions-schema:check
217+
215218
- name: Check plugin SDK surface budget
216219
run: pnpm plugin-sdk:surface:check

‎AGENTS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ Skills own workflows; root owns hard policy and routing.
7676
- State/storage migrations are database-first. Runtime reads/writes the canonical store only. Old file stores, sidecars, aliases, and fallback readers belong in `openclaw doctor --fix` migration code only, never steady-state runtime.
7777
- Storage default: SQLite only. Do not add JSON/JSONL/TXT/sidecar files for OpenClaw-owned runtime state, caches, queues, registries, indexes, cursors, checkpoints, or plugin scratch data.
7878
- SQLite runtime access uses Kysely helpers, not raw SQL statement strings, except schema DDL, migrations, low-level DB bootstrap, or narrowly justified SQLite primitives.
79+
- SQLite write transactions are synchronous commit sections only. Finish async planning, filesystem access, plugin hooks, and predicates before `BEGIN`; then reread and validate authoritative rows before writing. Never return a Promise or execute `await` from a transaction callback.
7980
- Use the shared state DB (`state/openclaw.sqlite`) for global runtime state and plugin KV data. Use the per-agent DB (`agents/<agentId>/agent/openclaw-agent.sqlite`) for agent-scoped state/cache. Use a dedicated SQLite DB only when schema, volume, or lifecycle clearly does not fit those stores.
8081
- Legacy state/cache files are migration debt. When touching code that reads/writes them, prefer moving the data into SQLite or calling out the refactor follow-up; do not add parallel file paths.
8182
- File storage must be a named product artifact: import/export, user attachment, log, backup, or external tool contract. If it is app state or cache, it belongs in SQLite.

‎docs/.generated/README.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
# Generated Docs Artifacts
22

3-
SHA-256 hash files are the tracked drift-detection artifacts. The full JSON
4-
baselines are generated locally (gitignored) for inspection only.
3+
SHA-256 hash files are the tracked drift-detection artifacts. Full baselines are
4+
generated locally for inspection only.
55

66
**Tracked (committed to git):**
77

88
- `config-baseline.sha256` — hashes of config baseline JSON artifacts.
99
- `plugin-sdk-api-baseline.sha256` — hashes of Plugin SDK API baseline artifacts.
10+
- `sqlite-session-transcript-schema-baseline.sha256` — hash of the sessions/transcripts SQLite schema baseline.
1011

1112
**Local only (gitignored):**
1213

1314
- `config-baseline.json`, `config-baseline.core.json`, `config-baseline.channel.json`, `config-baseline.plugin.json`
1415
- `plugin-sdk-api-baseline.json`, `plugin-sdk-api-baseline.jsonl`
16+
- `.artifacts/sqlite-session-transcript-schema-baseline.sql`
1517

1618
Do not edit any of these files by hand.
1719

1820
- Regenerate config baseline: `pnpm config:docs:gen`
1921
- Validate config baseline: `pnpm config:docs:check`
2022
- Regenerate Plugin SDK API baseline: `pnpm plugin-sdk:api:gen`
2123
- Validate Plugin SDK API baseline: `pnpm plugin-sdk:api:check`
24+
- Regenerate SQLite sessions/transcripts schema baseline: `pnpm sqlite:sessions-schema:gen`
25+
- Validate SQLite sessions/transcripts schema baseline: `pnpm sqlite:sessions-schema:check`
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
6f7e0f6f2e6d5b107500b62f9556abdc5e450eb80a7a8586c1baf7d82894ed2e plugin-sdk-api-baseline.json
2-
a3d7d4f1a3aff973cb59bac966f884a812cbc633f90a8d9cd793209192916e5c plugin-sdk-api-baseline.jsonl
1+
07c747212af5b8a228a7ff23101a2fa4096f148600d1d2e0fc83ac8f151cb58b plugin-sdk-api-baseline.json
2+
85868eedeb4a99e714468f7f8c81d070227f8ed7e247939a4ac0d458ad04426d plugin-sdk-api-baseline.jsonl
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4bb5f50a60c3664656d56f2be8d9884d48287f084c03959120b2825991b23f21 sqlite-session-transcript-schema-baseline.sql

‎docs/.i18n/glossary.zh-CN.json‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,5 +1458,13 @@
14581458
{
14591459
"source": "Crabbox plugin",
14601460
"target": "Crabbox 插件"
1461+
},
1462+
{
1463+
"source": "Path 3 SQLite session artifact family",
1464+
"target": "Path 3 SQLite 会话工件族"
1465+
},
1466+
{
1467+
"source": "Path 3 live SQLite E2E harness",
1468+
"target": "Path 3 live SQLite E2E harness"
14611469
}
14621470
]

‎docs/channels/channel-routing.md‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,24 @@ Example:
134134

135135
## Session storage
136136

137-
Session stores live under the state directory (default `~/.openclaw`):
137+
Runtime session rows live in each agent's SQLite database under the state
138+
directory (default `~/.openclaw`):
138139

139-
- `~/.openclaw/agents/<agentId>/sessions/sessions.json`
140-
- JSONL transcripts live alongside the store
140+
- `~/.openclaw/agents/<agentId>/agent/openclaw-agent.sqlite`
141141

142-
You can override the store path via `session.store` and `{agentId}` templating.
142+
Older installs may have legacy transcript JSONL files and a `sessions.json` row
143+
store under `~/.openclaw/agents/<agentId>/sessions/`. Gateway startup and
144+
`openclaw doctor --fix` import hot legacy rows/history into SQLite
145+
automatically. Use `openclaw doctor --session-sqlite inspect
146+
--session-sqlite-all-agents` and the
147+
[Doctor](/cli/doctor#session-sqlite-migration) validation sequence when you need
148+
explicit migration evidence.
149+
You can still select a legacy store path via `session.store` and `{agentId}`
150+
templating for migration and offline-maintenance workflows.
143151

144152
Gateway and ACP session discovery also scans disk-backed agent stores under the
145153
default `agents/` root and under templated `session.store` roots. Discovered
146-
stores must stay inside that resolved agent root and use a regular
154+
stores must stay inside that resolved agent root and use a regular legacy
147155
`sessions.json` file. Symlinks and out-of-root paths are ignored.
148156

149157
## WebChat behavior

‎docs/channels/group-messages.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Only owner numbers (from `channels.whatsapp.allowFrom`, or the bot's own E.164 w
8787

8888
- Heartbeats run in the agent's main session; group sessions never get heartbeat runs.
8989
- Echo suppression remembers the combined prompt (history + current message) per session so the bot's own delivered messages do not retrigger it; an identical repeated batch can be skipped as an echo.
90-
- Session store entries appear as `agent:<agentId>:whatsapp:group:<jid>` in the session store (`~/.openclaw/agents/<agentId>/sessions/sessions.json` by default); a missing entry just means the group has not triggered a run yet.
90+
- Session store entries appear as `agent:<agentId>:whatsapp:group:<jid>` in the per-agent SQLite session store; a missing entry just means the group has not triggered a run yet.
9191
- Typing indicators follow `session.typingMode` / `agents.defaults.typingMode`. When visible replies are opted into message-tool-only mode, typing starts immediately by default so group members can see the agent working even if no automatic final reply is posted. Explicit typing-mode config still wins.
9292

9393
## Related

0 commit comments

Comments
 (0)