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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ it("readThread preserves caller access errors and prefers activity-run status ov
makeRun({ id: cancelledRunId, ordinal: 2, status: "cancelled" }),
],
visibleTurnItems: [],
turnItems: [],
providerTurns: [],
runtimeRequests: [],
messages: [],
Expand Down
44 changes: 35 additions & 9 deletions apps/server/src/mcp/OrchestratorMcpService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1785,12 +1785,12 @@ const make = Effect.gen(function* () {
: runStatus === "queued" || runStatus === "starting"
? runStatus
: "running";
const derivedResult =
task.result !== null
? task.result
: taskResultRun !== undefined && isTerminalTaskStatus(status)
? subagentResultForRun(childProjection, taskResultRun).text
: null;
const derived =
task.result === null && taskResultRun !== undefined && isTerminalTaskStatus(status)
? subagentResultForRun(childProjection, taskResultRun)
: undefined;
const derivedResult = task.result ?? derived?.text ?? null;
const workerAlert = task.result === null ? derived?.workerAlert : task.workerAlert;
const resultTransfers = childControls.contextTransfers.filter(
(transfer) =>
transfer.type === "subagent_result" &&
Expand Down Expand Up @@ -1839,6 +1839,7 @@ const make = Effect.gen(function* () {
providerInstanceId: modelSelection.instanceId,
model: modelSelection.model,
executedModel: runExecutedModel(childControls, executionRun),
...(workerAlert === undefined ? {} : { workerAlert }),
options: modelSelection.options ?? [],
summary: derivedResult,
resultContextTransferId,
Expand Down Expand Up @@ -2034,6 +2035,7 @@ const make = Effect.gen(function* () {
providerInstanceId: task.providerInstanceId,
model: task.model,
executedModel: task.executedModel ?? null,
...(task.workerAlert === undefined ? {} : { workerAlert: task.workerAlert }),
summary: task.result,
resultContextTransferId: null,
latestTerminalRunId: null,
Expand Down Expand Up @@ -3451,10 +3453,34 @@ const make = Effect.gen(function* () {
input.beforePosition === undefined &&
input.itemId === undefined,
);
const recentRuns = target.runs
const runs = target.runs
.toSorted((left, right) => right.ordinal - left.ordinal)
.slice(0, input.runLimit ?? DEFAULT_THREAD_RUN_LIMIT)
.map((run) => threadRun(target, run));
.slice(0, input.runLimit ?? DEFAULT_THREAD_RUN_LIMIT);
// Only an agent-created thread's latest run is judged, so a read loads one run's replies and notices.
const latestRun = runs[0];
const latestAlert =
latestRun === undefined ||
target.thread.createdBy !== "agent" ||
!ThreadManagementService.isTerminalRunStatus(latestRun.status)
? undefined
: subagentResultForRun(
{
...target,
...(yield* threadManagement
.getThreadRecords(input.threadId, ["messages", "turnItems"], {
messageRoles: ["assistant"],
messageRunIds: [latestRun.id],
turnItemRunId: latestRun.id,
turnItemTypes: ["assistant_message", "error", "system_notice"],
})
.pipe(Effect.mapError(threadManagementFailure))),
},
latestRun,
).workerAlert;
const recentRuns = runs.map((run) => ({
...threadRun(target, run),
...(run === latestRun && latestAlert !== undefined ? { workerAlert: latestAlert } : {}),
}));
const { items, start } = timelineItemsWithinBudget(
{ thread, recentRuns, nextPosition: null, hasMore: false, earlierPosition: null },
timeline.items.map((row) => ({
Expand Down
19 changes: 14 additions & 5 deletions apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10249,7 +10249,8 @@ describe("ClaudeAdapterV2 background wake turns", () => {
{ name: "hides a passive summary", level: "notice", visible: false },
{ name: "shows a blocking notice", level: "notice", preventContinuation: true, visible: true },
{ name: "shows a warning", level: "warning", visible: true },
])("Claude Stop-hook notices: $name", ({ level, preventContinuation, visible }) =>
{ name: "marks a safeguard fallback", fallbackModel: "claude-opus-4-8", visible: true },
])("Claude system notices: $name", ({ level, preventContinuation, fallbackModel, visible }) =>
Effect.gen(function* () {
const harness = yield* makeWakeHarness;
yield* harness.runtime.startTurn(
Expand All @@ -10267,9 +10268,17 @@ describe("ClaudeAdapterV2 background wake turns", () => {
harness.sdkMessages,
claudeSdkFrame({
type: "system",
subtype: "informational",
...(fallbackModel === undefined
? { subtype: "informational", level }
: {
subtype: "model_refusal_fallback",
trigger: "refusal",
direction: "retry",
original_model: "claude-opus-5-5",
fallback_model: fallbackModel,
request_id: null,
}),
content,
level,
...(preventContinuation === undefined
? {}
: { prevent_continuation: preventContinuation }),
Expand All @@ -10285,10 +10294,10 @@ describe("ClaudeAdapterV2 background wake turns", () => {
assert.deepEqual(
harness.events.flatMap((event) =>
event.type === "turn_item.updated" && event.turnItem.type === "system_notice"
? [event.turnItem.message]
? [[event.turnItem.message, event.turnItem.fallbackModel]]
: [],
),
visible ? [content] : [],
visible ? [[content, fallbackModel]] : [],
);
assert.isFalse(
harness.events.some(
Expand Down
3 changes: 3 additions & 0 deletions apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10983,6 +10983,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
status: "completed",
title: message.content,
message: message.content,
...(message.subtype === "model_refusal_fallback"
? { fallbackModel: message.fallback_model }
: {}),
startedAt: now,
completedAt: now,
updatedAt: now,
Expand Down
130 changes: 130 additions & 0 deletions apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4005,6 +4005,10 @@ it.layer(Layer.merge(layerTest, ProviderContinuationRequests.layer))(
],
});
yield* orchestrator.recoverDelegatedTasks;
const limited = (yield* orchestrator.getThreadProjection(parentThreadId)).subagents.find(
(task) => task.id === child.taskId,
);
assert.deepEqual(limited?.workerAlert?.reasons, ["usage_limit"]);
const childShell = Effect.map(orchestrator.getShellSnapshot(), (snapshot) =>
snapshot.threads.find((thread) => thread.id === child.childThreadId),
);
Expand Down Expand Up @@ -4740,6 +4744,132 @@ it.layer(layerTest)("delegated tasks across a server restart", (it) => {
}),
);

it.effect("tells the parent when a safeguard swapped its child's model and the child held", () =>
Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;
const eventSink = yield* EventSink.EventSinkV2;
const now = yield* DateTime.now;
const threadId = ThreadId.make("thread:worker-alert-parent");
const runId = RunId.make("run:worker-alert-parent");
const rootNodeId = NodeId.make("node:worker-alert-parent-root");
yield* seedParentWithTerminalTask({
threadId,
projectId: ProjectId.make("project:worker-alert-parent"),
runId,
rootNodeId,
taskId: NodeId.make("node:worker-alert-parent-settled"),
deliveryState: "delivered",
now,
});
const child = yield* seedRestartCancelledChild({
parentThreadId: threadId,
projectId: ProjectId.make("project:worker-alert-parent"),
parentRunId: runId,
rootNodeId,
name: "worker-alert-child",
completionWake: "always",
continuationPending: false,
runStatus: "completed",
now,
});
const item = {
threadId: child.childThreadId,
runId: child.childRunId,
nodeId: rootNodeId,
providerThreadId: null,
providerTurnId: null,
nativeItemRef: null,
parentItemId: null,
status: "completed" as const,
startedAt: now,
completedAt: now,
updatedAt: now,
};
const scope = {
threadId: child.childThreadId,
runId: child.childRunId,
nodeId: rootNodeId,
providerInstanceId: modelSelection.instanceId,
occurredAt: now,
};
const reply = "Holding until Kameron gives direct confirmation.";
yield* eventSink.write({
commandId: CommandId.make("command:worker-alert-child:turn"),
events: [
{
...scope,
id: EventId.make("event:worker-alert-child:fallback"),
type: "turn-item.updated",
payload: {
...item,
id: TurnItemId.make("item:worker-alert-child:fallback"),
type: "system_notice",
ordinal: 1,
title: "Switched models",
message: "Switched to claude-opus-4-8 after a safeguard.",
fallbackModel: "claude-opus-4-8",
},
},
{
...scope,
id: EventId.make("event:worker-alert-child:reply"),
type: "message.updated",
payload: {
id: MessageId.make("message:worker-alert-child:reply"),
threadId: child.childThreadId,
runId: child.childRunId,
nodeId: null,
role: "assistant",
text: reply,
attachments: [],
streaming: false,
createdBy: "agent",
creationSource: "server",
createdAt: now,
updatedAt: now,
},
},
],
});
yield* orchestrator.recoverDelegatedTask(child.childThreadId);
const parent = yield* orchestrator.getThreadProjection(threadId);
const task = parent.subagents.find((row) => row.id === child.taskId);
const notice =
"Worker alert: a provider safeguard switched it to claude-opus-4-8; its final message withholds the task. Replace the worker if it cannot do the task.";
assert.deepEqual(task?.workerAlert, {
reasons: ["provider_fallback", "withheld_task"],
notice,
});
assert.equal(task?.result, `${notice}\n\n${reply}`);

const delivery = parent.runs.find((run) => run.id === runId)?.delegatedCompletion?.delivery;
assert.deepEqual(delivery?.taskIds, [child.taskId]);
yield* orchestrator.dispatch({
type: "message.dispatch",
createdBy: "agent",
creationSource: "server",
commandId: CommandId.make("command:worker-alert-wake"),
threadId,
messageId: delivery!.messageId,
text: "Delegated task completed",
attachments: [],
dispatchMode: { type: "queue_after_active" },
delegatedCompletion: {
parentRunId: runId,
generation: delivery!.generation,
taskIds: delivery!.taskIds,
},
});
const wake = (yield* orchestrator.getThreadProjection(threadId)).messages.find(
(message) => message.id === delivery!.messageId,
);
assert.equal(
wake?.text,
`Delegated task ${child.taskId} [completed] (child <t3-thread://v1/${child.childThreadId}>):\n${notice}\n\n${reply}`,
);
}),
);

// Running does not prove prompt consumption in Claude; re-offer cut deliveries even when resuming the turn.
it.effect("re-offers a cut delivery even when a continuation resumes its turn", () =>
Effect.gen(function* () {
Expand Down
20 changes: 17 additions & 3 deletions apps/server/src/orchestration-v2/Orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ import { modelSwitchSince } from "./modelSwitch.ts";
import { isPlaceholderThreadTitle } from "./placeholderThreadTitle.ts";
import { combinePullRequestWatchWakes, type PullRequestWatchWake } from "./pullRequestWatch.ts";
import { recordThreadOpen } from "@t3tools/shared/paletteSearch";
import { formatThreadLink } from "@t3tools/shared/threadLinks";
import {
backgroundWorkHoldsCompletion,
derivePendingBackgroundWork,
Expand Down Expand Up @@ -931,7 +932,17 @@ function delegatedCompletionWakeDetail(
return taskIds
.map((taskId) => {
const task = tasks.find((candidate) => candidate.id === taskId);
const label = `Delegated task ${taskId}${task?.title == null ? "" : ` (${task.title})`}${task === undefined ? " reached a terminal state" : ` [${task.status}]`}`;
const child =
task?.childThreadId != null
? { environmentId: null, threadId: task.childThreadId }
: task?.remoteChild;
const alert =
task?.workerAlert === undefined
? ""
: child === undefined
? ""
: ` (child ${formatThreadLink(child, task.title ?? undefined)})`;
const label = `Delegated task ${taskId}${task?.title == null ? "" : ` (${task.title})`}${task === undefined ? " reached a terminal state" : ` [${task.status}]`}${alert}`;
if (task?.result != null) {
const detail = `${label}:\n${task.result}`;
const bytes = Buffer.byteLength(detail);
Expand All @@ -940,7 +951,7 @@ function delegatedCompletionWakeDetail(
return detail;
}
}
return `${label}. Use task_status with taskId ${taskId} to read the result.`;
return `${label}. ${task?.workerAlert === undefined ? "" : `${task.workerAlert.notice} `}Use task_status with taskId ${taskId} to read the result.`;
})
.join("\n\n");
}
Expand Down Expand Up @@ -10103,6 +10114,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
status: command.status,
result: command.result,
...(command.executedModel === undefined ? {} : { executedModel: command.executedModel }),
...(command.workerAlert === undefined ? {} : { workerAlert: command.workerAlert }),
completedAt: now,
updatedAt: now,
};
Expand Down Expand Up @@ -14513,11 +14525,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
const parentTurnItem = parentTask.turnItems.find(
(candidate) => candidate.type === "subagent" && candidate.subagentId === task.id,
);
const { workerAlert: _previousAlert, ...taskWithoutAlert } = task;
const updatedTask: OrchestrationV2Subagent = {
...task,
...taskWithoutAlert,
providerThreadId: childRun.providerThreadId,
status: terminalStatus,
result: result.text,
...(result.workerAlert === undefined ? {} : { workerAlert: result.workerAlert }),
completedAt: now,
updatedAt: now,
// The cancelling parent gets no wake, and the task records the run it settled from.
Expand Down
Loading
Loading