Skip to content

Commit 49c42d9

Browse files
fix(shared): remote delegated subagents no longer trigger completion notifications (#45297)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
1 parent 1b63f99 commit 49c42d9

4 files changed

Lines changed: 111 additions & 0 deletions

File tree

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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2849,6 +2849,81 @@ it.effect("SQL projection keeps background thread updates out of updatedAt", ()
28492849
backgroundThreadUpdatesKeepTimestamp.pipe(Effect.provide(layerTest)),
28502850
);
28512851

2852+
it.effect("awareness snapshots leave a linked environment's finished task to its parent", () =>
2853+
Effect.gen(function* () {
2854+
const store = yield* ProjectionStore.ProjectionStoreV2;
2855+
const now = yield* DateTime.now;
2856+
const threadId = ThreadId.make("thread:delegated-awareness");
2857+
const runId = RunId.make("run:delegated-awareness");
2858+
yield* store.apply({
2859+
id: EventId.make("event:delegated-awareness:created"),
2860+
type: "thread.created",
2861+
threadId,
2862+
occurredAt: now,
2863+
payload: {
2864+
createdBy: "agent",
2865+
creationSource: "server",
2866+
id: threadId,
2867+
projectId: ProjectId.make("project:delegated-awareness"),
2868+
title: "Delegated task",
2869+
providerInstanceId,
2870+
modelSelection,
2871+
runtimeMode: "full-access",
2872+
interactionMode: "default",
2873+
branch: null,
2874+
worktreePath: null,
2875+
activeProviderThreadId: null,
2876+
lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
2877+
delegatedFrom: {
2878+
environmentId: EnvironmentId.make("environment:parent"),
2879+
threadId: ThreadId.make("thread:parent-elsewhere"),
2880+
title: "Parent",
2881+
},
2882+
forkedFrom: null,
2883+
createdAt: now,
2884+
updatedAt: now,
2885+
archivedAt: null,
2886+
settledOverride: null,
2887+
settledAt: null,
2888+
lastVisitedAt: null,
2889+
deletedAt: null,
2890+
},
2891+
});
2892+
yield* store.apply({
2893+
id: EventId.make("event:delegated-awareness:run"),
2894+
type: "run.created",
2895+
threadId,
2896+
occurredAt: now,
2897+
payload: {
2898+
id: runId,
2899+
threadId,
2900+
ordinal: 1,
2901+
providerInstanceId,
2902+
modelSelection,
2903+
providerThreadId: null,
2904+
userMessageId: MessageId.make("message:delegated-awareness"),
2905+
rootNodeId: NodeId.make("node:delegated-awareness"),
2906+
activeAttemptId: null,
2907+
status: "completed",
2908+
requestedAt: now,
2909+
startedAt: now,
2910+
completedAt: now,
2911+
checkpointId: null,
2912+
contextHandoffId: null,
2913+
},
2914+
});
2915+
const [thread] = (yield* store.getAgentAwarenessSnapshot(threadId)).threads;
2916+
assert.equal(thread?.status, "completed");
2917+
assert.isNull(
2918+
projectThreadAwarenessV2({
2919+
environmentId: EnvironmentId.make("test"),
2920+
project: { title: "Test" },
2921+
thread: thread!,
2922+
}),
2923+
);
2924+
}).pipe(Effect.provide(layerTest)),
2925+
);
2926+
28522927
const runlessNativeSubagentNeedsRecovery = Effect.gen(function* () {
28532928
const store = yield* ProjectionStore.ProjectionStoreV2;
28542929
const threadId = yield* addRolledBackRecoveryCandidate("runless-native-recovery");

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9750,6 +9750,9 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, ProjectionStoreService
97509750
hasActionableProposedPlan: state.hasActionableProposedPlan,
97519751
title: state.thread.title,
97529752
lineage: state.thread.lineage,
9753+
...(state.thread.delegatedFrom == null
9754+
? {}
9755+
: { delegatedFrom: state.thread.delegatedFrom }),
97539756
modelSelection: state.thread.modelSelection,
97549757
settledOverride: state.thread.settledOverride,
97559758
updatedAt: state.updatedAt,

‎packages/shared/src/agentAwareness.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,34 @@ describe("projectThreadAwarenessV2", () => {
126126
).toBeNull();
127127
});
128128

129+
it("leaves a linked environment's task outcome to its parent", () => {
130+
const delegatedFrom = {
131+
environmentId: "env-parent" as EnvironmentId,
132+
threadId: "parent" as ThreadId,
133+
title: "Parent",
134+
};
135+
const phaseFor = (status: "running" | "completed" | "failed") =>
136+
projectThreadAwarenessV2({
137+
environmentId: "env-1" as EnvironmentId,
138+
project,
139+
thread: { ...v2Thread({ status }), delegatedFrom },
140+
})?.phase ?? null;
141+
expect(phaseFor("running")).toBe("running");
142+
expect(phaseFor("completed")).toBeNull();
143+
expect(phaseFor("failed")).toBeNull();
144+
});
145+
146+
it("still reports outcomes for threads stored with a null delegatedFrom", () => {
147+
expect(
148+
projectThreadAwarenessV2({
149+
environmentId: "env-1" as EnvironmentId,
150+
project,
151+
// MCP-launched threads store `delegatedFrom: null` at runtime.
152+
thread: { ...v2Thread({ status: "completed" }), delegatedFrom: null as never },
153+
})?.phase,
154+
).toBe("completed");
155+
});
156+
129157
it("keeps an older activity run visible over a newer cancelled run", () => {
130158
expect(
131159
projectThreadAwarenessV2({

‎packages/shared/src/agentAwareness.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export interface ProjectThreadAwarenessV2Input {
8787
readonly thread: Pick<
8888
OrchestrationV2ThreadShell,
8989
| "activityRunStatus"
90+
| "delegatedFrom"
9091
| "id"
9192
| "lineage"
9293
| "latestRunBackgroundWake"
@@ -114,6 +115,10 @@ export function projectThreadAwarenessV2(
114115
return null;
115116
}
116117
if (phase === "completed" && thread.latestRunStopRequested === true) return null;
118+
// Like a local subagent's, a linked environment's task outcome reaches the user through its parent.
119+
if (thread.delegatedFrom != null && (phase === "completed" || phase === "failed")) {
120+
return null;
121+
}
117122
// Resolve the phase first so live or waiting work stays visible after settlement.
118123
const runStatus = thread.activityRunStatus ?? thread.status;
119124
if (

0 commit comments

Comments
 (0)