From 7a4a0fa759287f19dd2eecc3fc5c3cb56ce61881 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 29 Jul 2026 03:06:12 +0200 Subject: [PATCH] fix(mobile): gate thread controls on live runs Co-authored-by: codex --- .../src/state/use-thread-composer-state.ts | 3 +- .../src/state/threadExecution.test.ts | 5 + .../src/state/threadExecution.ts | 8 ++ .../src/state/threadWorkflows.test.ts | 92 ++++++++++++++++++- .../src/state/threadWorkflows.ts | 8 +- 5 files changed, 113 insertions(+), 3 deletions(-) diff --git a/apps/mobile/src/state/use-thread-composer-state.ts b/apps/mobile/src/state/use-thread-composer-state.ts index 61aeca9c93..04f81147ae 100644 --- a/apps/mobile/src/state/use-thread-composer-state.ts +++ b/apps/mobile/src/state/use-thread-composer-state.ts @@ -2,6 +2,7 @@ import { useAtomValue } from "@effect/atom-react"; import { threadRuntimeIsActive } from "@t3tools/client-runtime/state/shell"; import { deriveThreadActivityRun, + deriveThreadInterruptibleRunId, deriveThreadRuntime, } from "@t3tools/client-runtime/state/thread-execution"; import { useCallback, useEffect, useMemo } from "react"; @@ -149,7 +150,7 @@ export function useThreadComposerState() { }, [selectedThreadActivityRun, selectedThreadSessionActivity, selectedThreadShell]); const activeThreadBusy = threadRuntimeIsActive(selectedThreadRuntime); - const interruptibleRunId = selectedThreadRuntime?.activeRunId ?? null; + const interruptibleRunId = deriveThreadInterruptibleRunId(selectedThreadProjection?.projection); const onSendMessage = useCallback(async () => { if (!selectedThreadShell) { diff --git a/packages/client-runtime/src/state/threadExecution.test.ts b/packages/client-runtime/src/state/threadExecution.test.ts index 85769a7624..8d2d7f0b21 100644 --- a/packages/client-runtime/src/state/threadExecution.test.ts +++ b/packages/client-runtime/src/state/threadExecution.test.ts @@ -6,6 +6,7 @@ import { v2Projection } from "./orchestrationV2TestFixtures.ts"; import { deriveLatestThreadRun, deriveThreadActivityRun, + deriveThreadInterruptibleRunId, deriveThreadRuntime, threadRuntimeHasInterruptibleRun, } from "./threadExecution.ts"; @@ -50,6 +51,7 @@ describe("thread execution presentation", () => { activeRunId: runningRun.id, }); expect(threadRuntimeHasInterruptibleRun(runtime)).toBe(true); + expect(deriveThreadInterruptibleRunId(projection)).toBe(runningRun.id); }); it("does not expose a queued-only run as interruptible", () => { @@ -67,6 +69,7 @@ describe("thread execution presentation", () => { activeRunId: null, }); expect(threadRuntimeHasInterruptibleRun(runtime)).toBe(false); + expect(deriveThreadInterruptibleRunId(projection)).toBeNull(); }); it("keeps checkpoint-wait activity visible without exposing a non-functional interrupt", () => { @@ -84,5 +87,7 @@ describe("thread execution presentation", () => { activeRunId: null, }); expect(threadRuntimeHasInterruptibleRun(runtime)).toBe(false); + expect(deriveThreadInterruptibleRunId(projection)).toBeNull(); + expect(deriveThreadInterruptibleRunId(null)).toBeNull(); }); }); diff --git a/packages/client-runtime/src/state/threadExecution.ts b/packages/client-runtime/src/state/threadExecution.ts index 3703867993..6ef20c91e3 100644 --- a/packages/client-runtime/src/state/threadExecution.ts +++ b/packages/client-runtime/src/state/threadExecution.ts @@ -78,6 +78,14 @@ export function deriveThreadRuntime( }; } +export function deriveThreadInterruptibleRunId( + projection: OrchestrationV2ThreadProjection | null | undefined, +): ThreadRuntimeSummary["activeRunId"] { + return projection === null || projection === undefined + ? null + : (deriveThreadRuntime(projection)?.activeRunId ?? null); +} + export function threadRuntimeHasInterruptibleRun( runtime: ThreadRuntimeSummary | null | undefined, ): boolean { diff --git a/packages/client-runtime/src/state/threadWorkflows.test.ts b/packages/client-runtime/src/state/threadWorkflows.test.ts index 44699d130f..71d5ab75bf 100644 --- a/packages/client-runtime/src/state/threadWorkflows.test.ts +++ b/packages/client-runtime/src/state/threadWorkflows.test.ts @@ -33,7 +33,13 @@ describe("thread workflows", () => { const state = deriveThreadQueueWorkflowState({ thread: { id: "thread", activeProviderThreadId: "provider-thread" }, runs: [ - { id: "active", status: "running", providerThreadId: "provider-thread", ordinal: 1 }, + { + id: "active", + status: "running", + providerThreadId: "provider-thread", + activeAttemptId: "attempt-active", + ordinal: 1, + }, { id: "later", status: "queued", userMessageId: "message-later", ordinal: 3 }, { id: "first", @@ -47,6 +53,13 @@ describe("thread workflows", () => { { id: "message-first", text: "First" }, { id: "message-later", text: "Later" }, ], + providerTurns: [ + { + id: "provider-turn-active", + runAttemptId: "attempt-active", + status: "running", + }, + ], providerThreads: [ { id: "provider-thread", @@ -72,6 +85,83 @@ describe("thread workflows", () => { expect(state.canPromoteToSteer).toBe(true); }); + it.each(["preparing", "starting", "waiting"] as const)( + "does not promote queued work into a %s run", + (status) => { + const state = deriveThreadQueueWorkflowState({ + thread: { id: "thread", activeProviderThreadId: "provider-thread" }, + runs: [ + { + id: "active", + status, + providerThreadId: "provider-thread", + activeAttemptId: "attempt-active", + ordinal: 1, + }, + { id: "queued", status: "queued", userMessageId: "message", ordinal: 2 }, + ], + messages: [{ id: "message", text: "Queued" }], + providerTurns: [ + { + id: "provider-turn-active", + runAttemptId: "attempt-active", + status: status === "waiting" ? "completed" : "starting", + }, + ], + providerThreads: [ + { + id: "provider-thread", + appThreadId: "thread", + providerSessionId: "provider-session", + }, + ], + providerSessions: [ + { + id: "provider-session", + status: "running", + capabilities: capabilities({ queued: true, steer: true }), + }, + ], + } as never); + + expect(state.canPromoteToSteer).toBe(false); + }, + ); + + it("does not promote queued work until the running provider turn is projected", () => { + const state = deriveThreadQueueWorkflowState({ + thread: { id: "thread", activeProviderThreadId: "provider-thread" }, + runs: [ + { + id: "active", + status: "running", + providerThreadId: "provider-thread", + activeAttemptId: "attempt-active", + ordinal: 1, + }, + { id: "queued", status: "queued", userMessageId: "message", ordinal: 2 }, + ], + messages: [{ id: "message", text: "Queued" }], + providerTurns: [], + providerThreads: [ + { + id: "provider-thread", + appThreadId: "thread", + providerSessionId: "provider-session", + }, + ], + providerSessions: [ + { + id: "provider-session", + status: "running", + capabilities: capabilities({ queued: true, steer: true }), + }, + ], + } as never); + + expect(state.canPromoteToSteer).toBe(false); + }); + it("does not expose known unsupported queue or fork actions", () => { const projection = { thread: { id: "thread", activeProviderThreadId: "provider-thread" }, diff --git a/packages/client-runtime/src/state/threadWorkflows.ts b/packages/client-runtime/src/state/threadWorkflows.ts index 20134b9cfc..2eac2f6336 100644 --- a/packages/client-runtime/src/state/threadWorkflows.ts +++ b/packages/client-runtime/src/state/threadWorkflows.ts @@ -84,6 +84,12 @@ export function deriveThreadQueueWorkflowState(projection: Projection): ThreadQu const activeRun = resolveActiveThreadRun(projection); const session = resolveThreadProviderSession(projection); const capabilities = session?.capabilities.turns; + const hasSteerableProviderTurn = + activeRun?.status === "running" && + activeRun.activeAttemptId !== null && + projection.providerTurns.some( + (turn) => turn.runAttemptId === activeRun.activeAttemptId && turn.status === "running", + ); const queuedRuns = copySorted( projection.runs.filter((run) => run.status === "queued"), (left, right) => @@ -101,7 +107,7 @@ export function deriveThreadQueueWorkflowState(projection: Projection): ThreadQu queuedRuns, canReorder: capabilities?.supportsQueuedMessages === true, canPromoteToSteer: - activeRun !== null && + hasSteerableProviderTurn && (capabilities?.supportsActiveSteering === true || capabilities?.supportsSteeringByInterruptRestart === true), };