Skip to content

Commit 908b33f

Browse files
t3dotggclaude
andcommitted
fix(server): a Muse report turn that overlaps a user turn joins its run
Muse runs its own report turn before a queued user turn. The user's run now shows that turn's events and approvals, Stop ends it too, and the held continuation is dropped instead of deadlocking behind the user run. A dropped continuation also clears the held turn so it no longer pins the host. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent be39584 commit 908b33f

2 files changed

Lines changed: 96 additions & 24 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/MuseAdapterV2.test.ts‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
import * as DateTime from "effect/DateTime";
2323
import * as Deferred from "effect/Deferred";
2424
import * as Effect from "effect/Effect";
25+
import * as Option from "effect/Option";
2526
import type * as Exit from "effect/Exit";
2627
import * as FileSystem from "effect/FileSystem";
2728
import * as Fiber from "effect/Fiber";
@@ -40,6 +41,7 @@ import {
4041
type ProviderAdapterV2Event,
4142
type ProviderAdapterV2TurnInput,
4243
} from "../ProviderAdapter.ts";
44+
import type { ProviderContinuationRequest } from "../ProviderContinuationRequests.ts";
4345
import { makeMuseAdapterV2, type MuseAdapterV2Options } from "./MuseAdapterV2.ts";
4446

4547
const testLayer = Layer.mergeAll(
@@ -192,7 +194,10 @@ const makeHarness = Effect.fnUntraced(function* (
192194
replacement?: Effect.Success<ReturnType<typeof makeFakeMuse>>,
193195
existingProviderThread?: OrchestrationV2ProviderThread,
194196
policy = runtimePolicy,
195-
overrides: Pick<MuseAdapterV2Options, "createHost" | "nativeEventLogger" | "modelCatalog"> = {},
197+
overrides: Pick<
198+
MuseAdapterV2Options,
199+
"createHost" | "nativeEventLogger" | "modelCatalog" | "continuationRequests"
200+
> = {},
196201
) {
197202
let hostCount = 0;
198203
const adapter = makeMuseAdapterV2({
@@ -764,6 +769,46 @@ describe("MuseAdapterV2", () => {
764769
}).pipe(Effect.scoped, Effect.provide(testLayer)),
765770
);
766771

772+
it.effect("lets a user turn take a held Muse report turn, approvals included", () =>
773+
Effect.gen(function* () {
774+
const fake = yield* makeFakeMuse();
775+
const offers: Array<ProviderContinuationRequest> = [];
776+
const harness = yield* makeHarness(
777+
fake,
778+
INSTANCE_ID,
779+
undefined,
780+
undefined,
781+
undefined,
782+
runtimePolicy,
783+
{ continuationRequests: { offer: (request) => Effect.sync(() => offers.push(request)) } },
784+
);
785+
// A finished workflow's report turn starts while T3 runs nothing: it is held.
786+
yield* fake.emit("turn/started", { turnId: "report-1" });
787+
yield* Effect.yieldNow;
788+
assert.lengthOf(offers, 1);
789+
// The user sends a message first; Muse runs the report before it.
790+
const { nativeId } = yield* startConversation(harness, fake);
791+
yield* fake.emit("approval/requested", approval("report-1"));
792+
const asked = yield* harness.takeEvent(
793+
"runtime_request.updated",
794+
(event) => event.runtimeRequest.status === "pending",
795+
);
796+
yield* harness.runtime.respondToRuntimeRequest({
797+
requestId: asked.runtimeRequest.id,
798+
decision: "accept",
799+
});
800+
yield* fake.takeCall("approval/decide");
801+
yield* fake.emit("approval/resolved", { approvalId: "approval-1", turnId: "report-1" });
802+
yield* fake.emit("turn/completed", { turnId: "report-1", terminal: "completed" });
803+
yield* fake.emit("turn/completed", { turnId: nativeId, terminal: "completed" });
804+
const terminal = yield* harness.takeEvent("turn.terminal");
805+
assert.strictEqual(terminal.status, "completed");
806+
// The user turn took it, so the continuation is not dispatched.
807+
const dispatched = yield* offers[0]!.dispatchIfCurrent!(Effect.succeed("run"));
808+
assert.isTrue(Option.isNone(dispatched));
809+
}).pipe(Effect.scoped, Effect.provide(testLayer)),
810+
);
811+
767812
it.effect("validates structured answers and preserves selected labels with a custom note", () =>
768813
Effect.gen(function* () {
769814
const fake = yield* makeFakeMuse();

‎apps/server/src/orchestration-v2/Adapters/MuseAdapterV2.ts‎

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import * as Cause from "effect/Cause";
2323
import * as DateTime from "effect/DateTime";
2424
import * as Deferred from "effect/Deferred";
2525
import * as Effect from "effect/Effect";
26+
import * as Option from "effect/Option";
2627
import * as FileSystem from "effect/FileSystem";
2728
import * as Queue from "effect/Queue";
2829
import * as Schema from "effect/Schema";
@@ -218,6 +219,8 @@ interface ActiveTurn {
218219
readonly input: ProviderAdapterV2TurnInput;
219220
providerTurn: OrchestrationV2ProviderTurn;
220221
readonly nativeId: string;
222+
/** Turns Muse started on its own while this run was active, which this run shows. */
223+
readonly joined: Set<string>;
221224
readonly items: Map<string, MuseItem>;
222225
readonly ordinals: Map<string, number>;
223226
readonly started: Map<string, DateTime.Utc>;
@@ -685,12 +688,14 @@ export function makeMuseAdapterV2(options: MuseAdapterV2Options): ProviderAdapte
685688
const failed = host;
686689
yield* Effect.tryPromise(() => failed.close()).pipe(Effect.ignore, Effect.forkIn(scope));
687690
});
691+
const owns = (turn: ActiveTurn, turnId: string) =>
692+
turnId === turn.nativeId || turn.joined.has(turnId);
688693
const publishRequest = Effect.fnUntraced(function* (native: PendingRequest["native"]) {
689694
const turn = active;
690695
if (
691696
!turn ||
692697
native.value.sessionId !== nativeSessionId ||
693-
(native.value.turnId && native.value.turnId !== turn.nativeId)
698+
(native.value.turnId && !owns(turn, native.value.turnId))
694699
)
695700
return;
696701
const nativeId =
@@ -847,19 +852,34 @@ export function makeMuseAdapterV2(options: MuseAdapterV2Options): ProviderAdapte
847852
}
848853
}
849854
// Muse starts a turn on its own when a workflow finishes, to report its result.
850-
// Hold it and ask the orchestrator for a run; that run takes the held events.
851-
if (method === "turn/started" && typeof params.turnId === "string" && !active && !wake) {
852-
wake = { nativeId: params.turnId, events: [] };
853-
const reports = finishedBackground.splice(0);
854-
const notification = backgroundWorkNotification(reports);
855-
if (thread?.appThreadId && options.continuationRequests)
856-
yield* options.continuationRequests.offer({
857-
threadId: thread.appThreadId,
858-
providerThreadId: thread.id,
859-
driver: MUSE_PROVIDER,
860-
detail: null,
861-
...(notification ? { notification } : {}),
862-
});
855+
// During a run, that run shows it. Otherwise hold it and ask the orchestrator for
856+
// a run; that run takes the held events, unless a user turn takes them first.
857+
if (
858+
method === "turn/started" &&
859+
typeof params.turnId === "string" &&
860+
params.turnId !== active?.nativeId &&
861+
params.turnId !== wake?.nativeId
862+
) {
863+
if (active && !active.compact) active.joined.add(params.turnId);
864+
else if (!active && !wake) {
865+
const held = { nativeId: params.turnId, events: [] };
866+
wake = held;
867+
const notification = backgroundWorkNotification(finishedBackground.splice(0));
868+
if (thread?.appThreadId && options.continuationRequests)
869+
yield* options.continuationRequests.offer({
870+
threadId: thread.appThreadId,
871+
providerThreadId: thread.id,
872+
driver: MUSE_PROVIDER,
873+
detail: null,
874+
...(notification ? { notification } : {}),
875+
dispatchIfCurrent: (dispatch) =>
876+
wake === held ? Effect.map(dispatch, Option.some) : Effect.succeed(Option.none()),
877+
clearIfCurrent: () =>
878+
Effect.sync(() => {
879+
if (wake === held) wake = undefined;
880+
}),
881+
});
882+
}
863883
}
864884
if (wake && active?.nativeId !== wake.nativeId) {
865885
const turnId = typeof params.turnId === "string" ? params.turnId : itemEvent?.turnId;
@@ -870,7 +890,7 @@ export function makeMuseAdapterV2(options: MuseAdapterV2Options): ProviderAdapte
870890
}
871891
const turn = active;
872892
if (!turn) return;
873-
if (typeof params.turnId === "string" && params.turnId !== turn.nativeId && !turn.compact)
893+
if (typeof params.turnId === "string" && !owns(turn, params.turnId) && !turn.compact)
874894
return;
875895
switch (method) {
876896
case "item/started":
@@ -879,7 +899,7 @@ export function makeMuseAdapterV2(options: MuseAdapterV2Options): ProviderAdapte
879899
const item = itemEvent!;
880900
if (
881901
item.turnId &&
882-
item.turnId !== turn.nativeId &&
902+
!owns(turn, item.turnId) &&
883903
!(turn.compact && item.kind === "compaction")
884904
)
885905
return;
@@ -933,6 +953,8 @@ export function makeMuseAdapterV2(options: MuseAdapterV2Options): ProviderAdapte
933953
case "turn/completed": {
934954
if (turn.compact) return;
935955
const result = yield* decode(MuseTurnCompleted, params);
956+
// A joined Muse turn ending does not end this run; its own turn does.
957+
if (result.turnId !== turn.nativeId) break;
936958
if (result.usage) {
937959
const usage = result.usage;
938960
turn.providerTurn = {
@@ -1487,6 +1509,7 @@ export function makeMuseAdapterV2(options: MuseAdapterV2Options): ProviderAdapte
14871509
input: turnInput,
14881510
providerTurn,
14891511
nativeId,
1512+
joined: new Set(),
14901513
items: new Map(),
14911514
ordinals: new Map(),
14921515
started: new Map(),
@@ -1513,13 +1536,14 @@ export function makeMuseAdapterV2(options: MuseAdapterV2Options): ProviderAdapte
15131536
lastRunOrdinal: turnInput.runOrdinal,
15141537
});
15151538
yield* updateSession("running");
1516-
if (continuation) {
1517-
wake = undefined;
1518-
// Its turn may have ended already; then the held turn/completed ends this run.
1519-
if (!adopted) return yield* finish(turn, "completed");
1520-
for (const [method, data] of adopted.events)
1521-
yield* handleNotification(method, data).pipe(Effect.catch(failHost));
1522-
}
1539+
// A user turn takes a held Muse turn: Muse runs it first, so this run shows it
1540+
// (and its approvals), and the continuation asked for it is no longer needed.
1541+
const held = wake;
1542+
wake = undefined;
1543+
if (continuation && !held) return yield* finish(turn, "completed");
1544+
if (held && !continuation) turn.joined.add(held.nativeId);
1545+
for (const [method, data] of held?.events ?? [])
1546+
yield* handleNotification(method, data).pipe(Effect.catch(failHost));
15231547
}).pipe(eventPermit.withPermits(1));
15241548
if (continuation) return;
15251549
yield* Effect.gen(function* () {
@@ -1732,6 +1756,9 @@ export function makeMuseAdapterV2(options: MuseAdapterV2Options): ProviderAdapte
17321756
);
17331757
return;
17341758
}
1759+
// Muse runs a joined turn first, so Stop must end it too.
1760+
for (const joined of turn.joined)
1761+
yield* request("turn/interrupt", { turnId: joined }).pipe(Effect.ignore);
17351762
yield* request("turn/interrupt", { turnId: turn.nativeId });
17361763
yield* Deferred.await(turn.done).pipe(
17371764
Effect.timeout(options.requestTimeoutMs ?? 30_000),

0 commit comments

Comments
 (0)