Skip to content

Commit dba634e

Browse files
AlexChambersclaude
andcommitted
fix(server): messages from other threads no longer reset a pending question
When a Claude turn was waiting on AskUserQuestion, a t3_thread_send in auto mode steered into it. Claude aborted the question and asked it again, while T3 kept the old card pending, so the user's answers went nowhere and they had to retype them. Auto sends now queue behind a turn that is waiting on a live question or approval. When Claude still abandons a request, such as on an explicit Steer, the adapter marks it cancelled so the dead card closes. Fixes #15517 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 066c8fc commit dba634e

6 files changed

Lines changed: 321 additions & 4 deletions

File tree

‎apps/server/src/mcp/toolkits/orchestrator/tools.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ const ThreadUpdateTool = Tool.make("t3_thread_update", {
234234

235235
const ThreadSendTool = Tool.make("t3_thread_send", {
236236
description:
237-
"Send a message to any T3 thread in this environment. The target cannot have broader permission modes than the caller. Use delegate_task with the full review context and a new clientRequestId for independent review rounds. For a follow-up that needs the same child's conversation, send to its childThreadId. After the original task completes, a requested follow-up creates a fresh task in that child thread and returns taskId when it starts a turn or restarts a turn without its own task; its completion notifies the parent, and the original result stays unchanged. A steer that joins a running turn returns that turn's task, if it has one. mode='auto' starts an idle thread, steers a fully active turn, or queues behind a turn that is not yet steerable. Use queue for a separate follow-up turn, steer for an in-flight update, or restart to interrupt-and-restart the active turn. clientRequestId makes retries idempotent.",
237+
"Send a message to any T3 thread in this environment. The target cannot have broader permission modes than the caller. Use delegate_task with the full review context and a new clientRequestId for independent review rounds. For a follow-up that needs the same child's conversation, send to its childThreadId. After the original task completes, a requested follow-up creates a fresh task in that child thread and returns taskId when it starts a turn or restarts a turn without its own task; its completion notifies the parent, and the original result stays unchanged. A steer that joins a running turn returns that turn's task, if it has one. mode='auto' starts an idle thread, steers a fully active turn, or queues behind a turn that is not yet steerable or is waiting on the user's answer. Use queue for a separate follow-up turn, steer for an in-flight update, or restart to interrupt-and-restart the active turn. clientRequestId makes retries idempotent.",
238238
parameters: OrchestratorMcpThreadSendInput,
239239
success: OrchestratorMcpThreadSendResult,
240240
failure: OrchestratorMcpFailure,

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,117 @@ describe("ClaudeAdapterV2 resume compaction", () => {
13311331
);
13321332
});
13331333

1334+
describe("ClaudeAdapterV2 abandoned questions", () => {
1335+
it.effect("closes a question Claude stopped waiting on", () =>
1336+
Effect.scoped(
1337+
Effect.gen(function* () {
1338+
const fileSystem = yield* FileSystem.FileSystem;
1339+
const idAllocator = yield* IdAllocator.IdAllocatorV2;
1340+
const attachmentsDir = yield* fileSystem.makeTempDirectoryScoped({
1341+
prefix: "t3-claude-abandoned-question-",
1342+
});
1343+
let openedOptions: ClaudeAdapterV2.ClaudeAgentSdkQueryOptions | undefined;
1344+
const adapter = yield* ClaudeAdapterV2.makeClaudeAdapterV2({
1345+
instanceId: ClaudeAdapterV2.CLAUDE_DEFAULT_INSTANCE_ID,
1346+
settings: DEFAULT_CLAUDE_SETTINGS,
1347+
environment: {},
1348+
attachmentsDir,
1349+
fileSystem,
1350+
path: yield* Path.Path,
1351+
crypto: yield* Crypto.Crypto,
1352+
idAllocator,
1353+
queryRunner: {
1354+
allocateSessionId: Effect.succeed("native-thread-claude-abandoned-question"),
1355+
open: (input) =>
1356+
Effect.sync(() => {
1357+
openedOptions = input.options;
1358+
return {
1359+
messages: Stream.never,
1360+
offer: () => Effect.void,
1361+
setModel: () => Effect.void,
1362+
applyFlagSettings: () => Effect.void,
1363+
setPermissionMode: () => Effect.void,
1364+
backgroundTasks: () => Effect.succeed(false),
1365+
stopTask: () => Effect.void,
1366+
interrupt: Effect.void,
1367+
close: Effect.void,
1368+
};
1369+
}),
1370+
forkSession: () => Effect.die("unused"),
1371+
subagentLaunchToolUseId: () => Effect.succeed(null),
1372+
assertComplete: Effect.void,
1373+
},
1374+
});
1375+
const threadId = ThreadId.make("thread-claude-abandoned-question");
1376+
const runtime = yield* adapter.openSession({
1377+
threadId,
1378+
providerSessionId: ProviderSessionId.make("provider-session-claude-abandoned-question"),
1379+
modelSelection: CLAUDE_TEST_MODEL_SELECTION,
1380+
runtimePolicy: CLAUDE_TEST_RUNTIME_POLICY,
1381+
});
1382+
const providerThread = yield* runtime.ensureThread({
1383+
threadId,
1384+
modelSelection: CLAUDE_TEST_MODEL_SELECTION,
1385+
runtimePolicy: CLAUDE_TEST_RUNTIME_POLICY,
1386+
});
1387+
yield* runtime.startTurn(
1388+
makeClaudeTestTurnInput({
1389+
threadId,
1390+
providerThread,
1391+
now: yield* DateTime.now,
1392+
attemptId: RunAttemptId.make("attempt-claude-abandoned-question"),
1393+
text: "ask me",
1394+
attachments: [],
1395+
}),
1396+
);
1397+
const canUseTool = openedOptions?.canUseTool;
1398+
assert.isFunction(canUseTool);
1399+
const controller = new AbortController();
1400+
const requestUpdates = yield* runtime.events.pipe(
1401+
Stream.filter((event) => event.type === "runtime_request.updated"),
1402+
Stream.tap(() => Effect.sync(() => controller.abort())),
1403+
Stream.take(2),
1404+
Stream.runCollect,
1405+
Effect.forkScoped,
1406+
);
1407+
const result = yield* Effect.promise(() =>
1408+
canUseTool!(
1409+
"AskUserQuestion",
1410+
{
1411+
questions: [
1412+
{
1413+
header: "Color",
1414+
question: "Which color?",
1415+
options: [
1416+
{ label: "Red", description: "Pick red." },
1417+
{ label: "Blue", description: "Pick blue." },
1418+
],
1419+
multiSelect: false,
1420+
},
1421+
],
1422+
},
1423+
{
1424+
signal: controller.signal,
1425+
toolUseID: "tool-abandoned-question",
1426+
requestId: "request-abandoned-question",
1427+
},
1428+
),
1429+
).pipe(Effect.forkScoped);
1430+
1431+
assert.equal((yield* Fiber.join(result))?.behavior, "deny");
1432+
const statuses = Array.from(yield* Fiber.join(requestUpdates), (event) =>
1433+
event.type === "runtime_request.updated" ? event.runtimeRequest.status : null,
1434+
);
1435+
assert.deepEqual(statuses, ["pending", "cancelled"]);
1436+
}).pipe(
1437+
Effect.provide(
1438+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
1439+
),
1440+
),
1441+
),
1442+
);
1443+
});
1444+
13341445
describe("ClaudeAdapterV2 attachments", () => {
13351446
it.effect.each([
13361447
{

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6441,6 +6441,42 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
64416441
return { node, request, turnItem };
64426442
});
64436443

6444+
// A steer offered with priority "now" aborts a pending permission
6445+
// callback, and Claude asks again as a new request. Left pending, the
6446+
// old card takes answers Claude no longer waits for (#15517).
6447+
const cancelAbandonedRequest = Effect.fnUntraced(function* (artifacts: {
6448+
readonly node: OrchestrationV2ExecutionNode;
6449+
readonly request: OrchestrationV2RuntimeRequest;
6450+
readonly turnItem: OrchestrationV2TurnItem;
6451+
}) {
6452+
const now = yield* DateTime.now;
6453+
yield* Effect.all(
6454+
[
6455+
emitProviderEvent({
6456+
type: "runtime_request.updated",
6457+
driver: CLAUDE_PROVIDER,
6458+
runtimeRequest: { ...artifacts.request, status: "cancelled", resolvedAt: now },
6459+
}),
6460+
emitProviderEvent({
6461+
type: "node.updated",
6462+
driver: CLAUDE_PROVIDER,
6463+
node: { ...artifacts.node, status: "cancelled", completedAt: now },
6464+
}),
6465+
emitProviderEvent({
6466+
type: "turn_item.updated",
6467+
driver: CLAUDE_PROVIDER,
6468+
turnItem: {
6469+
...artifacts.turnItem,
6470+
status: "cancelled",
6471+
completedAt: now,
6472+
updatedAt: now,
6473+
},
6474+
}),
6475+
],
6476+
{ concurrency: 1 },
6477+
);
6478+
});
6479+
64446480
const ensureReasoningBlock = Effect.fnUntraced(function* (
64456481
context: ActiveClaudeTurnContext,
64466482
itemId: string,
@@ -8851,6 +8887,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
88518887
}),
88528888
),
88538889
);
8890+
if (callbackOptions.signal.aborted && !(yield* Deferred.isDone(answers))) {
8891+
yield* cancelAbandonedRequest(artifacts);
8892+
}
88548893
return callbackOptions.signal.aborted
88558894
? ({
88568895
behavior: "deny",
@@ -8963,6 +9002,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
89639002
}),
89649003
),
89659004
);
9005+
if (callbackOptions.signal.aborted && !(yield* Deferred.isDone(decision))) {
9006+
yield* cancelAbandonedRequest(artifacts);
9007+
}
89669008

89679009
return permissionResultFromDecision({
89689010
toolName,

‎apps/server/src/orchestration-v2/ThreadManagementService.test.ts‎

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
import { expect, it } from "@effect/vitest";
22
import {
33
CommandId,
4+
EventId,
45
MessageId,
56
type OrchestrationV2Run,
67
type OrchestrationV2StoredEvent,
78
type OrchestrationV2ThreadProjection,
89
ProjectId,
10+
ProviderDriverKind,
911
ProviderInstanceId,
12+
ProviderSessionId,
13+
ProviderTurnId,
1014
RunId,
15+
RuntimeRequestId,
1116
ThreadId,
1217
} from "@t3tools/contracts";
18+
import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
1319
import * as Deferred from "effect/Deferred";
1420
import * as DateTime from "effect/DateTime";
1521
import * as Effect from "effect/Effect";
@@ -20,10 +26,16 @@ import * as Stream from "effect/Stream";
2026

2127
import * as ServerSettings from "../serverSettings.ts";
2228
import * as SqlitePersistence from "../persistence/Sqlite.ts";
29+
import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
2330
import * as CommandReceiptStore from "./CommandReceiptStore.ts";
31+
import * as EffectOutbox from "./EffectOutbox.ts";
32+
import * as EventStore from "./EventStore.ts";
2433
import * as Orchestrator from "./Orchestrator.ts";
34+
import * as ProjectionStore from "./ProjectionStore.ts";
35+
import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts";
2536
import * as ProviderSessionManager from "./ProviderSessionManager.ts";
2637
import * as ThreadManagementService from "./ThreadManagementService.ts";
38+
import * as ProviderReplayHarness from "./testkit/ProviderReplayHarness.ts";
2739

2840
const layerReceiptsTest = CommandReceiptStore.layer.pipe(
2941
Layer.provide(SqlitePersistence.layerMemory),
@@ -337,3 +349,129 @@ it.effect.each([
337349
expect(dispatched).toEqual(settles ? ["thread.settle"] : []);
338350
}),
339351
);
352+
353+
it.effect("an auto send queues behind a turn waiting on the user's answer", () => {
354+
const projectId = ProjectId.make("project:thread-management:auto-send");
355+
const threadId = ThreadId.make("thread:thread-management:auto-send");
356+
const instanceId = ProviderInstanceId.make("codex");
357+
const layerDatabase = SqlitePersistence.layerMemory;
358+
const adapter = {
359+
instanceId,
360+
driver: ProviderDriverKind.make("codex"),
361+
getCapabilities: () => Effect.succeed(CodexProviderCapabilitiesV2),
362+
planSelectionTransition: () => Effect.succeed({ type: "apply_on_next_turn" as const }),
363+
openSession: () => Effect.die("Runs here never reach a provider"),
364+
} as ProviderAdapter.ProviderAdapterV2["Service"];
365+
const layerTest = ThreadManagementService.layer.pipe(
366+
Layer.provideMerge(
367+
Layer.mergeAll(
368+
layerDatabase,
369+
ProjectionStore.layer.pipe(Layer.provide(layerDatabase)),
370+
EventStore.layer.pipe(Layer.provide(layerDatabase)),
371+
EffectOutbox.layer.pipe(Layer.provide(layerDatabase)),
372+
ProviderReplayHarness.layerWithRegistry(
373+
{ name: "auto-send" },
374+
ProviderAdapterRegistry.layerFromAdapters([adapter]),
375+
{ databaseLayer: layerDatabase, runEffectWorker: false },
376+
),
377+
),
378+
),
379+
Layer.provideMerge(ServerSettings.layerTest()),
380+
Layer.provideMerge(CommandReceiptStore.layer.pipe(Layer.provide(layerDatabase))),
381+
);
382+
383+
return Effect.gen(function* () {
384+
const orchestrator = yield* Orchestrator.OrchestratorV2;
385+
const projections = yield* ProjectionStore.ProjectionStoreV2;
386+
const threads = yield* ThreadManagementService.ThreadManagementService;
387+
yield* orchestrator.dispatch({
388+
type: "thread.create",
389+
commandId: CommandId.make("command:auto-send:create"),
390+
threadId,
391+
projectId,
392+
title: "Auto send",
393+
modelSelection: { instanceId, model: "gpt-5.1-codex" },
394+
runtimeMode: "full-access",
395+
interactionMode: "default",
396+
branch: null,
397+
worktreePath: null,
398+
createdBy: "user",
399+
creationSource: "web",
400+
});
401+
yield* orchestrator.dispatch({
402+
type: "message.dispatch",
403+
commandId: CommandId.make("command:auto-send:first"),
404+
threadId,
405+
messageId: MessageId.make("message:auto-send:first"),
406+
text: "Ask me a question",
407+
attachments: [],
408+
dispatchMode: { type: "start_immediately" },
409+
createdBy: "user",
410+
creationSource: "web",
411+
});
412+
const now = yield* DateTime.now;
413+
const run = (yield* orchestrator.getThreadProjection(threadId)).runs[0]!;
414+
const providerTurnId = ProviderTurnId.make("provider-turn:auto-send");
415+
yield* projections.apply({
416+
id: EventId.make("event:auto-send:running"),
417+
type: "run.updated",
418+
threadId,
419+
runId: run.id,
420+
occurredAt: now,
421+
payload: { ...run, status: "running", startedAt: now },
422+
});
423+
yield* projections.apply({
424+
id: EventId.make("event:auto-send:turn"),
425+
type: "provider-turn.updated",
426+
threadId,
427+
occurredAt: now,
428+
payload: {
429+
id: providerTurnId,
430+
providerThreadId: run.providerThreadId!,
431+
nodeId: run.rootNodeId!,
432+
runAttemptId: run.activeAttemptId!,
433+
nativeTurnRef: null,
434+
ordinal: 1,
435+
status: "running",
436+
startedAt: now,
437+
completedAt: null,
438+
},
439+
});
440+
yield* projections.apply({
441+
id: EventId.make("event:auto-send:question"),
442+
type: "runtime-request.updated",
443+
threadId,
444+
runId: run.id,
445+
occurredAt: now,
446+
payload: {
447+
id: RuntimeRequestId.make("request:auto-send"),
448+
nodeId: run.rootNodeId!,
449+
providerTurnId,
450+
nativeRequestRef: null,
451+
kind: "user_input",
452+
status: "pending",
453+
responseCapability: {
454+
type: "live",
455+
providerSessionId: ProviderSessionId.make("provider-session:auto-send"),
456+
},
457+
createdAt: now,
458+
resolvedAt: null,
459+
},
460+
});
461+
462+
const sent = yield* threads.sendToThread({
463+
projectId,
464+
commandId: CommandId.make("command:auto-send:other-thread"),
465+
threadId,
466+
messageId: MessageId.make("message:auto-send:other-thread"),
467+
text: "A message from another thread",
468+
attachments: [],
469+
mode: "auto",
470+
createdBy: "agent",
471+
creationSource: "mcp",
472+
});
473+
474+
expect(sent.delivery).toBe("queued");
475+
expect(sent.run.status).toBe("queued");
476+
}).pipe(Effect.provide(layerTest));
477+
});

0 commit comments

Comments
 (0)