Skip to content

Commit 8cf8dba

Browse files
committed
fix(muse): list background children under their turn item's native id
Muse listed running children on the background roster under itemId while their subagent turn items use childSessionId, then subagentId, then itemId. The roster now uses the same id and follows it as Muse reveals the child's ids. Fixes #23844
1 parent 2c8bdf6 commit 8cf8dba

2 files changed

Lines changed: 63 additions & 5 deletions

File tree

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,6 +1578,57 @@ describe("MuseAdapterV2", () => {
15781578
}).pipe(Effect.scoped, Effect.provide(testLayer)),
15791579
);
15801580

1581+
it.effect("lists a running subagent under its turn item's native id as Muse reveals it", () =>
1582+
Effect.gen(function* () {
1583+
const fake = yield* makeFakeMuse();
1584+
const harness = yield* makeHarness(fake);
1585+
const { nativeId } = yield* startConversation(harness, fake);
1586+
const revisions = [
1587+
{ revision: 1, status: "inProgress" },
1588+
{ revision: 2, status: "inProgress", subagentId: "subagent-1" },
1589+
{
1590+
revision: 3,
1591+
status: "inProgress",
1592+
subagentId: "subagent-1",
1593+
childSessionId: "child-session-1",
1594+
},
1595+
{
1596+
revision: 4,
1597+
status: "completed",
1598+
subagentId: "subagent-1",
1599+
childSessionId: "child-session-1",
1600+
},
1601+
];
1602+
const listed: Array<{ nativeId: string | null | undefined; roster: Array<string> }> = [];
1603+
for (const fields of revisions) {
1604+
yield* fake.emit(fields.revision === 1 ? "item/started" : "item/updated", {
1605+
turnId: nativeId,
1606+
item: { itemId: "sub-1", kind: "subagent", turnId: nativeId, ...fields },
1607+
});
1608+
const item = yield* harness.takeEvent(
1609+
"turn_item.updated",
1610+
(event) => event.turnItem.type === "subagent",
1611+
);
1612+
const roster = harness.allEvents.findLast(
1613+
(event) => event.type === "provider_thread.updated",
1614+
);
1615+
listed.push({
1616+
nativeId: item.turnItem.nativeItemRef?.nativeId,
1617+
roster:
1618+
roster?.type === "provider_thread.updated"
1619+
? (roster.providerThread.pendingBackgroundTasks ?? []).map((task) => task.taskId)
1620+
: [],
1621+
});
1622+
}
1623+
assert.deepStrictEqual(listed, [
1624+
{ nativeId: "sub-1", roster: ["sub-1"] },
1625+
{ nativeId: "subagent-1", roster: ["subagent-1"] },
1626+
{ nativeId: "child-session-1", roster: ["child-session-1"] },
1627+
{ nativeId: "child-session-1", roster: [] },
1628+
]);
1629+
}).pipe(Effect.scoped, Effect.provide(testLayer)),
1630+
);
1631+
15811632
it.effect("keeps a workflow child's first label after later revisions drop it", () =>
15821633
Effect.gen(function* () {
15831634
const fake = yield* makeFakeMuse();

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ const nativeRef = (nativeId: string) => ({
252252
nativeId,
253253
strength: "strong" as const,
254254
});
255+
/** The roster lists a child under its turn item's native id, so readers can pair the two. */
256+
const backgroundTaskId = (item: MuseItem) =>
257+
item.kind === "subagent" ? item.childSessionId || (item.subagentId ?? item.itemId) : item.itemId;
255258
const recordSchema = Schema.Record(Schema.String, Schema.Unknown);
256259
const INFORMATIONAL_NOTIFICATIONS = new Set([
257260
"session/contextUsage",
@@ -601,9 +604,7 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
601604
item.result?.summary ??
602605
(status === "failed" ? item.failureReason : undefined);
603606
const result = resultText ?? null;
604-
const nativeTaskRef = item.childSessionId
605-
? nativeRef(item.childSessionId)
606-
: nativeRef(item.subagentId ?? item.itemId);
607+
const nativeTaskRef = nativeRef(backgroundTaskId(item));
607608
const subagent: OrchestrationV2Subagent = {
608609
id: base.nodeId,
609610
threadId: base.threadId,
@@ -681,7 +682,7 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
681682
const description = item?.objective ?? item?.entryId ?? item?.fallbackText;
682683
return [
683684
{
684-
taskId: itemId,
685+
taskId: item ? backgroundTaskId(item) : itemId,
685686
kind:
686687
item?.kind === "subagent" ? ("subagent" as const) : ("background_task" as const),
687688
...(description ? { description } : {}),
@@ -720,7 +721,13 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
720721
? "cancelled"
721722
: "failed",
722723
});
723-
if (wasRunning !== observedChildren.has(item.itemId)) yield* syncBackground();
724+
const running = observedChildren.has(item.itemId);
725+
// Muse reveals a subagent's ids over several revisions, so a running child is
726+
// relisted whenever the id its turn item carries moves.
727+
const listed = thread?.pendingBackgroundTasks?.some(
728+
(task) => task.taskId === backgroundTaskId(item),
729+
);
730+
if (wasRunning !== running || (running && !listed)) yield* syncBackground();
724731
}
725732
const streaming = status === "running";
726733
const base = {

0 commit comments

Comments
 (0)