From 50b885e03617e84d85d64e8a3bf599daee1345cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Radek=20Ma=C5=88=C3=A1k?= Date: Mon, 16 Nov 2026 11:43:23 +0100 Subject: [PATCH 001/253] fix(web): switching a draft's machine uses that machine's default model (#37593) --- .../components/AddProjectToMachineDialog.tsx | 2 + .../src/components/chat/DraftHeroHeadline.tsx | 62 +++---------------- ...seDraftEnvironmentRouting.browser.test.tsx | 40 +++++++++++- .../chat/useDraftEnvironmentRouting.ts | 2 + apps/web/src/lib/seedDraftModelSelection.ts | 37 +++++++++++ apps/web/src/state/environments.ts | 6 ++ 6 files changed, 93 insertions(+), 56 deletions(-) create mode 100644 apps/web/src/lib/seedDraftModelSelection.ts diff --git a/apps/web/src/components/AddProjectToMachineDialog.tsx b/apps/web/src/components/AddProjectToMachineDialog.tsx index 3bb70c8b609eb..a6f2a2fa541a5 100644 --- a/apps/web/src/components/AddProjectToMachineDialog.tsx +++ b/apps/web/src/components/AddProjectToMachineDialog.tsx @@ -32,6 +32,7 @@ import { isDefaultCopiedFile, } from "../lib/addProjectToMachine"; import { formatBytes } from "../lib/formatBytes"; +import { seedDraftModelSelection } from "../lib/seedDraftModelSelection"; import { newProjectId } from "../lib/utils"; import { readProject, subscribeProject, useProject } from "../state/entities"; import { useEnvironments } from "../state/environments"; @@ -201,6 +202,7 @@ function AddProjectToMachineDialog({ request }: { request: AddProjectToMachineRe environmentSelection: "manual", loadBalancedEnvironmentId: null, }); + seedDraftModelSelection(draftId, targetRef); return; } void handleNewThread(targetRef); diff --git a/apps/web/src/components/chat/DraftHeroHeadline.tsx b/apps/web/src/components/chat/DraftHeroHeadline.tsx index 26c741cd8b8df..772f83cb149c0 100644 --- a/apps/web/src/components/chat/DraftHeroHeadline.tsx +++ b/apps/web/src/components/chat/DraftHeroHeadline.tsx @@ -16,7 +16,7 @@ import { useProjectPin } from "~/hooks/useProjectGroupUpdate"; import { useScratchProject } from "~/hooks/useScratchProject"; import { isTriagePrompt } from "@t3tools/shared/triagePrompt"; import { useClientSettings } from "~/hooks/useSettings"; -import { hasExplicitComposerModelSelection } from "~/lib/chatThreadActions"; +import { seedDraftModelSelection } from "~/lib/seedDraftModelSelection"; import { deriveLogicalProjectKeyFromSettings, pickableProjectGroups, @@ -27,11 +27,7 @@ import { projectGroupsSpanEnvironments, } from "~/sidebarProjectGrouping"; import { useProjects } from "~/state/entities"; -import { - useEnvironments, - usePrimaryEnvironmentId, - useEnvironmentProviders, -} from "~/state/environments"; +import { useEnvironments, usePrimaryEnvironmentId } from "~/state/environments"; import { ProjectEnvironmentBadge } from "../ProjectEnvironmentBadge"; import { ProjectFavicon } from "../ProjectFavicon"; import { isProjectPinShortcut, ProjectPinButton } from "../ProjectPinButton"; @@ -51,9 +47,6 @@ import { InlineButton } from "../ui/button"; import { DraftProjectName } from "./DraftProjectName"; import { DraftHeroProjectHeading } from "./DraftHeroProjectHeading"; import { TriageDraftHero } from "./TriageDraftHero"; -import { resolveProjectSettings } from "@t3tools/shared/projectSettings"; -import { resolveProjectDefaultProviderModelSelection } from "../../providerInstances"; -import { resolveNewThreadModelSelection } from "@t3tools/shared/usageLimits"; const ADD_PROJECT_VALUE = "add-project"; @@ -75,7 +68,6 @@ export function DraftHeroHeadline({ }: DraftHeroHeadlineProps) { const projects = useProjects(); const { environments } = useEnvironments(); - const providersByEnvironment = useEnvironmentProviders(); const primaryEnvironmentId = usePrimaryEnvironmentId(); const projectGroupingSettings = useClientSettings(selectProjectGroupingSettings); const setLogicalProjectDraftThreadId = useComposerDraftStore( @@ -86,8 +78,6 @@ export function DraftHeroHeadline({ const isTriageDraftPrompt = useComposerDraftStore((store) => draftId ? isTriagePrompt(store.getComposerDraft(draftId)?.prompt ?? "") : false, ); - const applyStickyState = useComposerDraftStore((store) => store.applyStickyState); - const setModelSelection = useComposerDraftStore((store) => store.setModelSelection); const openAddProject = () => openCommandPalette({ open: "add-project" }); const { scratchEnvironmentId, scratchWorkspaceRootFor, openScratchProject } = useScratchProject(); const openProjectDraft = useNewThreadHandler(); @@ -186,51 +176,13 @@ export function DraftHeroHeadline({ return; } pendingScratchRef.current = null; - const currentDraft = getComposerDraft(draftId); - if (!composerDraftHasUserContent(currentDraft)) { - void openProjectDraft(scopeProjectRef(project.environmentId, project.id)); + const projectRef = scopeProjectRef(project.environmentId, project.id); + if (!composerDraftHasUserContent(getComposerDraft(draftId))) { + void openProjectDraft(projectRef); return; } - setLogicalProjectDraftThreadId( - logicalProjectKey, - scopeProjectRef(project.environmentId, project.id), - draftId, - ); - if (!hasExplicitComposerModelSelection(currentDraft)) { - applyStickyState(draftId); - const environmentConfig = environments.find( - (environment) => environment.environmentId === project.environmentId, - )?.serverConfig; - const environmentSettings = environmentConfig?.settings; - const defaultSelection = environmentSettings - ? resolveProjectSettings(environmentSettings, project.id, project).settings - .defaultModelSelection - : project.defaultModelSelection; - const fallbackResolution = resolveNewThreadModelSelection({ - defaultSelection: - defaultSelection ?? - (useComposerDraftStore.getState().stickyActiveProvider !== null || !environmentSettings - ? null - : resolveProjectDefaultProviderModelSelection( - providersByEnvironment.get(project.environmentId) ?? [], - environmentSettings, - project.id, - )), - fallbackSelection: environmentSettings?.fallbackModelSelection ?? null, - providers: providersByEnvironment.get(project.environmentId) ?? [], - ...(environmentSettings ? { settings: environmentSettings } : {}), - projectId: project.id, - now: Date.now(), - }); - const defaultModelSelection = fallbackResolution.fallback - ? fallbackResolution.selection - : defaultSelection; - if (defaultModelSelection) { - setModelSelection(draftId, defaultModelSelection, { - replaceOptions: true, - }); - } - } + setLogicalProjectDraftThreadId(logicalProjectKey, projectRef, draftId); + seedDraftModelSelection(draftId, projectRef); }; const startScratch = async (): Promise => { if (scratchTargetEnvironmentId === null || isScratchDraft) { diff --git a/apps/web/src/components/chat/useDraftEnvironmentRouting.browser.test.tsx b/apps/web/src/components/chat/useDraftEnvironmentRouting.browser.test.tsx index 3b23abfcb7c9a..b4ddb847d0824 100644 --- a/apps/web/src/components/chat/useDraftEnvironmentRouting.browser.test.tsx +++ b/apps/web/src/components/chat/useDraftEnvironmentRouting.browser.test.tsx @@ -39,7 +39,13 @@ vi.mock("../../hooks/useLoadBalancedEnvironment", () => ({ refreshSelection: resources.refreshSelection, }), })); -vi.mock("../../state/entities", () => ({ useProjects: () => resources.projects })); +vi.mock("../../state/entities", () => ({ + useProjects: () => resources.projects, + readProject: (ref: { environmentId: EnvironmentId; projectId: ProjectId }) => + resources.projects.find( + (project) => project.environmentId === ref.environmentId && project.id === ref.projectId, + ) ?? null, +})); vi.mock("../../hooks/useScratchProject", () => ({ useScratchProject: () => ({}) })); function setup() { @@ -314,3 +320,35 @@ it("keeps the selected models when automatic routing picks another machine", asy await expect.element(screen.getByText(b)).toBeInTheDocument(); await expect.element(screen.getByText("gpt-6.1-sol, gpt-6.1-luna")).toBeInTheDocument(); }); + +it.each([ + { name: "takes the destination default", explicit: false, remoteDefault: true, model: "luna" }, + { name: "keeps a model the user picked", explicit: true, remoteDefault: true, model: "sol" }, + { + name: "keeps the last-used model without a default", + explicit: false, + remoteDefault: false, + model: "sol", + }, +])("$name when the draft switches machine by hand", async ({ explicit, remoteDefault, model }) => { + const { b, draftId, input, store } = fanoutSetup(); + const codex = ProviderInstanceId.make("codex"); + resources.projects = resources.projects.map((project) => + project.environmentId === b && remoteDefault + ? { ...project, defaultModelSelection: createModelSelection(codex, "luna") } + : project, + ); + store.setStickyModelSelection(createModelSelection(codex, "sol")); + store.setModelSelection(draftId, createModelSelection(codex, "sol"), { explicit }); + await using screen = await render( + , + ); + await screen.getByRole("button", { name: `Switch to ${b}` }).click(); + await expect.element(screen.getByText(b)).toBeInTheDocument(); + const draft = store.getComposerDraft(draftId); + expect(draft?.activeProvider).toBe(codex); + expect(draft?.modelSelectionByProvider[codex]?.model).toBe(model); + expect(draft?.modelSelectionExplicit === true).toBe(explicit); +}); diff --git a/apps/web/src/components/chat/useDraftEnvironmentRouting.ts b/apps/web/src/components/chat/useDraftEnvironmentRouting.ts index 977f5cb31c7f1..86664b567a658 100644 --- a/apps/web/src/components/chat/useDraftEnvironmentRouting.ts +++ b/apps/web/src/components/chat/useDraftEnvironmentRouting.ts @@ -35,6 +35,7 @@ import { useAtomCommand } from "../../state/use-atom-command"; import type { DraftSessionState } from "../../composerDraftStore"; import { loadBalancingSendWarning } from "./composerSend.logic"; import { appAtomRegistry } from "../../rpc/atomRegistry"; +import { seedDraftModelSelection } from "../../lib/seedDraftModelSelection"; import { draftFanoutSelectionsAtom } from "./chatViewSend"; interface DraftEnvironmentRoutingInput { @@ -337,6 +338,7 @@ export function useDraftEnvironmentRouting(input: DraftEnvironmentRoutingInput) loadBalancedEnvironmentId: null, }); } + seedDraftModelSelection(draftId, projectRef); }; const finish = () => { if (environmentChangeRef.current === request) environmentChangeRef.current = null; diff --git a/apps/web/src/lib/seedDraftModelSelection.ts b/apps/web/src/lib/seedDraftModelSelection.ts new file mode 100644 index 0000000000000..2e781a932110f --- /dev/null +++ b/apps/web/src/lib/seedDraftModelSelection.ts @@ -0,0 +1,37 @@ +import type { ScopedProjectRef } from "@t3tools/contracts"; +import { resolveProjectSettings } from "@t3tools/shared/projectSettings"; +import { resolveNewThreadModelSelection } from "@t3tools/shared/usageLimits"; + +import { type DraftId, useComposerDraftStore } from "../composerDraftStore"; +import { resolveProjectDefaultProviderModelSelection } from "../providerInstances"; +import { readProject } from "../state/entities"; +import { readEnvironment, readEnvironmentProviders } from "../state/environments"; +import { hasExplicitComposerModelSelection } from "./chatThreadActions"; + +/** Gives a draft that moved project or machine the model a new thread there would start with. */ +export function seedDraftModelSelection(draftId: DraftId, projectRef: ScopedProjectRef): void { + const store = useComposerDraftStore.getState(); + if (hasExplicitComposerModelSelection(store.getComposerDraft(draftId))) return; + store.applyStickyState(draftId); + const project = readProject(projectRef); + const settings = readEnvironment(projectRef.environmentId)?.serverConfig?.settings; + const providers = readEnvironmentProviders(projectRef.environmentId) ?? []; + const defaultSelection = settings + ? resolveProjectSettings(settings, projectRef.projectId, project ?? undefined).settings + .defaultModelSelection + : (project?.defaultModelSelection ?? null); + const resolution = resolveNewThreadModelSelection({ + defaultSelection: + defaultSelection ?? + (useComposerDraftStore.getState().stickyActiveProvider !== null || !settings + ? null + : resolveProjectDefaultProviderModelSelection(providers, settings, projectRef.projectId)), + fallbackSelection: settings?.fallbackModelSelection ?? null, + providers, + ...(settings ? { settings } : {}), + projectId: projectRef.projectId, + now: Date.now(), + }); + const selection = resolution.fallback ? resolution.selection : defaultSelection; + if (selection) store.setModelSelection(draftId, selection, { replaceOptions: true }); +} diff --git a/apps/web/src/state/environments.ts b/apps/web/src/state/environments.ts index 0b2cab287d223..832a00a01616e 100644 --- a/apps/web/src/state/environments.ts +++ b/apps/web/src/state/environments.ts @@ -61,6 +61,12 @@ export function useEnvironmentProviders() { return useAtomValue(environmentPresentations.providersByEnvironmentAtom); } +export function readEnvironmentProviders(environmentId: EnvironmentId) { + return appAtomRegistry + .get(environmentPresentations.providersByEnvironmentAtom) + .get(environmentId); +} + export function usePrimaryEnvironmentId(): EnvironmentId | null { return useAtomValue(primaryEnvironmentIdAtom); } From 760f9cbe64e3409a7ebcc1ec16debe31a61e9f2b Mon Sep 17 00:00:00 2001 From: fast-fourier-transfem | yuridkowskian catgirlmbinator <33045716+01-1@users.noreply.github.com> Date: Mon, 16 Nov 2026 05:44:09 -0500 Subject: [PATCH 002/253] fix(provider-core): thread title instructions override the generic title limits (#37565) Co-authored-by: Julius Marminge --- .../ClaudeTextGeneration.test.ts | 2 +- evals/thread-titles/README.md | 2 +- evals/thread-titles/cases.ts | 16 +++++++++ evals/thread-titles/run.ts | 1 + .../src/server/textGenerationPrompts.test.ts | 8 ++++- .../src/server/textGenerationPrompts.ts | 33 ++++++++++++++----- 6 files changed, 51 insertions(+), 11 deletions(-) diff --git a/apps/server/src/textGeneration/ClaudeTextGeneration.test.ts b/apps/server/src/textGeneration/ClaudeTextGeneration.test.ts index d2e165ea3b2b5..84b7a9151e062 100644 --- a/apps/server/src/textGeneration/ClaudeTextGeneration.test.ts +++ b/apps/server/src/textGeneration/ClaudeTextGeneration.test.ts @@ -188,7 +188,7 @@ it.layer(layerClaudeTextGenerationTest)("ClaudeTextGeneration", (it) => { withFakeClaudeEnv( { output: JSON.stringify({ structured_output: { title: "Reconnect failures" } }), - stdinMustContain: "Additional instructions:\nUse two to four words.", + stdinMustContain: "Title instructions from the user:\nUse two to four words.", }, (textGeneration) => textGeneration.generateThreadTitle({ diff --git a/evals/thread-titles/README.md b/evals/thread-titles/README.md index 405ca9dde45c4..d180b8d0abef1 100644 --- a/evals/thread-titles/README.md +++ b/evals/thread-titles/README.md @@ -17,4 +17,4 @@ pnpm thread-titles --model --out runs/titles-new --baseline runs/t ## Add a case -Add an entry to `cases.ts` with an `id`, a `source`, the thread `messages`, the `previousTitle`, an optional `projectTitle`, and a `rubric` that says what a good title names. +Add an entry to `cases.ts` with an `id`, a `source`, the thread `messages`, the `previousTitle`, an optional `projectTitle`, optional `instructions` (the Thread title instructions setting), and a `rubric` that says what a good title names. diff --git a/evals/thread-titles/cases.ts b/evals/thread-titles/cases.ts index 795004fee422f..6f95afb55f829 100644 --- a/evals/thread-titles/cases.ts +++ b/evals/thread-titles/cases.ts @@ -152,12 +152,28 @@ export const threadTitleEvaluationCases = [ ], rubric: "Name the default config on init. Leave out Hyperdrive, the project name.", }, + { + id: "custom-title-scheme", + source: "https://github.com/pingdotgg/t3code/pull/37565", + request: "Relay chat to the lead agent, titled with the user's naming scheme.", + previousTitle: "Relay chat to lead agent", + instructions: 'Title every thread "Owner: what it does (model effort)".', + messages: [ + { + role: "user", + text: "Make the relay forward every chat message to the lead agent so reviewers can see questions from subagents.", + }, + ], + rubric: + 'Use the user\'s "Owner: what it does (model effort)" shape with all three parts, even past the default 8-word, 40-character limit. A bare short title fails.', + }, ] satisfies ReadonlyArray<{ id: string; source: string; request: string; projectTitle?: string; previousTitle: string; + instructions?: string; messages: ReadonlyArray; rubric: string; }>; diff --git a/evals/thread-titles/run.ts b/evals/thread-titles/run.ts index 3061e0e81e6da..d53571698b021 100644 --- a/evals/thread-titles/run.ts +++ b/evals/thread-titles/run.ts @@ -103,6 +103,7 @@ await Effect.runPromise( cwd, message, ...("projectTitle" in fixture ? { projectTitle: fixture.projectTitle } : {}), + ...("instructions" in fixture ? { instructions: fixture.instructions } : {}), previousTitle: values.initial ? undefined : fixture.previousTitle, attachments, linkedContext, diff --git a/packages/provider-core/src/server/textGenerationPrompts.test.ts b/packages/provider-core/src/server/textGenerationPrompts.test.ts index 3bc55845574c6..b257e9c9a98ea 100644 --- a/packages/provider-core/src/server/textGenerationPrompts.test.ts +++ b/packages/provider-core/src/server/textGenerationPrompts.test.ts @@ -18,14 +18,20 @@ describe("normalizeCliError", () => { }); describe("buildThreadTitlePrompt", () => { - it("names the project to leave out of initial and regenerated titles", () => { + it("names the project to leave out and puts the user's instructions above the limits", () => { for (const previousTitle of [undefined, "Hyperdrive Config Crash"]) { const { prompt } = buildThreadTitlePrompt({ message: "hyperdrive init crashes without a config", previousTitle, projectTitle: "Hyperdrive", + instructions: "Prefix every title with the team name.", }); expect(prompt).toContain('Never include the project name ("Hyperdrive")'); + expect(prompt).not.toContain("fewer than 40 characters"); + expect(prompt).toContain("Title instructions from the user:\nPrefix every title"); } + expect(buildThreadTitlePrompt({ message: "Fix login", instructions: " " }).prompt).toContain( + "3-8 words, fewer than 40 characters.", + ); }); }); diff --git a/packages/provider-core/src/server/textGenerationPrompts.ts b/packages/provider-core/src/server/textGenerationPrompts.ts index 61be2c1a6a222..7726ab9c0bb5a 100644 --- a/packages/provider-core/src/server/textGenerationPrompts.ts +++ b/packages/provider-core/src/server/textGenerationPrompts.ts @@ -9,9 +9,12 @@ import type { TextGenerationPolicy } from "@t3tools/provider-core/server/textGen const EARLIER_CONTENT_TRUNCATION_MARKER = "[Earlier content truncated]\n\n"; -function policyInstruction(instruction: string | undefined): ReadonlyArray { +function policyInstruction( + instruction: string | undefined, + heading = "Additional instructions:", +): ReadonlyArray { const trimmed = instruction?.trim(); - return trimmed ? ["", "Additional instructions:", limitSection(trimmed, 20_000)] : []; + return trimmed ? ["", heading, limitSection(trimmed, 20_000)] : []; } export interface CommitMessagePromptInput { @@ -375,8 +378,17 @@ function avoidRule(projectTitle: string | undefined, others: string): string { : `- Avoid project names already visible in the UI, ${others}.`; } +function lengthRule(hasInstructions: boolean): string { + return hasInstructions + ? "- Follow the user's title instructions at the end of this prompt. Where they conflict with these rules, including on length and format, they win." + : "- 3-8 words, fewer than 40 characters."; +} + // Keep shared editorial rules in these two prompts in sync. -function initialThreadTitlePrompt(projectTitle: string | undefined): string { +function initialThreadTitlePrompt( + projectTitle: string | undefined, + hasInstructions: boolean, +): string { return `Generate a title that will help the user recognize this T3 Code thread weeks later. Return JSON with keys title and needsRefinement. Set needsRefinement to true only if the subject is still unknown, such as an unresolved link, "fix this", or an unexplained attachment. Otherwise set it to false. @@ -389,7 +401,7 @@ Before answering, silently reduce the request to: Title the subject and outcome. Discard incidental instructions. Editorial rules: -- 3-8 words, fewer than 40 characters. +${lengthRule(hasInstructions)} - Use a compact noun phrase or clear action phrase. - Capture the umbrella goal when the request lists several symptoms or steps. - When asked about thread behavior, never include the referenced titles' topics. "[Fix login] why didn't this thread start?" means "Thread Startup Failure". Use linked topics only for requests about that work. @@ -409,6 +421,7 @@ ${avoidRule(projectTitle, "quotes, labels, filler, and trailing punctuation")} function regenerateThreadTitlePrompt( previousTitle: string, projectTitle: string | undefined, + hasInstructions: boolean, ): string { return `Regenerate the title for an existing T3 Code thread so the user can recognize it weeks later. The previous title was ${JSON.stringify(previousTitle)}. @@ -421,7 +434,7 @@ Determine the title in this order: 4. Title the durable subject and desired outcome, not the current workflow state. Editorial rules: -- 3-8 words, fewer than 40 characters. +${lengthRule(hasInstructions)} - Use a compact noun phrase or clear action phrase. - Preserve the umbrella subject when later messages focus on one finding, provider, platform, or implementation detail. - When asked about thread behavior, never include the referenced titles' topics. "[Fix login] why didn't this thread start?" means "Thread Startup Failure". Use linked topics only for requests about that work. @@ -467,7 +480,10 @@ ${limitTitleMessage(forkSource.history, 8_000)}`; } function threadTitlePromptSuffix(input: ThreadTitlePromptInput): string { - const additionalInstructions = policyInstruction(input.instructions); + const additionalInstructions = policyInstruction( + input.instructions, + "Title instructions from the user:", + ); const attachmentLines = (input.attachments ?? []).map( (attachment) => `- ${attachment.name} (${attachment.mimeType}, ${attachment.sizeBytes} bytes)`, ); @@ -485,16 +501,17 @@ function threadTitlePromptSuffix(input: ThreadTitlePromptInput): string { } export function buildThreadTitlePrompt(input: ThreadTitlePromptInput) { + const hasInstructions = Boolean(input.instructions?.trim()); let prompt: string; if (input.previousTitle === undefined) { const message = limitTitleMessage(input.message, 8_000); const label = input.forkSource === undefined ? "User message" : "User message after the fork"; - prompt = `${initialThreadTitlePrompt(input.projectTitle)}${forkSourceSection(input.forkSource)}\n\n${label}:\n${message}${threadTitlePromptSuffix(input)}`; + prompt = `${initialThreadTitlePrompt(input.projectTitle, hasInstructions)}${forkSourceSection(input.forkSource)}\n\n${label}:\n${message}${threadTitlePromptSuffix(input)}`; } else { const message = preserveMessageEnd(input.message); const label = input.forkSource === undefined ? "Thread contents" : "Thread contents after the fork"; - prompt = `${regenerateThreadTitlePrompt(input.previousTitle, input.projectTitle)}${forkSourceSection(input.forkSource)}\n\n${label}:\n${message}${threadTitlePromptSuffix(input)}`; + prompt = `${regenerateThreadTitlePrompt(input.previousTitle, input.projectTitle, hasInstructions)}${forkSourceSection(input.forkSource)}\n\n${label}:\n${message}${threadTitlePromptSuffix(input)}`; } const outputSchema = Schema.Struct({ title: Schema.String, From 629d5083b7b0278edf0a5f587f25b8d40c3fdfb2 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 03:08:42 -0800 Subject: [PATCH 003/253] fix(server): a Claude session gone on resume starts fresh instead of failing every send (#37610) --- .../Adapters/ClaudeAdapterV2.test.ts | 44 ++++++++++ .../Adapters/ClaudeAdapterV2.ts | 53 +++++++++++- .../ClaudeAgentSdkQueryRunner.test.ts | 43 ++++++++++ .../ProviderSessionManager.test.ts | 84 ++++++++++++++++++- .../ProviderSessionManager.ts | 29 +++++++ packages/provider-core/src/server/failure.ts | 1 + 6 files changed, 249 insertions(+), 5 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index b9e2f977a36ee..1d549d7f2c18b 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -14087,6 +14087,50 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); + it.effect("resumes in a fresh session after Claude Code reports its conversation gone", () => + Effect.gen(function* () { + const harness = yield* openAdapterWhoseFirstQueryEndsBeforeItsFirstFrame( + "claude-query-conversation-missing", + Stream.fail( + new ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError({ + method: "fromAsyncIterable", + cause: new ClaudeAdapterV2.ClaudeConversationNotFoundError({ + cause: new Error("Claude Code process exited with code 1"), + }), + }), + ), + ); + const turnInput = harness.turnInput("a", 1); + const started = yield* harness.runtime.startTurn(turnInput).pipe(Effect.exit); + assert.isTrue(Exit.isFailure(started)); + if (Exit.isFailure(started)) { + assert.include( + makeProviderFailure({ cause: started.cause, class: "transport_error" }).message, + "could not find this conversation", + ); + } + // A failed resume is what sends the next turn to a fresh native session. + const resumed = yield* harness.runtime + .resumeThread({ + providerThread: turnInput.providerThread, + threadId: turnInput.threadId, + modelSelection: CLAUDE_TEST_MODEL_SELECTION, + runtimePolicy: CLAUDE_TEST_RUNTIME_POLICY, + }) + .pipe(Effect.exit); + assert.isTrue(Exit.isFailure(resumed)); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + it.effect("fails a turn whose Claude query ends before its first frame", () => Effect.gen(function* () { const harness = yield* openAdapterWhoseFirstQueryEndsBeforeItsFirstFrame( diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 996501e885849..5dcb77f7abddc 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -383,6 +383,8 @@ const CLAUDE_DESCENDANT_POLL = "1 second"; // Claude Code's refusal of bypassPermissions as root, which it explains only on stderr. const CLAUDE_ROOT_REFUSAL = "--dangerously-skip-permissions cannot be used with root/sudo privileges for security reasons"; +// Claude Code's answer to a --resume whose session store is gone, as a result error and on stderr. +const CLAUDE_CONVERSATION_NOT_FOUND = "No conversation found with session ID:"; // fd 4 gates the launch until the server has recorded the process it is about to become. // The shell enters the cwd itself: as a native cwd, a mount that hangs chdir freezes the server. @@ -725,6 +727,17 @@ export class ClaudeCodeRootRefusalError extends Schema.TaggedError()( + "ClaudeConversationNotFoundError", + { + cause: Schema.Defect(), + }, +) { + override get message(): string { + return "Claude Code could not find this conversation to resume. Send the message again to continue in a new Claude session with this thread's history."; + } +} + class ClaudeMcpRegistrationTimeoutError extends Schema.TaggedError()( "ClaudeMcpRegistrationTimeoutError", { phase: Schema.Literals(["initialize", "setMcpServers"]) }, @@ -821,6 +834,14 @@ export interface ClaudeAgentSdkSubagentLookupInput { } const isClaudeAgentSdkQueryRunnerError = Schema.is(ClaudeAgentSdkQueryRunnerError); + +const isClaudeConversationNotFound = (cause: Cause.Cause) => { + const error = Cause.squash(cause); + return ( + isClaudeAgentSdkQueryRunnerError(error) && + Predicate.isTagged(error.cause, "ClaudeConversationNotFoundError") + ); +}; const isClaudeMcpRegistrationTimeoutError = Schema.is(ClaudeMcpRegistrationTimeoutError); const isProviderAdapterRuntimeRequestResponseError = Schema.is( ProviderAdapter.ProviderAdapterRuntimeRequestResponseError, @@ -1517,6 +1538,7 @@ export const layerQueryRunner: Layer.Layer< let stderrTail = ""; let stderrClosed: Promise = Promise.resolve(); let answered = false; + let conversationMissing = false; const appendStderr = (text: string) => { if (answered) return; stderrTail = (stderrTail + text).slice(-CLAUDE_STDERR_TAIL_LENGTH); @@ -1695,15 +1717,27 @@ export const layerQueryRunner: Layer.Layer< } const refusedRoot = !answered && stderrTail.includes(CLAUDE_ROOT_REFUSAL); return yield* queryRunnerError( - refusedRoot ? new ClaudeCodeRootRefusalError({ cause }) : cause, + refusedRoot + ? new ClaudeCodeRootRefusalError({ cause }) + : conversationMissing || + (!answered && stderrTail.includes(CLAUDE_CONVERSATION_NOT_FOUND)) + ? new ClaudeConversationNotFoundError({ cause }) + : cause, "fromAsyncIterable", ); }), ), ), - Stream.tap(() => + Stream.tap((message) => Effect.sync(() => { answered = true; + if ( + message.type === "result" && + message.subtype !== "success" && + message.errors.some((error) => error.startsWith(CLAUDE_CONVERSATION_NOT_FOUND)) + ) { + conversationMissing = true; + } }), ), Stream.tap((message) => @@ -5843,6 +5877,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( ); }); const openedNativeThreads = yield* Ref.make(new Set()); + // Sessions Claude Code reported gone on resume; resumeThread fails them into a fresh session. + const missingNativeThreads = new Set(); const pdfBudgets = new Map>(); const latestPlanByKind = yield* Ref.make(new Map()); // Claude 5 task tools edit one list per native session, across turns. @@ -12910,7 +12946,12 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( Effect.exit, Effect.flatMap( Effect.fnUntraced(function* (exit: ClaudeQueryStreamExit) { - if (exit._tag === "Failure") context.exitCause = exit.cause; + if (exit._tag === "Failure") { + context.exitCause = exit.cause; + if (isClaudeConversationNotFound(exit.cause)) { + missingNativeThreads.add(context.nativeThreadId); + } + } const heldContext = yield* Ref.get(activeTurn); if ( (yield* Ref.get(queryContext))?.query === querySession && @@ -13877,6 +13918,12 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( // A live process holds the conversation even before saving it. (yield* Ref.get(queryContext))?.nativeThreadId !== nativeThreadId ) { + if (missingNativeThreads.has(nativeThreadId)) { + return yield* new ProviderAdapter.ProviderAdapterProtocolError({ + driver: CLAUDE_PROVIDER, + detail: `Claude Code has no conversation ${nativeThreadId} to resume.`, + }); + } // An unreadable transcript leaves the resume to the CLI. const transcript = yield* claudeTranscriptState(nativeThreadId).pipe( Effect.orElseSucceed(() => "missing" as const), diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts index 4a87c04043029..28072c29d7fda 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts @@ -710,6 +710,49 @@ describe.skipIf(HostProcess.Platform.defaultValue() === "win32")("Claude CLI pro const unknown = yield* startupFailureMessage("token=sk-private-123"); assert.notInclude(unknown, "sk-private-123"); + + const missingConversation = yield* startupFailureMessage( + "No conversation found with session ID: sk-private-123", + ); + assert.include(missingConversation, "could not find this conversation"); + assert.notInclude(missingConversation, "sk-private-123"); + }).pipe(Effect.provide(realQueryRunner)), + ); + + // What Claude Code writes when the session store behind --resume is gone. + it.effect("names a resumed conversation Claude Code cannot find", () => + Effect.gen(function* () { + const missing = "No conversation found with session ID: native-session-fake-cli"; + const result = JSON.stringify({ + type: "result", + subtype: "error_during_execution", + is_error: true, + num_turns: 0, + duration_ms: 0, + duration_api_ms: 0, + total_cost_usd: 0, + usage: {}, + modelUsage: {}, + permission_denials: [], + errors: [missing], + session_id: "native-session-fake-cli", + uuid: "00000000-0000-4000-8000-000000000000", + }); + const cli = fakeClaudeCli( + `printf '%s\\n' '${result}'\nprintf '%s\\n' '${missing}' >&2\nexit 1`, + ); + yield* Effect.addFinalizer(() => + Effect.sync(() => NodeFS.rmSync(NodePath.dirname(cli), { recursive: true, force: true })), + ); + const session = yield* openFakeCli(cli); + const exit = yield* Stream.runDrain(session.messages).pipe(Effect.exit); + assert.isTrue(Exit.isFailure(exit)); + if (Exit.isFailure(exit)) { + assert.include( + makeProviderFailure({ cause: exit.cause, class: "transport_error" }).message, + "could not find this conversation", + ); + } }).pipe(Effect.provide(realQueryRunner)), ); diff --git a/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts b/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts index 919784509bf09..69375c34a2a66 100644 --- a/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts +++ b/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts @@ -459,7 +459,7 @@ const makeProviderAdapter = Effect.fnUntraced(function* ( readonly hasPendingBackgroundWorkForThread?: Effect.Effect; readonly backgroundTasksEndWithProcess?: boolean; readonly hangSessionScopeClose?: boolean; - readonly startTurn?: Effect.Effect; + readonly startTurn?: Effect.Effect; readonly interruptTurn?: Effect.Effect; readonly beforeUnload?: Effect.Effect; readonly unloads?: Queue.Queue; @@ -641,7 +641,7 @@ function layerTest(input: { readonly hasPendingBackgroundWorkForThread?: Effect.Effect; readonly backgroundTasksEndWithProcess?: boolean; readonly hangSessionScopeClose?: boolean; - readonly startTurn?: Effect.Effect; + readonly startTurn?: Effect.Effect; readonly interruptTurn?: Effect.Effect; readonly beforeUnload?: Effect.Effect; readonly unloads?: Queue.Queue; @@ -1634,6 +1634,86 @@ it.effect("ProviderSessionManagerV2 gives up on a provider that never confirms a }), ); +// A failed start can leave its native thread gone, as when Claude rejects a resume. +it.effect("ProviderSessionManagerV2 resumes a thread natively again after its start failed", () => + Effect.gen(function* () { + const state = yield* Ref.make(emptyState); + yield* Effect.gen(function* () { + const eventSink = yield* EventSink.EventSinkV2; + const idAllocator = yield* IdAllocator.IdAllocatorV2; + const manager = yield* ProviderSessionManager.ProviderSessionManagerV2; + const projectionStore = yield* ProjectionStore.ProjectionStoreV2; + const now = yield* DateTime.now; + const threadId = ThreadId.make("thread-provider-session-manager-failed-start-resume"); + const providerSessionId = idAllocator.derive.providerSession({ + providerInstanceId: modelSelection.instanceId, + }); + yield* eventSink.write({ + events: [yield* makeThreadCreatedEvent({ idAllocator, threadId, now })], + }); + const runtime = yield* manager.open({ + threadId, + providerSessionId, + modelSelection, + runtimePolicy, + }); + const providerThread = makeProviderThread({ + idAllocator, + threadId, + providerSessionId, + now, + nativeThreadId: `native-${threadId}`, + }); + const resume = runtime.resumeThread({ + providerThread, + threadId, + modelSelection, + runtimePolicy, + }); + yield* resume; + yield* resume; + assert.equal((yield* Ref.get(state)).resumeCount, 1); + const runId = idAllocator.derive.run({ threadId, ordinal: 1 }); + yield* runtime + .startTurn({ + appThread: (yield* projectionStore.getThreadProjection(threadId)).thread, + threadId, + runId, + runOrdinal: 1, + providerTurnOrdinal: 1, + attemptId: idAllocator.derive.runAttempt({ runId, attemptOrdinal: 1 }), + rootNodeId: idAllocator.derive.rootNode({ runId }), + providerThread, + message: { + createdBy: "user", + creationSource: "web", + messageId: yield* idAllocator.allocate.message({ threadId, ordinal: 1 }), + text: "turn", + attachments: [], + }, + modelSelection, + runtimePolicy, + }) + .pipe(Effect.flip); + yield* resume; + assert.equal((yield* Ref.get(state)).resumeCount, 2); + }).pipe( + Effect.provide( + layerTest({ + state, + idleTimeoutMs: 60_000, + startTurn: Effect.fail( + new ProviderAdapter.ProviderAdapterProtocolError({ + driver: CODEX_DRIVER, + detail: "query ended before the turn could start", + }), + ), + }), + ), + ); + }), +); + it.effect("ProviderSessionManagerV2 still idles out a session releaseUnlessBusy kept", () => Effect.gen(function* () { const state = yield* Ref.make(emptyState); diff --git a/apps/server/src/orchestration-v2/ProviderSessionManager.ts b/apps/server/src/orchestration-v2/ProviderSessionManager.ts index 8e4c381bc8b9a..ba9ba7a2bd3f3 100644 --- a/apps/server/src/orchestration-v2/ProviderSessionManager.ts +++ b/apps/server/src/orchestration-v2/ProviderSessionManager.ts @@ -1683,6 +1683,25 @@ export const layerWithOptions = ( ), ); + const forgetProviderThreadLoaded = (input: { + readonly providerSessionId: ProviderSessionId; + readonly threadId: ThreadId; + readonly runtime: ProviderAdapter.ProviderAdapterV2SessionRuntime; + }) => + Ref.update(sessions, (current) => { + const key = sessionKey(input.providerSessionId); + const entry = current.get(key); + if ( + entry?.runtime !== input.runtime || + !entry.loadedProviderThreadKeyByThread.has(input.threadId) + ) { + return current; + } + const loadedProviderThreadKeyByThread = new Map(entry.loadedProviderThreadKeyByThread); + loadedProviderThreadKeyByThread.delete(input.threadId); + return new Map(current).set(key, { ...entry, loadedProviderThreadKeyByThread }); + }); + /** Pass the setting read before a native load; cache hits keep the earlier record. */ const markProviderThreadLoaded = (input: { readonly providerSessionId: ProviderSessionId; @@ -2494,6 +2513,16 @@ export const layerWithOptions = ( ? observeActivity( providerSessionId, markIdle(providerSessionId, input.providerThread.id, input.runOrdinal), + ).pipe( + // A failed start can leave its native thread gone (Claude rejecting a + // resume), so the next start must ask the adapter to resume it again. + Effect.andThen( + forgetProviderThreadLoaded({ + providerSessionId, + threadId: input.threadId, + runtime, + }), + ), ) : Effect.void, ), diff --git a/packages/provider-core/src/server/failure.ts b/packages/provider-core/src/server/failure.ts index 4131e470fa2f5..136d867e521c2 100644 --- a/packages/provider-core/src/server/failure.ts +++ b/packages/provider-core/src/server/failure.ts @@ -122,6 +122,7 @@ function curatedCauseMessages(cause: unknown): { case "ClaudeSettingsLaunchArgError": case "ClaudeMcpRegistrationTimeoutError": case "ClaudeCodeRootRefusalError": + case "ClaudeConversationNotFoundError": case "CursorModelUnavailableError": case "CodexGoalObjectiveTooLongError": case "ProviderSetupError": From 1f698f01d9fbbc5858963c1fe81d4b3e00783a5d Mon Sep 17 00:00:00 2001 From: Arif Ali <51419655+arifali123@users.noreply.github.com> Date: Mon, 16 Nov 2026 04:13:21 -0800 Subject: [PATCH 004/253] fix: PR diffs fold Viewed files the way GitHub does, on web and mobile (#37605) Co-authored-by: Orin Co-authored-by: Julius Marminge --- .../diffs/nativeReviewDiffSurface.test.tsx | 1 + .../pull-requests/PullRequestRouteScreen.tsx | 52 ++++++++++++------ .../pullRequestDiffDebug.test.ts | 6 +++ .../pull-requests/pullRequestDiffDebug.ts | 8 ++- .../PullRequestCodeTab.browser.test.tsx | 54 ++++++++++++++++++- .../pullRequest/PullRequestCodeTab.tsx | 27 +++++----- .../pullRequest/pullRequestDiff.logic.test.ts | 36 +------------ .../pullRequest/pullRequestDiff.logic.ts | 27 ---------- packages/client-runtime/package.json | 4 ++ .../src/pullRequestDiffFold.test.ts | 41 ++++++++++++++ .../client-runtime/src/pullRequestDiffFold.ts | 25 +++++++++ 11 files changed, 187 insertions(+), 94 deletions(-) create mode 100644 packages/client-runtime/src/pullRequestDiffFold.test.ts create mode 100644 packages/client-runtime/src/pullRequestDiffFold.ts diff --git a/apps/mobile/src/features/diffs/nativeReviewDiffSurface.test.tsx b/apps/mobile/src/features/diffs/nativeReviewDiffSurface.test.tsx index 2d29be6d8b5ec..8957f95c8a97e 100644 --- a/apps/mobile/src/features/diffs/nativeReviewDiffSurface.test.tsx +++ b/apps/mobile/src/features/diffs/nativeReviewDiffSurface.test.tsx @@ -219,6 +219,7 @@ function setup(integrated = false) { const onDebug: NonNullable = (event) => handlePullRequestDiffDebugEvent(event, { rowCount: data.rows.length, + lastShownRowIndex: data.rows.length - 1, targetFileId, focusIndex: -1, isIos: true, diff --git a/apps/mobile/src/features/pull-requests/PullRequestRouteScreen.tsx b/apps/mobile/src/features/pull-requests/PullRequestRouteScreen.tsx index d72cbe743f23e..0ae3386b45b45 100644 --- a/apps/mobile/src/features/pull-requests/PullRequestRouteScreen.tsx +++ b/apps/mobile/src/features/pull-requests/PullRequestRouteScreen.tsx @@ -1,3 +1,8 @@ +import { + isFileDiffCollapsed, + withFileDiffFold, + type FileDiffFoldChoices, +} from "@t3tools/client-runtime/pull-request-diff-fold"; import { parsePullRequestAnchor, pullRequestAnchorMatchesUrl, @@ -130,8 +135,6 @@ import { sourceControlClients, } from "@t3tools/client-runtime/source-control-clients"; -const EMPTY_IDS: readonly string[] = []; - function usePullRequestActivityRows(params: PullRequestParams, filter: ActivityFilter) { const navigation = useNavigation(); const result = useEnvironmentQuery(composerPullRequests.activity(requestTarget(params))); @@ -1088,7 +1091,26 @@ function PullRequestFileRouteContent({ route }: PullRequestFileRouteProps) { const focusIndex = focusedRows.length ? data.rows.findIndex((row) => row.id === focusedRows[0]?.id) : -1; - const [collapsedIds, setCollapsedIds] = useState(EMPTY_IDS); + const [foldChoices, setFoldChoices] = useState(() => new Map()); + const [foldScope, setFoldScope] = useState(`${scopeKey}:${commit ?? "all"}`); + const [revealedFileId, setRevealedFileId] = useState(null); + if (foldScope !== `${scopeKey}:${commit ?? "all"}`) { + setFoldScope(`${scopeKey}:${commit ?? "all"}`); + setFoldChoices(new Map()); + setRevealedFileId(null); + } + const requestedFileId = + (jumpPath ? data.files.find((file) => file.path === jumpPath)?.id : null) ?? null; + // A linked file must stay open if its Viewed response arrives after the first native draw. + if (requestedFileId !== revealedFileId) { + setRevealedFileId(requestedFileId); + if (requestedFileId) + setFoldChoices((current) => withFileDiffFold(current, requestedFileId, false)); + } + const viewedIdSet = new Set(viewedIds); + const collapsedIds = data.files + .filter((file) => isFileDiffCollapsed(file.id, null, foldChoices, viewedIdSet.has(file.id))) + .map((file) => file.id); const [visibleFileId, setVisibleFileId] = useState(null); const nativeView = useRef(null); // The filter the current jump was made under. Hiding or showing test files replaces the rows, @@ -1114,7 +1136,7 @@ function PullRequestFileRouteContent({ route }: PullRequestFileRouteProps) { jumpedUnder.current = null; const file = data.files.find((entry) => entry.path === path); if (!file) return; - setCollapsedIds((ids) => ids.filter((id) => id !== file.id)); + setFoldChoices((current) => withFileDiffFold(current, file.id, false)); scrollToFile(file.id); }); // Subscribed for the whole screen, so a pick made while the diff is still loading is kept for @@ -1138,9 +1160,14 @@ function PullRequestFileRouteContent({ route }: PullRequestFileRouteProps) { useEffect(() => { if (!targetFile) loadMore(); }, [targetFile, loadMore]); + const lastFileRowIndex = data.rows.findLastIndex((row) => row.kind === "file"); + const lastFileId = data.rows[lastFileRowIndex]?.fileId; + const lastShownRowIndex = + lastFileId && collapsedIds.includes(lastFileId) ? lastFileRowIndex : data.rows.length - 1; const onDebug: NonNullable = (event) => handlePullRequestDiffDebugEvent(event, { rowCount: data.rows.length, + lastShownRowIndex, targetFileId: targetFile?.id, focusIndex, isIos: Platform.OS === "ios", @@ -1155,8 +1182,8 @@ function PullRequestFileRouteContent({ route }: PullRequestFileRouteProps) { const onToggleFile: NonNullable = (event) => { const id = event.nativeEvent.fileId; if (!id) return; - setCollapsedIds((ids) => - ids.includes(id) ? ids.filter((value) => value !== id) : [...ids, id], + setFoldChoices((current) => + withFileDiffFold(current, id, !isFileDiffCollapsed(id, null, current, viewedIdSet.has(id))), ); }; const visibleFile = data.files.find((file) => file.id === visibleFileId) ?? targetFile; @@ -1270,7 +1297,7 @@ function PullRequestFileRouteContent({ route }: PullRequestFileRouteProps) { setDiffHead({ scopeKey, sha: detail.data?.headSha ?? null }); jumpedUnder.current = null; setSelectedRowId(null); - setCollapsedIds(EMPTY_IDS); + setFoldChoices(new Map()); }} /> @@ -1311,16 +1338,7 @@ function PullRequestFileRouteContent({ route }: PullRequestFileRouteProps) { onToggleViewedFile={(event) => { const file = data.files.find((file) => file.id === event.nativeEvent.fileId); if (!file || (!commit && !viewed.canToggle)) return; - const wasViewed = commit - ? commitViewedPaths.has(file.path) - : viewed.isViewed(file.path); - setCollapsedIds((ids) => - wasViewed - ? ids.filter((id) => id !== file.id) - : ids.includes(file.id) - ? ids - : [...ids, file.id], - ); + setFoldChoices((current) => withFileDiffFold(current, file.id, undefined)); if (commit) setCommitViewedPaths((current) => { const next = new Set(current); diff --git a/apps/mobile/src/features/pull-requests/pullRequestDiffDebug.test.ts b/apps/mobile/src/features/pull-requests/pullRequestDiffDebug.test.ts index 23f5ba4726d62..80ccb09e50da2 100644 --- a/apps/mobile/src/features/pull-requests/pullRequestDiffDebug.test.ts +++ b/apps/mobile/src/features/pull-requests/pullRequestDiffDebug.test.ts @@ -42,9 +42,11 @@ it.each([true, false])( lastRowIndex: nativeEvent.lastRowIndex as number, }); }; + let lastShownRowIndex = 999; const emit = (nativeEvent: Record) => handlePullRequestDiffDebugEvent({ nativeEvent } as Parameters[0], { rowCount: 1000, + lastShownRowIndex, targetFileId: "last-file", focusIndex: -1, isIos, @@ -74,6 +76,10 @@ it.each([true, false])( expect(loadMore).not.toHaveBeenCalled(); emit({ message: "visible-range", firstRowIndex: 950, lastRowIndex: 990, totalRows: 1000 }); expect(loadMore).toHaveBeenCalledOnce(); + // A folded final file ends at its header, short of the full payload's tail. + lastShownRowIndex = 920; + emit({ message: "visible-range", firstRowIndex: 880, lastRowIndex: 920, totalRows: 1000 }); + expect(loadMore).toHaveBeenCalledTimes(2); } finally { scheduler.cancel(); vi.useRealTimers(); diff --git a/apps/mobile/src/features/pull-requests/pullRequestDiffDebug.ts b/apps/mobile/src/features/pull-requests/pullRequestDiffDebug.ts index b6db266c38aa5..52fe61a65a4cc 100644 --- a/apps/mobile/src/features/pull-requests/pullRequestDiffDebug.ts +++ b/apps/mobile/src/features/pull-requests/pullRequestDiffDebug.ts @@ -21,6 +21,8 @@ export function handlePullRequestDiffDebugEvent( event: Parameters>[0], input: { readonly rowCount: number; + /** The last row native can draw: a folded final file ends at its header. */ + readonly lastShownRowIndex: number; readonly targetFileId: string | undefined; readonly focusIndex: number; readonly isIos: boolean; @@ -60,5 +62,9 @@ export function handlePullRequestDiffDebugEvent( } if (!drawn) return; const lastRow = event.nativeEvent.lastRowIndex; - if (typeof lastRow === "number" && lastRow >= input.rowCount - 40) input.loadMore(); + if ( + typeof lastRow === "number" && + lastRow >= Math.min(input.rowCount - 40, input.lastShownRowIndex) + ) + input.loadMore(); } diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx index 5b9d0af32a129..21f5c099a54cb 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx @@ -8,6 +8,7 @@ import { type PullRequestDiffResult, } from "@t3tools/contracts"; import { expect, it, vi } from "vite-plus/test"; +import { userEvent } from "vite-plus/test/browser"; import "~/index.css"; import { __setClientSettingsForTests } from "../../hooks/useSettings"; @@ -85,7 +86,12 @@ vi.mock(import("./usePullRequestFilesViewed"), async () => { enabled: viewedPaths !== null, isViewed: (path: string) => viewedPaths?.has(path) === true, isStale: () => false, - setViewed: () => {}, + setViewed: (path: string, viewed: boolean) => { + const next = new Set(mocks.viewedPaths); + if (viewed) next.add(path); + else next.delete(path); + deliverViewedPaths(next); + }, truncated: false, error: null, refresh: () => {}, @@ -207,6 +213,52 @@ it("keeps the diff after a refresh that brings back the same page", async () => await expect.element(screen.getByText("1 file", { exact: true })).toBeVisible(); }); +it("restores saved Viewed folds, follows cleared marks, and allows explicit expansion", async () => { + setup({ paged: false }); + __setClientSettingsForTests({ ...DEFAULT_CLIENT_SETTINGS, diffFilesCollapsed: false }); + using _preferences = { + [Symbol.dispose]() { + __setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS); + }, + }; + mocks.viewedPaths = new Set(["a.ts"]); + const container = document.createElement("div"); + container.className = "flex h-[700px] w-[900px]"; + await using screen = await render(tab(0), { container }); + await expect.element(screen.getByRole("checkbox", { name: "Viewed" })).toBeChecked(); + await expect + .element(screen.getByRole("button", { name: "Expand diff", exact: true })) + .toHaveAttribute("aria-expanded", "false"); + + await screen.getByRole("button", { name: "Expand diff", exact: true }).click(); + await expect.element(screen.getByText("three()", { exact: true })).toBeVisible(); + screen.getByRole("checkbox", { name: "Viewed" }).element().focus(); + await userEvent.keyboard(" "); + await expect.element(screen.getByRole("checkbox", { name: "Viewed" })).not.toBeChecked(); + await expect.element(screen.getByText("three()", { exact: true })).toBeVisible(); + screen.getByRole("checkbox", { name: "Viewed" }).element().focus(); + await userEvent.keyboard(" "); + await expect + .element(screen.getByRole("button", { name: "Expand diff", exact: true })) + .toHaveAttribute("aria-expanded", "false"); + + deliverViewedPaths(new Set()); + await expect.element(screen.getByRole("checkbox", { name: "Viewed" })).not.toBeChecked(); + await expect.element(screen.getByText("three()", { exact: true })).toBeVisible(); + deliverViewedPaths(new Set(["a.ts"])); + await expect + .element(screen.getByRole("button", { name: "Expand diff", exact: true })) + .toHaveAttribute("aria-expanded", "false"); + await screen.getByRole("button", { name: "Expand all files", exact: true }).click(); + await expect.element(screen.getByText("three()", { exact: true })).toBeVisible(); + + await screen.rerender(
); + await screen.rerender(tab(1)); + await expect + .element(screen.getByRole("button", { name: "Expand diff", exact: true })) + .toHaveAttribute("aria-expanded", "false"); +}); + // regression: #13279 a Viewed answer that lands after a link reveals a file must not fold it it("keeps a revealed file open when its Viewed mark loads afterwards", async () => { setup({ paged: false }); diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx index f52f1a064277f..5acc37153d736 100644 --- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx @@ -1,3 +1,9 @@ +import { + isFileDiffCollapsed, + withFileDiffFold, + type DiffFoldOverride, + type FileDiffFoldChoices, +} from "@t3tools/client-runtime/pull-request-diff-fold"; import { parsePullRequestAnchor, pullRequestDiffPathMatches, @@ -132,14 +138,7 @@ import { toViewerSide, } from "./PullRequestReviewAnnotation"; import { pullRequestCompletionScope } from "./PullRequestMarkdownTextarea"; -import { - getFileViewedRange, - isFileDiffCollapsed, - isLineInFileDiff, - withFileDiffFold, - type DiffFoldOverride, - type FileDiffFoldChoices, -} from "./pullRequestDiff.logic"; +import { getFileViewedRange, isLineInFileDiff } from "./pullRequestDiff.logic"; import { PullRequestDiffStat, PullRequestMetaLine } from "./pullRequestPresentation"; import { PullRequestDiffUnavailableState } from "./PullRequestDiffUnavailableState"; import { usePullRequestFilesViewed } from "./usePullRequestFilesViewed"; @@ -439,8 +438,7 @@ function PullRequestCodeTab({ const [lastViewedFileKey, setLastViewedFileKey] = useState(null); const [visibleCommitCount, setVisibleCommitCount] = useState(COMMIT_PAGE_SIZE); const [foldOverride, setFoldOverride] = useState(null); - const effectiveFoldOverride = - foldOverride ?? (settings.diffFilesCollapsed ? "folded" : "expanded"); + const effectiveFoldOverride = foldOverride ?? (settings.diffFilesCollapsed ? "folded" : null); const diffLayout = settings.diffLayout; const [wordWrapOverride, setWordWrapOverride] = useState(null); const wordWrap = wordWrapOverride ?? settings.wordWrap; @@ -932,9 +930,8 @@ function PullRequestCodeTab({
); - // A stale mark means the file changed since it was viewed; Fold all and Expand all outrank marks. const foldsAsViewed = (path: string) => - foldOverride === null && filesViewedEnabled && isFileViewed(path) && !isFileViewedStale(path); + filesViewedEnabled && isFileViewed(path) && !isFileViewedStale(path); const items: CodeViewDiffItem[] = annotatedFiles.map( ({ fileKey, path, fileDiff, annotations, annotationsVersion }) => { const collapsed = isFileDiffCollapsed( @@ -1007,7 +1004,11 @@ function PullRequestCodeTab({ setViewed(file.path, viewed, commit === null ? file.fileDiff.newObjectId : undefined); } setFoldChoices((current) => - range.reduce((next, file) => withFileDiffFold(next, file.fileKey, viewed), current), + range.reduce( + (next, file) => + withFileDiffFold(next, file.fileKey, effectiveFoldOverride === null ? undefined : viewed), + current, + ), ); }; const fileViewedToggle = { diff --git a/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts b/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts index 47f309b3e32c0..ce73f75d2db0c 100644 --- a/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts +++ b/apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts @@ -1,12 +1,7 @@ import type { FileDiffMetadata } from "@pierre/diffs"; import { describe, expect, it } from "vite-plus/test"; -import { - getFileViewedRange, - isFileDiffCollapsed, - isLineInFileDiff, - withFileDiffFold, -} from "./pullRequestDiff.logic"; +import { getFileViewedRange, isLineInFileDiff } from "./pullRequestDiff.logic"; function fileWithHunks( hunks: ReadonlyArray<{ @@ -40,35 +35,6 @@ describe("isLineInFileDiff", () => { }); }); -describe("isFileDiffCollapsed", () => { - // regression: #9174 diffs now expand by default; a reader's fold must survive new slices - it("keeps a file the reader folded closed as the next slice arrives", () => { - const choices = new Map([["b.ts", true]]); - expect(isFileDiffCollapsed("b.ts", "expanded", choices)).toBe(true); - expect(isFileDiffCollapsed("c.ts", "expanded", choices)).toBe(false); - }); - - it("folds a file marked Viewed when the Code tab mounts again with no fold choices", () => { - expect(isFileDiffCollapsed("a.ts", "expanded", new Map(), true)).toBe(true); - expect(isFileDiffCollapsed("a.ts", "expanded", new Map(), false)).toBe(false); - }); - - it("keeps a reader's choice when the file's Viewed mark loads later", () => { - const choices = new Map([["a.ts", false]]); - expect(isFileDiffCollapsed("a.ts", "expanded", choices, false)).toBe(false); - expect(isFileDiffCollapsed("a.ts", "expanded", choices, true)).toBe(false); - }); -}); - -describe("withFileDiffFold", () => { - it("records an absolute choice and keeps the same map when nothing changes", () => { - const folded = withFileDiffFold(new Map(), "a.ts", true); - expect(folded.get("a.ts")).toBe(true); - expect(withFileDiffFold(folded, "a.ts", true)).toBe(folded); - expect(withFileDiffFold(folded, "a.ts", false).get("a.ts")).toBe(false); - }); -}); - describe("getFileViewedRange", () => { const files = ["c.ts", "a.ts", "b.ts", "d.ts"].map((fileKey) => ({ fileKey })); diff --git a/apps/web/src/components/pullRequest/pullRequestDiff.logic.ts b/apps/web/src/components/pullRequest/pullRequestDiff.logic.ts index da3844e90e1bb..f0c01a0611c3b 100644 --- a/apps/web/src/components/pullRequest/pullRequestDiff.logic.ts +++ b/apps/web/src/components/pullRequest/pullRequestDiff.logic.ts @@ -14,33 +14,6 @@ export function isLineInFileDiff( ); } -export type DiffFoldOverride = "expanded" | "folded" | null; - -/** The reader's own fold choice per file. It is absolute, so a Viewed mark loading later keeps it. */ -export type FileDiffFoldChoices = ReadonlyMap; - -/** - * A reader's own choice wins. Otherwise the toolbar decides, and a file still marked Viewed - * starts folded. Fold all and Expand all clear the choices, so later pages follow the toolbar. - */ -export function isFileDiffCollapsed( - fileKey: string, - foldOverride: DiffFoldOverride, - choices: FileDiffFoldChoices, - viewed = false, -): boolean { - return choices.get(fileKey) ?? (foldOverride === "folded" || viewed); -} - -export function withFileDiffFold( - choices: FileDiffFoldChoices, - fileKey: string, - collapsed: boolean, -): FileDiffFoldChoices { - if (choices.get(fileKey) === collapsed) return choices; - return new Map(choices).set(fileKey, collapsed); -} - export function getFileViewedRange( files: ReadonlyArray, fileKey: string, diff --git a/packages/client-runtime/package.json b/packages/client-runtime/package.json index 19989716bc3c6..6fa96cdd493ae 100644 --- a/packages/client-runtime/package.json +++ b/packages/client-runtime/package.json @@ -3,6 +3,10 @@ "private": true, "type": "module", "exports": { + "./pull-request-diff-fold": { + "types": "./src/pullRequestDiffFold.ts", + "default": "./src/pullRequestDiffFold.ts" + }, "./assembly-highlighting": { "types": "./src/assemblyHighlighting.ts", "default": "./src/assemblyHighlighting.ts" diff --git a/packages/client-runtime/src/pullRequestDiffFold.test.ts b/packages/client-runtime/src/pullRequestDiffFold.test.ts new file mode 100644 index 0000000000000..34269c2212a05 --- /dev/null +++ b/packages/client-runtime/src/pullRequestDiffFold.test.ts @@ -0,0 +1,41 @@ +import { describe, expect, it } from "vite-plus/test"; + +import { isFileDiffCollapsed, withFileDiffFold } from "./pullRequestDiffFold.ts"; + +describe("isFileDiffCollapsed", () => { + it("collapses saved viewed files on reopening and when a later read marks them viewed", () => { + const overrides = new Map(); + expect(isFileDiffCollapsed("a.ts", null, overrides, false)).toBe(false); + expect(isFileDiffCollapsed("a.ts", null, overrides, true)).toBe(true); + expect(isFileDiffCollapsed("a.ts", null, overrides, false)).toBe(false); + }); + + it("preserves individual choices while viewed state and loaded pages change", () => { + const overrides = new Map([ + ["a.ts", false], + ["b.ts", true], + ]); + expect(isFileDiffCollapsed("a.ts", null, overrides, true)).toBe(false); + expect(isFileDiffCollapsed("b.ts", null, overrides, false)).toBe(true); + expect(isFileDiffCollapsed("c.ts", null, overrides, true)).toBe(true); + expect(isFileDiffCollapsed("d.ts", null, overrides, false)).toBe(false); + }); + + it("respects expand all and fold all, including later pages and individual exceptions", () => { + const overrides = new Map(); + expect(isFileDiffCollapsed("a.ts", "expanded", overrides, true)).toBe(false); + expect(isFileDiffCollapsed("b.ts", "folded", overrides, false)).toBe(true); + expect(isFileDiffCollapsed("c.ts", "folded", new Map([["c.ts", false]]), true)).toBe(false); + expect(isFileDiffCollapsed("d.ts", "expanded", new Map([["d.ts", true]]), false)).toBe(true); + }); +}); + +describe("withFileDiffFold", () => { + it("records or clears a choice and keeps the same map when nothing changes", () => { + const folded = withFileDiffFold(new Map(), "a.ts", true); + expect(folded.get("a.ts")).toBe(true); + expect(withFileDiffFold(folded, "a.ts", true)).toBe(folded); + expect(withFileDiffFold(folded, "a.ts", false).get("a.ts")).toBe(false); + expect(withFileDiffFold(folded, "a.ts", undefined).has("a.ts")).toBe(false); + }); +}); diff --git a/packages/client-runtime/src/pullRequestDiffFold.ts b/packages/client-runtime/src/pullRequestDiffFold.ts new file mode 100644 index 0000000000000..8083c93c25b09 --- /dev/null +++ b/packages/client-runtime/src/pullRequestDiffFold.ts @@ -0,0 +1,25 @@ +export type DiffFoldOverride = "expanded" | "folded" | null; + +/** The reader's own fold choice per file. It is absolute, so a Viewed mark loading later keeps it. */ +export type FileDiffFoldChoices = ReadonlyMap; + +export function isFileDiffCollapsed( + fileKey: string, + foldOverride: DiffFoldOverride, + choices: FileDiffFoldChoices, + viewed = false, +): boolean { + return choices.get(fileKey) ?? (foldOverride === null ? viewed : foldOverride === "folded"); +} + +export function withFileDiffFold( + choices: FileDiffFoldChoices, + fileKey: string, + collapsed: boolean | undefined, +): FileDiffFoldChoices { + if (choices.get(fileKey) === collapsed) return choices; + const next = new Map(choices); + if (collapsed === undefined) next.delete(fileKey); + else next.set(fileKey, collapsed); + return next; +} From d427894aa1d170ce0222fe005723f5e91419916b Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 05:19:49 -0800 Subject: [PATCH 005/253] fix(server): GitLab reads with no project on the host name the host, not the repository (#37638) --- .../pullRequest/PullRequestService.test.ts | 28 +++++++++++++++++++ .../src/pullRequest/PullRequestService.ts | 10 ++++--- packages/contracts/src/pullRequest.ts | 8 ++++++ 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/apps/server/src/pullRequest/PullRequestService.test.ts b/apps/server/src/pullRequest/PullRequestService.test.ts index 1ca1d3885b936..827acef899357 100644 --- a/apps/server/src/pullRequest/PullRequestService.test.ts +++ b/apps/server/src/pullRequest/PullRequestService.test.ts @@ -2419,6 +2419,34 @@ it.effect.each([ }), ); +// regression: #37633 any checkout on the host routes the read, so name the host, not the repository +it.effect("names the host when no project has a checkout on it for a host-routed read", () => + Effect.gen(function* () { + const service = yield* makeService({ + projects: [project({ id: "wrapper", title: "Wrapper", workspaceRoot: "/work" })], + providers: [ + fakePullRequestProvider(SourceControlProviderKind.make("gitlab"), { + getChangeRequestSummary: () => Effect.die("no checkout to read through"), + }), + ], + }); + const error = yield* Effect.flip( + service.summary( + { + projectId: "wrapper" as ProjectId, + host: "gitlab.com", + repository: "acme/web", + number: 7, + }, + { recoverTransientFailure: false }, + ), + ); + assert.strictEqual(error._tag, "PullRequestUnavailableError"); + expect(error.message).not.toContain("acme/web is not a project"); + expect(error.message).toContain("gitlab.com"); + }), +); + it.effect("prefers a matching checkout over a non-repository project's directory", () => Effect.gen(function* () { const seen: string[] = []; diff --git a/apps/server/src/pullRequest/PullRequestService.ts b/apps/server/src/pullRequest/PullRequestService.ts index 92671afb75b58..592bfb92ce7cd 100644 --- a/apps/server/src/pullRequest/PullRequestService.ts +++ b/apps/server/src/pullRequest/PullRequestService.ts @@ -1092,10 +1092,12 @@ export const make = Effect.gen(function* () { !readable.readsPublicHostWithoutCheckout && readable.servesHost?.(host) !== false ) { - return yield* new PullRequestUnavailableError({ - reason: "repository-without-project", - repository, - }); + // Without repository keys, any checkout on the host routes the read. + return yield* new PullRequestUnavailableError( + readable.repositoryKey === undefined + ? { reason: "repository-without-project", host } + : { reason: "repository-without-project", repository }, + ); } return yield* new PullRequestUnavailableError({ reason: "provider-unsupported" }); }); diff --git a/packages/contracts/src/pullRequest.ts b/packages/contracts/src/pullRequest.ts index 4d313c0ceb98d..30517a6960292 100644 --- a/packages/contracts/src/pullRequest.ts +++ b/packages/contracts/src/pullRequest.ts @@ -1002,6 +1002,11 @@ export class PullRequestUnavailableError extends Schema.TaggedError Date: Mon, 16 Nov 2026 17:46:57 +0400 Subject: [PATCH 006/253] fix(server): T3 Connect error 1016 tells you to link again instead of restart (#37636) --- apps/server/src/cloud/ConnectReachability.test.ts | 5 +++-- apps/server/src/cloud/ConnectReachability.ts | 7 +++++++ docs/user/t3-connect-troubleshooting.md | 4 ++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/apps/server/src/cloud/ConnectReachability.test.ts b/apps/server/src/cloud/ConnectReachability.test.ts index 91ef5bdc785d5..fccbfa6c4d578 100644 --- a/apps/server/src/cloud/ConnectReachability.test.ts +++ b/apps/server/src/cloud/ConnectReachability.test.ts @@ -47,7 +47,8 @@ describe("classifyReachability", () => { [{ kind: "status", status: 502 }, "origin_unreachable"], [{ kind: "status", status: 530 }, "no_connector"], [{ kind: "status", status: 530, cloudflareError: "1033" }, "no_connector"], - [{ kind: "status", status: 530, cloudflareError: "1016" }, "unexpected_response"], + [{ kind: "status", status: 530, cloudflareError: "1016" }, "dns_failed"], + [{ kind: "status", status: 530, cloudflareError: "1001" }, "unexpected_response"], [{ kind: "network_error" }, "timeout"], ] as const)("maps endpoint result %o to %s", (endpoint, cause) => { expect(classifyReachability(facts({ endpoint, edgeReachable: true }), checkedAt)).toMatchObject( @@ -209,7 +210,7 @@ describe("ConnectReachability stale connector recovery", () => { expect(yield* reachability.check).toMatchObject({ status: "reachable" }); status = 530; body = { cloudflareError: 1016 }; - expect(yield* reachability.check).toMatchObject({ cause: "unexpected_response" }); + expect(yield* reachability.check).toMatchObject({ cause: "dns_failed" }); yield* TestClock.adjust(Duration.minutes(2)); expect(yield* Queue.size(restarts)).toBe(0); diff --git a/apps/server/src/cloud/ConnectReachability.ts b/apps/server/src/cloud/ConnectReachability.ts index 77e18974dff57..c3ed37f01c91d 100644 --- a/apps/server/src/cloud/ConnectReachability.ts +++ b/apps/server/src/cloud/ConnectReachability.ts @@ -151,6 +151,13 @@ export function classifyReachability( restart, ); } + if (status === 530 && cloudflareError === "1016") { + return fail( + "dns_failed", + "The T3 Connect address no longer points to this machine's tunnel (Cloudflare error 1016). T3 Connect may have removed this environment.", + "Link this environment again. Turn T3 Connect off and on in Settings > Sharing & access, or run `t3 connect link` and then restart T3 Code.", + ); + } if (status !== null) { const code = cloudflareError === undefined ? "" : ` (Cloudflare error ${cloudflareError})`; return fail( diff --git a/docs/user/t3-connect-troubleshooting.md b/docs/user/t3-connect-troubleshooting.md index 2b3b9deaf5ea4..6dadd78be3032 100644 --- a/docs/user/t3-connect-troubleshooting.md +++ b/docs/user/t3-connect-troubleshooting.md @@ -80,6 +80,10 @@ Fix: 3. If the check fails only on the host, check the host's DNS, VPN, or proxy. If it says the host's network has an old cached answer, wait: it usually clears within about 30 minutes. +4. If it says the address no longer points to this machine's tunnel, the + environment was likely removed from T3 Connect. Link it again: on a desktop + host, turn **T3 Connect** off and on in **Settings > Sharing & access**. On a + command-line host, run `t3 connect link`, then restart T3 Code. ## This device can't reach T3 Connect From cd42e1a4973723a2601008d1e7dbeb2fa9a99fb9 Mon Sep 17 00:00:00 2001 From: preet Date: Mon, 16 Nov 2026 19:47:33 +0530 Subject: [PATCH 007/253] fix(web): the PR author filter says "1 merge loaded", not "1 merges loaded" (#37651) Co-authored-by: Julius Marminge --- apps/web/src/components/pullRequest/PullRequestListFilters.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/web/src/components/pullRequest/PullRequestListFilters.tsx b/apps/web/src/components/pullRequest/PullRequestListFilters.tsx index 912e4c6d7ac4d..222afb2b45f27 100644 --- a/apps/web/src/components/pullRequest/PullRequestListFilters.tsx +++ b/apps/web/src/components/pullRequest/PullRequestListFilters.tsx @@ -315,7 +315,7 @@ function PullRequestAuthorChoices({ {option.actor.login} {option.mergedCount > 0 ? ( - {option.mergedCount} merges loaded + {option.mergedCount} {option.mergedCount === 1 ? "merge" : "merges"} loaded ) : null} From 222288b00f9ed392e1e7352ad929c9ae9ac10cef Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 06:19:22 -0800 Subject: [PATCH 008/253] fix(pi): health checks no longer run Pi extensions in the server's folder (#37654) --- packages/provider-pi/src/server/driver.test.ts | 10 +++++++--- packages/provider-pi/src/server/driver.ts | 15 ++++++++++----- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/packages/provider-pi/src/server/driver.test.ts b/packages/provider-pi/src/server/driver.test.ts index 072742c5cd7d8..974ea0b7f0359 100644 --- a/packages/provider-pi/src/server/driver.test.ts +++ b/packages/provider-pi/src/server/driver.test.ts @@ -75,7 +75,7 @@ const makePiSpawner = Effect.fnUntraced(function* ( ? { commands: [ personalSkill, - ...(cwd === "/machine" + ...(cwd?.endsWith("/pi-status-probe") ? [] : [ { @@ -171,9 +171,13 @@ it.layer(layerTest)("PiDriver workspace discovery", (it) => { assert.equal(workspaceLaunch!.options.env?.PI_CODING_AGENT_DIR, "/isolated-pi"); // Discovery processes are stopped as soon as they answer, so neither may start Pi's // network refresh at launch. - for (const cwd of ["/machine", "/first"]) { + assert.isFalse(launches.some((launch) => launch.options.cwd === "/machine")); + for (const isLaunch of [ + (cwd: string | undefined) => cwd?.endsWith("/pi-status-probe") === true, + (cwd: string | undefined) => cwd === "/first", + ]) { const discovery = launches.find( - (launch) => launch.options.cwd === cwd && launch.args.includes("--mode"), + (launch) => isLaunch(launch.options.cwd) && launch.args.includes("--mode"), ); assert.deepEqual( discovery?.args.filter((arg) => arg === "--offline"), diff --git a/packages/provider-pi/src/server/driver.ts b/packages/provider-pi/src/server/driver.ts index 9f32de9d29c4d..325b500a35d94 100644 --- a/packages/provider-pi/src/server/driver.ts +++ b/packages/provider-pi/src/server/driver.ts @@ -97,7 +97,8 @@ export const PiDriver: ProviderDriver = { const httpClient = yield* HttpClient.HttpClient; const latestVersions = yield* ProviderLatestVersions.ProviderLatestVersions; const host = yield* ProviderHost.ProviderHost; - const { cwd } = host.paths; + // Pi runs extensions' session_start in its cwd; the server's cwd is $HOME on packaged desktop. + const probeCwd = pathService.join(host.paths.providerStatusCacheDir, "pi-status-probe"); const processEnv = yield* mergeProviderInstanceEnvironment(environment); const continuationIdentity = defaultProviderContinuationIdentity({ driverKind: DRIVER_KIND, @@ -141,10 +142,14 @@ export const PiDriver: ProviderDriver = { ); const textGeneration = yield* makePiTextGeneration(effectiveConfig, processEnv); - const checkProvider = checkPiProviderStatus(effectiveConfig, processEnv, cwd).pipe( - Effect.map(stampIdentity), - Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), - ); + const checkProvider = fileSystem + .makeDirectory(probeCwd, { recursive: true }) + .pipe( + Effect.ignore, + Effect.andThen(checkPiProviderStatus(effectiveConfig, processEnv, probeCwd)), + Effect.map(stampIdentity), + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), + ); const snapshotSettings = yield* makeProviderSnapshotSettingsSource(effectiveConfig); const snapshot = yield* makeManagedServerProvider>({ From ab8ff33a16c796b48177481adf2167e2aab693fb Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 07:37:00 -0800 Subject: [PATCH 009/253] fix(mobile): Duo scroll views recheck their bleed after a fold (#37571) --- .../ios/T3ScrollEdgeBleedView.swift | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/apps/mobile/modules/t3-native-controls/ios/T3ScrollEdgeBleedView.swift b/apps/mobile/modules/t3-native-controls/ios/T3ScrollEdgeBleedView.swift index 6732f2dae2f18..065eba4331036 100644 --- a/apps/mobile/modules/t3-native-controls/ios/T3ScrollEdgeBleedView.swift +++ b/apps/mobile/modules/t3-native-controls/ios/T3ScrollEdgeBleedView.swift @@ -20,6 +20,10 @@ final class T3ScrollEdgeBleedView: ExpoView { /// React Native's clipping, saved while bleeding. React Native only rewrites it when the /// `overflow` style changes, so it stays off while bleeding. private var containerClipped: Bool? + /// The bar's side is read from the root view controller, whose traits can settle after this + /// view's own during a fold; with no size change after that, nothing else would recheck. + private weak var observedWindow: UIWindow? + private var windowTraitRegistration: (any UITraitChangeRegistration)? required init(appContext: AppContext? = nil) { super.init(appContext: appContext) @@ -38,9 +42,33 @@ final class T3ScrollEdgeBleedView: ExpoView { override func didMoveToWindow() { super.didMoveToWindow() + observeWindowTraits() if window == nil { detach() } else { setNeedsLayout() } } + override func safeAreaInsetsDidChange() { + super.safeAreaInsetsDidChange() + setNeedsLayout() + } + + private func observeWindowTraits() { + guard observedWindow !== window else { return } + if let observedWindow, let windowTraitRegistration { + observedWindow.unregisterForTraitChanges(windowTraitRegistration) + } + observedWindow = window + windowTraitRegistration = nil + #if canImport(UIKit, _version: 9127.0.85) + if #available(iOS 27.1, *), let window { + windowTraitRegistration = window.registerForTraitChanges( + UITraitCollection.systemTraitsAffectingVerticalBarEdge + ) { [weak self] (_: UIWindow, _: UITraitCollection) in + self?.setNeedsLayout() + } + } + #endif + } + override func didAddSubview(_ subview: UIView) { super.didAddSubview(subview) setNeedsLayout() From 59df51f6e19dfcd6624e4d30cb76ebb14de79347 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 07:47:22 -0800 Subject: [PATCH 010/253] perf(mobile): switching back to a thread lays its feed out once (#37578) --- .../src/features/threads/ThreadDetailScreen.tsx | 13 ++++++++----- apps/mobile/src/features/threads/ThreadFeed.tsx | 14 ++++++++++++-- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/apps/mobile/src/features/threads/ThreadDetailScreen.tsx b/apps/mobile/src/features/threads/ThreadDetailScreen.tsx index 25b8894cc8040..425a5cecac599 100644 --- a/apps/mobile/src/features/threads/ThreadDetailScreen.tsx +++ b/apps/mobile/src/features/threads/ThreadDetailScreen.tsx @@ -120,6 +120,7 @@ import { useBarOverlap } from "../../components/ui/useBarInset"; import { useWorkspaceContentWidth, WorkspaceSettledContentWidthContext, + useWorkspaceSettledContentWidth, } from "../layout/workspace-content-width"; import { useAdaptiveWorkspaceLayout } from "../layout/adaptive-workspace-context"; import { useNativeWorkspaceColumnsSupported } from "../../native/NativeWorkspaceColumns"; @@ -328,12 +329,14 @@ export interface ThreadDetailScreenProps { // iOS questions overlay the controls; Android questions stack with child approvals. const ComposerPanels = Platform.OS === "android" ? Animated.ScrollView : Animated.View; -/** Hands the feed the screen width measured for a native column; elsewhere keeps the inherited one. */ +/** + * Hands the feed the screen width measured for a native column; elsewhere keeps the inherited one. + * Always the same element, so the feed below is not remounted when the width arrives. + */ function ColumnWidth(props: { readonly width: number | null; readonly children: React.ReactNode }) { - return props.width === null ? ( - props.children - ) : ( - + const inherited = useWorkspaceSettledContentWidth(); + return ( + {props.children} ); diff --git a/apps/mobile/src/features/threads/ThreadFeed.tsx b/apps/mobile/src/features/threads/ThreadFeed.tsx index d3786eb4932aa..1304fa17932d4 100644 --- a/apps/mobile/src/features/threads/ThreadFeed.tsx +++ b/apps/mobile/src/features/threads/ThreadFeed.tsx @@ -2824,6 +2824,10 @@ const THREAD_FEED_MAINTAIN_SCROLL_AT_END = { on: { dataChange: true, itemLayout: true, layout: true }, } as const; +// A thread switch remounts the feed. Starting from the last split pane's size lays rows out once, +// instead of at width 0 and again when the new feed's own layout reports the same width. +let lastSplitViewport: { windowWidth: number; width: number; height: number } | null = null; + export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { const formatMessageTime = useMessageTimeFormatter(); const { listRef, onIsAtEndChange, onHeaderMaterialVisibilityChange } = props; @@ -2862,10 +2866,15 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { }); const settledPaneWidth = useWorkspaceSettledContentWidth(); const splitLayout = (props.layoutVariant ?? "compact") !== "compact"; - const [measuredViewportWidth, setViewportWidth] = useState(() => (splitLayout ? 0 : windowWidth)); + const [initialSplitViewport] = useState(() => + splitLayout && lastSplitViewport?.windowWidth === windowWidth ? lastSplitViewport : null, + ); + const [measuredViewportWidth, setViewportWidth] = useState(() => + splitLayout ? (initialSplitViewport?.width ?? 0) : windowWidth, + ); const viewportWidth = settledPaneWidth !== null ? Math.round(settledPaneWidth) : measuredViewportWidth; - const [viewportHeight, setViewportHeight] = useState(0); + const [viewportHeight, setViewportHeight] = useState(() => initialSplitViewport?.height ?? 0); const [disclosureLayout, setDisclosureLayout] = useState(null); const disclosureMotionRef = useRef(null); const disclosureMotionTimer = useRef | null>(null); @@ -3567,6 +3576,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { const handleViewportLayout = (event: LayoutChangeEvent) => { const nextWidth = Math.round(event.nativeEvent.layout.width); const nextHeight = Math.round(event.nativeEvent.layout.height); + if (splitLayout) lastSplitViewport = { windowWidth, width: nextWidth, height: nextHeight }; setViewportWidth((current) => (Math.abs(current - nextWidth) > 1 ? nextWidth : current)); setViewportHeight((current) => (Math.abs(current - nextHeight) > 1 ? nextHeight : current)); }; From 1c7d9454393316cd81dc4296b5a25eb66b72bfbf Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 07:48:11 -0800 Subject: [PATCH 011/253] fix(web): typing in a file after a find match stays in the file (#37674) --- .../files/FilePreviewPanel.browser.test.tsx | 11 +++++++++++ patches/@pierre__diffs@1.7.0-beta.4.patch | 16 ++++++++++++---- pnpm-lock.yaml | 10 +++++----- pnpm-workspace.yaml | 4 ++-- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/apps/web/src/components/files/FilePreviewPanel.browser.test.tsx b/apps/web/src/components/files/FilePreviewPanel.browser.test.tsx index 82fcd5fcbca3a..670154a009f6e 100644 --- a/apps/web/src/components/files/FilePreviewPanel.browser.test.tsx +++ b/apps/web/src/components/files/FilePreviewPanel.browser.test.tsx @@ -223,6 +223,17 @@ describe("source file focus", () => { isMacPlatform(navigator.platform) ? "{Meta>}f{/Meta}" : "{Control>}f{/Control}", ); await vi.waitFor(() => expect(deepActiveElement()).toBeInstanceOf(HTMLInputElement)); + + const findField = deepActiveElement() as HTMLInputElement; + await userEvent.keyboard("server"); + findField.blur(); + expect(focus.focus("panel", false)).toBe(true); + await vi.waitFor(editorHasFocus); + const editor = deepActiveElement() as HTMLElement; + await userEvent.keyboard("ab"); + await vi.waitFor(() => expect(editor.textContent).toContain("xabconst server")); + expect(findField.value).toBe("server"); + editorHasFocus(); }); it("focuses the editor when a file row is clicked or activated with Enter", async () => { diff --git a/patches/@pierre__diffs@1.7.0-beta.4.patch b/patches/@pierre__diffs@1.7.0-beta.4.patch index adfe946dab74e..ff75d50800ea5 100644 --- a/patches/@pierre__diffs@1.7.0-beta.4.patch +++ b/patches/@pierre__diffs@1.7.0-beta.4.patch @@ -1,5 +1,5 @@ diff --git a/dist/editor/editor.js b/dist/editor/editor.js -index b4c280390b52434f63fbe7916d81b150d9006fb4..8564980bdfe2ef8e17e8ab0a87627709091101ee 100644 +index b4c280390b52434f63fbe7916d81b150d9006fb4..cb7fb89a22c8adac9ea951b8767b5fd446347746 100644 --- a/dist/editor/editor.js +++ b/dist/editor/editor.js @@ -95,6 +95,7 @@ var Editor = class { @@ -38,7 +38,15 @@ index b4c280390b52434f63fbe7916d81b150d9006fb4..8564980bdfe2ef8e17e8ab0a87627709 const replacementFocusTargetIsContent = (event) => { const target = event.composedPath()[0]; return target instanceof Node && this.#contentElement?.contains(target) === true; -@@ -1397,6 +1412,7 @@ var Editor = class { +@@ -1130,6 +1145,7 @@ var Editor = class { + this.#editorEventDisposes = [ + addEventListener(contentEl, "focus", () => { + this.#contentHasFocus = true; ++ this.#retainSearchPanelFocus = false; + onFocus?.(); + if (!this.#isContentMouseDown && !this.#isComposing && this.#focusRequest == null && this.#selections !== void 0 && this.#selections.length > 0) this.#setWindowSelection(this.#selections.at(-1)); + }, { passive: true }), +@@ -1397,6 +1413,7 @@ var Editor = class { this.#resizeObserver = new ResizeObserver(this.#handleLayoutResize); this.#resizeObserver.observe(contentEl); this.#resizeObserver.observe(contentEl.parentElement); @@ -46,7 +54,7 @@ index b4c280390b52434f63fbe7916d81b150d9006fb4..8564980bdfe2ef8e17e8ab0a87627709 this.#computeContentOffset(contentEl); } #handleCustomPasteEvent = async () => { -@@ -1852,7 +1868,9 @@ var Editor = class { +@@ -1852,7 +1869,9 @@ var Editor = class { const change = textDocument.applyEdits(edits, true, selections, nextSelections, true); if (change !== void 0) this.#applyChange(change, nextSelections); } @@ -57,7 +65,7 @@ index b4c280390b52434f63fbe7916d81b150d9006fb4..8564980bdfe2ef8e17e8ab0a87627709 const lineAnnotations = this.#lineAnnotations?.length ?? 0; const prevGutterWidth = this.#gutterWidthCache; const prevContentWidth = this.#contentWidthCache; -@@ -1860,12 +1878,12 @@ var Editor = class { +@@ -1860,12 +1879,12 @@ var Editor = class { this.#contentWidthCache = void 0; const gutterWidthChanged = this.#getGutterWidth() !== prevGutterWidth; const contentWidthChanged = this.#getContentWidth() !== prevContentWidth; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4b55abacfb80c..5c84d21586059 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -372,7 +372,7 @@ patchedDependencies: '@ff-labs/fff-node@0.11.0': 93f62a76399b435a76588b2bef237db0a1e85880c1ac0d0759c416e9cc579252 '@legendapp/list@3.7.2': 3c68607b8bb7a531873bcbe0b197d163ef1fdb4d4cf52a4ddf9d2b3804671658 '@muse-code/sdk@1.4.6': 7b495b607fc5d76b444c7c4d58821e839726a30e6f3c53564f0d59f2d5b42101 - '@pierre/diffs@1.7.0-beta.4': dd078a7448ce6727aaaa8b8b6702fe0ff02e85f18efa7c441a768530cabbe0cc + '@pierre/diffs@1.7.0-beta.4': 756e7b154f48b9084ea6630520736cf446b69e6000777f3717363836694dd00c '@react-native-ai/apple@0.12.0': 2d09870c2848d185cb05b53ed823a46e12dba519324d8dd8e584e28731990f9d '@react-native-menu/menu@2.0.0': 6872ba71b9847c5a0210b1b896923dadc3860faedfe28d9f062bcedfe786ef1d '@react-navigation/native-stack@7.21.0': 990d575444c4af2ce8218b3b7a8280fe244c03abbdb32b822a2e862f791dbf42 @@ -573,7 +573,7 @@ importers: version: 0.3.0 '@pierre/diffs': specifier: 1.7.0-beta.4 - version: 1.7.0-beta.4(patch_hash=dd078a7448ce6727aaaa8b8b6702fe0ff02e85f18efa7c441a768530cabbe0cc)(@shikijs/themes@4.2.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0) + version: 1.7.0-beta.4(patch_hash=756e7b154f48b9084ea6630520736cf446b69e6000777f3717363836694dd00c)(@shikijs/themes@4.2.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0) '@pierre/highlights': specifier: 'catalog:' version: 0.5.0 @@ -1094,7 +1094,7 @@ importers: version: 1.8.0 '@pierre/diffs': specifier: 1.7.0-beta.4 - version: 1.7.0-beta.4(patch_hash=dd078a7448ce6727aaaa8b8b6702fe0ff02e85f18efa7c441a768530cabbe0cc)(@shikijs/themes@4.2.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0) + version: 1.7.0-beta.4(patch_hash=756e7b154f48b9084ea6630520736cf446b69e6000777f3717363836694dd00c)(@shikijs/themes@4.2.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0) '@pierre/trees': specifier: 1.0.0-beta.6 version: 1.0.0-beta.6(@pierre/theme@2.0.0)(@shikijs/themes@4.2.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(shiki@4.2.0) @@ -1510,7 +1510,7 @@ importers: dependencies: '@pierre/diffs': specifier: 1.7.0-beta.4 - version: 1.7.0-beta.4(patch_hash=dd078a7448ce6727aaaa8b8b6702fe0ff02e85f18efa7c441a768530cabbe0cc)(@shikijs/themes@4.2.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0) + version: 1.7.0-beta.4(patch_hash=756e7b154f48b9084ea6630520736cf446b69e6000777f3717363836694dd00c)(@shikijs/themes@4.2.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0) '@t3tools/contracts': specifier: workspace:* version: link:../contracts @@ -16440,7 +16440,7 @@ snapshots: tslib: 2.8.1 webcrypto-core: 1.9.2 - '@pierre/diffs@1.7.0-beta.4(patch_hash=dd078a7448ce6727aaaa8b8b6702fe0ff02e85f18efa7c441a768530cabbe0cc)(@shikijs/themes@4.2.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)': + '@pierre/diffs@1.7.0-beta.4(patch_hash=756e7b154f48b9084ea6630520736cf446b69e6000777f3717363836694dd00c)(@shikijs/themes@4.2.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)': dependencies: '@pierre/highlights': 0.5.1 '@pierre/patches': 0.0.1 diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 7d1064ffc9716..5c66aef02dda2 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -248,8 +248,8 @@ patchedDependencies: # scroll target a Material bottom sheet picks, so sheet content can sit below its header. "@expo/ui@58.0.27": patches/@expo__ui@58.0.27.patch "@ff-labs/fff-node@0.11.0": patches/@ff-labs__fff-node@0.11.0.patch - # Exports the dependency-light subpaths mobile and the web diff parser import, and re-measures - # editor carets when the code font changes. + # Exports the dependency-light subpaths mobile and the web diff parser import, re-measures + # editor carets when the code font changes, and stops the find bar reclaiming focus from the file. "@pierre/diffs@1.7.0-beta.4": patches/@pierre__diffs@1.7.0-beta.4.patch "@react-native-ai/apple@0.12.0": patches/@react-native-ai__apple@0.12.0.patch "@react-native-menu/menu@2.0.0": patches/@react-native-menu__menu@2.0.0.patch From 023620d3a51e815d49723d514ce7cae3a1a5a26f Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 07:48:25 -0800 Subject: [PATCH 012/253] fix(server): Codex health checks no longer kill ssh mid SSH-agent request (#37677) --- apps/server/src/provider/CodexProvider.ts | 42 +++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/apps/server/src/provider/CodexProvider.ts b/apps/server/src/provider/CodexProvider.ts index 7dcf9079f82b3..63154f4e99499 100644 --- a/apps/server/src/provider/CodexProvider.ts +++ b/apps/server/src/provider/CodexProvider.ts @@ -4,6 +4,7 @@ import * as DateTime from "effect/DateTime"; import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; import * as Equal from "effect/Equal"; +import * as Exit from "effect/Exit"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; @@ -676,9 +677,31 @@ export const withCodexAppServerClient = Effect.fn("withCodexAppServerClient")(fu ); const initialize = yield* client.request("initialize", buildCodexInitializeParams()); yield* client.notify("initialized", undefined); - return { client, initialize }; + return { client, initialize, pid: child.pid }; }); +// Codex runs git (ssh for ssh URLs) for plugin marketplaces at launch and exits without waiting +// for it, so stopping a short probe mid-fetch strands ssh in an SSH-agent request. +const CODEX_STARTUP_WORK_GRACE = Duration.minutes(1); + +const codexProcessGroupIsBusy = Effect.fn("codexProcessGroupIsBusy")( + function* (pid: number) { + const { stdout } = yield* spawnAndCollect( + "pgrep", + ChildProcess.make("pgrep", ["-g", `${pid}`]), + ); + return stdout.split("\n").some((line) => line.trim() !== "" && Number(line) !== pid); + }, + Effect.orElseSucceed(() => false), +); + +const stopCodexAfterStartupWork = (pid: number, processScope: Scope.Closeable) => + codexProcessGroupIsBusy(pid).pipe( + Effect.repeat({ schedule: Schedule.spaced("500 millis"), while: (busy) => busy }), + Effect.timeoutOption(CODEX_STARTUP_WORK_GRACE), + Effect.ensuring(Scope.close(processScope, Exit.void)), + ); + // Usage is an enrichment: a failure or a slow answer degrades to "no usage this // probe" rather than costing the account and models. const readCodexRateLimits = ( @@ -713,7 +736,22 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun readonly skipNativeUsage?: boolean; }) { const spawnedAtMs = DateTime.toEpochMillis(yield* DateTime.now); - const { client, initialize } = yield* withCodexAppServerClient(input); + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; + const processScope = yield* Scope.make(); + let pid: number | undefined; + // Returning the snapshot must not wait for Codex's own startup work. + yield* Effect.addFinalizer(() => + pid === undefined + ? Scope.close(processScope, Exit.void) + : stopCodexAfterStartupWork(pid, processScope).pipe( + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), + Effect.forkDetach, + Effect.asVoid, + ), + ); + const opened = yield* withCodexAppServerClient(input).pipe(Scope.provide(processScope)); + pid = opened.pid; + const { client, initialize } = opened; const versionMatch = initialize.userAgent.match(/\/([^\s]+)/); const version = versionMatch ? versionMatch[1] : undefined; From 5a20d59db38abef8e0b98cfe47fed81d074bcecc Mon Sep 17 00:00:00 2001 From: Dieter Date: Mon, 16 Nov 2026 23:48:40 +0800 Subject: [PATCH 013/253] fix(shell): Windows PATH probe reads User and Machine registry (#37632) Co-authored-by: Julius Marminge --- .../src/shell/DesktopShellEnvironment.ts | 3 +- apps/server/src/os-jank.test.ts | 19 +++- apps/server/src/os-jank.ts | 16 ++++ apps/server/src/policy/ManagedPolicy.test.ts | 6 +- apps/server/src/policy/managedProviders.ts | 17 ++++ .../src/provider/ProviderInstanceRegistry.ts | 10 +++ .../ProviderInstanceRegistryHydration.test.ts | 1 + .../ProviderInstanceRegistryMutator.ts | 2 + .../src/provider/ProviderRegistry.test.ts | 87 +++++++++++++++++++ apps/server/src/provider/ProviderRegistry.ts | 29 +++++-- packages/shared/src/shell.test.ts | 13 +++ packages/shared/src/shell.ts | 16 +++- 12 files changed, 210 insertions(+), 9 deletions(-) diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.ts b/apps/desktop/src/shell/DesktopShellEnvironment.ts index 351150aa4b987..f2a92069f066b 100644 --- a/apps/desktop/src/shell/DesktopShellEnvironment.ts +++ b/apps/desktop/src/shell/DesktopShellEnvironment.ts @@ -6,6 +6,7 @@ import { mergePathValues, readUserLoginShell, setWindowsPath, + windowsEnvironmentValueCommand, } from "@t3tools/shared/shell"; import * as Context from "effect/Context"; import * as Duration from "effect/Duration"; @@ -232,7 +233,7 @@ const captureWindowsEnvironmentCommand = (names: ReadonlyArray) => ...names.flatMap((name) => { return [ `Write-Output '${startMarker(name)}'`, - `$value = [Environment]::GetEnvironmentVariable('${name}')`, + windowsEnvironmentValueCommand(name), "if ($null -ne $value -and $value.Length -gt 0) { Write-Output $value }", `Write-Output '${endMarker(name)}'`, ]; diff --git a/apps/server/src/os-jank.test.ts b/apps/server/src/os-jank.test.ts index 346c711ba6346..857adb2c8e5c3 100644 --- a/apps/server/src/os-jank.test.ts +++ b/apps/server/src/os-jank.test.ts @@ -8,7 +8,7 @@ import * as NodeChildProcess from "node:child_process"; import * as NodeOS from "node:os"; import { assert, it, vi } from "vite-plus/test"; -import { fixPath, hydratePosixHome } from "./os-jank.ts"; +import { fixPath, hydratePosixHome, refreshWindowsPath } from "./os-jank.ts"; vi.mock("node:child_process", { spy: true }); @@ -204,3 +204,20 @@ effectIt.effect("replaces a Path-spelled Windows PATH instead of adding a second assert.deepEqual(env, { PATH: "C:\\Tools;C:\\Windows" }); }), ); + +effectIt.effect("adds a directory an installer wrote to the registry after launch", () => + Effect.gen(function* () { + const env: NodeJS.ProcessEnv = { Path: "C:\\Tools;C:\\Windows" }; + const installed = "C:\\Users\\me\\AppData\\Local\\cursor-agent"; + + yield* refreshWindowsPath().pipe( + Effect.provideService(HostProcess.Platform, "win32"), + Effect.provideService(HostProcess.Environment, env), + Effect.provideService(WindowsShellEnvironment, () => + Effect.succeed({ PATH: `C:\\Windows;${installed}` }), + ), + ); + + assert.deepEqual(env, { PATH: `C:\\Tools;C:\\Windows;${installed}` }); + }), +); diff --git a/apps/server/src/os-jank.ts b/apps/server/src/os-jank.ts index d28d3f80261f4..bc6528f91a5be 100644 --- a/apps/server/src/os-jank.ts +++ b/apps/server/src/os-jank.ts @@ -4,10 +4,12 @@ import { extractEnvironmentValues, listLoginShellCandidates, mergePathEntries, + mergePathValues, readEnvironmentFromLoginShell, readPathFromLaunchctl, resolveWindowsEnvironment, setWindowsPath, + WindowsShellEnvironment, } from "@t3tools/shared/shell"; import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; @@ -217,6 +219,20 @@ export const refreshPathFromLoginShell = Effect.fn("refreshPathFromLoginShell")( } }); +// Installers write PATH to the registry, which the server's launch-time PATH never sees again. +// Returns whether PATH gained a directory. +export const refreshWindowsPath = Effect.fn("refreshWindowsPath")(function* () { + if ((yield* HostProcess.Platform) !== "win32") return false; + const env = yield* HostProcess.Environment; + const readEnvironment = yield* WindowsShellEnvironment; + const { PATH: registryPath } = yield* readEnvironment(["PATH"], { loadProfile: false }); + const currentPath = env.PATH ?? env.Path ?? env.path; + const mergedPath = mergePathValues(currentPath, registryPath, "win32"); + if (!mergedPath || mergedPath === currentPath) return false; + setWindowsPath(env, mergedPath); + return true; +}); + export const expandHomePath = Effect.fn(function* (input: string) { const { join } = yield* Path.Path; const home = yield* HostProcess.HomeDirectory; diff --git a/apps/server/src/policy/ManagedPolicy.test.ts b/apps/server/src/policy/ManagedPolicy.test.ts index d6c7710b57ead..a91cdec613095 100644 --- a/apps/server/src/policy/ManagedPolicy.test.ts +++ b/apps/server/src/policy/ManagedPolicy.test.ts @@ -193,7 +193,11 @@ it.layer(NodeServices.layer)("ManagedPolicy.load", (it) => { ]); // The host variable that replaces Codex's launch arguments is ignored too. const host = { PATH: "/usr/bin", T3CODE_CODEX_LAUNCH_ARGS: "-c mcp_servers.x.command=sh" }; - assert.deepEqual(providerHostEnvironment(policy.summary, host), { PATH: "/usr/bin" }); + const managedHost = providerHostEnvironment(policy.summary, host); + assert.deepEqual(managedHost, { PATH: "/usr/bin" }); + // A PATH refresh after launch reaches drivers that hold the managed copy. + host.PATH = "/opt/new/bin:/usr/bin"; + assert.strictEqual(managedHost.PATH, "/opt/new/bin:/usr/bin"); assert.strictEqual(providerHostEnvironment(undefined, host), host); }), ); diff --git a/apps/server/src/policy/managedProviders.ts b/apps/server/src/policy/managedProviders.ts index 059f6aeb4b782..1996aa2e11646 100644 --- a/apps/server/src/policy/managedProviders.ts +++ b/apps/server/src/policy/managedProviders.ts @@ -168,6 +168,23 @@ export const providerHostEnvironment = ( delete next[name]; } } + // Drivers keep this object, so PATH reads through to the host for a refresh after launch to reach them. + const pathKeys = Object.keys(next).filter((name) => name.toUpperCase() === "PATH"); + if (pathKeys.length > 0) { + for (const name of pathKeys) delete next[name]; + Object.defineProperty(next, "PATH", { + enumerable: true, + configurable: true, + get: () => environment.PATH ?? environment.Path ?? environment.path, + set: (value) => + Object.defineProperty(next, "PATH", { + value, + enumerable: true, + writable: true, + configurable: true, + }), + }); + } return next; }; diff --git a/apps/server/src/provider/ProviderInstanceRegistry.ts b/apps/server/src/provider/ProviderInstanceRegistry.ts index ae51e8009645c..9ab3f56aedcd5 100644 --- a/apps/server/src/provider/ProviderInstanceRegistry.ts +++ b/apps/server/src/provider/ProviderInstanceRegistry.ts @@ -637,6 +637,15 @@ export const makeProviderInstanceRegistry = Effect.fn( (rawInstanceId) => instanceId === undefined || rawInstanceId === instanceId, undefined, ).pipe(Effect.asVoid); + const rebuild: ProviderInstanceRegistryMutator.ProviderInstanceRegistryMutatorShape["rebuild"] = ( + instanceIds, + ) => + semaphore.withPermits(1)( + Ref.get(configMapRef).pipe( + Effect.flatMap((configMap) => reconcileWithR(configMap, new Set(instanceIds))), + Effect.provideContext(driverContext), + ), + ); const applySecret: ProviderInstanceRegistryMutator.ProviderInstanceRegistryMutatorShape["applySecret"] = (reference, value) => reloadWhere( @@ -697,6 +706,7 @@ export const makeProviderInstanceRegistry = Effect.fn( const mutator: ProviderInstanceRegistryMutator.ProviderInstanceRegistryMutatorShape = { reconcile, reloadSecrets, + rebuild, applySecret, }; diff --git a/apps/server/src/provider/ProviderInstanceRegistryHydration.test.ts b/apps/server/src/provider/ProviderInstanceRegistryHydration.test.ts index 02bc92f4b4504..a9de2878a39c2 100644 --- a/apps/server/src/provider/ProviderInstanceRegistryHydration.test.ts +++ b/apps/server/src/provider/ProviderInstanceRegistryHydration.test.ts @@ -165,6 +165,7 @@ describe("layerSettingsWatcher", () => { const mutator = Layer.succeed(ProviderInstanceRegistryMutator, { reconcile: () => Queue.offer(reconciled, undefined), reloadSecrets: () => Effect.sync(() => void reloads++), + rebuild: () => Effect.void, applySecret: () => Effect.succeed([]), }); yield* Layer.build( diff --git a/apps/server/src/provider/ProviderInstanceRegistryMutator.ts b/apps/server/src/provider/ProviderInstanceRegistryMutator.ts index 911a055d79625..a7cb5be09378a 100644 --- a/apps/server/src/provider/ProviderInstanceRegistryMutator.ts +++ b/apps/server/src/provider/ProviderInstanceRegistryMutator.ts @@ -11,6 +11,8 @@ export interface ProviderInstanceRegistryMutatorShape { readonly reconcile: (configMap: ProviderInstanceConfigMap) => Effect.Effect; /** Re-reads the secrets of every instance, or one, and rebuilds those whose secrets changed. */ readonly reloadSecrets: (instanceId?: ProviderInstanceId) => Effect.Effect; + /** Rebuilds these live instances from their current configuration and the host environment. */ + readonly rebuild: (instanceIds: ReadonlyArray) => Effect.Effect; /** * Rebuilds the instances that read `reference` with `value`, keeping the other secrets a live * instance holds, and returns them. A parked instance reads its other secrets from the resolver. diff --git a/apps/server/src/provider/ProviderRegistry.test.ts b/apps/server/src/provider/ProviderRegistry.test.ts index 2ea6d98c083c5..021c3ddb57039 100644 --- a/apps/server/src/provider/ProviderRegistry.test.ts +++ b/apps/server/src/provider/ProviderRegistry.test.ts @@ -43,6 +43,7 @@ import { import { HttpClient, HttpClientResponse } from "effect/http"; import { ChildProcessSpawner } from "effect/process"; import * as HostProcess from "@t3tools/shared/HostProcess"; +import { WindowsShellEnvironment } from "@t3tools/shared/shell"; import { deepMerge } from "@t3tools/shared/Struct"; import { createModelCapabilities } from "@t3tools/shared/model"; import { applyServerSettingsPatch } from "@t3tools/shared/serverSettings"; @@ -2425,6 +2426,92 @@ it.layer( }), ); + it.effect( + "rebuilds only instances missing their CLI when a refresh finds a new Windows PATH", + () => { + const env: NodeJS.ProcessEnv = { PATH: "C:\\Windows" }; + return Effect.gen(function* () { + const driver = ProviderDriverKind.make("codex"); + const makeInstance = (id: string, installed: boolean): ProviderInstance => { + const instanceId = ProviderInstanceId.make(id); + const provider = { + instanceId, + driver, + status: installed ? "ready" : "error", + enabled: true, + installed, + auth: { status: "unknown" }, + checkedAt: "1970-01-01T00:00:00.000Z", + version: null, + models: [], + slashCommands: [], + skills: [], + } as const satisfies ServerProvider; + return { + instanceId, + driverKind: driver, + continuationIdentity: { driverKind: driver, continuationKey: `codex:${id}` }, + displayName: undefined, + enabled: true, + snapshot: { + resolveMaintenance: () => + Effect.succeed( + makeManualOnlyProviderMaintenanceCapabilities({ + provider: driver, + packageName: null, + }), + ), + getSnapshot: Effect.succeed(provider), + refresh: Effect.succeed(provider), + streamChanges: Stream.empty, + applyUsageLimits: () => Effect.void, + }, + orchestrationAdapter: {} as ProviderInstance["orchestrationAdapter"], + textGeneration: {} as ProviderInstance["textGeneration"], + }; + }; + const instances = [makeInstance("present", true), makeInstance("missing", false)]; + const rebuilt: Array> = []; + const changes = yield* PubSub.unbounded(); + const registry = yield* ProviderRegistry.ProviderRegistry.pipe( + Effect.provide( + ProviderRegistry.layer.pipe( + Layer.provide( + Layer.mock(ProviderInstanceRegistryMutator.ProviderInstanceRegistryMutator)({ + rebuild: (ids) => Effect.sync(() => void rebuilt.push(ids)), + }), + ), + Layer.provideMerge( + Layer.succeed(ProviderInstanceRegistry.ProviderInstanceRegistry, { + getInstance: (id) => Effect.succeed(instances.find((i) => i.instanceId === id)), + listInstances: Effect.succeed(instances), + listUnavailable: Effect.succeed([]), + streamChanges: Stream.fromPubSub(changes), + subscribeChanges: PubSub.subscribe(changes), + }), + ), + Layer.provideMerge( + ServerConfig.layerTest(process.cwd(), { prefix: "t3-provider-registry-path-" }), + ), + Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), + ), + ), + ); + + yield* registry.refresh(); + + assert.strictEqual(env.PATH, "C:\\Windows;C:\\Tools"); + assert.deepStrictEqual(rebuilt, [[ProviderInstanceId.make("missing")]]); + }).pipe( + Effect.provideService(HostProcess.Platform, "win32"), + Effect.provideService(HostProcess.Environment, env), + Effect.provideService(WindowsShellEnvironment, () => + Effect.succeed({ PATH: "C:\\Windows;C:\\Tools" }), + ), + ); + }, + ); + it.effect("rescans a requested cwd whose snapshot is still current", () => Effect.gen(function* () { const h = yield* makeWorkspaceRescanHarness; diff --git a/apps/server/src/provider/ProviderRegistry.ts b/apps/server/src/provider/ProviderRegistry.ts index f23058f1b88c5..ed51584b070e4 100644 --- a/apps/server/src/provider/ProviderRegistry.ts +++ b/apps/server/src/provider/ProviderRegistry.ts @@ -11,6 +11,7 @@ import { import * as Cause from "effect/Cause"; import * as Context from "effect/Context"; import * as DateTime from "effect/DateTime"; +import * as Duration from "effect/Duration"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; import * as Equal from "effect/Equal"; @@ -30,6 +31,7 @@ import * as ProviderLatestVersions from "@t3tools/provider-core/server/ProviderL import * as ModelManifest from "./ModelManifest.ts"; import { applyProviderCompatibility } from "./providerCompatibility.ts"; import * as ServerConfig from "../config.ts"; +import { refreshWindowsPath } from "../os-jank.ts"; import * as ProviderInstanceRegistry from "./ProviderInstanceRegistry.ts"; import * as ProviderInstanceRegistryMutator from "./ProviderInstanceRegistryMutator.ts"; import { @@ -699,6 +701,25 @@ export const layer = Layer.effect( }); type RefreshAllCompletion = Deferred.Deferred>; + // A refresh is how a user asks T3 to see a CLI installed since launch; a burst shares one probe. + // Drivers copy the environment when built, so instances still missing their CLI are rebuilt + // on the new PATH. They cannot have live sessions, and installed ones keep theirs. + const refreshHostPath = yield* refreshWindowsPath().pipe( + Effect.flatMap((changed) => + changed + ? Ref.get(providersRef).pipe( + Effect.flatMap((providers) => + instanceRegistryMutator.rebuild( + providers + .filter((provider) => provider.enabled && !provider.installed) + .map((provider) => provider.instanceId), + ), + ), + ) + : Effect.void, + ), + Effect.cachedWithTTL(Duration.seconds(10)), + ); const refreshAllInFlightRef = yield* Ref.make>( Option.none(), ); @@ -707,6 +728,7 @@ export const layer = Layer.effect( const probedManifestUpdatedAt = yield* Ref.make(initialManifest.updatedAt); const refreshAll = Effect.fn("refreshAll")(function* () { + yield* refreshHostPath; const claimed = yield* Ref.modify( refreshAllInFlightRef, ( @@ -754,16 +776,13 @@ export const layer = Layer.effect( if (provider === undefined) { return yield* refreshAll(); } - const instance = (yield* Ref.get(liveSubsRef)).get(defaultInstanceIdForDriver(provider)); - if (!instance) { - return yield* Ref.get(providersRef); - } - return yield* refreshOneSource(instance); + return yield* refreshInstance(defaultInstanceIdForDriver(provider)); }); const refreshInstance = Effect.fn("refreshInstance")(function* ( instanceId: ProviderInstanceId, ) { + yield* refreshHostPath; yield* syncLiveSourcesAndContinue; const instance = (yield* Ref.get(liveSubsRef)).get(instanceId); if (!instance) { diff --git a/packages/shared/src/shell.test.ts b/packages/shared/src/shell.test.ts index 1c2a2c8cf4f25..ac5c59c752379 100644 --- a/packages/shared/src/shell.test.ts +++ b/packages/shared/src/shell.test.ts @@ -35,6 +35,7 @@ import { resolveSpawnCommand, resolveWindowsEnvironment, runsGitWithoutConsole, + windowsEnvironmentValueCommand, SpawnExecutableResolution, WindowsShellEnvironment, withPathDirectoryListings, @@ -257,6 +258,18 @@ describe("mergePathEntries", () => { }); }); +describe("windowsEnvironmentValueCommand", () => { + it("joins PATH as Process, then Machine, then User so prepends win and User installs still appear", () => { + const command = windowsEnvironmentValueCommand("PATH"); + const processAt = command.indexOf("[Environment]::GetEnvironmentVariable('PATH','Process')"); + const machineAt = command.indexOf("[Environment]::GetEnvironmentVariable('PATH','Machine')"); + const userAt = command.indexOf("[Environment]::GetEnvironmentVariable('PATH','User')"); + expect(processAt).toBeGreaterThan(-1); + expect(processAt).toBeLessThan(machineAt); + expect(machineAt).toBeLessThan(userAt); + }); +}); + describe("readEnvironmentFromWindowsShell", () => { effectIt.effect("yields while the native shell probe is pending", () => Effect.gen(function* () { diff --git a/packages/shared/src/shell.ts b/packages/shared/src/shell.ts index e5833d097029b..fd881c5fcd830 100644 --- a/packages/shared/src/shell.ts +++ b/packages/shared/src/shell.ts @@ -278,6 +278,20 @@ export function buildEnvironmentCaptureCommand( : command; } +// Process first so profile prepends win; User and Machine add dirs missing from the launch snapshot. +export function windowsEnvironmentValueCommand(name: string): string { + if (name === "PATH") { + return ( + "$value = (@(" + + "[Environment]::GetEnvironmentVariable('PATH','Process')," + + "[Environment]::GetEnvironmentVariable('PATH','Machine')," + + "[Environment]::GetEnvironmentVariable('PATH','User')" + + ") | Where-Object { $_ }) -join ';'" + ); + } + return `$value = [Environment]::GetEnvironmentVariable('${name}')`; +} + function buildWindowsEnvironmentCaptureCommand(names: ReadonlyArray): string { return [ "$ErrorActionPreference = 'Stop'", @@ -288,7 +302,7 @@ function buildWindowsEnvironmentCaptureCommand(names: ReadonlyArray): st return [ `Write-Output '${envCaptureStart(name)}'`, - `$value = [Environment]::GetEnvironmentVariable('${name}')`, + windowsEnvironmentValueCommand(name), "if ($null -ne $value -and $value.Length -gt 0) { Write-Output $value }", `Write-Output '${envCaptureEnd(name)}'`, ]; From 83e3c4f60da114220eac8d0b54c238e33defbab3 Mon Sep 17 00:00:00 2001 From: Elias <69803556+elias1218@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:09:28 -0500 Subject: [PATCH 014/253] fix(web): published themes keep their CSS when a saved theme shares the id (#37493) Co-authored-by: Julius Marminge --- .../ThemeImportDialog.browser.test.tsx | 20 +++++--- .../components/settings/ThemeImportDialog.tsx | 32 ++++++------ .../theme-keep-both-copy-keeps-css.e2e.ts | 50 +++++++++++++++++++ 3 files changed, 79 insertions(+), 23 deletions(-) create mode 100644 scripts/scenarios/theme-keep-both-copy-keeps-css.e2e.ts diff --git a/apps/web/src/components/settings/ThemeImportDialog.browser.test.tsx b/apps/web/src/components/settings/ThemeImportDialog.browser.test.tsx index acaca34e332b6..0cde5b8f1f477 100644 --- a/apps/web/src/components/settings/ThemeImportDialog.browser.test.tsx +++ b/apps/web/src/components/settings/ThemeImportDialog.browser.test.tsx @@ -65,15 +65,20 @@ describe("ThemeImportDialog", () => { }); }); -const theme = (name: string, accent = "#3b82f6") => ({ +const theme = (name: string, accent = "#3b82f6", css?: string) => ({ version: THEME_FILE_VERSION, name, appearance: "dark", colors: { accent }, + ...(css ? { css } : {}), }); -const themeFile = (name: string, accent?: string) => - new File([JSON.stringify(theme(name, accent))], `${name}.json`, { type: "application/json" }); +const themeFile = (name: string, accent?: string, css?: string) => + new File([JSON.stringify(theme(name, accent, css))], `${name}.json`, { + type: "application/json", + }); + +const DUSK_CSS = ".chat-header { height: 3rem; }"; it.each(["Update existing", "Keep both"])( "keeps batch failures visible after %s", @@ -97,7 +102,7 @@ it.each(["Update existing", "Keep both"])( const input = document.querySelector('input[type="file"]')!; await userEvent.upload(input, [ themeFile("Dawn"), - themeFile("Dusk", "#f97316"), + themeFile("Dusk", "#f97316", DUSK_CSS), new File(["{"], "broken.json", { type: "application/json" }), ]); @@ -107,9 +112,10 @@ it.each(["Update existing", "Keep both"])( await page.getByRole("button", { name: choice }).click(); invalidateCustomThemes(); const label = choice === "Update existing" ? "Dusk" : "Dusk (1)"; - expect(getCustomThemes().find((theme) => theme.label === label)?.colors).toEqual( - parseThemeFile(theme("Dusk", "#f97316")).colors, - ); + expect(getCustomThemes().find((theme) => theme.label === label)).toMatchObject({ + colors: parseThemeFile(theme("Dusk", "#f97316")).colors, + css: DUSK_CSS, + }); await expect.element(page.getByText(/^broken\.json:/)).toBeVisible(); expect(onOpenChange).not.toHaveBeenCalledWith(false); }, diff --git a/apps/web/src/components/settings/ThemeImportDialog.tsx b/apps/web/src/components/settings/ThemeImportDialog.tsx index dc093d55b65c9..17fb7b43daae0 100644 --- a/apps/web/src/components/settings/ThemeImportDialog.tsx +++ b/apps/web/src/components/settings/ThemeImportDialog.tsx @@ -39,6 +39,10 @@ function describeOversizedThemeFile(bytes: number): string | null { return `That file is ${formatBytes(bytes)}. Theme files are only a few KB, so this one was not read (limit ${formatBytes(MAX_THEME_FILE_BYTES)}).`; } +function isThemeIdTaken(themeId: string): boolean { + return getCustomThemes().some((existing) => existing.id === themeId); +} + function escapeJsonHtml(value: string): string { return value.replace( /[&<>"']/g, @@ -264,7 +268,7 @@ function ThemeImportBody({ const installed: ThemeDefinition[] = []; const conflicting: ThemeDefinition[] = []; for (const theme of pairVsCodeThemes(resolveThemeLabelCollisions(parsed))) { - if (getCustomThemes().some((existing) => existing.id === theme.id)) { + if (isThemeIdTaken(theme.id)) { conflicting.push(theme); continue; } @@ -325,28 +329,24 @@ function ThemeImportBody({ theme: ThemeDefinition, preferredName?: string | null, ): ThemeDefinition => { - if (preferredName && preferredName.toLowerCase() !== theme.label.toLowerCase()) { - const candidate = parseThemeFile({ + // The collection stays behind: updating a collection replaces every theme in it. + const copyNamed = (name: string) => + parseThemeFile({ version: THEME_FILE_VERSION, - name: preferredName.slice(0, 48), + name, appearance: theme.appearance, colors: theme.colors, ...(theme.variants ? { variants: theme.variants } : {}), ...(theme.managed ? { managed: true } : {}), + ...(theme.css ? { css: theme.css } : {}), }); - if (!getCustomThemes().some((existing) => existing.id === candidate.id)) return candidate; + if (preferredName && preferredName.toLowerCase() !== theme.label.toLowerCase()) { + const candidate = copyNamed(preferredName.slice(0, 48)); + if (!isThemeIdTaken(candidate.id)) return candidate; } for (let copy = 1; copy < 100; copy += 1) { - const candidate = parseThemeFile({ - version: THEME_FILE_VERSION, - name: `${theme.label.slice(0, 48 - ` (${copy})`.length)} (${copy})`, - appearance: theme.appearance, - colors: theme.colors, - ...(theme.variants ? { variants: theme.variants } : {}), - ...(theme.managed ? { managed: true } : {}), - }); - if (getCustomThemes().some((existing) => existing.id === candidate.id)) continue; - return candidate; + const candidate = copyNamed(`${theme.label.slice(0, 48 - ` (${copy})`.length)} (${copy})`); + if (!isThemeIdTaken(candidate.id)) return candidate; } throw new Error(`Too many copies of "${theme.label}".`); }; @@ -391,7 +391,7 @@ function ThemeImportBody({ } try { const theme = parseThemeImport(json); - if (getCustomThemes().some((existing) => existing.id === theme.id)) { + if (isThemeIdTaken(theme.id)) { setError(null); setConflicts([theme]); return; diff --git a/scripts/scenarios/theme-keep-both-copy-keeps-css.e2e.ts b/scripts/scenarios/theme-keep-both-copy-keeps-css.e2e.ts new file mode 100644 index 0000000000000..8d921d67433c2 --- /dev/null +++ b/scripts/scenarios/theme-keep-both-copy-keeps-css.e2e.ts @@ -0,0 +1,50 @@ +import { expect } from "e2e"; + +import { test, WEB } from "./t3.ts"; + +const themeJson = (css?: string) => + JSON.stringify({ + version: 1, + name: "Nyx", + // The scenario browser is in light mode, and a one-appearance theme only fills that half. + appearance: "light", + colors: { canvas: "#f4f1ff", accent: "#6d5ae6" }, + ...(css ? { css } : {}), + }); + +const NYX_CSS = + ":root { --keep-both-marker: applied; } [data-settings-page-scroll] { outline: 8px solid #ff2d95; outline-offset: -8px; }"; + +test( + "Keep both on a theme import conflict keeps the imported theme's CSS", + WEB, + async ({ t3, app, screen, browser }) => { + await t3.project("demo"); + await t3.open("/settings/appearance"); + + const importTheme = async (json: string) => { + await screen.getByRole("button", "Add theme", { exact: true }).tap(); + const dialog = screen.getByRole("dialog", "Add a theme"); + await dialog.getByRole("textbox", "Theme JSON").fill(json); + await dialog.getByRole("button", "Add theme", { exact: true }).tap(); + return dialog; + }; + + await expect(await importTheme(themeJson())).toBeHidden(); + + const dialog = await importTheme(themeJson(NYX_CSS)); + await expect(dialog.getByText("Already installed")).toBeVisible(); + await dialog.getByRole("button", "Keep both").tap(); + await expect(dialog).toBeHidden(); + + await screen.getByRole("button", "Use Nyx (1) theme").tap(); + await app.screenshot("keep-both-copy-selected"); + await expect + .poll(() => + browser.evaluate(() => + getComputedStyle(document.documentElement).getPropertyValue("--keep-both-marker").trim(), + ), + ) + .toBe("applied"); + }, +); From 1ae5590a329b14f9e35a94c02faf405f6c67a56e Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 08:18:20 -0800 Subject: [PATCH 015/253] fix(web): find in thread no longer announces streamed count refreshes (#37204) --- .../web/src/components/chat/ThreadFindBar.tsx | 30 +++++++++-- .../src/components/chat/threadFind.test.ts | 53 +++++++++++++++++++ apps/web/src/components/chat/threadFind.ts | 25 +++++++++ apps/web/src/components/chat/useThreadFind.ts | 3 ++ 4 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 apps/web/src/components/chat/threadFind.test.ts diff --git a/apps/web/src/components/chat/ThreadFindBar.tsx b/apps/web/src/components/chat/ThreadFindBar.tsx index 466cd929a7580..5ef0485ff4f50 100644 --- a/apps/web/src/components/chat/ThreadFindBar.tsx +++ b/apps/web/src/components/chat/ThreadFindBar.tsx @@ -1,9 +1,13 @@ -import { useEffect, useRef, type KeyboardEvent } from "react"; +import { useEffect, useRef, useState, type KeyboardEvent } from "react"; import { ChevronDownIcon, ChevronUpIcon, XIcon } from "lucide-react"; import { Button } from "../ui/button"; import { InputGroup, InputGroupAddon, InputGroupInput } from "../ui/input-group"; import { cn } from "~/lib/utils"; -import { formatThreadFindCount } from "./threadFind"; +import { + formatThreadFindCount, + nextThreadFindAnnouncement, + type ThreadFindAnnouncement, +} from "./threadFind"; import { THREAD_DETAILS_CARD_GAP } from "./threadDetailsCardLayout"; interface ThreadFindBarProps { @@ -14,6 +18,10 @@ interface ThreadFindBarProps { readonly status: "loading" | "error" | null; readonly onRetry: () => void; readonly activeIndex: number; + /** Bumped by each user search or step; background refreshes keep it. */ + readonly navigationId: number; + /** Whether the label shows the outcome of `navigationId`: a count or a failure. */ + readonly settled: boolean; readonly focusRequestId: number; readonly onQueryChange: (query: string) => void; readonly onNext: () => void; @@ -37,8 +45,6 @@ export function ThreadFindBar(props: ThreadFindBarProps) { props.open, ]); - if (!props.open) return null; - const hasQuery = props.query.trim().length > 0; const noResults = hasQuery && !props.status && props.matchCount === 0; let label = ""; @@ -47,6 +53,17 @@ export function ThreadFindBar(props: ThreadFindBarProps) { } if (props.status === "loading") label = "Searching…"; if (props.status === "error") label = "Search failed"; + const [storedAnnouncement, setAnnouncement] = useState(null); + const announcement = nextThreadFindAnnouncement(storedAnnouncement, { + navigationId: props.navigationId, + text: label, + settled: props.settled || props.status === "loading", + provisional: props.status !== null, + }); + if (announcement !== storedAnnouncement) setAnnouncement(announcement); + + if (!props.open) return null; + const navigationDisabled = props.matchCount === 0 || props.status === "loading" || props.counting === true; const handleKeyDown = (event: KeyboardEvent) => { @@ -87,8 +104,11 @@ export function ThreadFindBar(props: ThreadFindBarProps) { onKeyDown={handleKeyDown} /> + + {announcement.text} + & { readonly provisional?: boolean }; + +function replay(steps: readonly Step[]) { + let current: ThreadFindAnnouncement | null = null; + const spoken: string[] = []; + for (const step of steps) { + const next = nextThreadFindAnnouncement(current, { provisional: false, ...step }); + if (next.text !== (current?.text ?? "")) spoken.push(next.text); + current = next; + } + return spoken; +} + +describe("nextThreadFindAnnouncement", () => { + it("speaks each settled search or step once and stays silent through streamed refreshes", () => { + expect( + replay([ + { navigationId: 1, text: "Searching…", settled: true, provisional: true }, + { navigationId: 1, text: "1/…", settled: false }, + { navigationId: 1, text: "1/3", settled: true }, + { navigationId: 1, text: "1/…", settled: false }, + { navigationId: 1, text: "1/4", settled: true }, + { navigationId: 2, text: "1/4", settled: false }, + { navigationId: 2, text: "2/4", settled: true }, + { navigationId: 2, text: "2/5", settled: true }, + ]), + ).toEqual(["Searching…", "1/3", "2/4"]); + }); + + it("speaks a failure and the result of retrying it", () => { + expect( + replay([ + { navigationId: 1, text: "Search failed", settled: true, provisional: true }, + { navigationId: 1, text: "1/3", settled: true }, + { navigationId: 1, text: "1/4", settled: true }, + ]), + ).toEqual(["Search failed", "1/3"]); + }); + + it("speaks a new search that ends on the same count as the last one", () => { + expect( + replay([ + { navigationId: 1, text: "Searching…", settled: true, provisional: true }, + { navigationId: 1, text: "0/0", settled: true }, + { navigationId: 2, text: "Searching…", settled: true, provisional: true }, + { navigationId: 2, text: "0/0", settled: true }, + ]), + ).toEqual(["Searching…", "0/0", "Searching…", "0/0"]); + }); +}); diff --git a/apps/web/src/components/chat/threadFind.ts b/apps/web/src/components/chat/threadFind.ts index d0a265ceb13fa..98bff31c95996 100644 --- a/apps/web/src/components/chat/threadFind.ts +++ b/apps/web/src/components/chat/threadFind.ts @@ -23,3 +23,28 @@ export function stepThreadFindIndex(index: number, total: number, delta: number) export function formatThreadFindCount(index: number, total: number): string { return total <= 0 ? "0/0" : `${clampThreadFindIndex(index, total) + 1}/${total}`; } + +export interface ThreadFindAnnouncement { + readonly navigationId: number; + readonly text: string; + /** The label shows the outcome of `navigationId`: a count, a search or a failure. */ + readonly settled: boolean; + /** "Searching…" or a failure: spoken, but a later count for the same navigation is too. */ + readonly provisional: boolean; +} + +/** + * What the find counter's live region says: the settled outcome of each + * user search or step, once. Background refreshes during streaming keep the + * navigation, so they stay silent. A new query passes through "Searching…", + * so its count is spoken even when it equals the last one. + */ +export function nextThreadFindAnnouncement( + previous: ThreadFindAnnouncement | null, + next: ThreadFindAnnouncement, +): ThreadFindAnnouncement { + if (!next.settled) return previous ?? { ...next, text: "" }; + if (previous?.navigationId !== next.navigationId) return next; + if (previous.settled && !previous.provisional) return previous; + return previous.text === next.text ? previous : next; +} diff --git a/apps/web/src/components/chat/useThreadFind.ts b/apps/web/src/components/chat/useThreadFind.ts index b88951e3b6ac3..ada7955e1c2ad 100644 --- a/apps/web/src/components/chat/useThreadFind.ts +++ b/apps/web/src/components/chat/useThreadFind.ts @@ -214,6 +214,9 @@ export function useThreadFind({ counting, activeIndex, status, + navigationId: state.navigationId, + settled: + status === "error" || (status === null && !counting && navigationId === state.navigationId), focusRequestId: state.focusRequestId, onRetry: remote.refresh, onQueryChange: (query: string) => { From 12b9845d1c5aec2bfede36b313adfc5098a2fb93 Mon Sep 17 00:00:00 2001 From: Simone Date: Mon, 16 Nov 2026 17:20:28 +0100 Subject: [PATCH 016/253] fix(ssh): verify runtime archives in homes containing newlines (#37150) Co-authored-by: Simone <185146821+Lucenx9@users.noreply.github.com> Co-authored-by: Julius Marminge --- packages/ssh/src/tunnel.test.ts | 14 ++++++++++---- packages/ssh/src/tunnel.ts | 4 ++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/ssh/src/tunnel.test.ts b/packages/ssh/src/tunnel.test.ts index 7c107140e02be..d2cd22f49d654 100644 --- a/packages/ssh/src/tunnel.test.ts +++ b/packages/ssh/src/tunnel.test.ts @@ -340,14 +340,20 @@ describe("ssh tunnel scripts", () => { }, ); - it("names gzip when a host with tar but no gzip cannot unpack the release archive", () => { + // sha256sum and shasum escape a newline in a file name they print, so the home path has one. + const checksumTools = + NodeChildProcess.spawnSync("sh", ["-c", 'command -v "$1"', "sh", "shasum"]).status === 0 + ? ["sha256sum", "shasum"] + : ["sha256sum"]; + it.each(checksumTools)("names missing gzip after verifying with %s", (checksum) => { const root = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-ssh-runner-")); try { const bin = NodePath.join(root, "bin"); const release = NodePath.join(root, "release"); + const home = NodePath.join(root, "home\ncache"); const archive = `t3-${ARCHIVE.archiveVersion}-linux-x64.tar.gz`; NodeFS.mkdirSync(bin); - NodeFS.mkdirSync(NodePath.join(root, "home")); + NodeFS.mkdirSync(home); NodeFS.mkdirSync(NodePath.join(release, "t3"), { recursive: true }); NodeFS.writeFileSync(NodePath.join(release, "t3", "t3"), "#!/bin/sh\necho ok\n", { mode: 0o755, @@ -364,7 +370,7 @@ describe("ssh tunnel scripts", () => { "rm", "grep", "cut", - "sha256sum", + checksum, "tar", "mv", ]) { @@ -389,7 +395,7 @@ describe("ssh tunnel scripts", () => { NodeFS.writeFileSync(runner, SshTunnel.buildRemoteT3RunnerScript(ARCHIVE)); const result = NodeChildProcess.spawnSync(NodePath.join(bin, "sh"), [runner, "--version"], { encoding: "utf8", - env: { HOME: NodePath.join(root, "home"), PATH: bin }, + env: { HOME: home, PATH: bin }, }); assert.notEqual(result.status, 0); assert.match(result.stderr, /(missing|needs) gzip/); diff --git a/packages/ssh/src/tunnel.ts b/packages/ssh/src/tunnel.ts index c3ff20293aa1d..0b27a94acb33d 100644 --- a/packages/ssh/src/tunnel.ts +++ b/packages/ssh/src/tunnel.ts @@ -542,9 +542,9 @@ if ! t3_runtime_ready; then t3_fetch "$T3_RELEASE_BASE_URL/v$T3_ARCHIVE_VERSION/$T3_ARCHIVE" "$T3_STAGING/$T3_ARCHIVE" @@T3_ARCHIVE_DOWNLOAD_SECONDS@@ T3_EXPECTED="$(grep " \\*\\{0,1\\}$T3_ARCHIVE$" "$T3_STAGING/SHA256SUMS" | cut -d' ' -f1)" if command -v sha256sum >/dev/null 2>&1; then - T3_ACTUAL="$(sha256sum "$T3_STAGING/$T3_ARCHIVE" | cut -d' ' -f1)" + T3_ACTUAL="$(sha256sum < "$T3_STAGING/$T3_ARCHIVE" | cut -d' ' -f1)" else - T3_ACTUAL="$(shasum -a 256 "$T3_STAGING/$T3_ARCHIVE" | cut -d' ' -f1)" + T3_ACTUAL="$(shasum -a 256 < "$T3_STAGING/$T3_ARCHIVE" | cut -d' ' -f1)" fi if [ -z "$T3_EXPECTED" ] || [ "$T3_ACTUAL" != "$T3_EXPECTED" ]; then printf 'Checksum mismatch for %s.\\n' "$T3_ARCHIVE" >&2; exit 1 From bcb0604581664c58ef57504751fc9473e00a5af6 Mon Sep 17 00:00:00 2001 From: Yash Singh Date: Mon, 16 Nov 2026 10:39:19 -0600 Subject: [PATCH 017/253] fix(web): model picker options no longer jump a pixel on hover (#37661) Co-authored-by: Cursor --- apps/web/src/components/ui/popover.tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/apps/web/src/components/ui/popover.tsx b/apps/web/src/components/ui/popover.tsx index 005654db7bdd7..05baa4b626496 100644 --- a/apps/web/src/components/ui/popover.tsx +++ b/apps/web/src/components/ui/popover.tsx @@ -94,6 +94,8 @@ function PopoverPopup({ className={cn( "relative flex h-(--popup-height,auto) w-(--popup-width,auto) origin-(--transform-origin) rounded-lg text-popover-foreground outline-none transition-[width,height,scale,opacity] before:pointer-events-none before:absolute before:inset-0 before:rounded-[calc(var(--radius-lg)-1px)] before:shadow-[0_1px_--theme(--color-black/4%)] has-data-[slot=calendar]:rounded-xl has-data-[slot=calendar]:before:rounded-[calc(var(--radius-xl)-1px)] data-ending-style:scale-98 data-starting-style:scale-98 data-ending-style:opacity-0 data-starting-style:opacity-0 dark:before:shadow-[0_-1px_--theme(--color-white/6%)]", variant !== "panel" && "dropdown-glass", + // Without it, Chromium re-snaps text after the scale-in at fractional zoom, so hovered rows jump a pixel. + "will-change-[scale]", // Electron drag hit-testing ignores z-index; popups over a drag region would swallow hover. "[-webkit-app-region:no-drag]", tooltipStyle && From fecd7138e97d6b77265f5d73ec8ed62261b8632c Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 08:40:56 -0800 Subject: [PATCH 018/253] fix(mobile): long threads no longer jump to the bottom after they appear (#37559) --- .../src/features/threads/ThreadFeed.tsx | 37 +++++++++++++++---- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/apps/mobile/src/features/threads/ThreadFeed.tsx b/apps/mobile/src/features/threads/ThreadFeed.tsx index 1304fa17932d4..4dd69a5558f7c 100644 --- a/apps/mobile/src/features/threads/ThreadFeed.tsx +++ b/apps/mobile/src/features/threads/ThreadFeed.tsx @@ -3476,14 +3476,26 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { }; const feedThreadKey = scopedThreadKey(props.environmentId, props.threadId); const [loadedListKey, setLoadedListKey] = useState(null); - // LegendList hides rows until it has scrolled to the end, but not its header. - const settling = props.feed.length > 0 && loadedListKey !== feedThreadKey; - const handleMetricsChange = () => { + const [landedListKey, setLandedListKey] = useState(null); + // LegendList reports ready, and shows its rows, while rows measured during its initial scroll can + // still leave it hundreds of points short of the end; isAtEnd stays true through that. + const settling = props.feed.length > 0 && landedListKey !== feedThreadKey; + const landingFallback = useRef | null>(null); + const reportMetrics = (ready: boolean) => { const state = listRef.current?.getState(); + if (!state) return; + if ( + settling && + ready && + (state.distanceFromEnd <= 2 || !state.isFollowingEnd || props.jumpToMessageId) + ) { + setLandedListKey(feedThreadKey); + } // Offset 0 before the first positioning, and the gaps while rows measure in, are not the // user leaving the end; only a scroll away stops end following. - if (state) onIsAtEndChange?.(settling || state.isAtEnd || state.isFollowingEnd); + onIsAtEndChange?.(settling || state.isAtEnd || state.isFollowingEnd); }; + const handleMetricsChange = () => reportMetrics(loadedListKey === feedThreadKey); const handleScroll = (event: NativeSyntheticEvent) => { reportHeaderMaterialVisibility( event.nativeEvent.contentOffset.y + (usesNativeAutomaticInsets ? barInset : 0) > 6, @@ -3506,6 +3518,13 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { for (const stop of stops) stop(); }; }, [listRef, handleMetricsChange]); + // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- clears the landing fallback timer when the feed unmounts + useEffect( + () => () => { + if (landingFallback.current !== null) clearTimeout(landingFallback.current); + }, + [], + ); const [workGroupScrollPositions] = useState( () => new Map(), ); @@ -3949,7 +3968,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { { setLoadedListKey(feedThreadKey); - handleMetricsChange(); + reportMetrics(true); + // Never leave the feed hidden if end following stalls short of the end. + landingFallback.current = setTimeout(() => setLandedListKey(feedThreadKey), 1000); }} onLayout={handleMetricsChange} onItemSizeChanged={handleItemSizeChanged} onScroll={handleScroll} scrollEventThrottle={16} ListHeaderComponent={ - + <> {usesNativeAutomaticInsets ? null : } {setupAnchorIndex < 0 && props.worktreeSetup ? ( @@ -4018,7 +4039,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { {props.historyControls ? ( ) : null} - + } ListFooterComponent={ props.pendingHandoff ? ( From 6f5838f9bcdaf31864b93694c5935c93f9532e06 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 08:49:11 -0800 Subject: [PATCH 019/253] fix(client-runtime): a relative "Add project starts in" opens the picker under home (#37691) --- apps/web/src/components/CommandPalette.tsx | 9 ++------- .../client-runtime/src/operations/projects.test.ts | 11 +++++++++++ packages/client-runtime/src/operations/projects.ts | 7 +++++-- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/apps/web/src/components/CommandPalette.tsx b/apps/web/src/components/CommandPalette.tsx index ec7393427b901..cd32eeb4a6468 100644 --- a/apps/web/src/components/CommandPalette.tsx +++ b/apps/web/src/components/CommandPalette.tsx @@ -19,6 +19,7 @@ import { getNewProjectPublishTarget, getNewProjectPublishTargets, getNewProjectRepository, + getAddProjectInitialQuery, githubIncompleteOwnerRepositoryMessage, normalizePastedCloneUrl, addProjectRemoteSourceLabel, @@ -154,7 +155,6 @@ import { } from "../lib/chatThreadActions"; import { appendBrowsePathSegment, - ensureBrowseDirectoryPath, findProjectByPath, getBrowseDirectoryPath, hasTrailingPathSeparator, @@ -683,12 +683,7 @@ function OpenCommandPaletteDialog(props: { const paletteMode = getCommandPaletteMode({ currentView, isBrowsing }); const getAddProjectInitialQueryForEnvironment = (environmentId: EnvironmentId | null): string => { const environment = environments.find((candidate) => candidate.environmentId === environmentId); - const environmentSettings = environment?.serverConfig?.settings ?? null; - const baseDirectory = environmentSettings?.addProjectBaseDirectory?.trim() ?? ""; - if (baseDirectory.length === 0) { - return "~/"; - } - return ensureBrowseDirectoryPath(baseDirectory); + return getAddProjectInitialQuery(environment?.serverConfig?.settings.addProjectBaseDirectory); }; const { projectByKey } = useProjectLookups(); diff --git a/packages/client-runtime/src/operations/projects.test.ts b/packages/client-runtime/src/operations/projects.test.ts index 070ae5fbc53fb..5975f9c0dfdf9 100644 --- a/packages/client-runtime/src/operations/projects.test.ts +++ b/packages/client-runtime/src/operations/projects.test.ts @@ -9,14 +9,25 @@ import { getCloneDirectoryLeafName, getCloneDirectoryName, getCloneDirectoryPath, + getAddProjectInitialQuery, getDefaultCloneUrl, githubIncompleteOwnerRepositoryMessage, normalizePastedCloneUrl, sortAddProjectProviderSources, } from "./projects.ts"; import type { EnvironmentProject } from "../state/models.ts"; +import { isFilesystemBrowseQuery } from "../state/projects.ts"; describe("add project shared logic", () => { + it("starts the Add Project picker on a browsable path for a relative base directory", () => { + expect(isFilesystemBrowseQuery(getAddProjectInitialQuery("Desktop/dev"))).toBe(true); + expect(getAddProjectInitialQuery("Desktop/dev")).toBe("~/Desktop/dev/"); + expect(getAddProjectInitialQuery("/srv/code")).toBe("/srv/code/"); + expect(getAddProjectInitialQuery("~")).toBe("~/"); + expect(getAddProjectInitialQuery("./work")).toBe("./work/"); + expect(getAddProjectInitialQuery("C:\\dev")).toBe("C:\\dev\\"); + }); + it("offers only GitHub until other hosts are turned on", () => { const ready = () => ({ ready: true, hint: null }); expect(sortAddProjectProviderSources(ready, {})).toEqual(["github"]); diff --git a/packages/client-runtime/src/operations/projects.ts b/packages/client-runtime/src/operations/projects.ts index 8f7fcb7d1b424..e207963676521 100644 --- a/packages/client-runtime/src/operations/projects.ts +++ b/packages/client-runtime/src/operations/projects.ts @@ -11,7 +11,7 @@ import type { } from "@t3tools/contracts"; import { SourceControlProviderKind } from "@t3tools/contracts"; import { cloneUrlRepositoryPath } from "@t3tools/shared/cloneUrl"; -import { newProjectFolderName } from "@t3tools/shared/path"; +import { isExplicitRelativePath, isHostPath, newProjectFolderName } from "@t3tools/shared/path"; import * as Arr from "effect/Array"; import * as Option from "effect/Option"; import * as Order from "effect/Order"; @@ -204,7 +204,10 @@ export function buildAddProjectRemoteSourceReadiness( export function getAddProjectInitialQuery(baseDirectory: string | null | undefined): string { const trimmed = baseDirectory?.trim() ?? ""; - return trimmed.length === 0 ? "~/" : ensureBrowseDirectoryPath(trimmed); + if (trimmed.length === 0) return "~/"; + // A bare relative setting such as `Desktop/dev` means a folder under home. + const isAnchored = trimmed === "~" || isHostPath(trimmed) || isExplicitRelativePath(trimmed); + return ensureBrowseDirectoryPath(isAnchored ? trimmed : `~/${trimmed}`); } export function getCloneDirectoryName(repositoryOrRemoteUrl: string | null | undefined): string { From 83c520c21f8f5c90bb0944345a763ed2c7c9d63f Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 09:01:58 -0800 Subject: [PATCH 020/253] fix(web): clicks beside an open file's find bar reach the code again (#37676) --- .../fileEditorDismissal.browser.test.tsx | 52 +++++++++++++++++++ patches/@pierre__diffs@1.7.0-beta.4.patch | 9 ++++ pnpm-lock.yaml | 10 ++-- 3 files changed, 66 insertions(+), 5 deletions(-) diff --git a/apps/web/src/components/files/fileEditorDismissal.browser.test.tsx b/apps/web/src/components/files/fileEditorDismissal.browser.test.tsx index 2da63f3312c62..38c8e53156ecc 100644 --- a/apps/web/src/components/files/fileEditorDismissal.browser.test.tsx +++ b/apps/web/src/components/files/fileEditorDismissal.browser.test.tsx @@ -63,4 +63,56 @@ describe("installFileEditorDismissal", () => { expect(window.getSelection()?.toString()).toBe("Assistant reply"); }); + + it("lets clicks beside the open search panel reach the file's code", async () => { + let editor: Editor<"file" | "file-diff", undefined, undefined> | undefined; + const createEditor: EditorFactory = (...args) => + (editor = new Editor(...args)); + await preloadHighlighter({ themes: Object.values(DEFAULT_THEMES), langs: ["typescript"] }); + const contents = Array.from( + { length: 12 }, + (_, index) => `const server${index} = "a line long enough to run under the find bar";\n`, + ).join(""); + await using screen = await render( +
+ + + +
, + ); + const shadowRoot = screen.container.querySelector("diffs-container")?.shadowRoot; + const content = shadowRoot?.querySelector("[data-content]"); + expect(content).toBeTruthy(); + + editor!.setSelections([ + { start: { line: 0, character: 8 }, end: { line: 0, character: 8 }, direction: "none" }, + ]); + const isMac = isMacPlatform(navigator.platform); + content!.dispatchEvent( + new KeyboardEvent("keydown", { + key: "f", + code: "KeyF", + metaKey: isMac, + ctrlKey: !isMac, + bubbles: true, + composed: true, + cancelable: true, + }), + ); + const widget = await vi.waitFor(() => { + const panelHost = [...shadowRoot!.querySelectorAll("*")].find((element) => + element.shadowRoot?.querySelector("[data-search-panel]"), + ); + const found = panelHost?.shadowRoot?.querySelector("[data-editor-widget]"); + expect(found).toBeTruthy(); + return found!; + }); + const widgetBox = widget.getBoundingClientRect(); + const beside = shadowRoot!.elementFromPoint( + widgetBox.left - 100, + widgetBox.top + widgetBox.height / 2, + ); + + expect(content!.contains(beside)).toBe(true); + }); }); diff --git a/patches/@pierre__diffs@1.7.0-beta.4.patch b/patches/@pierre__diffs@1.7.0-beta.4.patch index ff75d50800ea5..4eacdc66fb9fe 100644 --- a/patches/@pierre__diffs@1.7.0-beta.4.patch +++ b/patches/@pierre__diffs@1.7.0-beta.4.patch @@ -81,6 +81,15 @@ index b4c280390b52434f63fbe7916d81b150d9006fb4..cb7fb89a22c8adac9ea951b8767b5fd4 } if (this.#selections !== void 0 || this.#matches !== void 0 || this.#markerRenderer !== void 0) { this.#updateSelections(this.#selections ?? []); +@@ -3369,7 +3388,8 @@ var Editor = class { + }); + this.#searchPanel = searchPanel; + this.#retainSearchPanelFocus = false; + searchPanel.mount(); ++ searchPanel.getHostElement().dataset.searchPanelOverlay = ""; + } + #searchWithNavigationTarget(textDocument, searchParams) { + const viewport = this.#getSearchViewportBounds(); diff --git a/dist/editor/textMeasure.d.ts b/dist/editor/textMeasure.d.ts index 89e938d89eca77d351aeed65f3c5495383effd97..cde1d6ba89a97bbb1614754a48f8994388c186bd 100644 --- a/dist/editor/textMeasure.d.ts diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5c84d21586059..985c914406cf7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -372,7 +372,7 @@ patchedDependencies: '@ff-labs/fff-node@0.11.0': 93f62a76399b435a76588b2bef237db0a1e85880c1ac0d0759c416e9cc579252 '@legendapp/list@3.7.2': 3c68607b8bb7a531873bcbe0b197d163ef1fdb4d4cf52a4ddf9d2b3804671658 '@muse-code/sdk@1.4.6': 7b495b607fc5d76b444c7c4d58821e839726a30e6f3c53564f0d59f2d5b42101 - '@pierre/diffs@1.7.0-beta.4': 756e7b154f48b9084ea6630520736cf446b69e6000777f3717363836694dd00c + '@pierre/diffs@1.7.0-beta.4': 241563d810c29250e0e92f3eda74214393726d46667fedbf1f91a5f5bea07d1e '@react-native-ai/apple@0.12.0': 2d09870c2848d185cb05b53ed823a46e12dba519324d8dd8e584e28731990f9d '@react-native-menu/menu@2.0.0': 6872ba71b9847c5a0210b1b896923dadc3860faedfe28d9f062bcedfe786ef1d '@react-navigation/native-stack@7.21.0': 990d575444c4af2ce8218b3b7a8280fe244c03abbdb32b822a2e862f791dbf42 @@ -573,7 +573,7 @@ importers: version: 0.3.0 '@pierre/diffs': specifier: 1.7.0-beta.4 - version: 1.7.0-beta.4(patch_hash=756e7b154f48b9084ea6630520736cf446b69e6000777f3717363836694dd00c)(@shikijs/themes@4.2.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0) + version: 1.7.0-beta.4(patch_hash=241563d810c29250e0e92f3eda74214393726d46667fedbf1f91a5f5bea07d1e)(@shikijs/themes@4.2.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0) '@pierre/highlights': specifier: 'catalog:' version: 0.5.0 @@ -1094,7 +1094,7 @@ importers: version: 1.8.0 '@pierre/diffs': specifier: 1.7.0-beta.4 - version: 1.7.0-beta.4(patch_hash=756e7b154f48b9084ea6630520736cf446b69e6000777f3717363836694dd00c)(@shikijs/themes@4.2.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0) + version: 1.7.0-beta.4(patch_hash=241563d810c29250e0e92f3eda74214393726d46667fedbf1f91a5f5bea07d1e)(@shikijs/themes@4.2.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0) '@pierre/trees': specifier: 1.0.0-beta.6 version: 1.0.0-beta.6(@pierre/theme@2.0.0)(@shikijs/themes@4.2.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(shiki@4.2.0) @@ -1510,7 +1510,7 @@ importers: dependencies: '@pierre/diffs': specifier: 1.7.0-beta.4 - version: 1.7.0-beta.4(patch_hash=756e7b154f48b9084ea6630520736cf446b69e6000777f3717363836694dd00c)(@shikijs/themes@4.2.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0) + version: 1.7.0-beta.4(patch_hash=241563d810c29250e0e92f3eda74214393726d46667fedbf1f91a5f5bea07d1e)(@shikijs/themes@4.2.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0) '@t3tools/contracts': specifier: workspace:* version: link:../contracts @@ -16440,7 +16440,7 @@ snapshots: tslib: 2.8.1 webcrypto-core: 1.9.2 - '@pierre/diffs@1.7.0-beta.4(patch_hash=756e7b154f48b9084ea6630520736cf446b69e6000777f3717363836694dd00c)(@shikijs/themes@4.2.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)': + '@pierre/diffs@1.7.0-beta.4(patch_hash=241563d810c29250e0e92f3eda74214393726d46667fedbf1f91a5f5bea07d1e)(@shikijs/themes@4.2.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)': dependencies: '@pierre/highlights': 0.5.1 '@pierre/patches': 0.0.1 From c8c51af4e3f67ec9d121b3fb10b4cfd17adfd541 Mon Sep 17 00:00:00 2001 From: Maximilian Fabian Kirchner <216908004+MaxFabian25@users.noreply.github.com> Date: Mon, 16 Nov 2026 18:08:50 +0100 Subject: [PATCH 021/253] fix(server): recover gateway cooldown after last error (#37692) Co-authored-by: Julius Marminge --- .../Adapters/ClaudeAdapterV2.test.ts | 14 ++++++++++++++ .../orchestration-v2/Adapters/ClaudeAdapterV2.ts | 12 ++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 1d549d7f2c18b..cf533de93e4d6 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -9472,6 +9472,20 @@ describe("ClaudeAdapterV2 background wake turns", () => { it.effect.each([ { cooldown: " (reset after 1h2m3s)", seconds: 3723 }, { cooldown: " (reset after 4m 6s) (last error: rate limited)", seconds: 246 }, + { + cooldown: + " (last error: rate_limit_error: This request would exceed your account's rate limit. Please try again later.) (reset after 19h30m2s)", + seconds: 70202, + }, + { + cooldown: + " (last error: All credentials for model upstream are cooling down (reset after 1h)) (reset after 1m)", + seconds: 60, + }, + { + cooldown: " (last error: Request rejected (429) (reset after 1h) (last error: rate limited))", + seconds: 0, + }, { cooldown: "", seconds: 0 }, ])("keeps a gateway's reset on a direct 429 with cooldown '$cooldown'", (input) => Effect.gen(function* () { diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 5dcb77f7abddc..fa8d3684f0f23 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -4769,12 +4769,16 @@ function providerFailureFromResult( apiErrorStatus === 429 && message.result.startsWith("API Error:") ? gatewayCooldownResetAt( // CLIProxyAPI nests upstream errors in its message; only its own cooldown counts. - (message.result.startsWith( + message.result.startsWith( "API Error: Request rejected (429) · All credentials for model ", ) - ? /^API Error: Request rejected \(429\) · All credentials for model [^\s()]+ are cooling down \(reset after (?:(\d+)h ?)?(?:(\d+)m ?)?(?:(\d+)s)?\)/ - : /\(reset after (?:(\d+)h ?)?(?:(\d+)m ?)?(?:(\d+)s)?\)/ - ).exec(message.result), + ? (/^API Error: Request rejected \(429\) · All credentials for model [^\s()]+ are cooling down \(reset after (?:(\d+)h ?)?(?:(\d+)m ?)?(?:(\d+)s)?\)/.exec( + message.result, + ) ?? + /^API Error: Request rejected \(429\) · All credentials for model [^\s()]+ are cooling down \(last error: [\s\S]*\) \(reset after (?:(\d+)h ?)?(?:(\d+)m ?)?(?:(\d+)s)?\)$/.exec( + message.result, + )) + : /\(reset after (?:(\d+)h ?)?(?:(\d+)m ?)?(?:(\d+)s)?\)/.exec(message.result), receivedAt, ) : undefined; From 4860a730695762331cc482253ba60beda7875c40 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 09:31:15 -0800 Subject: [PATCH 022/253] fix(mobile): sent videos no longer flash black and queued attachments keep uploading (#37681) Co-authored-by: Claude Opus 5.5 --- .../src/features/threads/ThreadFeed.tsx | 151 +++++++++++------- .../features/threads/ThreadMarkdownVideo.tsx | 4 +- .../threads/pending-thread-feed.test.ts | 31 ++++ .../features/threads/pending-thread-feed.ts | 17 ++ .../src/lib/composerAttachmentUploadQueue.ts | 24 +++ .../composer-attachment-upload-states.ts | 8 + .../state/composer-attachment-uploads.test.ts | 29 ++++ .../src/state/composer-attachment-uploads.ts | 109 ++++++++++--- .../threadOutboxDelivery.permissions.test.ts | 68 ++++++++ apps/mobile/src/state/threadOutboxDelivery.ts | 39 +++++ .../src/state/use-thread-composer-state.ts | 9 +- 11 files changed, 407 insertions(+), 82 deletions(-) create mode 100644 apps/mobile/src/state/composer-attachment-upload-states.ts diff --git a/apps/mobile/src/features/threads/ThreadFeed.tsx b/apps/mobile/src/features/threads/ThreadFeed.tsx index 4dd69a5558f7c..3e1ee80f2831f 100644 --- a/apps/mobile/src/features/threads/ThreadFeed.tsx +++ b/apps/mobile/src/features/threads/ThreadFeed.tsx @@ -143,7 +143,10 @@ import { useFilePreviewGallery, FilePreviewGallery } from "../../components/ui/F import { assetImageSource } from "../../lib/assetImageSource"; import { filePreviewSourceIdentifier } from "../../lib/filePreviewGallery"; import { composerImageGallery } from "../../lib/composerImageGallery"; -import { isFileBackedComposerAttachment } from "../../lib/composerImages"; +import { + isFileBackedComposerAttachment, + type FileBackedComposerAttachment, +} from "../../lib/composerImages"; import { useLocalAttachmentPreview } from "../../lib/localAttachmentPreview"; import { rememberSentImage, @@ -180,7 +183,11 @@ import { import { AppText as Text } from "../../components/ui/AppText"; import { VideoPreviewModal, type VideoPreviewSource } from "../../components/ui/VideoPreviewModal"; import { markdownFileEmbedLabel, resolveMarkdownMediaPreview } from "../../lib/markdownMedia"; -import { attachmentVideoPreviewSource } from "../../lib/videoPreviewSource"; +import { + attachmentVideoPreviewSource, + mediaVideoThumbnailKey, + type MediaVideoPreviewSource, +} from "../../lib/videoPreviewSource"; import { CopyTextButton } from "./CopyTextButton"; import { getReviewSectionIdForCheckpoint } from "../review/reviewModel"; import { setReviewSelectedSectionId } from "../review/reviewState"; @@ -672,7 +679,6 @@ function MessageAttachmentFile(props: { readonly attachment: ChatFileAttachment; readonly onPressPreview: (source: FilePreviewSource) => void; readonly onPlaybackStart: () => void; - readonly standaloneMedia?: boolean; readonly standaloneFile?: boolean; readonly available?: boolean; }) { @@ -809,14 +815,10 @@ function MessageAttachmentFile(props: { if (videoType !== null) { return ( - + ); @@ -907,7 +909,6 @@ function UserMessageMedia(props: { ...props.pendingAttachments.map((attachment, index) => { const image = isImageAttachment(attachment); return { - id: attachment.id, image, content: image && attachment.uploadedAttachmentId ? ( @@ -921,30 +922,31 @@ function UserMessageMedia(props: { onImageLoad={(loaded) => rememberSentImage(props.messageId, index, loaded)} onPressPreview={props.onPressPreview} /> - ) : !image && attachment.uploadedAttachmentId ? ( - ) : ( - rememberSentImage(props.messageId, index, image)} onPressPreview={props.onPressPreview} - onPlaybackStart={props.onPlaybackStart} /> ), }; }), ...props.attachments.map((attachment, index) => ({ - id: attachment.id, image: isImageAttachment(attachment), content: isImageAttachment(attachment) ? ( ) : isFileAttachment(attachment) ? ( - ) : null, })), @@ -986,7 +989,8 @@ function UserMessageMedia(props: { media .filter((_, index) => index % 2 === 0) .map((item, index) => ( - + // oxlint-disable-next-line react/no-array-index-key -- delivery re-ids attachments; position is the stable identity + {item.content} {media[index * 2 + 1] ? ( {media[index * 2 + 1]!.content} @@ -998,9 +1002,10 @@ function UserMessageMedia(props: { ) : ( - {media.map((item) => ( + {media.map((item, index) => ( @@ -1015,7 +1020,7 @@ function UserMessageMedia(props: { ); } -function PendingMessageMedia(props: { +function PendingMessageImage(props: { readonly attachment: QueuedThreadMessage["attachments"][number]; readonly className: string; readonly maxWidth?: number; @@ -1023,36 +1028,70 @@ function PendingMessageMedia(props: { readonly handoff?: SentImageHandoff; readonly onImageLoad?: (image: SentImageHandoff) => void; readonly onPressPreview: (source: FilePreviewSource) => void; - readonly onPlaybackStart: () => void; }) { const withGallery = useFilePreviewGallery(); const { attachment, handoff } = props; const fileBacked = isFileBackedComposerAttachment(attachment); const preview = useLocalAttachmentPreview(fileBacked ? attachment : null); - if (isImageAttachment(attachment)) { - // The composer already showed this image; reuse its pixels and shape instead of a blank box. - const uri = - handoff?.uri ?? (fileBacked ? preview.uri : (attachment.dataUrl ?? attachment.previewUri)); - return ( - { - if (uri) props.onImageLoad?.({ uri, aspectRatio: size.width / size.height }); - }} - onPress={() => { - const source = composerImageGallery([attachment])[0]; - if (source) props.onPressPreview(withGallery(source)); - }} - /> - ); + // The composer already showed this image; reuse its pixels and shape instead of a blank box. + const uri = + handoff?.uri ?? (fileBacked ? preview.uri : (attachment.dataUrl ?? attachment.previewUri)); + return ( + { + if (uri) props.onImageLoad?.({ uri, aspectRatio: size.width / size.height }); + }} + onPress={() => { + const source = composerImageGallery([attachment])[0]; + if (source) props.onPressPreview(withGallery(source)); + }} + /> + ); +} + +/** + * A sent video from queueing through delivery. Every stage renders this one player under a + * thumbnail key of message and position, so swapping the local file for the server copy keeps + * the poster on screen instead of remounting to black and extracting a frame again. + */ +function UserMessageVideo(props: { + readonly environmentId: EnvironmentId; + readonly messageId: string; + readonly index: number; + readonly local: FileBackedComposerAttachment | null; + readonly uploaded: ChatFileAttachment | null; + readonly onPlaybackStart: () => void; +}) { + const sourceIdentifier = useId(); + const preview = useLocalAttachmentPreview(props.local); + const current: MediaVideoPreviewSource | null = + props.local !== null && preview.uri !== null + ? { + type: "media", + name: props.local.name, + mimeType: props.local.mimeType, + uri: preview.uri, + } + : props.uploaded !== null + ? attachmentVideoPreviewSource(props.environmentId, props.uploaded, sourceIdentifier) + : null; + // Each copy of a message reopens its local file; keep the last source while the next one loads. + const [shown, setShown] = useState(current); + if ( + current !== null && + (shown === null || mediaVideoThumbnailKey(current) !== mediaVideoThumbnailKey(shown)) + ) { + setShown(current); } - if (preview.uri === null) { + const source = current ?? shown; + if (source === null) { return ( {preview.error ? ( @@ -1065,12 +1104,8 @@ function PendingMessageMedia(props: { } return ( diff --git a/apps/mobile/src/features/threads/ThreadMarkdownVideo.tsx b/apps/mobile/src/features/threads/ThreadMarkdownVideo.tsx index 4d6d566f051ac..3e2d5dc05081f 100644 --- a/apps/mobile/src/features/threads/ThreadMarkdownVideo.tsx +++ b/apps/mobile/src/features/threads/ThreadMarkdownVideo.tsx @@ -14,6 +14,8 @@ export const ThreadMediaRetainContext = createContext<(retained: boolean) => voi export function ThreadMarkdownVideo(props: { readonly source: MediaVideoPreviewSource; + /** Keeps the player and its poster across source changes that show the same video. */ + readonly thumbnailKey?: string; readonly thumbnailVisible?: boolean; readonly onPlaybackStart?: () => void; readonly borderRadius?: number; @@ -21,7 +23,7 @@ export function ThreadMarkdownVideo(props: { const { source } = props; const feedVisible = useContext(ThreadMediaVisibleContext); const visible = props.thumbnailVisible ?? feedVisible; - const thumbnailKey = mediaVideoThumbnailKey(source); + const thumbnailKey = props.thumbnailKey ?? mediaVideoThumbnailKey(source); const asset = useAssetUrlState( "environmentId" in source ? source.environmentId : null, "resource" in source ? source.resource : null, diff --git a/apps/mobile/src/features/threads/pending-thread-feed.test.ts b/apps/mobile/src/features/threads/pending-thread-feed.test.ts index b16fab3a20a90..99cabea0d5443 100644 --- a/apps/mobile/src/features/threads/pending-thread-feed.test.ts +++ b/apps/mobile/src/features/threads/pending-thread-feed.test.ts @@ -9,6 +9,7 @@ import { } from "@t3tools/contracts"; import type { QueuedThreadMessage } from "../../state/thread-outbox-model"; import { + appendAcceptedThreadMessages, appendPendingThreadMessages, createThreadFeedGrouping, isCompactionCommandFeedEntry, @@ -138,6 +139,36 @@ describe("pending timeline messages", () => { expect(result[0]?.acknowledged).toBe(false); }); + it("keeps a new thread's first message attachments after delivery until the server copy loads", () => { + const video = { + id: "video-1", + type: "file" as const, + name: "clip.mp4", + mimeType: "video/mp4", + sizeBytes: 3, + fileUri: "file:///documents/clip.mp4", + }; + const creation = { + ...pending("creation"), + attachments: [video], + creation: { + projectId: ProjectId.make("project"), + workspaceMode: "worktree" as const, + branch: "main", + worktreePath: null, + }, + }; + const synthetic = { + ...appendPendingThreadMessages([], [], [creation])[0]!, + pendingMessage: undefined, + }; + // Delivered and caught up by sequence, but the server's copy is not projected yet. + const result = appendAcceptedThreadMessages([synthetic], [], creation); + expect(result).toHaveLength(1); + expect(result[0]?.pendingMessage?.attachments).toEqual([video]); + expect(result[0]?.acknowledged).toBe(true); + }); + it("retains context records while a message is waiting for delivery", () => { const record = { version: 1 as const, diff --git a/apps/mobile/src/features/threads/pending-thread-feed.ts b/apps/mobile/src/features/threads/pending-thread-feed.ts index 586f8ff3b8820..940ec12db8604 100644 --- a/apps/mobile/src/features/threads/pending-thread-feed.ts +++ b/apps/mobile/src/features/threads/pending-thread-feed.ts @@ -158,6 +158,23 @@ export function appendPendingThreadMessages( ]; } +/** + * Messages the server accepted but has not projected yet. A new thread's first message stays + * anchored until the server's copy loads, and keeps its local attachments so its media does + * not drop out of the feed in between. + */ +export function appendAcceptedThreadMessages( + feed: ReadonlyArray, + acknowledged: ReadonlyArray, + creation: QueuedThreadMessage | null, +): ReadonlyArray { + const accepted = creation === null ? acknowledged : [...acknowledged, creation]; + if (accepted.length === 0) return feed; + return appendPendingThreadMessages(feed, feed, accepted).map((entry) => + entry.pendingMessage ? { ...entry, acknowledged: true } : entry, + ); +} + export function isCompactionCommandFeedEntry(entry: PendingThreadFeedEntry): boolean { return ( entry.type === "message" && diff --git a/apps/mobile/src/lib/composerAttachmentUploadQueue.ts b/apps/mobile/src/lib/composerAttachmentUploadQueue.ts index 72d3eef9c21a6..0a28fff2231d5 100644 --- a/apps/mobile/src/lib/composerAttachmentUploadQueue.ts +++ b/apps/mobile/src/lib/composerAttachmentUploadQueue.ts @@ -105,6 +105,30 @@ export function composerAttachmentsStillUploading(input: { }); } +/** Changes when an upload starts or settles, not on its progress ticks. */ +export function uploadingComposerAttachmentKeys( + states: Readonly>, +): string { + return Object.keys(states) + .filter((key) => states[key]?.status === "uploading") + .sort() + .join("\n"); +} + +/** A queued message waits for the worker's upload instead of starting a second one. */ +export function queuedAttachmentsStillUploading(input: { + readonly environmentId: EnvironmentId; + readonly attachments: ReadonlyArray; + readonly states: Readonly>; +}): boolean { + return input.attachments.some( + (attachment) => + attachment.uploadedAttachmentId === undefined && + input.states[composerAttachmentUploadKey(input.environmentId, attachment.id)]?.status === + "uploading", + ); +} + export function createComposerAttachmentUploadQueue(options: { readonly upload: ( request: ComposerAttachmentUploadRequest, diff --git a/apps/mobile/src/state/composer-attachment-upload-states.ts b/apps/mobile/src/state/composer-attachment-upload-states.ts new file mode 100644 index 0000000000000..2dafe12ef678a --- /dev/null +++ b/apps/mobile/src/state/composer-attachment-upload-states.ts @@ -0,0 +1,8 @@ +import { Atom } from "effect/reactivity"; + +import type { ComposerAttachmentUploadState } from "../lib/composerAttachmentUploadQueue"; + +/** The upload worker's per-attachment states, apart from the worker so the outbox can read them. */ +export const composerAttachmentUploadsAtom = Atom.make< + Readonly> +>({}).pipe(Atom.keepAlive); diff --git a/apps/mobile/src/state/composer-attachment-uploads.test.ts b/apps/mobile/src/state/composer-attachment-uploads.test.ts index 4a9c0d427276e..81a542a5aca1c 100644 --- a/apps/mobile/src/state/composer-attachment-uploads.test.ts +++ b/apps/mobile/src/state/composer-attachment-uploads.test.ts @@ -190,6 +190,7 @@ import { } from "./composer-attachment-uploads"; import { enqueueThreadOutboxMessage, + flattenQueuedThreadMessages, flushThreadOutbox, threadOutboxManager, } from "./thread-outbox"; @@ -216,6 +217,7 @@ const requestsAtom = Atom.make((get) => composerAttachmentUploadRequests({ drafts: get(drafts.composerDraftUploadSourcesAtom), queuedMessagesByThreadKey: get(threadOutboxManager.queuedMessagesByThreadKeyAtom), + editingQueuedMessageIds: {}, knownEnvironmentIds: [environmentId], connectedEnvironmentIds: new Set([environmentId]), operableEnvironmentIds: new Set([environmentId]), @@ -319,6 +321,31 @@ it.each(["decode", "write"] as const)( }, ); +it("keeps an upload running when its message is sent mid-upload and records it on the message", async () => { + const transferStarted = Promise.withResolvers(); + const finishTransfer = Promise.withResolvers<{ status: number }>(); + native.transfer.mockImplementationOnce(async (uri, url) => { + native.server.set(url.split("/").at(-1)!, native.files.get(uri)!); + transferStarted.resolve(); + return finishTransfer.promise; + }); + const queue = worker(); + await transferStarted.promise; + + // Send enqueues the message, then clears the draft that owned the upload. + await enqueueThreadOutboxMessage(queuedMessage()); + drafts.clearComposerDraftContent(draftKey, { deferAttachmentCleanup: true }); + finishTransfer.resolve({ status: 204 }); + await queue.settled(); + + expect(native.transfer).toHaveBeenCalledOnce(); + expect(native.deleted).toEqual([]); + const [queued] = flattenQueuedThreadMessages( + appAtomRegistry.get(threadOutboxManager.queuedMessagesByThreadKeyAtom), + ); + expect(queued?.attachments[0]?.uploadedAttachmentId).toBe("pending-1"); +}); + it.each(["http", "permission"] as const)( "keeps a real %s upload failure blocking Send", async (failure) => { @@ -355,6 +382,7 @@ it("uploads draft attachments for threads whose ids contain colons", () => { const requests = composerAttachmentUploadRequests({ drafts: Object.fromEntries(keys.map((key) => [key, { attachments: [{ ...image, id: key }] }])), queuedMessagesByThreadKey: {}, + editingQueuedMessageIds: {}, knownEnvironmentIds: [environmentId], connectedEnvironmentIds: new Set([environmentId]), operableEnvironmentIds: new Set([environmentId]), @@ -371,6 +399,7 @@ it("does not upload a disconnected colon environment's draft to its connected pr const requests = composerAttachmentUploadRequests({ drafts: { [`${disconnected}:thread-1`]: { attachments: [image] } }, queuedMessagesByThreadKey: {}, + editingQueuedMessageIds: {}, knownEnvironmentIds: [connected, disconnected], connectedEnvironmentIds: new Set([connected]), operableEnvironmentIds: new Set([connected]), diff --git a/apps/mobile/src/state/composer-attachment-uploads.ts b/apps/mobile/src/state/composer-attachment-uploads.ts index 0749116fa6859..8e2921c5f62d2 100644 --- a/apps/mobile/src/state/composer-attachment-uploads.ts +++ b/apps/mobile/src/state/composer-attachment-uploads.ts @@ -15,11 +15,18 @@ import { composerDraftEnvironmentId, canUploadComposerAttachment, createComposerAttachmentUploadQueue, - type ComposerAttachmentUploadState, } from "../lib/composerAttachmentUploadQueue"; import { appAtomRegistry } from "./atom-registry"; +import { composerAttachmentUploadsAtom } from "./composer-attachment-upload-states"; import { environmentServerConfigsAtom } from "./server"; -import { flattenQueuedThreadMessages, threadOutboxManager } from "./thread-outbox"; +import { + flattenQueuedThreadMessages, + threadOutboxManager, + threadOutboxRevision, + updateThreadOutboxMessage, + type QueuedThreadMessage, +} from "./thread-outbox"; +import { editingQueuedMessageIdsAtom } from "./use-thread-outbox"; import { ComposerDraftPersistenceError, type ComposerDraft, @@ -39,9 +46,7 @@ export { composerAttachmentsStillUploading, } from "../lib/composerAttachmentUploadQueue"; -export const composerAttachmentUploadsAtom = Atom.make< - Readonly> ->({}).pipe(Atom.keepAlive); +export { composerAttachmentUploadsAtom }; const uploadStateAtom = Atom.family((key: string) => Atom.map(composerAttachmentUploadsAtom, (states) => states[key]), ); @@ -69,6 +74,8 @@ type ComposerAttachmentUploadRequest = { export function composerAttachmentUploadRequests(input: { readonly drafts: Readonly>>; readonly queuedMessagesByThreadKey: Parameters[0]; + /** An edited message's attachments upload through its editor draft. */ + readonly editingQueuedMessageIds: Readonly>; /** Every environment the client knows, so a draft key never resolves to a shorter connected prefix. */ readonly knownEnvironmentIds: Iterable; readonly connectedEnvironmentIds: ReadonlySet; @@ -79,20 +86,82 @@ export function composerAttachmentUploadRequests(input: { >; }): ReadonlyArray { const queued = flattenQueuedThreadMessages(input.queuedMessagesByThreadKey); - return Object.entries(input.drafts).flatMap(([key, draft]) => { - const environmentId = composerDraftEnvironmentId(key, input.knownEnvironmentIds, queued, draft); - if ( - environmentId === null || - !input.connectedEnvironmentIds.has(environmentId) || - !input.operableEnvironmentIds.has(environmentId) - ) - return []; - return draft.attachments - .filter((attachment) => - canUploadComposerAttachment(attachment, input.serverConfigs.get(environmentId)), - ) - .map((attachment) => ({ environmentId, attachment })); - }); + const uploadable = ( + environmentId: EnvironmentId | null, + attachments: ReadonlyArray, + ): ReadonlyArray => + environmentId === null || + !input.connectedEnvironmentIds.has(environmentId) || + !input.operableEnvironmentIds.has(environmentId) + ? [] + : attachments + .filter((attachment) => + canUploadComposerAttachment(attachment, input.serverConfigs.get(environmentId)), + ) + .map((attachment) => ({ environmentId, attachment })); + return [ + ...Object.entries(input.drafts).flatMap(([key, draft]) => + uploadable( + composerDraftEnvironmentId(key, input.knownEnvironmentIds, queued, draft), + draft.attachments, + ), + ), + // Sending mid-upload hands the attachment to the outbox; requesting it here keeps that + // upload running instead of aborting it for the outbox to start over. + ...queued.flatMap((message) => + input.editingQueuedMessageIds[message.messageId] + ? [] + : uploadable( + message.environmentId, + message.attachments.filter( + (attachment) => attachment.uploadedAttachmentId === undefined, + ), + ), + ), + ]; +} + +function withUploadedAttachment( + message: QueuedThreadMessage, + uploaded: DraftComposerAttachment, +): QueuedThreadMessage | null { + const awaiting = (attachment: DraftComposerAttachment) => + attachment.id === uploaded.id && attachment.uploadedAttachmentId === undefined; + if (!message.attachments.some(awaiting)) return null; + return { + ...message, + attachments: message.attachments.map((attachment) => + awaiting(attachment) ? uploaded : attachment, + ), + }; +} + +async function recordQueuedAttachmentUpload( + environmentId: EnvironmentId, + uploaded: DraftComposerAttachment, +): Promise { + const awaiting = () => { + const editing = appAtomRegistry.get(editingQueuedMessageIdsAtom); + return flattenQueuedThreadMessages( + appAtomRegistry.get(threadOutboxManager.queuedMessagesByThreadKeyAtom), + ).flatMap((message) => { + if (message.environmentId !== environmentId || editing[message.messageId]) return []; + const updated = withUploadedAttachment(message, uploaded); + return updated === null + ? [] + : [{ updated, revision: threadOutboxRevision(message.messageId) }]; + }); + }; + let recorded = false; + // A sibling attachment finishing at the same time bumps the revision; reread and retry. + for (let attempt = 0; attempt < 3; attempt++) { + const pending = awaiting(); + if (pending.length === 0) break; + for (const { updated, revision } of pending) { + recorded = (await updateThreadOutboxMessage(updated, revision)) || recorded; + } + } + return recorded; } // Each connected environment's session is read here so a granted or revoked @@ -116,6 +185,7 @@ const composerAttachmentUploadRequestsAtom = Atom.make((get) => { return composerAttachmentUploadRequests({ drafts: get(composerDraftUploadSourcesAtom), queuedMessagesByThreadKey: get(threadOutboxManager.queuedMessagesByThreadKeyAtom), + editingQueuedMessageIds: get(editingQueuedMessageIdsAtom), knownEnvironmentIds: environments.map((environment) => environment.environmentId), connectedEnvironmentIds: new Set(connected.map((environment) => environment.environmentId)), operableEnvironmentIds, @@ -164,6 +234,7 @@ export function createComposerAttachmentUploadWorker( retained = setComposerDraftAttachmentUpload(key, uploaded) || retained; } } + retained = (await recordQueuedAttachmentUpload(environmentId, uploaded)) || retained; if (!retained) return "abandon"; // Draft-save failure only costs re-upload after restart; the outbox saves references independently, so preserve accepted uploads. await flushComposerDrafts().catch((error: unknown) => { diff --git a/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts b/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts index 0d7bfdc3e8c7a..263c48195d75d 100644 --- a/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts +++ b/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts @@ -163,6 +163,8 @@ import { appAtomRegistry } from "./atom-registry"; import { environmentSession } from "./session"; import { dispatchingQueuedMessageIdAtom, editingQueuedMessageIdsAtom } from "./use-thread-outbox"; import { createThreadOutboxDelivery } from "./threadOutboxDelivery"; +import { composerAttachmentUploadsAtom } from "./composer-attachment-upload-states"; +import { workspaceConnections } from "./workspace"; const message = (overrides: Partial = {}): QueuedThreadMessage => ({ environmentId: EnvironmentId.make("secondary"), @@ -651,3 +653,69 @@ describe("compaction before a queued send", () => { expect(remaining()).toEqual([queued]); }); }); + +describe("queued delivery timing", () => { + /** Runs `change`, then resolves once the delivery it starts has settled. */ + async function deliveryAfter(change: () => void): Promise { + const settled = Promise.withResolvers(); + const release = appAtomRegistry.subscribe(dispatchingQueuedMessageIdAtom, (id) => { + if (id === null) settled.resolve(); + }); + change(); + expect(appAtomRegistry.get(dispatchingQueuedMessageIdAtom)).not.toBeNull(); + await settled.promise.finally(release); + } + + it("waits for the composer's upload of a sent attachment instead of uploading it again", async () => { + setOperateGrant("secondary", true); + const photo = { + id: "photo-1", + type: "image" as const, + name: "photo.png", + mimeType: "image/png", + sizeBytes: 3, + previewUri: "file:///photo.png", + fileUri: "file:///photo.png", + }; + appAtomRegistry.set(composerAttachmentUploadsAtom, { + "secondary:photo-1": { status: "uploading", progress: 0.5 }, + }); + state.cleanups.push(() => appAtomRegistry.set(composerAttachmentUploadsAtom, {})); + await state.manager.enqueue(message({ attachments: [photo] })); + startDelivery(); + expect(appAtomRegistry.get(dispatchingQueuedMessageIdAtom)).toBeNull(); + + await deliveryAfter(() => + appAtomRegistry.set(composerAttachmentUploadsAtom, { + "secondary:photo-1": { status: "ready" }, + }), + ); + expect(state.prepare).toHaveBeenCalledOnce(); + expect(state.start).toHaveBeenCalledOnce(); + expect(remaining()).toEqual([]); + }); + + it("sends at once when its environment reconnects instead of waiting out the backoff", async () => { + setOperateGrant("secondary", true); + vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "Date"] }); + state.cleanups.push(() => vi.useRealTimers()); + const environments = workspaceConnections.environmentsAtom as unknown as Atom.Writable< + ReadonlyArray<{ environmentId: string; connectionState: string }> + >; + const connected = [{ environmentId: "secondary", connectionState: "connected" }]; + state.cleanups.push(() => appAtomRegistry.set(environments, connected)); + await state.manager.enqueue(message()); + state.start.mockResolvedValueOnce( + AsyncResult.failure(Cause.fail(new Error("Socket is not connected"))), + ); + await runDrain(); + expect(state.start).toHaveBeenCalledOnce(); + + appAtomRegistry.set(environments, [ + { environmentId: "secondary", connectionState: "disconnected" }, + ]); + await deliveryAfter(() => appAtomRegistry.set(environments, connected)); + expect(state.start).toHaveBeenCalledTimes(2); + expect(remaining()).toEqual([]); + }); +}); diff --git a/apps/mobile/src/state/threadOutboxDelivery.ts b/apps/mobile/src/state/threadOutboxDelivery.ts index b3feeae3bb801..74cf7597e0ef7 100644 --- a/apps/mobile/src/state/threadOutboxDelivery.ts +++ b/apps/mobile/src/state/threadOutboxDelivery.ts @@ -48,6 +48,11 @@ import { } from "./pending-thread-creation"; import { environmentProjects } from "./projects"; import { environmentServerConfigsAtom, serverEnvironment } from "./server"; +import { composerAttachmentUploadsAtom } from "./composer-attachment-upload-states"; +import { + queuedAttachmentsStillUploading, + uploadingComposerAttachmentKeys, +} from "../lib/composerAttachmentUploadQueue"; import { gitEnvironment } from "./git"; import { environmentSession, readEnvironmentScope, sessionHasScope } from "./session"; import { @@ -589,6 +594,11 @@ async function preserveUploadedAttachmentsForEditor( // Everything a drain pass decides from. Each queued environment's session is // read here so a granted or revoked operate scope re-runs the drain. +const uploadingComposerAttachmentKeysAtom = Atom.map( + composerAttachmentUploadsAtom, + uploadingComposerAttachmentKeys, +); + const deliveryInputsAtom = Atom.make((get) => { const connectedEnvironments = get(workspaceConnections.environmentsAtom); const operableEnvironments = new Set(); @@ -612,6 +622,8 @@ const deliveryInputsAtom = Atom.make((get) => { serverConfigs: get(environmentServerConfigsAtom), connectedEnvironments, operableEnvironments, + // Wakes a message waiting on the upload worker when that upload settles. + uploadingAttachmentKeys: get(uploadingComposerAttachmentKeysAtom), }; }).pipe(Atom.withLabel("mobile:thread-outbox:delivery-inputs")); @@ -691,6 +703,7 @@ export function createThreadOutboxDelivery(): { } { let disposed = false; const retries = new Map(); + let readyEnvironmentIds = new Set(); // Existing-thread turns the server accepted but whose outbox removal has // not landed; the next pass retries only the removal, never the send. const acknowledgedExistingThreadMessageIds = new Set(); @@ -1192,6 +1205,23 @@ export function createThreadOutboxDelivery(): { connectedEnvironments, operableEnvironments, } = appAtomRegistry.get(deliveryInputsAtom); + // Failures while an environment was unreachable say nothing about it now; a backoff + // started then would hold a message for up to its full delay after reconnecting. + const ready = new Set( + connectedEnvironments + .filter( + (environment) => + environment.connectionState === "connected" && + serverConfigs.has(environment.environmentId), + ) + .map((environment) => environment.environmentId), + ); + for (const message of Object.values(queuedMessagesByThreadKey).flat()) { + if (ready.has(message.environmentId) && !readyEnvironmentIds.has(message.environmentId)) { + clearRetry(message.messageId); + } + } + readyEnvironmentIds = ready; if (dispatchingQueuedMessageId !== null) { return; } @@ -1255,6 +1285,15 @@ export function createThreadOutboxDelivery(): { if (retryNotBefore(messageId) > Date.now()) { continue; } + if ( + queuedAttachmentsStillUploading({ + environmentId: nextQueuedMessage.environmentId, + attachments: nextQueuedMessage.attachments, + states: appAtomRegistry.get(composerAttachmentUploadsAtom), + }) + ) { + continue; + } let thread = findThread(threads, nextQueuedMessage); if (thread && scopedThreadKey(thread.environmentId, thread.id) !== threadKey) { diff --git a/apps/mobile/src/state/use-thread-composer-state.ts b/apps/mobile/src/state/use-thread-composer-state.ts index 5796535029582..08b9e20b27f60 100644 --- a/apps/mobile/src/state/use-thread-composer-state.ts +++ b/apps/mobile/src/state/use-thread-composer-state.ts @@ -63,7 +63,7 @@ import { pruneCaughtUpAcknowledgedThreadMessages, } from "./acknowledged-thread-messages"; import { - appendPendingThreadMessages, + appendAcceptedThreadMessages, selectPendingAcknowledgedMessages, } from "../features/threads/pending-thread-feed"; import { threadAllowsProviderSwitch } from "./thread-provider-switching"; @@ -294,9 +294,10 @@ export function useThreadComposerState(input: { sequence: selectedThreadSequence, projectedMessages: selectedThreadMessages, }); - if (pendingAcknowledgments.length === 0) return feed; - return appendPendingThreadMessages(feed, feed, pendingAcknowledgments).map((entry) => - entry.pendingMessage ? { ...entry, acknowledged: true } : entry, + return appendAcceptedThreadMessages( + feed, + pendingAcknowledgments, + pendingCreation.length > 0 ? pendingCreationMessage : null, ); })(); From caef8c3847be4580aa035279e5a7f86e89e458a5 Mon Sep 17 00:00:00 2001 From: Marker-bit <63698566+Marker-bit@users.noreply.github.com> Date: Mon, 16 Nov 2026 20:43:10 +0300 Subject: [PATCH 023/253] fix(composer): show local command actions while running (#32912) Co-authored-by: maria-rcks Co-authored-by: Julius Marminge Co-authored-by: Marker-bit --- .../src/features/threads/ThreadComposer.tsx | 43 ++++++++++++------- apps/mobile/src/lib/composerContext.test.ts | 2 +- apps/mobile/src/lib/composerContext.ts | 4 +- apps/web/src/components/chat/ChatComposer.tsx | 24 ++++++++--- .../src/state/composerDispatch.test.ts | 26 ++++++++++- .../src/state/composerDispatch.ts | 23 ++++++++++ 6 files changed, 97 insertions(+), 25 deletions(-) diff --git a/apps/mobile/src/features/threads/ThreadComposer.tsx b/apps/mobile/src/features/threads/ThreadComposer.tsx index 27551a83afa41..3894ab61a4af3 100644 --- a/apps/mobile/src/features/threads/ThreadComposer.tsx +++ b/apps/mobile/src/features/threads/ThreadComposer.tsx @@ -30,7 +30,6 @@ import { hasProviderUsageLimits, isUsageLimitsCommand, } from "@t3tools/shared/usageLimits"; -import { parseSideChatCommand } from "@t3tools/shared/composerTrigger"; import { StackActions, useFocusEffect, useNavigation } from "@react-navigation/native"; import type { ReactNode } from "react"; import { useEffect, useEffectEvent, useRef, useState, type RefObject } from "react"; @@ -101,7 +100,10 @@ import { useComposerTextRole, type ScaledTextRole } from "../settings/appearance import type { RemoteClientConnectionState } from "../../lib/connection"; import { MOBILE_TYPOGRAPHY } from "../../lib/typography"; import { Menu } from "../../components/ui/Menu"; -import type { ActiveTurnComposerAction } from "@t3tools/client-runtime/state/composer-dispatch"; +import { + composerRunsDraftLocally, + type ActiveTurnComposerAction, +} from "@t3tools/client-runtime/state/composer-dispatch"; import type { FollowUpBehavior } from "../../lib/followUpBehavior"; import { resolveComposerSendPresentation, @@ -502,7 +504,11 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps const [previewVideo, setPreviewVideo] = useState(null); const composerOwnerKey = scopedThreadKey(props.environmentId, props.selectedThread.id); const composerDraftKey = props.draftKey ?? composerOwnerKey; - const { text: draftMessage, attachments: draftAttachments } = useComposerDraft(composerDraftKey); + const { + text: draftMessage, + attachments: draftAttachments, + context: draftContext, + } = useComposerDraft(composerDraftKey); const queuedEdit = props.queuedEdit ?? null; const hasContent = draftMessage.trim().length > 0 || @@ -534,18 +540,6 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps serverConfig: props.serverConfig, states: uploadStates, }); - const sendPresentation = resolveComposerSendPresentation({ - editingQueuedMessage: queuedEdit !== null, - // A side-chat command starts a side chat; it never queues or steers the running turn. - running: - props.activeThreadBusy && - (props.redirectsApproval || parseSideChatCommand(draftMessage) === null), - canSteer: props.canSteerActiveTurn, - followUpBehavior: props.followUpBehavior, - deliveryDeferred: - props.connectionState !== "connected" || props.queueCount > 0 || attachmentsUploading, - }); - const sendLabel = sendPresentation.label; const currentModelSelection = props.selectedThread.modelSelection; const modelUnavailable = props.connectionState === "connected" && @@ -582,6 +576,25 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps props.serverConfig?.providers ?? [], props.serverConfig?.usageLimitSources ?? [], ); + const draftRunsLocally = composerRunsDraftLocally({ + text: draftMessage, + holdsOnlyText: draftAttachments.length === 0, + // A redirect's text answers its approval, so `/side` there is that text. + sideChat: !props.redirectsApproval, + usageLimits: usageLimitsOffered, + codexFeedback: selectedProviderStatus?.driver === "codex", + freshSession: props.onFreshSession !== undefined && draftContext === undefined, + }); + const sendPresentation = resolveComposerSendPresentation({ + editingQueuedMessage: queuedEdit !== null, + running: props.activeThreadBusy && !draftRunsLocally, + canSteer: props.canSteerActiveTurn, + followUpBehavior: props.followUpBehavior, + deliveryDeferred: + !draftRunsLocally && + (props.connectionState !== "connected" || props.queueCount > 0 || attachmentsUploading), + }); + const sendLabel = sendPresentation.label; const openUsageLimits = () => { const report = collectProviderUsageLimits( currentModelSelection.instanceId, diff --git a/apps/mobile/src/lib/composerContext.test.ts b/apps/mobile/src/lib/composerContext.test.ts index 2e1ee64831d08..9d4d7274cfd22 100644 --- a/apps/mobile/src/lib/composerContext.test.ts +++ b/apps/mobile/src/lib/composerContext.test.ts @@ -154,7 +154,7 @@ describe("mobile composer context", () => { expect(restore("deleted", initial)).toBeUndefined(); expect(restore(source)?.records).toEqual(initial.records); expect(restore("deleted")?.records ?? []).toEqual([]); - expect(createComposerContextHistory()(source)?.records).toEqual([]); + expect(createComposerContextHistory()(source)).toBeUndefined(); }); it("chips a model reference only once it is complete and names a known model", () => { const known = new Set(["model:codex/gpt-6.1"]); diff --git a/apps/mobile/src/lib/composerContext.ts b/apps/mobile/src/lib/composerContext.ts index 53494209362af..d96704b9a61c2 100644 --- a/apps/mobile/src/lib/composerContext.ts +++ b/apps/mobile/src/lib/composerContext.ts @@ -233,8 +233,8 @@ export function referencedComposerContext(text: string, context?: OrchestrationM } } const records = context.records.filter((record) => ids.has(record.contextId)); - if (records.length === context.records.length) return context; - return records.length ? { version: 1 as const, records } : undefined; + if (records.length === 0) return undefined; + return records.length === context.records.length ? context : { version: 1 as const, records }; } /** Uploads change attachment ids; keep context bindings attached to the same ordered file. */ diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index 83bd2f63883e7..be3144efb40b9 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -53,7 +53,7 @@ import * as Schema from "effect/Schema"; import { formatComposerModelMention } from "@t3tools/client-runtime/composer-model-mentions"; import { isPasteAsTextShortcut } from "@t3tools/client-runtime/text-paste"; import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment"; -import { parseSideChatCommand, serializeComposerFileLink } from "@t3tools/shared/composerTrigger"; +import { serializeComposerFileLink } from "@t3tools/shared/composerTrigger"; import { type FolderDropTarget, folderDropTarget, resolveDroppedFolderPath } from "./folderDrop"; import { splitTransferredFiles } from "./workspaceFileDrop"; import { createModelSelection, normalizeModelSlug } from "@t3tools/shared/model"; @@ -253,6 +253,7 @@ import { type ComposerPromptHistoryPosition } from "./composerPromptHistory"; import { usePendingUserInputDraft } from "./usePendingThreadRequests"; import type { PendingApproval, PendingUserInput, TimelineEntry } from "../../session-logic"; import { + composerRunsDraftLocally, resolveComposerDispatchMode, type ComposerDispatchMode, } from "@t3tools/client-runtime/state/composer-dispatch"; @@ -2489,6 +2490,21 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) // An open question owns the space above the composer. Only a machine problem, // which is why an answer could not go through, stays beside it. const replyTo = composerDraft?.replyTo ?? null; + const draftRunsLocally = composerRunsDraftLocally({ + text: prompt, + holdsOnlyText: + composerImages.length + composerFiles.length === 0 && + composerSendState.sendableTerminalContexts.length === 0 && + composerPreviewAnnotations.length === 0 && + composerReviewComments.length === 0 && + composerThreadContexts.length === 0 && + composerEnvironmentContexts.length === 0 && + replyTo === null, + sideChat: props.sideChatAvailable === true || (props.sideChatDisabledReason ?? null) !== null, + usageLimits: props.onUsageLimitsCommand !== undefined, + codexFeedback: selectedProvider === "codex" && multipleModelSelections === null, + freshSession: props.onFreshSession !== undefined, + }); const replyBannerItems: readonly ComposerBannerStackItem[] = replyTo ? [ { @@ -3737,11 +3753,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) compact={isComposerPrimaryActionsCompact} canOperateThread={canOperateThread} pendingAction={pendingPrimaryAction} - // A side-chat command starts a side chat; it never queues or steers the turn. - isRunning={ - (phase === "running" || phase === "connecting") && - !(props.sideChatAvailable === true && parseSideChatCommand(prompt) !== null) - } + isRunning={(phase === "running" || phase === "connecting") && !draftRunsLocally} canInterrupt={canInterrupt} runId={activeThread?.runtime?.activeRunId ?? null} followUpBehavior={followUpBehavior} diff --git a/packages/client-runtime/src/state/composerDispatch.test.ts b/packages/client-runtime/src/state/composerDispatch.test.ts index 936ae33111ef4..2eb4728012a94 100644 --- a/packages/client-runtime/src/state/composerDispatch.test.ts +++ b/packages/client-runtime/src/state/composerDispatch.test.ts @@ -1,6 +1,6 @@ import { expect, it } from "vite-plus/test"; -import { compactionBlocksSteering } from "./composerDispatch.ts"; +import { compactionBlocksSteering, composerRunsDraftLocally } from "./composerDispatch.ts"; it("blocks steering for explicit compaction, and for automatic compaction unless the provider takes steers then", () => { const during = ( @@ -47,3 +47,27 @@ it("blocks steering for explicit compaction, and for automatic compaction unless }), ).toBe(false); }); + +it("treats only the commands the composer runs itself as local", () => { + const none = { + holdsOnlyText: true, + sideChat: false, + usageLimits: false, + codexFeedback: false, + freshSession: false, + }; + const all = { + ...none, + sideChat: true, + usageLimits: true, + codexFeedback: true, + freshSession: true, + }; + for (const text of ["/btw why?", "/usage-limits", "/feedback slow", "/clear"]) { + expect(composerRunsDraftLocally({ ...all, text })).toBe(true); + expect(composerRunsDraftLocally({ ...none, text })).toBe(false); + } + expect(composerRunsDraftLocally({ ...all, text: "fix the bug" })).toBe(false); + expect(composerRunsDraftLocally({ ...all, holdsOnlyText: false, text: "/clear" })).toBe(false); + expect(composerRunsDraftLocally({ ...all, holdsOnlyText: false, text: "/side why?" })).toBe(true); +}); diff --git a/packages/client-runtime/src/state/composerDispatch.ts b/packages/client-runtime/src/state/composerDispatch.ts index c24bb95628d97..3860bfc529ded 100644 --- a/packages/client-runtime/src/state/composerDispatch.ts +++ b/packages/client-runtime/src/state/composerDispatch.ts @@ -1,3 +1,8 @@ +import { isClearComposerCommand, parseSideChatCommand } from "@t3tools/shared/composerTrigger"; +import { isUsageLimitsCommand } from "@t3tools/shared/usageLimits"; + +import { parseCodexFeedbackCommand } from "./threadFeedback.ts"; + export type ComposerDispatchMode = "auto" | "queue" | "steer" | "restart"; export type ActiveTurnComposerAction = Exclude; @@ -49,3 +54,21 @@ export function alternateComposerDispatchAction( ...(activeTurnDefault === undefined ? {} : { activeTurnDefault }), }) as ActiveTurnComposerAction; } + +/** The composer runs these drafts itself, so they never queue for or steer a running turn. */ +export function composerRunsDraftLocally(input: { + readonly text: string; + readonly holdsOnlyText: boolean; + readonly sideChat: boolean; + readonly usageLimits: boolean; + readonly codexFeedback: boolean; + readonly freshSession: boolean; +}): boolean { + if (input.sideChat && parseSideChatCommand(input.text) !== null) return true; + if (!input.holdsOnlyText) return false; + return ( + (input.usageLimits && isUsageLimitsCommand(input.text)) || + (input.codexFeedback && parseCodexFeedbackCommand(input.text) !== null) || + (input.freshSession && isClearComposerCommand(input.text)) + ); +} From b0f0c44d228552eb1ce3d43f21b0272ce5fc985f Mon Sep 17 00:00:00 2001 From: Arav Jain Date: Mon, 16 Nov 2026 12:08:31 -0600 Subject: [PATCH 024/253] fix(shared): open Forgejo and Gitea /pulls/ tab and commit links (#37703) Co-authored-by: Julius Marminge --- packages/shared/src/pullRequestAnchor.test.ts | 3 +++ packages/shared/src/pullRequestAnchor.ts | 7 ++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/shared/src/pullRequestAnchor.test.ts b/packages/shared/src/pullRequestAnchor.test.ts index 6ea826924a992..08bb25e75f477 100644 --- a/packages/shared/src/pullRequestAnchor.test.ts +++ b/packages/shared/src/pullRequestAnchor.test.ts @@ -9,6 +9,7 @@ import { } from "./pullRequestAnchor.ts"; const gitlab = "https://gitlab.com/group/repo/-/merge_requests/7"; +const forgejo = "https://codeberg.org/owner/repo/pulls/7"; const url = "https://github.com/owner/repo/pull/7"; it.each(["discussion_r123", "r123", "discussion-diff-123R5-L9", "discussion-diff-123"])( @@ -107,6 +108,7 @@ it("handles encoded hashes and rejects invalid line numbers and URLs", () => { it("keeps the commit scope of a PR diff permalink", () => { const sha = "a".repeat(40); expect(pullRequestLinkCommit(`${url}/commits/${sha}#diff-abcR5`)).toBe(sha); + expect(pullRequestLinkCommit(`${forgejo}/commits/${sha}#diff-abcR5`)).toBe(sha); expect(pullRequestLinkCommit(`${url}/files#diff-abcR5`)).toBeNull(); expect(pullRequestLinkCommit(`${gitlab}/diffs?commit_id=${sha}#${"b".repeat(40)}`)).toBe(sha); expect(pullRequestLinkCommit(`${gitlab}/diffs?commit_id=main`)).toBeNull(); @@ -153,6 +155,7 @@ it.each([ [`${url}/commits/${"a".repeat(40)}`, "code"], [`${url}/commits`, "commits"], [`${url}/checks?check_run_id=1`, "checks"], + [`${forgejo}/files`, "code"], [`${gitlab}/diffs`, "code"], [`${gitlab}/commits`, "commits"], [`${gitlab}/pipelines`, "checks"], diff --git a/packages/shared/src/pullRequestAnchor.ts b/packages/shared/src/pullRequestAnchor.ts index a16e7ced305a3..3e02f0f383424 100644 --- a/packages/shared/src/pullRequestAnchor.ts +++ b/packages/shared/src/pullRequestAnchor.ts @@ -39,9 +39,10 @@ function normalizePullRequestCommentAnchor(anchor: string): string { ); } -// GitHub pull request tabs and their GitLab merge request equivalents. +// GitHub pull request tabs and their GitLab merge request equivalents. Forgejo and Gitea spell the +// path `/pulls/`. function pullRequestPathTab(pathname: string): PullRequestAnchor | null { - const tab = /\/(?:pull|-\/merge_requests)\/\d+\/([a-z]+)(?:\/(.*))?$/i.exec(pathname); + const tab = /\/(?:pulls?|-\/merge_requests)\/\d+\/([a-z]+)(?:\/(.*))?$/i.exec(pathname); const rest = tab?.[2] ?? ""; switch (tab?.[1]?.toLowerCase()) { case "files": @@ -206,7 +207,7 @@ export function pullRequestLinkCommit(url: string | null | undefined): string | const { pathname, searchParams } = new URL(url); const commit = /\/-\/merge_requests\/\d+\/diffs\/?$/i.test(pathname) ? searchParams.get("commit_id") - : /\/pull\/\d+\/commits\/([a-f0-9]{40})\/?$/i.exec(pathname)?.[1]; + : /\/pulls?\/\d+\/commits\/([a-f0-9]{40})\/?$/i.exec(pathname)?.[1]; return commit && /^[a-f0-9]{40}$/i.test(commit) ? commit : null; } catch { return null; From 0a842b222d5732e12451484098e035429dc0f315 Mon Sep 17 00:00:00 2001 From: Screen <228304862+screen-gd@users.noreply.github.com> Date: Mon, 16 Nov 2026 22:36:37 +0400 Subject: [PATCH 025/253] fix(web): list each pull request once across servers (#37704) Co-authored-by: screen Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Julius Marminge --- .../pullRequest/pullRequestList.logic.test.ts | 27 ++++++++++++++++--- .../pullRequest/pullRequestList.logic.ts | 23 +++++++++------- apps/web/src/state/pullRequests.test.ts | 2 +- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/apps/web/src/components/pullRequest/pullRequestList.logic.test.ts b/apps/web/src/components/pullRequest/pullRequestList.logic.test.ts index 29611bb44cece..dfa31d4dbcd6b 100644 --- a/apps/web/src/components/pullRequest/pullRequestList.logic.test.ts +++ b/apps/web/src/components/pullRequest/pullRequestList.logic.test.ts @@ -388,6 +388,18 @@ describe("partitioning with the hosts' own priority reads", () => { expect(groups[0]!.entries.map((item) => item.number)).toEqual([1]); expect(groups[1]!.entries.map((item) => item.number)).toEqual([2, 3]); }); + + it("lists a pull request once when another environment's authored read supplies it", () => { + const mine = authoredRow(4, "2026-07-01T00:00:00Z"); + const groups = partitionPullRequestsWithPriority( + [{ ...mine, environmentId: ENV_1 }], + [{ ...mine, environmentId: ENV_2 }], + [], + ); + expect(groups.map((group) => [group.key, group.entries])).toEqual([ + ["authored", [{ ...mine, environmentId: ENV_1 }]], + ]); + }); }); describe("remembered pull request list controls", () => { @@ -491,13 +503,20 @@ describe("merging the environments' own listings", () => { ...rows.map((row) => [ENV_1, answer({ entries: [row] })] as const), [ENV_2, answer({ entries: [older] })], ]); - expect(merged?.entries).toEqual([ - { ...newer, environmentId: ENV_1 }, - { ...older, environmentId: ENV_2 }, - ]); + expect(merged?.entries).toEqual([{ ...newer, environmentId: ENV_1 }]); }, ); + it("keeps a pull request two environments read on the first environment that answered", () => { + const older = entry({ number: 4, updatedAt: "2026-07-01T00:00:00Z" }); + const newer = entry({ number: 4, updatedAt: "2026-07-02T00:00:00Z" }); + const merged = mergePullRequestLists([ + [ENV_1, answer({ entries: [older] })], + [ENV_2, answer({ entries: [newer] })], + ]); + expect(merged?.entries).toEqual([{ ...older, environmentId: ENV_1 }]); + }); + it("keeps Azure pull requests from distinct projects with the same repository name and number", () => { const first = entry({ provider: SourceControlProviderKind.make("azure-devops"), diff --git a/apps/web/src/components/pullRequest/pullRequestList.logic.ts b/apps/web/src/components/pullRequest/pullRequestList.logic.ts index 7688183833060..0345e5fd82d07 100644 --- a/apps/web/src/components/pullRequest/pullRequestList.logic.ts +++ b/apps/web/src/components/pullRequest/pullRequestList.logic.ts @@ -453,16 +453,16 @@ export function partitionPullRequestsWithPriority, reviewRequested: ReadonlyArray, ): ReadonlyArray> { - const authoredByKey = new Map(authored.map((entry) => [pullRequestEntryKey(entry), entry])); + // Matched by URL: each list may hold the pull request under a different environment. + const authoredByKey = new Map(authored.map((entry) => [entry.url, entry])); const reviewByKey = new Map( - reviewRequested.flatMap((entry) => { - const key = pullRequestEntryKey(entry); - return authoredByKey.has(key) ? [] : [[key, entry] as const]; - }), + reviewRequested.flatMap((entry) => + authoredByKey.has(entry.url) ? [] : [[entry.url, entry] as const], + ), ); const others: Entry[] = []; for (const entry of entries) { - const key = pullRequestEntryKey(entry); + const key = entry.url; if (authoredByKey.has(key)) { authoredByKey.set(key, entry); } else if (reviewByKey.has(key)) { @@ -561,10 +561,13 @@ export function mergePullRequestLists( ); } for (const entry of answer.entries) { - const key = JSON.stringify([environmentId, entry.url]); - const held = entries.get(key); - if (held === undefined || entry.updatedAt > held.updatedAt) { - entries.set(key, { ...entry, environmentId }); + const held = entries.get(entry.url); + // The first environment to answer keeps a pull request that several of them read. + if ( + held === undefined || + (held.environmentId === environmentId && entry.updatedAt > held.updatedAt) + ) { + entries.set(entry.url, { ...entry, environmentId }); } } errors.push(...answer.errors.map((error) => ({ ...error, environmentId }))); diff --git a/apps/web/src/state/pullRequests.test.ts b/apps/web/src/state/pullRequests.test.ts index 5c47ff8666185..c0839ff0a728e 100644 --- a/apps/web/src/state/pullRequests.test.ts +++ b/apps/web/src/state/pullRequests.test.ts @@ -438,7 +438,7 @@ describe("pull request list answers across a reload", () => { { environmentId: remote, input }, ]); expect(restored?.entries.map((row) => [row.environmentId, row.updatedAt])).toEqual([ - [remote, "2026-07-03T00:00:00Z"], + [local, "2026-07-01T00:00:00Z"], ]); }); From 1d1489d0b8e7448b7f020fc9a6325f593caf147e Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 10:43:10 -0800 Subject: [PATCH 026/253] fix(web): keyboard-opened sidebar menus select their first item (#37727) --- .../src/components/Sidebar.browser.test.tsx | 42 +++++++++++++++++++ apps/web/src/components/Sidebar.tsx | 19 ++++++--- .../components/sidebar/SidebarDraftBlock.tsx | 18 ++++++-- .../sidebar/SidebarProjectFolderHeader.tsx | 14 ++++++- .../components/sidebar/SidebarThreadRow.tsx | 10 ++++- apps/web/src/lib/sidebarTree.ts | 5 ++- 6 files changed, 94 insertions(+), 14 deletions(-) diff --git a/apps/web/src/components/Sidebar.browser.test.tsx b/apps/web/src/components/Sidebar.browser.test.tsx index be674b66520d2..b99591c92b112 100644 --- a/apps/web/src/components/Sidebar.browser.test.tsx +++ b/apps/web/src/components/Sidebar.browser.test.tsx @@ -6,6 +6,7 @@ import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell import { DEFAULT_CLIENT_SETTINGS, EnvironmentId, + type LocalApi, ProjectId, ThreadId, type ScopedThreadRef, @@ -25,6 +26,7 @@ import { useProjects, useServerConfigs, useQuietThreadShells } from "../state/en import { readEnvironmentScope, useEnvironmentScope } from "../state/session"; import { useOrchestrationCommand } from "../state/use-orchestration-command"; import { useTerminalUiStateStore } from "../terminalUiStateStore"; +import { readLocalApi } from "../localApi"; import type { Project } from "../types"; import Sidebar from "./Sidebar"; import { THREAD_JUMP_HINT_SHOW_DELAY_MS } from "./Sidebar.logic"; @@ -42,6 +44,7 @@ vi.mock("../hooks/useThreadRouteTarget", { spy: true }); vi.mock("./sidebar/SidebarThreadRow", { spy: true }); vi.mock("../state/session", { spy: true }); vi.mock("../state/use-orchestration-command", { spy: true }); +vi.mock("../localApi", { spy: true }); vi.mock(import("../state/server"), async (original) => { const { Atom } = await import("effect/reactivity"); const { compileResolvedKeybindingsConfig } = await import("@t3tools/shared/keybindings"); @@ -261,6 +264,45 @@ describe("sidebar thread rename", () => { ); }); +describe("sidebar thread menu", () => { + it("tells the menu whether the keyboard opened it", async () => { + const activeProject = project("active"); + const thread = threadShell(activeProject.id, 0); + const threadRef = { environmentId: activeProject.environmentId, threadId: thread.id }; + const show = vi.fn().mockResolvedValue(null); + vi.mocked(readLocalApi).mockReturnValue({ contextMenu: { show } } as unknown as LocalApi); + vi.mocked(useProjects).mockReturnValue([activeProject]); + vi.mocked(useQuietThreadShells).mockReturnValue([thread]); + vi.mocked(useThreadRouteTarget).mockReturnValue({ kind: "server", threadRef }); + vi.mocked(useHandleNewThread).mockReturnValue({ + activeDraftThread: null, + activeThread: null, + defaultProjectRef: { + environmentId: activeProject.environmentId, + projectId: activeProject.id, + }, + defaultProjectFallbackReady: true, + handleNewThread: vi.fn().mockResolvedValue(null), + routeDraftId: null, + routeThreadRef: threadRef, + }); + await using screen = await render( + + + , + ); + const row = screen.getByText("Thread 0").first(); + await expect.element(row).toBeInTheDocument(); + row.element().closest("[data-sidebar-row]")!.focus(); + await userEvent.keyboard("{Shift>}{F10}{/Shift}"); + await vi.waitFor(() => expect(show).toHaveBeenCalledTimes(1)); + expect(show.mock.lastCall?.[2]).toBe("keyboard"); + await userEvent.click(row, { button: "right" }); + await vi.waitFor(() => expect(show).toHaveBeenCalledTimes(2)); + expect(show.mock.lastCall?.[2]).toBe("mouse"); + }); +}); + describe("sidebar settled pagination", () => { it("keeps the viewport in place when showing more settled threads", async () => { using stack = new DisposableStack(); diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsx index 3cb77464473b4..e6cdb90d5c86d 100644 --- a/apps/web/src/components/Sidebar.tsx +++ b/apps/web/src/components/Sidebar.tsx @@ -55,6 +55,7 @@ import { } from "@t3tools/client-runtime/environment"; import { AuthOrchestrationOperateScope, + type ContextMenuSource, ProjectId, type ScopedThreadRef, type ThreadId, @@ -1250,7 +1251,11 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve void handleNewThread(scopeProjectRef(group.environmentId, group.id)); }); const handleProjectFolderContextMenu = useStableCallback( - (group: SidebarProjectSnapshot, position: { x: number; y: number }) => { + ( + group: SidebarProjectSnapshot, + position: { x: number; y: number }, + source: ContextMenuSource, + ) => { const api = readLocalApi(); if (!api) return; void (async () => { @@ -1269,6 +1274,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve { id: "project-settings", label: "Project settings", icon: "settings" }, ], position, + source, ), ); if (clicked._tag === "Failure") return; @@ -2283,7 +2289,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve ); const handleMultiSelectContextMenu = useStableCallback( - async (position: { x: number; y: number }) => { + async (position: { x: number; y: number }, source: ContextMenuSource) => { const api = readLocalApi(); if (!api) return; const selectedThreadKeys = [...useThreadSelectionStore.getState().selectedThreadKeys]; @@ -2381,6 +2387,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve }, ], position, + source, ), ); if (clicked._tag === "Failure" || clicked.value === null) return; @@ -2534,7 +2541,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve ); const handleDraftContextMenu = useStableCallback( - (draftId: DraftId, position: { x: number; y: number }) => { + (draftId: DraftId, position: { x: number; y: number }, source: ContextMenuSource) => { void (async () => { const api = readLocalApi(); const session = useComposerDraftStore.getState().getDraftSession(draftId); @@ -2556,6 +2563,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve hasProject: projectGroup != null, }), position, + source, ), ); if (clicked._tag === "Failure") return; @@ -2585,14 +2593,14 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve ); const handleThreadContextMenu = useStableCallback( - (threadRef: ScopedThreadRef, position: { x: number; y: number }) => { + (threadRef: ScopedThreadRef, position: { x: number; y: number }, source: ContextMenuSource) => { void (async () => { const api = readLocalApi(); if (!api) return; const threadKey = scopedThreadKey(threadRef); const selectionState = useThreadSelectionStore.getState(); if (selectionState.hasSelection() && selectionState.selectedThreadKeys.has(threadKey)) { - await handleMultiSelectContextMenu(position); + await handleMultiSelectContextMenu(position, source); return; } const thread = threadByKeyRef.current.get(threadKey); @@ -2659,6 +2667,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve snoozePresets, }), position, + source, ), ); if (clicked._tag === "Failure" || clicked.value === null) return; diff --git a/apps/web/src/components/sidebar/SidebarDraftBlock.tsx b/apps/web/src/components/sidebar/SidebarDraftBlock.tsx index 7ac90c0d48219..ac126ae423a45 100644 --- a/apps/web/src/components/sidebar/SidebarDraftBlock.tsx +++ b/apps/web/src/components/sidebar/SidebarDraftBlock.tsx @@ -1,4 +1,6 @@ import { discardComposerDraft } from "../../lib/discardComposerDraft"; +import type { ContextMenuSource } from "@t3tools/contracts"; +import { contextMenuSourceOf } from "../../contextMenuSource"; import { assistantCitationsToPlainText } from "@t3tools/shared/assistantCitations"; import { replaceComposerContextReferences } from "@t3tools/shared/composerContextReferences"; import { SquarePenIcon, XIcon } from "lucide-react"; @@ -31,7 +33,11 @@ const SidebarDraftRow = memo(function SidebarDraftRow(props: { isActive: boolean; onNavigate: (draftId: DraftId) => void; onDiscard: (draftId: DraftId) => void; - onContextMenu: (draftId: DraftId, position: { x: number; y: number }) => void; + onContextMenu: ( + draftId: DraftId, + position: { x: number; y: number }, + source: ContextMenuSource, + ) => void; }) { const { composer, draftId, onContextMenu, onDiscard, onNavigate } = props; const promptPreview = @@ -67,7 +73,7 @@ const SidebarDraftRow = memo(function SidebarDraftRow(props: { if (event.key === "ContextMenu" || (event.shiftKey && event.key === "F10")) { event.preventDefault(); const rect = event.currentTarget.getBoundingClientRect(); - onContextMenu(draftId, { x: rect.left, y: rect.bottom }); + onContextMenu(draftId, { x: rect.left, y: rect.bottom }, "keyboard"); } else if (event.key === "Enter" || event.key === " ") { event.preventDefault(); onNavigate(draftId); @@ -75,7 +81,7 @@ const SidebarDraftRow = memo(function SidebarDraftRow(props: { }; const handleContextMenu = (event: ReactMouseEvent) => { event.preventDefault(); - onContextMenu(draftId, { x: event.clientX, y: event.clientY }); + onContextMenu(draftId, { x: event.clientX, y: event.clientY }, contextMenuSourceOf(event)); }; const handleDiscard = (event: ReactMouseEvent) => { event.preventDefault(); @@ -145,7 +151,11 @@ export const SidebarDraftBlock = memo(function SidebarDraftBlock(props: { scopedProjectKeys: ReadonlySet | null; routeDraftId: string | null; onNavigateToDraft: (draftId: DraftId) => void; - onDraftContextMenu: (draftId: DraftId, position: { x: number; y: number }) => void; + onDraftContextMenu: ( + draftId: DraftId, + position: { x: number; y: number }, + source: ContextMenuSource, + ) => void; }) { const draftThreadsByThreadKey = useComposerDraftStore((store) => store.draftThreadsByThreadKey); const draftsByThreadKey = useComposerDraftStore( diff --git a/apps/web/src/components/sidebar/SidebarProjectFolderHeader.tsx b/apps/web/src/components/sidebar/SidebarProjectFolderHeader.tsx index 1e6510bbe37e4..b438db4e6a111 100644 --- a/apps/web/src/components/sidebar/SidebarProjectFolderHeader.tsx +++ b/apps/web/src/components/sidebar/SidebarProjectFolderHeader.tsx @@ -1,9 +1,11 @@ import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/models"; import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment"; +import type { ContextMenuSource } from "@t3tools/contracts"; import { ChevronDownIcon, SquarePenIcon } from "lucide-react"; import { memo } from "react"; import { cn } from "~/lib/utils"; +import { contextMenuSourceOf } from "../../contextMenuSource"; import type { SidebarProjectSnapshot } from "../../sidebarProjectGrouping"; import { useUiStateStore } from "../../uiStateStore"; import { resolveSidebarFolderDot, resolveThreadLastVisitedAt } from "../Sidebar.logic"; @@ -41,7 +43,11 @@ export const SidebarProjectFolderHeader = memo(function SidebarProjectFolderHead hiddenThreads: readonly EnvironmentThreadShell[]; onToggle: (group: SidebarProjectSnapshot) => void; onNewThread: (group: SidebarProjectSnapshot) => void; - onContextMenu: (group: SidebarProjectSnapshot, position: { x: number; y: number }) => void; + onContextMenu: ( + group: SidebarProjectSnapshot, + position: { x: number; y: number }, + source: ContextMenuSource, + ) => void; }) { const { group, collapsed, hiddenThreads } = props; const rowId = sidebarFolderRowId(group.projectKey); @@ -71,7 +77,11 @@ export const SidebarProjectFolderHeader = memo(function SidebarProjectFolderHead onClick={() => props.onToggle(group)} onContextMenu={(event) => { event.preventDefault(); - props.onContextMenu(group, { x: event.clientX, y: event.clientY }); + props.onContextMenu( + group, + { x: event.clientX, y: event.clientY }, + contextMenuSourceOf(event), + ); }} className="flex h-8 w-full cursor-pointer items-center gap-2.5 rounded-(--control-radius) px-(--sidebar-row-content-inset) text-left outline-none hover:bg-sidebar-row-hover focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring" > diff --git a/apps/web/src/components/sidebar/SidebarThreadRow.tsx b/apps/web/src/components/sidebar/SidebarThreadRow.tsx index 641bc0cfa00ba..d901418db85a5 100644 --- a/apps/web/src/components/sidebar/SidebarThreadRow.tsx +++ b/apps/web/src/components/sidebar/SidebarThreadRow.tsx @@ -3,6 +3,7 @@ import { asLaunchingThread } from "@t3tools/client-runtime/state/thread-inbox"; import { ThreadHoverCard, ThreadHoverCardPopup } from "../ThreadHoverCard"; import { ThreadTerminalStatusButton } from "../ThreadStatusIndicators"; import { discardComposerDraft } from "../../lib/discardComposerDraft"; +import { contextMenuSourceOf } from "../../contextMenuSource"; import { requestCustomSnooze } from "../CustomSnoozeDialog"; import { useSupportsMultiplePullRequests } from "~/hooks/useSupportsMultiplePullRequests"; import { useDeadlinePassed } from "~/hooks/useDeadline"; @@ -29,6 +30,7 @@ import { scopeThreadRef, scopedThreadKey } from "@t3tools/client-runtime/environ import { toServerClock } from "@t3tools/client-runtime/clock"; import { AuthOrchestrationOperateScope, + type ContextMenuSource, type EnvironmentId, type EnvironmentMachineKind, type ScopedThreadRef, @@ -587,7 +589,11 @@ type SidebarThreadRowProps = { onCancelRename: () => void; isRenaming: boolean; renamingTitle: string; - onContextMenu: (threadRef: ScopedThreadRef, position: { x: number; y: number }) => void; + onContextMenu: ( + threadRef: ScopedThreadRef, + position: { x: number; y: number }, + source: ContextMenuSource, + ) => void; onSettle: (threadRef: ScopedThreadRef) => void; onActionSweepStart: ( threadRef: ScopedThreadRef, @@ -861,7 +867,7 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowP }; const handleContextMenu = (event: ReactMouseEvent) => { event.preventDefault(); - onContextMenu(threadRef, { x: event.clientX, y: event.clientY }); + onContextMenu(threadRef, { x: event.clientX, y: event.clientY }, contextMenuSourceOf(event)); }; const handleKeyDown = (event: ReactKeyboardEvent) => { if (event.target !== event.currentTarget) return; diff --git a/apps/web/src/lib/sidebarTree.ts b/apps/web/src/lib/sidebarTree.ts index 7a6183b5e4acb..6440a85461557 100644 --- a/apps/web/src/lib/sidebarTree.ts +++ b/apps/web/src/lib/sidebarTree.ts @@ -155,9 +155,12 @@ export function installSidebarTree(root: HTMLElement) { // Like VS Code's list, suppress keydown and open on release. Reuse the row's // selection-aware handler, including its Electron native menu route. row.dispatchEvent( - new MouseEvent("contextmenu", { + // A PointerEvent, as Chromium sends, keeps button -1 (a MouseEvent clamps it to 0), + // which contextMenuSourceOf reads as a keyboard-opened menu. + new PointerEvent("contextmenu", { bubbles: true, cancelable: true, + button: -1, clientX: rect.left, clientY: rect.bottom, }), From 4247b5aa1a1d0e4115a9bcf3860f24e6f435fb52 Mon Sep 17 00:00:00 2001 From: Utkarsh Patil <73941998+UtkarshUsername@users.noreply.github.com> Date: Tue, 17 Nov 2026 00:57:50 +0530 Subject: [PATCH 027/253] feat(voice): add a native streaming transcription toggle (#34380) --- apps/server/src/speech/SpeechService.ts | 7 +- apps/server/src/speech/native.test.ts | 22 ++ apps/server/src/speech/native.ts | 4 +- .../VoiceSettingsPanel.browser.test.tsx | 28 +- .../settings/VoiceSettingsPanel.tsx | 26 +- .../src/components/settings/settingsSearch.ts | 6 + .../speech/browserVoiceInput.browser.test.ts | 250 ++++++++++++++++++ apps/web/src/speech/browserVoiceInput.ts | 9 +- .../src/speech/useEnvironmentSpeechInput.ts | 1 + docs/user/voice-input.md | 5 +- packages/contracts/src/settings.test.ts | 5 + packages/contracts/src/settings.ts | 2 + 12 files changed, 353 insertions(+), 12 deletions(-) create mode 100644 apps/web/src/speech/browserVoiceInput.browser.test.ts diff --git a/apps/server/src/speech/SpeechService.ts b/apps/server/src/speech/SpeechService.ts index b4ae198b9447d..58fc6a9e1f5d5 100644 --- a/apps/server/src/speech/SpeechService.ts +++ b/apps/server/src/speech/SpeechService.ts @@ -582,8 +582,9 @@ const make = Effect.gen(function* () { const silenceModelPath = Effect.fn("SpeechService.silenceModelPath")(function* ( loaded: LoadedModel, options: SpeechTranscriptionOptions, + nativeStreaming: boolean, ) { - if (!options.speechSilenceFiltering && (loaded.supportsStreaming || loaded.supportsLongForm)) + if (!options.speechSilenceFiltering && (nativeStreaming || loaded.supportsLongForm)) return undefined; return yield* acquireSilenceModel("recording"); }); @@ -619,7 +620,7 @@ const make = Effect.gen(function* () { ), catch: (cause) => speechError("model preparation", cause), }); - const vadPath = yield* silenceModelPath(loaded, options); + const vadPath = yield* silenceModelPath(loaded, options, false); const dictionary = normalizeSpeechCustomWords(options.speechCustomWords); const customWords = dictionary.map(({ term }) => term); // The vocabulary capability guarantees plain transcription, not translation. @@ -755,7 +756,7 @@ const make = Effect.gen(function* () { loadModel(modelPath, definition, signal, settings.speechAcceleration), ); loaded = streamModel; - const vadPath = yield* silenceModelPath(streamModel, options); + const vadPath = yield* silenceModelPath(streamModel, options, streamModel.supportsStreaming); let translateToEnglish = false; const beginStream = async () => { translateToEnglish = diff --git a/apps/server/src/speech/native.test.ts b/apps/server/src/speech/native.test.ts index a41f917b2a682..cb7254891a51b 100644 --- a/apps/server/src/speech/native.test.ts +++ b/apps/server/src/speech/native.test.ts @@ -299,6 +299,28 @@ it("decodes complete stable chunks before Stop and returns the ordered final tai } }); +it("uses bounded offline chunks for a native streaming model without opening a session", async () => { + const moduleUrl = `data:text/javascript,${encodeURIComponent(` + export const TranscribeModel = { load: async () => ({ + capabilities: { supportsStreaming: true, maxAudioMs: 4000 }, + supports: () => false, + createSession: () => { throw new Error("Offline inference must not open a stream"); }, + transcribe: async pcm => { + if (pcm.length > 32000) throw new Error("Model input limit exceeded"); + return { text: String(pcm.length) }; + }, + }) }; + `)}`; + const model = await loadNativeSpeechModel("asr.gguf", new AbortController().signal, moduleUrl); + try { + expect( + await model.transcribe(new Float32Array(48002).fill(0.8), { timestamps: "none" }, false), + ).toEqual({ text: "32000 16002" }); + } finally { + await model.dispose(); + } +}); + it("preserves withheld audio on VAD failure while keeping ASR failures distinct", async () => { for (const streaming of [false, true]) { const model = await loadNativeSpeechModel( diff --git a/apps/server/src/speech/native.ts b/apps/server/src/speech/native.ts index 70c5d21c6038d..2bd2631c63fba 100644 --- a/apps/server/src/speech/native.ts +++ b/apps/server/src/speech/native.ts @@ -28,7 +28,7 @@ const resetAsr = () => { stream = session = undefined; }; const newAudio = (filterSilence) => createSpeechAudio({ - frameSamples: 512, filterSilence, nativeStreaming: model.capabilities.supportsStreaming, + frameSamples: 512, filterSilence, nativeStreaming, longForm: model.supports("long_form"), maxAudioMs: model.capabilities.maxAudioMs, }); const openAsr = async () => { @@ -45,7 +45,7 @@ const begin = async (message, live) => { scored = revision = 0; heardAudio = false; useVad = Boolean(message.vadPath) && (message.filterSilence || - (!model.capabilities.supportsStreaming && !model.supports("long_form"))); + (!nativeStreaming && !model.supports("long_form"))); if (useVad) { try { if (!vadModel) { diff --git a/apps/web/src/components/settings/VoiceSettingsPanel.browser.test.tsx b/apps/web/src/components/settings/VoiceSettingsPanel.browser.test.tsx index 98558bc26b4be..4c8172333ff98 100644 --- a/apps/web/src/components/settings/VoiceSettingsPanel.browser.test.tsx +++ b/apps/web/src/components/settings/VoiceSettingsPanel.browser.test.tsx @@ -14,6 +14,7 @@ import { expect, it, vi } from "vite-plus/test"; import { userEvent } from "vite-plus/test/browser"; import { render } from "../../../test/render"; import { __setClientSettingsForTests, getClientSettings } from "../../hooks/useSettings"; +import { readBrowserClientSettings } from "../../clientPersistenceStorage"; import { chooseRecordingMicrophone } from "../../speech/browserMicrophones"; import { useEnvironmentSpeechInput, @@ -121,7 +122,7 @@ it.each([{ cpu: null, accelerated: 2.1 }, { cpu: 5, accelerated: null }, { accel modelId: "both", model: "Bilingual", size: 100, - supportsStreaming: false, + supportsStreaming: true, supportsTranslation: false, acceleration: "auto", modelUnloadTimeout: "never", @@ -171,6 +172,10 @@ it.each([{ cpu: null, accelerated: 2.1 }, { cpu: 5, accelerated: null }, { accel prepared, }), async () => { + const previous = getClientSettings(); + using cleanup = new DisposableStack(); + cleanup.defer(() => __setClientSettingsForTests(previous)); + __setClientSettingsForTests({ ...previous, speechStreaming: true }); const panel = () => ( {}}> @@ -178,6 +183,13 @@ it.each([{ cpu: null, accelerated: 2.1 }, { cpu: 5, accelerated: null }, { accel ); await using screen = await render(panel()); await expect.element(screen.getByText(cpuBenchmark)).toBeVisible(); + await userEvent.click(screen.getByRole("button", { name: "Advanced", exact: true })); + const streaming = screen.getByRole("switch", { name: "Streaming transcription" }); + await expect.element(streaming).toBeEnabled(); + await expect.element(streaming).toBeChecked(); + streaming.element().focus(); + await userEvent.keyboard(" "); + await vi.waitFor(() => expect(readBrowserClientSettings()?.speechStreaming).toBe(false)); prepared = remote; await screen.rerender(panel()); await remoteRequested.promise; @@ -185,15 +197,28 @@ it.each([{ cpu: null, accelerated: 2.1 }, { cpu: 5, accelerated: null }, { accel remoteStatus.resolve( Response.json({ ...status, + supportsStreaming: false, benchmarkTier: "accelerated", gpuDevices: [{ id: "gpu", name: "Remote GPU" }], }), ); await expect.element(screen.getByText(gpuBenchmark)).toBeVisible(); + await userEvent.click(screen.getByRole("button", { name: "Advanced", exact: true })); + await expect.element(streaming).toBeDisabled(); + await expect.element(streaming).not.toBeChecked(); + await expect + .element(screen.getByText("This model doesn’t support native streaming.")) + .toBeVisible(); prepared = local; await screen.rerender(panel()); await expect.element(screen.getByText(cpuBenchmark)).toBeVisible(); + await userEvent.click(screen.getByRole("button", { name: "Advanced", exact: true })); await expect.element(screen.getByText(gpuBenchmark)).not.toBeInTheDocument(); + await expect.element(streaming).toBeEnabled(); + await expect.element(streaming).not.toBeChecked(); + streaming.element().focus(); + await userEvent.keyboard(" "); + await vi.waitFor(() => expect(readBrowserClientSettings()?.speechStreaming).toBe(true)); }, ); }, @@ -517,6 +542,7 @@ it("recovers from a failed download progress poll without notifying, but reports it.each([ ["speech-models", "voice-transcription-destination"], ["speech-hardware", "voice-transcription-destination"], + ["speech-streaming", "voice-transcription-destination"], ["transcription-test", "voice-transcription-destination"], ["speech-correction-word", "speech-post-processing"], ["speech-post-processing-model", "speech-post-processing"], diff --git a/apps/web/src/components/settings/VoiceSettingsPanel.tsx b/apps/web/src/components/settings/VoiceSettingsPanel.tsx index 66cacf6eb7ee9..91d73a0d07f15 100644 --- a/apps/web/src/components/settings/VoiceSettingsPanel.tsx +++ b/apps/web/src/components/settings/VoiceSettingsPanel.tsx @@ -728,11 +728,34 @@ function AdvancedSpeechSettings({ prepared: PreparedConnection; catalog: VoiceModels; }) { + const settings = useClientSettings(); const { run, controlsDisabled, status } = catalog; if (!status?.supported) return null; const supported = status; return ( - + + void updateClientSettings({ speechStreaming: checked })} + /> + } + /> { + const [{ Atom }, { layerRemoteHttpClient }] = await Promise.all([ + import("effect/reactivity"), + import("@t3tools/client-runtime/rpc"), + ]); + return { + connectionAtomRuntime: Atom.runtime( + layerRemoteHttpClient((input, init) => globalThis.fetch(input, init)), + ), + }; +}); + +function capture() { + let receive: ((event: { data: Float32Array | "stopped" }) => void) | undefined; + const stopTrack = vi.fn(); + const resources = new DisposableStack(); + resources.use( + stubGlobal( + "AudioContext", + class { + audioWorklet = { addModule: async () => {} }; + createMediaStreamSource() { + return { connect() {} }; + } + resume = async () => {}; + close = async () => {}; + }, + ), + ); + resources.use( + stubGlobal( + "AudioWorkletNode", + class { + port = { + set onmessage(handler: typeof receive) { + receive = handler; + }, + postMessage(message: string) { + if (message === "stop") receive?.({ data: "stopped" }); + }, + close() {}, + }; + connect() {} + disconnect() {} + }, + ), + ); + resources.use( + vi.spyOn(navigator.mediaDevices, "getUserMedia").mockResolvedValue({ + getTracks: () => [{ stop: stopTrack, addEventListener() {} }], + } as unknown as MediaStream), + ); + return { + feed: (data: Float32Array) => receive?.({ data }), + stopTrack, + [Symbol.dispose]: () => resources.dispose(), + }; +} + +const target = new BearerConnectionTarget({ + environmentId: EnvironmentId.make("remote-transcription"), + label: "Remote transcription", + connectionId: "remote", +}); +const prepared = { + environmentId: target.environmentId, + label: target.label, + target, + httpBaseUrl: "https://transcription.example.test", + socketUrl: "wss://transcription.example.test/ws", + httpAuthorization: { _tag: "Bearer" as const, token: "audio-grant" }, +}; +const status = { + supported: true, + state: "ready", + modelId: "native-streaming", + model: "Native streaming", + size: 1, + supportsStreaming: true, + supportsBufferedStreaming: true, + supportsTranslation: true, + languages: ["en", "fr"], + supportsLanguageDetection: false, + acceleration: "cpu", + modelUnloadTimeout: "never", + gpuDevices: [], + benchmarkTier: "cpu", +} satisfies EnvironmentSpeechStatus; + +it("retains all PCM until stop, retries batch audio, and captures preferences before status resolves", async () => { + using microphone = capture(); + let streaming = false; + let options = { ...DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS, speechLanguage: "fr" }; + const requests: Array<{ pcm: Uint8Array; options: typeof options }> = []; + let resolveStatus!: (response: Response) => void; + const pendingStatus = new Promise((resolve) => { + resolveStatus = resolve; + }); + const fetch = globalThis.fetch; + using _network = vi.spyOn(globalThis, "fetch").mockImplementation(async (input, init) => { + const request = new Request(input, init); + if (request.url === `${prepared.httpBaseUrl}/api/voice/status`) + return (await pendingStatus).clone(); + if (request.url === `${prepared.httpBaseUrl}/api/voice/transcribe`) { + expect(request.headers.get("authorization")).toBe("Bearer audio-grant"); + requests.push(decodeSpeechPcmRequest(new Uint8Array(await request.arrayBuffer()))); + return requests.length === 1 + ? new Response(null, { status: 503 }) + : Response.json({ text: "bonjour" }); + } + return fetch(input, init); + }); + const onError = vi.fn(); + const platform = createBrowserVoiceInputPlatform({ + prepared, + hasAudioConsent: () => true, + getStreamingEnabled: () => streaming, + getTranscriptionOptions: () => options, + getMicrophoneId: () => "", + onLevel() {}, + onDurationLimit() {}, + onText() {}, + onError, + }); + const commitDraft = vi.fn(); + const controller = new VoiceInputController({ + recorder: platform.recorder, + retryFailedRecording: true, + getTranscriber: () => platform.transcriber, + requestPermission: async () => ({ granted: true, canAskAgain: true }), + configureRecording: async () => {}, + releaseRecording: async () => platform.cancelRecording(), + deleteRecording: platform.deleteRecording, + readDraft: () => ({ + ownerKey: "draft", + text: "", + selection: { start: 0, end: 0 }, + revision: 0, + }), + commitDraft, + onStateChange() {}, + }); + using _controller = { [Symbol.dispose]: () => controller.dispose() }; + const start = controller.start(); + await vi.waitFor(() => expect(_network).toHaveBeenCalled()); + streaming = true; + options = { ...options, speechLanguage: "en" }; + resolveStatus(Response.json(status)); + await start; + expect(controller.currentState.phase).toBe("recording"); + microphone.feed(new Float32Array([0.1, 0.2])); + microphone.feed(new Float32Array([0.3])); + expect(requests).toHaveLength(0); + await controller.stop(); + expect(controller.currentState.phase).toBe("error"); + expect(platform.recorder.uri).not.toBeNull(); + await controller.start(); + expect(controller.currentState.phase).toBe("idle"); + expect(commitDraft).toHaveBeenCalledWith("bonjour", { start: 7, end: 7 }); + expect(requests).toHaveLength(2); + for (const request of requests) { + expect([...new Float32Array(request.pcm.slice().buffer)]).toEqual([ + Math.fround(0.1), + Math.fround(0.2), + Math.fround(0.3), + ]); + expect(request.options.speechLanguage).toBe("fr"); + } + expect(platform.recorder.uri).toBeNull(); + expect(microphone.stopTrack).toHaveBeenCalledOnce(); + expect(onError).not.toHaveBeenCalled(); + streaming = false; + await controller.start(); + microphone.feed(new Float32Array([0.4])); + controller.cancel(); + await vi.waitFor(() => expect(microphone.stopTrack).toHaveBeenCalledTimes(2)); + expect(requests).toHaveLength(2); + expect(platform.recorder.uri).toBeNull(); +}); + +it.each([ + { native: true, enabled: true, buffered: true, live: true }, + { native: true, enabled: false, buffered: true, live: false }, + { native: false, enabled: false, buffered: true, live: true }, + { native: false, enabled: true, buffered: true, live: true }, + { native: false, enabled: false, buffered: false, live: false }, +])( + "selects the destination model's processing path: %j", + async ({ native, enabled, buffered, live }) => { + const sockets: string[] = []; + using _socket = stubGlobal( + "WebSocket", + class extends EventTarget { + constructor(url: string) { + super(); + sockets.push(url); + } + close() {} + }, + ); + using _network = vi.spyOn(globalThis, "fetch").mockImplementation(async (input, init) => { + const request = new Request(input, init); + if (request.url.endsWith("/api/voice/status")) + return Response.json({ + ...status, + supportsStreaming: native, + supportsBufferedStreaming: buffered, + }); + if (request.url.includes("/api/auth/websocket-ticket/")) + return Response.json({ ticket: "stream-ticket", expiresAt: "2030-01-01T00:00:00.000Z" }); + throw new Error(`Unexpected request: ${request.url}`); + }); + const abort = new AbortController(); + using _abort = { [Symbol.dispose]: () => abort.abort() }; + const platform = createBrowserVoiceInputPlatform({ + prepared, + hasAudioConsent: () => true, + getStreamingEnabled: () => enabled, + getTranscriptionOptions: () => ({ + ...DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS, + speechTranslateToEnglish: true, + speechLanguage: "fr", + }), + getMicrophoneId: () => "", + onLevel() {}, + onDurationLimit() {}, + onText() {}, + onError() {}, + }); + const transcription = await platform.transcriber.prepare({ signal: abort.signal }); + expect(Boolean(transcription.streaming)).toBe(live); + expect(transcription.locale).toBe(native && enabled ? "fr" : "en"); + expect(sockets).toEqual( + live ? ["wss://transcription.example.test/ws/voice?wsTicket=stream-ticket"] : [], + ); + }, +); diff --git a/apps/web/src/speech/browserVoiceInput.ts b/apps/web/src/speech/browserVoiceInput.ts index e6d164db59f3c..ee16af12c4043 100644 --- a/apps/web/src/speech/browserVoiceInput.ts +++ b/apps/web/src/speech/browserVoiceInput.ts @@ -27,6 +27,7 @@ export function createBrowserVoiceInputPlatform(input: { readonly cloudModel?: CloudSpeechModelId | undefined; readonly hasAudioConsent: () => boolean; readonly getTranscriptionOptions: () => SpeechTranscriptionOptions; + readonly getStreamingEnabled: () => boolean; readonly getMicrophoneId: () => string | Promise; readonly onLevel: (level: number) => void; readonly onDurationLimit: () => void; @@ -35,6 +36,7 @@ export function createBrowserVoiceInputPlatform(input: { }) { return createBrowserPcmCapture(input, async (options) => { const recordingOptions = input.getTranscriptionOptions(); + const streamingEnabled = input.getStreamingEnabled(); const transcribeRecording = async ( uri: string, { signal: transcriptionSignal }: VoiceTranscriptionOptions, @@ -101,13 +103,12 @@ export function createBrowserVoiceInputPlatform(input: { recordingOptions.speechLanguage, ) : "auto"; + const nativeStreaming = status.supportsStreaming && streamingEnabled; const locale = - !status.supportsStreaming && - status.supportsTranslation && - recordingOptions.speechTranslateToEnglish + !nativeStreaming && status.supportsTranslation && recordingOptions.speechTranslateToEnglish ? "en" : language; - if (!status.supportsStreaming && !status.supportsBufferedStreaming) { + if (status.supportsStreaming ? !nativeStreaming : !status.supportsBufferedStreaming) { return { locale, transcribe: transcribeRecording }; } const url = await runEnvironmentSpeech(getEnvironmentSpeechStreamUrl(input.prepared), options); diff --git a/apps/web/src/speech/useEnvironmentSpeechInput.ts b/apps/web/src/speech/useEnvironmentSpeechInput.ts index 94dd7cfb8b4cf..9078a16b7d2c9 100644 --- a/apps/web/src/speech/useEnvironmentSpeechInput.ts +++ b/apps/web/src/speech/useEnvironmentSpeechInput.ts @@ -235,6 +235,7 @@ export function useEnvironmentSpeechInput(input: HookInput) { cloudProvider, cloudModel, hasAudioConsent, + getStreamingEnabled: () => latestRef.current.clientSettings.speechStreaming, getTranscriptionOptions: (): SpeechTranscriptionOptions => { const { clientSettings: settings, diff --git a/docs/user/voice-input.md b/docs/user/voice-input.md index 592e110c896cc..4da5e0b750a87 100644 --- a/docs/user/voice-input.md +++ b/docs/user/voice-input.md @@ -73,7 +73,10 @@ language; speed is the time to transcribe 30 seconds on a reference GPU. Choose **Auto**, a recording that fails on the GPU is retried once on that same environment's CPU; a GPU you picked yourself reports the error instead. Nothing is ever retried on another environment. Models marked **Live preview** show -text while you speak. Other models can process long recordings in chunks at pauses +text while you speak. On web and desktop, turn off **Advanced → Streaming transcription** +to transcribe with the same model after stopping instead. This may help when live +transcription falls behind. The preference is saved on this device and only applies +to models that support native streaming. Other models can process long recordings in chunks at pauses while you record; models with built-in long-form support transcribe when you stop. ## Dictate diff --git a/packages/contracts/src/settings.test.ts b/packages/contracts/src/settings.test.ts index 1db913e38ba3b..e7cea79c7663e 100644 --- a/packages/contracts/src/settings.test.ts +++ b/packages/contracts/src/settings.test.ts @@ -516,6 +516,11 @@ describe("voice settings", () => { speechPostProcessingEnabled: true, }; expect(decodeClientSettings({}).speechSilenceFiltering).toBe(true); + expect(decodeClientSettings({}).speechStreaming).toBe(true); + expect(decodeClientSettings({ speechStreaming: false }).speechStreaming).toBe(false); + expect(decodeClientSettingsPatch({ speechStreaming: false })).toEqual({ + speechStreaming: false, + }); expect(decodeClientSettingsPatch({ speechSilenceFiltering: false })).toEqual({ speechSilenceFiltering: false, }); diff --git a/packages/contracts/src/settings.ts b/packages/contracts/src/settings.ts index 7bbb03595d4bb..0013e288983e0 100644 --- a/packages/contracts/src/settings.ts +++ b/packages/contracts/src/settings.ts @@ -572,6 +572,7 @@ export const ClientSettingsSchema = Schema.Struct({ speechTranslateToEnglish: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))), speechCustomWords: SpeechCustomWords.pipe(Schema.withDecodingDefault(Effect.succeed([]))), speechSilenceFiltering: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(true))), + speechStreaming: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(true))), speechRemoveFillerWords: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(true))), speechPostProcessingPrompt: SpeechPostProcessingPromptSettings.pipe( Schema.withDecodingDefault( @@ -2106,6 +2107,7 @@ export const ClientSettingsPatch = Schema.Struct({ speechTranslateToEnglish: Schema.optionalKey(Schema.Boolean), speechCustomWords: Schema.optionalKey(SpeechCustomWords), speechSilenceFiltering: Schema.optionalKey(Schema.Boolean), + speechStreaming: Schema.optionalKey(Schema.Boolean), speechRemoveFillerWords: Schema.optionalKey(Schema.Boolean), speechPostProcessingPrompt: Schema.optionalKey(SpeechPostProcessingPromptSettings), speechCorrectionWord: Schema.optionalKey(SpeechCorrectionCue), From 375343aaf35133f659435554cd01634bab333a7d Mon Sep 17 00:00:00 2001 From: Aditya Garud <153842990+yashranaway@users.noreply.github.com> Date: Tue, 17 Nov 2026 01:03:58 +0530 Subject: [PATCH 028/253] fix(mobile): expanding a live tool-call row no longer crashes the thread (#37735) Co-authored-by: Julius Marminge --- patches/@legendapp__list@3.7.2.patch | 28 ++++++++++++++++++++++++++-- pnpm-lock.yaml | 8 ++++---- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/patches/@legendapp__list@3.7.2.patch b/patches/@legendapp__list@3.7.2.patch index 2b556ebde74fa..beafc1c4a6fcf 100644 --- a/patches/@legendapp__list@3.7.2.patch +++ b/patches/@legendapp__list@3.7.2.patch @@ -1,5 +1,5 @@ diff --git a/react-native.js b/react-native.js -index 4af685dfb961eb0c8a4bf1ae9132c9304cf4fa4d..df4ead7ae4f03cd0fbcd99a3df7d95e15b1852b1 100644 +index 4af685dfb961eb0c8a4bf1ae9132c9304cf4fa4d..a50466b8fc8509db2d097b85a09d2109a20530cb 100644 --- a/react-native.js +++ b/react-native.js @@ -1795,7 +1795,35 @@ function resolvePendingNativeMVCPAdjust(ctx, newScroll) { @@ -38,8 +38,20 @@ index 4af685dfb961eb0c8a4bf1ae9132c9304cf4fa4d..df4ead7ae4f03cd0fbcd99a3df7d95e1 var _a3, _b, _c; const state = ctx.state; const { idsInView, positions, props } = state; +@@ -5422,6 +5450,11 @@ function updateItemSizesBatch(ctx, measurements) { + const result = {}; + const previousTotalSize = state.totalSize; + for (const measurement of measurements) { ++ // T3 patch: Fabric's measure() reports NaN for a container with no layout yet. A NaN size makes ++ // totalSize NaN, and since NaN !== NaN, useArr$ returns a new snapshot on every read until React ++ // throws "Maximum update depth exceeded". The container's onLayout reports its real size later. ++ const measuredSize = state.props.horizontal ? measurement.size.width : measurement.size.height; ++ if (!Number.isFinite(measuredSize)) continue; + const ownsMeasuredItem = measurement.containerId === void 0 || isContainerItemCurrent(ctx, measurement.containerId, measurement.itemKey); + if (ownsMeasuredItem) { + const index = state.indexByKey.get(measurement.itemKey); diff --git a/react-native.mjs b/react-native.mjs -index 9ebfddc183a9b202d35c990a387c9b628feeb185..c414a49e866ecab1393645101fb67d55530a7d00 100644 +index 9ebfddc183a9b202d35c990a387c9b628feeb185..489050190144997ac72de8f4f7c6219479216dba 100644 --- a/react-native.mjs +++ b/react-native.mjs @@ -1774,7 +1774,35 @@ function resolvePendingNativeMVCPAdjust(ctx, newScroll) { @@ -78,3 +90,15 @@ index 9ebfddc183a9b202d35c990a387c9b628feeb185..c414a49e866ecab1393645101fb67d55 var _a3, _b, _c; const state = ctx.state; const { idsInView, positions, props } = state; +@@ -5401,6 +5429,11 @@ function updateItemSizesBatch(ctx, measurements) { + const result = {}; + const previousTotalSize = state.totalSize; + for (const measurement of measurements) { ++ // T3 patch: Fabric's measure() reports NaN for a container with no layout yet. A NaN size makes ++ // totalSize NaN, and since NaN !== NaN, useArr$ returns a new snapshot on every read until React ++ // throws "Maximum update depth exceeded". The container's onLayout reports its real size later. ++ const measuredSize = state.props.horizontal ? measurement.size.width : measurement.size.height; ++ if (!Number.isFinite(measuredSize)) continue; + const ownsMeasuredItem = measurement.containerId === void 0 || isContainerItemCurrent(ctx, measurement.containerId, measurement.itemKey); + if (ownsMeasuredItem) { + const index = state.indexByKey.get(measurement.itemKey); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 985c914406cf7..07806d3cac658 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -370,7 +370,7 @@ patchedDependencies: '@effect/vitest@4.1.2': 299510b71c36537a832a67e9d160a5f112a5f326a29e95c41cb45fc8234e47c7 '@expo/ui@58.0.27': 9f2547678939f4ec78fe45163b6455aad63d3c80353d7e0cc783f6a61a7ccc2f '@ff-labs/fff-node@0.11.0': 93f62a76399b435a76588b2bef237db0a1e85880c1ac0d0759c416e9cc579252 - '@legendapp/list@3.7.2': 3c68607b8bb7a531873bcbe0b197d163ef1fdb4d4cf52a4ddf9d2b3804671658 + '@legendapp/list@3.7.2': 8f361e44c0756d883f8b8b5964f918f37b514a436c5058b773a1dc57eee5bfcc '@muse-code/sdk@1.4.6': 7b495b607fc5d76b444c7c4d58821e839726a30e6f3c53564f0d59f2d5b42101 '@pierre/diffs@1.7.0-beta.4': 241563d810c29250e0e92f3eda74214393726d46667fedbf1f91a5f5bea07d1e '@react-native-ai/apple@0.12.0': 2d09870c2848d185cb05b53ed823a46e12dba519324d8dd8e584e28731990f9d @@ -567,7 +567,7 @@ importers: version: 4.2.13 '@legendapp/list': specifier: 3.7.2 - version: 3.7.2(patch_hash=3c68607b8bb7a531873bcbe0b197d163ef1fdb4d4cf52a4ddf9d2b3804671658)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0) + version: 3.7.2(patch_hash=8f361e44c0756d883f8b8b5964f918f37b514a436c5058b773a1dc57eee5bfcc)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0) '@material/material-color-utilities': specifier: 0.3.0 version: 0.3.0 @@ -1088,7 +1088,7 @@ importers: version: 5.3.0 '@legendapp/list': specifier: 3.7.2 - version: 3.7.2(patch_hash=3c68607b8bb7a531873bcbe0b197d163ef1fdb4d4cf52a4ddf9d2b3804671658)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0) + version: 3.7.2(patch_hash=8f361e44c0756d883f8b8b5964f918f37b514a436c5058b773a1dc57eee5bfcc)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0) '@noble/hashes': specifier: 'catalog:' version: 1.8.0 @@ -15564,7 +15564,7 @@ snapshots: '@koromix/koffi-win32-x64@3.3.6': optional: true - '@legendapp/list@3.7.2(patch_hash=3c68607b8bb7a531873bcbe0b197d163ef1fdb4d4cf52a4ddf9d2b3804671658)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0)': + '@legendapp/list@3.7.2(patch_hash=8f361e44c0756d883f8b8b5964f918f37b514a436c5058b773a1dc57eee5bfcc)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0)': dependencies: react: 19.3.0 use-sync-external-store: 1.7.0(react@19.3.0) From bc0988d4d3f8c81912c89a70d7d7078c38863138 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 11:45:48 -0800 Subject: [PATCH 029/253] fix(ci): iOS preview builds record their tag on macOS awk (#37746) --- .github/workflows/mobile-eas-preview.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/mobile-eas-preview.yml b/.github/workflows/mobile-eas-preview.yml index 26f9a16f8a5d7..7bc68234e364b 100644 --- a/.github/workflows/mobile-eas-preview.yml +++ b/.github/workflows/mobile-eas-preview.yml @@ -638,7 +638,7 @@ jobs: run: | git config user.name "github-actions[bot]" git config user.email "41898282+github-actions[bot]@users.noreply.github.com" - profile_path="$(unzip -Z -1 "$RUNNER_TEMP/t3code-preview.ipa" | awk '/^Payload\/[^/]+\.app\/embedded.mobileprovision$/')" + profile_path="$(unzip -Z -1 "$RUNNER_TEMP/t3code-preview.ipa" | grep -E '^Payload/[^/]+\.app/embedded\.mobileprovision$')" test -n "$profile_path" unzip -p "$RUNNER_TEMP/t3code-preview.ipa" "$profile_path" > "$RUNNER_TEMP/preview.mobileprovision" security cms -D -i "$RUNNER_TEMP/preview.mobileprovision" > "$RUNNER_TEMP/preview-profile.plist" From c02b98bb3fdcbf02a5ca4db2d81d09226c4755ec Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 11:50:52 -0800 Subject: [PATCH 030/253] fix(server): a Codex hook that stops a delegated child no longer reads as a silent success (#37738) --- apps/server/src/mcp/OrchestratorMcpService.ts | 2 +- .../Adapters/CodexAdapterV2.test.ts | 2 +- .../Adapters/CodexAdapterV2.ts | 2 +- .../DelegatedCompletionDelivery.test.ts | 43 +++++++++++++++++++ .../src/orchestration-v2/Orchestrator.ts | 2 +- .../RunExecutionService.test.ts | 15 +++++++ .../orchestration-v2/RunExecutionService.ts | 37 ++++++++++++++++ .../SubagentProjection.test.ts | 33 ++++++++++++++ .../src/server/subagentProjection.ts | 16 ++++++- 9 files changed, 147 insertions(+), 5 deletions(-) diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.ts index d1bae5d859d65..5c5c9636406e1 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.ts @@ -1746,7 +1746,7 @@ const make = Effect.gen(function* () { messageRoles: ["assistant"], messageRunIds: resultRunIds, turnItemRunIds: resultRunIds, - turnItemTypes: ["assistant_message", "error"], + turnItemTypes: ["assistant_message", "error", "system_notice"], }) .pipe(Effect.mapError(threadManagementFailure)); const childProjection = { diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts index 4308e780ba4fb..019dcf4ec0edb 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts @@ -3743,7 +3743,7 @@ describe("CodexAdapterV2 post-settle continuation", () => { { hook: "wrong-turn", status: "interrupted", expected: false }, { hook: "wrong-thread", status: "interrupted", expected: false }, { hook: "unscoped", status: "interrupted", expected: false }, - { hook: "stopped", status: "completed", expected: false }, + { hook: "stopped", status: "completed", expected: true }, { hook: "stopped", status: "failed", expected: false }, ] as const)( "correlates $hook hook output with a $status turn only", diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts index c464b1e3c287a..d1ddd569e60d5 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts @@ -6435,7 +6435,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( runOrdinal: input.context.input.runOrdinal, status: terminalStatus, failure: null, - ...(terminalStatus === "interrupted" && input.context.hookStopReason !== undefined + ...(input.context.hookStopReason !== undefined ? { reason: input.context.hookStopReason } : {}), threadDisposition: "reusable", diff --git a/apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts b/apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts index e26487fdf2667..e6e29a1b34a76 100644 --- a/apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts +++ b/apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts @@ -57,6 +57,7 @@ import * as ProviderTurnStartServiceTestkit from "./ProviderTurnStartService.tes import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"; import * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests"; import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; +import { PROVIDER_STOP_REASON_SIGNAL } from "@t3tools/provider-core/server/subagentProjection"; import * as ProviderContinuationService from "./ProviderContinuationService.ts"; const layerPlatformTest = Layer.merge( @@ -4652,6 +4653,7 @@ it.layer(layerTest)("delegated tasks across a server restart", (it) => { it.effect("cancelling a task whose child already finished keeps its completed result", () => Effect.gen(function* () { const orchestrator = yield* Orchestrator.OrchestratorV2; + const eventSink = yield* EventSink.EventSinkV2; const now = yield* DateTime.now; const threadId = ThreadId.make("thread:cancel-finished-parent"); const projectId = ProjectId.make("project:cancel-finished-parent"); @@ -4678,6 +4680,43 @@ it.layer(layerTest)("delegated tasks across a server restart", (it) => { runStatus: "completed", now, }); + const stopReason = "sessionStart hook (user): Plugin guard blocked this session."; + yield* eventSink.write({ + commandId: CommandId.make("command:cancel-finished-child:stop-reason"), + events: [ + { + id: EventId.make("event:cancel-finished-child:stop-reason"), + type: "turn-item.updated", + threadId: child.childThreadId, + runId: child.childRunId, + nodeId: rootNodeId, + providerInstanceId: modelSelection.instanceId, + occurredAt: now, + payload: { + id: IdAllocator.derive.runSignalTurnItem({ + runId: child.childRunId, + signal: PROVIDER_STOP_REASON_SIGNAL, + }), + type: "system_notice", + level: "warning", + threadId: child.childThreadId, + runId: child.childRunId, + nodeId: rootNodeId, + providerThreadId: null, + providerTurnId: null, + nativeItemRef: null, + parentItemId: null, + ordinal: 98, + status: "completed", + title: "Stopped by the provider", + startedAt: now, + completedAt: now, + updatedAt: now, + message: stopReason, + }, + }, + ], + }); yield* orchestrator.cancelDelegatedTask({ parentThreadId: threadId, taskId: child.taskId, @@ -4693,6 +4732,10 @@ it.layer(layerTest)("delegated tasks across a server restart", (it) => { (row) => row.id === child.taskId, ); assert.equal(task?.status, "completed"); + assert.equal( + task?.result, + `Child task completed without an assistant result.\n\n${stopReason}`, + ); assert.isUndefined(task?.cancelledRunId); }), ); diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index 27f7aa82fd25c..2805935d38890 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -14428,7 +14428,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio messageRoles: ["assistant"], messageRunIds: [childRun.id], turnItemRunId: childRun.id, - turnItemTypes: ["assistant_message", "error"], + turnItemTypes: ["assistant_message", "error", "system_notice"], }, ); const childProjection = { diff --git a/apps/server/src/orchestration-v2/RunExecutionService.test.ts b/apps/server/src/orchestration-v2/RunExecutionService.test.ts index 9a35624ecfe5f..8a3540a6a61f0 100644 --- a/apps/server/src/orchestration-v2/RunExecutionService.test.ts +++ b/apps/server/src/orchestration-v2/RunExecutionService.test.ts @@ -3183,6 +3183,21 @@ it.effect.each([ }), ); +it.effect("keeps a provider's stop reason on a completed run", () => + Effect.gen(function* () { + const reason = "sessionStart hook (user): Plugin guard blocked this session."; + const { written } = yield* captureRootRunTermination({ + key: "completed-stop-reason", + shouldFinalizeRun: () => Effect.succeed(true), + events: (ids) => Stream.make({ ...rootTerminalEvent(ids, "completed"), reason }), + }); + assert.deepEqual( + written.map((item) => (item.type === "system_notice" ? item.message : item.type)), + [reason], + ); + }), +); + it.effect("records a finished run as failed when its ownership check cannot be read", () => Effect.gen(function* () { const { observed } = yield* captureRootRunTermination({ diff --git a/apps/server/src/orchestration-v2/RunExecutionService.ts b/apps/server/src/orchestration-v2/RunExecutionService.ts index d29b47bbbb821..d7a021a873799 100644 --- a/apps/server/src/orchestration-v2/RunExecutionService.ts +++ b/apps/server/src/orchestration-v2/RunExecutionService.ts @@ -46,6 +46,7 @@ import * as CheckpointService from "./CheckpointService.ts"; import * as EventSink from "./EventSink.ts"; import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"; +import { PROVIDER_STOP_REASON_SIGNAL } from "@t3tools/provider-core/server/subagentProjection"; import * as ProviderEventIngestor from "./ProviderEventIngestor.ts"; import * as ProviderUsageAnalytics from "../telemetry/ProviderUsageAnalytics.ts"; import type { ProjectionStoreV2Error } from "./ProjectionStore.ts"; @@ -915,6 +916,42 @@ export const layer: Layer.Layer< }, ] : []), + // Codex reports a hook that stops the turn as completed; keep its reason visible. + ...(input.terminal.status === "completed" && input.terminal.reason !== undefined + ? [ + { + id: yield* allocateEventId(), + type: "turn-item.updated" as const, + threadId: input.run.threadId, + runId: input.run.id, + nodeId: input.rootNode.id, + providerInstanceId: input.run.providerInstanceId, + occurredAt: completedAt, + payload: { + id: idAllocator.derive.runSignalTurnItem({ + runId: input.run.id, + signal: PROVIDER_STOP_REASON_SIGNAL, + }), + threadId: input.run.threadId, + runId: input.run.id, + nodeId: input.rootNode.id, + providerThreadId: input.providerThread.id, + providerTurnId: input.rootNode.providerTurnId, + nativeItemRef: null, + parentItemId: null, + ordinal: input.run.ordinal * 100 + 98, + status: "completed" as const, + title: "Stopped by the provider", + startedAt: completedAt, + completedAt, + updatedAt: completedAt, + type: "system_notice" as const, + level: "warning" as const, + message: input.terminal.reason, + }, + }, + ] + : []), ...(input.terminal.status === "failed" && !input.failureItemPersisted ? [ { diff --git a/apps/server/src/orchestration-v2/SubagentProjection.test.ts b/apps/server/src/orchestration-v2/SubagentProjection.test.ts index 87204d763d2df..2f764233ba97b 100644 --- a/apps/server/src/orchestration-v2/SubagentProjection.test.ts +++ b/apps/server/src/orchestration-v2/SubagentProjection.test.ts @@ -10,6 +10,7 @@ import { EventId, TurnItemId, type OrchestrationV2Run, + type OrchestrationV2TurnItem, type OrchestrationV2AppThread, ProjectId, ProviderInstanceId, @@ -27,7 +28,9 @@ import { makeSubagentConversationArtifacts, endOrphanedNativeSubagent, endRunlessRootTurns, + PROVIDER_STOP_REASON_SIGNAL, } from "@t3tools/provider-core/server/subagentProjection"; +import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; import * as SqlitePersistence from "../persistence/Sqlite.ts"; import * as ProjectionStore from "./ProjectionStore.ts"; @@ -400,6 +403,36 @@ it("exposes the provider failure rather than a progress message from the failed assert.isNull(result.messageId); }); +it("reports a provider's stop reason for a completed run without an assistant result", () => { + const { projection, run } = taskFixture(); + const notice = { + id: IdAllocator.derive.runSignalTurnItem({ + runId: run.id, + signal: PROVIDER_STOP_REASON_SIGNAL, + }), + threadId: parentThreadId, + runId: run.id, + nodeId: NodeId.make("root"), + providerThreadId: null, + providerTurnId: null, + nativeItemRef: null, + parentItemId: null, + ordinal: 98, + status: "completed", + title: "Stopped by the provider", + startedAt: childCreatedAt, + completedAt: childCreatedAt, + updatedAt: childCreatedAt, + type: "system_notice", + level: "warning", + message: "sessionStart hook (user): Plugin guard blocked this session.", + } satisfies OrchestrationV2TurnItem; + assert.equal( + subagentResultForRun({ ...projection, turnItems: [notice] }, run).text, + `Child task completed without an assistant result.\n\n${notice.message}`, + ); +}); + it("keeps the final report of a run that failed after its turn ended", () => { const { projection, run } = taskFixture(); const failedRun = { ...run, status: "failed" as const }; diff --git a/packages/provider-core/src/server/subagentProjection.ts b/packages/provider-core/src/server/subagentProjection.ts index f2c3092bf17da..f1b0a41905fb3 100644 --- a/packages/provider-core/src/server/subagentProjection.ts +++ b/packages/provider-core/src/server/subagentProjection.ts @@ -22,6 +22,7 @@ import { backgroundWorkHoldsCompletion } from "@t3tools/shared/orchestrationV2Pe import { runRanAfter } from "@t3tools/shared/orchestrationV2ThreadError"; import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; +import { derive } from "./IdAllocator.ts"; import { isOrchestrationV2WorkActive, latestProviderTurnForAttempt } from "@t3tools/contracts"; function trimmed(value: string | null | undefined): string | undefined { @@ -189,6 +190,19 @@ export function withExecutedModelNote( : `Requested model ${requestedModel}; the provider reported running ${executedModel}.\n\n${result}`; } +/** Run signal of the notice that carries a provider's stop reason on a completed run. */ +export const PROVIDER_STOP_REASON_SIGNAL = "provider-stop-reason"; + +function withStopNotice( + projection: Pick, + run: OrchestrationV2Run, + text: string, +): string { + const id = derive.runSignalTurnItem({ runId: run.id, signal: PROVIDER_STOP_REASON_SIGNAL }); + const notice = projection.turnItems.find((item) => item.id === id); + return notice?.type === "system_notice" ? `${text}\n\n${notice.message}` : text; +} + export function subagentResultForRun( projection: Pick, run: OrchestrationV2Run, @@ -242,7 +256,7 @@ export function subagentResultForRun( message?.text ?? turnItem?.text ?? (run.status === "completed" - ? "Child task completed without an assistant result." + ? withStopNotice(projection, run, "Child task completed without an assistant result.") : `Child task ended with status ${run.status}.`); const executedModel = latestProviderTurnForAttempt( projection.providerTurns, From 15271c8d00875d2fb03a71cf8dec778273c19cdd Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 11:54:55 -0800 Subject: [PATCH 031/253] fix(pr): label and reviewer pickers send picks once, when they close (#37733) Co-authored-by: Claude Opus 5.5 --- .../pull-requests/PullRequestLabelsScreen.tsx | 90 +++++++++++------- .../PullRequestReviewersScreen.tsx | 92 ++++++++++++++---- .../PullRequestCandidatePicker.tsx | 10 +- .../pullRequest/PullRequestLabelPicker.tsx | 65 +++++++++---- .../pullRequest/PullRequestReviewerPicker.tsx | 94 +++++++++++++------ packages/client-runtime/package.json | 4 + .../src/pullRequestSelection.test.ts | 48 ++++++++++ .../src/pullRequestSelection.ts | 54 +++++++++++ packages/contracts/src/pullRequest.ts | 10 +- 9 files changed, 351 insertions(+), 116 deletions(-) create mode 100644 packages/client-runtime/src/pullRequestSelection.test.ts create mode 100644 packages/client-runtime/src/pullRequestSelection.ts diff --git a/apps/mobile/src/features/pull-requests/PullRequestLabelsScreen.tsx b/apps/mobile/src/features/pull-requests/PullRequestLabelsScreen.tsx index 24fd3533ce358..95cf64d02e848 100644 --- a/apps/mobile/src/features/pull-requests/PullRequestLabelsScreen.tsx +++ b/apps/mobile/src/features/pull-requests/PullRequestLabelsScreen.tsx @@ -1,15 +1,23 @@ import { NativeScrollView } from "../../native/NativeScrollViewMarker"; import { LegendList } from "@legendapp/list/react-native"; import { useAtomValue } from "@effect/atom-react"; -import type { StaticScreenProps } from "@react-navigation/native"; +import { useNavigation, type StaticScreenProps } from "@react-navigation/native"; import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; +import { + emptySelection, + isSelected, + changeBatches, + selectionChanges, + toggleSelection, +} from "@t3tools/client-runtime/pull-request-selection"; import type { PullRequestLabelCandidate } from "@t3tools/contracts"; import { AsyncResult } from "effect/reactivity"; -import { useRef, useState } from "react"; +import { useEffect, useState } from "react"; import { Platform, View } from "react-native"; import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator"; import { AppText as Text } from "../../components/ui/AppText"; import { Button } from "../../components/ui/Button"; +import * as Dialog from "../../components/ui/Dialog"; import { EmptyState } from "../../components/ui/EmptyState"; import { SelectionRow } from "../../components/ui/SelectionRow"; import { composerPullRequests } from "../../state/pull-requests"; @@ -22,6 +30,7 @@ import { usePullRequestConnected } from "./usePullRequestConnected"; import { pullRequestTarget, type PullRequestParams } from "./usePullRequestDiff"; export function PullRequestLabelsScreen({ route }: StaticScreenProps) { + const navigation = useNavigation(); const target = pullRequestTarget(route.params); const pr = useEnvironmentQuery(composerPullRequests.detail(target)).data; const canWrite = useAtomValue( @@ -47,30 +56,46 @@ export function PullRequestLabelsScreen({ route }: StaticScreenProps(); - const [error, setError] = useState(null); - const inFlight = useRef(false); + const [draft, setDraft] = useState(emptySelection); const needle = query.trim().toLowerCase(); // Host order, not applied-first: re-sorting on toggle would move the row out from under a tap. const candidates = (result.data?.candidates ?? []).filter((candidate) => `${candidate.name} ${candidate.description ?? ""}`.toLowerCase().includes(needle), ); - const toggle = async (candidate: PullRequestLabelCandidate) => { - if (!allowed || inFlight.current) return; - inFlight.current = true; - setPending({ name: candidate.name, applied: !candidate.isApplied }); - setError(null); - const response = await setLabels({ - ...target, - input: { ...target.input, labels: [candidate.name], applied: !candidate.isApplied }, - }); - inFlight.current = false; - setPending(undefined); - if (AsyncResult.isFailure(response)) { - const cause = squashAtomCommandFailure(response); - setError(cause instanceof Error ? cause.message : "Couldn't update this label. Try again."); - } - }; + const latest = result.data?.candidates; + // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- picks are sent when the sheet leaves the navigation stack, however it is dismissed + useEffect( + () => + navigation.addListener("beforeRemove", () => { + const { add, remove } = selectionChanges( + draft, + (name, label) => + latest?.find((entry) => entry.name === name)?.isApplied ?? label.isApplied, + ); + const sendBatch = async ( + labels: ReadonlyArray, + applied: boolean, + ) => { + const names = labels.map((label) => label.name); + const response = await setLabels({ + ...target, + input: { ...target.input, labels: names, applied }, + }); + if (!AsyncResult.isFailure(response)) return; + const cause = squashAtomCommandFailure(response); + Dialog.alert( + applied ? `Couldn't add ${names.join(", ")}` : `Couldn't remove ${names.join(", ")}`, + cause instanceof Error ? cause.message : "The provider refused the change.", + ); + }; + const send = async (labels: ReadonlyArray, applied: boolean) => { + for (const batch of changeBatches(labels)) await sendBatch(batch, applied); + }; + // Adding first keeps the Labels row from showing empty between the two writes. + void send(add, true).then(() => send(remove, false)); + }), + [navigation, draft, latest, setLabels, target], + ); return ( candidate.name} - extraData={{ pending, allowed, count: candidates.length }} + extraData={{ draft, allowed, count: candidates.length }} estimatedItemSize={72} recycleItems className="flex-1" @@ -96,16 +121,9 @@ export function PullRequestLabelsScreen({ route }: StaticScreenProps - {readOnlyReason ? ( - {readOnlyReason} - ) : null} - {error ? ( - - {error} - - ) : null} + readOnlyReason ? ( + + {readOnlyReason} ) : undefined } @@ -114,13 +132,15 @@ export function PullRequestLabelsScreen({ route }: StaticScreenProps} - onPress={() => void toggle(item)} + onPress={() => + setDraft((current) => toggleSelection(current, item.name, item, item.isApplied)) + } /> )} ListEmptyComponent={ diff --git a/apps/mobile/src/features/pull-requests/PullRequestReviewersScreen.tsx b/apps/mobile/src/features/pull-requests/PullRequestReviewersScreen.tsx index 2daad20250a59..2a74bad7cc478 100644 --- a/apps/mobile/src/features/pull-requests/PullRequestReviewersScreen.tsx +++ b/apps/mobile/src/features/pull-requests/PullRequestReviewersScreen.tsx @@ -12,14 +12,22 @@ import { reviewerCandidateView, type ReviewerTruncation, } from "@t3tools/client-runtime/pull-request-reviewers"; +import { + emptySelection, + isSelected, + changeBatches, + selectionChanges, + toggleSelection, +} from "@t3tools/client-runtime/pull-request-selection"; import type { PullRequestReviewerCandidate } from "@t3tools/contracts"; -import { useRef, useState } from "react"; +import { useEffect, useRef, useState } from "react"; import { Pressable, ScrollView, View } from "react-native"; import { NativeScrollView } from "../../native/NativeScrollViewMarker"; import { useContentTopInset, useSearchToolbarInset } from "../../components/ui/useBarInset"; import { AppText as Text, AppTextInput as TextInput } from "../../components/ui/AppText"; import { EmptyState } from "../../components/ui/EmptyState"; import { Button } from "../../components/ui/Button"; +import * as Dialog from "../../components/ui/Dialog"; import { SymbolView } from "../../components/ui/AppSymbol"; import { composerPullRequests } from "../../state/pull-requests"; import { useEnvironmentQuery } from "../../state/query"; @@ -68,6 +76,7 @@ export function PullRequestReviewersScreen({ route }: StaticScreenProps(null); const [error, setError] = useState(null); const inFlight = useRef(false); + const [draft, setDraft] = useState(emptySelection); const debouncedQuery = useDebouncedValue(query.trim(), 250); const searchInput = { loaded: result.data, @@ -100,6 +109,7 @@ export function PullRequestReviewersScreen({ route }: StaticScreenProps, ) => { @@ -124,13 +134,60 @@ export function PullRequestReviewersScreen({ route }: StaticScreenProps + navigation.addListener("beforeRemove", () => { + const { add, remove } = selectionChanges( + draft, + (key, person) => + latest.find((entry) => `${entry.kind}:${entry.id}` === key)?.isRequested ?? + person.isRequested, + ); + const sendBatch = async ( + people: ReadonlyArray, + requested: boolean, + ) => { + const response = await request({ + ...target, + input: { + ...target.input, + reviewers: people.map(({ id, kind }) => ({ id, kind })), + requested, + }, + }); + if (!AsyncResult.isFailure(response)) return; + const cause = squashAtomCommandFailure(response); + const logins = people.map((person) => person.login).join(", "); + Dialog.alert( + requested + ? `Couldn't request a review from ${logins}` + : `Couldn't remove the review request from ${logins}`, + cause instanceof Error ? cause.message : "The provider refused the change.", + ); + }; + const send = async ( + people: ReadonlyArray, + requested: boolean, + ) => { + for (const batch of changeBatches(people)) await sendBatch(batch, requested); + }; + // A search result is keyed by what was typed, so the writes do not reach it on their own. + const refreshSearch = + hostQuery !== null && add.length + remove.length > 0 ? searchResult.refresh : null; + // Removing first lets a host that keeps one reviewer take the new one. + void send(remove, false) + .then(() => send(add, true)) + .then(() => refreshSearch?.()); + }), + [navigation, draft, latest, request, target, hostQuery, searchResult.refresh], + ); const reviewerSearch: AppBarSearch | undefined = listCandidates ? { mode: "inline", @@ -155,7 +212,7 @@ export function PullRequestReviewersScreen({ route }: StaticScreenProps `${candidate.kind}:${candidate.id}`} - extraData={{ allowed, pending }} + extraData={{ allowed, draft }} estimatedItemSize={64} recycleItems renderScrollComponent={NativeScrollView} @@ -168,28 +225,27 @@ export function PullRequestReviewersScreen({ route }: StaticScreenProps Tap someone to request a review. Tap a requested reviewer to remove the request. + Changes are sent when you close this sheet. {!allowed ? ( Requesting reviews requires write access and an open {singular}. ) : null} - {error ? ( - - {error} - - ) : null} } renderItem={({ item }) => { const id = `${item.kind}:${item.id}`; + const requested = isSelected(draft, id, item.isRequested); return ( void update(item)} + accessibilityLabel={`${requested ? "Remove review request from" : "Request review from"} ${item.login}${item.kind === "team" ? " team" : ""}`} + accessibilityState={{ disabled: !allowed }} + disabled={!allowed} + onPress={() => + setDraft((current) => toggleSelection(current, id, item, item.isRequested)) + } className="min-h-16 flex-row items-center gap-3 border-b border-border-subtle px-4 py-3 active:opacity-70" > @@ -202,9 +258,7 @@ export function PullRequestReviewersScreen({ route }: StaticScreenProps - {pending === id ? ( - - ) : item.isRequested ? ( + {requested ? ( Requested diff --git a/apps/web/src/components/pullRequest/PullRequestCandidatePicker.tsx b/apps/web/src/components/pullRequest/PullRequestCandidatePicker.tsx index 9662e0e0899bc..4810e8b5e2183 100644 --- a/apps/web/src/components/pullRequest/PullRequestCandidatePicker.tsx +++ b/apps/web/src/components/pullRequest/PullRequestCandidatePicker.tsx @@ -32,7 +32,6 @@ export function PullRequestCandidatePicker({ truncated, truncatedLabel, candidateKey, - disabled, onSelect, children, }: { @@ -54,7 +53,6 @@ export function PullRequestCandidatePicker({ truncated: boolean; truncatedLabel: string; candidateKey: (candidate: T) => string; - disabled: boolean; onSelect: (candidate: T) => void; children: (candidate: T) => ReactNode; }) { @@ -123,13 +121,7 @@ export function PullRequestCandidatePicker({

) : ( candidates.map((candidate, index) => ( - + {children(candidate)} )) diff --git a/apps/web/src/components/pullRequest/PullRequestLabelPicker.tsx b/apps/web/src/components/pullRequest/PullRequestLabelPicker.tsx index 24b4d1eedd7b5..2640aa3b5142b 100644 --- a/apps/web/src/components/pullRequest/PullRequestLabelPicker.tsx +++ b/apps/web/src/components/pullRequest/PullRequestLabelPicker.tsx @@ -6,6 +6,13 @@ import { useState } from "react"; import { pullRequestEnvironment } from "~/state/pullRequests"; import { useEnvironmentQuery } from "~/state/query"; import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; +import { + emptySelection, + isSelected, + changeBatches, + selectionChanges, + toggleSelection, +} from "@t3tools/client-runtime/pull-request-selection"; import { toastManager } from "../ui/toast"; import { PullRequestCandidatePicker } from "./PullRequestCandidatePicker"; @@ -32,6 +39,7 @@ export function PullRequestLabelPicker({ }) { const [open, setOpen] = useState(false); const [query, setQuery] = useState(""); + const [draft, setDraft] = useState(emptySelection); const candidatesQuery = useEnvironmentQuery( open ? pullRequestEnvironment.labelCandidates({ environmentId, input: reference }) : null, @@ -44,25 +52,39 @@ export function PullRequestLabelPicker({ matches(entry, query), ); - // Each change shows at once and queues behind the one before it on the host. - const toggle = async (candidate: PullRequestLabelCandidate) => { + const send = async (labels: ReadonlyArray, applied: boolean) => { + for (const batch of changeBatches(labels)) await sendBatch(batch, applied); + }; + const sendBatch = async (labels: ReadonlyArray, applied: boolean) => { + const names = labels.map((label) => label.name); const result = await setLabels({ environmentId, - input: { ...reference, labels: [candidate.name], applied: !candidate.isApplied }, + input: { ...reference, labels: names, applied }, + }); + if (result._tag === "Success") return; + toastManager.add({ + type: "error", + title: applied + ? `Could not put ${names.join(", ")} on` + : `Could not take ${names.join(", ")} off`, + description: readableFailure( + squashAtomCommandFailure(result), + "The host refused it. Check that you have triage access on this repository.", + ), }); - if (result._tag === "Failure") { - toastManager.add({ - type: "error", - title: candidate.isApplied - ? `Could not take ${candidate.name} off` - : `Could not put ${candidate.name} on`, - description: readableFailure( - squashAtomCommandFailure(result), - "The host refused it. Check that you have triage access on this repository.", - ), - }); - return; - } + }; + + const onOpenChange = (nextOpen: boolean) => { + setOpen(nextOpen); + if (nextOpen) return; + const latest = candidatesQuery.data?.candidates ?? []; + const { add, remove } = selectionChanges( + draft, + (name, label) => latest.find((entry) => entry.name === name)?.isApplied ?? label.isApplied, + ); + setDraft(emptySelection()); + // Adding first keeps the Labels row from showing empty between the two writes. + void send(add, true).then(() => send(remove, false)); }; return ( @@ -72,7 +94,7 @@ export function PullRequestLabelPicker({ allowed={allowed} disabledReason="Changing labels needs triage access on this repository" open={open} - onOpenChange={setOpen} + onOpenChange={onOpenChange} query={query} onQueryChange={setQuery} searchLabel="Search labels" @@ -85,8 +107,11 @@ export function PullRequestLabelPicker({ truncated={candidatesQuery.data?.truncated === true} truncatedLabel="This repository has more labels than are listed here. Apply the rest on the host." candidateKey={(candidate) => candidate.name} - disabled={false} - onSelect={(candidate) => void toggle(candidate)} + onSelect={(candidate) => + setDraft((current) => + toggleSelection(current, candidate.name, candidate, candidate.isApplied), + ) + } > {(candidate) => { const dot = pullRequestLabelColor(candidate.color); @@ -103,7 +128,7 @@ export function PullRequestLabelPicker({ · {candidate.description} ) : null}
- {candidate.isApplied ? ( + {isSelected(draft, candidate.name, candidate.isApplied) ? ( ) : null} diff --git a/apps/web/src/components/pullRequest/PullRequestReviewerPicker.tsx b/apps/web/src/components/pullRequest/PullRequestReviewerPicker.tsx index 729bd089be9b1..5df993cfe0f90 100644 --- a/apps/web/src/components/pullRequest/PullRequestReviewerPicker.tsx +++ b/apps/web/src/components/pullRequest/PullRequestReviewerPicker.tsx @@ -9,6 +9,13 @@ import { reviewerCandidateView, type ReviewerTruncation, } from "@t3tools/client-runtime/pull-request-reviewers"; +import { + emptySelection, + isSelected, + changeBatches, + selectionChanges, + toggleSelection, +} from "@t3tools/client-runtime/pull-request-selection"; import { CheckIcon, UserPlusIcon } from "lucide-react"; import { useState } from "react"; @@ -46,7 +53,7 @@ export function PullRequestReviewerPicker({ }) { const [open, setOpen] = useState(false); const [query, setQuery] = useState(""); - const [pending, setPending] = useState(null); + const [draft, setDraft] = useState(emptySelection); const candidatesQuery = useEnvironmentQuery( open ? pullRequestEnvironment.reviewerCandidates({ environmentId, input: reference }) : null, @@ -72,42 +79,59 @@ export function PullRequestReviewerPicker({ search: searchResult, }); - const toggle = async (candidate: PullRequestReviewerCandidate) => { - if (pending !== null) return; - setPending(candidate.id); + const send = async (people: ReadonlyArray, requested: boolean) => { + for (const batch of changeBatches(people)) await sendBatch(batch, requested); + }; + const sendBatch = async ( + people: ReadonlyArray, + requested: boolean, + ) => { const result = await requestReviewers({ environmentId, input: { ...reference, - reviewers: [{ id: candidate.id, kind: candidate.kind }], - requested: !candidate.isRequested, + reviewers: people.map(({ id, kind }) => ({ id, kind })), + requested, }, }); - setPending(null); - if (result._tag === "Failure") { - toastManager.add({ - type: "error", - title: candidate.isRequested - ? `Could not take back the review request to ${candidate.login}` - : `Could not ask ${candidate.login} for a review`, - description: readableFailure( - squashAtomCommandFailure(result), - "The host refused it. Check that you have write access on this repository, and that they still have access to it.", - ), - }); - return; - } - // The command reads the loaded list again only for somebody it lacked, and a search result - // is keyed by what was typed, so the one on screen is read here or it keeps the old request. - if (hostQuery !== null) searchResult.refresh(); + if (result._tag === "Success") return; + const logins = people.map((person) => person.login).join(", "); toastManager.add({ - type: "success", - title: candidate.isRequested - ? `Review request to ${candidate.login} taken back` - : `Review requested from ${candidate.login}`, + type: "error", + title: requested + ? `Could not ask ${logins} for a review` + : `Could not take back the review request to ${logins}`, + description: readableFailure( + squashAtomCommandFailure(result), + "The host refused it. Check that you have write access on this repository, and that they still have access to it.", + ), }); }; + const onOpenChange = (nextOpen: boolean) => { + setOpen(nextOpen); + if (nextOpen) return; + // Loaded rows first: they carry the request state mutations update. + const latest = [ + ...(candidatesQuery.data?.candidates ?? []), + ...(searchResult.data?.candidates ?? []), + ]; + const { add, remove } = selectionChanges( + draft, + (key, person) => + latest.find((entry) => `${entry.kind}:${entry.id}` === key)?.isRequested ?? + person.isRequested, + ); + setDraft(emptySelection()); + // A search result is keyed by what was typed, so the writes do not reach it on their own. + const refreshSearch = + hostQuery !== null && add.length + remove.length > 0 ? searchResult.refresh : null; + // Removing first lets a host that keeps one reviewer take the new one. + void send(remove, false) + .then(() => send(add, true)) + .then(() => refreshSearch?.()); + }; + return ( } @@ -115,7 +139,7 @@ export function PullRequestReviewerPicker({ allowed={allowed} disabledReason="Asking someone to review needs write access on this repository" open={open} - onOpenChange={setOpen} + onOpenChange={onOpenChange} query={query} onQueryChange={setQuery} searchLabel="Search people with access" @@ -134,8 +158,16 @@ export function PullRequestReviewerPicker({ truncated={truncation !== null} truncatedLabel={truncation === null ? "" : TRUNCATED_LABELS[truncation](searchError)} candidateKey={(candidate) => `${candidate.kind}:${candidate.id}`} - disabled={pending !== null} - onSelect={(candidate) => void toggle(candidate)} + onSelect={(candidate) => + setDraft((current) => + toggleSelection( + current, + `${candidate.kind}:${candidate.id}`, + candidate, + candidate.isRequested, + ), + ) + } > {(candidate) => ( <> @@ -143,7 +175,7 @@ export function PullRequestReviewerPicker({ {candidate.kind === "team" ? ( team ) : null} - {candidate.isRequested ? ( + {isSelected(draft, `${candidate.kind}:${candidate.id}`, candidate.isRequested) ? ( ) : null} diff --git a/packages/client-runtime/package.json b/packages/client-runtime/package.json index 6fa96cdd493ae..d303b27c879f9 100644 --- a/packages/client-runtime/package.json +++ b/packages/client-runtime/package.json @@ -563,6 +563,10 @@ "types": "./src/pullRequestReviewers.ts", "default": "./src/pullRequestReviewers.ts" }, + "./pull-request-selection": { + "types": "./src/pullRequestSelection.ts", + "default": "./src/pullRequestSelection.ts" + }, "./provider-clients": { "types": "./src/providerClients.ts", "default": "./src/providerClients.ts" diff --git a/packages/client-runtime/src/pullRequestSelection.test.ts b/packages/client-runtime/src/pullRequestSelection.test.ts new file mode 100644 index 0000000000000..cc8daffc0c7b5 --- /dev/null +++ b/packages/client-runtime/src/pullRequestSelection.test.ts @@ -0,0 +1,48 @@ +import { describe, expect, it } from "@effect/vitest"; + +import { + changeBatches, + emptySelection, + isSelected, + selectionChanges, + toggleSelection, + type SelectionDraft, +} from "./pullRequestSelection.ts"; + +const host = new Set(["bug"]); +const hostSelected = (key: string) => host.has(key); + +function pick(draft: SelectionDraft, key: string) { + return toggleSelection(draft, key, key, hostSelected(key)); +} + +describe("pull request selection draft", () => { + it("collapses several picks into one add and one remove", () => { + let draft = emptySelection(); + for (const key of ["docs", "bug", "feature", "chore", "chore"]) draft = pick(draft, key); + expect(isSelected(draft, "bug", true)).toBe(false); + expect(isSelected(draft, "docs", false)).toBe(true); + expect(selectionChanges(draft, hostSelected)).toEqual({ + add: ["docs", "feature"], + remove: ["bug"], + }); + }); + + it("sends nothing for a pick toggled back", () => { + const draft = pick(pick(emptySelection(), "bug"), "bug"); + expect(draft.size).toBe(0); + expect(selectionChanges(draft, hostSelected)).toEqual({ add: [], remove: [] }); + }); + + it("diffs against the host's state at close, not at the pick", () => { + const draft = pick(pick(emptySelection(), "docs"), "bug"); + const refreshed = new Set(["docs"]); + expect(selectionChanges(draft, (key) => refreshed.has(key))).toEqual({ add: [], remove: [] }); + }); + + it("splits a change into writes the contract accepts", () => { + const labels = Array.from({ length: 26 }, (_, index) => `label-${index}`); + expect(changeBatches(labels).map((batch) => batch.length)).toEqual([25, 1]); + expect(changeBatches([])).toEqual([]); + }); +}); diff --git a/packages/client-runtime/src/pullRequestSelection.ts b/packages/client-runtime/src/pullRequestSelection.ts new file mode 100644 index 0000000000000..24dc86891b59f --- /dev/null +++ b/packages/client-runtime/src/pullRequestSelection.ts @@ -0,0 +1,54 @@ +import { PULL_REQUEST_CHANGE_BATCH_LIMIT } from "@t3tools/contracts"; + +/** Picks that differ from the host, keyed by candidate. Nothing is sent until the picker closes. */ +export type SelectionDraft = ReadonlyMap< + string, + { readonly item: T; readonly selected: boolean } +>; + +export function emptySelection(): SelectionDraft { + return new Map(); +} + +export function isSelected(draft: SelectionDraft, key: string, hostSelected: boolean) { + return draft.get(key)?.selected ?? hostSelected; +} + +export function toggleSelection( + draft: SelectionDraft, + key: string, + item: T, + hostSelected: boolean, +): SelectionDraft { + const next = new Map(draft); + const selected = !isSelected(draft, key, hostSelected); + if (selected === hostSelected) next.delete(key); + else next.set(key, { item, selected }); + return next; +} + +/** + * What to send on close, diffed against the host's latest state rather than the state when the + * pick was made, so a refresh while the picker was open does not resend anything. + */ +export function selectionChanges( + draft: SelectionDraft, + hostSelected: (key: string, item: T) => boolean, +): { readonly add: ReadonlyArray; readonly remove: ReadonlyArray } { + const add: T[] = []; + const remove: T[] = []; + for (const [key, { item, selected }] of draft) { + if (selected === hostSelected(key, item)) continue; + (selected ? add : remove).push(item); + } + return { add, remove }; +} + +/** Splits a change into writes the contract accepts. */ +export function changeBatches(items: ReadonlyArray): ReadonlyArray> { + const batches: Array> = []; + for (let start = 0; start < items.length; start += PULL_REQUEST_CHANGE_BATCH_LIMIT) { + batches.push(items.slice(start, start + PULL_REQUEST_CHANGE_BATCH_LIMIT)); + } + return batches; +} diff --git a/packages/contracts/src/pullRequest.ts b/packages/contracts/src/pullRequest.ts index 30517a6960292..d438080369a40 100644 --- a/packages/contracts/src/pullRequest.ts +++ b/packages/contracts/src/pullRequest.ts @@ -946,18 +946,24 @@ export const PullRequestReactionInput = Schema.Struct({ }); export type PullRequestReactionInput = typeof PullRequestReactionInput.Type; +/** The most labels or reviewers one write may change. */ +export const PULL_REQUEST_CHANGE_BATCH_LIMIT = 25; + export const PullRequestReviewerRequestInput = Schema.Struct({ ...PullRequestRef.fields, reviewers: Schema.Array( Schema.Struct({ id: TrimmedNonEmptyString, kind: PullRequestReviewerKind }), - ).check(Schema.isMinLength(1), Schema.isMaxLength(25)), + ).check(Schema.isMinLength(1), Schema.isMaxLength(PULL_REQUEST_CHANGE_BATCH_LIMIT)), requested: Schema.Boolean, }); export type PullRequestReviewerRequestInput = typeof PullRequestReviewerRequestInput.Type; export const PullRequestLabelChangeInput = Schema.Struct({ ...PullRequestRef.fields, - labels: Schema.Array(TrimmedNonEmptyString).check(Schema.isMinLength(1), Schema.isMaxLength(25)), + labels: Schema.Array(TrimmedNonEmptyString).check( + Schema.isMinLength(1), + Schema.isMaxLength(PULL_REQUEST_CHANGE_BATCH_LIMIT), + ), applied: Schema.Boolean, }); export type PullRequestLabelChangeInput = typeof PullRequestLabelChangeInput.Type; From 02f8a3280b8635d75a64b21587aef678b4391b51 Mon Sep 17 00:00:00 2001 From: Usama Khan Date: Mon, 16 Nov 2026 14:07:36 -0600 Subject: [PATCH 032/253] fix(server): PR watches wake when CI passes before the first sweep (#37741) Co-authored-by: Usama Khan Co-authored-by: Julius Marminge --- .../PullRequestWatchReactor.ts | 19 ++- .../src/orchestration-v2/runtimeLayer.test.ts | 130 ++++++++++++++++++ 2 files changed, 147 insertions(+), 2 deletions(-) diff --git a/apps/server/src/orchestration-v2/PullRequestWatchReactor.ts b/apps/server/src/orchestration-v2/PullRequestWatchReactor.ts index 5b69a7efa3377..3485765a0ed93 100644 --- a/apps/server/src/orchestration-v2/PullRequestWatchReactor.ts +++ b/apps/server/src/orchestration-v2/PullRequestWatchReactor.ts @@ -137,8 +137,9 @@ interface WatchLife { readonly reads: number; } -const lifeKey = ({ thread, link, watch }: WatchTarget) => - `${thread.id} ${threadPullRequestKeyOf(link)} ${watch.startedAt}`; +const lifeKeyOf = (threadId: string, link: ThreadPullRequestLink, watch: ThreadPullRequestWatch) => + `${threadId} ${threadPullRequestKeyOf(link)} ${watch.startedAt}`; +const lifeKey = ({ thread, link, watch }: WatchTarget) => lifeKeyOf(thread.id, link, watch); const minutes = (ms: number) => Math.max(0, Math.round(ms / 60_000)); @@ -863,6 +864,20 @@ export const make = Effect.gen(function* () { ); } }); + // A watch's first read is its baseline, so it reads now: CI that finishes before the next + // sweep would otherwise be taken as old news and never wake the thread. + yield* forkParked( + Stream.runForEach(engine.streamDomainEvents, (event) => { + if (event.type !== "thread.pull-request-synced") return Effect.void; + const unread = visibleThreadPullRequests(event.payload.pullRequests ?? []).flatMap( + (link) => + link.watch === undefined || lives.has(lifeKeyOf(event.threadId, link, link.watch)) + ? [] + : [threadPullRequestKeyOf(link)], + ); + return Effect.forEach(new Set(unread), readOnChange, { discard: true }); + }).pipe(Effect.catchCause(logFailure("pull request watch event stream failed", {}))), + ); yield* forkParked( sweep.pipe( Effect.andThen(follow), diff --git a/apps/server/src/orchestration-v2/runtimeLayer.test.ts b/apps/server/src/orchestration-v2/runtimeLayer.test.ts index 0614a14861abd..b2d12935aa2f8 100644 --- a/apps/server/src/orchestration-v2/runtimeLayer.test.ts +++ b/apps/server/src/orchestration-v2/runtimeLayer.test.ts @@ -21,6 +21,7 @@ import { RuntimeRequestId, TurnItemId, type ModelSelection, + type OrchestrationV2DomainEvent, type OrchestrationV2Run, type OrchestrationV2ShellSnapshot, type OrchestrationV2RuntimeRequest, @@ -5450,6 +5451,135 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => { }), ); + it.effect( + "reads a pull request when its watch starts, so checks that pass before a sweep wake", + () => + Effect.gen(function* () { + const orchestrator = yield* Orchestrator.OrchestratorV2; + const projections = yield* ProjectionStore.ProjectionStoreV2; + const threadId = ThreadId.make("runtime-pull-request-watch-start-read"); + const projectId = ProjectId.make("pr-watch-start-read-project"); + yield* seedProject({ + projectId, + title: "Watch start read", + workspaceRoot: "/workspace/watch-start-read", + defaultModelSelection: null, + createdAt: "2026-10-01T00:00:00.000Z", + }); + yield* orchestrator.dispatch({ + type: "thread.create", + createdBy: "user", + creationSource: "web", + commandId: CommandId.make("pr-watch-start-read-create"), + threadId, + projectId, + title: "Watch start read", + modelSelection, + runtimeMode: "full-access", + interactionMode: "default", + branch: null, + worktreePath: null, + }); + + let checks: PullRequestDetail["checks"] = [ + { name: "lint", status: "pending", description: null, url: null, required: true }, + ]; + const detailReads = yield* Queue.unbounded(); + const watchListings = yield* Queue.unbounded(); + const domainEvents = yield* Queue.unbounded(); + const host = Layer.mergeAll( + NodeServices.layer, + NodeCrypto.layer, + Layer.mock(PullRequestService.PullRequestService)({ + // Earlier tests can leave watches in other projects; only this one's reads count. + detail: (input) => + (input.projectId === projectId + ? Queue.offer(detailReads, undefined) + : Effect.void + ).pipe( + Effect.as({ + ...watchedPullRequestDetail({ + projectId, + number: 16, + at: "2026-10-02T12:00:00.000Z", + }), + headSha: "bbbbbbbbbb", + checks, + }), + ), + activity: () => + Effect.succeed({ + comments: [], + commentCount: 0, + commentsTruncated: false, + reviewThreads: [], + commits: [], + }), + invalidate: () => Effect.void, + changes: () => Stream.never, + }), + // Events come from a queue that exists before the reactor subscribes, so none is missed. + Layer.mock(Orchestrator.OrchestratorV2)({ + dispatch: orchestrator.dispatch, + streamDomainEvents: Stream.fromQueue(domainEvents), + }), + Layer.mock(ProjectionStore.ProjectionStoreV2)({ + getThreadsWithPullRequests: (id) => + projections + .getThreadsWithPullRequests(id) + .pipe(Effect.tap(() => Queue.offer(watchListings, undefined))), + }), + ); + const wakes = Effect.map( + orchestrator.getThreadRecords(threadId, ["messages"]), + ({ messages }) => + messages.filter( + (message) => message.notification !== undefined && message.notification !== null, + ), + ); + const reactor = yield* PullRequestWatchReactor.make.pipe(Effect.provide(host)); + const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; + yield* reactor.start(); + // The first sweep listed watches before this one existed, so it cannot be the read below. + yield* Queue.take(watchListings); + + const key = { host: "github.com", repository: "pingdotgg/t3code", number: 16 }; + const started = yield* orchestrator.dispatch({ + type: "thread.pull-request.watch", + commandId: CommandId.make("pr-watch-start-read-start"), + threadId, + ...key, + watching: true, + link: { url: "https://github.com/pingdotgg/t3code/pull/16", source: "agent" }, + }); + yield* Queue.offerAll( + domainEvents, + started.storedEvents.map((stored) => stored.event), + ); + // The clock never moves, so no sweep can be the read that sees the checks running. + yield* Queue.take(detailReads); + + checks = [ + { name: "lint", status: "success", description: null, url: null, required: true }, + ]; + // A sweep waits for the watch-start read in flight. + yield* reactor.sweep; + yield* worker.drain(); + const woken = yield* wakes; + assert.lengthOf(woken, 1); + assert.equal(woken[0]!.notification?.summary, "#16: checks passed"); + + yield* orchestrator.dispatch({ + type: "thread.pull-request.watch", + commandId: CommandId.make("pr-watch-start-read-end"), + threadId, + ...key, + watching: false, + }); + yield* worker.drain(); + }), + ); + it.effect("wakes for checks that complete after the no-checks notice on the same head", () => Effect.gen(function* () { const orchestrator = yield* Orchestrator.OrchestratorV2; From 7d32075e3757ded707914e2450c3e392a9665765 Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Mon, 16 Nov 2026 12:13:35 -0800 Subject: [PATCH 033/253] feat(web): themes can use full-size background images (#36680) Co-authored-by: Claude Fable 5.2 --- apps/server/src/assets/AssetAccess.test.ts | 24 +++++ apps/server/src/assets/AssetAccess.ts | 37 +++++++ apps/server/src/environmentTheme.test.ts | 29 ++++++ apps/server/src/environmentTheme.ts | 45 ++++++++- .../server/src/mcp/toolkits/theme/handlers.ts | 11 ++- apps/server/src/mcp/toolkits/theme/tools.ts | 2 + apps/server/src/ws.ts | 1 + apps/web/src/appInit.tsx | 2 + apps/web/src/themeBackground.ts | 96 +++++++++++++++++++ apps/web/src/themePalette.ts | 2 + docs/user/appearance.md | 17 +++- evals/mcp-tool-use/cases/themes.ndjson | 1 + evals/mcp-tool-use/run.ts | 3 + packages/contracts/src/assets.ts | 4 + packages/contracts/src/server.ts | 2 + packages/shared/src/themeCss.ts | 2 +- 16 files changed, 271 insertions(+), 7 deletions(-) create mode 100644 apps/web/src/themeBackground.ts diff --git a/apps/server/src/assets/AssetAccess.test.ts b/apps/server/src/assets/AssetAccess.test.ts index d137305317327..8f2e133b2a6e3 100644 --- a/apps/server/src/assets/AssetAccess.test.ts +++ b/apps/server/src/assets/AssetAccess.test.ts @@ -215,6 +215,30 @@ describe("AssetAccess", () => { }).pipe(Effect.provide(layerTest)), ); + it.effect("serves the background image published beside an environment theme", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const { environmentThemesDir } = yield* ServerConfig.ServerConfig; + yield* fs.makeDirectory(environmentThemesDir, { recursive: true }); + const imagePath = `${environmentThemesDir}/frieren.png`; + yield* fs.writeFile(imagePath, screenshotPng); + const result = yield* issueAssetUrl({ + resource: { _tag: "environment-theme-background", themeId: "frieren" }, + }); + const suffix = result.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length); + const separator = suffix.indexOf("/"); + const token = suffix.slice(0, separator); + const asset = yield* resolveAsset(token, suffix.slice(separator + 1)); + expect(asset).toMatchObject({ kind: "file", path: yield* fs.realPath(imagePath) }); + if (asset?.kind !== "file") return; + const response = yield* assetFileResponse(asset); + expect(response.headers["content-type"]).toBe("image/png"); + + yield* fs.remove(imagePath); + expect(yield* resolveAsset(token, suffix.slice(separator + 1))).toBeNull(); + }).pipe(Effect.scoped, Effect.provide(layerTest)), + ); + it.effect("issues exact URLs for media and browser documents outside the workspace", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; diff --git a/apps/server/src/assets/AssetAccess.ts b/apps/server/src/assets/AssetAccess.ts index 5674372bee1a9..362d00fca9230 100644 --- a/apps/server/src/assets/AssetAccess.ts +++ b/apps/server/src/assets/AssetAccess.ts @@ -17,6 +17,7 @@ import { ThreadId, NativeAppIconReference, TurnItemId, + EnvironmentThemeId, } from "@t3tools/contracts"; import { hostPreviewMimeTypeFromExtension, @@ -52,6 +53,7 @@ import * as AssetSigner from "./AssetSigner.ts"; import { makeIsServerInternalPath } from "../auth/serverInternalPaths.ts"; import { parseAttachmentFileExtension, resolveAttachmentPathById } from "../attachmentStore.ts"; import * as ServerConfig from "../config.ts"; +import { findThemeBackground } from "../environmentTheme.ts"; import * as Orchestrator from "../orchestration-v2/Orchestrator.ts"; import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion"; import * as ProjectFaviconResolver from "../project/ProjectFaviconResolver.ts"; @@ -168,6 +170,12 @@ const AssetClaimsSchema = Schema.Union([ app: NativeAppIconReference, expiresAt: Schema.Number, }), + Schema.Struct({ + version: Schema.Literal(1), + kind: Schema.Literal("environment-theme-background"), + themeId: EnvironmentThemeId, + expiresAt: Schema.Number, + }), Schema.Struct({ version: Schema.Literal(1), kind: Schema.Literal("tool-output-image"), @@ -834,6 +842,17 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i imageDimensions = readImageDimensions(image.bytes); break; } + case "environment-theme-background": { + // Published themes go to every client, so any client may read their backgrounds. + claims = { + version: 1, + kind: "environment-theme-background", + themeId: input.resource.themeId, + expiresAt, + }; + fileName = `${input.resource.themeId}-background`; + break; + } case "native-app-icon": { claims = { version: 1, @@ -982,6 +1001,24 @@ export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* ( return image ? ({ kind: "bytes", ...image } satisfies ResolvedAsset) : null; } + if (claims.kind === "environment-theme-background") { + const config = yield* ServerConfig.ServerConfig; + const fileSystem = yield* FileSystem.FileSystem; + // The themes directory may be a symlink; openMediaFile refuses any path that is not canonical. + const themesDir = yield* fileSystem + .realPath(config.environmentThemesDir) + .pipe(Effect.orElseSucceed(() => null)); + const background = themesDir === null ? null : findThemeBackground(themesDir, claims.themeId); + if (background === null) return null; + const file = yield* openMediaFile(background.path).pipe( + Effect.tapError((cause) => + Effect.logError("Failed to open theme background.", { path: background.path, cause }), + ), + Effect.orElseSucceed(() => null), + ); + return file ? ({ kind: "file", path: background.path, file } satisfies ResolvedAsset) : null; + } + if (claims.kind === "native-app-icon") { const nativeAppIconResolver = yield* NativeAppIconResolver.NativeAppIconResolver; const iconPath = yield* nativeAppIconResolver.resolve(claims.app); diff --git a/apps/server/src/environmentTheme.test.ts b/apps/server/src/environmentTheme.test.ts index 241c89546c228..a0bd921ad3f2e 100644 --- a/apps/server/src/environmentTheme.test.ts +++ b/apps/server/src/environmentTheme.test.ts @@ -304,6 +304,35 @@ it.layer(NodeServices.layer)("environment theme", (it) => { ); }); + it.effect( + "reports the image beside a theme as its background, with a new revision per file", + () => + withEnvironmentThemes( + { "look.json": encodeThemeFile(NIGHTFALL_THEME), "look.png": "first" }, + Effect.gen(function* () { + const { environmentThemesDir } = yield* ServerConfig.ServerConfig; + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const background = Effect.map(currentThemes, (themes) => themes[0]?.background); + const image = path.join(environmentThemesDir, "look.png"); + + const first = yield* background; + assert.isDefined(first); + yield* fs.writeFileString(image, "a longer second image"); + assert.notStrictEqual(yield* background, first); + + yield* fs.remove(image); + assert.isUndefined(yield* background); + if (symlinksSupported) { + const outside = path.join(environmentThemesDir, "..", "outside.png"); + yield* fs.writeFileString(outside, "outside"); + yield* fs.symlink(outside, image); + assert.isUndefined(yield* background); + } + }), + ), + ); + it.effect("keeps the served theme when an update does not fit the size limit", () => { const large = (name: string): EnvironmentThemeFile => ({ ...NIGHTFALL_THEME, diff --git a/apps/server/src/environmentTheme.ts b/apps/server/src/environmentTheme.ts index 1e7f4d3052301..0d45fd02de79d 100644 --- a/apps/server/src/environmentTheme.ts +++ b/apps/server/src/environmentTheme.ts @@ -1,6 +1,7 @@ // @effect-diagnostics nodeBuiltinImport:off - the guarded file read needs open // flags (O_NOFOLLOW, O_NONBLOCK) the FileSystem service does not expose. import * as NodeFS from "node:fs"; +import * as NodePath from "node:path"; import { type DefaultAppearancePreference, @@ -52,6 +53,10 @@ const MAX_THEME_FILES = 32; export const MAX_THEME_FILE_BYTES = 6 * THEME_CSS_MAX_LENGTH + 16 * 1024; const MAX_THEME_TOTAL_BYTES = 6 * MAX_THEME_FILE_BYTES; +/** A theme's background is `.` beside `.json`; earlier extensions win. */ +const THEME_BACKGROUND_EXTENSIONS = ["webp", "png", "jpg", "jpeg"] as const; +const MAX_THEME_BACKGROUND_BYTES = 16 * 1024 * 1024; + interface PublishedThemes { readonly seq: number; readonly themes: ReadonlyArray; @@ -101,6 +106,8 @@ export class EnvironmentThemeService extends Context.Service< readonly read: (themeId?: string) => Effect.Effect< { readonly current: string; + /** Where theme files and their background images live. */ + readonly themesDir: string; readonly themes: ReadonlyArray; readonly theme?: EnvironmentTheme; }, @@ -173,6 +180,35 @@ export const readThemeFileGuarded = (filePath: string, maxBytes: number): string } }; +/** + * The background image published beside `themeId`, if any. Symlinks and oversized files are + * skipped. `entries` lets a directory scan skip lookups for names it did not list. + */ +export function findThemeBackground( + themesDir: string, + themeId: string, + entries?: ReadonlySet, +): { readonly path: string; readonly revision: string } | null { + for (const extension of THEME_BACKGROUND_EXTENSIONS) { + const name = `${themeId}.${extension}`; + if (entries !== undefined && !entries.has(name)) continue; + // Native separators: the asset route refuses a path that differs from its canonical form. + const filePath = NodePath.join(themesDir, name); + let info: NodeFS.BigIntStats; + try { + info = NodeFS.lstatSync(filePath, { bigint: true }); + } catch { + continue; + } + if (!info.isFile() || info.size > MAX_THEME_BACKGROUND_BYTES) continue; + return { + path: filePath, + revision: [info.ino, info.size, info.mtimeNs].map((part) => part.toString(36)).join("-"), + }; + } + return null; +} + export const readPublishedThemes = Effect.fn(function* (themesDir: string) { const fs = yield* FileSystem.FileSystem; const entries = yield* fs @@ -180,6 +216,7 @@ export const readPublishedThemes = Effect.fn(function* (themesDir: string) { .pipe(Effect.orElseSucceed((): Array => [])); const themes: Array = []; + const entryNames = new Set(entries); let examined = 0; let totalBytes = 0; for (const entry of entries.toSorted()) { @@ -232,7 +269,8 @@ export const readPublishedThemes = Effect.fn(function* (themesDir: string) { break; } - themes.push({ id, ...file }); + const background = findThemeBackground(themesDir, id, entryNames); + themes.push({ id, ...file, ...(background ? { background: background.revision } : {}) }); } return themes; }); @@ -406,7 +444,8 @@ const make = Effect.gen(function* () { )(function* (themeId) { const { themes } = yield* refresh; const { defaultTheme } = yield* settings.getSettings; - if (themeId === undefined) return { current: defaultTheme, themes }; + const themesDir = environmentThemesDir; + if (themeId === undefined) return { current: defaultTheme, themesDir, themes }; const theme = themes.find((candidate) => candidate.id === themeId); if (theme === undefined) { return yield* new EnvironmentThemeRefusedError({ @@ -415,7 +454,7 @@ const make = Effect.gen(function* () { known: themes.map(({ id }) => id), }); } - return { current: defaultTheme, themes, theme }; + return { current: defaultTheme, themesDir, themes, theme }; }); return { diff --git a/apps/server/src/mcp/toolkits/theme/handlers.ts b/apps/server/src/mcp/toolkits/theme/handlers.ts index ad469095b0e0d..1f8aede7f3996 100644 --- a/apps/server/src/mcp/toolkits/theme/handlers.ts +++ b/apps/server/src/mcp/toolkits/theme/handlers.ts @@ -16,6 +16,7 @@ export const layer = McpToolAccess.toLayer(ThemeToolkit, { const themes = yield* EnvironmentTheme.EnvironmentThemeService; const { current, + themesDir, themes: published, theme, } = yield* themes.read(themeId).pipe( @@ -30,8 +31,14 @@ export const layer = McpToolAccess.toLayer(ThemeToolkit, { return { current, builtIn: BUILT_IN_THEME_IDS, - published: published.map(({ id, name, css }) => ({ id, name, hasCss: Boolean(css) })), - ...(theme === undefined ? {} : { theme: Struct.omit(theme, ["id"]) }), + published: published.map(({ id, name, css, background }) => ({ + id, + name, + hasCss: Boolean(css), + hasBackground: background !== undefined, + })), + ...(theme === undefined ? {} : { theme: Struct.omit(theme, ["id", "background"]) }), + themesDir, guide: THEME_GUIDE, }; }), diff --git a/apps/server/src/mcp/toolkits/theme/tools.ts b/apps/server/src/mcp/toolkits/theme/tools.ts index f055e8776180f..716c540b2c41e 100644 --- a/apps/server/src/mcp/toolkits/theme/tools.ts +++ b/apps/server/src/mcp/toolkits/theme/tools.ts @@ -41,9 +41,11 @@ const ThemeReadTool = Tool.make("t3_theme_read", { id: EnvironmentThemeId, name: Schema.String, hasCss: Schema.Boolean, + hasBackground: Schema.Boolean, }), ), theme: Schema.optionalKey(EnvironmentThemeFile), + themesDir: Schema.String, guide: Schema.String, }), }) diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts index 8e8b30bab3270..6defac543eaac 100644 --- a/apps/server/src/ws.ts +++ b/apps/server/src/ws.ts @@ -3031,6 +3031,7 @@ export const layerWsRpc = ( if ( input.resource._tag === "attachment" || input.resource._tag === "native-app-icon" || + input.resource._tag === "environment-theme-background" || input.resource._tag === "tool-output-image" || input.resource._tag === SOURCE_CONTROL_MEDIA_ASSET || // A network path is served only from the thread's project, looked up below. diff --git a/apps/web/src/appInit.tsx b/apps/web/src/appInit.tsx index faa71137db049..d1873481ee510 100644 --- a/apps/web/src/appInit.tsx +++ b/apps/web/src/appInit.tsx @@ -37,6 +37,7 @@ import { } from "./composerDraftStore"; import { startDefaultThemeAdoption } from "./hooks/useDefaultTheme"; import { startEnvironmentThemeRepaint } from "./hooks/useEnvironmentTheme"; +import { startThemeBackground } from "./themeBackground"; import { readLocalStorageValue, subscribeLocalStorageKey } from "./hooks/useLocalStorage"; import { startHostEnvironmentLookup } from "./state/primaryEnvironment"; import { @@ -264,6 +265,7 @@ export function startAppServices(): AppRouter { startDocumentTitle(router); startHostEnvironmentLookup(); startEnvironmentThemeRepaint(); + startThemeBackground(); startBrowserChromeThemeSync(router); startDefaultThemeAdoption(); startModelPreferencesMigration(); diff --git a/apps/web/src/themeBackground.ts b/apps/web/src/themeBackground.ts new file mode 100644 index 0000000000000..c4bdd1bff5950 --- /dev/null +++ b/apps/web/src/themeBackground.ts @@ -0,0 +1,96 @@ +import { resolveAssetUrl } from "@t3tools/client-runtime/state/assets"; +import { executeAtomQuery } from "@t3tools/client-runtime/state/runtime"; +import type { EnvironmentId } from "@t3tools/contracts"; + +import { appAtomRegistry, subscribeAtomChanges } from "./rpc/atomRegistry"; +import { assetEnvironment } from "./state/assets"; +import { themeEnvironmentIdAtom } from "./state/primaryEnvironment"; +import { themeServerEnvironmentThemesAtom } from "./state/server"; +import { environmentSession } from "./state/session"; + +const THEME_BACKGROUND_VARIABLE = "--theme-background-image"; + +let shownKey: string | null = null; +let loading: AbortController | null = null; +let objectUrl: string | null = null; + +function show(nextUrl: string | null): void { + const root = document.documentElement; + if (nextUrl === null) root.style.removeProperty(THEME_BACKGROUND_VARIABLE); + else root.style.setProperty(THEME_BACKGROUND_VARIABLE, `url("${nextUrl}")`); + if (objectUrl !== null) URL.revokeObjectURL(objectUrl); + objectUrl = nextUrl; +} + +async function loadBackground( + environmentId: EnvironmentId, + themeId: string, + signal: AbortSignal, +): Promise { + const minted = await executeAtomQuery( + appAtomRegistry, + assetEnvironment.createUrl({ + environmentId, + input: { resource: { _tag: "environment-theme-background", themeId } }, + }), + { label: "theme background", reportFailure: false, refresh: true, signal }, + ); + const connection = appAtomRegistry.get( + environmentSession.preparedConnectionValueAtom(environmentId), + ); + if (minted._tag !== "Success" || connection._tag === "None") return null; + const url = resolveAssetUrl(connection.value.httpBaseUrl, minted.value.relativeUrl); + if (url === null) return null; + const response = await fetch(url, { signal }); + if (!response.ok) return null; + const image = await response.blob(); + return image.type.startsWith("image/") ? URL.createObjectURL(image) : null; +} + +/** + * Sets `--theme-background-image` to the image published beside `themeId`, or clears it. + * The image is copied into a blob URL once per revision, so the signed URL may expire. + */ +export function applyThemeBackground(themeId: string | undefined): void { + if (typeof document === "undefined") return; + const environmentId = appAtomRegistry.get(themeEnvironmentIdAtom); + const revision = + themeId === undefined + ? undefined + : appAtomRegistry.get(themeServerEnvironmentThemesAtom)?.find((theme) => theme.id === themeId) + ?.background; + const key = + environmentId === null || themeId === undefined || revision === undefined + ? null + : JSON.stringify([environmentId, themeId, revision]); + if (key === shownKey) return; + shownKey = key; + loading?.abort(); + loading = null; + // The previous image must not show under the next theme while it loads. + show(null); + if (environmentId === null || themeId === undefined || key === null) return; + const controller = new AbortController(); + loading = controller; + const failed = () => { + if (controller.signal.aborted) return; + // Lets the next repaint try again. + shownKey = null; + }; + loadBackground(environmentId, themeId, controller.signal).then((url) => { + if (controller.signal.aborted) { + if (url !== null) URL.revokeObjectURL(url); + return; + } + loading = null; + if (url === null) failed(); + else show(url); + }, failed); +} + +/** A new image revision does not change the palette, so it does not repaint the theme. */ +export function startThemeBackground(): () => void { + return subscribeAtomChanges(themeServerEnvironmentThemesAtom, () => + applyThemeBackground(document.documentElement.dataset.themeId), + ); +} diff --git a/apps/web/src/themePalette.ts b/apps/web/src/themePalette.ts index 1abd73b2cecb7..5e85922759ddb 100644 --- a/apps/web/src/themePalette.ts +++ b/apps/web/src/themePalette.ts @@ -30,6 +30,7 @@ import { } from "./lib/contextChipAccents"; import { appAtomRegistry, subscribeAtomChanges } from "./rpc/atomRegistry"; +import { applyThemeBackground } from "./themeBackground"; import { applyThemeCss } from "./themeCss"; import { themeServerEnvironmentThemesAtom } from "./state/server"; @@ -1681,6 +1682,7 @@ export function applyThemePalette(theme: ThemePreference, appearance?: ThemeAppe setThemePreviewAppearance(null); const palette = getThemeDefinition(theme); applyThemeCss(palette?.css); + applyThemeBackground(palette?.id); if (palette) { root.dataset.themeId = palette.id; diff --git a/docs/user/appearance.md b/docs/user/appearance.md index c37855fdf3c87..340e9e5ff5f1f 100644 --- a/docs/user/appearance.md +++ b/docs/user/appearance.md @@ -62,7 +62,7 @@ the theme, applies after T3 Code's own styles on web and desktop, and is include Custom CSS cannot load files. `@import`, `image-set()`, and `url()` are ignored, except `url()` with a `data:` address or an `#id`. This keeps an imported theme from sending what is on your screen -to another site. +to another site. For a full-size image, use a [background image](#background-image). Class names in the app change between releases. For rules that keep working, target `data-slot` and other `data-` attributes, such as `[data-chat-header]` or `[data-slot="button"]`. @@ -132,3 +132,18 @@ view. An optional `css` string adds [custom CSS](#custom-css). Write updates to a temporary file and rename it into place so clients never read a partial theme. Invalid files are not published. + +### Background image + +A published theme can have a full-size background image. Ask an agent, for example "use +~/Pictures/wallpaper.jpg as T3 Code's background", or save a JPEG, PNG, or WebP file of up to +16 MB beside the theme file: `nightfall.jpg` beside `nightfall.json`. The theme's CSS decides +where the image shows: + +```css +[data-workspace-area="conversation"] { + background: var(--theme-background-image) center / cover no-repeat; +} +``` + +Delete the image file to remove it. diff --git a/evals/mcp-tool-use/cases/themes.ndjson b/evals/mcp-tool-use/cases/themes.ndjson index 462086e6b7a48..93f2c76a99e4d 100644 --- a/evals/mcp-tool-use/cases/themes.ndjson +++ b/evals/mcp-tool-use/cases/themes.ndjson @@ -1,3 +1,4 @@ {"id": "restyle-t3-code-uses-theme-set", "area": "themes", "kind": "guard", "pattern": "restyle T3 Code itself", "experience": "Asked to change how T3 Code itself looks, an agent reads the theme guide, then publishes a theme with t3_theme_set. It does not write theme files by hand or edit the project.", "rule": "t3_theme_set: Restyle T3 Code itself, only when the user asks.", "privacy": "commit-safe", "source": {"pullRequest": "https://github.com/pingdotgg/t3code/pull/29293", "note": "A user needed a look the app had no setting for."}, "setup": {"role": "top", "mocks": {"t3_theme_read": {"current": "", "builtIn": ["t3-chat", "grove", "ocean", "ember", "iris"], "published": [], "guide": "Themes restyle T3 Code on web and desktop. `canvas` and `accent` (hex) derive a full palette. `css` applies after the app's styles."}, "t3_theme_set": {"themeId": "night-terminal", "published": true}}}, "prompt": "Make T3 Code itself look like a late-night terminal: near-black background with a green accent.", "expect": {"calls": [{"tool": "t3_theme_set", "args": {"theme.appearance": "dark"}}], "order": ["t3_theme_read", "t3_theme_set"]}, "maxSteps": 6} {"id": "control-project-theme-edit-leaves-t3-code-alone", "area": "themes", "kind": "control", "pattern": "ordinary task: edit the project's own theme", "experience": "The user's project has a theme of its own. A request to change it is a file edit. T3 Code's look must not change.", "rule": "t3_theme_set: Restyle T3 Code itself, only when the user asks.", "privacy": "commit-safe", "setup": {"role": "top", "world": {"files": {"src/theme.css": ":root { --accent: #3b82f6; }\n.dark { --accent: #60a5fa; }\n"}}}, "prompt": "Change the dark theme accent to green.", "expect": {"notCalls": ["t3_theme_set", "t3_theme_read"], "textMust": ["theme\\.css"]}, "maxSteps": 6, "source": {"pullRequest": "https://github.com/pingdotgg/t3code/pull/35247", "note": "A real Haiku turn in the dev app during this work: asked to change a project's theme file, it edited the file and left T3 Code alone."}} {"id": "dark-mode-request-sets-appearance", "area": "themes", "kind": "guard", "pattern": "switch T3 Code to dark mode", "experience": "Asked to switch T3 Code to dark mode, an agent sets the appearance with t3_theme_set. It does not tell the user to do it by hand, and it does not publish a new theme.", "rule": "t3_theme_set: `appearance` sets light, dark, or system mode.", "privacy": "commit-safe", "source": {"pullRequest": "https://github.com/pingdotgg/t3code/pull/35247", "note": "In the dev app, an agent asked to swap to dark mode said it could not and sent the user to Settings."}, "setup": {"role": "top", "mocks": {"t3_theme_read": {"current": "sumi", "builtIn": ["t3-chat", "grove", "ocean", "ember", "iris"], "published": [{"id": "sumi", "name": "Sumi", "hasCss": true}], "guide": "Themes restyle T3 Code on web and desktop."}, "t3_theme_set": {"appearance": "dark", "published": false}}}, "prompt": "swap me to dark mode", "expect": {"calls": [{"tool": "t3_theme_set", "args": {"appearance": "dark"}}]}, "maxSteps": 4} +{"id": "theme-background-uses-image-file", "area": "themes", "kind": "bad", "pattern": "put a full-size image behind T3 Code", "experience": "Asked to use a 2.3 MB wallpaper as T3 Code's background, an agent could only embed it as data: text in theme CSS, hit the 20,000 character limit, and told the user T3 Code could not do it.", "rule": "THEME_GUIDE: For a background image, copy a JPEG, PNG, or WebP file to `/.jpg`, beside the theme file. CSS shows it with `var(--theme-background-image)`.", "privacy": "commit-safe", "source": {"note": "A user on X shared an agent turn that shrank a wallpaper to fit theme CSS, then said a full-size image needed a T3 Code change."}, "setup": {"role": "top", "mocks": {"t3_theme_read": {"current": "sumi", "builtIn": ["t3-chat", "grove", "ocean", "ember", "iris"], "published": [{"id": "sumi", "name": "Sumi", "hasCss": true, "hasBackground": false}], "theme": {"name": "Sumi", "appearance": "dark", "canvas": "#16161d", "accent": "#c4746e", "css": "[data-chat-header] { border-bottom: 1px solid #2a2a37; }"}, "guide": "Themes restyle T3 Code on web and desktop. `canvas` and `accent` (hex) derive a full palette. `css` applies after the app's styles.", "themesDir": "/home/user/.t3/userdata/themes"}, "t3_theme_set": {"themeId": "sumi", "published": true}}}, "prompt": "Put /home/user/Downloads/frieren.jpeg behind T3 Code as the background of my Sumi theme. It's 2.3 MB and I want the full quality, not a shrunk copy.", "expect": {"shellMust": ["frieren\\.jpeg.*/home/user/\\.t3/userdata/themes/sumi\\.jpe?g"], "calls": [{"tool": "t3_theme_set", "args": {"themeId": "sumi", "theme.css": "~var(--theme-background-image)"}}]}, "maxSteps": 8} diff --git a/evals/mcp-tool-use/run.ts b/evals/mcp-tool-use/run.ts index 5786de748eaf1..b08cb8a490de3 100644 --- a/evals/mcp-tool-use/run.ts +++ b/evals/mcp-tool-use/run.ts @@ -48,6 +48,7 @@ import { ThreadId, } from "@t3tools/contracts"; import { projectComposerContextForProvider } from "@t3tools/shared/composerContextReferences"; +import { THEME_GUIDE } from "@t3tools/shared/themeCss"; import { formatThreadLink } from "@t3tools/shared/threadLinks"; import { buildCodexAdditionalContext } from "../../apps/server/src/provider/CodexDeveloperInstructions.ts"; import { makeSideChatContext } from "../../apps/server/src/orchestration-v2/ContextHandoffService.ts"; @@ -1371,6 +1372,8 @@ const unsavedSecretNote = (Contracts as Record).SECRET_REQUEST_ /** Fields T3 adds to real thread and task results, applied to mocks and defaults alike. */ const withProductFields = (name: string, value: unknown, child: boolean): unknown => { if (!isRecord(value)) return value; + // The guide is the checkout's text, so a case's mock cannot go stale. + if (name === "t3_theme_read") return { ...value, guide: THEME_GUIDE }; const linked = (thread: unknown, idKey = "threadId") => { if (!isRecord(thread) || thread.link !== undefined) return thread; const id = thread[idKey] ?? thread.id; diff --git a/packages/contracts/src/assets.ts b/packages/contracts/src/assets.ts index 58b7ce1d18530..0a1e711c39227 100644 --- a/packages/contracts/src/assets.ts +++ b/packages/contracts/src/assets.ts @@ -8,6 +8,7 @@ import { } from "./chatAttachment.ts"; import { ProjectFaviconPath } from "./project.ts"; import { ToolActivityNativeAppReference } from "./providerRuntime.ts"; +import { EnvironmentThemeId } from "./server.ts"; const ASSET_PATH_MAX_LENGTH = 1024; @@ -66,6 +67,9 @@ export const AssetResource = Schema.Union([ // to a request that carries a credential, which the client has none of, so the server fetches // them with the credential the host's driver holds for the repository at `cwd`. `repository` // names the change request's repository, which can differ from the checkout at `cwd`. + Schema.TaggedStruct("environment-theme-background", { + themeId: EnvironmentThemeId, + }), Schema.TaggedStruct(SOURCE_CONTROL_MEDIA_ASSET, { cwd: TrimmedNonEmptyString.check(Schema.isMaxLength(ASSET_PATH_MAX_LENGTH)), url: TrimmedNonEmptyString.check(Schema.isMaxLength(2048)), diff --git a/packages/contracts/src/server.ts b/packages/contracts/src/server.ts index 144d2814b8804..f2e02c6b0a95e 100644 --- a/packages/contracts/src/server.ts +++ b/packages/contracts/src/server.ts @@ -653,6 +653,8 @@ export type EnvironmentThemeFile = typeof EnvironmentThemeFile.Type; export const EnvironmentTheme = Schema.Struct({ id: EnvironmentThemeId, ...environmentThemeFields, + /** Revision of the image file published beside the theme; absent without one. */ + background: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(64))), }); export type EnvironmentTheme = typeof EnvironmentTheme.Type; diff --git a/packages/shared/src/themeCss.ts b/packages/shared/src/themeCss.ts index 26a72b4117d99..910b18150fdea 100644 --- a/packages/shared/src/themeCss.ts +++ b/packages/shared/src/themeCss.ts @@ -79,7 +79,7 @@ export const THEME_GUIDE = [ "", "CSS: `css` applies after the app's styles. Use `.dark` or `:root:not(.dark)` for one appearance.", "It cannot load files: no @import, no image-set(), and url() only with data: or #id.", - "A background image must be embedded as a data: address. `css` holds 50,000 characters, so keep the image near 30 KB.", + 'For a background image, copy a JPEG, PNG, or WebP file of up to 16 MB to `/.jpg` (or .png, .webp), beside the theme file. CSS shows it with `var(--theme-background-image)`, such as `[data-workspace-area="conversation"] { background: var(--theme-background-image) center / cover no-repeat; }`. Delete the file to remove it. Do not embed images in `css`: it holds 50,000 characters.', "The app's own rules can be more specific. If a rule has no effect, add !important.", "It cannot reach the terminal text, the file tree, or diff internals. Use color roles for those.", "Native scrollbars take `::-webkit-scrollbar` and `::-webkit-scrollbar-thumb`.", From cfb1f125412d40a78979c2e8056556266ede19da Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 12:15:39 -0800 Subject: [PATCH 034/253] fix(mobile): one shared upload per attachment, and new threads keep their project (#37682) Co-authored-by: Claude Opus 5.5 --- .../features/threads/NewTaskDraftScreen.tsx | 18 +- .../threads/new-task-flow-provider.test.tsx | 27 +- .../threads/new-task-flow-provider.tsx | 40 +- apps/mobile/src/lib/attachmentUpload.test.ts | 1011 +++++++++-------- apps/mobile/src/lib/attachmentUpload.ts | 275 +++-- .../lib/composerAttachmentUploadQueue.test.ts | 82 +- .../src/lib/composerAttachmentUploadQueue.ts | 222 ++-- .../state/composer-attachment-uploads.test.ts | 3 +- .../src/state/composer-attachment-uploads.ts | 178 +-- .../mobile/src/state/thread-outbox-manager.ts | 41 + apps/mobile/src/state/thread-outbox-model.ts | 3 + apps/mobile/src/state/thread-outbox.test.ts | 36 + apps/mobile/src/state/thread-outbox.ts | 8 + .../threadOutboxDelivery.permissions.test.ts | 40 +- .../src/state/threadOutboxDelivery.test.ts | 46 +- apps/mobile/src/state/threadOutboxDelivery.ts | 41 +- .../src/state/use-composer-drafts.test.ts | 114 +- .../src/state/use-thread-composer-state.ts | 16 +- 18 files changed, 1267 insertions(+), 934 deletions(-) diff --git a/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx b/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx index 201204a8a57bf..ef279152d2581 100644 --- a/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx +++ b/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx @@ -431,15 +431,16 @@ export function NewTaskDraftScreen(props: { if (appliedInitialProjectKeyRef.current === directProjectKey) { return; } + // setProject creates the draft, so it also runs when the fallback already shows this project. + const showsDirectProject = + flow.draftKey !== null && + selectedProject?.environmentId === directProject.environmentId && + selectedProject.id === directProject.id; if (props.initialProjectRef?.branch) { - if ( - selectedProject?.environmentId !== directProject.environmentId || - selectedProject.id !== directProject.id - ) { + if (!showsDirectProject || !flow.draftKey) { setProject(directProject); return; } - if (!flow.draftKey) return; // Checkout completed before mount; local mode reuses it while worktree mode would create another. updateComposerDraftSettings(flow.draftKey, { workspaceSelection: { @@ -451,12 +452,7 @@ export function NewTaskDraftScreen(props: { }); } appliedInitialProjectKeyRef.current = directProjectKey; - if ( - selectedProject?.environmentId === directProject.environmentId && - selectedProject.id === directProject.id - ) { - return; - } + if (showsDirectProject) return; setProject(directProject); return; } diff --git a/apps/mobile/src/features/threads/new-task-flow-provider.test.tsx b/apps/mobile/src/features/threads/new-task-flow-provider.test.tsx index 05ccf29b2ae70..0972d937696e1 100644 --- a/apps/mobile/src/features/threads/new-task-flow-provider.test.tsx +++ b/apps/mobile/src/features/threads/new-task-flow-provider.test.tsx @@ -14,7 +14,7 @@ import * as Layer from "effect/Layer"; import * as Queue from "effect/Queue"; import * as Stream from "effect/Stream"; import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity"; -import { act, useLayoutEffect } from "react"; +import { act, useEffect, useLayoutEffect } from "react"; import { createRoot } from "react-dom/client"; import { expect, it, vi } from "vite-plus/test"; import type { ComposerDraft } from "../../state/use-composer-drafts"; @@ -213,11 +213,16 @@ function setup() { mocks.writeSettings.mockImplementation(write); mocks.writeText.mockImplementation((key, text) => write(key, { text })); let current: ReturnType; - function Probe() { + function Probe(props: { readonly pickOnMount?: EnvironmentProject }) { const value = useNewTaskFlow(); useLayoutEffect(() => { current = value; }); + // Mirrors the draft screen applying its route project on mount, before the provider's effects run. + useEffect(() => { + if (props.pickOnMount) value.setProject(props.pickOnMount); + // oxlint-disable-next-line react/exhaustive-effect-dependencies -- picks once on mount; setProject changes every render + }, []); return null; } const root = createRoot(document.createElement("div")); @@ -245,16 +250,20 @@ function setup() { get current() { return current; }, - async render(active: boolean) { + async render(active: boolean, pickOnMount?: EnvironmentProject) { await act(async () => root.render( - + , ), ); + // The draft screen applies a project whenever it shows without a draft. + if (active && current.draftKey === null) { + await act(async () => current.setProject(projects[0]!)); + } }, async emit(cwd: string, branch: string, isRepo = true) { await act(async () => emitters.get(cwd)!(branch, isRepo)); @@ -270,6 +279,16 @@ function setup() { }; } +it("keeps the draft on a project the screen picks as it mounts", async () => { + using screen = setup(); + await screen.render(true, screen.projects[2]); + expect(screen.current.selectedProject).toEqual(screen.projects[2]); + expect(screen.snapshot(screen.current.draftKey).project).toEqual({ + environmentId: "two", + projectId: "b", + }); +}); + it("owns a status stream only while the draft is active, retaining draft choices on reopen", async () => { using screen = setup(); await screen.render(false); diff --git a/apps/mobile/src/features/threads/new-task-flow-provider.tsx b/apps/mobile/src/features/threads/new-task-flow-provider.tsx index 5852206095a8f..83d87d6eba882 100644 --- a/apps/mobile/src/features/threads/new-task-flow-provider.tsx +++ b/apps/mobile/src/features/threads/new-task-flow-provider.tsx @@ -2,7 +2,7 @@ import { ActiveNewTaskContext } from "../../state/active-new-task"; import { resolveNewThreadModelSelection } from "@t3tools/shared/usageLimits"; import { useNowMinuteMs } from "../../lib/clock"; import * as Dialog from "../../components/ui/Dialog"; -import React, { useEffect, useRef, useState } from "react"; +import React, { useRef, useState } from "react"; import type { EnvironmentId, @@ -304,11 +304,23 @@ export function NewTaskFlowProvider( // binding outlives the draft's project stamp (submit clears it). const updateFlow = (patch: Partial) => updateNewTaskFlowState(flowKey, { ...readFlow(), ...patch }); + // A draft's project stamp is the selection: picking a project writes it, and uploads and + // the send read it, so the screen cannot show one project while the draft holds another. + const draftStamp = useComposerDraft(activeDraftKey).project; + const stampedProject = + draftStamp === undefined + ? undefined + : projects.find( + (project) => + project.environmentId === draftStamp.environmentId && + project.id === draftStamp.projectId, + ); const selectedEnvironmentId = - selectedEnvironmentIdOverride !== null && + stampedProject?.environmentId ?? + (selectedEnvironmentIdOverride !== null && projects.some((project) => project.environmentId === selectedEnvironmentIdOverride) ? selectedEnvironmentIdOverride - : (projects[0]?.environmentId ?? null); + : (projects[0]?.environmentId ?? null)); const projectsForEnvironment = pipe( projects, Arr.filter((project) => project.environmentId === selectedEnvironmentId), @@ -319,7 +331,8 @@ export function NewTaskFlowProvider( const selectedProject = routeDraftResolution.kind === "pending" ? null - : (projectsForEnvironment.find( + : (stampedProject ?? + projectsForEnvironment.find( (project) => scopedProjectKey(project.environmentId, project.id) === selectedProjectKey, ) ?? projectsForEnvironment[0] ?? @@ -333,25 +346,6 @@ export function NewTaskFlowProvider( const serverConfigs = useServerConfigs(); const { connectedEnvironments } = useRemoteConnectionStatus(); const selectedProjectDraftKey = selectedProject ? activeDraftKey : null; - // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- creates the persistent draft in the new-task flow store once a project is selected, before the composer mounts - useEffect(() => { - // A route that names a draft binds that draft, or returns to the picker. - if ( - props.active === false || - props.routeDraftId !== undefined || - activeDraftKey !== null || - selectedProject === null - ) { - return; - } - // Creates the persistent draft once the project selection is ready; the key must exist before the composer mounts. - updateNewTaskFlowState(flowKey, { - draftKey: createNewTaskDraft({ - environmentId: selectedProject.environmentId, - projectId: selectedProject.id, - }), - }); - }, [activeDraftKey, flowKey, selectedProject, props.active, props.routeDraftId]); const activePendingKey = props.active === false ? null diff --git a/apps/mobile/src/lib/attachmentUpload.test.ts b/apps/mobile/src/lib/attachmentUpload.test.ts index 0097ab7ed77c0..fb7fa933fbca9 100644 --- a/apps/mobile/src/lib/attachmentUpload.test.ts +++ b/apps/mobile/src/lib/attachmentUpload.test.ts @@ -1,6 +1,7 @@ import { EnvironmentId, PROVIDER_SEND_TURN_MAX_IMAGE_BYTES } from "@t3tools/contracts"; import * as Option from "effect/Option"; -import { beforeEach, describe, expect, it, vi } from "vite-plus/test"; +import { describe, expect, it } from "@effect/vitest"; +import { beforeEach, vi } from "vite-plus/test"; const mocks = vi.hoisted(() => ({ canOperate: true, @@ -89,11 +90,51 @@ vi.mock("expo-file-system", () => ({ UploadType: { BINARY_CONTENT: 0 }, })); +import * as Effect from "effect/Effect"; +import * as Exit from "effect/Exit"; +import * as Fiber from "effect/Fiber"; +import { uploadError } from "./composerAttachmentUploadQueue"; import { - prepareTurnAttachments, + prepareTurnAttachments as prepareWithUploads, releasePendingAttachmentUploads, + uploadComposerAttachment, withUploadedMobileAttachmentReferences, } from "./attachmentUpload"; + +/** Uploads each attachment directly, as the shared registry does for one holder. */ +function prepare( + input: Omit[0], "uploadAttachment"> & { + readonly onUploadProgress?: (attachmentId: string, progress: number) => void; + }, +) { + return prepareWithUploads({ + ...input, + // An interrupted send waits for its transfer to settle, as the shared registry does. + uploadAttachment: (attachment) => + Effect.suspend(() => { + const controller = new AbortController(); + const upload = uploadComposerAttachment( + input.environmentId, + attachment, + controller.signal, + input.onUploadProgress + ? (progress) => input.onUploadProgress?.(attachment.id, progress) + : undefined, + ); + return Effect.tryPromise({ try: () => upload, catch: uploadError }).pipe( + Effect.onInterrupt(() => + Effect.promise(() => { + controller.abort(); + return upload.then( + () => undefined, + () => undefined, + ); + }), + ), + ); + }), + }); +} import type { DraftComposerAttachment } from "./composerImages"; const environmentId = EnvironmentId.make("environment-1"); @@ -176,68 +217,80 @@ describe("prepareTurnAttachments", () => { mocks.upload.mockResolvedValue({ status: 204, body: "", headers: {} }); }); - it("does not mint or transfer attachments without task operation access", async () => { - mocks.canOperate = false; - await expect( - prepareTurnAttachments({ - environmentId, - attachments: [fileBackedImage], - supportsImageUploads: true, - }), - ).rejects.toThrow("cannot upload attachments"); - expect(mocks.runAtomCommand).not.toHaveBeenCalled(); - expect(mocks.upload).not.toHaveBeenCalled(); - }); - - it("rechecks access after a signed URL is minted before sending any bytes", async () => { - mocks.runAtomCommand.mockImplementationOnce(async () => { + it.effect("does not mint or transfer attachments without task operation access", () => + Effect.gen(function* () { mocks.canOperate = false; - return { - _tag: "Success", - value: { - attachmentId: MINTED_ID, - relativeUrl: "/api/attachments/upload/signed", - expiresAt: 1, - }, - }; - }); - const warning = vi.spyOn(console, "warn").mockImplementation(() => {}); - try { - await expect( - prepareTurnAttachments({ - environmentId, - attachments: [fileBackedImage], - supportsImageUploads: true, - }), - ).rejects.toThrow("cannot upload attachments"); + expect( + (yield* Effect.flip( + prepare({ + environmentId, + attachments: [fileBackedImage], + supportsImageUploads: true, + }), + )).message, + ).toContain("cannot upload attachments"); + expect(mocks.runAtomCommand).not.toHaveBeenCalled(); expect(mocks.upload).not.toHaveBeenCalled(); - expect(mocks.runAtomCommand).toHaveBeenCalledTimes(1); - } finally { - warning.mockRestore(); - } - }); + }), + ); - it("does not replace an expired upload when access was revoked during verification", async () => { - mocks.executeAtomQuery.mockImplementationOnce(async () => { - mocks.canOperate = false; - return { _tag: "Failure", error: { _tag: "AssetAttachmentNotFoundError" } }; - }); - await expect( - prepareTurnAttachments({ - environmentId, - attachments: [ - { - ...fileBackedImage, - uploadedAttachmentId: MINTED_ID, - uploadEnvironmentId: environmentId, + it.effect("rechecks access after a signed URL is minted before sending any bytes", () => + Effect.gen(function* () { + mocks.runAtomCommand.mockImplementationOnce(async () => { + mocks.canOperate = false; + return { + _tag: "Success", + value: { + attachmentId: MINTED_ID, + relativeUrl: "/api/attachments/upload/signed", + expiresAt: 1, }, - ], - supportsImageUploads: true, - }), - ).rejects.toThrow("cannot upload attachments"); - expect(mocks.runAtomCommand).not.toHaveBeenCalled(); - expect(mocks.upload).not.toHaveBeenCalled(); - }); + }; + }); + const warning = vi.spyOn(console, "warn").mockImplementation(() => {}); + try { + expect( + (yield* Effect.flip( + prepare({ + environmentId, + attachments: [fileBackedImage], + supportsImageUploads: true, + }), + )).message, + ).toContain("cannot upload attachments"); + expect(mocks.upload).not.toHaveBeenCalled(); + expect(mocks.runAtomCommand).toHaveBeenCalledTimes(1); + } finally { + warning.mockRestore(); + } + }), + ); + + it.effect("does not replace an expired upload when access was revoked during verification", () => + Effect.gen(function* () { + mocks.executeAtomQuery.mockImplementationOnce(async () => { + mocks.canOperate = false; + return { _tag: "Failure", error: { _tag: "AssetAttachmentNotFoundError" } }; + }); + expect( + (yield* Effect.flip( + prepare({ + environmentId, + attachments: [ + { + ...fileBackedImage, + uploadedAttachmentId: MINTED_ID, + uploadEnvironmentId: environmentId, + }, + ], + supportsImageUploads: true, + }), + )).message, + ).toContain("cannot upload attachments"); + expect(mocks.runAtomCommand).not.toHaveBeenCalled(); + expect(mocks.upload).not.toHaveBeenCalled(); + }), + ); it("rechecks deletion permission before retrying a failed cleanup", async () => { mocks.runAtomCommand.mockImplementationOnce(async () => { @@ -250,256 +303,272 @@ describe("prepareTurnAttachments", () => { expect(mocks.runAtomCommand).toHaveBeenCalledTimes(1); }); - it("inlines a file-backed image lazily when the server lacks image uploads", async () => { - const prepared = await prepareTurnAttachments({ - environmentId, - attachments: [fileBackedImage], - }); + it.effect("inlines a file-backed image lazily when the server lacks image uploads", () => + Effect.gen(function* () { + const prepared = yield* prepare({ + environmentId, + attachments: [fileBackedImage], + }); - expect(prepared.status).toBe("ready"); - if (prepared.status !== "ready") return; - expect(prepared.attachments).toEqual([ - { - type: "image", - name: "photo.png", - mimeType: "image/png", - sizeBytes: 3, - dataUrl: "data:image/png;base64,YWJj", - }, - ]); - expect(mocks.readBase64).toHaveBeenCalledExactlyOnceWith(fileBackedImage.fileUri); - expect(mocks.upload).not.toHaveBeenCalled(); - }); + expect(prepared.status).toBe("ready"); + if (prepared.status !== "ready") return; + expect(prepared.attachments).toEqual([ + { + type: "image", + name: "photo.png", + mimeType: "image/png", + sizeBytes: 3, + dataUrl: "data:image/png;base64,YWJj", + }, + ]); + expect(mocks.readBase64).toHaveBeenCalledExactlyOnceWith(fileBackedImage.fileUri); + expect(mocks.upload).not.toHaveBeenCalled(); + }), + ); // regression: #9713 read file-backed image drafts before enabling them - it("reads restored images from the current iOS document container for legacy sends", async () => { - const fileName = "33333333-3333-4333-8333-333333333333-photo.png"; - mocks.documentUri = - "file:///var/mobile/Containers/Data/Application/22222222-2222-4222-8222-222222222222/Documents"; - const oldUri = `file:///var/mobile/Containers/Data/Application/11111111-1111-4111-8111-111111111111/Documents/t3-composer-attachments/${fileName}`; - const prepared = await prepareTurnAttachments({ - environmentId, - attachments: [{ ...fileBackedImage, fileUri: oldUri, previewUri: oldUri }], - }); - expect(prepared.status).toBe("ready"); - expect(mocks.readBase64).toHaveBeenCalledExactlyOnceWith( - `${mocks.documentUri}/t3-composer-attachments/${fileName}`, - ); - }); + it.effect("reads restored images from the current iOS document container for legacy sends", () => + Effect.gen(function* () { + const fileName = "33333333-3333-4333-8333-333333333333-photo.png"; + mocks.documentUri = + "file:///var/mobile/Containers/Data/Application/22222222-2222-4222-8222-222222222222/Documents"; + const oldUri = `file:///var/mobile/Containers/Data/Application/11111111-1111-4111-8111-111111111111/Documents/t3-composer-attachments/${fileName}`; + const prepared = yield* prepare({ + environmentId, + attachments: [{ ...fileBackedImage, fileUri: oldUri, previewUri: oldUri }], + }); + expect(prepared.status).toBe("ready"); + expect(mocks.readBase64).toHaveBeenCalledExactlyOnceWith( + `${mocks.documentUri}/t3-composer-attachments/${fileName}`, + ); + }), + ); // regression: #9713 read file-backed image drafts before enabling them - it("rejects a legacy image without inline bytes or a file", async () => { - const { fileUri: _, ...missingImage } = fileBackedImage; - await expect( - prepareTurnAttachments({ environmentId, attachments: [missingImage] }), - ).rejects.toThrow("'photo.png' is no longer available. Attach the image again."); - }); + it.effect("rejects a legacy image without inline bytes or a file", () => + Effect.gen(function* () { + const { fileUri: _, ...missingImage } = fileBackedImage; + expect( + (yield* Effect.flip(prepare({ environmentId, attachments: [missingImage] }))).message, + ).toContain("'photo.png' is no longer available. Attach the image again."); + }), + ); - it.each(["resolve", "reject"] as const)( + it.effect.each(["resolve", "reject"] as const)( "abandons a canceled legacy image read that later %ss", - async (outcome) => { - const readStarted = Promise.withResolvers(); - const read = Promise.withResolvers(); - const controller = new AbortController(); - mocks.readBase64.mockImplementation(() => { - readStarted.resolve(); - return read.promise; - }); - const preparing = prepareTurnAttachments({ + (outcome) => + Effect.gen(function* () { + const readStarted = Promise.withResolvers(); + const read = Promise.withResolvers(); + mocks.readBase64.mockImplementation(() => { + readStarted.resolve(); + return read.promise; + }); + const preparing = yield* Effect.forkChild( + prepare({ environmentId, attachments: [fileBackedImage] }), + ); + yield* Effect.promise(() => readStarted.promise); + yield* Fiber.interrupt(preparing); + if (outcome === "resolve") read.resolve("YWJj"); + else read.reject(new Error("Native read failed after cancellation")); + + expect(Exit.hasInterrupts(yield* Fiber.await(preparing))).toBe(true); + }), + ); + + // regression: #9713 read file-backed image drafts before enabling them + it.effect("uploads a file-backed image from its owned copy without staging base64", () => + Effect.gen(function* () { + mocks.upload.mockImplementation( + async ( + _uri: string, + _url: string, + options: { onProgress?: (event: { bytesSent: unknown; totalBytes: unknown }) => void }, + ) => { + // expo-modules-jsi delivers native Int64 progress counts as BigInt. + options.onProgress?.({ bytesSent: BigInt(1), totalBytes: BigInt(4) }); + return { status: 204, body: "", headers: {} }; + }, + ); + const progress: Array = []; + const prepared = yield* prepare({ environmentId, attachments: [fileBackedImage], - signal: controller.signal, + supportsImageUploads: true, + onUploadProgress: (_, value) => progress.push(value), }); - await readStarted.promise; - controller.abort(); - if (outcome === "resolve") read.resolve("YWJj"); - else read.reject(new Error("Native read failed after cancellation")); - await expect(preparing).resolves.toEqual({ status: "abandoned" }); - }, + expect(progress).toEqual([0.25]); + + expect(mocks.upload).toHaveBeenCalledWith( + fileBackedImage.fileUri, + "https://environment.example/api/attachments/upload/signed", + expect.objectContaining({ headers: { "Content-Type": "image/png" } }), + ); + expect(mocks.readBase64).not.toHaveBeenCalled(); + expect(mocks.writeFile).not.toHaveBeenCalled(); + expect(mocks.deleteFile).not.toHaveBeenCalled(); + expect(prepared.status).toBe("ready"); + if (prepared.status !== "ready") return; + expect(prepared.attachments).toEqual([ + { + type: "image", + id: MINTED_ID, + name: "photo.png", + mimeType: "image/png", + sizeBytes: 3, + }, + ]); + }), ); - // regression: #9713 read file-backed image drafts before enabling them - it("uploads a file-backed image from its owned copy without staging base64", async () => { - mocks.upload.mockImplementation( - async ( - _uri: string, - _url: string, - options: { onProgress?: (event: { bytesSent: unknown; totalBytes: unknown }) => void }, - ) => { - // expo-modules-jsi delivers native Int64 progress counts as BigInt. - options.onProgress?.({ bytesSent: BigInt(1), totalBytes: BigInt(4) }); - return { status: 204, body: "", headers: {} }; - }, - ); - const progress: Array = []; - const prepared = await prepareTurnAttachments({ - environmentId, - attachments: [fileBackedImage], - supportsImageUploads: true, - onUploadProgress: (_, value) => progress.push(value), - }); - - expect(progress).toEqual([0.25]); + it.effect("sends a document-picked picture with the mime it was uploaded under", () => + Effect.gen(function* () { + const prepared = yield* prepare({ + environmentId, + attachments: [documentPickedImage], + }); - expect(mocks.upload).toHaveBeenCalledWith( - fileBackedImage.fileUri, - "https://environment.example/api/attachments/upload/signed", - expect.objectContaining({ headers: { "Content-Type": "image/png" } }), - ); - expect(mocks.readBase64).not.toHaveBeenCalled(); - expect(mocks.writeFile).not.toHaveBeenCalled(); - expect(mocks.deleteFile).not.toHaveBeenCalled(); - expect(prepared.status).toBe("ready"); - if (prepared.status !== "ready") return; - expect(prepared.attachments).toEqual([ - { + expect(mocks.upload).toHaveBeenCalledWith( + "file:///documents/photo.png", + "https://environment.example/api/attachments/upload/signed", + expect.objectContaining({ headers: { "Content-Type": "image/png" } }), + ); + expect(prepared.status).toBe("ready"); + if (prepared.status !== "ready") return; + expect(prepared.attachments[0]).toEqual({ type: "image", id: MINTED_ID, name: "photo.png", mimeType: "image/png", sizeBytes: 3, - }, - ]); - }); - - it("sends a document-picked picture with the mime it was uploaded under", async () => { - const prepared = await prepareTurnAttachments({ - environmentId, - attachments: [documentPickedImage], - }); + }); + }), + ); - expect(mocks.upload).toHaveBeenCalledWith( - "file:///documents/photo.png", - "https://environment.example/api/attachments/upload/signed", - expect.objectContaining({ headers: { "Content-Type": "image/png" } }), - ); - expect(prepared.status).toBe("ready"); - if (prepared.status !== "ready") return; - expect(prepared.attachments[0]).toEqual({ - type: "image", - id: MINTED_ID, - name: "photo.png", - mimeType: "image/png", - sizeBytes: 3, - }); - }); + it.effect("uploads an oversized document-picked image as a file", () => + Effect.gen(function* () { + const largeImage = { + ...documentPickedImage, + mimeType: "image/png", + sizeBytes: PROVIDER_SEND_TURN_MAX_IMAGE_BYTES + 1, + }; + const prepared = yield* prepare({ environmentId, attachments: [largeImage] }); - it("uploads an oversized document-picked image as a file", async () => { - const largeImage = { - ...documentPickedImage, - mimeType: "image/png", - sizeBytes: PROVIDER_SEND_TURN_MAX_IMAGE_BYTES + 1, - }; - const prepared = await prepareTurnAttachments({ environmentId, attachments: [largeImage] }); - - expect(mocks.runAtomCommand).toHaveBeenCalledWith( - expect.anything(), - mocks.createUploadUrl, - expect.objectContaining({ - input: { + expect(mocks.runAtomCommand).toHaveBeenCalledWith( + expect.anything(), + mocks.createUploadUrl, + expect.objectContaining({ + input: { + type: "file", + name: largeImage.name, + mimeType: "image/png", + sizeBytes: largeImage.sizeBytes, + }, + }), + expect.anything(), + ); + expect(prepared.status).toBe("ready"); + if (prepared.status !== "ready") return; + expect(prepared.attachments).toEqual([ + { type: "file", + id: MINTED_ID, name: largeImage.name, mimeType: "image/png", sizeBytes: largeImage.sizeBytes, }, - }), - expect.anything(), - ); - expect(prepared.status).toBe("ready"); - if (prepared.status !== "ready") return; - expect(prepared.attachments).toEqual([ - { - type: "file", - id: MINTED_ID, - name: largeImage.name, - mimeType: "image/png", - sizeBytes: largeImage.sizeBytes, - }, - ]); - expect(mocks.readBase64).not.toHaveBeenCalled(); - }); + ]); + expect(mocks.readBase64).not.toHaveBeenCalled(); + }), + ); - it("uploads generic file bytes directly and keeps mixed attachment order", async () => { - const pastedFile = { - ...file, - source: { _tag: "pasted-text" as const }, - }; - const prepared = await prepareTurnAttachments({ - environmentId, - attachments: [pastedFile, image], - }); + it.effect("uploads generic file bytes directly and keeps mixed attachment order", () => + Effect.gen(function* () { + const pastedFile = { + ...file, + source: { _tag: "pasted-text" as const }, + }; + const prepared = yield* prepare({ + environmentId, + attachments: [pastedFile, image], + }); - expect(mocks.upload).toHaveBeenCalledWith( - "file:///documents/report.pdf", - "https://environment.example/api/attachments/upload/signed", - expect.objectContaining({ - httpMethod: "POST", - uploadType: 0, - headers: { "Content-Type": "application/pdf" }, - }), - ); - expect(prepared.status).toBe("ready"); - if (prepared.status !== "ready") return; - expect(prepared.attachments[0]).toEqual({ - type: "file", - id: MINTED_ID, - name: "report.pdf", - mimeType: "application/pdf", - sizeBytes: 42, - source: { _tag: "pasted-text" }, - }); - expect(prepared.attachments[1]?.type).toBe("image"); - expect(prepared.pendingAttachmentIds).toEqual([MINTED_ID]); - expect(prepared.draftAttachments[0]).toEqual({ - ...pastedFile, - uploadedAttachmentId: MINTED_ID, - uploadEnvironmentId: environmentId, - }); - }); + expect(mocks.upload).toHaveBeenCalledWith( + "file:///documents/report.pdf", + "https://environment.example/api/attachments/upload/signed", + expect.objectContaining({ + httpMethod: "POST", + uploadType: 0, + headers: { "Content-Type": "application/pdf" }, + }), + ); + expect(prepared.status).toBe("ready"); + if (prepared.status !== "ready") return; + expect(prepared.attachments[0]).toEqual({ + type: "file", + id: MINTED_ID, + name: "report.pdf", + mimeType: "application/pdf", + sizeBytes: 42, + source: { _tag: "pasted-text" }, + }); + expect(prepared.attachments[1]?.type).toBe("image"); + expect(prepared.pendingAttachmentIds).toEqual([MINTED_ID]); + expect(prepared.draftAttachments[0]).toEqual({ + ...pastedFile, + uploadedAttachmentId: MINTED_ID, + uploadEnvironmentId: environmentId, + }); + }), + ); - it("uses the current connection when an environment reconnects during URL creation", async () => { - mocks.readAtom - .mockReturnValueOnce(Option.some({ httpBaseUrl: "https://old-environment.example/" })) - .mockReturnValueOnce(Option.some({ httpBaseUrl: "https://new-environment.example/" })); + it.effect("uses the current connection when an environment reconnects during URL creation", () => + Effect.gen(function* () { + mocks.readAtom + .mockReturnValueOnce(Option.some({ httpBaseUrl: "https://old-environment.example/" })) + .mockReturnValueOnce(Option.some({ httpBaseUrl: "https://new-environment.example/" })); - await prepareTurnAttachments({ environmentId, attachments: [file] }); + yield* prepare({ environmentId, attachments: [file] }); - expect(mocks.upload).toHaveBeenCalledWith( - file.fileUri, - "https://new-environment.example/api/attachments/upload/signed", - expect.anything(), - ); - }); + expect(mocks.upload).toHaveBeenCalledWith( + file.fileUri, + "https://new-environment.example/api/attachments/upload/signed", + expect.anything(), + ); + }), + ); - it("uploads a restored draft file from the current iOS document container", async () => { - const fileName = "33333333-3333-4333-8333-333333333333-report%20%23.pdf"; - const restoredFile = { - ...file, - fileUri: `file:///private/var/mobile/Containers/Data/Application/11111111-1111-4111-8111-111111111111/Documents/t3-composer-attachments/${fileName}`, - }; - mocks.documentUri = - "file:///var/mobile/Containers/Data/Application/22222222-2222-4222-8222-222222222222/Documents"; - const currentUri = `${mocks.documentUri}/t3-composer-attachments/${fileName}`; - mocks.upload.mockImplementation(async (uri: string) => { - if (uri !== currentUri) { - throw new Error("File does not exist in the previous application container."); - } - return { status: 204, body: "", headers: {} }; - }); + it.effect("uploads a restored draft file from the current iOS document container", () => + Effect.gen(function* () { + const fileName = "33333333-3333-4333-8333-333333333333-report%20%23.pdf"; + const restoredFile = { + ...file, + fileUri: `file:///private/var/mobile/Containers/Data/Application/11111111-1111-4111-8111-111111111111/Documents/t3-composer-attachments/${fileName}`, + }; + mocks.documentUri = + "file:///var/mobile/Containers/Data/Application/22222222-2222-4222-8222-222222222222/Documents"; + const currentUri = `${mocks.documentUri}/t3-composer-attachments/${fileName}`; + mocks.upload.mockImplementation(async (uri: string) => { + if (uri !== currentUri) { + throw new Error("File does not exist in the previous application container."); + } + return { status: 204, body: "", headers: {} }; + }); - const prepared = await prepareTurnAttachments({ - environmentId, - attachments: [restoredFile], - }); + const prepared = yield* prepare({ + environmentId, + attachments: [restoredFile], + }); - expect(prepared.status).toBe("ready"); - expect(mocks.upload).toHaveBeenCalledWith( - currentUri, - "https://environment.example/api/attachments/upload/signed", - expect.anything(), - ); - }); + expect(prepared.status).toBe("ready"); + expect(mocks.upload).toHaveBeenCalledWith( + currentUri, + "https://environment.example/api/attachments/upload/signed", + expect.anything(), + ); + }), + ); it("adds uploaded file references to durable drafts without changing images", () => { expect( @@ -533,215 +602,219 @@ describe("prepareTurnAttachments", () => { ]); }); - it("reuses a pending file upload from a previous outbox attempt", async () => { - const previouslyUploaded = { - ...file, - uploadedAttachmentId: "pending-existing-pdf", - uploadEnvironmentId: environmentId, - }; - - const prepared = await prepareTurnAttachments({ - environmentId, - attachments: [previouslyUploaded, image], - }); + it.effect("reuses a pending file upload from a previous outbox attempt", () => + Effect.gen(function* () { + const previouslyUploaded = { + ...file, + uploadedAttachmentId: "pending-existing-pdf", + uploadEnvironmentId: environmentId, + }; - expect(prepared.status).toBe("ready"); - if (prepared.status !== "ready") return; - expect(prepared.attachments).toEqual([ - { - type: "file", - id: "pending-existing-pdf", - name: "report.pdf", - mimeType: "application/pdf", - sizeBytes: 42, - }, - { - type: "image", - name: "screenshot.png", - mimeType: "image/png", - sizeBytes: 3, - dataUrl: "data:image/png;base64,YWJj", - }, - ]); - expect(prepared.pendingAttachmentIds).toEqual(["pending-existing-pdf"]); - expect(mocks.upload).not.toHaveBeenCalled(); - expect(mocks.runAtomCommand).not.toHaveBeenCalled(); - }); + const prepared = yield* prepare({ + environmentId, + attachments: [previouslyUploaded, image], + }); - it("uploads a file again when its saved pending upload has expired", async () => { - mocks.executeAtomQuery.mockResolvedValueOnce({ - _tag: "Failure", - error: { _tag: "AssetAttachmentNotFoundError" }, - }); - const previouslyUploaded = { - ...file, - uploadedAttachmentId: "pending-expired-pdf", - uploadEnvironmentId: environmentId, - }; - - const prepared = await prepareTurnAttachments({ - environmentId, - attachments: [previouslyUploaded], - }); + expect(prepared.status).toBe("ready"); + if (prepared.status !== "ready") return; + expect(prepared.attachments).toEqual([ + { + type: "file", + id: "pending-existing-pdf", + name: "report.pdf", + mimeType: "application/pdf", + sizeBytes: 42, + }, + { + type: "image", + name: "screenshot.png", + mimeType: "image/png", + sizeBytes: 3, + dataUrl: "data:image/png;base64,YWJj", + }, + ]); + expect(prepared.pendingAttachmentIds).toEqual(["pending-existing-pdf"]); + expect(mocks.upload).not.toHaveBeenCalled(); + expect(mocks.runAtomCommand).not.toHaveBeenCalled(); + }), + ); - expect(mocks.upload).toHaveBeenCalledOnce(); - expect(prepared.status).toBe("ready"); - if (prepared.status !== "ready") return; - expect(prepared.pendingAttachmentIds).toEqual([MINTED_ID]); - }); + it.effect("uploads a file again when its saved pending upload has expired", () => + Effect.gen(function* () { + mocks.executeAtomQuery.mockResolvedValueOnce({ + _tag: "Failure", + error: { _tag: "AssetAttachmentNotFoundError" }, + }); + const previouslyUploaded = { + ...file, + uploadedAttachmentId: "pending-expired-pdf", + uploadEnvironmentId: environmentId, + }; - it("uploads image bytes over HTTP while retaining the durable offline image", async () => { - const persisted = vi.fn(async () => "persisted" as const); - const prepared = await prepareTurnAttachments({ - environmentId, - attachments: [image], - supportsImageUploads: true, - persistUploadedReferences: persisted, - }); - expect(mocks.writeFile).toHaveBeenCalledWith("file:///cache/t3-upload-uuid", "YWJj", { - encoding: "base64", - }); - expect(mocks.upload).toHaveBeenCalledWith( - "file:///cache/t3-upload-uuid", - "https://environment.example/api/attachments/upload/signed", - expect.objectContaining({ headers: { "Content-Type": "image/png" } }), - ); - expect(mocks.deleteFile).toHaveBeenCalledExactlyOnceWith("file:///cache/t3-upload-uuid"); - expect(prepared.status).toBe("ready"); - if (prepared.status !== "ready") return; - expect(prepared.attachments).toEqual([ - { - type: "image", - id: MINTED_ID, - name: image.name, - mimeType: image.mimeType, - sizeBytes: image.sizeBytes, - }, - ]); - expect(prepared.draftAttachments).toEqual([ - { ...image, uploadedAttachmentId: MINTED_ID, uploadEnvironmentId: environmentId }, - ]); - expect(persisted).toHaveBeenCalledWith(prepared.draftAttachments); - }); + const prepared = yield* prepare({ + environmentId, + attachments: [previouslyUploaded], + }); - it("reuses an uploaded image and reuploads its local bytes after server expiry", async () => { - const saved = { - ...image, - uploadedAttachmentId: "saved-image", - uploadEnvironmentId: environmentId, - }; - const reused = await prepareTurnAttachments({ - environmentId, - attachments: [saved], - supportsImageUploads: true, - }); - expect(reused.status === "ready" && reused.attachments[0]).toEqual({ - type: "image", - id: "saved-image", - name: image.name, - mimeType: image.mimeType, - sizeBytes: image.sizeBytes, - }); - expect(mocks.upload).not.toHaveBeenCalled(); - mocks.executeAtomQuery.mockResolvedValueOnce({ - _tag: "Failure", - error: { _tag: "AssetAttachmentNotFoundError" }, - }); - const restored = await prepareTurnAttachments({ - environmentId, - attachments: [saved], - supportsImageUploads: true, - }); - expect(restored.status === "ready" && restored.draftAttachments[0]).toEqual({ - ...saved, - uploadedAttachmentId: MINTED_ID, - }); - expect(mocks.writeFile).toHaveBeenCalledWith("file:///cache/t3-upload-uuid", "YWJj", { - encoding: "base64", - }); - }); + expect(mocks.upload).toHaveBeenCalledOnce(); + expect(prepared.status).toBe("ready"); + if (prepared.status !== "ready") return; + expect(prepared.pendingAttachmentIds).toEqual([MINTED_ID]); + }), + ); - it("does not reuse an image upload from another environment", async () => { - const prepared = await prepareTurnAttachments({ - environmentId, - attachments: [ + it.effect("uploads image bytes over HTTP while retaining the durable offline image", () => + Effect.gen(function* () { + const persisted = vi.fn(() => Effect.succeed("persisted" as const)); + const prepared = yield* prepare({ + environmentId, + attachments: [image], + supportsImageUploads: true, + persistUploadedReferences: persisted, + }); + expect(mocks.writeFile).toHaveBeenCalledWith("file:///cache/t3-upload-uuid", "YWJj", { + encoding: "base64", + }); + expect(mocks.upload).toHaveBeenCalledWith( + "file:///cache/t3-upload-uuid", + "https://environment.example/api/attachments/upload/signed", + expect.objectContaining({ headers: { "Content-Type": "image/png" } }), + ); + expect(mocks.deleteFile).toHaveBeenCalledExactlyOnceWith("file:///cache/t3-upload-uuid"); + expect(prepared.status).toBe("ready"); + if (prepared.status !== "ready") return; + expect(prepared.attachments).toEqual([ { - ...image, - uploadedAttachmentId: "other-image", - uploadEnvironmentId: EnvironmentId.make("other"), + type: "image", + id: MINTED_ID, + name: image.name, + mimeType: image.mimeType, + sizeBytes: image.sizeBytes, }, - ], - supportsImageUploads: true, - }); - expect(mocks.executeAtomQuery).not.toHaveBeenCalled(); - expect(mocks.upload).toHaveBeenCalledOnce(); - expect(prepared.status === "ready" && prepared.draftAttachments[0]?.uploadEnvironmentId).toBe( - environmentId, - ); - }); + ]); + expect(prepared.draftAttachments).toEqual([ + { ...image, uploadedAttachmentId: MINTED_ID, uploadEnvironmentId: environmentId }, + ]); + expect(persisted).toHaveBeenCalledWith(prepared.draftAttachments); + }), + ); - it("aborts an active transfer without dropping local bytes or stamping a partial upload", async () => { - const started = Promise.withResolvers(); - const controller = new AbortController(); - const persist = vi.fn(async () => "persisted" as const); - mocks.upload.mockImplementation( - (_uri: string, _url: string, options: { signal: AbortSignal }) => - new Promise((_, reject) => { - options.signal.addEventListener("abort", () => reject(new Error("cancelled")), { - once: true, - }); - started.resolve(); - }), - ); - const preparing = prepareTurnAttachments({ - environmentId, - attachments: [file], - signal: controller.signal, - persistUploadedReferences: persist, - }); - await started.promise; - controller.abort(); - expect(await preparing).toEqual({ status: "abandoned" }); - expect(persist).not.toHaveBeenCalled(); - expect(mocks.deleteFile).not.toHaveBeenCalled(); - expect(removeCallsFor(MINTED_ID)).toBe(1); - }); + it.effect("reuses an uploaded image and reuploads its local bytes after server expiry", () => + Effect.gen(function* () { + const saved = { + ...image, + uploadedAttachmentId: "saved-image", + uploadEnvironmentId: environmentId, + }; + const reused = yield* prepare({ + environmentId, + attachments: [saved], + supportsImageUploads: true, + }); + expect(reused.status === "ready" && reused.attachments[0]).toEqual({ + type: "image", + id: "saved-image", + name: image.name, + mimeType: image.mimeType, + sizeBytes: image.sizeBytes, + }); + expect(mocks.upload).not.toHaveBeenCalled(); + mocks.executeAtomQuery.mockResolvedValueOnce({ + _tag: "Failure", + error: { _tag: "AssetAttachmentNotFoundError" }, + }); + const restored = yield* prepare({ + environmentId, + attachments: [saved], + supportsImageUploads: true, + }); + expect(restored.status === "ready" && restored.draftAttachments[0]).toEqual({ + ...saved, + uploadedAttachmentId: MINTED_ID, + }); + expect(mocks.writeFile).toHaveBeenCalledWith("file:///cache/t3-upload-uuid", "YWJj", { + encoding: "base64", + }); + }), + ); - it("removes pending uploads when the native HTTP request fails", async () => { - mocks.upload.mockResolvedValue({ status: 500, body: "failed", headers: {} }); + it.effect("does not reuse an image upload from another environment", () => + Effect.gen(function* () { + const prepared = yield* prepare({ + environmentId, + attachments: [ + { + ...image, + uploadedAttachmentId: "other-image", + uploadEnvironmentId: EnvironmentId.make("other"), + }, + ], + supportsImageUploads: true, + }); + expect(mocks.executeAtomQuery).not.toHaveBeenCalled(); + expect(mocks.upload).toHaveBeenCalledOnce(); + expect(prepared.status === "ready" && prepared.draftAttachments[0]?.uploadEnvironmentId).toBe( + environmentId, + ); + }), + ); - await expect(prepareTurnAttachments({ environmentId, attachments: [file] })).rejects.toThrow( - "Upload failed for 'report.pdf' (500).", - ); - expect(removeCallsFor(MINTED_ID)).toBe(1); - }); + it.effect( + "aborts an active transfer without dropping local bytes or stamping a partial upload", + () => + Effect.gen(function* () { + const started = Promise.withResolvers(); + const persist = vi.fn(() => Effect.succeed("persisted" as const)); + mocks.upload.mockImplementation( + (_uri: string, _url: string, options: { signal: AbortSignal }) => + new Promise((_, reject) => { + options.signal.addEventListener("abort", () => reject(new Error("cancelled")), { + once: true, + }); + started.resolve(); + }), + ); + const preparing = yield* Effect.forkChild( + prepare({ environmentId, attachments: [file], persistUploadedReferences: persist }), + ); + yield* Effect.promise(() => started.promise); + yield* Fiber.interrupt(preparing); + expect(Exit.hasInterrupts(yield* Fiber.await(preparing))).toBe(true); + expect(persist).not.toHaveBeenCalled(); + expect(mocks.deleteFile).not.toHaveBeenCalled(); + expect(removeCallsFor(MINTED_ID)).toBe(1); + }), + ); - it("keeps a previously persisted upload when a later attachment fails", async () => { - const previouslyUploaded = { - ...file, - id: "file-existing", - uploadedAttachmentId: "pending-existing-pdf", - uploadEnvironmentId: environmentId, - }; - mocks.upload.mockResolvedValue({ status: 500, body: "failed", headers: {} }); + it.effect("removes pending uploads when the native HTTP request fails", () => + Effect.gen(function* () { + mocks.upload.mockResolvedValue({ status: 500, body: "failed", headers: {} }); - await expect( - prepareTurnAttachments({ environmentId, attachments: [previouslyUploaded, file] }), - ).rejects.toThrow("Upload failed for 'report.pdf' (500)."); + expect( + (yield* Effect.flip(prepare({ environmentId, attachments: [file] }))).message, + ).toContain("Upload failed for 'report.pdf' (500)."); + expect(removeCallsFor(MINTED_ID)).toBe(1); + }), + ); - expect(removeCallsFor("pending-existing-pdf")).toBe(0); - }); + it.effect("keeps a previously persisted upload when a later attachment fails", () => + Effect.gen(function* () { + const previouslyUploaded = { + ...file, + id: "file-existing", + uploadedAttachmentId: "pending-existing-pdf", + uploadEnvironmentId: environmentId, + }; + mocks.upload.mockResolvedValue({ status: 500, body: "failed", headers: {} }); - it("deletes the minted uploads when the owner abandons the send", async () => { - const result = await prepareTurnAttachments({ - environmentId, - attachments: [file], - persistUploadedReferences: async () => "abandon", - }); + expect( + (yield* Effect.flip(prepare({ environmentId, attachments: [previouslyUploaded, file] }))) + .message, + ).toContain("Upload failed for 'report.pdf' (500)."); - expect(result.status).toBe("abandoned"); - expect(removeCallsFor(MINTED_ID)).toBe(1); - }); + expect(removeCallsFor("pending-existing-pdf")).toBe(0); + }), + ); }); describe("releasePendingAttachmentUploads", () => { diff --git a/apps/mobile/src/lib/attachmentUpload.ts b/apps/mobile/src/lib/attachmentUpload.ts index 57699af22819e..b4bf8b0aeb472 100644 --- a/apps/mobile/src/lib/attachmentUpload.ts +++ b/apps/mobile/src/lib/attachmentUpload.ts @@ -15,6 +15,7 @@ import { AuthOrchestrationOperateScope, PROVIDER_SEND_TURN_SUPPORTED_IMAGE_MIME_TYPES, } from "@t3tools/contracts"; +import * as Effect from "effect/Effect"; import * as Option from "effect/Option"; import { appAtomRegistry } from "../state/atom-registry"; @@ -31,6 +32,7 @@ import { } from "./composerImages"; import { imageMimeType } from "@t3tools/shared/image"; import { uuidv4 } from "./uuid"; +import { type ComposerAttachmentUploadError, uploadError } from "./composerAttachmentUploadQueue"; export type UploadedMobileAttachment = | UploadChatImageAttachment @@ -248,159 +250,150 @@ async function uploadFileBytes( } } -/** persistUploadedReferences runs only for new uploaded ids; save them to their owner so crash retries reuse bytes. */ -export async function prepareTurnAttachments(input: { +function requireUploadAccess(environmentId: EnvironmentId): void { + if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) { + throw new Error("This connection cannot upload attachments."); + } +} + +/** + * Uploads one attachment and resolves to its uploaded id, reusing an earlier upload the server + * still has. An abort deletes the pending upload it created. + */ +export async function uploadComposerAttachment( + environmentId: EnvironmentId, + attachment: DraftComposerAttachment, + signal: AbortSignal, + onProgress?: (progress: number) => void, +): Promise { + requireUploadAccess(environmentId); + if ( + attachment.uploadEnvironmentId === environmentId && + attachment.uploadedAttachmentId !== undefined + ) { + const verification = await verifyPersistedAttachmentUpload({ + registry: appAtomRegistry, + createAssetUrl: assetEnvironment.createUrl, + environmentId, + attachmentId: attachment.uploadedAttachmentId, + }); + if (verification.status === "failed") throw verification.error; + if (verification.status === "verified") return attachment.uploadedAttachmentId; + } + if (signal.aborted) throw new Error("Upload cancelled."); + requireUploadAccess(environmentId); + if ( + Option.isNone( + appAtomRegistry.get(environmentSession.preparedConnectionValueAtom(environmentId)), + ) + ) { + throw new Error("The environment is not connected."); + } + let minted: string | null = null; + const result = await runAttachmentUploadCycle({ + registry: appAtomRegistry, + createUploadUrl: attachmentEnvironment.createUploadUrl, + remove: attachmentEnvironment.remove, + environmentId, + upload: attachmentUploadInput(attachment), + // Resolve at transfer time; reconnects may change the base URL. + resolveUploadUrl: (relativeUrl) => { + requireUploadAccess(environmentId); + const connection = appAtomRegistry.get( + environmentSession.preparedConnectionValueAtom(environmentId), + ); + return Option.isNone(connection) + ? null + : resolveAssetUrl(connection.value.httpBaseUrl, relativeUrl); + }, + transport: (url) => ({ + done: uploadFileBytes(environmentId, attachment, url, signal, onProgress), + abort: () => {}, + }), + onMinted: (attachmentId) => { + if (signal.aborted) return "cancel"; + minted = attachmentId; + return "continue"; + }, + }); + if (result.status === "uploaded") return result.attachmentId; + if (minted !== null) await releaseCreatedUploadsQuietly(environmentId, [minted]); + throw result.status === "failed" && result.error !== undefined + ? result.error + : new Error(`Upload failed for '${attachment.name}'.`); +} + +/** + * Turns a message's attachments into wire references. `uploadAttachment` is the shared upload + * registry, so a send joins an upload the composer already started; interrupting this stops + * waiting without cancelling that shared upload. persistUploadedReferences runs only for new + * uploaded ids; save them to their owner so crash retries reuse bytes. + */ +export const prepareTurnAttachments = Effect.fn("prepareTurnAttachments")(function* (input: { readonly environmentId: EnvironmentId; readonly attachments: ReadonlyArray; readonly supportsImageUploads?: boolean; - readonly signal?: AbortSignal; - readonly onUploadProgress?: (attachmentId: string, progress: number) => void; + readonly uploadAttachment: ( + attachment: DraftComposerAttachment, + ) => Effect.Effect; readonly persistUploadedReferences?: ( draftAttachments: ReadonlyArray, - ) => Promise<"persisted" | "abandon">; -}): Promise { + ) => Effect.Effect<"persisted" | "abandon">; +}) { const { environmentId } = input; - if (input.signal?.aborted) return { status: "abandoned" }; + const inlineImages = (attachments: ReadonlyArray) => + Effect.tryPromise({ + try: () => + toUploadChatImageAttachments( + attachments.filter( + (attachment): attachment is DraftComposerImageAttachment => attachment.type === "image", + ), + ), + catch: uploadError, + }); const files = input.attachments.filter((attachment) => attachment.type === "file"); - const ready = ( - attachments: ReadonlyArray, - pendingAttachmentIds: ReadonlyArray, - draftAttachments: ReadonlyArray, - ): PreparedTurnAttachments => ({ - status: "ready", - attachments, - draftAttachments, - pendingAttachmentIds, - }); - if (input.attachments.length === 0 || (files.length === 0 && !input.supportsImageUploads)) { - try { - const imageAttachments = await toUploadChatImageAttachments( - input.attachments.filter((attachment) => attachment.type === "image"), - ); - if (input.signal?.aborted) return { status: "abandoned" }; - return ready(imageAttachments, [], input.attachments); - } catch (error) { - if (input.signal?.aborted) return { status: "abandoned" }; - throw error; - } - } - - const requireUploadAccess = () => { - if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) { - throw new Error("This connection cannot upload attachments."); - } - }; - requireUploadAccess(); - - const connection = appAtomRegistry.get( - environmentSession.preparedConnectionValueAtom(environmentId), - ); - if (Option.isNone(connection)) { - throw new Error("The environment is not connected."); + const imageAttachments = yield* inlineImages(input.attachments); + return { + status: "ready", + attachments: imageAttachments, + draftAttachments: input.attachments, + pendingAttachmentIds: [], + } satisfies PreparedTurnAttachments as PrepareTurnAttachmentsResult; } + yield* Effect.try({ try: () => requireUploadAccess(environmentId), catch: uploadError }); const uploadedAttachments: UploadedMobileAttachment[] = []; const pendingAttachmentIds: string[] = []; - const createdAttachmentIds: string[] = []; - const controller = new AbortController(); - const abort = () => controller.abort(); - input.signal?.addEventListener("abort", abort, { once: true }); - try { - for (const attachment of input.attachments) { - if (controller.signal.aborted) throw new Error("Upload cancelled."); - requireUploadAccess(); - if (attachment.type === "image" && !input.supportsImageUploads) { - uploadedAttachments.push(...(await toUploadChatImageAttachments([attachment]))); - continue; - } - - if ( - attachment.uploadEnvironmentId === environmentId && - attachment.uploadedAttachmentId !== undefined - ) { - const verification = await verifyPersistedAttachmentUpload({ - registry: appAtomRegistry, - createAssetUrl: assetEnvironment.createUrl, - environmentId, - attachmentId: attachment.uploadedAttachmentId, - }); - if (verification.status === "failed") { - throw verification.error; - } - if (verification.status === "verified") { - pendingAttachmentIds.push(attachment.uploadedAttachmentId); - uploadedAttachments.push(uploadedReference(attachment, attachment.uploadedAttachmentId)); - continue; - } - } - - requireUploadAccess(); - const result = await runAttachmentUploadCycle({ - registry: appAtomRegistry, - createUploadUrl: attachmentEnvironment.createUploadUrl, - remove: attachmentEnvironment.remove, - environmentId, - upload: attachmentUploadInput(attachment), - // Resolve at transfer time; reconnects may change the base URL. - resolveUploadUrl: (relativeUrl) => { - requireUploadAccess(); - const currentConnection = appAtomRegistry.get( - environmentSession.preparedConnectionValueAtom(environmentId), - ); - return Option.isNone(currentConnection) - ? null - : resolveAssetUrl(currentConnection.value.httpBaseUrl, relativeUrl); - }, - transport: (url) => ({ - done: uploadFileBytes( - environmentId, - attachment, - url, - controller.signal, - input.onUploadProgress - ? (progress) => input.onUploadProgress?.(attachment.id, progress) - : undefined, - ), - abort, - }), - onMinted: (attachmentId) => { - if (controller.signal.aborted) return "cancel"; - pendingAttachmentIds.push(attachmentId); - createdAttachmentIds.push(attachmentId); - return "continue"; - }, - }); - if (result.status !== "uploaded") { - throw result.status === "failed" && result.error !== undefined - ? result.error - : new Error(`Upload failed for '${attachment.name}'.`); - } - uploadedAttachments.push(uploadedReference(attachment, result.attachmentId)); + for (const attachment of input.attachments) { + if (attachment.type === "image" && !input.supportsImageUploads) { + uploadedAttachments.push(...(yield* inlineImages([attachment]))); + continue; } + const uploadedId = yield* input.uploadAttachment(attachment); + pendingAttachmentIds.push(uploadedId); + uploadedAttachments.push(uploadedReference(attachment, uploadedId)); + } - if (controller.signal.aborted) throw new Error("Upload cancelled."); - - const draftAttachments = withUploadedMobileAttachmentReferences({ - environmentId, - attachments: input.attachments, - uploadedAttachments, - }); - const referencesChanged = draftAttachments.some( - (attachment, index) => attachment !== input.attachments[index], - ); - if (referencesChanged && input.persistUploadedReferences) { - if ((await input.persistUploadedReferences(draftAttachments)) === "abandon") { - await releaseCreatedUploadsQuietly(environmentId, createdAttachmentIds); - return { status: "abandoned" }; - } - } - return ready(uploadedAttachments, pendingAttachmentIds, draftAttachments); - } catch (error) { - await releaseCreatedUploadsQuietly(environmentId, createdAttachmentIds); - if (controller.signal.aborted) return { status: "abandoned" }; - throw error; - } finally { - input.signal?.removeEventListener("abort", abort); + const draftAttachments = withUploadedMobileAttachmentReferences({ + environmentId, + attachments: input.attachments, + uploadedAttachments, + }); + const referencesChanged = draftAttachments.some( + (attachment, index) => attachment !== input.attachments[index], + ); + if ( + referencesChanged && + input.persistUploadedReferences && + (yield* input.persistUploadedReferences(draftAttachments)) === "abandon" + ) { + return { status: "abandoned" } as PrepareTurnAttachmentsResult; } -} + return { + status: "ready", + attachments: uploadedAttachments, + draftAttachments, + pendingAttachmentIds, + } satisfies PreparedTurnAttachments as PrepareTurnAttachmentsResult; +}); diff --git a/apps/mobile/src/lib/composerAttachmentUploadQueue.test.ts b/apps/mobile/src/lib/composerAttachmentUploadQueue.test.ts index 6de39c413d49e..9a65e348a28d0 100644 --- a/apps/mobile/src/lib/composerAttachmentUploadQueue.test.ts +++ b/apps/mobile/src/lib/composerAttachmentUploadQueue.test.ts @@ -1,5 +1,8 @@ import { ApprovalRequestId, EnvironmentId, ThreadId } from "@t3tools/contracts"; -import { describe, expect, it, vi } from "vite-plus/test"; +import * as Effect from "effect/Effect"; +import * as Fiber from "effect/Fiber"; +import { describe, expect, it } from "@effect/vitest"; +import { vi } from "vite-plus/test"; import { questionAttachmentDraftKey, questionAttachmentDraftPrefix, @@ -33,7 +36,7 @@ function request(id: string, environment = environmentId): ComposerAttachmentUpl describe("composer attachment upload queue", () => { it("bounds concurrency, deduplicates updates, and drains all attachments", async () => { - const gates = new Map>>(); + const gates = new Map>>(); const fourthStarted = Promise.withResolvers(); const firstThreeStarted = Promise.withResolvers(); let active = 0; @@ -41,7 +44,7 @@ describe("composer attachment upload queue", () => { const upload = vi.fn(async (input: ComposerAttachmentUploadRequest) => { active += 1; maximum = Math.max(maximum, active); - const gate = Promise.withResolvers(); + const gate = Promise.withResolvers(); gates.set(input.attachment.id, gate); if (gates.size === 3) firstThreeStarted.resolve(); if (gates.size === 4) fourthStarted.resolve(); @@ -57,9 +60,9 @@ describe("composer attachment upload queue", () => { queue.sync(requests); await firstThreeStarted.promise; expect(upload).toHaveBeenCalledTimes(3); - gates.get("one")!.resolve(true); + gates.get("one")!.resolve("uploaded"); await fourthStarted.promise; - for (const gate of gates.values()) gate.resolve(true); + for (const gate of gates.values()) gate.resolve("uploaded"); await queue.settled(); queue.sync(requests); await queue.settled(); @@ -76,8 +79,8 @@ describe("composer attachment upload queue", () => { async (_request: ComposerAttachmentUploadRequest, currentSignal: AbortSignal) => { signal = currentSignal; started.resolve(); - return new Promise((resolve) => - currentSignal.addEventListener("abort", () => resolve(false), { once: true }), + return new Promise((resolve) => + currentSignal.addEventListener("abort", () => resolve("aborted"), { once: true }), ); }, ); @@ -94,7 +97,7 @@ describe("composer attachment upload queue", () => { await queue.settled(); expect(signal?.aborted).toBe(true); expect(states).toEqual({}); - upload.mockResolvedValueOnce(true); + upload.mockResolvedValueOnce("uploaded"); queue.sync([local]); await queue.settled(); expect(upload.mock.calls[1]?.[0]).toBe(local); @@ -107,7 +110,10 @@ describe("composer attachment upload queue", () => { it("retries a failed attachment when the worker restores requests after reconnect", async () => { let states: Readonly> = {}; - const upload = vi.fn().mockRejectedValueOnce(new Error("Disconnected")).mockResolvedValue(true); + const upload = vi + .fn() + .mockRejectedValueOnce(new Error("Disconnected")) + .mockResolvedValue("uploaded"); const queue = createComposerAttachmentUploadQueue({ upload, onChange: (next) => { @@ -131,7 +137,7 @@ describe("composer attachment upload queue", () => { }); it("ignores a late completion after removal or environment switch", async () => { - const gate = Promise.withResolvers(); + const gate = Promise.withResolvers(); const started = Promise.withResolvers(); let states: Readonly> = {}; const upload = vi.fn(async () => { @@ -146,10 +152,10 @@ describe("composer attachment upload queue", () => { }); queue.sync([request("photo")]); await started.promise; - upload.mockResolvedValueOnce(true); + upload.mockResolvedValueOnce("uploaded"); const other = EnvironmentId.make("environment-2"); queue.sync([request("photo", other)]); - gate.resolve(true); + gate.resolve("uploaded"); await queue.settled(); expect(states).toEqual({ [composerAttachmentUploadKey(other, "photo")]: { status: "ready" } }); queue.sync([]); @@ -159,9 +165,9 @@ describe("composer attachment upload queue", () => { it("restarts a re-added attachment after its aborted transfer finishes settling", async () => { const firstStarted = Promise.withResolvers(); - const firstSettled = Promise.withResolvers(); + const firstSettled = Promise.withResolvers(); const secondStarted = Promise.withResolvers(); - const secondSettled = Promise.withResolvers(); + const secondSettled = Promise.withResolvers(); let states: Readonly> = {}; let firstSignal: AbortSignal | undefined; const upload = vi.fn(async (_request: ComposerAttachmentUploadRequest, signal: AbortSignal) => { @@ -186,10 +192,10 @@ describe("composer attachment upload queue", () => { queue.sync([local]); expect(firstSignal?.aborted).toBe(true); expect(upload).toHaveBeenCalledOnce(); - firstSettled.resolve(false); + firstSettled.resolve("aborted"); await secondStarted.promise; expect(upload).toHaveBeenCalledTimes(2); - secondSettled.resolve(true); + secondSettled.resolve("uploaded"); await queue.settled(); expect(states[composerAttachmentUploadKey(environmentId, local.attachment.id)]).toEqual({ status: "ready", @@ -205,7 +211,7 @@ describe("composer attachment upload queue", () => { _request: ComposerAttachmentUploadRequest, _signal: AbortSignal, report: (value: number) => void, - ): Promise => { + ): Promise => { report(0.12); report(0.13); report(1.1); @@ -229,21 +235,45 @@ describe("composer attachment upload queue", () => { reason: "Server unavailable", }); expect(progress).toEqual([0, 0.1, 1]); - upload.mockImplementationOnce(async () => true); + upload.mockImplementationOnce(async () => "uploaded"); queue.retry(environmentId, "file"); await queue.settled(); expect(states[composerAttachmentUploadKey(environmentId, "file")]).toEqual({ status: "ready" }); queue.dispose(); }); - it("does not spin when an upload's draft was abandoned before persistence", async () => { - const upload = vi.fn(async () => false); - const queue = createComposerAttachmentUploadQueue({ upload, onChange: () => {} }); - queue.sync([request("discarded")]); - await queue.settled(); - expect(upload).toHaveBeenCalledOnce(); - queue.dispose(); - }); + it.effect("joins a send to the running upload instead of uploading again", () => + Effect.gen(function* () { + const gate = Promise.withResolvers(); + const started = Promise.withResolvers(); + const uploaded = vi.fn(); + const upload = vi.fn(async () => { + started.resolve(); + return gate.promise; + }); + let states: Readonly> = {}; + const queue = createComposerAttachmentUploadQueue({ + upload, + onUploaded: uploaded, + onChange: (next) => { + states = next; + }, + }); + const local = request("video"); + queue.sync([local]); + yield* Effect.promise(() => started.promise); + // Sending hands the attachment from the draft to the outbox, which uploads it too. + const sent = yield* Effect.forkChild(queue.upload(local), { startImmediately: true }); + queue.sync([]); + gate.resolve("uploaded-1"); + expect(yield* Fiber.join(sent)).toBe("uploaded-1"); + expect(upload).toHaveBeenCalledOnce(); + expect(uploaded).toHaveBeenCalledExactlyOnceWith(local, "uploaded-1"); + yield* Effect.promise(() => queue.settled()); + expect(states).toEqual({}); + queue.dispose(); + }), + ); }); describe("draft upload scope and offline submission", () => { diff --git a/apps/mobile/src/lib/composerAttachmentUploadQueue.ts b/apps/mobile/src/lib/composerAttachmentUploadQueue.ts index 0a28fff2231d5..f52b6fcdbe36b 100644 --- a/apps/mobile/src/lib/composerAttachmentUploadQueue.ts +++ b/apps/mobile/src/lib/composerAttachmentUploadQueue.ts @@ -1,4 +1,13 @@ import { EnvironmentId, type ServerConfig } from "@t3tools/contracts"; +import * as Effect from "effect/Effect"; +import * as Equal from "effect/Equal"; +import * as Exit from "effect/Exit"; +import * as Fiber from "effect/Fiber"; +import * as Hash from "effect/Hash"; +import * as RcMap from "effect/RcMap"; +import * as Schema from "effect/Schema"; +import * as Scope from "effect/Scope"; +import * as Semaphore from "effect/Semaphore"; import { clampFileAttachmentUploadBytes } from "@t3tools/client-runtime/state/attachments"; import { parseLegacyNewTaskDraftKey } from "../state/new-task-draft-key"; @@ -129,22 +138,45 @@ export function queuedAttachmentsStillUploading(input: { ); } +/** Equal by environment and attachment id; the request rides along for the upload. */ +class UploadKey implements Equal.Equal { + constructor( + readonly id: string, + readonly request: ComposerAttachmentUploadRequest | null, + ) {} + static of(request: ComposerAttachmentUploadRequest): UploadKey { + return new UploadKey( + composerAttachmentUploadKey(request.environmentId, request.attachment.id), + request, + ); + } + [Equal.symbol](that: unknown): boolean { + return that instanceof UploadKey && that.id === this.id; + } + [Hash.symbol](): number { + return Hash.string(this.id); + } +} + +/** + * Every upload of a composer attachment, shared by key: the composer holds the attachments it + * shows or has queued, and a send joins the upload already running instead of starting another. + * An upload is interrupted once nothing holds it. + */ export function createComposerAttachmentUploadQueue(options: { readonly upload: ( request: ComposerAttachmentUploadRequest, signal: AbortSignal, onProgress: (progress: number) => void, - ) => Promise; + ) => Promise; + /** Saves a finished upload's id; the upload is ready once this succeeds. */ + readonly onUploaded?: ( + request: ComposerAttachmentUploadRequest, + uploadedId: string, + ) => Effect.Effect; readonly onChange: (states: Readonly>) => void; }) { - const jobs = new Map< - string, - { readonly controller: AbortController; readonly done: Promise } - >(); - let desired = new Map(); let states: Readonly> = {}; - let disposed = false; - function setState(key: string, state: ComposerAttachmentUploadState | undefined) { const previous = states[key]; if ( @@ -161,82 +193,134 @@ export function createComposerAttachmentUploadQueue(options: { options.onChange(states); } - function pump() { - if (disposed) return; - for (const [key, request] of desired) { - if (jobs.size >= 3) break; - if (jobs.has(key) || states[key]?.status === "ready" || states[key]?.status === "failed") - continue; + const permits = Semaphore.makeUnsafe(3); + const running = new Set>(); + // The latest transfer per attachment: a re-added attachment waits for an aborted one to settle. + const transfers = new Map>(); + + /** The native transfer; interrupting it aborts the request and waits for it to settle. */ + const nativeTransfer = (key: UploadKey) => + Effect.suspend(() => { const controller = new AbortController(); - setState(key, { status: "uploading", progress: 0 }); - // Publish the job before starting async work, including synchronous test transports. - const done = Promise.resolve() - .then(() => - options.upload(request, controller.signal, (progress) => { - if (controller.signal.aborted) return; - setState(key, { - status: "uploading", - progress: Math.floor(Math.max(0, Math.min(1, progress)) * 20) / 20, - }); - }), - ) - .then((persisted) => { - if (!controller.signal.aborted && desired.has(key)) { - if (!persisted) desired.delete(key); - setState(key, persisted ? { status: "ready" } : undefined); - } - }) - .catch((error: unknown) => { - if (!controller.signal.aborted && desired.has(key)) { - setState(key, { - status: "failed", - reason: error instanceof Error ? error.message : "Upload failed. Tap to retry.", - }); - } - }) - .finally(() => { - jobs.delete(key); - pump(); + if (states[key.id]?.status !== "uploading") { + setState(key.id, { status: "uploading", progress: 0 }); + } + const done = options.upload(key.request!, controller.signal, (progress) => { + if (controller.signal.aborted) return; + setState(key.id, { + status: "uploading", + progress: Math.floor(Math.max(0, Math.min(1, progress)) * 20) / 20, }); - jobs.set(key, { controller, done }); + }); + return Effect.tryPromise({ try: () => done, catch: uploadError }).pipe( + Effect.onInterrupt(() => + Effect.sync(() => controller.abort()).pipe( + Effect.andThen(Effect.tryPromise({ try: () => done, catch: uploadError })), + Effect.ignore, + ), + ), + ); + }); + + const transfer = (key: UploadKey, previous: Fiber.Fiber | undefined) => + (previous === undefined ? Effect.void : Fiber.await(previous)).pipe( + Effect.andThen(nativeTransfer(key).pipe(Semaphore.withPermits(permits, 1))), + Effect.tap((uploadedId) => options.onUploaded?.(key.request!, uploadedId) ?? Effect.void), + Effect.tap(() => Effect.sync(() => setState(key.id, { status: "ready" }))), + Effect.tapError((error) => + Effect.sync(() => setState(key.id, { status: "failed", reason: error.message })), + ), + ); + + const scope = Scope.makeUnsafe(); + const uploads = Effect.runSync( + Scope.provide(scope)( + RcMap.make({ + lookup: Effect.fn("ComposerAttachmentUploads.lookup")(function* (key: UploadKey) { + setState(key.id, { status: "uploading", progress: 0 }); + yield* Effect.addFinalizer(() => Effect.sync(() => setState(key.id, undefined))); + const fiber = yield* Effect.forkScoped(transfer(key, transfers.get(key.id))); + transfers.set(key.id, fiber); + running.add(fiber); + fiber.addObserver(() => { + running.delete(fiber); + if (transfers.get(key.id) === fiber) transfers.delete(key.id); + }); + return fiber; + }), + }), + ), + ); + + const holders = new Map(); + let requested: ReadonlyArray = []; + let disposed = false; + const release = (id: string) => { + const holder = holders.get(id); + if (!holder) return; + holders.delete(id); + Effect.runFork(Scope.close(holder, Exit.void)); + }; + + /** Holds exactly these uploads; anything no longer requested and held nowhere else stops. */ + function sync(requests: ReadonlyArray) { + if (disposed) return; + requested = requests; + const keys = new Map(requests.map((request) => [UploadKey.of(request).id, request])); + for (const id of holders.keys()) { + if (!keys.has(id)) release(id); + } + for (const [id, request] of keys) { + if (holders.has(id)) continue; + const holder = Scope.makeUnsafe(); + holders.set(id, holder); + Effect.runSync(Scope.provide(holder)(RcMap.get(uploads, UploadKey.of(request)))); } } return { - sync(requests: ReadonlyArray) { - if (disposed) return; - desired = new Map( - requests.map((request) => [ - composerAttachmentUploadKey(request.environmentId, request.attachment.id), - request, - ]), - ); - for (const [key, job] of jobs) { - if (!desired.has(key)) job.controller.abort(); - } - for (const key of Object.keys(states)) { - if (!desired.has(key)) setState(key, undefined); - } - for (const key of desired.keys()) { - if (!states[key]) setState(key, { status: "uploading", progress: 0 }); - } - pump(); + sync, + /** Uploads the attachment, joining its running upload when there is one. */ + upload( + request: ComposerAttachmentUploadRequest, + ): Effect.Effect { + return disposed + ? Effect.fail(uploadError(new Error("Upload cancelled."))) + : RcMap.get(uploads, UploadKey.of(request)).pipe(Effect.flatMap(Fiber.join), Effect.scoped); }, retry(environmentId: EnvironmentId, attachmentId: string) { - const key = composerAttachmentUploadKey(environmentId, attachmentId); - if (states[key]?.status !== "failed") return; - setState(key, undefined); - pump(); + const id = composerAttachmentUploadKey(environmentId, attachmentId); + if (states[id]?.status !== "failed") return; + Effect.runSync(RcMap.invalidate(uploads, new UploadKey(id, null))); + release(id); + sync(requested); }, async settled() { - while (jobs.size > 0) await Promise.all([...jobs.values()].map((job) => job.done)); + while (running.size > 0) { + await Effect.runPromise(Fiber.awaitAll([...running])); + } }, dispose() { disposed = true; - desired.clear(); - for (const job of jobs.values()) job.controller.abort(); + holders.clear(); + Effect.runFork(Scope.close(scope, Exit.void)); states = {}; options.onChange(states); }, }; } + +export function uploadError(cause: unknown): ComposerAttachmentUploadError { + if (isComposerAttachmentUploadError(cause)) return cause; + return new ComposerAttachmentUploadError({ + message: cause instanceof Error ? cause.message : "Upload failed. Tap to retry.", + cause, + }); +} + +export class ComposerAttachmentUploadError extends Schema.TaggedError()( + "ComposerAttachmentUploadError", + { message: Schema.String, cause: Schema.Defect() }, +) {} + +const isComposerAttachmentUploadError = Schema.is(ComposerAttachmentUploadError); diff --git a/apps/mobile/src/state/composer-attachment-uploads.test.ts b/apps/mobile/src/state/composer-attachment-uploads.test.ts index 81a542a5aca1c..ac8bed2a42a75 100644 --- a/apps/mobile/src/state/composer-attachment-uploads.test.ts +++ b/apps/mobile/src/state/composer-attachment-uploads.test.ts @@ -369,7 +369,8 @@ it("keeps an unexpected draft flush error blocking Send", async () => { expect(composerAttachmentUploadBlockReason(sendInput())).toBe( "Retry or remove the failed attachment", ); - expect(native.deleted).toEqual(["pending-1"]); + // The server sweeps pending uploads nothing claims; a retry uploads again. + expect(native.deleted).toEqual([]); }); it("uploads draft attachments for threads whose ids contain colons", () => { diff --git a/apps/mobile/src/state/composer-attachment-uploads.ts b/apps/mobile/src/state/composer-attachment-uploads.ts index 8e2921c5f62d2..71812bb70aa72 100644 --- a/apps/mobile/src/state/composer-attachment-uploads.ts +++ b/apps/mobile/src/state/composer-attachment-uploads.ts @@ -1,16 +1,17 @@ -import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; import { useAtomValue } from "@effect/atom-react"; import { AuthOrchestrationOperateScope, type EnvironmentId } from "@t3tools/contracts"; +import * as Effect from "effect/Effect"; import * as Schema from "effect/Schema"; -import { Atom, AsyncResult } from "effect/reactivity"; +import { Atom } from "effect/reactivity"; -import { prepareTurnAttachments } from "../lib/attachmentUpload"; +import { uploadComposerAttachment } from "../lib/attachmentUpload"; import { retainComposerAttachmentFileForPreview } from "../lib/composerAttachmentPreviewRetention"; import { isFileBackedComposerAttachment, type DraftComposerAttachment, } from "../lib/composerImages"; import { + uploadError, composerAttachmentUploadKey, composerDraftEnvironmentId, canUploadComposerAttachment, @@ -22,8 +23,7 @@ import { environmentServerConfigsAtom } from "./server"; import { flattenQueuedThreadMessages, threadOutboxManager, - threadOutboxRevision, - updateThreadOutboxMessage, + modifyThreadOutboxMessage, type QueuedThreadMessage, } from "./thread-outbox"; import { editingQueuedMessageIdsAtom } from "./use-thread-outbox"; @@ -136,33 +136,35 @@ function withUploadedAttachment( }; } -async function recordQueuedAttachmentUpload( +const recordQueuedAttachmentUpload = Effect.fn("ComposerAttachments.recordQueuedUpload")(function* ( environmentId: EnvironmentId, uploaded: DraftComposerAttachment, -): Promise { - const awaiting = () => { - const editing = appAtomRegistry.get(editingQueuedMessageIdsAtom); - return flattenQueuedThreadMessages( - appAtomRegistry.get(threadOutboxManager.queuedMessagesByThreadKeyAtom), - ).flatMap((message) => { - if (message.environmentId !== environmentId || editing[message.messageId]) return []; - const updated = withUploadedAttachment(message, uploaded); - return updated === null - ? [] - : [{ updated, revision: threadOutboxRevision(message.messageId) }]; - }); - }; - let recorded = false; - // A sibling attachment finishing at the same time bumps the revision; reread and retry. - for (let attempt = 0; attempt < 3; attempt++) { - const pending = awaiting(); - if (pending.length === 0) break; - for (const { updated, revision } of pending) { - recorded = (await updateThreadOutboxMessage(updated, revision)) || recorded; - } - } - return recorded; -} +) { + const editing = appAtomRegistry.get(editingQueuedMessageIdsAtom); + const holders = flattenQueuedThreadMessages( + appAtomRegistry.get(threadOutboxManager.queuedMessagesByThreadKeyAtom), + ).filter( + (message) => + message.environmentId === environmentId && + !editing[message.messageId] && + withUploadedAttachment(message, uploaded) !== null, + ); + yield* Effect.forEach( + holders, + (message) => + Effect.tryPromise({ + // An editor that opened meanwhile owns the message; its draft records the upload. + try: () => + modifyThreadOutboxMessage(message.messageId, (current) => + appAtomRegistry.get(editingQueuedMessageIdsAtom)[current.messageId] + ? null + : withUploadedAttachment(current, uploaded), + ), + catch: uploadError, + }), + { concurrency: "unbounded", discard: true }, + ); +}); // Each connected environment's session is read here so a granted or revoked // operate scope re-syncs the queue. @@ -193,8 +195,73 @@ const composerAttachmentUploadRequestsAtom = Atom.make((get) => { }); }).pipe(Atom.withLabel("mobile:composer-attachment-upload-requests")); +/** Saves a finished upload's id on every draft and queued message holding the attachment. */ +const recordComposerAttachmentUpload = Effect.fn("ComposerAttachments.recordUpload")(function* ( + request: ComposerAttachmentUploadRequest, + uploadedId: string, +) { + const { environmentId, attachment } = request; + const uploaded: DraftComposerAttachment = { + ...attachment, + uploadedAttachmentId: uploadedId, + uploadEnvironmentId: environmentId, + }; + const queued = flattenQueuedThreadMessages( + appAtomRegistry.get(threadOutboxManager.queuedMessagesByThreadKeyAtom), + ); + const knownEnvironmentIds = appAtomRegistry + .get(workspaceConnections.environmentsAtom) + .map((environment) => environment.environmentId); + let draftsChanged = false; + for (const [key, draft] of Object.entries(appAtomRegistry.get(composerDraftsAtom))) { + if ( + composerDraftEnvironmentId(key, knownEnvironmentIds, queued, draft) === environmentId && + draft.attachments.some((candidate) => candidate.id === attachment.id) + ) { + draftsChanged = setComposerDraftAttachmentUpload(key, uploaded) || draftsChanged; + } + } + yield* recordQueuedAttachmentUpload(environmentId, uploaded); + if (!draftsChanged) return; + // A failed draft save only costs a re-upload after restart; the outbox saves its own references. + yield* Effect.tryPromise({ try: () => flushComposerDrafts(), catch: uploadError }).pipe( + Effect.catchIf( + (error) => isComposerDraftPersistenceError(error.cause), + (error) => + Effect.logWarning( + "[composer-attachments] could not save the uploaded reference", + error.cause, + ), + ), + ); +}); + +function composerAttachmentUploads() { + uploadQueue ??= createComposerAttachmentUploadQueue({ + onChange: (states) => appAtomRegistry.set(composerAttachmentUploadsAtom, states), + upload: ({ environmentId, attachment }, signal, onProgress) => { + const release = isFileBackedComposerAttachment(attachment) + ? retainComposerAttachmentFileForPreview(attachment) + : undefined; + return uploadComposerAttachment(environmentId, attachment, signal, onProgress).finally(() => + release?.(), + ); + }, + onUploaded: recordComposerAttachmentUpload, + }); + return uploadQueue; +} + +/** Every send uploads through here, so it joins the composer's upload of the same attachment. */ +export function uploadComposerAttachmentShared( + environmentId: EnvironmentId, + attachment: DraftComposerAttachment, +) { + return composerAttachmentUploads().upload({ environmentId, attachment }); +} + /** - * Uploads draft attachments to their environment as soon as it is connected and + * Uploads draft and queued attachments to their environment as soon as it is connected and * operable. The queue follows the requests atom for its whole lifetime. */ export function createComposerAttachmentUploadWorker( @@ -203,54 +270,7 @@ export function createComposerAttachmentUploadWorker( > = composerAttachmentUploadRequestsAtom, ) { ensureComposerDraftsLoaded(); - const queue = createComposerAttachmentUploadQueue({ - onChange: (states) => appAtomRegistry.set(composerAttachmentUploadsAtom, states), - upload: async ({ environmentId, attachment }, signal, onProgress) => { - const release = isFileBackedComposerAttachment(attachment) - ? retainComposerAttachmentFileForPreview(attachment) - : undefined; - const prepared = await settlePromise(() => - prepareTurnAttachments({ - environmentId, - attachments: [attachment], - supportsImageUploads: true, - signal, - onUploadProgress: (_, progress) => onProgress(progress), - persistUploadedReferences: async ([uploaded]) => { - if (signal.aborted || !uploaded) return "abandon"; - const queued = flattenQueuedThreadMessages( - appAtomRegistry.get(threadOutboxManager.queuedMessagesByThreadKeyAtom), - ); - const knownEnvironmentIds = appAtomRegistry - .get(workspaceConnections.environmentsAtom) - .map((environment) => environment.environmentId); - let retained = false; - for (const [key, draft] of Object.entries(appAtomRegistry.get(composerDraftsAtom))) { - if ( - composerDraftEnvironmentId(key, knownEnvironmentIds, queued, draft) === - environmentId && - draft.attachments.some((candidate) => candidate.id === attachment.id) - ) { - retained = setComposerDraftAttachmentUpload(key, uploaded) || retained; - } - } - retained = (await recordQueuedAttachmentUpload(environmentId, uploaded)) || retained; - if (!retained) return "abandon"; - // Draft-save failure only costs re-upload after restart; the outbox saves references independently, so preserve accepted uploads. - await flushComposerDrafts().catch((error: unknown) => { - if (!isComposerDraftPersistenceError(error)) throw error; - console.warn("[composer-attachments] could not save the uploaded reference", error); - }); - return "persisted"; - }, - }), - ); - release?.(); - if (AsyncResult.isFailure(prepared)) throw squashAtomCommandFailure(prepared); - return prepared.value.status === "ready"; - }, - }); - uploadQueue = queue; + const queue = composerAttachmentUploads(); const unsubscribe = appAtomRegistry.subscribe(requestsAtom, (requests) => queue.sync(requests), { immediate: true, }); diff --git a/apps/mobile/src/state/thread-outbox-manager.ts b/apps/mobile/src/state/thread-outbox-manager.ts index 732db0159b7f7..a83537fdb0a0c 100644 --- a/apps/mobile/src/state/thread-outbox-manager.ts +++ b/apps/mobile/src/state/thread-outbox-manager.ts @@ -176,6 +176,46 @@ export function createThreadOutboxManager(options: ThreadOutboxManagerOptions) { return true; }); + /** + * Applies `change` to the message as it stands when this mutation's turn comes, so concurrent + * changes compose instead of failing on each other's revision. `null` leaves it unchanged. + */ + const modify = ( + messageId: MessageId, + change: (message: QueuedThreadMessage) => QueuedThreadMessage | null, + ): Promise => + serialize(async () => { + const current = currentMessages().find((candidate) => candidate.messageId === messageId); + const next = current === undefined ? null : change(current); + if (current === undefined || next === null) return false; + const revision = revisions.get(messageId) ?? 0; + try { + await options.storage.write(next); + } catch (cause) { + throw new ThreadOutboxManagerError({ + operation: "update", + environmentId: next.environmentId, + threadId: next.threadId, + messageId, + cause, + }); + } + // An enqueue publishes without waiting its turn; when one replaced the message during the + // write, it wins and stays durable. + if ((revisions.get(messageId) ?? 0) !== revision || !currentMessages().includes(current)) { + const winner = currentMessages().find((candidate) => candidate.messageId === messageId); + if (winner !== undefined) { + try { + await options.storage.write(winner); + } catch {} + } + return false; + } + bumpRevision(messageId); + setMessages(currentMessages().map((candidate) => (candidate === current ? next : candidate))); + return true; + }); + // Check live ownership too; editor locks can change without message revision updates. const remove = ( message: QueuedThreadMessage, @@ -347,6 +387,7 @@ export function createThreadOutboxManager(options: ThreadOutboxManagerOptions) { confirmQueued, revisionOf: (messageId: MessageId): number => revisions.get(messageId) ?? 0, update, + modify, remove, clearEnvironment, }; diff --git a/apps/mobile/src/state/thread-outbox-model.ts b/apps/mobile/src/state/thread-outbox-model.ts index 64fe62d8a15f7..59427907c4ebf 100644 --- a/apps/mobile/src/state/thread-outbox-model.ts +++ b/apps/mobile/src/state/thread-outbox-model.ts @@ -288,6 +288,9 @@ export function shouldRetryThreadOutboxDelivery(error: unknown): boolean { case "EnvironmentRpcUnavailableError": case "EnvironmentNotRegisteredError": return true; + // A shared upload's failure decides by what made the upload fail. + case "ComposerAttachmentUploadError": + return "cause" in error && shouldRetryThreadOutboxDelivery(error.cause); default: break; } diff --git a/apps/mobile/src/state/thread-outbox.test.ts b/apps/mobile/src/state/thread-outbox.test.ts index 95333fc5925d2..5ff7bd53a1c7d 100644 --- a/apps/mobile/src/state/thread-outbox.test.ts +++ b/apps/mobile/src/state/thread-outbox.test.ts @@ -718,6 +718,42 @@ describe("thread outbox", () => { registry.dispose(); }); + it("composes concurrent modifications of one message instead of rejecting the later one", async () => { + const registry = AtomRegistry.make(); + const stored = new Map(); + const manager = createThreadOutboxManager({ + registry, + storage: { + load: async () => ({ messages: [], errors: [] }), + write: async (message) => { + stored.set(message.messageId, message); + }, + remove: async () => undefined, + }, + }); + const message = queuedMessage({ + messageId: "message-two-uploads", + createdAt: "2026-06-08T10:00:01.000Z", + }); + await manager.enqueue(message); + + // Two attachments finishing together each record their upload on the same message. + const results = await Promise.all([ + manager.modify(message.messageId, (current) => ({ ...current, text: `${current.text} a` })), + manager.modify(message.messageId, (current) => ({ ...current, text: `${current.text} b` })), + ]); + + expect(results).toEqual([true, true]); + const composed = { ...message, text: `${message.text} a b` }; + expect(registry.get(manager.queuedMessagesByThreadKeyAtom)).toEqual({ + "environment-1:thread-1": [composed], + }); + expect(stored.get(message.messageId)).toEqual(composed); + await manager.remove(composed); + await expect(manager.modify(message.messageId, (current) => current)).resolves.toBe(false); + registry.dispose(); + }); + it("rejects a stale revision before its payload reaches durable storage", async () => { const registry = AtomRegistry.make(); const writes: string[] = []; diff --git a/apps/mobile/src/state/thread-outbox.ts b/apps/mobile/src/state/thread-outbox.ts index 4a9c7d666e129..24f3c1512abce 100644 --- a/apps/mobile/src/state/thread-outbox.ts +++ b/apps/mobile/src/state/thread-outbox.ts @@ -32,6 +32,14 @@ export function updateThreadOutboxMessage( return threadOutboxManager.update(message, expectedRevision); } +/** Changes the message as it is when its turn comes; false when it is gone or `change` returns null. */ +export function modifyThreadOutboxMessage( + messageId: QueuedThreadMessage["messageId"], + change: (message: QueuedThreadMessage) => QueuedThreadMessage | null, +): Promise { + return threadOutboxManager.modify(messageId, change); +} + export function threadOutboxRevision(messageId: QueuedThreadMessage["messageId"]): number { return threadOutboxManager.revisionOf(messageId); } diff --git a/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts b/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts index 263c48195d75d..b9ad37a6d7d0d 100644 --- a/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts +++ b/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts @@ -7,6 +7,8 @@ import { ProviderInstanceId, ThreadId, } from "@t3tools/contracts"; +import { uploadError } from "../lib/composerAttachmentUploadQueue"; +import * as Effect from "effect/Effect"; import * as Cause from "effect/Cause"; import { AsyncResult, type Atom } from "effect/reactivity"; import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; @@ -107,6 +109,7 @@ vi.mock("./git", () => ({ gitEnvironment: { preparePullRequestThread: state.chec vi.mock("../lib/modelOptions", () => ({ isModelSelectionUnavailable: state.modelUnavailable })); vi.mock("../lib/uuid", () => ({ uuidv4: () => "uuid", randomHex: () => "abcd" })); vi.mock("../lib/attachmentUpload", () => ({ prepareTurnAttachments: state.prepare })); +vi.mock("./composer-attachment-uploads", () => ({ uploadComposerAttachmentShared: vi.fn() })); vi.mock("./thread-outbox", async () => { const { createThreadOutboxManager } = await import("./thread-outbox-manager"); const { appAtomRegistry } = await import("./atom-registry"); @@ -229,12 +232,14 @@ beforeEach(() => { command.mockReset().mockResolvedValue(AsyncResult.success(undefined)); } state.start.mockResolvedValue(AsyncResult.success({})); - state.prepare.mockReset().mockImplementation(async ({ attachments }) => ({ - status: "ready", - attachments: [], - draftAttachments: attachments, - pendingAttachmentIds: [], - })); + state.prepare.mockReset().mockImplementation(({ attachments }) => + Effect.succeed({ + status: "ready", + attachments: [], + draftAttachments: attachments, + pendingAttachmentIds: [], + }), + ); }); describe("queued PR checkout", () => { @@ -485,16 +490,19 @@ describe("queued task operation access", () => { : {}, ); await state.manager.enqueue(queued); - state.prepare.mockImplementationOnce(async ({ attachments }) => { - setOperateGrant("secondary", false); - if (uploadFails) throw new Error("This connection cannot upload attachments."); - return { - status: "ready", - attachments: [], - draftAttachments: attachments, - pendingAttachmentIds: [], - }; - }); + state.prepare.mockImplementationOnce(({ attachments }) => + Effect.suspend(() => { + setOperateGrant("secondary", false); + return uploadFails + ? Effect.fail(uploadError(new Error("This connection cannot upload attachments."))) + : Effect.succeed({ + status: "ready", + attachments: [], + draftAttachments: attachments, + pendingAttachmentIds: [], + }); + }), + ); await runDrain(); expect(state.prepare).toHaveBeenCalledOnce(); expect(state.start).not.toHaveBeenCalled(); diff --git a/apps/mobile/src/state/threadOutboxDelivery.test.ts b/apps/mobile/src/state/threadOutboxDelivery.test.ts index f7d67f4349397..b85ed8e4bf4cd 100644 --- a/apps/mobile/src/state/threadOutboxDelivery.test.ts +++ b/apps/mobile/src/state/threadOutboxDelivery.test.ts @@ -2,6 +2,7 @@ import { acknowledgedThreadMessagesAtom, pruneCaughtUpAcknowledgedThreadMessages, } from "./acknowledged-thread-messages"; +import * as Effect from "effect/Effect"; import { CommandId, EnvironmentId, @@ -84,6 +85,7 @@ vi.mock("../lib/uuid", () => ({ vi.mock("../lib/attachmentUpload", () => ({ prepareTurnAttachments: harness.prepareTurnAttachments, })); +vi.mock("./composer-attachment-uploads", () => ({ uploadComposerAttachmentShared: vi.fn() })); vi.mock("./projects", () => ({ environmentProjects: {} })); @@ -241,12 +243,14 @@ describe("thread outbox attachment preparation", () => { fileUri: "file:///documents/keep.pdf", }); await harness.manager.enqueue(message); - harness.prepareTurnAttachments.mockResolvedValueOnce({ - status: "ready", - attachments: [], - draftAttachments: message.attachments, - pendingAttachmentIds: [], - }); + harness.prepareTurnAttachments.mockReturnValueOnce( + Effect.succeed({ + status: "ready", + attachments: [], + draftAttachments: message.attachments, + pendingAttachmentIds: [], + }), + ); harness.canOperate = false; await expect(prepareQueuedMessageAttachments(message)).resolves.toEqual({ status: "abandoned", @@ -266,10 +270,12 @@ describe("thread outbox attachment preparation", () => { ); const preparationStarted = Promise.withResolvers(); const preparationBarrier = Promise.withResolvers(); - harness.prepareTurnAttachments.mockImplementationOnce(async () => { - preparationStarted.resolve(); - return preparationBarrier.promise; - }); + harness.prepareTurnAttachments.mockImplementationOnce(() => + Effect.promise(() => { + preparationStarted.resolve(); + return preparationBarrier.promise; + }), + ); await harness.manager.enqueue(message); appAtomRegistry.set(editingQueuedMessageIdsAtom, { [message.messageId]: true }); @@ -304,15 +310,17 @@ describe("thread outbox attachment preparation", () => { } : attachment, ); - harness.prepareTurnAttachments.mockImplementationOnce(async (input) => { - expect(await input.persistUploadedReferences?.(uploadedAttachments)).toBe("persisted"); - return { - status: "ready", - attachments: [], - draftAttachments: uploadedAttachments, - pendingAttachmentIds: ["pending-new-upload"], - }; - }); + harness.prepareTurnAttachments.mockImplementationOnce((input) => + Effect.gen(function* () { + expect(yield* input.persistUploadedReferences!(uploadedAttachments)).toBe("persisted"); + return { + status: "ready", + attachments: [], + draftAttachments: uploadedAttachments, + pendingAttachmentIds: ["pending-new-upload"], + } as const; + }), + ); await harness.manager.enqueue(message); const revision = harness.manager.revisionOf(message.messageId); diff --git a/apps/mobile/src/state/threadOutboxDelivery.ts b/apps/mobile/src/state/threadOutboxDelivery.ts index 74cf7597e0ef7..3a3802d425897 100644 --- a/apps/mobile/src/state/threadOutboxDelivery.ts +++ b/apps/mobile/src/state/threadOutboxDelivery.ts @@ -23,6 +23,7 @@ import { } from "@t3tools/contracts"; import { buildTemporaryWorktreeBranchName } from "@t3tools/shared/git"; import * as Cause from "effect/Cause"; +import * as Effect from "effect/Effect"; import { AsyncResult, Atom } from "effect/reactivity"; import { createDebugLogger } from "../lib/debugLog"; @@ -49,6 +50,7 @@ import { import { environmentProjects } from "./projects"; import { environmentServerConfigsAtom, serverEnvironment } from "./server"; import { composerAttachmentUploadsAtom } from "./composer-attachment-upload-states"; +import { uploadComposerAttachmentShared } from "./composer-attachment-uploads"; import { queuedAttachmentsStillUploading, uploadingComposerAttachmentKeys, @@ -229,23 +231,28 @@ export async function prepareQueuedMessageAttachments(queuedMessage: QueuedThrea } let persistedMessage = queuedMessage; let deliveryRevision = revision; - const result = await prepareTurnAttachments({ - environmentId: queuedMessage.environmentId, - attachments: queuedMessage.attachments, - supportsImageUploads: true, - persistUploadedReferences: async (draftAttachments) => { - if (appAtomRegistry.get(editingQueuedMessageIdsAtom)[queuedMessage.messageId]) { - return "abandon"; - } - const updatedMessage = { ...queuedMessage, attachments: draftAttachments }; - if (!(await updateThreadOutboxMessage(updatedMessage, revision))) { - return "abandon"; - } - persistedMessage = updatedMessage; - deliveryRevision = revision + 1; - return "persisted"; - }, - }); + const result = await Effect.runPromise( + prepareTurnAttachments({ + environmentId: queuedMessage.environmentId, + attachments: queuedMessage.attachments, + supportsImageUploads: true, + uploadAttachment: (attachment) => + uploadComposerAttachmentShared(queuedMessage.environmentId, attachment), + persistUploadedReferences: (draftAttachments) => + Effect.promise(async () => { + if (appAtomRegistry.get(editingQueuedMessageIdsAtom)[queuedMessage.messageId]) { + return "abandon" as const; + } + const updatedMessage = { ...queuedMessage, attachments: draftAttachments }; + if (!(await updateThreadOutboxMessage(updatedMessage, revision))) { + return "abandon" as const; + } + persistedMessage = updatedMessage; + deliveryRevision = revision + 1; + return "persisted" as const; + }), + }), + ); if ( result.status === "abandoned" || !isQueuedMessagePayloadCurrent(persistedMessage, deliveryRevision) diff --git a/apps/mobile/src/state/use-composer-drafts.test.ts b/apps/mobile/src/state/use-composer-drafts.test.ts index 200b2b165c6b5..0f0e4a69419fb 100644 --- a/apps/mobile/src/state/use-composer-drafts.test.ts +++ b/apps/mobile/src/state/use-composer-drafts.test.ts @@ -1,4 +1,6 @@ import { PrimaryConnectionTarget } from "@t3tools/client-runtime/connection"; +import * as Effect from "effect/Effect"; +import * as Fiber from "effect/Fiber"; import { createEnvironmentThreadShellAtoms } from "@t3tools/client-runtime/state/threads"; import { applyShellStreamEvent, @@ -1213,63 +1215,67 @@ describe("mobile composer drafts", () => { expect(composerAttachmentCleanupMocks.remove).toHaveBeenCalledExactlyOnceWith(image.fileUri); }); - it("keeps an image through an inline read after its draft is removed", async () => { - const { prepareTurnAttachments } = - await vi.importActual("../lib/attachmentUpload"); - const image = { - id: "image-reading", - type: "image" as const, - name: "photo.png", - mimeType: "image/png", - sizeBytes: 3, - fileUri: "file:///documents/t3-composer-attachments/photo.png", - previewUri: "file:///documents/t3-composer-attachments/photo.png", - }; - const readStarted = Promise.withResolvers(); - const read = Promise.withResolvers(); - const removed = Promise.withResolvers(); - let imageExists = true; - composerDraftFileMocks.readImage.mockImplementation(async () => { - readStarted.resolve(); - await read.promise; - if (!imageExists) throw new Error("The image was deleted during its read"); - return "YWJj"; - }); - composerAttachmentCleanupMocks.remove.mockImplementationOnce(async () => { - imageExists = false; - removed.resolve(); - }); - appendComposerDraftAttachments("environment-1:thread-1", [image]); - await flushComposerDrafts(); - const preparing = prepareTurnAttachments({ - environmentId: EnvironmentId.make("environment-1"), - attachments: [image], - }); - onTestFinished(async () => { - read.resolve(); - await preparing.catch(() => undefined); - }); - await readStarted.promise; - clearComposerDraftContent("environment-1:thread-1", { deferAttachmentCleanup: true }); - await releaseUnusedComposerAttachmentFiles([image]); - expect(imageExists).toBe(true); - - read.resolve(); - const prepared = await preparing; - expect(prepared.status).toBe("ready"); - if (prepared.status !== "ready") return; - expect(prepared.attachments).toEqual([ - { - type: "image", + it.effect("keeps an image through an inline read after its draft is removed", () => + Effect.gen(function* () { + const { prepareTurnAttachments } = yield* Effect.promise(() => + vi.importActual("../lib/attachmentUpload"), + ); + const image = { + id: "image-reading", + type: "image" as const, name: "photo.png", mimeType: "image/png", sizeBytes: 3, - dataUrl: "data:image/png;base64,YWJj", - }, - ]); - await removed.promise; - expect(imageExists).toBe(false); - }); + fileUri: "file:///documents/t3-composer-attachments/photo.png", + previewUri: "file:///documents/t3-composer-attachments/photo.png", + }; + const readStarted = Promise.withResolvers(); + const read = Promise.withResolvers(); + const removed = Promise.withResolvers(); + let imageExists = true; + composerDraftFileMocks.readImage.mockImplementation(async () => { + readStarted.resolve(); + await read.promise; + if (!imageExists) throw new Error("The image was deleted during its read"); + return "YWJj"; + }); + composerAttachmentCleanupMocks.remove.mockImplementationOnce(async () => { + imageExists = false; + removed.resolve(); + }); + appendComposerDraftAttachments("environment-1:thread-1", [image]); + yield* Effect.promise(() => flushComposerDrafts()); + // Without image uploads the image is inlined, so nothing goes through the upload registry. + const preparing = yield* Effect.forkChild( + prepareTurnAttachments({ + environmentId: EnvironmentId.make("environment-1"), + attachments: [image], + uploadAttachment: vi.fn(), + }), + ); + onTestFinished(() => read.resolve()); + yield* Effect.promise(() => readStarted.promise); + clearComposerDraftContent("environment-1:thread-1", { deferAttachmentCleanup: true }); + yield* Effect.promise(() => releaseUnusedComposerAttachmentFiles([image])); + expect(imageExists).toBe(true); + + read.resolve(); + const prepared = yield* Fiber.join(preparing); + expect(prepared.status).toBe("ready"); + if (prepared.status !== "ready") return; + expect(prepared.attachments).toEqual([ + { + type: "image", + name: "photo.png", + mimeType: "image/png", + sizeBytes: 3, + dataUrl: "data:image/png;base64,YWJj", + }, + ]); + yield* Effect.promise(() => removed.promise); + expect(imageExists).toBe(false); + }), + ); it("keeps a failed-send draft's pending upload for retry", async () => { const outboxLoad = vi.spyOn(threadOutboxManager, "load").mockResolvedValue(true); diff --git a/apps/mobile/src/state/use-thread-composer-state.ts b/apps/mobile/src/state/use-thread-composer-state.ts index 08b9e20b27f60..f8f836dd77e9b 100644 --- a/apps/mobile/src/state/use-thread-composer-state.ts +++ b/apps/mobile/src/state/use-thread-composer-state.ts @@ -1,4 +1,5 @@ import { useComposerAttachmentActions } from "./use-composer-attachment-actions"; +import * as Effect from "effect/Effect"; import * as Dialog from "../components/ui/Dialog"; import { AuthOrchestrationOperateScope } from "@t3tools/contracts"; import { readEnvironmentScope } from "./session"; @@ -72,6 +73,7 @@ import { pendingThreadCreationMessage } from "./pending-thread-creation"; import { composerAttachmentUploadBlockReason, composerAttachmentUploadsAtom, + uploadComposerAttachmentShared, } from "./composer-attachment-uploads"; import { appendComposerDraftAttachments, @@ -510,11 +512,15 @@ export function useThreadComposerState(input: { setIsSavingQueuedEdit(true); const capabilities = selectedEnvironmentRuntime?.serverConfig?.environment.capabilities; const prepared = await settlePromise(() => - prepareTurnAttachments({ - environmentId: thread.environmentId, - attachments: draft.attachments, - supportsImageUploads: capabilities !== undefined, - }), + Effect.runPromise( + prepareTurnAttachments({ + environmentId: thread.environmentId, + attachments: draft.attachments, + supportsImageUploads: capabilities !== undefined, + uploadAttachment: (attachment) => + uploadComposerAttachmentShared(thread.environmentId, attachment), + }), + ), ); if (AsyncResult.isFailure(prepared)) { savingQueuedEditRef.current = false; From 07e8bfa70d6e237baa769842d2b406c0eb4b65c7 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 12:15:40 -0800 Subject: [PATCH 035/253] refactor(mobile): deliver queued messages with one Effect fiber per thread (#37725) Co-authored-by: Claude Opus 5.5 --- .../src/lib/composerAttachmentUploadQueue.ts | 24 - .../composer-attachment-upload-states.ts | 8 - .../state/composer-attachment-uploads.test.ts | 49 +- .../src/state/composer-attachment-uploads.ts | 6 +- .../state/edit-pending-thread-message.test.ts | 8 +- .../src/state/edit-pending-thread-message.ts | 4 +- .../src/state/thread-task-permissions.test.ts | 2 +- .../threadOutboxDelivery.permissions.test.ts | 132 +- .../src/state/threadOutboxDelivery.test.ts | 1058 ++++++------ apps/mobile/src/state/threadOutboxDelivery.ts | 1528 +++++++++-------- .../src/state/use-thread-composer-state.ts | 8 +- apps/mobile/src/state/use-thread-outbox.ts | 8 +- 12 files changed, 1504 insertions(+), 1331 deletions(-) delete mode 100644 apps/mobile/src/state/composer-attachment-upload-states.ts diff --git a/apps/mobile/src/lib/composerAttachmentUploadQueue.ts b/apps/mobile/src/lib/composerAttachmentUploadQueue.ts index f52b6fcdbe36b..92434c56b391d 100644 --- a/apps/mobile/src/lib/composerAttachmentUploadQueue.ts +++ b/apps/mobile/src/lib/composerAttachmentUploadQueue.ts @@ -114,30 +114,6 @@ export function composerAttachmentsStillUploading(input: { }); } -/** Changes when an upload starts or settles, not on its progress ticks. */ -export function uploadingComposerAttachmentKeys( - states: Readonly>, -): string { - return Object.keys(states) - .filter((key) => states[key]?.status === "uploading") - .sort() - .join("\n"); -} - -/** A queued message waits for the worker's upload instead of starting a second one. */ -export function queuedAttachmentsStillUploading(input: { - readonly environmentId: EnvironmentId; - readonly attachments: ReadonlyArray; - readonly states: Readonly>; -}): boolean { - return input.attachments.some( - (attachment) => - attachment.uploadedAttachmentId === undefined && - input.states[composerAttachmentUploadKey(input.environmentId, attachment.id)]?.status === - "uploading", - ); -} - /** Equal by environment and attachment id; the request rides along for the upload. */ class UploadKey implements Equal.Equal { constructor( diff --git a/apps/mobile/src/state/composer-attachment-upload-states.ts b/apps/mobile/src/state/composer-attachment-upload-states.ts deleted file mode 100644 index 2dafe12ef678a..0000000000000 --- a/apps/mobile/src/state/composer-attachment-upload-states.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { Atom } from "effect/reactivity"; - -import type { ComposerAttachmentUploadState } from "../lib/composerAttachmentUploadQueue"; - -/** The upload worker's per-attachment states, apart from the worker so the outbox can read them. */ -export const composerAttachmentUploadsAtom = Atom.make< - Readonly> ->({}).pipe(Atom.keepAlive); diff --git a/apps/mobile/src/state/composer-attachment-uploads.test.ts b/apps/mobile/src/state/composer-attachment-uploads.test.ts index ac8bed2a42a75..c931dd1a87aaa 100644 --- a/apps/mobile/src/state/composer-attachment-uploads.test.ts +++ b/apps/mobile/src/state/composer-attachment-uploads.test.ts @@ -1,5 +1,7 @@ import { CommandId, EnvironmentId, MessageId, ThreadId } from "@t3tools/contracts"; -import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test"; +import { expect, it } from "@effect/vitest"; +import * as Effect from "effect/Effect"; +import { afterEach, beforeEach, vi } from "vite-plus/test"; type UploadOptions = { signal: AbortSignal; @@ -167,7 +169,7 @@ vi.mock("./use-thread-outbox", async () => { const { Atom } = await import("effect/reactivity"); return { editingQueuedMessageIdsAtom: Atom.make>({}).pipe(Atom.keepAlive), - dispatchingQueuedMessageIdAtom: Atom.make>({}).pipe(Atom.keepAlive), + dispatchingQueuedMessageIdsAtom: Atom.make>({}).pipe(Atom.keepAlive), useThreadOutboxMessages: () => appAtomRegistry.get(threadOutboxManager.queuedMessagesByThreadKeyAtom), retryingQueuedMessageIdsAtom: Atom.make({}).pipe(Atom.keepAlive), @@ -296,29 +298,30 @@ function uploadState() { ]; } -it.each(["decode", "write"] as const)( +it.effect.each(["decode", "write"] as const)( "keeps an accepted upload ready when the draft %s fails", - async (fault) => { - native.fault = fault; - if (fault === "decode") - native.files.set(draftsUri, Buffer.from('{"schemaVersion":1,"drafts":')); - drafts.resetComposerDraftsLoadState(); - const queue = worker(); - await queue.settled(); - expect(uploadState()).toEqual({ status: "ready" }); - expect(native.deleted).toEqual([]); - expect(composerAttachmentUploadBlockReason(sendInput())).toBeNull(); - expect(composerAttachmentsStillUploading(sendInput())).toBe(false); + (fault) => + Effect.gen(function* () { + native.fault = fault; + if (fault === "decode") + native.files.set(draftsUri, Buffer.from('{"schemaVersion":1,"drafts":')); + drafts.resetComposerDraftsLoadState(); + const queue = worker(); + yield* Effect.promise(() => queue.settled()); + expect(uploadState()).toEqual({ status: "ready" }); + expect(native.deleted).toEqual([]); + expect(composerAttachmentUploadBlockReason(sendInput())).toBeNull(); + expect(composerAttachmentsStillUploading(sendInput())).toBe(false); - const message = queuedMessage(); - expect(message.attachments[0]?.uploadedAttachmentId).toBe("pending-1"); - await enqueueThreadOutboxMessage(message); - const prepared = await prepareQueuedMessageAttachments(message); - expect(prepared.status).toBe("ready"); - if (prepared.status !== "ready") throw new Error("Outbox preparation abandoned"); - expect(prepared.prepared.pendingAttachmentIds).toEqual(["pending-1"]); - expect(native.transfer).toHaveBeenCalledTimes(1); - }, + const message = queuedMessage(); + expect(message.attachments[0]?.uploadedAttachmentId).toBe("pending-1"); + yield* Effect.promise(() => enqueueThreadOutboxMessage(message)); + const prepared = yield* prepareQueuedMessageAttachments(message); + expect(prepared.status).toBe("ready"); + if (prepared.status !== "ready") throw new Error("Outbox preparation abandoned"); + expect(prepared.prepared.pendingAttachmentIds).toEqual(["pending-1"]); + expect(native.transfer).toHaveBeenCalledTimes(1); + }), ); it("keeps an upload running when its message is sent mid-upload and records it on the message", async () => { diff --git a/apps/mobile/src/state/composer-attachment-uploads.ts b/apps/mobile/src/state/composer-attachment-uploads.ts index 71812bb70aa72..acc7576a8d1d8 100644 --- a/apps/mobile/src/state/composer-attachment-uploads.ts +++ b/apps/mobile/src/state/composer-attachment-uploads.ts @@ -16,9 +16,9 @@ import { composerDraftEnvironmentId, canUploadComposerAttachment, createComposerAttachmentUploadQueue, + type ComposerAttachmentUploadState, } from "../lib/composerAttachmentUploadQueue"; import { appAtomRegistry } from "./atom-registry"; -import { composerAttachmentUploadsAtom } from "./composer-attachment-upload-states"; import { environmentServerConfigsAtom } from "./server"; import { flattenQueuedThreadMessages, @@ -46,7 +46,9 @@ export { composerAttachmentsStillUploading, } from "../lib/composerAttachmentUploadQueue"; -export { composerAttachmentUploadsAtom }; +export const composerAttachmentUploadsAtom = Atom.make< + Readonly> +>({}).pipe(Atom.keepAlive); const uploadStateAtom = Atom.family((key: string) => Atom.map(composerAttachmentUploadsAtom, (states) => states[key]), ); diff --git a/apps/mobile/src/state/edit-pending-thread-message.test.ts b/apps/mobile/src/state/edit-pending-thread-message.test.ts index c9c8b4f6a47f8..c9efb7c934e5c 100644 --- a/apps/mobile/src/state/edit-pending-thread-message.test.ts +++ b/apps/mobile/src/state/edit-pending-thread-message.test.ts @@ -3,7 +3,7 @@ import { CommandId, EnvironmentId, MessageId, ThreadId } from "@t3tools/contract import type { QueuedThreadMessage } from "./thread-outbox-model"; const state = vi.hoisted(() => ({ - dispatching: null as string | null, + dispatching: {} as Record, held: {} as Record, draft: { text: "Existing draft", attachments: [] as { id: string }[] }, confirm: vi.fn(async () => true), @@ -16,7 +16,7 @@ vi.mock("./atom-registry", () => ({ }, })); vi.mock("./use-thread-outbox", () => ({ - dispatchingQueuedMessageIdAtom: "dispatching", + dispatchingQueuedMessageIdsAtom: "dispatching", editingQueuedMessageIdsAtom: "editing", holdEditingQueuedMessage: (id: string) => { state.held[id] = true; @@ -67,7 +67,7 @@ const message: QueuedThreadMessage = { }; beforeEach(() => { vi.clearAllMocks(); - state.dispatching = null; + state.dispatching = {}; state.held = {}; state.draft = { text: "Existing draft", attachments: [] }; state.confirm.mockResolvedValue(true); @@ -90,7 +90,7 @@ describe("editing a pending message", () => { expect(state.held).toEqual({}); }); it("does not reclaim a message already being dispatched", async () => { - state.dispatching = message.messageId; + state.dispatching = { [message.messageId]: true }; expect(await editPendingThreadMessage(message)).toBe(false); expect(state.confirm).not.toHaveBeenCalled(); expect(state.draft.text).toBe("Existing draft"); diff --git a/apps/mobile/src/state/edit-pending-thread-message.ts b/apps/mobile/src/state/edit-pending-thread-message.ts index e2f3a2bfcb5d5..6e599abf6823d 100644 --- a/apps/mobile/src/state/edit-pending-thread-message.ts +++ b/apps/mobile/src/state/edit-pending-thread-message.ts @@ -16,7 +16,7 @@ import { waitForComposerDraftsLoaded, } from "./use-composer-drafts"; import { - dispatchingQueuedMessageIdAtom, + dispatchingQueuedMessageIdsAtom, editingQueuedMessageIdsAtom, holdEditingQueuedMessage, releaseEditingQueuedMessage, @@ -26,7 +26,7 @@ import { export async function editPendingThreadMessage(message: QueuedThreadMessage): Promise { if ( message.creation || - appAtomRegistry.get(dispatchingQueuedMessageIdAtom) === message.messageId || + appAtomRegistry.get(dispatchingQueuedMessageIdsAtom)[message.messageId] || appAtomRegistry.get(editingQueuedMessageIdsAtom)[message.messageId] ) { return false; diff --git a/apps/mobile/src/state/thread-task-permissions.test.ts b/apps/mobile/src/state/thread-task-permissions.test.ts index d037ac106a025..7051193be1175 100644 --- a/apps/mobile/src/state/thread-task-permissions.test.ts +++ b/apps/mobile/src/state/thread-task-permissions.test.ts @@ -141,7 +141,7 @@ vi.mock("./threads", async () => { }); vi.mock("./use-thread-outbox", async () => { const { Atom } = await import("effect/reactivity"); - return { dispatchingQueuedMessageIdAtom: Atom.make(null), useThreadOutboxMessages: () => ({}) }; + return { dispatchingQueuedMessageIdsAtom: Atom.make({}), useThreadOutboxMessages: () => ({}) }; }); vi.mock("./thread-outbox", () => ({ enqueueThreadOutboxMessage: state.enqueue })); vi.mock("./composer-attachment-uploads", async () => { diff --git a/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts b/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts index b9ad37a6d7d0d..700b58499d691 100644 --- a/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts +++ b/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts @@ -138,7 +138,9 @@ vi.mock("./use-thread-outbox", async () => { const { Atom } = await import("effect/reactivity"); return { editingQueuedMessageIdsAtom: Atom.make({}).pipe(Atom.keepAlive), - dispatchingQueuedMessageIdAtom: Atom.make(null).pipe(Atom.keepAlive), + dispatchingQueuedMessageIdsAtom: Atom.make>>({}).pipe( + Atom.keepAlive, + ), retryingQueuedMessageIdsAtom: Atom.make({}).pipe(Atom.keepAlive), threadOutboxShellStatusesAtom: Atom.make(new Map([["secondary", "live"]])), }; @@ -149,7 +151,7 @@ vi.mock("./use-composer-drafts", async () => { composerDraftsAtom: Atom.make({}), removeDeliveredCloudQueuedMessage: async () => {}, appendComposerDraftAttachments: vi.fn(), - flushComposerDrafts: vi.fn(), + flushComposerDrafts: vi.fn(async () => {}), getComposerDraftSnapshot: vi.fn(() => ({ text: state.draftText, attachments: [] })), mergeComposerDraftContent: vi.fn(async (_key: string, input: { text: string }) => { state.draftText += `\n${input.text}`; @@ -164,9 +166,10 @@ vi.mock("./use-composer-drafts", async () => { import { appAtomRegistry } from "./atom-registry"; import { environmentSession } from "./session"; -import { dispatchingQueuedMessageIdAtom, editingQueuedMessageIdsAtom } from "./use-thread-outbox"; +import { dispatchingQueuedMessageIdsAtom, editingQueuedMessageIdsAtom } from "./use-thread-outbox"; + +const sendingMessageIds = () => Object.keys(appAtomRegistry.get(dispatchingQueuedMessageIdsAtom)); import { createThreadOutboxDelivery } from "./threadOutboxDelivery"; -import { composerAttachmentUploadsAtom } from "./composer-attachment-upload-states"; import { workspaceConnections } from "./workspace"; const message = (overrides: Partial = {}): QueuedThreadMessage => ({ @@ -190,8 +193,8 @@ function setOperateGrant(environmentId: string, granted: boolean): void { function runDrain() { const settled = Promise.withResolvers(); - const release = appAtomRegistry.subscribe(dispatchingQueuedMessageIdAtom, (id) => { - if (id === null) settled.resolve(); + const release = appAtomRegistry.subscribe(dispatchingQueuedMessageIdsAtom, (ids) => { + if (Object.keys(ids).length === 0) settled.resolve(); }); startDelivery(); return settled.promise.finally(release); @@ -220,7 +223,7 @@ beforeEach(() => { state.familyMount.mockReset().mockReturnValue(() => {}); appAtomRegistry.set(state.threadShells, [state.thread]); appAtomRegistry.set(state.manager.queuedMessagesByThreadKeyAtom, {}); - appAtomRegistry.set(dispatchingQueuedMessageIdAtom, null); + appAtomRegistry.set(dispatchingQueuedMessageIdsAtom, {}); appAtomRegistry.set(editingQueuedMessageIdsAtom, {}); for (const command of [ state.start, @@ -361,7 +364,7 @@ describe("queued task operation access", () => { expect(state.modelUnavailable).toHaveBeenCalledOnce(); expect(state.prepare).not.toHaveBeenCalled(); expect(state.start).not.toHaveBeenCalled(); - expect(appAtomRegistry.get(dispatchingQueuedMessageIdAtom)).toBeNull(); + expect(sendingMessageIds()).toEqual([]); expect(remaining()).toEqual([queued]); }); @@ -378,17 +381,17 @@ describe("queued task operation access", () => { startDelivery(); appAtomRegistry.set(editingQueuedMessageIdsAtom, { [queued.messageId]: true }); await state.manager.enqueue(queued); - using confirm = vi.spyOn(state.manager, "confirmQueued"); const delivered = Promise.withResolvers(); - const release = appAtomRegistry.subscribe(dispatchingQueuedMessageIdAtom, (id) => { - if (id === null) delivered.resolve(); + const release = appAtomRegistry.subscribe(dispatchingQueuedMessageIdsAtom, (ids) => { + if (Object.keys(ids).length === 0) delivered.resolve(); }); - // Ending the edit wakes the drain through a registry notification, the path on which the - // mount's synchronous row change notifies the drain again mid-pass. + // Ending the edit wakes the thread through a registry notification, the path on which the + // mount's synchronous row change wakes it again while it is deciding. appAtomRegistry.set(editingQueuedMessageIdsAtom, {}); - expect(state.familyMount).toHaveBeenCalledOnce(); - expect(confirm).toHaveBeenCalledOnce(); await delivered.promise.finally(release); + expect(state.familyMount).toHaveBeenCalledOnce(); + // One fiber took the message: a second would have prepared it again. + expect(state.prepare).toHaveBeenCalledOnce(); expect(state.start).toHaveBeenCalledOnce(); expect(remaining()).toEqual([]); }); @@ -544,8 +547,8 @@ describe("queued task whose launch reply is lost", () => { async function fireRetry(): Promise { const settled = Promise.withResolvers(); - const release = appAtomRegistry.subscribe(dispatchingQueuedMessageIdAtom, (id) => { - if (id === null) settled.resolve(); + const release = appAtomRegistry.subscribe(dispatchingQueuedMessageIdsAtom, (ids) => { + if (Object.keys(ids).length === 0) settled.resolve(); }); vi.advanceTimersByTime(threadOutboxRetryDelayMs(1)); await settled.promise.finally(release); @@ -577,7 +580,7 @@ describe("queued task whose launch reply is lost", () => { await loseLaunchReply(queuedTask()); setOperateGrant("secondary", false); vi.advanceTimersByTime(threadOutboxRetryDelayMs(1)); - expect(appAtomRegistry.get(dispatchingQueuedMessageIdAtom)).toBeNull(); + expect(sendingMessageIds()).toEqual([]); expect(state.start).toHaveBeenCalledOnce(); expect(remaining()).toHaveLength(1); @@ -665,42 +668,87 @@ describe("compaction before a queued send", () => { describe("queued delivery timing", () => { /** Runs `change`, then resolves once the delivery it starts has settled. */ async function deliveryAfter(change: () => void): Promise { + const started = Promise.withResolvers(); const settled = Promise.withResolvers(); - const release = appAtomRegistry.subscribe(dispatchingQueuedMessageIdAtom, (id) => { - if (id === null) settled.resolve(); + const release = appAtomRegistry.subscribe(dispatchingQueuedMessageIdsAtom, (ids) => { + if (Object.keys(ids).length > 0) started.resolve(); + else settled.resolve(); }); change(); - expect(appAtomRegistry.get(dispatchingQueuedMessageIdAtom)).not.toBeNull(); + await started.promise; await settled.promise.finally(release); } - it("waits for the composer's upload of a sent attachment instead of uploading it again", async () => { + it("starts a thread's next message only after its interrupted send has finished", async () => { setOperateGrant("secondary", true); - const photo = { - id: "photo-1", - type: "image" as const, - name: "photo.png", - mimeType: "image/png", - sizeBytes: 3, - previewUri: "file:///photo.png", - fileUri: "file:///photo.png", - }; - appAtomRegistry.set(composerAttachmentUploadsAtom, { - "secondary:photo-1": { status: "uploading", progress: 0.5 }, + const firstStarted = Promise.withResolvers(); + const finishFirst = Promise.withResolvers>(); + state.start.mockImplementationOnce(async () => { + firstStarted.resolve(); + return finishFirst.promise; }); - state.cleanups.push(() => appAtomRegistry.set(composerAttachmentUploadsAtom, {})); - await state.manager.enqueue(message({ attachments: [photo] })); + const first = message(); + await state.manager.enqueue(first); startDelivery(); - expect(appAtomRegistry.get(dispatchingQueuedMessageIdAtom)).toBeNull(); + await firstStarted.promise; + // The queue empties under the running send, then the thread gets its next message. + await state.manager.remove(first); + const allDelivered = Promise.withResolvers(); + const release = appAtomRegistry.subscribe(state.manager.queuedMessagesByThreadKeyAtom, () => { + if (remaining().length === 0) allDelivered.resolve(); + }); + await state.manager.enqueue( + message({ messageId: MessageId.make("next"), commandId: CommandId.make("next-command") }), + ); + // The next message waits; only the interrupted send is still marked as sending. + expect(sendingMessageIds()).toEqual(["message"]); + expect(state.start).toHaveBeenCalledOnce(); + finishFirst.resolve(AsyncResult.success({})); + await allDelivered.promise.finally(release); + expect(state.start).toHaveBeenCalledTimes(2); + }); - await deliveryAfter(() => - appAtomRegistry.set(composerAttachmentUploadsAtom, { - "secondary:photo-1": { status: "ready" }, + it("sends another thread's message while one thread's send is still running", async () => { + setOperateGrant("secondary", true); + const other = { ...state.thread, id: "other" }; + appAtomRegistry.set(state.threadShells, [state.thread, other]); + const slowStarted = Promise.withResolvers(); + const finishSlow = Promise.withResolvers>(); + state.start.mockImplementation(async ({ input }: { input: { threadId: string } }) => { + if (input.threadId !== "thread") return AsyncResult.success({}); + slowStarted.resolve(); + return finishSlow.promise; + }); + /** Resolves once only these message ids are still queued. */ + const queuedBecomes = (ids: ReadonlyArray) => { + const reached = Promise.withResolvers(); + const release = appAtomRegistry.subscribe(state.manager.queuedMessagesByThreadKeyAtom, () => { + if ( + remaining() + .map((queued) => queued.messageId) + .join() === ids.join() + ) { + reached.resolve(); + } + }); + return reached.promise.finally(release); + }; + await state.manager.enqueue(message()); + startDelivery(); + await slowStarted.promise; + const otherDelivered = queuedBecomes(["message"]); + await state.manager.enqueue( + message({ + threadId: ThreadId.make("other"), + messageId: MessageId.make("other-message"), + commandId: CommandId.make("other-command"), }), ); - expect(state.prepare).toHaveBeenCalledOnce(); - expect(state.start).toHaveBeenCalledOnce(); - expect(remaining()).toEqual([]); + await otherDelivered; + const allDelivered = queuedBecomes([]); + finishSlow.resolve(AsyncResult.success({})); + await allDelivered; + expect(state.start).toHaveBeenCalledTimes(2); }); it("sends at once when its environment reconnects instead of waiting out the backoff", async () => { diff --git a/apps/mobile/src/state/threadOutboxDelivery.test.ts b/apps/mobile/src/state/threadOutboxDelivery.test.ts index b85ed8e4bf4cd..aa6e04c1867c8 100644 --- a/apps/mobile/src/state/threadOutboxDelivery.test.ts +++ b/apps/mobile/src/state/threadOutboxDelivery.test.ts @@ -3,6 +3,7 @@ import { pruneCaughtUpAcknowledgedThreadMessages, } from "./acknowledged-thread-messages"; import * as Effect from "effect/Effect"; +import * as Fiber from "effect/Fiber"; import { CommandId, EnvironmentId, @@ -11,7 +12,8 @@ import { ProviderInstanceId, ThreadId, } from "@t3tools/contracts"; -import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; +import { describe, expect, it } from "@effect/vitest"; +import { afterEach, beforeEach, vi } from "vite-plus/test"; import type { PreparedTurnAttachments } from "../lib/attachmentUpload"; @@ -121,7 +123,7 @@ vi.mock("./use-thread-outbox", async () => { const { Atom } = await import("effect/reactivity"); return { editingQueuedMessageIdsAtom: Atom.make>({}).pipe(Atom.keepAlive), - dispatchingQueuedMessageIdAtom: Atom.make(null).pipe(Atom.keepAlive), + dispatchingQueuedMessageIdsAtom: Atom.make({}).pipe(Atom.keepAlive), retryingQueuedMessageIdsAtom: Atom.make({}).pipe(Atom.keepAlive), threadOutboxShellStatusesAtom: Atom.make(new Map()), }; @@ -160,6 +162,7 @@ import { recoverFailedThreadDraft } from "./recover-failed-thread-draft"; import { editingQueuedMessageIdsAtom } from "./use-thread-outbox"; import { completeQueuedMessageDelivery, + type DeliveryRetry, prepareQueuedMessageAttachments, recoverEditedCreationAfterDelivery, removeAcknowledgedExistingThreadMessage, @@ -235,447 +238,522 @@ afterEach(() => { harness.prepareTurnAttachments.mockReset(); }); +/** True once a delivery step handled its message, false when it asks to retry. */ +const handled = (step: Effect.Effect) => + step.pipe(Effect.as(true), Effect.catchTags({ DeliveryRetry: () => Effect.succeed(false) })); + describe("thread outbox attachment preparation", () => { - it("keeps a denied message queued without starting its attachment upload", async () => { - const message = queuedMessage({ - messageId: "denied-queued-message", - text: "Keep this queued", - fileUri: "file:///documents/keep.pdf", - }); - await harness.manager.enqueue(message); - harness.prepareTurnAttachments.mockReturnValueOnce( - Effect.succeed({ + it.effect("keeps a denied message queued without starting its attachment upload", () => + Effect.gen(function* () { + const message = queuedMessage({ + messageId: "denied-queued-message", + text: "Keep this queued", + fileUri: "file:///documents/keep.pdf", + }); + yield* Effect.promise(() => harness.manager.enqueue(message)); + harness.prepareTurnAttachments.mockReturnValueOnce( + Effect.succeed({ + status: "ready", + attachments: [], + draftAttachments: message.attachments, + pendingAttachmentIds: [], + }), + ); + harness.canOperate = false; + expect(yield* prepareQueuedMessageAttachments(message)).toEqual({ + status: "abandoned", + }); + expect(harness.prepareTurnAttachments).not.toHaveBeenCalled(); + expect(remainingMessages()).toEqual([message]); + }), + ); + + it.effect("abandons reused uploads when an editor saves changed text during verification", () => + Effect.gen(function* () { + const message = withReusedFileUpload( + queuedMessage({ + messageId: "message-reused-upload-race", + text: "original text", + fileUri: "file:///documents/t3-composer-attachments/reused.pdf", + }), + "pending-reused-upload", + ); + const preparationStarted = Promise.withResolvers(); + const preparationBarrier = Promise.withResolvers(); + harness.prepareTurnAttachments.mockImplementationOnce(() => + Effect.promise(() => { + preparationStarted.resolve(); + return preparationBarrier.promise; + }), + ); + yield* Effect.promise(() => harness.manager.enqueue(message)); + appAtomRegistry.set(editingQueuedMessageIdsAtom, { [message.messageId]: true }); + + const preparation = yield* Effect.forkChild(prepareQueuedMessageAttachments(message), { + startImmediately: true, + }); + yield* Effect.promise(() => preparationStarted.promise); + const edited = { ...message, text: "saved editor text" }; + yield* Effect.promise(() => harness.manager.update(edited)); + appAtomRegistry.set(editingQueuedMessageIdsAtom, {}); + preparationBarrier.resolve({ status: "ready", attachments: [], draftAttachments: message.attachments, - pendingAttachmentIds: [], - }), - ); - harness.canOperate = false; - await expect(prepareQueuedMessageAttachments(message)).resolves.toEqual({ - status: "abandoned", - }); - expect(harness.prepareTurnAttachments).not.toHaveBeenCalled(); - expect(remainingMessages()).toEqual([message]); - }); - - it("abandons reused uploads when an editor saves changed text during verification", async () => { - const message = withReusedFileUpload( - queuedMessage({ - messageId: "message-reused-upload-race", - text: "original text", - fileUri: "file:///documents/t3-composer-attachments/reused.pdf", - }), - "pending-reused-upload", - ); - const preparationStarted = Promise.withResolvers(); - const preparationBarrier = Promise.withResolvers(); - harness.prepareTurnAttachments.mockImplementationOnce(() => - Effect.promise(() => { - preparationStarted.resolve(); - return preparationBarrier.promise; - }), - ); - await harness.manager.enqueue(message); - appAtomRegistry.set(editingQueuedMessageIdsAtom, { [message.messageId]: true }); - - const preparation = prepareQueuedMessageAttachments(message); - await preparationStarted.promise; - const edited = { ...message, text: "saved editor text" }; - await harness.manager.update(edited); - appAtomRegistry.set(editingQueuedMessageIdsAtom, {}); - preparationBarrier.resolve({ - status: "ready", - attachments: [], - draftAttachments: message.attachments, - pendingAttachmentIds: ["pending-reused-upload"], - }); + pendingAttachmentIds: ["pending-reused-upload"], + }); - await expect(preparation).resolves.toEqual({ status: "abandoned" }); - expect(remainingMessages()).toEqual([edited]); - }); + expect(yield* Fiber.join(preparation)).toEqual({ status: "abandoned" }); + expect(remainingMessages()).toEqual([edited]); + }), + ); - it("uses the known next revision after persisting uploaded references", async () => { - const message = queuedMessage({ - messageId: "message-new-upload-revision", - text: "upload this file", - fileUri: "file:///documents/t3-composer-attachments/new.pdf", - }); - const uploadedAttachments = message.attachments.map((attachment) => - attachment.type === "file" - ? { - ...attachment, - uploadedAttachmentId: "pending-new-upload", - uploadEnvironmentId: message.environmentId, - } - : attachment, - ); - harness.prepareTurnAttachments.mockImplementationOnce((input) => - Effect.gen(function* () { - expect(yield* input.persistUploadedReferences!(uploadedAttachments)).toBe("persisted"); - return { - status: "ready", - attachments: [], - draftAttachments: uploadedAttachments, - pendingAttachmentIds: ["pending-new-upload"], - } as const; - }), - ); - await harness.manager.enqueue(message); - const revision = harness.manager.revisionOf(message.messageId); + it.effect("uses the known next revision after persisting uploaded references", () => + Effect.gen(function* () { + const message = queuedMessage({ + messageId: "message-new-upload-revision", + text: "upload this file", + fileUri: "file:///documents/t3-composer-attachments/new.pdf", + }); + const uploadedAttachments = message.attachments.map((attachment) => + attachment.type === "file" + ? { + ...attachment, + uploadedAttachmentId: "pending-new-upload", + uploadEnvironmentId: message.environmentId, + } + : attachment, + ); + harness.prepareTurnAttachments.mockImplementationOnce((input) => + Effect.gen(function* () { + expect(yield* input.persistUploadedReferences!(uploadedAttachments)).toBe("persisted"); + return { + status: "ready", + attachments: [], + draftAttachments: uploadedAttachments, + pendingAttachmentIds: ["pending-new-upload"], + } as const; + }), + ); + yield* Effect.promise(() => harness.manager.enqueue(message)); + const revision = harness.manager.revisionOf(message.messageId); - const result = await prepareQueuedMessageAttachments(message); + const result = yield* prepareQueuedMessageAttachments(message); - expect(result).toMatchObject({ - status: "ready", - persistedMessage: { attachments: uploadedAttachments }, - deliveryRevision: revision + 1, - }); - expect(harness.manager.revisionOf(message.messageId)).toBe(revision + 1); - }); + expect(result).toMatchObject({ + status: "ready", + persistedMessage: { attachments: uploadedAttachments }, + deliveryRevision: revision + 1, + }); + expect(harness.manager.revisionOf(message.messageId)).toBe(revision + 1); + }), + ); - it("does not prepare a payload that was already replaced", async () => { - const message = queuedMessage({ messageId: "message-stale-before-upload", text: "old" }); - await harness.manager.enqueue(message); - const edited = { ...message, text: "new" }; - await harness.manager.update(edited); + it.effect("does not prepare a payload that was already replaced", () => + Effect.gen(function* () { + const message = queuedMessage({ messageId: "message-stale-before-upload", text: "old" }); + yield* Effect.promise(() => harness.manager.enqueue(message)); + const edited = { ...message, text: "new" }; + yield* Effect.promise(() => harness.manager.update(edited)); - await expect(prepareQueuedMessageAttachments(message)).resolves.toEqual({ - status: "abandoned", - }); - expect(harness.prepareTurnAttachments).not.toHaveBeenCalled(); - expect(remainingMessages()).toEqual([edited]); - }); + expect(yield* prepareQueuedMessageAttachments(message)).toEqual({ + status: "abandoned", + }); + expect(harness.prepareTurnAttachments).not.toHaveBeenCalled(); + expect(remainingMessages()).toEqual([edited]); + }), + ); }); describe("thread outbox drain delivery cleanup", () => { - it("drops caught-up acknowledged rows at the next delivery without subscribing to the thread", async () => { - const first = queuedMessage({ messageId: "awaiting-echo", text: "Delivered" }); - const second = queuedMessage({ messageId: "next-delivery", text: "Also delivered" }); - await harness.manager.enqueue(first); - await harness.manager.enqueue(second); - - await expect( - completeQueuedMessageDelivery(first, harness.manager.revisionOf(first.messageId), 7), - ).resolves.toBe("removed"); - expect(appAtomRegistry.get(acknowledgedThreadMessagesAtom)).toEqual([ - { ...first, sequence: 7 }, - ]); - - // The thread applied the message, but its bounded window does not hold it. - appAtomRegistry.set(harness.threadState, { - data: Option.some({ messages: [{ id: "unrelated" }] }), - sequence: 7, - }); - expect(appAtomRegistry.get(acknowledgedThreadMessagesAtom)).toHaveLength(1); - - await expect( - completeQueuedMessageDelivery(second, harness.manager.revisionOf(second.messageId), 9), - ).resolves.toBe("removed"); - expect(appAtomRegistry.get(acknowledgedThreadMessagesAtom)).toEqual([ - { ...second, sequence: 9 }, - ]); - }); - - it("drops an acknowledged row once the open thread catches up, so a reload cannot resurrect it", async () => { - const message = queuedMessage({ messageId: "paged-out-later", text: "Delivered" }); - await harness.manager.enqueue(message); - await completeQueuedMessageDelivery(message, harness.manager.revisionOf(message.messageId), 7); - - pruneCaughtUpAcknowledgedThreadMessages( - scopedThreadKey(message.environmentId, message.threadId), - { - sequence: 6, - messages: [], - }, - ); - expect(appAtomRegistry.get(acknowledgedThreadMessagesAtom)).toEqual([ - { ...message, sequence: 7 }, - ]); - - pruneCaughtUpAcknowledgedThreadMessages( - scopedThreadKey(message.environmentId, message.threadId), - { - sequence: 7, - messages: [], - }, - ); - expect(appAtomRegistry.get(acknowledgedThreadMessagesAtom)).toEqual([]); - }); + it.effect( + "drops caught-up acknowledged rows at the next delivery without subscribing to the thread", + () => + Effect.gen(function* () { + const first = queuedMessage({ messageId: "awaiting-echo", text: "Delivered" }); + const second = queuedMessage({ messageId: "next-delivery", text: "Also delivered" }); + yield* Effect.promise(() => harness.manager.enqueue(first)); + yield* Effect.promise(() => harness.manager.enqueue(second)); + + expect( + yield* completeQueuedMessageDelivery( + first, + harness.manager.revisionOf(first.messageId), + 7, + ), + ).toBe("removed"); + expect(appAtomRegistry.get(acknowledgedThreadMessagesAtom)).toEqual([ + { ...first, sequence: 7 }, + ]); + + // The thread applied the message, but its bounded window does not hold it. + appAtomRegistry.set(harness.threadState, { + data: Option.some({ messages: [{ id: "unrelated" }] }), + sequence: 7, + }); + expect(appAtomRegistry.get(acknowledgedThreadMessagesAtom)).toHaveLength(1); + + expect( + yield* completeQueuedMessageDelivery( + second, + harness.manager.revisionOf(second.messageId), + 9, + ), + ).toBe("removed"); + expect(appAtomRegistry.get(acknowledgedThreadMessagesAtom)).toEqual([ + { ...second, sequence: 9 }, + ]); + }), + ); - it("removes an acknowledged outbox item even when the sign-out archive write fails", async () => { - const message = queuedMessage({ messageId: "archive-write-failure", text: "Delivered" }); - await harness.manager.enqueue(message); - await composerDrafts.archiveCloudComposerDrafts("account-a", new Set([message.environmentId]), [ - message.environmentId, - ]); - harness.draftFile.setWriteError(new Error("Draft storage unavailable")); - - await expect( - completeQueuedMessageDelivery( - message, - harness.manager.revisionOf(message.messageId), - undefined, - ), - ).resolves.toBe("removed"); - expect(remainingMessages()).toEqual([]); + it.effect( + "drops an acknowledged row once the open thread catches up, so a reload cannot resurrect it", + () => + Effect.gen(function* () { + const message = queuedMessage({ messageId: "paged-out-later", text: "Delivered" }); + yield* Effect.promise(() => harness.manager.enqueue(message)); + yield* completeQueuedMessageDelivery( + message, + harness.manager.revisionOf(message.messageId), + 7, + ); + + pruneCaughtUpAcknowledgedThreadMessages( + scopedThreadKey(message.environmentId, message.threadId), + { + sequence: 6, + messages: [], + }, + ); + expect(appAtomRegistry.get(acknowledgedThreadMessagesAtom)).toEqual([ + { ...message, sequence: 7 }, + ]); - harness.draftFile.setWriteError(null); - await composerDrafts.flushComposerDrafts(); - appAtomRegistry.set(composerDrafts.composerCloudDraftsAtom, { accountId: null, signedOut: {} }); - composerDrafts.resetComposerDraftsLoadState(); - await composerDrafts.restoreCloudComposerDrafts("account-a"); - expect(remainingMessages()).toEqual([]); - }); + pruneCaughtUpAcknowledgedThreadMessages( + scopedThreadKey(message.environmentId, message.threadId), + { + sequence: 7, + messages: [], + }, + ); + expect(appAtomRegistry.get(acknowledgedThreadMessagesAtom)).toEqual([]); + }), + ); - it.each([false, true])( - "does not restore a message delivered after the sign-out snapshot (outbox already cleared: %s)", - async (cleared) => { - const message = queuedMessage({ - messageId: "delivered-during-sign-out", - text: "Already delivered", - }); - await harness.manager.enqueue(message); - const deliveryRevision = harness.manager.revisionOf(message.messageId); - await composerDrafts.archiveCloudComposerDrafts( - "account-a", - new Set([message.environmentId]), - [message.environmentId], + it.effect("removes an acknowledged outbox item even when the sign-out archive write fails", () => + Effect.gen(function* () { + const message = queuedMessage({ messageId: "archive-write-failure", text: "Delivered" }); + yield* Effect.promise(() => harness.manager.enqueue(message)); + yield* Effect.promise(() => + composerDrafts.archiveCloudComposerDrafts("account-a", new Set([message.environmentId]), [ + message.environmentId, + ]), ); - expect( - appAtomRegistry.get(composerDrafts.composerCloudDraftsAtom).signedOut["account-a"] - ?.queuedMessages, - ).toEqual([message]); + harness.draftFile.setWriteError(new Error("Draft storage unavailable")); - if (cleared) await harness.manager.clearEnvironment(message.environmentId); - await expect( - completeQueuedMessageDelivery(message, deliveryRevision, undefined), - ).resolves.toBe(cleared ? "edited" : "removed"); + expect( + yield* completeQueuedMessageDelivery( + message, + harness.manager.revisionOf(message.messageId), + undefined, + ), + ).toBe("removed"); + expect(remainingMessages()).toEqual([]); + harness.draftFile.setWriteError(null); + yield* Effect.promise(() => composerDrafts.flushComposerDrafts()); appAtomRegistry.set(composerDrafts.composerCloudDraftsAtom, { accountId: null, signedOut: {}, }); composerDrafts.resetComposerDraftsLoadState(); - await composerDrafts.restoreCloudComposerDrafts("account-a"); + yield* Effect.promise(() => composerDrafts.restoreCloudComposerDrafts("account-a")); expect(remainingMessages()).toEqual([]); - }, + }), ); - it("preserves an archived edit when an older payload finishes delivery", async () => { - const message = queuedMessage({ messageId: "edited-during-sign-out", text: "Original" }); - await harness.manager.enqueue(message); - const deliveryRevision = harness.manager.revisionOf(message.messageId); - const edited = { ...message, text: "Keep this edit" }; - await harness.manager.update(edited); - await composerDrafts.archiveCloudComposerDrafts("account-a", new Set([message.environmentId]), [ - message.environmentId, - ]); - await harness.manager.clearEnvironment(message.environmentId); - await expect(completeQueuedMessageDelivery(message, deliveryRevision, undefined)).resolves.toBe( - "edited", - ); - await composerDrafts.restoreCloudComposerDrafts("account-a"); - expect(remainingMessages()).toEqual([edited]); - }); - - it("retries only cleanup after an acknowledged send removal fails", async () => { - const message = queuedMessage({ messageId: "message-acknowledged", text: "delivered" }); - const acknowledged = new Set([message.messageId]); - harness.removeOutboxMessage.mockRejectedValueOnce(new Error("storage unavailable")); - await harness.manager.enqueue(message); - - await expect(removeAcknowledgedExistingThreadMessage(message, acknowledged)).resolves.toBe( - false, - ); - expect(remainingMessages()).toEqual([message]); - expect(acknowledged).toEqual(new Set([message.messageId])); - - await expect(removeAcknowledgedExistingThreadMessage(message, acknowledged)).resolves.toBe( - true, - ); - expect(remainingMessages()).toEqual([]); - expect(acknowledged).toEqual(new Set()); - }); + it.effect.each([false, true])( + "does not restore a message delivered after the sign-out snapshot (outbox already cleared: %s)", + (cleared) => + Effect.gen(function* () { + const message = queuedMessage({ + messageId: "delivered-during-sign-out", + text: "Already delivered", + }); + yield* Effect.promise(() => harness.manager.enqueue(message)); + const deliveryRevision = harness.manager.revisionOf(message.messageId); + yield* Effect.promise(() => + composerDrafts.archiveCloudComposerDrafts("account-a", new Set([message.environmentId]), [ + message.environmentId, + ]), + ); + expect( + appAtomRegistry.get(composerDrafts.composerCloudDraftsAtom).signedOut["account-a"] + ?.queuedMessages, + ).toEqual([message]); + + if (cleared) { + yield* Effect.promise(() => harness.manager.clearEnvironment(message.environmentId)); + } + expect(yield* completeQueuedMessageDelivery(message, deliveryRevision, undefined)).toBe( + cleared ? "edited" : "removed", + ); + + appAtomRegistry.set(composerDrafts.composerCloudDraftsAtom, { + accountId: null, + signedOut: {}, + }); + composerDrafts.resetComposerDraftsLoadState(); + yield* Effect.promise(() => composerDrafts.restoreCloudComposerDrafts("account-a")); + expect(remainingMessages()).toEqual([]); + }), + ); - it("keeps an edited message and its files when delivery cleanup loses the revision race", async () => { - const message = queuedMessage({ - messageId: "message-edited", - text: "original", - fileUri: "file:///documents/t3-composer-attachments/report.pdf", - }); - await harness.manager.enqueue(message); - const deliveryRevision = harness.manager.revisionOf(message.messageId); - const edited = { ...message, text: "edited while the turn delivered" }; - await harness.manager.update(edited); + it.effect("preserves an archived edit when an older payload finishes delivery", () => + Effect.gen(function* () { + const message = queuedMessage({ messageId: "edited-during-sign-out", text: "Original" }); + yield* Effect.promise(() => harness.manager.enqueue(message)); + const deliveryRevision = harness.manager.revisionOf(message.messageId); + const edited = { ...message, text: "Keep this edit" }; + yield* Effect.promise(() => harness.manager.update(edited)); + yield* Effect.promise(() => + composerDrafts.archiveCloudComposerDrafts("account-a", new Set([message.environmentId]), [ + message.environmentId, + ]), + ); + yield* Effect.promise(() => harness.manager.clearEnvironment(message.environmentId)); + expect(yield* completeQueuedMessageDelivery(message, deliveryRevision, undefined)).toBe( + "edited", + ); + yield* Effect.promise(() => composerDrafts.restoreCloudComposerDrafts("account-a")); + expect(remainingMessages()).toEqual([edited]); + }), + ); - await expect(completeQueuedMessageDelivery(message, deliveryRevision, undefined)).resolves.toBe( - "edited", - ); + it.effect("retries only cleanup after an acknowledged send removal fails", () => + Effect.gen(function* () { + const message = queuedMessage({ messageId: "message-acknowledged", text: "delivered" }); + const acknowledged = new Set([message.messageId]); + harness.removeOutboxMessage.mockRejectedValueOnce(new Error("storage unavailable")); + yield* Effect.promise(() => harness.manager.enqueue(message)); - expect(remainingMessages()).toEqual([edited]); - expect(harness.removePersistedFile).not.toHaveBeenCalled(); - }); - - it("removes the delivered message when no edit was accepted", async () => { - const message = queuedMessage({ messageId: "message-clean", text: "hello" }); - await harness.manager.enqueue(message); - const deliveryRevision = harness.manager.revisionOf(message.messageId); + expect(yield* handled(removeAcknowledgedExistingThreadMessage(message, acknowledged))).toBe( + false, + ); + expect(remainingMessages()).toEqual([message]); + expect(acknowledged).toEqual(new Set([message.messageId])); - await expect(completeQueuedMessageDelivery(message, deliveryRevision, undefined)).resolves.toBe( - "removed", - ); + expect(yield* handled(removeAcknowledgedExistingThreadMessage(message, acknowledged))).toBe( + true, + ); + expect(remainingMessages()).toEqual([]); + expect(acknowledged).toEqual(new Set()); + }), + ); - expect(remainingMessages()).toEqual([]); - expect(appAtomRegistry.get(acknowledgedThreadMessagesAtom)).toEqual([message]); - }); + it.effect( + "keeps an edited message and its files when delivery cleanup loses the revision race", + () => + Effect.gen(function* () { + const message = queuedMessage({ + messageId: "message-edited", + text: "original", + fileUri: "file:///documents/t3-composer-attachments/report.pdf", + }); + yield* Effect.promise(() => harness.manager.enqueue(message)); + const deliveryRevision = harness.manager.revisionOf(message.messageId); + const edited = { ...message, text: "edited while the turn delivered" }; + yield* Effect.promise(() => harness.manager.update(edited)); + + expect(yield* completeQueuedMessageDelivery(message, deliveryRevision, undefined)).toBe( + "edited", + ); + + expect(remainingMessages()).toEqual([edited]); + expect(harness.removePersistedFile).not.toHaveBeenCalled(); + }), + ); - it("keeps a delivered message when its editor opens during storage removal", async () => { - const message = queuedMessage({ - messageId: "message-editor-removal-race", - text: "keep editor changes", - fileUri: "file:///documents/t3-composer-attachments/editor-race.pdf", - }); - const removeStarted = Promise.withResolvers(); - const removeBarrier = Promise.withResolvers(); - harness.removeOutboxMessage.mockImplementationOnce(async () => { - removeStarted.resolve(); - await removeBarrier.promise; - }); - await harness.manager.enqueue(message); - const deliveryRevision = harness.manager.revisionOf(message.messageId); + it.effect("removes the delivered message when no edit was accepted", () => + Effect.gen(function* () { + const message = queuedMessage({ messageId: "message-clean", text: "hello" }); + yield* Effect.promise(() => harness.manager.enqueue(message)); + const deliveryRevision = harness.manager.revisionOf(message.messageId); - const cleanup = completeQueuedMessageDelivery(message, deliveryRevision, undefined); - await removeStarted.promise; - appAtomRegistry.set(editingQueuedMessageIdsAtom, { [message.messageId]: true }); - removeBarrier.resolve(); + expect(yield* completeQueuedMessageDelivery(message, deliveryRevision, undefined)).toBe( + "removed", + ); - await expect(cleanup).resolves.toBe("edited"); - expect(remainingMessages()).toEqual([message]); - expect(harness.removePersistedFile).not.toHaveBeenCalled(); - }); -}); + expect(remainingMessages()).toEqual([]); + expect(appAtomRegistry.get(acknowledgedThreadMessagesAtom)).toEqual([message]); + }), + ); -describe("thread outbox delivered creation recovery", () => { - it("keeps an edit accepted while the older payload is persisted to the draft", async () => { - const message = queuedMessage({ - messageId: "message-recovery-race", - text: "original queued text", - fileUri: "file:///documents/t3-composer-attachments/report.pdf", - }); - const originalMergeComposerDraftContent = composerDrafts.mergeComposerDraftContent; - const mergeCompleted = Promise.withResolvers(); - const releaseRecovery = Promise.withResolvers(); - const mergeSpy = vi - .spyOn(composerDrafts, "mergeComposerDraftContent") - .mockImplementation(async (draftKey, content) => { - const result = await originalMergeComposerDraftContent(draftKey, content); - mergeCompleted.resolve(); - await releaseRecovery.promise; - return result; + it.effect("keeps a delivered message when its editor opens during storage removal", () => + Effect.gen(function* () { + const message = queuedMessage({ + messageId: "message-editor-removal-race", + text: "keep editor changes", + fileUri: "file:///documents/t3-composer-attachments/editor-race.pdf", }); - - try { - await harness.manager.enqueue(message); - const recovery = recoverEditedCreationAfterDelivery(message); - await mergeCompleted.promise; - - const newer = { ...message, text: "edited while recovery persisted the draft" }; - await harness.manager.update(newer); - - releaseRecovery.resolve(); - await expect(recovery).resolves.toBe(false); - - expect(remainingMessages()).toEqual([newer]); - expect( - composerDrafts.getComposerDraftSnapshot(`${message.environmentId}:${message.threadId}`), - ).toMatchObject({ text: message.text, attachments: [] }); - expect(harness.removePersistedFile).not.toHaveBeenCalled(); - } finally { - releaseRecovery.resolve(); - mergeSpy.mockRestore(); - } - }); - - it("leaves recovery to an editor that opens while the draft persists", async () => { - const message = queuedMessage({ - messageId: "message-recovery-editor", - text: "recover this text", - fileUri: "file:///documents/t3-composer-attachments/editor.pdf", - }); - const originalMergeComposerDraftContent = composerDrafts.mergeComposerDraftContent; - const mergeCompleted = Promise.withResolvers(); - const releaseRecovery = Promise.withResolvers(); - const mergeSpy = vi - .spyOn(composerDrafts, "mergeComposerDraftContent") - .mockImplementation(async (draftKey, content) => { - const result = await originalMergeComposerDraftContent(draftKey, content); - mergeCompleted.resolve(); - await releaseRecovery.promise; - return result; + const removeStarted = Promise.withResolvers(); + const removeBarrier = Promise.withResolvers(); + harness.removeOutboxMessage.mockImplementationOnce(async () => { + removeStarted.resolve(); + await removeBarrier.promise; }); + yield* Effect.promise(() => harness.manager.enqueue(message)); + const deliveryRevision = harness.manager.revisionOf(message.messageId); - try { - await harness.manager.enqueue(message); - const recovery = recoverEditedCreationAfterDelivery(message); - await mergeCompleted.promise; + const cleanup = yield* Effect.forkChild( + completeQueuedMessageDelivery(message, deliveryRevision, undefined), + { startImmediately: true }, + ); + yield* Effect.promise(() => removeStarted.promise); appAtomRegistry.set(editingQueuedMessageIdsAtom, { [message.messageId]: true }); + removeBarrier.resolve(); - releaseRecovery.resolve(); - await expect(recovery).resolves.toBe(true); - + expect(yield* Fiber.join(cleanup)).toBe("edited"); expect(remainingMessages()).toEqual([message]); - expect( - composerDrafts.getComposerDraftSnapshot(`${message.environmentId}:${message.threadId}`), - ).toMatchObject({ text: message.text, attachments: [] }); expect(harness.removePersistedFile).not.toHaveBeenCalled(); - } finally { - releaseRecovery.resolve(); - mergeSpy.mockRestore(); - } - }); - - it("retries a failed removal without duplicating recovered draft content", async () => { - const message = queuedMessage({ - messageId: "message-recovery-removal", - text: "recover once", - fileUri: "file:///documents/t3-composer-attachments/retry.pdf", - }); - const draftKey = `${message.environmentId}:${message.threadId}`; - const removeSpy = vi - .spyOn(harness.manager, "remove") - .mockRejectedValueOnce(new Error("storage unavailable")); + }), + ); +}); - try { - await harness.manager.enqueue(message); +describe("thread outbox delivered creation recovery", () => { + it.effect("keeps an edit accepted while the older payload is persisted to the draft", () => + Effect.gen(function* () { + const message = queuedMessage({ + messageId: "message-recovery-race", + text: "original queued text", + fileUri: "file:///documents/t3-composer-attachments/report.pdf", + }); + const originalMergeComposerDraftContent = composerDrafts.mergeComposerDraftContent; + const mergeCompleted = Promise.withResolvers(); + const releaseRecovery = Promise.withResolvers(); + const mergeSpy = vi + .spyOn(composerDrafts, "mergeComposerDraftContent") + .mockImplementation(async (draftKey, content) => { + const result = await originalMergeComposerDraftContent(draftKey, content); + mergeCompleted.resolve(); + await releaseRecovery.promise; + return result; + }); + + try { + yield* Effect.promise(() => harness.manager.enqueue(message)); + const recovery = yield* Effect.forkChild( + handled(recoverEditedCreationAfterDelivery(message)), + { startImmediately: true }, + ); + yield* Effect.promise(() => mergeCompleted.promise); + + const newer = { ...message, text: "edited while recovery persisted the draft" }; + yield* Effect.promise(() => harness.manager.update(newer)); + + releaseRecovery.resolve(); + expect(yield* Fiber.join(recovery)).toBe(false); + + expect(remainingMessages()).toEqual([newer]); + expect( + composerDrafts.getComposerDraftSnapshot(`${message.environmentId}:${message.threadId}`), + ).toMatchObject({ text: message.text, attachments: [] }); + expect(harness.removePersistedFile).not.toHaveBeenCalled(); + } finally { + releaseRecovery.resolve(); + mergeSpy.mockRestore(); + } + }), + ); - await expect(recoverEditedCreationAfterDelivery(message)).resolves.toBe(false); - expect(remainingMessages()).toEqual([message]); + it.effect("leaves recovery to an editor that opens while the draft persists", () => + Effect.gen(function* () { + const message = queuedMessage({ + messageId: "message-recovery-editor", + text: "recover this text", + fileUri: "file:///documents/t3-composer-attachments/editor.pdf", + }); + const originalMergeComposerDraftContent = composerDrafts.mergeComposerDraftContent; + const mergeCompleted = Promise.withResolvers(); + const releaseRecovery = Promise.withResolvers(); + const mergeSpy = vi + .spyOn(composerDrafts, "mergeComposerDraftContent") + .mockImplementation(async (draftKey, content) => { + const result = await originalMergeComposerDraftContent(draftKey, content); + mergeCompleted.resolve(); + await releaseRecovery.promise; + return result; + }); + + try { + yield* Effect.promise(() => harness.manager.enqueue(message)); + const recovery = yield* Effect.forkChild( + handled(recoverEditedCreationAfterDelivery(message)), + { startImmediately: true }, + ); + yield* Effect.promise(() => mergeCompleted.promise); + appAtomRegistry.set(editingQueuedMessageIdsAtom, { [message.messageId]: true }); - await expect(recoverEditedCreationAfterDelivery(message)).resolves.toBe(true); + releaseRecovery.resolve(); + expect(yield* Fiber.join(recovery)).toBe(true); + + expect(remainingMessages()).toEqual([message]); + expect( + composerDrafts.getComposerDraftSnapshot(`${message.environmentId}:${message.threadId}`), + ).toMatchObject({ text: message.text, attachments: [] }); + expect(harness.removePersistedFile).not.toHaveBeenCalled(); + } finally { + releaseRecovery.resolve(); + mergeSpy.mockRestore(); + } + }), + ); - const draft = composerDrafts.getComposerDraftSnapshot(draftKey); - expect(draft.text).toBe(message.text); - expect(draft.attachments).toEqual(message.attachments); - expect(remainingMessages()).toEqual([]); - expect(harness.removePersistedFile).not.toHaveBeenCalled(); - } finally { - removeSpy.mockRestore(); - } - }); + it.effect("retries a failed removal without duplicating recovered draft content", () => + Effect.gen(function* () { + const message = queuedMessage({ + messageId: "message-recovery-removal", + text: "recover once", + fileUri: "file:///documents/t3-composer-attachments/retry.pdf", + }); + const draftKey = `${message.environmentId}:${message.threadId}`; + const removeSpy = vi + .spyOn(harness.manager, "remove") + .mockRejectedValueOnce(new Error("storage unavailable")); + + try { + yield* Effect.promise(() => harness.manager.enqueue(message)); + + expect(yield* handled(recoverEditedCreationAfterDelivery(message))).toBe(false); + expect(remainingMessages()).toEqual([message]); + + expect(yield* handled(recoverEditedCreationAfterDelivery(message))).toBe(true); + + const draft = composerDrafts.getComposerDraftSnapshot(draftKey); + expect(draft.text).toBe(message.text); + expect(draft.attachments).toEqual(message.attachments); + expect(remainingMessages()).toEqual([]); + expect(harness.removePersistedFile).not.toHaveBeenCalled(); + } finally { + removeSpy.mockRestore(); + } + }), + ); - it("keeps the queue entry when the recovered draft cannot persist", async () => { - const message = queuedMessage({ - messageId: "message-recovery-persistence", - text: "recover after persistence returns", - }); - await harness.manager.enqueue(message); - harness.draftFile.setWriteError(new Error("disk full")); + it.effect("keeps the queue entry when the recovered draft cannot persist", () => + Effect.gen(function* () { + const message = queuedMessage({ + messageId: "message-recovery-persistence", + text: "recover after persistence returns", + }); + yield* Effect.promise(() => harness.manager.enqueue(message)); + harness.draftFile.setWriteError(new Error("disk full")); - await expect(recoverEditedCreationAfterDelivery(message)).resolves.toBe(false); + expect(yield* handled(recoverEditedCreationAfterDelivery(message))).toBe(false); - expect(remainingMessages()).toEqual([message]); - }); + expect(remainingMessages()).toEqual([message]); + }), + ); }); describe("thread outbox recovery rollback", () => { @@ -728,111 +806,121 @@ describe("thread outbox recovery rollback", () => { ); }); - it("restores a rejected new task as its own draft for the project", async () => { - const message: QueuedThreadMessage = { - ...queuedMessage({ messageId: "message-creation-restore", text: "new task text" }), - modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.6-sol" }, - creation: { - projectId: ProjectId.make("project-1"), - workspaceMode: "local", - branch: null, - worktreePath: null, - }, - }; - await harness.manager.enqueue(message); - - await expect(restoreRejectedQueuedMessage(message, "rejected by server")).resolves.toBe( - "restored", - ); + it.effect("restores a rejected new task as its own draft for the project", () => + Effect.gen(function* () { + const message: QueuedThreadMessage = { + ...queuedMessage({ messageId: "message-creation-restore", text: "new task text" }), + modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.6-sol" }, + creation: { + projectId: ProjectId.make("project-1"), + workspaceMode: "local", + branch: null, + worktreePath: null, + }, + }; + yield* Effect.promise(() => harness.manager.enqueue(message)); + + expect(yield* restoreRejectedQueuedMessage(message, "rejected by server")).toBe("restored"); - expect( - composerDrafts.getComposerDraftSnapshot(`new-task:restored-${message.messageId}`), - ).toMatchObject({ - text: message.text, - attachments: message.attachments, - modelSelection: message.modelSelection, - project: { - environmentId: message.environmentId, - projectId: message.creation!.projectId, - createdAt: message.createdAt, - }, - }); - expect(remainingMessages()).toEqual([]); - expect(appAtomRegistry.get(threadComposerErrorsAtom)).toEqual({}); - expect( - appAtomRegistry.get(pendingThreadCreationOutcomesAtom)[ - `${message.environmentId}:${message.threadId}` - ], - ).toEqual({ kind: "failed", message, reason: "rejected by server" }); - }); + expect( + composerDrafts.getComposerDraftSnapshot(`new-task:restored-${message.messageId}`), + ).toMatchObject({ + text: message.text, + attachments: message.attachments, + modelSelection: message.modelSelection, + project: { + environmentId: message.environmentId, + projectId: message.creation!.projectId, + createdAt: message.createdAt, + }, + }); + expect(remainingMessages()).toEqual([]); + expect(appAtomRegistry.get(threadComposerErrorsAtom)).toEqual({}); + expect( + appAtomRegistry.get(pendingThreadCreationOutcomesAtom)[ + `${message.environmentId}:${message.threadId}` + ], + ).toEqual({ kind: "failed", message, reason: "rejected by server" }); + }), + ); - it("does not record a creation outcome for a rejected follow-up message", async () => { - const message = queuedMessage({ messageId: "message-followup-restore", text: "follow up" }); - await harness.manager.enqueue(message); + it.effect("does not record a creation outcome for a rejected follow-up message", () => + Effect.gen(function* () { + const message = queuedMessage({ messageId: "message-followup-restore", text: "follow up" }); + yield* Effect.promise(() => harness.manager.enqueue(message)); - await expect(restoreRejectedQueuedMessage(message, "rejected")).resolves.toBe("restored"); + expect(yield* restoreRejectedQueuedMessage(message, "rejected")).toBe("restored"); - expect(appAtomRegistry.get(pendingThreadCreationOutcomesAtom)).toEqual({}); - expect( - appAtomRegistry.get(threadComposerErrorsAtom)[`${message.environmentId}:${message.threadId}`], - ).toEqual({ message: "rejected", messageId: message.messageId }); - }); + expect(appAtomRegistry.get(pendingThreadCreationOutcomesAtom)).toEqual({}); + expect( + appAtomRegistry.get(threadComposerErrorsAtom)[ + `${message.environmentId}:${message.threadId}` + ], + ).toEqual({ message: "rejected", messageId: message.messageId }); + }), + ); - it("leaves no error when the restored text is resent before recovery finishes", async () => { - const message = queuedMessage({ messageId: "message-resent", text: "resend me" }); - const threadKey = `${message.environmentId}:${message.threadId}`; - await harness.manager.enqueue(message); - const unsubscribe = appAtomRegistry.subscribe(composerDrafts.composerDraftsAtom, (drafts) => { - if (drafts[threadKey]?.text === "resend me") { - unsubscribe(); - clearThreadComposerError(threadKey); - void composerDrafts.clearComposerDraftContent(threadKey); - } - }); + it.effect("leaves no error when the restored text is resent before recovery finishes", () => + Effect.gen(function* () { + const message = queuedMessage({ messageId: "message-resent", text: "resend me" }); + const threadKey = `${message.environmentId}:${message.threadId}`; + yield* Effect.promise(() => harness.manager.enqueue(message)); + const unsubscribe = appAtomRegistry.subscribe(composerDrafts.composerDraftsAtom, (drafts) => { + if (drafts[threadKey]?.text === "resend me") { + unsubscribe(); + clearThreadComposerError(threadKey); + void composerDrafts.clearComposerDraftContent(threadKey); + } + }); - await expect(restoreRejectedQueuedMessage(message, "rejected")).resolves.toBe("restored"); + expect(yield* restoreRejectedQueuedMessage(message, "rejected")).toBe("restored"); - expect(appAtomRegistry.get(threadComposerErrorsAtom)).toEqual({}); - }); + expect(appAtomRegistry.get(threadComposerErrorsAtom)).toEqual({}); + }), + ); - it("withdraws the error when an edit makes the recovery back out", async () => { - const message = queuedMessage({ messageId: "message-edited-mid-recovery", text: "edit me" }); - const threadKey = `${message.environmentId}:${message.threadId}`; - await harness.manager.enqueue(message); - const unsubscribe = appAtomRegistry.subscribe(composerDrafts.composerDraftsAtom, (drafts) => { - if (drafts[threadKey]?.text === "edit me") { - unsubscribe(); - appAtomRegistry.set(editingQueuedMessageIdsAtom, { [message.messageId]: true }); - } - }); + it.effect("withdraws the error when an edit makes the recovery back out", () => + Effect.gen(function* () { + const message = queuedMessage({ messageId: "message-edited-mid-recovery", text: "edit me" }); + const threadKey = `${message.environmentId}:${message.threadId}`; + yield* Effect.promise(() => harness.manager.enqueue(message)); + const unsubscribe = appAtomRegistry.subscribe(composerDrafts.composerDraftsAtom, (drafts) => { + if (drafts[threadKey]?.text === "edit me") { + unsubscribe(); + appAtomRegistry.set(editingQueuedMessageIdsAtom, { [message.messageId]: true }); + } + }); - await expect(restoreRejectedQueuedMessage(message, "rejected")).resolves.toBe("deferred"); + expect(yield* restoreRejectedQueuedMessage(message, "rejected")).toBe("deferred"); - expect(appAtomRegistry.get(threadComposerErrorsAtom)).toEqual({}); - expect(remainingMessages()).toEqual([message]); - }); + expect(appAtomRegistry.get(threadComposerErrorsAtom)).toEqual({}); + expect(remainingMessages()).toEqual([message]); + }), + ); - it("rolls a failed recovery merge back so the retry cannot duplicate the text", async () => { - const message = queuedMessage({ messageId: "message-restore", text: "queued text" }); - const draftKey = `${message.environmentId}:${message.threadId}`; - appAtomRegistry.set(composerDrafts.composerDraftsAtom, { - [draftKey]: { text: "typed offline", attachments: [] }, - }); - await harness.manager.enqueue(message); + it.effect("rolls a failed recovery merge back so the retry cannot duplicate the text", () => + Effect.gen(function* () { + const message = queuedMessage({ messageId: "message-restore", text: "queued text" }); + const draftKey = `${message.environmentId}:${message.threadId}`; + appAtomRegistry.set(composerDrafts.composerDraftsAtom, { + [draftKey]: { text: "typed offline", attachments: [] }, + }); + yield* Effect.promise(() => harness.manager.enqueue(message)); - harness.draftFile.setWriteError(new Error("disk full")); - await expect(restoreRejectedQueuedMessage(message, "too large")).resolves.toBe("retry"); + harness.draftFile.setWriteError(new Error("disk full")); + expect(yield* restoreRejectedQueuedMessage(message, "too large")).toBe("retry"); - expect(composerDrafts.getComposerDraftSnapshot(draftKey).text).toBe("typed offline"); - expect(remainingMessages()).toEqual([message]); + expect(composerDrafts.getComposerDraftSnapshot(draftKey).text).toBe("typed offline"); + expect(remainingMessages()).toEqual([message]); - harness.draftFile.setWriteError(null); - await expect(restoreRejectedQueuedMessage(message, "too large")).resolves.toBe("restored"); + harness.draftFile.setWriteError(null); + expect(yield* restoreRejectedQueuedMessage(message, "too large")).toBe("restored"); - expect(composerDrafts.getComposerDraftSnapshot(draftKey).text).toBe( - "typed offline\n\nqueued text", - ); - expect(remainingMessages()).toEqual([]); - expect(appAtomRegistry.get(threadComposerErrorsAtom)[draftKey]?.message).toBe("too large"); - }); + expect(composerDrafts.getComposerDraftSnapshot(draftKey).text).toBe( + "typed offline\n\nqueued text", + ); + expect(remainingMessages()).toEqual([]); + expect(appAtomRegistry.get(threadComposerErrorsAtom)[draftKey]?.message).toBe("too large"); + }), + ); }); diff --git a/apps/mobile/src/state/threadOutboxDelivery.ts b/apps/mobile/src/state/threadOutboxDelivery.ts index 3a3802d425897..5eb87964571db 100644 --- a/apps/mobile/src/state/threadOutboxDelivery.ts +++ b/apps/mobile/src/state/threadOutboxDelivery.ts @@ -23,14 +23,20 @@ import { } from "@t3tools/contracts"; import { buildTemporaryWorktreeBranchName } from "@t3tools/shared/git"; import * as Cause from "effect/Cause"; +import * as Clock from "effect/Clock"; import * as Effect from "effect/Effect"; +import * as Exit from "effect/Exit"; +import * as Fiber from "effect/Fiber"; +import * as Latch from "effect/Latch"; +import * as Schema from "effect/Schema"; import { AsyncResult, Atom } from "effect/reactivity"; import { createDebugLogger } from "../lib/debugLog"; +import type { DraftComposerAttachment } from "../lib/composerImages"; import { scopedThreadKey } from "../lib/scopedEntities"; import { buildProjectThreadStartTurnInput } from "../lib/projectThreadStartTurn"; import { serializeComposerMessageForServer, uploadedComposerContext } from "../lib/composerContext"; -import { prepareTurnAttachments, type PreparedTurnAttachments } from "../lib/attachmentUpload"; +import { prepareTurnAttachments } from "../lib/attachmentUpload"; import { randomHex } from "../lib/uuid"; import { describeUnavailableModelSelection, @@ -49,12 +55,7 @@ import { } from "./pending-thread-creation"; import { environmentProjects } from "./projects"; import { environmentServerConfigsAtom, serverEnvironment } from "./server"; -import { composerAttachmentUploadsAtom } from "./composer-attachment-upload-states"; import { uploadComposerAttachmentShared } from "./composer-attachment-uploads"; -import { - queuedAttachmentsStillUploading, - uploadingComposerAttachmentKeys, -} from "../lib/composerAttachmentUploadQueue"; import { gitEnvironment } from "./git"; import { environmentSession, readEnvironmentScope, sessionHasScope } from "./session"; import { @@ -92,7 +93,7 @@ import { waitForComposerDraftsLoaded, } from "./use-composer-drafts"; import { - dispatchingQueuedMessageIdAtom, + dispatchingQueuedMessageIdsAtom, editingQueuedMessageIdsAtom, retryingQueuedMessageIdsAtom, threadOutboxShellStatusesAtom, @@ -177,12 +178,17 @@ function setQueuedMessageRetrying(queuedMessageId: MessageId, retrying: boolean) function beginDispatchingQueuedMessage(queuedMessageId: MessageId): void { // An attempt in flight is sending, even while its retry count is kept. setQueuedMessageRetrying(queuedMessageId, false); - appAtomRegistry.set(dispatchingQueuedMessageIdAtom, queuedMessageId); + appAtomRegistry.set(dispatchingQueuedMessageIdsAtom, { + ...appAtomRegistry.get(dispatchingQueuedMessageIdsAtom), + [queuedMessageId]: true, + }); } function finishDispatchingQueuedMessage(queuedMessageId: MessageId): void { - const current = appAtomRegistry.get(dispatchingQueuedMessageIdAtom); - appAtomRegistry.set(dispatchingQueuedMessageIdAtom, current === queuedMessageId ? null : current); + const { [queuedMessageId]: _finished, ...others } = appAtomRegistry.get( + dispatchingQueuedMessageIdsAtom, + ); + appAtomRegistry.set(dispatchingQueuedMessageIdsAtom, others); } function findThread( @@ -210,57 +216,93 @@ function settingsCommandId(message: QueuedThreadMessage, setting: string): Comma return CommandId.make(`${message.commandId}:${setting}`); } -export async function prepareQueuedMessageAttachments(queuedMessage: QueuedThreadMessage): Promise< - | { - readonly status: "ready"; - readonly prepared: PreparedTurnAttachments; - readonly persistedMessage: QueuedThreadMessage; - readonly deliveryRevision: number; - } - | { readonly status: "abandoned" } -> { - if (!(await confirmThreadOutboxMessageQueued(queuedMessage))) { - return { status: "abandoned" }; - } - if (!readEnvironmentScope(queuedMessage.environmentId, AuthOrchestrationOperateScope)) { - return { status: "abandoned" }; +/** Outbox or draft storage refused a step; the step decides what that means for the message. */ +class ThreadOutboxStorageError extends Schema.TaggedError()( + "ThreadOutboxStorageError", + { + operation: Schema.Literals([ + "forget-sign-out-snapshot", + "remove-delivered", + "remove-acknowledged", + "remove-unsendable", + "hand-back-to-draft", + "restore-to-draft", + "undo-draft-merge", + "load-drafts", + "flush-drafts", + "remove-recovered", + "remove-restored", + ]), + messageId: Schema.String, + cause: Schema.Defect(), + }, +) { + override get message(): string { + return `Queued message ${this.messageId} could not ${this.operation.replaceAll("-", " ")}.`; } - const revision = threadOutboxRevision(queuedMessage.messageId); - if (!isQueuedMessagePayloadCurrent(queuedMessage, revision)) { - return { status: "abandoned" }; - } - let persistedMessage = queuedMessage; - let deliveryRevision = revision; - const result = await Effect.runPromise( - prepareTurnAttachments({ +} + +const storage = ( + operation: ThreadOutboxStorageError["operation"], + messageId: MessageId, + write: () => Promise, +) => + Effect.tryPromise({ + try: write, + catch: (cause) => new ThreadOutboxStorageError({ operation, messageId, cause }), + }); + +/** The head was not handled; back off and try it again. */ +export class DeliveryRetry extends Schema.TaggedError()("DeliveryRetry", {}) {} + +const retryDelivery = Effect.fail(new DeliveryRetry()); + +const isEditingQueuedMessage = (messageId: MessageId): boolean => + appAtomRegistry.get(editingQueuedMessageIdsAtom)[messageId] === true; + +const confirmQueued = (message: QueuedThreadMessage) => + Effect.promise(() => confirmThreadOutboxMessageQueued(message)); + +const abandoned = { status: "abandoned" } as const; + +export const prepareQueuedMessageAttachments = Effect.fn("ThreadOutbox.prepareAttachments")( + function* (queuedMessage: QueuedThreadMessage) { + if (!(yield* confirmQueued(queuedMessage))) return abandoned; + if (!readEnvironmentScope(queuedMessage.environmentId, AuthOrchestrationOperateScope)) { + return abandoned; + } + const revision = threadOutboxRevision(queuedMessage.messageId); + if (!isQueuedMessagePayloadCurrent(queuedMessage, revision)) return abandoned; + let persistedMessage = queuedMessage; + let deliveryRevision = revision; + const result = yield* prepareTurnAttachments({ environmentId: queuedMessage.environmentId, attachments: queuedMessage.attachments, supportsImageUploads: true, uploadAttachment: (attachment) => uploadComposerAttachmentShared(queuedMessage.environmentId, attachment), - persistUploadedReferences: (draftAttachments) => - Effect.promise(async () => { - if (appAtomRegistry.get(editingQueuedMessageIdsAtom)[queuedMessage.messageId]) { - return "abandon" as const; - } - const updatedMessage = { ...queuedMessage, attachments: draftAttachments }; - if (!(await updateThreadOutboxMessage(updatedMessage, revision))) { - return "abandon" as const; - } - persistedMessage = updatedMessage; - deliveryRevision = revision + 1; - return "persisted" as const; - }), - }), - ); - if ( - result.status === "abandoned" || - !isQueuedMessagePayloadCurrent(persistedMessage, deliveryRevision) - ) { - return { status: "abandoned" }; - } - return { status: "ready", prepared: result, persistedMessage, deliveryRevision }; -} + persistUploadedReferences: Effect.fn("ThreadOutbox.persistUploadedReferences")(function* ( + draftAttachments: ReadonlyArray, + ) { + if (isEditingQueuedMessage(queuedMessage.messageId)) return "abandon" as const; + const updatedMessage = { ...queuedMessage, attachments: draftAttachments }; + if (!(yield* Effect.promise(() => updateThreadOutboxMessage(updatedMessage, revision)))) { + return "abandon" as const; + } + persistedMessage = updatedMessage; + deliveryRevision = revision + 1; + return "persisted" as const; + }), + }); + if ( + result.status === "abandoned" || + !isQueuedMessagePayloadCurrent(persistedMessage, deliveryRevision) + ) { + return abandoned; + } + return { status: "ready", prepared: result, persistedMessage, deliveryRevision } as const; + }, +); function isQueuedMessagePayloadCurrent( message: QueuedThreadMessage, @@ -274,273 +316,279 @@ function isQueuedMessagePayloadCurrent( ); } -export async function completeQueuedMessageDelivery( +const forgetCloudQueuedMessage = (queuedMessage: QueuedThreadMessage) => + storage("forget-sign-out-snapshot", queuedMessage.messageId, () => + removeDeliveredCloudQueuedMessage(queuedMessage), + ).pipe( + Effect.catch((error) => + Effect.logWarning("[thread-outbox] could not update sign-out snapshot after delivery", { + messageId: queuedMessage.messageId, + error: error.cause, + }), + ), + ); + +export const completeQueuedMessageDelivery = Effect.fn("ThreadOutbox.completeDelivery")(function* ( queuedMessage: QueuedThreadMessage, deliveryRevision: number, - sequence: number | undefined, -): Promise<"removed" | "edited" | "failed"> { + sequence?: number, +) { clearThreadComposerError( scopedThreadKey(queuedMessage.environmentId, queuedMessage.threadId), queuedMessage.messageId, ); - try { - await removeDeliveredCloudQueuedMessage(queuedMessage).catch((error) => { - console.warn("[thread-outbox] could not update sign-out snapshot after delivery", { - messageId: queuedMessage.messageId, - error, - }); - }); - // Editors can acquire entries during startTurn without bumping revisions; CAS alone would lose unsaved edits. - if (appAtomRegistry.get(editingQueuedMessageIdsAtom)[queuedMessage.messageId]) { - return "edited"; - } - retainAcknowledgedThreadMessage( - sequence === undefined ? queuedMessage : { ...queuedMessage, sequence }, - ); - const removed = await removeThreadOutboxMessage( + yield* forgetCloudQueuedMessage(queuedMessage); + // Editors can acquire entries during startTurn without bumping revisions; CAS alone would lose unsaved edits. + if (isEditingQueuedMessage(queuedMessage.messageId)) return "edited" as const; + retainAcknowledgedThreadMessage( + sequence === undefined ? queuedMessage : { ...queuedMessage, sequence }, + ); + const removed = yield* storage("remove-delivered", queuedMessage.messageId, () => + removeThreadOutboxMessage( queuedMessage, deliveryRevision, - () => !appAtomRegistry.get(editingQueuedMessageIdsAtom)[queuedMessage.messageId], - ); - if (!removed) { - forgetAcknowledgedThreadMessage(queuedMessage); - threadOutboxDebug.log("delivered message was edited before cleanup", { + () => !isEditingQueuedMessage(queuedMessage.messageId), + ), + ).pipe( + Effect.catch((error) => + Effect.logWarning("[thread-outbox] failed to remove delivered queued message", { environmentId: queuedMessage.environmentId, threadId: queuedMessage.threadId, messageId: queuedMessage.messageId, - }); - return "edited"; - } - return "removed"; - } catch (error) { - forgetAcknowledgedThreadMessage(queuedMessage); - console.warn("[thread-outbox] failed to remove delivered queued message", { - environmentId: queuedMessage.environmentId, - threadId: queuedMessage.threadId, - messageId: queuedMessage.messageId, - error, - }); - return "failed"; - } -} + error: error.cause, + }).pipe(Effect.as(null)), + ), + ); + if (removed === true) return "removed" as const; + forgetAcknowledgedThreadMessage(queuedMessage); + if (removed === null) return "failed" as const; + threadOutboxDebug.log("delivered message was edited before cleanup", { + environmentId: queuedMessage.environmentId, + threadId: queuedMessage.threadId, + messageId: queuedMessage.messageId, + }); + return "edited" as const; +}); -export async function removeAcknowledgedExistingThreadMessage( - queuedMessage: QueuedThreadMessage, - acknowledgedMessageIds: Set, -): Promise { - try { - await removeDeliveredCloudQueuedMessage(queuedMessage).catch((error) => { - console.warn("[thread-outbox] could not update sign-out snapshot after delivery", { +export const removeAcknowledgedExistingThreadMessage = Effect.fn( + "ThreadOutbox.removeAcknowledgedMessage", +)(function* (queuedMessage: QueuedThreadMessage, acknowledgedMessageIds: Set) { + yield* forgetCloudQueuedMessage(queuedMessage); + const removed = yield* storage("remove-acknowledged", queuedMessage.messageId, () => + removeThreadOutboxMessage(queuedMessage), + ).pipe( + Effect.catch((error) => + Effect.logWarning("[thread-outbox] failed to remove acknowledged queued message", { + environmentId: queuedMessage.environmentId, + threadId: queuedMessage.threadId, messageId: queuedMessage.messageId, - error, - }); - }); - const removed = await removeThreadOutboxMessage(queuedMessage); - if (removed) { - acknowledgedMessageIds.delete(queuedMessage.messageId); - } - return removed; - } catch (error) { - console.warn("[thread-outbox] failed to remove acknowledged queued message", { - environmentId: queuedMessage.environmentId, - threadId: queuedMessage.threadId, - messageId: queuedMessage.messageId, - error, - }); - return false; - } -} + error: error.cause, + }).pipe(Effect.as(false)), + ), + ); + if (!removed) return yield* retryDelivery; + acknowledgedMessageIds.delete(queuedMessage.messageId); +}); -export async function recoverEditedCreationAfterDelivery( - queuedMessage: QueuedThreadMessage, -): Promise { - const kept = Object.values(appAtomRegistry.get(threadOutboxManager.queuedMessagesByThreadKeyAtom)) - .flat() - .find((candidate) => candidate.messageId === queuedMessage.messageId); - if (!kept) { - return true; - } - const keptRevision = threadOutboxRevision(kept.messageId); - if (appAtomRegistry.get(editingQueuedMessageIdsAtom)[kept.messageId]) { - return true; - } - const draftKey = scopedThreadKey(kept.environmentId, kept.threadId); - try { - // Merge before removal to retain file ownership; allow overflow and let send guards require trimming. - await mergeComposerDraftContent(draftKey, { - text: kept.text, - context: kept.context, - attachments: [], - }); - if (appAtomRegistry.get(editingQueuedMessageIdsAtom)[kept.messageId]) { - return true; - } - if (threadOutboxRevision(kept.messageId) !== keptRevision) { - return false; - } - const existingAttachmentIds = new Set( - getComposerDraftSnapshot(draftKey).attachments.map((attachment) => attachment.id), - ); - appendComposerDraftAttachments( - draftKey, - kept.attachments.filter((attachment) => !existingAttachmentIds.has(attachment.id)), - { restore: true }, +/** Hands a new task edited after its launch back to the composer, then drops it from the queue. */ +export const recoverEditedCreationAfterDelivery = Effect.fn("ThreadOutbox.recoverEditedCreation")( + function* (queuedMessage: QueuedThreadMessage) { + const kept = Object.values( + appAtomRegistry.get(threadOutboxManager.queuedMessagesByThreadKeyAtom), + ) + .flat() + .find((candidate) => candidate.messageId === queuedMessage.messageId); + if (!kept || isEditingQueuedMessage(kept.messageId)) return; + const keptRevision = threadOutboxRevision(kept.messageId); + const draftKey = scopedThreadKey(kept.environmentId, kept.threadId); + const handedBack = yield* Effect.gen(function* () { + // Merge before removal to retain file ownership; allow overflow and let send guards require trimming. + yield* storage("hand-back-to-draft", kept.messageId, () => + mergeComposerDraftContent(draftKey, { + text: kept.text, + context: kept.context, + attachments: [], + }), + ); + if (isEditingQueuedMessage(kept.messageId)) return "edited" as const; + if (threadOutboxRevision(kept.messageId) !== keptRevision) return "changed" as const; + const existingAttachmentIds = new Set( + getComposerDraftSnapshot(draftKey).attachments.map((attachment) => attachment.id), + ); + appendComposerDraftAttachments( + draftKey, + kept.attachments.filter((attachment) => !existingAttachmentIds.has(attachment.id)), + { restore: true }, + ); + updateComposerDraftSettings(draftKey, { + ...(kept.modelSelection !== undefined ? { modelSelection: kept.modelSelection } : {}), + ...(kept.runtimeMode !== undefined ? { runtimeMode: kept.runtimeMode } : {}), + ...(kept.interactionMode !== undefined ? { interactionMode: kept.interactionMode } : {}), + }); + // Flush before removal: append only schedules persistence, leaving the queue as the sole durable copy. + yield* storage("flush-drafts", kept.messageId, () => flushComposerDrafts()); + return "handed-back" as const; + }).pipe( + Effect.catch((error) => + Effect.logWarning( + "[thread-outbox] could not hand an edited pending task to the composer", + error.cause, + ).pipe(Effect.as("failed" as const)), + ), ); - updateComposerDraftSettings(draftKey, { - ...(kept.modelSelection !== undefined ? { modelSelection: kept.modelSelection } : {}), - ...(kept.runtimeMode !== undefined ? { runtimeMode: kept.runtimeMode } : {}), - ...(kept.interactionMode !== undefined ? { interactionMode: kept.interactionMode } : {}), - }); - // Flush before removal: append only schedules persistence, leaving the queue as the sole durable copy. - await flushComposerDrafts(); - } catch (error) { - console.warn("[thread-outbox] could not hand an edited pending task to the composer", error); - return false; - } - if (appAtomRegistry.get(editingQueuedMessageIdsAtom)[kept.messageId]) { - return true; - } - try { - return await removeThreadOutboxMessage( - kept, - keptRevision, - () => !appAtomRegistry.get(editingQueuedMessageIdsAtom)[kept.messageId], + if (handedBack === "edited") return; + if (handedBack !== "handed-back") return yield* retryDelivery; + if (isEditingQueuedMessage(kept.messageId)) return; + const removed = yield* storage("remove-recovered", kept.messageId, () => + removeThreadOutboxMessage(kept, keptRevision, () => !isEditingQueuedMessage(kept.messageId)), + ).pipe( + Effect.catch((error) => + Effect.logWarning( + "[thread-outbox] could not remove recovered pending task", + error.cause, + ).pipe(Effect.as(false)), + ), ); - } catch (error) { - console.warn("[thread-outbox] could not remove recovered pending task", error); - return false; - } -} - -export async function restoreRejectedQueuedMessage( - queuedMessage: QueuedThreadMessage, - message: string, -): Promise<"restored" | "deferred" | "blocked" | "retry"> { - const draftKey = recoveryDraftKey(queuedMessage); - const threadKey = scopedThreadKey(queuedMessage.environmentId, queuedMessage.threadId); - let rollback: { readonly snapshot: ComposerDraft; readonly merged: ComposerDraft } | null = null; - try { - if ( - appAtomRegistry.get(editingQueuedMessageIdsAtom)[queuedMessage.messageId] || - !(await confirmThreadOutboxMessageQueued(queuedMessage)) || - appAtomRegistry.get(editingQueuedMessageIdsAtom)[queuedMessage.messageId] - ) { - return "deferred"; - } - const revision = threadOutboxRevision(queuedMessage.messageId); + if (!removed) return yield* retryDelivery; + }, +); - await waitForComposerDraftsLoaded(); - if (appAtomRegistry.get(editingQueuedMessageIdsAtom)[queuedMessage.messageId]) { - return "deferred"; - } - const originalDraft = getComposerDraftSnapshot(draftKey); - const existingAttachmentIds = new Set( - originalDraft.attachments.map((attachment) => attachment.id), - ); - const addedAttachmentCount = queuedMessage.attachments.filter( - (attachment) => !existingAttachmentIds.has(attachment.id), - ).length; - if (existingAttachmentIds.size + addedAttachmentCount > PROVIDER_SEND_TURN_MAX_ATTACHMENTS) { - setThreadComposerError( - threadKey, - `Remove attachments from the draft before restoring this message. Messages can contain at most ${PROVIDER_SEND_TURN_MAX_ATTACHMENTS} attachments.`, - queuedMessage.messageId, +/** + * Hands an undeliverable message back to its draft with the reason. "blocked" means the draft + * cannot take it yet (attachment cap); "deferred" means an editor or newer write owns it. + */ +export const restoreRejectedQueuedMessage = Effect.fn("ThreadOutbox.restoreRejectedMessage")( + function* (queuedMessage: QueuedThreadMessage, message: string) { + const draftKey = recoveryDraftKey(queuedMessage); + const threadKey = scopedThreadKey(queuedMessage.environmentId, queuedMessage.threadId); + const editing = () => isEditingQueuedMessage(queuedMessage.messageId); + // Until the queue drops the message, a failure undoes the merge, keeping concurrent edits. + let rollback: { readonly snapshot: ComposerDraft; readonly merged: ComposerDraft } | null = + null; + const undo = (snapshot: ComposerDraft, merged: ComposerDraft) => + storage("undo-draft-merge", queuedMessage.messageId, () => + undoComposerDraftMerge(draftKey, snapshot, merged), ); - return "blocked"; - } + return yield* Effect.gen(function* () { + if (editing() || !(yield* confirmQueued(queuedMessage)) || editing()) { + return "deferred" as const; + } + const revision = threadOutboxRevision(queuedMessage.messageId); + yield* storage("load-drafts", queuedMessage.messageId, () => waitForComposerDraftsLoaded()); + if (editing()) return "deferred" as const; + const originalDraft = getComposerDraftSnapshot(draftKey); + const existingAttachmentIds = new Set( + originalDraft.attachments.map((attachment) => attachment.id), + ); + const addedAttachmentCount = queuedMessage.attachments.filter( + (attachment) => !existingAttachmentIds.has(attachment.id), + ).length; + if (existingAttachmentIds.size + addedAttachmentCount > PROVIDER_SEND_TURN_MAX_ATTACHMENTS) { + setThreadComposerError( + threadKey, + `Remove attachments from the draft before restoring this message. Messages can contain at most ${PROVIDER_SEND_TURN_MAX_ATTACHMENTS} attachments.`, + queuedMessage.messageId, + ); + return "blocked" as const; + } - const withdrawError = () => clearThreadComposerError(threadKey, queuedMessage.messageId); - if (!queuedMessage.creation) { - setThreadComposerError(threadKey, message, queuedMessage.messageId); - } - let mergedDraft: ComposerDraft; - try { + const withdraw = (merged: ComposerDraft) => + Effect.sync(() => clearThreadComposerError(threadKey, queuedMessage.messageId)).pipe( + Effect.andThen(undo(originalDraft, merged)), + Effect.as("deferred" as const), + ); + if (!queuedMessage.creation) { + setThreadComposerError(threadKey, message, queuedMessage.messageId); + } stampRecoveryDraftProject(queuedMessage, draftKey); - await mergeComposerDraftContent(draftKey, { - text: queuedMessage.text, - context: queuedMessage.context, - attachments: queuedMessage.attachments, - }); - } finally { - // Capture rollback in finally because merge publishes even on persistence failure; undo must preserve concurrent edits. - mergedDraft = getComposerDraftSnapshot(draftKey); - rollback = { snapshot: originalDraft, merged: mergedDraft }; - } - if (appAtomRegistry.get(editingQueuedMessageIdsAtom)[queuedMessage.messageId]) { - withdrawError(); - await undoComposerDraftMerge(draftKey, originalDraft, mergedDraft); - return "deferred"; - } - updateComposerDraftSettings(draftKey, { - ...(queuedMessage.modelSelection ? { modelSelection: queuedMessage.modelSelection } : {}), - ...(queuedMessage.runtimeMode ? { runtimeMode: queuedMessage.runtimeMode } : {}), - ...(queuedMessage.interactionMode ? { interactionMode: queuedMessage.interactionMode } : {}), - ...(queuedMessage.freshContext ? { freshContext: true } : {}), - ...(queuedMessage.creation - ? { - workspaceSelection: { - mode: queuedMessage.creation.workspaceMode, - branch: queuedMessage.creation.branch, - worktreePath: queuedMessage.creation.worktreePath, - pullRequestReference: queuedMessage.creation.pullRequestReference, - ...(queuedMessage.creation.startFromOrigin !== undefined - ? { startFromOrigin: queuedMessage.creation.startFromOrigin } - : {}), - }, - } - : {}), - }); - const restoredDraft = getComposerDraftSnapshot(draftKey); - rollback = { snapshot: originalDraft, merged: restoredDraft }; - await flushComposerDrafts(); - if ( - appAtomRegistry.get(editingQueuedMessageIdsAtom)[queuedMessage.messageId] || - !(await confirmThreadOutboxMessageQueued(queuedMessage)) || - appAtomRegistry.get(editingQueuedMessageIdsAtom)[queuedMessage.messageId] - ) { - withdrawError(); - await undoComposerDraftMerge(draftKey, originalDraft, restoredDraft); - return "deferred"; - } - if ( - !(await removeThreadOutboxMessage( - queuedMessage, - revision, - () => !appAtomRegistry.get(editingQueuedMessageIdsAtom)[queuedMessage.messageId], - )) - ) { - withdrawError(); - await undoComposerDraftMerge(draftKey, originalDraft, restoredDraft); - return "deferred"; - } - // After queue removal, the draft owns content and must never roll back. - rollback = null; - if (queuedMessage.creation) { - withdrawError(); - recordPendingThreadCreationOutcome({ - kind: "failed", - message: queuedMessage, - reason: message, + // The merge publishes even when persisting it fails, so the rollback is captured either way. + yield* storage("restore-to-draft", queuedMessage.messageId, () => + mergeComposerDraftContent(draftKey, { + text: queuedMessage.text, + context: queuedMessage.context, + attachments: queuedMessage.attachments, + }), + ).pipe( + Effect.ensuring( + Effect.sync(() => { + rollback = { snapshot: originalDraft, merged: getComposerDraftSnapshot(draftKey) }; + }), + ), + ); + if (editing()) return yield* withdraw(getComposerDraftSnapshot(draftKey)); + updateComposerDraftSettings(draftKey, { + ...(queuedMessage.modelSelection ? { modelSelection: queuedMessage.modelSelection } : {}), + ...(queuedMessage.runtimeMode ? { runtimeMode: queuedMessage.runtimeMode } : {}), + ...(queuedMessage.interactionMode + ? { interactionMode: queuedMessage.interactionMode } + : {}), + ...(queuedMessage.freshContext ? { freshContext: true } : {}), + ...(queuedMessage.creation + ? { + workspaceSelection: { + mode: queuedMessage.creation.workspaceMode, + branch: queuedMessage.creation.branch, + worktreePath: queuedMessage.creation.worktreePath, + pullRequestReference: queuedMessage.creation.pullRequestReference, + ...(queuedMessage.creation.startFromOrigin !== undefined + ? { startFromOrigin: queuedMessage.creation.startFromOrigin } + : {}), + }, + } + : {}), }); - } - return "restored"; - } catch (error) { - if (rollback !== null) { - await undoComposerDraftMerge(draftKey, rollback.snapshot, rollback.merged).catch( - (undoError) => { - console.warn("[thread-outbox] failed to persist a recovery rollback", undoError); - }, + const restoredDraft = getComposerDraftSnapshot(draftKey); + rollback = { snapshot: originalDraft, merged: restoredDraft }; + yield* storage("flush-drafts", queuedMessage.messageId, () => flushComposerDrafts()); + if (editing() || !(yield* confirmQueued(queuedMessage)) || editing()) { + return yield* withdraw(restoredDraft); + } + const removed = yield* storage("remove-restored", queuedMessage.messageId, () => + removeThreadOutboxMessage(queuedMessage, revision, () => !editing()), ); - } - console.warn("[thread-outbox] failed to restore an undeliverable message", error); - setThreadComposerError( - threadKey, - error instanceof Error ? error.message : "The unsent message could not be restored.", - queuedMessage.messageId, + if (!removed) return yield* withdraw(restoredDraft); + // After queue removal, the draft owns content and must never roll back. + rollback = null; + if (queuedMessage.creation) { + clearThreadComposerError(threadKey, queuedMessage.messageId); + recordPendingThreadCreationOutcome({ + kind: "failed", + message: queuedMessage, + reason: message, + }); + } + return "restored" as const; + }).pipe( + Effect.catch((error) => + Effect.gen(function* () { + const pending: typeof rollback = rollback; + if (pending !== null) { + yield* undo(pending.snapshot, pending.merged).pipe( + Effect.catch((undoError) => + Effect.logWarning( + "[thread-outbox] failed to persist a recovery rollback", + undoError.cause, + ), + ), + ); + } + yield* Effect.logWarning( + "[thread-outbox] failed to restore an undeliverable message", + error.cause, + ); + setThreadComposerError( + threadKey, + error.cause instanceof Error + ? error.cause.message + : "The unsent message could not be restored.", + queuedMessage.messageId, + ); + return "retry" as const; + }), + ), ); - return "retry"; - } -} + }, +); /** Derive keys from message ids so failed recovery retries reuse the same draft. */ function recoveryDraftKey(queuedMessage: QueuedThreadMessage): string { @@ -562,14 +610,10 @@ function stampRecoveryDraftProject(queuedMessage: QueuedThreadMessage, draftKey: }); } -async function preserveUploadedAttachmentsForEditor( - originalMessage: QueuedThreadMessage, - uploadedMessage: QueuedThreadMessage, -): Promise { - if (!originalMessage.creation) { - return; - } - +const preserveUploadedAttachmentsForEditor = Effect.fn( + "ThreadOutbox.preserveUploadedAttachmentsForEditor", +)(function* (originalMessage: QueuedThreadMessage, uploadedMessage: QueuedThreadMessage) { + if (!originalMessage.creation) return; const draftKey = `pending-task:${originalMessage.messageId}`; const draft = getComposerDraftSnapshot(draftKey); const uploadedById = new Map( @@ -593,18 +637,13 @@ async function preserveUploadedAttachmentsForEditor( uploadEnvironmentId: uploaded.uploadEnvironmentId, }; }); - if (changed) { - replaceComposerDraftAttachments(draftKey, nextAttachments); - await flushComposerDrafts(); - } -} + if (!changed) return; + replaceComposerDraftAttachments(draftKey, nextAttachments); + yield* Effect.promise(() => flushComposerDrafts()); +}); -// Everything a drain pass decides from. Each queued environment's session is -// read here so a granted or revoked operate scope re-runs the drain. -const uploadingComposerAttachmentKeysAtom = Atom.map( - composerAttachmentUploadsAtom, - uploadingComposerAttachmentKeys, -); +// Everything delivery decides from. Each queued environment's session is read +// here so a granted or revoked operate scope wakes delivery. const deliveryInputsAtom = Atom.make((get) => { const connectedEnvironments = get(workspaceConnections.environmentsAtom); @@ -620,7 +659,6 @@ const deliveryInputsAtom = Atom.make((get) => { } } return { - dispatchingQueuedMessageId: get(dispatchingQueuedMessageIdAtom), editingQueuedMessageIds: get(editingQueuedMessageIdsAtom), queuedMessagesByThreadKey: get(threadOutboxManager.queuedMessagesByThreadKeyAtom), shellStatuses: get(threadOutboxShellStatusesAtom), @@ -629,8 +667,6 @@ const deliveryInputsAtom = Atom.make((get) => { serverConfigs: get(environmentServerConfigsAtom), connectedEnvironments, operableEnvironments, - // Wakes a message waiting on the upload worker when that upload settles. - uploadingAttachmentKeys: get(uploadingComposerAttachmentKeysAtom), }; }).pipe(Atom.withLabel("mobile:thread-outbox:delivery-inputs")); @@ -660,11 +696,11 @@ function deliveredSequence( return AsyncResult.isSuccess(result) ? result.value.sequence : undefined; } -const runDeliveryCommand = ( +const deliveryCommand = ( command: AtomCommand, input: W, -): Promise> => - runAtomCommand(appAtomRegistry, command, input, { reportFailure: false }); +): Effect.Effect> => + Effect.promise(() => runAtomCommand(appAtomRegistry, command, input, { reportFailure: false })); function reportCommandFailure( queuedMessage: QueuedThreadMessage, @@ -695,14 +731,33 @@ function reportCommandFailure( interface ThreadOutboxRetry { readonly attempt: number; readonly notBefore: number; - readonly timer: ReturnType; } +/** A thread's delivery fiber; `retiring` once interrupted, until the fiber has exited. */ +interface ThreadFiber { + readonly wake: Latch.Latch; + fiber: Fiber.Fiber | undefined; + retiring: boolean; +} + +/** What a thread's delivery fiber does next with the head of its queue. */ +type ThreadDeliveryStep = + /** Nothing to send until an input changes, or until `until` when a backoff is running. */ + | { readonly _tag: "Wait"; readonly until?: number } + /** The head failed without a send; back off before trying it again. */ + | { readonly _tag: "Backoff"; readonly messageId: MessageId } + | { + readonly _tag: "Dispatch"; + readonly messageId: MessageId; + /** Succeeds once the message is handled (sent, removed or handed back). */ + readonly run: Effect.Effect; + }; + /** - * Delivers queued thread messages one at a time. A drain pass runs whenever its inputs - * change (outbox, locks, shells, projects, configs, connections, scopes) and when a retry - * backoff expires. `dispatchingQueuedMessageIdAtom` is the single in-flight slot; clearing - * it re-runs the drain for the next message. + * Delivers queued thread messages, one fiber per thread so a slow send in one thread never + * holds another. Each fiber sends its thread's head message in order, backs off after a + * failure, and otherwise waits until an input changes (outbox, locks, shells, projects, + * configs, connections, scopes). */ export function createThreadOutboxDelivery(): { readonly drain: () => void; @@ -732,52 +787,29 @@ export function createThreadOutboxDelivery(): { const scheduleRetry = (messageId: MessageId): void => { if (disposed) return; - const previous = retries.get(messageId); - if (previous !== undefined) clearTimeout(previous.timer); - const attempt = (previous?.attempt ?? 0) + 1; - const delayMs = threadOutboxRetryDelayMs(attempt); + const attempt = (retries.get(messageId)?.attempt ?? 0) + 1; retries.set(messageId, { attempt, - notBefore: Date.now() + delayMs, - timer: setTimeout(drain, delayMs), + notBefore: Date.now() + threadOutboxRetryDelayMs(attempt), }); setQueuedMessageRetrying(messageId, true); }; const clearRetry = (messageId: MessageId): void => { - const retry = retries.get(messageId); - if (retry === undefined) return; - clearTimeout(retry.timer); - retries.delete(messageId); + if (!retries.delete(messageId)) return; setQueuedMessageRetrying(messageId, false); }; - // A delivery that throws backs off like any failure. Releasing the slot - // without a retry would re-run the drain straight into the same message. - const settle = (messageId: MessageId, delivery: Promise): void => { - void delivery - .then( - (sent) => (sent ? clearRetry(messageId) : scheduleRetry(messageId)), - (error: unknown) => { - console.warn("[thread-outbox] queued message delivery threw", { messageId, error }); - scheduleRetry(messageId); - }, - ) - .finally(() => finishDispatchingQueuedMessage(messageId)); - }; - // An over-cap draft blocks recovery until the user changes its attachments; // parking on the draft avoids hammering the restore with backoff retries. - const restoreQueuedMessage = async ( + const restoreQueuedMessage = Effect.fn("ThreadOutbox.restoreQueuedMessage")(function* ( queuedMessage: QueuedThreadMessage, message: string, - ): Promise => { - const result = await restoreRejectedQueuedMessage(queuedMessage, message); - if (result !== "blocked") { - return result !== "retry"; - } - if (disposed || blockedRecoveries.has(queuedMessage.messageId)) { - return true; + ) { + const result = yield* restoreRejectedQueuedMessage(queuedMessage, message); + if (result === "retry") return yield* retryDelivery; + if (result !== "blocked" || disposed || blockedRecoveries.has(queuedMessage.messageId)) { + return; } const draftKey = recoveryDraftKey(queuedMessage); const editorDraftKey = queuedMessage.creation @@ -800,23 +832,22 @@ export function createThreadOutboxDelivery(): { } blockedRecoveries.delete(queuedMessage.messageId); active.unsubscribe(); - drain(); + wakeThreads(); }); blockedRecoveries.set(queuedMessage.messageId, { message: queuedMessage, unsubscribe }); - return true; - }; + }); - const sendQueuedMessage = async ( + const sendQueuedMessage = Effect.fn("ThreadOutbox.sendQueuedMessage")(function* ( queuedMessage: QueuedThreadMessage, thread: EnvironmentThreadShell, - ): Promise => { + ) { const hasAccess = () => readEnvironmentScope(queuedMessage.environmentId, AuthOrchestrationOperateScope); - if (!hasAccess()) return true; + if (!hasAccess()) return; const serverConfig = appAtomRegistry.get( serverEnvironment.configValueAtom(queuedMessage.environmentId), ); - if (!serverConfig) return false; + if (!serverConfig) return yield* retryDelivery; const settings = resolveQueuedThreadSettings(queuedMessage, thread, serverConfig.providers); if (isModelSelectionUnavailable(serverConfig, settings.modelSelection, thread.projectId)) { const { title, detail } = describeUnavailableModelSelection( @@ -824,7 +855,7 @@ export function createThreadOutboxDelivery(): { settings.modelSelection, thread.projectId, ); - return restoreQueuedMessage(queuedMessage, `${title}. ${detail}`); + return yield* restoreQueuedMessage(queuedMessage, `${title}. ${detail}`); } // The server would refuse this mode in settings sync, which retries forever; stop first. const runtimeModeDenial = managedRuntimeModeDenial( @@ -832,12 +863,12 @@ export function createThreadOutboxDelivery(): { settings.runtimeMode, ); if (runtimeModeDenial !== undefined) { - return restoreQueuedMessage(queuedMessage, runtimeModeDenial); + return yield* restoreQueuedMessage(queuedMessage, runtimeModeDenial); } if (settings.runtimeMode !== thread.runtimeMode) { - if (!hasAccess()) return true; - const runtimeResult = await runDeliveryCommand(threadEnvironment.setRuntimeMode, { + if (!hasAccess()) return; + const runtimeResult = yield* deliveryCommand(threadEnvironment.setRuntimeMode, { environmentId: queuedMessage.environmentId, input: { commandId: settingsCommandId(queuedMessage, "runtime-mode"), @@ -848,13 +879,13 @@ export function createThreadOutboxDelivery(): { }); if (AsyncResult.isFailure(runtimeResult)) { reportCommandFailure(queuedMessage, runtimeResult, "settings-sync"); - return false; + return yield* retryDelivery; } } if (settings.interactionMode !== thread.interactionMode) { - if (!hasAccess()) return true; - const interactionResult = await runDeliveryCommand(threadEnvironment.setInteractionMode, { + if (!hasAccess()) return; + const interactionResult = yield* deliveryCommand(threadEnvironment.setInteractionMode, { environmentId: queuedMessage.environmentId, input: { commandId: settingsCommandId(queuedMessage, "interaction-mode"), @@ -865,51 +896,48 @@ export function createThreadOutboxDelivery(): { }); if (AsyncResult.isFailure(interactionResult)) { reportCommandFailure(queuedMessage, interactionResult, "settings-sync"); - return false; + return yield* retryDelivery; } } - let prepared: PreparedTurnAttachments; - let persistedMessage: QueuedThreadMessage; - let deliveryRevision: number; - if (!hasAccess()) return true; - try { - const preparedResult = await prepareQueuedMessageAttachments(queuedMessage); - if (preparedResult.status === "abandoned") { - return true; - } - prepared = preparedResult.prepared; - persistedMessage = preparedResult.persistedMessage; - deliveryRevision = preparedResult.deliveryRevision; - if (appAtomRegistry.get(editingQueuedMessageIdsAtom)[queuedMessage.messageId]) { - await preserveUploadedAttachmentsForEditor(queuedMessage, preparedResult.persistedMessage); - return true; - } - } catch (error) { - if (!hasAccess()) return true; + if (!hasAccess()) return; + // Waiting on the shared upload is the one part of a send that may be cut short. + const preparedExit = yield* Effect.exit( + Effect.interruptible(prepareQueuedMessageAttachments(queuedMessage)), + ); + if (Exit.isFailure(preparedExit)) { + if (Cause.hasInterruptsOnly(preparedExit.cause)) return yield* Effect.interrupt; + const error = Cause.squash(preparedExit.cause); + if (!hasAccess()) return; logThreadOutboxUploadFailure(queuedMessage, error); if (!shouldRetryThreadOutboxDelivery(error)) { - return restoreQueuedMessage( + return yield* restoreQueuedMessage( queuedMessage, error instanceof Error ? error.message : "An attachment could not upload.", ); } - return false; + return yield* retryDelivery; + } + if (preparedExit.value.status === "abandoned") return; + const { prepared, persistedMessage, deliveryRevision } = preparedExit.value; + if (isEditingQueuedMessage(queuedMessage.messageId)) { + yield* preserveUploadedAttachmentsForEditor(queuedMessage, persistedMessage); + return; } if (!isQueuedMessagePayloadCurrent(persistedMessage, deliveryRevision)) { - return true; + return; } const currentConfig = appAtomRegistry.get( serverEnvironment.configValueAtom(queuedMessage.environmentId), ); - if (!currentConfig) return false; + if (!currentConfig) return yield* retryDelivery; if (isModelSelectionUnavailable(currentConfig, settings.modelSelection, thread.projectId)) { const { title, detail } = describeUnavailableModelSelection( currentConfig, settings.modelSelection, thread.projectId, ); - return restoreQueuedMessage(persistedMessage, `${title}. ${detail}`); + return yield* restoreQueuedMessage(persistedMessage, `${title}. ${detail}`); } const sendSettings = resolveQueuedThreadSettings( queuedMessage, @@ -917,9 +945,9 @@ export function createThreadOutboxDelivery(): { currentConfig.providers, ); if (queuedMessage.compactBeforeSend === true) { - if (!hasAccess()) return true; + if (!hasAccess()) return; // Queued, since steering a running turn with a compaction is rejected. - const compactionResult = await runDeliveryCommand(threadEnvironment.startTurn, { + const compactionResult = yield* deliveryCommand(threadEnvironment.startTurn, { environmentId: queuedMessage.environmentId, input: { commandId: settingsCommandId(queuedMessage, "compact"), @@ -940,16 +968,16 @@ export function createThreadOutboxDelivery(): { resendAfterSocketLoss: false, }, }); - if (AsyncResult.isFailure(compactionResult) && !hasAccess()) return true; + if (AsyncResult.isFailure(compactionResult) && !hasAccess()) return; const compactionFailure = reportCommandFailure(queuedMessage, compactionResult, "start-turn"); - if (compactionFailure?.action === "retry") return false; + if (compactionFailure?.action === "retry") return yield* retryDelivery; if (compactionFailure?.action === "restore") { - return restoreQueuedMessage(persistedMessage, compactionFailure.message); + return yield* restoreQueuedMessage(persistedMessage, compactionFailure.message); } - if (!isQueuedMessagePayloadCurrent(persistedMessage, deliveryRevision)) return true; + if (!isQueuedMessagePayloadCurrent(persistedMessage, deliveryRevision)) return; } - if (!hasAccess()) return true; - const deliveryResult = await runDeliveryCommand(threadEnvironment.startTurn, { + if (!hasAccess()) return; + const deliveryResult = yield* deliveryCommand(threadEnvironment.startTurn, { environmentId: queuedMessage.environmentId, input: { commandId: queuedMessage.commandId, @@ -981,43 +1009,40 @@ export function createThreadOutboxDelivery(): { resendAfterSocketLoss: false, }, }); - if (AsyncResult.isFailure(deliveryResult) && !hasAccess()) return true; + if (AsyncResult.isFailure(deliveryResult) && !hasAccess()) return; const failure = reportCommandFailure(queuedMessage, deliveryResult, "start-turn"); if (failure?.action === "retry") { - return false; + return yield* retryDelivery; } if (failure?.action === "restore") { - return restoreQueuedMessage(persistedMessage, failure.message); + return yield* restoreQueuedMessage(persistedMessage, failure.message); } acknowledgedExistingThreadMessageIds.add(persistedMessage.messageId); - const delivered = - (await completeQueuedMessageDelivery( - persistedMessage, - deliveryRevision, - deliveredSequence(deliveryResult), - )) === "removed"; - if (delivered) { - acknowledgedExistingThreadMessageIds.delete(persistedMessage.messageId); - } - return delivered; - }; + const outcome = yield* completeQueuedMessageDelivery( + persistedMessage, + deliveryRevision, + deliveredSequence(deliveryResult), + ); + if (outcome !== "removed") return yield* retryDelivery; + acknowledgedExistingThreadMessageIds.delete(persistedMessage.messageId); + }); - const sendQueuedCreation = async ( + const sendQueuedCreation = Effect.fn("ThreadOutbox.sendQueuedCreation")(function* ( queuedMessage: QueuedThreadMessage, creation: QueuedThreadCreation, projectCwd: string, - ): Promise => { + ) { const hasAccess = () => readEnvironmentScope(queuedMessage.environmentId, AuthOrchestrationOperateScope); - if (!hasAccess()) return true; + if (!hasAccess()) return; const modelSelection = queuedMessage.modelSelection; if (modelSelection === undefined) { - return false; + return yield* retryDelivery; } const serverConfig = appAtomRegistry.get( serverEnvironment.configValueAtom(queuedMessage.environmentId), ); - if (!serverConfig) return false; + if (!serverConfig) return yield* retryDelivery; const settings = resolveQueuedThreadSettings( queuedMessage, { @@ -1033,84 +1058,81 @@ export function createThreadOutboxDelivery(): { settings.modelSelection, creation.projectId, ); - return restoreQueuedMessage(queuedMessage, `${title}. ${detail}`); + return yield* restoreQueuedMessage(queuedMessage, `${title}. ${detail}`); } const runtimeModeDenial = managedRuntimeModeDenial( serverConfig.managedPolicy, settings.runtimeMode, ); if (runtimeModeDenial !== undefined) { - return restoreQueuedMessage(queuedMessage, runtimeModeDenial); + return yield* restoreQueuedMessage(queuedMessage, runtimeModeDenial); } - let prepared: PreparedTurnAttachments; - let persistedMessage: QueuedThreadMessage; - let deliveryRevision: number; - try { - const preparedResult = await prepareQueuedMessageAttachments(queuedMessage); - if (preparedResult.status === "abandoned") { - return true; - } - prepared = preparedResult.prepared; - persistedMessage = preparedResult.persistedMessage; - deliveryRevision = preparedResult.deliveryRevision; - if (appAtomRegistry.get(editingQueuedMessageIdsAtom)[queuedMessage.messageId]) { - await preserveUploadedAttachmentsForEditor(queuedMessage, preparedResult.persistedMessage); - return true; - } - } catch (error) { - if (!hasAccess()) return true; + // Waiting on the shared upload is the one part of a send that may be cut short. + const preparedExit = yield* Effect.exit( + Effect.interruptible(prepareQueuedMessageAttachments(queuedMessage)), + ); + if (Exit.isFailure(preparedExit)) { + if (Cause.hasInterruptsOnly(preparedExit.cause)) return yield* Effect.interrupt; + const error = Cause.squash(preparedExit.cause); + if (!hasAccess()) return; logThreadOutboxUploadFailure(queuedMessage, error); if (!shouldRetryThreadOutboxDelivery(error)) { - return restoreQueuedMessage( + return yield* restoreQueuedMessage( queuedMessage, error instanceof Error ? error.message : "An attachment could not upload.", ); } - return false; + return yield* retryDelivery; + } + if (preparedExit.value.status === "abandoned") return; + const { prepared, persistedMessage, deliveryRevision } = preparedExit.value; + if (isEditingQueuedMessage(queuedMessage.messageId)) { + yield* preserveUploadedAttachmentsForEditor(queuedMessage, persistedMessage); + return; } if (!isQueuedMessagePayloadCurrent(persistedMessage, deliveryRevision)) { - return true; + return; } const currentConfig = appAtomRegistry.get( serverEnvironment.configValueAtom(queuedMessage.environmentId), ); - if (!currentConfig) return false; + if (!currentConfig) return yield* retryDelivery; if (isModelSelectionUnavailable(currentConfig, settings.modelSelection, creation.projectId)) { const { title, detail } = describeUnavailableModelSelection( currentConfig, settings.modelSelection, creation.projectId, ); - return restoreQueuedMessage(persistedMessage, `${title}. ${detail}`); + return yield* restoreQueuedMessage(persistedMessage, `${title}. ${detail}`); } const sendSettings = resolveQueuedThreadSettings( queuedMessage, settings, currentConfig.providers, ); - if (!hasAccess()) return true; + if (!hasAccess()) return; let workspace = creation; const launched = launchedCreations.get(persistedMessage.messageId); if (launched?.revision === deliveryRevision) { workspace = launched.workspace; } else if (creation.pullRequestReference) { - const checkout = await runDeliveryCommand(gitEnvironment.preparePullRequestThread, { + const checkout = yield* deliveryCommand(gitEnvironment.preparePullRequestThread, { environmentId: queuedMessage.environmentId, input: { cwd: projectCwd, reference: creation.pullRequestReference, mode: "worktree" }, }); if (!isQueuedMessagePayloadCurrent(persistedMessage, deliveryRevision) || !hasAccess()) - return true; + return; const failure = reportCommandFailure(queuedMessage, checkout, "start-turn"); - if (failure?.action === "retry") return false; + if (failure?.action === "retry") return yield* retryDelivery; if (failure?.action === "restore") - return restoreQueuedMessage(persistedMessage, failure.message); - if (!AsyncResult.isSuccess(checkout)) return false; + return yield* restoreQueuedMessage(persistedMessage, failure.message); + if (!AsyncResult.isSuccess(checkout)) return yield* retryDelivery; if (!checkout.value.worktreePath || !checkout.value.isOnPullRequestHead) { const { shortLabel } = ( sourceControlClients.findByChangeRequestUrl(creation.pullRequestReference) ?? sourceControlClients.get(undefined) ).changeRequest; - return restoreQueuedMessage( + return yield* restoreQueuedMessage( persistedMessage, `The ${shortLabel} worktree has local changes and could not update to the ${shortLabel} head. Resolve them before trying again.`, ); @@ -1124,7 +1146,7 @@ export function createThreadOutboxDelivery(): { }; } launchedCreations.set(persistedMessage.messageId, { revision: deliveryRevision, workspace }); - const deliveryResult = await runDeliveryCommand(threadEnvironment.startTurn, { + const deliveryResult = yield* deliveryCommand(threadEnvironment.startTurn, { environmentId: queuedMessage.environmentId, input: buildProjectThreadStartTurnInput({ projectId: creation.projectId, @@ -1153,65 +1175,305 @@ export function createThreadOutboxDelivery(): { worktreeBranchName: buildTemporaryWorktreeBranchName(randomHex), }), }); - if (AsyncResult.isFailure(deliveryResult) && !hasAccess()) return true; + if (AsyncResult.isFailure(deliveryResult) && !hasAccess()) return; const failure = reportCommandFailure(queuedMessage, deliveryResult, "start-turn"); if (failure?.action === "retry") { - return false; + return yield* retryDelivery; } if (failure?.action === "restore") { - return restoreQueuedMessage(persistedMessage, failure.message); + return yield* restoreQueuedMessage(persistedMessage, failure.message); } recordPendingThreadCreationOutcome({ kind: "delivered", message: persistedMessage }); - const outcome = await completeQueuedMessageDelivery( + const outcome = yield* completeQueuedMessageDelivery( persistedMessage, deliveryRevision, deliveredSequence(deliveryResult), ); if (outcome === "edited") { - if (appAtomRegistry.get(editingQueuedMessageIdsAtom)[queuedMessage.messageId]) { - return true; - } - return recoverEditedCreationAfterDelivery(persistedMessage); + if (isEditingQueuedMessage(queuedMessage.messageId)) return; + return yield* recoverEditedCreationAfterDelivery(persistedMessage); } - return outcome === "removed"; - }; - - // A pass writes atoms the pass reads (family mounts, the dispatch slot), and the registry - // notifies subscribers synchronously, so drain() can be called from inside a pass. That call - // only asks for another pass; a nested pass would decide from inputs the outer one is still - // acting on and could start a second delivery. - let draining = false; - let drainRequested = false; - function drain(): void { - if (disposed) return; - if (draining) { - drainRequested = true; - return; - } - draining = true; - try { - do { - if (disposed) return; - drainRequested = false; - drainPass(); - } while (drainRequested); - } finally { - draining = false; - } - } + if (outcome !== "removed") return yield* retryDelivery; + }); - function drainPass(): void { + /** The head of one thread's queue, as the inputs stand now. */ + function nextStep( + threadKey: string, + queuedMessages: ReadonlyArray, + inputs: Atom.Type, + ): ThreadDeliveryStep { const { - dispatchingQueuedMessageId, editingQueuedMessageIds, - queuedMessagesByThreadKey, shellStatuses, threads, projects, serverConfigs, connectedEnvironments, operableEnvironments, - } = appAtomRegistry.get(deliveryInputsAtom); + } = inputs; + const nextQueuedMessage = queuedMessages[0]; + if (!nextQueuedMessage) { + return { _tag: "Wait" }; + } + const messageId = nextQueuedMessage.messageId; + if ( + nextQueuedMessage.creation === undefined && + acknowledgedExistingThreadMessageIds.has(messageId) + ) { + if (retryNotBefore(messageId) > Date.now()) { + return { _tag: "Wait", until: retryNotBefore(messageId) }; + } + return { + _tag: "Dispatch", + messageId, + run: removeAcknowledgedExistingThreadMessage( + nextQueuedMessage, + acknowledgedExistingThreadMessageIds, + ), + }; + } + if (editingQueuedMessageIds[messageId]) { + return { _tag: "Wait" }; + } + const blockedRecovery = blockedRecoveries.get(messageId); + if (blockedRecovery) { + if (blockedRecovery.message === nextQueuedMessage) { + return { _tag: "Wait" }; + } + blockedRecoveries.delete(messageId); + blockedRecovery.unsubscribe(); + } + if (retryNotBefore(messageId) > Date.now()) { + return { _tag: "Wait", until: retryNotBefore(messageId) }; + } + let thread = findThread(threads, nextQueuedMessage); + if (thread && scopedThreadKey(thread.environmentId, thread.id) !== threadKey) { + return { _tag: "Wait" }; + } + + const creation = nextQueuedMessage.creation; + const environment = connectedEnvironments.find( + (candidate) => candidate.environmentId === nextQueuedMessage.environmentId, + ); + const shellStatus = shellStatuses.get(nextQueuedMessage.environmentId) ?? "empty"; + const serverConfig = serverConfigs.get(nextQueuedMessage.environmentId); + let familyRead: ThreadFamilyRead | undefined; + if ( + thread === undefined && + creation === undefined && + serverConfig?.shellThreadFamilies === true + ) { + // The family keeps the row readable through dispatch once it loads; the current shell + // generation's read decides whether the thread is gone. + if (!familyMounts.has(nextQueuedMessage.messageId)) { + familyMounts.set( + nextQueuedMessage.messageId, + threadFamilies.mount( + appAtomRegistry, + nextQueuedMessage.environmentId, + nextQueuedMessage.threadId, + ), + ); + } + familyRead = threadFamilyRead( + appAtomRegistry.get( + threadFamilies.familyAtom(nextQueuedMessage.environmentId, nextQueuedMessage.threadId), + ), + ); + if (familyRead === "settled") { + // The rows read at the start of this pass can lag the family that just settled; + // decide from the merged rows as they are now. + thread = findThread( + appAtomRegistry.get(environmentThreadShells.threadShellsAtom), + nextQueuedMessage, + ); + } else if (familyRead === "failed") { + threadFamilies.refresh( + appAtomRegistry, + nextQueuedMessage.environmentId, + nextQueuedMessage.threadId, + ); + } + } + const resendLaunch = + creation !== undefined && + launchedCreations.get(messageId)?.revision === threadOutboxRevision(messageId); + const deliveryAction = resolveThreadOutboxDeliveryAction({ + isCreation: creation !== undefined, + // A resend goes through the same gates as the first launch. + threadExists: thread !== undefined && !resendLaunch, + shellStatus, + environmentConnected: environment?.connectionState === "connected", + ...(familyRead === undefined ? {} : { familyRead }), + }); + if ( + deliveryAction === "wait" && + shellStatus === "live" && + familyRead !== undefined && + familyRead !== "settled" + ) { + // An empty family read changes no merged row, so nothing else would wake the thread. + return { _tag: "Backoff", messageId }; + } + if (deliveryAction === "send" && !operableEnvironments.has(nextQueuedMessage.environmentId)) { + return { _tag: "Wait" }; + } + const dispatchStep = resolveThreadOutboxDispatchStep({ + deliveryAction, + fileAttachments: nextQueuedMessage.attachments.filter( + (attachment) => attachment.type === "file", + ), + serverConfig: serverConfig + ? { + maxFileUploadBytes: + serverConfig.environment.capabilities.fileAttachments?.maxUploadBytes, + } + : null, + }); + if (dispatchStep.step === "wait") { + return { _tag: "Wait" }; + } + if (dispatchStep.step === "retry") { + return { _tag: "Backoff", messageId }; + } + if (dispatchStep.step === "restore") { + const attachmentError = dispatchStep.reason; + return { + _tag: "Dispatch", + messageId, + run: Effect.gen(function* () { + if (!(yield* confirmQueued(nextQueuedMessage)) || isEditingQueuedMessage(messageId)) { + return; + } + yield* restoreQueuedMessage(nextQueuedMessage, attachmentError); + }), + }; + } + const creationProjectCwd = + creation !== undefined + ? (findCreationProject(projects, nextQueuedMessage)?.workspaceRoot ?? + creation.projectCwd ?? + null) + : null; + if (deliveryAction === "send" && creation !== undefined) { + if (!isQueuedThreadCreationSendable(nextQueuedMessage)) { + return { _tag: "Wait" }; + } + if (creationProjectCwd === null && shellStatus !== "live") { + return { _tag: "Wait" }; + } + } + + const removeQueuedMessage = (warning: string) => + storage("remove-unsendable", messageId, () => + removeThreadOutboxMessage(nextQueuedMessage), + ).pipe( + Effect.catch((error) => + Effect.logWarning(warning, { + environmentId: nextQueuedMessage.environmentId, + threadId: nextQueuedMessage.threadId, + messageId, + error: error.cause, + }).pipe(Effect.andThen(retryDelivery)), + ), + Effect.asVoid, + ); + // Confirm durability before sending; optimistic enqueue can still roll back on write failure. + const run = Effect.gen(function* () { + if (!(yield* confirmQueued(nextQueuedMessage))) return; + // An editor that took the message after confirmation owns it; that is not a failure. + if (isEditingQueuedMessage(messageId)) return; + if (deliveryAction === "send") { + const liveThread = findThread( + appAtomRegistry.get(environmentThreadShells.threadShellsAtom), + nextQueuedMessage, + ); + const liveDeliveryAction = resolveThreadOutboxDeliveryAction({ + isCreation: creation !== undefined, + threadExists: liveThread !== undefined && !resendLaunch, + shellStatus, + environmentConnected: environment?.connectionState === "connected", + }); + if (liveDeliveryAction !== "send") return; + } + if (deliveryAction === "remove") { + // Edited since launch, or launched before a restart that lost the revision: hand it back. + return yield* creation !== undefined + ? recoverEditedCreationAfterDelivery(nextQueuedMessage) + : removeQueuedMessage("[thread-outbox] failed to remove message for a missing thread"); + } + if (creation !== undefined) { + return yield* creationProjectCwd !== null + ? sendQueuedCreation(nextQueuedMessage, creation, creationProjectCwd) + : removeQueuedMessage("[thread-outbox] dropped pending task for a missing project"); + } + return yield* thread !== undefined + ? sendQueuedMessage(nextQueuedMessage, thread) + : retryDelivery; + }); + return { _tag: "Dispatch", messageId, run }; + } + + // One fiber per thread with queued messages. Opening a thread's latch wakes its fiber to + // reconsider its head; the fiber closes it before reading the inputs, so no wake is lost. + // An entry outlives its interrupted fiber until that fiber exits, so a thread never has two. + const threadFibers = new Map(); + + const deliverThread = (threadKey: string, wake: Latch.Latch) => + Effect.forever( + Effect.gen(function* () { + yield* wake.close; + const inputs = appAtomRegistry.get(deliveryInputsAtom); + const step = nextStep(threadKey, inputs.queuedMessagesByThreadKey[threadKey] ?? [], inputs); + if (step._tag === "Backoff") { + scheduleRetry(step.messageId); + return; + } + if (step._tag === "Wait") { + if (step.until === undefined) { + yield* wake.await; + return; + } + const now = yield* Clock.currentTimeMillis; + yield* wake.await.pipe( + Effect.timeoutOrElse({ + duration: Math.max(0, step.until - now), + orElse: () => Effect.void, + }), + ); + return; + } + // A send that started finishes, even when its thread leaves the queue meanwhile; only + // its wait on a shared upload stops early. + beginDispatchingQueuedMessage(step.messageId); + const sent = yield* step.run.pipe( + Effect.as(true), + Effect.catchTags({ DeliveryRetry: () => Effect.succeed(false) }), + // A send that throws backs off like any failure, not straight back into the send. + Effect.catchDefect((error) => + Effect.logWarning("[thread-outbox] queued message delivery threw", { + messageId: step.messageId, + error, + }).pipe(Effect.as(false)), + ), + Effect.ensuring(Effect.sync(() => finishDispatchingQueuedMessage(step.messageId))), + Effect.uninterruptible, + ); + if (sent) clearRetry(step.messageId); + else scheduleRetry(step.messageId); + }), + ); + + function wakeThreads(): void { + for (const { wake } of threadFibers.values()) wake.openUnsafe(); + } + + /** Starts a fiber for each thread with queued messages, stops the rest, and wakes them all. */ + function onInputs(): void { + if (disposed) return; + const { queuedMessagesByThreadKey, serverConfigs, connectedEnvironments } = + appAtomRegistry.get(deliveryInputsAtom); + const queued = Object.values(queuedMessagesByThreadKey).flat(); // Failures while an environment was unreachable say nothing about it now; a backoff // started then would hold a message for up to its full delay after reconnecting. const ready = new Set( @@ -1223,30 +1485,19 @@ export function createThreadOutboxDelivery(): { ) .map((environment) => environment.environmentId), ); - for (const message of Object.values(queuedMessagesByThreadKey).flat()) { + for (const message of queued) { if (ready.has(message.environmentId) && !readyEnvironmentIds.has(message.environmentId)) { clearRetry(message.messageId); } } readyEnvironmentIds = ready; - if (dispatchingQueuedMessageId !== null) { - return; - } - const queuedMessageIds = new Set( - Object.values(queuedMessagesByThreadKey) - .flat() - .map((message) => message.messageId), - ); + const queuedMessageIds = new Set(queued.map((message) => message.messageId)); for (const messageId of acknowledgedExistingThreadMessageIds) { - if (!queuedMessageIds.has(messageId)) { - acknowledgedExistingThreadMessageIds.delete(messageId); - } + if (!queuedMessageIds.has(messageId)) acknowledgedExistingThreadMessageIds.delete(messageId); } for (const messageId of launchedCreations.keys()) { - if (!queuedMessageIds.has(messageId)) { - launchedCreations.delete(messageId); - } + if (!queuedMessageIds.has(messageId)) launchedCreations.delete(messageId); } for (const [messageId, release] of familyMounts) { if (!queuedMessageIds.has(messageId)) { @@ -1255,230 +1506,36 @@ export function createThreadOutboxDelivery(): { } } - for (const [threadKey, queuedMessages] of Object.entries(queuedMessagesByThreadKey)) { - const nextQueuedMessage = queuedMessages[0]; - if (!nextQueuedMessage) { - continue; - } - const messageId = nextQueuedMessage.messageId; - if ( - nextQueuedMessage.creation === undefined && - acknowledgedExistingThreadMessageIds.has(messageId) - ) { - if (retryNotBefore(messageId) > Date.now()) { - continue; - } - beginDispatchingQueuedMessage(messageId); - settle( - messageId, - removeAcknowledgedExistingThreadMessage( - nextQueuedMessage, - acknowledgedExistingThreadMessageIds, - ), - ); - return; - } - if (editingQueuedMessageIds[messageId]) { - continue; - } - const blockedRecovery = blockedRecoveries.get(messageId); - if (blockedRecovery) { - if (blockedRecovery.message === nextQueuedMessage) { - continue; - } - blockedRecoveries.delete(messageId); - blockedRecovery.unsubscribe(); - } - if (retryNotBefore(messageId) > Date.now()) { - continue; - } - if ( - queuedAttachmentsStillUploading({ - environmentId: nextQueuedMessage.environmentId, - attachments: nextQueuedMessage.attachments, - states: appAtomRegistry.get(composerAttachmentUploadsAtom), - }) - ) { - continue; - } - - let thread = findThread(threads, nextQueuedMessage); - if (thread && scopedThreadKey(thread.environmentId, thread.id) !== threadKey) { - continue; - } - - const creation = nextQueuedMessage.creation; - const environment = connectedEnvironments.find( - (candidate) => candidate.environmentId === nextQueuedMessage.environmentId, - ); - const shellStatus = shellStatuses.get(nextQueuedMessage.environmentId) ?? "empty"; - const serverConfig = serverConfigs.get(nextQueuedMessage.environmentId); - let familyRead: ThreadFamilyRead | undefined; - if ( - thread === undefined && - creation === undefined && - serverConfig?.shellThreadFamilies === true - ) { - // The family keeps the row readable through dispatch once it loads; the current shell - // generation's read decides whether the thread is gone. - if (!familyMounts.has(nextQueuedMessage.messageId)) { - familyMounts.set( - nextQueuedMessage.messageId, - threadFamilies.mount( - appAtomRegistry, - nextQueuedMessage.environmentId, - nextQueuedMessage.threadId, - ), - ); - } - familyRead = threadFamilyRead( - appAtomRegistry.get( - threadFamilies.familyAtom(nextQueuedMessage.environmentId, nextQueuedMessage.threadId), - ), - ); - if (familyRead === "settled") { - // The rows read at the start of this pass can lag the family that just settled; - // decide from the merged rows as they are now. - thread = findThread( - appAtomRegistry.get(environmentThreadShells.threadShellsAtom), - nextQueuedMessage, - ); - } else if (familyRead === "failed") { - threadFamilies.refresh( - appAtomRegistry, - nextQueuedMessage.environmentId, - nextQueuedMessage.threadId, - ); - } - } - const resendLaunch = - creation !== undefined && - launchedCreations.get(messageId)?.revision === threadOutboxRevision(messageId); - const deliveryAction = resolveThreadOutboxDeliveryAction({ - isCreation: creation !== undefined, - // A resend goes through the same gates as the first launch. - threadExists: thread !== undefined && !resendLaunch, - shellStatus, - environmentConnected: environment?.connectionState === "connected", - ...(familyRead === undefined ? {} : { familyRead }), - }); - if ( - deliveryAction === "wait" && - shellStatus === "live" && - familyRead !== undefined && - familyRead !== "settled" - ) { - // An empty family read changes no merged row, so nothing else would wake the drain. - scheduleRetry(messageId); - continue; - } - if (deliveryAction === "send" && !operableEnvironments.has(nextQueuedMessage.environmentId)) { - continue; - } - const dispatchStep = resolveThreadOutboxDispatchStep({ - deliveryAction, - fileAttachments: nextQueuedMessage.attachments.filter( - (attachment) => attachment.type === "file", - ), - serverConfig: serverConfig - ? { - maxFileUploadBytes: - serverConfig.environment.capabilities.fileAttachments?.maxUploadBytes, - } - : null, - }); - if (dispatchStep.step === "wait") { - continue; - } - if (dispatchStep.step === "retry") { - scheduleRetry(messageId); - continue; - } - if (dispatchStep.step === "restore") { - const attachmentError = dispatchStep.reason; - beginDispatchingQueuedMessage(messageId); - settle( - messageId, - confirmThreadOutboxMessageQueued(nextQueuedMessage).then((queued) => - !queued || appAtomRegistry.get(editingQueuedMessageIdsAtom)[messageId] - ? true - : restoreQueuedMessage(nextQueuedMessage, attachmentError), - ), - ); - return; - } - const creationProjectCwd = - creation !== undefined - ? (findCreationProject(projects, nextQueuedMessage)?.workspaceRoot ?? - creation.projectCwd ?? - null) - : null; - if (deliveryAction === "send" && creation !== undefined) { - if (!isQueuedThreadCreationSendable(nextQueuedMessage)) { - continue; - } - if (creationProjectCwd === null && shellStatus !== "live") { - continue; - } - } - - beginDispatchingQueuedMessage(messageId); - const removeQueuedMessage = (warning: string) => - removeThreadOutboxMessage(nextQueuedMessage).then( - () => true, - (error) => { - console.warn(warning, { - environmentId: nextQueuedMessage.environmentId, - threadId: nextQueuedMessage.threadId, - messageId, - error, - }); - return false; - }, - ); - // Confirm durability before sending; optimistic enqueue can still roll back on write failure. - const delivery = confirmThreadOutboxMessageQueued(nextQueuedMessage).then((queued) => { - if (!queued) { - return true; - } - // Re-check editor ownership after confirmation; return true to defer without failure backoff. - if (appAtomRegistry.get(editingQueuedMessageIdsAtom)[messageId]) { - return true; - } - if (deliveryAction === "send") { - const liveThread = findThread( - appAtomRegistry.get(environmentThreadShells.threadShellsAtom), - nextQueuedMessage, - ); - const liveDeliveryAction = resolveThreadOutboxDeliveryAction({ - isCreation: creation !== undefined, - threadExists: liveThread !== undefined && !resendLaunch, - shellStatus, - environmentConnected: environment?.connectionState === "connected", - }); - if (liveDeliveryAction !== "send") { - return true; - } - } - return deliveryAction === "remove" - ? creation !== undefined - ? // Edited since launch, or launched before a restart that lost the revision: hand it back. - recoverEditedCreationAfterDelivery(nextQueuedMessage) - : removeQueuedMessage("[thread-outbox] failed to remove message for a missing thread") - : creation !== undefined - ? creationProjectCwd !== null - ? sendQueuedCreation(nextQueuedMessage, creation, creationProjectCwd) - : removeQueuedMessage("[thread-outbox] dropped pending task for a missing project") - : thread !== undefined - ? sendQueuedMessage(nextQueuedMessage, thread) - : Promise.resolve(false); + for (const [threadKey, entry] of threadFibers) { + if ((queuedMessagesByThreadKey[threadKey]?.length ?? 0) > 0 || entry.retiring) continue; + entry.retiring = true; + if (entry.fiber !== undefined) Effect.runFork(Fiber.interrupt(entry.fiber)); + } + for (const [threadKey, messages] of Object.entries(queuedMessagesByThreadKey)) { + if (messages.length === 0 || threadFibers.has(threadKey)) continue; + // Registered before the fiber starts: its first step runs synchronously and can re-enter here. + const entry: ThreadFiber = { + wake: Latch.makeUnsafe(true), + fiber: undefined, + retiring: false, + }; + threadFibers.set(threadKey, entry); + const fiber = Effect.runFork(deliverThread(threadKey, entry.wake)); + entry.fiber = fiber; + fiber.addObserver(() => { + if (threadFibers.get(threadKey) !== entry) return; + threadFibers.delete(threadKey); + // A message queued while this fiber finished its send starts the next fiber now. + if (!disposed) onInputs(); }); - settle(messageId, delivery); - return; + if (entry.retiring) Effect.runFork(Fiber.interrupt(fiber)); } + wakeThreads(); } - const stopDraining = appAtomRegistry.subscribe(deliveryInputsAtom, drain, { immediate: true }); + const stopDelivering = appAtomRegistry.subscribe(deliveryInputsAtom, onInputs, { + immediate: true, + }); const stopCollectingOutcomes = appAtomRegistry.subscribe( settledCreationOutcomeKeysAtom, (threadKeys) => { @@ -1488,12 +1545,15 @@ export function createThreadOutboxDelivery(): { ); return { - drain, + drain: wakeThreads, dispose: () => { disposed = true; - stopDraining(); + stopDelivering(); stopCollectingOutcomes(); - for (const retry of retries.values()) clearTimeout(retry.timer); + for (const { fiber } of threadFibers.values()) { + if (fiber !== undefined) Effect.runFork(Fiber.interrupt(fiber)); + } + threadFibers.clear(); retries.clear(); appAtomRegistry.set(retryingQueuedMessageIdsAtom, {}); for (const blocked of blockedRecoveries.values()) blocked.unsubscribe(); diff --git a/apps/mobile/src/state/use-thread-composer-state.ts b/apps/mobile/src/state/use-thread-composer-state.ts index f8f836dd77e9b..58f2da087ea41 100644 --- a/apps/mobile/src/state/use-thread-composer-state.ts +++ b/apps/mobile/src/state/use-thread-composer-state.ts @@ -122,7 +122,7 @@ import { } from "./use-thread-detail"; import { useThreadSelection } from "./use-thread-selection"; import { enqueueThreadOutboxMessage } from "./thread-outbox"; -import { dispatchingQueuedMessageIdAtom, useThreadOutboxMessages } from "./use-thread-outbox"; +import { dispatchingQueuedMessageIdsAtom, useThreadOutboxMessages } from "./use-thread-outbox"; import { threadEnvironment } from "./threads"; import { useAtomCommand } from "./use-atom-command"; @@ -217,7 +217,7 @@ export function useThreadComposerState(input: { const selectedThreadVisibleTurnItems = useSelectedThreadVisibleTurnItems(); const acknowledgedMessages = useAtomValue(acknowledgedThreadMessagesAtom); const queuedMessagesByThreadKey = useThreadOutboxMessages(); - const dispatchingQueuedMessageId = useAtomValue(dispatchingQueuedMessageIdAtom); + const dispatchingQueuedMessageIds = useAtomValue(dispatchingQueuedMessageIdsAtom); const [feedbackSubmissionsByThreadKey, setFeedbackSubmissionsByThreadKey] = useState< Record> >({}); @@ -240,6 +240,10 @@ export function useThreadComposerState(input: { (message) => message.creation === undefined, ) : []; + // The thread sends one message at a time, the head of its queue. + const dispatchingQueuedMessageId = + selectedThreadQueuedMessages.find((message) => dispatchingQueuedMessageIds[message.messageId]) + ?.messageId ?? null; const feedbackSubmissions = selectedThreadKey ? (feedbackSubmissionsByThreadKey[selectedThreadKey] ?? []) : []; diff --git a/apps/mobile/src/state/use-thread-outbox.ts b/apps/mobile/src/state/use-thread-outbox.ts index bc1f3ea149e1b..8edb8e033b361 100644 --- a/apps/mobile/src/state/use-thread-outbox.ts +++ b/apps/mobile/src/state/use-thread-outbox.ts @@ -26,10 +26,10 @@ export const editingQueuedMessageIdsAtom = Atom.make(null).pipe( - Atom.keepAlive, - Atom.withLabel("mobile:thread-outbox:dispatching-message-id"), -); +/** Messages being sent now: at most one per thread, the head of its queue. */ +export const dispatchingQueuedMessageIdsAtom = Atom.make< + Readonly>> +>({}).pipe(Atom.keepAlive, Atom.withLabel("mobile:thread-outbox:dispatching-message-ids")); /** Messages waiting out a retry backoff, which can repeat for as long as the failure lasts. */ export const retryingQueuedMessageIdsAtom = Atom.make>>>( From 1da6b14e92218f8b378d95c62223c4ca37b020dc Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 12:15:40 -0800 Subject: [PATCH 036/253] refactor(client-runtime): share the Effect attachment upload registry with web (#37743) Co-authored-by: Claude Opus 5.5 --- apps/mobile/src/lib/attachmentUpload.test.ts | 31 +- apps/mobile/src/lib/attachmentUpload.ts | 81 ++-- .../lib/composerAttachmentUploadQueue.test.ts | 246 ----------- .../src/lib/composerAttachmentUploadQueue.ts | 202 +-------- apps/mobile/src/state/attachments.test.ts | 50 ++- .../state/composer-attachment-uploads.test.ts | 22 +- .../src/state/composer-attachment-uploads.ts | 73 +++- apps/mobile/src/state/thread-outbox-model.ts | 2 +- .../threadOutboxDelivery.permissions.test.ts | 6 +- apps/web/src/lib/attachmentUploadQueue.ts | 390 ++++++++---------- packages/client-runtime/package.json | 4 + .../src/state/attachmentUploads.test.ts | 301 ++++++++++++++ .../src/state/attachmentUploads.ts | 240 +++++++++++ .../src/state/attachments.test.ts | 135 +++--- .../client-runtime/src/state/attachments.ts | 162 ++++---- 15 files changed, 1038 insertions(+), 907 deletions(-) create mode 100644 packages/client-runtime/src/state/attachmentUploads.test.ts create mode 100644 packages/client-runtime/src/state/attachmentUploads.ts diff --git a/apps/mobile/src/lib/attachmentUpload.test.ts b/apps/mobile/src/lib/attachmentUpload.test.ts index fb7fa933fbca9..9c58c9a3b05f6 100644 --- a/apps/mobile/src/lib/attachmentUpload.test.ts +++ b/apps/mobile/src/lib/attachmentUpload.test.ts @@ -93,7 +93,6 @@ vi.mock("expo-file-system", () => ({ import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; import * as Fiber from "effect/Fiber"; -import { uploadError } from "./composerAttachmentUploadQueue"; import { prepareTurnAttachments as prepareWithUploads, releasePendingAttachmentUploads, @@ -109,30 +108,14 @@ function prepare( ) { return prepareWithUploads({ ...input, - // An interrupted send waits for its transfer to settle, as the shared registry does. uploadAttachment: (attachment) => - Effect.suspend(() => { - const controller = new AbortController(); - const upload = uploadComposerAttachment( - input.environmentId, - attachment, - controller.signal, - input.onUploadProgress - ? (progress) => input.onUploadProgress?.(attachment.id, progress) - : undefined, - ); - return Effect.tryPromise({ try: () => upload, catch: uploadError }).pipe( - Effect.onInterrupt(() => - Effect.promise(() => { - controller.abort(); - return upload.then( - () => undefined, - () => undefined, - ); - }), - ), - ); - }), + uploadComposerAttachment( + input.environmentId, + attachment, + input.onUploadProgress + ? (progress) => input.onUploadProgress?.(attachment.id, progress) + : undefined, + ), }); } import type { DraftComposerAttachment } from "./composerImages"; diff --git a/apps/mobile/src/lib/attachmentUpload.ts b/apps/mobile/src/lib/attachmentUpload.ts index b4bf8b0aeb472..ab102a059934e 100644 --- a/apps/mobile/src/lib/attachmentUpload.ts +++ b/apps/mobile/src/lib/attachmentUpload.ts @@ -32,7 +32,10 @@ import { } from "./composerImages"; import { imageMimeType } from "@t3tools/shared/image"; import { uuidv4 } from "./uuid"; -import { type ComposerAttachmentUploadError, uploadError } from "./composerAttachmentUploadQueue"; +import { + attachmentUploadError, + type AttachmentUploadError, +} from "@t3tools/client-runtime/state/attachment-uploads"; export type UploadedMobileAttachment = | UploadChatImageAttachment @@ -257,40 +260,46 @@ function requireUploadAccess(environmentId: EnvironmentId): void { } /** - * Uploads one attachment and resolves to its uploaded id, reusing an earlier upload the server - * still has. An abort deletes the pending upload it created. + * Uploads one attachment and returns its uploaded id, reusing an earlier upload the server + * still has. Interrupting it aborts the native transfer and deletes the pending upload it minted; + * a failed transfer deletes it too. */ -export async function uploadComposerAttachment( +export const uploadComposerAttachment = Effect.fn("uploadComposerAttachment")(function* ( environmentId: EnvironmentId, attachment: DraftComposerAttachment, - signal: AbortSignal, onProgress?: (progress: number) => void, -): Promise { - requireUploadAccess(environmentId); +) { + yield* Effect.try({ + try: () => requireUploadAccess(environmentId), + catch: attachmentUploadError, + }); if ( attachment.uploadEnvironmentId === environmentId && attachment.uploadedAttachmentId !== undefined ) { - const verification = await verifyPersistedAttachmentUpload({ + const verification = yield* verifyPersistedAttachmentUpload({ registry: appAtomRegistry, createAssetUrl: assetEnvironment.createUrl, environmentId, attachmentId: attachment.uploadedAttachmentId, }); - if (verification.status === "failed") throw verification.error; + if (verification.status === "failed") { + return yield* attachmentUploadError(verification.error); + } if (verification.status === "verified") return attachment.uploadedAttachmentId; } - if (signal.aborted) throw new Error("Upload cancelled."); - requireUploadAccess(environmentId); + yield* Effect.try({ + try: () => requireUploadAccess(environmentId), + catch: attachmentUploadError, + }); if ( Option.isNone( appAtomRegistry.get(environmentSession.preparedConnectionValueAtom(environmentId)), ) ) { - throw new Error("The environment is not connected."); + return yield* attachmentUploadError(new Error("The environment is not connected.")); } - let minted: string | null = null; - const result = await runAttachmentUploadCycle({ + const result = yield* runAttachmentUploadCycle({ registry: appAtomRegistry, createUploadUrl: attachmentEnvironment.createUploadUrl, remove: attachmentEnvironment.remove, @@ -298,7 +307,7 @@ export async function uploadComposerAttachment( upload: attachmentUploadInput(attachment), // Resolve at transfer time; reconnects may change the base URL. resolveUploadUrl: (relativeUrl) => { - requireUploadAccess(environmentId); + if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) return null; const connection = appAtomRegistry.get( environmentSession.preparedConnectionValueAtom(environmentId), ); @@ -306,22 +315,27 @@ export async function uploadComposerAttachment( ? null : resolveAssetUrl(connection.value.httpBaseUrl, relativeUrl); }, - transport: (url) => ({ - done: uploadFileBytes(environmentId, attachment, url, signal, onProgress), - abort: () => {}, - }), - onMinted: (attachmentId) => { - if (signal.aborted) return "cancel"; - minted = attachmentId; - return "continue"; - }, + transport: (url) => + Effect.tryPromise({ + try: (signal) => uploadFileBytes(environmentId, attachment, url, signal, onProgress), + catch: attachmentUploadError, + }), }); if (result.status === "uploaded") return result.attachmentId; - if (minted !== null) await releaseCreatedUploadsQuietly(environmentId, [minted]); - throw result.status === "failed" && result.error !== undefined - ? result.error - : new Error(`Upload failed for '${attachment.name}'.`); -} + if (result.attachmentId !== null) { + yield* Effect.promise(() => + releaseCreatedUploadsQuietly(environmentId, [result.attachmentId!]), + ); + } + return yield* attachmentUploadError( + result.step === "resolve-url" && + !readEnvironmentScope(environmentId, AuthOrchestrationOperateScope) + ? new Error("This connection cannot upload attachments.") + : result.error instanceof Error + ? result.error + : new Error(`Upload failed for '${attachment.name}'.`), + ); +}); /** * Turns a message's attachments into wire references. `uploadAttachment` is the shared upload @@ -335,7 +349,7 @@ export const prepareTurnAttachments = Effect.fn("prepareTurnAttachments")(functi readonly supportsImageUploads?: boolean; readonly uploadAttachment: ( attachment: DraftComposerAttachment, - ) => Effect.Effect; + ) => Effect.Effect; readonly persistUploadedReferences?: ( draftAttachments: ReadonlyArray, ) => Effect.Effect<"persisted" | "abandon">; @@ -349,7 +363,7 @@ export const prepareTurnAttachments = Effect.fn("prepareTurnAttachments")(functi (attachment): attachment is DraftComposerImageAttachment => attachment.type === "image", ), ), - catch: uploadError, + catch: attachmentUploadError, }); const files = input.attachments.filter((attachment) => attachment.type === "file"); if (input.attachments.length === 0 || (files.length === 0 && !input.supportsImageUploads)) { @@ -362,7 +376,10 @@ export const prepareTurnAttachments = Effect.fn("prepareTurnAttachments")(functi } satisfies PreparedTurnAttachments as PrepareTurnAttachmentsResult; } - yield* Effect.try({ try: () => requireUploadAccess(environmentId), catch: uploadError }); + yield* Effect.try({ + try: () => requireUploadAccess(environmentId), + catch: attachmentUploadError, + }); const uploadedAttachments: UploadedMobileAttachment[] = []; const pendingAttachmentIds: string[] = []; for (const attachment of input.attachments) { diff --git a/apps/mobile/src/lib/composerAttachmentUploadQueue.test.ts b/apps/mobile/src/lib/composerAttachmentUploadQueue.test.ts index 9a65e348a28d0..cfff340939a65 100644 --- a/apps/mobile/src/lib/composerAttachmentUploadQueue.test.ts +++ b/apps/mobile/src/lib/composerAttachmentUploadQueue.test.ts @@ -1,6 +1,4 @@ import { ApprovalRequestId, EnvironmentId, ThreadId } from "@t3tools/contracts"; -import * as Effect from "effect/Effect"; -import * as Fiber from "effect/Fiber"; import { describe, expect, it } from "@effect/vitest"; import { vi } from "vite-plus/test"; import { @@ -14,9 +12,7 @@ import { composerAttachmentsStillUploading, composerAttachmentUploadKey, composerDraftEnvironmentId, - createComposerAttachmentUploadQueue, type ComposerAttachmentUploadRequest, - type ComposerAttachmentUploadState, } from "./composerAttachmentUploadQueue"; const environmentId = EnvironmentId.make("environment-1"); @@ -34,248 +30,6 @@ function request(id: string, environment = environmentId): ComposerAttachmentUpl }; } -describe("composer attachment upload queue", () => { - it("bounds concurrency, deduplicates updates, and drains all attachments", async () => { - const gates = new Map>>(); - const fourthStarted = Promise.withResolvers(); - const firstThreeStarted = Promise.withResolvers(); - let active = 0; - let maximum = 0; - const upload = vi.fn(async (input: ComposerAttachmentUploadRequest) => { - active += 1; - maximum = Math.max(maximum, active); - const gate = Promise.withResolvers(); - gates.set(input.attachment.id, gate); - if (gates.size === 3) firstThreeStarted.resolve(); - if (gates.size === 4) fourthStarted.resolve(); - try { - return await gate.promise; - } finally { - active -= 1; - } - }); - const queue = createComposerAttachmentUploadQueue({ upload, onChange: () => {} }); - const requests = [request("one"), request("two"), request("three"), request("four")]; - queue.sync(requests); - queue.sync(requests); - await firstThreeStarted.promise; - expect(upload).toHaveBeenCalledTimes(3); - gates.get("one")!.resolve("uploaded"); - await fourthStarted.promise; - for (const gate of gates.values()) gate.resolve("uploaded"); - await queue.settled(); - queue.sync(requests); - await queue.settled(); - expect(maximum).toBe(3); - expect(upload).toHaveBeenCalledTimes(4); - queue.dispose(); - }); - - it("cancels on disconnect and resumes from the same local draft on reconnect", async () => { - const started = Promise.withResolvers(); - let states: Readonly> = {}; - let signal: AbortSignal | undefined; - const upload = vi.fn( - async (_request: ComposerAttachmentUploadRequest, currentSignal: AbortSignal) => { - signal = currentSignal; - started.resolve(); - return new Promise((resolve) => - currentSignal.addEventListener("abort", () => resolve("aborted"), { once: true }), - ); - }, - ); - const queue = createComposerAttachmentUploadQueue({ - upload, - onChange: (next) => { - states = next; - }, - }); - const local = request("offline-draft"); - queue.sync([local]); - await started.promise; - queue.sync([]); - await queue.settled(); - expect(signal?.aborted).toBe(true); - expect(states).toEqual({}); - upload.mockResolvedValueOnce("uploaded"); - queue.sync([local]); - await queue.settled(); - expect(upload.mock.calls[1]?.[0]).toBe(local); - expect(states[composerAttachmentUploadKey(environmentId, local.attachment.id)]).toEqual({ - status: "ready", - }); - expect(local.attachment).toMatchObject({ fileUri: "file:///documents/offline-draft.pdf" }); - queue.dispose(); - }); - - it("retries a failed attachment when the worker restores requests after reconnect", async () => { - let states: Readonly> = {}; - const upload = vi - .fn() - .mockRejectedValueOnce(new Error("Disconnected")) - .mockResolvedValue("uploaded"); - const queue = createComposerAttachmentUploadQueue({ - upload, - onChange: (next) => { - states = next; - }, - }); - const local = request("failed-upload"); - const key = composerAttachmentUploadKey(environmentId, local.attachment.id); - queue.sync([local]); - await queue.settled(); - expect(states[key]?.status).toBe("failed"); - queue.sync([local]); - await queue.settled(); - expect(upload).toHaveBeenCalledTimes(1); - queue.sync([]); - queue.sync([local]); - await queue.settled(); - expect(upload).toHaveBeenCalledTimes(2); - expect(states[key]?.status).toBe("ready"); - queue.dispose(); - }); - - it("ignores a late completion after removal or environment switch", async () => { - const gate = Promise.withResolvers(); - const started = Promise.withResolvers(); - let states: Readonly> = {}; - const upload = vi.fn(async () => { - started.resolve(); - return gate.promise; - }); - const queue = createComposerAttachmentUploadQueue({ - upload, - onChange: (next) => { - states = next; - }, - }); - queue.sync([request("photo")]); - await started.promise; - upload.mockResolvedValueOnce("uploaded"); - const other = EnvironmentId.make("environment-2"); - queue.sync([request("photo", other)]); - gate.resolve("uploaded"); - await queue.settled(); - expect(states).toEqual({ [composerAttachmentUploadKey(other, "photo")]: { status: "ready" } }); - queue.sync([]); - expect(states).toEqual({}); - queue.dispose(); - }); - - it("restarts a re-added attachment after its aborted transfer finishes settling", async () => { - const firstStarted = Promise.withResolvers(); - const firstSettled = Promise.withResolvers(); - const secondStarted = Promise.withResolvers(); - const secondSettled = Promise.withResolvers(); - let states: Readonly> = {}; - let firstSignal: AbortSignal | undefined; - const upload = vi.fn(async (_request: ComposerAttachmentUploadRequest, signal: AbortSignal) => { - if (!firstSignal) { - firstSignal = signal; - firstStarted.resolve(); - return firstSettled.promise; - } - secondStarted.resolve(); - return secondSettled.promise; - }); - const queue = createComposerAttachmentUploadQueue({ - upload, - onChange: (next) => { - states = next; - }, - }); - const local = request("re-added"); - queue.sync([local]); - await firstStarted.promise; - queue.sync([]); - queue.sync([local]); - expect(firstSignal?.aborted).toBe(true); - expect(upload).toHaveBeenCalledOnce(); - firstSettled.resolve("aborted"); - await secondStarted.promise; - expect(upload).toHaveBeenCalledTimes(2); - secondSettled.resolve("uploaded"); - await queue.settled(); - expect(states[composerAttachmentUploadKey(environmentId, local.attachment.id)]).toEqual({ - status: "ready", - }); - queue.dispose(); - }); - - it("keeps failures stable until retry and reports bounded progress", async () => { - let states: Readonly> = {}; - const progress: number[] = []; - const upload = vi.fn( - async ( - _request: ComposerAttachmentUploadRequest, - _signal: AbortSignal, - report: (value: number) => void, - ): Promise => { - report(0.12); - report(0.13); - report(1.1); - throw new Error("Server unavailable"); - }, - ); - const queue = createComposerAttachmentUploadQueue({ - upload, - onChange: (next) => { - states = next; - const state = next[composerAttachmentUploadKey(environmentId, "file")]; - if (state?.status === "uploading") progress.push(state.progress); - }, - }); - queue.sync([request("file")]); - await queue.settled(); - queue.sync([request("file")]); - expect(upload).toHaveBeenCalledOnce(); - expect(states[composerAttachmentUploadKey(environmentId, "file")]).toEqual({ - status: "failed", - reason: "Server unavailable", - }); - expect(progress).toEqual([0, 0.1, 1]); - upload.mockImplementationOnce(async () => "uploaded"); - queue.retry(environmentId, "file"); - await queue.settled(); - expect(states[composerAttachmentUploadKey(environmentId, "file")]).toEqual({ status: "ready" }); - queue.dispose(); - }); - - it.effect("joins a send to the running upload instead of uploading again", () => - Effect.gen(function* () { - const gate = Promise.withResolvers(); - const started = Promise.withResolvers(); - const uploaded = vi.fn(); - const upload = vi.fn(async () => { - started.resolve(); - return gate.promise; - }); - let states: Readonly> = {}; - const queue = createComposerAttachmentUploadQueue({ - upload, - onUploaded: uploaded, - onChange: (next) => { - states = next; - }, - }); - const local = request("video"); - queue.sync([local]); - yield* Effect.promise(() => started.promise); - // Sending hands the attachment from the draft to the outbox, which uploads it too. - const sent = yield* Effect.forkChild(queue.upload(local), { startImmediately: true }); - queue.sync([]); - gate.resolve("uploaded-1"); - expect(yield* Fiber.join(sent)).toBe("uploaded-1"); - expect(upload).toHaveBeenCalledOnce(); - expect(uploaded).toHaveBeenCalledExactlyOnceWith(local, "uploaded-1"); - yield* Effect.promise(() => queue.settled()); - expect(states).toEqual({}); - queue.dispose(); - }), - ); -}); - describe("draft upload scope and offline submission", () => { it("uploads question drafts in their environment without matching other thread prefixes", () => { const requestId = ApprovalRequestId.make("request:1"); diff --git a/apps/mobile/src/lib/composerAttachmentUploadQueue.ts b/apps/mobile/src/lib/composerAttachmentUploadQueue.ts index 92434c56b391d..7c0e56dae08de 100644 --- a/apps/mobile/src/lib/composerAttachmentUploadQueue.ts +++ b/apps/mobile/src/lib/composerAttachmentUploadQueue.ts @@ -1,14 +1,6 @@ import { EnvironmentId, type ServerConfig } from "@t3tools/contracts"; -import * as Effect from "effect/Effect"; -import * as Equal from "effect/Equal"; -import * as Exit from "effect/Exit"; -import * as Fiber from "effect/Fiber"; -import * as Hash from "effect/Hash"; -import * as RcMap from "effect/RcMap"; -import * as Schema from "effect/Schema"; -import * as Scope from "effect/Scope"; -import * as Semaphore from "effect/Semaphore"; import { clampFileAttachmentUploadBytes } from "@t3tools/client-runtime/state/attachments"; +import type { AttachmentUploadStatus } from "@t3tools/client-runtime/state/attachment-uploads"; import { parseLegacyNewTaskDraftKey } from "../state/new-task-draft-key"; import type { DraftComposerAttachment } from "./composerImages"; @@ -18,10 +10,7 @@ export interface ComposerAttachmentUploadRequest { readonly attachment: DraftComposerAttachment; } -export type ComposerAttachmentUploadState = - | { readonly status: "uploading"; readonly progress: number } - | { readonly status: "ready" } - | { readonly status: "failed"; readonly reason: string }; +export type ComposerAttachmentUploadState = AttachmentUploadStatus; export function composerAttachmentUploadKey( environmentId: EnvironmentId, @@ -113,190 +102,3 @@ export function composerAttachmentsStillUploading(input: { return state?.status !== "ready" && state?.status !== "failed"; }); } - -/** Equal by environment and attachment id; the request rides along for the upload. */ -class UploadKey implements Equal.Equal { - constructor( - readonly id: string, - readonly request: ComposerAttachmentUploadRequest | null, - ) {} - static of(request: ComposerAttachmentUploadRequest): UploadKey { - return new UploadKey( - composerAttachmentUploadKey(request.environmentId, request.attachment.id), - request, - ); - } - [Equal.symbol](that: unknown): boolean { - return that instanceof UploadKey && that.id === this.id; - } - [Hash.symbol](): number { - return Hash.string(this.id); - } -} - -/** - * Every upload of a composer attachment, shared by key: the composer holds the attachments it - * shows or has queued, and a send joins the upload already running instead of starting another. - * An upload is interrupted once nothing holds it. - */ -export function createComposerAttachmentUploadQueue(options: { - readonly upload: ( - request: ComposerAttachmentUploadRequest, - signal: AbortSignal, - onProgress: (progress: number) => void, - ) => Promise; - /** Saves a finished upload's id; the upload is ready once this succeeds. */ - readonly onUploaded?: ( - request: ComposerAttachmentUploadRequest, - uploadedId: string, - ) => Effect.Effect; - readonly onChange: (states: Readonly>) => void; -}) { - let states: Readonly> = {}; - function setState(key: string, state: ComposerAttachmentUploadState | undefined) { - const previous = states[key]; - if ( - previous === state || - (previous?.status === "uploading" && - state?.status === "uploading" && - previous.progress === state.progress) - ) - return; - const next = { ...states }; - if (state) next[key] = state; - else delete next[key]; - states = next; - options.onChange(states); - } - - const permits = Semaphore.makeUnsafe(3); - const running = new Set>(); - // The latest transfer per attachment: a re-added attachment waits for an aborted one to settle. - const transfers = new Map>(); - - /** The native transfer; interrupting it aborts the request and waits for it to settle. */ - const nativeTransfer = (key: UploadKey) => - Effect.suspend(() => { - const controller = new AbortController(); - if (states[key.id]?.status !== "uploading") { - setState(key.id, { status: "uploading", progress: 0 }); - } - const done = options.upload(key.request!, controller.signal, (progress) => { - if (controller.signal.aborted) return; - setState(key.id, { - status: "uploading", - progress: Math.floor(Math.max(0, Math.min(1, progress)) * 20) / 20, - }); - }); - return Effect.tryPromise({ try: () => done, catch: uploadError }).pipe( - Effect.onInterrupt(() => - Effect.sync(() => controller.abort()).pipe( - Effect.andThen(Effect.tryPromise({ try: () => done, catch: uploadError })), - Effect.ignore, - ), - ), - ); - }); - - const transfer = (key: UploadKey, previous: Fiber.Fiber | undefined) => - (previous === undefined ? Effect.void : Fiber.await(previous)).pipe( - Effect.andThen(nativeTransfer(key).pipe(Semaphore.withPermits(permits, 1))), - Effect.tap((uploadedId) => options.onUploaded?.(key.request!, uploadedId) ?? Effect.void), - Effect.tap(() => Effect.sync(() => setState(key.id, { status: "ready" }))), - Effect.tapError((error) => - Effect.sync(() => setState(key.id, { status: "failed", reason: error.message })), - ), - ); - - const scope = Scope.makeUnsafe(); - const uploads = Effect.runSync( - Scope.provide(scope)( - RcMap.make({ - lookup: Effect.fn("ComposerAttachmentUploads.lookup")(function* (key: UploadKey) { - setState(key.id, { status: "uploading", progress: 0 }); - yield* Effect.addFinalizer(() => Effect.sync(() => setState(key.id, undefined))); - const fiber = yield* Effect.forkScoped(transfer(key, transfers.get(key.id))); - transfers.set(key.id, fiber); - running.add(fiber); - fiber.addObserver(() => { - running.delete(fiber); - if (transfers.get(key.id) === fiber) transfers.delete(key.id); - }); - return fiber; - }), - }), - ), - ); - - const holders = new Map(); - let requested: ReadonlyArray = []; - let disposed = false; - const release = (id: string) => { - const holder = holders.get(id); - if (!holder) return; - holders.delete(id); - Effect.runFork(Scope.close(holder, Exit.void)); - }; - - /** Holds exactly these uploads; anything no longer requested and held nowhere else stops. */ - function sync(requests: ReadonlyArray) { - if (disposed) return; - requested = requests; - const keys = new Map(requests.map((request) => [UploadKey.of(request).id, request])); - for (const id of holders.keys()) { - if (!keys.has(id)) release(id); - } - for (const [id, request] of keys) { - if (holders.has(id)) continue; - const holder = Scope.makeUnsafe(); - holders.set(id, holder); - Effect.runSync(Scope.provide(holder)(RcMap.get(uploads, UploadKey.of(request)))); - } - } - - return { - sync, - /** Uploads the attachment, joining its running upload when there is one. */ - upload( - request: ComposerAttachmentUploadRequest, - ): Effect.Effect { - return disposed - ? Effect.fail(uploadError(new Error("Upload cancelled."))) - : RcMap.get(uploads, UploadKey.of(request)).pipe(Effect.flatMap(Fiber.join), Effect.scoped); - }, - retry(environmentId: EnvironmentId, attachmentId: string) { - const id = composerAttachmentUploadKey(environmentId, attachmentId); - if (states[id]?.status !== "failed") return; - Effect.runSync(RcMap.invalidate(uploads, new UploadKey(id, null))); - release(id); - sync(requested); - }, - async settled() { - while (running.size > 0) { - await Effect.runPromise(Fiber.awaitAll([...running])); - } - }, - dispose() { - disposed = true; - holders.clear(); - Effect.runFork(Scope.close(scope, Exit.void)); - states = {}; - options.onChange(states); - }, - }; -} - -export function uploadError(cause: unknown): ComposerAttachmentUploadError { - if (isComposerAttachmentUploadError(cause)) return cause; - return new ComposerAttachmentUploadError({ - message: cause instanceof Error ? cause.message : "Upload failed. Tap to retry.", - cause, - }); -} - -export class ComposerAttachmentUploadError extends Schema.TaggedError()( - "ComposerAttachmentUploadError", - { message: Schema.String, cause: Schema.Defect() }, -) {} - -const isComposerAttachmentUploadError = Schema.is(ComposerAttachmentUploadError); diff --git a/apps/mobile/src/state/attachments.test.ts b/apps/mobile/src/state/attachments.test.ts index 5f727e3304f94..052c5ef045cb1 100644 --- a/apps/mobile/src/state/attachments.test.ts +++ b/apps/mobile/src/state/attachments.test.ts @@ -6,7 +6,10 @@ import { import { runAttachmentUploadCycle } from "@t3tools/client-runtime/state/attachments"; import * as Cause from "effect/Cause"; import { AsyncResult, AtomRegistry } from "effect/reactivity"; -import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; +import { describe, expect, it } from "@effect/vitest"; +import * as Effect from "effect/Effect"; +import * as Fiber from "effect/Fiber"; +import { afterEach, beforeEach, vi } from "vite-plus/test"; const state = vi.hoisted(() => ({ grantedEnvironments: new Set(), @@ -76,24 +79,29 @@ describe("attachment mutation grants", () => { expect(state.remove).toHaveBeenCalledOnce(); }); - it("blocks the upload cycle's asynchronous cleanup after grant revocation", async () => { - state.grantedEnvironments.add("secondary"); - const transport = vi.fn(); - const result = await runAttachmentUploadCycle({ - registry, - ...attachmentEnvironment, - environmentId, - upload: { name: "image.png", mimeType: "image/png", sizeBytes: 1 }, - resolveUploadUrl: () => "https://example.test/upload", - transport, - onMinted: () => { - state.grantedEnvironments.delete("secondary"); - return "cancel"; - }, - }); - expect(result.status).toBe("cancelled"); - expect(state.create).toHaveBeenCalledOnce(); - expect(state.remove).not.toHaveBeenCalled(); - expect(transport).not.toHaveBeenCalled(); - }); + it.effect("blocks the upload cycle's cleanup after grant revocation", () => + Effect.gen(function* () { + state.grantedEnvironments.add("secondary"); + const transferStarted = Promise.withResolvers(); + const cycle = yield* Effect.forkChild( + runAttachmentUploadCycle({ + registry, + ...attachmentEnvironment, + environmentId, + upload: { name: "image.png", mimeType: "image/png", sizeBytes: 1 }, + resolveUploadUrl: () => "https://example.test/upload", + transport: () => + Effect.callback(() => { + state.grantedEnvironments.delete("secondary"); + transferStarted.resolve(); + }), + }), + ); + yield* Effect.promise(() => transferStarted.promise); + // Interrupting deletes the fresh mint, which the revoked grant must refuse. + yield* Fiber.interrupt(cycle); + expect(state.create).toHaveBeenCalledOnce(); + expect(state.remove).not.toHaveBeenCalled(); + }), + ); }); diff --git a/apps/mobile/src/state/composer-attachment-uploads.test.ts b/apps/mobile/src/state/composer-attachment-uploads.test.ts index c931dd1a87aaa..7d8a431150d26 100644 --- a/apps/mobile/src/state/composer-attachment-uploads.test.ts +++ b/apps/mobile/src/state/composer-attachment-uploads.test.ts @@ -143,7 +143,9 @@ vi.mock("@t3tools/client-runtime/state/runtime", async (importOriginal) => ({ ) => native.server.get(query.input.resource.attachmentId) ? { _tag: "Success", value: {} } - : { _tag: "Failure", error: { _tag: "AssetAttachmentNotFoundError" } }, + : (await import("effect/reactivity")).AsyncResult.failure( + (await import("effect/Cause")).fail({ _tag: "AssetAttachmentNotFoundError" }), + ), runAtomCommand: async ( _registry: unknown, command: unknown, @@ -324,6 +326,24 @@ it.effect.each(["decode", "write"] as const)( }), ); +it.effect("uploads again at send time when the server swept a finished upload", () => + Effect.gen(function* () { + const queue = worker(); + yield* Effect.promise(() => queue.settled()); + expect(uploadState()).toEqual({ status: "ready" }); + // The server sweeps pending uploads nothing claimed, here while the draft sat unsent. + native.server.delete("pending-1"); + + const message = queuedMessage(); + yield* Effect.promise(() => enqueueThreadOutboxMessage(message)); + const prepared = yield* prepareQueuedMessageAttachments(message); + expect(prepared.status).toBe("ready"); + if (prepared.status !== "ready") throw new Error("Outbox preparation abandoned"); + expect(prepared.prepared.pendingAttachmentIds).toEqual(["pending-2"]); + expect(native.transfer).toHaveBeenCalledTimes(2); + }), +); + it("keeps an upload running when its message is sent mid-upload and records it on the message", async () => { const transferStarted = Promise.withResolvers(); const finishTransfer = Promise.withResolvers<{ status: number }>(); diff --git a/apps/mobile/src/state/composer-attachment-uploads.ts b/apps/mobile/src/state/composer-attachment-uploads.ts index acc7576a8d1d8..0779d153d3f23 100644 --- a/apps/mobile/src/state/composer-attachment-uploads.ts +++ b/apps/mobile/src/state/composer-attachment-uploads.ts @@ -2,6 +2,11 @@ import { useAtomValue } from "@effect/atom-react"; import { AuthOrchestrationOperateScope, type EnvironmentId } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; import * as Schema from "effect/Schema"; +import { + attachmentUploadError, + createAttachmentUploadRegistry, +} from "@t3tools/client-runtime/state/attachment-uploads"; +import { verifyPersistedAttachmentUpload } from "@t3tools/client-runtime/state/attachments"; import { Atom } from "effect/reactivity"; import { uploadComposerAttachment } from "../lib/attachmentUpload"; @@ -11,14 +16,13 @@ import { type DraftComposerAttachment, } from "../lib/composerImages"; import { - uploadError, composerAttachmentUploadKey, composerDraftEnvironmentId, canUploadComposerAttachment, - createComposerAttachmentUploadQueue, type ComposerAttachmentUploadState, } from "../lib/composerAttachmentUploadQueue"; import { appAtomRegistry } from "./atom-registry"; +import { assetEnvironment } from "./assets"; import { environmentServerConfigsAtom } from "./server"; import { flattenQueuedThreadMessages, @@ -52,7 +56,7 @@ export const composerAttachmentUploadsAtom = Atom.make< const uploadStateAtom = Atom.family((key: string) => Atom.map(composerAttachmentUploadsAtom, (states) => states[key]), ); -let uploadQueue: ReturnType | null = null; +let uploadQueue: ReturnType | null = null; export function useComposerAttachmentUploadState( environmentId: EnvironmentId | undefined, @@ -65,7 +69,7 @@ export function useComposerAttachmentUploadState( export function retryComposerAttachmentUpload(environmentId: EnvironmentId, attachmentId: string) { if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) return; - uploadQueue?.retry(environmentId, attachmentId); + uploadQueue?.retry(composerAttachmentUploadKey(environmentId, attachmentId)); } type ComposerAttachmentUploadRequest = { @@ -162,7 +166,7 @@ const recordQueuedAttachmentUpload = Effect.fn("ComposerAttachments.recordQueued ? null : withUploadedAttachment(current, uploaded), ), - catch: uploadError, + catch: attachmentUploadError, }), { concurrency: "unbounded", discard: true }, ); @@ -226,7 +230,7 @@ const recordComposerAttachmentUpload = Effect.fn("ComposerAttachments.recordUplo yield* recordQueuedAttachmentUpload(environmentId, uploaded); if (!draftsChanged) return; // A failed draft save only costs a re-upload after restart; the outbox saves its own references. - yield* Effect.tryPromise({ try: () => flushComposerDrafts(), catch: uploadError }).pipe( + yield* Effect.tryPromise({ try: () => flushComposerDrafts(), catch: attachmentUploadError }).pipe( Effect.catchIf( (error) => isComposerDraftPersistenceError(error.cause), (error) => @@ -238,29 +242,54 @@ const recordComposerAttachmentUpload = Effect.fn("ComposerAttachments.recordUplo ); }); -function composerAttachmentUploads() { - uploadQueue ??= createComposerAttachmentUploadQueue({ +const createComposerUploadRegistry = () => + createAttachmentUploadRegistry({ + key: (request) => composerAttachmentUploadKey(request.environmentId, request.attachment.id), + environmentId: (request) => request.environmentId, + concurrencyPerEnvironment: 3, onChange: (states) => appAtomRegistry.set(composerAttachmentUploadsAtom, states), - upload: ({ environmentId, attachment }, signal, onProgress) => { - const release = isFileBackedComposerAttachment(attachment) - ? retainComposerAttachmentFileForPreview(attachment) - : undefined; - return uploadComposerAttachment(environmentId, attachment, signal, onProgress).finally(() => - release?.(), - ); - }, + // The file stays on disk while its upload reads it, even if its draft lets go meanwhile. + upload: ({ environmentId, attachment }, reportProgress) => + Effect.acquireUseRelease( + Effect.sync(() => + isFileBackedComposerAttachment(attachment) + ? retainComposerAttachmentFileForPreview(attachment) + : undefined, + ), + () => uploadComposerAttachment(environmentId, attachment, reportProgress), + (release) => Effect.sync(() => release?.()), + ), onUploaded: recordComposerAttachmentUpload, }); + +function composerAttachmentUploads() { + uploadQueue ??= createComposerUploadRegistry(); return uploadQueue; } /** Every send uploads through here, so it joins the composer's upload of the same attachment. */ -export function uploadComposerAttachmentShared( - environmentId: EnvironmentId, - attachment: DraftComposerAttachment, -) { - return composerAttachmentUploads().upload({ environmentId, attachment }); -} +export const uploadComposerAttachmentShared = Effect.fn("ComposerAttachments.uploadForSend")( + function* (environmentId: EnvironmentId, attachment: DraftComposerAttachment) { + const uploads = composerAttachmentUploads(); + const request = { environmentId, attachment }; + const key = composerAttachmentUploadKey(environmentId, attachment.id); + // An upload that finished earlier can outlive the server's pending-upload sweep. + const finishedEarlier = uploads.status(key)?.status === "ready"; + const uploadedId = yield* uploads.upload(request); + if (!finishedEarlier) return uploadedId; + const verification = yield* verifyPersistedAttachmentUpload({ + registry: appAtomRegistry, + createAssetUrl: assetEnvironment.createUrl, + environmentId, + attachmentId: uploadedId, + }); + if (verification.status === "verified") return uploadedId; + if (verification.status === "failed") return yield* attachmentUploadError(verification.error); + // Starting over verifies the saved id again and uploads the bytes when it is gone. + uploads.restart(key); + return yield* uploads.upload(request); + }, +); /** * Uploads draft and queued attachments to their environment as soon as it is connected and diff --git a/apps/mobile/src/state/thread-outbox-model.ts b/apps/mobile/src/state/thread-outbox-model.ts index 59427907c4ebf..2ec0d29eca21a 100644 --- a/apps/mobile/src/state/thread-outbox-model.ts +++ b/apps/mobile/src/state/thread-outbox-model.ts @@ -289,7 +289,7 @@ export function shouldRetryThreadOutboxDelivery(error: unknown): boolean { case "EnvironmentNotRegisteredError": return true; // A shared upload's failure decides by what made the upload fail. - case "ComposerAttachmentUploadError": + case "AttachmentUploadError": return "cause" in error && shouldRetryThreadOutboxDelivery(error.cause); default: break; diff --git a/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts b/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts index 700b58499d691..b8588e0d40082 100644 --- a/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts +++ b/apps/mobile/src/state/threadOutboxDelivery.permissions.test.ts @@ -7,7 +7,7 @@ import { ProviderInstanceId, ThreadId, } from "@t3tools/contracts"; -import { uploadError } from "../lib/composerAttachmentUploadQueue"; +import { attachmentUploadError } from "@t3tools/client-runtime/state/attachment-uploads"; import * as Effect from "effect/Effect"; import * as Cause from "effect/Cause"; import { AsyncResult, type Atom } from "effect/reactivity"; @@ -497,7 +497,9 @@ describe("queued task operation access", () => { Effect.suspend(() => { setOperateGrant("secondary", false); return uploadFails - ? Effect.fail(uploadError(new Error("This connection cannot upload attachments."))) + ? Effect.fail( + attachmentUploadError(new Error("This connection cannot upload attachments.")), + ) : Effect.succeed({ status: "ready", attachments: [], diff --git a/apps/web/src/lib/attachmentUploadQueue.ts b/apps/web/src/lib/attachmentUploadQueue.ts index cf9bbedeb853a..0e4dffcfb193f 100644 --- a/apps/web/src/lib/attachmentUploadQueue.ts +++ b/apps/web/src/lib/attachmentUploadQueue.ts @@ -7,6 +7,10 @@ import { } from "@t3tools/contracts"; import { parseScopedThreadKey } from "@t3tools/client-runtime/environment"; import { resolveAssetUrl } from "@t3tools/client-runtime/state/assets"; +import { + attachmentUploadError, + createAttachmentUploadRegistry, +} from "@t3tools/client-runtime/state/attachment-uploads"; import { deletePendingAttachmentUpload, runAttachmentUploadCycle, @@ -14,6 +18,8 @@ import { type PersistedAttachmentVerification, } from "@t3tools/client-runtime/state/attachments"; import { create } from "zustand"; +import * as Effect from "effect/Effect"; +import * as Fiber from "effect/Fiber"; import * as Option from "effect/Option"; import { AsyncResult } from "effect/reactivity"; @@ -50,19 +56,19 @@ interface UploadJob { readonly previous?: ReadyAttachmentUpload; /** Draft-owned; delete only through releasePersistedAttachmentUpload, never queue cancel/retry. */ readonly persistedAttachmentId?: string; - readonly settled: Promise; - resolveSettled: () => void; - attachmentId: string | null; - cancelled: boolean; - abort: (() => void) | null; - stopWatchingConnection: () => void; } type DraftFileOwner = Pick; -const jobsByImageId = new Map(); -const queue: UploadJob[] = []; -const activeUploadsByEnvironment = new Map(); +/** The upload each image holds; a failed one stays held for its retry. */ +interface HeldUpload { + readonly job: UploadJob; + readonly release: () => void; + readonly stopWatchingConnection: () => void; +} + +const heldUploads = new Map(); +const jobKey = (job: UploadJob) => `${job.environmentId}:${job.image.id}`; function setUploadState(imageId: string, upload: AttachmentUploadState): void { useAttachmentUploadStore.setState((state) => ({ @@ -137,10 +143,11 @@ function uploadBytes(input: { readonly url: string; readonly file: File; readonly mimeType: string; + readonly signal: AbortSignal; readonly onProgress: (progress: number) => void; -}): { readonly done: Promise; readonly abort: () => void } { +}): Promise { const xhr = new XMLHttpRequest(); - const done = new Promise((resolve, reject) => { + return new Promise((resolve, reject) => { xhr.open("POST", input.url, true); xhr.timeout = UPLOAD_TIMEOUT_MS; xhr.setRequestHeader("Content-Type", input.mimeType); @@ -159,32 +166,38 @@ function uploadBytes(input: { xhr.addEventListener("error", () => reject(new Error("Upload failed"))); xhr.addEventListener("timeout", () => reject(new Error("Upload timed out"))); xhr.addEventListener("abort", () => reject(new Error("Upload cancelled"))); + input.signal.addEventListener("abort", () => xhr.abort(), { once: true }); xhr.send(input.file); }); - - return { done, abort: () => xhr.abort() }; } -async function runUpload(job: UploadJob): Promise { - if (!readEnvironmentScope(job.environmentId, AuthOrchestrationOperateScope)) { +const canOperate = (environmentId: EnvironmentId) => + readEnvironmentScope(environmentId, AuthOrchestrationOperateScope); + +/** Records the failure the composer shows, then fails the upload with it. */ +const failUpload = (job: UploadJob, reason: string, attachmentId?: string) => + Effect.sync(() => setUploadState(job.image.id, { status: "failed", environmentId: job.environmentId, - reason: "This connection cannot upload attachments.", + reason, + ...(attachmentId ? { attachmentId } : {}), ...(job.previous ? { previous: job.previous } : {}), - }); - return; + }), + ).pipe(Effect.andThen(Effect.fail(attachmentUploadError(new Error(reason))))); + +/** Uploads one composer attachment, writing the state its composer shows as it goes. */ +const uploadJob = Effect.fn("WebAttachmentUploads.upload")(function* (job: UploadJob) { + if (!canOperate(job.environmentId)) { + return yield* failUpload(job, "This connection cannot upload attachments."); } if (job.persistedAttachmentId) { - const verification = await verifyPersistedAttachmentUpload({ + const verification = yield* verifyPersistedAttachmentUpload({ registry: appAtomRegistry, createAssetUrl: assetEnvironment.createUrl, environmentId: job.environmentId, attachmentId: job.persistedAttachmentId, }); - if (job.cancelled) { - return; - } if (verification.status === "verified") { setUploadState(job.image.id, { status: "ready", @@ -192,7 +205,7 @@ async function runUpload(job: UploadJob): Promise { attachmentId: job.persistedAttachmentId, }); stampDraftFileUpload(job, job.persistedAttachmentId); - return; + return job.persistedAttachmentId; } if (verification.status === "missing" && !job.image.file && job.image.type === "file") { const draftTarget = resolveCurrentFileDraftTarget(job); @@ -207,22 +220,19 @@ async function runUpload(job: UploadJob): Promise { job.persistedAttachmentId, ) ) { + // The draft now shows the file as missing, so the composer has no upload to show. clearUploadState(job.image.id); - return; + return yield* attachmentUploadError(new Error("Uploaded file expired.")); } } if (verification.status === "failed" || !job.image.file) { // Failed-state ids get deleted on retry/release; exclude persisted ids after transient verification failure. - setUploadState(job.image.id, { - status: "failed", - environmentId: job.environmentId, - reason: - verification.status === "missing" - ? "Uploaded file expired. Remove it and attach it again." - : "Uploaded file could not be verified. Retry when the server reconnects.", - ...(job.previous ? { previous: job.previous } : {}), - }); - return; + return yield* failUpload( + job, + verification.status === "missing" + ? "Uploaded file expired. Remove it and attach it again." + : "Uploaded file could not be verified. Retry when the server reconnects.", + ); } } @@ -232,37 +242,15 @@ async function runUpload(job: UploadJob): Promise { : PROVIDER_SEND_TURN_SUPPORTED_IMAGE_MIME_TYPES.find( (supportedMimeType) => supportedMimeType === job.image.mimeType.toLowerCase(), ); - if (!mimeType) { - setUploadState(job.image.id, { - status: "failed", - environmentId: job.environmentId, - reason: "Unsupported image type", - ...(job.previous ? { previous: job.previous } : {}), - }); - return; - } + if (!mimeType) return yield* failUpload(job, "Unsupported image type"); const file = job.image.file; - if (!file) { - setUploadState(job.image.id, { - status: "failed", - environmentId: job.environmentId, - reason: "Original file is no longer available", - ...(job.previous ? { previous: job.previous } : {}), - }); - return; + if (!file) return yield* failUpload(job, "Original file is no longer available"); + if (!canOperate(job.environmentId)) { + return yield* failUpload(job, "This connection cannot upload attachments."); } let lastStep = -1; - if (!readEnvironmentScope(job.environmentId, AuthOrchestrationOperateScope)) { - setUploadState(job.image.id, { - status: "failed", - environmentId: job.environmentId, - reason: "This connection cannot upload attachments.", - ...(job.previous ? { previous: job.previous } : {}), - }); - return; - } - const result = await runAttachmentUploadCycle({ + const result = yield* runAttachmentUploadCycle({ registry: appAtomRegistry, createUploadUrl: attachmentEnvironment.createUploadUrl, remove: attachmentEnvironment.remove, @@ -274,49 +262,33 @@ async function runUpload(job: UploadJob): Promise { sizeBytes: file.size, }, resolveUploadUrl: (relativeUrl) => { - if ( - job.cancelled || - !readEnvironmentScope(job.environmentId, AuthOrchestrationOperateScope) - ) { - return null; - } + if (!canOperate(job.environmentId)) return null; const connection = readPreparedConnection(job.environmentId); return connection ? resolveAssetUrl(connection.httpBaseUrl, relativeUrl) : null; }, transport: (url) => - uploadBytes({ - url, - file, - mimeType, - onProgress: (progress) => { - const step = Math.floor(progress * 20); - if (step === lastStep || job.cancelled) { - return; - } - lastStep = step; - setUploadState(job.image.id, { - status: "uploading", - environmentId: job.environmentId, - progress, - ...(job.previous ? { previous: job.previous } : {}), - }); - }, + Effect.tryPromise({ + try: (signal) => + uploadBytes({ + url, + file, + mimeType, + signal, + onProgress: (progress) => { + const step = Math.floor(progress * 20); + if (step === lastStep || signal.aborted) return; + lastStep = step; + setUploadState(job.image.id, { + status: "uploading", + environmentId: job.environmentId, + progress, + ...(job.previous ? { previous: job.previous } : {}), + }); + }, + }), + catch: attachmentUploadError, }), - onMinted: (attachmentId) => { - if (job.cancelled && readEnvironmentScope(job.environmentId, AuthOrchestrationOperateScope)) { - return "cancel"; - } - job.attachmentId = attachmentId; - return "continue"; - }, - onTransferStart: (abort) => { - job.abort = abort; - }, }); - job.abort = null; - if (result.status === "cancelled" || job.cancelled) { - return; - } if (result.status === "uploaded") { setUploadState(job.image.id, { status: "ready", @@ -327,69 +299,42 @@ async function runUpload(job: UploadJob): Promise { if (job.previous) { deletePendingUpload(job.previous.environmentId, job.previous.attachmentId); } - return; - } - setUploadState(job.image.id, { - status: "failed", - environmentId: job.environmentId, - reason: - result.step === "mint" - ? "Upload could not start" - : result.step === "resolve-url" - ? readEnvironmentScope(job.environmentId, AuthOrchestrationOperateScope) - ? "Not connected" - : "This connection cannot upload attachments." - : result.error instanceof Error - ? result.error.message - : "Upload failed", - ...(result.attachmentId ? { attachmentId: result.attachmentId } : {}), - ...(job.previous ? { previous: job.previous } : {}), - }); -} - -function pumpUploads(): void { - for (let index = 0; index < queue.length;) { - const job = queue[index]!; - const active = activeUploadsByEnvironment.get(job.environmentId) ?? 0; - if (active >= MAX_UPLOADS_PER_ENVIRONMENT) { - index += 1; - continue; - } - - queue.splice(index, 1); - if (job.cancelled) { - continue; - } - activeUploadsByEnvironment.set(job.environmentId, active + 1); - void runUpload(job) - .catch(() => { - if (!job.cancelled) { - setUploadState(job.image.id, { - status: "failed", - environmentId: job.environmentId, - reason: "Upload failed", - ...(job.previous ? { previous: job.previous } : {}), - }); - } - }) - .finally(() => { - if ( - jobsByImageId.get(job.image.id) === job && - readAttachmentUpload(job.image.id)?.status !== "failed" - ) { - job.stopWatchingConnection(); - jobsByImageId.delete(job.image.id); - } - const remaining = (activeUploadsByEnvironment.get(job.environmentId) ?? 1) - 1; - if (remaining > 0) { - activeUploadsByEnvironment.set(job.environmentId, remaining); - } else { - activeUploadsByEnvironment.delete(job.environmentId); - } - job.resolveSettled(); - pumpUploads(); - }); + return result.attachmentId; } + return yield* failUpload( + job, + result.step === "mint" + ? "Upload could not start" + : result.step === "resolve-url" + ? canOperate(job.environmentId) + ? "Not connected" + : "This connection cannot upload attachments." + : result.error instanceof Error + ? result.error.message + : "Upload failed", + result.attachmentId ?? undefined, + ); +}); + +const uploads = createAttachmentUploadRegistry({ + key: jobKey, + environmentId: (job) => job.environmentId, + concurrencyPerEnvironment: MAX_UPLOADS_PER_ENVIRONMENT, + upload: (job) => + uploadJob(job).pipe( + Effect.catchDefect((defect) => + failUpload(job, "Upload failed").pipe( + Effect.ensuring(Effect.logWarning("Composer attachment upload threw", defect)), + ), + ), + ), +}); + +/** Waits for an image's held upload to finish, without keeping it running. */ +function awaitHeldUpload(imageId: string): Effect.Effect { + const held = heldUploads.get(imageId); + const fiber = held === undefined ? undefined : uploads.current(jobKey(held.job)); + return fiber === undefined ? Effect.void : Effect.asVoid(Fiber.await(fiber)); } export function startAttachmentUpload(input: { @@ -398,9 +343,9 @@ export function startAttachmentUpload(input: { readonly draftTarget?: ComposerThreadTarget; readonly force?: boolean; }): void { - if (!readEnvironmentScope(input.environmentId, AuthOrchestrationOperateScope)) return; - const existingJob = jobsByImageId.get(input.image.id); - if (existingJob?.environmentId === input.environmentId) { + if (!canOperate(input.environmentId)) return; + const existingHold = heldUploads.get(input.image.id); + if (existingHold?.job.environmentId === input.environmentId) { return; } @@ -433,14 +378,10 @@ export function startAttachmentUpload(input: { return; } - if (existingJob) { + if (existingHold) { cancelAttachmentUpload(input.image.id); } const previous = existing?.status === "ready" ? existing : existing?.previous; - let resolveSettled: () => void = () => {}; - const settled = new Promise((resolve) => { - resolveSettled = resolve; - }); const job: UploadJob = { image: input.image, environmentId: input.environmentId, @@ -452,65 +393,68 @@ export function startAttachmentUpload(input: { input.image.uploadedAttachmentId ? { persistedAttachmentId: input.image.uploadedAttachmentId } : {}), - settled, - resolveSettled, - attachmentId: null, - cancelled: false, - abort: null, - stopWatchingConnection: () => {}, }; - jobsByImageId.set(input.image.id, job); + setUploadState(input.image.id, { + status: "uploading", + environmentId: input.environmentId, + progress: 0, + ...(previous ? { previous } : {}), + }); const connectionAtom = environmentCatalog.stateAtom(job.environmentId); const isConnected = () => Option.exists( AsyncResult.value(appAtomRegistry.get(connectionAtom)), (state) => state.phase === "connected", ); + const ownsImage = () => heldUploads.get(input.image.id)?.job === job; let wasConnected = isConnected(); - job.stopWatchingConnection = appAtomRegistry.subscribe(connectionAtom, () => { + const stopWatchingConnection = appAtomRegistry.subscribe(connectionAtom, () => { const connected = isConnected(); const reconnected = connected && !wasConnected; wasConnected = connected; if (!reconnected) return; // HTTP failures can follow reconnection; await the attempt and retry only if this job still owns the file. - void job.settled.then(() => { - if ( - jobsByImageId.get(job.image.id) === job && - readAttachmentUpload(job.image.id)?.status === "failed" && - isConnected() - ) { - retryAttachmentUpload(input); - } - }); + Effect.runFork( + awaitHeldUpload(input.image.id).pipe( + Effect.andThen( + Effect.sync(() => { + if ( + ownsImage() && + readAttachmentUpload(input.image.id)?.status === "failed" && + isConnected() + ) { + retryAttachmentUpload(input); + } + }), + ), + ), + ); }); - queue.push(job); - setUploadState(input.image.id, { - status: "uploading", - environmentId: input.environmentId, - progress: 0, - ...(previous ? { previous } : {}), + heldUploads.set(input.image.id, { + job, + release: uploads.hold(job), + stopWatchingConnection, + }); + // A finished upload lets go of its image; a failed one stays held for its retry. + uploads.current(jobKey(job))?.addObserver((exit) => { + if (exit._tag !== "Success" || !ownsImage()) return; + const held = heldUploads.get(input.image.id)!; + heldUploads.delete(input.image.id); + held.stopWatchingConnection(); + held.release(); }); - pumpUploads(); } function cancelAttachmentUpload(imageId: string): void { - const job = jobsByImageId.get(imageId); - if (!job) { + const held = heldUploads.get(imageId); + if (!held) { return; } - job.cancelled = true; - job.stopWatchingConnection(); - jobsByImageId.delete(imageId); - const queuedIndex = queue.indexOf(job); - if (queuedIndex !== -1) { - queue.splice(queuedIndex, 1); - } - job.abort?.(); - if (job.attachmentId) { - deletePendingUpload(job.environmentId, job.attachmentId); - } - job.resolveSettled(); + heldUploads.delete(imageId); + held.stopWatchingConnection(); + // Releasing interrupts a running upload, which aborts its transfer and deletes its mint. + held.release(); } export function releaseAttachmentUpload(imageId: string): void { @@ -543,10 +487,10 @@ export function releasePersistedAttachmentUpload(input: { releaseAttachmentUpload(input.id); return; } - const job = jobsByImageId.get(input.id); + const held = heldUploads.get(input.id); if ( - job?.environmentId === input.environmentId && - job.persistedAttachmentId === input.attachmentId + held?.job.environmentId === input.environmentId && + held.job.persistedAttachmentId === input.attachmentId ) { releaseAttachmentUpload(input.id); } @@ -558,9 +502,12 @@ export function retryAttachmentUpload(input: { readonly image: ComposerImageAttachment | ComposerFileAttachment; readonly draftTarget?: ComposerThreadTarget; }): void { - if (!readEnvironmentScope(input.environmentId, AuthOrchestrationOperateScope)) return; + if (!canOperate(input.environmentId)) return; const previous = readAttachmentUpload(input.image.id); + const held = heldUploads.get(input.image.id); cancelAttachmentUpload(input.image.id); + // The failed upload stays cached until dropped, and the retry must start a fresh one. + if (held) uploads.invalidate(jobKey(held.job)); if (previous?.status === "failed" && previous.attachmentId) { deletePendingUpload(previous.environmentId, previous.attachmentId); } @@ -577,16 +524,21 @@ export function verifyStashedAttachmentUpload(input: { readonly environmentId: EnvironmentId; readonly attachmentId: string; }): Promise { - return verifyPersistedAttachmentUpload({ - registry: appAtomRegistry, - createAssetUrl: assetEnvironment.createUrl, - environmentId: input.environmentId, - attachmentId: input.attachmentId, - }); + return Effect.runPromise( + verifyPersistedAttachmentUpload({ + registry: appAtomRegistry, + createAssetUrl: assetEnvironment.createUrl, + environmentId: input.environmentId, + attachmentId: input.attachmentId, + }), + ); } -export async function awaitAttachmentUploads(imageIds: ReadonlyArray): Promise { - await Promise.all(imageIds.map((imageId) => jobsByImageId.get(imageId)?.settled)); +/** Resolves once these images' uploads finish, fail or are cancelled. */ +export function awaitAttachmentUploads(imageIds: ReadonlyArray): Promise { + return Effect.runPromise( + Effect.forEach(imageIds, awaitHeldUpload, { concurrency: "unbounded", discard: true }), + ); } /** Revalidate ready uploads at send time; a long-lived draft can outlive server cleanup. */ @@ -685,7 +637,7 @@ export function releaseDraftAttachment( environmentId: attachment.uploadEnvironmentId, attachmentId: attachment.uploadedAttachmentId, }); - if (jobsByImageId.has(attachment.id) || readAttachmentUpload(attachment.id)) { + if (heldUploads.has(attachment.id) || readAttachmentUpload(attachment.id)) { releaseAttachmentUpload(attachment.id); } return; diff --git a/packages/client-runtime/package.json b/packages/client-runtime/package.json index d303b27c879f9..77d755190f8af 100644 --- a/packages/client-runtime/package.json +++ b/packages/client-runtime/package.json @@ -175,6 +175,10 @@ "types": "./src/state/attachments.ts", "default": "./src/state/attachments.ts" }, + "./state/attachment-uploads": { + "types": "./src/state/attachmentUploads.ts", + "default": "./src/state/attachmentUploads.ts" + }, "./text-paste": { "types": "./src/textPaste.ts", "default": "./src/textPaste.ts" diff --git a/packages/client-runtime/src/state/attachmentUploads.test.ts b/packages/client-runtime/src/state/attachmentUploads.test.ts new file mode 100644 index 0000000000000..53df58ba9bdb3 --- /dev/null +++ b/packages/client-runtime/src/state/attachmentUploads.test.ts @@ -0,0 +1,301 @@ +import { EnvironmentId } from "@t3tools/contracts"; +import { describe, expect, it } from "@effect/vitest"; +import * as Effect from "effect/Effect"; +import * as Fiber from "effect/Fiber"; +import { vi } from "vite-plus/test"; + +import { + attachmentUploadError, + createAttachmentUploadRegistry, + type AttachmentUploadError, + type AttachmentUploadStatus, +} from "./attachmentUploads.ts"; + +interface UploadRequest { + readonly environmentId: EnvironmentId; + readonly attachment: { readonly id: string; readonly fileUri: string }; +} + +const environmentId = EnvironmentId.make("environment-1"); +const uploadKey = (environment: EnvironmentId, id: string) => `${environment}:${id}`; + +function request(id: string, environment = environmentId): UploadRequest { + return { environmentId: environment, attachment: { id, fileUri: `file:///documents/${id}.pdf` } }; +} + +/** The registry over a promise-based transfer, as each client wires its own. */ +function createQueue(options: { + readonly upload: ( + request: UploadRequest, + signal: AbortSignal, + onProgress: (progress: number) => void, + ) => Promise; + readonly onUploaded?: ( + request: UploadRequest, + uploadedId: string, + ) => Effect.Effect; + readonly onChange: (statuses: Readonly>) => void; + readonly concurrencyPerEnvironment?: number; +}) { + return createAttachmentUploadRegistry({ + key: (candidate) => uploadKey(candidate.environmentId, candidate.attachment.id), + environmentId: (candidate) => candidate.environmentId, + concurrencyPerEnvironment: options.concurrencyPerEnvironment ?? 3, + upload: (candidate, report) => + Effect.tryPromise({ + try: (signal) => options.upload(candidate, signal, report), + catch: attachmentUploadError, + }), + ...(options.onUploaded ? { onUploaded: options.onUploaded } : {}), + onChange: options.onChange, + }); +} + +describe("attachment upload registry", () => { + it("bounds concurrency, deduplicates updates, and drains all attachments", async () => { + const gates = new Map>>(); + const fourthStarted = Promise.withResolvers(); + const firstThreeStarted = Promise.withResolvers(); + let active = 0; + let maximum = 0; + const upload = vi.fn(async (input: UploadRequest) => { + active += 1; + maximum = Math.max(maximum, active); + const gate = Promise.withResolvers(); + gates.set(input.attachment.id, gate); + if (gates.size === 3) firstThreeStarted.resolve(); + if (gates.size === 4) fourthStarted.resolve(); + try { + return await gate.promise; + } finally { + active -= 1; + } + }); + const queue = createQueue({ upload, onChange: () => {} }); + const requests = [request("one"), request("two"), request("three"), request("four")]; + queue.sync(requests); + queue.sync(requests); + await firstThreeStarted.promise; + expect(upload).toHaveBeenCalledTimes(3); + gates.get("one")!.resolve("uploaded"); + await fourthStarted.promise; + for (const gate of gates.values()) gate.resolve("uploaded"); + await queue.settled(); + queue.sync(requests); + await queue.settled(); + expect(maximum).toBe(3); + expect(upload).toHaveBeenCalledTimes(4); + queue.dispose(); + }); + + it("cancels on disconnect and resumes from the same local draft on reconnect", async () => { + const started = Promise.withResolvers(); + let states: Readonly> = {}; + let signal: AbortSignal | undefined; + const upload = vi.fn(async (_request: UploadRequest, currentSignal: AbortSignal) => { + signal = currentSignal; + started.resolve(); + return new Promise((resolve) => + currentSignal.addEventListener("abort", () => resolve("aborted"), { once: true }), + ); + }); + const queue = createQueue({ + upload, + onChange: (next) => { + states = next; + }, + }); + const local = request("offline-draft"); + queue.sync([local]); + await started.promise; + queue.sync([]); + await queue.settled(); + expect(signal?.aborted).toBe(true); + expect(states).toEqual({}); + upload.mockResolvedValueOnce("uploaded"); + queue.sync([local]); + await queue.settled(); + expect(upload.mock.calls[1]?.[0]).toBe(local); + expect(states[uploadKey(environmentId, local.attachment.id)]).toEqual({ + status: "ready", + }); + expect(local.attachment).toMatchObject({ fileUri: "file:///documents/offline-draft.pdf" }); + queue.dispose(); + }); + + it("retries a failed attachment when the worker restores requests after reconnect", async () => { + let states: Readonly> = {}; + const upload = vi + .fn() + .mockRejectedValueOnce(new Error("Disconnected")) + .mockResolvedValue("uploaded"); + const queue = createQueue({ + upload, + onChange: (next) => { + states = next; + }, + }); + const local = request("failed-upload"); + const key = uploadKey(environmentId, local.attachment.id); + queue.sync([local]); + await queue.settled(); + expect(states[key]?.status).toBe("failed"); + queue.sync([local]); + await queue.settled(); + expect(upload).toHaveBeenCalledTimes(1); + queue.sync([]); + queue.sync([local]); + await queue.settled(); + expect(upload).toHaveBeenCalledTimes(2); + expect(states[key]?.status).toBe("ready"); + queue.dispose(); + }); + + it("ignores a late completion after removal or environment switch", async () => { + const gate = Promise.withResolvers(); + const started = Promise.withResolvers(); + let states: Readonly> = {}; + const upload = vi.fn(async () => { + started.resolve(); + return gate.promise; + }); + const queue = createQueue({ + upload, + onChange: (next) => { + states = next; + }, + }); + queue.sync([request("photo")]); + await started.promise; + upload.mockResolvedValueOnce("uploaded"); + const other = EnvironmentId.make("environment-2"); + queue.sync([request("photo", other)]); + gate.resolve("uploaded"); + await queue.settled(); + expect(states).toEqual({ [uploadKey(other, "photo")]: { status: "ready" } }); + queue.sync([]); + expect(states).toEqual({}); + queue.dispose(); + }); + + it("keeps failures stable until retry and reports bounded progress", async () => { + let states: Readonly> = {}; + const progress: number[] = []; + const upload = vi.fn( + async ( + _request: UploadRequest, + _signal: AbortSignal, + report: (value: number) => void, + ): Promise => { + report(0.12); + report(0.13); + report(1.1); + throw new Error("Server unavailable"); + }, + ); + const queue = createQueue({ + upload, + onChange: (next) => { + states = next; + const state = next[uploadKey(environmentId, "file")]; + if (state?.status === "uploading") progress.push(state.progress); + }, + }); + queue.sync([request("file")]); + await queue.settled(); + queue.sync([request("file")]); + expect(upload).toHaveBeenCalledOnce(); + expect(states[uploadKey(environmentId, "file")]).toEqual({ + status: "failed", + reason: "Server unavailable", + }); + expect(progress).toEqual([0, 0.1, 1]); + upload.mockImplementationOnce(async () => "uploaded"); + queue.retry(uploadKey(environmentId, "file")); + await queue.settled(); + expect(states[uploadKey(environmentId, "file")]).toEqual({ status: "ready" }); + queue.dispose(); + }); + + it.effect("joins a send to the running upload instead of uploading again", () => + Effect.gen(function* () { + const gate = Promise.withResolvers(); + const started = Promise.withResolvers(); + const uploaded = vi.fn(); + const upload = vi.fn(async () => { + started.resolve(); + return gate.promise; + }); + let states: Readonly> = {}; + const queue = createQueue({ + upload, + onUploaded: uploaded, + onChange: (next) => { + states = next; + }, + }); + const local = request("video"); + queue.sync([local]); + yield* Effect.promise(() => started.promise); + // Sending hands the attachment from the draft to the outbox, which uploads it too. + const sent = yield* Effect.forkChild(queue.upload(local), { startImmediately: true }); + queue.sync([]); + gate.resolve("uploaded-1"); + expect(yield* Fiber.join(sent)).toBe("uploaded-1"); + expect(upload).toHaveBeenCalledOnce(); + expect(uploaded).toHaveBeenCalledExactlyOnceWith(local, "uploaded-1"); + yield* Effect.promise(() => queue.settled()); + expect(states).toEqual({}); + queue.dispose(); + }), + ); + it.effect("restarts a failed upload when a send joins it instead of rejoining the failure", () => + Effect.gen(function* () { + const upload = vi + .fn() + .mockRejectedValueOnce(new Error("Server unavailable")) + .mockResolvedValue("uploaded-2"); + const queue = createQueue({ upload, onChange: () => {} }); + const local = request("photo"); + queue.sync([local]); + yield* Effect.promise(() => queue.settled()); + expect(queue.status(uploadKey(environmentId, "photo"))?.status).toBe("failed"); + + expect(yield* queue.upload(local)).toBe("uploaded-2"); + expect(upload).toHaveBeenCalledTimes(2); + queue.dispose(); + }), + ); + + it.effect("keeps a replacement upload's status when the upload it replaced is released", () => + Effect.gen(function* () { + const replacement = Promise.withResolvers(); + const upload = vi + .fn() + .mockRejectedValueOnce(new Error("Server unavailable")) + .mockImplementationOnce(() => replacement.promise); + let statuses: Readonly> = {}; + const queue = createQueue({ + upload, + onChange: (next) => { + statuses = next; + }, + }); + const local = request("photo"); + const releaseFailed = queue.hold(local); + yield* Effect.promise(() => queue.settled()); + queue.invalidate(uploadKey(environmentId, "photo")); + const releaseReplacement = queue.hold(local); + releaseFailed(); + expect(statuses[uploadKey(environmentId, "photo")]).toEqual({ + status: "uploading", + progress: 0, + }); + replacement.resolve("uploaded-2"); + yield* Effect.promise(() => queue.settled()); + expect(statuses[uploadKey(environmentId, "photo")]).toEqual({ status: "ready" }); + releaseReplacement(); + queue.dispose(); + }), + ); +}); diff --git a/packages/client-runtime/src/state/attachmentUploads.ts b/packages/client-runtime/src/state/attachmentUploads.ts new file mode 100644 index 0000000000000..a3afa29d24273 --- /dev/null +++ b/packages/client-runtime/src/state/attachmentUploads.ts @@ -0,0 +1,240 @@ +import type { EnvironmentId } from "@t3tools/contracts"; +import * as Effect from "effect/Effect"; +import * as Equal from "effect/Equal"; +import * as Exit from "effect/Exit"; +import * as Fiber from "effect/Fiber"; +import * as Hash from "effect/Hash"; +import * as RcMap from "effect/RcMap"; +import * as Schema from "effect/Schema"; +import * as Scope from "effect/Scope"; +import * as Semaphore from "effect/Semaphore"; + +export class AttachmentUploadError extends Schema.TaggedError()( + "AttachmentUploadError", + { message: Schema.String, cause: Schema.Defect() }, +) {} + +const isAttachmentUploadError = Schema.is(AttachmentUploadError); + +export function attachmentUploadError(cause: unknown): AttachmentUploadError { + if (isAttachmentUploadError(cause)) return cause; + return new AttachmentUploadError({ + message: cause instanceof Error ? cause.message : "Upload failed. Tap to retry.", + cause, + }); +} + +export type AttachmentUploadStatus = + | { readonly status: "uploading"; readonly progress: number } + | { readonly status: "ready" } + | { readonly status: "failed"; readonly reason: string }; + +/** Equal by key; the request rides along for the upload. */ +class UploadKey implements Equal.Equal { + readonly id: string; + readonly request: Request | null; + constructor(id: string, request: Request | null) { + this.id = id; + this.request = request; + } + [Equal.symbol](that: unknown): boolean { + return that instanceof UploadKey && that.id === this.id; + } + [Hash.symbol](): number { + return Hash.string(this.id); + } +} + +/** + * Every upload of an attachment, shared by key. Holders keep an upload running, and anyone who + * needs its result joins the running upload instead of starting another. An upload is + * interrupted once nothing holds it. + */ +export function createAttachmentUploadRegistry(options: { + readonly key: (request: Request) => string; + readonly environmentId: (request: Request) => EnvironmentId; + /** Uploads running at once against one environment; the rest wait their turn. */ + readonly concurrencyPerEnvironment: number; + /** Uploads one attachment and returns its uploaded id; interrupting it cancels the transfer. */ + readonly upload: ( + request: Request, + reportProgress: (progress: number) => void, + ) => Effect.Effect; + /** Saves a finished upload's id; the upload is ready once this succeeds. */ + readonly onUploaded?: ( + request: Request, + uploadedId: string, + ) => Effect.Effect; + readonly onChange?: (statuses: Readonly>) => void; +}) { + let statuses: Readonly> = {}; + function setStatus(key: string, status: AttachmentUploadStatus | undefined) { + const previous = statuses[key]; + if ( + previous === status || + (previous?.status === "uploading" && + status?.status === "uploading" && + previous.progress === status.progress) + ) + return; + const next = { ...statuses }; + if (status) next[key] = status; + else delete next[key]; + statuses = next; + options.onChange?.(statuses); + } + + const permits = new Map(); + const permitsFor = (environmentId: EnvironmentId) => { + let semaphore = permits.get(environmentId); + if (semaphore === undefined) { + semaphore = Semaphore.makeUnsafe(options.concurrencyPerEnvironment); + permits.set(environmentId, semaphore); + } + return semaphore; + }; + const running = new Map>(); + // The entry that owns each key's status: an invalidated entry finishing or closing late must + // not overwrite or clear the status of the entry that replaced it. + const owners = new Map(); + const setOwnStatus = (owner: object, key: string, status: AttachmentUploadStatus | undefined) => { + if (owners.get(key) === owner) setStatus(key, status); + }; + + const transfer = (key: UploadKey, owner: object) => { + const request = key.request!; + return Effect.suspend(() => { + if (statuses[key.id]?.status !== "uploading") { + setOwnStatus(owner, key.id, { status: "uploading", progress: 0 }); + } + return options.upload(request, (progress) => + setOwnStatus(owner, key.id, { + status: "uploading", + progress: Math.floor(Math.max(0, Math.min(1, progress)) * 20) / 20, + }), + ); + }).pipe( + Semaphore.withPermits(permitsFor(options.environmentId(request)), 1), + Effect.tap((uploadedId) => options.onUploaded?.(request, uploadedId) ?? Effect.void), + Effect.tap(() => Effect.sync(() => setOwnStatus(owner, key.id, { status: "ready" }))), + Effect.tapError((error) => + Effect.sync(() => setOwnStatus(owner, key.id, { status: "failed", reason: error.message })), + ), + ); + }; + + const scope = Scope.makeUnsafe(); + const uploads = Effect.runSync( + Scope.provide(scope)( + RcMap.make({ + lookup: Effect.fn("AttachmentUploads.lookup")(function* (key: UploadKey) { + const owner = {}; + owners.set(key.id, owner); + setStatus(key.id, { status: "uploading", progress: 0 }); + yield* Effect.addFinalizer(() => + Effect.sync(() => { + setOwnStatus(owner, key.id, undefined); + if (owners.get(key.id) === owner) owners.delete(key.id); + }), + ); + // Starts in the holder's call, so an upload begins as soon as something holds it. + const fiber = yield* Effect.forkScoped(transfer(key, owner), { + startImmediately: true, + }); + running.set(key.id, fiber); + fiber.addObserver(() => { + if (running.get(key.id) === fiber) running.delete(key.id); + }); + return fiber; + }), + }), + ), + ); + const keyOf = (request: Request) => new UploadKey(options.key(request), request); + + let disposed = false; + /** Keeps the request's upload running until the returned release is called. */ + function hold(request: Request): () => void { + if (disposed) return () => {}; + const holder = Scope.makeUnsafe(); + Effect.runSync(Scope.provide(holder)(RcMap.get(uploads, keyOf(request)))); + let released = false; + return () => { + if (released) return; + released = true; + Effect.runFork(Scope.close(holder, Exit.void)); + }; + } + + const synced = new Map void>(); + let requested: ReadonlyArray = []; + /** Holds exactly these uploads; anything no longer requested and held nowhere else stops. */ + function sync(requests: ReadonlyArray) { + if (disposed) return; + requested = requests; + const keys = new Map(requests.map((request) => [options.key(request), request])); + for (const [id, release] of synced) { + if (keys.has(id)) continue; + synced.delete(id); + release(); + } + for (const [id, request] of keys) { + if (!synced.has(id)) synced.set(id, hold(request)); + } + } + + /** Drops a failed upload so the next holder starts it again. */ + function invalidate(key: string) { + Effect.runSync(RcMap.invalidate(uploads, new UploadKey(key, null))); + } + + /** Drops the key's upload and starts it again for whatever `sync` holds. */ + function restart(key: string) { + invalidate(key); + synced.get(key)?.(); + synced.delete(key); + sync(requested); + } + + return { + hold, + sync, + invalidate, + restart, + /** + * Uploads the request, joining its running or finished upload. A failed one starts again, + * so a retrying send never just rejoins the failure. + */ + upload(request: Request): Effect.Effect { + return Effect.suspend(() => { + if (disposed) return Effect.fail(attachmentUploadError(new Error("Upload cancelled."))); + const key = options.key(request); + if (statuses[key]?.status === "failed") restart(key); + return RcMap.get(uploads, keyOf(request)).pipe(Effect.flatMap(Fiber.join), Effect.scoped); + }); + }, + /** Starts a failed upload that `sync` holds again. */ + retry(key: string) { + if (statuses[key]?.status === "failed") restart(key); + }, + /** The key's running upload, to await it without holding it. */ + current(key: string): Fiber.Fiber | undefined { + return running.get(key); + }, + status(key: string): AttachmentUploadStatus | undefined { + return statuses[key]; + }, + async settled() { + while (running.size > 0) { + await Effect.runPromise(Fiber.awaitAll([...running.values()])); + } + }, + dispose() { + disposed = true; + synced.clear(); + Effect.runFork(Scope.close(scope, Exit.void)); + statuses = {}; + options.onChange?.(statuses); + }, + }; +} diff --git a/packages/client-runtime/src/state/attachments.test.ts b/packages/client-runtime/src/state/attachments.test.ts index a5396dcc0c66c..5a467311ad5a0 100644 --- a/packages/client-runtime/src/state/attachments.test.ts +++ b/packages/client-runtime/src/state/attachments.test.ts @@ -6,6 +6,7 @@ import { type AttachmentDeleteInput, } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; +import * as Fiber from "effect/Fiber"; import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity"; import type { AtomCommand } from "./runtime.ts"; @@ -59,79 +60,87 @@ const uploadInput: AttachmentCreateUploadUrlInput = { }; describe("runAttachmentUploadCycle", () => { - it("deletes the fresh mint when the caller cancels at onMinted", async () => { - removeCalls.length = 0; - const result = await runAttachmentUploadCycle({ - registry, - createUploadUrl: makeCreateUploadUrl("pending-cancelled"), - remove, - environmentId, - upload: uploadInput, - resolveUploadUrl: () => "https://environment.test/upload", - transport: () => { - throw new Error("transport must not run after cancel"); - }, - onMinted: () => "cancel", - }); - - expect(result).toEqual({ status: "cancelled", attachmentId: "pending-cancelled" }); - expect(removeCalls).toEqual(["pending-cancelled"]); - }); - - it("keeps the minted id on transfer failure so the caller can retry or release", async () => { - removeCalls.length = 0; - const result = await runAttachmentUploadCycle({ - registry, - createUploadUrl: makeCreateUploadUrl("pending-failed"), - remove, - environmentId, - upload: uploadInput, - resolveUploadUrl: () => "https://environment.test/upload", - transport: () => ({ - done: Promise.reject(new Error("Upload rejected (413)")), - abort: () => {}, - }), - }); + it.effect("aborts the transfer and deletes the fresh mint when interrupted", () => + Effect.gen(function* () { + removeCalls.length = 0; + const transferStarted = Promise.withResolvers(); + let aborted = false; + const cycle = yield* Effect.forkChild( + runAttachmentUploadCycle({ + registry, + createUploadUrl: makeCreateUploadUrl("pending-cancelled"), + remove, + environmentId, + upload: uploadInput, + resolveUploadUrl: () => "https://environment.test/upload", + transport: () => + Effect.callback(() => { + transferStarted.resolve(); + return Effect.sync(() => { + aborted = true; + }); + }), + }), + ); + yield* Effect.promise(() => transferStarted.promise); + yield* Fiber.interrupt(cycle); + + expect(aborted).toBe(true); + expect(removeCalls).toEqual(["pending-cancelled"]); + }), + ); + + it.effect("keeps the minted id on transfer failure so the caller can retry or release", () => + Effect.gen(function* () { + removeCalls.length = 0; + const result = yield* runAttachmentUploadCycle({ + registry, + createUploadUrl: makeCreateUploadUrl("pending-failed"), + remove, + environmentId, + upload: uploadInput, + resolveUploadUrl: () => "https://environment.test/upload", + transport: () => Effect.fail({ _tag: "UploadRejected", status: 413 } as const), + }); - expect(result).toMatchObject({ - status: "failed", - step: "transfer", - attachmentId: "pending-failed", - }); - expect(removeCalls).toEqual([]); - }); + expect(result).toMatchObject({ + status: "failed", + step: "transfer", + attachmentId: "pending-failed", + }); + expect(removeCalls).toEqual([]); + }), + ); }); describe("verifyPersistedAttachmentUpload", () => { - it("hits the server on every verification instead of reusing a cached failure", async () => { - let lookups = 0; - const assetUrlAtom = Atom.make( - Effect.promise(() => Promise.resolve()).pipe( - Effect.flatMap(() => { - lookups += 1; - return lookups === 1 - ? Effect.fail({ _tag: "TransportError" } as const) - : Effect.succeed({ url: "/api/assets/pending-1" }); - }), - ), - ).pipe(Atom.swr({ staleTime: 60_000 }), Atom.keepAlive); - const liveRegistry = AtomRegistry.make(); - - const verify = () => - verifyPersistedAttachmentUpload({ + it.effect("hits the server on every verification instead of reusing a cached failure", () => + Effect.gen(function* () { + let lookups = 0; + const assetUrlAtom = Atom.make( + Effect.promise(() => Promise.resolve()).pipe( + Effect.flatMap(() => { + lookups += 1; + return lookups === 1 + ? Effect.fail({ _tag: "TransportError" } as const) + : Effect.succeed({ url: "/api/assets/pending-1" }); + }), + ), + ).pipe(Atom.swr({ staleTime: 60_000 }), Atom.keepAlive); + const liveRegistry = AtomRegistry.make(); + + const verify = verifyPersistedAttachmentUpload({ registry: liveRegistry, createAssetUrl: () => assetUrlAtom, environmentId, attachmentId: "pending-1", }); - const first = await verify(); - expect(first).toMatchObject({ status: "failed" }); - - const second = await verify(); - expect(second).toEqual({ status: "verified" }); - expect(lookups).toBe(2); - }); + expect(yield* verify).toMatchObject({ status: "failed" }); + expect(yield* verify).toEqual({ status: "verified" }); + expect(lookups).toBe(2); + }), + ); }); describe("formatAttachmentSize", () => { diff --git a/packages/client-runtime/src/state/attachments.ts b/packages/client-runtime/src/state/attachments.ts index 800393470ffad..24d5b7723c062 100644 --- a/packages/client-runtime/src/state/attachments.ts +++ b/packages/client-runtime/src/state/attachments.ts @@ -6,6 +6,7 @@ import { type AttachmentDeleteInput, type EnvironmentId, } from "@t3tools/contracts"; +import * as Effect from "effect/Effect"; import type { AsyncResult, Atom, AtomRegistry } from "effect/reactivity"; import type { EnvironmentRegistry } from "../connection/registry.ts"; @@ -51,7 +52,7 @@ export type PersistedAttachmentVerification = | { readonly status: "missing" } | { readonly status: "failed"; readonly error: unknown }; -export async function verifyPersistedAttachmentUpload(input: { +export function verifyPersistedAttachmentUpload(input: { readonly registry: AtomRegistry.AtomRegistry; readonly createAssetUrl: (query: { readonly environmentId: EnvironmentId; @@ -61,25 +62,29 @@ export async function verifyPersistedAttachmentUpload(input: { }) => Atom.Atom>; readonly environmentId: EnvironmentId; readonly attachmentId: string; -}): Promise { - const result = await executeAtomQuery( - input.registry, - input.createAssetUrl({ - environmentId: input.environmentId, - input: { resource: { _tag: "attachment", attachmentId: input.attachmentId } }, +}): Effect.Effect { + return Effect.promise(() => + executeAtomQuery( + input.registry, + input.createAssetUrl({ + environmentId: input.environmentId, + input: { resource: { _tag: "attachment", attachmentId: input.attachmentId } }, + }), + // `refresh` forces a server round trip: the asset URL query atom caches + // results (SWR), so a retry right after a transient failure would + // otherwise re-observe the cached failure and never ask the server. + { reportFailure: false, reportDefect: false, refresh: true }, + ), + ).pipe( + Effect.map((result): PersistedAttachmentVerification => { + if (result._tag === "Success") return { status: "verified" }; + const error = squashAtomCommandFailure(result); + return isAssetAttachmentNotFoundFailure(error) + ? { status: "missing" } + : { status: "failed", error }; }), - // `refresh` forces a server round trip: the asset URL query atom caches - // results (SWR), so a retry right after a transient failure would - // otherwise re-observe the cached failure and never ask the server. - { reportFailure: false, reportDefect: false, refresh: true }, + Effect.withSpan("Attachments.verifyPersistedUpload"), ); - if (result._tag === "Success") { - return { status: "verified" }; - } - const error = squashAtomCommandFailure(result); - return isAssetAttachmentNotFoundFailure(error) - ? { status: "missing" } - : { status: "failed", error }; } type AttachmentCreateUploadUrlCommand = AtomCommand< @@ -108,14 +113,8 @@ export function deletePendingAttachmentUpload(input: { ); } -export interface AttachmentByteUpload { - readonly done: Promise; - readonly abort: () => void; -} - export type AttachmentUploadCycleResult = | { readonly status: "uploaded"; readonly attachmentId: string } - | { readonly status: "cancelled"; readonly attachmentId: string | null } | { readonly status: "failed"; readonly step: "mint" | "resolve-url" | "transfer"; @@ -124,65 +123,76 @@ export type AttachmentUploadCycleResult = }; /** - * The cycle never deletes the minted pending upload on failure: callers keep the returned - * `attachmentId` and decide between retry and release. The one exception is `onMinted` - * returning `"cancel"`, where the caller already abandoned the upload and the fresh mint is - * deleted before returning. + * Mints an upload slot and sends the bytes. A failure keeps the minted id in the result so the + * caller decides between retry and release. Interrupting the cycle aborts the transfer and + * deletes the fresh mint, including when the interruption arrives while minting. */ -export async function runAttachmentUploadCycle(input: { +export function runAttachmentUploadCycle(input: { readonly registry: AtomRegistry.AtomRegistry; readonly createUploadUrl: AttachmentCreateUploadUrlCommand; readonly remove: AttachmentRemoveCommand; readonly environmentId: EnvironmentId; readonly upload: AttachmentCreateUploadUrlInput; readonly resolveUploadUrl: (relativeUrl: string) => string | null; - readonly transport: (url: string) => AttachmentByteUpload; - readonly onMinted?: (attachmentId: string) => "continue" | "cancel"; - readonly onTransferStart?: (abort: () => void) => void; -}): Promise { - const minted = await runAtomCommand( - input.registry, - input.createUploadUrl, - { environmentId: input.environmentId, input: input.upload }, - { reportFailure: false }, - ); - if (minted._tag !== "Success") { - return { - status: "failed", - step: "mint", - attachmentId: null, - error: squashAtomCommandFailure(minted), - }; - } - const attachmentId = minted.value.attachmentId; - if (input.onMinted?.(attachmentId) === "cancel") { - deletePendingAttachmentUpload({ - registry: input.registry, - remove: input.remove, - environmentId: input.environmentId, - attachmentId, - }); - return { status: "cancelled", attachmentId }; - } - - const url = input.resolveUploadUrl(minted.value.relativeUrl); - if (!url) { - return { - status: "failed", - step: "resolve-url", - attachmentId, - error: new Error("The environment is not connected."), - }; - } - - const transfer = input.transport(url); - input.onTransferStart?.(transfer.abort); - try { - await transfer.done; - } catch (error) { - return { status: "failed", step: "transfer", attachmentId, error }; - } - return { status: "uploaded", attachmentId }; + /** Sends the bytes; interrupting it aborts the transfer. */ + readonly transport: (url: string) => Effect.Effect; +}): Effect.Effect { + return Effect.uninterruptibleMask((restore) => + Effect.gen(function* () { + const minted = yield* Effect.promise(() => + runAtomCommand( + input.registry, + input.createUploadUrl, + { environmentId: input.environmentId, input: input.upload }, + { reportFailure: false }, + ), + ); + if (minted._tag !== "Success") { + return { + status: "failed", + step: "mint", + attachmentId: null, + error: squashAtomCommandFailure(minted), + } as const; + } + const attachmentId = minted.value.attachmentId; + return yield* restore( + Effect.suspend(() => { + const url = input.resolveUploadUrl(minted.value.relativeUrl); + if (!url) { + return Effect.succeed({ + status: "failed", + step: "resolve-url", + attachmentId, + error: new Error("The environment is not connected."), + }); + } + return input.transport(url).pipe( + Effect.as({ status: "uploaded", attachmentId }), + Effect.catch((error) => + Effect.succeed({ + status: "failed", + step: "transfer", + attachmentId, + error, + }), + ), + ); + }), + ).pipe( + Effect.onInterrupt(() => + Effect.sync(() => + deletePendingAttachmentUpload({ + registry: input.registry, + remove: input.remove, + environmentId: input.environmentId, + attachmentId, + }), + ), + ), + ); + }), + ).pipe(Effect.withSpan("Attachments.runUploadCycle")); } /** From 8bbe7fdd1a2ab217a77aeef561e19beb7bb632eb Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 12:16:12 -0800 Subject: [PATCH 037/253] fix(mobile): keep turn and connection status from shifting the composer area (#37508) Co-authored-by: Claude Opus 5.5 --- .../src/components/ui/AppSymbol.android.tsx | 2 + .../features/threads/ThreadDetailScreen.tsx | 106 ++++-------- .../src/features/threads/ThreadFeed.tsx | 136 ++++++++++++--- .../features/threads/ThreadRouteScreen.tsx | 33 +++- .../threads/floating-working-control.tsx | 160 +++--------------- .../threads/floating-working-status.ts | 48 ++---- .../threads/pending-thread-feed.test.ts | 27 +++ .../features/threads/pending-thread-feed.ts | 67 +++++++- .../threads/thread-context-divider.tsx | 101 ++++++++--- apps/mobile/src/lib/threadActivity.test.ts | 78 +++++++-- apps/mobile/src/lib/threadActivity.ts | 94 ++++++++-- .../chat/MessagesTimeline.browser.test.tsx | 2 +- .../chat/MessagesTimeline.logic.test.ts | 2 +- packages/client-runtime/src/userMessage.ts | 2 +- 14 files changed, 525 insertions(+), 333 deletions(-) diff --git a/apps/mobile/src/components/ui/AppSymbol.android.tsx b/apps/mobile/src/components/ui/AppSymbol.android.tsx index 4a5a16298b956..a98e9d4ba8ea1 100644 --- a/apps/mobile/src/components/ui/AppSymbol.android.tsx +++ b/apps/mobile/src/components/ui/AppSymbol.android.tsx @@ -1,6 +1,7 @@ import type { Icon } from "@tabler/icons-react-native/types"; /* Package-root imports eagerly register the entire Tabler icon set in Metro. */ import IconAdjustmentsHorizontal from "@tabler/icons-react-native/IconAdjustmentsHorizontal"; +import IconAlarm from "@tabler/icons-react-native/IconAlarm"; import IconAlignLeft from "@tabler/icons-react-native/IconAlignLeft"; import IconAlertCircle from "@tabler/icons-react-native/IconAlertCircle"; import IconAlertTriangle from "@tabler/icons-react-native/IconAlertTriangle"; @@ -189,6 +190,7 @@ const ANDROID_ICON_BY_SF_SYMBOL = { "circle.inset.filled": IconCircleDot, "circle.dashed.inset.filled": IconCircleDot, "calendar.badge.clock": IconCalendarClock, + alarm: IconAlarm, clock: IconClock, timer: IconClock, "face.smiling": IconMoodPlus, diff --git a/apps/mobile/src/features/threads/ThreadDetailScreen.tsx b/apps/mobile/src/features/threads/ThreadDetailScreen.tsx index 425a5cecac599..b711889a01008 100644 --- a/apps/mobile/src/features/threads/ThreadDetailScreen.tsx +++ b/apps/mobile/src/features/threads/ThreadDetailScreen.tsx @@ -168,7 +168,6 @@ import { appAtomRegistry } from "../../state/atom-registry"; import { threadStreamHaptics } from "./threadStreamHaptics"; import { useAtomCommand } from "../../state/use-atom-command"; import { orchestrationEnvironment } from "../../state/orchestration"; -import { useDelayedStatus } from "../../lib/useDelayedStatus"; import type { PendingUserInputDraftAnswer } from "@t3tools/client-runtime/state/pending-user-input"; import type { RunStopPresentation } from "@t3tools/client-runtime/state/thread-execution"; import type { @@ -191,6 +190,7 @@ import { FloatingWorkingControl, } from "./floating-working-control"; import { connectionFloatingStatus, type FloatingWorkingStatus } from "./floating-working-status"; +import type { ThreadLifecycleEvent } from "./thread-context-divider"; import { derivePendingUserInputMaxHeight, ESTIMATED_KEYBOARD_HEIGHT, @@ -268,7 +268,6 @@ export interface ThreadDetailScreenProps { readonly activePendingUserInputAnswers: Record> | null; readonly respondingUserInputId: RuntimeRequestId | null; readonly connectionStateLabel: EnvironmentConnectionPhase; - readonly connectionAwaitingApproval?: boolean; readonly historyControls?: ThreadFeedHistoryControls; readonly activeThreadBusy: boolean; readonly canStopThread: boolean; @@ -383,9 +382,6 @@ const NO_RUN_STOP_ATOM = Atom.make(null).pipe( Atom.withLabel("mobile-thread-run-stop:empty"), ); -// Connection failures survive retries and app suspension. -const RECONNECT_ERROR_DELAY_MS = 5_000; - // Anchors, the submitted message, follow state and pending scrolls belong to // one thread, so the body remounts when the thread changes. export function ThreadDetailScreen(props: ThreadDetailScreenProps) { @@ -569,14 +565,6 @@ function ThreadDetailBody( ); const composerKeyboardOffset = composerBottomInset; const contentPresentationKind = props.contentPresentation.kind; - const connectionRetrying = - props.connectionStateLabel === "connecting" || props.connectionStateLabel === "reconnecting"; - const retryError = connectionRetrying ? props.connectionError : null; - const retryConnectionError = useDelayedStatus( - `${props.environmentId}:${retryError !== null}`, - retryError, - RECONNECT_ERROR_DELAY_MS, - ); const interruptThreadTurn = useAtomCommand(threadEnvironment.interruptTurn, { reportFailure: false, }); @@ -750,11 +738,41 @@ function ThreadDetailBody( Dialog.alert("Pi", props.extensionStatuses?.map((status) => status.text).join("\n\n")), } : null; + const lifecycleEvent = ((): ThreadLifecycleEvent | null => { + if (contentPresentationKind !== "ready" || !(lifecycleSnoozed || settled || woke)) return null; + const actionLabel = lifecycleSnoozed ? "Wake now" : settled ? "Un-settle" : "Dismiss"; + const wake = + lifecycleSnoozed && snoozedUntil !== null + ? snoozeWakeLabel(snoozedUntil, { now: lifecycleNow }) + : "now"; + return { + state: lifecycleSnoozed ? "snoozed" : settled ? "settled" : "woke", + label: lifecycleSnoozed + ? wake === "now" + ? "Snoozed" + : `Snoozed, ${wake} left` + : settled + ? "Settled" + : "Woke from snooze", + action: { + label: statusPending + ? lifecycleSnoozed + ? "Waking..." + : settled + ? "Un-settling..." + : "Dismissing..." + : actionLabel, + accessibilityLabel: actionLabel, + disabled: !props.canOperateThread || statusPending, + onPress: () => + void runStatusAction(lifecycleSnoozed ? "wake" : settled ? "unsettle" : "dismiss"), + }, + }; + })(); const floatingStatus = ((): FloatingWorkingStatus | null => { const connectionStatus = connectionFloatingStatus({ - connectionError: connectionRetrying ? retryConnectionError : props.connectionError, + connectionError: props.connectionError, connectionState: props.connectionStateLabel, - connectionAwaitingApproval: props.connectionAwaitingApproval === true, environmentLabel: props.environmentLabel, onReconnect: props.onReconnectEnvironment, }); @@ -810,16 +828,6 @@ function ThreadDetailBody( }, }; } - if (props.isCompacting && contentPresentationKind === "ready") { - return { kind: "compacting" }; - } - if (props.activeWorkStartedAt !== null && contentPresentationKind === "ready") { - return { - kind: "working", - environmentId: props.environmentId, - startedAt: props.activeWorkStartedAt, - }; - } if (pendingBackgroundWork !== null && contentPresentationKind === "ready") { return { kind: "background", @@ -843,34 +851,6 @@ function ThreadDetailBody( }, }; } - if (contentPresentationKind === "ready" && (lifecycleSnoozed || settled || woke)) { - const state = lifecycleSnoozed ? "snoozed" : settled ? "settled" : "woke"; - const label = - lifecycleSnoozed && snoozedUntil !== null - ? `Snoozed, wakes in ${snoozeWakeLabel(snoozedUntil, { now: lifecycleNow })}` - : settled - ? "Settled" - : "Woke from snooze"; - const actionLabel = lifecycleSnoozed ? "Wake now" : settled ? "Un-settle" : "Dismiss"; - return { - kind: "lifecycle", - state, - label, - action: { - label: statusPending - ? lifecycleSnoozed - ? "Waking..." - : settled - ? "Un-settling..." - : "Dismissing..." - : actionLabel, - accessibilityLabel: actionLabel, - disabled: !props.canOperateThread || statusPending, - onPress: () => - void runStatusAction(lifecycleSnoozed ? "wake" : settled ? "unsettle" : "dismiss"), - }, - }; - } if (props.selectedThread.goal !== null && contentPresentationKind === "ready") { const goal = presentProviderGoal(props.selectedThread.goal, false); return { @@ -885,23 +865,12 @@ function ThreadDetailBody( return null; })(); const showWorkingControl = floatingStatus !== null; - // A pending message holds the pill's row for its Starting/Working pill. Worktree setup reports - // progress in the feed instead, so holding the row there only leaves a gap that later closes. - const worktreeSetupInFeed = - props.worktreeSetup?.snapshot.phase === "running" || - (props.creationState?.kind === "preparing" && props.creationState.preparingWorktree); const showFloatingStatus = showWorkingControl || queuedCount > 0 || agentsSegment !== null || devicePreviews.length > 0 || - browserTabs.tabs.length > 0 || - props.connectionStateLabel !== "connected" || - (!worktreeSetupInFeed && - (props.queuedMessages.length > 0 || - props.selectedThreadFeed.some( - (entry) => "acknowledged" in entry && entry.acknowledged === true, - ))); + browserTabs.tabs.length > 0; const selectedThreadFeed = props.selectedThreadFeed; // A palette message result opens the thread at that message: earlier // history loads until the message is in the feed, which then scrolls to it. @@ -1479,6 +1448,7 @@ function ThreadDetailBody( workspaceRoot={props.threadCwd} feed={props.selectedThreadFeed} pendingHandoff={props.pendingHandoff} + lifecycleEvent={lifecycleEvent} worktreeSetup={props.worktreeSetup} setupWorkingStartedAt={props.setupWorkingStartedAt} preparingWorktree={ @@ -1606,11 +1576,7 @@ function ThreadDetailBody( status={floatingStatus} extensionStatus={ floatingStatus !== null && - (floatingStatus.kind === "working" || - floatingStatus.kind === "compacting" || - floatingStatus.kind === "background" || - floatingStatus.kind === "goal" || - floatingStatus.kind === "lifecycle") + (floatingStatus.kind === "background" || floatingStatus.kind === "goal") ? extensionStatus : null } diff --git a/apps/mobile/src/features/threads/ThreadFeed.tsx b/apps/mobile/src/features/threads/ThreadFeed.tsx index 3e1ee80f2831f..be4e05273a999 100644 --- a/apps/mobile/src/features/threads/ThreadFeed.tsx +++ b/apps/mobile/src/features/threads/ThreadFeed.tsx @@ -1,7 +1,10 @@ +import { toServerClock } from "@t3tools/client-runtime/clock"; +import { formatDuration } from "@t3tools/shared/orchestrationTiming"; import { Button } from "../../components/ui/Button"; import * as Dialog from "../../components/ui/Dialog"; import { useOpenPullRequest } from "../../native/useOpenPullRequest"; import { useWorkspaceSettledContentWidth } from "../layout/workspace-content-width"; +import { useVisibleSecondClock } from "./use-visible-second-clock"; import { ThreadMarkdownVideo, ThreadMediaRetainContext, @@ -18,7 +21,11 @@ import { enrichedSkillReference, } from "../../lib/enrichedLinkPresentation"; import { formatProviderSkillDisplayName } from "@t3tools/shared/inlineSkills"; -import { ThreadContextDivider } from "./thread-context-divider"; +import { + ThreadContextDivider, + ThreadLifecycleLine, + type ThreadLifecycleEvent, +} from "./thread-context-divider"; import { ThreadHandoffDivider, ThreadHandoffRow, @@ -243,6 +250,7 @@ import { import { appendGuestPendingMessages, appendPendingThreadMessages, + createPendingTurnAppender, createThreadFeedGrouping, isCompactionCommandFeedEntry, type GroupedThreadFeedRow, @@ -354,6 +362,7 @@ export interface ThreadFeedProps { readonly workspaceRoot?: string | null; readonly feed: ReadonlyArray; readonly pendingHandoff?: HandoffEndpoints | null; + readonly lifecycleEvent?: ThreadLifecycleEvent | null; readonly contentPresentation: ThreadContentPresentation; readonly latestRun: ThreadFeedLatestRun | null; readonly activeWorkStartedAt: string | null; @@ -1682,9 +1691,35 @@ function ThreadAutomationMessageRow(props: { /** Leads with why T3 Code compacted, when it did so without being asked. */ function compactionLabel(label: string, title: string | undefined) { - return title !== undefined && label.startsWith("Context compacted") - ? title + label.slice("Context compacted".length) - : label; + const text = + title !== undefined && label.startsWith("Context compacted") + ? title + label.slice("Context compacted".length) + : label; + // Non-breaking spaces keep "549K → 16.8K tokens" on one line when the label wraps. + return text.replace(/ (→|tokens)/g, "\u00A0$1").replace(/→ /g, "→\u00A0"); +} + +function RunFoldWorking(props: { + environmentId: EnvironmentId; + startedAt: string; + rowSizing: ReturnType; +}) { + const nowMs = toServerClock(props.environmentId, useVisibleSecondClock(true)); + const seconds = Math.floor((nowMs - Date.parse(props.startedAt)) / 1_000); + return ( + + + Working for {seconds > 0 ? formatDuration(seconds * 1_000) : "0s"} + + + ); } function RunFoldButton({ @@ -1785,6 +1820,7 @@ function renderFeedEntry( | "threadId" | "workspaceRoot" > & { + readonly pendingFoldMessageId: string | null; readonly formatMessageTime: (input: string) => string; readonly copiedRowId: string | null; readonly expandedWorkRows: Record; @@ -1844,18 +1880,47 @@ function renderFeedEntry( ); } + if (entry.type === "pending-fold") { + return ( + + + + Pending + + + + ); + } + if (entry.type === "run-fold") { return ( - + {entry.workingSince ? ( + + ) : ( + + )} {entry.checkpointSummary ? ( ) : null} - {entry.pendingMessage && !entry.acknowledged ? "Pending" : timestampLabel} + {entry.pendingMessage && + !entry.acknowledged && + entry.id !== props.pendingFoldMessageId + ? "Pending" + : timestampLabel} {props.onEditPendingMessage !== null && entry.pendingMessage && @@ -3572,10 +3641,16 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { if (jumpReady && revealedMessageId !== jumpToMessageId) { setInteractionState((current) => ({ ...current, revealedMessageId: jumpToMessageId })); } + const [appendPendingTurn] = useState(createPendingTurnAppender); const presentedFeed = appendGuestPendingMessages( - useThreadFeedPresentation(props, expandedTurnIds, expandedWorkGroups, revealedMessageId), + appendPendingTurn( + useThreadFeedPresentation(props, expandedTurnIds, expandedWorkGroups, revealedMessageId), + { zen: props.toolCallVisibility === "zen" }, + ), guestShare, ); + const pendingFoldMessageId = + presentedFeed.findLast((entry) => entry.type === "pending-fold")?.messageId ?? null; const jumpRunId = jumpReady && !presentedFeed.some((entry) => entry.type === "message" && entry.id === jumpToMessageId) @@ -3834,6 +3909,11 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { // corrects each row's height as it mounts — the feed visibly jumps. const getFixedItemSize = (entry: ThreadFeedRow) => { if (entry.type === "guest-pending") return undefined; + if (entry.type === "pending-fold") { + return workRowSizing.fixedRowHeight === undefined + ? undefined + : resolveThreadFeedFixedItemSize("run-fold"); + } if (entry.type === "html-render") { return htmlRenderRowHeight(htmlRenderFrameHeight(entry.render, contentWidth)); } @@ -3924,6 +4004,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { formatMessageTime, environmentId: props.environmentId, dispatchingMessageId: props.dispatchingMessageId, + pendingFoldMessageId, onEditPendingMessage: props.onEditPendingMessage, onUseArtifactTemplate: props.onUseArtifactTemplate, threadId: props.threadId, @@ -4023,6 +4104,8 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { viewabilityConfig={THREAD_MEDIA_VIEWABILITY_CONFIG} keyExtractor={(entry) => entry.id} getItemType={(entry) => { + // Same container as the live fold that replaces it. + if (entry.type === "pending-fold") return "run-fold"; if (entry.type !== "message") return entry.type; if (isCompactionCommandFeedEntry(entry)) return "message:compaction"; if ( @@ -4077,14 +4160,21 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { } ListFooterComponent={ - props.pendingHandoff ? ( - + props.pendingHandoff || props.lifecycleEvent ? ( + <> + {props.pendingHandoff ? ( + + ) : null} + {props.lifecycleEvent ? ( + + ) : null} + ) : null } contentContainerStyle={{ diff --git a/apps/mobile/src/features/threads/ThreadRouteScreen.tsx b/apps/mobile/src/features/threads/ThreadRouteScreen.tsx index 4d814d3a8a4f7..01b6aa551070a 100644 --- a/apps/mobile/src/features/threads/ThreadRouteScreen.tsx +++ b/apps/mobile/src/features/threads/ThreadRouteScreen.tsx @@ -52,6 +52,7 @@ import { EmptyState } from "../../components/ui/EmptyState"; import { LoadingScreen } from "../../components/LoadingScreen"; import { scopedThreadKey } from "../../lib/scopedEntities"; import { useDelayedStatus } from "../../lib/useDelayedStatus"; +import { connectionRetryLabel } from "./floating-working-status"; import { scopeThreadRef } from "@t3tools/client-runtime/environment"; import { useThreadShell } from "../../state/entities"; import { NATIVE_LIQUID_GLASS_SUPPORTED } from "../../native/native-glass"; @@ -136,6 +137,8 @@ function firstRouteParam(value: string | string[] | undefined): string | null { return first === undefined || first.trim().length === 0 ? null : first; } +const RECONNECT_ERROR_DELAY_MS = 5_000; + // The inspector pane's bar belongs to ThreadInspectorHeader; the fallback must not draw its own. const InspectorHeaderShownContext = createContext(false); @@ -961,18 +964,33 @@ function ThreadRouteContent( detailDeleted: selectedThreadDetailState.status === "deleted", connectionState: routeConnectionState, }); - // Shown in the header subtitle so the composer's status slot doesn't jump while history syncs. - const threadSyncLabel = useDelayedStatus( - selectedThread === null ? "" : scopedThreadKey(selectedThread.environmentId, selectedThread.id), - // A thread this client is creating has nothing older to sync. + const connectionRetrying = + routeConnectionState === "connecting" || routeConnectionState === "reconnecting"; + // Connection failures survive retries and app suspension. + const retryConnectionError = useDelayedStatus( + `${environmentId}:${connectionRetrying && routeConnectionError !== null}`, + connectionRetrying ? routeConnectionError : null, + RECONNECT_ERROR_DELAY_MS, + ); + const connectionStatusLabel = connectionRetryLabel({ + connectionError: retryConnectionError, + connectionState: routeConnectionState, + connectionAwaitingApproval: routeEnvironmentRuntime?.connectionAwaitingApproval === true, + }); + // A thread this client is creating has nothing older to sync. + const threadSyncLabel = creationState === null ? threadSyncStatusLabel(selectedThreadDetailState.status, contentPresentation.kind) - : null, + : null; + // Transient states go in the header subtitle so the composer's status slot doesn't jump. + const threadStatusLabel = useDelayedStatus( + selectedThread === null ? "" : scopedThreadKey(selectedThread.environmentId, selectedThread.id), + connectionStatusLabel !== null ? connectionStatusLabel : threadSyncLabel, ); const inspectorHeader = props.inspectorPresentation ? ( ) : null; @@ -1000,7 +1018,7 @@ function ThreadRouteContent( props.inspectorPresentation ? null : ( void }) { - return ( - - - Compacting… - - ); -} - function FloatingStatusLabel(props: { readonly status: FloatingWorkingStatus; readonly onLayout: (event: LayoutChangeEvent) => void; @@ -427,9 +399,6 @@ function FloatingStatusLabel(props: { ); } - if (props.status.kind === "compacting") { - return ; - } if (props.status.kind === "child-input") { return ( - {props.status.tone === "reconnecting" ? ( - - ) : ( - - )} + ); } - if (props.status.kind === "lifecycle") { - return ( - - - - {props.status.label} - - - ); - } if (props.status.kind === "background") { return ( ); } - if (props.status.kind === "preparing") { - return ( - - - - - ); - } return ( - + > + + + ); } @@ -629,53 +564,6 @@ function StatusLabelRow(props: { ); } -function WorkingDuration(props: { - readonly environmentId: EnvironmentId; - readonly startedAt: string; - readonly onLayout: (event: LayoutChangeEvent) => void; -}) { - return ( - - - - ); -} - -function WorkingTimer(props: { - readonly environmentId: EnvironmentId; - readonly startedAt: string; -}) { - const nowMs = useVisibleSecondClock(true); - return ( - - Working {formatWorkingDuration(props.startedAt, toServerClock(props.environmentId, nowMs))} - - ); -} - -function formatWorkingDuration(startedAt: string, nowMs: number): string { - const startedAtMs = Date.parse(startedAt); - if (!Number.isFinite(startedAtMs) || nowMs <= startedAtMs) { - return "0s"; - } - - const totalSeconds = Math.floor((nowMs - startedAtMs) / 1_000); - if (totalSeconds < 60) { - return `${totalSeconds}s`; - } - if (totalSeconds >= 3_600) { - return formatDuration(totalSeconds * 1_000); - } - - const minutes = Math.floor(totalSeconds / 60); - const seconds = String(totalSeconds % 60).padStart(2, "0"); - return `${minutes}m ${seconds}s`; -} - function ScrollToEndButton(props: { readonly disabled?: boolean; readonly onPress: () => void }) { return ( void; readonly action: FloatingStatusAction; } - | { - readonly kind: "lifecycle"; - readonly state: "snoozed" | "settled" | "woke"; - readonly label: string; - readonly action: FloatingStatusAction; - } | { readonly kind: "stop"; readonly label: string; @@ -42,46 +33,39 @@ export type FloatingWorkingStatus = | { readonly kind: "preparing"; readonly label: string } | { readonly kind: "connection"; - readonly tone: "reconnecting" | "unavailable"; readonly label: string; readonly onPress: () => void; }; -export function connectionFloatingStatus(input: { +/** A connection still retrying on its own; short enough for the thread header subtitle. */ +export function connectionRetryLabel(input: { readonly connectionError: string | null; readonly connectionState: EnvironmentConnectionPhase; readonly connectionAwaitingApproval?: boolean; +}): string | null { + if (input.connectionState === "reconnecting" && input.connectionAwaitingApproval) { + return "Awaiting device approval"; + } + if (input.connectionState !== "connecting" && input.connectionState !== "reconnecting") { + return null; + } + return input.connectionError === null ? "Reconnecting…" : "Retrying connection…"; +} + +export function connectionFloatingStatus(input: { + readonly connectionError: string | null; + readonly connectionState: EnvironmentConnectionPhase; readonly environmentLabel: string | null; readonly onReconnect: () => void; }): FloatingWorkingStatus | null { const environmentLabel = input.environmentLabel ?? "Environment"; const unavailable = (label: string): FloatingWorkingStatus => ({ kind: "connection", - tone: "unavailable", label, onPress: input.onReconnect, }); - if (input.connectionState === "reconnecting" && input.connectionAwaitingApproval) { - return { - kind: "connection", - tone: "reconnecting", - label: `Waiting for ${environmentLabel} to approve this device`, - onPress: input.onReconnect, - }; - } switch (input.connectionState) { - case "connecting": - case "reconnecting": - return { - kind: "connection", - tone: "reconnecting", - label: - input.connectionError === null - ? `Reconnecting to ${environmentLabel}...` - : `Failed to connect. Retrying ${environmentLabel}...`, - onPress: input.onReconnect, - }; case "offline": return unavailable("You are offline"); case "unsupported": @@ -94,6 +78,8 @@ export function connectionFloatingStatus(input: { ); case "available": return unavailable(`${environmentLabel} is not connected`); + case "connecting": + case "reconnecting": case "connected": return null; } diff --git a/apps/mobile/src/features/threads/pending-thread-feed.test.ts b/apps/mobile/src/features/threads/pending-thread-feed.test.ts index 99cabea0d5443..f7c237d4aa9f1 100644 --- a/apps/mobile/src/features/threads/pending-thread-feed.test.ts +++ b/apps/mobile/src/features/threads/pending-thread-feed.test.ts @@ -11,6 +11,7 @@ import type { QueuedThreadMessage } from "../../state/thread-outbox-model"; import { appendAcceptedThreadMessages, appendPendingThreadMessages, + createPendingTurnAppender, createThreadFeedGrouping, isCompactionCommandFeedEntry, selectPendingAcknowledgedMessages, @@ -229,4 +230,30 @@ describe("pending timeline messages", () => { "waiting", ]); }); + + it("holds the run's rows after a sent message until the run shows", () => { + const appendTurn = createPendingTurnAppender(); + const sent = appendPendingThreadMessages([], [], [pending("sent")]); + const held = appendTurn(sent, { zen: false }); + expect(held.map((entry) => [entry.id, "label" in entry ? entry.label : null])).toEqual([ + ["sent", null], + ["pending-fold:sent", null], + ["live-activity-row", "Sending…"], + ]); + expect(appendTurn(sent, { zen: false })[1]).toBe(held[1]); + const delivered = sent.map((entry) => ({ ...entry, acknowledged: true })); + expect(appendTurn(delivered, { zen: true }).map((entry) => entry.id)).toEqual([ + "sent", + "live-activity-row", + ]); + // A working run already shows its own rows. + const thinking = { + type: "thinking", + runId: null, + id: "thinking", + createdAt: "2026-09-06T11:00:00.000Z", + } as const; + const working = appendPendingThreadMessages([thinking], [], [pending("queued")]); + expect(appendTurn(working, { zen: false })).toBe(working); + }); }); diff --git a/apps/mobile/src/features/threads/pending-thread-feed.ts b/apps/mobile/src/features/threads/pending-thread-feed.ts index 940ec12db8604..af39959f2ebe7 100644 --- a/apps/mobile/src/features/threads/pending-thread-feed.ts +++ b/apps/mobile/src/features/threads/pending-thread-feed.ts @@ -3,7 +3,7 @@ import { type ThreadGuestMessage, type ThreadGuestShare, } from "@t3tools/contracts"; -import type { ThreadFeedEntry } from "../../lib/threadActivity"; +import { LIVE_ACTIVITY_ROW_ID, type ThreadFeedEntry } from "../../lib/threadActivity"; import { scopedThreadKey } from "../../lib/scopedEntities"; import { threadHasCaughtUpTo } from "@t3tools/client-runtime/state/threads"; import { resolveUserMessageGrouping } from "@t3tools/client-runtime/user-message"; @@ -51,8 +51,19 @@ export function selectPendingAcknowledgedMessages( ); } +/** + * Holds the turn's fold slot while a sent message waits for its run, so the live "Working for" + * row replaces it in place instead of being inserted. + */ +export interface PendingFoldFeedEntry { + readonly type: "pending-fold"; + readonly id: string; + readonly createdAt: string; + readonly messageId: string; +} + /** A row of the thread feed's list. */ -export type ThreadFeedRow = PendingThreadFeedEntry | GuestPendingFeedEntry; +export type ThreadFeedRow = PendingThreadFeedEntry | GuestPendingFeedEntry | PendingFoldFeedEntry; export type GroupedThreadFeedRow = ThreadFeedRow & { readonly userMessageGrouping: ReturnType; @@ -100,7 +111,7 @@ export function createThreadFeedGrouping() { /** Waiting guest messages go after the transcript, oldest first. */ export function appendGuestPendingMessages( - presentedFeed: ReadonlyArray, + presentedFeed: ReadonlyArray, share: ThreadGuestShare | null, ): ReadonlyArray { const pending = share?.messages.filter((message) => message.status === "pending") ?? []; @@ -175,6 +186,56 @@ export function appendAcceptedThreadMessages( ); } +/** + * Ends the feed with the rows a starting run will fill, while its last row is a sent message no + * run has picked up: the fold slot and the live activity row, under the activity row's own key. + * The run's rows then replace them in place instead of being inserted. + */ +export function createPendingTurnAppender() { + let fold: PendingFoldFeedEntry | null = null; + let activity: Extract | null = null; + return ( + feed: ReadonlyArray, + options: { readonly zen: boolean }, + ): ReadonlyArray => { + const last = feed.at(-1); + if ( + last?.type !== "message" || + last.message.role !== "user" || + last.pendingMessage === undefined || + last.pendingMessage.creation !== undefined || + isCompactionCommandMessage(last.message) || + feed.some( + (entry) => + (entry.type === "run-fold" && entry.workingSince !== undefined) || + entry.type === "thinking", + ) + ) { + return feed; + } + const label = last.acknowledged ? "Starting…" : "Sending…"; + if (activity?.createdAt !== last.createdAt || activity.label !== label) { + activity = { + type: "thinking", + id: LIVE_ACTIVITY_ROW_ID, + createdAt: last.createdAt, + runId: null, + label, + }; + } + if (options.zen) return [...feed, activity]; + if (fold?.messageId !== last.id) { + fold = { + type: "pending-fold", + id: `pending-fold:${last.id}`, + createdAt: last.createdAt, + messageId: last.id, + }; + } + return [...feed, fold, activity]; + }; +} + export function isCompactionCommandFeedEntry(entry: PendingThreadFeedEntry): boolean { return ( entry.type === "message" && diff --git a/apps/mobile/src/features/threads/thread-context-divider.tsx b/apps/mobile/src/features/threads/thread-context-divider.tsx index 19c99f6e63e45..2db044d7da6a6 100644 --- a/apps/mobile/src/features/threads/thread-context-divider.tsx +++ b/apps/mobile/src/features/threads/thread-context-divider.tsx @@ -1,5 +1,5 @@ import type { ReactNode } from "react"; -import { View, type ColorValue } from "react-native"; +import { Pressable, View, type ColorValue } from "react-native"; import { AppText as Text } from "../../components/ui/AppText"; import { SymbolView, type AppSymbolName } from "../../components/ui/AppSymbol"; import { ShimmeringWorkContent } from "./thread-work-log"; @@ -14,35 +14,82 @@ export function ThreadContextDivider(props: { }) { return ( - + - {props.icon ? ( - - ) : null} - {props.active ? ( - - ) : ( - - {props.label} - - )} + {/* One row, so a long label wraps beside its icon instead of under it. */} + + {props.icon ? ( + + ) : null} + {props.active ? ( + + ) : ( + + {props.label} + + )} + {props.children} - + + + ); +} + +export interface ThreadLifecycleEvent { + readonly state: "snoozed" | "settled" | "woke"; + readonly label: string; + readonly action: { + readonly label: string; + readonly accessibilityLabel: string; + readonly disabled: boolean; + readonly onPress: () => void; + }; +} + +/** Closes the feed while the thread is snoozed, settled, or just woke. Left-aligned, as on web. */ +export function ThreadLifecycleLine(props: { + readonly event: ThreadLifecycleEvent; + readonly iconColor: ColorValue; +}) { + const { action } = props.event; + return ( + + + + {props.event.label} + + · + + {action.label} + ); } diff --git a/apps/mobile/src/lib/threadActivity.test.ts b/apps/mobile/src/lib/threadActivity.test.ts index cfc228a982339..175ef5b4bab3c 100644 --- a/apps/mobile/src/lib/threadActivity.test.ts +++ b/apps/mobile/src/lib/threadActivity.test.ts @@ -1328,24 +1328,24 @@ describe("buildThreadFeed", () => { "assistant:resume-answer", ]); - expect( - shape( - deriveThreadFeedPresentation( - feed(true), - null, - new Set(), - new Set(), - "2026-06-20T00:01:12.000Z", - true, - ), - ), - ).toEqual([ + const working = deriveThreadFeedPresentation( + feed(true), + null, + new Set(), + new Set(), + "2026-06-20T00:01:12.000Z", + true, + ); + expect(shape(working)).toEqual([ "user:launch", "fold:Worked for 8s", "assistant:launch-answer", "user:resume", + "fold:Working", "activity-group", ]); + // The live timer row becomes the settled fold in place. + expect(working[4]?.id).toBe(settled[4]?.id); }); it("keeps imported V1 turns folded once the thread's first V2 run starts", () => { @@ -1443,8 +1443,8 @@ describe("buildThreadFeed", () => { new Set(), new Set(), startedAt, - ), - ).toEqual([]); + ).map((row) => row.type), + ).toEqual(["run-fold"]); }); it("hides tool calls or everything but the final answer", () => { const at = (second: number) => `2026-06-20T00:00:${String(second).padStart(2, "0")}.000Z`; @@ -1794,8 +1794,9 @@ describe("subagent continued by a later turn", () => { ]; it("keeps each turn's fold between its own prompt and answer", () => { + const feed = buildThreadFeed(items.map((entry, index) => projected(entry, index))); const presented = deriveThreadFeedPresentation( - buildThreadFeed(items.map((entry, index) => projected(entry, index))), + feed, { runId: run2, status: "completed", startedAt: at(30), completedAt: at(41) }, new Set(), ); @@ -1807,6 +1808,53 @@ describe("subagent continued by a later turn", () => { `run-fold:${run2}`, "closing-2", ]); + + // While the run works, its fold row already holds that slot with a live timer. + const live = deriveThreadFeedPresentation( + feed, + { runId: run2, status: "running", startedAt: at(30), completedAt: null }, + new Set(), + new Set(), + at(30), + ); + expect(live.slice(3, 6).map((entry) => entry.id)).toEqual([ + "prompt-2", + `run-fold:${run2}`, + expect.not.stringMatching(/^run-fold:/), + ]); + expect(live[4]).toMatchObject({ workingSince: at(30) }); + expect(live.filter((entry) => entry.type === "run-fold")).toHaveLength(2); + }); + + it("puts the live fold after a plan the settled fold leaves in view", () => { + const planned = buildThreadFeed( + [ + user("prompt", run2, 1, 0), + { + ...item("plan", run2, 2, 1), + type: "proposed_plan", + planId: "plan", + markdown: "The plan", + streaming: false, + } as OrchestrationV2TurnItem, + reasoning("reasoning", run2, 3, 2), + assistant("closing", run2, 4, 9), + ].map((entry, index) => projected(entry, index)), + ); + const ids = (running: boolean) => + deriveThreadFeedPresentation( + planned, + running + ? { runId: run2, status: "running", startedAt: at(0), completedAt: null } + : { runId: run2, status: "completed", startedAt: at(0), completedAt: at(9) }, + new Set(), + new Set(), + running ? at(0) : null, + ).map((entry) => entry.id); + const settledIds = ids(false); + const foldIndex = settledIds.indexOf(`run-fold:${run2}`); + expect(foldIndex).toBeGreaterThan(1); + expect(ids(true).indexOf(`run-fold:${run2}`)).toBe(foldIndex); }); }); diff --git a/apps/mobile/src/lib/threadActivity.ts b/apps/mobile/src/lib/threadActivity.ts index ed85709f30059..e07e7dbc1b29d 100644 --- a/apps/mobile/src/lib/threadActivity.ts +++ b/apps/mobile/src/lib/threadActivity.ts @@ -248,6 +248,8 @@ type ThreadFeedEntryContent = | { readonly type: "run-fold"; readonly deferredWorkItemIds?: ReadonlyArray; + /** Set while the run works: the row shows a live timer and does not toggle. */ + readonly workingSince?: string; readonly id: string; readonly createdAt: string; readonly runId: RunId; @@ -1242,23 +1244,8 @@ function deriveThreadFeedRunFolds( (entry) => entry.id !== terminalAssistantId && !(entry.type === "message" && retainedAnswerIds.has(entry.message.id)) && - entry.type !== "html-render" && - entry.type !== "sent-message" && - entry.type !== "mcp-app" && entry.id !== visibleCompactionId && - !( - entry.type === "activity-group" && - entry.activities.some( - (activity) => - activity.prominent || - activity.workEntry.questionAnswer !== undefined || - activity.projectedItem.item.type === "notification" || - activity.projectedItem.item.type === "handoff" || - // A child still working outlives its settled launching run; - // its group stays visible while any member is live. - isLiveSubagentTurnItem(activity.projectedItem.item), - ) - ), + !staysOutOfFold(entry), ) .map((entry) => entry.id), ); @@ -1426,6 +1413,26 @@ function latestRunsEqual(left: ThreadFeedLatestRun | null, right: ThreadFeedLate ); } +/** Rows a fold leaves in view; the live fold skips them too, so settling keeps its place. */ +function staysOutOfFold(entry: ThreadFeedEntry): boolean { + return ( + entry.type === "html-render" || + entry.type === "sent-message" || + entry.type === "mcp-app" || + (entry.type === "activity-group" && + entry.activities.some( + (activity) => + activity.prominent || + activity.workEntry.questionAnswer !== undefined || + activity.projectedItem.item.type === "notification" || + activity.projectedItem.item.type === "handoff" || + // A child still working outlives its settled launching run; + // its group stays visible while any member is live. + isLiveSubagentTurnItem(activity.projectedItem.item), + )) + ); +} + function feedEntryRunId(entry: ThreadFeedEntry): RunId | null { return entry.type === "message" ? (entry.message.runId ?? null) : entry.runId; } @@ -1725,9 +1732,44 @@ export function deriveThreadFeedPresentation( for (const entryId of fold.hiddenEntryIds) collapsedEntryIds.add(entryId); } } + // The working run's fold row shows its live timer where "Worked for" lands once it settles. + // Zen never folds, so it keeps only the Thinking row. + // Runless work folds under its prompt's key once it settles, so the live row takes that key. + const runlessPrompt = + activeRunId === null && runlessWorkActive + ? sourceFeed.findLast((entry) => isUserMessageEntry(entry) && feedEntryRunId(entry) === null) + : undefined; + const liveRunId = + activeRunId ?? (runlessPrompt ? RunId.make(`runless:${runlessPrompt.id}`) : null); + const liveFold = + !zen && + isWorking && + activeWorkStartedAt !== null && + liveRunId !== null && + !compactionCommandRunIds.has(liveRunId) + ? liveRunFoldRow(liveRunId, activeWorkStartedAt) + : null; + let liveFoldPlaced = false; + // Earlier turns can hold the run's own items (a subagent it continued), so wait for its prompt. + let liveFoldReady = + liveFold !== null && + activeRunId !== null && + !sourceFeed.some((entry) => isUserMessageEntry(entry) && feedEntryRunId(entry) === activeRunId); for (let sourceIndex = 0; sourceIndex < sourceFeed.length; sourceIndex += 1) { const entry = sourceFeed[sourceIndex]!; const feedIndex = retainedFeedIndices[sourceIndex]!; + if (liveFold !== null && !liveFoldPlaced && feedEntryRunId(entry) === activeRunId) { + if (isUserMessageEntry(entry)) { + liveFoldReady = activeRunId !== null || entry === runlessPrompt; + } else if ( + liveFoldReady && + !staysOutOfFold(entry) && + !(entry.type === "activity-group" && isContextCompactionActivityGroup(entry)) + ) { + result.push(liveFold); + liveFoldPlaced = true; + } + } if (isUserMessageEntry(entry)) { restarts.push({ entry: feedIndex, rowsLength: result.length }); } @@ -1808,6 +1850,7 @@ export function deriveThreadFeedPresentation( ); } } + if (liveFold !== null && !liveFoldPlaced) result.push(liveFold); if ( isWorking && activeWorkStartedAt !== null && @@ -1932,7 +1975,7 @@ function isWorkLogFeedRow(row: ThreadFeedEntry | undefined): boolean { ); } -const LIVE_ACTIVITY_ROW_ID = "live-activity-row"; +export const LIVE_ACTIVITY_ROW_ID = "live-activity-row"; /** Reads the feed before tool visibility filters it, so hidden newer parent work still counts. */ function runningSubagentsAtRunTail(feed: ReadonlyArray, runId: RunId): number { @@ -1947,6 +1990,23 @@ function runningSubagentsAtRunTail(feed: ReadonlyArray, runId: return running; } +let cachedLiveRunFoldRow: Extract | null = null; + +function liveRunFoldRow(runId: RunId, startedAt: string) { + if (cachedLiveRunFoldRow?.runId !== runId || cachedLiveRunFoldRow.workingSince !== startedAt) { + cachedLiveRunFoldRow = { + type: "run-fold", + id: `run-fold:${runId}`, + createdAt: startedAt, + runId, + label: "Working", + expanded: false, + workingSince: startedAt, + }; + } + return cachedLiveRunFoldRow; +} + function thinkingRow( createdAt: string, runId: RunId | null, diff --git a/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx b/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx index 448e6f90a8ed0..2606ae7b898db 100644 --- a/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx @@ -2083,7 +2083,7 @@ it("renders automatic compaction as one live divider that finishes with its reas await expect .element( screen.getByRole("separator", { - name: "Context automatically compacted before cache expiry 899K → 19K tokens", + name: "Auto-compacted before cache expiry 899K → 19K tokens", }), ) .toBeVisible(); diff --git a/apps/web/src/components/chat/MessagesTimeline.logic.test.ts b/apps/web/src/components/chat/MessagesTimeline.logic.test.ts index 1cd8066dbcd32..f834c7cc355b9 100644 --- a/apps/web/src/components/chat/MessagesTimeline.logic.test.ts +++ b/apps/web/src/components/chat/MessagesTimeline.logic.test.ts @@ -686,7 +686,7 @@ describe("deriveMessagesTimelineRows", () => { active: true, label: "Compacting before the cache expires…", }, - "Context automatically compacted before cache expiry", + "Auto-compacted before cache expiry", ], ["onReturn", { kind: "working", compacting: true }, "Context compacted before sending"], [undefined, { kind: "context-compaction", active: true }, undefined], diff --git a/packages/client-runtime/src/userMessage.ts b/packages/client-runtime/src/userMessage.ts index a860c0188b9d2..6c41248d91377 100644 --- a/packages/client-runtime/src/userMessage.ts +++ b/packages/client-runtime/src/userMessage.ts @@ -25,7 +25,7 @@ export const IDLE_COMPACTING_LABELS = { /** Leads the finished compaction's token counts, in place of "Context compacted". */ export const IDLE_COMPACTION_TITLES = { - beforeCacheExpires: "Context automatically compacted before cache expiry", + beforeCacheExpires: "Auto-compacted before cache expiry", onReturn: "Context compacted before sending", } satisfies Record; From 6f5861fa27c9d1c21e5b48677ac0da1188bfd233 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 12:20:37 -0800 Subject: [PATCH 038/253] fix(shared): inline code paths with spaces become file chips (#37750) --- apps/mobile/src/lib/markdownDocument.ts | 16 ++++++++-- .../src/lib/markdownInlineFileLinks.test.ts | 5 +++- packages/shared/src/markdownLinks.test.ts | 15 ++++++++++ packages/shared/src/markdownLinks.ts | 30 +++++++++++++++++-- 4 files changed, 60 insertions(+), 6 deletions(-) diff --git a/apps/mobile/src/lib/markdownDocument.ts b/apps/mobile/src/lib/markdownDocument.ts index ddeca4b2d2103..55e8c09394387 100644 --- a/apps/mobile/src/lib/markdownDocument.ts +++ b/apps/mobile/src/lib/markdownDocument.ts @@ -3,7 +3,11 @@ import { unified, type Processor } from "unified"; import remarkParse from "remark-parse"; import remarkGfm from "remark-gfm"; import { remarkKeepWindowsProsePaths } from "@t3tools/shared/markdownWindowsPaths"; -import { absoluteFilePathMatches, parseMarkdownFileLink } from "@t3tools/shared/markdownLinks"; +import { + absoluteFilePathMatches, + inlineCodeFilePathCandidate, + parseMarkdownFileLink, +} from "@t3tools/shared/markdownLinks"; import { isWindowsAbsolutePath } from "@t3tools/shared/path"; const parser = unified().use(remarkParse).use(remarkGfm).use(remarkKeepWindowsProsePaths); @@ -322,6 +326,11 @@ export function withReferenceDefinitions( // Start only at the beginning of a backtick run to avoid retrying every suffix. const INLINE_CODE_SPAN = /(?]/.test(node.value) ) { diff --git a/apps/mobile/src/lib/markdownInlineFileLinks.test.ts b/apps/mobile/src/lib/markdownInlineFileLinks.test.ts index 3de6590dc7a4b..465afe21f34a2 100644 --- a/apps/mobile/src/lib/markdownInlineFileLinks.test.ts +++ b/apps/mobile/src/lib/markdownInlineFileLinks.test.ts @@ -99,13 +99,16 @@ describe("inline code that names a file", () => { "數學.ts:2", "src/數學.ts:2-4", "docker/Dockerfile", + "/Users/me/Path Test/notes with spaces.md", + "~/Library/Mobile Documents/notes.md", ]) { expect(rewrite(`See \`${path}\`.`), path).toBe(`See [${path}](<${path}>).`); } }); it("keeps commands, identifiers and URLs as code", () => { - const markdown = "Run `vp test run`, call `useMemo`, open `https://example.com/a.ts`."; + const markdown = + "Run `vp test run`, `/usr/bin/python3 -m venv .venv`, call `useMemo`, open `https://example.com/a.ts`."; expect(rewrite(markdown)).toBe(markdown); }); diff --git a/packages/shared/src/markdownLinks.test.ts b/packages/shared/src/markdownLinks.test.ts index 76699b40d7a95..0a997aee79eac 100644 --- a/packages/shared/src/markdownLinks.test.ts +++ b/packages/shared/src/markdownLinks.test.ts @@ -48,6 +48,21 @@ describe("inlineCodeFilePathCandidate", () => { ["./bin/run:12", "./bin/run:12"], ["src/資料.備份:12", "src/資料.備份:12"], ["src/foo.c++:12", "src/foo.c++:12"], + // regression: #37740 absolute paths with spaces are files, commands are not + ["/Users/me/Path Test/notes with spaces.md", "/Users/me/Path Test/notes with spaces.md"], + ["~/Library/Mobile Documents/notes.md", "~/Library/Mobile Documents/notes.md"], + [ + "/Users/me/Library/CloudStorage/OneDrive-Org/Alex/Partners/Acme/Finance Use Cases/working/2026-11-16 message - draft.md", + "/Users/me/Library/CloudStorage/OneDrive-Org/Alex/Partners/Acme/Finance Use Cases/working/2026-11-16 message - draft.md", + ], + ["cd /tmp && ls", null], + ["/usr/bin/python3 -m venv .venv", null], + ["/usr/local/bin/node server.js", null], + ["/usr/bin/env python3 foo.py", null], + ["/opt/homebrew/bin/python3 manage.py", null], + ["/bin/echo hello world.txt", null], + ["~/bin/run tests.sh", null], + ["/Users/me/notes with spaces.md", "/Users/me/notes with spaces.md"], ])("distinguishes file paths from code and hostnames in %s", (source, candidate) => { expect(inlineCodeFilePathCandidate(source)).toBe(candidate); }); diff --git a/packages/shared/src/markdownLinks.ts b/packages/shared/src/markdownLinks.ts index 0ae35231179e1..0336f8adb07be 100644 --- a/packages/shared/src/markdownLinks.ts +++ b/packages/shared/src/markdownLinks.ts @@ -30,7 +30,30 @@ const POSITION_SUFFIX_PATTERN = /:\d+(?::\d+|-\d+)?$/; const POSITION_ONLY_PATTERN = /^\d+(?::\d+|-\d+)?$/; -const INLINE_CODE_DISQUALIFIER_PATTERN = /[\s`]/; +const INLINE_CODE_WHITESPACE_PATTERN = /\s/; + +// Inline code with spaces is a file only when it is absolute and every word is a plain name: +// `/Users/me/Mobile Documents/a - b.md` is, `/usr/bin/python3 -m venv .venv` (a flag) is not. +// A space only in the last segment reads as a command (`/usr/local/bin/node server.js`), so it +// must sit under a user root, and nothing after a bin folder counts. +const SPACED_ABSOLUTE_PATH_ROOT_PATTERN = /^(?:\/|~\/|[A-Za-z]:[\\/])/; +const SPACED_PATH_BODY_PATTERN = + /^(?:[\p{L}\p{M}\p{N}._$+@()[\]-]+(?: (?!-+\p{L})[\p{L}\p{M}\p{N}._$+@()[\]-]+)*(?:[\\/]|$))+$/u; + +function isSpacedAbsoluteFilePath(text: string): boolean { + const root = SPACED_ABSOLUTE_PATH_ROOT_PATTERN.exec(text)?.[0]; + if (root === undefined) return false; + const body = text.slice(root.length).replace(POSITION_SUFFIX_PATTERN, ""); + if (!SPACED_PATH_BODY_PATTERN.test(body)) return false; + const firstSpace = body.indexOf(" "); + const foldersBeforeSpace = body.slice(0, firstSpace).split(PATH_SEPARATOR_PATTERN).slice(0, -1); + if (foldersBeforeSpace.some((folder) => folder === "bin" || folder === "sbin")) return false; + const spaceInFolder = PATH_SEPARATOR_PATTERN.test(body.slice(firstSpace)); + if (!spaceInFolder && root === "/" && !/^(?:Users|home)[\\/]/.test(body)) return false; + if (PATH_SEPARATOR_PATTERN.test(body.at(-1) ?? "")) return true; + const basename = body.split(PATH_SEPARATOR_PATTERN).at(-1) ?? ""; + return FILE_EXTENSION_PATTERN.test(basename) || EXTENSIONLESS_FILE_NAMES.has(basename); +} const PATH_SEPARATOR_PATTERN = /[\\/]/; // Agents shorten long paths with a `...` or `…` segment, which no file chip can open. @@ -177,7 +200,10 @@ function looksLikeHostname(segment: string, hasPosition: boolean): boolean { export function inlineCodeFilePathCandidate(codeText: string): string | null { const trimmed = codeText.trim(); - if (trimmed.length === 0 || INLINE_CODE_DISQUALIFIER_PATTERN.test(trimmed)) return null; + if (trimmed.length === 0 || trimmed.includes("`")) return null; + if (INLINE_CODE_WHITESPACE_PATTERN.test(trimmed) && !isSpacedAbsoluteFilePath(trimmed)) { + return null; + } const candidate = isWindowsAbsolutePath(trimmed) ? trimmed : trimmed.replaceAll("\\", "/"); const hasPosition = POSITION_SUFFIX_PATTERN.test(candidate); From 6dd092315e6f87922427ebcefe5e8f4c75793c1f Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 12:27:20 -0800 Subject: [PATCH 039/253] fix(web): file tree icons and ignored rows follow the theme's muted text (#31785) --- apps/web/src/pierre-tree-theme.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/apps/web/src/pierre-tree-theme.ts b/apps/web/src/pierre-tree-theme.ts index f3d21a1c38f5f..ada2a7d1abc7b 100644 --- a/apps/web/src/pierre-tree-theme.ts +++ b/apps/web/src/pierre-tree-theme.ts @@ -23,5 +23,7 @@ export function pierreTreeStyle(colorScheme: "light" | "dark"): CSSProperties { return { colorScheme, ["--trees-fg-override" as string]: "var(--contrast-foreground)", + ["--trees-fg-muted-override" as string]: "var(--contrast-muted-foreground)", + ["--trees-status-ignored-override" as string]: "var(--contrast-muted-foreground)", }; } From 8ca0c88cfd16135afb995771ac5becee0de9bc1b Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 12:30:12 -0800 Subject: [PATCH 040/253] fix(web): screen readers see one Notifications landmark, not two (#31724) --- .../src/components/ui/toast.browser.test.tsx | 21 ++++++++++++++++++- apps/web/src/components/ui/toast.tsx | 7 ++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/apps/web/src/components/ui/toast.browser.test.tsx b/apps/web/src/components/ui/toast.browser.test.tsx index d826ab6bdc40a..eb5a599c0dce0 100644 --- a/apps/web/src/components/ui/toast.browser.test.tsx +++ b/apps/web/src/components/ui/toast.browser.test.tsx @@ -2,7 +2,7 @@ import { describe, expect, it } from "vite-plus/test"; import { page } from "vite-plus/test/browser"; import { render } from "../../../test/render"; -import { ToastProvider, toastManager } from "./toast"; +import { AnchoredToastProvider, ToastProvider, toastManager } from "./toast"; describe("toastManager", () => { it("shows a toast added before any provider mounted", async () => { @@ -12,4 +12,23 @@ describe("toastManager", () => { await expect.element(page.getByText("Raised before the first render")).toBeVisible(); }); + + it("exposes one Notifications landmark when both toast providers mount", async () => { + await using _screen = await render( + + {null} + , + ); + + const notifications = page.getByRole("region", { name: "Notifications" }); + await expect.element(notifications.first()).toBeInTheDocument(); + expect( + notifications.elements(), + "only the corner toast viewport should be a Notifications landmark", + ).toHaveLength(1); + await expect.element(notifications).toHaveAttribute("data-slot", "toast-viewport"); + + const anchoredViewport = document.querySelector("[data-slot=toast-viewport-anchored]"); + expect(anchoredViewport).toHaveAttribute("aria-live", "polite"); + }); }); diff --git a/apps/web/src/components/ui/toast.tsx b/apps/web/src/components/ui/toast.tsx index 16b72ebb48344..07d2cc43fb2e2 100644 --- a/apps/web/src/components/ui/toast.tsx +++ b/apps/web/src/components/ui/toast.tsx @@ -726,7 +726,12 @@ function AnchoredToasts() { return ( - + {toasts .filter((toast) => shouldRenderThreadScopedToast(toast.data, activeThreadRef)) .map((toast) => { From c1a9279602db53bda109b5dd769ee8420b708b9a Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 12:30:21 -0800 Subject: [PATCH 041/253] ci(mobile): share one local build action across preview and store builds (#37755) Co-authored-by: Claude Opus 5.5 --- .../drop-checkout-credentials/action.yml | 38 ++ .github/actions/eas-local-build/action.yml | 188 ++++++++++ .github/actions/record-build-tag/action.yml | 57 +++ .github/workflows/mobile-eas-preview-fork.yml | 225 ++---------- .github/workflows/mobile-eas-preview.yml | 313 +++------------- .github/workflows/mobile-eas-production.yml | 335 +++--------------- 6 files changed, 422 insertions(+), 734 deletions(-) create mode 100644 .github/actions/drop-checkout-credentials/action.yml create mode 100644 .github/actions/eas-local-build/action.yml create mode 100644 .github/actions/record-build-tag/action.yml diff --git a/.github/actions/drop-checkout-credentials/action.yml b/.github/actions/drop-checkout-credentials/action.yml new file mode 100644 index 0000000000000..f9a60340e1fe5 --- /dev/null +++ b/.github/actions/drop-checkout-credentials/action.yml @@ -0,0 +1,38 @@ +name: Drop checkout credentials +description: Removes the token actions/checkout leaves behind, so later steps can run dependency and build scripts. + +# checkout keeps the token in a file under RUNNER_TEMP that .git/config includes, so +# both go. `persist-credentials: false` fails in this repo: its cleanup runs `git +# submodule foreach`, which stops at the vendored gitlinks under .repos (no .gitmodules). +runs: + using: composite + steps: + - name: Drop checkout credentials + shell: bash + run: | + # Status 1 means no match; any other failure stops the step. + lookup() { + local status=0 + git -C "$dir" config --local "$@" || status=$? + if [ "$status" -gt 1 ]; then exit "$status"; fi + } + for dir in . .trusted; do + [ -e "$dir/.git" ] || continue + lookup --get-regexp '^includeif\..*\.path$' | while read -r key file; do + rm -f "$file" + git -C "$dir" config --local --unset-all "$key" + done + lookup --name-only --get-regexp '^http\..*\.extraheader$' | while read -r key; do + git -C "$dir" config --local --unset-all "$key" + done + remaining="$(lookup --get-regexp 'extraheader|includeif')" + if [ -n "$remaining" ]; then + echo "::error::checkout credentials are still configured in $dir" + exit 1 + fi + done + rm -f "$RUNNER_TEMP"/git-credentials-*.config + if ls "$RUNNER_TEMP"/git-credentials-*.config 2>/dev/null; then + echo "::error::a checkout credential file is still present" + exit 1 + fi diff --git a/.github/actions/eas-local-build/action.yml b/.github/actions/eas-local-build/action.yml new file mode 100644 index 0000000000000..847c8e0117492 --- /dev/null +++ b/.github/actions/eas-local-build/action.yml @@ -0,0 +1,188 @@ +name: EAS local build +description: Installs the toolchain and mobile dependencies, then runs `eas build --local` on this runner. + +inputs: + platform: + description: android or ios + required: true + profile: + description: eas.json build profile + required: true + environment: + description: EAS environment to pull, preview or production + required: true + expo-token: + description: Expo token for that environment + required: true + output: + description: Path the build artifact is written to + required: true + cache: + description: "false for untrusted code: no sticky disk and no EAS cache, so nothing it writes reaches trusted builds" + default: "true" + sourcemaps-r2: + description: R2 credentials for the eas-build-on-success source map upload; EAS secrets don't reach local builds + default: "" + +runs: + using: composite + steps: + - name: Prepare Android build cache + if: inputs.platform == 'android' && inputs.cache == 'true' + shell: bash + env: + CACHE_NAME: ${{ inputs.environment == 'production' && 'store' || 'preview' }} + run: | + cache="$HOME/.cache/t3-android-$CACHE_NAME" + { + echo "GRADLE_USER_HOME=$cache/gradle" + echo "CCACHE_DIR=$cache/ccache" + echo "CCACHE_MAXSIZE=5G" + # Gradle installs the NDK fresh each run, so key on the compiler's version, not its mtime. + echo "CCACHE_COMPILERCHECK=%compiler% -v" + echo "pnpm_config_store_dir=$cache/pnpm-store" + # eas build --local copies the project here. A fixed path keeps + # ccache keys stable, and the same disk as the store lets pnpm hardlink. + echo "EAS_LOCAL_BUILD_WORKINGDIR=$cache/eas-build" + echo "CCACHE_BASEDIR=$cache/eas-build" + # CMake (3.17+) reads these, and AGP passes Gradle's environment through. + echo "CMAKE_C_COMPILER_LAUNCHER=ccache" + echo "CMAKE_CXX_COMPILER_LAUNCHER=ccache" + } >> "$GITHUB_ENV" + + - name: Mount Android build cache + if: inputs.platform == 'android' && inputs.cache == 'true' + uses: useblacksmith/stickydisk@3f6be1451e3cad893b778bc93495c80452a5da26 # v1.7.2 + with: + key: ${{ github.repository }}-android-${{ inputs.environment == 'production' && 'store' || 'preview' }}-build + path: ~/.cache/t3-android-${{ inputs.environment == 'production' && 'store' || 'preview' }} + + - name: Configure Gradle and ccache + if: inputs.platform == 'android' && inputs.cache == 'true' + shell: bash + run: | + mkdir -p "$GRADLE_USER_HOME" "$CCACHE_DIR" + rm -rf "$EAS_LOCAL_BUILD_WORKINGDIR" + # No daemon: nothing should hold the sticky disk open when it is committed. + printf 'org.gradle.caching=true\norg.gradle.daemon=false\n' > "$GRADLE_USER_HOME/gradle.properties" + sudo sed -i 's|http://|https://|g' /etc/apt/blacksmith-ubuntu-mirrors.txt /etc/apt/sources.list.d/ubuntu.sources + sudo apt-get update -qq && sudo apt-get install -y -qq ccache + ccache --zero-stats + + # Matches the EAS build image in eas.json. + - name: Select Xcode 27.1 + if: inputs.platform == 'ios' + shell: bash + run: | + xcode=/Applications/Xcode_27.1.app + if [ ! -d "$xcode" ]; then + ls -d /Applications/Xcode*.app + echo "::error::Xcode 27.1 is not on this runner." + exit 1 + fi + sudo xcode-select -s "$xcode/Contents/Developer" + xcodebuild -version + echo "- Xcode: \`$(basename "$xcode")\`" >> "$GITHUB_STEP_SUMMARY" + + # EAS's credentials step fails to import the distribution certificate without it. + - name: Install Apple WWDR G3 intermediate + if: inputs.platform == 'ios' + shell: bash + run: | + curl -fsSL -o "$RUNNER_TEMP/AppleWWDRCAG3.cer" https://www.apple.com/certificateauthority/AppleWWDRCAG3.cer + sudo security add-certificates -k /Library/Keychains/System.keychain "$RUNNER_TEMP/AppleWWDRCAG3.cer" || true + security find-certificate -c "Apple Worldwide Developer Relations" /Library/Keychains/System.keychain | grep labl + + - name: Setup Node + uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6.5.0 + with: + node-version-file: package.json + + - name: Enable corepack + shell: bash + run: | + npm install --global corepack@latest + corepack enable + pnpm --version + + # EAS commands load app.config.ts, whose plugins must resolve. Keep install + # scripts: some download native assets that the build's fingerprint check hashes. + - name: Install mobile dependencies + shell: bash + run: pnpm install --frozen-lockfile --prefer-offline --filter=@t3tools/mobile... + + - name: Setup Java + if: inputs.platform == 'android' + uses: actions/setup-java@b6effb05e454b25005698d916606bdc6ffcbf961 # v5.7.0 + with: + distribution: temurin + java-version: 17 + + - name: Setup EAS + uses: expo/expo-github-action@c7b66a9c327a43a8fa7c0158e7f30d6040d2481e # v8.2.1 + with: + eas-version: latest + eas-cache: ${{ inputs.cache }} + token: ${{ inputs.expo-token }} + # npm, not pnpm: this only installs eas-cli into the action's own + # tool dir, and pnpm 11 hard-fails that install on dtrace-provider's + # ignored build script (no allowBuilds config outside the repo). + packager: npm + + - name: Build tools + if: inputs.platform == 'ios' + shell: bash + run: | + command -v pod || sudo gem install cocoapods --no-document + command -v fastlane || brew install fastlane + pod --version + fastlane --version | tail -1 + + # EAS commands skip dotenv, and the Android fingerprint hashes this file. + - name: Pull ${{ inputs.environment }} environment variables + shell: bash + working-directory: apps/mobile + env: + EXPO_TOKEN: ${{ inputs.expo-token }} + ENVIRONMENT: ${{ inputs.environment }} + PLATFORM: ${{ inputs.platform }} + run: | + eas env:pull "$ENVIRONMENT" --non-interactive + node --input-type=module <<'NODE' + import { appendFileSync, readFileSync } from "node:fs"; + import { parseEnv } from "node:util"; + const env = parseEnv(readFileSync(".env.local", "utf8")); + const googleServicesFile = env.T3CODE_ANDROID_GOOGLE_SERVICES_FILE; + if (!googleServicesFile && process.env.PLATFORM === "android") { + throw new Error(`T3CODE_ANDROID_GOOGLE_SERVICES_FILE is missing from the ${process.env.ENVIRONMENT} environment`); + } + if (googleServicesFile) { + appendFileSync(process.env.GITHUB_ENV, `T3CODE_ANDROID_GOOGLE_SERVICES_FILE=${googleServicesFile}\n`); + } + NODE + + - name: Build + shell: bash + working-directory: apps/mobile + env: + EXPO_TOKEN: ${{ inputs.expo-token }} + MOBILE_SOURCEMAPS_R2: ${{ inputs.sourcemaps-r2 }} + PLATFORM: ${{ inputs.platform }} + PROFILE: ${{ inputs.profile }} + OUTPUT: ${{ inputs.output }} + run: | + if [ "$PLATFORM" = "ios" ]; then + # A real path: /var -> /private/var symlinks break the expo-widgets bundle step. + export EAS_LOCAL_BUILD_WORKINGDIR="$RUNNER_TEMP/eas-build" + fi + eas build --local --platform "$PLATFORM" --profile "$PROFILE" --non-interactive --output "$OUTPUT" + + - name: Report ccache + if: inputs.platform == 'android' && inputs.cache == 'true' + shell: bash + run: | + { + echo '```' + ccache --show-stats + echo '```' + } >> "$GITHUB_STEP_SUMMARY" diff --git a/.github/actions/record-build-tag/action.yml b/.github/actions/record-build-tag/action.yml new file mode 100644 index 0000000000000..2086d004b295c --- /dev/null +++ b/.github/actions/record-build-tag/action.yml @@ -0,0 +1,57 @@ +name: Record build tag +description: Pushes the tag that tells later runs a build for this fingerprint exists. + +inputs: + tag: + description: Tag name, without refs/tags/ + required: true + message: + description: Annotated tag message; empty makes a lightweight tag + default: "" + replace: + description: "true replaces an existing tag; otherwise an existing tag counts as recorded" + default: "false" + token: + description: Token with contents write + required: true + +runs: + using: composite + steps: + - name: Record build tag + shell: bash + env: + TAG: ${{ inputs.tag }} + MESSAGE: ${{ inputs.message }} + REPLACE: ${{ inputs.replace }} + GH_TOKEN: ${{ inputs.token }} + run: | + git config user.name "github-actions[bot]" + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" + remote="https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" + # GitHub occasionally fails ref writes server-side ("fatal error in commit_refs"). + for attempt in 1 2 3; do + if refs="$(git ls-remote "$remote" "refs/tags/$TAG")"; then + old="$(printf '%s\n' "$refs" | while read -r sha ref; do + if [ "$ref" = "refs/tags/$TAG" ]; then echo "$sha"; fi + done)" + if [ -n "$old" ] && [ "$REPLACE" != "true" ]; then + echo "$TAG exists." + exit 0 + fi + if [ -n "$MESSAGE" ]; then + git tag -f -a "$TAG" -m "$MESSAGE" + else + git tag -f "$TAG" + fi + # An empty lease means the tag must not exist yet. + if git push --force-with-lease="refs/tags/$TAG:$old" "$remote" "refs/tags/$TAG"; then + exit 0 + fi + fi + if [ "$attempt" = 3 ]; then + echo "::error::Could not push $TAG." + exit 1 + fi + sleep $((attempt * 15)) + done diff --git a/.github/workflows/mobile-eas-preview-fork.yml b/.github/workflows/mobile-eas-preview-fork.yml index dc06871822830..890bd0c2aecd9 100644 --- a/.github/workflows/mobile-eas-preview-fork.yml +++ b/.github/workflows/mobile-eas-preview-fork.yml @@ -87,40 +87,20 @@ jobs: fetch-depth: 0 # No sparse-checkout: see mobile-eas-preview.yml. - # The comment script comes from main, not from the PR. + # Scripts and actions come from main, not from the PR. - name: Checkout trusted scripts if: steps.expo-token.outputs.present == 'true' uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 with: path: .trusted - sparse-checkout: .github/scripts + sparse-checkout: | + .github/scripts + .github/actions - # The steps below run the PR's dependency and build scripts; they get no token. - # checkout keeps it in a file under RUNNER_TEMP that .git/config includes, so both - # go. Asking checkout not to persist it fails here: that cleanup runs `git submodule - # foreach`, which stops at the vendored gitlinks under .repos (no .gitmodules entry). + # The steps below run dependency and build scripts; they get no token. - name: Drop checkout credentials if: steps.expo-token.outputs.present == 'true' - run: | - for dir in . .trusted; do - [ -e "$dir/.git" ] || continue - git -C "$dir" config --local --get-regexp '^includeif\..*\.path$' | while read -r key file; do - rm -f "$file" - git -C "$dir" config --local --unset-all "$key" - done - git -C "$dir" config --local --name-only --get-regexp '^http\..*\.extraheader$' | while read -r key; do - git -C "$dir" config --local --unset-all "$key" - done - if git -C "$dir" config --local --get-regexp 'extraheader|includeif'; then - echo "::error::checkout credentials are still configured in $dir" - exit 1 - fi - done - rm -f "$RUNNER_TEMP"/git-credentials-*.config - if ls "$RUNNER_TEMP"/git-credentials-*.config 2>/dev/null; then - echo "::error::a checkout credential file is still present" - exit 1 - fi + uses: ./.trusted/.github/actions/drop-checkout-credentials # No caches: pull_request_target saves them under main's scope, where # trusted workflows with other secrets would restore what PR code wrote. @@ -268,91 +248,37 @@ jobs: ref: ${{ github.event.pull_request.head.sha }} fetch-depth: 0 - # The comment script comes from main, not from the PR. + # Scripts and actions come from main, not from the PR. - name: Checkout trusted scripts uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 with: path: .trusted - sparse-checkout: .github/scripts + sparse-checkout: | + .github/scripts + .github/actions - # The steps below run the PR's dependency and build scripts; they get no token. - # checkout keeps it in a file under RUNNER_TEMP that .git/config includes, so both - # go. Asking checkout not to persist it fails here: that cleanup runs `git submodule - # foreach`, which stops at the vendored gitlinks under .repos (no .gitmodules entry). + # The steps below run dependency and build scripts; they get no token. - name: Drop checkout credentials - run: | - for dir in . .trusted; do - [ -e "$dir/.git" ] || continue - git -C "$dir" config --local --get-regexp '^includeif\..*\.path$' | while read -r key file; do - rm -f "$file" - git -C "$dir" config --local --unset-all "$key" - done - git -C "$dir" config --local --name-only --get-regexp '^http\..*\.extraheader$' | while read -r key; do - git -C "$dir" config --local --unset-all "$key" - done - if git -C "$dir" config --local --get-regexp 'extraheader|includeif'; then - echo "::error::checkout credentials are still configured in $dir" - exit 1 - fi - done - rm -f "$RUNNER_TEMP"/git-credentials-*.config - if ls "$RUNNER_TEMP"/git-credentials-*.config 2>/dev/null; then - echo "::error::a checkout credential file is still present" - exit 1 - fi - - - name: Setup Node - uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6.5.0 - with: - node-version-file: package.json - - - name: Enable corepack - run: | - npm install --global corepack@latest - corepack enable - pnpm --version + uses: ./.trusted/.github/actions/drop-checkout-credentials - - name: Install mobile dependencies - run: pnpm install --frozen-lockfile --filter=@t3tools/mobile... - - - name: Setup Java - uses: actions/setup-java@b6effb05e454b25005698d916606bdc6ffcbf961 # v5.7.0 + # Cold: no sticky disk or EAS cache that trusted builds would restore. + - name: Build + uses: ./.trusted/.github/actions/eas-local-build with: - distribution: temurin - java-version: 17 - - - name: Setup EAS - uses: expo/expo-github-action@c7b66a9c327a43a8fa7c0158e7f30d6040d2481e # v8.2.1 - with: - eas-version: latest - eas-cache: false - token: ${{ secrets.EXPO_PREVIEW_TOKEN }} - packager: npm - - - name: Pull preview environment variables - working-directory: apps/mobile - env: - EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }} - run: | - eas env:pull preview --non-interactive - node --input-type=module <<'NODE' - import { appendFileSync, readFileSync } from "node:fs"; - import { parseEnv } from "node:util"; - const env = parseEnv(readFileSync(".env.local", "utf8")); - const googleServicesFile = env.T3CODE_ANDROID_GOOGLE_SERVICES_FILE; - if (googleServicesFile) { - appendFileSync(process.env.GITHUB_ENV, `T3CODE_ANDROID_GOOGLE_SERVICES_FILE=${googleServicesFile}\n`); - } - NODE + platform: android + profile: preview:dev + environment: preview + expo-token: ${{ secrets.EXPO_PREVIEW_TOKEN }} + output: ${{ runner.temp }}/t3code-preview.apk + cache: "false" - id: build - name: Build and upload + name: Upload working-directory: apps/mobile shell: bash env: EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }} run: | - eas build --local --platform android --profile preview:dev --non-interactive --output "$RUNNER_TEMP/t3code-preview.apk" eas upload --platform android --build-path "$RUNNER_TEMP/t3code-preview.apk" --fingerprint "$FINGERPRINT" --non-interactive --json | tee "$RUNNER_TEMP/upload.json" link="$(sed -n '/^{/,$p' "$RUNNER_TEMP/upload.json" | jq -r '.url // empty')" if [ -z "$link" ]; then @@ -384,112 +310,37 @@ jobs: ref: ${{ github.event.pull_request.head.sha }} fetch-depth: 0 - # The comment script comes from main, not from the PR. + # Scripts and actions come from main, not from the PR. - name: Checkout trusted scripts uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 with: path: .trusted - sparse-checkout: .github/scripts + sparse-checkout: | + .github/scripts + .github/actions - # The steps below run the PR's dependency and build scripts; they get no token. - # checkout keeps it in a file under RUNNER_TEMP that .git/config includes, so both - # go. Asking checkout not to persist it fails here: that cleanup runs `git submodule - # foreach`, which stops at the vendored gitlinks under .repos (no .gitmodules entry). + # The steps below run dependency and build scripts; they get no token. - name: Drop checkout credentials - run: | - for dir in . .trusted; do - [ -e "$dir/.git" ] || continue - git -C "$dir" config --local --get-regexp '^includeif\..*\.path$' | while read -r key file; do - rm -f "$file" - git -C "$dir" config --local --unset-all "$key" - done - git -C "$dir" config --local --name-only --get-regexp '^http\..*\.extraheader$' | while read -r key; do - git -C "$dir" config --local --unset-all "$key" - done - if git -C "$dir" config --local --get-regexp 'extraheader|includeif'; then - echo "::error::checkout credentials are still configured in $dir" - exit 1 - fi - done - rm -f "$RUNNER_TEMP"/git-credentials-*.config - if ls "$RUNNER_TEMP"/git-credentials-*.config 2>/dev/null; then - echo "::error::a checkout credential file is still present" - exit 1 - fi - - - name: Select Xcode 27.1 - run: | - xcode=/Applications/Xcode_27.1.app - if [ ! -d "$xcode" ]; then - ls -d /Applications/Xcode*.app - echo "::error::Xcode 27.1 is not on this runner." - exit 1 - fi - sudo xcode-select -s "$xcode/Contents/Developer" - xcodebuild -version - echo "- Xcode: \`$(basename "$xcode")\`" >> "$GITHUB_STEP_SUMMARY" - - # EAS's credentials step fails to import the distribution certificate without it. - - name: Install Apple WWDR G3 intermediate - run: | - curl -fsSL -o "$RUNNER_TEMP/AppleWWDRCAG3.cer" https://www.apple.com/certificateauthority/AppleWWDRCAG3.cer - sudo security add-certificates -k /Library/Keychains/System.keychain "$RUNNER_TEMP/AppleWWDRCAG3.cer" || true - security find-certificate -c "Apple Worldwide Developer Relations" /Library/Keychains/System.keychain | grep -m1 labl - - - name: Setup Node - uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6.5.0 - with: - node-version-file: package.json - - - name: Enable corepack - run: | - npm install --global corepack@latest - corepack enable - pnpm --version - - - name: Install mobile dependencies - run: pnpm install --frozen-lockfile --filter=@t3tools/mobile... + uses: ./.trusted/.github/actions/drop-checkout-credentials - - name: Setup EAS - uses: expo/expo-github-action@c7b66a9c327a43a8fa7c0158e7f30d6040d2481e # v8.2.1 + # Cold: no sticky disk or EAS cache that trusted builds would restore. + - name: Build + uses: ./.trusted/.github/actions/eas-local-build with: - eas-version: latest - eas-cache: false - token: ${{ secrets.EXPO_PREVIEW_TOKEN }} - packager: npm - - - name: Build tools - run: | - command -v pod || sudo gem install cocoapods --no-document - command -v fastlane || brew install fastlane - pod --version - fastlane --version | tail -1 - - - name: Pull preview environment variables - working-directory: apps/mobile - env: - EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }} - run: | - eas env:pull preview --non-interactive - node --input-type=module <<'NODE' - import { appendFileSync, readFileSync } from "node:fs"; - import { parseEnv } from "node:util"; - const env = parseEnv(readFileSync(".env.local", "utf8")); - const googleServicesFile = env.T3CODE_ANDROID_GOOGLE_SERVICES_FILE; - if (googleServicesFile) { - appendFileSync(process.env.GITHUB_ENV, `T3CODE_ANDROID_GOOGLE_SERVICES_FILE=${googleServicesFile}\n`); - } - NODE + platform: ios + profile: preview:dev + environment: preview + expo-token: ${{ secrets.EXPO_PREVIEW_TOKEN }} + output: ${{ runner.temp }}/t3code-preview.ipa + cache: "false" - id: build - name: Build and upload + name: Upload working-directory: apps/mobile shell: bash env: EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }} run: | - export EAS_LOCAL_BUILD_WORKINGDIR="$RUNNER_TEMP/eas-build" - eas build --local --platform ios --profile preview:dev --non-interactive --output "$RUNNER_TEMP/t3code-preview.ipa" eas upload --platform ios --build-path "$RUNNER_TEMP/t3code-preview.ipa" --fingerprint "$FINGERPRINT" --non-interactive --json | tee "$RUNNER_TEMP/upload.json" link="$(sed -n '/^{/,$p' "$RUNNER_TEMP/upload.json" | jq -r '.url // empty')" if [ -z "$link" ]; then diff --git a/.github/workflows/mobile-eas-preview.yml b/.github/workflows/mobile-eas-preview.yml index 7bc68234e364b..53061f25e9c48 100644 --- a/.github/workflows/mobile-eas-preview.yml +++ b/.github/workflows/mobile-eas-preview.yml @@ -47,32 +47,10 @@ jobs: with: fetch-depth: 0 - # The steps below run the PR's dependency and build scripts; they get no token. - # checkout keeps it in a file under RUNNER_TEMP that .git/config includes, so both - # go. Asking checkout not to persist it fails here: that cleanup runs `git submodule - # foreach`, which stops at the vendored gitlinks under .repos (no .gitmodules entry). + # The steps below run dependency and build scripts; they get no token. - name: Drop checkout credentials if: steps.expo-token.outputs.present == 'true' - run: | - for dir in . .trusted; do - [ -e "$dir/.git" ] || continue - git -C "$dir" config --local --get-regexp '^includeif\..*\.path$' | while read -r key file; do - rm -f "$file" - git -C "$dir" config --local --unset-all "$key" - done - git -C "$dir" config --local --name-only --get-regexp '^http\..*\.extraheader$' | while read -r key; do - git -C "$dir" config --local --unset-all "$key" - done - if git -C "$dir" config --local --get-regexp 'extraheader|includeif'; then - echo "::error::checkout credentials are still configured in $dir" - exit 1 - fi - done - rm -f "$RUNNER_TEMP"/git-credentials-*.config - if ls "$RUNNER_TEMP"/git-credentials-*.config 2>/dev/null; then - echo "::error::a checkout credential file is still present" - exit 1 - fi + uses: ./.github/actions/drop-checkout-credentials - name: Setup Vite+ if: steps.expo-token.outputs.present == 'true' @@ -191,32 +169,10 @@ jobs: with: fetch-depth: 0 - # The steps below run the PR's dependency and build scripts; they get no token. - # checkout keeps it in a file under RUNNER_TEMP that .git/config includes, so both - # go. Asking checkout not to persist it fails here: that cleanup runs `git submodule - # foreach`, which stops at the vendored gitlinks under .repos (no .gitmodules entry). + # The steps below run dependency and build scripts; they get no token. - name: Drop checkout credentials if: steps.expo-token.outputs.present == 'true' - run: | - for dir in . .trusted; do - [ -e "$dir/.git" ] || continue - git -C "$dir" config --local --get-regexp '^includeif\..*\.path$' | while read -r key file; do - rm -f "$file" - git -C "$dir" config --local --unset-all "$key" - done - git -C "$dir" config --local --name-only --get-regexp '^http\..*\.extraheader$' | while read -r key; do - git -C "$dir" config --local --unset-all "$key" - done - if git -C "$dir" config --local --get-regexp 'extraheader|includeif'; then - echo "::error::checkout credentials are still configured in $dir" - exit 1 - fi - done - rm -f "$RUNNER_TEMP"/git-credentials-*.config - if ls "$RUNNER_TEMP"/git-credentials-*.config 2>/dev/null; then - echo "::error::a checkout credential file is still present" - exit 1 - fi + uses: ./.github/actions/drop-checkout-credentials - name: Setup Vite+ if: steps.expo-token.outputs.present == 'true' @@ -330,124 +286,27 @@ jobs: echo "link=$(git tag -l --format='%(contents:subject)' "$BUILD_TAG")" >> "$GITHUB_OUTPUT" fi - # The steps below run the PR's dependency and build scripts; they get no token. - # checkout keeps it in a file under RUNNER_TEMP that .git/config includes, so both - # go. Asking checkout not to persist it fails here: that cleanup runs `git submodule - # foreach`, which stops at the vendored gitlinks under .repos (no .gitmodules entry). + # The steps below run dependency and build scripts; they get no token. - name: Drop checkout credentials - run: | - for dir in . .trusted; do - [ -e "$dir/.git" ] || continue - git -C "$dir" config --local --get-regexp '^includeif\..*\.path$' | while read -r key file; do - rm -f "$file" - git -C "$dir" config --local --unset-all "$key" - done - git -C "$dir" config --local --name-only --get-regexp '^http\..*\.extraheader$' | while read -r key; do - git -C "$dir" config --local --unset-all "$key" - done - if git -C "$dir" config --local --get-regexp 'extraheader|includeif'; then - echo "::error::checkout credentials are still configured in $dir" - exit 1 - fi - done - rm -f "$RUNNER_TEMP"/git-credentials-*.config - if ls "$RUNNER_TEMP"/git-credentials-*.config 2>/dev/null; then - echo "::error::a checkout credential file is still present" - exit 1 - fi + uses: ./.github/actions/drop-checkout-credentials - - name: Prepare build directories - if: steps.record.outputs.built != 'true' - run: | - cache="$HOME/.cache/t3-android-preview" - { - echo "GRADLE_USER_HOME=$cache/gradle" - echo "CCACHE_DIR=$cache/ccache" - echo "CCACHE_MAXSIZE=5G" - # Gradle installs the NDK fresh each run, so key on the compiler's version, not its mtime. - echo "CCACHE_COMPILERCHECK=%compiler% -v" - echo "pnpm_config_store_dir=$cache/pnpm-store" - # eas build --local copies the project here. A fixed path keeps - # ccache keys stable, and the same disk as the store lets pnpm hardlink. - echo "EAS_LOCAL_BUILD_WORKINGDIR=$cache/eas-build" - echo "CCACHE_BASEDIR=$cache/eas-build" - echo "CMAKE_C_COMPILER_LAUNCHER=ccache" - echo "CMAKE_CXX_COMPILER_LAUNCHER=ccache" - } >> "$GITHUB_ENV" - - - name: Mount build cache + - name: Build if: steps.record.outputs.built != 'true' - uses: useblacksmith/stickydisk@3f6be1451e3cad893b778bc93495c80452a5da26 # v1.7.2 + uses: ./.github/actions/eas-local-build with: - key: ${{ github.repository }}-android-preview-build - path: ~/.cache/t3-android-preview - - - name: Configure Gradle and ccache - if: steps.record.outputs.built != 'true' - run: | - mkdir -p "$GRADLE_USER_HOME" "$CCACHE_DIR" - rm -rf "$EAS_LOCAL_BUILD_WORKINGDIR" - # No daemon: nothing should hold the sticky disk open when it is committed. - printf 'org.gradle.caching=true\norg.gradle.daemon=false\n' > "$GRADLE_USER_HOME/gradle.properties" - sudo sed -i 's|http://|https://|g' /etc/apt/blacksmith-ubuntu-mirrors.txt /etc/apt/sources.list.d/ubuntu.sources - sudo apt-get update -qq && sudo apt-get install -y -qq ccache - ccache --zero-stats - - - name: Setup Node - if: steps.record.outputs.built != 'true' - uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6.5.0 - with: - node-version-file: package.json - - - name: Enable corepack - if: steps.record.outputs.built != 'true' - run: | - npm install --global corepack@latest - corepack enable - pnpm --version - - - name: Install mobile dependencies - if: steps.record.outputs.built != 'true' - run: pnpm install --frozen-lockfile --prefer-offline --filter=@t3tools/mobile... - - - name: Setup Java - if: steps.record.outputs.built != 'true' - uses: actions/setup-java@b6effb05e454b25005698d916606bdc6ffcbf961 # v5.7.0 - with: - distribution: temurin - java-version: 17 - - - name: Setup EAS - if: steps.record.outputs.built != 'true' - uses: expo/expo-github-action@c7b66a9c327a43a8fa7c0158e7f30d6040d2481e # v8.2.1 - with: - eas-version: latest - token: ${{ secrets.EXPO_PREVIEW_TOKEN }} - packager: npm - - - name: Pull preview environment variables - if: steps.record.outputs.built != 'true' - working-directory: apps/mobile - run: | - eas env:pull preview --non-interactive - node --input-type=module <<'NODE' - import { appendFileSync, readFileSync } from "node:fs"; - import { parseEnv } from "node:util"; - const env = parseEnv(readFileSync(".env.local", "utf8")); - const googleServicesFile = env.T3CODE_ANDROID_GOOGLE_SERVICES_FILE; - if (googleServicesFile) { - appendFileSync(process.env.GITHUB_ENV, `T3CODE_ANDROID_GOOGLE_SERVICES_FILE=${googleServicesFile}\n`); - } - NODE + platform: android + profile: preview:dev + environment: preview + expo-token: ${{ secrets.EXPO_PREVIEW_TOKEN }} + output: ${{ runner.temp }}/t3code-preview.apk # `eas upload` hosts the APK on EAS and returns the page testers install from. - id: build - name: Build and upload + name: Upload if: steps.record.outputs.built != 'true' working-directory: apps/mobile shell: bash run: | - eas build --local --platform android --profile preview:dev --non-interactive --output "$RUNNER_TEMP/t3code-preview.apk" eas upload --platform android --build-path "$RUNNER_TEMP/t3code-preview.apk" --fingerprint "$FINGERPRINT" --non-interactive --json | tee "$RUNNER_TEMP/upload.json" link="$(sed -n '/^{/,$p' "$RUNNER_TEMP/upload.json" | jq -r '.url // empty')" if [ -z "$link" ]; then @@ -455,25 +314,16 @@ jobs: exit 1 fi echo "link=$link" >> "$GITHUB_OUTPUT" - { - echo "### Android preview client for \`$FINGERPRINT\`: $link" - echo '```' - ccache --show-stats - echo '```' - } >> "$GITHUB_STEP_SUMMARY" + echo "### Android preview client for \`$FINGERPRINT\`: $link" >> "$GITHUB_STEP_SUMMARY" # The tag is the record later runs look up; its message is the install link. - name: Record build if: steps.record.outputs.built != 'true' - env: - LINK: ${{ steps.build.outputs.link }} - GH_TOKEN: ${{ github.token }} - run: | - git config user.name "github-actions[bot]" - git config user.email "41898282+github-actions[bot]@users.noreply.github.com" - git tag -a "$BUILD_TAG" -m "$LINK" - # Only this step has the token; the checkout's copy was removed before the build. - git push "https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" "refs/tags/$BUILD_TAG" + uses: ./.github/actions/record-build-tag + with: + tag: ${{ env.BUILD_TAG }} + message: ${{ steps.build.outputs.link }} + token: ${{ github.token }} ios-build: name: iOS preview client @@ -520,104 +370,27 @@ jobs: fi fi - # The steps below run the PR's dependency and build scripts; they get no token. - # checkout keeps it in a file under RUNNER_TEMP that .git/config includes, so both - # go. Asking checkout not to persist it fails here: that cleanup runs `git submodule - # foreach`, which stops at the vendored gitlinks under .repos (no .gitmodules entry). + # The steps below run dependency and build scripts; they get no token. - name: Drop checkout credentials - run: | - for dir in . .trusted; do - [ -e "$dir/.git" ] || continue - git -C "$dir" config --local --get-regexp '^includeif\..*\.path$' | while read -r key file; do - rm -f "$file" - git -C "$dir" config --local --unset-all "$key" - done - git -C "$dir" config --local --name-only --get-regexp '^http\..*\.extraheader$' | while read -r key; do - git -C "$dir" config --local --unset-all "$key" - done - if git -C "$dir" config --local --get-regexp 'extraheader|includeif'; then - echo "::error::checkout credentials are still configured in $dir" - exit 1 - fi - done - rm -f "$RUNNER_TEMP"/git-credentials-*.config - if ls "$RUNNER_TEMP"/git-credentials-*.config 2>/dev/null; then - echo "::error::a checkout credential file is still present" - exit 1 - fi - - - name: Select Xcode 27.1 - if: steps.record.outputs.built != 'true' - run: | - xcode=/Applications/Xcode_27.1.app - if [ ! -d "$xcode" ]; then - ls -d /Applications/Xcode*.app - echo "::error::Xcode 27.1 is not on this runner." - exit 1 - fi - sudo xcode-select -s "$xcode/Contents/Developer" - xcodebuild -version - echo "- Xcode: \`$(basename "$xcode")\`" >> "$GITHUB_STEP_SUMMARY" + uses: ./.github/actions/drop-checkout-credentials - # EAS's credentials step fails to import the distribution certificate without it. - - name: Install Apple WWDR G3 intermediate + - name: Build if: steps.record.outputs.built != 'true' - run: | - curl -fsSL -o "$RUNNER_TEMP/AppleWWDRCAG3.cer" https://www.apple.com/certificateauthority/AppleWWDRCAG3.cer - sudo security add-certificates -k /Library/Keychains/System.keychain "$RUNNER_TEMP/AppleWWDRCAG3.cer" || true - security find-certificate -c "Apple Worldwide Developer Relations" /Library/Keychains/System.keychain | grep -m1 labl - - - name: Enable corepack - if: steps.record.outputs.built != 'true' - run: | - npm install --global corepack@latest - corepack enable - pnpm --version - - - name: Install mobile dependencies - if: steps.record.outputs.built != 'true' - run: pnpm install --frozen-lockfile --prefer-offline --filter=@t3tools/mobile... - - - name: Setup EAS - if: steps.record.outputs.built != 'true' - uses: expo/expo-github-action@c7b66a9c327a43a8fa7c0158e7f30d6040d2481e # v8.2.1 + uses: ./.github/actions/eas-local-build with: - eas-version: latest - token: ${{ secrets.EXPO_PREVIEW_TOKEN }} - packager: npm - - - name: Build tools - if: steps.record.outputs.built != 'true' - run: | - command -v pod || sudo gem install cocoapods --no-document - command -v fastlane || brew install fastlane - pod --version - fastlane --version | tail -1 - - - name: Pull preview environment variables - if: steps.record.outputs.built != 'true' - working-directory: apps/mobile - run: | - eas env:pull preview --non-interactive - node --input-type=module <<'NODE' - import { appendFileSync, readFileSync } from "node:fs"; - import { parseEnv } from "node:util"; - const env = parseEnv(readFileSync(".env.local", "utf8")); - const googleServicesFile = env.T3CODE_ANDROID_GOOGLE_SERVICES_FILE; - if (googleServicesFile) { - appendFileSync(process.env.GITHUB_ENV, `T3CODE_ANDROID_GOOGLE_SERVICES_FILE=${googleServicesFile}\n`); - } - NODE + platform: ios + profile: preview:dev + environment: preview + expo-token: ${{ secrets.EXPO_PREVIEW_TOKEN }} + output: ${{ runner.temp }}/t3code-preview.ipa # EAS hosts the signed client and returns its install page. - id: build - name: Build and upload + name: Upload if: steps.record.outputs.built != 'true' working-directory: apps/mobile shell: bash run: | - export EAS_LOCAL_BUILD_WORKINGDIR="$RUNNER_TEMP/eas-build" - eas build --local --platform ios --profile preview:dev --non-interactive --output "$RUNNER_TEMP/t3code-preview.ipa" eas upload --platform ios --build-path "$RUNNER_TEMP/t3code-preview.ipa" --fingerprint "$FINGERPRINT" --non-interactive --json | tee "$RUNNER_TEMP/upload.json" link="$(sed -n '/^{/,$p' "$RUNNER_TEMP/upload.json" | jq -r '.url // empty')" if [ -z "$link" ]; then @@ -625,28 +398,30 @@ jobs: exit 1 fi echo "link=$link" >> "$GITHUB_OUTPUT" - { - echo "### iOS preview client for \`$FINGERPRINT\`: $link" - } >> "$GITHUB_STEP_SUMMARY" + echo "### iOS preview client for \`$FINGERPRINT\`: $link" >> "$GITHUB_STEP_SUMMARY" # The tag stores the install link and provisioning-profile expiry. - - name: Record build + - id: profile + name: Read the provisioning profile if: steps.record.outputs.built != 'true' env: LINK: ${{ steps.build.outputs.link }} - GH_TOKEN: ${{ github.token }} run: | - git config user.name "github-actions[bot]" - git config user.email "41898282+github-actions[bot]@users.noreply.github.com" profile_path="$(unzip -Z -1 "$RUNNER_TEMP/t3code-preview.ipa" | grep -E '^Payload/[^/]+\.app/embedded\.mobileprovision$')" - test -n "$profile_path" unzip -p "$RUNNER_TEMP/t3code-preview.ipa" "$profile_path" > "$RUNNER_TEMP/preview.mobileprovision" security cms -D -i "$RUNNER_TEMP/preview.mobileprovision" > "$RUNNER_TEMP/preview-profile.plist" record="$(node .github/scripts/mobile-ios-preview-record.cjs create "$LINK" "$RUNNER_TEMP/preview-profile.plist")" - old_tag="$(git rev-parse -q --verify "refs/tags/$BUILD_TAG" || true)" - git tag -f -a "$BUILD_TAG" -m "$record" - # Only this step has the token; the checkout's copy was removed before the build. - git push --force-with-lease="refs/tags/$BUILD_TAG:$old_tag" "https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" "refs/tags/$BUILD_TAG" + echo "record=$record" >> "$GITHUB_OUTPUT" + + # Replaces an expired record for the same fingerprint. + - name: Record build + if: steps.record.outputs.built != 'true' + uses: ./.github/actions/record-build-tag + with: + tag: ${{ env.BUILD_TAG }} + message: ${{ steps.profile.outputs.record }} + replace: "true" + token: ${{ github.token }} comment: name: Mobile preview comment diff --git a/.github/workflows/mobile-eas-production.yml b/.github/workflows/mobile-eas-production.yml index cdabb1e4b7ca3..fe4d7456acdf4 100644 --- a/.github/workflows/mobile-eas-production.yml +++ b/.github/workflows/mobile-eas-production.yml @@ -7,10 +7,8 @@ name: Mobile EAS Production # runner, with corepack pinning pnpm 10.24 in eas.json, local == build. # # Every merge reconciles native fingerprints and publishes matching OTAs. -# Android store builds run here with `eas build --local` on Blacksmith (the -# android job). iOS builds run on EAS unless the MOBILE_IOS_LOCAL_BUILDS -# repository variable is `true`, which moves them to the ios job the same way. -# Local builds never appear in `eas build:list`, so each finished local build +# Store builds run here with `eas build --local` on Blacksmith (the android and +# ios jobs). Local builds never appear in `eas build:list`, so each finished build # pushes a mobile-android-build// or # mobile-ios-build/// tag that the reconcile reads. # iOS prereleases use the testflight profile (3.0.0); App Store binaries use @@ -253,17 +251,9 @@ jobs: echo "> Apple closes an iOS release train after App Store approval. Before building iOS, confirm \`$version\` is newer than the approved App Store version." } >> "$GITHUB_STEP_SUMMARY" - - name: Build and submit iOS (manual) - if: steps.expo-token.outputs.present == 'true' && github.event_name == 'workflow_dispatch' && inputs.mode == 'build' && inputs.platform != 'android' && vars.MOBILE_IOS_LOCAL_BUILDS != 'true' - working-directory: apps/mobile - env: - EXPO_TOKEN: ${{ secrets.EXPO_PRODUCTION_TOKEN }} - MOBILE_BUILD_PROFILE: ${{ inputs.profile || 'production' }} - run: eas build --platform ios --profile "$MOBILE_BUILD_PROFILE" --auto-submit --non-interactive --no-wait - - id: ios_manual name: Queue iOS build (manual) - if: steps.expo-token.outputs.present == 'true' && github.event_name == 'workflow_dispatch' && inputs.mode == 'build' && inputs.platform != 'android' && vars.MOBILE_IOS_LOCAL_BUILDS == 'true' + if: steps.expo-token.outputs.present == 'true' && github.event_name == 'workflow_dispatch' && inputs.mode == 'build' && inputs.platform != 'android' working-directory: apps/mobile env: EXPO_TOKEN: ${{ secrets.EXPO_PRODUCTION_TOKEN }} @@ -351,7 +341,6 @@ jobs: working-directory: apps/mobile env: EXPO_TOKEN: ${{ secrets.EXPO_PRODUCTION_TOKEN }} - IOS_LOCAL_BUILDS: ${{ vars.MOBILE_IOS_LOCAL_BUILDS == 'true' }} run: | failed=0 message="$(git log -1 --pretty=%s | head -c 120) ($(git rev-parse --short=9 HEAD))" @@ -374,19 +363,16 @@ jobs: latest_version="$(eas build:list --platform "$platform" --build-profile "$profile" --limit 1 --json --non-interactive | jq -r '.[0].appVersion // "none"')" # Local builds (the android and ios jobs) record each build as a tag; # EAS records still count for builds made before that. - tag_prefix="" if [ "$platform" = "android" ]; then tag_prefix="mobile-android-build" - elif [ "$IOS_LOCAL_BUILDS" = "true" ]; then + else tag_prefix="mobile-ios-build/$profile" fi - if [ -n "$tag_prefix" ]; then - if [ -n "$(git tag -l "$tag_prefix/*/$hash")" ]; then - finished=$((finished + 1)) - fi - if [ -n "$(git tag -l "$tag_prefix/$version/*")" ]; then - latest_version="$version" - fi + if [ -n "$(git tag -l "$tag_prefix/*/$hash")" ]; then + finished=$((finished + 1)) + fi + if [ -n "$(git tag -l "$tag_prefix/$version/*")" ]; then + latest_version="$version" fi if [ "$finished" -gt 0 ]; then @@ -441,23 +427,14 @@ jobs: echo ":building_construction: android: building and submitting $version in the Android store build job ($reason)" >> "$GITHUB_STEP_SUMMARY" continue fi - if [ "$platform" = "ios" ] && [ "$IOS_LOCAL_BUILDS" = "true" ]; then - { - echo "ios_build=true" - echo "ios_profile=$profile" - echo "ios_fingerprint=$hash" - echo "ios_version=$version" - echo "ios_sha=$(git rev-parse HEAD)" - } >> "$GITHUB_OUTPUT" - echo ":building_construction: ios/$profile: building and submitting $version in the iOS store build job ($reason)" >> "$GITHUB_STEP_SUMMARY" - continue - fi - if eas build --platform "$platform" --profile "$profile" --auto-submit --non-interactive --no-wait; then - echo ":building_construction: $platform: scheduled production build and submission for $version ($reason)" >> "$GITHUB_STEP_SUMMARY" - else - failed=1 - echo ":x: $platform: production build or submission failed for $version" >> "$GITHUB_STEP_SUMMARY" - fi + { + echo "ios_build=true" + echo "ios_profile=$profile" + echo "ios_fingerprint=$hash" + echo "ios_version=$version" + echo "ios_sha=$(git rev-parse HEAD)" + } >> "$GITHUB_OUTPUT" + echo ":building_construction: ios/$profile: building and submitting $version in the iOS store build job ($reason)" >> "$GITHUB_STEP_SUMMARY" done exit "$failed" @@ -476,9 +453,9 @@ jobs: fi done - # `eas build --local` on Blacksmith, ARM-only, without release lint (see the - # production profile in eas.json), with Gradle, ccache, and pnpm state on a - # sticky disk. Its own concurrency group keeps later OTAs from waiting on it. + # ARM-only, without release lint (see the production profile in eas.json), + # with Gradle, ccache, and pnpm state on a sticky disk. Its own concurrency + # group keeps later OTAs from waiting on it. android: name: Android store build needs: production @@ -503,20 +480,9 @@ jobs: ref: ${{ needs.production.outputs.android_sha }} fetch-depth: 0 - # Dependency scripts and the build run in this job; only Record build gets a token. - # persist-credentials: false would clean up via `git submodule foreach`, which fails on .repos gitlinks. + # The steps below run dependency and build scripts; they get no token. - name: Drop checkout credentials - run: | - git config --local --unset-all http.https://github.com/.extraheader || true - { git config --local --name-only --get-regexp '^includeif\.' || true; } | while read -r key; do - path=$(git config --local --get "$key") - git config --local --unset-all "$key" - case "$path" in "$RUNNER_TEMP"/*) rm -f "$path" ;; esac - done - if git config --local --get-regexp '^(http\..*\.extraheader|includeif\.)'; then - echo "::error::Checkout credentials are still configured" - exit 1 - fi + uses: ./.github/actions/drop-checkout-credentials # A queued run can follow a build of the same fingerprint; manual runs always build. - id: record @@ -529,111 +495,22 @@ jobs: echo ":white_check_mark: android: \`$BUILD_TAG\` was built by an earlier run" >> "$GITHUB_STEP_SUMMARY" fi - - name: Prepare build directories - if: steps.record.outputs.built != 'true' - run: | - cache="$HOME/.cache/t3-android-store" - { - echo "GRADLE_USER_HOME=$cache/gradle" - echo "CCACHE_DIR=$cache/ccache" - echo "CCACHE_MAXSIZE=5G" - # Gradle installs the NDK fresh each run, so key on the compiler's version, not its mtime. - echo "CCACHE_COMPILERCHECK=%compiler% -v" - echo "pnpm_config_store_dir=$cache/pnpm-store" - # eas build --local copies the project here. A fixed path keeps - # ccache keys stable, and the same disk as the store lets pnpm hardlink. - echo "EAS_LOCAL_BUILD_WORKINGDIR=$cache/eas-build" - echo "CCACHE_BASEDIR=$cache/eas-build" - # CMake (3.17+) reads these, and AGP passes Gradle's environment through. - echo "CMAKE_C_COMPILER_LAUNCHER=ccache" - echo "CMAKE_CXX_COMPILER_LAUNCHER=ccache" - } >> "$GITHUB_ENV" - - - name: Mount build cache - if: steps.record.outputs.built != 'true' - uses: useblacksmith/stickydisk@3f6be1451e3cad893b778bc93495c80452a5da26 # v1.7.2 - with: - key: ${{ github.repository }}-android-store-build - path: ~/.cache/t3-android-store - - - name: Configure Gradle and ccache - if: steps.record.outputs.built != 'true' - run: | - mkdir -p "$GRADLE_USER_HOME" "$CCACHE_DIR" - rm -rf "$EAS_LOCAL_BUILD_WORKINGDIR" - # No daemon: nothing should hold the sticky disk open when it is committed. - printf 'org.gradle.caching=true\norg.gradle.daemon=false\n' > "$GRADLE_USER_HOME/gradle.properties" - sudo sed -i 's|http://|https://|g' /etc/apt/blacksmith-ubuntu-mirrors.txt /etc/apt/sources.list.d/ubuntu.sources - sudo apt-get update -qq && sudo apt-get install -y -qq ccache - ccache --zero-stats - - - name: Setup Node - if: steps.record.outputs.built != 'true' - uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6.5.0 - with: - node-version-file: package.json - - - name: Enable corepack - if: steps.record.outputs.built != 'true' - run: | - npm install --global corepack@latest - corepack enable - pnpm --version - - # EAS commands load app.config.ts, whose plugins must resolve. Keep install - # scripts: some download native assets that the build's fingerprint check hashes. - - name: Install mobile dependencies - if: steps.record.outputs.built != 'true' - run: pnpm install --frozen-lockfile --prefer-offline --filter=@t3tools/mobile... - - - name: Setup Java - if: steps.record.outputs.built != 'true' - uses: actions/setup-java@b6effb05e454b25005698d916606bdc6ffcbf961 # v5.7.0 - with: - distribution: temurin - java-version: 17 - - - name: Setup EAS + - name: Build if: steps.record.outputs.built != 'true' - uses: expo/expo-github-action@c7b66a9c327a43a8fa7c0158e7f30d6040d2481e # v8.2.1 + uses: ./.github/actions/eas-local-build with: - eas-version: latest - token: ${{ secrets.EXPO_PRODUCTION_TOKEN }} - packager: npm - - - name: Pull production environment variables - if: steps.record.outputs.built != 'true' - working-directory: apps/mobile - run: | - eas env:pull production --non-interactive - node --input-type=module <<'NODE' - import { appendFileSync, readFileSync } from "node:fs"; - import { parseEnv } from "node:util"; - const env = parseEnv(readFileSync(".env.local", "utf8")); - const googleServicesFile = env.T3CODE_ANDROID_GOOGLE_SERVICES_FILE; - if (!googleServicesFile) { - throw new Error("T3CODE_ANDROID_GOOGLE_SERVICES_FILE is missing from the production environment"); - } - appendFileSync(process.env.GITHUB_ENV, `T3CODE_ANDROID_GOOGLE_SERVICES_FILE=${googleServicesFile}\n`); - NODE - - - name: Build + platform: android + profile: production + environment: production + expo-token: ${{ secrets.EXPO_PRODUCTION_TOKEN }} + output: ${{ runner.temp }}/t3code.aab + sourcemaps-r2: ${{ secrets.MOBILE_SOURCEMAPS_R2 }} + + - name: Check ABIs if: steps.record.outputs.built != 'true' - working-directory: apps/mobile - env: - # EAS secret variables don't reach local builds; the eas-build-on-success - # hook needs these to upload the bundle's source map. - MOBILE_SOURCEMAPS_R2: ${{ secrets.MOBILE_SOURCEMAPS_R2 }} run: | - eas build --local --platform android --profile production --non-interactive --output "$RUNNER_TEMP/t3code.aab" abis="$(unzip -Z1 "$RUNNER_TEMP/t3code.aab" | sed -n 's|^base/lib/\([^/]*\)/.*|\1|p' | sort -u | paste -sd, -)" - { - echo "### Android $BUILD_TAG" - echo "- ABIs: \`$abis\`" - echo '```' - ccache --show-stats - echo '```' - } >> "$GITHUB_STEP_SUMMARY" + echo "### Android $BUILD_TAG: \`$abis\`" >> "$GITHUB_STEP_SUMMARY" if [ "$abis" != "arm64-v8a,armeabi-v7a" ]; then echo "::error::Expected only ARM native libraries in the AAB, found $abis" exit 1 @@ -658,30 +535,16 @@ jobs: # pushed, the next merge rebuilds with a new build number; nothing is lost. - name: Record build if: steps.record.outputs.built != 'true' - env: - GH_TOKEN: ${{ github.token }} + uses: ./.github/actions/record-build-tag + with: + tag: ${{ env.BUILD_TAG }} + token: ${{ github.token }} + + - name: Summarize + if: steps.record.outputs.built != 'true' run: | - sha="$(git rev-parse HEAD)" - # GitHub occasionally fails ref writes server-side ("fatal error in commit_refs"). - for attempt in 1 2 3; do - if gh api "repos/$GITHUB_REPOSITORY/git/ref/tags/$BUILD_TAG" >/dev/null 2>&1; then - echo "$BUILD_TAG exists." - break - fi - if gh api "repos/$GITHUB_REPOSITORY/git/refs" -f ref="refs/tags/$BUILD_TAG" -f sha="$sha" >/dev/null; then - break - fi - if [ "$attempt" = 3 ]; then - echo "::error::Submitted, but could not create $BUILD_TAG; the next merge will rebuild." - exit 1 - fi - sleep $((attempt * 15)) - done echo ":rocket: android: submitted and recorded \`$BUILD_TAG\`" >> "$GITHUB_STEP_SUMMARY" - # Off unless the MOBILE_IOS_LOCAL_BUILDS repository variable is `true`; the - # production job only sets ios_build then. Xcode 27.1 matches the EAS build - # image in eas.json. # No sticky disk: it mounts ext4 and only works on Linux runners. ios: name: iOS store build @@ -708,20 +571,9 @@ jobs: ref: ${{ needs.production.outputs.ios_sha }} fetch-depth: 0 - # Dependency scripts and the build run in this job; only Record build gets a token. - # persist-credentials: false would clean up via `git submodule foreach`, which fails on .repos gitlinks. + # The steps below run dependency and build scripts; they get no token. - name: Drop checkout credentials - run: | - git config --local --unset-all http.https://github.com/.extraheader || true - { git config --local --name-only --get-regexp '^includeif\.' || true; } | while read -r key; do - path=$(git config --local --get "$key") - git config --local --unset-all "$key" - case "$path" in "$RUNNER_TEMP"/*) rm -f "$path" ;; esac - done - if git config --local --get-regexp '^(http\..*\.extraheader|includeif\.)'; then - echo "::error::Checkout credentials are still configured" - exit 1 - fi + uses: ./.github/actions/drop-checkout-credentials # A queued run can follow a build of the same fingerprint; manual runs always build. - id: record @@ -734,78 +586,16 @@ jobs: echo ":white_check_mark: ios: \`$BUILD_TAG\` was built by an earlier run" >> "$GITHUB_STEP_SUMMARY" fi - - name: Select Xcode 27.1 - if: steps.record.outputs.built != 'true' - run: | - xcode=/Applications/Xcode_27.1.app - if [ ! -d "$xcode" ]; then - ls -d /Applications/Xcode*.app - echo "::error::Xcode 27.1 is not on this runner." - exit 1 - fi - sudo xcode-select -s "$xcode/Contents/Developer" - xcodebuild -version - echo "- Xcode: \`$(basename "$xcode")\`" >> "$GITHUB_STEP_SUMMARY" - - # EAS's credentials step fails to import the distribution certificate without it. - - name: Install Apple WWDR G3 intermediate - if: steps.record.outputs.built != 'true' - run: | - curl -fsSL -o "$RUNNER_TEMP/AppleWWDRCAG3.cer" https://www.apple.com/certificateauthority/AppleWWDRCAG3.cer - sudo security add-certificates -k /Library/Keychains/System.keychain "$RUNNER_TEMP/AppleWWDRCAG3.cer" || true - security find-certificate -c "Apple Worldwide Developer Relations" /Library/Keychains/System.keychain | grep -m1 labl - - - name: Setup Node - if: steps.record.outputs.built != 'true' - uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6.5.0 - with: - node-version-file: package.json - - - name: Enable corepack - if: steps.record.outputs.built != 'true' - run: | - npm install --global corepack@latest - corepack enable - pnpm --version - - # EAS commands load app.config.ts, whose plugins must resolve. Keep install - # scripts: some download native assets that the build's fingerprint check hashes. - - name: Install mobile dependencies - if: steps.record.outputs.built != 'true' - run: pnpm install --frozen-lockfile --prefer-offline --filter=@t3tools/mobile... - - - name: Setup EAS - if: steps.record.outputs.built != 'true' - uses: expo/expo-github-action@c7b66a9c327a43a8fa7c0158e7f30d6040d2481e # v8.2.1 - with: - eas-version: latest - token: ${{ secrets.EXPO_PRODUCTION_TOKEN }} - packager: npm - - - name: Build tools - if: steps.record.outputs.built != 'true' - run: | - command -v pod || sudo gem install cocoapods --no-document - command -v fastlane || brew install fastlane - pod --version - fastlane --version | tail -1 - - - name: Pull production environment variables - if: steps.record.outputs.built != 'true' - working-directory: apps/mobile - run: eas env:pull production --non-interactive - - name: Build if: steps.record.outputs.built != 'true' - working-directory: apps/mobile - env: - # EAS secret variables don't reach local builds; the eas-build-on-success - # hook needs these to upload the bundle's source map. - MOBILE_SOURCEMAPS_R2: ${{ secrets.MOBILE_SOURCEMAPS_R2 }} - run: | - # A real path: /var -> /private/var symlinks break the expo-widgets bundle step. - export EAS_LOCAL_BUILD_WORKINGDIR="$RUNNER_TEMP/eas-build" - eas build --local --platform ios --profile "$PROFILE" --non-interactive --output "$RUNNER_TEMP/t3code.ipa" + uses: ./.github/actions/eas-local-build + with: + platform: ios + profile: ${{ env.PROFILE }} + environment: production + expo-token: ${{ secrets.EXPO_PRODUCTION_TOKEN }} + output: ${{ runner.temp }}/t3code.ipa + sourcemaps-r2: ${{ secrets.MOBILE_SOURCEMAPS_R2 }} - name: Submit to App Store Connect if: steps.record.outputs.built != 'true' @@ -816,23 +606,12 @@ jobs: # pushed, the next merge rebuilds with a new build number; nothing is lost. - name: Record build if: steps.record.outputs.built != 'true' - env: - GH_TOKEN: ${{ github.token }} + uses: ./.github/actions/record-build-tag + with: + tag: ${{ env.BUILD_TAG }} + token: ${{ github.token }} + + - name: Summarize + if: steps.record.outputs.built != 'true' run: | - sha="$(git rev-parse HEAD)" - # GitHub occasionally fails ref writes server-side ("fatal error in commit_refs"). - for attempt in 1 2 3; do - if gh api "repos/$GITHUB_REPOSITORY/git/ref/tags/$BUILD_TAG" >/dev/null 2>&1; then - echo "$BUILD_TAG exists." - break - fi - if gh api "repos/$GITHUB_REPOSITORY/git/refs" -f ref="refs/tags/$BUILD_TAG" -f sha="$sha" >/dev/null; then - break - fi - if [ "$attempt" = 3 ]; then - echo "::error::Submitted, but could not create $BUILD_TAG; the next merge will rebuild." - exit 1 - fi - sleep $((attempt * 15)) - done echo ":rocket: ios: submitted and recorded \`$BUILD_TAG\`" >> "$GITHUB_STEP_SUMMARY" From 943de8fe9d5dc751a06f4985dfae59cbe1724359 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 12:30:42 -0800 Subject: [PATCH 042/253] fix(server): a Claude request that gets no answer fails after 10 minutes instead of running forever (#37728) --- .../Adapters/ClaudeAdapterV2.test.ts | 120 +++++++++++++++++ .../Adapters/ClaudeAdapterV2.ts | 122 +++++++++++++----- 2 files changed, 212 insertions(+), 30 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index cf533de93e4d6..b2b27327b66dc 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -15224,6 +15224,126 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); + // regression: #37708 a gateway that holds the request open leaves Claude silent after "requesting" + it.effect.each([ + { case: "after the prompt starts", requesting: false, backgroundProgress: false }, + { case: "after a request goes out", requesting: true, backgroundProgress: false }, + { case: "while a skill it forked runs", requesting: false, forkedTask: true }, + { + case: "while an earlier background agent starts a nested task", + requesting: false, + nestedTaskStarted: true, + }, + { + case: "while an earlier background task reports progress", + requesting: false, + backgroundProgress: true, + }, + ])( + "does not keep a turn silently running when Claude goes quiet $case", + ({ requesting, backgroundProgress = false, forkedTask = false, nestedTaskStarted = false }) => + Effect.gen(function* () { + const harness = yield* makeWakeHarness; + const now = yield* DateTime.now; + yield* harness.runtime.startTurn( + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now, + attemptId: RunAttemptId.make("attempt-claude-silent-after-started"), + text: "start again", + attachments: [], + }), + ); + // Captured from Claude Code 2.1.325 against a gateway that never answers /v1/messages. + for (const [index, state] of (["queued", "started"] as const).entries()) { + yield* harness.offerAndWait( + claudeSdkFrame({ + type: "command_lifecycle", + command_uuid: harness.promptUuid(0), + state, + uuid: `00000000-0000-4000-8000-00000000079${index}`, + session_id: WAKE_NATIVE_SESSION, + }), + ); + } + if (requesting) { + yield* harness.offerAndWait( + claudeSdkFrame({ + type: "system", + subtype: "status", + status: "requesting", + uuid: "00000000-0000-4000-8000-000000000792", + session_id: WAKE_NATIVE_SESSION, + }), + ); + } + if (backgroundProgress) { + yield* harness.offerAndWait( + claudeSdkFrame({ + type: "system", + subtype: "task_progress", + task_id: "earlier-background-task", + description: "Earlier background task", + usage: { total_tokens: 1, tool_uses: 0, duration_ms: 1 }, + uuid: "00000000-0000-4000-8000-000000000793", + session_id: WAKE_NATIVE_SESSION, + }), + ); + } + if (nestedTaskStarted) { + yield* harness.offerAndWait( + claudeSdkFrame({ + type: "system", + subtype: "task_started", + task_id: "earlier-nested-task", + description: "Nested task of an earlier background agent", + spawn_depth: 2, + uuid: "00000000-0000-4000-8000-000000000795", + session_id: WAKE_NATIVE_SESSION, + }), + ); + } + if (forkedTask) { + yield* harness.offerAndWait( + claudeSdkFrame({ + type: "system", + subtype: "task_started", + task_id: "forked-skill-task", + description: "Forked skill", + uuid: "00000000-0000-4000-8000-000000000794", + session_id: WAKE_NATIVE_SESSION, + }), + ); + } + assert.lengthOf(harness.terminalEvents(), 0); + + yield* TestClock.adjust("1 hour"); + yield* yieldTicks(50); + const surfaced = + harness.terminalEvents().length > 0 || + harness.events.some( + (event) => + event.type === "turn_item.updated" && + (event.turnItem.type === "system_notice" || event.turnItem.type === "error"), + ); + if (forkedTask) { + assert.lengthOf(harness.terminalEvents(), 0, "a long forked skill was failed as silence"); + } else { + assert.isTrue(surfaced, "an hour of silence after the prompt started surfaced nothing"); + } + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + // regression: #17531 a mod that holds a tool behind a focused pane must fail the turn, not hang it it.effect.each([ { case: "fails a turn whose mod requests focus for a pane T3 cannot show", focus: true }, diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index fa8d3684f0f23..34d81c88d3559 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -4949,6 +4949,8 @@ function formatClaudeUsageLimitWait(waitMs: number): string { interface ActiveClaudeTurnContext { retryBudgetFailure?: OrchestrationV2ProviderFailure; + // Set when the prompt starts or a request goes out; cleared by the turn's next frame. + unansweredRequestSince?: DateTime.Utc | undefined; readonly queryPolicy: ClaudeRuntimeQueryPolicy; readonly input: ProviderAdapter.ProviderAdapterV2TurnInput; readonly autoCompactWindow: string; @@ -6397,40 +6399,73 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( : undefined; }); const retry = yield* isCurrentRetry; - // A user Stop already in flight keeps its interrupted outcome. - if ( - retry === undefined || - (yield* Ref.get(interruptedTurns)).has(context.providerTurnId) - ) - return; - context.retryBudgetFailure = { ...retry.failure, retryable: false }; - const liveQuery = yield* Ref.get(queryContext); - yield* interruptTurn({ - providerThread: context.input.providerThread, - providerTurnId: context.providerTurnId, - }).pipe( - Effect.timeoutOption(CLAUDE_STOP_CONFIRM_TIMEOUT_MS), - Effect.catch((cause) => - Effect.logWarning("orchestration-v2.claude-retry-budget-stop-failed", { cause }), - ), - ); - if (liveQuery === null || (yield* isCurrentRetry) === undefined) return; - // The CLI did not confirm the stop; settle the turn without it. - const ownedLiveQuery = yield* Ref.modify(queryContext, (current) => - current?.query === liveQuery.query ? [true, null] : [false, current], - ); - if (!ownedLiveQuery) return; - liveQuery.stopping = true; - yield* liveQuery.query.close.pipe( - Effect.timeoutOption(CLAUDE_STOP_CONFIRM_TIMEOUT_MS), - Effect.ignore, + if (retry === undefined) return; + yield* failStalledTurn( + context, + { ...retry.failure, retryable: false }, + Effect.map(isCurrentRetry, (current) => current !== undefined), ); - yield* finalizeActiveTurnAfterQueryExit(liveQuery); - yield* endSubagentCallsOfClosedQuery(liveQuery.nativeThreadId, "interrupted"); - yield* Deferred.succeed(liveQuery.closed, undefined); }, ); + // Partial messages are on, so a started prompt or a sent request streams within seconds. + // A gateway can hold the request open without ever letting the CLI time out and retry. + const failUnansweredRequest = Effect.fn("ClaudeAdapterV2.failUnansweredRequest")(function* ( + context: ActiveClaudeTurnContext, + since: DateTime.Utc, + ) { + const isUnanswered = Effect.map( + Ref.get(activeTurn), + (active) => active === context && context.unansweredRequestSince === since, + ); + if (!(yield* isUnanswered)) return; + yield* failStalledTurn( + context, + makeProviderFailure({ + class: "transport_error", + code: "claude_request_unanswered", + message: `Claude sent nothing for ${PROVIDER_RETRY_BUDGET_MS / 60_000} minutes. The API or a gateway in front of it is not responding.`, + retryable: false, + }), + // Once the stop is sent, a frame clearing the silence must not skip forced settlement. + Effect.map(Ref.get(activeTurn), (active) => active === context), + ); + }); + + const failStalledTurn = Effect.fn("ClaudeAdapterV2.failStalledTurn")(function* ( + context: ActiveClaudeTurnContext, + failure: OrchestrationV2ProviderFailure, + stillStalled: Effect.Effect, + ) { + // A user Stop already in flight keeps its interrupted outcome. + if ((yield* Ref.get(interruptedTurns)).has(context.providerTurnId)) return; + context.retryBudgetFailure = failure; + const liveQuery = yield* Ref.get(queryContext); + yield* interruptTurn({ + providerThread: context.input.providerThread, + providerTurnId: context.providerTurnId, + }).pipe( + Effect.timeoutOption(CLAUDE_STOP_CONFIRM_TIMEOUT_MS), + Effect.catch((cause) => + Effect.logWarning("orchestration-v2.claude-retry-budget-stop-failed", { cause }), + ), + ); + if (liveQuery === null || !(yield* stillStalled)) return; + // The CLI did not confirm the stop; settle the turn without it. + const ownedLiveQuery = yield* Ref.modify(queryContext, (current) => + current?.query === liveQuery.query ? [true, null] : [false, current], + ); + if (!ownedLiveQuery) return; + liveQuery.stopping = true; + yield* liveQuery.query.close.pipe( + Effect.timeoutOption(CLAUDE_STOP_CONFIRM_TIMEOUT_MS), + Effect.ignore, + ); + yield* finalizeActiveTurnAfterQueryExit(liveQuery); + yield* endSubagentCallsOfClosedQuery(liveQuery.nativeThreadId, "interrupted"); + yield* Deferred.succeed(liveQuery.closed, undefined); + }); + const rememberProviderThread = /* Provider events include streaming deltas; avoid a span for each update. */ Effect.fnUntraced(function* (providerThread: OrchestrationV2ProviderThread) { @@ -10556,6 +10591,33 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( } return; } + if (context === (yield* Ref.get(activeTurn))) { + const lifecycle = claudePromptLifecycle(message); + if ( + (message.type === "system" && + message.subtype === "status" && + message.status === "requesting") || + (lifecycle?.state === "started" && lifecycle.uuid === context.promptUuid) + ) { + const since = yield* DateTime.now; + context.unansweredRequestSince = since; + yield* failUnansweredRequest(context, since).pipe( + Effect.delay(PROVIDER_RETRY_BUDGET_MS), + Effect.forkIn(sessionScope), + ); + } else if ( + // Background tasks from earlier turns keep streaming while the root request hangs. + // Only the root starts depth-1 tasks, so such a start (a forked skill) is this turn's. + Reflect.get(message, "parent_tool_use_id") == null && + !( + message.type === "system" && + message.subtype.startsWith("task_") && + !(message.subtype === "task_started" && (message.spawn_depth ?? 1) <= 1) + ) + ) { + context.unansweredRequestSince = undefined; + } + } const focusedPlugin = claudeFocusRequestedPanePlugin(message); if (focusedPlugin !== null && context === (yield* Ref.get(activeTurn))) { yield* Effect.logWarning("orchestration-v2.claude-mod-pane-focus-unsupported", { From 50717b8167766d0fc913cb6865cd35c5a637922b Mon Sep 17 00:00:00 2001 From: Martin Bergo Date: Mon, 16 Nov 2026 21:32:24 +0100 Subject: [PATCH 043/253] fix(server): reading t3.json from a cleaned-up worktree no longer logs an error (#37747) Co-authored-by: Martin Bergo <14320143+mInrOz@users.noreply.github.com> Co-authored-by: Julius Marminge --- .../src/workspace/WorkspaceFileSystem.test.ts | 26 +++++++++++++++++++ .../src/workspace/WorkspaceFileSystem.ts | 12 ++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/apps/server/src/workspace/WorkspaceFileSystem.test.ts b/apps/server/src/workspace/WorkspaceFileSystem.test.ts index bc22117ba74ab..f00017e4001ec 100644 --- a/apps/server/src/workspace/WorkspaceFileSystem.test.ts +++ b/apps/server/src/workspace/WorkspaceFileSystem.test.ts @@ -258,6 +258,32 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (i }), ); + it.effect("reports an optional file as absent when the workspace root was removed", () => + Effect.gen(function* () { + const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; + const path = yield* Path.Path; + const cwd = path.join(yield* makeTempDir, "removed-worktree"); + + const result = yield* workspaceFileSystem.readFile({ + cwd, + relativePath: "t3.json", + allowMissing: true, + }); + expect(result).toEqual({ + relativePath: "t3.json", + contents: "", + byteLength: 0, + truncated: false, + exists: false, + }); + + const error = yield* workspaceFileSystem + .readFile({ cwd, relativePath: "t3.json" }) + .pipe(Effect.flip); + expect(error).toMatchObject({ operation: "realpath-workspace-root" }); + }), + ); + it.effect("finishes short reads before decoding UTF-8 text", () => Effect.gen(function* () { const files = yield* WorkspaceFileSystem.WorkspaceFileSystem; diff --git a/apps/server/src/workspace/WorkspaceFileSystem.ts b/apps/server/src/workspace/WorkspaceFileSystem.ts index 494472e4ce68d..98047345d4ff6 100644 --- a/apps/server/src/workspace/WorkspaceFileSystem.ts +++ b/apps/server/src/workspace/WorkspaceFileSystem.ts @@ -315,7 +315,17 @@ export const make = Effect.gen(function* () { operation: "realpath-workspace-root", cause, }), - }); + }).pipe( + // Storage cleanup removes idle worktrees but keeps their path, so nothing below them exists. + Effect.catchIf( + (error) => + input.allowMissing === true && (error.cause as NodeJS.ErrnoException).code === "ENOENT", + () => Effect.succeed(null), + ), + ); + if (realWorkspaceRoot === null) { + return { relativePath: target.relativePath, realTargetPath: null }; + } const resolved = yield* Effect.tryPromise({ try: async () => { let candidate = target.absolutePath; From 70a7cfe5a20f6ea82f5a5b89fe08da13c468d80f Mon Sep 17 00:00:00 2001 From: Alex Vallone <39172404+amv146@users.noreply.github.com> Date: Mon, 16 Nov 2026 12:32:37 -0800 Subject: [PATCH 044/253] fix(azure-devops): deleted files appear in the pull request Code tab (#37745) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Julius Marminge --- .../server/azureDevOpsPullRequestJson.test.ts | 26 ++++++++++++++++--- .../src/server/azureDevOpsPullRequestJson.ts | 14 ++++++---- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/packages/source-control-azure-devops/src/server/azureDevOpsPullRequestJson.test.ts b/packages/source-control-azure-devops/src/server/azureDevOpsPullRequestJson.test.ts index aeb3d2232d7b6..cdd86e31b8303 100644 --- a/packages/source-control-azure-devops/src/server/azureDevOpsPullRequestJson.test.ts +++ b/packages/source-control-azure-devops/src/server/azureDevOpsPullRequestJson.test.ts @@ -317,15 +317,33 @@ describe("decodeIterationChangesJson", () => { item: { path: "/README.md", objectId: "8f80", originalObjectId: "0ca4" }, }, { changeType: "delete", item: { path: "/OLD.md", originalObjectId: "1111" } }, + // Azure names a deleted file only where it came from. + { + changeType: "delete", + originalPath: "/GONE.md", + item: { path: null, objectId: "2222", originalObjectId: "2222" }, + }, + { changeType: "delete", sourceServerItem: "/src/gone.ts", item: { objectId: null } }, + { changeType: "delete", item: { originalObjectId: "3333" } }, + { changeType: "edit", originalPath: "/LOST.md", item: { objectId: "4444" } }, ], }), ), ); - expect(page.changes.map((change) => [change.path, change.changeKind])).toEqual([ - ["DEMO.md", "new"], - ["README.md", "change"], - ["OLD.md", "deleted"], + expect( + page.changes.map((change) => [ + change.path, + change.oldPath, + change.changeKind, + change.objectId, + ]), + ).toEqual([ + ["DEMO.md", "DEMO.md", "new", "ec00"], + ["README.md", "README.md", "change", "8f80"], + ["OLD.md", "OLD.md", "deleted", null], + ["GONE.md", "GONE.md", "deleted", null], + ["src/gone.ts", "src/gone.ts", "deleted", null], ]); }); diff --git a/packages/source-control-azure-devops/src/server/azureDevOpsPullRequestJson.ts b/packages/source-control-azure-devops/src/server/azureDevOpsPullRequestJson.ts index c21de6fee4122..aa05451452b73 100644 --- a/packages/source-control-azure-devops/src/server/azureDevOpsPullRequestJson.ts +++ b/packages/source-control-azure-devops/src/server/azureDevOpsPullRequestJson.ts @@ -579,18 +579,22 @@ export function decodeIterationChangesJson( const decodedChange = decodeChangeEntry(entry); if (Exit.isFailure(decodedChange)) continue; const change = decodedChange.value; - const path = toRepositoryPath(change.item?.path); + // Azure uses either field for renames, depending on route/version. + const sourcePath = + toRepositoryPath(change.sourceServerItem) ?? toRepositoryPath(change.originalPath); + const deleted = toChangeKind(change.changeType, false) === "deleted"; + // A deleted file has no `item.path`; only the source fields name it. + const path = toRepositoryPath(change.item?.path) ?? (deleted ? sourcePath : null); if (path === null) continue; if (change.item?.isFolder === true) continue; if ((change.item?.gitObjectType ?? "blob").toLowerCase() !== "blob") continue; - // Azure uses either field for renames, depending on route/version. - const oldPath = - toRepositoryPath(change.sourceServerItem) ?? toRepositoryPath(change.originalPath) ?? path; + const oldPath = sourcePath ?? path; changes.push({ path, oldPath, changeKind: toChangeKind(change.changeType, oldPath !== path), - objectId: trimmed(change.item?.objectId), + // Viewed marks read a deleted file at the empty revision, not a blob id. + objectId: deleted ? null : trimmed(change.item?.objectId), originalObjectId: trimmed(change.item?.originalObjectId), }); } From f4d0855a50d3baafdcc5b8955a382ac40ba29e07 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 12:48:29 -0800 Subject: [PATCH 045/253] fix(web): PDF attachment chips no longer look like failed ones (#33805) --- apps/web/src/components/ContextChip.tsx | 8 +++++--- apps/web/src/components/chat/TimelineUserMessageRow.tsx | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/apps/web/src/components/ContextChip.tsx b/apps/web/src/components/ContextChip.tsx index 13a19167fe929..dabe1f4844532 100644 --- a/apps/web/src/components/ContextChip.tsx +++ b/apps/web/src/components/ContextChip.tsx @@ -6,7 +6,7 @@ import type * as React from "react"; import { cn } from "~/lib/utils"; const contextChipVariants = cva( - "inline-flex max-w-full items-center gap-[0.2em] font-medium leading-[normal] text-(--context-chip-ink) [--context-chip-ink:var(--context-chip-accent)] [--document-icon-stroke:var(--context-chip-ink)] [&_svg]:block [&_svg]:size-[1em] [&_svg]:shrink-0 [&_svg]:self-center [button&,a&,[data-popup-open]&]:cursor-pointer focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-current rounded-sm disabled:cursor-default", + "inline-flex max-w-full items-center gap-[0.2em] font-medium leading-[normal] text-(--context-chip-ink) [--context-chip-ink:var(--context-chip-message-ink,var(--context-chip-accent))] [--document-icon-stroke:var(--context-chip-ink)] [&_svg]:block [&_svg]:size-[1em] [&_svg]:shrink-0 [&_svg]:self-center [button&,a&,[data-popup-open]&]:cursor-pointer focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-current rounded-sm disabled:cursor-default", { defaultVariants: { kind: "neutral" }, variants: { @@ -30,7 +30,9 @@ const contextChipVariants = cva( thread: "[--context-chip-accent:var(--context-chip-thread)]", environment: "[--context-chip-accent:var(--context-chip-environment)]", citation: "[--context-chip-accent:var(--context-chip-citation)]", - "doc-pdf": "[--context-chip-accent:var(--context-chip-doc-pdf)]", + // PDF red reads as the invalid state, so only the icon carries it. + "doc-pdf": + "[--context-chip-accent:var(--context-chip-doc-pdf)] [--context-chip-ink:var(--context-chip-message-ink,currentColor)] [--document-icon-stroke:var(--context-chip-message-ink,var(--context-chip-accent))]", "doc-word": "[--context-chip-accent:var(--context-chip-doc-word)]", "doc-spreadsheet": "[--context-chip-accent:var(--context-chip-doc-spreadsheet)]", "doc-slides": "[--context-chip-accent:var(--context-chip-doc-slides)]", @@ -43,7 +45,7 @@ const contextChipVariants = cva( state: { unresolved: "data-[state=unresolved]:[--context-chip-accent:currentColor]", invalid: - "data-[state=invalid]:[--context-chip-accent:var(--context-chip-invalid,var(--destructive-foreground))]", + "data-[state=invalid]:[--context-chip-accent:var(--context-chip-invalid,var(--destructive-foreground))] data-[state=invalid]:[--context-chip-ink:var(--context-chip-message-ink,var(--context-chip-accent))]", }, }, }, diff --git a/apps/web/src/components/chat/TimelineUserMessageRow.tsx b/apps/web/src/components/chat/TimelineUserMessageRow.tsx index 3fee2134c3f58..163635a9b6748 100644 --- a/apps/web/src/components/chat/TimelineUserMessageRow.tsx +++ b/apps/web/src/components/chat/TimelineUserMessageRow.tsx @@ -102,7 +102,7 @@ const MEDIA_EDGE_CLASS_NAME = "relative after:pointer-events-none after:absolute after:inset-0 after:z-10 after:rounded-[inherit] after:ring-1 after:ring-inset after:ring-black/9 dark:after:ring-white/12"; const BUBBLE_CHIP_CLASS_NAME = - "[&_[data-slot=context-chip]]:[--context-chip-ink:var(--context-chip-message,var(--context-chip-accent))]"; + "[&_[data-slot=context-chip]]:[--context-chip-message-ink:var(--context-chip-message)]"; function UserFileCard({ file, From 959f422d457bc7c5845cb531996da7a398dd95a3 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 12:51:10 -0800 Subject: [PATCH 046/253] fix(web): screen reader announcements no longer linger after they stop applying (#31676) --- .../chat/OpenInPicker.browser.test.tsx | 2 +- apps/web/src/hooks/useLiveRegion.ts | 7 ++--- apps/web/src/lib/announce.browser.test.ts | 31 ++++++++++++++++--- apps/web/src/lib/announce.ts | 25 ++++++++++++--- 4 files changed, 50 insertions(+), 15 deletions(-) diff --git a/apps/web/src/components/chat/OpenInPicker.browser.test.tsx b/apps/web/src/components/chat/OpenInPicker.browser.test.tsx index da9d7d50a5396..cc246a1f9967c 100644 --- a/apps/web/src/components/chat/OpenInPicker.browser.test.tsx +++ b/apps/web/src/components/chat/OpenInPicker.browser.test.tsx @@ -232,7 +232,7 @@ describe("open in editor announcement", () => { using _timers = fakeTimers(); const regions = () => [...document.querySelectorAll('body > [aria-live="polite"]')]; const spoken = async () => { - await act(async () => vi.runAllTimers()); + await act(async () => vi.advanceTimersByTime(250)); return regions() .map((region) => region.textContent) .join(""); diff --git a/apps/web/src/hooks/useLiveRegion.ts b/apps/web/src/hooks/useLiveRegion.ts index fcfb6cf2fdaee..47e81168c0918 100644 --- a/apps/web/src/hooks/useLiveRegion.ts +++ b/apps/web/src/hooks/useLiveRegion.ts @@ -1,9 +1,6 @@ import { useState } from "react"; -// NVDA reads a polite region's leftover text again when focus returns from a -// dialog, so an announcement is cleared once it has had time to be spoken. -// Clearing doesn't cut off speech that already started. -const CLEAR_AFTER_MS = 7_000; +import { CLEAR_SPOKEN_AFTER_MS } from "../lib/announce"; /** * A polite live region written straight to the DOM, so speaking never @@ -30,7 +27,7 @@ export function createLiveRegion() { // Screen readers ignore a write that leaves the text unchanged, so a repeat // gets a trailing non-breaking space to count as new. region.textContent = region.textContent === text ? `${text}\u00A0` : text; - clearTimer = setTimeout(clear, CLEAR_AFTER_MS); + clearTimer = setTimeout(clear, CLEAR_SPOKEN_AFTER_MS); }; const attach = (node: HTMLElement | null) => { diff --git a/apps/web/src/lib/announce.browser.test.ts b/apps/web/src/lib/announce.browser.test.ts index 72406754e2bbb..5d02802c06f6d 100644 --- a/apps/web/src/lib/announce.browser.test.ts +++ b/apps/web/src/lib/announce.browser.test.ts @@ -33,10 +33,10 @@ describe("announce", () => { Array.from(document.querySelectorAll('[aria-live="polite"]'), (region) => region.textContent); announce("Claude"); - vi.runAllTimers(); + vi.advanceTimersByTime(250); const first = regionTexts(); announce("Claude"); - vi.runAllTimers(); + vi.advanceTimersByTime(250); expect(regionTexts()).toEqual([...first].reverse()); expect(regionTexts()).toContain("Claude"); @@ -48,15 +48,38 @@ describe("announce", () => { announce("Settled 1 thread, Ctrl+Z to undo", "undo"); announce("Opening in VS Code"); announce("Settled 2 threads, Ctrl+Z to undo", "undo"); - vi.runAllTimers(); + vi.advanceTimersByTime(250); expect(spokenText()).toEqual(["Opening in VS Code. Settled 2 threads, Ctrl+Z to undo"]); announce("Settled 1 thread, Ctrl+Z to undo", "undo"); announce("Opening in Zed"); cancelAnnouncement("undo"); - vi.runAllTimers(); + vi.advanceTimersByTime(250); expect(spokenText()).toEqual(["Opening in Zed"]); }); + + it("removes a spoken message from the page once it has had time to be read", () => { + using _timers = fakeTimers(); + + announce("Model: Claude Sonnet 5.5"); + vi.advanceTimersByTime(250); + expect(spokenText()).toEqual(["Model: Claude Sonnet 5.5"]); + + vi.advanceTimersByTime(60_000); + expect(spokenText()).toEqual([]); + }); + + it("removes a spoken message from the page when its source cancels it", () => { + using _timers = fakeTimers(); + + announce("Settled 1 thread, Ctrl+Z to undo", "thread-undo-notice"); + vi.advanceTimersByTime(250); + expect(spokenText()).toEqual(["Settled 1 thread, Ctrl+Z to undo"]); + + vi.advanceTimersByTime(4_750); + cancelAnnouncement("thread-undo-notice"); + expect(spokenText()).toEqual([]); + }); }); diff --git a/apps/web/src/lib/announce.ts b/apps/web/src/lib/announce.ts index e95bed66304fe..0b553c108464b 100644 --- a/apps/web/src/lib/announce.ts +++ b/apps/web/src/lib/announce.ts @@ -3,24 +3,38 @@ // messages wait until focus has stayed put for a moment, then are spoken together. const FOCUS_SETTLE_MS = 250; +// NVDA reads a polite region's leftover text again when focus returns from a +// dialog, so an announcement is cleared once it has had time to be spoken. +// Clearing doesn't cut off speech that already started. +export const CLEAR_SPOKEN_AFTER_MS = 7_000; + let regions: readonly [HTMLElement, HTMLElement] | null = null; let pending: { message: string; source: string | undefined }[] = []; +let spokenSources: (string | undefined)[] = []; let flushTimer: ReturnType | undefined; +let clearTimer: ReturnType | undefined; function scheduleFlush() { clearTimeout(flushTimer); flushTimer = setTimeout(flush, FOCUS_SETTLE_MS); } +function clearSpoken() { + clearTimeout(clearTimer); + spokenSources = []; + for (const region of regions ?? []) region.textContent = ""; +} + function flush() { if (!regions || pending.length === 0) return; - const [first, second] = regions; + clearSpoken(); // Alternating between two regions lets the same message be spoken twice in a row. - const target = first.textContent ? second : first; - first.textContent = ""; - second.textContent = ""; + const [target, other] = regions; + regions = [other, target]; target.textContent = pending.map((entry) => entry.message).join(". "); + spokenSources = pending.map((entry) => entry.source); pending = []; + clearTimer = setTimeout(clearSpoken, CLEAR_SPOKEN_AFTER_MS); } function createRegion() { @@ -50,7 +64,8 @@ export function announce(message: string, source?: string) { scheduleFlush(); } -/** Drops a message from `source` that hasn't been spoken yet, once it no longer applies. */ +/** Drops a message from `source`, waiting or already spoken, once it no longer applies. */ export function cancelAnnouncement(source: string) { pending = pending.filter((entry) => entry.source !== source); + if (spokenSources.includes(source)) clearSpoken(); } From db4221b74b33ba71e1c04570ff185d8078770ac7 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 12:54:02 -0800 Subject: [PATCH 047/253] fix(server): long sessions import when most transcript lines are bookkeeping (#31687) --- .../src/project/AgentSessionScanner.test.ts | 65 ++++++++++--------- .../server/src/project/AgentSessionScanner.ts | 6 +- 2 files changed, 34 insertions(+), 37 deletions(-) diff --git a/apps/server/src/project/AgentSessionScanner.test.ts b/apps/server/src/project/AgentSessionScanner.test.ts index 97a2e97e78fad..9c62ddb1a2b84 100644 --- a/apps/server/src/project/AgentSessionScanner.test.ts +++ b/apps/server/src/project/AgentSessionScanner.test.ts @@ -225,6 +225,24 @@ function makeRecordLimitTranscript(cwd: string, overflow: boolean): string { : records; } +// Pi retains every entry, keeping tool results as links outside the history budget. +function makePiToolResultTranscript(sessionId: string, cwd: string, recordCount: number): string { + const entry = (id: number, message: { readonly role: string; readonly content: string }) => + encodeTranscriptRecord({ + type: "message", + id: `e${id}`, + parentId: id === 0 ? null : `e${id - 1}`, + message, + }); + return [ + encodeTranscriptRecord({ type: "session", version: 3, id: sessionId, cwd }), + entry(0, { role: "user", content: "Imported prompt" }), + ...Array.from({ length: recordCount - 2 }, (_, index) => + entry(index + 1, { role: "toolResult", content: "Tool output" }), + ), + ].join("\n"); +} + const makeFileIdentityFixture = Effect.fn("AgentSessionScanner.test.makeFileIdentityFixture")( function* (originalInode = 10_000_000_000_000_001n, replacementInode = 10_000_000_000_000_000n) { const path = yield* Path.Path; @@ -2282,22 +2300,20 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { const claudeHomePath = yield* makeTempDir("t3code-record-budget-claude-"); const codexHomePath = yield* makeTempDir("t3code-record-budget-codex-"); const workspace = yield* makeTempDir("t3code-record-budget-workspace-"); - const exactLimit = makeRecordLimitTranscript(workspace, false); - // The newer transcript has 100,001 records; the older one has exactly 100,000. - for (const [sessionId, contents, mtimeMs] of [ - ["excessive", exactLimit.replace("record-limit-session", "excessive") + "{}\n", nowMs], - ["older", exactLimit, nowMs - 1_000], + // The newer transcript retains 100,001 records; the older one exactly 100,000. + for (const [sessionId, recordCount, mtimeMs] of [ + ["excessive", 100_001, nowMs], + ["older", 100_000, nowMs - 1_000], ] as const) { yield* writeTranscript({ filePath: path.join( - codexHomePath, + claudeHomePath, + "pi-agent", "sessions", - "2026", - "08", - "24", - `rollout-${sessionId}.jsonl`, + "--workspace--", + `${sessionId}.jsonl`, ), - contents, + contents: makePiToolResultTranscript(sessionId, workspace, recordCount), mtimeMs, }); } @@ -2307,9 +2323,7 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { workspaceRoot: workspace, }); expect(outcomes.map((outcome) => outcome._tag)).toEqual(["Skipped", "Importable"]); - expect(outcomes[1]).toMatchObject({ - thread: { providerSessionId: "record-limit-session" }, - }); + expect(outcomes[1]).toMatchObject({ thread: { providerSessionId: "older" } }); }), ); @@ -2326,26 +2340,13 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { for (const [index, recordCount] of recordCounts.entries()) { yield* writeTranscript({ filePath: path.join( - codexHomePath, + claudeHomePath, + "pi-agent", "sessions", - "2026", - "08", - "24", - `rollout-${index}.jsonl`, + "--workspace--", + `${index}.jsonl`, ), - contents: - [ - encodeTranscriptRecord({ - type: "session_meta", - payload: { id: `session-${index}`, cwd: workspace }, - }), - encodeTranscriptRecord({ - type: "event_msg", - payload: { type: "user_message", message: "Imported prompt" }, - }), - ].join("\n") + - "\n" + - "{}\n".repeat(recordCount - 2), + contents: makePiToolResultTranscript(`session-${index}`, workspace, recordCount), mtimeMs: nowMs - index * 1_000, }); } diff --git a/apps/server/src/project/AgentSessionScanner.ts b/apps/server/src/project/AgentSessionScanner.ts index c1f42586b2f33..1e300d1fe0410 100644 --- a/apps/server/src/project/AgentSessionScanner.ts +++ b/apps/server/src/project/AgentSessionScanner.ts @@ -1242,7 +1242,6 @@ export const make = Effect.gen(function* () { let claudeFields = emptyClaudeFields; let historyBytes = prefix?.historyBytes ?? 0; let recordBytes = 0; - let recordCount = 0; let bytesRead = 0; const reserve = (bytes: number) => { recordBytes += bytes; @@ -1258,9 +1257,6 @@ export const make = Effect.gen(function* () { const finishRecord = () => { reader.write(decoder.decode()); - recordCount += 1; - // Claude keeps a bounded window, so only the byte budgets limit its length. - if (recordCount > MAX_IMPORT_RECORDS && source !== "claudeAgent") return false; const value = reader.finish(); const decoded = decodeTranscriptValue(value); if (Option.isNone(decoded) && source === "pi") { @@ -1292,7 +1288,7 @@ export const make = Effect.gen(function* () { reader = createTranscriptJsonReader(reserve, selectTranscriptPath); decoder = new TextDecoder(); recordStarted = false; - return true; + return records.length <= MAX_IMPORT_RECORDS; }; while (bytesRead < readBytes) { From 6e650b384906ffa1dd28645a68d5295c06a18af9 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 12:57:42 -0800 Subject: [PATCH 048/253] fix(web): returning to the window keeps a text selection (#31710) --- .../components/ChatView.logic.browser.test.ts | 36 ++++++++++++++++--- apps/web/src/components/ChatView.logic.ts | 10 +++--- apps/web/src/components/ChatView.tsx | 2 +- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/apps/web/src/components/ChatView.logic.browser.test.ts b/apps/web/src/components/ChatView.logic.browser.test.ts index 880a640cc6e20..724c77538d315 100644 --- a/apps/web/src/components/ChatView.logic.browser.test.ts +++ b/apps/web/src/components/ChatView.logic.browser.test.ts @@ -21,11 +21,33 @@ it("keeps keyboard focus on a sidebar row on window return, but not a clicked ro await userEvent.click(clicked); expect(document.activeElement).toBe(clicked); - expect(shouldRefocusComposerOnWindowFocus(document.activeElement)).toBe(true); + expect(shouldRefocusComposerOnWindowFocus(document.activeElement, document.getSelection())).toBe( + true, + ); await userEvent.tab(); expect(document.activeElement).toBe(tabbed); - expect(shouldRefocusComposerOnWindowFocus(document.activeElement)).toBe(false); + expect(shouldRefocusComposerOnWindowFocus(document.activeElement, document.getSelection())).toBe( + false, + ); +}); + +it("leaves a text selection in the timeline alone on window return", async () => { + const timeline = document.createElement("div"); + timeline.tabIndex = -1; + const reply = document.createElement("p"); + reply.textContent = "Steel bridges are also easy to recycle at the end of their life."; + timeline.append(reply); + document.body.append(timeline); + using _timeline = { [Symbol.dispose]: () => timeline.remove() }; + + await userEvent.tripleClick(reply); + expect(document.activeElement).toBe(timeline); + expect(document.getSelection()?.toString()).toContain("Steel bridges"); + expect( + shouldRefocusComposerOnWindowFocus(document.activeElement, document.getSelection()), + "refocusing the composer would clear the selected reply text", + ).toBe(false); }); it("keeps focus in a region with its own keys on window return", async () => { @@ -38,7 +60,9 @@ it("keeps focus in a region with its own keys on window return", async () => { await userEvent.click(region); expect(document.activeElement).toBe(region); - expect(shouldRefocusComposerOnWindowFocus(document.activeElement)).toBe(false); + expect(shouldRefocusComposerOnWindowFocus(document.activeElement, document.getSelection())).toBe( + false, + ); }); it("refocuses the composer on window return from a clicked area after a bare key press", async () => { @@ -61,7 +85,7 @@ it("refocuses the composer on window return from a clicked area after a bare key // A window switch can deliver a lone modifier key without moving focus. await userEvent.keyboard("{Shift}"); expect( - shouldRefocusComposerOnWindowFocus(document.activeElement), + shouldRefocusComposerOnWindowFocus(document.activeElement, document.getSelection()), "a clicked area counts as reached from the keyboard after a bare key press", ).toBe(true); @@ -70,5 +94,7 @@ it("refocuses the composer on window return from a clicked area after a bare key await userEvent.keyboard("{F6}"); controller.move(1); expect(document.activeElement).toBe(conversation); - expect(shouldRefocusComposerOnWindowFocus(document.activeElement)).toBe(false); + expect(shouldRefocusComposerOnWindowFocus(document.activeElement, document.getSelection())).toBe( + false, + ); }); diff --git a/apps/web/src/components/ChatView.logic.ts b/apps/web/src/components/ChatView.logic.ts index e528246e7aea1..cc9e43b847dfd 100644 --- a/apps/web/src/components/ChatView.logic.ts +++ b/apps/web/src/components/ChatView.logic.ts @@ -1096,15 +1096,17 @@ export function hasServerAcknowledgedLocalDispatch(input: { ); } -// Window return puts the caret in the composer unless focus is deliberate: another text field, -// a terminal, an open dialog, menu, or popup, a region, timeline, or sidebar row reached from the -// keyboard, or a region with its own keys (aria-keyshortcuts). A plain focused button or a clicked -// row yields to the composer. +// Window return puts the caret in the composer unless focus or a selection is deliberate: another +// text field, a terminal, an open dialog, menu, or popup, a region, timeline, or sidebar row reached +// from the keyboard, a region with its own keys (aria-keyshortcuts), or selected text the composer +// would clear. A plain focused button or a clicked row yields to the composer. export function shouldRefocusComposerOnWindowFocus( activeElement: | (Pick & { isContentEditable?: boolean }) | null, + selection: Pick | null, ): boolean { + if (selection !== null && !selection.isCollapsed) return false; if (activeElement === null || activeElement.tagName === "BODY") return true; if ( activeElement.tagName === "INPUT" || diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index fbec56205975c..c0f8ff42f451e 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -2192,7 +2192,7 @@ export default function ChatView(props: ChatViewProps) { if (document.querySelector("[data-mcp-app-fullscreen]") !== null) return; if ( !isInSideChatPane(document.activeElement) && - shouldRefocusComposerOnWindowFocus(document.activeElement) + shouldRefocusComposerOnWindowFocus(document.activeElement, document.getSelection()) ) { focusComposerStable(); } From c69f4b780bdb3fd7b3d6a06f41b7017f77298c5e Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 13:07:30 -0800 Subject: [PATCH 049/253] fix(web): model picker lists a model reported twice once (#31590) --- .../components/chat/ModelPickerContent.tsx | 4 ++ .../chat/ProviderModelPicker.browser.test.tsx | 68 +++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/apps/web/src/components/chat/ModelPickerContent.tsx b/apps/web/src/components/chat/ModelPickerContent.tsx index e9a884fde6a17..87bacfd7ca2a2 100644 --- a/apps/web/src/components/chat/ModelPickerContent.tsx +++ b/apps/web/src/components/chat/ModelPickerContent.tsx @@ -548,13 +548,16 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: { const flatModels = (() => { const out: ModelPickerItem[] = []; + const seenKeys = new Set(); for (const [instanceId, models] of modelOptionsByInstance) { const entry = entryByInstanceId.get(instanceId); if (!entry) { continue; } for (const model of models) { + const key = modelPickerModelKey(instanceId, model.slug); if ( + seenKeys.has(key) || !shouldIncludeModelPickerOption({ entry, option: model, @@ -564,6 +567,7 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: { ) { continue; } + seenKeys.add(key); out.push({ slug: model.slug, name: model.name, diff --git a/apps/web/src/components/chat/ProviderModelPicker.browser.test.tsx b/apps/web/src/components/chat/ProviderModelPicker.browser.test.tsx index bf196f52542c2..06bb36a34c07f 100644 --- a/apps/web/src/components/chat/ProviderModelPicker.browser.test.tsx +++ b/apps/web/src/components/chat/ProviderModelPicker.browser.test.tsx @@ -4,6 +4,7 @@ import { ProviderInstanceId, type ModelSelection, } from "@t3tools/contracts"; +import { compileResolvedKeybindingsConfig } from "@t3tools/shared/keybindings"; import { useState } from "react"; import { expect, it, vi } from "vite-plus/test"; import { page, userEvent } from "vite-plus/test/browser"; @@ -176,6 +177,73 @@ it("keeps a docked picker open after a pick and hands focus to that model's effo await expect.element(search).not.toBeInTheDocument(); }); +it("lists a model reported twice by one provider once, with contiguous jump shortcuts", async () => { + const instanceId = ProviderInstanceId.make("claude"); + const entries = deriveProviderInstanceEntries([ + { + instanceId, + driver: ProviderDriverKind.make("claudeAgent"), + enabled: true, + installed: true, + version: null, + status: "ready", + auth: { status: "authenticated" }, + checkedAt: "2026-01-01T00:00:00.000Z", + models: [], + slashCommands: [], + skills: [], + }, + ]); + const options = new Map([ + [ + instanceId, + ["a", "b", "c", "c", "d", "d"].map((slug) => ({ + slug, + name: `Model ${slug.toUpperCase()}`, + })), + ], + ]); + const keybindings = compileResolvedKeybindingsConfig( + ([1, 2, 3, 4, 5, 6] as const).map((index) => ({ + key: `mod+${index}`, + command: `modelPicker.jump.${index}` as const, + when: "modelPickerOpen", + })), + ); + + await using screen = await render( + {}} + />, + ); + await screen.getByRole("button", { name: "Choose model" }).click(); + await expect.element(page.getByRole("option", { name: /Model A/ })).toBeVisible(); + await vi.waitFor(() => + expect( + page + .getByRole("option") + .elements() + .map((row) => [ + row.textContent?.match(/Model [A-Z]/)?.[0], + row.querySelector("kbd")?.textContent?.slice(-1), + ]), + ).toEqual([ + ["Model A", "1"], + ["Model B", "2"], + ["Model C", "3"], + ["Model D", "4"], + ]), + ); +}); + it("opens on the active provider when every favorite belongs to a disabled provider", async () => { const entries = deriveProviderInstanceEntries( ["Codex", "Claude"].map((name) => ({ From ca432d7ed645a8ebd032456b7bac95c0a0590ced Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 13:11:10 -0800 Subject: [PATCH 050/253] fix(server): log the connections the private-network gate closes (#31647) --- apps/server/src/server.ts | 9 ++--- apps/server/src/startupAccess.test.ts | 55 +++++++++++++++++++++++++++ apps/server/src/startupAccess.ts | 41 ++++++++++++++++++++ 3 files changed, 99 insertions(+), 6 deletions(-) diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts index 47c4a4c9a20ec..01b659b0944ad 100644 --- a/apps/server/src/server.ts +++ b/apps/server/src/server.ts @@ -219,7 +219,7 @@ import * as RunFinalizationService from "./orchestration-v2/RunFinalizationServi import * as ProjectionMaintenance from "./orchestration-v2/ProjectionMaintenance.ts"; import * as ProjectionStoreV2 from "./orchestration-v2/ProjectionStore.ts"; import { makePersistedServerRuntimeState } from "./serverRuntimeState.ts"; -import { acceptsPrivateNetworkConnection, loopbackUrlHostForAddress } from "./startupAccess.ts"; +import { closeNonPrivateNetworkConnections, loopbackUrlHostForAddress } from "./startupAccess.ts"; import * as OrchestrationHttp from "./orchestration-v2/http.ts"; import * as ProjectHttp from "./project/http.ts"; import * as NetService from "@t3tools/shared/Net"; @@ -364,17 +364,14 @@ const layerHttpServer = Layer.unwrap( privateNetworkOnly: config.privateNetworkOnly, }); yield* ManagedLimits.closeTailscaleServeTo(policy, { host: bindHost, port: config.port }); + const runSync = Effect.runSyncWith(yield* Effect.context()); return NodeHttpServer.layer( () => { const server = rejectRequestsUntilServing( keepIdleConnections(guardHttpResponseWriteErrors(NodeHttp.createServer())), ); if (config.privateNetworkOnly) { - server.on("connection", (socket) => { - if (!acceptsPrivateNetworkConnection(socket)) { - socket.destroy(); - } - }); + runSync(closeNonPrivateNetworkConnections(server)); } return server; }, diff --git a/apps/server/src/startupAccess.test.ts b/apps/server/src/startupAccess.test.ts index 159778b3c7e03..7290d6063db9b 100644 --- a/apps/server/src/startupAccess.test.ts +++ b/apps/server/src/startupAccess.test.ts @@ -1,10 +1,17 @@ +// @effect-diagnostics nodeBuiltinImport:off +import * as NodeHttp from "node:http"; +import * as NodeNet from "node:net"; import type * as NodeOS from "node:os"; import { describe, expect, it } from "@effect/vitest"; +import * as Effect from "effect/Effect"; +import * as Logger from "effect/Logger"; import * as NetAddress from "effect/net/NetAddress"; +import * as TestClock from "effect/testing/TestClock"; import { acceptsPrivateNetworkConnection, + closeNonPrivateNetworkConnections, loopbackUrlHostForAddress, resolveHeadlessConnectionString, } from "./startupAccess.ts"; @@ -104,3 +111,51 @@ describe("acceptsPrivateNetworkConnection", () => { expect(accepts(undefined, "192.168.1.31")).toBe(false); }); }); + +describe("closeNonPrivateNetworkConnections", () => { + it.effect("logs the local and remote address of a connection it closes", () => + Effect.gen(function* () { + const warnings: Array> = []; + const server = NodeHttp.createServer(); + yield* closeNonPrivateNetworkConnections(server).pipe( + Effect.provide( + Logger.layer([Logger.make((entry) => warnings.push(entry))], { + mergeWithExisting: false, + }), + ), + ); + + // An unconnected socket: Node reports neither address. + const socket = new NodeNet.Socket(); + server.emit("connection", socket); + + expect(socket.destroyed).toBe(true); + expect(warnings.map((entry) => entry.logLevel)).toEqual(["Warn"]); + const line = Logger.formatLogFmt.log(warnings[0]!); + expect(line).toMatch(/localAddress\W+undefined/); + expect(line).toMatch(/remoteAddress\W+undefined/); + }), + ); + + it.effect("logs a flood of closed connections at most once a minute, with a count", () => + Effect.gen(function* () { + const warnings: Array> = []; + const server = NodeHttp.createServer(); + yield* closeNonPrivateNetworkConnections(server).pipe( + Effect.provide( + Logger.layer([Logger.make((entry) => warnings.push(entry))], { + mergeWithExisting: false, + }), + ), + ); + const sockets = Array.from({ length: 1000 }, () => new NodeNet.Socket()); + for (const socket of sockets) server.emit("connection", socket); + yield* TestClock.adjust("1 minute"); + server.emit("connection", new NodeNet.Socket()); + + expect(sockets.every((socket) => socket.destroyed)).toBe(true); + expect(warnings).toHaveLength(2); + expect(Logger.formatLogFmt.log(warnings[1]!)).toMatch(/closedSinceLastLog\W+999/); + }), + ); +}); diff --git a/apps/server/src/startupAccess.ts b/apps/server/src/startupAccess.ts index 1720300bd7beb..ee372a1ab1514 100644 --- a/apps/server/src/startupAccess.ts +++ b/apps/server/src/startupAccess.ts @@ -1,3 +1,5 @@ +// @effect-diagnostics nodeBuiltinImport:off - The private-network gate guards the node:http server. +import type * as NodeHttp from "node:http"; import * as NodeNet from "node:net"; import * as NodeOS from "node:os"; @@ -10,6 +12,7 @@ import { import * as HostProcess from "@t3tools/shared/HostProcess"; import { QrCode } from "@t3tools/shared/qrCode"; import { buildPairingAppLink } from "@t3tools/shared/remote"; +import * as Clock from "effect/Clock"; import * as Effect from "effect/Effect"; import { HttpServer } from "effect/http"; import * as NetAddress from "effect/net/NetAddress"; @@ -81,6 +84,44 @@ export const acceptsPrivateNetworkConnection = (socket: { (address) => address !== undefined && isPrivateNetworkAddress(address), ); +const CLOSED_CONNECTION_LOG_INTERVAL_NANOS = 60_000_000_000n; + +/** + * Closing is silent to the client, so closed connections are logged with both + * addresses: the first at once, then at most one line a minute with a count, so + * a connect flood from outside cannot grow the service log without bound. + */ +// Untraced: the captured context would otherwise tie every later warning to a span that ended at startup. +export const closeNonPrivateNetworkConnections = Effect.fnUntraced(function* ( + server: NodeHttp.Server, +) { + const runSync = Effect.runSyncWith(yield* Effect.context()); + let nextLogAt: bigint | undefined; + let closedSinceLastLog = 0; + server.on("connection", (socket) => { + if (acceptsPrivateNetworkConnection(socket)) return; + // Read before destroy: Node forgets localAddress once the handle closes. + const addresses = { localAddress: socket.localAddress, remoteAddress: socket.remoteAddress }; + socket.destroy(); + runSync( + Effect.gen(function* () { + // Monotonic: a wall clock set backwards must not silence the warnings. + const now = yield* Clock.monotonicTimeNanos; + if (nextLogAt !== undefined && now < nextLogAt) { + closedSinceLastLog += 1; + return; + } + nextLogAt = now + CLOSED_CONNECTION_LOG_INTERVAL_NANOS; + yield* Effect.logWarning("closed a connection outside the private network", { + ...addresses, + ...(closedSinceLastLog > 0 ? { closedSinceLastLog } : {}), + }); + closedSinceLastLog = 0; + }), + ); + }); +}); + export const loopbackUrlHostForAddress = (address: NetAddress.InetAddress): string => NetAddress.isUnspecified(address.address) ? "127.0.0.1" From 991c62d0d7cf2b29beca05e2fe56eb3227af4fae Mon Sep 17 00:00:00 2001 From: Yash Singh Date: Mon, 16 Nov 2026 15:14:26 -0600 Subject: [PATCH 051/253] fix(web): project picker rows no longer jump when the palette opens (#37764) Co-authored-by: Cursor Co-authored-by: Julius Marminge Co-authored-by: Claude Opus 5.5 --- .../hooks/useLegendListRef.browser.test.tsx | 26 ++++- patches/@legendapp__list@3.7.2.patch | 106 ++++++++++++++++++ pnpm-lock.yaml | 8 +- 3 files changed, 135 insertions(+), 5 deletions(-) diff --git a/apps/web/src/hooks/useLegendListRef.browser.test.tsx b/apps/web/src/hooks/useLegendListRef.browser.test.tsx index 22e258f30a061..d74ac578b8b07 100644 --- a/apps/web/src/hooks/useLegendListRef.browser.test.tsx +++ b/apps/web/src/hooks/useLegendListRef.browser.test.tsx @@ -1,6 +1,6 @@ import { LegendList, type LegendListRef } from "@legendapp/list/react"; import { createRef, useState, type RefObject } from "react"; -import { expect, it } from "vite-plus/test"; +import { expect, it, vi } from "vite-plus/test"; import { act, render } from "../../test/render"; import { useLegendListRef } from "./useLegendListRef"; @@ -30,6 +30,30 @@ function ListProbe({ ); } +it("keeps rows at their layout height when they re-render while an ancestor is scaled", async () => { + const items = Array.from({ length: 6 }, (_, index) => `Row ${index}`); + // Dialogs and popovers scale in from 0.98; a re-render mid-animation must not record scaled sizes. + const list = (highlighted: string) => ( +
+ item} + renderItem={({ item }) =>
{item}
} + style={{ height: 300, width: 300 }} + /> +
+ ); + await using screen = await render(list("Row 0")); + await expect.element(screen.getByText("Row 5")).toBeVisible(); + await screen.rerender(list("Row 1")); + await vi.waitFor(() => { + const first = screen.getByText("Row 0").element().getBoundingClientRect(); + const last = screen.getByText("Row 5").element().getBoundingClientRect(); + expect((last.top - first.top) / 0.98).toBeCloseTo(200, 0); + }); +}); + it("refreshes list edges after content shrinks without scrolling and after the list reopens", async () => { const listRef = createRef(); const items = Array.from({ length: 20 }, (_, index) => `Row ${index}`); diff --git a/patches/@legendapp__list@3.7.2.patch b/patches/@legendapp__list@3.7.2.patch index beafc1c4a6fcf..a9a1d31a3982a 100644 --- a/patches/@legendapp__list@3.7.2.patch +++ b/patches/@legendapp__list@3.7.2.patch @@ -102,3 +102,109 @@ index 9ebfddc183a9b202d35c990a387c9b628feeb185..489050190144997ac72de8f4f7c62194 const ownsMeasuredItem = measurement.containerId === void 0 || isContainerItemCurrent(ctx, measurement.containerId, measurement.itemKey); if (ownsMeasuredItem) { const index = state.indexByKey.get(measurement.itemKey); +diff --git a/react.js b/react.js +index 515545471e08426fc90856256db9491db0ca4b92..b741735df1fbc0c954ed48f15590abce727d5336 100644 +--- a/react.js ++++ b/react.js +@@ -5635,6 +5635,19 @@ function updateOneItemSize(ctx, itemKey, sizeObj, resolvedMeasurementItem, notif + } + + // src/core/measureContainersInLayoutEffect.ts ++// T3 patch: measure rows by their layout box, as ResizeObserver does. getBoundingClientRect ++// includes ancestor transforms, so rows that mount while a dialog scales in measure short and ++// stay packed too tightly until they re-render. ++function measureContainerLayoutSize(element) { ++ const rect = element.getBoundingClientRect(); ++ if (Math.abs(rect.width - element.offsetWidth) < 1 && Math.abs(rect.height - element.offsetHeight) < 1) { ++ return { height: rect.height, width: rect.width }; ++ } ++ const style = getComputedStyle(element); ++ const height = Number.parseFloat(style.height); ++ const width = Number.parseFloat(style.width); ++ return Number.isFinite(height) && Number.isFinite(width) ? { height, width } : { height: rect.height, width: rect.width }; ++} + function measureContainersInLayoutEffect(ctx, targetContainerIds = null) { + var _a3, _b, _c; + const dataChangeEpoch = ctx.state.dataChangeEpoch; +@@ -5646,13 +5659,13 @@ function measureContainersInLayoutEffect(ctx, targetContainerIds = null) { + const element = viewRef == null ? void 0 : viewRef.current; + if (itemKey !== void 0 && element && isContainerItemCurrent(ctx, containerId, itemKey)) { + const generation = (_b = ctx.state.containerItemGenerations[containerId]) != null ? _b : 0; +- const rect = element.getBoundingClientRect(); +- setContainerLayoutBaseline(element, rect); ++ const size = measureContainerLayoutSize(element); ++ setContainerLayoutBaseline(element, size); + measurements.push({ + containerId, + generation, + itemKey, +- size: { height: rect.height, width: rect.width } ++ size + }); + } + } +@@ -6296,9 +6309,8 @@ function processContainerLayout({ containerId, ctx, rectangle, ref, state }) { + var _a4; + if (state.itemKey === currentItemKey) { + const element = ref.current; +- const rect = (_a4 = element == null ? void 0 : element.getBoundingClientRect) == null ? void 0 : _a4.call(element); +- if (rect) { +- layout = { height: rect.height, width: rect.width }; ++ if (element) { ++ layout = measureContainerLayoutSize(element); + } + applyLayout(); + } +diff --git a/react.mjs b/react.mjs +index ce08abe7ee62e8940f9c6ff169829244bd6bb736..fce79caef8387e517cbab8051b213cb57b1df590 100644 +--- a/react.mjs ++++ b/react.mjs +@@ -5614,6 +5614,19 @@ function updateOneItemSize(ctx, itemKey, sizeObj, resolvedMeasurementItem, notif + } + + // src/core/measureContainersInLayoutEffect.ts ++// T3 patch: measure rows by their layout box, as ResizeObserver does. getBoundingClientRect ++// includes ancestor transforms, so rows that mount while a dialog scales in measure short and ++// stay packed too tightly until they re-render. ++function measureContainerLayoutSize(element) { ++ const rect = element.getBoundingClientRect(); ++ if (Math.abs(rect.width - element.offsetWidth) < 1 && Math.abs(rect.height - element.offsetHeight) < 1) { ++ return { height: rect.height, width: rect.width }; ++ } ++ const style = getComputedStyle(element); ++ const height = Number.parseFloat(style.height); ++ const width = Number.parseFloat(style.width); ++ return Number.isFinite(height) && Number.isFinite(width) ? { height, width } : { height: rect.height, width: rect.width }; ++} + function measureContainersInLayoutEffect(ctx, targetContainerIds = null) { + var _a3, _b, _c; + const dataChangeEpoch = ctx.state.dataChangeEpoch; +@@ -5625,13 +5638,13 @@ function measureContainersInLayoutEffect(ctx, targetContainerIds = null) { + const element = viewRef == null ? void 0 : viewRef.current; + if (itemKey !== void 0 && element && isContainerItemCurrent(ctx, containerId, itemKey)) { + const generation = (_b = ctx.state.containerItemGenerations[containerId]) != null ? _b : 0; +- const rect = element.getBoundingClientRect(); +- setContainerLayoutBaseline(element, rect); ++ const size = measureContainerLayoutSize(element); ++ setContainerLayoutBaseline(element, size); + measurements.push({ + containerId, + generation, + itemKey, +- size: { height: rect.height, width: rect.width } ++ size + }); + } + } +@@ -6275,9 +6288,8 @@ function processContainerLayout({ containerId, ctx, rectangle, ref, state }) { + var _a4; + if (state.itemKey === currentItemKey) { + const element = ref.current; +- const rect = (_a4 = element == null ? void 0 : element.getBoundingClientRect) == null ? void 0 : _a4.call(element); +- if (rect) { +- layout = { height: rect.height, width: rect.width }; ++ if (element) { ++ layout = measureContainerLayoutSize(element); + } + applyLayout(); + } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 07806d3cac658..5256d97fe1498 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -370,7 +370,7 @@ patchedDependencies: '@effect/vitest@4.1.2': 299510b71c36537a832a67e9d160a5f112a5f326a29e95c41cb45fc8234e47c7 '@expo/ui@58.0.27': 9f2547678939f4ec78fe45163b6455aad63d3c80353d7e0cc783f6a61a7ccc2f '@ff-labs/fff-node@0.11.0': 93f62a76399b435a76588b2bef237db0a1e85880c1ac0d0759c416e9cc579252 - '@legendapp/list@3.7.2': 8f361e44c0756d883f8b8b5964f918f37b514a436c5058b773a1dc57eee5bfcc + '@legendapp/list@3.7.2': fc8470158c72676c35f43a0a84f90e53105c3eedd930e242534a37ff4159a3ef '@muse-code/sdk@1.4.6': 7b495b607fc5d76b444c7c4d58821e839726a30e6f3c53564f0d59f2d5b42101 '@pierre/diffs@1.7.0-beta.4': 241563d810c29250e0e92f3eda74214393726d46667fedbf1f91a5f5bea07d1e '@react-native-ai/apple@0.12.0': 2d09870c2848d185cb05b53ed823a46e12dba519324d8dd8e584e28731990f9d @@ -567,7 +567,7 @@ importers: version: 4.2.13 '@legendapp/list': specifier: 3.7.2 - version: 3.7.2(patch_hash=8f361e44c0756d883f8b8b5964f918f37b514a436c5058b773a1dc57eee5bfcc)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0) + version: 3.7.2(patch_hash=fc8470158c72676c35f43a0a84f90e53105c3eedd930e242534a37ff4159a3ef)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0) '@material/material-color-utilities': specifier: 0.3.0 version: 0.3.0 @@ -1088,7 +1088,7 @@ importers: version: 5.3.0 '@legendapp/list': specifier: 3.7.2 - version: 3.7.2(patch_hash=8f361e44c0756d883f8b8b5964f918f37b514a436c5058b773a1dc57eee5bfcc)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0) + version: 3.7.2(patch_hash=fc8470158c72676c35f43a0a84f90e53105c3eedd930e242534a37ff4159a3ef)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0) '@noble/hashes': specifier: 'catalog:' version: 1.8.0 @@ -15564,7 +15564,7 @@ snapshots: '@koromix/koffi-win32-x64@3.3.6': optional: true - '@legendapp/list@3.7.2(patch_hash=8f361e44c0756d883f8b8b5964f918f37b514a436c5058b773a1dc57eee5bfcc)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0)': + '@legendapp/list@3.7.2(patch_hash=fc8470158c72676c35f43a0a84f90e53105c3eedd930e242534a37ff4159a3ef)(react-dom@19.3.0(react@19.3.0))(react-native@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.1(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0)': dependencies: react: 19.3.0 use-sync-external-store: 1.7.0(react@19.3.0) From fb89ce1680bcaa16d5c564369543ffefda755529 Mon Sep 17 00:00:00 2001 From: Yash Singh Date: Mon, 16 Nov 2026 15:14:46 -0600 Subject: [PATCH 052/253] feat(desktop): drag file pills into Finder on macOS (#37768) Co-authored-by: Cursor --- apps/desktop/src/ipc/DesktopIpcHandlers.ts | 2 + apps/desktop/src/ipc/channels.ts | 2 + apps/desktop/src/ipc/methods/fileDrag.test.ts | 51 +++++++++++++++++++ apps/desktop/src/ipc/methods/fileDrag.ts | 36 +++++++++++++ apps/desktop/src/preload.ts | 1 + .../ChatMarkdown.permissions.browser.test.tsx | 1 + apps/web/src/components/ChatMarkdown.tsx | 4 ++ .../components/ComposerPromptEditorTiptap.tsx | 6 +++ .../src/components/chat/MarkdownFileLink.tsx | 7 +++ .../chat/UserMessageContextChips.tsx | 6 +++ apps/web/src/useNativeFileDrag.ts | 47 +++++++++++++++++ packages/contracts/src/ipc.ts | 1 + 12 files changed, 164 insertions(+) create mode 100644 apps/desktop/src/ipc/methods/fileDrag.test.ts create mode 100644 apps/desktop/src/ipc/methods/fileDrag.ts create mode 100644 apps/web/src/useNativeFileDrag.ts diff --git a/apps/desktop/src/ipc/DesktopIpcHandlers.ts b/apps/desktop/src/ipc/DesktopIpcHandlers.ts index 60b72deb293a5..fe65edd0ee1e9 100644 --- a/apps/desktop/src/ipc/DesktopIpcHandlers.ts +++ b/apps/desktop/src/ipc/DesktopIpcHandlers.ts @@ -1,3 +1,4 @@ +import { startFileDrag } from "./methods/fileDrag.ts"; import * as Effect from "effect/Effect"; import { receiveProviderAuthCallback, cancelProviderAuthCallback } from "./methods/providerAuth.ts"; @@ -110,6 +111,7 @@ export const installDesktopIpcHandlers = Effect.fn("desktop.ipc.installHandlers" yield* ipc.handle(getHostEnvironmentId); yield* ipc.handle(getLocalEnvironmentBearerToken); + yield* ipc.handle(startFileDrag); yield* ipc.handle(restartAudioService); yield* ipc.handle(getClientSettings); yield* ipc.handle(setClientSettings); diff --git a/apps/desktop/src/ipc/channels.ts b/apps/desktop/src/ipc/channels.ts index 9e5c7296a3a03..d8652bd199713 100644 --- a/apps/desktop/src/ipc/channels.ts +++ b/apps/desktop/src/ipc/channels.ts @@ -142,3 +142,5 @@ export const TAKE_LEGACY_LOCAL_STORAGE_CHANNEL = "desktop:take-legacy-local-stor export const COMPLETE_LEGACY_LOCAL_STORAGE_CHANNEL = "desktop:complete-legacy-local-storage"; export const RESTART_AUDIO_SERVICE_CHANNEL = "desktop:restart-audio-service"; + +export const START_FILE_DRAG_CHANNEL = "desktop:start-file-drag"; diff --git a/apps/desktop/src/ipc/methods/fileDrag.test.ts b/apps/desktop/src/ipc/methods/fileDrag.test.ts new file mode 100644 index 0000000000000..04b75c9be98a1 --- /dev/null +++ b/apps/desktop/src/ipc/methods/fileDrag.test.ts @@ -0,0 +1,51 @@ +import { assert, it } from "@effect/vitest"; +import * as NodeServices from "@effect/platform-node/NodeServices"; +import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; +import * as Path from "effect/Path"; +import { vi } from "vite-plus/test"; + +const { startDrag, getFileIcon } = vi.hoisted(() => ({ + startDrag: vi.fn(), + getFileIcon: vi.fn(async () => "file-icon"), +})); +vi.mock("electron", () => ({ app: { getFileIcon } })); + +import * as DesktopEnvironment from "../../app/DesktopEnvironment.ts"; +import * as ElectronWindow from "../../electron/ElectronWindow.ts"; +import { startFileDrag } from "./fileDrag.ts"; + +it.effect("only drags existing absolute paths from the Mac host environment and app window", () => + Effect.gen(function* () { + startDrag.mockClear(); + getFileIcon.mockClear(); + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const stateDir = yield* fs.makeTempDirectoryScoped(); + const filePath = path.join(stateDir, "file with spaces.txt"); + yield* fs.writeFileString(filePath, "file"); + yield* fs.writeFileString(path.join(stateDir, "environment-id"), "host\n"); + const run = (environmentId: string, target: string, platform = "darwin", sender = 1) => + startFileDrag.handler({ environmentId, path: target }, { sender: { id: sender } }).pipe( + Effect.provideService(DesktopEnvironment.DesktopEnvironment, { + path, + stateDir, + platform, + } as DesktopEnvironment.DesktopEnvironment["Service"]), + Effect.provideService(ElectronWindow.ElectronWindow, { + main: Effect.succeedSome({ webContents: { id: 1, startDrag } }), + } as unknown as ElectronWindow.ElectronWindow["Service"]), + ); + assert.isFalse(yield* run("remote", filePath)); + assert.isFalse(yield* run("host", "relative.txt")); + assert.isFalse(yield* run("host", `${filePath}:12`)); + assert.isFalse(yield* run("host", filePath, "linux")); + assert.isFalse(yield* run("host", filePath, "darwin", 2)); + assert.strictEqual(startDrag.mock.calls.length, 0); + assert.isTrue(yield* run("host", filePath)); + assert.deepEqual(startDrag.mock.calls, [[{ file: filePath, icon: "file-icon" }]]); + yield* fs.remove(path.join(stateDir, "environment-id")); + assert.isFalse(yield* run("host", filePath)); + assert.strictEqual(startDrag.mock.calls.length, 1); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), +); diff --git a/apps/desktop/src/ipc/methods/fileDrag.ts b/apps/desktop/src/ipc/methods/fileDrag.ts new file mode 100644 index 0000000000000..9701dfafcda32 --- /dev/null +++ b/apps/desktop/src/ipc/methods/fileDrag.ts @@ -0,0 +1,36 @@ +import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; +import * as Option from "effect/Option"; +import * as Schema from "effect/Schema"; +import * as Electron from "electron"; + +import * as DesktopEnvironment from "../../app/DesktopEnvironment.ts"; +import * as ElectronWindow from "../../electron/ElectronWindow.ts"; +import * as IpcChannels from "../channels.ts"; +import { makeIpcMethod } from "../DesktopIpc.ts"; +import { getHostEnvironmentId } from "./localEnvironment.ts"; + +export const startFileDrag = makeIpcMethod({ + channel: IpcChannels.START_FILE_DRAG_CHANNEL, + payload: Schema.Struct({ environmentId: Schema.String, path: Schema.String }), + result: Schema.Boolean, + handler: Effect.fn("desktop.ipc.fileDrag.start")(function* (input, event) { + const environment = yield* DesktopEnvironment.DesktopEnvironment; + if (environment.platform !== "darwin" || !environment.path.isAbsolute(input.path)) return false; + const hostEnvironmentId = yield* getHostEnvironmentId.handler(undefined); + if (hostEnvironmentId === null || input.environmentId !== hostEnvironmentId) return false; + const window = yield* (yield* ElectronWindow.ElectronWindow).main; + if (Option.isNone(window) || event?.sender.id !== window.value.webContents.id) return false; + const fs = yield* FileSystem.FileSystem; + if (!(yield* fs.exists(input.path))) return false; + return yield* Effect.promise(async () => { + try { + const icon = await Electron.app.getFileIcon(input.path, { size: "normal" }); + window.value.webContents.startDrag({ file: input.path, icon }); + return true; + } catch { + return false; + } + }); + }), +}); diff --git a/apps/desktop/src/preload.ts b/apps/desktop/src/preload.ts index ba9c17aae2695..c215c4b2e639f 100644 --- a/apps/desktop/src/preload.ts +++ b/apps/desktop/src/preload.ts @@ -255,6 +255,7 @@ contextBridge.exposeInMainWorld("desktopBridge", { ipcRenderer.invoke(IpcChannels.RECEIVE_PROVIDER_AUTH_CALLBACK_CHANNEL, url), cancelProviderAuthCallback: (url: string) => ipcRenderer.invoke(IpcChannels.CANCEL_PROVIDER_AUTH_CALLBACK_CHANNEL, url), + startFileDrag: (input) => ipcRenderer.invoke(IpcChannels.START_FILE_DRAG_CHANNEL, input), openExternal: (url: string) => ipcRenderer.invoke(IpcChannels.OPEN_EXTERNAL_CHANNEL, url), readMedia: (url: string) => ipcRenderer.invoke(IpcChannels.READ_MEDIA_CHANNEL, url), checkSystemPermission: (pane: string) => diff --git a/apps/web/src/components/ChatMarkdown.permissions.browser.test.tsx b/apps/web/src/components/ChatMarkdown.permissions.browser.test.tsx index 324c2c839ccd9..a072ecc107e26 100644 --- a/apps/web/src/components/ChatMarkdown.permissions.browser.test.tsx +++ b/apps/web/src/components/ChatMarkdown.permissions.browser.test.tsx @@ -308,6 +308,7 @@ it("preserves keyboard activation of a file-options button", async () => { copyMarkdown="[Readme](readme.md)" theme="dark" onOpenInPanel={vi.fn()} + resolveDragPath={async () => "/work/readme.md"} resolveCopyPaths={async () => ({ relativePath: "readme.md", fullPath: "/work/readme.md" })} openInEditorMenuLabel="Open in editor" />, diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx index b48485fc61212..455f18304ab20 100644 --- a/apps/web/src/components/ChatMarkdown.tsx +++ b/apps/web/src/components/ChatMarkdown.tsx @@ -1,3 +1,4 @@ +import { resolvePathLinkTarget } from "@t3tools/shared/fileLinks"; import { clientHostEnvironmentAtom } from "../state/clientHostEnvironment"; import { buildFileLinkParentSuffixByPath, fileLinkLabel } from "@t3tools/shared/fileLinks"; import { @@ -941,6 +942,9 @@ function useChatMarkdownState({ ) : undefined } + resolveDragPath={async () => + resolvePathLinkTarget((await resolveHostPath()).absolutePath, cwd ?? "") + } resolveCopyPaths={async () => markdownFileCopyPaths(fileLinkMeta, await resolveHostPath(), cwd) } diff --git a/apps/web/src/components/ComposerPromptEditorTiptap.tsx b/apps/web/src/components/ComposerPromptEditorTiptap.tsx index d89f57068e9cf..20ea35168d213 100644 --- a/apps/web/src/components/ComposerPromptEditorTiptap.tsx +++ b/apps/web/src/components/ComposerPromptEditorTiptap.tsx @@ -1,3 +1,5 @@ +import { useNativeFileDrag } from "../useNativeFileDrag"; +import { resolvePathLinkTarget } from "@t3tools/shared/fileLinks"; import { useAtomValue } from "@effect/atom-react"; import { documentChipKindForFileName } from "~/pierre-icons"; import { isProviderSkillUserInvocable } from "@t3tools/client-runtime/providerSkills"; @@ -281,8 +283,12 @@ const ComposerMentionExtension = Node.create({ function ComposerMentionNodeView({ node }: NodeViewProps) { const actions = use(ComposerContextActionsContext); const path = (node.attrs.path as string) ?? ""; + const nativeDrag = useNativeFileDrag(actions.environmentId, () => + resolvePathLinkTarget(path, actions.cwd ?? ""), + ); const chip = ( } diff --git a/apps/web/src/components/chat/MarkdownFileLink.tsx b/apps/web/src/components/chat/MarkdownFileLink.tsx index 5d0238fb1f3af..53e575bd5bc99 100644 --- a/apps/web/src/components/chat/MarkdownFileLink.tsx +++ b/apps/web/src/components/chat/MarkdownFileLink.tsx @@ -1,3 +1,4 @@ +import { useNativeFileDrag } from "../../useNativeFileDrag"; import { useAssetUrlState } from "../../assets/assetUrls"; import { formatFilePathForClipboard } from "@t3tools/client-runtime/media-actions"; import { @@ -42,6 +43,7 @@ interface MarkdownFileLinkProps { threadRef?: ScopedThreadRef | undefined; onOpen?: (() => Promise>) | undefined; onOpenInPanel: (panelPath: string, line: number | undefined) => void; + resolveDragPath: () => Promise; resolveCopyPaths: () => Promise>; openInEditorMenuLabel: string; onOpenInBrowser?: (() => Promise>) | undefined; @@ -73,7 +75,9 @@ export const MarkdownFileLink = memo(function MarkdownFileLink({ onReveal, revealLabel, resolveCopyPaths, + resolveDragPath, }: MarkdownFileLinkProps) { + const nativeDrag = useNativeFileDrag(environmentId, resolveDragPath); const handleOpenInEditor = () => { if (!onOpen) { return; @@ -328,6 +332,7 @@ export const MarkdownFileLink = memo(function MarkdownFileLink({ render={ hasPrimaryAction ? ( } @@ -362,6 +367,7 @@ export const MarkdownFileLink = memo(function MarkdownFileLink({ ) : ( } @@ -418,6 +424,7 @@ function areMarkdownFileLinkPropsEqual( previous.onOpenMedia === next.onOpenMedia && previous.onReveal === next.onReveal && previous.resolveCopyPaths === next.resolveCopyPaths && + previous.resolveDragPath === next.resolveDragPath && previous.revealLabel === next.revealLabel ); } diff --git a/apps/web/src/components/chat/UserMessageContextChips.tsx b/apps/web/src/components/chat/UserMessageContextChips.tsx index 39801a908b952..e345472b1f972 100644 --- a/apps/web/src/components/chat/UserMessageContextChips.tsx +++ b/apps/web/src/components/chat/UserMessageContextChips.tsx @@ -1,3 +1,5 @@ +import { useNativeFileDrag } from "../../useNativeFileDrag"; +import { resolvePathLinkTarget } from "@t3tools/shared/fileLinks"; import { documentChipKindForFileName } from "~/pierre-icons"; import { isSvgAttachment } from "@t3tools/shared/image"; import { useRenderedAppearance } from "../../hooks/useTheme"; @@ -68,11 +70,15 @@ function UserMessageMentionChip(props: { copyMarkdown: string; }) { const ctx = use(TimelineRowCtx); + const nativeDrag = useNativeFileDrag(ctx.activeThreadEnvironmentId, () => + resolvePathLinkTarget(props.record.path, ctx.workspaceRoot ?? ""), + ); return ( } aria-label={`Preview ${props.record.path}`} diff --git a/apps/web/src/useNativeFileDrag.ts b/apps/web/src/useNativeFileDrag.ts new file mode 100644 index 0000000000000..3195b5ef677b4 --- /dev/null +++ b/apps/web/src/useNativeFileDrag.ts @@ -0,0 +1,47 @@ +import { splitFilePathPosition } from "@t3tools/shared/fileLinks"; +import { isMacPlatform } from "./lib/utils"; +import { useAtomValue } from "@effect/atom-react"; +import { AuthOrchestrationOperateScope, type EnvironmentId } from "@t3tools/contracts"; +import type { DragEvent } from "react"; + +import { hostEnvironmentIdAtom } from "./state/primaryEnvironment"; +import { useEnvironmentScope } from "./state/session"; +import { toastManager } from "./components/ui/toast"; + +export function useNativeFileDrag( + environmentId: EnvironmentId | null | undefined, + resolvePath: () => string | Promise, +) { + const hostEnvironmentId = useAtomValue(hostEnvironmentIdAtom); + const canOperate = useEnvironmentScope(environmentId ?? null, AuthOrchestrationOperateScope); + const enabled = + typeof window.desktopBridge?.startFileDrag === "function" && + isMacPlatform(navigator.platform) && + environmentId != null && + environmentId === hostEnvironmentId && + canOperate; + return { + draggable: enabled || undefined, + onDragStart: (event: DragEvent) => { + if (!enabled) return; + // Electron replaces the HTML drag with a native file drag. + event.preventDefault(); + event.stopPropagation(); + let released = false; + window.addEventListener("pointerup", () => (released = true), { once: true }); + void (async () => { + const { path } = splitFilePathPosition(await resolvePath()); + if (released) return; + const started = await window.desktopBridge?.startFileDrag({ environmentId, path }); + if (!started) throw new Error("The file is unavailable on this Mac."); + })().catch((error: unknown) => { + if (released) return; + toastManager.add({ + type: "error", + title: "Unable to drag file", + description: error instanceof Error ? error.message : "An error occurred.", + }); + }); + }, + }; +} diff --git a/packages/contracts/src/ipc.ts b/packages/contracts/src/ipc.ts index 25339cb3216bc..96942123f56d3 100644 --- a/packages/contracts/src/ipc.ts +++ b/packages/contracts/src/ipc.ts @@ -1151,6 +1151,7 @@ export interface DesktopBridge { ) => Promise; receiveProviderAuthCallback: (authorizationUrl: string) => Promise; cancelProviderAuthCallback: (authorizationUrl: string) => Promise; + startFileDrag: (input: { environmentId: EnvironmentId; path: string }) => Promise; openExternal: (url: string) => Promise; /** Reads http(s) media in the main process, outside renderer CORS and CSP; null on network failure. */ readMedia: (url: string) => Promise; From e05b087a34b71a8fe14cc99d4f5b5041e1096e51 Mon Sep 17 00:00:00 2001 From: Martin Bergo Date: Mon, 16 Nov 2026 22:16:48 +0100 Subject: [PATCH 053/253] fix(server): creating a thread no longer records a failed projection read span (#37763) Co-authored-by: Julius Marminge --- .../Orchestrator.control-reads.test.ts | 19 ++++++++- .../src/orchestration-v2/Orchestrator.ts | 25 +++--------- .../src/orchestration-v2/ProjectionStore.ts | 39 +++++++++++-------- .../ProviderTurnControlService.test.ts | 1 + 4 files changed, 47 insertions(+), 37 deletions(-) diff --git a/apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts b/apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts index 214f6cf360e20..ff96b2fbac1ac 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts @@ -23,14 +23,15 @@ import { import * as DateTime from "effect/DateTime"; 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 Layer from "effect/Layer"; import * as Metric from "effect/Metric"; import * as Option from "effect/Option"; import * as Result from "effect/Result"; import * as Stream from "effect/Stream"; -import type * as Tracer from "effect/Tracer"; import * as TestClock from "effect/testing/TestClock"; +import * as Tracer from "effect/Tracer"; import * as SqlClient from "effect/sql/SqlClient"; import * as Statement from "effect/sql/Statement"; import { @@ -1880,7 +1881,21 @@ it.effect.each([false, true])("rejects thread.create for an existing id (deleted ref: { driver: adapter.driver, nativeId: "native-original", strength: "strong" }, }, } as const; - yield* orchestrator.dispatch(create); + const failedSpans: Array = []; + const tracer = Tracer.make({ + span(options) { + const span = new Tracer.NativeSpan(options); + const end = span.end.bind(span); + span.end = (time, exit) => { + end(time, exit); + if (Exit.isFailure(exit)) failedSpans.push(span.name); + }; + return span; + }, + }); + yield* orchestrator.dispatch(create).pipe(Effect.withTracer(tracer)); + // The id check expects a miss; a failed span there reads as an error in Tempo. + assert.deepEqual(failedSpans, []); if (deleted) { yield* orchestrator.dispatch({ type: "thread.delete", diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index 2805935d38890..f047fe771f1d2 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -1821,10 +1821,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio /** Includes tombstones, which still reserve their id. */ const findThread = (threadId: ThreadId) => - projectionStore.getThread(threadId).pipe( - Effect.catchTags({ ProjectionStoreThreadNotFoundError: () => Effect.succeed(null) }), - Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })), - ); + projectionStore + .findThread(threadId) + .pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause }))); /** Side chats live inside their parent, so deleting the parent deletes them too. */ const liveSideChatIds = Effect.fn("Orchestrator.liveSideChatIds")( @@ -1839,11 +1838,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio const sideChatIds: Array = []; for (const transfer of contextTransfers) { if (transfer.type !== "fork" || transfer.sourceThreadId !== threadId) continue; - const child = yield* projectionStore - .getThread(transfer.targetThreadId) - .pipe( - Effect.catchTags({ ProjectionStoreThreadNotFoundError: () => Effect.succeed(null) }), - ); + const child = yield* projectionStore.findThread(transfer.targetThreadId); if (child?.lineage.sideChat === true && child.deletedAt === null) { sideChatIds.push(child.id); } @@ -3762,12 +3757,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio }); // Tombstones reserve their ids too; recreating one would overwrite its history. - const existing = yield* projectionStore.getThread(command.threadId).pipe( - Effect.catchTags({ ProjectionStoreThreadNotFoundError: () => Effect.succeed(null) }), - Effect.mapError( - (cause) => new OrchestratorProjectionError({ threadId: command.threadId, cause }), - ), - ); + const existing = yield* findThread(command.threadId); if (existing !== null) { return yield* new OrchestratorThreadAlreadyExistsError({ commandId: command.commandId, @@ -5643,10 +5633,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio if (runtimeModeCeiling === undefined) return; const runtimeModes = new Map(); for (const threadId of threadsStartingWork(events)) { - const thread = yield* projectionStore.getThread(threadId).pipe( - Effect.catchTags({ ProjectionStoreThreadNotFoundError: () => Effect.succeed(null) }), - Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })), - ); + const thread = yield* findThread(threadId); if (thread !== null) runtimeModes.set(threadId, thread.runtimeMode); } const refusal = runtimeModeCeilingRefusal( diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts index 5b71a6ac042f1..25a86d351548a 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -625,6 +625,10 @@ export interface ProjectionStoreV2Shape { readonly getThread: ( threadId: ThreadId, ) => Effect.Effect; + /** Like getThread, but a missing thread is null, so an expected miss is not an error span. */ + readonly findThread: ( + threadId: ThreadId, + ) => Effect.Effect; readonly getLimitRecoveryCandidates: (options: { readonly now: DateTime.Utc; readonly autoResume: boolean; @@ -5611,27 +5615,29 @@ export const layer: Layer.Layer = ), ); + const selectThread = (threadId: ThreadId) => + sql` + SELECT payload_json FROM orchestration_v2_projection_threads + WHERE thread_id = ${threadId} + `.pipe( + Effect.flatMap((rows) => + rows[0] === undefined ? Effect.succeed(null) : decodeThreadPayload(rows[0].payload_json), + ), + Effect.mapError((cause) => new ProjectionStoreReadError({ threadId, cause })), + ); + const getThread: ProjectionStoreV2Shape["getThread"] = Effect.fn("ProjectionStore.getThread")( function* (threadId) { - const rows = yield* sql` - SELECT payload_json FROM orchestration_v2_projection_threads - WHERE thread_id = ${threadId} - `; - if (rows[0] === undefined) { - return yield* new ProjectionStoreThreadNotFoundError({ threadId }); - } - return yield* decodeThreadPayload(rows[0].payload_json); + const thread = yield* selectThread(threadId); + if (thread === null) return yield* new ProjectionStoreThreadNotFoundError({ threadId }); + return thread; }, - (effect, threadId) => - effect.pipe( - Effect.mapError((cause) => - isProjectionStoreThreadNotFoundError(cause) - ? cause - : new ProjectionStoreReadError({ threadId, cause }), - ), - ), ); + const findThread: ProjectionStoreV2Shape["findThread"] = Effect.fn( + "ProjectionStore.findThread", + )(selectThread); + const requireThread = Effect.fn("ProjectionStore.requireThread")(function* ( threadId: ThreadId, ) { @@ -9292,6 +9298,7 @@ export const layer: Layer.Layer = getThreadShell: readOnReader(getThreadShell), findGuestShareThreadId: readOnReader(findGuestShareThreadId), getThread: readOnReader(getThread), + findThread: readOnReader(findThread), getSettlementCandidates: readOnReader(getSettlementCandidates), getThreadsWithPullRequests: readOnReader(getThreadsWithPullRequests), getThreadTitles: readOnReader(getThreadTitles), diff --git a/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts b/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts index 532d019ddd6fb..069e36ceef916 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts @@ -226,6 +226,7 @@ it.effect( getThreadLinkMetadata: () => Effect.die("unused getThreadLinkMetadata"), findGuestShareThreadId: () => Effect.die("unused findGuestShareThreadId"), getThread: () => Ref.get(projection).pipe(Effect.map((state) => state.thread)), + findThread: () => Effect.die("unused findThread"), getSettlementCandidates: () => Effect.die("unused getSettlementCandidates"), getThreadsWithPullRequests: () => Effect.die("unused getThreadsWithPullRequests"), getThreadWorktreePaths: () => Effect.die("unused getThreadWorktreePaths"), From 80b9620454f29df388659468eb7cc0b213a520ef Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 13:17:19 -0800 Subject: [PATCH 054/253] fix(gitlab): Create MR works with snap-installed glab (#31763) --- packages/source-control-gitlab/package.json | 1 + .../src/server/GitLabCli.test.ts | 64 +++++++++++++++++-- .../src/server/GitLabCli.ts | 36 ++++++++++- .../GitLabPullRequestCli.legacy.test.ts | 2 + ...itLabPullRequestProvider.rateLimit.test.ts | 2 + .../server/GitLabPullRequestProvider.test.ts | 3 +- .../GitLabSourceControlProvider.git.test.ts | 2 + .../GitLabSourceControlProvider.test.ts | 14 +++- pnpm-lock.yaml | 3 + 9 files changed, 119 insertions(+), 8 deletions(-) diff --git a/packages/source-control-gitlab/package.json b/packages/source-control-gitlab/package.json index 592ca6e9d0d86..ec9e4705ace70 100644 --- a/packages/source-control-gitlab/package.json +++ b/packages/source-control-gitlab/package.json @@ -23,6 +23,7 @@ "effect": "catalog:" }, "devDependencies": { + "@effect/platform-node": "catalog:", "@effect/vitest": "catalog:", "@t3tools/source-control-testing": "workspace:*", "@types/node": "catalog:", diff --git a/packages/source-control-gitlab/src/server/GitLabCli.test.ts b/packages/source-control-gitlab/src/server/GitLabCli.test.ts index 884556c67f048..4ec945afb1491 100644 --- a/packages/source-control-gitlab/src/server/GitLabCli.test.ts +++ b/packages/source-control-gitlab/src/server/GitLabCli.test.ts @@ -1,6 +1,9 @@ +import * as NodeServices from "@effect/platform-node/NodeServices"; import { assert, it, afterEach, expect, vi } from "@effect/vitest"; import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; +import * as Path from "effect/Path"; import { ChildProcessSpawner } from "effect/process"; import { VcsProcessExitError } from "@t3tools/contracts"; @@ -11,7 +14,10 @@ import * as GitLabCli from "./GitLabCli.ts"; const mockedRun = vi.fn(); const layer = it.layer( - GitLabCli.layer.pipe(Layer.provide(TestSourceControlHost.layer({ process: { run: mockedRun } }))), + GitLabCli.layer.pipe( + Layer.provide(TestSourceControlHost.layer({ process: { run: mockedRun } })), + Layer.provideMerge(NodeServices.layer), + ), ); function processOutput(stdout: string): SourceControlHost.SourceControlProcessOutput { @@ -98,6 +104,54 @@ layer("GitLabCli.layer", (it) => { }), ); + // A strictly confined glab (Snap) has a private /tmp, so it cannot open the server's body file. + it.effect("sends the merge request description to glab on stdin, not as a file path", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const bodyDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-gitlab-mr-body-" }); + const bodyFile = path.join(bodyDir, "t3code-pr-body.md"); + yield* fileSystem.writeFileString(bodyFile, "## Summary\n\nFixes the thing.\n"); + mockedRun.mockReturnValueOnce(Effect.succeed(processOutput("{}"))); + + const glab = yield* GitLabCli.GitLabCli; + yield* glab.createMergeRequest({ + cwd: "/repo", + baseBranch: "main", + headSelector: "feature/mr", + title: "Add MR", + bodyFile, + }); + + const call = mockedRun.mock.calls.at(-1)?.[0]; + expect(call?.args.join(" ")).not.toContain(bodyFile); + expect(call?.args).toContain("description=@-"); + assert.strictEqual(call?.stdin, "## Summary\n\nFixes the thing.\n"); + }).pipe(Effect.scoped), + ); + + it.effect("fails with a typed error and skips glab when the description cannot be read", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const bodyDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-gitlab-mr-body-" }); + const glab = yield* GitLabCli.GitLabCli; + const error = yield* glab + .createMergeRequest({ + cwd: "/repo", + baseBranch: "main", + headSelector: "feature/mr", + title: "Add MR", + bodyFile: path.join(bodyDir, "missing.md"), + }) + .pipe(Effect.flip); + + assert.strictEqual(error._tag, "GitLabMergeRequestBodyReadError"); + assert.strictEqual(error.detail, "The merge request description could not be read."); + expect(mockedRun).not.toHaveBeenCalled(); + }).pipe(Effect.scoped), + ); + it.effect("skips invalid entries when parsing MR lists", () => Effect.gen(function* () { mockedRun.mockReturnValueOnce( @@ -209,6 +263,8 @@ layer("GitLabCli.layer", (it) => { it.effect("creates same-project MRs without a repository lookup", () => Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const bodyFile = yield* fileSystem.makeTempFileScoped({ prefix: "t3-gitlab-mr-body-" }); mockedRun.mockReturnValueOnce(Effect.succeed(processOutput("{}"))); const glab = yield* GitLabCli.GitLabCli; yield* glab.createMergeRequest({ @@ -216,7 +272,7 @@ layer("GitLabCli.layer", (it) => { baseBranch: "main", headSelector: "feature/fix", title: "Fix remote flow", - bodyFile: "/tmp/body.md", + bodyFile, }); expect(mockedRun).toHaveBeenCalledExactlyOnceWith( expect.objectContaining({ @@ -232,11 +288,11 @@ layer("GitLabCli.layer", (it) => { "--raw-field", "title=Fix remote flow", "--field", - "description=@/tmp/body.md", + "description=@-", ], }), ); - }), + }).pipe(Effect.scoped), ); it.effect("surfaces a friendly error when the merge request is not found", () => diff --git a/packages/source-control-gitlab/src/server/GitLabCli.ts b/packages/source-control-gitlab/src/server/GitLabCli.ts index 649b33cd41332..8b6d7e1ab3c6e 100644 --- a/packages/source-control-gitlab/src/server/GitLabCli.ts +++ b/packages/source-control-gitlab/src/server/GitLabCli.ts @@ -1,6 +1,7 @@ import * as Context from "effect/Context"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; +import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Match from "effect/Match"; import * as Option from "effect/Option"; @@ -238,6 +239,24 @@ export class GitLabNamespaceDecodeError extends Schema.TaggedError()( + "GitLabMergeRequestBodyReadError", + { + operation: Schema.Literal("createMergeRequest"), + command: Schema.Literal("glab"), + cwd: Schema.String, + cause: Schema.Defect(), + }, +) { + get detail(): string { + return "The merge request description could not be read."; + } + + override get message(): string { + return `GitLab CLI failed in ${this.operation}: ${this.detail}`; + } +} + export const GitLabCliError = Schema.Union([ GitLabCliUnavailableError, GitLabCliAuthenticationError, @@ -248,6 +267,7 @@ export const GitLabCliError = Schema.Union([ GitLabMergeRequestDecodeError, GitLabRepositoryDecodeError, GitLabNamespaceDecodeError, + GitLabMergeRequestBodyReadError, ]); export type GitLabCliError = typeof GitLabCliError.Type; @@ -459,6 +479,7 @@ const classifyGlabFailure = (stderr: string): VcsProcessExitFailureKind | null = /** @public */ export const make = Effect.gen(function* () { const { process } = yield* SourceControlHost.SourceControlHost; + const fileSystem = yield* FileSystem.FileSystem; const run = ( input: Parameters[0], @@ -681,6 +702,18 @@ export const make = Effect.gen(function* () { ); }), createMergeRequest: Effect.fn("GitLabCli.createMergeRequest")(function* (input) { + // A confined glab (Snap) has a private /tmp, so it cannot open the server's body file. + const description = yield* fileSystem.readFileString(input.bodyFile).pipe( + Effect.mapError( + (cause) => + new GitLabMergeRequestBodyReadError({ + operation: "createMergeRequest", + command: "glab", + cwd: input.cwd, + cause, + }), + ), + ); let project = ":fullpath"; let targetProjectId: number | undefined; if (input.source?.repository || input.source?.owner) { @@ -718,8 +751,9 @@ export const make = Effect.gen(function* () { "--raw-field", `title=${input.title}`, "--field", - `description=@${input.bodyFile}`, + "description=@-", ], + stdin: description, }); }), getDefaultBranch: Effect.fn("GitLabCli.getDefaultBranch")(function (input) { diff --git a/packages/source-control-gitlab/src/server/GitLabPullRequestCli.legacy.test.ts b/packages/source-control-gitlab/src/server/GitLabPullRequestCli.legacy.test.ts index cf21ac0c24034..5888836ace13a 100644 --- a/packages/source-control-gitlab/src/server/GitLabPullRequestCli.legacy.test.ts +++ b/packages/source-control-gitlab/src/server/GitLabPullRequestCli.legacy.test.ts @@ -1,5 +1,6 @@ import { afterEach, expect, it, vi } from "@effect/vitest"; import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import { ChildProcessSpawner } from "effect/process"; import { VcsProcessExitError } from "@t3tools/contracts"; @@ -14,6 +15,7 @@ const layer = it.layer( GitLabPullRequestCli.layer.pipe( Layer.provide(GitLabCli.layer), Layer.provide(TestSourceControlHost.layer({ process: { run } })), + Layer.provide(FileSystem.layerNoop({})), ), ); const input = { cwd: "/repo", repository: "acme/nested/web", number: 7 }; diff --git a/packages/source-control-gitlab/src/server/GitLabPullRequestProvider.rateLimit.test.ts b/packages/source-control-gitlab/src/server/GitLabPullRequestProvider.rateLimit.test.ts index 9c59f2320326a..c6e18a77e61c9 100644 --- a/packages/source-control-gitlab/src/server/GitLabPullRequestProvider.rateLimit.test.ts +++ b/packages/source-control-gitlab/src/server/GitLabPullRequestProvider.rateLimit.test.ts @@ -1,6 +1,7 @@ import { VcsProcessExitError } from "@t3tools/contracts"; import { expect, it } from "@effect/vitest"; import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost"; @@ -33,6 +34,7 @@ it.effect("reports activity as rate limited when only the reactions endpoint is" }, }), ), + Layer.provide(FileSystem.layerNoop({})), ), ), ); diff --git a/packages/source-control-gitlab/src/server/GitLabPullRequestProvider.test.ts b/packages/source-control-gitlab/src/server/GitLabPullRequestProvider.test.ts index c8f684ff8999c..5b0b41f6c574f 100644 --- a/packages/source-control-gitlab/src/server/GitLabPullRequestProvider.test.ts +++ b/packages/source-control-gitlab/src/server/GitLabPullRequestProvider.test.ts @@ -1,5 +1,6 @@ import { assert, describe, expect, it } from "@effect/vitest"; import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as TestClock from "effect/testing/TestClock"; @@ -75,7 +76,7 @@ it.effect.each(["merge", "disable-auto-merge"] as const)( ); }, }, - }), + }).pipe(Layer.merge(FileSystem.layerNoop({}))), ), ); const cli = yield* GitLabPullRequestCli.make.pipe( diff --git a/packages/source-control-gitlab/src/server/GitLabSourceControlProvider.git.test.ts b/packages/source-control-gitlab/src/server/GitLabSourceControlProvider.git.test.ts index ab6a77dae5bb1..7038e6a82d630 100644 --- a/packages/source-control-gitlab/src/server/GitLabSourceControlProvider.git.test.ts +++ b/packages/source-control-gitlab/src/server/GitLabSourceControlProvider.git.test.ts @@ -7,6 +7,7 @@ import type { GitCommandError } from "@t3tools/contracts"; import { assert, expect, it } from "@effect/vitest"; import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; @@ -59,6 +60,7 @@ it.effect("lists a nested-group fork's merge requests by source branch and names }, }), ), + Layer.provide(FileSystem.layerNoop({})), ), ), ); diff --git a/packages/source-control-gitlab/src/server/GitLabSourceControlProvider.test.ts b/packages/source-control-gitlab/src/server/GitLabSourceControlProvider.test.ts index bc503c9db46a5..8e5077044c6b3 100644 --- a/packages/source-control-gitlab/src/server/GitLabSourceControlProvider.test.ts +++ b/packages/source-control-gitlab/src/server/GitLabSourceControlProvider.test.ts @@ -4,6 +4,7 @@ import { refineUnknownRemoteProvider } from "@t3tools/source-control-core/server import { processOutput } from "@t3tools/source-control-testing/TestSourceControlHost"; import { assert, it } from "@effect/vitest"; import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import { ChildProcessSpawner } from "effect/process"; @@ -333,14 +334,23 @@ it.effect.each([ "--raw-field", "title=Fix remote flow", "--field", - "description=@/tmp/body.md", + "description=@-", ]); + assert.strictEqual(input.stdin, "Body"); return Effect.succeed(processOutput("{}")); }, }, }); const provider = yield* GitLabSourceControlProvider.make.pipe( - Effect.provide(Layer.merge(host, GitLabCli.layer.pipe(Layer.provide(host)))), + Effect.provide( + Layer.merge( + host, + GitLabCli.layer.pipe( + Layer.provide(host), + Layer.provide(FileSystem.layerNoop({ readFileString: () => Effect.succeed("Body") })), + ), + ), + ), ); yield* provider.createChangeRequest({ cwd: "/repo", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5256d97fe1498..9c1a4a0649334 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2437,6 +2437,9 @@ importers: specifier: 4.1.3 version: 4.1.3 devDependencies: + '@effect/platform-node': + specifier: 4.1.2 + version: 4.1.2(bufferutil@4.1.0)(effect@4.1.3)(redis@6.2.1)(utf-8-validate@6.0.6) '@effect/vitest': specifier: 4.1.2 version: 4.1.2(patch_hash=299510b71c36537a832a67e9d160a5f112a5f326a29e95c41cb45fc8234e47c7)(@astrojs/compiler-rs@0.5.1)(@types/node@24.12.4)(@vitest/browser-playwright@5.0.5)(@vitest/ui@5.0.5)(@volar/typescript@2.4.28(typescript@6.0.3))(bufferutil@4.1.0)(effect@4.1.3)(esbuild@0.28.2)(jiti@2.7.0)(jsdom@30.1.0(@noble/hashes@1.8.0))(playwright@1.64.1)(terser@5.48.0)(typescript@6.0.3)(unrun@0.2.39)(utf-8-validate@6.0.6)(yaml@2.9.0) From 747ed9c5b1d9b2d41638ef94003bda1d996f4353 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 13:20:00 -0800 Subject: [PATCH 055/253] fix(mobile): show background work at the end of the feed, and even out its spacing (#37767) --- .../features/threads/ThreadDetailScreen.tsx | 66 ++++++++++--------- .../src/features/threads/ThreadFeed.tsx | 20 +++--- .../threads/floating-working-control.tsx | 30 +-------- .../threads/floating-working-status.ts | 8 --- .../threads/thread-context-divider.tsx | 53 ++++++++++----- 5 files changed, 86 insertions(+), 91 deletions(-) diff --git a/apps/mobile/src/features/threads/ThreadDetailScreen.tsx b/apps/mobile/src/features/threads/ThreadDetailScreen.tsx index b711889a01008..e1f38fec47f25 100644 --- a/apps/mobile/src/features/threads/ThreadDetailScreen.tsx +++ b/apps/mobile/src/features/threads/ThreadDetailScreen.tsx @@ -190,7 +190,7 @@ import { FloatingWorkingControl, } from "./floating-working-control"; import { connectionFloatingStatus, type FloatingWorkingStatus } from "./floating-working-status"; -import type { ThreadLifecycleEvent } from "./thread-context-divider"; +import type { ThreadStatusLineModel } from "./thread-context-divider"; import { derivePendingUserInputMaxHeight, ESTIMATED_KEYBOARD_HEIGHT, @@ -341,6 +341,7 @@ function ColumnWidth(props: { readonly width: number | null; readonly children: ); } const USAGE_LIMITS_PANEL_GAP = 12; +const FEED_END_GAP = 12; const USAGE_LIMITS_PANEL_MIN_HEIGHT = 44; const USER_INPUT_TOGGLE_TIMING = { @@ -738,7 +739,31 @@ function ThreadDetailBody( Dialog.alert("Pi", props.extensionStatuses?.map((status) => status.text).join("\n\n")), } : null; - const lifecycleEvent = ((): ThreadLifecycleEvent | null => { + const backgroundLine = ((): ThreadStatusLineModel | null => { + if (pendingBackgroundWork === null || contentPresentationKind !== "ready") return null; + return { + key: "background", + icon: pendingBackgroundWork.waiting ? "person.2" : "terminal", + label: pendingBackgroundWork.title, + accessibilityLabel: `${pendingBackgroundWork.title}: ${pendingBackgroundWork.items + .map((item) => item.label) + .join(", ")}`, + onOpen: () => { + Keyboard.dismiss(); + navigation.navigate("ThreadBackgroundWork", { + environmentId: props.environmentId, + threadId: props.selectedThread.id, + }); + }, + action: { + label: statusPending ? "Stopping..." : "Stop", + accessibilityLabel: "Stop background work", + disabled: !props.canOperateThread || statusPending, + onPress: () => void runStatusAction("stop"), + }, + }; + })(); + const lifecycleLine = ((): ThreadStatusLineModel | null => { if (contentPresentationKind !== "ready" || !(lifecycleSnoozed || settled || woke)) return null; const actionLabel = lifecycleSnoozed ? "Wake now" : settled ? "Un-settle" : "Dismiss"; const wake = @@ -746,7 +771,8 @@ function ThreadDetailBody( ? snoozeWakeLabel(snoozedUntil, { now: lifecycleNow }) : "now"; return { - state: lifecycleSnoozed ? "snoozed" : settled ? "settled" : "woke", + key: "lifecycle", + icon: settled && !lifecycleSnoozed ? "checkmark.circle" : "alarm", label: lifecycleSnoozed ? wake === "now" ? "Snoozed" @@ -769,6 +795,7 @@ function ThreadDetailBody( }, }; })(); + const statusLines = [backgroundLine, lifecycleLine].filter((line) => line !== null); const floatingStatus = ((): FloatingWorkingStatus | null => { const connectionStatus = connectionFloatingStatus({ connectionError: props.connectionError, @@ -828,29 +855,6 @@ function ThreadDetailBody( }, }; } - if (pendingBackgroundWork !== null && contentPresentationKind === "ready") { - return { - kind: "background", - label: pendingBackgroundWork.title, - accessibilityLabel: `${pendingBackgroundWork.title}: ${pendingBackgroundWork.items - .map((item) => item.label) - .join(", ")}`, - waiting: pendingBackgroundWork.waiting, - onPress: () => { - Keyboard.dismiss(); - navigation.navigate("ThreadBackgroundWork", { - environmentId: props.environmentId, - threadId: props.selectedThread.id, - }); - }, - action: { - label: statusPending ? "Stopping..." : "Stop", - accessibilityLabel: "Stop background work", - disabled: !props.canOperateThread || statusPending, - onPress: () => void runStatusAction("stop"), - }, - }; - } if (props.selectedThread.goal !== null && contentPresentationKind === "ready") { const goal = presentProviderGoal(props.selectedThread.goal, false); return { @@ -1119,7 +1123,8 @@ function ThreadDetailBody( questionInFlow ? 0 : contentInsetEndAdjustment.value + - floatingControlCoverage.value + + // The feed's last row keeps a gap above the composer; the status row grows from it. + Math.max(FEED_END_GAP, floatingControlCoverage.value) + (userInputCoverageApplies ? userInputInsetProgress.value * userInputCardCoverage.value : 0), @@ -1448,7 +1453,7 @@ function ThreadDetailBody( workspaceRoot={props.threadCwd} feed={props.selectedThreadFeed} pendingHandoff={props.pendingHandoff} - lifecycleEvent={lifecycleEvent} + statusLines={statusLines} worktreeSetup={props.worktreeSetup} setupWorkingStartedAt={props.setupWorkingStartedAt} preparingWorktree={ @@ -1483,7 +1488,7 @@ function ThreadDetailBody( questionInFlow ? 0 : estimatedOverlayHeight + - (showFloatingStatus ? FLOATING_WORKING_CONTROL_COVERAGE : 0) + (showFloatingStatus ? FLOATING_WORKING_CONTROL_COVERAGE : FEED_END_GAP) } contentMaxWidth={contentMaxWidth} historyControls={props.historyControls} @@ -1575,8 +1580,7 @@ function ThreadDetailBody( colorScheme={isDarkMode ? "dark" : "light"} status={floatingStatus} extensionStatus={ - floatingStatus !== null && - (floatingStatus.kind === "background" || floatingStatus.kind === "goal") + floatingStatus !== null && floatingStatus.kind === "goal" ? extensionStatus : null } diff --git a/apps/mobile/src/features/threads/ThreadFeed.tsx b/apps/mobile/src/features/threads/ThreadFeed.tsx index be4e05273a999..e092777ad494e 100644 --- a/apps/mobile/src/features/threads/ThreadFeed.tsx +++ b/apps/mobile/src/features/threads/ThreadFeed.tsx @@ -23,8 +23,8 @@ import { import { formatProviderSkillDisplayName } from "@t3tools/shared/inlineSkills"; import { ThreadContextDivider, - ThreadLifecycleLine, - type ThreadLifecycleEvent, + ThreadStatusLine, + type ThreadStatusLineModel, } from "./thread-context-divider"; import { ThreadHandoffDivider, @@ -362,7 +362,8 @@ export interface ThreadFeedProps { readonly workspaceRoot?: string | null; readonly feed: ReadonlyArray; readonly pendingHandoff?: HandoffEndpoints | null; - readonly lifecycleEvent?: ThreadLifecycleEvent | null; + /** Thread states that close the feed: background work, then snooze or settle. */ + readonly statusLines?: ReadonlyArray; readonly contentPresentation: ThreadContentPresentation; readonly latestRun: ThreadFeedLatestRun | null; readonly activeWorkStartedAt: string | null; @@ -1604,7 +1605,7 @@ function ThreadAutomationMessageRow(props: { const summary = taskTitle ?? props.text.trim().split(/\r?\n/, 1)[0] ?? ""; return ( - + 0); return ( {presentation.attribution === "t3code" ? ( @@ -4160,7 +4162,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { } ListFooterComponent={ - props.pendingHandoff || props.lifecycleEvent ? ( + props.pendingHandoff || props.statusLines?.length ? ( <> {props.pendingHandoff ? ( ) : null} - {props.lifecycleEvent ? ( - - ) : null} + {props.statusLines?.map((line) => ( + + ))} ) : null } diff --git a/apps/mobile/src/features/threads/floating-working-control.tsx b/apps/mobile/src/features/threads/floating-working-control.tsx index 3624bc6f310e9..f1143e9e5040f 100644 --- a/apps/mobile/src/features/threads/floating-working-control.tsx +++ b/apps/mobile/src/features/threads/floating-working-control.tsx @@ -81,10 +81,7 @@ export function FloatingWorkingControl(props: { const extensionStatus = props.extensionStatus ?? null; const extensionOnly = props.status?.kind === "extension"; const useGlass = NATIVE_LIQUID_GLASS_SUPPORTED && !extensionOnly; - const action = - props.status?.kind === "background" || props.status?.kind === "stop" - ? props.status.action - : null; + const action = props.status?.kind === "stop" ? props.status.action : null; const [agentsWidth, setAgentsWidth] = useState(0); const hasQueue = props.queuedCount > 0; const hasPreview = props.devicePreview !== null || props.browserPreview !== null; @@ -164,8 +161,7 @@ export function FloatingWorkingControl(props: { const statusInteractive = props.status?.kind === "connection" || props.status?.kind === "extension" || - props.status?.kind === "child-input" || - props.status?.kind === "background"; + props.status?.kind === "child-input"; const capsuleInteractive = statusInteractive || extensionStatus !== null || @@ -436,28 +432,6 @@ function FloatingStatusLabel(props: {
); } - if (props.status.kind === "background") { - return ( - - - - {props.status.label} - - - ); - } if (props.status.kind === "stop") { return ( void; } - | { - readonly kind: "background"; - readonly label: string; - readonly accessibilityLabel: string; - readonly waiting: boolean; - readonly onPress: () => void; - readonly action: FloatingStatusAction; - } | { readonly kind: "stop"; readonly label: string; diff --git a/apps/mobile/src/features/threads/thread-context-divider.tsx b/apps/mobile/src/features/threads/thread-context-divider.tsx index 2db044d7da6a6..ccaa1b1d8aa9d 100644 --- a/apps/mobile/src/features/threads/thread-context-divider.tsx +++ b/apps/mobile/src/features/threads/thread-context-divider.tsx @@ -50,9 +50,13 @@ export function ThreadContextDivider(props: { ); } -export interface ThreadLifecycleEvent { - readonly state: "snoozed" | "settled" | "woke"; +export interface ThreadStatusLineModel { + readonly key: string; + readonly icon: AppSymbolName; readonly label: string; + readonly accessibilityLabel?: string; + /** Tapping the label opens the state's detail. */ + readonly onOpen?: () => void; readonly action: { readonly label: string; readonly accessibilityLabel: string; @@ -61,23 +65,42 @@ export interface ThreadLifecycleEvent { }; } -/** Closes the feed while the thread is snoozed, settled, or just woke. Left-aligned, as on web. */ -export function ThreadLifecycleLine(props: { - readonly event: ThreadLifecycleEvent; +/** A thread state that closes the feed with its one action, left-aligned as on web. */ +export function ThreadStatusLine(props: { + readonly line: ThreadStatusLineModel; readonly iconColor: ColorValue; }) { - const { action } = props.event; - return ( - - + const { line } = props; + const { action } = line; + const body = ( + <> + - {props.event.label} + {line.label} + + ); + return ( + + {line.onOpen ? ( + + {body} + + ) : ( + + {body} + + )} · Date: Mon, 16 Nov 2026 15:38:37 -0600 Subject: [PATCH 056/253] feat(web): copy snapshots into another composer (#37771) Co-authored-by: Cursor --- .../src/lib/composerContextClipboard.test.ts | 13 +++- .../src/lib/composerContextClipboard.ts | 7 +- ...rPromptEditorTiptap.input.browser.test.tsx | 67 ++++++++++++++++- .../components/ComposerPromptEditorTiptap.tsx | 13 +++- apps/web/src/components/chat/ChatComposer.tsx | 37 ++++++++- .../chat/ComposerAttachmentStrip.tsx | 9 ++- .../components/chat/ExpandedImageDialog.tsx | 6 ++ .../components/chat/ExpandedImagePreview.tsx | 9 ++- .../chat/SnapShotAttachmentDetails.tsx | 75 ++++++++++++++++++- .../chat/TimelineUserMessageRow.tsx | 29 ++++++- .../src/components/chat/copySnapshot.test.ts | 75 +++++++++++++++++++ apps/web/src/components/chat/copySnapshot.ts | 33 ++++++++ .../useComposerContextImport.browser.test.tsx | 17 ++++- .../chat/useComposerContextImport.ts | 1 + packages/contracts/src/composerContext.ts | 3 + 15 files changed, 374 insertions(+), 20 deletions(-) create mode 100644 apps/web/src/components/chat/copySnapshot.test.ts create mode 100644 apps/web/src/components/chat/copySnapshot.ts diff --git a/apps/mobile/src/lib/composerContextClipboard.test.ts b/apps/mobile/src/lib/composerContextClipboard.test.ts index 77305cce84439..41cdcf3078bce 100644 --- a/apps/mobile/src/lib/composerContextClipboard.test.ts +++ b/apps/mobile/src/lib/composerContextClipboard.test.ts @@ -73,6 +73,13 @@ const image = { mimeType: "image/png", sizeBytes: 7, attachmentId: "source-file", + source: { + kind: "snap-shot" as const, + capturedAt: "2026-01-01T00:00:00.000Z", + appName: "Finder", + windowTitle: "Downloads", + accessibleText: "Downloads", + }, }; const terminal = { version: 1 as const, @@ -146,7 +153,11 @@ describe("mobile context clipboard imports", () => { const signal = { aborted: false } as AbortSignal; const result = await importComposerContextClipboard(clipboard, 0, signal); expect(result?.failures).toEqual([]); - expect(result?.attachments[0]).toMatchObject({ fileUri: "file:///owned.png", sizeBytes: 42 }); + expect(result?.attachments[0]).toMatchObject({ + fileUri: "file:///owned.png", + sizeBytes: 42, + source: image.source, + }); expect(result?.context.records[0]).toMatchObject({ contextId: "import-1", attachmentId: result?.attachments[0]?.id, diff --git a/apps/mobile/src/lib/composerContextClipboard.ts b/apps/mobile/src/lib/composerContextClipboard.ts index 8c5a31cdb7e44..e744a2cbbf96a 100644 --- a/apps/mobile/src/lib/composerContextClipboard.ts +++ b/apps/mobile/src/lib/composerContextClipboard.ts @@ -253,6 +253,11 @@ async function persistImportedAttachment( sizeBytes: new File(fileUri).size, }; return record.kind === "image" - ? { ...common, type: "image", previewUri: fileUri } + ? { + ...common, + type: "image", + previewUri: fileUri, + ...(record.source ? { source: record.source } : {}), + } : { ...common, type: "file" }; } diff --git a/apps/web/src/components/ComposerPromptEditorTiptap.input.browser.test.tsx b/apps/web/src/components/ComposerPromptEditorTiptap.input.browser.test.tsx index 692a135ad883b..58e9a29b57c1d 100644 --- a/apps/web/src/components/ComposerPromptEditorTiptap.input.browser.test.tsx +++ b/apps/web/src/components/ComposerPromptEditorTiptap.input.browser.test.tsx @@ -7,7 +7,17 @@ import { useRetainedContextPayloads, } from "./chat/composerContextUndo"; -import type { ServerProviderSkill } from "@t3tools/contracts"; +import { + ComposerContextId, + type ComposerContextRecord, + EnvironmentId, + type ServerProviderSkill, +} from "@t3tools/contracts"; +import { + COMPOSER_CONTEXT_CLIPBOARD_MIME, + encodeComposerContextFragment, +} from "@t3tools/shared/composerContextClipboard"; +import { formatComposerContextReference } from "@t3tools/shared/composerContextReferences"; import type { Editor } from "@tiptap/core"; import { redoDepth, undoDepth } from "@tiptap/pm/history"; import { NodeSelection } from "@tiptap/pm/state"; @@ -54,12 +64,16 @@ function Composer({ skills, onChange, onCommandKeyDown = () => false, + importContextFragment, + literalText = false, }: { value: string; richTextEnabled: boolean; skills: ReadonlyArray; onChange?: ComposerPromptEditorProps["onChange"] | undefined; onCommandKeyDown?: ComposerPromptEditorProps["onCommandKeyDown"] | undefined; + importContextFragment?: ComposerPromptEditorProps["importContextFragment"]; + literalText?: boolean; }) { const [prompt, setPrompt] = useState({ value, cursor: value.length }); return ( @@ -78,6 +92,8 @@ function Composer({ }} onPaste={() => {}} onCommandKeyDown={onCommandKeyDown} + importContextFragment={importContextFragment} + literalText={literalText} /> ); } @@ -482,6 +498,55 @@ it.each([ expect(serializeEditorDoc(editor.state.doc).value).toBe(pasted); }); +it.each([ + { literalText: false, imports: 1 }, + { literalText: true, imports: 0 }, +])( + "imports a copied snapshot's context over its image file: %o", + async ({ literalText, imports }) => { + const record = { + version: 1, + kind: "image", + contextId: ComposerContextId.make("image_snapshot"), + label: "shot.png", + attachmentId: "snapshot", + name: "shot.png", + mimeType: "image/png", + sizeBytes: 3, + } satisfies ComposerContextRecord; + const importContextFragment = vi.fn(() => new Map([[record.contextId, record.contextId]])); + await using screen = await render( + , + ); + const editor = editorIn(screen.container); + await act(async () => { + const clipboardData = new DataTransfer(); + clipboardData.items.add( + new File([new Uint8Array([1, 2, 3])], "shot.png", { type: "image/png" }), + ); + clipboardData.setData("text/plain", formatComposerContextReference(record)); + clipboardData.setData( + COMPOSER_CONTEXT_CLIPBOARD_MIME, + encodeComposerContextFragment({ + version: 1, + source: { environmentId: EnvironmentId.make("snapshot-source") }, + records: [record], + })!, + ); + editor.view.dom.dispatchEvent( + new ClipboardEvent("paste", { clipboardData, cancelable: true }), + ); + }); + expect(importContextFragment).toHaveBeenCalledTimes(imports); + }, +); + it.each(["x\r\n\r\ny", "x\r\ry"])( "pastes %j with the same blank line as x\\n\\ny", async (text) => { diff --git a/apps/web/src/components/ComposerPromptEditorTiptap.tsx b/apps/web/src/components/ComposerPromptEditorTiptap.tsx index 20ea35168d213..59d37fbe8c788 100644 --- a/apps/web/src/components/ComposerPromptEditorTiptap.tsx +++ b/apps/web/src/components/ComposerPromptEditorTiptap.tsx @@ -138,7 +138,11 @@ import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip"; import { ComposerCodeBlockNodeView } from "./chat/ComposerCodeBlockNodeView"; import { resolveAssistantCitationCommentDismissal } from "./chat/assistantCitationCommentDismissal"; import { composerCodeBlockHighlight } from "./composerCodeBlockHighlight"; -import { importPastedComposerText, readPastedPlainText } from "./composerInlineTokenPaste"; +import { + importPastedComposerText, + readPastedComposerContext, + readPastedPlainText, +} from "./composerInlineTokenPaste"; import { didComposerSelectionChangeVisibly } from "./composerSelection"; import { captureComposerAttachmentInsertion, @@ -1404,7 +1408,12 @@ function ComposerPromptEditorTiptapInner( }, handlePaste: (view, event) => { const clipboardData = event.clipboardData; - if (!clipboardData || clipboardData.files.length > 0) return false; + if (!clipboardData) return false; + if ( + clipboardData.files.length > 0 && + (literalText || !readPastedComposerContext(clipboardData)?.records.length) + ) + return false; const pastedText = readPastedPlainText(clipboardData); if (!pastedText) return false; event.preventDefault(); diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index be3144efb40b9..8fb98edc14466 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -1,3 +1,4 @@ +import { copySnapshot } from "./copySnapshot"; import { VoiceInputSetup } from "./VoiceInputSetup"; import { useComposerTypingGuard } from "./useComposerTypingGuard"; import { insertAttachmentReferencesForDraft } from "./composerAttachmentReferences"; @@ -148,6 +149,7 @@ import { type TerminalContextSelection, } from "../../lib/terminalContext"; import { + formatComposerContextReference, threadContextRecord, toKindScopedComposerContextId, } from "@t3tools/shared/composerContextReferences"; @@ -717,7 +719,11 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) cwd: gitCwd ?? undefined, projectId: activeProjectId ?? undefined, expandImage: (imageId: string) => { - const preview = buildExpandedImagePreview(composerImages, imageId); + const preview = buildExpandedImagePreview( + composerImages, + imageId, + copyComposerSnapshotStable, + ); if (preview) onExpandImage(preview); }, openFile: setPreviewFileId, @@ -1334,7 +1340,11 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) : null); if (!record) return []; if (attachment.file) copiedFiles.set(record.attachmentId, attachment.file); - return [record]; + return [ + record.kind === "image" && attachment.type === "image" && attachment.source + ? { ...record, source: attachment.source } + : record, + ]; }), ]; if (records.length === 0) return null; @@ -1346,6 +1356,19 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) if (fragment) rememberCopiedComposerAttachments(environmentId, copiedFiles); return fragment; }; + const copyComposerSnapshot = async (imageId: string) => { + const image = composerImages.find((candidate) => candidate.id === imageId); + if (!image) throw new Error("Snapshot is no longer available."); + const contextId = toKindScopedComposerContextId("image", image.id); + const fragment = buildContextClipboardFragment([contextId]); + if (!fragment || !image.previewUrl) throw new Error("The snapshot is not ready to copy."); + await copySnapshot( + image.previewUrl, + formatComposerContextReference({ kind: "image", contextId, label: image.name }), + fragment, + ); + }; + const copyComposerSnapshotStable = useStableCallback(copyComposerSnapshot); const buildContextClipboardFragmentStable = useStableCallback(buildContextClipboardFragment); const { loadThreadTranscript, @@ -2416,6 +2439,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) allImages={composerImages} resolvedTheme={resolvedTheme} onExpandImage={onExpandImage} + onCopySnapshot={copyComposerSnapshotStable} onShowMore={expandMobileComposer} /> ) : null; @@ -2760,9 +2784,13 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) const { files, folders } = splitTransferredFiles(event.clipboardData); const plainText = event.clipboardData.getData("text/plain"); const bypassAutoAttachment = consumePasteAsTextShortcut(pasteAsTextShortcutUntilRef); + const pastedContext = + !isLiteralPendingAnswer && + (readPastedComposerContext(event.clipboardData)?.records.length ?? 0) > 0; if ( folders.length > 0 || - (activeThreadId && + (!pastedContext && + activeThreadId && files.length > 0 && shouldHandleComposerAttachmentPaste({ files, plainText })) ) { @@ -2775,7 +2803,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) return; } - if ((readPastedComposerContext(event.clipboardData)?.records.length ?? 0) > 0) return; + if (pastedContext) return; if (!foldPastedText(plainText, bypassAutoAttachment)) { return; } @@ -3315,6 +3343,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) environmentId={environmentId} attachmentDraftTarget={attachmentDraftTarget} canOperateThread={canOperateThread} + onCopySnapshot={copyComposerSnapshotStable} onExpandImage={onExpandImage} onFileOpen={onFileOpen} onRemoveImage={removeComposerImage} diff --git a/apps/web/src/components/chat/ComposerAttachmentStrip.tsx b/apps/web/src/components/chat/ComposerAttachmentStrip.tsx index ca98b4d2f6c79..59115804e4972 100644 --- a/apps/web/src/components/chat/ComposerAttachmentStrip.tsx +++ b/apps/web/src/components/chat/ComposerAttachmentStrip.tsx @@ -149,6 +149,7 @@ export function ComposerAttachmentMediaRow({ attachmentDraftTarget, canOperateThread, onExpandImage, + onCopySnapshot, onFileOpen, onRemoveImage, onRemoveFile, @@ -167,6 +168,7 @@ export function ComposerAttachmentMediaRow({ readonly environmentId: EnvironmentId; readonly attachmentDraftTarget: ScopedThreadRef | DraftId; readonly canOperateThread: boolean; + readonly onCopySnapshot: (imageId: string) => Promise; readonly onExpandImage: (preview: ExpandedImagePreview) => void; readonly onFileOpen: (attachment: ChatFileAttachment) => void; readonly onRemoveImage: (imageId: string) => void; @@ -224,7 +226,7 @@ export function ComposerAttachmentMediaRow({ aria-label={`Preview ${image.name}`} disabled={!ready} onClick={() => { - const preview = buildExpandedImagePreview(images, image.id); + const preview = buildExpandedImagePreview(images, image.id, onCopySnapshot); if (!preview) return; onExpandImage(preview); }} @@ -254,6 +256,7 @@ export function ComposerAttachmentMediaRow({ {source?.kind === "snap-shot" ? ( onCopySnapshot(ready.id) : undefined} className={cn( upload?.status === "uploading" && "bottom-4", upload?.status === "failed" && "bottom-8", @@ -586,11 +589,13 @@ export function ComposerRestingImagePreviews({ allImages, resolvedTheme, onExpandImage, + onCopySnapshot, onShowMore, }: { readonly images: ReadonlyArray; readonly allImages: ReadonlyArray; readonly resolvedTheme: "light" | "dark"; + readonly onCopySnapshot: (imageId: string) => Promise; readonly onExpandImage: (preview: ExpandedImagePreview) => void; readonly onShowMore: () => void; }) { @@ -606,7 +611,7 @@ export function ComposerRestingImagePreviews({ aria-label={`Preview ${image.name}`} onPointerDown={(event) => event.preventDefault()} onClick={() => { - const preview = buildExpandedImagePreview(allImages, image.id); + const preview = buildExpandedImagePreview(allImages, image.id, onCopySnapshot); if (preview) onExpandImage(preview); }} > diff --git a/apps/web/src/components/chat/ExpandedImageDialog.tsx b/apps/web/src/components/chat/ExpandedImageDialog.tsx index 2d70c5f2ef1dc..a539994d01862 100644 --- a/apps/web/src/components/chat/ExpandedImageDialog.tsx +++ b/apps/web/src/components/chat/ExpandedImageDialog.tsx @@ -18,6 +18,7 @@ import { MediaVideoPlayer } from "../media/MediaVideoPlayer"; import { isContextMenuOpen } from "../../contextMenuFallback"; import { SnapShotAccessibilityData, + SnapShotCopyButton, SnapShotContentsButton, snapShotAccessibilityDetails, } from "./SnapShotAttachmentDetails"; @@ -277,6 +278,11 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({ ) : item.source ? ( ) : null} + {item.onCopySnapshot && ( +
+ +
+ )} diff --git a/apps/web/src/components/chat/ExpandedImagePreview.tsx b/apps/web/src/components/chat/ExpandedImagePreview.tsx index 0eec6753f8e96..ddd2990000119 100644 --- a/apps/web/src/components/chat/ExpandedImagePreview.tsx +++ b/apps/web/src/components/chat/ExpandedImagePreview.tsx @@ -27,6 +27,7 @@ export interface ExpandedImageItem { /** Revoke this owned object URL; other blob URLs belong to the timeline. */ ownsSrc?: boolean; source?: SnapShotSource; + onCopySnapshot?: (() => Promise) | undefined; autoPlay?: boolean; originalUrl?: string; srcFragment?: string; @@ -162,6 +163,7 @@ export function attachVideoThumbnail(video: HTMLVideoElement, file: File): () => export function buildExpandedImagePreview( images: ReadonlyArray, selectedImageId: string, + onCopySnapshot?: (id: string) => Promise, ): ExpandedImagePreview | null { const selected = images.find((image) => image.id === selectedImageId); if (selected?.type === "file" && selected.file && isVideoAttachment(selected)) { @@ -202,7 +204,12 @@ export function buildExpandedImagePreview( src: image.src, name: image.name, mimeType: image.mimeType, - ...(image.source?.kind === "snap-shot" ? { source: image.source } : {}), + ...(image.source?.kind === "snap-shot" + ? { + source: image.source, + onCopySnapshot: onCopySnapshot ? () => onCopySnapshot(image.id) : undefined, + } + : {}), })), index: selectedIndex, }; diff --git a/apps/web/src/components/chat/SnapShotAttachmentDetails.tsx b/apps/web/src/components/chat/SnapShotAttachmentDetails.tsx index 07f16c8794e98..826b335469d09 100644 --- a/apps/web/src/components/chat/SnapShotAttachmentDetails.tsx +++ b/apps/web/src/components/chat/SnapShotAttachmentDetails.tsx @@ -1,6 +1,6 @@ import type { SnapShotSource } from "@t3tools/contracts"; -import { ImageIcon, TextIcon } from "lucide-react"; -import { Suspense } from "react"; +import { CheckIcon, CopyIcon, ImageIcon, TextIcon } from "lucide-react"; +import { Suspense, useRef, useState } from "react"; import { useRenderedAppearance } from "../../hooks/useTheme"; import { cn } from "../../lib/utils"; @@ -8,6 +8,10 @@ import { RenderErrorBoundary } from "../RenderErrorBoundary"; import { Button } from "../ui/button"; import { Popover, PopoverPopup, PopoverTitle, PopoverTrigger } from "../ui/popover"; import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; +import { + ANCHORED_COPY_TOAST_TIMEOUT_MS, + showAnchoredCopyErrorToast, +} from "../ui/anchoredCopyToast"; import { HighlightedTokens } from "./HighlightedTokens"; export const SNAP_SHOT_ATTACHMENT_FRAME_CLASS = @@ -71,14 +75,64 @@ function snapShotIncludesAccessibility(source: SnapShotSource): boolean { return Boolean(source.accessibility || source.accessibleText?.trim()); } +export function SnapShotCopyButton({ + onCopy, + side = "top", +}: { + onCopy: () => Promise; + side?: "top" | "right" | "bottom" | "left"; +}) { + const ref = useRef(null); + const [copied, setCopied] = useState(false); + + return ( + + { + event.stopPropagation(); + void onCopy().then( + () => { + setCopied(true); + setTimeout(() => setCopied(false), ANCHORED_COPY_TOAST_TIMEOUT_MS); + }, + (error: unknown) => + showAnchoredCopyErrorToast( + ref, + error instanceof Error ? error : new Error(String(error)), + ), + ); + }} + size="xs" + variant="ghost-muted" + /> + } + > + {copied ? ( + + Copy snapshot + + ); +} + export function SnapShotContentsButton({ source, className, side = "top", + onCopySnapshot, }: { source: SnapShotSource; className?: string; side?: "top" | "right" | "bottom" | "left"; + onCopySnapshot?: (() => Promise) | undefined; }) { const includesAccessibility = snapShotIncludesAccessibility(source); const ContentsIcon = includesAccessibility ? TextIcon : ImageIcon; @@ -111,7 +165,14 @@ export function SnapShotContentsButton({
- Accessibility data +
+ Accessibility data + {onCopySnapshot && ( +
+ +
+ )} +
{accessibilityDetails ? ( Promise) | undefined; className?: string; }) { return ( @@ -157,7 +220,11 @@ export function SnapShotAttachmentDetails({
{source.appName} - +
{source.windowTitle || "Captured window"} diff --git a/apps/web/src/components/chat/TimelineUserMessageRow.tsx b/apps/web/src/components/chat/TimelineUserMessageRow.tsx index 163635a9b6748..7cd57ed18c0c1 100644 --- a/apps/web/src/components/chat/TimelineUserMessageRow.tsx +++ b/apps/web/src/components/chat/TimelineUserMessageRow.tsx @@ -1,3 +1,4 @@ +import { copySnapshot as copySnapshotToClipboard } from "./copySnapshot"; import { InlineAudioPreview } from "../files/InlineAudioPreview"; import { filePreviewKind } from "@t3tools/shared/filePreview"; import { @@ -597,6 +598,25 @@ function UserTimelineRow({ row }: { row: Extract { + const image = userImages.find((candidate) => candidate.id === id); + const record = resolvedContext.records + .map(asKnownContextRecord) + .find((candidate) => candidate?.kind === "image" && candidate.attachmentId === id); + if (!image?.previewUrl || record?.kind !== "image") + throw new Error("Snapshot is no longer available."); + const fragment = encodeComposerContextFragment({ + version: 1, + source: { environmentId: ctx.activeThreadEnvironmentId, messageId: row.message.id }, + records: [{ ...record, ...(image.source ? { source: image.source } : {}) }], + }); + if (!fragment) throw new Error("The snapshot is too large to copy."); + await copySnapshotToClipboard( + image.previewUrl, + formatComposerContextReference(record), + fragment, + ); + }; const onBodyCopyCapture = (event: React.ClipboardEvent) => { if (resolvedContext.records.length === 0 || !event.clipboardData) return; const selection = window.getSelection(); @@ -661,7 +681,7 @@ function UserTimelineRow({ row }: { row: Extract { - const preview = buildExpandedImagePreview(userImages, image.id); + const preview = buildExpandedImagePreview(userImages, image.id, copySnapshot); if (preview) onImageExpand(preview); }} onOpenFile={onFileOpen} @@ -737,7 +757,7 @@ function UserTimelineRow({ row }: { row: Extract { - const preview = buildExpandedImagePreview(regularImages, image.id); + const preview = buildExpandedImagePreview(regularImages, image.id, copySnapshot); if (!preview) return; ctx.onImageExpand(preview); }} @@ -772,7 +792,10 @@ function UserTimelineRow({ row }: { row: Extract )} {image.previewUrl && image.source?.kind === "snap-shot" ? ( - + copySnapshot(image.id)} + /> ) : null}
))} diff --git a/apps/web/src/components/chat/copySnapshot.test.ts b/apps/web/src/components/chat/copySnapshot.test.ts new file mode 100644 index 0000000000000..9ac7080f7b0f7 --- /dev/null +++ b/apps/web/src/components/chat/copySnapshot.test.ts @@ -0,0 +1,75 @@ +import { ComposerContextId, EnvironmentId } from "@t3tools/contracts"; +import { + COMPOSER_CONTEXT_CLIPBOARD_MIME, + decodeComposerContextClipboardHtml, + encodeComposerContextFragment, +} from "@t3tools/shared/composerContextClipboard"; +import { expect, it } from "vite-plus/test"; + +import { copySnapshot } from "./copySnapshot"; + +it.each([false, true])( + "preserves image bytes and snapshot metadata, custom format rejected: %s", + async (rejectCustom) => { + class Item { + constructor(readonly data: Record>) {} + } + let clipboard: Item | undefined; + const write = async ([item]: Item[]) => { + if (rejectCustom && item?.data[COMPOSER_CONTEXT_CLIPBOARD_MIME]) + throw new Error("Unsupported format"); + clipboard = item; + await Promise.all(Object.values(item!.data)); + }; + const originals = { + navigator: Object.getOwnPropertyDescriptor(globalThis, "navigator"), + ClipboardItem: Object.getOwnPropertyDescriptor(globalThis, "ClipboardItem"), + }; + using _globals = { + [Symbol.dispose]() { + for (const key of ["navigator", "ClipboardItem"] as const) { + const descriptor = originals[key]; + if (descriptor) Object.defineProperty(globalThis, key, descriptor); + else Reflect.deleteProperty(globalThis, key); + } + }, + }; + Object.defineProperty(globalThis, "navigator", { + configurable: true, + value: { clipboard: { write } }, + }); + Object.defineProperty(globalThis, "ClipboardItem", { configurable: true, value: Item }); + const fragment = encodeComposerContextFragment({ + version: 1, + source: { environmentId: EnvironmentId.make("snapshot-source") }, + records: [ + { + version: 1, + kind: "image", + contextId: ComposerContextId.make("image_snapshot"), + label: "Snapshot", + attachmentId: "snapshot", + name: "snapshot.png", + mimeType: "image/png", + sizeBytes: 3, + source: { + kind: "snap-shot", + capturedAt: "2026-01-01T00:00:00.000Z", + appName: "Finder", + windowTitle: "Downloads", + accessibleText: "Downloads", + }, + }, + ], + })!; + const image = "data:image/png;base64,AQID"; + await copySnapshot(image, "Snapshot", fragment); + const png = await clipboard!.data["image/png"]!; + expect(png.type).toBe("image/png"); + expect([...new Uint8Array(await png.arrayBuffer())]).toEqual([1, 2, 3]); + expect( + decodeComposerContextClipboardHtml(await (await clipboard!.data["text/html"]!).text()), + ).toEqual(JSON.parse(fragment)); + expect(await (await clipboard!.data["text/plain"]!).text()).toBe("Snapshot"); + }, +); diff --git a/apps/web/src/components/chat/copySnapshot.ts b/apps/web/src/components/chat/copySnapshot.ts new file mode 100644 index 0000000000000..118121e753647 --- /dev/null +++ b/apps/web/src/components/chat/copySnapshot.ts @@ -0,0 +1,33 @@ +import { + COMPOSER_CONTEXT_CLIPBOARD_MIME, + encodeComposerContextClipboardHtml, +} from "@t3tools/shared/composerContextClipboard"; + +import { readMediaPng } from "../media/mediaContent"; + +export async function copySnapshot(src: string, text: string, fragment: string): Promise { + if (!navigator.clipboard?.write || typeof ClipboardItem === "undefined") { + throw new Error("Snapshot copying requires a secure browser connection."); + } + const formats = { + "image/png": readMediaPng(src), + "text/plain": new Blob([text], { type: "text/plain" }), + "text/html": new Blob([encodeComposerContextClipboardHtml(text, fragment)], { + type: "text/html", + }), + }; + // Start writing within the click gesture, before the image fetch or decode completes. + try { + await navigator.clipboard.write([ + new ClipboardItem({ + ...formats, + [COMPOSER_CONTEXT_CLIPBOARD_MIME]: new Blob([fragment], { + type: COMPOSER_CONTEXT_CLIPBOARD_MIME, + }), + }), + ]); + } catch { + // HTML carries the same snapshot metadata when custom clipboard formats are unsupported. + await navigator.clipboard.write([new ClipboardItem(formats)]); + } +} diff --git a/apps/web/src/components/chat/useComposerContextImport.browser.test.tsx b/apps/web/src/components/chat/useComposerContextImport.browser.test.tsx index e2bf9676b8b6d..d48e965895ddd 100644 --- a/apps/web/src/components/chat/useComposerContextImport.browser.test.tsx +++ b/apps/web/src/components/chat/useComposerContextImport.browser.test.tsx @@ -48,6 +48,7 @@ function deferred() { it.each([ { kind: "file", edit: "delete" }, { kind: "image", edit: "delete" }, + { kind: "image", edit: "retain" }, { kind: "file", edit: "undo" }, { kind: "file", edit: "retain" }, { kind: "file", edit: "switch" }, @@ -124,7 +125,17 @@ it.each([ const attachment = ( kind === "file" ? { ...attachmentFields, kind: "file" as const } - : { ...attachmentFields, kind: "image" as const } + : { + ...attachmentFields, + kind: "image" as const, + source: { + kind: "snap-shot" as const, + appName: "Finder", + windowTitle: "Downloads", + capturedAt: "2026-01-01T00:00:00.000Z", + accessibleText: "Downloads", + }, + } ) satisfies ComposerContextRecord; const annotation = { version: 1, @@ -182,6 +193,10 @@ it.each([ const draft = store.getComposerDraft(target)!; expect(draft.prompt).toBe(promptRef.current); const attachments = kind === "file" ? draft.files : draft.images; + if (kind === "image" && edit === "retain") + expect(draft.images[0]?.source).toEqual( + attachment.kind === "image" ? attachment.source : undefined, + ); expect(attachments.map((file) => file.name)).toEqual( edit === "undo" || edit === "retain" || diff --git a/apps/web/src/components/chat/useComposerContextImport.ts b/apps/web/src/components/chat/useComposerContextImport.ts index 0cc66d6a71d19..265278faec747 100644 --- a/apps/web/src/components/chat/useComposerContextImport.ts +++ b/apps/web/src/components/chat/useComposerContextImport.ts @@ -246,6 +246,7 @@ export function useComposerContextImport(input: { sizeBytes: file.size, previewUrl: URL.createObjectURL(file), file, + ...(record.source ? { source: record.source } : {}), }, ], { diff --git a/packages/contracts/src/composerContext.ts b/packages/contracts/src/composerContext.ts index 982202b05bc30..9884284b3f0db 100644 --- a/packages/contracts/src/composerContext.ts +++ b/packages/contracts/src/composerContext.ts @@ -1,5 +1,7 @@ import * as Schema from "effect/Schema"; +import { SnapShotSource } from "./chatAttachment.ts"; + import { AssistantCitation } from "./assistantCitations.ts"; import { EnvironmentId, @@ -99,6 +101,7 @@ export const ImageContextRecord = Schema.Struct({ ...recordBase, kind: Schema.Literal("image"), ...attachmentBinding, + source: Schema.optional(SnapShotSource), }); export type ImageContextRecord = typeof ImageContextRecord.Type; From acddfae5139ac0bda3300af4440b8f9bbea7a7de Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 13:40:33 -0800 Subject: [PATCH 057/253] fix(desktop): opening the app refreshes the t3 launcher with Local environment off (#31522) --- apps/desktop/src/app/DesktopApp.test.ts | 422 ++++++++++++++---------- apps/desktop/src/app/DesktopApp.ts | 3 + 2 files changed, 258 insertions(+), 167 deletions(-) diff --git a/apps/desktop/src/app/DesktopApp.test.ts b/apps/desktop/src/app/DesktopApp.test.ts index aca3b4fc7fe66..c35dee1ce2a3d 100644 --- a/apps/desktop/src/app/DesktopApp.test.ts +++ b/apps/desktop/src/app/DesktopApp.test.ts @@ -1,5 +1,6 @@ import { assert, it } from "@effect/vitest"; import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; +import * as NodeServices from "@effect/platform-node/NodeServices"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; @@ -54,6 +55,7 @@ import * as DesktopWslBackend from "../wsl/DesktopWslBackend.ts"; import * as BrowserImport from "../preview/BrowserImport/BrowserImport.ts"; import * as DesktopBackendConfiguration from "../backend/DesktopBackendConfiguration.ts"; import * as DesktopCliCommand from "./DesktopCliCommand.ts"; +import * as DesktopCliShim from "./DesktopCliShim.ts"; import * as DesktopClientSettings from "../settings/DesktopClientSettings.ts"; import * as DesktopConnectionCatalogStore from "./DesktopConnectionCatalogStore.ts"; import * as DesktopIpc from "../ipc/DesktopIpc.ts"; @@ -72,6 +74,178 @@ import * as ElectronWindow from "../electron/ElectronWindow.ts"; import * as MacPermissions from "../permissions/MacPermissions.ts"; import * as PreviewManager from "../preview/Manager.ts"; +interface StubbedDesktop { + readonly localEnvironmentEnabled: boolean; + readonly environment: Partial; + readonly fileSystem: Layer.Layer; + readonly record: (event: string) => Effect.Effect; + readonly windowStarted: Deferred.Deferred; + readonly registerLinuxUrlHandler: Effect.Effect; + readonly backends?: ReadonlyArray<{ readonly stop: () => Effect.Effect }>; + readonly flushRendererHistory?: Effect.Effect; + readonly tokenRequested?: Deferred.Deferred; +} + +const runStubbedDesktop = (stub: StubbedDesktop) => + Effect.gen(function* () { + const quitting = yield* Ref.make(false); + const runApp = DesktopApp.program + .pipe( + Effect.provide(NodeCrypto.layer), + Effect.provideService(DesktopAppIdentity.DesktopAppIdentity, { + resolveUserDataPath: Effect.succeed("/isolated/userdata"), + configure: stub.record("identity"), + } as DesktopAppIdentity.DesktopAppIdentity["Service"]), + Effect.provideService(ElectronApp.ElectronApp, { + setPath: () => Effect.void, + whenReady: stub.record("ready"), + } as unknown as ElectronApp.ElectronApp["Service"]), + Effect.provideService(ElectronDialog.ElectronDialog, { + showErrorBox: () => Effect.die("unexpected fatal startup error"), + } as unknown as ElectronDialog.ElectronDialog["Service"]), + Effect.provideService(DesktopEnvironment.DesktopEnvironment, { + platform: "linux", + isDevelopment: false, + isPackaged: true, + stateDir: "/isolated/userdata", + configuredBackendPort: Option.some(3773), + defaultDesktopSettings: { serverExposureMode: "local-only" }, + ...stub.environment, + } as unknown as DesktopEnvironment.DesktopEnvironment["Service"]), + Effect.provideService(DesktopPreReadyPlatform.DesktopPreReadyElectronOptions, { + linux: null, + linuxPasswordStoreCommandLine: "basic", + }), + Effect.provideService(DesktopShellEnvironment.DesktopShellEnvironment, { + installIntoProcess: Effect.succeed(true), + awaitDiscovery: Effect.succeed(true), + awaitUserShellDiscovery: Effect.succeed(true), + updates: Stream.empty, + }), + Effect.provideService(DesktopAppSettings.DesktopAppSettings, { + load: Effect.void, + get: Effect.succeed({ + localEnvironmentEnabled: stub.localEnvironmentEnabled, + serverExposureMode: "local-only", + }), + } as unknown as DesktopAppSettings.DesktopAppSettings["Service"]), + Effect.provideService(DesktopLifecycle.DesktopLifecycle, { + register: Effect.void, + relaunch: () => Effect.void, + }), + Effect.provideService(DesktopClerk.DesktopClerk, { + configure: Effect.void, + } as DesktopClerk.DesktopClerk["Service"]), + Effect.provideService(ElectronSafeStorage.ElectronSafeStorage, { + selectedStorageBackend: Effect.succeedNone, + } as unknown as ElectronSafeStorage.ElectronSafeStorage["Service"]), + Effect.provideService(PreviewPasskeys.PreviewPasskeys, { + configure: Effect.void, + } as PreviewPasskeys.PreviewPasskeys["Service"]), + Effect.provideService(DesktopApplicationMenu.DesktopApplicationMenu, { + configure: stub.record("menu"), + }), + Effect.provideService(DesktopUpdates.DesktopUpdates, { + configure: Effect.void, + } as unknown as DesktopUpdates.DesktopUpdates["Service"]), + Effect.provideService(DesktopLinuxUrlHandler.DesktopLinuxUrlHandler, { + register: stub.registerLinuxUrlHandler, + }), + ) + .pipe( + Effect.provideService(ElectronProtocol.ElectronProtocol, { + registerDesktopProtocol: () => Effect.void, + } as unknown as ElectronProtocol.ElectronProtocol["Service"]), + Effect.provideService(DesktopLegacyLocalStorage.DesktopLegacyLocalStorage, { + load: () => Effect.void, + } as unknown as DesktopLegacyLocalStorage.DesktopLegacyLocalStorage["Service"]), + Effect.provideService(DesktopSnapShot.DesktopSnapShot, { + initialize: Effect.void, + } as DesktopSnapShot.DesktopSnapShot["Service"]), + Effect.provideService(DesktopState.DesktopState, { + quitting, + } as DesktopState.DesktopState["Service"]), + Effect.provideService(DesktopWindow.DesktopWindow, { + ensureMain: stub + .record("window") + .pipe(Effect.andThen(Deferred.succeed(stub.windowStarted, undefined))), + createMainIfBackendReady: stub + .record("window") + .pipe(Effect.andThen(Deferred.succeed(stub.windowStarted, undefined))), + } as unknown as DesktopWindow.DesktopWindow["Service"]), + Effect.provideService(DesktopBackendPool.DesktopBackendPool, { + primary: Effect.succeed({ + start: stub.record("backend"), + currentConfig: Effect.succeedSome({ preflightFailure: Option.none() }), + }), + list: Effect.succeed(stub.backends ?? []), + } as unknown as DesktopBackendPool.DesktopBackendPool["Service"]), + Effect.provideService(DesktopServerExposure.DesktopServerExposure, { + configureFromSettings: () => Effect.succeed({ mode: "local-only" }), + backendConfig: Effect.succeed({ httpBaseUrl: new URL("http://127.0.0.1:3773") }), + } as unknown as DesktopServerExposure.DesktopServerExposure["Service"]), + Effect.provideService(DesktopWslBackend.DesktopWslBackend, { + reconcile: Effect.void, + } as DesktopWslBackend.DesktopWslBackend["Service"]), + Effect.provideService(DesktopAppActivation.DesktopAppActivation, { + start: Effect.void, + } as unknown as DesktopAppActivation.DesktopAppActivation["Service"]), + Effect.provideService(DesktopRendererHistory.DesktopRendererHistory, { + shutdown: stub.flushRendererHistory ?? Effect.void, + } as DesktopRendererHistory.DesktopRendererHistory["Service"]), + ); + return yield* runApp.pipe( + Effect.provide( + Layer.mergeAll( + Layer.mock(BrowserImport.BrowserImport, {}), + Layer.mock(ChildProcessSpawner.ChildProcessSpawner, {}), + Layer.mock(DesktopBackendConfiguration.DesktopBackendConfiguration, {}), + Layer.mock(DesktopCliCommand.DesktopCliCommand, {}), + Layer.mock(DesktopClientSettings.DesktopClientSettings, {}), + Layer.mock(DesktopConnectionCatalogStore.DesktopConnectionCatalogStore, {}), + Layer.mock(DesktopIpc.DesktopIpc, {}), + Layer.mock(DesktopLocalEnvironmentAuth.DesktopLocalEnvironmentAuth, { + getBearerToken: stub + .record("token") + .pipe( + Effect.andThen( + stub.tokenRequested + ? Deferred.succeed(stub.tokenRequested, undefined) + : Effect.void, + ), + Effect.as("token"), + ), + }), + Layer.mock(DesktopManagedPolicy.DesktopManagedPolicy, {}), + Layer.succeed(DesktopPrimaryPaste.DesktopPrimaryPaste, { enabled: true }), + Layer.mock(DesktopSshEnvironment.DesktopSshEnvironment, {}), + Layer.mock(DesktopSshPasswordPrompts.DesktopSshPasswordPrompts, {}), + Layer.mock(DesktopTelemetryPublisher.DesktopTelemetryPublisher, {}), + Layer.mock(DesktopWebLinks.DesktopWebLinks, {}), + Layer.mock(DesktopWslEnvironment.DesktopWslEnvironment, {}), + Layer.mock(ElectronMenu.ElectronMenu, {}), + Layer.mock(ElectronShell.ElectronShell, {}), + Layer.mock(ElectronTheme.ElectronTheme, {}), + Layer.mock(ElectronWindow.ElectronWindow, {}), + Layer.mock(MacPermissions.MacPermissions, {}), + Layer.mock(PreviewManager.PreviewManager, { + isBrowserPartition: () => { + throw new Error("unexpected browser partition lookup"); + }, + }), + stub.fileSystem, + Layer.mock(NetService.NetService, {}), + Path.layer, + Layer.succeed( + HttpClient.HttpClient, + HttpClient.make(() => Effect.die("unexpected HTTP request")), + ), + ), + ), + Effect.forkScoped, + ); + }); + it.effect.each([false, true])( "stops backends together before flushing history and cancels Linux registration, stop defect=%s", (stopDefect) => @@ -85,178 +259,42 @@ it.effect.each([false, true])( const releaseStops = yield* Deferred.make(); const historyStarted = yield* Deferred.make(); const releaseHistory = yield* Deferred.make(); - const quitting = yield* Ref.make(false); const tokenRequested = yield* Deferred.make(); const events: string[] = []; - const record = (event: string) => - Effect.sync(() => { - events.push(event); - }); - const runApp = DesktopApp.program - .pipe( - Effect.provide(NodeCrypto.layer), - Effect.provideService(DesktopAppIdentity.DesktopAppIdentity, { - resolveUserDataPath: Effect.succeed("/isolated/userdata"), - configure: record("identity"), - } as DesktopAppIdentity.DesktopAppIdentity["Service"]), - Effect.provideService(ElectronApp.ElectronApp, { - setPath: () => Effect.void, - whenReady: record("ready"), - } as unknown as ElectronApp.ElectronApp["Service"]), - Effect.provideService(ElectronDialog.ElectronDialog, { - showErrorBox: () => Effect.die("unexpected fatal startup error"), - } as unknown as ElectronDialog.ElectronDialog["Service"]), - Effect.provideService(DesktopEnvironment.DesktopEnvironment, { - platform: "linux", - isDevelopment: false, - isPackaged: true, - stateDir: "/isolated/userdata", - configuredBackendPort: Option.some(3773), - defaultDesktopSettings: { serverExposureMode: "local-only" }, - } as unknown as DesktopEnvironment.DesktopEnvironment["Service"]), - Effect.provideService(DesktopPreReadyPlatform.DesktopPreReadyElectronOptions, { - linux: null, - linuxPasswordStoreCommandLine: "basic", - }), - Effect.provideService(DesktopShellEnvironment.DesktopShellEnvironment, { - installIntoProcess: Effect.succeed(true), - awaitDiscovery: Effect.succeed(true), - awaitUserShellDiscovery: Effect.succeed(true), - updates: Stream.empty, + const app = yield* runStubbedDesktop({ + localEnvironmentEnabled: true, + environment: {}, + fileSystem: FileSystem.layerNoop({}), + record: (event) => + Effect.sync(() => { + events.push(event); }), - Effect.provideService(DesktopAppSettings.DesktopAppSettings, { - load: Effect.void, - get: Effect.succeed({ - localEnvironmentEnabled: true, - serverExposureMode: "local-only", - }), - } as unknown as DesktopAppSettings.DesktopAppSettings["Service"]), - Effect.provideService(DesktopLifecycle.DesktopLifecycle, { - register: Effect.void, - relaunch: () => Effect.void, - }), - Effect.provideService(DesktopClerk.DesktopClerk, { - configure: Effect.void, - } as DesktopClerk.DesktopClerk["Service"]), - Effect.provideService(ElectronSafeStorage.ElectronSafeStorage, { - selectedStorageBackend: Effect.succeedNone, - } as unknown as ElectronSafeStorage.ElectronSafeStorage["Service"]), - Effect.provideService(PreviewPasskeys.PreviewPasskeys, { - configure: Effect.void, - } as PreviewPasskeys.PreviewPasskeys["Service"]), - Effect.provideService(DesktopApplicationMenu.DesktopApplicationMenu, { - configure: record("menu"), - }), - Effect.provideService(DesktopUpdates.DesktopUpdates, { - configure: Effect.void, - } as unknown as DesktopUpdates.DesktopUpdates["Service"]), - Effect.provideService(DesktopLinuxUrlHandler.DesktopLinuxUrlHandler, { - register: Deferred.succeed(registrationStarted, undefined).pipe( - Effect.andThen(Deferred.await(releaseRegistration)), - Effect.ensuring(Deferred.succeed(registrationStopped, undefined)), - ), - }), - ) - .pipe( - Effect.provideService(ElectronProtocol.ElectronProtocol, { - registerDesktopProtocol: () => Effect.void, - } as unknown as ElectronProtocol.ElectronProtocol["Service"]), - Effect.provideService(DesktopLegacyLocalStorage.DesktopLegacyLocalStorage, { - load: () => Effect.void, - } as unknown as DesktopLegacyLocalStorage.DesktopLegacyLocalStorage["Service"]), - Effect.provideService(DesktopSnapShot.DesktopSnapShot, { - initialize: Effect.void, - } as DesktopSnapShot.DesktopSnapShot["Service"]), - Effect.provideService(DesktopState.DesktopState, { - quitting, - } as DesktopState.DesktopState["Service"]), - Effect.provideService(DesktopWindow.DesktopWindow, { - ensureMain: record("window").pipe( - Effect.andThen(Deferred.succeed(windowStarted, undefined)), - ), - } as unknown as DesktopWindow.DesktopWindow["Service"]), - Effect.provideService(DesktopBackendPool.DesktopBackendPool, { - primary: Effect.succeed({ - start: record("backend"), - currentConfig: Effect.succeedSome({ preflightFailure: Option.none() }), - }), - list: Effect.succeed([ - { - stop: () => - Deferred.succeed(firstStopStarted, undefined).pipe( - Effect.andThen(Deferred.await(releaseStops)), - Effect.andThen(stopDefect ? Effect.die("stop defect") : Effect.void), - ), - }, - { - stop: () => - Deferred.succeed(secondStopStarted, undefined).pipe( - Effect.andThen(Deferred.await(releaseStops)), - ), - }, - ]), - } as unknown as DesktopBackendPool.DesktopBackendPool["Service"]), - Effect.provideService(DesktopServerExposure.DesktopServerExposure, { - configureFromSettings: () => Effect.succeed({ mode: "local-only" }), - backendConfig: Effect.succeed({ httpBaseUrl: new URL("http://127.0.0.1:3773") }), - } as unknown as DesktopServerExposure.DesktopServerExposure["Service"]), - Effect.provideService(DesktopWslBackend.DesktopWslBackend, { - reconcile: Effect.void, - } as DesktopWslBackend.DesktopWslBackend["Service"]), - Effect.provideService(DesktopAppActivation.DesktopAppActivation, { - start: Effect.void, - } as unknown as DesktopAppActivation.DesktopAppActivation["Service"]), - Effect.provideService(DesktopRendererHistory.DesktopRendererHistory, { - shutdown: Deferred.succeed(historyStarted, undefined).pipe( - Effect.andThen(Deferred.await(releaseHistory)), - Effect.asVoid, - ), - } as DesktopRendererHistory.DesktopRendererHistory["Service"]), - ); - const app = yield* runApp.pipe( - Effect.provide( - Layer.mergeAll( - Layer.mock(BrowserImport.BrowserImport, {}), - Layer.mock(ChildProcessSpawner.ChildProcessSpawner, {}), - Layer.mock(DesktopBackendConfiguration.DesktopBackendConfiguration, {}), - Layer.mock(DesktopCliCommand.DesktopCliCommand, {}), - Layer.mock(DesktopClientSettings.DesktopClientSettings, {}), - Layer.mock(DesktopConnectionCatalogStore.DesktopConnectionCatalogStore, {}), - Layer.mock(DesktopIpc.DesktopIpc, {}), - Layer.mock(DesktopLocalEnvironmentAuth.DesktopLocalEnvironmentAuth, { - getBearerToken: record("token").pipe( - Effect.andThen(Deferred.succeed(tokenRequested, undefined)), - Effect.as("token"), + windowStarted, + tokenRequested, + registerLinuxUrlHandler: Deferred.succeed(registrationStarted, undefined).pipe( + Effect.andThen(Deferred.await(releaseRegistration)), + Effect.ensuring(Deferred.succeed(registrationStopped, undefined)), + ), + backends: [ + { + stop: () => + Deferred.succeed(firstStopStarted, undefined).pipe( + Effect.andThen(Deferred.await(releaseStops)), + Effect.andThen(stopDefect ? Effect.die("stop defect") : Effect.void), ), - }), - Layer.mock(DesktopManagedPolicy.DesktopManagedPolicy, {}), - Layer.succeed(DesktopPrimaryPaste.DesktopPrimaryPaste, { enabled: true }), - Layer.mock(DesktopSshEnvironment.DesktopSshEnvironment, {}), - Layer.mock(DesktopSshPasswordPrompts.DesktopSshPasswordPrompts, {}), - Layer.mock(DesktopTelemetryPublisher.DesktopTelemetryPublisher, {}), - Layer.mock(DesktopWebLinks.DesktopWebLinks, {}), - Layer.mock(DesktopWslEnvironment.DesktopWslEnvironment, {}), - Layer.mock(ElectronMenu.ElectronMenu, {}), - Layer.mock(ElectronShell.ElectronShell, {}), - Layer.mock(ElectronTheme.ElectronTheme, {}), - Layer.mock(ElectronWindow.ElectronWindow, {}), - Layer.mock(MacPermissions.MacPermissions, {}), - Layer.mock(PreviewManager.PreviewManager, { - isBrowserPartition: () => { - throw new Error("unexpected browser partition lookup"); - }, - }), - FileSystem.layerNoop({}), - Layer.mock(NetService.NetService, {}), - Path.layer, - Layer.succeed( - HttpClient.HttpClient, - HttpClient.make(() => Effect.die("unexpected HTTP request")), - ), - ), + }, + { + stop: () => + Deferred.succeed(secondStopStarted, undefined).pipe( + Effect.andThen(Deferred.await(releaseStops)), + ), + }, + ], + flushRendererHistory: Deferred.succeed(historyStarted, undefined).pipe( + Effect.andThen(Deferred.await(releaseHistory)), + Effect.asVoid, ), - Effect.forkScoped, - ); + }); yield* Deferred.await(registrationStarted); yield* Deferred.await(windowStarted); yield* Deferred.await(tokenRequested); @@ -283,3 +321,53 @@ it.effect.each([false, true])( assert.isTrue(yield* Deferred.isDone(registrationStopped)); }).pipe(Effect.provide(DesktopShutdown.layer)), ); + +it.effect("points a stale t3 launcher at the updated AppImage with Local environment off", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-desktop-app-" }); + const launcher = DesktopCliShim.launcherPath({ + path, + baseDir, + platform: "linux", + } as DesktopEnvironment.DesktopEnvironment["Service"]); + const removedAppImage = path.join(baseDir, "T3-Code-0.1.6-x86_64.AppImage"); + const updatedAppImage = path.join(baseDir, "T3-Code-0.1.7-x86_64.AppImage"); + yield* fs.makeDirectory(path.dirname(launcher), { recursive: true }); + yield* fs.writeFileString( + launcher, + DesktopCliShim.renderCliShim({ + target: { kind: "appimage", appImage: removedAppImage, executableName: "t3code" }, + shimPath: launcher, + t3Home: baseDir, + }), + ); + yield* fs.writeFileString(updatedAppImage, "", { mode: 0o755 }); + const windowStarted = yield* Deferred.make(); + + const app = yield* runStubbedDesktop({ + localEnvironmentEnabled: false, + environment: { + path, + baseDir, + serverRoot: path.join(baseDir, "resources", "app.asar"), + appImagePath: Option.some(updatedAppImage), + }, + fileSystem: Layer.succeed(FileSystem.FileSystem, fs), + record: () => Effect.void, + windowStarted, + registerLinuxUrlHandler: Effect.void, + }); + yield* Deferred.await(windowStarted); + yield* (yield* DesktopShutdown.DesktopShutdown).request; + yield* Fiber.join(app); + + const content = yield* fs.readFileString(launcher); + assert.include( + content, + `app='${updatedAppImage}'`, + "startup left the t3 launcher pointing at the removed AppImage", + ); + }).pipe(Effect.provide(Layer.mergeAll(DesktopShutdown.layer, NodeServices.layer))), +); diff --git a/apps/desktop/src/app/DesktopApp.ts b/apps/desktop/src/app/DesktopApp.ts index f69bf965d0058..56fe979cf8c67 100644 --- a/apps/desktop/src/app/DesktopApp.ts +++ b/apps/desktop/src/app/DesktopApp.ts @@ -15,6 +15,7 @@ import { installDesktopIpcHandlers } from "../ipc/DesktopIpcHandlers.ts"; import * as DesktopAppActivation from "./DesktopAppActivation.ts"; import * as DesktopAppIdentity from "./DesktopAppIdentity.ts"; import * as DesktopClerk from "./DesktopClerk.ts"; +import * as DesktopCliShim from "./DesktopCliShim.ts"; import * as DesktopCliCommand from "./DesktopCliCommand.ts"; import * as DesktopApplicationMenu from "../window/DesktopApplicationMenu.ts"; import * as DesktopWindow from "../window/DesktopWindow.ts"; @@ -301,6 +302,8 @@ const bootstrap = Effect.fn("desktop.bootstrap")(function* (backendStarted: bool ); if (!settings.localEnvironmentEnabled) { + // The backend config refreshes the launcher when the local environment runs; an update may move the app. + yield* DesktopCliShim.install; if (!(yield* Ref.get(state.quitting))) { yield* desktopWindow.createMainIfBackendReady; } From a6f7981abba71a51b299bd0ecbf7577d20660541 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 13:41:38 -0800 Subject: [PATCH 058/253] fix(pi): ask_user_question single-select questions render as a structured card (#32125) --- .../provider-pi/src/server/adapter.test.ts | 289 ++++++++++++++++++ packages/provider-pi/src/server/adapter.ts | 147 ++++++++- 2 files changed, 434 insertions(+), 2 deletions(-) diff --git a/packages/provider-pi/src/server/adapter.test.ts b/packages/provider-pi/src/server/adapter.test.ts index ff9f9c22ef976..31d0a70cae48a 100644 --- a/packages/provider-pi/src/server/adapter.test.ts +++ b/packages/provider-pi/src/server/adapter.test.ts @@ -4895,6 +4895,295 @@ describe("PiAdapterV2", () => { }).pipe(Effect.scoped, Effect.provide(layerTest)), ); + it.effect("renders an ask_user_question select from the tool call's structured question", () => + Effect.gen(function* () { + const fake = yield* makeFakePi; + const { runtime, takeEvent } = yield* openRuntime(fake); + const providerThread = yield* runtime.ensureThread({ + threadId: THREAD_ID, + modelSelection: modelSelection("default"), + runtimePolicy, + }); + yield* startTurn(runtime, providerThread); + yield* fake.takeRequest("prompt"); + yield* fake.emit({ + type: "tool_execution_start", + toolCallId: "ask-call", + toolName: "ask_user_question", + args: { + questions: [ + { + header: "Area", + question: "Which area should we work on?", + options: [ + { + label: "Docker/Compose", + description: "Review or edit Compose projects", + preview: "services:\n web: {}", + }, + { label: "Home Assistant", description: "Automations, dashboards or YAML" }, + ], + }, + ], + }, + }); + const dockerLine = "1. Docker/Compose — Review or edit Compose projects"; + yield* fake.emit({ + type: "extension_ui_request", + id: "ui-ask", + method: "select", + title: + "[Area] Which area should we work on?\n\n--- 1. Docker/Compose preview ---\nservices:\n web: {}", + options: [ + dockerLine, + "2. Home Assistant — Automations, dashboards or YAML", + "3. Type something.", + ], + }); + const event = yield* takeEvent( + (event) => + event.type === "turn_item.updated" && event.turnItem.type === "user_input_request", + ); + if (event.type !== "turn_item.updated" || event.turnItem.type !== "user_input_request") { + return assert.fail("expected a user input request"); + } + const question = decodeUserInputQuestions(event.turnItem.questions)[0]; + assert.deepEqual( + { + header: question?.header, + question: question?.question, + options: question?.options.map(({ label, description }) => ({ label, description })), + }, + { + header: "Area", + question: + "Which area should we work on?\n\n--- 1. Docker/Compose preview ---\nservices:\n web: {}", + options: [ + { label: "Docker/Compose", description: "Review or edit Compose projects" }, + { label: "Home Assistant", description: "Automations, dashboards or YAML" }, + ], + }, + ); + yield* runtime.respondToRuntimeRequest({ + requestId: event.turnItem.requestId, + answers: { [question!.id]: question!.options[0]!.value }, + }); + const response = yield* fake.takeRequest("extension_ui_response"); + assert.equal(response["id"], "ui-ask"); + assert.equal(response["value"], dockerLine); + }).pipe(Effect.scoped, Effect.provide(layerTest)), + ); + + it.effect("matches an ask_user_question select to the question with the same full title", () => + Effect.gen(function* () { + const fake = yield* makeFakePi; + const { runtime, takeEvent } = yield* openRuntime(fake); + const providerThread = yield* runtime.ensureThread({ + threadId: THREAD_ID, + modelSelection: modelSelection("default"), + runtimePolicy, + }); + yield* startTurn(runtime, providerThread); + yield* fake.takeRequest("prompt"); + const options = [ + { label: "Yes", description: "Go ahead" }, + { label: "No", description: "Stop here" }, + ]; + yield* fake.emit({ + type: "tool_execution_start", + toolCallId: "ask-call", + toolName: "ask_user_question", + // Some models JSON-encode `questions`; the extension decodes it before asking. + args: { + questions: JSON.stringify([ + { header: "Deploy", question: "Proceed?", options }, + { header: "Deploy", question: "Proceed? Only for staging?", options }, + ]), + }, + }); + yield* fake.emit({ + type: "extension_ui_request", + id: "ui-ask", + method: "select", + title: "[Deploy] Proceed? Only for staging?", + options: ["1. Yes — Go ahead", "2. No — Stop here", "3. Type something."], + }); + const event = yield* takeEvent( + (event) => + event.type === "turn_item.updated" && event.turnItem.type === "user_input_request", + ); + if (event.type !== "turn_item.updated" || event.turnItem.type !== "user_input_request") { + return assert.fail("expected a user input request"); + } + const question = decodeUserInputQuestions(event.turnItem.questions)[0]; + assert.equal(question?.question, "Proceed? Only for staging?"); + // Option lines that differ from the recorded call keep the raw select. + const unmatchedLines = ["1. Yes — Go", "2. No — Stop here", "3. Type something."]; + yield* fake.emit({ + type: "extension_ui_request", + id: "ui-unmatched", + method: "select", + title: "[Deploy] Proceed?", + options: unmatchedLines, + }); + const unmatched = yield* takeEvent( + (event) => + event.type === "turn_item.updated" && event.turnItem.type === "user_input_request", + ); + if ( + unmatched.type !== "turn_item.updated" || + unmatched.turnItem.type !== "user_input_request" + ) { + return assert.fail("expected a user input request"); + } + const unmatchedQuestion = decodeUserInputQuestions(unmatched.turnItem.questions)[0]; + assert.deepEqual( + { + header: unmatchedQuestion?.header, + question: unmatchedQuestion?.question, + options: unmatchedQuestion?.options.map(({ label, value }) => [label, value]), + }, + { + header: "Question", + question: "[Deploy] Proceed?", + options: unmatchedLines.map((line) => [line, line]), + }, + ); + }).pipe(Effect.scoped, Effect.provide(layerTest)), + ); + + it.effect("answers each ask_user_question custom text through its follow-up input", () => + Effect.gen(function* () { + const fake = yield* makeFakePi; + const { runtime, takeEvent } = yield* openRuntime(fake); + const providerThread = yield* runtime.ensureThread({ + threadId: THREAD_ID, + modelSelection: modelSelection("default"), + runtimePolicy, + }); + yield* startTurn(runtime, providerThread); + yield* fake.takeRequest("prompt"); + const args = { + questions: [ + { + question: "How many replicas?", + options: [ + { label: "One", description: "Single instance" }, + { label: "Three", description: "High availability" }, + ], + }, + ], + }; + // Parallel questionnaires can ask the same question. + yield* fake.emit({ + type: "tool_execution_start", + toolCallId: "ask-call", + toolName: "ask_user_question", + args, + }); + yield* fake.emit({ + type: "tool_execution_start", + toolCallId: "ask-call-parallel", + toolName: "ask_user_question", + args, + }); + const lines = ["1. One — Single instance", "2. Three — High availability"]; + const followUpTitle = `How many replicas?\n\n${lines.join("\n")}\n\nType your answer:`; + const takeWaitingRequest = Effect.fnUntraced(function* () { + const event = yield* takeEvent( + (event) => + event.type === "turn_item.updated" && + event.turnItem.type === "user_input_request" && + event.turnItem.status === "waiting", + ); + if (event.type !== "turn_item.updated" || event.turnItem.type !== "user_input_request") { + return assert.fail("expected a user input request"); + } + return event.turnItem; + }); + const answerWithCustomText = Effect.fnUntraced(function* (id: string, text: string) { + yield* fake.emit({ + type: "extension_ui_request", + id, + method: "select", + title: "How many replicas?", + options: [...lines, "3. Type something."], + }); + const request = yield* takeWaitingRequest(); + yield* runtime.respondToRuntimeRequest({ + requestId: request.requestId, + answers: { [request.questions[0]!.id]: text }, + }); + const selectResponse = yield* fake.takeRequest("extension_ui_response"); + assert.equal(selectResponse["value"], "3. Type something."); + }); + // The extension reads a bare number sent as the select's value as that option. + yield* answerWithCustomText("ui-ask", "2"); + yield* answerWithCustomText("ui-ask-parallel", "5"); + // Parallel dialogs can arrive before the follow-up input, including a multi-select of the + // same question, which the extension asks as an input with a non-empty placeholder. + const multiSelectTitle = `How many replicas?\n\n${lines.join("\n")}\n\nEnter the numbers of all that apply.`; + yield* fake.emit({ + type: "extension_ui_request", + id: "ui-multi", + method: "input", + title: multiSelectTitle, + placeholder: "1,3; custom answer", + }); + // Another extension's blank-placeholder input that only shares the question is asked. + const unrelatedTitle = "How many replicas?\n\nEnter the release note:"; + yield* fake.emit({ + type: "extension_ui_request", + id: "ui-unrelated", + method: "input", + title: unrelatedTitle, + placeholder: "", + }); + yield* fake.emit({ + type: "extension_ui_request", + id: "ui-other", + method: "input", + title: "Anything else?", + }); + assert.equal((yield* takeWaitingRequest()).title, multiSelectTitle); + assert.equal((yield* takeWaitingRequest()).title, unrelatedTitle); + assert.equal((yield* takeWaitingRequest()).title, "Anything else?"); + for (const [id, value] of [ + ["ui-custom", "2"], + ["ui-custom-parallel", "5"], + ]) { + yield* fake.emit({ + type: "extension_ui_request", + id, + method: "input", + title: followUpTitle, + placeholder: "", + }); + const inputResponse = yield* fake.takeRequest("extension_ui_response"); + assert.deepEqual({ id: inputResponse["id"], value: inputResponse["value"] }, { id, value }); + } + // An answer staged when the turn ends is dropped, so a later matching input is asked. + yield* answerWithCustomText("ui-ask-late", "7"); + yield* fake.emit({ type: "agent_end", messages: [], willRetry: false }); + yield* fake.emit({ type: "agent_settled" }); + yield* takeEvent((event) => event.type === "turn.terminal"); + yield* fake.emit({ + type: "extension_ui_request", + id: "ui-custom-late", + method: "input", + title: followUpTitle, + placeholder: "", + }); + yield* fake.emit({ + type: "extension_ui_request", + id: "ui-after", + method: "input", + title: "Anything else?", + }); + assert.equal((yield* takeWaitingRequest()).title, followUpTitle); + }).pipe(Effect.scoped, Effect.provide(layerTest)), + ); + it.effect.each(["Which database?\n1. Postgres\n2. Type something.\nType your answer:", " "])( "asks an input dialog's title when its placeholder is blank, with title '%s'", (title) => diff --git a/packages/provider-pi/src/server/adapter.ts b/packages/provider-pi/src/server/adapter.ts index 6694623d98883..418db7219cb95 100644 --- a/packages/provider-pi/src/server/adapter.ts +++ b/packages/provider-pi/src/server/adapter.ts @@ -409,6 +409,17 @@ interface PendingPiPrompt { runtimeRequest: OrchestrationV2RuntimeRequest; readonly node: OrchestrationV2ExecutionNode; readonly turnItem: OrchestrationV2TurnItem; + readonly customAnswerRoute: PiCustomAnswerRoute | undefined; +} + +/** + * The ask_user_question extension misreads typed text sent as a select value, so a custom answer + * picks the select's last "Type something." line and answers the input dialog that follows. + */ +interface PiCustomAnswerRoute { + readonly lines: ReadonlyArray; + readonly sentinel: string; + readonly followUpTitlePrefix: string; } function piApprovalRequestKind(title: string): "command" | "file-change" { @@ -601,6 +612,9 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* ( ProviderAdapter.ProviderAdapterV2Error | Cause.Done >(); const pendingPrompts = new Map(); + // Custom answers waiting for their ask_user_question follow-up input, keyed by title prefix. + // FIFO per prefix: the extension asks each follow-up right after its own select is answered. + const customAnswerFollowUps = new Map(); const sessionApprovals = new Set(); // Async pi-subagents runs launched in this session, by async id, until they settle. const piAsyncRuns = new Map< @@ -1685,6 +1699,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* ( ) { const pending = Array.from(pendingPrompts.values()); pendingPrompts.clear(); + customAnswerFollowUps.clear(); yield* Effect.forEach(pending, (prompt) => cancelPrompt(prompt, resolvedAt), { discard: true, }); @@ -1777,6 +1792,24 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* ( return; } if (nativeRequestId === undefined) return; + // The extension's follow-up input has an empty placeholder; its multi-select input does not. + const inputTitle = + method === "input" && event["placeholder"] === "" + ? (recordString(event, "title") ?? "") + : ""; + const followUpPrefix = Array.from(customAnswerFollowUps.keys()) + .filter((prefix) => inputTitle.startsWith(prefix)) + .reduce( + (longest, prefix) => (prefix.length > (longest?.length ?? -1) ? prefix : longest), + undefined, + ); + if (followUpPrefix !== undefined) { + const queued = customAnswerFollowUps.get(followUpPrefix) ?? []; + const value = queued.shift() ?? ""; + if (queued.length === 0) customAnswerFollowUps.delete(followUpPrefix); + yield* connection.send({ type: "extension_ui_response", id: nativeRequestId, value }); + return; + } const approvalTitle = recordString(event, "title") ?? ""; const approvalKey = `${approvalTitle.length}:${approvalTitle}${recordString(event, "message") ?? ""}`; if (method === "confirm" && sessionApprovals.has(approvalKey)) { @@ -1851,6 +1884,10 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* ( completedAt: null, updatedAt: createdAt, }; + const askUser = + method === "select" && turn !== null + ? piAskUserSingleSelectQuestion(nativeRequestId, title, event, turn.toolArgs) + : undefined; const turnItem: OrchestrationV2TurnItem = method === "confirm" ? { @@ -1864,7 +1901,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* ( ...itemBase, type: "user_input_request", requestId, - questions: [piQuestion(nativeRequestId, method, title, event)], + questions: [askUser?.question ?? piQuestion(nativeRequestId, method, title, event)], }; const pending: PendingPiPrompt = { nativeRequestId, @@ -1876,6 +1913,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* ( runtimeRequest, node, turnItem, + customAnswerRoute: askUser?.customAnswerRoute, }; pendingPrompts.set(String(requestId), pending); yield* emit({ @@ -3575,7 +3613,20 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* ( `Pi extension request ${requestInput.requestId} has expired`, ); } - const response = piUiResponse(pending, requestInput.decision, requestInput.answers); + let response = piUiResponse(pending, requestInput.decision, requestInput.answers); + const route = pending.customAnswerRoute; + if ( + route !== undefined && + typeof response["value"] === "string" && + response["value"].trim().length > 0 && + !route.lines.includes(response["value"]) + ) { + customAnswerFollowUps.set(route.followUpTitlePrefix, [ + ...(customAnswerFollowUps.get(route.followUpTitlePrefix) ?? []), + response["value"], + ]); + response = { value: route.sentinel }; + } yield* connection.send({ type: "extension_ui_response", id: pending.nativeRequestId, @@ -4170,6 +4221,98 @@ function piQuestion( }; } +/** The extension normalizes line terminators in each question field before building the dialog. */ +const rpivField = (input: unknown, key: string) => + recordString(input, key)?.replace(/\r\n/g, "\n").replace(/\r/g, ""); + +const rpivOptionLine = (option: unknown, index: number) => + // oxlint-disable-next-line t3code/no-em-dash-copy -- Matches the extension's option line format. + `${index + 1}. ${rpivField(option, "label")} — ${rpivField(option, "description")}`; + +/** The extension folds option previews into the select title, the only place clients show them. */ +const rpivPreviewBlock = (options: ReadonlyArray) => { + const blocks = options.flatMap((option, index) => { + const preview = rpivField(option, "preview"); + return preview + ? [`--- ${index + 1}. ${rpivField(option, "label")} preview ---\n${preview.slice(0, 600)}`] + : []; + }); + return blocks.length > 0 ? `\n\n${blocks.join("\n\n")}` : ""; +}; + +/** Mirrors the extension's `prepareQuestionArguments`: some models JSON-encode nested args. */ +const rpivDecoded = (value: unknown, isShape: (parsed: unknown) => boolean) => { + if (typeof value !== "string") return value; + try { + const parsed: unknown = JSON.parse(value); + return isShape(parsed) ? parsed : value; + } catch { + return value; + } +}; + +/** + * @juicesharp/rpiv-ask-user-question asks a single-select question over RPC as a select of + * "N. label — description" lines plus a trailing "Type something." entry. Rebuild the question + * from the tool call's arguments; each option answers with its original line, which the extension + * maps back to that option. + */ +function piAskUserSingleSelectQuestion( + questionId: string, + title: string, + event: PiRpcRecord, + toolArgs: ReadonlyMap, +): + | { question: OrchestrationV2UserInputQuestion; customAnswerRoute: PiCustomAnswerRoute } + | undefined { + const lines = event["options"]; + if (!Array.isArray(lines) || lines.length < 2) return undefined; + const sentinel = lines.at(-1); + if (typeof sentinel !== "string") return undefined; + for (const args of toolArgs.values()) { + const questions = rpivDecoded(recordField(args, "questions"), Array.isArray); + if (!Array.isArray(questions)) continue; + for (const entry of questions) { + const question = rpivDecoded(entry, Predicate.isObject); + const text = rpivField(question, "question"); + const header = rpivField(question, "header"); + if (recordField(question, "multiSelect") === true || !text?.trim()) continue; + const prefix = header ? `[${header}] ` : ""; + if (!title.startsWith(`${prefix}${text}`)) continue; + const encodedOptions = rpivDecoded(recordField(question, "options"), Array.isArray); + if (!Array.isArray(encodedOptions) || encodedOptions.length !== lines.length - 1) continue; + const options = encodedOptions.map((option) => rpivDecoded(option, Predicate.isObject)); + const body = `${text}${rpivPreviewBlock(options)}`; + if (title !== `${prefix}${body}`) continue; + if (!options.every((option, index) => lines[index] === rpivOptionLine(option, index))) { + continue; + } + return { + question: { + id: questionId, + header: header?.trim() || "Question", + question: body.trim(), + options: options.map((option, index) => { + const label = rpivField(option, "label")?.trim() || `Option ${index + 1}`; + return { + label, + description: rpivField(option, "description")?.trim() || label, + value: rpivOptionLine(option, index), + }; + }), + }, + // The follow-up repeats the option lines; only its trailing instruction is localized. + customAnswerRoute: { + lines, + sentinel, + followUpTitlePrefix: `${prefix}${text}\n\n${lines.slice(0, -1).join("\n")}\n\n`, + }, + }; + } + } + return undefined; +} + function piUiResponse( pending: PendingPiPrompt, decision: ProviderApprovalDecision | undefined, From a10f9e41d378de696f1bb2f1c69580c5df59af18 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 13:42:44 -0800 Subject: [PATCH 059/253] fix(server): pull request links whose reads keep failing back off instead of re-reading every minute (#31969) --- .../PullRequestSyncReactor.test.ts | 88 +++++++++++++++++++ .../PullRequestSyncReactor.ts | 19 +++- 2 files changed, 106 insertions(+), 1 deletion(-) diff --git a/apps/server/src/orchestration-v2/PullRequestSyncReactor.test.ts b/apps/server/src/orchestration-v2/PullRequestSyncReactor.test.ts index bb0b3380c38c4..4cb350a380a84 100644 --- a/apps/server/src/orchestration-v2/PullRequestSyncReactor.test.ts +++ b/apps/server/src/orchestration-v2/PullRequestSyncReactor.test.ts @@ -325,6 +325,24 @@ const sweepAgain = Effect.fn("sweepPullRequestSyncHarness")(function* ( yield* reactor.drain; }); +const sweepFor = Effect.fn("sweepPullRequestSyncHarnessFor")(function* ( + fixture: Harness, + reactor: PullRequestSyncReactor.PullRequestSyncReactor["Service"], + minutes: number, +) { + for (let minute = 0; minute < minutes; minute++) yield* sweepAgain(fixture, reactor); +}); + +const failingGitLabLink = makeLink(13, null, { + host: "gitlab.com", + url: "https://gitlab.com/owner/repository/-/merge_requests/13", +}); + +const gitLabCliFailure = new PullRequestOperationError({ + operation: "summary", + detail: "GitLab CLI command failed", +}); + function runUpdated( threadId: ThreadId, status: OrchestrationV2Run["status"], @@ -1839,6 +1857,76 @@ describe("PullRequestSyncReactor", () => { }), ); + it.effect( + "caps the failed-read backoff at the slow cadence and restarts it after a success", + () => + Effect.scoped( + Effect.gen(function* () { + yield* TestClock.setTime(Date.parse(NOW)); + const failing = yield* Ref.make(true); + const fixture = yield* makeHarness({ + snapshot: makeSnapshot([makeThread("one", { pullRequests: [failingGitLabLink] })]), + summary: (input) => + Ref.get(failing).pipe( + Effect.flatMap((fails) => + fails ? Effect.fail(gitLabCliFailure) : Effect.succeed(makeSummary(input)), + ), + ), + }); + yield* Effect.gen(function* () { + const reactor = yield* startAndSweep(fixture); + const readMinutes = [0]; + const sweepMinutes = Effect.fnUntraced(function* (from: number, to: number) { + for (let minute = from; minute <= to; minute++) { + const before = (yield* Ref.get(fixture.summaryCalls)).length; + yield* sweepAgain(fixture, reactor); + if ((yield* Ref.get(fixture.summaryCalls)).length > before) + readMinutes.push(minute); + } + }); + + yield* sweepMinutes(1, 60); + assert.deepStrictEqual(readMinutes, [0, 1, 3, 7, 15, 30, 45, 60]); + + yield* Ref.set(failing, false); + yield* sweepMinutes(61, 75); + yield* Ref.set(failing, true); + readMinutes.length = 0; + yield* sweepMinutes(76, 80); + assert.deepStrictEqual(readMinutes, [76, 77, 79]); + }).pipe(Effect.provide(fixture.layer)); + }), + ), + ); + + it.effect("reads a backed-off link at once when a refresh asks, then keeps backing off", () => + Effect.scoped( + Effect.gen(function* () { + yield* TestClock.setTime(Date.parse(NOW)); + const fixture = yield* makeHarness({ + snapshot: makeSnapshot([makeThread("one", { pullRequests: [failingGitLabLink] })]), + summary: () => Effect.fail(gitLabCliFailure), + }); + yield* Effect.gen(function* () { + const reactor = yield* startAndSweep(fixture); + yield* sweepFor(fixture, reactor, 4); + const backedOffReads = (yield* Ref.get(fixture.summaryCalls)).length; + + yield* reactor.requestSync(failingGitLabLink); + yield* Queue.take(fixture.snapshotReads); + yield* reactor.drain; + assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, backedOffReads + 1); + + // The refresh failed at minute 4 with the backoff doubled to 8 minutes. + yield* sweepFor(fixture, reactor, 7); + assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, backedOffReads + 1); + yield* sweepAgain(fixture, reactor); + assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, backedOffReads + 2); + }).pipe(Effect.provide(fixture.layer)); + }), + ), + ); + it.effect.each([ "gh pr merge 7 --squash 2>&1 | tail -3", "cafe pr merge 7 --squash", diff --git a/apps/server/src/orchestration-v2/PullRequestSyncReactor.ts b/apps/server/src/orchestration-v2/PullRequestSyncReactor.ts index 96f0be4e59dc6..d962ee8f672d0 100644 --- a/apps/server/src/orchestration-v2/PullRequestSyncReactor.ts +++ b/apps/server/src/orchestration-v2/PullRequestSyncReactor.ts @@ -48,6 +48,8 @@ export const QUIET_SYNC_INTERVAL_MS = 5 * 60 * 1_000; // A pull request closed this long ago is rarely reopened, so the sweep stops reading it, as it // does merged ones. A reader that sees it open, or a requested refresh, still reads it at once. const CLOSED_FINAL_AFTER_MS = 24 * 60 * 60 * 1_000; +// A link whose host read failed waits a sweep, then twice as long after each further failure. +const FAILED_READ_BACKOFF_MS = 60 * 1_000; const isChangeRequestCloseCommand = (command: string) => sourceControlDefinitions.definitions.some( @@ -369,6 +371,7 @@ export const make = Effect.gen(function* () { // linking dozens of pull requests) is read together and shares the summary batches. let requestedSweepQueued = false; const retryStacks = new Set(); + const failedReads = new Map(); // Group keys the host answered not-found for, or this server cannot read (no CLI, signed out, // unsupported host); they are read on the slow cadence until a read succeeds. const missing = new Set(); @@ -376,6 +379,7 @@ export const make = Effect.gen(function* () { const pausedUntil = new Map(); const isDue = (key: string, entries: ReadonlyArray, nowMs: number): boolean => { + if ((failedReads.get(key)?.retryAtMs ?? 0) > nowMs) return false; if (requested.has(key)) return true; const active = entries.filter((entry) => isUnsettled(entry.thread)); if (active.length === 0) return false; @@ -425,12 +429,17 @@ export const make = Effect.gen(function* () { // A host-level refresh reaches every project, each retaining its own pending retry. for (const [key, entries] of groups) { - if (pendingRequests.has(threadPullRequestKeyOf(entries[0]!.link))) requested.add(key); + if (!pendingRequests.has(threadPullRequestKeyOf(entries[0]!.link))) continue; + requested.add(key); + // A new request reads at once; a failure after it backs off further, not from the start. + const failed = failedReads.get(key); + if (failed !== undefined) failedReads.set(key, { ...failed, retryAtMs: nowMs }); } pendingRequests.clear(); for (const key of lastSyncedAt.keys()) if (!groups.has(key)) lastSyncedAt.delete(key); for (const key of retryStacks) if (!groups.has(key)) retryStacks.delete(key); + for (const key of failedReads.keys()) if (!groups.has(key)) failedReads.delete(key); for (const key of missing) if (!groups.has(key)) missing.delete(key); for (const key of requested.keys()) if (!groups.has(key)) requested.delete(key); @@ -599,6 +608,7 @@ export const make = Effect.gen(function* () { } // The host answered, so the cadence clock ticks even if a dispatch below is rejected. lastSyncedAt.set(key, nowMs); + failedReads.delete(key); requested.delete(key); yield* Effect.forEach( entries, @@ -624,6 +634,13 @@ export const make = Effect.gen(function* () { const retryAt = rateLimitRetryAt(cause); if (retryAt !== undefined) { pausedUntil.set(pauseKey, Math.max(retryAt, pausedUntil.get(pauseKey) ?? 0)); + } else { + const previous = failedReads.get(key); + const backoffMs = + previous === undefined + ? FAILED_READ_BACKOFF_MS + : Math.min(previous.backoffMs * 2, SLOW_SYNC_INTERVAL_MS); + failedReads.set(key, { retryAtMs: nowMs + backoffMs, backoffMs }); } const reason = skipReason(cause); const skip = skips.get(reason); From c24255826be5aabb0f32ee18f6ac7dec0b3f1347 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 13:45:07 -0800 Subject: [PATCH 060/253] feat(web): dictation keeps recording into its thread when you switch away (#35761) Co-authored-by: Claude Opus 5.5 --- .../components/AppRouteModal.browser.test.tsx | 2 + apps/web/src/components/AppRouteModal.tsx | 2 + apps/web/src/components/ThreadRouteView.tsx | 6 +- apps/web/src/components/chat/ChatComposer.tsx | 30 +- .../components/chat/ComposerSpeechButton.tsx | 17 +- .../chat/GlobalVoiceInputIndicator.tsx | 52 +++ .../src/components/chat/backgroundThread.ts | 3 + .../VoiceSettingsPanel.browser.test.tsx | 1 + apps/web/src/composerDraftStore.ts | 21 + apps/web/src/routes/_chat.tsx | 2 + ...useEnvironmentSpeechInput.browser.test.tsx | 81 +++- .../src/speech/useEnvironmentSpeechInput.ts | 320 +++++-------- apps/web/src/speech/voiceInputSession.ts | 424 ++++++++++++++++++ 13 files changed, 755 insertions(+), 206 deletions(-) create mode 100644 apps/web/src/components/chat/GlobalVoiceInputIndicator.tsx create mode 100644 apps/web/src/speech/voiceInputSession.ts diff --git a/apps/web/src/components/AppRouteModal.browser.test.tsx b/apps/web/src/components/AppRouteModal.browser.test.tsx index 004e07744e22e..c0864d31c178f 100644 --- a/apps/web/src/components/AppRouteModal.browser.test.tsx +++ b/apps/web/src/components/AppRouteModal.browser.test.tsx @@ -5,6 +5,8 @@ import { render } from "../../test/render"; import { useCommitOnBlur } from "../hooks/useCommitOnBlur"; import { AppRouteModal } from "./AppRouteModal"; +vi.mock("./chat/GlobalVoiceInputIndicator", () => ({ GlobalVoiceInputIndicator: () => null })); + function CommitOnBlurField({ onCommit }: { onCommit: (next: string) => void }) { return ; } diff --git a/apps/web/src/components/AppRouteModal.tsx b/apps/web/src/components/AppRouteModal.tsx index 0c8a8a670d2f6..f4cde43a8cccc 100644 --- a/apps/web/src/components/AppRouteModal.tsx +++ b/apps/web/src/components/AppRouteModal.tsx @@ -4,6 +4,7 @@ import { useRef, useState, type CSSProperties, type ReactNode } from "react"; import { useNativeTitlebarHeight } from "../lib/windowControlsOverlay"; import { useCloseAppModal } from "./sidebar/mainAppLocation"; +import { GlobalVoiceInputIndicator } from "./chat/GlobalVoiceInputIndicator"; import { Button } from "./ui/button"; import { DialogBackdrop } from "./ui/dialog"; import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip"; @@ -89,6 +90,7 @@ export function AppRouteModal({ label, children }: { label: string; children: Re className="absolute inset-x-0 top-0 hidden h-(--app-modal-titlebar-height) [-webkit-app-region:drag] max-sm:block" /> {children} + diff --git a/apps/web/src/components/ThreadRouteView.tsx b/apps/web/src/components/ThreadRouteView.tsx index a7b7da0a6b53b..5217b068f9a35 100644 --- a/apps/web/src/components/ThreadRouteView.tsx +++ b/apps/web/src/components/ThreadRouteView.tsx @@ -8,7 +8,7 @@ import { Navigate, useNavigate } from "@tanstack/react-router"; import { useEffect, useState } from "react"; import ChatView from "./ChatView"; -import { BackgroundThreadContext } from "./chat/backgroundThread"; +import { BackgroundThreadContext, HiddenThreadContext } from "./chat/backgroundThread"; import { SidebarInset } from "./ui/sidebar"; import { useComposerDraftStore } from "../composerDraftStore"; import { useSidebarPendingFileDropStore } from "../sidebarPendingFileDropStore"; @@ -229,7 +229,9 @@ export function ThreadRouteView({ className="h-svh min-h-0 overflow-hidden overscroll-y-none md:h-dvh" inert={background} > - {view} + + {view} + {background ? null : redirect} ); diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index 8fb98edc14466..69250e47b89c3 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -4,7 +4,7 @@ import { useComposerTypingGuard } from "./useComposerTypingGuard"; import { insertAttachmentReferencesForDraft } from "./composerAttachmentReferences"; import type { ComposerAttachmentInsertion } from "../composerAttachmentInsertion"; import { DESKTOP_PASTE_AS_TEXT_EVENT } from "../../lib/desktopPasteAsText"; -import { BackgroundThreadContext } from "./backgroundThread"; +import { BackgroundThreadContext, HiddenThreadContext } from "./backgroundThread"; import { resolveCompatibleRuntimeMode, runtimeModeConfig, @@ -107,9 +107,12 @@ import { composerFileNeedsReattach, composerDraftHasUserContent, composerTargetKey, + readComposerDraftPrompt, + resolveLiveComposerDraftTarget, useComposerDraftStore, useComposerThreadDraft, } from "../../composerDraftStore"; +import { buildDraftThreadRouteParams, buildThreadRouteParams } from "../../threadRoutes"; import { ComposerStashBadge } from "./ComposerStashBadge"; import { ComposerStashMenu } from "./ComposerStashMenu"; import { useComposerTriggerState } from "./useComposerTriggerState"; @@ -1052,6 +1055,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) }); const backgroundThread = use(BackgroundThreadContext); + const hiddenThread = use(HiddenThreadContext); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to keydown, window blur and the desktop paste-as-text event to arm plain-text paste useEffect(() => { // A background composer listens on its own form, so it never hears the main composer's keys. @@ -1846,6 +1850,29 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) projectName, ownerKey: questionAttachmentTarget ?? composerDraftTargetKey, disabled: dictationDisabled, + label: activeThread?.title ?? null, + presented: !hiddenThread, + // A question's answer lives only in this composer, so only the thread draft outlives it. + persistentDraft: questionAttachmentTarget + ? undefined + : { + readStoredText: () => readComposerDraftPrompt(composerDraftTarget), + commitStoredText: (text) => { + const live = resolveLiveComposerDraftTarget(composerDraftTarget); + if (live !== null) setComposerDraftPrompt(live, text); + }, + // The route, not the draft target: a queued-message edit has a draft id but no route. + open: () => + void (routeKind === "draft" && typeof composerDraftTarget === "string" + ? navigate({ + to: "/draft/$draftId", + params: buildDraftThreadRouteParams(composerDraftTarget), + }) + : navigate({ + to: "/$environmentId/$threadId", + params: buildThreadRouteParams(routeThreadRef), + })), + }, readDraft: () => { const snapshot = readComposerSnapshot(); return { @@ -3828,6 +3855,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) finishShortcutLabel={dictationShortcutLabel} cancelShortcutLabel="Esc" level={speechInput.level} + recordingStartedAt={speechInput.recordingStartedAt} disabled={dictationDisabled} onStart={() => void speechInput.start()} onStop={() => void speechInput.stop()} diff --git a/apps/web/src/components/chat/ComposerSpeechButton.tsx b/apps/web/src/components/chat/ComposerSpeechButton.tsx index ca9ef2a278ad6..9f7eb4f37ab74 100644 --- a/apps/web/src/components/chat/ComposerSpeechButton.tsx +++ b/apps/web/src/components/chat/ComposerSpeechButton.tsx @@ -43,18 +43,25 @@ function formatElapsed(seconds: number) { return `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, "0")}`; } -function RecordingStatus(props: { level: number; recordingLimitSeconds?: number | undefined }) { - const [elapsedSeconds, setElapsedSeconds] = useState(0); +function RecordingStatus(props: { + level: number; + recordingStartedAt?: number | null | undefined; + recordingLimitSeconds?: number | undefined; +}) { + const { recordingStartedAt } = props; + const [elapsedSeconds, setElapsedSeconds] = useState(() => + recordingStartedAt ? Math.max(0, Math.floor((Date.now() - recordingStartedAt) / 1_000)) : 0, + ); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- owns the timer displaying the native recording duration useEffect(() => { - const startedAt = Date.now(); + const startedAt = recordingStartedAt ?? Date.now(); const timer = window.setInterval( () => setElapsedSeconds(Math.floor((Date.now() - startedAt) / 1_000)), 250, ); return () => window.clearInterval(timer); - }, []); + }, [recordingStartedAt]); return ( <> @@ -125,6 +132,7 @@ export function ComposerSpeechRecordingPill(props: { finishShortcutLabel?: string | null; cancelShortcutLabel?: string | null; level: number; + recordingStartedAt?: number | null | undefined; recordingLimitSeconds?: number | undefined; /** Compact matches the `xs` buttons beside text-field editors. */ size?: "default" | "compact"; @@ -181,6 +189,7 @@ export function ComposerSpeechRecordingPill(props: { {recording ? ( ) : ( diff --git a/apps/web/src/components/chat/GlobalVoiceInputIndicator.tsx b/apps/web/src/components/chat/GlobalVoiceInputIndicator.tsx new file mode 100644 index 0000000000000..3bc724fe1962a --- /dev/null +++ b/apps/web/src/components/chat/GlobalVoiceInputIndicator.tsx @@ -0,0 +1,52 @@ +import { useVoiceTranscriptionDestination } from "../../speech/useEnvironmentSpeechInput"; +import { + useRetainedVoiceInputSession, + useVoiceInputSessionActivity, + useVoiceInputSessionSnapshot, + useVoiceOwnerPresented, +} from "../../speech/voiceInputSession"; +import { Button } from "../ui/button"; +import { ComposerSpeechRecordingPill } from "./ComposerSpeechButton"; + +/** + * Keeps the chosen destination's dictation alive across navigation and, while the draft it + * records into is off screen, shows it so it can be finished, cancelled, or returned to. + */ +export function GlobalVoiceInputIndicator({ hidden = false }: { hidden?: boolean }) { + const { prepared, grantId, cloudProvider, cloudModel } = useVoiceTranscriptionDestination(); + const session = useRetainedVoiceInputSession({ prepared, grantId, cloudProvider, cloudModel }); + const { state, target, recordingStartedAt } = useVoiceInputSessionSnapshot(session); + const presented = useVoiceOwnerPresented(target?.ownerKey ?? null); + const { level } = useVoiceInputSessionActivity( + hidden || presented ? null : session, + target?.ownerKey ?? null, + ); + if (hidden || !session || !target || state.phase === "idle" || presented) return null; + + return ( +
+
+ + void session.start(target)} + onStop={() => void session.stop()} + onCancel={() => session.cancel()} + onSkipPostProcessing={() => session.skipPostProcessing()} + /> +
+
+ ); +} diff --git a/apps/web/src/components/chat/backgroundThread.ts b/apps/web/src/components/chat/backgroundThread.ts index b3dfee09a663c..e402bc5e62165 100644 --- a/apps/web/src/components/chat/backgroundThread.ts +++ b/apps/web/src/components/chat/backgroundThread.ts @@ -5,3 +5,6 @@ import { createContext } from "react"; * parent's panel; its global shortcuts and listeners stand down. */ export const BackgroundThreadContext = createContext(false); + +/** True only for a thread kept mounted out of sight under Settings or Usage. */ +export const HiddenThreadContext = createContext(false); diff --git a/apps/web/src/components/settings/VoiceSettingsPanel.browser.test.tsx b/apps/web/src/components/settings/VoiceSettingsPanel.browser.test.tsx index 4c8172333ff98..c0107483461cc 100644 --- a/apps/web/src/components/settings/VoiceSettingsPanel.browser.test.tsx +++ b/apps/web/src/components/settings/VoiceSettingsPanel.browser.test.tsx @@ -328,6 +328,7 @@ it("tests the selected model after download and preserves its transcript across state: { phase: "idle", error: null, errorAction: null }, preview: null, level: 0, + recordingStartedAt: null, preparing: false, blocksSubmission: false, freezesEditor: false, diff --git a/apps/web/src/composerDraftStore.ts b/apps/web/src/composerDraftStore.ts index 4066715c53b2c..3c64aa0f65817 100644 --- a/apps/web/src/composerDraftStore.ts +++ b/apps/web/src/composerDraftStore.ts @@ -1660,6 +1660,27 @@ function getComposerDraftState( return state.draftsByThreadKey[threadKey] ?? null; } +/** Where a draft lives now: its thread once promoted, or null once it was discarded. */ +export function resolveLiveComposerDraftTarget( + target: ComposerThreadTarget, +): ComposerThreadTarget | null { + if (typeof target !== "string") return target; + const state = composerDraftStore.getState(); + const promoted = state.promotedDraftRedirects[target]; + if (promoted) return promoted; + return state.draftThreadsByThreadKey[target] || state.draftsByThreadKey[target] ? target : null; +} + +/** The stored prompt for a target, empty when it has no draft, or null when the target is gone. */ +export function readComposerDraftPrompt(target: ComposerThreadTarget): string | null { + const live = resolveLiveComposerDraftTarget(target); + if (live === null) return null; + const state = composerDraftStore.getState(); + const threadKey = resolveComposerDraftKey(state, live); + if (!threadKey) return null; + return state.draftsByThreadKey[threadKey]?.prompt ?? ""; +} + function draftThreadHasOpenPreview(draftThread: DraftThreadState | undefined): boolean { if (draftThread === undefined) return false; const ref = scopeThreadRef(draftThread.environmentId, draftThread.threadId); diff --git a/apps/web/src/routes/_chat.tsx b/apps/web/src/routes/_chat.tsx index ad04d04d29493..c65ed390288f8 100644 --- a/apps/web/src/routes/_chat.tsx +++ b/apps/web/src/routes/_chat.tsx @@ -1,6 +1,7 @@ import { Outlet, createFileRoute, redirect } from "@tanstack/react-router"; import { ThreadRouteView } from "../components/ThreadRouteView"; +import { GlobalVoiceInputIndicator } from "../components/chat/GlobalVoiceInputIndicator"; import { isAppModalPage } from "../components/sidebar/mainAppLocation"; import { SidebarInset } from "../components/ui/sidebar"; import { resolveThreadRouteTarget } from "../threadRoutes"; @@ -24,6 +25,7 @@ function ChatRouteLayout() { )} {modalOpen ? : null} +