import * as DocumentText from "../provider/DocumentText.ts"; import * as Scheduler from "../scheduling/Scheduler.ts"; import * as WorktreeSetupTracker from "../project/WorktreeSetupTracker.ts"; import * as ProjectCloneTracker from "../project/ProjectCloneTracker.ts"; import * as TerminalManager from "../terminal/Manager.ts"; import * as NodeServices from "@effect/platform-node/NodeServices"; import * as FileSystem from "effect/FileSystem"; import * as ServerConfig from "../config.ts"; import { createPendingAttachmentId, resolveAttachmentPath } from "../attachmentStore.ts"; import * as ThreadMessageIntake from "./ThreadMessageIntake.ts"; import { assert, it, vi } from "@effect/vitest"; import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; import { ChatAttachmentId, ComposerContextId, type ChatAttachment, CommandId, DEFAULT_SERVER_SETTINGS, GitCommandError, MessageId, ProjectId, ProviderDriverKind, ProviderInstanceId, OrchestrationV2ThreadProjectionJson, ScheduledTaskId, type ServerProvider, ThreadId, TextGenerationError, } from "@t3tools/contracts"; import * as Cause from "effect/Cause"; import * as Deferred from "effect/Deferred"; import * as DateTime from "effect/DateTime"; import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as Ref from "effect/Ref"; import * as Result from "effect/Result"; import * as Stream from "effect/Stream"; import * as Schema from "effect/Schema"; import * as SqlClient from "effect/sql/SqlClient"; import * as TestClock from "effect/testing/TestClock"; import * as GitWorkflow from "../git/GitWorkflowService.ts"; import * as SqlitePersistence from "../persistence/Sqlite.ts"; import * as ProjectStore from "./ProjectStore.ts"; import * as ProjectService from "../project/ProjectService.ts"; import * as ProjectSetupScriptRunner from "../project/ProjectSetupScriptRunner.ts"; import * as ManagedProjectFolders from "../project/ManagedProjectFolders.ts"; import * as ProviderRegistryMock from "../provider/testUtils/providerRegistryMock.ts"; import * as ServerSettings from "../serverSettings.ts"; import * as ScheduledTasks from "../scheduledTasks/ScheduledTaskService.ts"; import * as ManagedPolicy from "../policy/ManagedPolicy.ts"; import * as SecretRequests from "../secrets/SecretRequests.ts"; import * as TextGeneration from "../textGeneration/TextGeneration.ts"; import { ClaudeProviderCapabilitiesV2 } from "./Adapters/ClaudeAdapterV2.ts"; import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts"; import * as CommandReceiptStore from "./CommandReceiptStore.ts"; import * as EffectOutbox from "./EffectOutbox.ts"; import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts"; import * as ThreadLaunch from "./ThreadLaunchService.ts"; import * as GitVcsDriver from "../vcs/GitVcsDriver.ts"; import * as ThreadManagement from "./ThreadManagementService.ts"; import * as ThreadTitleRegeneration from "./ThreadTitleRegenerationService.ts"; import * as ProviderReplayHarness from "./testkit/ProviderReplayHarness.ts"; import * as RemoteDelegation from "../peer/RemoteDelegation.ts"; import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"; const projectId = ProjectId.make("project:launch-test"); const otherProjectId = ProjectId.make("project:launch-other"); const encodeThreadProjection = Schema.encodeEffect(OrchestrationV2ThreadProjectionJson); const modelSelection = { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.1-codex", } as const; const project = { id: projectId, title: "Project", workspaceRoot: "/repo", repositoryIdentity: null, faviconPath: null, defaultModelSelection: modelSelection, defaultThreadEnvMode: null, scripts: [], pinnedAt: null, archivedAt: null, createdAt: "2026-06-20T00:00:00.000Z", updatedAt: "2026-06-20T00:00:00.000Z", deletedAt: null, } as const; const otherProject = { ...project, id: otherProjectId, title: "Other", workspaceRoot: "/other-repo", } as const; const adapter = { instanceId: modelSelection.instanceId, driver: ProviderDriverKind.make("codex"), getCapabilities: () => Effect.succeed(CodexProviderCapabilitiesV2), planSelectionTransition: () => Effect.succeed({ type: "apply_on_next_turn" as const }), openSession: () => Effect.die("provider execution is disabled in launch tests"), } as ProviderAdapter.ProviderAdapterV2["Service"]; const claudeModelSelection = { instanceId: ProviderInstanceId.make("claudeAgent"), model: "claude-opus-5-5", } as const; const claudeAdapter = { instanceId: claudeModelSelection.instanceId, driver: ProviderDriverKind.make("claudeAgent"), getCapabilities: () => Effect.succeed(ClaudeProviderCapabilitiesV2), planSelectionTransition: () => Effect.succeed({ type: "apply_on_next_turn" as const }), openSession: () => Effect.die("provider execution is disabled in launch tests"), } as ProviderAdapter.ProviderAdapterV2["Service"]; interface HarnessOptions { readonly closeTerminal?: TerminalManager.TerminalManager["Service"]["close"]; readonly adapters?: ReadonlyArray; readonly managedFolders?: Layer.Layer; readonly createWorktree?: GitWorkflow.GitWorkflowService["Service"]["createWorktree"]; readonly fetchWorktreeBaseCommit?: GitWorkflow.GitWorkflowService["Service"]["fetchWorktreeBaseCommit"]; readonly remoteExists?: GitWorkflow.GitWorkflowService["Service"]["remoteExists"]; readonly hasCommit?: GitWorkflow.GitWorkflowService["Service"]["hasCommit"]; readonly renameBranch?: GitWorkflow.GitWorkflowService["Service"]["renameBranch"]; readonly runSetup?: ProjectSetupScriptRunner.ProjectSetupScriptRunner["Service"]["runForThread"]; readonly generateTitle?: TextGeneration.TextGeneration["Service"]["generateThreadTitle"]; readonly generateBranchName?: TextGeneration.TextGeneration["Service"]["generateBranchName"]; readonly serverSettings?: Parameters[0]; readonly providers?: ReadonlyArray; readonly git?: Partial; } function makeHarness(options: HarnessOptions = {}) { const layerDatabase = SqlitePersistence.layerMemory; const layerRegistry = ProviderAdapterRegistry.layerFromAdapters(options.adapters ?? [adapter]); const layerOrchestrator = ProviderReplayHarness.layerWithRegistry( { name: "thread-launch" }, layerRegistry, { databaseLayer: layerDatabase, runEffectWorker: false }, ); const layerServerSettings = ServerSettings.layerTest(options.serverSettings); const layerReceipts = CommandReceiptStore.layer.pipe(Layer.provide(layerDatabase)); const layerThreadManagement = ThreadManagement.layer.pipe( Layer.provide(layerOrchestrator), Layer.provide(layerServerSettings), Layer.provide(layerReceipts), ); const layerOutbox = EffectOutbox.layer.pipe(Layer.provide(layerDatabase)); const createWorktree = vi.fn( options.createWorktree ?? ((input) => Effect.succeed({ worktree: { path: "/repo-worktrees/feature", refName: input.newRefName, headSha: "abc" }, } as never)), ); const renameBranch = vi.fn( options.renameBranch ?? ((input) => Effect.succeed({ branch: input.newBranch })), ); const removeWorktree = vi.fn( (_input: Parameters[0]) => Effect.void, ); const deleteLocalBranch = vi.fn( (_input: Parameters[0]) => Effect.void, ); const runSetup = vi.fn( options.runSetup ?? (() => Effect.succeed({ status: "no-script" as const })), ); const closeTerminal = vi.fn(options.closeTerminal ?? (() => Effect.void)); const generateBranchName = vi.fn( options.generateBranchName ?? (() => Effect.succeed({ branch: "generated-branch" })), ); const generateThreadTitle = vi.fn( options.generateTitle ?? (() => Effect.succeed({ title: "Generated title" })), ); const layerExternalServices = Layer.mergeAll( WorktreeSetupTracker.layer, Layer.mock(ProjectCloneTracker.ProjectCloneTracker)({ get: () => Effect.succeed(null) }), Layer.mock(TerminalManager.TerminalManager)({ close: closeTerminal }), Layer.succeed(ProjectService.ProjectService, { create: () => Effect.die("unused"), bootstrap: () => Effect.die("unused"), update: () => Effect.die("unused"), delete: () => Effect.die("unused"), listPage: () => Effect.die("unused"), getById: (id) => Effect.succeed( id === projectId ? Option.some(project) : id === otherProjectId ? Option.some(otherProject) : Option.none(), ), getByWorkspaceRoot: () => Effect.succeedSome(project), workspaceScanDirectory: () => Effect.die("unused"), snapshot: Effect.die("unused"), getShell: () => Effect.die("unused"), listShells: () => Effect.die("unused"), }), Layer.mock(GitWorkflow.GitWorkflowService)({ repositoryRoot: () => Effect.succeed(project.workspaceRoot), createWorktree, renameBranch, fetchWorktreeBaseCommit: options.fetchWorktreeBaseCommit ?? (() => Effect.succeed({ commitSha: "remote-main-sha", remoteRefName: "origin/main" })), hasCommit: options.hasCommit ?? (() => Effect.succeed(false)), currentBranch: () => Effect.succeed(null), resolveWorktreeBaseRemote: () => Effect.succeed({ remoteName: "origin", branchName: "main" }), remoteExists: options.remoteExists ?? (() => Effect.succeed(true)), remoteBranchExists: () => Effect.succeed(true), listLocalBranchNames: () => Effect.succeed([]), removeWorktree, deleteLocalBranch, ...options.git, }), Layer.succeed(ProjectSetupScriptRunner.ProjectSetupScriptRunner, { runForThread: runSetup, }), Layer.mock(TextGeneration.TextGeneration)({ generateThreadTitle, generateBranchName, }), layerServerSettings, ProviderRegistryMock.layer(options.providers), options.managedFolders ?? Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({ namedProjectsRoot: "/projects", isScratchProject: () => Effect.succeed(false), folderForThread: () => Effect.succeedNone, }), ); const layerLaunch = ThreadLaunch.layer.pipe( Layer.provide( Layer.mergeAll( layerExternalServices, layerThreadManagement, layerReceipts, layerOrchestrator, IdAllocator.layer, ), ), ); const layerProjectedProjects = Layer.mock(ProjectStore.ProjectStoreV2)({ get: (requestedProjectId) => Effect.succeed( requestedProjectId === projectId ? Option.some({ projectId, title: project.title, workspaceRoot: project.workspaceRoot, defaultModelSelection: project.defaultModelSelection, defaultThreadEnvMode: null, autoPull: false, faviconPath: null, projectIcon: null, scripts: project.scripts, pinnedAt: null, archivedAt: null, createdAt: project.createdAt, updatedAt: project.updatedAt, deletedAt: project.deletedAt, }) : Option.none(), ), }); const layerTitleRegeneration = ThreadTitleRegeneration.layer.pipe( Layer.provide( Layer.mergeAll( layerThreadManagement, layerProjectedProjects, layerExternalServices, NodeServices.layer, ), ), ); return { layer: Layer.mergeAll( layerLaunch, layerRegistry, layerThreadManagement, layerTitleRegeneration, layerOutbox, layerDatabase, layerExternalServices, ), createWorktree, removeWorktree, deleteLocalBranch, renameBranch, generateBranchName, generateThreadTitle, runSetup, closeTerminal, }; } function launchInput(input: { readonly command: string; readonly thread: string; readonly message?: string; readonly workspace?: ThreadLaunch.ThreadLaunchWorkspaceStrategy; }) { return { commandId: CommandId.make(input.command), threadId: ThreadId.make(input.thread), projectId, title: "New thread", modelSelection, runtimeMode: "full-access" as const, interactionMode: "default" as const, workspaceStrategy: input.workspace ?? { type: "root" as const }, ...(input.message === undefined ? {} : { initialMessage: { messageId: MessageId.make(`${input.message}:id`), text: input.message, attachments: [], }, }), createdBy: "user" as const, creationSource: "web" as const, }; } it.effect.each( (["new", "existing"] as const).flatMap((target) => (["user", "agent"] as const).map((createdBy) => ({ target, createdBy })), ), )( "attributes $createdBy-configured automations in $target threads without changing their prompt", ({ target, createdBy }) => { const harness = makeHarness(); const layerScheduledTasks = ScheduledTasks.layer.pipe( Layer.provide( Layer.mergeAll( harness.layer, NodeCrypto.layer, Scheduler.layer, Layer.mock(SecretRequests.SecretRequests)({}), ManagedPolicy.layerTest(), Layer.mock(RemoteDelegation.RemoteDelegation)({}), ), ), ); return Effect.gen(function* () { const tasks = yield* ScheduledTasks.ScheduledTaskService; const launches = yield* ThreadLaunch.ThreadLaunchService; const threads = yield* ThreadManagement.ThreadManagementService; const existing = target === "existing" ? yield* launches.launch( launchInput({ command: "command:existing", thread: "thread:existing" }), ) : null; const { task } = yield* tasks.upsert({ id: ScheduledTaskId.make("scheduled-task:attribution"), title: "Daily audit", prompt: "Audit performance and crashes.", enabled: false, schedule: { type: "interval", everyMs: 60_000 }, projectId, threadId: existing?.threadId ?? null, workspaceStrategy: { type: "root" }, modelSelection, runtimeMode: "full-access", interactionMode: "default", createdBy, creationSource: createdBy === "agent" ? "mcp" : "web", }); const result = yield* tasks.runNow({ id: task.id }); assert.equal(result.task.lastRunStatus, "succeeded"); const projectThreads = yield* threads.listProjectThreads({ projectId, includeSubagents: false, }); const thread = projectThreads.find((candidate) => candidate.id === existing?.threadId) ?? projectThreads[0]; assert.isDefined(thread); assert.equal(result.threadId, thread!.id); const projection = yield* threads.getThreadProjection(thread!.id); const wire = yield* encodeThreadProjection(projection); assert.equal(wire.messages[0]?.text, task.prompt); assert.equal(wire.messages[0]?.scheduledTaskId, task.id); assert.equal(wire.messages[0]?.createdBy, createdBy); const turnItem = wire.turnItems.find( (item): item is Extract => item.type === "user_message", ); assert.equal(turnItem?.text, task.prompt); assert.equal(turnItem?.scheduledTaskId, task.id); }).pipe(Effect.provide(Layer.mergeAll(harness.layer, layerScheduledTasks))); }, ); it.effect( "returns no dispatched thread when sending an automation to a missing thread fails", () => { const harness = makeHarness(); const layerScheduledTasks = ScheduledTasks.layer.pipe( Layer.provide( Layer.mergeAll( harness.layer, NodeCrypto.layer, Scheduler.layer, Layer.mock(SecretRequests.SecretRequests)({}), ManagedPolicy.layerTest(), ), ), ); return Effect.gen(function* () { const tasks = yield* ScheduledTasks.ScheduledTaskService; const launches = yield* ThreadLaunch.ThreadLaunchService; const threads = yield* ThreadManagement.ThreadManagementService; const { threadId } = yield* launches.launch( launchInput({ command: "command:missing", thread: "thread:missing" }), ); const { task } = yield* tasks.upsert({ title: "Daily audit", prompt: "Audit performance and crashes.", enabled: false, schedule: { type: "interval", everyMs: 60_000 }, projectId, threadId, workspaceStrategy: { type: "root" }, modelSelection, runtimeMode: "full-access", interactionMode: "default", createdBy: "user", creationSource: "web", }); yield* threads.dispatch({ type: "thread.delete", commandId: CommandId.make("command:missing:delete"), threadId, }); const result = yield* tasks.runNow({ id: task.id }); assert.equal(result.task.lastRunStatus, "failed"); assert.isNull(result.threadId); assert.equal(result.task.threadId, threadId); }).pipe(Effect.provide(Layer.mergeAll(harness.layer, layerScheduledTasks))); }, ); it.effect("runs a thread-bound automation on the provider the thread uses now", () => { const harness = makeHarness({ adapters: [adapter, claudeAdapter] }); const layerScheduledTasks = ScheduledTasks.layer.pipe( Layer.provide( Layer.mergeAll( harness.layer, NodeCrypto.layer, Scheduler.layer, Layer.mock(SecretRequests.SecretRequests)({}), ManagedPolicy.layerTest(), ), ), ); return Effect.gen(function* () { const tasks = yield* ScheduledTasks.ScheduledTaskService; const launches = yield* ThreadLaunch.ThreadLaunchService; const threads = yield* ThreadManagement.ThreadManagementService; const launched = yield* launches.launch( launchInput({ command: "command:switched", thread: "thread:switched" }), ); const { task } = yield* tasks.upsert({ id: ScheduledTaskId.make("scheduled-task:switched"), title: "Hourly check", prompt: "Check on the delegated workers.", enabled: false, schedule: { type: "interval", everyMs: 3_600_000 }, projectId, threadId: launched.threadId, workspaceStrategy: { type: "root" }, modelSelection, runtimeMode: "full-access", interactionMode: "default", createdBy: "agent", creationSource: "mcp", }); yield* threads.dispatch({ type: "thread.model-selection.set", commandId: CommandId.make("command:switch-selection"), threadId: launched.threadId, modelSelection: claudeModelSelection, }); const result = yield* tasks.runNow({ id: task.id }); assert.equal(result.task.lastRunStatus, "succeeded"); const projection = yield* threads.getThreadProjection(launched.threadId); const message = projection.messages.find((item) => item.scheduledTaskId === task.id); const run = projection.runs.find((item) => item.id === message?.runId); assert.deepStrictEqual(run?.modelSelection, claudeModelSelection); assert.deepStrictEqual(projection.thread.modelSelection, claudeModelSelection); }).pipe(Effect.provide(Layer.mergeAll(harness.layer, layerScheduledTasks))); }); it.effect("retains automation and sender attribution while a message waits in the queue", () => { const harness = makeHarness({ runSetup: () => Effect.never }); return Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const threads = yield* ThreadManagement.ThreadManagementService; const launched = yield* launches.launch( launchInput({ command: "command:automation:queue", thread: "thread:automation:queue", message: "First message", workspace: { type: "worktree", baseRef: "main" }, }), ); const scheduledTaskId = ScheduledTaskId.make("scheduled-task:queued"); const senderThreadId = ThreadId.make("thread:agent-sender"); const queued = yield* threads.sendToThread({ projectId, commandId: CommandId.make("command:automation:queued"), threadId: launched.threadId, messageId: MessageId.make("message:automation:queued"), scheduledTaskId, senderThreadId, text: "Run the audit", attachments: [], mode: "queue", createdBy: "agent", creationSource: "mcp", }); assert.equal(queued.delivery, "queued"); const projection = yield* threads.getThreadProjection(launched.threadId); const message = projection.messages.find((item) => item.id === queued.message.id); assert.equal(message?.scheduledTaskId, scheduledTaskId); assert.equal(message?.senderThreadId, senderThreadId); assert.equal(message?.text, "Run the audit"); // A fresh send queues instead of steering, so it needs no steerable run. const fresh = yield* threads.sendToThread({ projectId, commandId: CommandId.make("command:automation:queued-fresh"), threadId: launched.threadId, messageId: MessageId.make("message:automation:queued-fresh"), text: "Start over", attachments: [], mode: "steer", freshContext: true, createdBy: "agent", creationSource: "mcp", }); assert.equal(fresh.delivery, "queued"); assert.isTrue(fresh.run.freshContext); }).pipe(Effect.provide(harness.layer)); }); it.effect("provisions independent launches concurrently instead of behind a global semaphore", () => Effect.gen(function* () { const setupCount = yield* Ref.make(0); const bothEntered = yield* Deferred.make(); const allowSetup = yield* Deferred.make(); const harness = makeHarness({ runSetup: () => Ref.updateAndGet(setupCount, (count) => count + 1).pipe( Effect.tap((count) => count === 2 ? Deferred.succeed(bothEntered, undefined) : Effect.void, ), Effect.andThen(Deferred.await(allowSetup)), Effect.as({ status: "no-script" as const }), ), }); yield* Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const results = yield* Effect.all( [ launches.launch( launchInput({ command: "command:launch:concurrent-a", thread: "thread:launch:concurrent-a", message: "First", workspace: { type: "worktree", baseRef: "main" }, }), ), launches.launch( launchInput({ command: "command:launch:concurrent-b", thread: "thread:launch:concurrent-b", message: "Second", workspace: { type: "worktree", baseRef: "main" }, }), ), ], { concurrency: "unbounded" }, ); assert.deepEqual( results.map((result) => result.projection.runs[0]?.status), ["preparing", "preparing"], ); yield* Deferred.await(bothEntered); assert.equal(yield* Ref.get(setupCount), 2); yield* Deferred.succeed(allowSetup, undefined); }).pipe(Effect.provide(harness.layer)); }), ); it.effect.each([" /COMPACT "])( "uses the first conversation message for a title after %s", (nativeCommand) => Effect.gen(function* () { const harness = makeHarness(); yield* Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const threads = yield* ThreadManagement.ThreadManagementService; const outbox = yield* EffectOutbox.EffectOutboxV2; const launched = yield* launches.launch({ ...launchInput({ command: "compact-title", thread: "compact-title-thread", message: nativeCommand, }), generateTitle: true, }); assert.isUndefined( (yield* threads.getThreadProjection(launched.threadId)).thread.titleRegeneration, ); assert.isNull((yield* threads.getThreadProjection(launched.threadId)).thread.lastVisitedAt); assert.isFalse( (yield* outbox.listByCommandId(CommandId.make("compact-title:initial-message"))).some( (effect) => effect.request.type === "thread-title.generate", ), ); const commandId = CommandId.make("compact-title-conversation"); const messageId = MessageId.make("compact-title-conversation-message"); yield* threads.dispatch({ type: "message.dispatch", commandId, threadId: launched.threadId, messageId, createdBy: "user", creationSource: "web", text: "Fix the failing parser", attachments: [], dispatchMode: { type: "defer_start" }, }); assert.equal( (yield* threads.getThreadProjection(launched.threadId)).thread.titleRegeneration ?.requestId, commandId, ); assert.deepEqual( (yield* outbox.listByCommandId(commandId)) .filter((effect) => effect.request.type === "thread-title.generate") .map((effect) => effect.request), [{ type: "thread-title.generate", kind: { type: "initial", messageId } }], ); }).pipe(Effect.provide(harness.layer)); }), ); it.effect("keeps native maintenance commands out of steering and restart messages", () => Effect.gen(function* () { const harness = makeHarness(); yield* Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const threads = yield* ThreadManagement.ThreadManagementService; const outbox = yield* EffectOutbox.EffectOutboxV2; for (const scenario of [ { name: "compact-steer", first: "Fix the parser", next: " /COMPACT ", mode: "steer_active", }, { name: "compact-restart", first: "Fix the parser", next: "/compact", mode: "restart_active", }, { name: "logout-steer", first: "Fix the parser", next: "/logout", mode: "steer_active", }, { name: "logout-restart", first: "Fix the parser", next: "/logout", mode: "restart_active", }, { name: "steer-logout", first: "/logout", next: "Continue with the parser", mode: "steer_active", }, { name: "steer-compaction", first: "/compact", next: "Continue with the parser", mode: "steer_active", }, ] as const) { const launched = yield* launches.launch( launchInput({ command: `${scenario.name}:launch`, thread: scenario.name, message: scenario.first, }), ); const before = yield* threads.getThreadProjection(launched.threadId); const targetRun = before.runs[0]; if (targetRun === undefined) return yield* Effect.die("Launch must create a run"); const commandId = CommandId.make(`${scenario.name}:message`); const dispatch = threads.dispatch({ type: "message.dispatch", commandId, threadId: launched.threadId, messageId: MessageId.make(`${scenario.name}:message`), createdBy: "user", creationSource: "web", text: scenario.next, attachments: [], dispatchMode: { type: scenario.mode, targetRunId: targetRun.id }, }); if (scenario.name.startsWith("steer-")) { yield* dispatch; const queued = (yield* threads.getThreadProjection(launched.threadId)).runs[1]; assert.equal(queued?.status, "queued"); assert.equal(queued?.userMessageId, MessageId.make(`${scenario.name}:message`)); continue; } const failure = yield* dispatch.pipe(Effect.flip); assert.include( String(failure.cause).toLowerCase(), scenario.name.includes("logout") ? "sign" : "context compaction", ); const after = yield* threads.getThreadProjection(launched.threadId); assert.deepEqual(after.messages, before.messages); assert.deepEqual(after.thread.titleRegeneration, before.thread.titleRegeneration); assert.deepEqual(yield* outbox.listByCommandId(commandId), []); } }).pipe(Effect.provide(harness.layer)); }), ); it.effect("arms durable title generation after accepting the first message", () => Effect.gen(function* () { const harness = makeHarness({ generateTitle: (input) => Effect.succeed({ title: input.previousTitle === undefined ? "Generated title" : "Regenerated title", }), }); yield* Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const threads = yield* ThreadManagement.ThreadManagementService; const outbox = yield* EffectOutbox.EffectOutboxV2; const titleRegeneration = yield* ThreadTitleRegeneration.ThreadTitleRegenerationService; const input = { ...launchInput({ command: "command:launch:title-generation", thread: "thread:launch:title-generation", message: "Generate my title", }), title: "Generate my title", generateTitle: true, }; const launched = yield* launches.launch(input); const generationCommandId = CommandId.make("command:launch:title-generation:initial-message"); const projection = yield* threads.getThreadProjection(launched.threadId); assert.equal(projection.thread.title, "Generate my title"); assert.equal(projection.thread.titleRegeneration?.requestId, generationCommandId); assert.deepEqual( (yield* outbox.listByCommandId(generationCommandId)).map((effect) => effect.request), [ { type: "thread-title.generate", kind: { type: "initial", messageId: MessageId.make("Generate my title:id") }, }, ], ); yield* titleRegeneration.execute({ threadId: launched.threadId, requestId: generationCommandId, kind: { type: "initial", messageId: MessageId.make("Generate my title:id") }, }); const generated = yield* threads.getThreadProjection(launched.threadId); assert.equal(generated.thread.title, "Generated title"); assert.deepEqual( harness.generateThreadTitle.mock.calls[0]?.[0]?.modelSelection, DEFAULT_SERVER_SETTINGS.textGenerationModelSelection, ); const manualRequestId = CommandId.make("command:title-generation:manual"); yield* threads.dispatch({ type: "thread.metadata.update", commandId: manualRequestId, threadId: launched.threadId, regenerateTitle: true, }); assert.deepEqual( (yield* outbox.listByCommandId(manualRequestId)).map((effect) => effect.request), [{ type: "thread-title.generate", kind: { type: "regenerate" } }], ); yield* titleRegeneration.execute({ threadId: launched.threadId, requestId: manualRequestId, kind: { type: "regenerate" }, }); const regenerated = yield* threads.getThreadProjection(launched.threadId); assert.equal(regenerated.thread.title, "Regenerated title"); assert.equal( harness.generateThreadTitle.mock.calls[1]?.[0]?.previousTitle, "Generated title", ); yield* threads.dispatch({ type: "thread.metadata.update", commandId: CommandId.make("command:title-generation:user-rename"), threadId: launched.threadId, title: "Keep my title", }); const renamed = yield* threads.getThreadProjection(launched.threadId); yield* TestClock.adjust(Duration.seconds(1)); yield* threads.dispatch({ type: "thread.title.regeneration.complete", commandId: CommandId.make("command:title-generation:stale-completion"), threadId: launched.threadId, requestId: generationCommandId, title: "Stale generated title", }); const afterStaleCompletion = yield* threads.getThreadProjection(launched.threadId); assert.equal(afterStaleCompletion.thread.title, "Keep my title"); assert.equal( DateTime.toEpochMillis(afterStaleCompletion.thread.updatedAt), DateTime.toEpochMillis(renamed.thread.updatedAt), ); }).pipe(Effect.provide(harness.layer)); }), ); it.effect("does not update a reused thread title when the initial message is rejected", () => Effect.gen(function* () { const harness = makeHarness(); yield* Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const threads = yield* ThreadManagement.ThreadManagementService; const outbox = yield* EffectOutbox.EffectOutboxV2; const threadId = ThreadId.make("thread:launch:reused-title-failure"); yield* threads.dispatch({ type: "thread.create", commandId: CommandId.make("command:launch:reused-title-failure:create"), threadId, projectId, title: "Original title", modelSelection, runtimeMode: "full-access", interactionMode: "default", branch: null, worktreePath: null, createdBy: "user", creationSource: "web", }); const commandId = CommandId.make("command:launch:reused-title-failure"); const failed = yield* launches .launch({ ...launchInput({ command: commandId, thread: threadId, message: "Generate a provisional title", }), reuseExistingThread: true, title: "Generate a provisional title", generateTitle: true, modelSelection: { instanceId: ProviderInstanceId.make("missing-provider"), model: "missing-model", }, }) .pipe(Effect.exit); assert.isTrue(Exit.isFailure(failed)); const projection = yield* threads.getThreadProjection(threadId); assert.equal(projection.thread.title, "Original title"); assert.isUndefined(projection.thread.titleRegeneration); assert.isEmpty(projection.messages); assert.isEmpty(yield* outbox.listByCommandId(CommandId.make(`${commandId}:initial-message`))); }).pipe(Effect.provide(harness.layer)); }), ); // Stands `projectId` in for the Scratch project: a plain folder, not a Git // repository, so Git fails there with the error a real one raises. function scratchHarness() { const claimed: Array = []; const scratchRoot = project.workspaceRoot; const notARepository = (operation: string, cwd: string) => new GitCommandError({ operation, command: "vcs-route", cwd, detail: "Failed to resolve the VCS driver for this Git command.", }); const harness = makeHarness({ managedFolders: Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({ namedProjectsRoot: "/projects", isScratchProject: (id) => Effect.succeed(id === projectId), folderForThread: (input) => Effect.sync(() => { if (input.projectId !== projectId) return Option.none(); claimed.push(input.threadId); return Option.some(`/scratch/folder-${claimed.length}`); }), }), git: { repositoryRoot: (cwd) => Effect.succeed(cwd === scratchRoot ? null : cwd) }, remoteExists: (input) => input.cwd === scratchRoot ? Effect.fail(notARepository("GitWorkflowService.remoteExists", input.cwd)) : Effect.succeed(true), createWorktree: (input) => input.cwd === scratchRoot ? Effect.fail(notARepository("GitWorkflowService.createWorktree", input.cwd)) : Effect.succeed({ worktree: { path: "/other-repo-worktrees/feature", refName: input.newRefName, headSha: "abc", }, } as never), }); const layerScheduledTasks = ScheduledTasks.layer.pipe( Layer.provide( Layer.mergeAll( harness.layer, NodeCrypto.layer, Scheduler.layer, Layer.mock(SecretRequests.SecretRequests)({}), ManagedPolicy.layerTest(), ), ), ); return { ...harness, claimed, layer: Layer.mergeAll(harness.layer, layerScheduledTasks), }; } const worktreeStrategy = { type: "worktree", baseRef: "main", startFromOrigin: true } as const; /** Settles once preparation releases the run, or fails it. */ const preparationSettled = (threadId: ThreadId) => Effect.gen(function* () { const threads = yield* ThreadManagement.ThreadManagementService; yield* threads.streamStoredEventsFrom({ threadId }).pipe( Stream.filter( (stored) => stored.event.type === "run.updated" && (stored.commandId?.endsWith(":release") === true || stored.event.payload.status === "failed"), ), Stream.runHead, ); return yield* threads.getThreadProjection(threadId); }); function scheduledTaskInput(id: string, targetProjectId: ProjectId) { return { id: ScheduledTaskId.make(id), title: "Daily summary", prompt: "Summarize the day.", enabled: false, schedule: { type: "interval" as const, everyMs: 60_000 }, projectId: targetProjectId, threadId: null, workspaceStrategy: worktreeStrategy, modelSelection, runtimeMode: "full-access" as const, interactionMode: "default" as const, }; } it.effect("records the checkout's branch on a root launch that names none", () => { const harness = makeHarness({ git: { currentBranch: () => Effect.succeed("fix/checkout-total") }, }); return Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const launched = yield* launches.launch( launchInput({ command: "command:launch:root-branch", thread: "thread:launch:root-branch" }), ); const threads = yield* ThreadManagement.ThreadManagementService; const { thread } = yield* threads.getThreadProjection(launched.threadId); assert.equal(thread.branch, "fix/checkout-total"); }).pipe(Effect.provide(harness.layer)); }); it.effect("launches a worktree request for Scratch at the root, in its own folder", () => { const harness = scratchHarness(); return Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; yield* launches.checkWorktreeBase({ projectId, workspaceStrategy: worktreeStrategy }); const launched = yield* launches.launch( launchInput({ command: "command:launch:scratch-worktree", thread: "thread:launch:scratch-worktree", message: "Tidy my notes", workspace: worktreeStrategy, }), ); const scratch = yield* preparationSettled(launched.threadId); assert.equal(scratch.thread.worktreePath, "/scratch/folder-1"); // Released past preparation; provider execution is disabled here. assert.equal(scratch.runs[0]?.status, "starting"); assert.deepEqual(scratch.runs[0]?.workspacePreparation, { type: "existing_worktree", worktreePath: "/scratch/folder-1", }); assert.equal(harness.runSetup.mock.calls[0]?.[0]?.worktreePath, "/scratch/folder-1"); assert.equal(harness.createWorktree.mock.calls.length, 0); // A Git project still gets the worktree it asked for. const other = yield* launches.launch({ ...launchInput({ command: "command:launch:git-worktree", thread: "thread:launch:git-worktree", message: "Build the feature", workspace: worktreeStrategy, }), projectId: otherProjectId, }); const git = yield* preparationSettled(other.threadId); assert.equal(git.runs[0]?.status, "starting"); assert.equal(git.runs[0]?.workspacePreparation?.type, "worktree"); assert.equal(git.thread.worktreePath, "/other-repo-worktrees/feature"); assert.equal(harness.createWorktree.mock.calls[0]?.[0]?.cwd, "/other-repo"); assert.deepEqual(harness.claimed, [launched.threadId]); }).pipe(Effect.provide(harness.layer)); }); it.effect("stores a Scratch scheduled task at the root and keeps worktrees elsewhere", () => { const harness = scratchHarness(); return Effect.gen(function* () { const tasks = yield* ScheduledTasks.ScheduledTaskService; const scratch = yield* tasks.upsert( scheduledTaskInput("scheduled-task:scratch-upsert", projectId), ); assert.deepEqual(scratch.task.workspaceStrategy, { type: "root" }); const git = yield* tasks.upsert( scheduledTaskInput("scheduled-task:git-upsert", otherProjectId), ); assert.deepEqual(git.task.workspaceStrategy, worktreeStrategy); }).pipe(Effect.provide(harness.layer)); }); it.effect("runs a Scratch scheduled task saved with a worktree in its own folder", () => { const harness = scratchHarness(); return Effect.gen(function* () { const tasks = yield* ScheduledTasks.ScheduledTaskService; const threads = yield* ThreadManagement.ThreadManagementService; const sql = yield* SqlClient.SqlClient; const { task } = yield* tasks.upsert( scheduledTaskInput("scheduled-task:scratch-stored", projectId), ); // Tasks saved before Scratch was coerced still hold a worktree strategy. yield* sql` UPDATE scheduled_tasks SET workspace_strategy_json = ${JSON.stringify(worktreeStrategy)} WHERE task_id = ${task.id} `; const ran = yield* tasks.runNow({ id: task.id }); assert.equal(ran.task.lastRunStatus, "succeeded"); const [thread] = yield* threads.listProjectThreads({ projectId, includeSubagents: false }); assert.isDefined(thread); const projection = yield* preparationSettled(thread!.id); assert.equal(projection.runs[0]?.status, "starting"); assert.equal(projection.thread.worktreePath, "/scratch/folder-1"); assert.deepEqual(projection.runs[0]?.workspacePreparation, { type: "existing_worktree", worktreePath: "/scratch/folder-1", }); assert.equal(harness.runSetup.mock.calls[0]?.[0]?.worktreePath, "/scratch/folder-1"); assert.equal(harness.createWorktree.mock.calls.length, 0); }).pipe(Effect.provide(harness.layer)); }); it.effect.each(["stable", "upstream/release"])( "launches from %s with the fetched commit and remote PR base", (baseRef) => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const git = yield* GitVcsDriver.GitVcsDriver; const directory = yield* fs.makeTempDirectoryScoped({ prefix: "launch-remote-base-" }); const cwd = `${directory}/repo`; const remote = `${directory}/remote.git`; const worktreePath = `${directory}/worktree`; yield* fs.makeDirectory(cwd); const command = (at: string, args: string[]) => git.execute({ operation: "launch-test", cwd: at, args }); yield* command(cwd, ["init", "-b", "stable"]); yield* command(cwd, ["config", "user.name", "Test"]); yield* command(cwd, ["config", "user.email", "test@example.test"]); yield* command(cwd, ["commit", "--allow-empty", "-m", "initial"]); const localHead = (yield* command(cwd, ["rev-parse", "HEAD"])).stdout.trim(); yield* command(cwd, ["init", "--bare", remote]); yield* command(cwd, ["remote", "add", "upstream", remote]); yield* command(cwd, ["push", "-u", "upstream", "stable:release"]); yield* command(cwd, ["commit", "--allow-empty", "-m", "remote advance"]); yield* command(cwd, ["push", "upstream", "stable:release"]); const remoteHead = (yield* command(cwd, ["rev-parse", "HEAD"])).stdout.trim(); yield* command(cwd, ["reset", "--hard", localHead]); yield* command(cwd, ["update-ref", "-d", "refs/remotes/upstream/release"]); const setupStarted = yield* Deferred.make(); const harness = makeHarness({ hasCommit: ({ refName }) => command(cwd, ["rev-parse", "--verify", `${refName}^{commit}`]).pipe( Effect.as(true), Effect.orElseSucceed(() => false), ), git: { resolveWorktreeBaseRemote: (input) => git.resolveWorktreeBaseRemote({ ...input, cwd }), hasRefNamed: () => Effect.succeed(false), }, fetchWorktreeBaseCommit: (input) => git.fetchWorktreeBaseCommit({ ...input, cwd }), createWorktree: (input, options) => git.createWorktree({ ...input, cwd, path: worktreePath }, options), runSetup: () => Deferred.succeed(setupStarted, undefined).pipe( Effect.as({ status: "no-script" as const }), ), }); yield* Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const threads = yield* ThreadManagement.ThreadManagementService; const workspace = { type: "worktree" as const, baseRef, branch: "feature", startFromOrigin: true, }; yield* launches.checkWorktreeBase({ projectId, workspaceStrategy: workspace }); const launched = yield* launches.launch( launchInput({ command: "command:launch:remote-base", thread: "thread:launch:remote-base", workspace, }), ); yield* Deferred.await(setupStarted); const projection = yield* threads.getThreadProjection(launched.threadId); assert.equal(projection.thread.worktreePath, worktreePath); assert.equal( (yield* command(worktreePath, ["rev-parse", "HEAD"])).stdout.trim(), remoteHead, ); const preview = yield* git.getReviewDiffPreview({ cwd: worktreePath }); const comparisonBase = preview.sources.find( (source) => source.kind === "branch-range", )!.baseRef; assert.equal( (yield* command(worktreePath, ["rev-parse", `${comparisonBase}^{commit}`])).stdout.trim(), remoteHead, ); }).pipe(Effect.provide(harness.layer)); }).pipe( Effect.provide( GitVcsDriver.layer.pipe( Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "launch-remote-git-" })), Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), ), ), ), ); it.effect("fetches the origin base from the remote it already resolved", () => { let fetchInput: Deferred.Deferred | null = null; const harness = makeHarness({ fetchWorktreeBaseCommit: (input) => Deferred.succeed(fetchInput!, input).pipe(Effect.as(null)), }); return Effect.gen(function* () { fetchInput = yield* Deferred.make(); const launches = yield* ThreadLaunch.ThreadLaunchService; yield* launches.launch( launchInput({ command: "command:launch:resolved-remote", thread: "thread:launch:resolved-remote", workspace: { type: "worktree", baseRef: "main", startFromOrigin: true }, }), ); assert.deepEqual(yield* Deferred.await(fetchInput), { cwd: project.workspaceRoot, refName: "main", remote: { remoteName: "origin", branchName: "main" }, }); }).pipe(Effect.provide(harness.layer)); }); it.effect("shows the fetch diagnosis when preparing a worktree from origin fails", () => { const detail = "Git could not authenticate with the remote. Check Git credentials or SSH access on the server, then retry."; const harness = makeHarness({ fetchWorktreeBaseCommit: () => Effect.fail( new GitCommandError({ operation: "GitVcsDriver.fetchRemote", command: "git", cwd: project.workspaceRoot, detail, exitCode: 128, }), ), }); return Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const threads = yield* ThreadManagement.ThreadManagementService; const launched = yield* launches.launch( launchInput({ command: "command:launch:fetch-failure", thread: "thread:launch:fetch-failure", message: "Start from origin", workspace: { type: "worktree", baseRef: "main", startFromOrigin: true }, }), ); yield* threads.streamStoredEventsFrom({ threadId: launched.threadId }).pipe( Stream.filter( (stored) => stored.event.type === "run.updated" && stored.event.payload.status === "failed", ), Stream.runHead, ); const projection = yield* threads.getThreadProjection(launched.threadId); assert.equal(projection.messages[0]?.text, "Start from origin"); assert.equal(projection.runs[0]?.status, "failed"); assert.equal(projection.thread.worktreePath, null); assert.equal( projection.turnItems.find((item) => item.type === "command_execution")?.status, "failed", ); assert.include( projection.turnItems.find((item) => item.type === "error")?.failure.message ?? "", detail, ); assert.equal(harness.createWorktree.mock.calls.length, 0); assert.equal(harness.runSetup.mock.calls.length, 0); }).pipe(Effect.provide(harness.layer)); }); it.effect("rejects a preparation retry after the project disables its provider", () => { let fetchAttempts = 0; const harness = makeHarness({ fetchWorktreeBaseCommit: () => fetchAttempts++ === 0 ? Effect.fail( new GitCommandError({ operation: "GitVcsDriver.fetchRemote", command: "git", cwd: project.workspaceRoot, detail: "Git could not reach the remote.", exitCode: 128, }), ) : Effect.succeed({ commitSha: "remote-main-sha", remoteRefName: "origin/main" }), }); return Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const settings = yield* ServerSettings.ServerSettingsService; const threads = yield* ThreadManagement.ThreadManagementService; const launched = yield* launches.launch( launchInput({ command: "command:launch:disabled-preparation-retry", thread: "thread:launch:disabled-preparation-retry", message: "Start from origin", workspace: { type: "worktree", baseRef: "main", startFromOrigin: true }, }), ); yield* preparationSettled(launched.threadId); yield* settings.updateSettings({ projectSettingsOverrides: { [projectId]: { providerInstances: { [modelSelection.instanceId]: { enabled: false } } }, }, }); const error = yield* launches .retryPreparation({ commandId: CommandId.make("command:launch:disabled-preparation-retry:retry"), threadId: launched.threadId, runId: launched.projection.runs[0]!.id, }) .pipe(Effect.flip); assert.equal(error._tag, "ThreadManagementProviderInstanceDisabledError"); assert.equal((yield* threads.getThreadProjection(launched.threadId)).runs[0]?.status, "failed"); assert.equal(fetchAttempts, 1); }).pipe(Effect.provide(harness.layer)); }); it.effect("does not release a prepared run after the project disables its provider", () => { return Effect.gen(function* () { const setupEntered = yield* Deferred.make(); const allowSetup = yield* Deferred.make(); const harness = makeHarness({ runSetup: () => Deferred.succeed(setupEntered, undefined).pipe( Effect.andThen(Deferred.await(allowSetup)), Effect.as({ status: "no-script" as const }), ), }); yield* Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const settings = yield* ServerSettings.ServerSettingsService; const launched = yield* launches.launch( launchInput({ command: "command:launch:disable-during-preparation", thread: "thread:launch:disable-during-preparation", message: "Prepare a worktree", workspace: { type: "worktree", baseRef: "main" }, }), ); yield* Deferred.await(setupEntered); yield* settings.updateSettings({ projectSettingsOverrides: { [projectId]: { providerInstances: { [modelSelection.instanceId]: { enabled: false } } }, }, }); yield* Deferred.succeed(allowSetup, undefined); const projection = yield* preparationSettled(launched.threadId); assert.equal(projection.runs[0]?.status, "failed"); const effects = yield* (yield* EffectOutbox.EffectOutboxV2).listByCommandId( CommandId.make("command:launch:disable-during-preparation:release"), ); assert.isFalse( effects.some( (effect) => effect.request.type === "provider-turn.start" && effect.request.runId === launched.projection.runs[0]?.id, ), ); }).pipe(Effect.provide(harness.layer)); }); }); it.effect.each(["fails", "is interrupted"] as const)( "holds a message queued during setup when the workspace preparation %s", (ending) => Effect.gen(function* () { const fetchEntered = yield* Deferred.make(); const allowFetch = yield* Deferred.make(); let fetchFailures = 1; const harness = makeHarness({ fetchWorktreeBaseCommit: () => fetchFailures-- <= 0 ? Effect.succeed({ commitSha: "remote-main-sha", remoteRefName: "origin/main" }) : Deferred.succeed(fetchEntered, undefined).pipe( Effect.andThen(Deferred.await(allowFetch)), Effect.andThen( Effect.fail( new GitCommandError({ operation: "GitVcsDriver.fetchRemote", command: "git", cwd: project.workspaceRoot, detail: "Git could not reach the remote.", exitCode: 128, }), ), ), ), }); yield* Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const threads = yield* ThreadManagement.ThreadManagementService; const launched = yield* launches.launch( launchInput({ command: `command:launch:queued-setup-${ending}`, thread: `thread:launch:queued-setup-${ending}`, message: "First message", workspace: { type: "worktree", baseRef: "main", startFromOrigin: true }, }), ); yield* Deferred.await(fetchEntered); const queued = yield* threads.sendToThread({ projectId, commandId: CommandId.make(`command:launch:queued-setup-${ending}:follow-up`), threadId: launched.threadId, messageId: MessageId.make(`message:launch:queued-setup-${ending}:follow-up`), text: "Sent during setup", attachments: [], mode: "queue", createdBy: "user", creationSource: "web", }); assert.equal(queued.delivery, "queued"); if (ending === "fails") { yield* Deferred.succeed(allowFetch, undefined); } else { yield* threads.dispatch({ type: "run.interrupt", commandId: CommandId.make("command:launch:queued-setup-interrupt"), threadId: launched.threadId, runId: launched.projection.runs[0]!.id, }); } yield* threads.streamStoredEventsFrom({ threadId: launched.threadId }).pipe( Stream.filter( (stored) => stored.event.type === "run.updated" && stored.event.payload.queueHeld === true, ), Stream.runHead, ); const projection = yield* threads.getThreadProjection(launched.threadId); assert.equal(projection.runs.at(-1)?.status, "queued"); assert.equal(projection.thread.worktreePath, null); if (ending === "fails") { yield* launches.retryPreparation({ commandId: CommandId.make("command:launch:queued-setup-retry"), threadId: launched.threadId, runId: launched.projection.runs[0]!.id, }); const retried = yield* threads.getThreadProjection(launched.threadId); assert.equal(retried.runs.at(-1)?.queueHeld, false); } }).pipe(Effect.provide(harness.layer)); }), ); it.effect.each(["failed", "interrupted"] as const)( "holds a message queued during setup that arrives after the setup %s", (ending) => { let fetchEntered: Deferred.Deferred | null = null; const harness = makeHarness({ fetchWorktreeBaseCommit: () => ending === "interrupted" ? Deferred.succeed(fetchEntered!, undefined).pipe(Effect.andThen(Effect.never)) : Effect.fail( new GitCommandError({ operation: "GitVcsDriver.fetchRemote", command: "git", cwd: project.workspaceRoot, detail: "Git could not reach the remote.", exitCode: 128, }), ), }); return Effect.gen(function* () { fetchEntered = yield* Deferred.make(); const launches = yield* ThreadLaunch.ThreadLaunchService; const threads = yield* ThreadManagement.ThreadManagementService; const launched = yield* launches.launch( launchInput({ command: `command:launch:late-queued-follow-up-${ending}`, thread: `thread:launch:late-queued-follow-up-${ending}`, message: "First message", workspace: { type: "worktree", baseRef: "main", startFromOrigin: true }, }), ); if (ending === "interrupted") { yield* Deferred.await(fetchEntered); yield* threads.dispatch({ type: "run.interrupt", commandId: CommandId.make("command:launch:late-queued-follow-up:interrupt"), threadId: launched.threadId, runId: launched.projection.runs[0]!.id, }); } yield* threads.streamStoredEventsFrom({ threadId: launched.threadId }).pipe( Stream.filter( (stored) => stored.event.type === "run.updated" && stored.event.payload.status === ending, ), Stream.runHead, ); const queued = yield* threads.sendToThread({ projectId, commandId: CommandId.make(`command:launch:late-queued-follow-up-${ending}:send`), threadId: launched.threadId, messageId: MessageId.make(`message:launch:late-queued-follow-up-${ending}:send`), text: "Sent during setup", attachments: [], mode: "queue", createdBy: "user", creationSource: "web", }); assert.equal(queued.delivery, "queued"); const projection = yield* threads.getThreadProjection(launched.threadId); const held = projection.runs.at(-1)!; assert.equal(held.status, "queued"); assert.equal(held.queueHeld, true); // A message the user starts does not release it: the held message would // run after it in the project checkout. yield* threads.sendToThread({ projectId, commandId: CommandId.make(`command:launch:late-queued-follow-up-${ending}:start`), threadId: launched.threadId, messageId: MessageId.make(`message:launch:late-queued-follow-up-${ending}:start`), text: "Sent after setup", attachments: [], mode: "auto", createdBy: "user", creationSource: "web", }); yield* threads.dispatch({ type: "message.dispatch", commandId: CommandId.make(`command:launch:late-queued-follow-up-${ending}:wake`), threadId: launched.threadId, messageId: MessageId.make(`message:launch:late-queued-follow-up-${ending}:wake`), text: "Background task completed.", attachments: [], dispatchMode: { type: "queue_after_active" }, createdBy: "agent", creationSource: "provider", }); const wake = (yield* threads.getThreadProjection(launched.threadId)).runs.at(-1)!; assert.equal(wake.status, "queued"); assert.isTrue(wake.queueHeld); // Nor does the next one, though the run that message started is now the // latest and carries no workspace preparation. yield* threads.sendToThread({ projectId, commandId: CommandId.make(`command:launch:late-queued-follow-up-${ending}:again`), threadId: launched.threadId, messageId: MessageId.make(`message:launch:late-queued-follow-up-${ending}:again`), text: "Sent after that", attachments: [], mode: "auto", createdBy: "user", creationSource: "web", }); const after = yield* threads.getThreadProjection(launched.threadId); assert.isTrue(after.runs.find((run) => run.id === held.id)?.queueHeld); }).pipe(Effect.provide(harness.layer)); }, ); it.effect("replays a server-allocated launch", () => Effect.gen(function* () { const setupEntered = yield* Deferred.make(); const allowSetup = yield* Deferred.make(); const harness = makeHarness({ runSetup: () => Deferred.succeed(setupEntered, undefined).pipe( Effect.andThen(Deferred.await(allowSetup)), Effect.as({ status: "no-script" as const }), ), }); yield* Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const threads = yield* ThreadManagement.ThreadManagementService; const { threadId: _unusedThreadId, ...rest } = launchInput({ command: "command:launch:allocated-retry", thread: "unused", message: "Only once", workspace: { type: "worktree", baseRef: "main" }, }); const first = yield* launches.launch(rest); yield* Deferred.await(setupEntered); const retry = yield* launches.launch(rest); assert.equal(first.threadId, retry.threadId); assert.isFalse(first.resumed); assert.isTrue(retry.resumed); // Clients drop their pending copy once the thread reaches the message's commit. assert.isNumber(first.sequence); assert.equal(retry.sequence, first.sequence); assert.equal(harness.runSetup.mock.calls.length, 1); assert.equal(retry.projection.messages.length, 1); assert.equal(retry.projection.runs.length, 1); assert.equal(retry.projection.messages[0]?.id, first.projection.messages[0]?.id); assert.equal(retry.projection.runs[0]?.id, first.projection.runs[0]?.id); yield* Deferred.succeed(allowSetup, undefined); yield* threads.streamStoredEventsFrom({ threadId: first.threadId }).pipe( Stream.filter( (stored) => stored.commandId === CommandId.make(`${rest.commandId}:release`) && stored.event.type === "run.updated", ), Stream.runHead, ); const settled = yield* launches.launch(rest); assert.equal(settled.threadId, first.threadId); assert.isTrue(settled.resumed); assert.equal(settled.projection.messages[0]?.id, first.projection.messages[0]?.id); assert.equal(settled.projection.runs[0]?.id, first.projection.runs[0]?.id); assert.equal(harness.runSetup.mock.calls.length, 1); }).pipe(Effect.provide(harness.layer)); }), ); it.effect("replays an accepted launch after the project disables the instance", () => { const harness = makeHarness(); return Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const settings = yield* ServerSettings.ServerSettingsService; const input = launchInput({ command: "command:launch:retry-after-disable", thread: "thread:launch:retry-after-disable", message: "Hello", }); const first = yield* launches.launch(input); yield* settings.updateSettings({ projectSettingsOverrides: { [projectId]: { providerInstances: { [modelSelection.instanceId]: { enabled: false } } }, }, }); const retry = yield* launches.launch(input); assert.equal(retry.threadId, first.threadId); assert.isTrue(retry.resumed); const fresh = yield* launches .launch( launchInput({ command: "command:launch:fresh-after-disable", thread: "thread:launch:fresh-after-disable", message: "Hello", }), ) .pipe(Effect.flip); if (fresh._tag !== "ThreadManagementProviderInstanceDisabledError") { assert.fail(`expected ThreadManagementProviderInstanceDisabledError, got ${fresh._tag}`); } }).pipe(Effect.provide(harness.layer)); }); it.effect("an agent's launch never starts work above the modes it asks for", () => { const harness = makeHarness(); return Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const threads = yield* ThreadManagement.ThreadManagementService; // The first launch, at full access, creates the thread without a message. const created = yield* launches.launch( launchInput({ command: "command:launch:narrowed-retry", thread: "thread:narrowed" }), ); // The same key comes back from a narrower agent, now carrying a message, // as a retry or a concurrent launch that lost the create would. const failed = yield* launches .launch({ ...launchInput({ command: "command:launch:narrowed-retry", thread: "thread:narrowed", message: "Run this", }), runtimeMode: "approval-required", interactionMode: "plan", createdBy: "agent", creationSource: "mcp", }) .pipe(Effect.flip); if (failed._tag !== "ThreadLaunchError") { assert.fail(`expected ThreadLaunchError, got ${failed._tag}`); } assert.equal(failed.operation, "dispatch-message"); const projection = yield* threads.getThreadProjection(created.threadId); assert.equal(projection.runs.length, 0); assert.equal(projection.messages.length, 0); }).pipe(Effect.provide(harness.layer)); }); it.effect("rejects a server-allocated launch replay with a mismatching thread id", () => { const harness = makeHarness(); return Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const { threadId: _unusedThreadId, ...rest } = launchInput({ command: "command:launch:allocated-mismatch", thread: "unused", message: "Mismatch", }); const first = yield* launches.launch(rest); const failed = yield* launches .launch({ ...rest, threadId: ThreadId.make("thread:launch:allocated-mismatch"), }) .pipe(Effect.flip); assert.notEqual(first.threadId, ThreadId.make("thread:launch:allocated-mismatch")); if (failed._tag !== "ThreadLaunchError") { assert.fail(`expected ThreadLaunchError, got ${failed._tag}`); } assert.equal(failed.operation, "create-thread"); assert.include(String(failed.cause), "cannot be replayed"); }).pipe(Effect.provide(harness.layer)); }); it.effect("does not treat an unrelated accepted command receipt as a launch", () => { const harness = makeHarness(); return Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const threads = yield* ThreadManagement.ThreadManagementService; const threadId = ThreadId.make("thread:launch:unrelated-receipt"); yield* threads.dispatch({ type: "thread.create", commandId: CommandId.make("command:launch:unrelated-receipt:create"), threadId, projectId, title: "Existing", modelSelection, runtimeMode: "full-access", interactionMode: "default", branch: null, worktreePath: null, createdBy: "user", creationSource: "web", }); yield* threads.dispatch({ type: "thread.metadata.update", commandId: CommandId.make("command:launch:unrelated-receipt"), threadId, expectedEmpty: true, }); const { threadId: _unusedThreadId, ...rest } = launchInput({ command: "command:launch:unrelated-receipt", thread: "unused", message: "Should not become a launch", }); const failed = yield* launches.launch(rest).pipe(Effect.flip); if (failed._tag !== "ThreadLaunchError") { assert.fail(`expected ThreadLaunchError, got ${failed._tag}`); } assert.equal(failed.operation, "create-thread"); assert.include(String(failed.cause), "cannot be replayed"); const projection = yield* threads.getThreadProjection(threadId); assert.equal(projection.messages.length, 0); assert.equal(projection.runs.length, 0); }).pipe(Effect.provide(harness.layer)); }); it.effect("bounds concurrent first launches to one thread per command", () => Effect.gen(function* () { const setupEntered = yield* Deferred.make(); const allowSetup = yield* Deferred.make(); const harness = makeHarness({ runSetup: () => Deferred.succeed(setupEntered, undefined).pipe( Effect.andThen(Deferred.await(allowSetup)), Effect.as({ status: "no-script" as const }), ), }); yield* Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const threads = yield* ThreadManagement.ThreadManagementService; const { threadId: _unusedThreadId, ...rest } = launchInput({ command: "command:launch:concurrent-allocated", thread: "unused", message: "Race me", workspace: { type: "worktree", baseRef: "main" }, }); const results = yield* Effect.all( [launches.launch(rest).pipe(Effect.exit), launches.launch(rest).pipe(Effect.exit)], { concurrency: "unbounded" }, ); const winner = results.find(Exit.isSuccess); assert.isDefined(winner); const threadId = winner!.value.threadId; for (const result of results) { if (Exit.isSuccess(result)) { assert.equal(result.value.threadId, threadId); continue; } const error = Cause.findErrorOption(result.cause).pipe(Option.getOrThrow); if (error._tag !== "ThreadLaunchError") { assert.fail(`expected ThreadLaunchError, got ${error._tag}`); } assert.equal(error.operation, "create-thread"); assert.include(String(error.cause), "cannot be replayed"); const retried = yield* launches.launch(rest); assert.equal(retried.threadId, threadId); } const projectThreads = yield* threads.listProjectThreads({ projectId, includeSubagents: false, }); assert.equal(projectThreads.length, 1); const projection = yield* threads.getThreadProjection(threadId); assert.equal(projection.messages.length, 1); assert.equal(projection.runs.length, 1); yield* Deferred.await(setupEntered); assert.equal(harness.runSetup.mock.calls.length, 1); yield* Deferred.succeed(allowSetup, undefined); }).pipe(Effect.provide(harness.layer)); }), ); it.effect("does not let a failing same-command caller strand a concurrent durable launch", () => Effect.gen(function* () { const setupEntered = yield* Deferred.make(); const allowSetup = yield* Deferred.make(); const harness = makeHarness({ runSetup: () => Deferred.succeed(setupEntered, undefined).pipe( Effect.andThen(Deferred.await(allowSetup)), Effect.as({ status: "no-script" as const }), ), }); yield* Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const command = "command:launch:failed-owner-race"; const [failed, launched] = yield* Effect.all( [ launches .launch({ ...launchInput({ command, thread: "thread:launch:failed-owner-race", message: "This invalid reuse fails", workspace: { type: "worktree", baseRef: "main" }, }), reuseExistingThread: true, }) .pipe(Effect.exit), launches.launch( launchInput({ command, thread: "thread:launch:successful-peer", message: "This peer persists", workspace: { type: "worktree", baseRef: "main" }, }), ), ], { concurrency: "unbounded" }, ); assert.isTrue(Exit.isFailure(failed)); assert.equal(launched.projection.runs[0]?.status, "preparing"); const entered = yield* Deferred.await(setupEntered).pipe( Effect.timeoutOption(Duration.seconds(2)), ); assert.isTrue(Option.isSome(entered)); assert.equal(harness.runSetup.mock.calls.length, 1); yield* Deferred.succeed(allowSetup, undefined); }).pipe(Effect.provide(harness.layer)); }), ); it.effect("reports one concurrent same-command launch as new and the rest as resumed", () => Effect.gen(function* () { // Both launches read no receipt before either one claims the thread. const arrived = yield* Ref.make(0); const bothRead = yield* Deferred.make(); const harness = makeHarness({ managedFolders: Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({ namedProjectsRoot: "/projects", isScratchProject: () => Effect.succeed(false), folderForThread: () => Ref.updateAndGet(arrived, (count) => count + 1).pipe( Effect.tap((count) => count === 2 ? Deferred.succeed(bothRead, undefined) : Effect.void, ), Effect.andThen(Deferred.await(bothRead)), Effect.as(Option.none()), ), }), }); yield* Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const input = launchInput({ command: "command:launch:concurrent-resumed", thread: "thread:launch:concurrent-resumed", }); const results = yield* Effect.all([launches.launch(input), launches.launch(input)], { concurrency: "unbounded", }); assert.deepEqual(results.map((result) => result.resumed).sort(), [false, true]); }).pipe(Effect.provide(harness.layer)); }), ); it.effect( "does not report a retry as resumed when the earlier attempt only claimed the thread", () => Effect.gen(function* () { const harness = makeHarness(); yield* Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const threads = yield* ThreadManagement.ThreadManagementService; const input = launchInput({ command: "command:launch:claimed-only", thread: "thread:launch:claimed-only", message: "Counted once", }); yield* threads.dispatch({ type: "thread.create", commandId: input.commandId, threadId: input.threadId, projectId: input.projectId, title: input.title, modelSelection: input.modelSelection, runtimeMode: input.runtimeMode, interactionMode: input.interactionMode, branch: null, worktreePath: null, createdBy: input.createdBy, creationSource: input.creationSource, }); const retried = yield* launches.launch(input); assert.isFalse(retried.resumed); assert.isTrue((yield* launches.launch(input)).resumed); }).pipe(Effect.provide(harness.layer)); }), ); it.effect("schedules an accepted preparing message exactly once across concurrent retries", () => Effect.gen(function* () { const setupEntered = yield* Deferred.make(); const allowSetup = yield* Deferred.make(); const harness = makeHarness({ runSetup: () => Deferred.succeed(setupEntered, undefined).pipe( Effect.andThen(Deferred.await(allowSetup)), Effect.as({ status: "no-script" as const }), ), }); yield* Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const threads = yield* ThreadManagement.ThreadManagementService; const input = launchInput({ command: "command:launch:accepted-before-fork", thread: "thread:launch:accepted-before-fork", message: "Resume preparation", workspace: { type: "worktree", baseRef: "main" }, }); const messageId = MessageId.make("message:launch:accepted-before-fork"); yield* threads.dispatch({ type: "thread.create", commandId: input.commandId, threadId: input.threadId, projectId: input.projectId, title: input.title, modelSelection: input.modelSelection, runtimeMode: input.runtimeMode, interactionMode: input.interactionMode, branch: null, worktreePath: null, createdBy: input.createdBy, creationSource: input.creationSource, }); yield* threads.dispatch({ type: "message.dispatch", commandId: CommandId.make(`${input.commandId}:initial-message`), threadId: input.threadId, messageId, text: "Resume preparation", attachments: [], modelSelection: input.modelSelection, dispatchMode: { type: "defer_start" }, createdBy: input.createdBy, creationSource: input.creationSource, }); const preparing = yield* threads.getThreadProjection(input.threadId); assert.equal(preparing.runs[0]?.status, "preparing"); const [first, second] = yield* Effect.all([launches.launch(input), launches.launch(input)], { concurrency: "unbounded", }); yield* Deferred.await(setupEntered); assert.isTrue(first.resumed); assert.isTrue(second.resumed); assert.equal(harness.runSetup.mock.calls.length, 1); yield* Deferred.succeed(allowSetup, undefined); }).pipe(Effect.provide(harness.layer)); }), ); it.effect("creates a strong provider-thread mapping for an imported native session", () => { const harness = makeHarness(); return Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const input = { ...launchInput({ command: "command:launch:imported-native-session", thread: "thread:launch:imported-native-session", }), importedNativeThread: { ref: { driver: ProviderDriverKind.make("codex"), nativeId: "native-session-42", strength: "strong" as const, }, metadata: { title: "Native session", updatedAt: "2026-08-23T00:00:00Z", }, }, }; const launched = yield* launches.launch(input); assert.deepInclude(launched.projection.providerThreads[0], { id: IdAllocator.deriveProviderThread({ driver: input.importedNativeThread.ref.driver, providerInstanceId: modelSelection.instanceId, nativeThreadId: input.importedNativeThread.ref.nativeId, }), driver: input.importedNativeThread.ref.driver, providerInstanceId: modelSelection.instanceId, appThreadId: input.threadId, nativeThreadRef: input.importedNativeThread.ref, status: "not_loaded", nativeMetadata: input.importedNativeThread.metadata, }); assert.equal( launched.projection.thread.activeProviderThreadId, launched.projection.providerThreads[0]?.id, ); }).pipe(Effect.provide(harness.layer)); }); it.effect("shared intake preserves durable attachment bytes after a lost launch result", () => { const harness = makeHarness(); const layerFiles = ServerConfig.layerTest(process.cwd(), { prefix: "t3-message-intake-" }).pipe( Layer.provideMerge(DocumentText.layer.pipe(Layer.provideMerge(NodeServices.layer))), ); return Effect.gen(function* () { const config = yield* ServerConfig.ServerConfig; const fs = yield* FileSystem.FileSystem; const launches = yield* ThreadLaunch.ThreadLaunchService; const threads = yield* ThreadManagement.ThreadManagementService; const pendingId = createPendingAttachmentId(); assert.isNotNull(pendingId); const attachment: ChatAttachment = { type: "image", id: ChatAttachmentId.make(pendingId), name: "image.png", mimeType: "image/png", sizeBytes: 4, }; const pendingPath = resolveAttachmentPath({ attachmentsDir: config.attachmentsDir, attachment, }); assert.isNotNull(pendingPath); yield* fs.makeDirectory(config.attachmentsDir, { recursive: true }); yield* fs.writeFile(pendingPath, new Uint8Array([1, 2, 3, 4])); const input = { ...launchInput({ command: "intake-launch", thread: "intake-thread" }), initialMessage: { messageId: MessageId.make("intake-first"), text: "First [file](t3-context://v1/file/intake-file)", context: { version: 1 as const, records: [ { version: 1 as const, contextId: ComposerContextId.make("intake-file"), kind: "file" as const, label: attachment.name, attachmentId: attachment.id, name: attachment.name, mimeType: attachment.mimeType, sizeBytes: attachment.sizeBytes, }, ], }, attachments: [attachment], }, }; const failed = yield* ThreadMessageIntake.launchThread(input).pipe( Effect.provideService(ThreadLaunch.ThreadLaunchService, { launch: (request) => launches.launch(request).pipe( Effect.andThen( new ThreadLaunch.ThreadLaunchError({ operation: "create-thread", commandId: request.commandId, projectId, cause: "lost result after acceptance", }), ), ), checkWorktreeBase: launches.checkWorktreeBase, retryPreparation: launches.retryPreparation, prepareRun: launches.prepareRun, }), Effect.flip, ); assert.equal(failed._tag, "ThreadLaunchError"); const accepted = yield* threads.getThreadProjection(input.threadId); const stored = accepted.messages.find( (message) => message.id === input.initialMessage.messageId, ); assert.isDefined(stored); assert.notEqual(stored.attachments[0]?.id, attachment.id); const record = stored.context?.records[0]; assert.isDefined(record); assert.equal((record as { attachmentId: string }).attachmentId, stored.attachments[0]?.id); const userItem = accepted.turnItems.find( (item) => item.type === "user_message" && item.messageId === stored.id, ); assert.ok(userItem?.type === "user_message"); assert.deepEqual(userItem.context, stored.context); const storedPath = resolveAttachmentPath({ attachmentsDir: config.attachmentsDir, attachment: stored.attachments[0]!, }); assert.isNotNull(storedPath); assert.deepEqual(yield* fs.readFile(storedPath), new Uint8Array([1, 2, 3, 4])); assert.deepEqual(yield* fs.readFile(pendingPath), new Uint8Array([1, 2, 3, 4])); const replayed = yield* ThreadMessageIntake.launchThread(input); assert.equal(replayed.projection.messages[0]?.id, stored.id); assert.deepEqual(replayed.projection.messages[0]?.attachments, stored.attachments); const claimedFiles = Effect.map(fs.readDirectory(config.attachmentsDir), (files) => files.filter((name) => !name.startsWith("pending-")), ); assert.equal((yield* claimedFiles).length, 1); const duplicate = yield* ThreadMessageIntake.launchThread({ ...input, commandId: CommandId.make("intake-duplicate-create"), initialMessage: { ...input.initialMessage, messageId: MessageId.make("intake-duplicate") }, }).pipe(Effect.result); assert.ok(Result.isFailure(duplicate)); assert.ok(duplicate.failure._tag === "ThreadLaunchError"); assert.equal(duplicate.failure.operation, "create-thread"); assert.propertyVal(duplicate.failure.cause, "_tag", "OrchestratorThreadAlreadyExistsError"); assert.equal((yield* claimedFiles).length, 1); assert.deepEqual(yield* fs.readFile(storedPath), new Uint8Array([1, 2, 3, 4])); assert.deepEqual(yield* fs.readFile(pendingPath), new Uint8Array([1, 2, 3, 4])); const missingProject = yield* ThreadMessageIntake.launchThread({ ...input, commandId: CommandId.make("intake-no-project"), projectId: ProjectId.make("missing-project"), }).pipe(Effect.flip); assert.equal(missingProject._tag, "ThreadLaunchError"); assert.equal((yield* claimedFiles).length, 1); const missingThread = yield* ThreadMessageIntake.dispatchCommand({ type: "message.dispatch", commandId: CommandId.make("intake-no-thread"), threadId: ThreadId.make("missing-thread"), messageId: MessageId.make("intake-missing"), text: "Missing", attachments: [attachment], dispatchMode: { type: "start_immediately" }, createdBy: "user", creationSource: "web", }).pipe(Effect.flip); assert.equal(missingThread._tag, "OrchestratorProjectionError"); assert.equal((yield* claimedFiles).length, 1); const dispatch = ThreadMessageIntake.dispatchCommand({ type: "message.dispatch", commandId: CommandId.make("intake-dispatch"), threadId: input.threadId, messageId: MessageId.make("intake-second"), text: "Second", attachments: [attachment], dispatchMode: { type: "queue_after_active" }, createdBy: "user", creationSource: "web", }); yield* dispatch; yield* dispatch; const queuedProjection = yield* threads.getThreadProjection(input.threadId); const queuedRun = queuedProjection.runs.find( (run) => run.userMessageId === MessageId.make("intake-second"), ); assert.isDefined(queuedRun); assert.equal(queuedRun.status, "queued"); const queuedMessage = queuedProjection.messages.find( (message) => message.id === queuedRun.userMessageId, ); assert.isDefined(queuedMessage); const file: ChatAttachment = { type: "file", id: ChatAttachmentId.make(createPendingAttachmentId("pdf")), name: "queued.pdf", mimeType: "application/pdf", sizeBytes: 4, }; const filePath = resolveAttachmentPath({ attachmentsDir: config.attachmentsDir, attachment: file, }); assert.isNotNull(filePath); yield* fs.writeFile(filePath, new Uint8Array([5, 6, 7, 8])); const edit = ThreadMessageIntake.dispatchCommand({ type: "queued-run.edit", commandId: CommandId.make("intake-edit"), threadId: input.threadId, runId: queuedRun.id, text: "Edited with a file", attachments: [...queuedMessage.attachments, file], }); yield* edit; yield* edit; const editedProjection = yield* threads.getThreadProjection(input.threadId); const editedMessage = editedProjection.messages.find( (message) => message.id === queuedRun.userMessageId, ); assert.isDefined(editedMessage); assert.equal(editedMessage.attachments.length, 2); assert.deepEqual(editedMessage.attachments[0], queuedMessage.attachments[0]); assert.notEqual(editedMessage.attachments[1]?.id, file.id); const durableFilePath = resolveAttachmentPath({ attachmentsDir: config.attachmentsDir, attachment: editedMessage.attachments[1]!, }); assert.isNotNull(durableFilePath); assert.deepEqual(yield* fs.readFile(durableFilePath), new Uint8Array([5, 6, 7, 8])); assert.deepEqual(yield* fs.readFile(filePath), new Uint8Array([5, 6, 7, 8])); const beforeRejectedEdit = (yield* claimedFiles).length; const rejectedEdit = yield* ThreadMessageIntake.dispatchCommand({ type: "queued-run.edit", commandId: CommandId.make("intake-edit-rejected"), threadId: input.threadId, runId: queuedRun.id, text: "", attachments: [file], }).pipe(Effect.flip); assert.equal(rejectedEdit._tag, "OrchestratorCommandRejectedError"); assert.equal((yield* claimedFiles).length, beforeRejectedEdit); assert.deepEqual(yield* fs.readFile(filePath), new Uint8Array([5, 6, 7, 8])); const send = ThreadMessageIntake.sendToThread({ commandId: CommandId.make("intake-send"), projectId, threadId: input.threadId, messageId: MessageId.make("intake-third"), text: "Third", attachments: [attachment], mode: "queue", createdBy: "agent", creationSource: "mcp", }); yield* send; yield* send; assert.equal((yield* claimedFiles).length, 4); const final = yield* threads.getThreadProjection(input.threadId); assert.equal(final.messages.length, 3); for (const message of final.messages) { const path = resolveAttachmentPath({ attachmentsDir: config.attachmentsDir, attachment: message.attachments[0]!, }); assert.isNotNull(path); assert.deepEqual(yield* fs.readFile(path), new Uint8Array([1, 2, 3, 4])); } }).pipe(Effect.provide(Layer.mergeAll(harness.layer, layerFiles))); }); it.effect("cancels tracked setup before provider work is released", () => Effect.gen(function* () { const entered = yield* Deferred.make(); const renamed = yield* Deferred.make(); const harness = makeHarness({ renameBranch: (input) => Deferred.succeed(renamed, undefined).pipe(Effect.as({ branch: input.newBranch })), runSetup: () => Deferred.await(renamed).pipe( Effect.andThen(Deferred.succeed(entered, undefined)), Effect.andThen(Effect.never), ), }); yield* Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const tracker = yield* WorktreeSetupTracker.WorktreeSetupTracker; const threads = yield* ThreadManagement.ThreadManagementService; const outbox = yield* EffectOutbox.EffectOutboxV2; const input = launchInput({ command: "launch:cancel-tracked", thread: "thread:cancel-tracked", message: "Start", workspace: { type: "worktree", baseRef: "main" }, }); const launched = yield* launches.launch(input); yield* Deferred.await(entered); assert.equal((yield* tracker.get(launched.threadId))?.phase, "running"); assert.isTrue(yield* tracker.cancel(launched.threadId)); assert.equal((yield* tracker.get(launched.threadId))?.phase, "cancelled"); const projection = yield* threads.getThreadProjection(launched.threadId); assert.equal(projection.runs[0]?.status, "failed"); assert.isNull(projection.thread.worktreePath); assert.deepEqual(harness.deleteLocalBranch.mock.calls[0]?.[0], { cwd: "/repo", refName: "generated-branch", force: true, }); assert.isEmpty(yield* outbox.listByCommandId(CommandId.make(`${input.commandId}:release`))); }).pipe(Effect.provide(harness.layer)); }), ); it.effect( "deletes a claimed worktree's branch when setup is cancelled before checkout returns", () => Effect.gen(function* () { const claimed = yield* Deferred.make(); const harness = makeHarness({ createWorktree: (_input, options) => (options?.progress?.onWorktreeClaimed?.("/repo-worktrees/feature") ?? Effect.void).pipe( Effect.andThen(Deferred.succeed(claimed, undefined)), Effect.andThen(Effect.never), ), }); yield* Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const tracker = yield* WorktreeSetupTracker.WorktreeSetupTracker; const launched = yield* launches.launch( launchInput({ command: "launch:cancel-claimed", thread: "thread:cancel-claimed", message: "Start", workspace: { type: "worktree", baseRef: "main" }, }), ); yield* Deferred.await(claimed); assert.isTrue(yield* tracker.cancel(launched.threadId)); assert.equal(harness.removeWorktree.mock.calls[0]?.[0].path, "/repo-worktrees/feature"); assert.equal( harness.deleteLocalBranch.mock.calls[0]?.[0].refName, harness.createWorktree.mock.calls[0]?.[0].newRefName, ); }).pipe(Effect.provide(harness.layer)); }), ); it.effect("stops an in-flight branch rename when tracked setup is cancelled", () => Effect.gen(function* () { const setupEntered = yield* Deferred.make(); const branchNameStarted = yield* Deferred.make(); const allowBranchName = yield* Deferred.make(); const branchNameInterrupted = yield* Deferred.make(); const renameAttempted = yield* Deferred.make(); const harness = makeHarness({ renameBranch: (input) => Deferred.succeed(renameAttempted, undefined).pipe(Effect.as({ branch: input.newBranch })), runSetup: () => Deferred.succeed(setupEntered, undefined).pipe(Effect.andThen(Effect.never)), generateBranchName: () => Deferred.succeed(branchNameStarted, undefined).pipe( Effect.andThen(Deferred.await(allowBranchName)), Effect.as({ branch: "generated-branch" }), Effect.onInterrupt(() => Deferred.succeed(branchNameInterrupted, undefined)), ), }); yield* Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const tracker = yield* WorktreeSetupTracker.WorktreeSetupTracker; const threads = yield* ThreadManagement.ThreadManagementService; const launched = yield* launches.launch( launchInput({ command: "launch:cancel-renaming", thread: "thread:cancel-renaming", message: "Start", workspace: { type: "worktree", baseRef: "main" }, }), ); yield* Deferred.await(setupEntered); yield* Deferred.await(branchNameStarted); assert.isTrue(yield* tracker.cancel(launched.threadId)); yield* Deferred.succeed(allowBranchName, undefined); yield* Effect.raceFirst( Deferred.await(branchNameInterrupted), Deferred.await(renameAttempted), ); assert.isTrue( yield* Deferred.isDone(branchNameInterrupted), "the cancelled setup's branch rename kept running", ); assert.equal(harness.renameBranch.mock.calls.length, 0); assert.isNull((yield* threads.getThreadProjection(launched.threadId)).thread.worktreePath); assert.equal( harness.deleteLocalBranch.mock.calls[0]?.[0].refName, harness.createWorktree.mock.calls[0]?.[0].newRefName, ); }).pipe(Effect.provide(harness.layer)); }), ); it.effect("starts the agent of a new worktree thread only after its branch is renamed", () => Effect.gen(function* () { const branchNameStarted = yield* Deferred.make(); const allowBranchName = yield* Deferred.make(); const releasedAtRename = yield* Deferred.make(); let releaseEffects: Effect.Effect> = Effect.succeed([]); const harness = makeHarness({ generateBranchName: () => Deferred.succeed(branchNameStarted, undefined).pipe( Effect.andThen(Deferred.await(allowBranchName)), Effect.as({ branch: "generated-branch" }), ), renameBranch: (input) => Effect.suspend(() => releaseEffects).pipe( Effect.flatMap((effects) => Deferred.succeed(releasedAtRename, effects.length > 0)), Effect.as({ branch: input.newBranch }), ), }); yield* Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const tracker = yield* WorktreeSetupTracker.WorktreeSetupTracker; const outbox = yield* EffectOutbox.EffectOutboxV2; const input = launchInput({ command: "launch:release-after-rename", thread: "thread:release-after-rename", message: "Start", workspace: { type: "worktree", baseRef: "main" }, }); releaseEffects = outbox .listByCommandId(CommandId.make(`${input.commandId}:release`)) .pipe(Effect.orDie); const launched = yield* launches.launch(input); yield* Deferred.await(branchNameStarted); yield* tracker.stream(launched.threadId).pipe( Stream.filter( (snapshot) => snapshot?.stages.some( (stage) => stage.id === "setup-script" && stage.status === "skipped", ) === true, ), Stream.runHead, ); yield* Deferred.succeed(allowBranchName, undefined); assert.isFalse( yield* Deferred.await(releasedAtRename), "the agent's run was released while its branch still had the temporary name", ); }).pipe(Effect.provide(harness.layer)); }), ); it.effect("tells the thread when its worktree branch could not be renamed", () => Effect.gen(function* () { const harness = makeHarness({ generateBranchName: () => Effect.fail( new TextGenerationError({ operation: "generateBranchName", detail: "Failed to spawn Codex CLI process", }), ), }); yield* Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const tracker = yield* WorktreeSetupTracker.WorktreeSetupTracker; const threads = yield* ThreadManagement.ThreadManagementService; const launched = yield* launches.launch( launchInput({ command: "launch:rename-fails", thread: "thread:rename-fails", message: "Start", workspace: { type: "worktree", baseRef: "main" }, }), ); yield* tracker.stream(launched.threadId).pipe( Stream.filter((snapshot) => snapshot?.phase === "done"), Stream.runHead, ); const projection = yield* threads.getThreadProjection(launched.threadId); const notice = projection.turnItems.find((item) => item.type === "system_notice"); assert.equal(notice?.type === "system_notice" ? notice.runId : null, projection.runs[0]?.id); assert.include( notice?.type === "system_notice" ? notice.message : "", `stays ${projection.thread.branch}. Text generation failed in generateBranchName: Failed to spawn Codex CLI process`, ); }).pipe(Effect.provide(harness.layer)); }), ); it.effect("reports submodule downloads as their own setup step", () => Effect.gen(function* () { const progressReady = yield* Deferred.make< NonNullable< NonNullable< Parameters[1] >["progress"] > >(); const release = yield* Deferred.make(); const harness = makeHarness({ createWorktree: (input, options) => Deferred.succeed(progressReady, options?.progress ?? {}).pipe( Effect.andThen(Deferred.await(release)), Effect.as({ worktree: { path: "/repo-worktrees/feature", refName: input.newRefName, headSha: "abc", }, } as never), ), }); yield* Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const tracker = yield* WorktreeSetupTracker.WorktreeSetupTracker; const launched = yield* launches.launch( launchInput({ command: "launch:submodule-progress", thread: "thread:submodule-progress", message: "Start", workspace: { type: "worktree", baseRef: "main" }, }), ); const stage = (id: string) => tracker .get(launched.threadId) .pipe(Effect.map((snapshot) => snapshot?.stages.find((entry) => entry.id === id))); const progress = yield* Deferred.await(progressReady); const line = (text: string) => progress.onSubmoduleLine?.(text) ?? Effect.void; yield* progress.onSubmodulesStarted?.() ?? Effect.void; yield* line("Receiving objects: 20% (2/10), 4.00 MiB | 1.50 MiB/s"); yield* line("Receiving objects: 30% (3/10), 6.00 MiB | 1.50 MiB/s"); assert.equal((yield* stage("checkout"))?.status, "done"); assert.deepInclude(yield* stage("submodules"), { status: "running", detail: "Receiving objects: 20% (2/10), 4.00 MiB | 1.50 MiB/s", }); yield* TestClock.adjust("1 second"); yield* line("Receiving objects: 60% (6/10), 12.00 MiB | 1.50 MiB/s"); assert.equal( (yield* stage("submodules"))?.detail, "Receiving objects: 60% (6/10), 12.00 MiB | 1.50 MiB/s", ); yield* line("Submodule path 'vendor/o'hare': checked out 'abc'"); yield* progress.onSubmodulesFinished?.({ ok: true, detail: null }) ?? Effect.void; yield* Deferred.succeed(release, undefined); yield* tracker.stream(launched.threadId).pipe( Stream.filter((snapshot) => snapshot?.phase !== "running"), Stream.runHead, ); assert.deepInclude(yield* stage("submodules"), { status: "done", detail: "vendor/o'hare", }); }).pipe(Effect.provide(harness.layer)); }), ); it.effect.each([0, 1])("releases an async setup before its completion with exit %s", (exitCode) => Effect.gen(function* () { const completion = yield* Deferred.make<{ exitCode: number | null; durationMs: number }>(); const harness = makeHarness({ runSetup: () => Effect.succeed({ status: "started" as const, async: true, scriptId: "setup", scriptName: "Setup", scriptCommand: "vp install", terminalId: "setup", cwd: "/repo-worktrees/feature", completion: Deferred.await(completion), }), }); yield* Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const threads = yield* ThreadManagement.ThreadManagementService; const tracker = yield* WorktreeSetupTracker.WorktreeSetupTracker; const launched = yield* launches.launch( launchInput({ command: `command:launch:async-${exitCode}`, thread: `thread:launch:async-${exitCode}`, message: "Start during setup", workspace: { type: "worktree", baseRef: "main" }, }), ); yield* tracker.stream(launched.threadId).pipe( Stream.filter( (snapshot) => snapshot?.stages.some((stage) => stage.id === "agent" && stage.status === "done") === true, ), Stream.runHead, ); const running = yield* threads.getThreadProjection(launched.threadId); assert.equal(running.runs[0]?.status, "starting"); assert.equal((yield* tracker.get(launched.threadId))?.phase, "running"); yield* Deferred.succeed(completion, { exitCode, durationMs: 1 }); yield* tracker.stream(launched.threadId).pipe( Stream.filter((snapshot) => snapshot?.phase === "done"), Stream.runHead, ); const settled = yield* tracker.get(launched.threadId); assert.equal( settled?.stages.find((stage) => stage.id === "setup-script")?.status, exitCode === 0 ? "done" : "failed", ); assert.deepInclude( settled?.stages.find((stage) => stage.id === "submodules"), { status: "skipped", detail: "none" }, ); assert.equal( (yield* threads.getThreadProjection(launched.threadId)).runs[0]?.status, "starting", ); }).pipe(Effect.provide(harness.layer)); }), ); it.effect.each(["origin/master", ":/initial"])( "refuses a worktree in a non-Git project before checking base %s", (baseRef) => Effect.gen(function* () { const harness = makeHarness({ git: { repositoryRoot: () => Effect.succeed(null) }, }); yield* Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const result = yield* launches .checkWorktreeBase({ projectId, workspaceStrategy: { type: "worktree", baseRef, startFromOrigin: true }, }) .pipe(Effect.result); assert.isTrue(Result.isFailure(result)); if (Result.isFailure(result)) { assert.instanceOf(result.failure, ThreadLaunch.ThreadLaunchNotRepositoryError); assert.deepInclude(result.failure, { projectId, projectTitle: project.title, workspaceRoot: project.workspaceRoot, }); } yield* launches.checkWorktreeBase({ projectId, workspaceStrategy: { type: "root" } }); yield* launches.checkWorktreeBase({ projectId, workspaceStrategy: { type: "existing_worktree", worktreePath: "/existing-worktree" }, }); }).pipe(Effect.provide(harness.layer)); }), ); it.effect("leaves uncertain Git detection failures to workspace preparation", () => Effect.gen(function* () { const harness = makeHarness({ git: { repositoryRoot: () => Effect.fail( new GitCommandError({ operation: "detect-repository", command: "git", cwd: project.workspaceRoot, detail: "Detection failed.", }), ), }, hasCommit: () => Effect.succeed(true), }); yield* ThreadLaunch.ThreadLaunchService.pipe( Effect.flatMap((launches) => launches.checkWorktreeBase({ projectId, workspaceStrategy: worktreeStrategy }), ), Effect.provide(harness.layer), ); }), ); it.effect.each([ { name: "is missing", baseRef: "t3/renamed", startFromOrigin: false, origin: true }, { name: "has no origin to come from", baseRef: "t3/renamed", startFromOrigin: true, origin: false, }, { name: "is a previous checkout there never was", baseRef: "-", startFromOrigin: false, origin: true, }, ])("refuses a new worktree whose base ref $name", (testCase) => Effect.gen(function* () { const harness = makeHarness({ hasCommit: ({ refName }) => Effect.succeed(refName === "HEAD"), git: { resolveWorktreeBaseRemote: () => Effect.succeed(testCase.origin ? { remoteName: "origin", branchName: "main" } : null), hasRefNamed: () => Effect.succeed(false), }, }); yield* Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const error = yield* launches .checkWorktreeBase({ projectId, workspaceStrategy: { type: "worktree", baseRef: testCase.baseRef, startFromOrigin: testCase.startFromOrigin, }, }) .pipe(Effect.flip); assert.deepInclude(error, { _tag: "ThreadLaunchBaseRefError", projectId, baseRef: testCase.baseRef, }); assert.equal( error.message, `Base ref "${testCase.baseRef}" does not resolve to a commit in project "Project" (/repo).`, ); }).pipe(Effect.provide(harness.layer)); }), ); it.effect.each([ { requested: "werkstatt/baseline", blocking: "werkstatt" }, { requested: "foo", blocking: "foo/bar" }, ])("refuses a new worktree branch $requested that collides with branch $blocking", (testCase) => Effect.gen(function* () { const harness = makeHarness({ hasCommit: () => Effect.succeed(true), git: { listLocalBranchNames: () => Effect.succeed(["main", "werkstatt", "foo/bar"]) }, }); yield* Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const workspace = { type: "worktree" as const, baseRef: "main", branch: testCase.requested }; const commandId = CommandId.make("launch:branch-conflict"); const check = (branch: string) => launches.checkWorktreeBase({ commandId, projectId, workspaceStrategy: { ...workspace, branch }, }); yield* check("werkstatt-baseline"); const error = yield* check(testCase.requested).pipe(Effect.flip); assert.deepInclude(error, { _tag: "ThreadLaunchBranchConflictError", branch: testCase.requested, conflictingBranch: testCase.blocking, }); yield* launches.launch( launchInput({ command: commandId, thread: "thread:branch-conflict", workspace }), ); yield* check(testCase.requested); }).pipe(Effect.provide(harness.layer)); }), ); it.effect("replays an accepted send after the project disables the instance", () => { const harness = makeHarness(); const layerFiles = ServerConfig.layerTest(process.cwd(), { prefix: "t3-send-retry-" }).pipe( Layer.provideMerge(DocumentText.layer.pipe(Layer.provideMerge(NodeServices.layer))), ); return Effect.gen(function* () { const launches = yield* ThreadLaunch.ThreadLaunchService; const settings = yield* ServerSettings.ServerSettingsService; const created = yield* launches.launch( launchInput({ command: "command:send:retry-setup", thread: "thread:send:retry-after-disable", }), ); const sendInput = { commandId: CommandId.make("command:send:retry-after-disable"), projectId, threadId: created.threadId, messageId: MessageId.make("message:send:retry-after-disable"), text: "Hello again", attachments: [], mode: "auto" as const, createdBy: "user" as const, creationSource: "web" as const, }; const first = yield* ThreadMessageIntake.sendToThread(sendInput); yield* settings.updateSettings({ projectSettingsOverrides: { [projectId]: { providerInstances: { [modelSelection.instanceId]: { enabled: false } } }, }, }); const retry = yield* ThreadMessageIntake.sendToThread(sendInput); assert.equal(retry.message.id, first.message.id); const fresh = yield* ThreadMessageIntake.sendToThread({ ...sendInput, commandId: CommandId.make("command:send:fresh-after-disable"), messageId: MessageId.make("message:send:fresh-after-disable"), }).pipe(Effect.flip); if (fresh._tag !== "ThreadManagementProviderInstanceDisabledError") { assert.fail(`expected ThreadManagementProviderInstanceDisabledError, got ${fresh._tag}`); } }).pipe(Effect.provide(Layer.mergeAll(harness.layer, layerFiles))); });