Skip to content

Commit 901ab2d

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 3bbfdc7 commit 901ab2d

2 files changed

Lines changed: 235 additions & 26 deletions

File tree

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

Lines changed: 186 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -834,28 +834,205 @@ describe("MuseAdapterV2", () => {
834834
}).pipe(Effect.scoped, Effect.provide(testLayer)),
835835
);
836836

837-
it.effect("approves a workflow child's approval in full access after its turn ended", () =>
837+
it.effect.each([
838+
{ when: "after its turn ended", turnEnded: true },
839+
{ when: "while its turn runs", turnEnded: false },
840+
] as const)(
841+
"approves a workflow child's approval in full access $when without showing it",
842+
({ turnEnded }) =>
843+
Effect.gen(function* () {
844+
const fake = yield* makeFakeMuse();
845+
const harness = yield* makeHarness(fake);
846+
const { nativeId } = yield* startConversation(harness, fake);
847+
if (turnEnded) {
848+
yield* fake.emit("turn/completed", { turnId: nativeId, terminal: "completed" });
849+
yield* harness.takeEvent("turn.terminal");
850+
}
851+
const acknowledgement = pendingPromise<Record<string, unknown>>();
852+
fake.queueResponse("approval/decide", acknowledgement.promise);
853+
// Muse runs workflow children without the session's allowAll mode.
854+
const childApproval = { ...approval("child-run-1"), subagentOrigin: { subagentId: "a7" } };
855+
yield* fake.emit("approval/requested", childApproval);
856+
const decision = yield* fake.takeCall("approval/decide");
857+
assert.strictEqual(decision.params.choiceId, "once");
858+
const snapshot = yield* harness.runtime.readThreadSnapshot!({
859+
providerThread: harness.providerThread,
860+
});
861+
assert.deepStrictEqual(snapshot.runtimeRequests, []);
862+
yield* fake.emit("approval/resolved", childApproval);
863+
const resolved = yield* harness.takeEvent("runtime_request.updated");
864+
assert.strictEqual(resolved.runtimeRequest.status, "resolved");
865+
assert.strictEqual(resolved.runtimeRequest.decision, "accept");
866+
assert.isNull(resolved.runtimeRequest.providerTurnId);
867+
acknowledgement.resolve({});
868+
assert.isFalse(
869+
harness.allEvents.some(
870+
(event) =>
871+
(event.type === "runtime_request.updated" &&
872+
event.runtimeRequest.status === "pending") ||
873+
(event.type === "provider_session.updated" &&
874+
event.providerSession.status === "waiting"),
875+
),
876+
);
877+
}).pipe(Effect.scoped, Effect.provide(testLayer)),
878+
);
879+
880+
it.effect("answers a workflow child's updated approval in full access without showing it", () =>
838881
Effect.gen(function* () {
839882
const fake = yield* makeFakeMuse();
840883
const harness = yield* makeHarness(fake);
841-
const { nativeId } = yield* startConversation(harness, fake);
842-
yield* fake.emit("turn/completed", { turnId: nativeId, terminal: "completed" });
843-
yield* harness.takeEvent("turn.terminal");
844-
// Muse runs workflow children without the session's allowAll mode.
884+
yield* startConversation(harness, fake);
885+
const acknowledgement = pendingPromise<Record<string, unknown>>();
886+
fake.queueResponse("approval/decide", acknowledgement.promise);
887+
const childApproval = { ...approval("child-run-1"), subagentOrigin: { subagentId: "a7" } };
888+
yield* fake.emit("approval/requested", childApproval);
889+
yield* fake.takeCall("approval/decide");
890+
// Muse moves to the next requirement while T3's first answer is in flight.
891+
yield* fake.emit("approval/updated", {
892+
...childApproval,
893+
currentRequirementId: { approvalId: "approval-1", sourceIndex: 1 },
894+
});
895+
const second = yield* fake.takeCall("approval/decide");
896+
assert.deepStrictEqual(second.params.requirementId, {
897+
approvalId: "approval-1",
898+
sourceIndex: 1,
899+
});
900+
acknowledgement.reject(new Error("approval requirement changed"));
901+
yield* fake.emit("approval/resolved", childApproval);
902+
const resolved = yield* harness.takeEvent("runtime_request.updated");
903+
assert.strictEqual(resolved.runtimeRequest.status, "resolved");
904+
assert.strictEqual(resolved.runtimeRequest.decision, "accept");
905+
assert.isFalse(
906+
harness.allEvents.some(
907+
(event) =>
908+
event.type === "runtime_request.updated" && event.runtimeRequest.status === "pending",
909+
),
910+
);
911+
}).pipe(Effect.scoped, Effect.provide(testLayer)),
912+
);
913+
914+
it.effect(
915+
"stops waiting when a shown approval settles before a workflow child's hidden one",
916+
() =>
917+
Effect.gen(function* () {
918+
const fake = yield* makeFakeMuse();
919+
const harness = yield* makeHarness(fake);
920+
const { nativeId } = yield* startConversation(harness, fake);
921+
const acknowledgement = pendingPromise<Record<string, unknown>>();
922+
fake.queueResponse("approval/decide", acknowledgement.promise);
923+
const childApproval = { ...approval("child-run-1"), subagentOrigin: { subagentId: "a7" } };
924+
yield* fake.emit("approval/requested", childApproval);
925+
yield* fake.takeCall("approval/decide");
926+
const turnApproval = {
927+
...approval(nativeId),
928+
approvalId: "approval-2",
929+
currentRequirementId: { approvalId: "approval-2", sourceIndex: 0 },
930+
};
931+
yield* fake.emit("approval/requested", turnApproval);
932+
yield* harness.takeEvent(
933+
"provider_session.updated",
934+
(event) => event.providerSession.status === "waiting",
935+
);
936+
// The shown approval settles while T3's answer to the child is still in flight.
937+
yield* fake.emit("approval/resolved", turnApproval);
938+
acknowledgement.resolve({});
939+
yield* fake.emit("approval/resolved", childApproval);
940+
yield* harness.takeEvent(
941+
"runtime_request.updated",
942+
(event) => event.runtimeRequest.nativeRequestRef?.nativeId === "approval-1",
943+
);
944+
yield* harness.takeEvent("provider_session.updated");
945+
const sequence = harness.allEvents.flatMap((event) =>
946+
event.type === "runtime_request.updated"
947+
? [`${event.runtimeRequest.nativeRequestRef?.nativeId} ${event.runtimeRequest.status}`]
948+
: event.type === "provider_session.updated"
949+
? [`session ${event.providerSession.status}`]
950+
: [],
951+
);
952+
assert.deepStrictEqual(sequence.slice(sequence.indexOf("approval-2 pending")), [
953+
"approval-2 pending",
954+
"session waiting",
955+
"approval-2 resolved",
956+
"session running",
957+
"approval-1 resolved",
958+
"session running",
959+
]);
960+
}).pipe(Effect.scoped, Effect.provide(testLayer)),
961+
);
962+
963+
it.effect("shows a workflow child's approval when Muse rejects T3's automatic answer", () =>
964+
Effect.gen(function* () {
965+
const fake = yield* makeFakeMuse();
966+
const harness = yield* makeHarness(fake);
967+
yield* startConversation(harness, fake);
968+
fake.queueResponse("approval/decide", new Error("approval requirement changed"));
845969
const childApproval = { ...approval("child-run-1"), subagentOrigin: { subagentId: "a7" } };
846970
yield* fake.emit("approval/requested", childApproval);
847971
const asked = yield* harness.takeEvent("runtime_request.updated");
848972
assert.strictEqual(asked.runtimeRequest.status, "pending");
849-
assert.isNull(asked.runtimeRequest.providerTurnId);
850-
const decision = yield* fake.takeCall("approval/decide");
851-
assert.strictEqual(decision.params.choiceId, "once");
973+
yield* harness.takeEvent(
974+
"provider_session.updated",
975+
(event) => event.providerSession.status === "waiting",
976+
);
977+
yield* harness.runtime.respondToRuntimeRequest({
978+
requestId: asked.runtimeRequest.id,
979+
decision: "decline",
980+
});
981+
assert.deepStrictEqual(
982+
fake.calls
983+
.filter((call) => call.method === "approval/decide")
984+
.map((call) => call.params.choiceId),
985+
["once", "deny"],
986+
);
852987
yield* fake.emit("approval/resolved", childApproval);
853988
const resolved = yield* harness.takeEvent("runtime_request.updated");
854989
assert.strictEqual(resolved.runtimeRequest.status, "resolved");
855-
assert.strictEqual(resolved.runtimeRequest.decision, "accept");
990+
assert.strictEqual(resolved.runtimeRequest.decision, "decline");
856991
}).pipe(Effect.scoped, Effect.provide(testLayer)),
857992
);
858993

994+
it.effect(
995+
"does not show a workflow child's approval that Muse settled before T3's answer failed",
996+
() =>
997+
Effect.gen(function* () {
998+
const fake = yield* makeFakeMuse();
999+
const harness = yield* makeHarness(fake);
1000+
yield* startConversation(harness, fake);
1001+
const acknowledgement = pendingPromise<Record<string, unknown>>();
1002+
fake.queueResponse("approval/decide", acknowledgement.promise);
1003+
const childApproval = { ...approval("child-run-1"), subagentOrigin: { subagentId: "a7" } };
1004+
yield* fake.emit("approval/requested", childApproval);
1005+
yield* fake.takeCall("approval/decide");
1006+
yield* fake.emit("approval/resolved", childApproval);
1007+
yield* harness.takeEvent(
1008+
"runtime_request.updated",
1009+
(event) => event.runtimeRequest.status === "resolved",
1010+
);
1011+
acknowledgement.reject(new Error("approval already resolved"));
1012+
// A later child request whose answer fails is shown, after the first failure was handled.
1013+
fake.queueResponse("approval/decide", new Error("approval requirement changed"));
1014+
yield* fake.emit("approval/requested", {
1015+
...childApproval,
1016+
approvalId: "approval-2",
1017+
currentRequirementId: { approvalId: "approval-2", sourceIndex: 0 },
1018+
});
1019+
yield* harness.takeEvent(
1020+
"runtime_request.updated",
1021+
(event) => event.runtimeRequest.status === "pending",
1022+
);
1023+
assert.deepStrictEqual(
1024+
harness.allEvents.flatMap((event) =>
1025+
event.type === "runtime_request.updated"
1026+
? [
1027+
`${event.runtimeRequest.nativeRequestRef?.nativeId} ${event.runtimeRequest.status}`,
1028+
]
1029+
: [],
1030+
),
1031+
["approval-1 resolved", "approval-2 pending"],
1032+
);
1033+
}).pipe(Effect.scoped, Effect.provide(testLayer)),
1034+
);
1035+
8591036
it.effect("keeps a workflow child's approval pending for the user past the turn's end", () =>
8601037
Effect.gen(function* () {
8611038
const fake = yield* makeFakeMuse();

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

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ interface PendingRequest {
231231
item: OrchestrationV2TurnItem;
232232
/** What T3 answered, recorded on the request once Muse confirms it. */
233233
response?: Pick<OrchestrationV2RuntimeRequest, "decision" | "answers">;
234+
/** False while T3 approves a workflow child's request for the user. */
235+
shown: boolean;
234236
}
235237

236238
/**
@@ -1086,8 +1088,21 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
10861088
choiceId: choice.choiceId,
10871089
}).pipe(Effect.tapError(() => Effect.sync(() => delete entry.response)));
10881090
});
1091+
/** Emits a pending request for the user to answer. */
1092+
const showRequest = Effect.fnUntraced(function* (entry: PendingRequest) {
1093+
entry.shown = true;
1094+
yield* emit({
1095+
type: "runtime_request.updated",
1096+
driver: MUSE_PROVIDER,
1097+
threadId: entry.node.threadId,
1098+
runtimeRequest: entry.request,
1099+
});
1100+
yield* emit({ type: "node.updated", driver: MUSE_PROVIDER, node: entry.node });
1101+
yield* emit({ type: "turn_item.updated", driver: MUSE_PROVIDER, turnItem: entry.item });
1102+
yield* updateSession("waiting");
1103+
});
10891104
/**
1090-
* Shows a Muse approval or question as a T3 request. A workflow child's approval
1105+
* Tracks a Muse approval or question as a T3 request. A workflow child's approval
10911106
* is runless: the turn that started the workflow may end long before the child
10921107
* asks, and T3 keeps a runless request on the thread until Muse settles it.
10931108
*/
@@ -1217,21 +1232,33 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
12171232
required: true,
12181233
})),
12191234
};
1220-
const entry: PendingRequest = { native, request: runtimeRequest, node, item };
1221-
pending.set(requestId, entry);
1222-
yield* emit({
1223-
type: "runtime_request.updated",
1224-
driver: MUSE_PROVIDER,
1225-
threadId,
1226-
runtimeRequest,
1227-
});
1228-
yield* emit({ type: "node.updated", driver: MUSE_PROVIDER, node });
1229-
yield* emit({ type: "turn_item.updated", driver: MUSE_PROVIDER, turnItem: item });
1230-
yield* updateSession("waiting");
12311235
// Muse does not give workflow children the session's allowAll mode, so in full
1232-
// access T3 approves for them. If Muse rejects that, the request waits for the user.
1233-
if (child && input.runtimePolicy.runtimeMode === "full-access")
1234-
yield* decideApproval(entry, "accept").pipe(Effect.ignore, Effect.forkIn(scope));
1236+
// access T3 approves for them without showing the request. If Muse rejects that,
1237+
// the request is shown and waits for the user.
1238+
const autoApprove = child && input.runtimePolicy.runtimeMode === "full-access";
1239+
const entry: PendingRequest = {
1240+
native,
1241+
request: runtimeRequest,
1242+
node,
1243+
item,
1244+
// A request the user already sees stays visible when Muse updates it.
1245+
shown: !autoApprove || previous?.shown === true,
1246+
};
1247+
pending.set(requestId, entry);
1248+
if (entry.shown) yield* showRequest(entry);
1249+
if (autoApprove)
1250+
yield* decideApproval(entry, "accept").pipe(
1251+
Effect.catch(() =>
1252+
Effect.gen(function* () {
1253+
// Muse may have settled or replaced the request while T3 waited, or the
1254+
// session may be closing. Nothing is left to show then.
1255+
if (!closed && !broken && pending.get(requestId) === entry)
1256+
yield* showRequest(entry);
1257+
}).pipe(eventPermit.withPermits(1)),
1258+
),
1259+
Effect.ignore,
1260+
Effect.forkIn(scope),
1261+
);
12351262
});
12361263
/** Approval and question events; `turn` is undefined for a workflow child's approval. */
12371264
const handleRequestEvent = Effect.fnUntraced(function* (
@@ -1279,7 +1306,10 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
12791306
if (!entry.response && native) entry.response = { decision: native };
12801307
yield* resolvePending(entry, "resolved");
12811308
}
1282-
if (!pending.size) yield* updateSession(active ? "running" : "ready");
1309+
// A request T3 is still approving for the user is hidden and does not keep
1310+
// the session waiting.
1311+
if (![...pending.values()].some((candidate) => candidate.shown))
1312+
yield* updateSession(active ? "running" : "ready");
12831313
}
12841314
}
12851315
});
@@ -2097,7 +2127,9 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
20972127
]
20982128
: [],
20992129
),
2100-
runtimeRequests: [...pending.values()].map((entry) => entry.request),
2130+
runtimeRequests: [...pending.values()]
2131+
.filter((entry) => entry.shown)
2132+
.map((entry) => entry.request),
21012133
providerPayload: items,
21022134
};
21032135
});

0 commit comments

Comments
 (0)