Skip to content

Commit f468370

Browse files
author
PR Batch Tester
committed
fix(server): preserve execution context for native-owned tasks
1 parent 5b57002 commit f468370

3 files changed

Lines changed: 93 additions & 56 deletions

File tree

‎apps/server/src/mcp/NativeSubagentDelegation.integration.test.ts‎

Lines changed: 87 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,11 @@ it.effect(
578578
// Claude's adapter projects native subagents A and B at depth 1, and C under A.
579579
const a = h.nativeTask({
580580
name: "a",
581-
listing: root.projection.thread,
581+
listing: {
582+
...root.projection.thread,
583+
branch: "before-workspace-change",
584+
worktreePath: "/tmp/t3code-28873-review-followup/stale-native-workspace",
585+
},
582586
parentNodeId: root.run.rootNodeId!,
583587
execution,
584588
});
@@ -610,6 +614,11 @@ it.effect(
610614

611615
// Siblings with equal request keys get their own tasks, each listed by its caller.
612616
const fromA = yield* h.delegate(scope("claude-tool:a"), "same-key");
617+
const delegatedThread = (yield* h.orchestrator.getThreadProjection(fromA.childThreadId!))
618+
.thread;
619+
assert.equal(delegatedThread.projectId, root.projection.thread.projectId);
620+
assert.equal(delegatedThread.branch, root.projection.thread.branch);
621+
assert.equal(delegatedThread.worktreePath, root.projection.thread.worktreePath);
613622
const fromB = yield* h.delegate(scope("claude-tool:b"), "same-key");
614623
const fromC = yield* h.delegate(scope("claude-tool:c"), "same-key");
615624
const fromRoot = yield* h.delegate(scope(), "same-key");
@@ -1041,58 +1050,81 @@ it.effect("a stopped app child's task ends once what its native subagent delegat
10411050
),
10421051
);
10431052

1044-
it.effect("a question from a native subagent's task wakes the thread whose run executes it", () =>
1045-
Effect.scoped(
1046-
Effect.gen(function* () {
1047-
const h = yield* makeHarness("native-delegation-question");
1048-
const rootThreadId = ThreadId.make("thread:question-root");
1049-
const root = yield* h.startRoot(rootThreadId, "provider-session:question-root");
1050-
const native = h.nativeTask({
1051-
name: "asker",
1052-
listing: root.projection.thread,
1053-
parentNodeId: root.run.rootNodeId!,
1054-
execution: { threadId: rootThreadId, runId: root.run.id },
1055-
});
1056-
yield* h.project(root.turn, [native]);
1057-
yield* h.mcpSessions.bindNativeCaller({
1058-
threadId: rootThreadId,
1059-
callId: "claude-tool:asker",
1060-
callerThreadId: native.thread.id,
1061-
});
1062-
const task = yield* h.delegate(
1063-
scopeFor(rootThreadId, "provider-session:question-root", "claude-tool:asker"),
1064-
"asker",
1065-
);
1066-
const childRun = (yield* h.orchestrator.getThreadProjection(task.childThreadId!)).runs[0]!;
1067-
const now = yield* DateTime.now;
1068-
const question = {
1069-
id: EventId.make("event:native-task-question"),
1070-
type: "runtime-request.updated",
1071-
threadId: task.childThreadId!,
1072-
runId: childRun.id,
1073-
occurredAt: now,
1074-
payload: {
1075-
id: RuntimeRequestId.make("request:native-task-question"),
1076-
nodeId: childRun.rootNodeId!,
1077-
providerTurnId: null,
1078-
nativeRequestRef: null,
1079-
kind: "user_input",
1080-
status: "pending",
1081-
responseCapability: { type: "message" },
1082-
createdAt: now,
1083-
resolvedAt: null,
1084-
},
1085-
} satisfies OrchestrationV2DomainEvent;
1086-
yield* h.eventSink.write({ events: [question] });
1087-
yield* h.questions.handle(question);
1088-
const wake = (yield* h.orchestrator.getThreadProjection(rootThreadId)).messages.find(
1089-
(message) => message.notification?.source.kind === "delegated_task",
1090-
);
1091-
assert.deepEqual(wake?.notification?.source, {
1092-
kind: "delegated_task",
1093-
taskIds: [task.taskId],
1094-
childThreadId: task.childThreadId!,
1095-
});
1096-
}),
1097-
),
1053+
it.effect.each(["active", "archived", "deleted"] as const)(
1054+
"a native task's question wakes only its active execution thread: %s",
1055+
(executionState) =>
1056+
Effect.scoped(
1057+
Effect.gen(function* () {
1058+
const h = yield* makeHarness("native-delegation-question");
1059+
const rootThreadId = ThreadId.make("thread:question-root");
1060+
const root = yield* h.startRoot(rootThreadId, "provider-session:question-root");
1061+
const native = h.nativeTask({
1062+
name: "asker",
1063+
listing: root.projection.thread,
1064+
parentNodeId: root.run.rootNodeId!,
1065+
execution: { threadId: rootThreadId, runId: root.run.id },
1066+
});
1067+
yield* h.project(root.turn, [native]);
1068+
yield* h.mcpSessions.bindNativeCaller({
1069+
threadId: rootThreadId,
1070+
callId: "claude-tool:asker",
1071+
callerThreadId: native.thread.id,
1072+
});
1073+
const task = yield* h.delegate(
1074+
scopeFor(rootThreadId, "provider-session:question-root", "claude-tool:asker"),
1075+
"asker",
1076+
);
1077+
const childRun = (yield* h.orchestrator.getThreadProjection(task.childThreadId!)).runs[0]!;
1078+
const now = yield* DateTime.now;
1079+
if (executionState !== "active") {
1080+
const execution = (yield* h.orchestrator.getThreadProjection(rootThreadId)).thread;
1081+
yield* h.eventSink.write({
1082+
events: [
1083+
{
1084+
id: EventId.make("event:inactive-question-execution"),
1085+
type: "thread.metadata-updated",
1086+
threadId: rootThreadId,
1087+
occurredAt: now,
1088+
payload: {
1089+
...execution,
1090+
...(executionState === "archived" ? { archivedAt: now } : { deletedAt: now }),
1091+
},
1092+
},
1093+
],
1094+
});
1095+
}
1096+
const question = {
1097+
id: EventId.make("event:native-task-question"),
1098+
type: "runtime-request.updated",
1099+
threadId: task.childThreadId!,
1100+
runId: childRun.id,
1101+
occurredAt: now,
1102+
payload: {
1103+
id: RuntimeRequestId.make("request:native-task-question"),
1104+
nodeId: childRun.rootNodeId!,
1105+
providerTurnId: null,
1106+
nativeRequestRef: null,
1107+
kind: "user_input",
1108+
status: "pending",
1109+
responseCapability: { type: "message" },
1110+
createdAt: now,
1111+
resolvedAt: null,
1112+
},
1113+
} satisfies OrchestrationV2DomainEvent;
1114+
yield* h.eventSink.write({ events: [question] });
1115+
yield* h.questions.handle(question);
1116+
const wake = (yield* h.orchestrator.getThreadProjection(rootThreadId)).messages.find(
1117+
(message) => message.notification?.source.kind === "delegated_task",
1118+
);
1119+
if (executionState === "active") {
1120+
assert.deepEqual(wake?.notification?.source, {
1121+
kind: "delegated_task",
1122+
taskIds: [task.taskId],
1123+
childThreadId: task.childThreadId!,
1124+
});
1125+
} else {
1126+
assert.isUndefined(wake);
1127+
}
1128+
}),
1129+
),
10981130
);

‎apps/server/src/orchestration-v2/DelegatedTaskQuestionReactor.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ const make = Effect.gen(function* () {
150150
if (
151151
task === undefined ||
152152
parent.thread.archivedAt !== null ||
153-
parent.thread.deletedAt !== null
153+
parent.thread.deletedAt !== null ||
154+
execution.thread.archivedAt !== null ||
155+
execution.thread.deletedAt !== null
154156
) {
155157
return;
156158
}

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7870,6 +7870,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
78707870
createdBy: command.createdBy,
78717871
creationSource: command.creationSource,
78727872
}),
7873+
projectId: parentProjection.thread.projectId,
7874+
branch: parentProjection.thread.branch,
7875+
worktreePath: parentProjection.thread.worktreePath,
78737876
runtimeMode: command.runtimeMode,
78747877
interactionMode: command.interactionMode,
78757878
...(command.workspaceStrategy === undefined ? {} : { branch: null, worktreePath: null }),

0 commit comments

Comments
 (0)