File size: 13,694 Bytes
4c76b0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import crypto from "node:crypto";
import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
import type { OpenClawConfig } from "../../../config/types.openclaw.js";
import type { SubagentLifecycleHookRunner } from "../../../plugins/hooks.js";
import { isValidAgentId, normalizeAgentId } from "../../../routing/session-key.js";
import { listAgentIds } from "../../agent-scope-config.js";
import { resolveSessionAgentId } from "../../agent-scope.js";
import { reserveChildAdmissionSlot } from "../../child-admission.js";
import { resolveSpawnAdmission, resolveSpawnMode } from "../../spawn-plan.js";
import { listSwarmRunsForGroup } from "../registry/subagent-registry.js";
import { resolveSwarmConfig } from "../swarm/swarm-config.js";
import { validateStructuredOutputSchema } from "../swarm/swarm-output-schema.js";
import { reserveSwarmRun } from "../swarm/swarm-scheduler.js";
import { resolveSubagentContextMode } from "./subagent-spawn-context.js";
import type {
  SpawnSubagentContext,
  SpawnSubagentParams,
  SpawnSubagentResult,
} from "./subagent-spawn-contract.js";
import { getSubagentSpawnDeps } from "./subagent-spawn-deps.js";
import { resolveSubagentSpawnOwnership } from "./subagent-spawn-ownership.js";
import { resolveConfiguredSubagentRunTimeoutSeconds } from "./subagent-spawn-plan.js";
import { loadSubagentConfig } from "./subagent-spawn-session-patch.js";
import {
  loadSessionEntry,
  resolveGatewaySessionStoreTarget,
  resolveInternalSessionKey,
  resolveMainSessionAlias,
} from "./subagent-spawn.runtime.js";
import { normalizeSubagentTaskName } from "./subagent-task-name.js";

type ResolvedSubagentSpawnRequest = {
  request: {
    taskName?: string;
    spawnMode: ReturnType<typeof resolveSpawnMode>;
    cleanup: "delete" | "keep";
    expectsCompletionMessage: boolean;
    completionRequesterSessionId?: string;
  };
  runtime: {
    hookRunner: SubagentLifecycleHookRunner | null;
    cfg: OpenClawConfig;
    runTimeoutSeconds: number;
    contextMode: ReturnType<typeof resolveSubagentContextMode>;
    requesterInternalKey: string;
    ownership: ReturnType<typeof resolveSubagentSpawnOwnership>;
    requesterAgentId: string;
    targetAgentId: string;
  };
  swarm: {
    config: ReturnType<typeof resolveSwarmConfig>;
    groupId?: string;
    schedulerGroupKey?: string;
    launchReplayKey?: string;
    soleImplicitMember: boolean;
    reservationPending: boolean;
  };
  admission: {
    resolve: (pendingChildren?: number) => ReturnType<typeof resolveSpawnAdmission>;
    initial: ReturnType<typeof resolveSpawnAdmission> & { ok: true };
    reservation?: { release: () => void };
    childDepth: number;
    maxSpawnDepth: number;
  };
  childIdem: string;
};

type ResolveSubagentSpawnRequestResult =
  | { ok: false; result: SpawnSubagentResult }
  | { ok: true; resolved: ResolvedSubagentSpawnRequest };

function rejectSubagentSpawnRequest(
  status: "error" | "forbidden",
  error: string,
): ResolveSubagentSpawnRequestResult {
  return { ok: false, result: { status, error } };
}

export function resolveSubagentSpawnRequest(
  params: SpawnSubagentParams,
  ctx: SpawnSubagentContext,
): ResolveSubagentSpawnRequestResult {
  const requestedAgentId = params.agentId?.trim();
  const taskNameResult = normalizeSubagentTaskName(params.taskName);
  if (taskNameResult.error) {
    return rejectSubagentSpawnRequest("error", taskNameResult.error);
  }
  const taskName = taskNameResult.taskName;

  // Reject malformed agentId before normalizeAgentId can mangle it.
  // Without this gate, error-message strings like "Agent not found: xyz" pass
  // through normalizeAgentId and become "agent-not-found--xyz", which later
  // creates ghost workspace directories and triggers cascading cron loops (#31311).
  if (requestedAgentId && !isValidAgentId(requestedAgentId)) {
    return rejectSubagentSpawnRequest(
      "error",
      `Invalid agentId "${requestedAgentId}". Agent IDs must match [a-z0-9][a-z0-9_-]{0,63}.`,
    );
  }
  const requestThreadBinding = params.thread === true;
  const spawnMode = resolveSpawnMode({
    requestedMode: params.mode,
    threadRequested: requestThreadBinding,
  });
  if (
    params.completionTarget === "parent" &&
    (params.collect ||
      requestThreadBinding ||
      spawnMode !== "run" ||
      params.expectsCompletionMessage === false)
  ) {
    return rejectSubagentSpawnRequest(
      "error",
      'sessions_spawn completionTarget="parent" requires mode="run", thread=false, collect=false, and completion notifications enabled.',
    );
  }
  if (params.collect && (requestThreadBinding || spawnMode === "session")) {
    return rejectSubagentSpawnRequest(
      "error",
      "sessions_spawn collect=true requires mode=run and thread=false.",
    );
  }
  if (spawnMode === "session" && !requestThreadBinding) {
    return rejectSubagentSpawnRequest(
      "error",
      'sessions_spawn(mode="session") requires thread=true so the subagent can stay bound to a channel thread. ' +
        'Retry with { mode: "session", thread: true } on a channel that supports threads, or use mode="run" for one-shot work.',
    );
  }
  const cleanup =
    spawnMode === "session"
      ? "keep"
      : params.cleanup === "keep" || params.cleanup === "delete"
        ? params.cleanup
        : "keep";
  const expectsCompletionMessage = params.collect
    ? false
    : params.expectsCompletionMessage !== false;
  const hookRunner = getSubagentSpawnDeps().getGlobalHookRunner();
  const cfg = loadSubagentConfig();

  // When agent omits runTimeoutSeconds, use the config default.
  // Falls back to 0 (no timeout) if config key is also unset,
  // preserving current behavior for existing deployments.
  const runTimeoutSeconds = resolveConfiguredSubagentRunTimeoutSeconds({
    cfg,
    runTimeoutSeconds: params.runTimeoutSeconds,
  });
  const contextMode = resolveSubagentContextMode({
    requestedContext: params.context,
    threadRequested: requestThreadBinding,
    cfg,
    requester: {
      channel: ctx.agentChannel,
      accountId: ctx.agentAccountId,
    },
  });
  const { mainKey, alias } = resolveMainSessionAlias(cfg);
  const requesterSessionKey = ctx.agentSessionKey;
  const requesterInternalKey = requesterSessionKey
    ? resolveInternalSessionKey({
        key: requesterSessionKey,
        alias,
        mainKey,
      })
    : alias;
  const ownership = resolveSubagentSpawnOwnership({
    cfg,
    agentSessionKey: ctx.agentSessionKey,
    completionOwnerKey: ctx.completionOwnerKey,
  });

  // Bind private results to the admitted parent incarnation; a reset must not
  // transfer a retained child result to a replacement session at the same key.
  let completionRequesterSessionId: string | undefined;
  if (params.completionTarget === "parent") {
    const target = resolveGatewaySessionStoreTarget({
      cfg,
      key: ownership.completionRequesterSessionKey,
    });
    completionRequesterSessionId = loadSessionEntry({
      storePath: target.storePath,
      sessionKey: target.canonicalKey,
      clone: false,
    })?.sessionId;
    if (!completionRequesterSessionId) {
      return rejectSubagentSpawnRequest(
        "error",
        "Private completion requires an existing requester session. Retry from an active session.",
      );
    }
  }

  const requesterAgentId = resolveSessionAgentId({
    config: cfg,
    sessionKey: requesterInternalKey,
    agentId: ctx.requesterAgentIdOverride,
  });
  const swarmConfig = resolveSwarmConfig(cfg, requesterAgentId);
  const hasSwarmParams =
    params.collect !== undefined ||
    params.outputSchema !== undefined ||
    params.fastMode !== undefined ||
    params.groupId !== undefined;
  if (hasSwarmParams && !swarmConfig.enabled) {
    return rejectSubagentSpawnRequest(
      "forbidden",
      "sessions_spawn swarm parameters require tools.swarm.enabled=true.",
    );
  }
  if (params.outputSchema && !params.collect) {
    return rejectSubagentSpawnRequest(
      "error",
      "sessions_spawn outputSchema requires collect=true.",
    );
  }
  if (params.groupId !== undefined && !params.collect) {
    return rejectSubagentSpawnRequest("error", "sessions_spawn groupId requires collect=true.");
  }
  if (params.outputSchema) {
    const schemaError = validateStructuredOutputSchema(params.outputSchema);
    if (schemaError) {
      return rejectSubagentSpawnRequest("error", schemaError);
    }
  }

  const usingDefaultAgentId =
    params.collect === true && !requestedAgentId && Boolean(swarmConfig.defaultAgentId);
  const effectiveRequestedAgentId = usingDefaultAgentId
    ? swarmConfig.defaultAgentId
    : requestedAgentId;
  if (usingDefaultAgentId) {
    if (!isValidAgentId(effectiveRequestedAgentId)) {
      return rejectSubagentSpawnRequest(
        "error",
        `tools.swarm.defaultAgentId contains invalid agentId "${effectiveRequestedAgentId}".`,
      );
    }
  }
  const targetAgentId = effectiveRequestedAgentId
    ? normalizeAgentId(effectiveRequestedAgentId)
    : requesterAgentId;
  const configuredAgentIds = listAgentIds(cfg);
  const explicitSwarmGroupId = normalizeOptionalString(params.groupId);
  const requesterRunId = normalizeOptionalString(ctx.requesterRunId);
  const swarmGroupId = params.collect
    ? (explicitSwarmGroupId ??
      (requesterRunId ? `swarm:${requesterInternalKey}:${requesterRunId}` : undefined))
    : undefined;
  const swarmSchedulerGroupKey = swarmGroupId
    ? JSON.stringify([requesterAgentId, requesterInternalKey, swarmGroupId])
    : undefined;
  const resolveAdmission = (pendingChildren = 0) => {
    const collectorRuns = params.collect
      ? swarmGroupId
        ? listSwarmRunsForGroup(swarmGroupId, requesterInternalKey, requesterAgentId)
        : []
      : undefined;
    return resolveSpawnAdmission({
      cfg,
      collector: collectorRuns
        ? {
            liveChildren: collectorRuns.filter((entry) => !entry.collectorCompletion).length,
            totalChildren: collectorRuns.length,
            maxChildrenPerGroup: swarmConfig.maxChildrenPerGroup,
            maxTotalPerGroup: swarmConfig.maxTotalPerGroup,
          }
        : undefined,
      requesterSessionKey: requesterInternalKey,
      requesterAgentId,
      targetAgentId,
      requestedAgentId: effectiveRequestedAgentId,
      configuredAgentIds,
      additionalActiveChildren: pendingChildren,
    });
  };
  const admissionReservation = params.collect
    ? undefined
    : reserveChildAdmissionSlot({
        controllerSessionKey: ownership.controllerSessionKey,
        resolveAdmission,
      });
  const admission = admissionReservation ?? resolveAdmission();
  if (admissionReservation?.ok) {
    ctx.onSpawnEffectsStart?.();
  }
  if (!admission.ok) {
    return rejectSubagentSpawnRequest(
      "forbidden",
      usingDefaultAgentId && !admission.governingCap?.startsWith("tools.swarm.")
        ? `tools.swarm.defaultAgentId is unavailable: ${admission.error}`
        : admission.error,
    );
  }
  if (params.collect && !swarmGroupId) {
    return rejectSubagentSpawnRequest(
      "error",
      "sessions_spawn collect=true requires a requesting run id when groupId is omitted.",
    );
  }
  const childDepth = admission.childSessionPatch?.spawnDepth ?? 1;
  const maxSpawnDepth = admission.maxSpawnDepth ?? childDepth;
  const swarmLaunchReplayKey = normalizeOptionalString(params.swarmLaunchReplayKey);
  // Registry and Gateway identities are global, while host replay keys are requester-scoped.
  const childIdem = swarmLaunchReplayKey
    ? `swarm_${crypto
        .createHash("sha256")
        .update(JSON.stringify([requesterInternalKey, swarmLaunchReplayKey]))
        .digest("hex")
        .slice(0, 32)}`
    : crypto.randomUUID();
  let reservationPending = false;
  let soleImplicitMember = false;
  if (params.collect && swarmGroupId && swarmSchedulerGroupKey) {
    const groupRuns = listSwarmRunsForGroup(swarmGroupId, requesterInternalKey, requesterAgentId);
    soleImplicitMember = !explicitSwarmGroupId && !swarmLaunchReplayKey && groupRuns.length === 0;
    // Swarm reservation can reconcile existing lane state even when it rejects a duplicate.
    ctx.onSpawnEffectsStart?.();
    if (
      !reserveSwarmRun({
        groupId: swarmSchedulerGroupKey,
        runId: childIdem,
        maxConcurrent: swarmConfig.maxConcurrent,
        activeRunIds: groupRuns
          .filter(
            (entry) =>
              entry.execution.status === "running" || entry.execution.status === "interrupted",
          )
          .map((entry) => entry.schedulerSlotId ?? entry.runId),
      })
    ) {
      return rejectSubagentSpawnRequest(
        "error",
        "sessions_spawn could not reserve swarm FIFO order.",
      );
    }
    reservationPending = true;
  }
  return {
    ok: true,
    resolved: {
      request: {
        taskName,
        spawnMode,
        cleanup,
        expectsCompletionMessage,
        completionRequesterSessionId,
      },
      runtime: {
        hookRunner,
        cfg,
        runTimeoutSeconds,
        contextMode,
        requesterInternalKey,
        ownership,
        requesterAgentId,
        targetAgentId,
      },
      swarm: {
        config: swarmConfig,
        groupId: swarmGroupId,
        schedulerGroupKey: swarmSchedulerGroupKey,
        launchReplayKey: swarmLaunchReplayKey,
        soleImplicitMember,
        reservationPending,
      },
      admission: {
        resolve: resolveAdmission,
        initial: admission,
        reservation: admissionReservation?.ok ? admissionReservation : undefined,
        childDepth,
        maxSpawnDepth,
      },
      childIdem,
    },
  };
}