Skip to content

Commit b9d68dd

Browse files
roboclaw-botVACInc
andcommitted
fix(doctor): cover supported SQLite planner stats
Use bounded explicit analysis so Doctor refreshes every indexed table on supported pre-3.46 SQLite runtimes. Strengthen the regression around named planner statistics and adopt automatic temp cleanup. Co-authored-by: VACInc <[email protected]>
1 parent d03c888 commit b9d68dd

2 files changed

Lines changed: 41 additions & 27 deletions

File tree

‎src/infra/state-migrations.media-persistence.test.ts‎

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from "node:fs";
22
import path from "node:path";
33
import { afterEach, describe, expect, it } from "vitest";
4-
import { cleanupTempDirs, makeTempDir } from "../../test/helpers/temp-dir.js";
4+
import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js";
55
import {
66
encodeSessionArchiveContent,
77
readSessionArchiveContentSync,
@@ -20,7 +20,7 @@ import { requireNodeSqlite } from "./node-sqlite.js";
2020
import { migrateLegacyMediaPersistence } from "./state-migrations.media-persistence.js";
2121
import { readDatabaseSnapshot } from "./state-migrations.media-persistence.test-support.js";
2222

23-
const tempDirs: string[] = [];
23+
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
2424
const PREVIOUS_VERSION = 16;
2525

2626
type FixtureEvent = Record<string, unknown>;
@@ -134,12 +134,11 @@ function writeArchive(filePath: string, events: FixtureEvent[], compressed: bool
134134
afterEach(() => {
135135
closeOpenClawAgentDatabasesForTest();
136136
closeOpenClawStateDatabaseForTest();
137-
cleanupTempDirs(tempDirs);
138137
});
139138

140139
describe("legacy media persistence doctor migration", () => {
141140
it("rewrites every active shape and trajectory snapshot, migrates mixed archives, and reruns as a no-op", async () => {
142-
const stateDir = makeTempDir(tempDirs, "media-persistence-migration-");
141+
const stateDir = tempDirs.make("media-persistence-migration-");
143142
const env = { OPENCLAW_STATE_DIR: stateDir };
144143
const legacy = createEvent({
145144
id: "event-legacy",
@@ -355,7 +354,7 @@ describe("legacy media persistence doctor migration", () => {
355354
});
356355

357356
it("migrates when valid transcript created_at rows have an unsafe aggregate", async () => {
358-
const stateDir = makeTempDir(tempDirs, "media-persistence-large-created-at-");
357+
const stateDir = tempDirs.make("media-persistence-large-created-at-");
359358
const env = { OPENCLAW_STATE_DIR: stateDir };
360359
const legacyMediaPaths = ["/media/a.png", "/media/b.png"];
361360
const databasePath = createLegacyDatabaseFixture({
@@ -398,7 +397,7 @@ describe("legacy media persistence doctor migration", () => {
398397
});
399398

400399
it("upgrades the existing v14 structural schema before the media cutover", async () => {
401-
const stateDir = makeTempDir(tempDirs, "media-persistence-v14-");
400+
const stateDir = tempDirs.make("media-persistence-v14-");
402401
const env = { OPENCLAW_STATE_DIR: stateDir };
403402
const databasePath = createLegacyDatabaseFixture({
404403
env,
@@ -437,9 +436,13 @@ describe("legacy media persistence doctor migration", () => {
437436
.get(),
438437
).toEqual({ name: "session_suggestions" });
439438
expect(
440-
(after.prepare("SELECT COUNT(*) AS count FROM sqlite_stat1").get() as { count: number })
441-
.count,
442-
).toBeGreaterThan(0);
439+
after
440+
.prepare("SELECT tbl, idx FROM sqlite_stat1 WHERE idx = ?")
441+
.get("idx_agent_transcript_event_sequence"),
442+
).toEqual({
443+
tbl: "transcript_event_identities",
444+
idx: "idx_agent_transcript_event_sequence",
445+
});
443446
const row = after
444447
.prepare("SELECT event_json FROM transcript_events WHERE session_id = ? AND seq = 0")
445448
.get("legacy") as { event_json: string };
@@ -454,7 +457,7 @@ describe("legacy media persistence doctor migration", () => {
454457
});
455458

456459
it("refreshes planner statistics for an already-current agent database", async () => {
457-
const stateDir = makeTempDir(tempDirs, "media-persistence-current-stats-");
460+
const stateDir = tempDirs.make("media-persistence-current-stats-");
458461
const env = { OPENCLAW_STATE_DIR: stateDir };
459462
const databasePath = createLegacyDatabaseFixture({
460463
env,
@@ -470,22 +473,33 @@ describe("legacy media persistence doctor migration", () => {
470473
],
471474
},
472475
});
476+
const { DatabaseSync } = requireNodeSqlite();
477+
const before = new DatabaseSync(databasePath);
478+
before
479+
.prepare(
480+
"INSERT INTO session_suggestions (id, session_key, author_id, text, created_at, state) VALUES (?, ?, ?, ?, ?, ?)",
481+
)
482+
.run("suggestion-current", "agent:main:current", "operator", "keep me", 1000, "pending");
483+
before.close();
473484

474485
expect(await migrateLegacyMediaPersistence({ env })).toEqual({ changes: [], warnings: [] });
475-
const { DatabaseSync } = requireNodeSqlite();
476486
const after = new DatabaseSync(databasePath, { readOnly: true });
477487
try {
478488
expect(
479-
(after.prepare("SELECT COUNT(*) AS count FROM sqlite_stat1").get() as { count: number })
480-
.count,
481-
).toBeGreaterThan(0);
489+
after
490+
.prepare("SELECT tbl, idx FROM sqlite_stat1 WHERE idx = ?")
491+
.get("idx_agent_session_suggestions_session_state_created"),
492+
).toEqual({
493+
tbl: "session_suggestions",
494+
idx: "idx_agent_session_suggestions_session_state_created",
495+
});
482496
} finally {
483497
after.close();
484498
}
485499
});
486500

487501
it("upgrades an owned v0 database through the media prerequisite schema", async () => {
488-
const stateDir = makeTempDir(tempDirs, "media-persistence-v0-");
502+
const stateDir = tempDirs.make("media-persistence-v0-");
489503
const env = { OPENCLAW_STATE_DIR: stateDir };
490504
const databasePath = createLegacyDatabaseFixture({
491505
env,
@@ -517,7 +531,7 @@ describe("legacy media persistence doctor migration", () => {
517531
});
518532

519533
it("migrates complete PR-1 facts beside a compact legacy projection", async () => {
520-
const stateDir = makeTempDir(tempDirs, "media-persistence-dual-write-");
534+
const stateDir = tempDirs.make("media-persistence-dual-write-");
521535
const env = { OPENCLAW_STATE_DIR: stateDir };
522536
const databasePath = createLegacyDatabaseFixture({
523537
env,
@@ -567,7 +581,7 @@ describe("legacy media persistence doctor migration", () => {
567581
});
568582

569583
it("repairs a missing canonical v15 index before the media cutover", async () => {
570-
const stateDir = makeTempDir(tempDirs, "media-persistence-v15-index-");
584+
const stateDir = tempDirs.make("media-persistence-v15-index-");
571585
const env = { OPENCLAW_STATE_DIR: stateDir };
572586
const databasePath = createLegacyDatabaseFixture({
573587
env,
@@ -609,7 +623,7 @@ describe("legacy media persistence doctor migration", () => {
609623
it.each([PREVIOUS_VERSION, OPENCLAW_AGENT_SCHEMA_VERSION])(
610624
"canonicalizes retired media carriers on every transcript message role at schema v%s",
611625
async (schemaVersion) => {
612-
const stateDir = makeTempDir(tempDirs, "media-persistence-message-roles-");
626+
const stateDir = tempDirs.make("media-persistence-message-roles-");
613627
const env = { OPENCLAW_STATE_DIR: stateDir };
614628
const databasePath = createLegacyDatabaseFixture({
615629
env,
@@ -664,7 +678,7 @@ describe("legacy media persistence doctor migration", () => {
664678
);
665679

666680
it("canonicalizes legacy trajectory metadata onto existing facts", async () => {
667-
const stateDir = makeTempDir(tempDirs, "media-persistence-trajectory-metadata-");
681+
const stateDir = tempDirs.make("media-persistence-trajectory-metadata-");
668682
const env = { OPENCLAW_STATE_DIR: stateDir };
669683
const databasePath = createLegacyDatabaseFixture({
670684
env,
@@ -741,7 +755,7 @@ describe("legacy media persistence doctor migration", () => {
741755
});
742756

743757
it("preserves duplicate physical transcript rows during canonicalization", async () => {
744-
const stateDir = makeTempDir(tempDirs, "media-persistence-duplicates-");
758+
const stateDir = tempDirs.make("media-persistence-duplicates-");
745759
const env = { OPENCLAW_STATE_DIR: stateDir };
746760
const event = createEvent({
747761
id: "duplicate-event",
@@ -777,7 +791,7 @@ describe("legacy media persistence doctor migration", () => {
777791
});
778792

779793
it("aborts one database on invalid JSON without advancing its version", async () => {
780-
const stateDir = makeTempDir(tempDirs, "media-persistence-corrupt-");
794+
const stateDir = tempDirs.make("media-persistence-corrupt-");
781795
const env = { OPENCLAW_STATE_DIR: stateDir };
782796
const databasePath = createLegacyDatabaseFixture({
783797
env,
@@ -819,7 +833,7 @@ describe("legacy media persistence doctor migration", () => {
819833
});
820834

821835
it("aborts one database on invalid trajectory JSON without advancing its version", async () => {
822-
const stateDir = makeTempDir(tempDirs, "media-persistence-corrupt-trajectory-");
836+
const stateDir = tempDirs.make("media-persistence-corrupt-trajectory-");
823837
const env = { OPENCLAW_STATE_DIR: stateDir };
824838
const databasePath = createLegacyDatabaseFixture({
825839
env,
@@ -854,7 +868,7 @@ describe("legacy media persistence doctor migration", () => {
854868
});
855869

856870
it("aborts on active-row drift and archive source replacement without partial deletion", async () => {
857-
const stateDir = makeTempDir(tempDirs, "media-persistence-drift-");
871+
const stateDir = tempDirs.make("media-persistence-drift-");
858872
const env = { OPENCLAW_STATE_DIR: stateDir };
859873
const { DatabaseSync } = requireNodeSqlite();
860874
const event = createEvent({
@@ -948,7 +962,7 @@ describe("legacy media persistence doctor migration", () => {
948962
});
949963

950964
it("rejects ambiguous sparse arrays and ignores stale interrupted temp files", async () => {
951-
const stateDir = makeTempDir(tempDirs, "media-persistence-sparse-");
965+
const stateDir = tempDirs.make("media-persistence-sparse-");
952966
const env = { OPENCLAW_STATE_DIR: stateDir };
953967
createLegacyDatabaseFixture({ env, eventsBySession: {} });
954968
const archiveDir = path.join(stateDir, "agents", "main", "sessions");

‎src/infra/state-migrations.media-persistence.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,9 @@ function createMigrationDatabaseHandle(
357357
}
358358

359359
function refreshAgentDatabasePlannerStatistics(database: DatabaseSync): void {
360-
// This short-lived Doctor connection has no query history. The 0x10000 bit checks
361-
// every table; analysis_limit also bounds work on supported pre-3.46 SQLite builds.
362-
database.exec("PRAGMA analysis_limit=1000; PRAGMA optimize=0x10002;");
360+
// Doctor owns a stopped-writer maintenance window here. Explicitly analyze every
361+
// table because the supported pre-3.46 SQLite floor lacks optimize's all-table bit.
362+
database.exec("PRAGMA analysis_limit=1000; ANALYZE main;");
363363
}
364364

365365
function migrateAgentDatabase(params: {

0 commit comments

Comments
 (0)