feat(orchestration-v2): stop a workflow task from the run card
A running workflow previously could only be abandoned by interrupting the whole run. A new subagent.stop command flows through the durable command path: the decider validates the subagent is active and carries a native task ref, records the user's intent as a Stop requested activity on the subagent row, and queues a process-bound subagent.stop effect. The effect worker resolves the session and calls the adapter's task stop — for Claude, the SDK runtime's undocumented stopTask, read behind the same single-cast convention as workflow_progress. The provider's own task_notification remains the only terminalizer; the command records intent, it does not fake a terminal state. A released session is treated as already stopped (nothing can still be running under it), while a runtime without task-level stop fails the effect loudly. Stop effects are process-bound, so recovery cancels them after process loss like the other session-scoped effects. The web run card shows Stop only while the coordinator is active, wired through thread environment commands as thread:stop-subagent. Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
2ac7918f5a
commit
ba24598606
17 files changed
+432
-2
No files matched your search
@@ -689,6 +689,7 @@ describe("ClaudeAdapterV2 attachments", () => {
|
||||
}),
|
||||
setModel: () => Effect.void,
|
||||
interrupt: Effect.void,
|
||||
stopTask: () => Effect.void,
|
||||
close: Effect.void,
|
||||
}),
|
||||
forkSession: () => Effect.die("unused forkSession"),
|
||||
@@ -801,6 +802,7 @@ describe("ClaudeAdapterV2 attachments", () => {
|
||||
offer: () => Effect.void,
|
||||
setModel: () => Effect.void,
|
||||
interrupt: Effect.void,
|
||||
stopTask: () => Effect.void,
|
||||
close: Effect.void,
|
||||
};
|
||||
}),
|
||||
@@ -892,6 +894,7 @@ describe("ClaudeAdapterV2 native fork", () => {
|
||||
offer: () => Effect.void,
|
||||
setModel: () => Effect.void,
|
||||
interrupt: Effect.void,
|
||||
stopTask: () => Effect.void,
|
||||
close: Effect.void,
|
||||
};
|
||||
}),
|
||||
@@ -1061,6 +1064,7 @@ describe("ClaudeAdapterV2 native session identity", () => {
|
||||
offer: () => Effect.void,
|
||||
setModel: () => Effect.void,
|
||||
interrupt: Effect.void,
|
||||
stopTask: () => Effect.void,
|
||||
close: Effect.void,
|
||||
};
|
||||
}),
|
||||
@@ -1276,6 +1280,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
|
||||
}),
|
||||
setModel: () => Effect.void,
|
||||
interrupt: options?.interrupt ?? Effect.void,
|
||||
stopTask: () => Effect.void,
|
||||
close: options?.close?.(sdkMessages) ?? Effect.void,
|
||||
}),
|
||||
forkSession: () => Effect.die("unused forkSession"),
|
||||
|
||||
@@ -187,6 +187,11 @@ interface ClaudeQueryInterruptFrame {
|
||||
readonly type: "query.interrupt";
|
||||
}
|
||||
|
||||
interface ClaudeTaskStopFrame {
|
||||
readonly type: "task.stop";
|
||||
readonly taskId: string;
|
||||
}
|
||||
|
||||
interface ClaudePermissionRequestFrame {
|
||||
readonly type: "permission.request";
|
||||
readonly toolName: string;
|
||||
@@ -228,6 +233,7 @@ type ClaudeOutboundFrame =
|
||||
| ClaudePromptOfferFrame
|
||||
| ClaudeQuerySetModelFrame
|
||||
| ClaudeQueryInterruptFrame
|
||||
| ClaudeTaskStopFrame
|
||||
| ClaudePermissionResponseFrame
|
||||
| ClaudeSessionForkFrame;
|
||||
|
||||
@@ -619,6 +625,10 @@ export function makeReplayQueryRunner(
|
||||
interrupt: replayEffect(() => {
|
||||
assertNextOutboundFrame({ type: "query.interrupt" });
|
||||
}),
|
||||
stopTask: (taskId) =>
|
||||
replayEffect(() => {
|
||||
assertNextOutboundFrame({ type: "task.stop", taskId });
|
||||
}),
|
||||
close: Effect.void,
|
||||
};
|
||||
},
|
||||
|
||||
@@ -277,6 +277,7 @@ export interface ClaudeAgentSdkQuerySession {
|
||||
readonly offer: (message: SDKUserMessage) => Effect.Effect<void, ClaudeAgentSdkQueryRunnerError>;
|
||||
readonly setModel: (model: string) => Effect.Effect<void, ClaudeAgentSdkQueryRunnerError>;
|
||||
readonly interrupt: Effect.Effect<void, ClaudeAgentSdkQueryRunnerError>;
|
||||
readonly stopTask: (taskId: string) => Effect.Effect<void, ClaudeAgentSdkQueryRunnerError>;
|
||||
readonly close: Effect.Effect<void, ClaudeAgentSdkQueryRunnerError>;
|
||||
}
|
||||
|
||||
@@ -393,6 +394,14 @@ export type ClaudeAgentSdkProtocolLogEvent =
|
||||
readonly type: "query.interrupt";
|
||||
};
|
||||
}
|
||||
| {
|
||||
readonly direction: "outgoing";
|
||||
readonly stage: "decoded";
|
||||
readonly payload: {
|
||||
readonly type: "task.stop";
|
||||
readonly taskId: string;
|
||||
};
|
||||
}
|
||||
| {
|
||||
readonly direction: "outgoing";
|
||||
readonly stage: "decoded";
|
||||
@@ -585,6 +594,35 @@ export const claudeAgentSdkQueryRunnerLiveLayer: Layer.Layer<
|
||||
}),
|
||||
),
|
||||
),
|
||||
stopTask: (taskId) =>
|
||||
Effect.tryPromise({
|
||||
try: () => {
|
||||
// stopTask is deliberate wire surface (the CLI's own
|
||||
// /workflows view stops through it) but absent from the
|
||||
// published SDK types — this cast is the single place the
|
||||
// undocumented method is read.
|
||||
const stop = (queryRuntime as { stopTask?: (taskId: string) => Promise<void> })
|
||||
.stopTask;
|
||||
if (stop === undefined) {
|
||||
throw new Error(
|
||||
"The Claude SDK runtime for this session does not expose stopTask.",
|
||||
);
|
||||
}
|
||||
return stop.call(queryRuntime, taskId);
|
||||
},
|
||||
catch: (cause) => queryRunnerError(cause, "stopTask"),
|
||||
}).pipe(
|
||||
Effect.tap(() =>
|
||||
logProtocolEvent({
|
||||
direction: "outgoing",
|
||||
stage: "decoded",
|
||||
payload: {
|
||||
type: "task.stop",
|
||||
taskId,
|
||||
},
|
||||
}),
|
||||
),
|
||||
),
|
||||
close: Queue.shutdown(promptQueue).pipe(
|
||||
Effect.andThen(closeClaudeQuery(queryRuntime)),
|
||||
Effect.tap(() =>
|
||||
@@ -4437,6 +4475,27 @@ export function makeClaudeAdapterV2(
|
||||
startTurn,
|
||||
steerTurn,
|
||||
interruptTurn,
|
||||
stopTask: Effect.fn("ClaudeAdapterV2.stopTask")(function* (stopInput: {
|
||||
readonly nativeTaskId: string;
|
||||
}) {
|
||||
const existing = yield* Ref.get(queryContext);
|
||||
if (existing === null) {
|
||||
return yield* new ProviderAdapterProtocolError({
|
||||
driver: CLAUDE_PROVIDER,
|
||||
detail: `Claude provider session ${input.providerSessionId} has no live query to stop task ${stopInput.nativeTaskId}.`,
|
||||
});
|
||||
}
|
||||
yield* existing.query.stopTask(stopInput.nativeTaskId).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ProviderAdapterProtocolError({
|
||||
driver: CLAUDE_PROVIDER,
|
||||
detail: `Stopping Claude task ${stopInput.nativeTaskId} failed.`,
|
||||
payload: cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
}),
|
||||
respondToRuntimeRequest: Effect.fn("ClaudeAdapterV2.respondToRuntimeRequest")(
|
||||
function* (requestInput) {
|
||||
const pending = (yield* Ref.get(pendingRuntimeRequests)).get(
|
||||
|
||||
@@ -72,6 +72,12 @@ export const OrchestrationEffectRequestV2 = Schema.Union([
|
||||
decision: Schema.optional(ProviderApprovalDecision),
|
||||
answers: Schema.optional(ProviderUserInputAnswers),
|
||||
}),
|
||||
Schema.Struct({
|
||||
type: Schema.Literal("subagent.stop"),
|
||||
providerSessionId: ProviderSessionId,
|
||||
/** The provider's own task id — what the runtime's stop call accepts. */
|
||||
nativeTaskId: Schema.String,
|
||||
}),
|
||||
Schema.Struct({
|
||||
type: Schema.Literal("provider-thread.rollback"),
|
||||
providerThreadId: ProviderThreadId,
|
||||
@@ -107,6 +113,7 @@ export const PROCESS_BOUND_EFFECT_TYPES = [
|
||||
"provider-turn.steer",
|
||||
"provider-turn.restart",
|
||||
"runtime-request.respond",
|
||||
"subagent.stop",
|
||||
] as const satisfies ReadonlyArray<OrchestrationEffectRequestV2["type"]>;
|
||||
|
||||
export const OrchestrationEffectStatusV2 = Schema.Literals([
|
||||
|
||||
@@ -93,6 +93,7 @@ function makeExecutorLayer(input: {
|
||||
ProviderTurnControlServiceV2.of({
|
||||
interrupt: () => Effect.void,
|
||||
steer: () => Effect.void,
|
||||
stopTask: () => Effect.void,
|
||||
interruptAndAwaitTerminal: (request) =>
|
||||
record(
|
||||
request.replacementProviderSessionId === undefined
|
||||
|
||||
@@ -210,6 +210,23 @@ export const executorLayer: Layer.Layer<
|
||||
}),
|
||||
),
|
||||
);
|
||||
case "subagent.stop":
|
||||
return providerTurnControl
|
||||
.stopTask({
|
||||
threadId: effect.threadId,
|
||||
providerSessionId: effect.request.providerSessionId,
|
||||
nativeTaskId: effect.request.nativeTaskId,
|
||||
})
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new OrchestrationEffectExecutionError({
|
||||
effectId: effect.id,
|
||||
effectType: effect.request.type,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
case "runtime-request.respond":
|
||||
return runtimeRequests
|
||||
.respond({
|
||||
|
||||
@@ -64,7 +64,11 @@ import {
|
||||
subagentResultForRun,
|
||||
subagentThreadTitle,
|
||||
} from "./SubagentProjection.ts";
|
||||
import { defaultSubagentRole, subagentActivationId } from "./SubagentObservability.ts";
|
||||
import {
|
||||
appendSubagentActivity,
|
||||
defaultSubagentRole,
|
||||
subagentActivationId,
|
||||
} from "./SubagentObservability.ts";
|
||||
import { ThreadForkServiceV2 } from "./ThreadForkService.ts";
|
||||
|
||||
export class OrchestratorDispatchError extends Schema.TaggedErrorClass<OrchestratorDispatchError>()(
|
||||
@@ -207,6 +211,7 @@ function commandThreadId(command: OrchestrationV2Command): ThreadId {
|
||||
case "prepared-run.progress":
|
||||
case "prepared-run.fail":
|
||||
case "run.interrupt":
|
||||
case "subagent.stop":
|
||||
case "queued-message.promote-to-steer":
|
||||
case "queued-run.reorder":
|
||||
case "queued-run.cancel":
|
||||
@@ -5312,6 +5317,99 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
|
||||
return undefined;
|
||||
});
|
||||
|
||||
const dispatchSubagentStop = (
|
||||
command: Extract<OrchestrationV2Command, { readonly type: "subagent.stop" }>,
|
||||
events: Ref.Ref<Array<OrchestrationV2DomainEvent>>,
|
||||
effects: Ref.Ref<Array<PendingOrchestrationEffectV2>>,
|
||||
) =>
|
||||
Effect.gen(function* () {
|
||||
const projection = yield* loadProjectionForCommand(command);
|
||||
const subagent = projection.subagents.find(
|
||||
(candidate) => candidate.id === command.subagentId,
|
||||
);
|
||||
if (subagent === undefined) {
|
||||
return yield* new OrchestratorDispatchError({
|
||||
commandId: command.commandId,
|
||||
commandType: command.type,
|
||||
cause: `Subagent ${command.subagentId} does not exist.`,
|
||||
});
|
||||
}
|
||||
if (
|
||||
subagent.status !== "pending" &&
|
||||
subagent.status !== "running" &&
|
||||
subagent.status !== "waiting"
|
||||
) {
|
||||
return yield* new OrchestratorDispatchError({
|
||||
commandId: command.commandId,
|
||||
commandType: command.type,
|
||||
cause: `Subagent ${command.subagentId} is not active (${subagent.status}).`,
|
||||
});
|
||||
}
|
||||
const nativeTaskId = subagent.nativeTaskRef?.nativeId;
|
||||
if (nativeTaskId === undefined || nativeTaskId === null) {
|
||||
return yield* new OrchestratorDispatchError({
|
||||
commandId: command.commandId,
|
||||
commandType: command.type,
|
||||
cause: `Subagent ${command.subagentId} has no native task reference to stop.`,
|
||||
});
|
||||
}
|
||||
// The stop call goes through the session the subagent's run executes
|
||||
// under; a workflow that outlived its launch run falls back to the
|
||||
// thread's bound session, which owns background tasks between turns.
|
||||
const run =
|
||||
subagent.runId === null
|
||||
? undefined
|
||||
: projection.runs.find((candidate) => candidate.id === subagent.runId);
|
||||
const runProviderThread =
|
||||
run?.providerThreadId === null
|
||||
? undefined
|
||||
: projection.providerThreads.find((candidate) => candidate.id === run?.providerThreadId);
|
||||
const providerSessionId =
|
||||
runProviderThread?.providerSessionId ??
|
||||
projection.providerThreads.find((candidate) => candidate.providerSessionId !== null)
|
||||
?.providerSessionId ??
|
||||
null;
|
||||
if (providerSessionId === null) {
|
||||
return yield* new OrchestratorDispatchError({
|
||||
commandId: command.commandId,
|
||||
commandType: command.type,
|
||||
cause: `Subagent ${command.subagentId} has no active provider session to stop it.`,
|
||||
});
|
||||
}
|
||||
|
||||
// The provider's task_notification is what terminalizes the subagent;
|
||||
// this only records the user's intent so the panel and card reflect
|
||||
// the request immediately.
|
||||
const now = yield* DateTime.now;
|
||||
const emitEvent = emit(events, command);
|
||||
yield* emitEvent({
|
||||
type: "subagent.updated",
|
||||
threadId: command.threadId,
|
||||
...(subagent.runId === null ? {} : { runId: subagent.runId }),
|
||||
nodeId: subagent.id,
|
||||
driver: subagent.driver,
|
||||
occurredAt: now,
|
||||
payload: {
|
||||
...subagent,
|
||||
recentActivity: appendSubagentActivity(subagent.recentActivity, "Stop requested", now),
|
||||
updatedAt: now,
|
||||
},
|
||||
});
|
||||
yield* Ref.update(effects, (existing) => [
|
||||
...existing,
|
||||
{
|
||||
id: `effect:${command.commandId}:subagent.stop:${subagent.id}`,
|
||||
commandId: command.commandId,
|
||||
threadId: command.threadId,
|
||||
request: {
|
||||
type: "subagent.stop",
|
||||
providerSessionId,
|
||||
nativeTaskId,
|
||||
},
|
||||
} satisfies PendingOrchestrationEffectV2,
|
||||
]);
|
||||
});
|
||||
|
||||
const dispatchCheckpointRollback = (
|
||||
command: Extract<OrchestrationV2Command, { readonly type: "checkpoint.rollback" }>,
|
||||
events: Ref.Ref<Array<OrchestrationV2DomainEvent>>,
|
||||
@@ -5817,6 +5915,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
|
||||
case "run.interrupt":
|
||||
cancelUnsettledEffects = yield* dispatchRunInterrupt(command, events, effects);
|
||||
break;
|
||||
case "subagent.stop":
|
||||
yield* dispatchSubagentStop(command, events, effects);
|
||||
break;
|
||||
case "queued-message.promote-to-steer":
|
||||
yield* dispatchQueuedMessagePromoteToSteer(command, events, effects);
|
||||
break;
|
||||
|
||||
@@ -526,6 +526,14 @@ export interface ProviderAdapterV2SessionRuntime {
|
||||
readonly interruptTurn: (
|
||||
input: ProviderAdapterV2InterruptInput,
|
||||
) => Effect.Effect<void, ProviderAdapterV2Error>;
|
||||
/**
|
||||
* Stop one background task (e.g. a workflow run) by its native task id
|
||||
* without interrupting the turn that launched it. Absent on providers
|
||||
* whose runtime has no task-level stop.
|
||||
*/
|
||||
readonly stopTask?: (input: {
|
||||
readonly nativeTaskId: string;
|
||||
}) => Effect.Effect<void, ProviderAdapterV2Error>;
|
||||
readonly respondToRuntimeRequest: (
|
||||
input: ProviderAdapterV2RuntimeRequestResponseInput,
|
||||
) => Effect.Effect<void, ProviderAdapterV2Error>;
|
||||
|
||||
@@ -504,6 +504,7 @@ it.effect(
|
||||
"provider-turn.steer",
|
||||
"provider-turn.restart",
|
||||
"runtime-request.respond",
|
||||
"subagent.stop",
|
||||
]);
|
||||
const events = committedInput?.events ?? [];
|
||||
assert.deepEqual(
|
||||
|
||||
@@ -285,3 +285,105 @@ it.effect(
|
||||
assert.equal(interrupted?.nativeThreadRef?.nativeId, "native-thread:restart-session");
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("stops a task through the live session and tolerates a released one", () =>
|
||||
Effect.gen(function* () {
|
||||
const now = yield* DateTime.now;
|
||||
const threadId = ThreadId.make("thread:stop-task");
|
||||
const sessionId = ProviderSessionId.make("provider-session:stop-task");
|
||||
const noStopSessionId = ProviderSessionId.make("provider-session:stop-task:no-stop");
|
||||
const stoppedTaskIds = yield* Ref.make<ReadonlyArray<string>>([]);
|
||||
const providerSession = {
|
||||
id: sessionId,
|
||||
driver,
|
||||
providerInstanceId,
|
||||
status: "running" as const,
|
||||
cwd: "/workspace",
|
||||
model: modelSelection.model,
|
||||
capabilities: CodexProviderCapabilitiesV2,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
lastError: null,
|
||||
};
|
||||
const baseRuntime: ProviderAdapterV2SessionRuntime = {
|
||||
instanceId: providerInstanceId,
|
||||
driver,
|
||||
providerSessionId: sessionId,
|
||||
providerSession,
|
||||
events: Stream.empty,
|
||||
ensureThread: () => Effect.die("unused ensureThread"),
|
||||
resumeThread: () => Effect.die("unused resumeThread"),
|
||||
startTurn: () => Effect.die("unused startTurn"),
|
||||
steerTurn: () => Effect.die("unused steerTurn"),
|
||||
interruptTurn: () => Effect.die("unused interruptTurn"),
|
||||
respondToRuntimeRequest: () => Effect.die("unused respondToRuntimeRequest"),
|
||||
readThreadSnapshot: () => Effect.die("unused readThreadSnapshot"),
|
||||
rollbackThread: () => Effect.die("unused rollbackThread"),
|
||||
forkThread: () => Effect.die("unused forkThread"),
|
||||
};
|
||||
const runtime: ProviderAdapterV2SessionRuntime = {
|
||||
...baseRuntime,
|
||||
stopTask: ({ nativeTaskId }) =>
|
||||
Ref.update(stoppedTaskIds, (current) => [...current, nativeTaskId]),
|
||||
};
|
||||
const projectionLayer = Layer.succeed(
|
||||
ProjectionStoreV2,
|
||||
ProjectionStoreV2.of({
|
||||
apply: () => Effect.void,
|
||||
getShellSnapshot: () => Effect.die("unused getShellSnapshot"),
|
||||
getThreadShell: () => Effect.die("unused getThreadShell"),
|
||||
getThreadProjection: () => Effect.die("stopTask must not read the projection"),
|
||||
getThreadSnapshot: () => Effect.die("unused getThreadSnapshot"),
|
||||
}),
|
||||
);
|
||||
const sessionManagerLayer = Layer.succeed(
|
||||
ProviderSessionManagerV2,
|
||||
ProviderSessionManagerV2.of({
|
||||
shutdown: Effect.void,
|
||||
open: () => Effect.die("unused open"),
|
||||
get: (providerSessionId) =>
|
||||
Effect.succeed(
|
||||
providerSessionId === sessionId
|
||||
? Option.some(runtime)
|
||||
: providerSessionId === noStopSessionId
|
||||
? Option.some(baseRuntime)
|
||||
: Option.none(),
|
||||
),
|
||||
close: () => Effect.void,
|
||||
release: () => Effect.void,
|
||||
detach: () => Effect.void,
|
||||
}),
|
||||
);
|
||||
const controlLayer = providerTurnControlLayer.pipe(
|
||||
Layer.provide(Layer.merge(projectionLayer, sessionManagerLayer)),
|
||||
);
|
||||
|
||||
yield* Effect.gen(function* () {
|
||||
const control = yield* ProviderTurnControlServiceV2;
|
||||
yield* control.stopTask({
|
||||
threadId,
|
||||
providerSessionId: sessionId,
|
||||
nativeTaskId: "wf-task-1",
|
||||
});
|
||||
// A released session cannot be running the task any more — the stop is
|
||||
// a no-op success, not a durable-effect failure that retries forever.
|
||||
yield* control.stopTask({
|
||||
threadId,
|
||||
providerSessionId: ProviderSessionId.make("provider-session:stop-task:gone"),
|
||||
nativeTaskId: "wf-task-2",
|
||||
});
|
||||
// A runtime without task-level stop must fail loudly rather than
|
||||
// silently pretending it stopped something.
|
||||
const unsupported = yield* Effect.exit(
|
||||
control.stopTask({
|
||||
threadId,
|
||||
providerSessionId: noStopSessionId,
|
||||
nativeTaskId: "wf-task-3",
|
||||
}),
|
||||
);
|
||||
assert.isTrue(Exit.isFailure(unsupported));
|
||||
}).pipe(Effect.provide(controlLayer));
|
||||
|
||||
assert.deepEqual(yield* Ref.get(stoppedTaskIds), ["wf-task-1"]);
|
||||
}),
|
||||
);
|
||||
@@ -36,6 +36,16 @@ export class ProviderTurnControlError extends Schema.TaggedErrorClass<ProviderTu
|
||||
},
|
||||
) {}
|
||||
|
||||
export class ProviderTaskStopError extends Schema.TaggedErrorClass<ProviderTaskStopError>()(
|
||||
"ProviderTaskStopError",
|
||||
{
|
||||
threadId: ThreadId,
|
||||
providerSessionId: ProviderSessionId,
|
||||
nativeTaskId: Schema.String,
|
||||
cause: Schema.optional(Schema.Defect()),
|
||||
},
|
||||
) {}
|
||||
|
||||
const isProviderTurnControlError = Schema.is(ProviderTurnControlError);
|
||||
|
||||
export interface ProviderTurnControlServiceV2Shape {
|
||||
@@ -60,6 +70,11 @@ export interface ProviderTurnControlServiceV2Shape {
|
||||
readonly providerTurnId: ProviderTurnId;
|
||||
readonly interruptedAttemptId: RunAttemptId;
|
||||
}) => Effect.Effect<void, ProviderTurnControlError>;
|
||||
readonly stopTask: (input: {
|
||||
readonly threadId: ThreadId;
|
||||
readonly providerSessionId: ProviderSessionId;
|
||||
readonly nativeTaskId: string;
|
||||
}) => Effect.Effect<void, ProviderTaskStopError>;
|
||||
}
|
||||
|
||||
export class ProviderTurnControlServiceV2 extends Context.Service<
|
||||
@@ -248,6 +263,44 @@ export const layer: Layer.Layer<
|
||||
}),
|
||||
),
|
||||
),
|
||||
stopTask: (input) =>
|
||||
Effect.gen(function* () {
|
||||
const session = yield* sessions.get(input.providerSessionId);
|
||||
if (Option.isNone(session)) {
|
||||
// The task's session was already released — the task cannot be
|
||||
// running under it any more, so there is nothing left to stop.
|
||||
yield* Effect.logWarning(
|
||||
"Provider task stop found no live session; treating as already stopped",
|
||||
{
|
||||
threadId: input.threadId,
|
||||
providerSessionId: input.providerSessionId,
|
||||
nativeTaskId: input.nativeTaskId,
|
||||
},
|
||||
);
|
||||
return;
|
||||
}
|
||||
const stop = session.value.stopTask;
|
||||
if (stop === undefined) {
|
||||
return yield* new ProviderTaskStopError({
|
||||
threadId: input.threadId,
|
||||
providerSessionId: input.providerSessionId,
|
||||
nativeTaskId: input.nativeTaskId,
|
||||
cause: "The provider runtime for this session does not support stopping tasks.",
|
||||
});
|
||||
}
|
||||
yield* stop({ nativeTaskId: input.nativeTaskId });
|
||||
}).pipe(
|
||||
Effect.mapError((cause) =>
|
||||
Schema.is(ProviderTaskStopError)(cause)
|
||||
? cause
|
||||
: new ProviderTaskStopError({
|
||||
threadId: input.threadId,
|
||||
providerSessionId: input.providerSessionId,
|
||||
nativeTaskId: input.nativeTaskId,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
),
|
||||
steer: (input) =>
|
||||
Effect.gen(function* () {
|
||||
const loaded = yield* load({ ...input, operation: "steer" });
|
||||
|
||||
@@ -117,6 +117,7 @@ function commandThreadIds(command: OrchestrationV2Command): ReadonlyArray<Thread
|
||||
case "prepared-run.progress":
|
||||
case "prepared-run.fail":
|
||||
case "run.interrupt":
|
||||
case "subagent.stop":
|
||||
case "queued-message.promote-to-steer":
|
||||
case "queued-run.reorder":
|
||||
case "queued-run.cancel":
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
type EnvironmentId,
|
||||
type MessageId,
|
||||
type ModelSelection,
|
||||
type NodeId,
|
||||
type OrchestrationV2ThreadProjection,
|
||||
type ProjectScript,
|
||||
type ProjectId,
|
||||
@@ -1220,6 +1221,9 @@ function ChatViewContent(props: ChatViewProps) {
|
||||
const interruptThreadTurn = useAtomCommand(threadEnvironment.interruptTurn, {
|
||||
reportFailure: false,
|
||||
});
|
||||
const stopThreadSubagent = useAtomCommand(threadEnvironment.stopSubagent, {
|
||||
reportFailure: false,
|
||||
});
|
||||
const respondToThreadApproval = useAtomCommand(threadEnvironment.respondToApproval, {
|
||||
reportFailure: false,
|
||||
});
|
||||
@@ -3292,6 +3296,19 @@ function ChatViewContent(props: ChatViewProps) {
|
||||
if (!activeThreadRef) return;
|
||||
useRightPanelStore.getState().open(activeThreadRef, "agents");
|
||||
}, [activeThreadRef]);
|
||||
const onStopWorkflow = useCallback(
|
||||
(subagentId: NodeId) => {
|
||||
if (!activeThreadRef) return;
|
||||
void stopThreadSubagent({
|
||||
environmentId: activeThreadRef.environmentId,
|
||||
input: {
|
||||
threadId: activeThreadRef.threadId,
|
||||
subagentId,
|
||||
},
|
||||
});
|
||||
},
|
||||
[activeThreadRef, stopThreadSubagent],
|
||||
);
|
||||
const openChangesFromThreadPanel = useCallback(() => {
|
||||
addDiffSurface();
|
||||
}, [addDiffSurface]);
|
||||
@@ -6283,6 +6300,7 @@ function ChatViewContent(props: ChatViewProps) {
|
||||
runs={serverProjection?.runs ?? EMPTY_PROJECTION_RUNS}
|
||||
subagents={serverProjection?.subagents ?? EMPTY_PROJECTION_SUBAGENTS}
|
||||
onOpenAgentsPanel={addAgentsSurface}
|
||||
onStopWorkflow={onStopWorkflow}
|
||||
anchorMessageId={timelineAnchorMessageId}
|
||||
onAnchorReady={onTimelineAnchorReady}
|
||||
onAnchorSizeChanged={onTimelineAnchorSizeChanged}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import {
|
||||
type EnvironmentId,
|
||||
type MessageId,
|
||||
type NodeId,
|
||||
type OrchestrationV2Subagent,
|
||||
type OrchestrationV2TurnItem,
|
||||
type RunAttemptId,
|
||||
@@ -170,6 +171,8 @@ interface TimelineRowSharedState {
|
||||
onToggleAttemptFold: (attemptId: RunAttemptId) => void;
|
||||
/** Opens the right-panel Agents surface; null when the host view has none. */
|
||||
onOpenAgentsPanel: (() => void) | null;
|
||||
/** Requests a stop for one workflow/subagent task; null when unavailable. */
|
||||
onStopWorkflow: ((subagentId: NodeId) => void) | null;
|
||||
}
|
||||
|
||||
interface TimelineRowActivityState {
|
||||
@@ -237,6 +240,7 @@ interface MessagesTimelineProps {
|
||||
/** Live projection subagents — feeds the inline workflow run cards. */
|
||||
subagents?: ReadonlyArray<OrchestrationV2Subagent>;
|
||||
onOpenAgentsPanel?: (() => void) | null;
|
||||
onStopWorkflow?: ((subagentId: NodeId) => void) | null;
|
||||
anchorMessageId: MessageId | null;
|
||||
onAnchorReady: (messageId: MessageId, anchorIndex: number) => void;
|
||||
onAnchorSizeChanged: (messageId: MessageId, size: number) => void;
|
||||
@@ -279,6 +283,7 @@ export const MessagesTimeline = memo(function MessagesTimeline({
|
||||
runs: runsProp = EMPTY_TIMELINE_RUNS,
|
||||
subagents = EMPTY_TIMELINE_SUBAGENTS,
|
||||
onOpenAgentsPanel = null,
|
||||
onStopWorkflow = null,
|
||||
anchorMessageId,
|
||||
onAnchorReady,
|
||||
onAnchorSizeChanged,
|
||||
@@ -497,6 +502,7 @@ export const MessagesTimeline = memo(function MessagesTimeline({
|
||||
onToggleTurnFold,
|
||||
onToggleAttemptFold,
|
||||
onOpenAgentsPanel,
|
||||
onStopWorkflow,
|
||||
}),
|
||||
[
|
||||
timestampFormat,
|
||||
@@ -517,6 +523,7 @@ export const MessagesTimeline = memo(function MessagesTimeline({
|
||||
onToggleTurnFold,
|
||||
onToggleAttemptFold,
|
||||
onOpenAgentsPanel,
|
||||
onStopWorkflow,
|
||||
],
|
||||
);
|
||||
const activityState = useMemo<TimelineRowActivityState>(
|
||||
@@ -1446,7 +1453,14 @@ function V2SubagentEventRow(props: {
|
||||
);
|
||||
const hiddenMemberIds = useMemo(() => orchestrationV2WorkflowMemberIds(subagents), [subagents]);
|
||||
if (card !== null) {
|
||||
return <WorkflowRunCardV2 card={card} onOpenDetails={ctx.onOpenAgentsPanel ?? undefined} />;
|
||||
const onStopWorkflow = ctx.onStopWorkflow;
|
||||
return (
|
||||
<WorkflowRunCardV2
|
||||
card={card}
|
||||
onOpenDetails={ctx.onOpenAgentsPanel ?? undefined}
|
||||
onStop={onStopWorkflow === null ? undefined : () => onStopWorkflow(props.item.subagentId)}
|
||||
/>
|
||||
);
|
||||
}
|
||||
if (hiddenMemberIds.has(props.item.subagentId)) {
|
||||
return null;
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
type ChatAttachment,
|
||||
type MessageId,
|
||||
type ModelSelection,
|
||||
type NodeId,
|
||||
type OrchestrationV2Command,
|
||||
type OrchestrationV2CreationSource,
|
||||
type PlanId,
|
||||
@@ -151,6 +152,10 @@ export interface InterruptThreadTurnInput extends ThreadCommandInput {
|
||||
readonly turnId?: string;
|
||||
}
|
||||
|
||||
export interface StopThreadSubagentInput extends ThreadCommandInput {
|
||||
readonly subagentId: NodeId;
|
||||
}
|
||||
|
||||
export interface RespondToThreadApprovalInput extends ThreadCommandInput {
|
||||
readonly requestId: RuntimeRequestId;
|
||||
readonly decision: ProviderApprovalDecision;
|
||||
@@ -616,6 +621,17 @@ export const interruptThreadTurn = Effect.fn("EnvironmentCommands.interruptThrea
|
||||
});
|
||||
});
|
||||
|
||||
export const stopThreadSubagent = Effect.fn("EnvironmentCommands.stopThreadSubagent")(function* (
|
||||
input: StopThreadSubagentInput,
|
||||
) {
|
||||
return yield* dispatch({
|
||||
type: "subagent.stop",
|
||||
commandId: yield* allocateCommandId(input),
|
||||
threadId: input.threadId,
|
||||
subagentId: input.subagentId,
|
||||
});
|
||||
});
|
||||
|
||||
export const respondToThreadApproval = Effect.fn("EnvironmentCommands.respondToThreadApproval")(
|
||||
function* (input: RespondToThreadApprovalInput) {
|
||||
return yield* dispatch({
|
||||
|
||||
@@ -23,6 +23,7 @@ import {
|
||||
type SnoozeThreadInput,
|
||||
type StartThreadTurnInput,
|
||||
type StopThreadSessionInput,
|
||||
type StopThreadSubagentInput,
|
||||
type UnarchiveThreadInput,
|
||||
type UnsettleThreadInput,
|
||||
type UnsnoozeThreadInput,
|
||||
@@ -48,6 +49,7 @@ import {
|
||||
snoozeThread,
|
||||
startThreadTurn,
|
||||
stopThreadSession,
|
||||
stopThreadSubagent,
|
||||
unarchiveThread,
|
||||
unsettleThread,
|
||||
unsnoozeThread,
|
||||
@@ -77,6 +79,7 @@ export type {
|
||||
SnoozeThreadInput,
|
||||
StartThreadTurnInput,
|
||||
StopThreadSessionInput,
|
||||
StopThreadSubagentInput,
|
||||
ThreadCommandInput,
|
||||
UnarchiveThreadInput,
|
||||
UnsettleThreadInput,
|
||||
@@ -185,6 +188,12 @@ export function createThreadEnvironmentAtoms<R, E>(
|
||||
scheduler,
|
||||
concurrency,
|
||||
}),
|
||||
stopSubagent: createEnvironmentCommand(runtime, {
|
||||
label: "environment-data:commands:thread:stop-subagent",
|
||||
execute: (input: StopThreadSubagentInput) => stopThreadSubagent(input),
|
||||
scheduler,
|
||||
concurrency,
|
||||
}),
|
||||
respondToApproval: createEnvironmentCommand(runtime, {
|
||||
label: "environment-data:commands:thread:respond-to-approval",
|
||||
execute: (input: RespondToThreadApprovalInput) => respondToThreadApproval(input),
|
||||
|
||||
@@ -2163,6 +2163,14 @@ export const OrchestrationV2Command = Schema.Union([
|
||||
runId: RunId,
|
||||
reason: Schema.optional(Schema.String),
|
||||
}),
|
||||
Schema.Struct({
|
||||
/** Stop one background subagent task (e.g. a workflow run) without
|
||||
* interrupting the run that launched it. */
|
||||
type: Schema.Literal("subagent.stop"),
|
||||
commandId: CommandId,
|
||||
threadId: ThreadId,
|
||||
subagentId: NodeId,
|
||||
}),
|
||||
Schema.Struct({
|
||||
type: Schema.Literal("queued-message.promote-to-steer"),
|
||||
commandId: CommandId,
|
||||
|
||||
Reference in new issue
Block a user