Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
fix(web): retry failed thread bootstraps with a fresh id (#7664)
Dara Adedeji8824f8f24f8b
diff --git a/apps/server/src/server.test.ts b/apps/server/src/server.test.tsindex 89f903c4f..7cda53f25 100644--- a/apps/server/src/server.test.ts+++ b/apps/server/src/server.test.ts@@ -7893,6 +7893,89 @@ it.layer(NodeServices.layer)("server router seam", (it) => {assertTrue(result._tag === "Failure");assertTrue(result.failure._tag === "OrchestrationDispatchCommandError");assert.include(result.failure.message, "worktree exploded");+ assert.strictEqual(result.failure.bootstrapThreadDisposition, "deleted");+ assert.deepEqual(+ dispatchedCommands.map((command) => command.type),+ ["thread.create", "thread.delete"],+ );+ }).pipe(Effect.provide(NodeHttpServer.layerTest)),+ );++ it.effect("does not report a deleted bootstrap thread when cleanup fails", () =>+ Effect.gen(function* () {+ const dispatchedCommands: Array<OrchestrationCommand> = [];+ const createWorktree = vi.fn(+ (_: Parameters<GitVcsDriver.GitVcsDriver["Service"]["createWorktree"]>[0]) =>+ Effect.die(new Error("worktree exploded")),+ );++ yield* buildAppUnderTest({+ layers: {+ gitVcsDriver: {+ createWorktree,+ },+ orchestrationEngine: {+ dispatch: (command) => {+ dispatchedCommands.push(command);+ if (command.type === "thread.delete") {+ return Effect.fail(+ new OrchestrationListenerCallbackError({+ listener: "domain-event",+ detail: "thread cleanup exploded",+ }),+ );+ }+ return Effect.succeed({ sequence: dispatchedCommands.length });+ },+ readEvents: () => Stream.empty,+ },+ },+ });++ const createdAt = "2026-01-01T00:00:00.000Z";+ const wsUrl = yield* getWsServerUrl("/ws");+ const result = yield* Effect.scoped(+ withWsRpcClient(wsUrl, (client) =>+ client[ORCHESTRATION_WS_METHODS.dispatchCommand]({+ type: "thread.turn.start",+ commandId: CommandId.make("cmd-bootstrap-turn-start-cleanup-defect"),+ threadId: ThreadId.make("thread-bootstrap-cleanup-defect"),+ message: {+ messageId: MessageId.make("msg-bootstrap-cleanup-defect"),+ role: "user",+ text: "hello",+ attachments: [],+ },+ modelSelection: defaultModelSelection,+ runtimeMode: "full-access",+ interactionMode: "default",+ bootstrap: {+ createThread: {+ projectId: defaultProjectId,+ title: "Bootstrap Thread",+ modelSelection: defaultModelSelection,+ runtimeMode: "full-access",+ interactionMode: "default",+ branch: "main",+ worktreePath: null,+ createdAt,+ },+ prepareWorktree: {+ projectCwd: "/tmp/project",+ baseBranch: "main",+ branch: "t3code/bootstrap-refName",+ },+ runSetupScript: false,+ },+ createdAt,+ }),+ ).pipe(Effect.result),+ );++ assertTrue(result._tag === "Failure");+ assertTrue(result.failure._tag === "OrchestrationDispatchCommandError");+ assert.include(result.failure.message, "worktree exploded");+ assert.strictEqual(result.failure.bootstrapThreadDisposition, undefined);assert.deepEqual(dispatchedCommands.map((command) => command.type),["thread.create", "thread.delete"],diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.tsindex ebcf65e4b..c5b7e50a8 100644--- a/apps/server/src/ws.ts+++ b/apps/server/src/ws.ts@@ -775,9 +775,9 @@ const makeWsRpcLayer = (threadId: command.threadId,}),),- Effect.ignoreCause({ log: true }),+ Effect.as(true),)- : Effect.void;+ : Effect.succeed(false);const recordSetupScriptLaunchFailure = (input: {readonly error: ProjectSetupScriptRunner.ProjectSetupScriptRunnerError;@@ -964,7 +964,27 @@ const makeWsRpcLayer = (if (Cause.hasInterruptsOnly(cause)) {return Effect.fail(dispatchError);}- return cleanupCreatedThread().pipe(Effect.flatMap(() => Effect.fail(dispatchError)));+ return Effect.uninterruptible(cleanupCreatedThread()).pipe(+ Effect.matchCauseEffect({+ onFailure: (cleanupCause) =>+ Effect.logWarning("bootstrap thread cleanup failed", {+ threadId: command.threadId,+ detail: Cause.pretty(cleanupCause),+ }).pipe(Effect.flatMap(() => Effect.fail(dispatchError))),+ onSuccess: (threadDeleted) =>+ Effect.fail(+ threadDeleted+ ? new OrchestrationDispatchCommandError({+ message: dispatchError.message,+ ...(dispatchError.cause !== undefined+ ? { cause: dispatchError.cause }+ : {}),+ bootstrapThreadDisposition: "deleted",+ })+ : dispatchError,+ ),+ }),+ );}),);});diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsxindex ec32db182..a84433749 100644--- a/apps/web/src/components/ChatView.tsx+++ b/apps/web/src/components/ChatView.tsx@@ -26,6 +26,7 @@ import {connectionStatusTitle,type EnvironmentConnectionPresentation,} from "@t3tools/client-runtime/connection";+import { wasBootstrapThreadDeleted } from "@t3tools/client-runtime/errors";import {changeRequestAutoSettles,effectiveSettled,@@ -5454,6 +5455,20 @@ function ChatViewContent(props: ChatViewProps) {}if (!isAtomCommandInterrupted(failure)) {const error = squashAtomCommandFailure(failure);+ if (isLocalDraftThread && draftId && wasBootstrapThreadDeleted(error)) {+ const failedDraftSession = getDraftSession(draftId);+ if (failedDraftSession?.threadId === threadIdForSend) {+ setLogicalProjectDraftThreadId(+ failedDraftSession.logicalProjectKey,+ scopeProjectRef(failedDraftSession.environmentId, failedDraftSession.projectId),+ draftId,+ {+ threadId: newThreadId(),+ createdAt: new Date().toISOString(),+ },+ );+ }+ }setThreadError(threadIdForSend,error instanceof Error ? error.message : "Failed to send message.",diff --git a/apps/web/src/composerDraftStore.test.ts b/apps/web/src/composerDraftStore.test.tsindex 3e4106c58..20c6603f7 100644--- a/apps/web/src/composerDraftStore.test.ts+++ b/apps/web/src/composerDraftStore.test.ts@@ -823,6 +823,43 @@ describe("composerDraftStore project draft thread mapping", () => {});});+ it("rotates a failed bootstrap thread id without losing its draft", () => {+ const store = useComposerDraftStore.getState();+ const retryThreadId = ThreadId.make("thread-retry");+ store.setProjectDraftThreadId(projectRef, draftId, {+ threadId,+ branch: "feature/test",+ worktreePath: null,+ createdAt: "2026-01-01T00:00:00.000Z",+ envMode: "worktree",+ startFromOrigin: true,+ runtimeMode: "approval-required",+ interactionMode: "plan",+ });+ store.setPrompt(draftId, "keep this prompt");+ markPromotedDraftThread(threadId);++ store.setLogicalProjectDraftThreadId(scopedProjectKey(projectRef), projectRef, draftId, {+ threadId: retryThreadId,+ createdAt: "2026-01-01T00:01:00.000Z",+ });++ expect(useComposerDraftStore.getState().getDraftThread(draftId)).toMatchObject({+ threadId: retryThreadId,+ branch: "feature/test",+ worktreePath: null,+ createdAt: "2026-01-01T00:01:00.000Z",+ envMode: "worktree",+ startFromOrigin: true,+ runtimeMode: "approval-required",+ interactionMode: "plan",+ promotedTo: null,+ });+ expect(useComposerDraftStore.getState().getComposerDraft(draftId)?.prompt).toBe(+ "keep this prompt",+ );+ });+it("clears only matching project draft mapping entries", () => {const store = useComposerDraftStore.getState();store.setProjectDraftThreadId(projectRef, draftId, { threadId });diff --git a/packages/client-runtime/src/errors/index.ts b/packages/client-runtime/src/errors/index.tsindex 7eb6244e5..6c90b3aea 100644--- a/packages/client-runtime/src/errors/index.ts+++ b/packages/client-runtime/src/errors/index.ts@@ -1,3 +1,4 @@export * from "./errorTrace.ts";+export * from "./orchestration.ts";export * from "./safeLog.ts";export * from "./transport.ts";diff --git a/packages/client-runtime/src/errors/orchestration.test.ts b/packages/client-runtime/src/errors/orchestration.test.tsnew file mode 100644index 000000000..0e1a723d5--- /dev/null+++ b/packages/client-runtime/src/errors/orchestration.test.ts@@ -0,0 +1,23 @@+import { OrchestrationDispatchCommandError } from "@t3tools/contracts";+import { describe, expect, it } from "vite-plus/test";++import { wasBootstrapThreadDeleted } from "./orchestration.ts";++describe("wasBootstrapThreadDeleted", () => {+ it("accepts only a confirmed deleted bootstrap thread", () => {+ expect(+ wasBootstrapThreadDeleted(+ new OrchestrationDispatchCommandError({+ message: "Failed to create worktree.",+ bootstrapThreadDisposition: "deleted",+ }),+ ),+ ).toBe(true);+ expect(+ wasBootstrapThreadDeleted(+ new OrchestrationDispatchCommandError({ message: "Failed to create worktree." }),+ ),+ ).toBe(false);+ expect(wasBootstrapThreadDeleted(new Error("connection lost"))).toBe(false);+ });+});diff --git a/packages/client-runtime/src/errors/orchestration.ts b/packages/client-runtime/src/errors/orchestration.tsnew file mode 100644index 000000000..39ef26e46--- /dev/null+++ b/packages/client-runtime/src/errors/orchestration.ts@@ -0,0 +1,10 @@+import { OrchestrationDispatchCommandError } from "@t3tools/contracts";+import * as Schema from "effect/Schema";++const isOrchestrationDispatchCommandError = Schema.is(OrchestrationDispatchCommandError);++export function wasBootstrapThreadDeleted(error: unknown): boolean {+ return (+ isOrchestrationDispatchCommandError(error) && error.bootstrapThreadDisposition === "deleted"+ );+}diff --git a/packages/contracts/src/orchestration.test.ts b/packages/contracts/src/orchestration.test.tsindex f403e6de2..3e1b9be0b 100644--- a/packages/contracts/src/orchestration.test.ts+++ b/packages/contracts/src/orchestration.test.ts@@ -7,6 +7,7 @@ import {DEFAULT_RUNTIME_MODE,ModelSelection,OrchestrationCommand,+ OrchestrationDispatchCommandError,OrchestrationEvent,OrchestrationGetFullThreadDiffInput,OrchestrationGetTurnDiffInput,@@ -54,6 +55,19 @@ const decodeThreadCreatedPayload = Schema.decodeUnknownEffect(ThreadCreatedPayloconst decodeOrchestrationCommand = Schema.decodeUnknownEffect(OrchestrationCommand);const decodeOrchestrationEvent = Schema.decodeUnknownEffect(OrchestrationEvent);const decodeThreadMetaUpdatedPayload = Schema.decodeUnknownEffect(ThreadMetaUpdatedPayload);+const decodeDispatchCommandError = Schema.decodeUnknownEffect(OrchestrationDispatchCommandError);++it.effect("decodes a dispatch error after its bootstrap thread was deleted", () =>+ Effect.gen(function* () {+ const error = yield* decodeDispatchCommandError({+ _tag: "OrchestrationDispatchCommandError",+ message: "Failed to create worktree.",+ bootstrapThreadDisposition: "deleted",+ });++ assert.strictEqual(error.bootstrapThreadDisposition, "deleted");+ }),+);it.effect("parses turn diff input when fromTurnCount <= toTurnCount", () =>Effect.gen(function* () {diff --git a/packages/contracts/src/orchestration.ts b/packages/contracts/src/orchestration.tsindex cd9f3a747..c7c63270c 100644--- a/packages/contracts/src/orchestration.ts+++ b/packages/contracts/src/orchestration.ts@@ -1711,6 +1711,7 @@ export class OrchestrationDispatchCommandError extends Schema.TaggedErrorClass<O{message: TrimmedNonEmptyString,cause: Schema.optional(Schema.Defect()),+ bootstrapThreadDisposition: Schema.optional(Schema.Literal("deleted")),},) {}