Skip to content

Commit 1886d77

Browse files
fix(media): fail closed on non-throwing table-missing session stores
1 parent 332eb20 commit 1886d77

3 files changed

Lines changed: 47 additions & 7 deletions

File tree

‎src/config/sessions/session-accessor.sqlite-entry.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ export function listSqliteSessionEntryKeysReadOnly(
199199
/** Exact persisted-key probe on the read-only handle, for per-row hot paths. */
200200
export function loadExactSqliteSessionEntryReadOnly(
201201
scope: SessionAccessScope,
202+
options?: { strictStoreAvailable?: boolean },
202203
): ExactSessionEntry | undefined {
203204
const sessionKey = scope.sessionKey.trim();
204205
if (!sessionKey) {
@@ -209,7 +210,17 @@ export function loadExactSqliteSessionEntryReadOnly(
209210
(database) => readExactSessionEntryRowValidated(database, sessionKey)?.entry,
210211
toDatabaseOptions(resolved),
211212
);
212-
return result.found && result.value
213+
if (!result.found) {
214+
if (options?.strictStoreAvailable === true && result.reason !== "database-missing") {
215+
// Fail-closed callers (destructive cleanup) must observe a present-but-
216+
// unusable store (missing schema/tables) instead of an absent entry that
217+
// looks like "no sessions". database-missing stays an absence: discovery
218+
// legitimately probes candidate paths that may not exist yet.
219+
throw new Error(`session store is unavailable (${result.reason}) for ${sessionKey}`);
220+
}
221+
return undefined;
222+
}
223+
return result.value
213224
? {
214225
sessionKey,
215226
entry: scope.clone === false ? result.value : cloneSessionEntry(result.value),

‎src/gateway/managed-image-attachments.unavailable-store.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import path from "node:path";
88
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
99
import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js";
1010
import { writeSessionEntry } from "../config/sessions/session-accessor.sqlite-entry-store.js";
11+
import { requireNodeSqlite } from "../infra/node-sqlite.js";
1112
import {
1213
closeOpenClawAgentDatabasesForTest,
1314
openOpenClawAgentDatabase,
@@ -143,6 +144,28 @@ describe("cleanupManagedOutgoingMediaRecords with a real SQLite session store",
143144
await expect(fs.access(fixture.originalPath)).resolves.toBeUndefined();
144145
});
145146
});
147+
148+
it("retains a record when the session store is missing its session tables", async () => {
149+
await withEnvAsync({ ...process.env, OPENCLAW_STATE_DIR: stateDir }, async () => {
150+
const fixture = await createManagedMediaFixture(
151+
stateDir,
152+
"55555555-5555-4555-8555-555555555555",
153+
);
154+
const agentSqlitePath = await createRealSessionStore(stateDir);
155+
// A store that opens but lost its session tables reports a non-throwing
156+
// table-missing result on ordinary reads; strict cleanup must still
157+
// treat it as unknown retention state, not as "no sessions".
158+
const { DatabaseSync } = requireNodeSqlite();
159+
const database = new DatabaseSync(agentSqlitePath);
160+
database.exec("DROP TABLE session_nodes");
161+
database.close();
162+
163+
const result = await cleanupManagedOutgoingMediaRecords({ stateDir });
164+
165+
expect(result).toEqual({ deletedRecordCount: 0, deletedFileCount: 0, retainedCount: 1 });
166+
await expect(fs.access(fixture.originalPath)).resolves.toBeUndefined();
167+
});
168+
});
146169
});
147170

148171
async function createDuplicateCanonicalKeyStore(stateDir: string): Promise<void> {

‎src/gateway/session-utils-store-lookup.ts‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,18 @@ function loadGatewaySessionLookupStoreUncached(
186186
if (options.exactKeys) {
187187
const store: Record<string, SessionEntry> = {};
188188
for (const sessionKey of options.exactKeys) {
189-
const match = loadExactSessionEntryReadOnly({
190-
...(agentId ? { agentId } : {}),
191-
clone: false,
192-
sessionKey,
193-
storePath,
194-
});
189+
// Strict readers must see a present-but-unusable store (missing schema
190+
// or tables surface as a typed non-throwing result) as a failure, not
191+
// as an absent entry.
192+
const match = loadExactSessionEntryReadOnly(
193+
{
194+
...(agentId ? { agentId } : {}),
195+
clone: false,
196+
sessionKey,
197+
storePath,
198+
},
199+
options.strict ? { strictStoreAvailable: true } : undefined,
200+
);
195201
if (match) {
196202
store[match.sessionKey] = match.entry;
197203
}

0 commit comments

Comments
 (0)