fix: resume subagent registry safely (#831) (thanks @roshanasingh4)

This commit is contained in:
Peter Steinberger
2026-01-13 10:10:15 +00:00
parent 714de9d996
commit b071f73fef
6 changed files with 115 additions and 35 deletions

View File

@@ -8,11 +8,13 @@ export type PersistedSubagentRegistryVersion = 1;
type PersistedSubagentRegistry = {
version: 1;
runs: Record<string, SubagentRunRecord>;
runs: Record<string, PersistedSubagentRunRecord>;
};
const REGISTRY_VERSION = 1 as const;
type PersistedSubagentRunRecord = Omit<SubagentRunRecord, "announceHandled">;
export function resolveSubagentRegistryPath(): string {
return path.join(STATE_DIR_CLAWDBOT, "subagents", "runs.json");
}
@@ -28,9 +30,17 @@ export function loadSubagentRegistryFromDisk(): Map<string, SubagentRunRecord> {
const out = new Map<string, SubagentRunRecord>();
for (const [runId, entry] of Object.entries(runsRaw)) {
if (!entry || typeof entry !== "object") continue;
const typed = entry as SubagentRunRecord;
const typed = entry as PersistedSubagentRunRecord;
if (!typed.runId || typeof typed.runId !== "string") continue;
out.set(runId, typed);
const announceCompletedAt =
typeof typed.announceCompletedAt === "number"
? typed.announceCompletedAt
: undefined;
out.set(runId, {
...typed,
announceCompletedAt,
announceHandled: Boolean(announceCompletedAt),
});
}
return out;
}
@@ -39,9 +49,14 @@ export function saveSubagentRegistryToDisk(
runs: Map<string, SubagentRunRecord>,
) {
const pathname = resolveSubagentRegistryPath();
const serialized: Record<string, PersistedSubagentRunRecord> = {};
for (const [runId, entry] of runs.entries()) {
const { announceHandled: _ignored, ...persisted } = entry;
serialized[runId] = persisted;
}
const out: PersistedSubagentRegistry = {
version: REGISTRY_VERSION,
runs: Object.fromEntries(runs.entries()),
runs: serialized,
};
saveJsonFile(pathname, out);
}