Skip to content

Commit b3a27dd

Browse files
fix(client): stop background work behind a held queued message
1 parent 48a7cf1 commit b3a27dd

2 files changed

Lines changed: 78 additions & 1 deletion

File tree

‎packages/client-runtime/src/operations/commands.test.ts‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,82 @@ describe("V2 environment commands", () => {
633633
}).pipe(Effect.provide(layerTestCrypto)),
634634
);
635635

636+
it.effect("stops completed run background work when a newer queued run is held", () =>
637+
Effect.gen(function* () {
638+
const completedRun = {
639+
id: RunId.make("run-completed"),
640+
threadId: v2ThreadId,
641+
ordinal: 1,
642+
providerInstanceId: v2Projection.thread.providerInstanceId,
643+
modelSelection: v2Projection.thread.modelSelection,
644+
providerThreadId: null,
645+
userMessageId: MessageId.make("message-completed"),
646+
rootNodeId: null,
647+
activeAttemptId: null,
648+
status: "completed" as const,
649+
requestedAt: v2Now,
650+
startedAt: v2Now,
651+
completedAt: v2Now,
652+
checkpointId: null,
653+
contextHandoffId: null,
654+
};
655+
const projection: OrchestrationV2ThreadProjection = {
656+
...v2Projection,
657+
runs: [
658+
completedRun,
659+
{
660+
...completedRun,
661+
id: RunId.make("run-held"),
662+
ordinal: 2,
663+
userMessageId: MessageId.make("message-held"),
664+
status: "queued",
665+
queuePosition: 1,
666+
queueHeld: true,
667+
startedAt: null,
668+
completedAt: null,
669+
},
670+
],
671+
turnItems: [
672+
{
673+
id: TurnItemId.make("background-command"),
674+
threadId: v2ThreadId,
675+
runId: completedRun.id,
676+
nodeId: null,
677+
providerThreadId: null,
678+
providerTurnId: null,
679+
nativeItemRef: null,
680+
parentItemId: null,
681+
ordinal: 1,
682+
status: "running",
683+
title: null,
684+
startedAt: v2Now,
685+
completedAt: null,
686+
updatedAt: v2Now,
687+
type: "command_execution",
688+
input: "vp run dev",
689+
},
690+
],
691+
};
692+
const commands: OrchestrationV2Command[] = [];
693+
const supervisor = yield* makeSupervisor({ commands, projects: [], projection });
694+
695+
const result = yield* interruptThreadTurn({ threadId: v2ThreadId }).pipe(
696+
Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
697+
);
698+
699+
expect(commands).toEqual([
700+
{
701+
type: "run.interrupt",
702+
commandId: expect.any(String),
703+
threadId: v2ThreadId,
704+
runId: completedRun.id,
705+
holdQueue: true,
706+
},
707+
]);
708+
expect(result).toEqual({ sequence: 1 });
709+
}).pipe(Effect.provide(layerTestCrypto)),
710+
);
711+
636712
it.effect(
637713
"dispatches V2-native relationship and queue commands without compatibility shaping",
638714
() =>

‎packages/client-runtime/src/operations/commands.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
} from "@t3tools/contracts";
3030
import { modelSelectionCommandType } from "@t3tools/shared/model";
3131
import { derivePendingBackgroundWork } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
32+
import { latestUnheldRun } from "@t3tools/shared/orchestrationV2ThreadError";
3233
import { visibleThreadPullRequests } from "@t3tools/shared/threadPullRequests";
3334
import * as Crypto from "effect/Crypto";
3435
import * as Effect from "effect/Effect";
@@ -792,7 +793,7 @@ export const interruptThreadTurn = Effect.fn("EnvironmentCommands.interruptThrea
792793
run.status === "waiting",
793794
)?.id;
794795
if (runId === undefined) {
795-
const latestRun = projection.runs.at(-1);
796+
const latestRun = latestUnheldRun(projection.runs);
796797
if (
797798
derivePendingBackgroundWork({
798799
latestRun,

0 commit comments

Comments
 (0)