Skip to content
Closed
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
89 changes: 89 additions & 0 deletions apps/server/src/mcp/McpHttpServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import * as McpSessionRegistry from "./McpSessionRegistry.ts";
import * as OrchestratorMcpService from "./OrchestratorMcpService.ts";
import * as PreviewAutomationBroker from "./PreviewAutomationBroker.ts";
import * as WorkflowRuns from "./WorkflowRuns.ts";
import { decodeWorkflowSpec } from "./workflowRun.ts";
import * as ThreadMetadataMcpService from "./ThreadMetadataMcpService.ts";
import * as OrchestratorHandlers from "./toolkits/orchestrator/handlers.ts";
import * as PeerForwarding from "../peer/PeerForwarding.ts";
Expand Down Expand Up @@ -495,6 +496,94 @@ it("advertises workflow stages as objects that need an id", () => {
expect(schema.properties.stages.items?.required).toEqual(["id"]);
});

const repeatSpec = {
name: "review-loop",
stages: [
{
id: "loop",
repeat: {
stages: [
{
id: "review",
agents: [
{
id: "judge",
task: "Judge.",
resultSchema: {
type: "object",
properties: { verdict: { type: "string" } },
required: ["verdict"],
},
},
],
},
],
judge: "judge",
until: { path: "verdict", equals: "accept" },
max: 3,
},
},
],
};

it("advertises the repeat shape with a string judge and required stages, until and max", () => {
const schema = advertisedJsonSchema(OrchestratorToolkit.tools.t3_workflow_run);
expect(schema).toMatchObject({
properties: {
stages: {
items: {
properties: {
repeat: {
type: "object",
required: ["stages", "judge", "until", "max"],
properties: {
stages: { type: "array", items: { type: "object", required: ["id", "agents"] } },
judge: { type: "string" },
until: { type: "object", required: ["path", "equals"] },
max: { type: "integer", minimum: 1 },
onExhausted: { enum: ["fail", "pass"] },
},
},
},
},
},
},
});
});

it.effect.each(["judge", "stages", "until", "max"])(
"rejects malformed repeat %s by naming the expected shape before the run starts",
(field) => {
const calls: Array<unknown> = [];
return Effect.gen(function* () {
const { repeat } = repeatSpec.stages[0]!;
const malformed: Record<string, unknown> = Object.fromEntries(
Object.entries(repeat).filter(([key]) => key !== field),
);
if (field === "judge") malformed.judge = { id: "private-input" };
const rejected = yield* callTool("t3_workflow_run", {
...repeatSpec,
stages: [{ id: "loop", repeat: malformed }],
}).pipe(Effect.flip);
expect(String(rejected)).toContain(
'repeat is {stages:[{id, agents:[…]}], judge:"agent-id", until:{path, equals}, max:1..10, onExhausted?:"fail"|"pass"}',
);
expect(String(rejected)).not.toContain("private-input");
expect(calls).toEqual([]);
}).pipe(Effect.provide(layerRecordingTools(calls)));
},
);

const decodeWorkflowParameters = Schema.decodeUnknownEffect(
OrchestratorToolkit.tools.t3_workflow_run.parametersSchema,
);
it.effect("keeps a valid repeat spec unchanged through tool and runner decoding", () =>
Effect.gen(function* () {
const input = yield* decodeWorkflowParameters(repeatSpec);
expect(yield* decodeWorkflowSpec(input)).toEqual(repeatSpec);
}),
);

it("keeps cross-tool routing out of the delegate_task description", () => {
const description = OrchestratorToolkit.tools.delegate_task.description ?? "";
expect(description).toContain("Start one child agent with only `task`");
Expand Down
28 changes: 25 additions & 3 deletions apps/server/src/mcp/toolkits/orchestrator/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ import {
TrimmedNonEmptyString,
} from "@t3tools/contracts";
import { McpSchema, Tool, Toolkit } from "effect/ai";
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";
import * as SchemaIssue from "effect/SchemaIssue";
import * as Struct from "effect/Struct";

import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
Expand All @@ -52,6 +54,7 @@ import * as McpInvocationContext from "../../McpInvocationContext.ts";
import * as OrchestratorMcpService from "../../OrchestratorMcpService.ts";
import * as ThreadMetadataMcpService from "../../ThreadMetadataMcpService.ts";
import * as WorkflowRuns from "../../WorkflowRuns.ts";
import { WorkflowRepeat } from "../../workflowRun.ts";

const dependencies = [
McpInvocationContext.McpInvocationContext,
Expand Down Expand Up @@ -128,15 +131,34 @@ const TaskCancelTool = Tool.make("task_cancel", {
const workflowDependencies = [...dependencies, WorkflowRuns.WorkflowRuns];

// Stages advertise only their outline to keep the tool list small; the service decodes the full spec.
const WorkflowAgentStage = Schema.Struct({
id: Schema.String,
agents: Schema.Array(Schema.Unknown),
});
const WorkflowRepeatInput = Schema.Struct({
...WorkflowRepeat.fields,
stages: Schema.Array(WorkflowAgentStage),
judge: Schema.String,
until: Schema.Struct({ path: Schema.String, equals: Schema.Unknown }),
}).pipe(
Schema.catchDecoding(() =>
Effect.fail(
new SchemaIssue.InvalidValue({
message:
'repeat is {stages:[{id, agents:[…]}], judge:"agent-id", until:{path, equals}, max:1..10, onExhausted?:"fail"|"pass"}',
}),
),
),
);
const WorkflowStage = Schema.Struct({
id: Schema.String,
agents: Schema.optional(Schema.Array(Schema.Unknown)),
repeat: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
repeat: Schema.optional(WorkflowRepeatInput),
}).annotate({ message: "stages[] entries are objects: {id, agents:[…]} or {id, repeat:{…}}" });

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.",
"Ordered stages; parallel agent objects {id,task,target?,resultSchema?}. Wakes on completion. Judge: last-stage agent id. Tasks insert {{stage.agent}}, {{feedback}}.",
parameters: Schema.Struct({
name: TrimmedNonEmptyString,
stages: Schema.Array(WorkflowStage),
Expand All @@ -155,7 +177,7 @@ const WorkflowRunTool = Tool.make("t3_workflow_run", {
.annotate(Tool.OpenWorld, true);

const WorkflowStatusTool = Tool.make("t3_workflow_status", {
description: "Read a `t3_workflow_run` and each agent's result.",
description: "Results.",
parameters: Schema.Struct({ subagentId: NodeId }),
success: Schema.Unknown,
failure: OrchestratorMcpFailure,
Expand Down
21 changes: 11 additions & 10 deletions apps/server/src/mcp/workflowRun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,19 @@ export type WorkflowAgent = typeof WorkflowAgent.Type;
const AgentStage = Schema.Struct({ id: WorkflowId, agents: Schema.NonEmptyArray(WorkflowAgent) });
type AgentStage = typeof AgentStage.Type;

export const WorkflowRepeat = Schema.Struct({
stages: Schema.NonEmptyArray(AgentStage),
judge: WorkflowId,
until: Schema.Struct({
path: TrimmedNonEmptyString,
equals: Schema.Union([Schema.String, Schema.Number, Schema.Boolean, Schema.Null]),
}),
max: Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 10 })),
onExhausted: Schema.optional(Schema.Literals(["fail", "pass"])),
});
const RepeatStage = Schema.Struct({
id: WorkflowId,
repeat: Schema.Struct({
stages: Schema.NonEmptyArray(AgentStage),
judge: WorkflowId,
until: Schema.Struct({
path: TrimmedNonEmptyString,
equals: Schema.Union([Schema.String, Schema.Number, Schema.Boolean, Schema.Null]),
}),
max: Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 10 })),
onExhausted: Schema.optional(Schema.Literals(["fail", "pass"])),
}),
repeat: WorkflowRepeat,
});
type RepeatStage = typeof RepeatStage.Type;

Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/provider/agentContextBudget.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"claudeSystemAppendBytes": 5135,
"codexAdditionalContextBytes": 6865,
"mcpToolListJsonBytes": 46990
"mcpToolListJsonBytes": 47103
}
Loading
Loading