diff --git a/apps/server/src/mcp/WorkflowRuns.test.ts b/apps/server/src/mcp/WorkflowRuns.test.ts index 52bd099ddeef..44f12e289d45 100644 --- a/apps/server/src/mcp/WorkflowRuns.test.ts +++ b/apps/server/src/mcp/WorkflowRuns.test.ts @@ -1108,6 +1108,99 @@ it.layer(layerTest)("t3_workflow_run", (it) => { }), ); + it.effect( + "shows a judge's grown agents queued under their phase and round, within 2 KB per update", + () => + Effect.gen(function* () { + const runs = yield* workflowRunsIn(yield* Effect.scope); + const parent = yield* seedParent("grow"); + const growing = { + name: "grow", + stages: [ + { + id: "loop", + repeat: { + stages: [ + { id: "draft", agents: [{ id: "poet", task: "Write." }] }, + { + id: "critique", + agents: [ + { + id: "critic", + task: "Judge {{draft}}.", + resultSchema: { type: "object", required: ["ok"] }, + }, + ], + }, + ], + judge: "critic", + until: { path: "ok", equals: true }, + max: 2, + templates: { probe: { task: "Probe {{input.angle}}." } }, + }, + }, + ], + }; + const { subagentId } = yield* runs.run(parent.scope, growing); + yield* parent.endTurn; + const settled = new Set(); + const settleNext = (count: number, text: string) => + Effect.gen(function* () { + const children = yield* childrenOf(parent.threadId, subagentId, settled.size + count); + for (const child of children.filter((c) => !settled.has(c.id))) { + settled.add(child.id); + yield* settleChild(child, text); + } + return children; + }); + const addAgents = Array.from({ length: 17 }, (_, index) => ({ + key: `probe-${index + 1}`, + template: "probe", + input: { angle: `angle ${index + 1}` }, + })); + yield* settleNext(1, "Draft."); + yield* settleNext( + 1, + `\`\`\`json\n${JSON.stringify({ ok: false, addAgents, rerun: ["critique"] })}\n\`\`\``, + ); + + const grown = yield* (yield* parentEvents(parent.threadId)).pipe( + Stream.filter(({ payload }) => + (payload.workflow?.agents ?? []).some((agent) => agent.label === "probe-17"), + ), + Stream.runHead, + Effect.map((event) => Option.getOrThrow(event).payload.workflow!), + ); + assert.deepEqual( + grown.phases.map((phase) => phase.title), + ["draft · round 1", "critique · round 1", "draft · round 2", "critique · round 2"], + ); + // Every grown row is there before round 2 starts anything; rerun left the poet out. + const round2 = grown.agents.filter((agent) => (agent.phaseIndex ?? 0) >= 2); + assert.deepEqual( + round2.map((row) => row.label), + [...addAgents.map((add) => add.key), "critic"], + ); + for (const row of round2) { + assert.deepEqual([row.state, row.childThreadId], ["queued", undefined]); + } + + const probes = yield* settleNext(17, "Probed."); + assert.include( + probes.find((child) => child.title === "draft.probe-3#2")?.prompt ?? "", + "Probe angle 3.", + ); + yield* settleNext(1, '```json\n{"ok":true}\n```'); + assert.equal((yield* ended(parent.threadId, subagentId))?.status, "completed"); + const status = yield* runs.status(parent.scope, subagentId); + assert.deepInclude(status.stages[0], { exit: "accepted" }); + + const sized = yield* largestUpdate(parent.threadId, subagentId); + assert.lengthOf(sized.agents, 20); + assert.isAtMost(sized.largest, 2048); + }), + ); + it.effect("resumes after a restart without relaunching a child and still wakes once", () => Effect.gen(function* () { const projections = yield* ProjectionStore.ProjectionStoreV2; diff --git a/apps/server/src/mcp/WorkflowRuns.ts b/apps/server/src/mcp/WorkflowRuns.ts index 1b8ac8e59f3f..ea4c8157df32 100644 --- a/apps/server/src/mcp/WorkflowRuns.ts +++ b/apps/server/src/mcp/WorkflowRuns.ts @@ -441,23 +441,31 @@ const make = Effect.gen(function* () { const scope = yield* requireThreadScope(callerScope, "t3_workflow_run"); const spec = yield* decodeWorkflowSpec(input); const targets = new Map(); - for (const stage of agentStages(spec)) { - for (const agent of stage.agents) { - if (agent.target?.environmentId !== undefined) { - return yield* failure( - "invalid_request", - `Agent "${agent.id}": workflow agents run here. Omit target.environmentId.`, - ); - } - const selection = yield* mcp - .checkTarget(scope, agent.target) - .pipe( - Effect.mapError((error) => - failure(error.code, `Agent "${agent.id}": ${error.message}`), - ), - ); - targets.set(targetKey(stage.id, agent.id), targetOf(selection)); + // Templates are checked up front too; only spec agents are planned, as grown keys come later. + const templates = spec.stages.flatMap((stage) => + "agents" in stage + ? [] + : Object.entries(stage.repeat.templates ?? {}).map(([id, template]) => ({ + stage: null, + agent: { ...template, id }, + })), + ); + const agents = agentStages(spec).flatMap((stage) => + stage.agents.map((agent) => ({ stage: stage.id, agent })), + ); + for (const { stage, agent } of [...agents, ...templates]) { + if (agent.target?.environmentId !== undefined) { + return yield* failure( + "invalid_request", + `Agent "${agent.id}": workflow agents run here. Omit target.environmentId.`, + ); } + const selection = yield* mcp + .checkTarget(scope, agent.target) + .pipe( + Effect.mapError((error) => failure(error.code, `Agent "${agent.id}": ${error.message}`)), + ); + if (stage !== null) targets.set(targetKey(stage, agent.id), targetOf(selection)); } const parent = yield* threads .getThreadRecords(scope.thread.threadId, ["runs", "providerThreads"]) diff --git a/apps/server/src/mcp/toolkits/orchestrator/tools.ts b/apps/server/src/mcp/toolkits/orchestrator/tools.ts index 9aab62c70592..93bd155d034c 100644 --- a/apps/server/src/mcp/toolkits/orchestrator/tools.ts +++ b/apps/server/src/mcp/toolkits/orchestrator/tools.ts @@ -136,7 +136,7 @@ const WorkflowStage = Schema.Struct({ const WorkflowRunTool = Tool.make("t3_workflow_run", { description: - "Run agents in stages; returns at once and wakes you when done. Stages run in order: `{id, agents:[{id, task, target?, role?, isolation?, resultSchema?}]}` in parallel, or `{id, repeat:{stages, judge, until:{path, equals}, max, onExhausted?}}`. `{{stage}}`, `{{stage.agent}}`, `{{feedback}}` in a task insert results.", + "Run stages in order; returns at once, wakes you when done. Stage: `{id,agents:[{id,task,target?,role?,isolation?,resultSchema?}]}` (parallel) or `{id,repeat:{stages,judge,until:{path,equals},max,onExhausted?,dry?,templates?:{name:{task}}}}`. Its judge may return `addAgents:[{key,template,input,stage?}]`, `rerun:[stage]`; `dry:N` ends after N rounds of neither. {{stage}}, {{stage.agent}}, {{feedback}}, {{input.x}} insert results.", parameters: Schema.Struct({ name: TrimmedNonEmptyString, stages: Schema.Array(WorkflowStage), diff --git a/apps/server/src/provider/agentContextBudget.json b/apps/server/src/provider/agentContextBudget.json index 0f9ad7d1fad0..1d99b2763b8b 100644 --- a/apps/server/src/provider/agentContextBudget.json +++ b/apps/server/src/provider/agentContextBudget.json @@ -1,5 +1,5 @@ { "claudeSystemAppendBytes": 5135, "codexAdditionalContextBytes": 6865, - "mcpToolListJsonBytes": 46990 + "mcpToolListJsonBytes": 47106 }