Skip to content

Commit 7fba71e

Browse files
authored
fix(agents): keep subagent status consistent after restart and large fan-outs (#117141)
* fix(agents): canonicalize subagent recovery and indexed lifecycle reads * fix(agents): retain single-scan subagent read-index contract * fix(agents): preserve canonical pending delivery across sqlite recovery
1 parent 1e0f62e commit 7fba71e

12 files changed

Lines changed: 454 additions & 934 deletions

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

Lines changed: 19 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -101,51 +101,24 @@ const defaultSubagentRegistryDeps: SubagentRegistryDeps = {
101101
};
102102

103103
export let subagentRegistryDeps: SubagentRegistryDeps = defaultSubagentRegistryDeps;
104-
type ContextEngineInitModule = Pick<
105-
{
106-
ensureContextEnginesInitialized: () => void;
107-
},
108-
"ensureContextEnginesInitialized"
109-
>;
110-
type ContextEngineRegistryModule = Pick<
111-
{
112-
resolveContextEngine: (
113-
cfg?: OpenClawConfig,
114-
options?: ResolveContextEngineOptions,
115-
) => Promise<ContextEngine>;
116-
},
117-
"resolveContextEngine"
118-
>;
119-
type RuntimePluginsModule = Pick<
120-
{
121-
ensureRuntimePluginsLoaded: typeof ensureRuntimePluginsLoadedFn;
122-
},
123-
"ensureRuntimePluginsLoaded"
124-
>;
104+
type SubagentRegistryRuntimeModule = {
105+
ensureContextEnginesInitialized: () => void;
106+
resolveContextEngine: (
107+
cfg?: OpenClawConfig,
108+
options?: ResolveContextEngineOptions,
109+
) => Promise<ContextEngine>;
110+
ensureRuntimePluginsLoaded: typeof ensureRuntimePluginsLoadedFn;
111+
};
125112

126113
const SUBAGENT_REGISTRY_RUNTIME_SPEC = ["./subagent-registry.runtime", ".js"] as const;
127114

128-
const contextEngineInitLoader = createLazyPromiseLoader(() =>
129-
importRuntimeModule<ContextEngineInitModule>(import.meta.url, SUBAGENT_REGISTRY_RUNTIME_SPEC),
130-
);
131-
const contextEngineRegistryLoader = createLazyPromiseLoader(() =>
132-
importRuntimeModule<ContextEngineRegistryModule>(import.meta.url, SUBAGENT_REGISTRY_RUNTIME_SPEC),
115+
// All three capabilities belong to the same lazy runtime module and lifecycle.
116+
const subagentRegistryRuntimeLoader = createLazyPromiseLoader(() =>
117+
importRuntimeModule<SubagentRegistryRuntimeModule>(
118+
import.meta.url,
119+
SUBAGENT_REGISTRY_RUNTIME_SPEC,
120+
),
133121
);
134-
const runtimePluginsLoader = createLazyPromiseLoader(() =>
135-
importRuntimeModule<RuntimePluginsModule>(import.meta.url, SUBAGENT_REGISTRY_RUNTIME_SPEC),
136-
);
137-
138-
function loadContextEngineInitModule(): Promise<ContextEngineInitModule> {
139-
return contextEngineInitLoader.load();
140-
}
141-
142-
function loadContextEngineRegistryModule(): Promise<ContextEngineRegistryModule> {
143-
return contextEngineRegistryLoader.load();
144-
}
145-
146-
function loadRuntimePluginsModule(): Promise<RuntimePluginsModule> {
147-
return runtimePluginsLoader.load();
148-
}
149122

150123
export async function ensureSubagentRegistryPluginRuntimeLoaded(params: {
151124
config: OpenClawConfig;
@@ -157,20 +130,18 @@ export async function ensureSubagentRegistryPluginRuntimeLoaded(params: {
157130
await ensureRuntimePluginsLoaded(params);
158131
return;
159132
}
160-
(await loadRuntimePluginsModule()).ensureRuntimePluginsLoaded(params);
133+
(await subagentRegistryRuntimeLoader.load()).ensureRuntimePluginsLoaded(params);
161134
}
162135

163136
export async function resolveSubagentRegistryContextEngine(
164137
cfg: OpenClawConfig,
165138
options?: ResolveContextEngineOptions,
166139
) {
167-
const initModule = await loadContextEngineInitModule();
168-
const registryModule = await loadContextEngineRegistryModule();
140+
const runtime = await subagentRegistryRuntimeLoader.load();
169141
const ensureContextEnginesInitialized =
170-
subagentRegistryDeps.ensureContextEnginesInitialized ??
171-
initModule.ensureContextEnginesInitialized;
142+
subagentRegistryDeps.ensureContextEnginesInitialized ?? runtime.ensureContextEnginesInitialized;
172143
const resolveContextEngine =
173-
subagentRegistryDeps.resolveContextEngine ?? registryModule.resolveContextEngine;
144+
subagentRegistryDeps.resolveContextEngine ?? runtime.resolveContextEngine;
174145
ensureContextEnginesInitialized();
175146
return await resolveContextEngine(cfg, options);
176147
}
@@ -182,9 +153,7 @@ export function setSubagentRegistryDepsForTest(overrides?: Partial<SubagentRegis
182153
}
183154

184155
export function resetSubagentRegistryRuntimeLoadersForTests() {
185-
contextEngineInitLoader.clear();
186-
contextEngineRegistryLoader.clear();
187-
runtimePluginsLoader.clear();
156+
subagentRegistryRuntimeLoader.clear();
188157
subagentAnnounceLoader.clear();
189158
browserCleanupLoader.clear();
190159
}

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

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,58 +25,55 @@ function collectorGroupKey(entry: SubagentRunRecord): string | undefined {
2525
]);
2626
}
2727

28-
function removeRunFromChildSessionIndex(runId: string, entry: SubagentRunRecord) {
29-
const sessionRuns = runsByChildSessionKey.get(entry.childSessionKey);
30-
if (sessionRuns?.get(runId) !== entry) {
28+
function removeIndexedSubagentRun(
29+
index: Map<string, Map<string, SubagentRunRecord>>,
30+
key: string | undefined,
31+
runId: string,
32+
entry: SubagentRunRecord,
33+
) {
34+
if (!key) {
35+
return;
36+
}
37+
const indexedRuns = index.get(key);
38+
if (indexedRuns?.get(runId) !== entry) {
3139
return;
3240
}
33-
sessionRuns.delete(runId);
34-
if (sessionRuns.size === 0) {
35-
runsByChildSessionKey.delete(entry.childSessionKey);
41+
indexedRuns.delete(runId);
42+
if (indexedRuns.size === 0) {
43+
index.delete(key);
3644
}
3745
}
3846

39-
function removeRunFromCollectorGroupIndex(runId: string, entry: SubagentRunRecord) {
40-
const key = collectorGroupKey(entry);
47+
function indexSubagentRun(
48+
index: Map<string, Map<string, SubagentRunRecord>>,
49+
key: string | undefined,
50+
runId: string,
51+
entry: SubagentRunRecord,
52+
) {
4153
if (!key) {
4254
return;
4355
}
44-
const groupRuns = runsByCollectorGroupKey.get(key);
45-
if (groupRuns?.get(runId) !== entry) {
46-
return;
47-
}
48-
groupRuns.delete(runId);
49-
if (groupRuns.size === 0) {
50-
runsByCollectorGroupKey.delete(key);
56+
const indexedRuns = index.get(key);
57+
if (indexedRuns) {
58+
indexedRuns.set(runId, entry);
59+
} else {
60+
index.set(key, new Map([[runId, entry]]));
5161
}
5262
}
5363

5464
class SubagentRunMap extends Map<string, SubagentRunRecord> {
5565
override set(runId: string, entry: SubagentRunRecord): this {
5666
const prev = this.get(runId);
5767
if (prev) {
58-
removeRunFromChildSessionIndex(runId, prev);
59-
removeRunFromCollectorGroupIndex(runId, prev);
68+
removeIndexedSubagentRun(runsByChildSessionKey, prev.childSessionKey, runId, prev);
69+
removeIndexedSubagentRun(runsByCollectorGroupKey, collectorGroupKey(prev), runId, prev);
6070
if (prev.collect === true && prev.childSessionKey) {
6171
collectorRunIdByChildSessionKey.delete(prev.childSessionKey);
6272
}
6373
}
6474
super.set(runId, entry);
65-
let sessionRuns = runsByChildSessionKey.get(entry.childSessionKey);
66-
if (!sessionRuns) {
67-
sessionRuns = new Map();
68-
runsByChildSessionKey.set(entry.childSessionKey, sessionRuns);
69-
}
70-
sessionRuns.set(runId, entry);
71-
const groupKey = collectorGroupKey(entry);
72-
if (groupKey) {
73-
let groupRuns = runsByCollectorGroupKey.get(groupKey);
74-
if (!groupRuns) {
75-
groupRuns = new Map();
76-
runsByCollectorGroupKey.set(groupKey, groupRuns);
77-
}
78-
groupRuns.set(runId, entry);
79-
}
75+
indexSubagentRun(runsByChildSessionKey, entry.childSessionKey, runId, entry);
76+
indexSubagentRun(runsByCollectorGroupKey, collectorGroupKey(entry), runId, entry);
8077
if (entry.collect === true && entry.childSessionKey) {
8178
collectorRunIdByChildSessionKey.set(entry.childSessionKey, runId);
8279
}
@@ -86,8 +83,8 @@ class SubagentRunMap extends Map<string, SubagentRunRecord> {
8683
override delete(runId: string): boolean {
8784
const prev = this.get(runId);
8885
if (prev) {
89-
removeRunFromChildSessionIndex(runId, prev);
90-
removeRunFromCollectorGroupIndex(runId, prev);
86+
removeIndexedSubagentRun(runsByChildSessionKey, prev.childSessionKey, runId, prev);
87+
removeIndexedSubagentRun(runsByCollectorGroupKey, collectorGroupKey(prev), runId, prev);
9188
}
9289
if (
9390
prev?.collect === true &&

0 commit comments

Comments
 (0)