fix(background): replay persisted updates before launch order

This commit is contained in:
pi
2026-04-12 14:43:14 +01:00
parent 0f60ecdb49
commit 48c9ba6126
2 changed files with 84 additions and 5 deletions

View File

@@ -110,7 +110,9 @@ export default function subagentsExtension(pi: ExtensionAPI, deps: any = {}) {
}, { triggerTurn: false });
} catch (e) {}
} catch (e) {
// monitorRun errors are non-fatal here
// Monitoring runs happens in the background. Failures here should not
// block tool registration or the foreground session; monitorRun errors
// are best-effort and final failures are logged via result.json.
}
}
@@ -178,18 +180,25 @@ export default function subagentsExtension(pi: ExtensionAPI, deps: any = {}) {
// replay persisted runs if session manager provides entries
try {
const entries = ctx.sessionManager?.getEntries?.() ?? [];
// clear existing registry and rebuild from session entries
registry.replay([]);
const launches: any[] = [];
const updates: any[] = [];
for (const e of entries) {
const type = (e.type ?? e.customType) as string | undefined;
if (type === "pi-subagents:bg-run") {
const run = e.data?.run ?? e.data;
if (run && run.runId) registry.recordLaunch(run as any);
if (run && run.runId) launches.push(run);
} else if (type === "pi-subagents:bg-update") {
const data = e.data;
if (data && data.runId) registry.recordUpdate(data.runId, data as any);
if (data && data.runId) updates.push(data);
}
}
// Rebuild in two passes so out-of-order persisted entries cannot drop
// terminal updates that happen to appear before their launch entry.
registry.replay([]);
for (const run of launches) registry.recordLaunch(run as any);
for (const update of updates) registry.recordUpdate(update.runId, update as any);
} catch (e) {}
updateStatus();