Skip to content

Commit a752363

Browse files
ScottN-PVclaudejuliusmarminge
authored
fix(provider-muse): workflow child approvals no longer flash in full access (pingdotgg#19306)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent cfa6944 commit a752363

2 files changed

Lines changed: 216 additions & 27 deletions

File tree

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

Lines changed: 177 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,24 +1035,190 @@ describe("MuseAdapterV2", () => {
10351035
}).pipe(Effect.scoped, Effect.provide(testLayer)),
10361036
);
10371037

1038-
it.effect("approves a workflow child's approval in full access after its turn ended", () =>
1038+
it.effect.each([
1039+
{ when: "after its turn ended", turnEnded: true },
1040+
{ when: "while its turn runs", turnEnded: false },
1041+
] as const)(
1042+
"hides a workflow child's automatic approval $when and retains completed history",
1043+
({ turnEnded }) =>
1044+
Effect.gen(function* () {
1045+
const fake = yield* makeFakeMuse();
1046+
const harness = yield* makeHarness(fake);
1047+
const { nativeId } = yield* startConversation(harness, fake);
1048+
if (turnEnded) {
1049+
yield* fake.emit("turn/completed", { turnId: nativeId, terminal: "completed" });
1050+
yield* harness.takeEvent("turn.terminal");
1051+
}
1052+
const acknowledgement = pendingPromise<Record<string, unknown>>();
1053+
fake.queueResponse("approval/decide", acknowledgement.promise);
1054+
const child = { ...approval("child-run-1"), subagentOrigin: { subagentId: "a7" } };
1055+
const before = harness.allEvents.length;
1056+
yield* fake.emit("approval/requested", child);
1057+
const decision = yield* fake.takeCall("approval/decide");
1058+
assert.strictEqual(decision.params.choiceId, "once");
1059+
const snapshot = yield* harness.runtime.readThreadSnapshot!({
1060+
providerThread: harness.providerThread,
1061+
});
1062+
assert.deepStrictEqual(snapshot.runtimeRequests, []);
1063+
assert.deepStrictEqual(harness.allEvents.slice(before), []);
1064+
if (!turnEnded) {
1065+
const parent = {
1066+
...approval(nativeId),
1067+
approvalId: "parent-approval",
1068+
currentRequirementId: { approvalId: "parent-approval", sourceIndex: 0 },
1069+
};
1070+
yield* fake.emit("approval/requested", parent);
1071+
const shown = yield* harness.takeEvent("runtime_request.updated");
1072+
assert.strictEqual(shown.runtimeRequest.status, "pending");
1073+
yield* harness.takeEvent("provider_session.updated");
1074+
yield* fake.emit("approval/resolved", parent);
1075+
// The child remains unacknowledged when the visible request stops blocking the turn.
1076+
const session = yield* harness.takeEvent("provider_session.updated");
1077+
assert.strictEqual(session.providerSession.status, "running");
1078+
}
1079+
yield* fake.emit("approval/resolved", child);
1080+
const resolved = yield* harness.takeEvent("runtime_request.updated");
1081+
assert.strictEqual(resolved.runtimeRequest.status, "resolved");
1082+
assert.strictEqual(resolved.runtimeRequest.decision, "accept");
1083+
assert.isNull(resolved.runtimeRequest.providerTurnId);
1084+
const node = yield* harness.takeEvent("node.updated");
1085+
const item = yield* harness.takeEvent("turn_item.updated");
1086+
assert.strictEqual(node.node.status, "completed");
1087+
assert.strictEqual(item.turnItem.status, "completed");
1088+
acknowledgement.resolve({});
1089+
}).pipe(Effect.scoped, Effect.provide(testLayer)),
1090+
);
1091+
1092+
it.effect("reveals only the current failed child approval and sends the user's decline", () =>
10391093
Effect.gen(function* () {
10401094
const fake = yield* makeFakeMuse();
10411095
const harness = yield* makeHarness(fake);
1042-
const { nativeId } = yield* startConversation(harness, fake);
1043-
yield* fake.emit("turn/completed", { turnId: nativeId, terminal: "completed" });
1044-
yield* harness.takeEvent("turn.terminal");
1045-
const childApproval = { ...approval("child-run-1"), subagentOrigin: { subagentId: "a7" } };
1046-
yield* fake.emit("approval/requested", childApproval);
1096+
yield* startConversation(harness, fake);
1097+
const outdated = pendingPromise<Record<string, unknown>>();
1098+
const current = pendingPromise<Record<string, unknown>>();
1099+
fake.queueResponse("approval/decide", outdated.promise);
1100+
fake.queueResponse("approval/decide", current.promise);
1101+
const child = {
1102+
...approval("child-run-1"),
1103+
subagentOrigin: {
1104+
subagentId: "a7",
1105+
childRunId: "child-run-1",
1106+
childSessionId: "child-session",
1107+
},
1108+
};
1109+
yield* fake.emit("approval/requested", child);
1110+
yield* fake.takeCall("approval/decide");
1111+
yield* fake.emit("approval/updated", {
1112+
approvalId: child.approvalId,
1113+
availableChoices: child.availableChoices,
1114+
subject: child.subject,
1115+
subagentOrigin: child.subagentOrigin,
1116+
currentRequirementId: { approvalId: "approval-1", sourceIndex: 1 },
1117+
});
1118+
const second = yield* fake.takeCall("approval/decide");
1119+
assert.deepStrictEqual(second.params.requirementId, {
1120+
approvalId: "approval-1",
1121+
sourceIndex: 1,
1122+
});
1123+
outdated.reject(
1124+
new MspError({
1125+
code: -32053,
1126+
message: "Stale requirement",
1127+
data: { kind: "approvalRequirementStale" },
1128+
}),
1129+
);
1130+
current.reject(
1131+
new MspError({
1132+
code: -32052,
1133+
message: "Choice rejected",
1134+
data: { kind: "approvalChoiceInvalid" },
1135+
}),
1136+
);
10471137
const asked = yield* harness.takeEvent("runtime_request.updated");
10481138
assert.strictEqual(asked.runtimeRequest.status, "pending");
1049-
assert.isNull(asked.runtimeRequest.providerTurnId);
1050-
const decision = yield* fake.takeCall("approval/decide");
1051-
assert.strictEqual(decision.params.choiceId, "once");
1052-
yield* fake.emit("approval/resolved", childApproval);
1139+
const waiting = yield* harness.takeEvent("provider_session.updated");
1140+
assert.strictEqual(waiting.providerSession.status, "waiting");
1141+
const snapshot = yield* harness.runtime.readThreadSnapshot!({
1142+
providerThread: harness.providerThread,
1143+
});
1144+
assert.deepStrictEqual(snapshot.runtimeRequests, [asked.runtimeRequest]);
1145+
assert.lengthOf(
1146+
harness.allEvents.filter((event) => event.type === "runtime_request.updated"),
1147+
1,
1148+
);
1149+
yield* harness.runtime.respondToRuntimeRequest({
1150+
requestId: asked.runtimeRequest.id,
1151+
decision: "decline",
1152+
});
1153+
const decline = yield* fake.takeCall("approval/decide");
1154+
assert.strictEqual(decline.params.choiceId, "deny");
1155+
assert.deepStrictEqual(decline.params.requirementId, second.params.requirementId);
1156+
yield* fake.emit("approval/resolved", child);
1157+
const resolved = yield* harness.takeEvent("runtime_request.updated");
1158+
assert.strictEqual(resolved.runtimeRequest.decision, "decline");
1159+
}).pipe(Effect.scoped, Effect.provide(testLayer)),
1160+
);
1161+
1162+
it.effect("does not resurrect a settled child approval when its automatic answer fails", () =>
1163+
Effect.gen(function* () {
1164+
const fake = yield* makeFakeMuse();
1165+
const harness = yield* makeHarness(fake);
1166+
yield* startConversation(harness, fake);
1167+
const acknowledgement = pendingPromise<Record<string, unknown>>();
1168+
fake.queueResponse("approval/decide", acknowledgement.promise);
1169+
const child = { ...approval("child-run-1"), subagentOrigin: { subagentId: "a7" } };
1170+
yield* fake.emit("approval/requested", child);
1171+
yield* fake.takeCall("approval/decide");
1172+
yield* fake.emit("approval/resolved", child);
10531173
const resolved = yield* harness.takeEvent("runtime_request.updated");
10541174
assert.strictEqual(resolved.runtimeRequest.status, "resolved");
1055-
assert.strictEqual(resolved.runtimeRequest.decision, "accept");
1175+
acknowledgement.reject(
1176+
new MspError({
1177+
code: -32051,
1178+
message: "Already resolved",
1179+
data: { kind: "approvalAlreadyResolved" },
1180+
}),
1181+
);
1182+
// A later rejected answer provides an observable milestone after the stale rejection.
1183+
fake.queueResponse("approval/decide", new Error("Choice rejected"));
1184+
yield* fake.emit("approval/requested", {
1185+
...child,
1186+
approvalId: "approval-2",
1187+
currentRequirementId: { approvalId: "approval-2", sourceIndex: 0 },
1188+
});
1189+
const asked = yield* harness.takeEvent("runtime_request.updated");
1190+
assert.strictEqual(asked.runtimeRequest.nativeRequestRef?.nativeId, "approval-2");
1191+
assert.deepStrictEqual(
1192+
harness.allEvents.flatMap((event) =>
1193+
event.type === "runtime_request.updated" ? [event.runtimeRequest.status] : [],
1194+
),
1195+
["resolved", "pending"],
1196+
);
1197+
}).pipe(Effect.scoped, Effect.provide(testLayer)),
1198+
);
1199+
1200+
it.effect("does not reveal an automatic approval after the Muse connection closes", () =>
1201+
Effect.gen(function* () {
1202+
const fake = yield* makeFakeMuse();
1203+
const harness = yield* makeHarness(fake);
1204+
yield* startConversation(harness, fake);
1205+
const acknowledgement = pendingPromise<Record<string, unknown>>();
1206+
fake.queueResponse("approval/decide", acknowledgement.promise);
1207+
yield* fake.emit("approval/requested", {
1208+
...approval("child-run-1"),
1209+
subagentOrigin: { subagentId: "a7" },
1210+
});
1211+
yield* fake.takeCall("approval/decide");
1212+
yield* fake.disconnect();
1213+
yield* Deferred.await(harness.eventsEnded);
1214+
acknowledgement.reject(new Error("Connection closed"));
1215+
yield* Effect.promise(() => fake.host.exited);
1216+
assert.deepStrictEqual(
1217+
harness.allEvents.flatMap((event) =>
1218+
event.type === "runtime_request.updated" ? [event.runtimeRequest.status] : [],
1219+
),
1220+
["cancelled"],
1221+
);
10561222
}).pipe(Effect.scoped, Effect.provide(testLayer)),
10571223
);
10581224

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

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ interface PendingRequest {
229229
node: OrchestrationV2ExecutionNode;
230230
item: OrchestrationV2TurnItem;
231231
response?: Pick<OrchestrationV2RuntimeRequest, "decision" | "answers">;
232+
shown: boolean;
232233
}
233234

234235
/**
@@ -1078,6 +1079,18 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
10781079
choiceId: choice.choiceId,
10791080
}).pipe(Effect.tapError(() => Effect.sync(() => delete entry.response)));
10801081
});
1082+
const showRequest = Effect.fnUntraced(function* (entry: PendingRequest) {
1083+
entry.shown = true;
1084+
yield* emit({
1085+
type: "runtime_request.updated",
1086+
driver: MUSE_PROVIDER,
1087+
threadId: entry.node.threadId,
1088+
runtimeRequest: entry.request,
1089+
});
1090+
yield* emit({ type: "node.updated", driver: MUSE_PROVIDER, node: entry.node });
1091+
yield* emit({ type: "turn_item.updated", driver: MUSE_PROVIDER, turnItem: entry.item });
1092+
yield* updateSession("waiting");
1093+
});
10811094
/**
10821095
* A workflow child's approval is runless: the turn that started the workflow
10831096
* 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* (
12081221
required: true,
12091222
})),
12101223
};
1211-
const entry: PendingRequest = { native, request: runtimeRequest, node, item };
1224+
// Muse does not propagate allowAll to workflow children; T3 answers for them.
1225+
const autoApprove = child && input.runtimePolicy.runtimeMode === "full-access";
1226+
const entry: PendingRequest = {
1227+
native,
1228+
request: runtimeRequest,
1229+
node,
1230+
item,
1231+
shown: !autoApprove || previous?.shown === true,
1232+
};
12121233
pending.set(requestId, entry);
1213-
yield* emit({
1214-
type: "runtime_request.updated",
1215-
driver: MUSE_PROVIDER,
1216-
threadId,
1217-
runtimeRequest,
1218-
});
1219-
yield* emit({ type: "node.updated", driver: MUSE_PROVIDER, node });
1220-
yield* emit({ type: "turn_item.updated", driver: MUSE_PROVIDER, turnItem: item });
1221-
yield* updateSession("waiting");
1222-
// Muse does not give workflow children the session's allowAll mode, so in full
1223-
// access T3 approves for them. If Muse rejects that, the request waits for the user.
1224-
if (child && input.runtimePolicy.runtimeMode === "full-access")
1225-
yield* decideApproval(entry, "accept").pipe(Effect.ignore, Effect.forkIn(scope));
1234+
if (entry.shown) yield* showRequest(entry);
1235+
if (autoApprove)
1236+
yield* decideApproval(entry, "accept").pipe(
1237+
Effect.catch(() =>
1238+
Effect.gen(function* () {
1239+
// Native updates may settle or replace this entry while the RPC is in flight.
1240+
if (!closed && !broken && pending.get(requestId) === entry)
1241+
yield* showRequest(entry);
1242+
}).pipe(eventPermit.withPermits(1)),
1243+
),
1244+
Effect.forkIn(scope),
1245+
);
12261246
});
12271247
const handleRequestEvent = Effect.fnUntraced(function* (
12281248
method: string,
@@ -1268,7 +1288,8 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
12681288
if (!entry.response && native) entry.response = { decision: native };
12691289
yield* resolvePending(entry, "resolved");
12701290
}
1271-
if (!pending.size) yield* updateSession(active ? "running" : "ready");
1291+
if (![...pending.values()].some((candidate) => candidate.shown))
1292+
yield* updateSession(active ? "running" : "ready");
12721293
}
12731294
}
12741295
});
@@ -2097,7 +2118,9 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (
20972118
]
20982119
: [],
20992120
),
2100-
runtimeRequests: [...pending.values()].map((entry) => entry.request),
2121+
runtimeRequests: [...pending.values()]
2122+
.filter((entry) => entry.shown)
2123+
.map((entry) => entry.request),
21012124
providerPayload: items,
21022125
};
21032126
});

0 commit comments

Comments
 (0)