Skip to content

Commit 16801bf

Browse files
morlutosteipete
andauthored
fix(agents): prevent subagent registry resurrection (#105793)
* fix(agents): retire legacy subagent registry JSON Co-authored-by: morluto <[email protected]> * fix(agents): keep migration decision internal Co-authored-by: morluto <[email protected]> * style(agents): format migration decision type Co-authored-by: morluto <[email protected]> --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 4a1eedb commit 16801bf

24 files changed

Lines changed: 1098 additions & 757 deletions

‎config/max-lines-baseline.txt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,6 @@ src/agents/subagent-control.ts
553553
src/agents/subagent-registry-lifecycle.test.ts
554554
src/agents/subagent-registry-lifecycle.ts
555555
src/agents/subagent-registry-run-manager.ts
556-
src/agents/subagent-registry.persistence.test.ts
557556
src/agents/subagent-registry.steer-restart.test.ts
558557
src/agents/subagent-registry.test.ts
559558
src/agents/subagent-registry.ts

‎docs/refactor/database-first.md‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,11 @@ The branch already has a real shared SQLite base:
397397
tables are rebuilt under their canonical names.
398398
- Subagent run recovery state now lives in typed shared `subagent_runs` rows
399399
with indexed child, requester, and controller session keys. The old
400-
`subagents/runs.json` file is doctor migration input only.
400+
`subagents/runs.json` file is Doctor cleanup input only. Its run entries are
401+
transient recovery state, so Doctor records the retirement receipt and
402+
discards the file without importing it. Because a file cannot prove whether
403+
its entries are live or stale after SQLite rows have been pruned, operators
404+
must let file-era active runs settle before upgrading across this boundary.
401405
- Current conversation bindings now live in typed shared
402406
`current_conversation_bindings` rows keyed by normalized conversation id, with
403407
target agent/session columns, conversation kind, status, expiry, and metadata
@@ -1367,8 +1371,11 @@ sessionId})`; create, branch, continue, list, and fork flows live in their
13671371
OpenClaw temp root. They are recreated as needed and are not backup or
13681372
migration inputs.
13691373
- Subagent run registry persistence uses typed shared `subagent_runs` rows. The
1370-
old `subagents/runs.json` path is now only a doctor migration input, and
1371-
runtime helper names no longer describe the state layer as disk-backed.
1374+
old `subagents/runs.json` path is now only a Doctor cleanup input. Doctor
1375+
claims it under the state maintenance lock, records the discard decision in
1376+
SQLite, and removes it without importing transient run state. No runtime JSON
1377+
reader, writer, cache, or fallback remains; cross-version recovery of file-only
1378+
in-flight runs is intentionally unsupported at this retirement boundary.
13721379
Runtime tests no longer create invalid or empty `runs.json` fixtures to prove
13731380
registry behavior; they seed/read SQLite rows directly.
13741381
- Backup stages the state directory before archiving, copies non-database files,

‎scripts/check-database-first-legacy-stores.mjs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ const legacyStorePatterns = [
9595
/\bmedia\/outgoing\/records\/[^"'`]*\.json\b/u,
9696
/\bpush\/(?:web-push-subscriptions|vapid-keys)\.json\b/u,
9797
/\bnode\.json\b/u,
98+
/\bsubagents\/runs\.json\b/u,
9899
/\btmp\/skill-uploads\b/u,
99100
/\b(?:crestodian|openclaw)\/rescue-pending\/[^"'`]*\.json\b/u,
100101
/\bcron\/(?:runs\/[^"'`]+\.jsonl|jobs\.json|jobs-state\.json)\b/u,
@@ -116,6 +117,7 @@ const allowedRuntimeMigrationPaths = [
116117
"src/infra/state-migrations.managed-outgoing-images.ts",
117118
"src/infra/state-migrations.web-push.ts",
118119
"src/infra/state-migrations.node-host.ts",
120+
"src/infra/state-migrations.subagent-registry.ts",
119121
"src/infra/state-migrations.rescue-pending.ts",
120122
"src/commands/session-state-migration.ts",
121123
"src/commands/doctor-state-migrations.test.ts",

‎scripts/check-kysely-guardrails.mjs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ const rawSqliteAllowPathGroups = {
7272
"shared database stores with direct DatabaseSync access": ["src/proxy-capture/store.sqlite.ts"],
7373
"Kysely-backed stores that own a DatabaseSync boundary": [
7474
"src/acp/event-ledger.ts",
75-
"src/agents/subagent-registry.store.ts",
7675
"src/cron/store.ts",
7776
"src/infra/outbound/current-conversation-bindings.ts",
7877
"src/media/store.ts",

‎src/agents/subagent-registry-state.test.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,23 @@ function createRun(runId: string): SubagentRunRecord {
3131
}
3232

3333
describe("subagent registry state read cache", () => {
34-
const previousReadDiskFlag = process.env.OPENCLAW_TEST_READ_SUBAGENT_RUNS_FROM_DISK;
34+
const previousReadSqliteFlag = process.env.OPENCLAW_TEST_READ_SUBAGENT_RUNS_FROM_SQLITE;
3535

3636
beforeEach(() => {
3737
vi.useFakeTimers();
3838
vi.setSystemTime(1_000);
39-
process.env.OPENCLAW_TEST_READ_SUBAGENT_RUNS_FROM_DISK = "1";
39+
process.env.OPENCLAW_TEST_READ_SUBAGENT_RUNS_FROM_SQLITE = "1";
4040
clearSubagentRunsReadCacheForTest();
4141
mocks.loadSubagentRegistryFromSqlite.mockReset();
4242
mocks.saveSubagentRegistryToSqlite.mockReset();
4343
});
4444

4545
afterEach(() => {
4646
clearSubagentRunsReadCacheForTest();
47-
if (previousReadDiskFlag === undefined) {
48-
delete process.env.OPENCLAW_TEST_READ_SUBAGENT_RUNS_FROM_DISK;
47+
if (previousReadSqliteFlag === undefined) {
48+
delete process.env.OPENCLAW_TEST_READ_SUBAGENT_RUNS_FROM_SQLITE;
4949
} else {
50-
process.env.OPENCLAW_TEST_READ_SUBAGENT_RUNS_FROM_DISK = previousReadDiskFlag;
50+
process.env.OPENCLAW_TEST_READ_SUBAGENT_RUNS_FROM_SQLITE = previousReadSqliteFlag;
5151
}
5252
vi.useRealTimers();
5353
});

‎src/agents/subagent-registry-state.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ export function getSubagentRunsSnapshotForRead(
9393
inMemoryRuns: Map<string, SubagentRunRecord>,
9494
): Map<string, SubagentRunRecord> {
9595
const merged = new Map<string, SubagentRunRecord>();
96-
const shouldReadDisk =
97-
process.env.OPENCLAW_TEST_READ_SUBAGENT_RUNS_FROM_DISK === "1" ||
96+
const shouldReadPersisted =
97+
process.env.OPENCLAW_TEST_READ_SUBAGENT_RUNS_FROM_SQLITE === "1" ||
9898
!(process.env.VITEST || process.env.NODE_ENV === "test");
99-
if (shouldReadDisk) {
99+
if (shouldReadPersisted) {
100100
try {
101101
// Persisted state lets other worker processes observe active runs.
102102
// Cache this hot cross-process snapshot briefly; writes refresh the local

‎src/agents/subagent-registry.archive.e2e.test.ts‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,6 @@ vi.mock("../plugins/hook-runner-global.js", () => ({
7777
getGlobalHookRunner: vi.fn(() => null),
7878
}));
7979

80-
vi.mock("./subagent-registry.store.js", () => ({
81-
loadSubagentRegistryFromDisk: vi.fn(() => new Map()),
82-
saveSubagentRegistryToDisk: vi.fn(() => {}),
83-
}));
84-
8580
describe("subagent registry archive behavior", () => {
8681
let mod: typeof import("./subagent-registry.test-helpers.js");
8782

‎src/agents/subagent-registry.lifecycle-retry-grace.e2e.test.ts‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,6 @@ const loadConfigMock = vi.fn(() => ({
8989
agents: { defaults: { subagents: { archiveAfterMinutes: 0 } } },
9090
session: { mainKey: "main", scope: "per-sender" },
9191
}));
92-
const registryStoreMocks = vi.hoisted(() => ({
93-
loadRegistryMock: vi.fn(() => new Map()),
94-
saveRegistryMock: vi.fn(() => {}),
95-
}));
96-
9792
vi.mock("../config/sessions.js", () => ({
9893
loadSessionStore: vi.fn(() => sessionStore),
9994
resolveAgentIdFromSessionKey: (key: string) => key.match(/^agent:([^:]+)/)?.[1] ?? "main",
@@ -114,11 +109,6 @@ vi.mock("./subagent-depth.js", () => ({
114109
getSubagentDepthFromSessionStore: () => 0,
115110
}));
116111

117-
vi.mock("./subagent-registry.store.js", () => ({
118-
loadSubagentRegistryFromDisk: registryStoreMocks.loadRegistryMock,
119-
saveSubagentRegistryToDisk: registryStoreMocks.saveRegistryMock,
120-
}));
121-
122112
describe("subagent registry lifecycle error grace", () => {
123113
let previousFastTestEnv: string | undefined;
124114

@@ -128,8 +118,6 @@ describe("subagent registry lifecycle error grace", () => {
128118
vi.useFakeTimers();
129119
callGatewayMock.mockClear();
130120
onAgentEventMock.mockClear();
131-
registryStoreMocks.loadRegistryMock.mockClear().mockReturnValue(new Map());
132-
registryStoreMocks.saveRegistryMock.mockClear();
133121
loadConfigMock.mockClear().mockReturnValue({
134122
agents: { defaults: { subagents: { archiveAfterMinutes: 0 } } },
135123
session: { mainKey: "main", scope: "per-sender" },

‎src/agents/subagent-registry.nested.e2e.test.ts‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ vi.mock("./subagent-announce.js", () => ({
2020
buildSubagentSystemPrompt: vi.fn(() => "test prompt"),
2121
}));
2222

23-
vi.mock("./subagent-registry.store.js", () => ({
24-
loadSubagentRegistryFromDisk: vi.fn(() => new Map()),
25-
saveSubagentRegistryToDisk: vi.fn(() => {}),
26-
}));
27-
2823
let subagentRegistry: typeof import("./subagent-registry.test-helpers.js");
2924

3025
describe("subagent registry nested agent tracking", () => {

0 commit comments

Comments
 (0)