Skip to content

Commit f49bdd1

Browse files
committed
fix(server): branch worktree vs workspace error and use stat
Address review feedback on the workspace existence guard: - Use fs.stat + isDirectory instead of fs.exists so a path that is a file is also rejected - Branch the error message on thread.worktreePath so a missing worktree gets worktree-specific guidance, not the Settings prompt - Use per-run temp dirs in tests to avoid concurrent run races
1 parent 63b7753 commit f49bdd1

2 files changed

Lines changed: 37 additions & 22 deletions

File tree

‎apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts‎

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,19 @@ describe("ProviderCommandReactor", () => {
9999
let scope: Scope.Closeable | null = null;
100100
const createdStateDirs = new Set<string>();
101101
const createdBaseDirs = new Set<string>();
102+
// Per-run workspace dirs so concurrent test runs don't race on the same path.
103+
// The reactor's existence guard checks these before starting a provider session.
104+
let workspaceRoot = "";
105+
let workspaceWorktree = "";
102106

103-
// The workspace existence guard in ProviderCommandReactor checks that the
104-
// project's workspaceRoot exists on disk before starting a provider session.
105-
// Create the paths the suite uses once for the whole run.
106-
const WORKSPACE_ROOT = "/tmp/provider-project";
107-
const WORKSPACE_WORKTREE = "/tmp/provider-project-worktree";
108107
beforeAll(() => {
109-
NodeFS.mkdirSync(WORKSPACE_ROOT, { recursive: true });
110-
NodeFS.mkdirSync(WORKSPACE_WORKTREE, { recursive: true });
108+
workspaceRoot = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3code-ws-"));
109+
workspaceWorktree = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3code-wt-"));
111110
});
111+
112112
afterAll(() => {
113-
NodeFS.rmSync(WORKSPACE_ROOT, { recursive: true, force: true });
114-
NodeFS.rmSync(WORKSPACE_WORKTREE, { recursive: true, force: true });
113+
NodeFS.rmSync(workspaceRoot, { recursive: true, force: true });
114+
NodeFS.rmSync(workspaceWorktree, { recursive: true, force: true });
115115
});
116116

117117
afterEach(async () => {
@@ -444,7 +444,7 @@ describe("ProviderCommandReactor", () => {
444444
commandId: CommandId.make("cmd-project-create"),
445445
projectId: asProjectId("project-1"),
446446
title: "Provider Project",
447-
workspaceRoot: input?.workspaceRoot ?? "/tmp/provider-project",
447+
workspaceRoot: input?.workspaceRoot ?? workspaceRoot,
448448
defaultModelSelection: modelSelection,
449449
createdAt: now,
450450
}),
@@ -552,7 +552,7 @@ describe("ProviderCommandReactor", () => {
552552
await waitFor(() => harness.sendTurn.mock.calls.length === 1);
553553
expect(harness.startSession.mock.calls[0]?.[0]).toEqual(ThreadId.make("thread-1"));
554554
expect(harness.startSession.mock.calls[0]?.[1]).toMatchObject({
555-
cwd: "/tmp/provider-project",
555+
cwd: workspaceRoot,
556556
modelSelection: {
557557
instanceId: ProviderInstanceId.make("codex"),
558558
model: "gpt-5-codex",
@@ -680,7 +680,7 @@ describe("ProviderCommandReactor", () => {
680680
effectIt.effect("rejects a turn when the workspace directory no longer exists", () =>
681681
Effect.gen(function* () {
682682
const harness = yield* Effect.promise(() =>
683-
createHarness({ workspaceRoot: "/tmp/provider-project-missing" }),
683+
createHarness({ workspaceRoot: "/tmp/t3code-definitely-missing-" + process.pid }),
684684
);
685685
const now = "2026-01-01T00:00:00.000Z";
686686

@@ -828,7 +828,7 @@ describe("ProviderCommandReactor", () => {
828828

829829
expect(harness.generateThreadTitle).toHaveBeenCalledTimes(1);
830830
expect(harness.generateThreadTitle.mock.calls[0]?.[0]).toMatchObject({
831-
cwd: "/tmp/provider-project",
831+
cwd: workspaceRoot,
832832
previousTitle: "Investigate reconnect regressions",
833833
message: [
834834
"USER:",
@@ -1519,7 +1519,7 @@ describe("ProviderCommandReactor", () => {
15191519
commandId: CommandId.make("cmd-thread-branch"),
15201520
threadId: ThreadId.make("thread-1"),
15211521
branch: "t3code/1234abcd",
1522-
worktreePath: "/tmp/provider-project-worktree",
1522+
worktreePath: workspaceWorktree,
15231523
}),
15241524
);
15251525

@@ -1560,7 +1560,7 @@ describe("ProviderCommandReactor", () => {
15601560
expect(harness.generateBranchName.mock.calls[0]?.[0]).toMatchObject({
15611561
message: "Add a safer reconnect backoff.",
15621562
});
1563-
expect(harness.refreshStatus.mock.calls[0]?.[0]).toBe("/tmp/provider-project-worktree");
1563+
expect(harness.refreshStatus.mock.calls[0]?.[0]).toBe(workspaceWorktree);
15641564
});
15651565

15661566
it("forwards codex model options through session start and turn send", async () => {
@@ -2053,15 +2053,15 @@ describe("ProviderCommandReactor", () => {
20532053
await waitFor(() => harness.startSession.mock.calls.length === 1);
20542054
await waitFor(() => harness.sendTurn.mock.calls.length === 1);
20552055
expect(harness.startSession.mock.calls[0]?.[1]).toMatchObject({
2056-
cwd: "/tmp/provider-project",
2056+
cwd: workspaceRoot,
20572057
});
20582058

20592059
await Effect.runPromise(
20602060
harness.engine.dispatch({
20612061
type: "thread.meta.update",
20622062
commandId: CommandId.make("cmd-thread-worktree-change"),
20632063
threadId: ThreadId.make("thread-1"),
2064-
worktreePath: "/tmp/provider-project-worktree",
2064+
worktreePath: workspaceWorktree,
20652065
}),
20662066
);
20672067

@@ -2087,7 +2087,7 @@ describe("ProviderCommandReactor", () => {
20872087
expect(harness.stopSession.mock.calls.length).toBe(0);
20882088
expect(harness.startSession.mock.calls[1]?.[1]).toMatchObject({
20892089
threadId: ThreadId.make("thread-1"),
2090-
cwd: "/tmp/provider-project-worktree",
2090+
cwd: workspaceWorktree,
20912091
resumeCursor: { opaque: "resume-1" },
20922092
modelSelection: {
20932093
instanceId: ProviderInstanceId.make("claudeAgent"),
@@ -2620,7 +2620,7 @@ describe("ProviderCommandReactor", () => {
26202620
status: "ready",
26212621
runtimeMode: "approval-required",
26222622
threadId: ThreadId.make("thread-1"),
2623-
cwd: "/tmp/provider-project",
2623+
cwd: workspaceRoot,
26242624
resumeCursor: { opaque: "resume-without-instance" },
26252625
createdAt: now,
26262626
updatedAt: now,

‎apps/server/src/orchestration/Layers/ProviderCommandReactor.ts‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -608,12 +608,27 @@ const make = Effect.gen(function* () {
608608

609609
if (effectiveCwd) {
610610
const fs = yield* FileSystem.FileSystem;
611-
const exists = yield* fs.exists(effectiveCwd).pipe(Effect.orElseSucceed(() => false));
612-
if (!exists) {
611+
const stat = yield* fs.stat(effectiveCwd).pipe(
612+
Effect.catchTags({
613+
PlatformError: (cause) =>
614+
cause.reason._tag === "NotFound"
615+
? Effect.succeed(null)
616+
: new ProviderAdapterRequestError({
617+
provider: preferredProvider,
618+
method: "thread.turn.start",
619+
detail: `Could not access workspace directory '${effectiveCwd}': ${cause.reason._tag}`,
620+
cause,
621+
}),
622+
}),
623+
);
624+
if (stat === null || stat.type !== "Directory") {
625+
const isWorktree = thread.worktreePath != null;
613626
return yield* new ProviderAdapterRequestError({
614627
provider: preferredProvider,
615628
method: "thread.turn.start",
616-
detail: `Workspace directory '${effectiveCwd}' no longer exists. Has it been moved or renamed? Update the project's workspace root in Settings to point at the new location.`,
629+
detail: isWorktree
630+
? `Worktree directory '${effectiveCwd}' no longer exists. It may have been cleaned up — start a new thread or recreate the worktree to continue.`
631+
: `Workspace directory '${effectiveCwd}' no longer exists. Has it been moved or renamed? Update the project's workspace root in Settings to point at the new location.`,
617632
});
618633
}
619634
}

0 commit comments

Comments
 (0)