fix(orchestration): cancel queued work on archive

Co-authored-by: codex <[email protected]>
This commit is contained in:
Julius Marmingeandcodex committed 2026-08-07 13:26:25 +02:00
1 parent b00f611f81
commit 3fb4e159bf
5 files changed
+222 -29

No files matched your search

@@ -0,0 +1,34 @@
import { ProviderInstanceId, RunId } from "@t3tools/contracts";
import { describe, expect, it } from "vite-plus/test";
import type { ThreadRuntimeSummary } from "@t3tools/client-runtime/state/models";
import { threadCanArchive } from "./threadArchive";
function runtime(
status: ThreadRuntimeSummary["status"],
activeRunId: ThreadRuntimeSummary["activeRunId"],
): ThreadRuntimeSummary {
return {
status,
activeRunId,
providerInstanceId: ProviderInstanceId.make("codex"),
providerName: "codex",
lastError: null,
updatedAt: "2026-07-28T10:00:00.000Z",
};
}
describe("threadCanArchive", () => {
it("blocks provider-active work", () => {
const activeRunId = RunId.make("run-live");
expect(threadCanArchive(runtime("preparing", activeRunId))).toBe(false);
expect(threadCanArchive(runtime("starting", activeRunId))).toBe(false);
expect(threadCanArchive(runtime("running", activeRunId))).toBe(false);
expect(threadCanArchive(runtime("queued", activeRunId))).toBe(false);
});
it("allows queued-only and post-provider waiting work", () => {
expect(threadCanArchive(runtime("queued", null))).toBe(true);
expect(threadCanArchive(runtime("waiting", null))).toBe(true);
});
});
@@ -0,0 +1,10 @@
import { threadRuntimeHasInterruptibleRun } from "@t3tools/client-runtime/state/thread-execution";
import type { ThreadRuntimeSummary } from "@t3tools/client-runtime/state/models";
/**
* Archiving may discard queued work, but it must not detach a provider while
* that provider is still executing a turn.
*/
export function threadCanArchive(runtime: ThreadRuntimeSummary | null | undefined): boolean {
return !threadRuntimeHasInterruptibleRun(runtime);
}
@@ -1,7 +1,4 @@
import {
threadRuntimeIsActive,
type EnvironmentThreadShell,
} from "@t3tools/client-runtime/state/shell";
import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
import { canSettle } from "@t3tools/client-runtime/state/thread-settled";
import * as Cause from "effect/Cause";
import * as Haptics from "expo-haptics";
@@ -15,6 +12,7 @@ import { appAtomRegistry } from "../../state/atom-registry";
import { environmentServerConfigsAtom } from "../../state/server";
import { threadEnvironment } from "../../state/threads";
import { useAtomCommand } from "../../state/use-atom-command";
import { threadCanArchive } from "./threadArchive";
/** Version skew: never send settle/unsettle to a server that predates them
(capability defaults false on decode for older servers). */
@@ -110,9 +108,9 @@ function useThreadActionExecutor(
);
return false;
}
// Archive keeps its original, narrower guard: never interrupt a
// thread mid-turn.
if (action === "archive" && threadRuntimeIsActive(thread.runtime)) {
// The server cancels queued runs on archive. Only an actual provider
// turn still needs the user to interrupt it first.
if (action === "archive" && !threadCanArchive(thread.runtime)) {
Alert.alert(
actionFailureTitle(action),
"This thread is working. Interrupt it first, then try again.",