diff --git a/apps/server/src/coderEnvironment.ts b/apps/server/src/coderEnvironment.ts index 92196c3c90769..cfc9299198fc4 100644 --- a/apps/server/src/coderEnvironment.ts +++ b/apps/server/src/coderEnvironment.ts @@ -86,6 +86,7 @@ export const CODER_OMITTED_CAPABILITIES = { pullRequestStackActions: "GitLab has no native stack actions.", otlpEndpointCheck: "No OpenTelemetry export settings or trace export.", serverSelfUpdate: "No server self-update; the gateway installs the helper from the checkout.", + npmReleaseLookup: "No server self-update, the only reader of `server.getNpmRelease`.", serverInstallation: "No server self-update.", serverSelfUpdateProgress: "No server self-update.", serverUpdateThreadContinuation: "No server self-update.", diff --git a/apps/server/src/config.ts b/apps/server/src/config.ts index fc59cd1aff6bf..d7103d89fd18c 100644 --- a/apps/server/src/config.ts +++ b/apps/server/src/config.ts @@ -75,6 +75,14 @@ export const ensureServerDirectories = Effect.fn(function* (paths: ServerDerived ].map((directory) => fileSystem.makeDirectory(directory, { recursive: true })), { concurrency: "unbounded" }, ); + // Logs, traces and the database are written with the process umask; keep them owner-only. + yield* fileSystem + .chmod(paths.stateDir, 0o700) + .pipe( + Effect.catch((cause) => + Effect.logWarning("Could not restrict state directory permissions.", { cause }), + ), + ); // Staged composer images that were never sent expire after a day, as upstream's do. sweepStalePendingAttachments({ diff --git a/apps/server/src/keybindings.test.ts b/apps/server/src/keybindings.test.ts index 97d7e375f8012..8b513c4ca30eb 100644 --- a/apps/server/src/keybindings.test.ts +++ b/apps/server/src/keybindings.test.ts @@ -444,6 +444,61 @@ it.layer(NodeServices.layer)("keybindings", (it) => { }).pipe(Effect.provide(layerKeybindings())), ); + it.effect("removes a hand-written keybinding targeted by its settings spelling", () => + Effect.gen(function* () { + const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig; + yield* writeKeybindingsConfig(keybindingsConfigPath, [ + { + key: "Shift+Cmd+R", + command: "script.run-tests.run", + when: "terminalFocus&&!previewFocus", + }, + { key: "mod+shift+r", command: "script.run-tests.run" }, + { key: "x\u0000true", command: "script.run-tests.run" }, + { key: "x", command: "script.run-tests.run", when: "true" }, + ]); + yield* Effect.gen(function* () { + const keybindings = yield* Keybindings.Keybindings; + yield* keybindings.removeKeybindingRule({ + key: "meta+shift+r", + command: "script.run-tests.run", + when: "terminalFocus && !previewFocus", + }); + yield* keybindings.removeKeybindingRule({ + key: "x", + command: "script.run-tests.run", + when: "true", + }); + }); + + const persisted = yield* readKeybindingsConfig(keybindingsConfigPath); + assert.deepEqual(persisted, [ + { key: "mod+shift+r", command: "script.run-tests.run" }, + { key: "x\u0000true", command: "script.run-tests.run" }, + ]); + }).pipe(Effect.provide(layerKeybindings())), + ); + + it.effect("replaces a hand-written keybinding targeted by its settings spelling", () => + Effect.gen(function* () { + const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig; + yield* writeKeybindingsConfig(keybindingsConfigPath, [ + { key: "escape", command: "script.run-tests.run", when: "a && b && c" }, + ]); + yield* Effect.gen(function* () { + const keybindings = yield* Keybindings.Keybindings; + return yield* keybindings.upsertKeybindingRule({ + key: "mod+alt+r", + command: "script.run-tests.run", + replace: { key: "esc", command: "script.run-tests.run", when: "(a && b) && c" }, + }); + }); + + const persisted = yield* readKeybindingsConfig(keybindingsConfigPath); + assert.deepEqual(persisted, [{ key: "mod+alt+r", command: "script.run-tests.run" }]); + }).pipe(Effect.provide(layerKeybindings())), + ); + it.effect("refuses to overwrite malformed keybindings config", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; diff --git a/apps/server/src/keybindings.ts b/apps/server/src/keybindings.ts index 44dae514dd029..1247428a56d82 100644 --- a/apps/server/src/keybindings.ts +++ b/apps/server/src/keybindings.ts @@ -41,6 +41,7 @@ import { compileResolvedKeybindingRule, compileResolvedKeybindingsConfig, parseKeybindingShortcut, + parseKeybindingWhenExpression, } from "@t3tools/shared/keybindings"; export const ResolvedKeybindingFromConfig = KeybindingRule.pipe( @@ -80,12 +81,18 @@ export const ResolvedKeybindingFromConfig = KeybindingRule.pipe( ), ); +// Settings sends replace/remove targets in its own normalized spelling, which can differ from a +// hand-edited file ("cmd+k" vs "meta+k", "a&&b" vs "a && b"), so compare parsed forms. +function keybindingRuleIdentity(rule: KeybindingRule): string { + const shortcut = parseKeybindingShortcut(rule.key); + const key = (shortcut && encodeShortcut(shortcut)) ?? rule.key; + if (rule.when === undefined) return JSON.stringify([rule.command, key, null]); + const whenAst = parseKeybindingWhenExpression(rule.when); + return JSON.stringify([rule.command, key, whenAst ? encodeWhenAst(whenAst) : rule.when]); +} + function isSameKeybindingRule(left: KeybindingRule, right: KeybindingRule): boolean { - return ( - left.command === right.command && - left.key === right.key && - (left.when ?? undefined) === (right.when ?? undefined) - ); + return keybindingRuleIdentity(left) === keybindingRuleIdentity(right); } const LATE_DEFAULT_KEYBINDINGS: ReadonlyArray<{ diff --git a/apps/server/src/mcp/OrchestratorMcpService.activity.test.ts b/apps/server/src/mcp/OrchestratorMcpService.activity.test.ts index f3b664a4d0c2b..3d76bfea85a3a 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.activity.test.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.activity.test.ts @@ -25,7 +25,7 @@ import * as ServerSettings from "../serverSettings.ts"; import * as SecretRequests from "../secrets/SecretRequests.ts"; import * as ThreadLaunchService from "../orchestration-v2/ThreadLaunchService.ts"; import * as GitVcsDriver from "../vcs/GitVcsDriver.ts"; -// Coder: no peer-link services to mock; `apps/server/src/peer` is not carried. +import * as Orchestrator from "../orchestration-v2/Orchestrator.ts"; import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts"; import type * as McpInvocationContext from "./McpInvocationContext.ts"; import * as OrchestratorMcpService from "./OrchestratorMcpService.ts"; @@ -117,7 +117,8 @@ function makeRun(input: { }; } -it("readThread prefers activity-run status over a newer cancelled queued run", async () => { +it("readThread preserves caller access errors and prefers activity-run status over a newer cancelled queued run", async () => { + let callerUnavailable = false; const projection = { thread: baseThread({ threadId: parentThreadId, @@ -143,15 +144,24 @@ it("readThread prefers activity-run status over a newer cancelled queued run", a Layer.mock(ThreadManagementService.ThreadManagementService)({ getTimelinePage: () => Effect.succeed({ items: [], totalItems: 0, hasMore: false }), getThreadRecords: (threadId) => - threadId === parentThreadId - ? Effect.succeed(projection) - : Effect.die(`unexpected thread ${threadId}`), + callerUnavailable + ? Effect.fail( + new Orchestrator.OrchestratorProjectionError({ + threadId, + cause: "Caller unavailable", + }), + ) + : threadId === parentThreadId + ? Effect.succeed(projection) + : Effect.die(`unexpected thread ${threadId}`), getThreadShell: (threadId) => - Effect.succeed( - threadId === parentThreadId - ? (projection.thread as unknown as OrchestrationV2ThreadShell) - : null, - ), + callerUnavailable + ? Effect.die("A failed caller read must not reach the target") + : Effect.succeed( + threadId === parentThreadId + ? (projection.thread as unknown as OrchestrationV2ThreadShell) + : null, + ), getProjectThreadRecords: (input) => input.threadId === parentThreadId ? Effect.succeed(projection) @@ -181,6 +191,17 @@ it("readThread prefers activity-run status over a newer cancelled queued run", a expect(result.thread.status).toBe("running"); expect(result.thread.latestRunId).toBe(cancelledRunId); expect(result.thread.activeRunId).toBe(activeRunId); + + callerUnavailable = true; + const denied = yield* service + .readThread({ ...makeScope(), capabilities: new Set() }, { threadId: parentThreadId }) + .pipe(Effect.flip); + expect(denied.code).toBe("capability_denied"); + const missingCaller = yield* service + .readThread(makeScope(), { threadId: parentThreadId }) + .pipe(Effect.flip); + expect(missingCaller.code).toBe("orchestration_error"); + expect(missingCaller.message).toContain(`Unable to read thread ${parentThreadId}:`); }).pipe(Effect.provide(layer), Effect.runPromise); }); diff --git a/apps/server/src/mcp/OrchestratorMcpService.test.ts b/apps/server/src/mcp/OrchestratorMcpService.test.ts index 87810e225d89a..077c39d0a4532 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.test.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.test.ts @@ -1304,6 +1304,7 @@ describe("OrchestratorMcpService provider resolution", () => { instanceId: codexInstanceId, driver: ProviderDriverKind.make("codex"), }), + displayName: "Codex", models: [ model("gpt-6", { name: "Sol", @@ -1331,7 +1332,7 @@ describe("OrchestratorMcpService provider resolution", () => { ], }, }), - model("gpt-5", { isLegacy: true }), + model("gpt-5", { name: "Code Legacy", isLegacy: true }), model("gpt-4", { isLegacy: true }), model("gpt-3", { name: "GPT-3", isLegacy: true }), model("my-alpha", { isCustom: true }), @@ -1437,6 +1438,18 @@ describe("OrchestratorMcpService provider resolution", () => { { id: codexInstanceId, models: ["gpt-6", "gpt-4", "gpt-3", "my-alpha"], omitted: 1 }, { id: opencodeInstanceId, models: [], omitted: 0 }, ]); + assert.deepEqual(listing(yield* capabilitiesWith(" ")), listing(yield* capabilitiesWith())); + for (const search of ["code", "codex code", " CODE "]) { + assert.deepEqual(listing(yield* capabilitiesWith(search))[0], { + id: codexInstanceId, + models: ["gpt-5"], + omitted: 0, + }); + } + assert.deepEqual( + listing(yield* capabilitiesWith("open"))[1], + listing(yield* capabilitiesWith())[1], + ); assert.deepEqual(listing(yield* capabilitiesWith("codex gpt 5"))[0], { id: codexInstanceId, models: ["gpt-5"], diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.ts index 8f7aa29b03e04..c1a32166ede7f 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.ts @@ -286,10 +286,8 @@ const FULL_CATALOG_DRIVERS: ReadonlySet = new Set(["codex", "claudeAgent const LISTED_MODEL_LIMIT = 8; /** - * Picks the models orchestrator_capabilities shows for one provider. Hidden models never - * appear. Search words naming the provider are dropped; other words list matches. With no - * words left, lists pinned models (custom, favorite, the caller's own) plus non-legacy - * models, or only the first few in the user's model order for large catalogs. + * Lists visible models, keeping pinned models and limiting large catalogs by default. + * Whole provider words select the provider; partial words filter models when any match. */ function listCapabilityModels(input: { readonly provider: ServerProvider; @@ -305,10 +303,22 @@ function listCapabilityModels(input: { .toSorted((a, b) => (rank.get(a.slug) ?? rank.size) - (rank.get(b.slug) ?? rank.size)); const { instanceId, driver, displayName } = input.provider; const providerText = `${instanceId} ${driver} ${displayName ?? ""}`.toLowerCase(); - const modelTerms = input.searchTerms.filter((term) => !providerText.includes(term)); + // Whole ids like "codex-work" and their parts both name the provider. + const providerWords = new Set([ + ...providerText.split(/\s+/), + ...providerText.split(/[^\p{L}\p{N}]+/u), + ]); + const modelText = (model: ServerProviderModel) => + `${model.slug} ${model.name} ${model.subProvider ?? ""}`.toLowerCase(); + // Part of a provider word ("agent" in "claudeAgent") names the provider unless a model matches it. + const modelTerms = input.searchTerms.filter( + (term) => + !providerWords.has(term) && + (!providerText.includes(term) || visible.some((model) => modelText(model).includes(term))), + ); if (modelTerms.length > 0) { const matches = visible.filter((model) => { - const text = `${model.slug} ${model.name} ${model.subProvider ?? ""}`.toLowerCase(); + const text = modelText(model); return modelTerms.every((term) => text.includes(term)); }); const listed = matches.slice(0, LISTED_MODEL_LIMIT); @@ -512,6 +522,21 @@ function pendingTaskApproval( return approval; } +/** Every task `directAppOwnedChildTasks` can return, so callers load only those. */ +function directChildTaskIds( + target: Pick, + returnedRunIds: ReadonlySet, +): ReadonlyArray { + const ids = new Set(); + if (target.thread.forkedFrom?.type === "node") ids.add(target.thread.forkedFrom.nodeId); + for (const run of target.runs) { + if (returnedRunIds.has(run.id) && run.delegatedTaskId !== undefined) { + ids.add(run.delegatedTaskId); + } + } + return [...ids]; +} + function directAppOwnedChildTasks( parent: Pick, target: Pick, @@ -1008,21 +1033,20 @@ const make = Effect.gen(function* () { ), ); - const loadProjection = (threadId: ThreadId) => + const loadProjection = (threadId: ThreadId, includeUserMessages = true) => threadManagement .getThreadRecords( threadId, [ "runs", "messages", - "subagents", "providerThreads", "providerTurns", "runtimeRequests", "contextTransfers", "turnItems", ], - { turnItemTypes: [], messageRoles: ["user"] }, + { turnItemTypes: [], messageRoles: includeUserMessages ? ["user"] : [] }, ) .pipe( Effect.mapError((error) => @@ -1033,6 +1057,15 @@ const make = Effect.gen(function* () { ), ); + // A dispatcher thread holds thousands of tasks, each with its full prompt; read only the ones named. + const loadTasks = (threadId: ThreadId, taskIds: ReadonlyArray) => + threadManagement.getThreadRecords(threadId, ["subagents"], { subagentIds: taskIds }).pipe( + Effect.map(({ subagents }) => subagents), + Effect.mapError((error) => + failure("orchestration_error", `Unable to read thread ${threadId}: ${errorMessage(error)}`), + ), + ); + // Task waits read on every wake of a parent that may hold thousands of // tasks, so they read only the task they watch and what its status needs. const loadTaskParent = (threadId: ThreadId, taskId: NodeId) => @@ -1072,6 +1105,7 @@ const make = Effect.gen(function* () { const loadCaller = Effect.fn("OrchestratorMcpService.loadCaller")(function* ( scope: McpInvocationScope, + includeUserMessages = true, ) { yield* requireCapability(scope); if (scope.thread === undefined) { @@ -1080,7 +1114,7 @@ const make = Effect.gen(function* () { limits: clientModeCeiling(scope.client), } as const; } - const parent = yield* loadProjection(scope.thread.threadId); + const parent = yield* loadProjection(scope.thread.threadId, includeUserMessages); return { parent, limits: { @@ -1189,7 +1223,8 @@ const make = Effect.gen(function* () { scope: McpInvocationScope, threadId: ThreadId, ) { - const { parent } = yield* loadCaller(scope); + // Caller prompts are not used to read a timeline or acknowledge a child result. + const { parent } = yield* loadCaller(scope, false); const shell = yield* threadManagement .getThreadShell(threadId) .pipe(Effect.mapError(threadManagementFailure)); @@ -1200,13 +1235,19 @@ const make = Effect.gen(function* () { `Thread ${threadId} is no longer available.`, ); } - const target = yield* threadManagement - .getProjectThreadRecords({ projectId: shell.projectId, threadId }, [ - "runs", - "runtimeRequests", - "contextTransfers", - ]) - .pipe(Effect.mapError(threadManagementFailure)); + const input = { projectId: shell.projectId, threadId }; + // Transfers are needed only to acknowledge a caller's child-task result. + const records = + parent === undefined + ? threadManagement + .getProjectThreadRecords(input, ["runs", "runtimeRequests"]) + .pipe(Effect.map((target) => ({ ...target, contextTransfers: [] }))) + : threadManagement.getProjectThreadRecords(input, [ + "runs", + "runtimeRequests", + "contextTransfers", + ]); + const target = yield* records.pipe(Effect.mapError(threadManagementFailure)); return { parent, target, shell } as const; }); @@ -2114,7 +2155,7 @@ const make = Effect.gen(function* () { Effect.mapError(() => failure("orchestration_error", "Unable to read server settings.")), ); const inherited = parent?.thread.modelSelection; - const searchTerms = input.search?.toLowerCase().split(/\s+/) ?? []; + const searchTerms = input.search?.toLowerCase().split(/\s+/).filter(Boolean) ?? []; return { parentThreadId: parent?.thread.id ?? null, inheritedProviderInstanceId: inherited?.instanceId ?? null, @@ -2384,7 +2425,7 @@ const make = Effect.gen(function* () { const childThreadId = current.childThreadId; if (childThreadId === null) { // The task runs in a linked environment: its thread there is stopped through the link. - const { subagents } = yield* loadProjection(scope.thread.threadId); + const subagents = yield* loadTasks(scope.thread.threadId, [input.taskId]); const remoteTask = subagents.find( (task) => task.id === input.taskId && @@ -2427,7 +2468,7 @@ const make = Effect.gen(function* () { } }); yield* assertStoppable(childThreadId); - const parentTask = parentProjection.subagents.find( + const parentTask = (yield* loadTasks(scope.thread.threadId, [input.taskId])).find( (task) => task.id === input.taskId && task.origin === "app_owned", ); const disposeCompletionDelivery = @@ -2547,9 +2588,11 @@ const make = Effect.gen(function* () { runId: parentTask.runId, subagentId: parentTask.id, }); - const { subagents } = yield* threadManagement.getThreadRecords(scope.thread.threadId, [ - "subagents", - ]); + const { subagents } = yield* threadManagement.getThreadRecords( + scope.thread.threadId, + ["subagents"], + { subagentIds: [input.taskId] }, + ); return subagents.some((task) => task.id === input.taskId && task.status === "cancelled"); }).pipe(Effect.mapError(threadManagementFailure)); if (isTerminalTaskStatus(current.status)) { @@ -2781,7 +2824,11 @@ const make = Effect.gen(function* () { ), ); const nowMs = yield* Clock.currentTimeMillis; - const statuses = input.statuses === undefined ? null : new Set(input.statuses); + // An empty list is no filter: clients that fill every optional field send `[]`. + const statuses = + input.statuses === undefined || input.statuses.length === 0 + ? null + : new Set(input.statuses); const titleContains = input.titleContains?.toLocaleLowerCase(); const filtered = projectThreads .filter( @@ -2846,7 +2893,19 @@ const make = Effect.gen(function* () { page.filter((row) => row.sourceThreadId === target.thread.id).map((row) => row.item.runId), ); const tasks = - parent === undefined ? [] : directAppOwnedChildTasks(parent, target, returnedRunIds); + parent === undefined + ? [] + : directAppOwnedChildTasks( + { + thread: parent.thread, + subagents: yield* loadTasks( + parent.thread.id, + directChildTaskIds(target, returnedRunIds), + ), + }, + target, + returnedRunIds, + ); if ( parent !== undefined && scope.thread !== undefined && @@ -2977,14 +3036,16 @@ const make = Effect.gen(function* () { const ownedTask = result.timedOut && status !== "waiting" && - parent?.subagents.some( + parent !== undefined && + taskId !== undefined && + (yield* loadTasks(parent.thread.id, [taskId])).some( (task) => task.id === taskId && task.origin === "app_owned" && task.threadId === parent.thread.id && task.childThreadId === input.threadId && task.completionDelivery?.state !== "disposed", - ) === true; + ); // A child waiting on a question or approval needs an answer, not a wake. const childRequests = ownedTask ? (yield* threadManagement diff --git a/apps/server/src/mcp/TaskCancelUsageLimit.integration.test.ts b/apps/server/src/mcp/TaskCancelUsageLimit.integration.test.ts new file mode 100644 index 0000000000000..e4176219244cf --- /dev/null +++ b/apps/server/src/mcp/TaskCancelUsageLimit.integration.test.ts @@ -0,0 +1,353 @@ +import * as NodeServices from "@effect/platform-node/NodeServices"; +import { assert, it } from "@effect/vitest"; +import { + CommandId, + EnvironmentId, + MessageId, + ProjectId, + ProviderDriverKind, + ProviderInstanceId, + ProviderThreadId, + ProviderTurnId, + ThreadId, + type OrchestrationV2DomainEvent, +} from "@t3tools/contracts"; +import * as DateTime from "effect/DateTime"; +import * as Effect from "effect/Effect"; +import * as Fiber from "effect/Fiber"; +import * as Layer from "effect/Layer"; +import * as Queue from "effect/Queue"; +import * as Stream from "effect/Stream"; +import * as TestClock from "effect/testing/TestClock"; + +import { ClaudeProviderCapabilitiesV2 } from "../orchestration-v2/Adapters/ClaudeAdapterV2.ts"; +import * as CommandReceiptStore from "../orchestration-v2/CommandReceiptStore.ts"; +import * as EffectWorker from "../orchestration-v2/EffectWorker.ts"; +import * as Orchestrator from "../orchestration-v2/Orchestrator.ts"; +import * as ProjectionStore from "../orchestration-v2/ProjectionStore.ts"; +import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"; +import * as ProviderAdapterRegistry from "../orchestration-v2/ProviderAdapterRegistry.ts"; +import * as ThreadLaunchService from "../orchestration-v2/ThreadLaunchService.ts"; +import * as UsageLimitRecoveryWorker from "../orchestration-v2/UsageLimitRecoveryWorker.ts"; +import * as GitVcsDriver from "../vcs/GitVcsDriver.ts"; +import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts"; +import * as ServerSettings from "../serverSettings.ts"; +import * as ProviderReplayHarness from "../orchestration-v2/testkit/ProviderReplayHarness.ts"; +import { checkpointWorkspace } from "@t3tools/provider-testing/replayWorkspace"; +import * as SqlitePersistence from "../persistence/Sqlite.ts"; +import * as ProjectService from "../project/ProjectService.ts"; +import * as ProviderRegistry from "../provider/ProviderRegistry.ts"; +import * as ScheduledTaskService from "../scheduledTasks/ScheduledTaskService.ts"; +import * as Scheduler from "../scheduling/Scheduler.ts"; +import * as SecretRequests from "../secrets/SecretRequests.ts"; +import * as RemoteDelegation from "../peer/RemoteDelegation.ts"; +import type { McpInvocationScope } from "./McpInvocationContext.ts"; +import * as OrchestratorMcpService from "./OrchestratorMcpService.ts"; + +const driver = ProviderDriverKind.make("claudeAgent"); +const instanceId = ProviderInstanceId.make("claudeAgent"); +const modelSelection = { instanceId, model: "claude-test" }; + +// A delegated child that fails on a usage limit is armed to continue at the reset. +// Once its parent cancels the task, nothing is waiting for that continuation. +it.effect.each([ + { cancel: "after its limit", cancelStatus: "failed", resumed: false }, + { cancel: "as its limit lands", cancelStatus: "cancel_requested", resumed: false }, + { cancel: "never", cancelStatus: null, resumed: true }, +] as const)( + "a delegated child whose task is cancelled $cancel resumes at the reset: $resumed", + ({ cancel, cancelStatus, resumed }) => + Effect.scoped( + Effect.gen(function* () { + const cwd = yield* checkpointWorkspace("task-cancel-usage-limit"); + // Set while the provider's usage-limit result is already on its way. + let limitInFlight: Effect.Effect = Effect.void; + const turns: Array<{ + readonly turn: ProviderAdapter.ProviderAdapterV2TurnInput; + readonly events: Queue.Queue; + }> = []; + const adapter: ProviderAdapter.ProviderAdapterV2["Service"] = { + instanceId, + driver, + getCapabilities: () => Effect.succeed(ClaudeProviderCapabilitiesV2), + planSelectionTransition: () => Effect.succeed({ type: "apply_on_next_turn" }), + openSession: (input) => + Effect.gen(function* () { + const now = yield* DateTime.now; + const events = yield* Queue.unbounded(); + return { + instanceId, + driver, + providerSessionId: input.providerSessionId, + providerSession: { + id: input.providerSessionId, + driver, + providerInstanceId: instanceId, + status: "ready", + cwd, + model: modelSelection.model, + capabilities: ClaudeProviderCapabilitiesV2, + createdAt: now, + updatedAt: now, + lastError: null, + }, + events: Stream.fromQueue(events), + ensureThread: ({ threadId }) => + Effect.succeed({ + id: ProviderThreadId.make(`provider-thread:${threadId}`), + driver, + providerInstanceId: instanceId, + providerSessionId: input.providerSessionId, + appThreadId: threadId, + ownerNodeId: null, + nativeThreadRef: { driver, nativeId: `native:${threadId}`, strength: "strong" }, + nativeConversationHeadRef: null, + status: "idle", + firstRunOrdinal: null, + lastRunOrdinal: null, + handoffIds: [], + forkedFrom: null, + createdAt: now, + updatedAt: now, + }), + resumeThread: ({ providerThread }) => Effect.succeed(providerThread), + startTurn: (turn) => + Effect.gen(function* () { + turns.push({ turn, events }); + yield* Queue.offer(events, { + type: "provider_turn.updated", + driver, + providerTurn: { + id: ProviderTurnId.make(`provider-turn:${turn.attemptId}`), + providerThreadId: turn.providerThread.id, + nodeId: turn.rootNodeId, + runAttemptId: turn.attemptId, + nativeTurnRef: null, + ordinal: turn.providerTurnOrdinal, + status: "running", + startedAt: now, + completedAt: null, + }, + }); + }), + steerTurn: () => Effect.die("unused"), + interruptTurn: () => Effect.suspend(() => limitInFlight), + respondToRuntimeRequest: () => Effect.die("unused"), + readThreadSnapshot: () => Effect.die("unused"), + rollbackThread: () => Effect.die("unused"), + forkThread: () => Effect.die("unused"), + }; + }), + }; + const layerAdapters = ProviderAdapterRegistry.layerSingle(adapter); + const layerOrchestrator = ProviderReplayHarness.layerWithRegistry( + { name: "task-cancel-usage-limit" }, + layerAdapters, + { runEffectWorker: false }, + ); + const layerOrchestration = Layer.merge( + layerOrchestrator, + ThreadManagementService.layer.pipe( + Layer.provide(layerOrchestrator), + Layer.provide(ServerSettings.layerTest()), + Layer.provide( + CommandReceiptStore.layer.pipe(Layer.provide(SqlitePersistence.layerMemory)), + ), + ), + ); + const sweeps = new Map>(); + const layerRecovery = UsageLimitRecoveryWorker.layer.pipe( + Layer.provide( + Layer.mergeAll( + Layer.succeed(Scheduler.Scheduler, { + register: (name: string, work: Effect.Effect) => + Effect.gen(function* () { + const context = yield* Effect.context(); + sweeps.set(name, work.pipe(Effect.provideContext(context), Effect.orDie)); + }), + }), + ProjectionStore.layer.pipe(Layer.provide(SqlitePersistence.layerMemory)), + ServerSettings.layerTest({ autoResumeLimitedThreads: true }), + ), + ), + ); + const layerTest = Layer.merge( + OrchestratorMcpService.layer.pipe( + Layer.provide(Layer.mock(ThreadLaunchService.ThreadLaunchService)({})), + Layer.provide(Layer.mock(GitVcsDriver.GitVcsDriver)({})), + ), + layerRecovery, + ).pipe( + Layer.provideMerge(layerOrchestration), + Layer.provide( + Layer.mergeAll( + layerAdapters, + Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }), + Layer.mock(ProjectService.ProjectService)({}), + Layer.mock(SecretRequests.SecretRequests)({}), + Layer.mock(RemoteDelegation.RemoteDelegation)({}), + ServerSettings.layerTest(), + Layer.mock(ScheduledTaskService.ScheduledTaskService)({}), + NodeServices.layer, + ), + ), + ); + + yield* Effect.gen(function* () { + const orchestrator = yield* Orchestrator.OrchestratorV2; + const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; + const service = yield* OrchestratorMcpService.OrchestratorMcpService; + const sweep = Effect.andThen( + Effect.suspend(() => sweeps.get("usage-limit-recovery")!), + worker.drain(), + ); + const parentThreadId = ThreadId.make("thread:task-cancel-limit-parent"); + const watch = (predicate: (event: OrchestrationV2DomainEvent) => boolean) => + orchestrator.streamDomainEvents.pipe( + Stream.filter(predicate), + Stream.take(1), + Stream.runDrain, + Effect.forkScoped, + ); + const turnRunning = () => + watch( + (event) => + event.type === "provider-turn.updated" && event.payload.status === "running", + ); + yield* orchestrator.dispatch({ + type: "thread.create", + commandId: CommandId.make("create-parent"), + threadId: parentThreadId, + projectId: ProjectId.make("project:task-cancel-limit"), + title: "Parent", + modelSelection, + runtimeMode: "full-access", + interactionMode: "default", + branch: null, + worktreePath: cwd, + createdBy: "user", + creationSource: "web", + }); + const parentRunning = yield* turnRunning(); + yield* orchestrator.dispatch({ + type: "message.dispatch", + commandId: CommandId.make("start-parent"), + threadId: parentThreadId, + messageId: MessageId.make("message:start-parent"), + text: "Delegate a review", + attachments: [], + dispatchMode: { type: "start_immediately" }, + createdBy: "user", + creationSource: "web", + }); + yield* worker.drain(); + yield* Fiber.join(parentRunning); + + const parentRun = (yield* orchestrator.getThreadProjection(parentThreadId)).runs[0]!; + const childRunning = yield* turnRunning(); + yield* orchestrator.dispatch({ + type: "delegated_task.request", + commandId: CommandId.make("delegate"), + parentThreadId, + parentRunId: parentRun.id, + parentNodeId: parentRun.rootNodeId!, + task: "Review the change", + modelSelection, + runtimeMode: "full-access", + interactionMode: "default", + completionWake: "always", + createdBy: "agent", + creationSource: "mcp", + }); + yield* worker.drain(); + yield* Fiber.join(childRunning); + const task = (yield* orchestrator.getThreadProjection(parentThreadId)).subagents[0]!; + const childThreadId = task.childThreadId!; + const child = turns.find((entry) => entry.turn.threadId === childThreadId)!; + const childTurn = (yield* orchestrator.getThreadProjection(childThreadId)) + .providerTurns[0]!; + + const scope: McpInvocationScope = { + environmentId: EnvironmentId.make("environment:task-cancel-limit"), + requestNamespace: "provider-session:task-cancel-limit", + thread: { + threadId: parentThreadId, + providerSessionId: "provider-session:task-cancel-limit", + providerInstanceId: instanceId, + }, + client: undefined, + capabilities: new Set(["orchestration"]), + issuedAt: 1, + }; + const cancelTask = Effect.gen(function* () { + const result = yield* service.cancelTask(scope, { + taskId: task.id, + clientRequestId: "cancel-task", + }); + yield* worker.drain(); + return result.status; + }); + + const now = yield* DateTime.now; + const childFailed = yield* watch( + (event) => + event.type === "run.updated" && + event.payload.id === child.turn.runId && + event.payload.status === "failed", + ); + const hitLimit = Effect.forEach( + [ + { + type: "provider_turn.updated", + driver, + providerTurn: { ...childTurn, status: "failed", completedAt: now }, + }, + { + type: "turn.terminal", + driver, + providerThreadId: childTurn.providerThreadId, + providerTurnId: childTurn.id, + runOrdinal: child.turn.runOrdinal, + failureItemOrdinal: 1, + status: "failed", + failure: { + class: "usage_limit", + message: "Claude usage limit reached.", + code: null, + retryable: null, + resetAt: DateTime.formatIso(DateTime.add(now, { minutes: 1 })), + }, + threadDisposition: "reusable", + }, + ] satisfies ReadonlyArray, + (event) => Queue.offer(child.events, event), + { discard: true }, + ); + let cancelled: string | null = null; + if (cancel === "as its limit lands") { + limitInFlight = hitLimit; + cancelled = yield* cancelTask; + } else { + yield* hitLimit; + } + yield* Fiber.join(childFailed); + yield* worker.drain(); + + // The sweep runs every few seconds, so it arms the child before a parent reacts. + yield* sweep; + if (cancel === "after its limit") cancelled = yield* cancelTask; + assert.equal(cancelled, cancelStatus); + + yield* TestClock.adjust("1 minute"); + yield* sweep; + const childAfter = yield* orchestrator.getThreadProjection(childThreadId); + assert.deepEqual( + childAfter.messages.flatMap((message) => + message.role === "user" ? [message.text] : [], + ), + resumed ? ["Review the change", "Continue where you left off."] : ["Review the change"], + ); + }).pipe(Effect.provide(layerTest)); + }), + ), +); diff --git a/apps/server/src/mcp/toolkits/project/handlers.test.ts b/apps/server/src/mcp/toolkits/project/handlers.test.ts index 729b4ee207d06..18226edd40966 100644 --- a/apps/server/src/mcp/toolkits/project/handlers.test.ts +++ b/apps/server/src/mcp/toolkits/project/handlers.test.ts @@ -2,11 +2,13 @@ import { expect, it } from "@effect/vitest"; import { EnvironmentId, ProjectId, + ProviderDriverKind, ProviderInstanceId, type Project as ProjectRecord, type ModelSelection, type RuntimeMode, type OrchestrationV2ThreadShell, + type ServerProvider, } from "@t3tools/contracts"; import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; import * as NodeServices from "@effect/platform-node/NodeServices"; @@ -24,7 +26,9 @@ import * as ThreadLaunch from "../../../orchestration-v2/ThreadLaunchService.ts" import * as ThreadManagement from "../../../orchestration-v2/ThreadManagementService.ts"; import * as ServerConfig from "../../../config.ts"; import * as ServerSettings from "../../../serverSettings.ts"; +import * as ProviderRegistry from "../../../provider/ProviderRegistry.ts"; import * as Project from "../../../project/ProjectService.ts"; +import * as ProjectCloneTracker from "../../../project/ProjectCloneTracker.ts"; import * as ManagedProjectFolders from "../../../project/ManagedProjectFolders.ts"; // Coder: no peer-link services to mock; `apps/server/src/peer` is not carried. import * as GitVcsDriver from "../../../vcs/GitVcsDriver.ts"; @@ -43,6 +47,7 @@ const clientLaunchHarness = (input: { readonly workspaceRoot?: string; readonly checkWorktreeBase?: ThreadLaunch.ThreadLaunchService["Service"]["checkWorktreeBase"]; readonly webOrigin?: string; + readonly providers?: readonly ServerProvider[]; }) => { const projectId = ProjectId.make("project:client-target"); const modelSelection = { @@ -54,6 +59,9 @@ const clientLaunchHarness = (input: { model: input.overrideModel === undefined ? modelSelection : null, }; const layerDependencies = Layer.mergeAll( + Layer.mock(ProviderRegistry.ProviderRegistry)({ + getProviders: Effect.succeed(input.providers ?? []), + }), ServerSettings.layerTest({ ...(input.defaultRuntimeMode === undefined ? {} @@ -233,6 +241,62 @@ it.effect( }), ); +it.effect("a launch steps an inherited mode down to the provider's, and refuses one it lacks", () => + Effect.gen(function* () { + // Pi offers no Auto; a mode a provider lacks would run as Supervised. + const pi = ProviderInstanceId.make("pi"); + const launchPi = Effect.fn("test.launchPi")(function* (runtimeMode?: RuntimeMode) { + const launched: Array = []; + const { projectId, dependencies } = clientLaunchHarness({ + runtimeModeCeiling: "auto", + launched, + providers: [ + { + instanceId: pi, + driver: ProviderDriverKind.make("pi"), + enabled: true, + installed: true, + status: "ready", + version: null, + auth: { status: "authenticated" }, + checkedAt: "2026-01-01T00:00:00Z", + models: [], + slashCommands: [], + skills: [], + supportedRuntimeModes: ["approval-required", "auto-accept-edits", "full-access"], + }, + ], + }); + const toolkit = yield* ProjectToolkit.pipe( + Effect.provide( + McpToolAccess.HandlersLayer.layer(ProjectHandlers.layer).pipe( + Layer.provide(dependencies), + ), + ), + ); + const result = yield* toolkit + .handle("t3_thread_launch", { + projectId, + title: "Fix", + modelSelection: { instanceId: pi, model: "pi-model" }, + ...(runtimeMode === undefined ? {} : { runtimeMode }), + }) + .pipe(Stream.unwrap, Stream.runCollect, Effect.provide(dependencies)); + return { result: result.at(-1)?.result, launched }; + }); + + const inherited = yield* launchPi(); + expect(inherited.launched[0]?.runtimeMode).toBe("auto-accept-edits"); + + const requested = yield* launchPi("auto"); + expect(requested.result).toMatchObject({ code: "invalid_request" }); + expect(requested.launched).toHaveLength(0); + + const offered = yield* launchPi("approval-required"); + expect(offered.launched[0]?.runtimeMode).toBe("approval-required"); + }), +); + it.effect("a launch retried with the same clientRequestId replays the first one", () => Effect.gen(function* () { const launched: Array = []; @@ -472,3 +536,47 @@ it.effect("a retried worktree launch replays after its base ref is gone", () => ]); }), ); + +it.effect("deleting a project through MCP also discards its clone record", () => + Effect.gen(function* () { + const discarded: Array = []; + const harness = clientLaunchHarness({ runtimeModeCeiling: "full-access", launched: [] }); + const dependencies = Layer.merge( + harness.dependencies, + Layer.mergeAll( + Layer.mock(Project.ProjectService)({ + delete: (input) => + Effect.succeed({ + id: input.projectId, + title: "client-target", + workspaceRoot: "/projects/client-target", + repositoryIdentity: null, + faviconPath: null, + defaultModelSelection: null, + defaultThreadEnvMode: null, + autoPull: false, + projectIcon: null, + scripts: [], + createdAt: "2026-01-01T00:00:00.000Z", + updatedAt: "2026-01-01T00:00:00.000Z", + deletedAt: "2026-01-01T00:00:00.000Z", + }), + }), + Layer.mock(ProjectCloneTracker.ProjectCloneTracker)({ + discard: (projectId) => Effect.sync(() => void discarded.push(projectId)), + }), + ), + ); + const toolkit = yield* ProjectToolkit.pipe( + Effect.provide( + McpToolAccess.HandlersLayer.layer(ProjectHandlers.layer).pipe(Layer.provide(dependencies)), + ), + ); + + yield* toolkit + .handle("t3_project_delete", { projectId: harness.projectId }) + .pipe(Stream.unwrap, Stream.runCollect, Effect.provide(dependencies)); + + expect(discarded).toEqual([harness.projectId]); + }), +); diff --git a/apps/server/src/mcp/toolkits/project/handlers.ts b/apps/server/src/mcp/toolkits/project/handlers.ts index 34a8b8c1f4bb6..3e8040c411075 100644 --- a/apps/server/src/mcp/toolkits/project/handlers.ts +++ b/apps/server/src/mcp/toolkits/project/handlers.ts @@ -14,10 +14,13 @@ import * as ThreadLaunch from "../../../orchestration-v2/ThreadLaunchService.ts" import * as ThreadMessageIntake from "../../../orchestration-v2/ThreadMessageIntake.ts"; import * as Claims from "../../../orchestration-v2/AttachmentClaims.ts"; import * as Project from "../../../project/ProjectService.ts"; +import * as ProjectCloneTracker from "../../../project/ProjectCloneTracker.ts"; +import { mutateProjectWithClone } from "../../../project/ProjectMutation.ts"; import * as ManagedProjectFolders from "../../../project/ManagedProjectFolders.ts"; import * as Repositories from "../../../sourceControl/SourceControlRepositoryService.ts"; import * as GitVcsDriver from "../../../vcs/GitVcsDriver.ts"; import * as ServerSettings from "../../../serverSettings.ts"; +import * as ProviderRegistry from "../../../provider/ProviderRegistry.ts"; import { resolveProjectSettings } from "@t3tools/shared/projectSettings"; import { resolveRuntimeMode } from "../../delegatedTaskModes.ts"; import * as McpInvocationContext from "../../McpInvocationContext.ts"; @@ -189,12 +192,21 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, { message: "Pass modelSelection: the project has no default model. orchestrator_capabilities lists providers and models.", }); - const startedRuntimeMode = + let startedRuntimeMode = projectDefaults !== undefined && input.runtimeMode === undefined ? yield* resolveRuntimeMode(runtimeMode, projectDefaults.defaultRuntimeMode).pipe( Effect.orElseSucceed(() => runtimeMode), ) : runtimeMode; + if (!replaying) { + const providers = yield* (yield* ProviderRegistry.ProviderRegistry).getProviders; + startedRuntimeMode = yield* resolveRuntimeMode(startedRuntimeMode, input.runtimeMode, { + modelSelection, + supportedRuntimeModes: providers.find( + (provider) => provider.instanceId === modelSelection.instanceId, + )?.supportedRuntimeModes, + }); + } const result = yield* ThreadMessageIntake.launchThread({ commandId, threadId, @@ -330,9 +342,12 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, { t3_project_delete: McpToolAccess.writesEnvironment((input) => Effect.gen(function* () { const projects = yield* Project.ProjectService; - return yield* projects - .delete({ ...input, commandId: yield* newCommandId() }) - .pipe(Effect.mapError(projectFailure)); + const clones = yield* ProjectCloneTracker.ProjectCloneTracker; + return yield* mutateProjectWithClone(projects, clones, { + ...input, + type: "project.delete", + commandId: yield* newCommandId(), + }).pipe(Effect.mapError(projectFailure)); }), ), t3_project_clone: McpToolAccess.writesEnvironment( diff --git a/apps/server/src/mcp/toolkits/project/tools.ts b/apps/server/src/mcp/toolkits/project/tools.ts index 38931596868b4..bd7c986f4c48a 100644 --- a/apps/server/src/mcp/toolkits/project/tools.ts +++ b/apps/server/src/mcp/toolkits/project/tools.ts @@ -23,12 +23,14 @@ import { import * as FileSystem from "effect/FileSystem"; import * as DocumentText from "../../../provider/DocumentText.ts"; import * as ServerConfig from "../../../config.ts"; +import * as ProviderRegistry from "../../../provider/ProviderRegistry.ts"; import * as ServerSettings from "../../../serverSettings.ts"; import * as ThreadLaunchService from "../../../orchestration-v2/ThreadLaunchService.ts"; import * as Crypto from "effect/Crypto"; import * as Schema from "effect/Schema"; import { Tool, Toolkit } from "effect/ai"; import * as ProjectService from "../../../project/ProjectService.ts"; +import * as ProjectCloneTracker from "../../../project/ProjectCloneTracker.ts"; import * as ManagedProjectFolders from "../../../project/ManagedProjectFolders.ts"; import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts"; import * as SourceControlRepositoryService from "../../../sourceControl/SourceControlRepositoryService.ts"; @@ -100,6 +102,7 @@ const ProjectDeleteTool = Tool.make("t3_project_delete", { projectId: ProjectId, force: Schema.optionalKey(Schema.Boolean), }), + dependencies: [...shared.dependencies, ProjectCloneTracker.ProjectCloneTracker], }).annotate(Tool.Destructive, true); const ProjectCloneTool = Tool.make("t3_project_clone", { ...shared, @@ -166,6 +169,7 @@ const ThreadLaunchTool = Tool.make("t3_thread_launch", { FileSystem.FileSystem, ServerConfig.ServerConfig, ServerSettings.ServerSettingsService, + ProviderRegistry.ProviderRegistry, DocumentText.DocumentText, ], }) diff --git a/apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts index 0dbf72afc7d83..a4cfd9b8bca74 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts @@ -27,6 +27,7 @@ import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; import * as EffectAcpErrors from "effect-acp/errors"; import type * as EffectAcpProtocol from "effect-acp/protocol"; +import { makeProviderFailure } from "@t3tools/provider-core/server/failure"; import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions"; import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost"; import * as ServerSettings from "../../serverSettings.ts"; @@ -147,6 +148,7 @@ describe("AcpRegistryAdapterV2", () => { readonly storedModel?: string; readonly storedModePick?: string; readonly turnModels?: ReadonlyArray; + readonly rejectedModel?: string; }) { const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; // Scope release gives the replay agent at most a second to exit, so the status @@ -281,7 +283,19 @@ describe("AcpRegistryAdapterV2", () => { configurations.push(published); current = target; } - }).pipe(Effect.scoped); + }).pipe( + Effect.match({ + onFailure: (error) => { + assert.isDefined(input.rejectedModel); + assert.equal( + makeProviderFailure({ cause: error }).message, + `The agent did not accept the model "${input.rejectedModel}". Choose another model for this provider, then retry.`, + ); + }, + onSuccess: () => assert.isUndefined(input.rejectedModel), + }), + Effect.scoped, + ); yield* Effect.all(agentExits, { discard: true }); yield* makeAcpReplayCompletenessAssertion(fileSystem, statusPath, transcript); return { configurations }; @@ -448,8 +462,70 @@ describe("AcpRegistryAdapterV2", () => { }).pipe(Effect.provide(layerTest), Effect.scoped), ); + it.effect.each([ + "config", + "legacy", + "updated-config", + "removed-config", + "configuration-update", + "unavailable-before-configuration", + ] as const)( + "rejects an unavailable explicit model in a %s catalog before inference", + (catalog) => { + const duringTurn = catalog === "updated-config" || catalog === "removed-config"; + const duringConfiguration = catalog === "configuration-update"; + const changesOptions = + duringConfiguration || catalog === "unavailable-before-configuration"; + const model = duringTurn || duringConfiguration ? "hermes" : "missing"; + const restrictedOption = { + ...modelOption("model", "other"), + options: [{ value: "other", name: "Other" }], + }; + return openWithStoredPicks({ + setup: { + ...(catalog === "legacy" || catalog === "removed-config" + ? { + models: { + currentModelId: "other", + availableModels: [{ modelId: "other", name: "Other" }], + }, + } + : {}), + configOptions: + catalog === "legacy" + ? [] + : [ + { + ...modelOption("model", "hermes"), + options: [{ value: "hermes", name: "Hermes" }], + }, + ...(changesOptions ? [permissionModeOption("ask")] : []), + ], + }, + storedModel: model, + ...(duringTurn ? { turnModels: [model, model] } : {}), + ...(changesOptions ? { storedModePick: "auto" } : {}), + rejectedModel: model, + pickFrames: duringTurn + ? prompt(configOptionUpdate(catalog === "removed-config" ? [] : [restrictedOption])) + : duringConfiguration + ? [ + outbound("session/set_config_option", { + sessionId: "agent-session", + configId: "permission-mode", + value: "auto", + }), + answer("session/set_config_option", { + configOptions: [restrictedOption, permissionModeOption("auto")], + }), + ] + : [], + }).pipe(Effect.provide(layerTest), Effect.scoped); + }, + ); + // regression: #19519 models come from session model state, not a hardcoded list - it.effect("leaves unadvertised model IDs alone in both model selection protocols", () => + it.effect("keeps unadvertised default and auto selections agent-controlled", () => Effect.gen(function* () { const { configurations } = yield* openWithStoredPicks({ setup: { @@ -459,15 +535,7 @@ describe("AcpRegistryAdapterV2", () => { }, }, storedModel: "default", - turnModels: [ - "auto", - "removed-model", - "other", - "default", - "auto", - "hermes", - "removed-model", - ], + turnModels: ["auto", "default", "other", "default", "auto", "default", "auto"], pickFrames: [ ...prompt(), ...prompt(), diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 55d55745c0c2f..a35029174b997 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -3,6 +3,7 @@ import * as NodeFS from "node:fs"; import * as NodePath from "node:path"; import type { + ElicitationRequest, Query as ClaudeQuery, SDKMessage, SDKResultMessage, @@ -1091,6 +1092,82 @@ describe("ClaudeAdapterV2 approval cancellation", () => { ); }); +describe("ClaudeAdapterV2 MCP elicitation acceptance", () => { + const form = ( + properties: Record, + required?: ReadonlyArray, + ): ElicitationRequest => ({ + serverName: "deploys", + message: "Deploy web?", + mode: "form", + requestedSchema: { type: "object", properties, ...(required ? { required } : {}) }, + }); + + it("answers an empty-properties form with empty content", () => { + assert.deepEqual(ClaudeAdapterV2.claudeMcpElicitationAcceptance(form({})), { + action: "accept", + content: {}, + }); + }); + + it("picks the one-time choice however the server spells it", () => { + for (const choice of [ + "allow_once", + "Allow", + "accept_once", + "Approve once", + "allow_this_time", + "allow_once_only", + ]) { + assert.deepEqual( + ClaudeAdapterV2.claudeMcpElicitationAcceptance( + form({ decision: { type: "string", enum: ["disallow", choice, "always"] } }, [ + "decision", + ]), + ), + { action: "accept", content: { decision: choice } }, + ); + } + }); + + it("answers a persistence toggle with false", () => { + assert.deepEqual( + ClaudeAdapterV2.claudeMcpElicitationAcceptance( + form({ remember: { type: "boolean", title: "Always allow" } }, ["remember"]), + ), + { action: "accept", content: { remember: false } }, + ); + }); + + it("leaves out optional fields the card does not show", () => { + assert.deepEqual( + ClaudeAdapterV2.claudeMcpElicitationAcceptance( + form({ scope: { type: "string", enum: ["once", "always"], default: "always" } }), + ), + { action: "accept", content: { scope: "once" } }, + ); + assert.deepEqual( + ClaudeAdapterV2.claudeMcpElicitationAcceptance( + form({ limit: { type: "number", default: 500 } }), + ), + { action: "accept", content: {} }, + ); + }); + + it("has no answer for URL mode or a form asking for a value", () => { + for (const request of [ + { serverName: "deploys", message: "Sign in", mode: "url", url: "https://example.com" }, + form({ reason: { type: "string" } }, ["reason"]), + form({ grant: { type: "string", enum: ["always", "session"] } }, ["grant"]), + form({ grant: { type: "string", enum: ["deny", "always"], default: "always" } }, ["grant"]), + form({ limit: { type: "number", default: 500 } }, ["limit"]), + { serverName: "deploys", message: "Deploy web?", mode: "form" }, + ] satisfies ReadonlyArray) { + assert.isUndefined(ClaudeAdapterV2.claudeMcpElicitationAcceptance(request)); + } + }); +}); + describe("ClaudeAdapterV2 resumeThread", () => { it.effect("fails for a transcript without messages so a fresh session replaces it", () => Effect.scoped( @@ -3344,6 +3421,32 @@ describe("ClaudeAdapterV2 background wake turns", () => { const restricted = yield* open; assert.equal(restricted.permissionMode, "default"); assert.isUndefined(restricted.allowDangerouslySkipPermissions); + yield* fileSystem.writeFileString( + remoteFile, + JSON.stringify({ + permissions: { disableBypassPermissionsMode: false, disableAutoMode: false }, + disableAutoMode: false, + managedSourcesBehavior: "merge", + }), + ); + const unrestricted = yield* open; + assert.equal(unrestricted.permissionMode, "bypassPermissions"); + assert.equal(unrestricted.allowDangerouslySkipPermissions, true); + yield* fileSystem.writeFileString( + remoteFile, + JSON.stringify({ + permissions: { disableBypassPermissionsMode: "disable", disableAutoMode: "disable" }, + disableAutoMode: false, + }), + ); + const mixed = yield* open; + assert.equal(mixed.permissionMode, "default"); + assert.isUndefined(mixed.allowDangerouslySkipPermissions); + yield* fileSystem.writeFileString( + remoteFile, + JSON.stringify({ permissions: { disableBypassPermissionsMode: true } }), + ); + assert.equal((yield* open).permissionMode, "auto"); yield* fileSystem.remove(remoteFile); assert.equal((yield* open).permissionMode, "bypassPermissions"); const launchRestricted = yield* openWith( @@ -3363,6 +3466,73 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); + it.effect("drops false bypass switches from --settings before they reach the SDK", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const home = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-claude-launch-" }); + const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-claude-launch-cwd-" }); + const openWith = (settings: Record) => + Effect.scoped( + Effect.gen(function* () { + const harness = yield* makeWakeHarnessWithOptions({ + settings: { + ...DEFAULT_CLAUDE_SETTINGS, + homePath: home, + launchArgs: `--settings '${JSON.stringify(settings)}'`, + }, + }); + yield* harness.runtime.startTurn( + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make("attempt-launch-arg-settings"), + text: "Run git status.", + attachments: [], + runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ + runtimeMode: "full-access", + interactionMode: "default", + cwd, + }), + }), + ); + const options = harness.getOpenedOptions()!; + return { + permissionMode: options.permissionMode, + settings: options.settings as { + disableAutoMode?: unknown; + permissions?: Record; + env?: Record; + }, + }; + }), + ); + const unrestricted = yield* openWith({ + disableAutoMode: false, + permissions: { disableBypassPermissionsMode: false, disableAutoMode: false, allow: [] }, + env: { KEPT: "1" }, + }); + assert.equal(unrestricted.permissionMode, "bypassPermissions"); + assert.notProperty(unrestricted.settings, "disableAutoMode"); + assert.deepEqual(unrestricted.settings.permissions, { allow: [] }); + assert.equal(unrestricted.settings.env?.KEPT, "1"); + const restricted = yield* openWith({ + disableAutoMode: false, + permissions: { disableBypassPermissionsMode: "disable", disableAutoMode: "disable" }, + }); + assert.equal(restricted.permissionMode, "default"); + assert.notProperty(restricted.settings, "disableAutoMode"); + assert.deepEqual(restricted.settings.permissions, { + disableBypassPermissionsMode: "disable", + disableAutoMode: "disable", + }); + }).pipe( + Effect.provide( + Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer), + ), + ), + ); + it.effect("asks before restricted Full access tools and follows policy across turns", () => { const remoteSettings = countRemoteSettingsReads(); return Effect.gen(function* () { @@ -4274,6 +4444,196 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); + describe("MCP elicitations", () => { + const testLayer = Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + ); + const approvalOnly: ElicitationRequest = { + serverName: "deploys", + message: "Deploy web to production?", + mode: "form", + requestedSchema: { type: "object", properties: {} }, + title: "Deploy", + }; + + const startElicitationTurn = Effect.fnUntraced(function* ( + options?: Parameters[0], + ) { + const harness = yield* makeWakeHarnessWithOptions(options); + yield* harness.runtime.startTurn( + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make("attempt-claude-elicitation"), + text: "Deploy web.", + attachments: [], + }), + ); + const onElicitation = harness.getOpenedOptions()?.onElicitation; + if (onElicitation === undefined) throw new Error("The Claude query has no onElicitation."); + const runtimeRequests = () => + harness.events.flatMap((event) => + event.type === "runtime_request.updated" ? [event.runtimeRequest] : [], + ); + let requestNumber = 0; + const elicit = (request: ElicitationRequest, signal = new AbortController().signal) => + Effect.promise(() => + onElicitation(request, { signal, requestId: `sdk-${requestNumber++}` }), + ).pipe(Effect.forkScoped); + const awaitCard = Effect.fnUntraced(function* (count: number) { + const event = yield* Queue.take(harness.runtimeRequestReceipts); + assert.equal(runtimeRequests().length, count); + return event.runtimeRequest; + }); + return { harness, elicit, awaitCard, runtimeRequests }; + }); + + it.effect("shows an approval-only form and accepts only after Approve", () => + Effect.gen(function* () { + const { harness, elicit, awaitCard } = yield* startElicitationTurn(); + const answer = yield* elicit(approvalOnly); + const request = yield* awaitCard(1); + assert.equal(request.kind, "mcp-elicitation"); + assert.isTrue(request.responseCapability.type === "live"); + if (request.responseCapability.type === "live") { + assert.isUndefined(request.responseCapability.supportsFeedback); + } + const card = harness.events.findLast( + (event) => + event.type === "turn_item.updated" && event.turnItem.type === "approval_request", + ); + assert.deepInclude(card?.type === "turn_item.updated" ? card.turnItem : undefined, { + prompt: "Deploy web to production?", + appName: "Deploy", + options: [ + { decision: "cancel", label: "Cancel" }, + { decision: "decline", label: "Decline" }, + { decision: "accept", label: "Approve" }, + ], + }); + assert.isUndefined(answer.pollUnsafe()); + + yield* harness.runtime.respondToRuntimeRequest({ + requestId: request.id, + decision: "accept", + }); + assert.deepEqual(yield* Fiber.join(answer), { action: "accept", content: {} }); + + // The card is gone, so a second click is an unknown request. + const late = yield* Effect.flip( + harness.runtime.respondToRuntimeRequest({ requestId: request.id, decision: "decline" }), + ); + assert.include(String(late.cause), "No pending Claude runtime request"); + }).pipe(Effect.scoped, Effect.provide(testLayer)), + ); + + it.effect.each(["decline", "cancel"] as const)("answers %s with that action", (decision) => + Effect.gen(function* () { + const { harness, elicit, awaitCard } = yield* startElicitationTurn(); + const answer = yield* elicit(approvalOnly); + const request = yield* awaitCard(1); + yield* harness.runtime.respondToRuntimeRequest({ requestId: request.id, decision }); + assert.deepEqual(yield* Fiber.join(answer), { action: decision }); + }).pipe(Effect.scoped, Effect.provide(testLayer)), + ); + + it.effect.each(["acceptForSession", "acceptAlways"] as const)( + "declines an unsupported %s decision", + (decision) => + Effect.gen(function* () { + const { harness, elicit, awaitCard } = yield* startElicitationTurn(); + const answer = yield* elicit(approvalOnly); + const request = yield* awaitCard(1); + yield* harness.runtime.respondToRuntimeRequest({ requestId: request.id, decision }); + assert.deepEqual(yield* Fiber.join(answer), { action: "decline" }); + }).pipe(Effect.scoped, Effect.provide(testLayer)), + ); + + it.effect("declines URL and value forms without a card", () => + Effect.gen(function* () { + const { elicit, runtimeRequests } = yield* startElicitationTurn(); + for (const request of [ + { ...approvalOnly, mode: "url", url: "https://example.com/auth" }, + { + ...approvalOnly, + requestedSchema: { + type: "object", + properties: { reason: { type: "string" } }, + required: ["reason"], + }, + }, + ] satisfies ReadonlyArray) { + assert.deepEqual(yield* Fiber.join(yield* elicit(request)), { action: "decline" }); + } + assert.isEmpty(runtimeRequests()); + }).pipe(Effect.scoped, Effect.provide(testLayer)), + ); + + it.effect("cancels an already-aborted request without a card", () => + Effect.gen(function* () { + const { harness, elicit, awaitCard, runtimeRequests } = yield* startElicitationTurn(); + const aborted = yield* Effect.sync(() => { + const controller = new AbortController(); + controller.abort(); + return controller.signal; + }); + const answer = yield* elicit(approvalOnly, aborted); + assert.deepEqual(yield* Fiber.join(answer), { action: "cancel" }); + assert.isEmpty(runtimeRequests()); + const next = yield* elicit(approvalOnly); + const card = yield* awaitCard(1); + assert.equal(card.nativeRequestRef?.nativeId, "mcp-elicitation:sdk-1"); + yield* harness.runtime.respondToRuntimeRequest({ requestId: card.id, decision: "accept" }); + assert.deepEqual(yield* Fiber.join(next), { action: "accept", content: {} }); + }).pipe(Effect.scoped, Effect.provide(testLayer)), + ); + + it.effect("withdraws an open card when the SDK cancels while the session continues", () => + Effect.gen(function* () { + const { harness, elicit, awaitCard } = yield* startElicitationTurn(); + const controller = yield* Effect.sync(() => new AbortController()); + const answer = yield* elicit(approvalOnly, controller.signal); + const request = yield* awaitCard(1); + yield* Effect.sync(() => controller.abort()); + assert.deepEqual(yield* Fiber.join(answer), { action: "cancel" }); + const cancelled = yield* Queue.take(harness.runtimeRequestReceipts); + assert.equal(cancelled.runtimeRequest.id, request.id); + assert.equal(cancelled.runtimeRequest.status, "cancelled"); + assert.equal( + harness.events.findLast((e) => e.type === "node.updated")?.node.status, + "cancelled", + ); + assert.equal( + harness.events.findLast((e) => e.type === "turn_item.updated")?.turnItem.status, + "cancelled", + ); + yield* Effect.flip( + harness.runtime.respondToRuntimeRequest({ requestId: request.id, decision: "accept" }), + ); + }).pipe(Effect.scoped, Effect.provide(testLayer)), + ); + + it.effect("cancels an open card when the session stops", () => + Effect.gen(function* () { + // Like the SDK, the fake query aborts in-flight callbacks when closed. + const inFlight = yield* Effect.sync(() => new AbortController()); + const session = yield* Scope.make(); + const { elicit, awaitCard } = yield* startElicitationTurn({ + close: () => Effect.sync(() => inFlight.abort()), + }).pipe(Scope.provide(session)); + const answer = yield* elicit(approvalOnly, inFlight.signal); + yield* awaitCard(1); + assert.isUndefined(answer.pollUnsafe()); + + yield* Scope.close(session, Exit.void); + assert.deepEqual(yield* Fiber.join(answer), { action: "cancel" }); + }).pipe(Effect.scoped, Effect.provide(testLayer)), + ); + }); + it.effect( "applies model, effort and Fast changes to a process running a background shell, but not thinking or a cleared effort", () => @@ -4557,6 +4917,69 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); + it.effect("completes a turn after the resume dialog without a failed tool row", () => + Effect.scoped( + Effect.gen(function* () { + const harness = yield* makeWakeHarness; + yield* harness.runtime.startTurn( + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make("attempt-resume-dialog"), + text: "continue", + attachments: [], + }), + ); + const dialog = yield* Effect.promise(() => + harness.getOpenedOptions()!.onUserDialog!( + { + dialogKind: "resume_return", + payload: { sessionAgeMinutes: 90, estimatedTokens: 120000 }, + }, + { signal: new AbortController().signal, requestId: "dialog-resume" }, + ), + ).pipe(Effect.forkScoped); + const request = yield* Queue.take(harness.runtimeRequestReceipts); + assert.deepEqual( + harness.events.flatMap((event) => + event.type === "node.updated" && event.node.id === request.runtimeRequest.nodeId + ? [event.node.parentNodeId] + : [], + ), + [NodeId.make("node-attempt-resume-dialog")], + ); + yield* harness.runtime.respondToRuntimeRequest({ + requestId: request.runtimeRequest.id, + answers: { + [formatClaudeResumeCompactionQuestion({ ageMinutes: 90, estimatedTokens: 120000 })]: + "Keep full history", + }, + }); + assert.deepEqual(yield* Fiber.join(dialog), { behavior: "completed", result: "continue" }); + yield* Queue.offer( + harness.sdkMessages, + makeResultFrame({ uuid: "result-resume-dialog", result: "Done" }), + ); + assert.equal((yield* Queue.take(harness.terminalReceipts)).status, "completed"); + assert.deepEqual( + harness.events.flatMap((event) => + event.type === "turn_item.updated" && + event.turnItem.type === "dynamic_tool" && + event.turnItem.nativeItemRef?.nativeId === "dialog-resume" + ? [event.turnItem.status] + : [], + ), + [], + ); + }).pipe( + Effect.provide( + Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer), + ), + ), + ), + ); + it.effect.each( (["aborted_tools", "aborted_streaming"] as const).flatMap((terminalReason) => [true, false].map((steered) => ({ terminalReason, steered })), @@ -5953,7 +6376,7 @@ describe("ClaudeAdapterV2 background wake turns", () => { ? [event.providerTurn.tokenUsage.autoCompactThreshold] : [], ), - [150_000, 150_000, 150_000, 150_000], + [117_000, 117_000, 117_000, 117_000], ); }).pipe( Effect.provide( @@ -5964,10 +6387,10 @@ describe("ClaudeAdapterV2 background wake turns", () => { it.effect.each([ { autoCompactWindow: "", model: "claude-sonnet-4-6", threshold: undefined }, - { autoCompactWindow: "150000", model: "claude-sonnet-4-6", threshold: 150_000 }, + { autoCompactWindow: "150000", model: "claude-sonnet-4-6", threshold: 117_000 }, { autoCompactWindow: "200000", model: "claude-sonnet-4-6", threshold: undefined }, { autoCompactWindow: "650000", model: "claude-sonnet-4-6", threshold: undefined }, - { autoCompactWindow: "650000", model: "uncatalogued-model", threshold: 650_000 }, + { autoCompactWindow: "650000", model: "uncatalogued-model", threshold: 617_000 }, ])( "ignores stale snapshots after compaction and reports threshold $autoCompactWindow for $model", ({ autoCompactWindow, model, threshold }) => @@ -6474,6 +6897,90 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); + it.effect("names the notebook a Claude NotebookEdit changed", () => + Effect.gen(function* () { + const harness = yield* makeWakeHarness; + yield* harness.runtime.startTurn( + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make("attempt-notebook-edit"), + text: "Fix the notebook", + attachments: [], + }), + ); + yield* Queue.offer( + harness.sdkMessages, + claudeSdkFrame({ + type: "assistant", + uuid: "00000000-0000-4000-8000-000000000604", + session_id: WAKE_NATIVE_SESSION, + parent_tool_use_id: null, + message: { + id: "msg_notebook_edit", + model: "claude-sonnet-4-6", + type: "message", + role: "assistant", + content: [ + { + type: "tool_use", + id: "notebook", + name: "NotebookEdit", + input: { + notebook_path: "/workspace/analysis.ipynb", + cell_id: "c1", + new_source: "print(1)", + }, + }, + ], + stop_reason: "tool_use", + stop_sequence: null, + usage: { + input_tokens: 1, + output_tokens: 1, + cache_creation_input_tokens: 0, + cache_read_input_tokens: 0, + }, + }, + }), + ); + yield* Queue.offer( + harness.sdkMessages, + claudeSdkFrame({ + type: "user", + uuid: "00000000-0000-4000-8000-000000000605", + session_id: WAKE_NATIVE_SESSION, + parent_tool_use_id: null, + message: { + role: "user", + content: [{ type: "tool_result", tool_use_id: "notebook", content: "Updated cell c1" }], + }, + }), + ); + yield* Queue.offer( + harness.sdkMessages, + makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000606", result: "Done" }), + ); + yield* Queue.take(harness.terminalReceipts); + const item = harness.events + .flatMap((event) => + event.type === "turn_item.updated" && + event.turnItem.nativeItemRef?.nativeId === "notebook" + ? [event.turnItem] + : [], + ) + .at(-1); + assert.equal(item?.type, "file_change"); + if (item?.type === "file_change") assert.equal(item.fileName, "/workspace/analysis.ipynb"); + }).pipe( + Effect.scoped, + Effect.provide( + Layer.mergeAll(NodeServices.layer, IdAllocator.layer, McpProviderSessions.layer), + ), + ), + ); + it.effect.each(["claude-haiku-4-5-20251001", "claude-sonnet-4-6"])( "renegotiates auto only when switching API models after fallback to default: %s", (nextModel) => @@ -7847,6 +8354,116 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); + it.effect.skipIf(process.getuid === undefined)( + "tails foreground Bash output under the physical cwd the query launched in", + () => + Effect.scoped( + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const root = yield* fileSystem.makeTempDirectoryScoped({ + prefix: "t3-claude-task-output-cwd-", + }); + const physicalCwd = path.join(root, "real", "project"); + const launchCwd = path.join(root, "link"); + yield* fileSystem.makeDirectory(physicalCwd, { recursive: true }); + yield* fileSystem.symlink(physicalCwd, launchCwd); + // The session opens in /workspace; a relative TMPDIR puts Claude's + // task files under whichever cwd the CLI was launched in. Linux, + // because Claude Code's macOS build ignores TMPDIR. + const harness = yield* makeWakeHarnessWithOptions({ environment: { TMPDIR: "../tmp" } }); + const now = yield* DateTime.now; + const toolUseId = "toolu_foreground_bash"; + const taskId = "foreground-bash-task"; + const tasks = path.join( + root, + "real", + "tmp", + `claude-${process.getuid?.()}`, + "project", + WAKE_NATIVE_SESSION, + "tasks", + ); + yield* fileSystem.makeDirectory(tasks, { recursive: true }); + yield* fileSystem.writeFileString(path.join(tasks, `${taskId}.output`), "line 1\n"); + + yield* harness.runtime.startTurn( + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now, + attemptId: RunAttemptId.make("attempt-claude-task-output-cwd"), + text: "Run the build.", + attachments: [], + runtimePolicy: { ...CLAUDE_TEST_RUNTIME_POLICY, cwd: launchCwd }, + }), + ); + assert.equal(harness.getOpenedOptions()?.cwd, launchCwd); + for (const frame of [ + claudeSdkFrame({ + type: "assistant", + message: { + model: "claude-sonnet-4-6", + id: "msg_foreground_bash", + type: "message", + role: "assistant", + content: [ + { + type: "tool_use", + id: toolUseId, + name: "Bash", + input: { command: "vp run build", description: "Build" }, + }, + ], + stop_reason: null, + stop_sequence: null, + usage: { input_tokens: 1, output_tokens: 1 }, + }, + parent_tool_use_id: null, + uuid: "00000000-0000-4000-8000-000000000a01", + session_id: WAKE_NATIVE_SESSION, + }), + claudeSdkFrame({ + type: "system", + subtype: "task_started", + task_id: taskId, + tool_use_id: toolUseId, + description: "vp run build", + is_backgrounded: false, + task_type: "local_bash", + uuid: "00000000-0000-4000-8000-000000000a02", + session_id: WAKE_NATIVE_SESSION, + }), + claudeSdkFrame({ + type: "user", + message: { + role: "user", + content: [{ type: "tool_result", tool_use_id: toolUseId, content: "line 1" }], + }, + parent_tool_use_id: null, + uuid: "00000000-0000-4000-8000-000000000a03", + session_id: WAKE_NATIVE_SESSION, + tool_use_result: { stdout: "line 1\n", stderr: "", interrupted: false }, + }), + makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000a04", result: "Built." }), + ]) { + yield* Queue.offer(harness.sdkMessages, frame); + } + yield* harness.awaitTerminal(1); + + const chunks = harness.events.flatMap((event) => + event.type === "command_output.delta" ? [event.chunk] : [], + ); + assert.deepEqual(chunks, ["line 1\n"]); + }).pipe( + Effect.provideService(HostProcess.Platform, "linux"), + Effect.provide( + Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer), + ), + ), + ), + ); + it.effect("preserves settled background work until an explicit stop", () => Effect.scoped( Effect.gen(function* () { diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.testkit.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.testkit.ts index 32278e5fb9705..d3d24f1c7af86 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.testkit.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.testkit.ts @@ -991,7 +991,12 @@ const writeReplayTaskOutputFiles = Effect.fn("ClaudeReplay.writeTaskOutputFiles" const stderr = typeof result.stderr === "string" ? result.stderr : ""; outputByToolUseId.set(toolResult.tool_use_id, `${result.stdout}${stderr}`); } - const root = claudeTaskOutputRoot({ CLAUDE_CODE_TMPDIR: tmpRoot }, path); + const root = claudeTaskOutputRoot( + { CLAUDE_CODE_TMPDIR: tmpRoot }, + path, + null, + yield* HostProcess.Platform, + ); for (const frame of frames) { if (frame.type !== "system" || frame.subtype !== "task_started") continue; if (frame.task_type !== "local_bash" || frame.is_backgrounded !== false) continue; diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 26e684c8ca96d..f89bd3fbb9722 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -22,9 +22,12 @@ import { import { isWorkspaceImagePreviewPath } from "@t3tools/shared/filePreview"; import { normalizeClaudeTurnTokenUsage } from "../../provider/ClaudeTurnTokenUsage.ts"; +import { toMcpElicitationResponse } from "../../provider/CodexMcpElicitation.ts"; // Coder: a type-only import, so the helper bundle never loads the SDK. import type { CanUseTool, + ElicitationRequest, + ElicitationResult, EffortLevel, ForkSessionOptions, ForkSessionResult, @@ -132,7 +135,7 @@ import { import { claudeLaunchArgDirectories, discoverClaudeSkills, - skillOverrideSettingsPaths, + claudeSettingsPaths, } from "../../provider/Drivers/ClaudeSkills.ts"; import { compileClaudeModelSelection, @@ -224,10 +227,14 @@ function claudePromptCacheTtlMs( } // Claude caps the window at the model's context, so a setting at or above it never applies. +// Below that, Claude compacts 33,000 tokens short of the window: Claude Code 2.1.305's +// getContextUsage reports a 150,000 window as a 117,000 threshold, for every catalog model. +const CLAUDE_AUTO_COMPACT_RESERVE_TOKENS = 33_000; + function claudeAutoCompactThreshold(autoCompactWindow: string, maxTokens: number | null) { - const threshold = Number(autoCompactWindow); - return threshold > 0 && (maxTokens === null || threshold < maxTokens) - ? { autoCompactThreshold: threshold } + const windowTokens = Number(autoCompactWindow); + return windowTokens > 0 && (maxTokens === null || windowTokens < maxTokens) + ? { autoCompactThreshold: windowTokens - CLAUDE_AUTO_COMPACT_RESERVE_TOKENS } : {}; } @@ -1366,6 +1373,7 @@ export function makeClaudeQueryOptions(input: { readonly permissionMode?: PermissionMode; readonly canUseTool?: CanUseTool; readonly onUserDialog?: ClaudeQueryOptions["onUserDialog"]; + readonly onElicitation?: ClaudeQueryOptions["onElicitation"]; readonly supportedDialogKinds?: ClaudeQueryOptions["supportedDialogKinds"]; readonly allowDangerouslySkipPermissions?: boolean; readonly appendSystemPromptFileText?: string; @@ -1472,6 +1480,7 @@ export function makeClaudeQueryOptions(input: { ? {} : { settings: effectiveQuerySettings }), ...(input.onUserDialog === undefined ? {} : { onUserDialog: input.onUserDialog }), + ...(input.onElicitation === undefined ? {} : { onElicitation: input.onElicitation }), ...(input.supportedDialogKinds === undefined ? {} : { supportedDialogKinds: input.supportedDialogKinds }), @@ -2468,12 +2477,12 @@ export function claudeRuntimeQueryPolicyForRuntimePolicy( const ClaudeBypassSettings = Schema.StructWithRest( Schema.Struct({ - disableAutoMode: Schema.optionalKey(Schema.Literal("disable")), + disableAutoMode: Schema.optionalKey(Schema.Literals(["disable", false])), permissions: Schema.optionalKey( Schema.StructWithRest( Schema.Struct({ - disableBypassPermissionsMode: Schema.optionalKey(Schema.Literal("disable")), - disableAutoMode: Schema.optionalKey(Schema.Literal("disable")), + disableBypassPermissionsMode: Schema.optionalKey(Schema.Literals(["disable", false])), + disableAutoMode: Schema.optionalKey(Schema.Literals(["disable", false])), }), [Schema.Record(Schema.String, Schema.Unknown)], ), @@ -2486,6 +2495,39 @@ const decodeClaudeBypassSettings = Schema.decodeUnknownEffect( ); const decodeClaudeLaunchArgBypassSettings = Schema.decodeUnknownEffect(ClaudeBypassSettings); +function claudeSdkBypassSettings(settings: typeof ClaudeBypassSettings.Type) { + const { disableAutoMode, permissions, ...rest } = settings; + const { + disableBypassPermissionsMode, + disableAutoMode: nestedAutoMode, + ...permissionRest + } = permissions ?? {}; + return { + ...rest, + ...(disableAutoMode === "disable" ? { disableAutoMode } : {}), + ...(permissions + ? { + permissions: { + ...permissionRest, + ...(disableBypassPermissionsMode === "disable" ? { disableBypassPermissionsMode } : {}), + ...(nestedAutoMode === "disable" ? { disableAutoMode: nestedAutoMode } : {}), + }, + } + : {}), + }; +} + +// --settings travels to the SDK as options.settings, whose type only allows "disable". +// Shapes the schema rejects keep their current pass-through; resolveBypassAvailability +// already treats them as a bypass failure. +const decodeClaudeLaunchArgBypassSettingsOption = Schema.decodeUnknownOption(ClaudeBypassSettings); +function claudeSdkLaunchArgSettings(settings: ClaudeSdkSettings): ClaudeSdkSettings { + const decoded = decodeClaudeLaunchArgBypassSettingsOption(settings); + return Option.isNone(decoded) + ? settings + : (claudeSdkBypassSettings(decoded.value) as ClaudeSdkSettings); +} + const ClaudePromptCacheTtl = Schema.Literals(["5m", "1h"]); const decodeClaudePromptCacheTtl = Schema.decodeUnknownOption(ClaudePromptCacheTtl); const decodeClaudeSubagentCacheSettings = Schema.decodeUnknownEffect( @@ -2795,7 +2837,7 @@ function parseClaudeBackgroundTaskEntry( function fileNameFromClaudeTool(toolName: string, input: ClaudeNativeToolInput): string { return ( - firstStringInputField(input, ["file_path", "path", "filename", "fileName"]) ?? + firstStringInputField(input, ["file_path", "notebook_path", "path", "filename", "fileName"]) ?? `${toolName} result` ); } @@ -3320,6 +3362,69 @@ export const awaitClaudeApprovalDecision = Effect.fn("awaitClaudeApprovalDecisio return yield* Effect.raceFirst(Deferred.await(decision), cancellation); }); +// Web and mobile offer a session grant when options are omitted, and this +// path only ever answers once. +const CLAUDE_MCP_ELICITATION_OPTIONS: ReadonlyArray = [ + { decision: "cancel", label: "Cancel" }, + { decision: "decline", label: "Decline" }, + { decision: "accept", label: "Approve" }, +]; + +type ClaudeMcpElicitationContent = NonNullable; + +const isClaudeMcpElicitationValue = ( + value: unknown, +): value is ClaudeMcpElicitationContent[string] => + typeof value === "string" || + typeof value === "number" || + typeof value === "boolean" || + (Array.isArray(value) && value.every((item) => typeof item === "string")); + +const withoutSchemaDefaults = (schema: Record): Record => { + const properties = schema.properties; + if (typeof properties !== "object" || properties === null || Array.isArray(properties)) { + return schema; + } + return { + ...schema, + properties: Object.fromEntries( + Object.entries(properties).map(([key, field]) => [ + key, + typeof field === "object" && field !== null + ? Object.fromEntries(Object.entries(field).filter(([name]) => name !== "default")) + : field, + ]), + ), + }; +}; + +// The card collects consent only; schema defaults must never grant unseen permissions. +export function claudeMcpElicitationAcceptance( + request: ElicitationRequest, +): ElicitationResult | undefined { + if (request.mode === "url" || request.requestedSchema === undefined) return undefined; + const response = toMcpElicitationResponse( + { mode: "form", requestedSchema: withoutSchemaDefaults(request.requestedSchema) }, + "accept", + ); + // The helper leaves content out when the schema is not a form it can read. + const filled = response.content; + if ( + response.action !== "accept" || + typeof filled !== "object" || + filled === null || + Array.isArray(filled) + ) { + return undefined; + } + const content: ClaudeMcpElicitationContent = {}; + for (const [key, value] of Object.entries(filled)) { + if (!isClaudeMcpElicitationValue(value)) return undefined; + content[key] = value; + } + return { action: "accept", content }; +} + const awaitClaudeUserInputAnswers = Effect.fn("awaitClaudeUserInputAnswers")(function* ( answers: Deferred.Deferred, signal: AbortSignal, @@ -3851,6 +3956,9 @@ interface ClaudeLiveQueryContext { permissionMode: PermissionMode; stopping: boolean; readonly subagentsFromEarlierProcesses: ReadonlySet; + // Per process, not per session: a relative temp dir follows the cwd this CLI + // was launched in, and a later turn's cwd can differ. + readonly taskOutputRoot: string | undefined; } interface ActiveClaudeToolCall { @@ -4278,11 +4386,14 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( adapterOptions.settings, adapterOptions.environment, ).pipe(Effect.provideService(Path.Path, path)); + const hostPlatform = yield* HostProcess.Platform; const announcedBypassDowngrades = new Set(); const readBypassSettings = (filePath: string) => fileSystem.readFileString(filePath).pipe( Effect.flatMap(decodeClaudeBypassSettings), - Effect.catchReason("PlatformError", "NotFound", () => Effect.succeed({})), + Effect.catchReason("PlatformError", "NotFound", () => + Effect.succeed({} as typeof ClaudeBypassSettings.Type), + ), ); // Coder: the Agent SDK is type-only, so the settings cascade is read from its files (the // cached remote settings, user, project, local, and managed) instead of the SDK's @@ -4293,7 +4404,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( const sources = yield* Effect.forEach( [ path.join(homePath, "remote-settings.json"), - ...skillOverrideSettingsPaths( + ...claudeSettingsPaths( path, homePath, cwd ?? undefined, @@ -4333,13 +4444,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( if (Option.isSome(fromEnvironment)) return fromEnvironment.value; const platform = yield* HostProcess.Platform; const sources = yield* Effect.forEach( - skillOverrideSettingsPaths( - path, - homePath, - cwd ?? undefined, - platform, - adapterOptions.environment, - ), + claudeSettingsPaths(path, homePath, cwd ?? undefined, platform, adapterOptions.environment), (filePath) => fileSystem.readFileString(filePath).pipe( Effect.flatMap(decodeClaudeSubagentCacheSettings), @@ -4466,7 +4571,6 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( string, { readonly fiber: Fiber.Fiber; readonly stop: Deferred.Deferred } >(); - const taskOutputRoot = claudeTaskOutputRoot(adapterOptions.environment, path); // Waits for the tail's last read so its output lands before the call settles. const stopTaskOutputTail = (toolUseId: string) => Effect.suspend(() => { @@ -4831,7 +4935,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( }); // Claude has no recovered frame; the next assistant message proves recovery. - const completeProviderRetry = Effect.fn("ClaudeAdapterV2.completeProviderRetry")(function* ( + // Runs on every model output frame and is almost always a no-op; only a pending retry gets a span. + const completeProviderRetry = Effect.fnUntraced(function* ( context: ActiveClaudeTurnContext, updatedAt: DateTime.Utc, ) { @@ -4864,7 +4969,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( startedAt: providerRetry.startedAt, updatedAt, }), - }); + }).pipe(Effect.withSpan("ClaudeAdapterV2.completeProviderRetry")); }); const rememberProviderThread = @@ -6899,9 +7004,13 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( readonly context: ActiveClaudeTurnContext; readonly nativeItemId: string; readonly nativeRequestId: string; + readonly parentNodeId?: OrchestrationV2ExecutionNode["id"]; readonly requestKind: OrchestrationV2RuntimeRequest["kind"]; readonly prompt?: string; + readonly appName?: string; + readonly options?: ReadonlyArray; readonly presentation?: ReturnType; + readonly questions?: ReadonlyArray; }) { const createdAt = yield* DateTime.now; @@ -6931,10 +7040,12 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( id: nodeId, threadId: input.context.input.threadId, runId: input.context.input.runId, - parentNodeId: idAllocator.derive.nodeFromProviderItem({ - driver: CLAUDE_PROVIDER, - nativeItemId: input.nativeItemId, - }), + parentNodeId: + input.parentNodeId ?? + idAllocator.derive.nodeFromProviderItem({ + driver: CLAUDE_PROVIDER, + nativeItemId: input.nativeItemId, + }), rootNodeId: input.context.input.rootNodeId, kind: input.questions === undefined ? "approval_request" : "user_input_request", status: "waiting", @@ -6961,7 +7072,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( responseCapability: { type: "live", providerSessionId, - ...(input.questions === undefined ? { supportsFeedback: true } : {}), + ...(input.questions === undefined && input.requestKind !== "mcp-elicitation" + ? { supportsFeedback: true } + : {}), }, createdAt, resolvedAt: null, @@ -6987,6 +7100,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( requestId, requestKind: input.requestKind as ProviderRequestKind, ...(input.prompt === undefined ? {} : { prompt: input.prompt }), + ...(input.appName === undefined ? {} : { appName: input.appName }), + ...(input.options === undefined ? {} : { options: input.options }), ...input.presentation, } : { @@ -8593,7 +8708,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( ? undefined : findToolCall(context, message.tool_use_id); if ( - taskOutputRoot !== undefined && + liveQuery.taskOutputRoot !== undefined && message.task_type === "local_bash" && // Older Claude Code builds omit the flag on foreground calls. message.is_backgrounded !== true && @@ -8606,7 +8721,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( }); const stop = yield* Deferred.make(); const fiber = yield* tailClaudeTaskOutput({ - root: taskOutputRoot, + root: liveQuery.taskOutputRoot, sessionId: message.session_id, taskId: message.task_id, stop, @@ -9367,6 +9482,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( toolName: Parameters[0], toolInput: Parameters[1], callbackOptions: Parameters[2], + options?: { readonly startToolCall?: boolean }, ) { const context = yield* Ref.get(activeTurn); if (context === null) { @@ -9380,6 +9496,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( const nativeRequestId = callbackOptions.toolUseID; const nativeToolInput = claudeNativeToolInputFromRecord(toolInput); const heldForEcho = context.heldRootFrames.length > 0; + const startToolCall = toolName !== "Agent" && options?.startToolCall !== false; if (toolName === "Agent") { rememberClaudeSubagentLaunch( subagentModelCatalog, @@ -9389,7 +9506,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( nativeToolInput, null, ); - } else if (!heldForEcho) { + } else if (startToolCall && !heldForEcho) { yield* ensureToolCallStarted({ context, nativeItemId: nativeRequestId, @@ -9406,7 +9523,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( ) { // The SDK blocks prompt echoes on permission answers; release held output except for ExitPlanMode. yield* releaseHeldRootFrames(context); - if (toolName !== "Agent") { + if (startToolCall) { yield* ensureToolCallStarted({ context, nativeItemId: nativeRequestId, @@ -9423,6 +9540,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( context, nativeItemId: nativeRequestId, nativeRequestId, + // No tool node exists for a control request to hang under. + ...(options?.startToolCall === false + ? { parentNodeId: context.input.rootNodeId } + : {}), requestKind: "user_input", questions, }); @@ -9601,6 +9722,125 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( const canUseTool: CanUseTool = (toolName, toolInput, callbackOptions) => runPromise(canUseToolEffect(toolName, toolInput, callbackOptions)); + const onElicitationEffect = Effect.fn("ClaudeAdapterV2.onElicitation")(function* ( + request: ElicitationRequest, + callbackOptions: Parameters>[1], + ) { + if (callbackOptions.signal.aborted) + return { action: "cancel" } satisfies ElicitationResult; + const acceptance = claudeMcpElicitationAcceptance(request); + const context = yield* Ref.get(activeTurn); + if (acceptance === undefined || context === null) { + yield* Effect.logWarning( + "Declined an MCP elicitation the approval card cannot answer.", + { + serverName: request.serverName, + mode: request.mode, + activeTurn: context !== null, + }, + ); + return { action: "decline" } satisfies ElicitationResult; + } + // The CLI waits on this answer, so a turn held for its prompt echo + // must show its output now, as canUseTool does. + if (context.heldRootFrames.length > 0) { + yield* releaseHeldRootFrames(context); + } + + // The SDK's requestId names this control request; T3 allocates its own + // runtime request id from it. + const nativeRequestId = `mcp-elicitation:${callbackOptions.requestId}`; + const artifacts = yield* buildApprovalRequestArtifacts({ + context, + nativeItemId: nativeRequestId, + nativeRequestId, + requestKind: "mcp-elicitation", + prompt: request.message, + appName: request.displayName ?? request.title ?? request.serverName, + options: CLAUDE_MCP_ELICITATION_OPTIONS, + }); + const decision = yield* Deferred.make(); + yield* Ref.update(pendingRuntimeRequests, (current) => { + const updated = new Map(current); + updated.set(String(artifacts.request.id), { + type: "approval", + requestKind: "mcp-elicitation", + decision, + }); + return updated; + }); + yield* Effect.all( + [ + emitProviderEvent({ + type: "node.updated", + driver: CLAUDE_PROVIDER, + node: artifacts.node, + }), + emitProviderEvent({ + type: "runtime_request.updated", + driver: CLAUDE_PROVIDER, + runtimeRequest: artifacts.request, + }), + emitProviderEvent({ + type: "turn_item.updated", + driver: CLAUDE_PROVIDER, + turnItem: artifacts.turnItem, + }), + ], + { concurrency: 1 }, + ); + + // Closing the query aborts the signal, so a stopped session settles + // this as cancel and a late response finds no pending request. + const { decision: resolved } = yield* awaitClaudeApprovalDecision( + decision, + callbackOptions.signal, + ).pipe( + Effect.ensuring( + Ref.update(pendingRuntimeRequests, (current) => { + const updated = new Map(current); + updated.delete(String(artifacts.request.id)); + return updated; + }), + ), + ); + if (resolved === "cancel" && callbackOptions.signal.aborted) { + const now = yield* DateTime.now; + yield* emitProviderEvent({ + type: "node.updated", + driver: CLAUDE_PROVIDER, + node: { ...artifacts.node, status: "cancelled", completedAt: now }, + }); + yield* emitProviderEvent({ + type: "turn_item.updated", + driver: CLAUDE_PROVIDER, + turnItem: { + ...artifacts.turnItem, + status: "cancelled", + completedAt: now, + updatedAt: now, + }, + }); + yield* emitProviderEvent({ + type: "runtime_request.updated", + driver: CLAUDE_PROVIDER, + runtimeRequest: { ...artifacts.request, status: "cancelled", resolvedAt: now }, + }); + } + // Never null: the SDK reads null as answered out of band and leaves + // the server waiting. Every accept is one-time; no grant is offered. + return resolved === "accept" + ? acceptance + : ({ + action: resolved === "cancel" ? "cancel" : "decline", + } satisfies ElicitationResult); + }); + + const onElicitation: NonNullable = ( + request, + callbackOptions, + ) => runPromise(onElicitationEffect(request, callbackOptions)); + const onUserDialog: NonNullable = ( request, callbackOptions, @@ -9665,6 +9905,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( requestId: callbackOptions.requestId, toolUseID: request.toolUseID ?? callbackOptions.requestId, }, + // The dialog is a control request, not a tool call, so no + // tool_result would ever settle a tool row started for it. + { startToolCall: false }, ); if (result.behavior !== "allow") return { behavior: "cancelled" as const }; const answers = @@ -9856,7 +10099,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( attachmentsDir, settings: adapterOptions.settings, ...(forkFrom === undefined ? {} : { forkFrom }), - ...(launchArgSettings === undefined ? {} : { sdkSettings: launchArgSettings }), + ...(launchArgSettings === undefined + ? {} + : { sdkSettings: claudeSdkLaunchArgSettings(launchArgSettings) }), environment: claudeQueryEnvironment( adapterOptions.environment, turnInput.appThread, @@ -9874,10 +10119,14 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( }), canUseTool, onUserDialog, + onElicitation, supportedDialogKinds: ["resume_return"], ...(appendSystemPromptFileText === undefined ? {} : { appendSystemPromptFileText }), ...(toolSearchOverride === undefined ? {} : { toolSearchOverride }), }); + const resolvedTurnCwd = yield* fileSystem + .realPath(turnInput.runtimePolicy.cwd ?? ".") + .pipe(Effect.orElseSucceed(() => turnInput.runtimePolicy.cwd)); const querySession = yield* queryRunner .open({ threadId: turnInput.threadId, @@ -9927,6 +10176,12 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( (subagent) => subagent.task.status === "running", ), ), + taskOutputRoot: claudeTaskOutputRoot( + adapterOptions.environment, + path, + resolvedTurnCwd, + hostPlatform, + ), }; yield* Ref.set(queryContext, context); yield* querySession.messages.pipe( diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeTaskOutputTail.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeTaskOutputTail.test.ts index 87992bab89169..8da21b20236e2 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeTaskOutputTail.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeTaskOutputTail.test.ts @@ -7,7 +7,7 @@ import * as FileSystem from "effect/FileSystem"; import * as Path from "effect/Path"; import * as Queue from "effect/Queue"; -import { tailClaudeTaskOutput } from "./ClaudeTaskOutputTail.ts"; +import { claudeTaskOutputRoot, tailClaudeTaskOutput } from "./ClaudeTaskOutputTail.ts"; const SESSION_ID = "0c91bb28-9e89-4889-bd22-3de9880233d3"; const TASK_ID = "bk6xreeye"; @@ -103,3 +103,39 @@ describe("tailClaudeTaskOutput", () => { ), ); }); + +describe("claudeTaskOutputRoot", () => { + it.effect.skipIf(process.getuid === undefined)( + "uses the temp dir Claude Code resolves from its environment on each platform", + () => + Effect.gen(function* () { + const path = yield* Path.Path; + const user = `claude-${process.getuid?.()}`; + const root = ( + environment: Record, + platform: NodeJS.Platform, + cwd: string | null = null, + ) => claudeTaskOutputRoot(environment, path, cwd, platform); + assert.equal(root({ TMPDIR: "/var/folders/x/T/" }, "linux"), `/var/folders/x/T/${user}`); + assert.equal(root({ TMP: "/tmp/user/1000" }, "linux"), `/tmp/user/1000/${user}`); + assert.equal(root({}, "linux"), `/tmp/${user}`); + assert.equal(root({ TMPDIR: "/var/folders/x/T/" }, "darwin"), `/tmp/${user}`); + assert.equal( + root({ CLAUDE_CODE_TMPDIR: "/srv/claude", TMPDIR: "/var/folders/x/T/" }, "linux"), + `/srv/claude/${user}`, + ); + assert.equal( + root({ CLAUDE_CODE_TMPDIR: "/srv/claude", TMPDIR: "/var/folders/x/T/" }, "darwin"), + `/srv/claude/${user}`, + ); + assert.equal( + root({ TMPDIR: "tmp" }, "linux", "/work/project"), + `/work/project/tmp/${user}`, + ); + assert.equal( + root({ CLAUDE_CODE_TMPDIR: "../claude" }, "darwin", "/work/project"), + `/work/claude/${user}`, + ); + }).pipe(Effect.provide(NodeServices.layer)), + ); +}); diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeTaskOutputTail.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeTaskOutputTail.ts index ff096cdad2913..0bd369eb4df3a 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeTaskOutputTail.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeTaskOutputTail.ts @@ -17,11 +17,21 @@ const MAX_READ_BYTES = 256 * 1024; export function claudeTaskOutputRoot( environment: Readonly> | undefined, path: Path.Path, + cwd: string | null, + platform: NodeJS.Platform, ): string | undefined { const uid = process.getuid?.(); if (uid === undefined) return undefined; - const base = environment?.CLAUDE_CODE_TMPDIR?.trim() || "/tmp"; - return path.join(base, `claude-${uid}`); + // Claude Code's macOS build falls back to /tmp and ignores the TMPDIR that + // launchd always sets. Its other builds use `os.tmpdir()`, which reads these + // from the environment it was launched with. A relative value is relative + // to Claude's working directory, not this server's. + const fallback = + platform === "darwin" + ? "/tmp" + : environment?.TMPDIR || environment?.TMP || environment?.TEMP || "/tmp"; + const base = environment?.CLAUDE_CODE_TMPDIR?.trim() || fallback; + return path.resolve(cwd ?? ".", base, `claude-${uid}`); } const locate = Effect.fn("ClaudeTaskOutputTail.locate")(function* (input: { diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts index 157cb14c4af25..2f7d418df0d9d 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts @@ -22,6 +22,7 @@ import { type ProviderApprovalDecision, ProviderInstanceId, ProviderSessionId, + ProviderSetupError, ProviderThreadId, ProviderTurnId, RunAttemptId, @@ -66,6 +67,8 @@ import * as Orchestrator from "../Orchestrator.ts"; import * as ProviderReplayHarness from "../testkit/ProviderReplayHarness.ts"; import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"; import type * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests"; +import * as ModelManifest from "../../provider/ModelManifest.ts"; +import { resolveProviderCompatibility } from "../../provider/providerCompatibility.ts"; import * as CodexAdapterV2 from "./CodexAdapterV2.ts"; import { withCodexReplayChildMetadata } from "./CodexAdapterV2.testkit.ts"; import * as CodexAdapterV2Testkit from "./CodexAdapterV2.testkit.ts"; @@ -77,6 +80,7 @@ const encodeStringJson = Schema.encodeEffect(Schema.fromJsonString(Schema.String const isProviderAdapterEventStreamError = Schema.is( ProviderAdapter.ProviderAdapterEventStreamError, ); +const encodeUnknownJson = Schema.encodeUnknownSync(Schema.fromJsonString(Schema.Unknown)); describe("CodexAdapterV2 context usage", () => { it("uses the current context rather than cumulative processed tokens", () => { @@ -913,6 +917,7 @@ describe("CodexAdapterV2 session initialize", () => { const openReplaySession = ( transcript: CodexReplay.CodexAppServerReplayTranscript, beforeEmitInbound?: CodexReplay.CodexAppServerReplayDriver["beforeEmitInbound"], + compatibility?: CodexAdapterV2.CodexAdapterV2Options["compatibility"], ) => Effect.gen(function* () { const driver = yield* CodexReplay.makeReplayDriver( @@ -924,6 +929,7 @@ describe("CodexAdapterV2 session initialize", () => { instanceId: CodexAdapterV2.CODEX_DEFAULT_INSTANCE_ID, settings: DEFAULT_CODEX_SETTINGS, environment: {}, + ...(compatibility === undefined ? {} : { compatibility }), clientFactory: { open: (openInput) => Layer.build(CodexReplay.layerReplayWithDriver(driver)).pipe( @@ -1055,6 +1061,54 @@ describe("CodexAdapterV2 session initialize", () => { ), ); + it.effect("refuses to open a session on a Codex version marked broken", () => + Effect.gen(function* () { + const [initialize] = replayPreamble("broken-version"); + const failure = yield* openReplaySession( + makeCodexReplayTranscript({ + scenario: "broken-version", + entries: [ + initialize!, + { + type: "emit_inbound", + label: "initialize", + frame: { + id: 1, + result: { + userAgent: "T3 Code/0.144.3 (Mac OS 15.5.0; x86_64) unknown (T3 Code; 0.1.5)", + codexHome: "/tmp/codex-home", + platformFamily: "unix", + platformOs: "macos", + }, + }, + }, + ], + }), + undefined, + (version) => + Effect.succeed( + resolveProviderCompatibility( + ModelManifest.BUNDLED_MODEL_MANIFEST.compatibility, + CodexAdapterV2.CODEX_DRIVER_KIND, + version, + ), + ), + ).pipe(Effect.flip); + + assert.instanceOf(failure, ProviderAdapter.ProviderAdapterOpenSessionError); + assert.instanceOf(failure.cause, ProviderSetupError); + assert.equal( + failure.cause.message, + "Codex 0.144.3 does not work with this T3 Code release. Update Codex to >=0.159.0, then retry.", + ); + }).pipe( + Effect.scoped, + Effect.provide( + Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer), + ), + ), + ); + it.effect("completes the handshake after a caller is interrupted mid-initialize", () => Effect.gen(function* () { const initializeAwaitingResponse = yield* Deferred.make(); @@ -1124,6 +1178,7 @@ describe("CodexAdapterV2 post-settle continuation", () => { Effect.void, onRequest: (method: string, params: unknown) => Effect.Effect = () => Effect.void, readChildMetadata?: Parameters[2], + beforeEmitInbound?: CodexReplay.CodexAppServerReplayDriver["beforeEmitInbound"], ) => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; @@ -1134,9 +1189,13 @@ describe("CodexAdapterV2 post-settle continuation", () => { const changes = yield* makeChangeSignal; const continuationRequests: Array = []; + const driver = yield* CodexReplay.makeReplayDriver( + transcript, + beforeEmitInbound === undefined ? {} : { beforeEmitInbound }, + ); const clientFactory: CodexAdapterV2.CodexAppServerClientFactoryShape = { open: (openInput) => - Layer.build(CodexReplay.layerReplay(transcript)).pipe( + Layer.build(CodexReplay.layerReplayWithDriver(driver)).pipe( Effect.mapError( (cause) => new ProviderAdapter.ProviderAdapterOpenSessionError({ @@ -1247,6 +1306,105 @@ describe("CodexAdapterV2 post-settle continuation", () => { }; }); + it.effect.each(["before", "after"] as const)( + "rejects a previous run's native turn when it completes %s the new start response", + (completionOrder) => + Effect.gen(function* () { + const nativeThreadId = "reused-turn-thread"; + const nativeTurnId = "reused-turn"; + const preamble = codexReplayPreamble({ nativeThreadId, nativeTurnId, prompt: "work" }); + const retry = codexReplayPreamble({ + nativeThreadId, + nativeTurnId: "retry-turn", + prompt: "work", + }); + const completion: CodexReplay.CodexAppServerReplayEntry = { + type: "emit_inbound", + label: "original turn completed", + frame: { + method: "turn/completed", + params: { + threadId: nativeThreadId, + turn: makeCodexReplayTurn({ id: nativeTurnId, status: "completed" }), + }, + }, + }; + const harness = yield* makeCodexReplayHarness( + makeCodexReplayTranscript({ + scenario: `reused-turn-${completionOrder}`, + entries: [ + ...preamble, + withReplayRequestId(preamble[5]!, 4), + ...(completionOrder === "before" ? [completion] : []), + withReplayRequestId(preamble[6]!, 4), + ...(completionOrder === "after" ? [completion] : []), + ...retry.slice(5, 7).map((entry) => withReplayRequestId(entry, 5)), + { + type: "emit_inbound", + label: "retry completed", + frame: { + method: "turn/completed", + params: { + threadId: nativeThreadId, + turn: makeCodexReplayTurn({ id: "retry-turn", status: "completed" }), + }, + }, + }, + ], + }), + ); + const first = makeCodexTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make("original"), + text: "work", + }); + yield* harness.runtime.startTurn(first); + const result = yield* harness.runtime + .startTurn({ + ...first, + runId: RunId.make("replacement"), + attemptId: RunAttemptId.make("replacement"), + rootNodeId: NodeId.make("replacement"), + runOrdinal: 2, + providerTurnOrdinal: 2, + }) + .pipe(Effect.result); + assert.equal(result._tag, "Failure", "a different run must not adopt the original turn"); + if (result._tag !== "Failure") return; + const failure = result.failure; + assert.equal(failure._tag, "ProviderAdapterTurnStartError"); + assert.instanceOf(failure.cause, ProviderAdapter.ProviderAdapterProtocolError); + yield* harness.firstTerminal; + assert.deepEqual( + harness.terminalEvents().map((event) => event.runOrdinal), + [1], + ); + yield* harness.runtime.startTurn({ + ...first, + runId: RunId.make("retry"), + attemptId: RunAttemptId.make("retry"), + rootNodeId: NodeId.make("retry"), + runOrdinal: 3, + providerTurnOrdinal: 3, + }); + yield* harness.awaitTerminal(2); + assert.deepEqual( + harness.terminalEvents().map((event) => [event.runOrdinal, event.status]), + [ + [1, "completed"], + [3, "completed"], + ], + ); + }).pipe( + Effect.scoped, + Effect.provide( + Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer), + ), + ), + ); + it.effect.each(["supported", "unsupported", "invalid"] as const)( "delivers native history with %s app-server protocol", (response) => @@ -3188,7 +3346,7 @@ describe("CodexAdapterV2 post-settle continuation", () => { answers: ReadonlyArray<{ readonly id: string; readonly text: string; - readonly phase?: "commentary" | "final_answer" | null; + readonly phase?: "commentary" | "partial_answer" | "final_answer" | null; readonly omitPhase?: boolean; readonly streamed?: boolean; readonly completionDelayMs?: number; @@ -3283,6 +3441,243 @@ describe("CodexAdapterV2 post-settle continuation", () => { }); }; + it.effect.each([false, true])( + "preserves a final answer that repeats a preceding partial answer (streamed: %s)", + (streamed) => + Effect.scoped( + Effect.gen(function* () { + const transcript = finalAnswerTranscript("codex-partial-then-final", [ + { id: "partial", text: "CODEX_RECOVERY_OK", phase: "partial_answer", streamed }, + { id: "final", text: "CODEX_RECOVERY_OK", phase: "final_answer", streamed }, + ]); + const harness = yield* makeCodexReplayHarness(transcript); + const now = yield* DateTime.now; + yield* harness.runtime.startTurn( + makeCodexTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now, + attemptId: RunAttemptId.make("attempt-codex-partial-then-final"), + text: "Reply with the requested recovery marker.", + }), + ); + yield* harness.awaitTerminal(1); + assert.deepEqual( + assistantMessages(harness.events).map((event) => event.message.text), + ["CODEX_RECOVERY_OK", "CODEX_RECOVERY_OK"], + ); + }).pipe( + Effect.provide( + Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer), + ), + ), + ), + ); + + it.effect.each([ + { + name: "a text reply gains each saved image once", + reply: "Done.", + images: [ + { savedPath: "/tmp/a.png" }, + { savedPath: "/tmp/a.png" }, + { savedPath: "/tmp/failed.png", status: "failed" }, + { + savedPath: "/tmp/limited.png", + failure: { type: "usageLimitExceeded", limitId: "image-generation" }, + }, + { savedPath: null }, + ], + expected: "Done.\n\n![]()", + }, + { + name: "an image-only result becomes the reply", + reply: null, + images: [{ savedPath: "/tmp/a.png" }], + expected: "![]()", + }, + ])("projects generated images: $name", ({ reply, images, expected }) => + Effect.gen(function* () { + const scenario = "codex-generated-images"; + const result = "aW1hZ2UtYnl0ZXMtbXVzdC1uZXZlci1yZWFjaC1jbGllbnRz"; + const base = finalAnswerTranscript( + scenario, + reply === null ? [] : [{ id: "answer", text: reply }], + ); + const harness = yield* makeCodexReplayHarness( + makeCodexReplayTranscript({ + scenario, + entries: [ + ...base.entries.slice(0, -1), + ...images.map((image, index) => ({ + type: "emit_inbound" as const, + frame: { + method: "item/completed", + params: { + threadId: `native-${scenario}-thread`, + turnId: `native-${scenario}-turn`, + item: { + type: "imageGeneration", + id: `image-${index}`, + status: "completed", + result, + ...image, + }, + }, + }, + })), + ...base.entries.slice(-1), + ], + }), + ); + yield* harness.runtime.startTurn( + makeCodexTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make("attempt-generated-images"), + text: "Reply with the requested recovery marker.", + }), + ); + yield* harness.firstTerminal; + + const messages = assistantMessages(harness.events); + assert.strictEqual(messages.at(-1)?.message.text, expected); + assert.strictEqual(messages.at(-1)?.message.streaming, false); + assert.strictEqual(new Set(messages.map(({ message }) => message.id)).size, 1); + // Image bytes stay with Codex; clients load the saved file. + assert.notInclude(encodeUnknownJson(harness.events), result); + }).pipe( + Effect.scoped, + Effect.provide( + Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer), + ), + ), + ); + + it.effect.each(["complete", "stop"] as const)( + "retains generating images after settlement until %s", + (outcome) => + Effect.gen(function* () { + const scenario = "codex-retained-image"; + const base = finalAnswerTranscript(scenario, [{ id: "answer", text: "Done." }]); + const completions = { + first: yield* Deferred.make(), + second: yield* Deferred.make(), + }; + const projected = yield* Deferred.make(); + const drained = yield* Deferred.make(); + const firstText = "Done.\n\n![]()"; + const imageEvent = (image: "first" | "second", phase: "started" | "completed") => ({ + type: "emit_inbound" as const, + label: `${image}-${phase}`, + frame: { + method: `item/${phase}`, + params: { + threadId: `native-${scenario}-thread`, + turnId: `native-${scenario}-turn`, + item: { + type: "imageGeneration", + id: image, + status: phase === "started" ? "inProgress" : "completed", + result: "", + ...(phase === "completed" ? { savedPath: `/tmp/${image}.png` } : {}), + }, + }, + }, + }); + const harness = yield* makeCodexReplayHarness( + makeCodexReplayTranscript({ + scenario, + entries: [ + ...base.entries.slice(0, -2), + imageEvent("first", "started"), + imageEvent("second", "started"), + ...base.entries.slice(-2), + imageEvent("first", "completed"), + imageEvent("second", "completed"), + ], + }), + (event) => + event.type === "message.updated" && event.message.text === firstText + ? Deferred.succeed(projected, undefined) + : event.type === "background_work.updated" && !event.pending + ? Deferred.succeed(drained, undefined) + : Effect.void, + undefined, + undefined, + (entry) => + entry.label === "first-completed" + ? Deferred.await(completions.first) + : entry.label === "second-completed" + ? Deferred.await(completions.second) + : Effect.void, + ); + yield* harness.runtime.startTurn( + makeCodexTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make("attempt-retained-image"), + text: "Reply with the requested recovery marker.", + }), + ); + yield* harness.firstTerminal; + const pending = Effect.all([ + harness.runtime.hasPendingBackgroundWorkForThread!(harness.providerThread), + harness.hasPendingBackgroundWork, + ]); + assert.deepEqual(yield* pending, [true, true]); + assert.strictEqual(assistantMessages(harness.events).at(-1)?.message.text, "Done."); + const cleared = () => + harness.events.some((e) => e.type === "background_work.updated" && !e.pending); + + if (outcome === "complete") { + yield* Deferred.succeed(completions.first, undefined); + yield* Deferred.await(projected); + // The second image is still generating, so the first must not release the turn. + assert.deepEqual(yield* pending, [true, true]); + assert.isFalse(cleared()); + yield* Deferred.succeed(completions.second, undefined); + } else { + yield* harness.runtime.interruptTurn({ + providerThread: harness.providerThread, + providerTurnId: harness.terminalEvents()[0]!.providerTurnId, + requestRuntimeRestart: true, + }); + } + yield* Deferred.await(drained); + assert.deepEqual(yield* pending, [false, false]); + assert.lengthOf(harness.terminalEvents(), 1); + // Run ingestion follows these updates, so they must bracket the terminal and the images. + const indexOf = (predicate: (event: ProviderAdapter.ProviderAdapterV2Event) => boolean) => + harness.events.findIndex(predicate); + const started = indexOf((e) => e.type === "background_work.updated" && e.pending); + const terminal = indexOf((e) => e.type === "turn.terminal"); + const finished = indexOf((e) => e.type === "background_work.updated" && !e.pending); + assert.isAtLeast(started, 0); + assert.isAbove(terminal, started); + assert.isAbove(finished, terminal); + if (outcome === "complete") { + const messages = assistantMessages(harness.events); + const projectedBoth = indexOf( + (e) => + e.type === "message.updated" && + e.message.text === `${firstText}\n\n![]()`, + ); + assert.isAbove(projectedBoth, terminal); + assert.isAbove(finished, projectedBoth); + assert.strictEqual(messages.at(-1)?.message.id, messages[0]?.message.id); + assert.strictEqual(messages.at(-1)?.message.streaming, false); + } + }).pipe( + Effect.scoped, + Effect.provide( + Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer), + ), + ), + ); + it.effect("suppresses a trailing empty final answer after a non-empty final answer", () => Effect.scoped( Effect.gen(function* () { @@ -4560,6 +4955,132 @@ describe("CodexAdapterV2 post-settle continuation", () => { ), ); + const interruptSubagentTurnAfterStopTranscript = makeCodexReplayTranscript({ + scenario: "codex-interrupt-subagent-turn-after-stop", + entries: [ + ...interruptSubagentCommandTranscript.entries.slice(0, -1), + { + type: "emit_inbound", + label: "turn/started/child-after-stop", + frame: { + method: "turn/started", + params: { + threadId: INTERRUPT_CHILD_NATIVE_THREAD, + turn: makeCodexReplayTurn({ + id: INTERRUPT_LATE_CHILD_NATIVE_TURN, + status: "inProgress", + }), + }, + }, + }, + { + type: "expect_outbound", + label: "turn/interrupt/child-after-stop", + frame: { + id: 7, + method: "turn/interrupt", + params: { + threadId: INTERRUPT_CHILD_NATIVE_THREAD, + turnId: INTERRUPT_LATE_CHILD_NATIVE_TURN, + }, + }, + }, + { + type: "emit_inbound", + label: "turn/interrupt/child-after-stop", + frame: { id: 7, result: {} }, + }, + { + type: "emit_inbound", + label: "thread/goal/updated/root", + frame: { + method: "thread/goal/updated", + params: { + threadId: INTERRUPT_NATIVE_THREAD, + turnId: null, + goal: { + threadId: INTERRUPT_NATIVE_THREAD, + objective: INTERRUPT_PROMPT, + status: "paused", + tokenBudget: null, + tokensUsed: 0, + timeUsedSeconds: 0, + createdAt: 1782622440, + updatedAt: 1782622450, + }, + }, + }, + }, + { type: "runtime_exit", status: "success" }, + ], + }); + + it.effect( + "keeps reading notifications while interrupting a child turn that starts after Stop", + () => + Effect.scoped( + Effect.gen(function* () { + const harness = yield* makeCodexReplayHarness(interruptSubagentTurnAfterStopTranscript); + const now = yield* DateTime.now; + + yield* harness.runtime.startTurn( + makeCodexTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now, + attemptId: RunAttemptId.make("attempt-codex-interrupt-subagent-turn-after-stop"), + text: INTERRUPT_PROMPT, + }), + ); + yield* harness.until(() => + harness.events.some( + (event) => + event.type === "turn_item.updated" && + event.turnItem.type === "command_execution" && + event.turnItem.nativeItemRef?.nativeId === INTERRUPT_CHILD_COMMAND_ITEM && + event.turnItem.status === "running", + ), + ); + const providerTurnId = harness.events.find( + ( + event, + ): event is Extract< + ProviderAdapter.ProviderAdapterV2Event, + { type: "provider_turn.updated" } + > => event.type === "provider_turn.updated" && event.threadId === harness.threadId, + )?.providerTurn.id; + assert.isDefined(providerTurnId); + + yield* harness.runtime.interruptTurn({ + providerThread: harness.providerThread, + providerTurnId, + }); + yield* harness.awaitTerminal(1); + + // The TestClock never reaches the interrupt's timeout, so this arrives only + // if the reader is free to read the interrupt response and the frame after it. + yield* harness.until(() => + harness.events.some( + (event) => + event.type === "provider_thread.updated" && + event.providerThread.goal?.status === "paused", + ), + ); + assert.isFalse( + harness.events.some( + (event) => + event.type === "provider_turn.updated" && + event.providerTurn.nativeTurnRef?.nativeId === INTERRUPT_LATE_CHILD_NATIVE_TURN, + ), + ); + }).pipe( + Effect.provide( + Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer), + ), + ), + ), + ); + it.effect.each([ { name: "usage", diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts index 9cd00c3331341..61cc4b41a502a 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts @@ -35,7 +35,8 @@ import { OrchestrationV2ProviderGoal, parseGoalCommand, ProviderDriverKind, - type ProviderSetupError, + ProviderSetupError, + type ServerProviderCompatibilityAdvisory, } from "@t3tools/contracts"; import { SKILL_MENTION_PATTERN } from "@t3tools/shared/composerInlineTokens"; import { AgentScope } from "@t3tools/shared/AgentScope"; @@ -46,6 +47,7 @@ import { dynamicToolTitle, } from "@t3tools/shared/toolActivity"; import { getModelSelectionStringOptionValue, modelSelectionsEqual } from "@t3tools/shared/model"; +import { appendGeneratedImageEmbeds } from "@t3tools/shared/generatedImageMarkdown"; import { resolveSpawnCommand } from "@t3tools/shared/shell"; import type { ChatAttachment, @@ -1519,7 +1521,7 @@ export type CodexAdapterV2DriverEnv = export const createCodexAdapterV2 = Effect.fn("CodexAdapterV2.createCodexAdapterV2")(function* ( { instanceId, environment, enabled, config }: ProviderAdapterDriverCreateInput, - hooks: Pick = {}, + hooks: Pick = {}, ) { const clientFactory = yield* CodexAppServerClientFactory; const continuationRequests = yield* ProviderContinuationRequests.ProviderContinuationRequests; @@ -1570,6 +1572,9 @@ export interface CodexAdapterV2Options { readonly clientFactory: CodexAppServerClientFactoryShape; readonly onUsageLimits?: ManagedServerProvider["applyUsageLimits"]; readonly resolveRuntime?: Effect.Effect; + readonly compatibility?: ( + version: string, + ) => Effect.Effect; readonly crypto: Crypto.Crypto; readonly fileSystem: FileSystem.FileSystem; readonly idAllocator: IdAllocator.IdAllocatorV2["Service"]; @@ -1676,24 +1681,41 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( return; } - yield* client + const userAgent = yield* client .request("initialize", { clientInfo: buildCodexInitializeParams().clientInfo, capabilities: CODEX_CLIENT_CAPABILITIES, }) .pipe( + Effect.map((response) => response.userAgent), Effect.catchTags({ // Interruption can leave Codex initialized before the local flag is set. CodexAppServerRequestError: (error) => error.code === -32600 && error.errorMessage === "Already initialized" - ? Effect.void + ? Effect.succeed(null) : Effect.fail(error), }), ); + const version = userAgent && /\/(\S+)/u.exec(userAgent)?.[1]; + const advisory = + version && adapterOptions.compatibility + ? yield* adapterOptions.compatibility(version) + : undefined; + if (advisory?.status === "broken") { + const target = + advisory.recommendedVersion ?? advisory.recommendedRange ?? "a newer version"; + return yield* new ProviderSetupError({ + instanceId: adapterOptions.instanceId, + operation: "runtime", + detail: `Codex ${version} does not work with this T3 Code release. Update Codex to ${target}, then retry.`, + }); + } yield* client.notify("initialized", undefined); yield* Ref.set(initialized, true); }), ); + // A failed open is discarded, so the next turn spawns the updated binary. + if (adapterOptions.compatibility !== undefined) yield* ensureInitialized; const now = yield* DateTime.now; const session = providerSession({ providerSessionId: input.providerSessionId, @@ -1813,6 +1835,23 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( const offeredContinuationItemsByTurn = yield* Ref.make(new Map>()); const finalAnswerItemIdsByTurn = yield* Ref.make(new Map>()); const completedFinalAnswerTextsByTurn = yield* Ref.make(new Map>()); + const imageTurns = new Map< + string, + { + readonly running: Set; + readonly saved: Map; + pending: boolean; + answer?: { readonly id: string; readonly text: string }; + } + >(); + const imageTurn = (nativeTurnId: string) => { + let state = imageTurns.get(nativeTurnId); + if (state === undefined) { + state = { running: new Set(), saved: new Map(), pending: false }; + imageTurns.set(nativeTurnId, state); + } + return state; + }; // Serialize native completion and interrupt timeout so only one publishes terminal events. const turnTerminalizationPermit = yield* Semaphore.make(1); const goalsByNativeThread = new Map(); @@ -1839,6 +1878,27 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( const emitProviderEvent = (event: ProviderAdapter.ProviderAdapterV2Event) => Queue.offer(events, event).pipe(Effect.asVoid); + const emitImageWorkState = + /* Provider events include streaming deltas; avoid a span for each update. */ + Effect.fnUntraced(function* (context: ActiveCodexTurnContext, pending: boolean) { + const state = imageTurns.get(context.nativeTurnId); + if (state === undefined || state.pending === pending) return; + state.pending = pending; + yield* emitProviderEvent({ + type: "background_work.updated", + driver: CODEX_PROVIDER, + providerTurnId: context.providerTurnId, + pending, + }); + }); + + const clearImageWork = Effect.fn("CodexAdapterV2.clearImageWork")(function* ( + context: ActiveCodexTurnContext, + ) { + yield* emitImageWorkState(context, false); + imageTurns.delete(context.nativeTurnId); + }); + // Setting a goal passes through cleared and paused states; publish only the result. const goalUpdatesHeld = new Set(); const emitGoalUpdate = Effect.fnUntraced(function* (nativeThreadId: string) { @@ -1877,7 +1937,8 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( }); // Only new model output proves recovery; local item/completed can arrive during upstream retries. - const completeProviderRetry = Effect.fn("CodexAdapterV2.completeProviderRetry")(function* ( + // Runs on every model output frame and is almost always a no-op; only a pending retry gets a span. + const completeProviderRetry = Effect.fnUntraced(function* ( context: ActiveCodexTurnContext, updatedAt: DateTime.Utc, ) { @@ -1897,7 +1958,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( type: "turn_item.updated", driver: CODEX_PROVIDER, turnItem: codexRetryTurnItem(context, providerRetry, "completed", updatedAt), - }); + }).pipe(Effect.withSpan("CodexAdapterV2.completeProviderRetry")); }); const registerRootTurn = Effect.fn("CodexAdapterV2.registerRootTurn")(function* (input: { @@ -2107,6 +2168,10 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( if ((yield* Ref.get(pendingMcpAppCaptures)).has(nativeTurnId)) { return true; } + const images = imageTurns.get(nativeTurnId); + if (images !== undefined && (images.running.size > 0 || images.saved.size > 0)) { + return true; + } const tools = (yield* Ref.get(runningDynamicToolsByTurn)).get(nativeTurnId); if (tools === undefined) { return false; @@ -2149,6 +2214,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( updated.delete(nativeTurnId); return updated; }); + imageTurns.delete(nativeTurnId); yield* Ref.update(finalAnswerItemIdsByTurn, (current) => { if (!current.has(nativeTurnId)) { return current; @@ -2470,31 +2536,33 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( while (ancestor !== undefined) { if (terminalizedNativeTurns.has(ancestor.nativeTurnId)) { const nativeThreadId = yield* getNativeThreadId(subagent.providerThread); - const interrupted = yield* client + // Fork requests off the notification reader so it can read their responses. + yield* client .request("turn/interrupt", { threadId: nativeThreadId, turnId: turn.nativeTurnId, }) .pipe( - Effect.as(true), Effect.catch((cause) => Effect.logWarning("orchestration-v2.codex-late-subagent-interrupt-failed", { nativeThreadId, nativeTurnId: turn.nativeTurnId, cause, - }).pipe(Effect.as(false)), + }), ), - Effect.timeoutOption("10 seconds"), - ); - if (Option.isNone(interrupted)) { - yield* Effect.logWarning( - "orchestration-v2.codex-late-subagent-interrupt-timeout", - { - nativeThreadId, - nativeTurnId: turn.nativeTurnId, - }, + Effect.timeoutOrElse({ + duration: "10 seconds", + orElse: () => + Effect.logWarning( + "orchestration-v2.codex-late-subagent-interrupt-timeout", + { + nativeThreadId, + nativeTurnId: turn.nativeTurnId, + }, + ), + }), + Effect.forkIn(scope), ); - } return; } ancestor = ancestor.subagent?.parentContext; @@ -3360,6 +3428,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( turnItem: artifacts.turnItem, }); if (finalAnswerItem && update.completed) { + imageTurn(update.turnId).answer = { id: update.itemId, text: update.text }; yield* Ref.update(completedFinalAnswerTextsByTurn, (current) => { const updated = new Map(current); const texts = new Set(updated.get(update.turnId) ?? []); @@ -4668,7 +4737,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( } if (payload.item.type === "agentMessage") { - if (payload.item.phase !== "commentary") { + if (payload.item.phase == null || payload.item.phase === "final_answer") { yield* Ref.update(finalAnswerItemIdsByTurn, (current) => { const updated = new Map(current); const itemIds = new Set(updated.get(payload.turnId) ?? []); @@ -4680,6 +4749,14 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( return; } + if (payload.item.type === "imageGeneration") { + if (!(yield* Ref.get(interruptingNativeTurns)).has(payload.turnId)) { + imageTurn(payload.turnId).running.add(payload.item.id); + yield* emitImageWorkState(context, true); + } + return; + } + if (payload.item.type === "commandExecution") { if (!codexItemStatus(payload.item.status).completed) { yield* trackRunningCommandItem(payload.turnId, { @@ -4958,6 +5035,25 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( return; } + if (payload.item.type === "imageGeneration") { + if ((yield* Ref.get(interruptingNativeTurns)).has(payload.turnId)) return; + const { id, savedPath, status, failure } = payload.item; + const state = imageTurn(payload.turnId); + if (savedPath?.trim() && status === "completed" && failure == null) { + state.saved.set(id, savedPath); + } + // Idle release runs concurrently, so keep this work pending through projection. + if (settled) { + yield* projectGeneratedImages(context, payload.turnId); + } + state.running.delete(id); + yield* emitImageWorkState(context, state.running.size > 0 || state.saved.size > 0); + if (settled) { + yield* releaseSettledTurnIfIdle(payload.turnId); + } + return; + } + if (payload.item.type !== "agentMessage") { return; } @@ -5004,7 +5100,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( return; } - const finalAnswer = payload.item.phase !== "commentary"; + const finalAnswer = payload.item.phase == null || payload.item.phase === "final_answer"; if (finalAnswer) { yield* Ref.update(finalAnswerItemIdsByTurn, (current) => { const updated = new Map(current); @@ -5809,6 +5905,39 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( }, ); + const projectGeneratedImages = Effect.fn("CodexAdapterV2.projectGeneratedImages")( + function* (context: ActiveCodexTurnContext, nativeTurnId: string) { + const state = imageTurns.get(nativeTurnId); + const firstImageItemId = state?.saved.keys().next().value; + if (state === undefined || firstImageItemId === undefined) return; + const finalAnswer = state.answer; + const text = appendGeneratedImageEmbeds(finalAnswer?.text ?? "", state.saved.values()); + if (text === (finalAnswer?.text ?? "")) { + state.saved.clear(); + return; + } + const message = { id: finalAnswer?.id ?? firstImageItemId, text }; + state.answer = message; + const artifacts = yield* buildAgentMessageArtifacts(context, message, true); + yield* emitProviderEvent({ + type: "node.updated", + driver: CODEX_PROVIDER, + node: artifacts.node, + }); + yield* emitProviderEvent({ + type: "message.updated", + driver: CODEX_PROVIDER, + message: artifacts.message, + }); + yield* emitProviderEvent({ + type: "turn_item.updated", + driver: CODEX_PROVIDER, + turnItem: artifacts.turnItem, + }); + state.saved.clear(); + }, + ); + const finalizeCodexTurn = (input: { readonly context: ActiveCodexTurnContext; readonly nativeTurnId: string; @@ -5882,6 +6011,15 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( input.completedAt, ); } + yield* projectGeneratedImages(input.context, input.nativeTurnId); + if (input.status !== "completed") { + yield* clearImageWork(input.context); + } else { + const images = imageTurns.get(input.nativeTurnId); + if (images !== undefined) { + yield* emitImageWorkState(input.context, images.running.size > 0); + } + } const nativeThreadId = input.context.providerThread.nativeThreadRef?.nativeId; const activation = nativeThreadId == null ? undefined : goalActivations.get(nativeThreadId); @@ -6102,6 +6240,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( updated.delete(input.nativeTurnId); return updated; }); + imageTurns.delete(input.nativeTurnId); yield* Ref.update(finalAnswerItemIdsByTurn, (current) => { if (!current.has(input.nativeTurnId)) { return current; @@ -6481,7 +6620,25 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( else next.delete(threadId); return next; }); + // Completion can remove the old context while turn/start is in flight. + const previousTurns = new Map([ + ...(yield* Ref.get(settledTurns)), + ...(yield* Ref.get(activeTurns)), + ]); const started = yield* client.request("turn/start", turnStartParams); + const previousTurn = + previousTurns.get(started.turn.id) ?? + (yield* Ref.get(activeTurns)).get(started.turn.id); + if ( + previousTurn !== undefined && + previousTurn.input.attemptId !== turnInput.attemptId + ) { + return yield* new ProviderAdapter.ProviderAdapterProtocolError({ + driver: CODEX_PROVIDER, + detail: + "Codex returned a turn belonging to an earlier run. Stop the existing turn before retrying this message.", + }); + } // A turn/started notification may have registered and completed this turn already. if ((yield* Ref.get(pendingRootTurns)).get(threadId) !== turnInput) return; yield* registerRootTurn({ @@ -6716,6 +6873,9 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( if ((yield* Ref.get(pendingMcpAppCaptures)).size > 0) { return true; } + for (const state of imageTurns.values()) { + if (state.running.size > 0 || state.saved.size > 0) return true; + } for (const subagent of (yield* Ref.get(subagentThreads)).values()) { if (subagent.task.status === "running") { return true; @@ -7386,6 +7546,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( updated.delete(context.nativeTurnId); return updated; }); + yield* clearImageWork(context); yield* releaseSettledTurnIfIdle(context.nativeTurnId); } }), diff --git a/apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts b/apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts index ad2bc395d083f..5053e8d568767 100644 --- a/apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts +++ b/apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts @@ -159,6 +159,11 @@ const seedParentWithTerminalTask = (input: { readonly deliveryState: "delivered" | "claimed" | "acknowledged" | "disposed"; readonly completionWake?: "always" | "settled_only"; readonly deliveryTaskIds?: ReadonlyArray; + readonly parentRunStatus?: "running" | "completed"; + readonly siblingTasks?: ReadonlyArray<{ + readonly id: NodeId; + readonly deliveryState: "delivered" | "claimed"; + }>; readonly now: DateTime.Utc; }) => Effect.gen(function* () { @@ -241,10 +246,10 @@ const seedParentWithTerminalTask = (input: { userMessageId: MessageId.make(`message:seed-user:${input.threadId}`), rootNodeId: input.rootNodeId, activeAttemptId: null, - status: "running", + status: input.parentRunStatus ?? "running", requestedAt: input.now, startedAt: input.now, - completedAt: null, + completedAt: input.parentRunStatus === "completed" ? input.now : null, checkpointId: null, contextHandoffId: null, delegatedCompletion: { @@ -261,22 +266,25 @@ const seedParentWithTerminalTask = (input: { }, }, }, - { - id: EventId.make(`event:seed-task:${input.threadId}`), - type: "subagent.updated", + ...[ + { id: input.taskId, deliveryState: input.deliveryState }, + ...(input.siblingTasks ?? []), + ].map((task) => ({ + id: EventId.make(`event:seed-task:${input.threadId}:${task.id}`), + type: "subagent.updated" as const, threadId: input.threadId, runId: input.runId, - nodeId: input.taskId, + nodeId: task.id, driver, providerInstanceId: modelSelection.instanceId, occurredAt: input.now, payload: { - id: input.taskId, + id: task.id, threadId: input.threadId, runId: input.runId, parentNodeId: input.rootNodeId, - origin: "app_owned", - createdBy: "agent", + origin: "app_owned" as const, + createdBy: "agent" as const, driver, providerInstanceId: modelSelection.instanceId, providerThreadId: null, @@ -287,16 +295,16 @@ const seedParentWithTerminalTask = (input: { model: null, completionWake: input.completionWake ?? "settled_only", completionDelivery: { - state: input.deliveryState, - observedByRunId: input.deliveryState === "acknowledged" ? input.runId : null, + state: task.deliveryState, + observedByRunId: task.deliveryState === "acknowledged" ? input.runId : null, }, - status: "completed", + status: "completed" as const, result: "child finished", startedAt: input.now, completedAt: input.now, updatedAt: input.now, }, - }, + })), ], }); }); @@ -683,6 +691,57 @@ it.layer(layerTest)("delegated completion delivery repairs", (it) => { }), ); + it.effect("counts already delivered siblings in the completion notice", () => + Effect.gen(function* () { + const orchestrator = yield* Orchestrator.OrchestratorV2; + const now = yield* DateTime.now; + const threadId = ThreadId.make("thread:delegated-delivery-notice-count"); + const runId = RunId.make("run:delegated-delivery-notice-count"); + const firstTaskId = NodeId.make("node:delegated-delivery-notice-count-first"); + const secondTaskId = NodeId.make("node:delegated-delivery-notice-count-second"); + + yield* seedParentWithTerminalTask({ + threadId, + projectId: ProjectId.make("project:delegated-delivery-notice-count"), + runId, + rootNodeId: NodeId.make("node:delegated-delivery-notice-count-root"), + taskId: firstTaskId, + deliveryState: "claimed", + completionWake: "always", + deliveryTaskIds: [firstTaskId, secondTaskId], + parentRunStatus: "completed", + siblingTasks: [ + { id: secondTaskId, deliveryState: "claimed" }, + { + id: NodeId.make("node:delegated-delivery-notice-count-earlier"), + deliveryState: "delivered", + }, + ], + now, + }); + + yield* orchestrator.dispatch({ + type: "message.dispatch", + commandId: CommandId.make("command:delegated-delivery-notice-count"), + threadId, + messageId: MessageId.make(`message:delegated-delivery:${threadId}`), + text: "Delegated tasks reached a terminal state.", + attachments: [], + dispatchMode: { type: "queue_after_active" }, + createdBy: "agent", + creationSource: "server", + delegatedCompletion: { parentRunId: runId, generation: 1, taskIds: [firstTaskId] }, + }); + + const projection = yield* orchestrator.getThreadProjection(threadId); + const notice = projection.turnItems.find((item) => item.type === "notification"); + assert.match( + notice?.type === "notification" ? notice.summary : "", + /^2 of 3 delegated tasks finished:/, + ); + }), + ); + it.effect.each([ { name: "oversized UTF-8 result", diff --git a/apps/server/src/orchestration-v2/IdleCompaction.test.ts b/apps/server/src/orchestration-v2/IdleCompaction.test.ts index 73b9a8b54caf5..bfa36cd2e5131 100644 --- a/apps/server/src/orchestration-v2/IdleCompaction.test.ts +++ b/apps/server/src/orchestration-v2/IdleCompaction.test.ts @@ -1,6 +1,9 @@ import { describe, expect, it } from "@effect/vitest"; import { + DEFAULT_SERVER_SETTINGS, + EventId, MessageId, + type OrchestrationV2ThreadProjection, NodeId, ProjectId, ProviderDriverKind, @@ -11,6 +14,17 @@ import { ThreadId, } from "@t3tools/contracts"; import * as DateTime from "effect/DateTime"; +import * as Deferred from "effect/Deferred"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as Stream from "effect/Stream"; +import * as Tracer from "effect/Tracer"; +import * as TestClock from "effect/testing/TestClock"; + +import * as ServerSettings from "../serverSettings.ts"; +import * as IdleCompactionService from "./IdleCompactionService.ts"; +import * as Orchestrator from "./Orchestrator.ts"; +import * as ProjectionStore from "./ProjectionStore.ts"; import { idleCompactionWindow, type IdleCompactionRecords } from "./IdleCompaction.ts"; @@ -154,3 +168,137 @@ describe("idleCompactionWindow", () => { expect(idleCompactionWindow(archived, NOW_MS)).toBeNull(); }); }); + +describe("IdleCompactionService", () => { + it.effect.each([ + { mode: "onReturn", reads: 0 }, + { mode: "off", reads: 0 }, + { mode: "beforeCacheExpires", reads: 1 }, + { mode: "beforeCacheExpires", override: "off", reads: 0 }, + { mode: "onReturn", override: "beforeCacheExpires", reads: 1 }, + { mode: "beforeCacheExpires", projectDisabled: true, reads: 0 }, + { mode: "beforeCacheExpires", enabled: false, reads: 0 }, + { mode: "beforeCacheExpires", driver: "codex", instance: "codex", reads: 0 }, + { mode: "beforeCacheExpires", driver: "codex", instance: "custom-codex", reads: 0 }, + { mode: "beforeCacheExpires", driver: "claudeAgent", instance: "custom-claude", reads: 1 }, + { mode: "beforeCacheExpires", unconfigured: true, reads: 1 }, + ] as const)( + "loads history only for eligible idle compaction: $mode/$override/$enabled/$projectDisabled/$instance/$unconfigured", + (scenario) => { + const test = { + override: "default", + enabled: true, + projectDisabled: false, + ...scenario, + } as const; + return Effect.scoped( + Effect.gen(function* () { + yield* TestClock.setTime(NOW_MS); + const selectedInstance = ProviderInstanceId.make( + "instance" in test ? test.instance : "claudeAgent", + ); + const driver = ProviderDriverKind.make("driver" in test ? test.driver : "claudeAgent"); + const base = records({ cacheExpiresInMs: 2 * MINUTE_MS }); + const data: OrchestrationV2ThreadProjection = { + ...base, + thread: { + ...base.thread, + providerInstanceId: selectedInstance, + modelSelection: { ...base.thread.modelSelection, instanceId: selectedInstance }, + }, + runs: base.runs.map((run) => ({ + ...run, + providerInstanceId: selectedInstance, + modelSelection: { ...run.modelSelection, instanceId: selectedInstance }, + })), + providerThreads: base.providerThreads.map((thread) => ({ + ...thread, + driver, + providerInstanceId: selectedInstance, + })), + attempts: [], + nodes: [], + subagents: [], + providerSessions: [], + plans: [], + checkpointScopes: [], + checkpoints: [], + contextHandoffs: [], + visibleTurnItems: [], + updatedAt: at(), + }; + let historyReads = 0; + let dispatches = 0; + const done = yield* Deferred.make(); + const tracer = Tracer.make({ + span(options) { + const span = new Tracer.NativeSpan(options); + const end = span.end.bind(span); + span.end = (...args) => { + end(...args); + if (span.name === "IdleCompactionService.compactWhenDue") + Deferred.doneUnsafe(done, Effect.void); + }; + return span; + }, + }); + const dependencies = Layer.mergeAll( + Layer.mock(Orchestrator.OrchestratorV2)({ + streamDomainEvents: Stream.make({ + id: EventId.make("idle-completed"), + type: "run.updated", + threadId, + runId, + occurredAt: at(), + payload: data.runs[0]!, + }), + dispatch: () => + Effect.sync(() => { + dispatches++; + return { sequence: 1, storedEvents: [] }; + }), + }), + Layer.mock(ProjectionStore.ProjectionStoreV2)({ + getThread: () => Effect.succeed(data.thread), + getThreadRecords: () => + Effect.sync(() => { + historyReads++; + return data; + }), + }), + Layer.mock(ServerSettings.ServerSettingsService)({ + getSettings: Effect.succeed({ + ...DEFAULT_SERVER_SETTINGS, + idleCompaction: test.mode, + providerInstances: + "unconfigured" in test + ? {} + : { + [selectedInstance]: { + driver, + enabled: test.enabled, + config: { idleCompaction: test.override }, + }, + }, + projectSettingsOverrides: test.projectDisabled + ? { + [data.thread.projectId]: { + providerInstances: { [selectedInstance]: { enabled: false } }, + }, + } + : {}, + }), + }), + ); + yield* Effect.gen(function* () { + const service = yield* IdleCompactionService.make; + yield* service.start(); + yield* Deferred.await(done); + }).pipe(Effect.provide(dependencies), Effect.withTracer(tracer)); + expect(historyReads).toBe(test.reads); + expect(dispatches).toBe(test.reads); + }), + ); + }, + ); +}); diff --git a/apps/server/src/orchestration-v2/IdleCompactionService.ts b/apps/server/src/orchestration-v2/IdleCompactionService.ts index 2b16cb8c1d73d..415bb14fa1a88 100644 --- a/apps/server/src/orchestration-v2/IdleCompactionService.ts +++ b/apps/server/src/orchestration-v2/IdleCompactionService.ts @@ -35,6 +35,20 @@ export const make = Effect.gen(function* () { // Loops once after the wait: the thread may have moved on. while (true) { const current = yield* settings.getSettings; + const thread = yield* projections.getThread(threadId); + const claudeInstance = Object.hasOwn(current.providerInstances, thread.providerInstanceId) + ? current.providerInstances[thread.providerInstanceId]?.driver === "claudeAgent" + : thread.providerInstanceId === "claudeAgent"; + if ( + !claudeInstance || + resolveIdleCompaction(current, thread.providerInstanceId) !== "beforeCacheExpires" || + !resolveProjectProviderInstanceEnabled( + current, + thread.projectId, + thread.providerInstanceId, + ) + ) + return; const records = yield* projections.getThreadRecords( threadId, [ diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index 7364c8f4b645f..25d2579a1ff2c 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -1152,6 +1152,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio turnItemTypes: ["user_message", "error"], messageRoles: ["user"], omitSettledLeafNodes: true, + // Commands here read only undelivered or active tasks; a dispatcher thread keeps + // thousands of delivered ones, each carrying its full prompt. + omitSettledSubagents: true, }, ) .pipe( @@ -1166,6 +1169,23 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })), ); + // Command reads omit settled tasks, but a completion notice counts its whole cohort. + const notificationTasks = ( + threadId: ThreadId, + message: Pick, + loaded: ReadonlyArray, + ) => + message.delegatedCompletion === undefined + ? Effect.succeed(loaded) + : projectionStore + .getThreadRecords(threadId, ["subagents"], { + subagentRunIds: [message.delegatedCompletion.parentRunId], + }) + .pipe( + Effect.map((records) => records.subagents), + Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })), + ); + const readHandoffItems = (threadId: ThreadId, runIds?: ReadonlyArray) => projectionStore .getThreadRecords( @@ -2173,7 +2193,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio nodeId: rootNodeId, providerInstanceId: queuedRun.providerInstanceId, occurredAt: now, - payload: notificationTurnItem(userTurnItem, queuedMessage, projection.subagents), + payload: notificationTurnItem( + userTurnItem, + queuedMessage, + yield* notificationTasks(threadId, queuedMessage, projection.subagents), + ), }, { type: "run.updated", @@ -4315,7 +4339,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio // The source is unlocked; validate the modes copied here, not an earlier read. yield* refuseAboveDispatchModeLimit(command, command.sourceThreadId, sourceProjection.thread); const targetProjection = yield* projectionStore - .getThreadRecords(command.targetThreadId, ["contextTransfers"]) + .getThreadRecords(command.targetThreadId, ["runs", "contextTransfers"]) .pipe( Effect.mapError( (cause) => @@ -4367,6 +4391,17 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio }); } + // Queued runs cannot consume merge-back context. Refuse under the target's + // command lock before a pending transfer can block its ordinary messages. + if (targetProjection.runs.some((run) => isBlockingRun(run) || run.status === "queued")) { + return yield* new OrchestratorDispatchError({ + commandId: command.commandId, + commandType: command.type, + cause: + "Finish or stop the target thread's active work and clear its queue before merging back.", + }); + } + const sourceProviderThread = providerThreadForRun(sourceProjection, sourceRun); const now = command.createdAt ?? (yield* DateTime.now); const emitEvent = emit(events, command); @@ -4676,7 +4711,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio nodeId: messageInput.nodeId, providerInstanceId: messageInput.providerInstanceId, occurredAt: now, - payload: notificationTurnItem(turnItem, message, input.projection.subagents), + payload: notificationTurnItem( + turnItem, + message, + yield* notificationTasks(input.command.threadId, message, input.projection.subagents), + ), }); }, ); @@ -6327,7 +6366,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio nodeId: rootNodeId, providerInstanceId: modelSelection.instanceId, occurredAt: now, - payload: notificationTurnItem(turnItem, message, projection.subagents), + payload: notificationTurnItem( + turnItem, + message, + yield* notificationTasks(command.threadId, message, projection.subagents), + ), }); if (preparationTurnItem !== null) { yield* emitEvent({ @@ -7221,7 +7264,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio nodeId: rootNodeId, providerInstanceId: modelSelection.instanceId, occurredAt: now, - payload: notificationTurnItem(turnItem, message, projection.subagents), + payload: notificationTurnItem( + turnItem, + message, + yield* notificationTasks(command.threadId, message, projection.subagents), + ), }); const forkResolution = nativeForkResolution ?? portableForkResolution; if (pendingForkTransfer !== undefined && forkResolution !== null) { @@ -7288,14 +7335,12 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio effects: Ref.Ref>, ) { const parentProjection = yield* projectionStore - .getThreadRecords(command.parentThreadId, [ - "runs", - "nodes", - "subagents", - "providerThreads", - "providerTurns", - "attempts", - ]) + .getThreadRecords( + command.parentThreadId, + ["runs", "nodes", "providerThreads", "providerTurns", "attempts"], + // The parent node must belong to the active run, whose nodes this keeps. + { omitSettledLeafNodes: true }, + ) .pipe( Effect.mapError( (cause) => @@ -7365,7 +7410,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio parentTitle: parentProjection.thread.title, prompt: command.task, ...(command.title === undefined ? {} : { title: command.title }), - ordinal: parentProjection.subagents.length + 1, + ordinal: (yield* subagentCount(command.parentThreadId)) + 1, }); const childThread: OrchestrationV2AppThread = { ...makeSubagentChildThread({ @@ -7600,7 +7645,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio ); if (run === undefined || run.delegatedTaskId !== undefined) return; const parent = yield* projectionStore - .getThreadRecords(parentThreadId, ["runs", "subagents", "providerTurns", "attempts"]) + .getThreadRecords(parentThreadId, ["runs", "subagents", "providerTurns", "attempts"], { + subagentIds: child.forkedFrom?.type === "node" ? [child.forkedFrom.nodeId] : [], + }) .pipe(mapDispatchError(command)); const original = parent.subagents.find( (task) => @@ -7728,13 +7775,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio events: Ref.Ref>, ) { const parentProjection = yield* projectionStore - .getThreadRecords(command.parentThreadId, [ - "runs", - "nodes", - "subagents", - "providerTurns", - "attempts", - ]) + .getThreadRecords(command.parentThreadId, ["runs", "providerTurns", "attempts"]) .pipe( Effect.mapError( (cause) => new OrchestratorProjectionError({ threadId: command.parentThreadId, cause }), @@ -7766,7 +7807,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio parentTitle: parentProjection.thread.title, prompt: command.task, ...(command.title === undefined ? {} : { title: command.title }), - ordinal: parentProjection.subagents.length + 1, + ordinal: (yield* subagentCount(command.parentThreadId)) + 1, }); const common = { threadId: command.parentThreadId, @@ -7871,11 +7912,19 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio >, events: Ref.Ref>, ) { + // Delivery planning reads only this task and the undelivered tasks of the queued delivery; + // the task's own node and turn item are all it updates besides. const parentProjection = yield* projectionStore .getThreadRecords( command.parentThreadId, ["runs", "messages", "subagents", "providerTurns", "nodes", "turnItems"], - { turnItemTypes: ["subagent"], messageRoles: ["user"] }, + { + turnItemTypes: ["subagent"], + messageRoles: ["user"], + omitSettledSubagents: { except: [command.taskId] }, + nodeIds: [command.taskId], + turnItemNodeIds: [command.taskId], + }, ) .pipe( Effect.mapError( @@ -8101,8 +8150,15 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio command: Extract, events: Ref.Ref>, ) { + // Delivery planning reads only this task and the undelivered tasks of the queued delivery. const parentProjection = yield* projectionStore - .getThreadRecords(command.parentThreadId, ["subagents", "runs", "messages", "providerTurns"]) + .getThreadRecords( + command.parentThreadId, + ["subagents", "runs", "messages", "providerTurns"], + { + omitSettledSubagents: { except: [command.taskId] }, + }, + ) .pipe( Effect.mapError( (cause) => @@ -9545,6 +9601,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio } }); + const subagentCount = (threadId: ThreadId) => + projectionStore + .getSubagentCount(threadId) + .pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause }))); + const loadProjectionForCommand = ( command: OrchestrationV2Command, fields: ReadonlyArray, @@ -13337,7 +13398,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio stored.event.payload.status === "rolled_back"), ), Stream.runForEach(handleRunUpdate), - Effect.forkDetach, + Effect.forkScoped, ); // A child whose run ends while its background tasks or its own subagents still run has no @@ -13379,7 +13440,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio ), ), ), - Effect.forkDetach, + Effect.forkScoped, ); const recoverDelegatedTasks = Effect.gen(function* () { @@ -13557,7 +13618,17 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio thread.limitRecovery.resetAt === input.limitRecovery.resetAt ) return; - yield* dispatchWithReceiptEffect({ type: "thread.metadata.update", ...input }); + const command = { type: "thread.metadata.update", ...input } as const; + // A Stop that reached the run before its limit failure landed had nothing to disarm. + const stopped = yield* stopReachedRun(command, input.threadId, input.limitRecovery.runId); + yield* dispatchWithReceiptEffect( + stopped + ? { + ...command, + limitRecovery: { ...input.limitRecovery, autoResume: false, snooze: false }, + } + : command, + ); }), ); }), diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts index dd5da22f4d431..510a89739b7df 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts @@ -850,6 +850,121 @@ const commandReadsSkipSettledLeafNodes = Effect.gen(function* () { nodeNames(yield* store.getThreadRecords(threadId, ["nodes"])), nodes.map(([name]) => name).toSorted(), ); + + const subagents = [ + ["delivered", settledRun, "completed", "delivered"], + ["acknowledged", settledRun, "failed", "acknowledged"], + ["disposed", settledRun, "cancelled", "disposed"], + ["pending-delivery", settledRun, "completed", "pending"], + ["claimed", settledRun, "completed", "claimed"], + ["no-delivery", settledRun, "interrupted", null], + ["running", settledRun, "running", null], + ["active-run-delivered", activeRun, "completed", "delivered"], + ] as const; + for (const [index, [name, run, status, delivery]] of subagents.entries()) { + const id = NodeId.make(`node:settled-subagents:${name}`); + yield* store.apply({ + id: EventId.make(`event:settled-subagents:item:${name}`), + type: "turn-item.updated", + threadId, + runId: run.id, + nodeId: id, + driver, + occurredAt: now, + payload: { + id: TurnItemId.make(`item:settled-subagents:${name}`), + type: "subagent", + threadId, + runId: run.id, + nodeId: id, + providerThreadId: null, + providerTurnId: null, + nativeItemRef: null, + parentItemId: null, + ordinal: 100 + index, + status: status === "running" ? "running" : "completed", + title: name, + startedAt: now, + completedAt: status === "running" ? null : now, + updatedAt: now, + subagentId: id, + origin: "app_owned", + driver, + providerInstanceId, + childThreadId: null, + prompt: "Do the work", + result: null, + }, + }); + yield* store.apply({ + id: EventId.make(`event:settled-subagents:${name}`), + type: "subagent.updated", + threadId, + runId: run.id, + nodeId: id, + driver, + providerInstanceId, + occurredAt: now, + payload: { + id, + threadId, + runId: run.id, + parentNodeId: run.rootNodeId!, + origin: "app_owned", + createdBy: "agent", + driver, + providerInstanceId, + providerThreadId: null, + childThreadId: null, + nativeTaskRef: null, + prompt: "Do the work", + title: name, + model: null, + ...(delivery === null + ? {} + : { completionDelivery: { state: delivery, observedByRunId: null } }), + status, + result: null, + startedAt: now, + completedAt: status === "running" ? null : now, + updatedAt: now, + }, + }); + } + const subagentNames = (records: { + readonly subagents: ReadonlyArray<{ readonly title: string | null }>; + }) => records.subagents.map((subagent) => subagent.title ?? "").toSorted(); + assert.deepEqual( + subagentNames( + yield* store.getThreadRecords(threadId, ["subagents"], { omitSettledSubagents: true }), + ), + ["active-run-delivered", "claimed", "no-delivery", "pending-delivery", "running"], + ); + assert.deepEqual( + subagentNames(yield* store.getThreadRecords(threadId, ["subagents"])), + subagents.map(([name]) => name).toSorted(), + ); + // A command about one settled task keeps it, and reads only its node and turn item. + const deliveredId = NodeId.make("node:settled-subagents:delivered"); + const forTask = yield* store.getThreadRecords(threadId, ["subagents", "nodes", "turnItems"], { + omitSettledSubagents: { except: [deliveredId] }, + nodeIds: [NodeId.make("node:settled-leaf-nodes:settled-tool")], + turnItemNodeIds: [deliveredId], + }); + assert.deepEqual(subagentNames(forTask), [ + "active-run-delivered", + "claimed", + "delivered", + "no-delivery", + "pending-delivery", + "running", + ]); + assert.deepEqual(nodeNames(forTask), ["settled-tool"]); + assert.deepEqual( + forTask.turnItems.map((item) => item.title), + ["delivered"], + ); + assert.strictEqual(yield* store.getSubagentCount(threadId), subagents.length); }); const backgroundThreadUpdatesKeepTimestamp = Effect.gen(function* () { @@ -3535,6 +3650,12 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { const projection = yield* projectionStore.getThreadProjection(threadId); assert.deepEqual(projection.runs[0]?.delegatedCompletion, run.delegatedCompletion); assert.deepEqual(projection.subagents[0]?.completionDelivery, task.completionDelivery); + assert.deepEqual( + (yield* projectionStore.getThreadRecords(threadId, ["subagents"], { + omitSettledSubagents: true, + })).subagents, + [], + ); }), ); @@ -4428,6 +4549,8 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { (row) => row.id === threadId, ), ); + assert.strictEqual(yield* store.getLimitRecoveryWakeAt(now), DateTime.toEpochMillis(reset)); + assert.isNull(yield* store.getLimitRecoveryWakeAt(reset)); const due = (yield* store.getLimitRecoveryCandidates({ ...recoveryOptions, now: reset, @@ -4450,9 +4573,14 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { (row) => row.id === threadId, ), ); + assert.strictEqual( + yield* store.getLimitRecoveryWakeAt(reset), + DateTime.toEpochMillis(DateTime.add(reset, { minutes: 1 })), + ); yield* sql`UPDATE orchestration_v2_projection_threads SET payload_json = json_set(payload_json, '$.snoozedUntil', NULL, '$.limitRecovery.autoResume', json('false')) WHERE thread_id = ${threadId}`; + assert.isNull(yield* store.getLimitRecoveryWakeAt(now)); assert.isUndefined( (yield* store.getLimitRecoveryCandidates({ ...recoveryOptions, diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts index bd2eff55bbc53..5846512f3f782 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -332,11 +332,19 @@ export interface ProjectionRecordFilter { readonly messageIds?: ReadonlyArray; readonly messageRunIds?: ReadonlyArray; readonly turnItemRunIds?: ReadonlyArray; + readonly turnItemNodeIds?: ReadonlyArray; readonly runIds?: ReadonlyArray; readonly subagentIds?: ReadonlyArray; + readonly subagentRunIds?: ReadonlyArray; + readonly nodeIds?: ReadonlyArray; readonly turnItemTypes?: ReadonlyArray; readonly turnItemStatuses?: ReadonlyArray; readonly omitSettledLeafNodes?: boolean; + /** + * Skips ended subagents whose completion was acknowledged, delivered or disposed, unless their + * run is active or `except` names them. + */ + readonly omitSettledSubagents?: true | { readonly except: ReadonlyArray }; } // A fork inherits its source's history before it has messages of its own, @@ -436,6 +444,7 @@ export interface ProjectionStoreV2Shape { options: ProjectionTimelinePageOptions, ) => Effect.Effect; readonly getMessageCount: (threadId: ThreadId) => Effect.Effect; + readonly getSubagentCount: (threadId: ThreadId) => Effect.Effect; readonly getNextTurnItemOrdinal: ( threadId: ThreadId, ) => Effect.Effect; @@ -496,6 +505,10 @@ export interface ProjectionStoreV2Shape { readonly autoResume: boolean; readonly snooze: boolean; }) => Effect.Effect, ProjectionStoreV2Error>; + /** Epoch ms of the next armed reset or snooze end after `now`, when a waiting thread becomes a candidate. */ + readonly getLimitRecoveryWakeAt: ( + now: DateTime.Utc, + ) => Effect.Effect; readonly getSettlementCandidates: ( threadId?: ThreadId, ) => Effect.Effect, ProjectionStoreV2Error>; @@ -2106,6 +2119,7 @@ export const layer: Layer.Layer = started_at, completed_at, updated_at, + completion_delivery_state, payload_json ) VALUES ( @@ -2123,6 +2137,7 @@ export const layer: Layer.Layer = ${nullableStringField(payload, "startedAt")}, ${nullableStringField(payload, "completedAt")}, ${stringField(payload, "updatedAt")}, + ${event.payload.completionDelivery?.state ?? null}, ${payloadJson} ) ON CONFLICT(subagent_id) @@ -2140,6 +2155,10 @@ export const layer: Layer.Layer = started_at = excluded.started_at, completed_at = excluded.completed_at, updated_at = excluded.updated_at, + completion_delivery_state = COALESCE( + excluded.completion_delivery_state, + orchestration_v2_projection_subagents.completion_delivery_state + ), payload_json = CASE WHEN json_type(excluded.payload_json, '$.completionDelivery') IS NULL AND json_type(orchestration_v2_projection_subagents.payload_json, '$.completionDelivery') IS NOT NULL @@ -2979,7 +2998,12 @@ export const layer: Layer.Layer = const boundedTurnItemRows: ReadonlyArray = (fields !== undefined && !fields.includes("turnItems")) || (window === undefined && - matchesNothing(filter?.turnItemTypes, filter?.turnItemStatuses, filter?.turnItemRunIds)) + matchesNothing( + filter?.turnItemTypes, + filter?.turnItemStatuses, + filter?.turnItemRunIds, + filter?.turnItemNodeIds, + )) ? [] : window === undefined ? yield* sql` @@ -2989,6 +3013,7 @@ export const layer: Layer.Layer = ${filter?.turnItemTypes === undefined ? sql`` : sql`AND type IN (SELECT value FROM json_each(${encodeIdList(filter.turnItemTypes)}))`} ${filter?.turnItemStatuses === undefined ? sql`` : sql`AND status IN (SELECT value FROM json_each(${encodeIdList(filter.turnItemStatuses)}))`} ${filter?.turnItemRunId === undefined ? sql`` : sql`AND run_id = ${filter.turnItemRunId}`} + ${filter?.turnItemNodeIds === undefined ? sql`` : sql`AND node_id IN (SELECT value FROM json_each(${encodeIdList(filter.turnItemNodeIds)}))`} ${filter?.turnItemRunIds === undefined ? sql`` : sql`AND (run_id IN (SELECT value FROM json_each(${encodeIdList(filter.turnItemRunIds.filter((id): id is RunId => id !== null))})) OR (${filter.turnItemRunIds.includes(null) ? 1 : 0} = 1 AND run_id IS NULL))`} ORDER BY ordinal ASC, turn_item_id ASC ` @@ -3215,13 +3240,15 @@ export const layer: Layer.Layer = OR run_id IN (SELECT value FROM json_each(${cohortRunIds}))) ORDER BY run_id ASC, attempt_ordinal ASC `, - fields !== undefined && !fields.includes("nodes") + (fields !== undefined && !fields.includes("nodes")) || + (window === undefined && matchesNothing(filter?.nodeIds)) ? Effect.succeed([]) : window === undefined ? sql` SELECT payload_json FROM orchestration_v2_projection_nodes AS node WHERE node.thread_id = ${threadId} + ${filter?.nodeIds === undefined ? sql`` : sql`AND node.node_id IN (SELECT value FROM json_each(${encodeIdList(filter.nodeIds)}))`} ${ filter?.omitSettledLeafNodes === true ? sql`AND NOT ( @@ -3252,14 +3279,34 @@ export const layer: Layer.Layer = ), ), (fields !== undefined && !fields.includes("subagents")) || - (window === undefined && matchesNothing(filter?.subagentIds)) + (window === undefined && + (matchesNothing(filter?.subagentIds) || matchesNothing(filter?.subagentRunIds))) ? Effect.succeed([]) : window === undefined ? sql` SELECT payload_json - FROM orchestration_v2_projection_subagents + FROM orchestration_v2_projection_subagents AS subagent WHERE thread_id = ${threadId} ${filter?.subagentIds === undefined ? sql`` : sql`AND subagent_id IN (SELECT value FROM json_each(${encodeIdList(filter.subagentIds)}))`} + ${filter?.subagentRunIds === undefined ? sql`` : sql`AND run_id IN (SELECT value FROM json_each(${encodeIdList(filter.subagentRunIds)}))`} + ${ + filter?.omitSettledSubagents !== undefined + ? sql`AND (${ + filter.omitSettledSubagents === true + ? sql`0` + : sql`subagent_id IN (SELECT value FROM json_each(${encodeIdList(filter.omitSettledSubagents.except)}))` + } OR NOT ( + subagent.status IN ('completed', 'failed', 'cancelled', 'interrupted') + AND COALESCE(subagent.completion_delivery_state, '') + IN ('acknowledged', 'delivered', 'disposed') + AND NOT EXISTS ( + SELECT 1 FROM orchestration_v2_projection_runs AS run + WHERE run.run_id = subagent.run_id + AND run.status IN ('preparing', 'queued', 'starting', 'running', 'waiting') + ) + ))` + : sql`` + } ORDER BY COALESCE(started_at, ''), subagent_id ASC ` : sql` @@ -3862,6 +3909,34 @@ export const layer: Layer.Layer = Effect.mapError((cause) => new ProjectionStoreSetupError({ cause })), ); + const getLimitRecoveryWakeAt = Effect.fn("ProjectionStore.getLimitRecoveryWakeAt")( + function* (now: DateTime.Utc) { + const nowIso = DateTime.formatIso(now); + const rows = yield* sql<{ + readonly reset_ms: number | null; + readonly snooze_ms: number | null; + }>` + SELECT + MIN(CASE WHEN julianday(reset_at) > julianday(${nowIso}) + THEN (julianday(reset_at) - 2440587.5) * 86400000 END) AS reset_ms, + MIN(CASE WHEN julianday(snoozed_until) > julianday(${nowIso}) + THEN (julianday(snoozed_until) - 2440587.5) * 86400000 END) AS snooze_ms + FROM ( + SELECT json_extract(payload_json, '$.limitRecovery.resetAt') AS reset_at, + json_extract(payload_json, '$.snoozedUntil') AS snoozed_until + FROM orchestration_v2_projection_threads + WHERE deleted_at IS NULL + AND json_extract(payload_json, '$.limitRecovery.autoResume') = 1 + ) + `; + const wakes = [rows[0]?.reset_ms, rows[0]?.snooze_ms].filter( + (value): value is number => value != null, + ); + return wakes.length === 0 ? null : Math.round(Math.min(...wakes)); + }, + Effect.mapError((cause) => new ProjectionStoreSetupError({ cause })), + ); + const getRecoveryThreadIds = Effect.fn("ProjectionStore.getRecoveryThreadIds")( function* (kind: ProjectionRecoveryKind) { const candidates = (() => { @@ -5203,6 +5278,13 @@ export const layer: Layer.Layer = Effect.map((rows) => rows[0]?.count ?? 0), Effect.mapError(controlReadError(threadId)), ); + const getSubagentCount: ProjectionStoreV2Shape["getSubagentCount"] = (threadId) => + sql<{ + count: number; + }>`SELECT COUNT(*) AS count FROM orchestration_v2_projection_subagents WHERE thread_id = ${threadId}`.pipe( + Effect.map((rows) => rows[0]?.count ?? 0), + Effect.mapError(controlReadError(threadId)), + ); const getNextTurnItemOrdinal: ProjectionStoreV2Shape["getNextTurnItemOrdinal"] = (threadId) => sql<{ ordinal: number | null }>`SELECT MAX(ordinal) AS ordinal FROM orchestration_v2_projection_turn_items WHERE thread_id = ${threadId}`.pipe( @@ -5236,6 +5318,14 @@ export const layer: Layer.Layer = WHERE thread_id = ${threadId} AND type = 'dynamic_tool' AND (payload_json LIKE '%htmlRender%' OR payload_json LIKE ${`%${MCP_APP_OUTPUT_KEY}%`}) + `, + sql<{ id: string }>` + SELECT DISTINCT json_extract(attachment.value, '$.id') AS id + FROM orchestration_v2_projection_turn_items AS item, + json_each(item.payload_json, '$.questionAnswer.attachmentsByQuestionId') AS question, + json_each(question.value) AS attachment + WHERE item.thread_id = ${threadId} + AND item.type = 'user_input_request' `, // A guest's waiting files are pending uploads that no message row holds yet. sql<{ id: string }>` @@ -5247,10 +5337,11 @@ export const layer: Layer.Layer = AND json_extract(message.value, '$.status') = 'pending' `, ]).pipe( - Effect.map(([messages, renders, guestFiles]) => [ + Effect.map(([messages, renders, answerFiles, guestFiles]) => [ ...new Set([ ...messages.map((row) => row.id), ...guestFiles.map((row) => row.id), + ...answerFiles.map((row) => row.id), ...threadHtmlRenderAttachmentIds( threadId, renders.map((row) => { @@ -6917,6 +7008,7 @@ export const layer: Layer.Layer = hasUnpairedRunInterruptRequest: readOnReader(hasUnpairedRunInterruptRequest), isNodeStored: readOnReader(isNodeStored), getMessageCount: readOnReader(getMessageCount), + getSubagentCount: readOnReader(getSubagentCount), getNextTurnItemOrdinal: readOnReader(getNextTurnItemOrdinal), getTurnItem: readOnReader(getTurnItem), getThreadRecords: (threadId, fields, filter) => @@ -6925,6 +7017,7 @@ export const layer: Layer.Layer = getPlan: readOnReader(getPlan), getProviderControlContext: readOnReader(getProviderControlContext), getLimitRecoveryCandidates: readOnReader(getLimitRecoveryCandidates), + getLimitRecoveryWakeAt: readOnReader(getLimitRecoveryWakeAt), getRecoveryThreadIds: readOnReader(getRecoveryThreadIds), getUnreadableThreadIds: readOnReader(getUnreadableThreadIds), getThreadSnapshot: readOnReader(getThreadSnapshot), diff --git a/apps/server/src/orchestration-v2/ProviderEventIngestor.ts b/apps/server/src/orchestration-v2/ProviderEventIngestor.ts index c5ec581f03bc7..784449209e290 100644 --- a/apps/server/src/orchestration-v2/ProviderEventIngestor.ts +++ b/apps/server/src/orchestration-v2/ProviderEventIngestor.ts @@ -687,6 +687,8 @@ export const layer: Layer.Layer< }), ]; } + case "background_work.updated": + return []; case "command_output.delta": // Live output is routed to the command output hub before ingestion. return []; diff --git a/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts b/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts index 65da8b23676b0..7932a03809c94 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts @@ -210,6 +210,7 @@ it.effect( ProjectionStore.ProjectionStoreV2.of({ apply: () => Effect.void, getLimitRecoveryCandidates: () => Effect.die("unused getLimitRecoveryCandidates"), + getLimitRecoveryWakeAt: () => Effect.die("unused getLimitRecoveryWakeAt"), getOpenRemoteDelegatedTasks: Effect.die("unused getOpenRemoteDelegatedTasks"), getShellSnapshot: () => Effect.die("unused getShellSnapshot"), listActiveSubagentThreadIds: () => Effect.die("unused listActiveSubagentThreadIds"), @@ -235,6 +236,7 @@ it.effect( getThreadHistoryPage: () => Effect.die("unused"), getTimelinePage: () => Effect.die("Unused timeline read"), getMessageCount: () => Effect.die("unused message count"), + getSubagentCount: () => Effect.die("unused subagent count"), getNextTurnItemOrdinal: () => Effect.die("unused ordinal read"), getTurnItem: () => Effect.die("unused turn item read"), getThreadRecords: () => Effect.die("unused record read"), diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts index beaf7961e3782..d9287644be82a 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts @@ -788,7 +788,7 @@ function makeLocalCommandHarness(input: { } effectIt.effect( - "recovers the thread branch tip, falls back to the current project branch, and rejects detached HEAD", + "recovers the local or pushed thread tip, else the current branch, remote default, or detached HEAD", () => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; @@ -797,6 +797,7 @@ effectIt.effect( const driver = yield* GitVcsDriver.GitVcsDriver; const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-recreate-repo-" }); const workspaceRoot = path.join(root, "project"); + const originRoot = path.join(root, "origin.git"); const worktreesDir = path.join(root, "worktrees"); const worktreePath = path.join(worktreesDir, "project", "restore"); yield* fileSystem.makeDirectory(workspaceRoot); @@ -804,10 +805,19 @@ effectIt.effect( driver .execute({ operation: "test.worktreeRecovery", cwd, args }) .pipe(Effect.map((result) => result.stdout.trim())); + const branchConfig = (key: string) => + git(workspaceRoot, ["config", "--get", `branch.feature/restore.${key}`]).pipe( + Effect.orElseSucceed(() => ""), + ); + yield* git(root, ["init", "--bare", "-b", "main", originRoot]); yield* git(workspaceRoot, ["init", "-b", "main"]); yield* git(workspaceRoot, ["config", "user.name", "Test"]); yield* git(workspaceRoot, ["config", "user.email", "test@example.com"]); yield* git(workspaceRoot, ["commit", "--allow-empty", "-m", "base"]); + const baseTip = yield* git(workspaceRoot, ["rev-parse", "HEAD"]); + yield* git(workspaceRoot, ["remote", "add", "origin", originRoot]); + yield* git(workspaceRoot, ["push", "origin", "main"]); + yield* git(workspaceRoot, ["remote", "set-head", "origin", "main"]); yield* git(workspaceRoot, ["worktree", "add", "-b", "feature/restore", worktreePath]); yield* fileSystem.writeFileString(path.join(worktreePath, "thread.txt"), "thread work"); yield* git(worktreePath, ["add", "."]); @@ -825,18 +835,48 @@ effectIt.effect( // Tags must neither hide the thread branch nor select a different recovery tip. yield* git(workspaceRoot, ["tag", "feature/restore", projectTip]); yield* git(workspaceRoot, ["tag", "project-current", threadTip]); + yield* git(workspaceRoot, ["tag", "origin/feature/restore", threadTip]); + const expectedTips = { + retained: { tip: threadTip, file: "thread.txt", upstream: null, base: "" }, + deleted: { tip: projectTip, file: "project.txt", upstream: null, base: "project-current" }, + pushed: { + tip: threadTip, + file: "thread.txt", + upstream: "refs/remotes/origin/feature/restore", + base: "", + }, + "pushed-detached": { + tip: threadTip, + file: "thread.txt", + upstream: "refs/remotes/origin/feature/restore", + base: "", + }, + detached: { tip: baseTip, file: null, upstream: null, base: "main" }, + "detached-no-remote": { tip: projectTip, file: "project.txt", upstream: null, base: "" }, + } as const; // Reuse the workflow across turns so recovery cannot select a cached project branch. - for (const scenario of ["retained", "deleted", "detached"] as const) { + for (const scenario of Object.keys(expectedTips) as Array) { + const expected = expectedTips[scenario]; + if (scenario === "pushed") { + yield* git(workspaceRoot, ["push", "origin", `${threadTip}:refs/heads/feature/restore`]); + yield* git(workspaceRoot, ["fetch", "origin"]); + } + if (scenario === "pushed-detached") { + yield* git(workspaceRoot, ["switch", "--detach"]); + expect(yield* git(workspaceRoot, ["branch", "--show-current"])).toBe(""); + } + if (scenario === "detached") { + yield* git(workspaceRoot, ["push", "origin", "--delete", "feature/restore"]); + } + if (scenario === "detached-no-remote") { + yield* git(workspaceRoot, ["remote", "remove", "origin"]); + } yield* fileSystem.remove(worktreePath, { recursive: true }); if (scenario !== "retained") { yield* git(workspaceRoot, ["worktree", "prune"]); yield* git(workspaceRoot, ["branch", "-D", "feature/restore"]); } - if (scenario === "detached") { - yield* git(workspaceRoot, ["switch", "--detach"]); - expect(yield* git(workspaceRoot, ["branch", "--show-current"])).toBe(""); - } const harness = makeLocalCommandHarness({ text: "Continue", workspaceRoot, @@ -850,38 +890,34 @@ effectIt.effect( harness.open.mockImplementation(() => Effect.gen(function* () { expect(yield* git(worktreePath, ["branch", "--show-current"])).toBe("feature/restore"); - expect(yield* git(worktreePath, ["rev-parse", "HEAD"])).toBe( - scenario === "retained" ? threadTip : projectTip, + expect(yield* git(worktreePath, ["rev-parse", "HEAD"])).toBe(expected.tip); + for (const file of ["thread.txt", "project.txt"]) { + expect(yield* fileSystem.exists(path.join(worktreePath, file))).toBe( + file === expected.file, + ); + } + expect(yield* branchConfig("merge")).toBe( + expected.upstream === null ? "" : "refs/heads/feature/restore", ); - expect( - yield* fileSystem.readFileString( - path.join(worktreePath, scenario === "retained" ? "thread.txt" : "project.txt"), - ), - ).toBe(scenario === "retained" ? "thread work" : "new project code"); + if (expected.upstream !== null) { + expect( + yield* git(worktreePath, ["rev-parse", "--symbolic-full-name", "@{upstream}"]), + ).toBe(expected.upstream); + } + expect(yield* branchConfig("t3-merge-base")).toBe(expected.base); return yield* open(); }).pipe(Effect.orDie), ); yield* harness.start; - if (scenario === "detached") { - expect(harness.open).not.toHaveBeenCalled(); - expect(harness.startRootRun).not.toHaveBeenCalled(); - expect(harness.projection().runs.at(-1)?.status).toBe("failed"); - expect(harness.projection().turnItems).toMatchObject([ - { type: "error", failure: { message: expect.stringContaining("no current branch") } }, - ]); - expect(yield* fileSystem.exists(worktreePath)).toBe(false); - expect(yield* git(workspaceRoot, ["branch", "--list", "feature/restore"])).toBe(""); - } else { - expect(harness.open).toHaveBeenCalledOnce(); - expect(harness.startRootRun).toHaveBeenCalledWith( - expect.objectContaining({ - runtimePolicy: expect.objectContaining({ cwd: worktreePath }), - }), - ); - expect(harness.projection().runs.at(-1)?.status).toBe("running"); - } + expect(harness.open).toHaveBeenCalledOnce(); + expect(harness.startRootRun).toHaveBeenCalledWith( + expect.objectContaining({ + runtimePolicy: expect.objectContaining({ cwd: worktreePath }), + }), + ); + expect(harness.projection().runs.at(-1)?.status).toBe("running"); } }).pipe(Effect.scoped, Effect.provide(layerRealGit)), ); diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts index fad06732fb48e..14c93a31ea047 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts @@ -87,7 +87,7 @@ class WorktreeRecreateNoBaseError extends Schema.TaggedError effect, summary, stack, invalidate: options.invalidate ?? (() => Effect.void), diff --git a/apps/server/src/orchestration-v2/PullRequestSyncReactor.ts b/apps/server/src/orchestration-v2/PullRequestSyncReactor.ts index ab3c0dca08adc..2c97b1867ef48 100644 --- a/apps/server/src/orchestration-v2/PullRequestSyncReactor.ts +++ b/apps/server/src/orchestration-v2/PullRequestSyncReactor.ts @@ -599,7 +599,7 @@ export const make = Effect.gen(function* () { const worker = yield* makeDrainableWorker((scope: "all" | "requested") => Effect.suspend(() => { if (scope === "requested") requestedSweepQueued = false; - return sweep(scope); + return pullRequests.withSharedProjectReads(sweep(scope)); }).pipe(Effect.catchCause(logSkipped("pull request sync sweep failed", {}))), ); diff --git a/apps/server/src/orchestration-v2/RunExecutionService.test.ts b/apps/server/src/orchestration-v2/RunExecutionService.test.ts index 10f6b84fae91e..13ee4f3939a13 100644 --- a/apps/server/src/orchestration-v2/RunExecutionService.test.ts +++ b/apps/server/src/orchestration-v2/RunExecutionService.test.ts @@ -36,7 +36,9 @@ import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as Ref from "effect/Ref"; import * as Stream from "effect/Stream"; +import { SqlError, classifySqliteError } from "effect/sql/SqlError"; +import { toPersistenceSqlError } from "../persistence/Errors.ts"; import * as ServerSettings from "../serverSettings.ts"; import * as CheckpointService from "./CheckpointService.ts"; import type { PendingOrchestrationEffectV2 } from "./EffectOutbox.ts"; @@ -1772,6 +1774,74 @@ it.effect("ends a nested native subagent and its thread when the root run is int }), ); +it.effect("keeps ingesting until queued background work clears after the probe goes idle", () => + Effect.gen(function* () { + // The adapter's probe runs ahead of its event queue, so only the queued update may release. + // A child transferred to another run takes its pending work with it. + const pending = yield* Ref.make(true); + const consumed = yield* Deferred.make(); + const result = yield* Effect.raceFirst( + runBackgroundItemScenario( + "queued-background-work", + (ids) => { + const childProviderThreadId = ProviderThreadId.make(`${ids.providerThreadId}:child`); + const childProviderTurnId = ProviderTurnId.make(`${ids.rootProviderTurnId}:child`); + const running = subagentEvent(ids, "running"); + if (running.type !== "subagent.updated") throw new Error("Missing subagent"); + const linked = { + ...running, + subagent: { + ...running.subagent, + origin: "provider_native" as const, + childThreadId: ids.childThreadId, + providerThreadId: childProviderThreadId, + }, + }; + const backgroundWork = (providerTurnId: ProviderTurnId, pending: boolean) => + ({ + type: "background_work.updated", + driver, + providerTurnId, + pending, + }) satisfies ProviderAdapter.ProviderAdapterV2Event; + return [ + childThreadCreatedEvent(ids), + linked, + { + type: "provider_turn.updated", + driver, + threadId: ids.childThreadId, + providerTurn: { + id: childProviderTurnId, + providerThreadId: childProviderThreadId, + runAttemptId: null, + status: "completed", + }, + } as ProviderAdapter.ProviderAdapterV2Event, + backgroundWork(childProviderTurnId, true), + backgroundWork(ids.rootProviderTurnId, true), + rootTerminalEvent(ids, "completed"), + { ...linked, subagent: { ...linked.subagent, runId: RunId.make("run:replacement") } }, + backgroundTurnItemEvent(ids, "dynamic_tool", "completed", 1), + backgroundWork(ids.rootProviderTurnId, false), + ]; + }, + { + keepEventStreamOpen: true, + hasPendingBackgroundWorkForThread: () => Ref.get(pending), + onFinalized: () => Ref.set(pending, false), + onEventsConsumed: Deferred.succeed(consumed, undefined).pipe(Effect.asVoid), + }, + ).pipe(Effect.asSome), + Deferred.await(consumed).pipe(Effect.as(Option.none())), + ); + assert.deepEqual( + result, + Option.some(["subagent:running", "root-finalized", "turn_item:completed"]), + ); + }), +); + it.effect("keeps ingesting a child thread's late background item completion", () => Effect.gen(function* () { const observed = yield* runBackgroundItemScenario("bg-child-item", (ids) => [ @@ -2537,7 +2607,7 @@ it.effect.each(["failure", "defect", "interrupt-failure", "ownership-lost"] as c ReadonlyArray >([]); const ids = backgroundScenarioIds(`ingestion:${failureMode}`); - const { observed, finalEvents } = yield* captureRootRunTermination({ + const { written, observed, finalEvents } = yield* captureRootRunTermination({ key: `ingestion:${failureMode}`, initialProviderTurnId: null, rejectTerminalWrite: failureMode === "ownership-lost", @@ -2570,7 +2640,13 @@ it.effect.each(["failure", "defect", "interrupt-failure", "ownership-lost"] as c const error = new ProviderEventIngestor.ProviderEventPublishError({ providerSessionId: ProviderSessionId.make(`session:${failureMode}`), eventCount: 1, - cause: new Error("SQLITE_FULL: database or disk is full"), + cause: toPersistenceSqlError("OrchestrationEventStore.appendAgentEvents:insert")( + new SqlError({ + reason: classifySqliteError( + Object.assign(new Error("unable to open database file"), { errcode: 14 }), + ), + }), + ), }); return failureMode === "defect" ? Effect.die(error) : Effect.fail(error); }, @@ -2614,6 +2690,8 @@ it.effect.each(["failure", "defect", "interrupt-failure", "ownership-lost"] as c failureMode === "interrupt-failure", ); assert.deepEqual(observed, ["run:failed", "pull-requests-refreshed"]); + const error = written.find((item) => item.type === "error"); + assert.include(error?.failure.message ?? "", "(SQLITE_CANTOPEN)"); const repaired = finalEvents.find((event) => event.type === "provider-turn.updated"); if (failureMode === "interrupt-failure") { assert.isUndefined(repaired); @@ -3383,6 +3461,9 @@ function runBackgroundItemScenario( makeEvents: (ids: BackgroundScenarioIds) => ReadonlyArray, options?: { readonly keepEventStreamOpen?: boolean; + readonly hasPendingBackgroundWorkForThread?: ( + providerThread: OrchestrationV2ProviderThread, + ) => Effect.Effect; readonly loadInheritedBackgroundTurnItems?: () => Effect.Effect< ReadonlyArray<{ readonly id: TurnItemId; readonly runId: RunId }> >; @@ -3453,6 +3534,9 @@ function runBackgroundItemScenario( providerSessionId: ProviderSessionId.make(`session:${key}`), session: { events: Stream.empty, + ...(options?.hasPendingBackgroundWorkForThread === undefined + ? {} + : { hasPendingBackgroundWorkForThread: options.hasPendingBackgroundWorkForThread }), subscribeEvents: Effect.gen(function* () { yield* options?.onSubscribe ?? Effect.void; const events = Stream.fromIterable(makeEvents(ids)).pipe( diff --git a/apps/server/src/orchestration-v2/RunExecutionService.ts b/apps/server/src/orchestration-v2/RunExecutionService.ts index d9687b79324cc..82f74dff96e8e 100644 --- a/apps/server/src/orchestration-v2/RunExecutionService.ts +++ b/apps/server/src/orchestration-v2/RunExecutionService.ts @@ -47,11 +47,13 @@ import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter" import * as ProviderEventIngestor from "./ProviderEventIngestor.ts"; import type { ProjectionStoreV2Error } from "./ProjectionStore.ts"; import { + knownProviderFailureMessage, makeProviderFailure, makeProviderFailureTurnItem, } from "@t3tools/provider-core/server/failure"; import * as RunFinalizationService from "./RunFinalizationService.ts"; import * as TurnItemPositionStore from "./TurnItemPositionStore.ts"; +import { userFacingDatabaseErrorMessage } from "./UserFacingErrors.ts"; export interface ProviderEventRoutingState { readonly ownedThreadIds: ReadonlySet; @@ -506,6 +508,8 @@ export function routeProviderEvent( state.ownedProviderTurns.has(event.runtimeRequest.providerTurnId)), state, ]; + case "background_work.updated": + return [state.ownedProviderTurns.has(event.providerTurnId), state]; case "command_output.delta": // The session manager hands live output to the command output hub; runs never see it. return [false, state]; @@ -1027,6 +1031,7 @@ export const layer: Layer.Layer< const rootTerminalSeen = yield* Ref.make(false); const rootRunFinalized = yield* Ref.make(false); const providerThreadOwnerLost = yield* Ref.make(false); + const pendingBackgroundProviderTurns = new Set(); const activeChildProviderTurns = yield* Ref.make>(new Set()); const activeChildSubagents = yield* Ref.make>(new Map()); const activeBackgroundTurnItems = yield* Ref.make< @@ -1228,6 +1233,9 @@ export const layer: Layer.Layer< ) { return true; } + if (pendingBackgroundProviderTurns.size > 0) { + return false; + } const childProviderTurns = yield* Ref.get(activeChildProviderTurns); if (childProviderTurns.size > 0) { return false; @@ -1345,6 +1353,11 @@ export const layer: Layer.Layer< (current) => new Set([...current].filter((id) => routing.ownedProviderTurns.has(id))), ); + for (const id of pendingBackgroundProviderTurns) { + if (!routing.ownedProviderTurns.has(id)) { + pendingBackgroundProviderTurns.delete(id); + } + } yield* Ref.update( activeBackgroundTurnItems, (current) => new Set([...current].filter((id) => !detached.itemIds.has(id))), @@ -1449,6 +1462,13 @@ export const layer: Layer.Layer< yield* Ref.set(rootTerminalSeen, true); yield* finalizeRootRun(event); } + if (event.type === "background_work.updated") { + if (event.pending) { + pendingBackgroundProviderTurns.add(event.providerTurnId); + } else { + pendingBackgroundProviderTurns.delete(event.providerTurnId); + } + } yield* trackChildLifecycle(event, deliveredEvent !== null); }), ), @@ -1525,6 +1545,7 @@ export const layer: Layer.Layer< : null; const latestItemOrdinal = yield* Ref.get(latestTurnItemOrdinal); const openSubagents = yield* Ref.get(openRunOwnedSubagents); + const squashed = Cause.squash(cause); let nextRetryWarningAt = completedAt; yield* writeFinalRunEvents({ run: input.run, @@ -1540,7 +1561,13 @@ export const layer: Layer.Layer< ...(abandonedProviderTurn === null ? {} : { abandonedProviderTurn }), completedAt, terminal: makeFailedTerminalEvent( - makeProviderFailure({ cause: Cause.squash(cause), class: "unknown" }), + makeProviderFailure({ + cause: squashed, + message: + knownProviderFailureMessage(squashed) ?? + userFacingDatabaseErrorMessage(squashed), + class: "unknown", + }), latestItemOrdinal + 1, ), failureItemPersisted: false, diff --git a/apps/server/src/orchestration-v2/SubagentProjection.test.ts b/apps/server/src/orchestration-v2/SubagentProjection.test.ts index 95abcbaf5c1a8..c410d6682da38 100644 --- a/apps/server/src/orchestration-v2/SubagentProjection.test.ts +++ b/apps/server/src/orchestration-v2/SubagentProjection.test.ts @@ -120,14 +120,17 @@ it("keeps a subagent child awake when its parent thread is snoozed", () => { }); }); -it.effect.each([ - ["sql", ProjectionStore.layer.pipe(Layer.provideMerge(SqlitePersistence.layerMemory))], -] as const)( - "%s: a pinned parent's subagent child is born unpinned and settles once idle", - ([, testLayer]) => +it.effect.each(["provider", "mcp"] as const)( + "a %s child does not inherit parent settlement protections and settles once idle", + (creationSource) => Effect.gen(function* () { const store = yield* ProjectionStore.ProjectionStoreV2; - const parentThread = { ...makeParentThread(), pinnedAt: parentCreatedAt, pinOrderKey: "a0" }; + const parentThread = { + ...makeParentThread(), + pinnedAt: parentCreatedAt, + pinOrderKey: "a0", + autoSettleDisabledAt: parentCreatedAt, + }; const childThread = makeSubagentChildThread({ parentThread, childThreadId, @@ -138,13 +141,14 @@ it.effect.each([ title: "Child", now: childCreatedAt, createdBy: "agent", - creationSource: "provider", + creationSource, }); assert.equal(parentThread.pinnedAt, parentCreatedAt); assert.equal(parentThread.pinOrderKey, "a0"); assert.isNull(childThread.pinnedAt); assert.isNull(childThread.pinOrderKey); + assert.isNull(childThread.autoSettleDisabledAt); for (const thread of [parentThread, childThread]) { yield* store.apply({ @@ -158,6 +162,7 @@ it.effect.each([ const savedParent = yield* store.getThread(parentThreadId); assert.deepEqual(savedParent.pinnedAt, parentCreatedAt); assert.equal(savedParent.pinOrderKey, "a0"); + assert.deepEqual(savedParent.autoSettleDisabledAt, parentCreatedAt); const candidates = yield* store.getSettlementCandidates(); assert.deepEqual( candidates.map((candidate) => candidate.id), @@ -171,7 +176,9 @@ it.effect.each([ autoSettleOnMerge: false, }); assert.equal(settledAt && DateTime.formatIso(settledAt), DateTime.formatIso(childCreatedAt)); - }).pipe(Effect.provide(testLayer)), + }).pipe( + Effect.provide(ProjectionStore.layer.pipe(Layer.provideMerge(SqlitePersistence.layerMemory))), + ), ); function taskFixture() { diff --git a/apps/server/src/orchestration-v2/ThreadForkService.test.ts b/apps/server/src/orchestration-v2/ThreadForkService.test.ts index b6b89758f7b5d..e1bf75dd12846 100644 --- a/apps/server/src/orchestration-v2/ThreadForkService.test.ts +++ b/apps/server/src/orchestration-v2/ThreadForkService.test.ts @@ -1,5 +1,6 @@ import { assert, it } from "@effect/vitest"; import { + CommandId, ContextTransferId, MessageId, type ModelSelection, @@ -159,6 +160,24 @@ it.effect("keeps a fork awake when its source thread is snoozed", () => }), ); +it.effect("starts a fork without the source thread's pending title regeneration", () => + Effect.gen(function* () { + const sourceRun = makeSourceRun("completed"); + const titleRegeneration = { + requestId: CommandId.make("command:fork-source-title"), + startedAt: snoozedAt, + }; + const sourceProjection = { + ...makeSourceProjection(sourceRun), + thread: { ...makeSourceThread(), titleRegeneration }, + }; + const result = yield* planFork(sourceRun, sourceProjection); + + assert.isNull(result.targetThread.titleRegeneration); + assert.deepEqual(sourceProjection.thread.titleRegeneration, titleRegeneration); + }), +); + it.effect("preserves pull request context without inheriting any watches", () => Effect.gen(function* () { const sourceRun = makeSourceRun("completed"); diff --git a/apps/server/src/orchestration-v2/ThreadForkService.ts b/apps/server/src/orchestration-v2/ThreadForkService.ts index a660de9a4e6a1..71ff9764868ad 100644 --- a/apps/server/src/orchestration-v2/ThreadForkService.ts +++ b/apps/server/src/orchestration-v2/ThreadForkService.ts @@ -90,6 +90,8 @@ export const layer: Layer.Layer = Layer.succeed( creationSource: input.creationSource, id: input.targetThreadId, title: input.title ?? `${input.sourceProjection.thread.title} fork`, + // The source's title job completes only on the source thread. + titleRegeneration: null, activeProviderThreadId: null, lineage: { parentThreadId: input.sourceProjection.thread.id, diff --git a/apps/server/src/orchestration-v2/ThreadLaunchService.test.ts b/apps/server/src/orchestration-v2/ThreadLaunchService.test.ts index 914ce43864ac5..cb361bec6b215 100644 --- a/apps/server/src/orchestration-v2/ThreadLaunchService.test.ts +++ b/apps/server/src/orchestration-v2/ThreadLaunchService.test.ts @@ -1162,6 +1162,100 @@ it.effect("shows the fetch diagnosis when preparing a worktree from origin fails }).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) => diff --git a/apps/server/src/orchestration-v2/ThreadManagementService.ts b/apps/server/src/orchestration-v2/ThreadManagementService.ts index 9bbdf74b47a4c..fa07b76f93a63 100644 --- a/apps/server/src/orchestration-v2/ThreadManagementService.ts +++ b/apps/server/src/orchestration-v2/ThreadManagementService.ts @@ -725,6 +725,22 @@ const make = Effect.gen(function* () { }), ), ); + case "prepared-run.release": + case "prepared-run.retry": + return orchestrator + .getThreadRecords(command.threadId, ["runs"], { runIds: [command.runId] }) + .pipe( + Effect.flatMap((projection) => { + const run = projection.runs.find((candidate) => candidate.id === command.runId); + return run === undefined + ? Effect.void + : assertProviderInstanceEnabled({ + projectId: projection.thread.projectId, + commandId: command.commandId, + instanceId: run.providerInstanceId, + }); + }), + ); default: return Effect.void; } diff --git a/apps/server/src/orchestration-v2/ThreadSearch.test.ts b/apps/server/src/orchestration-v2/ThreadSearch.test.ts index 399943c7b1b92..5d816133c89c7 100644 --- a/apps/server/src/orchestration-v2/ThreadSearch.test.ts +++ b/apps/server/src/orchestration-v2/ThreadSearch.test.ts @@ -18,6 +18,7 @@ import * as Fiber from "effect/Fiber"; import * as Layer from "effect/Layer"; import * as Path from "effect/Path"; import * as SqlClient from "effect/sql/SqlClient"; +import { ATTACHMENT_ONLY_BOOTSTRAP_PROMPT } from "@t3tools/shared/composerContextReferences"; import * as SqlitePersistence from "../persistence/Sqlite.ts"; import * as ProjectionStore from "./ProjectionStore.ts"; @@ -141,6 +142,119 @@ const message = ( }, }); +it.effect.each([true, false])( + "ThreadSearch excludes bootstrap text before the hit and candidate limits (indexed: %s)", + (indexed) => + Effect.gen(function* () { + const projections = yield* ProjectionStore.ProjectionStoreV2; + const search = yield* ThreadSearch.ThreadSearch; + const sql = yield* SqlClient.SqlClient; + const project = ProjectId.make("project:attachments"); + const threadId = ThreadId.make("thread:attachments"); + yield* createProject(project); + yield* Effect.forEach( + [ + thread(threadId, project), + message(threadId, "ordinary", "user", "Review the attached files in context", { + minute: 1, + }), + message(threadId, "attachment", "user", ATTACHMENT_ONLY_BOOTSTRAP_PROMPT, { minute: 2 }), + message( + threadId, + "ultrathink", + "user", + `Ultrathink:\n${ATTACHMENT_ONLY_BOOTSTRAP_PROMPT}`, + { minute: 3 }, + ), + ], + projections.apply, + { discard: true }, + ); + if (!indexed) { + yield* sql`DROP TRIGGER orchestration_v2_message_search_insert`; + yield* sql`DROP TRIGGER orchestration_v2_message_search_update`; + yield* sql`DROP TRIGGER orchestration_v2_message_search_delete`; + yield* sql`DROP TABLE orchestration_v2_message_search`; + } + assert.deepEqual((yield* search.search({ query: "conversation context" })).matches, []); + assert.deepEqual((yield* search.search({ query: "ultrathink" })).matches, []); + assert.deepEqual( + (yield* search.search({ query: "attached files" })).matches.map((match) => [ + match.messageId, + match.snippet, + ]), + [[MessageId.make("ordinary"), "Review the attached files in context"]], + ); + for (const text of [ + ATTACHMENT_ONLY_BOOTSTRAP_PROMPT, + `Ultrathink:\n${ATTACHMENT_ONLY_BOOTSTRAP_PROMPT}`, + `uLtRaThInK:\r\n\t\u00a0${ATTACHMENT_ONLY_BOOTSTRAP_PROMPT}`, + ]) { + yield* sql` + WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < ${indexed ? 10_000 : 2000}) + INSERT INTO orchestration_v2_projection_messages + (message_id, thread_id, role, streaming, created_at, updated_at, payload_json) + SELECT 'attachment-limit:' || i, ${threadId}, 'user', 0, + ${DateTime.formatIso(at(5))}, ${DateTime.formatIso(at(5))}, json_object('text', ${text}) + FROM n + `; + assert.deepEqual( + (yield* search.search({ query: "context" })).matches.map((match) => match.messageId), + [MessageId.make("ordinary")], + ); + yield* sql` + DELETE FROM orchestration_v2_projection_messages WHERE message_id LIKE 'attachment-limit:%' + `; + } + }).pipe(Effect.provide(layerTest)), +); + +it.effect("ThreadSearch excludes legacy placeholders still in the index", () => + Effect.gen(function* () { + const projections = yield* ProjectionStore.ProjectionStoreV2; + const search = yield* ThreadSearch.ThreadSearch; + const sql = yield* SqlClient.SqlClient; + const project = ProjectId.make("project:legacy-attachments"); + const threadId = ThreadId.make("thread:legacy-attachments"); + yield* createProject(project); + // Simulate the pre-upgrade trigger, which indexed attachment placeholders. + yield* sql`DROP TRIGGER orchestration_v2_message_search_insert`; + yield* sql` + CREATE TRIGGER orchestration_v2_message_search_insert + AFTER INSERT ON orchestration_v2_projection_messages + BEGIN + INSERT INTO orchestration_v2_message_search (rowid, text) + VALUES (new.rowid, json_extract(new.payload_json, '$.text')); + END + `; + yield* Effect.forEach( + [ + thread(threadId, project), + message(threadId, "ordinary", "user", "Review the attached files in context"), + message(threadId, "attachment", "user", ATTACHMENT_ONLY_BOOTSTRAP_PROMPT), + message( + threadId, + "ultrathink", + "user", + `uLtRaThInK:\r\n\t\u00a0${ATTACHMENT_ONLY_BOOTSTRAP_PROMPT}`, + ), + ], + projections.apply, + { discard: true }, + ); + assert.lengthOf( + yield* sql`SELECT rowid FROM orchestration_v2_message_search WHERE orchestration_v2_message_search MATCH '"context"'`, + 3, + ); + assert.deepEqual((yield* search.search({ query: "conversation context" })).matches, []); + assert.deepEqual((yield* search.search({ query: "ultrathink" })).matches, []); + assert.deepEqual( + (yield* search.search({ query: "context" })).matches.map((match) => match.messageId), + [MessageId.make("ordinary")], + ); + }).pipe(Effect.provide(layerTest)), +); + it.layer(layerTest)("ThreadSearch", (it) => { it.effect("reads the newest hits without sorting all of them", () => Effect.gen(function* () { @@ -153,6 +267,15 @@ it.layer(layerTest)("ThreadSearch", (it) => { plan.some((row) => row.detail.includes("TEMP B-TREE")), plan.map((row) => row.detail).join("\n"), ); + if (match !== null) { + assert.lengthOf( + plan.filter((row) => + row.detail.startsWith("SEARCH messages USING INTEGER PRIMARY KEY"), + ), + 1, + plan.map((row) => row.detail).join("\n"), + ); + } } }), ); diff --git a/apps/server/src/orchestration-v2/ThreadSearch.ts b/apps/server/src/orchestration-v2/ThreadSearch.ts index 37a1b9e726f9b..62c7f4eed45ec 100644 --- a/apps/server/src/orchestration-v2/ThreadSearch.ts +++ b/apps/server/src/orchestration-v2/ThreadSearch.ts @@ -33,7 +33,7 @@ import { type ThreadSearchSubject, } from "@t3tools/shared/paletteSearch"; import { threadSearchPullRequests } from "@t3tools/shared/threadPullRequests"; -import { indexedMessageCondition } from "./messageSearchIndex.ts"; +import { indexedMessageCondition, nonPlaceholderMessageCondition } from "./messageSearchIndex.ts"; import { loadOlderSettledThreadsPage } from "./ShellStream.ts"; // When the user last wrote in the thread aliased `threads`, as the clients @@ -209,11 +209,12 @@ export const messageHitsQuery = ( INNER JOIN eligible ON eligible.thread_id = messages.thread_id WHERE orchestration_v2_message_search MATCH ${match} AND orchestration_v2_message_search.rowid >= coalesce(( - SELECT rowid FROM orchestration_v2_message_search + SELECT orchestration_v2_message_search.rowid FROM orchestration_v2_message_search WHERE orchestration_v2_message_search MATCH ${match} - ORDER BY rowid DESC + ORDER BY orchestration_v2_message_search.rowid DESC LIMIT 1 OFFSET ${MESSAGE_CANDIDATE_LIMIT - 1} ), 0) + AND ${nonPlaceholderMessageCondition(sql)} ` } -- Answers to an agent's question show inside its question card, where diff --git a/apps/server/src/orchestration-v2/ThreadTitleRegenerationService.test.ts b/apps/server/src/orchestration-v2/ThreadTitleRegenerationService.test.ts index 8759917344060..8300d50acb021 100644 --- a/apps/server/src/orchestration-v2/ThreadTitleRegenerationService.test.ts +++ b/apps/server/src/orchestration-v2/ThreadTitleRegenerationService.test.ts @@ -1,6 +1,8 @@ import { assert, describe, it, vi } from "@effect/vitest"; import { CommandId, + ChatAttachmentId, + type ChatAttachment, EventId, MessageId, ProjectId, @@ -14,6 +16,7 @@ import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; +import { ATTACHMENT_ONLY_BOOTSTRAP_PROMPT } from "@t3tools/shared/composerContextReferences"; import * as SqlitePersistence from "../persistence/Sqlite.ts"; import * as ProjectStore from "./ProjectStore.ts"; @@ -134,6 +137,7 @@ function dispatchUserMessage(input: { readonly command: string; readonly threadId: ThreadId; readonly text: string; + readonly attachments?: ReadonlyArray; }) { return Effect.gen(function* () { const threads = yield* ThreadManagement.ThreadManagementService; @@ -143,7 +147,7 @@ function dispatchUserMessage(input: { threadId: input.threadId, messageId: MessageId.make(`${input.command}:message`), text: input.text, - attachments: [], + attachments: input.attachments ?? [], modelSelection, dispatchMode: { type: "defer_start" }, createdBy: "user", @@ -270,6 +274,44 @@ describe("formatThreadTitleContext", () => { }); describe("ThreadTitleRegenerationService", () => { + it.effect( + "titles an image-only first message without sending bootstrap text to the title model", + () => + Effect.gen(function* () { + for (const prefix of ["", "Ultrathink:\n"]) { + const harness = makeHarness(); + yield* Effect.gen(function* () { + const threads = yield* ThreadManagement.ThreadManagementService; + const service = yield* ThreadTitleRegeneration.ThreadTitleRegenerationService; + const threadId = yield* createThread({ + command: "image:create", + thread: "image:thread", + }); + const text = prefix + ATTACHMENT_ONLY_BOOTSTRAP_PROMPT; + const attachments = [ + { + type: "image" as const, + id: ChatAttachmentId.make("diagram"), + name: "diagram.png", + mimeType: "image/png", + sizeBytes: 10, + }, + ]; + yield* dispatchUserMessage({ command: "image:message", threadId, text, attachments }); + const requestId = yield* armRegeneration({ command: "image:title", threadId }); + const messageId = MessageId.make("image:message:message"); + yield* service.execute({ threadId, requestId, kind: { type: "initial", messageId } }); + const call = harness.generateThreadTitle.mock.calls[0]?.[0]; + assert.equal(call?.message, ""); + assert.deepEqual(call?.attachments, attachments); + const projection = yield* threads.getThreadProjection(threadId); + assert.equal(projection.messages[0]?.text, text); + assert.equal(projection.thread.title, "Generated title"); + }).pipe(Effect.provide(harness.layer)); + } + }), + ); + it.effect("arms and clears the regeneration marker through metadata commands", () => Effect.gen(function* () { const harness = makeHarness(); diff --git a/apps/server/src/orchestration-v2/ThreadTitleRegenerationService.ts b/apps/server/src/orchestration-v2/ThreadTitleRegenerationService.ts index b73f6a2ab061a..601b783675d0e 100644 --- a/apps/server/src/orchestration-v2/ThreadTitleRegenerationService.ts +++ b/apps/server/src/orchestration-v2/ThreadTitleRegenerationService.ts @@ -1,4 +1,5 @@ import { resolveProjectSettings } from "@t3tools/shared/projectSettings"; +import { isAttachmentOnlyBootstrapPrompt } from "@t3tools/shared/composerContextReferences"; import { CommandId, type ChatAttachment, @@ -138,7 +139,10 @@ const make = Effect.gen(function* () { context = message === undefined ? { message: "", attachments: [] } - : { message: message.text, attachments: message.attachments }; + : { + message: isAttachmentOnlyBootstrapPrompt(message.text) ? "" : message.text, + attachments: message.attachments, + }; } else { context = formatThreadTitleContext( projection.messages.filter((message) => !message.streaming), diff --git a/apps/server/src/orchestration-v2/TurnStartReads.test.ts b/apps/server/src/orchestration-v2/TurnStartReads.test.ts index caa941396a77b..6db6b4c308827 100644 --- a/apps/server/src/orchestration-v2/TurnStartReads.test.ts +++ b/apps/server/src/orchestration-v2/TurnStartReads.test.ts @@ -9,6 +9,7 @@ import { ProviderDriverKind, ProviderInstanceId, RunId, + RuntimeRequestId, ThreadId, TurnItemId, type OrchestrationV2AppThread, @@ -360,6 +361,57 @@ it.effect.each(["sqlite"] as const)( yield* store.getTurnStartContext(ThreadId.make("missing"), runId).pipe(Effect.flip), ProjectionStore.ProjectionStoreThreadNotFoundError, ); + const file = { + type: "file" as const, + id: "message-file", + name: "notes.pdf", + mimeType: "application/pdf", + sizeBytes: 12, + }; + yield* store.apply({ + id: EventId.make("attachment:message-event"), + type: "message.updated", + threadId, + runId, + occurredAt: now, + payload: { ...message, attachments: [file] }, + }); + yield* store.apply({ + id: EventId.make("attachment:question-event"), + type: "turn-item.updated", + threadId, + runId: oldRunId, + occurredAt: now, + payload: { + ...item, + id: TurnItemId.make("attachment:question"), + type: "user_input_request", + requestId: RuntimeRequestId.make("attachment:request"), + questions: [], + questionAnswer: { + requestId: "attachment:request", + answers: { document: ["See document"], image: ["See image"] }, + attachmentsByQuestionId: { + document: [{ ...file, id: "answer-document" }], + image: [ + { + type: "image", + id: "answer-image", + name: "photo.png", + mimeType: "image/png", + sizeBytes: 8, + }, + ], + }, + }, + }, + }); + assert.deepEqual((yield* store.getThreadAttachmentIds(threadId)).toSorted(), [ + "answer-document", + "answer-image", + "attachment:old", + "message-file", + ]); }).pipe(Effect.provide(layerDatabase)), ); diff --git a/apps/server/src/orchestration-v2/UsageLimitRecoveryWorker.test.ts b/apps/server/src/orchestration-v2/UsageLimitRecoveryWorker.test.ts new file mode 100644 index 0000000000000..2e077314b8212 --- /dev/null +++ b/apps/server/src/orchestration-v2/UsageLimitRecoveryWorker.test.ts @@ -0,0 +1,70 @@ +import { assert, it } from "@effect/vitest"; +import type { OrchestrationV2DomainEvent } from "@t3tools/contracts"; +import * as Clock from "effect/Clock"; +import * as DateTime from "effect/DateTime"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as Queue from "effect/Queue"; +import * as Stream from "effect/Stream"; +import * as TestClock from "effect/testing/TestClock"; + +import * as Scheduler from "../scheduling/Scheduler.ts"; +import * as ServerSettings from "../serverSettings.ts"; +import * as Orchestrator from "./Orchestrator.ts"; +import * as ProjectionStore from "./ProjectionStore.ts"; +import * as ThreadManagement from "./ThreadManagementService.ts"; +import * as UsageLimitRecoveryWorker from "./UsageLimitRecoveryWorker.ts"; + +it.effect("idle sweeps skip SQL until the next wake time or a relevant event", () => + Effect.gen(function* () { + yield* TestClock.setTime(Date.parse("2026-07-01T09:00:00.000Z")); + const startMs = Date.parse("2026-07-01T09:00:00.000Z"); + const queries = yield* Queue.unbounded(); + const events = yield* Queue.unbounded(); + const layer = UsageLimitRecoveryWorker.layer.pipe( + Layer.provide( + Layer.mergeAll( + Scheduler.layer, + Layer.mock(ProjectionStore.ProjectionStoreV2)({ + getLimitRecoveryCandidates: () => + Clock.currentTimeMillis.pipe( + Effect.flatMap((nowMs) => Queue.offer(queries, nowMs - startMs)), + Effect.as([]), + ), + // One armed thread resets at 60s. + getLimitRecoveryWakeAt: (now) => + Effect.succeed( + DateTime.toEpochMillis(now) < startMs + 60_000 ? startMs + 60_000 : null, + ), + }), + Layer.mock(ThreadManagement.ThreadManagementService)({}), + Layer.mock(Orchestrator.OrchestratorV2)({ + streamDomainEvents: Stream.fromQueue(events), + }), + Layer.mock(ServerSettings.ServerSettingsService)({ + getSettings: Effect.succeed({ + autoResumeLimitedThreads: true, + snoozeLimitedThreads: false, + } as never), + streamChanges: Stream.never, + }), + ), + ), + ); + yield* Effect.gen(function* () { + assert.equal(yield* Queue.take(queries), 0); + // Eleven idle ticks before the recorded wake time, then the wake tick itself. + yield* TestClock.adjust("60 seconds"); + assert.equal(yield* Queue.take(queries), 60_000); + yield* Queue.offer(events, { + type: "turn-item.updated", + payload: { type: "assistant_message" }, + } as never); + yield* TestClock.adjust("30 seconds"); + yield* Queue.offer(events, { type: "run.updated", payload: { status: "failed" } } as never); + yield* TestClock.adjust("5 seconds"); + // A streaming item at 90s does not wake it; the failed run does on the 95s tick. + assert.equal(yield* Queue.take(queries), 95_000); + }).pipe(Effect.provide(layer)); + }), +); diff --git a/apps/server/src/orchestration-v2/UsageLimitRecoveryWorker.ts b/apps/server/src/orchestration-v2/UsageLimitRecoveryWorker.ts index b6ebedfec4ca5..224eecbbe5f69 100644 --- a/apps/server/src/orchestration-v2/UsageLimitRecoveryWorker.ts +++ b/apps/server/src/orchestration-v2/UsageLimitRecoveryWorker.ts @@ -2,7 +2,11 @@ import { CommandId, MessageId, type OrchestrationV2Command } from "@t3tools/cont import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; +import * as Stream from "effect/Stream"; +import type { OrchestrationV2DomainEvent } from "@t3tools/contracts"; +import * as DueHint from "../scheduling/DueHint.ts"; import * as Scheduler from "../scheduling/Scheduler.ts"; +import { forkParked } from "../serverActivation.ts"; import * as ServerSettings from "../serverSettings.ts"; import * as ProjectionStore from "./ProjectionStore.ts"; import * as Orchestrator from "./Orchestrator.ts"; @@ -71,20 +75,39 @@ export function limitRecoveryCommand( }; } +// Events that can make a thread a candidate or change its command; streaming items cannot. +const affectsLimitRecovery = (event: OrchestrationV2DomainEvent) => + event.type.startsWith("thread.") || + event.type === "run.created" || + event.type === "run.updated" || + event.type === "runtime-request.updated" || + event.type.startsWith("provider-session.") || + (event.type === "turn-item.updated" && event.payload.type === "error"); + const makeSweep = Effect.gen(function* () { const projections = yield* ProjectionStore.ProjectionStoreV2; const threads = yield* ThreadManagement.ThreadManagementService; const orchestrator = yield* Orchestrator.OrchestratorV2; const settings = yield* ServerSettings.ServerSettingsService; + const due = DueHint.make(); + yield* forkParked( + Stream.runForEach(orchestrator.streamDomainEvents, (event) => + affectsLimitRecovery(event) ? due.invalidate : Effect.void, + ), + ); + yield* forkParked(Stream.runForEach(settings.streamChanges, () => due.invalidate)); return Effect.fn("UsageLimitRecoveryWorker.sweep")(function* () { - const preferences = yield* settings.getSettings; const now = yield* DateTime.now; + const nowMs = DateTime.toEpochMillis(now); + if (yield* due.isIdle(nowMs)) return; + const readVersion = yield* due.version; + const preferences = yield* settings.getSettings; const candidates = yield* projections.getLimitRecoveryCandidates({ now, autoResume: preferences.autoResumeLimitedThreads, snooze: preferences.snoozeLimitedThreads, }); - const nowMs = DateTime.toEpochMillis(now); + let acted = false; for (const thread of candidates) { const command = limitRecoveryCommand( thread, @@ -93,6 +116,7 @@ const makeSweep = Effect.gen(function* () { preferences.snoozeLimitedThreads, ); if (command === null) continue; + acted = true; yield* Effect.gen(function* () { if (command.type === "thread.metadata.update" && command.limitRecovery != null) { yield* orchestrator.autoArmLimitRecovery({ @@ -112,6 +136,10 @@ const makeSweep = Effect.gen(function* () { ), ); } + // A sweep that acted, or failed to, re-checks on the next tick. + if (acted) return; + const wakeAt = yield* projections.getLimitRecoveryWakeAt(now); + yield* due.record(readVersion, wakeAt ?? Infinity, nowMs); }); }); diff --git a/apps/server/src/orchestration-v2/UserFacingErrors.ts b/apps/server/src/orchestration-v2/UserFacingErrors.ts index c47f1c2227d62..7d47dffd1a762 100644 --- a/apps/server/src/orchestration-v2/UserFacingErrors.ts +++ b/apps/server/src/orchestration-v2/UserFacingErrors.ts @@ -37,6 +37,18 @@ function databaseErrorMessage(value: unknown): string | undefined { return "The T3 Code server could not complete this database request. Retry once; if it still fails, check the server logs for the database error."; } +export function userFacingDatabaseErrorMessage(cause: unknown): string | undefined { + const seen = new Set(); + let value = cause; + while (Predicate.isObject(value) && !seen.has(value)) { + seen.add(value); + const message = databaseErrorMessage(value); + if (message !== undefined) return message; + value = value.cause; + } + return undefined; +} + const CAPABILITY_REJECTION_MESSAGES: Record string> = { queued_messages: (p) => `${p} cannot queue messages behind an active run.`, active_steering: (p) => diff --git a/apps/server/src/orchestration-v2/WireProjection.test.ts b/apps/server/src/orchestration-v2/WireProjection.test.ts index d8edfcf78a892..66857c791f2fd 100644 --- a/apps/server/src/orchestration-v2/WireProjection.test.ts +++ b/apps/server/src/orchestration-v2/WireProjection.test.ts @@ -280,6 +280,21 @@ describe("orchestration V2 wire projection", () => { }); }); + it.each([ + "logs/app.log:12: Error: ENOENT: no such file or directory", + "fixtures/ci.txt:3: Process exited with exit code 2", + ])("trusts a zero exit code over failure text in the output: %s", (output) => { + const projected = projectTurnItemForWire({ + ...base, + type: "command_execution" as const, + input: "rg -n error logs fixtures", + output, + exitCode: 0, + }); + expect(projected).not.toHaveProperty("outputIndicatesFailure"); + expect(projected).toMatchObject({ exitCode: 0, outputOmitted: true }); + }); + it("omits inline file bodies but preserves file identity and change counts", () => { const item = { ...base, diff --git a/apps/server/src/orchestration-v2/WireProjection.ts b/apps/server/src/orchestration-v2/WireProjection.ts index a040f0816baf0..8f9292b29b9ff 100644 --- a/apps/server/src/orchestration-v2/WireProjection.ts +++ b/apps/server/src/orchestration-v2/WireProjection.ts @@ -158,11 +158,14 @@ export function projectTurnItemForWire(item: OrchestrationV2TurnItem): Orchestra } case "command_execution": { const { output, ...projected } = item; + // A successful search can print "ENOENT" or "exit code 2", so output text + // decides only when the provider reported no exit code. const failed = - item.outputIndicatesFailure === true || - (item.exitCode !== undefined && item.exitCode !== 0) || - (output !== undefined && - toolOutputIndicatesFailure(output.slice(0, MAX_DETAIL_STRING_BYTES))); + item.exitCode === undefined + ? item.outputIndicatesFailure === true || + (output !== undefined && + toolOutputIndicatesFailure(output.slice(0, MAX_DETAIL_STRING_BYTES))) + : item.exitCode !== 0; // Coder: an `html_render` call over the T3 tool bridge is a shell command. Keep only its // compact page reference, as the dynamic-tool output keeps upstream's, so the page shows. const htmlRender = htmlRenderFromBridgeCommand(item); diff --git a/apps/server/src/orchestration-v2/legacy/LegacyV1ThreadImporter.test.ts b/apps/server/src/orchestration-v2/legacy/LegacyV1ThreadImporter.test.ts index 3b2351b28a1e1..87059b7c1d0ae 100644 --- a/apps/server/src/orchestration-v2/legacy/LegacyV1ThreadImporter.test.ts +++ b/apps/server/src/orchestration-v2/legacy/LegacyV1ThreadImporter.test.ts @@ -2,6 +2,8 @@ import { assert, it } from "@effect/vitest"; import { ComposerContextId, EventId, + ProjectId, + ProviderInstanceId, type OrchestrationMessageContext, ThreadId, } from "@t3tools/contracts"; @@ -150,6 +152,69 @@ it.effect("propagates interruption from transcript reads and retry-marker writes }).pipe(Effect.provide(layerTest)), ); +it.effect("caches the absent legacy transcript for a native V2 thread", () => + Effect.gen(function* () { + const importer = yield* LegacyV1ThreadImporter.LegacyV1ThreadImporter; + const eventSink = yield* EventSink.EventSinkV2; + const projections = yield* ProjectionStore.ProjectionStoreV2; + const threadId = ThreadId.make("thread:v2-born"); + const providerInstanceId = ProviderInstanceId.make("codex"); + const now = DateTime.makeUnsafe("2026-01-01T00:00:00.000Z"); + yield* eventSink.write({ + events: [ + { + id: EventId.make("event:v2-born"), + type: "thread.created", + threadId, + occurredAt: now, + payload: { + id: threadId, + projectId: ProjectId.make("project:v2-born"), + title: "Native V2 thread", + providerInstanceId, + modelSelection: { instanceId: providerInstanceId, model: "gpt-6" }, + createdBy: "user", + creationSource: "web", + runtimeMode: "full-access", + interactionMode: "default", + branch: null, + worktreePath: null, + activeProviderThreadId: null, + lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId }, + forkedFrom: null, + createdAt: now, + updatedAt: now, + archivedAt: null, + settledOverride: null, + settledAt: null, + lastVisitedAt: null, + deletedAt: null, + }, + }, + ], + }); + const before = yield* projections.getThreadProjection(threadId); + let legacyLookups = 0; + for (let read = 0; read < 3; read++) { + const summary = yield* importer.ensureTranscript(threadId).pipe( + Effect.provideService(Statement.CurrentTransformer, (statement) => { + const [query, parameters] = statement.compile(); + if ( + query.includes("FROM orchestration_v2_legacy_imports") && + parameters[0] === threadId + ) { + legacyLookups++; + } + return Effect.succeed(statement); + }), + ); + assert.deepStrictEqual(summary, { importedThreadCount: 0, importedMessageCount: 0 }); + } + assert.equal(legacyLookups, 1); + assert.deepStrictEqual(yield* projections.getThreadProjection(threadId), before); + }).pipe(Effect.provide(layerTest)), +); + it.layer(layerTest)("LegacyV1ThreadImporter", (it) => { it.effect("imports lightweight shells, hydrates transcripts, and remains idempotent", () => Effect.gen(function* () { diff --git a/apps/server/src/orchestration-v2/legacy/LegacyV1ThreadImporter.ts b/apps/server/src/orchestration-v2/legacy/LegacyV1ThreadImporter.ts index ee4baf0abf83c..d82a9e80a23b5 100644 --- a/apps/server/src/orchestration-v2/legacy/LegacyV1ThreadImporter.ts +++ b/apps/server/src/orchestration-v2/legacy/LegacyV1ThreadImporter.ts @@ -677,7 +677,7 @@ const make = Effect.gen(function* () { ), ); - // transcript_imported_at never resets; cache confirmed imports for the process lifetime to skip repeated locks and reads. + // Both results are final for this process: transcript_imported_at never resets, and only startup shell reconciliation creates legacy rows. const confirmedTranscriptThreadIds = new Set(); const ensureTranscriptBase = (threadId: ThreadId) => @@ -692,9 +692,7 @@ const make = Effect.gen(function* () { `.pipe(NodeSqliteClient.readOnly); const imported = imports[0]; if (imported === undefined || imported.transcript_imported_at !== null) { - if (imported !== undefined) { - confirmedTranscriptThreadIds.add(threadId); - } + confirmedTranscriptThreadIds.add(threadId); return { importedThreadCount: 0, importedMessageCount: 0 }; } const messages = yield* listMessages(threadId); diff --git a/apps/server/src/orchestration-v2/messageSearchIndex.ts b/apps/server/src/orchestration-v2/messageSearchIndex.ts index 318a92e9efd5c..dbd3545e5906b 100644 --- a/apps/server/src/orchestration-v2/messageSearchIndex.ts +++ b/apps/server/src/orchestration-v2/messageSearchIndex.ts @@ -1,13 +1,28 @@ +import { ATTACHMENT_ONLY_BOOTSTRAP_PROMPT } from "@t3tools/shared/composerContextReferences"; import * as Effect from "effect/Effect"; import type * as SqlClient from "effect/sql/SqlClient"; +// SQLite's ltrim needs the characters matched by isAttachmentOnlyBootstrapPrompt's \s explicitly. +const BOOTSTRAP_PREFIX_WHITESPACE = + "\t\n\v\f\r \u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000\ufeff"; + +export const nonPlaceholderMessageCondition = (sql: SqlClient.SqlClient, row = "messages") => + sql.literal(` + CASE + WHEN substr(json_extract(${row}.payload_json, '$.text'), 1, 11) = 'Ultrathink:' COLLATE NOCASE + THEN ltrim(substr(json_extract(${row}.payload_json, '$.text'), 12), '${BOOTSTRAP_PREFIX_WHITESPACE}') + ELSE json_extract(${row}.payload_json, '$.text') + END COLLATE BINARY <> '${ATTACHMENT_ONLY_BOOTSTRAP_PROMPT.replaceAll("'", "''")}' +`); + /** - * Exclude generated notices and subagent threads: search hides them, and ACP subagents rewrite - * their finished answer on every streamed chunk. + * Exclude attachment placeholders, generated notices and subagent threads. ACP subagents + * rewrite their finished answer on every streamed chunk. * Migration triggers, backfill, rebuild and scans share this predicate; `row` may be `new`. */ export const indexedMessageCondition = (sql: SqlClient.SqlClient, row = "messages") => - sql.literal(` + sql` + ${sql.literal(` ${row}.streaming = 0 AND ( ${row}.role = 'assistant' @@ -21,7 +36,9 @@ export const indexedMessageCondition = (sql: SqlClient.SqlClient, row = "message WHERE subagent_threads.thread_id = ${row}.thread_id AND json_extract(subagent_threads.payload_json, '$.lineage.relationshipToParent') = 'subagent' ) -`); +`)} + AND ${nonPlaceholderMessageCondition(sql, row)} +`; /** * What the index stores for a message: its text with curly apostrophes @@ -32,6 +49,52 @@ export const indexedMessageCondition = (sql: SqlClient.SqlClient, row = "message export const indexedMessageText = (sql: SqlClient.SqlClient, payload: string) => sql.literal(`replace(replace(json_extract(${payload}, '$.text'), '’', ''''), '‘', '''')`); +export const createMessageSearchTriggers = Effect.fn("MessageSearchIndex.createTriggers")( + function* (sql: SqlClient.SqlClient) { + yield* sql` + CREATE TRIGGER orchestration_v2_message_search_insert + AFTER INSERT ON orchestration_v2_projection_messages + WHEN ${indexedMessageCondition(sql, "new")} + BEGIN + INSERT INTO orchestration_v2_message_search (rowid, text) + SELECT new.rowid, ${indexedMessageText(sql, "new.payload_json")} + WHERE json_extract(new.payload_json, '$.text') IS NOT NULL; + END + `; + // Streaming deltas rewrite the row many times a second; skip them until the + // message settles. Upserts rewrite every column, so a finished message + // re-indexes only when what the index reads changed. Deleting a rowid the + // index never held is a no-op. + yield* sql` + CREATE TRIGGER orchestration_v2_message_search_update + AFTER UPDATE OF payload_json, streaming, role ON orchestration_v2_projection_messages + WHEN (old.streaming = 0 OR new.streaming = 0) + AND ( + old.streaming IS NOT new.streaming + OR old.role IS NOT new.role + OR json_extract(old.payload_json, '$.text') IS NOT json_extract(new.payload_json, '$.text') + OR json_extract(old.payload_json, '$.createdBy') + IS NOT json_extract(new.payload_json, '$.createdBy') + ) + BEGIN + DELETE FROM orchestration_v2_message_search WHERE rowid = old.rowid AND old.streaming = 0; + INSERT INTO orchestration_v2_message_search (rowid, text) + SELECT new.rowid, ${indexedMessageText(sql, "new.payload_json")} + WHERE ${indexedMessageCondition(sql, "new")} + AND json_extract(new.payload_json, '$.text') IS NOT NULL; + END + `; + yield* sql` + CREATE TRIGGER orchestration_v2_message_search_delete + AFTER DELETE ON orchestration_v2_projection_messages + WHEN old.streaming = 0 + BEGIN + DELETE FROM orchestration_v2_message_search WHERE rowid = old.rowid; + END + `; + }, +); + /** * Refills the search index from the messages table. The index is keyed by * message rowid, so anything that may renumber or copy rows (VACUUM, a dev diff --git a/apps/server/src/orchestration-v2/runtimeLayer.test.ts b/apps/server/src/orchestration-v2/runtimeLayer.test.ts index 1f3b961e8e804..c7a4b384c8e40 100644 --- a/apps/server/src/orchestration-v2/runtimeLayer.test.ts +++ b/apps/server/src/orchestration-v2/runtimeLayer.test.ts @@ -1721,7 +1721,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive", (it) => { }), ); - it.effect("merges an explicit provider-finished run while checkpoint capture is pending", () => + it.effect("refuses busy merge-back targets without blocking the queue", () => Effect.gen(function* () { const orchestrator = yield* Orchestrator.OrchestratorV2; const eventSink = yield* EventSink.EventSinkV2; @@ -1736,6 +1736,13 @@ it.layer(layerTest)("OrchestrationV2LayerLive", (it) => { ); const forkTransferId = ContextTransferId.make("runtime-layer-waiting-merge-fork-transfer"); + yield* seedProject({ + projectId, + title: "Merge-back project", + workspaceRoot: process.cwd(), + defaultModelSelection: modelSelection, + createdAt: DateTime.formatIso(now), + }); yield* orchestrator.dispatch({ type: "thread.create", createdBy: "user", @@ -1863,6 +1870,87 @@ it.layer(layerTest)("OrchestrationV2LayerLive", (it) => { ], }); + yield* orchestrator.dispatch({ + type: "message.dispatch", + createdBy: "user", + creationSource: "web", + commandId: CommandId.make("runtime-layer-merge-target-start"), + threadId: targetThreadId, + messageId: MessageId.make("runtime-layer-merge-target-message"), + text: "Keep working in the parent.", + attachments: [], + dispatchMode: { type: "start_immediately" }, + }); + const activeTarget = yield* orchestrator.getThreadProjection(targetThreadId); + const targetRun = activeTarget.runs[0]!; + for (const status of ["preparing", "starting", "waiting", "queued", "running"] as const) { + yield* eventSink.write({ + commandId: CommandId.make(`runtime-layer-merge-target-${status}`), + events: [ + { + id: EventId.make(`runtime-layer-merge-target-${status}-event`), + type: "run.updated", + threadId: targetThreadId, + runId: targetRun.id, + occurredAt: now, + payload: { ...targetRun, status }, + }, + ], + }); + const error = yield* orchestrator + .dispatch({ + type: "thread.merge_back", + createdBy: "user", + creationSource: "mobile", + commandId: CommandId.make(`runtime-layer-merge-refused-${status}`), + sourceThreadId, + targetThreadId, + sourcePoint: { type: "run", runId: sourceRunId }, + }) + .pipe(Effect.flip); + assert.instanceOf(error, Orchestrator.OrchestratorDispatchError); + assert.equal( + error.cause, + "Finish or stop the target thread's active work and clear its queue before merging back.", + ); + const refusedTarget = yield* orchestrator.getThreadProjection(targetThreadId); + assert.deepEqual(refusedTarget.contextTransfers, activeTarget.contextTransfers); + assert.equal(refusedTarget.runs[0]?.status, status); + } + yield* orchestrator.dispatch({ + type: "message.dispatch", + createdBy: "user", + creationSource: "web", + commandId: CommandId.make("runtime-layer-merge-target-queue"), + threadId: targetThreadId, + messageId: MessageId.make("runtime-layer-merge-target-queued-message"), + text: "Run this after the current turn.", + attachments: [], + dispatchMode: { type: "queue_after_active" }, + }); + const queuedTarget = yield* orchestrator.getThreadProjection(targetThreadId); + const queuedRun = queuedTarget.runs.find((run) => run.status === "queued"); + assert.isDefined(queuedRun); + yield* orchestrator.dispatch({ + type: "queued-run.cancel", + commandId: CommandId.make("runtime-layer-merge-target-cancel-queue"), + threadId: targetThreadId, + runId: queuedRun.id, + }); + yield* eventSink.write({ + commandId: CommandId.make("runtime-layer-merge-target-complete"), + events: [ + { + id: EventId.make("runtime-layer-merge-target-completed-event"), + type: "run.updated", + threadId: targetThreadId, + runId: targetRun.id, + occurredAt: now, + payload: { ...targetRun, status: "completed", completedAt: now }, + }, + ], + }); + yield* orchestrator.dispatch({ type: "thread.merge_back", createdBy: "user", diff --git a/apps/server/src/orchestration-v2/testkit/fixtures/thread_rollback_after_stop/pi_transcript.ndjson b/apps/server/src/orchestration-v2/testkit/fixtures/thread_rollback_after_stop/pi_transcript.ndjson index ffdecab232a55..a024d2db1416e 100644 --- a/apps/server/src/orchestration-v2/testkit/fixtures/thread_rollback_after_stop/pi_transcript.ndjson +++ b/apps/server/src/orchestration-v2/testkit/fixtures/thread_rollback_after_stop/pi_transcript.ndjson @@ -133,7 +133,9 @@ {"type":"emit_inbound","label":"tool_execution_start","frame":{"type":"tool_execution_start","toolCallId":"call_8ac3cb4177994abdb9aa1c57","toolName":"bash","args":{"command":"node -e \"console.log('interrupt fixture tool started'); setTimeout(() => {}, 30000)\"","timeout":35}}} {"type":"emit_inbound","label":"tool_execution_update","frame":{"type":"tool_execution_update","toolCallId":"call_8ac3cb4177994abdb9aa1c57","toolName":"bash","args":{"command":"node -e \"console.log('interrupt fixture tool started'); setTimeout(() => {}, 30000)\"","timeout":35},"partialResult":{"content":[]}}} {"type":"emit_inbound","label":"tool_execution_update","frame":{"type":"tool_execution_update","toolCallId":"call_8ac3cb4177994abdb9aa1c57","toolName":"bash","args":{"command":"node -e \"console.log('interrupt fixture tool started'); setTimeout(() => {}, 30000)\"","timeout":35},"partialResult":{"content":[{"type":"text","text":"interrupt fixture tool started\n"}],"details":{}}}} -{"type":"expect_outbound","label":"abort","frame":{"type":"abort","id":"t3-9"}} +{"type":"expect_outbound","label":"clear_queue","frame":{"type":"clear_queue","id":"t3-9"}} +{"type":"emit_inbound","label":"response:clear_queue","frame":{"id":"t3-9","type":"response","command":"clear_queue","success":true,"data":{"steering":[],"followUp":[]}}} +{"type":"expect_outbound","label":"abort","frame":{"type":"abort","id":"t3-10"}} {"type":"emit_inbound","label":"tool_execution_end","frame":{"type":"tool_execution_end","toolCallId":"call_8ac3cb4177994abdb9aa1c57","toolName":"bash","result":{"content":[{"type":"text","text":"interrupt fixture tool started\n\n\nCommand aborted"}],"details":{}},"isError":true}} {"type":"emit_inbound","label":"message_start","frame":{"type":"message_start","message":{"role":"toolResult","toolCallId":"call_8ac3cb4177994abdb9aa1c57","toolName":"bash","content":[{"type":"text","text":"interrupt fixture tool started\n\n\nCommand aborted"}],"details":{},"isError":true,"timestamp":1767225606463}}} {"type":"emit_inbound","label":"message_end","frame":{"type":"message_end","message":{"role":"toolResult","toolCallId":"call_8ac3cb4177994abdb9aa1c57","toolName":"bash","content":[{"type":"text","text":"interrupt fixture tool started\n\n\nCommand aborted"}],"details":{},"isError":true,"timestamp":1767225606463}}} @@ -144,9 +146,9 @@ {"type":"emit_inbound","label":"turn_end","frame":{"type":"turn_end","message":{"role":"assistant","content":[],"api":"openai-completions","provider":"openrouter","model":"deepseek/deepseek-v4-flash","usage":{"input":0,"output":0,"cacheRead":0,"cacheWrite":0,"totalTokens":0,"cost":{"input":0,"output":0,"cacheRead":0,"cacheWrite":0,"total":0}},"stopReason":"error","errorMessage":"This operation was aborted","timestamp":1767225606465,"thinkingLevel":"high"},"toolResults":[]}} {"type":"emit_inbound","label":"agent_end","frame":{"type":"agent_end","messages":[{"role":"user","content":[{"type":"text","text":"Run this exact local command: `node -e \"console.log('interrupt fixture tool started'); setTimeout(() => {}, 30000)\"`. Do not answer until it completes, then respond exactly: interrupt fixture should not finish naturally."}],"timestamp":1767225601969},{"role":"assistant","content":[{"type":"thinking","thinking":"The user wants me to run a command that will wait for 30 seconds, and then after it completes, respond with a specific message. But they also say \"Do not answer until it completes\" - but this command will run for 30 seconds and then naturally finish. However, the response they want is \"interrupt fixture should not finish naturally\" which contradicts the fact that it did finish naturally.\n\nWait, let me re-read. They want me to run the command and not answer until it completes. The command is a node script that logs \"interrupt fixture tool started\" and then waits 30 seconds. If I wait for it to complete, it will finish naturally after 30 seconds. But the response they want is \"interrupt fixture should not finish naturally\" - implying that the fixture should have been interrupted before finishing naturally.\n\nThis is a test scenario. Let me just run it and see what happens. Actually, maybe they want me to just run it and respond accordingly. Let me execute the command.\n\nActually, I think this is part of a test where the system might interrupt/kill the process. Let me just run the command with a sufficient timeout.","thinkingSignature":""},{"type":"toolCall","id":"call_8ac3cb4177994abdb9aa1c57","name":"bash","arguments":{"command":"node -e \"console.log('interrupt fixture tool started'); setTimeout(() => {}, 30000)\"","timeout":35}}],"api":"openai-completions","provider":"openrouter","model":"deepseek/deepseek-v4-flash","usage":{"input":77,"output":317,"cacheRead":1792,"cacheWrite":0,"reasoning":284,"totalTokens":2186,"cost":{"input":0.00000322322,"output":0.000026539239999999998,"cacheRead":0.000015002623999999998,"cacheWrite":0,"total":0.000044765084}},"stopReason":"toolUse","timestamp":1767225601972,"responseId":"gen-1790898593-E5hfYfRp6aKBI57pUKss","rawStopReason":"tool_calls","thinkingLevel":"high"},{"role":"toolResult","toolCallId":"call_8ac3cb4177994abdb9aa1c57","toolName":"bash","content":[{"type":"text","text":"interrupt fixture tool started\n\n\nCommand aborted"}],"details":{},"isError":true,"timestamp":1767225606463},{"role":"assistant","content":[],"api":"openai-completions","provider":"openrouter","model":"deepseek/deepseek-v4-flash","usage":{"input":0,"output":0,"cacheRead":0,"cacheWrite":0,"totalTokens":0,"cost":{"input":0,"output":0,"cacheRead":0,"cacheWrite":0,"total":0}},"stopReason":"error","errorMessage":"This operation was aborted","timestamp":1767225606465,"thinkingLevel":"high"}],"willRetry":false}} {"type":"emit_inbound","label":"agent_settled","frame":{"type":"agent_settled"}} -{"type":"emit_inbound","label":"response:abort","frame":{"id":"t3-9","type":"response","command":"abort","success":true}} -{"type":"expect_outbound","label":"get_entries","frame":{"type":"get_entries","since":"b184b5a8","id":"t3-10"}} -{"type":"emit_inbound","label":"response:get_entries","frame":{"id":"t3-10","type":"response","command":"get_entries","success":true,"data":{"entries":[{"type":"message","id":"ee73daae","parentId":"b184b5a8","timestamp":"2026-01-01T00:00:01.971Z","message":{"role":"user","content":[{"type":"text","text":"Run this exact local command: `node -e \"console.log('interrupt fixture tool started'); setTimeout(() => {}, 30000)\"`. Do not answer until it completes, then respond exactly: interrupt fixture should not finish naturally."}],"timestamp":1767225601969}},{"type":"message","id":"c3915df7","parentId":"ee73daae","timestamp":"2026-01-01T00:00:06.406Z","message":{"role":"assistant","content":[{"type":"thinking","thinking":"The user wants me to run a command that will wait for 30 seconds, and then after it completes, respond with a specific message. But they also say \"Do not answer until it completes\" - but this command will run for 30 seconds and then naturally finish. However, the response they want is \"interrupt fixture should not finish naturally\" which contradicts the fact that it did finish naturally.\n\nWait, let me re-read. They want me to run the command and not answer until it completes. The command is a node script that logs \"interrupt fixture tool started\" and then waits 30 seconds. If I wait for it to complete, it will finish naturally after 30 seconds. But the response they want is \"interrupt fixture should not finish naturally\" - implying that the fixture should have been interrupted before finishing naturally.\n\nThis is a test scenario. Let me just run it and see what happens. Actually, maybe they want me to just run it and respond accordingly. Let me execute the command.\n\nActually, I think this is part of a test where the system might interrupt/kill the process. Let me just run the command with a sufficient timeout.","thinkingSignature":""},{"type":"toolCall","id":"call_8ac3cb4177994abdb9aa1c57","name":"bash","arguments":{"command":"node -e \"console.log('interrupt fixture tool started'); setTimeout(() => {}, 30000)\"","timeout":35}}],"api":"openai-completions","provider":"openrouter","model":"deepseek/deepseek-v4-flash","usage":{"input":77,"output":317,"cacheRead":1792,"cacheWrite":0,"reasoning":284,"totalTokens":2186,"cost":{"input":0.00000322322,"output":0.000026539239999999998,"cacheRead":0.000015002623999999998,"cacheWrite":0,"total":0.000044765084}},"stopReason":"toolUse","timestamp":1767225601972,"responseId":"gen-1790898593-E5hfYfRp6aKBI57pUKss","rawStopReason":"tool_calls","thinkingLevel":"high"}},{"type":"message","id":"641f3d3f","parentId":"c3915df7","timestamp":"2026-01-01T00:00:06.463Z","message":{"role":"toolResult","toolCallId":"call_8ac3cb4177994abdb9aa1c57","toolName":"bash","content":[{"type":"text","text":"interrupt fixture tool started\n\n\nCommand aborted"}],"details":{},"isError":true,"timestamp":1767225606463}},{"type":"message","id":"e3ab1495","parentId":"641f3d3f","timestamp":"2026-01-01T00:00:06.465Z","message":{"role":"assistant","content":[],"api":"openai-completions","provider":"openrouter","model":"deepseek/deepseek-v4-flash","usage":{"input":0,"output":0,"cacheRead":0,"cacheWrite":0,"totalTokens":0,"cost":{"input":0,"output":0,"cacheRead":0,"cacheWrite":0,"total":0}},"stopReason":"error","errorMessage":"This operation was aborted","timestamp":1767225606465,"thinkingLevel":"high"}}],"leafId":"e3ab1495"}}} +{"type":"emit_inbound","label":"response:abort","frame":{"id":"t3-10","type":"response","command":"abort","success":true}} +{"type":"expect_outbound","label":"get_entries","frame":{"type":"get_entries","since":"b184b5a8","id":"t3-11"}} +{"type":"emit_inbound","label":"response:get_entries","frame":{"id":"t3-11","type":"response","command":"get_entries","success":true,"data":{"entries":[{"type":"message","id":"ee73daae","parentId":"b184b5a8","timestamp":"2026-01-01T00:00:01.971Z","message":{"role":"user","content":[{"type":"text","text":"Run this exact local command: `node -e \"console.log('interrupt fixture tool started'); setTimeout(() => {}, 30000)\"`. Do not answer until it completes, then respond exactly: interrupt fixture should not finish naturally."}],"timestamp":1767225601969}},{"type":"message","id":"c3915df7","parentId":"ee73daae","timestamp":"2026-01-01T00:00:06.406Z","message":{"role":"assistant","content":[{"type":"thinking","thinking":"The user wants me to run a command that will wait for 30 seconds, and then after it completes, respond with a specific message. But they also say \"Do not answer until it completes\" - but this command will run for 30 seconds and then naturally finish. However, the response they want is \"interrupt fixture should not finish naturally\" which contradicts the fact that it did finish naturally.\n\nWait, let me re-read. They want me to run the command and not answer until it completes. The command is a node script that logs \"interrupt fixture tool started\" and then waits 30 seconds. If I wait for it to complete, it will finish naturally after 30 seconds. But the response they want is \"interrupt fixture should not finish naturally\" - implying that the fixture should have been interrupted before finishing naturally.\n\nThis is a test scenario. Let me just run it and see what happens. Actually, maybe they want me to just run it and respond accordingly. Let me execute the command.\n\nActually, I think this is part of a test where the system might interrupt/kill the process. Let me just run the command with a sufficient timeout.","thinkingSignature":""},{"type":"toolCall","id":"call_8ac3cb4177994abdb9aa1c57","name":"bash","arguments":{"command":"node -e \"console.log('interrupt fixture tool started'); setTimeout(() => {}, 30000)\"","timeout":35}}],"api":"openai-completions","provider":"openrouter","model":"deepseek/deepseek-v4-flash","usage":{"input":77,"output":317,"cacheRead":1792,"cacheWrite":0,"reasoning":284,"totalTokens":2186,"cost":{"input":0.00000322322,"output":0.000026539239999999998,"cacheRead":0.000015002623999999998,"cacheWrite":0,"total":0.000044765084}},"stopReason":"toolUse","timestamp":1767225601972,"responseId":"gen-1790898593-E5hfYfRp6aKBI57pUKss","rawStopReason":"tool_calls","thinkingLevel":"high"}},{"type":"message","id":"641f3d3f","parentId":"c3915df7","timestamp":"2026-01-01T00:00:06.463Z","message":{"role":"toolResult","toolCallId":"call_8ac3cb4177994abdb9aa1c57","toolName":"bash","content":[{"type":"text","text":"interrupt fixture tool started\n\n\nCommand aborted"}],"details":{},"isError":true,"timestamp":1767225606463}},{"type":"message","id":"e3ab1495","parentId":"641f3d3f","timestamp":"2026-01-01T00:00:06.465Z","message":{"role":"assistant","content":[],"api":"openai-completions","provider":"openrouter","model":"deepseek/deepseek-v4-flash","usage":{"input":0,"output":0,"cacheRead":0,"cacheWrite":0,"totalTokens":0,"cost":{"input":0,"output":0,"cacheRead":0,"cacheWrite":0,"total":0}},"stopReason":"error","errorMessage":"This operation was aborted","timestamp":1767225606465,"thinkingLevel":"high"}}],"leafId":"e3ab1495"}}} {"type":"expect_outbound","label":"process_start@p2","frame":{"type":"process_start","args":["--mode","rpc","--provider","openrouter","--model","deepseek/deepseek-v4-flash","--no-extensions","--no-skills","--no-prompt-templates","--no-context-files","--approve","--extension",""]}} {"type":"expect_outbound","label":"switch_session@p2","frame":{"type":"switch_session","sessionPath":"/pi-sessions/session-1.jsonl","id":"t3-0"}} {"type":"emit_inbound","label":"extension_ui_request@p2","frame":{"type":"extension_ui_request","id":"00000000-0000-4000-8000-000000000003","method":"notify","message":"t3-code MCP unavailable: T3_MCP_URL or T3_MCP_BEARER_TOKEN is missing.","notifyType":"warning"}} diff --git a/apps/server/src/orchestration-v2/testkit/fixtures/turn_interrupt_mid_tool/pi_transcript.ndjson b/apps/server/src/orchestration-v2/testkit/fixtures/turn_interrupt_mid_tool/pi_transcript.ndjson index d4db23f4fd8e3..072994e9d82a9 100644 --- a/apps/server/src/orchestration-v2/testkit/fixtures/turn_interrupt_mid_tool/pi_transcript.ndjson +++ b/apps/server/src/orchestration-v2/testkit/fixtures/turn_interrupt_mid_tool/pi_transcript.ndjson @@ -41,7 +41,9 @@ {"type":"emit_inbound","label":"message_end","frame":{"type":"message_end","message":{"role":"assistant","content":[{"type":"thinking","thinking":"The user wants me to run a specific command and wait for it to complete. Let me execute it.","thinkingSignature":""},{"type":"toolCall","id":"call_9726ad99cc114832996228eb","name":"bash","arguments":{"command":"node -e \"console.log('interrupt fixture tool started'); setTimeout(() => {}, 30000)\"","timeout":35}}],"api":"openai-completions","provider":"openrouter","model":"deepseek/deepseek-v4-flash","usage":{"input":1830,"output":102,"cacheRead":0,"cacheWrite":0,"reasoning":23,"totalTokens":1932,"cost":{"input":0.0000766038,"output":0.00000853944,"cacheRead":0,"cacheWrite":0,"total":0.00008514324}},"stopReason":"toolUse","timestamp":1767225600401,"responseId":"gen-1790898592-Mzq78XbaTGiNE3YSafgy","rawStopReason":"tool_calls","thinkingLevel":"high"}}} {"type":"emit_inbound","label":"tool_execution_start","frame":{"type":"tool_execution_start","toolCallId":"call_9726ad99cc114832996228eb","toolName":"bash","args":{"command":"node -e \"console.log('interrupt fixture tool started'); setTimeout(() => {}, 30000)\"","timeout":35}}} {"type":"emit_inbound","label":"tool_execution_update","frame":{"type":"tool_execution_update","toolCallId":"call_9726ad99cc114832996228eb","toolName":"bash","args":{"command":"node -e \"console.log('interrupt fixture tool started'); setTimeout(() => {}, 30000)\"","timeout":35},"partialResult":{"content":[]}}} -{"type":"expect_outbound","label":"abort","frame":{"type":"abort","id":"t3-6"}} +{"type":"expect_outbound","label":"clear_queue","frame":{"type":"clear_queue","id":"t3-6"}} +{"type":"emit_inbound","label":"response:clear_queue","frame":{"id":"t3-6","type":"response","command":"clear_queue","success":true,"data":{"steering":[],"followUp":[]}}} +{"type":"expect_outbound","label":"abort","frame":{"type":"abort","id":"t3-7"}} {"type":"emit_inbound","label":"tool_execution_end","frame":{"type":"tool_execution_end","toolCallId":"call_9726ad99cc114832996228eb","toolName":"bash","result":{"content":[{"type":"text","text":"Command aborted"}],"details":{}},"isError":true}} {"type":"emit_inbound","label":"message_start","frame":{"type":"message_start","message":{"role":"toolResult","toolCallId":"call_9726ad99cc114832996228eb","toolName":"bash","content":[{"type":"text","text":"Command aborted"}],"details":{},"isError":true,"timestamp":1767225602282}}} {"type":"emit_inbound","label":"message_end","frame":{"type":"message_end","message":{"role":"toolResult","toolCallId":"call_9726ad99cc114832996228eb","toolName":"bash","content":[{"type":"text","text":"Command aborted"}],"details":{},"isError":true,"timestamp":1767225602282}}} @@ -52,6 +54,6 @@ {"type":"emit_inbound","label":"turn_end","frame":{"type":"turn_end","message":{"role":"assistant","content":[],"api":"openai-completions","provider":"openrouter","model":"deepseek/deepseek-v4-flash","usage":{"input":0,"output":0,"cacheRead":0,"cacheWrite":0,"totalTokens":0,"cost":{"input":0,"output":0,"cacheRead":0,"cacheWrite":0,"total":0}},"stopReason":"error","errorMessage":"This operation was aborted","timestamp":1767225602287,"thinkingLevel":"high"},"toolResults":[]}} {"type":"emit_inbound","label":"agent_end","frame":{"type":"agent_end","messages":[{"role":"system","content":"","sections":{"preamble":"","tools":"","rules":"","docs":"","cwd":""},"timestamp":1790898591817,"toolsAdded":[{"name":"read","description":"Read the contents of a file. Supports text files and images (jpg, png, gif, webp, bmp). Images are sent as attachments. For text files, output is truncated to 2000 lines or 50KB (whichever is hit first). Use offset/limit for large files. When you need the full file, continue with offset until complete.","parameters":{"type":"object","required":["path"],"properties":{"path":{"type":"string","description":"Path to the file to read (relative or absolute)"},"offset":{"type":"number","description":"Line number to start reading from (1-indexed)"},"limit":{"type":"number","description":"Maximum number of lines to read"}}},"constrainedSampling":{"type":"json_schema","strict":"prefer"}},{"name":"bash","description":"Execute a bash command in the current working directory. Returns stdout and stderr. Output is truncated to last 2000 lines or 50KB (whichever is hit first). If truncated, full output is saved to a temp file. Optionally provide a timeout in seconds.","parameters":{"type":"object","required":["command"],"properties":{"command":{"type":"string","description":"Shell command to execute"},"timeout":{"type":"number","description":"Timeout in seconds (optional, no default timeout)"}}},"constrainedSampling":{"type":"json_schema","strict":"prefer"}},{"name":"edit","description":"Edit a single file using exact text replacement. Every edits[].oldText must match a unique, non-overlapping region of the original file. If two changes affect the same block or nearby lines, merge them into one edit instead of emitting overlapping edits. Do not include large unchanged regions just to connect distant changes.","parameters":{"type":"object","required":["path","edits"],"properties":{"path":{"type":"string","description":"Path to the file to edit (relative or absolute)"},"edits":{"type":"array","items":{"type":"object","required":["oldText","newText"],"properties":{"oldText":{"type":"string","description":"Exact text for one targeted replacement. It must be unique in the original file and must not overlap with any other edits[].oldText in the same call."},"newText":{"type":"string","description":"Replacement text for this targeted edit."}}},"description":"One or more targeted replacements. Each edit is matched against the original file, not incrementally. Do not include overlapping or nested edits. If two changes touch the same block or nearby lines, merge them into one edit instead."}}},"constrainedSampling":{"type":"json_schema","strict":"prefer"}},{"name":"write","description":"Write content to a file. Creates the file if it doesn't exist, overwrites if it does. Automatically creates parent directories.","parameters":{"type":"object","required":["path","content"],"properties":{"path":{"type":"string","description":"Path to the file to write (relative or absolute)"},"content":{"type":"string","description":"Content to write to the file"}}},"constrainedSampling":{"type":"json_schema","strict":"prefer"}}]},{"role":"user","content":[{"type":"text","text":"Run this exact local command: `node -e \"console.log('interrupt fixture tool started'); setTimeout(() => {}, 30000)\"`. Do not answer until it completes, then respond exactly: interrupt fixture should not finish naturally."}],"timestamp":1767225600375},{"role":"assistant","content":[{"type":"thinking","thinking":"The user wants me to run a specific command and wait for it to complete. Let me execute it.","thinkingSignature":""},{"type":"toolCall","id":"call_9726ad99cc114832996228eb","name":"bash","arguments":{"command":"node -e \"console.log('interrupt fixture tool started'); setTimeout(() => {}, 30000)\"","timeout":35}}],"api":"openai-completions","provider":"openrouter","model":"deepseek/deepseek-v4-flash","usage":{"input":1830,"output":102,"cacheRead":0,"cacheWrite":0,"reasoning":23,"totalTokens":1932,"cost":{"input":0.0000766038,"output":0.00000853944,"cacheRead":0,"cacheWrite":0,"total":0.00008514324}},"stopReason":"toolUse","timestamp":1767225600401,"responseId":"gen-1790898592-Mzq78XbaTGiNE3YSafgy","rawStopReason":"tool_calls","thinkingLevel":"high"},{"role":"toolResult","toolCallId":"call_9726ad99cc114832996228eb","toolName":"bash","content":[{"type":"text","text":"Command aborted"}],"details":{},"isError":true,"timestamp":1767225602282},{"role":"assistant","content":[],"api":"openai-completions","provider":"openrouter","model":"deepseek/deepseek-v4-flash","usage":{"input":0,"output":0,"cacheRead":0,"cacheWrite":0,"totalTokens":0,"cost":{"input":0,"output":0,"cacheRead":0,"cacheWrite":0,"total":0}},"stopReason":"error","errorMessage":"This operation was aborted","timestamp":1767225602287,"thinkingLevel":"high"}],"willRetry":false}} {"type":"emit_inbound","label":"agent_settled","frame":{"type":"agent_settled"}} -{"type":"emit_inbound","label":"response:abort","frame":{"id":"t3-6","type":"response","command":"abort","success":true}} -{"type":"expect_outbound","label":"get_entries","frame":{"type":"get_entries","since":"4405fe6a","id":"t3-7"}} -{"type":"emit_inbound","label":"response:get_entries","frame":{"id":"t3-7","type":"response","command":"get_entries","success":true,"data":{"entries":[{"type":"model_change","id":"b78c2012","parentId":"4405fe6a","timestamp":"2026-01-01T00:00:00.366Z","provider":"openrouter","modelId":"deepseek/deepseek-v4-flash"},{"type":"session_info","id":"9dea9c23","parentId":"b78c2012","timestamp":"2026-01-01T00:00:00.370Z","name":"Replay fixture: turn_interrupt_mid_tool"},{"type":"message","id":"8dc98fa4","parentId":"9dea9c23","timestamp":"2026-01-01T00:00:00.379Z","message":{"role":"system","content":"","sections":{"preamble":"","tools":"","rules":"","docs":"","cwd":""},"timestamp":1790898591817,"toolsAdded":[{"name":"read","description":"Read the contents of a file. Supports text files and images (jpg, png, gif, webp, bmp). Images are sent as attachments. For text files, output is truncated to 2000 lines or 50KB (whichever is hit first). Use offset/limit for large files. When you need the full file, continue with offset until complete.","parameters":{"type":"object","required":["path"],"properties":{"path":{"type":"string","description":"Path to the file to read (relative or absolute)"},"offset":{"type":"number","description":"Line number to start reading from (1-indexed)"},"limit":{"type":"number","description":"Maximum number of lines to read"}}},"constrainedSampling":{"type":"json_schema","strict":"prefer"}},{"name":"bash","description":"Execute a bash command in the current working directory. Returns stdout and stderr. Output is truncated to last 2000 lines or 50KB (whichever is hit first). If truncated, full output is saved to a temp file. Optionally provide a timeout in seconds.","parameters":{"type":"object","required":["command"],"properties":{"command":{"type":"string","description":"Shell command to execute"},"timeout":{"type":"number","description":"Timeout in seconds (optional, no default timeout)"}}},"constrainedSampling":{"type":"json_schema","strict":"prefer"}},{"name":"edit","description":"Edit a single file using exact text replacement. Every edits[].oldText must match a unique, non-overlapping region of the original file. If two changes affect the same block or nearby lines, merge them into one edit instead of emitting overlapping edits. Do not include large unchanged regions just to connect distant changes.","parameters":{"type":"object","required":["path","edits"],"properties":{"path":{"type":"string","description":"Path to the file to edit (relative or absolute)"},"edits":{"type":"array","items":{"type":"object","required":["oldText","newText"],"properties":{"oldText":{"type":"string","description":"Exact text for one targeted replacement. It must be unique in the original file and must not overlap with any other edits[].oldText in the same call."},"newText":{"type":"string","description":"Replacement text for this targeted edit."}}},"description":"One or more targeted replacements. Each edit is matched against the original file, not incrementally. Do not include overlapping or nested edits. If two changes touch the same block or nearby lines, merge them into one edit instead."}}},"constrainedSampling":{"type":"json_schema","strict":"prefer"}},{"name":"write","description":"Write content to a file. Creates the file if it doesn't exist, overwrites if it does. Automatically creates parent directories.","parameters":{"type":"object","required":["path","content"],"properties":{"path":{"type":"string","description":"Path to the file to write (relative or absolute)"},"content":{"type":"string","description":"Content to write to the file"}}},"constrainedSampling":{"type":"json_schema","strict":"prefer"}}]}},{"type":"message","id":"b36f8c4a","parentId":"8dc98fa4","timestamp":"2026-01-01T00:00:00.379Z","message":{"role":"user","content":[{"type":"text","text":"Run this exact local command: `node -e \"console.log('interrupt fixture tool started'); setTimeout(() => {}, 30000)\"`. Do not answer until it completes, then respond exactly: interrupt fixture should not finish naturally."}],"timestamp":1767225600375}},{"type":"message","id":"8ea47a23","parentId":"b36f8c4a","timestamp":"2026-01-01T00:00:02.246Z","message":{"role":"assistant","content":[{"type":"thinking","thinking":"The user wants me to run a specific command and wait for it to complete. Let me execute it.","thinkingSignature":""},{"type":"toolCall","id":"call_9726ad99cc114832996228eb","name":"bash","arguments":{"command":"node -e \"console.log('interrupt fixture tool started'); setTimeout(() => {}, 30000)\"","timeout":35}}],"api":"openai-completions","provider":"openrouter","model":"deepseek/deepseek-v4-flash","usage":{"input":1830,"output":102,"cacheRead":0,"cacheWrite":0,"reasoning":23,"totalTokens":1932,"cost":{"input":0.0000766038,"output":0.00000853944,"cacheRead":0,"cacheWrite":0,"total":0.00008514324}},"stopReason":"toolUse","timestamp":1767225600401,"responseId":"gen-1790898592-Mzq78XbaTGiNE3YSafgy","rawStopReason":"tool_calls","thinkingLevel":"high"}},{"type":"message","id":"aa5aec89","parentId":"8ea47a23","timestamp":"2026-01-01T00:00:02.282Z","message":{"role":"toolResult","toolCallId":"call_9726ad99cc114832996228eb","toolName":"bash","content":[{"type":"text","text":"Command aborted"}],"details":{},"isError":true,"timestamp":1767225602282}},{"type":"message","id":"206b2637","parentId":"aa5aec89","timestamp":"2026-01-01T00:00:02.287Z","message":{"role":"assistant","content":[],"api":"openai-completions","provider":"openrouter","model":"deepseek/deepseek-v4-flash","usage":{"input":0,"output":0,"cacheRead":0,"cacheWrite":0,"totalTokens":0,"cost":{"input":0,"output":0,"cacheRead":0,"cacheWrite":0,"total":0}},"stopReason":"error","errorMessage":"This operation was aborted","timestamp":1767225602287,"thinkingLevel":"high"}}],"leafId":"206b2637"}}} +{"type":"emit_inbound","label":"response:abort","frame":{"id":"t3-7","type":"response","command":"abort","success":true}} +{"type":"expect_outbound","label":"get_entries","frame":{"type":"get_entries","since":"4405fe6a","id":"t3-8"}} +{"type":"emit_inbound","label":"response:get_entries","frame":{"id":"t3-8","type":"response","command":"get_entries","success":true,"data":{"entries":[{"type":"model_change","id":"b78c2012","parentId":"4405fe6a","timestamp":"2026-01-01T00:00:00.366Z","provider":"openrouter","modelId":"deepseek/deepseek-v4-flash"},{"type":"session_info","id":"9dea9c23","parentId":"b78c2012","timestamp":"2026-01-01T00:00:00.370Z","name":"Replay fixture: turn_interrupt_mid_tool"},{"type":"message","id":"8dc98fa4","parentId":"9dea9c23","timestamp":"2026-01-01T00:00:00.379Z","message":{"role":"system","content":"","sections":{"preamble":"","tools":"","rules":"","docs":"","cwd":""},"timestamp":1790898591817,"toolsAdded":[{"name":"read","description":"Read the contents of a file. Supports text files and images (jpg, png, gif, webp, bmp). Images are sent as attachments. For text files, output is truncated to 2000 lines or 50KB (whichever is hit first). Use offset/limit for large files. When you need the full file, continue with offset until complete.","parameters":{"type":"object","required":["path"],"properties":{"path":{"type":"string","description":"Path to the file to read (relative or absolute)"},"offset":{"type":"number","description":"Line number to start reading from (1-indexed)"},"limit":{"type":"number","description":"Maximum number of lines to read"}}},"constrainedSampling":{"type":"json_schema","strict":"prefer"}},{"name":"bash","description":"Execute a bash command in the current working directory. Returns stdout and stderr. Output is truncated to last 2000 lines or 50KB (whichever is hit first). If truncated, full output is saved to a temp file. Optionally provide a timeout in seconds.","parameters":{"type":"object","required":["command"],"properties":{"command":{"type":"string","description":"Shell command to execute"},"timeout":{"type":"number","description":"Timeout in seconds (optional, no default timeout)"}}},"constrainedSampling":{"type":"json_schema","strict":"prefer"}},{"name":"edit","description":"Edit a single file using exact text replacement. Every edits[].oldText must match a unique, non-overlapping region of the original file. If two changes affect the same block or nearby lines, merge them into one edit instead of emitting overlapping edits. Do not include large unchanged regions just to connect distant changes.","parameters":{"type":"object","required":["path","edits"],"properties":{"path":{"type":"string","description":"Path to the file to edit (relative or absolute)"},"edits":{"type":"array","items":{"type":"object","required":["oldText","newText"],"properties":{"oldText":{"type":"string","description":"Exact text for one targeted replacement. It must be unique in the original file and must not overlap with any other edits[].oldText in the same call."},"newText":{"type":"string","description":"Replacement text for this targeted edit."}}},"description":"One or more targeted replacements. Each edit is matched against the original file, not incrementally. Do not include overlapping or nested edits. If two changes touch the same block or nearby lines, merge them into one edit instead."}}},"constrainedSampling":{"type":"json_schema","strict":"prefer"}},{"name":"write","description":"Write content to a file. Creates the file if it doesn't exist, overwrites if it does. Automatically creates parent directories.","parameters":{"type":"object","required":["path","content"],"properties":{"path":{"type":"string","description":"Path to the file to write (relative or absolute)"},"content":{"type":"string","description":"Content to write to the file"}}},"constrainedSampling":{"type":"json_schema","strict":"prefer"}}]}},{"type":"message","id":"b36f8c4a","parentId":"8dc98fa4","timestamp":"2026-01-01T00:00:00.379Z","message":{"role":"user","content":[{"type":"text","text":"Run this exact local command: `node -e \"console.log('interrupt fixture tool started'); setTimeout(() => {}, 30000)\"`. Do not answer until it completes, then respond exactly: interrupt fixture should not finish naturally."}],"timestamp":1767225600375}},{"type":"message","id":"8ea47a23","parentId":"b36f8c4a","timestamp":"2026-01-01T00:00:02.246Z","message":{"role":"assistant","content":[{"type":"thinking","thinking":"The user wants me to run a specific command and wait for it to complete. Let me execute it.","thinkingSignature":""},{"type":"toolCall","id":"call_9726ad99cc114832996228eb","name":"bash","arguments":{"command":"node -e \"console.log('interrupt fixture tool started'); setTimeout(() => {}, 30000)\"","timeout":35}}],"api":"openai-completions","provider":"openrouter","model":"deepseek/deepseek-v4-flash","usage":{"input":1830,"output":102,"cacheRead":0,"cacheWrite":0,"reasoning":23,"totalTokens":1932,"cost":{"input":0.0000766038,"output":0.00000853944,"cacheRead":0,"cacheWrite":0,"total":0.00008514324}},"stopReason":"toolUse","timestamp":1767225600401,"responseId":"gen-1790898592-Mzq78XbaTGiNE3YSafgy","rawStopReason":"tool_calls","thinkingLevel":"high"}},{"type":"message","id":"aa5aec89","parentId":"8ea47a23","timestamp":"2026-01-01T00:00:02.282Z","message":{"role":"toolResult","toolCallId":"call_9726ad99cc114832996228eb","toolName":"bash","content":[{"type":"text","text":"Command aborted"}],"details":{},"isError":true,"timestamp":1767225602282}},{"type":"message","id":"206b2637","parentId":"aa5aec89","timestamp":"2026-01-01T00:00:02.287Z","message":{"role":"assistant","content":[],"api":"openai-completions","provider":"openrouter","model":"deepseek/deepseek-v4-flash","usage":{"input":0,"output":0,"cacheRead":0,"cacheWrite":0,"totalTokens":0,"cost":{"input":0,"output":0,"cacheRead":0,"cacheWrite":0,"total":0}},"stopReason":"error","errorMessage":"This operation was aborted","timestamp":1767225602287,"thinkingLevel":"high"}}],"leafId":"206b2637"}}} diff --git a/apps/server/src/persistence/Migrations.ts b/apps/server/src/persistence/Migrations.ts index c6db28e9e6d56..b18280483868e 100644 --- a/apps/server/src/persistence/Migrations.ts +++ b/apps/server/src/persistence/Migrations.ts @@ -69,6 +69,8 @@ import Migration0068 from "./Migrations/068_ShellStopRequestIndex.ts"; import Migration0069 from "./Migrations/069_ThreadMessageSearch.ts"; import Migration0072 from "./Migrations/072_WaitingLinkRequestIndex.ts"; import Migration0073 from "./Migrations/073_ActiveTurnItemIndexes.ts"; +import Migration0074 from "./Migrations/074_ExcludeAttachmentPlaceholdersFromSearch.ts"; +import Migration0075 from "./Migrations/075_SubagentDeliveryState.ts"; // Coder: T3 Coder has no auth sessions or pairing. Upstream's auth migrations keep // their IDs so later upstream migrations stay aligned, but they create nothing. @@ -152,6 +154,8 @@ export const migrationEntries = [ [71, "PeerLinkFence", omittedPeerLinkMigration], [72, "WaitingLinkRequestIndex", Migration0072], [73, "ActiveTurnItemIndexes", Migration0073], + [74, "ExcludeAttachmentPlaceholdersFromSearch", Migration0074], + [75, "SubagentDeliveryState", Migration0075], ] as const; export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const); diff --git a/apps/server/src/persistence/Migrations/069_ThreadMessageSearch.test.ts b/apps/server/src/persistence/Migrations/069_ThreadMessageSearch.test.ts index 029218bb67b1b..68e6d66ed3f7f 100644 --- a/apps/server/src/persistence/Migrations/069_ThreadMessageSearch.test.ts +++ b/apps/server/src/persistence/Migrations/069_ThreadMessageSearch.test.ts @@ -1,8 +1,10 @@ import { assert, it } from "@effect/vitest"; +import { ATTACHMENT_ONLY_BOOTSTRAP_PROMPT } from "@t3tools/shared/composerContextReferences"; import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; import * as Effect from "effect/Effect"; import * as SqlClient from "effect/sql/SqlClient"; +import { rebuildMessageSearch } from "../../orchestration-v2/messageSearchIndex.ts"; import { runMigrations } from "../Migrations.ts"; const layer = it.layer(NodeSqliteClient.layer({ filename: ":memory:" })); @@ -53,3 +55,86 @@ layer("069_ThreadMessageSearch", (it) => { }), ); }); + +it.effect.each(["inserted", "updated", "backfilled", "rebuilt"])( + "keeps normal messages indexed and excludes %s placeholders", + (path) => + Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + yield* runMigrations({ toMigrationInclusive: path === "backfilled" ? 68 : 73 }); + if (path !== "backfilled") { + // Pre-upgrade triggers indexed user text even when it was an attachment placeholder. + yield* sql`DROP TRIGGER orchestration_v2_message_search_insert`; + yield* sql`DROP TRIGGER orchestration_v2_message_search_update`; + yield* sql` + CREATE TRIGGER orchestration_v2_message_search_insert + AFTER INSERT ON orchestration_v2_projection_messages + BEGIN + INSERT INTO orchestration_v2_message_search (rowid, text) + VALUES (new.rowid, json_extract(new.payload_json, '$.text')); + END + `; + yield* sql` + CREATE TRIGGER orchestration_v2_message_search_update + AFTER UPDATE OF payload_json ON orchestration_v2_projection_messages + BEGIN + DELETE FROM orchestration_v2_message_search WHERE rowid = old.rowid; + INSERT INTO orchestration_v2_message_search (rowid, text) + VALUES (new.rowid, json_extract(new.payload_json, '$.text')); + END + `; + if (path !== "rebuilt") yield* runMigrations(); + } + const placeholders = [ + ATTACHMENT_ONLY_BOOTSTRAP_PROMPT, + ...[ + "", + " ", + "\n", + "\t\r\n", + "\v\f", + "\u00a0", + "\u1680", + "\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a", + "\u2028\u2029\u202f\u205f\u3000\ufeff", + ].map((whitespace) => `uLtRaThInK:${whitespace}${ATTACHMENT_ONLY_BOOTSTRAP_PROMPT}`), + ]; + const normal = [ + "normal context", + ATTACHMENT_ONLY_BOOTSTRAP_PROMPT.toLowerCase(), + `Ultrathink:\n${ATTACHMENT_ONLY_BOOTSTRAP_PROMPT.toLowerCase()}`, + `quoted ${ATTACHMENT_ONLY_BOOTSTRAP_PROMPT}`, + `Ultrathink:\n${ATTACHMENT_ONLY_BOOTSTRAP_PROMPT} extra`, + ` ${ATTACHMENT_ONLY_BOOTSTRAP_PROMPT}`, + `Ultrathink:\u0085${ATTACHMENT_ONLY_BOOTSTRAP_PROMPT}`, + ]; + for (const [index, text] of [...normal, ...placeholders].entries()) { + const payload = JSON.stringify({ text, createdBy: "user" }); + yield* sql` + INSERT INTO orchestration_v2_projection_messages + (message_id, thread_id, role, streaming, created_at, updated_at, payload_json) + VALUES (${String(index)}, 'thread-1', 'user', 0, + '2026-09-01T00:00:00.000Z', '2026-09-01T00:00:00.000Z', + ${path === "updated" ? JSON.stringify({ text: "normal context" }) : payload}) + `; + if (path === "updated") { + yield* sql` + UPDATE orchestration_v2_projection_messages SET payload_json = ${payload} + WHERE message_id = ${String(index)} + `; + } + } + yield* runMigrations({ toMigrationInclusive: path === "backfilled" ? 69 : undefined }); + if (path === "rebuilt") yield* rebuildMessageSearch(sql); + const indexed = yield* sql<{ readonly messageId: string }>` + SELECT messages.message_id AS "messageId" + FROM orchestration_v2_message_search AS search + INNER JOIN orchestration_v2_projection_messages AS messages ON messages.rowid = search.rowid + ORDER BY messages.rowid + `; + assert.deepEqual( + indexed.map((row) => row.messageId), + normal.map((_, index) => String(index)), + ); + }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))), +); diff --git a/apps/server/src/persistence/Migrations/069_ThreadMessageSearch.ts b/apps/server/src/persistence/Migrations/069_ThreadMessageSearch.ts index 4652debcbfd72..2c527d7e3ea99 100644 --- a/apps/server/src/persistence/Migrations/069_ThreadMessageSearch.ts +++ b/apps/server/src/persistence/Migrations/069_ThreadMessageSearch.ts @@ -3,6 +3,7 @@ import * as Predicate from "effect/Predicate"; import * as SqlClient from "effect/sql/SqlClient"; import { + createMessageSearchTriggers, indexedMessageCondition, indexedMessageText, } from "../../orchestration-v2/messageSearchIndex.ts"; @@ -39,47 +40,7 @@ export default Effect.gen(function* () { ), ); if (!created) return; - yield* sql` - CREATE TRIGGER orchestration_v2_message_search_insert - AFTER INSERT ON orchestration_v2_projection_messages - WHEN ${indexedMessageCondition(sql, "new")} - BEGIN - INSERT INTO orchestration_v2_message_search (rowid, text) - SELECT new.rowid, ${indexedMessageText(sql, "new.payload_json")} - WHERE json_extract(new.payload_json, '$.text') IS NOT NULL; - END - `; - // Streaming deltas rewrite the row many times a second; skip them until the - // message settles. Upserts rewrite every column, so a finished message - // re-indexes only when what the index reads changed. Deleting a rowid the - // index never held is a no-op. - yield* sql` - CREATE TRIGGER orchestration_v2_message_search_update - AFTER UPDATE OF payload_json, streaming, role ON orchestration_v2_projection_messages - WHEN (old.streaming = 0 OR new.streaming = 0) - AND ( - old.streaming IS NOT new.streaming - OR old.role IS NOT new.role - OR json_extract(old.payload_json, '$.text') IS NOT json_extract(new.payload_json, '$.text') - OR json_extract(old.payload_json, '$.createdBy') - IS NOT json_extract(new.payload_json, '$.createdBy') - ) - BEGIN - DELETE FROM orchestration_v2_message_search WHERE rowid = old.rowid AND old.streaming = 0; - INSERT INTO orchestration_v2_message_search (rowid, text) - SELECT new.rowid, ${indexedMessageText(sql, "new.payload_json")} - WHERE ${indexedMessageCondition(sql, "new")} - AND json_extract(new.payload_json, '$.text') IS NOT NULL; - END - `; - yield* sql` - CREATE TRIGGER orchestration_v2_message_search_delete - AFTER DELETE ON orchestration_v2_projection_messages - WHEN old.streaming = 0 - BEGIN - DELETE FROM orchestration_v2_message_search WHERE rowid = old.rowid; - END - `; + yield* createMessageSearchTriggers(sql); yield* sql` INSERT INTO orchestration_v2_message_search (rowid, text) SELECT messages.rowid, ${indexedMessageText(sql, "messages.payload_json")} diff --git a/apps/server/src/persistence/Migrations/074_ExcludeAttachmentPlaceholdersFromSearch.ts b/apps/server/src/persistence/Migrations/074_ExcludeAttachmentPlaceholdersFromSearch.ts new file mode 100644 index 0000000000000..6ec95d108f306 --- /dev/null +++ b/apps/server/src/persistence/Migrations/074_ExcludeAttachmentPlaceholdersFromSearch.ts @@ -0,0 +1,18 @@ +import * as Effect from "effect/Effect"; +import * as SqlClient from "effect/sql/SqlClient"; + +import { createMessageSearchTriggers } from "../../orchestration-v2/messageSearchIndex.ts"; + +export default Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + const index = yield* sql` + SELECT 1 FROM sqlite_master WHERE name = 'orchestration_v2_message_search' + `; + if (index.length === 0) return; + + // Legacy placeholders stay indexed until rebuild; FTS deletes can overflow last_insert_rowid at commit. + yield* sql`DROP TRIGGER orchestration_v2_message_search_insert`; + yield* sql`DROP TRIGGER orchestration_v2_message_search_update`; + yield* sql`DROP TRIGGER orchestration_v2_message_search_delete`; + yield* createMessageSearchTriggers(sql); +}); diff --git a/apps/server/src/persistence/Migrations/075_SubagentDeliveryState.ts b/apps/server/src/persistence/Migrations/075_SubagentDeliveryState.ts new file mode 100644 index 0000000000000..4406911cc8a8c --- /dev/null +++ b/apps/server/src/persistence/Migrations/075_SubagentDeliveryState.ts @@ -0,0 +1,20 @@ +import * as Effect from "effect/Effect"; +import * as SqlClient from "effect/sql/SqlClient"; + +export default Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + + // Command reads skip settled subagents by delivery state. Read from payload_json, that parsed + // every stored prompt; an added column sits after the payload's overflow pages, so the index + // must cover the whole filter. + yield* sql`ALTER TABLE orchestration_v2_projection_subagents ADD COLUMN completion_delivery_state TEXT`; + yield* sql` + UPDATE orchestration_v2_projection_subagents + SET completion_delivery_state = json_extract(payload_json, '$.completionDelivery.state') + WHERE json_type(payload_json, '$.completionDelivery') IS NOT NULL + `; + yield* sql` + CREATE INDEX orchestration_v2_projection_subagents_settled_idx + ON orchestration_v2_projection_subagents(thread_id, status, completion_delivery_state, run_id) + `; +}); diff --git a/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts b/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts index c08062c09d1ad..4fefa61ec010f 100644 --- a/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts +++ b/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts @@ -53,6 +53,8 @@ describe("V2 preview upgrade", () => { [71, "PeerLinkFence"], [72, "WaitingLinkRequestIndex"], [73, "ActiveTurnItemIndexes"], + [74, "ExcludeAttachmentPlaceholdersFromSearch"], + [75, "SubagentDeliveryState"], ]); assert.deepStrictEqual(yield* runMigrations(), []); assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports); @@ -149,6 +151,8 @@ describe("V2 preview upgrade", () => { [71, "PeerLinkFence"], [72, "WaitingLinkRequestIndex"], [73, "ActiveTurnItemIndexes"], + [74, "ExcludeAttachmentPlaceholdersFromSearch"], + [75, "SubagentDeliveryState"], ]); }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))), ); diff --git a/apps/server/src/project/AgentSessionImporter.test.ts b/apps/server/src/project/AgentSessionImporter.test.ts index 69026f91202af..4248e51b55977 100644 --- a/apps/server/src/project/AgentSessionImporter.test.ts +++ b/apps/server/src/project/AgentSessionImporter.test.ts @@ -1,5 +1,6 @@ import { expect, it } from "@effect/vitest"; import { + type AgentSessionSource, EventId, ProjectId, ProviderDriverKind, @@ -50,7 +51,7 @@ const persistence = Layer.mergeAll( function candidate( modelSelection: typeof CODEX_MODEL_SELECTION, providerSessionId: string, - provider: "codex" | "claudeAgent" = "codex", + provider: AgentSessionSource = "codex", ) { const providerInstanceId = modelSelection.instanceId; return { @@ -82,6 +83,12 @@ function candidate( } satisfies AgentSessionScanner.AgentSessionRecentThread; } +const PI_MODEL_SELECTION = { instanceId: ProviderInstanceId.make("pi"), model: "default" }; + +// Pi threads are owned by their session file path, not the session UUID. +const nativeId = ({ source }: ReturnType) => + source.provider === "pi" ? source.filePath : source.providerSessionId; + function importId(source: ReturnType) { return ThreadId.make( `import:${source.source.providerInstanceId}:${source.source.providerSessionId}`, @@ -95,19 +102,17 @@ const seedOwner = Effect.fn(function* ( const sink = yield* EventSink.EventSinkV2; const ids = yield* IdAllocator.IdAllocatorV2; const { providerInstanceId, providerSessionId, provider } = source.source; + const nativeThreadId = nativeId(source); const threadId = ThreadId.make(`owner:${providerInstanceId}:${providerSessionId}`); const driver = ProviderDriverKind.make(provider); const providerThread: OrchestrationV2ProviderThread = { - id: ids.derive.providerThread({ - driver, - nativeThreadId: providerSessionId, - }), + id: ids.derive.providerThread({ driver, nativeThreadId }), driver, providerInstanceId, providerSessionId: null, appThreadId: threadId, ownerNodeId: null, - nativeThreadRef: { driver, nativeId: providerSessionId, strength: "strong" }, + nativeThreadRef: { driver, nativeId: nativeThreadId, strength: "strong" }, nativeConversationHeadRef: null, status: "idle", firstRunOrdinal: 1, @@ -252,10 +257,12 @@ it.effect( "claudeAgent", ); const deleted = candidate(CODEX_MODEL_SELECTION, "deleted"); + const ownedPi = candidate(PI_MODEL_SELECTION, "owned-pi", "pi"); const owners = [ yield* seedOwner(active), yield* seedOwner(archived, "archived"), yield* seedOwner(deleted, "deleted"), + yield* seedOwner(ownedPi), ]; const before = yield* Effect.forEach(owners, (id) => projections.getThreadRecords(id, ["providerThreads"]), @@ -266,10 +273,19 @@ it.effect( "claudeAgent", ); const unowned = candidate(CODEX_MODEL_SELECTION, "unowned"); - const importer = yield* makeImporter([active, archived, deleted, otherInstance, unowned]); + const unownedPi = candidate(PI_MODEL_SELECTION, "unowned-pi", "pi"); + const importer = yield* makeImporter([ + active, + archived, + deleted, + ownedPi, + otherInstance, + unowned, + unownedPi, + ]); - expect(yield* importer.run).toEqual({ importedCount: 2, skippedCount: 0 }); - for (const source of [active, archived, deleted]) { + expect(yield* importer.run).toEqual({ importedCount: 3, skippedCount: 0 }); + for (const source of [active, archived, deleted, ownedPi]) { const threadId = importId(source); expect(yield* Effect.option(projections.getThread(threadId))).toEqual(Option.none()); expect(yield* events.read({ threadId }).pipe(Stream.runCollect)).toEqual([]); @@ -286,8 +302,8 @@ it.effect( "event" in stored ? [stored.event.threadId] : [], ), ), - ).toEqual(new Set([importId(otherInstance), importId(unowned)])); - for (const source of [otherInstance, unowned]) { + ).toEqual(new Set([importId(otherInstance), importId(unowned), importId(unownedPi)])); + for (const source of [otherInstance, unowned, unownedPi]) { const threadId = importId(source); const records = yield* projections.getThreadRecords(threadId, [ "messages", @@ -296,15 +312,13 @@ it.effect( expect(records.messages.map((message) => message.text)).toEqual(["Fix it", "Fixed"]); expect(records.thread.historyOrigin).toBe("v1_import"); expect(records.thread.activeProviderThreadId).toBe(records.providerThreads[0]?.id); - expect(records.providerThreads[0]?.nativeThreadRef?.nativeId).toBe( - source.source.providerSessionId, - ); + expect(records.providerThreads[0]?.nativeThreadRef?.nativeId).toBe(nativeId(source)); expect(Option.getOrThrow(yield* runtimes.getByThreadId({ threadId }))).toMatchObject({ providerInstanceId: source.source.providerInstanceId, resumeCursor: - source.source.provider === "codex" - ? { threadId: source.source.providerSessionId } - : { threadId, resume: source.source.providerSessionId }, + source.source.provider === "claudeAgent" + ? { threadId, resume: source.source.providerSessionId } + : { threadId: nativeId(source) }, runtimePayload: { importedTranscripts: [source.source] }, }); } diff --git a/apps/server/src/project/AgentSessionImporter.ts b/apps/server/src/project/AgentSessionImporter.ts index 11f168ff1d7be..038b488635053 100644 --- a/apps/server/src/project/AgentSessionImporter.ts +++ b/apps/server/src/project/AgentSessionImporter.ts @@ -250,6 +250,9 @@ const make = Effect.gen(function* () { }); } const driver = ProviderDriverKind.make(thread.source); + // Pi resumes by session file path; its session UUID only names the thread. + const nativeThreadId = + thread.source === "pi" ? source.filePath : thread.providerSessionId; const persistRuntime = runtimes.upsert( { threadId, @@ -260,9 +263,9 @@ const make = Effect.gen(function* () { status: "stopped", lastSeenAt: thread.updatedAt, resumeCursor: - thread.source === "codex" - ? { threadId: thread.providerSessionId } - : { threadId, resume: thread.providerSessionId }, + thread.source === "claudeAgent" + ? { threadId, resume: thread.providerSessionId } + : { threadId: nativeThreadId }, runtimePayload: { cwd: project.workspaceRoot }, }, { onConflict: "ignore" }, @@ -288,7 +291,7 @@ const make = Effect.gen(function* () { const providerThreadId = idAllocator.derive.providerThread({ driver, providerInstanceId: thread.providerInstanceId, - nativeThreadId: thread.providerSessionId, + nativeThreadId, }); const createdAt = dateTime(thread.createdAt); const updatedAt = dateTime(thread.updatedAt); @@ -337,7 +340,7 @@ const make = Effect.gen(function* () { ownerNodeId: null, nativeThreadRef: { driver, - nativeId: thread.providerSessionId, + nativeId: nativeThreadId, strength: "strong", }, nativeConversationHeadRef: null, diff --git a/apps/server/src/project/AgentSessionScanner.test.ts b/apps/server/src/project/AgentSessionScanner.test.ts index 0a79d62bd30e8..20488823366a7 100644 --- a/apps/server/src/project/AgentSessionScanner.test.ts +++ b/apps/server/src/project/AgentSessionScanner.test.ts @@ -46,6 +46,7 @@ const layerProjectStore = (importedWorkspaceRoots: ReadonlyArray) => interface ScannerTestInput { readonly claudeHomePath: string; readonly codexHomePath: string; + readonly piAgentDir?: string; readonly importedWorkspaceRoots?: ReadonlyArray; readonly configBaseDir?: string; readonly providerInstances?: ContractServerSettings["providerInstances"]; @@ -65,6 +66,20 @@ const layerScannerTest = (input: ScannerTestInput) => driver: ProviderDriverKind.make("codex"), config: { homePath: input.codexHomePath }, }, + // Keeps the default Pi instance off the developer's real ~/.pi. + [ProviderInstanceId.make("pi")]: { + driver: ProviderDriverKind.make("pi"), + enabled: true, + environment: [ + { + name: "PI_CODING_AGENT_DIR", + value: input.piAgentDir ?? `${input.claudeHomePath}/pi-agent`, + sensitive: false, + }, + { name: "PI_CODING_AGENT_SESSION_DIR", value: "", sensitive: false }, + ], + config: {}, + }, ...input.providerInstances, }, }), @@ -561,9 +576,32 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { const codexHomePath = yield* makeTempDir("t3code-codex-legacy-"); const claudeEnvironmentHome = yield* makeTempDir("t3code-claude-env-"); const codexEnvironmentHome = yield* makeTempDir("t3code-codex-env-"); + const piAgentDir = yield* makeTempDir("t3code-pi-agent-"); + const piSessionDir = yield* makeTempDir("t3code-pi-sessions-"); const claudeWorkspace = yield* makeTempDir("t3code-workspace-claude-"); const codexWorkspace = yield* makeTempDir("t3code-workspace-codex-"); - + const piWorkspace = yield* makeTempDir("t3code-workspace-pi-"); + const piFlatWorkspace = yield* makeTempDir("t3code-workspace-pi-flat-"); + + // Pi nests sessions per cwd by default and keeps a custom session directory flat. + for (const [filePath, cwd, mtime] of [ + [ + path.join(piAgentDir, "sessions", "--slug--", "2026-01-03_pi.jsonl"), + piWorkspace, + "2026-01-03T00:00:00.000Z", + ], + [ + path.join(piSessionDir, "2026-01-04_pi-flat.jsonl"), + piFlatWorkspace, + "2026-01-04T00:00:00.000Z", + ], + ] as const) { + yield* writeTranscript({ + filePath, + contents: `${encodeTranscriptRecord({ type: "session", version: 3, id: "pi", cwd })}\n`, + mtimeMs: Date.parse(mtime), + }); + } yield* writeTranscript({ filePath: path.join(claudeEnvironmentHome, "projects", "-actual", "session.jsonl"), contents: claudeSessionLine(claudeWorkspace), @@ -598,12 +636,31 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { environment: [{ name: "CODEX_HOME", value: codexEnvironmentHome, sensitive: false }], config: {}, }, + [ProviderInstanceId.make("pi")]: { + driver: ProviderDriverKind.make("pi"), + enabled: true, + environment: [ + { name: "PI_CODING_AGENT_DIR", value: piAgentDir, sensitive: false }, + { name: "PI_CODING_AGENT_SESSION_DIR", value: "", sensitive: false }, + ], + config: {}, + }, + [ProviderInstanceId.make("pi-flat")]: { + driver: ProviderDriverKind.make("pi"), + enabled: true, + environment: [ + { name: "PI_CODING_AGENT_SESSION_DIR", value: piSessionDir, sensitive: false }, + ], + config: {}, + }, }, }); - expect(result.candidates.map((candidate) => candidate.path)).toEqual([ - codexWorkspace, - claudeWorkspace, + expect(result.candidates.map((candidate) => [candidate.path, candidate.sources])).toEqual([ + [piFlatWorkspace, ["pi"]], + [piWorkspace, ["pi"]], + [codexWorkspace, ["codex"]], + [claudeWorkspace, ["claudeAgent"]], ]); }), ); @@ -987,13 +1044,14 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { }); describe("recentThreads", () => { - it.effect("imports recent Claude and Codex sessions for the selected project only", () => + it.effect("imports recent Claude, Codex, and Pi sessions for the selected project only", () => Effect.gen(function* () { const path = yield* Path.Path; const nowMs = Date.parse("2026-08-24T12:00:00.000Z"); yield* TestClock.setTime(nowMs); const claudeHomePath = yield* makeTempDir("t3code-claude-home-"); const codexHomePath = yield* makeTempDir("t3code-codex-home-"); + const piAgentDir = yield* makeTempDir("t3code-pi-agent-"); const workspace = yield* makeTempDir("t3code-workspace-"); const otherWorkspace = yield* makeTempDir("t3code-workspace-other-"); @@ -1057,21 +1115,96 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { ].join("\n"), mtimeMs: nowMs - 60 * 60 * 1000, }); + const piMessage = ( + id: string, + parentId: string | null, + message: { readonly role: string; readonly content: unknown }, + ) => + encodeTranscriptRecord({ + type: "message", + id, + parentId, + timestamp: "2026-08-24T11:00:00.000Z", + message, + }); + // The user rewound to the first prompt; the abandoned branch stays in the file. + yield* writeTranscript({ + filePath: path.join(piAgentDir, "sessions", "--selected--", "2026-08-24_pi.jsonl"), + contents: [ + encodeTranscriptRecord({ + type: "session", + version: 3, + id: "pi-recent", + cwd: workspace, + }), + piMessage("a1", null, { role: "user", content: "Plan the change" }), + piMessage("a2", "a1", { + role: "assistant", + content: [{ type: "text", text: "Abandoned answer" }], + }), + piMessage("a3", "a2", { role: "user", content: "Abandoned follow-up" }), + piMessage("b1", "a1", { + role: "assistant", + content: [ + { type: "thinking", thinking: "Hidden reasoning" }, + { type: "toolCall", id: "call-1", name: "bash", arguments: {} }, + ], + }), + piMessage("bn", "b1", { role: "toolResult", content: null }), + piMessage("b2", "bn", { + role: "toolResult", + content: [{ type: "text", text: "Tool output" }], + }), + piMessage("b3", "b2", { + role: "assistant", + content: [{ type: "text", text: "Active answer" }], + }), + encodeTranscriptRecord({ + type: "session_info", + id: "b4", + parentId: "b3", + name: "Pi refactor", + }), + ].join("\n"), + mtimeMs: nowMs - 30 * 60 * 1000, + }); + // Version 1 sessions predate the entry tree. + yield* writeTranscript({ + filePath: path.join(piAgentDir, "sessions", "--selected--", "2025-01-01_pi-v1.jsonl"), + contents: [ + encodeTranscriptRecord({ type: "session", id: "pi-v1", cwd: workspace }), + encodeTranscriptRecord({ + type: "message", + message: { role: "user", content: "Legacy prompt" }, + }), + encodeTranscriptRecord({ + type: "message", + message: { role: "assistant", content: [{ type: "text", text: "Legacy answer" }] }, + }), + ].join("\n"), + mtimeMs: nowMs - 2 * 24 * 60 * 60 * 1000, + }); const threads = yield* runRecentThreads({ claudeHomePath, codexHomePath, + piAgentDir, workspaceRoot: workspace, }); expect(threads.map((thread) => thread.providerSessionId)).toEqual([ + "pi-recent", "codex-recent", "claude-recent", + "pi-v1", ]); expect(threads.map((thread) => thread.messages.map((message) => message.text))).toEqual([ + ["Plan the change", "Active answer"], ["Review this code", "Looks good"], ["Fix the project", "Done"], + ["Legacy prompt", "Legacy answer"], ]); + expect(threads[0]).toMatchObject({ source: "pi", title: "Pi refactor" }); }), ); diff --git a/apps/server/src/project/AgentSessionScanner.ts b/apps/server/src/project/AgentSessionScanner.ts index 7ec79c38a38a9..0d60793cb2610 100644 --- a/apps/server/src/project/AgentSessionScanner.ts +++ b/apps/server/src/project/AgentSessionScanner.ts @@ -32,6 +32,7 @@ import { repositoryPathFromRemoteUrl } from "@t3tools/source-control-core/server import * as ServerConfig from "../config.ts"; import * as ProjectStore from "../orchestration-v2/ProjectStore.ts"; import { resolveCodexHomeLayout } from "../provider/Drivers/CodexHomeLayout.ts"; +import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment"; import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion"; import * as ServerSettings from "../serverSettings.ts"; import { sourceControlDefinitions } from "../sourceControl/sourceControlDefinitions.ts"; @@ -41,6 +42,23 @@ import { TranscriptJsonLimitError, } from "./AgentSessionJson.ts"; +// Coder: upstream exports this from provider-pi's usage reader (`server/usage.ts`), which is not +// carried (no usage dashboard); Pi session import needs only the lookup. +/** Where Pi stores session JSONL files for a provider environment, matching Pi's own lookup. */ +function resolvePiSessionsDir( + environment: NodeJS.ProcessEnv, + home: string, + path: Path.Path, +): string { + const sessionDir = environment.PI_CODING_AGENT_SESSION_DIR?.trim(); + if (sessionDir) return path.resolve(expandHomePath(sessionDir, home)); + const agentDir = environment.PI_CODING_AGENT_DIR?.trim(); + return path.resolve( + agentDir ? expandHomePath(agentDir, home) : path.join(home, ".pi", "agent"), + "sessions", + ); +} + const TRANSCRIPT_PREFIX_BYTES = 32 * 1024; const METADATA_READ_BYTES = 8 * 1024; const MAX_TRANSCRIPT_SCAN_BYTES = 1024 * 1024; @@ -78,6 +96,10 @@ const CodexTurnMetadata = Schema.Struct({ const TranscriptRecord = Schema.Struct({ type: Schema.optional(Schema.String), + // Pi entries form a tree through id/parentId; session_info entries carry its name. + id: Schema.optional(Schema.String), + parentId: Schema.optional(Schema.NullOr(Schema.String)), + name: Schema.optional(Schema.String), timestamp: Schema.optional(Schema.String), cwd: Schema.optional(Schema.String), sessionId: Schema.optional(Schema.String), @@ -106,6 +128,13 @@ const decodeClaudeSettings = Schema.decodeUnknownOption(ClaudeSettings); const decodeCodexSettings = Schema.decodeUnknownOption(CodexSettings); const decodeTranscriptRecord = Schema.decodeUnknownOption(Schema.fromJsonString(TranscriptRecord)); const decodeTranscriptValue = Schema.decodeUnknownOption(TranscriptRecord); +const decodePiEntryLinks = Schema.decodeUnknownOption( + Schema.Struct({ + type: TranscriptRecord.fields.type, + id: TranscriptRecord.fields.id, + parentId: TranscriptRecord.fields.parentId, + }), +); const selectTranscriptPath = createTranscriptJsonSelector(TranscriptRecord); const decodeCodexTurnMetadata = Schema.decodeUnknownOption(CodexTurnMetadata); @@ -287,6 +316,39 @@ function deriveTitle(source: AgentSessionSource, text: string): string | undefin return request.trim().split("\n")[0]?.slice(0, 100).trim() || undefined; } +// Pi keeps abandoned branches in the file; its active branch runs from the last entry to the root. +function activePiBranch( + records: ReadonlyArray, +): ReadonlyArray { + const entries = records.filter((record) => record.type !== "session"); + const leaf = entries.at(-1); + // Version 1 sessions predate the tree and are linear. + if (leaf?.id === undefined) return entries; + const byId = new Map( + entries.flatMap((entry) => (entry.id === undefined ? [] : [[entry.id, entry] as const])), + ); + const branch: Array = []; + for ( + let entry: DecodedTranscriptRecord | undefined = leaf; + entry !== undefined && branch.length < entries.length; + entry = entry.parentId ? byId.get(entry.parentId) : undefined + ) { + branch.push(entry); + } + return branch.toReversed(); +} + +function isPiConversationRecord( + record: DecodedTranscriptRecord, +): record is DecodedTranscriptRecord & { + readonly message: typeof TranscriptMessage.Type & { readonly role: "user" | "assistant" }; +} { + return ( + record.type === "message" && + (record.message?.role === "user" || record.message?.role === "assistant") + ); +} + export function parseAgentSessionTranscript( input: AgentSessionTranscriptMetadata & { readonly contents: string; @@ -311,8 +373,16 @@ function parseAgentSessionRecords( // Claude filenames are session IDs. Codex rollout filenames include extra // timestamp text, so only transcript metadata can provide a resumable ID. let providerSessionId = - input.source === "codex" ? "" : (claudeFields.sessionId ?? input.fallbackSessionId); - const title = claudeFields.title ?? null; + input.source === "codex" + ? "" + : input.source === "pi" + ? records.find((record) => record.type === "session")?.id?.trim() || input.fallbackSessionId + : (claudeFields.sessionId ?? input.fallbackSessionId); + // Pi titles a session by its latest session_info on any branch; an empty name clears it. + const title = + input.source === "pi" + ? records.findLast((record) => record.type === "session_info")?.name?.trim() || null + : (claudeFields.title ?? null); let model = claudeFields.model ?? null; let derivedTitle: string | undefined; let hasCodexSessionId = false; @@ -403,8 +473,20 @@ function parseAgentSessionRecords( }; let recordIndex = -1; - for (const record of records) { + for (const record of input.source === "pi" ? activePiBranch(records) : records) { recordIndex += 1; + if (input.source === "pi") { + if (!isPiConversationRecord(record)) continue; + const text = extractText(record.message.content); + if (text.length === 0) continue; + retainMessage({ + role: record.message.role, + text, + createdAt: normalizeTimestamp(record.timestamp, fallbackTimestamp), + codexResponseUser: false, + }); + continue; + } if (input.source === "claudeAgent") { if (isHiddenClaudeRecord(record)) continue; if (record.type !== "user" && record.type !== "assistant") continue; @@ -507,6 +589,8 @@ function shouldRetainDecodedRecord( source: AgentSessionSource, record: DecodedTranscriptRecord, ): boolean { + // Any Pi entry can be a link on the active branch. + if (source === "pi") return true; if (source === "claudeAgent") { if (isHiddenClaudeRecord(record) || (record.type !== "user" && record.type !== "assistant")) { return false; @@ -843,15 +927,29 @@ export const make = Effect.gen(function* () { reader.write(decoder.decode()); recordCount += 1; if (recordCount > recordLimit) return false; - const decoded = decodeTranscriptValue(reader.finish()); - if (Option.isSome(decoded)) { + const value = reader.finish(); + const decoded = decodeTranscriptValue(value); + if (Option.isNone(decoded) && source === "pi") { + // Pi accepts messages this schema rejects, such as null content; keep their tree links. + records.push(...Option.toArray(decodePiEntryLinks(value))); + } else if (Option.isSome(decoded)) { if (Option.isNone(cwd)) cwd = Option.fromNullOr(extractDecodedCwd(decoded.value)); if (source === "claudeAgent") { claudeFields = foldClaudeFields(claudeFields, decoded.value); } if (shouldRetainDecodedRecord(source, decoded.value)) { - records.push(decoded.value); - historyBytes += recordBytes; + if ( + source === "pi" && + decoded.value.type === "message" && + !isPiConversationRecord(decoded.value) + ) { + // Tool output can dwarf the conversation; keep only the tree links. + const { message: _message, ...links } = decoded.value; + records.push(links); + } else { + records.push(decoded.value); + historyBytes += recordBytes; + } } } recordBytes = 0; @@ -1030,6 +1128,59 @@ export const make = Effect.gen(function* () { }, ); + // Pi nests sessions in one directory per cwd, but a custom session directory holds them flat. + const discoverPiTranscripts = Effect.fn("AgentSessionScanner.discoverPiTranscripts")(function* ( + sessionsDir: string, + providerInstanceId: ProviderInstanceId, + operationBudget: number, + ) { + let operationsRemaining = operationBudget; + let truncated = false; + const readDirectory = (directory: string) => { + if (operationsRemaining <= 0) { + truncated = true; + return Effect.succeed>([]); + } + operationsRemaining -= 1; + return listDirectory(directory); + }; + const transcripts: Array = []; + for (const entry of yield* readDirectory(sessionsDir)) { + if (operationsRemaining <= 0) { + truncated = true; + break; + } + const entryPath = path.join(sessionsDir, entry); + const filePaths = entry.endsWith(".jsonl") + ? [entryPath] + : (yield* readDirectory(entryPath)) + .filter((nested) => nested.endsWith(".jsonl")) + .map((nested) => path.join(entryPath, nested)); + + for (const filePath of filePaths) { + if (operationsRemaining <= 0) { + truncated = true; + break; + } + operationsRemaining -= 1; + const stats = yield* statOption(filePath); + if ( + Option.isSome(stats) && + stats.value.type === "File" && + Option.isSome(stats.value.mtime) + ) { + transcripts.push({ + filePath, + mtimeMs: stats.value.mtime.value.getTime(), + providerInstanceId, + size: Number(stats.value.size), + }); + } + } + } + return { transcripts, truncated }; + }); + const groupTranscriptsByCwd = Effect.fn("AgentSessionScanner.groupTranscriptsByCwd")(function* ( source: AgentSessionSource, transcripts: ReadonlyArray, @@ -1081,7 +1232,7 @@ export const make = Effect.gen(function* () { const raw: Array = []; let truncated = false; - for (const source of ["claudeAgent", "codex"] as const) { + for (const source of ["claudeAgent", "codex", "pi"] as const) { const instances: Array<{ readonly instanceId: ProviderInstanceId; readonly config: ProviderInstanceConfig; @@ -1111,26 +1262,36 @@ export const make = Effect.gen(function* () { const homes: Array<{ homePath: string; providerInstanceId: ProviderInstanceId }> = []; const seenHomes = new Set(); for (const { instanceId, config: instance } of instances) { - const homeVariable = source === "claudeAgent" ? "CLAUDE_CONFIG_DIR" : "CODEX_HOME"; - const configuredHome = instance.environment?.findLast( - (variable) => variable.name === homeVariable, - )?.value; - const environmentHome = - typeof configuredHome === "string" ? configuredHome : hostEnvironment[homeVariable]; + const environmentHome = (name: "CLAUDE_CONFIG_DIR" | "CODEX_HOME") => { + const configured = instance.environment?.findLast( + (variable) => variable.name === name, + )?.value; + return typeof configured === "string" ? configured : hostEnvironment[name]; + }; let homePath: string; - if (source === "claudeAgent") { + if (source === "pi") { + const environment = yield* mergeProviderInstanceEnvironment( + instance.environment, + hostEnvironment, + ).pipe(Effect.provideService(HostProcess.HomeDirectory, homeDir)); + homePath = resolvePiSessionsDir(environment, homeDir, path); + } else if (source === "claudeAgent") { const config = decodeClaudeSettings(instance.config ?? {}); if (Option.isNone(config)) continue; - homePath = resolveClaudeConfigDir(config.value.homePath, environmentHome); + homePath = resolveClaudeConfigDir( + config.value.homePath, + environmentHome("CLAUDE_CONFIG_DIR"), + ); } else { const config = decodeCodexSettings(instance.config ?? {}); if (Option.isNone(config)) continue; + const codexHome = environmentHome("CODEX_HOME"); const codexSettings = config.value.homePath.trim().length === 0 && config.value.shadowHomePath.trim().length === 0 && - environmentHome?.trim() - ? { ...config.value, homePath: environmentHome } + codexHome?.trim() + ? { ...config.value, homePath: codexHome } : config.value; const layout = yield* resolveCodexHomeLayout(codexSettings).pipe( Effect.provideService(Path.Path, path), @@ -1155,9 +1316,13 @@ export const make = Effect.gen(function* () { truncated = true; continue; } - const discovered = yield* source === "claudeAgent" - ? discoverClaudeTranscripts(home.homePath, home.providerInstanceId, operationBudget) - : discoverCodexTranscripts(home.homePath, home.providerInstanceId, operationBudget); + const discover = + source === "claudeAgent" + ? discoverClaudeTranscripts + : source === "codex" + ? discoverCodexTranscripts + : discoverPiTranscripts; + const discovered = yield* discover(home.homePath, home.providerInstanceId, operationBudget); truncated ||= discovered.truncated; transcriptCandidates.push(...discovered.transcripts); } diff --git a/apps/server/src/project/ProjectMutation.test.ts b/apps/server/src/project/ProjectMutation.test.ts new file mode 100644 index 0000000000000..9e4e075f06ea0 --- /dev/null +++ b/apps/server/src/project/ProjectMutation.test.ts @@ -0,0 +1,78 @@ +import { describe, expect, it } from "@effect/vitest"; +import { CommandId, type Project, ProjectId } from "@t3tools/contracts"; +import * as Deferred from "effect/Deferred"; +import * as Effect from "effect/Effect"; +import * as Fiber from "effect/Fiber"; + +import { ProjectNotEmptyError } from "./ProjectService.ts"; +import { mutateProjectWithClone } from "./ProjectMutation.ts"; + +const projectId = ProjectId.make("project-1"); +const deletedProject: Project = { + id: projectId, + title: "kh-frontend", + workspaceRoot: "/workspace/kh-frontend", + repositoryIdentity: null, + faviconPath: null, + defaultModelSelection: null, + defaultThreadEnvMode: null, + autoPull: false, + projectIcon: null, + scripts: [], + createdAt: "2026-01-01T00:00:00.000Z", + updatedAt: "2026-01-01T00:00:00.000Z", + deletedAt: "2026-01-01T00:00:00.000Z", +}; + +describe("mutateProjectWithClone", () => { + it.effect("keeps the clone when deletion is rejected", () => + Effect.gen(function* () { + const rejected = new ProjectNotEmptyError({ projectId }); + const result = yield* mutateProjectWithClone( + { + create: () => Effect.die("unused"), + update: () => Effect.die("unused"), + delete: () => Effect.fail(rejected), + }, + { discard: () => Effect.die("must not discard a project that still exists") }, + { type: "project.delete", commandId: CommandId.make("rejected-delete"), projectId }, + ).pipe(Effect.flip); + + expect(result).toBe(rejected); + }), + ); + + it.effect("discards the clone record even when the caller disconnects mid-delete", () => + Effect.gen(function* () { + const committed = yield* Deferred.make(); + const finishDelete = yield* Deferred.make(); + const discarded: Array = []; + const projects = { + create: () => Effect.die("unused"), + update: () => Effect.die("unused"), + // The delete event is already durable while its post-commit cleanup runs. + delete: () => + Deferred.succeed(committed, undefined).pipe( + Effect.andThen(Deferred.await(finishDelete)), + Effect.as(deletedProject), + ), + }; + const clones = { + discard: (id: ProjectId) => Effect.sync(() => void discarded.push(id)), + }; + + const request = yield* mutateProjectWithClone(projects, clones, { + type: "project.delete", + commandId: CommandId.make("command-1"), + projectId, + }).pipe(Effect.forkChild); + yield* Deferred.await(committed); + // Delivered synchronously so it lands before the delete can finish. + request.interruptUnsafe(); + yield* Deferred.succeed(finishDelete, undefined); + yield* Fiber.await(request); + + expect(discarded).toEqual([projectId]); + }), + ); +}); diff --git a/apps/server/src/project/ProjectMutation.ts b/apps/server/src/project/ProjectMutation.ts index 6f5747ee7f5e6..8cf70f302b8f2 100644 --- a/apps/server/src/project/ProjectMutation.ts +++ b/apps/server/src/project/ProjectMutation.ts @@ -1,6 +1,7 @@ import { type ProjectMutation } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; +import { type ProjectCloneTracker } from "./ProjectCloneTracker.ts"; import { type ProjectService } from "./ProjectService.ts"; type ProjectMutations = Pick; @@ -51,3 +52,17 @@ export const projectMutationOperation = Effect.fn("projectMutationOperation")(fu }); } }); + +// A disconnect after deletion commits must not leave the in-memory clone record behind. +export const mutateProjectWithClone = Effect.fn("mutateProjectWithClone")(function* ( + projects: ProjectMutations, + clones: Pick, + mutation: ProjectMutation, +) { + if (mutation.type !== "project.delete") + return yield* projectMutationOperation(projects, mutation); + return yield* projectMutationOperation(projects, mutation).pipe( + Effect.tap(() => clones.discard(mutation.projectId)), + Effect.uninterruptible, + ); +}); diff --git a/apps/server/src/project/ProjectSetupScriptRunner.test.ts b/apps/server/src/project/ProjectSetupScriptRunner.test.ts index 29a15a6f0715b..f383436cdb439 100644 --- a/apps/server/src/project/ProjectSetupScriptRunner.test.ts +++ b/apps/server/src/project/ProjectSetupScriptRunner.test.ts @@ -1,7 +1,11 @@ import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; import { describe, expect, it } from "@effect/vitest"; -import { ProjectId, type TerminalEvent, type TerminalSessionSnapshot } from "@t3tools/contracts"; -import * as HostProcess from "@t3tools/shared/HostProcess"; +import { + ProjectId, + TerminalNotRunningError, + type TerminalEvent, + type TerminalSessionSnapshot, +} from "@t3tools/contracts"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; import * as Fiber from "effect/Fiber"; @@ -36,6 +40,7 @@ const openSnapshot = (history: string): TerminalSessionSnapshot => ({ const makeHarness = Effect.fn(function* (options: { readonly history?: string; readonly opening?: Deferred.Deferred; + readonly shell?: string | null; }) { const listeners = new Set<(event: TerminalEvent) => Effect.Effect>(); const writes = yield* Queue.unbounded(); @@ -46,6 +51,10 @@ const makeHarness = Effect.fn(function* (options: { ? Deferred.succeed(options.opening, undefined).pipe(Effect.andThen(Effect.never)) : Effect.void ).pipe(Effect.as(openSnapshot(options.history ?? ""))), + getShell: () => + options.shell === null + ? Effect.fail(new TerminalNotRunningError({ threadId, terminalId })) + : Effect.succeed(options.shell ?? "/bin/zsh"), subscribe: (listener) => Effect.sync(() => { listeners.add(listener); @@ -66,8 +75,6 @@ const makeHarness = Effect.fn(function* (options: { NodeCrypto.layer, ), ), - Effect.provideService(HostProcess.Platform, "darwin"), - Effect.provideService(HostProcess.Environment, { SHELL: "/bin/zsh" }), ); const start = runner.runForThread({ threadId, @@ -92,7 +99,12 @@ const makeHarness = Effect.fn(function* (options: { return { start, listeners, writes, outputLines, emit }; }); -const setup = Effect.fn(function* (options: { readonly history?: string } = {}) { +const setup = Effect.fn(function* ( + options: { + readonly history?: string; + readonly shell?: string | null; + } = {}, +) { const harness = yield* makeHarness(options); const result = yield* harness.start; const completion = result.status === "started" ? result.completion : undefined; @@ -101,6 +113,15 @@ const setup = Effect.fn(function* (options: { readonly history?: string } = {}) }); describe("ProjectSetupScriptRunner", () => { + it.effect("writes immediately when the spawned fallback is PowerShell", () => + Effect.gen(function* () { + const { writes } = yield* setup({ + shell: "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe", + }); + expect(yield* Queue.take(writes)).toContain("$global:LASTEXITCODE"); + }), + ); + it.effect("shows a startup prompt and types only once the line editor starts", () => Effect.gen(function* () { const { result, writes, outputLines, emit } = yield* setup(); @@ -160,13 +181,20 @@ describe("ProjectSetupScriptRunner", () => { }), ); - it.effect("does not type into a shell that exits during startup", () => - Effect.gen(function* () { - const { completion, writes, emit } = yield* setup(); - yield* emit({ ...event, type: "exited", exitCode: 1, exitSignal: null }); - expect(yield* completion).toMatchObject({ exitCode: null }); - yield* TestClock.adjust("1 minute"); - expect(yield* Queue.size(writes)).toBe(0); - }), + it.effect.each([false, true])( + "does not type into a shell that exits during startup (before shell lookup: %s)", + (beforeShellLookup) => + Effect.gen(function* () { + const { completion, writes, emit, listeners } = yield* setup( + beforeShellLookup ? { shell: null } : {}, + ); + if (!beforeShellLookup) { + yield* emit({ ...event, type: "exited", exitCode: 1, exitSignal: null }); + } + expect(yield* completion).toMatchObject({ exitCode: null }); + yield* TestClock.adjust("1 minute"); + expect(yield* Queue.size(writes)).toBe(0); + expect(listeners.size).toBe(0); + }), ); }); diff --git a/apps/server/src/project/ProjectSetupScriptRunner.ts b/apps/server/src/project/ProjectSetupScriptRunner.ts index 095f20938c8b0..ec11ce9840448 100644 --- a/apps/server/src/project/ProjectSetupScriptRunner.ts +++ b/apps/server/src/project/ProjectSetupScriptRunner.ts @@ -1,5 +1,4 @@ import { linkOriginIsFenced, ProjectId, type ProjectScript, ThreadId } from "@t3tools/contracts"; -import * as HostProcess from "@t3tools/shared/HostProcess"; import { projectScriptRuntimeEnv, projectScriptTerminalInput, @@ -107,9 +106,19 @@ export class ProjectSetupScriptProjectNotFoundError extends Schema.TaggedError

()( + "ProjectSetupScriptUnsupportedShellError", + { shell: Schema.String }, +) { + override get message(): string { + return "Project script completion is not supported in cmd.exe. Install PowerShell and retry."; + } +} + export const ProjectSetupScriptRunnerError = Schema.Union([ ProjectSetupScriptOperationError, ProjectSetupScriptProjectNotFoundError, + ProjectSetupScriptUnsupportedShellError, ]); export type ProjectSetupScriptRunnerError = typeof ProjectSetupScriptRunnerError.Type; @@ -159,13 +168,9 @@ function lineEditorReading(text: string, previous: boolean): boolean { type CompletionShell = "posix" | "fish" | "powershell"; -function resolveCompletionShell( - platform: NodeJS.Platform, - env: NodeJS.ProcessEnv, -): CompletionShell { - if (platform === "win32") return "powershell"; - const shell = env.SHELL ?? ""; - const name = shell.split("/").at(-1) ?? shell; +function resolveCompletionShell(shell: string): CompletionShell | null { + const name = (shell.split(/[/\\]/).at(-1) ?? shell).toLowerCase().replace(/\.exe$/, ""); + if (name === "cmd") return null; if (name === "fish") return "fish"; if (name === "pwsh" || name === "powershell") return "powershell"; return "posix"; @@ -196,10 +201,6 @@ export const make = Effect.gen(function* () { const serverSettings = yield* ServerSettings.ServerSettingsService; const crypto = yield* Crypto.Crypto; const serviceScope = yield* Effect.scope; - const completionShell = resolveCompletionShell( - yield* HostProcess.Platform, - yield* HostProcess.Environment, - ); const observeTerminalCompletion = Effect.fn("ProjectSetupScriptRunner.observeTerminalCompletion")( function* (input: { @@ -444,15 +445,7 @@ export const make = Effect.gen(function* () { const completionToken = observe ? (yield* crypto.randomUUIDv4().pipe(Effect.orDie)).replaceAll("-", "") : null; - const commandLine = - observe && completionToken - ? wrapCommandForCompletion( - script.command, - completionShell, - completionSentinel(completionToken), - ) - : script.command; - + const echoedWrapperLines: string[] = []; // Subscribe before opening so neither startup activity nor the sentinel can race the listeners. const observed = observe && completionToken @@ -461,49 +454,76 @@ export const make = Effect.gen(function* () { terminalId, sentinel: completionSentinel(completionToken), sentinelPattern: completionSentinelPattern(completionToken), - echoedWrapperLines: commandLine.split("\r").filter((line) => line.length > 0), + echoedWrapperLines, onOutputLine: observe.onOutputLine, }) : undefined; - // PowerShell has no line-editor signal; keep its immediate write. - const readiness = - completionShell === "powershell" - ? undefined - : yield* watchShellReadiness({ - threadId: input.threadId, - terminalId, - onStartupPrompt: observed?.flushPartialLine ?? Effect.void, - }); - - yield* terminalManager - .open({ - threadId: input.threadId, - terminalId, - cwd, - worktreePath: input.worktreePath, - env: { ...env, NO_COLOR: "1", FORCE_COLOR: "0" }, - }) - .pipe( - Effect.mapError( - (cause) => - new ProjectSetupScriptOperationError({ - ...errorContext, - operation: "openTerminal", - cause, - }), - ), - Effect.onError(() => - Effect.sync(() => { - observed?.unsubscribe(); - readiness?.unsubscribe(); - }), - ), - ); + const readiness = yield* watchShellReadiness({ + threadId: input.threadId, + terminalId, + onStartupPrompt: observed?.flushPartialLine ?? Effect.void, + }); + const { completionShell, commandLine } = yield* Effect.gen(function* () { + const shell = yield* terminalManager + .open({ + threadId: input.threadId, + terminalId, + cwd, + worktreePath: input.worktreePath, + env: { ...env, NO_COLOR: "1", FORCE_COLOR: "0" }, + }) + .pipe( + Effect.andThen(() => + terminalManager + .getShell({ threadId: input.threadId, terminalId }) + .pipe(Effect.catchTags({ TerminalNotRunningError: () => Effect.succeed(null) })), + ), + Effect.mapError( + (cause) => + new ProjectSetupScriptOperationError({ + ...errorContext, + operation: "openTerminal", + cause, + }), + ), + ); + if (shell === null) { + yield* observed?.abandon ?? Effect.void; + return { completionShell: null, commandLine: null }; + } + const completionShell = resolveCompletionShell(shell); + let commandLine = script.command; + if (observe && completionToken) { + if (completionShell === null) { + yield* terminalManager.closeIdle({ threadId: input.threadId, terminalId }); + return yield* new ProjectSetupScriptUnsupportedShellError({ shell }); + } + commandLine = wrapCommandForCompletion( + script.command, + completionShell, + completionSentinel(completionToken), + ); + } + echoedWrapperLines.push(...commandLine.split("\r").filter((line) => line.length > 0)); + return { completionShell, commandLine }; + }).pipe( + Effect.onError(() => + Effect.sync(() => { + observed?.unsubscribe(); + readiness.unsubscribe(); + }), + ), + ); // Return once the terminal exists so callers can show it while a startup prompt waits. - yield* (readiness?.wait ?? Effect.succeed("ready" as const)).pipe( + // PowerShell and cmd have no line-editor signal. + yield* ( + completionShell === "posix" || completionShell === "fish" + ? readiness.wait + : Effect.succeed("ready" as const) + ).pipe( Effect.flatMap((shell) => - shell === "exited" + shell === "exited" || commandLine === null ? Effect.void : terminalManager.write({ threadId: input.threadId, @@ -518,7 +538,7 @@ export const make = Effect.gen(function* () { cause, }).pipe(Effect.andThen(observed?.abandon ?? Effect.void)), ), - Effect.ensuring(Effect.sync(() => readiness?.unsubscribe())), + Effect.ensuring(Effect.sync(() => readiness.unsubscribe())), Effect.forkIn(serviceScope), ); diff --git a/apps/server/src/provider/ClaudeProvider.ts b/apps/server/src/provider/ClaudeProvider.ts index 264a0d8eaa000..0eaa641a72cc9 100644 --- a/apps/server/src/provider/ClaudeProvider.ts +++ b/apps/server/src/provider/ClaudeProvider.ts @@ -38,7 +38,7 @@ import { type ServerProviderDraft, } from "@t3tools/provider-core/server/snapshotProbe"; import { hasClaudeManagedMcpConfig, makeClaudeEnvironment } from "./Drivers/ClaudeHome.ts"; -import { discoverClaudeSkills } from "./Drivers/ClaudeSkills.ts"; +import { discoverClaudeSkills, readClaudeSettingsBaseUrl } from "./Drivers/ClaudeSkills.ts"; import type { ProviderWorkspaceSnapshot } from "@t3tools/provider-core/server/driver"; import { makeUnavailableUsageLimits } from "@t3tools/provider-core/server/usageLimits"; import { @@ -82,11 +82,10 @@ function claudeSubscriptionLabel(subscriptionType: string | undefined): string | switch (normalized) { case "claudemaxsubscription": - return "Max"; case "claudemax5xsubscription": - return "Max 5x"; + return "Max"; case "claudemax20xsubscription": - return "Max 20x"; + return "Ultra"; case "claudeenterprisesubscription": return "Enterprise"; case "claudeteamsubscription": @@ -97,11 +96,10 @@ function claudeSubscriptionLabel(subscriptionType: string | undefined): string | return "Free"; case "max": case "maxplan": - return "Max"; case "max5": - return "Max 5x"; + return "Max"; case "max20": - return "Max 20x"; + return "Ultra"; case "enterprise": return "Enterprise"; case "team": @@ -650,7 +648,10 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")( } // First-party account fields cannot verify custom endpoint credentials. - const endpoint = URL.parse(resolvedEnvironment.ANTHROPIC_BASE_URL?.trim() ?? ""); + const baseUrl = + (yield* readClaudeSettingsBaseUrl(claudeSettings, cwd, resolvedEnvironment)) ?? + resolvedEnvironment.ANTHROPIC_BASE_URL; + const endpoint = URL.parse(baseUrl?.trim() ?? ""); const customApiEndpoint = endpoint !== null && (endpoint.protocol === "http:" || endpoint.protocol === "https:") && @@ -662,6 +663,7 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")( enabled: claudeSettings.enabled, checkedAt, models, + updateRequiredModels, slashCommands: dedupedSlashCommands, skills, probe: { @@ -676,13 +678,14 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")( }); } + // A proxy's ANTHROPIC_AUTH_TOKEN reads as an API key; the endpoint is what the token is for. const authMetadata = + apiProviderAuthMetadata(capabilities.apiProvider) ?? + (customApiEndpoint ? { type: "customEndpoint", label: "Custom API endpoint" } : undefined) ?? claudeAuthMetadata({ subscriptionType: capabilities.subscriptionType, authMethod: capabilities.tokenSource, - }) ?? - apiProviderAuthMetadata(capabilities.apiProvider) ?? - (customApiEndpoint ? { type: "customEndpoint", label: "Custom API endpoint" } : undefined); + }); const usageCheckedAt = capabilities.usageCheckedAt ?? checkedAt; const usageLimits = !capabilities.usage ? makeUnavailableUsageLimits({ checkedAt, reason: "probeFailed" }) diff --git a/apps/server/src/provider/CodexMcpElicitation.ts b/apps/server/src/provider/CodexMcpElicitation.ts index 7703b02f03b94..eafe09e07e706 100644 --- a/apps/server/src/provider/CodexMcpElicitation.ts +++ b/apps/server/src/provider/CodexMcpElicitation.ts @@ -76,7 +76,14 @@ function mcpElicitationPersistenceDecision( return null; } -function mcpElicitationFormFields(payload: EffectCodexSchema.McpServerElicitationRequestParams) { +// Claude hands over the same MCP request with its own envelope; these are the +// only fields the response is built from. +type McpElicitationResponseInput = { + readonly mode: string; + readonly requestedSchema?: unknown; +}; + +function mcpElicitationFormFields(payload: McpElicitationResponseInput) { if (payload.mode === "url" || !isMcpElicitationForm(payload.requestedSchema)) { return undefined; } @@ -171,7 +178,7 @@ export function describeMcpElicitation( } export function toMcpElicitationResponse( - payload: EffectCodexSchema.McpServerElicitationRequestParams, + payload: McpElicitationResponseInput, decision: ProviderApprovalDecision, ): EffectCodexSchema.McpServerElicitationRequestResponse { if (decision === "decline" || decision === "cancel") { @@ -196,8 +203,9 @@ export function toMcpElicitationResponse( const chosenOption = options.find((option) => persist ? mcpElicitationPersistenceDecision(option.value) === decision - : /once|accept|approve|allow/i.test(option.value) && - mcpElicitationPersistenceDecision(option.value) === null, + : /^(?:once|accept|approve|allow)(?:[_\s-]*(?:once(?:[_\s-]*only)?|this[_\s-]*time))?$/i.test( + option.value.trim(), + ) && mcpElicitationPersistenceDecision(option.value) === null, ); if (chosenOption) { content[key] = chosenOption.value; diff --git a/apps/server/src/provider/CodexProvider.ts b/apps/server/src/provider/CodexProvider.ts index a170a6c8e695e..28a4a649d53bd 100644 --- a/apps/server/src/provider/CodexProvider.ts +++ b/apps/server/src/provider/CodexProvider.ts @@ -45,7 +45,9 @@ import { SAFE_RUNTIME_MODES } from "./runtimeModeCapabilities.ts"; import { buildServerProvider, COMPACT_SLASH_COMMAND, + DEFAULT_TIMEOUT_MS, providerModelsFromSettings, + spawnAndCollect, type ServerProviderDraft, } from "@t3tools/provider-core/server/snapshotProbe"; import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion"; @@ -81,6 +83,9 @@ const CODEX_CLIENT_NAME = "T3 Code"; // Handshake plus account/models/skills requests can exceed the shared 10s probe budget. const CODEX_AUTH_PROBE_TIMEOUT_MS = 20_000; const CODEX_DISCOVERY_MARGIN_MS = 250; +// `codex-cli 0.159.0`, `codex-cli 0.160.0-alpha.1` or the npm CLI's bare `0.1.4161006`. +// Keep a prerelease suffix so compatibility stays unknown for it, as it does for `initialize`. +const CODEX_CLI_VERSION_PATTERN = /(?:^|\s)v?(\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?)(?=\s|$)/u; const CODEX_PRESENTATION = { displayName: "Codex", @@ -469,22 +474,29 @@ export function buildCodexInitializeParams(): CodexSchema.V1InitializeParams { }; } -export const withCodexAppServerClient = Effect.fn("withCodexAppServerClient")(function* (input: { - readonly binaryPath: string; +const codexProcessEnvironment = Effect.fn("codexProcessEnvironment")(function* (input: { readonly homePath?: string | undefined; - readonly launchArgs?: string | undefined; - readonly cwd: string; readonly environment?: NodeJS.ProcessEnv | undefined; }) { // Expand CODEX_HOME explicitly: spawn passes a literal ~ that Codex rejects. const resolvedHomePath = input.homePath ? expandHomePath(input.homePath, yield* HostProcess.HomeDirectory) : undefined; - const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; - const environment = { + return { ...codexAppServerEnvironment(input.environment), ...(resolvedHomePath ? { CODEX_HOME: resolvedHomePath } : {}), }; +}); + +export const withCodexAppServerClient = Effect.fn("withCodexAppServerClient")(function* (input: { + readonly binaryPath: string; + readonly homePath?: string | undefined; + readonly launchArgs?: string | undefined; + readonly cwd: string; + readonly environment?: NodeJS.ProcessEnv | undefined; +}) { + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; + const environment = yield* codexProcessEnvironment(input); const spawnCommand = yield* resolveSpawnCommand( input.binaryPath, codexAppServerArgs(input.launchArgs), @@ -613,6 +625,39 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun } satisfies CodexAppServerProviderSnapshot; }); +// Old CLIs can exit before initialize reports the version needed for compatibility advice. +const probeCodexCliVersion = Effect.fn("probeCodexCliVersion")( + function* (input: { + readonly binaryPath: string; + readonly homePath?: string | undefined; + readonly cwd: string; + readonly environment?: NodeJS.ProcessEnv | undefined; + }) { + const environment = yield* codexProcessEnvironment(input); + const spawnCommand = yield* resolveSpawnCommand(input.binaryPath, ["--version"], { + env: environment, + extendEnv: true, + }); + const result = yield* spawnAndCollect( + input.binaryPath, + ChildProcess.make(spawnCommand.command, spawnCommand.args, { + cwd: input.cwd, + env: environment, + extendEnv: true, + forceKillAfter: CODEX_APP_SERVER_PROBE_FORCE_KILL_AFTER, + shell: spawnCommand.shell, + }), + ); + if (result.code !== 0) return null; + return CODEX_CLI_VERSION_PATTERN.exec(result.stdout)?.[1] ?? null; + }, + Effect.timeoutOption(Duration.millis(DEFAULT_TIMEOUT_MS)), + Effect.map(Option.getOrNull), + Effect.catch((cause) => + Effect.logDebug("Codex --version fallback failed.", { cause }).pipe(Effect.as(null)), + ), +); + export const probeCodexSkillsForCwd = Effect.fn("probeCodexSkillsForCwd")(function* (input: { readonly binaryPath: string; readonly homePath?: string; @@ -758,6 +803,14 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu if (Result.isFailure(probeResult)) { const error = probeResult.failure; const installed = !isCodexAppServerSpawnError(error); + const version = installed + ? yield* probeCodexCliVersion({ + binaryPath: codexSettings.binaryPath, + homePath: codexSettings.homePath, + cwd: process.cwd(), + environment: resolvedEnvironment, + }) + : null; return buildServerProvider({ presentation: CODEX_PRESENTATION, enabled: codexSettings.enabled, @@ -766,7 +819,7 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu skills: [], probe: { installed, - version: null, + version, status: "error", auth: { status: "unknown" }, message: installed diff --git a/apps/server/src/provider/Drivers/ClaudeAgentSdk.ts b/apps/server/src/provider/Drivers/ClaudeAgentSdk.ts index 149f45b6827bb..7da3a03f414c2 100644 --- a/apps/server/src/provider/Drivers/ClaudeAgentSdk.ts +++ b/apps/server/src/provider/Drivers/ClaudeAgentSdk.ts @@ -37,6 +37,7 @@ const SUPPORTED_QUERY_OPTIONS: ReadonlySet = new Set([ "includePartialMessages", "mcpServers", "model", + "onElicitation", "onUserDialog", "pathToClaudeCodeExecutable", "permissionMode", @@ -113,6 +114,7 @@ export function toClaudeCliOptions(options: Sdk.Options): Cli.Options { } : {}), ...(options.model ? { model: options.model } : {}), + ...(options.onElicitation ? { onElicitation: options.onElicitation } : {}), ...(options.onUserDialog ? { onUserDialog: options.onUserDialog } : {}), ...(options.permissionMode ? { permissionMode: options.permissionMode } : {}), ...(options.persistSession === false ? { persistSession: false } : {}), diff --git a/apps/server/src/provider/Drivers/ClaudeCli.test.ts b/apps/server/src/provider/Drivers/ClaudeCli.test.ts index 6fdc3b2a3e1bb..7ad89ed4cd859 100644 --- a/apps/server/src/provider/Drivers/ClaudeCli.test.ts +++ b/apps/server/src/provider/Drivers/ClaudeCli.test.ts @@ -772,3 +772,96 @@ createInterface({ input: process.stdin }).on("line", (line) => { await NodeFS.rm(directory, { force: true, recursive: true }); } }, 5000); + +it("hands an MCP elicitation to onElicitation and answers it, as the Agent SDK does", async () => { + const directory = await NodeFS.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-claude-elicitation-")); + const executable = NodePath.join(directory, "fake-claude"); + // Asks once with the SDK 0.3.276 elicitation wire shape, then reports the answer as a result. + await NodeFS.writeFile( + executable, + `#!/usr/bin/env node +const { createInterface } = require("node:readline"); +createInterface({ input: process.stdin }).on("line", (line) => { + const value = JSON.parse(line); + if (value.type === "control_request") { + process.stdout.write(JSON.stringify({ + type: "control_response", + response: { subtype: "success", request_id: value.request_id, response: {} }, + }) + "\\n"); + return; + } + if (value.type === "user") { + process.stdout.write(JSON.stringify({ + type: "control_request", + request_id: "elicit-1", + request: { + subtype: "elicitation", + mcp_server_name: "deploys", + message: "Deploy web?", + mode: "form", + requested_schema: { type: "object", properties: {} }, + display_name: "Deploys", + }, + }) + "\\n"); + return; + } + if (value.type === "control_response") { + process.stdout.write(JSON.stringify({ type: "result", subtype: "success", session_id: "s", is_error: false, answer: value.response }) + "\\n", () => process.exit(0)); + } +}); +`, + { mode: 0o700 }, + ); + const asked: Array = []; + const answers: Array = []; + let resolveAnswered: (() => void) | undefined; + const answered = new Promise((resolve) => { + resolveAnswered = resolve; + }); + try { + const runtime = query({ + prompt: (async function* (): AsyncGenerator { + yield { + type: "user", + session_id: "", + parent_tool_use_id: null, + message: { role: "user", content: "hello" }, + }; + // Keep input open until the CLI has reported the answer. + await answered; + })(), + options: { + pathToClaudeCodeExecutable: executable, + env: process.env, + onElicitation: async (request, options) => { + asked.push(request); + assert.equal(options.requestId, "elicit-1"); + return { action: "accept", content: {} }; + }, + }, + }); + for await (const message of runtime) { + if (message.type !== "result") continue; + answers.push((message as { answer?: unknown }).answer); + resolveAnswered?.(); + } + assert.deepEqual(asked, [ + { + serverName: "deploys", + message: "Deploy web?", + mode: "form", + requestedSchema: { type: "object", properties: {} }, + displayName: "Deploys", + }, + ]); + assert.deepEqual(answers, [ + { + subtype: "success", + request_id: "elicit-1", + response: { action: "accept", content: {} }, + }, + ]); + } finally { + await NodeFS.rm(directory, { force: true, recursive: true }); + } +}, 5000); diff --git a/apps/server/src/provider/Drivers/ClaudeCli.ts b/apps/server/src/provider/Drivers/ClaudeCli.ts index 338c7b7ffbf0b..4559e23570bb8 100644 --- a/apps/server/src/provider/Drivers/ClaudeCli.ts +++ b/apps/server/src/provider/Drivers/ClaudeCli.ts @@ -64,6 +64,7 @@ export type Options = { /** External MCP server configurations, passed as `--mcp-config` as the SDK does. */ readonly mcpServers?: Readonly>>>; readonly strictMcpConfig?: boolean; + readonly onElicitation?: NonNullable; readonly onUserDialog?: NonNullable; readonly pathToClaudeCodeExecutable: string; /** Arguments before the CLI's own, as the SDK passes them to a native executable. */ @@ -122,6 +123,12 @@ type ControlRequest = { readonly agent_id?: string; readonly dialog_kind?: string; readonly payload?: Record; + readonly mcp_server_name?: string; + readonly message?: string; + readonly mode?: "form" | "url"; + readonly url?: string; + readonly elicitation_id?: string; + readonly requested_schema?: Record; }; }; @@ -217,6 +224,12 @@ const ControlRequestSchema = Schema.Struct({ agent_id: Schema.optional(Schema.String), dialog_kind: Schema.optional(Schema.String), payload: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)), + mcp_server_name: Schema.optional(Schema.String), + message: Schema.optional(Schema.String), + mode: Schema.optional(Schema.Literals(["form", "url"])), + url: Schema.optional(Schema.String), + elicitation_id: Schema.optional(Schema.String), + requested_schema: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)), }), }); @@ -722,6 +735,40 @@ class ClaudeCliQuery implements Query { }); return; } + if (message.request.subtype === "elicitation") { + // As the SDK does: without a handler the elicitation is declined, and a null answer + // sends no response. + const request = message.request; + if (request.mcp_server_name === undefined || request.message === undefined) { + throw new Error("Claude Code sent an invalid elicitation request."); + } + const result = this.options.onElicitation + ? await this.options.onElicitation( + { + serverName: request.mcp_server_name, + message: request.message, + ...(request.mode ? { mode: request.mode } : {}), + ...(request.url ? { url: request.url } : {}), + ...(request.elicitation_id ? { elicitationId: request.elicitation_id } : {}), + ...(request.requested_schema ? { requestedSchema: request.requested_schema } : {}), + ...(request.title ? { title: request.title } : {}), + ...(request.display_name ? { displayName: request.display_name } : {}), + ...(request.description ? { description: request.description } : {}), + }, + { signal: callbackController.signal, requestId: message.request_id }, + ) + : { action: "decline" as const }; + if (result === null) return; + await this.write({ + type: "control_response", + response: { + subtype: "success", + request_id: message.request_id, + response: result as Record, + }, + }); + return; + } if (message.request.subtype !== "can_use_tool" || !this.options.canUseTool) { throw new Error(`Unsupported Claude Code control request: ${message.request.subtype}`); } diff --git a/apps/server/src/provider/Drivers/ClaudeSkills.ts b/apps/server/src/provider/Drivers/ClaudeSkills.ts index f5b3b9abe3756..f0bc05eaeaa91 100644 --- a/apps/server/src/provider/Drivers/ClaudeSkills.ts +++ b/apps/server/src/provider/Drivers/ClaudeSkills.ts @@ -119,8 +119,9 @@ function claudeManagedSettingsPath( return "/etc/claude-code/managed-settings.json"; } +/** Claude Code settings files, lowest precedence first. */ // Coder: exported for ClaudeAdapterV2, which reads bypass availability from the same settings files. -export function skillOverrideSettingsPaths( +export function claudeSettingsPaths( path: Path.Path, configDirPath: string, cwd: string | undefined, @@ -248,7 +249,7 @@ const readSkillSettings = Effect.fn("readSkillSettings")(function* ( const enabledPlugins = new Map(); const repositoryRoot = cwd === undefined ? undefined : yield* findRepositoryRoot(cwd); - for (const settingsPath of skillOverrideSettingsPaths( + for (const settingsPath of claudeSettingsPaths( path, configDirPath, cwd, @@ -428,6 +429,43 @@ const resolveClaudeConfigDirPath = Effect.fn("resolveClaudeConfigDirPath")(funct return path.join(home, ".claude"); }); +const SettingsBaseUrl = fromLenientJson( + Schema.Struct({ + env: Schema.optional(Schema.Struct({ ANTHROPIC_BASE_URL: Schema.optional(Schema.String) })), + }), +); +const decodeSettingsBaseUrl = Schema.decodeUnknownEffect(SettingsBaseUrl); + +/** ANTHROPIC_BASE_URL from Claude's settings `env`, which Claude Code applies over the process env. */ +export const readClaudeSettingsBaseUrl = Effect.fn("readClaudeSettingsBaseUrl")(function* ( + config: Pick, + cwd: string | undefined, + environment: NodeJS.ProcessEnv, +): Effect.fn.Return { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const platform = yield* HostProcess.Platform; + const configDirPath = yield* resolveClaudeConfigDirPath(config, environment, cwd); + let baseUrl: string | undefined; + for (const settingsPath of claudeSettingsPaths( + path, + configDirPath, + undefined, + platform, + environment, + )) { + const contents = yield* fileSystem + .readFileString(settingsPath) + .pipe(Effect.orElseSucceed(() => undefined)); + if (contents === undefined) continue; + const parsed = yield* decodeSettingsBaseUrl(contents).pipe( + Effect.orElseSucceed(() => undefined), + ); + baseUrl = parsed?.env?.ANTHROPIC_BASE_URL ?? baseUrl; + } + return baseUrl; +}); + // Claude's --add-dir is repeatable and variadic; parseCliArgs keeps only one value. export function claudeLaunchArgDirectories(launchArgs?: string): ReadonlyArray { const directories: string[] = []; diff --git a/apps/server/src/provider/Drivers/CodexDriver.test.ts b/apps/server/src/provider/Drivers/CodexDriver.test.ts index e067d1a31f696..2f9cffbc757fa 100644 --- a/apps/server/src/provider/Drivers/CodexDriver.test.ts +++ b/apps/server/src/provider/Drivers/CodexDriver.test.ts @@ -405,7 +405,9 @@ it.layer(layerTest)("CodexDriver", (it) => { homePath: NodePath.join(tempDir, "codex-home"), }, }).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, metadataSpawner)); - const capabilities = yield* instance.snapshot.resolveMaintenance(); + const capabilities = yield* instance.snapshot + .resolveMaintenance() + .pipe(Effect.provideService(HostProcess.Environment, { PATH: "" })); const latestVersion = yield* resolveLatestProviderVersion(capabilities).pipe( Effect.provideServiceEffect( ProviderLatestVersions.ProviderLatestVersions, diff --git a/apps/server/src/provider/Drivers/CodexDriver.ts b/apps/server/src/provider/Drivers/CodexDriver.ts index 485d7e98a23e7..64dc57ec06400 100644 --- a/apps/server/src/provider/Drivers/CodexDriver.ts +++ b/apps/server/src/provider/Drivers/CodexDriver.ts @@ -173,6 +173,8 @@ export const CodexDriver: ProviderDriver snapshot.applyUsageLimits(update) }, ).pipe( Effect.mapError( diff --git a/apps/server/src/provider/ProviderInstanceRegistry.test.ts b/apps/server/src/provider/ProviderInstanceRegistry.test.ts index 7a5b5272e89c2..36dcdf46948b0 100644 --- a/apps/server/src/provider/ProviderInstanceRegistry.test.ts +++ b/apps/server/src/provider/ProviderInstanceRegistry.test.ts @@ -10,6 +10,7 @@ import { type ResolvedProviderInstanceEnvironment, ProviderInstanceId, SecretProviderId, + SettingsSecretValue, type SecretReference, } from "@t3tools/contracts"; import * as ProviderLatestVersions from "@t3tools/provider-core/server/ProviderLatestVersions"; @@ -17,6 +18,9 @@ import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderS import * as HostProcess from "@t3tools/shared/HostProcess"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; +import * as Exit from "effect/Exit"; +import * as Scope from "effect/Scope"; +import * as Fiber from "effect/Fiber"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Path from "effect/Path"; @@ -33,6 +37,7 @@ import { CodexDriver, type CodexDriverEnv } from "./Drivers/CodexDriver.ts"; import * as ModelManifest from "./ModelManifest.ts"; import * as ProviderEventLoggers from "@t3tools/provider-core/server/ProviderEventLoggers"; import { makeProviderInstanceRegistry } from "./ProviderInstanceRegistry.ts"; +import { ProviderDriverError } from "./Errors.ts"; import * as ProviderOrchestrationAdapterInfrastructure from "./ProviderOrchestrationAdapterInfrastructure.ts"; import * as ProviderHostLive from "./ProviderHostLive.ts"; // Coder: secret providers are not carried; the registry reads through the stand-in resolver. @@ -158,6 +163,136 @@ describe("ProviderInstanceRegistry — multi-instance codex slice", () => { Layer.provideMerge(layerBase), ); + it.effect.each(["replacement", "removal", "creation failure"] as const)( + "detaches retired provider scopes after repeated %s", + (scenario) => + Effect.gen(function* () { + const parent = yield* Scope.make(); + const fileSystem = yield* FileSystem.FileSystem; + const homePath = yield* fileSystem.makeTempDirectoryScoped(); + let creates = 0; + let closes = 0; + const driver: typeof CodexDriver = { + ...CodexDriver, + create: Effect.fn("ProviderInstanceRegistry.test.create")(function* ( + input: Parameters[0], + ) { + creates++; + yield* Effect.addFinalizer(() => + Effect.sync(() => { + closes++; + }), + ); + if (input.config.binaryPath === "creation-failure") { + return yield* new ProviderDriverError({ + driver: CodexDriver.driverKind, + instanceId: input.instanceId, + detail: "fixture creation failed", + }); + } + return yield* CodexDriver.create(input); + }), + }; + const siblingId = ProviderInstanceId.make("sibling"); + const retiredId = ProviderInstanceId.make("retired"); + const entry = { + driver: CodexDriver.driverKind, + enabled: false, + config: makeCodexConfig({ homePath }), + }; + yield* Effect.gen(function* () { + const { registry, mutator } = yield* makeProviderInstanceRegistry({ + drivers: [driver], + configMap: { [siblingId]: entry }, + }); + const sibling = yield* registry.getInstance(siblingId); + const finalizerCount = () => { + const state = parent.state; + return state._tag === "Open" ? (state.finalizers?.size ?? 1) : 0; + }; + const baseline = finalizerCount(); + for (let index = 0; index < 20; index++) { + yield* mutator.reconcile({ + [siblingId]: entry, + [retiredId]: { + ...entry, + config: makeCodexConfig({ + homePath, + binaryPath: scenario === "creation failure" ? "creation-failure" : "unused-codex", + customModels: [`revision-${index}`], + }), + }, + }); + if (scenario === "removal") yield* mutator.reconcile({ [siblingId]: entry }); + } + yield* mutator.reconcile({ [siblingId]: entry }); + expect(yield* registry.getInstance(siblingId)).toBe(sibling); + expect(closes).toBe(creates - 1); + expect(parent.state._tag).toBe("Open"); + expect(finalizerCount()).toBe(baseline); + }).pipe( + Effect.provideService(Scope.Scope, parent), + Effect.ensuring(Scope.close(parent, Exit.void)), + ); + expect(closes).toBe(creates); + }).pipe(Effect.provide(layerTest)), + ); + + it.effect.each(["replacement", "removal"] as const)( + "hides retired instances during reconcile (%s)", + (scenario) => + Effect.gen(function* () { + let closed = 0; + const replacementStarted = yield* Deferred.make(); + const releaseReplacement = yield* Deferred.make(); + const driver: typeof CodexDriver = { + ...CodexDriver, + create: (input) => + Effect.gen(function* () { + if (input.displayName === "Second") { + yield* Deferred.succeed(replacementStarted, undefined); + yield* Deferred.await(releaseReplacement); + } + const instance = yield* CodexDriver.create(input); + yield* Effect.addFinalizer(() => Effect.sync(() => closed++)); + return instance; + }), + }; + const retiredId = ProviderInstanceId.make("retired"); + const siblingId = ProviderInstanceId.make("sibling"); + const nextId = scenario === "replacement" ? retiredId : ProviderInstanceId.make("new"); + const entry = { + driver: CodexDriver.driverKind, + enabled: false, + config: makeCodexConfig({}), + }; + const { registry, mutator } = yield* makeProviderInstanceRegistry({ + drivers: [driver], + configMap: { [retiredId]: entry, [siblingId]: entry }, + }); + const sibling = yield* registry.getInstance(siblingId); + const reconcile = yield* mutator + .reconcile({ + [siblingId]: entry, + [nextId]: { + ...entry, + displayName: "Second", + config: makeCodexConfig({ customModels: ["replacement-model"] }), + }, + }) + .pipe(Effect.forkChild({ startImmediately: true })); + yield* Deferred.await(replacementStarted); + expect(closed).toBe(1); + expect(yield* registry.getInstance(retiredId)).toBeUndefined(); + expect(yield* registry.listInstances).toEqual([sibling]); + yield* Deferred.succeed(releaseReplacement, undefined); + yield* Fiber.join(reconcile); + expect((yield* registry.getInstance(nextId))?.displayName).toBe("Second"); + expect(yield* registry.getInstance(siblingId)).toBe(sibling); + expect(closed).toBe(1); + }).pipe(Effect.provide(layerTest)), + ); + it.live("treats an explicit in-config enabled:false as disabling despite the envelope", () => Effect.gen(function* () { const staleId = ProviderInstanceId.make("codex_stale"); @@ -504,11 +639,13 @@ describe("ProviderInstanceRegistry secret environment", () => { function setup( initial: Record, configSchema: AnyProviderDriver["configSchema"] = Schema.Unknown, + config: unknown = {}, ) { const vault = new Map(Object.entries(initial)); const reads: Array> = []; const invalidated: Array> = []; const created: Array = []; + const configs: Array = []; const driver: AnyProviderDriver = { driverKind, metadata: { displayName: "Fake" }, @@ -517,6 +654,7 @@ describe("ProviderInstanceRegistry secret environment", () => { create: (input) => Effect.sync(() => { created.push(input.environment); + configs.push(input.config); return { instanceId: input.instanceId, enabled: input.enabled } as ProviderInstance; }), }; @@ -539,7 +677,7 @@ describe("ProviderInstanceRegistry secret environment", () => { const configMap: ProviderInstanceConfigMap = { [instanceId]: { driver: driverKind, - config: {}, + config, environment: [ { name: "OPENAI_API_KEY", value: secret, sensitive: true }, { name: "BASE_URL", value: "https://example.test", sensitive: false }, @@ -557,7 +695,7 @@ describe("ProviderInstanceRegistry secret environment", () => { yield* PubSub.take(changes); return built; }); - return { vault, reads, invalidated, created, make, makeGated, gate }; + return { vault, reads, invalidated, created, configs, make, makeGated, gate }; } it.effect("starts an instance with the secret its environment points at", () => { @@ -623,6 +761,26 @@ describe("ProviderInstanceRegistry secret environment", () => { }).pipe(Effect.scoped); }); + it.effect("reads a config field typed SettingsSecretValue and rebuilds when it rotates", () => { + const apiKey: SecretReference = { ...secret, reference: "op://Private/Gateway/key" }; + const fixture = setup( + { [secret.reference]: "sk-live", [apiKey.reference]: "gw-live" }, + Schema.Struct({ apiKey: SettingsSecretValue, model: Schema.String }), + { apiKey, model: "m" }, + ); + return Effect.gen(function* () { + const { mutator } = yield* fixture.make; + expect(fixture.reads.at(-1)).toEqual([secret.reference, apiKey.reference]); + + fixture.vault.set(apiKey.reference, "gw-rotated"); + yield* mutator.reloadSecrets(); + expect(fixture.configs).toEqual([ + { apiKey: "gw-live", model: "m" }, + { apiKey: "gw-rotated", model: "m" }, + ]); + }).pipe(Effect.scoped); + }); + it.effect("keeps a working instance live when a reload cannot read its secret", () => { const fixture = setup({ [secret.reference]: "sk-live" }); return Effect.gen(function* () { diff --git a/apps/server/src/provider/ProviderInstanceRegistry.ts b/apps/server/src/provider/ProviderInstanceRegistry.ts index 0807186655ae2..91c9d86beca87 100644 --- a/apps/server/src/provider/ProviderInstanceRegistry.ts +++ b/apps/server/src/provider/ProviderInstanceRegistry.ts @@ -7,6 +7,7 @@ import { type ProviderDriverKind, type ResolvedProviderInstanceEnvironment, type SecretReference, + secretReferencesInSettings, type ServerProvider, } from "@t3tools/contracts"; import * as Context from "effect/Context"; @@ -43,11 +44,16 @@ export class ProviderInstanceRegistry extends Context.Service< } >()("t3/provider/ProviderInstanceRegistry") {} +interface InstanceSecrets { + readonly environment: ResolvedProviderInstanceEnvironment; + readonly config: Readonly>; +} + interface LiveEntry { readonly instance: ProviderInstance; readonly scope: Scope.Closeable; readonly entry: ProviderInstanceConfig; - readonly environment: ResolvedProviderInstanceEnvironment; + readonly secrets: InstanceSecrets; } interface RegistryState { @@ -62,8 +68,18 @@ const entryEqual = (a: ProviderInstanceConfig, b: ProviderInstanceConfig): boole const secretReferencesIn = (environment: ProviderInstanceEnvironment): Array => environment.flatMap((variable) => (typeof variable.value === "string" ? [] : [variable.value])); -const secretReferencesOf = (entry: ProviderInstanceConfig): ReadonlyArray => - secretReferencesIn(entry.environment ?? []); +const configSecretReferences = ( + driver: AnyProviderDriver | undefined, + entry: ProviderInstanceConfig, +) => (driver === undefined ? [] : secretReferencesInSettings(driver.configSchema, entry.config)); + +const secretReferencesOf = ( + driversById: ReadonlyMap>, + entry: ProviderInstanceConfig, +): ReadonlyArray => [ + ...secretReferencesIn(entry.environment ?? []), + ...configSecretReferences(driversById.get(entry.driver), entry).map(({ reference }) => reference), +]; const withoutSecretReferences = ( environment: ProviderInstanceEnvironment, @@ -72,25 +88,38 @@ const withoutSecretReferences = ( typeof variable.value === "string" ? [{ ...variable, value: variable.value }] : [], ); -const readSecretEnvironment = Effect.fn("ProviderInstanceRegistry.readSecretEnvironment")( - function* (environment: ProviderInstanceEnvironment) { - const references = secretReferencesIn(environment); - const values = - references.length === 0 ? [] : yield* (yield* SecretResolver.SecretResolver).read(references); - const unreadable: Array = []; - let index = 0; - const resolved: ResolvedProviderInstanceEnvironment = environment.flatMap((variable) => { - if (typeof variable.value === "string") return [{ ...variable, value: variable.value }]; - const secret = values[index++]; - if (secret === undefined) { - unreadable.push(variable.name); - return []; - } - return [{ ...variable, value: secret, sensitive: true }]; - }); - return { environment: resolved, unreadable }; - }, -); +const readInstanceSecrets = Effect.fn("ProviderInstanceRegistry.readInstanceSecrets")(function* ( + driver: AnyProviderDriver | undefined, + entry: ProviderInstanceConfig, +) { + const variables = entry.environment ?? []; + const configFields = configSecretReferences(driver, entry); + const references = [ + ...secretReferencesIn(variables), + ...configFields.map(({ reference }) => reference), + ]; + const values = + references.length === 0 ? [] : yield* (yield* SecretResolver.SecretResolver).read(references); + const unreadable: Array = []; + let index = 0; + const environment: ResolvedProviderInstanceEnvironment = variables.flatMap((variable) => { + if (typeof variable.value === "string") return [{ ...variable, value: variable.value }]; + const secret = values[index++]; + if (secret === undefined) { + unreadable.push(variable.name); + return []; + } + return [{ ...variable, value: secret, sensitive: true }]; + }); + const config: Record = {}; + for (const { field } of configFields) { + const secret = values[index++]; + if (secret === undefined) unreadable.push(field); + else config[field] = secret; + } + const secrets: InstanceSecrets = { environment, config }; + return { secrets, unreadable }; +}); /** Conflicting saved flags must preserve any explicit disable. */ const resolveEntryEnabled = (entry: ProviderInstanceConfig, typedConfig: unknown): boolean => { @@ -165,7 +194,7 @@ const buildEntry = Effect.fn("ProviderInstanceRegistry.buildEntry")(function* 0) { + if (deferSecrets && enabled && secretReferencesOf(driversById, entry).length > 0) { return { kind: "unavailable" as const, snapshot: yield* buildUnavailableProviderSnapshot({ @@ -177,9 +206,12 @@ const buildEntry = Effect.fn("ProviderInstanceRegistry.buildEntry")(function* 0) { @@ -195,17 +227,19 @@ const buildEntry = Effect.fn("ProviderInstanceRegistry.buildEntry")(function* (input: { replacedIds.add(instanceId); } } - for (const id of [...removedIds, ...replacedIds]) { + const retiredIds = [...removedIds, ...replacedIds]; + if (retiredIds.length > 0) { + const remaining = new Map(previousEntries); + for (const id of retiredIds) remaining.delete(id); + yield* Ref.set(state.entries, remaining); + } + for (const id of retiredIds) { const live = previousEntries.get(id); if (live) { yield* Scope.close(live.scope, Exit.void).pipe(Effect.ignore); @@ -287,7 +327,7 @@ const makeReconcile = (input: { const kept = previousEntries.has(instanceId) && !replacedIds.has(instanceId); return kept || !(yield* isEntryEnabled(driversById, entry)) ? [] - : secretReferencesOf(entry); + : secretReferencesOf(driversById, entry); }), ).pipe(Effect.map((perEntry) => perEntry.flat())); if (references.length > 0 && !deferSecrets) { @@ -409,13 +449,13 @@ export const makeProviderInstanceRegistry = Effect.fn( const configMap = yield* Ref.get(configMapRef); const targets = yield* Effect.filter(Object.entries(configMap), ([rawInstanceId, entry]) => (instanceId === undefined || rawInstanceId === instanceId) && - secretReferencesOf(entry).length > 0 + secretReferencesOf(driversById, entry).length > 0 ? isEntryEnabled(driversById, entry) : Effect.succeed(false), ); if (targets.length === 0) return; const resolver = yield* SecretResolver.SecretResolver; - const references = targets.flatMap(([, entry]) => secretReferencesOf(entry)); + const references = targets.flatMap(([, entry]) => secretReferencesOf(driversById, entry)); yield* resolver.invalidate(references); yield* resolver.read(references); const live = yield* Ref.get(entries); @@ -423,9 +463,12 @@ export const makeProviderInstanceRegistry = Effect.fn( for (const [rawInstanceId, entry] of targets) { const current = live.get(ProviderInstanceId.make(rawInstanceId)); if (current === undefined) continue; - const { environment, unreadable } = yield* readSecretEnvironment(entry.environment ?? []); + const { secrets, unreadable } = yield* readInstanceSecrets( + driversById.get(entry.driver), + entry, + ); // A failed re-read keeps the working instance and the value it already holds. - if (unreadable.length === 0 && !Equal.equals(environment, current.environment)) { + if (unreadable.length === 0 && !Equal.equals(secrets, current.secrets)) { rebuild.add(current.instance.instanceId); } } @@ -443,10 +486,12 @@ export const makeProviderInstanceRegistry = Effect.fn( ), ); const startupReferences = yield* Effect.filter(Object.entries(input.configMap), ([, entry]) => - secretReferencesOf(entry).length > 0 + secretReferencesOf(driversById, entry).length > 0 ? isEntryEnabled(driversById, entry) : Effect.succeed(false), - ).pipe(Effect.map((targets) => targets.flatMap(([, entry]) => secretReferencesOf(entry)))); + ).pipe( + Effect.map((targets) => targets.flatMap(([, entry]) => secretReferencesOf(driversById, entry))), + ); if (startupReferences.length > 0) { // The read waits outside the lock so other mutations are not held for a vault prompt; a // configuration saved meanwhile has already reconciled itself. diff --git a/apps/server/src/provider/ProviderRegistry.test.ts b/apps/server/src/provider/ProviderRegistry.test.ts index 87adb43a52375..95b7c217dbe69 100644 --- a/apps/server/src/provider/ProviderRegistry.test.ts +++ b/apps/server/src/provider/ProviderRegistry.test.ts @@ -1,3 +1,4 @@ +import * as CodexErrors from "effect-codex-app-server/errors"; import { vi } from "vite-plus/test"; import * as ServerSecretStore from "../auth/ServerSecretStore.ts"; import * as NodeServices from "@effect/platform-node/NodeServices"; @@ -6,7 +7,9 @@ import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; import * as Fiber from "effect/Fiber"; +import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; +import * as Path from "effect/Path"; import * as PubSub from "effect/PubSub"; import * as Ref from "effect/Ref"; import * as Schema from "effect/Schema"; @@ -30,6 +33,7 @@ import { } from "@t3tools/contracts"; import { HttpClient, HttpClientResponse } from "effect/http"; import { ChildProcessSpawner } from "effect/process"; +import * as HostProcess from "@t3tools/shared/HostProcess"; import { deepMerge } from "@t3tools/shared/Struct"; import { createModelCapabilities } from "@t3tools/shared/model"; import { applyServerSettingsPatch } from "@t3tools/shared/serverSettings"; @@ -79,6 +83,7 @@ const disabledDefaultSlots = ( ); const defaultClaudeSettings: ClaudeSettings = Schema.decodeSync(ClaudeSettings)({}); +const decodeCodexSettings = Schema.decodeSync(CodexSettings); const defaultCodexSettings: CodexSettings = Schema.decodeSync(CodexSettings)({}); process.env.T3CODE_CURSOR_ENABLED = "1"; @@ -191,6 +196,20 @@ function layerMockSpawner( ); } +// A Codex CLI on disk whose `app-server` and `--version` behave as `script` says. +const writeFakeCodexCli = Effect.fn("writeFakeCodexCli")(function* (script: string) { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-fake-codex-" }); + const binaryPath = path.join(directory, "codex"); + yield* fs.writeFileString(binaryPath, `#!/bin/sh\n${script}\n`); + yield* fs.chmod(binaryPath, 0o755); + return { directory, binaryPath }; +}); + +// The npm `@openai/codex` 0.1 CLI treats `app-server` as a prompt and exits 1. +const OBSOLETE_CODEX_APP_SERVER = `echo "Missing OpenAI API key." >&2; exit 1`; + const codexModelCapabilities = createModelCapabilities({ optionDescriptors: [ selectDescriptor("reasoningEffort", "Reasoning", [ @@ -287,6 +306,13 @@ const layerTestNodeServices = Layer.mergeAll( ProviderLatestVersions.layer, NodeServices.layer, Layer.mock(ServerSecretStore.ServerSecretStore)({}), + // An empty home, so the developer's own Claude settings never reach provider probes. + Layer.effect( + HostProcess.HomeDirectory, + Effect.gen(function* () { + return yield* (yield* FileSystem.FileSystem).makeTempDirectoryScoped(); + }), + ).pipe(Layer.provide(NodeServices.layer)), ); it.layer( @@ -393,6 +419,137 @@ it.layer( assert.strictEqual(status.auth.status, "unknown"); }), ); + + it.effect("reads the version of a Codex CLI too old to run app-server (#20351)", () => + Effect.gen(function* () { + if ((yield* HostProcess.Platform) === "win32") return; + const codexHome = yield* FileSystem.FileSystem.pipe( + Effect.flatMap((fs) => fs.makeTempDirectoryScoped({ prefix: "t3-codex-home-" })), + ); + const { binaryPath } = yield* writeFakeCodexCli( + [ + `if [ "$1" = "--version" ]; then`, + // The fallback runs with the provider's environment and CODEX_HOME. + ` [ "$CODEX_HOME" = "${codexHome}" ] && [ "$FAKE_CODEX_ENV" = "kept" ] || exit 2`, + ` echo 0.1.4161006; exit 0`, + `fi`, + OBSOLETE_CODEX_APP_SERVER, + ].join("\n"), + ); + const settings = decodeCodexSettings({ binaryPath, homePath: codexHome }); + + const status = yield* checkCodexProviderStatus(settings, undefined, { + ...process.env, + FAKE_CODEX_ENV: "kept", + }); + + assert.strictEqual(status.status, "error"); + assert.strictEqual(status.installed, true); + assert.strictEqual(status.version, "0.1.4161006"); + assert.match(status.message ?? "", /^Codex app-server provider probe failed: /); + const classified = withBundledCompatibility({ + ...status, + instanceId: ProviderInstanceId.make("codex"), + driver: ProviderDriverKind.make("codex"), + }); + assert.strictEqual(classified.compatibilityAdvisory?.status, "broken"); + }), + ); + + it.effect("keeps a prerelease --version unclassified instead of reading it as stable", () => + Effect.gen(function* () { + if ((yield* HostProcess.Platform) === "win32") return; + const { binaryPath } = yield* writeFakeCodexCli( + [ + `[ "$1" = "--version" ] && { echo "codex-cli 0.159.0-alpha.1"; exit 0; }`, + OBSOLETE_CODEX_APP_SERVER, + ].join("\n"), + ); + + const status = yield* checkCodexProviderStatus(decodeCodexSettings({ binaryPath })); + + assert.strictEqual(status.version, "0.159.0-alpha.1"); + const classified = withBundledCompatibility({ + ...status, + instanceId: ProviderInstanceId.make("codex"), + driver: ProviderDriverKind.make("codex"), + }); + assert.strictEqual(classified.compatibilityAdvisory?.status, "unknown"); + }), + ); + + it.effect("keeps the probe failure without a version when --version also fails", () => + Effect.gen(function* () { + if ((yield* HostProcess.Platform) === "win32") return; + const { binaryPath } = yield* writeFakeCodexCli( + [ + `[ "$1" = "--version" ] && { echo "codex 0.1.0" >&2; exit 3; }`, + OBSOLETE_CODEX_APP_SERVER, + ].join("\n"), + ); + + const status = yield* checkCodexProviderStatus(decodeCodexSettings({ binaryPath })); + + assert.strictEqual(status.status, "error"); + assert.strictEqual(status.installed, true); + assert.strictEqual(status.version, null); + assert.match(status.message ?? "", /^Codex app-server provider probe failed: /); + }), + ); + + it.effect("bounds a hanging Codex --version fallback and releases its process scope", () => + Effect.gen(function* () { + const started = yield* Deferred.make(); + const released = yield* Deferred.make(); + const spawner = ChildProcessSpawner.make(() => + Effect.gen(function* () { + yield* Effect.addFinalizer(() => Deferred.succeed(released, undefined)); + return ChildProcessSpawner.makeHandle({ + ...mockHandle({ stdout: "", stderr: "", code: 0 }), + exitCode: Deferred.succeed(started, undefined).pipe(Effect.andThen(Effect.never)), + }); + }), + ); + const fiber = yield* checkCodexProviderStatus(defaultCodexSettings, () => + Effect.fail(new CodexErrors.CodexAppServerProcessExitedError({ code: 1 })), + ).pipe( + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), + Effect.forkChild, + ); + yield* Deferred.await(started); + yield* TestClock.adjust("4 seconds"); + const status = yield* Fiber.join(fiber); + assert.strictEqual(status.version, null); + assert.strictEqual(status.status, "error"); + assert.strictEqual(status.installed, true); + assert.match(status.message ?? "", /^Codex app-server provider probe failed: /); + assert.isTrue(yield* Deferred.isDone(released)); + }), + ); + + it.effect("does not run --version when the Codex executable cannot start", () => + Effect.gen(function* () { + if ((yield* HostProcess.Platform) === "win32") return; + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const { directory, binaryPath } = yield* writeFakeCodexCli( + `touch "$(dirname "$0")/ran"; echo 0.1.4161006`, + ); + + const status = yield* checkCodexProviderStatus(decodeCodexSettings({ binaryPath }), () => + Effect.fail( + new CodexErrors.CodexAppServerSpawnError({ + command: `${binaryPath} app-server`, + cause: new Error("spawn ENOENT"), + }), + ), + ); + + assert.strictEqual(status.installed, false); + assert.strictEqual(status.version, null); + assert.isFalse(yield* fs.exists(path.join(directory, "ran"))); + }), + ); }); describe("ProviderRegistry.layer", () => { @@ -2345,6 +2502,42 @@ it.layer( ), ); + it.effect("tells a logged-out CLI which models need a Claude Code update", () => + Effect.gen(function* () { + const status = yield* checkClaudeProviderStatus( + defaultClaudeSettings, + claudeCapabilities({ + tokenSource: "none", + apiKeySource: "none", + apiProvider: "firstParty", + }), + { ANTHROPIC_BASE_URL: "" }, + undefined, + { + models: [ + { + model: { slug: "claude-next", name: "Next", isCustom: false, capabilities: null }, + runtime: {}, + compatibility: { minVersion: "1.1.0" }, + }, + ], + }, + ); + assert.strictEqual(status.auth.status, "unauthenticated"); + assert.deepStrictEqual(status.updateRequiredModels, [ + { slug: "claude-next", name: "Next", minVersion: "1.1.0" }, + ]); + }).pipe( + Effect.provide( + layerMockSpawner((args) => { + const joined = args.join(" "); + if (joined === "--version") return { stdout: "1.0.0\n", stderr: "", code: 0 }; + throw new Error(`Unexpected args: ${joined}`); + }), + ), + ), + ); + it.effect.each([ { source: "inherited HTTPS endpoint", baseUrl: "https://proxy.example/api", overrides: [] }, { @@ -2383,6 +2576,68 @@ it.layer( ), ); + it.effect.each([ + { + settings: { env: { ANTHROPIC_BASE_URL: "https://hub.example.ts.net:8317" } }, + baseUrl: "https://api.anthropic.com", + type: "customEndpoint", + label: "Custom API endpoint", + }, + { + settings: { env: { ANTHROPIC_BASE_URL: "https://api.anthropic.com" } }, + baseUrl: "https://proxy.example", + type: "apiKey", + label: "Claude API Key", + }, + { + settings: {}, + baseUrl: "https://proxy.example", + type: "customEndpoint", + label: "Custom API endpoint", + }, + ])( + "uses the settings endpoint before the process endpoint: $type / $baseUrl", + ({ settings, baseUrl, type, label }) => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const configDir = yield* fileSystem.makeTempDirectoryScoped({ + prefix: "t3-claude-settings-", + }); + yield* fileSystem.writeFileString( + path.join(configDir, "settings.json"), + JSON.stringify(settings), + ); + const stateDir = yield* fileSystem.makeTempDirectoryScoped(); + yield* fileSystem.makeDirectory(path.join(stateDir, ".claude")); + yield* fileSystem.writeFileString( + path.join(stateDir, ".claude", "settings.json"), + JSON.stringify({ env: { ANTHROPIC_BASE_URL: "https://state-dir.example" } }), + ); + const status = yield* checkClaudeProviderStatus( + { ...defaultClaudeSettings, homePath: configDir }, + claudeCapabilities({ + tokenSource: "ANTHROPIC_AUTH_TOKEN", + apiKeySource: "none", + apiProvider: "firstParty", + }), + { ANTHROPIC_BASE_URL: baseUrl }, + stateDir, + ); + assert.strictEqual(status.status, "ready"); + assert.strictEqual(status.auth.type, type); + assert.strictEqual(status.auth.label, label); + }).pipe( + Effect.provide( + layerMockSpawner((args) => { + const joined = args.join(" "); + if (joined === "--version") return { stdout: "1.0.0\n", stderr: "", code: 0 }; + throw new Error(`Unexpected args: ${joined}`); + }), + ), + ), + ); + it.effect.each([ "https://api.anthropic.com", "https://api.anthropic.com:443", diff --git a/apps/server/src/provider/ProviderRegistry.ts b/apps/server/src/provider/ProviderRegistry.ts index 0932422d8496a..c980a22169bf1 100644 --- a/apps/server/src/provider/ProviderRegistry.ts +++ b/apps/server/src/provider/ProviderRegistry.ts @@ -44,6 +44,7 @@ import type { import { makeManualOnlyProviderMaintenanceCapabilities, type ProviderMaintenanceCapabilities, + latestProviderVersionCacheKeys, } from "@t3tools/provider-core/server/maintenanceResolver"; import type { ProviderSnapshotSource } from "./builtInProviderCatalog.ts"; @@ -722,9 +723,11 @@ export const layer = Layer.effect( Effect.gen(function* () { yield* instance.invalidateCaches ?? Effect.void; const maintenance = yield* instance.snapshot.resolveMaintenance({ fresh: true }); - if (maintenance.packageName) { - yield* latestVersions.invalidate(maintenance.packageName); - } + yield* Effect.forEach( + latestProviderVersionCacheKeys(maintenance), + latestVersions.invalidate, + { discard: true }, + ); }).pipe(Effect.catchCause((cause) => recoverInstanceFailure(instance.instanceId, cause))), { concurrency: "unbounded", discard: true }, ); diff --git a/apps/server/src/provider/providerMaintenanceRunner.test.ts b/apps/server/src/provider/providerMaintenanceRunner.test.ts index 943928c4e18cf..9b7d1ffb35d41 100644 --- a/apps/server/src/provider/providerMaintenanceRunner.test.ts +++ b/apps/server/src/provider/providerMaintenanceRunner.test.ts @@ -153,6 +153,9 @@ function layerMockSpawner( function makeRegistry( initialProviders: ServerProvider | ReadonlyArray = baseProvider, + options?: { + readonly installedVersionAfterUpdate?: string; + }, ) { return Effect.gen(function* () { const providersRef = yield* Ref.make>( @@ -193,7 +196,18 @@ function makeRegistry( refresh: () => Ref.get(providersRef), refreshInstance: () => Ref.get(providersRef), reloadSecretsAndRefresh: () => Ref.get(providersRef), - refreshAfterUpdate: () => Ref.get(providersRef), + refreshAfterUpdate: () => { + const version = options?.installedVersionAfterUpdate; + if (version === undefined) { + return Ref.get(providersRef); + } + return Ref.updateAndGet(providersRef, (providers) => + providers.map((provider) => ({ + ...provider, + version, + })), + ); + }, refreshWorkspaceSnapshot: () => Ref.get(providersRef), getProviderMaintenanceCapabilitiesForInstance: (_instanceId, provider) => Effect.succeed(lifecycleFor(provider)), @@ -220,6 +234,7 @@ const makeTestRunner = ( ranges: [], })), }, + latestVersions: Layer.Layer = ProviderLatestVersions.layer, ) => Effect.service(ProviderMaintenanceRunner.ProviderMaintenanceRunner).pipe( Effect.provide( @@ -233,13 +248,28 @@ const makeTestRunner = ( forceRefresh: Effect.succeed(manifest), refreshInBackground: Effect.void, }), - ProviderLatestVersions.layer, + latestVersions, ), ), ), ), ); +function layerFailingLatestLookupAfterFirst() { + let lookups = 0; + return Layer.succeed(ProviderLatestVersions.ProviderLatestVersions, { + cached: (_key, lookup) => + Effect.gen(function* () { + lookups += 1; + if (lookups > 1) { + return yield* Effect.die("latest lookup failed"); + } + return yield* lookup; + }), + invalidate: () => Effect.void, + }); +} + describe("providerMaintenanceRunner", () => { it.effect("runs the allowlisted provider update command and records success", () => { const calls: Array<{ command: string; args: ReadonlyArray }> = []; @@ -269,7 +299,190 @@ describe("providerMaintenanceRunner", () => { Effect.provide( Layer.mergeAll( layerNonWindowsPlatform, - layerLatestVersionHttpClient("0.0.0"), + layerLatestVersionHttpClient("1.0.0"), + layerMockSpawner((command, args) => { + calls.push({ command, args }); + return { stdout: "updated" }; + }), + ), + ), + ); + }); + + it.effect.each([ + { name: "current", probe: {}, skip: true }, + { name: "failed", probe: { status: "error" }, skip: false }, + { name: "behind", probe: { version: "2.1.309" }, skip: false }, + { name: "unknown", probe: { version: null }, skip: false }, + { + name: "installed current", + probe: { version: "2.1.309", installedVersion: "2.1.310" }, + skip: true, + }, + { name: "installed behind", probe: { installedVersion: "2.1.309" }, skip: false }, + { name: "installed unknown", probe: { installedVersion: null }, skip: false }, + ] as const)("only skips a successful fresh current probe: $name", ({ probe, skip }) => { + const calls: Array<{ command: string; args: ReadonlyArray }> = []; + const refreshedInstanceIds: Array = []; + return Effect.gen(function* () { + const { registry, updateStatesRef, providersRef } = yield* makeRegistry({ + ...baseProvider, + version: "2.1.309", + versionAdvisory: { + status: "behind_latest", + currentVersion: "2.1.309", + latestVersion: "2.1.310", + updateCommand: "claude update", + canUpdate: true, + checkedAt: "2026-04-10T00:00:00.000Z", + message: "Update available.", + }, + }); + const updater = yield* makeTestRunner({ + ...registry, + refreshInstance: (instanceId) => { + refreshedInstanceIds.push(instanceId); + return Ref.updateAndGet(providersRef, (providers) => + providers.map((provider) => + provider.instanceId === instanceId + ? { + ...provider, + version: "2.1.310", + ...probe, + checkedAt: "2026-04-10T00:00:01.000Z", + } + : provider, + ), + ); + }, + getProviderMaintenanceCapabilitiesForInstance: () => + Effect.succeed( + makeProviderMaintenanceCapabilities({ + provider: CODEX_DRIVER, + packageName: "@anthropic-ai/claude-code", + updateExecutable: "claude", + updateArgs: ["update"], + updateLockKey: "claudeAgent-native", + }), + ), + }); + + const result = yield* updater.updateProvider(CODEX_DRIVER); + assert.deepStrictEqual(refreshedInstanceIds, [CODEX_INSTANCE_ID]); + assert.deepStrictEqual(calls, skip ? [] : [{ command: "claude", args: ["update"] }]); + if (!skip) return; + assert.strictEqual(result.providers[0]?.updateState?.status, "succeeded"); + assert.strictEqual(result.providers[0]?.updateState?.message, "Already up to date."); + assert.deepStrictEqual( + (yield* Ref.get(updateStatesRef)).map((state) => [state.status, state.message]), + [ + ["queued", "Waiting for another provider update to finish."], + ["running", "Checking for the latest version"], + ["succeeded", "Already up to date."], + ], + ); + }).pipe( + Effect.provide( + Layer.mergeAll( + layerNonWindowsPlatform, + layerLatestVersionHttpClient("2.1.310"), + layerMockSpawner((command, args) => { + calls.push({ command, args }); + return { stdout: "updated" }; + }), + ), + ), + ); + }); + + it.effect("still runs the installer when refreshInstance returns the cached snapshot", () => { + const calls: Array<{ command: string; args: ReadonlyArray }> = []; + return Effect.gen(function* () { + const { registry } = yield* makeRegistry({ + ...baseProvider, + version: "2.1.310", + }); + const updater = yield* makeTestRunner({ + ...registry, + getProviderMaintenanceCapabilitiesForInstance: () => + Effect.succeed( + makeProviderMaintenanceCapabilities({ + provider: CODEX_DRIVER, + packageName: "@anthropic-ai/claude-code", + updateExecutable: "claude", + updateArgs: ["update"], + updateLockKey: "claudeAgent-native", + }), + ), + }); + + const result = yield* updater.updateProvider(CODEX_DRIVER); + assert.deepStrictEqual(calls, [{ command: "claude", args: ["update"] }]); + assert.strictEqual(result.providers[0]?.updateState?.status, "succeeded"); + }).pipe( + Effect.provide( + Layer.mergeAll( + layerNonWindowsPlatform, + layerLatestVersionHttpClient("2.1.310"), + layerMockSpawner((command, args) => { + calls.push({ command, args }); + return { stdout: "updated" }; + }), + ), + ), + ); + }); + + it.effect("still runs the installer when precheck advisory enrichment fails", () => { + const calls: Array<{ command: string; args: ReadonlyArray }> = []; + return Effect.gen(function* () { + const { registry, providersRef } = yield* makeRegistry({ + ...baseProvider, + version: "2.1.310", + versionAdvisory: { + status: "current", + currentVersion: "2.1.310", + latestVersion: "2.1.310", + updateCommand: "claude update", + canUpdate: true, + checkedAt: "2026-04-10T00:00:00.000Z", + message: null, + }, + }); + const updater = yield* makeTestRunner( + { + ...registry, + refreshInstance: (instanceId) => + Ref.updateAndGet(providersRef, (providers) => + providers.map((provider) => + provider.instanceId === instanceId + ? { ...provider, checkedAt: "2026-04-10T00:00:01.000Z" } + : provider, + ), + ), + getProviderMaintenanceCapabilitiesForInstance: () => + Effect.succeed( + makeProviderMaintenanceCapabilities({ + provider: CODEX_DRIVER, + packageName: "@anthropic-ai/claude-code", + updateExecutable: "claude", + updateArgs: ["update"], + updateLockKey: "claudeAgent-native", + }), + ), + }, + undefined, + layerFailingLatestLookupAfterFirst(), + ); + + const result = yield* updater.updateProvider(CODEX_DRIVER); + assert.deepStrictEqual(calls, [{ command: "claude", args: ["update"] }]); + assert.strictEqual(result.providers[0]?.updateState?.status, "succeeded"); + }).pipe( + Effect.provide( + Layer.mergeAll( + layerNonWindowsPlatform, + layerLatestVersionHttpClient("2.1.310"), layerMockSpawner((command, args) => { calls.push({ command, args }); return { stdout: "updated" }; @@ -298,7 +511,7 @@ describe("providerMaintenanceRunner", () => { Effect.provide( Layer.mergeAll( layerNonWindowsPlatform, - layerLatestVersionHttpClient("0.0.0"), + layerLatestVersionHttpClient("1.0.0"), layerMockSpawner(() => ({ stdout: "updated" })), ), ), @@ -331,7 +544,7 @@ describe("providerMaintenanceRunner", () => { Effect.provide( Layer.mergeAll( layerNonWindowsPlatform, - layerLatestVersionHttpClient("0.0.0"), + layerLatestVersionHttpClient("1.0.0"), layerMockSpawner((_command, _args, options) => { seen.push(options.env); return { stdout: "updated" }; @@ -371,7 +584,7 @@ describe("providerMaintenanceRunner", () => { Effect.provide( Layer.mergeAll( layerNonWindowsPlatform, - layerLatestVersionHttpClient("0.0.0"), + layerLatestVersionHttpClient("1.0.0"), layerMockSpawner((command, args) => { calls.push({ command, args }); return { stdout: "updated" }; @@ -396,6 +609,28 @@ describe("providerMaintenanceRunner", () => { 4_096, ); assert.strictEqual(ProviderMaintenanceRunner.toProgressLine("x".repeat(300))?.length, 200); + + for (const redraw of [ + "\r", + "\u001b[G", + "\u001b[0G", + "\u001b[1G", + "\u001b[01G", + "\u001b[999D", + "\u001b[999D\u001b[J", + ]) { + const output = `first${redraw}second${redraw}last`; + for (let boundary = 0; boundary <= output.length; boundary++) { + let partialLine = ""; + for (const chunk of [output.slice(0, boundary), output.slice(boundary)]) { + partialLine = ProviderMaintenanceRunner.splitOutputLines(partialLine, chunk).partialLine; + } + assert.strictEqual(ProviderMaintenanceRunner.toProgressLine(partialLine), "last"); + } + } + for (const incomplete of ["\u001b", "\u001b[", "\u001b[0", "\u001b[01"]) { + assert.strictEqual(ProviderMaintenanceRunner.toProgressLine(`progress${incomplete}`), null); + } }); it.effect("aborts without running when the installation changed since the advisory", () => { @@ -433,7 +668,7 @@ describe("providerMaintenanceRunner", () => { Effect.provide( Layer.mergeAll( layerNonWindowsPlatform, - layerLatestVersionHttpClient("0.0.0"), + layerLatestVersionHttpClient("1.0.0"), layerMockSpawner((command) => { calls.push(command); return { stdout: "updated" }; @@ -461,7 +696,7 @@ describe("providerMaintenanceRunner", () => { Effect.provide( Layer.mergeAll( layerNonWindowsPlatform, - layerLatestVersionHttpClient("0.0.0"), + layerLatestVersionHttpClient("1.0.0"), layerMockSpawner(() => ({ stdout: "checking install\n", stderr: "Error: Could not detect the Codex installation method.\n\n", @@ -487,7 +722,7 @@ describe("providerMaintenanceRunner", () => { Effect.provide( Layer.mergeAll( layerNonWindowsPlatform, - layerLatestVersionHttpClient("0.0.0"), + layerLatestVersionHttpClient("1.0.0"), layerMockSpawner(() => ({ code: 1 })), ), ), @@ -552,7 +787,7 @@ describe("providerMaintenanceRunner", () => { Effect.provide( Layer.mergeAll( layerNonWindowsPlatform, - layerLatestVersionHttpClient("0.0.0"), + layerLatestVersionHttpClient("1.0.0"), layerMockSpawner(() => { startedLatch.resolve(); return { @@ -571,7 +806,9 @@ describe("providerMaintenanceRunner", () => { "releases the running-provider marker when interrupted after queuing but before the lock run starts", () => Effect.gen(function* () { - const { registry } = yield* makeRegistry(baseProvider); + const { registry } = yield* makeRegistry(baseProvider, { + installedVersionAfterUpdate: "1.0.0", + }); let blockQueuedState = true; const queuedStateWrittenLatch: { resolve: () => void } = { resolve: () => {} }; const releaseQueuedStateLatch: { resolve: () => void } = { resolve: () => {} }; @@ -612,7 +849,7 @@ describe("providerMaintenanceRunner", () => { Effect.provide( Layer.mergeAll( layerNonWindowsPlatform, - layerLatestVersionHttpClient("0.0.0"), + layerLatestVersionHttpClient("1.0.0"), layerMockSpawner(() => ({ stdout: "updated" })), ), ), @@ -626,7 +863,9 @@ describe("providerMaintenanceRunner", () => { readonly shell: boolean | string | undefined; }> = []; return Effect.gen(function* () { - const { registry } = yield* makeRegistry(baseProvider); + const { registry } = yield* makeRegistry(baseProvider, { + installedVersionAfterUpdate: "1.0.0", + }); const runner = yield* makeTestRunner(registry); const result = yield* runner.updateProvider(CODEX_DRIVER); @@ -652,7 +891,7 @@ describe("providerMaintenanceRunner", () => { Layer.succeed(SpawnExecutableResolution, (command) => command === "npm" ? "C:\\fake\\npm\\npm.cmd" : undefined, ), - layerLatestVersionHttpClient("0.0.0"), + layerLatestVersionHttpClient("1.0.0"), Layer.succeed( ChildProcessSpawner.ChildProcessSpawner, ChildProcessSpawner.make((command) => { @@ -689,8 +928,14 @@ it.effect.each([ status: "succeeded", }, { - mode: "unknown", + mode: "targeted-current", version: "1.14.20", + installation: { installedVersion: "1.14.20" }, + status: "succeeded", + }, + { + mode: "unknown", + version: "1.14.19", installation: { installedVersion: null }, status: "unchanged", }, @@ -723,6 +968,10 @@ it.effect.each([ const updater = yield* makeTestRunner( { ...registry, + refreshInstance: () => + Ref.updateAndGet(providersRef, (providers) => + providers.map((provider) => ({ ...provider, checkedAt: "2026-04-10T00:00:01.000Z" })), + ), getProviderMaintenanceCapabilitiesForInstance: () => Effect.succeed(pinnedCapabilities), refreshAfterUpdate: () => Effect.gen(function* () { @@ -786,7 +1035,7 @@ it.effect( "uses fresh installer versions for post-update verification and compatibility checks", () => { const calls: string[] = []; - let latest = "1.0.0"; + let latest = "2.0.0"; const manifest: ModelManifest.ModelManifestData = { version: 1, currentModels: {}, diff --git a/apps/server/src/provider/providerMaintenanceRunner.ts b/apps/server/src/provider/providerMaintenanceRunner.ts index 9dddf23579d8a..c2347da003300 100644 --- a/apps/server/src/provider/providerMaintenanceRunner.ts +++ b/apps/server/src/provider/providerMaintenanceRunner.ts @@ -83,6 +83,7 @@ class ProviderMaintenanceCommandError extends Data.TaggedError("ProviderMaintena interface VerifiedProviderRefresh { readonly providers: ReadonlyArray; readonly verifiedProviders: ReadonlyArray; + readonly advisoryEnriched: boolean; } const nowIso = Effect.map(DateTime.now, DateTime.formatIso); @@ -203,11 +204,14 @@ export function splitOutputLines( partialLine: string, text: string, ): { readonly lines: ReadonlyArray; readonly partialLine: string } { - const parts = (partialLine + text).split(/\r\n|\r|\n/); + const parts = (partialLine + text).replace(/\x1b\[(?:0*1?G|999D)/g, "\r").split(/\r\n|\r|\n/); return { partialLine: (parts.pop() ?? "").slice(-UPDATE_PARTIAL_LINE_MAX_LENGTH), lines: parts }; } export function toProgressLine(line: string): string | null { + if (/\x1b(?:\[[0-9]*)?$/.test(line)) { + return null; + } const text = NodeUtil.stripVTControlCharacters(line).replace(/\s+/g, " ").trim(); if (text.length === 0) { return null; @@ -315,12 +319,14 @@ export const make = Effect.fn("ProviderMaintenanceRunner.make")(function* () { }), }); - const verifyRefreshedProvider = ( + const verifyFromRefresh = ( + refresh: Effect.Effect>, provider: ProviderDriverKind, maintenanceCapabilities: ProviderMaintenanceCapabilities, instanceId: ProviderInstanceId, + phase: "pre-update" | "post-update", ): Effect.Effect => - providerRegistry.refreshAfterUpdate(provider, instanceId).pipe( + refresh.pipe( Effect.flatMap((providers) => { const refreshedProviders = providers.filter( (candidate) => candidate.driver === provider && candidate.instanceId === instanceId, @@ -329,6 +335,7 @@ export const make = Effect.fn("ProviderMaintenanceRunner.make")(function* () { return Effect.succeed({ providers, verifiedProviders: [], + advisoryEnriched: true, }); } return Effect.forEach( @@ -348,15 +355,18 @@ export const make = Effect.fn("ProviderMaintenanceRunner.make")(function* () { Effect.map((verifiedProviders): VerifiedProviderRefresh => ({ providers, verifiedProviders, + advisoryEnriched: true, })), Effect.catchCause((cause) => - Effect.logWarning("Provider post-update version verification failed", { + Effect.logWarning("Provider version verification failed", { provider, + phase, cause: Cause.pretty(cause), }).pipe( Effect.as({ providers, verifiedProviders: refreshedProviders, + advisoryEnriched: false, }), ), ), @@ -481,6 +491,41 @@ export const make = Effect.fn("ProviderMaintenanceRunner.make")(function* () { }), ); } + + if (targetVersion === undefined) { + const prior = (yield* providerRegistry.getProviders).find( + (candidate) => candidate.instanceId === instanceId, + ); + const precheck = yield* verifyFromRefresh( + providerRegistry.refreshInstance(instanceId), + provider, + fresh, + instanceId, + "pre-update", + ); + const alreadyCurrent = + precheck.advisoryEnriched && + prior !== undefined && + precheck.verifiedProviders.length > 0 && + precheck.verifiedProviders.every( + (verified) => + verified.checkedAt !== prior.checkedAt && + verified.installed && + verified.status !== "error" && + verified.versionAdvisory?.status === "current", + ); + if (alreadyCurrent) { + return yield* finish( + makeUpdateState({ + status: "succeeded", + startedAt, + finishedAt: yield* nowIso, + message: "Already up to date.", + }), + ); + } + } + yield* setRunningMessage(`Running ${describeCommand(command)}`); const result = yield* runMaintenanceCommand(command, setRunningMessage); const finishedAt = yield* nowIso; @@ -512,10 +557,12 @@ export const make = Effect.fn("ProviderMaintenanceRunner.make")(function* () { yield* invalidateLatestProviderVersion(verified).pipe( Effect.provideService(ProviderLatestVersions.ProviderLatestVersions, latestVersions), ); - const { verifiedProviders } = yield* verifyRefreshedProvider( + const { verifiedProviders } = yield* verifyFromRefresh( + providerRegistry.refreshAfterUpdate(provider, instanceId), provider, verified, instanceId, + "post-update", ); // Cursor about probes can fail transiently; missing versions alone do not imply failure. const couldNotVerify = diff --git a/apps/server/src/pullRequest/PullRequestReadCache.test.ts b/apps/server/src/pullRequest/PullRequestReadCache.test.ts index 2485be7a97a30..bb6eb38f16146 100644 --- a/apps/server/src/pullRequest/PullRequestReadCache.test.ts +++ b/apps/server/src/pullRequest/PullRequestReadCache.test.ts @@ -123,6 +123,27 @@ it.layer(NodeServices.layer)("PR filesystem cache", (it) => { }), ); + it.effect.each(["", "{", '{"pr":{"revision":42}}'])( + "recovers from malformed scope revisions without reusing stale entries: %j", + (corrupt) => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pr-cache-" }); + const original = yield* cacheLayer(directory); + yield* original.get("summary", Effect.succeed("stale"), ["pr"]); + yield* fs.writeFileString(directory + "/revisions", corrupt); + const recovered = yield* cacheLayer(directory); + let reads = 0; + const lookup = Effect.sync(() => String(++reads)); + assert.strictEqual(yield* recovered.get("summary", lookup, ["pr"]), "1"); + assert.strictEqual(yield* recovered.get("summary", lookup, ["pr"]), "1"); + yield* recovered.invalidate("pr"); + assert.strictEqual(yield* recovered.get("summary", lookup, ["pr"]), "2"); + const restarted = yield* cacheLayer(directory); + assert.strictEqual(yield* restarted.get("summary", lookup, ["pr"]), "2"); + assert.strictEqual(reads, 2); + }), + ); it.effect("resumes caching after a failed scope read", () => Effect.gen(function* () { const backing = yield* KeyValueStore.KeyValueStore; diff --git a/apps/server/src/pullRequest/PullRequestReadCache.ts b/apps/server/src/pullRequest/PullRequestReadCache.ts index 62ac7da3789d6..6ea0aea8f76b6 100644 --- a/apps/server/src/pullRequest/PullRequestReadCache.ts +++ b/apps/server/src/pullRequest/PullRequestReadCache.ts @@ -88,9 +88,17 @@ export const make = Effect.gen(function* () { crypto.digest("SHA-256", new TextEncoder().encode(key)).pipe(Effect.map(Hex.encode)); const revisions = yield* Cache.makeWith( () => - backing - .get("revisions") - .pipe(Effect.flatMap((raw) => Schema.decodeEffect(revisionCodec)(raw ?? "{}"))), + backing.get("revisions").pipe( + Effect.flatMap((raw) => Schema.decodeEffect(revisionCodec)(raw ?? "{}")), + Effect.catchTags({ + SchemaError: Effect.fn("PullRequestReadCache.recoverRevisions")(function* () { + // Without scope revisions, persisted PR entries cannot be trusted. + yield* backing.clear; + yield* Effect.logWarning("PR cache reset after invalid scope revisions"); + return yield* Schema.decodeEffect(revisionCodec)("{}"); + }), + }), + ), { capacity: 1, timeToLive: (exit) => (Exit.isSuccess(exit) ? Duration.infinity : Duration.zero), diff --git a/apps/server/src/pullRequest/PullRequestReviewGuideService.test.ts b/apps/server/src/pullRequest/PullRequestReviewGuideService.test.ts index 40db12212520b..e5f99df9d9354 100644 --- a/apps/server/src/pullRequest/PullRequestReviewGuideService.test.ts +++ b/apps/server/src/pullRequest/PullRequestReviewGuideService.test.ts @@ -467,6 +467,51 @@ it.layer(layerBase, { excludeTestServices: true })("PullRequestReviewGuideServic ), ); + it.effect("keeps excerpts of source lines a configured diff driver would convert away", () => + Effect.scoped( + Effect.gen(function* () { + const repositories = yield* makeRepositories; + const fs = yield* FileSystem.FileSystem; + yield* fs.writeFileString( + `${repositories.workspace}/.git/info/attributes`, + "feature.txt diff=blank\n", + ); + yield* git(repositories.workspace, ["config", "diff.blank.textconv", "true"]); + const stored = yield* Deferred.make(); + const context = yield* Layer.build( + makeLayer({ + ...repositories, + stored, + generate: () => + Effect.succeed({ + stops: [ + { + title: "The feature", + summary: "The change.", + attention: null, + excerpts: [{ path: "feature.txt", side: "new", start: 1, end: 3, caption: "" }], + files: [], + notes: [], + questions: [], + }, + ], + }), + }), + ); + const service = Context.get( + context, + PullRequestReviewGuideService.PullRequestReviewGuideService, + ); + yield* service.generate(reference); + yield* Deferred.await(stored); + const result = yield* service.get(reference); + assert.deepStrictEqual(result.guide?.stops[0]?.excerpts, [ + { path: "feature.txt", side: "new", start: 1, end: 3, caption: "" }, + ]); + }), + ), + ); + it.effect("refuses generation when the verified remote disappeared despite a usable origin", () => Effect.scoped( Effect.gen(function* () { diff --git a/apps/server/src/pullRequest/PullRequestReviewGuideService.ts b/apps/server/src/pullRequest/PullRequestReviewGuideService.ts index bdd39ffa7cca8..2da7350f2ebfd 100644 --- a/apps/server/src/pullRequest/PullRequestReviewGuideService.ts +++ b/apps/server/src/pullRequest/PullRequestReviewGuideService.ts @@ -345,7 +345,10 @@ const make = Effect.gen(function* () { }) .pipe(Effect.map((result) => result.stdout)); const [nameOnly, patch] = yield* Effect.all( - [diff(["--name-only", "-z"]), diff(["--unified=0", "--no-color", "--no-ext-diff"])], + [ + diff(["--name-only", "-z"]), + diff(["--unified=0", "--no-color", "--no-ext-diff", "--no-textconv"]), + ], { concurrency: "unbounded" }, ); return parseReviewGuideChange(nameOnly, patch); diff --git a/apps/server/src/pullRequest/PullRequestService.test.ts b/apps/server/src/pullRequest/PullRequestService.test.ts index 4334485a98a18..1b3a3fd073082 100644 --- a/apps/server/src/pullRequest/PullRequestService.test.ts +++ b/apps/server/src/pullRequest/PullRequestService.test.ts @@ -251,6 +251,7 @@ function makeService(input: { readonly projects: ReadonlyArray; readonly providers: ReadonlyArray; readonly resolveHandle?: SourceControlProviderRegistry.SourceControlProviderRegistry["Service"]["resolveHandle"]; + readonly onProjectRead?: () => void; }) { // Build in the test scope; per-call Effect.provide would close the marks database on service acquisition. return Effect.flatMap( @@ -268,11 +269,17 @@ function makeService(input: { }), Layer.mock(ProjectService.ProjectService)({ listShells: (options) => - Effect.succeed( - input.projects.filter((project) => options?.projectIds?.includes(project.id) ?? true), - ), + Effect.sync(() => { + input.onProjectRead?.(); + return input.projects.filter( + (project) => options?.projectIds?.includes(project.id) ?? true, + ); + }), getShell: (projectId) => - Effect.succeed(Option.fromNullishOr(input.projects.find((p) => p.id === projectId))), + Effect.sync(() => { + input.onProjectRead?.(); + return Option.fromNullishOr(input.projects.find((p) => p.id === projectId)); + }), }), Layer.mock(RepositoryIdentityResolver.RepositoryIdentityResolver)({ resolve: () => Effect.succeed(null), @@ -415,6 +422,52 @@ it.effect("reads nothing from a host with no implementation, but reports it", () }), ); +it.effect("reads nothing from a host its provider does not serve, but reports it", () => + Effect.gen(function* () { + const reached = new Set(); + const bitbucket = (id: string, host: string) => + project({ + id, + title: host, + workspaceRoot: `/${id}`, + repository: "proj/repo", + provider: SourceControlProviderKind.make("bitbucket"), + host, + }); + const service = yield* makeService({ + projects: [bitbucket("p1", "bitbucket.org"), bitbucket("p2", "bitbucket.example.com")], + providers: [ + fakePullRequestProvider(SourceControlProviderKind.make("bitbucket"), { + servesHost: (host) => host === "bitbucket.org", + getViewer: ({ host }) => { + reached.add(host ?? ""); + return Effect.succeed("bilal"); + }, + listChangeRequests: ({ host }) => { + reached.add(host); + return Effect.succeed({ items: [], truncated: false, continues: false }); + }, + }), + ], + }); + + const result = yield* service.list({ state: "open" }); + + assert.deepStrictEqual([...reached], ["bitbucket.org"]); + assert.deepStrictEqual( + result.providers.map(({ host, configured, detail }) => ({ host, configured, detail })), + [ + { host: "bitbucket.org", configured: true, detail: null }, + { + host: "bitbucket.example.com", + configured: false, + detail: "This host cannot be browsed here yet.", + }, + ], + ); + }), +); + it.effect("says where each repository carries on, and from nothing it has run out of", () => Effect.gen(function* () { const service = yield* makeService({ @@ -1303,7 +1356,11 @@ it.effect("refuses a repository that does not belong to the requested project", it.effect("routes a hosted reference to another repository through a project on that host", () => Effect.gen(function* () { const seen: Array<{ cwd: string; repository: string; host: string }> = []; + let projectReads = 0; const service = yield* makeService({ + onProjectRead: () => { + projectReads += 1; + }, projects: [ project({ id: "frontend", title: "web", workspaceRoot: "/web", repository: "acme/web" }), ], @@ -1325,6 +1382,20 @@ it.effect("routes a hosted reference to another repository through a project on assert.strictEqual(summary.number, 7); assert.deepStrictEqual(seen, [{ cwd: "/web", repository: "acme/api", host: "github.com" }]); + + // A sync sweep resolves the project once for all of its references, then reads fresh again. + const readsPerReference = projectReads; + const read = (number: number) => + service.summary( + { projectId: "frontend" as ProjectId, host: "github.com", repository: "acme/api", number }, + { recoverTransientFailure: false }, + ); + assert.isAbove(readsPerReference, 2); + yield* service.withSharedProjectReads(Effect.all([read(8), read(9), read(10)])); + // One read of the project and one of the host's projects, for all three references. + assert.strictEqual(projectReads, readsPerReference + 2); + yield* read(11); + assert.strictEqual(projectReads, readsPerReference * 2 + 2); }), ); diff --git a/apps/server/src/pullRequest/PullRequestService.ts b/apps/server/src/pullRequest/PullRequestService.ts index ab5f855191ced..e854abd38eb11 100644 --- a/apps/server/src/pullRequest/PullRequestService.ts +++ b/apps/server/src/pullRequest/PullRequestService.ts @@ -198,6 +198,10 @@ export class PullRequestService extends Context.Service< ) => Effect.Effect; readonly subscribeRefreshes: Stream.Stream; readonly refreshAfterTurn: (projectId: ProjectId) => Effect.Effect; + /** Project and remote lookups inside `effect` read once per filter, e.g. across one sync sweep. */ + readonly withSharedProjectReads: ( + effect: Effect.Effect, + ) => Effect.Effect; readonly detail: (input: PullRequestRef) => Effect.Effect; readonly preview: ( input: PullRequestRef, @@ -317,6 +321,12 @@ interface WorkspaceProjects { readonly viewerRoots: ReadonlyMap>; } +// Inherited by fibers forked inside the scope, so it is closed, not just dropped, when the scope ends. +const sharedProjectReads = Context.Reference<{ + open: boolean; + readonly reads: Map>; +} | null>("t3/PullRequestService/sharedProjectReads", { defaultValue: () => null }); + interface RepositoryBatch { readonly key: string; readonly entries: ReadonlyArray; @@ -488,6 +498,7 @@ function withRateLimitBackoff( ? {} : { unavailableRequirement: api.unavailableRequirement }), ...(api.repositoryKey === undefined ? {} : { repositoryKey: api.repositoryKey }), + ...(api.servesHost === undefined ? {} : { servesHost: api.servesHost }), ...(api.readsPublicHostWithoutCheckout === undefined ? {} : { readsPublicHostWithoutCheckout: api.readsPublicHostWithoutCheckout }), @@ -807,6 +818,34 @@ export const make = Effect.gen(function* () { const listWorkspaceProjects = ( filter: Pick, + ): Effect.Effect => + Effect.flatMap(sharedProjectReads, (shared) => { + if (shared === null || !shared.open) return readWorkspaceProjects(filter); + const key = JSON.stringify([filter.projectId, filter.projectIds, filter.host]); + const existing = shared.reads.get(key); + if (existing !== undefined) return existing; + return Effect.cached(readWorkspaceProjects(filter)).pipe( + Effect.tap((read) => Effect.sync(() => shared.reads.set(key, read))), + Effect.flatten, + ); + }); + + const withSharedProjectReads = (effect: Effect.Effect) => + Effect.suspend(() => { + const shared = { open: true, reads: new Map() }; + return effect.pipe( + Effect.provideService(sharedProjectReads, shared), + Effect.ensuring( + Effect.sync(() => { + shared.open = false; + shared.reads.clear(); + }), + ), + ); + }); + + const readWorkspaceProjects = ( + filter: Pick, ): Effect.Effect => (filter.projectId === undefined ? projects.listShells( @@ -866,7 +905,8 @@ export const make = Effect.gen(function* () { if (filter.host !== undefined && host !== filter.host.toLowerCase()) { continue; } - const api = registry.get(kind); + const registered = registry.get(kind); + const api = registered?.servesHost?.(host) === false ? null : registered; if (api !== null) { const roots = viewerRoots.get(host); if (roots === undefined) viewerRoots.set(host, [project.workspaceRoot]); @@ -1338,19 +1378,24 @@ export const make = Effect.gen(function* () { : null, }; }), - Effect.orElseSucceed((): RepositoryBatch => ({ - key, - entries: [], - errors: [ - { - projectId: project.project.id, - projectTitle: project.project.title, - message: `${project.repository} could not be read.`, - }, - ], - truncated: false, - nextCursor: null, - })), + Effect.catch((error): Effect.Effect => + Effect.succeed({ + key, + entries: [], + errors: [ + { + projectId: project.project.id, + projectTitle: project.project.title, + message: + error.accessHint === undefined + ? `${project.repository} could not be read.` + : `${project.repository} could not be read. ${error.accessHint}`, + }, + ], + truncated: false, + nextCursor: null, + }), + ), ); } }; @@ -3528,6 +3573,7 @@ export const make = Effect.gen(function* () { ); return PullRequestService.of({ + withSharedProjectReads, list, filterOptions, // The local operation owns the span; this wrapper only canonicalizes refs. diff --git a/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.test.ts b/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.test.ts index 69479b1ccfeed..a47743b34263a 100644 --- a/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.test.ts +++ b/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.test.ts @@ -91,6 +91,37 @@ describe("parseReviewGuideChange", () => { expect(change.hunks.get("src/gone.ts")).toEqual({ new: [], old: [{ start: 1, end: 20 }] }); expect(change.hunks.get("docs/readme.md")).toEqual({ new: [{ start: 3, end: 3 }], old: [] }); }); + + it("reads file names git quotes or ends with a tab", () => { + const unusual = [ + "diff --git a/with space.txt b/with space.txt", + "--- a/with space.txt\t", + "+++ b/with space.txt\t", + "@@ -1,0 +2 @@", + 'diff --git "a/tab\\tname.txt" "b/tab\\tname.txt"', + '--- "a/tab\\tname.txt"', + '+++ "b/tab\\tname.txt"', + "@@ -1,0 +2 @@", + 'diff --git "a/back\\\\slash.txt" "b/back\\\\slash.txt"', + '--- "a/back\\\\slash.txt"', + '+++ "b/back\\\\slash.txt"', + "@@ -1,0 +2 @@", + 'diff --git "a/gone\\tfile.txt" "b/gone\\tfile.txt"', + '--- "a/gone\\tfile.txt"', + "+++ /dev/null", + "@@ -1 +0,0 @@", + "", + ].join("\n"); + const names = ["with space.txt", "tab\tname.txt", "back\\slash.txt", "gone\tfile.txt"]; + const parsed = parseReviewGuideChange(names.join("\0"), unusual); + expect([...parsed.hunks.keys()]).toEqual(names); + expect(parsed.hunks.get("gone\tfile.txt")).toEqual({ new: [], old: [{ start: 1, end: 1 }] }); + const result = normalizeReviewGuide( + guide({ stops: [stop({ excerpts: names.slice(0, 3).map((path) => excerpt(path, 1, 5)) })] }), + parsed, + ); + expect(result.stops[0]?.excerpts).toEqual(names.slice(0, 3).map((path) => excerpt(path, 2, 2))); + }); }); describe("normalizeReviewGuide", () => { diff --git a/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.ts b/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.ts index 4075f19ba9ba9..184908e4bd7f2 100644 --- a/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.ts +++ b/apps/server/src/pullRequest/pullRequestReviewGuideNormalize.ts @@ -1,5 +1,6 @@ import type { PullRequestReviewGuideExcerpt, PullRequestReviewGuideStop } from "@t3tools/contracts"; import type { ReviewGuideGenerationResult } from "@t3tools/provider-core/server/textGeneration"; +import { unquoteGitPatchPath } from "@t3tools/shared/gitPatchPath"; const MAX_EXCERPT_LINES = 25; @@ -25,6 +26,18 @@ const hunkRange = (start: string, count: string | undefined): LineRange | null = return lines === 0 ? null : { start: Number(start), end: Number(start) + lines - 1 }; }; +/** + * Git quotes a name holding a tab, quote or backslash, and ends an unquoted name holding a space + * with a tab, so the first literal tab is never part of what the file is called. + */ +function headerPath(line: string, prefix: string): string | null { + const rest = line.slice(4); + const tab = rest.indexOf("\t"); + const token = tab === -1 ? rest : rest.slice(0, tab); + const path = token.startsWith('"') ? unquoteGitPatchPath(token) : token; + return path.startsWith(prefix) ? path.slice(prefix.length) : null; +} + export function parseReviewGuideChange(nameOnly: string, patch: string): ReviewGuideChange { const hunks = new Map(); let oldPath: string | null = null; @@ -37,9 +50,9 @@ export function parseReviewGuideChange(nameOnly: string, patch: string): ReviewG current = null; inHunks = false; } else if (!inHunks && line.startsWith("--- ")) { - oldPath = line.startsWith("--- a/") ? line.slice(6) : null; + oldPath = headerPath(line, "a/"); } else if (!inHunks && line.startsWith("+++ ")) { - const path = line.startsWith("+++ b/") ? line.slice(6) : oldPath; + const path = headerPath(line, "b/") ?? oldPath; if (path === null) continue; current = hunks.get(path) ?? { new: [], old: [] }; hunks.set(path, current); diff --git a/apps/server/src/scheduledTasks/ScheduledTaskService.ts b/apps/server/src/scheduledTasks/ScheduledTaskService.ts index 3829124f4e57c..472102f031409 100644 --- a/apps/server/src/scheduledTasks/ScheduledTaskService.ts +++ b/apps/server/src/scheduledTasks/ScheduledTaskService.ts @@ -46,6 +46,7 @@ import * as ThreadLaunchService from "../orchestration-v2/ThreadLaunchService.ts import * as ManagedProjectFolders from "../project/ManagedProjectFolders.ts"; import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts"; import * as SecretRequests from "../secrets/SecretRequests.ts"; +import * as DueHint from "../scheduling/DueHint.ts"; import * as Scheduler from "../scheduling/Scheduler.ts"; import { isMissedFixedTimeRun, isSameSchedule, nextScheduledRunAt } from "./Schedule.ts"; import { @@ -393,35 +394,6 @@ export const listDueTasks = Effect.fn("ScheduledTaskService.listDueTasks")(funct return tasks; }); -// Backstop for a write path that forgets to invalidate; the tick still runs every few seconds. -const DUE_HINT_MAX_AGE_MS = 10 * 60 * 1_000; - -/** - * Remembers when a poller next has work so idle scheduler ticks skip SQL. - * `invalidate` after every write that can move work earlier; a tick records - * only if no write happened since it read `version`. - */ -const makeDueHint = () => { - let version = 0; - let hint: { readonly dueAtMs: number; readonly checkedAtMs: number } | null = null; - return { - invalidate: Effect.sync(() => { - version++; - hint = null; - }), - version: Effect.sync(() => version), - isIdle: (nowMs: number) => - Effect.sync( - () => - hint !== null && hint.dueAtMs > nowMs && nowMs - hint.checkedAtMs < DUE_HINT_MAX_AGE_MS, - ), - record: (readVersion: number, dueAtMs: number, nowMs: number) => - Effect.sync(() => { - if (readVersion === version) hint = { dueAtMs, checkedAtMs: nowMs }; - }), - }; -}; - export const layer = Layer.effect( ScheduledTaskService, Effect.gen(function* () { @@ -452,8 +424,8 @@ export const layer = Layer.effect( const activeRuns = yield* Ref.make>(new Set()); // Notifications re-emit the full list; slow subscribers need only the newest dirty signal. const changesPubSub = yield* PubSub.sliding(1); - const dueTasks = makeDueHint(); - const pendingWebhooks = makeDueHint(); + const dueTasks = DueHint.make(); + const pendingWebhooks = DueHint.make(); // Every scheduled_tasks write that can change next_run_at, enabled or run state notifies. const notifyChanged = dueTasks.invalidate.pipe( Effect.andThen(PubSub.publish(changesPubSub, undefined)), @@ -1481,8 +1453,11 @@ export const layer = Layer.effect( const truncated = request.body.byteLength > WEBHOOK_DELIVERY_LOG_BODY_LIMIT; return { truncated, + // Streaming decode drops a character the byte limit split, instead of ending in U+FFFD. body: truncated - ? new TextDecoder().decode(request.body.subarray(0, WEBHOOK_DELIVERY_LOG_BODY_LIMIT)) + ? new TextDecoder().decode(request.body.subarray(0, WEBHOOK_DELIVERY_LOG_BODY_LIMIT), { + stream: true, + }) : request.bodyText, }; }; @@ -1495,6 +1470,35 @@ export const layer = Layer.effect( WHERE delivery_id = ${deliveryId}`.pipe(Effect.asVoid, Effect.mapError(dedupeError)); }; + // Each suppression bucket retains its own rows, never real run history. + const deliveryBucket = (outcome: string, error: string | undefined) => + outcome === "filtered" + ? "filtered" + : outcome === "duplicate" || + outcome === "coalesced" || + outcome === "rate_limited" || + error === PROMPT_TOO_LONG + ? "dedupe" + : "main"; + const pruneDeliveries = ( + taskId: string, + retentionBucket: ReturnType, + ) => { + const rowBucket = sql.literal(`CASE WHEN outcome = 'filtered' THEN 'filtered' + WHEN outcome IN ('duplicate', 'coalesced', 'rate_limited') OR error = 'The filled-in prompt is too long.' THEN 'dedupe' + ELSE 'main' END`); + return sql` + DELETE FROM scheduled_task_webhook_deliveries + WHERE task_id = ${taskId} AND ${rowBucket} = ${retentionBucket} + AND delivery_id NOT IN ( + SELECT delivery_id FROM scheduled_task_webhook_deliveries + WHERE task_id = ${taskId} AND ${rowBucket} = ${retentionBucket} + ORDER BY received_at DESC, rowid DESC + LIMIT ${retentionBucket === "main" ? WEBHOOK_DELIVERY_RETENTION : retentionBucket === "filtered" ? WEBHOOK_FILTERED_DELIVERY_RETENTION : WEBHOOK_DEDUPE_DELIVERY_RETENTION} + ) + `; + }; + const recordDelivery = (input: { readonly id: string; readonly taskId: ScheduledTaskId; @@ -1536,30 +1540,7 @@ export const layer = Layer.effect( rendered_prompt = excluded.rendered_prompt, error = excluded.error `; - // Each suppression bucket retains its own rows, never real run history. - const bucket = (outcome: string, error: string | undefined) => - outcome === "filtered" - ? "filtered" - : outcome === "duplicate" || - outcome === "coalesced" || - outcome === "rate_limited" || - error === PROMPT_TOO_LONG - ? "dedupe" - : "main"; - const retentionBucket = bucket(input.outcome, input.error); - const rowBucket = sql.literal(`CASE WHEN outcome = 'filtered' THEN 'filtered' - WHEN outcome IN ('duplicate', 'coalesced', 'rate_limited') OR error = 'The filled-in prompt is too long.' THEN 'dedupe' - ELSE 'main' END`); - yield* sql` - DELETE FROM scheduled_task_webhook_deliveries - WHERE task_id = ${input.taskId} AND ${rowBucket} = ${retentionBucket} - AND delivery_id NOT IN ( - SELECT delivery_id FROM scheduled_task_webhook_deliveries - WHERE task_id = ${input.taskId} AND ${rowBucket} = ${retentionBucket} - ORDER BY received_at DESC, rowid DESC - LIMIT ${retentionBucket === "main" ? WEBHOOK_DELIVERY_RETENTION : retentionBucket === "filtered" ? WEBHOOK_FILTERED_DELIVERY_RETENTION : WEBHOOK_DEDUPE_DELIVERY_RETENTION} - ) - `; + yield* pruneDeliveries(input.taskId, deliveryBucket(input.outcome, input.error)); }), ) .pipe( @@ -1570,11 +1551,21 @@ export const layer = Layer.effect( }; const markDeliveryFailed = (deliveryId: string, message: string) => - sql` - UPDATE scheduled_task_webhook_deliveries - SET outcome = 'dispatch_failed', error = ${message} - WHERE delivery_id = ${deliveryId} - `.pipe(Effect.ignore); + sql + .withTransaction( + Effect.gen(function* () { + const [updated] = yield* sql<{ task_id: string }>` + UPDATE scheduled_task_webhook_deliveries + SET outcome = 'dispatch_failed', error = ${message} + WHERE delivery_id = ${deliveryId} + RETURNING task_id + `; + // An oversized prompt moves the row into the suppression bucket. + if (updated !== undefined) + yield* pruneDeliveries(updated.task_id, deliveryBucket("dispatch_failed", message)); + }), + ) + .pipe(Effect.ignore); /** Sliding one-minute window for deliveries that can start work. */ const takeRateSlot = ( diff --git a/apps/server/src/scheduledTasks/ScheduledTaskService.webhook.test.ts b/apps/server/src/scheduledTasks/ScheduledTaskService.webhook.test.ts index 48d3024d4f938..cdd4e60e62d76 100644 --- a/apps/server/src/scheduledTasks/ScheduledTaskService.webhook.test.ts +++ b/apps/server/src/scheduledTasks/ScheduledTaskService.webhook.test.ts @@ -481,6 +481,33 @@ it.effect("keeps credential headers and query values out of the delivery log", ( ), ); +it.effect("drops a character split by the delivery log's body limit", () => + withService(({ service }) => + Effect.gen(function* () { + const { task } = yield* service.upsert(yield* webhookTaskInput({ enabled: false })); + const limit = 64 * 1024; + const euro = new TextEncoder().encode("€"); + const body = new Uint8Array(limit - 1 + euro.length).fill(0x61); + body.set(euro, limit - 1); + yield* service.triggerWebhook( + requestFor(task, { body, bodyText: new TextDecoder().decode(body) }), + ); + const [summary] = (yield* service.listWebhookDeliveries({ + id: task.id, + acceptAnyOutcome: true, + })).deliveries; + const { delivery } = yield* service.getWebhookDelivery({ + id: task.id, + deliveryId: summary!.id, + }); + assert.isTrue(delivery.bodyTruncated); + assert.equal(delivery.bodyBytes, body.byteLength); + assert.equal(delivery.body.slice(-3), "aaa"); + assert.equal(delivery.body.length, limit - 1); + }), + ), +); + const queuedDeliveryCases = [ { change: "paused", @@ -1320,6 +1347,40 @@ it.effect.each([119_000, 120_000])( }, ); +it.effect("oversized coalesced windows stay within the suppression bucket's limit", () => { + const workers = new Map>(); + return withService( + ({ service, sql, launches }) => + Effect.gen(function* () { + const { task } = yield* service.upsert( + yield* webhookTaskInput({ + prompt: `{{body.absent}}${" ".repeat(10_000)}{{body.text}}`, + schedule: { + type: "webhook", + dedupe: { coalesceKey: "{{body.group}}", coalesceWindowSeconds: 10 }, + }, + }), + ); + const bodyText = JSON.stringify({ group: "B", text: "x".repeat(120_000) }); + for (let i = 0; i < 21; i++) { + const accepted = yield* service.triggerWebhook( + requestFor(task, { bodyText, body: new TextEncoder().encode(bodyText) }), + ); + assert.equal(accepted._tag === "accepted" && accepted.outcome, "accepted"); + yield* TestClock.adjust("10 seconds"); + yield* drainPendingDispatch(workers); + } + assert.equal(yield* Queue.size(launches), 0); + const counts = yield* sql<{ + outcome: string; + count: number; + }>`SELECT outcome, COUNT(*) AS count FROM scheduled_task_webhook_deliveries GROUP BY outcome`; + assert.deepEqual(counts, [{ outcome: "dispatch_failed", count: 20 }]); + }), + { workers }, + ); +}); + it.effect.each([1, 11])( "keeps relay claims for %i coalesced updates after their window closes", (count) => { diff --git a/apps/server/src/scheduling/DueHint.ts b/apps/server/src/scheduling/DueHint.ts new file mode 100644 index 0000000000000..2df8af5f0c536 --- /dev/null +++ b/apps/server/src/scheduling/DueHint.ts @@ -0,0 +1,30 @@ +import * as Effect from "effect/Effect"; + +// Backstop for a write path that forgets to invalidate; the tick still runs every few seconds. +const DUE_HINT_MAX_AGE_MS = 10 * 60 * 1_000; + +/** + * Remembers when a poller next has work so idle scheduler ticks skip SQL. + * `invalidate` after every write that can move work earlier; a tick records + * only if no write happened since it read `version`. + */ +export const make = () => { + let version = 0; + let hint: { readonly dueAtMs: number; readonly checkedAtMs: number } | null = null; + return { + invalidate: Effect.sync(() => { + version++; + hint = null; + }), + version: Effect.sync(() => version), + isIdle: (nowMs: number) => + Effect.sync( + () => + hint !== null && hint.dueAtMs > nowMs && nowMs - hint.checkedAtMs < DUE_HINT_MAX_AGE_MS, + ), + record: (readVersion: number, dueAtMs: number, nowMs: number) => + Effect.sync(() => { + if (readVersion === version) hint = { dueAtMs, checkedAtMs: nowMs }; + }), + }; +}; diff --git a/apps/server/src/settingsSecrets.ts b/apps/server/src/settingsSecrets.ts index e4a35527dc39e..16450d180d759 100644 --- a/apps/server/src/settingsSecrets.ts +++ b/apps/server/src/settingsSecrets.ts @@ -6,6 +6,7 @@ import { type ServerSettings, SimlockDeviceHostConfig, UsageLimitSourceConfig, + isSecretReference, secretSettingsFields, } from "@t3tools/contracts"; import { acpRegistryClient } from "@t3tools/provider-acp-registry/client"; @@ -58,7 +59,7 @@ export function mapSettingsSecrets( ) => { const next = { ...fields }; for (const field of secretFields) { - if (!(field in next)) continue; + if (!(field in next) || isSecretReference(next[field])) continue; next[field] = writeSecretFieldValues( next[field], readSecretFieldValues(next[field]).map(([serverHost, value]) => { diff --git a/apps/server/src/sourceControl/ServerSourceControlHost.ts b/apps/server/src/sourceControl/ServerSourceControlHost.ts index 9208997f67fc0..0ac83bdfa9359 100644 --- a/apps/server/src/sourceControl/ServerSourceControlHost.ts +++ b/apps/server/src/sourceControl/ServerSourceControlHost.ts @@ -15,7 +15,7 @@ export const layer = Layer.effect( const git = yield* GitVcsDriver.GitVcsDriver; const vcsRegistry = yield* VcsDriverRegistry.VcsDriverRegistry; return SourceControlHost.SourceControlHost.of({ - // Coder: no `update`; see SourceControlHost. + // Coder: no `update` or `getWithSecrets`; see SourceControlHost. settings: { get: serverSettings.getSettings }, process: { run: process.run }, git: { diff --git a/apps/server/src/sourceControl/sourceControlHostSecrets.ts b/apps/server/src/sourceControl/sourceControlHostSecrets.ts index 46c96ca5b7f05..aaf6476b376dc 100644 --- a/apps/server/src/sourceControl/sourceControlHostSecrets.ts +++ b/apps/server/src/sourceControl/sourceControlHostSecrets.ts @@ -1,4 +1,4 @@ -import { secretSettingsFields } from "@t3tools/contracts"; +import { isSecretReference, secretSettingsFields } from "@t3tools/contracts"; import { sourceControlDefinitions } from "./sourceControlDefinitions.ts"; @@ -30,6 +30,7 @@ export function readSecretFieldValues( value: unknown, ): ReadonlyArray { if (typeof value === "string") return [[null, value]]; + if (isSecretReference(value)) return []; if (value === null || typeof value !== "object" || Array.isArray(value)) return []; return Object.entries(value).flatMap(([serverHost, entry]) => typeof entry === "string" ? [[serverHost.trim().toLowerCase(), entry] as const] : [], @@ -41,5 +42,9 @@ export function writeSecretFieldValues( values: ReadonlyArray, ): unknown { if (typeof original === "string") return values[0]?.[1] ?? ""; + if (isSecretReference(original)) return original; return Object.fromEntries(values.map(([serverHost, value]) => [serverHost ?? "", value])); } + +// Coder: upstream resolves secret provider references in host credentials here (#27746); secret +// providers are not carried, and only the Bitbucket and GitCafe drivers, also not carried, read them. diff --git a/apps/server/src/storageCleanup.test.ts b/apps/server/src/storageCleanup.test.ts index a595e8b174f65..a22223b5410c7 100644 --- a/apps/server/src/storageCleanup.test.ts +++ b/apps/server/src/storageCleanup.test.ts @@ -460,6 +460,22 @@ const cleanupFixture = ( setOpenSessionCwd: (cwd: string | null) => { openSessionCwd = cwd; }, + openTerminal: (paths: { cwd: string; worktreePath: string | null }) => + terminalListener?.({ + type: "upsert", + terminal: { + threadId: "thread-1", + terminalId: "terminal-1", + status: "running", + pid: 1, + exitCode: null, + exitSignal: null, + hasRunningSubprocess: false, + label: "Terminal", + updatedAt: "2026-06-10T12:00:00.000Z", + ...paths, + }, + }) ?? Effect.void, fullStatusCalls: () => fullStatusCalls, snapshotReads: () => snapshotReads, activate: (domainEvents: Stream.Stream) => { @@ -1604,6 +1620,54 @@ describe("orphan worktree reports", () => { }), ), ); + + it.live.each([ + ["win32", "cwd"], + ["win32", "worktreePath"], + ["linux", "cwd"], + ["linux", "worktreePath"], + ] as const)( + "matches a terminal at the worktree root with host casing rules (%s, %s)", + ([platform, field]) => + runCleanupTest( + Effect.gen(function* () { + const fixture = yield* cleanupFixture().pipe( + Effect.provideService(HostProcess.Platform, platform), + ); + const upper = fixture.worktree.toUpperCase(); + yield* fixture.openTerminal( + field === "cwd" + ? { cwd: upper, worktreePath: null } + : { cwd: fixture.repo, worktreePath: upper }, + ); + const report = yield* fixture.service.runNow; + expect(report.entries[0]).toMatchObject( + platform === "win32" + ? { outcome: "kept", reason: "Open terminal" } + : { outcome: "removed" }, + ); + }), + ), + ); + + it.live.each(["win32", "linux"] as const)( + "matches a provider session at the worktree root with host casing rules (%s)", + (platform) => + runCleanupTest( + Effect.gen(function* () { + const fixture = yield* cleanupFixture().pipe( + Effect.provideService(HostProcess.Platform, platform), + ); + fixture.setOpenSessionCwd(fixture.worktree.toUpperCase()); + const report = yield* fixture.service.runNow; + expect(report.entries[0]).toMatchObject( + platform === "win32" + ? { outcome: "kept", reason: "Provider session is still open" } + : { outcome: "removed" }, + ); + }), + ), + ); }); describe("storage cleanup reports and local file policies", () => { diff --git a/apps/server/src/storageCleanup.ts b/apps/server/src/storageCleanup.ts index 17d543ca4c675..40ee347343095 100644 --- a/apps/server/src/storageCleanup.ts +++ b/apps/server/src/storageCleanup.ts @@ -248,8 +248,8 @@ const make = Effect.gen(function* () { const cwd = path.resolve(terminal.cwd); return ( (terminal.worktreePath !== null && - path.resolve(terminal.worktreePath) === worktreePath) || - cwd === worktreePath || + pathKey(terminal.worktreePath) === pathKey(worktreePath)) || + pathKey(cwd) === pathKey(worktreePath) || inside(worktreePath, cwd) ); }); @@ -987,7 +987,7 @@ const make = Effect.gen(function* () { for (const session of sessions) { const cwd = path.resolve(session.cwd); if ( - (cwd === worktreePath || inside(worktreePath, cwd)) && + (pathKey(cwd) === pathKey(worktreePath) || inside(worktreePath, cwd)) && (yield* providerSessions.isLive(session.id)) ) return waitForStop("provider session is still open"); diff --git a/apps/server/src/terminal/Manager.test.ts b/apps/server/src/terminal/Manager.test.ts index 4c82f615f19c8..0c0073b97563a 100644 --- a/apps/server/src/terminal/Manager.test.ts +++ b/apps/server/src/terminal/Manager.test.ts @@ -7,6 +7,7 @@ import { type TerminalOpenInput, ProviderDriverKind, ProviderInstanceId, + ProjectId, TerminalProviderInstanceNotFoundError, } from "@t3tools/contracts"; import * as HostProcess from "@t3tools/shared/HostProcess"; @@ -30,8 +31,11 @@ import * as SynchronizedRef from "effect/SynchronizedRef"; import * as TestClock from "effect/testing/TestClock"; import { expect, vi } from "vite-plus/test"; +import * as ProjectionStore from "../orchestration-v2/ProjectionStore.ts"; import * as ProcessRunner from "../processRunner.ts"; import * as ServerSettings from "../serverSettings.ts"; +import * as ProjectService from "../project/ProjectService.ts"; +import * as ProjectSetupScriptRunner from "../project/ProjectSetupScriptRunner.ts"; import * as TerminalManager from "./Manager.ts"; import { terminalOutputStream } from "./OutputProtocol.ts"; import * as PtyAdapter from "@t3tools/shared/PtyAdapter"; @@ -1129,6 +1133,92 @@ it.layer( }), ); + it.effect("releases delivered output while the process queue stays busy", () => + Effect.gen(function* () { + // Observe retained payloads, since delivery and bounded history alone cannot detect a leak. + let readState: Effect.Effect = Effect.void; + const makeRef = vi.mocked(SynchronizedRef.make).getMockImplementation()!; + using _capture = vi.spyOn(SynchronizedRef, "make").mockImplementation((value: A) => + makeRef(value).pipe( + Effect.tap((ref) => + Effect.sync(() => { + if (typeof value === "object" && value !== null && "sessions" in value) { + readState = SynchronizedRef.get(ref); + } + }), + ), + ), + ); + const { manager, ptyAdapter } = yield* createManager(5, { historyByteLimit: 1024 }); + yield* manager.open(openInput()); + const process = ptyAdapter.processes[0]!; + const entered = yield* Deferred.make(); + const release = yield* Deferred.make(); + const measured = yield* Deferred.make(); + const finish = yield* Deferred.make(); + const drained = yield* Deferred.make(); + yield* Effect.addFinalizer(() => Deferred.succeed(finish, undefined)); + yield* Effect.addFinalizer(() => Deferred.succeed(release, undefined)); + const chunkBytes = 64 * 1024; + const chunkCount = 256; + let delivered = 0; + const unsubscribe = yield* manager.subscribe((event) => + Effect.gen(function* () { + if (event.type !== "output") return; + delivered += 1; + expect(event.data).toBe( + delivered === 1 + ? "start" + : (delivered === 2 + ? "queued-a" + : delivered === 3 + ? "queued-b" + : `${delivered - 3}` + ).padEnd(chunkBytes, "x"), + ); + if (delivered === 1) { + yield* Deferred.succeed(entered, undefined); + yield* Deferred.await(release); + } + if (delivered === chunkCount) { + yield* Deferred.succeed(measured, undefined); + yield* Deferred.await(finish); + } else if (delivered < chunkCount) { + process.emitData(`${delivered}`.padEnd(chunkBytes, "x")); + } else if (delivered === chunkCount + 2) { + yield* Deferred.succeed(drained, undefined); + } + }), + ); + yield* Effect.addFinalizer(() => Effect.sync(unsubscribe)); + process.emitData("start"); + yield* Deferred.await(entered); + process.emitData("queued-a".padEnd(chunkBytes, "x")); + process.emitData("queued-b".padEnd(chunkBytes, "x")); + yield* Deferred.succeed(release, undefined); + yield* Deferred.await(measured); + + const state = (yield* readState) as { + sessions: ReadonlyMap< + string, + { pendingProcessEvents: ReadonlyArray<{ data?: string } | undefined> } + >; + }; + const retained = [...state.sessions.values()].flatMap( + (session) => session.pendingProcessEvents, + ); + const retainedBytes = retained.reduce( + (bytes, event) => bytes + (event?.data?.length ?? 0), + 0, + ); + expect(delivered).toBe(chunkCount); + expect(retainedBytes).toBeLessThanOrEqual(2 * chunkBytes); + expect(retained.length).toBeLessThanOrEqual(4); + yield* Deferred.succeed(finish, undefined); + yield* Deferred.await(drained); + }), + ); + it.effect("retains plain Unicode and control characters around a split query", () => Effect.gen(function* () { const { manager, ptyAdapter } = yield* createManager(); @@ -1333,7 +1423,7 @@ it.layer( it.effect("keeps reading and deleting history at an encoded path that already fit", () => Effect.gen(function* () { - const threadId = "x".repeat(120); + const threadId = "x".repeat(48); const { manager, logsDir } = yield* createManager(); const fs = yield* FileSystem.FileSystem; const logPath = yield* historyLogPath(logsDir, threadId); @@ -1351,6 +1441,180 @@ it.layer( }), ); + it.effect.each([ + { + label: "delegated setup", + threadId: + "thread:delegated-task:command%3Amcp%3A00000000-0000-4000-8000-000000000000%3Adelegate-task%3Aexample-r4-workspace-prepare-engine3", + terminalId: "setup-setup-worktree-2", + }, + { label: "long thread", threadId: "t".repeat(400), terminalId: DEFAULT_TERMINAL_ID }, + { label: "Unicode thread", threadId: "项目😀".repeat(60), terminalId: "setup" }, + { label: "long terminal", threadId: "short", terminalId: "t".repeat(128) }, + { label: "142-byte boundary", threadId: "t".repeat(48), terminalId: "s".repeat(48) }, + { label: "144-byte boundary", threadId: "t".repeat(49), terminalId: "s".repeat(48) }, + ])("persists $label history with a 143-byte filename limit", ({ threadId, terminalId }) => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const check = (filePath: string) => + Buffer.byteLength(path.basename(filePath)) <= 143 + ? Effect.void + : Effect.fail( + PlatformError.systemError({ + _tag: "Unknown", + module: "FileSystem", + method: "access", + pathOrDescriptor: filePath, + cause: { code: "ENAMETOOLONG" }, + }), + ); + const limited = FileSystem.FileSystem.of({ + ...fs, + exists: (filePath) => Effect.andThen(check(filePath), fs.exists(filePath)), + open: (filePath, options) => Effect.andThen(check(filePath), fs.open(filePath, options)), + writeFileString: (filePath, data, options) => + Effect.andThen(check(filePath), fs.writeFileString(filePath, data, options)), + remove: (filePath, options) => + Effect.andThen(check(filePath), fs.remove(filePath, options)), + }); + const { manager, ptyAdapter, logsDir } = yield* createManager().pipe( + Effect.provideService(FileSystem.FileSystem, limited), + ); + yield* manager.open(openInput({ threadId, terminalId })); + ptyAdapter.processes[0]!.emitData("setup output\n"); + yield* manager.close({ threadId, terminalId }); + const names = yield* fs.readDirectory(logsDir); + expect(names).toHaveLength(1); + expect(Buffer.byteLength(names[0]!)).toBeLessThanOrEqual(143); + expect((yield* manager.open(openInput({ threadId, terminalId }))).history).toBe( + "setup output\n", + ); + const otherThread = `${threadId}-other`; + expect((yield* manager.open(openInput({ threadId: otherThread, terminalId }))).history).toBe( + "", + ); + ptyAdapter.processes[2]!.emitData("other output\n"); + yield* manager.close({ threadId: otherThread, terminalId }); + expect(yield* fs.readDirectory(logsDir)).toHaveLength(2); + yield* manager.close({ threadId, terminalId, deleteHistory: true }); + expect(yield* fs.readDirectory(logsDir)).toHaveLength(1); + yield* manager.close({ threadId: otherThread, deleteHistory: true }); + expect(yield* fs.readDirectory(logsDir)).toEqual([]); + }), + ); + + it.effect.each([ + { source: "encoded", terminalId: DEFAULT_TERMINAL_ID }, + { source: "encoded", terminalId: "setup-setup-worktree-2" }, + { source: "sanitized", terminalId: DEFAULT_TERMINAL_ID }, + ])("migrates oversized $source history for $terminalId", ({ source, terminalId }) => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const { manager, logsDir } = yield* createManager(); + const threadId = "m".repeat(100); + const oldPath = + source === "encoded" + ? path.join( + logsDir, + `terminal_${Base64Url.encode(threadId)}${terminalId === DEFAULT_TERMINAL_ID ? "" : `_${Base64Url.encode(terminalId)}`}.log`, + ) + : path.join(logsDir, `${threadId}.log`); + yield* fs.writeFileString(oldPath, "old output\n"); + expect((yield* manager.open(openInput({ threadId, terminalId }))).history).toBe( + "old output\n", + ); + const names = yield* fs.readDirectory(logsDir); + expect(names).toHaveLength(1); + expect(Buffer.byteLength(names[0]!)).toBeLessThanOrEqual(143); + expect(yield* fs.exists(oldPath)).toBe(false); + expect(yield* fs.readFileString(path.join(logsDir, names[0]!))).toBe("old output\n"); + yield* manager.close({ threadId, terminalId, deleteHistory: true }); + expect(yield* fs.readDirectory(logsDir)).toEqual([]); + }), + ); + + it.effect.each(["terminal", "thread"] as const)( + "deletes compact and older encoded history when closing a %s", + (scope) => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const { manager, logsDir } = yield* createManager(); + const threadId = "d".repeat(100); + const terminalId = "setup-setup-worktree-2"; + yield* manager.open(openInput({ threadId, terminalId })); + yield* manager.close({ threadId, terminalId }); + const oldPath = path.join( + logsDir, + `terminal_${Base64Url.encode(threadId)}_${Base64Url.encode(terminalId)}.log`, + ); + yield* fs.writeFileString(oldPath, "stale copy\n"); + yield* fs.writeFileString(path.join(logsDir, "unrelated.log"), "keep\n"); + if (scope === "terminal") { + yield* manager.close({ threadId, terminalId, deleteHistory: true }); + } else { + yield* manager.close({ threadId, deleteHistory: true }); + } + expect(yield* fs.readDirectory(logsDir)).toEqual(["unrelated.log"]); + }), + ); + + it.effect("prefers bounded history over a stale encoded copy", () => + Effect.gen(function* () { + const { manager, ptyAdapter, logsDir } = yield* createManager(); + const fs = yield* FileSystem.FileSystem; + const threadId = "p".repeat(100); + yield* manager.open(openInput({ threadId })); + ptyAdapter.processes[0]!.emitData("current\n"); + yield* manager.close({ threadId }); + const oldPath = yield* historyLogPath(logsDir, threadId); + yield* fs.writeFileString(oldPath, "stale\n"); + + expect((yield* manager.open(openInput({ threadId }))).history).toBe("current\n"); + yield* manager.close({ threadId, terminalId: DEFAULT_TERMINAL_ID, deleteHistory: true }); + expect(yield* fs.readDirectory(logsDir)).toEqual([]); + }), + ); + + it.effect("retains encoded history if migration cannot write the bounded file", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + let failNextWrite = true; + const limited = FileSystem.FileSystem.of({ + ...fs, + writeFileString: (filePath, data, options) => { + if (!failNextWrite) return fs.writeFileString(filePath, data, options); + failNextWrite = false; + return fs.writeFileString(filePath, "partial").pipe( + Effect.andThen( + Effect.fail( + PlatformError.systemError({ + _tag: "Unknown", + module: "FileSystem", + method: "writeFileString", + cause: { code: "ENOSPC" }, + }), + ), + ), + ); + }, + }); + const { manager, logsDir } = yield* createManager().pipe( + Effect.provideService(FileSystem.FileSystem, limited), + ); + const threadId = "f".repeat(100); + const oldPath = yield* historyLogPath(logsDir, threadId); + yield* fs.writeFileString(oldPath, "preserve me\n"); + + expect(Exit.isFailure(yield* Effect.exit(manager.open(openInput({ threadId }))))).toBe(true); + expect(yield* fs.readFileString(oldPath)).toBe("preserve me\n"); + expect(yield* fs.readDirectory(logsDir)).toHaveLength(1); + expect((yield* manager.open(openInput({ threadId }))).history).toBe("preserve me\n"); + }), + ); + it.effect("migrates legacy transcript filenames to terminal-scoped history path on open", () => Effect.gen(function* () { const { manager, logsDir } = yield* createManager(); @@ -1405,6 +1669,61 @@ it.layer( }), ); + it.effect( + "writes completion syntax for the fallback shell instead of the configured fish shell", + () => + Effect.gen(function* () { + const env = { SHELL: "/missing/fish" }; + const { manager, ptyAdapter, baseDir } = yield* createManager(5, { env }).pipe( + Effect.provide(layerWithHostPlatform("linux")), + ); + ptyAdapter.spawnFailures.push(new Error("spawn fish ENOENT")); + const written = yield* Deferred.make(); + const terminals = TerminalManager.TerminalManager.of({ + ...manager, + write: (input) => + manager.write(input).pipe(Effect.tap(() => Deferred.succeed(written, undefined))), + }); + const runner = yield* ProjectSetupScriptRunner.make.pipe( + Effect.provide( + Layer.mergeAll( + Layer.succeed(TerminalManager.TerminalManager, terminals), + Layer.mock(ProjectService.ProjectService)({}), + Layer.mock(ProjectionStore.ProjectionStoreV2)({ + getThreadShell: () => Effect.succeed(null), + }), + ServerSettings.layerTest(), + Layer.succeed(HostProcess.Platform, "linux"), + Layer.succeed(HostProcess.Environment, env), + ), + ), + ); + yield* runner.runForThread({ + threadId: "thread-setup-fallback", + worktreePath: baseDir, + project: { + id: ProjectId.make("project-setup-fallback"), + workspaceRoot: baseDir, + scripts: [ + { + id: "setup", + name: "Setup", + command: "echo setup-ready", + icon: "play", + runOnWorktreeCreate: true, + }, + ], + }, + observeCompletion: {}, + }); + const process = ptyAdapter.processes[0]!; + process.emitData("\x1b[?2004h"); + yield* Deferred.await(written); + const command = process.writes[0]!; + expect(command).toContain('"$?"'); + }), + ); + it.effect("falls back to built-in PowerShell by absolute path on Windows", () => Effect.gen(function* () { const ptyAdapter = new FakePtyAdapter(); @@ -1434,6 +1753,62 @@ it.layer( }), ); + it.effect("closes an idle cmd fallback before rejecting its completion wrapper", () => + Effect.gen(function* () { + const { manager, ptyAdapter, baseDir } = yield* createManager(5, { + env: { SystemRoot: "C:\\Windows" }, + subprocessInspector: () => + Effect.succeed({ + hasRunningSubprocess: false, + childCommand: null, + processIds: [], + }), + }).pipe(Effect.provide(layerWithHostPlatform("win32"))); + ptyAdapter.spawnFailures.push( + new Error("spawn pwsh ENOENT"), + new Error("spawn WindowsPowerShell ENOENT"), + new Error("spawn powershell ENOENT"), + ); + const runner = yield* ProjectSetupScriptRunner.make.pipe( + Effect.provide( + Layer.mergeAll( + Layer.succeed(TerminalManager.TerminalManager, manager), + Layer.mock(ProjectService.ProjectService)({}), + Layer.mock(ProjectionStore.ProjectionStoreV2)({ + getThreadShell: () => Effect.succeed(null), + }), + ServerSettings.layerTest(), + ), + ), + ); + const error = yield* runner + .runForThread({ + threadId: "thread-cmd-fallback", + worktreePath: baseDir, + project: { + id: ProjectId.make("project-cmd-fallback"), + workspaceRoot: baseDir, + scripts: [ + { + id: "setup", + name: "Setup", + command: "echo setup-ready", + icon: "play", + runOnWorktreeCreate: true, + }, + ], + }, + observeCompletion: {}, + }) + .pipe(Effect.flip); + + expect(error._tag).toBe("ProjectSetupScriptUnsupportedShellError"); + expect(ptyAdapter.spawnInputs.at(-1)?.shell).toBe("C:\\Windows\\System32\\cmd.exe"); + expect(ptyAdapter.processes[0]?.writes).toEqual([]); + expect(ptyAdapter.processes[0]?.killed).toBe(true); + }), + ); + it.effect("turns off fish terminal queries unless the user chose query-term", () => Effect.gen(function* () { for (const [platform, env, runtimeEnv, expected] of [ diff --git a/apps/server/src/terminal/Manager.ts b/apps/server/src/terminal/Manager.ts index ac8e43378cb68..39871e50c6965 100644 --- a/apps/server/src/terminal/Manager.ts +++ b/apps/server/src/terminal/Manager.ts @@ -87,6 +87,8 @@ export { const DEFAULT_HISTORY_LINE_LIMIT = 5_000; const DEFAULT_HISTORY_BYTE_LIMIT = 8 * 1024 * 1024; +// eCryptfs permits 143-byte names. +const MAX_HISTORY_FILENAME_BYTES = 143; const MAX_HISTORY_CHUNK_LENGTH = 16 * 1024; // Coder: attach resumes from a bounded per-terminal replay window so a reconnect over the // workspace connection does not resend the whole history snapshot. @@ -168,6 +170,8 @@ export class TerminalManager extends Context.Service< input: TerminalOpenInput, ) => Effect.Effect; + readonly getShell: (input: TerminalObserveInput) => Effect.Effect; + /** * Attached viewers share one PTY sized to the smallest of them, so a narrow client * still sees the whole TUI. Streams and resizes with the same `viewerId` (one client @@ -281,7 +285,7 @@ interface TerminalSessionState { pid: number | null; history: BoundedTerminalHistory; pendingHistoryControlSequence: string; - pendingProcessEvents: Array; + pendingProcessEvents: Array; pendingProcessEventIndex: number; processEventDrainRunning: boolean; exitCode: number | null; @@ -294,6 +298,7 @@ interface TerminalSessionState { cols: number; rows: number; process: PtyAdapter.PtyProcess | null; + shell: string | null; unsubscribeData: (() => void) | null; unsubscribeExit: (() => void) | null; hasRunningSubprocess: boolean; @@ -1312,8 +1317,6 @@ function legacySafeThreadId(threadId: string): string { return threadId.replace(/[^a-zA-Z0-9._-]/g, "_"); } -const MAX_FILE_NAME_BYTES = 255; - function encodedHistoryThreadPart(threadId: string): string { return `terminal_${Base64Url.encode(threadId)}`; } @@ -1724,28 +1727,46 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func } }); - // Nested subagent thread ids can encode past the filename limit. Only names that would not fit - // switch to hashed ids, so every history file that already exists keeps its path. - const historyPath = (threadId: string, terminalId: string) => { - const isDefault = terminalId === DEFAULT_TERMINAL_ID; - const encodedName = joinHistoryFileName( - encodedHistoryThreadPart(threadId), - isDefault ? null : Base64Url.encode(terminalId), + const encodedHistoryPath = (threadId: string, terminalId: string) => + path.join( + logsDir, + joinHistoryFileName( + encodedHistoryThreadPart(threadId), + terminalId === DEFAULT_TERMINAL_ID ? null : Base64Url.encode(terminalId), + ), ); - if (encodedName.length <= MAX_FILE_NAME_BYTES) { - return path.join(logsDir, encodedName); - } + + const historyPath = (threadId: string, terminalId: string) => { + const encoded = encodedHistoryPath(threadId, terminalId); + if (path.basename(encoded).length <= MAX_HISTORY_FILENAME_BYTES) return encoded; return path.join( logsDir, - joinHistoryFileName(hashedHistoryThreadPart(threadId), isDefault ? null : hashId(terminalId)), + joinHistoryFileName( + hashedHistoryThreadPart(threadId), + terminalId === DEFAULT_TERMINAL_ID ? null : hashId(terminalId), + ), ); }; - const legacyHistoryName = (threadId: string) => `${legacySafeThreadId(threadId)}.log`; - const legacyHistoryPath = (threadId: string) => path.join(logsDir, legacyHistoryName(threadId)); - // Ids this long could never have been written under the legacy name. - const hasLegacyHistoryPath = (threadId: string) => - Buffer.byteLength(legacyHistoryName(threadId)) <= MAX_FILE_NAME_BYTES; + const legacyHistoryPath = (threadId: string) => + path.join(logsDir, `${legacySafeThreadId(threadId)}.log`); + + const historyPaths = (threadId: string, terminalId: string) => [ + ...new Set([ + historyPath(threadId, terminalId), + encodedHistoryPath(threadId, terminalId), + ...(terminalId === DEFAULT_TERMINAL_ID ? [legacyHistoryPath(threadId)] : []), + ]), + ]; + + const historyExists = (filePath: string) => { + const name = path.basename(filePath); + // An oversized old path can itself fail on access. Directory entries also + // preserve migration of such histories on filesystems with a larger limit. + return name.length <= MAX_HISTORY_FILENAME_BYTES + ? fileSystem.exists(filePath) + : fileSystem.readDirectory(logsDir).pipe(Effect.map((entries) => entries.includes(name))); + }; const readManagerState = SynchronizedRef.get(managerStateRef); @@ -2074,105 +2095,86 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func terminalId: string, ) { const nextPath = historyPath(threadId, terminalId); - if ( - yield* fileSystem - .exists(nextPath) - .pipe( + for (const sourcePath of historyPaths(threadId, terminalId)) { + const migrating = sourcePath !== nextPath; + const operation = migrating ? "migrate" : "read"; + if ( + !(yield* historyExists(sourcePath).pipe( Effect.mapError( - (cause) => new TerminalHistoryError({ operation: "read", threadId, terminalId, cause }), + (cause) => new TerminalHistoryError({ operation, threadId, terminalId, cause }), ), - ) - ) { - const { history: raw, truncated } = yield* readHistoryTail(nextPath).pipe( + )) + ) + continue; + const { history: raw, truncated } = yield* readHistoryTail(sourcePath).pipe( Effect.scoped, Effect.mapError( - (cause) => new TerminalHistoryError({ operation: "read", threadId, terminalId, cause }), + (cause) => new TerminalHistoryError({ operation, threadId, terminalId, cause }), ), ); const history = new BoundedTerminalHistory(historyLineLimit, raw, historyByteLimit); const capped = history.value(); - if (truncated || capped !== raw) { - yield* fileSystem - .writeFileString(nextPath, capped) - .pipe( - Effect.mapError( - (cause) => - new TerminalHistoryError({ operation: "truncate", threadId, terminalId, cause }), - ), - ); - } - return history; - } - - if (terminalId !== DEFAULT_TERMINAL_ID || !hasLegacyHistoryPath(threadId)) { - return new BoundedTerminalHistory(historyLineLimit, "", historyByteLimit); - } - - const legacyPath = legacyHistoryPath(threadId); - if ( - !(yield* fileSystem - .exists(legacyPath) - .pipe( + if (migrating || truncated || capped !== raw) { + const write = migrating + ? Effect.gen(function* () { + const temporaryPath = yield* fileSystem.makeTempFileScoped({ + directory: logsDir, + prefix: ".history-", + }); + yield* fileSystem.writeFileString(temporaryPath, capped); + yield* fileSystem.rename(temporaryPath, nextPath); + }).pipe(Effect.scoped) + : fileSystem.writeFileString(nextPath, capped); + yield* write.pipe( Effect.mapError( (cause) => - new TerminalHistoryError({ operation: "migrate", threadId, terminalId, cause }), + new TerminalHistoryError({ + operation: migrating ? "migrate" : "truncate", + threadId, + terminalId, + cause, + }), ), - )) - ) { - return new BoundedTerminalHistory(historyLineLimit, "", historyByteLimit); + ); + } + if (migrating) { + yield* fileSystem.remove(sourcePath, { force: true }).pipe( + Effect.catch((error) => + Effect.logWarning("failed to remove legacy terminal history", { + threadId, + terminalId, + error, + }), + ), + ); + } + return history; } - - const { history: raw } = yield* readHistoryTail(legacyPath).pipe( - Effect.scoped, - Effect.mapError( - (cause) => new TerminalHistoryError({ operation: "migrate", threadId, terminalId, cause }), - ), - ); - const history = new BoundedTerminalHistory(historyLineLimit, raw, historyByteLimit); - const capped = history.value(); - yield* fileSystem - .writeFileString(nextPath, capped) - .pipe( - Effect.mapError( - (cause) => - new TerminalHistoryError({ operation: "migrate", threadId, terminalId, cause }), - ), - ); - yield* fileSystem.remove(legacyPath, { force: true }).pipe( - Effect.catch((cleanupError) => - Effect.logWarning("failed to remove legacy terminal history", { - threadId, - error: cleanupError, - }), - ), - ); - return history; + return new BoundedTerminalHistory(historyLineLimit, "", historyByteLimit); }); const deleteHistory = Effect.fn("terminal.deleteHistory")(function* ( threadId: string, terminalId: string, ) { - yield* fileSystem.remove(historyPath(threadId, terminalId), { force: true }).pipe( - Effect.catch((error) => - Effect.logWarning("failed to delete terminal history", { - threadId, - terminalId, - error, - }), - ), - ); - if (terminalId === DEFAULT_TERMINAL_ID && hasLegacyHistoryPath(threadId)) { - yield* fileSystem.remove(legacyHistoryPath(threadId), { force: true }).pipe( - Effect.catch((error) => - Effect.logWarning("failed to delete terminal history", { - threadId, - terminalId, - error, - }), + yield* Effect.forEach( + historyPaths(threadId, terminalId), + (filePath) => + Effect.gen(function* () { + if (yield* historyExists(filePath)) { + yield* fileSystem.remove(filePath, { force: true }); + } + }).pipe( + Effect.catch((error) => + Effect.logWarning("failed to delete terminal history", { + threadId, + terminalId, + error, + }), + ), ), - ); - } + { discard: true }, + ); }); const deleteAllHistoryForThread = Effect.fn("terminal.deleteAllHistoryForThread")(function* ( @@ -2293,9 +2295,12 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func return { type: "idle" } as const; } + session.pendingProcessEvents[session.pendingProcessEventIndex] = undefined; session.pendingProcessEventIndex += 1; - if (session.pendingProcessEventIndex >= session.pendingProcessEvents.length) { - session.pendingProcessEvents = []; + if (session.pendingProcessEventIndex * 2 >= session.pendingProcessEvents.length) { + session.pendingProcessEvents = session.pendingProcessEvents.slice( + session.pendingProcessEventIndex, + ); session.pendingProcessEventIndex = 0; } @@ -2423,7 +2428,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func index = 0, lastError: PtyAdapter.PtySpawnError | null = null, ): Effect.fn.Return< - { process: PtyAdapter.PtyProcess; shellLabel: string }, + { process: PtyAdapter.PtyProcess; shell: string; shellLabel: string }, PtyAdapter.PtySpawnError > { if (index >= shellCandidates.length) { @@ -2466,6 +2471,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func if (attempt._tag === "Success") { return { process: attempt.success, + shell: candidate.shell, shellLabel: formatShellCandidate(candidate), }; } @@ -2494,6 +2500,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func const startingAt = yield* nowIso; yield* modifyManagerState((state) => { session.status = "starting"; + session.shell = null; session.cwd = input.cwd; session.worktreePath = input.worktreePath ?? null; session.cols = input.cols; @@ -2536,6 +2543,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func }; yield* modifyManagerState((state) => { session.process = ptyProcess; + session.shell = spawnResult.shell; session.pid = processPid; session.status = "running"; // onExit may replay an exit immediately; accept it before subscribing. @@ -2911,6 +2919,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func cols, rows, process: null, + shell: null, unsubscribeData: null, unsubscribeExit: null, hasRunningSubprocess: false, @@ -3008,6 +3017,14 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func resolveLaunchInputEnvironment(input).pipe(Effect.flatMap(openLocked)), ); + const getShell = Effect.fn("terminal.getShell")(function* (input: TerminalObserveInput) { + const session = yield* requireSession(input.threadId, input.terminalId); + if (session.status !== "running" || session.shell === null) { + return yield* new TerminalNotRunningError(input); + } + return session.shell; + }); + const openOrAttachForStream = Effect.fnUntraced(function* (requestedInput: TerminalAttachInput) { const terminalId = requestedInput.terminalId; const input = withSharedGrid(toSessionKey(requestedInput.threadId, terminalId), requestedInput); @@ -3540,6 +3557,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func return TerminalManager.of({ open, + getShell, attachStream, attachOutputStream, observeStream, diff --git a/apps/server/src/vcs/GitVcsDriver.test.ts b/apps/server/src/vcs/GitVcsDriver.test.ts index 4b134765e1c7a..06f17625c2c79 100644 --- a/apps/server/src/vcs/GitVcsDriver.test.ts +++ b/apps/server/src/vcs/GitVcsDriver.test.ts @@ -915,6 +915,126 @@ it.effect.each(["missing", "invalid"] as const)( }).pipe(Effect.scoped, Effect.provide(layerGitContract)), ); +const captureRecordingGitCommands = Effect.fn("test.captureRecordingGitCommands")(function* ( + cwd: string, + checkpointRef: CheckpointRef, +) { + const liveProcess = yield* VcsProcess.VcsProcess; + const commands: Array = []; + const driver = yield* GitVcsDriver.makeVcsDriverShape().pipe( + Effect.provideService(VcsProcess.VcsProcess, { + run: (input) => { + let index = 0; + while (input.args[index] === "-C" || input.args[index] === "-c") index += 2; + commands.push(input.args[index]); + return liveProcess.run(input); + }, + }), + ); + yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef }); + return commands; +}); + +it.effect.each(["committed", "unborn"] as const)( + "checkpoint capture reads the repository with one rev-parse (HEAD=%s)", + (head) => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const driver = yield* GitVcsDriver.makeVcsDriverShape(); + const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-processes-" }); + const git = (args: ReadonlyArray) => + driver.execute({ operation: "checkpoint-test", cwd, args }); + const checkpointRef = CheckpointRef.make("refs/t3/checkpoints/test"); + if (head === "committed") { + yield* makeCheckpointFixture(driver, cwd); + } else { + yield* git(["init"]); + yield* fileSystem.writeFileString(path.join(cwd, "file.txt"), "unstaged\n"); + } + + const commands = yield* captureRecordingGitCommands(cwd, checkpointRef); + + assert.deepStrictEqual(commands, [ + "rev-parse", + "config", + ...(head === "committed" ? ["read-tree", "ls-files"] : []), + "check-ignore", + "add", + "write-tree", + "commit-tree", + "update-ref", + ]); + assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "unstaged\n"); + const [, ...parents] = (yield* git([ + "rev-list", + "--parents", + "-n", + "1", + checkpointRef, + ])).stdout + .trim() + .split(" "); + assert.deepStrictEqual( + parents, + head === "committed" ? [(yield* git(["rev-parse", "HEAD"])).stdout.trim()] : [], + ); + }).pipe(Effect.scoped, Effect.provide(layerGitContract)), +); + +it.effect("checkpoint capture outside a repository fails with the common dir lookup error", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-no-repo-" }); + + const error = yield* captureRecordingGitCommands( + cwd, + CheckpointRef.make("refs/t3/checkpoints/test"), + ).pipe(Effect.flip); + + assert.instanceOf(error, VcsProcessExitError); + assert.strictEqual(error.operation, "GitVcsDriver.checkpoints.resolveGitCommonDir"); + assert.strictEqual(error.exitCode, 128); + }).pipe(Effect.scoped, Effect.provide(layerGitContract)), +); + +it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")( + "checkpoint capture handles a repository path containing a newline", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const driver = yield* GitVcsDriver.makeVcsDriverShape(); + const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-new\nline-" }); + const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd); + const originalIndex = yield* fileSystem.readFile(path.join(cwd, ".git", "index")); + + const commands = yield* captureRecordingGitCommands(cwd, checkpointRef); + + assert.deepStrictEqual(commands, [ + "rev-parse", + "rev-parse", + "config", + "rev-parse", + "read-tree", + "ls-files", + "check-ignore", + "add", + "write-tree", + "commit-tree", + "update-ref", + ]); + assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "unstaged\n"); + assert.deepEqual(yield* fileSystem.readFile(path.join(cwd, ".git", "index")), originalIndex); + assert.deepStrictEqual( + (yield* fileSystem.readDirectory(path.join(cwd, ".git"))).filter((name) => + name.startsWith("t3-checkpoint-index-"), + ), + [], + ); + }).pipe(Effect.scoped, Effect.provide(layerGitContract)), +); + it.effect("restores empty checkpoints without changing paths outside the workspace", () => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; diff --git a/apps/server/src/vcs/GitVcsDriver.ts b/apps/server/src/vcs/GitVcsDriver.ts index 6b46c2a450e78..d9a98dd871c8b 100644 --- a/apps/server/src/vcs/GitVcsDriver.ts +++ b/apps/server/src/vcs/GitVcsDriver.ts @@ -846,6 +846,45 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* ( return path.resolve(physicalCwd, gitCommonDir); }); + // --verify prints HEAD after the paths; --quiet leaves only paths for an unborn HEAD. + const resolveCaptureRepository = Effect.fn("GitVcsDriver.resolveCaptureRepository")(function* ( + cwd: string, + ) { + const result = yield* execute({ + operation: VcsProcess.CHECKPOINT_CAPTURE_OPERATION, + cwd, + args: [ + "rev-parse", + "--path-format=absolute", + "--git-common-dir", + "--git-path", + "index", + "--verify", + "--quiet", + "HEAD", + ], + allowNonZeroExit: true, + }); + const headExists = result.exitCode === 0; + const headChecked = headExists || result.exitCode === 1; + const lines = result.stdout.split("\n"); + const [gitCommonDir = "", indexPath = ""] = lines; + if (headChecked && lines.length === (headExists ? 4 : 3) && lines.at(-1) === "") { + return { + gitCommonDir: gitCommonDir.trim(), + indexPath: indexPath.trim(), + headExists, + }; + } + // Git before 2.31 echoes --path-format, and a path containing a newline spans lines. + // Separate commands read the paths as before and report a failure the same way. + return { + gitCommonDir: yield* resolveGitCommonDir(cwd), + indexPath: undefined, + headExists: headChecked ? headExists : yield* hasHeadCommit(cwd), + }; + }); + // Git defaults and macOS writeout-only can leave empty refs after crashes; flush before publication. // Set fsync first: Git 2.36 ignores unknown batch without resetting the method. // Batch fully flushes refs and equals fsync on macOS HFS+/APFS and Windows NTFS/ReFS. @@ -878,7 +917,7 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* ( "-c", "splitIndex.sharedIndexExpire=never", ]; - const gitCommonDir = yield* resolveGitCommonDir(input.cwd); + const { gitCommonDir, indexPath, headExists } = yield* resolveCaptureRepository(input.cwd); const indexId = yield* crypto.randomUUIDv4().pipe(Effect.orDie); const tempIndexPath = path.join(gitCommonDir, `t3-checkpoint-index-${indexId}`); const commitEnv: NodeJS.ProcessEnv = { @@ -898,7 +937,6 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* ( ); yield* Effect.gen(function* () { - const headExists = yield* hasHeadCommit(input.cwd); const sparseConfig = yield* execute({ operation, cwd: input.cwd, @@ -917,17 +955,19 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* ( } if (headExists) { const reusedIndex = yield* Effect.gen(function* () { - const indexPath = yield* execute({ - operation, - cwd: input.cwd, - args: ["rev-parse", "--path-format=absolute", "--git-path", "index"], - }); - const { mtime } = yield* fileSystem.stat(indexPath.stdout.trim()); + const userIndexPath = + indexPath ?? + (yield* execute({ + operation, + cwd: input.cwd, + args: ["rev-parse", "--path-format=absolute", "--git-path", "index"], + })).stdout.trim(); + const { mtime } = yield* fileSystem.stat(userIndexPath); if (Option.isNone(mtime)) return false; // Stay below the source timestamp even if Date rounded up, preserving Git's racy check. const indexTime = Math.floor((mtime.value.getTime() - 1) / 1000); if (indexTime <= 0) return false; - yield* fileSystem.copyFile(indexPath.stdout.trim(), tempIndexPath); + yield* fileSystem.copyFile(userIndexPath, tempIndexPath); yield* execute({ operation, cwd: input.cwd, diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts index 14eac2d47d220..b6b8f44283d18 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.test.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts @@ -5,8 +5,10 @@ import * as NodeURL from "node:url"; import * as NodeServices from "@effect/platform-node/NodeServices"; import * as HostProcess from "@t3tools/shared/HostProcess"; import { assert, it, describe } from "@effect/vitest"; +import * as Cause from "effect/Cause"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; +import * as Exit from "effect/Exit"; import * as FileSystem from "effect/FileSystem"; import * as Fiber from "effect/Fiber"; import * as Layer from "effect/Layer"; @@ -49,7 +51,7 @@ const layerServerConfig = ServerConfig.layerTest(process.cwd(), { prefix: "t3-git-vcs-driver-test-", }); const layerTest = GitVcsDriver.layer.pipe( - Layer.provide(layerServerConfig), + Layer.provideMerge(layerServerConfig), Layer.provideMerge(NodeServices.layer), ); @@ -333,6 +335,415 @@ it.effect("invalidates origin remote cache when a driver mutation adds origin", }).pipe(Effect.provide(layerTest)), ); +it.effect.each([ + { hookExitCode: 0, commonDirFailure: "none" }, + { hookExitCode: 1, commonDirFailure: "none" }, + { hookExitCode: 0, commonDirFailure: "error" }, + { hookExitCode: 1, commonDirFailure: "error" }, + { hookExitCode: 0, commonDirFailure: "non-repository" }, + { hookExitCode: 1, commonDirFailure: "non-repository" }, +])( + "invalidates sibling worktree refs and status after checkout hook exit $hookExitCode with common-dir failure $commonDirFailure", + ({ hookExitCode, commonDirFailure }) => + Effect.scoped( + Effect.gen(function* () { + const delegate = yield* ChildProcessSpawner.ChildProcessSpawner; + const spawnedArgs: Array> = []; + let invalidationFailure = "none"; + const spawner = ChildProcessSpawner.make((command) => + Effect.gen(function* () { + if (!ChildProcess.isStandardCommand(command)) { + return yield* Effect.die("expected a standard Git command"); + } + spawnedArgs.push(command.args); + if (invalidationFailure !== "none" && command.args.includes("--git-common-dir")) { + if (invalidationFailure === "non-repository") return makeNonRepositoryHandle(); + return ChildProcessSpawner.makeHandle({ + ...makeNonRepositoryHandle(), + stderr: Stream.encodeText(Stream.make("fatal: could not read repository metadata")), + }); + } + return yield* delegate.spawn(command); + }), + ); + const driver = yield* makeGitVcsDriverCore().pipe( + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), + ); + const cwd = yield* makeTmpDir(); + const { initialBranch } = yield* initRepoWithCommit(cwd); + const path = yield* Path.Path; + const fs = yield* FileSystem.FileSystem; + const siblingPath = path.join(yield* makeTmpDir("git-worktrees-"), "sibling"); + yield* git(cwd, ["worktree", "add", "-b", "cache-sibling", siblingPath, "HEAD"]); + + const before = yield* driver.listRefs({ cwd: siblingPath, refresh: true }); + assert.isTrue(before.refs.find((ref) => ref.name === "cache-sibling")?.current); + const statusBefore = yield* driver.statusDetailsLocal(siblingPath); + assert.isFalse(statusBefore.hasOriginRemote); + assert.isFalse(statusBefore.isDefaultBranch); + yield* driver.listRefs({ cwd, refresh: true }); + yield* git(cwd, ["checkout", "-b", "creator-current"]); + yield* writeTextFile( + cwd, + ".git/hooks/post-checkout", + "#!/bin/sh\n" + + "git remote add origin https://example.invalid/repo.git || exit 2\n" + + "git update-ref refs/remotes/origin/cache-sibling HEAD || exit 2\n" + + "git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/cache-sibling || exit 2\n" + + `exit ${hookExitCode}\n`, + ); + yield* fs.chmod(path.join(cwd, ".git/hooks/post-checkout"), 0o755); + const worktreePath = path.join(yield* makeTmpDir("git-worktrees-"), "created"); + spawnedArgs.length = 0; + invalidationFailure = commonDirFailure; + + const created = yield* driver + .createWorktree({ + cwd, + path: worktreePath, + refName: initialBranch, + newRefName: "feature/cache-created", + }) + .pipe(Effect.result); + + assert.equal(Result.isSuccess(created), hookExitCode === 0); + if (Result.isFailure(created)) { + assert.equal(created.failure.operation, "GitVcsDriver.createWorktree"); + assert.equal(created.failure.detail, "git worktree add failed"); + assert.equal(created.failure.exitCode, hookExitCode); + } + const metadataQueries = spawnedArgs.filter((args) => args.includes("--git-common-dir")); + assert.deepStrictEqual(metadataQueries, [["rev-parse", "--git-common-dir"]]); + assert.isFalse(spawnedArgs.some((args) => args.includes("symbolic-ref"))); + invalidationFailure = "none"; + + spawnedArgs.length = 0; + const creatorStatus = yield* driver.statusDetailsLocal(cwd); + assert.equal(creatorStatus.branch, "creator-current"); + assert.isTrue(spawnedArgs.some((args) => args.includes("--git-common-dir"))); + const siblingRefs = yield* driver.listRefs({ cwd: siblingPath }); + const createdRef = siblingRefs.refs.find((ref) => ref.name === "feature/cache-created"); + assert.isDefined(createdRef?.worktreePath); + assert.equal( + NodeFS.realpathSync.native(createdRef!.worktreePath!), + NodeFS.realpathSync.native(worktreePath), + ); + assert.isTrue(siblingRefs.hasPrimaryRemote); + assert.isTrue(siblingRefs.refs.find((ref) => ref.name === "cache-sibling")?.current); + const statusAfter = yield* driver.statusDetailsLocal(siblingPath); + assert.isTrue(statusAfter.hasOriginRemote); + assert.isTrue(statusAfter.isDefaultBranch); + const creatorRefs = yield* driver.listRefs({ cwd }); + assert.isTrue(creatorRefs.refs.find((ref) => ref.name === "creator-current")?.current); + assert.isFalse(creatorRefs.refs.find((ref) => ref.name === initialBranch)?.current); + }), + ).pipe(Effect.provide(layerTest)), +); + +it.effect.each(["branch", "detached", "nested", "aliased-root", "aliased-nested"] as const)( + "discovers only the common directory during worktree creation from %s", + (location) => + Effect.scoped( + Effect.gen(function* () { + const delegate = yield* ChildProcessSpawner.ChildProcessSpawner; + const spawnedArgs: Array> = []; + const spawner = ChildProcessSpawner.make((command) => { + if (ChildProcess.isStandardCommand(command)) spawnedArgs.push(command.args); + return delegate.spawn(command); + }); + const driver = yield* makeGitVcsDriverCore().pipe( + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), + ); + const cwd = yield* makeTmpDir(); + const { initialBranch } = yield* initRepoWithCommit(cwd); + const path = yield* Path.Path; + if (location === "detached") yield* git(cwd, ["checkout", "--detach"]); + const isNested = location === "nested" || location === "aliased-nested"; + const physicalCwd = isNested ? path.join(cwd, "nested") : cwd; + const fs = yield* FileSystem.FileSystem; + yield* fs.makeDirectory(physicalCwd, { recursive: true }); + const isAliased = location === "aliased-root" || location === "aliased-nested"; + const creationCwd = isAliased + ? path.join(yield* makeTmpDir("git-alias-"), "link") + : physicalCwd; + if (isAliased) NodeFS.symlinkSync(physicalCwd, creationCwd, "junction"); + const worktreePath = path.join(yield* makeTmpDir("git-worktrees-"), "cold"); + + yield* driver.createWorktree({ + cwd: creationCwd, + path: worktreePath, + refName: initialBranch, + newRefName: "feature/cold", + }); + + assert.equal(spawnedArgs.filter((args) => args.includes("--git-common-dir")).length, 1); + assert.isFalse(spawnedArgs.some((args) => args.includes("--show-toplevel"))); + assert.isFalse(spawnedArgs.some((args) => args.includes("symbolic-ref"))); + spawnedArgs.length = 0; + const originalStatus = yield* driver.statusDetailsLocal(creationCwd); + assert.equal(originalStatus.branch, location === "detached" ? null : initialBranch); + const originalRefs = yield* driver.listRefs({ cwd: creationCwd }); + assert.equal(originalRefs.isAtRepositoryRoot, !isNested); + assert.deepStrictEqual( + originalRefs.refs.filter((ref) => ref.current).map((ref) => ref.name), + location === "detached" ? [] : [initialBranch], + ); + assert.equal(spawnedArgs.filter((args) => args.includes("--git-common-dir")).length, 1); + const refs = yield* driver.listRefs({ cwd: worktreePath }); + assert.isTrue(refs.refs.find((ref) => ref.name === "feature/cold")?.current); + assert.equal( + yield* git(worktreePath, ["rev-parse", "HEAD"]), + yield* git(cwd, ["rev-parse", "HEAD"]), + ); + }), + ).pipe(Effect.provide(layerTest)), +); + +it.effect.each(["bare", "unborn"] as const)( + "invalidates a %s repository after worktree creation", + (state) => + Effect.scoped( + Effect.gen(function* () { + const source = yield* makeTmpDir(); + const cwd = yield* makeTmpDir("git-discovery-fallback-"); + if (state === "bare") { + yield* initRepoWithCommit(source); + yield* git(source, ["clone", "--bare", source, cwd]); + } else { + yield* git(cwd, ["init"]); + } + const delegate = yield* ChildProcessSpawner.ChildProcessSpawner; + const spawnedArgs: Array> = []; + const spawner = ChildProcessSpawner.make((command) => { + if (ChildProcess.isStandardCommand(command)) spawnedArgs.push(command.args); + return delegate.spawn( + state === "bare" ? ChildProcess.setEnv(command, { GIT_DIR: cwd }) : command, + ); + }); + const driver = yield* makeGitVcsDriverCore().pipe( + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), + ); + const path = yield* Path.Path; + const worktreePath = path.join(yield* makeTmpDir("git-worktrees-"), "fallback"); + const result = yield* driver + .createWorktree({ + cwd, + path: worktreePath, + refName: "HEAD", + newRefName: "feature/fallback", + }) + .pipe(Effect.result); + assert.equal(Result.isSuccess(result), state === "bare"); + if (Result.isFailure(result)) { + assert.equal(result.failure.operation, "GitVcsDriver.createWorktree.createBranch"); + assert.equal(result.failure.detail, "git worktree add failed"); + } + assert.equal(spawnedArgs.filter((args) => args.includes("--git-common-dir")).length, 1); + assert.isFalse(spawnedArgs.some((args) => args[0] === "symbolic-ref")); + spawnedArgs.length = 0; + const refs = yield* driver.listRefs({ cwd }); + assert.isTrue(refs.isRepo); + if (state === "bare") { + assert.isTrue(refs.refs.some((ref) => ref.name === "feature/fallback")); + } else { + assert.isEmpty(refs.refs); + } + assert.equal(spawnedArgs.filter((args) => args.includes("--git-common-dir")).length, 1); + }), + ).pipe(Effect.provide(layerTest)), +); + +it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")( + "preserves embedded newlines in repository paths after worktree creation", + () => + Effect.scoped( + Effect.gen(function* () { + const path = yield* Path.Path; + const fs = yield* FileSystem.FileSystem; + const cwd = path.join(yield* makeTmpDir(), "repo\nwith-newline"); + yield* fs.makeDirectory(cwd); + const { initialBranch } = yield* initRepoWithCommit(cwd); + const driver = yield* GitVcsDriver.GitVcsDriver; + const worktreePath = path.join(yield* makeTmpDir("git-worktrees-"), "newline"); + yield* driver.createWorktree({ + cwd, + path: worktreePath, + refName: initialBranch, + newRefName: "feature/newline", + }); + const status = yield* driver.statusDetailsLocal(cwd); + assert.equal(status.branch, initialBranch); + const refs = yield* driver.listRefs({ cwd }); + assert.isTrue(refs.refs.find((ref) => ref.name === initialBranch)?.current); + assert.isTrue(refs.refs.some((ref) => ref.name === "feature/newline")); + }), + ).pipe(Effect.provide(layerTest)), +); + +it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")( + "preserves a trailing carriage return in the repository root after creation through an alias", + () => + Effect.scoped( + Effect.gen(function* () { + const path = yield* Path.Path; + const fs = yield* FileSystem.FileSystem; + const sandbox = yield* makeTmpDir(); + const repository = path.join(sandbox, "repo\r"); + const cwd = path.join(sandbox, "alias"); + yield* fs.makeDirectory(repository); + const { initialBranch } = yield* initRepoWithCommit(repository); + NodeFS.symlinkSync(repository, cwd, "junction"); + const driver = yield* GitVcsDriver.GitVcsDriver; + yield* driver.createWorktree({ + cwd, + path: path.join(yield* makeTmpDir("git-worktrees-"), "created"), + refName: initialBranch, + newRefName: "feature/carriage-return", + }); + const status = yield* driver.statusDetailsLocal(cwd); + assert.equal(status.workingTree.rootPath, NodeFS.realpathSync.native(repository)); + assert.equal(status.branch, initialBranch); + const refs = yield* driver.listRefs({ cwd }); + assert.isTrue(refs.isAtRepositoryRoot); + assert.isTrue(refs.refs.some((ref) => ref.name === "feature/carriage-return")); + }), + ).pipe(Effect.provide(layerTest)), +); + +it.effect( + "reads fresh metadata when another mutation completes during common-directory discovery", + () => + Effect.scoped( + Effect.gen(function* () { + const cwd = yield* makeTmpDir(); + yield* initRepoWithCommit(cwd); + const commit = yield* git(cwd, ["rev-parse", "HEAD"]); + const delegate = yield* ChildProcessSpawner.ChildProcessSpawner; + const discoveryComplete = yield* Deferred.make(); + const releaseDiscovery = yield* Deferred.make(); + let holdDiscovery = false; + const spawner = ChildProcessSpawner.make((command) => + Effect.gen(function* () { + const handle = yield* delegate.spawn(command); + if ( + !holdDiscovery || + !ChildProcess.isStandardCommand(command) || + !command.args.includes("--git-common-dir") + ) + return handle; + holdDiscovery = false; + const output = yield* Stream.runCollect(handle.stdout); + yield* handle.exitCode; + yield* Deferred.succeed(discoveryComplete, undefined); + yield* Deferred.await(releaseDiscovery); + return ChildProcessSpawner.makeHandle({ + ...handle, + stdout: Stream.fromIterable(output), + }); + }), + ); + const driver = yield* makeGitVcsDriverCore().pipe( + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), + ); + yield* driver.listRefs({ cwd, refresh: true }); + const path = yield* Path.Path; + const worktreePath = path.join(yield* makeTmpDir("git-worktrees-"), "concurrent"); + holdDiscovery = true; + const creation = yield* driver + .createWorktree({ + cwd, + path: worktreePath, + refName: "HEAD", + newRefName: "feature/concurrent", + }) + .pipe(Effect.forkChild({ startImmediately: true })); + yield* Effect.gen(function* () { + yield* Deferred.await(discoveryComplete); + yield* driver.switchRef({ cwd, refName: commit }); + }).pipe(Effect.ensuring(Deferred.succeed(releaseDiscovery, undefined))); + const created = yield* Fiber.join(creation); + assert.equal(created.worktree.path, worktreePath); + const refs = yield* driver.listRefs({ cwd }); + assert.isEmpty(refs.refs.filter((ref) => ref.current)); + assert.isTrue(refs.refs.some((ref) => ref.name === "feature/concurrent")); + assert.equal((yield* driver.statusDetailsLocal(cwd)).branch, null); + }), + ).pipe(Effect.provide(layerTest)), +); + +it.effect("invalidates cached metadata after interrupted worktree creation", () => + Effect.scoped( + Effect.gen(function* () { + const cwd = yield* makeTmpDir(); + yield* initRepoWithCommit(cwd); + const delegate = yield* ChildProcessSpawner.ChildProcessSpawner; + const discoveryComplete = yield* Deferred.make(); + const releaseDiscovery = yield* Deferred.make(); + let holdDiscovery = false; + const spawner = ChildProcessSpawner.make((command) => + Effect.gen(function* () { + const handle = yield* delegate.spawn(command); + if ( + !holdDiscovery || + !ChildProcess.isStandardCommand(command) || + !command.args.includes("--git-common-dir") + ) + return handle; + holdDiscovery = false; + const output = yield* Stream.runCollect(handle.stdout); + yield* handle.exitCode; + yield* Deferred.succeed(discoveryComplete, undefined); + yield* Deferred.await(releaseDiscovery); + return ChildProcessSpawner.makeHandle({ ...handle, stdout: Stream.fromIterable(output) }); + }), + ); + const driver = yield* makeGitVcsDriverCore().pipe( + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), + ); + yield* driver.listRefs({ cwd, refresh: true }); + yield* driver.statusDetailsLocal(cwd); + yield* git(cwd, ["checkout", "-b", "creator-current"]); + const path = yield* Path.Path; + const worktreePath = path.join(yield* makeTmpDir("git-worktrees-"), "interrupted"); + holdDiscovery = true; + const creation = yield* driver + .createWorktree({ + cwd, + path: worktreePath, + refName: "HEAD", + newRefName: "feature/interrupted", + }) + .pipe(Effect.forkChild({ startImmediately: true })); + const cancellation = yield* Effect.gen(function* () { + yield* Deferred.await(discoveryComplete); + return yield* Fiber.interrupt(creation).pipe(Effect.forkChild({ startImmediately: true })); + }).pipe(Effect.ensuring(Deferred.succeed(releaseDiscovery, undefined))); + yield* Fiber.join(cancellation); + const exit = yield* Fiber.await(creation); + assert.isTrue(Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause)); + const refs = yield* driver.listRefs({ cwd }); + assert.isTrue(refs.refs.find((ref) => ref.name === "creator-current")?.current); + const interruptedRef = refs.refs.find((ref) => ref.name === "feature/interrupted"); + assert.isDefined(interruptedRef?.worktreePath); + assert.equal( + NodeFS.realpathSync.native(interruptedRef!.worktreePath!), + NodeFS.realpathSync.native(worktreePath), + ); + assert.equal((yield* driver.statusDetailsLocal(cwd)).branch, "creator-current"); + const nextPath = path.join(yield* makeTmpDir("git-worktrees-"), "after-interruption"); + yield* driver.createWorktree({ + cwd, + path: nextPath, + refName: "HEAD", + newRefName: "feature/after", + }); + assert.isTrue( + (yield* driver.listRefs({ cwd })).refs.some((ref) => ref.name === "feature/after"), + ); + }), + ).pipe(Effect.provide(layerTest)), +); + describe("pullCurrentBranch", () => { // A fast-forward that prints over 1 MB takes thousands of files, so the real pull's output is // padded past the cap instead. @@ -2129,6 +2540,27 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { }), ); + it.effect.each([ + { scenario: "kept", before: "\uFEFFold\n", after: "\uFEFFnew\n" }, + { scenario: "added", before: "same\n", after: "\uFEFFsame\n" }, + { scenario: "removed", before: "\uFEFFsame\n", after: "same\n" }, + ])("preserves byte-order marks in diff expansion when $scenario", ({ before, after }) => + Effect.gen(function* () { + const cwd = yield* makeTmpDir(); + yield* initRepoWithCommit(cwd); + yield* writeTextFile(cwd, "README.md", before); + yield* git(cwd, ["commit", "-am", "write original contents"]); + yield* writeTextFile(cwd, "README.md", after); + const driver = yield* GitVcsDriver.GitVcsDriver; + + const contents = yield* driver.getReviewDiffFileContents( + makeReviewDiffFileContentsInput(cwd), + ); + + assert.deepStrictEqual(contents, { oldContents: before, newContents: after }); + }), + ); + it.effect("reads a base ref that looks like an option as a revision", () => Effect.gen(function* () { const cwd = yield* makeTmpDir(); @@ -2628,10 +3060,23 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { yield* git(repo, ["checkout", "-b", "real-only"]); // The decoy must exist before the first lookup, which caches repository paths. - const refs = yield* (yield* GitVcsDriver.GitVcsDriver).listRefs({ cwd: link }); + const driver = yield* GitVcsDriver.GitVcsDriver; + const refs = yield* driver.listRefs({ cwd: link }); assert.isTrue(refs.isRepo); assert.isFalse(refs.refs.some((ref) => ref.name === "decoy-only")); assert.isTrue(refs.refs.some((ref) => ref.name === "real-only")); + + yield* driver.createWorktree({ + cwd: link, + path: pathService.join(yield* makeTmpDir("git-worktrees-"), "real"), + refName: "real-only", + newRefName: "feature/real-created", + }); + const createdRefs = yield* driver.listRefs({ cwd: link }); + assert.isFalse(createdRefs.isAtRepositoryRoot); + assert.isFalse(createdRefs.refs.some((ref) => ref.name === "decoy-only")); + assert.isTrue(createdRefs.refs.some((ref) => ref.name === "real-only" && ref.current)); + assert.isTrue(createdRefs.refs.some((ref) => ref.name === "feature/real-created")); }), ); diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index 4cbe628a88e57..2987a6d983336 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -821,7 +821,7 @@ const collectOutput = Effect.fnUntraced(function* ( { readonly text: string; readonly truncated: boolean; readonly invalidUtf8: boolean }, GitCommandError > { - const decoder = new TextDecoder(); + const decoder = new TextDecoder("utf-8", { ignoreBOM: true }); // Coder: preserve invalid UTF-8 detection for strict review blob expansion. const utf8Validator = new TextDecoder("utf-8", { fatal: true }); let invalidUtf8 = false; @@ -1358,9 +1358,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* } }); - const resolveRepositoryPathsUncached = Effect.fn("resolveRepositoryPathsUncached")(function* ( - cwd: string, - ) { + const readGitCommonDir = Effect.fn("readGitCommonDir")(function* (cwd: string) { // Git reports relative paths from the physical cwd, which can differ from a symlink. const commonDirResult = yield* executeGitWithStableDiagnostics( "GitVcsDriver.resolveRepositoryPaths.commonDir", @@ -1394,9 +1392,16 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* const resolvedGitCommonDir = path.isAbsolute(commonDirOutput) ? path.normalize(commonDirOutput) : path.resolve(physicalCwd, commonDirOutput); - const gitCommonDir = yield* fileSystem + return yield* fileSystem .realPath(resolvedGitCommonDir) .pipe(Effect.orElseSucceed(() => resolvedGitCommonDir)); + }); + + const resolveRepositoryPathsUncached = Effect.fn("resolveRepositoryPathsUncached")(function* ( + cwd: string, + ) { + const gitCommonDir = yield* readGitCommonDir(cwd); + if (gitCommonDir === null) return null; const [worktreeRootResult, currentBranchResult] = yield* Effect.all( [ executeGit( @@ -1534,11 +1539,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* }, ); const invalidateStatusStaticCaches = Effect.fn("GitVcsDriverCore.invalidateStatusStaticCaches")( - function* (cwd: string) { - const repositoryPaths = yield* resolveRepositoryPaths(cwd).pipe( - Effect.catchTags({ GitCommandError: () => Effect.succeed(null) }), - ); - const cacheKey = repositoryPaths?.gitCommonDir ?? normalizeRepositoryPathsCacheKey(cwd); + function* (cwd: string, gitCommonDir?: string | null) { + const commonDir = + gitCommonDir === undefined + ? (yield* resolveRepositoryPaths(cwd).pipe( + Effect.catchTags({ GitCommandError: () => Effect.succeed(null) }), + ))?.gitCommonDir + : gitCommonDir; + const cacheKey = commonDir ?? normalizeRepositoryPathsCacheKey(cwd); yield* Cache.invalidate(defaultBranchCache, cacheKey); yield* Cache.invalidate(originExistsCache, cacheKey); }, @@ -3556,7 +3564,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* } // Coder: reject invalid UTF-8 instead of replacing bytes in review contents. return yield* Effect.try({ - try: () => new TextDecoder("utf-8", { fatal: true }).decode(bytes), + try: () => new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(bytes), catch: (cause) => fileError("fs.readFile", `Cannot expand binary file '${input.newPath}'.`, cause), }); @@ -3948,17 +3956,21 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* }); const invalidateListRefsSnapshot = Effect.fn("invalidateListRefsSnapshot")(function* ( cwd: string, + gitCommonDir?: string | null, ) { const repositoryPathsCacheKey = normalizeRepositoryPathsCacheKey(cwd); - const repositoryPaths = yield* Cache.get(repositoryPathsCache, repositoryPathsCacheKey); - if (repositoryPaths === null) return; - const previousGeneration = currentListRefsGeneration(repositoryPaths.gitCommonDir); - bumpListRefsGeneration(repositoryPaths.gitCommonDir); - bumpListRefsEpoch(repositoryPaths.gitCommonDir); + const commonDir = + gitCommonDir === undefined + ? (yield* Cache.get(repositoryPathsCache, repositoryPathsCacheKey))?.gitCommonDir + : gitCommonDir; + if (commonDir == null) return; + const previousGeneration = currentListRefsGeneration(commonDir); + bumpListRefsGeneration(commonDir); + bumpListRefsEpoch(commonDir); yield* Cache.invalidate( listRefsRefreshSnapshotCache, new GitRefsRefreshCacheKey({ - gitCommonDir: repositoryPaths.gitCommonDir, + gitCommonDir: commonDir, generation: previousGeneration, }), ); @@ -4865,6 +4877,26 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* ]), ), ); + const invalidateWorktreeCreationCaches = Effect.fn( + "GitVcsDriverCore.invalidateWorktreeCreationCaches", + )(function* (cwd: string) { + let gitCommonDir = yield* readGitCommonDir(cwd).pipe( + Effect.catchTags({ GitCommandError: () => Effect.succeed(null) }), + ); + if (gitCommonDir === null) { + gitCommonDir = yield* Cache.getOption( + repositoryPathsCache, + normalizeRepositoryPathsCacheKey(cwd), + ).pipe( + Effect.map((cached) => Option.getOrNull(cached)?.gitCommonDir ?? null), + Effect.orElseSucceed(() => null), + ); + } + yield* Effect.all([ + invalidateListRefsSnapshot(cwd, gitCommonDir).pipe(Effect.ignore), + invalidateStatusStaticCaches(cwd, gitCommonDir).pipe(Effect.ignore), + ]); + }); const initRepoWithListRefsInvalidation: GitVcsDriver.GitVcsDriver["Service"]["initRepo"] = ( input, ) => @@ -4907,7 +4939,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* resolveBranchName, listRefs, createWorktree: Effect.fnUntraced(function* (input, options) { - return yield* withListRefsInvalidation(input.cwd, createWorktree(input, options)); + return yield* createWorktree(input, options).pipe( + Effect.ensuring(invalidateWorktreeCreationCaches(input.cwd)), + ); }), fetchRefIntoBranch: Effect.fnUntraced(function* (input) { return yield* withListRefsInvalidation(input.cwd, fetchRefIntoBranch(input)); diff --git a/apps/server/src/workspace/WorkspaceEntries.test.ts b/apps/server/src/workspace/WorkspaceEntries.test.ts index 98da0f32dffd1..5848f3de615bb 100644 --- a/apps/server/src/workspace/WorkspaceEntries.test.ts +++ b/apps/server/src/workspace/WorkspaceEntries.test.ts @@ -1,4 +1,5 @@ // @effect-diagnostics nodeBuiltinImport:off +import * as NodeFSP from "node:fs/promises"; import * as NodeServices from "@effect/platform-node/NodeServices"; import { it, afterEach, describe, expect } from "@effect/vitest"; import * as Effect from "effect/Effect"; @@ -132,6 +133,113 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => { }), ); + it.effect("lists symlinks as what they point to, hiding ones that cannot be opened", () => + Effect.gen(function* () { + const platform = yield* HostProcess.Platform; + if (platform === "win32") return; + const cwd = yield* makeTempDir({ git: true }); + const outside = yield* makeTempDir(); + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + yield* writeTextFile(cwd, "real.txt"); + yield* writeTextFile(cwd, "realdir/nested.txt"); + yield* writeTextFile(outside, "outside.txt"); + yield* fileSystem.symlink("real.txt", path.join(cwd, "link.txt")); + yield* fileSystem.symlink("realdir", path.join(cwd, "linkdir")); + yield* fileSystem.symlink(outside, path.join(cwd, "outside-link")); + yield* fileSystem.symlink(".git", path.join(cwd, "git-link")); + yield* fileSystem.symlink("missing", path.join(cwd, "dangling")); + + const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; + const root = yield* workspaceEntries.list({ cwd, directoryPath: "" }); + expect(root.entries.toSorted((a, b) => a.path.localeCompare(b.path))).toEqual([ + { path: "link.txt", kind: "file" }, + { path: "linkdir", kind: "directory" }, + { path: "real.txt", kind: "file" }, + { path: "realdir", kind: "directory" }, + ]); + expect(yield* workspaceEntries.list({ cwd, directoryPath: "linkdir" })).toEqual({ + entries: [{ path: "linkdir/nested.txt", kind: "file" }], + truncated: false, + }); + }), + ); + + it.effect("hides folder links that lead back to a folder on the listed path", () => + // "Expand all folders" loads every folder it expands, so a link that loops would + // keep nesting the same entries without end. + Effect.gen(function* () { + const platform = yield* HostProcess.Platform; + if (platform === "win32") return; + const cwd = yield* makeTempDir(); + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + yield* writeTextFile(cwd, "a/file.txt"); + yield* writeTextFile(cwd, "b/file.txt"); + yield* fileSystem.symlink(".", path.join(cwd, "self")); + yield* fileSystem.symlink("..", path.join(cwd, "a/up")); + yield* fileSystem.symlink("../b", path.join(cwd, "a/to-b")); + yield* fileSystem.symlink("../a", path.join(cwd, "b/to-a")); + + const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; + const paths = (directoryPath: string) => + workspaceEntries + .list({ cwd, directoryPath }) + .pipe(Effect.map(({ entries }) => entries.map((entry) => entry.path).toSorted())); + expect(yield* paths("")).toEqual(["a", "b"]); + // A link into a sibling folder is fine on its own... + expect(yield* paths("a")).toEqual(["a/file.txt", "a/to-b"]); + // ...but once followed, the sibling's link back is hidden. + expect(yield* paths("a/to-b")).toEqual(["a/to-b/file.txt"]); + }), + ); + + it.effect("resolves a folder of many symlinks in bounded batches", () => + Effect.gen(function* () { + const platform = yield* HostProcess.Platform; + if (platform === "win32") return; + const cwd = yield* makeTempDir(); + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + // Shaped like a pnpm `node_modules`: one link per package into `.pnpm`. + const directoryPath = [...Array.from({ length: 80 }, () => "nested"), "node_modules"].join( + "/", + ); + const packages = Array.from({ length: 200 }, (_, index) => `pkg-${index}`); + yield* Effect.forEach( + packages, + (name) => + Effect.gen(function* () { + yield* writeTextFile(cwd, `${directoryPath}/.pnpm/${name}/index.js`); + yield* fileSystem.symlink(`.pnpm/${name}`, path.join(cwd, directoryPath, name)); + }), + { concurrency: 32, discard: true }, + ); + const realpath = NodeFSP.realpath; + let inFlight = 0; + let maxInFlight = 0; + vi.spyOn(NodeFSP, "realpath").mockImplementation((async (target: string) => { + maxInFlight = Math.max(maxInFlight, ++inFlight); + try { + return await realpath(target); + } finally { + inFlight--; + } + }) as typeof NodeFSP.realpath); + + const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; + const { entries } = yield* workspaceEntries.list({ cwd, directoryPath }); + expect(entries.map((entry) => entry.path).toSorted()).toEqual( + [ + `${directoryPath}/.pnpm`, + ...packages.map((name) => `${directoryPath}/${name}`), + ].toSorted(), + ); + expect(entries.every((entry) => entry.kind === "directory")).toBe(true); + expect(maxInFlight).toBeLessThanOrEqual(64); + }), + ); + it.effect("labels the children of wide folders exactly as git does for each child", () => Effect.gen(function* () { // Above 1,000 children, listing first asks whether the whole folder is ignored. diff --git a/apps/server/src/workspace/WorkspaceEntries.ts b/apps/server/src/workspace/WorkspaceEntries.ts index ab839e593aa2e..53f16a830ee3a 100644 --- a/apps/server/src/workspace/WorkspaceEntries.ts +++ b/apps/server/src/workspace/WorkspaceEntries.ts @@ -112,6 +112,9 @@ const MAX_PROJECT_DIRECTORY_ENTRIES = 5000; /** Paths sent to one `git check-ignore` call when labelling a folder's children. */ const IGNORE_CHECK_CHUNK_SIZE = 1000; +/** Children classified at once when listing a folder; each symlink costs a realpath and a stat. */ +const SYMLINK_RESOLVE_BATCH_SIZE = 64; + const resolveBrowseTarget = Effect.fn("WorkspaceEntries.resolveBrowseTarget")(function* ( input: FilesystemBrowseInput, path: Path.Path, @@ -313,12 +316,70 @@ export const make = Effect.gen(function* () { throw new Error("Directory must be inside the workspace and outside .git."); } const children = await NodeFSP.readdir(directory, { withFileTypes: true }); - const listed = children.flatMap((child): ProjectEntry[] => { - if (child.name === ".git" || (!child.isDirectory() && !child.isFile())) return []; + const isSameOrInside = (parent: string, child: string) => { + const relativeChild = path.relative(parent, child); + return ( + relativeChild !== ".." && + !relativeChild.startsWith(`..${path.sep}`) && + !path.isAbsolute(relativeChild) + ); + }; + // Reject canonical ancestors so "Expand all folders" cannot follow a cycle. + let visitedFolders: Promise> | undefined; + const loadVisitedFolders = () => + (visitedFolders ??= (async () => { + const folders = [root]; + let current = normalizedCwd; + for (const segment of target.relativePath.split("/").filter(Boolean)) { + current = path.join(current, segment); + folders.push(await NodeFSP.realpath(current)); + } + return folders; + })()); + const resolveSymlinkKind = async ( + name: string, + ): Promise => { + const resolved = await NodeFSP.realpath(path.join(directory, name)).catch( + () => undefined, + ); + if ( + resolved === undefined || + !isSameOrInside(root, resolved) || + path.relative(root, resolved).split(path.sep).includes(".git") + ) { + return undefined; + } + const stat = await NodeFSP.stat(resolved).catch(() => undefined); + if (stat?.isFile()) return "file"; + if (!stat?.isDirectory()) return undefined; + const folders = await loadVisitedFolders(); + return folders.some((folder) => isSameOrInside(resolved, folder)) + ? undefined + : "directory"; + }; + // Bound filesystem work for directories such as pnpm node_modules. + const kinds: Array = []; + for (let offset = 0; offset < children.length; offset += SYMLINK_RESOLVE_BATCH_SIZE) { + kinds.push( + ...(await Promise.all( + children + .slice(offset, offset + SYMLINK_RESOLVE_BATCH_SIZE) + .map(async (child): Promise => { + if (child.name === ".git") return undefined; + if (child.isDirectory()) return "directory"; + if (child.isFile()) return "file"; + return child.isSymbolicLink() ? resolveSymlinkKind(child.name) : undefined; + }), + )), + ); + } + const listed = children.flatMap((child, index): ProjectEntry[] => { + const kind = kinds[index]; + if (kind === undefined) return []; return [ { path: target.relativePath ? `${target.relativePath}/${child.name}` : child.name, - kind: child.isDirectory() ? "directory" : "file", + kind, }, ]; }); diff --git a/apps/server/src/workspace/WorkspaceFileSystem.test.ts b/apps/server/src/workspace/WorkspaceFileSystem.test.ts index 0cf5524df4e17..37d85a0fc684e 100644 --- a/apps/server/src/workspace/WorkspaceFileSystem.test.ts +++ b/apps/server/src/workspace/WorkspaceFileSystem.test.ts @@ -428,6 +428,38 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (i }), ); + it.effect("saves over an existing file without rescanning the workspace", () => + Effect.gen(function* () { + const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const cwd = yield* makeTempDir; + yield* writeTextFile(cwd, "src/main.ts", "export {};\n"); + + yield* workspaceFileSystem.writeFile({ + cwd, + relativePath: "src/main.ts", + contents: "export const edited = true;\n", + }); + + expect(yield* fileSystem.readFileString(path.join(cwd, "src/main.ts"))).toBe( + "export const edited = true;\n", + ); + }).pipe( + Effect.provide( + Layer.fresh(WorkspaceFileSystem.layer).pipe( + Layer.provide(layerKnownWorkspacesTest), + Layer.provide(WorkspacePaths.layer), + Layer.provide( + Layer.mock(WorkspaceEntries.WorkspaceEntries)({ + refresh: () => Effect.die("An overwrite rescanned the workspace."), + }), + ), + ), + ), + ), + ); + it.effect("rejects writes outside the workspace root", () => Effect.gen(function* () { const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; diff --git a/apps/server/src/workspace/WorkspaceFileSystem.ts b/apps/server/src/workspace/WorkspaceFileSystem.ts index 7107cd99631a8..053360572b312 100644 --- a/apps/server/src/workspace/WorkspaceFileSystem.ts +++ b/apps/server/src/workspace/WorkspaceFileSystem.ts @@ -531,6 +531,9 @@ export const make = Effect.gen(function* () { yield* knownWorkspaces.assertContains(input.cwd); const target = yield* resolveWriteTarget(input); const realTargetPath = target.realTargetPath; + const replacesFile = yield* fileSystem + .exists(realTargetPath) + .pipe(Effect.orElseSucceed(() => false)); yield* fileSystem.makeDirectory(path.dirname(realTargetPath), { recursive: true }).pipe( Effect.mapError( @@ -565,7 +568,12 @@ export const make = Effect.gen(function* () { cause, }), }); - yield* workspaceEntries.refresh(input.cwd); + // Editor autosave overwrites existing files, which leaves the indexed paths unchanged, and + // fff's watcher applies the new contents on its own. A forced rescan re-walks the whole + // workspace, and the save would wait for it. + if (!replacesFile) { + yield* workspaceEntries.refresh(input.cwd); + } return { relativePath: target.relativePath }; }); diff --git a/apps/server/src/workspace/WorkspacePaths.test.ts b/apps/server/src/workspace/WorkspacePaths.test.ts index 574904b343563..b178c621453b8 100644 --- a/apps/server/src/workspace/WorkspacePaths.test.ts +++ b/apps/server/src/workspace/WorkspacePaths.test.ts @@ -5,6 +5,8 @@ import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Path from "effect/Path"; +import * as HostProcess from "@t3tools/shared/HostProcess"; + import * as WorkspacePaths from "./WorkspacePaths.ts"; const layerTest = Layer.empty.pipe( @@ -58,5 +60,25 @@ it.layer(layerTest)("WorkspacePathsLive", (it) => { ); }), ); + + // On Windows `\` is a separator, so these names cannot exist there. + it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")( + "keeps a backslash in a name as part of the name", + () => + Effect.gen(function* () { + const workspacePaths = yield* WorkspacePaths.WorkspacePaths; + const path = yield* Path.Path; + const cwd = yield* makeTempDir(); + + for (const relativePath of ["weird\\name.txt", "..\\..\\etc"]) { + expect( + yield* workspacePaths.resolveRelativePathWithinRoot({ + workspaceRoot: cwd, + relativePath, + }), + ).toEqual({ absolutePath: path.join(cwd, relativePath), relativePath }); + } + }), + ); }); }); diff --git a/apps/server/src/workspace/WorkspacePaths.ts b/apps/server/src/workspace/WorkspacePaths.ts index 6b748e19396ec..ea183ed43e1ab 100644 --- a/apps/server/src/workspace/WorkspacePaths.ts +++ b/apps/server/src/workspace/WorkspacePaths.ts @@ -103,10 +103,6 @@ export class WorkspacePaths extends Context.Service< } >()("t3/workspace/WorkspacePaths") {} -function toPosixRelativePath(input: string): string { - return input.replaceAll("\\", "/"); -} - const make = Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; @@ -188,7 +184,10 @@ const make = Effect.gen(function* () { } const absolutePath = path.resolve(input.workspaceRoot, normalizedInputPath); - const relativeToRoot = toPosixRelativePath(path.relative(input.workspaceRoot, absolutePath)); + // Only the host separator becomes `/`: on POSIX `\` is a file name character. + const relativeToRoot = path + .relative(input.workspaceRoot, absolutePath) + .replaceAll(path.sep, "/"); if ( relativeToRoot.length === 0 || relativeToRoot === "." || diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts index 10a3b4fd20b14..6eb3b420e0f8e 100644 --- a/apps/server/src/ws.ts +++ b/apps/server/src/ws.ts @@ -138,7 +138,7 @@ import * as ProjectEnrichmentService from "./project/ProjectEnrichmentService.ts import * as ProjectService from "./project/ProjectService.ts"; import * as ManagedProjectFolders from "./project/ManagedProjectFolders.ts"; import * as ScratchThreadPromotion from "./project/ScratchThreadPromotion.ts"; -import { projectMutationOperation } from "./project/ProjectMutation.ts"; +import { mutateProjectWithClone } from "./project/ProjectMutation.ts"; import * as ProjectCloneTracker from "./project/ProjectCloneTracker.ts"; import * as RepositoryIdentityResolver from "./project/RepositoryIdentityResolver.ts"; import * as WorktreeSetupTracker from "./project/WorktreeSetupTracker.ts"; @@ -1034,10 +1034,7 @@ const layerWsRpc = () => ); const mutateProject = Effect.fn("ws.projects.mutate")(function* (mutation: ProjectMutation) { - const result = yield* projectMutationOperation(projectService, mutation); - if (mutation.type === "project.delete") - yield* projectCloneTracker.discard(mutation.projectId); - return result; + return yield* mutateProjectWithClone(projectService, projectCloneTracker, mutation); }); const handlers = ServerWsRpcGroup.of({ diff --git a/apps/web/package.json b/apps/web/package.json index 8d51cbce61554..07a68d80bee5d 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -11,7 +11,7 @@ "test": "vp test run --passWithNoTests --project unit --project browser" }, "dependencies": { - "@base-ui/react": "^1.4.1", + "@base-ui/react": "^1.9.0", "@betteroffice/docx": "0.4.3", "@betteroffice/fonts": "0.4.0", "@betteroffice/pptx": "0.2.0", @@ -54,10 +54,13 @@ "hast-util-to-html": "^9.0.5", "hast-util-to-jsx-runtime": "^2.3.6", "heic-to": "^1.5.2", + "html-url-attributes": "^3.0.1", "jszip": "3.10.1", "katex": "^0.16.47", "lucide": "^0.564.0", "lucide-react": "^0.564.0", + "mdast-util-from-markdown": "^2.0.3", + "micromark-util-combine-extensions": "^2.0.1", "morphicons": "^1.7.1", "pdfjs-dist": "6.4.299", "pele": "^1.8.0", @@ -67,6 +70,7 @@ "remark-gfm": "catalog:", "remark-math": "6.0.0", "remark-parse": "catalog:", + "remark-rehype": "catalog:", "saxes": "6.0.0", "sql.js": "1.15.0", "tailwind-merge": "^3.4.0", @@ -74,6 +78,7 @@ "unbash": "^5.0.0", "unified": "catalog:", "use-sync-external-store": "^1.6.0", + "vfile": "^6.0.3", "zustand": "^5.0.11" }, "devDependencies": { @@ -81,6 +86,7 @@ "@tailwindcss/vite": "^4.0.0", "@tanstack/router-plugin": "^1.161.0", "@types/culori": "^4.0.1", + "@types/hast": "^3.0.4", "@types/mdast": "^4.0.4", "@types/react": "catalog:", "@types/react-dom": "catalog:", @@ -91,6 +97,7 @@ "@vitest/browser-playwright": "5.0.5", "happy-dom": "^20.11.6", "jsdom": "^30.0.1", + "micromark-util-types": "catalog:", "oxc-transform-react": "~0.152.0", "playwright": "1.60.0", "react-test-renderer": "19.3.0", diff --git a/apps/web/src/appInit.tsx b/apps/web/src/appInit.tsx index a4bfdc5150bdc..812d5242af849 100644 --- a/apps/web/src/appInit.tsx +++ b/apps/web/src/appInit.tsx @@ -1,9 +1,10 @@ import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment"; import type { ClientSettings } from "@t3tools/contracts/settings"; import { createBrowserHistory } from "@tanstack/react-router"; +import * as Schema from "effect/Schema"; import { applyAppearanceContrast } from "./appearanceContrast"; -import { applyAppearanceFontVariables } from "./appearanceFonts"; +import { applyAppearanceFontVariables, TYPOGRAPHY_ADVANCED_STORAGE_KEY } from "./appearanceFonts"; import { retainThreadOverlaysForThread } from "./components/chat/threadOverlayRetention"; import { startBackgroundThreadAnnouncements } from "./components/BackgroundThreadAnnouncer"; import { startKeybindingsUpdateToasts } from "./components/KeybindingsUpdateToast"; @@ -20,6 +21,7 @@ import { useComposerDraftStore, } from "./composerDraftStore"; import { startEnvironmentThemeRepaint } from "./hooks/useEnvironmentTheme"; +import { readLocalStorageValue, subscribeLocalStorageKey } from "./hooks/useLocalStorage"; import { getClientSettings, startModelPreferencesMigration, @@ -41,10 +43,18 @@ import { resolveThreadRouteTarget } from "./threadRoutes"; function startClientAppearance(): void { const root = document.documentElement; let applied: ClientSettings | null = null; + let appliedSeparateFonts: boolean | null = null; const apply = () => { const settings = getClientSettings(); const previous = applied; applied = settings; + const separateFonts = readLocalStorageValue( + TYPOGRAPHY_ADVANCED_STORAGE_KEY, + false, + Schema.Boolean, + ); + const previousSeparateFonts = appliedSeparateFonts; + appliedSeparateFonts = separateFonts; if (previous?.diffColorScheme !== settings.diffColorScheme) { root.dataset.diffColorScheme = settings.diffColorScheme; } @@ -69,7 +79,8 @@ function startClientAppearance(): void { previous.fontSizeInterface !== settings.fontSizeInterface || previous.fontSizePrompt !== settings.fontSizePrompt || previous.fontSizeCode !== settings.fontSizeCode || - previous.fontSmoothing !== settings.fontSmoothing + previous.fontSmoothing !== settings.fontSmoothing || + previousSeparateFonts !== separateFonts ) { applyAppearanceFontVariables(root, { sans: settings.fontFamilySans, @@ -79,10 +90,12 @@ function startClientAppearance(): void { sizePrompt: settings.fontSizePrompt, sizeCode: settings.fontSizeCode, smoothing: settings.fontSmoothing, + separate: separateFonts, }); } }; subscribeClientSettings(apply); + subscribeLocalStorageKey(TYPOGRAPHY_ADVANCED_STORAGE_KEY, apply); apply(); } diff --git a/apps/web/src/appearanceFonts.browser.test.ts b/apps/web/src/appearanceFonts.browser.test.ts new file mode 100644 index 0000000000000..9bdc5c2ef9864 --- /dev/null +++ b/apps/web/src/appearanceFonts.browser.test.ts @@ -0,0 +1,55 @@ +import { describe, expect, it } from "vite-plus/test"; + +import "~/index.css"; +import { applyAppearanceFontVariables } from "./appearanceFonts"; + +function renderedFontSize(style: (probe: HTMLElement) => void): string { + const probe = document.createElement("div"); + style(probe); + document.body.append(probe); + try { + return getComputedStyle(probe).fontSize; + } finally { + probe.remove(); + } +} + +const composerFontSize = () => + renderedFontSize((probe) => { + // The composer editor's own size class. + probe.className = "text-(length:--font-size-prompt,var(--text-sm))"; + }); + +const touchComposerFontSize = () => + renderedFontSize((probe) => { + probe.style.fontSize = "var(--font-size-prompt-touch)"; + }); + +describe("applyAppearanceFontVariables", () => { + it("sizes the composer from the interface size until fonts are set separately", () => { + const root = document.documentElement; + const style = root.getAttribute("style"); + const preferences = { + sans: "", + code: "", + composer: "", + sizeInterface: 20, + sizePrompt: 14, + sizeCode: 13, + smoothing: true, + }; + try { + applyAppearanceFontVariables(root, { ...preferences, separate: false }); + expect(composerFontSize()).toBe("17.5px"); + expect(touchComposerFontSize()).toBe("17.5px"); + + applyAppearanceFontVariables(root, { ...preferences, separate: true }); + expect(composerFontSize()).toBe("14px"); + // iOS zooms into inputs under 16px. + expect(touchComposerFontSize()).toBe("16px"); + } finally { + if (style === null) root.removeAttribute("style"); + else root.setAttribute("style", style); + } + }); +}); diff --git a/apps/web/src/appearanceFonts.ts b/apps/web/src/appearanceFonts.ts index 48f8e5955e7e1..e7c2867cb279f 100644 --- a/apps/web/src/appearanceFonts.ts +++ b/apps/web/src/appearanceFonts.ts @@ -66,9 +66,10 @@ export interface AppearanceFontPreferences { readonly sizePrompt: number; readonly sizeCode: number; readonly smoothing: boolean; + readonly separate: boolean; } -/** Always writes sizes: interface size sets the root; prompt and code use pixels to avoid double scaling. */ +/** Interface size sets the root; prompt and code use pixels to avoid double scaling. */ export function applyAppearanceFontVariables( root: HTMLElement, preferences: AppearanceFontPreferences, @@ -76,7 +77,7 @@ export function applyAppearanceFontVariables( const families: ReadonlyArray = [ ["--font-sans", preferences.sans, DEFAULT_SANS_FONT_STACK], ["--font-mono", preferences.code, DEFAULT_CODE_FONT_STACK], - ["--font-composer", preferences.composer, "var(--font-sans)"], + ["--font-composer", preferences.separate ? preferences.composer : "", "var(--font-sans)"], ]; for (const [variable, custom, fallback] of families) { const list = cssFontFamilies(custom); @@ -88,7 +89,10 @@ export function applyAppearanceFontVariables( } root.style.fontSize = `${clampInterfaceFontSize(preferences.sizeInterface)}px`; - root.style.setProperty("--font-size-prompt", `${clampPromptFontSize(preferences.sizePrompt)}px`); + root.style.setProperty( + "--font-size-prompt", + preferences.separate ? `${clampPromptFontSize(preferences.sizePrompt)}px` : "var(--text-sm)", + ); const code = clampCodeFontSize(preferences.sizeCode); root.style.setProperty("--font-size-code", `${code}px`); root.style.setProperty("--diffs-font-size", `${code}px`); diff --git a/apps/web/src/components/AppRouteModal.browser.test.tsx b/apps/web/src/components/AppRouteModal.browser.test.tsx new file mode 100644 index 0000000000000..cb31eeca45ab0 --- /dev/null +++ b/apps/web/src/components/AppRouteModal.browser.test.tsx @@ -0,0 +1,27 @@ +import { describe, expect, it, vi } from "vite-plus/test"; +import { page, userEvent } from "vite-plus/test/browser"; + +import { render } from "../../test/render"; +import { useCommitOnBlur } from "../hooks/useCommitOnBlur"; +import { AppRouteModal } from "./AppRouteModal"; + +function CommitOnBlurField({ onCommit }: { onCommit: (next: string) => void }) { + return ; +} + +describe("AppRouteModal", () => { + it("commits a focused field when Esc closes it", async () => { + const onCommit = vi.fn(); + await using _screen = await render( + + + , + { router: "/settings/general" }, + ); + + await page.getByRole("textbox", { name: "Instructions" }).fill("Keep titles short"); + await userEvent.keyboard("{Escape}"); + + expect(onCommit).toHaveBeenCalledWith("Keep titles short"); + }); +}); diff --git a/apps/web/src/components/AppRouteModal.tsx b/apps/web/src/components/AppRouteModal.tsx index 89e676da2e3c3..55d7c4112f968 100644 --- a/apps/web/src/components/AppRouteModal.tsx +++ b/apps/web/src/components/AppRouteModal.tsx @@ -27,7 +27,12 @@ export function AppRouteModal({ label, children }: { label: string; children: Re // Portaled layers outside this tree (toasts, Clerk, confirm dialogs) are not backdrop presses. disablePointerDismissal onOpenChange={(nextOpen) => { - if (!nextOpen) setOpen(false); + if (nextOpen) return; + // Closing unmounts a focused field without a blur, so commit-on-blur rows would + // drop what was typed. Blur first so they commit. + const focused = document.activeElement; + if (focused instanceof HTMLElement && popupRef.current?.contains(focused)) focused.blur(); + setOpen(false); }} onOpenChangeComplete={(nextOpen) => { if (!nextOpen) void closeAppModal(); diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx index e16b235712e46..5d70470153661 100644 --- a/apps/web/src/components/ChatMarkdown.tsx +++ b/apps/web/src/components/ChatMarkdown.tsx @@ -79,8 +79,8 @@ import type { ExtraProps as ReactMarkdownExtraProps, Options as ReactMarkdownOptions, } from "react-markdown"; -import ReactMarkdown from "react-markdown"; import { createIncrementalMarkdownPlugin } from "../markdown-incremental"; +import type { Pluggable, PluggableList } from "unified"; import { defaultUrlTransform } from "react-markdown"; import { parseAssistantCitationHref } from "@t3tools/shared/assistantCitations"; import { parseComposerContextHref } from "@t3tools/shared/composerContextReferences"; @@ -148,6 +148,7 @@ import { useOpenLink } from "../browser/useOpenLink"; import { writeTextToClipboard } from "../hooks/useCopyToClipboard"; import { PullRequestLinkPreview } from "./pullRequest/PullRequestLinkPreview"; import { MarkdownCodeBlock, SuspenseShikiCodeBlock } from "./chat/MarkdownCodeBlock"; +import { createMarkdownRenderer } from "./chat/markdownRenderer"; import { splitMarkdownBlocks } from "./chat/markdownStreamBlocks"; import { MarkdownMermaidCodeBlock } from "./chat/MarkdownMermaidCodeBlock"; import { MarkdownTable } from "./chat/MarkdownTable"; @@ -163,6 +164,7 @@ import { } from "./chat/ChatMarkdownMedia"; import { MarkdownExternalLinkContent } from "./chat/MarkdownExternalLink"; import { MarkdownFileLink } from "./chat/MarkdownFileLink"; +import { rehypeChipPunctuation } from "./chat/markdownChipPunctuation"; import { handleMarkdownFragmentClick, rehypeHeadingIds } from "./chat/markdownFragments"; import { rehypeTextDirection } from "./chat/markdownTextDirection"; import { hastHasText, hastPlainTextDeep, plainHastText } from "./chat/markdownHast.logic"; @@ -348,13 +350,23 @@ const CHAT_MARKDOWN_RENDER_REHYPE_PLUGINS = [ ...CHAT_MARKDOWN_REHYPE_PLUGINS, rehypeHeadingIds, rehypeTextDirection, + rehypeChipPunctuation, ] satisfies NonNullable; const CHAT_MARKDOWN_LITERAL_HTML_REHYPE_PLUGINS = [ rehypeHeadingIds, rehypeTextDirection, + rehypeChipPunctuation, ] satisfies NonNullable; +function markdownUrlTransform(href: string) { + if (parseAssistantCitationHref(href)) return href; + if (parseComposerContextHref(href)) return href; + if (parseThreadLinkHref(href)) return href; + if (isWindowsDrivePathHref(href)) return href; + return rewriteMarkdownFileUriHref(href) ?? defaultUrlTransform(href); +} + type MarkdownServerConfigFields = ReturnType; function markdownServerConfigFields(config: ServerConfig | null) { @@ -548,13 +560,6 @@ function useChatMarkdownState({ const { pullRequestsEnabled } = useAtomValue(markdownServerConfigFieldsAtom(environmentId)); const projects = useProjects(); const diffThemeName = resolveDiffThemeName(resolvedTheme); - const markdownUrlTransform = (href: string) => { - if (parseAssistantCitationHref(href)) return href; - if (parseComposerContextHref(href)) return href; - if (parseThreadLinkHref(href)) return href; - if (isWindowsDrivePathHref(href)) return href; - return rewriteMarkdownFileUriHref(href) ?? defaultUrlTransform(href); - }; const handleCopy = (event: ReactClipboardEvent) => { const selection = window.getSelection(); if (!selection || selection.isCollapsed || !event.clipboardData) return; @@ -751,7 +756,6 @@ function useChatMarkdownState({ componentState, handleCopy, markdownRef, - markdownUrlTransform, localMediaPreview, closeLocalMediaPreview, }; @@ -1113,7 +1117,8 @@ const CHAT_MARKDOWN_COMPONENTS = { chip ) : ( - {children} {chip} + {children} + {chip} ); }, @@ -1407,29 +1412,62 @@ const CHAT_MARKDOWN_COMPONENTS = { }, } satisfies Components; +const REMARK_MATH: Pluggable = [remarkMath, { singleDollarTextMath: true }]; +// Shared lists, so every message with the same options shares a processor and its cache. +const CHAT_MARKDOWN_REMARK_PLUGIN_SETS = { + plain: CHAT_MARKDOWN_REMARK_PLUGINS, + breaks: CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS, + math: [REMARK_MATH, ...CHAT_MARKDOWN_REMARK_PLUGINS], + mathBreaks: [REMARK_MATH, ...CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS], +} satisfies Record; + +const markdownRenderers = new WeakMap< + PluggableList, + WeakMap> +>(); + +function markdownRenderer(remarkPlugins: PluggableList, rehypePlugins: PluggableList) { + let byRehype = markdownRenderers.get(remarkPlugins); + if (!byRehype) markdownRenderers.set(remarkPlugins, (byRehype = new WeakMap())); + let renderer = byRehype.get(rehypePlugins); + if (!renderer) { + renderer = createMarkdownRenderer({ + remarkPlugins, + rehypePlugins, + components: CHAT_MARKDOWN_COMPONENTS, + urlTransform: markdownUrlTransform, + }); + byRehype.set(rehypePlugins, renderer); + } + return renderer; +} + const MarkdownSource = memo(function MarkdownSource({ text, remarkPlugins, rehypePlugins, - urlTransform, + shared, + cache, }: { text: string; - remarkPlugins: NonNullable; - rehypePlugins: NonNullable; - urlTransform: ReactMarkdownOptions["urlTransform"]; + remarkPlugins: PluggableList; + rehypePlugins: PluggableList; + /** The plugins are one of the shared sets. */ + shared: boolean; + /** The text will not change again; streamed text would only fill the cache with prefixes. */ + cache: boolean; }) { + // Per-instance plugins, such as the incremental parser, set up their state on every render. + const render = shared + ? markdownRenderer(remarkPlugins, rehypePlugins) + : createMarkdownRenderer({ + remarkPlugins, + rehypePlugins, + components: CHAT_MARKDOWN_COMPONENTS, + urlTransform: markdownUrlTransform, + }); return ( - - - {text} - - + {render(text, shared && cache)} ); }); @@ -1442,14 +1480,8 @@ function ChatMarkdown({ extraRemarkPlugins = EMPTY_REMARK_PLUGINS, ...props }: ChatMarkdownProps) { - const { - componentState, - handleCopy, - markdownRef, - markdownUrlTransform, - localMediaPreview, - closeLocalMediaPreview, - } = useChatMarkdownState({ text, inline, ...props }); + const { componentState, handleCopy, markdownRef, localMediaPreview, closeLocalMediaPreview } = + useChatMarkdownState({ text, inline, ...props }); // Streaming re-renders only the last block; offsets for task edits need the whole text. const streamingBlocks = props.isStreaming === true && @@ -1467,13 +1499,26 @@ function ChatMarkdown({ const bindMarkdownRef = (node: HTMLElement | null) => { markdownRef.current = node; }; - const remarkPlugins = [ - ...(lineBreaks ? CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS : CHAT_MARKDOWN_REMARK_PLUGINS), - ...extraRemarkPlugins, - ...(incrementalParsing ? [createIncrementalMarkdownPlugin()] : []), - ]; - if (componentState.mathEnabled) - remarkPlugins.unshift([remarkMath, { singleDollarTextMath: true }]); + const sharedRemarkPlugins = + CHAT_MARKDOWN_REMARK_PLUGIN_SETS[ + componentState.mathEnabled + ? lineBreaks + ? "mathBreaks" + : "math" + : lineBreaks + ? "breaks" + : "plain" + ]; + // Extra and incremental plugins are per instance, so their output is not shared. + const uncachedPlugins = extraRemarkPlugins.length > 0 || incrementalParsing; + const remarkPlugins = uncachedPlugins + ? [ + ...sharedRemarkPlugins, + ...extraRemarkPlugins, + ...(incrementalParsing ? [createIncrementalMarkdownPlugin()] : []), + ] + : sharedRemarkPlugins; + const blocks = streamingBlocks ?? [text]; return ( - {(streamingBlocks ?? [text]).map((source, index) => ( + {blocks.map((source, index) => ( ))} diff --git a/apps/web/src/components/ChatView.logic.ts b/apps/web/src/components/ChatView.logic.ts index 88c1eb4f2d1aa..2ccd26ca2942b 100644 --- a/apps/web/src/components/ChatView.logic.ts +++ b/apps/web/src/components/ChatView.logic.ts @@ -480,6 +480,7 @@ export function resolveComposerProviderSelection(input: { : requestedInstanceId; return { selectedProviderEntry, + requestedInstanceId, requestedDriverKind, lockedContinuationGroupKey, unavailableProviderInstanceId, @@ -667,6 +668,7 @@ export function cloneComposerImageForRetry( export function deriveComposerSendState(options: { prompt: string; imageCount: number; + existingAttachmentCount?: number; terminalContexts: ReadonlyArray; elementContextCount?: number; }): { @@ -687,6 +689,7 @@ export function deriveComposerSendState(options: { hasSendableContent: trimmedPrompt.length > 0 || options.imageCount > 0 || + (options.existingAttachmentCount ?? 0) > 0 || sendableTerminalContexts.length > 0 || elementContextCount > 0, }; diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 88ce7aa18129f..11d54873754b7 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -2138,6 +2138,7 @@ export default function ChatView(props: ChatViewProps) { activeThread, activeThreadId, activeThreadRef, + threadDetailRef: routeThreadDetailRef, activeThreadKey, activeThreadShell, activeLatestRun, @@ -2159,7 +2160,6 @@ export default function ChatView(props: ChatViewProps) { setThreadError, beginLocalDispatch, resetLocalDispatch, - scheduleComposerFocus, scrollToEnd, persistThreadSettingsForNextTurn, }); diff --git a/apps/web/src/components/CommandPalette.composition.browser.test.tsx b/apps/web/src/components/CommandPalette.composition.browser.test.tsx new file mode 100644 index 0000000000000..765de6d0b99a4 --- /dev/null +++ b/apps/web/src/components/CommandPalette.composition.browser.test.tsx @@ -0,0 +1,140 @@ +import { describe, expect, it, vi } from "vite-plus/test"; +import { page, userEvent } from "vite-plus/test/browser"; +import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings"; +import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings"; +import { act, render } from "../../test/render"; +import { CommandPalette } from "./CommandPalette"; +import { openCommandPalette } from "../commandPaletteBus"; +import { primaryServerKeybindingsAtom } from "../state/server"; + +vi.mock("@effect/atom-react", async (original) => ({ + ...(await original()), + useAtomValue: (atom: unknown) => + atom === primaryServerKeybindingsAtom ? DEFAULT_RESOLVED_KEYBINDINGS : [], +})); +// Coder: Add project also lists each deployment's Coder workspaces, read from the Coder context. +vi.mock("../coder/CoderBootstrap", () => ({ + useCoder: () => ({ + config: { deployments: [], workspaces: [] }, + saveConfig: async () => undefined, + connectWorkspace: async () => undefined, + }), +})); +vi.mock("../hooks/useTheme", () => ({ + useRenderedAppearance: () => "light", + useTheme: () => ({ theme: "t3-chat", resolvedTheme: "light", appearanceMode: "light" }), +})); +vi.mock("../hooks/useCustomThemes", () => ({ useCustomThemes: () => [] })); +vi.mock("../hooks/useEnvironmentTheme", () => ({ useEnvironmentThemeDefinitions: () => [] })); +vi.mock("../hooks/useSettings", async (original) => ({ + ...(await original()), + useClientSettings: () => DEFAULT_CLIENT_SETTINGS, +})); +vi.mock("../hooks/useScratchProject", () => ({ + useScratchProject: () => ({ scratchEnvironmentId: () => null }), +})); +vi.mock("../hooks/useNewProject", () => ({ useNewProject: () => async () => undefined })); +vi.mock("../hooks/useHandleNewThread", () => ({ + useHandleNewThread: () => ({ + activeDraftThread: null, + activeThread: null, + defaultProjectRef: null, + handleNewThread: async () => undefined, + }), + useNewThreadHandler: () => async () => undefined, +})); +vi.mock("../hooks/useOpenPanelPullRequestUrl", () => ({ useOpenPanelPullRequestUrl: () => null })); +vi.mock("../connection/useDesktopLocalBootstraps", () => ({ useDesktopLocalBootstraps: () => [] })); +vi.mock("../state/use-atom-command", () => ({ + useAtomCommand: () => async () => ({ _tag: "Success", value: undefined }), +})); +vi.mock("../state/use-atom-query-runner", () => ({ + useAtomQueryRunner: () => async () => ({ _tag: "Success", value: undefined }), +})); +vi.mock("../state/environments", async (original) => ({ + ...(await original()), + useEnvironments: () => ({ + environments: [ + { + environmentId: "local", + label: "Local", + connection: { phase: "connected" }, + entry: { target: { _tag: "PrimaryConnectionTarget" } }, + serverConfig: { + environment: { platform: { os: "linux" }, capabilities: { projectCloneTracking: true } }, + settings: { addProjectBaseDirectory: "/workspace/" }, + }, + }, + ], + }), + usePrimaryEnvironmentId: () => "local", + useEnvironmentProviders: () => new Map(), +})); +vi.mock("../state/entities", async (original) => ({ + ...(await original()), + useProjects: () => [], + useThreadShells: () => [], + useServerConfigs: () => new Map(), + waitForProject: async () => undefined, +})); +vi.mock("../state/queries", async (original) => ({ + ...(await original()), + useThreadSearch: (_environmentIds: unknown, query: string) => ({ + matches: [], + query, + isPending: false, + }), +})); +vi.mock("../state/session", async (original) => ({ + ...(await original()), + useEnvironmentScope: () => true, + readEnvironmentScope: () => true, +})); +vi.mock("../state/filesystem", async (original) => ({ + ...(await original()), + useFilesystemReadAccess: () => ({ canReadFiles: true, isPending: false, error: null }), +})); +vi.mock("../state/query", async (original) => ({ + ...(await original()), + useEnvironmentQuery: () => ({ data: undefined, isLoading: false }), +})); +vi.mock("./settings/useAvailableSettingsSearchItems", () => ({ + useAvailableSettingsSearchItems: () => [], +})); + +// The real palette and Base UI input must agree that confirming composition is typing. +describe.each(["repository", "folder", "destination"] as const)("add-project %s", (step) => { + it.each([ + { isComposing: true, keyCode: 13 }, + { isComposing: false, keyCode: 229 }, + ])("keeps Enter in the input during composition: %j", async (composition) => { + await using _screen = await render({null}); + await act(() => openCommandPalette({ open: "add-project" })); + // Coder: Add project opens the workspace picker first. + await page.getByText("Local", { exact: true }).click(); + await page.getByText(step === "folder" ? "Local folder" : "Git URL", { exact: true }).click(); + const input = () => page.getByRole("combobox"); + if (step === "destination") { + await input().fill("https://example.com/team/project.git"); + await userEvent.keyboard("{Enter}"); + } + const value = step === "repository" ? "https://example.com/team/計画.git" : "/workspace/計画"; + await input().fill(value); + const event = new KeyboardEvent("keydown", { + key: "Enter", + bubbles: true, + cancelable: true, + ...composition, + }); + await act(() => input().element().dispatchEvent(event)); + expect(event.defaultPrevented).toBe(false); + await expect.element(input()).toHaveValue(value); + if (step === "repository") { + await userEvent.keyboard("{Enter}"); + await expect.element(input()).toHaveValue("/workspace/計画"); + } else { + await userEvent.keyboard("{Enter}"); + await expect.element(page.getByRole("combobox")).not.toBeInTheDocument(); + } + }); +}); diff --git a/apps/web/src/components/CommandPalette.logic.test.ts b/apps/web/src/components/CommandPalette.logic.test.ts index 7ec1a3e9ac1e3..8cf712b113cf7 100644 --- a/apps/web/src/components/CommandPalette.logic.test.ts +++ b/apps/web/src/components/CommandPalette.logic.test.ts @@ -4,6 +4,7 @@ import type { Thread } from "../types"; import { makeThreadFixture } from "../test-fixtures"; import { buildNestedSearchItems, + resolveCommandPaletteProject, buildThreadActionItems, buildLinkedThreadActionItems, filterPinnedBrowseEntries, @@ -918,3 +919,22 @@ describe("returning from file and content search", () => { expect(reduceCommandPaletteUiState(closed, { _tag: "SetOpen", open: true })).toEqual(root); }); }); + +it("resolves project metadata and browse roots within the active environment", () => { + const local = { title: "Local project", workspaceRoot: "/local/repo" }; + const remote = { title: "Remote project", workspaceRoot: "/remote/repo" }; + const projects = new Map([ + ["local:shared-project", local], + ["remote:shared-project", remote], + ]); + expect( + resolveCommandPaletteProject(projects, EnvironmentId.make("local"), PROJECT_ID), + ).toBeUndefined(); + const sharedId = ProjectId.make("shared-project"); + expect(resolveCommandPaletteProject(projects, EnvironmentId.make("local"), sharedId)).toBe(local); + expect(resolveCommandPaletteProject(projects, EnvironmentId.make("remote"), sharedId)).toBe( + remote, + ); + expect(resolveCommandPaletteProject(projects, null, sharedId)).toBeUndefined(); + expect(resolveCommandPaletteProject(projects, EnvironmentId.make("local"), null)).toBeUndefined(); +}); diff --git a/apps/web/src/components/CommandPalette.logic.ts b/apps/web/src/components/CommandPalette.logic.ts index 6102028889369..96d439252ac53 100644 --- a/apps/web/src/components/CommandPalette.logic.ts +++ b/apps/web/src/components/CommandPalette.logic.ts @@ -20,6 +20,7 @@ import { } from "@t3tools/client-runtime/operations/projects"; import { type EnvironmentId, + type ProjectId, type EnvironmentMachineKind, type FilesystemBrowseEntry, type KeybindingCommand, @@ -860,3 +861,11 @@ export function overlayModeForCommand(command: string | null): SearchOverlayMode ? OVERLAY_MODE_BY_COMMAND[command as keyof typeof OVERLAY_MODE_BY_COMMAND] : null; } + +export function resolveCommandPaletteProject( + projects: ReadonlyMap, + environmentId: EnvironmentId | null, + projectId: ProjectId | null, +) { + return environmentId && projectId ? projects.get(`${environmentId}:${projectId}`) : undefined; +} diff --git a/apps/web/src/components/CommandPalette.tsx b/apps/web/src/components/CommandPalette.tsx index 5de2f73e4f29e..13c1d635a9028 100644 --- a/apps/web/src/components/CommandPalette.tsx +++ b/apps/web/src/components/CommandPalette.tsx @@ -125,6 +125,7 @@ import { cn, isMacPlatform, isWindowsPlatform, newProjectId } from "../lib/utils import { buildThreadRouteParams } from "../threadRoutes"; import { useAvailableSettingsSearchItems } from "./settings/useAvailableSettingsSearchItems"; import { + resolveCommandPaletteProject, ADDON_ICON_CLASS, browseInputEndPaddingClass, buildBrowseGroups, @@ -571,7 +572,7 @@ function OpenCommandPaletteDialog(props: { return ensureBrowseDirectoryPath(baseDirectory); }; - const { projectCwdById, projectByKey, projectTitleById } = useProjectLookups(); + const { projectByKey } = useProjectLookups(); const activeThreadKey = activeThread ? scopedThreadKey(scopeThreadRef(activeThread.environmentId, activeThread.id)) @@ -583,9 +584,12 @@ function OpenCommandPaletteDialog(props: { activeThread?.environmentId ?? activeDraftThread?.environmentId ?? null; const currentProjectId = activeThread?.projectId ?? activeDraftThread?.projectId ?? null; const scratchTargetEnvironmentId = scratchEnvironmentId(currentProjectEnvironmentId); - const currentProjectCwd = currentProjectId - ? (projectCwdById.get(currentProjectId) ?? null) - : null; + const currentProject = resolveCommandPaletteProject( + projectByKey, + currentProjectEnvironmentId, + currentProjectId, + ); + const currentProjectCwd = currentProject?.workspaceRoot ?? null; const currentProjectCwdForBrowse = browseEnvironmentId && currentProjectEnvironmentId === browseEnvironmentId ? currentProjectCwd @@ -1199,7 +1203,8 @@ function OpenCommandPaletteDialog(props: { if (projects.length > 0) { const activeProjectTitle = projectPickerEntries.find((entry) => entry.isPreferred)?.group.displayName ?? - (currentProjectId ? (projectTitleById.get(currentProjectId) ?? null) : null); + currentProject?.title ?? + null; if (activeProjectTitle) { actionItems.push({ @@ -2206,6 +2211,12 @@ function OpenCommandPaletteDialog(props: { } function handleKeyDown(event: KeyboardEvent): void { + if (event.key === "Enter" && (event.nativeEvent.isComposing || event.keyCode === 229)) { + // Confirming an IME candidate must not submit a path or select a row. + (event as typeof event & { preventBaseUIHandler?: () => void }).preventBaseUIHandler?.(); + return; + } + const command = resolveShortcutCommand(event, keybindings, { platform: navigator.platform, context: { modelPickerOpen: false, editableFocus: true }, diff --git a/apps/web/src/components/CommandPaletteLookups.ts b/apps/web/src/components/CommandPaletteLookups.ts index a7623ffa93b03..1179958500fed 100644 --- a/apps/web/src/components/CommandPaletteLookups.ts +++ b/apps/web/src/components/CommandPaletteLookups.ts @@ -1,7 +1,6 @@ import { type ClientSettings, type EnvironmentId, - type ProjectId, resolveEnvironmentMachineKind, } from "@t3tools/contracts"; @@ -41,16 +40,10 @@ export function useEnvironmentLookups() { export function useProjectLookups() { const projects = useProjects(); - const projectCwdById = new Map( - projects.map((project) => [project.id, project.workspaceRoot]), - ); const projectByKey = new Map( projects.map((project) => [`${project.environmentId}:${project.id}`, project]), ); - const projectTitleById = new Map( - projects.map((project) => [project.id, project.title]), - ); - return { projectCwdById, projectByKey, projectTitleById }; + return { projectByKey }; } export const selectCommandPaletteClientSettings = (settings: ClientSettings) => ({ diff --git a/apps/web/src/components/ComposerContextPermissions.tsx b/apps/web/src/components/ComposerContextPermissions.tsx index 8ee7766edc565..b4ac2a2eb6983 100644 --- a/apps/web/src/components/ComposerContextPermissions.tsx +++ b/apps/web/src/components/ComposerContextPermissions.tsx @@ -26,6 +26,7 @@ export function ComposerContextPermissions(props: { size="xs" className="ms-auto min-w-0 shrink-0" aria-label={`Permissions: ${label}`} + aria-haspopup="menu" data-composer-context-control onClick={props.onOpen} /> diff --git a/apps/web/src/components/ContextChip.tsx b/apps/web/src/components/ContextChip.tsx index a49fcb59a9955..993ccedd22c5a 100644 --- a/apps/web/src/components/ContextChip.tsx +++ b/apps/web/src/components/ContextChip.tsx @@ -35,6 +35,10 @@ const contextChipVariants = cva( "doc-slides": "[--context-chip-accent:var(--context-chip-doc-slides)]", "doc-epub": "[--context-chip-accent:var(--context-chip-doc-epub)]", }, + shape: { + inline: "", + bubble: "w-64 gap-2 rounded-2xl bg-message px-3 py-2 text-sm text-message-foreground", + }, state: { unresolved: "data-[state=unresolved]:[--context-chip-accent:currentColor]", invalid: @@ -49,11 +53,12 @@ type ContextChipKind = NonNullable["kin interface ContextChipProps extends useRender.ComponentProps<"span"> { kind?: ContextChipKind; state?: "unresolved" | "invalid"; + shape?: "inline" | "bubble"; } -function ContextChip({ className, kind, state, render, ...props }: ContextChipProps) { +function ContextChip({ className, kind, state, shape, render, ...props }: ContextChipProps) { const defaultProps = { - className: cn(contextChipVariants({ kind, state }), className), + className: cn(contextChipVariants({ kind, state, shape }), className), "data-slot": "context-chip", "data-state": state, }; diff --git a/apps/web/src/components/DiffFilePathCopyButton.browser.test.tsx b/apps/web/src/components/DiffFilePathCopyButton.browser.test.tsx new file mode 100644 index 0000000000000..a2bec09ca960d --- /dev/null +++ b/apps/web/src/components/DiffFilePathCopyButton.browser.test.tsx @@ -0,0 +1,30 @@ +import { expect, it, vi } from "vite-plus/test"; +import { page } from "vite-plus/test/browser"; + +import { render } from "../../test/render"; +import { DiffFilePathCopyButton } from "./DiffFilePathCopyButton"; +import { AnchoredToastProvider } from "./ui/toast"; + +it("explains a denied clipboard write and lets the same path be copied again", async () => { + await using screen = await render( + + + , + ); + using write = vi + .spyOn(navigator.clipboard, "writeText") + .mockRejectedValueOnce(new DOMException("Clipboard access denied", "NotAllowedError")) + .mockResolvedValue(undefined); + using _console = vi.spyOn(console, "error").mockImplementation(() => undefined); + + const copy = screen.getByRole("button", { name: "Copy file path" }); + await copy.click(); + + await expect.element(page.getByText("Failed to copy text to the clipboard.")).toBeVisible(); + await page.getByRole("button", { name: "Dismiss notification" }).click(); + await copy.click(); + + await expect.element(page.getByText("Copied!", { exact: true })).toBeVisible(); + expect(write).toHaveBeenCalledTimes(2); + expect(write).toHaveBeenLastCalledWith("apps/web/src/index.ts"); +}); diff --git a/apps/web/src/components/GitActionsControl.logic.test.ts b/apps/web/src/components/GitActionsControl.logic.test.ts index 3a7892759346a..49b2bf83a9035 100644 --- a/apps/web/src/components/GitActionsControl.logic.test.ts +++ b/apps/web/src/components/GitActionsControl.logic.test.ts @@ -94,6 +94,36 @@ describe("when: on default ref without open PR", () => { }); }); +describe("when: local changes are behind upstream", () => { + it("keeps the commit action available without pushing, with or without an open PR", () => { + const behind = status({ hasWorkingTreeChanges: true, behindCount: 2 }); + const commit = { + label: "Commit", + disabled: false, + kind: "run_action", + action: "commit", + } as const; + assert.deepEqual(resolveQuickAction(behind, false), commit); + assert.deepEqual( + resolveQuickAction( + { + ...behind, + pr: { + number: 13, + title: "Open PR", + url: "https://example.com/pr/13", + baseRef: "main", + headRef: "feature/test", + state: "open", + }, + }, + false, + ), + commit, + ); + }); +}); + describe("when: ref has no upstream configured", () => { it("resolveQuickAction runs push and create PR when no upstream and commits are ahead", () => { const quick = resolveQuickAction( diff --git a/apps/web/src/components/GitActionsControl.logic.ts b/apps/web/src/components/GitActionsControl.logic.ts index 65123285a78fc..98a55c3577e01 100644 --- a/apps/web/src/components/GitActionsControl.logic.ts +++ b/apps/web/src/components/GitActionsControl.logic.ts @@ -212,7 +212,7 @@ export function resolveQuickAction( } if (hasChanges) { - if (!gitStatus.hasUpstream && !hasPrimaryRemote) { + if (isBehind || (!gitStatus.hasUpstream && !hasPrimaryRemote)) { return { label: "Commit", disabled: false, kind: "run_action", action: "commit" }; } if (hasOpenPr || isDefaultRef) { diff --git a/apps/web/src/components/PersistentThreadTerminals.tsx b/apps/web/src/components/PersistentThreadTerminals.tsx index 5e2fe0dbe91f5..a387349b4c038 100644 --- a/apps/web/src/components/PersistentThreadTerminals.tsx +++ b/apps/web/src/components/PersistentThreadTerminals.tsx @@ -294,7 +294,7 @@ export interface PersistentThreadTerminalPanelProps { onSplitTerminalVertical: () => void; onNewTerminal: () => void; onActiveTerminalChange: (terminalId: string) => void; - onCloseTerminal: (terminalId: string) => void; + onCloseTerminal: (terminalId: string, threadRef?: ScopedThreadRef) => void; splitShortcutLabel?: string | undefined; splitVerticalShortcutLabel?: string | undefined; newShortcutLabel?: string | undefined; diff --git a/apps/web/src/components/ProjectFavicon.browser.test.tsx b/apps/web/src/components/ProjectFavicon.browser.test.tsx new file mode 100644 index 0000000000000..746302ef5b242 --- /dev/null +++ b/apps/web/src/components/ProjectFavicon.browser.test.tsx @@ -0,0 +1,57 @@ +import { RegistryContext } from "@effect/atom-react"; +import { EnvironmentId } from "@t3tools/contracts"; +import { Atom, AtomRegistry } from "effect/reactivity"; +import { expect, it, vi } from "vite-plus/test"; + +import { render } from "../../test/render"; +import { ProjectFavicon, type ProjectFaviconProject } from "./ProjectFavicon"; + +const state = vi.hoisted(() => ({ favicon: undefined as Atom.Atom | undefined })); +vi.mock(import("../state/assets"), () => ({ + projectFaviconUrlAtom: () => { + if (!state.favicon) throw new Error("Favicon resource not configured"); + return state.favicon; + }, +})); + +function setup() { + const activeResources = new Set(); + state.favicon = Atom.make((get) => { + activeResources.add("favicon"); + get.addFinalizer(() => activeResources.delete("favicon")); + return null; + }); + return { activeResources }; +} + +it("only activates the favicon resource after removing a custom icon", async () => { + const { activeResources } = setup(); + const registry = AtomRegistry.make(); + const project: ProjectFaviconProject = { + environmentId: EnvironmentId.make("test"), + workspaceRoot: "/project", + title: "Project", + faviconPath: "icon.png", + projectIcon: { kind: "monogram", text: "PR", color: "blue" }, + }; + const view = (projectIcon: ProjectFaviconProject["projectIcon"]) => ( + + + + ); + try { + await using screen = await render(view(project.projectIcon)); + expect(activeResources.size).toBe(0); + await screen.rerender(view({ kind: "emoji", emoji: "🌱" })); + expect(activeResources.size).toBe(0); + await screen.rerender(view({ kind: "lucide", name: "leaf", color: "green" })); + expect(activeResources.size).toBe(0); + await screen.rerender(view(null)); + expect(activeResources.size).toBe(1); + await screen.rerender(view(project.projectIcon)); + await screen.rerender(view(null)); + expect(activeResources.size).toBe(1); + } finally { + registry.dispose(); + } +}); diff --git a/apps/web/src/components/ProjectFavicon.tsx b/apps/web/src/components/ProjectFavicon.tsx index 14a00ae86583b..3545a0aa5db87 100644 --- a/apps/web/src/components/ProjectFavicon.tsx +++ b/apps/web/src/components/ProjectFavicon.tsx @@ -38,13 +38,6 @@ export function ProjectFavicon(input: { fallbackIcon?: ComponentType<{ className?: string }>; }) { const { project } = input; - const src = useAtomValue( - projectFaviconUrlAtom({ - environmentId: project.environmentId, - cwd: project.workspaceRoot, - faviconPath: project.faviconPath, - }), - ); if (project.projectIcon?.kind === "monogram") { return ( ); } + return ; +} + +function AutomaticProjectFavicon(input: Parameters[0]) { + const { project } = input; + const src = useAtomValue( + projectFaviconUrlAtom({ + environmentId: project.environmentId, + cwd: project.workspaceRoot, + faviconPath: project.faviconPath, + }), + ); const FallbackIcon = input.fallbackIcon ?? FolderCodeIcon; if (!src || isProjectFaviconFallbackUrl(src)) { diff --git a/apps/web/src/components/Sidebar.logic.test.ts b/apps/web/src/components/Sidebar.logic.test.ts index e4fa68a65201b..68d8577690643 100644 --- a/apps/web/src/components/Sidebar.logic.test.ts +++ b/apps/web/src/components/Sidebar.logic.test.ts @@ -23,6 +23,7 @@ import { sortInboxThreadsByReturn, resolveSidebarDropTarget, planSidebarThreadDrop, + resolveSidebarReorderableThreadKeys, sortPinnedThreadsForSidebar, sortProjectsForSidebar, sortScopedProjectsForSidebar, @@ -36,6 +37,11 @@ import { resolveSidebarFolderDot, sidebarFolderExpansionKeys, } from "./Sidebar.logic"; +import { scopeThreadRef, scopedThreadKey } from "@t3tools/client-runtime/environment"; +import { + sortActiveThreadsByOrderKey, + sortPinnedThreadsByOrderKey, +} from "@t3tools/client-runtime/state/thread-sort"; import { resolveProjectExpanded } from "../uiStateStore"; import { threadSearchMatchKey } from "@t3tools/client-runtime/state/thread-search"; import { EnvironmentId, ProjectId, ProviderInstanceId, RunId, ThreadId } from "@t3tools/contracts"; @@ -1230,6 +1236,240 @@ describe("Working shelf (beta)", () => { expect(above.kind === "move-active" && above.assignments[0]!.orderKey < "m").toBe(true); }); + describe("reordering with cached offline configuration", () => { + const onlineEnvironmentId = EnvironmentId.make("online"); + const offlineEnvironmentId = EnvironmentId.make("offline"); + const capabilities = { + repositoryIdentity: true, + threadPinning: true, + threadPinReorder: true, + threadActiveReorder: true, + }; + const config = (environmentId: EnvironmentId) => ({ + environment: { + environmentId, + label: environmentId, + platform: { os: "linux" as const, arch: "x64" as const }, + serverVersion: "0.1.1", + capabilities, + }, + }); + const serverConfigs = new Map([ + [onlineEnvironmentId, config(onlineEnvironmentId)], + [offlineEnvironmentId, config(offlineEnvironmentId)], + ]); + const thread = ( + id: string, + environmentId = onlineEnvironmentId, + key: string | null = null, + ) => ({ + id: ThreadId.make(id), + environmentId, + createdAt: `2026-03-09T${id === "offline" ? "12" : id === "online" ? "11" : "10"}:00:00.000Z`, + activeOrderKey: key, + pinOrderKey: key, + }); + const keyOf = (entry: ReturnType) => + scopedThreadKey(scopeThreadRef(entry.environmentId, entry.id)); + const online = thread("online"); + const offline = thread("offline", offlineEnvironmentId); + const membership = (connectedEnvironmentIds = [onlineEnvironmentId]) => + resolveSidebarReorderableThreadKeys({ + threads: [online, offline], + serverConfigs, + connectedEnvironmentIds, + }); + const planDrop = (input: { + section: "active" | "pinned"; + rows: ReturnType[]; + moved: ReturnType; + order: ReturnType[]; + from?: "active" | "pinned"; + connectedEnvironmentIds?: EnvironmentId[]; + }) => { + const writable = resolveSidebarReorderableThreadKeys({ + threads: input.rows, + serverConfigs, + connectedEnvironmentIds: input.connectedEnvironmentIds ?? [onlineEnvironmentId], + }); + const sort = (rows: typeof input.rows) => + input.section === "active" + ? sortActiveThreadsByOrderKey(rows) + : sortPinnedThreadsByOrderKey(rows); + const originalOrder = sort(input.rows).map(keyOf); + const targetOrder = input.order.map(keyOf); + const keysById = new Map(input.rows.map((entry) => [keyOf(entry), entry.activeOrderKey])); + const plan = planSidebarThreadDrop({ + activeKey: keyOf(input.moved), + activeSection: input.from ?? input.section, + target: { section: input.section, pinnedOrder: targetOrder, activeOrder: targetOrder }, + pinnedOrder: originalOrder, + activeOrder: originalOrder, + pinnedKeysById: keysById, + activeKeysById: keysById, + reorderableKeys: writable.draggableThreadKeys, + activeReorderableKeys: writable.activeReorderableThreadKeys, + }); + const assignments = + plan.kind === "move-active" || plan.kind === "reorder-pinned" + ? plan.assignments + : plan.kind === "pin" + ? [ + ...plan.extraAssignments, + ...(plan.orderKey === undefined + ? [] + : [{ id: keyOf(input.moved), orderKey: plan.orderKey }]), + ] + : []; + const assignedKeys = new Map(assignments.map(({ id, orderKey }) => [id, orderKey])); + const actualOrder = sort( + input.rows.map((entry) => ({ + ...entry, + activeOrderKey: assignedKeys.get(keyOf(entry)) ?? entry.activeOrderKey, + pinOrderKey: assignedKeys.get(keyOf(entry)) ?? entry.pinOrderKey, + })), + ).map(keyOf); + + return { plan, assignments, actualOrder, targetOrder }; + }; + + it("excludes cached capable offline rows from both writable sets", () => { + const writable = membership(); + + expect([...writable.draggableThreadKeys]).toEqual([keyOf(online)]); + expect([...writable.activeReorderableThreadKeys]).toEqual([keyOf(online)]); + expect(serverConfigs.has(offlineEnvironmentId)).toBe(true); + }); + + it("updates membership through disconnect, reconnect, and missing configuration", () => { + expect(membership([]).draggableThreadKeys.size).toBe(0); + const connected = membership([onlineEnvironmentId, offlineEnvironmentId]); + expect(connected.draggableThreadKeys.size).toBe(2); + expect(connected.activeReorderableThreadKeys.size).toBe(2); + + const changed = resolveSidebarReorderableThreadKeys({ + threads: [online, offline], + connectedEnvironmentIds: [onlineEnvironmentId, offlineEnvironmentId], + serverConfigs: new Map([[onlineEnvironmentId, config(onlineEnvironmentId)]]), + }); + expect([...changed.draggableThreadKeys]).toEqual([keyOf(online)]); + expect([...changed.activeReorderableThreadKeys]).toEqual([keyOf(online)]); + }); + + it.each(["active", "pinned"] as const)( + "replans a disconnected drag source in %s", + (section) => { + const bound = thread("offline", offlineEnvironmentId, "m"); + const moved = thread("online", onlineEnvironmentId, "t"); + const input = { section, rows: [bound, moved], moved, order: [moved, bound] }; + expect(planDrop(input).plan.kind).toBe( + section === "active" ? "move-active" : "reorder-pinned", + ); + + const disconnected = planDrop({ ...input, connectedEnvironmentIds: [] }); + + expect(disconnected.plan).toEqual({ kind: "none" }); + expect(disconnected.assignments).toEqual([]); + }, + ); + + it.each(["active", "pinned"] as const)("preserves a keyed offline bound in %s", (section) => { + const bound = thread("offline", offlineEnvironmentId, "m"); + const moved = thread("online", onlineEnvironmentId, "t"); + const result = planDrop({ section, rows: [bound, moved], moved, order: [moved, bound] }); + + expect(result.plan.kind).toBe(section === "active" ? "move-active" : "reorder-pinned"); + expect(result.assignments.map(({ id }) => id)).toEqual([keyOf(moved)]); + expect(result.actualOrder).toEqual(result.targetOrder); + }); + + it.each(["active", "pinned"] as const)( + "rejects rebalancing a keyed offline bound in %s", + (section) => { + const bound = thread("offline", offlineEnvironmentId, "m"); + const neighbor = thread("neighbor"); + const result = planDrop({ + section, + rows: [bound, online, neighbor], + moved: online, + order: [bound, neighbor, online], + }); + + expect(result.plan).toEqual({ kind: "none" }); + expect(result.assignments).toEqual([]); + }, + ); + + it.each(["active", "pinned"] as const)( + "rejects crossing the frozen keyless boundary in %s", + (section) => { + const bound = { + ...offline, + createdAt: + section === "active" ? "2026-03-09T12:00:00.000Z" : "2026-03-09T10:00:00.000Z", + }; + const order = section === "active" ? [online, bound] : [bound, online]; + const result = planDrop({ section, rows: [bound, online], moved: online, order }); + + expect(result.plan).toEqual({ kind: "none" }); + expect(result.assignments).toEqual([]); + }, + ); + + it.each(["active", "pinned"] as const)( + "allows realizable moves beside a frozen keyless row in %s", + (section) => { + const neighbor = thread("neighbor"); + const order = + section === "active" ? [offline, neighbor, online] : [online, neighbor, offline]; + const result = planDrop({ + section, + rows: [offline, online, neighbor], + moved: online, + order, + }); + + expect(result.plan.kind).toBe(section === "active" ? "move-active" : "reorder-pinned"); + expect(result.assignments.length).toBeGreaterThan(0); + expect(result.assignments.every(({ id }) => id !== keyOf(offline))).toBe(true); + expect(result.actualOrder).toEqual(result.targetOrder); + }, + ); + + it.each(["active", "pinned"] as const)( + "checks fallback assignments to other rows at the %s boundary", + (section) => { + const neighbor = thread("neighbor"); + const order = + section === "active" ? [neighbor, offline, online] : [online, offline, neighbor]; + const result = planDrop({ + section, + rows: [offline, online, neighbor], + moved: online, + order, + }); + + expect(result.plan).toEqual({ kind: "none" }); + }, + ); + + it.each(["active", "pinned"] as const)( + "rejects impossible pin/unpin placement across the %s boundary", + (section) => { + const order = section === "active" ? [online, offline] : [offline, online]; + const result = planDrop({ + section, + rows: [offline, online], + moved: online, + order, + from: section === "active" ? "pinned" : "active", + }); + + expect(result.plan).toEqual({ kind: "none" }); + }, + ); + }); + it("only changes lifecycle when the inbox is time-ordered", () => { const base = { pinnedOrder: ["p1"], diff --git a/apps/web/src/components/Sidebar.logic.ts b/apps/web/src/components/Sidebar.logic.ts index e8b841a6c339b..73e9a20bc6061 100644 --- a/apps/web/src/components/Sidebar.logic.ts +++ b/apps/web/src/components/Sidebar.logic.ts @@ -1,3 +1,5 @@ +import type { EnvironmentServerConfig } from "@t3tools/client-runtime/connection"; +import { scopeThreadRef, scopedThreadKey } from "@t3tools/client-runtime/environment"; import { resolveThreadWorkingStartedAt } from "@t3tools/client-runtime/state/models"; import { isShownUnderParent } from "@t3tools/client-runtime/state/threads"; import { backgroundWorkHoldsCompletion } from "@t3tools/shared/orchestrationV2PendingBackgroundWork"; @@ -310,6 +312,32 @@ export function sidebarThreadKeyAtY(list: HTMLElement, y: number): string | null return key; } +export function resolveSidebarReorderableThreadKeys(input: { + readonly threads: ReadonlyArray>; + readonly serverConfigs: ReadonlyMap>; + readonly connectedEnvironmentIds: readonly EnvironmentId[]; +}) { + const connectedEnvironmentIds = new Set(input.connectedEnvironmentIds); + const draggableThreadKeys = new Set(); + const activeReorderableThreadKeys = new Set(); + + for (const thread of input.threads) { + // Config is cached for offline rendering, so its presence does not make a row writable. + if ( + !connectedEnvironmentIds.has(thread.environmentId) || + !input.serverConfigs.has(thread.environmentId) + ) { + continue; + } + + const threadKey = scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)); + activeReorderableThreadKeys.add(threadKey); + draggableThreadKeys.add(threadKey); + } + + return { draggableThreadKeys, activeReorderableThreadKeys }; +} + export function planSidebarThreadDrop(input: { readonly activeKey: string; readonly activeSection: SidebarSection; @@ -354,7 +382,28 @@ export function planSidebarThreadDrop(input: { keysById, movedId: activeKey, }); - return assignments.every(({ id }) => writable.has(id)) ? assignments : null; + if (assignments.some(({ id }) => !writable.has(id))) return null; + + // Keyless Active rows sort before keyed rows; keyless Pinned rows sort after them. + // Materializing any row across a frozen keyless boundary cannot persist that position. + const assignedIds = new Set(assignments.map(({ id }) => id)); + let hasFrozenKeylessBefore = false; + let hasAssignedBefore = false; + for (const id of order) { + const isFrozenKeyless = !writable.has(id) && keysById.get(id) == null; + const isAssigned = assignedIds.has(id); + if ( + (target.section === "active" && isFrozenKeyless && hasAssignedBefore) || + (target.section === "pinned" && isAssigned && hasFrozenKeylessBefore) + ) { + return null; + } + + hasFrozenKeylessBefore ||= isFrozenKeyless; + hasAssignedBefore ||= isAssigned; + } + + return assignments; }; switch (target.section) { case "active": { diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsx index 519b41caa2028..93245e9e70268 100644 --- a/apps/web/src/components/Sidebar.tsx +++ b/apps/web/src/components/Sidebar.tsx @@ -186,6 +186,7 @@ import { resolveSidebarDropTarget, resolveSidebarDropVerb, resolveSidebarThreadSection, + resolveSidebarReorderableThreadKeys, searchSidebarThreads, shouldCreateNewThreadInCurrentProject, shouldNavigateAfterThreadPark, @@ -336,6 +337,7 @@ interface SidebarThreadPartitionInput { /** Tasks linked environments run for a listed thread; they show under it. */ readonly delegatedUnderParent: ReadonlySet; readonly serverConfigs: ReadonlyMap; + readonly connectedEnvironmentIds: readonly EnvironmentId[]; readonly optimisticDrop: SidebarOptimisticDrop | null; readonly workingShelfEnabled: boolean; /** Boring mode: folders list active threads by the user's latest message. */ @@ -396,6 +398,7 @@ function useSidebarThreads(input: { readonly projectScopeKey: string | null; readonly scopedProjectKeys: ReadonlySet | null; readonly serverConfigs: ReadonlyMap; + readonly connectedEnvironmentIds: readonly EnvironmentId[]; readonly optimisticDrop: SidebarOptimisticDrop | null; readonly threads: readonly EnvironmentThreadShell[]; readonly delegatedUnderParent: ReadonlySet; @@ -409,6 +412,7 @@ function useSidebarThreads(input: { projectScopeKey, scopedProjectKeys, serverConfigs, + connectedEnvironmentIds, optimisticDrop, threads, delegatedUnderParent, @@ -459,6 +463,7 @@ function useSidebarThreads(input: { scopedProjectKeys, delegatedUnderParent, serverConfigs, + connectedEnvironmentIds, optimisticDrop, workingShelfEnabled, sortActiveBySend: folderGroups !== null, @@ -611,15 +616,14 @@ function partitionSidebarThreads(input: SidebarThreadPartitionInput) { : active; const snoozed: EnvironmentThreadShell[] = []; const settled: EnvironmentThreadShell[] = []; - const draggable = new Set(); - const activeReorderable = new Set(); + const { draggableThreadKeys, activeReorderableThreadKeys } = resolveSidebarReorderableThreadKeys({ + threads: visible, + serverConfigs, + connectedEnvironmentIds: input.connectedEnvironmentIds, + }); for (const thread of visible) { const configLoaded = serverConfigs.has(thread.environmentId); const threadKey = scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)); - if (configLoaded) { - activeReorderable.add(threadKey); - draggable.add(threadKey); - } if (optimisticDrop?.key === threadKey) { const projected = applySidebarThreadDrop( thread, @@ -653,7 +657,7 @@ function partitionSidebarThreads(input: SidebarThreadPartitionInput) { ).push(thread); } } - // A loaded config gates dragging only; the sort must not depend on it, so web and mobile agree. + // Availability gates dragging only; sorting must stay consistent across clients. const sortedPinned = sortPinnedThreadsForSidebar(pinned); const sortedActive = sortActiveBySend ? sortWorkingThreadsBySend(active) @@ -669,8 +673,8 @@ function partitionSidebarThreads(input: SidebarThreadPartitionInput) { preferredIds: optimisticDrop.order, getId: (thread) => scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)), }), - draggableThreadKeys: draggable, - activeReorderableThreadKeys: activeReorderable, + draggableThreadKeys, + activeReorderableThreadKeys, activeThreads: optimisticDrop?.section !== "active" || optimisticDrop.order === null ? sortedActive @@ -998,7 +1002,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve const threadSearchInputRef = useRef(null); const [threadSearchQuery, setThreadSearchQuery] = useState(""); const [searchHighlightKey, setSearchHighlightKey] = useState(null); - const searchEnvironmentIds = useConnectedEnvironmentIds(); + const connectedEnvironmentIds = useConnectedEnvironmentIds(); const queuePendingFileDrop = useSidebarPendingFileDropStore((s) => s.queuePendingFileDrop); const clearPendingFileDrop = useSidebarPendingFileDropStore((s) => s.clearPendingFileDrop); const [renamingThreadRef, setRenamingThreadRef] = useState(null); @@ -1142,7 +1146,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve const scopedProjectRefs = scopedProjectGroups.flatMap((project) => project.memberProjectRefs); const scopedProjectKeys = revealWidensScope ? null : chosenScopeProjectKeys; const threadSearch = useThreadSearch( - searchEnvironmentIds, + connectedEnvironmentIds, threadSearchQuery, scopedProjectGroups.length === 0 ? {} @@ -1274,6 +1278,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve projectScopeKey, scopedProjectKeys, serverConfigs, + connectedEnvironmentIds, optimisticDrop, threads, delegatedUnderParent, diff --git a/apps/web/src/components/ThreadTerminalDrawer.browser.test.tsx b/apps/web/src/components/ThreadTerminalDrawer.browser.test.tsx new file mode 100644 index 0000000000000..a99434f6659c6 --- /dev/null +++ b/apps/web/src/components/ThreadTerminalDrawer.browser.test.tsx @@ -0,0 +1,129 @@ +import { EMPTY_TERMINAL_SESSION_STATE } from "@t3tools/client-runtime/state/terminal"; +import { EnvironmentId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts"; +import { expect, it, vi } from "vite-plus/test"; +import { act, fakeTimers, render } from "../../test/render"; +import ThreadTerminalDrawer from "./ThreadTerminalDrawer"; + +const fixture = vi.hoisted(() => ({ + statusByThreadId: new Map(), + pendingSurfaces: [] as Array<() => void>, +})); + +// Every surface method is a no-op; `then` stays undefined so awaiting the surface resolves to it. +const fakeSurface: object = new Proxy( + { input: document.createElement("textarea") }, + { + get: (target, key) => + key === "then" ? undefined : key in target ? Reflect.get(target, key) : () => undefined, + }, +); +vi.mock("~/terminal/ghostty/surface", () => ({ + GhosttyTerminalSurface: { + create: () => + new Promise((resolve) => { + fixture.pendingSurfaces.push(() => resolve(fakeSurface)); + }), + }, +})); +vi.mock("../state/terminalSessions", async () => { + const Atom = await import("effect/reactivity/Atom"); + const sessions = Atom.family((_environmentId: EnvironmentId) => + Atom.family((threadId: ThreadId) => + Atom.make({ + ...EMPTY_TERMINAL_SESSION_STATE, + status: fixture.statusByThreadId.get(threadId) ?? "closed", + version: 1, + }), + ), + ); + return { + attachedTerminalSessionAtom: (environmentId: EnvironmentId, terminal: { threadId: ThreadId }) => + sessions(environmentId)(terminal.threadId), + }; +}); +vi.mock("../state/session", () => ({ + useEnvironmentScope: () => true, + readEnvironmentScope: () => true, +})); +vi.mock("../state/use-atom-command", () => ({ + useAtomCommand: () => async () => ({ _tag: "Success" }), +})); +// The commands are replaced above; importing their runtime would connect a client. +vi.mock("../state/server", async () => { + const Atom = await import("effect/reactivity/Atom"); + const config = Atom.make(null); + return { serverEnvironment: { configValueAtom: () => config } }; +}); +vi.mock("../state/terminal", () => ({ terminalEnvironment: { write: {}, resize: {} } })); +vi.mock("../state/preview", () => ({ previewEnvironment: { open: {} } })); +vi.mock("../editorPreferences", () => ({ useOpenInPreferredEditor: () => () => {} })); +vi.mock("./preview/openTerminalLinkInPreview", () => ({ openTerminalLinkInPreview: () => {} })); +vi.mock("~/localApi", () => ({ readLocalApi: () => null })); +vi.mock("../hooks/useSettings", async () => { + const { DEFAULT_CLIENT_SETTINGS } = await import("@t3tools/contracts/settings"); + return { + useClientSettings: (select: (settings: typeof DEFAULT_CLIENT_SETTINGS) => T) => + select(DEFAULT_CLIENT_SETTINGS), + }; +}); + +function Drawer(props: { + threadRef: ScopedThreadRef; + onCloseTerminal: (terminalId: string, threadRef?: ScopedThreadRef) => void; +}) { + return ( + {}} + onSplitTerminalVertical={() => {}} + onNewTerminal={() => {}} + onActiveTerminalChange={() => {}} + onCloseTerminal={props.onCloseTerminal} + onHeightChange={() => {}} + onAddTerminalContext={() => {}} + keybindings={[]} + /> + ); +} + +function setup() { + const original: ScopedThreadRef = { + environmentId: EnvironmentId.make("environment-a"), + threadId: ThreadId.make("thread-a"), + }; + const other: ScopedThreadRef = { + environmentId: EnvironmentId.make("environment-b"), + threadId: ThreadId.make("thread-b"), + }; + fixture.statusByThreadId = new Map([ + [original.threadId, "exited"], + [other.threadId, "running"], + ]); + fixture.pendingSurfaces = []; + return { original, other }; +} + +it("closes an exited terminal on its own thread after navigating to one reusing its ID", async () => { + const { original, other } = setup(); + const onCloseTerminal = vi.fn(); + await using screen = await render( + , + ); + using _timers = fakeTimers(); + // The exit is seen once the surface loads, and its close waits for the next task. + await act(async () => { + fixture.pendingSurfaces.shift()?.(); + }); + await screen.rerender(); + await act(async () => { + vi.runOnlyPendingTimers(); + }); + expect(onCloseTerminal.mock.calls).toEqual([["term-1", original]]); +}); diff --git a/apps/web/src/components/ThreadTerminalDrawer.test.ts b/apps/web/src/components/ThreadTerminalDrawer.test.ts index 9979d3563ff88..83358c39d8260 100644 --- a/apps/web/src/components/ThreadTerminalDrawer.test.ts +++ b/apps/web/src/components/ThreadTerminalDrawer.test.ts @@ -40,7 +40,13 @@ describe("terminal selection actions", () => { }); it("retains visible output and selection until a replacement subscription receives its snapshot", () => { - const terminal = { resetAndWrite: vi.fn(), write: vi.fn(), clearSelection: vi.fn() }; + const terminal = { + resetAndWrite: vi.fn(), + write: vi.fn(), + getSelection: vi.fn(() => "selected"), + getSelectionCellCount: vi.fn(() => 8), + clearSelection: vi.fn(), + }; const snapshot = { threadId: "thread", terminalId: "term-1", @@ -90,7 +96,13 @@ it("retains visible output and selection until a replacement subscription receiv }); it("reports an attach failure that leaves the output unchanged", () => { - const terminal = { resetAndWrite: vi.fn(), write: vi.fn(), clearSelection: vi.fn() }; + const terminal = { + resetAndWrite: vi.fn(), + write: vi.fn(), + getSelection: vi.fn(() => "selected"), + getSelectionCellCount: vi.fn(() => 8), + clearSelection: vi.fn(), + }; const previous = { ...EMPTY_TERMINAL_BUFFER_STATE, error: null }; const failed = { ...previous, error: "Terminal attach failed" }; @@ -99,5 +111,7 @@ it("reports an attach failure that leaves the output unchanged", () => { ).toBe(INITIAL_TERMINAL_OUTPUT_CURSOR); expect(terminal.write).toHaveBeenCalledWith("\r\n[terminal] Terminal attach failed\r\n"); expect(terminal.resetAndWrite).not.toHaveBeenCalled(); + expect(terminal.getSelection).not.toHaveBeenCalled(); + expect(terminal.getSelectionCellCount).not.toHaveBeenCalled(); expect(terminal.clearSelection).not.toHaveBeenCalled(); }); diff --git a/apps/web/src/components/ThreadTerminalDrawer.tsx b/apps/web/src/components/ThreadTerminalDrawer.tsx index 8247ab63ee44e..1fb3efc89567e 100644 --- a/apps/web/src/components/ThreadTerminalDrawer.tsx +++ b/apps/web/src/components/ThreadTerminalDrawer.tsx @@ -158,21 +158,33 @@ export function writeTerminalOutputUpdate( } } +// Avoid formatting unbounded scrollback selections on every output update. +const MAX_COMPARED_SELECTION_CELLS = 3_000; + export function synchronizeTerminalOutput( - terminal: Pick, + terminal: Pick< + GhosttyTerminalSurface, + "resetAndWrite" | "write" | "getSelection" | "getSelectionCellCount" | "clearSelection" + >, session: Pick, cursor: TerminalOutputCursor, ): TerminalOutputCursor { if (session.version === 0) return cursor; const update = readTerminalOutputUpdate(session.output, cursor); + if (update.type === "none") return update.cursor; + const cells = terminal.getSelectionCellCount(); + const compared = cells <= MAX_COMPARED_SELECTION_CELLS; + const selection = cells > 0 && compared ? terminal.getSelection() : ""; writeTerminalOutputUpdate(terminal, update); - terminal.clearSelection(); + if (!compared || (cells > 0 && terminal.getSelection() !== selection)) { + terminal.clearSelection(); + } return update.cursor; } /** Writes what changed between two session notifications: new output, then a new error. */ export function synchronizeTerminalSession( - terminal: Pick, + terminal: Parameters[0], previous: Pick, session: Pick, cursor: TerminalOutputCursor, @@ -760,8 +772,12 @@ function TerminalSessionViewport({ }; }; - const addSelectionToChat = (selection: TerminalContextSelection) => { - handleAddTerminalContext(selection); + const addSelectionToChat = (clipboardText: string) => { + // Output keeps moving the selected rows while a menu is open, so take + // the line numbers now, and only if the same text is still selected. + const current = readSelectionAction(); + if (current?.clipboardText !== clipboardText) return; + handleAddTerminalContext(current.selection); terminalRef.current?.clearSelection(); terminalRef.current?.focus(); }; @@ -834,7 +850,7 @@ function TerminalSessionViewport({ switch (clicked) { case "add-to-chat": if (selectionAction && canAddSelectionToChat()) { - addSelectionToChat(selectionAction.selection); + addSelectionToChat(selectionAction.clipboardText); } return; case "copy": @@ -884,7 +900,7 @@ function TerminalSessionViewport({ } switch (clicked) { case "add-to-chat": - if (canAddSelectionToChat()) addSelectionToChat(nextAction.selection); + if (canAddSelectionToChat()) addSelectionToChat(nextAction.clipboardText); return; case "copy": await copySelection(nextAction.clipboardText, requestId); @@ -1109,7 +1125,7 @@ interface ThreadTerminalDrawerProps { newShortcutLabel?: string | undefined; closeShortcutLabel?: string | undefined; onActiveTerminalChange: (terminalId: string) => void; - onCloseTerminal: (terminalId: string) => void; + onCloseTerminal: (terminalId: string, threadRef?: ScopedThreadRef) => void; onHeightChange: (height: number) => void; onAddTerminalContext: (selection: TerminalContextSelection) => void; keybindings: ResolvedKeybindingsConfig; @@ -1182,7 +1198,7 @@ function TerminalSplitPanes({ terminalLabelById: ReadonlyMap; resolveLaunchLocation: (terminalId: string) => TerminalLaunchLocation; onActiveTerminalChange: (terminalId: string) => void; - onCloseTerminal: (terminalId: string) => void; + onCloseTerminal: (terminalId: string, threadRef?: ScopedThreadRef) => void; advancedTypography: boolean; threadRef: ScopedThreadRef; threadId: ThreadId; @@ -1233,7 +1249,7 @@ function TerminalSplitPanes({ {...(terminalLaunchLocation.runtimeEnv ? { runtimeEnv: terminalLaunchLocation.runtimeEnv } : {})} - onSessionExited={() => onCloseTerminal(terminalId)} + onSessionExited={() => onCloseTerminal(terminalId, threadRef)} onAddTerminalContext={onAddTerminalContext} visible={visible} keybindings={keybindings} @@ -1557,7 +1573,7 @@ export default function ThreadTerminalDrawer({ const label = terminalLabelById.get(terminalId) ?? getTerminalLabel(terminalId); void confirmTerminalClose([label]).then((confirmed) => { if (confirmed && readEnvironmentScope(threadRef.environmentId, AuthTerminalOperateScope)) { - onCloseTerminal(terminalId); + onCloseTerminal(terminalId, threadRef); } }); }; diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index e0d5b82c3daf9..f6a16bab81a8f 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -72,7 +72,6 @@ import { import { DEFAULT_COMPOSER_PLACEHOLDER } from "../../composerPlaceholder"; import { deriveComposerSendState, - readFileAsDataUrl, resolveComposerInteractionMode, threadShellHasStarted, } from "../ChatView.logic"; @@ -80,6 +79,7 @@ import { dataTransferHasComposerMention, makeComposerMentionDragHandlers, } from "./composerMentionDrag"; +import { serializeComposerImages } from "./composerImagePersistence"; import { isInsideCollapsedComposerControls, isInsideRestingComposerControlScope, @@ -89,7 +89,6 @@ import { type ComposerFileAttachment, type ComposerImageAttachment, type DraftId, - type PersistedComposerImageAttachment, composerFileNeedsReattach, composerDraftHasUserContent, composerTargetKey, @@ -1012,6 +1011,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) const composerSendState = deriveComposerSendState({ prompt, imageCount: composerImages.length + composerFiles.length, + existingAttachmentCount: editingQueuedAttachments?.length ?? 0, terminalContexts: composerTerminalContexts, elementContextCount: composerReviewComments.length, }); @@ -1243,6 +1243,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) pendingUserInputs, composerContextRecords, composerFilesRef, + promptRef, pendingThreadImportsRef, addComposerFilesToDraft, }); @@ -1407,30 +1408,10 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) getComposerDraft(attachmentDraftTarget)?.persistedAttachments ?? []; try { const currentPersistedAttachments = getPersistedAttachmentsForThread(); - const existingPersistedById = new Map( - currentPersistedAttachments.map((attachment) => [attachment.id, attachment]), - ); - const stagedAttachmentById = new Map(); - await Promise.all( - composerImages.map(async (image) => { - try { - const dataUrl = await readFileAsDataUrl(image.file); - stagedAttachmentById.set(image.id, { - id: image.id, - name: image.name, - mimeType: image.mimeType, - sizeBytes: image.sizeBytes, - dataUrl, - }); - } catch { - const existingPersisted = existingPersistedById.get(image.id); - if (existingPersisted) { - stagedAttachmentById.set(image.id, existingPersisted); - } - } - }), + const serialized = await serializeComposerImages( + composerImages, + currentPersistedAttachments, ); - const serialized = Array.from(stagedAttachmentById.values()); if (cancelled) return; syncComposerDraftPersistedAttachments(attachmentDraftTarget, serialized); } catch { diff --git a/apps/web/src/components/chat/ComposerBannerStack.browser.test.tsx b/apps/web/src/components/chat/ComposerBannerStack.browser.test.tsx index dc23c4452b4f7..e1eeea73a20d8 100644 --- a/apps/web/src/components/chat/ComposerBannerStack.browser.test.tsx +++ b/apps/web/src/components/chat/ComposerBannerStack.browser.test.tsx @@ -1,11 +1,16 @@ import { InfoIcon } from "lucide-react"; -import { expect, it } from "vite-plus/test"; +import type { ReactNode } from "react"; +import { expect, it, vi } from "vite-plus/test"; import "~/index.css"; import { render } from "../../../test/render"; import { ComposerBanner } from "./ComposerBanner"; -import { ComposerBannerStack, type ComposerBannerStackItem } from "./ComposerBannerStack"; +import { + ComposerBannerStack, + NoticeText, + type ComposerBannerStackItem, +} from "./ComposerBannerStack"; function notice(title: string, group?: ComposerBannerStackItem["group"]): ComposerBannerStackItem { return { id: title, variant: "default", icon: , title, ...(group ? { group } : {}) }; @@ -33,3 +38,73 @@ it("leaves the next attachment its own top edge when there are no notices", asyn expect(surface.borderTopWidth).toBe("1px"); expect(surface.borderTopLeftRadius).not.toBe("0px"); }); + +function noticeRow(width: string, children: ReactNode) { + return ( +

+ ); +} + +it("leaves out the details button when only a link's hit area reaches past the text", async () => { + // Phones lay out in device pixels, so a range can measure one float32 step (2^-17 px at + // this size) wider than its element. Headless Chromium's rects agree exactly. + const rangeRect = Range.prototype.getBoundingClientRect; + using _rangeRect = vi + .spyOn(Range.prototype, "getBoundingClientRect") + .mockImplementation(function (this: Range) { + const rect = rangeRect.call(this); + return new DOMRect(rect.x, rect.y, rect.width + 2 ** -17, rect.height); + }); + // The flicker is a details button that mounts and then unmounts, so record every insertion. + const inserted: Node[] = []; + using observing = new DisposableStack(); + const mutations = new MutationObserver((records) => + inserted.push(...records.flatMap((record) => [...record.addedNodes])), + ); + mutations.observe(document.body, { childList: true, subtree: true }); + observing.defer(() => mutations.disconnect()); + await using screen = await render( + noticeRow( + "20rem", + <> + Needs input{" "} + + , + ), + ); + + await expect.element(screen.getByText("Review")).toBeVisible(); + const details = [ + ...inserted, + ...mutations.takeRecords().flatMap((record) => [...record.addedNodes]), + ].filter( + (node) => + node instanceof Element && + (node.matches('[aria-label="Show details"]') || + node.querySelector('[aria-label="Show details"]')), + ); + expect(details).toEqual([]); +}); + +it("shows the details button while text or nested text is cut, until it fits again", async () => { + await using screen = await render(noticeRow("4rem", "Waiting on a subagent with a long name")); + const details = screen.getByRole("button", { name: "Show details" }); + await expect.element(details).toBeInTheDocument(); + + screen.getByTestId("row").element().style.width = "40rem"; + await expect.element(details).not.toBeInTheDocument(); + + await screen.rerender( + noticeRow( + "40rem", + A long nested name, + ), + ); + await expect.element(details).toBeInTheDocument(); +}); diff --git a/apps/web/src/components/chat/ComposerBannerStack.tsx b/apps/web/src/components/chat/ComposerBannerStack.tsx index 755d20ba8c6ae..ceafe9afeac2d 100644 --- a/apps/web/src/components/chat/ComposerBannerStack.tsx +++ b/apps/web/src/components/chat/ComposerBannerStack.tsx @@ -59,6 +59,8 @@ export function ComposerBannerStack({ )); } +const SUBPIXEL_ROUNDING = 0.001; + export function NoticeText({ children, className, @@ -76,6 +78,7 @@ export function NoticeText({ useLayoutEffect(() => { const text = textRef.current; if (!text) return; + const range = document.createRange(); const measure = () => { // Ignore the space taken by the details button itself so it cannot // sustain its own overflow after the text would otherwise fit. @@ -83,9 +86,17 @@ export function NoticeText({ const hidden = getComputedStyle(text).position === "absolute"; setShowDetails( hidden || - [text, ...text.querySelectorAll("*")].some( - (element) => element.scrollWidth > element.clientWidth + recoveredWidth, - ), + [text, ...text.querySelectorAll("*")].some((element) => { + // A range spans laid-out content only; scrollWidth also counts positioned layers + // such as a link's touch hit area, which overflow without cutting any text. + // The two rects can disagree by float rounding (~0.00001px on phones); real + // overflow is at least one layout unit, 1/64 of a device pixel. + range.selectNodeContents(element); + return ( + range.getBoundingClientRect().width > + element.getBoundingClientRect().width + recoveredWidth + SUBPIXEL_ROUNDING + ); + }), ); }; measure(); diff --git a/apps/web/src/components/chat/ComposerFooterControls.tsx b/apps/web/src/components/chat/ComposerFooterControls.tsx index 46de66194dd0d..d99e538f6fe1f 100644 --- a/apps/web/src/components/chat/ComposerFooterControls.tsx +++ b/apps/web/src/components/chat/ComposerFooterControls.tsx @@ -182,7 +182,7 @@ export function ComposerPermissionsSection(props: { onValueChange={(value: RuntimeMode) => { if (value !== props.runtimeMode) props.onRuntimeModeChange(value); }} - className="flex gap-0.5 rounded-lg bg-input/40 p-0.5" + className="flex rounded-lg bg-input/40 p-0.5" // Menus navigate with Up/Down only; the row reads horizontally, so Left/Right move too. onKeyDown={(event) => { if (event.key !== "ArrowLeft" && event.key !== "ArrowRight") return; @@ -211,7 +211,7 @@ export function ComposerPermissionsSection(props: { onPointerLeave={() => setPreviewMode(null)} onFocus={() => setPreviewMode(option.mode)} onBlur={() => setPreviewMode(null)} - className="flex h-6 flex-1 cursor-pointer items-center justify-center rounded-md text-muted-foreground outline-none data-checked:bg-input/72 data-checked:text-foreground data-highlighted:text-foreground not-data-checked:data-highlighted:bg-accent focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-inset" + className="flex h-6 flex-1 cursor-pointer items-center justify-center text-muted-foreground outline-none first:rounded-l-md last:rounded-r-md data-checked:bg-input/72 data-checked:text-foreground data-highlighted:text-foreground not-data-checked:data-highlighted:bg-accent focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-inset" >
diff --git a/apps/web/src/components/chat/ThreadRelationshipsControl.browser.test.tsx b/apps/web/src/components/chat/ThreadRelationshipsControl.browser.test.tsx new file mode 100644 index 0000000000000..88bd956decc2f --- /dev/null +++ b/apps/web/src/components/chat/ThreadRelationshipsControl.browser.test.tsx @@ -0,0 +1,126 @@ +import { EnvironmentId, ThreadId } from "@t3tools/contracts"; +import * as Cause from "effect/Cause"; +import { AsyncResult } from "effect/reactivity"; +import { expect, it, vi } from "vite-plus/test"; +import { page } from "vite-plus/test/browser"; + +const state = vi.hoisted(() => ({ projection: null as unknown, commandSucceeds: false })); + +vi.mock(import("@effect/atom-react"), async (original) => ({ + ...(await original()), + useAtomValue: (() => false) as never, +})); + +vi.mock(import("../../state/entities"), () => ({ + useQuietThreadShell: (() => null) as never, + useThreadProjection: ((ref: unknown) => + ref === null ? null : { projection: state.projection }) as never, + useThreadRelationshipShells: (() => + state.projection === null ? [] : [(state.projection as { thread: unknown }).thread]) as never, + useProjects: (() => []) as never, +})); +vi.mock(import("../../state/environments"), () => ({ + useEnvironment: (() => null) as never, + useEnvironmentProviders: (() => new Map()) as never, +})); +vi.mock(import("../../lib/archivedThreadsState"), () => ({ + useArchivedThreadSnapshots: (() => ({ snapshots: [] })) as never, +})); +vi.mock(import("../../state/use-atom-command"), () => ({ + useAtomCommand: (() => async () => + state.commandSucceeds + ? AsyncResult.success(undefined) + : AsyncResult.failure(Cause.fail(new Error("Connection lost")))) as never, +})); + +import { render } from "../../../test/render"; +import { ToastProvider } from "../ui/toast"; +import { ThreadRelationshipsPanel } from "./ThreadRelationshipsControl"; + +const environmentId = EnvironmentId.make("environment"); +const threadId = ThreadId.make("fork"); + +function setup() { + state.commandSucceeds = false; + state.projection = { + thread: { + id: threadId, + title: "Fork", + activeProviderThreadId: null, + lineage: { parentThreadId: ThreadId.make("source"), relationshipToParent: "fork" }, + forkedFrom: null, + }, + runs: [{ id: "run", ordinal: 1, status: "completed" }], + subagents: [], + contextTransfers: [], + providerThreads: [], + providerSessions: [{ id: "session", status: "ready" }], + }; +} + +it.each([ + ["merge", "Could not merge back"], + ["disconnect", "Could not disconnect agent session"], +])("reports a rejected %s command and clears the busy state", async (action, title) => { + setup(); + await using screen = await render( + + + , + ); + + if (action === "merge") { + await screen.getByRole("button", { name: "Merge back to source conversation" }).click(); + } else { + await screen.getByRole("button", { name: "More thread actions" }).click(); + await page.getByRole("menuitem", { name: "Disconnect agent session" }).click(); + } + + await expect.element(page.getByText(title, { exact: true })).toBeVisible(); + await expect + .element( + screen.getByRole("button", { + name: action === "merge" ? "Merge back to source conversation" : "More thread actions", + }), + ) + .toBeEnabled(); +}); + +it.each([ + ["merge", "Could not merge back"], + ["disconnect", "Could not disconnect agent session"], +])("clears the previous %s error after a successful retry", async (action, title) => { + setup(); + await using screen = await render( + + + , + { router: "/environment/fork" }, + ); + + const runAction = async () => { + if (action === "merge") { + await screen.getByRole("button", { name: "Merge back to source conversation" }).click(); + } else { + await screen.getByRole("button", { name: "More thread actions" }).click(); + await page.getByRole("menuitem", { name: "Disconnect agent session" }).click(); + } + }; + + await runAction(); + await expect.element(page.getByText(title, { exact: true })).toBeVisible(); + expect(screen.router?.state.location.pathname).toBe("/environment/fork"); + + state.commandSucceeds = true; + await runAction(); + + await expect.element(page.getByText(title, { exact: true })).not.toBeInTheDocument(); + await vi.waitFor(() => { + expect(screen.router?.state.location.pathname).toBe( + action === "merge" ? "/environment/source" : "/environment/fork", + ); + }); + if (action === "disconnect") { + await expect.element(screen.getByRole("button", { name: "More thread actions" })).toBeEnabled(); + } +}); diff --git a/apps/web/src/components/chat/ThreadRelationshipsControl.tsx b/apps/web/src/components/chat/ThreadRelationshipsControl.tsx index 69403392b5ce8..63a8635c066f5 100644 --- a/apps/web/src/components/chat/ThreadRelationshipsControl.tsx +++ b/apps/web/src/components/chat/ThreadRelationshipsControl.tsx @@ -54,7 +54,7 @@ import { SquareIcon, UnplugIcon, } from "lucide-react"; -import { useState, type ReactNode } from "react"; +import { useRef, useState, type ReactNode } from "react"; import { useArchivedThreadSnapshots } from "../../lib/archivedThreadsState"; import { buildThreadRouteParams } from "../../threadRoutes"; @@ -385,6 +385,8 @@ export function ThreadRelationshipsPanel(props: { orchestrationEnvironment.stopWorkflow.permissionAtom(props.environmentId), ); const [busyAction, setBusyAction] = useState<"merge" | "detach" | null>(null); + const mergeErrorToastId = useRef(null); + const detachErrorToastId = useRef(null); const [stoppingId, setStoppingId] = useState(null); // Derive graphs after hooks to preserve their compiler cache. const providers = providersByEnvironment.get(props.environmentId); @@ -492,6 +494,10 @@ export function ThreadRelationshipsPanel(props: { const merge = async () => { if (!latestMergeBackRun || mergeTargetThreadId === null || busyAction !== null) return; + if (mergeErrorToastId.current !== null) { + toastManager.close(mergeErrorToastId.current); + mergeErrorToastId.current = null; + } setBusyAction("merge"); const result = await mergeBack({ environmentId: props.environmentId, @@ -502,17 +508,34 @@ export function ThreadRelationshipsPanel(props: { }, }); setBusyAction(null); - if (result._tag === "Success") openThread(mergeTargetThreadId); + if (result._tag === "Failure") { + mergeErrorToastId.current = toastManager.add({ + type: "error", + title: "Could not merge back", + }); + return; + } + openThread(mergeTargetThreadId); }; const detach = async () => { if (!canDetach || busyAction !== null) return; + if (detachErrorToastId.current !== null) { + toastManager.close(detachErrorToastId.current); + detachErrorToastId.current = null; + } setBusyAction("detach"); - await stopSession({ + const result = await stopSession({ environmentId: props.environmentId, input: { threadId: props.threadId }, }); setBusyAction(null); + if (result._tag === "Failure") { + detachErrorToastId.current = toastManager.add({ + type: "error", + title: "Could not disconnect agent session", + }); + } }; const stopSubagent = async ( diff --git a/apps/web/src/components/chat/TimelineUserMessageRow.logic.ts b/apps/web/src/components/chat/TimelineUserMessageRow.logic.ts index 9738e29d7ccaa..2bb6d0470fa18 100644 --- a/apps/web/src/components/chat/TimelineUserMessageRow.logic.ts +++ b/apps/web/src/components/chat/TimelineUserMessageRow.logic.ts @@ -1,7 +1,11 @@ -import { type ChatImageAttachment } from "../../types"; +import { type ChatFileAttachment, type ChatImageAttachment } from "../../types"; import type { ComposerContextRecord, KnownComposerContextRecord } from "@t3tools/contracts"; import { asKnownContextRecord } from "~/lib/composerContextRecords"; +export function userMessageFileLabel(file: Pick) { + return file.source?._tag === "pasted-text" ? "Pasted text" : file.name; +} + export function resolvePreviewAnnotationImage(input: { record: Extract; recordsById: ReadonlyMap; diff --git a/apps/web/src/components/chat/TimelineUserMessageRow.tsx b/apps/web/src/components/chat/TimelineUserMessageRow.tsx index 27e234aa21c73..ddd2ccd45e5b7 100644 --- a/apps/web/src/components/chat/TimelineUserMessageRow.tsx +++ b/apps/web/src/components/chat/TimelineUserMessageRow.tsx @@ -47,6 +47,7 @@ import { } from "./ExpandedImagePreview"; import { MessageCopyButton } from "./MessageCopyButton"; import { PierreEntryIcon } from "./PierreEntryIcon"; +import { formatBytes } from "../../lib/formatBytes"; import { shouldCollapseUserMessage } from "./MessagesTimeline.logic"; import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; import { @@ -54,7 +55,10 @@ import { resolveUserMessageContext, selectedMessageContextFragment, } from "~/lib/composerContextRecords"; -import { collectComposerContextReferences } from "@t3tools/shared/composerContextReferences"; +import { + collectComposerContextReferences, + formatComposerContextReference, +} from "@t3tools/shared/composerContextReferences"; import { COMPOSER_CONTEXT_CLIPBOARD_MIME, encodeComposerContextClipboardHtml, @@ -72,7 +76,10 @@ import { TimelineRowTimestamp, } from "./TimelineRowParts"; import { UserMessageContextReferenceChip } from "./UserMessageContextChips"; -import { resolvePreviewAnnotationImage } from "./TimelineUserMessageRow.logic"; +import { + resolvePreviewAnnotationImage, + userMessageFileLabel, +} from "./TimelineUserMessageRow.logic"; // Drawn inside the media so dark screenshots keep an edge on the dark background. const MEDIA_EDGE_CLASS_NAME = @@ -81,7 +88,89 @@ const MEDIA_EDGE_CLASS_NAME = const BUBBLE_CHIP_CLASS_NAME = "[&_[data-slot=context-chip]]:[--context-chip-ink:var(--context-chip-message,var(--context-chip-accent))]"; -function UserVideoAttachment({ file }: { readonly file: ChatFileAttachment }) { +function UserFileCard({ + file, + copyMarkdown, +}: { + readonly file: ChatFileAttachment; + readonly copyMarkdown: string | undefined; +}) { + const ctx = use(TimelineRowCtx); + const pastedText = file.source?._tag === "pasted-text"; + const label = userMessageFileLabel(file); + const extension = /\.([a-z0-9]{1,8})$/i.exec(file.name)?.[1]?.toUpperCase() ?? "File"; + const identity = ( + <> + + + + + + {label} + + + {file.downloadable === false ? ( + "Sending…" + ) : ( + <> + {!pastedText && `${extension} · `} + {formatBytes(file.sizeBytes)} + + )} + + + + ); + return ( +
+ {file.downloadable === false ? ( + <> +
{identity}
+ + + ) : ( + <> + + + ctx.onFileDownload(file)} + /> + } + > + + + Download {label} + + + )} +
+ ); +} + +function UserVideoAttachment({ + file, + standalone = false, +}: { + readonly file: ChatFileAttachment; + readonly standalone?: boolean; +}) { const ctx = use(TimelineRowCtx); // Row updates rebuild attachment objects, so the asset follows only the // fields it reads. @@ -98,13 +187,20 @@ function UserVideoAttachment({ file }: { readonly file: ChatFileAttachment }) { if (asset === null && src === null) { return ( -
+
{file.name}
); } - return ( + const player = ( ); + return standalone ? ( +
{player}
+ ) : ( + player + ); } export function UserMessageRow({ row }: { row: Extract }) { @@ -319,11 +426,22 @@ function UserTimelineRow({ row }: { row: Extract image.name.startsWith("preview-annotation-")); const revertTurnCount = row.revertTurnCount; - const chippedAttachmentIds = new Set( - collectComposerContextReferences(resolvedContext.text).flatMap((occurrence) => { + const attachmentReferences = collectComposerContextReferences(resolvedContext.text).flatMap( + (occurrence) => { const record = asKnownContextRecord(resolvedContext.recordsById.get(occurrence.contextId)); - return record?.kind === "file" || record?.kind === "image" ? [record.attachmentId] : []; - }), + return record?.kind === "file" || record?.kind === "image" + ? [ + { + attachmentId: record.attachmentId, + contextId: occurrence.contextId, + source: occurrence.source, + }, + ] + : []; + }, + ); + const chippedAttachmentIds = new Set( + attachmentReferences.map((reference) => reference.attachmentId), ); const regularImages = userImages.filter((image) => !image.name.startsWith("preview-annotation-")); const unchippedFiles = otherUserFiles.filter((file) => !chippedAttachmentIds.has(file.id)); @@ -336,18 +454,51 @@ function UserTimelineRow({ row }: { row: Extract 0; + // Media sent with words sits above the bubble as its own shape, like a chat app. + const mediaAboveText = + !folded && + userMessage.attribution !== "guest" && + !mediaOnly && + regularImages.length + userVideos.length > 0; + const standaloneMedia = mediaOnly || mediaAboveText; // A lone image keeps its own shape; a 4:3 crop hides most of a wide screenshot. - const loneImage = - mediaOnly && - userVideos.length === 0 && - regularImages.length === 1 && - regularImages[0]!.source?.kind !== "snap-shot"; - // Several images sent without words share one rounded shape, split by hairlines. + const loneImage = standaloneMedia && userVideos.length === 0 && regularImages.length === 1; + // Several images share one rounded shape, split by hairlines. const collage = - mediaOnly && + standaloneMedia && userVideos.length === 0 && regularImages.length > 1 && regularImages.every((image) => image.source?.kind !== "snap-shot"); + // Files sent without words show as cards, not as a bubble around a name chip. + const fileCardsOnly = + !folded && + userMessage.attribution !== "guest" && + otherUserFiles.length > 0 && + regularImages.length + userVideos.length + userAudio.length + unknownAttachments.length === 0 && + replaceComposerContextReferences(resolvedContext.text, (reference) => + attachmentReferences.some( + (attached) => + attached.contextId === reference.contextId && + otherUserFiles.some((file) => file.id === attached.attachmentId), + ) + ? "" + : reference.label, + ).trim() === ""; + const audioOnly = + !folded && + userMessage.attribution !== "guest" && + userAudio.length > 0 && + regularImages.length + userVideos.length + otherUserFiles.length + unknownAttachments.length === + 0 && + replaceComposerContextReferences(resolvedContext.text, (reference) => + attachmentReferences.some( + (attached) => + attached.contextId === reference.contextId && + userAudio.some((file) => file.id === attached.attachmentId), + ) + ? "" + : reference.label, + ).trim() === ""; const annotationRecordIds = resolvedContext.records .filter((record) => record.kind === "preview-annotation") .map((record) => record.contextId); @@ -360,8 +511,19 @@ function UserTimelineRow({ row }: { row: Extract line.trim().length > 0) ?.trim() ?? ""; + const mediaCopyRecords = mediaOnly + ? resolvedContext.records.flatMap((record) => { + const known = asKnownContextRecord(record); + return (known?.kind === "image" || known?.kind === "file") && + (regularImages.some((image) => image.id === known.attachmentId) || + userVideos.some((file) => file.id === known.attachmentId)) + ? [known] + : []; + }) + : []; + const copyRecords = mediaOnly ? mediaCopyRecords : resolvedContext.records; const contextClipboardFragment = - resolvedContext.records.length === 0 + copyRecords.length === 0 ? null : encodeComposerContextFragment({ version: 1, @@ -370,8 +532,10 @@ function UserTimelineRow({ row }: { row: Extract) => { if (resolvedContext.records.length === 0 || !event.clipboardData) return; const selection = window.getSelection(); @@ -449,6 +613,98 @@ function UserTimelineRow({ row }: { row: Extract 0 || userVideos.length > 0 ? ( +
+ {regularImages.map((image, index) => ( +
3 && + regularImages.length % 2 === 1 && + index === regularImages.length - 1 && + "col-span-2", + ) + : standaloneMedia + ? cn(MEDIA_EDGE_CLASS_NAME, "aspect-[4/3] overflow-hidden rounded-2xl") + : "aspect-[4/3] overflow-hidden rounded-lg border border-border/80", + )} + > + {image.previewUrl ? ( + + ) : ( +
+ {image.name} +
+ )} +
+ ))} + {userVideos.map((file) => ( + + ))} +
+ ) : null; + return (
) : null} - {row.message.inputIntent === "steer" || - row.message.inputIntent === "promoted_queued_to_steer" ? ( + {(row.message.inputIntent === "steer" || + row.message.inputIntent === "promoted_queued_to_steer") && + !row.continuesSteer ? ( ) : null} + {mediaAboveText ? ( +
+ {/* The heading leads the media so screen readers reach it before the images. */} + You + {mediaBlock} +
+ ) : null}
{/* Outside the fold so a collapsed message still has a heading to navigate to. */} - {folded ? senderLabel : "You"} + {mediaAboveText ? null : ( + {folded ? senderLabel : "You"} + )} {folded ? ( - {(regularImages.length > 0 || userVideos.length > 0) && ( -
- {regularImages.map((image, index) => ( -
3 && - regularImages.length % 2 === 1 && - index === regularImages.length - 1 && - "col-span-2", - ) - : "aspect-[4/3] overflow-hidden rounded-lg border border-border/80", - )} - > - {image.previewUrl ? ( - - ) : ( -
- {image.name} -
- )} -
- ))} - {userVideos.map((file) => ( - - ))} -
- )} + {!mediaAboveText && mediaBlock} {userAudio.length > 0 ? ( -
+
{userAudio.map((file) => ( ) : null} - {unchippedFiles.length > 0 || unknownAttachments.length > 0 ? ( + {fileCardsOnly ? ( +
+ {otherUserFiles.map((file) => ( + reference.attachmentId === file.id) + ?.source + } + /> + ))} +
+ ) : null} + {!fileCardsOnly && (unchippedFiles.length > 0 || unknownAttachments.length > 0) ? (
{unchippedFiles.map((file) => { + const pastedText = file.source?._tag === "pasted-text"; + const label = userMessageFileLabel(file); const fileIdentity = ( <> - {file.name} + + {label} + {file.downloadable === false || pastedText ? ( + + {file.downloadable === false ? "Sending…" : formatBytes(file.sizeBytes)} + + ) : null} + ); if (file.downloadable !== false) { @@ -621,7 +827,7 @@ function UserTimelineRow({ row }: { row: Extract
); @@ -665,15 +871,17 @@ function UserTimelineRow({ row }: { row: Extract ) : null} -
- -
+ {fileCardsOnly || audioOnly ? null : ( +
+ +
+ )}
) : null}
@@ -698,9 +906,9 @@ function UserTimelineRow({ row }: { row: Extract )} - {resolvedContext.text && ( + {copyText && ( ; @@ -368,12 +369,14 @@ const userMessageContextPresentationRegistry = createContextPresentationRegistry } const attachment = context.attachment; const isVideo = isVideoAttachment(attachment); + const label = userMessageFileLabel(attachment); const disabled = attachment.downloadable === false && (!isVideo || attachment.previewUrl === undefined); - const size = formatAttachmentSize(record.sizeBytes); + const size = + attachment.downloadable === false ? "Sending…" : formatAttachmentSize(record.sizeBytes); return ( isVideo ? context.onExpandVideo(attachment) : context.onOpenFile(attachment) } - tooltip={`${record.name}\n${size}`} + tooltip={`${label}\n${size}`} /> ); }, diff --git a/apps/web/src/components/chat/V2LifecycleRow.tsx b/apps/web/src/components/chat/V2LifecycleRow.tsx index 42ab22fa5f3d8..aa6f7819ca02c 100644 --- a/apps/web/src/components/chat/V2LifecycleRow.tsx +++ b/apps/web/src/components/chat/V2LifecycleRow.tsx @@ -311,17 +311,22 @@ const NOTIFICATION_OUTCOME_LABEL: Record | undefined; readonly outcome: OrchestrationV2Notification["outcome"]; readonly createdAt: string; readonly timestampFormat: TimestampFormat; readonly onOpenThread: (threadId: ThreadId) => void; readonly fallback: ReactNode; }) { + // Follow-up tasks share a child conversation; this notice reports a particular task result. const agent = useAtomValue( environmentThreadDetails.threadAtom(props.parentRef), (thread) => - thread?.projection.subagents.find((agent) => agent.childThreadId === props.childThreadId) ?? - null, + thread?.projection.subagents.find((agent) => + props.taskIds === undefined + ? agent.childThreadId === props.childThreadId + : props.taskIds.includes(agent.id), + ) ?? null, ); if (agent === null) return props.fallback; const resumed = diff --git a/apps/web/src/components/chat/chatCanvasLayout.test.ts b/apps/web/src/components/chat/chatCanvasLayout.test.ts index 762f8db785356..f6a85eb08037a 100644 --- a/apps/web/src/components/chat/chatCanvasLayout.test.ts +++ b/apps/web/src/components/chat/chatCanvasLayout.test.ts @@ -11,6 +11,9 @@ describe("workspace card beside chat", () => { composerHeight: 180, detailsCard: { left: width - 292, right: width - 12, bottom: 400 }, }); + it("keeps chat centered while the card fits beside it", () => { + expect(withCard(1500).chat).toEqual({ left: 382, width: 736, insetStart: 0, insetEnd: 0 }); + }); it("narrows chat only after it reaches the left padding", () => { expect(withCard(1108).chat).toMatchObject({ left: 48, width: 736 }); expect(withCard(1028).chat).toEqual({ left: 48, width: 656, insetStart: 0, insetEnd: 276 }); @@ -30,15 +33,14 @@ describe("workspace card beside chat", () => { expect(card.x - chat.left - chat.width).toBeGreaterThanOrEqual(32); } }); - it("centers the chat beside a details card it would crowd", () => { + it("moves the chat left only as far as a details card it would crowd requires", () => { const { chat } = resolveChatCanvasLayout({ container: { width: 1344, height: 988 }, maxChatWidth: 736, detailsCard: { left: 1052, right: 1332, bottom: 339 }, preview: null, }); - // Equal space on both sides of the chat. - expect(chat).toMatchObject({ left: (1052 - 736) / 2, width: 736 }); + expect(chat).toMatchObject({ left: 1052 - 32 - 736, width: 736 }); }); it("keeps the chat canvas-centered when it clears the details card", () => { const result = resolveChatCanvasLayout({ diff --git a/apps/web/src/components/chat/chatCanvasLayout.ts b/apps/web/src/components/chat/chatCanvasLayout.ts index afc2ed450f5d3..b4129cfcd3de5 100644 --- a/apps/web/src/components/chat/chatCanvasLayout.ts +++ b/apps/web/src/components/chat/chatCanvasLayout.ts @@ -51,22 +51,7 @@ export function resolveChatCanvasLayout({ : container.width - padding; const normalWidth = Math.max(0, Math.min(centeredWidth, laneRight - padding)); const canvasCenterLeft = (container.width - normalWidth) / 2; - // Beside a card it would crowd, the chat centers between the card and the canvas edge. The shift - // eases to zero as clearance grows, so very wide windows keep the chat still and a resize - // never jumps it. - const besideShift = detailsCard - ? Math.min( - (container.width - detailsCard.left) / 2, - Math.max( - 0, - (container.width - detailsCard.left) / 2 - (laneRight - canvasCenterLeft - normalWidth), - ), - ) - : 0; - const normalLeft = Math.max( - padding, - Math.min(canvasCenterLeft - besideShift, laneRight - normalWidth), - ); + const normalLeft = Math.max(padding, Math.min(canvasCenterLeft, laneRight - normalWidth)); const chat = { left: normalLeft, width: normalWidth, diff --git a/apps/web/src/components/chat/composerImagePersistence.test.ts b/apps/web/src/components/chat/composerImagePersistence.test.ts new file mode 100644 index 0000000000000..5d8a210977bfd --- /dev/null +++ b/apps/web/src/components/chat/composerImagePersistence.test.ts @@ -0,0 +1,98 @@ +import { beforeEach, describe, expect, it, vi } from "vite-plus/test"; + +import type { ComposerImageAttachment } from "../../composerDraftStore"; +import { readFileAsDataUrl } from "../../lib/imageCompression"; +import { serializeComposerImages } from "./composerImagePersistence"; + +vi.mock("../../lib/imageCompression", () => ({ readFileAsDataUrl: vi.fn() })); + +function image(id: string): ComposerImageAttachment { + const file = new File([id], `${id}.png`, { type: "image/png" }); + return { + type: "image", + id, + name: file.name, + mimeType: file.type, + sizeBytes: file.size, + previewUrl: `blob:${id}`, + file, + }; +} + +describe("serializeComposerImages", () => { + beforeEach(() => { + vi.mocked(readFileAsDataUrl).mockReset(); + }); + + it("reads only new images and drops removed images without rereading retained bytes", async () => { + const a = image("a"); + const b = image("b"); + vi.mocked(readFileAsDataUrl).mockImplementation(async (file) => + file === a.file ? "data:image/png;base64,YQ==" : "data:image/png;base64,Yg==", + ); + + const first = await serializeComposerImages([a], []); + expect(first).toEqual([ + { + id: "a", + name: "a.png", + mimeType: "image/png", + sizeBytes: 1, + dataUrl: "data:image/png;base64,YQ==", + }, + ]); + expect(readFileAsDataUrl).toHaveBeenCalledTimes(1); + + const second = await serializeComposerImages([a, b], first); + expect(second).toEqual([ + first[0], + { + id: "b", + name: "b.png", + mimeType: "image/png", + sizeBytes: 1, + dataUrl: "data:image/png;base64,Yg==", + }, + ]); + expect(readFileAsDataUrl).toHaveBeenCalledTimes(2); + expect(readFileAsDataUrl).toHaveBeenNthCalledWith(2, b.file); + + expect(await serializeComposerImages([a], second)).toEqual(first); + expect(await serializeComposerImages([], second)).toEqual([]); + expect(readFileAsDataUrl).toHaveBeenCalledTimes(2); + }); + + it("preserves cached bytes without trying to read an unavailable file", async () => { + const a = image("a"); + const persisted = { + id: a.id, + name: a.name, + mimeType: a.mimeType, + sizeBytes: a.sizeBytes, + dataUrl: "data:image/png;base64,YQ==", + }; + vi.mocked(readFileAsDataUrl).mockRejectedValue(new Error("File is unavailable")); + + expect(await serializeComposerImages([a], [persisted])).toEqual([persisted]); + expect(readFileAsDataUrl).not.toHaveBeenCalled(); + }); + + it("omits failed uncached reads while keeping successfully serialized images", async () => { + const a = image("a"); + const b = image("b"); + vi.mocked(readFileAsDataUrl) + .mockRejectedValueOnce(new Error("File is unavailable")) + .mockResolvedValueOnce("data:image/png;base64,Yg=="); + + expect(await serializeComposerImages([a, b], [])).toEqual([ + { + id: "b", + name: "b.png", + mimeType: "image/png", + sizeBytes: 1, + dataUrl: "data:image/png;base64,Yg==", + }, + ]); + expect(readFileAsDataUrl).toHaveBeenCalledTimes(2); + }); +}); diff --git a/apps/web/src/components/chat/composerImagePersistence.ts b/apps/web/src/components/chat/composerImagePersistence.ts new file mode 100644 index 0000000000000..ad189b5224c99 --- /dev/null +++ b/apps/web/src/components/chat/composerImagePersistence.ts @@ -0,0 +1,37 @@ +import type { + ComposerImageAttachment, + PersistedComposerImageAttachment, +} from "../../composerDraftStore"; +import { readFileAsDataUrl } from "../../lib/imageCompression"; + +export async function serializeComposerImages( + images: ReadonlyArray, + persistedAttachments: ReadonlyArray, +): Promise { + const existingPersistedById = new Map( + persistedAttachments.map((attachment) => [attachment.id, attachment]), + ); + const stagedAttachmentById = new Map(); + await Promise.all( + images.map(async (image) => { + try { + const dataUrl = + existingPersistedById.get(image.id)?.dataUrl ?? (await readFileAsDataUrl(image.file)); + stagedAttachmentById.set(image.id, { + id: image.id, + name: image.name, + mimeType: image.mimeType, + sizeBytes: image.sizeBytes, + dataUrl, + ...(image.source ? { source: image.source } : {}), + }); + } catch { + const existingPersisted = existingPersistedById.get(image.id); + if (existingPersisted) { + stagedAttachmentById.set(image.id, existingPersisted); + } + } + }), + ); + return Array.from(stagedAttachmentById.values()); +} diff --git a/apps/web/src/components/chat/markdownChipPunctuation.test.ts b/apps/web/src/components/chat/markdownChipPunctuation.test.ts new file mode 100644 index 0000000000000..9f09ffbf80457 --- /dev/null +++ b/apps/web/src/components/chat/markdownChipPunctuation.test.ts @@ -0,0 +1,32 @@ +import { describe, expect, it } from "vite-plus/test"; + +import { rehypeChipPunctuation } from "./markdownChipPunctuation"; + +const text = (value: string) => ({ type: "text", value }); +const element = (tagName: string, children: object[]) => ({ + type: "element", + tagName, + properties: {} as Record, + children, +}); + +function paired(block: ReturnType) { + rehypeChipPunctuation()({ type: "root", children: [block] }); + return block.children; +} + +describe("rehypeChipPunctuation", () => { + it("pairs a link with the punctuation that follows it", () => { + const link = element("a", [text("a.ts")]); + expect(paired(element("p", [text("See "), link, text("... next")]))).toEqual([ + text("See "), + expect.objectContaining({ tagName: "span", children: [link, text("...")] }), + text(" next"), + ]); + }); + + it("leaves code blocks alone", () => { + const children = [element("code", [text("a.ts")]), text(".")]; + expect(paired(element("pre", [...children]))).toEqual(children); + }); +}); diff --git a/apps/web/src/components/chat/markdownChipPunctuation.ts b/apps/web/src/components/chat/markdownChipPunctuation.ts new file mode 100644 index 0000000000000..67d78a524799e --- /dev/null +++ b/apps/web/src/components/chat/markdownChipPunctuation.ts @@ -0,0 +1,41 @@ +type HastNode = { + type?: string; + tagName?: string; + value?: string; + properties?: Record; + children?: HastNode[]; +}; + +const LEADING_PUNCTUATION = /^[.,:;!?…)\]}"'’”]+/; + +// Browsers allow a wrap after inline-flex chips even before punctuation. +export function rehypeChipPunctuation() { + const visit = (node: HastNode): void => { + if (node.tagName === "pre" || !node.children) return; + const children: HastNode[] = []; + for (const child of node.children) { + visit(child); + const previous = children.at(-1); + const punctuation = + child.type === "text" && (previous?.tagName === "a" || previous?.tagName === "code") + ? LEADING_PUNCTUATION.exec(child.value ?? "")?.[0] + : undefined; + if (!previous || !punctuation) { + children.push(child); + continue; + } + children[children.length - 1] = { + type: "element", + tagName: "span", + properties: { + className: ["has-[[data-slot=context-chip-label]]:whitespace-nowrap"], + }, + children: [previous, { type: "text", value: punctuation }], + }; + const rest = (child.value ?? "").slice(punctuation.length); + if (rest) children.push({ ...child, value: rest }); + } + node.children = children; + }; + return visit; +} diff --git a/apps/web/src/components/chat/markdownRenderer.test.tsx b/apps/web/src/components/chat/markdownRenderer.test.tsx new file mode 100644 index 0000000000000..e5ba98d141f82 --- /dev/null +++ b/apps/web/src/components/chat/markdownRenderer.test.tsx @@ -0,0 +1,85 @@ +import { + CHAT_MARKDOWN_REHYPE_PLUGINS, + CHAT_MARKDOWN_REMARK_PLUGINS, +} from "@t3tools/shared/markdownPipeline"; +import { renderToStaticMarkup } from "react-dom/server"; +import ReactMarkdown from "react-markdown"; +import { describe, expect, it } from "vite-plus/test"; + +import { createIncrementalMarkdownPlugin } from "../../markdown-incremental"; +import { createMarkdownRenderer } from "./markdownRenderer"; + +// Visible in the markup, so every URL attribute must pass through it. +const urlTransform = (url: string) => `${url}#seen`; + +describe("createMarkdownRenderer", () => { + const render = createMarkdownRenderer({ + remarkPlugins: CHAT_MARKDOWN_REMARK_PLUGINS, + rehypePlugins: CHAT_MARKDOWN_REHYPE_PLUGINS, + components: {}, + urlTransform, + }); + + it.each([ + '# Title\n\nText with `code`, **bold**, [a link](https://example.com "t") and https://bare.example.com.', + "![shot](./shot.png) and [file](file:///C:/repo/a.ts) and [bad](javascript:alert(1))", + "
More\n\nHidden bold \n\n
", + "| a | b |\n| - | - |\n|
| [x](#frag) |\n\n- [ ] task\n- [x] done", + '```ts title="a.ts"\nconst a = 1;\n```\n\n> [!WARNING]\n> careful
bare', + "Footnote[^1] and ~~strike~~\n\n[^1]: the note", + // Each GFM construct the parser skips when its text cannot match, next to one that can. + "Mail me@example.com, see www.example.com and HTTPS://example.com/a, not [x](https://y.z).", + "a.b-c_d+e@x.io and @user\n\n`me@x.io` ", + "A table without pipes\n:--", + "> | quoted |\n> | - |\n> | body |\n\n- [ ] open\n- [X] done", + "Escaped www\\.example.com and me@example.com", + "\ufeffA byte order mark\n\n| a |\n| - |\n| b |", + "> - | a |\r\n> | - |\r\n> | me@x.io |\r\n\n-\t[ ] tab\n\n ~~lazy~~", + ])("renders what react-markdown renders: %j", (source) => { + const expected = renderToStaticMarkup( + + {source} + , + ); + const element = render(source, true); + expect(renderToStaticMarkup(element)).toBe(expected); + expect(render(source, true)).toBe(element); + }); + + it("renders what react-markdown renders while a reply streams through the incremental parser", () => { + const reply = + "```ts\nconst a = 1;\n```\n\n| a | b |\n| - | - |\n| me@x.io | www.x.io |\n\nDone ~~now~~."; + // As ChatMarkdown streams: one incremental plugin for the reply, a fresh renderer per update. + const incremental = createIncrementalMarkdownPlugin(); + for (let end = 1; end <= reply.length; end++) { + const source = reply.slice(0, end); + const streamed = createMarkdownRenderer({ + remarkPlugins: [...CHAT_MARKDOWN_REMARK_PLUGINS, incremental], + rehypePlugins: CHAT_MARKDOWN_REHYPE_PLUGINS, + components: {}, + urlTransform, + })(source, false); + expect(renderToStaticMarkup(streamed)).toBe( + renderToStaticMarkup( + + {source} + , + ), + ); + } + }); + + it("keeps cached messages when one far larger than the cache is rendered", () => { + const element = render("Kept *message*", true); + render("x".repeat(600_000), true); + expect(render("Kept *message*", true)).toBe(element); + }); +}); diff --git a/apps/web/src/components/chat/markdownRenderer.ts b/apps/web/src/components/chat/markdownRenderer.ts new file mode 100644 index 0000000000000..ab4083f2adf47 --- /dev/null +++ b/apps/web/src/components/chat/markdownRenderer.ts @@ -0,0 +1,109 @@ +import type { Element, Nodes, Root } from "hast"; +import { toJsxRuntime, type Components } from "hast-util-to-jsx-runtime"; +import { urlAttributes } from "html-url-attributes"; +import type { ReactElement } from "react"; +import { Fragment, jsx, jsxs } from "react/jsx-runtime"; +import remarkParse from "remark-parse"; +import remarkRehype from "remark-rehype"; +import { type PluggableList, unified } from "unified"; +import { VFile } from "vfile"; + +import { remarkParseWithoutUnusedSyntax } from "./markdownSyntaxGates"; + +export type MarkdownUrlTransform = (url: string, key: string, node: Element) => string; + +interface MarkdownRendererOptions { + readonly remarkPlugins: PluggableList; + readonly rehypePlugins: PluggableList; + readonly components: Partial; + readonly urlTransform: MarkdownUrlTransform; +} + +// Elements are immutable, so a remount (a thread switch, a row scrolled back into view) +// reuses one instead of parsing again. Parsing is most of the cost of rendering markdown. +// An element and the hast it keeps hold about 20 bytes per source character, so ~10 MB. +const MAX_CACHED_CHARS = 500_000; +const rendered = new Map(); +let cachedChars = 0; +let nextRendererId = 0; + +// A short block still holds a whole element tree. +const ENTRY_OVERHEAD_CHARS = 200; +const entrySize = (key: string) => key.length + ENTRY_OVERHEAD_CHARS; + +function remember(key: string, element: ReactElement) { + // One huge message would otherwise evict everything else. + if (entrySize(key) > MAX_CACHED_CHARS / 4) return; + rendered.set(key, element); + cachedChars += entrySize(key); + for (const oldest of rendered.keys()) { + if (cachedChars <= MAX_CACHED_CHARS) break; + rendered.delete(oldest); + cachedChars -= entrySize(oldest); + } +} + +/** Renders markdown as react-markdown does, with one processor per plugin set. */ +export function createMarkdownRenderer({ + remarkPlugins, + rehypePlugins, + components, + urlTransform, +}: MarkdownRendererOptions) { + const processor = unified() + .use(remarkParse) + .use(remarkParseWithoutUnusedSyntax) + .use(remarkPlugins) + .use(remarkRehype, { allowDangerousHtml: true }) + .use(rehypePlugins) + .freeze(); + const keyPrefix = `${nextRendererId++}\u0000`; + + // Literal raw HTML that survived the rehype plugins, and URL attributes, as react-markdown treats them. + const prepare = (node: Nodes) => { + if (!("children" in node)) return; + const { children } = node; + for (let index = 0; index < children.length; index++) { + const child = children[index]!; + if (child.type === "raw") { + children[index] = { type: "text", value: child.value }; + continue; + } + if (child.type !== "element") continue; + const { properties } = child; + for (const key in properties) { + if (!Object.hasOwn(urlAttributes, key)) continue; + const tags = urlAttributes[key]; + if (tags === null || tags?.includes(child.tagName)) { + properties[key] = urlTransform(String(properties[key] || ""), key, child); + } + } + prepare(child); + } + }; + + return (text: string, cache: boolean): ReactElement => { + const key = keyPrefix + text; + const cached = rendered.get(key); + if (cached !== undefined) { + // Refresh its place in the eviction order. + rendered.delete(key); + rendered.set(key, cached); + return cached; + } + const file = new VFile(text); + const tree = processor.runSync(processor.parse(file), file) as Root; + prepare(tree); + const element = toJsxRuntime(tree, { + Fragment, + components, + ignoreInvalidStyle: true, + jsx, + jsxs, + passKeys: true, + passNode: true, + }); + if (cache) remember(key, element); + return element; + }; +} diff --git a/apps/web/src/components/chat/markdownSyntaxGates.ts b/apps/web/src/components/chat/markdownSyntaxGates.ts new file mode 100644 index 0000000000000..57587a51b9d13 --- /dev/null +++ b/apps/web/src/components/chat/markdownSyntaxGates.ts @@ -0,0 +1,204 @@ +import type { Nodes as MdastNodes } from "mdast"; +import { + type Extension as MdastExtension, + fromMarkdown, + type Options as FromMarkdownOptions, + type Transform, +} from "mdast-util-from-markdown"; +import { combineExtensions } from "micromark-util-combine-extensions"; +import type { Construct, Extension, TokenizeContext } from "micromark-util-types"; +import type { Processor } from "unified"; + +// A table needs a delimiter row: dashes with a `|` or `:`, after any `>` or indent. +// One character class per pattern: overlapping ones backtrack quadratically on a run of dashes. +const DELIMITER_LINE = /^[ \t>|:-]+$/gm; +const isDelimiterRow = (line: string) => line.includes("-") && /[|:]/.test(line); +const NEXT_LINE = /[^\r\n]*(?:\r\n?|\n)([^\r\n]*)/y; + +function hasDelimiterRow(text: string) { + for (const [line] of text.matchAll(DELIMITER_LINE)) if (isDelimiterRow(line)) return true; + return false; +} +const PROTOCOL = /https?:\/\//iy; +const WWW = /www\./iy; +const EMAIL_ATEXT = /[\w+.-]*@/y; + +const matchesAt = (pattern: RegExp, text: string, offset: number) => { + pattern.lastIndex = offset; + return pattern.test(text); +}; + +interface Gate { + readonly names: ReadonlyArray; + /** False when the construct cannot match anywhere in the text. */ + readonly inText: (text: string) => boolean; + /** False when it cannot start at this offset, which keeps micromark from trying it there. */ + readonly at?: (context: TokenizeContext, text: string, offset: number) => boolean; +} + +// micromark tries these at every candidate character: email autolinks at every word, tables +// at every line. That is most of what GFM adds to a parse, and most texts cannot match them. +const GATES: ReadonlyArray = [ + { + names: ["emailAutolink"], + inText: (text) => text.includes("@"), + at: (_, text, offset) => matchesAt(EMAIL_ATEXT, text, offset), + }, + { + names: ["protocolAutolink"], + inText: (text) => /https?:\/\//i.test(text), + at: (_, text, offset) => matchesAt(PROTOCOL, text, offset), + }, + { + names: ["wwwAutolink"], + inText: (text) => /www\./i.test(text), + at: (_, text, offset) => matchesAt(WWW, text, offset), + }, + { + names: ["table"], + inText: hasDelimiterRow, + at: (context, text, offset) => { + NEXT_LINE.lastIndex = offset; + const nextLine = NEXT_LINE.exec(text)?.[1]; + return (nextLine !== undefined && hasDelimiterRow(nextLine)) || continuesTable(context); + }, + }, + { names: ["strikethrough"], inText: (text) => text.includes("~") }, + { + names: ["gfmFootnoteCall", "gfmPotentialFootnoteCall", "gfmFootnoteDefinition"], + inText: (text) => text.includes("[^"), + }, + { names: ["tasklistCheck"], inText: (text) => /\[[\sxX]\]/.test(text) }, +]; + +// A body row follows the head or another row. Mirrors `start` in micromark-extension-gfm-table's +// lib/syntax.js: recheck it when that package changes. +function continuesTable({ events }: TokenizeContext) { + let index = events.length - 1; + while (index > -1 && /^(?:lineEnding|linePrefix)$/.test(events[index]![1].type)) index--; + // The table extension's token types are not in micromark's map unless its types are loaded. + const tail: string | undefined = events[index]?.[1].type; + return tail === "tableHead" || tail === "tableRow"; +} + +let parsing = ""; +const gated = new WeakMap(); + +function gatedConstruct(construct: Construct, gate: Gate): Construct { + const at = gate.at; + if (!at) return construct; + let wrapped = gated.get(construct); + if (!wrapped) { + const { previous, tokenize } = construct; + wrapped = { + ...construct, + tokenize(effects, ok, nok) { + return at(this, parsing, this.now().offset) ? tokenize.call(this, effects, ok, nok) : nok; + }, + ...(previous && { + previous(code) { + return previous.call(this, code) && at(this, parsing, this.now().offset); + }, + }), + }; + gated.set(construct, wrapped); + } + return wrapped; +} + +type ConstructMap = Record; + +function gateExtension(extension: Extension, absent: ReadonlySet): Extension { + const gateOf = (name: string | undefined) => + GATES.find((gate) => name && gate.names.includes(name)); + const result: Record = {}; + for (const [field, value] of Object.entries(extension)) { + if (field === "disable" || field === "attentionMarkers" || !value) { + result[field] = value; + continue; + } + const map: ConstructMap = {}; + for (const [code, entry] of Object.entries(value as ConstructMap)) { + const kept = (Array.isArray(entry) ? entry : [entry]).flatMap((construct) => { + const gate = construct && gateOf(construct.name); + if (!construct || !gate) return construct ? [construct] : []; + return absent.has(gate) ? [] : [gatedConstruct(construct, gate)]; + }); + if (kept.length > 0) map[code] = Array.isArray(entry) ? kept : kept[0]; + } + result[field] = map; + } + return result as Extension; +} + +type MdastExtensions = NonNullable; + +// Merged once, as mdast-util-from-markdown merges them on every call: lists in order, handlers by key. +function mergeMdastExtensions(extensions: MdastExtensions): MdastExtension { + const merged = { + canContainEols: [] as string[], + enter: {}, + exit: {}, + transforms: [] as Transform[], + }; + for (const extension of extensions.flat()) { + merged.canContainEols.push(...(extension.canContainEols ?? [])); + Object.assign(merged.enter, extension.enter); + Object.assign(merged.exit, extension.exit); + // GFM looks for autolink literals again in the decoded text outside links, so `www\.x.io` + // links too. Most texts have URLs only in link destinations and code, where it finds nothing. + for (const transform of extension.transforms ?? []) { + merged.transforms.push( + extension.enter?.literalAutolink + ? (tree) => (hasAutolinkText(tree) ? transform(tree) : undefined) + : transform, + ); + } + } + return merged; +} + +const AUTOLINK_TEXT = /@|https?:\/\/|www\./i; + +function hasAutolinkText(node: MdastNodes): boolean { + if (node.type === "text") return AUTOLINK_TEXT.test(node.value); + if (node.type === "link" || node.type === "linkReference" || !("children" in node)) return false; + return node.children.some(hasAutolinkText); +} + +/** + * Parses as remark-parse does, without the syntax extension constructs that cannot match the + * text. Same tree, in about half the time. + */ +function attachParserWithoutUnusedSyntax(this: Processor) { + const data = this.data(); + const byMask = new Map(); + let mdastExtensions: MdastExtension[] | undefined; + this.parser = (document) => { + let mask = 0; + GATES.forEach((gate, index) => { + if (!gate.inText(document)) mask |= 1 << index; + }); + let extensions = byMask.get(mask); + if (!extensions) { + const absent = new Set(GATES.filter((_, index) => mask & (1 << index))); + extensions = [ + combineExtensions( + (data.micromarkExtensions ?? []).map((extension) => gateExtension(extension, absent)), + ), + ]; + byMask.set(mask, extensions); + } + mdastExtensions ??= [mergeMdastExtensions(data.fromMarkdownExtensions ?? [])]; + const outer = parsing; + // Gates read micromark offsets, which skip a leading byte order mark. + parsing = document.charCodeAt(0) === 0xfeff ? document.slice(1) : document; + try { + return fromMarkdown(document, { ...data.settings, extensions, mdastExtensions }); + } finally { + parsing = outer; + } + }; +} + +export const remarkParseWithoutUnusedSyntax = attachParserWithoutUnusedSyntax; diff --git a/apps/web/src/components/chat/timelineMinimapItems.test.ts b/apps/web/src/components/chat/timelineMinimapItems.test.ts index 20d4c75997b1d..776cd39096a8a 100644 --- a/apps/web/src/components/chat/timelineMinimapItems.test.ts +++ b/apps/web/src/components/chat/timelineMinimapItems.test.ts @@ -1,6 +1,38 @@ import { describe, expect, it } from "vite-plus/test"; +import { MessageId } from "@t3tools/contracts"; +import { ATTACHMENT_ONLY_BOOTSTRAP_PROMPT } from "@t3tools/shared/composerContextReferences"; -import { resolveTimelineMinimapPreview } from "./timelineMinimapItems"; +import { deriveTimelineMinimapItems, resolveTimelineMinimapPreview } from "./timelineMinimapItems"; + +describe("deriveTimelineMinimapItems", () => { + it.each(["", "Ultrathink:\n"])( + "omits bootstrap text from the minimap label and tooltip (%j)", + (prefix) => { + const at = "2026-10-01T00:00:00Z"; + const items = deriveTimelineMinimapItems([ + { + kind: "message", + id: "image-only", + createdAt: at, + showAssistantMeta: false, + showAssistantCopyButton: false, + assistantCopyStreaming: false, + message: { + id: MessageId.make("image-only"), + role: "user", + text: prefix + ATTACHMENT_ONLY_BOOTSTRAP_PROMPT, + runId: null, + streaming: false, + createdAt: at, + updatedAt: at, + }, + }, + ]); + expect(items).toEqual([{ id: "image-only", rowIndex: 0, userText: "", assistantText: null }]); + expect(resolveTimelineMinimapPreview(items[0]!)?.userText).toBeNull(); + }, + ); +}); describe("resolveTimelineMinimapPreview", () => { it("previews a thread autolink as prose, not its URL", () => { diff --git a/apps/web/src/components/chat/timelineMinimapItems.ts b/apps/web/src/components/chat/timelineMinimapItems.ts index 099c58379ccf6..3c730df680e54 100644 --- a/apps/web/src/components/chat/timelineMinimapItems.ts +++ b/apps/web/src/components/chat/timelineMinimapItems.ts @@ -1,4 +1,5 @@ import { parseThreadLinkHref } from "@t3tools/shared/threadLinks"; +import { isAttachmentOnlyBootstrapPrompt } from "@t3tools/shared/composerContextReferences"; import type { Nodes } from "mdast"; import remarkGfm from "remark-gfm"; import remarkParse from "remark-parse"; @@ -26,7 +27,7 @@ export function deriveTimelineMinimapItems( items.push({ id: row.id, rowIndex: index, - userText: row.message.text, + userText: isAttachmentOnlyBootstrapPrompt(row.message.text) ? "" : row.message.text, assistantText: resolveFinalAssistantTextForTurn(rows, index), }); } diff --git a/apps/web/src/components/chat/useChatTerminalActions.ts b/apps/web/src/components/chat/useChatTerminalActions.ts index 0ef4ca8c47c9a..16e74bfa82aef 100644 --- a/apps/web/src/components/chat/useChatTerminalActions.ts +++ b/apps/web/src/components/chat/useChatTerminalActions.ts @@ -23,7 +23,11 @@ import { squashAtomCommandFailure, } from "@t3tools/client-runtime/state/runtime"; import { MAX_TERMINALS_PER_GROUP } from "../../types"; -import { type RightPanelSurface, useRightPanelStore } from "../../rightPanelStore"; +import { + selectThreadRightPanelState, + type RightPanelSurface, + useRightPanelStore, +} from "../../rightPanelStore"; import { confirmTerminalClose } from "../../lib/terminalCloseConfirm"; import { findReusableProjectScriptTerminal } from "../../lib/projectScriptTerminal"; import { type TerminalContextSelection } from "../../lib/terminalContext"; @@ -183,7 +187,7 @@ export function useChatTerminalActions(input: ChatTerminalActionsInput) { const createNewTerminalStable = useStableCallback(createNewTerminal); const closeTerminal = (terminalId: string) => { if (!hasTerminalWriteAccess() || !activeThreadRef) return; - terminalActions.closeTerminalSession(terminalId); + terminalActions.closeTerminalSession(terminalId, activeThreadRef); storeCloseTerminal(activeThreadRef, terminalId); focusActiveDrawerTerminal(activeThreadRef); }; @@ -337,21 +341,17 @@ export function useChatTerminalActions(input: ChatTerminalActionsInput) { focusTerminal(activeThreadRef, terminalId); }; const activatePanelTerminalStable = useStableCallback(activatePanelTerminal); - const closePanelTerminal = (terminalId: string) => { - if ( - !hasTerminalWriteAccess() || - !activeThreadRef || - activeRightPanelSurface?.kind !== "terminal" - ) - return; - terminalActions.closeTerminalSession(terminalId); - storeCloseTerminal(activeThreadRef, terminalId); - useRightPanelStore - .getState() - .closeTerminal(activeThreadRef, activeRightPanelSurface.id, terminalId); - // Closing a surface's last terminal falls back to a neighbouring surface, - // which can be another terminal surface. - focusActivePanelTerminal(activeThreadRef); + const closePanelTerminal = (terminalId: string, target = activeThreadRef) => { + if (!target || !readEnvironmentScope(target.environmentId, AuthTerminalOperateScope)) return; + const store = useRightPanelStore.getState(); + const surface = selectThreadRightPanelState(store.byThreadKey, target).surfaces.find( + (entry) => entry.kind === "terminal" && entry.terminalIds.includes(terminalId), + ); + if (!surface) return; + terminalActions.closeTerminalSession(terminalId, target); + storeCloseTerminal(target, terminalId); + store.closeTerminal(target, surface.id, terminalId); + focusActivePanelTerminal(target); }; const closePanelTerminalStable = useStableCallback(closePanelTerminal); const requestCloseTerminal = (terminalId: string) => { diff --git a/apps/web/src/components/chat/useComposerContextImport.browser.test.tsx b/apps/web/src/components/chat/useComposerContextImport.browser.test.tsx new file mode 100644 index 0000000000000..2caae47f6e08a --- /dev/null +++ b/apps/web/src/components/chat/useComposerContextImport.browser.test.tsx @@ -0,0 +1,158 @@ +import { useEffect } from "react"; +import { type ComposerContextRecord, ComposerContextId, EnvironmentId } from "@t3tools/contracts"; +import { expect, it, vi } from "vite-plus/test"; + +import { act, render, stubGlobal } from "../../../test/render"; +import { DraftId, useComposerDraftStore } from "../../composerDraftStore"; +import { formatInlineContextReference } from "../../lib/composerContextReferences"; +import { rememberCopiedComposerAttachments } from "../../lib/copiedComposerAttachments"; +import { pendingDraftWork } from "./pendingDraftWork"; +import { useComposerContextImport } from "./useComposerContextImport"; + +vi.mock("~/state/use-atom-query-runner", async (importOriginal) => ({ + ...(await importOriginal()), + useAtomQueryRunner: () => async () => ({ _tag: "Success", value: { relativeUrl: "/file" } }), +})); +vi.mock("../../state/use-atom-command", async (importOriginal) => ({ + ...(await importOriginal()), + useAtomCommand: () => () => {}, +})); +vi.mock("~/state/session", async (importOriginal) => ({ + ...(await importOriginal()), + readPreparedConnection: () => ({ httpBaseUrl: "http://localhost" }), +})); + +type ImportInput = Parameters[0]; +type Importer = ReturnType; + +function Harness({ input, ready }: { input: ImportInput; ready: (value: Importer) => void }) { + const importer = useComposerContextImport(input); + useEffect(() => ready(importer)); + return null; +} + +function deferred() { + let resolve!: (value: T) => void; + const promise = new Promise((fulfill) => { + resolve = fulfill; + }); + return { promise, resolve }; +} + +it.each([ + { kind: "file", edit: "delete" }, + { kind: "image", edit: "delete" }, + { kind: "file", edit: "undo" }, + { kind: "file", edit: "retain" }, + { kind: "file", edit: "switch" }, + { kind: "file", edit: "cached" }, + { kind: "file", edit: "question" }, + { kind: "image", edit: "question" }, + // Coder: no preview annotations, so upstream's annotation and delete-annotation cases are cut. +] as const)("settles a delayed $kind import after $edit", async ({ kind, edit }) => { + const target = DraftId.make(`clipboard-${kind}-${edit}`); + const store = useComposerDraftStore.getState(); + using _draft = { [Symbol.dispose]: () => store.clearComposerContent(target) }; + const sourceEnvironmentId = EnvironmentId.make("clipboard-source"); + rememberCopiedComposerAttachments( + sourceEnvironmentId, + edit === "cached" + ? new Map([["source-attachment", new File(["notes"], "notes.txt")]]) + : new Map(), + ); + using _copy = { + [Symbol.dispose]: () => rememberCopiedComposerAttachments(sourceEnvironmentId, new Map()), + }; + const promptRef = { current: "" }; + const downloaded = deferred(); + const requested = deferred(); + const settled = deferred(); + const input = { + environmentId: EnvironmentId.make("clipboard-target"), + activeThread: undefined, + activeThreadId: null, + composerDraftTarget: target, + attachmentDraftTarget: target, + attachmentTargetKey: target, + attachmentTargetKeyRef: { current: target }, + questionAttachmentTarget: edit === "question" ? target : null, + pendingUserInputs: [], + composerContextRecords: new Map(), + composerFilesRef: { current: [] }, + pendingThreadImportsRef: { current: new Map() }, + promptRef, + addComposerFilesToDraft: (files: Parameters[1]) => + store.addFiles(target, files, { appendReference: edit !== "question" }), + } satisfies ImportInput & { promptRef: { current: string } }; + let importer!: Importer; + await using _screen = await render( + { + importer = value; + }} + />, + ); + const end = pendingDraftWork.end.bind(pendingDraftWork); + using _settled = vi.spyOn(pendingDraftWork, "end").mockImplementation((key) => { + end(key); + settled.resolve(); + }); + using _fetch = stubGlobal("fetch", () => { + requested.resolve(); + return downloaded.promise; + }); + const contextId = ComposerContextId.make(`${kind}_source`); + const attachmentFields = { + version: 1 as const, + contextId, + label: "notes.txt", + attachmentId: "source-attachment", + name: "notes.txt", + mimeType: kind === "file" ? "text/plain" : "image/png", + sizeBytes: 5, + }; + const attachment = ( + kind === "file" + ? { ...attachmentFields, kind: "file" as const } + : { ...attachmentFields, kind: "image" as const } + ) satisfies ComposerContextRecord; + let pastedPrompt = ""; + await act(async () => { + const remapped = importer.importContextRecords([attachment], sourceEnvironmentId); + pastedPrompt = formatInlineContextReference({ + kind, + contextId: remapped.get(contextId)!, + label: "notes.txt", + }); + if (edit === "question") pastedPrompt = "Question answer"; + promptRef.current = pastedPrompt; + store.setPrompt(target, pastedPrompt); + if (edit !== "cached") await requested.promise; + }); + if (edit !== "cached") expect(pendingDraftWork.has(target)).toBe(true); + await act(() => { + if (edit !== "retain" && edit !== "cached" && edit !== "question" && edit !== "switch") { + promptRef.current = "Keep this edit"; + store.setPrompt(target, promptRef.current); + } + if (edit === "undo") { + promptRef.current = pastedPrompt; + store.setPrompt(target, pastedPrompt); + } + if (edit === "switch") input.attachmentTargetKeyRef.current = DraftId.make("other-draft"); + }); + await act(async () => { + downloaded.resolve(new Response("notes", { headers: { "Content-Type": attachment.mimeType } })); + await settled.promise; + }); + expect(pendingDraftWork.has(target)).toBe(false); + const draft = store.getComposerDraft(target)!; + expect(draft.prompt).toBe(promptRef.current); + const attachments = kind === "file" ? draft.files : draft.images; + expect(attachments.map((file) => file.name)).toEqual( + edit === "undo" || edit === "retain" || edit === "cached" || edit === "question" + ? ["notes.txt"] + : [], + ); +}); diff --git a/apps/web/src/components/chat/useComposerContextImport.ts b/apps/web/src/components/chat/useComposerContextImport.ts index 42cd1e77e1427..88224fa5504a9 100644 --- a/apps/web/src/components/chat/useComposerContextImport.ts +++ b/apps/web/src/components/chat/useComposerContextImport.ts @@ -35,6 +35,7 @@ import { import { assetEnvironment } from "~/state/assets"; import { copiedComposerAttachmentFile } from "~/lib/copiedComposerAttachments"; import { countQuestionAttachments, questionAttachmentDraftId } from "../../questionAttachments"; +import { collectInlineContextIds } from "../../lib/composerContextReferences"; import { orchestrationEnvironment } from "~/state/orchestration"; import { pendingDraftWork } from "./pendingDraftWork"; import { randomUUID } from "~/lib/utils"; @@ -80,6 +81,7 @@ export function useComposerContextImport(input: { readonly pendingUserInputs: ReadonlyArray; readonly composerContextRecords: ComposerDraftContextRecords; readonly composerFilesRef: ChatComposerProps["composerFilesRef"]; + readonly promptRef: RefObject; readonly pendingThreadImportsRef: RefObject>; readonly addComposerFilesToDraft: (files: ComposerFileAttachment[]) => string[]; }) { @@ -95,6 +97,7 @@ export function useComposerContextImport(input: { pendingUserInputs, composerContextRecords, composerFilesRef, + promptRef, pendingThreadImportsRef, addComposerFilesToDraft, } = input; @@ -176,6 +179,14 @@ export function useComposerContextImport(input: { fail("Downloading it from the source failed."); return; } + // Question attachments have no inline reference. Coder: no preview annotations, so no + // annotation owns a screenshot. + const references = new Set(collectInlineContextIds(promptRef.current)); + if ( + !questionAttachmentTarget && + !references.has(toKindScopedComposerContextId(record.kind, localId)) + ) + return; file = new File([blob], record.name, { type: record.mimeType || blob.type }); } // The draft these bytes belong to may have been sent or switched away from while they diff --git a/apps/web/src/components/chat/useComposerMultilinePrompt.browser.test.tsx b/apps/web/src/components/chat/useComposerMultilinePrompt.browser.test.tsx new file mode 100644 index 0000000000000..f2f14684b208c --- /dev/null +++ b/apps/web/src/components/chat/useComposerMultilinePrompt.browser.test.tsx @@ -0,0 +1,23 @@ +import { expect, it } from "vite-plus/test"; + +import { render } from "../../../test/render"; +import { useComposerMultilinePrompt } from "./useComposerMultilinePrompt"; + +function Composer({ text }: { readonly text: string }) { + const [multiline, observeBody] = useComposerMultilinePrompt(); + return ( +
+
+ {text} +
+ {multiline ? "multiline" : "single line"} +
+ ); +} + +it("notices when typing makes the prompt wrap", async () => { + await using screen = await render(, { router: false }); + await expect.element(screen.getByText("single line")).toBeVisible(); + await screen.rerender(); + await expect.element(screen.getByText("multiline")).toBeVisible(); +}); diff --git a/apps/web/src/components/chat/useComposerMultilinePrompt.ts b/apps/web/src/components/chat/useComposerMultilinePrompt.ts index c09c371aa997a..b9462ffe46aab 100644 --- a/apps/web/src/components/chat/useComposerMultilinePrompt.ts +++ b/apps/web/src/components/chat/useComposerMultilinePrompt.ts @@ -27,9 +27,16 @@ export function useComposerMultilinePrompt(): readonly [ const [isMultiline, setIsMultiline] = useState(false); const observeBody = (body: HTMLElement | null) => { if (!body) return; + // Measuring reads layout, so one measurement per frame runs after all of the frame's DOM + // writes instead of forcing a layout after each mutation. + let frame: number | null = null; const measure = () => { - const next = measureComposerMultilinePrompt(body); - if (next !== null) setIsMultiline(next); + if (frame !== null) return; + frame = requestAnimationFrame(() => { + frame = null; + const next = measureComposerMultilinePrompt(body); + if (next !== null) setIsMultiline(next); + }); }; measure(); const editor = body.querySelector('[data-testid="composer-editor"]'); @@ -37,6 +44,7 @@ export function useComposerMultilinePrompt(): readonly [ const mutationObserver = new MutationObserver(measure); mutationObserver.observe(body, { childList: true, characterData: true, subtree: true }); return () => { + if (frame !== null) cancelAnimationFrame(frame); stopObserving(); mutationObserver.disconnect(); }; diff --git a/apps/web/src/components/chat/useComposerProviderModel.ts b/apps/web/src/components/chat/useComposerProviderModel.ts index 7fd1130859fec..7d36d56b08922 100644 --- a/apps/web/src/components/chat/useComposerProviderModel.ts +++ b/apps/web/src/components/chat/useComposerProviderModel.ts @@ -70,6 +70,7 @@ export function useComposerProviderModel( providers: providerStatuses, selectedProvider, selectedInstanceId, + requestedInstanceId: selection.requestedInstanceId, threadModelSelection, projectModelSelection, settings, diff --git a/apps/web/src/components/chat/useComposerResume.browser.test.tsx b/apps/web/src/components/chat/useComposerResume.browser.test.tsx new file mode 100644 index 0000000000000..53d691bb224e7 --- /dev/null +++ b/apps/web/src/components/chat/useComposerResume.browser.test.tsx @@ -0,0 +1,93 @@ +import { EnvironmentId, ProviderInstanceId, RunId, ThreadId } from "@t3tools/contracts"; +import type { AtomCommandResult } from "@t3tools/client-runtime/state/runtime"; +import { AsyncResult } from "effect/reactivity"; +import { expect, it, vi } from "vite-plus/test"; + +const state = vi.hoisted(() => ({ + startedTurn: null as ReturnType> | null, +})); + +vi.mock(import("../../state/use-orchestration-command"), async (importOriginal) => ({ + ...(await importOriginal()), + useOrchestrationCommand: (() => async (value: { readonly input: unknown }) => { + state.startedTurn?.resolve(value.input); + return AsyncResult.success(undefined); + }) as never, +})); + +import { render } from "../../../test/render"; +import type { ComposerHandleRef } from "../../composerHandleContext"; +import { useComposerResume } from "./useComposerResume"; + +function deferred() { + let resolve!: (value: T) => void; + const promise = new Promise((complete) => { + resolve = complete; + }); + return { promise, resolve }; +} + +function setup() { + const startedTurn = deferred(); + state.startedTurn = startedTurn; + return { startedTurn }; +} + +const threadA = ThreadId.make("thread:a"); +const threadAModel = { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.5" }; +const threadBModel = { instanceId: ProviderInstanceId.make("claudeAgent"), model: "claude-opus" }; + +function composerShowing(selectedModelSelection: typeof threadAModel) { + return { getSendContext: () => ({ selectedModelSelection }) } as never; +} + +function ResumeButton(props: { + readonly composerRef: ComposerHandleRef; + readonly persistSettings: () => Promise>; +}) { + const { onResume } = useComposerResume({ + environmentId: EnvironmentId.make("environment:test"), + routeThreadKey: "environment:test:thread:a", + currentRouteThreadKeyRef: { current: "environment:test:thread:a" }, + activeThread: { id: threadA } as never, + resumableRunId: RunId.make("run:interrupted"), + hasHeldQueuedRuns: false, + isSendBusy: false, + isRevertingCheckpoint: false, + threadDetailLoading: false, + activeEnvironmentUnavailable: false, + runtimeMode: "full-access", + interactionMode: "default", + localCheckoutBranchMismatch: null, + composerRef: props.composerRef, + persistThreadSettingsForNextTurn: props.persistSettings, + scrollToEnd: () => undefined, + setThreadError: () => undefined, + // Coder: no usage-limits panel, so there is no `clearUsageLimitsFor` input. + }); + return ( + + ); +} + +it("continues on the resumed thread's model after the composer moves to another thread", async () => { + const { startedTurn } = setup(); + const settingsPersisted = deferred>(); + // ChatView keeps one composer across thread routes, so its ref follows navigation. + const composerRef: ComposerHandleRef = { current: composerShowing(threadAModel) }; + await using screen = await render( + settingsPersisted.promise} />, + ); + + await screen.getByRole("button", { name: "Resume" }).click(); + composerRef.current = composerShowing(threadBModel); + settingsPersisted.resolve(AsyncResult.success(undefined)); + + expect(await startedTurn.promise).toMatchObject({ + threadId: threadA, + manualContinuationOfRunId: "run:interrupted", + modelSelection: threadAModel, + }); +}); diff --git a/apps/web/src/components/chat/useComposerResume.ts b/apps/web/src/components/chat/useComposerResume.ts index 9f7647148e81e..3a703c405ae94 100644 --- a/apps/web/src/components/chat/useComposerResume.ts +++ b/apps/web/src/components/chat/useComposerResume.ts @@ -103,6 +103,9 @@ export function useComposerResume(input: ComposerResumeInput) { if (resumableRunId === null) { return resumeThreadQueue({ environmentId, input: { threadId } }); } + // Read before the first await: the composer follows the route, so after the user switches + // threads it holds the other thread's model. + const modelSelection = composerRef.current?.getSendContext().selectedModelSelection; const createdAt = new Date().toISOString(); const settingsResult = await persistThreadSettingsForNextTurn({ threadId, @@ -114,7 +117,6 @@ export function useComposerResume(input: ComposerResumeInput) { interactionMode, }); if (settingsResult._tag === "Failure") return settingsResult; - const modelSelection = composerRef.current?.getSendContext().selectedModelSelection; const turnResult = await startThreadTurn({ environmentId, input: { diff --git a/apps/web/src/components/chat/useComposerSend.ts b/apps/web/src/components/chat/useComposerSend.ts index ecd8548226349..1906c3405be12 100644 --- a/apps/web/src/components/chat/useComposerSend.ts +++ b/apps/web/src/components/chat/useComposerSend.ts @@ -1158,6 +1158,7 @@ export function useComposerSend(input: ComposerSendInput) { ) { handleNewThreadInActiveProject(); } + // Coder: no GitHub star prompt (#27832), so sends are not counted toward one. } } diff --git a/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx b/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx index 08e919cb152ec..2bd46f93cf874 100644 --- a/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx +++ b/apps/web/src/components/chat/useQueuedRunEdit.browser.test.tsx @@ -1,6 +1,7 @@ import type { ThreadStructure } from "@t3tools/client-runtime/state/shell"; import { EnvironmentId, + ChatAttachmentId, MessageId, RunId, ThreadId, @@ -8,9 +9,13 @@ import { } from "@t3tools/contracts"; import { useImperativeHandle, type RefObject } from "react"; import { describe, expect, it, vi } from "vite-plus/test"; +import { ATTACHMENT_ONLY_BOOTSTRAP_PROMPT } from "@t3tools/shared/composerContextReferences"; +import { toastManager } from "../ui/toast"; import { act, render } from "../../../test/render"; -import { useComposerDraftStore } from "../../composerDraftStore"; +import { useComposerDraftStore, useComposerThreadDraft } from "../../composerDraftStore"; +import { deriveComposerSendState } from "../ChatView.logic"; +import { ComposerPrimaryActions } from "./ComposerPrimaryActions"; import { AppAtomRegistryProvider, appAtomRegistry } from "../../rpc/atomRegistry"; import { useQueuedRunEdit } from "./useQueuedRunEdit"; @@ -34,6 +39,10 @@ vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: () => mocks.editQueuedRun, })); +vi.mock("../../hooks/useSettings", () => ({ + useEnvironmentIdentificationMode: () => "none", +})); + const runsKey = (ref: ScopedThreadRef) => `${ref.environmentId}:${ref.threadId}`; vi.mock("../../state/threads", async () => { @@ -88,7 +97,34 @@ function Harness(props: { composerRef: { current: null }, }); useImperativeHandle(props.hookRef, () => result); - return null; + const draft = useComposerThreadDraft(result.draftTarget); + const sendState = deriveComposerSendState({ + prompt: draft.prompt, + imageCount: draft.images.length + draft.files.length, + existingAttachmentCount: result.editingAttachments?.length ?? 0, + terminalContexts: draft.terminalContexts, + }); + return result.isEditing ? ( + 0} + isSendBusy={result.isSaving} + sendDisabledReason={null} + isEnvironmentUnavailable={false} + isPreparingWorktree={false} + hasSendableContent={sendState.hasSendableContent} + isEditingQueuedMessage + onSubmitMessage={() => void result.saveEdit(savedContent(draft.prompt), () => {})} + onPreviousPendingQuestion={() => {}} + onInterrupt={() => {}} + onImplementPlanInNewThread={() => {}} + /> + ) : null; } function view(threadRef: ScopedThreadRef, runStatus: string, hook: { current: HookResult | null }) { @@ -140,6 +176,94 @@ const promptOf = (ref: ScopedThreadRef) => useComposerDraftStore.getState().getComposerDraft(ref)?.prompt ?? ""; describe("useQueuedRunEdit", () => { + it.each(["", "Ultrathink:\n"])( + "opens attachment-only edits without the placeholder and preserves effort on save (%j)", + async (prefix) => { + const { hook } = setup(); + mocks.editQueuedRun.mockResolvedValue({ _tag: "Success" }); + await using screen = await render(view(THREAD_A, "queued", hook)); + using toast = vi.spyOn(toastManager, "add"); + const attachments = [ + { + type: "image" as const, + id: ChatAttachmentId.make("kept"), + name: "kept.png", + mimeType: "image/png", + sizeBytes: 10, + }, + ]; + const request = { + runId: RUN, + messageId: MessageId.make("message-1"), + text: prefix + ATTACHMENT_ONLY_BOOTSTRAP_PROMPT, + attachments, + }; + await act(() => requireHook(hook).beginEdit(request)); + expect( + useComposerDraftStore.getState().getComposerDraft(requireHook(hook).draftTarget)?.prompt ?? + "", + ).toBe(prefix.trimEnd()); + expect(requireHook(hook).editingAttachments?.map(({ attachment }) => attachment)).toEqual( + attachments, + ); + await act(() => + useComposerDraftStore + .getState() + .setRuntimeMode(requireHook(hook).draftTarget, "full-access"), + ); + await act(() => setRuns(THREAD_A, "running")); + expect(toast).not.toHaveBeenCalled(); + expect(requireHook(hook).isEditing).toBe(false); + await act(() => setRuns(THREAD_A, "queued")); + await act(() => requireHook(hook).beginEdit(request)); + const update = screen.getByRole("button", { name: "Update queued message" }); + await expect.element(update).toBeEnabled(); + await update.click(); + await vi.waitFor(() => expect(mocks.editQueuedRun).toHaveBeenCalledOnce()); + expect(mocks.editQueuedRun).toHaveBeenCalledWith( + expect.objectContaining({ + input: expect.objectContaining({ + text: prefix + ATTACHMENT_ONLY_BOOTSTRAP_PROMPT, + edit: expect.objectContaining({ attachments }), + }), + }), + ); + }, + ); + + it("saves an effort-only prompt without claiming removed attachments remain", async () => { + const { hook } = setup(); + mocks.editQueuedRun.mockResolvedValue({ _tag: "Success" }); + await using screen = await render(view(THREAD_A, "queued", hook)); + await act(() => + requireHook(hook).beginEdit({ + runId: RUN, + messageId: MessageId.make("message-1"), + text: `Ultrathink:\n${ATTACHMENT_ONLY_BOOTSTRAP_PROMPT}`, + attachments: [ + { + type: "image", + id: ChatAttachmentId.make("removed"), + name: "removed.png", + mimeType: "image/png", + sizeBytes: 10, + }, + ], + }), + ); + await act(() => requireHook(hook).removeEditingAttachment("removed")); + await screen.getByRole("button", { name: "Update queued message" }).click(); + await vi.waitFor(() => expect(mocks.editQueuedRun).toHaveBeenCalledOnce()); + expect(mocks.editQueuedRun).toHaveBeenCalledWith( + expect.objectContaining({ + input: expect.objectContaining({ + text: "Ultrathink:", + edit: expect.objectContaining({ attachments: [] }), + }), + }), + ); + }); + it("keeps an open edit when the user leaves the thread and comes back", async () => { const { hook } = setup(); await using screen = await render(view(THREAD_A, "queued", hook)); diff --git a/apps/web/src/components/chat/useQueuedRunEdit.ts b/apps/web/src/components/chat/useQueuedRunEdit.ts index 3a082464b425c..af22cad990f93 100644 --- a/apps/web/src/components/chat/useQueuedRunEdit.ts +++ b/apps/web/src/components/chat/useQueuedRunEdit.ts @@ -17,7 +17,10 @@ import { type ThreadId, } from "@t3tools/contracts"; import { scopedThreadKey } from "@t3tools/client-runtime/environment"; -import { ATTACHMENT_ONLY_BOOTSTRAP_PROMPT } from "@t3tools/shared/composerContextReferences"; +import { + restoreAttachmentOnlyBootstrapPrompt, + stripAttachmentOnlyBootstrapPrompt, +} from "@t3tools/shared/composerContextReferences"; import { Atom } from "effect/reactivity"; import type { RefObject } from "react"; import { useAssetUrls } from "../../assets/assetUrls"; @@ -141,7 +144,7 @@ async function submitQueuedRunEdit(input: { input: { threadId: editing.threadId, runId: editing.runId, - text: editText.length === 0 ? ATTACHMENT_ONLY_BOOTSTRAP_PROMPT : editText, + text: restoreAttachmentOnlyBootstrapPrompt(editText, uploads.length > 0), edit: { messageId: editing.messageId, attachments: uploads, @@ -222,9 +225,10 @@ export function useQueuedRunEdit(input: { if (activeThreadId === null) return; if (editing !== null && editing.runId !== request.runId) clearEditDraft(editing.runId); const target = queuedEditDraftTarget(routeThreadRef, request.runId); + const text = stripAttachmentOnlyBootstrapPrompt(request.text); const store = useComposerDraftStore.getState(); store.clearComposerContent(target); - store.setPrompt(target, request.text); + store.setPrompt(target, text); const threadRef = { environmentId, threadId: activeThreadId }; queuedRunEdits.begin( editKey, @@ -232,7 +236,7 @@ export function useQueuedRunEdit(input: { threadId: activeThreadId, runId: request.runId, messageId: request.messageId, - originalText: request.text, + originalText: text, existingAttachments: request.attachments, context: serverProjection?.messages.find((message) => message.id === request.messageId) ?.context, diff --git a/apps/web/src/components/chat/useSystemComposerBanners.tsx b/apps/web/src/components/chat/useSystemComposerBanners.tsx index 9f0ffb84dbab0..064ff41813a67 100644 --- a/apps/web/src/components/chat/useSystemComposerBanners.tsx +++ b/apps/web/src/components/chat/useSystemComposerBanners.tsx @@ -47,6 +47,7 @@ export function useSystemComposerBanners(input: SystemComposerBannersInput) { ENVIRONMENT_RECONNECT_WARNING_GRACE_MS, ); const handleReconnectActiveEnvironment = async (environmentId: EnvironmentId) => { + // Coder: the Coder config enables every workspace, so none is switched off to turn back on. const result = await retryEnvironment(environmentId); if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) { const error = squashAtomCommandFailure(result); diff --git a/apps/web/src/components/chat/useThreadLifecycle.browser.test.tsx b/apps/web/src/components/chat/useThreadLifecycle.browser.test.tsx new file mode 100644 index 0000000000000..ad3857cfc9e50 --- /dev/null +++ b/apps/web/src/components/chat/useThreadLifecycle.browser.test.tsx @@ -0,0 +1,183 @@ +import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment"; +import { EnvironmentId, ThreadId } from "@t3tools/contracts"; +import { expect, it, vi } from "vite-plus/test"; +import { page } from "vite-plus/test/browser"; + +const state = vi.hoisted(() => ({ + switchRef: (async () => undefined) as (input: unknown) => Promise, +})); + +vi.mock(import("../../state/session"), async (importOriginal) => ({ + ...(await importOriginal()), + useEnvironmentScope: (() => true) as never, + readEnvironmentScope: (() => true) as never, +})); + +vi.mock(import("../../state/use-atom-command"), async (importOriginal) => ({ + ...(await importOriginal()), + useAtomCommand: (() => (input: unknown) => state.switchRef(input)) as never, +})); + +vi.mock(import("../../state/use-orchestration-command"), async (importOriginal) => ({ + ...(await importOriginal()), + useOrchestrationCommand: (() => async () => ({ _tag: "Success", value: undefined })) as never, +})); + +vi.mock(import("../../hooks/useThreadActions"), async (importOriginal) => ({ + ...(await importOriginal()), + useThreadActions: (() => ({})) as never, + useAcknowledgeThreadWoke: (() => () => undefined) as never, +})); + +vi.mock(import("../../state/entities"), async (importOriginal) => ({ + ...(await importOriginal()), + useChildThreadInputs: (() => []) as never, +})); + +import { render } from "../../../test/render"; +import { useThreadLifecycle } from "./useThreadLifecycle"; + +const environmentId = EnvironmentId.make("environment:test"); + +function setup() { + state.switchRef = async () => undefined; +} + +function threadInput(threadId: string, branch: string) { + const ref = scopeThreadRef(environmentId, ThreadId.make(threadId)); + const thread = { id: ref.threadId, environmentId, branch, worktreePath: null }; + return { + routeThreadKey: scopedThreadKey(ref), + activeThread: thread as never, + activeThreadId: ref.threadId, + activeThreadRef: ref, + threadDetailRef: ref, + activeThreadKey: scopedThreadKey(ref), + localCheckoutBranchMismatch: { threadBranch: branch, currentBranch: "main" }, + }; +} + +type ThreadInput = ReturnType; + +function RestoreBranchHarness(props: { + readonly thread: ThreadInput; + readonly currentRouteThreadKeyRef: { readonly current: string | null }; + readonly refreshGitStatus: () => void; + readonly focusComposer: () => void; +}) { + const lifecycle = useThreadLifecycle({ + ...props.thread, + environmentId, + currentRouteThreadKeyRef: props.currentRouteThreadKeyRef, + canOperateThread: true, + clientSettingsHydrated: true, + activeThreadShell: null, + activeLatestRun: null as never, + activeProjectCwd: "/repo", + serverConfig: null, + runtimeMode: "full-access" as never, + isWorking: false, + pendingBackgroundTasks: [], + turnItems: [], + activePendingApproval: null as never, + activePendingUserInput: null as never, + gitStatusQuery: { refresh: props.refreshGitStatus }, + nowMinute: "", + composerRef: { current: { focusAtEnd: props.focusComposer } } as never, + composerDraftTarget: { kind: "thread", ref: props.thread.activeThreadRef } as never, + sendInFlightRef: { current: false }, + setOptimisticUserMessages: () => undefined, + setThreadError: () => undefined, + // Coder: no usage-limits panel, so there is no `clearUsageLimitsFor` input. + beginLocalDispatch: () => undefined, + resetLocalDispatch: () => undefined, + scrollToEnd: () => undefined, + persistThreadSettingsForNextTurn: async () => ({ _tag: "Success", value: undefined }) as never, + }); + return ( + + ); +} + +it.each(["a", "b", "navigation"] as const)( + "isolates restore completion and queued focus for %s", + async (scenario) => { + const first = scenario === "b" ? "b" : "a"; + setup(); + const completions: Array<(result: unknown) => void> = []; + const switchRef = vi.fn(() => new Promise((resolve) => completions.push(resolve))); + state.switchRef = switchRef; + const threadA = threadInput("thread:a", "feature-a"); + const threadB = threadInput("thread:b", "feature-b"); + const currentRouteThreadKeyRef = { current: threadA.routeThreadKey }; + const refreshGitStatus = vi.fn(); + const focusComposer = vi.fn(); + const harness = (thread: ThreadInput) => ( + + ); + await using screen = await render(harness(threadA)); + const focusFrames: FrameRequestCallback[] = []; + using _frame = vi.spyOn(window, "requestAnimationFrame").mockImplementation((callback) => { + focusFrames.push(callback); + return 0; + }); + + await page.getByRole("button", { name: "Restore branch" }).click(); + await expect.element(page.getByRole("button", { name: "Restoring..." })).toBeDisabled(); + + // ChatView stays mounted across server threads, so the hook sees thread B in place. + currentRouteThreadKeyRef.current = threadB.routeThreadKey; + await screen.rerender(harness(threadB)); + await expect.element(page.getByRole("button", { name: "Restore branch" })).toBeEnabled(); + await page.getByRole("button", { name: "Restore branch" }).click(); + expect(switchRef).toHaveBeenLastCalledWith( + expect.objectContaining({ input: { cwd: "/repo", refName: "feature-b" } }), + ); + + // Back on thread A, its own restore is still pending, so it can't start a second one. + currentRouteThreadKeyRef.current = threadA.routeThreadKey; + await screen.rerender(harness(threadA)); + await expect.element(page.getByRole("button", { name: "Restoring..." })).toBeDisabled(); + currentRouteThreadKeyRef.current = threadB.routeThreadKey; + await screen.rerender(harness(threadB)); + + const completed = first === "a" ? threadA : threadB; + const pending = first === "a" ? threadB : threadA; + currentRouteThreadKeyRef.current = pending.routeThreadKey; + await screen.rerender(harness(pending)); + completions[first === "a" ? 0 : 1]!({ + _tag: "Success", + value: { refName: completed.localCheckoutBranchMismatch.threadBranch }, + }); + await vi.waitFor(() => expect(refreshGitStatus).toHaveBeenCalledOnce()); + expect(focusComposer).not.toHaveBeenCalled(); + await expect.element(page.getByRole("button", { name: "Restoring..." })).toBeDisabled(); + expect(switchRef).toHaveBeenCalledTimes(2); + + completions[first === "a" ? 1 : 0]!({ + _tag: "Success", + value: { refName: pending.localCheckoutBranchMismatch.threadBranch }, + }); + await expect.element(page.getByRole("button", { name: "Restore branch" })).toBeEnabled(); + expect(refreshGitStatus).toHaveBeenCalledTimes(2); + expect(focusComposer).not.toHaveBeenCalled(); + expect(focusFrames).toHaveLength(1); + if (scenario === "navigation") { + currentRouteThreadKeyRef.current = completed.routeThreadKey; + await screen.rerender(harness(completed)); + } + focusFrames[0]!(0); + expect(focusComposer).toHaveBeenCalledTimes(scenario === "navigation" ? 0 : 1); + }, +); diff --git a/apps/web/src/components/chat/useThreadLifecycle.tsx b/apps/web/src/components/chat/useThreadLifecycle.tsx index ef16d53fc5e74..4eeaab789e045 100644 --- a/apps/web/src/components/chat/useThreadLifecycle.tsx +++ b/apps/web/src/components/chat/useThreadLifecycle.tsx @@ -87,6 +87,8 @@ interface ThreadLifecycleInput { readonly activeThread: EnvironmentThreadShell | undefined; readonly activeThreadId: ThreadId | null; readonly activeThreadRef: ScopedThreadRef | null; + /** Null until the thread's detail may load; see resolveThreadDetailRef. */ + readonly threadDetailRef: ScopedThreadRef | null; readonly activeThreadKey: string | null; readonly activeThreadShell: EnvironmentThreadShell | null; readonly activeLatestRun: EnvironmentThreadShell["latestRun"]; @@ -111,7 +113,6 @@ interface ThreadLifecycleInput { readonly setThreadError: ReturnType; readonly beginLocalDispatch: LocalDispatchState["beginLocalDispatch"]; readonly resetLocalDispatch: LocalDispatchState["resetLocalDispatch"]; - readonly scheduleComposerFocus: () => void; readonly scrollToEnd: (animated?: boolean) => void; readonly persistThreadSettingsForNextTurn: (input: { threadId: ThreadId; @@ -137,6 +138,7 @@ export function useThreadLifecycle(input: ThreadLifecycleInput) { activeThread, activeThreadId, activeThreadRef, + threadDetailRef, activeThreadKey, activeThreadShell, activeLatestRun, @@ -158,7 +160,6 @@ export function useThreadLifecycle(input: ThreadLifecycleInput) { setThreadError, beginLocalDispatch, resetLocalDispatch, - scheduleComposerFocus, scrollToEnd, persistThreadSettingsForNextTurn, } = input; @@ -289,7 +290,11 @@ export function useThreadLifecycle(input: ThreadLifecycleInput) { setUnsnoozingThreadKey((current) => (current === threadKey ? null : current)); }; const handleUnsnoozeActiveThreadStable = useStableCallback(handleUnsnoozeActiveThread); - const [isRestoringThreadBranch, setIsRestoringThreadBranch] = useState(false); + const [restoringBranchThreadKeys, setRestoringBranchThreadKeys] = useState>( + () => new Set(), + ); + const isRestoringThreadBranch = + activeThreadKey !== null && restoringBranchThreadKeys.has(activeThreadKey); const [revealedBranchMismatchKey, setRevealedBranchMismatchKey] = useState(null); const [, setBranchMismatchDismissTick] = useState(0); const composerHasDraftContent = useComposerDraftStore((store) => @@ -324,7 +329,14 @@ export function useThreadLifecycle(input: ThreadLifecycleInput) { ) { return; } - setIsRestoringThreadBranch(true); + const threadKey = scopedThreadKey(scopeThreadRef(environmentId, activeThread.id)); + const finishRestore = () => + setRestoringBranchThreadKeys((current) => { + const next = new Set(current); + next.delete(threadKey); + return next; + }); + setRestoringBranchThreadKeys((current) => new Set(current).add(threadKey)); const checkoutResult = await switchGitRef({ environmentId, input: { @@ -333,7 +345,7 @@ export function useThreadLifecycle(input: ThreadLifecycleInput) { }, }); if (checkoutResult._tag === "Failure") { - setIsRestoringThreadBranch(false); + finishRestore(); if (!isAtomCommandInterrupted(checkoutResult)) { toastManager.add( stackedThreadToast({ @@ -347,7 +359,7 @@ export function useThreadLifecycle(input: ThreadLifecycleInput) { } if (!readEnvironmentScope(environmentId, AuthSourceControlWriteScope)) { - setIsRestoringThreadBranch(false); + finishRestore(); return; } const nextBranch = checkoutResult.value.refName ?? localCheckoutBranchMismatch.threadBranch; @@ -357,7 +369,7 @@ export function useThreadLifecycle(input: ThreadLifecycleInput) { input: { threadId: activeThread.id, branch: nextBranch, worktreePath: null }, }); if (updateResult._tag === "Failure") { - setIsRestoringThreadBranch(false); + finishRestore(); if (!isAtomCommandInterrupted(updateResult)) { toastManager.add( stackedThreadToast({ @@ -372,8 +384,12 @@ export function useThreadLifecycle(input: ThreadLifecycleInput) { } } gitStatusQuery.refresh(); - setIsRestoringThreadBranch(false); - scheduleComposerFocus(); + finishRestore(); + if (currentRouteThreadKeyRef.current === routeThreadKey) { + window.requestAnimationFrame(() => { + if (currentRouteThreadKeyRef.current === routeThreadKey) composerRef.current?.focusAtEnd(); + }); + } }; const handleSwitchCheckoutToThreadStable = useStableCallback(() => { void handleSwitchCheckoutToThread(); @@ -520,7 +536,7 @@ export function useThreadLifecycle(input: ThreadLifecycleInput) { const forceStop = useForceStopThread(environmentId); const anyRunStop = useAtomValue( - activeThreadRef ? environmentThreadDetails.runStopAtom(activeThreadRef) : NO_RUN_STOP_ATOM, + threadDetailRef ? environmentThreadDetails.runStopAtom(threadDetailRef) : NO_RUN_STOP_ATOM, ); // A background Stop matters only while that work is still pending. const runStop = anyRunStop?.background && activeBackgroundTasks.length === 0 ? null : anyRunStop; diff --git a/apps/web/src/components/files/AttachmentFilePreview.browser.test.tsx b/apps/web/src/components/files/AttachmentFilePreview.browser.test.tsx index 7492313f3c7e8..fc5a1aa7b91f9 100644 --- a/apps/web/src/components/files/AttachmentFilePreview.browser.test.tsx +++ b/apps/web/src/components/files/AttachmentFilePreview.browser.test.tsx @@ -1930,13 +1930,13 @@ it("reloads a workspace Word file after a preview remount", async () => { it("clears an Office metadata error when the same URL becomes previewable", async () => { const src = "data:application/octet-stream,invalid-office-file"; - let resolveResponse!: (response: Response) => void; - const response = new Promise((resolve) => { - resolveResponse = resolve; + let resolveBody!: (body: string) => void; + const body = new Promise((resolve) => { + resolveBody = resolve; }); using _fetch = stubGlobal( "fetch", - vi.fn(() => response), + vi.fn(() => body.then((text) => new Response(text))), ); await using screen = await render( , @@ -1949,7 +1949,7 @@ it("clears an Office metadata error when the same URL becomes previewable", asyn expect(screen.container.querySelector('[aria-label="Loading file"]')).not.toBeNull(), ); expect(screen.container.textContent).not.toContain("under 50 MB"); - resolveResponse(new Response("invalid-office-file")); + resolveBody("invalid-office-file"); await vi.waitFor(() => expect(screen.container.textContent).toContain("not a readable ZIP"), { timeout: 10_000, }); @@ -2345,6 +2345,274 @@ it("toggles a Mermaid attachment between diagram and source through the lazy pre expect(screen.container.querySelector('[role="alert"]')).toBeNull(); }); +it("keeps a workspace PDF open across URL renewals and reloads it when the file changes", async () => { + const { WorkspaceBrowserPreview } = await import("./FilePreviewPanel"); + using _native = vi.spyOn(navigator, "pdfViewerEnabled", "get").mockReturnValue(true); + const original = URL.createObjectURL(new Blob(["%PDF-1.4"], { type: "application/pdf" })); + const edited = URL.createObjectURL(new Blob(["%PDF-1.4 edited"], { type: "application/pdf" })); + const preview = (mutation: string) => ( + + ); + assetUrlState.mockReturnValue({ + _tag: "Success", + url: `${original}#initial`, + expiresAt: 0, + fileRevision: "one", + }); + try { + await using screen = await render(preview("first")); + const opened = screen.container.querySelector("iframe"); + expect(opened?.src.startsWith(original)).toBe(true); + + // An agent command renews the signed URL but leaves the file alone. + assetUrlState.mockReturnValue({ + _tag: "Success", + url: `${original}#renewed`, + expiresAt: 0, + fileRevision: "one", + }); + await screen.rerender(preview("second")); + expect(assetUrlState).toHaveBeenLastCalledWith(expect.anything(), expect.anything(), "second"); + expect(screen.container.querySelector("iframe")).toBe(opened); + + assetUrlState.mockReturnValue({ + _tag: "Success", + url: edited, + expiresAt: 0, + fileRevision: "two", + }); + await screen.rerender(preview("third")); + const reloaded = screen.container.querySelector("iframe"); + expect(reloaded).not.toBe(opened); + expect(reloaded?.src.startsWith(edited)).toBe(true); + + // Servers without revisions hold the preview back until a fresh grant arrives. + assetUrlState.mockReturnValue({ + _tag: "Success", + url: `${edited}#stale`, + expiresAt: 0, + waiting: true, + }); + await screen.rerender(preview("fourth")); + expect(screen.container.querySelector("iframe")).toBeNull(); + assetUrlState.mockReturnValue({ _tag: "Success", url: original, expiresAt: 0 }); + await screen.rerender(preview("fourth")); + expect(screen.container.querySelector("iframe")?.src.startsWith(original)).toBe(true); + } finally { + URL.revokeObjectURL(original); + URL.revokeObjectURL(edited); + } +}); +function planningPdf( + titles = ["Quarterly outlook", "Delivery milestones", "Budget and risks"], + firstHeight = 140, + pageHeight = 792, +) { + const objects = [ + "<< /Type /Catalog /Pages 2 0 R >>", + `<< /Type /Pages /Count ${titles.length} /Kids [${titles.map((_, index) => `${3 + index * 2} 0 R`).join(" ")}] >>`, + ]; + const font = 3 + titles.length * 2; + for (const [index, title] of titles.entries()) { + const text = `/GS1 gs BT /F1 24 Tf 30 ${index === 0 ? 70 : 700} Td ${index === 0 ? "(Quarter) Tj /F2 24 Tf (ly outlook) Tj" : `(${title}) Tj`} ET`; + objects.push( + `<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 ${index === 0 ? firstHeight : pageHeight}] /Rotate ${index === 2 ? 90 : 0} /Resources << /Font << /F1 ${font} 0 R /F2 ${font + 1} 0 R >> /ExtGState << /GS1 << /TR << /FunctionType 2 /Domain [0 1] /C0 [0] /C1 [1] /N 2 >> >> >> >> /Contents ${4 + index * 2} 0 R >>`, + `<< /Length ${text.length} >>\nstream\n${text}\nendstream`, + ); + } + objects.push("<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>"); + objects.push("<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica-Bold >>"); + let source = "%PDF-1.4\n"; + const offsets = [0]; + for (const [index, object] of objects.entries()) { + offsets.push(source.length); + source += `${index + 1} 0 obj\n${object}\nendobj\n`; + } + const xref = source.length; + source += `xref\n0 ${objects.length + 1}\n0000000000 65535 f \n${offsets + .slice(1) + .map((offset) => `${String(offset).padStart(10, "0")} 00000 n \n`) + .join("")}trailer\n<< /Size ${objects.length + 1} /Root 1 0 R >>\nstartxref\n${xref}\n%%EOF`; + return new Blob([source], { type: "application/pdf" }); +} + +async function workspacePdfPreview() { + assetUrlState.mockReset(); + const { WorkspaceBrowserPreview } = await import("./FilePreviewPanel"); + const native = vi.spyOn(navigator, "pdfViewerEnabled", "get").mockReturnValue(false); + const urls = (() => { + const original = URL.createObjectURL(planningPdf()); + const updated = URL.createObjectURL( + planningPdf(["Quarterly outlook", "Delivery milestones", "Updated budget"], 500), + ); + const shorter = URL.createObjectURL(planningPdf(["Quarterly outlook", "Updated milestones"])); + const shrunk = URL.createObjectURL( + planningPdf(["Quarterly outlook", "Small page", "Budget and risks"], 500, 200), + ); + const single = URL.createObjectURL(planningPdf(["Quarterly outlook"])); + return { + original, + updated, + shorter, + shrunk, + single, + [Symbol.dispose]: () => + [original, updated, shorter, shrunk, single].forEach(URL.revokeObjectURL), + }; + })(); + const preview = (src: string, mutation: string, path = "/repo/planning.pdf") => { + assetUrlState.mockReturnValue({ + _tag: "Success", + url: `${src}#preview`, + expiresAt: Date.now() + 3_600_000, + }); + return ( +
+ +
+ ); + }; + return { + preview, + urls, + [Symbol.dispose]() { + native.mockRestore(); + urls[Symbol.dispose](); + }, + }; +} + +function pdfScroll(screen: Screen) { + const region = () => screen.container.querySelector('[role="region"]')!; + const canvas = (page: number) => + region().querySelector(`canvas[aria-label="Page ${page}"]`)!; + const offset = (page: number) => + region().getBoundingClientRect().top - canvas(page).getBoundingClientRect().top; + const scrollTo = (options: ScrollToOptions) => + act(async () => { + const node = region(); + const scrolled = new Promise((resolve) => + node.addEventListener("scroll", () => resolve(), { once: true }), + ); + node.scrollTo(options); + await scrolled; + }); + return { region, canvas, offset, scrollTo }; +} + +it("keeps workspace PDF scroll and zoom after reloads", async () => { + using fixture = await workspacePdfPreview(); + const { preview, urls } = fixture; + await using screen = await render(preview(urls.original, "one")); + await expect.element(screen.getByText("1 / 3 pages", { exact: true })).toBeVisible(); + await screen.getByRole("button", { name: "Next page", exact: true }).click(); + await vi.waitFor(() => expect(screen.container.textContent).toContain("2 / 3 pages")); + await screen.getByRole("button", { name: "Next page", exact: true }).click(); + await vi.waitFor(() => expect(screen.container.textContent).toContain("3 / 3 pages")); + for (let index = 0; index < 4; index++) { + await screen.getByRole("button", { name: "Zoom in", exact: true }).click(); + } + const { region, offset, scrollTo } = pdfScroll(screen); + const zoom = screen.container.textContent!.match(/\d+%/)![0]; + const top = region().scrollTop; + await scrollTo({ top: top + 100, left: 80 }); + await vi.waitFor(() => { + expect(region().scrollTop).toBeCloseTo(top + 100, 0); + expect(region().scrollLeft).toBe(80); + }); + const savedOffset = offset(3); + await screen.rerender(preview(urls.updated, "two")); + await expect.element(screen.getByText("3 / 3 pages", { exact: true })).toBeVisible(); + await vi.waitFor(() => { + expect(screen.container.querySelector('[role="region"]')?.textContent).toContain( + "Updated budget", + ); + expect(screen.container.textContent).toContain(zoom); + expect(region().scrollLeft).toBe(80); + expect(offset(3)).toBeCloseTo(savedOffset, 0); + }); + await screen.getByRole("button", { name: "Previous page", exact: true }).click(); + await vi.waitFor(() => expect(screen.container.textContent).toContain("2 / 3 pages")); + await screen.getByRole("button", { name: "Fit width", exact: true }).click(); + await scrollTo({ top: region().scrollTop + 30 }); + const fitOffset = offset(2); + await screen.rerender(preview(urls.original, "fit-width")); + await expect.element(screen.getByText("2 / 3 pages", { exact: true })).toBeVisible(); + await vi.waitFor(() => { + expect(screen.container.textContent).toContain("Fit width"); + expect(region().scrollLeft).toBe(0); + expect(offset(2)).toBeCloseTo(fitOffset, 0); + }); + await screen.getByRole("button", { name: "Previous page", exact: true }).click(); + await vi.waitFor(() => expect(screen.container.textContent).toContain("1 / 3 pages")); + await screen.getByRole("button", { name: "Zoom in", exact: true }).click(); + const firstPageZoom = screen.container.textContent!.match(/\d+%/)![0]; + await screen.rerender(preview(urls.original, "first-page")); + await expect.element(screen.getByText("1 / 3 pages", { exact: true })).toBeVisible(); + await vi.waitFor(() => expect(screen.container.textContent).toContain(firstPageZoom)); + await screen.getByRole("button", { name: "Next page", exact: true }).click(); + await vi.waitFor(() => expect(screen.container.textContent).toContain("2 / 3 pages")); + await screen.rerender(preview(urls.original, "one", "/repo/another.pdf")); + await expect.element(screen.getByText("1 / 3 pages", { exact: true })).toBeVisible(); + await vi.waitFor(() => { + expect(screen.container.textContent).toContain("Fit width"); + expect(region().scrollTop).toBe(0); + expect(region().scrollLeft).toBe(0); + }); +}); + +it("clamps workspace PDF scroll when pages shrink or disappear", async () => { + using fixture = await workspacePdfPreview(); + const { preview, urls } = fixture; + await using screen = await render(preview(urls.original, "one")); + await expect.element(screen.getByText("1 / 3 pages", { exact: true })).toBeVisible(); + await screen.getByRole("button", { name: "Next page", exact: true }).click(); + await expect.element(screen.getByText("2 / 3 pages", { exact: true })).toBeVisible(); + for (let index = 0; index < 4; index++) { + await screen.getByRole("button", { name: "Zoom in", exact: true }).click(); + } + const { region, canvas, offset, scrollTo } = pdfScroll(screen); + const zoom = screen.container.textContent!.match(/\d+%/)![0]; + const secondPageTop = region().scrollTop; + await scrollTo({ top: secondPageTop + 600, left: 80 }); + await vi.waitFor(() => expect(region().scrollTop).toBeCloseTo(secondPageTop + 600, 0)); + await screen.rerender(preview(urls.shrunk, "smaller-page")); + await expect.element(screen.getByText("2 / 3 pages", { exact: true })).toBeVisible(); + await vi.waitFor(() => { + expect(screen.container.textContent).toContain(zoom); + expect(region().scrollLeft).toBe(80); + expect(offset(2)).toBeGreaterThan(0); + expect(offset(2)).toBeLessThan(canvas(2).getBoundingClientRect().height); + }); + await screen.rerender(preview(urls.shorter, "three")); + await expect.element(screen.getByText("2 / 2 pages", { exact: true })).toBeVisible(); + await vi.waitFor(() => { + expect(screen.container.querySelector('[role="region"]')?.textContent).toContain( + "Updated milestones", + ); + }); + await screen.rerender(preview(urls.single, "four")); + await expect.element(screen.getByText("1 / 1 pages", { exact: true })).toBeVisible(); + await screen.rerender(preview(urls.original, "five")); + await expect.element(screen.getByText("1 / 3 pages", { exact: true })).toBeVisible(); +}); + it("unwraps one text attachment without turning off the word wrap setting", async () => { const file = new Blob(["a long line"], { type: "text/plain" }); await using screen = await render( diff --git a/apps/web/src/components/files/AudioPreview.tsx b/apps/web/src/components/files/AudioPreview.tsx index 0a8f4bcfe16c9..abd05015a9414 100644 --- a/apps/web/src/components/files/AudioPreview.tsx +++ b/apps/web/src/components/files/AudioPreview.tsx @@ -15,6 +15,7 @@ type AudioPreviewProps = { readonly revision?: string | null; readonly onError?: () => void; readonly compact?: boolean; + readonly variant?: "chip" | "bubble"; readonly failed?: boolean; readonly onOpen?: (() => void) | undefined; readonly onRetry?: () => void; @@ -111,6 +112,7 @@ function AudioChip(props: AudioPreviewProps) { @@ -140,7 +142,7 @@ function AudioChip(props: AudioPreviewProps) { > - + {open ? (
); } - if (assetUrl._tag !== "Success") { + if ( + assetUrl._tag !== "Success" || + (pdf && assetUrl.fileRevision === undefined && assetUrl.waiting) + ) { return (
@@ -241,9 +251,10 @@ function WorkspaceBrowserPreview(props: { return ( ); } @@ -1387,7 +1398,7 @@ export default function FilePreviewPanel({ threadRef={threadRef} contents={file.data.contents} workspaceMutationId={workspaceMutationId} - readOnly={isHostFile || !canWriteFiles} + readOnly={file.data.truncated || isHostFile || !canWriteFiles} onPendingChange={onPendingChange} /> ) : tableDelimiter && renderTable ? ( diff --git a/apps/web/src/components/files/InlineAudioPreview.tsx b/apps/web/src/components/files/InlineAudioPreview.tsx index 6cc0e18f047df..94709bcebb5b2 100644 --- a/apps/web/src/components/files/InlineAudioPreview.tsx +++ b/apps/web/src/components/files/InlineAudioPreview.tsx @@ -12,6 +12,7 @@ export function InlineAudioPreview(props: { readonly src?: string; readonly copyMarkdown?: string; readonly onOpen?: () => void; + readonly variant?: "chip" | "bubble"; }) { const container = useRef(null); const [visible, setVisible] = useState(false); @@ -41,6 +42,7 @@ export function InlineAudioPreview(props: { void; +}) { + const [initialView] = useState( + () => props.initialView ?? { page: 0, offset: 0, zoom: null, left: 0 }, + ); const [pdf, setPdf] = useState(null); const [error, setError] = useState(null); const [revision, setRevision] = useState(0); const [ratios, setRatios] = useState([]); - const [zoom, setZoom] = useState(null); + const [zoom, setZoom] = useState(initialView.zoom); const [pageWidth, setPageWidth] = useState(612); const [viewport, setViewport] = useState({ width: 600, height: 800 }); const [scrollTop, setScrollTop] = useState(0); @@ -133,6 +148,7 @@ export default function PdfViewer(props: { src: string; title: string }) { const scroll = useRef(null); const find = useRef(null); const zoomAnchor = useRef<{ page: number; offset: number; width: number } | null>(null); + const initialPage = initialView.page; // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Own the PDF fetch, load timeout, and pdf.js document task across source changes, retries, and unmounts. useEffect(() => { const controller = new AbortController(); @@ -156,9 +172,20 @@ export default function PdfViewer(props: { src: string; title: string }) { if (document.numPages > MAX_PAGES) throw new Error("Preview limited to 2,000 pages. Save this PDF to read it in full."); const first = (await document.getPage(1)).getViewport({ scale: 1 }); + const sizes = await Promise.all( + Array.from({ length: Math.min(initialPage + 1, document.numPages) }, async (_, index) => { + const size = (await document.getPage(index + 1)).getViewport({ scale: 1 }); + return size.height / size.width; + }), + ); if (controller.signal.aborted) return; setPageWidth(first.width); - setRatios(Array.from({ length: document.numPages }, () => first.height / first.width)); + setRatios( + Array.from( + { length: document.numPages }, + (_, index) => sizes[index] ?? first.height / first.width, + ), + ); setPdf(document); }) .catch((cause: unknown) => { @@ -172,7 +199,7 @@ export default function PdfViewer(props: { src: string; title: string }) { clearTimeout(timeout); void task?.destroy(); }; - }, [props.src, revision]); + }, [props.src, revision, initialPage]); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Measure the PDF viewport before paint and observe resizes for fit-width sizing and visible-page selection. useLayoutEffect(() => { const node = scroll.current; @@ -195,6 +222,30 @@ export default function PdfViewer(props: { src: string; title: string }) { const jumpToPage = useEffectEvent((index: number) => { scroll.current?.scrollTo({ top: pdfPageScrollTop(offsets, index) }); }); + const saveView = (node: HTMLDivElement) => { + const page = currentPdfPage(offsets, ratios.length, node.scrollTop); + props.onViewChange?.({ + page, + offset: Math.max(0, node.scrollTop - (offsets[page]! - 16)), + zoom, + left: node.scrollLeft, + }); + }; + const restoreView = useEffectEvent(() => { + const node = scroll.current; + if (!pdf || !node) return; + const page = Math.min(initialPage, pdf.numPages - 1); + node.scrollTo({ + top: + offsets[page]! - 16 + Math.min(initialView.offset, Math.max(0, width * ratios[page]! - 1)), + left: initialView.left, + }); + saveView(node); + }); + // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Restore the saved PDF scroll position after the loaded document's page layout commits. + useLayoutEffect(() => { + if (pdf) restoreView(); + }, [pdf]); const showPage = (index: number) => { scroll.current?.scrollTo({ top: pdfPageScrollTop(offsets, index) }); }; @@ -210,10 +261,14 @@ export default function PdfViewer(props: { src: string; title: string }) { useLayoutEffect(() => { const anchor = zoomAnchor.current; zoomAnchor.current = null; - if (!anchor || anchor.width === width) return; - scroll.current?.scrollTo({ - top: offsets[anchor.page]! - 16 + (anchor.offset * width) / anchor.width, - }); + const node = scroll.current; + if (!anchor || !node) return; + if (anchor.width !== width) { + node.scrollTo({ + top: offsets[anchor.page]! - 16 + (anchor.offset * width) / anchor.width, + }); + } + saveView(node); }); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Scroll to the first asynchronous search result after its measured page offsets have committed. useLayoutEffect(() => { @@ -354,8 +409,12 @@ export default function PdfViewer(props: { src: string; title: string }) { role="region" aria-label={props.title} tabIndex={0} - className="relative min-h-0 min-w-0 w-full flex-1 overflow-auto bg-muted/40" - onScroll={(event) => setScrollTop(event.currentTarget.scrollTop)} + className="relative min-h-0 min-w-0 w-full flex-1 overflow-auto scrollbar-gutter-stable bg-muted/40" + onScroll={(event) => { + const top = event.currentTarget.scrollTop; + setScrollTop(top); + saveView(event.currentTarget); + }} onKeyDown={(event) => { if ((event.ctrlKey || event.metaKey) && event.key === "f") { event.preventDefault(); diff --git a/apps/web/src/components/pullRequest/PullRequestDetailPanel.permissions.browser.test.tsx b/apps/web/src/components/pullRequest/PullRequestDetailPanel.permissions.browser.test.tsx new file mode 100644 index 0000000000000..ea544da84ecfd --- /dev/null +++ b/apps/web/src/components/pullRequest/PullRequestDetailPanel.permissions.browser.test.tsx @@ -0,0 +1,164 @@ +import { + AuthOrchestrationOperateScope, + AuthSourceControlWriteScope, + EnvironmentId, + ProjectId, + SourceControlProviderKind, + type PullRequestDetailView, +} from "@t3tools/contracts"; +import { expect, it, vi } from "vite-plus/test"; +import { DraftId, useComposerDraftStore } from "~/composerDraftStore"; +import { act, render } from "../../../test/render"; + +const { permissions } = vi.hoisted(() => ({ + permissions: { checkout: true, operate: true }, +})); + +vi.mock(import("~/state/session"), async (original) => ({ + ...(await original()), + useEnvironmentScope: (_environmentId: unknown, scope: unknown) => + scope === AuthOrchestrationOperateScope ? permissions.operate : permissions.checkout, + readEnvironmentScope: (_environmentId: unknown, scope: unknown) => + scope === AuthOrchestrationOperateScope ? permissions.operate : permissions.checkout, +})); +vi.mock(import("~/state/sourceControlActions"), async (original) => ({ + ...(await original()), + usePreparePullRequestThreadAction: (() => ({ + isAllowed: permissions.checkout, + run: vi.fn(), + error: null, + })) as never, +})); +vi.mock(import("./usePullRequestDetail"), async (original) => ({ + ...(await original()), + usePullRequestDetail: (() => ({ + canWriteSourceControl: permissions.checkout, + detailQuery: { isPending: false, refresh: vi.fn() }, + activityQuery: { isPending: false, refresh: vi.fn(), error: null }, + activity: null, + sharedSummary: detail, + coreDetail: detail, + detail, + })) as never, +})); +vi.mock(import("~/state/usePullRequestStack"), async (original) => ({ + ...(await original()), + usePullRequestStack: (() => ({ + data: null, + isFresh: true, + isPending: false, + error: null, + refresh: vi.fn(), + })) as never, +})); +vi.mock(import("~/state/use-atom-command"), async (original) => ({ + ...(await original()), + useAtomCommand: (() => vi.fn()) as never, +})); + +import { PullRequestDetailPanel } from "./PullRequestDetailPanel"; + +const detail: PullRequestDetailView = { + provider: SourceControlProviderKind.make("github"), + projectId: ProjectId.make("project"), + projectTitle: "Project", + workspaceRoot: "/workspace", + repository: "owner/repo", + number: 1, + title: "Test pull request", + body: "Original description", + url: "https://github.com/owner/repo/pull/1", + author: { login: "author", name: null, avatarUrl: null }, + viewer: "author", + state: "open", + isDraft: false, + mergeability: "conflicting", + additions: 1, + deletions: 0, + changedFiles: 1, + headBranch: "feature", + baseBranch: "main", + createdAt: "2026-09-01T00:00:00Z", + updatedAt: "2026-09-01T00:00:00Z", + mergedAt: null, + closedAt: null, + reviewers: [], + labels: [], + checks: [{ name: "Unit tests", status: "success", description: null, url: null }], + comments: [], + commentCount: 0, + commentsTruncated: false, + reviewThreads: [], + commits: [], + mergeCapabilities: { merge: false, squash: false, rebase: false }, + capabilities: { + diff: true, + comment: false, + search: true, + actions: [], + mergeMethods: [], + review: { inlineComment: false, reply: false, resolve: false, verdicts: [] }, + reviewers: { request: false, listCandidates: false }, + edit: { changeRequest: true, comment: false }, + }, + viewerPermissions: { + actions: [], + comment: false, + resolve: false, + verdicts: [], + requestReviewers: false, + }, +}; + +const environmentId = EnvironmentId.make("environment"); +const draftId = DraftId.make("conflicts-draft"); +const panel = (attach = false) => ( + ({ + terminalFocus: false, + terminalOpen: false, + previewFocus: false, + previewOpen: false, + isWeb: true, + isDesktop: false, + })} + {...(attach ? { composerDraftTarget: draftId } : {})} + /> +); + +function setup() { + permissions.checkout = true; + permissions.operate = true; + useComposerDraftStore.setState({ draftsByThreadKey: {} }); +} + +it.each([AuthSourceControlWriteScope, AuthOrchestrationOperateScope])( + "disables conflict checkout when the connection loses %s", + async (scope) => { + setup(); + await using screen = await render(panel()); + const resolve = screen.getByRole("button", { name: "Resolve conflicts" }); + await expect.element(resolve).toBeEnabled(); + if (scope === AuthSourceControlWriteScope) permissions.checkout = false; + else permissions.operate = false; + await screen.rerender(panel()); + await expect.element(resolve).toBeDisabled(); + }, +); + +it("can still add conflict resolution to an existing composer without checkout scopes", async () => { + setup(); + permissions.checkout = false; + permissions.operate = false; + await using screen = await render(panel(true)); + const resolve = screen.getByRole("button", { name: "Resolve conflicts" }); + await expect.element(resolve).toBeEnabled(); + await act(() => resolve.click()); + const draft = useComposerDraftStore.getState().getComposerDraft(draftId); + expect(draft?.prompt).toContain("conflicts"); + expect(draft?.prompt).toContain(detail.url); +}); diff --git a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx index b86d271d3c8da..096cfe2dafb04 100644 --- a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx +++ b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx @@ -245,6 +245,7 @@ function pullRequestMarkdownContext( detail === null ? null : (sourceControlClients.parseChangeRequestUrl(detail.url)?.link.repositoryUrl ?? null), + lineBreaks: sourceControlClients.find(detail?.provider ?? "")?.hardLineBreaks ?? false, threadRef, }; } @@ -1233,7 +1234,7 @@ export function PullRequestDetailPanel({ const resolveConflictsControl = ( ); diff --git a/apps/web/src/components/pullRequest/PullRequestGuideTab.browser.test.tsx b/apps/web/src/components/pullRequest/PullRequestGuideTab.browser.test.tsx new file mode 100644 index 0000000000000..0b746725e6253 --- /dev/null +++ b/apps/web/src/components/pullRequest/PullRequestGuideTab.browser.test.tsx @@ -0,0 +1,75 @@ +import { EnvironmentId, ProjectId, type PullRequestRef } from "@t3tools/contracts"; +import { expect, it, vi } from "vite-plus/test"; +import { userEvent } from "vite-plus/test/browser"; + +vi.mock(import("./guide/useReviewGuideWriter"), () => ({ useReviewGuideWriter: vi.fn() })); + +import { render } from "../../../test/render"; +import { useReviewGuideWriter } from "./guide/useReviewGuideWriter"; +import { PullRequestGuideTab } from "./PullRequestGuideTab"; + +const reference: PullRequestRef = { + projectId: ProjectId.make("project"), + repository: "owner/repo", + number: 1, +}; + +const verboseFailure = [ + "Codex CLI command failed: OpenAI Codex v0.164.0", + ...Array.from({ length: 200 }, (_, line) => `prompt line ${line}: read the change in order`), + "ERROR: provider auth command `sh` timed out after 5000 ms", +].join("\n"); + +it("keeps Try again inside a short panel when a failed guide has long diagnostics", async () => { + const write = vi.fn(async () => {}); + vi.mocked(useReviewGuideWriter).mockReturnValue({ + query: { isPending: false, error: null } as never, + data: { + guide: null, + run: { + _tag: "Failed", + headSha: "head", + failedAt: "2026-10-27T16:37:47.661Z", + detail: verboseFailure, + }, + currentHeadSha: "head", + }, + running: false, + canWrite: true, + write, + }); + const panel = document.createElement("div"); + panel.style.cssText = "position: relative; width: 480px; height: 360px; overflow: hidden"; + const tab = document.createElement("div"); + tab.style.cssText = "position: absolute; inset: 0"; + panel.append(tab); + document.body.append(panel); + using _panel = { [Symbol.dispose]: () => panel.remove() }; + + await using screen = await render( + {}} + />, + { container: tab }, + ); + + const retry = screen.getByRole("button", { name: "Try again" }); + await expect.element(retry).toBeVisible(); + const panelBox = panel.getBoundingClientRect(); + const retryBox = retry.element().getBoundingClientRect(); + expect(retryBox.top).toBeGreaterThanOrEqual(panelBox.top); + expect(retryBox.bottom).toBeLessThanOrEqual(panelBox.bottom); + + await userEvent.click(retry); + expect(write).toHaveBeenCalledOnce(); +}); diff --git a/apps/web/src/components/pullRequest/PullRequestGuideTab.tsx b/apps/web/src/components/pullRequest/PullRequestGuideTab.tsx index fef668b3f3466..c7a032f6ffd55 100644 --- a/apps/web/src/components/pullRequest/PullRequestGuideTab.tsx +++ b/apps/web/src/components/pullRequest/PullRequestGuideTab.tsx @@ -104,18 +104,24 @@ export function PullRequestGuideTab({ ); } return ( -
+

- {running ? "Writing a review guide" : "No review guide yet"} + {running + ? "Writing a review guide" + : failure === null + ? "No review guide yet" + : "Could not write a review guide"}

- {failure ?? - query.error ?? - (running - ? "An agent is reading the change. A large one takes a few minutes. You can leave; you will be told when it is ready." - : "A guide is a tour of the change: stops through the code in reading order, with the calls only a maintainer can make.")} + {running + ? "An agent is reading the change. A large one takes a few minutes. You can leave; you will be told when it is ready." + : failure === null + ? (query.error ?? + "A guide is a tour of the change: stops through the code in reading order, with the calls only a maintainer can make.") + : FAILURE_HINT}

{writeButton(failure === null ? "Write guide" : "Try again", "sm")} + {failure === null || running ? null : }
); } @@ -125,9 +131,16 @@ export function PullRequestGuideTab({ {running ? : } - {running - ? "Rewriting this guide. You will be told when the new one is ready." - : (failure ?? "Written before the latest push.")} + {running ? ( + "Rewriting this guide. You will be told when the new one is ready." + ) : failure === null ? ( + "Written before the latest push." + ) : ( + <> + {`Could not rewrite this guide. ${FAILURE_HINT}`} + + + )} {!running && canWrite ? {writeButton("Rewrite", "xs")} : null} @@ -156,3 +169,18 @@ export function PullRequestGuideTab({ ); } + +const FAILURE_HINT = "Check the review guide model in Settings → Source control."; + +function FailureDetails({ detail }: { detail: string }) { + return ( +
+ + Technical details + +
+        {detail}
+      
+
+ ); +} diff --git a/apps/web/src/components/pullRequest/PullRequestMarkdown.browser.test.tsx b/apps/web/src/components/pullRequest/PullRequestMarkdown.browser.test.tsx new file mode 100644 index 0000000000000..23ccc904d264a --- /dev/null +++ b/apps/web/src/components/pullRequest/PullRequestMarkdown.browser.test.tsx @@ -0,0 +1,51 @@ +import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; +import { EnvironmentId } from "@t3tools/contracts"; +import { expect, it } from "vite-plus/test"; + +import { render } from "../../../test/render"; +import { PullRequestMarkdown, PullRequestMarkdownContext } from "./PullRequestMarkdown"; + +// Coder: only GitLab's definition is registered, so the hosts that break single newlines are +// represented by a context with `lineBreaks` set, and the context carries `repositoryUrl`. +async function renderBody( + host: string | { readonly hardLineBreaks: boolean }, + options: { lineBreaks?: boolean } = {}, +) { + const context = { + autolinks: null, + lineBreaks: + typeof host === "string" + ? (sourceControlClients.get(host).hardLineBreaks ?? false) + : host.hardLineBreaks, + repositoryUrl: null, + threadRef: null, + }; + return render( + + + , + ); +} + +it("a host with hard line breaks breaks a single newline", async () => { + await using screen = await renderBody({ hardLineBreaks: true }); + await expect.element(screen.getByText(/first line/)).toBeInTheDocument(); + expect(screen.container.querySelectorAll("p br")).toHaveLength(1); +}); + +it.each(["gitlab", "unknown"])("%s joins a single newline", async (host) => { + await using screen = await renderBody(host); + await expect.element(screen.getByText(/first line/)).toBeInTheDocument(); + expect(screen.container.querySelectorAll("p br")).toHaveLength(0); +}); + +it("lets text the host did not write opt out of the host's breaks", async () => { + await using screen = await renderBody({ hardLineBreaks: true }, { lineBreaks: false }); + await expect.element(screen.getByText(/first line/)).toBeInTheDocument(); + expect(screen.container.querySelectorAll("p br")).toHaveLength(0); +}); diff --git a/apps/web/src/components/pullRequest/PullRequestMarkdown.test.tsx b/apps/web/src/components/pullRequest/PullRequestMarkdown.test.tsx index 812517b1453bb..d46ebdfeb9b17 100644 --- a/apps/web/src/components/pullRequest/PullRequestMarkdown.test.tsx +++ b/apps/web/src/components/pullRequest/PullRequestMarkdown.test.tsx @@ -66,6 +66,7 @@ it("opens a recognized MR in the page panel and updates its URL selection", asyn ReadonlyArray) | null; + lineBreaks: boolean; // Coder: the repository GitLab's project-relative `/uploads/…` links resolve against. repositoryUrl: string | null; threadRef: ScopedThreadRef | null; @@ -83,6 +84,7 @@ export function PullRequestMarkdown({ cwd, environmentId, threadRef, + lineBreaks, className, }: { text: string; @@ -90,6 +92,7 @@ export function PullRequestMarkdown({ environmentId: EnvironmentId; /** Thread the body is shown beside, so its merge request links open in that thread's panel. */ threadRef?: ScopedThreadRef | null; + lineBreaks?: boolean; className?: string; }) { const context = useContext(PullRequestMarkdownContext); @@ -120,6 +123,7 @@ export function PullRequestMarkdown({ threadRef={resolvedThreadRef} pullRequestPanelRef={resolvedThreadRef ?? PULL_REQUESTS_PANEL_REF} extraRemarkPlugins={extraRemarkPlugins} + lineBreaks={lineBreaks ?? context?.lineBreaks ?? false} sourceControlMedia /> ); diff --git a/apps/web/src/components/pullRequest/guide/GuideProse.tsx b/apps/web/src/components/pullRequest/guide/GuideProse.tsx index e117191fb97a6..adcdb7cd0a868 100644 --- a/apps/web/src/components/pullRequest/guide/GuideProse.tsx +++ b/apps/web/src/components/pullRequest/guide/GuideProse.tsx @@ -26,6 +26,7 @@ export function GuideProse({ text={text} cwd={context.cwd} environmentId={context.environmentId} + lineBreaks={false} className={cn("space-y-0 [&_.chat-markdown]:text-inherit", className)} /> ); diff --git a/apps/web/src/components/pullRequest/guide/GuideStop.tsx b/apps/web/src/components/pullRequest/guide/GuideStop.tsx index 81e5aa407eb3a..3599defc8f680 100644 --- a/apps/web/src/components/pullRequest/guide/GuideStop.tsx +++ b/apps/web/src/components/pullRequest/guide/GuideStop.tsx @@ -270,8 +270,8 @@ export function GuideStop({
{nextTitle !== null ? ( - ) : onFinish !== null ? ( diff --git a/apps/web/src/components/pullRequest/guide/useGuideDiffFiles.browser.test.tsx b/apps/web/src/components/pullRequest/guide/useGuideDiffFiles.browser.test.tsx index cdb7d4099e5c7..8a92f2c2bf0d0 100644 --- a/apps/web/src/components/pullRequest/guide/useGuideDiffFiles.browser.test.tsx +++ b/apps/web/src/components/pullRequest/guide/useGuideDiffFiles.browser.test.tsx @@ -66,5 +66,5 @@ it.each([ expect(result()).toEqual({ loading: true, files: [] }); await act(() => registry.set(first, AsyncResult.success(page("wanted.ts", "FRESH", null)))); - expect(result()).toEqual({ loading: false, files: [["wanted.ts", "FRESH"]] }); + expect(result()).toEqual({ loading: false, files: [["wanted.ts", "FRESH\n"]] }); }); diff --git a/apps/web/src/components/settings/EnvironmentRow.tsx b/apps/web/src/components/settings/EnvironmentRow.tsx index 18151d659cda4..9f0478ad46c15 100644 --- a/apps/web/src/components/settings/EnvironmentRow.tsx +++ b/apps/web/src/components/settings/EnvironmentRow.tsx @@ -11,6 +11,17 @@ export function environmentTransportLabel(_environment: EnvironmentPresentation) return "Coder workspace"; } +// Coder: the label never depends on the prepared connection target. +export function useEnvironmentTransportLabel(environment: EnvironmentPresentation): string { + return environmentTransportLabel(environment); +} + +export function EnvironmentTransportLabel(props: { + readonly environment: EnvironmentPresentation; +}) { + return useEnvironmentTransportLabel(props.environment); +} + export function EnvironmentRow({ kind, label, diff --git a/apps/web/src/components/settings/ImportAgentSessionsDialog.tsx b/apps/web/src/components/settings/ImportAgentSessionsDialog.tsx index 0bcc9ab9b86ee..6cc32a1dc5ab1 100644 --- a/apps/web/src/components/settings/ImportAgentSessionsDialog.tsx +++ b/apps/web/src/components/settings/ImportAgentSessionsDialog.tsx @@ -1,6 +1,6 @@ // Coder: upstream's welcome-wizard import step, offered from Settings → Providers. The wizard -// itself is not carried. The helper scans only the workspace's own Claude Code and Codex session -// stores and imports their history over its stdio RPC. +// itself is not carried. The helper scans only the workspace's own Claude Code, Codex, and Pi +// session stores and imports their history over its stdio RPC. import type { AgentSessionProjectCandidate, EnvironmentId, @@ -9,7 +9,12 @@ import type { } from "@t3tools/contracts"; import { scopeProjectRef } from "@t3tools/client-runtime/environment"; import { isAtomCommandInterrupted } from "@t3tools/client-runtime/state/runtime"; -import { AuthOrchestrationOperateScope, CommandId, ProviderDriverKind } from "@t3tools/contracts"; +import { + AgentSessionSource, + AuthOrchestrationOperateScope, + CommandId, + ProviderDriverKind, +} from "@t3tools/contracts"; import { ChevronRightIcon } from "lucide-react"; import { useEffect, useMemo, useRef, useState } from "react"; @@ -43,7 +48,7 @@ import { formatRelativeTime } from "../../timestampFormat"; const SCAN_LIMIT_MESSAGE = "Scan limit reached. Some projects or conversations may be missing."; -/** Imports Claude Code and Codex projects and their history from one workspace. */ +/** Imports Claude Code, Codex, and Pi projects and their history from one workspace. */ export function ImportAgentSessionsDialog({ environmentId, open, @@ -641,6 +646,12 @@ function ImportCandidateRow({ ); } +const IMPORT_SOURCE_NAMES: Record = { + claudeAgent: "Claude Code", + codex: "Codex", + pi: "Pi", +}; + /** * Trailing columns shared by every import row: source icons, thread count, * last activity. Each column has a fixed width and each icon has its own slot @@ -651,7 +662,7 @@ function ImportRowMeta({ threadCount, lastActiveAt, }: { - readonly sources: ReadonlyArray<"claudeAgent" | "codex"> | null; + readonly sources: ReadonlyArray | null; readonly threadCount: number; readonly lastActiveAt: string | null; }) { @@ -659,29 +670,20 @@ function ImportRowMeta({ // "just now" does not fit the fixed column, so collapse it. const age = relative === null ? "" : relative.suffix === null ? "now" : relative.value; return ( - - - {sources?.includes("claudeAgent") ? ( - - - - ) : null} - - - {sources?.includes("codex") ? ( - - - - ) : null} - + + {AgentSessionSource.literals.map((source) => ( + + {sources?.includes(source) ? ( + + + + ) : null} + + ))} {threadCount} {age} diff --git a/apps/web/src/components/settings/KeybindingsSettings.browser.test.tsx b/apps/web/src/components/settings/KeybindingsSettings.browser.test.tsx new file mode 100644 index 0000000000000..c65d2cdfbd906 --- /dev/null +++ b/apps/web/src/components/settings/KeybindingsSettings.browser.test.tsx @@ -0,0 +1,54 @@ +import { RegistryProvider } from "@effect/atom-react"; +import { userEvent } from "vite-plus/test/browser"; +import { expect, it, vi } from "vite-plus/test"; + +import { render } from "../../../test/render"; + +vi.mock(import("./SettingsScopeContext"), async (original) => ({ + ...(await original()), + useSettingsScope: (() => { + const environment = { + environmentId: "env", + label: "Build workstation", + serverConfig: { keybindings: [], keybindingsConfigPath: null, availableEditors: [] }, + }; + return { + scope: { kind: "environment" }, + environment, + environments: [environment], + connectedEnvironments: [environment], + targets: [], + target: null, + }; + }) as never, +})); +vi.mock(import("../../state/session"), async (original) => ({ + ...(await original()), + useEnvironmentScope: () => true, + useEnvironmentsWithScope: ((environments: ReadonlyArray<{ environmentId: string }>) => + new Set(environments.map((environment) => environment.environmentId))) as never, + readEnvironmentScope: () => true, +})); + +import { KeybindingsSettingsPanel } from "./KeybindingsSettings"; + +it("re-enables Save when a When editor closes on an expression it could not parse", async () => { + await using screen = await render( + + + , + ); + const whenTrigger = screen.getByRole("button", { name: "Edit when clause for Terminal: Toggle" }); + const expression = screen.getByRole("textbox", { name: "When expression" }); + + await whenTrigger.click(); + await userEvent.type(expression, "terminalFocus"); + await userEvent.type(expression, " &&"); + await expect.element(screen.getByRole("button", { name: "Save" })).toBeDisabled(); + + await userEvent.keyboard("{Escape}"); + await whenTrigger.click(); + + await expect.element(expression).toHaveValue("terminalFocus"); + await expect.element(screen.getByRole("button", { name: "Save" })).toBeEnabled(); +}); diff --git a/apps/web/src/components/settings/KeybindingsSettings.logic.test.ts b/apps/web/src/components/settings/KeybindingsSettings.logic.test.ts index 7db732781773c..7f57caaf04f68 100644 --- a/apps/web/src/components/settings/KeybindingsSettings.logic.test.ts +++ b/apps/web/src/components/settings/KeybindingsSettings.logic.test.ts @@ -266,4 +266,38 @@ describe("KeybindingsSettings.logic", () => { }), ).toEqual(["Chat: New Local"]); }); + + it.each([ + { platform: "Linux x86_64", ctrl: ["Chat: New"], meta: [] }, + { platform: "Win32", ctrl: ["Chat: New"], meta: [] }, + { platform: "MacIntel", ctrl: [], meta: ["Chat: New"] }, + ])("resolves mod to the $platform modifier when reporting conflicts", (expected) => { + const rows = buildKeybindingRows( + compileResolvedKeybindingsConfig([ + { command: "chat.new", key: "mod+k" }, + { command: "chat.newLocal", key: "ctrl+k" }, + { command: "thread.copyReference", key: "meta+k" }, + ]), + expected.platform, + ); + const conflictsFor = (key: string) => rows.find((row) => row.key === key)?.conflicts; + + expect(conflictsFor("ctrl+k")).toEqual(expected.ctrl); + expect(conflictsFor("meta+k")).toEqual(expected.meta); + expect( + keybindingConflictLabels(rows, { rowId: "new", key: "ctrl+k", when: "" }, expected.platform), + ).toEqual(["Chat: New Local", ...expected.ctrl].toSorted()); + }); + + it("keeps mod and ctrl shortcuts apart when their when contexts differ", () => { + const rows = buildKeybindingRows( + compileResolvedKeybindingsConfig([ + { command: "chat.new", key: "mod+k", when: "!terminalFocus" }, + { command: "chat.newLocal", key: "ctrl+k", when: "terminalFocus" }, + ]), + "Linux x86_64", + ); + + expect(rows.map((row) => row.conflicts)).toEqual([[], []]); + }); }); diff --git a/apps/web/src/components/settings/KeybindingsSettings.logic.ts b/apps/web/src/components/settings/KeybindingsSettings.logic.ts index 1b7dea91a7b10..eb62ffed9c2a8 100644 --- a/apps/web/src/components/settings/KeybindingsSettings.logic.ts +++ b/apps/web/src/components/settings/KeybindingsSettings.logic.ts @@ -8,11 +8,12 @@ import { } from "@t3tools/contracts"; import { DEFAULT_RESOLVED_KEYBINDINGS, + parseKeybindingShortcut, parseKeybindingWhenExpression, US_SHIFTED_TO_UNSHIFTED_KEYS, } from "@t3tools/shared/keybindings"; -import { shortcutKeyFromEvent } from "../../keybindings"; +import { shortcutConflictKey, shortcutKeyFromEvent } from "../../keybindings"; import { isMacPlatform } from "../../lib/utils"; // Coder: no Usage page, so no Usage command block in the ordering. @@ -165,13 +166,18 @@ function conflictsWithWhen(leftWhen: string, rightWhen: string): boolean { export function keybindingConflictLabels( rows: ReadonlyArray, input: { readonly rowId: string; readonly key: string; readonly when: string }, + platform = navigator.platform, ): ReadonlyArray { if (input.key.trim().length === 0) return []; + const shortcut = parseKeybindingShortcut(input.key); + if (!shortcut) return []; + // Compare resolved chords so mod+k and ctrl+k collide where mod is Ctrl. + const conflictKey = shortcutConflictKey(shortcut, platform); const conflicts: Array = []; for (const candidate of rows) { if ( candidate.id !== input.rowId && - candidate.key === input.key && + shortcutConflictKey(candidate.binding.shortcut, platform) === conflictKey && conflictsWithWhen(candidate.when, input.when) ) { conflicts.push(commandLabel(candidate.command)); @@ -227,6 +233,7 @@ export function groupKeybindingRows( export function buildKeybindingRows( keybindings: ResolvedKeybindingsConfig, + platform = navigator.platform, ): ReadonlyArray { const rowsById = new Map(); for (const binding of keybindings) { @@ -249,11 +256,15 @@ export function buildKeybindingRows( const rows = [...rowsById.values()]; const rowsWithConflicts = rows.map((row) => { - const conflicts = keybindingConflictLabels(rows, { - rowId: row.id, - key: row.key, - when: row.when, - }); + const conflicts = keybindingConflictLabels( + rows, + { + rowId: row.id, + key: row.key, + when: row.when, + }, + platform, + ); return conflicts.length > 0 ? Object.assign({}, row, { conflicts: [...new Set(conflicts)].toSorted() }) : row; diff --git a/apps/web/src/components/settings/KeybindingsSettings.tsx b/apps/web/src/components/settings/KeybindingsSettings.tsx index b195bad3e070c..5b2ec1362e7c8 100644 --- a/apps/web/src/components/settings/KeybindingsSettings.tsx +++ b/apps/web/src/components/settings/KeybindingsSettings.tsx @@ -847,8 +847,13 @@ function WhenClauseControl({ onChange: (value: KeybindingWhenNode | undefined) => void; onValidityChange: (valid: boolean) => void; }) { + // Closing unmounts the builder, which drops an unparsed draft and keeps the last valid value. + const handleOpenChange = (open: boolean) => { + if (!open) onValidityChange(true); + }; + return ( - + {shortcutLabel} ) : null} - + + } + /> + ) : null}
); @@ -123,6 +141,7 @@ function ProviderCustomColorPicker(props: { value={normalized ?? FALLBACK_ACCENT_COLOR} onPreview={props.onPreview} onCommit={props.onCommit} + showAddButton={!normalized} /> {normalized ? (
diff --git a/apps/web/src/components/settings/ProviderModelsSection.tsx b/apps/web/src/components/settings/ProviderModelsSection.tsx index 25171dc5bd1f4..1bb6c9dde0d4a 100644 --- a/apps/web/src/components/settings/ProviderModelsSection.tsx +++ b/apps/web/src/components/settings/ProviderModelsSection.tsx @@ -5,6 +5,7 @@ import { type ProviderInstanceId, type ServerProviderModel, } from "@t3tools/contracts"; +import { REASONING_SELECT_DESCRIPTOR_IDS } from "@t3tools/client-runtime/model-traits"; import { type CustomModelDefinition, normalizeCustomModelSlug } from "@t3tools/shared/model"; import { useStableCallback } from "../../hooks/useStableCallback"; @@ -27,7 +28,21 @@ const CUSTOM_MODEL_PLACEHOLDER_BY_KIND: Partial + REASONING_SELECT_DESCRIPTOR_IDS.has(descriptor.id), + ); +} + +// Reasoning is the norm, so only its absence is labeled, and only when other models have it. +// Custom and null capabilities are fallbacks or user-written, not discovered, so they prove nothing. +// Alias slugs ("default", Cursor's "auto") defer to the provider's own model choice, so reasoning is unknown. +function describeModelCapabilities( + model: ServerProviderModel, + flagMissingReasoning: boolean, +): string[] { const descriptors = model.capabilities?.optionDescriptors ?? []; const labels: string[] = []; const hasFastMode = descriptors.some( @@ -38,18 +53,14 @@ function describeModelCapabilities(model: ServerProviderModel): string[] { descriptor.options.some((option) => option.id === "fast" || option.label === "Fast")), ); if (hasFastMode) labels.push("Fast mode"); - if (descriptors.some((descriptor) => descriptor.id === "thinking")) labels.push("Thinking"); if ( - descriptors.some( - (descriptor) => - descriptor.type === "select" && - (descriptor.id === "reasoningEffort" || - descriptor.id === "effort" || - descriptor.id === "reasoning" || - descriptor.id === "variant"), - ) + flagMissingReasoning && + !model.isCustom && + model.capabilities && + !PROVIDER_ALIAS_SLUGS.has(model.slug) && + !hasReasoningControl(model) ) { - labels.push("Reasoning"); + labels.push("No reasoning"); } return labels; } @@ -144,6 +155,7 @@ export function ProviderModelsSection({ const hiddenCount = displayModels.filter( (model) => !model.isCustom && hiddenModelSet.has(model.slug), ).length; + const flagMissingReasoning = models.some(hasReasoningControl); const showFilter = models.length > FILTER_THRESHOLD; const normalizedFilter = filter.trim().toLowerCase(); const isFiltering = showFilter && normalizedFilter.length > 0; @@ -278,6 +290,7 @@ export function ProviderModelsSection({ canMoveUp={!isFiltering && previousModel !== undefined && groupOf(previousModel) === group} canMoveDown={!isFiltering && nextModel !== undefined && groupOf(nextModel) === group} isFiltering={isFiltering} + flagMissingReasoning={flagMissingReasoning} canWritePreferences={canWritePreferences} canManageCustomModels={canManageCustomModels} {...rowHandlers} @@ -437,6 +450,7 @@ function ProviderModelRow({ canMoveUp, canMoveDown, isFiltering, + flagMissingReasoning, canWritePreferences, canManageCustomModels, onToggleFavorite, @@ -451,6 +465,7 @@ function ProviderModelRow({ readonly canMoveUp: boolean; readonly canMoveDown: boolean; readonly isFiltering: boolean; + readonly flagMissingReasoning: boolean; readonly canWritePreferences: boolean; readonly canManageCustomModels: boolean; readonly onToggleFavorite: (slug: string) => void; @@ -459,7 +474,7 @@ function ProviderModelRow({ readonly onToggleEdit: (slug: string) => void; readonly onRemove: (slug: string) => void; }) { - const capLabels = describeModelCapabilities(model); + const capLabels = describeModelCapabilities(model, flagMissingReasoning); const nameClassName = cn("text-xs", isHidden ? "text-muted-foreground" : "text-foreground/90"); const pickerTooltip = model.isCustom ? "Custom models are always shown in the picker" diff --git a/apps/web/src/components/settings/ProviderSettingsForm.test.ts b/apps/web/src/components/settings/ProviderSettingsForm.test.ts index b39aecb0664e6..fd709d0f3ecb2 100644 --- a/apps/web/src/components/settings/ProviderSettingsForm.test.ts +++ b/apps/web/src/components/settings/ProviderSettingsForm.test.ts @@ -9,7 +9,8 @@ import { seedRowsDraft, } from "./ProviderSettingsForm"; -// Coder: the OpenCode case is omitted because its definition is not carried. +// Coder: the OpenCode case is omitted because its definition is not carried, and the secret +// reference case (#27746) because secret providers are not carried. describe("ProviderSettingsForm helpers", () => { it("keeps every masked field as a stored secret", () => { const settingsSchema = makeProviderSettingsSchema({ diff --git a/apps/web/src/components/settings/ProviderSettingsForm.tsx b/apps/web/src/components/settings/ProviderSettingsForm.tsx index a36f93fec7cdb..768f9ba1ef483 100644 --- a/apps/web/src/components/settings/ProviderSettingsForm.tsx +++ b/apps/web/src/components/settings/ProviderSettingsForm.tsx @@ -311,6 +311,8 @@ interface ProviderSettingsFormProps { readonly idPrefix: string; readonly variant: "card" | "dialog" | "settings"; readonly onChange: (nextConfig: Record | undefined) => void; + // Coder: secret providers are not carried, so there is no `environmentId` for a secret-source + // picker and a `SettingsSecretValue` field keeps the plain `SecretInput` (#27746). } function ProviderSettingsSelect({ diff --git a/apps/web/src/components/settings/ScheduledTasksSettings.browser.test.tsx b/apps/web/src/components/settings/ScheduledTasksSettings.browser.test.tsx index 124a27c7b11f4..7e42bba4bc6c8 100644 --- a/apps/web/src/components/settings/ScheduledTasksSettings.browser.test.tsx +++ b/apps/web/src/components/settings/ScheduledTasksSettings.browser.test.tsx @@ -6,6 +6,7 @@ import { ProjectId, ProviderInstanceId, ScheduledTaskId, + ThreadId, type ScheduledTask, } from "@t3tools/contracts"; import { describe, expect, it, vi } from "vite-plus/test"; @@ -24,7 +25,7 @@ const task: ScheduledTask = { enabled: true, schedule: { type: "interval", everyMs: 60_000 }, projectId, - threadId: null, + threadId: ThreadId.make("older-thread"), workspaceStrategy: { type: "root" }, modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" }, runtimeMode: "full-access", @@ -40,6 +41,12 @@ const task: ScheduledTask = { runCount: 0, }; +// Stands in for the thread family read the editor makes when the shell lacks the bound thread. +const threadFamilyRead = { + snapshotSequence: 1, + threads: [{ id: task.threadId, title: "Fix login bug" }], +}; + const state = vi.hoisted(() => ({ connected: true, tasksLoaded: true, secondary: false })); const secondaryId = EnvironmentId.make("secondary"); const permissions = new Map>(); @@ -102,11 +109,21 @@ vi.mock(import("../../state/server"), async (original) => { }, }; }); +vi.mock(import("../../state/threads"), async (original) => { + const actual = await original(); + return { + ...actual, + threadFamilies: { ...actual.threadFamilies, familyAtom: (() => threadFamilyRead) as never }, + }; +}); // Mirrors the real query: a null request (disconnected environment) has no data. vi.mock(import("../../state/query"), async (original) => ({ ...(await original()), useEnvironmentQuery: ((request: unknown) => ({ - data: request === null ? null : { tasks: state.tasksLoaded ? [task] : [] }, + data: + request === null || request === threadFamilyRead + ? request + : { tasks: state.tasksLoaded ? [task] : [] }, error: null, isPending: false, refresh: vi.fn(), @@ -166,6 +183,7 @@ describe("ScheduledTasksSettings", () => { it("keeps a linked task's unsaved edits across a disconnect and reconnect", async () => { setup(); await using screen = await render(); + await expect.element(page.getByRole("dialog").getByText("Fix login bug")).toBeVisible(); const name = page.getByRole("dialog").getByRole("textbox", { name: "Name" }); await expect.element(name).toHaveValue("Review issues"); await userEvent.fill(name, "Review stale issues"); diff --git a/apps/web/src/components/settings/ScheduledTasksSettings.tsx b/apps/web/src/components/settings/ScheduledTasksSettings.tsx index c54f4158f1631..77c2777315971 100644 --- a/apps/web/src/components/settings/ScheduledTasksSettings.tsx +++ b/apps/web/src/components/settings/ScheduledTasksSettings.tsx @@ -39,10 +39,12 @@ import { providerInstanceEntriesFor } from "../../providerInstances"; import { useEnvironment, type EnvironmentPresentation } from "../../state/environments"; import { useNavigate } from "@tanstack/react-router"; import { useNowMinute } from "../../hooks/useNowMinute"; -import { useProjects } from "../../state/entities"; +import { useProjects, useThreadShell } from "../../state/entities"; +import { scopeThreadRef } from "@t3tools/client-runtime/environment"; import { environmentAtomList, environmentIdsKey } from "../../state/environmentAtomList"; import { formatEnvironmentQueryError, useEnvironmentQuery } from "../../state/query"; import { EMPTY_SERVER_PROVIDERS, serverEnvironment } from "../../state/server"; +import { threadFamilies } from "../../state/threads"; import { useAtomCommand } from "../../state/use-atom-command"; import { WorktreeBaseBranchPicker } from "../WorktreeBaseBranchPicker"; import { EnvironmentMachineIcon } from "../EnvironmentMachineIcon"; @@ -50,7 +52,7 @@ import { ConnectionStatusDot, connectionPhaseDotClassName } from "../ConnectionS import { AuthOrchestrationOperateScope } from "@t3tools/contracts"; import { readEnvironmentScope } from "~/state/session"; import { useSettingsScope } from "./SettingsScopeContext"; -import { EnvironmentRow, environmentTransportLabel } from "./EnvironmentRow"; +import { EnvironmentRow, useEnvironmentTransportLabel } from "./EnvironmentRow"; import { SettingsGroup } from "./SettingsGroup"; import { settingsScopeEnvironmentLabel, settingsScopeEnvironmentLabels } from "./settingsScopeAxis"; import { @@ -412,19 +414,13 @@ function ScheduledTaskEnvironmentList({ {populated.map(({ environment, tasks }) => { const { primary, secondary } = settingsScopeEnvironmentLabels(environment, environments); - const transport = environmentTransportLabel(environment); return (
- {secondary && secondary !== transport ? ( - {secondary} - ) : null} - {transport} - + } > + {secondary && secondary !== transport ? ( + {secondary} + ) : null} + {transport} + + ); +} + function ScheduledTaskEnvironmentSection({ environment, taskId, @@ -701,6 +715,38 @@ function ScheduledTaskEditorDialog({ ); } +function ScheduledTaskDestination({ + environmentId, + threadId, +}: { + readonly environmentId: EnvironmentId; + readonly threadId: ThreadId | null; +}) { + const ref = threadId === null ? null : scopeThreadRef(environmentId, threadId); + const shell = useThreadShell(ref); + // The shell can omit older settled threads; their family read still carries the title. + const family = useEnvironmentQuery( + ref !== null && shell === null ? threadFamilies.familyAtom(environmentId, ref.threadId) : null, + ); + const thread = shell ?? family.data?.threads.find((row) => row.id === threadId); + return ( + + {ref === null ? ( +

+ Each run starts a new thread with the workspace and model below. +

+ ) : ( +
+

{thread?.title.trim() || `Thread ${ref.threadId}`}

+

+ Each run continues this conversation, in its workspace and with its current model. +

+
+ )} +
+ ); +} + function ScheduledTaskEditorForm({ initialEnvironmentId, task, @@ -978,6 +1024,11 @@ function ScheduledTaskEditorForm({ /> + +