feat(web): show Claude workflow phases and members in Lineage (#12598)

Co-authored-by: Julius Marminge <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: GPT-6.1-Sol (Codex) <[email protected]>
Co-authored-by: maria-rcks <[email protected]>
This commit is contained in:
authored and GitHub committed 2026-10-10 21:34:02 -07:00
1 parent a908a7ac87
commit b9afba59f4
56 files changed
+7216 -661

No files matched your search

@@ -8,6 +8,7 @@ import {
ProviderInstanceId,
ThreadId,
AuthOrchestrationOperateScope,
ORCHESTRATION_V2_WS_METHODS,
AuthOrchestrationReadScope,
AuthSourceControlWriteScope,
AuthPreviewOperateScope,
@@ -35,6 +36,12 @@ describe("RPC authorization scopes", () => {
expect(new Set(Object.keys(RPC_REQUIRED_SCOPES))).toEqual(new Set(WsRpcGroup.requests.keys()));
});
it("requires operate access to stop a workflow", () => {
expect(requiredScopeForRpcMethod(ORCHESTRATION_V2_WS_METHODS.stopWorkflow)).toBe(
AuthOrchestrationOperateScope,
);
});
it("authorizes background policy reporting and observation deliberately", () => {
expect(requiredScopeForRpcMethod(WS_METHODS.serverReportClientActivity)).toBe(
AuthOrchestrationReadScope,
+1
View File
@@ -46,6 +46,7 @@ export const RPC_REQUIRED_SCOPES = {
...CLIENT_GUARDED_RPC_SCOPES,
[ORCHESTRATION_V2_WS_METHODS.dispatchCommand]: AuthOrchestrationOperateScope,
[ORCHESTRATION_V2_WS_METHODS.getWorkflowScript]: AuthOrchestrationReadScope,
[ORCHESTRATION_V2_WS_METHODS.stopWorkflow]: AuthOrchestrationOperateScope,
[ORCHESTRATION_V2_WS_METHODS.getTurnDiff]: AuthOrchestrationReadScope,
[ORCHESTRATION_V2_WS_METHODS.getFullThreadDiff]: AuthOrchestrationReadScope,
[ORCHESTRATION_V2_WS_METHODS.searchThreads]: AuthOrchestrationReadScope,
@@ -879,6 +879,7 @@ function timelineItem(input: {
visibility: input.row.visibility,
sourceThreadId: input.row.sourceThreadId,
itemId: input.row.sourceItemId,
...(input.row.item.type === "subagent" ? { subagentId: input.row.item.subagentId } : {}),
runId: input.row.item.runId,
messageId,
createdBy: message?.createdBy ?? null,
+132 -21
View File
@@ -8,9 +8,14 @@ import {
ChatImageAttachment,
CommandId,
EnvironmentId,
NodeId,
OrchestratorMcpThreadReadResult,
ProviderDriverKind,
ProviderInstanceId,
RunId,
ThreadId,
TurnItemId,
type OrchestrationV2ProjectedTurnItem,
type OrchestrationV2ThreadShell,
} from "@t3tools/contracts";
import * as Effect from "effect/Effect";
@@ -69,6 +74,7 @@ import {
import { htmlRenderFromToolItem } from "@t3tools/shared/toolOutput";
const decodeMcpAttachmentInput = Schema.decodeUnknownEffect(McpAttachmentInput);
const decodeThreadReadResult = Schema.decodeUnknownEffect(OrchestratorMcpThreadReadResult);
// Registration asks for every service the thread tools declare; these cases call none that use them.
const layerThreadToolkit = McpHttpServer.layerThreadToolkit.pipe(
@@ -663,16 +669,127 @@ function scheduledTask(id: string, runtimeMode: "auto" | "full-access"): never {
} as never;
}
it.effect("a caller cannot interrupt a thread that runs above its own modes", () =>
Effect.gen(function* () {
it.effect.each(["t3_thread_interrupt", "t3_workflow_stop"])(
"a caller cannot use %s on a thread that runs above its own modes",
(toolName) =>
Effect.gen(function* () {
const server = yield* McpServer.McpServer;
const result = yield* server
.callTool({
name: toolName,
arguments: {
threadId: "full-access-thread",
...(toolName === "t3_workflow_stop" ? { subagentId: "workflow-coordinator" } : {}),
},
})
.pipe(
Effect.provideService(McpInvocationContext.McpInvocationContext, clientScope("auto")),
Effect.provideService(McpSchema.McpServerClient, client),
);
expect(declaredFailure(result)).toMatchObject({ code: "runtime_mode_escalation_denied" });
}).pipe(
Effect.provide(
McpHttpServer.layerOrchestratorToolkit.pipe(
Layer.provideMerge(McpServer.McpServer.layer),
Layer.provide(NodeCrypto.layer),
Layer.provide(
Layer.mock(ThreadManagement.ThreadManagementService)({
getThreadShell: () =>
Effect.succeed({
projectId: "project-a",
deletedAt: null,
runtimeMode: "full-access",
interactionMode: "default",
} as never),
getProjectThreadRecords: () =>
Effect.succeed({
thread: {
id: ThreadId.make("full-access-thread"),
projectId: "project-a",
runtimeMode: "full-access",
interactionMode: "default",
deletedAt: null,
},
runs: [],
} as never),
stopWorkflow: () => Effect.die("workflow stop must not dispatch above the ceiling"),
interruptThread: () => Effect.die("interrupt must not dispatch above the ceiling"),
}),
),
Layer.provide(Layer.mock(ProviderRegistry.ProviderRegistry)({})),
Layer.provide(Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({})),
Layer.provide(Layer.mock(ScheduledTaskService.ScheduledTaskService)({})),
Layer.provide(Layer.mock(ProjectService.ProjectService)({})),
Layer.provide(Layer.mock(SecretRequests.SecretRequests)({})),
),
),
),
);
it.effect("discovers workflow stop targets and requires orchestration capability", () => {
const threadId = ThreadId.make("workflow-parent");
const shell = McpToolAccessTestkit.liveThreadShell(threadId);
const subagentId = NodeId.make("workflow-coordinator");
const itemId = TurnItemId.make("workflow-coordinator-activity");
const row: OrchestrationV2ProjectedTurnItem = {
position: 0,
visibility: "local",
sourceThreadId: threadId,
sourceItemId: itemId,
item: {
id: itemId,
threadId,
runId: shell.activeRunId,
nodeId: subagentId,
providerThreadId: null,
providerTurnId: null,
nativeItemRef: null,
parentItemId: null,
ordinal: 0,
status: "running",
title: "Workflow: checkout review",
startedAt: shell.createdAt,
completedAt: null,
updatedAt: shell.updatedAt,
type: "subagent",
subagentId,
origin: "provider_native",
driver: ProviderDriverKind.make("claudeAgent"),
providerInstanceId: ProviderInstanceId.make("claudeAgent"),
childThreadId: ThreadId.make("workflow-child"),
prompt: "Review checkout",
result: null,
},
};
const stopped: Array<{ readonly threadId: ThreadId; readonly subagentId: string }> = [];
return Effect.gen(function* () {
const server = yield* McpServer.McpServer;
const result = yield* server
.callTool({ name: "t3_thread_interrupt", arguments: { threadId: "full-access-thread" } })
const scope = clientScope("full-access");
const read = yield* server
.callTool({ name: "t3_thread_read", arguments: { threadId, view: "activity" } })
.pipe(
Effect.provideService(McpInvocationContext.McpInvocationContext, clientScope("auto")),
Effect.provideService(McpInvocationContext.McpInvocationContext, scope),
Effect.provideService(McpSchema.McpServerClient, client),
);
expect(declaredFailure(result)).toMatchObject({ code: "runtime_mode_escalation_denied" });
expect(read.isError).toBe(false);
const result = yield* decodeThreadReadResult(read.structuredContent);
expect(result.items[0]?.subagentId).toBe(subagentId);
const call = (invocation: McpInvocationContext.McpInvocationScope) =>
server
.callTool({
name: "t3_workflow_stop",
arguments: { threadId, subagentId: result.items[0]?.subagentId },
})
.pipe(
Effect.provideService(McpInvocationContext.McpInvocationContext, invocation),
Effect.provideService(McpSchema.McpServerClient, client),
);
const denied = yield* call({ ...scope, capabilities: new Set(["worktree"]) });
expect(declaredFailure(denied)).toMatchObject({ code: "capability_denied" });
expect(stopped).toEqual([]);
const allowed = yield* call(scope);
expect(allowed.isError).toBe(false);
expect(stopped).toEqual([{ threadId: "workflow-parent", subagentId: "workflow-coordinator" }]);
}).pipe(
Effect.provide(
McpHttpServer.layerOrchestratorToolkit.pipe(
@@ -680,20 +797,14 @@ it.effect("a caller cannot interrupt a thread that runs above its own modes", ()
Layer.provide(NodeCrypto.layer),
Layer.provide(
Layer.mock(ThreadManagement.ThreadManagementService)({
getThreadShell: () =>
Effect.succeed({ projectId: "project-a", deletedAt: null } as never),
getThreadShell: (id) => Effect.succeed(McpToolAccessTestkit.liveThreadShell(id)),
getProjectThreadRecords: () =>
Effect.succeed({
thread: {
id: ThreadId.make("full-access-thread"),
projectId: "project-a",
runtimeMode: "full-access",
interactionMode: "default",
deletedAt: null,
},
runs: [],
} as never),
interruptThread: () => Effect.die("interrupt must not dispatch above the ceiling"),
Effect.succeed(McpToolAccessTestkit.idleThreadProjection(shell)),
getTimelinePage: () => Effect.succeed({ items: [row], totalItems: 1, hasMore: false }),
stopWorkflow: (input) =>
Effect.sync(() => {
stopped.push(input);
}),
}),
),
Layer.provide(Layer.mock(ProviderRegistry.ProviderRegistry)({})),
@@ -703,8 +814,8 @@ it.effect("a caller cannot interrupt a thread that runs above its own modes", ()
Layer.provide(Layer.mock(SecretRequests.SecretRequests)({})),
),
),
),
);
);
});
it.effect("only the caller that prepared a pending upload can discard it", () =>
Effect.gen(function* () {
@@ -1,6 +1,8 @@
import { OrchestratorMcpFailure } from "@t3tools/contracts";
import { OrchestratorToolkit } from "./tools.ts";
import * as Effect from "effect/Effect";
import { readCaller } from "../../threadAccess.ts";
import * as McpInvocationContext from "../../McpInvocationContext.ts";
import * as McpToolAccess from "../../McpToolAccess.ts";
import * as OrchestratorMcpService from "../../OrchestratorMcpService.ts";
@@ -35,6 +37,26 @@ const handlers = {
return yield* service.cancelTask(scope, input);
}),
),
t3_workflow_stop: McpToolAccess.writesThreads(
(input) => [input.threadId],
(input) =>
Effect.gen(function* () {
const { threads } = yield* readCaller();
return yield* threads.stopWorkflow(input).pipe(
Effect.as({}),
Effect.mapError(
(cause) =>
new OrchestratorMcpFailure({
code:
cause.reason === "unavailable" || cause.reason === "stop-failed"
? "orchestration_error"
: "invalid_request",
message: cause.message,
}),
),
);
}),
),
schedule_task: McpToolAccess.startsThreads(
// A scheduled task runs with the caller's own modes.
() => ({}),
@@ -1,5 +1,6 @@
import {
OrchestratorMcpCapabilitiesResult,
OrchestrationV2StopWorkflowInput,
OrchestratorMcpCreateThreadsInput,
OrchestratorMcpCreateThreadsResult,
OrchestratorMcpDelegateTaskInput,
@@ -31,6 +32,7 @@ import {
ThreadMetadataMcpUpdateResult,
} from "@t3tools/contracts";
import { Tool, Toolkit } from "effect/ai";
import * as Schema from "effect/Schema";
import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
import * as McpInvocationContext from "../../McpInvocationContext.ts";
@@ -100,6 +102,18 @@ const TaskCancelTool = Tool.make("task_cancel", {
.annotate(Tool.Title, "Cancel delegated task")
.annotate(Tool.Destructive, true);
const WorkflowStopTool = Tool.make("t3_workflow_stop", {
description:
"Stop one running Claude workflow in a T3 thread. Use the workflow coordinator's subagentId returned by t3_thread_read with view='activity'. Stops the whole workflow, including its phases and agents. Does not stop the parent thread or other workflows. Individual workflow phases and agents cannot be stopped separately.",
parameters: OrchestrationV2StopWorkflowInput,
success: Schema.Struct({}),
failure: OrchestratorMcpFailure,
failureMode: "return",
dependencies,
})
.annotate(Tool.Title, "Stop a workflow")
.annotate(Tool.Destructive, true);
export const ScheduleTaskTool = Tool.make("schedule_task", {
description:
"Create persistent work in the app scheduler that runs even when no turn is active. Pass schedule as a STRUCTURED OBJECT, never JSON text. Timers: {type:'interval', everyMs:3600000} is hourly; {type:'fixed_time', timeOfDay:'09:00', weekdays:[1,2,3,4,5]} is weekday mornings; report the returned nextRunAt. Webhooks: {type:'webhook'} runs once per request to a generated URL. The run sees the request ONLY through prompt placeholders: {{body.path}} (e.g. {{body.action}}, {{body.release.tag_name}}), {{headers.name}}, {{query.name}}, {{body}}, or {{request}} (method, headers with credentials redacted, and body). For a sender that signs requests, first call request_secret so the user enters the secret privately (never ask for it in chat or invent one), then set signature with the returned secretRef, e.g. GitHub: {type:'webhook', signature:{header:'x-hub-signature-256', encoding:'hex', prefix:'sha256=', secretRef}}. The result's webhookUrl is the public URL to give the user; if it is absent, this environment has no T3 Connect managed tunnel, so tell the user to enable T3 Connect remote access rather than sharing a path. Omit projectId for this thread's project. In this thread's project, runs post into THIS thread by default (bindToCurrentThread=true), which suits an orchestrator that sees every trigger, delegates work, and can dedupe against what is in flight; use false only when the user wants a fresh top-level thread per run. Elsewhere each run launches a fresh thread. Provider, model, and runtime settings inherit from this thread, or from the project default when there is no calling thread.",
@@ -261,6 +275,7 @@ export const OrchestratorToolkit = Toolkit.make(
DelegateTaskTool,
TaskStatusTool,
TaskCancelTool,
WorkflowStopTool,
ScheduleTaskTool,
ListScheduledTasksTool,
UpdateScheduledTaskTool,
@@ -17,6 +17,7 @@ type WsRpcMethod = RpcGroup.Rpcs<typeof WsRpcGroup>["_tag"];
const RPC_AGGREGATES = {
[ORCHESTRATION_V2_WS_METHODS.dispatchCommand]: "orchestrationV2",
[ORCHESTRATION_V2_WS_METHODS.getWorkflowScript]: "orchestration",
[ORCHESTRATION_V2_WS_METHODS.stopWorkflow]: "orchestration",
[ORCHESTRATION_V2_WS_METHODS.getTurnItem]: "orchestration",
[ORCHESTRATION_V2_WS_METHODS.getTurnDiff]: "orchestration",
[ORCHESTRATION_V2_WS_METHODS.getFullThreadDiff]: "orchestration",
File diff suppressed because it is too large. Load diff
@@ -17,8 +17,8 @@ import {
type ProviderReplayTranscript,
} from "@t3tools/contracts";
import * as Cause from "effect/Cause";
import * as Duration from "effect/Duration";
import * as Crypto from "effect/Crypto";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Layer from "effect/Layer";
@@ -242,6 +242,7 @@ interface ClaudeSubagentFoundFrame {
}
type ClaudeOutboundFrame =
| { readonly type: "query.stop_task"; readonly taskId: string }
| ClaudeQueryOpenFrame
| ClaudePromptOfferFrame
| ClaudeQuerySetModelFrame
@@ -825,6 +826,10 @@ function makeReplayQueryRunner(
mode,
});
}),
stopTask: (taskId) =>
replayEffect(() => {
assertNextOutboundFrame({ type: "query.stop_task", taskId });
}),
interrupt: replayEffect(() => {
assertNextOutboundFrame({ type: "query.interrupt" });
}),
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,80 @@
import * as ClaudeSdk from "@anthropic-ai/claude-agent-sdk";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { assert, it } from "@effect/vitest";
import { ProviderSessionId, ThreadId } from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Layer from "effect/Layer";
import { vi } from "vite-plus/test";
import { ProviderEventLoggers } from "@t3tools/provider-core/server/ProviderEventLoggers";
import {
CLAUDE_AGENT_SDK_QUERY_PROTOCOL,
ClaudeAgentSdkQueryRunner,
layerQueryRunner,
} from "./ClaudeAdapterV2.ts";
vi.mock("@anthropic-ai/claude-agent-sdk", { spy: true });
it.effect("logs successful task stops for replay and omits failed stops", () =>
Effect.gen(function* () {
const writes: unknown[] = [];
const stopTask = vi.fn(async (_taskId: string) => {});
const query = vi.spyOn(ClaudeSdk, "query").mockReturnValue({
stopTask,
} as unknown as ClaudeSdk.Query);
yield* Effect.addFinalizer(() => Effect.sync(() => query.mockRestore()));
const threadId = ThreadId.make("thread-stop");
const providerSessionId = ProviderSessionId.make("session-stop");
yield* Effect.gen(function* () {
const runner = yield* ClaudeAgentSdkQueryRunner;
const session = yield* runner.open({
threadId,
providerSessionId,
options: {
sessionId: "native-session-stop",
model: "claude-sonnet-4-6",
tools: [],
permissionMode: "default",
},
});
writes.length = 0;
yield* session.stopTask("workflow-task");
assert.deepEqual(stopTask.mock.calls, [["workflow-task"]]);
assert.deepEqual(writes, [
{
provider: "claudeAgent",
protocol: CLAUDE_AGENT_SDK_QUERY_PROTOCOL,
kind: "protocol",
providerSessionId,
event: {
direction: "outgoing",
stage: "decoded",
payload: { type: "query.stop_task", taskId: "workflow-task" },
},
},
]);
stopTask.mockRejectedValueOnce(new Error("Stop failed"));
assert.isTrue(Exit.isFailure(yield* Effect.exit(session.stopTask("failed-task"))));
assert.equal(writes.length, 1);
}).pipe(
Effect.provide(
layerQueryRunner.pipe(
Layer.provide(
Layer.succeed(ProviderEventLoggers, {
native: {
filePath: "/tmp/events.log",
write: (event) =>
Effect.sync(() => {
writes.push(event);
}),
close: () => Effect.void,
},
canonical: undefined,
}),
),
),
),
);
}).pipe(Effect.provide(NodeServices.layer)),
);
@@ -4975,17 +4975,17 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
driver: CODEX_PROVIDER,
node: artifacts.node,
});
yield* emitProviderEvent({
type: "turn_item.updated",
driver: CODEX_PROVIDER,
turnItem: artifacts.turnItem,
});
yield* emitProviderEvent({
type: "runtime_request.updated",
driver: CODEX_PROVIDER,
threadId: artifacts.node.threadId,
runtimeRequest: artifacts.request,
});
yield* emitProviderEvent({
type: "turn_item.updated",
driver: CODEX_PROVIDER,
turnItem: artifacts.turnItem,
});
yield* Ref.update(finalAnswerItemIdsByTurn, (current) => {
const ids = current.get(payload.turnId);
if (!ids?.has(payload.item.id)) return current;
@@ -5082,17 +5082,17 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
driver: CODEX_PROVIDER,
node: artifacts.node,
});
yield* emitProviderEvent({
type: "turn_item.updated",
driver: CODEX_PROVIDER,
turnItem: artifacts.turnItem,
});
yield* emitProviderEvent({
type: "runtime_request.updated",
driver: CODEX_PROVIDER,
threadId: artifacts.node.threadId,
runtimeRequest: artifacts.request,
});
yield* emitProviderEvent({
type: "turn_item.updated",
driver: CODEX_PROVIDER,
turnItem: artifacts.turnItem,
});
const resolved = yield* Deferred.await(decision).pipe(
Effect.ensuring(
@@ -5142,17 +5142,17 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
driver: CODEX_PROVIDER,
node: artifacts.node,
});
yield* emitProviderEvent({
type: "turn_item.updated",
driver: CODEX_PROVIDER,
turnItem: artifacts.turnItem,
});
yield* emitProviderEvent({
type: "runtime_request.updated",
driver: CODEX_PROVIDER,
threadId: artifacts.node.threadId,
runtimeRequest: artifacts.request,
});
yield* emitProviderEvent({
type: "turn_item.updated",
driver: CODEX_PROVIDER,
turnItem: artifacts.turnItem,
});
const resolved = yield* Deferred.await(decision).pipe(
Effect.ensuring(
@@ -5203,17 +5203,17 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
driver: CODEX_PROVIDER,
node: artifacts.node,
});
yield* emitProviderEvent({
type: "turn_item.updated",
driver: CODEX_PROVIDER,
turnItem: artifacts.turnItem,
});
yield* emitProviderEvent({
type: "runtime_request.updated",
driver: CODEX_PROVIDER,
threadId: artifacts.node.threadId,
runtimeRequest: artifacts.request,
});
yield* emitProviderEvent({
type: "turn_item.updated",
driver: CODEX_PROVIDER,
turnItem: artifacts.turnItem,
});
const resolved = yield* Deferred.await(decision).pipe(
Effect.ensuring(
@@ -5288,17 +5288,17 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
driver: CODEX_PROVIDER,
node: artifacts.node,
});
yield* emitProviderEvent({
type: "turn_item.updated",
driver: CODEX_PROVIDER,
turnItem: artifacts.turnItem,
});
yield* emitProviderEvent({
type: "runtime_request.updated",
driver: CODEX_PROVIDER,
threadId: artifacts.node.threadId,
runtimeRequest: artifacts.request,
});
yield* emitProviderEvent({
type: "turn_item.updated",
driver: CODEX_PROVIDER,
turnItem: artifacts.turnItem,
});
const resolved = yield* Deferred.await(decision).pipe(
Effect.ensuring(
@@ -5347,17 +5347,17 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
driver: CODEX_PROVIDER,
node: artifacts.node,
});
yield* emitProviderEvent({
type: "turn_item.updated",
driver: CODEX_PROVIDER,
turnItem: artifacts.turnItem,
});
yield* emitProviderEvent({
type: "runtime_request.updated",
driver: CODEX_PROVIDER,
threadId: artifacts.node.threadId,
runtimeRequest: artifacts.request,
});
yield* emitProviderEvent({
type: "turn_item.updated",
driver: CODEX_PROVIDER,
turnItem: artifacts.turnItem,
});
const resolved = yield* Deferred.await(decision).pipe(
Effect.ensuring(
@@ -5407,17 +5407,17 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
driver: CODEX_PROVIDER,
node: artifacts.node,
});
yield* emitProviderEvent({
type: "turn_item.updated",
driver: CODEX_PROVIDER,
turnItem: artifacts.turnItem,
});
yield* emitProviderEvent({
type: "runtime_request.updated",
driver: CODEX_PROVIDER,
threadId: artifacts.node.threadId,
runtimeRequest: artifacts.request,
});
yield* emitProviderEvent({
type: "turn_item.updated",
driver: CODEX_PROVIDER,
turnItem: artifacts.turnItem,
});
const resolved = yield* Deferred.await(decision).pipe(
Effect.ensuring(
@@ -5465,17 +5465,17 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
driver: CODEX_PROVIDER,
node: artifacts.node,
});
yield* emitProviderEvent({
type: "turn_item.updated",
driver: CODEX_PROVIDER,
turnItem: artifacts.turnItem,
});
yield* emitProviderEvent({
type: "runtime_request.updated",
driver: CODEX_PROVIDER,
threadId: artifacts.node.threadId,
runtimeRequest: artifacts.request,
});
yield* emitProviderEvent({
type: "turn_item.updated",
driver: CODEX_PROVIDER,
turnItem: artifacts.turnItem,
});
const resolved = yield* Deferred.await(answers).pipe(
Effect.ensuring(
@@ -0,0 +1,318 @@
import { describe, expect, it } from "vite-plus/test";
import {
mergeClaudeWorkflowProgress,
parseClaudeWorkflowRunHandles,
} from "./claudeWorkflowProgress.ts";
const startFrame = {
type: "system",
subtype: "task_started",
task_id: "w1",
task_type: "local_workflow",
workflow_name: "probe-wf",
};
const snapshotFrame = (
entries: ReadonlyArray<Record<string, unknown>>,
usage?: Record<string, number>,
) => ({
type: "system",
subtype: "task_progress",
task_id: "w1",
description: "Alpha: alpha:one",
workflow_progress: entries,
...(usage === undefined ? {} : { usage }),
});
const phase = (index: number, title: string) => ({ type: "workflow_phase", index, title });
const agent = (overrides: Record<string, unknown>) => ({
type: "workflow_agent",
index: 1,
label: "alpha:one",
state: "start",
phaseIndex: 1,
phaseTitle: "Alpha",
...overrides,
});
describe("mergeClaudeWorkflowProgress", () => {
it("marks a coordinator as a workflow before any member exists", () => {
const workflow = mergeClaudeWorkflowProgress({
previous: undefined,
name: "probe-wf",
message: startFrame,
});
expect(workflow).toEqual({ name: "probe-wf", phases: [], agents: [] });
});
it("leaves an ordinary subagent's frames alone", () => {
expect(
mergeClaudeWorkflowProgress({
previous: undefined,
message: {
type: "system",
subtype: "task_progress",
task_id: "t1",
description: "reading files",
usage: { total_tokens: 500, tool_uses: 2, duration_ms: 90 },
},
}),
).toBeUndefined();
});
it("maps provider agent states and per-member metrics", () => {
const workflow = mergeClaudeWorkflowProgress({
previous: undefined,
message: snapshotFrame([
phase(1, "Alpha"),
agent({
state: "done",
agentId: "a1",
model: "claude-opus-5[1m]",
attempt: 2,
tokens: 71_141,
toolCalls: 3,
durationMs: 1277,
startedAt: 1_700_000_000_000,
queuedAt: 1_699_999_999_000,
promptPreview: "Reply with exactly: A1",
resultPreview: "A1",
}),
agent({ index: 2, label: "alpha:two", state: "queued" }),
]),
});
expect(workflow?.phases).toEqual([{ index: 1, title: "Alpha" }]);
expect(workflow?.agents[0]).toEqual({
index: 1,
label: "alpha:one",
agentId: "a1",
state: "completed",
phaseIndex: 1,
phaseTitle: "Alpha",
model: "claude-opus-5[1m]",
attempt: 2,
totalTokens: 71_141,
toolCalls: 3,
durationMs: 1277,
queuedAt: 1_699_999_999_000,
startedAt: 1_700_000_000_000,
prompt: "Reply with exactly: A1",
result: "A1",
});
expect(workflow?.agents[1]?.state).toBe("queued");
});
it.each(["something-new", "toString", "constructor", "__proto__"])(
"treats unrecognised state %s as still running rather than settled",
(state) => {
const workflow = mergeClaudeWorkflowProgress({
previous: undefined,
message: snapshotFrame([agent({ state })]),
});
expect(workflow?.agents[0]?.state).toBe("running");
},
);
it("keeps the roster through a usage-only frame", () => {
const first = mergeClaudeWorkflowProgress({
previous: undefined,
name: "probe-wf",
message: snapshotFrame([phase(1, "Alpha"), agent({})], {
total_tokens: 100,
tool_uses: 1,
duration_ms: 40,
}),
});
const second = mergeClaudeWorkflowProgress({
previous: first,
message: snapshotFrame([], { total_tokens: 250, tool_uses: 2, duration_ms: 900 }),
});
expect(second?.agents).toHaveLength(1);
expect(second?.phases).toHaveLength(1);
expect(second?.name).toBe("probe-wf");
expect(second?.totalTokens).toBe(250);
});
it("harvests run handles from the launch acknowledgement", () => {
const workflow = mergeClaudeWorkflowProgress({
previous: undefined,
runHandles: parseClaudeWorkflowRunHandles({
status: "async_launched",
taskType: "local_workflow",
runId: "wf_abc",
transcriptDir: "/root/.claude/projects/p/s/subagents/workflows/wf_abc",
scriptPath: "/root/.claude/projects/p/s/workflows/scripts/x.js",
}),
message: startFrame,
});
expect(workflow?.runHandles?.transcriptDir).toBe(
"/root/.claude/projects/p/s/subagents/workflows/wf_abc",
);
expect(workflow?.runHandles?.runId).toBe("wf_abc");
});
it("ignores a non-workflow tool acknowledgement", () => {
expect(
parseClaudeWorkflowRunHandles({ status: "async_launched", taskType: "local_agent" }),
).toBeUndefined();
});
it("keeps a phase the plan reached at runtime even if a later frame omits it", () => {
const first = mergeClaudeWorkflowProgress({
previous: undefined,
message: snapshotFrame([phase(1, "Alpha"), phase(2, "Beta"), phase(3, "Gamma")]),
});
const second = mergeClaudeWorkflowProgress({
previous: first,
message: snapshotFrame([phase(1, "Alpha")]),
});
expect(second?.phases.map((entry) => entry.title)).toEqual(["Alpha", "Beta", "Gamma"]);
});
it("keeps other members and phase links when a sparse frame starts a new attempt", () => {
const first = mergeClaudeWorkflowProgress({
previous: undefined,
message: snapshotFrame([
phase(1, "Alpha"),
agent({
state: "error",
attempt: 1,
promptPreview: "First prompt",
resultPreview: "First result",
tokens: 100,
toolCalls: 2,
durationMs: 50,
queuedAt: 1000,
startedAt: 1010,
}),
agent({ index: 2, state: "done" }),
]),
});
const second = mergeClaudeWorkflowProgress({
previous: first,
message: snapshotFrame([
{ type: "workflow_agent", index: 1, label: "alpha:one", state: "start", attempt: 2 },
]),
});
expect(second?.agents).toEqual([
{
index: 1,
label: "alpha:one",
state: "running",
attempt: 2,
phaseIndex: 1,
phaseTitle: "Alpha",
},
first?.agents[1],
]);
expect(second?.phases).toEqual(first?.phases);
});
it("ignores an older attempt after a retry completes", () => {
const first = mergeClaudeWorkflowProgress({
previous: undefined,
message: snapshotFrame([agent({ state: "done", attempt: 2, resultPreview: "New result" })]),
});
const second = mergeClaudeWorkflowProgress({
previous: first,
message: snapshotFrame([agent({ state: "error", attempt: 1, resultPreview: "Old result" })]),
});
expect(second?.agents).toEqual(first?.agents);
});
it.each([
["done", "start"],
["done", "queued"],
["error", "start"],
["error", "queued"],
["start", "queued"],
])("keeps member state when the same attempt regresses from %s to %s", (state, staleState) => {
const first = mergeClaudeWorkflowProgress({
previous: undefined,
message: snapshotFrame([agent({ state, attempt: 2, resultPreview: "Current result" })]),
});
const second = mergeClaudeWorkflowProgress({
previous: first,
message: snapshotFrame([
agent({ state: staleState, attempt: 2, resultPreview: "Old result" }),
]),
});
expect(second?.agents).toEqual(first?.agents);
});
it("retains member fields omitted by a later frame in the same attempt", () => {
const first = mergeClaudeWorkflowProgress({
previous: undefined,
message: snapshotFrame([agent({ attempt: 2, promptPreview: "Current prompt", tokens: 100 })]),
});
const second = mergeClaudeWorkflowProgress({
previous: first,
message: snapshotFrame([agent({ state: "done", resultPreview: "Current result" })]),
});
expect(second?.agents[0]).toMatchObject({
state: "completed",
attempt: 2,
prompt: "Current prompt",
result: "Current result",
totalTokens: 100,
});
});
it("keeps existing run handles when a later acknowledgement supplies only a script path", () => {
const first = mergeClaudeWorkflowProgress({
previous: undefined,
runHandles: { runId: "wf_abc", transcriptDir: "/workflows/wf_abc" },
message: startFrame,
});
const second = mergeClaudeWorkflowProgress({
previous: first,
runHandles: { scriptPath: "/scripts/workflow.js" },
message: startFrame,
});
expect(second?.runHandles).toEqual({
runId: "wf_abc",
transcriptDir: "/workflows/wf_abc",
scriptPath: "/scripts/workflow.js",
});
});
it("preserves prompt and result whitespace", () => {
const workflow = mergeClaudeWorkflowProgress({
previous: undefined,
message: snapshotFrame([
agent({ promptPreview: " indented\n", resultPreview: "\n answer\n" }),
]),
});
expect(workflow?.agents[0]?.prompt).toBe(" indented\n");
expect(workflow?.agents[0]?.result).toBe("\n answer\n");
});
it("drops timestamps outside the DateTime range", () => {
const workflow = mergeClaudeWorkflowProgress({
previous: undefined,
message: snapshotFrame([
agent({ startedAt: 8_640_000_000_000_001, queuedAt: Number.MAX_SAFE_INTEGER }),
agent({ index: 2, startedAt: 8_640_000_000_000_000, queuedAt: 0 }),
]),
});
expect(workflow?.agents[0]).not.toHaveProperty("startedAt");
expect(workflow?.agents[0]).not.toHaveProperty("queuedAt");
expect(workflow?.agents[1]?.startedAt).toBe(8_640_000_000_000_000);
expect(workflow?.agents[1]?.queuedAt).toBe(0);
});
it("drops malformed entries instead of failing the frame", () => {
const workflow = mergeClaudeWorkflowProgress({
previous: undefined,
message: snapshotFrame([
{ type: "workflow_phase", title: "No index" },
{ type: "workflow_agent", index: 4 },
{ type: "something_else", index: 1, title: "Ignored" },
phase(1, "Alpha"),
]),
});
expect(workflow).toEqual({ phases: [{ index: 1, title: "Alpha" }], agents: [] });
});
});
@@ -0,0 +1,192 @@
import type {
OrchestrationV2SubagentWorkflow,
OrchestrationV2WorkflowAgent,
OrchestrationV2WorkflowPhase,
OrchestrationV2WorkflowRunHandles,
} from "@t3tools/contracts";
import { field, text } from "../unknownField.ts";
export const CLAUDE_WORKFLOW_TASK_TYPE = "local_workflow";
export function hasClaudeWorkflowSnapshot(
message: unknown,
): message is { readonly workflow_progress: ReadonlyArray<unknown> } {
const entries = field(message, "workflow_progress");
return Array.isArray(entries) && entries.length > 0;
}
const AGENT_STATES: Record<string, OrchestrationV2WorkflowAgent["state"] | undefined> = {
queued: "queued",
start: "running",
done: "completed",
error: "failed",
};
function count(value: unknown): number | undefined {
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0 ? value : undefined;
}
function timestamp(value: unknown): number | undefined {
const milliseconds = count(value);
return milliseconds !== undefined && milliseconds <= 8_640_000_000_000_000
? milliseconds
: undefined;
}
function preview(value: unknown): string | undefined {
return typeof value === "string" && value.length > 0 ? value : undefined;
}
function optional<T>(key: string, value: T | undefined): Record<string, T> {
return value === undefined ? {} : ({ [key]: value } as Record<string, T>);
}
function parsePhase(entry: unknown): OrchestrationV2WorkflowPhase | null {
const index = count(field(entry, "index"));
const title = text(field(entry, "title"));
return index === undefined || title === undefined ? null : { index, title };
}
function parseAgent(entry: unknown): OrchestrationV2WorkflowAgent | null {
const index = count(field(entry, "index"));
const label = text(field(entry, "label"));
if (index === undefined || label === undefined) return null;
// An unknown state is in-flight, not settled: guessing "completed" would
// freeze a live row, while guessing "running" self-corrects on the next frame.
const reportedState = text(field(entry, "state")) ?? "";
const state = Object.hasOwn(AGENT_STATES, reportedState)
? (AGENT_STATES[reportedState] ?? "running")
: "running";
return {
index,
label,
state,
...optional("agentId", text(field(entry, "agentId"))),
...optional("phaseIndex", count(field(entry, "phaseIndex"))),
...optional("phaseTitle", text(field(entry, "phaseTitle"))),
...optional("model", text(field(entry, "model"))),
...optional("attempt", count(field(entry, "attempt"))),
...optional("totalTokens", count(field(entry, "tokens"))),
...optional("toolCalls", count(field(entry, "toolCalls"))),
...optional("durationMs", count(field(entry, "durationMs"))),
...optional("queuedAt", timestamp(field(entry, "queuedAt"))),
...optional("startedAt", timestamp(field(entry, "startedAt"))),
...optional("prompt", preview(field(entry, "promptPreview"))),
...optional("result", preview(field(entry, "resultPreview"))),
};
}
/**
* Reads the Workflow tool's structured result. A background launch answers the
* model with an acknowledgement rather than the run's output, and that
* acknowledgement is the only place the run's filesystem handles appear —
* nothing later on the event stream repeats them.
*/
export function parseClaudeWorkflowRunHandles(
toolUseResult: unknown,
): OrchestrationV2WorkflowRunHandles | undefined {
if (text(field(toolUseResult, "taskType")) !== CLAUDE_WORKFLOW_TASK_TYPE) return undefined;
const handles = {
...optional("runId", text(field(toolUseResult, "runId"))),
...optional("transcriptDir", text(field(toolUseResult, "transcriptDir"))),
...optional("scriptPath", text(field(toolUseResult, "scriptPath"))),
};
return Object.keys(handles).length === 0 ? undefined : handles;
}
function parseClaudeWorkflowProgress(message: unknown): {
readonly phases: ReadonlyArray<OrchestrationV2WorkflowPhase>;
readonly agents: ReadonlyArray<OrchestrationV2WorkflowAgent>;
} | null {
if (!hasClaudeWorkflowSnapshot(message)) return null;
const phases: OrchestrationV2WorkflowPhase[] = [];
const agents: OrchestrationV2WorkflowAgent[] = [];
for (const entry of message.workflow_progress) {
const type = text(field(entry, "type"));
if (type === "workflow_phase") {
const phase = parsePhase(entry);
if (phase !== null) phases.push(phase);
} else if (type === "workflow_agent") {
const agent = parseAgent(entry);
if (agent !== null) agents.push(agent);
}
}
return phases.length === 0 && agents.length === 0 ? null : { phases, agents };
}
/**
* Folds one frame into the coordinator's workflow state. A frame without a
* snapshot still carries usage, so totals keep climbing between snapshots.
* A phase only ever joins the plan — the provider re-sends the full plan on
* every snapshot, but a late frame that omits an already-seen phase must not
* shorten the arc the user is looking at.
*
* Returns undefined when nothing identifies the task as a workflow. Progress
* frames do not repeat `task_type`, so this is the single place that decides
* whether a task is a coordinator; without it every ordinary subagent's usage
* frame would mint an empty workflow and render as a coordinator.
*/
export function mergeClaudeWorkflowProgress(input: {
readonly previous: OrchestrationV2SubagentWorkflow | undefined;
readonly name?: string | undefined;
readonly runHandles?: OrchestrationV2WorkflowRunHandles | undefined;
readonly message: unknown;
}): OrchestrationV2SubagentWorkflow | undefined {
const { previous } = input;
const snapshot = parseClaudeWorkflowProgress(input.message);
if (
previous === undefined &&
input.name === undefined &&
input.runHandles === undefined &&
snapshot === null
) {
return undefined;
}
const reported = field(input.message, "usage");
const phasesByIndex = new Map(
(previous?.phases ?? []).map((phase) => [phase.index, phase] as const),
);
for (const phase of snapshot?.phases ?? []) phasesByIndex.set(phase.index, phase);
const agentsByIndex = new Map(
(previous?.agents ?? []).map((agent) => [agent.index, agent] as const),
);
for (const agent of snapshot?.agents ?? []) {
const prior = agentsByIndex.get(agent.index);
const attempt = agent.attempt ?? prior?.attempt ?? 1;
if (prior !== undefined && attempt < (prior.attempt ?? 1)) continue;
const restarted = prior !== undefined && attempt > (prior.attempt ?? 1);
const regressed =
!restarted &&
(((prior?.state === "completed" || prior?.state === "failed") &&
agent.state !== prior.state) ||
(prior?.state === "running" && agent.state === "queued"));
agentsByIndex.set(
agent.index,
regressed
? { ...agent, ...prior }
: {
...(restarted
? {
...optional("phaseIndex", prior.phaseIndex),
...optional("phaseTitle", prior.phaseTitle),
}
: prior),
...agent,
},
);
}
const runHandles =
input.runHandles === undefined
? previous?.runHandles
: { ...previous?.runHandles, ...input.runHandles };
return {
...optional("name", input.name ?? previous?.name),
...optional("runHandles", runHandles),
phases: Array.from(phasesByIndex.values()).sort((left, right) => left.index - right.index),
agents: Array.from(agentsByIndex.values()).sort((left, right) => left.index - right.index),
...optional("totalTokens", count(field(reported, "total_tokens")) ?? previous?.totalTokens),
...optional("toolCalls", count(field(reported, "tool_uses")) ?? previous?.toolCalls),
...optional("durationMs", count(field(reported, "duration_ms")) ?? previous?.durationMs),
};
}
@@ -42,10 +42,12 @@ const stopEarlierBackgroundWork = ({
stopWithQueue,
olderStart = false,
stalledRun,
workflowMember = false,
}: {
readonly failedStart?: boolean;
readonly stopWithQueue?: "thread.stop" | "run.interrupt";
readonly olderStart?: boolean;
readonly workflowMember?: boolean;
readonly stalledRun?:
| "missing-session"
| "missing-session-terminal"
@@ -488,23 +490,27 @@ const stopEarlierBackgroundWork = ({
events: [
// A subagent the reviewer started, which the provider records on the
// reviewer's own thread.
{
id: EventId.make("nested-subagent-item"),
type: "turn-item.updated",
threadId: reviewerThreadId,
occurredAt: now,
payload: {
...nested,
id: nestedItemId,
nodeId: nestedSubagentId,
providerTurnId: null,
nativeItemRef: null,
parentItemId: null,
ordinal: 1,
type: "subagent",
subagentId: nestedSubagentId,
},
},
...(workflowMember
? []
: [
{
id: EventId.make("nested-subagent-item"),
type: "turn-item.updated",
threadId: reviewerThreadId,
occurredAt: now,
payload: {
...nested,
id: nestedItemId,
nodeId: nestedSubagentId,
providerTurnId: null,
nativeItemRef: null,
parentItemId: null,
ordinal: 1,
type: "subagent",
subagentId: nestedSubagentId,
},
} as const,
]),
{
id: EventId.make("nested-subagent"),
type: "subagent.updated",
@@ -515,12 +521,36 @@ const stopEarlierBackgroundWork = ({
id: nestedSubagentId,
parentNodeId: reviewerRootId,
createdBy: "agent",
nativeTaskRef: null,
nativeTaskRef: workflowMember
? { driver, nativeId: "workflow:member", strength: "strong" }
: null,
model: null,
},
},
],
});
if (workflowMember) {
// Workflow members have execution nodes and child roots, but no subagent turn item.
const memberRoot = (yield* orchestrator.getThreadProjection(tester.threadId)).nodes[0]!;
yield* sink.write({
events: [
{
id: EventId.make("workflow-member-node"),
type: "node.updated",
threadId: reviewerThreadId,
occurredAt: now,
payload: {
...memberRoot,
id: nestedSubagentId,
threadId: reviewerThreadId,
parentNodeId: reviewerRootId,
rootNodeId: reviewerRootId,
kind: "subagent",
},
},
],
});
}
// A settled run with its root node, attempt, provider turn and,
// optionally, a command or native subagent it left running.
const settledRun = (input: {
@@ -883,10 +913,18 @@ const stopEarlierBackgroundWork = ({
const reviewerThread = yield* orchestrator.getThreadProjection(reviewerThreadId);
assert.equal(reviewerThread.nodes[0]?.status, "interrupted");
assert.equal(reviewerThread.subagents[0]?.status, "interrupted");
assert.equal(
reviewerThread.turnItems.find((item) => item.id === nestedItemId)?.status,
"interrupted",
);
if (workflowMember) {
assert.isUndefined(reviewerThread.turnItems.find((item) => item.id === nestedItemId));
assert.equal(
reviewerThread.nodes.find((node) => node.id === nestedSubagentId)?.status,
"interrupted",
);
} else {
assert.equal(
reviewerThread.turnItems.find((item) => item.id === nestedItemId)?.status,
"interrupted",
);
}
const testerThread = yield* orchestrator.getThreadProjection(tester.threadId);
assert.equal(testerThread.nodes[0]?.status, "interrupted");
}).pipe(
@@ -905,6 +943,10 @@ it.effect("Stop reaches background work an earlier provider thread still runs",
stopEarlierBackgroundWork({}),
);
it.effect("Stop settles workflow members without subagent turn items", () =>
stopEarlierBackgroundWork({ workflowMember: true }),
);
it.effect(
"Stop reaches earlier background work after the newest run fails before provider start",
() => stopEarlierBackgroundWork({ failedStart: true }),
@@ -158,6 +158,7 @@ it.effect.each(["child completion", "scheduled message", "user steering"] as con
}),
setModel: () => Effect.void,
setPermissionMode: () => Effect.void,
stopTask: () => Effect.void,
interrupt: Effect.die("automatic delivery must never interrupt"),
close: Effect.void,
}),
@@ -8421,7 +8421,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
OrchestrationV2ThreadProjection,
"nodes" | "subagents" | "turnItems"
>;
readonly subagent: Extract<OrchestrationV2TurnItem, { readonly type: "subagent" }>;
readonly subagent: Pick<
Extract<OrchestrationV2TurnItem, { readonly type: "subagent" }>,
"subagentId" | "origin" | "childThreadId" | "providerInstanceId"
>;
}> = [{ projection: input.projection, subagent: item }];
for (const { projection, subagent } of dying) {
const ended = yield* endOrphanedNativeSubagent({
@@ -8466,6 +8469,20 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
for (const nested of child.turnItems) {
if (nested.type === "subagent") dying.push({ projection: child, subagent: nested });
}
// Workflow members have records and child roots without subagent turn items.
for (const nested of child.subagents) {
if (nested.origin === "provider_native" && nested.runId === null) {
dying.push({
projection: child,
subagent: {
subagentId: nested.id,
origin: nested.origin,
childThreadId: nested.childThreadId,
providerInstanceId: nested.providerInstanceId,
},
});
}
}
}
}
for (const providerThread of input.projection.providerThreads) {
@@ -349,6 +349,54 @@ it.effect("memory recovery selection includes unfinished items from missing runs
}).pipe(Effect.provide(ProjectionStore.layerMemory)),
);
const runlessNativeSubagentNeedsRecovery = Effect.gen(function* () {
const store = yield* ProjectionStore.ProjectionStoreV2;
const threadId = yield* addRolledBackRecoveryCandidate("runless-native-recovery");
const now = yield* DateTime.now;
const id = NodeId.make("node:runless-native-recovery");
assert.notInclude(yield* store.getRecoveryThreadIds("runtime"), threadId);
for (const status of ["pending", "running", "waiting", "completed"] as const) {
yield* store.apply({
id: EventId.make(`event:runless-native-recovery:${status}`),
type: "subagent.updated",
threadId,
nodeId: id,
driver,
providerInstanceId,
occurredAt: now,
payload: {
id,
threadId,
runId: null,
parentNodeId: NodeId.make("node:runless-native-parent"),
origin: "provider_native",
createdBy: "agent",
driver,
providerInstanceId,
providerThreadId: null,
childThreadId: null,
nativeTaskRef: { driver, nativeId: "runless-native", strength: "strong" },
prompt: "Do the work",
title: "Workflow member",
model: null,
status,
result: null,
startedAt: now,
completedAt: status === "completed" ? now : null,
updatedAt: now,
},
});
assert.equal(
(yield* store.getRecoveryThreadIds("runtime")).includes(threadId),
status !== "completed",
);
}
});
it.effect("memory recovery selects unfinished runless native subagents", () =>
runlessNativeSubagentNeedsRecovery.pipe(Effect.provide(ProjectionStore.layerMemory)),
);
// Project A holds an active thread, an archived thread and a fork of a run in
// an archived project B thread, so a project-scoped snapshot still has to load
// a fork source that is outside the project and archived.
@@ -501,6 +549,11 @@ it.effect("memory shell snapshots scope to one project", () =>
);
it.layer(layerTest)("ProjectionStoreV2", (it) => {
it.effect(
"SQL recovery selects unfinished runless native subagents",
() => runlessNativeSubagentNeedsRecovery,
);
it.effect(
"keeps restart-cancelled work through a stale run.updated",
() => restartCancelledWorkSurvivesStaleRunUpdate,
@@ -2520,6 +2573,8 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
const now = yield* DateTime.now;
const settledThreadId = ThreadId.make("thread:recovery-candidates:settled");
const runningThreadId = ThreadId.make("thread:recovery-candidates:running");
const memberThreadId = ThreadId.make("thread:recovery-candidates:member");
const childThreadId = ThreadId.make("thread:recovery-candidates:child");
const rolledBackThreadId = yield* addRolledBackRecoveryCandidate("recovery-candidates");
const orphanedThreadId = yield* addOrphanedRecoveryCandidate("recovery-candidates");
const projectId = ProjectId.make("project:recovery-candidates");
@@ -2551,7 +2606,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
deletedAt: null,
});
for (const threadId of [settledThreadId, runningThreadId]) {
for (const threadId of [settledThreadId, runningThreadId, memberThreadId, childThreadId]) {
yield* projectionStore.apply({
id: EventId.make(`event:recovery-candidates:${threadId}:created`),
type: "thread.created",
@@ -2591,11 +2646,118 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
},
});
const memberId = NodeId.make("node:recovery-candidates:member");
const childRootNodeId = NodeId.make("node:recovery-candidates:child-root");
const nativeTaskRef = { driver, nativeId: "workflow:agent:1", strength: "strong" as const };
const memberNode = {
id: memberId,
threadId: memberThreadId,
runId: null,
parentNodeId: NodeId.make("node:recovery-candidates:coordinator-root"),
rootNodeId: NodeId.make("node:recovery-candidates:coordinator-root"),
kind: "subagent" as const,
status: "running" as const,
countsForRun: false,
providerThreadId: null,
providerTurnId: null,
nativeItemRef: nativeTaskRef,
runtimeRequestId: null,
checkpointScopeId: null,
startedAt: now,
completedAt: null,
};
yield* projectionStore.apply({
id: EventId.make("event:recovery-candidates:member"),
type: "subagent.updated",
threadId: memberThreadId,
nodeId: memberId,
driver,
providerInstanceId,
occurredAt: now,
payload: {
id: memberId,
threadId: memberThreadId,
runId: null,
parentNodeId: memberNode.parentNodeId,
origin: "provider_native",
createdBy: "agent",
driver,
providerInstanceId,
providerThreadId: null,
childThreadId,
nativeTaskRef,
prompt: "Do the work",
title: "Member",
model: null,
status: "running",
result: null,
startedAt: now,
completedAt: null,
updatedAt: now,
},
});
for (const [threadId, node] of [
[memberThreadId, memberNode],
[
childThreadId,
{
...memberNode,
id: childRootNodeId,
threadId: childThreadId,
parentNodeId: null,
rootNodeId: childRootNodeId,
kind: "root_turn" as const,
},
],
] as const) {
yield* projectionStore.apply({
id: EventId.make(`event:recovery-candidates:node:${node.id}`),
type: "node.updated",
threadId,
nodeId: node.id,
driver,
occurredAt: now,
payload: node,
});
}
const recoveryThreadIds = yield* projectionStore.getRecoveryThreadIds("runtime");
assert.include(recoveryThreadIds, runningThreadId);
assert.include(recoveryThreadIds, orphanedThreadId);
assert.include(recoveryThreadIds, memberThreadId);
assert.include(recoveryThreadIds, childThreadId);
assert.notInclude(recoveryThreadIds, settledThreadId);
assert.notInclude(recoveryThreadIds, rolledBackThreadId);
const memberRecovery = yield* projectionStore.getRuntimeRecoveryProjection(memberThreadId);
const childRecovery = yield* projectionStore.getRuntimeRecoveryProjection(childThreadId);
assert.deepEqual(
memberRecovery.subagents.map((subagent) => subagent.id),
[memberId],
);
assert.deepEqual(
memberRecovery.nodes.map((node) => node.id),
[memberId],
);
assert.deepEqual(
childRecovery.nodes.map((node) => node.id),
[childRootNodeId],
);
yield* projectionStore.apply({
id: EventId.make("event:recovery-candidates:member-cancelled"),
type: "subagent.updated",
threadId: memberThreadId,
nodeId: memberId,
driver,
providerInstanceId,
occurredAt: now,
payload: {
...memberRecovery.subagents[0]!,
status: "cancelled",
completedAt: now,
updatedAt: now,
},
});
assert.include(yield* projectionStore.getRecoveryThreadIds("runtime"), childThreadId);
}),
);
@@ -1589,6 +1589,90 @@ layer("ProviderEventIngestorV2", (it) => {
}),
);
it.effect("updates provider child metadata without recreating its saved thread", () =>
Effect.gen(function* () {
const now = yield* DateTime.now;
const eventSink = yield* EventSink.EventSinkV2;
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
const ingestor = yield* ProviderEventIngestor.ProviderEventIngestorV2;
const idAllocator = yield* IdAllocator.IdAllocatorV2;
const rootEvent = yield* threadCreatedEvent(now);
if (rootEvent.type !== "thread.created") throw new Error("Expected thread fixture");
const childThreadId = idAllocator.derive.threadFromProviderThread({
driver: CODEX_DRIVER,
nativeThreadId: "workflow-member-metadata",
});
const providerSessionId = yield* idAllocator.allocate.providerSession({
providerInstanceId: modelSelection.instanceId,
threadId: rootEvent.threadId,
});
const offered = {
...rootEvent.payload,
id: childThreadId,
title: "Inspect checkout",
activeProviderThreadId: null,
lineage: {
parentThreadId: rootEvent.threadId,
relationshipToParent: "subagent" as const,
rootThreadId: rootEvent.threadId,
},
};
const ingest = (appThread: OrchestrationV2AppThread) =>
ingestor.ingestNormalized({
providerSessionId,
providerInstanceId: modelSelection.instanceId,
threadId: rootEvent.threadId,
event: { type: "app_thread.created", driver: CODEX_DRIVER, appThread },
});
yield* eventSink.write({ events: [rootEvent] });
const created = yield* Effect.all([ingest(offered), ingest(offered)], {
concurrency: "unbounded",
});
assert.deepEqual(
created.flat().map((stored) => stored.event.type),
["thread.created"],
);
const saved = {
...offered,
archivedAt: now,
deletedAt: now,
activeProviderThreadId: rootEvent.payload.activeProviderThreadId,
branch: "user/checkout",
};
yield* eventSink.write({
events: [
{
...rootEvent,
id: yield* idAllocator.allocate.event({ threadId: childThreadId }),
threadId: childThreadId,
type: "thread.deleted",
payload: saved,
},
],
});
for (const metadata of [
{ title: "Review checkout", modelSelection },
{ title: "Review checkout", modelSelection: { ...modelSelection, model: "gpt-6.1-sol" } },
]) {
const updated = yield* ingest({ ...offered, ...metadata });
assert.deepEqual(
updated.map((stored) => stored.event.type),
["thread.metadata-updated"],
);
const thread = yield* projectionStore.getThread(childThreadId);
assert.deepEqual(thread, { ...saved, ...metadata, updatedAt: thread.updatedAt });
}
assert.deepEqual(
yield* ingest({
...offered,
title: "Review checkout",
modelSelection: { ...modelSelection, model: "gpt-6.1-sol" },
}),
[],
);
}),
);
it.effect("moves a native subagent's thread to the model its provider reports later", () =>
Effect.gen(function* () {
const now = yield* DateTime.now;
@@ -535,14 +535,37 @@ export const layer: Layer.Layer<
const normalize: ProviderEventIngestorV2Shape["normalize"] = (input) =>
Effect.gen(function* () {
switch (input.event.type) {
case "app_thread.created":
case "app_thread.created": {
const offered = input.event.appThread;
const existing = yield* projections.getThread(offered.id).pipe(
Effect.catchTags({
ProjectionStoreThreadNotFoundError: () => Effect.succeed(null),
}),
);
if (
existing !== null &&
existing.title === offered.title &&
modelSelectionsEqual(existing.modelSelection, offered.modelSelection)
)
return [];
const now = yield* DateTime.now;
return [
yield* makeDomainEvent(input, {
type: "thread.created",
threadId: input.event.appThread.id,
payload: input.event.appThread,
type: existing === null ? "thread.created" : "thread.metadata-updated",
threadId: offered.id,
payload:
existing === null
? offered
: {
...existing,
title: offered.title,
modelSelection: offered.modelSelection,
updatedAt: now,
},
occurredAt: now,
}),
];
}
case "provider_session.updated":
return [
yield* makeDomainEvent(input, {
@@ -795,6 +818,10 @@ export const layer: Layer.Layer<
);
}),
),
(effect) =>
input.event.type === "app_thread.created"
? threadCommands.withLock(input.event.appThread.id, effect)
: effect,
),
});
}),
@@ -854,6 +854,97 @@ it.effect(
},
);
it.effect("cancels workflow members and child roots without run ids after restart", () => {
const parentThreadId = ThreadId.make("thread_recovery_workflow_members");
const childThreadId = ThreadId.make("thread_recovery_workflow_member_child");
const memberId = NodeId.make("node_recovery_workflow_member");
const childRootId = NodeId.make("node_recovery_workflow_member_root");
const providerInstanceId = ProviderInstanceId.make("claudeAgent");
const driver = ProviderDriverKind.make("claudeAgent");
const member = {
id: memberId,
threadId: parentThreadId,
runId: null,
origin: "provider_native",
status: "running",
driver,
providerInstanceId,
};
const parentProjection = {
thread: { id: parentThreadId, providerInstanceId },
runs: [],
attempts: [],
nodes: [
{ id: memberId, threadId: parentThreadId, runId: null, kind: "subagent", status: "running" },
],
subagents: [member],
runtimeRequests: [],
providerSessions: [],
providerThreads: [],
providerTurns: [],
messages: [],
turnItems: [],
} as unknown as OrchestrationV2ThreadProjection;
const childProjection = {
...parentProjection,
thread: { id: childThreadId, providerInstanceId },
nodes: [
{
id: childRootId,
threadId: childThreadId,
runId: null,
kind: "root_turn",
status: "running",
},
],
subagents: [],
} as unknown as OrchestrationV2ThreadProjection;
const committed: Array<Parameters<EventSink.EventSinkV2["Service"]["commitCommand"]>[0]> = [];
const layer = ProviderRuntimeRecovery.layer.pipe(
Layer.provide(ServerSettings.layerTest()),
Layer.provide(
Layer.mergeAll(
Layer.mock(ProjectionStore.ProjectionStoreV2)({
getRecoveryThreadIds: () => Effect.succeed([parentThreadId, childThreadId]),
getRuntimeRecoveryProjection: (threadId) =>
Effect.succeed(threadId === parentThreadId ? parentProjection : childProjection),
}),
Layer.mock(EventSink.EventSinkV2)({
commitCommand: (input) => {
committed.push(input);
return Effect.succeed({ committed: true, cancelledEffectCount: 0 } as never);
},
}),
IdAllocator.layer,
Layer.mock(EffectWorker.OrchestrationEffectWorkerV2)({
runRecoveryOnce: Effect.succeed(false),
}),
Layer.mock(EffectOutbox.EffectOutboxV2)({
reconcileAfterProcessLoss: Effect.succeed({ requeued: 0, cancelled: 0 }),
}),
),
),
);
return Effect.gen(function* () {
yield* (yield* ProviderRuntimeRecovery.ProviderRuntimeRecoveryService).reconcile("startup");
assert.deepEqual(
committed.flatMap((command) =>
command.events.map((event) => [
command.threadId,
event.type,
"status" in event.payload ? event.payload.status : null,
]),
),
[
[parentThreadId, "subagent.updated", "cancelled"],
[parentThreadId, "node.updated", "cancelled"],
[childThreadId, "node.updated", "cancelled"],
],
);
}).pipe(Effect.provide(layer));
});
it.effect(
"clears persisted pendingBackgroundTasks and terminalizes stale background items on settled runs",
() => {
@@ -165,7 +165,7 @@ it.effect("routes shared-runtime events only to their owning root run", () =>
}),
);
it("leaves a child thread created after the root turn ended to the run that is live then", () => {
it("routes only enrolled workflow members after the root turn ends", () => {
const threadId = ThreadId.make("thread:late-child");
const rootProviderTurnId = ProviderTurnId.make("provider-turn:late-child");
const identity: RunExecutionService.ProviderEventRouteIdentity = {
@@ -174,14 +174,17 @@ it("leaves a child thread created after the root turn ended to the run that is l
attemptId: RunAttemptId.make("attempt:late-child"),
providerThreadId: ProviderThreadId.make("provider-thread:late-child"),
};
const childCreated = (childThreadId: ThreadId): ProviderAdapter.ProviderAdapterV2Event =>
const childCreated = (
childThreadId: ThreadId,
parentThreadId = threadId,
): ProviderAdapter.ProviderAdapterV2Event =>
({
type: "app_thread.created",
driver,
appThread: {
id: childThreadId,
lineage: {
parentThreadId: threadId,
parentThreadId,
relationshipToParent: "subagent",
rootThreadId: threadId,
},
@@ -200,6 +203,13 @@ it("leaves a child thread created after the root turn ended to the run that is l
initial,
);
assert.isTrue(earlyAccepted);
const existingDescendant = ThreadId.make("thread:late-child:existing-descendant");
const [descendantAccepted, withDescendant] = RunExecutionService.routeProviderEvent(
childCreated(existingDescendant, earlyChild),
identity,
live,
);
assert.isTrue(descendantAccepted);
const [terminalAccepted, ended] = RunExecutionService.routeProviderEvent(
{
type: "turn.terminal",
@@ -212,7 +222,7 @@ it("leaves a child thread created after the root turn ended to the run that is l
threadDisposition: "reusable",
},
identity,
live,
withDescendant,
);
assert.isTrue(terminalAccepted);
// A child the root launched before it ended stays with this run.
@@ -224,6 +234,92 @@ it("leaves a child thread created after the root turn ended to the run that is l
);
assert.isFalse(lateAccepted);
assert.isFalse(afterLate.ownedThreadIds.has(lateChild));
const memberId = ThreadId.make("thread:late-child:workflow-member");
assert.isFalse(
RunExecutionService.routeProviderEvent(childCreated(memberId, earlyChild), identity, ended)[0],
);
const coordinator: OrchestrationV2Subagent = {
id: NodeId.make("node:late-child:workflow"),
threadId,
runId: identity.runId,
parentNodeId: NodeId.make("node:late-child"),
origin: "provider_native",
createdBy: "agent",
driver,
providerInstanceId: ProviderInstanceId.make("codex"),
providerThreadId: null,
childThreadId: earlyChild,
nativeTaskRef: null,
prompt: "Review the code",
title: "Review",
model: null,
status: "running",
result: null,
startedAt: null,
completedAt: null,
updatedAt: DateTime.makeUnsafe(0),
workflow: {
phases: [],
agents: [{ index: 0, label: "Reviewer", state: "running", childThreadId: memberId }],
},
};
const [foreignAccepted, afterForeign] = RunExecutionService.routeProviderEvent(
{
type: "subagent.updated",
driver,
subagent: {
...coordinator,
threadId: earlyChild,
childThreadId: existingDescendant,
runId: RunId.make("run:late-child:new"),
},
},
identity,
ended,
);
assert.isTrue(foreignAccepted);
assert.isFalse(
RunExecutionService.routeProviderEvent(
childCreated(memberId, existingDescendant),
identity,
afterForeign,
)[0],
);
const [workflowAccepted, afterWorkflow] = RunExecutionService.routeProviderEvent(
{ type: "subagent.updated", driver, subagent: coordinator },
identity,
ended,
);
assert.isTrue(workflowAccepted);
assert.isFalse(
RunExecutionService.routeProviderEvent(childCreated(memberId), identity, afterWorkflow)[0],
);
const [memberAccepted, afterMember] = RunExecutionService.routeProviderEvent(
childCreated(memberId, earlyChild),
identity,
afterWorkflow,
);
assert.isTrue(memberAccepted);
assert.isTrue(afterMember.ownedThreadIds.has(memberId));
const [unrelatedAccepted] = RunExecutionService.routeProviderEvent(
childCreated(ThreadId.make("thread:late-child:unrelated-member"), lateChild),
identity,
ended,
);
assert.isFalse(unrelatedAccepted);
const [unrelatedDescendantAccepted, afterUnrelatedDescendant] =
RunExecutionService.routeProviderEvent(
childCreated(ThreadId.make("thread:late-child:unrelated-descendant"), earlyChild),
identity,
afterWorkflow,
);
assert.isFalse(unrelatedDescendantAccepted);
assert.isFalse(
afterUnrelatedDescendant.ownedThreadIds.has(
ThreadId.make("thread:late-child:unrelated-descendant"),
),
);
});
it("does not route a superseded attempt through a reused provider thread", () => {
@@ -1225,10 +1321,11 @@ it.effect.each(["failure", "interruption", "stale-attempt", "start-guard"] as co
}),
);
it.effect("keeps ingesting owned child events after the root turn terminalizes", () =>
it.effect("ingests new member threads of an owned child after the root turn terminalizes", () =>
Effect.gen(function* () {
const threadId = ThreadId.make("thread:run-execution-late-child");
const childThreadId = ThreadId.make("thread:run-execution-late-child:child");
const memberThreadId = ThreadId.make("thread:run-execution-late-child:member");
const runId = RunId.make("run:run-execution-late-child");
const attemptId = RunAttemptId.make("attempt:run-execution-late-child");
const providerInstanceId = ProviderInstanceId.make("codex");
@@ -1268,9 +1365,15 @@ it.effect("keeps ingesting owned child events after the root turn terminalizes",
Layer.mock(ProviderEventIngestor.ProviderEventIngestorV2)({
ingestNormalized: (input) =>
Effect.gen(function* () {
if (
input.event.type === "app_thread.created" &&
input.event.appThread.id === memberThreadId
) {
yield* Ref.update(order, (current) => [...current, "member-thread"]);
}
if (
input.event.type === "message.updated" &&
input.event.message.threadId === childThreadId
input.event.message.threadId === memberThreadId
) {
yield* Ref.update(order, (current) => [...current, "child-message"]);
yield* Deferred.succeed(childMessageIngested, undefined).pipe(Effect.ignore);
@@ -1307,12 +1410,21 @@ it.effect("keeps ingesting owned child events after the root turn terminalizes",
type: "subagent.updated",
driver,
subagent: {
id: subagentNodeId,
threadId,
runId,
status: "running",
...makeRunOwnedSubagentFixture({
ids: backgroundScenarioIds("run-execution-late-child"),
providerInstanceId,
childThreadId,
driver,
status: "running",
}),
workflow: {
phases: [],
agents: [
{ index: 0, label: "Reviewer", state: "running", childThreadId: memberThreadId },
],
},
},
} as ProviderAdapter.ProviderAdapterV2Event,
},
{
type: "provider_turn.updated",
driver,
@@ -1335,12 +1447,24 @@ it.effect("keeps ingesting owned child events after the root turn terminalizes",
failure: null,
threadDisposition: "reusable",
},
{
type: "app_thread.created",
driver,
appThread: {
id: memberThreadId,
lineage: {
parentThreadId: childThreadId,
relationshipToParent: "subagent",
rootThreadId: threadId,
},
},
} as ProviderAdapter.ProviderAdapterV2Event,
{
type: "message.updated",
driver,
message: {
id: MessageId.make("message:run-execution-late-child:child"),
threadId: childThreadId,
threadId: memberThreadId,
runId: null,
nodeId: childNodeId,
role: "assistant",
@@ -1428,7 +1552,7 @@ it.effect("keeps ingesting owned child events after the root turn terminalizes",
Effect.timeoutOption("2 seconds"),
);
assert.isTrue(Option.isSome(observed), "child message was not ingested after root terminal");
assert.deepEqual(yield* Ref.get(order), ["root-finalized", "child-message"]);
assert.deepEqual(yield* Ref.get(order), ["root-finalized", "member-thread", "child-message"]);
}),
);
@@ -2614,11 +2738,42 @@ it.effect(
ids,
status: "running",
});
const memberThreadId = ThreadId.make("thread:subagent-interrupt-cascade:member");
const runningMember = {
...runningSubagent,
id: NodeId.make("node:subagent-interrupt-cascade:member"),
threadId: childThreadId,
runId: null,
parentNodeId: runningChildNode.id,
childThreadId: memberThreadId,
};
const foreignSubagent = {
...runningMember,
id: NodeId.make("node:subagent-interrupt-cascade:foreign-subagent"),
runId: RunId.make("run:subagent-interrupt-cascade:foreign"),
childThreadId: unrelatedChildThreadId,
};
const runningMemberRoot = {
...runningChildNode,
id: NodeId.make("node:subagent-interrupt-cascade:member-root"),
threadId: memberThreadId,
rootNodeId: NodeId.make("node:subagent-interrupt-cascade:member-root"),
};
const foreignChildNode = {
...runningChildNode,
id: NodeId.make("node:subagent-interrupt-cascade:foreign-child"),
runId: foreignSubagent.runId,
};
const runningChildTurnItem = makeLinkedChildTurnItemFixture({
ids,
driver,
type: "command_execution",
});
const foreignChildTurnItem = {
...runningChildTurnItem,
id: TurnItemId.make("turn-item:subagent-interrupt-cascade:foreign-child"),
runId: foreignSubagent.runId,
};
const suppressedChildAssistantTurnItem = makeLinkedChildTurnItemFixture({
ids: {
...ids,
@@ -2692,6 +2847,11 @@ it.effect(
driver,
turnItem: unrelatedChildTurnItem,
},
{ type: "subagent.updated", driver, subagent: runningMember },
{ type: "subagent.updated", driver, subagent: foreignSubagent },
{ type: "node.updated", driver, node: runningMemberRoot },
{ type: "node.updated", driver, node: foreignChildNode },
{ type: "turn_item.updated", driver, turnItem: foreignChildTurnItem },
rootTerminalEvent(ids, "interrupted"),
// Late provider completion after interrupt must not be ingested.
{
@@ -2758,7 +2918,7 @@ it.effect(
providerTurnId: ids.rootProviderTurnId,
} as OrchestrationV2RunAttempt,
attemptId: ids.attemptId,
relatedThreadIds: [unrelatedChildThreadId],
relatedThreadIds: [unrelatedChildThreadId, memberThreadId],
providerTurnOrdinal: 1,
message: {
messageId: MessageId.make("message:subagent-interrupt-cascade:user"),
@@ -2810,8 +2970,39 @@ it.effect(
const runUpdatedIndex = events.findIndex((event) => event.type === "run.updated");
assert.isAtLeast(runUpdatedIndex, 0, "root run.updated must be written");
assert.lengthOf(subagentEvents, 1);
const terminalSubagent = subagentEvents[0];
assert.lengthOf(subagentEvents, 2);
const terminalSubagent = subagentEvents.find(
(event) => event.payload.id === runningSubagent.id,
);
const terminalMember = subagentEvents.find((event) => event.payload.id === runningMember.id);
assert.isDefined(terminalMember);
assert.equal(terminalMember.payload.status, "interrupted");
assert.equal(terminalMember.payload.runId, null);
assert.equal(terminalMember.payload.childThreadId, memberThreadId);
assert.isTrue(
events.some(
(event) =>
event.type === "node.updated" &&
event.payload.id === runningMemberRoot.id &&
event.payload.status === "interrupted",
),
);
assert.isFalse(
events.some(
(event) => event.type === "subagent.updated" && event.payload.id === foreignSubagent.id,
),
);
assert.isFalse(
events.some(
(event) => event.type === "node.updated" && event.payload.id === foreignChildNode.id,
),
);
assert.isFalse(
events.some(
(event) =>
event.type === "turn-item.updated" && event.payload.id === foreignChildTurnItem.id,
),
);
assert.isDefined(terminalSubagent);
assert.equal(terminalSubagent.payload.status, "interrupted");
assert.equal(terminalSubagent.payload.childThreadId, childThreadId);
@@ -53,8 +53,12 @@ import * as RunFinalizationService from "./RunFinalizationService.ts";
export interface ProviderEventRoutingState {
readonly ownedThreadIds: ReadonlySet<ThreadId>;
// Set once this run's root turn ended. A child thread created after that
// belongs to the run that is live then, so this one no longer adopts it.
// belongs to the run that is live then, so this one no longer adopts it,
// except a workflow member enrolled below: its coordinator keeps this run
// open, so the member is this run's to follow.
readonly rootTurnEnded: boolean;
/** Member thread id to the coordinator child thread that spawned it. */
readonly workflowMemberParents: ReadonlyMap<ThreadId, ThreadId>;
readonly ownedProviderThreadIds: ReadonlySet<ProviderThreadId>;
readonly ownedProviderTurnIds: ReadonlySet<ProviderTurnId>;
readonly inheritedBackgroundTurnItems: ReadonlyMap<TurnItemId, OrchestrationV2Run["id"]>;
@@ -334,6 +338,7 @@ export function makeProviderEventRoutingState(input: {
return {
ownedThreadIds: new Set([input.identity.threadId, ...(input.relatedThreadIds ?? [])]),
rootTurnEnded: false,
workflowMemberParents: new Map(),
ownedProviderThreadIds: new Set([
input.identity.providerThreadId,
...(input.relatedProviderThreadIds ?? []),
@@ -379,10 +384,12 @@ export function routeProviderEvent(
return [true, state];
}
const isOwnedSubagent =
!state.rootTurnEnded &&
event.appThread.lineage.relationshipToParent === "subagent" &&
event.appThread.lineage.parentThreadId !== null &&
ownsThread(event.appThread.lineage.parentThreadId);
ownsThread(event.appThread.lineage.parentThreadId) &&
(!state.rootTurnEnded ||
state.workflowMemberParents.get(event.appThread.id) ===
event.appThread.lineage.parentThreadId);
if (!isOwnedSubagent) {
return [false, state];
}
@@ -417,8 +424,24 @@ export function routeProviderEvent(
}
return [true, addProviderThread(event.node.providerThreadId)];
}
case "subagent.updated":
return [ownsRun(event.subagent.runId) || ownsChildThread(event.subagent.threadId), state];
case "subagent.updated": {
const belongs = ownsRun(event.subagent.runId) || ownsChildThread(event.subagent.threadId);
const coordinator = event.subagent;
if (
!ownsRun(coordinator.runId) ||
coordinator.workflow === undefined ||
coordinator.childThreadId === null
) {
return [belongs, state];
}
const workflowMemberParents = new Map(state.workflowMemberParents);
for (const member of coordinator.workflow.agents) {
if (member.childThreadId !== undefined) {
workflowMemberParents.set(member.childThreadId, coordinator.childThreadId);
}
}
return [true, { ...state, workflowMemberParents }];
}
case "message.updated":
return [ownsRun(event.message.runId) || ownsChildThread(event.message.threadId), state];
case "turn_item.updated": {
@@ -1070,7 +1093,13 @@ export const layer: Layer.Layer<
const belongsToOwnedChildThread =
event.node.threadId !== input.run.threadId &&
routing.ownedThreadIds.has(event.node.threadId);
if (!belongsToRootSubagent && !belongsToOwnedChildThread) {
if (
!belongsToRootSubagent &&
!(
belongsToOwnedChildThread &&
(event.node.runId === null || event.node.runId === input.run.id)
)
) {
return;
}
yield* Ref.update(openRunOwnedSubagents, (current) => {
@@ -1108,7 +1137,11 @@ export const layer: Layer.Layer<
return next;
});
}
if (belongsToOwnedChildThread && deliverable) {
if (
belongsToOwnedChildThread &&
(event.turnItem.runId === null || belongsToRootRun) &&
deliverable
) {
yield* Ref.update(openRunOwnedSubagents, (current) => {
const childTurnItems = new Map(current.childTurnItems);
if (isSettledTurnItemStatus(event.turnItem.status)) {
@@ -388,7 +388,7 @@ function makeCompletingHandoffAdapter(
};
}
it.live("restarts selection as a new attempt and retries after old-session cleanup", () =>
it.live("retries a failed selection restart session open before completing", () =>
Effect.scoped(
Effect.gen(function* () {
const cwd = yield* checkpointWorkspace("selection-restart-lifecycle");
@@ -404,6 +404,9 @@ it.live("restarts selection as a new attempt and retries after old-session clean
const result = yield* Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
const eventSink = yield* EventSink.EventSinkV2;
yield* orchestrator.dispatch({
type: "thread.create",
createdBy: "user",
@@ -418,6 +421,21 @@ it.live("restarts selection as a new attempt and retries after old-session clean
branch: null,
worktreePath: cwd,
});
const running = yield* eventSink
.stream({
threadId,
afterSequence: yield* eventSink.latestSequence({ threadId }),
})
.pipe(
Stream.map((stored) => stored.event),
Stream.filter(
(event) =>
event.type === "provider-turn.updated" && event.payload.status === "running",
),
Stream.take(1),
Stream.runDrain,
Effect.forkScoped,
);
yield* orchestrator.dispatch({
type: "message.dispatch",
createdBy: "user",
@@ -430,11 +448,8 @@ it.live("restarts selection as a new attempt and retries after old-session clean
modelSelection: initialSelection,
dispatchMode: { type: "start_immediately" },
});
for (let index = 0; index < 1_000; index += 1) {
const current = yield* orchestrator.getThreadProjection(threadId);
if (current.providerTurns.some((turn) => turn.status === "running")) break;
yield* Effect.sleep("5 millis");
}
yield* Fiber.join(running);
yield* worker.drain();
const activeProjection = yield* orchestrator.getThreadProjection(threadId);
assert.isTrue(activeProjection.providerTurns.some((turn) => turn.status === "running"));
const activeRunId = activeProjection.runs[0]?.id;
@@ -442,6 +457,20 @@ it.live("restarts selection as a new attempt and retries after old-session clean
return yield* Effect.die("active restart test run is missing");
}
const completed = yield* eventSink
.stream({
threadId,
afterSequence: yield* eventSink.latestSequence({ threadId }),
})
.pipe(
Stream.map((stored) => stored.event),
Stream.filter(
(event) => event.type === "run.updated" && event.payload.status === "completed",
),
Stream.take(1),
Stream.runDrain,
Effect.forkScoped,
);
yield* orchestrator.dispatch({
type: "message.dispatch",
createdBy: "user",
@@ -454,27 +483,12 @@ it.live("restarts selection as a new attempt and retries after old-session clean
modelSelection: replacementSelection,
dispatchMode: { type: "restart_active", targetRunId: activeRunId },
});
for (let index = 0; index < 1_000; index += 1) {
const current = yield* orchestrator.getThreadProjection(threadId);
if (current.attempts.length === 2 && current.attempts[1]?.status === "completed") {
const captured = yield* Ref.get(state);
return { projection: current, captured };
}
yield* Effect.sleep("5 millis");
}
const current = yield* orchestrator.getThreadProjection(threadId);
const adapterState = yield* Ref.get(state);
yield* Effect.logError("selection restart did not complete", {
runs: current.runs.map((run) => [run.status, run.activeAttemptId]),
attempts: current.attempts.map((attempt) => [attempt.id, attempt.status]),
providerTurns: current.providerTurns.map((turn) => [turn.id, turn.status]),
providerThreads: current.providerThreads.map((thread) => [
thread.providerSessionId,
thread.status,
]),
adapterState,
});
return yield* Effect.die("selection restart did not complete");
yield* Fiber.join(completed);
yield* worker.drain();
return {
projection: yield* orchestrator.getThreadProjection(threadId),
captured: yield* Ref.get(state),
};
}).pipe(
Effect.provide(
ProviderReplayHarness.layerWithRegistry(
@@ -501,7 +515,8 @@ it.live("restarts selection as a new attempt and retries after old-session clean
),
"selection restart supersede must not project hard-Stop interrupt items",
);
assert.equal(projection.runs[0]?.modelSelection.model, replacementSelection.model);
assert.equal(projection.runs.at(-1)?.modelSelection.model, replacementSelection.model);
assert.equal(projection.runs.at(-1)?.status, "completed");
assert.isTrue(captured.failedReplacementOpen);
// The old pooled process remains available to its other threads; this
// thread moved to a freshly allocated replacement session.
@@ -5,25 +5,38 @@ import {
NodeId,
type OrchestrationV2Command,
type OrchestrationV2Run,
type OrchestrationV2Subagent,
type OrchestrationV2ProviderThread,
ProviderThreadId,
ProviderDriverKind,
ProviderSessionId,
type OrchestrationV2StoredEvent,
type OrchestrationV2ThreadProjection,
type OrchestrationV2ThreadShell,
ProjectId,
ProviderInstanceId,
RunId,
ThreadId,
} from "@t3tools/contracts";
import * as Deferred from "effect/Deferred";
import * as DateTime from "effect/DateTime";
import * as Option from "effect/Option";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Fiber from "effect/Fiber";
import * as Layer from "effect/Layer";
import * as Queue from "effect/Queue";
import * as Ref from "effect/Ref";
import * as Stream from "effect/Stream";
import * as TestClock from "effect/testing/TestClock";
import * as LegacyV1ThreadImporter from "./legacy/LegacyV1ThreadImporter.ts";
import * as Orchestrator from "./Orchestrator.ts";
import * as ProviderSessionManager from "./ProviderSessionManager.ts";
import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
import * as ThreadManagementService from "./ThreadManagementService.ts";
import * as ThreadCommandExecutor from "./ThreadCommandExecutor.ts";
import { DispatchModeLimit, type DispatchModeRefusal } from "./DispatchModeLimit.ts";
it("stamps authoritative provenance on commands that create threads or messages", () => {
const command: OrchestrationV2Command = {
@@ -269,6 +282,7 @@ it.effect("classifies projection infrastructure failures separately from a missi
cause: infrastructureCause,
});
const layerTest = ThreadManagementService.layer.pipe(
Layer.provide(Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({})),
Layer.provide(
Layer.mock(Orchestrator.OrchestratorV2)({
getThreadProjection: () => Effect.fail(projectionError),
@@ -302,6 +316,7 @@ it.effect("uses thread-not-found only after a projection loads outside the proje
},
} as OrchestrationV2ThreadProjection;
const layerTest = ThreadManagementService.layer.pipe(
Layer.provide(Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({})),
Layer.provide(
Layer.mock(Orchestrator.OrchestratorV2)({
getThreadProjection: () => Effect.succeed(projection),
@@ -327,6 +342,7 @@ it.effect("preserves failed legacy materialization when reading checkpoint conte
cause: new Error("checkpoint import failed"),
});
const layerTest = ThreadManagementService.layerWithLegacyImporter.pipe(
Layer.provide(Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({})),
Layer.provide(
Layer.mergeAll(
Layer.mock(Orchestrator.OrchestratorV2)({
@@ -368,6 +384,7 @@ it.effect.each([
runs: status === "missing" ? [] : [{ id: runId, status }],
}) as unknown as OrchestrationV2ThreadProjection;
const layerTest = ThreadManagementService.layer.pipe(
Layer.provide(Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({})),
Layer.provide(
Layer.mock(Orchestrator.OrchestratorV2)({
getThreadEventSequence: () => Effect.succeed(0),
@@ -434,6 +451,7 @@ it.effect("waitForThread reads the run again only when the run updates", () =>
const stored = (sequence: number, event: object) =>
({ sequence, event: { threadId, ...event } }) as unknown as OrchestrationV2StoredEvent;
const layerTest = ThreadManagementService.layer.pipe(
Layer.provide(Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({})),
Layer.provide(
Layer.mock(Orchestrator.OrchestratorV2)({
getThreadEventSequence: () => Effect.succeed(0),
@@ -474,6 +492,222 @@ it.effect("waitForThread reads the run again only when the run updates", () =>
}),
);
it.effect.each([
"running",
"missing",
"completed",
"unsupported",
"missing-native-id",
"wrong-thread",
"no-session",
"unsupported-runtime",
"provider-failed",
"provider-timeout",
"within-limit",
"runtime-raised",
"interaction-raised",
] as const)("stops a persisted workflow with state %s", (state) =>
Effect.gen(function* () {
const threadId = ThreadId.make("thread:workflow-stop");
const subagentId = NodeId.make("node:workflow-stop");
const runId = RunId.make("run:workflow-stop");
const providerThreadId = ProviderThreadId.make("provider-thread:workflow-stop");
const providerSessionId = ProviderSessionId.make("provider-session:workflow-stop");
const driver = ProviderDriverKind.make("claudeAgent");
const instanceId = ProviderInstanceId.make("claudeAgent");
const now = yield* DateTime.now;
let targetModes: Pick<OrchestrationV2ThreadShell, "runtimeMode" | "interactionMode"> = {
runtimeMode: "approval-required",
interactionMode: "plan",
};
const task: OrchestrationV2Subagent = {
id: subagentId,
threadId,
runId,
parentNodeId: NodeId.make("node:workflow-parent"),
origin: "provider_native",
createdBy: "agent",
driver,
providerInstanceId: instanceId,
providerThreadId: null,
childThreadId: null,
nativeTaskRef:
state === "missing-native-id"
? null
: {
driver,
nativeId: "native-workflow-id",
strength: "strong",
},
workflow: { phases: [], agents: [] },
prompt: "Run the workflow",
title: null,
model: null,
status: state === "completed" ? "completed" : "running",
result: null,
startedAt: now,
completedAt: null,
updatedAt: now,
...(state === "unsupported" ? { driver: ProviderDriverKind.make("codex") } : {}),
};
const providerThread: OrchestrationV2ProviderThread = {
id: providerThreadId,
driver,
providerInstanceId: instanceId,
providerSessionId,
appThreadId: state === "wrong-thread" ? ThreadId.make("other-thread") : threadId,
ownerNodeId: null,
nativeThreadRef: { driver, nativeId: "native-thread", strength: "strong" },
nativeConversationHeadRef: null,
status: "idle",
firstRunOrdinal: 1,
lastRunOrdinal: 1,
handoffIds: [],
forkedFrom: null,
pendingBackgroundTasks: [],
contextUsage: null,
nativeMetadata: null,
createdAt: now,
updatedAt: now,
};
const calls: Array<{ taskId: string; providerThread: OrchestrationV2ProviderThread }> = [];
const stopStarted = yield* Deferred.make<void>();
const runtime = {
driver,
providerSessionId,
instanceId,
...(state === "unsupported-runtime"
? {}
: {
stopTask: (input: (typeof calls)[number]) =>
state === "provider-failed"
? Effect.fail(
new ProviderAdapter.ProviderAdapterProtocolError({
driver,
detail: "private provider detail",
}),
)
: state === "provider-timeout"
? Deferred.succeed(stopStarted, undefined).pipe(Effect.andThen(Effect.never))
: Effect.sync(() => {
calls.push(input);
}),
}),
} as unknown as ProviderAdapter.ProviderAdapterV2SessionRuntime;
const layerTest = ThreadManagementService.layer.pipe(
Layer.provide(
Layer.mergeAll(
Layer.mock(Orchestrator.OrchestratorV2)({
getThreadRecords: () =>
Effect.sync(
() =>
({
thread: { id: threadId, deletedAt: null, ...targetModes },
subagents: state === "missing" ? [] : [task],
runs: [{ id: runId, providerThreadId }],
providerThreads: [providerThread],
}) as unknown as OrchestrationV2ThreadProjection,
),
}),
Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({
get: (id) => {
expect(id).toBe(providerSessionId);
return Effect.succeed(state === "no-session" ? Option.none() : Option.some(runtime));
},
}),
),
),
);
yield* Effect.gen(function* () {
const service = yield* ThreadManagementService.ThreadManagementService;
const refused = yield* Ref.make<DispatchModeRefusal | undefined>(undefined);
const stop = service
.stopWorkflow({ threadId, subagentId })
.pipe(
Effect.provideService(
DispatchModeLimit,
state === "within-limit" || state === "runtime-raised" || state === "interaction-raised"
? { runtimeMode: "approval-required", interactionMode: "plan", refused }
: undefined,
),
Effect.result,
);
const executor = yield* ThreadCommandExecutor.ThreadCommandExecutor;
const result =
state === "runtime-raised" || state === "interaction-raised"
? yield* Effect.gen(function* () {
const started = yield* Deferred.make<void>();
const pending = yield* executor.withLock(
threadId,
Effect.gen(function* () {
const fiber = yield* Deferred.succeed(started, undefined).pipe(
Effect.andThen(stop),
Effect.forkChild,
);
yield* Deferred.await(started);
targetModes =
state === "runtime-raised"
? { ...targetModes, runtimeMode: "full-access" }
: { ...targetModes, interactionMode: "default" };
return fiber;
}),
);
return yield* Fiber.join(pending);
})
: state === "provider-timeout"
? yield* Effect.gen(function* () {
const finished = yield* Ref.make(false);
const pending = yield* stop.pipe(
Effect.tap(() => Ref.set(finished, true)),
Effect.forkChild,
);
yield* Deferred.await(stopStarted);
yield* TestClock.adjust("15 seconds");
expect(yield* Ref.get(finished)).toBe(true);
const result = yield* Fiber.join(pending);
expect(yield* executor.withLock(threadId, Effect.succeed("lock-released"))).toBe(
"lock-released",
);
return result;
})
: yield* stop;
if (state === "running" || state === "within-limit") {
expect(result).toMatchObject({ _tag: "Success" });
expect(calls).toEqual([{ taskId: "native-workflow-id", providerThread }]);
expect(task.status).toBe("running");
} else {
expect(result).toMatchObject({
_tag: "Failure",
failure: {
_tag: "OrchestrationV2StopWorkflowError",
threadId,
subagentId,
reason:
state === "missing" || state === "completed"
? "not-running"
: state === "unsupported" || state === "unsupported-runtime"
? "unsupported"
: state === "provider-failed" || state === "provider-timeout"
? "stop-failed"
: "unavailable",
},
});
expect(calls).toEqual([]);
if (state === "provider-timeout") {
expect(result).toMatchObject({ failure: { cause: { _tag: "TimeoutError" } } });
}
if (state === "runtime-raised" || state === "interaction-raised") {
expect(yield* Ref.get(refused)).toEqual({
threadId,
...targetModes,
mode: state === "runtime-raised" ? "runtime" : "interaction",
});
}
}
}).pipe(Effect.provide(Layer.merge(layerTest, ThreadCommandExecutor.layer)));
}),
);
it.effect.each([
{ status: "completed" as const, settles: true },
{ status: "failed" as const, settles: false },
@@ -485,6 +719,7 @@ it.effect.each([
const runId = RunId.make("run:thread-management:settle-after-run");
const dispatched: Array<string> = [];
const layerTest = ThreadManagementService.layer.pipe(
Layer.provide(Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({})),
Layer.provide(
Layer.mock(Orchestrator.OrchestratorV2)({
getThreadEventSequence: () => Effect.succeed(0),
@@ -11,6 +11,8 @@ import {
type ChatAttachment,
CommandId,
MessageId,
OrchestrationV2StopWorkflowError,
type OrchestrationV2StopWorkflowInput,
type ModelSelection,
type OrchestrationV2Actor,
type OrchestrationV2Command,
@@ -35,10 +37,14 @@ import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Ref from "effect/Ref";
import * as Schema from "effect/Schema";
import * as Stream from "effect/Stream";
import * as Orchestrator from "./Orchestrator.ts";
import * as ProviderSessionManager from "./ProviderSessionManager.ts";
import * as ThreadCommandExecutor from "./ThreadCommandExecutor.ts";
import { DispatchModeLimit, exceededDispatchModeLimit } from "./DispatchModeLimit.ts";
import { projectTurnItemForDetail } from "./WireProjection.ts";
import * as LegacyV1ThreadImporter from "./legacy/LegacyV1ThreadImporter.ts";
@@ -364,6 +370,13 @@ export interface ThreadManagementServiceShape {
readonly interruptThread: (
input: ThreadManagementInterruptInput,
) => Effect.Effect<ThreadManagementInterruptResult, ThreadManagementFailure>;
/**
* Asks the provider to stop a running workflow coordinator. The provider's
* own terminal notification then settles the coordinator and its members.
*/
readonly stopWorkflow: (
input: OrchestrationV2StopWorkflowInput,
) => Effect.Effect<void, OrchestrationV2StopWorkflowError>;
/**
* Sends `thread.stop` to every delegated task under a thread, depth first. Their command IDs
* derive from `commandId`, so a retry repeats nothing that already stopped. A task that
@@ -440,7 +453,9 @@ function latestSteerableRun(
const SETTLE_AFTER_RUN_WAIT_MS = 24 * 60 * 60 * 1_000;
const make = Effect.gen(function* () {
const threadDispatch = yield* ThreadCommandExecutor.ThreadCommandExecutor;
const orchestrator = yield* Orchestrator.OrchestratorV2;
const sessions = yield* ProviderSessionManager.ProviderSessionManagerV2;
const layerScope = yield* Effect.scope;
const legacyImporter = yield* LegacyV1ThreadImporter.LegacyV1ThreadImporter;
@@ -841,6 +856,90 @@ const make = Effect.gen(function* () {
return { type: "interrupt_requested", run: interruptibleRun, dispatch } as const;
});
const stopWorkflow: ThreadManagementServiceShape["stopWorkflow"] = (input) =>
Effect.gen(function* () {
const records = yield* orchestrator
.getThreadRecords(input.threadId, ["subagents", "runs", "providerThreads"])
.pipe(
Effect.mapError(
(cause) =>
new OrchestrationV2StopWorkflowError({ ...input, reason: "unavailable", cause }),
),
);
if (records.thread.deletedAt !== null) {
return yield* new OrchestrationV2StopWorkflowError({ ...input, reason: "unavailable" });
}
// A limited caller (an agent over MCP) may not stop work on a thread
// running above its own modes. Checked under the thread lock, where the
// thread's user cannot raise the modes between this read and the stop.
const limit = yield* DispatchModeLimit;
const exceeded =
limit === undefined ? undefined : exceededDispatchModeLimit(limit, records.thread);
if (limit !== undefined && exceeded !== undefined) {
if (limit.refused !== undefined) {
yield* Ref.set(limit.refused, {
threadId: input.threadId,
runtimeMode: records.thread.runtimeMode,
interactionMode: records.thread.interactionMode,
mode: exceeded,
});
}
return yield* new OrchestrationV2StopWorkflowError({ ...input, reason: "unavailable" });
}
const task = records.subagents.find((candidate) => candidate.id === input.subagentId);
if (
task === undefined ||
task.threadId !== input.threadId ||
task.workflow === undefined ||
task.status !== "running"
) {
return yield* new OrchestrationV2StopWorkflowError({ ...input, reason: "not-running" });
}
if (task.origin !== "provider_native" || task.driver !== "claudeAgent") {
return yield* new OrchestrationV2StopWorkflowError({ ...input, reason: "unsupported" });
}
const providerThreadId =
task.providerThreadId ??
records.runs.find((run) => run.id === task.runId)?.providerThreadId;
const providerThread = records.providerThreads.find(
(candidate) =>
candidate.id === providerThreadId &&
candidate.appThreadId === input.threadId &&
candidate.providerInstanceId === task.providerInstanceId &&
candidate.driver === task.driver,
);
const taskId = task.nativeTaskRef?.nativeId;
if (providerThread?.providerSessionId == null || taskId == null || taskId.length === 0) {
return yield* new OrchestrationV2StopWorkflowError({ ...input, reason: "unavailable" });
}
const runtime = yield* sessions
.get(providerThread.providerSessionId)
.pipe(
Effect.mapError(
(cause) =>
new OrchestrationV2StopWorkflowError({ ...input, reason: "unavailable", cause }),
),
);
if (Option.isNone(runtime)) {
return yield* new OrchestrationV2StopWorkflowError({ ...input, reason: "unavailable" });
}
if (runtime.value.stopTask === undefined) {
return yield* new OrchestrationV2StopWorkflowError({ ...input, reason: "unsupported" });
}
// Bounded so a provider that never answers cannot hold the thread lock.
yield* runtime.value.stopTask({ providerThread, taskId }).pipe(
Effect.timeout("15 seconds"),
Effect.mapError(
(cause) =>
new OrchestrationV2StopWorkflowError({
...input,
reason: "stop-failed",
cause,
}),
),
);
}).pipe((effect) => threadDispatch.withLock(input.threadId, effect));
const stopDelegatedTasks: ThreadManagementServiceShape["stopDelegatedTasks"] = (input) =>
Effect.gen(function* () {
const { subagents } = yield* orchestrator.getThreadRecords(input.threadId, ["subagents"]);
@@ -919,6 +1018,7 @@ const make = Effect.gen(function* () {
settleThread,
interruptThread,
stopDelegatedTasks,
stopWorkflow,
getThreadEventSequence: orchestrator.getThreadEventSequence,
recoverDelegatedTask: orchestrator.recoverDelegatedTask,
delegatedTaskResultPending: orchestrator.delegatedTaskResultPending,
@@ -938,11 +1038,19 @@ const layerLegacyV1ThreadImporterNoop = Layer.succeed(
}),
);
export const layer: Layer.Layer<ThreadManagementService, never, Orchestrator.OrchestratorV2> =
Layer.effect(ThreadManagementService, make).pipe(Layer.provide(layerLegacyV1ThreadImporterNoop));
export const layer: Layer.Layer<
ThreadManagementService,
never,
Orchestrator.OrchestratorV2 | ProviderSessionManager.ProviderSessionManagerV2
> = Layer.effect(ThreadManagementService, make).pipe(
Layer.provide(layerLegacyV1ThreadImporterNoop),
Layer.provide(ThreadCommandExecutor.layer),
);
export const layerWithLegacyImporter: Layer.Layer<
ThreadManagementService,
never,
LegacyV1ThreadImporter.LegacyV1ThreadImporter | Orchestrator.OrchestratorV2
> = Layer.effect(ThreadManagementService, make);
| LegacyV1ThreadImporter.LegacyV1ThreadImporter
| Orchestrator.OrchestratorV2
| ProviderSessionManager.ProviderSessionManagerV2
> = Layer.effect(ThreadManagementService, make).pipe(Layer.provide(ThreadCommandExecutor.layer));
@@ -240,7 +240,13 @@ const layerAgentSessionImporterProvided = AgentSessionImporter.layer.pipe(
);
const layerThreadManagementProvided = ThreadManagementService.layerWithLegacyImporter.pipe(
Layer.provide(Layer.merge(layerOrchestratorProvided, layerLegacyV1ThreadImporterProvided)),
Layer.provide(
Layer.mergeAll(
layerOrchestratorProvided,
layerLegacyV1ThreadImporterProvided,
layerProviderSessionManagerProvided,
),
),
);
export const layerProjectSetupScriptRunner = ProjectSetupScriptRunner.layer.pipe(
Layer.provide(layerProjectService),
@@ -242,7 +242,10 @@ export function layerProviderReplay<Transcript extends ProviderReplayTranscript,
readonly replayGate?: ProviderReplayGate;
} = {},
): Layer.Layer<
Orchestrator.OrchestratorV2 | EffectWorker.OrchestrationEffectWorkerV2 | EventSink.EventSinkV2,
| Orchestrator.OrchestratorV2
| EffectWorker.OrchestrationEffectWorkerV2
| EventSink.EventSinkV2
| ProviderSessionManager.ProviderSessionManagerV2,
Error | MigrationError | PlatformError.PlatformError | SqlError
> {
const layerRegistry = harness.makeProviderAdapterRegistryLayer(
@@ -59,6 +59,20 @@ export function assertSubagentV2ApprovalOutput(
const approval = approvalItems[0]!;
assert.equal(approval.runId, run.id);
assert.equal(approval.status, "completed");
const pendingRequestIndex = result.domainEvents.findIndex(
(event) =>
event.type === "runtime-request.updated" &&
event.payload.id === request.id &&
event.payload.status === "pending",
);
const waitingItemIndex = result.domainEvents.findIndex(
(event) =>
event.type === "turn-item.updated" &&
event.payload.id === approval.id &&
event.payload.status === "waiting",
);
assert.isAtLeast(waitingItemIndex, 0);
assert.isAbove(pendingRequestIndex, waitingItemIndex);
if (approval.type !== "approval_request") throw new Error("expected an approval item");
assert.equal(approval.requestId, request.id);
assert.include(approval.prompt ?? "", "subagent-approval.txt");
@@ -72,6 +72,20 @@ export function assertSubagentV2NestedApprovalOutput(
const approval = approvalItems[0]!;
assert.equal(approval.runId, run.id);
assert.equal(approval.status, "completed");
const pendingRequestIndex = result.domainEvents.findIndex(
(event) =>
event.type === "runtime-request.updated" &&
event.payload.id === request.id &&
event.payload.status === "pending",
);
const waitingItemIndex = result.domainEvents.findIndex(
(event) =>
event.type === "turn-item.updated" &&
event.payload.id === approval.id &&
event.payload.status === "waiting",
);
assert.isAtLeast(waitingItemIndex, 0);
assert.isAbove(pendingRequestIndex, waitingItemIndex);
if (approval.type !== "approval_request") throw new Error("expected an approval item");
assert.equal(approval.requestId, request.id);
assert.include(approval.prompt ?? "", "nested-approval.txt");
@@ -0,0 +1,10 @@
/** Readers for decoding undeclared provider payloads without trusting shapes. */
export function field(source: unknown, key: string): unknown {
return typeof source === "object" && source !== null ? Reflect.get(source, key) : undefined;
}
export function text(value: unknown): string | undefined {
if (typeof value !== "string") return undefined;
const trimmed = value.trim();
return trimmed.length === 0 ? undefined : trimmed;
}
@@ -0,0 +1,121 @@
// @effect-diagnostics nodeBuiltinImport:off
import * as NodeFS from "node:fs";
import * as NodeOS from "node:os";
import * as NodePath from "node:path";
import { it as effectIt } from "@effect/vitest";
import * as Effect from "effect/Effect";
import { afterAll, beforeAll, describe, expect, it } from "vite-plus/test";
import { parseWorkflowAgentAnswers, readWorkflowAgentAnswers } from "./workflowAgentAnswers.ts";
const line = (value: unknown) => `${JSON.stringify(value)}\n`;
const assistant = (id: string, content: ReadonlyArray<unknown>) =>
line({ type: "assistant", message: { role: "assistant", id, content } });
describe("parseWorkflowAgentAnswers", () => {
it("returns one entry per assistant turn", () => {
expect(
parseWorkflowAgentAnswers(
assistant("a", [{ type: "text", text: "first" }]) +
assistant("b", [{ type: "text", text: "second" }]),
),
).toEqual(["first", "second"]);
});
it("joins the lines of one split assistant message into a single turn", () => {
expect(
parseWorkflowAgentAnswers(
assistant("a", [{ type: "thinking", thinking: "weighing it up" }]) +
assistant("a", [{ type: "text", text: "the answer" }]) +
assistant("a", [{ type: "text", text: "and more" }]),
),
).toEqual(["the answer\n\nand more"]);
});
it("ignores harness bookkeeping and the member's own prompt", () => {
expect(
parseWorkflowAgentAnswers(
line({ type: "attachment", attachment: { type: "skill_listing", content: "- adhd" } }) +
line({ type: "user", message: { role: "user", content: "the prompt" } }) +
line({
type: "assistant",
isMeta: true,
message: { role: "assistant", id: "m", content: [{ type: "text", text: "injected" }] },
}) +
assistant("a", [{ type: "text", text: "real answer" }]),
),
).toEqual(["real answer"]);
});
it("skips a turn that produced no text", () => {
expect(
parseWorkflowAgentAnswers(
assistant("a", [{ type: "tool_use", id: "t", name: "Bash", input: {} }]),
),
).toEqual([]);
});
it("preserves answer indentation and ignores blank text", () => {
expect(
parseWorkflowAgentAnswers(
assistant("a", [{ type: "text", text: " indented code" }]) +
assistant("b", [{ type: "text", text: " " }]),
),
).toEqual([" indented code"]);
});
it("survives a trailing line the byte cap cut in half", () => {
expect(
parseWorkflowAgentAnswers(
assistant("a", [{ type: "text", text: "complete" }]) + '{"type":"assistant","mess',
),
).toEqual(["complete"]);
});
});
// Transcripts must sit under <configDir>/projects: readContainedWorkflowFile
// rejects any path outside it. A temporary configDir keeps the real
// ~/.claude untouched.
let configDir: string;
let transcriptDir: string;
beforeAll(() => {
configDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "wf-config-"));
transcriptDir = NodePath.join(configDir, "projects", "test");
NodeFS.mkdirSync(transcriptDir, { recursive: true });
});
afterAll(() => {
NodeFS.rmSync(configDir, { recursive: true, force: true });
});
describe("readWorkflowAgentAnswers", () => {
// The under-cap case is the control: without it a read stuck on [] would pass.
effectIt.effect("reads final answers from both small and capped transcripts", () =>
Effect.gen(function* () {
const turn = assistant("a", [{ type: "text", text: "the answer" }]);
const filler = line({ type: "user", message: { role: "user", content: "x".repeat(4096) } });
const write = (agentId: string, contents: string) =>
NodeFS.writeFileSync(NodePath.join(transcriptDir, `agent-${agentId}.jsonl`), contents);
const read = (agentId: string) =>
readWorkflowAgentAnswers({ transcriptDir, agentId, configDir });
write("small", turn);
const edgeAnswer = "x".repeat(
512 * 1024 + 1 - Buffer.byteLength(assistant("edge", [{ type: "text", text: "" }])),
);
write("edge", assistant("edge", [{ type: "text", text: edgeAnswer }]));
write(
"boundary",
filler.repeat(160) + turn + " ".repeat(512 * 1024 - Buffer.byteLength(turn)),
);
write(
"huge",
turn + filler.repeat(160) + assistant("final", [{ type: "text", text: "final answer" }]),
);
expect(yield* read("small")).toEqual(["the answer"]);
expect(yield* read("edge")).toEqual([edgeAnswer]);
expect(yield* read("huge")).toEqual(["final answer"]);
expect(yield* read("boundary")).toEqual(["the answer"]);
}),
);
});
@@ -0,0 +1,97 @@
// Path joining only; the filesystem access lives in readContainedWorkflowFile.
// @effect-diagnostics nodeBuiltinImport:off
/**
* Reads what a workflow member actually answered.
*
* A member agent never becomes a provider task and emits nothing on the event
* stream, so the transcript the harness writes is the only full record of its
* turn — the progress snapshot carries a capped excerpt. The path is derived
* here from the run's transcript directory and the member's provider id rather
* than accepted from the caller, and the read itself is contained by
* readContainedWorkflowFile.
*/
import * as NodePath from "node:path";
import { OrchestrationWorkflowFileError } from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import { field, text } from "./unknownField.ts";
import { readContainedWorkflowFile } from "./workflowFileRead.ts";
const TRANSCRIPT_BYTE_CAP = 512 * 1024;
/** A member id names a file, so anything path-shaped is rejected outright. */
const AGENT_ID_PATTERN = /^[A-Za-z0-9_-]{1,128}$/;
/**
* Collects the assistant text of each turn in the harness's JSONL.
*
* Two shapes have to be handled: `attachment` lines and `isMeta` user lines are
* harness bookkeeping (hook output, skill listings, injected context) and are
* never conversation, and one assistant API message is split across several
* lines sharing a `message.id`, so those are joined into a single turn rather
* than read as separate ones.
*/
export function parseWorkflowAgentAnswers(contents: string): ReadonlyArray<string> {
const turns: string[] = [];
const turnIndexById = new Map<string, number>();
for (const line of contents.split("\n")) {
if (line.trim().length === 0) continue;
let entry: unknown;
try {
entry = JSON.parse(line);
} catch {
// A capped read can cut the final line mid-object; drop it and keep the rest.
continue;
}
if (text(field(entry, "type")) !== "assistant") continue;
if (field(entry, "isMeta") === true) continue;
const message = field(entry, "message");
const content = field(message, "content");
if (!Array.isArray(content)) continue;
const body = content
.filter((block) => text(field(block, "type")) === "text")
.map((block) => field(block, "text"))
.filter((value): value is string => typeof value === "string" && value.trim().length > 0)
.join("\n\n");
if (body.length === 0) continue;
const messageId = text(field(message, "id"));
const existing = messageId === undefined ? undefined : turnIndexById.get(messageId);
if (existing !== undefined) {
turns[existing] = `${turns[existing]}\n\n${body}`;
continue;
}
if (messageId !== undefined) turnIndexById.set(messageId, turns.length);
turns.push(body);
}
return turns;
}
export const readWorkflowAgentAnswers = Effect.fn("orchestration.readWorkflowAgentAnswers")(
function* (input: {
readonly transcriptDir: string;
readonly agentId: string;
readonly configDir?: string;
}) {
if (!AGENT_ID_PATTERN.test(input.agentId)) {
return yield* new OrchestrationWorkflowFileError({
reason: "invalid-path",
path: input.agentId,
});
}
const file = yield* readContainedWorkflowFile({
path: NodePath.join(input.transcriptDir, `agent-${input.agentId}.jsonl`),
extension: ".jsonl",
byteCap: TRANSCRIPT_BYTE_CAP,
tail: true,
...(input.configDir === undefined ? {} : { configDir: input.configDir }),
});
return parseWorkflowAgentAnswers(
file.truncated ? file.contents.slice(file.contents.indexOf("\n") + 1) : file.contents,
);
},
);
@@ -0,0 +1,112 @@
// @effect-diagnostics nodeBuiltinImport:off
/**
* Contained reads of files the Claude harness persisted for a workflow run.
*
* Containment rules (lifted from the reviewed #3650 inspection service):
* - the resolved realpath must live under the configured Claude projects
* directory (where the harness persists workflow scripts and transcripts) —
* realpath re-containment defeats symlink escapes, including a symlinked
* leaf file;
* - only the caller's expected extension is served;
* - reads are size-capped rather than failed, with a truncation marker.
*
* The client-supplied path is a hint from the workflow's runHandles; it is
* never trusted beyond these checks.
*/
import * as NodeFS from "node:fs";
import * as NodeFSP from "node:fs/promises";
import * as NodePath from "node:path";
import { OrchestrationWorkflowFileError } from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as HostProcess from "@t3tools/shared/HostProcess";
function workflowFilesRoot(home: string, configDir?: string): string {
return NodePath.join(configDir?.trim() || NodePath.join(home, ".claude"), "projects");
}
export const readContainedWorkflowFile = Effect.fn("orchestration.readContainedWorkflowFile")(
function* (input: {
readonly path: string;
readonly extension: string;
readonly byteCap: number;
readonly tail?: boolean;
readonly configDir?: string;
}) {
if (!NodePath.isAbsolute(input.path) || NodePath.extname(input.path) !== input.extension) {
return yield* new OrchestrationWorkflowFileError({
reason: "invalid-path",
path: input.path,
});
}
const home = yield* HostProcess.HomeDirectory;
const root = yield* Effect.tryPromise({
try: () => NodeFSP.realpath(workflowFilesRoot(home, input.configDir)),
catch: (cause) =>
new OrchestrationWorkflowFileError({
reason: "root-unavailable",
path: input.path,
cause,
}),
});
// Realpath the FILE itself (not just its directory): a symlink named
// like a transcript inside a contained directory must not escape.
const resolved = yield* Effect.tryPromise({
try: () => NodeFSP.realpath(input.path),
catch: (cause) =>
new OrchestrationWorkflowFileError({ reason: "not-found", path: input.path, cause }),
});
if (resolved !== root && !resolved.startsWith(`${root}${NodePath.sep}`)) {
return yield* new OrchestrationWorkflowFileError({ reason: "outside-root", path: resolved });
}
if (NodePath.extname(resolved) !== input.extension) {
return yield* new OrchestrationWorkflowFileError({
reason: "wrong-extension",
path: resolved,
});
}
// fd inode vs path inode catches a swapped leaf, not a swapped intermediate
// dir; root is 0700 ~/.claude, so that already needs the uid we run as.
// Both containment checks fail with their own tagged reason; "read-failed"
// is reserved for genuine platform failures with the real cause attached.
const read = yield* Effect.tryPromise({
try: async () => {
const handle = await NodeFSP.open(
resolved,
NodeFS.constants.O_RDONLY | (NodeFS.constants.O_NONBLOCK ?? 0),
);
try {
const stat = await handle.stat();
if (!stat.isFile()) {
return { failure: "not-regular-file" as const };
}
const pathStat = await NodeFSP.lstat(resolved);
if (stat.ino !== pathStat.ino || stat.dev !== pathStat.dev) {
return { failure: "changed-during-read" as const };
}
const buffer = Buffer.alloc(Math.min(stat.size, input.byteCap + (input.tail ? 1 : 0)));
const truncated = stat.size > buffer.length;
const offset = input.tail ? stat.size - buffer.length : 0;
const { bytesRead } = await handle.read(buffer, 0, buffer.length, offset);
return {
contents: buffer.subarray(0, bytesRead).toString("utf8"),
truncated,
};
} finally {
await handle.close();
}
},
catch: (cause) =>
new OrchestrationWorkflowFileError({ reason: "read-failed", path: resolved, cause }),
});
if ("failure" in read) {
return yield* new OrchestrationWorkflowFileError({ reason: read.failure, path: resolved });
}
return { path: resolved, contents: read.contents, truncated: read.truncated };
},
);
@@ -2,6 +2,9 @@
import * as NodeFS from "node:fs";
import * as NodeOS from "node:os";
import * as NodePath from "node:path";
import * as NodeChildProcess from "node:child_process";
import { OrchestrationWorkflowFileError } from "@t3tools/contracts";
import * as HostProcess from "@t3tools/shared/HostProcess";
import { it as effectIt } from "@effect/vitest";
import * as Effect from "effect/Effect";
import { afterAll, assert, describe } from "vite-plus/test";
@@ -30,7 +33,18 @@ afterAll(() => {
NodeFS.rmSync(outside, { force: true });
});
describe("readWorkflowScript containment", () => {
describe("workflow script containment", () => {
effectIt.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
"rejects a FIFO without waiting for a writer",
() =>
Effect.gen(function* () {
const fifo = NodePath.join(root, "pipe.js");
NodeChildProcess.execFileSync("mkfifo", [fifo]);
const error = yield* Effect.flip(readWorkflowScript({ scriptPath: fifo }));
assert.equal(error.reason, "not-regular-file");
}),
);
effectIt.effect("serves a real script under the projects root", () =>
Effect.gen(function* () {
const result = yield* readWorkflowScript({ scriptPath });
@@ -47,6 +61,12 @@ describe("readWorkflowScript containment", () => {
readWorkflowScript({ scriptPath: scriptPath.replace(".js", ".ts") }),
);
assert.equal(nonJs._tag, "Failure");
const error = yield* Effect.flip(readWorkflowScript({ scriptPath: "run.js" }));
assert.equal(error._tag, "OrchestrationGetWorkflowScriptError");
assert.equal(error.scriptPath, "run.js");
assert.equal(error.reason, "invalid-path");
assert.equal(error.message, "Workflow scripts must be absolute .js paths.");
assert.instanceOf(error.cause, OrchestrationWorkflowFileError);
}),
);
@@ -1,128 +1,23 @@
// @effect-diagnostics nodeBuiltinImport:off
/**
* Read-only access to persisted workflow scripts for the Agents surface's
* "{} script" affordance.
*
* Containment rules (lifted from the reviewed #3650 inspection service):
* - the resolved realpath must live under ~/.claude/projects (where the
* Claude harness persists workflow scripts) — realpath re-containment
* defeats symlink escapes, including a symlinked leaf file;
* - only .js leaf files are served;
* - reads are size-capped rather than failed, with a truncation marker.
*
* The client-supplied path is a hint from the workflow's runHandles; it is
* never trusted beyond these checks.
*/
import * as NodeFSP from "node:fs/promises";
import * as NodePath from "node:path";
import { OrchestrationGetWorkflowScriptError } from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as HostProcess from "@t3tools/shared/HostProcess";
const SCRIPT_BYTE_CAP = 256 * 1024;
function scriptsRoot(home: string): string {
return NodePath.join(home, ".claude", "projects");
}
import { readContainedWorkflowFile } from "./workflowFileRead.ts";
export const readWorkflowScript = Effect.fn("orchestration.readWorkflowScript")(function* (input: {
readonly scriptPath: string;
}) {
const requested = input.scriptPath;
if (!NodePath.isAbsolute(requested) || NodePath.extname(requested) !== ".js") {
return yield* new OrchestrationGetWorkflowScriptError({
reason: "invalid-path",
scriptPath: requested,
});
}
const home = yield* HostProcess.HomeDirectory;
const root = yield* Effect.tryPromise({
try: () => NodeFSP.realpath(scriptsRoot(home)),
catch: (cause) =>
new OrchestrationGetWorkflowScriptError({
reason: "root-unavailable",
scriptPath: requested,
cause,
}),
});
// Realpath the FILE itself (not just its directory): a symlink named
// like a script inside a contained directory must not escape.
const resolved = yield* Effect.tryPromise({
try: () => NodeFSP.realpath(requested),
catch: (cause) =>
new OrchestrationGetWorkflowScriptError({
reason: "not-found",
scriptPath: requested,
cause,
}),
});
if (resolved !== root && !resolved.startsWith(`${root}${NodePath.sep}`)) {
return yield* new OrchestrationGetWorkflowScriptError({
reason: "outside-root",
scriptPath: resolved,
});
}
if (NodePath.extname(resolved) !== ".js") {
return yield* new OrchestrationGetWorkflowScriptError({
reason: "not-js",
scriptPath: resolved,
});
}
// TOCTOU-safe read (review finding): open FIRST, then verify what was
// actually opened via the file descriptor. Re-checking the path after
// open would race against a swap; fstat on the handle cannot. The two
// containment checks fail with their own tagged reasons (not manufactured
// Errors folded into read-failed); "read-failed" is reserved for genuine
// platform failures with the real cause attached.
const read = yield* Effect.tryPromise({
try: async () => {
const handle = await NodeFSP.open(resolved, "r");
try {
const stat = await handle.stat();
if (!stat.isFile()) {
return { failure: "not-regular-file" as const };
}
// The opened inode must be the same one realpath resolved to: a
// process swapping the path between realpath and open changes the
// inode, which this comparison catches.
const pathStat = await NodeFSP.lstat(resolved);
if (stat.ino !== pathStat.ino || stat.dev !== pathStat.dev) {
return { failure: "changed-during-read" as const };
}
const truncated = stat.size > SCRIPT_BYTE_CAP;
const buffer = Buffer.alloc(Math.min(stat.size, SCRIPT_BYTE_CAP));
const { bytesRead } = await handle.read(buffer, 0, buffer.length, 0);
return {
contents: buffer.subarray(0, bytesRead).toString("utf8"),
truncated,
};
} finally {
await handle.close();
}
},
catch: (cause) =>
new OrchestrationGetWorkflowScriptError({
reason: "read-failed",
scriptPath: resolved,
cause,
}),
});
if ("failure" in read) {
return yield* new OrchestrationGetWorkflowScriptError({
reason: read.failure,
scriptPath: resolved,
});
}
return {
scriptPath: resolved,
contents: read.contents,
truncated: read.truncated,
};
const file = yield* readContainedWorkflowFile({
path: input.scriptPath,
extension: ".js",
byteCap: 256 * 1024,
}).pipe(
Effect.mapError(
(error) =>
new OrchestrationGetWorkflowScriptError({
reason: error.reason === "wrong-extension" ? "not-js" : error.reason,
scriptPath: error.path,
cause: error,
}),
),
);
return { scriptPath: file.path, contents: file.contents, truncated: file.truncated };
});
@@ -212,6 +212,7 @@ const makeTestRelay = Effect.fnUntraced(function* (
settleAfterRun: unused,
settleThread: unused,
interruptThread: unused,
stopWorkflow: () => Effect.die("unused stopWorkflow"),
stopDelegatedTasks: unused,
getThreadEventSequence: unused,
recoverDelegatedTask: unused,
+1
View File
@@ -1864,6 +1864,7 @@ const layerWsRpc = (
),
),
),
[ORCHESTRATION_V2_WS_METHODS.stopWorkflow]: (input) => threadManagement.stopWorkflow(input),
[ORCHESTRATION_V2_WS_METHODS.getWorkflowScript]: (input) =>
readWorkflowScript({ scriptPath: input.scriptPath }),
[ORCHESTRATION_V2_WS_METHODS.getTurnItem]: (input) =>
@@ -0,0 +1,266 @@
import type {
SubagentWorkflowGroup,
RuntimeSubagent,
} from "@t3tools/client-runtime/state/subagentRuntime";
import type { ProviderDriverKind, ProviderInstanceId, ServerProvider } from "@t3tools/contracts";
import { ChevronDownIcon, LoaderCircleIcon, SquareIcon } from "lucide-react";
import { useState, type ReactNode } from "react";
import { AgentElapsed } from "./AgentElapsed";
import { SubagentTooltipContent } from "./SubagentTooltipContent";
import { ThreadHoverCardPopup } from "../ThreadHoverCard";
import { ThreadRelationshipIcon } from "./ThreadRelationshipIcon";
import { cn } from "../../lib/utils";
import { ThreadDetailsControl } from "./ThreadDetailsControl";
import { CollapsibleSectionHeader, SectionHeaderStatus } from "../ui/collapsible-section-header";
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
import {
THREAD_DETAILS_PANEL_SPLIT_GROUP_CLASS,
THREAD_DETAILS_PANEL_SPLIT_SEPARATOR_CLASS,
} from "./threadDetailsPanelStyles";
/**
* Wall clock for the phase: its members run in parallel, so the span from the
* first start to the last finish is the time the phase actually took. Shaped
* for AgentElapsed, which ticks it while the phase is still running.
*/
function phaseElapsed(phase: SubagentWorkflowGroup["phases"][number]) {
const instants = (key: "startedAt" | "completedAt") =>
phase.members
.map((member) => member[key])
.filter((value) => value !== null)
.sort();
const running = phase.state === "running";
const startedAt = instants("startedAt")[0] ?? null;
const completedAt = running ? null : (instants("completedAt").at(-1) ?? null);
return {
status: running ? ("running" as const) : ("completed" as const),
// A settled phase whose members never reported an end has no span to show.
startedAt: running || completedAt !== null ? startedAt : null,
completedAt,
};
}
/** Phase state for the header's label; the tone and member badges show it visually. */
function phaseStatusLabel(phase: SubagentWorkflowGroup["phases"][number]) {
if (phase.state === "running") return "running";
if (phase.members.some((member) => member.status === "failed")) return "failed";
if (
phase.members.some((member) => member.status === "cancelled" || member.status === "interrupted")
) {
return "stopped";
}
return phase.state === "done" ? "done" : "not started";
}
/** Members run under the coordinator's provider, so they share its glyph. */
function WorkflowMemberRow({
member,
providerInstanceId,
provider,
providers,
driver,
onOpen,
isThreadAvailable,
}: {
member: RuntimeSubagent;
providerInstanceId: ProviderInstanceId;
provider: ServerProvider | undefined;
providers: ReadonlyArray<ServerProvider> | undefined;
driver: ProviderDriverKind | undefined;
onOpen: (threadId: string) => void;
isThreadAvailable: (threadId: string) => boolean;
}) {
const threadId = member.childThreadId;
const canOpen = threadId !== null && isThreadAvailable(threadId);
return (
<li>
<Tooltip>
<TooltipTrigger
delay={200}
render={
<ThreadDetailsControl
part="row"
aria-label={`Open ${member.title} chat`}
disabled={!canOpen}
onClick={() => canOpen && onOpen(threadId)}
/>
}
>
<ThreadRelationshipIcon driver={driver} provider={provider} status={member.status} />
<span className="min-w-0 flex-1 truncate">{member.title}</span>
<span className="sr-only">{member.status}</span>
{member.startedAt ? (
<span className="shrink-0 text-2xs font-normal tabular-nums text-muted-foreground">
<AgentElapsed agent={member} />
</span>
) : null}
</TooltipTrigger>
<ThreadHoverCardPopup side="left">
<SubagentTooltipContent
title={member.title}
model={member.model}
providerInstanceId={providerInstanceId}
origin="provider_native"
provider={provider}
providers={providers}
driver={driver}
elapsed={<AgentElapsed agent={member} />}
status={member.status}
result={member.result ?? member.error}
progress={member.progress}
/>
</ThreadHoverCardPopup>
</Tooltip>
</li>
);
}
export function ThreadLineageWorkflowRow({
group,
header,
providerInstanceId,
provider,
providers,
driver,
onOpenThread,
isThreadAvailable,
onStop,
stopping,
stopDisabled,
}: {
readonly group: SubagentWorkflowGroup;
readonly header: ReactNode;
readonly providerInstanceId: ProviderInstanceId;
readonly provider: ServerProvider | undefined;
readonly providers: ReadonlyArray<ServerProvider> | undefined;
readonly driver: ProviderDriverKind | undefined;
readonly onOpenThread: (threadId: string) => void;
readonly isThreadAvailable: (threadId: string) => boolean;
readonly onStop?: (() => void) | undefined;
readonly stopping: boolean;
readonly stopDisabled: boolean;
}) {
const [expanded, setExpanded] = useState(false);
// The running phase opens itself until the user picks a side for it.
const [phaseOpen, setPhaseOpen] = useState<ReadonlyMap<number, boolean>>(() => new Map());
const label = group.workflow.workflowName ?? group.workflow.title;
const phases = group.phases.filter((phase) => phase.members.length > 0);
return (
// The flag lets the lineage list trade its compact height for the open tree.
<li className="group" data-workflow-expanded={expanded ? "" : undefined}>
<div className={cn("relative", THREAD_DETAILS_PANEL_SPLIT_GROUP_CLASS)}>
{header}
{onStop ? (
<div className="pointer-events-none absolute right-9 top-1/2 -translate-y-1/2 opacity-0 group-hover:pointer-events-auto group-hover:opacity-100 group-focus-within:pointer-events-auto group-focus-within:opacity-100 pointer-coarse:pointer-events-auto pointer-coarse:opacity-100 [@media(hover:none)]:pointer-events-auto [@media(hover:none)]:opacity-100">
<Tooltip>
<TooltipTrigger
render={
<ThreadDetailsControl
size="icon-xs"
variant="ghost"
part="icon"
tone="destructive"
aria-label={`Stop workflow ${label}`}
disabled={stopDisabled}
onClick={onStop}
/>
}
>
{stopping ? (
<LoaderCircleIcon aria-hidden className="size-3 animate-spin" />
) : (
<SquareIcon aria-hidden className="size-3 fill-current" />
)}
</TooltipTrigger>
<TooltipPopup side="left">Stop entire workflow</TooltipPopup>
</Tooltip>
</div>
) : null}
<span aria-hidden="true" className={THREAD_DETAILS_PANEL_SPLIT_SEPARATOR_CLASS} />
<ThreadDetailsControl
size="sm"
variant="ghost"
part="secondary"
aria-expanded={expanded}
aria-label={`${expanded ? "Collapse" : "Expand"} ${label}`}
onClick={() => setExpanded((value) => !value)}
>
<ChevronDownIcon
aria-hidden
className={cn(
"size-3.5 text-muted-foreground transition-transform",
expanded && "rotate-180",
)}
/>
</ThreadDetailsControl>
</div>
{expanded ? (
<ul className="m-0 list-none p-0 ps-6.5">
{phases.map((phase) => {
const open = phaseOpen.get(phase.index) ?? phase.state === "running";
const status = phaseStatusLabel(phase);
return (
<li key={phase.index}>
<CollapsibleSectionHeader
variant="panel"
tone={phase.state === "running" ? "emphasized" : "muted"}
expanded={open}
aria-label={`${open ? "Collapse" : "Expand"} ${phase.title} phase, ${status}`}
onClick={() => setPhaseOpen((phases) => new Map(phases).set(phase.index, !open))}
accessory={
<>
{status === "failed" ? (
<SectionHeaderStatus>Failed</SectionHeaderStatus>
) : null}
<span className="shrink-0 text-2xs font-normal tabular-nums">
{phase.settledCount}/{phase.members.length}{" "}
{phase.members.length === 1 ? "agent" : "agents"}
</span>
<span className="shrink-0 text-2xs font-normal tabular-nums empty:hidden">
<AgentElapsed agent={phaseElapsed(phase)} />
</span>
</>
}
>
{phase.title}
</CollapsibleSectionHeader>
{open ? (
<ul className="m-0 list-none p-0">
{phase.members.map((member) => (
<WorkflowMemberRow
key={member.id}
member={member}
providerInstanceId={providerInstanceId}
provider={provider}
providers={providers}
driver={driver}
onOpen={onOpenThread}
isThreadAvailable={isThreadAvailable}
/>
))}
</ul>
) : null}
</li>
);
})}
{group.unphasedMembers.map((member) => (
<WorkflowMemberRow
key={member.id}
member={member}
providerInstanceId={providerInstanceId}
provider={provider}
providers={providers}
driver={driver}
onOpen={onOpenThread}
isThreadAvailable={isThreadAvailable}
/>
))}
{phases.length === 0 && group.unphasedMembers.length === 0 ? (
<li className="px-2.5 py-1.5 text-2xs text-muted-foreground/70">No agents yet</li>
) : null}
</ul>
) : null}
</li>
);
}
@@ -18,6 +18,8 @@ const state = vi.hoisted(() => ({
configs: new Map<string, unknown>(),
showTooltips: false,
command: vi.fn().mockResolvedValue({ _tag: "Success" }),
workflowCommand: vi.fn().mockResolvedValue({ _tag: "Success" }),
toast: vi.fn(),
}));
vi.mock("@tanstack/react-router", () => ({ useNavigate: () => state.navigate }));
@@ -30,7 +32,25 @@ vi.mock("../../state/entities", () => ({
vi.mock("../../lib/archivedThreadsState", () => ({
useArchivedThreadSnapshots: () => ({ snapshots: [] }),
}));
vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: () => state.command }));
vi.mock("../../state/use-atom-command", () => ({
useAtomCommand: (command: { label: string }) =>
command.label === "environment-data:orchestration:stop-workflow"
? state.workflowCommand
: state.command,
}));
vi.mock("../ui/toast", () => ({ toastManager: { add: state.toast } }));
vi.mock("../../state/orchestration", async () => {
const { Atom } = await import("effect/reactivity");
const granted = Atom.make(true);
return {
orchestrationEnvironment: {
stopWorkflow: {
label: "environment-data:orchestration:stop-workflow",
permissionAtom: () => granted,
},
},
};
});
vi.mock("../ui/tooltip", () => ({
Tooltip: ({ children }: { children: ReactNode }) => children,
TooltipTrigger: ({ render, children }: { render: ReactElement; children: ReactNode }) =>
@@ -49,17 +69,423 @@ afterEach(async () => {
state.projects = [];
state.configs.clear();
state.showTooltips = false;
state.navigate.mockClear();
state.command.mockClear();
state.workflowCommand.mockReset().mockResolvedValue({ _tag: "Success" });
state.toast.mockClear();
state.projection = null;
});
it("stops the entire native workflow through its parent without changing state before acknowledgement", async () => {
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
const coordinator = {
id: "workflow",
origin: "provider_native",
driver: "claudeAgent",
providerInstanceId: "claude-work",
childThreadId: "workflow-chat",
title: "Checkout review",
status: "running",
startedAt: DateTime.makeUnsafe("2026-09-21T12:00:00Z"),
completedAt: null,
updatedAt: DateTime.makeUnsafe("2026-09-21T12:00:00Z"),
workflow: {
name: "Checkout review",
phases: [{ index: 0, title: "Inspect" }],
agents: [{ index: 0, label: "Checker", state: "running", phaseIndex: 0 }],
},
};
state.projection = {
thread: { id: "parent", lineage: { relationshipToParent: null } },
runs: [],
providerThreads: [],
providerSessions: [],
contextTransfers: [],
subagents: [
coordinator,
{
...coordinator,
id: "worker",
childThreadId: "worker-chat",
origin: "app_owned",
title: "Worker",
workflow: undefined,
},
],
};
state.shells = [
{
environmentId: "remote",
source: {
id: "workflow-chat",
title: "Coordinator chat",
lineage: { parentThreadId: "parent", relationshipToParent: "subagent" },
activityRunStatus: "waiting",
},
},
];
await act(async () => {
renderer = create(
<ThreadRelationshipsPanel
environmentId={EnvironmentId.make("remote")}
threadId={ThreadId.make("parent")}
/>,
);
});
await act(async () =>
renderer.root.findByProps({ "aria-label": "Expand Checkout review" }).props.onClick(),
);
const stopButton = () =>
renderer.root.findByProps({ "aria-label": "Stop workflow Checkout review" });
expect(
renderer.root.findAll(
(node) => node.type === "button" && String(node.props["aria-label"]).startsWith("Stop "),
),
).toHaveLength(2);
let acknowledge!: (result: { _tag: string }) => void;
state.workflowCommand.mockImplementationOnce(
() =>
new Promise((resolve) => {
acknowledge = resolve;
}),
);
await act(async () => {
stopButton().props.onClick();
});
expect(state.workflowCommand).toHaveBeenCalledExactlyOnceWith({
environmentId: "remote",
input: { threadId: "parent", subagentId: "workflow" },
});
expect(state.command).not.toHaveBeenCalled();
expect(state.navigate).not.toHaveBeenCalled();
expect(stopButton().props.disabled).toBe(true);
expect(renderer.root.findByProps({ "aria-label": "Stop subagent Worker" }).props.disabled).toBe(
true,
);
expect(renderer.root.findByProps({ "aria-label": "Open Checker chat" })).toBeDefined();
expect(renderer.root.findByType("h3").children).toEqual(["Lineage · 2 running"]);
await act(async () => {
acknowledge({ _tag: "Success" });
});
expect(stopButton().props.disabled).toBe(false);
expect(renderer.root.findByType("h3").children).toEqual(["Lineage · 2 running"]);
state.workflowCommand.mockResolvedValueOnce({ _tag: "Failure" });
await act(async () => {
stopButton().props.onClick();
});
expect(state.toast).toHaveBeenCalledExactlyOnceWith({
type: "error",
title: "Could not stop workflow",
});
expect(stopButton().props.disabled).toBe(false);
expect(state.command).not.toHaveBeenCalled();
expect(state.navigate).not.toHaveBeenCalled();
});
it.each([
{ driver: "codex", origin: "app_owned" },
{ driver: "claudeAgent", origin: "app_owned" },
{ driver: "claudeAgent", origin: "provider_native" },
["claudeAgent", "provider_native", "completed"],
["claudeAgent", "provider_native", "failed"],
["claudeAgent", "provider_native", "interrupted"],
["claudeAgent", "provider_native", "waiting"],
["codex", "provider_native", "running"],
["claudeAgent", "app_owned", "running"],
])("does not offer workflow stop for %s %s %s", async (driver, origin, status) => {
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
state.projection = {
thread: { id: "parent", lineage: { relationshipToParent: null } },
runs: [],
providerThreads: [],
providerSessions: [],
contextTransfers: [],
subagents: [
{
id: "workflow",
driver,
origin,
status,
providerInstanceId: "claude-work",
childThreadId: "workflow-chat",
title: "Checkout review",
startedAt: DateTime.makeUnsafe("2026-09-21T12:00:00Z"),
completedAt: null,
updatedAt: DateTime.makeUnsafe("2026-09-21T12:00:00Z"),
workflow: { name: "Checkout review", phases: [], agents: [] },
},
],
};
await act(async () => {
renderer = create(
<ThreadRelationshipsPanel
environmentId={EnvironmentId.make("remote")}
threadId={ThreadId.make("parent")}
/>,
);
});
const previousAgents = renderer.root.findAll(
(node) =>
node.type === "button" && node.props["aria-expanded"] === false && !node.props["aria-label"],
);
if (previousAgents.length > 0) await act(async () => previousAgents[0]!.props.onClick());
expect(renderer.root.findByProps({ "aria-label": "Expand Checkout review" })).toBeDefined();
expect(
renderer.root.findAllByProps({ "aria-label": "Stop workflow Checkout review" }),
).toHaveLength(0);
expect(state.workflowCommand).not.toHaveBeenCalled();
});
it("opens the correct chat for every workflow phase and unphased member", async () => {
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
state.showTooltips = true;
state.configs.set("remote", {
providers: [
{
instanceId: "claude-personal",
driver: "claudeAgent",
displayName: "Personal account",
models: [],
},
{
instanceId: "claude-work",
driver: "claudeAgent",
displayName: "Work account",
models: [{ slug: "claude-sonnet-4-6", name: "Sonnet" }],
},
],
});
const phases = ["Inspect", "Improve", "Verify"].map((title, index) => ({ index, title }));
const agents = Array.from({ length: 7 }, (_, index) => ({
index,
label: `Member ${index}`,
state:
index === 2 ? "cancelled" : index === 4 ? "failed" : index === 5 ? "running" : "completed",
...(index < 6 ? { phaseIndex: Math.floor(index / 2) } : {}),
childThreadId: `member-chat-${index}`,
model: "claude-sonnet-4-6",
result: `Result ${index}`,
totalTokens: 1200,
// Staggered starts, so a phase spans longer than either member's own run.
startedAt: 1_700_000_000_000 + index * 1000,
durationMs: 5000,
}));
const project = (members: typeof agents) => ({
thread: { id: "parent", lineage: { relationshipToParent: null } },
runs: [],
providerThreads: [],
providerSessions: [],
contextTransfers: [],
subagents: [
{
id: "workflow",
driver: "claudeAgent",
providerInstanceId: "claude-work",
childThreadId: "workflow-chat",
title: "Checkout review",
status: "running",
startedAt: null,
completedAt: null,
updatedAt: DateTime.makeUnsafe("2026-09-21T12:00:00Z"),
workflow: { name: "Checkout review", phases, agents: members },
},
],
});
state.projection = project(agents);
// Matching IDs from another environment cannot make these chats available.
const memberShells = agents.map((agent) => ({
id: agent.childThreadId,
title: agent.label,
lineage: { parentThreadId: "workflow-chat", relationshipToParent: "subagent" },
status: "completed",
activityRunStatus: null,
}));
state.shells = memberShells.map((source) => ({ environmentId: "other", source }));
const panel = (
<ThreadRelationshipsPanel
environmentId={EnvironmentId.make("remote")}
threadId={ThreadId.make("parent")}
/>
);
await act(async () => {
renderer = create(panel);
});
await act(async () =>
renderer.root.findByProps({ "aria-label": "Expand Checkout review" }).props.onClick(),
);
const memberButtons = () =>
renderer.root.findAll(
(node) =>
node.type === "button" && String(node.props["aria-label"]).startsWith("Open Member"),
);
const phaseButtons = () =>
renderer.root.findAll(
(node) =>
node.type === "button" &&
typeof node.props["aria-expanded"] === "boolean" &&
String(node.props["aria-label"]).includes(" phase, "),
);
// Only the running phase starts open, beside the unphased member.
expect(memberButtons()).toHaveLength(3);
expect(phaseButtons()).toHaveLength(3);
expect(renderer.root.findByProps({ "aria-label": "Expand Inspect phase, done" })).toBeDefined();
expect(
renderer.root.findByProps({ "aria-label": "Expand Improve phase, stopped" }),
).toBeDefined();
expect(
renderer.root.findByProps({ "aria-label": "Collapse Verify phase, running" }),
).toBeDefined();
for (const phase of phaseButtons().slice(0, 2)) await act(async () => phase.props.onClick());
expect(memberButtons()).toHaveLength(7);
for (const button of memberButtons()) {
expect(button.props.disabled).toBe(true);
await act(async () => button.props.onClick());
}
expect(state.navigate).not.toHaveBeenCalled();
// The coordinator roster arrives before the member thread shells.
state.shells = memberShells.map((source) => ({ environmentId: "remote", source }));
await act(async () => renderer.update(cloneElement(panel)));
for (const button of memberButtons()) expect(button.props.disabled).toBe(false);
for (const agent of agents) {
await act(async () =>
renderer.root.findByProps({ "aria-label": `Open ${agent.label} chat` }).props.onClick(),
);
expect(state.navigate).toHaveBeenLastCalledWith({
to: "/$environmentId/$threadId",
params: { environmentId: "remote", threadId: agent.childThreadId },
});
}
const rendered = JSON.stringify(renderer.toJSON());
expect(rendered.match(/Sonnet · Work account/g)).toHaveLength(7);
expect(rendered).not.toContain("Personal account");
for (const phase of phases) expect(rendered).toContain(phase.title);
// The closed row leaves phase progress to the tree.
expect(rendered).not.toContain(" phases");
expect(rendered).toContain('"1","/","2"," ","agents"');
expect(rendered).toContain("Running");
// The phase holding the running member reports itself as the active one.
expect(rendered).toContain("running");
expect(rendered).toContain("done");
expect(rendered).toContain("stopped");
// Members 0 and 1 start a second apart and run 5s each, so Inspect took 6s.
expect(rendered).toContain('"6s"');
for (const phase of phaseButtons()) {
const label = String(phase.props["aria-label"]).slice("Collapse ".length);
await act(async () => phase.props.onClick());
expect(memberButtons()).toHaveLength(5);
await act(async () =>
renderer.root.findByProps({ "aria-label": `Expand ${label}` }).props.onClick(),
);
expect(memberButtons()).toHaveLength(7);
}
// A phase the user closed stays closed once it starts running, and stays
// closed after it settles.
await act(async () => phaseButtons()[0]!.props.onClick());
expect(memberButtons()).toHaveLength(5);
const restarted = agents.map((agent) =>
agent.index === 0 ? { ...agent, state: "running" } : agent,
);
state.projection = project(restarted);
await act(async () => renderer.update(cloneElement(panel)));
expect(memberButtons()).toHaveLength(5);
state.projection = project(agents);
await act(async () => renderer.update(cloneElement(panel)));
expect(memberButtons()).toHaveLength(5);
await act(async () =>
renderer.root.findByProps({ "aria-label": "Collapse Checkout review" }).props.onClick(),
);
expect(memberButtons()).toHaveLength(0);
expect(state.navigate).toHaveBeenCalledTimes(7);
});
it("groups and counts a workflow by its run status instead of its coordinator chat", async () => {
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
state.showTooltips = true;
const coordinator = {
id: "workflow",
driver: "claudeAgent",
providerInstanceId: "claudeAgent",
childThreadId: "workflow-chat",
title: "Checkout review",
status: "running",
startedAt: DateTime.makeUnsafe("2026-09-21T12:00:00Z"),
progress: "Inspecting the checkout workflow",
result: "Workflow checks complete",
completedAt: null,
updatedAt: DateTime.makeUnsafe("2026-09-21T12:00:00Z"),
workflow: {
name: "Checkout review",
phases: [{ index: 0, title: "Inspect" }],
agents: [{ index: 0, label: "Checker", state: "running", phaseIndex: 0 }],
},
};
const projection = {
thread: { id: "parent", lineage: { relationshipToParent: null } },
runs: [],
providerThreads: [],
providerSessions: [],
contextTransfers: [],
subagents: [coordinator],
};
const child = {
id: "workflow-chat",
title: "Coordinator chat",
lineage: { parentThreadId: "parent", relationshipToParent: "subagent" },
status: "completed",
activityRunStatus: null,
};
state.projection = projection;
state.shells = [{ environmentId: "test", source: child }];
const panel = (
<ThreadRelationshipsPanel
environmentId={EnvironmentId.make("test")}
threadId={ThreadId.make("parent")}
/>
);
await act(async () => {
renderer = create(panel);
});
const text = () =>
renderer.root
.findAll((node) => typeof node.type === "string")
.flatMap((node) => node.children.filter((child) => typeof child === "string"))
.join("");
expect(renderer.root.findByType("h3").children).toEqual(["Lineage · 1 running"]);
await act(async () =>
renderer.root.findByProps({ "aria-label": "Expand Checkout review" }).props.onClick(),
);
expect(renderer.root.findByProps({ "aria-label": "Open Checker chat" })).toBeDefined();
state.shells = [{ environmentId: "test", source: { ...child, activityRunStatus: "waiting" } }];
await act(async () => renderer.update(cloneElement(panel)));
expect(renderer.root.findByType("h3").children).toEqual(["Lineage · 1 running"]);
expect(text()).toContain("Inspecting the checkout workflow");
state.projection = {
...projection,
subagents: [
{
...coordinator,
status: "completed",
completedAt: DateTime.makeUnsafe("2026-09-21T12:10:00Z"),
},
],
};
await act(async () => renderer.update(cloneElement(panel)));
expect(renderer.root.findByType("h3").children).toEqual(["Lineage"]);
expect(text()).toContain("Previous agents");
expect(renderer.root.findAllByProps({ "aria-label": "Open Checker chat" })).toHaveLength(0);
await act(async () =>
renderer.root.findByProps({ type: "button", "aria-expanded": false }).props.onClick(),
);
expect(renderer.root.findByProps({ "aria-label": "Expand Checkout review" })).toBeDefined();
expect(text()).toContain("Workflow checks complete");
expect(text()).toContain("10m");
});
it.each([
{ driver: "codex", origin: "app_owned", runId: null, canStop: true },
{ driver: "claudeAgent", origin: "app_owned", runId: null, canStop: true },
{ driver: "claudeAgent", origin: "provider_native", runId: "run-1", canStop: true },
{ driver: "claudeAgent", origin: "provider_native", runId: null, canStop: false },
])(
"stops active $origin $driver subagents without opening their thread",
async ({ driver, origin }) => {
"offers individual Stop for supported $origin $driver subagents with run $runId",
async ({ driver, origin, runId, canStop }) => {
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
const parent = {
id: "parent",
@@ -76,6 +502,7 @@ it.each([
childThreadId: "child",
origin,
threadId: "parent",
runId,
nativeTaskRef: { driver, nativeId: "claude-task", strength: "strong" },
driver,
providerInstanceId: "codex",
@@ -108,6 +535,12 @@ it.each([
await act(async () => {
renderer = create(panel);
});
if (!canStop) {
expect(renderer.root.findAllByProps({ "aria-label": "Stop subagent Worker" })).toHaveLength(
0,
);
return;
}
const stopButton = () => renderer.root.findByProps({ "aria-label": "Stop subagent Worker" });
await act(async () => stopButton().props.onClick());
expect(state.command).toHaveBeenCalledWith({
@@ -397,11 +830,16 @@ it("shows readable models and only differing workspace details in agent tooltips
};
return read(renderer.root);
};
expect(text()).toContain("My GPT · high");
state.projection = {
const rerender = async (source = child) => {
state.shells = [{ environmentId: "test", source: { ...source } }];
await act(async () => renderer.update(cloneElement(panel)));
};
const nativeProjection = {
...projection,
subagents: [{ ...projection.subagents[0], origin: "provider_native" }],
};
expect(text()).toContain("My GPT · high");
state.projection = nativeProjection;
await act(async () => renderer.update(cloneElement(panel)));
expect(text()).toContain("My GPT");
expect(text()).not.toContain("My GPT · high");
@@ -467,8 +905,7 @@ it("shows readable models and only differing workspace details in agent tooltips
[undefined, ""],
] as const) {
child.modelSelection.options = options;
state.shells = [{ environmentId: "test", source: { ...child } }];
await act(async () => renderer.update(cloneElement(panel)));
await rerender();
expect(text()).toContain(`My GPT${expected}`);
if (!expected) expect(text()).not.toContain("My GPT ·");
}
@@ -532,8 +969,7 @@ it("shows readable models and only differing workspace details in agent tooltips
{ id: "reasoningEffort", value: "high" },
...(value === undefined ? [] : [{ id: descriptor.id, value }]),
];
state.shells = [{ environmentId: "test", source: { ...child } }];
await act(async () => renderer.update(cloneElement(panel)));
await rerender();
expect(text(true)).toContain("My GPT · high");
expect(text(true)).not.toMatch(/Fast|Ultrafast|Normal|Standard|Flex/);
expect(text()).toContain(`My GPT · ${iconLabel}high`);
@@ -541,27 +977,11 @@ it("shows readable models and only differing workspace details in agent tooltips
child.modelSelection.options = child.modelSelection.options.filter(
({ id }) => id !== "reasoningEffort",
);
state.shells = [{ environmentId: "test", source: { ...child } }];
await act(async () => renderer.update(cloneElement(panel)));
await rerender();
expect(text()).not.toContain(" · high");
expect(text(true)).not.toMatch(/Fast|Ultrafast|Normal|Standard|Flex/);
if (iconLabel) expect(text()).toContain(iconLabel);
else expect(text()).not.toContain("mode on");
state.projection = {
...projection,
subagents: [{ ...projection.subagents[0], origin: "provider_native" }],
};
await act(async () => renderer.update(cloneElement(panel)));
expect(text()).not.toMatch(/Fast|Ultrafast|Normal|Standard|Flex| · high/);
state.projection = projection;
for (const modelSelection of [
{ ...child.modelSelection, instanceId: "other" },
{ ...child.modelSelection, model: "gpt-5.5" },
]) {
state.shells = [{ environmentId: "test", source: { ...child, modelSelection } }];
await act(async () => renderer.update(cloneElement(panel)));
expect(text()).not.toMatch(/Fast|Ultrafast|Normal|Standard|Flex| · high/);
}
}
state.configs.set("test", {
providers: [
@@ -589,11 +1009,21 @@ it("shows readable models and only differing workspace details in agent tooltips
{ id: "reasoningEffort", value: "high" },
{ id: "serviceTier", value: "priority" },
];
state.shells = [{ environmentId: "test", source: { ...child } }];
await act(async () => renderer.update(cloneElement(panel)));
await rerender();
expect(text(true)).toContain("My GPT · Work account · high");
expect(text()).toContain("My GPT · Work account · Fast mode onhigh");
expect(text()).not.toContain("Personal account");
state.projection = nativeProjection;
await act(async () => renderer.update(cloneElement(panel)));
expect(text()).not.toMatch(/Fast|Ultrafast|Normal|Standard|Flex| · high/);
state.projection = projection;
for (const modelSelection of [
{ ...child.modelSelection, instanceId: "other" },
{ ...child.modelSelection, model: "gpt-5.5" },
]) {
await rerender({ ...child, modelSelection });
expect(text()).not.toMatch(/Fast|Ultrafast|Normal|Standard|Flex| · high/);
}
state.shells = [];
for (const status of ["running", "completed", "failed"] as const) {
state.projection = {
@@ -629,21 +1059,14 @@ it("shows readable models and only differing workspace details in agent tooltips
state.projection = projection;
state.configs.set("test", speedConfig);
child.modelSelection.options = [{ id: "reasoningEffort", value: "high" }];
state.shells = [
{
environmentId: "test",
source: { ...child, modelSelection: { ...child.modelSelection, instanceId: "other" } },
},
];
await act(async () => renderer.update(cloneElement(panel)));
await rerender({ ...child, modelSelection: { ...child.modelSelection, instanceId: "other" } });
expect(text()).not.toContain("My GPT ·");
const config = state.configs.get("test");
state.configs.clear();
await act(async () => renderer.update(cloneElement(panel)));
expect(text()).toContain("GPT-5.4");
expect(text()).not.toContain("GPT-5.4 ·");
state.shells = [{ environmentId: "test", source: { ...child } }];
await act(async () => renderer.update(cloneElement(panel)));
await rerender();
expect(text()).toContain("GPT-5.4 · high");
state.configs.set("test", config);
for (const model of ["custom/model-v1", "custom/model-v2"]) {
@@ -651,13 +1074,10 @@ it("shows readable models and only differing workspace details in agent tooltips
...projection,
subagents: [{ ...projection.subagents[0], model }],
};
state.shells = [
{
environmentId: "test",
source: { ...child, modelSelection: { ...child.modelSelection, model: "custom/model-v1" } },
},
];
await act(async () => renderer.update(cloneElement(panel)));
await rerender({
...child,
modelSelection: { ...child.modelSelection, model: "custom/model-v1" },
});
expect(text().includes(`${model} · high`)).toBe(model === "custom/model-v1");
}
@@ -692,16 +1112,14 @@ it("shows readable models and only differing workspace details in agent tooltips
state.projection = projection;
child.worktreePath = "/main/worktrees/checker";
state.shells = [{ environmentId: "test", source: { ...child } }];
await act(async () => renderer.update(cloneElement(panel)));
await rerender();
expect(text()).toContain("Worktree");
expect(text()).toContain("checker");
expect(text()).not.toContain("/main/worktrees");
expect(text()).not.toContain("Project");
child.branch = "fix/checker";
state.shells = [{ environmentId: "test", source: { ...child } }];
await act(async () => renderer.update(cloneElement(panel)));
await rerender();
expect(text()).toContain("Branch");
expect(text()).toContain("fix/checker");
expect(text()).not.toContain("Worktree");
@@ -710,8 +1128,7 @@ it("shows readable models and only differing workspace details in agent tooltips
child.projectId = "other";
child.worktreePath = null;
child.branch = null;
state.shells = [{ environmentId: "test", source: { ...child } }];
await act(async () => renderer.update(cloneElement(panel)));
await rerender();
expect(text()).toContain("Other project");
expect(text()).toContain("Workspace");
expect(text()).toContain("other");
@@ -1,3 +1,4 @@
import { useAtomValue } from "@effect/atom-react";
import { ThreadDetailsControl } from "./ThreadDetailsControl";
import { ThreadHoverCardPopup } from "../ThreadHoverCard";
import { ThreadDetailsSection } from "./ThreadDetailsSection";
@@ -6,6 +7,7 @@ import { SubagentTooltipContent } from "./SubagentTooltipContent";
import { PullRequestGlyph } from "../pullRequest/pullRequestIcons";
import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment";
import {
deriveWorkflowGroups,
projectedSubagentsToRuntime,
type RuntimeSubagent,
} from "@t3tools/client-runtime/state/subagentRuntime";
@@ -24,11 +26,12 @@ import {
canDetachThreadProviderSession,
resolveLatestMergeBackRun,
} from "@t3tools/client-runtime/state/thread-workflows";
import type {
EnvironmentId,
OrchestrationV2Subagent,
OrchestrationV2ThreadShell,
ThreadId,
import {
NodeId,
type EnvironmentId,
type OrchestrationV2Subagent,
type OrchestrationV2ThreadShell,
type ThreadId,
} from "@t3tools/contracts";
import { deriveSubagentElapsedMs } from "@t3tools/shared/orchestrationTiming";
import { groupBy } from "effect/Array";
@@ -56,9 +59,11 @@ import {
useThreadShells,
} from "../../state/entities";
import { threadEnvironment } from "../../state/threads";
import { orchestrationEnvironment } from "../../state/orchestration";
import { useAtomCommand } from "../../state/use-atom-command";
import { AgentElapsed } from "./AgentElapsed";
import { ThreadRelationshipIcon, threadRelationshipStatusLabel } from "./ThreadRelationshipIcon";
import { ThreadLineageWorkflowRow } from "./ThreadLineageWorkflowRow";
import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../ui/menu";
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
@@ -97,11 +102,13 @@ export function ThreadLineageRowList(props: {
component: this sits inside an already scrolling panel, where a
max-height-only virtual viewport measures badly. Every row is a focusable
button, so keyboard users reach and scroll the region through the rows
themselves and the container needs no extra tab stop of its own.
themselves and the container needs no extra tab stop of its own. An
expanded workflow asks for a taller window, so it raises the bound while
its tree is open.
*/}
<ul
aria-label="Related threads"
className="m-0 max-h-[13.5rem] list-none overflow-y-auto overscroll-contain p-0"
className="m-0 max-h-[13.5rem] list-none overflow-y-auto overscroll-contain p-0 has-data-workflow-expanded:max-h-[min(28rem,55dvh)]"
>
{props.children}
</ul>
@@ -136,6 +143,7 @@ function ThreadLineageGroup(props: {
<div>
{props.label ? (
<CollapsibleSectionHeader
variant="panel"
expanded={expanded}
onClick={() => setExpanded(!expanded)}
accessory={
@@ -217,28 +225,39 @@ export function ThreadRelationshipsPanel(props: {
const ref = scopeThreadRef(props.environmentId, props.threadId);
const projection = useThreadProjection(ref)?.projection ?? null;
const providers = useServerConfigs().get(props.environmentId)?.providers;
const subagentsByThreadId = useMemo(
() =>
new Map(
(projection?.subagents ?? [])
.filter((subagent) => subagent.childThreadId !== null)
.map((subagent) => [
subagent.childThreadId,
{
...projectedSubagentsToRuntime([subagent])[0]!,
id: subagent.id,
threadId: subagent.threadId,
nativeTaskRef: subagent.nativeTaskRef,
nativeStatus: subagent.status,
driver: subagent.driver,
providerInstanceId: subagent.providerInstanceId,
origin: subagent.origin,
modelSelection: subagent.modelSelection,
},
]),
),
const runtimeSubagents = useMemo(
() => projectedSubagentsToRuntime(projection?.subagents ?? []),
[projection?.subagents],
);
const subagentsByThreadId = useMemo(() => {
const byId = new Map(runtimeSubagents.map((agent) => [agent.id, agent]));
return new Map(
(projection?.subagents ?? [])
.filter((subagent) => subagent.childThreadId !== null)
.map((subagent) => [
subagent.childThreadId,
{
...byId.get(subagent.id)!,
id: subagent.id,
threadId: subagent.threadId,
runId: subagent.runId,
nativeTaskRef: subagent.nativeTaskRef,
nativeStatus: subagent.status,
driver: subagent.driver,
providerInstanceId: subagent.providerInstanceId,
origin: subagent.origin,
modelSelection: subagent.modelSelection,
},
]),
);
}, [projection?.subagents, runtimeSubagents]);
// A workflow coordinator is already a subagent row here; the grouped model
// is what lets that row unfold into the phases and members it ran.
const workflowGroupsById = useMemo(
() =>
new Map(deriveWorkflowGroups(runtimeSubagents).map((group) => [group.workflow.id, group])),
[runtimeSubagents],
);
const threadShells = useThreadShells();
const projects = useProjects().filter((project) => project.environmentId === props.environmentId);
const archived = useArchivedThreadSnapshots([props.environmentId]);
@@ -260,8 +279,12 @@ export function ThreadRelationshipsPanel(props: {
const mergeBack = useAtomCommand(threadEnvironment.mergeBack);
const stopSession = useAtomCommand(threadEnvironment.stopSession);
const interruptTurn = useAtomCommand(threadEnvironment.interruptTurn);
const stopWorkflowCommand = useAtomCommand(orchestrationEnvironment.stopWorkflow);
const canOperateWorkflow = useAtomValue(
orchestrationEnvironment.stopWorkflow.permissionAtom(props.environmentId),
);
const [busyAction, setBusyAction] = useState<"merge" | "detach" | null>(null);
const [stoppingThreadId, setStoppingThreadId] = useState<ThreadId | null>(null);
const [stoppingId, setStoppingId] = useState<string | null>(null);
const latestMergeBackRun = projection === null ? null : resolveLatestMergeBackRun(projection);
const mergeTargetThreadId = resolveMergeBackTargetThreadId(projection);
const relationshipRows = useMemo(
@@ -271,8 +294,17 @@ export function ThreadRelationshipsPanel(props: {
rows: immediateThreadRelationships(graph, props.threadId),
currentThreadId: props.threadId,
mergeTargetThreadId,
}).map((row) => {
const agent =
row.edge.kind === "subagent" && !isParentThreadRelationship(row.edge, props.threadId)
? subagentsByThreadId.get(row.threadId)
: undefined;
const workflowGroup = agent === undefined ? undefined : workflowGroupsById.get(agent.id);
return workflowGroup === undefined
? row
: { ...row, edge: { ...row.edge, status: workflowGroup.workflow.status } };
}),
[graph, mergeTargetThreadId, props.threadId],
[graph, mergeTargetThreadId, props.threadId, subagentsByThreadId, workflowGroupsById],
);
const canMerge = mergeTargetThreadId !== null && latestMergeBackRun !== null;
const canDetach = projection ? canDetachThreadProviderSession(projection) : false;
@@ -341,8 +373,8 @@ export function ThreadRelationshipsPanel(props: {
childThreadId: ThreadId,
agent: Pick<OrchestrationV2Subagent, "id" | "origin" | "threadId">,
) => {
if (stoppingThreadId !== null) return;
setStoppingThreadId(childThreadId);
if (stoppingId !== null) return;
setStoppingId(childThreadId);
const result = await interruptTurn({
environmentId: props.environmentId,
input:
@@ -350,12 +382,25 @@ export function ThreadRelationshipsPanel(props: {
? { threadId: agent.threadId, subagentId: agent.id }
: { threadId: childThreadId },
});
setStoppingThreadId(null);
setStoppingId(null);
if (result._tag === "Failure") {
toastManager.add({ type: "error", title: "Could not stop subagent" });
}
};
const stopWorkflow = async (subagentId: string) => {
if (stoppingId !== null) return;
setStoppingId(subagentId);
const result = await stopWorkflowCommand({
environmentId: props.environmentId,
input: { threadId: props.threadId, subagentId: NodeId.make(subagentId) },
});
setStoppingId(null);
if (result._tag === "Failure") {
toastManager.add({ type: "error", title: "Could not stop workflow" });
}
};
const parentTitle =
mergeTargetThreadId === null
? null
@@ -400,33 +445,41 @@ export function ThreadRelationshipsPanel(props: {
const isSubagent = edge.kind === "subagent";
const isMergeTarget = threadId === mergeTargetThreadId;
const isParent = isParentThreadRelationship(edge, props.threadId);
const status = threadRelationshipRowStatus(graph, { threadId, edge });
const rowStatus = threadRelationshipRowStatus(graph, { threadId, edge });
const RelationshipIcon = isParent
? CornerLeftUpIcon
: isSubagent
? BotIcon
: GitForkIcon;
const relationship = relationshipLabel(edge, props.threadId);
const agent = currentSubagent(
isSubagent && !isParent ? subagentsByThreadId.get(threadId) : undefined,
node?.thread,
);
const failed = status === "failed" || status === "error";
const projectedAgent =
isSubagent && !isParent ? subagentsByThreadId.get(threadId) : undefined;
const workflowGroup =
projectedAgent === undefined
? undefined
: workflowGroupsById.get(projectedAgent.id);
const agent = workflowGroup
? projectedAgent
: currentSubagent(projectedAgent, node?.thread);
const failed = rowStatus === "failed" || rowStatus === "error";
const canStop =
agent &&
(agent.origin === "app_owned" ||
(agent.origin === "provider_native" &&
agent.driver === "claudeAgent" &&
agent.runId != null &&
agent.nativeTaskRef?.strength === "strong" &&
agent.nativeTaskRef.nativeId !== null &&
["pending", "running", "waiting"].includes(agent.nativeStatus))) &&
agent.startedAt &&
["pending", "running", "waiting"].includes(agent.status);
const trailingVisibilityClass = canStop
? "group-hover:opacity-0 group-focus-within:opacity-0 pointer-coarse:opacity-0 [@media(hover:none)]:opacity-0"
: "";
const threadTitle = relationshipThreadTitle({
title: node?.thread?.title ?? agent?.title ?? threadId,
// A workflow's name is shorter than its child thread's title.
title:
workflowGroup?.workflow.workflowName ??
node?.thread?.title ??
agent?.title ??
threadId,
isSubagent,
});
const provider = providers?.find(
@@ -435,6 +488,14 @@ export function ThreadRelationshipsPanel(props: {
(agent?.providerInstanceId ?? node?.thread?.providerInstanceId),
);
const providerDriver = agent?.driver ?? provider?.driver;
const canStopWorkflow =
canOperateWorkflow &&
workflowGroup?.workflow.status === "running" &&
agent?.origin === "provider_native" &&
providerDriver === "claudeAgent";
const trailingVisibilityClass = (workflowGroup ? canStopWorkflow : canStop)
? "group-hover:opacity-0 group-focus-within:opacity-0 pointer-coarse:opacity-0 [@media(hover:none)]:opacity-0"
: "";
const project = projects.find((project) => project.id === node?.thread?.projectId);
const relationshipHint = node?.missing
? "This related thread is unavailable"
@@ -468,7 +529,7 @@ export function ThreadRelationshipsPanel(props: {
driver={isSubagent && !isParent ? providerDriver : undefined}
provider={provider}
fallbackIcon={RelationshipIcon}
status={status}
status={rowStatus}
/>
<span className="min-w-0 flex-1">
<span className="block truncate text-left text-sm font-medium leading-4 text-foreground/85">
@@ -478,23 +539,68 @@ export function ThreadRelationshipsPanel(props: {
{agent ? null : (
<ArrowRightIcon className="size-3 shrink-0 text-muted-foreground opacity-0 transition-opacity group-hover:opacity-100" />
)}
{/* One trailing item keeps room for the title: the icon dot already
carries status, so an agent with a known time shows only that. */}
{agent && !failed && deriveSubagentElapsedMs(agent, 0) !== null ? (
{agent &&
!failed &&
(deriveSubagentElapsedMs(agent, 0) !== null || canStopWorkflow) ? (
<span
className={`shrink-0 text-2xs font-normal tabular-nums text-muted-foreground ${trailingVisibilityClass}`}
className={`shrink-0 text-2xs font-normal tabular-nums text-muted-foreground ${canStopWorkflow ? "min-w-6 text-end" : ""} ${trailingVisibilityClass}`}
>
<AgentElapsed agent={agent} compact />
</span>
) : !isMergeTarget ? (
<span
className={`shrink-0 text-2xs ${failed ? "text-destructive" : "text-muted-foreground"} ${trailingVisibilityClass}`}
className={
workflowGroup
? "sr-only"
: `shrink-0 text-2xs ${failed ? "text-destructive" : "text-muted-foreground"} ${trailingVisibilityClass}`
}
>
{threadRelationshipStatusLabel(status)}
{threadRelationshipStatusLabel(rowStatus)}
</span>
) : null}
</>
);
// A workflow pairs this row with a disclosure, so it renders as the
// leading half of a split row; everything else renders it directly.
const relationshipLink = (
<Tooltip>
<TooltipTrigger
delay={200}
render={
<ThreadDetailsControl
size="sm"
variant="ghost"
part={workflowGroup ? "primary" : "row"}
disabled={node?.missing === true}
onClick={() => openThread(threadId)}
/>
}
>
{relationshipContent}
</TooltipTrigger>
<RelationshipPopup side="left">{relationshipTooltip}</RelationshipPopup>
</Tooltip>
);
if (workflowGroup && agent) {
return (
<ThreadLineageWorkflowRow
key={threadId}
group={workflowGroup}
providerInstanceId={agent.providerInstanceId}
provider={provider}
providers={providers}
driver={providerDriver}
onOpenThread={(memberThreadId) => openThread(memberThreadId as ThreadId)}
isThreadAvailable={(memberThreadId) =>
graph.nodes.get(memberThreadId as ThreadId)?.missing === false
}
onStop={canStopWorkflow ? () => void stopWorkflow(agent.id) : undefined}
stopping={stoppingId === agent.id}
stopDisabled={stoppingId !== null}
header={relationshipLink}
/>
);
}
return (
<li key={threadId} className="group relative flex h-8 items-center rounded-lg">
{isMergeTarget ? (
@@ -507,7 +613,7 @@ export function ThreadRelationshipsPanel(props: {
size="sm"
variant="ghost"
part="primary"
aria-label={`${threadTitle} ${threadRelationshipStatusLabel(status)}`}
aria-label={`${threadTitle} ${threadRelationshipStatusLabel(rowStatus)}`}
disabled={node?.missing === true}
onClick={() => openThread(threadId)}
/>
@@ -553,27 +659,11 @@ export function ThreadRelationshipsPanel(props: {
</TooltipPopup>
</Tooltip>
<span className="shrink-0 border border-transparent ps-1 pe-2.5 text-2xs font-medium text-muted-foreground">
{threadRelationshipStatusLabel(status)}
{threadRelationshipStatusLabel(rowStatus)}
</span>
</div>
) : (
<Tooltip>
<TooltipTrigger
delay={200}
render={
<ThreadDetailsControl
size="sm"
variant="ghost"
disabled={node?.missing === true}
onClick={() => openThread(threadId)}
part="row"
/>
}
>
{relationshipContent}
</TooltipTrigger>
<RelationshipPopup side="left">{relationshipTooltip}</RelationshipPopup>
</Tooltip>
relationshipLink
)}
{canStop && agent ? (
<div className="pointer-events-none absolute right-1 top-1/2 -translate-y-1/2 opacity-0 group-hover:pointer-events-auto group-hover:opacity-100 group-focus-within:pointer-events-auto group-focus-within:opacity-100 pointer-coarse:pointer-events-auto pointer-coarse:opacity-100 [@media(hover:none)]:pointer-events-auto [@media(hover:none)]:opacity-100">
@@ -586,12 +676,12 @@ export function ThreadRelationshipsPanel(props: {
part="icon"
tone="destructive"
aria-label={`Stop subagent ${threadTitle}`}
disabled={stoppingThreadId !== null}
disabled={stoppingId !== null}
onClick={() => void stopSubagent(threadId, agent)}
/>
}
>
{stoppingThreadId === threadId ? (
{stoppingId === threadId ? (
<LoaderCircleIcon aria-hidden className="size-3 animate-spin" />
) : (
<SquareIcon aria-hidden className="size-3 fill-current" />
@@ -14,11 +14,13 @@ export function CollapsibleSectionHeader({
children,
expanded,
tone = "muted",
variant = "default",
accessory,
...buttonProps
}: Omit<ComponentProps<"button">, "className" | "style" | "aria-expanded"> & {
expanded: boolean;
tone?: keyof typeof tones;
variant?: "default" | "panel";
accessory?: ReactNode;
}) {
return (
@@ -27,7 +29,8 @@ export function CollapsibleSectionHeader({
type="button"
aria-expanded={expanded}
className={cn(
"flex h-8 w-full cursor-pointer items-center gap-2 rounded-md px-2 text-left text-xs font-medium focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring/70",
"flex h-8 w-full cursor-pointer items-center gap-2 rounded-md text-left text-xs font-medium focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring/70",
variant === "panel" ? "ps-2 pe-2.5" : "px-2",
tones[tone].label,
)}
>
@@ -44,6 +44,10 @@ export function createOrchestrationEnvironmentAtoms<R, E>(
staleTimeMs: 300_000,
idleTtlMs: 300_000,
}),
stopWorkflow: createEnvironmentRpcCommand(runtime, {
label: "environment-data:orchestration:stop-workflow",
tag: ORCHESTRATION_V2_WS_METHODS.stopWorkflow,
}),
// Keyed by the item revision, so a live row refetches as its output grows.
turnItem: createEnvironmentRpcQueryAtomFamily(runtime, {
label: "environment-data:orchestration:turn-item",
@@ -0,0 +1,220 @@
import { describe, expect, it } from "vite-plus/test";
import type { OrchestrationV2Subagent, OrchestrationV2SubagentWorkflow } from "@t3tools/contracts";
import * as DateTime from "effect/DateTime";
import { deriveWorkflowGroups, projectedSubagentsToRuntime } from "./subagentRuntime.ts";
const at = (iso: string) => DateTime.makeUnsafe(iso);
type ProjectedSubagent = Parameters<typeof projectedSubagentsToRuntime>[0][number];
function subagent(overrides: Partial<ProjectedSubagent> = {}): ProjectedSubagent {
return {
id: "coordinator",
title: "probe",
prompt: "export const meta = …",
model: "claude-opus-5[1m]",
status: "running" satisfies OrchestrationV2Subagent["status"],
result: null,
startedAt: at("2026-08-01T10:00:00.000Z"),
completedAt: null,
updatedAt: at("2026-08-01T10:00:05.000Z"),
...overrides,
};
}
const member = (
overrides: Partial<OrchestrationV2SubagentWorkflow["agents"][number]> = {},
): OrchestrationV2SubagentWorkflow["agents"][number] => ({
index: 1,
label: "alpha:one",
state: "running",
phaseIndex: 1,
phaseTitle: "Alpha",
...overrides,
});
const groupsOf = (subagents: ReadonlyArray<ProjectedSubagent>) =>
deriveWorkflowGroups(projectedSubagentsToRuntime(subagents));
describe("projectedSubagentsToRuntime workflow expansion", () => {
it("leaves a plain subagent flat", () => {
const [agent, ...rest] = projectedSubagentsToRuntime([subagent()]);
expect(rest).toEqual([]);
expect(agent?.kind).toBe("subagent");
expect(agent?.phases).toEqual([]);
});
it("projects a coordinator plus one row per member", () => {
const rows = projectedSubagentsToRuntime([
subagent({
workflow: {
name: "probe-wf",
phases: [
{ index: 1, title: "Alpha" },
{ index: 2, title: "Beta" },
],
agents: [member(), member({ index: 2, label: "alpha:two", state: "queued" })],
totalTokens: 1000,
},
}),
]);
expect(rows.map((row) => row.kind)).toEqual(["workflow", "workflow_agent", "workflow_agent"]);
expect(rows[0]?.workflowName).toBe("probe-wf");
expect(rows[0]?.usage?.totalTokens).toBe(1000);
expect(rows[1]).toMatchObject({
id: "coordinator:agent:1",
title: "alpha:one",
parentAgentId: "coordinator",
phaseIndex: 1,
phaseTitle: "Alpha",
status: "running",
});
// A queued member is not started work: it must not report an elapsed clock.
expect(rows[2]?.status).toBe("pending");
expect(rows[2]?.startedAt).toBeNull();
});
it("derives a settled member's end from its reported duration", () => {
const [, settled] = projectedSubagentsToRuntime([
subagent({
workflow: {
phases: [],
agents: [
member({
state: "completed",
startedAt: Date.UTC(2026, 7, 1, 10, 0, 0),
durationMs: 1500,
totalTokens: 71_141,
toolCalls: 3,
prompt: "Reply with exactly: A1",
result: "A1",
}),
],
},
}),
]);
expect(settled?.status).toBe("completed");
expect(settled?.completedAt).toBe("2026-08-01T10:00:01.500Z");
expect(settled?.result).toBe("A1");
// The prompt stays on progress so the detail view can show both halves.
expect(settled?.progress).toBe("Reply with exactly: A1");
expect(settled?.usage).toEqual({ totalTokens: 71_141, toolUses: 3, durationMs: 1500 });
});
it.each(["completed", "cancelled", "failed"] as const)(
"settles unfinished members when the coordinator is %s",
(status) => {
const groups = groupsOf([
subagent({
status,
completedAt: at("2026-08-01T10:00:09.000Z"),
workflow: {
phases: [],
agents: [
member(),
member({ index: 2, state: "queued" }),
member({ index: 3, state: "completed" }),
],
},
}),
]);
// The settled run keeps its members reachable under the coordinator.
expect(groups[0]?.workflow.status).toBe(status);
const members = groups[0]?.phases.flatMap((phase) => phase.members);
expect(members?.map((agent) => agent.status)).toEqual([status, status, "completed"]);
expect(members?.[0]?.completedAt).toBe("2026-08-01T10:00:09.000Z");
},
);
it("surfaces a retried member through the row's activation count", () => {
const [, retried] = projectedSubagentsToRuntime([
subagent({ workflow: { phases: [], agents: [member({ attempt: 3 })] } }),
]);
expect(retried?.activationCount).toBe(3);
expect(retried?.attempt).toBe(3);
});
});
describe("deriveWorkflowGroups over a dynamic workflow", () => {
it("groups members under their phases and counts the run", () => {
const groups = groupsOf([
subagent({
workflow: {
name: "probe-wf",
phases: [
{ index: 1, title: "Alpha" },
{ index: 2, title: "Beta" },
],
agents: [
member({ state: "completed" }),
member({ index: 2, label: "alpha:two", state: "failed" }),
member({ index: 3, label: "beta:one", phaseIndex: 2, phaseTitle: "Beta" }),
member({
index: 4,
label: "beta:two",
phaseIndex: 2,
phaseTitle: "Beta",
state: "queued",
}),
],
},
}),
]);
const group = groups[0];
expect(group?.phases.map((phase) => [phase.title, phase.state])).toEqual([
["Alpha", "done"],
["Beta", "running"],
]);
expect(group?.phases.map((phase) => phase.settledCount)).toEqual([2, 0]);
expect(group?.phases.flatMap((phase) => phase.members)).toHaveLength(4);
expect(group?.unphasedMembers).toEqual([]);
});
it("shows a phase the script only reached at runtime", () => {
const groups = groupsOf([
subagent({
workflow: {
// The declared plan has not caught up with the member's phase yet.
phases: [{ index: 1, title: "Alpha" }],
agents: [
member({ state: "completed" }),
member({ index: 2, label: "dynamic:late", phaseIndex: 3, phaseTitle: "Gamma" }),
],
},
}),
]);
const group = groups[0];
expect(group?.phases.map((phase) => phase.title)).toEqual(["Alpha", "Gamma"]);
expect(group?.unphasedMembers).toEqual([]);
});
it.each([false, true])("keeps a reported phase title, missing title first=%s", (missingFirst) => {
const agents = [member({ phaseTitle: "Review" }), member({ index: 2, phaseTitle: undefined })];
const groups = groupsOf([
subagent({ workflow: { phases: [], agents: missingFirst ? agents.toReversed() : agents } }),
]);
expect(groups[0]?.phases[0]?.title).toBe("Review");
});
it("keeps unphased members with their workflow and excludes unrelated agents", () => {
const runtime = projectedSubagentsToRuntime([
subagent({ id: "ordinary" }),
subagent({
id: "__proto__",
workflow: {
phases: [],
agents: [member({ phaseIndex: undefined, phaseTitle: undefined })],
},
}),
subagent({ id: "constructor", workflow: { phases: [], agents: [] } }),
subagent({ id: "orphan", workflow: { phases: [], agents: [member()] } }),
]);
const groups = deriveWorkflowGroups(runtime.filter((agent) => agent.id !== "orphan"));
expect(groups.map((group) => group.workflow.id)).toEqual(["__proto__", "constructor"]);
expect(groups[0]?.phases).toEqual([]);
expect(groups[0]?.unphasedMembers.map((agent) => agent.title)).toEqual(["alpha:one"]);
expect(groups[1]?.phases).toEqual([]);
expect(groups[1]?.unphasedMembers).toEqual([]);
});
});
@@ -2,8 +2,13 @@
* Subagent status helpers shared by web and mobile, and the runtime shape the
* web agent rows render.
*/
import { groupBy } from "effect/Array";
import * as DateTime from "effect/DateTime";
import type { OrchestrationV2Subagent, OrchestrationV2TurnItem } from "@t3tools/contracts";
import type {
OrchestrationV2Subagent,
OrchestrationV2SubagentWorkflow,
OrchestrationV2TurnItem,
} from "@t3tools/contracts";
import { isOrchestrationV2WorkActive } from "@t3tools/contracts";
export type RuntimeSubagentStatus =
@@ -36,11 +41,12 @@ export interface SubagentWorkflowPhase {
readonly title: string;
}
// Optional-and-undefined, matching the contract's own optionals.
export interface SubagentRunHandles {
readonly runId?: string;
readonly scriptPath?: string;
readonly transcriptDir?: string;
readonly sessionUrl?: string;
readonly runId?: string | undefined;
readonly scriptPath?: string | undefined;
readonly transcriptDir?: string | undefined;
readonly sessionUrl?: string | undefined;
}
export interface RuntimeSubagent {
@@ -66,6 +72,8 @@ export interface RuntimeSubagent {
readonly workflowName: string | null;
readonly phases: ReadonlyArray<SubagentWorkflowPhase>;
readonly runHandles: SubagentRunHandles | null;
/** The thread this agent owns, when it has one. */
readonly childThreadId: string | null;
readonly recentActivity: ReadonlyArray<SubagentActivityEntry>;
/** First retained observation, used as the roster's stable display order. */
readonly firstSeenAt: string;
@@ -91,18 +99,105 @@ export function isActiveSubagentStatus(status: RuntimeSubagentStatus): boolean {
return isOrchestrationV2WorkActive(status);
}
/**
* A subagent card whose child is still working. The card outlives its
* launching turn: once that turn settles, the waiting footer counts the child,
* so the card stays out of the turn's fold until the child finishes.
*/
export function isLiveSubagentTurnItem(item: OrchestrationV2TurnItem): boolean {
return item.type === "subagent" && isOrchestrationV2WorkActive(item.status);
}
/** A workflow coordinator with its members grouped by phase, as Lineage renders it. */
export interface SubagentWorkflowGroup {
readonly workflow: RuntimeSubagent;
readonly phases: ReadonlyArray<{
readonly index: number;
readonly title: string;
readonly members: ReadonlyArray<RuntimeSubagent>;
/** done = every member settled (success or error); running = any active. */
readonly state: "pending" | "running" | "done";
readonly settledCount: number;
}>;
/** Members with no resolvable phase (orphans render under the workflow). */
readonly unphasedMembers: ReadonlyArray<RuntimeSubagent>;
}
function workflowUsage(source: {
readonly totalTokens?: number | undefined;
readonly toolCalls?: number | undefined;
readonly durationMs?: number | undefined;
}): SubagentUsage | null {
if (source.totalTokens === undefined) return null;
return {
totalTokens: source.totalTokens,
...(source.toolCalls === undefined ? {} : { toolUses: source.toolCalls }),
...(source.durationMs === undefined ? {} : { durationMs: source.durationMs }),
};
}
function isoFromEpochMillis(value: number | undefined): string | null {
return value === undefined ? null : DateTime.formatIso(DateTime.makeUnsafe(value));
}
/**
* Projects orchestration-v2 subagent entities into the runtime shape the web
* agent rows render.
* Expands a coordinator's workflow roster into member rows. Members are
* synthesized from the coordinator's snapshot rather than read from their own
* subagent entities, so a parent thread's Lineage can show them without
* loading the coordinator's child thread; their identity is the coordinator id
* plus the spawn ordinal.
*/
function workflowMembersToRuntime(
coordinator: RuntimeSubagent,
workflow: OrchestrationV2SubagentWorkflow,
): ReadonlyArray<RuntimeSubagent> {
return workflow.agents.map((agent) => {
// Every member state but queued is already a runtime status.
const status =
(agent.state === "queued" || agent.state === "running") &&
isTerminalSubagentStatus(coordinator.status)
? coordinator.status
: agent.state === "queued"
? "pending"
: agent.state;
const failed = status === "failed";
const startedAt = isoFromEpochMillis(agent.startedAt);
// The provider reports a settled member's duration, not its end instant.
const completedAt = isTerminalSubagentStatus(status)
? agent.startedAt !== undefined && agent.durationMs !== undefined
? isoFromEpochMillis(agent.startedAt + agent.durationMs)
: coordinator.completedAt
: null;
return {
// Members share the run's handles: the transcript directory is what
// makes their conversation readable, and only the run knows it.
...coordinator,
id: `${coordinator.id}:agent:${agent.index}`,
kind: "workflow_agent" as const,
title: agent.label,
model: agent.model ?? null,
status,
// A member the workflow retried counts each attempt as a run.
activationCount: agent.attempt ?? 1,
usage: workflowUsage(agent),
// A member keeps its prompt on `progress` even once settled: that is the
// question half of its conversation, and the detail view shows both halves.
progress: agent.prompt ?? null,
result: failed ? null : (agent.result ?? null),
error: failed ? (agent.result ?? null) : null,
parentAgentId: coordinator.id,
agentIndex: agent.index,
phaseIndex: agent.phaseIndex ?? null,
phaseTitle: agent.phaseTitle ?? null,
attempt: agent.attempt ?? null,
phases: [],
childThreadId: agent.childThreadId ?? null,
recentActivity: [],
firstSeenAt: isoFromEpochMillis(agent.queuedAt) ?? startedAt ?? coordinator.firstSeenAt,
startedAt,
completedAt,
updatedAt: completedAt ?? startedAt ?? coordinator.firstSeenAt,
} satisfies RuntimeSubagent;
});
}
/**
* Projects subagents and workflow members into the runtime roster.
*/
export function projectedSubagentsToRuntime(
subagents: ReadonlyArray<{
@@ -112,18 +207,23 @@ export function projectedSubagentsToRuntime(
readonly model: string | null;
readonly status: OrchestrationV2Subagent["status"];
readonly progress?: string | undefined;
readonly childThreadId?: string | null | undefined;
readonly workflow?: OrchestrationV2SubagentWorkflow | undefined;
readonly result: string | null;
readonly startedAt: DateTime.Utc | null;
readonly completedAt: DateTime.Utc | null;
readonly updatedAt: DateTime.Utc;
}>,
): ReadonlyArray<RuntimeSubagent> {
return subagents.map((subagent) => {
return subagents.flatMap((subagent) => {
const updatedAt = DateTime.formatIso(subagent.updatedAt);
const startedAt = subagent.startedAt === null ? null : DateTime.formatIso(subagent.startedAt);
return {
const firstSeenAt = startedAt ?? updatedAt;
const { workflow } = subagent;
const runHandles = workflow?.runHandles ?? null;
const coordinator = {
id: subagent.id,
kind: "subagent" as const,
kind: workflow === undefined ? "subagent" : "workflow",
title:
subagent.title ??
(subagent.prompt.length > 80 ? `${subagent.prompt.slice(0, 77)}...` : subagent.prompt),
@@ -132,7 +232,7 @@ export function projectedSubagentsToRuntime(
effort: null,
status: subagent.status,
activationCount: 1,
usage: null,
usage: workflow === undefined ? null : workflowUsage(workflow),
progress: subagent.progress ?? null,
lastToolName: null,
result: subagent.result,
@@ -143,14 +243,78 @@ export function projectedSubagentsToRuntime(
phaseIndex: null,
phaseTitle: null,
attempt: null,
workflowName: null,
phases: [],
runHandles: null,
workflowName: workflow?.name ?? null,
phases: workflow?.phases ?? [],
runHandles,
childThreadId: subagent.childThreadId ?? null,
recentActivity: [],
firstSeenAt: startedAt ?? updatedAt,
firstSeenAt,
startedAt,
completedAt: subagent.completedAt === null ? null : DateTime.formatIso(subagent.completedAt),
updatedAt,
} satisfies RuntimeSubagent;
return workflow === undefined
? [coordinator]
: [coordinator, ...workflowMembersToRuntime(coordinator, workflow)];
});
}
export function deriveWorkflowGroups(
agents: ReadonlyArray<RuntimeSubagent>,
): ReadonlyArray<SubagentWorkflowGroup> {
const workflows = agents
.filter((agent) => agent.kind === "workflow")
.sort((a, b) => a.firstSeenAt.localeCompare(b.firstSeenAt) || a.id.localeCompare(b.id));
const members = groupBy(
agents.filter((agent) => agent.kind !== "workflow" && agent.parentAgentId !== null),
(agent) => agent.parentAgentId!,
);
return workflows.map((workflow) => {
const workflowMembers = Object.hasOwn(members, workflow.id) ? members[workflow.id]! : [];
// Union, not either/or: the declared plan lags the members, because the
// provider only admits a phase once something in it starts. Members seed
// the map; the declared titles then overwrite whatever they guessed.
const phaseTitles = new Map<number, string>();
for (const member of workflowMembers) {
if (member.phaseIndex === null) continue;
phaseTitles.set(
member.phaseIndex,
member.phaseTitle ?? phaseTitles.get(member.phaseIndex) ?? `Phase ${member.phaseIndex + 1}`,
);
}
for (const phase of workflow.phases) phaseTitles.set(phase.index, phase.title);
const knownPhases = Array.from(phaseTitles.entries())
.map(([index, title]) => ({ index, title }))
.sort((a, b) => a.index - b.index);
const phases = knownPhases.map((phase) => {
const phaseMembers = workflowMembers
.filter((member) => member.phaseIndex === phase.index)
.sort((a, b) => (a.agentIndex ?? 0) - (b.agentIndex ?? 0));
const hasActiveMember = phaseMembers.some(
// Idle members count as active for phase-liveness: a resumable Codex
// member has not finished the phase.
(member) => isActiveSubagentStatus(member.status) || member.status === "idle",
);
const settledCount = phaseMembers.filter((member) =>
isTerminalSubagentStatus(member.status),
).length;
const state: "pending" | "running" | "done" =
phaseMembers.length === 0 ? "pending" : hasActiveMember ? "running" : "done";
return {
index: phase.index,
title: phase.title,
members: phaseMembers,
state,
settledCount,
};
});
const unphasedMembers = workflowMembers
.filter((member) => member.phaseIndex === null)
.sort((a, b) => (a.agentIndex ?? 0) - (b.agentIndex ?? 0));
return { workflow, phases, unphasedMembers };
});
}
@@ -248,4 +248,21 @@ describe("summarizeT3ToolCalls", () => {
]).label,
).toBe("Requested deletion of 1 scheduled task");
});
it("counts workflow stop requests by coordinator and excludes failed requests", () => {
const first = completed({ threadId: "parent", subagentId: "workflow-1" }, {});
const failed = completed({ threadId: "parent", subagentId: "workflow-3" }, { isError: true });
expect(
summarizeT3ToolCalls("workflow-stop", [
first,
first,
completed({ threadId: "parent", subagentId: "workflow-2" }, {}),
failed,
]),
).toEqual({ label: "Requested stops for 2 workflows", failedCount: 1 });
expect(summarizeT3ToolCalls("workflow-stop", [failed])).toEqual({
label: "Tried to stop 1 workflow",
failedCount: 1,
});
});
});
@@ -168,6 +168,13 @@ export function summarizeT3ToolCalls(
quantity(countEntities(threadIds), "thread"),
);
break;
case "workflow-stop":
label = phrase(
"Requested stops for",
"stop",
quantity(countEntities(entityIds("subagentId")), "workflow"),
);
break;
case "task-status":
label = phrase("Checked", "check", `task status ${times}`);
break;
@@ -308,6 +308,7 @@ describe("resolveWorkEntryToolPresentation", () => {
["t3_thread_fork", "Forking this thread", "Requested a fork of this thread"],
["t3_thread_send_attachments", "Sending attachments", "Sent attachments"],
["run_scheduled_task_now", "Running a scheduled task", "Requested a run of a scheduled task"],
["t3_workflow_stop", "Stopping a workflow", "Requested a stop for a workflow"],
])("labels %s through its lifecycle", (tool, running, completed) => {
expect(resolveWorkEntryToolPresentation({ label: `T3-code.${tool}` })?.displayName).toBe(
running,
@@ -320,6 +321,47 @@ describe("resolveWorkEntryToolPresentation", () => {
).toBe(completed);
});
it("summarizes workflow stop targets separately from their parent thread", () => {
const entry = {
id: "workflow-stop",
createdAt: "2026-10-07T00:00:00.000Z",
tone: "tool",
label: "Custom title",
itemType: "dynamic_tool",
toolLifecycleStatus: "completed",
toolData: {
server: "t3-code",
tool: "t3_workflow_stop",
arguments: { threadId: "parent", subagentId: "workflow-1" },
result: {},
},
} satisfies WorkLogPresentationEntry;
expect(
summarizeToolGroup([
entry,
entry,
{
...entry,
toolData: {
...entry.toolData,
arguments: { threadId: "parent", subagentId: "workflow-2" },
},
},
{
...entry,
toolData: {
...entry.toolData,
tool: "t3_thread_interrupt",
arguments: { threadId: "parent" },
},
},
]),
).toEqual({
summary: "Requested stops for 2 workflows and requested interrupts for 1 thread",
hasFailure: false,
});
});
it("summarizes project tools from MCP arguments and results without claiming failed effects", () => {
const entry: WorkLogPresentationEntry = {
id: "clone",
@@ -609,6 +609,7 @@ function summaryActionPriority(action: ToolGroupAction | T3McpToolSummaryAction)
case "thread-create":
case "thread-send":
case "thread-interrupt":
case "workflow-stop":
case "schedule-create":
case "schedule-update":
case "schedule-delete":
@@ -6,6 +6,7 @@ import {
AuthSourceControlWriteScope,
type AuthEnvironmentScope,
} from "./auth.ts";
import { ORCHESTRATION_V2_WS_METHODS } from "./orchestrationV2.ts";
import { WS_METHODS } from "./rpc.ts";
/** Incremental client enforcement; the server still authorizes every request. */
@@ -43,6 +44,7 @@ export const CLIENT_GUARDED_RPC_SCOPES = {
[WS_METHODS.scheduledTasksDelete]: AuthOrchestrationOperateScope,
[WS_METHODS.scheduledTasksRunNow]: AuthOrchestrationOperateScope,
[WS_METHODS.scheduledTasksRotateWebhookToken]: AuthOrchestrationOperateScope,
[ORCHESTRATION_V2_WS_METHODS.stopWorkflow]: AuthOrchestrationOperateScope,
} as const;
export type ClientGuardedRpcTag = keyof typeof CLIENT_GUARDED_RPC_SCOPES;
@@ -36,6 +36,7 @@ import {
OrchestrationV2ShellSnapshot,
OrchestrationV2SubscribeThreadInput,
OrchestrationV2Subagent,
OrchestrationV2SubagentJson,
OrchestrationV2ThreadHistoryPage,
OrchestrationV2ThreadProjection,
OrchestrationV2ThreadStreamItem,
@@ -112,6 +113,7 @@ const decodeOrchestrationV2Checkpoint = Schema.decodeUnknownSync(OrchestrationV2
const decodeOrchestrationV2DomainEvent = Schema.decodeUnknownSync(OrchestrationV2DomainEvent);
const decodeProviderReplayTranscript = Schema.decodeUnknownSync(ProviderReplayTranscript);
const decodeOrchestrationV2Subagent = Schema.decodeUnknownSync(OrchestrationV2Subagent);
const encodeOrchestrationV2SubagentJson = Schema.encodeSync(OrchestrationV2SubagentJson);
const decodeOrchestrationV2ThreadProjection = Schema.decodeUnknownSync(
OrchestrationV2ThreadProjection,
);
@@ -929,6 +931,68 @@ describe("orchestration V2 contracts", () => {
expect(turnItem.progress).toBe("Inspecting package metadata");
});
it.each([" indented prompt\n", "\n \t\n"])(
"preserves workflow prompt and result %j in stored and wire JSON",
(preview) => {
const subagent = {
...decodeOrchestrationV2Subagent({
id: "workflow-coordinator",
threadId: "thread-1",
runId: "run-1",
parentNodeId: "node-root-1",
origin: "provider_native",
createdBy: "agent",
driver: "claudeAgent",
providerInstanceId: "claudeAgent",
providerThreadId: null,
childThreadId: null,
nativeTaskRef: null,
prompt: "Run workflow",
title: null,
model: null,
status: "running",
result: null,
startedAt: now,
completedAt: null,
updatedAt: now,
}),
workflow: {
phases: [],
agents: [
{
index: 1,
label: "Member",
state: "completed" as const,
prompt: preview,
result: preview,
},
],
},
};
const { workflow, ...coordinator } = subagent;
const incoming = { ...encodeOrchestrationV2SubagentJson(coordinator), workflow };
for (const codec of [
OrchestrationV2SubagentJson,
Schema.toCodecJson(OrchestrationV2Subagent),
]) {
const encode = Schema.encodeSync(codec);
const decode = Schema.decodeUnknownSync(codec);
const json: unknown = JSON.parse(JSON.stringify(encode(subagent)));
expect(json).toMatchObject({
workflow: { agents: [{ prompt: preview, result: preview }] },
});
expect(decode(json).workflow?.agents[0]).toMatchObject({
prompt: preview,
result: preview,
});
expect(decode(incoming).workflow?.agents[0]).toMatchObject({
prompt: preview,
result: preview,
});
}
},
);
it("decodes app-owned subagent parent-wake policies", () => {
const appOwnedSubagent = {
id: "node-subagent-2",
+155
View File
@@ -664,6 +664,86 @@ export const OrchestrationV2ExecutionNode = Schema.Struct({
});
export type OrchestrationV2ExecutionNode = typeof OrchestrationV2ExecutionNode.Type;
/**
* One entry of a dynamic workflow's phase plan. The plan grows while the run
* executes: a phase a script only reaches at runtime joins the list when it
* starts, so a client must treat the list as append-only rather than as a
* fixed arc declared up front.
*/
export const OrchestrationV2WorkflowPhase = Schema.Struct({
index: NonNegativeInt,
title: TrimmedNonEmptyString,
});
export type OrchestrationV2WorkflowPhase = typeof OrchestrationV2WorkflowPhase.Type;
/**
* One agent inside a workflow run. Timestamps are epoch milliseconds rather
* than `DateTimeUtc` because only the top-level entity fields are remapped for
* the JSON wire shape (see `OrchestrationV2SubagentJson`), so a nested
* `DateTimeUtc` would never be encoded.
*/
export const OrchestrationV2WorkflowAgent = Schema.Struct({
/** Spawn ordinal within the run. Stable identity: an agent keeps it across updates. */
index: NonNegativeInt,
label: TrimmedNonEmptyString,
/**
* Provider-assigned id, absent until the agent actually starts. Names the
* member's transcript file inside the run's transcript directory.
*/
agentId: Schema.optional(TrimmedNonEmptyString),
/** The member's own thread, so the UI can open it like any subagent's. */
childThreadId: Schema.optional(ThreadId),
state: Schema.Literals(["queued", "running", "completed", "failed"]),
phaseIndex: Schema.optional(NonNegativeInt),
phaseTitle: Schema.optional(TrimmedNonEmptyString),
model: Schema.optional(TrimmedNonEmptyString),
/** Retry counter; > 1 means the agent was restarted after a provider error. */
attempt: Schema.optional(NonNegativeInt),
totalTokens: Schema.optional(NonNegativeInt),
toolCalls: Schema.optional(NonNegativeInt),
durationMs: Schema.optional(NonNegativeInt),
queuedAt: Schema.optional(NonNegativeInt),
startedAt: Schema.optional(NonNegativeInt),
/**
* Prompt and result excerpts. A workflow member's whole conversation is one
* prompt and one answer, so together these are its transcript as far as the
* provider exposes it — both are capped by the CLI at a few hundred
* characters.
*/
prompt: Schema.optional(Schema.NonEmptyString),
result: Schema.optional(Schema.NonEmptyString),
});
export type OrchestrationV2WorkflowAgent = typeof OrchestrationV2WorkflowAgent.Type;
/**
* Filesystem handles the provider reports when a run launches. The transcript
* directory is what makes a member's full answer readable: the progress
* snapshot only carries a capped excerpt, so the server reads the member's
* transcript from here to fill its thread.
*/
export const OrchestrationV2WorkflowRunHandles = Schema.Struct({
runId: Schema.optional(TrimmedNonEmptyString),
transcriptDir: Schema.optional(TrimmedNonEmptyString),
scriptPath: Schema.optional(TrimmedNonEmptyString),
});
export type OrchestrationV2WorkflowRunHandles = typeof OrchestrationV2WorkflowRunHandles.Type;
/**
* Workflow-run shape carried by the coordinator subagent. The snapshot is the
* provider's own replace-in-full view of the run; members are additionally
* projected as subagents in their own right.
*/
export const OrchestrationV2SubagentWorkflow = Schema.Struct({
name: Schema.optional(TrimmedNonEmptyString),
runHandles: Schema.optional(OrchestrationV2WorkflowRunHandles),
phases: Schema.Array(OrchestrationV2WorkflowPhase),
agents: Schema.Array(OrchestrationV2WorkflowAgent),
totalTokens: Schema.optional(NonNegativeInt),
toolCalls: Schema.optional(NonNegativeInt),
durationMs: Schema.optional(NonNegativeInt),
});
export type OrchestrationV2SubagentWorkflow = typeof OrchestrationV2SubagentWorkflow.Type;
export const OrchestrationV2Subagent = Schema.Struct({
id: NodeId,
threadId: ThreadId,
@@ -698,6 +778,8 @@ export const OrchestrationV2Subagent = Schema.Struct({
"interrupted",
]),
progress: Schema.optional(Schema.String),
/** Present only on a workflow coordinator task; absent on ordinary subagents. */
workflow: Schema.optional(OrchestrationV2SubagentWorkflow),
result: Schema.NullOr(Schema.String),
startedAt: Schema.NullOr(Schema.DateTimeUtc),
completedAt: Schema.NullOr(Schema.DateTimeUtc),
@@ -3163,6 +3245,7 @@ export const ORCHESTRATION_V2_WS_METHODS = {
getArchivedShellSnapshot: "orchestration.getArchivedShellSnapshot",
getThreadProjection: "orchestration.getThreadProjection",
getWorkflowScript: "orchestration.getWorkflowScript",
stopWorkflow: "orchestration.stopWorkflow",
getTurnItem: "orchestration.getTurnItem",
launchThread: "orchestration.launchThread",
subscribeArchivedShell: "orchestration.subscribeArchivedShell",
@@ -3448,11 +3531,41 @@ export class OrchestrationV2ThreadLaunchError extends Schema.TaggedError<Orchest
},
) {}
export const OrchestrationV2StopWorkflowInput = Schema.Struct({
threadId: ThreadId,
subagentId: NodeId,
});
export type OrchestrationV2StopWorkflowInput = typeof OrchestrationV2StopWorkflowInput.Type;
export class OrchestrationV2StopWorkflowError extends Schema.TaggedError<OrchestrationV2StopWorkflowError>()(
"OrchestrationV2StopWorkflowError",
{
threadId: ThreadId,
subagentId: NodeId,
reason: Schema.Literals(["not-running", "unsupported", "unavailable", "stop-failed"]),
cause: Schema.optional(Schema.Defect()),
},
) {
override get message(): string {
switch (this.reason) {
case "not-running":
return "This workflow is no longer running.";
case "unsupported":
return "This provider cannot stop a workflow.";
case "unavailable":
return "This workflow has no live provider task.";
case "stop-failed":
return "Failed to stop the workflow.";
}
}
}
export const OrchestrationV2RpcError = Schema.Union([
OrchestrationV2DispatchCommandError,
OrchestrationV2GetThreadProjectionError,
OrchestrationV2GetShellSnapshotError,
OrchestrationV2ThreadLaunchError,
OrchestrationV2StopWorkflowError,
]);
export type OrchestrationV2RpcError = typeof OrchestrationV2RpcError.Type;
@@ -3473,6 +3586,44 @@ export const OrchestrationV2GetWorkflowScriptResult = Schema.Struct({
export type OrchestrationV2GetWorkflowScriptResult =
typeof OrchestrationV2GetWorkflowScriptResult.Type;
/**
* Shared by every read of a file the Claude harness persisted for a workflow
* run — the script and each member's transcript. One vocabulary because both
* reads apply identical containment rules.
*/
const WORKFLOW_FILE_ERROR_MESSAGES = {
"invalid-path": "Workflow files must be absolute paths with the expected extension.",
"root-unavailable": "Workflow file root unavailable.",
"not-found": "Workflow file not found.",
"outside-root": "Path is outside the workflow files root.",
"wrong-extension": "Resolved file has the wrong extension.",
"not-regular-file": "Workflow file is not a regular file.",
"changed-during-read": "Workflow file changed between resolution and open.",
"read-failed": "Workflow file read failed.",
} as const;
export class OrchestrationWorkflowFileError extends Schema.TaggedError<OrchestrationWorkflowFileError>()(
"OrchestrationWorkflowFileError",
{
reason: Schema.Literals([
"invalid-path",
"root-unavailable",
"not-found",
"outside-root",
"wrong-extension",
"not-regular-file",
"changed-during-read",
"read-failed",
]),
path: Schema.String,
cause: Schema.optional(Schema.Defect()),
},
) {
override get message(): string {
return WORKFLOW_FILE_ERROR_MESSAGES[this.reason];
}
}
export const OrchestrationV2GetTurnItemInput = Schema.Struct({
threadId: ThreadId,
itemId: TurnItemId,
@@ -3601,6 +3752,10 @@ export const OrchestrationV2RpcSchemas = {
input: OrchestrationV2GetThreadProjectionInput,
output: OrchestrationV2ThreadProjection,
},
stopWorkflow: {
input: OrchestrationV2StopWorkflowInput,
output: Schema.Void,
},
getWorkflowScript: {
input: OrchestrationV2GetWorkflowScriptInput,
output: OrchestrationV2GetWorkflowScriptResult,
@@ -399,6 +399,7 @@ export const OrchestratorMcpThreadTimelineItem = Schema.Struct({
visibility: Schema.Literals(["local", "inherited", "synthetic"]),
sourceThreadId: ThreadId,
itemId: TurnItemId,
subagentId: Schema.optional(NodeId),
runId: Schema.NullOr(RunId),
messageId: Schema.NullOr(MessageId),
createdBy: Schema.NullOr(OrchestrationV2Actor),
+8
View File
@@ -208,6 +208,7 @@ import {
import {
ORCHESTRATION_V2_WS_METHODS,
OrchestrationGetWorkflowScriptError,
OrchestrationV2StopWorkflowError,
OrchestrationV2DispatchCommandError,
OrchestrationV2GetShellSnapshotError,
OrchestrationV2GetThreadProjectionError,
@@ -1662,6 +1663,12 @@ const WsOrchestrationV2GetThreadProjectionRpc = Rpc.make(
},
);
const WsOrchestrationV2StopWorkflowRpc = Rpc.make(ORCHESTRATION_V2_WS_METHODS.stopWorkflow, {
payload: OrchestrationV2RpcSchemas.stopWorkflow.input,
success: OrchestrationV2RpcSchemas.stopWorkflow.output,
error: Schema.Union([OrchestrationV2StopWorkflowError, EnvironmentAuthorizationError]),
});
const WsOrchestrationV2GetWorkflowScriptRpc = Rpc.make(
ORCHESTRATION_V2_WS_METHODS.getWorkflowScript,
{
@@ -2032,6 +2039,7 @@ export const WsRpcGroup = RpcGroup.make(
WsSubscribeResourceTelemetryRpc,
WsOrchestrationV2DispatchCommandRpc,
WsOrchestrationV2GetWorkflowScriptRpc,
WsOrchestrationV2StopWorkflowRpc,
WsOrchestrationV2GetTurnItemRpc,
WsOrchestrationV2GetTurnDiffRpc,
WsOrchestrationV2GetFullThreadDiffRpc,
@@ -595,6 +595,10 @@ export interface ProviderAdapterV2SessionRuntime {
readonly steerTurn: (
input: ProviderAdapterV2SteerInput,
) => Effect.Effect<void, ProviderAdapterV2Error>;
readonly stopTask?: (input: {
readonly providerThread: OrchestrationV2ProviderThread;
readonly taskId: string;
}) => Effect.Effect<void, ProviderAdapterV2Error>;
readonly interruptTurn: (
input: ProviderAdapterV2InterruptInput,
) => Effect.Effect<void, ProviderAdapterV2Error>;
@@ -21,6 +21,7 @@ export type T3McpToolSummaryAction =
| "thread-send"
| "thread-wait"
| "thread-interrupt"
| "workflow-stop"
| "thread-configuration"
| "thread-configure"
| "thread-fork"
@@ -138,6 +139,10 @@ const T3_MCP_TOOLS: Readonly<Record<string, T3McpToolDefinition>> = {
t3_thread_read: tool(["Read", "Reading", "Read", "a T3 thread"], "thread-read"),
t3_thread_send: tool(["Send", "Sending", "Sent", "to a T3 thread"], "thread-send"),
t3_thread_wait: tool(["Wait", "Waiting", "Waited", "for a T3 thread"], "thread-wait"),
t3_workflow_stop: tool(
["Stop", "Stopping", "Requested a stop for", "a workflow"],
"workflow-stop",
),
t3_thread_interrupt: tool(
["Interrupt", "Interrupting", "Requested an interrupt of", "a T3 thread"],
"thread-interrupt",