Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions apps/server/src/mcp/WorkflowRuns.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ import * as ScheduledTaskService from "../scheduledTasks/ScheduledTaskService.ts
import * as SecretRequests from "../secrets/SecretRequests.ts";
import * as ServerSettings from "../serverSettings.ts";
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
import {
deriveWorkflowGroups,
projectedSubagentsToRuntime,
} from "../../../../packages/client-runtime/src/state/subagentRuntime.ts";
import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
import * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests";
import type { McpThreadInvocationScope } from "./McpInvocationContext.ts";
Expand Down Expand Up @@ -1004,6 +1008,106 @@ it.layer(layerTest)("t3_workflow_run", (it) => {
}),
);

it.effect(
"puts a judge's grown agents in their own round's phase, which reads as not started until they launch",
() =>
Effect.gen(function* () {
const runs = yield* workflowRunsIn(yield* Effect.scope);
const parent = yield* seedParent("grow-phase");
const { subagentId } = yield* runs.run(parent.scope, {
name: "grow-phase",
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}}." } },
},
},
],
});
yield* parent.endTurn;
const [poet] = yield* childrenOf(parent.threadId, subagentId, 1);
yield* settleChild(poet!, "Draft.");
const critic = (yield* childrenOf(parent.threadId, subagentId, 2)).find(
(child) => child.id !== poet!.id,
)!;
const addAgents = ["probe-1", "probe-2"].map((key) => ({
key,
template: "probe",
input: { angle: key },
}));
yield* settleChild(
critic,
`\`\`\`json\n${JSON.stringify({ ok: false, addAgents, rerun: ["critique"] })}\n\`\`\``,
);

// The row as it stands once both grown agents are recorded, before either launches.
const row = yield* (yield* parentEvents(parent.threadId)).pipe(
Stream.filter(({ payload }) =>
(payload.workflow?.agents ?? []).some((agent) => agent.label === "probe-2"),
),
Stream.runHead,
Effect.map((event) => Option.getOrThrow(event).payload),
);
const titles = row.workflow!.phases.map((phase) => phase.title);
assert.deepEqual(titles, [
"draft · round 1",
"critique · round 1",
"draft · round 2",
"critique · round 2",
]);
const grown = row.workflow!.agents.filter((agent) => agent.label.startsWith("probe-"));
assert.deepEqual(
grown.map((agent) => [agent.label, titles[agent.phaseIndex!], agent.state]),
[
["probe-1", "draft · round 2", "queued"],
["probe-2", "draft · round 2", "queued"],
],
);
const phaseStates = (subagent: OrchestrationV2Subagent) =>
deriveWorkflowGroups(projectedSubagentsToRuntime([subagent]))[0]!.phases.map((phase) => [
phase.title,
phase.state,
]);
assert.deepEqual(phaseStates(row), [
["draft · round 1", "done"],
["critique · round 1", "done"],
["draft · round 2", "pending"],
["critique · round 2", "pending"],
]);

const probes = (yield* childrenOf(parent.threadId, subagentId, 4)).filter((child) =>
child.title?.startsWith("draft.probe-"),
);
assert.deepEqual(probes.map((child) => child.title).toSorted(), [
"draft.probe-1#2",
"draft.probe-2#2",
]);
for (const child of probes) yield* settleChild(child, "Probed.");
const last = (yield* childrenOf(parent.threadId, subagentId, 5)).find(
(child) => child.title === "critique.critic#2",
)!;
yield* settleChild(last, '```json\n{"ok":true}\n```');
assert.equal((yield* ended(parent.threadId, subagentId))?.status, "completed");
}),
);

it.effect("resumes after a restart without relaunching a child and still wakes once", () =>
Effect.gen(function* () {
const projections = yield* ProjectionStore.ProjectionStoreV2;
Expand Down
8 changes: 7 additions & 1 deletion apps/server/src/mcp/WorkflowRuns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
agentStages,
decodeWorkflowSpec,
initialSnapshot,
phaseAgents,
resumeWorkflow,
WorkflowAgentOutcome,
type WorkflowPort,
Expand Down Expand Up @@ -171,7 +172,12 @@ function toRow(
const { launches, ...state } = snapshot;
const phases = workflowPhases(spec, snapshot);
const agents = phases.flatMap(({ stage, round }, phaseIndex) =>
stage.agents.map((agent) => ({ stage: stage.id, agent: agent.id, round, phaseIndex })),
phaseAgents(spec, snapshot, stage, round).map((agent) => ({
stage: stage.id,
agent,
round,
phaseIndex,
})),
);
return {
workflow: {
Expand Down
Loading
Loading