Skip to content

Commit d026f33

Browse files
ScottN-PVclaude
andcommitted
fix(provider-muse): answer workflow child approvals before showing them
In full access, T3 approves a workflow child's approval request for the user because Muse does not pass allowAll to child sessions. The request was emitted as pending first, so each child tool call flashed an unclickable approval popup until the answer landed. Now the request is shown only if Muse rejects the automatic answer and still holds it. Closes #19289 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 09c5e7a commit d026f33

2 files changed

Lines changed: 232 additions & 24 deletions

File tree

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

Lines changed: 185 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -854,27 +854,204 @@ describe("MuseAdapterV2", () => {
854854
}).pipe(Effect.scoped, Effect.provide(testLayer)),
855855
);
856856

857-
it.effect("approves a workflow child's approval in full access after its turn ended", () =>
857+
it.effect.each([
858+
{ when: "after its turn ended", turnEnded: true },
859+
{ when: "while its turn runs", turnEnded: false },
860+
] as const)(
861+
"approves a workflow child's approval in full access $when without showing it",
862+
({ turnEnded }) =>
863+
Effect.gen(function* () {
864+
const fake = yield* makeFakeMuse();
865+
const harness = yield* makeHarness(fake);
866+
const { nativeId } = yield* startConversation(harness, fake);
867+
if (turnEnded) {
868+
yield* fake.emit("turn/completed", { turnId: nativeId, terminal: "completed" });
869+
yield* harness.takeEvent("turn.terminal");
870+
}
871+
const acknowledgement = pendingPromise<Record<string, unknown>>();
872+
fake.queueResponse("approval/decide", acknowledgement.promise);
873+
const childApproval = { ...approval("child-run-1"), subagentOrigin: { subagentId: "a7" } };
874+
yield* fake.emit("approval/requested", childApproval);
875+
const decision = yield* fake.takeCall("approval/decide");
876+
assert.strictEqual(decision.params.choiceId, "once");
877+
const snapshot = yield* harness.runtime.readThreadSnapshot!({
878+
providerThread: harness.providerThread,
879+
});
880+
assert.deepStrictEqual(snapshot.runtimeRequests, []);
881+
yield* fake.emit("approval/resolved", childApproval);
882+
const resolved = yield* harness.takeEvent("runtime_request.updated");
883+
assert.strictEqual(resolved.runtimeRequest.status, "resolved");
884+
assert.strictEqual(resolved.runtimeRequest.decision, "accept");
885+
assert.isNull(resolved.runtimeRequest.providerTurnId);
886+
acknowledgement.resolve({});
887+
assert.isFalse(
888+
harness.allEvents.some(
889+
(event) =>
890+
(event.type === "runtime_request.updated" &&
891+
event.runtimeRequest.status === "pending") ||
892+
(event.type === "provider_session.updated" &&
893+
event.providerSession.status === "waiting"),
894+
),
895+
);
896+
}).pipe(Effect.scoped, Effect.provide(testLayer)),
897+
);
898+
899+
it.effect("answers a workflow child's updated approval in full access without showing it", () =>
858900
Effect.gen(function* () {
859901
const fake = yield* makeFakeMuse();
860902
const harness = yield* makeHarness(fake);
861-
const { nativeId } = yield* startConversation(harness, fake);
862-
yield* fake.emit("turn/completed", { turnId: nativeId, terminal: "completed" });
863-
yield* harness.takeEvent("turn.terminal");
903+
yield* startConversation(harness, fake);
904+
const acknowledgement = pendingPromise<Record<string, unknown>>();
905+
fake.queueResponse("approval/decide", acknowledgement.promise);
906+
const childApproval = { ...approval("child-run-1"), subagentOrigin: { subagentId: "a7" } };
907+
yield* fake.emit("approval/requested", childApproval);
908+
yield* fake.takeCall("approval/decide");
909+
// Muse moves to the next requirement while T3's first answer is in flight.
910+
yield* fake.emit("approval/updated", {
911+
...childApproval,
912+
currentRequirementId: { approvalId: "approval-1", sourceIndex: 1 },
913+
});
914+
const second = yield* fake.takeCall("approval/decide");
915+
assert.deepStrictEqual(second.params.requirementId, {
916+
approvalId: "approval-1",
917+
sourceIndex: 1,
918+
});
919+
acknowledgement.reject(new Error("approval requirement changed"));
920+
yield* fake.emit("approval/resolved", childApproval);
921+
const resolved = yield* harness.takeEvent("runtime_request.updated");
922+
assert.strictEqual(resolved.runtimeRequest.status, "resolved");
923+
assert.strictEqual(resolved.runtimeRequest.decision, "accept");
924+
assert.isFalse(
925+
harness.allEvents.some(
926+
(event) =>
927+
event.type === "runtime_request.updated" && event.runtimeRequest.status === "pending",
928+
),
929+
);
930+
}).pipe(Effect.scoped, Effect.provide(testLayer)),
931+
);
932+
933+
it.effect(
934+
"stops waiting when a shown approval settles before a workflow child's hidden one",
935+
() =>
936+
Effect.gen(function* () {
937+
const fake = yield* makeFakeMuse();
938+
const harness = yield* makeHarness(fake);
939+
const { nativeId } = yield* startConversation(harness, fake);
940+
const acknowledgement = pendingPromise<Record<string, unknown>>();
941+
fake.queueResponse("approval/decide", acknowledgement.promise);
942+
const childApproval = { ...approval("child-run-1"), subagentOrigin: { subagentId: "a7" } };
943+
yield* fake.emit("approval/requested", childApproval);
944+
yield* fake.takeCall("approval/decide");
945+
const turnApproval = {
946+
...approval(nativeId),
947+
approvalId: "approval-2",
948+
currentRequirementId: { approvalId: "approval-2", sourceIndex: 0 },
949+
};
950+
yield* fake.emit("approval/requested", turnApproval);
951+
yield* harness.takeEvent(
952+
"provider_session.updated",
953+
(event) => event.providerSession.status === "waiting",
954+
);
955+
// The shown approval settles while T3's answer to the child is still in flight.
956+
yield* fake.emit("approval/resolved", turnApproval);
957+
acknowledgement.resolve({});
958+
yield* fake.emit("approval/resolved", childApproval);
959+
yield* harness.takeEvent(
960+
"runtime_request.updated",
961+
(event) => event.runtimeRequest.nativeRequestRef?.nativeId === "approval-1",
962+
);
963+
yield* harness.takeEvent("provider_session.updated");
964+
const sequence = harness.allEvents.flatMap((event) =>
965+
event.type === "runtime_request.updated"
966+
? [`${event.runtimeRequest.nativeRequestRef?.nativeId} ${event.runtimeRequest.status}`]
967+
: event.type === "provider_session.updated"
968+
? [`session ${event.providerSession.status}`]
969+
: [],
970+
);
971+
assert.deepStrictEqual(sequence.slice(sequence.indexOf("approval-2 pending")), [
972+
"approval-2 pending",
973+
"session waiting",
974+
"approval-2 resolved",
975+
"session running",
976+
"approval-1 resolved",
977+
"session running",
978+
]);
979+
}).pipe(Effect.scoped, Effect.provide(testLayer)),
980+
);
981+
982+
it.effect("shows a workflow child's approval when Muse rejects T3's automatic answer", () =>
983+
Effect.gen(function* () {
984+
const fake = yield* makeFakeMuse();
985+
const harness = yield* makeHarness(fake);
986+
yield* startConversation(harness, fake);
987+
fake.queueResponse("approval/decide", new Error("approval requirement changed"));
864988
const childApproval = { ...approval("child-run-1"), subagentOrigin: { subagentId: "a7" } };
865989
yield* fake.emit("approval/requested", childApproval);
866990
const asked = yield* harness.takeEvent("runtime_request.updated");
867991
assert.strictEqual(asked.runtimeRequest.status, "pending");
868-
assert.isNull(asked.runtimeRequest.providerTurnId);
869-
const decision = yield* fake.takeCall("approval/decide");
870-
assert.strictEqual(decision.params.choiceId, "once");
992+
yield* harness.takeEvent(
993+
"provider_session.updated",
994+
(event) => event.providerSession.status === "waiting",
995+
);
996+
yield* harness.runtime.respondToRuntimeRequest({
997+
requestId: asked.runtimeRequest.id,
998+
decision: "decline",
999+
});
1000+
assert.deepStrictEqual(
1001+
fake.calls
1002+
.filter((call) => call.method === "approval/decide")
1003+
.map((call) => call.params.choiceId),
1004+
["once", "deny"],
1005+
);
8711006
yield* fake.emit("approval/resolved", childApproval);
8721007
const resolved = yield* harness.takeEvent("runtime_request.updated");
8731008
assert.strictEqual(resolved.runtimeRequest.status, "resolved");
874-
assert.strictEqual(resolved.runtimeRequest.decision, "accept");
1009+
assert.strictEqual(resolved.runtimeRequest.decision, "decline");
8751010
}).pipe(Effect.scoped, Effect.provide(testLayer)),
8761011
);
8771012

1013+
it.effect(
1014+
"does not show a workflow child's approval that Muse settled before T3's answer failed",
1015+
() =>
1016+
Effect.gen(function* () {
1017+
const fake = yield* makeFakeMuse();
1018+
const harness = yield* makeHarness(fake);
1019+
yield* startConversation(harness, fake);
1020+
const acknowledgement = pendingPromise<Record<string, unknown>>();
1021+
fake.queueResponse("approval/decide", acknowledgement.promise);
1022+
const childApproval = { ...approval("child-run-1"), subagentOrigin: { subagentId: "a7" } };
1023+
yield* fake.emit("approval/requested", childApproval);
1024+
yield* fake.takeCall("approval/decide");
1025+
yield* fake.emit("approval/resolved", childApproval);
1026+
yield* harness.takeEvent(
1027+
"runtime_request.updated",
1028+
(event) => event.runtimeRequest.status === "resolved",
1029+
);
1030+
acknowledgement.reject(new Error("approval already resolved"));
1031+
// A later child request whose answer fails is shown, after the first failure was handled.
1032+
fake.queueResponse("approval/decide", new Error("approval requirement changed"));
1033+
yield* fake.emit("approval/requested", {
1034+
...childApproval,
1035+
approvalId: "approval-2",
1036+
currentRequirementId: { approvalId: "approval-2", sourceIndex: 0 },
1037+
});
1038+
yield* harness.takeEvent(
1039+
"runtime_request.updated",
1040+
(event) => event.runtimeRequest.status === "pending",
1041+
);
1042+
assert.deepStrictEqual(
1043+
harness.allEvents.flatMap((event) =>
1044+
event.type === "runtime_request.updated"
1045+
? [
1046+
`${event.runtimeRequest.nativeRequestRef?.nativeId} ${event.runtimeRequest.status}`,
1047+
]
1048+
: [],
1049+
),
1050+
["approval-1 resolved", "approval-2 pending"],
1051+
);
1052+
}).pipe(Effect.scoped, Effect.provide(testLayer)),
1053+
);
1054+
8781055
it.effect("keeps a workflow child's approval pending for the user past the turn's end", () =>
8791056
Effect.gen(function* () {
8801057
const fake = yield* makeFakeMuse();

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

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ interface PendingRequest {
227227
node: OrchestrationV2ExecutionNode;
228228
item: OrchestrationV2TurnItem;
229229
response?: Pick<OrchestrationV2RuntimeRequest, "decision" | "answers">;
230+
/** False while T3 approves a workflow child's request for the user. */
231+
shown: boolean;
230232
}
231233

232234
/**
@@ -1049,6 +1051,18 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
10491051
choiceId: choice.choiceId,
10501052
}).pipe(Effect.tapError(() => Effect.sync(() => delete entry.response)));
10511053
});
1054+
const showRequest = Effect.fnUntraced(function* (entry: PendingRequest) {
1055+
entry.shown = true;
1056+
yield* emit({
1057+
type: "runtime_request.updated",
1058+
driver: MUSE_PROVIDER,
1059+
threadId: entry.node.threadId,
1060+
runtimeRequest: entry.request,
1061+
});
1062+
yield* emit({ type: "node.updated", driver: MUSE_PROVIDER, node: entry.node });
1063+
yield* emit({ type: "turn_item.updated", driver: MUSE_PROVIDER, turnItem: entry.item });
1064+
yield* updateSession("waiting");
1065+
});
10521066
/**
10531067
* A workflow child's approval is runless: the turn that started the workflow
10541068
* may end long before the child asks, and T3 keeps a runless request on the
@@ -1179,21 +1193,33 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
11791193
required: true,
11801194
})),
11811195
};
1182-
const entry: PendingRequest = { native, request: runtimeRequest, node, item };
1183-
pending.set(requestId, entry);
1184-
yield* emit({
1185-
type: "runtime_request.updated",
1186-
driver: MUSE_PROVIDER,
1187-
threadId,
1188-
runtimeRequest,
1189-
});
1190-
yield* emit({ type: "node.updated", driver: MUSE_PROVIDER, node });
1191-
yield* emit({ type: "turn_item.updated", driver: MUSE_PROVIDER, turnItem: item });
1192-
yield* updateSession("waiting");
11931196
// Muse does not give workflow children the session's allowAll mode, so in full
1194-
// access T3 approves for them. If Muse rejects that, the request waits for the user.
1195-
if (child && input.runtimePolicy.runtimeMode === "full-access")
1196-
yield* decideApproval(entry, "accept").pipe(Effect.ignore, Effect.forkIn(scope));
1197+
// access T3 approves for them without showing the request. If Muse rejects that,
1198+
// the request is shown and waits for the user.
1199+
const autoApprove = child && input.runtimePolicy.runtimeMode === "full-access";
1200+
const entry: PendingRequest = {
1201+
native,
1202+
request: runtimeRequest,
1203+
node,
1204+
item,
1205+
// A request the user already sees stays visible when Muse updates it.
1206+
shown: !autoApprove || previous?.shown === true,
1207+
};
1208+
pending.set(requestId, entry);
1209+
if (entry.shown) yield* showRequest(entry);
1210+
if (autoApprove)
1211+
yield* decideApproval(entry, "accept").pipe(
1212+
Effect.catch(() =>
1213+
Effect.gen(function* () {
1214+
// Muse may have settled or replaced the request while T3 waited, or the
1215+
// session may be closing. Nothing is left to show then.
1216+
if (!closed && !broken && pending.get(requestId) === entry)
1217+
yield* showRequest(entry);
1218+
}).pipe(eventPermit.withPermits(1)),
1219+
),
1220+
Effect.ignore,
1221+
Effect.forkIn(scope),
1222+
);
11971223
});
11981224
const handleRequestEvent = Effect.fnUntraced(function* (
11991225
method: string,
@@ -1239,7 +1265,10 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
12391265
if (!entry.response && native) entry.response = { decision: native };
12401266
yield* resolvePending(entry, "resolved");
12411267
}
1242-
if (!pending.size) yield* updateSession(active ? "running" : "ready");
1268+
// A request T3 is still approving for the user is hidden and does not keep
1269+
// the session waiting.
1270+
if (![...pending.values()].some((candidate) => candidate.shown))
1271+
yield* updateSession(active ? "running" : "ready");
12431272
}
12441273
}
12451274
});
@@ -2046,7 +2075,9 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
20462075
]
20472076
: [],
20482077
),
2049-
runtimeRequests: [...pending.values()].map((entry) => entry.request),
2078+
runtimeRequests: [...pending.values()]
2079+
.filter((entry) => entry.shown)
2080+
.map((entry) => entry.request),
20502081
providerPayload: items,
20512082
};
20522083
});

0 commit comments

Comments
 (0)