Skip to content
TypeScript

fix(provider-muse): workflow child approvals no longer flash in full access (#19306)

Scott Nortemana752363b18db
diff --git a/packages/provider-muse/src/server/adapter.test.ts b/packages/provider-muse/src/server/adapter.test.ts
index 8ebf635c18c..48ec539c94c 100644
--- a/packages/provider-muse/src/server/adapter.test.ts
+++ b/packages/provider-muse/src/server/adapter.test.ts
@@ -1035,24 +1035,190 @@ describe("MuseAdapterV2", () => {
}).pipe(Effect.scoped, Effect.provide(testLayer)),
);
- it.effect("approves a workflow child's approval in full access after its turn ended", () =>
+ it.effect.each([
+ { when: "after its turn ended", turnEnded: true },
+ { when: "while its turn runs", turnEnded: false },
+ ] as const)(
+ "hides a workflow child's automatic approval $when and retains completed history",
+ ({ turnEnded }) =>
+ Effect.gen(function* () {
+ const fake = yield* makeFakeMuse();
+ const harness = yield* makeHarness(fake);
+ const { nativeId } = yield* startConversation(harness, fake);
+ if (turnEnded) {
+ yield* fake.emit("turn/completed", { turnId: nativeId, terminal: "completed" });
+ yield* harness.takeEvent("turn.terminal");
+ }
+ const acknowledgement = pendingPromise<Record<string, unknown>>();
+ fake.queueResponse("approval/decide", acknowledgement.promise);
+ const child = { ...approval("child-run-1"), subagentOrigin: { subagentId: "a7" } };
+ const before = harness.allEvents.length;
+ yield* fake.emit("approval/requested", child);
+ const decision = yield* fake.takeCall("approval/decide");
+ assert.strictEqual(decision.params.choiceId, "once");
+ const snapshot = yield* harness.runtime.readThreadSnapshot!({
+ providerThread: harness.providerThread,
+ });
+ assert.deepStrictEqual(snapshot.runtimeRequests, []);
+ assert.deepStrictEqual(harness.allEvents.slice(before), []);
+ if (!turnEnded) {
+ const parent = {
+ ...approval(nativeId),
+ approvalId: "parent-approval",
+ currentRequirementId: { approvalId: "parent-approval", sourceIndex: 0 },
+ };
+ yield* fake.emit("approval/requested", parent);
+ const shown = yield* harness.takeEvent("runtime_request.updated");
+ assert.strictEqual(shown.runtimeRequest.status, "pending");
+ yield* harness.takeEvent("provider_session.updated");
+ yield* fake.emit("approval/resolved", parent);
+ // The child remains unacknowledged when the visible request stops blocking the turn.
+ const session = yield* harness.takeEvent("provider_session.updated");
+ assert.strictEqual(session.providerSession.status, "running");
+ }
+ yield* fake.emit("approval/resolved", child);
+ const resolved = yield* harness.takeEvent("runtime_request.updated");
+ assert.strictEqual(resolved.runtimeRequest.status, "resolved");
+ assert.strictEqual(resolved.runtimeRequest.decision, "accept");
+ assert.isNull(resolved.runtimeRequest.providerTurnId);
+ const node = yield* harness.takeEvent("node.updated");
+ const item = yield* harness.takeEvent("turn_item.updated");
+ assert.strictEqual(node.node.status, "completed");
+ assert.strictEqual(item.turnItem.status, "completed");
+ acknowledgement.resolve({});
+ }).pipe(Effect.scoped, Effect.provide(testLayer)),
+ );
+
+ it.effect("reveals only the current failed child approval and sends the user's decline", () =>
Effect.gen(function* () {
const fake = yield* makeFakeMuse();
const harness = yield* makeHarness(fake);
- const { nativeId } = yield* startConversation(harness, fake);
- yield* fake.emit("turn/completed", { turnId: nativeId, terminal: "completed" });
- yield* harness.takeEvent("turn.terminal");
- const childApproval = { ...approval("child-run-1"), subagentOrigin: { subagentId: "a7" } };
- yield* fake.emit("approval/requested", childApproval);
+ yield* startConversation(harness, fake);
+ const outdated = pendingPromise<Record<string, unknown>>();
+ const current = pendingPromise<Record<string, unknown>>();
+ fake.queueResponse("approval/decide", outdated.promise);
+ fake.queueResponse("approval/decide", current.promise);
+ const child = {
+ ...approval("child-run-1"),
+ subagentOrigin: {
+ subagentId: "a7",
+ childRunId: "child-run-1",
+ childSessionId: "child-session",
+ },
+ };
+ yield* fake.emit("approval/requested", child);
+ yield* fake.takeCall("approval/decide");
+ yield* fake.emit("approval/updated", {
+ approvalId: child.approvalId,
+ availableChoices: child.availableChoices,
+ subject: child.subject,
+ subagentOrigin: child.subagentOrigin,
+ currentRequirementId: { approvalId: "approval-1", sourceIndex: 1 },
+ });
+ const second = yield* fake.takeCall("approval/decide");
+ assert.deepStrictEqual(second.params.requirementId, {
+ approvalId: "approval-1",
+ sourceIndex: 1,
+ });
+ outdated.reject(
+ new MspError({
+ code: -32053,
+ message: "Stale requirement",
+ data: { kind: "approvalRequirementStale" },
+ }),
+ );
+ current.reject(
+ new MspError({
+ code: -32052,
+ message: "Choice rejected",
+ data: { kind: "approvalChoiceInvalid" },
+ }),
+ );
const asked = yield* harness.takeEvent("runtime_request.updated");
assert.strictEqual(asked.runtimeRequest.status, "pending");
- assert.isNull(asked.runtimeRequest.providerTurnId);
- const decision = yield* fake.takeCall("approval/decide");
- assert.strictEqual(decision.params.choiceId, "once");
- yield* fake.emit("approval/resolved", childApproval);
+ const waiting = yield* harness.takeEvent("provider_session.updated");
+ assert.strictEqual(waiting.providerSession.status, "waiting");
+ const snapshot = yield* harness.runtime.readThreadSnapshot!({
+ providerThread: harness.providerThread,
+ });
+ assert.deepStrictEqual(snapshot.runtimeRequests, [asked.runtimeRequest]);
+ assert.lengthOf(
+ harness.allEvents.filter((event) => event.type === "runtime_request.updated"),
+ 1,
+ );
+ yield* harness.runtime.respondToRuntimeRequest({
+ requestId: asked.runtimeRequest.id,
+ decision: "decline",
+ });
+ const decline = yield* fake.takeCall("approval/decide");
+ assert.strictEqual(decline.params.choiceId, "deny");
+ assert.deepStrictEqual(decline.params.requirementId, second.params.requirementId);
+ yield* fake.emit("approval/resolved", child);
+ const resolved = yield* harness.takeEvent("runtime_request.updated");
+ assert.strictEqual(resolved.runtimeRequest.decision, "decline");
+ }).pipe(Effect.scoped, Effect.provide(testLayer)),
+ );
+
+ it.effect("does not resurrect a settled child approval when its automatic answer fails", () =>
+ Effect.gen(function* () {
+ const fake = yield* makeFakeMuse();
+ const harness = yield* makeHarness(fake);
+ yield* startConversation(harness, fake);
+ const acknowledgement = pendingPromise<Record<string, unknown>>();
+ fake.queueResponse("approval/decide", acknowledgement.promise);
+ const child = { ...approval("child-run-1"), subagentOrigin: { subagentId: "a7" } };
+ yield* fake.emit("approval/requested", child);
+ yield* fake.takeCall("approval/decide");
+ yield* fake.emit("approval/resolved", child);
const resolved = yield* harness.takeEvent("runtime_request.updated");
assert.strictEqual(resolved.runtimeRequest.status, "resolved");
- assert.strictEqual(resolved.runtimeRequest.decision, "accept");
+ acknowledgement.reject(
+ new MspError({
+ code: -32051,
+ message: "Already resolved",
+ data: { kind: "approvalAlreadyResolved" },
+ }),
+ );
+ // A later rejected answer provides an observable milestone after the stale rejection.
+ fake.queueResponse("approval/decide", new Error("Choice rejected"));
+ yield* fake.emit("approval/requested", {
+ ...child,
+ approvalId: "approval-2",
+ currentRequirementId: { approvalId: "approval-2", sourceIndex: 0 },
+ });
+ const asked = yield* harness.takeEvent("runtime_request.updated");
+ assert.strictEqual(asked.runtimeRequest.nativeRequestRef?.nativeId, "approval-2");
+ assert.deepStrictEqual(
+ harness.allEvents.flatMap((event) =>
+ event.type === "runtime_request.updated" ? [event.runtimeRequest.status] : [],
+ ),
+ ["resolved", "pending"],
+ );
+ }).pipe(Effect.scoped, Effect.provide(testLayer)),
+ );
+
+ it.effect("does not reveal an automatic approval after the Muse connection closes", () =>
+ Effect.gen(function* () {
+ const fake = yield* makeFakeMuse();
+ const harness = yield* makeHarness(fake);
+ yield* startConversation(harness, fake);
+ const acknowledgement = pendingPromise<Record<string, unknown>>();
+ fake.queueResponse("approval/decide", acknowledgement.promise);
+ yield* fake.emit("approval/requested", {
+ ...approval("child-run-1"),
+ subagentOrigin: { subagentId: "a7" },
+ });
+ yield* fake.takeCall("approval/decide");
+ yield* fake.disconnect();
+ yield* Deferred.await(harness.eventsEnded);
+ acknowledgement.reject(new Error("Connection closed"));
+ yield* Effect.promise(() => fake.host.exited);
+ assert.deepStrictEqual(
+ harness.allEvents.flatMap((event) =>
+ event.type === "runtime_request.updated" ? [event.runtimeRequest.status] : [],
+ ),
+ ["cancelled"],
+ );
}).pipe(Effect.scoped, Effect.provide(testLayer)),
);
diff --git a/packages/provider-muse/src/server/adapter.ts b/packages/provider-muse/src/server/adapter.ts
index 39f81166dc6..c098a0aa7ea 100644
--- a/packages/provider-muse/src/server/adapter.ts
+++ b/packages/provider-muse/src/server/adapter.ts
@@ -229,6 +229,7 @@ interface PendingRequest {
node: OrchestrationV2ExecutionNode;
item: OrchestrationV2TurnItem;
response?: Pick<OrchestrationV2RuntimeRequest, "decision" | "answers">;
+ shown: boolean;
}
/**
@@ -1078,6 +1079,18 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
choiceId: choice.choiceId,
}).pipe(Effect.tapError(() => Effect.sync(() => delete entry.response)));
});
+ const showRequest = Effect.fnUntraced(function* (entry: PendingRequest) {
+ entry.shown = true;
+ yield* emit({
+ type: "runtime_request.updated",
+ driver: MUSE_PROVIDER,
+ threadId: entry.node.threadId,
+ runtimeRequest: entry.request,
+ });
+ yield* emit({ type: "node.updated", driver: MUSE_PROVIDER, node: entry.node });
+ yield* emit({ type: "turn_item.updated", driver: MUSE_PROVIDER, turnItem: entry.item });
+ yield* updateSession("waiting");
+ });
/**
* A workflow child's approval is runless: the turn that started the workflow
* may end long before the child asks, and T3 keeps a runless request on the
@@ -1208,21 +1221,28 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
required: true,
})),
};
- const entry: PendingRequest = { native, request: runtimeRequest, node, item };
+ // Muse does not propagate allowAll to workflow children; T3 answers for them.
+ const autoApprove = child && input.runtimePolicy.runtimeMode === "full-access";
+ const entry: PendingRequest = {
+ native,
+ request: runtimeRequest,
+ node,
+ item,
+ shown: !autoApprove || previous?.shown === true,
+ };
pending.set(requestId, entry);
- yield* emit({
- type: "runtime_request.updated",
- driver: MUSE_PROVIDER,
- threadId,
- runtimeRequest,
- });
- yield* emit({ type: "node.updated", driver: MUSE_PROVIDER, node });
- yield* emit({ type: "turn_item.updated", driver: MUSE_PROVIDER, turnItem: item });
- yield* updateSession("waiting");
- // Muse does not give workflow children the session's allowAll mode, so in full
- // access T3 approves for them. If Muse rejects that, the request waits for the user.
- if (child && input.runtimePolicy.runtimeMode === "full-access")
- yield* decideApproval(entry, "accept").pipe(Effect.ignore, Effect.forkIn(scope));
+ if (entry.shown) yield* showRequest(entry);
+ if (autoApprove)
+ yield* decideApproval(entry, "accept").pipe(
+ Effect.catch(() =>
+ Effect.gen(function* () {
+ // Native updates may settle or replace this entry while the RPC is in flight.
+ if (!closed && !broken && pending.get(requestId) === entry)
+ yield* showRequest(entry);
+ }).pipe(eventPermit.withPermits(1)),
+ ),
+ Effect.forkIn(scope),
+ );
});
const handleRequestEvent = Effect.fnUntraced(function* (
method: string,
@@ -1268,7 +1288,8 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
if (!entry.response && native) entry.response = { decision: native };
yield* resolvePending(entry, "resolved");
}
- if (!pending.size) yield* updateSession(active ? "running" : "ready");
+ if (![...pending.values()].some((candidate) => candidate.shown))
+ yield* updateSession(active ? "running" : "ready");
}
}
});
@@ -2097,7 +2118,9 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
]
: [],
),
- runtimeRequests: [...pending.values()].map((entry) => entry.request),
+ runtimeRequests: [...pending.values()]
+ .filter((entry) => entry.shown)
+ .map((entry) => entry.request),
providerPayload: items,
};
});