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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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/174] 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