Skip to content

Commit 4eb6aa3

Browse files
authored
refactor(gateway): carry authoritative read facts (#125145)
* refactor(gateway): carry authoritative read facts * fix(gateway): fence prepared device auth rotations * fix(ci): split oversized session types
1 parent e6616fd commit 4eb6aa3

21 files changed

Lines changed: 742 additions & 649 deletions

‎src/agents/mcp-oauth-store.ts‎

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// Canonical MCP OAuth session state. Legacy JSON import belongs to doctor only.
2-
import fs from "node:fs";
32
import type { DatabaseSync } from "node:sqlite";
43
import type { OAuthDiscoveryState } from "@modelcontextprotocol/sdk/client/auth.js";
54
import {
@@ -17,15 +16,14 @@ import {
1716
executeSqliteQueryTakeFirstSync,
1817
getNodeSqliteKysely,
1918
} from "../infra/kysely-sync.js";
20-
import { withOpenClawStateDatabaseReadOnly } from "../state/openclaw-state-db-readonly.js";
19+
import { withExistingOpenClawStateDatabaseReadOnly } from "../state/openclaw-state-db-readonly.js";
2120
import { ensureMcpOAuthPendingSchema } from "../state/openclaw-state-db-schema-additive.js";
2221
import { tableExists } from "../state/openclaw-state-db-schema-helpers.js";
2322
import type { DB as OpenClawStateKyselyDatabase } from "../state/openclaw-state-db.generated.js";
2423
import {
2524
openOpenClawStateDatabase,
2625
runOpenClawStateWriteTransaction,
2726
} from "../state/openclaw-state-db.js";
28-
import { resolveOpenClawStateSqlitePath } from "../state/openclaw-state-db.paths.js";
2927

3028
type McpOAuthDatabase = Pick<
3129
OpenClawStateKyselyDatabase,
@@ -245,37 +243,33 @@ export function readMcpOAuthStore(storeKey: string): McpOAuthStore {
245243

246244
/** Read status state without creating or repairing the shared database. */
247245
export function readMcpOAuthStoreReadOnly(storeKey: string): McpOAuthStore {
248-
const databasePath = resolveOpenClawStateSqlitePath();
249-
if (!fs.existsSync(databasePath)) {
250-
return {};
251-
}
252-
return withOpenClawStateDatabaseReadOnly(({ db }) => {
253-
if (!tableExists(db, "mcp_oauth_stores")) {
254-
return {};
255-
}
256-
return readFromDatabase(db, storeKey);
257-
});
246+
return (
247+
withExistingOpenClawStateDatabaseReadOnly(({ db }) => {
248+
if (!tableExists(db, "mcp_oauth_stores")) {
249+
return {};
250+
}
251+
return readFromDatabase(db, storeKey);
252+
}) ?? {}
253+
);
258254
}
259255

260256
/** List canonical store keys matching one server/principal prefix without creating state. */
261257
export function listMcpOAuthStoreKeysByPrefix(prefix: string): string[] {
262-
const databasePath = resolveOpenClawStateSqlitePath();
263-
if (!fs.existsSync(databasePath)) {
264-
return [];
265-
}
266-
return withOpenClawStateDatabaseReadOnly(({ db }) => {
267-
if (!tableExists(db, "mcp_oauth_stores")) {
268-
return [];
269-
}
270-
const rows = executeSqliteQuerySync(
271-
db,
272-
getNodeSqliteKysely<McpOAuthDatabase>(db)
273-
.selectFrom("mcp_oauth_stores")
274-
.select("store_key")
275-
.orderBy("store_key", "asc"),
276-
).rows;
277-
return rows.map((row) => row.store_key).filter((storeKey) => storeKey.startsWith(prefix));
278-
});
258+
return (
259+
withExistingOpenClawStateDatabaseReadOnly(({ db }) => {
260+
if (!tableExists(db, "mcp_oauth_stores")) {
261+
return [];
262+
}
263+
const rows = executeSqliteQuerySync(
264+
db,
265+
getNodeSqliteKysely<McpOAuthDatabase>(db)
266+
.selectFrom("mcp_oauth_stores")
267+
.select("store_key")
268+
.orderBy("store_key", "asc"),
269+
).rows;
270+
return rows.map((row) => row.store_key).filter((storeKey) => storeKey.startsWith(prefix));
271+
}) ?? []
272+
);
279273
}
280274

281275
function ensurePendingSchema(database: DatabaseSync): void {
@@ -315,11 +309,7 @@ const MCP_OAUTH_PENDING_STATE_TTL_MS = 10 * 60 * 1000;
315309
export function readMcpOAuthPendingAuthorization(state: string): string | undefined {
316310
// Public unauthenticated callback path: must stay read-only. Table creation
317311
// belongs to start-authorization; an unknown state must not write anything.
318-
const databasePath = resolveOpenClawStateSqlitePath();
319-
if (!fs.existsSync(databasePath)) {
320-
return undefined;
321-
}
322-
return withOpenClawStateDatabaseReadOnly(({ db }) => {
312+
return withExistingOpenClawStateDatabaseReadOnly(({ db }) => {
323313
if (!tableExists(db, "mcp_oauth_pending_authorizations")) {
324314
return undefined;
325315
}

‎src/agents/sandbox/registry.ts‎

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
*
44
* Tracks runtime and browser containers in the shared state DB.
55
*/
6-
import fsSync from "node:fs";
76
import type { Insertable, Selectable, Updateable } from "kysely";
87
import { executeSqliteQuerySync, getNodeSqliteKysely } from "../../infra/kysely-sync.js";
9-
import { withOpenClawStateDatabaseReadOnly } from "../../state/openclaw-state-db-readonly.js";
8+
import { withExistingOpenClawStateDatabaseReadOnly } from "../../state/openclaw-state-db-readonly.js";
109
import { tableExists } from "../../state/openclaw-state-db-schema-helpers.js";
1110
import type { DB as OpenClawStateKyselyDatabase } from "../../state/openclaw-state-db.generated.js";
1211
import { runOpenClawStateWriteTransaction } from "../../state/openclaw-state-db.js";
13-
import { resolveOpenClawStateSqlitePath } from "../../state/openclaw-state-db.paths.js";
1412
import type { SandboxContainerEngineTarget } from "./container-engine.js";
1513

1614
export type SandboxRegistryEntry = {
@@ -181,58 +179,56 @@ function readRegistryRows(
181179
kind: SandboxRegistryKind,
182180
filter?: { backendId: string; scopeKey: string },
183181
): SandboxRegistryRow[] {
184-
if (!fsSync.existsSync(resolveOpenClawStateSqlitePath(process.env))) {
185-
return [];
186-
}
187182
// CLI reads must not join the Gateway's writable SQLite lifecycle (#101290).
188-
return withOpenClawStateDatabaseReadOnly(({ db }) => {
189-
if (!tableExists(db, "sandbox_registry_entries")) {
190-
return [];
191-
}
192-
const stateDb = getSandboxRegistryKysely(db);
193-
let query = stateDb
194-
.selectFrom("sandbox_registry_entries")
195-
.selectAll()
196-
.where("registry_kind", "=", kind);
197-
if (filter) {
198-
query = query
199-
.where("session_key", "=", filter.scopeKey)
200-
.where("backend_id", "=", filter.backendId);
201-
}
202-
return executeSqliteQuerySync(
203-
db,
204-
filter
205-
? query.orderBy("last_used_at_ms", "desc").orderBy("container_name", "asc")
206-
: query.orderBy("container_name", "asc"),
207-
).rows;
208-
});
183+
return (
184+
withExistingOpenClawStateDatabaseReadOnly(({ db }) => {
185+
if (!tableExists(db, "sandbox_registry_entries")) {
186+
return [];
187+
}
188+
const stateDb = getSandboxRegistryKysely(db);
189+
let query = stateDb
190+
.selectFrom("sandbox_registry_entries")
191+
.selectAll()
192+
.where("registry_kind", "=", kind);
193+
if (filter) {
194+
query = query
195+
.where("session_key", "=", filter.scopeKey)
196+
.where("backend_id", "=", filter.backendId);
197+
}
198+
return executeSqliteQuerySync(
199+
db,
200+
filter
201+
? query.orderBy("last_used_at_ms", "desc").orderBy("container_name", "asc")
202+
: query.orderBy("container_name", "asc"),
203+
).rows;
204+
}) ?? []
205+
);
209206
}
210207

211208
function readRegistryRow(
212209
kind: SandboxRegistryKind,
213210
containerName: string,
214211
): SandboxRegistryRow | null {
215-
if (!fsSync.existsSync(resolveOpenClawStateSqlitePath(process.env))) {
216-
return null;
217-
}
218212
// CLI reads must not join the Gateway's writable SQLite lifecycle (#101290).
219-
return withOpenClawStateDatabaseReadOnly(({ db }) => {
220-
if (!tableExists(db, "sandbox_registry_entries")) {
221-
return null;
222-
}
223-
const stateDb = getSandboxRegistryKysely(db);
224-
return (
225-
executeSqliteQuerySync(
226-
db,
227-
stateDb
228-
.selectFrom("sandbox_registry_entries")
229-
.selectAll()
230-
.where("registry_kind", "=", kind)
231-
.where("container_name", "=", containerName)
232-
.limit(1),
233-
).rows[0] ?? null
234-
);
235-
});
213+
return (
214+
withExistingOpenClawStateDatabaseReadOnly(({ db }) => {
215+
if (!tableExists(db, "sandbox_registry_entries")) {
216+
return null;
217+
}
218+
const stateDb = getSandboxRegistryKysely(db);
219+
return (
220+
executeSqliteQuerySync(
221+
db,
222+
stateDb
223+
.selectFrom("sandbox_registry_entries")
224+
.selectAll()
225+
.where("registry_kind", "=", kind)
226+
.where("container_name", "=", containerName)
227+
.limit(1),
228+
).rows[0] ?? null
229+
);
230+
}) ?? null
231+
);
236232
}
237233

238234
function insertRegistryRowIfMissing(row: SandboxRegistryInsert): void {

0 commit comments

Comments
 (0)