Skip to content

Commit 38dcec3

Browse files
committed
fix(web): tighten offline-thread escape and drop dead worktrees
Require an actual latest turn before skipping an offline thread, and clear branch/worktree options when New Thread retargets to another environment so a dead machine's checkout is not reused.
1 parent 93d4fac commit 38dcec3

5 files changed

Lines changed: 89 additions & 13 deletions

File tree

‎apps/web/src/hooks/useHandleNewThread.ts‎

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { useEnvironments, usePrimaryEnvironmentId } from "../state/environments"
2929
import {
3030
resolveAvailableNewThreadProjectRef,
3131
resolveNewDraftStartFromOrigin,
32+
resolveWorkspaceOptionsAfterEnvironmentRetarget,
3233
} from "../lib/chatThreadActions";
3334
import { readT3ProjectFileDefaultThreadEnvMode } from "../lib/t3ProjectFileDefaults";
3435
import { shouldReadProjectFileForNewThreadDefaults } from "../session-logic";
@@ -198,6 +199,11 @@ export function useNewThreadHandler() {
198199
candidate.id === projectRef.projectId &&
199200
candidate.environmentId === projectRef.environmentId,
200201
) ?? requestedProject;
202+
const workspaceOptions = resolveWorkspaceOptionsAfterEnvironmentRetarget({
203+
requestedEnvironmentId: requestedProjectRef.environmentId,
204+
targetEnvironmentId: projectRef.environmentId,
205+
options,
206+
});
201207
// The shared resolver owns the priority order. The t3.json read is
202208
// skipped entirely when a higher-priority source decides or the
203209
// environment cannot serve: executeAtomQuery waits on a live RPC, so
@@ -223,10 +229,10 @@ export function useNewThreadHandler() {
223229
const logicalProjectKey = project
224230
? deriveLogicalProjectKeyFromSettings(project, projectGroupingSettings)
225231
: scopedProjectKey(projectRef);
226-
const hasBranchOption = options?.branch !== undefined;
227-
const hasWorktreePathOption = options?.worktreePath !== undefined;
228-
const hasEnvModeOption = options?.envMode !== undefined;
229-
const hasStartFromOriginOption = options?.startFromOrigin !== undefined;
232+
const hasBranchOption = workspaceOptions?.branch !== undefined;
233+
const hasWorktreePathOption = workspaceOptions?.worktreePath !== undefined;
234+
const hasEnvModeOption = workspaceOptions?.envMode !== undefined;
235+
const hasStartFromOriginOption = workspaceOptions?.startFromOrigin !== undefined;
230236
const storedDraftThread = getDraftSessionByLogicalProjectKey(logicalProjectKey);
231237
const storedDraftThreadRef = storedDraftThread
232238
? scopeThreadRef(storedDraftThread.environmentId, storedDraftThread.threadId)
@@ -273,7 +279,7 @@ export function useNewThreadHandler() {
273279
// the user may have just picked a branch in the composer.
274280
let workspaceContext: NewThreadWorkspaceOptions | null = null;
275281
if (hasExplicitWorkspaceOption) {
276-
workspaceContext = pickExplicitWorkspaceOptions(options);
282+
workspaceContext = pickExplicitWorkspaceOptions(workspaceOptions);
277283
} else if (!isDraftAlreadyOpen) {
278284
const defaultEnvMode = await resolveDefaultEnvMode();
279285
// The await yields. If the draft was opened (a concurrent
@@ -379,14 +385,17 @@ export function useNewThreadHandler() {
379385
hasEnvModeOption ||
380386
hasStartFromOriginOption
381387
) {
382-
setDraftThreadContext(currentRouteTarget.draftId, pickExplicitWorkspaceOptions(options));
388+
setDraftThreadContext(
389+
currentRouteTarget.draftId,
390+
pickExplicitWorkspaceOptions(workspaceOptions),
391+
);
383392
}
384393
setLogicalProjectDraftThreadId(logicalProjectKey, projectRef, currentRouteTarget.draftId, {
385394
threadId: latestActiveDraftThread.threadId,
386395
createdAt: latestActiveDraftThread.createdAt,
387396
runtimeMode: latestActiveDraftThread.runtimeMode,
388397
interactionMode: latestActiveDraftThread.interactionMode,
389-
...pickExplicitWorkspaceOptions(options),
398+
...pickExplicitWorkspaceOptions(workspaceOptions),
390399
});
391400
return Promise.resolve({
392401
draftId: currentRouteTarget.draftId,
@@ -398,7 +407,7 @@ export function useNewThreadHandler() {
398407
const threadId = newThreadId();
399408
const createdAt = new Date().toISOString();
400409
return (async () => {
401-
const initialEnvMode = options?.envMode ?? (await resolveDefaultEnvMode());
410+
const initialEnvMode = workspaceOptions?.envMode ?? (await resolveDefaultEnvMode());
402411
// The await yields, so a concurrent invocation may have registered a
403412
// draft for this logical project in the meantime. Registering ours
404413
// too would evict that draft while its navigation is in flight —
@@ -426,7 +435,7 @@ export function useNewThreadHandler() {
426435
createdAt: racedDraft.createdAt,
427436
runtimeMode: racedDraft.runtimeMode,
428437
interactionMode: racedDraft.interactionMode,
429-
...pickExplicitWorkspaceOptions(options),
438+
...pickExplicitWorkspaceOptions(workspaceOptions),
430439
});
431440
carryComposerContentTo(racedDraft.draftId);
432441
await router.navigate({
@@ -439,11 +448,11 @@ export function useNewThreadHandler() {
439448
setLogicalProjectDraftThreadId(logicalProjectKey, projectRef, draftId, {
440449
threadId,
441450
createdAt,
442-
branch: options?.branch ?? null,
443-
worktreePath: options?.worktreePath ?? null,
451+
branch: workspaceOptions?.branch ?? null,
452+
worktreePath: workspaceOptions?.worktreePath ?? null,
444453
envMode: initialEnvMode,
445454
startFromOrigin:
446-
options?.startFromOrigin ??
455+
workspaceOptions?.startFromOrigin ??
447456
resolveNewDraftStartFromOrigin({
448457
envMode: initialEnvMode,
449458
newWorktreesStartFromOrigin: primaryServerSettings.newWorktreesStartFromOrigin,

‎apps/web/src/lib/chatThreadActions.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
resolveAvailableNewThreadProjectRef,
66
resolveThreadActionProjectRef,
77
resolveNewDraftStartFromOrigin,
8+
resolveWorkspaceOptionsAfterEnvironmentRetarget,
89
startNewThreadFromContext,
910
type ChatThreadActionContext,
1011
} from "./chatThreadActions";
@@ -161,4 +162,46 @@ describe("chatThreadActions", () => {
161162
}),
162163
).toEqual(requested);
163164
});
165+
166+
it("keeps explicit workspace options when the draft stays on the requested environment", () => {
167+
expect(
168+
resolveWorkspaceOptionsAfterEnvironmentRetarget({
169+
requestedEnvironmentId: ENVIRONMENT_ID,
170+
targetEnvironmentId: ENVIRONMENT_ID,
171+
options: {
172+
branch: "feat/checkout",
173+
worktreePath: "/dead/machine/worktree",
174+
envMode: "worktree",
175+
startFromOrigin: false,
176+
},
177+
}),
178+
).toEqual({
179+
branch: "feat/checkout",
180+
worktreePath: "/dead/machine/worktree",
181+
envMode: "worktree",
182+
startFromOrigin: false,
183+
});
184+
});
185+
186+
it("clears machine-specific workspace options when retargeting to another environment", () => {
187+
const targetEnvironmentId = EnvironmentId.make("environment-2");
188+
189+
expect(
190+
resolveWorkspaceOptionsAfterEnvironmentRetarget({
191+
requestedEnvironmentId: ENVIRONMENT_ID,
192+
targetEnvironmentId,
193+
options: {
194+
branch: "feat/checkout",
195+
worktreePath: "/dead/machine/worktree",
196+
envMode: "worktree",
197+
startFromOrigin: false,
198+
},
199+
}),
200+
).toEqual({
201+
branch: null,
202+
worktreePath: null,
203+
envMode: "worktree",
204+
startFromOrigin: false,
205+
});
206+
});
164207
});

‎apps/web/src/lib/chatThreadActions.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,25 @@ export function resolveAvailableNewThreadProjectRef(input: {
6868
return next ? scopeProjectRef(next.environmentId, next.projectId) : input.requested;
6969
}
7070

71+
export function resolveWorkspaceOptionsAfterEnvironmentRetarget<
72+
TOptions extends {
73+
branch?: string | null;
74+
worktreePath?: string | null;
75+
},
76+
>(input: {
77+
requestedEnvironmentId: EnvironmentId;
78+
targetEnvironmentId: EnvironmentId;
79+
options: TOptions | undefined;
80+
}): TOptions | undefined {
81+
if (input.options === undefined) return undefined;
82+
if (input.requestedEnvironmentId === input.targetEnvironmentId) return input.options;
83+
return {
84+
...input.options,
85+
...(input.options.branch !== undefined ? { branch: null } : {}),
86+
...(input.options.worktreePath !== undefined ? { worktreePath: null } : {}),
87+
};
88+
}
89+
7190
// New threads inherit only the *project* from the current context. Branch,
7291
// worktree, and env mode always come from the user's configured defaults —
7392
// carrying them over from the viewed thread meant "new thread" silently

‎apps/web/src/session-logic.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,6 +1708,11 @@ describe("offline environment escape for unsettled latest turns", () => {
17081708
).toBe(false);
17091709
});
17101710

1711+
it("does not treat a never-started thread as an unsettled trap when the environment is offline", () => {
1712+
expect(shouldEscapeUnsettledThreadOnOfflineEnvironment(null, null, "offline")).toBe(false);
1713+
expect(shouldOpenLatestThreadForProject(null, null, "offline")).toBe(true);
1714+
});
1715+
17111716
it("does not consult t3.json for new-thread defaults unless the environment is connected", () => {
17121717
expect(shouldReadProjectFileForNewThreadDefaults(null, "connected")).toBe(true);
17131718
expect(shouldReadProjectFileForNewThreadDefaults(null, "offline")).toBe(false);

‎apps/web/src/session-logic.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ export function shouldEscapeUnsettledThreadOnOfflineEnvironment(
347347
connectionPhase: EnvironmentConnectionPhase | null | undefined,
348348
): boolean {
349349
if (canCreateProjectInEnvironment(connectionPhase)) return false;
350-
return !isLatestTurnSettled(latestTurn, session);
350+
return latestTurn !== null && !isLatestTurnSettled(latestTurn, session);
351351
}
352352

353353
export function shouldOpenLatestThreadForProject(

0 commit comments

Comments
 (0)