Skip to content

Commit b4ae3c8

Browse files
authored
fix(muse): list background children under their turn item's native id (pingdotgg#23856)
1 parent e5a2031 commit b4ae3c8

2 files changed

Lines changed: 65 additions & 5 deletions

File tree

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,6 +1739,59 @@ describe("MuseAdapterV2", () => {
17391739
}).pipe(Effect.scoped, Effect.provide(testLayer)),
17401740
);
17411741

1742+
it.effect("lists a running subagent under its turn item's native id as Muse reveals it", () =>
1743+
Effect.gen(function* () {
1744+
const fake = yield* makeFakeMuse();
1745+
const harness = yield* makeHarness(fake);
1746+
const { nativeId } = yield* startConversation(harness, fake);
1747+
const revisions = [
1748+
{ revision: 1, status: "inProgress" },
1749+
{ revision: 2, status: "inProgress", subagentId: "" },
1750+
{ revision: 3, status: "inProgress", subagentId: "subagent-1" },
1751+
{
1752+
revision: 4,
1753+
status: "inProgress",
1754+
subagentId: "subagent-1",
1755+
childSessionId: "child-session-1",
1756+
},
1757+
{
1758+
revision: 5,
1759+
status: "completed",
1760+
subagentId: "subagent-1",
1761+
childSessionId: "child-session-1",
1762+
},
1763+
];
1764+
const listed: Array<{ nativeId: string | null | undefined; roster: Array<string> }> = [];
1765+
for (const fields of revisions) {
1766+
yield* fake.emit(fields.revision === 1 ? "item/started" : "item/updated", {
1767+
turnId: nativeId,
1768+
item: { itemId: "sub-1", kind: "subagent", turnId: nativeId, ...fields },
1769+
});
1770+
const item = yield* harness.takeEvent(
1771+
"turn_item.updated",
1772+
(event) => event.turnItem.type === "subagent",
1773+
);
1774+
const roster = harness.allEvents.findLast(
1775+
(event) => event.type === "provider_thread.updated",
1776+
);
1777+
listed.push({
1778+
nativeId: item.turnItem.nativeItemRef?.nativeId,
1779+
roster:
1780+
roster?.type === "provider_thread.updated"
1781+
? (roster.providerThread.pendingBackgroundTasks ?? []).map((task) => task.taskId)
1782+
: [],
1783+
});
1784+
}
1785+
assert.deepStrictEqual(listed, [
1786+
{ nativeId: "sub-1", roster: ["sub-1"] },
1787+
{ nativeId: "sub-1", roster: ["sub-1"] },
1788+
{ nativeId: "subagent-1", roster: ["subagent-1"] },
1789+
{ nativeId: "child-session-1", roster: ["child-session-1"] },
1790+
{ nativeId: "child-session-1", roster: [] },
1791+
]);
1792+
}).pipe(Effect.scoped, Effect.provide(testLayer)),
1793+
);
1794+
17421795
it.effect("keeps a workflow child's first label after later revisions drop it", () =>
17431796
Effect.gen(function* () {
17441797
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
@@ -248,6 +248,9 @@ const nativeRef = (nativeId: string) => ({
248248
nativeId,
249249
strength: "strong" as const,
250250
});
251+
/** The roster lists a child under its turn item's native id, so readers can pair the two. */
252+
const backgroundTaskId = (item: MuseItem) =>
253+
item.kind === "subagent" ? item.childSessionId || item.subagentId || item.itemId : item.itemId;
251254
const recordSchema = Schema.Record(Schema.String, Schema.Unknown);
252255
const INFORMATIONAL_NOTIFICATIONS = new Set([
253256
"session/contextUsage",
@@ -597,9 +600,7 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
597600
item.result?.summary ??
598601
(status === "failed" ? item.failureReason : undefined);
599602
const result = resultText ?? null;
600-
const nativeTaskRef = item.childSessionId
601-
? nativeRef(item.childSessionId)
602-
: nativeRef(item.subagentId ?? item.itemId);
603+
const nativeTaskRef = nativeRef(backgroundTaskId(item));
603604
const subagent: OrchestrationV2Subagent = {
604605
id: base.nodeId,
605606
threadId: base.threadId,
@@ -677,7 +678,7 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
677678
const description = item?.objective ?? item?.entryId ?? item?.fallbackText;
678679
return [
679680
{
680-
taskId: itemId,
681+
taskId: item ? backgroundTaskId(item) : itemId,
681682
kind:
682683
item?.kind === "subagent" ? ("subagent" as const) : ("background_task" as const),
683684
...(description ? { description } : {}),
@@ -716,7 +717,13 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
716717
? "cancelled"
717718
: "failed",
718719
});
719-
if (wasRunning !== observedChildren.has(item.itemId)) yield* syncBackground();
720+
const running = observedChildren.has(item.itemId);
721+
// Muse reveals a subagent's ids over several revisions, so a running child is
722+
// relisted whenever the id its turn item carries moves.
723+
const listed = thread?.pendingBackgroundTasks?.some(
724+
(task) => task.taskId === backgroundTaskId(item),
725+
);
726+
if (wasRunning !== running || (running && !listed)) yield* syncBackground();
720727
}
721728
const streaming = status === "running";
722729
const base = {

0 commit comments

Comments
 (0)