Skip to content

Commit fb2e5a0

Browse files
fix(client-runtime): imported threads that never ran no longer sit in Working as Waiting (#33042)
1 parent 8cb55fa commit fb2e5a0

7 files changed

Lines changed: 66 additions & 12 deletions

File tree

‎apps/web/src/components/ChatView.logic.test.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import {
99
deriveAvailableRevertTurnCounts,
1010
deriveThreadPendingBackgroundTasks,
1111
} from "./ChatView.logic";
12-
import { recallCheckoutIsRepo, rememberCheckoutIsRepo } from "./ChatView.logic";
12+
import {
13+
isThreadEnvironmentLocked,
14+
recallCheckoutIsRepo,
15+
rememberCheckoutIsRepo,
16+
} from "./ChatView.logic";
1317
import { ProviderDriverKind, type ServerProvider } from "@t3tools/contracts";
1418
import { deriveProviderInstanceEntries } from "../providerInstances";
1519
import { ensureInlineContextReferences } from "../lib/composerContextReferences";
@@ -133,6 +137,26 @@ describe("deriveComposerSendState", () => {
133137
});
134138
});
135139

140+
describe("isThreadEnvironmentLocked", () => {
141+
it.each([
142+
{ name: "an empty unbound thread", providerThread: null, expected: false },
143+
{
144+
name: "an imported session bound before its first run",
145+
providerThread: "imported",
146+
expected: true,
147+
},
148+
])("locks $name: $expected", ({ providerThread, expected }) => {
149+
expect(
150+
isThreadEnvironmentLocked({
151+
messageCount: 0,
152+
hasRuntime: false,
153+
activeProviderThreadId:
154+
providerThread === null ? null : ProviderThreadId.make(providerThread),
155+
}),
156+
).toBe(expected);
157+
});
158+
});
159+
136160
describe("buildExpiredTerminalContextToastCopy", () => {
137161
it("refers to several expired contexts as them", () => {
138162
expect(buildExpiredTerminalContextToastCopy(2, "empty")).toEqual({

‎apps/web/src/components/ChatView.logic.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
type PreviewAnnotationPayload,
3333
ProviderDriverKind,
3434
type ProviderInstanceId,
35+
type ProviderThreadId,
3536
type ServerProvider,
3637
type ScopedProjectRef,
3738
type ScopedThreadRef,
@@ -1541,3 +1542,12 @@ export function deriveAvailableRevertTurnCounts(projection: ThreadStructure | nu
15411542
}
15421543
return available;
15431544
}
1545+
1546+
/** An imported native session is bound before it has messages or runs, and its cwd is fixed. */
1547+
export function isThreadEnvironmentLocked(input: {
1548+
readonly messageCount: number;
1549+
readonly hasRuntime: boolean;
1550+
readonly activeProviderThreadId: ProviderThreadId | null;
1551+
}): boolean {
1552+
return input.messageCount > 0 || input.hasRuntime || input.activeProviderThreadId !== null;
1553+
}

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { ThreadFind, ThreadFindCanvas } from "./chat/ThreadFindProvider";
3737
import { closeThreadFind, openThreadFind, useThreadFindOpen } from "./chat/threadFindStore";
3838
import { THREAD_FIND_BAR_RESERVED_HEIGHT } from "./chat/ThreadFindBar";
3939
import { usageLimitRecoveryBannerItem } from "./chat/UsageLimitRecoveryBanner";
40-
import { resolveDraftHeroState } from "./ChatView.logic";
40+
import { isThreadEnvironmentLocked, resolveDraftHeroState } from "./ChatView.logic";
4141
import { useQueuedRunEdit } from "./chat/useQueuedRunEdit";
4242
import { recallCheckoutIsRepo, rememberCheckoutIsRepo } from "./ChatView.logic";
4343
import { useLogicalProjectEnvironments } from "../hooks/useLogicalProjectEnvironments";
@@ -1281,7 +1281,13 @@ export default function ChatView(props: ChatViewProps) {
12811281
advertisedFileAttachmentBytes === null
12821282
? null
12831283
: clampFileAttachmentUploadBytes(advertisedFileAttachmentBytes);
1284-
const envLocked = Boolean(activeThread && (activeMessageCount > 0 || activeRuntime !== null));
1284+
const envLocked =
1285+
Boolean(activeThread) &&
1286+
isThreadEnvironmentLocked({
1287+
messageCount: activeMessageCount,
1288+
hasRuntime: activeRuntime !== null,
1289+
activeProviderThreadId: serverThread?.activeProviderThreadId ?? null,
1290+
});
12851291
// onEnvironmentChange only retargets an unsent draft. envLocked alone does not
12861292
// rule out a started thread: one whose machine is offline never loads its
12871293
// messages, so it would otherwise be offered a machine picker that does nothing.

‎packages/client-runtime/src/state/entities.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,19 @@ describe("V2 client presentation", () => {
9898
).toBeNull();
9999
});
100100

101+
it("keeps an imported thread that never ran out of Working", () => {
102+
const imported = presentThreadShell(environmentId, {
103+
...v2ThreadShell,
104+
historyOrigin: "v1_import",
105+
activeProviderThreadId: ProviderThreadId.make("provider-thread-imported"),
106+
latestRunId: null,
107+
activeRunId: null,
108+
status: "idle",
109+
pendingBackgroundTasks: [],
110+
});
111+
expect(isThreadWorking(imported)).toBe(false);
112+
});
113+
101114
it.each([
102115
{ kinds: ["command"], expected: "completed" },
103116
{ kinds: ["command", "subagent"], expected: "idle" },

‎packages/client-runtime/src/state/models.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ function shellRuntime(thread: OrchestrationV2ThreadShell): ThreadRuntimeSummary
231231
const parkAtIdle =
232232
backgroundWorkHoldsCompletion(thread.pendingBackgroundTasks ?? []) &&
233233
thread.status !== "failed";
234-
if (thread.latestRunId === null && thread.activeProviderThreadId === null && !parkAtIdle) {
234+
if (thread.latestRunId === null && !parkAtIdle) {
235235
return null;
236236
}
237237
const status = parkAtIdle ? "idle" : (thread.activityRunStatus ?? thread.status);

‎packages/client-runtime/src/state/threadExecution.test.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,14 @@ describe("deriveRunlessWorkStartedAt", () => {
553553
};
554554

555555
it("times a provider-native subagent from its runless root turn while it works", () => {
556-
const projection = { ...nativeChild, nodes: [rootTurn("running", later)] };
556+
const projection = {
557+
...nativeChild,
558+
thread: {
559+
...nativeChild.thread,
560+
activeProviderThreadId: ProviderThreadId.make("native-child"),
561+
},
562+
nodes: [rootTurn("running", later)],
563+
};
557564
expect(deriveRunlessWorkStartedAt(projection)).toBe("2026-07-28T10:05:00.000Z");
558565
expect(deriveThreadRuntime(projection)).toBeNull();
559566
});

‎packages/client-runtime/src/state/threadExecution.ts‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -366,13 +366,7 @@ export function deriveThreadStructureRuntime(
366366
}),
367367
);
368368
// A pull request watch can hold a thread that never ran.
369-
if (
370-
latestRun === null &&
371-
projection.thread.activeProviderThreadId === null &&
372-
!backgroundWorkHoldsRun
373-
) {
374-
return null;
375-
}
369+
if (latestRun === null && !backgroundWorkHoldsRun) return null;
376370
const activeRunId =
377371
latestMatchingRun(projection, (run) => INTERRUPTIBLE_RUN_STATUSES.has(run.status))?.id ?? null;
378372
return {

0 commit comments

Comments
 (0)