Skip to content

Commit 3dcf4ab

Browse files
t3dotggclaude
andcommitted
fix(provider-muse): show a child's answer once and recover child gaps
A subagent that settled before its child transcript loaded showed its final answer twice: once synthesized, once from the transcript. The synthesized result now waits for the first transcript read and is skipped when the transcript already ends with it. A view/gap on a child session was dropped silently. It now reads the child session back and publishes what was missed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent eed5e87 commit 3dcf4ab

2 files changed

Lines changed: 162 additions & 26 deletions

File tree

‎packages/provider-muse/src/server/adapter.test.ts‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,105 @@ describe("MuseAdapterV2", () => {
12701270
}).pipe(Effect.scoped, Effect.provide(testLayer)),
12711271
);
12721272

1273+
it.effect("shows a child's answer once when its transcript loads late, and recovers a gap", () =>
1274+
Effect.gen(function* () {
1275+
const fake = yield* makeFakeMuse();
1276+
const harness = yield* makeHarness(fake);
1277+
const { nativeId } = yield* startConversation(harness, fake);
1278+
const rootSessionId = String(
1279+
fake.calls.find((call) => call.method === "session/start")!.params.sessionId,
1280+
);
1281+
const childSession = { sessionId: "child-session-1", modelId: MODEL, activeTurnId: null };
1282+
const answer: MuseItem = {
1283+
itemId: "child-message",
1284+
kind: "agentMessage",
1285+
revision: 1,
1286+
status: "completed",
1287+
turnId: "child-turn",
1288+
text: "PROBEOK done",
1289+
};
1290+
const slowRead = pendingPromise<Record<string, unknown>>();
1291+
fake.queueResponse("session/read", slowRead.promise);
1292+
const subagentItem = (revision: number, extra: Record<string, unknown> = {}) => ({
1293+
itemId: "sub-1",
1294+
kind: "subagent",
1295+
revision,
1296+
status: "inProgress",
1297+
turnId: nativeId,
1298+
subagentId: "subagent-1",
1299+
childSessionId: "child-session-1",
1300+
objective: "Reply PROBEOK",
1301+
...extra,
1302+
});
1303+
yield* fake.emit("item/started", {
1304+
sessionId: rootSessionId,
1305+
turnId: nativeId,
1306+
item: subagentItem(1, { controlStatus: "running" }),
1307+
});
1308+
const childThreadId = (yield* harness.takeEvent("app_thread.created")).appThread.id;
1309+
yield* fake.takeCall("session/read");
1310+
// The subagent settles while its transcript is still loading.
1311+
yield* fake.emit("item/updated", {
1312+
sessionId: rootSessionId,
1313+
turnId: nativeId,
1314+
item: subagentItem(2, {
1315+
controlStatus: "resultReady",
1316+
result: { summary: "PROBEOK done", text: "PROBEOK done" },
1317+
}),
1318+
});
1319+
yield* harness.takeEvent(
1320+
"subagent.updated",
1321+
(event) => event.subagent.status === "completed",
1322+
);
1323+
slowRead.resolve({ session: childSession, history: { items: [answer] } });
1324+
yield* harness.takeEvent(
1325+
"message.updated",
1326+
(event) => event.message.threadId === childThreadId && event.message.role === "assistant",
1327+
);
1328+
// A gap on the child session reads it back and shows what was missed.
1329+
fake.queueResponse("session/read", {
1330+
session: childSession,
1331+
history: {
1332+
items: [
1333+
{
1334+
itemId: "child-tool",
1335+
kind: "toolCall",
1336+
revision: 1,
1337+
status: "completed",
1338+
turnId: "child-turn",
1339+
tool: "bash",
1340+
args: JSON.stringify({ command: "echo PROBEOK" }),
1341+
visibleOutput: "PROBEOK",
1342+
},
1343+
answer,
1344+
] satisfies MuseItem[],
1345+
},
1346+
});
1347+
yield* fake.emit("view/gap", { sessionId: "child-session-1" });
1348+
yield* harness.takeEvent(
1349+
"turn_item.updated",
1350+
(event) =>
1351+
event.turnItem.threadId === childThreadId && event.turnItem.type === "command_execution",
1352+
);
1353+
yield* fake.emit("turn/completed", {
1354+
sessionId: rootSessionId,
1355+
turnId: nativeId,
1356+
terminal: "completed",
1357+
});
1358+
yield* harness.takeEvent("turn.terminal");
1359+
const answerIds = new Set(
1360+
harness.allEvents.flatMap((event) =>
1361+
event.type === "message.updated" &&
1362+
event.message.threadId === childThreadId &&
1363+
event.message.text === "PROBEOK done"
1364+
? [event.message.id]
1365+
: [],
1366+
),
1367+
);
1368+
assert.strictEqual(answerIds.size, 1);
1369+
}).pipe(Effect.scoped, Effect.provide(testLayer)),
1370+
);
1371+
12731372
it.effect("keeps a prompt-and-result child thread when the child session cannot be read", () =>
12741373
Effect.gen(function* () {
12751374
const fake = yield* makeFakeMuse();

‎packages/provider-muse/src/server/adapter.ts‎

Lines changed: 63 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,11 @@ interface ChildContext {
279279
nextOrdinal: number;
280280
flushScheduled: boolean;
281281
status: OrchestrationV2TurnItemStatus;
282+
/** True until the first transcript read settles; the result waits for it. */
283+
transcriptLoading: boolean;
282284
lastAssistantText?: string;
285+
/** The subagent's final result, once it settled. */
286+
result?: string;
283287
}
284288

285289
const nativeRef = (nativeId: string) => ({
@@ -692,30 +696,57 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
692696
});
693697
yield* emit({ type: "turn_item.updated", driver: MUSE_PROVIDER, turnItem });
694698
});
699+
/**
700+
* Shows a settled subagent's result in its child thread, unless the child's
701+
* transcript already ends with it. Waits for the first transcript read so a
702+
* slow read cannot show the final answer twice.
703+
*/
704+
const publishChildResult = Effect.fnUntraced(function* (context: ChildContext) {
705+
const result = context.result;
706+
if (context.transcriptLoading || !result?.trim()) return;
707+
if (context.lastAssistantText?.trim() === result.trim()) return;
708+
yield* publishChildMessage(context, "assistant", result, "result", yield* DateTime.now);
709+
});
710+
/** A child session's items; fails when the host cannot address it. */
711+
const readChildItems = (context: ChildContext) =>
712+
request(
713+
"session/read",
714+
{ sessionId: context.childSessionId, excludeItems: false },
715+
false,
716+
).pipe(
717+
Effect.flatMap((value) => decode(MuseSessionResult, value)),
718+
Effect.map((result) =>
719+
result.session.sessionId === context.childSessionId
720+
? (result.history?.items ?? result.history?.snapshot?.state.items ?? [])
721+
: [],
722+
),
723+
);
724+
/** Publishes read-back child items newer than what the thread shows, then the result. */
725+
const applyChildItems = (context: ChildContext, items: ReadonlyArray<MuseItem>) =>
726+
Effect.gen(function* () {
727+
for (const item of items) {
728+
const previous = context.items.get(item.itemId);
729+
if (previous && previous.revision >= item.revision) continue;
730+
context.items.set(item.itemId, item);
731+
yield* publishChildItem(context, item);
732+
}
733+
context.transcriptLoading = false;
734+
yield* publishChildResult(context);
735+
}).pipe(eventPermit.withPermits(1));
695736
/**
696737
* Follows a child's own session: subscribe first so nothing is missed, then
697738
* read its history back. Hosts that cannot address an inline child session
698739
* yet (sessionNotFound) leave the prompt/result-only thread in place.
699740
*/
700741
const attachChildTranscript = (context: ChildContext): Effect.Effect<void> =>
701-
Effect.gen(function* () {
702-
yield* request("view/subscribe", { sessionId: context.childSessionId }, false);
703-
const result = yield* request(
704-
"session/read",
705-
{ sessionId: context.childSessionId, excludeItems: false },
706-
false,
707-
).pipe(Effect.flatMap((value) => decode(MuseSessionResult, value)));
708-
if (result.session.sessionId !== context.childSessionId) return;
709-
const items = result.history?.items ?? result.history?.snapshot?.state.items ?? [];
710-
yield* Effect.gen(function* () {
711-
for (const item of items) {
712-
const previous = context.items.get(item.itemId);
713-
if (previous && previous.revision >= item.revision) continue;
714-
context.items.set(item.itemId, item);
715-
yield* publishChildItem(context, item);
716-
}
717-
}).pipe(eventPermit.withPermits(1));
718-
}).pipe(Effect.ignore, Effect.forkIn(scope), Effect.asVoid);
742+
request("view/subscribe", { sessionId: context.childSessionId }, false).pipe(
743+
Effect.andThen(readChildItems(context)),
744+
Effect.orElseSucceed((): ReadonlyArray<MuseItem> => []),
745+
Effect.flatMap((items) => applyChildItems(context, items)),
746+
Effect.ignore,
747+
Effect.forkIn(scope),
748+
Effect.asVoid,
749+
);
719750
/** Creates the child thread for a native subagent the first time its session id is known. */
720751
const ensureChildContext = Effect.fnUntraced(function* (
721752
scope: SubagentParentScope,
@@ -768,6 +799,7 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
768799
nextOrdinal: 1,
769800
flushScheduled: false,
770801
status,
802+
transcriptLoading: true,
771803
};
772804
childrenBySessionId.set(childSessionId, context);
773805
childrenByItemId.set(item.itemId, context);
@@ -883,14 +915,10 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
883915
});
884916
if (context) {
885917
if (context.status !== status) yield* publishChildRoot(context, status, time);
886-
// The result is already the child's last assistant message when its
887-
// transcript attached; only synthesize it when it did not.
888-
const resultShown =
889-
result !== null &&
890-
context.lastAssistantText !== undefined &&
891-
context.lastAssistantText.trim() === result.trim();
892-
if (settled && result !== null && result.trim() && !resultShown)
893-
yield* publishChildMessage(context, "assistant", result, "result", time);
918+
if (settled && result !== null) {
919+
context.result = result;
920+
yield* publishChildResult(context);
921+
}
894922
}
895923
});
896924
/** Lists running workflows and subagents on the thread, so it shows it still waits on them. */
@@ -1520,6 +1548,15 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
15201548
method: string,
15211549
params: Record<string, unknown>,
15221550
) {
1551+
if (method === "view/gap") {
1552+
// Some child notifications were lost; read the child session back to catch up.
1553+
yield* readChildItems(context).pipe(
1554+
Effect.flatMap((items) => applyChildItems(context, items)),
1555+
Effect.ignore,
1556+
Effect.forkIn(scope),
1557+
);
1558+
return;
1559+
}
15231560
if (["item/started", "item/updated", "item/completed"].includes(method)) {
15241561
const item = (yield* decode(MuseItemEvent, params)).item;
15251562
const previous = context.items.get(item.itemId);

0 commit comments

Comments
 (0)