From 7d9c365e173f2fcbe9e47feadc98c565370520e0 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 15 Nov 2026 10:40:17 +0100 Subject: [PATCH 001/124] fix(web): keep distinct images with matching attachment metadata (#37116) Co-authored-by: Julius Marminge --- .../src/state/use-composer-drafts.test.ts | 20 +++++++ apps/mobile/src/state/use-composer-drafts.ts | 17 ++++-- .../web/src/components/ChatView.logic.test.ts | 59 ++++++++++++++++++- apps/web/src/components/ChatView.logic.ts | 7 ++- .../chat/composerAttachmentFiles.ts | 56 +++++++++++++++++- .../chat/useComposerAttachmentIntake.ts | 2 + .../chat/useComposerContextImport.ts | 4 +- .../src/components/chat/useComposerStash.ts | 27 ++++----- .../SnapShotCoordinator.browser.test.ts | 9 ++- .../desktop/SnapShotCoordinator.tsx | 2 + .../src/components/preview/PreviewView.tsx | 2 + apps/web/src/composerDraftStore.test.ts | 36 ++++++----- apps/web/src/composerDraftStore.ts | 10 +++- ...same-size-pasted-images-both-attach.e2e.ts | 44 ++++++++++++++ 14 files changed, 244 insertions(+), 51 deletions(-) create mode 100644 scripts/scenarios/distinct-same-size-pasted-images-both-attach.e2e.ts diff --git a/apps/mobile/src/state/use-composer-drafts.test.ts b/apps/mobile/src/state/use-composer-drafts.test.ts index adb8eff775bdd..200b2b165c6b5 100644 --- a/apps/mobile/src/state/use-composer-drafts.test.ts +++ b/apps/mobile/src/state/use-composer-drafts.test.ts @@ -1120,6 +1120,26 @@ describe("mobile composer drafts", () => { appendComposerDraftAttachments(draftKey, [makeVideo("restored")], { restore: true }); expect(appAtomRegistry.get(composerDraftsAtom)[draftKey]?.attachments).toHaveLength(2); + + // Every native paste is named pasted-image.png, so only the bytes tell two pastes apart. + const makePaste = (id: string, base64: string) => ({ + id, + type: "image" as const, + name: "pasted-image.png", + mimeType: "image/png", + sizeBytes: 3, + dataUrl: `data:image/png;base64,${base64}`, + previewUri: `data:image/png;base64,${base64}`, + }); + const pasteKey = "new-task:environment-1:project-pastes"; + appendComposerDraftAttachments(pasteKey, [makePaste("red", "AQID")]); + appendComposerDraftAttachments(pasteKey, [ + makePaste("blue", "BAUG"), + makePaste("red-2", "AQID"), + ]); + expect( + appAtomRegistry.get(composerDraftsAtom)[pasteKey]?.attachments.map(({ id }) => id), + ).toEqual(["red", "blue"]); }); it("keeps shared attachment files until every draft releases them", async () => { diff --git a/apps/mobile/src/state/use-composer-drafts.ts b/apps/mobile/src/state/use-composer-drafts.ts index 643d348093208..0306d5e7db5ea 100644 --- a/apps/mobile/src/state/use-composer-drafts.ts +++ b/apps/mobile/src/state/use-composer-drafts.ts @@ -1412,8 +1412,14 @@ export function appendComposerDraftText(draftKey: string, value: string): void { }); } -function attachmentDedupKey(attachment: DraftComposerAttachment): string { - return `${attachment.mimeType}\u0000${attachment.sizeBytes}\u0000${attachment.name}`; +// Pasted images all share one name, so an image's inline bytes decide whether it is a repeat. +function isSameAttachment(a: DraftComposerAttachment, b: DraftComposerAttachment): boolean { + return ( + a.mimeType === b.mimeType && + a.sizeBytes === b.sizeBytes && + a.name === b.name && + (a.type !== "image" || b.type !== "image" || a.dataUrl === b.dataUrl) + ); } /** @@ -1441,13 +1447,12 @@ export function appendComposerDraftAttachments( const retained = options?.appendReference ? draftWithoutInsertionSelection(draftKey, existing, options.insertion) : existing; - const keys = new Set(retained.attachments.map(attachmentDedupKey)); + const seen = [...retained.attachments]; const attachments = options?.restore ? incoming : incoming.filter((attachment) => { - const key = attachmentDedupKey(attachment); - if (keys.has(key)) return false; - keys.add(key); + if (seen.some((other) => isSameAttachment(other, attachment))) return false; + seen.push(attachment); return true; }); duplicates = incoming.filter((attachment) => !attachments.includes(attachment)); diff --git a/apps/web/src/components/ChatView.logic.test.ts b/apps/web/src/components/ChatView.logic.test.ts index 54174262fd70f..d9877b83c01b1 100644 --- a/apps/web/src/components/ChatView.logic.test.ts +++ b/apps/web/src/components/ChatView.logic.test.ts @@ -1,4 +1,5 @@ import { + prepareRevertedMessageAttachments, boundAttachmentIds, buildExpiredTerminalContextToastCopy, mergeDirectPreviewAnnotation, @@ -40,11 +41,12 @@ import { import * as DateTime from "effect/DateTime"; import * as Option from "effect/Option"; import { afterEach, describe, expect, it, vi } from "vite-plus/test"; -import { Atom } from "effect/reactivity"; +import { AsyncResult, Atom } from "effect/reactivity"; import { appAtomRegistry } from "../rpc/atomRegistry"; import { environmentThreadDetails } from "../state/threads"; import type { Thread } from "../types"; +import { DraftId, hydrateImagesFromPersisted, useComposerDraftStore } from "../composerDraftStore"; import { derivePhase } from "../session-logic"; import { makeThreadFixture, makeThreadProjectionFixture } from "../test-fixtures"; import { @@ -1499,3 +1501,58 @@ describe("deriveThreadPendingBackgroundTasks", () => { ).toEqual([{ taskId: "task-1", kind: "background_task" }]); }); }); + +describe("prepareRevertedMessageAttachments", () => { + it("suppresses a repeated image after restoring a rewound message", async () => { + using _fetch = vi + .spyOn(globalThis, "fetch") + .mockResolvedValue(new Response(new Uint8Array([1, 2, 3]))); + const [file] = await prepareRevertedMessageAttachments({ + message: { + id: MessageId.make("rewound"), + role: "user", + text: "", + runId: null, + streaming: false, + createdAt: now, + updatedAt: now, + attachments: [ + { type: "image", id: "restored", name: "image.png", mimeType: "image/png", sizeBytes: 3 }, + ], + }, + environmentId: EnvironmentId.make("rewind-images"), + httpBaseUrl: "http://localhost", + createAssetUrl: async () => + AsyncResult.success({ relativeUrl: "/attachment/image.png", expiresAt: 0 }), + }); + const target = DraftId.make("rewind-image-repeat"); + const store = useComposerDraftStore.getState(); + using _draft = { [Symbol.dispose]: () => store.clearComposerContent(target) }; + store.addImages( + target, + [ + { + type: "image", + id: "restored", + name: file!.name, + mimeType: file!.type, + sizeBytes: file!.size, + file: file!, + previewUrl: "data:image/png;base64,AQID", + }, + ], + { allowDuplicates: true }, + ); + const repeat = hydrateImagesFromPersisted([ + { + id: "repeat", + name: "image.png", + mimeType: "image/png", + sizeBytes: 3, + dataUrl: "data:image/png;base64,AQID", + }, + ]); + expect(store.addImages(target, repeat)).toEqual([]); + expect(store.getComposerDraft(target)?.images.map(({ id }) => id)).toEqual(["restored"]); + }); +}); diff --git a/apps/web/src/components/ChatView.logic.ts b/apps/web/src/components/ChatView.logic.ts index 927a253ead2b8..057bd52d282fe 100644 --- a/apps/web/src/components/ChatView.logic.ts +++ b/apps/web/src/components/ChatView.logic.ts @@ -52,6 +52,7 @@ import { squashAtomCommandFailure, type AtomCommandResult, } from "@t3tools/client-runtime/state/runtime"; +import { readComposerImageBytes } from "./chat/composerAttachmentFiles"; import { videoMimeType } from "@t3tools/shared/video"; import { appendCodexArtifactTemplateUsePrompt, @@ -653,7 +654,11 @@ export async function prepareRevertedMessageAttachments(input: { if (url === null) throw new Error("The environment returned an invalid attachment URL."); const response = await fetch(url, { signal: AbortSignal.timeout(30_000) }); if (!response.ok) throw new Error(`Could not restore attachment: ${attachment.name}`); - return new File([await response.blob()], attachment.name, { type: attachment.mimeType }); + const file = new File([await response.blob()], attachment.name, { + type: attachment.mimeType, + }); + if (attachment.type === "image") await readComposerImageBytes(file); + return file; }), ); } diff --git a/apps/web/src/components/chat/composerAttachmentFiles.ts b/apps/web/src/components/chat/composerAttachmentFiles.ts index f613b695ffd51..293d517564189 100644 --- a/apps/web/src/components/chat/composerAttachmentFiles.ts +++ b/apps/web/src/components/chat/composerAttachmentFiles.ts @@ -61,21 +61,71 @@ export function normalizeComposerImageFileMimeType(file: File): File { }); } -export function composerImageDedupKey( +function composerImageSignature( image: Pick, ): string { // Hydrated images get fresh lastModified values, so exclude it from dedupe. return `${image.mimeType}\u0000${image.sizeBytes}\u0000${image.name}`; } +// Byte hashes by file; a file whose bytes were never read gets a key that matches only itself. +const composerImageFingerprints = new WeakMap(); +let unreadComposerImageCount = 0; + +function composerImageFingerprint(file: File): string { + let fingerprint = composerImageFingerprints.get(file); + if (fingerprint === undefined) { + fingerprint = `unread:${unreadComposerImageCount++}`; + composerImageFingerprints.set(file, fingerprint); + } + return fingerprint; +} + +/** Records the bytes behind an image file, so a draft drops it only as a byte-for-byte repeat. */ +export function rememberComposerImageBytes(file: File, bytes: Uint8Array): void { + // cyrb53, synchronous: crypto.subtle is missing on plain-http remote origins. + let first = 0xdeadbeef; + let second = 0x41c6ce57; + for (let index = 0; index < bytes.length; index++) { + const byte = bytes[index]!; + first = Math.imul(first ^ byte, 2654435761); + second = Math.imul(second ^ byte, 1597334677); + } + first = Math.imul(first ^ (first >>> 16), 2246822507); + first ^= Math.imul(second ^ (second >>> 13), 3266489909); + second = Math.imul(second ^ (second >>> 16), 2246822507); + second ^= Math.imul(first ^ (first >>> 13), 3266489909); + composerImageFingerprints.set(file, `${second >>> 0}:${first >>> 0}`); +} + +export async function readComposerImageBytes(file: File): Promise { + // An unreadable file keeps matching only itself. + const bytes = await file.arrayBuffer().catch(() => null); + if (bytes) rememberComposerImageBytes(file, new Uint8Array(bytes)); +} + +/** + * Matches name, type, size and bytes; unread files match only themselves. + * Producers must await readComposerImageBytes before publishing images to the draft. + */ +export function composerImageDedupKey( + image: Pick, +): string { + return `${composerImageSignature(image)}\u0000${composerImageFingerprint(image.file)}`; +} + // Compression changes an image's bytes, so a compressed image answers for the file it came from. const composerImageSourceKeys = new WeakMap(); -/** The draft store's image dedupe key, taken from the file as pasted rather than as compressed. */ +/** A pasted image's name, type and size: a guess, before its bytes are read, that the draft has it. */ export function composerImageSourceKey(file: File): string { return ( composerImageSourceKeys.get(file) ?? - composerImageDedupKey({ mimeType: file.type, sizeBytes: file.size, name: file.name || "image" }) + composerImageSignature({ + mimeType: file.type, + sizeBytes: file.size, + name: file.name || "image", + }) ); } diff --git a/apps/web/src/components/chat/useComposerAttachmentIntake.ts b/apps/web/src/components/chat/useComposerAttachmentIntake.ts index 9bd8d1293e617..481846a1fe30b 100644 --- a/apps/web/src/components/chat/useComposerAttachmentIntake.ts +++ b/apps/web/src/components/chat/useComposerAttachmentIntake.ts @@ -29,6 +29,7 @@ import { classifyComposerAttachmentFile, composerFileAttachmentMimeType, composerImageSourceKey, + readComposerImageBytes, normalizeComposerImageFileMimeType, rememberComposerImageSource, } from "./composerAttachmentFiles"; @@ -290,6 +291,7 @@ export function useComposerAttachmentIntake(input: { continue; } await inheritComposerImageThumbnail(file, result.file); + await readComposerImageBytes(result.file); rememberComposerImageSource(file, result.file); compressed.push({ id, file: result.file }); } diff --git a/apps/web/src/components/chat/useComposerContextImport.ts b/apps/web/src/components/chat/useComposerContextImport.ts index bf23b05d3ab84..0cc66d6a71d19 100644 --- a/apps/web/src/components/chat/useComposerContextImport.ts +++ b/apps/web/src/components/chat/useComposerContextImport.ts @@ -43,6 +43,7 @@ import { collectInlineContextIds } from "../../lib/composerContextReferences"; import { elementContextToPreviewAnnotation } from "../../lib/elementContext"; import { orchestrationEnvironment } from "~/state/orchestration"; import { pendingDraftWork } from "./pendingDraftWork"; +import { readComposerImageBytes } from "./composerAttachmentFiles"; import { randomUUID } from "~/lib/utils"; import { readPreparedConnection } from "~/state/session"; import { resolveAssetUrl } from "~/assets/assetUrls"; @@ -194,6 +195,8 @@ export function useComposerContextImport(input: { fail("Downloading it from the source failed."); return; } + file = new File([blob], record.name, { type: record.mimeType || blob.type }); + if (record.kind === "image") await readComposerImageBytes(file); // Question attachments have no inline reference; annotations can own their screenshots. const references = new Set(collectInlineContextIds(promptRef.current)); if ( @@ -208,7 +211,6 @@ export function useComposerContextImport(input: { ) ) return; - file = new File([blob], record.name, { type: record.mimeType || blob.type }); } // The draft these bytes belong to may have been sent or switched away from while they // downloaded. Dropping them here keeps them out of whatever draft is open now. diff --git a/apps/web/src/components/chat/useComposerStash.ts b/apps/web/src/components/chat/useComposerStash.ts index 65b635acc1df6..f89a51a2f8c99 100644 --- a/apps/web/src/components/chat/useComposerStash.ts +++ b/apps/web/src/components/chat/useComposerStash.ts @@ -49,6 +49,7 @@ import { verifyStashedAttachmentUpload, } from "../../lib/attachmentUploadQueue"; import { remainingComposerAttachmentSlots } from "./composerThreadImport"; +import { composerImageDedupKey } from "./composerAttachmentFiles"; import { replaceComposerContextReferences } from "@t3tools/shared/composerContextReferences"; import { resizeSnapShotSource } from "../../lib/snapShotSource"; import { registerCommands } from "../../shortcuts/dispatcher"; @@ -387,28 +388,20 @@ export function useComposerStash(input: { if (entry.attachments.length > 0) { const attachmentDraft = readAttachmentDraft(); const existingIds = new Set(attachmentDraft.images.map((image) => image.id)); - // The draft store also dedupes by mimeType+sizeBytes+name, so filter - // on the same key here. Counting a duplicate against capacity would - // burn a slot the store then refuses to fill, pushing a genuinely - // unique image into the overflow list for nothing. - const existingDedupKeys = new Set( - attachmentDraft.images.map( - (image) => `${image.mimeType}\0${image.sizeBytes}\0${image.name}`, - ), - ); + // Filter on the draft store's dedupe key here. Counting a duplicate + // against capacity would burn a slot the store then refuses to fill, + // pushing a genuinely unique image into the overflow list for nothing. + const existingDedupKeys = new Set(attachmentDraft.images.map(composerImageDedupKey)); const capacity = remainingComposerAttachmentSlots( attachmentDraft.images.length + attachmentDraft.files.length + restoredFileCount, pendingThreadImportsRef.current.get(composerTargetKey(composerDraftTarget)) ?? 0, ); - const pending = entry.attachments.filter( - (attachment) => - !existingIds.has(attachment.id) && - !existingDedupKeys.has( - `${attachment.mimeType}\0${attachment.sizeBytes}\0${attachment.name}`, - ), + const pending = hydrateImagesFromPersisted(entry.attachments).filter( + (image) => + !existingIds.has(image.id) && !existingDedupKeys.has(composerImageDedupKey(image)), ); - unrestoredImageNames = pending.slice(capacity).map((attachment) => attachment.name); - const restoredImages = hydrateImagesFromPersisted(pending.slice(0, capacity)); + unrestoredImageNames = pending.slice(capacity).map((image) => image.name); + const restoredImages = pending.slice(0, capacity); if (restoredImages.length > 0) { addComposerDraftImages(composerDraftTarget, restoredImages); } diff --git a/apps/web/src/components/desktop/SnapShotCoordinator.browser.test.ts b/apps/web/src/components/desktop/SnapShotCoordinator.browser.test.ts index a504d429185af..0915ed9df3b53 100644 --- a/apps/web/src/components/desktop/SnapShotCoordinator.browser.test.ts +++ b/apps/web/src/components/desktop/SnapShotCoordinator.browser.test.ts @@ -2,7 +2,11 @@ import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime/environ import { type DesktopBridge, EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts"; import { describe, expect, it, vi } from "vite-plus/test"; -import { DraftId, useComposerDraftStore } from "../../composerDraftStore"; +import { + DraftId, + hydrateImagesFromPersisted, + useComposerDraftStore, +} from "../../composerDraftStore"; import * as imageCompression from "../../lib/imageCompression"; import { questionAttachmentDraftId, @@ -142,6 +146,9 @@ describe("open question delivery", () => { expect(store.getComposerDraft(first)?.images.map(({ id }) => id)).toEqual([capture.id]); expect(store.getComposerDraft(threadRef)?.images ?? []).toHaveLength(0); expect(acknowledgeSnapShot).toHaveBeenCalledExactlyOnceWith(capture.id); + const repeat = hydrateImagesFromPersisted([{ ...capture, id: "repeat" }]); + expect(store.addImages(first, repeat)).toEqual([]); + expect(store.getComposerDraft(first)?.images.map(({ id }) => id)).toEqual([capture.id]); } finally { untrack(); } diff --git a/apps/web/src/components/desktop/SnapShotCoordinator.tsx b/apps/web/src/components/desktop/SnapShotCoordinator.tsx index 9150116af1f94..a75a32fc50369 100644 --- a/apps/web/src/components/desktop/SnapShotCoordinator.tsx +++ b/apps/web/src/components/desktop/SnapShotCoordinator.tsx @@ -41,6 +41,7 @@ import { openQuestionAttachmentDraft, } from "../../questionAttachments"; import { stackedThreadToast, toastManager } from "../ui/toast"; +import { readComposerImageBytes } from "../chat/composerAttachmentFiles"; type CaptureTarget = DraftId | ScopedThreadRef; type CaptureFailure = "missing-project" | "delivery"; @@ -173,6 +174,7 @@ export async function deliverSnapShot( throw new Error("The captured window is too large to attach."); } const file = compressed.file; + await readComposerImageBytes(file); const source = resizeSnapShotSource(capture.source, compressed.imageSize); const dataUrl = compressed.recompressed ? await readFileAsDataUrl(file) : capture.dataUrl; const target = resolveTarget(); diff --git a/apps/web/src/components/preview/PreviewView.tsx b/apps/web/src/components/preview/PreviewView.tsx index 02d856d78ca61..e303ae1451619 100644 --- a/apps/web/src/components/preview/PreviewView.tsx +++ b/apps/web/src/components/preview/PreviewView.tsx @@ -35,6 +35,7 @@ import { } from "~/browserHistoryStore"; import { type ComposerImageAttachment, useComposerDraftStore } from "~/composerDraftStore"; import { capturePreviewAnnotationScreenshot } from "~/lib/previewAnnotation"; +import { readComposerImageBytes } from "../chat/composerAttachmentFiles"; import { ensureLocalApi } from "~/localApi"; import { getClientSettings } from "~/hooks/useSettings"; import { @@ -859,6 +860,7 @@ export function PreviewView({ const capture = capturePreviewAnnotationScreenshot(picked); const cropDropped = screenshotFailed || capture.status === "failed"; const annotation = capture.status === "failed" ? { ...picked, screenshot: null } : picked; + if (capture.status === "captured") await readComposerImageBytes(capture.file); addPreviewAnnotation(threadRef, annotation); if (cropDropped) { toastManager.add( diff --git a/apps/web/src/composerDraftStore.test.ts b/apps/web/src/composerDraftStore.test.ts index 43d61924a8c74..a04d00cbbc2de 100644 --- a/apps/web/src/composerDraftStore.test.ts +++ b/apps/web/src/composerDraftStore.test.ts @@ -92,6 +92,7 @@ import { type ComposerFileAttachment, type ComposerImageAttachment, composerFileNeedsReattach, + hydrateImagesFromPersisted, partializeComposerDraftStoreState, useComposerDraftStore, DraftId, @@ -261,29 +262,28 @@ describe("composerDraftStore addImages", () => { expect(revokeSpy).not.toHaveBeenCalled(); }); - it("deduplicates against existing images across calls by file signature", () => { - const first = makeImage({ - id: "img-a", - previewUrl: "blob:a", + it("deduplicates byte-identical images across calls and keeps distinct bytes", () => { + // Hydrated copies get fresh lastModified values, like a restored stash or draft. + const persisted = (id: string, fill: number) => ({ + id, name: "same.png", mimeType: "image/png", sizeBytes: 9, - lastModified: 777, - }); - const duplicateLater = makeImage({ - id: "img-b", - previewUrl: "blob:b", - name: "same.png", - mimeType: "image/png", - sizeBytes: 9, - lastModified: 999, + dataUrl: `data:image/png;base64,${btoa(String.fromCharCode(...new Uint8Array(9).fill(fill)))}`, }); + const [first, repeat, different] = hydrateImagesFromPersisted([ + persisted("img-a", 1), + persisted("img-b", 1), + persisted("img-c", 2), + ]); + const store = useComposerDraftStore.getState(); - expect(useComposerDraftStore.getState().addImage(threadRef, first)).toBe(true); - expect(useComposerDraftStore.getState().addImage(threadRef, duplicateLater)).toBe(false); + expect(store.addImage(threadRef, first!)).toBe(true); + expect(store.addImage(threadRef, { ...repeat!, previewUrl: "blob:b" })).toBe(false); + expect(store.addImage(threadRef, different!)).toBe(true); const draft = draftFor(threadId, TEST_ENVIRONMENT_ID); - expect(draft?.images.map((image) => image.id)).toEqual(["img-a"]); + expect(draft?.images.map((image) => image.id)).toEqual(["img-a", "img-c"]); expect(revokeSpy).toHaveBeenCalledWith("blob:b"); }); }); @@ -2674,9 +2674,7 @@ describe("composerDraftStore attachment references", () => { const image = makeImage({ id: "img-1", previewUrl: "blob:img-1", name: "shot.png" }); expect(store.addImages(threadRef, [image])).toEqual(["img-1"]); expect( - store.addImages(threadRef, [ - makeImage({ id: "img-2", previewUrl: "blob:img-2", name: "shot.png" }), - ]), + store.addImages(threadRef, [{ ...image, id: "img-2", previewUrl: "blob:img-2" }]), ).toEqual([]); }); diff --git a/apps/web/src/composerDraftStore.ts b/apps/web/src/composerDraftStore.ts index e45d2facbdf78..4066715c53b2c 100644 --- a/apps/web/src/composerDraftStore.ts +++ b/apps/web/src/composerDraftStore.ts @@ -1,6 +1,7 @@ import { composerFileAttachmentMimeType, composerImageDedupKey, + rememberComposerImageBytes, } from "./components/chat/composerAttachmentFiles"; import { elementContextToPreviewAnnotation } from "./lib/elementContext"; import { @@ -2628,16 +2629,21 @@ function hydratePersistedComposerImageAttachment( header.startsWith("data:") && header.includes(";") ? header.slice("data:".length, header.indexOf(";")) : attachment.mimeType; - return new File([decodedText], attachment.name, { + const textBytes = new TextEncoder().encode(decodedText); + const textFile = new File([textBytes], attachment.name, { type: inferredMimeType || attachment.mimeType, }); + rememberComposerImageBytes(textFile, textBytes); + return textFile; } const binary = atob(payload); const bytes = new Uint8Array(binary.length); for (let index = 0; index < binary.length; index += 1) { bytes[index] = binary.charCodeAt(index); } - return new File([bytes], attachment.name, { type: attachment.mimeType }); + const file = new File([bytes], attachment.name, { type: attachment.mimeType }); + rememberComposerImageBytes(file, bytes); + return file; } catch { return null; } diff --git a/scripts/scenarios/distinct-same-size-pasted-images-both-attach.e2e.ts b/scripts/scenarios/distinct-same-size-pasted-images-both-attach.e2e.ts new file mode 100644 index 0000000000000..1f1931aa5bfde --- /dev/null +++ b/scripts/scenarios/distinct-same-size-pasted-images-both-attach.e2e.ts @@ -0,0 +1,44 @@ +import { expect } from "e2e"; +import type { Browser } from "@e2e-dev/web"; + +import { test, WEB } from "./t3.ts"; + +// Two 1x1 PNGs, one red and one blue, that encode to the same number of bytes. +const RED_PIXEL = + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAD0lEQVR4AQEEAPv/AP8AAAMBAQCNHeWCAAAAAElFTkSuQmCC"; +const BLUE_PIXEL = + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAD0lEQVR4AQEEAPv/AAAA/wEDAQB8wnTbAAAAAElFTkSuQmCC"; + +/** Pastes a PNG the way a clipboard image arrives: a new File named image.png. */ +const pastePng = (browser: Browser, base64: string) => + browser.evaluate((payload: string) => { + const bytes = Uint8Array.from(atob(payload), (char) => char.charCodeAt(0)); + const clipboardData = new DataTransfer(); + clipboardData.items.add(new File([bytes], "image.png", { type: "image/png" })); + const editor = document.querySelector('[contenteditable="true"]'); + if (!editor) throw new Error("No composer editor to paste into"); + return editor.dispatchEvent( + new ClipboardEvent("paste", { clipboardData, bubbles: true, cancelable: true }), + ); + }, base64); + +test( + "Two different pasted images with the same name, type and size both attach", + WEB, + async ({ t3, app, browser, screen }) => { + const project = await t3.project("demo"); + const thread = await t3.thread({ project, title: "Screenshots", messages: ["first"] }); + await t3.open(thread); + await expect(screen.getByText("first fixture turn complete", { exact: false })).toBeVisible(); + + const composer = screen.getByRole("textbox"); + await composer.fill("Compare "); + const chips = composer.getByRole("button", "Image attachment", { exact: false }); + + await pastePng(browser, RED_PIXEL); + await expect(chips).toHaveCount(1); + await pastePng(browser, BLUE_PIXEL); + await app.screenshot("after-second-paste"); + await expect(chips).toHaveCount(2); + }, +); From 8dba23e4206083c3736654bcd18996ae38e730f9 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 15 Nov 2026 10:41:03 +0100 Subject: [PATCH 002/124] fix(web): large terminal pastes reach the program intact (#37103) Co-authored-by: Julius Marminge --- .../terminal/ThreadTerminalRouteScreen.tsx | 25 ++- .../features/terminal/terminalInput.test.ts | 27 +-- .../src/features/terminal/terminalInput.ts | 18 -- .../features/terminal/terminalPaste.test.ts | 11 +- .../src/features/terminal/terminalPaste.ts | 11 +- .../ThreadTerminalDrawer.browser.test.tsx | 146 ++++++++++++++- .../src/components/ThreadTerminalDrawer.tsx | 85 ++++++--- .../terminal/terminalInputWriter.test.ts | 170 ++++++++++++++++++ .../terminal/terminalInputWriter.ts | 106 +++++++++++ packages/contracts/src/terminal.test.ts | 22 +++ packages/contracts/src/terminal.ts | 23 ++- ...erminal-large-paste-reaches-program.e2e.ts | 105 +++++++++++ 12 files changed, 669 insertions(+), 80 deletions(-) create mode 100644 apps/web/src/components/terminal/terminalInputWriter.test.ts create mode 100644 apps/web/src/components/terminal/terminalInputWriter.ts create mode 100644 scripts/scenarios/terminal-large-paste-reaches-program.e2e.ts diff --git a/apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx b/apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx index 82dc526d3de0c..f3ed8b01983ca 100644 --- a/apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx +++ b/apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx @@ -2,6 +2,7 @@ import * as Dialog from "../../components/ui/Dialog"; import { AuthTerminalReadScope, AuthTerminalOperateScope, + chunkTerminalWrite, DEFAULT_TERMINAL_ID, EnvironmentId, ThreadId, @@ -607,15 +608,21 @@ function ThreadTerminal(props: { return false; } - const result = await writeTerminal({ - environmentId: selectedThread.environmentId, - input: { - threadId: selectedThread.id, - terminalId, - data, - }, - }); - return result._tag === "Success"; + // Android's IME delivers a system paste as one input, which can exceed one write. + const isSameShell = pasteSession.liveness(); + for (const chunk of chunkTerminalWrite(data)) { + if (!isSameShell()) return false; + const result = await writeTerminal({ + environmentId: selectedThread.environmentId, + input: { + threadId: selectedThread.id, + terminalId, + data: chunk, + }, + }); + if (result._tag !== "Success") return false; + } + return true; }; const pasteText = async (readText: () => Promise) => { diff --git a/apps/mobile/src/features/terminal/terminalInput.test.ts b/apps/mobile/src/features/terminal/terminalInput.test.ts index 17fabe588115c..6e5046dab35c4 100644 --- a/apps/mobile/src/features/terminal/terminalInput.test.ts +++ b/apps/mobile/src/features/terminal/terminalInput.test.ts @@ -1,11 +1,6 @@ import { describe, expect, it } from "vite-plus/test"; -import { - chunkTerminalWrite, - encodeTerminalPaste, - resolveModifiedTerminalInput, - TERMINAL_WRITE_MAX_LENGTH, -} from "./terminalInput"; +import { encodeTerminalPaste, resolveModifiedTerminalInput } from "./terminalInput"; const byte = (code: number) => String.fromCharCode(code); const ESC = byte(0x1b); @@ -78,23 +73,3 @@ describe("encodeTerminalPaste", () => { expect(encodeTerminalPaste(`safe${ESC}[201~; rm -rf /\n`)).toBe("safe [201~; rm -rf /\r"); }); }); - -describe("chunkTerminalWrite", () => { - it("splits oversized writes so every chunk fits the contract", () => { - const chunks = chunkTerminalWrite("y".repeat(TERMINAL_WRITE_MAX_LENGTH * 2 + 5)); - expect(chunks.map((chunk) => chunk.length)).toEqual([ - TERMINAL_WRITE_MAX_LENGTH, - TERMINAL_WRITE_MAX_LENGTH, - 5, - ]); - expect(chunks.join("")).toHaveLength(TERMINAL_WRITE_MAX_LENGTH * 2 + 5); - }); - - it("does not cut a surrogate pair in half at the boundary", () => { - const data = `${"z".repeat(TERMINAL_WRITE_MAX_LENGTH - 1)}😀tail`; - const chunks = chunkTerminalWrite(data); - expect(chunks[0]).toHaveLength(TERMINAL_WRITE_MAX_LENGTH - 1); - expect(chunks[1]).toBe("😀tail"); - expect(chunks.join("")).toBe(data); - }); -}); diff --git a/apps/mobile/src/features/terminal/terminalInput.ts b/apps/mobile/src/features/terminal/terminalInput.ts index 9c5e05dcc24f0..0f09d109e3634 100644 --- a/apps/mobile/src/features/terminal/terminalInput.ts +++ b/apps/mobile/src/features/terminal/terminalInput.ts @@ -3,9 +3,6 @@ import type { ExecutionEnvironmentPlatformOs } from "@t3tools/contracts"; export type PendingModifier = "ctrl" | "meta"; export type HostPlatform = "mac" | "linux" | "windows" | "unknown"; -/** Upper bound of `TerminalWriteInput.data`; longer writes are rejected by the server. */ -export const TERMINAL_WRITE_MAX_LENGTH = 65_536; - export type ModifiedTerminalInput = | { readonly kind: "write"; readonly data: string } | { readonly kind: "paste" }; @@ -64,21 +61,6 @@ export function encodeTerminalPaste(text: string): string { return text.replace(UNSAFE_PASTE_BYTES, " ").replace(/\r\n|\n/g, "\r"); } -export function chunkTerminalWrite(data: string): ReadonlyArray { - const chunks: string[] = []; - let start = 0; - while (start < data.length) { - let end = Math.min(start + TERMINAL_WRITE_MAX_LENGTH, data.length); - const last = data.charCodeAt(end - 1); - if (end < data.length && last >= 0xd800 && last <= 0xdbff) { - end -= 1; - } - chunks.push(data.slice(start, end)); - start = end; - } - return chunks; -} - /** Returns null for unknown OSes so callers can try weaker signals. */ export function hostPlatformFromOs(os: ExecutionEnvironmentPlatformOs | null): HostPlatform | null { if (os === "darwin") return "mac"; diff --git a/apps/mobile/src/features/terminal/terminalPaste.test.ts b/apps/mobile/src/features/terminal/terminalPaste.test.ts index ec9bd9d74d933..2545085137916 100644 --- a/apps/mobile/src/features/terminal/terminalPaste.test.ts +++ b/apps/mobile/src/features/terminal/terminalPaste.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from "vite-plus/test"; -import { TERMINAL_WRITE_MAX_LENGTH } from "./terminalInput"; +import { TERMINAL_WRITE_MAX_LENGTH } from "@t3tools/contracts"; import { createTerminalPasteSession } from "./terminalPaste"; function deferred() { @@ -34,6 +34,15 @@ describe("terminal paste session", () => { expect(writes).toEqual([]); }); + it("reports a liveness change after capture, including a restart in place", () => { + const session = createTerminalPasteSession(); + session.reset(true); + const isSameShell = session.liveness(); + expect(isSameShell()).toBe(true); + session.reset(true); + expect(isSameShell()).toBe(false); + }); + it("never overlaps writes from rapid paste requests", async () => { const session = createTerminalPasteSession(); session.reset(true); diff --git a/apps/mobile/src/features/terminal/terminalPaste.ts b/apps/mobile/src/features/terminal/terminalPaste.ts index d070bdc168826..b09e365587d57 100644 --- a/apps/mobile/src/features/terminal/terminalPaste.ts +++ b/apps/mobile/src/features/terminal/terminalPaste.ts @@ -1,4 +1,6 @@ -import { chunkTerminalWrite, encodeTerminalPaste } from "./terminalInput"; +import { chunkTerminalWrite } from "@t3tools/contracts"; + +import { encodeTerminalPaste } from "./terminalInput"; interface TerminalPasteInput { readonly readText: () => Promise; @@ -8,6 +10,8 @@ interface TerminalPasteInput { export interface TerminalPasteSession { readonly reset: (active: boolean) => void; + /** True until the terminal's liveness changes (stop, start or restart) after this call. */ + readonly liveness: () => () => boolean; readonly paste: (input: TerminalPasteInput) => Promise; } @@ -21,6 +25,11 @@ export function createTerminalPasteSession(): TerminalPasteSession { liveTarget = active ? {} : null; }, + liveness() { + const target = liveTarget; + return () => liveTarget === target; + }, + async paste({ readText, write, onReadError }) { const target = liveTarget; if (target === null) { diff --git a/apps/web/src/components/ThreadTerminalDrawer.browser.test.tsx b/apps/web/src/components/ThreadTerminalDrawer.browser.test.tsx index 739136162d31f..f9990ea7d0c45 100644 --- a/apps/web/src/components/ThreadTerminalDrawer.browser.test.tsx +++ b/apps/web/src/components/ThreadTerminalDrawer.browser.test.tsx @@ -1,4 +1,7 @@ -import { EMPTY_TERMINAL_SESSION_STATE } from "@t3tools/client-runtime/state/terminal"; +import { + applyTerminalAttachStreamEvent, + EMPTY_TERMINAL_SESSION_STATE, +} from "@t3tools/client-runtime/state/terminal"; import { EnvironmentId, ThreadId, @@ -24,6 +27,7 @@ const fixture = vi.hoisted(() => ({ typeIntoSurface: null as ((data: string) => void) | null, pressKey: null as ((event: KeyboardEvent) => boolean) | null, writes: [] as Array<{ threadId: string; data: string }>, + messages: [] as string[], resetSessions: () => {}, setSession: ( _thread: ScopedThreadRef, @@ -31,9 +35,11 @@ const fixture = vi.hoisted(() => ({ _version: number, _generation?: number, ) => {}, + sessionEvent: (_thread: ScopedThreadRef, _type: "snapshot" | "restarted", _pid = 123) => {}, surfaceOptions: [] as GhosttyTerminalSurfaceOptions[], reportedSize: null as { cols: number; rows: number } | null, resizeCommand: {}, + runWrite: (): Promise<{ _tag: "Success" }> => Promise.resolve({ _tag: "Success" }), runResize: (_input: unknown): Promise> => Promise.resolve(AsyncResult.success(undefined)), metadata: new Map< @@ -46,6 +52,7 @@ const fixture = vi.hoisted(() => ({ const fakeSurface: object = new Proxy( { input: document.createElement("textarea"), + write: (data: string) => fixture.messages.push(data), // The real surface re-reports its measured grid. resendSize: () => { const size = fixture.reportedSize; @@ -73,9 +80,23 @@ vi.mock("~/terminal/ghostty/surface", () => ({ vi.mock("../state/terminalSessions", async () => { const Atom = await import("effect/reactivity/Atom"); // A strong map, not Atom.family: a family holds weakly, so a GC could hand setSession a new atom. + const summary = (threadId: ThreadId, pid: number) => ({ + threadId, + terminalId: "term-1", + cwd: "/repo", + worktreePath: null, + status: "running" as const, + pid, + exitCode: null, + exitSignal: null, + hasRunningSubprocess: false, + label: "Terminal 1", + updatedAt: "2026-11-15T00:00:00.000Z", + }); const makeSession = (threadId: ThreadId) => Atom.make({ ...EMPTY_TERMINAL_SESSION_STATE, + summary: summary(threadId, 123), status: fixture.statusByThreadId.get(threadId) ?? "closed", version: 1, }); @@ -99,6 +120,32 @@ vi.mock("../state/terminalSessions", async () => { : { output: { ...EMPTY_TERMINAL_SESSION_STATE.output, generation } }), }); }; + fixture.sessionEvent = (thread, type, pid = 123) => { + const atom = session(thread.environmentId, thread.threadId); + const current = appAtomRegistry.get(atom); + appAtomRegistry.set(atom, { + ...current, + summary: summary(thread.threadId, pid), + ...applyTerminalAttachStreamEvent(current, { + type, + threadId: thread.threadId, + terminalId: "term-1", + snapshot: { + threadId: thread.threadId, + terminalId: "term-1", + cwd: "/repo", + worktreePath: null, + status: "running", + pid, + history: "", + exitCode: null, + exitSignal: null, + label: "Terminal 1", + updatedAt: "2026-11-15T00:00:00.000Z", + }, + }), + }); + }; return { attachedTerminalSessionAtom: (environmentId: EnvironmentId, terminal: { threadId: ThreadId }) => session(environmentId, terminal.threadId), @@ -114,6 +161,7 @@ vi.mock("../state/use-atom-command", () => ({ if (command === fixture.resizeCommand) return fixture.runResize(args); if (args.input.data !== undefined) { fixture.writes.push({ threadId: args.input.threadId, data: args.input.data }); + return fixture.runWrite(); } return { _tag: "Success" }; }, @@ -194,11 +242,11 @@ function Drawer(props: { function setup(originalStatus: "starting" | "exited" = "exited") { fixture.resetSessions(); const original: ScopedThreadRef = { - environmentId: EnvironmentId.make("environment-a"), + environmentId: EnvironmentId.make(`environment-a-${expect.getState().currentTestName}`), threadId: ThreadId.make("thread-a"), }; const other: ScopedThreadRef = { - environmentId: EnvironmentId.make("environment-b"), + environmentId: EnvironmentId.make(`environment-b-${expect.getState().currentTestName}`), threadId: ThreadId.make("thread-b"), }; fixture.statusByThreadId = new Map([ @@ -209,9 +257,11 @@ function setup(originalStatus: "starting" | "exited" = "exited") { fixture.typeIntoSurface = null; fixture.pressKey = null; fixture.writes = []; + fixture.messages = []; fixture.surfaceOptions = []; fixture.reportedSize = null; fixture.runResize = () => Promise.resolve(AsyncResult.success(undefined)); + fixture.runWrite = () => Promise.resolve({ _tag: "Success" }); fixture.metadata.clear(); useRightPanelStore.setState(useRightPanelStore.getInitialState()); useTerminalUiStateStore.setState(useTerminalUiStateStore.getInitialState()); @@ -377,6 +427,96 @@ it("only activates terminal paths confirmed to exist on their environment", asyn expect(canActivate("~/notes.ts:5:2")).toBe(true); }); +it("drops the rest of a split paste when the shell restarts mid-paste", async () => { + const { other: threadRef } = setup(); + let settleWrite = () => {}; + fixture.runWrite = () => + new Promise((resolve) => { + settleWrite = () => resolve({ _tag: "Success" }); + }); + await using _screen = await render( {}} />); + await act(async () => { + fixture.pendingSurfaces.shift()?.(); + }); + fixture.typeIntoSurface?.("p".repeat(70_000)); + await act(async () => { + fixture.sessionEvent(threadRef, "restarted"); + settleWrite(); + }); + fixture.typeIntoSurface?.("after"); + expect(fixture.writes.map(({ data }) => data.length)).toEqual([65_536, 5]); +}); + +it.each([ + { pid: 123, closes: true }, + { pid: 456, closes: false }, +])( + "closes interrupted bracket framing only for its process (PID $pid)", + async ({ pid, closes }) => { + const { other: threadRef } = setup(); + let settleWrite = () => {}; + fixture.runWrite = () => + new Promise((resolve) => { + settleWrite = () => { + fixture.runWrite = () => Promise.resolve({ _tag: "Success" }); + resolve({ _tag: "Success" }); + }; + }); + await using _screen = await render( {}} />); + await act(async () => { + fixture.pendingSurfaces.shift()?.(); + }); + const large = `\x1b[200~${"p".repeat(70_000)}\x1b[201~`; + fixture.typeIntoSurface?.(large); + await act(async () => { + fixture.sessionEvent(threadRef, "snapshot", pid); + settleWrite(); + }); + await act(async () => { + fixture.typeIntoSurface?.("after"); + }); + expect( + fixture.messages.filter((message) => message.includes("paste interrupted")), + ).toHaveLength(1); + expect(fixture.writes.map(({ data }) => data)).toEqual([ + large.slice(0, 65_536), + ...(closes ? ["\x1b[201~"] : []), + "after", + ]); + }, +); + +it("closes an interrupted paste after disposal before input from a remounted view", async () => { + const { other: threadRef } = setup(); + let settleWrite = () => {}; + fixture.runWrite = () => + new Promise((resolve) => { + settleWrite = () => { + fixture.runWrite = () => Promise.resolve({ _tag: "Success" }); + resolve({ _tag: "Success" }); + }; + }); + await using screen = await render( {}} />); + await act(async () => { + fixture.pendingSurfaces.shift()?.(); + }); + const large = `\x1b[200~${"p".repeat(70_000)}\x1b[201~`; + fixture.typeIntoSurface?.(large); + fixture.typeIntoSurface?.("stale"); + await screen.rerender(
); + await act(async () => { + settleWrite(); + }); + await screen.rerender( {}} />); + await act(async () => { + fixture.pendingSurfaces.shift()?.(); + }); + await act(async () => { + fixture.typeIntoSurface?.("after"); + }); + expect(fixture.writes.map(({ data }) => data.length)).toEqual([65_536, 6, 5]); +}); + it("resends a failed resize once in each writable session", async () => { const { other: threadRef } = setup(); const resizes: unknown[] = []; diff --git a/apps/web/src/components/ThreadTerminalDrawer.tsx b/apps/web/src/components/ThreadTerminalDrawer.tsx index e5de4ea090b44..e2ab63f058ed1 100644 --- a/apps/web/src/components/ThreadTerminalDrawer.tsx +++ b/apps/web/src/components/ThreadTerminalDrawer.tsx @@ -88,6 +88,7 @@ import { readEnvironmentScope } from "../state/session"; import { terminalEnvironment } from "../state/terminal"; import { useEnvironmentScope } from "../state/session"; import { openTerminalLinkInPreview } from "./preview/openTerminalLinkInPreview"; +import { terminalInputWriter } from "./terminal/terminalInputWriter"; import { useAtomCommand } from "../state/use-atom-command"; import { preventTerminalCloseShortcut } from "../lib/terminalCloseShortcut"; import { observeResize } from "../lib/observeResize"; @@ -575,6 +576,8 @@ function TerminalSessionViewport({ const runTerminalWrite = useAtomCommand(terminalEnvironment.write, { reportFailure: false, }); + // Recovery keeps the original registry-bound command after this viewport unmounts. + const retainedTerminalWrite = useRef(runTerminalWrite); const runTerminalResize = useAtomCommand(terminalEnvironment.resize, { reportFailure: false, }); @@ -622,12 +625,6 @@ function TerminalSessionViewport({ (listener: (session: TerminalSessionState) => void) => appAtomRegistry.subscribe(terminalSessionAtom, listener), ); - const writeTerminal = useEffectEvent((data: string) => - runTerminalWrite({ - environmentId, - input: { threadId, terminalId, data }, - }), - ); // The surface reports a resent grid like any other, so the next report is the retry. const resizeRetryGenerationRef = useRef(null); const resizeTerminal = useEffectEvent(async (cols: number, rows: number) => { @@ -928,21 +925,67 @@ function TerminalSessionViewport({ } }; - const sendTerminalInput = async (data: string, fallbackError: string) => { + const readInputSession = () => appAtomRegistry.get(terminalSessionAtom); + const canWriteInput = () => readEnvironmentScope(environmentId, AuthTerminalOperateScope); + const writeInputChunk = (data: string) => + retainedTerminalWrite.current({ + environmentId, + input: { threadId, terminalId, data }, + }); + const sendTerminalInput = (data: string, fallbackError: string) => { // The server rejects input until the shell runs, and each rejection would print an error. - if (writableSessionGeneration(readTerminalSession(), hasTerminalWriteAccess()) === null) { - return; - } - const activeTerminal = terminalRef.current; - if (!activeTerminal) return; - const result = await writeTerminal(data); - if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) { - const error = squashAtomCommandFailure(result); - writeSystemMessage( - activeTerminal, - error instanceof Error ? error.message : fallbackError, - ); - } + // Input belongs to the shell and surface it was typed into; a restart the client has seen drops what is left. + const session = readTerminalSession(); + const generation = writableSessionGeneration(session, hasTerminalWriteAccess()); + const lifecycleVersion = session.lifecycleVersion; + if (generation === null) return; + const writeInput = terminalInputWriter( + JSON.stringify([environmentId, threadId, terminalId]), + () => appAtomRegistry.mount(terminalSessionAtom), + ); + let reported = false; + const report = (message: string) => { + if (reported || terminalRef.current !== terminal) return; + reported = true; + writeSystemMessage(terminal, message); + }; + return writeInput( + data, + async (chunk) => { + const currentSession = readInputSession(); + if ( + terminalRef.current !== terminal || + currentSession.lifecycleVersion !== lifecycleVersion || + writableSessionGeneration(currentSession, canWriteInput()) !== generation + ) { + return "not-dispatched"; + } + const result = await writeInputChunk(chunk); + if (result._tag === "Success") return true; + if (terminalRef.current === terminal && !isAtomCommandInterrupted(result)) { + const error = squashAtomCommandFailure(result); + report(error instanceof Error ? error.message : fallbackError); + } + return false; + }, + // Close the bracket only for the process that saw it open; wait while its pid is unknown. + async () => { + const pid = session.summary?.pid ?? null; + const current = readInputSession(); + const currentPid = current.summary?.pid ?? null; + if (currentPid === null) return false; + if (pid !== null && currentPid !== pid) return true; + if (writableSessionGeneration(current, canWriteInput()) === null) return false; + const result = await writeInputChunk("\x1b[201~"); + if (result._tag === "Success") return true; + if (!isAtomCommandInterrupted(result)) { + const error = squashAtomCommandFailure(result); + report(error instanceof Error ? error.message : "Failed to close interrupted paste"); + } + return false; + }, + () => report("Terminal paste interrupted. Paste again when the terminal is ready."), + ); }; function handleBeforeKey(event: KeyboardEvent): boolean { @@ -1104,7 +1147,7 @@ function TerminalSessionViewport({ mount.focus({ preventScroll: true }); } }; - }, [cwd, environmentId, terminalId]); + }, [cwd, environmentId, terminalId, terminalSessionAtom, threadId]); const attachContainer = (mount: HTMLDivElement) => { containerRef.current = mount; diff --git a/apps/web/src/components/terminal/terminalInputWriter.test.ts b/apps/web/src/components/terminal/terminalInputWriter.test.ts new file mode 100644 index 0000000000000..2670add86a2f3 --- /dev/null +++ b/apps/web/src/components/terminal/terminalInputWriter.test.ts @@ -0,0 +1,170 @@ +import { TerminalWriteInput } from "@t3tools/contracts"; +import * as Schema from "effect/Schema"; +import { describe, expect, it, vi } from "vite-plus/test"; + +import { createTerminalInputWriter } from "./terminalInputWriter"; + +const isWriteInput = Schema.is(TerminalWriteInput); +const fitsWrite = (data: string) => isWriteInput({ threadId: "t", terminalId: "default", data }); + +/** A `send` whose calls stay pending until the test settles them. */ +function heldSend() { + const sent: Array<{ readonly chunk: string; readonly settle: (ok: boolean) => void }> = []; + const waiters: Array<{ readonly count: number; readonly resolve: () => void }> = []; + return { + sent, + chunks: () => sent.map(({ chunk }) => chunk), + /** Resolves once `count` chunks have been sent. */ + sends: (count: number) => + new Promise((resolve) => { + if (sent.length >= count) resolve(); + else waiters.push({ count, resolve }); + }), + send: (chunk: string) => + new Promise((settle) => { + sent.push({ chunk, settle }); + for (const waiter of waiters) if (sent.length >= waiter.count) waiter.resolve(); + }), + }; +} + +describe("createTerminalInputWriter", () => { + const line = ' {"file":"src/日本語.ts","message":"Review 🧑🏽‍💻 café changes"},\n'; + const paste = (length: number) => + line.repeat(Math.ceil(length / line.length)).slice(0, length - 1) + "\n"; + + it("delivers a bracketed paste over the limit as writes the server accepts", async () => { + const data = `\x1b[200~${paste(100_000)}\x1b[201~`; + const sent: string[] = []; + await createTerminalInputWriter()(data, async (chunk) => { + sent.push(chunk); + return true; + }); + + expect(sent).toHaveLength(2); + expect(sent.every(fitsWrite)).toBe(true); + expect(sent.join("")).toBe(data); + }); + + it("sends one chunk per round trip and holds later input behind a split write", async () => { + const write = createTerminalInputWriter(); + const { sent, chunks, sends, send } = heldSend(); + const large = `\x1b[200~${"p".repeat(70_000)}\x1b[201~`; + const [first, second] = [large.slice(0, 65_536), large.slice(65_536)]; + + void write("a", send); + const pasted = write(large, send); + void write("b", send); + void write("c", send); + expect(chunks()).toEqual(["a"]); + sent[0]!.settle(true); + await sends(2); + expect(chunks()).toEqual(["a", first]); + + sent[1]!.settle(true); + await sends(3); + expect(chunks()).toEqual(["a", first, second]); + sent[2]!.settle(true); + await pasted; + await sends(4); + expect(chunks()).toEqual(["a", first, second, "b"]); + sent[3]!.settle(true); + await sends(5); + expect(chunks()).toEqual(["a", first, second, "b", "c"]); + }); + + it("dispatches small writes without waiting for acknowledgements and queues a paste behind them", async () => { + const write = createTerminalInputWriter(); + const { sent, chunks, sends, send } = heldSend(); + const inputs = Array.from({ length: 32 }, (_, index) => `key-${index}`); + const smallWrites = inputs.map((data) => write(data, send)); + expect(chunks()).toEqual(inputs); + + const large = "p".repeat(70_000); + const pasted = write(large, send); + const after = write("after", send); + // Finishing newer sends first must not let queued input overtake the oldest write. + for (let index = inputs.length - 1; index > 0; index--) sent[index]!.settle(true); + await Promise.all(smallWrites.slice(1)); + expect(sent).toHaveLength(32); + sent[0]!.settle(true); + await sends(33); + expect(sent[32]!.chunk).toBe(large.slice(0, 65_536)); + sent[32]!.settle(true); + await sends(34); + expect(sent[33]!.chunk).toBe(large.slice(65_536)); + sent[33]!.settle(true); + await pasted; + await sends(35); + expect(sent[34]!.chunk).toBe("after"); + sent[34]!.settle(true); + await after; + }); + + it("closes an interrupted bracketed paste before later input", async () => { + const write = createTerminalInputWriter(); + const { sent, chunks, sends, send } = heldSend(); + const large = `\x1b[200~${"p".repeat(140_000)}\x1b[201~`; + const pasted = write(large, send); + const after = write("after", send); + sent[0]!.settle(true); + await sends(2); + sent[1]!.settle(false); + await sends(3); + expect(chunks()).toEqual([large.slice(0, 65_536), large.slice(65_536, 131_072), "\x1b[201~"]); + sent[2]!.settle(false); + await pasted; + await sends(4); + expect(sent[3]!.chunk).toBe("\x1b[201~"); + sent[3]!.settle(true); + await sends(5); + expect(sent[4]!.chunk).toBe("after"); + sent[4]!.settle(true); + await after; + }); + + it("closes an opening chunk whose acknowledgement was lost without retrying payload", async () => { + const sent: string[] = []; + const write = createTerminalInputWriter(); + const send = async (chunk: string) => { + sent.push(chunk); + return chunk === "\x1b[201~" || chunk === "after"; + }; + const large = `\x1b[200~${"p".repeat(70_000)}\x1b[201~`; + await write(large, send); + await write("after", send); + expect(sent).toEqual([large.slice(0, 65_536), "\x1b[201~", "after"]); + }); + + it("reports each input blocked by failed framing recovery without retrying payload", async () => { + const sent: string[] = []; + const interrupted = vi.fn(); + const write = createTerminalInputWriter(); + const send = async (chunk: string) => { + sent.push(chunk); + return false; + }; + const recover = async () => { + sent.push("\x1b[201~"); + return false; + }; + const large = `\x1b[200~${"p".repeat(70_000)}\x1b[201~`; + await write(large, send, recover, interrupted); + await write("after", send, recover, interrupted); + expect(sent).toEqual([large.slice(0, 65_536), "\x1b[201~", "\x1b[201~"]); + expect(interrupted).toHaveBeenCalledTimes(2); + }); + + it("drops the rest of a split write after a failed chunk and keeps later input", async () => { + const write = createTerminalInputWriter(); + const { sent, chunks, sends, send } = heldSend(); + + const pasted = write("q".repeat(140_000), send); + void write("next", send); + sent[0]!.settle(false); + await pasted; + await sends(2); + + expect(chunks()).toEqual(["q".repeat(65_536), "next"]); + }); +}); diff --git a/apps/web/src/components/terminal/terminalInputWriter.ts b/apps/web/src/components/terminal/terminalInputWriter.ts new file mode 100644 index 0000000000000..4d269ae86eab4 --- /dev/null +++ b/apps/web/src/components/terminal/terminalInputWriter.ts @@ -0,0 +1,106 @@ +import { chunkTerminalWrite } from "@t3tools/contracts"; + +/** Sends one chunk of terminal input; resolves false when it failed and the rest should drop. */ +type SendTerminalChunk = (chunk: string) => Promise; + +/** + * Orders one terminal's input into writes that fit `terminal.write`. A split write goes one chunk + * per round trip and later input waits behind it; other writes go out immediately, like keystrokes. + */ +export function createTerminalInputWriter(onIdle = () => {}) { + let pending: Promise | null = null; + const immediate = new Set>(); + + let closeInterruptedPaste: (() => Promise) | null = null; + const closePaste = async () => { + if (closeInterruptedPaste === null) return true; + if (!(await closeInterruptedPaste())) return false; + closeInterruptedPaste = null; + return true; + }; + + const sendChunks = async ( + data: string, + chunks: ReadonlyArray, + send: SendTerminalChunk, + recover: () => Promise, + interrupted: () => void, + ) => { + if (closeInterruptedPaste !== null && !(await closePaste())) { + interrupted(); + return; + } + const bracketed = + chunks.length > 1 && data.startsWith("\x1b[200~") && data.endsWith("\x1b[201~"); + let opened = false; + try { + for (const chunk of chunks) { + const result = await send(chunk); + if (result !== true) { + if (result !== "not-dispatched") opened = bracketed; + if (chunks.length > 1) interrupted(); + return; + } + opened = bracketed; + } + opened = false; + } finally { + if (opened) { + closeInterruptedPaste = recover; + await closePaste(); + } + } + }; + + return ( + data: string, + send: SendTerminalChunk, + recover: () => Promise = async () => (await send("\x1b[201~")) === true, + interrupted = () => {}, + ): Promise => { + const idle = () => { + if (pending === null && immediate.size === 0 && closeInterruptedPaste === null) onIdle(); + }; + const chunks = chunkTerminalWrite(data); + const run = () => sendChunks(data, chunks, send, recover, interrupted); + if (pending === null && closeInterruptedPaste === null && chunks.length === 1) { + const current = run(); + immediate.add(current); + void current.then( + () => { + immediate.delete(current); + idle(); + }, + () => { + immediate.delete(current); + idle(); + }, + ); + return current; + } + const previous = pending ?? (immediate.size > 0 ? Promise.allSettled(immediate) : null); + const current = previous === null ? run() : previous.then(run, run); + pending = current; + const reset = () => { + if (pending === current) pending = null; + idle(); + }; + void current.then(reset, reset); + return current; + }; +} + +const writers = new Map>(); + +/** Retain ordering and framing obligations until acknowledged, across terminal view remounts. */ +export function terminalInputWriter(key: string, retain: () => () => void) { + const existing = writers.get(key); + if (existing) return existing; + const release = retain(); + const writer = createTerminalInputWriter(() => { + if (writers.get(key) === writer) writers.delete(key); + release(); + }); + writers.set(key, writer); + return writer; +} diff --git a/packages/contracts/src/terminal.test.ts b/packages/contracts/src/terminal.test.ts index dda8060195fcf..16b3ed1d38eb9 100644 --- a/packages/contracts/src/terminal.test.ts +++ b/packages/contracts/src/terminal.test.ts @@ -2,7 +2,9 @@ import * as Schema from "effect/Schema"; import { describe, expect, it } from "vite-plus/test"; import { + chunkTerminalWrite, DEFAULT_TERMINAL_ID, + TERMINAL_WRITE_MAX_LENGTH, TerminalError, TerminalOpenInput, TerminalProviderEnvironmentError, @@ -49,3 +51,23 @@ describe("TerminalOpenInput", () => { expect(parsed.env).toEqual({ GOOD_KEY: "2" }); }); }); + +describe("chunkTerminalWrite", () => { + it("splits oversized writes so every chunk fits the contract", () => { + const chunks = chunkTerminalWrite("y".repeat(TERMINAL_WRITE_MAX_LENGTH * 2 + 5)); + expect(chunks.map((chunk) => chunk.length)).toEqual([ + TERMINAL_WRITE_MAX_LENGTH, + TERMINAL_WRITE_MAX_LENGTH, + 5, + ]); + expect(chunks.join("")).toHaveLength(TERMINAL_WRITE_MAX_LENGTH * 2 + 5); + }); + + it("does not cut a surrogate pair in half at the boundary", () => { + const data = `${"z".repeat(TERMINAL_WRITE_MAX_LENGTH - 1)}😀tail`; + const chunks = chunkTerminalWrite(data); + expect(chunks[0]).toHaveLength(TERMINAL_WRITE_MAX_LENGTH - 1); + expect(chunks[1]).toBe("😀tail"); + expect(chunks.join("")).toBe(data); + }); +}); diff --git a/packages/contracts/src/terminal.ts b/packages/contracts/src/terminal.ts index 91e952326a05c..ddb5e5c10344a 100644 --- a/packages/contracts/src/terminal.ts +++ b/packages/contracts/src/terminal.ts @@ -190,12 +190,33 @@ export type TerminalAttachInput = typeof TerminalAttachInput.Type; export const TerminalObserveInput = TerminalSessionInput; export type TerminalObserveInput = Schema.Codec.Encoded; +/** Longest `data` one `terminal.write` carries, in UTF-16 code units. */ +export const TERMINAL_WRITE_MAX_LENGTH = 65_536; + export const TerminalWriteInput = Schema.Struct({ ...TerminalSessionInput.fields, - data: Schema.String.check(Schema.isNonEmpty()).check(Schema.isMaxLength(65_536)), + data: Schema.String.check(Schema.isNonEmpty()).check( + Schema.isMaxLength(TERMINAL_WRITE_MAX_LENGTH), + ), }); export type TerminalWriteInput = Schema.Codec.Encoded; +/** Splits input into writes that fit `TerminalWriteInput`, never between a surrogate pair. */ +export function chunkTerminalWrite(data: string): ReadonlyArray { + const chunks: string[] = []; + let start = 0; + while (start < data.length) { + let end = Math.min(start + TERMINAL_WRITE_MAX_LENGTH, data.length); + const last = data.charCodeAt(end - 1); + if (end < data.length && last >= 0xd800 && last <= 0xdbff) { + end -= 1; + } + chunks.push(data.slice(start, end)); + start = end; + } + return chunks; +} + export const TerminalResizeInput = Schema.Struct({ ...TerminalSessionInput.fields, cols: TerminalColsSchema, diff --git a/scripts/scenarios/terminal-large-paste-reaches-program.e2e.ts b/scripts/scenarios/terminal-large-paste-reaches-program.e2e.ts new file mode 100644 index 0000000000000..35f551fb3aabb --- /dev/null +++ b/scripts/scenarios/terminal-large-paste-reaches-program.e2e.ts @@ -0,0 +1,105 @@ +/// +// The DOM lib types the function this scenario runs in the page. +import { expect } from "e2e"; +import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; + +import { runPromise, test, WEB } from "./t3.ts"; + +// A raw-mode program that turns on bracketed paste, logs every byte it receives to `received`, +// and exits once the paste's closing marker arrives. +const PROBE = String.raw`import { appendFileSync, writeFileSync } from "node:fs"; + +const log = new URL("received", import.meta.url); +let tail = ""; +let bytes = 0; + +writeFileSync(log, ""); +process.stdin.setRawMode(true); +process.stdin.on("data", (chunk) => { + appendFileSync(log, chunk); + bytes += chunk.length; + tail = (tail + chunk.toString("latin1")).slice(-6); + if (tail === "\x1b[201~") { + process.stdin.setRawMode(false); + process.stdout.write("\x1b[?2004l\r\nreceived " + bytes + " bytes\r\n"); + process.exit(0); + } +}); +process.stdout.write("\x1b[?2004h"); +writeFileSync(new URL("ready", import.meta.url), "ready"); +`; + +// Over the 65,536-unit write limit, multiline, with a surrogate pair straddling the first +// 65,536-unit boundary of the bracketed paste (6 units of ESC[200~ come first). +const LINE = ' {"file":"src/日本語.ts","message":"café changes"},\n'; +const EMOJI_LINE = ' {"user":"🧑🏽‍💻","note":"naïve 𝔘𝔫𝔦𝔠𝔬𝔡𝔢"},\n'; +const HEAD = LINE.repeat(Math.ceil(65_529 / LINE.length)).slice(0, 65_529); +const PASTE = HEAD + "🧑🏽‍💻" + EMOJI_LINE.repeat(Math.ceil(34_000 / EMOJI_LINE.length)); +const EXPECTED = Buffer.from("\x1b[200~" + PASTE + "\x1b[201~"); +const compare = (received: Uint8Array) => ({ + bytes: received.length, + intact: EXPECTED.equals(received), +}); + +test( + "A web terminal paste larger than one write reaches the program intact", + WEB, + async ({ t3, app, browser, screen }) => { + await browser.emulateMedia({ colorScheme: "dark" }); + const project = await t3.project("demo", { "paste-probe.mjs": PROBE }); + const thread = await t3.thread({ project, title: "Paste", messages: ["hello"] }); + const read = (file: string) => + runPromise( + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + return yield* fs.readFileString(`${project}/${file}`); + }).pipe(Effect.orElseSucceed(() => "")), + ); + const received = () => + runPromise( + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + return compare(yield* fs.readFile(`${project}/received`)); + }).pipe(Effect.orElseSucceed(() => compare(new Uint8Array()))), + ); + await t3.open(thread); + + await screen.getByRole("button", "Toggle terminal drawer").tap(); + const terminal = screen.getByLabel("Terminal input"); + + // Input sent before the shell runs is dropped, so retype until it lands. + await expect + .poll( + async () => { + await terminal.focus(); + await browser.keyboard.type(`echo ready > '${project}/shell-ready'`); + await browser.keyboard.press("Enter"); + return read("shell-ready"); + }, + { timeout: 30_000 }, + ) + .toBe("ready\n"); + + await terminal.focus(); + await browser.keyboard.type(`'${process.execPath}' '${project}/paste-probe.mjs'`); + await browser.keyboard.press("Enter"); + await expect.poll(() => read("ready"), { timeout: 15_000 }).toBe("ready"); + + await browser.evaluate((text) => { + const input = document.querySelector('[aria-label="Terminal input"]'); + if (!input) throw new Error("no terminal input"); + const clipboardData = new DataTransfer(); + clipboardData.setData("text/plain", text); + input.dispatchEvent( + new ClipboardEvent("paste", { clipboardData, bubbles: true, cancelable: true }), + ); + return null; + }, PASTE); + + await expect + .poll(received, { timeout: 20_000 }) + .toEqual({ bytes: EXPECTED.length, intact: true }); + await app.screenshot("pasted"); + }, +); From f0a33213547ce81ea9c39e4bedb13829cf307f56 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 15 Nov 2026 01:44:31 -0800 Subject: [PATCH 003/124] fix(server): settling a thread keeps exec and foreground Bash terminals open (#37125) --- apps/server/src/terminal/Manager.test.ts | 48 ++++++++++++ apps/server/src/terminal/Manager.ts | 93 +++++++++++++++++++++--- 2 files changed, 130 insertions(+), 11 deletions(-) diff --git a/apps/server/src/terminal/Manager.test.ts b/apps/server/src/terminal/Manager.test.ts index 3df1132e99d83..18199f97d9193 100644 --- a/apps/server/src/terminal/Manager.test.ts +++ b/apps/server/src/terminal/Manager.test.ts @@ -242,6 +242,7 @@ interface CreateManagerOptions { ReadonlyArray<{ readonly pid: number; readonly ppid: number; readonly name: string }>, never >; + foregroundProcessGroup?: (terminalPid: number) => Effect.Effect; subprocessPollIntervalMs?: number; processKillGraceMs?: number; registerTerminalProcesses?: Parameters< @@ -293,6 +294,8 @@ const createManager = ( ? { subprocessInspector: options.subprocessInspector } : {}), ...(options.processTable !== undefined ? { processTable: options.processTable } : {}), + // Fixture pids are not real processes; default to a shell idle at its prompt. + foregroundProcessGroup: options.foregroundProcessGroup ?? ((pid) => Effect.succeed(pid)), ...(options.subprocessPollIntervalMs !== undefined ? { subprocessPollIntervalMs: options.subprocessPollIntervalMs } : {}), @@ -943,6 +946,51 @@ it.layer( }), ); + it.effect("keeps a terminal whose shell was replaced by exec open on settlement", () => + Effect.gen(function* () { + const { manager, ptyAdapter } = yield* createManager(5, { + shellResolver: () => "/bin/bash", + processTable: Effect.succeed([{ pid: 9000, ppid: 1, name: "node" }]), + }).pipe(Effect.provide(layerWithHostPlatform("linux"))); + yield* manager.open(openInput()); + + yield* manager.closeOnSettle({ threadId: "thread-1" }); + + expect(ptyAdapter.processes[0]?.killed).toBe(false); + }), + ); + + it.effect.each([ + { label: "idle prompt", platform: "linux", foregroundProcessGroup: 9000, killed: true }, + { label: "foreground job", platform: "linux", foregroundProcessGroup: 100, killed: false }, + { + label: "unknown foreground group", + platform: "linux", + foregroundProcessGroup: null, + killed: false, + }, + { label: "idle prompt", platform: "darwin", foregroundProcessGroup: 9000, killed: true }, + { label: "foreground job", platform: "darwin", foregroundProcessGroup: 100, killed: false }, + ] as const)( + "settles a shell with a childless shell-named child by its foreground group: $label ($platform)", + ({ platform, foregroundProcessGroup, killed }) => + Effect.gen(function* () { + const { manager, ptyAdapter } = yield* createManager(5, { + shellResolver: () => "/bin/bash", + processTable: Effect.succeed([ + { pid: 9000, ppid: 1, name: "bash" }, + { pid: 100, ppid: 9000, name: "bash" }, + ]), + foregroundProcessGroup: () => Effect.succeed(foregroundProcessGroup), + }).pipe(Effect.provide(layerWithHostPlatform(platform))); + yield* manager.open(openInput()); + + yield* manager.closeOnSettle({ threadId: "thread-1" }); + + expect(ptyAdapter.processes[0]?.killed).toBe(killed); + }), + ); + it.effect("labels a running command by its whole process name, spaces included", () => Effect.gen(function* () { const { manager } = yield* createManager(5, { diff --git a/apps/server/src/terminal/Manager.ts b/apps/server/src/terminal/Manager.ts index d152aaa14389a..6ec31ad76191d 100644 --- a/apps/server/src/terminal/Manager.ts +++ b/apps/server/src/terminal/Manager.ts @@ -255,6 +255,7 @@ interface TerminalSubprocessInspectResult { interface TerminalSubprocessInspector { ( terminalPid: number, + shell: string | null, ): Effect.Effect; } @@ -766,21 +767,48 @@ function processTableSnapshotFromProcesses( return { childrenByParent, commandById }; } +// A root with one of these names is still an interactive shell, e.g. after `.bashrc` runs `exec zsh`. +const INTERACTIVE_SHELL_NAMES = new Set([ + "bash", + "zsh", + "fish", + "sh", + "dash", + "ksh", + "mksh", + "tcsh", + "csh", + "nu", +]); + function deriveSubprocessInspectResult( snapshot: TerminalProcessTableSnapshot, terminalPid: number, + shell: string | null, platform: NodeJS.Platform, + foregroundProcessGroup?: number | null, ): TerminalSubprocessInspectResult { const commandName = (pid: number) => normalizeChildCommandName(snapshot.commandById.get(pid) ?? "", platform); const shellName = commandName(terminalPid); - // Async prompt themes fork childless shell helpers that are not user commands. - const childPid = (snapshot.childrenByParent.get(terminalPid) ?? []).find( - (pid) => - shellName === null || - commandName(pid) !== shellName || - (snapshot.childrenByParent.get(pid)?.length ?? 0) > 0, - ); + // `exec ` replaces the shell, leaving the command itself as the root. + const rootIsCommand = + platform !== "win32" && + shell !== null && + shellName !== null && + shellName !== normalizeChildCommandName(shell, platform) && + !INTERACTIVE_SHELL_NAMES.has(shellName.replace(/^-/, "")); + // Async prompt themes fork childless shell helpers that are not user commands; + // a foreground job owns the terminal's process group, and an unknown one counts as foreground. + const childPid = rootIsCommand + ? terminalPid + : (snapshot.childrenByParent.get(terminalPid) ?? []).find( + (pid) => + shellName === null || + commandName(pid) !== shellName || + (snapshot.childrenByParent.get(pid)?.length ?? 0) > 0 || + (foregroundProcessGroup !== undefined && foregroundProcessGroup !== terminalPid), + ); if (childPid === undefined) { return { hasRunningSubprocess: false, childCommand: null, processIds: [] }; } @@ -803,6 +831,12 @@ function deriveSubprocessInspectResult( }; } +// Field 8 of /proc//stat is the tty's foreground process group; comm (field 2) may hold spaces or parens. +function parseProcStatForegroundProcessGroup(stat: string): number | null { + const tpgid = Number(stat.slice(stat.lastIndexOf(")") + 2).split(" ")[5]); + return Number.isInteger(tpgid) ? tpgid : null; +} + const POSIX_PS_ABSOLUTE_PATHS = ["/bin/ps", "/usr/bin/ps"] as const; // Resolve ps once; bare-name spawns repeatedly probe long PATHs during one-second polling. @@ -1620,6 +1654,7 @@ interface TerminalManagerOptions { ReadonlyArray, TerminalSubprocessCheckError >; + foregroundProcessGroup?: (terminalPid: number) => Effect.Effect; subprocessPollIntervalMs?: number; processKillGraceMs?: number; maxRetainedInactiveSessions?: number; @@ -1800,6 +1835,30 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func snapshotSucceeded: Result.isSuccess(fallback.snapshot), })), ); + const foregroundProcessGroup = + options.foregroundProcessGroup ?? + ((terminalPid: number) => + platform === "linux" + ? fileSystem.readFileString(`/proc/${terminalPid}/stat`).pipe( + Effect.map(parseProcStatForegroundProcessGroup), + Effect.orElseSucceed(() => null), + ) + : processRunner + .run({ + command: posixPsCommand ?? "ps", + args: ["-o", "tpgid=", "-p", String(terminalPid)], + timeout: CLOSE_IDLE_QUERY_TIMEOUT.posix, + timeoutBehavior: "timedOutResult", + }) + .pipe( + Effect.map((result) => { + const tpgid = Number(result.stdout.trim()); + return result.code === 0 && !result.timedOut && Number.isInteger(tpgid) && tpgid > 0 + ? tpgid + : null; + }), + Effect.orElseSucceed(() => null), + )); const customSubprocessInspector = options.subprocessInspector; const acquireSubprocessInspector = ( fallbackTimeout: Duration.Input, @@ -1820,8 +1879,20 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func inspector: Result.map( snapshot, (table): TerminalSubprocessInspector => - (terminalPid) => - Effect.succeed(deriveSubprocessInspectResult(table, terminalPid, platform)), + (terminalPid, shell) => { + const result = deriveSubprocessInspectResult(table, terminalPid, shell, platform); + // Only shell-named childless children are ambiguous; read the foreground group for those alone. + if ( + result.hasRunningSubprocess || + platform === "win32" || + (table.childrenByParent.get(terminalPid)?.length ?? 0) === 0 + ) { + return Effect.succeed(result); + } + return Effect.map(foregroundProcessGroup(terminalPid), (group) => + deriveSubprocessInspectResult(table, terminalPid, shell, platform, group), + ); + }, ), snapshotSucceeded, queryElapsedMs, @@ -2946,7 +3017,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func let mark = candidate.mark; const previous = quietSessions.get(session); quietSessions.delete(session); - const inspectResult = yield* subprocessInspector(terminalPid).pipe( + const inspectResult = yield* subprocessInspector(terminalPid, session.shell).pipe( Effect.asSome, Effect.catch((reason) => Effect.logWarning("failed to check terminal subprocess activity", { @@ -3741,7 +3812,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func yield* Effect.forEach( running, (session) => - inspector(session.pid).pipe( + inspector(session.pid, session.shell).pipe( Effect.flatMap((result) => result.hasRunningSubprocess || terminalActivityMark(session) !== marks.get(session.terminalId) From 96290af4e0d74f5385b2e1886543ce62757827c5 Mon Sep 17 00:00:00 2001 From: Yash Singh Date: Sun, 15 Nov 2026 04:00:54 -0600 Subject: [PATCH 004/124] fix(codex): keep browser access prompts available in full access (#37104) Co-authored-by: Julius Marminge Co-authored-by: Claude Opus 5.5 --- .../Adapters/CodexAdapterV2.test.ts | 144 +++++++++++++++++- .../Adapters/CodexAdapterV2.ts | 44 ++++-- .../src/provider/CodexMcpElicitation.test.ts | 4 +- .../src/provider/CodexMcpElicitation.ts | 6 +- .../effect-codex-app-server/src/replay.ts | 15 +- 5 files changed, 195 insertions(+), 18 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts index 01acc8e75f464..eb70b52ee47f1 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts @@ -225,7 +225,15 @@ describe("CodexAdapterV2 runtime policy", () => { assert.equal(auto.approvalPolicy, "on-request"); assert.equal(auto.approvalsReviewer, "auto_review"); assert.equal(auto.sandboxPolicy?.type, "workspaceWrite"); - assert.equal(fullAccess.approvalPolicy, "never"); + assert.deepEqual(fullAccess.approvalPolicy, { + granular: { + mcp_elicitations: true, + request_permissions: false, + rules: false, + sandbox_approval: false, + skill_approval: false, + }, + }); assert.equal(fullAccess.approvalsReviewer, "user"); assert.equal(fullAccess.sandboxPolicy?.type, "dangerFullAccess"); }), @@ -657,6 +665,10 @@ function codexReplayPreamble(input: { readonly prompt: string; readonly sentPrompt?: string; readonly effort?: string; + readonly turnPolicy?: Pick< + typeof CodexClient.V2TurnStartParams.Type, + "approvalPolicy" | "sandboxPolicy" + >; }): Array { return [ { @@ -754,6 +766,7 @@ function codexReplayPreamble(input: { approvalPolicy: "never", approvalsReviewer: "user", sandboxPolicy: { type: "dangerFullAccess" }, + ...input.turnPolicy, summary: "auto", ...(input.effort === undefined ? {} : { effort: input.effort }), collaborationMode: { @@ -1603,6 +1616,135 @@ describe("CodexAdapterV2 post-settle continuation", () => { }; }); + it.effect.each([ + { runtimeMode: "full-access", turnIdKind: "present" }, + { runtimeMode: "auto-accept-edits", turnIdKind: "null" }, + ] as const)( + "handles MCP access approvals in $runtimeMode with a $turnIdKind turn id", + ({ runtimeMode, turnIdKind }) => + Effect.gen(function* () { + const nativeThreadId = "mcp-approval-thread"; + const nativeTurnId = "mcp-approval-turn"; + const prompt = "Read the dashboard in Chrome."; + const pending = yield* Queue.unbounded(); + const harness = yield* makeCodexReplayHarness( + makeCodexReplayTranscript({ + scenario: `mcp-approval-${turnIdKind}`, + entries: [ + ...codexReplayPreamble({ + nativeThreadId, + nativeTurnId, + prompt, + ...(runtimeMode === "full-access" + ? {} + : { + turnPolicy: { + approvalPolicy: "on-request", + sandboxPolicy: { type: "workspaceWrite" }, + }, + }), + }), + { + type: "emit_inbound", + frame: { + id: 0, + method: "mcpServer/elicitation/request", + params: { + threadId: nativeThreadId, + turnId: turnIdKind === "null" ? null : nativeTurnId, + serverName: "browser", + mode: "form", + message: "Allow ChatGPT to use Google Chrome?", + requestedSchema: { + type: "object", + properties: { approval: { type: "string", enum: ["once", "always"] } }, + required: ["approval"], + }, + }, + }, + }, + { + type: "expect_outbound", + frame: { id: 0, result: { action: "accept", content: { approval: "once" } } }, + }, + { + type: "emit_inbound", + frame: { + method: "turn/completed", + params: { + threadId: nativeThreadId, + turn: makeCodexReplayTurn({ id: nativeTurnId, status: "completed" }), + }, + }, + }, + ], + }), + (event) => + event.type === "runtime_request.updated" && event.runtimeRequest.status === "pending" + ? Queue.offer(pending, event.runtimeRequest) + : Effect.void, + ); + const turnInput = makeCodexTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make(`mcp-approval-${turnIdKind}-attempt`), + text: prompt, + }); + yield* harness.runtime.startTurn({ + ...turnInput, + runtimePolicy: { ...turnInput.runtimePolicy, runtimeMode }, + }); + if (runtimeMode === "full-access") { + const request = yield* Effect.raceFirst( + harness.firstTerminal.pipe(Effect.as(undefined)), + Queue.take(pending), + ); + assert.isUndefined(request, "Full access must not wait for an MCP approval"); + assert.deepEqual( + harness.terminalEvents().map((event) => event.status), + ["completed"], + ); + return; + } + const request = yield* Effect.raceFirst( + Queue.take(pending), + harness.firstTerminal.pipe(Effect.andThen(Effect.die("MCP approval was not presented"))), + ); + assert.equal(request.kind, "mcp-elicitation"); + const approvals = harness.events.flatMap((event) => + event.type === "turn_item.updated" && + event.turnItem.type === "approval_request" && + event.turnItem.requestId === request.id + ? [event.turnItem] + : [], + ); + assert.deepEqual( + approvals.map((approval) => [approval.threadId, approval.appName, approval.prompt]), + [[harness.threadId, "Google Chrome", "Allow ChatGPT to use Google Chrome?"]], + ); + yield* harness.runtime.respondToRuntimeRequest({ + requestId: request.id, + decision: "accept", + }); + yield* harness.firstTerminal; + assert.deepEqual( + harness.terminalEvents().map((event) => event.status), + ["completed"], + ); + }).pipe( + Effect.scoped, + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + it.effect.each(["native", "ordinary"] as const)( "resumes Codex without a prompt only for an existing conversation (%s)", (mode) => diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts index ee35f0a61671a..4947f4da8a440 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts @@ -766,7 +766,16 @@ function codexRuntimeModeTurnDefaults(runtimeMode: RuntimeMode): { }; case "full-access": return { - approvalPolicy: "never", + // `never` also rejects browser and app access prompts before the client sees them. + approvalPolicy: { + granular: { + mcp_elicitations: true, + request_permissions: false, + rules: false, + sandbox_approval: false, + skill_approval: false, + }, + }, approvalsReviewer: "user", sandboxPolicy: { type: "dangerFullAccess", @@ -2526,19 +2535,25 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( ); }); - // Streaming updates repeatedly await this context, including retries before registration. - const awaitActiveTurn = Effect.fnUntraced(function* ( - nativeTurnId: string, + // Native events can arrive before their turn registers, so lookups retry briefly. + const awaitRegisteredTurn = Effect.fnUntraced(function* ( + lookup: Effect.Effect, attemptsRemaining = 1_000, ): Effect.fn.Return { - const context = (yield* Ref.get(activeTurns)).get(nativeTurnId); + const context = yield* lookup; if (context !== undefined || attemptsRemaining <= 0) { return context; } yield* Effect.yieldNow; - return yield* awaitActiveTurn(nativeTurnId, attemptsRemaining - 1); + return yield* awaitRegisteredTurn(lookup, attemptsRemaining - 1); }); + // Streaming updates repeatedly await this context; avoid a span for each update. + const awaitActiveTurn = (nativeTurnId: string) => + awaitRegisteredTurn( + Ref.get(activeTurns).pipe(Effect.map((turns) => turns.get(nativeTurnId))), + ); + const resolveItemEventContext = /* Provider events include streaming deltas; avoid a span for each update. */ Effect.fnUntraced(function* (nativeTurnId: string) { @@ -6044,29 +6059,34 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( yield* client.handleServerRequest("mcpServer/elicitation/request", (payload) => Effect.gen(function* () { - if (toMcpElicitationResponse(payload, "accept").action !== "accept") { - yield* Effect.logWarning("Declined an unsupported MCP elicitation.", { + const acceptance = toMcpElicitationResponse(payload, "accept"); + if (acceptance.action !== "accept") { + yield* Effect.logWarning("Cancelled an unsupported MCP elicitation.", { serverName: payload.serverName, mode: payload.mode, }); return { - action: "decline", + action: "cancel", } satisfies CodexSchema.McpServerElicitationRequestResponse; } const context = payload.turnId === undefined || payload.turnId === null - ? undefined + ? yield* awaitRegisteredTurn(findActiveTurnByNativeThreadId(payload.threadId)) : yield* awaitActiveTurn(payload.turnId); if (context === undefined) { yield* Effect.logWarning( - "Declined an MCP elicitation without an active Codex turn context.", + "Cancelled an MCP elicitation without an active Codex turn context.", { serverName: payload.serverName }, ); return { - action: "decline", + action: "cancel", } satisfies CodexSchema.McpServerElicitationRequestResponse; } + if (context.input.runtimePolicy.runtimeMode === "full-access") { + return acceptance; + } + const nativeRequestId = payload.mode === "url" ? payload.elicitationId diff --git a/apps/server/src/provider/CodexMcpElicitation.test.ts b/apps/server/src/provider/CodexMcpElicitation.test.ts index 66dd7ddb6e7d0..60a4b7fe8f72e 100644 --- a/apps/server/src/provider/CodexMcpElicitation.test.ts +++ b/apps/server/src/provider/CodexMcpElicitation.test.ts @@ -146,7 +146,7 @@ describe("Codex MCP elicitation approvals", () => { } satisfies EffectCodexSchema.McpServerElicitationRequestParams; NodeAssert.deepStrictEqual(toMcpElicitationResponse(inputRequest, "accept"), { - action: "decline", + action: "cancel", }); }); @@ -162,7 +162,7 @@ describe("Codex MCP elicitation approvals", () => { } satisfies EffectCodexSchema.McpServerElicitationRequestParams; NodeAssert.deepStrictEqual(toMcpElicitationResponse(urlRequest, "accept"), { - action: "decline", + action: "cancel", }); }); diff --git a/apps/server/src/provider/CodexMcpElicitation.ts b/apps/server/src/provider/CodexMcpElicitation.ts index eafe09e07e706..c8362dad1e84b 100644 --- a/apps/server/src/provider/CodexMcpElicitation.ts +++ b/apps/server/src/provider/CodexMcpElicitation.ts @@ -185,8 +185,10 @@ export function toMcpElicitationResponse( return { action: decision }; } + // T3 cannot answer these itself. `decline` reads as the user's refusal, and Codex's browser + // plugin saves it as a thread-wide site denial; `cancel` leaves no saved decision. if (payload.mode === "url") { - return { action: "decline" }; + return { action: "cancel" }; } const persist = @@ -217,7 +219,7 @@ export function toMcpElicitationResponse( } if (form?.required?.some((key) => !Object.hasOwn(content, key))) { - return { action: "decline" }; + return { action: "cancel" }; } return { diff --git a/packages/effect-codex-app-server/src/replay.ts b/packages/effect-codex-app-server/src/replay.ts index 7aba60a1cfd79..219feae25126a 100644 --- a/packages/effect-codex-app-server/src/replay.ts +++ b/packages/effect-codex-app-server/src/replay.ts @@ -203,7 +203,20 @@ function normalizeReplayFrame(value: unknown): unknown { normalized.params !== null ) { const params = { ...(normalized.params as Record) }; - if (params.approvalPolicy === "never") { + // Older recordings used `never` for full access, before MCP prompts were kept interactive. + if ( + params.approvalPolicy === "never" || + stableStringify(params.approvalPolicy) === + stableStringify({ + granular: { + mcp_elicitations: true, + request_permissions: false, + rules: false, + sandbox_approval: false, + skill_approval: false, + }, + }) + ) { delete params.approvalPolicy; } if ( From 46f3ab39f227467d62eeda595f043765816ef1fd Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 15 Nov 2026 11:03:21 +0100 Subject: [PATCH 005/124] fix(web): open terminal file links naming the server hostname (#37133) Co-authored-by: Julius Marminge --- apps/server/src/ws.ts | 2 + .../src/components/ThreadTerminalDrawer.tsx | 10 +-- apps/web/src/terminal-links.ts | 9 ++- packages/contracts/src/server.ts | 2 + packages/shared/src/fileLinks.test.ts | 17 ++++- packages/shared/src/fileLinks.ts | 17 ++++- ...l-hostname-file-link-opens-its-file.e2e.ts | 66 +++++++++++++++++++ 7 files changed, 111 insertions(+), 12 deletions(-) create mode 100644 scripts/scenarios/terminal-hostname-file-link-opens-its-file.e2e.ts diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts index c819d099938e2..4de72cce57d83 100644 --- a/apps/server/src/ws.ts +++ b/apps/server/src/ws.ts @@ -1,6 +1,7 @@ import { OrchestrationDispatchCommandError } from "@t3tools/contracts"; // @effect-diagnostics-next-line nodeBuiltinImport:off -- A synchronous digest inside the pure provider mapping. import * as NodeCrypto from "node:crypto"; +import * as HostProcess from "@t3tools/shared/HostProcess"; import * as Context from "effect/Context"; import * as NodeHttpServerRequest from "@effect/platform-node/NodeHttpServerRequest"; import * as SecureRandom from "effect/crypto/SecureRandom"; @@ -1836,6 +1837,7 @@ export const layerWsRpc = ( environment, auth, cwd: config.cwd, + hostname: yield* HostProcess.Hostname, keybindingsConfigPath: config.keybindingsConfigPath, keybindings: keybindingsConfig.keybindings, issues: keybindingsConfig.issues, diff --git a/apps/web/src/components/ThreadTerminalDrawer.tsx b/apps/web/src/components/ThreadTerminalDrawer.tsx index e2ab63f058ed1..caa98b60290f0 100644 --- a/apps/web/src/components/ThreadTerminalDrawer.tsx +++ b/apps/web/src/components/ThreadTerminalDrawer.tsx @@ -527,11 +527,14 @@ function TerminalSessionViewport({ const canReadFilesystem = useEnvironmentScope(environmentId, AuthFilesystemReadScope); const [linkPath, setLinkPath] = useState(null); const metadataThreadRef = useContext(FileMetadataThreadContext); + const serverConfig = useAtomValue(serverEnvironment.configValueAtom(environmentId)); + const hostname = serverConfig?.hostname; + const readHostname = useEffectEvent(() => hostname); const linkMetadataQuery = linkPath !== null && canOpenHostEditor && canReadFilesystem ? getFileMetadataQueryAtom( environmentId, - resolveTerminalLinkTarget(linkPath, cwd), + resolveTerminalLinkTarget(linkPath, cwd, hostname), cwd, metadataThreadRef, ) @@ -552,7 +555,7 @@ function TerminalSessionViewport({ return false; const query = getFileMetadataQueryAtom( environmentId, - resolveTerminalLinkTarget(text, cwd), + resolveTerminalLinkTarget(text, cwd, hostname), cwd, metadataThreadRef, ); @@ -563,7 +566,6 @@ function TerminalSessionViewport({ (result.value?.kind === "file" || result.value?.kind === "directory") ); }); - const serverConfig = useAtomValue(serverEnvironment.configValueAtom(environmentId)); const openInPreferredEditor = useOpenInPreferredEditor( environmentId, serverConfig?.availableEditors ?? [], @@ -1069,7 +1071,7 @@ function TerminalSessionViewport({ ); return; } - const target = resolveTerminalLinkTarget(text, cwd); + const target = resolveTerminalLinkTarget(text, cwd, readHostname()); void (async () => { const result = await openTerminalPath(target); if (result._tag === "Success" || isAtomCommandInterrupted(result)) { diff --git a/apps/web/src/terminal-links.ts b/apps/web/src/terminal-links.ts index 1881a322ee82c..f953f6dfee298 100644 --- a/apps/web/src/terminal-links.ts +++ b/apps/web/src/terminal-links.ts @@ -162,9 +162,12 @@ export function isTerminalUrl(value: string): boolean { return /^https?:\/\//iu.test(value); } -/** Explicit OSC 8 targets may be `file://` URIs; everything else is a path relative to `cwd`. */ -export function resolveTerminalLinkTarget(text: string, cwd: string): string { - const fileUrl = /^file:\/\//iu.test(text) ? parseFileUrlHref(text) : null; +/** + * Explicit OSC 8 targets may be `file://` URIs; everything else is a path relative to `cwd`. + * `hostname` is the terminal's environment's, which producers such as GNU `ls` put in the URI. + */ +export function resolveTerminalLinkTarget(text: string, cwd: string, hostname?: string): string { + const fileUrl = /^file:\/\//iu.test(text) ? parseFileUrlHref(text, hostname) : null; if (!fileUrl) return resolvePathLinkTarget(text, cwd); return formatFilePathPosition( splitFilePathPosition( diff --git a/packages/contracts/src/server.ts b/packages/contracts/src/server.ts index e2662bca07463..b46a2823e9153 100644 --- a/packages/contracts/src/server.ts +++ b/packages/contracts/src/server.ts @@ -685,6 +685,8 @@ export const ServerConfig = Schema.Struct({ environment: ExecutionEnvironmentDescriptor, auth: ServerAuthDescriptor, cwd: TrimmedNonEmptyString, + /** The machine's own name, which file URIs from its shells may carry. Older servers omit it. */ + hostname: Schema.optionalKey(Schema.String), keybindingsConfigPath: TrimmedNonEmptyString, keybindings: ResolvedKeybindingsConfig, issues: ServerConfigIssues, diff --git a/packages/shared/src/fileLinks.test.ts b/packages/shared/src/fileLinks.test.ts index e555d9ce184c8..9596bed6c7118 100644 --- a/packages/shared/src/fileLinks.test.ts +++ b/packages/shared/src/fileLinks.test.ts @@ -87,10 +87,21 @@ describe("parseFileUrlHref", () => { "D:/Programme/t3code/OpenInPicker.tsx", "#L69", ], - ["file://server/share/workspace-image.svg", "\\\\server\\share\\workspace-image.svg", ""], + [ + "file://server/share/workspace-image.svg", + "\\\\server\\share\\workspace-image.svg", + "", + "box-node", + ], ["file://localhost/home/me/notes.md", "/home/me/notes.md", ""], - ])("parses %s", (href, path, hash) => { - expect(parseFileUrlHref(href)).toEqual({ path, hash }); + // GNU `ls --hyperlink` names the machine's own hostname. + ["file://box-node/var/tmp/report.txt", "/var/tmp/report.txt", "", "Box-Node"], + ["file://1234/var/tmp/report.txt", "/var/tmp/report.txt", "", "1234"], + ["file://böx/var/tmp/report.txt", "/var/tmp/report.txt", "", "böx"], + ["file://server/share/report.txt", "\\\\server\\share\\report.txt", "", "09"], + ["file://a/share/report.txt", "\\\\a\\share\\report.txt", "", "a#b"], + ])("parses %s", (href, path, hash, localHostname?: string) => { + expect(parseFileUrlHref(href, localHostname)).toEqual({ path, hash }); }); it("keeps percent-encoding so the caller decodes once", () => { diff --git a/packages/shared/src/fileLinks.ts b/packages/shared/src/fileLinks.ts index f5af03deabf99..d3a3cc1ec6f42 100644 --- a/packages/shared/src/fileLinks.ts +++ b/packages/shared/src/fileLinks.ts @@ -9,9 +9,13 @@ const POSITION_SUFFIX_CAPTURE_PATTERN = /:(\d+)(?::(\d+)|-(\d+))?$/; const POSITION_HASH_PATTERN = /^#L(\d+)(?:C(\d+)|-L?(\d+))?$/i; -/** Keeps paths percent-encoded to avoid double decoding; non-localhost authorities become UNC shares. */ +/** + * Keeps paths percent-encoded to avoid double decoding. `localhost` and `localHostname` (the + * machine the path lives on) are local authorities; any other becomes a UNC share. + */ export function parseFileUrlHref( href: string, + localHostname?: string, ): { readonly path: string; readonly hash: string } | null { // Chromium on Windows parses any one-letter scheme as a drive letter // (`new URL("d:x")` is `file:///D:/x`), so the protocol check alone would turn @@ -21,7 +25,16 @@ export function parseFileUrlHref( const parsed = new URL(href); if (parsed.protocol.toLowerCase() !== "file:") return null; - const uncHostname = parsed.hostname.toLowerCase() === "localhost" ? "" : parsed.hostname; + const host = parsed.hostname.toLowerCase(); + let localHost = ""; + if (host && localHostname) { + try { + localHost = new URL(`file://${encodeURIComponent(localHostname)}/`).hostname; + } catch { + // System hostnames need not be valid URL authorities; they cannot match a file URI. + } + } + const uncHostname = host === "localhost" || host === localHost ? "" : parsed.hostname; const path = uncHostname ? `\\\\${uncHostname}${parsed.pathname.replaceAll("/", "\\")}` : parsed.pathname; diff --git a/scripts/scenarios/terminal-hostname-file-link-opens-its-file.e2e.ts b/scripts/scenarios/terminal-hostname-file-link-opens-its-file.e2e.ts new file mode 100644 index 0000000000000..515b03b8dd58b --- /dev/null +++ b/scripts/scenarios/terminal-hostname-file-link-opens-its-file.e2e.ts @@ -0,0 +1,66 @@ +/// +// The DOM lib types the function this scenario runs in the page. +import { expect } from "e2e"; +import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; + +import { T3_HOME } from "../e2e.config.ts"; +import { runPromise, test, WEB } from "./t3.ts"; + +const EDITOR_ARGS = `${T3_HOME}/editor-args.txt`; + +/** The file's lines, or none while it does not exist. */ +const readLines = (file: string) => + runPromise( + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + if (!(yield* fs.exists(file))) return []; + return (yield* fs.readFileString(file)).trim().split("\n"); + }), + ); + +test( + "Ctrl+click on a GNU ls --hyperlink filename, whose file URI names this machine, opens that file", + WEB, + async ({ t3, app, browser, screen }) => { + await runPromise( + Effect.flatMap(FileSystem.FileSystem, (fs) => fs.remove(EDITOR_ARGS, { force: true })), + ); + const project = await t3.project("demo", { "one.txt": "hello\n" }); + const thread = await t3.thread({ project, title: "List", messages: ["List it"] }); + await t3.open(thread); + + await screen.getByRole("button", "Toggle terminal drawer").tap(); + const input = screen.getByRole("textbox", "Terminal input"); + // The input mounts before fonts and WASM finish loading and its handlers are installed. + await expect(input).toHaveAttribute("data-workspace-focus-default", ""); + await input.focus(); + // GNU ls writes `file:///`. Enough rows to scroll, so every row is the link. + await browser.keyboard.type( + "for i in $(seq 1 100); do command ls --hyperlink=always --color=never -1 one.txt; done; touch ls.done\n", + ); + await expect.poll(() => readLines(`${project}/ls.done`), { timeout: 30_000 }).toEqual([""]); + await browser.setViewport({ width: 1000, height: 700 }); + await app.screenshot("listed-and-resized"); + + // The terminal draws on a canvas, so point at the first cells of its middle row. + const canvas = await browser.evaluate(() => { + const box = document + .querySelector('[aria-label="Terminal input"]') + ?.parentElement?.querySelector("canvas") + ?.getBoundingClientRect(); + return box ? { x: box.x, y: box.y, width: box.width, height: box.height } : null; + }); + if (!canvas) throw new Error("The terminal canvas is not on the page."); + await browser.mouse.move(canvas.x + 16, canvas.y + canvas.height / 2); + await browser.keyboard.down("Control"); + await browser.mouse.down(); + await browser.mouse.up(); + await browser.keyboard.up("Control"); + await app.screenshot("after-ctrl-click"); + + await expect + .poll(() => readLines(EDITOR_ARGS), { timeout: 15_000 }) + .toEqual(["--classic", `${project}/one.txt`]); + }, +); From 1b14aade4de1db0c82b8d61ccfd9c011b04c3810 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 15 Nov 2026 11:04:52 +0100 Subject: [PATCH 006/124] fix(mobile): recheck the current branch before selecting it (#37121) Co-authored-by: Julius Marminge --- .../features/threads/checkout-new-task-branch.test.ts | 4 ++-- .../src/features/threads/checkout-new-task-branch.ts | 9 +++------ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/apps/mobile/src/features/threads/checkout-new-task-branch.test.ts b/apps/mobile/src/features/threads/checkout-new-task-branch.test.ts index bee63a9775a05..328f56bcf0066 100644 --- a/apps/mobile/src/features/threads/checkout-new-task-branch.test.ts +++ b/apps/mobile/src/features/threads/checkout-new-task-branch.test.ts @@ -135,12 +135,12 @@ async function addRemote(remoteName: string, sourceRef = branch.name) { } describe("new-task branch checkout", () => { - it("switches to a free branch and clears its historical worktree path", async () => { + it("switches to a free branch despite stale current and worktree metadata", async () => { const previousWorktree = NodePath.join(directory, "previous-worktree"); await git("worktree", "add", previousWorktree, branch.name); await exec("git", ["-C", previousWorktree, "checkout", "-b", "other-branch"]); const result = await checkoutNewTaskBranch({ - branch: { ...branch, worktreePath: previousWorktree }, + branch: { ...branch, current: true, worktreePath: previousWorktree }, project: { environmentId, workspaceRoot: cwd }, workspaceMode: "local", readRefs, diff --git a/apps/mobile/src/features/threads/checkout-new-task-branch.ts b/apps/mobile/src/features/threads/checkout-new-task-branch.ts index fb7920dcdd5f6..a4f3a306b2eb0 100644 --- a/apps/mobile/src/features/threads/checkout-new-task-branch.ts +++ b/apps/mobile/src/features/threads/checkout-new-task-branch.ts @@ -34,13 +34,10 @@ export async function checkoutNewTaskBranch(input: { Cause.fail(new Error("The selected project is unavailable. Reconnect and try again.")), ); } - if (input.workspaceMode === "worktree" || input.branch.current) { - return AsyncResult.success( - input.branch.current ? { ...input.branch, worktreePath: null } : input.branch, - ); - } + if (input.workspaceMode === "worktree") return AsyncResult.success(input.branch); - // Remote refs omit local worktrees, and historical thread metadata can be stale. + // The picked ref's current flag, remote refs' missing worktrees and historical + // thread metadata can all be stale, so Git is asked again before choosing. const localName = localBranchName(input.branch); let cursor: number | undefined; for (;;) { From 834eb51301a447e6f4a8f5f517fb4eb75e00a783 Mon Sep 17 00:00:00 2001 From: shivam <91240327+shivamhwp@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:46:03 +0530 Subject: [PATCH 007/124] fix(acp): wait for session/load to reply before prompting (#37139) Co-authored-by: Julius Marminge --- apps/server/scripts/acp-mock-agent.ts | 18 +++++ .../provider/acp/AcpJsonRpcConnection.test.ts | 66 +++++++++++++++++++ .../src/server/AcpSessionRuntime.ts | 23 ++++--- .../provider-grok/src/server/acpSupport.ts | 2 + 4 files changed, 101 insertions(+), 8 deletions(-) diff --git a/apps/server/scripts/acp-mock-agent.ts b/apps/server/scripts/acp-mock-agent.ts index a97aab9cdf2b1..78373d82e1487 100644 --- a/apps/server/scripts/acp-mock-agent.ts +++ b/apps/server/scripts/acp-mock-agent.ts @@ -70,6 +70,9 @@ const failLoadSession = process.env.T3_ACP_FAIL_LOAD_SESSION === "1"; const failLoadSessionAfterConfigReplay = process.env.T3_ACP_FAIL_LOAD_SESSION_AFTER_CONFIG_REPLAY === "1"; const emitLoadReplay = process.env.T3_ACP_EMIT_LOAD_REPLAY === "1"; +const loadReplyDelayMs = Number(process.env.T3_ACP_LOAD_REPLY_DELAY_MS ?? "0"); +const loadNeverReplies = process.env.T3_ACP_LOAD_NEVER_REPLIES === "1"; +const rejectPromptUntilLoaded = process.env.T3_ACP_REJECT_PROMPT_UNTIL_LOADED === "1"; const failPrompt = process.env.T3_ACP_FAIL_PROMPT === "1"; // JSON for the `data` of an "Internal error" response to `session/new`, as // Antigravity sends when its runtime cannot reach its local harness. @@ -122,6 +125,7 @@ let currentAgent = ""; let authenticated = !requiresAuthentication; let promptCount = 0; const cancelledSessions = new Set(); +const sessionsLoading = new Set(); let configuredProvider: AcpSchema.ProviderCurrentConfig | null = null; function promptIdFromRequestMeta( @@ -687,6 +691,7 @@ const program = Effect.gen(function* () { yield* agent.handleLoadSession((request) => Effect.gen(function* () { const requestedSessionId = String(request.sessionId ?? sessionId); + sessionsLoading.add(requestedSessionId); if (loadedCommandNames !== undefined) { yield* agent.client.sessionUpdate({ sessionId: requestedSessionId, @@ -729,6 +734,13 @@ const program = Effect.gen(function* () { content: { type: "text", text: "replay" }, }, }); + if (loadNeverReplies) { + return yield* Effect.never; + } + if (Number.isFinite(loadReplyDelayMs) && loadReplyDelayMs > 0) { + yield* Effect.sleep(`${loadReplyDelayMs} millis`); + } + sessionsLoading.delete(requestedSessionId); return { configOptions: configOptions(), }; @@ -868,6 +880,12 @@ const program = Effect.gen(function* () { yield* agent.handlePrompt((request) => Effect.gen(function* () { const requestedSessionId = String(request.sessionId ?? sessionId); + if (rejectPromptUntilLoaded && sessionsLoading.has(requestedSessionId)) { + return yield* new AcpError.AcpRequestError({ + code: -32002, + errorMessage: `Resource not found: ${requestedSessionId}`, + }); + } beginAcpMockPrompt(cancelledSessions, requestedSessionId); promptCount += 1; if (idleError === "grok-background" && promptCount === 1) { diff --git a/apps/server/src/provider/acp/AcpJsonRpcConnection.test.ts b/apps/server/src/provider/acp/AcpJsonRpcConnection.test.ts index 4c21e400f3c8e..5dacd1f2d9267 100644 --- a/apps/server/src/provider/acp/AcpJsonRpcConnection.test.ts +++ b/apps/server/src/provider/acp/AcpJsonRpcConnection.test.ts @@ -1286,6 +1286,72 @@ describe("AcpSessionRuntime", () => { ), ); + // regression: #37106 prompt sent before session/load replied + it.live("does not prompt before session/load replies after replay goes idle", () => + Effect.gen(function* () { + const runtime = yield* AcpSessionRuntime.AcpSessionRuntime; + yield* runtime.start(); + + const result = yield* runtime.prompt({ + prompt: [{ type: "text", text: "hi" }], + }); + expect(result.stopReason).toBe("end_turn"); + }).pipe( + Effect.provide( + AcpSessionRuntime.layer({ + authMethodId: "test", + spawn: { + command: mockAgentCommand, + args: mockAgentArgs, + env: { + T3_ACP_EMIT_LOAD_REPLAY: "1", + T3_ACP_LOAD_REPLY_DELAY_MS: "500", + T3_ACP_REJECT_PROMPT_UNTIL_LOADED: "1", + }, + }, + cwd: process.cwd(), + resumeSessionId: "mock-session-1", + sessionLoadReplayIdleGap: "50 millis", + clientInfo: { name: "t3-test", version: "0.0.0" }, + }), + ), + Effect.scoped, + Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), + ), + ); + + it.live("treats replay idle as loaded when the agent opts in", () => + Effect.gen(function* () { + const runtime = yield* AcpSessionRuntime.AcpSessionRuntime; + const started = yield* runtime.start(); + + expect(started.sessionSetupResult._meta).toMatchObject({ + t3SessionLoadReady: "replay_idle", + }); + }).pipe( + Effect.provide( + AcpSessionRuntime.layer({ + authMethodId: "test", + spawn: { + command: mockAgentCommand, + args: mockAgentArgs, + env: { + T3_ACP_EMIT_LOAD_REPLAY: "1", + T3_ACP_LOAD_NEVER_REPLIES: "1", + }, + }, + cwd: process.cwd(), + resumeSessionId: "mock-session-1", + sessionLoadReplayIdleGap: "50 millis", + sessionLoadReplayIdleReadiness: true, + clientInfo: { name: "t3-test", version: "0.0.0" }, + }), + ), + Effect.scoped, + Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), + ), + ); + it.effect("rejects invalid config option values before sending session/set_config_option", () => { const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "acp-runtime-")); const requestLogPath = NodePath.join(tempDir, "requests.ndjson"); diff --git a/packages/provider-acp/src/server/AcpSessionRuntime.ts b/packages/provider-acp/src/server/AcpSessionRuntime.ts index 59d701eddb97b..21ae7f7fb1990 100644 --- a/packages/provider-acp/src/server/AcpSessionRuntime.ts +++ b/packages/provider-acp/src/server/AcpSessionRuntime.ts @@ -101,6 +101,8 @@ export interface AcpSessionRuntimeOptions { readonly resumeMethod?: "load" | "resume"; readonly sessionLoadTimeout?: Duration.Input; readonly sessionLoadReplayIdleGap?: Duration.Input; + // Treat replay going quiet as loaded, for agents whose session/load may never reply. + readonly sessionLoadReplayIdleReadiness?: boolean; readonly interruptPromptOnCancel?: boolean; readonly cancelMeta?: EffectAcpSchema.CancelNotification["_meta"]; readonly initializeMeta?: EffectAcpSchema.InitializeRequest["_meta"]; @@ -1998,14 +2000,19 @@ export const make = Effect.fn("AcpSessionRuntime.make")(function* ( status: "started", }); - const idleFiber = yield* waitForSessionLoadReplayIdle({ - gateRef: sessionLoadGateRef, - }).pipe(Effect.forkIn(runtimeScope)); - const loaded = yield* Effect.raceFirst( - acp.agent.loadSession(loadPayload), - Fiber.join(idleFiber), - ).pipe( - Effect.ensuring(Fiber.interrupt(idleFiber).pipe(Effect.ignore)), + const loadOrReplayIdle = + options.sessionLoadReplayIdleReadiness === true + ? Effect.gen(function* () { + const idleFiber = yield* waitForSessionLoadReplayIdle({ + gateRef: sessionLoadGateRef, + }).pipe(Effect.forkIn(runtimeScope)); + return yield* Effect.raceFirst( + acp.agent.loadSession(loadPayload), + Fiber.join(idleFiber), + ).pipe(Effect.ensuring(Fiber.interrupt(idleFiber).pipe(Effect.ignore))); + }) + : acp.agent.loadSession(loadPayload); + const loaded = yield* loadOrReplayIdle.pipe( Effect.timeoutOption(sessionLoadTimeout), Effect.flatMap((result) => Effect.fromOption( diff --git a/packages/provider-grok/src/server/acpSupport.ts b/packages/provider-grok/src/server/acpSupport.ts index a7d1d5ed3b0cd..729ce24d71551 100644 --- a/packages/provider-grok/src/server/acpSupport.ts +++ b/packages/provider-grok/src/server/acpSupport.ts @@ -141,6 +141,8 @@ export const makeGrokAcpRuntime = Effect.fn("GrokAcpSupport.makeGrokAcpRuntime") authMethodId: resolveGrokAuthMethodId(input.environment), cancelMeta: { ...input.cancelMeta, ...GROK_ACP_CANCEL_META }, initializeMeta: GROK_ACP_INITIALIZE_META, + // Grok's session/load can stay unanswered after replay finishes. + sessionLoadReplayIdleReadiness: true, ...grokAcpRuntimeProcessOwnership(processGroupPlatform), }), ); From d8c7855a884c58715c2d66ca82a3334bd6bc398c Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 15 Nov 2026 11:30:52 +0100 Subject: [PATCH 008/124] fix(web): terminal mouse reports hit the drawn cell (#37148) Co-authored-by: Julius Marminge --- apps/web/src/terminal/ghostty/core.test.ts | 55 +++++++++ apps/web/src/terminal/ghostty/core.ts | 64 ++++++++-- .../terminal-drags-tmux-pane-divider.e2e.ts | 110 ++++++++++++++++++ 3 files changed, 219 insertions(+), 10 deletions(-) create mode 100644 scripts/scenarios/terminal-drags-tmux-pane-divider.e2e.ts diff --git a/apps/web/src/terminal/ghostty/core.test.ts b/apps/web/src/terminal/ghostty/core.test.ts index 27a10f2fd9f72..147af94a10d16 100644 --- a/apps/web/src/terminal/ghostty/core.test.ts +++ b/apps/web/src/terminal/ghostty/core.test.ts @@ -485,3 +485,58 @@ describe("GhosttyTerminalCore scrollback", () => { } }); }); + +describe("GhosttyTerminalCore mouse encoding", () => { + it("reports the cell drawn under the pointer on a fractional grid", async () => { + // The default 13px font measures a fractional 7.82666015625px cell; rows are 18px. + const cellWidth = 7.82666015625; + const cellHeight = 18; + const core = await GhosttyTerminalCore.create( + 54, + 15, + cellWidth, + cellHeight, + { + foreground: { r: 255, g: 255, b: 255 }, + background: { r: 0, g: 0, b: 0 }, + cursor: { r: 255, g: 255, b: 255 }, + }, + () => {}, + ); + const mouse = ( + x: number, + y: number, + paddingTop: number, + screenWidth = 437, + action: "press" | "release" = "press", + ) => + core.encodeMouse({ + action, + button: 1, + mods: 0, + x, + y, + screenWidth, + screenHeight: 279, + cellWidth, + cellHeight, + paddingLeft: 4, + paddingRight: 4, + paddingTop, + paddingBottom: 279 - paddingTop - 15 * cellHeight, + anyButtonPressed: true, + }); + try { + core.write("\x1b[?1002h\x1b[?1006h"); + expect(mouse(218.5, 140, 4)).toBe("\x1b[<0;28;8M"); + expect(mouse(4 + 53.5 * cellWidth, 140, 4)).toBe("\x1b[<0;54;8M"); + expect(mouse(10, 4.5 + cellHeight + 0.2, 4.5)).toBe("\x1b[<0;1;2M"); + expect(mouse(304, 140, 4, 305)).toBe("\x1b[<0;37;8M"); + expect(mouse(500, 140, 4, 305, "release")).toBe("\x1b[<0;37;8m"); + core.write("\x1b[?1016h"); + expect(mouse(218.5, 22, 4.5)).toBe("\x1b[<0;215;17M"); + } finally { + core.dispose(); + } + }); +}); diff --git a/apps/web/src/terminal/ghostty/core.ts b/apps/web/src/terminal/ghostty/core.ts index 8311a1a95f503..dae62e989d3ff 100644 --- a/apps/web/src/terminal/ghostty/core.ts +++ b/apps/web/src/terminal/ghostty/core.ts @@ -165,6 +165,31 @@ export interface GhosttyMouseInput { const decoder = new TextDecoder("utf-8", { ignoreBOM: true }); const encoder = new TextEncoder(); +// The encoder only takes whole-pixel cells, so cell reports rescale the pointer onto that grid. +function encoderAxis( + position: number, + screen: number, + cell: number, + paddingStart: number, + paddingEnd: number, + pixelReports: boolean, +) { + const wholeCell = Math.max(1, Math.round(cell)); + if (pixelReports) { + return { position, screen, cell: wholeCell, paddingStart, paddingEnd }; + } + const scale = wholeCell / cell; + const start = Math.round(paddingStart); + const end = Math.round(paddingEnd); + return { + position: Math.max(0, start + (position - paddingStart) * scale), + screen: start + end + Math.floor((screen - paddingStart - paddingEnd) / cell) * wholeCell, + cell: wholeCell, + paddingStart: start, + paddingEnd: end, + }; +} + function blend(foreground: GhosttyColor, background: GhosttyColor): GhosttyColor { const channel = (front: number, back: number) => Math.floor((front * 155 + back * 100) / 255); return { @@ -638,18 +663,37 @@ export class GhosttyTerminalCore { this.terminal, ); + // SGR pixel reports (DEC 1016) must name surface pixels, so they skip the rescale. + const pixelReports = this.modeEnabled(1016); + const x = encoderAxis( + input.x, + input.screenWidth, + input.cellWidth, + input.paddingLeft, + input.paddingRight, + pixelReports, + ); + const y = encoderAxis( + input.y, + input.screenHeight, + input.cellHeight, + input.paddingTop, + input.paddingBottom, + pixelReports, + ); + const sizeLayout = this.runtime.layout("GhosttyMouseEncoderSize"); const size = this.runtime.alloc(sizeLayout.size); for (const [field, value] of [ ["size", sizeLayout.size], - ["screen_width", input.screenWidth], - ["screen_height", input.screenHeight], - ["cell_width", input.cellWidth], - ["cell_height", input.cellHeight], - ["padding_top", input.paddingTop], - ["padding_bottom", input.paddingBottom], - ["padding_right", input.paddingRight], - ["padding_left", input.paddingLeft], + ["screen_width", x.screen], + ["screen_height", y.screen], + ["cell_width", x.cell], + ["cell_height", y.cell], + ["padding_top", y.paddingStart], + ["padding_bottom", y.paddingEnd], + ["padding_right", x.paddingEnd], + ["padding_left", x.paddingStart], ] as const) { this.runtime.setField(size, "GhosttyMouseEncoderSize", field, Math.max(0, Math.round(value))); } @@ -675,8 +719,8 @@ export class GhosttyTerminalCore { const positionLayout = this.runtime.layout("GhosttyMousePosition"); const position = this.runtime.alloc(positionLayout.size); const positionView = this.runtime.view(position, positionLayout.size); - positionView.setFloat32(positionLayout.fields.x!.offset, input.x, true); - positionView.setFloat32(positionLayout.fields.y!.offset, input.y, true); + positionView.setFloat32(positionLayout.fields.x!.offset, x.position, true); + positionView.setFloat32(positionLayout.fields.y!.offset, y.position, true); this.runtime.call("ghostty_mouse_event_set_position", this.mouseEvent, position); this.runtime.free(position, positionLayout.size); diff --git a/scripts/scenarios/terminal-drags-tmux-pane-divider.e2e.ts b/scripts/scenarios/terminal-drags-tmux-pane-divider.e2e.ts new file mode 100644 index 0000000000000..e58c1ac6cc4cb --- /dev/null +++ b/scripts/scenarios/terminal-drags-tmux-pane-divider.e2e.ts @@ -0,0 +1,110 @@ +/// +// The DOM lib types the function this scenario runs in the page. +import { expect } from "e2e"; +import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; + +import { run } from "./shell.ts"; +import { runPromise, test, WEB } from "./t3.ts"; + +const SOCKET = `t3-scenario-divider-${process.pid}`; + +const tmux = (...args: ReadonlyArray) => + runPromise( + run("tmux", ["-L", SOCKET, ...args], { + env: { PATH: process.env.PATH ?? "", HOME: process.env.HOME ?? "" }, + allowExit: [0, 1], + }), + ); + +const paneWidths = async () => + (await tmux("list-panes", "-F", "#{pane_width}")).stdout.trim().split("\n").filter(Boolean); + +test( + "Dragging a tmux pane divider in the web terminal resizes the panes", + WEB, + async ({ t3, app, browser, screen }) => { + const project = await t3.project("demo"); + const thread = await t3.thread({ project, title: "Panes", messages: ["hello"] }); + const read = (file: string) => + runPromise( + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + return yield* fs.readFileString(`${project}/${file}`); + }).pipe(Effect.orElseSucceed(() => "")), + ); + await t3.open(thread); + + await screen.getByRole("button", "Toggle terminal drawer").tap(); + const terminal = screen.getByLabel("Terminal input"); + + // Input sent before the shell runs is dropped, so retype until it lands. + await expect + .poll( + async () => { + await terminal.focus(); + await browser.keyboard.type(`echo ready > '${project}/shell-ready'`); + await browser.keyboard.press("Enter"); + return read("shell-ready"); + }, + { timeout: 30_000 }, + ) + .toBe("ready\n"); + + try { + await terminal.focus(); + await browser.keyboard.type( + `tmux -L ${SOCKET} -f /dev/null new-session \\; set -g mouse on \\; set -g status off \\; split-window -h -l 30%`, + ); + await browser.keyboard.press("Enter"); + await expect.poll(async () => (await paneWidths()).length, { timeout: 15_000 }).toBe(2); + const [leftWidth] = await paneWidths(); + + // Find the divider tmux drew from the canvas pixels of an empty row, not from cell math. + const findDivider = () => + browser.evaluate(() => { + const input = document.querySelector('textarea[aria-label="Terminal input"]'); + const canvas = input?.parentElement?.querySelector("canvas"); + const context = canvas?.getContext("2d"); + if (!canvas || !context) return null; + const bounds = canvas.getBoundingClientRect(); + const ratio = canvas.width / bounds.width; + const row = Math.floor(canvas.height * 0.75); + const pixels = context.getImageData(0, row, canvas.width, 1).data; + const lit: number[] = []; + for (let x = 0; x < canvas.width; x += 1) { + const offset = x * 4; + const difference = + Math.abs(pixels[offset]! - pixels[0]!) + + Math.abs(pixels[offset + 1]! - pixels[1]!) + + Math.abs(pixels[offset + 2]! - pixels[2]!); + if (difference > 90) lit.push(x); + } + const first = lit[0]; + const last = lit.at(-1); + if (first === undefined || last === undefined || last - first > 4 * ratio) return null; + return { + x: bounds.left + (first + last + 1) / 2 / ratio, + y: bounds.top + (row + 0.5) / ratio, + }; + }); + await expect.poll(findDivider, { timeout: 10_000 }).not.toBeNull(); + const divider = (await findDivider())!; + await app.screenshot("before-drag"); + + await browser.mouse.move(divider.x, divider.y); + await browser.mouse.down(); + for (let step = 1; step <= 8; step += 1) { + await browser.mouse.move(divider.x - step * 10, divider.y); + } + await browser.mouse.up(); + + await app.screenshot("after-drag"); + await expect + .poll(async () => Number((await paneWidths())[0]), { timeout: 5_000 }) + .toBeLessThan(Number(leftWidth)); + } finally { + await tmux("kill-server"); + } + }, +); From def4cd4884855e530b09e2df12585a48021569cf Mon Sep 17 00:00:00 2001 From: Fuksss Date: Sun, 15 Nov 2026 11:49:21 +0100 Subject: [PATCH 009/124] fix(server): run_scheduled_task_now returns why a started run failed (#37149) Co-authored-by: Julius Marminge --- apps/server/src/mcp/toolkits/core.test.ts | 48 +++++++++++++++++++ .../src/mcp/toolkits/thread/handlers.ts | 1 + apps/server/src/mcp/toolkits/thread/tools.ts | 1 + 3 files changed, 50 insertions(+) diff --git a/apps/server/src/mcp/toolkits/core.test.ts b/apps/server/src/mcp/toolkits/core.test.ts index 0e9b5cb557d41..81614e5eb4274 100644 --- a/apps/server/src/mcp/toolkits/core.test.ts +++ b/apps/server/src/mcp/toolkits/core.test.ts @@ -1126,6 +1126,54 @@ it.effect.each([ ), ); +it.effect("run_scheduled_task_now reports why a started run failed to dispatch", () => + Effect.gen(function* () { + const server = yield* McpServer.McpServer; + const result = yield* server + .callTool({ name: "run_scheduled_task_now", arguments: { taskId: "task-full-access" } }) + .pipe( + Effect.provideService( + McpInvocationContext.McpInvocationContext, + clientScope("full-access"), + ), + Effect.provideService(McpSchema.McpServerClient, client), + ); + expect(result.isError).toBe(false); + expect(result.structuredContent).toMatchObject({ + threadId: null, + lastRunStatus: "failed", + lastRunError: "the provider is disabled", + }); + }).pipe( + Effect.provide( + McpHttpServer.layerThreadToolkit.pipe( + Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), + Layer.provide(NodeCrypto.layer), + Layer.provide(Layer.mock(ThreadManagement.ThreadManagementService)({})), + Layer.provide(Layer.mock(ThreadSearch.ThreadSearch)({})), + Layer.provide(Layer.mock(PeerForwarding.PeerForwarding)({})), + Layer.provide(Layer.mock(PeerLinkRequests.PeerLinkRequests)({})), + Layer.provide(Layer.mock(PeerLinks.PeerLinks)({})), + Layer.provide( + Layer.mock(ScheduledTaskService.ScheduledTaskService)({ + list: () => + Effect.succeed({ tasks: [scheduledTask("task-full-access", "full-access")] }), + runNow: () => + Effect.succeed({ + threadId: null, + task: { + ...(scheduledTask("task-full-access", "full-access") as object), + lastRunStatus: "failed" as const, + lastRunError: "the provider is disabled", + }, + } as never), + }), + ), + ), + ), + ), +); + function scheduledTask(id: string, runtimeMode: "auto" | "full-access"): never { return { id, diff --git a/apps/server/src/mcp/toolkits/thread/handlers.ts b/apps/server/src/mcp/toolkits/thread/handlers.ts index 03b796c6da892..972174638e10f 100644 --- a/apps/server/src/mcp/toolkits/thread/handlers.ts +++ b/apps/server/src/mcp/toolkits/thread/handlers.ts @@ -134,6 +134,7 @@ export const layer = McpToolAccess.toLayer(ThreadToolkit, { link: McpInvocationContext.threadLinkFor(scope, threadId), }), lastRunStatus: task.lastRunStatus, + lastRunError: task.lastRunError, runCount: task.runCount, nextRunAt: task.nextRunAt, }; diff --git a/apps/server/src/mcp/toolkits/thread/tools.ts b/apps/server/src/mcp/toolkits/thread/tools.ts index bea03c83c8465..d6c9a34565ddb 100644 --- a/apps/server/src/mcp/toolkits/thread/tools.ts +++ b/apps/server/src/mcp/toolkits/thread/tools.ts @@ -232,6 +232,7 @@ const ScheduledTaskRunTool = Tool.make("run_scheduled_task_now", { threadId: ScheduledTask.fields.threadId, link: Schema.optional(Schema.String), lastRunStatus: ScheduledTask.fields.lastRunStatus, + lastRunError: ScheduledTask.fields.lastRunError, runCount: NonNegativeInt, nextRunAt: ScheduledTask.fields.nextRunAt, }), From 70abb6c002c093ddb44beb0ccba4424f446e3f71 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 15 Nov 2026 02:54:05 -0800 Subject: [PATCH 010/124] fix(azure-devops): pull request titles like `@notes.txt` are saved as typed (#37110) --- .../src/server/AzureDevOpsCli.test.ts | 30 +++++++++++ .../src/server/AzureDevOpsCli.ts | 28 +++++++--- .../server/AzureDevOpsPullRequestCli.test.ts | 30 +++++++++++ .../src/server/AzureDevOpsPullRequestCli.ts | 54 ++++++++++++------- 4 files changed, 115 insertions(+), 27 deletions(-) diff --git a/packages/source-control-azure-devops/src/server/AzureDevOpsCli.test.ts b/packages/source-control-azure-devops/src/server/AzureDevOpsCli.test.ts index f2052fff8fd54..200f2d67fdb80 100644 --- a/packages/source-control-azure-devops/src/server/AzureDevOpsCli.test.ts +++ b/packages/source-control-azure-devops/src/server/AzureDevOpsCli.test.ts @@ -287,6 +287,36 @@ describe("AzureDevOpsCli.layer", () => { }).pipe(Effect.provide(layer)), ); + it.effect("sends a title that looks like `@path` as text, not as a file for az to read", () => + Effect.gen(function* () { + const az = yield* AzureDevOpsCli.AzureDevOpsCli; + for (const title of ["@notes.txt", "a=@notes.txt"]) { + mockRun.mockReturnValueOnce(Effect.succeed(processOutput("{}"))); + yield* az.createPullRequest({ + cwd: "/somewhere", + baseBranch: "main", + headSelector: "feature", + title, + bodyFile: "/tmp/body.md", + }); + + // az replaces an argument that starts with `@`, or whose text after the first `=` does, with that file's contents. + const fileReads = (mockRun.mock.calls.at(-1)?.[0].args ?? []).flatMap((arg) => { + const value = arg.startsWith("@") ? arg : arg.slice(arg.indexOf("=") + 1); + return (arg.startsWith("@") || arg.includes("=")) && value.startsWith("@") + ? [value.slice(1)] + : []; + }); + expect(fileReads).not.toContain("notes.txt"); + expect(mockRun.mock.calls.at(-1)?.[0].stdin).toBe( + title.startsWith("@") ? title : undefined, + ); + // az expands a separate `@file` value a second time, so a body of `@notes.txt` read notes.txt. + expect(mockRun.mock.calls.at(-1)?.[0].args).toContain("--description=@/tmp/body.md"); + } + }).pipe(Effect.provide(layer)), + ); + it.effect("preserves VCS causes without copying upstream details into messages", () => Effect.gen(function* () { const cause = new VcsProcessExitError({ diff --git a/packages/source-control-azure-devops/src/server/AzureDevOpsCli.ts b/packages/source-control-azure-devops/src/server/AzureDevOpsCli.ts index 7019b59bdf7ee..4052429d18a57 100644 --- a/packages/source-control-azure-devops/src/server/AzureDevOpsCli.ts +++ b/packages/source-control-azure-devops/src/server/AzureDevOpsCli.ts @@ -223,6 +223,7 @@ export class AzureDevOpsCli extends Context.Service< readonly maxOutputBytes?: number; /** The remote T3 selected; without one, the checkout's primary remote scopes the command. */ readonly remoteUrl?: string; + readonly stdin?: string; }) => Effect.Effect; readonly listPullRequests: (input: { @@ -507,6 +508,15 @@ export const azureCliInvocation = Effect.fn("AzureDevOpsCli.invocation")(functio } satisfies AzureCliInvocation); }); +/** + * `--option=value` for free text. az replaces a value starting with `@` by that file's + * contents; `@-` reads it from stdin instead, which reaches az wherever it runs (WSL included). + */ +export const textOption = (option: string, value: string) => + value.startsWith("@") + ? { arg: `${option}=@-`, stdin: value } + : { arg: `${option}=${value}`, stdin: undefined }; + /** @public */ export const make = Effect.gen(function* () { const { process } = yield* SourceControlHost.SourceControlHost; @@ -554,6 +564,7 @@ export const make = Effect.gen(function* () { args: [...invocation.args, ...args], cwd: input.cwd, ...(invocation.env === undefined ? {} : { env: invocation.env }), + ...(input.stdin === undefined ? {} : { stdin: input.stdin }), classifyFailure: classifyAzFailure, notFoundDetail: "Pull request not found.", timeoutMs: input.timeoutMs ?? DEFAULT_TIMEOUT_MS, @@ -717,6 +728,9 @@ export const make = Effect.gen(function* () { ); }), createPullRequest: Effect.fn("AzureDevOpsCli.createPullRequest")(function (input) { + // A full ref keeps a branch named `@...` from reading as a file; az accepts either form. + const branch = (name: string) => (name.startsWith("@") ? `refs/heads/${name}` : name); + const title = textOption("--title", input.title); return execute({ cwd: input.cwd, ...remoteOf(input), @@ -727,15 +741,13 @@ export const make = Effect.gen(function* () { "--only-show-errors", "--detect", "true", - "--target-branch", - input.target?.refName ?? input.baseBranch, - "--source-branch", - SourceControlProvider.sourceBranch(input), - "--title", - input.title, - "--description", - `@${input.bodyFile}`, + `--target-branch=${branch(input.target?.refName ?? input.baseBranch)}`, + `--source-branch=${branch(SourceControlProvider.sourceBranch(input))}`, + title.arg, + // Joined: az expands a separate `@file` value again, so a body of `@path` read that file. + `--description=@${input.bodyFile}`, ], + ...(title.stdin === undefined ? {} : { stdin: title.stdin }), }).pipe(Effect.asVoid); }), getDefaultBranch: Effect.fn("AzureDevOpsCli.getDefaultBranch")(function (input) { diff --git a/packages/source-control-azure-devops/src/server/AzureDevOpsPullRequestCli.test.ts b/packages/source-control-azure-devops/src/server/AzureDevOpsPullRequestCli.test.ts index 80c02ae847131..1a2c34d8f0e08 100644 --- a/packages/source-control-azure-devops/src/server/AzureDevOpsPullRequestCli.test.ts +++ b/packages/source-control-azure-devops/src/server/AzureDevOpsPullRequestCli.test.ts @@ -447,6 +447,36 @@ layer("AzureDevOpsPullRequestCli.layer", (it) => { }), ); + it.effect( + "sends a title or description that looks like `@path` as text, not as a file for az to read", + () => + Effect.gen(function* () { + mockedExecute.mockReturnValue(Effect.succeed(output("{}"))); + const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli; + + yield* cli.updatePullRequest({ + cwd: "/w", + number: 42, + title: "@notes.txt", + body: "@notes.txt", + }); + + // az replaces an argument that starts with `@`, or whose text after the first `=` does, with that file's contents. + const fileReads = argsOfCall(0).flatMap((arg) => { + const value = arg.startsWith("@") ? arg : arg.slice(arg.indexOf("=") + 1); + return (arg.startsWith("@") || arg.includes("=")) && value.startsWith("@") + ? [value.slice(1)] + : []; + }); + expect(fileReads).not.toContain("notes.txt"); + // Each `@` value goes over stdin, which reaches az wherever it runs; one per call. + expect(mockedExecute.mock.calls.map(([call]) => call.stdin)).toEqual([ + "@notes.txt", + "@notes.txt", + ]); + }), + ); + it.effect("names the head's blob as what a cleared file was cleared at", () => Effect.gen(function* () { mockedExecute diff --git a/packages/source-control-azure-devops/src/server/AzureDevOpsPullRequestCli.ts b/packages/source-control-azure-devops/src/server/AzureDevOpsPullRequestCli.ts index a1b517282204f..7ed77ab5a4ec7 100644 --- a/packages/source-control-azure-devops/src/server/AzureDevOpsPullRequestCli.ts +++ b/packages/source-control-azure-devops/src/server/AzureDevOpsPullRequestCli.ts @@ -818,25 +818,41 @@ export const make = Effect.gen(function* () { ), updatePullRequest: Effect.fn("AzureDevOpsPullRequestCli.updatePullRequest")(function (input) { - return azure - .execute({ - cwd: input.cwd, - args: [ - "repos", - "pr", - "update", - ...detectArgs, - "--id", - String(input.number), - // One argv entry preserves leading dashes and multi-string descriptions. - ...(input.title === undefined ? [] : [`--title=${input.title}`]), - ...(input.body === undefined ? [] : [`--description=${input.body}`]), - "--only-show-errors", - "--output", - "json", - ], - }) - .pipe(Effect.asVoid); + // One argv entry preserves leading dashes and multi-string descriptions. + const fields = [ + ...(input.title === undefined ? [] : [AzureDevOpsCli.textOption("--title", input.title)]), + ...(input.body === undefined + ? [] + : [AzureDevOpsCli.textOption("--description", input.body)]), + ]; + // az has one stdin, so each value read from it needs its own call. + const batches = + fields.filter((field) => field.stdin !== undefined).length > 1 + ? fields.map((field) => [field]) + : [fields]; + return Effect.forEach( + batches, + (batch) => { + const stdin = batch.find((field) => field.stdin !== undefined)?.stdin; + return azure.execute({ + cwd: input.cwd, + args: [ + "repos", + "pr", + "update", + ...detectArgs, + "--id", + String(input.number), + ...batch.map((field) => field.arg), + "--only-show-errors", + "--output", + "json", + ], + ...(stdin === undefined ? {} : { stdin }), + }); + }, + { discard: true }, + ); }), }); }); From 789d10fb54adfce468dbf5c7ad8f118ceeed6c53 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 15 Nov 2026 03:00:07 -0800 Subject: [PATCH 011/124] fix(web): Sharing & access can select a desktop-local WSL backend (#37151) --- apps/web/src/components/settings/useAccessEnvironment.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/apps/web/src/components/settings/useAccessEnvironment.ts b/apps/web/src/components/settings/useAccessEnvironment.ts index 6e94e2365ca7d..b1d1bc34d45a2 100644 --- a/apps/web/src/components/settings/useAccessEnvironment.ts +++ b/apps/web/src/components/settings/useAccessEnvironment.ts @@ -1,14 +1,13 @@ import type { EnvironmentId } from "@t3tools/contracts"; -import { isDesktopLocalConnectionTarget } from "../../connection/desktopLocal"; import { useEnvironments, usePrimaryEnvironmentId } from "../../state/environments"; import { resolveAccessEnvironmentId } from "./ConnectionsSettings.logic"; import { settingsScopeEnvironmentLabel } from "./settingsScopeAxis"; /** * The environments Sharing & access manages, the one `requested` resolves to, and its name. - * The MCP wizard uses the same, so both name an environment alike. A requested environment - * always counts, so a thread on a second WSL backend opens on that backend. + * The MCP wizard uses the same, so both name an environment alike. A desktop-local WSL backend + * runs its own server with its own access, so it is a candidate too. */ export function useAccessEnvironment(requested: EnvironmentId | null) { const { environments } = useEnvironments(); @@ -17,7 +16,7 @@ export function useAccessEnvironment(requested: EnvironmentId | null) { (environment) => environment.environmentId === primaryEnvironmentId || environment.environmentId === requested || - (environment.entry.enabled && !isDesktopLocalConnectionTarget(environment.entry.target)), + environment.entry.enabled, ); const environmentId = resolveAccessEnvironmentId({ primaryEnvironmentId, From 0afd8c0295d6dafce602bc808ec123baf37c6dc1 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 15 Nov 2026 12:07:25 +0100 Subject: [PATCH 012/124] fix(mobile): pasted GIFs on iOS keep their animation (#37143) Co-authored-by: Julius Marminge --- .../ios/T3ComposerEditorView.swift | 36 +++++++++++++------ .../t3-composer-editor/tests/ios/main.swift | 22 +++++++++++- apps/mobile/src/lib/composerImages.test.ts | 7 +++- apps/mobile/src/lib/composerImages.ts | 4 +-- 4 files changed, 53 insertions(+), 16 deletions(-) diff --git a/apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift b/apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift index 87029ef29fdaf..7bc50b694df5f 100644 --- a/apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift +++ b/apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift @@ -1,5 +1,6 @@ import ExpoModulesCore import UIKit +import UniformTypeIdentifiers private struct ComposerTokenPayload: Decodable, Equatable { let type: String @@ -309,18 +310,28 @@ private final class ComposerTextView: UITextView { func loadImages(from providers: [NSItemProvider]) { let group = DispatchGroup() let lock = NSLock() - var images = [UIImage?](repeating: nil, count: providers.count) + var images = [(data: Data, fileExtension: String)?](repeating: nil, count: providers.count) for (index, provider) in providers.enumerated() { group.enter() - provider.loadObject(ofClass: UIImage.self) { object, _ in - defer { group.leave() } - guard let image = object as? UIImage else { - return - } + let store = { (image: (data: Data, fileExtension: String)?) in lock.lock() images[index] = image lock.unlock() + group.leave() + } + let loadPNG = { + _ = provider.loadObject(ofClass: UIImage.self) { object, _ in + store((object as? UIImage)?.pngData().map { ($0, "png") }) + } + } + // Decoding a GIF to UIImage keeps only its first frame, so pass its bytes through. + if provider.hasItemConformingToTypeIdentifier(UTType.gif.identifier) { + _ = provider.loadDataRepresentation(forTypeIdentifier: UTType.gif.identifier) { data, _ in + if let data { store((data, "gif")) } else { loadPNG() } + } + } else { + loadPNG() } } @@ -328,7 +339,9 @@ private final class ComposerTextView: UITextView { guard let self, !self.isReadOnly else { return } - let urls = images.compactMap { $0 }.compactMap(Self.writeTemporaryImage) + let urls = images.compactMap { $0 }.compactMap { + Self.writeTemporaryImage($0.data, fileExtension: $0.fileExtension) + } if !urls.isEmpty { self.onPasteImages?(urls) } @@ -408,9 +421,10 @@ private final class ComposerTextView: UITextView { } private static func writeTemporaryImage(_ image: UIImage) -> String? { - guard let data = image.pngData() else { - return nil - } + image.pngData().flatMap { writeTemporaryImage($0, fileExtension: "png") } + } + + private static func writeTemporaryImage(_ data: Data, fileExtension: String) -> String? { let directory = FileManager.default.temporaryDirectory .appendingPathComponent(pastedImageDirectoryName, isDirectory: true) do { @@ -419,7 +433,7 @@ private final class ComposerTextView: UITextView { withIntermediateDirectories: true ) removeStaleTemporaryImages(in: directory) - let url = directory.appendingPathComponent("\(UUID().uuidString).png") + let url = directory.appendingPathComponent("\(UUID().uuidString).\(fileExtension)") try data.write(to: url, options: .atomic) return url.absoluteString } catch { diff --git a/apps/mobile/modules/t3-composer-editor/tests/ios/main.swift b/apps/mobile/modules/t3-composer-editor/tests/ios/main.swift index 32651e2af6352..e9751eec843ac 100644 --- a/apps/mobile/modules/t3-composer-editor/tests/ios/main.swift +++ b/apps/mobile/modules/t3-composer-editor/tests/ios/main.swift @@ -212,6 +212,25 @@ for readOnly in [true, false] { check(provider.loadCount == 0, "Locked inputs must not load pasted image providers") } +// Two-frame animated GIF: a mixed paste keeps the GIF's original bytes and the still image's PNG, in order. +private let gifBytes = Data(base64Encoded: "R0lGODlhAQABAIEAAP8AAAAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh+QQACgAAACwAAAAAAQABAAAIBAABBAQAIfkEAAoAAAAsAAAAAAEAAQCBAP8AAAAAAAAAAAAACAQAAQQEADs=")! +private let gifProvider = NSItemProvider() +gifProvider.registerDataRepresentation(forTypeIdentifier: UTType.gif.identifier, visibility: .all) { completion in + completion(gifBytes, nil) + return nil +} +private let gifEditor = ComposerTextView() +gifEditor.onPasteImages = { urls in + let files = urls.compactMap(URL.init(string:)) + let contents = files.compactMap { try? Data(contentsOf: $0) } + files.forEach { try? FileManager.default.removeItem(at: $0) } + check(files.map(\.pathExtension) == ["gif", "png"], "A mixed paste must keep each image's format, in order: \(urls)") + check(contents.first == gifBytes, "A pasted animated GIF must keep its original bytes") + check(contents.count == 2 && contents[1] == image.pngData(), "The still image beside a GIF must still attach as PNG") + print("Composer GIF-paste checks passed") + exit(0) +} + // Inline provider completion places the attachment callback before this main-queue check. DispatchQueue.main.async { check(pastedImages.count == 1, "An accepted provider-based paste must emit exactly one attachment batch") @@ -228,6 +247,7 @@ DispatchQueue.main.async { "The attachment must contain the pasted image's pixels") check(pasteEditor.text == "Keep this draft", "Image paste must preserve the draft text") print("Composer provider-paste checks passed") - exit(0) + check(gifProvider.canLoadObject(ofClass: UIImage.self), "A GIF provider must take the image paste path") + gifEditor.paste(itemProviders: [gifProvider, ImmediateImageProvider(image: image)]) } dispatchMain() diff --git a/apps/mobile/src/lib/composerImages.test.ts b/apps/mobile/src/lib/composerImages.test.ts index c723293c85270..b5b10ac02c7bb 100644 --- a/apps/mobile/src/lib/composerImages.test.ts +++ b/apps/mobile/src/lib/composerImages.test.ts @@ -199,10 +199,13 @@ describe("native pasted image cleanup", () => { it("converts owned files to data-backed previews and deletes the source", async () => { const uri = "file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/id.png"; + const gif = + "file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/anim.gif"; files.set(uri, { base64: "aGVsbG8=", deleted: false }); + files.set(gif, { base64: "R0lGODlh", deleted: false }); const { attachments } = await convertPastedImagesToAttachments({ - uris: [uri], + uris: [uri, gif], existingCount: 0, }); @@ -211,8 +214,10 @@ describe("native pasted image cleanup", () => { dataUrl: "data:image/png;base64,aGVsbG8=", previewUri: "data:image/png;base64,aGVsbG8=", }), + expect.objectContaining({ mimeType: "image/gif", dataUrl: "data:image/gif;base64,R0lGODlh" }), ]); expect(files.get(uri)?.deleted).toBe(true); + expect(files.get(gif)?.deleted).toBe(true); }); it("deletes rejected and overflow owned files without deleting user-owned files", async () => { diff --git a/apps/mobile/src/lib/composerImages.ts b/apps/mobile/src/lib/composerImages.ts index b0bcec2ad89c3..6c19874e1df53 100644 --- a/apps/mobile/src/lib/composerImages.ts +++ b/apps/mobile/src/lib/composerImages.ts @@ -561,9 +561,7 @@ function isOwnedPastedImageUri(uri: string): boolean { return false; } const segments = url.pathname.split("/").filter(Boolean); - return ( - segments.at(-2) === OWNED_PASTED_IMAGE_DIRECTORY && segments.at(-1)?.endsWith(".png") === true - ); + return segments.at(-2) === OWNED_PASTED_IMAGE_DIRECTORY; } catch { return false; } From 4b9cf579ba62a808d9f99506474d971e30e917ae Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 15 Nov 2026 03:17:55 -0800 Subject: [PATCH 013/124] fix(acp): Copilot's reply after a background agent wakes it opens a continuation run (#37159) --- .../Adapters/AcpRegistryAdapterV2.test.ts | 96 +++++++++++++++++++ .../src/server/adapter.ts | 2 + packages/provider-acp/src/server/adapter.ts | 17 ++++ 3 files changed, 115 insertions(+) diff --git a/apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts index 97fb6e636054f..ee82cc06de836 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts @@ -1901,6 +1901,102 @@ describe("AcpRegistryAdapterV2", () => { }).pipe(Effect.provide(layerTest)), ); + // Copilot wakes its root agent when a background subagent finishes, after the run settled. + it.effect("does not drop Copilot's root reply when a finished background agent wakes it", () => + Effect.gen(function* () { + const sessionId = "copilot-session"; + const sessionEvent = (type: string) => ({ + type: "emit_inbound", + frame: { + kind: "notification", + method: "github.com/copilot/sessionEvent", + params: { sessionId, type, data: {} }, + }, + }); + const update = (sessionUpdate: Record) => + claudeFrames.update(sessionId, sessionUpdate); + const session = yield* openClaudeReplaySession( + [ + { + type: "expect_outbound", + frame: { kind: "request", method: "initialize", params: "" }, + }, + { + type: "emit_inbound", + frame: { + kind: "response", + method: "initialize", + result: { + protocolVersion: 1, + agentInfo: { name: "Copilot", version: "1.0.112" }, + agentCapabilities: { loadSession: true }, + authMethods: [], + }, + }, + }, + { + type: "expect_outbound", + frame: { kind: "request", method: "session/new", params: "" }, + }, + { + type: "emit_inbound", + frame: { kind: "response", method: "session/new", result: { sessionId } }, + }, + { + type: "expect_outbound", + frame: { kind: "request", method: "session/prompt", params: "" }, + }, + sessionEvent("assistant.turn_start"), + update({ + sessionUpdate: "agent_message_chunk", + content: { type: "text", text: "Agents are still working." }, + }), + sessionEvent("assistant.idle"), + update({ sessionUpdate: "session_info_update", title: "after foreground idle" }), + // The background agent finishes; Copilot reads it and starts a new root turn. + sessionEvent("assistant.turn_start"), + update({ + sessionUpdate: "tool_call", + toolCallId: "read-agent-1", + title: "read_agent", + kind: "other", + status: "completed", + rawInput: { agent_id: "agent-1" }, + }), + update({ + sessionUpdate: "agent_message_chunk", + content: { type: "text", text: "Agent 1 finished" }, + }), + sessionEvent("assistant.idle"), + { + type: "emit_inbound", + frame: { + kind: "response", + method: "session/prompt", + result: { stopReason: "end_turn" }, + }, + }, + update({ sessionUpdate: "session_info_update", title: "after wake idle" }), + ], + undefined, + true, + "github-copilot-cli", + ); + yield* session.runtime.startTurn(session.turnInput(1)); + const foreground = yield* session.throughMarker("after foreground idle"); + assert.isDefined(foreground.find((event) => event.type === "turn.terminal")); + const wake = yield* session.throughMarker("after wake idle"); + assert.isTrue( + session.offers.length > 0 || JSON.stringify(wake).includes("Agent 1 finished"), + "the woken root reply is shown or opens a continuation run", + ); + yield* session.runtime.startTurn(session.turnInput(2, { continuation: true })); + const continuation = yield* session.until((event) => event.type === "turn.terminal"); + assert.include(JSON.stringify(continuation), "Agent 1 finished"); + assert.include(JSON.stringify(continuation.at(-1)), '"status":"completed"'); + }).pipe(Effect.provide(layerTest)), + ); + it.effect.each([ { kind: "approval", stopReason: "end_turn", status: "completed" }, { kind: "question", stopReason: "error", status: "failed" }, diff --git a/packages/provider-acp-registry/src/server/adapter.ts b/packages/provider-acp-registry/src/server/adapter.ts index 360ba35e8cc5c..49dc4426b1428 100644 --- a/packages/provider-acp-registry/src/server/adapter.ts +++ b/packages/provider-acp-registry/src/server/adapter.ts @@ -281,6 +281,8 @@ export const makeAcpRegistryAdapterV2 = Effect.fn("makeAcpRegistryAdapterV2")(fu normalizeSessionUpdate: copilotRouting.normalizeSessionUpdate, extractSubagentUpdate: extractCopilotSubagentUpdate, restartRuntimeAfterPromptError: isCopilotLostSessionError, + // A finished background agent wakes the root after assistant.idle settled the run. + enablePostSettleContinuation: true, // session/new reports a built-in default, not the model Copilot runs (github/copilot-cli#5363). alwaysSetModel: true, }), diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index dc12bd7aa1e14..b7f5753539c68 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -2049,6 +2049,8 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( EffectAcpSchema.SessionUpdate, { sessionUpdate: "state_update" } > | null = null; + // The root's last reportForegroundWork state; undefined until the agent reports one. + let rootForegroundWork: "running" | "idle" | undefined; const agentRequests = new Set(); const nativeChildren = new Map(); const extractSubagentUpdate = (tool: AcpToolCallState) => @@ -2082,6 +2084,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( continuationRequests !== undefined && setup.initializeResult.protocolVersion === 2; agentWork = null; + rootForegroundWork = undefined; agentRequests.clear(); nativeChildren.clear(); yield* Ref.set(activeSessionSetup, setup); @@ -7163,6 +7166,9 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( handlerGeneration, Effect.gen(function* () { if (yield* Ref.get(stoppedRunQuarantine)) return; + if ((yield* Ref.get(activeSessionId)) === notice.sessionId) { + rootForegroundWork = notice.state; + } const context = yield* Ref.get(activeTurn); if ( context === null || @@ -8759,6 +8765,17 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( ), Effect.map((drained) => drained.length), ); + // A root woken after foreground idle ends on its next idle report. + if (drainedWakeCount > 0 && rootForegroundWork !== undefined) { + yield* runtimeCallbackPermit.withPermit( + Effect.suspend(() => { + if (rootForegroundWork === "idle") return finalizeTurn(context, "completed"); + context.workAccepted = true; + return Effect.void; + }), + ); + return; + } context.promptSettled = true; context.promptSettledStatus = "completed"; if (drainedWakeCount === 0) { From a16796b956ae670db221df6178ab7f724c2f817c Mon Sep 17 00:00:00 2001 From: Leon Date: Sun, 15 Nov 2026 12:18:24 +0100 Subject: [PATCH 014/124] =?UTF-8?q?fix(shared):=20=C3=9F=20no=20longer=20s?= =?UTF-8?q?plits=20words=20in=20generated=20folder=20and=20branch=20names?= =?UTF-8?q?=20(#37152)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: lowlyleon <183293296+lowlyleon@users.noreply.github.com> Co-authored-by: Julius Marminge --- packages/shared/src/git.test.ts | 7 +++++++ packages/shared/src/git.ts | 6 +++--- packages/shared/src/path.test.ts | 1 + packages/shared/src/path.ts | 6 ++++-- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/shared/src/git.test.ts b/packages/shared/src/git.test.ts index e9927f805e2fb..a11230586ba11 100644 --- a/packages/shared/src/git.test.ts +++ b/packages/shared/src/git.test.ts @@ -363,6 +363,13 @@ describe("sanitizeFeatureBranchName", () => { "fix/anadir-validacion-del-cafe", ); }); + + it("keeps words with letters that have no Unicode decomposition whole", () => { + expect(sanitizeFeatureBranchName("Größe der Straße korrigieren")).toBe( + "feature/grosse-der-strasse-korrigieren", + ); + expect(sanitizeFeatureBranchName("fix/Đặt tên")).toBe("fix/dat-ten"); + }); }); describe("resolveAutoFeatureBranchName", () => { diff --git a/packages/shared/src/git.ts b/packages/shared/src/git.ts index 113d2545e1b66..7ef56875e34b5 100644 --- a/packages/shared/src/git.ts +++ b/packages/shared/src/git.ts @@ -9,6 +9,8 @@ import type { import * as Arr from "effect/Array"; import * as Result from "effect/Result"; +import { foldLatinDiacritics } from "./path.ts"; + export const WORKTREE_BRANCH_PREFIX = "t3"; const TEMP_WORKTREE_HEX_TOKEN = "[0-9a-f]{8}"; const TEMP_WORKTREE_UUID_V4_TOKEN = @@ -18,9 +20,7 @@ const TEMP_WORKTREE_BRANCH_PATTERN = new RegExp( ); export function sanitizeBranchFragment(raw: string): string { - const normalized = raw - .normalize("NFKD") - .replace(/[̀-ͯ]/g, "") + const normalized = foldLatinDiacritics(raw) .trim() .toLowerCase() .replace(/['"`]/g, "") diff --git a/packages/shared/src/path.test.ts b/packages/shared/src/path.test.ts index cec173343b51f..514f92b8f54e0 100644 --- a/packages/shared/src/path.test.ts +++ b/packages/shared/src/path.test.ts @@ -30,6 +30,7 @@ describe("path helpers", () => { expect(newProjectFolderName("Pinball Stats")).toBe("pinball-stats"); expect(newProjectFolderName(" Café & Crème! ")).toBe("cafe-creme"); expect(newProjectFolderName("Đặt tên dự án")).toBe("dat-ten-du-an"); + expect(newProjectFolderName("Straßenbahn GROẞ")).toBe("strassenbahn-gross"); expect(newProjectFolderName("../../etc")).toBe("etc"); expect(newProjectFolderName("🎱🎱")).toBe("project"); expect(newProjectFolderName(`${"a".repeat(63)} b`)).toBe("a".repeat(63)); diff --git a/packages/shared/src/path.ts b/packages/shared/src/path.ts index 31fb06bd0591c..45b49b200091c 100644 --- a/packages/shared/src/path.ts +++ b/packages/shared/src/path.ts @@ -68,8 +68,10 @@ export function foldLatinDiacritics(text: string): string { return text .normalize("NFKD") .replace(/[\u0300-\u036f]/g, "") - .replace(/đ/g, "d") // d-with-stroke has no Unicode decomposition - .replace(/Đ/g, "D"); + .replace(/đ/g, "d") // d-with-stroke and sharp s have no Unicode decomposition + .replace(/Đ/g, "D") + .replace(/ß/g, "ss") + .replace(/ẞ/g, "SS"); } export function newProjectFolderName(name: string): string { From 86a9d9fb71ae010ac59014e7d9d0842db16acf1d Mon Sep 17 00:00:00 2001 From: Sudharsan <126713117+Sidreyas@users.noreply.github.com> Date: Sun, 15 Nov 2026 17:03:18 +0530 Subject: [PATCH 015/124] fix(server): name the missing project when a change request's repository has none (#37161) Co-authored-by: Julius Marminge --- .../src/features/diffs/nativeReviewDiffSource.ts | 1 + .../src/mcp/toolkits/pullRequests/handlers.ts | 3 ++- .../src/pullRequest/PullRequestService.test.ts | 16 ++++++++++++---- .../server/src/pullRequest/PullRequestService.ts | 13 +++++++++++++ packages/contracts/src/pullRequest.ts | 5 +++++ 5 files changed, 33 insertions(+), 5 deletions(-) diff --git a/apps/mobile/src/features/diffs/nativeReviewDiffSource.ts b/apps/mobile/src/features/diffs/nativeReviewDiffSource.ts index 7f503239c21d2..4d73a7cda5c3c 100644 --- a/apps/mobile/src/features/diffs/nativeReviewDiffSource.ts +++ b/apps/mobile/src/features/diffs/nativeReviewDiffSource.ts @@ -19,6 +19,7 @@ function permanentSourceFailureReason(error: unknown): string | undefined { if (name === "GitHubDiffFileContentsUnavailableError") return "github-source-unavailable"; for (const permanent of [ "provider-unsupported", + "repository-without-project", "unsupported", "oversized", "binary", diff --git a/apps/server/src/mcp/toolkits/pullRequests/handlers.ts b/apps/server/src/mcp/toolkits/pullRequests/handlers.ts index b738b2d8b4359..884dd09a3dea4 100644 --- a/apps/server/src/mcp/toolkits/pullRequests/handlers.ts +++ b/apps/server/src/mcp/toolkits/pullRequests/handlers.ts @@ -512,7 +512,8 @@ const make = Effect.gen(function* () { Effect.mapError((error) => entry.project === undefined && error._tag === "PullRequestUnavailableError" && - error.reason === "provider-unsupported" + (error.reason === "provider-unsupported" || + error.reason === "repository-without-project") ? new PullRequestProjectNotFoundError({ repository: target.repository }) : new PullRequestGuideFailedError({ detail: guideFailureDetail(error), diff --git a/apps/server/src/pullRequest/PullRequestService.test.ts b/apps/server/src/pullRequest/PullRequestService.test.ts index 905c96319d52a..1ca1d3885b936 100644 --- a/apps/server/src/pullRequest/PullRequestService.test.ts +++ b/apps/server/src/pullRequest/PullRequestService.test.ts @@ -2375,7 +2375,10 @@ it.effect.each([ }), ); -it.effect("refuses Azure cross-organization reads and writes without its checkout", () => +it.effect.each([ + { case: "another organization", repository: "org-b/project/_git/web" }, + { case: "a sibling repository", repository: "org-a/project/_git/web-e2e" }, +])("refuses Azure reads and writes in $case without its checkout", ({ repository }) => Effect.gen(function* () { const service = yield* makeService({ projects: [ @@ -2399,15 +2402,20 @@ it.effect("refuses Azure cross-organization reads and writes without its checkou const reference = { projectId: "org-a" as ProjectId, host: "dev.azure.com", - repository: "org-b/project/_git/web", + repository, number: 7, }; const readError = yield* Effect.flip( service.summary(reference, { recoverTransientFailure: false }), ); const writeError = yield* Effect.flip(service.runAction({ ...reference, action: "close" })); - assert.strictEqual(readError._tag, "PullRequestUnavailableError"); - assert.strictEqual(writeError._tag, "PullRequestUnavailableError"); + for (const error of [readError, writeError]) { + expect(error).toMatchObject({ + _tag: "PullRequestUnavailableError", + reason: "repository-without-project", + message: `${repository} is not a project in T3 Code. Add it as a project to read its change requests.`, + }); + } }), ); diff --git a/apps/server/src/pullRequest/PullRequestService.ts b/apps/server/src/pullRequest/PullRequestService.ts index 74da9bacee95d..92671afb75b58 100644 --- a/apps/server/src/pullRequest/PullRequestService.ts +++ b/apps/server/src/pullRequest/PullRequestService.ts @@ -1084,6 +1084,19 @@ export const make = Effect.gen(function* () { }; } } + const owner = + publicHost ?? sourceControlDefinitions.findByRepositoryKey(referenceKey); + const readable = owner === undefined ? null : registry.get(owner.kind); + if ( + readable !== null && + !readable.readsPublicHostWithoutCheckout && + readable.servesHost?.(host) !== false + ) { + return yield* new PullRequestUnavailableError({ + 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 0662c93f9a06d..4d313c0ceb98d 100644 --- a/packages/contracts/src/pullRequest.ts +++ b/packages/contracts/src/pullRequest.ts @@ -970,6 +970,7 @@ export const PullRequestUnavailableReason = Schema.Literals([ "cli-missing", "cli-unauthenticated", "provider-unsupported", + "repository-without-project", ]); export type PullRequestUnavailableReason = typeof PullRequestUnavailableReason.Type; @@ -999,6 +1000,8 @@ export class PullRequestUnavailableError extends Schema.TaggedError Date: Sun, 15 Nov 2026 17:14:04 +0530 Subject: [PATCH 016/124] fix(server): an oversized provider request reads as too large, not raw 413 HTML (#37156) Co-authored-by: Julius Marminge --- .../provider-core/src/server/failure.test.ts | 26 +++++++++++++++++++ packages/provider-core/src/server/failure.ts | 16 +++++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/packages/provider-core/src/server/failure.test.ts b/packages/provider-core/src/server/failure.test.ts index 1e6c4040d0323..c222ef9b27462 100644 --- a/packages/provider-core/src/server/failure.test.ts +++ b/packages/provider-core/src/server/failure.test.ts @@ -59,6 +59,32 @@ it("redacts credentials whose key names end in a credential word", () => { ); }); +it("reports an oversized provider request as not retryable instead of the gateway's error page", () => { + for (const message of [ + "413 \n413 Request Entity Too Large\n\n

413 Request Entity Too Large

\n
nginx
\n\n", + "unexpected status 413 Payload Too Large: request body exceeds the limit", + 'API Error: 413 {"type":"error","error":{"type":"request_too_large"}}', + ]) { + const failure = makeProviderFailure({ message, class: "provider_error", retryable: true }); + + assert.equal(failure.class, "provider_error"); + assert.equal(failure.retryable, false); + assert.equal( + failure.message, + "This thread's request is too large for the provider (HTTP 413), so retrying fails the same way. Start a fresh session with /clear, or continue in a new thread.", + ); + } + for (const message of [ + "Request too large for gpt-4o on tokens per min (TPM): Limit 30000, Requested 41300.", + "Rate limited after 413 requests.", + "Tool result content too large to return.", + ]) { + const failure = makeProviderFailure({ message, retryable: true }); + assert.equal(failure.message, message); + assert.equal(failure.retryable, true); + } +}); + it("does not split a surrogate pair at the truncation boundary", () => { const failure = makeProviderFailure({ message: `${"a".repeat(MAX_PROVIDER_FAILURE_MESSAGE_LENGTH - 2)}🚀tail`, diff --git a/packages/provider-core/src/server/failure.ts b/packages/provider-core/src/server/failure.ts index 86919622f03d4..4131e470fa2f5 100644 --- a/packages/provider-core/src/server/failure.ts +++ b/packages/provider-core/src/server/failure.ts @@ -40,6 +40,12 @@ const MAX_PROVIDER_FAILURE_CODE_LENGTH = 128; const DEFAULT_PROVIDER_FAILURE_MESSAGE = "Provider turn failed."; +// Gateways answer an oversized body with a 413 page; resending the same history fails identically. +const REQUEST_TOO_LARGE_STATUS = /^\s*(?:(?:API Error|Error|HTTP|status)(?:\s*:)?\s*)?413\b/iu; +const REQUEST_TOO_LARGE_REASON = /\b(?:Request Entity|Payload|Content) Too Large\b/u; +const REQUEST_TOO_LARGE_MESSAGE = + "This thread's request is too large for the provider (HTTP 413), so retrying fails the same way. Start a fresh session with /clear, or continue in a new thread."; + const PROVIDER_FAILURE_CATEGORIES: Partial< Record > = { @@ -276,8 +282,12 @@ export function makeProviderFailure(input: { readonly resetAt?: string | null; }): OrchestrationV2ProviderFailure { const curated = curatedCauseMessages(input.cause); - const rawMessage = - input.message ?? curated.known ?? curated.category ?? DEFAULT_PROVIDER_FAILURE_MESSAGE; + const requestTooLarge = + input.message !== undefined && + (REQUEST_TOO_LARGE_STATUS.test(input.message) || REQUEST_TOO_LARGE_REASON.test(input.message)); + const rawMessage = requestTooLarge + ? REQUEST_TOO_LARGE_MESSAGE + : (input.message ?? curated.known ?? curated.category ?? DEFAULT_PROVIDER_FAILURE_MESSAGE); const message = boundedText(rawMessage, MAX_PROVIDER_FAILURE_MESSAGE_LENGTH); const rawCode = input.code ?? stringField(input.cause, "code") ?? null; const code = @@ -287,7 +297,7 @@ export function makeProviderFailure(input: { class: input.class ?? "unknown", message: message || DEFAULT_PROVIDER_FAILURE_MESSAGE, code, - retryable: input.retryable ?? null, + retryable: requestTooLarge ? false : (input.retryable ?? null), ...(input.class === "usage_limit" && input.resetAt != null && Number.isFinite(Date.parse(input.resetAt)) From 649d9fc1ab16fd28f8d927155c4315ab8034963a Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 15 Nov 2026 03:46:14 -0800 Subject: [PATCH 017/124] fix(provider-acp): Grok early task completion no longer replaces the child's answer (#37162) --- .../Adapters/AcpAdapterV2.test.ts | 91 ++++++++++++++----- packages/provider-acp/src/server/adapter.ts | 19 ++-- 2 files changed, 77 insertions(+), 33 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts index 7643610fd850c..d71b0f7b5e2b8 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts @@ -9461,9 +9461,9 @@ describe("AcpAdapterV2", () => { }).pipe(Effect.provide(layerTest)), ); - it.effect( - "staggered pre-settle completion keeps midTurn marks until last background task ends post-finalize", - () => + it.effect.each(["unpolled", "polled"] as const)( + "staggered pre-settle completion waits for native wake output when task B is %s", + (taskB) => Effect.gen(function* () { const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; const idAllocator = yield* IdAllocator.IdAllocatorV2; @@ -9615,6 +9615,16 @@ describe("AcpAdapterV2", () => { 0, "pre-settle unhandled completion must not offer while the turn is active", ); + if (taskB === "polled") { + yield* sessionUpdateHandler!({ + sessionId: "mock-session-1", + update: { + sessionUpdate: "tool_call_update", + toolCallId: "tool-call-monitor-b", + status: "completed", + }, + }); + } yield* Deferred.succeed(promptGate, { stopReason: "end_turn" }); const providerTurnId = idAllocator.derive.providerTurn({ @@ -9632,35 +9642,44 @@ describe("AcpAdapterV2", () => { assert.lengthOf( continuationRequests, 0, - "finalize must not offer while B is still running", + "finalize must not offer without native wake output", ); - const bEndNotice = { + if (taskB === "unpolled") { + const bEndNotice = { + sessionId: "mock-session-1", + update: { + sessionUpdate: "user_message_chunk" as const, + content: { + type: "text" as const, + text: 'Monitor "task-monitor-b" ended: [monitor ended: exit 0]', + }, + }, + }; + assert.isFalse( + acpPostSettleWakeEvidence(bEndNotice, { + extractBackgroundToolMutation: (text) => + text.includes('Monitor "task-monitor-b" ended') + ? [{ taskId: "task-monitor-b", status: "completed", appendOutput: "" }] + : [], + }), + "mutation-only end-notice must not count as post-settle wake evidence", + ); + yield* sessionUpdateHandler!(bEndNotice); + assert.lengthOf(continuationRequests, 0, "a task end notice alone cannot start a wake"); + } + assert.isTrue(yield* runtime.hasPendingBackgroundWork!); + yield* sessionUpdateHandler!({ sessionId: "mock-session-1", update: { - sessionUpdate: "user_message_chunk" as const, - content: { - type: "text" as const, - text: 'Monitor "task-monitor-b" ended: [monitor ended: exit 0]', - }, + sessionUpdate: "agent_message_chunk", + content: { type: "text", text: "Both background tasks finished." }, }, - }; - assert.isFalse( - acpPostSettleWakeEvidence(bEndNotice, { - extractBackgroundToolMutation: (text) => - text.includes('Monitor "task-monitor-b" ended') - ? [{ taskId: "task-monitor-b", status: "completed", appendOutput: "" }] - : [], - }), - "mutation-only end-notice must not count as post-settle wake evidence", - ); - yield* sessionUpdateHandler!(bEndNotice); - yield* Effect.yieldNow; - yield* Effect.yieldNow; + }); assert.lengthOf( continuationRequests, 1, - "exactly one continuation when the last running task ends post-finalize with kept midTurn marks", + "the native report must start exactly one continuation", ); }).pipe(Effect.provide(layerTest)), ); @@ -10311,6 +10330,11 @@ describe("AcpAdapterV2", () => { yield* complete("early"); assert.lengthOf(continuationRequests, 0); + yield* update({ + sessionUpdate: "agent_message_chunk", + content: { type: "text", text: "EARLY_SUCCESS_DONE" }, + }); + let backgroundItem: ProviderAdapter.ProviderAdapterV2Event | undefined; let checkpoint = 0; const assertCommand = Effect.fnUntraced(function* (expectedStatus: string, output: string) { @@ -10387,9 +10411,26 @@ describe("AcpAdapterV2", () => { } break; } - yield* Deferred.await(continuationOffered); yield* complete("early"); yield* complete("blocker"); + assert.lengthOf( + continuationRequests, + 0, + "a task completion without native wake output must not replace the root answer", + ); + assert.isTrue(yield* runtime.hasPendingBackgroundWork!); + + yield* update({ + sessionUpdate: "agent_thought_chunk", + content: { type: "text", text: "Preparing the background report" }, + }); + yield* complete("early"); + assert.lengthOf(continuationRequests, 0, "reasoning alone is not a result-bearing wake"); + yield* update({ + sessionUpdate: "agent_message_chunk", + content: { type: "text", text: "LATE_NATIVE_ANSWER" }, + }); + yield* Deferred.await(continuationOffered); assert.lengthOf( continuationRequests, 1, diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index b7f5753539c68..b2a28c68c6c08 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -4948,8 +4948,9 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( postSettleContinuationEnabled && (yield* Ref.get(activeSessionId)) === sessionId && (yield* Ref.get(runningBackgroundTaskIds)).size === 0 && - ((yield* Ref.get(wakeBuffer)).length > 0 || - (yield* Ref.get(midTurnUnreportedCompletedTaskIds)).size > 0) + (yield* Ref.get(wakeBuffer)).some((notification) => + acpPostSettleContinuationOfferEvidence(notification, flavor), + ) ) { yield* offerContinuationRun(sessionId); } @@ -5069,8 +5070,10 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( } const backgroundWorkRunning = (yield* Ref.get(runningBackgroundTaskIds)).size > 0; const handledInTurnCount = (yield* Ref.get(handledBackgroundTaskIdsInActiveTurn)).size; + // Chunks carry no task id: while an unpolled completion awaits its report, keep them. const isInTurnHandledAgentChatter = handledInTurnCount > 0 && + (yield* Ref.get(midTurnUnreportedCompletedTaskIds)).size === 0 && (update.sessionUpdate === "agent_message_chunk" || update.sessionUpdate === "agent_thought_chunk"); // Grok re-prompts for each post-settle monitor event; retaining them would replay the whole burst. @@ -5401,6 +5404,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( context !== null && context.finalized && (yield* Ref.get(handledBackgroundTaskIdsInActiveTurn)).size > 0 && + (yield* Ref.get(midTurnUnreportedCompletedTaskIds)).size === 0 && (update.sessionUpdate === "agent_message_chunk" || update.sessionUpdate === "agent_thought_chunk") ) { @@ -8186,18 +8190,16 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( settledStatus === "completed" && !directStopQuarantine && (yield* Ref.get(runningBackgroundTaskIds)).size === 0 && - ((yield* Ref.get(wakeBuffer)).length > 0 || - (yield* Ref.get(midTurnUnreportedCompletedTaskIds)).size > 0) + (yield* Ref.get(wakeBuffer)).some((notification) => + acpPostSettleContinuationOfferEvidence(notification, flavor), + ) ) { const sessionId = yield* Ref.get(activeSessionId); if (sessionId !== null) { yield* offerContinuationRun(sessionId); } } - if ( - settledStatus !== "completed" || - (yield* Ref.get(runningBackgroundTaskIds)).size === 0 - ) { + if (settledStatus !== "completed") { yield* Ref.set(midTurnUnreportedCompletedTaskIds, new Set()); } yield* Deferred.succeed(context.completed, undefined).pipe(Effect.ignore); @@ -9098,6 +9100,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( ) return true; if ((yield* Ref.get(wakeBuffer)).length > 0) return true; + if ((yield* Ref.get(midTurnUnreportedCompletedTaskIds)).size > 0) return true; if (yield* Ref.get(continuationRequested)) return true; if ((yield* Ref.get(runningBackgroundTaskIds)).size > 0) return true; const active = yield* Ref.get(activeTurn); From 7379f1969b0f4b579327cf81299ef8cc3ddf8e9c Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 15 Nov 2026 13:04:04 +0100 Subject: [PATCH 018/124] fix(web): keypad navigation works with NumLock off (#37175) Co-authored-by: Julius Marminge --- apps/web/src/terminal/ghostty/core.test.ts | 44 +++++---- apps/web/src/terminal/ghostty/keyCodes.ts | 27 +++++- ...rminal-numlock-off-keypad-navigates.e2e.ts | 90 +++++++++++++++++++ 3 files changed, 143 insertions(+), 18 deletions(-) create mode 100644 scripts/scenarios/terminal-numlock-off-keypad-navigates.e2e.ts diff --git a/apps/web/src/terminal/ghostty/core.test.ts b/apps/web/src/terminal/ghostty/core.test.ts index 147af94a10d16..b17b86b96d3c8 100644 --- a/apps/web/src/terminal/ghostty/core.test.ts +++ b/apps/web/src/terminal/ghostty/core.test.ts @@ -130,28 +130,40 @@ describe("GhosttyTerminalCore snapshots", () => { }); it.each([ + { code: "Numpad1", key: "End", expected: "\x1b[F" }, + { code: "Numpad5", key: "Clear", expected: "\x1b[E" }, + { code: "Numpad8", key: "ArrowUp", expected: "\x1b[A" }, + { code: "NumpadDecimal", key: "Delete", expected: "\x1b[3~" }, + { mode: "\x1b[?1h", code: "Numpad8", key: "ArrowUp", expected: "\x1bOA" }, + { mode: "\x1b[>1u", code: "Numpad8", key: "ArrowUp", expected: "\x1b[57419u" }, + { mode: "\x1b=\x1b[?1035l", code: "Numpad8", key: "8", numLock: true, expected: "\x1bOx" }, + { mode: "\x1b[>1u", code: "Numpad8", key: "8", numLock: true, expected: "8" }, { code: "", key: "Backspace", expected: "\x7f" }, { code: "Unidentified", key: "Backspace", expected: "\x7f" }, { code: "Backspace", key: "Backspace", expected: "\x7f" }, { code: "Delete", key: "Backspace", expected: "\x1b[3~" }, { code: "", key: "a", expected: "a" }, { code: "", key: "Unidentified", expected: "" }, - ])("encodes key $key with physical code '$code'", async ({ code, key, expected }) => { - const core = await createCore(); - const event = { - code, - key, - repeat: false, - shiftKey: false, - ctrlKey: false, - altKey: false, - metaKey: false, - isComposing: false, - getModifierState: () => false, - } as unknown as KeyboardEvent; - - expect(core.encodeKey(event)).toBe(expected); - }); + ])( + "encodes key $key with physical code '$code'", + async ({ mode = "", code, key, numLock = false, expected }) => { + const core = await createCore(); + core.write(mode); + const event = { + code, + key, + repeat: false, + shiftKey: false, + ctrlKey: false, + altKey: false, + metaKey: false, + isComposing: false, + getModifierState: (modifier: string) => modifier === "NumLock" && numLock, + } as unknown as KeyboardEvent; + + expect(core.encodeKey(event)).toBe(expected); + }, + ); it.each(["", "\x1b[>1u"])("encodes macOS Option text with keyboard mode %j", async (mode) => { using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("MacIntel"); diff --git a/apps/web/src/terminal/ghostty/keyCodes.ts b/apps/web/src/terminal/ghostty/keyCodes.ts index bb13c09c8d2e0..d70fe13463c05 100644 --- a/apps/web/src/terminal/ghostty/keyCodes.ts +++ b/apps/web/src/terminal/ghostty/keyCodes.ts @@ -190,9 +190,32 @@ export function ghosttyKeyForCode(code: string): number { return codeToGhosttyKey.get(code) ?? 0; } +// With NumLock off, keypad digits report navigation keys; Ghostty encodes those as keypad variants. +const NUMPAD_NAVIGATION_KEYS = new Map( + ( + [ + ["ArrowDown", "NumpadArrowDown"], + ["ArrowLeft", "NumpadArrowLeft"], + ["ArrowRight", "NumpadArrowRight"], + ["ArrowUp", "NumpadArrowUp"], + ["Clear", "NumpadBegin"], + ["Delete", "NumpadDelete"], + ["End", "NumpadEnd"], + ["Home", "NumpadHome"], + ["Insert", "NumpadInsert"], + ["PageDown", "NumpadPageDown"], + ["PageUp", "NumpadPageUp"], + ] as const + ).map(([key, code]) => [key, ghosttyKeyForCode(code)]), +); + export function ghosttyKeyForEvent(event: Pick): number { - // Android browsers can omit the physical code for keys such as Backspace. - return ghosttyKeyForCode(event.code) || ghosttyKeyForCode(event.key); + return ( + (event.code.startsWith("Numpad") && NUMPAD_NAVIGATION_KEYS.get(event.key)) || + ghosttyKeyForCode(event.code) || + // Android browsers can omit the physical code for keys such as Backspace. + ghosttyKeyForCode(event.key) + ); } export interface GhosttyKeyboardLayoutMap { diff --git a/scripts/scenarios/terminal-numlock-off-keypad-navigates.e2e.ts b/scripts/scenarios/terminal-numlock-off-keypad-navigates.e2e.ts new file mode 100644 index 0000000000000..4b4652aa2d7a0 --- /dev/null +++ b/scripts/scenarios/terminal-numlock-off-keypad-navigates.e2e.ts @@ -0,0 +1,90 @@ +/// +// The DOM lib types the function this scenario runs in the page. +import { expect } from "e2e"; +import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; + +import { runPromise, test, WEB } from "./t3.ts"; + +// A raw-mode program turns on application cursor and keypad modes as ncurses keypad(TRUE) does, +// asks for DECCKM so its reply marks when the terminal has applied them, and logs what it reads. +const PROBE = String.raw`import { appendFileSync, writeFileSync } from "node:fs"; + +const log = new URL("received", import.meta.url); +writeFileSync(log, ""); +process.stdin.setRawMode(true); +process.stdin.on("data", (chunk) => { + appendFileSync(log, chunk); + if (chunk.includes("x")) { + process.stdout.write("\x1b[?1l\x1b>"); + process.stdin.setRawMode(false); + process.exit(0); + } +}); +process.stdout.write("\x1b[?1h\x1b=\x1b[?1$p"); +`; + +const READY = "\x1b[?1;1$y"; +// Keypad keys as a browser reports them with NumLock off: the physical digit, the navigation key. +const KEYPAD = [ + { code: "Numpad8", key: "ArrowUp" }, + { code: "Numpad1", key: "End" }, + { code: "NumpadDecimal", key: "Delete" }, +]; + +test( + "NumLock-off keypad navigation keys reach a terminal program", + WEB, + async ({ t3, app, browser, screen }) => { + const project = await t3.project("demo", { "keypad-probe.mjs": PROBE }); + const thread = await t3.thread({ project, title: "Keypad", messages: ["hello"] }); + const read = (file: string) => + runPromise( + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + return yield* fs.readFileString(`${project}/${file}`); + }).pipe(Effect.orElseSucceed(() => "")), + ); + await t3.open(thread); + + await screen.getByRole("button", "Toggle terminal drawer").tap(); + const terminal = screen.getByLabel("Terminal input"); + + // Input sent before the shell runs is dropped, so retype until it lands. + await expect + .poll( + async () => { + await terminal.focus(); + await browser.keyboard.type(`echo ready > '${project}/shell-ready'`); + await browser.keyboard.press("Enter"); + return read("shell-ready"); + }, + { timeout: 30_000 }, + ) + .toBe("ready\n"); + + await terminal.focus(); + await browser.keyboard.type(`'${process.execPath}' '${project}/keypad-probe.mjs'`); + await browser.keyboard.press("Enter"); + await expect.poll(() => read("received"), { timeout: 15_000 }).toBe(READY); + + // The e2e keyboard cannot press a keypad key with NumLock off, so send the events Chromium reports. + await browser.evaluate((keys) => { + const input = document.activeElement; + if (!input) throw new Error("Terminal input lost focus"); + for (const init of keys) { + for (const type of ["keydown", "keyup"]) { + const options = { ...init, location: 3, bubbles: true, cancelable: true }; + input.dispatchEvent(new KeyboardEvent(type, options)); + } + } + return null; + }, KEYPAD); + await browser.keyboard.type("x"); + await app.screenshot("after-keypad"); + + await expect + .poll(() => read("received"), { timeout: 10_000 }) + .toBe(`${READY}\x1bOA\x1bOF\x1b[3~x`); + }, +); From 8897a6ebe194b2d22955cda8936f519c484b35a9 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 15 Nov 2026 13:17:55 +0100 Subject: [PATCH 019/124] fix(web): keep thread mentions when editing sent messages (#37183) --- .../web/src/components/ChatView.logic.test.ts | 77 +++++++++++++++++++ apps/web/src/components/chat/chatViewSend.ts | 13 +++- .../components/chat/composerPromptHistory.ts | 7 +- ...edit-from-here-keeps-thread-mention.e2e.ts | 32 ++++++++ 4 files changed, 122 insertions(+), 7 deletions(-) create mode 100644 scripts/scenarios/edit-from-here-keeps-thread-mention.e2e.ts diff --git a/apps/web/src/components/ChatView.logic.test.ts b/apps/web/src/components/ChatView.logic.test.ts index d9877b83c01b1..d1ff164f16a90 100644 --- a/apps/web/src/components/ChatView.logic.test.ts +++ b/apps/web/src/components/ChatView.logic.test.ts @@ -1,3 +1,11 @@ +import { rewindToMessage } from "./chat/chatViewSend"; +import * as session from "../state/session"; +import { + environmentContextRecord, + environmentContextReference, + threadContextReference, +} from "../lib/composerContextRecords"; +import { threadContextRecord } from "@t3tools/shared/composerContextReferences"; import { prepareRevertedMessageAttachments, boundAttachmentIds, @@ -909,6 +917,75 @@ describe("waitForRevertedMessage", () => { afterEach(() => vi.restoreAllMocks()); + it("restores the rewound message's thread and machine mentions beside the existing draft", async () => { + const { state, projection } = projectionAtom(); + vi.spyOn(session, "readPreparedConnection").mockReturnValue({ + httpBaseUrl: "http://localhost", + } as never); + useComposerDraftStore.setState({ draftsByThreadKey: {}, draftThreadsByThreadKey: {} }); + const thread = (id: string, title: string) => + threadContextRecord( + { environmentId: threadRef.environmentId, threadId: ThreadId.make(id) }, + title, + ); + const drafted = thread("drafted-thread", "Drafted thread"); + const mentioned = thread("referenced-thread", "Other thread"); + const machine = environmentContextRecord(EnvironmentId.make("env-2"), "Build box"); + useComposerDraftStore.getState().addThreadContexts(threadRef, [drafted]); + const draftPrompt = useComposerDraftStore.getState().getComposerDraft(threadRef)!.prompt; + const sentPrompt = ensureInlineContextReferences("Review", [ + threadContextReference(mentioned), + environmentContextReference(machine), + ]).trim(); + + const result = await rewindToMessage({ + message: { + id: messageId, + role: "user", + runId: projection.messages[0]!.runId, + streaming: false, + createdAt: "2026-03-29T00:00:10.000Z", + updatedAt: "2026-03-29T00:00:10.000Z", + text: sentPrompt, + context: { version: 1, records: [mentioned, machine] }, + }, + environmentId: threadRef.environmentId, + routeThreadRef: threadRef, + threadId: threadRef.threadId, + turnCount: 1, + restoreFiles: false, + checkpoint: undefined, + draftTarget: threadRef, + composerHasPendingAttachments: false, + createAssetUrl: async () => { + throw new Error("No attachment expected"); + }, + revertThreadCheckpoint: async () => { + appAtomRegistry.set(state, { + data: Option.some({ + ...projection, + providerThreads: projection.providerThreads.map((providerThread) => ({ + ...providerThread, + lastRunOrdinal: 1, + })), + runs: projection.runs.map((run) => + run.ordinal > 1 ? { ...run, status: "rolled_back" as const } : run, + ), + }), + }); + return AsyncResult.success(undefined); + }, + }); + + const prompt = `${draftPrompt}\n\n${sentPrompt}`; + expect(result).toEqual({ error: null, prompt }); + expect(useComposerDraftStore.getState().getComposerDraft(threadRef)).toMatchObject({ + prompt, + threadContexts: [drafted, mentioned], + environmentContexts: [machine], + }); + }); + it("rejects with the projected reason when the rollback fails for good", async () => { const { state, projection } = projectionAtom(); const waiting = waitForRevertedMessage(threadRef, 1, requestId, async () => {}); diff --git a/apps/web/src/components/chat/chatViewSend.ts b/apps/web/src/components/chat/chatViewSend.ts index 58f12549d15f3..079e6d18d5eed 100644 --- a/apps/web/src/components/chat/chatViewSend.ts +++ b/apps/web/src/components/chat/chatViewSend.ts @@ -46,7 +46,7 @@ import { waitForRevertedMessage, } from "../ChatView.logic"; import { readPreparedConnection } from "../../state/session"; -import { recallableComposerPrompt } from "./composerPromptHistory"; +import { recallComposerMessage } from "./composerPromptHistory"; // Kept per route so a send in flight, and the multiple-model selections, survive // ChatView remounting while the user navigates. @@ -151,8 +151,8 @@ export async function startFanoutThread(input: { } /** - * Reverts the thread to before `message` and puts that message's text and - * attachments back in the composer draft. Returns the restored prompt, or the + * Reverts the thread to before `message` and puts that message's text, mentions + * and attachments back in the composer draft. Returns the restored prompt, or the * error to show; on error the draft is untouched. */ export async function rewindToMessage(input: { @@ -221,7 +221,7 @@ export async function rewindToMessage(input: { if (result._tag === "Failure") throw squashAtomCommandFailure(result); }); const currentPrompt = store.getComposerDraft(draftTarget)?.prompt ?? ""; - const restoredPrompt = recallableComposerPrompt(message.text); + const { prompt: restoredPrompt, mentions } = recallComposerMessage(message); const nextPrompt = restoredPrompt.length === 0 ? currentPrompt @@ -229,6 +229,11 @@ export async function rewindToMessage(input: { ? `${currentPrompt}\n\n${restoredPrompt}` : restoredPrompt; store.setPrompt(draftTarget, nextPrompt); + // The restored prompt already carries the chips; these add only their records. + store.addThreadContexts(draftTarget, mentions.threadContexts, { appendReference: false }); + store.addEnvironmentContexts(draftTarget, mentions.environmentContexts, { + appendReference: false, + }); const images: ComposerImageAttachment[] = []; const restoredFiles: ComposerFileAttachment[] = []; files.forEach((file, index) => { diff --git a/apps/web/src/components/chat/composerPromptHistory.ts b/apps/web/src/components/chat/composerPromptHistory.ts index a4f4ed62a7c04..65244d9259b86 100644 --- a/apps/web/src/components/chat/composerPromptHistory.ts +++ b/apps/web/src/components/chat/composerPromptHistory.ts @@ -141,8 +141,9 @@ export function recallableComposerPrompt( return trimmed; } -function recallMessage( - message: ComposerPromptHistoryMessage, +/** A sent message's text as the composer can hold it, with the mention records its chips need. */ +export function recallComposerMessage( + message: Pick, ): Pick { const records = message.context?.records ?? []; const threadRecords = records.filter(isThreadContextRecord); @@ -172,7 +173,7 @@ export function buildComposerPromptHistoryEntries( if (message.role !== "user") continue; // Automations and other agents send user-role messages; recall only what the user typed. if (resolveUserMessagePresentation(message).attribution !== null) continue; - const { prompt, mentions } = recallMessage(message); + const { prompt, mentions } = recallComposerMessage(message); if (prompt.length === 0) continue; const entry = { id: message.id, prompt, mentions }; const previous = entries[entries.length - 1]; diff --git a/scripts/scenarios/edit-from-here-keeps-thread-mention.e2e.ts b/scripts/scenarios/edit-from-here-keeps-thread-mention.e2e.ts new file mode 100644 index 0000000000000..677d34522d7c0 --- /dev/null +++ b/scripts/scenarios/edit-from-here-keeps-thread-mention.e2e.ts @@ -0,0 +1,32 @@ +import { expect } from "e2e"; + +import { test, WEB } from "./t3.ts"; + +test( + "Edit from here puts a sent message back with its thread mention", + WEB, + async ({ t3, app, screen, browser }) => { + const project = await t3.project("demo"); + await t3.thread({ project, title: "Release notes", messages: ["Draft the notes"] }); + const thread = await t3.thread({ project, title: "Review work", messages: ["Start"] }); + await t3.open(thread); + + const composer = screen.getByRole("textbox"); + await composer.pressSequentially("Review @Release"); + await screen.getByRole("option", "Release notes", { exact: false }).tap(); + await expect(composer).toContainText("Release notes"); + await browser.keyboard.press("Enter"); + + // The seeded message came from the CLI, which the timeline attributes to an agent: only the + // message sent here offers Edit from here. + const editFromHere = screen.getByRole("button", "Edit from here"); + await expect(editFromHere).toHaveCount(1, { timeout: 60_000 }); + await expect(editFromHere).toBeEnabled({ timeout: 60_000 }); + await editFromHere.tap(); + await screen.getByRole("button", "Revert and keep changes").tap(); + + await expect(composer).toContainText("Review"); + await app.screenshot("restored-draft"); + await expect(composer).toContainText("Release notes"); + }, +); From dcaedc980034008cdf99fe18340cd3018869f154 Mon Sep 17 00:00:00 2001 From: Katya Date: Sun, 15 Nov 2026 13:23:29 +0100 Subject: [PATCH 020/124] fix(server): skills linked by a SessionStart hook appear in the $ picker in the same session (#37165) Co-authored-by: Julius Marminge --- .../Adapters/ClaudeAdapterV2.test.ts | 76 +++++ .../Adapters/ClaudeAdapterV2.ts | 16 +- .../src/provider/Drivers/ClaudeDriver.ts | 8 + .../src/provider/ProviderRegistry.test.ts | 290 ++++++++++++++++++ apps/server/src/provider/ProviderRegistry.ts | 87 +++++- .../client-runtime/src/providerSkills.test.ts | 35 ++- packages/client-runtime/src/providerSkills.ts | 14 +- .../client-runtime/src/state/server.test.ts | 10 + packages/client-runtime/src/state/server.ts | 19 +- packages/contracts/src/server.ts | 5 + packages/provider-core/src/server/driver.ts | 6 + 11 files changed, 533 insertions(+), 33 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 419b5a340389b..4bb712aa9ffca 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -3550,6 +3550,7 @@ describe("ClaudeAdapterV2 background wake turns", () => { > & Partial>; readonly environment?: NodeJS.ProcessEnv; + readonly onSessionInit?: (cwd: string) => Effect.Effect; readonly subagentLaunchToolUseId?: string; readonly subagentModel?: string; readonly nativeSessionId?: string; @@ -3625,6 +3626,7 @@ describe("ClaudeAdapterV2 background wake turns", () => { path: yield* Path.Path, random: yield* SecureRandom.SecureRandom, idAllocator, + ...(options?.onSessionInit ? { onSessionInit: options.onSessionInit } : {}), ...(options?.routeModelCatalog ? { routeModelCatalog: options.routeModelCatalog } : {}), continuationRequests: { offer: (request) => @@ -4167,6 +4169,80 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); + it.effect("reports the session cwd on each process's first init, not on repeated inits", () => + Effect.gen(function* () { + const initCwds: Array = []; + const harness = yield* makeWakeHarnessWithOptions({ + freshQueueOnReopen: true, + close: (queue) => Queue.shutdown(queue), + onSessionInit: (cwd) => + Effect.sync(() => { + initCwds.push(cwd); + }), + }); + // The CLI may report a resolved path, not the cwd clients key their snapshots on. + const init = (uuid: string) => + claudeSdkFrame({ + type: "system", + subtype: "init", + cwd: "/private/workspace", + session_id: WAKE_NATIVE_SESSION, + uuid, + }); + const latest = (): OrchestrationV2ProviderThread => { + const event = harness.events.findLast( + (candidate) => candidate.type === "provider_thread.updated", + ); + return event?.type === "provider_thread.updated" + ? event.providerThread + : harness.providerThread; + }; + const turn = (ordinal: number) => + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: latest(), + now: DateTime.makeUnsafe(0), + attemptId: RunAttemptId.make(`session-init-${ordinal}`), + text: `Turn ${ordinal}`, + attachments: [], + providerTurnOrdinal: ordinal, + }); + yield* harness.runtime.startTurn(turn(1)); + assert.deepEqual(initCwds, []); + yield* harness.offerAndWait(init("00000000-0000-4000-8000-000000000701")); + assert.deepEqual(initCwds, ["/workspace"]); + yield* harness.offerAndWait(turnOneResult); + yield* Queue.take(harness.terminalReceipts); + // The CLI repeats init on every turn of one process; its startup hooks ran once. + yield* harness.runtime.startTurn(turn(2)); + yield* harness.offerAndWait(init("00000000-0000-4000-8000-000000000702")); + yield* harness.offerAndWait( + makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000703", result: "Two." }), + ); + yield* Queue.take(harness.terminalReceipts); + assert.deepEqual(initCwds, ["/workspace"]); + yield* harness.runtime.interruptTurn({ + providerThread: latest(), + providerTurnId: lastProviderTurn(harness.events).id, + requestRuntimeRestart: true, + }); + yield* harness.runtime.startTurn(turn(3)); + assert.equal(harness.processQueues.length, 2); + yield* harness.offerAndWait(init("00000000-0000-4000-8000-000000000704")); + assert.deepEqual(initCwds, ["/workspace", "/workspace"]); + }).pipe( + Effect.scoped, + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + it.effect("resumes the session a /clear selected after the Claude process is replaced", () => Effect.gen(function* () { const harness = yield* makeWakeHarnessWithOptions({ diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index a83be3cd01527..e8cfd9d8ce08d 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -5468,6 +5468,11 @@ export interface ClaudeAdapterV2Options { readonly queryRunner: ClaudeAgentSdkQueryRunnerShape; readonly scopedLimitNames?: Ref.Ref; readonly onUsageLimits?: ManagedServerProvider["applyUsageLimits"]; + /** + * Called with the requested cwd on a process's first init message, after its synchronous + * SessionStart hooks have run, so cwd-keyed workspace scans can be refreshed. + */ + readonly onSessionInit?: (cwd: string) => Effect.Effect; /** * Applies Claude Code's reported model ids for a turn's cwd, so turns send ids its backend * accepts. Defaults to the catalog unchanged. @@ -12812,11 +12817,13 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( Stream.runForEach((message) => sdkMessageLock.withPermits(1)( Effect.suspend(() => { + let firstInit = false; if ( message.type === "system" && message.subtype === "init" && !context.receivedInit ) { + firstInit = true; context.openedPermissionMode = message.permissionMode ?? context.openedPermissionMode; context.receivedInit = true; @@ -12855,7 +12862,12 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( resultsReportingNewContextWindow.add(message); } } - return handleSdkMessage({ query: querySession, message }); + const handled = handleSdkMessage({ query: querySession, message }); + const reportedCwd = turnInput.runtimePolicy.cwd; + if (!firstInit || reportedCwd === null || !adapterOptions.onSessionInit) { + return handled; + } + return adapterOptions.onSessionInit(reportedCwd).pipe(Effect.andThen(handled)); }), ), ), @@ -14385,7 +14397,7 @@ export const createClaudeAdapterV2 = Effect.fn("ClaudeAdapterV2Driver.create")( input: ProviderAdapterDriverCreateInput, hooks: Pick< ClaudeAdapterV2Options, - "scopedLimitNames" | "onUsageLimits" | "modelCatalog" | "routeModelCatalog" + "scopedLimitNames" | "onUsageLimits" | "onSessionInit" | "modelCatalog" | "routeModelCatalog" > = {}, ) { const { instanceId, environment, enabled, config } = input; diff --git a/apps/server/src/provider/Drivers/ClaudeDriver.ts b/apps/server/src/provider/Drivers/ClaudeDriver.ts index d75b1cad86402..5bedffe9242b4 100644 --- a/apps/server/src/provider/Drivers/ClaudeDriver.ts +++ b/apps/server/src/provider/Drivers/ClaudeDriver.ts @@ -1,13 +1,16 @@ import { ClaudeSettings, ProviderDriverKind } from "@t3tools/contracts"; import * as Cache from "effect/Cache"; +import type * as Cause from "effect/Cause"; import * as Duration from "effect/Duration"; import * as SecureRandom from "effect/crypto/SecureRandom"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; import * as FileSystem from "effect/FileSystem"; import * as Path from "effect/Path"; +import * as Queue from "effect/Queue"; import * as Ref from "effect/Ref"; import * as Schema from "effect/Schema"; +import * as Stream from "effect/Stream"; import { HttpClient } from "effect/http"; import { ChildProcessSpawner } from "effect/process"; @@ -167,6 +170,9 @@ export const ClaudeDriver: ProviderDriver(); + yield* Effect.addFinalizer(() => Queue.end(workspaceRescans)); const orchestrationAdapter = yield* createClaudeAdapterV2( { instanceId, @@ -179,6 +185,7 @@ export const ClaudeDriver: ProviderDriver snapshot.applyUsageLimits(update), + onSessionInit: (cwd) => Queue.offer(workspaceRescans, cwd).pipe(Effect.asVoid), modelCatalog, routeModelCatalog: modelRoutes.route, }, @@ -412,6 +419,7 @@ export const ClaudeDriver: ProviderDriver snapshot.getSnapshot.pipe( Effect.flatMap((machineSnapshot) => diff --git a/apps/server/src/provider/ProviderRegistry.test.ts b/apps/server/src/provider/ProviderRegistry.test.ts index c42d78b237d42..27c1cd15a8d79 100644 --- a/apps/server/src/provider/ProviderRegistry.test.ts +++ b/apps/server/src/provider/ProviderRegistry.test.ts @@ -156,6 +156,135 @@ const layerBackgroundPolicyAlwaysRun = Layer.mock(BackgroundPolicy.BackgroundPol shouldRunOpportunisticWork: Effect.succeed(true), }); +const makeWorkspaceRescanHarness = Effect.gen(function* () { + const instanceId = ProviderInstanceId.make("claude"); + const driver = ProviderDriverKind.make("claudeCode"); + const provider = { + instanceId, + driver, + status: "ready", + enabled: true, + installed: true, + auth: { status: "authenticated" }, + checkedAt: "1970-01-01T00:00:00.000Z", + version: "1.0.0", + models: [], + slashCommands: [], + skills: [], + } as const satisfies ServerProvider; + const scans = yield* Queue.unbounded<{ + readonly cwd: string; + readonly result: Deferred.Deferred; + }>(); + const makeInstance = Effect.gen(function* () { + const rescans = yield* Queue.unbounded(); + const reports = yield* Queue.unbounded(); + const instance: ProviderInstance = { + instanceId, + driverKind: driver, + continuationIdentity: { driverKind: driver, continuationKey: "claude:rescan-test" }, + 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, + }, + // A pull-based stream pulls the next report only after the registry queued the last. + workspaceRescans: Stream.fromQueue(rescans).pipe( + Stream.tap((cwd) => Queue.offer(reports, cwd)), + ), + snapshotForCwd: (cwd) => + Effect.gen(function* () { + const result = yield* Deferred.make(); + yield* Queue.offer(scans, { cwd, result }); + return yield* Deferred.await(result); + }), + orchestrationAdapter: {} as ProviderInstance["orchestrationAdapter"], + textGeneration: {} as ProviderInstance["textGeneration"], + }; + return { instance, rescans, reports }; + }); + const first = yield* makeInstance; + const instances = yield* Ref.make([first.instance]); + const changes = yield* PubSub.unbounded(); + const scope = yield* Scope.make(); + yield* Effect.addFinalizer(() => Scope.close(scope, Exit.void)); + const services = yield* Layer.build( + ProviderRegistry.layer.pipe( + Layer.provide(layerUnusedMutator), + Layer.provideMerge( + Layer.succeed(ProviderInstanceRegistry.ProviderInstanceRegistry, { + getInstance: (id) => + Ref.get(instances).pipe(Effect.map((all) => all.find((i) => i.instanceId === id))), + listInstances: Ref.get(instances), + listUnavailable: Effect.succeed([]), + streamChanges: Stream.fromPubSub(changes), + subscribeChanges: PubSub.subscribe(changes), + }), + ), + Layer.provideMerge(ServerConfig.layerTest(process.cwd(), { prefix: "t3-workspace-rescan-" })), + Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), + ), + ).pipe(Scope.provide(scope)); + const registry = yield* ProviderRegistry.ProviderRegistry.pipe(Effect.provide(services)); + const snapshot = (name: string): ProviderWorkspaceSnapshot => ({ + ...provider, + skills: [{ name, path: `/workspace/${name}/SKILL.md`, enabled: true }], + }); + const nextScan = Effect.fnUntraced(function* (cwd: string) { + const scan = yield* Queue.take(scans); + assert.equal(scan.cwd, cwd); + return (name: string) => Deferred.succeed(scan.result, snapshot(name)); + }); + const refresh = (cwd: string, fresh = false) => + registry.refreshWorkspaceSnapshot({ instanceId, cwd, fresh }); + const seed = Effect.fnUntraced(function* (cwd: string) { + const pending = yield* refresh(cwd).pipe(Effect.forkChild); + const complete = yield* nextScan(cwd); + yield* complete("initial"); + yield* Fiber.join(pending); + }); + // A later requested cwd reaching the serial consumer proves the previous rescan settled, + // including when that rescan correctly publishes no change. Seed "/barrier" first; it rescans + // to the skills it was seeded with, so it never stamps workspaceRescannedAt. + const drainRescans = Effect.gen(function* () { + yield* Queue.offer(first.rescans, "/barrier"); + const complete = yield* nextScan("/barrier"); + yield* complete("initial"); + }); + const skillsOf = (cwd: string) => + Effect.map(registry.getProviders, (providers) => + providers[0]?.workspaceSnapshots?.find((s) => s.cwd === cwd)?.skills?.map((s) => s.name), + ); + const rescannedAtOf = Effect.map( + registry.getProviders, + (providers) => providers[0]?.workspaceRescannedAt, + ); + return { + first, + makeInstance, + instances, + changes, + registry, + scans, + nextScan, + refresh, + seed, + drainRescans, + skillsOf, + rescannedAtOf, + }; +}); + function selectDescriptor( id: string, label: string, @@ -931,6 +1060,20 @@ it.layer( unchanged.workspaceSnapshots?.map((snapshot) => snapshot.cwd), ["/project", "/plain"], ); + // A machine health refresh must not read as a rescan, nor erase the last one. + const rescanned = { ...withCatalog, workspaceRescannedAt: "2026-03-25T00:01:00.000Z" }; + assert.equal( + ProviderRegistry.mergeProviderSnapshot(rescanned, provider).workspaceRescannedAt, + "2026-03-25T00:01:00.000Z", + ); + assert.equal( + ProviderRegistry.mergeProviderSnapshot(rescanned, { + ...provider, + workspaceRescannedAt: "2026-03-25T00:02:00.000Z", + }).workspaceRescannedAt, + "2026-03-25T00:02:00.000Z", + ); + assert.equal(unchanged.workspaceRescannedAt, undefined); const changed = ProviderRegistry.mergeProviderSnapshot(withCatalog, { ...provider, models: [machineModel, addedModel], @@ -2285,6 +2428,153 @@ it.layer( }), ); + it.effect("rescans a requested cwd whose snapshot is still current", () => + Effect.gen(function* () { + const h = yield* makeWorkspaceRescanHarness; + yield* h.seed("/barrier"); + yield* h.seed("/workspace"); + assert.deepEqual(yield* h.skillsOf("/workspace"), ["initial"]); + yield* Queue.offer(h.first.rescans, "/workspace"); + const complete = yield* h.nextScan("/workspace"); + yield* complete("hook"); + yield* h.drainRescans; + assert.deepEqual(yield* h.skillsOf("/workspace"), ["hook"]); + // Clients that fetched the old skills are told to fetch again. + const scannedAt = (yield* h.registry.getProviders)[0]?.workspaceSnapshots?.find( + (snapshot) => snapshot.cwd === "/workspace", + )?.checkedAt; + assert.isString(scannedAt); + assert.equal(yield* h.rescannedAtOf, scannedAt); + }), + ); + + it.effect("stamps a rescan whose awaited scan already published the change", () => + Effect.gen(function* () { + const h = yield* makeWorkspaceRescanHarness; + yield* h.seed("/barrier"); + yield* h.seed("/workspace"); + const ordinary = yield* h.refresh("/workspace", true).pipe(Effect.forkChild); + const completeOrdinary = yield* h.nextScan("/workspace"); + yield* Queue.offer(h.first.rescans, "/workspace"); + yield* Queue.take(h.first.reports); + // Let the consumer reach its wait on the running scan. + yield* TestClock.adjust(0); + yield* completeOrdinary("hook"); + yield* Fiber.join(ordinary); + const completeRescan = yield* h.nextScan("/workspace"); + yield* completeRescan("hook"); + yield* h.drainRescans; + assert.isString(yield* h.rescannedAtOf); + }), + ); + + it.effect("leaves workspaceRescannedAt unset when a rescan changes nothing", () => + Effect.gen(function* () { + const h = yield* makeWorkspaceRescanHarness; + yield* h.seed("/barrier"); + yield* h.seed("/workspace"); + yield* Queue.offer(h.first.rescans, "/workspace"); + const complete = yield* h.nextScan("/workspace"); + yield* complete("initial"); + yield* h.drainRescans; + assert.deepEqual(yield* h.skillsOf("/workspace"), ["initial"]); + assert.equal(yield* h.rescannedAtOf, undefined); + }), + ); + + it.effect("starts no scan for a reported cwd nobody requested", () => + Effect.gen(function* () { + const h = yield* makeWorkspaceRescanHarness; + yield* h.seed("/barrier"); + yield* Queue.offer(h.first.rescans, "/unrequested"); + // The next scan being the barrier's shows the report started none. + yield* h.drainRescans; + assert.deepEqual(yield* h.skillsOf("/unrequested"), undefined); + }), + ); + + it.effect("rescans after an initial scan in flight when the hook report arrives", () => + Effect.gen(function* () { + const h = yield* makeWorkspaceRescanHarness; + yield* h.seed("/barrier"); + const initial = yield* h.refresh("/workspace").pipe(Effect.forkChild); + const completeInitial = yield* h.nextScan("/workspace"); + yield* Queue.offer(h.first.rescans, "/workspace"); + yield* Queue.take(h.first.reports); + // Let the consumer reach its wait on the running scan. + yield* TestClock.adjust(0); + yield* completeInitial("initial"); + // The requesting client's reply is its only copy of the scan, so the rescan must not + // supersede it. + const reply = yield* Fiber.join(initial); + assert.deepEqual( + reply[0]?.workspaceSnapshots + ?.find((s) => s.cwd === "/workspace") + ?.skills?.map((s) => s.name), + ["initial"], + ); + const completeRescan = yield* h.nextScan("/workspace"); + yield* completeRescan("hook"); + yield* h.drainRescans; + assert.deepEqual(yield* h.skillsOf("/workspace"), ["hook"]); + }), + ); + + it.effect("lets a fresh refresh started while a rescan waits keep its own result", () => + Effect.gen(function* () { + const h = yield* makeWorkspaceRescanHarness; + yield* h.seed("/barrier"); + const initial = yield* h.refresh("/workspace").pipe(Effect.forkChild); + const completeInitial = yield* h.nextScan("/workspace"); + yield* Queue.offer(h.first.rescans, "/workspace"); + yield* Queue.take(h.first.reports); + // Let the consumer reach its wait on the running scan. + yield* TestClock.adjust(0); + const fresh = yield* h.refresh("/workspace", true).pipe(Effect.forkChild); + const completeFresh = yield* h.nextScan("/workspace"); + yield* completeInitial("initial"); + yield* completeFresh("fresh"); + yield* Fiber.join(initial); + const reply = yield* Fiber.join(fresh); + assert.deepEqual( + reply[0]?.workspaceSnapshots + ?.find((s) => s.cwd === "/workspace") + ?.skills?.map((s) => s.name), + ["fresh"], + ); + // The rescan joins the fresh scan, so the next scan started is the barrier's. + yield* h.drainRescans; + assert.deepEqual(yield* h.skillsOf("/workspace"), ["fresh"]); + }), + ); + + it.effect("ignores a queued rescan from a replaced instance", () => + Effect.gen(function* () { + const h = yield* makeWorkspaceRescanHarness; + yield* h.seed("/workspace"); + const replacement = yield* h.makeInstance; + const rebuilt = yield* h.registry.streamChanges.pipe( + Stream.filter((providers) => providers[0]?.workspaceSnapshots === undefined), + Stream.runHead, + Effect.forkChild({ startImmediately: true }), + ); + yield* Ref.set(h.instances, [replacement.instance]); + yield* PubSub.publish(h.changes, undefined); + yield* Fiber.join(rebuilt); + yield* h.seed("/workspace"); + yield* h.seed("/barrier"); + // The forwarder pulls the second report only after queueing the first. + yield* Queue.offerAll(h.first.rescans, ["/workspace", "/workspace"]); + yield* Queue.take(h.first.reports); + yield* Queue.take(h.first.reports); + yield* Queue.offer(replacement.rescans, "/barrier"); + // A scan started for the stale report would come before the barrier's. + const completeBarrier = yield* h.nextScan("/barrier"); + yield* completeBarrier("barrier"); + assert.deepEqual(yield* h.skillsOf("/workspace"), ["initial"]); + }), + ); + it.effect("scans scanCwd and keys the workspace snapshot by cwd", () => Effect.gen(function* () { const driver = ProviderDriverKind.make("codex"); diff --git a/apps/server/src/provider/ProviderRegistry.ts b/apps/server/src/provider/ProviderRegistry.ts index 8a7ff0ac7fd37..f1ae2e51637cc 100644 --- a/apps/server/src/provider/ProviderRegistry.ts +++ b/apps/server/src/provider/ProviderRegistry.ts @@ -20,6 +20,7 @@ import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as Path from "effect/Path"; import * as PubSub from "effect/PubSub"; +import * as Queue from "effect/Queue"; import * as Ref from "effect/Ref"; import * as Scope from "effect/Scope"; import * as Stream from "effect/Stream"; @@ -329,6 +330,10 @@ export const mergeProviderSnapshot = ( : previousWorkspaceSnapshots !== undefined ? { workspaceSnapshots: previousWorkspaceSnapshots } : {}), + ...(nextProvider.workspaceRescannedAt === undefined && + previousProvider.workspaceRescannedAt !== undefined + ? { workspaceRescannedAt: previousProvider.workspaceRescannedAt } + : {}), ...(shouldRetainMissingOpenCodeMetadata(nextProvider) ? { slashCommands: @@ -481,6 +486,10 @@ export const layer = Layer.effect( const workspaceRefreshesRef = yield* Ref.make< ReadonlyMap>> >(new Map()); + const workspaceRescans = yield* Queue.unbounded<{ + readonly instance: ProviderInstance; + readonly cwd: string; + }>(); const maintenanceActionStatesRef = yield* Ref.make< ReadonlyMap< ProviderInstanceId, @@ -501,7 +510,11 @@ export const layer = Layer.effect( cacheDir: config.providerStatusCacheDir, instanceId: key, }).pipe(Effect.provideService(Path.Path, path)); - const { workspaceSnapshots: _workspaceSnapshots, ...machineProvider } = provider; + const { + workspaceSnapshots: _workspaceSnapshots, + workspaceRescannedAt: _workspaceRescannedAt, + ...machineProvider + } = provider; yield* writeProviderStatusCache({ filePath, provider: machineProvider }).pipe( Effect.provideService(FileSystem.FileSystem, fileSystem), Effect.provideService(Path.Path, path), @@ -927,6 +940,11 @@ export const layer = Layer.effect( yield* Stream.runForEach(source.streamChanges, (provider) => correlateSnapshotWithSource(source, provider).pipe(Effect.flatMap(syncProvider)), ).pipe(Effect.forkScoped); + if (instance.workspaceRescans) { + yield* Stream.runForEach(instance.workspaceRescans, (cwd) => + Queue.offer(workspaceRescans, { instance, cwd }), + ).pipe(Effect.forkScoped); + } } yield* Effect.yieldNow; @@ -1043,7 +1061,25 @@ export const layer = Layer.effect( readonly cwd: string; readonly scanCwd?: string; readonly fresh?: boolean; + // Set by an instance's own report that its workspace skills changed. + readonly rescan?: ProviderInstance; }) { + const workspaceSnapshotOf = (candidate: ServerProvider | undefined) => + candidate?.workspaceSnapshots?.find((s) => s.cwd === input.cwd); + // What clients saw before the awaited scan, which may already have published the change. + const heldBeforeWait = input.rescan + ? workspaceSnapshotOf( + (yield* Ref.get(providersRef)).find( + (candidate) => candidate.instanceId === input.instanceId, + ), + ) + : undefined; + // A rescan waits for a running scan, which may have read files before the hook linked them. + // Claims are read before snapshots: a scan publishes before it releases its claim. + const runningScan = input.rescan + ? (yield* Ref.get(workspaceRefreshesRef)).get(input.rescan)?.get(input.cwd) + : undefined; + if (runningScan) yield* Deferred.await(runningScan); if (input.fresh) { yield* updateProviders((providers) => providers.map((candidate) => @@ -1055,14 +1091,13 @@ export const layer = Layer.effect( } const providers = yield* Ref.get(providersRef); const provider = providers.find((candidate) => candidate.instanceId === input.instanceId); - const workspaceSnapshotOf = (candidate: ServerProvider | undefined) => - candidate?.workspaceSnapshots?.find((s) => s.cwd === input.cwd); const scannedFrom = workspaceSnapshotOf(provider); const now = yield* DateTime.now; if ( !provider || !provider.enabled || (!input.fresh && + !input.rescan && scannedFrom && !isProviderWorkspaceSnapshotPending(scannedFrom) && isProviderWorkspaceSnapshotCurrent(scannedFrom, DateTime.toEpochMillis(now))) @@ -1074,6 +1109,10 @@ export const layer = Layer.effect( const instance = yield* instanceRegistry.getInstance(input.instanceId); const snapshotForCwd = instance?.snapshotForCwd; if (!instance || !snapshotForCwd) return providers; + // A report never enrolls a cwd nobody requested, nor outlives the instance that made it. + if (input.rescan && (input.rescan !== instance || (!runningScan && !scannedFrom))) { + return providers; + } const token = yield* Deferred.make(); // A machine refresh can change this cwd's snapshot, so the scan publishes against the result. const refreshMachineSnapshot = input.fresh @@ -1104,18 +1143,31 @@ export const layer = Layer.effect( if (Ref.getUnsafe(workspaceRefreshesRef).get(instance)?.get(input.cwd) !== token) { return currentProviders; } - return currentProviders.map((candidate) => - candidate.instanceId === input.instanceId && - Equal.equals(workspaceSnapshotOf(candidate), scanBase) - ? upsertProviderWorkspaceSnapshot(candidate, input.cwd, { - ...scopedSnapshot, - checkedAt: scannedAt, - // Without driver command discovery, retain previous commands after failed health checks and retry. - slashCommandsPending: - scopedSnapshot.slashCommandsPending ?? scopedSnapshot.status === "error", - }) - : candidate, - ); + return currentProviders.map((candidate) => { + if ( + candidate.instanceId !== input.instanceId || + !Equal.equals(workspaceSnapshotOf(candidate), scanBase) + ) { + return candidate; + } + const scanned = upsertProviderWorkspaceSnapshot(candidate, input.cwd, { + ...scopedSnapshot, + checkedAt: scannedAt, + // Without driver command discovery, retain previous commands after failed health checks and retry. + slashCommandsPending: + scopedSnapshot.slashCommandsPending ?? scopedSnapshot.status === "error", + }); + // Clients keep fetched snapshots, so a changed rescan must tell them to fetch again. + const rescanned = workspaceSnapshotOf(scanned); + const held = heldBeforeWait ?? scanBase; + return input.rescan && + !( + Equal.equals(rescanned?.skills, held?.skills) && + Equal.equals(rescanned?.slashCommands, held?.slashCommands) + ) + ? { ...scanned, workspaceRescannedAt: scannedAt } + : scanned; + }); }); }), ), @@ -1158,6 +1210,11 @@ export const layer = Layer.effect( } return yield* Fiber.join(claim.scan); }); + yield* Stream.runForEach(Stream.fromQueue(workspaceRescans), ({ instance, cwd }) => + refreshWorkspaceSnapshot({ instanceId: instance.instanceId, cwd, rescan: instance }).pipe( + Effect.ignoreCause({ log: true }), + ), + ).pipe(Effect.forkScoped); const invalidateModelCaches = Effect.fn("ProviderRegistry.invalidateModelCaches")(function* ( input: Parameters[0], diff --git a/packages/client-runtime/src/providerSkills.test.ts b/packages/client-runtime/src/providerSkills.test.ts index acd4a4904c1d4..143efdbe7869b 100644 --- a/packages/client-runtime/src/providerSkills.test.ts +++ b/packages/client-runtime/src/providerSkills.test.ts @@ -176,16 +176,25 @@ describe("provider workspace snapshot refresh atom", () => { | null | Promise | null>; - function setup(initialProviders: ReadonlyArray, initialResponses?: Response[]) { + function setup( + initialProviders: ReadonlyArray, + initialResponses?: Response[], + // The client drops a reply whose provider changed while it was in flight. + options?: { readonly droppedReplies?: number }, + ) { + let droppedReplies = options?.droppedReplies ?? 0; const providersAtom = Atom.make | null>(initialProviders); const responses: Response[] = [...(initialResponses ?? [])]; const run = vi.fn(async () => { const providers = await (responses.shift() ?? (() => null))(); - return providers === null - ? AsyncResult.failure<{ providers: ReadonlyArray }, string>( - Cause.fail("offline"), - ) - : AsyncResult.success({ providers }); + if (providers === null) { + return AsyncResult.failure<{ providers: ReadonlyArray }, string>( + Cause.fail("offline"), + ); + } + if (droppedReplies > 0) droppedReplies -= 1; + else registry.set(providersAtom, providers); + return AsyncResult.success({ providers }); }); const family = createProviderWorkspaceSnapshotRefreshAtomFamily({ providers: () => providersAtom, @@ -243,6 +252,20 @@ describe("provider workspace snapshot refresh atom", () => { }, ); + it("asks again at once when the client dropped a complete reply", async () => { + vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") }); + const scanned = withSnapshot("2026-01-01T00:00:00.000Z"); + const { registry, atom, run, unmount } = setup(noSnapshot, [() => scanned, () => scanned], { + droppedReplies: 1, + }); + await vi.advanceTimersByTimeAsync(0); + expect(run).toHaveBeenCalledTimes(2); + expect(isProviderWorkspaceSnapshotScanning(registry.get(atom))).toBe(false); + registry.refresh(atom); + expect(run).toHaveBeenCalledTimes(2); + unmount(); + }); + it("keeps the TTL guard for a newly scanned catalog when the client clock is ahead", async () => { vi.useFakeTimers({ now: Date.parse("2026-01-01T01:00:00.000Z") }); const { registry, atom, run, unmount } = setup(withSnapshot("2026-01-01T00:00:00.000Z"), [ diff --git a/packages/client-runtime/src/providerSkills.ts b/packages/client-runtime/src/providerSkills.ts index 0f91fc29f74e5..e7dfb12feafe0 100644 --- a/packages/client-runtime/src/providerSkills.ts +++ b/packages/client-runtime/src/providerSkills.ts @@ -235,6 +235,7 @@ function failProviderWorkspaceSnapshotRefresh( state: ProviderWorkspaceSnapshotRefreshState, requestedAt: number, now: number, + cooldownMs = WORKSPACE_SNAPSHOT_RETRY_COOLDOWN_MS, ): ProviderWorkspaceSnapshotRefreshState { // A newer request, or a snapshot loss, already superseded this one. if (state.requestedAt !== requestedAt) return state; @@ -243,7 +244,7 @@ function failProviderWorkspaceSnapshotRefresh( return { ...state, requestedAt: null, - retryNotBefore: now + WORKSPACE_SNAPSHOT_RETRY_COOLDOWN_MS, + retryNotBefore: cooldownMs === 0 ? null : now + cooldownMs, failedAttempts, }; } @@ -335,14 +336,15 @@ export function createProviderWorkspaceSnapshotRefreshAtomFamily(input: { // Outlives this build on purpose: a rebuild must not drop the retry. // Once nothing subscribes, the retry state dies with the node; writing // would recreate and rebuild it, which scans again with no subscriber. - const retryLater = () => { + const retry = (cooldownMs?: number) => { const node = registry.getNodes().get(refreshAtom); if (node === undefined || node.canBeRemoved) return; registry.set(refreshAtom, (state) => // @effect-diagnostics-next-line globalDate:off - failProviderWorkspaceSnapshotRefresh(state, now, Date.now()), + failProviderWorkspaceSnapshotRefresh(state, now, Date.now(), cooldownMs), ); }; + const retryLater = () => retry(); void runAtomCommand( registry, input.refreshProviders, @@ -361,7 +363,11 @@ export function createProviderWorkspaceSnapshotRefreshAtomFamily(input: { (hasCurrentProviderWorkspaceSnapshot(refreshedProvider, cwd, completedAt) || snapshot?.checkedAt !== (provider && resolveProviderWorkspaceSnapshot(provider, cwd))?.checkedAt); - if (!refreshed) retryLater(); + if (!refreshed) return retryLater(); + // A reply is dropped when the provider changed while it was in flight, such as a + // SessionStart rescan; ask again at once for the snapshot that change produced. + const held = findProvider(registry.get(input.providers(environmentId))); + if (!hasCompleteProviderWorkspaceSnapshot(held, cwd)) retry(0); }, retryLater); return plan.state; }, diff --git a/packages/client-runtime/src/state/server.test.ts b/packages/client-runtime/src/state/server.test.ts index daec4ac7087ef..e6ce1725cc15f 100644 --- a/packages/client-runtime/src/state/server.test.ts +++ b/packages/client-runtime/src/state/server.test.ts @@ -1286,6 +1286,16 @@ describe("server state projection", () => { ).providers[0]?.workspaceSnapshots, ).toBeUndefined(); + // A server rescan that changed the skills retires the snapshot fetched before it. + expect( + withFetchedWorkspaceSnapshots( + configWith(true, [ + { ...provider(claude), workspaceRescannedAt: "2026-10-25T00:09:00.000Z" }, + ] as unknown as ServerConfig["providers"]), + fetched, + ).providers[0]?.workspaceSnapshots, + ).toBeUndefined(); + // A catalog digest stands for the lists: loading them keeps the snapshot, a new digest retires it. const digested = (catalogDigest: string, slashCommands: ReadonlyArray<{ name: string }> = []) => ({ ...provider(claude, undefined, slashCommands), catalogDigest }) as ReturnType< diff --git a/packages/client-runtime/src/state/server.ts b/packages/client-runtime/src/state/server.ts index a8c22d24d755e..cbc4de0beac72 100644 --- a/packages/client-runtime/src/state/server.ts +++ b/packages/client-runtime/src/state/server.ts @@ -747,8 +747,9 @@ export function resolveServerConfigValue( } /** - * The provider-wide fields a workspace snapshot builds on. A live session, a sign-out, or a - * /usage-limits change shows up here, so a snapshot fetched before it no longer applies. + * The provider-wide fields a workspace snapshot builds on. A live session, a sign-out, a + * /usage-limits change, or a server rescan that changed the skills shows up here, so a snapshot + * fetched before it no longer applies. */ const snapshotBasis = (provider: ServerProviderEntry | undefined) => provider && { @@ -758,6 +759,9 @@ const snapshotBasis = (provider: ServerProviderEntry | undefined) => ...(provider.catalogDigest === undefined ? { slashCommands: provider.slashCommands, skills: provider.skills } : { catalogDigest: provider.catalogDigest }), + ...(provider.workspaceRescannedAt === undefined + ? {} + : { workspaceRescannedAt: provider.workspaceRescannedAt }), }; interface FetchedWorkspaceSnapshot { @@ -1582,6 +1586,11 @@ export function createServerEnvironmentAtoms( refreshProviders: createEnvironmentRpcCommand(runtime, { label: "environment-data:server:refresh-providers", tag: WS_METHODS.serverRefreshProviders, + // A rescan during the request must leave the stored basis behind the new stamp. + onStart: ({ environmentId, input }, registry) => + registry + .get(configValueAtom(environmentId)) + ?.providers.find((provider) => provider.instanceId === input.instanceId), execute: (input, registry, environmentId) => { const { instanceId, cwd } = input; const provider = @@ -1603,7 +1612,7 @@ export function createServerEnvironmentAtoms( : {}), }); }, - onSuccess: ({ environmentId, input }, registry, result) => + onSuccess: ({ environmentId, input }, registry, result, startedProvider) => Effect.sync(() => { const { instanceId, cwd } = input; if (instanceId === undefined || cwd === undefined) return; @@ -1625,9 +1634,7 @@ export function createServerEnvironmentAtoms( current, { instanceId, cwd, fresh: input.fresh === true }, result.providers, - registry - .get(configValueAtom(environmentId)) - ?.providers.find((provider) => provider.instanceId === instanceId), + startedProvider, ), ); }), diff --git a/packages/contracts/src/server.ts b/packages/contracts/src/server.ts index b46a2823e9153..144d2814b8804 100644 --- a/packages/contracts/src/server.ts +++ b/packages/contracts/src/server.ts @@ -276,6 +276,11 @@ export const ServerProvider = Schema.Struct({ ), skills: Schema.Array(ServerProviderSkill).pipe(Schema.withDecodingDefault(Effect.succeed([]))), workspaceSnapshots: Schema.optionalKey(Schema.Array(ServerProviderWorkspaceSnapshot)), + /** + * Set when the server rescanned a workspace after a session start and its skills or commands + * changed. Clients holding fetched workspace snapshots for this provider fetch them again. + */ + workspaceRescannedAt: Schema.optionalKey(IsoDateTime), usageLimits: Schema.optional(ServerProviderUsageLimits), versionAdvisory: Schema.optionalKey(ServerProviderVersionAdvisory), compatibilityAdvisory: Schema.optionalKey(ServerProviderCompatibilityAdvisory), diff --git a/packages/provider-core/src/server/driver.ts b/packages/provider-core/src/server/driver.ts index ac5ea46f39f16..c0474da3b55d9 100644 --- a/packages/provider-core/src/server/driver.ts +++ b/packages/provider-core/src/server/driver.ts @@ -14,6 +14,7 @@ import type { import type * as Effect from "effect/Effect"; import type * as Schema from "effect/Schema"; import type * as Scope from "effect/Scope"; +import type * as Stream from "effect/Stream"; import type * as ProviderAdapter from "./ProviderAdapter.ts"; import type { ProviderAuthController } from "./auth.ts"; @@ -48,6 +49,11 @@ export interface ProviderInstance { readonly snapshotForCwd?: ( cwd: string, ) => Effect.Effect; + /** + * Cwds whose workspace skills may have changed. The registry rescans only cwds already + * requested. The stream must end when the instance is torn down. + */ + readonly workspaceRescans?: Stream.Stream; readonly refreshModels?: () => Effect.Effect; readonly invalidateCaches?: Effect.Effect; /** Moves a long-lived service onto a newly updated binary before its version is read back. */ From 2becf31b640695216df4156f48239ddf35a7983c Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 15 Nov 2026 04:33:39 -0800 Subject: [PATCH 021/124] fix(server): Windows spawn workers no longer leak a short child's output pipe to a sibling (#37180) --- apps/server/src/process/SpawnWorker.test.ts | 32 ++++++++++++++++++ apps/server/src/process/SpawnWorker.ts | 36 ++++++++++++++++++--- 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/apps/server/src/process/SpawnWorker.test.ts b/apps/server/src/process/SpawnWorker.test.ts index 27d31aa69c179..9c88d4f586c4b 100644 --- a/apps/server/src/process/SpawnWorker.test.ts +++ b/apps/server/src/process/SpawnWorker.test.ts @@ -138,6 +138,38 @@ describe("SpawnWorker", () => { }).pipe(Effect.scoped, Effect.provide(layer)), ); + it.live("a short child's output ends while a sibling launched beside it still runs", () => + Effect.gen(function* () { + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; + const untilStdin = + "process.stdout.write('ready');process.stdin.once('data',()=>process.exit(0))"; + // Workers that own a child stay alive, as beside a running agent, so each round's two + // launches land on two warm workers at once. + yield* Effect.all( + Array.from({ length: 4 }, () => + Effect.gen(function* () { + const holder = yield* spawner.spawn(node(untilStdin)); + expect(yield* holder.stdout.pipe(Stream.take(1), text)).toBe("ready"); + }), + ), + { concurrency: "unbounded" }, + ); + for (let round = 0; round < 20; round++) { + const [sibling, output] = yield* Effect.all( + [ + spawner.spawn(node(untilStdin, [], { stdin: { stream: Stream.never } })), + spawner + .spawn(node("process.stdout.write('ok')")) + .pipe(Effect.flatMap((child) => text(child.stdout))), + ], + { concurrency: "unbounded" }, + ); + expect(output).toBe("ok"); + expect(yield* sibling.isRunning).toBe(true); + } + }).pipe(Effect.scoped, Effect.provide(layer)), + ); + it.live("kills running commands, detached or not, and on scope interruption", () => Effect.gen(function* () { const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; diff --git a/apps/server/src/process/SpawnWorker.ts b/apps/server/src/process/SpawnWorker.ts index a43868b6d7b41..5a391ab18ca1b 100644 --- a/apps/server/src/process/SpawnWorker.ts +++ b/apps/server/src/process/SpawnWorker.ts @@ -60,10 +60,27 @@ type FromWorker = } | { readonly type: "close"; readonly id: number }; +// Windows makes a child's stdio pipes inheritable for the length of its CreateProcess, so a child +// another worker launches meanwhile inherits them and holds the first child's output open. Every +// pool's workers take this lock around spawn(); it holds the owning worker's threadId. +const windowsSpawnLock = new Int32Array(new SharedArrayBuffer(4)); + // Inline source runs from bundles, asar archives and single-executable builds without module files. const workerSource = ` -const { parentPort } = require("node:worker_threads"); +const { parentPort, threadId, workerData } = require("node:worker_threads"); const { spawn } = require("node:child_process"); +const { lock } = workerData; +const locked = (launch) => { + if (lock === undefined) return launch(); + // Wait on the owner compareExchange saw: an unlock after it changes the value, so no wakeup is lost. + for (let owner; (owner = Atomics.compareExchange(lock, 0, 0, threadId)) !== 0; ) Atomics.wait(lock, 0, owner); + try { + return launch(); + } finally { + Atomics.store(lock, 0, 0); + Atomics.notify(lock, 0, 1); + } +}; const children = new Map(); const post = (message, transfer) => parentPort.postMessage(message, transfer); const serialize = (error) => { @@ -78,7 +95,7 @@ parentPort.on("message", (request) => { if (request.type === "spawn") { let child; try { - child = spawn(request.command, request.args, request.options); + child = locked(() => spawn(request.command, request.args, request.options)); } catch (error) { post({ type: "error", id, error: serialize(error) }); return; @@ -290,13 +307,19 @@ class SpawnWorker { readonly worker: NodeWorkerThreads.Worker; readonly children = new Map(); readonly pool: SpawnWorkerPool; + /** Node sets `worker.threadId` to -1 once the thread exits, so keep the id the lock holds. */ + readonly threadId: number; /** Launches sent but not yet answered; the worker is inside CreateProcess for one of them. */ launching = 0; closed = false; constructor(pool: SpawnWorkerPool) { this.pool = pool; - this.worker = new NodeWorkerThreads.Worker(workerSource, { eval: true }); + this.worker = new NodeWorkerThreads.Worker(workerSource, { + eval: true, + workerData: { lock: pool.platform === "win32" ? windowsSpawnLock : undefined }, + }); + this.threadId = this.worker.threadId; this.worker.on("message", (message: FromWorker) => this.onMessage(message)); this.worker.on("error", (error) => this.fail(error)); this.worker.on("exit", () => this.fail(new Error("The process launcher thread exited."))); @@ -426,7 +449,12 @@ class SpawnWorker { if (this.closed) return; this.closed = true; this.pool.workers.delete(this); - void this.worker.terminate(); + void this.worker.terminate().then(() => { + // The thread may have died holding the lock, or between its unlock and notify, or after + // a notify woke it: free the lock and wake every waiter so none sleeps on a free lock. + Atomics.compareExchange(windowsSpawnLock, 0, this.threadId, 0); + Atomics.notify(windowsSpawnLock, 0); + }); } } From be8c4c6d9fdb2f14066447b158f9706f5a76e483 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 15 Nov 2026 13:45:39 +0100 Subject: [PATCH 022/124] fix(web): repaint terminal when programs change default colors (#37193) Co-authored-by: Julius Marminge --- apps/web/src/terminal/ghostty/core.test.ts | 37 +++++++- apps/web/src/terminal/ghostty/core.ts | 9 +- apps/web/src/terminal/ghostty/surface.ts | 6 ++ ...-program-background-repaints-screen.e2e.ts | 91 +++++++++++++++++++ 4 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 scripts/scenarios/terminal-program-background-repaints-screen.e2e.ts diff --git a/apps/web/src/terminal/ghostty/core.test.ts b/apps/web/src/terminal/ghostty/core.test.ts index b17b86b96d3c8..7958475ea54ef 100644 --- a/apps/web/src/terminal/ghostty/core.test.ts +++ b/apps/web/src/terminal/ghostty/core.test.ts @@ -248,7 +248,7 @@ describe("GhosttyTerminalCore snapshots", () => { ]); }); - it("paints ANSI colors from the theme palette and repaints them on theme change", async () => { + it("paints ANSI colors from the theme palette and repaints them when the theme or a program changes colors", async () => { const core = await createCore(); const theme = { foreground: { r: 255, g: 255, b: 255 }, @@ -266,6 +266,41 @@ describe("GhosttyTerminalCore snapshots", () => { cells = core.snapshot().rowData[0]!.cells; expect(cells[0]!.foreground).toEqual({ r: 1, g: 9, b: 2 }); expect(cells[1]!.foreground).toEqual({ r: 255, g: 0, b: 0 }); + + // Inverse, default and explicit-background cells, cached before a program sets OSC 10/11. + core.resetAndWrite("\x1b[7mI\x1b[0mD\x1b[48;2;9;8;7mB\x1b[0m\r\n"); + core.snapshot(); + const colors = () => { + const { background, rowData } = core.snapshot(); + return { + background, + cells: rowData[0]!.cells + .slice(0, 3) + .map(({ foreground, background }) => ({ foreground, background })), + }; + }; + const programForeground = { r: 17, g: 34, b: 51 }; + const programBackground = { r: 68, g: 85, b: 102 }; + const explicitBackground = { r: 9, g: 8, b: 7 }; + core.write("\x1b]10;rgb:11/22/33\x07\x1b]11;rgb:44/55/66\x07"); + expect(colors()).toEqual({ + background: programBackground, + cells: [ + { foreground: programBackground, background: programForeground }, + { foreground: programForeground, background: programBackground }, + { foreground: programForeground, background: explicitBackground }, + ], + }); + + core.write("\x1b]110\x07\x1b]111\x07"); + expect(colors()).toEqual({ + background: theme.background, + cells: [ + { foreground: theme.background, background: theme.foreground }, + { foreground: theme.foreground, background: theme.background }, + { foreground: theme.foreground, background: explicitBackground }, + ], + }); }); it("preserves Unicode and split ANSI parser state across batched renderer reads", async () => { diff --git a/apps/web/src/terminal/ghostty/core.ts b/apps/web/src/terminal/ghostty/core.ts index dae62e989d3ff..119156cc56554 100644 --- a/apps/web/src/terminal/ghostty/core.ts +++ b/apps/web/src/terminal/ghostty/core.ts @@ -248,6 +248,8 @@ export class GhosttyTerminalCore { private cellQuery = 0; private scrollbar = 0; private rows: GhosttyRow[] = []; + // Defaults the cached rows resolved; Ghostty marks no row dirty when OSC 10/11 change them. + private rowColors: Pick | null = null; private disposed = false; private keyboardLayoutMap: GhosttyKeyboardLayoutMap | undefined; @@ -865,9 +867,14 @@ export class GhosttyTerminalCore { ); const cols = this.getU16(RENDER_DATA.cols); const rowCount = this.getU16(RENDER_DATA.rows); - const dirty = this.getU32(RENDER_DATA.dirty); const foreground = this.getColor(RENDER_DATA.foreground, { r: 229, g: 231, b: 235 }); const background = this.getColor(RENDER_DATA.background, { r: 0, g: 0, b: 0 }); + const colorsChanged = + this.rowColors !== null && + (!sameColor(this.rowColors.foreground, foreground) || + !sameColor(this.rowColors.background, background)); + this.rowColors = { foreground, background }; + const dirty = colorsChanged ? 2 : this.getU32(RENDER_DATA.dirty); const cursorHasValue = this.getBool(RENDER_DATA.cursorHasValue); const cursor = cursorHasValue ? this.getColor(RENDER_DATA.cursor, foreground) : foreground; const cursorInViewport = this.getBool(RENDER_DATA.cursorInViewport); diff --git a/apps/web/src/terminal/ghostty/surface.ts b/apps/web/src/terminal/ghostty/surface.ts index 537f7bb524e59..6e86cf9ee3ecc 100644 --- a/apps/web/src/terminal/ghostty/surface.ts +++ b/apps/web/src/terminal/ghostty/surface.ts @@ -5,6 +5,7 @@ import { collectWrappedTerminalLinkLine, extractTerminalLinks } from "../../term import { GHOSTTY_CELL_WIDE, GhosttyTerminalCore, + ghosttyColorsEqual, type GhosttyCell, type GhosttyScrollbar, type GhosttySnapshot, @@ -2043,7 +2044,12 @@ export class GhosttyTerminalSurface { this.canvasConfigured = true; this.canvasDevicePixelRatio = canvasConfiguration.ratio; } + const previousBackground = this.snapshot?.background; this.snapshot = this.core.snapshot(); + // Only a full render repaints the padding around the rows in the new default background. + if (previousBackground && !ghosttyColorsEqual(previousBackground, this.snapshot.background)) { + this.forceFullRender = true; + } if (!this.blinkEnabled()) this.cursorOn = true; // Move the origin only on full repaint; dirty-row updates cannot mix origins. const scrollState = this.readScrollbarState(); diff --git a/scripts/scenarios/terminal-program-background-repaints-screen.e2e.ts b/scripts/scenarios/terminal-program-background-repaints-screen.e2e.ts new file mode 100644 index 0000000000000..b847073711ee5 --- /dev/null +++ b/scripts/scenarios/terminal-program-background-repaints-screen.e2e.ts @@ -0,0 +1,91 @@ +/// +// The DOM lib types the function this scenario runs in the page. +import { expect } from "e2e"; +import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; + +import { runPromise, test, WEB } from "./t3.ts"; + +// A raw-mode process prints rows, then on `b` sets the default background with OSC 11. +const PROGRAM = String.raw`import { writeFileSync } from "node:fs"; + +const state = new URL("state", import.meta.url); +process.stdin.setRawMode(true); +process.stdout.write("\x1b[2J\x1b[HROW ONE\r\nROW TWO\r\nROW THREE\r\nROW FOUR\r\n"); +writeFileSync(state, "ready"); +process.stdin.on("data", (chunk) => { + const key = chunk.toString(); + if (key === "b") { + process.stdout.write("\x1b]11;rgb:44/55/66\x07", () => writeFileSync(state, "changed")); + } + if (key === "x") { + process.stdin.setRawMode(false); + process.exit(0); + } +}); +`; + +test( + "A program setting the terminal's default background repaints the whole screen", + WEB, + async ({ t3, app, browser, screen }) => { + await browser.emulateMedia({ colorScheme: "dark" }); + const project = await t3.project("colors", { "program.mjs": PROGRAM }); + const thread = await t3.thread({ project, title: "Colors", messages: ["hello"] }); + const read = (file: string) => + runPromise( + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + return yield* fs.readFileString(`${project}/${file}`); + }).pipe(Effect.orElseSucceed(() => "")), + ); + await t3.open(thread); + await screen.getByRole("button", "Toggle terminal drawer").tap(); + const terminal = screen.getByLabel("Terminal input"); + + // Input sent before the shell runs is dropped, so retype until it lands. + await expect + .poll( + async () => { + await terminal.focus(); + await browser.keyboard.type(`echo ready > '${project}/shell-ready'`); + await browser.keyboard.press("Enter"); + return read("shell-ready"); + }, + { timeout: 30_000 }, + ) + .toBe("ready\n"); + + await terminal.focus(); + await browser.keyboard.type(`'${process.execPath}' '${project}/program.mjs'`); + await browser.keyboard.press("Enter"); + await expect.poll(() => read("state"), { timeout: 15_000 }).toBe("ready"); + await app.screenshot("before-background"); + + await terminal.focus(); + await browser.keyboard.type("b"); + await expect.poll(() => read("state"), { timeout: 15_000 }).toBe("changed"); + await app.screenshot("after-background"); + + // Sample the padding above the rows, a cell beside the printed rows, and the canvas bottom. + const pixels = await browser.evaluate(() => { + const canvas = document + .querySelector('[aria-label="Terminal input"]')! + .parentElement!.querySelector("canvas")!; + const context = canvas.getContext("2d")!; + const at = (x: number, y: number) => [...context.getImageData(x, y, 1, 1).data]; + return { + padding: at(canvas.width - 2, 1), + row: at(canvas.width - 20, Math.floor(canvas.height / 2)), + bottom: at(canvas.width - 2, canvas.height - 1), + }; + }); + await browser.keyboard.type("x"); + const programBackground = [68, 85, 102, 255]; + expect(pixels).toEqual({ + padding: programBackground, + row: programBackground, + bottom: programBackground, + }); + }, +); From cb251b9abc1403aa02e21e400d0b7f396a195e3c Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 15 Nov 2026 13:47:53 +0100 Subject: [PATCH 023/124] fix(web): composer suggestions stay reachable in short windows (#37179) Co-authored-by: Grok 4.7 Co-authored-by: Julius Marminge --- apps/web/src/components/chat/ChatComposer.tsx | 2 +- .../chat/ComposerCommandMenuLayer.tsx | 17 ++- apps/web/src/components/ui/autocomplete.tsx | 3 +- scripts/scenarios/composer-menu.ts | 105 ++++++++++++++++++ ...ow-file-suggestions-reach-last-file.e2e.ts | 29 +++++ ...w-file-suggestions-reach-first-file.e2e.ts | 32 ++++++ 6 files changed, 181 insertions(+), 7 deletions(-) create mode 100644 scripts/scenarios/composer-menu.ts create mode 100644 scripts/scenarios/short-window-file-suggestions-reach-last-file.e2e.ts create mode 100644 scripts/scenarios/shrunk-window-file-suggestions-reach-first-file.e2e.ts diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index dd0fe8461ff28..2499ddf7c2279 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -3517,7 +3517,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) )} > {isStashMenuOpen && !composerMenuOpen && ( - + (null); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- positions the menu from the composer's DOM rect, tracking window resize, scroll and ancestor resizes useLayoutEffect(() => { - const anchor = props.anchor; if (!anchor) return; const updatePosition = () => { @@ -47,10 +49,15 @@ export function ComposerCommandMenuLayer(props: { return; } const composerOverlap = rootFontSizePx + 1; + const spaceAbove = rect.top + composerOverlap; + const bottom = window.innerHeight - spaceAbove; + const floorHeight = Math.max(96, spaceAbove - 24); + const maxHeight = canOverflowTop ? floorHeight : Math.min(floorHeight, window.innerHeight); const next = { - bottom: window.innerHeight - rect.top - composerOverlap, + // With too little room above, the menu slides down over the composer instead of past the window top. + bottom: canOverflowTop ? bottom : Math.min(bottom, window.innerHeight - maxHeight), left: rect.left + drawerInset, - maxHeight: Math.max(96, rect.top - 24 + composerOverlap), + maxHeight, width, }; setPosition((current) => @@ -73,9 +80,9 @@ export function ComposerCommandMenuLayer(props: { window.removeEventListener("resize", updatePosition); window.removeEventListener("scroll", updatePosition, true); }; - }, [props.anchor]); + }, [anchor, canOverflowTop]); - if (!props.anchor || !position) return null; + if (!anchor || !position) return null; return createPortal(
+ // A flex column lets the viewport shrink with a height-capped parent instead of overflowing it. + +// Helpers for composer suggestion scenarios in short windows; the DOM lib types their page code. +import type { Browser } from "@e2e-dev/web"; +import { expect, type Screen } from "e2e"; + +import type { T3 } from "./t3.ts"; + +const FILE_NAMES = Array.from( + { length: 40 }, + (_, index) => `entry-${String(index + 1).padStart(2, "0")}.ts`, +); + +/** Seeds a project with enough files to overflow the menu and opens a thread in it. */ +export async function openThreadWithManyFiles(t3: T3) { + const files = Object.fromEntries(FILE_NAMES.map((name) => [`src/${name}`, `export {};\n`])); + const project = await t3.project("demo", files); + const thread = await t3.thread({ project, title: "Short window", messages: ["hello"] }); + await t3.open(thread); +} + +/** Types a fourteen-line draft, then opens the `@` file suggestions at its end. */ +export async function openFileSuggestionsUnderTallDraft(browser: Browser, screen: Screen) { + // A phone-width window collapses the composer until it is tapped. + await screen.getByRole("button", "Expand composer").tap(); + await screen.getByRole("textbox").tap(); + for (let line = 1; line <= 14; line += 1) { + await browser.keyboard.type(`line ${line}`); + if (line < 14) await browser.keyboard.press("Shift+Enter"); + } + await browser.keyboard.press("Control+End"); + await browser.keyboard.type(" @"); + await expect + .poll(() => + browser.evaluate( + () => document.querySelectorAll('[data-composer-command-drawer] [role="option"]').length, + ), + ) + .toBeGreaterThan(FILE_NAMES.length); +} + +/** Wheels the suggestions from a point inside the visible drawer until they reach that end. */ +export async function wheelSuggestions(browser: Browser, direction: "up" | "down") { + const point = await browser.evaluate(() => { + const layer = document.querySelector('[data-composer-drawer-layer="true"]'); + if (!layer) return null; + const rect = layer.getBoundingClientRect(); + const top = Math.max(rect.top, 0); + const bottom = Math.min(rect.bottom, window.innerHeight); + return { x: rect.left + rect.width / 2, y: top + (bottom - top) / 3 }; + }); + expect(point).not.toBeNull(); + await browser.mouse.move(point!.x, point!.y); + // Wheel scrolling lands a frame or more later, so keep wheeling until the list is at its end. + await expect + .poll(async () => { + await browser.mouse.wheel(0, direction === "down" ? 4000 : -4000); + return browser.evaluate((direction) => { + const viewport = document.querySelector( + '[data-composer-command-drawer] [data-slot="scroll-area-viewport"]', + ); + if (!viewport) return false; + return direction === "down" + ? viewport.scrollTop >= viewport.scrollHeight - viewport.clientHeight - 1 + : viewport.scrollTop <= 0; + }, direction); + }) + .toBe(true); +} + +/** + * The first suggestion or last file row, and its center when the whole row is visible + * and a click there lands on it, otherwise `null`. + */ +export const findHittableRow = (browser: Browser, which: "first" | "last-file") => + browser.evaluate( + ({ which, names }) => { + const options = [ + ...document.querySelectorAll('[data-composer-command-drawer] [role="option"]'), + ]; + const row = + which === "first" + ? options[0] + : options.findLast((option) => + names.some((name) => option.textContent?.startsWith(name)), + ); + if (!row) return { label: null, point: null }; + const text = row.textContent ?? ""; + const label = names.find((name) => text.startsWith(name)) ?? text; + const rect = row.getBoundingClientRect(); + const viewport = row.closest('[data-slot="scroll-area-viewport"]')?.getBoundingClientRect(); + const top = Math.max(0, viewport?.top ?? 0); + const bottom = Math.min(window.innerHeight, viewport?.bottom ?? window.innerHeight); + if (rect.top < top - 1 || rect.bottom > bottom + 1) return { label, point: null }; + const point = { x: rect.left + rect.width / 2, y: rect.top + rect.height / 2 }; + const hit = document.elementFromPoint(point.x, point.y)?.closest('[role="option"]') === row; + return { label, point: hit ? point : null }; + }, + { which, names: [...FILE_NAMES, "README.md"] }, + ); + +export async function clickAt(browser: Browser, point: { readonly x: number; readonly y: number }) { + await browser.mouse.move(point.x, point.y); + await browser.mouse.down(); + await browser.mouse.up(); +} diff --git a/scripts/scenarios/short-window-file-suggestions-reach-last-file.e2e.ts b/scripts/scenarios/short-window-file-suggestions-reach-last-file.e2e.ts new file mode 100644 index 0000000000000..01b5ed306c79b --- /dev/null +++ b/scripts/scenarios/short-window-file-suggestions-reach-last-file.e2e.ts @@ -0,0 +1,29 @@ +import { expect } from "e2e"; + +import { + clickAt, + findHittableRow, + openFileSuggestionsUnderTallDraft, + openThreadWithManyFiles, + wheelSuggestions, +} from "./composer-menu.ts"; +import { test, WEB } from "./t3.ts"; + +test( + "In a short window, file suggestions wheel down to a last file the pointer can pick", + WEB, + async ({ t3, app, browser, screen }) => { + await browser.setViewport({ width: 375, height: 500 }); + await openThreadWithManyFiles(t3); + await openFileSuggestionsUnderTallDraft(browser, screen); + + await wheelSuggestions(browser, "down"); + await app.screenshot("scrolled-to-bottom"); + const last = await findHittableRow(browser, "last-file"); + expect(last.label).toBe("README.md"); + expect(last.point).not.toBeNull(); + + await clickAt(browser, last.point!); + await expect(screen.getByRole("textbox")).toContainText("README.md"); + }, +); diff --git a/scripts/scenarios/shrunk-window-file-suggestions-reach-first-file.e2e.ts b/scripts/scenarios/shrunk-window-file-suggestions-reach-first-file.e2e.ts new file mode 100644 index 0000000000000..cf3087b2e122e --- /dev/null +++ b/scripts/scenarios/shrunk-window-file-suggestions-reach-first-file.e2e.ts @@ -0,0 +1,32 @@ +import { expect } from "e2e"; + +import { + clickAt, + findHittableRow, + openFileSuggestionsUnderTallDraft, + openThreadWithManyFiles, + wheelSuggestions, +} from "./composer-menu.ts"; +import { test, WEB } from "./t3.ts"; + +test( + "After the window shrinks, file suggestions wheel up to a first suggestion the pointer can pick", + WEB, + async ({ t3, app, browser, screen }) => { + await browser.setViewport({ width: 375, height: 500 }); + await openThreadWithManyFiles(t3); + await openFileSuggestionsUnderTallDraft(browser, screen); + await browser.keyboard.press("Escape"); + await browser.keyboard.press("Backspace"); + await browser.keyboard.type("@"); + + await browser.setViewport({ width: 375, height: 300 }); + await wheelSuggestions(browser, "up"); + await app.screenshot("scrolled-to-top"); + const first = await findHittableRow(browser, "first"); + expect(first.point).not.toBeNull(); + + await clickAt(browser, first.point!); + await expect(screen.getByRole("textbox")).toContainText(first.label!); + }, +); From 6071022cb71b92c761bc06aabff6865243fa956c Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 15 Nov 2026 04:52:35 -0800 Subject: [PATCH 024/124] fix(server): Claude session name follows thread renames without waiting for the next turn (#37185) --- .../Adapters/ClaudeAdapterV2.ts | 55 ++++--- ...laudeAutomaticDelivery.integration.test.ts | 147 ++++++++++++++++++ .../src/orchestration-v2/EffectOutbox.ts | 5 + .../src/orchestration-v2/EffectWorker.test.ts | 1 + .../src/orchestration-v2/EffectWorker.ts | 13 ++ .../src/orchestration-v2/Orchestrator.ts | 17 ++ .../ProviderRuntimeRecoveryService.test.ts | 1 + .../ProviderTurnControlService.ts | 22 +++ .../src/server/ProviderAdapter.ts | 5 + 9 files changed, 248 insertions(+), 18 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index e8cfd9d8ce08d..bced035fed011 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -6390,6 +6390,28 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( ); }); + // `claude --resume` shows the native title. A failed or timed-out rename forgets the + // applied title so the next turn, even after a restart, sends it again. + const applySessionTitle = Effect.fn("ClaudeAdapterV2.applySessionTitle")(function* ( + query: ClaudeAgentSdkQuerySession, + nativeThreadId: string, + title: string, + ) { + if (appliedSessionTitles.get(nativeThreadId) === title) return; + const renamed = yield* query + .renameSession(title) + .pipe(Effect.timeout("10 seconds"), Effect.isSuccess); + if (renamed) { + appliedSessionTitles.set(nativeThreadId, title); + } else { + appliedSessionTitles.delete(nativeThreadId); + } + const remembered = (yield* Ref.get(lastProviderThreadByNativeThread)).get(nativeThreadId); + if (remembered !== undefined) { + yield* rememberProviderThread(remembered); + } + }); + const rosterForNativeThread = ( all: ReadonlyMap>, nativeThreadId: string, @@ -13029,24 +13051,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( Effect.provideService(Path.Path, path), ); const querySession = yield* openQuery(turnInput, nativeThreadId, queryPolicy); - // `claude --resume` shows the native title. A failed or timed-out rename forgets the - // applied title so the next turn, even after a restart, sends it again. - if (appliedSessionTitles.get(nativeThreadId) !== turnInput.appThread.title) { - const renamed = yield* querySession.query - .renameSession(turnInput.appThread.title) - .pipe(Effect.timeout("10 seconds"), Effect.isSuccess); - if (renamed) { - appliedSessionTitles.set(nativeThreadId, turnInput.appThread.title); - } else { - appliedSessionTitles.delete(nativeThreadId); - } - const remembered = (yield* Ref.get(lastProviderThreadByNativeThread)).get( - nativeThreadId, - ); - if (remembered !== undefined) { - yield* rememberProviderThread(remembered); - } - } + yield* applySessionTitle(querySession.query, nativeThreadId, turnInput.appThread.title); const autoCompactWindow = yield* resolveAutoCompactWindow( querySession.settingsCwd, turnInput.modelSelection, @@ -14029,6 +14034,20 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( ), ); }), + applyThreadTitle: Effect.fn("ClaudeAdapterV2.applyThreadTitle")(function* ({ + providerThread, + title, + }) { + const live = yield* Ref.get(queryContext); + if ( + live === null || + live.stopping || + live.nativeThreadId !== providerThread.nativeThreadRef?.nativeId + ) { + return; + } + yield* applySessionTitle(live.query, live.nativeThreadId, title); + }), stopTask: Effect.fn("ClaudeAdapterV2.stopTask")(function* ({ providerThread, taskId }) { const liveQuery = yield* Ref.get(queryContext); const task = (yield* Ref.get(sessionSubagentsByTaskId)).get(taskId); diff --git a/apps/server/src/orchestration-v2/ClaudeAutomaticDelivery.integration.test.ts b/apps/server/src/orchestration-v2/ClaudeAutomaticDelivery.integration.test.ts index 0fead24d97792..1fa7047aff428 100644 --- a/apps/server/src/orchestration-v2/ClaudeAutomaticDelivery.integration.test.ts +++ b/apps/server/src/orchestration-v2/ClaudeAutomaticDelivery.integration.test.ts @@ -607,6 +607,153 @@ it.effect("applies an effort change to the running Claude query without waiting ), ); +const renamedClaudeSessionTitles = (options: { readonly renameDuringStartup: boolean }) => + Effect.gen(function* () { + const cwd = yield* checkpointWorkspace("claude-live-rename"); + const sdkMessages = yield* Queue.unbounded(); + const renames: string[] = []; + const startupEntered = yield* Deferred.make(); + const startupReleased = yield* Deferred.make(); + const adapter = yield* ClaudeAdapterV2.makeClaudeAdapterV2({ + instanceId: modelSelection.instanceId, + settings, + environment: {}, + attachmentsDir: cwd, + fileSystem: yield* FileSystem.FileSystem, + path: yield* Path.Path, + random: yield* SecureRandom.SecureRandom, + idAllocator: yield* IdAllocator.IdAllocatorV2, + queryRunner: { + allocateSessionId: Effect.succeed(sessionId), + open: () => + Effect.succeed({ + messages: Stream.fromQueue(sdkMessages), + offer: () => Effect.void, + setModel: () => Effect.void, + applyFlagSettings: () => Effect.void, + setPermissionMode: () => Effect.void, + renameSession: (title) => + Effect.sync(() => { + renames.push(title); + }), + backgroundTasks: () => Effect.succeed(false), + mcpServerStatus: Effect.succeed([]), + readMcpResource: () => Effect.die("unexpected MCP resource read"), + callMcpTool: () => Effect.die("unexpected MCP tool call"), + listPermissionRules: Effect.die("unexpected permission rules read"), + stopTask: () => Effect.void, + interrupt: Effect.die("a rename must not interrupt the turn"), + close: Effect.void, + }), + forkSession: () => Effect.die("unused"), + subagentLaunchToolUseId: () => Effect.succeed(null), + subagentModel: () => Effect.succeed(null), + assertComplete: Effect.void, + }, + }); + return yield* Effect.gen(function* () { + const orchestrator = yield* Orchestrator.OrchestratorV2; + const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; + const threadId = ThreadId.make("thread:live-rename"); + const running = yield* orchestrator.streamDomainEvents.pipe( + Stream.filter( + (event) => event.type === "provider-turn.updated" && event.payload.status === "running", + ), + Stream.take(1), + Stream.runDrain, + Effect.forkScoped, + ); + yield* orchestrator.dispatch({ + type: "thread.create", + commandId: CommandId.make("create"), + threadId, + projectId: ProjectId.make("project:live-rename"), + title: "Seed title", + modelSelection, + runtimeMode: "full-access", + interactionMode: "default", + branch: null, + worktreePath: cwd, + createdBy: "user", + creationSource: "web", + }); + yield* orchestrator.dispatch({ + type: "message.dispatch", + commandId: CommandId.make("first"), + threadId, + messageId: MessageId.make("first"), + text: "Keep working.", + attachments: [], + dispatchMode: { type: "start_immediately" }, + createdBy: "user", + creationSource: "web", + }); + const rename = orchestrator.dispatch({ + type: "thread.metadata.update", + commandId: CommandId.make("rename"), + threadId, + title: "Renamed title", + }); + if (options.renameDuringStartup) { + const starting = yield* worker.drain().pipe(Effect.forkScoped); + yield* Deferred.await(startupEntered); + yield* rename; + yield* Deferred.succeed(startupReleased, undefined); + yield* Fiber.join(starting); + yield* Fiber.join(running); + } else { + yield* worker.drain(); + yield* Fiber.join(running); + assert.deepEqual(renames, []); + yield* rename; + } + yield* worker.drain(); + return renames; + }).pipe( + Effect.provide( + ProviderReplayHarness.layerWithRegistry( + { name: "claude-live-rename" }, + ProviderAdapterRegistry.layerSingle({ + ...adapter, + openSession: (input) => + options.renameDuringStartup + ? Deferred.succeed(startupEntered, undefined).pipe( + Effect.andThen(Deferred.await(startupReleased)), + Effect.andThen(adapter.openSession(input)), + ) + : adapter.openSession(input), + }), + { runEffectWorker: false }, + ), + ), + ); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ); + +it.effect("renames the running Claude session when the thread is renamed mid-turn", () => + Effect.gen(function* () { + assert.deepEqual(yield* renamedClaudeSessionTitles({ renameDuringStartup: false }), [ + "Renamed title", + ]); + }), +); + +it.effect("renames the Claude session when the thread is renamed while it starts", () => + Effect.gen(function* () { + assert.deepEqual(yield* renamedClaudeSessionTitles({ renameDuringStartup: true }), [ + "Renamed title", + ]); + }), +); + const backgroundTaskStarted = (taskId: string) => frame({ type: "system", diff --git a/apps/server/src/orchestration-v2/EffectOutbox.ts b/apps/server/src/orchestration-v2/EffectOutbox.ts index 66ae3d65d2485..69fd60684bbcb 100644 --- a/apps/server/src/orchestration-v2/EffectOutbox.ts +++ b/apps/server/src/orchestration-v2/EffectOutbox.ts @@ -50,6 +50,10 @@ export const OrchestrationEffectRequestV2 = Schema.Union([ providerThreadId: ProviderThreadId, modelSelection: ModelSelection, }), + Schema.Struct({ + type: Schema.Literal("provider-session.apply-thread-title"), + title: Schema.String, + }), Schema.Struct({ type: Schema.Literal("provider-turn.start"), runId: RunId, @@ -150,6 +154,7 @@ export const REPLAY_SAFE_EFFECT_TYPES_AFTER_PROCESS_LOSS = [ export const PROCESS_BOUND_EFFECT_TYPES = [ "provider-session.apply-model-selection", + "provider-session.apply-thread-title", "provider-turn.start", "provider-turn.interrupt", "provider-turn.steer", diff --git a/apps/server/src/orchestration-v2/EffectWorker.test.ts b/apps/server/src/orchestration-v2/EffectWorker.test.ts index 94444eb8e5c69..428d7d47066aa 100644 --- a/apps/server/src/orchestration-v2/EffectWorker.test.ts +++ b/apps/server/src/orchestration-v2/EffectWorker.test.ts @@ -102,6 +102,7 @@ function layerExecutorFor(input: { interrupt: input.interrupt ?? (() => Effect.void), steer: () => Effect.void, applyModelSelection: () => Effect.void, + applyThreadTitle: () => Effect.void, interruptAndAwaitTerminal: (request) => record( request.replacementProviderSessionId === undefined diff --git a/apps/server/src/orchestration-v2/EffectWorker.ts b/apps/server/src/orchestration-v2/EffectWorker.ts index 41b2d234f26b3..c6aa09efd5e69 100644 --- a/apps/server/src/orchestration-v2/EffectWorker.ts +++ b/apps/server/src/orchestration-v2/EffectWorker.ts @@ -222,6 +222,19 @@ export const layerExecutor: Layer.Layer< }), ), ); + case "provider-session.apply-thread-title": + return providerTurnControl + .applyThreadTitle({ threadId: effect.threadId, title: effect.request.title }) + .pipe( + Effect.mapError( + (cause) => + new OrchestrationEffectExecutionError({ + effectId: effect.id, + effectType: effect.request.type, + cause, + }), + ), + ); case "provider-turn.start": return startRun(effect, effect.request.runId, willRetry).pipe( Effect.mapError( diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index 53f726bf4628d..5257330e2cb5d 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -5312,6 +5312,23 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio } } + // Queued even while no session is live: it runs after any in-flight turn start publishes one. + if ( + (command.type === "thread.metadata.update" || + command.type === "thread.title.regeneration.complete") && + updatedThread.title !== thread.title + ) { + yield* Ref.update(effects, (existing) => [ + ...existing, + { + id: `effect:${command.commandId}:provider-session.apply-thread-title`, + commandId: command.commandId, + threadId: command.threadId, + request: { type: "provider-session.apply-thread-title", title: updatedThread.title }, + } satisfies PendingOrchestrationEffectV2, + ]); + } + if (command.type === "thread.metadata.update" && command.regenerateTitle === true) { yield* Ref.update(effects, (existing) => [ ...existing, diff --git a/apps/server/src/orchestration-v2/ProviderRuntimeRecoveryService.test.ts b/apps/server/src/orchestration-v2/ProviderRuntimeRecoveryService.test.ts index f08a3a038be6f..0a1e71f78d098 100644 --- a/apps/server/src/orchestration-v2/ProviderRuntimeRecoveryService.test.ts +++ b/apps/server/src/orchestration-v2/ProviderRuntimeRecoveryService.test.ts @@ -901,6 +901,7 @@ it.effect( assert.equal(summary.retiredEffects, 2); assert.deepEqual(committedInput?.cancelUnsettledEffects?.effectTypes, [ "provider-session.apply-model-selection", + "provider-session.apply-thread-title", "provider-turn.start", "provider-turn.interrupt", "provider-turn.steer", diff --git a/apps/server/src/orchestration-v2/ProviderTurnControlService.ts b/apps/server/src/orchestration-v2/ProviderTurnControlService.ts index 5de7f965474bd..f9727734bf870 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnControlService.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnControlService.ts @@ -84,6 +84,16 @@ export interface ProviderTurnControlServiceV2Shape { | ProviderSessionManager.ProviderSessionManagerV2Error | ProviderAdapter.ProviderAdapterV2Error >; + /** Best effort: a provider thread that is no longer live picks the title up when its next turn starts. */ + readonly applyThreadTitle: (input: { + readonly threadId: ThreadId; + readonly title: string; + }) => Effect.Effect< + void, + | ProjectionStore.ProjectionStoreV2Error + | ProviderSessionManager.ProviderSessionManagerV2Error + | ProviderAdapter.ProviderAdapterV2Error + >; readonly interruptAndAwaitTerminal: (input: { readonly threadId: ThreadId; readonly providerSessionId: ProviderSessionId; @@ -249,6 +259,18 @@ export const layer: Layer.Layer< }); }, ), + applyThreadTitle: Effect.fn("ProviderTurnControlService.applyThreadTitle")(function* (input) { + const { thread, providerThreads } = yield* projections.getThreadRecords(input.threadId, [ + "providerThreads", + ]); + const providerThread = providerThreads.find( + (candidate) => candidate.id === thread.activeProviderThreadId, + ); + if (!providerThread?.providerSessionId) return; + const session = yield* sessions.get(providerThread.providerSessionId); + if (Option.isNone(session) || session.value.applyThreadTitle === undefined) return; + yield* session.value.applyThreadTitle({ providerThread, title: input.title }); + }), interrupt: Effect.fn("ProviderTurnControlService.interrupt")( function* (input) { const loaded = yield* load({ ...input, operation: "interrupt" }); diff --git a/packages/provider-core/src/server/ProviderAdapter.ts b/packages/provider-core/src/server/ProviderAdapter.ts index 58f1773974069..80d3ef8a5285a 100644 --- a/packages/provider-core/src/server/ProviderAdapter.ts +++ b/packages/provider-core/src/server/ProviderAdapter.ts @@ -750,6 +750,11 @@ export interface ProviderAdapterV2SessionRuntime { readonly providerThread: OrchestrationV2ProviderThread; readonly modelSelection: ModelSelection; }) => Effect.Effect; + /** Renames the provider's native session while its process is running; otherwise the next turn does. */ + readonly applyThreadTitle?: (input: { + readonly providerThread: OrchestrationV2ProviderThread; + readonly title: string; + }) => Effect.Effect; readonly stopTask?: (input: { readonly providerThread: OrchestrationV2ProviderThread; readonly taskId: string; From 6d3f6423a341b315a091567aee5ab34a3b8b0373 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 15 Nov 2026 13:52:47 +0100 Subject: [PATCH 025/124] fix(web): Enter sends when a panel-conditioned shortcut is inactive (#37198) Co-authored-by: Grok 4.7 Co-authored-by: Julius Marminge --- apps/web/src/components/ChatView.tsx | 1 + apps/web/src/components/chat/ChatComposer.tsx | 6 +- .../src/components/chat/composerCommandKey.ts | 3 + apps/web/src/composer-logic.test.ts | 14 +++++ apps/web/src/composer-logic.ts | 9 ++- ...panel-conditioned-enter-still-sends.e2e.ts | 59 +++++++++++++++++++ 6 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 scripts/scenarios/panel-conditioned-enter-still-sends.e2e.ts diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 34d3f7244a49b..67638fc4e63ca 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -3726,6 +3726,7 @@ export default function ChatView(props: ChatViewProps) { resolvedTheme={resolvedTheme} settings={settings} keybindings={keybindings} + getShortcutContext={getShortcutContextStable} terminalOpen={Boolean(terminalUiState.terminalOpen)} gitCwd={gitCwd} pullRequestProjectId={ diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index 2499ddf7c2279..ff14154dccb8f 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -142,7 +142,7 @@ import { useDictationSubmissionShortcut } from "../../speech/useDictationSubmiss import { useEnvironmentSpeechInput } from "../../speech/useEnvironmentSpeechInput"; import { ComposerSpeechButton, ComposerSpeechRecordingPill } from "./ComposerSpeechButton"; import type { AssistantCitationSourceAnchor } from "~/lib/assistantTextSelection"; -import { shortcutLabelForCommand } from "../../keybindings"; +import { shortcutLabelForCommand, type ShortcutMatchContext } from "../../keybindings"; import { type TerminalContextDraft, type TerminalContextSelection, @@ -420,6 +420,7 @@ export interface ChatComposerProps { resolvedTheme: "light" | "dark"; settings: UnifiedSettings; keybindings: ResolvedKeybindingsConfig; + getShortcutContext: (eventTarget: EventTarget | null) => ShortcutMatchContext; terminalOpen: boolean; gitCwd: string | null; pullRequestProjectId: ProjectId | null; @@ -533,6 +534,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) resolvedTheme, settings, keybindings, + getShortcutContext, terminalOpen, gitCwd, pullRequestProjectId, @@ -2199,6 +2201,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) const intent = composerSubmissionIntentForKey({ event, keybindings, + context: getShortcutContext(event.target), isSoftwareKeyboardOpen, isDraftThread: routeKind === "draft", isRunning: phase === "running" || phase === "connecting", @@ -2318,6 +2321,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) const onComposerCommandKey = (key: string, event: KeyboardEvent, isTaskItem?: boolean) => composerCommandKeyHandler({ keybindings, + getShortcutContext, sendShortcut: settings.sendShortcut, routeKind, phase, diff --git a/apps/web/src/components/chat/composerCommandKey.ts b/apps/web/src/components/chat/composerCommandKey.ts index 243b69d09e4df..549c3ee12f650 100644 --- a/apps/web/src/components/chat/composerCommandKey.ts +++ b/apps/web/src/components/chat/composerCommandKey.ts @@ -40,6 +40,7 @@ import type { Dispatch, RefObject, SetStateAction } from "react"; */ export function composerCommandKeyHandler(input: { readonly keybindings: ChatComposerProps["keybindings"]; + readonly getShortcutContext: ChatComposerProps["getShortcutContext"]; readonly sendShortcut: ChatComposerProps["settings"]["sendShortcut"]; readonly routeKind: ChatComposerProps["routeKind"]; readonly phase: ChatComposerProps["phase"]; @@ -101,6 +102,7 @@ export function composerCommandKeyHandler(input: { }) { const { keybindings, + getShortcutContext, sendShortcut, routeKind, phase, @@ -223,6 +225,7 @@ export function composerCommandKeyHandler(input: { const submissionIntent = composerSubmissionIntentForKey({ event, keybindings, + context: getShortcutContext(event.target), isSoftwareKeyboardOpen, isDraftThread: routeKind === "draft", isRunning: phase === "running" || phase === "connecting", diff --git a/apps/web/src/composer-logic.test.ts b/apps/web/src/composer-logic.test.ts index e7ec86822a547..8a714a4c572db 100644 --- a/apps/web/src/composer-logic.test.ts +++ b/apps/web/src/composer-logic.test.ts @@ -98,6 +98,7 @@ describe("formatAssistantCitationForComposer", () => { describe("composerSubmissionIntentForKey", () => { const input = { keybindings: DEFAULT_RESOLVED_KEYBINDINGS, + context: {}, platform: "Linux", isSoftwareKeyboardOpen: false, isDraftThread: false, @@ -244,6 +245,19 @@ describe("composerSubmissionIntentForKey", () => { }, ); + it("sends on Enter when a binding conditioned on !rightPanelOpen is inactive", () => { + const keybindings = mergeWithDefaultKeybindings( + compileResolvedKeybindingsConfig([ + { key: "enter", command: "commandPalette.toggle", when: "!rightPanelOpen" }, + ]), + ); + const conditioned = { ...input, keybindings, sendShortcut: "enter" as const, event: enter }; + expect(composerSubmissionIntentForKey(conditioned)).toBeNull(); + expect( + composerSubmissionIntentForKey({ ...conditioned, context: { rightPanelOpen: true } }), + ).toBe("foreground"); + }); + it.each([ { isSoftwareKeyboardOpen: true }, { event: { ...enter, isComposing: true } }, diff --git a/apps/web/src/composer-logic.ts b/apps/web/src/composer-logic.ts index 37fef411897fc..ca266c77e0d45 100644 --- a/apps/web/src/composer-logic.ts +++ b/apps/web/src/composer-logic.ts @@ -15,7 +15,11 @@ import { type ComposerPromptSegment, } from "./composer-editor-mentions"; -import { resolveShortcutCommand, type ShortcutEventLike } from "./keybindings"; +import { + resolveShortcutCommand, + type ShortcutEventLike, + type ShortcutMatchContext, +} from "./keybindings"; export type ComposerTriggerKind = "path" | "pull-request" | "slash-command" | "skill"; export type ComposerSlashCommand = "model" | "side" | "btw" | "clear"; @@ -59,6 +63,8 @@ export function composerRequiresModifier( export function composerSubmissionIntentForKey(input: { event: ShortcutEventLike & { isComposing?: boolean; keyCode?: number; repeat?: boolean }; keybindings: ResolvedKeybindingsConfig; + /** The when-clause context ChatView's shortcut listeners see, so both resolve Enter alike. */ + context: Partial; platform?: string; isSoftwareKeyboardOpen: boolean; isDraftThread: boolean; @@ -73,6 +79,7 @@ export function composerSubmissionIntentForKey(input: { const command = resolveShortcutCommand(event, input.keybindings, { ...(input.platform === undefined ? {} : { platform: input.platform }), context: { + ...input.context, composerFocus: true, draftThreadRoute: input.isDraftThread, turnRunning: input.isRunning === true, diff --git a/scripts/scenarios/panel-conditioned-enter-still-sends.e2e.ts b/scripts/scenarios/panel-conditioned-enter-still-sends.e2e.ts new file mode 100644 index 0000000000000..f837946e75655 --- /dev/null +++ b/scripts/scenarios/panel-conditioned-enter-still-sends.e2e.ts @@ -0,0 +1,59 @@ +import { expect } from "e2e"; +import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; +import * as Path from "effect/Path"; + +import { T3_HOME } from "../e2e.config.ts"; +import { runPromise, test, WEB } from "./t3.ts"; + +// A saved rule that binds Enter to the command palette only while the right panel is closed. +const panelConditionedEnter = Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const keybindingsPath = (yield* Path.Path).join(T3_HOME, "userdata/keybindings.json"); + const original = (yield* fs.exists(keybindingsPath)) + ? yield* fs.readFileString(keybindingsPath) + : null; + return { + save: fs.writeFileString( + keybindingsPath, + '[{ "key": "enter", "command": "commandPalette.toggle", "when": "!rightPanelOpen" }]\n', + ), + restore: + original === null + ? fs.remove(keybindingsPath, { force: true }) + : fs.writeFileString(keybindingsPath, original), + }; +}); + +test( + "Enter sends from the composer while the right panel is open and a rule binds it when closed", + WEB, + async ({ t3, app, screen }) => { + const keybindings = await runPromise(panelConditionedEnter); + try { + await runPromise(keybindings.save); + const project = await t3.project("demo"); + const thread = await t3.thread({ project, title: "Panel work", messages: ["Start"] }); + + // The settings list shows the rule once the server has loaded the saved file. + await t3.open("/settings/keybindings"); + await screen + .getByRole("button", "Edit shortcut for Command Palette: Toggle: Enter") + .waitFor(); + + await t3.open(thread); + await screen.getByRole("button", "Toggle right panel").tap(); + await screen.getByRole("button", { name: "Toggle right panel", pressed: true }).waitFor(); + + const composer = screen.getByTestId("composer-editor"); + await composer.tap(); + await composer.pressSequentially("Hello with the panel open"); + await composer.press("Enter"); + await app.screenshot("after-enter"); + await expect(composer).toHaveText(""); + await expect(screen.getByText("Hello with the panel open")).toBeVisible(); + } finally { + await runPromise(keybindings.restore); + } + }, +); From 7e69e880b630ceb372e22a41987543d997771269 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 15 Nov 2026 05:00:17 -0800 Subject: [PATCH 026/124] fix(server): project search starts working once a slow content index finishes building (#37138) --- .../workspace/WorkspaceSearchIndex.test.ts | 177 ++++++++++ .../src/workspace/WorkspaceSearchIndex.ts | 304 ++++++++++++++++-- 2 files changed, 451 insertions(+), 30 deletions(-) diff --git a/apps/server/src/workspace/WorkspaceSearchIndex.test.ts b/apps/server/src/workspace/WorkspaceSearchIndex.test.ts index 6c4e873f12b47..13ccf443fb108 100644 --- a/apps/server/src/workspace/WorkspaceSearchIndex.test.ts +++ b/apps/server/src/workspace/WorkspaceSearchIndex.test.ts @@ -12,6 +12,7 @@ import * as HostProcess from "@t3tools/shared/HostProcess"; import { PROJECT_FAVICON_EXTENSIONS, isProjectFaviconPath } from "@t3tools/shared/projectFavicon"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; +import { ChildProcessSpawner } from "effect/process"; import * as Fiber from "effect/Fiber"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; @@ -294,6 +295,182 @@ it.effect("continues whole-word searches after a filtered grep page", () => ).pipe(Effect.provide(testLayer)), ); +it.live("answers content searches with git grep while a slow content index builds", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const vcsProcess = yield* VcsProcess.VcsProcess; + const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-content-warmup-" }); + yield* vcsProcess.run({ operation: "test.gitInit", command: "git", args: ["init"], cwd }); + yield* fileSystem.writeFileString(path.join(cwd, ".gitignore"), "ignored.txt\n"); + yield* fileSystem.makeDirectory(path.join(cwd, "src")); + yield* fileSystem.writeFileString( + path.join(cwd, "src/a.ts"), + "const Needle = 1;\r\nneedles(a.b);\nnone\n", + ); + yield* fileSystem.writeFileString(path.join(cwd, "ignored.txt"), "needle\n"); + // fff leaves out what `.ignore` excludes, so git results must leave it out too. + yield* fileSystem.writeFileString(path.join(cwd, ".ignore"), "out/\n"); + yield* fileSystem.makeDirectory(path.join(cwd, "out/deep"), { recursive: true }); + yield* fileSystem.writeFileString(path.join(cwd, "out/deep/g.txt"), "needle\n"); + + let isWarmupComplete = false; + const grep = vi.fn(async () => ({ + ok: true as const, + value: { + items: [], + totalMatched: 0, + totalFilesSearched: 0, + totalFiles: 0, + filteredFileCount: 0, + nextCursor: null, + } satisfies GrepResult, + })); + const finder = { + destroy: vi.fn(), + waitForIndexReady: vi.fn(async () => ({ ok: true as const, value: false })), + getScanProgressAsync: async () => ({ + ok: true as const, + value: { isScanning: false, isWarmupComplete }, + }), + watch: () => ({ ok: true as const, value: () => undefined }), + grepAsync: grep, + } as unknown as FileFinder; + const noPcreFinder = { + ...finder, + waitForIndexReady: vi.fn(async () => ({ ok: true as const, value: true })), + } as unknown as FileFinder; + vi.spyOn(FileFinder, "create") + .mockReturnValueOnce({ ok: true, value: finder }) + .mockReturnValueOnce({ ok: true, value: noPcreFinder }) + .mockReturnValueOnce({ ok: true, value: noPcreFinder }) + .mockReturnValueOnce({ ok: true, value: noPcreFinder }); + + const searchIndex = yield* WorkspaceSearchIndex.make(cwd, "content"); + const search = (input: Partial[0]>) => + searchIndex.searchContents({ + query: "needle", + limit: 10, + caseSensitive: false, + wholeWord: false, + useRegex: false, + ...input, + }); + + expect(yield* search({})).toEqual({ + matches: [ + { + path: "src/a.ts", + lineNumber: 1, + lineContent: "const Needle = 1;", + matchRanges: [{ start: 6, end: 12 }], + }, + { + path: "src/a.ts", + lineNumber: 2, + lineContent: "needles(a.b);", + matchRanges: [{ start: 0, end: 6 }], + }, + ], + truncated: false, + }); + expect((yield* search({ caseSensitive: true, wholeWord: true })).matches).toEqual([]); + expect((yield* search({ query: "a.b" })).matches.map((match) => match.matchRanges)).toEqual([ + [{ start: 8, end: 11 }], + ]); + expect( + (yield* search({ query: "ne+dles?", useRegex: true })).matches.map( + (match) => match.matchRanges, + ), + ).toEqual([[{ start: 6, end: 12 }], [{ start: 0, end: 7 }]]); + expect( + (yield* search({ query: "[a-z]e", useRegex: true })).matches.map( + (match) => match.matchRanges, + ), + ).toEqual([ + [{ start: 4, end: 6 }], + [ + { start: 6, end: 8 }, + { start: 10, end: 12 }, + ], + [ + { start: 0, end: 2 }, + { start: 4, end: 6 }, + ], + [{ start: 2, end: 4 }], + ]); + const invalid = yield* search({ query: "a.b(", useRegex: true }); + expect(invalid.matches).toEqual([]); + expect(invalid.regexFallbackError).toBeDefined(); + expect(yield* search({ limit: 1 })).toMatchObject({ truncated: true }); + expect(grep).not.toHaveBeenCalled(); + + // When git cannot give regex results or their ranges, fff answers once ready instead. + const failingGitIndex = ( + failingArgument: string, + stderr: string, + output: { readonly exitCode: number; readonly stdout: string } = { + exitCode: 128, + stdout: "", + }, + ) => + WorkspaceSearchIndex.make(cwd, "content").pipe( + Effect.provideService( + VcsProcess.VcsProcess, + VcsProcess.VcsProcess.of({ + run: (input) => + input.args.includes(failingArgument) + ? Effect.succeed({ + exitCode: ChildProcessSpawner.ExitCode(output.exitCode), + stdout: output.stdout, + stderr, + stdoutTruncated: output.exitCode === 0, + stderrTruncated: false, + }) + : vcsProcess.run(input), + }), + ), + ); + const regexInput = { + query: "ne+dles?", + limit: 10, + caseSensitive: false, + wholeWord: true, + useRegex: true, + }; + const noPcreIndex = yield* failingGitIndex( + "-P", + "fatal: cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE\n", + ); + expect((yield* noPcreIndex.searchContents(regexInput)).regexFallbackError).toBeUndefined(); + expect(grep).toHaveBeenCalledTimes(1); + const noRangesIndex = yield* failingGitIndex("-o", "fatal: unexpected failure\n"); + yield* noRangesIndex.searchContents(regexInput); + expect(grep).toHaveBeenCalledTimes(2); + // Ranges output cut off after line 1: line 2 is left out and the result says so. + const cutRangesIndex = yield* failingGitIndex("-o", "", { + exitCode: 0, + stdout: "src/a.ts\u00001\u00007\u0000Needle\nsrc/a.ts\u00002", + }); + expect(yield* cutRangesIndex.searchContents(regexInput)).toEqual({ + matches: [ + { + path: "src/a.ts", + lineNumber: 1, + lineContent: "const Needle = 1;", + matchRanges: [{ start: 6, end: 12 }], + }, + ], + truncated: true, + }); + + isWarmupComplete = true; + yield* search({}); + expect(grep).toHaveBeenCalledTimes(3); + expect(finder.destroy).not.toHaveBeenCalled(); + }).pipe(Effect.scoped, Effect.provide(testLayer)), +); + function fakeSymlinkFinder( directories: ReadonlyArray, options: { diff --git a/apps/server/src/workspace/WorkspaceSearchIndex.ts b/apps/server/src/workspace/WorkspaceSearchIndex.ts index fd92420396e48..858c420c72fb0 100644 --- a/apps/server/src/workspace/WorkspaceSearchIndex.ts +++ b/apps/server/src/workspace/WorkspaceSearchIndex.ts @@ -57,6 +57,9 @@ const WORKSPACE_INDEX_SCAN_TIMEOUT_MS = 15_000; const WORKSPACE_INDEX_IDLE_TTL = "15 minutes"; const CONTENT_SEARCH_TIME_BUDGET_MS = 250; const CONTENT_SEARCH_MAX_MATCHES_PER_FILE = 100; +const GIT_GREP_MAX_OUTPUT_BYTES = 4 * 1024 * 1024; +// fff's lineContent cap; matches past it are reported without ranges. +const CONTENT_LINE_MAX_LENGTH = 512; const SYMLINK_SCAN_CONCURRENCY = 16; // A scan no watcher can invalidate is reused this long, so typing does not rescan every folder. const UNWATCHED_SYMLINK_SCAN_TTL_MS = 2_000; @@ -294,6 +297,47 @@ function mapContentMatchRanges( })); } +function escapeRegExp(input: string): string { + return input.replace(/[\\^$.*+?()[\]{}|/]/g, "\\$&"); +} + +// Git before 2.55 reports wrong `--column` values for every match after a line's first, so +// later matches are found by their text, searching on from the end of the previous one. +function locateRegexMatches( + line: string, + matches: { readonly column: number; readonly texts: ReadonlyArray } | undefined, +): Array { + if (!matches) return []; + const lineBytes = Buffer.from(line); + const ranges: Array = []; + let start = matches.column - 1; + for (const text of matches.texts) { + const textBytes = Buffer.from(text); + if (ranges.length > 0) start = lineBytes.indexOf(textBytes, ranges.at(-1)![1]); + if (start < 0) break; + ranges.push([start, start + textBytes.length]); + } + return ranges; +} + +// `git grep -z` records: NUL-separated fields, one record per line; a truncated last line is dropped. +function parseGitGrepRecords( + stdout: string, + fieldCount: number, + truncated: boolean, +): Array> { + const lines = stdout.split("\n"); + if (truncated || lines.at(-1) === "") lines.pop(); + const records: Array> = []; + for (const line of lines) { + const fields = line.split("\0"); + if (fields.length < fieldCount) continue; + // Only the last field (the line or match text) can contain NUL bytes. + records.push([...fields.slice(0, fieldCount - 1), fields.slice(fieldCount - 1).join("\0")]); + } + return records; +} + // Post-filter whole words: consuming boundary regexes swallow adjacent separators and widen ranges; \b misses punctuation edges. function isWholeWordRange( line: string, @@ -576,26 +620,30 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* ( }).pipe(Effect.orDie), ); let initialScanTimedOut = false; + // The content index reads every file, which takes minutes where antivirus scans each open, + // so content searches do not wait for it; git grep answers until it is ready. + let contentIndexPending = variant === "content"; const vcsProcess = yield* VcsProcess.VcsProcess; let ignoredPaths = yield* listGitIgnoredPaths(vcsProcess, cwd, hostIsWindows); - yield* waitForIndexReady( - cwd, - finder, - ({ reason, cause }) => - new WorkspaceSearchIndexCreateFailed({ - cwd, - reason, - cause, - }), - ).pipe( - // The scan keeps running on this finder; failing here would destroy it and cache the failure. - Effect.catchTags({ - WorkspaceSearchIndexScanTimedOut: () => - Effect.sync(() => { - initialScanTimedOut = true; + if (variant === "paths") { + yield* waitForIndexReady( + cwd, + finder, + ({ reason, cause }) => + new WorkspaceSearchIndexCreateFailed({ + cwd, + reason, + cause, }), - }), - ); + ).pipe( + Effect.catchTags({ + WorkspaceSearchIndexScanTimedOut: () => + Effect.sync(() => { + initialScanTimedOut = true; + }), + }), + ); + } // fff's isScanning takes the picker lock too, so progress is read off the JS thread. const isScanning = Effect.promise(async () => { @@ -608,10 +656,203 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* ( }); const hasIncompleteInitialScan = () => (initialScanTimedOut ? isScanning : Effect.succeed(false)); + const isContentIndexReady = Effect.fn("WorkspaceSearchIndex.isContentIndexReady")(function* () { + if (!contentIndexPending) return true; + const progress = yield* Effect.promise(() => + finder.getScanProgressAsync().catch(() => undefined), + ); + if (progress?.ok && !progress.value.isScanning && progress.value.isWarmupComplete) { + contentIndexPending = false; + } + return !contentIndexPending; + }); + + const runGitGrep = Effect.fn("WorkspaceSearchIndex.runGitGrep")(function* ( + pattern: string, + options: { + readonly regexMode: boolean; + readonly ignoreCase: boolean; + readonly maxMatchesPerFile: number; + readonly onlyMatching: boolean; + }, + ) { + return yield* vcsProcess + .run({ + operation: "WorkspaceSearchIndex.gitGrep", + command: "git", + args: [ + "-c", + "core.quotePath=false", + "-c", + "core.fsmonitor=false", + "grep", + "--untracked", + "--no-color", + "-I", + "-n", + "-z", + "-m", + String(options.maxMatchesPerFile), + ...(options.onlyMatching ? ["-o", "--column"] : []), + ...(options.ignoreCase ? ["-i"] : []), + options.regexMode ? "-P" : "-F", + "-e", + pattern, + ], + cwd, + allowNonZeroExit: true, + // Defender scanning fff's warmup reads slows git too; a late answer beats a failure. + timeoutMs: 30_000, + maxOutputBytes: GIT_GREP_MAX_OUTPUT_BYTES, + outputMode: "truncate", + appendTruncationMarker: false, + }) + .pipe(Effect.option); + }); + + // fff leaves out what `.ignore` files exclude, which git cannot evaluate; the nearest + // `.ignore` that matches a path or one of its folders decides, as in decideByIgnoreFiles. + const makeIgnoreFileFilter = () => { + const ignoreFiles = new Map(); + const readIgnoreFile = Effect.fn("WorkspaceSearchIndex.readIgnoreFile")(function* ( + directory: string, + ) { + if (ignoreFiles.has(directory)) return ignoreFiles.get(directory); + const contents = yield* fileSystem + .readFileString(path.join(cwd, directory, ".ignore")) + .pipe(Effect.option); + const file = Option.isSome(contents) ? parseIgnoreFile(contents.value) : undefined; + ignoreFiles.set(directory, file); + return file; + }); + return Effect.fn("WorkspaceSearchIndex.isExcludedByIgnoreFiles")(function* ( + relativePath: string, + ) { + const segments = relativePath.split("/"); + for (let depth = 1; depth <= segments.length; depth += 1) { + const candidate = segments.slice(0, depth).join("/"); + for (let ancestor = depth - 1; ancestor >= 0; ancestor -= 1) { + const directory = segments.slice(0, ancestor).join("/"); + const file = yield* readIgnoreFile(directory); + const relative = directory ? candidate.slice(directory.length + 1) : candidate; + const decision = file?.decide(relative, depth < segments.length); + if (decision === "ignored") return true; + if (decision === "included") break; + } + } + return false; + }); + }; + + // fff's grep before its content index is ready opens every file itself, as slowly as the build. + // None when git cannot search this folder (not a repository, git missing, no PCRE for a regex). + const searchContentsWithGit = Effect.fn("WorkspaceSearchIndex.searchContentsWithGit")(function* ( + input: Omit, + ): Effect.fn.Return> { + const options = { + ignoreCase: !input.caseSensitive, + maxMatchesPerFile: CONTENT_SEARCH_MAX_MATCHES_PER_FILE, + onlyMatching: false, + }; + let regexMode = input.useRegex; + let regexFallbackError: string | undefined; + let result = yield* runGitGrep(input.query, { ...options, regexMode }); + if (regexMode && Option.isSome(result) && result.value.exitCode === 128) { + if (/USE_LIBPCRE|Perl-compatible/i.test(result.value.stderr)) return Option.none(); + // fff searches an invalid pattern as a literal and reports why. + regexFallbackError = result.value.stderr.trim().split("\n")[0] || "Invalid regex"; + regexMode = false; + result = yield* runGitGrep(input.query, { ...options, regexMode }); + } + if (Option.isNone(result) || result.value.exitCode > 1) return Option.none(); + + // Per line: the first match's byte column and every match's text, in order. + const regexMatches = new Map(); + let rangesTruncated = false; + if (regexMode) { + // Ranges come from git too: running the user's pattern in JS could backtrack without bound. + const ranges = yield* runGitGrep(input.query, { ...options, regexMode, onlyMatching: true }); + // Without ranges, whole-word filtering would drop every hit and report no matches. + if (Option.isNone(ranges) || ranges.value.exitCode > 1) return Option.none(); + rangesTruncated = ranges.value.stdoutTruncated; + if (ranges.value.exitCode === 0) { + for (const record of parseGitGrepRecords( + ranges.value.stdout, + 4, + ranges.value.stdoutTruncated, + )) { + const [relativePath, lineNumber, column, text] = record as [ + string, + string, + string, + string, + ]; + const key = `${relativePath}\0${lineNumber}`; + const lineMatches = regexMatches.get(key); + if (lineMatches) lineMatches.texts.push(text); + else regexMatches.set(key, { column: Number(column), texts: [text] }); + } + } + } + const literal = regexMode + ? undefined + : new RegExp(escapeRegExp(input.query), input.caseSensitive ? "gu" : "giu"); + + const isExcludedByIgnoreFiles = makeIgnoreFileFilter(); + const matches: Array = []; + let rangesCutOff = false; + for (const record of parseGitGrepRecords( + result.value.stdout, + 3, + result.value.stdoutTruncated, + )) { + const [relativePath, lineNumber, rawLine] = record as [string, string, string]; + if (yield* isExcludedByIgnoreFiles(relativePath)) continue; + const key = `${relativePath}\0${lineNumber}`; + // Lines past the cut-off of a truncated ranges run have no ranges; stop there instead. + if (rangesTruncated && !regexMatches.has(key)) { + rangesCutOff = true; + break; + } + const lineContent = (rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine).slice( + 0, + CONTENT_LINE_MAX_LENGTH, + ); + const matchRanges = ( + literal + ? Array.from(lineContent.matchAll(literal), (match) => ({ + start: match.index, + end: match.index + match[0].length, + })) + : mapContentMatchRanges( + lineContent, + locateRegexMatches(lineContent, regexMatches.get(key)), + ) + ).filter( + (range) => + range.end <= lineContent.length && + (!input.wholeWord || isWholeWordRange(lineContent, range)), + ); + if (input.wholeWord && matchRanges.length === 0) continue; + matches.push({ + path: relativePath, + lineNumber: Number(lineNumber), + lineContent, + matchRanges, + }); + if (matches.length > input.limit) break; + } + return Option.some({ + matches: matches.slice(0, input.limit), + truncated: matches.length > input.limit || result.value.stdoutTruncated || rangesCutOff, + ...(regexFallbackError !== undefined ? { regexFallbackError } : {}), + }); + }); + const runSearch = Effect.fn("WorkspaceSearchIndex.runSearch")(function* ( query: string, pageSize: number, - operation: "directorySearch" | "fileSearch" | "getScanProgress" | "grep" | "mixedSearch", + operation: "directorySearch" | "fileSearch" | "grep" | "mixedSearch", execute: () => Promise>, ): Effect.fn.Return { // fff's searches wait on its picker lock, which its watcher can hold for minutes. @@ -1038,20 +1279,23 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* ( const searchContents: WorkspaceSearchIndex["Service"]["searchContents"] = Effect.fn( "WorkspaceSearchIndex.searchContents", )(function* (input) { - if (initialScanTimedOut) { - const progress = yield* runSearch(input.query, input.limit, "getScanProgress", () => - finder.getScanProgressAsync(), + if (!(yield* isContentIndexReady())) { + const gitResult = yield* searchContentsWithGit(input); + if (Option.isSome(gitResult)) return gitResult.value; + yield* waitForIndexReady( + cwd, + finder, + ({ reason, cause }) => + new WorkspaceSearchIndexSearchFailed({ + cwd, + queryLength: input.query.length, + pageSize: input.limit, + reason, + cause, + }), ); - // Files the first scan has not indexed yet would read as "No results found". - if (!progress.isWarmupComplete) { - return yield* new WorkspaceSearchIndexScanTimedOut({ - cwd, - timeout: WORKSPACE_INDEX_SCAN_TIMEOUT, - }); - } - initialScanTimedOut = false; + contentIndexPending = false; } - let { searchQuery, regexMode } = buildContentSearchQuery(input); const deadline = performance.now() + CONTENT_SEARCH_TIME_BUDGET_MS; // Grep cursors advance per file; whole-word filtering needs enough raw candidates before advancing. From 304cf149c8cb5e2c1ab921428773361752d7aff2 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 15 Nov 2026 14:01:50 +0100 Subject: [PATCH 027/124] fix(web): terminal file links preserve numeric filename suffixes (#37201) Co-authored-by: Grok 4.7 Co-authored-by: Julius Marminge --- .../src/process/externalLauncher.test.ts | 47 ++++++++- apps/server/src/process/externalLauncher.ts | 43 ++++++-- apps/web/src/editorPreferences.test.ts | 22 +++++ apps/web/src/editorPreferences.ts | 7 +- apps/web/src/hooks/useFileMetadata.ts | 14 ++- apps/web/src/terminal-links.test.ts | 23 ++++- apps/web/src/terminal-links.ts | 20 ++-- packages/contracts/src/editor.ts | 9 +- ...minal-file-uri-opens-its-exact-file.e2e.ts | 98 +++++++++++++++++++ 9 files changed, 253 insertions(+), 30 deletions(-) create mode 100644 scripts/scenarios/terminal-file-uri-opens-its-exact-file.e2e.ts diff --git a/apps/server/src/process/externalLauncher.test.ts b/apps/server/src/process/externalLauncher.test.ts index f215fe4ebc6a2..8ce873733d443 100644 --- a/apps/server/src/process/externalLauncher.test.ts +++ b/apps/server/src/process/externalLauncher.test.ts @@ -117,8 +117,8 @@ it.effect.skipIf(windowsHost)("opens home-relative targets using the server home const spawned: ChildProcess.StandardCommand[] = []; yield* Effect.gen(function* () { const launcher = yield* ExternalLauncher.ExternalLauncher; - for (const cwd of ["~/Applications/My App.app", "/Applications/My App.app"]) { - yield* launcher.launchEditor({ editor: "file-manager", cwd }); + for (const cwd of ["~/Applications/My App.app", "/Applications/My App.app", "/tmp/a:12"]) { + yield* launcher.launchEditor({ editor: "file-manager", cwd, position: { line: 5 } }); } yield* launcher.launchEditor({ editor: "vscode", cwd: "~/notes/todo.md:3" }); yield* launcher.launchEditor({ @@ -141,6 +141,7 @@ it.effect.skipIf(windowsHost)("opens home-relative targets using the server home [ ["/Users/server/Applications/My App.app"], ["/Applications/My App.app"], + ["/tmp/a:12"], ["--goto", "/Users/server/notes/todo.md:3"], ["/Users/server/notes", "--line", "3", "/Users/server/notes/todo.md"], ], @@ -203,12 +204,20 @@ it.effect.skipIf(windowsHost)( const path = yield* Path.Path; const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" }); const project = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-project-" }); - const codePath = path.join(binDir, "code"); - yield* fileSystem.writeFileString(codePath, "#!/bin/sh\n"); - yield* fileSystem.chmod(codePath, 0o755); + for (const name of ["code", "zeditor", "idea"]) { + const executable = path.join(binDir, name); + yield* fileSystem.writeFileString(executable, "#!/bin/sh\n"); + yield* fileSystem.chmod(executable, 0o755); + } const colonProject = path.join(project, "build:2026"); yield* fileSystem.makeDirectory(colonProject); const file = `${path.join(project, "file.ts")}:3:7`; + const exactFile = path.join(project, "a:12"); + yield* fileSystem.writeFileString(exactFile, "exact"); + yield* fileSystem.makeDirectory(`${exactFile}:1:1`); + const positionedFile = path.join(project, "position.txt"); + yield* fileSystem.writeFileString(positionedFile, "position"); + yield* fileSystem.writeFileString(`${positionedFile}:7`, "collision"); const spawned: ChildProcess.StandardCommand[] = []; yield* Effect.gen(function* () { @@ -216,6 +225,27 @@ it.effect.skipIf(windowsHost)( yield* launcher.launchEditor({ editor: "vscode", cwd: project }); yield* launcher.launchEditor({ editor: "vscode", cwd: file }); yield* launcher.launchEditor({ editor: "vscode", cwd: colonProject }); + for (const position of [{}, { line: 5 }]) { + yield* launcher.launchEditor({ editor: "vscode", cwd: exactFile, position }); + } + + yield* launcher.launchEditor({ editor: "zed", cwd: exactFile, position: {} }); + yield* launcher.launchEditor({ + editor: "zed", + cwd: positionedFile, + position: { line: 5, column: 3 }, + }); + yield* launcher.launchEditor({ editor: "zed", cwd: positionedFile, position: { line: 7 } }); + yield* launcher.launchEditor({ + editor: "vscode", + cwd: `${project}/exponent:1e3`, + position: { line: 5 }, + }); + yield* launcher.launchEditor({ + editor: "idea", + cwd: exactFile, + position: { line: 5, column: 3 }, + }); }).pipe( Effect.provide( layerTest({ @@ -233,6 +263,13 @@ it.effect.skipIf(windowsHost)( ["--new-window", project], ["--goto", file], ["--new-window", colonProject], + [exactFile], + [exactFile], + [exactFile], + [`${positionedFile}:5:3`], + [positionedFile], + [`${project}/exponent:1e3`], + ["--line", "5", "--column", "2", exactFile], ], ); }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), diff --git a/apps/server/src/process/externalLauncher.ts b/apps/server/src/process/externalLauncher.ts index 2bdb7bd3db4db..caa6664a1ba80 100644 --- a/apps/server/src/process/externalLauncher.ts +++ b/apps/server/src/process/externalLauncher.ts @@ -142,17 +142,37 @@ function resolveCommandEditorArgs( target: string, isDirectory: boolean, projectRoot: string | undefined, + position: LaunchEditorInput["position"], ): ReadonlyArray { - const parsedTarget = parseTargetPathAndPosition(target); + const parsedTarget = + position === undefined + ? parseTargetPathAndPosition(target) + : Option.fromUndefinedOr(position.line).pipe( + Option.map((line) => ({ + path: target, + line: String(line), + column: Option.fromUndefinedOr(position.column).pipe(Option.map(String)), + })), + ); + const positionedTarget = Option.match(parsedTarget, { + onNone: () => target, + onSome: ({ path, line, column }) => [path, line, ...Option.toArray(column)].join(":"), + }); switch (editor.launchStyle) { case "direct-path": - return [target]; + return [positionedTarget]; case "goto": // An existing folder wins over a position: `build:2026` is a legal folder name. // Without a window flag, VS Code's window settings may replace the user's last active window. if (isDirectory) return ["--new-window", target]; - return Option.isSome(parsedTarget) ? ["--goto", target] : [target]; + // VS Code splits every numeric colon segment, including ones inside the filename. + if ( + position !== undefined && + target.split(":").some((segment) => !Number.isNaN(Number(segment))) + ) + return [target]; + return Option.isSome(parsedTarget) ? ["--goto", positionedTarget] : [target]; case "line-column": return Option.match(parsedTarget, { onNone: () => [target], @@ -526,6 +546,18 @@ const resolveEditorLaunch = Effect.fn("resolveEditorLaunch")(function* ( return yield* new ExternalLauncherUnknownEditorError({ editor: input.editor }); } + const target = input.cwd; + let position = input.position; + if (editorDef.id === "zed" && position?.line !== undefined) { + // Zed resolves existing paths before positions, so ambiguous spellings must stay literal. + const collision = + /:\d+(?=:|$)/.test(target) || + (yield* (yield* FileSystem.FileSystem) + .exists(`${target}:${position.line}${position.column ? `:${position.column}` : ""}`) + .pipe(Effect.orElseSucceed(() => true))); + if (collision) position = {}; + } + if (editorDef.commands) { const resolved = yield* resolveEditorCommand(editorDef, env); if (Option.isNone(resolved)) { @@ -537,7 +569,7 @@ const resolveEditorLaunch = Effect.fn("resolveEditorLaunch")(function* ( const { command, baseArgs } = resolved.value; const isDirectory = editorDef.launchStyle === "goto" && - (yield* (yield* FileSystem.FileSystem).stat(input.cwd).pipe( + (yield* (yield* FileSystem.FileSystem).stat(target).pipe( Effect.map((info) => info.type === "Directory"), Effect.orElseSucceed(() => false), )); @@ -547,7 +579,7 @@ const resolveEditorLaunch = Effect.fn("resolveEditorLaunch")(function* ( command, args: [ ...baseArgs, - ...resolveCommandEditorArgs(editorDef, input.cwd, isDirectory, input.projectRoot), + ...resolveCommandEditorArgs(editorDef, target, isDirectory, input.projectRoot, position), ], }; } @@ -561,7 +593,6 @@ const resolveEditorLaunch = Effect.fn("resolveEditorLaunch")(function* ( return yield* new ExternalLauncherUnsupportedEditorError({ editor: input.editor }); } - const target = input.cwd; if (input.reveal === true) { return yield* resolveFileManagerRevealLaunch(target, platform, env, command); } diff --git a/apps/web/src/editorPreferences.test.ts b/apps/web/src/editorPreferences.test.ts index 8a525628db5a4..c55fd71a385b8 100644 --- a/apps/web/src/editorPreferences.test.ts +++ b/apps/web/src/editorPreferences.test.ts @@ -88,3 +88,25 @@ it.each(["read", "write"] as const)( }); }, ); + +it.each([ + ["file:///work/read%20me.md", "/work/read me.md", {}], + [ + "file://foreign/share/read%20me.md:12#L5", + String.raw`\\foreign\share\read me.md:12`, + { line: 5 }, + ], + ["file:///work/report.txt:12", "/work/report.txt:12", {}], + ["file:///work/report.txt:12#L5C3", "/work/report.txt:12", { line: 5, column: 3 }], +])( + "sends a plain exact path for %s to compatible and older servers", + async (uri, cwd, position) => { + state.allowed.add(secondary); + const open = useOpenInPreferredEditor(secondary, ["vscode"]); + await open(uri); + expect(state.run).toHaveBeenCalledExactlyOnceWith({ + environmentId: secondary, + input: { cwd, position, editor: "vscode" }, + }); + }, +); diff --git a/apps/web/src/editorPreferences.ts b/apps/web/src/editorPreferences.ts index 712d7affc2e0a..6b3e99c1bda67 100644 --- a/apps/web/src/editorPreferences.ts +++ b/apps/web/src/editorPreferences.ts @@ -10,6 +10,8 @@ import { type AtomCommandFailure, type AtomCommandResult, } from "@t3tools/client-runtime/state/runtime"; +import { parseFileUrlHref, splitFilePathPosition } from "@t3tools/shared/fileLinks"; +import { safeDecodeURIComponent } from "@t3tools/shared/markdownLinks"; import * as Cause from "effect/Cause"; import * as Schema from "effect/Schema"; import { AsyncResult } from "effect/reactivity"; @@ -127,10 +129,13 @@ export function useOpenInPreferredEditor( ), ); } + const uri = /^file:\/\//iu.test(targetPath) ? parseFileUrlHref(targetPath) : null; + const { line, column } = uri ? splitFilePathPosition("", uri.hash) : {}; const result = await openInEditor({ environmentId, input: { - cwd: targetPath, + cwd: uri ? safeDecodeURIComponent(uri.path) : targetPath, + ...(uri ? { position: { line, column } } : {}), editor, ...(projectRoot ? { projectRoot } : {}), }, diff --git a/apps/web/src/hooks/useFileMetadata.ts b/apps/web/src/hooks/useFileMetadata.ts index 398df04a88ded..5b8f1a0cfc791 100644 --- a/apps/web/src/hooks/useFileMetadata.ts +++ b/apps/web/src/hooks/useFileMetadata.ts @@ -3,12 +3,17 @@ import { type EnvironmentId, type ScopedThreadRef, } from "@t3tools/contracts"; -import { resolvePathLinkTarget, splitFilePathPosition } from "@t3tools/shared/fileLinks"; +import { + parseFileUrlHref, + resolvePathLinkTarget, + splitFilePathPosition, +} from "@t3tools/shared/fileLinks"; import { isAbsolutePath, isNetworkPath, normalizeProjectPathForComparison, } from "@t3tools/shared/path"; +import { safeDecodeURIComponent } from "@t3tools/shared/markdownLinks"; import { useContext } from "react"; import { useEnvironmentScope } from "../state/session"; @@ -22,9 +27,14 @@ export function getFileMetadataQueryAtom( cwd?: string, threadRef?: ScopedThreadRef | null, ) { + const uri = /^file:\/\//iu.test(path) ? parseFileUrlHref(path) : null; const hostPath = splitFilePathPosition(path).path; const targetPath = normalizeProjectPathForComparison( - hostPath.startsWith("~/") || !cwd ? hostPath : resolvePathLinkTarget(hostPath, cwd), + uri + ? safeDecodeURIComponent(uri.path) + : hostPath.startsWith("~/") || !cwd + ? hostPath + : resolvePathLinkTarget(hostPath, cwd), ); const absolute = isAbsolutePath(targetPath) || targetPath.startsWith("~/"); return ( diff --git a/apps/web/src/terminal-links.test.ts b/apps/web/src/terminal-links.test.ts index 105fc9719b48e..2bf8714e700e7 100644 --- a/apps/web/src/terminal-links.test.ts +++ b/apps/web/src/terminal-links.test.ts @@ -351,11 +351,32 @@ describe("resolvePathLinkTarget", () => { // regression: #36005 OSC 8 file URIs were joined onto the cwd as relative paths it("does not resolve an OSC 8 file URI target against the cwd", () => { expect(resolveTerminalLinkTarget("file:///var/tmp/report.txt", "/workspace/project")).toBe( - "/var/tmp/report.txt", + "file:///var/tmp/report.txt", ); expect( resolveTerminalLinkTarget("file://remote.invalid/var/tmp/report.txt", "/workspace/project"), ).not.toMatch(/^\/workspace\/project\//); + expect( + resolveTerminalLinkTarget( + "file://myhost/var/tmp/report.txt:12#L5", + "/workspace/project", + "myhost", + ), + ).toBe("file:///var/tmp/report.txt:12#L5"); + expect( + resolveTerminalLinkTarget( + "file://foreign/var/tmp/report.txt:12#L5", + "/workspace/project", + "myhost", + ), + ).toBe("file://foreign/var/tmp/report.txt:12#L5"); + for (const name of ["report.txt:12-64", "report.txt:0", "report.txt:12"]) { + const uri = `file:///var/tmp/${encodeURIComponent(name)}`; + expect(resolveTerminalLinkTarget(uri, "/workspace/project")).toBe(uri); + } + expect( + resolveTerminalLinkTarget("file:///var/tmp/report.txt:12#L5", "/workspace/project"), + ).toBe("file:///var/tmp/report.txt:12#L5"); expect(resolvePathLinkTarget("file:notes.md", "/workspace/project")).toBe( "/workspace/project/file:notes.md", ); diff --git a/apps/web/src/terminal-links.ts b/apps/web/src/terminal-links.ts index f953f6dfee298..621ff0fc72479 100644 --- a/apps/web/src/terminal-links.ts +++ b/apps/web/src/terminal-links.ts @@ -1,11 +1,4 @@ -import { - formatFilePathPosition, - parseFileUrlHref, - resolvePathLinkTarget, - splitFilePathPosition, -} from "@t3tools/shared/fileLinks"; -import { safeDecodeURIComponent } from "@t3tools/shared/markdownLinks"; -import { stripSlashPrefixedWindowsDrive } from "@t3tools/shared/path"; +import { parseFileUrlHref, resolvePathLinkTarget } from "@t3tools/shared/fileLinks"; import { isMacPlatform } from "./lib/utils"; export type TerminalLinkKind = "url" | "path"; @@ -165,16 +158,15 @@ export function isTerminalUrl(value: string): boolean { /** * Explicit OSC 8 targets may be `file://` URIs; everything else is a path relative to `cwd`. * `hostname` is the terminal's environment's, which producers such as GNU `ls` put in the URI. + * A URI's path is the exact filename, so only its `#L12C3` hash is a position. */ export function resolveTerminalLinkTarget(text: string, cwd: string, hostname?: string): string { const fileUrl = /^file:\/\//iu.test(text) ? parseFileUrlHref(text, hostname) : null; if (!fileUrl) return resolvePathLinkTarget(text, cwd); - return formatFilePathPosition( - splitFilePathPosition( - stripSlashPrefixedWindowsDrive(safeDecodeURIComponent(fileUrl.path)), - fileUrl.hash, - ), - ); + // Keep URI identity until consumers decode the filename, separately from its position. + const uri = new URL(text); + if (!fileUrl.path.startsWith("\\")) uri.hostname = ""; + return uri.href; } export function collectWrappedTerminalLinkLine( diff --git a/packages/contracts/src/editor.ts b/packages/contracts/src/editor.ts index 28fd10d6e7b23..146007499d440 100644 --- a/packages/contracts/src/editor.ts +++ b/packages/contracts/src/editor.ts @@ -1,5 +1,5 @@ import * as Schema from "effect/Schema"; -import { TrimmedNonEmptyString } from "./baseSchemas.ts"; +import { PositiveInt, TrimmedNonEmptyString } from "./baseSchemas.ts"; export const EditorLaunchStyle = Schema.Literals(["direct-path", "goto", "line-column"]); export type EditorLaunchStyle = typeof EditorLaunchStyle.Type; @@ -157,6 +157,13 @@ export type FileManagerRevealKind = typeof FileManagerRevealKind.Type; export const LaunchEditorInput = Schema.Struct({ cwd: TrimmedNonEmptyString, editor: EditorId, + /** When present, cwd is an exact path; {} opens it without a position. Omit for diagnostic suffix parsing. */ + position: Schema.optional( + Schema.Struct({ + line: Schema.optional(PositiveInt), + column: Schema.optional(PositiveInt), + }), + ), /** Workspace the target belongs to; JetBrains IDEs open positioned files in this project. */ projectRoot: Schema.optional(TrimmedNonEmptyString), /** Only for file-manager; requires the server's shellRevealInFileManager capability. */ diff --git a/scripts/scenarios/terminal-file-uri-opens-its-exact-file.e2e.ts b/scripts/scenarios/terminal-file-uri-opens-its-exact-file.e2e.ts new file mode 100644 index 0000000000000..8c4265b50a796 --- /dev/null +++ b/scripts/scenarios/terminal-file-uri-opens-its-exact-file.e2e.ts @@ -0,0 +1,98 @@ +/// +// The DOM lib types the function this scenario runs in the page. +import { expect } from "e2e"; +import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; + +import { T3_HOME } from "../e2e.config.ts"; +import { runPromise, test, WEB } from "./t3.ts"; + +const EDITOR_ARGS = `${T3_HOME}/editor-args.txt`; + +/** The file's lines, or none while it does not exist. */ +const readLines = (file: string) => + runPromise( + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + if (!(yield* fs.exists(file))) return []; + return (yield* fs.readFileString(file)).trim().split("\n"); + }), + ); + +const removeEditorArgs = () => + runPromise( + Effect.flatMap(FileSystem.FileSystem, (fs) => fs.remove(EDITOR_ARGS, { force: true })), + ); + +// GNU ls, made hostless, for filenames that end like a position. +const listed = (name: string) => + `command ls -d --hyperlink=always --color=never -1 --quoting-style=literal -- '${name}' | sed -E 's@file://[^/]+/@file:///@'`; +// An opaque label, so only the URI can name the file. +const hashed = (uriPath: string) => + `printf '\\033]8;;file://%s\\033\\\\CLICKME\\033]8;;\\033\\\\\\n' "$PWD/${uriPath}"`; + +const LINKS = [ + { print: listed("report.txt:12-64"), opens: ["report.txt:12-64"] }, + { print: listed("zero.txt:0"), opens: ["zero.txt:0"] }, + { print: hashed("numeric.txt%3A12#L5"), opens: ["numeric.txt:12"] }, + { print: hashed("position.txt#L5C3"), opens: ["--goto", "position.txt:5:3"] }, + { print: listed("build:2026"), opens: ["--new-window", "build:2026"] }, +]; + +test( + "Ctrl+click on a terminal file URI opens the exact file it names, even one ending like a position", + WEB, + async ({ t3, app, browser, screen }) => { + await removeEditorArgs(); + const files: Record = { + "build:2026/keep.txt": "keep\n", + "numeric.txt:12:1:1/keep.txt": "decoy\n", + }; + for (const name of ["report.txt:12-64", "zero.txt:0", "numeric.txt:12", "position.txt"]) { + files[name] = `${name}\n`; + } + // Enough rows to scroll, so every row is the link. Scripts keep the typed commands short. + for (const [index, link] of LINKS.entries()) { + files[`print-${index}.sh`] = + `for i in $(seq 1 100); do ${link.print}; done; touch printed-${index}\n`; + } + const project = await t3.project("demo", files); + const thread = await t3.thread({ project, title: "List", messages: ["List it"] }); + await t3.open(thread); + + await screen.getByRole("button", "Toggle terminal drawer").tap(); + const input = screen.getByRole("textbox", "Terminal input"); + // The input mounts before fonts and WASM finish loading and its handlers are installed. + await expect(input).toHaveAttribute("data-workspace-focus-default", ""); + + for (const [index, { opens }] of LINKS.entries()) { + await removeEditorArgs(); + await input.focus(); + await browser.keyboard.type(`bash print-${index}.sh\n`); + await expect + .poll(() => readLines(`${project}/printed-${index}`), { timeout: 30_000 }) + .toEqual([""]); + await app.screenshot(`printed-${index}`); + + // The terminal draws on a canvas, so point at the first cells of a row near its middle. + const canvas = await browser.evaluate(() => { + const box = document + .querySelector('[aria-label="Terminal input"]') + ?.parentElement?.querySelector("canvas") + ?.getBoundingClientRect(); + return box ? { x: box.x, y: box.y, width: box.width, height: box.height } : null; + }); + if (!canvas) throw new Error("The terminal canvas is not on the page."); + // Alternate rows, or a quick second click on the same spot counts as a double-click. + await browser.mouse.move(canvas.x + 16, canvas.y + canvas.height / 2 - (index % 2) * 40); + await browser.keyboard.down("Control"); + await browser.mouse.down(); + await browser.mouse.up(); + await browser.keyboard.up("Control"); + + await expect + .poll(() => readLines(EDITOR_ARGS), { timeout: 15_000 }) + .toEqual(["--classic", ...opens.slice(0, -1), `${project}/${opens.at(-1)}`]); + } + }, +); From 507093152148c0d31c09bcd40e3ce4b5214a8557 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 15 Nov 2026 14:12:34 +0100 Subject: [PATCH 028/124] fix(mobile): cutting text in the iOS composer can be undone (#37158) Co-authored-by: Julius Marminge --- .../ios/T3ComposerEditorView.swift | 40 +++++++++++-------- .../t3-composer-editor/tests/ios/main.swift | 25 ++++++++++++ 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift b/apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift index 7bc50b694df5f..6b2e183892937 100644 --- a/apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift +++ b/apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift @@ -47,12 +47,12 @@ private enum ComposerEnterBehavior: String { } private final class ComposerTextAttachment: NSTextAttachment { - let source: String - let label: String + let token: ComposerTokenPayload + var source: String { token.source } + var label: String { token.label } - init(source: String, label: String, image: UIImage, size: CGSize, baselineOffset: CGFloat) { - self.source = source - self.label = label + init(token: ComposerTokenPayload, image: UIImage, size: CGSize, baselineOffset: CGFloat) { + self.token = token super.init(data: nil, ofType: nil) self.image = image bounds = CGRect(x: 0, y: baselineOffset, width: size.width, height: size.height) @@ -89,7 +89,6 @@ private final class ComposerTextView: UITextView { var onPasteContext: (([String: String]) -> Void)? var onPasteText: ((String, NSRange) -> Void)? var clipboardFragment = "" - var onAttributedMutation: (() -> Void)? var onSubmit: ((Bool) -> Void)? var isReadOnly = false var textPasteThresholdBytes = 0 @@ -363,9 +362,7 @@ private final class ComposerTextView: UITextView { return super.cut(sender) } copy(sender) - textStorage.replaceCharacters(in: selectedRange, with: "") - selectedRange = NSRange(location: selectedRange.location, length: 0) - onAttributedMutation?() + replaceDisplayRange(selectedRange) } func serializedText() -> String { @@ -564,9 +561,6 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro "selection": ["start": selection.start, "end": selection.end], ]) } - textView.onAttributedMutation = { [weak self] in - self?.emitTextChange() - } textView.onSubmit = { [weak self] alternate in self?.onComposerSubmit(["alternate": alternate]) } @@ -737,8 +731,10 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro if tokensJson != document.tokensJson { tokensJson = document.tokensJson let nextTokens = decode([ComposerTokenPayload].self, from: document.tokensJson) ?? [] - // Typing before a chip only moves its offsets; rebuilding then makes iOS autocorrect behind the caret. - tokensNeedRebuild = tokensNeedRebuild || !nextTokens.elementsEqual(tokens, by: { $0.rendersLike($1) }) + // Rebuild only when a chip on screen would look different: typing before a chip moves its + // offsets, and Undo restores the chip it removed. A rebuild there makes iOS autocorrect + // behind the caret, and replacing the text drops UIKit's Redo. + tokensNeedRebuild = tokensNeedRebuild || !nextTokens.elementsEqual(renderedChipTokens(), by: { $0.rendersLike($1) }) tokens = nextTokens } let previousSelection = sourceSelection() @@ -1052,8 +1048,7 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro font: chipFont ) let attachment = ComposerTextAttachment( - source: token.source, - label: token.label, + token: token, image: image, size: image.size, baselineOffset: chipFont.ascender - image.size.height @@ -1353,6 +1348,19 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro } } + private func renderedChipTokens() -> [ComposerTokenPayload] { + var rendered: [ComposerTokenPayload] = [] + textView.textStorage.enumerateAttribute( + .attachment, + in: NSRange(location: 0, length: textView.textStorage.length) + ) { value, _, _ in + if let attachment = value as? ComposerTextAttachment { + rendered.append(attachment.token) + } + } + return rendered + } + private func documentMatchesExpectedTokens() -> Bool { let source = value as NSString let expectedChips = tokens.compactMap { token -> (Int, String)? in diff --git a/apps/mobile/modules/t3-composer-editor/tests/ios/main.swift b/apps/mobile/modules/t3-composer-editor/tests/ios/main.swift index e9751eec843ac..6009daacb4046 100644 --- a/apps/mobile/modules/t3-composer-editor/tests/ios/main.swift +++ b/apps/mobile/modules/t3-composer-editor/tests/ios/main.swift @@ -146,6 +146,31 @@ do { check(view.editor.accessibilityLabel == "Ask anything", "VoiceOver label: \(String(describing: view.editor.accessibilityLabel))") } +// Cutting a chip is undoable: Undo restores the chip and tells the parent, Redo removes it again. +// The parent echoes each edit with its re-derived chips; those echoes must not rebuild the text. +do { + let view = makeEditor("hi @README.md ok", tokensJson: mention("README.md", at: 3)) + func echo() { + let revision = view.onComposerChange.payloads.last?["eventCount"] as? Int ?? 0 + let chips = view.text.contains("@README.md") ? mention("README.md", at: 3) : "[]" + view.setControlledDocumentJson(document(view.text, revision: revision, isNativeEcho: true, tokensJson: chips)) + } + view.editor.selectedRange = NSRange(location: 3, length: 2) + view.editor.cut(nil) + check(view.text == "hi ok", "Cut left the selection behind: \(view.text)") + check(view.onComposerChange.payloads.last?["value"] as? String == "hi ok", "Cut did not emit the new value") + echo() + view.editor.undoManager?.undo() + check(view.text == "hi @README.md ok", "Undo did not restore the cut text: \(view.text)") + let restored = view.chip(at: 3) + check(restored is ComposerTextAttachment, "Undo did not restore the cut chip") + check(view.onComposerChange.payloads.last?["value"] as? String == "hi @README.md ok", "Undo did not emit the restored value") + echo() + check(view.chip(at: 3) === restored, "The parent's echo of the restored chip rebuilt the text and dropped Redo") + view.editor.undoManager?.redo() + check(view.text == "hi ok", "Redo did not cut again: \(view.text)") +} + print("composer editor tests passed") private let dropEditor = ComposerTextView() From 449deb635ec0fc7735b42671afcdb7ee73eebf45 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 15 Nov 2026 05:22:34 -0800 Subject: [PATCH 029/124] fix(web): modified F3 reaches ncurses terminal bindings (#37211) --- apps/web/src/terminal/ghostty/core.test.ts | 28 ++++++++++++++++++++++ apps/web/src/terminal/ghostty/core.ts | 15 +++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/apps/web/src/terminal/ghostty/core.test.ts b/apps/web/src/terminal/ghostty/core.test.ts index 7958475ea54ef..c3f0f3aa0706a 100644 --- a/apps/web/src/terminal/ghostty/core.test.ts +++ b/apps/web/src/terminal/ghostty/core.test.ts @@ -165,6 +165,34 @@ describe("GhosttyTerminalCore snapshots", () => { }, ); + // smkx is what ncurses keypad(True) sends; a raw-mode parser such as Crossterm never does. + it.each([ + { mode: "", ctrlKey: true, expected: "\x1b[13;5~" }, + { mode: "\x1b[?1h\x1b=", ctrlKey: true, expected: "\x1b[1;5R" }, + { mode: "\x1b[?1h\x1b=", shiftKey: true, expected: "\x1b[1;2R" }, + { mode: "\x1b[?1h\x1b=\x1b[?1l\x1b>", ctrlKey: true, expected: "\x1b[13;5~" }, + { mode: "\x1b[?1h\x1b=\x1b[>1u", ctrlKey: true, expected: "\x1b[13;5~" }, + ])( + "encodes modified F3 as $expected after $mode", + async ({ mode, ctrlKey = false, shiftKey = false, expected }) => { + const core = await createCore(); + core.write(mode); + const event = { + code: "F3", + key: "F3", + repeat: false, + shiftKey, + ctrlKey, + altKey: false, + metaKey: false, + isComposing: false, + getModifierState: () => false, + } as unknown as KeyboardEvent; + + expect(core.encodeKey(event)).toBe(expected); + }, + ); + it.each(["", "\x1b[>1u"])("encodes macOS Option text with keyboard mode %j", async (mode) => { using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("MacIntel"); const core = await createCore(); diff --git a/apps/web/src/terminal/ghostty/core.ts b/apps/web/src/terminal/ghostty/core.ts index 119156cc56554..4bd4fd960951d 100644 --- a/apps/web/src/terminal/ghostty/core.ts +++ b/apps/web/src/terminal/ghostty/core.ts @@ -162,6 +162,11 @@ export interface GhosttyMouseInput { readonly anyButtonPressed: boolean; } +// Ghostty sends modified F3 as CSI 13;m ~ to stay clear of cursor position reports, but the PTY +// advertises xterm-256color, whose kf15/kf27/... are CSI 1;m R. Terminfo keys only apply in +// keypad-transmit mode (smkx), so that mode marks a terminfo reader; raw parsers never set it. +const MODIFIED_F3 = /^\x1b\[13;(\d+)~$/; + const decoder = new TextDecoder("utf-8", { ignoreBOM: true }); const encoder = new TextEncoder(); @@ -602,7 +607,9 @@ export class GhosttyTerminalCore { ); this.runtime.call("ghostty_wasm_free_usize", written); if (textPointer !== 0) this.runtime.free(textPointer, textBytes.length); - return encoded; + return MODIFIED_F3.test(encoded) && this.modeEnabled(66) && this.kittyKeyboardFlags() === 0 + ? encoded.replace(MODIFIED_F3, "\x1b[1;$1R") + : encoded; } encodePaste(data: string): string { @@ -1040,6 +1047,12 @@ export class GhosttyTerminalCore { } } + private kittyKeyboardFlags(): number { + this.runtime.bytes(this.scratch, 1)[0] = 0; + this.runtime.call("ghostty_terminal_get", this.terminal, 8, this.scratch); + return this.runtime.bytes(this.scratch, 1)[0]!; + } + /** Reads a DEC private mode; an unknown mode reads as off. */ private modeEnabled(mode: number): boolean { this.runtime.bytes(this.scratch, 1)[0] = 0; From d21d8d8b5df7ce2f07d27df05a410f9fcaf37f37 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 15 Nov 2026 05:30:00 -0800 Subject: [PATCH 030/124] fix(mobile): iOS video previews keep their contain fit instead of sometimes cropping (#37186) --- apps/mobile/src/components/VideoThumbnailImage.tsx | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/apps/mobile/src/components/VideoThumbnailImage.tsx b/apps/mobile/src/components/VideoThumbnailImage.tsx index f4ddb25a276fb..2a031b54b44f9 100644 --- a/apps/mobile/src/components/VideoThumbnailImage.tsx +++ b/apps/mobile/src/components/VideoThumbnailImage.tsx @@ -4,6 +4,7 @@ import { Atom, AsyncResult } from "effect/reactivity"; import { Image } from "expo-image"; import { useIsFocused } from "@react-navigation/native"; import type { VideoThumbnail } from "expo-video"; +import { useState } from "react"; import { StyleSheet } from "react-native"; import type { DraftComposerFileAttachment } from "../lib/composerImages"; @@ -49,13 +50,17 @@ export function VideoThumbnailImage(props: { const thumbnail = (loaded._tag === "Success" ? loaded.value : null) ?? cachedVideoThumbnail(cacheKey); - return thumbnail ? ( + // iOS draws a frame object the moment its prop is set, with the contentFit the view has then, + // and a later contentFit never refits it. Send the frame after mount so contentFit is set first. + const [laidOut, setLaidOut] = useState(false); + return ( setLaidOut(true)} + source={laidOut ? thumbnail : null} style={StyleSheet.absoluteFill} contentFit={props.contentFit ?? "cover"} recyclingKey={cacheKey} accessible={false} /> - ) : null; + ); } From 9c1dfbb5aaf3aab1a64a6bb4a98e06ad87fbdb81 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 15 Nov 2026 05:42:13 -0800 Subject: [PATCH 031/124] fix(server): delegate_task names the CLI version that hides a model (#37214) --- .../src/mcp/OrchestratorMcpService.test.ts | 9 ++++++ apps/server/src/mcp/OrchestratorMcpService.ts | 28 +++++++++++++------ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/apps/server/src/mcp/OrchestratorMcpService.test.ts b/apps/server/src/mcp/OrchestratorMcpService.test.ts index 40b5584e95ac4..1be59bc41184f 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.test.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.test.ts @@ -2512,6 +2512,10 @@ describe("OrchestratorMcpService provider resolution", () => { { slug: "claude-opus-4-1", isLegacy: true }, ...Array.from({ length: 9 }, (_, index) => ({ slug: `claude-opus-5-${index + 1}` })), ].map((model) => ({ ...model, name: model.slug, isCustom: false, capabilities: null })), + version: "2.1.302", + updateRequiredModels: [ + { slug: "claude-fable-5-2", name: "Claude Fable 5.2", minVersion: "2.1.321" }, + ], }, ]), adapterRegistryLayer([codexInstanceId]), @@ -2549,6 +2553,11 @@ describe("OrchestratorMcpService provider resolution", () => { "claude-opus-5-6, claude-opus-5-7, claude-opus-5-8 and 2 more; " + "orchestrator_capabilities with search finds them.", ); + + const updateRequired = yield* refuse("claude-fable-5-2"); + assert.equal(updateRequired.code, "model_unavailable"); + assert.include(updateRequired.message, "v2.1.302"); + assert.include(updateRequired.message, "2.1.321"); }).pipe(Effect.provide(layerOrchestratorService.pipe(Layer.provide(layerDependencies)))); }), ); diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.ts index cf5efb0ab333d..19a4132136338 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.ts @@ -395,16 +395,26 @@ const advertisedModel = (provider: ServerProvider, model: string) => candidate.aliases?.some((alias) => alias.toLowerCase() === model.toLowerCase()), ); +const withV = (version: string) => (version.startsWith("v") ? version : `v${version}`); + /** A provider that lists no models accepts any slug. */ -export const requireAdvertisedModel = (provider: ServerProvider, model: string) => - provider.models.length > 0 && advertisedModel(provider, model) === undefined - ? Effect.fail( - failure( - "model_unavailable", - `Model ${model} is not advertised by provider ${provider.instanceId}. ${advertisedModelsHint(provider.models, model)}`, - ), - ) - : Effect.void; +export const requireAdvertisedModel = (provider: ServerProvider, model: string) => { + if (provider.models.length === 0 || advertisedModel(provider, model) !== undefined) { + return Effect.void; + } + const updateRequired = provider.updateRequiredModels?.find( + (candidate) => candidate.slug === model, + ); + const detectedVersion = provider.installedVersion ?? provider.version; + return Effect.fail( + failure( + "model_unavailable", + updateRequired === undefined + ? `Model ${model} is not advertised by provider ${provider.instanceId}. ${advertisedModelsHint(provider.models, model)}` + : `Model ${model} needs provider ${provider.instanceId} at ${withV(updateRequired.minVersion)} or newer, but the detected CLI is ${detectedVersion === null ? "an unknown version" : withV(detectedVersion)}. Ask the user to update it or point the provider's binary path at a newer install.`, + ), + ); +}; function providerConstraints( provider: ServerProvider | undefined, From 0b489dada1f00db39c99074239c09c006f223aee Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 15 Nov 2026 14:47:56 +0100 Subject: [PATCH 032/124] =?UTF-8?q?fix(web):=20switching=20a=20draft?= =?UTF-8?q?=E2=80=99s=20machine=20keeps=20its=20selected=20models=20(#3720?= =?UTF-8?q?8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Grok 4.7 Co-authored-by: Julius Marminge --- ...seDraftEnvironmentRouting.browser.test.tsx | 105 ++++++++++++++++-- .../chat/useDraftEnvironmentRouting.ts | 31 ++++-- ...achine-switch-keeps-selected-models.e2e.ts | 86 ++++++++++++++ ...moved-environment-closes-its-toasts.e2e.ts | 67 +---------- scripts/scenarios/second-server.ts | 68 ++++++++++++ 5 files changed, 275 insertions(+), 82 deletions(-) create mode 100644 scripts/scenarios/machine-switch-keeps-selected-models.e2e.ts create mode 100644 scripts/scenarios/second-server.ts diff --git a/apps/web/src/components/chat/useDraftEnvironmentRouting.browser.test.tsx b/apps/web/src/components/chat/useDraftEnvironmentRouting.browser.test.tsx index 0dafee8167119..3b23abfcb7c9a 100644 --- a/apps/web/src/components/chat/useDraftEnvironmentRouting.browser.test.tsx +++ b/apps/web/src/components/chat/useDraftEnvironmentRouting.browser.test.tsx @@ -1,3 +1,5 @@ +import { useAtomValue } from "@effect/atom-react"; +import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; import { PrimaryConnectionTarget } from "@t3tools/client-runtime/connection"; import * as Option from "effect/Option"; import type { EnvironmentPresentation } from "../../state/environments"; @@ -20,26 +22,30 @@ import { expect, it, vi } from "vite-plus/test"; import { act, render } from "../../../test/render"; import { DraftId, useComposerDraftStore } from "../../composerDraftStore"; import { useDraftEnvironmentRouting } from "./useDraftEnvironmentRouting"; -import { appAtomRegistry } from "../../rpc/atomRegistry"; +import { appAtomRegistry, AppAtomRegistryProvider } from "../../rpc/atomRegistry"; import { draftFanoutSelectionsAtom } from "./chatViewSend"; const resources = vi.hoisted(() => ({ + projects: [] as EnvironmentProject[], + pickedEnvironmentId: null as EnvironmentId | null, refreshSelection: vi.fn<(ids: readonly EnvironmentId[]) => Promise>(), })); vi.mock("../../hooks/useLoadBalancedEnvironment", () => ({ useLoadBalancedEnvironment: () => ({ pending: false, failed: false, - environmentId: null, + environmentId: resources.pickedEnvironmentId, refresh: () => {}, refreshSelection: resources.refreshSelection, }), })); -vi.mock("../../state/entities", () => ({ useProjects: () => [] })); +vi.mock("../../state/entities", () => ({ useProjects: () => resources.projects })); vi.mock("../../hooks/useScratchProject", () => ({ useScratchProject: () => ({}) })); function setup() { resources.refreshSelection.mockReset(); + resources.projects = []; + resources.pickedEnvironmentId = null; const a = EnvironmentId.make("machine-a"); const b = EnvironmentId.make("machine-b"); const draftId = DraftId.make("fresh-routing-draft"); @@ -115,17 +121,40 @@ function setup() { return { a, b, draftId, input, store }; } -function Harness({ - input, - onPrepared, -}: { +interface HarnessProps { input: Parameters[0]; onPrepared?: (ready: boolean | null) => void; -}) { +} + +function Harness(props: HarnessProps) { + return ( + + + + ); +} + +function RoutingProbe({ input, onPrepared }: HarnessProps) { const routing = useDraftEnvironmentRouting(input); const draft = useComposerDraftStore((state) => state.getDraftSession(input.draftId!)); + const selections = useAtomValue( + draftFanoutSelectionsAtom( + scopedThreadKey(scopeThreadRef(draft!.environmentId, draft!.threadId)), + ), + ); return ( <> + + {selections?.map((selection) => selection.model).join(", ") ?? "single model"} + + {input.logicalProjectEnvironments.map(({ environmentId }) => ( + + ))} {draft?.environmentId}