From b35c9702f940ef1395b6cadb1215c51dd81a95a7 Mon Sep 17 00:00:00 2001 From: shivam <91240327+shivamhwp@users.noreply.github.com> Date: Sun, 15 Nov 2026 13:31:25 +0530 Subject: [PATCH 001/446] fix(acp): optional text questions can be left blank (#36997) Co-authored-by: Claude Haiku 5.5 --- .../Adapters/AcpAdapterV2.test.ts | 20 +++++++++++++++++-- .../src/state/pendingUserInput.test.ts | 12 +++++++++++ .../src/state/pendingUserInput.ts | 4 ++++ .../src/server/elicitationForm.test.ts | 7 +++++++ .../src/server/elicitationForm.ts | 1 + 5 files changed, 42 insertions(+), 2 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts index 3f5423a848e16..7643610fd850c 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts @@ -1961,7 +1961,15 @@ describe("AcpAdapterV2", () => { scenario: "OMP unrelated form uses generic mapping", settings: OMP_SETTINGS, schema: { type: "object", properties: { name: { type: "string" } } }, - questions: [{ id: "name", header: "Question 1", question: "Your answer?", options: [] }], + questions: [ + { + id: "name", + header: "Question 1", + question: "Your answer?", + options: [], + required: false, + }, + ], answers: { name: "Ada" }, result: { action: "accept", content: { name: "Ada" } }, }, @@ -2004,7 +2012,15 @@ describe("AcpAdapterV2", () => { { scenario: "falls back for an unrelated form", schema: { type: "object", properties: { name: { type: "string" } } }, - questions: [{ id: "name", header: "Question 1", question: "Your answer?", options: [] }], + questions: [ + { + id: "name", + header: "Question 1", + question: "Your answer?", + options: [], + required: false, + }, + ], answers: { name: "Ada" }, result: { action: "accept", content: { name: "Ada" } }, }, diff --git a/packages/client-runtime/src/state/pendingUserInput.test.ts b/packages/client-runtime/src/state/pendingUserInput.test.ts index 5baaaac41c83a..d9d4ddbddb6bc 100644 --- a/packages/client-runtime/src/state/pendingUserInput.test.ts +++ b/packages/client-runtime/src/state/pendingUserInput.test.ts @@ -83,6 +83,18 @@ describe("resolvePendingUserInputAnswer", () => { ).toBe("Node.js"); }); + it("lets an optional text question stay blank", () => { + const textQuestion = { id: "notes", options: [] }; + expect(resolvePendingUserInputAnswer({ ...textQuestion, required: false }, undefined)).toBe(""); + expect( + resolvePendingUserInputAnswer({ ...textQuestion, required: false }, { customAnswer: " " }), + ).toBe(""); + expect(resolvePendingUserInputAnswer(textQuestion, { customAnswer: " " })).toBeNull(); + expect( + resolvePendingUserInputAnswer({ ...textQuestion, required: true }, { customAnswer: "" }), + ).toBeNull(); + }); + it("accepts ready attachment-only answers while preserving selected options", () => { expect(resolvePendingUserInputAnswer(singleSelectQuestion, { attachmentCount: 1 })).toBe(""); expect( diff --git a/packages/client-runtime/src/state/pendingUserInput.ts b/packages/client-runtime/src/state/pendingUserInput.ts index 520fb626b5936..6569d7a5db77e 100644 --- a/packages/client-runtime/src/state/pendingUserInput.ts +++ b/packages/client-runtime/src/state/pendingUserInput.ts @@ -5,6 +5,7 @@ export interface PendingUserInputQuestion { readonly multiSelect?: boolean | undefined; readonly allowCustomAnswer?: boolean | undefined; readonly initialAnswer?: string | undefined; + readonly required?: boolean | undefined; } export interface PendingUserInputDraftAnswer { @@ -71,6 +72,9 @@ export function resolvePendingUserInputAnswer( if (customAnswer !== null && customAnswer.length > 0) { return customAnswer; } + if (question.required === false && question.options.length === 0) { + return ""; + } const selectedOptionValues = normalizeSelectedOptionValues(question, draft?.selectedOptionValues); if (question.multiSelect) { diff --git a/packages/provider-acp/src/server/elicitationForm.test.ts b/packages/provider-acp/src/server/elicitationForm.test.ts index 6e82e70efdfcf..b288f8603132d 100644 --- a/packages/provider-acp/src/server/elicitationForm.test.ts +++ b/packages/provider-acp/src/server/elicitationForm.test.ts @@ -92,6 +92,13 @@ describe("parseElicitationForm", () => { "Databases?", ); assert(multi !== undefined); + assert.deepStrictEqual( + multi.questions.map((question) => [question.id, question.required]), + [ + ["q0", undefined], + ["q0__other", false], + ], + ); assert.deepStrictEqual(multi.content({ q0: ["Postgres"], q0__other: "SQLite" }), { q0: ["Postgres"], q0__other: "SQLite", diff --git a/packages/provider-acp/src/server/elicitationForm.ts b/packages/provider-acp/src/server/elicitationForm.ts index 40b2b478f9f99..c2c9bc019a41b 100644 --- a/packages/provider-acp/src/server/elicitationForm.ts +++ b/packages/provider-acp/src/server/elicitationForm.ts @@ -230,6 +230,7 @@ export function parseElicitationForm( ? {} : { allowCustomAnswer: false }), ...(kind === "multiChoice" ? { multiSelect: true } : {}), + ...(kind === "text" && !required.has(id) ? { required: false } : {}), }); } return { From 4ab3b984112f6f1b9544ff4d92dd0a6f2b2005bf Mon Sep 17 00:00:00 2001 From: Jason Lernerman <43508353+u9g@users.noreply.github.com> Date: Sun, 15 Nov 2026 00:01:42 -0800 Subject: [PATCH 002/446] fix(shared): MCP apps that fall back to a blob: frame no longer render blank (#32129) --- packages/shared/src/mcpApp.test.ts | 7 ++++++- packages/shared/src/mcpApp.ts | 5 ++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/shared/src/mcpApp.test.ts b/packages/shared/src/mcpApp.test.ts index e0d902c350566..9f4fb151398d7 100644 --- a/packages/shared/src/mcpApp.test.ts +++ b/packages/shared/src/mcpApp.test.ts @@ -18,10 +18,15 @@ describe("mcpAppContentSecurityPolicy", () => { const policy = mcpAppContentSecurityPolicy(undefined); expect(policy).toContain("default-src 'none'"); expect(policy).toContain("connect-src 'none'"); - expect(policy).toContain("frame-src 'none'"); + expect(policy).toContain("frame-src blob:;"); expect(policy).not.toContain("'self'"); }); + it("lets apps frame their own blob: documents alongside declared frame origins", () => { + const policy = mcpAppContentSecurityPolicy({ frameDomains: ["https://embed.test"] }); + expect(policy).toContain("frame-src blob: https://embed.test;"); + }); + it("allows exactly the declared origins", () => { const policy = mcpAppContentSecurityPolicy({ connectDomains: ["https://api.weather.test"], diff --git a/packages/shared/src/mcpApp.ts b/packages/shared/src/mcpApp.ts index 40b3975ebd645..7c3afd221365d 100644 --- a/packages/shared/src/mcpApp.ts +++ b/packages/shared/src/mcpApp.ts @@ -177,7 +177,10 @@ export function mcpAppContentSecurityPolicy(csp: McpAppCsp | undefined): string `font-src data: ${resources}`.trim(), `media-src data: blob: ${resources}`.trim(), `connect-src ${list(csp?.connectDomains, "'none'")}`, - `frame-src ${list(csp?.frameDomains, "'none'")}`, + // A blob: frame holds HTML the app already has and inherits its opaque + // origin, sandbox and policy. Apps whose own embed can't load from an + // opaque origin fall back to one. + `frame-src blob: ${csp?.frameDomains?.join(" ") ?? ""}`.trim(), "object-src 'none'", `base-uri ${list(csp?.baseUriDomains, "'none'")}`, "form-action 'none'", From 2045004096340f58cd72b31708f5aec7e63f29d0 Mon Sep 17 00:00:00 2001 From: luke <97411838+luke2x@users.noreply.github.com> Date: Sun, 15 Nov 2026 08:02:00 +0000 Subject: [PATCH 003/446] fix(shared): keep coalescing workers alive when processors throw (#32178) --- .../shared/src/KeyedCoalescingWorker.test.ts | 25 +++++++++++++++++++ packages/shared/src/KeyedCoalescingWorker.ts | 7 +++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/packages/shared/src/KeyedCoalescingWorker.test.ts b/packages/shared/src/KeyedCoalescingWorker.test.ts index e00834ddae058..07bd670a32dca 100644 --- a/packages/shared/src/KeyedCoalescingWorker.test.ts +++ b/packages/shared/src/KeyedCoalescingWorker.test.ts @@ -179,4 +179,29 @@ describe("makeKeyedCoalescingWorker", () => { expect(processed).toEqual(["terminal-1:first", "terminal-2:quiet", "terminal-1:second"]); }), ); + it.effect( + "keeps processing and releases the key when a processor throws before returning an effect", + () => + Effect.scoped( + Effect.gen(function* () { + const processed: string[] = []; + const worker = yield* makeKeyedCoalescingWorker({ + merge: (_current, next) => next, + process: (key, value) => { + if (value === "broken") throw new Error("processor construction failed"); + return Effect.sync(() => { + processed.push(`${key}:${value}`); + }); + }, + }); + yield* worker.enqueue("first", "broken"); + yield* worker.enqueue("second", "healthy"); + yield* worker.drainKey("first"); + yield* worker.drainKey("second"); + yield* worker.enqueue("first", "recovered"); + yield* worker.drainKey("first"); + expect(processed).toEqual(["second:healthy", "first:recovered"]); + }), + ), + ); }); diff --git a/packages/shared/src/KeyedCoalescingWorker.ts b/packages/shared/src/KeyedCoalescingWorker.ts index e36859367a9c2..56e71d0e028a7 100644 --- a/packages/shared/src/KeyedCoalescingWorker.ts +++ b/packages/shared/src/KeyedCoalescingWorker.ts @@ -70,9 +70,10 @@ export const makeKeyedCoalescingWorker = Effect.fn( Effect.flatMap((item) => item === null ? Effect.void - : options - .process(item.key, item.value) - .pipe(Effect.ignoreCause, Effect.ensuring(completeKey(item.key))), + : Effect.suspend(() => options.process(item.key, item.value)).pipe( + Effect.ignoreCause, + Effect.ensuring(completeKey(item.key)), + ), ), Effect.forever, Effect.forkScoped, From 96188376948545593a50c3a42d1aaebf9e7c7a97 Mon Sep 17 00:00:00 2001 From: Alex Southwell Date: Sun, 15 Nov 2026 19:02:17 +1100 Subject: [PATCH 004/446] fix(server): an empty Antigravity MCP config still allows text generation (#36496) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../AntigravityTextGeneration.test.ts | 1 + .../AntigravityTextGeneration.ts | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/apps/server/src/textGeneration/AntigravityTextGeneration.test.ts b/apps/server/src/textGeneration/AntigravityTextGeneration.test.ts index 783417693c722..5a410dbf342f7 100644 --- a/apps/server/src/textGeneration/AntigravityTextGeneration.test.ts +++ b/apps/server/src/textGeneration/AntigravityTextGeneration.test.ts @@ -549,6 +549,7 @@ it.layer(NodeServices.layer)("AntigravityTextGeneration", (it) => { { name: "hooks.json", value: "{}", available: true }, { name: "hooks.json", value: '{"hooks":{}}', available: true }, { name: "mcp_config.json", value: '{"mcpServers":{}}', available: true }, + { name: "mcp_config.json", value: "", available: true }, ])("checks $name before starting a helper, available=$available", ({ name, value, available }) => Effect.gen(function* () { const fixture = yield* makeFixture(); diff --git a/apps/server/src/textGeneration/AntigravityTextGeneration.ts b/apps/server/src/textGeneration/AntigravityTextGeneration.ts index b7e7cfaeb40e8..b67bad85faf03 100644 --- a/apps/server/src/textGeneration/AntigravityTextGeneration.ts +++ b/apps/server/src/textGeneration/AntigravityTextGeneration.ts @@ -79,14 +79,20 @@ export const isAntigravityTextGenerationAvailable = Effect.fn( if (info.type !== "File" || info.size > 64_000n) { return false; } + // CLI 1.5.8 writes a 0-byte mcp_config.json before any server exists. const empty = yield* fs.readFileString(configurationPath).pipe( - Effect.flatMap(decodeConfiguration), - Effect.flatMap((configuration) => - decodeConfigurationObject( - configuration[name === "hooks.json" ? "hooks" : "mcpServers"] ?? configuration, - ), + Effect.flatMap((text) => + text.trim().length === 0 + ? Effect.succeed(true) + : decodeConfiguration(text).pipe( + Effect.flatMap((configuration) => + decodeConfigurationObject( + configuration[name === "hooks.json" ? "hooks" : "mcpServers"] ?? configuration, + ), + ), + Effect.map((configuration) => Object.keys(configuration).length === 0), + ), ), - Effect.map((configuration) => Object.keys(configuration).length === 0), Effect.orElseSucceed(() => false), ); if (!empty) return false; From 570640b1605e7f52cbaeed41940961b0dc946b51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mahir=20Taha=20=C3=96zdin?= Date: Sun, 15 Nov 2026 11:02:36 +0300 Subject: [PATCH 005/446] perf(server): active thread reads filter on the archived_at column (#28992) --- .../orchestration-v2/ProjectionStore.test.ts | 33 ++++++++++++------- .../src/orchestration-v2/ProjectionStore.ts | 8 ++--- .../src/orchestration-v2/ThreadSearch.test.ts | 12 ++++++- .../src/orchestration-v2/ThreadSearch.ts | 4 +-- 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts index 2f924709071b5..ea95f8aafca1f 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts @@ -8470,21 +8470,32 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { const [originalRow] = yield* sql<{ payload_json: string; }>`SELECT payload_json FROM orchestration_v2_projection_threads WHERE thread_id = ${threadId}`; - for (const [field, value] of [ - ["archivedAt", DateTime.formatIso(now)], - ["settledOverride", "settled"], - ]) { - yield* sql`UPDATE orchestration_v2_projection_threads - SET payload_json = json_set(payload_json, ${`$.${field}`}, ${value}) - WHERE thread_id = ${threadId}`; - assert.isUndefined( - (yield* store.getLimitRecoveryCandidates({ now, autoResume: true, snooze: false })).find( + const limitThread = (yield* store.getThreadProjection(threadId)).thread; + for (const archivedAt of [now, null]) { + yield* store.apply({ + id: EventId.make(`event:limit-shell:archived:${archivedAt !== null}`), + type: archivedAt === null ? "thread.unarchived" : "thread.archived", + threadId, + occurredAt: now, + payload: { ...limitThread, archivedAt }, + }); + assert.strictEqual( + (yield* store.getLimitRecoveryCandidates({ now, autoResume: true, snooze: false })).some( (row) => row.id === threadId, ), + archivedAt === null, ); - yield* sql`UPDATE orchestration_v2_projection_threads - SET payload_json = ${originalRow!.payload_json} WHERE thread_id = ${threadId}`; } + yield* sql`UPDATE orchestration_v2_projection_threads + SET payload_json = json_set(payload_json, '$.settledOverride', 'settled') + WHERE thread_id = ${threadId}`; + assert.isUndefined( + (yield* store.getLimitRecoveryCandidates({ now, autoResume: true, snooze: false })).find( + (row) => row.id === threadId, + ), + ); + yield* sql`UPDATE orchestration_v2_projection_threads + SET payload_json = ${originalRow!.payload_json} WHERE thread_id = ${threadId}`; yield* sql`UPDATE orchestration_v2_projection_threads SET deleted_at = ${DateTime.formatIso(now)} WHERE thread_id = ${threadId}`; assert.isUndefined( (yield* store.getLimitRecoveryCandidates({ now, autoResume: true, snooze: false })).find( diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts index 96e8883a0ba7c..a3fcd139fd5b0 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -4666,7 +4666,7 @@ export const layer: Layer.Layer = WHERE failure.type = 'error' AND failure.status = 'failed' AND json_extract(failure.payload_json, '$.failure.class') = 'usage_limit' ) - AND json_extract(t.payload_json, '$.archivedAt') IS NULL + AND t.archived_at IS NULL AND json_extract(t.payload_json, '$.settledOverride') IS NOT 'settled' AND json_extract(item.payload_json, '$.failure.class') = 'usage_limit' AND json_extract(item.payload_json, '$.failure.resetAt') IS NOT NULL @@ -8459,7 +8459,7 @@ export const layer: Layer.Layer = SELECT thread_id, json_extract(payload_json, '$.worktreePath') AS worktree_path FROM orchestration_v2_projection_threads WHERE deleted_at IS NULL - ${options?.includeArchived === true ? sql`` : sql`AND json_extract(payload_json, '$.archivedAt') IS NULL`} + ${options?.includeArchived === true ? sql`` : sql`AND archived_at IS NULL`} AND json_extract(payload_json, '$.worktreePath') IS NOT NULL `.pipe( Effect.map((rows) => @@ -8911,7 +8911,7 @@ export const layer: Layer.Layer = SELECT thread_id FROM orchestration_v2_projection_threads WHERE deleted_at IS NULL - AND json_extract(payload_json, '$.archivedAt') IS NULL + AND archived_at IS NULL AND json_extract(payload_json, '$.lineage.relationshipToParent') = 'subagent' `; return rows.map((row) => ThreadId.make(row.thread_id)); @@ -9131,7 +9131,7 @@ export const layer: Layer.Layer = json_extract(t.payload_json, '$.settledAt') AS settled_at FROM orchestration_v2_projection_threads t WHERE t.deleted_at IS NULL - AND json_extract(t.payload_json, '$.archivedAt') IS NULL + AND t.archived_at IS NULL AND json_extract(t.payload_json, '$.lineage.relationshipToParent') IS NOT 'subagent' AND json_extract(t.payload_json, '$.lineage.sideChat') IS NOT 1 AND json_extract(t.payload_json, '$.settledOverride') = 'settled' diff --git a/apps/server/src/orchestration-v2/ThreadSearch.test.ts b/apps/server/src/orchestration-v2/ThreadSearch.test.ts index 7518705f5a5ac..399224533089d 100644 --- a/apps/server/src/orchestration-v2/ThreadSearch.test.ts +++ b/apps/server/src/orchestration-v2/ThreadSearch.test.ts @@ -1149,12 +1149,22 @@ it.layer(layerTest)("ThreadSearch.searchOlderSettled", (it) => { thread(id("elsewhere"), other, { title: "Kestrel nest", settledAt: old }), thread(id("note"), project, { title: "Unrelated", settledAt: old }), thread(id("new-note"), project, { title: "Unrelated", settledAt: at(50) }), + thread(id("archived"), project, { + title: "Kestrel nest", + settledAt: old, + archivedAt: old, + }), + thread(id("archived-note"), project, { + title: "Unrelated", + settledAt: old, + archivedAt: old, + }), ], projections.apply, { discard: true }, ); const before = DateTime.formatIso(at(30)); - const messageThreadIds = [id("new-note"), id("note"), id("old")]; + const messageThreadIds = [id("new-note"), id("note"), id("archived-note"), id("old")]; assert.sameMembers( [...(yield* search.searchOlderSettled({ query: "kestrel", before, messageThreadIds }))], [id("old"), id("elsewhere"), id("note")], diff --git a/apps/server/src/orchestration-v2/ThreadSearch.ts b/apps/server/src/orchestration-v2/ThreadSearch.ts index f01fb6b0f35eb..3ad48b26f172d 100644 --- a/apps/server/src/orchestration-v2/ThreadSearch.ts +++ b/apps/server/src/orchestration-v2/ThreadSearch.ts @@ -450,7 +450,7 @@ export const make = Effect.gen(function* () { INNER JOIN projection_projects AS projects ON projects.project_id = threads.project_id WHERE threads.deleted_at IS NULL - AND json_extract(threads.payload_json, '$.archivedAt') IS NULL + AND threads.archived_at IS NULL AND projects.deleted_at IS NULL AND json_extract(threads.payload_json, '$.lineage.relationshipToParent') IS NOT 'subagent' @@ -474,7 +474,7 @@ export const make = Effect.gen(function* () { FROM orchestration_v2_projection_threads AS threads WHERE threads.thread_id IN (SELECT value FROM json_each(${threadIds})) AND threads.deleted_at IS NULL - AND json_extract(threads.payload_json, '$.archivedAt') IS NULL + AND threads.archived_at IS NULL AND json_extract(threads.payload_json, '$.lineage.relationshipToParent') IS NOT 'subagent' AND json_extract(threads.payload_json, '$.lineage.sideChat') IS NOT 1 From 64440877793d2fdb8a559347b14475b504d6f306 Mon Sep 17 00:00:00 2001 From: Hieu Le <78847613+hieu-lee@users.noreply.github.com> Date: Sun, 15 Nov 2026 09:02:54 +0100 Subject: [PATCH 006/446] perf(resource-monitor): look up external process identities by PID (#31202) --- native/resource-monitor/src/main.rs | 55 +++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/native/resource-monitor/src/main.rs b/native/resource-monitor/src/main.rs index ba60bbc7527e6..ffcc821a67c67 100644 --- a/native/resource-monitor/src/main.rs +++ b/native/resource-monitor/src/main.rs @@ -463,13 +463,12 @@ impl Collector { .external_processes .iter() .filter_map(|(pid, expected_start_time_ms)| { - let (_, _, actual_start_time_ms) = rows - .iter() - .find(|(candidate_pid, _, _)| candidate_pid == pid)?; - matches_external_identity(*actual_start_time_ms, *expected_start_time_ms).then_some( + let process = self.system.process(Pid::from_u32(*pid))?; + let actual_start_time_ms = process.start_time().saturating_mul(1_000); + matches_external_identity(actual_start_time_ms, *expected_start_time_ms).then_some( ExternalProcess { pid: *pid, - start_time_ms: Some(*actual_start_time_ms), + start_time_ms: Some(actual_start_time_ms), }, ) }) @@ -1048,10 +1047,19 @@ mod tests { #[test] fn validates_external_process_start_identity() { - assert!(matches_external_identity(10_000, None)); - assert!(matches_external_identity(10_000, Some(10_999))); - assert!(!matches_external_identity(10_000, Some(11_000))); - assert!(!matches_external_identity(10_000, Some(9_999))); + for (actual, expected, matches) in [ + (10_000, None, true), + (10_000, Some(10_000), true), + (10_000, Some(10_999), true), + (10_000, Some(11_000), false), + (10_000, Some(9_999), false), + (0, Some(999), true), + (0, Some(1_000), false), + (u64::MAX, None, true), + (u64::MAX, Some(u64::MAX), false), + ] { + assert_eq!(matches_external_identity(actual, expected), matches); + } } #[test] @@ -1094,7 +1102,13 @@ mod tests { assert_eq!(unselected.memory(), 0); assert_eq!(unselected.virtual_memory(), 0); - config.external_processes.insert(pid.as_u32(), None); + config.external_processes = [ + (pid.as_u32(), Some(0)), + (u32::MAX, None), + (pid.as_u32(), None), + ] + .into_iter() + .collect(); let snapshot = collector.sample(&config, None); let selected = snapshot .processes @@ -1104,6 +1118,27 @@ mod tests { assert!(!selected.command.is_empty()); assert!(selected.resident_bytes > 0); assert!(selected.start_time_ms > 0); + assert_eq!( + snapshot + .external_processes + .iter() + .map(|process| (process.pid, process.start_time_ms)) + .collect::>(), + vec![(pid.as_u32(), Some(selected.start_time_ms))], + ); + + config.root_pid = pid.as_u32(); + config.external_processes.insert(pid.as_u32(), Some(0)); + let snapshot = collector.sample(&config, None); + assert!(snapshot.external_processes.is_empty()); + assert_eq!( + snapshot + .processes + .iter() + .filter(|process| process.pid == pid.as_u32()) + .count(), + 1, + ); let retained = collector .system From 3ee4aa62ec0ed16375e3f28c8ad2138a44ebaa88 Mon Sep 17 00:00:00 2001 From: Darsh Mahadevia <160497999+darshmahadevia@users.noreply.github.com> Date: Sun, 15 Nov 2026 03:03:14 -0500 Subject: [PATCH 007/446] fix(server): accept ASCII whitespace in base64 image payloads (#31798) --- apps/server/src/imageMime.test.ts | 20 +++++++++++++------- apps/server/src/imageMime.ts | 2 +- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/apps/server/src/imageMime.test.ts b/apps/server/src/imageMime.test.ts index 5d427b957265a..87fb46d912939 100644 --- a/apps/server/src/imageMime.test.ts +++ b/apps/server/src/imageMime.test.ts @@ -10,16 +10,22 @@ describe("imageMime", () => { }); }); - it("parses base64 data URL with spaces in payload", () => { - expect(parseBase64DataUrl("data:image/png;base64,SGVs bG8=\n")).toEqual({ - mimeType: "image/png", - base64: "SGVsbG8=", - }); - }); + it.each(["\t", "\n", "\f", "\r", " "])( + "parses base64 data URL with ASCII whitespace %j", + (space) => { + expect(parseBase64DataUrl(`data:image/png;base64,SGVs${space}bG8=\n`)).toEqual({ + mimeType: "image/png", + base64: "SGVsbG8=", + }); + }, + ); it("rejects payload with characters outside the base64 alphabet", () => { expect(parseBase64DataUrl("data:image/png;base64,SGVs!bG8=")).toBeNull(); expect(parseBase64DataUrl("data:image/png;base64,SGVs,bG8=")).toBeNull(); + for (const space of ["\v", "\u00a0", "\u2003"]) { + expect(parseBase64DataUrl(`data:image/png;base64,SGVs${space}bG8=`)).toBeNull(); + } }); // regression: #3952 image upload crashed dispatchCommand with a stack overflow @@ -43,7 +49,7 @@ describe("imageMime", () => { it("rejects empty and whitespace-only payloads", () => { expect(parseBase64DataUrl("data:image/png;base64,")).toBeNull(); - expect(parseBase64DataUrl("data:image/png;base64, \r\n")).toBeNull(); + expect(parseBase64DataUrl("data:image/png;base64, \t\r\n\f")).toBeNull(); }); it("parses a case-insensitive scheme and mime type", () => { diff --git a/apps/server/src/imageMime.ts b/apps/server/src/imageMime.ts index 338bbd62ce592..4c985dccb8235 100644 --- a/apps/server/src/imageMime.ts +++ b/apps/server/src/imageMime.ts @@ -42,7 +42,7 @@ function isBase64Char(code: number): boolean { } function isBase64Whitespace(code: number): boolean { - return code === 0x0d || code === 0x0a || code === 0x20; + return code === 0x09 || code === 0x0a || code === 0x0c || code === 0x0d || code === 0x20; } // V8 regexes use the JS stack; scanning image payloads can overflow a deep fiber stack. From 8c80003d68b0d94cedb898210c09823b77533d6d Mon Sep 17 00:00:00 2001 From: Andrei <108398860+Yuruzuu@users.noreply.github.com> Date: Sun, 15 Nov 2026 16:12:38 +0800 Subject: [PATCH 008/446] perf(server): Codex skills for four workspaces load in a median 138 ms instead of 1,657 ms (12x) (#33972) Co-authored-by: maria-rcks Co-authored-by: Julius Marminge --- .../server/src/provider/CodexProvider.test.ts | 138 ++++++++++++++++++ apps/server/src/provider/CodexProvider.ts | 48 ++++++ .../src/provider/Drivers/CodexDriver.ts | 13 +- 3 files changed, 192 insertions(+), 7 deletions(-) diff --git a/apps/server/src/provider/CodexProvider.test.ts b/apps/server/src/provider/CodexProvider.test.ts index ad15bf0896eb2..a70ea15a73570 100644 --- a/apps/server/src/provider/CodexProvider.test.ts +++ b/apps/server/src/provider/CodexProvider.test.ts @@ -1,12 +1,150 @@ import { assert, it } from "@effect/vitest"; +import * as Cause from "effect/Cause"; +import * as Deferred from "effect/Deferred"; +import * as Effect from "effect/Effect"; +import * as Exit from "effect/Exit"; +import * as Queue from "effect/Queue"; +import * as Scope from "effect/Scope"; +import * as Sink from "effect/Sink"; +import * as Stream from "effect/Stream"; +import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; +import * as TestClock from "effect/testing/TestClock"; +import * as HostProcess from "@t3tools/shared/HostProcess"; import { applyPreferredCodexDefaultModel, codexCustomEndpoint, codexRoutesSpendNativeQuota, + makeCodexSkillsProbe, mapCodexModelCapabilities, } from "./CodexProvider.ts"; +function makeFakeCodexAppServers() { + const helpers: Array<{ + readonly codexHome: unknown; + readonly crash: Effect.Effect; + readonly requests: Array; + closed: boolean; + }> = []; + const spawner = ChildProcessSpawner.make((command) => + Effect.gen(function* () { + const output = yield* Queue.unbounded(); + const exited = yield* Deferred.make(); + const crash = Deferred.succeed(exited, ChildProcessSpawner.ExitCode(1)).pipe( + Effect.andThen(Queue.end(output)), + Effect.asVoid, + ); + const helper = { + codexHome: command._tag === "StandardCommand" ? command.options.env?.CODEX_HOME : undefined, + crash, + requests: [] as Array, + closed: false, + }; + helpers.push(helper); + yield* Effect.addFinalizer(() => Effect.sync(() => (helper.closed = true))); + const respond = (id: unknown, result: unknown) => + Queue.offer(output, new TextEncoder().encode(`${JSON.stringify({ id, result })}\n`)); + return ChildProcessSpawner.makeHandle({ + pid: ChildProcessSpawner.ProcessId(900_000_000 + helpers.length), + exitCode: Deferred.await(exited), + isRunning: Effect.map(Deferred.isDone(exited), (done) => !done), + kill: () => crash, + unref: Effect.succeed(Effect.void), + stdin: Sink.forEach((chunk: string | Uint8Array) => + Effect.forEach( + (typeof chunk === "string" ? chunk : new TextDecoder().decode(chunk)) + .split("\n") + .filter(Boolean), + (line) => { + const frame = JSON.parse(line) as { + readonly id?: number; + readonly method: string; + readonly params?: { readonly cwds: ReadonlyArray }; + }; + if (frame.id === undefined) return Effect.void; + if (frame.method === "initialize") { + return respond(frame.id, { + codexHome: "/codex-home", + platformFamily: "unix", + platformOs: "linux", + userAgent: "codex/0.0.0", + }); + } + helper.requests.push(frame.params); + return respond(frame.id, { + data: (frame.params?.cwds ?? []).map((cwd) => ({ + cwd, + errors: [], + skills: [ + { + name: `skill-in-${cwd}`, + path: `${cwd}/SKILL.md`, + enabled: true, + description: "", + scope: "repo", + }, + ], + })), + }); + }, + { discard: true }, + ), + ), + stdout: Stream.fromQueue(output), + stderr: Stream.empty, + all: Stream.empty, + getInputFd: () => Sink.drain, + getOutputFd: () => Stream.empty, + }); + }), + ); + return { helpers, spawner }; +} + +it.effect("reuses one Codex skills helper per launch until it idles out", () => + Effect.gen(function* () { + const { helpers, spawner } = makeFakeCodexAppServers(); + const providerScope = yield* Scope.make(); + const probe = yield* makeCodexSkillsProbe().pipe( + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), + Scope.provide(providerScope), + ); + const read = (cwd: string, homePath = "/home-a") => + probe({ binaryPath: "codex", homePath, cwd, environment: {} }).pipe( + Effect.map((skills) => skills.map((skill) => skill.name)), + ); + + assert.deepStrictEqual(yield* read("/alpha"), ["skill-in-/alpha"]); + assert.deepStrictEqual(yield* read("/beta"), ["skill-in-/beta"]); + assert.strictEqual(helpers.length, 1); + assert.deepStrictEqual(helpers[0]!.requests, [ + { cwds: ["/alpha"], forceReload: true }, + { cwds: ["/beta"], forceReload: true }, + ]); + + // Another home is another launch identity; the previous helper retires at once. + assert.deepStrictEqual(yield* read("/alpha", "/home-b"), ["skill-in-/alpha"]); + assert.strictEqual(helpers.length, 2); + assert.strictEqual(helpers[0]!.closed, true); + assert.strictEqual(helpers[1]!.codexHome, "/home-b"); + + // A helper that exits is replaced within the same read. + yield* helpers[1]!.crash; + assert.deepStrictEqual(yield* read("/beta", "/home-b"), ["skill-in-/beta"]); + assert.strictEqual(helpers.length, 3); + + yield* TestClock.adjust("29 seconds"); + assert.strictEqual(helpers[2]!.closed, false); + yield* TestClock.adjust("1 second"); + assert.strictEqual(helpers[2]!.closed, true); + assert.deepStrictEqual(yield* read("/alpha", "/home-b"), ["skill-in-/alpha"]); + assert.strictEqual(helpers.length, 4); + + yield* Scope.close(providerScope, Exit.void); + assert.strictEqual(helpers[3]!.closed, true); + }).pipe(Effect.provideService(HostProcess.Platform, "linux")), +); + it("maps current Codex model capability fields", () => { const capabilities = mapCodexModelCapabilities({ additionalSpeedTiers: [], diff --git a/apps/server/src/provider/CodexProvider.ts b/apps/server/src/provider/CodexProvider.ts index df105d3ba20fe..7dcf9079f82b3 100644 --- a/apps/server/src/provider/CodexProvider.ts +++ b/apps/server/src/provider/CodexProvider.ts @@ -3,10 +3,12 @@ import { pipe } from "effect/Function"; import * as DateTime from "effect/DateTime"; import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; +import * as Equal from "effect/Equal"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as Path from "effect/Path"; +import * as RcMap from "effect/RcMap"; import * as Result from "effect/Result"; import * as Schema from "effect/Schema"; import * as Schedule from "effect/Schedule"; @@ -839,6 +841,52 @@ export const probeCodexSkillsForCwd = Effect.fn("probeCodexSkillsForCwd")(functi return parseCodexSkillsListResponse(skillsResponse, input.cwd); }); +// Long enough for a burst of workspace switches; short because the helper holds the user's +// binary open, and Windows refuses to overwrite or delete a running executable. +const CODEX_SKILLS_HELPER_IDLE_TTL = "30 seconds"; + +/** Reuses one app-server per launch identity; each read passes its cwd and rescans from disk. */ +export const makeCodexSkillsProbe = Effect.fn("makeCodexSkillsProbe")(function* () { + const helpers = yield* RcMap.make({ + lookup: (launch: { + readonly binaryPath: string; + readonly homePath?: string; + readonly launchArgs?: string; + readonly environment: NodeJS.ProcessEnv; + }) => + withCodexAppServerClient({ ...launch, cwd: process.cwd() }).pipe( + Effect.map(({ client }) => client), + ), + idleTimeToLive: CODEX_SKILLS_HELPER_IDLE_TTL, + }); + return Effect.fn("CodexSkillsProbe.probe")(function* (input: { + readonly binaryPath: string; + readonly homePath?: string; + readonly launchArgs?: string; + readonly cwd: string; + readonly environment?: NodeJS.ProcessEnv; + }) { + const { cwd, ...rest } = input; + const launch = { + ...rest, + environment: providerProbeEnvironment(rest.environment ?? process.env), + }; + // Retire helpers for a previous binary, home, credentials, or launch options. + const stale = Array.from(yield* RcMap.keys(helpers)).filter( + (key) => !Equal.equals(key, launch), + ); + yield* Effect.forEach(stale, (key) => RcMap.invalidate(helpers, key), { discard: true }); + const skillsResponse = yield* RcMap.get(helpers, launch).pipe( + Effect.flatMap((client) => client.request("skills/list", { cwds: [cwd], forceReload: true })), + Effect.scoped, + // RcMap keeps a failed spawn, and a dead helper keeps failing; drop it so the retry respawns. + Effect.onError(() => RcMap.invalidate(helpers, launch)), + Effect.retry({ times: 1 }), + ); + return parseCodexSkillsListResponse(skillsResponse, cwd); + }); +}); + const emptyCodexModelsFromSettings = (codexSettings: CodexSettings): ServerProvider["models"] => appendCustomCodexModels([], codexSettings.customModels); diff --git a/apps/server/src/provider/Drivers/CodexDriver.ts b/apps/server/src/provider/Drivers/CodexDriver.ts index f8a753169b54a..03245ba46d6c1 100644 --- a/apps/server/src/provider/Drivers/CodexDriver.ts +++ b/apps/server/src/provider/Drivers/CodexDriver.ts @@ -29,7 +29,7 @@ import * as ResetCreditCoordinator from "../resetCreditCoordinator.ts"; import { checkCodexProviderStatus, makePendingCodexProvider, - probeCodexSkillsForCwd, + makeCodexSkillsProbe, withCodexAppServerClient, } from "../CodexProvider.ts"; import { resolveCodexLaunchArgs } from "../codexLaunchArgs.ts"; @@ -321,22 +321,21 @@ export const CodexDriver: ProviderDriver< processEnv, snapshot.getSnapshot.pipe(Effect.map((value) => value.models)), ); + const probeSkills = yield* makeCodexSkillsProbe().pipe( + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), + ); const snapshotForCwd = (cwd: string) => !effectiveConfig.enabled ? snapshot.getSnapshot : Effect.all([ snapshot.getSnapshot, - probeCodexSkillsForCwd({ + probeSkills({ binaryPath: effectiveConfig.binaryPath, homePath: effectiveConfig.homePath, launchArgs: resolveCodexLaunchArgs(effectiveConfig.launchArgs, processEnv), cwd, environment: processEnv, - }).pipe( - Effect.scoped, - Effect.timeout(resolveProviderProbeTimeoutMs("codex", 20_000, processEnv)), - Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), - ), + }).pipe(Effect.timeout(resolveProviderProbeTimeoutMs("codex", 20_000, processEnv))), ]).pipe( Effect.map(([machineSnapshot, skills]) => ({ ...machineSnapshot, skills })), Effect.mapError( From f2c944947ddf2b343395049aa4ffb9792b714a04 Mon Sep 17 00:00:00 2001 From: Adamulek123 Date: Sun, 15 Nov 2026 09:20:39 +0100 Subject: [PATCH 009/446] fix(web): recover expired composer uploads before sending (#32910) Co-authored-by: maria-rcks Co-authored-by: Julius Marminge --- .../src/components/ChatView.browser.test.tsx | 5 + apps/web/src/components/chat/chatViewSend.ts | 15 +- .../chat/composerSend.logic.test.ts | 42 +++++ .../src/components/chat/composerSend.logic.ts | 23 +++ .../src/components/chat/useComposerSend.ts | 41 +++- .../components/chat/useMultipleModelSend.ts | 4 + .../chat/usePendingThreadRequests.ts | 145 ++++++++++----- .../src/components/chat/useQueuedRunEdit.ts | 25 ++- .../web/src/lib/attachmentUploadQueue.test.ts | 175 ++++++++++++++++++ apps/web/src/lib/attachmentUploadQueue.ts | 71 ++++++- 10 files changed, 488 insertions(+), 58 deletions(-) diff --git a/apps/web/src/components/ChatView.browser.test.tsx b/apps/web/src/components/ChatView.browser.test.tsx index 5bb8af3ae46d0..a0458f9e80280 100644 --- a/apps/web/src/components/ChatView.browser.test.tsx +++ b/apps/web/src/components/ChatView.browser.test.tsx @@ -60,6 +60,10 @@ function acceptAttachmentUploads(): Disposable { const mint = answerRpc(WS_METHODS.attachmentsCreateUploadUrl, () => Effect.succeed({ attachmentId: "upload-1", relativeUrl: "/upload", expiresAt: 0 }), ); + // Sending verifies each ready upload still exists on the server. + const verify = answerRpc(WS_METHODS.assetsCreateUrl, () => + Effect.succeed({ relativeUrl: "/upload-1", expiresAt: 0 }), + ); const status = vi.spyOn(XMLHttpRequest.prototype, "status", "get").mockReturnValue(200); const send = vi.spyOn(XMLHttpRequest.prototype, "send").mockImplementation(function ( this: XMLHttpRequest, @@ -71,6 +75,7 @@ function acceptAttachmentUploads(): Disposable { send.mockRestore(); status.mockRestore(); mint[Symbol.dispose](); + verify[Symbol.dispose](); }, }; } diff --git a/apps/web/src/components/chat/chatViewSend.ts b/apps/web/src/components/chat/chatViewSend.ts index c5c65e7d6e783..58f12549d15f3 100644 --- a/apps/web/src/components/chat/chatViewSend.ts +++ b/apps/web/src/components/chat/chatViewSend.ts @@ -16,7 +16,10 @@ import { type AtomCommandResult, } from "@t3tools/client-runtime/state/runtime"; import { readLocalApi } from "../../localApi"; -import { awaitAttachmentUploads, getUploadedAttachments } from "../../lib/attachmentUploadQueue"; +import { + getUploadedAttachments, + refreshExpiredAttachmentUploads, +} from "../../lib/attachmentUploadQueue"; import { findRevertCheckpoint } from "../../session-logic"; import { type ChatMessage } from "../../types"; import { randomUUID } from "~/lib/utils"; @@ -25,6 +28,7 @@ import { newThreadId } from "~/lib/utils"; import { type ComposerFileAttachment, type ComposerImageAttachment, + type ComposerThreadTarget, useComposerDraftStore, DraftId, } from "../../composerDraftStore"; @@ -328,12 +332,17 @@ export function prepareTurnAttachments(input: { readonly environmentId: EnvironmentId; readonly attachments: ReadonlyArray; readonly usesAttachmentUploads: boolean; + readonly draftTarget: ComposerThreadTarget; }) { - const { environmentId, usesAttachmentUploads } = input; + const { environmentId, usesAttachmentUploads, draftTarget } = input; return Promise.all( input.attachments.map(async (attachment) => { if (usesAttachmentUploads) { - await awaitAttachmentUploads([attachment.id]); + await refreshExpiredAttachmentUploads({ + environmentId, + images: [attachment], + draftTarget, + }); const uploaded = getUploadedAttachments({ environmentId, images: [attachment] })?.[0]; if (!uploaded) { throw new Error(`Attachment '${attachment.name}' failed to upload.`); diff --git a/apps/web/src/components/chat/composerSend.logic.test.ts b/apps/web/src/components/chat/composerSend.logic.test.ts index 13e36ea5659c9..dea00844d6a50 100644 --- a/apps/web/src/components/chat/composerSend.logic.test.ts +++ b/apps/web/src/components/chat/composerSend.logic.test.ts @@ -1,11 +1,13 @@ import { EnvironmentId, ThreadId } from "@t3tools/contracts"; import { scopeThreadRef } from "@t3tools/client-runtime/environment"; import { describe, expect, it } from "vite-plus/test"; +import type { ComposerThreadDraftState } from "../../composerDraftStore"; import { formatInlineContextReference } from "../../lib/composerContextReferences"; import { threadContextReference } from "../../lib/composerContextRecords"; import { threadContextRecord } from "@t3tools/shared/composerContextReferences"; import { approvalRedirectNotice, + composerDraftContentChanged, loadBalancingSendWarning, multipleModelSendBlock, referencedThreadContexts, @@ -16,6 +18,46 @@ const remote = EnvironmentId.make("remote"); const localThread = threadContextRecord(scopeThreadRef(local, ThreadId.make("a")), "Local"); const remoteThread = threadContextRecord(scopeThreadRef(remote, ThreadId.make("b")), "Remote"); +it("detects draft edits during upload preparation without treating upload metadata as an edit", () => { + const draft: ComposerThreadDraftState = { + prompt: "before", + images: [], + files: [ + { + type: "file", + id: "file", + name: "a.pdf", + mimeType: "application/pdf", + sizeBytes: 1, + file: null, + }, + ], + nonPersistedImageIds: [], + persistedAttachments: [], + terminalContexts: [], + previewAnnotations: [], + reviewComments: [], + threadContexts: [], + environmentContexts: [], + replyTo: null, + modelSelectionByProvider: {}, + activeProvider: null, + runtimeMode: null, + interactionMode: null, + }; + expect( + composerDraftContentChanged(draft, { + ...draft, + files: [{ ...draft.files[0]!, uploadedAttachmentId: "upload", uploadEnvironmentId: local }], + }), + ).toBe(false); + expect(composerDraftContentChanged(draft, { ...draft, prompt: "after" })).toBe(true); + expect(composerDraftContentChanged(draft, { ...draft, files: [] })).toBe(true); + expect(composerDraftContentChanged(draft, { ...draft, threadContexts: [localThread] })).toBe( + true, + ); +}); + describe("referencedThreadContexts", () => { it("keeps only the attached threads the prompt still links", () => { const prompt = `see ${formatInlineContextReference(threadContextReference(remoteThread))}`; diff --git a/apps/web/src/components/chat/composerSend.logic.ts b/apps/web/src/components/chat/composerSend.logic.ts index e19512e6c90d4..5b7a5ac3a09c7 100644 --- a/apps/web/src/components/chat/composerSend.logic.ts +++ b/apps/web/src/components/chat/composerSend.logic.ts @@ -1,5 +1,28 @@ import type { EnvironmentId, ThreadContextRecord } from "@t3tools/contracts"; import { collectInlineContextIds } from "../../lib/composerContextReferences"; +import type { ComposerThreadDraftState } from "../../composerDraftStore"; + +/** Compare immutable user content without copying image bytes or upload metadata. */ +export function composerDraftContentChanged( + before: ComposerThreadDraftState | null, + after: ComposerThreadDraftState | null, +): boolean { + if (before === after) return false; + if (!before || !after) return true; + const sameIds = (left: ReadonlyArray<{ id: string }>, right: ReadonlyArray<{ id: string }>) => + left.length === right.length && left.every((item, index) => item.id === right[index]?.id); + return ( + before.prompt !== after.prompt || + !sameIds(before.images, after.images) || + !sameIds(before.files, after.files) || + before.terminalContexts !== after.terminalContexts || + before.previewAnnotations !== after.previewAnnotations || + before.reviewComments !== after.reviewComments || + before.threadContexts !== after.threadContexts || + before.environmentContexts !== after.environmentContexts || + before.replyTo !== after.replyTo + ); +} /** Attached threads the prompt still links to; replacing linked text leaves stale records. */ export function referencedThreadContexts(draft: { diff --git a/apps/web/src/components/chat/useComposerSend.ts b/apps/web/src/components/chat/useComposerSend.ts index 80555864284a6..1335bbdde2d14 100644 --- a/apps/web/src/components/chat/useComposerSend.ts +++ b/apps/web/src/components/chat/useComposerSend.ts @@ -99,6 +99,7 @@ import { useStableCallback } from "../../hooks/useStableCallback"; import { environmentPresentations } from "../../state/presentation"; import { approvalRedirectNotice, + composerDraftContentChanged, loadBalancingSendWarning, multipleModelSendBlock, referencedThreadContexts, @@ -106,8 +107,8 @@ import { import { useMultipleModelSend } from "./useMultipleModelSend"; import { type ComposerDispatchMode } from "@t3tools/client-runtime/state/composer-dispatch"; import { - awaitAttachmentUploads, getUploadedAttachments, + refreshExpiredAttachmentUploads, readAttachmentUpload, releaseDraftAttachments, retryAttachmentUpload, @@ -872,6 +873,14 @@ export function useComposerSend(input: ComposerSendInput) { multipleTargets.push(resolved); } + const draftBeforePreparation = useComposerDraftStore + .getState() + .getComposerDraft(composerDraftTarget); + const draftChangedDuringPreparation = () => { + if (composerRef.current?.hasPendingAttachments() === true) return true; + const draft = useComposerDraftStore.getState().getComposerDraft(composerDraftTarget); + return composerDraftContentChanged(draftBeforePreparation, draft); + }; sendInFlightRef.current = true; const sendGeneration = ++composerSendGenerationRef.current; const attachmentCapabilitiesBeforeUpload = readLiveAttachmentCapabilities(); @@ -919,6 +928,17 @@ export function useComposerSend(input: ComposerSendInput) { } const attachmentCapabilitiesBeforeDispatch = readLiveAttachmentCapabilities(); + if (draftChangedDuringPreparation()) { + sendInFlightRef.current = false; + setThreadError( + threadIdForSend, + "Draft changed while preparing this message. Review it and send again.", + ); + setDockedDraftHeroThreadKey((currentThreadKey) => + currentThreadKey === activeThreadKey ? null : currentThreadKey, + ); + return; + } if ( !readEnvironmentScope(environmentId, AuthOrchestrationOperateScope) || attachmentCapabilitiesBeforeDispatch.fileBlockReason !== null @@ -940,7 +960,22 @@ export function useComposerSend(input: ComposerSendInput) { composerAttachmentsSnapshot.length > 0 && (sendCtx?.shownPendingApprovalIds.length ?? 0) > 0 ) { - await awaitAttachmentUploads(composerAttachmentsSnapshot.map((attachment) => attachment.id)); + await refreshExpiredAttachmentUploads({ + environmentId, + images: composerAttachmentsSnapshot, + draftTarget: composerDraftTarget, + }); + if (draftChangedDuringPreparation()) { + sendInFlightRef.current = false; + setThreadError( + threadIdForSend, + "Draft changed while preparing this message. Review it and send again.", + ); + setDockedDraftHeroThreadKey((currentThreadKey) => + currentThreadKey === activeThreadKey ? null : currentThreadKey, + ); + return; + } const uploadBlockReason = readLiveAttachmentCapabilities().fileBlockReason ?? (getUploadedAttachments({ environmentId, images: composerAttachmentsSnapshot }) === null @@ -1103,6 +1138,7 @@ export function useComposerSend(input: ComposerSendInput) { environmentId, attachments: composerAttachmentsSnapshot, usesAttachmentUploads: turnUsesAttachmentUploads, + draftTarget: composerDraftTarget, }); if (multipleModelSelections !== null) { await sendToMultipleModels({ @@ -1118,6 +1154,7 @@ export function useComposerSend(input: ComposerSendInput) { readFileBlockReason: () => readLiveAttachmentCapabilities().fileBlockReason, buildOutgoingMessageContext, isCurrentSend: () => composerSendGenerationRef.current === sendGeneration, + draftChangedDuringPreparation, }); return; } diff --git a/apps/web/src/components/chat/useMultipleModelSend.ts b/apps/web/src/components/chat/useMultipleModelSend.ts index ccea2378968f5..ccf3b32621ce2 100644 --- a/apps/web/src/components/chat/useMultipleModelSend.ts +++ b/apps/web/src/components/chat/useMultipleModelSend.ts @@ -97,6 +97,7 @@ export function useMultipleModelSend(input: MultipleModelSendInput) { readFileBlockReason, buildOutgoingMessageContext, isCurrentSend, + draftChangedDuringPreparation, }: { readonly threadId: ThreadId; readonly project: EnvironmentProject; @@ -113,6 +114,7 @@ export function useMultipleModelSend(input: MultipleModelSendInput) { ) => ReturnType; /** False once a newer send from this composer started. */ readonly isCurrentSend: () => boolean; + readonly draftChangedDuringPreparation: () => boolean; }) => { const { attachments: attachmentsSnapshot, messageText } = sendSnapshot; const failedSelections: ModelSelection[] = []; @@ -124,6 +126,8 @@ export function useMultipleModelSend(input: MultipleModelSendInput) { // fails every selection; the draft restore below still runs. const sendResult = await settlePromise(async () => { const attachments = await attachmentsPromise; + if (draftChangedDuringPreparation()) + throw new Error("Draft changed while preparing this message. Review it and send again."); const fileBlockReason = readFileBlockReason(); if (fileBlockReason !== null) throw new Error(fileBlockReason); const context = buildOutgoingMessageContext( diff --git a/apps/web/src/components/chat/usePendingThreadRequests.ts b/apps/web/src/components/chat/usePendingThreadRequests.ts index 14c7242821076..8c32b07a29271 100644 --- a/apps/web/src/components/chat/usePendingThreadRequests.ts +++ b/apps/web/src/components/chat/usePendingThreadRequests.ts @@ -25,7 +25,12 @@ import { useComposerDraftStore, } from "../../composerDraftStore"; import { useStableCallback } from "../../hooks/useStableCallback"; -import { getUploadedAttachments, useAttachmentUploadStore } from "../../lib/attachmentUploadQueue"; +import { + getUploadedAttachments, + refreshExpiredAttachmentUploads, + useAttachmentUploadStore, +} from "../../lib/attachmentUploadQueue"; +import { composerDraftContentChanged } from "./composerSend.logic"; import { buildPendingUserInputAnswers, setPendingUserInputCustomAnswer, @@ -390,60 +395,102 @@ export function usePendingThreadRequests(input: { if (!pendingInput || pendingInput.responseCapability === "not_resumable") return; const responseKey = JSON.stringify([environmentId, activeThreadId, requestId]); if (userInputResponsesInFlight.current.has(responseKey)) return; - const attachmentsByQuestionId = new Map(); - const providerAnswers = { ...answers }; - const threadDraft = useComposerDraftStore.getState().getComposerDraft(composerDraftTarget); - for (const question of pendingInput.questions) { - const target = questionAttachmentDraftId( - environmentId, - activeThreadId, - requestId, - question.id, - ); - if ((useQuestionAttachmentPreparation.getState().counts[target] ?? 0) > 0) return; - const draft = useComposerDraftStore.getState().getComposerDraft(target); - providerAnswers[question.id] = projectPendingUserInputAnswer( - answers[question.id], - answerContextRecords(draft, threadDraft), - ); - const attachments = draft ? [...draft.images, ...draft.files] : []; - if (attachments.length === 0) continue; - const uploaded = getUploadedAttachments({ environmentId, images: attachments }); - if (!uploaded) { + userInputResponsesInFlight.current.add(responseKey); + const answerDraftKey = userInputDraftKey(environmentId, activeThreadId, requestId); + const answerDraftBeforePreparation = usePendingUserInputAnswers.getState()[answerDraftKey]; + setRespondingUserInputRequestIds((existing) => + existing.includes(requestId) ? existing : [...existing, requestId], + ); + const prepareAndRespond = async () => { + const attachmentsByQuestionId = new Map(); + const providerAnswers = { ...answers }; + const threadDraft = useComposerDraftStore.getState().getComposerDraft(composerDraftTarget); + const questionDrafts = pendingInput.questions.map((question) => { + const target = questionAttachmentDraftId( + environmentId, + activeThreadId, + requestId, + question.id, + ); + const draft = useComposerDraftStore.getState().getComposerDraft(target); + return { question, target, draft }; + }); + for (const { question, target, draft } of questionDrafts) { + if ((useQuestionAttachmentPreparation.getState().counts[target] ?? 0) > 0) return; + providerAnswers[question.id] = projectPendingUserInputAnswer( + answers[question.id], + answerContextRecords(draft, threadDraft), + ); + const attachments = draft ? [...draft.images, ...draft.files] : []; + if (attachments.length === 0) continue; + await refreshExpiredAttachmentUploads({ + environmentId, + images: attachments, + draftTarget: target, + }); + if ( + composerDraftContentChanged( + draft, + useComposerDraftStore.getState().getComposerDraft(target), + ) + ) { + setThreadError( + activeThreadId, + "Answer changed while preparing attachments. Review it and send again.", + ); + return; + } + const uploaded = getUploadedAttachments({ environmentId, images: attachments }); + if (!uploaded) { + setThreadError( + activeThreadId, + "Wait for attachments to finish uploading, or remove failed uploads.", + ); + return; + } + attachmentsByQuestionId.set(question.id, uploaded as UserInputAttachments[string]); + } + if ( + usePendingUserInputAnswers.getState()[answerDraftKey] !== answerDraftBeforePreparation || + questionDrafts.some( + ({ target, draft }) => + (useQuestionAttachmentPreparation.getState().counts[target] ?? 0) > 0 || + composerDraftContentChanged( + draft, + useComposerDraftStore.getState().getComposerDraft(target), + ), + ) + ) { setThreadError( activeThreadId, - "Wait for attachments to finish uploading, or remove failed uploads.", + "Answer changed while preparing attachments. Review it and send again.", ); return; } - attachmentsByQuestionId.set(question.id, uploaded as UserInputAttachments[string]); - } - userInputResponsesInFlight.current.add(responseKey); - - setRespondingUserInputRequestIds((existing) => - existing.includes(requestId) ? existing : [...existing, requestId], - ); - const result = await respondToThreadUserInput({ - environmentId, - input: { - threadId: activeThreadId, - requestId, - answers: providerAnswers, - ...(attachmentsByQuestionId.size > 0 - ? { attachmentsByQuestionId: Object.fromEntries(attachmentsByQuestionId) } - : {}), - }, + const result = await respondToThreadUserInput({ + environmentId, + input: { + threadId: activeThreadId, + requestId, + answers: providerAnswers, + ...(attachmentsByQuestionId.size > 0 + ? { attachmentsByQuestionId: Object.fromEntries(attachmentsByQuestionId) } + : {}), + }, + }); + if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) { + const error = squashAtomCommandFailure(result); + setThreadError( + activeThreadId, + error instanceof Error ? error.message : "Failed to submit user input.", + ); + } + return result; + }; + return prepareAndRespond().finally(() => { + userInputResponsesInFlight.current.delete(responseKey); + setRespondingUserInputRequestIds((existing) => existing.filter((id) => id !== requestId)); }); - if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) { - const error = squashAtomCommandFailure(result); - setThreadError( - activeThreadId, - error instanceof Error ? error.message : "Failed to submit user input.", - ); - } - userInputResponsesInFlight.current.delete(responseKey); - setRespondingUserInputRequestIds((existing) => existing.filter((id) => id !== requestId)); - return result; }; const onDismissUserInput = async (requestId: RuntimeRequestId) => { diff --git a/apps/web/src/components/chat/useQueuedRunEdit.ts b/apps/web/src/components/chat/useQueuedRunEdit.ts index e7f321bec9dc2..d5cb8505a013e 100644 --- a/apps/web/src/components/chat/useQueuedRunEdit.ts +++ b/apps/web/src/components/chat/useQueuedRunEdit.ts @@ -38,6 +38,7 @@ import { useStableCallback } from "../../hooks/useStableCallback"; import { awaitAttachmentUploads, getUploadedAttachments, + refreshExpiredAttachmentUploads, releaseDraftAttachments, startAttachmentUpload, } from "../../lib/attachmentUploadQueue"; @@ -60,6 +61,7 @@ import { useAtomCommand } from "../../state/use-atom-command"; import { stackedThreadToast, toastManager } from "../ui/toast"; import type { ChatComposerHandle } from "./ChatComposer"; import { fileAttachmentCapabilityBlockReason } from "./composerAttachmentFiles"; +import { composerDraftContentChanged } from "./composerSend.logic"; import { discardQueuedEditDraft, prepareQueuedEditAttachments, @@ -105,6 +107,7 @@ async function submitQueuedRunEdit(input: { readonly editing: EditingQueuedRun; readonly content: QueuedRunEditContent; readonly editQueuedRun: ReturnType; + readonly draftChanged: () => boolean; }): Promise { const { environmentId, draftTarget, editing, content } = input; const editText = content.prompt.trim(); @@ -124,6 +127,7 @@ async function submitQueuedRunEdit(input: { validateFiles(); for (const file of files) startAttachmentUpload({ environmentId, image: file, draftTarget }); await awaitAttachmentUploads(files.map((file) => file.id)); + await refreshExpiredAttachmentUploads({ environmentId, images: files, draftTarget }); validateFiles(); const uploaded = getUploadedAttachments({ environmentId, images: files }); if (uploaded === null) throw new Error("Retry or remove failed uploads before saving."); @@ -137,6 +141,8 @@ async function submitQueuedRunEdit(input: { readImage: readFileAsDataUrl, uploadFiles, }); + if (input.draftChanged()) + return "Draft changed while preparing this message. Review it and save again."; // The edit draft owns thread and machine mentions, so a chip removed while editing is dropped. const keptRecords = (editing.context?.records ?? []).filter( (record) => @@ -363,15 +369,29 @@ export function useQueuedRunEdit(input: { // The save's result decides the edit's fate, even if the run starts meanwhile. const finishSave = queuedRunEdits.startSave(editKey); if (finishSave === null) return; + const draftBeforeSave = useComposerDraftStore.getState().getComposerDraft(draftTarget); + const draftChanged = () => + appAtomRegistry.get(queuedRunEdits.atom)[editKey] !== editing || + composerDraftContentChanged( + draftBeforeSave, + useComposerDraftStore.getState().getComposerDraft(draftTarget), + ); const error = await submitQueuedRunEdit({ environmentId, draftTarget, editing, content, editQueuedRun: editQueuedRunCommand, + draftChanged, }); - setThreadError(editing.threadId, error); - const current = finishSave(error === null); + const changedAfterSave = error === null && draftChanged(); + setThreadError( + editing.threadId, + changedAfterSave + ? "The earlier edit was saved. Review your newer changes and save again." + : error, + ); + const current = finishSave(error === null && !changedAfterSave); if (!current) { // A failed save can recover into the thread draft; a newer edit can also retain resources. const drafts = Object.values(useComposerDraftStore.getState().draftsByThreadKey); @@ -394,6 +414,7 @@ export function useQueuedRunEdit(input: { return; } if (error !== null) return; + if (changedAfterSave) return; releaseDraftAttachments(newAttachments); promptRef.current = ""; clearEditDraft(editing.runId); diff --git a/apps/web/src/lib/attachmentUploadQueue.test.ts b/apps/web/src/lib/attachmentUploadQueue.test.ts index 1b9902336e027..a9483e1755e35 100644 --- a/apps/web/src/lib/attachmentUploadQueue.test.ts +++ b/apps/web/src/lib/attachmentUploadQueue.test.ts @@ -57,6 +57,7 @@ import { awaitAttachmentUploads, getUploadedAttachments, readAttachmentUpload, + refreshExpiredAttachmentUploads, releaseAttachmentUpload, releaseDraftAttachment, releaseDraftAttachments, @@ -210,6 +211,180 @@ describe("attachmentUploadQueue", () => { vi.unstubAllGlobals(); }); + it("reuploads an expired image while retaining other ready images", async () => { + const images = [makeImage("first"), makeImage("second")].map((image) => ({ + ...image, + name: "image.png", + })); + let minted = 0; + mocks.runAtomCommand.mockImplementation(async (_registry, command) => { + if (command !== mocks.createUploadUrl) return { _tag: "Success", value: undefined }; + const attachmentId = `pending-test-${++minted}`; + return { + _tag: "Success", + value: { + attachmentId, + relativeUrl: `/api/attachments/upload/${attachmentId}`, + expiresAt: 1, + }, + }; + }); + for (const image of images) startAttachmentUpload({ environmentId: firstEnvironment, image }); + await Promise.resolve(); + const initial = awaitAttachmentUploads(images.map((image) => image.id)); + TestXmlHttpRequest.requests.forEach((request) => request.complete()); + await initial; + mocks.executeAtomQuery.mockImplementation( + async (_registry, target: { input: { resource: { attachmentId: string } } }) => + target.input.resource.attachmentId === "pending-test-1" + ? { _tag: "Failure", error: { _tag: "AssetAttachmentNotFoundError" } } + : { _tag: "Success", value: {} }, + ); + const refreshing = refreshExpiredAttachmentUploads({ environmentId: firstEnvironment, images }); + await vi.waitFor(() => expect(TestXmlHttpRequest.requests).toHaveLength(3)); + TestXmlHttpRequest.requests[2]!.complete(); + await refreshing; + expect( + getUploadedAttachments({ environmentId: firstEnvironment, images })?.map((image) => image.id), + ).toEqual(["pending-test-3", "pending-test-2"]); + expect(TestXmlHttpRequest.requests).toHaveLength(3); + }); + + it("reuploads local file bytes after its persisted upload is known to be missing", async () => { + const file = { + ...makeFile("expired-saved-file"), + uploadedAttachmentId: "pending-expired", + uploadEnvironmentId: firstEnvironment, + }; + useAttachmentUploadStore.setState({ + uploadsByImageId: { + [file.id]: { + status: "ready", + environmentId: firstEnvironment, + attachmentId: "pending-expired", + }, + }, + }); + mocks.executeAtomQuery + .mockResolvedValueOnce({ + _tag: "Failure", + error: { _tag: "AssetAttachmentNotFoundError" }, + }) + .mockResolvedValue({ _tag: "Failure", error: new Error("Connection interrupted") }); + let uploadStarted!: () => void; + const started = new Promise((resolve) => { + uploadStarted = resolve; + }); + const runCommand = mocks.runAtomCommand.getMockImplementation()!; + mocks.runAtomCommand.mockImplementation(async (...args) => { + const result = await runCommand(...args); + if (args[1] === mocks.createUploadUrl) uploadStarted(); + return result; + }); + const refresh = refreshExpiredAttachmentUploads({ + environmentId: firstEnvironment, + images: [file], + }); + expect(await Promise.race([started.then(() => "upload"), refresh.then(() => "settled")])).toBe( + "upload", + ); + expect(TestXmlHttpRequest.requests).toHaveLength(1); + TestXmlHttpRequest.requests[0]!.complete(); + await refresh; + expect(mocks.executeAtomQuery).toHaveBeenCalledTimes(1); + expect( + getUploadedAttachments({ environmentId: firstEnvironment, images: [file] }), + ).toMatchObject([{ id: "pending-environment-1-expired-saved-file.pdf" }]); + }); + + it.each([false, true])( + "retains cleanup identity after verification fails (retry: %s)", + async (retry) => { + const image = makeImage("connection-failure"); + useAttachmentUploadStore.setState({ + uploadsByImageId: { + [image.id]: { + status: "ready", + environmentId: firstEnvironment, + attachmentId: "pending-original", + }, + }, + }); + mocks.executeAtomQuery.mockRejectedValueOnce(new Error("offline")); + await refreshExpiredAttachmentUploads({ environmentId: firstEnvironment, images: [image] }); + expect(readAttachmentUpload(image.id)).toMatchObject({ status: "failed" }); + expect( + getUploadedAttachments({ environmentId: firstEnvironment, images: [image] }), + ).toBeNull(); + expect(TestXmlHttpRequest.requests).toHaveLength(0); + if (retry) retryAttachmentUpload({ environmentId: firstEnvironment, image }); + releaseAttachmentUpload(image.id); + expect(readAttachmentUpload(image.id)).toBeUndefined(); + expect(mocks.runAtomCommand).toHaveBeenCalledExactlyOnceWith( + expect.anything(), + mocks.removeUpload, + { environmentId: firstEnvironment, input: { attachmentId: "pending-original" } }, + expect.anything(), + ); + }, + ); + + it("reuploads local bytes for a connection without read permission", async () => { + const image = makeImage("operate-only"); + useAttachmentUploadStore.setState({ + uploadsByImageId: { + [image.id]: { + status: "ready", + environmentId: firstEnvironment, + attachmentId: "pending-original", + }, + }, + }); + mocks.readEnvironmentScope.mockImplementation( + (_environmentId, scope) => scope !== "orchestration:read", + ); + const refresh = refreshExpiredAttachmentUploads({ + environmentId: firstEnvironment, + images: [image], + }); + await vi.waitFor(() => expect(TestXmlHttpRequest.requests).toHaveLength(1)); + TestXmlHttpRequest.requests[0]!.complete(); + await refresh; + expect(readAttachmentUpload(image.id)).toMatchObject({ + status: "ready", + attachmentId: "pending-environment-1-operate-only.png", + }); + expect(mocks.executeAtomQuery).not.toHaveBeenCalled(); + }); + + it("does not resurrect an image removed while its upload is being checked", async () => { + const image = makeImage("removed-during-verification"); + useAttachmentUploadStore.setState({ + uploadsByImageId: { + [image.id]: { + status: "ready", + environmentId: firstEnvironment, + attachmentId: "pending-original", + }, + }, + }); + let finish!: (value: unknown) => void; + mocks.executeAtomQuery.mockReturnValueOnce( + new Promise((resolve) => { + finish = resolve; + }), + ); + const refresh = refreshExpiredAttachmentUploads({ + environmentId: firstEnvironment, + images: [image], + }); + releaseAttachmentUpload(image.id); + finish({ _tag: "Failure", error: { _tag: "AssetAttachmentNotFoundError" } }); + await refresh; + expect(readAttachmentUpload(image.id)).toBeUndefined(); + expect(TestXmlHttpRequest.requests).toHaveLength(0); + }); + it("keeps an attachment local until its own environment grants write access", async () => { const image = makeImage("permission-gain"); mocks.readEnvironmentScope.mockImplementation( diff --git a/apps/web/src/lib/attachmentUploadQueue.ts b/apps/web/src/lib/attachmentUploadQueue.ts index 73458849164cd..cf9bbedeb853a 100644 --- a/apps/web/src/lib/attachmentUploadQueue.ts +++ b/apps/web/src/lib/attachmentUploadQueue.ts @@ -1,5 +1,6 @@ import { AuthOrchestrationOperateScope, + AuthOrchestrationReadScope, PROVIDER_SEND_TURN_SUPPORTED_IMAGE_MIME_TYPES, type ChatAttachment, type EnvironmentId, @@ -395,6 +396,7 @@ export function startAttachmentUpload(input: { readonly environmentId: EnvironmentId; readonly image: ComposerImageAttachment | ComposerFileAttachment; readonly draftTarget?: ComposerThreadTarget; + readonly force?: boolean; }): void { if (!readEnvironmentScope(input.environmentId, AuthOrchestrationOperateScope)) return; const existingJob = jobsByImageId.get(input.image.id); @@ -403,7 +405,11 @@ export function startAttachmentUpload(input: { } const existing = readAttachmentUpload(input.image.id); - if (existing?.status === "ready" && existing.environmentId === input.environmentId) { + if ( + !input.force && + existing?.status === "ready" && + existing.environmentId === input.environmentId + ) { // A file restored into a draft after its upload finished (stash, failed send) missed the // completion stamp; stamping here keeps it from re-uploading after a reload. stampDraftFileUpload(input, existing.attachmentId); @@ -440,7 +446,8 @@ export function startAttachmentUpload(input: { environmentId: input.environmentId, ...(input.draftTarget !== undefined ? { draftTarget: input.draftTarget } : {}), ...(previous ? { previous } : {}), - ...(input.image.type === "file" && + ...(!input.force && + input.image.type === "file" && input.image.uploadEnvironmentId === input.environmentId && input.image.uploadedAttachmentId ? { persistedAttachmentId: input.image.uploadedAttachmentId } @@ -582,6 +589,66 @@ export async function awaitAttachmentUploads(imageIds: ReadonlyArray): P await Promise.all(imageIds.map((imageId) => jobsByImageId.get(imageId)?.settled)); } +/** Revalidate ready uploads at send time; a long-lived draft can outlive server cleanup. */ +export async function refreshExpiredAttachmentUploads(input: { + readonly environmentId: EnvironmentId; + readonly images: ReadonlyArray; + readonly draftTarget?: ComposerThreadTarget; +}): Promise { + const canVerify = readEnvironmentScope(input.environmentId, AuthOrchestrationReadScope); + await Promise.all( + input.images.map(async (image) => { + const upload = readAttachmentUpload(image.id); + if (upload?.status !== "ready" || upload.environmentId !== input.environmentId) return; + if (!canVerify) { + if (image.file) { + startAttachmentUpload({ + environmentId: input.environmentId, + image, + force: true, + ...(input.draftTarget !== undefined ? { draftTarget: input.draftTarget } : {}), + }); + } else { + setUploadState(image.id, { + status: "failed", + environmentId: input.environmentId, + previous: upload, + reason: "Uploaded attachment could not be verified. Attach the file again.", + }); + } + return; + } + const verification = await verifyStashedAttachmentUpload({ + environmentId: input.environmentId, + attachmentId: upload.attachmentId, + }).catch((error: unknown): PersistedAttachmentVerification => ({ status: "failed", error })); + // The user may have removed or replaced the attachment while verification was in flight. + if (readAttachmentUpload(image.id) !== upload) return; + if (verification.status === "verified") return; + if (verification.status === "missing" && image.file) { + clearUploadState(image.id); + startAttachmentUpload({ + environmentId: input.environmentId, + image, + force: true, + ...(input.draftTarget !== undefined ? { draftTarget: input.draftTarget } : {}), + }); + return; + } + setUploadState(image.id, { + status: "failed", + environmentId: input.environmentId, + previous: upload, + reason: + verification.status === "missing" + ? "Uploaded attachment expired. Remove it and attach it again." + : "Uploaded attachment could not be verified. Retry when the server reconnects.", + }); + }), + ); + await awaitAttachmentUploads(input.images.map((image) => image.id)); +} + export function getUploadedAttachments(input: { readonly environmentId: EnvironmentId; readonly images: ReadonlyArray; From 32c069eed1d22ca4b4bbaa52ec8d34bb3f4a9ff3 Mon Sep 17 00:00:00 2001 From: Boban Kostadinoski Date: Sun, 15 Nov 2026 02:29:12 -0600 Subject: [PATCH 010/446] fix(server): content search recovers after a slow first index (#33138) Co-authored-by: ItsThierry <274141289+ItsThierry@users.noreply.github.com> Co-authored-by: maria-rcks Co-authored-by: Julius Marminge --- .../src/workspace/WorkspaceEntries.test.ts | 62 +++++++++++++++++++ apps/server/src/workspace/WorkspaceEntries.ts | 2 - .../src/workspace/WorkspaceSearchIndex.ts | 32 +++++++--- apps/web/src/state/queries.ts | 20 ++++++ 4 files changed, 106 insertions(+), 10 deletions(-) diff --git a/apps/server/src/workspace/WorkspaceEntries.test.ts b/apps/server/src/workspace/WorkspaceEntries.test.ts index 1c50b94a7cbcc..92d8bc882c951 100644 --- a/apps/server/src/workspace/WorkspaceEntries.test.ts +++ b/apps/server/src/workspace/WorkspaceEntries.test.ts @@ -1014,6 +1014,68 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => { expect({ finders, rescans }).toEqual({ finders: 1, rescans: 1 }); }), ); + + it.effect("serves content search once a first scan that outlasted the wait finishes", () => + Effect.gen(function* () { + const cwd = yield* makeTempDir({ prefix: "t3code-workspace-content-slow-scan-" }); + yield* writeTextFile(cwd, "src/notes.ts", "const needle = 1;\n"); + // Real finders; only the initial wait and the progress it reads report a scan still running. + let scanning = true; + let finders = 0; + let settle = (): Promise => Promise.resolve(); + const createFinder = FileFinder.create.bind(FileFinder); + vi.spyOn(FileFinder, "create").mockImplementation((options) => { + const created = createFinder(options); + if (!created.ok) return created; + const finder = created.value; + finders += 1; + const waitForIndexReady = finder.waitForIndexReady.bind(finder); + settle = () => waitForIndexReady(15_000); + finder.waitForIndexReady = (timeoutMs) => + scanning ? Promise.resolve({ ok: true, value: false }) : waitForIndexReady(timeoutMs); + const getScanProgress = finder.getScanProgressAsync.bind(finder); + finder.getScanProgressAsync = async () => { + const progress = await getScanProgress(); + return scanning && progress.ok + ? { + ok: true, + value: { ...progress.value, isScanning: true, isWarmupComplete: false }, + } + : progress; + }; + return created; + }); + const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; + const searchNeedle = workspaceEntries + .searchContents({ + cwd, + query: "needle", + limit: 100, + caseSensitive: true, + wholeWord: false, + useRegex: false, + }) + .pipe( + Effect.map((result): unknown => result.matches.map((match) => match.path)), + Effect.catchTags({ + WorkspaceSearchIndexScanTimedOut: (error) => Effect.succeed(error._tag), + }), + ); + + const whileScanning = yield* Effect.all([searchNeedle, searchNeedle], { + concurrency: "unbounded", + }); + scanning = false; + yield* Effect.promise(() => settle()); + const afterScan = yield* searchNeedle; + + expect({ whileScanning, afterScan, finders }).toEqual({ + whileScanning: ["WorkspaceSearchIndexScanTimedOut", "WorkspaceSearchIndexScanTimedOut"], + afterScan: ["src/notes.ts"], + finders: 1, + }); + }), + ); }); describe("browse", () => { diff --git a/apps/server/src/workspace/WorkspaceEntries.ts b/apps/server/src/workspace/WorkspaceEntries.ts index 51b1720126536..ad1d2c2ae0ebe 100644 --- a/apps/server/src/workspace/WorkspaceEntries.ts +++ b/apps/server/src/workspace/WorkspaceEntries.ts @@ -179,7 +179,6 @@ export const make = Effect.gen(function* () { function* ( cause: | WorkspaceSearchIndex.WorkspaceSearchIndexCreateFailed - | WorkspaceSearchIndex.WorkspaceSearchIndexScanTimedOut | WorkspaceSearchIndex.WorkspaceSearchIndexRefreshFailed, ) { yield* Effect.logWarning("Failed to refresh workspace search index", { @@ -197,7 +196,6 @@ export const make = Effect.gen(function* () { Effect.provide(workspaceSearchIndexes.get(indexKey)), Effect.catchTags({ WorkspaceSearchIndexCreateFailed: recoverRefreshFailure, - WorkspaceSearchIndexScanTimedOut: recoverRefreshFailure, WorkspaceSearchIndexRefreshFailed: recoverRefreshFailure, }), ); diff --git a/apps/server/src/workspace/WorkspaceSearchIndex.ts b/apps/server/src/workspace/WorkspaceSearchIndex.ts index 8d9798baadfd7..fd92420396e48 100644 --- a/apps/server/src/workspace/WorkspaceSearchIndex.ts +++ b/apps/server/src/workspace/WorkspaceSearchIndex.ts @@ -142,7 +142,10 @@ export class WorkspaceSearchIndex extends Context.Service< ) => Effect.Effect; readonly searchContents: ( input: Omit, - ) => Effect.Effect; + ) => Effect.Effect< + ProjectSearchContentsResult, + WorkspaceSearchIndexScanTimedOut | WorkspaceSearchIndexSearchFailed + >; readonly refresh: () => Effect.Effect; } >()("t3/workspace/WorkspaceSearchIndex") {} @@ -585,13 +588,12 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* ( cause, }), ).pipe( + // The scan keeps running on this finder; failing here would destroy it and cache the failure. Effect.catchTags({ - WorkspaceSearchIndexScanTimedOut: (error) => - variant === "paths" - ? Effect.sync(() => { - initialScanTimedOut = true; - }) - : Effect.fail(error), + WorkspaceSearchIndexScanTimedOut: () => + Effect.sync(() => { + initialScanTimedOut = true; + }), }), ); @@ -609,7 +611,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* ( const runSearch = Effect.fn("WorkspaceSearchIndex.runSearch")(function* ( query: string, pageSize: number, - operation: "directorySearch" | "fileSearch" | "grep" | "mixedSearch", + operation: "directorySearch" | "fileSearch" | "getScanProgress" | "grep" | "mixedSearch", execute: () => Promise>, ): Effect.fn.Return { // fff's searches wait on its picker lock, which its watcher can hold for minutes. @@ -1036,6 +1038,20 @@ 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(), + ); + // 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; + } + 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. diff --git a/apps/web/src/state/queries.ts b/apps/web/src/state/queries.ts index aa97d5f17aa66..8afc9d7ba9217 100644 --- a/apps/web/src/state/queries.ts +++ b/apps/web/src/state/queries.ts @@ -13,6 +13,7 @@ import { type ThreadSearchScope, } from "@t3tools/client-runtime/state/thread-search"; import { + ProjectSearchContentsError, PROJECT_SEARCH_CONTENTS_QUERY_MAX_LENGTH, THREAD_SEARCH_QUERY_MAX_LENGTH, } from "@t3tools/contracts"; @@ -30,6 +31,7 @@ import type { VcsRef, } from "@t3tools/contracts"; import * as Cause from "effect/Cause"; +import * as Schema from "effect/Schema"; import { AsyncResult, Atom } from "effect/reactivity"; import { useEffect, useState } from "react"; @@ -46,6 +48,8 @@ const PROJECT_PATH_SEARCH_DEBOUNCE_MS = 120; const COMPOSER_PATH_SEARCH_LIMIT = 80; const PROJECT_CONTENT_SEARCH_DEBOUNCE_MS = 120; const PROJECT_CONTENT_SEARCH_LIMIT = 500; +const PROJECT_CONTENT_SEARCH_SCAN_RETRY_MS = 30_000; +const isProjectSearchContentsError = Schema.is(ProjectSearchContentsError); const THREAD_SEARCH_DEBOUNCE_MS = 120; const VCS_REF_LIST_LIMIT = 100; const EMPTY_REFS: ReadonlyArray = []; @@ -390,6 +394,22 @@ export function useProjectContentSearch(target: ProjectContentSearchTarget) { }) : null, ); + // Only once the timeout has come back: a refresh restarts a search still in flight. + const settledScanTimeout = + !result.isPending && + isProjectSearchContentsError(result.failure) && + result.failure.failure === "search_index_scan_timed_out" + ? result.failure + : null; + const { refresh } = result; + // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- retry timer: nothing tells the client when the server's first scan ends + useEffect(() => { + if (settledScanTimeout === null) return; + const timer = window.setTimeout(refresh, PROJECT_CONTENT_SEARCH_SCAN_RETRY_MS); + return () => { + window.clearTimeout(timer); + }; + }, [refresh, settledScanTimeout]); return { canReadFiles, From e101cae1b9e96049eabb342eecd23283913a1b55 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 15 Nov 2026 09:43:53 +0100 Subject: [PATCH 011/446] fix(terminal): show shell startup failures across attachments (#37088) Co-authored-by: Julius Marminge --- .../terminal/ThreadTerminalRouteScreen.tsx | 2 +- apps/server/src/terminal/Manager.test.ts | 35 +++++++ apps/server/src/terminal/Manager.ts | 7 +- .../components/ThreadTerminalDrawer.test.ts | 30 ++++++ .../src/components/ThreadTerminalDrawer.tsx | 9 +- .../src/state/terminalSession.test.ts | 9 ++ .../src/state/terminalSession.ts | 2 +- packages/contracts/src/terminal.ts | 2 + .../terminal-shows-startup-failure.e2e.ts | 99 +++++++++++++++++++ 9 files changed, 190 insertions(+), 5 deletions(-) create mode 100644 scripts/scenarios/terminal-shows-startup-failure.e2e.ts diff --git a/apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx b/apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx index 173e0cb62c624..82dc526d3de0c 100644 --- a/apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx +++ b/apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx @@ -893,7 +893,7 @@ function ThreadTerminal(props: { title="No terminal sessions" detail="Existing terminals will appear here when another client opens one." /> - ) : !canOperateTerminal && terminal.error !== null ? ( + ) : terminal.error !== null && (!canOperateTerminal || terminal.status === "error") ? ( ) : ( <> diff --git a/apps/server/src/terminal/Manager.test.ts b/apps/server/src/terminal/Manager.test.ts index b0dce3035ff76..e7c273569a443 100644 --- a/apps/server/src/terminal/Manager.test.ts +++ b/apps/server/src/terminal/Manager.test.ts @@ -1940,6 +1940,41 @@ it.layer( }), ); + it.effect("keeps a startup failure in the snapshot every later attachment receives", () => + Effect.gen(function* () { + const { manager, ptyAdapter } = yield* createManager(); + ptyAdapter.spawnFailures.push(new Error("forkpty(3) failed.")); + const startupFailure = { + type: "snapshot", + snapshot: { + status: "error", + error: expect.stringContaining("Failed to spawn PTY process"), + }, + }; + const record = (events: TerminalAttachStreamEvent[]) => (event: TerminalAttachStreamEvent) => + Effect.sync(() => events.push(event)); + + const creating: TerminalAttachStreamEvent[] = []; + (yield* manager.attachStream(openInput(), record(creating)))(); + const resumed: TerminalAttachStreamEvent[] = []; + (yield* manager.attachStream({ ...openInput(), resume: true }, record(resumed)))(); + const observed: TerminalAttachStreamEvent[] = []; + (yield* manager.observeStream( + { threadId: "thread-1", terminalId: DEFAULT_TERMINAL_ID }, + record(observed), + ))(); + expect([creating, resumed, observed]).toMatchObject([ + [startupFailure], + [startupFailure], + [startupFailure], + ]); + + const restarted = yield* manager.open(openInput()); + expect(restarted.status).toBe("running"); + expect(restarted).not.toHaveProperty("error"); + }), + ); + it.effect("retries with fallback shells when preferred shell spawn fails", () => Effect.gen(function* () { const platform = yield* HostProcess.Platform; diff --git a/apps/server/src/terminal/Manager.ts b/apps/server/src/terminal/Manager.ts index 625f9a3bf190b..d034af98ee1f3 100644 --- a/apps/server/src/terminal/Manager.ts +++ b/apps/server/src/terminal/Manager.ts @@ -316,6 +316,7 @@ interface TerminalSessionState { childCommandLabel: string | null; runtimeEnv: Record | null; stopOnSettle: boolean; + startupError: string | null; } interface TerminalGrid { @@ -411,6 +412,7 @@ function snapshot(session: TerminalSessionState): TerminalSessionSnapshot { sequence: session.eventSequence, cols: session.cols, rows: session.rows, + ...(session.startupError === null ? {} : { error: session.startupError }), }; } @@ -2680,6 +2682,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func const startingAt = yield* nowIso; yield* modifyManagerState((state) => { session.status = "starting"; + session.startupError = null; session.shell = null; session.cwd = input.cwd; session.worktreePath = input.worktreePath ?? null; @@ -2800,6 +2803,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func { const error = startResult.failure; + const message = error.message; if (ptyProcess) { yield* startKillEscalation(ptyProcess, session.threadId, session.terminalId); } @@ -2807,6 +2811,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func yield* modifyManagerState((state) => { cleanupProcessHandles(session); session.status = "error"; + session.startupError = message; session.pid = null; session.process = null; session.hasRunningSubprocess = false; @@ -2824,7 +2829,6 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func yield* evictInactiveSessionsIfNeeded(); - const message = error.message; yield* publishEvent({ type: "error", threadId: session.threadId, @@ -3143,6 +3147,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func childCommandLabel: null, runtimeEnv: normalizedRuntimeEnv(input.env), stopOnSettle: input.stopOnSettle ?? false, + startupError: null, }; const createdSession = session; diff --git a/apps/web/src/components/ThreadTerminalDrawer.test.ts b/apps/web/src/components/ThreadTerminalDrawer.test.ts index 9f0204cd70c30..a88fa94521085 100644 --- a/apps/web/src/components/ThreadTerminalDrawer.test.ts +++ b/apps/web/src/components/ThreadTerminalDrawer.test.ts @@ -98,4 +98,34 @@ it("reports an attach failure that leaves the output unchanged", () => { expect(terminal.getSelection).not.toHaveBeenCalled(); expect(terminal.getSelectionCellCount).not.toHaveBeenCalled(); expect(terminal.clearSelection).not.toHaveBeenCalled(); + + const snapshot = { + threadId: "thread", + terminalId: "term-1", + cwd: "/repo", + worktreePath: null, + status: "error" as const, + pid: null, + history: "", + exitCode: null, + exitSignal: null, + label: "Terminal", + updatedAt: "2026-11-15T00:00:00.000Z", + error: "Terminal attach failed", + }; + const attached = applyTerminalAttachStreamEvent(nextTerminalAttachSeedState(), { + type: "snapshot", + snapshot, + }); + const cursor = synchronizeTerminalSession( + terminal, + failed, + attached, + INITIAL_TERMINAL_OUTPUT_CURSOR, + ); + terminal.write.mockClear(); + const reconnected = applyTerminalAttachStreamEvent(attached, { type: "snapshot", snapshot }); + synchronizeTerminalSession(terminal, attached, reconnected, cursor); + expect(terminal.resetAndWrite).toHaveBeenLastCalledWith(""); + expect(terminal.write).toHaveBeenCalledWith("\r\n[terminal] Terminal attach failed\r\n"); }); diff --git a/apps/web/src/components/ThreadTerminalDrawer.tsx b/apps/web/src/components/ThreadTerminalDrawer.tsx index e3008b6ce98ee..e5de4ea090b44 100644 --- a/apps/web/src/components/ThreadTerminalDrawer.tsx +++ b/apps/web/src/components/ThreadTerminalDrawer.tsx @@ -191,7 +191,7 @@ export function synchronizeTerminalOutput( return update.cursor; } -/** Writes what changed between two session notifications: new output, then a new error. */ +/** Writes what changed between two session notifications: new output, then the error, again after a reset. */ export function synchronizeTerminalSession( terminal: Parameters[0], previous: Pick, @@ -203,7 +203,12 @@ export function synchronizeTerminalSession( ? cursor : synchronizeTerminalOutput(terminal, session, cursor); // An attach failure can change only the error, keeping the last buffer. - if (session.error !== null && session.error !== previous.error) { + if ( + session.error !== null && + (session.error !== previous.error || + nextCursor.generation !== cursor.generation || + nextCursor.resetVersion !== cursor.resetVersion) + ) { writeSystemMessage(terminal, session.error); } return nextCursor; diff --git a/packages/client-runtime/src/state/terminalSession.test.ts b/packages/client-runtime/src/state/terminalSession.test.ts index 12fc73d261911..d4e850b9c8b0a 100644 --- a/packages/client-runtime/src/state/terminalSession.test.ts +++ b/packages/client-runtime/src/state/terminalSession.test.ts @@ -72,6 +72,15 @@ describe("terminal session reducers", () => { }); }); + it("keeps a startup failure carried by an attach snapshot", () => { + const attached = applyTerminalAttachStreamEvent(nextTerminalAttachSeedState(), { + type: "snapshot", + snapshot: { ...BASE_SNAPSHOT, status: "error", pid: null, error: "Failed to spawn PTY." }, + }); + + expect(attached).toMatchObject({ status: "error", error: "Failed to spawn PTY." }); + }); + it("follows the shared PTY grid from snapshots and activity without disturbing output", () => { const attached = applyTerminalAttachStreamEvent(nextTerminalAttachSeedState(), { type: "snapshot", diff --git a/packages/client-runtime/src/state/terminalSession.ts b/packages/client-runtime/src/state/terminalSession.ts index a4057519c199b..cedc25ec62d48 100644 --- a/packages/client-runtime/src/state/terminalSession.ts +++ b/packages/client-runtime/src/state/terminalSession.ts @@ -118,7 +118,7 @@ function terminalBufferStateFromSnapshot( output: resetOutput(current.output, snapshot.history, maxBufferBytes), grid: nextGrid(current.grid, snapshot), status: snapshot.status, - error: null, + error: snapshot.error ?? null, updatedAt: snapshot.updatedAt, version: current.version + 1, lifecycleVersion: current.lifecycleVersion, diff --git a/packages/contracts/src/terminal.ts b/packages/contracts/src/terminal.ts index 84ca7477ea808..91e952326a05c 100644 --- a/packages/contracts/src/terminal.ts +++ b/packages/contracts/src/terminal.ts @@ -232,6 +232,8 @@ export const TerminalSessionSnapshot = Schema.Struct({ // The PTY grid every renderer of this terminal should draw; absent from older servers. cols: Schema.optional(TerminalColsSchema), rows: Schema.optional(TerminalRowsSchema), + // Why the shell failed to start, kept so later attachments can show it. + error: Schema.optional(Schema.String.check(Schema.isNonEmpty())), }); export type TerminalSessionSnapshot = typeof TerminalSessionSnapshot.Type; diff --git a/scripts/scenarios/terminal-shows-startup-failure.e2e.ts b/scripts/scenarios/terminal-shows-startup-failure.e2e.ts new file mode 100644 index 0000000000000..05d6df28bb0ac --- /dev/null +++ b/scripts/scenarios/terminal-shows-startup-failure.e2e.ts @@ -0,0 +1,99 @@ +/// +// 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 * as Path from "effect/Path"; +import * as Schema from "effect/Schema"; + +import { APP_DIR, T3_HOME } from "../e2e.config.ts"; +import { run } from "./shell.ts"; +import { runPromise, test, WEB } from "./t3.ts"; + +const decodeRuntime = Schema.decodeUnknownEffect( + Schema.fromJsonString(Schema.Struct({ pid: Schema.Int })), +); + +/** The scenario's own server: the pid its T3 home records, running from the app checkout. */ +const scenarioServerPid = Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const runtime = yield* Effect.firstSuccessOf( + ["userdata", "dev"].map((dir) => + fs.readFileString(path.join(T3_HOME, dir, "server-runtime.json")), + ), + ); + const { pid } = yield* decodeRuntime(runtime); + const cwd = yield* fs.readLink(`/proc/${pid}/cwd`); + if (!cwd.startsWith(path.resolve(APP_DIR))) { + return yield* Effect.die(new Error(`pid ${pid} runs in ${cwd}, not ${APP_DIR}`)); + } + return pid; +}); + +/** + * Linux only: leaves the server no free file descriptor, so node-pty's forkpty(3) fails as it does when a + * host runs out. Returns the restore; only this process's soft limit changes. + */ +const exhaustFileDescriptors = Effect.fn("scenario.exhaustFileDescriptors")(function* ( + pid: number, +) { + const limits = yield* run("prlimit", [ + `--pid=${pid}`, + "--nofile", + "--raw", + "--noheadings", + "--output=SOFT,HARD", + ]); + const [soft = "", hard = ""] = limits.stdout.trim().split(/\s+/); + yield* run("prlimit", [`--pid=${pid}`, `--nofile=0:${hard}`]); + return run("prlimit", [`--pid=${pid}`, `--nofile=${soft}:${hard}`]); +}); + +test( + "A terminal whose shell cannot start says why, also after its drawer reopens", + WEB, + async ({ t3, app, browser, screen }) => { + const project = await t3.project("demo"); + const thread = await t3.thread({ project, title: "Shell", messages: ["hello"] }); + await t3.open(thread); + const pid = await runPromise(scenarioServerPid); + + const toggle = screen.getByRole("button", "Toggle terminal drawer"); + const terminal = screen.getByLabel("Terminal input"); + // The canvas has no DOM text: select all and read what the terminal's copy hands over. + const terminalText = async () => { + await browser.evaluate(() => { + delete document.body.dataset.terminalCopy; + window.addEventListener( + "copy", + (event) => { + document.body.dataset.terminalCopy = event.clipboardData?.getData("text/plain") ?? ""; + }, + { once: true }, + ); + return null; + }); + await terminal.focus(); + await browser.keyboard.press("Control+Shift+A"); + await browser.keyboard.press("Control+Shift+C"); + return browser.evaluate(() => document.body.dataset.terminalCopy ?? ""); + }; + + await expect(toggle).toBeVisible(); + const restore = await runPromise(exhaustFileDescriptors(pid)); + try { + await toggle.tap(); + await expect.poll(terminalText, { timeout: 15_000 }).toContain("Failed to spawn PTY process"); + } finally { + await runPromise(restore); + } + await app.screenshot("startup-failure"); + + await toggle.tap(); + await expect(terminal).toBeHidden(); + await toggle.tap(); + await expect.poll(terminalText, { timeout: 15_000 }).toContain("Failed to spawn PTY process"); + await app.screenshot("after-reopen"); + }, +); From 4f829644f668f7e3ac2ed0351003c7de722ca09d Mon Sep 17 00:00:00 2001 From: Valtrizt Khalifah Warri <108137942+valuin@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:57:21 +0700 Subject: [PATCH 012/446] perf(server): long threads open about 2.5x faster (#34014) Co-authored-by: maria-rcks Co-authored-by: Julius Marminge --- .../orchestration-v2/ProjectionStore.test.ts | 100 ++++++++++++++---- .../src/orchestration-v2/ProjectionStore.ts | 47 +++++--- apps/server/src/persistence/Migrations.ts | 2 + .../094_TurnItemMessageOrdinalIndex.test.ts | 2 +- .../095_TurnItemInlineWorkOutput.test.ts | 47 ++++++++ .../095_TurnItemInlineWorkOutput.ts | 23 ++++ .../reconcileV2PreviewMigration.test.ts | 2 + 7 files changed, 185 insertions(+), 38 deletions(-) create mode 100644 apps/server/src/persistence/Migrations/095_TurnItemInlineWorkOutput.test.ts create mode 100644 apps/server/src/persistence/Migrations/095_TurnItemInlineWorkOutput.ts diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts index ea95f8aafca1f..b961ef1e0dc34 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts @@ -44,6 +44,7 @@ import * as Tracer from "effect/Tracer"; import * as SqlClient from "effect/sql/SqlClient"; import * as Statement from "effect/sql/Statement"; import { projectThreadAwarenessV2 } from "@t3tools/shared/agentAwareness"; +import { MCP_APP_OUTPUT_KEY } from "@t3tools/shared/mcpApp"; import * as SqlitePersistence from "../persistence/Sqlite.ts"; import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts"; @@ -4327,13 +4328,13 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { ordinalsFrom(915, 75), ); assert.strictEqual(decodeThreadHistoryCursor(page.nextCursor!).p, 914); - // A jump to an entry reads the full index and leaves it cached. With work, it scans - // payloads for inline output only in its page's runs, not in a run after its cursor. + // A jump to an entry reads the full index and leaves it cached. With work, it looks for + // inline output only in its page's runs, not in a run after its cursor. const scannedRuns: Array = []; yield* read("turn-item:bounded-sql-history:950").pipe( Effect.provideService(Statement.CurrentTransformer, (statement) => { const [query, params] = statement.compile(); - if (query.includes("LIKE '%htmlRender%'")) scannedRuns.push(String(params[0])); + if (query.includes("item.inline_work_output = 1")) scannedRuns.push(String(params[0])); return Effect.succeed(statement); }), ); @@ -4558,17 +4559,49 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { workCursor = page.nextCursor; } assert.strictEqual(workIds.size, 999); - for (const [toolName, output] of [ - ["t3-code.delegate_task", null], - ["mcp__t3_code__delegate_task", null], - ["mcp__t3-code__send_to_user", null], + const secondItemId = TurnItemId.make("turn-item:bounded-sql-history:2"); + const secondItem = { + id: secondItemId, + threadId, + runId: (yield* projectionStore.getTurnItem({ threadId, itemId: secondItemId }))!.runId, + nodeId: null, + providerThreadId: null, + providerTurnId: null, + nativeItemRef: null, + parentItemId: null, + ordinal: 2, + status: "completed" as const, + title: "command 2", + startedAt: now, + completedAt: now, + updatedAt: now, + }; + const writeSecondItem = (item: OrchestrationV2TurnItem) => + projectionStore.apply({ + id: EventId.make(`event:bounded-sql-history:item-2:${item.type}`), + type: "turn-item.updated", + threadId, + runId: secondItem.runId ?? undefined, + driver, + occurredAt: now, + payload: item, + }); + for (const inline of [ + { toolName: "t3-code.delegate_task" }, + { toolName: "mcp__t3_code__delegate_task" }, + { toolName: "mcp__docs__search", output: { next: "Delegate-Task" } }, + { toolName: "view_image", viewedImagePath: "/repo/screenshot.png" }, + { toolName: "mcp__docs__chart", output: { [MCP_APP_OUTPUT_KEY]: { uri: "ui://chart" } } }, + { toolName: "mcp__t3-code__send_to_user" }, // OpenCode Code Mode reports a nested send_to_user call as one `execute` item. - ["execute", JSON.stringify({ sentMessage: "Draft" })], - ] as const) { - yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'dynamic_tool', - payload_json = json_set(payload_json, '$.type', 'dynamic_tool', '$.toolName', ${toolName}, - '$.output', ${output}) - WHERE thread_id = ${threadId} AND ordinal = 2`; + { toolName: "execute", output: JSON.stringify({ sentMessage: "Draft" }) }, + ]) { + yield* writeSecondItem({ + ...secondItem, + type: "dynamic_tool", + input: {}, + ...inline, + }); const window = yield* projectionStore.getThreadSnapshotWindow(threadId, { rowLimit: sqlPageLimit, userTurnLimit: 10, @@ -4590,8 +4623,8 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { }); const page = yield* historyPage(threadId, newestCursor, undefined, false, "deferred"); assert.isUndefined(page.items.find((row) => row.item.ordinal === 2)?.item.deferredWork); - // Tool payloads hold megabytes, so a page that reads the full index, as a jump to an - // entry does, looks for inline output only in its own runs. + // A page that reads the full index, as a jump to an entry does, looks for inline output + // only in its own runs. const pageRead = traceSqlStatements(); yield* historyPage( threadId, @@ -4601,15 +4634,15 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { "deferred", ).pipe(Effect.withTracer(pageRead.tracer)); const payloadScans = pageRead.statements.filter((statement) => - statement.includes("LIKE '%htmlRender%'"), + statement.includes("item.inline_work_output = 1"), ); assert.lengthOf(payloadScans, 1); assert.include(payloadScans[0], "item.run_id IS json_extract(wanted.value, '$.id')"); } // Until its run is scanned, a cursor on inline output is hidden inside a work group. yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'dynamic_tool', - payload_json = json_set(payload_json, '$.type', 'dynamic_tool', '$.toolName', 't3-code.delegate_task') - WHERE thread_id = ${threadId} AND ordinal = 4`; + payload_json = json_set(payload_json, '$.type', 'dynamic_tool', '$.toolName', 't3-code.delegate_task'), + inline_work_output = 1 WHERE thread_id = ${threadId} AND ordinal = 4`; const sharedRunId = "run:bounded-sql-history:1"; // A work page of rows that have no run leaves their inline output out too. yield* sql`UPDATE orchestration_v2_projection_turn_items SET run_id = NULL @@ -4670,8 +4703,8 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { '$.runId', CASE WHEN ordinal <= 700 THEN 'run:bounded-sql-history:300' ELSE ${sharedRunId} END) WHERE thread_id = ${threadId} AND ordinal >= 100`; yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'dynamic_tool', - payload_json = json_set(payload_json, '$.type', 'dynamic_tool', '$.toolName', 't3-code.delegate_task') - WHERE thread_id = ${threadId} AND ordinal = 720`; + payload_json = json_set(payload_json, '$.type', 'dynamic_tool', '$.toolName', 't3-code.delegate_task'), + inline_work_output = 1 WHERE thread_id = ${threadId} AND ordinal = 720`; const beforeMissingCursor = yield* historyPage( threadId, encodeThreadHistoryCursor({ @@ -4690,7 +4723,15 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { payload_json = json_set(payload_json, '$.status', 'completed', '$.runId', ${sharedRunId}) WHERE thread_id = ${threadId} AND ordinal >= 100`; yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'command_execution', - payload_json = json_set(payload_json, '$.type', 'command_execution') WHERE thread_id = ${threadId} AND ordinal IN (2, 4, 720)`; + payload_json = json_set(payload_json, '$.type', 'command_execution'), inline_work_output = 0 + WHERE thread_id = ${threadId} AND ordinal IN (4, 720)`; + yield* writeSecondItem({ + ...secondItem, + type: "command_execution", + input: "echo 2", + output: "ok", + exitCode: 0, + }); for (const [ordinal, status] of [ [0, "completed"], [1, "superseded"], @@ -4727,6 +4768,23 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { }); const oldWork = window.projection.visibleTurnItems.find((row) => row.item.deferredWork); assert.strictEqual(oldWork?.item.deferredWork?.itemCount, 987); + // A jump to an entry reads the full index, which folds the same group. + const jump = yield* historyPage( + threadId, + encodeThreadHistoryCursor({ + snapshotSequence: 0, + sourceThreadId: threadId, + sourceItemId: "turn-item:bounded-sql-history:1000", + position: 1000, + }), + oldWork!.sourceItemId, + false, + "deferred", + ); + assert.strictEqual( + jump.items.find((row) => row.item.deferredWork)?.item.deferredWork?.itemCount, + 987, + ); let cursor: string | null = oldWork!.item.deferredWork!.cursor; const loaded = new Set(); for (let pages = 0; cursor !== null; pages += 1) { diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts index a3fcd139fd5b0..5b71a6ac042f1 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -2178,18 +2178,31 @@ function shellFromState(input: { }; } +// Dynamic tool output the client renders inline (HTML, MCP apps, viewed images, delegated +// tasks, messages sent to the user), so its work group never defers. Mirrors the LIKE backfill +// in migration 095, where a tool name counts anywhere after the first toolName. Searching from +// that first one once avoids rescanning the payload for every nested toolName. +const INLINE_WORK_OUTPUT = new RegExp( + `htmlRender|${MCP_APP_OUTPUT_KEY}|"viewedImagePath"|sentMessage`, + "i", +); +const TOOL_NAME_PREFIX = /"toolName":"/i; +const INLINE_TOOL_NAME = /(?:delegate.task|send.to.user)"/is; + +const isInlineWorkOutput = (payloadJson: string) => { + if (INLINE_WORK_OUTPUT.test(payloadJson)) return true; + const toolName = TOOL_NAME_PREFIX.exec(payloadJson); + return ( + toolName !== null && + INLINE_TOOL_NAME.test(payloadJson.slice(toolName.index + toolName[0].length)) + ); +}; + export const layer: Layer.Layer = Layer.effect( ProjectionStoreV2, Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; - const inlineWorkOutput = sql`item.type = 'dynamic_tool' AND ( - item.payload_json LIKE '%htmlRender%' OR item.payload_json LIKE ${`%${MCP_APP_OUTPUT_KEY}%`} - OR item.payload_json LIKE '%"viewedImagePath"%' - OR item.payload_json LIKE '%"toolName":"%delegate_task"%' - OR item.payload_json LIKE '%"toolName":"%send_to_user"%' - OR item.payload_json LIKE '%sentMessage%')`; - // SQLite uses the partial conversation index of migration 082 only for this exact predicate. const CONVERSATION_INDEX_EXCLUDED_TYPES = sql.literal( `(${DEFERRED_WORK_ITEM_TYPES.map((type) => `'${type}'`).join(", ")})`, @@ -2939,7 +2952,8 @@ export const layer: Layer.Layer = type, status, updated_at, - payload_json + payload_json, + inline_work_output ) VALUES ( ${event.payload.id}, @@ -2953,7 +2967,8 @@ export const layer: Layer.Layer = ${event.payload.type}, ${event.payload.status}, ${updatedAt}, - ${payloadJson} + ${payloadJson}, + ${event.payload.type === "dynamic_tool" && isInlineWorkOutput(payloadJson) ? 1 : 0} ) ON CONFLICT(turn_item_id) DO UPDATE SET @@ -2967,7 +2982,8 @@ export const layer: Layer.Layer = type = excluded.type, status = excluded.status, updated_at = excluded.updated_at, - payload_json = excluded.payload_json + payload_json = excluded.payload_json, + inline_work_output = excluded.inline_work_output `; break; } @@ -3189,7 +3205,8 @@ export const layer: Layer.Layer = const eligible = sql` WITH eligible AS NOT MATERIALIZED ( SELECT item.rowid AS item_rowid, item.payload_json, item.ordinal, item.turn_item_id, - item.run_id, item.node_id, item.type, item.status, run.status AS run_status, + item.run_id, item.node_id, item.type, item.status, item.inline_work_output, + run.status AS run_status, attempt.attempt_id, attempt.status AS attempt_status FROM orchestration_v2_projection_turn_items AS item LEFT JOIN orchestration_v2_projection_runs AS run @@ -3480,7 +3497,7 @@ export const layer: Layer.Layer = OVER (PARTITION BY run_id ORDER BY ordinal) AS segment, CASE WHEN status = 'completed' AND (run_status = 'completed' OR attempt_status = 'superseded') AND type IN ${sql.in(DEFERRED_WORK_ITEM_TYPES)} - AND NOT (${inlineWorkOutput}) + AND inline_work_output = 0 AND run_id NOT IN (SELECT run_id FROM blocked_runs) THEN 1 ELSE 0 END AS can_defer FROM eligible AS item @@ -6757,9 +6774,7 @@ export const layer: Layer.Layer = }>`SELECT item.turn_item_id, item.run_id, item.node_id, item.parent_item_id, item.type, item.status, item.ordinal, ${includePersistentOutput ? sql`attempt.attempt_id` : sql`NULL`} AS attempt_id, ${includePersistentOutput ? sql`attempt.status` : sql`NULL`} AS attempt_status, - ${ - includePersistentOutput ? sql`CASE WHEN ${inlineWorkOutput} THEN 1 ELSE 0 END` : sql`0` - } AS persistent_output, + ${includePersistentOutput ? sql`item.inline_work_output` : sql`0`} AS persistent_output, CASE WHEN item.type = 'user_message' THEN json_extract(item.payload_json, '$.inputIntent') END AS input_intent, CASE WHEN item.type = 'user_message' THEN json_extract(item.payload_json, '$.createdBy') END AS created_by, CASE WHEN item.type IN ('user_message', 'assistant_message') THEN json_extract(item.payload_json, '$.messageId') END AS message_id @@ -7313,7 +7328,7 @@ export const layer: Layer.Layer = CROSS JOIN orchestration_v2_projection_turn_items AS item ON item.thread_id = json_extract(wanted.value, '$.threadId') AND item.run_id IS json_extract(wanted.value, '$.id') - WHERE item.status = 'completed' AND ${inlineWorkOutput}`; + WHERE item.status = 'completed' AND item.inline_work_output = 1`; if (found.length === 0) break; for (const row of found) persistentOutputs.add(`${row.thread_id}:${row.turn_item_id}`); diff --git a/apps/server/src/persistence/Migrations.ts b/apps/server/src/persistence/Migrations.ts index 0e6b502285113..9eaddd70c1278 100644 --- a/apps/server/src/persistence/Migrations.ts +++ b/apps/server/src/persistence/Migrations.ts @@ -102,6 +102,7 @@ import Migration0091 from "./Migrations/091_AuthConnectDeniedRequests.ts"; import Migration0092 from "./Migrations/092_QueuedTurnInputIndexJsonValid.ts"; import Migration0093 from "./Migrations/093_ProviderTurnAttemptIndex.ts"; import Migration0094 from "./Migrations/094_TurnItemMessageOrdinalIndex.ts"; +import Migration0095 from "./Migrations/095_TurnItemInlineWorkOutput.ts"; const migrationEntries = [ [1, "OrchestrationEvents", Migration0001], @@ -199,6 +200,7 @@ const migrationEntries = [ [92, "QueuedTurnInputIndexJsonValid", Migration0092], [93, "ProviderTurnAttemptIndex", Migration0093], [94, "TurnItemMessageOrdinalIndex", Migration0094], + [95, "TurnItemInlineWorkOutput", Migration0095], ] as const; export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const); diff --git a/apps/server/src/persistence/Migrations/094_TurnItemMessageOrdinalIndex.test.ts b/apps/server/src/persistence/Migrations/094_TurnItemMessageOrdinalIndex.test.ts index 1a8ea4ecfed43..5d76b7d1a95b9 100644 --- a/apps/server/src/persistence/Migrations/094_TurnItemMessageOrdinalIndex.test.ts +++ b/apps/server/src/persistence/Migrations/094_TurnItemMessageOrdinalIndex.test.ts @@ -43,7 +43,7 @@ it.effect("migrates over a message whose payload is not JSON, and takes one afte yield* insertTurnItem("ok-1", "thread-a", 2, "user_message", '{"messageId":"m1"}'); assert.deepEqual( - (yield* runMigrations()).map(([id]) => id), + (yield* runMigrations({ toMigrationInclusive: 94 })).map(([id]) => id), [94], ); yield* insertTurnItem("bad-2", "thread-a", 3, "user_message", "{not json"); diff --git a/apps/server/src/persistence/Migrations/095_TurnItemInlineWorkOutput.test.ts b/apps/server/src/persistence/Migrations/095_TurnItemInlineWorkOutput.test.ts new file mode 100644 index 0000000000000..2b07b076bf0cc --- /dev/null +++ b/apps/server/src/persistence/Migrations/095_TurnItemInlineWorkOutput.test.ts @@ -0,0 +1,47 @@ +import { assert, it } from "@effect/vitest"; +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; +import * as Effect from "effect/Effect"; +import * as SqlClient from "effect/sql/SqlClient"; + +import { runMigrations } from "../Migrations.ts"; + +const layer = it.layer(NodeSqliteClient.layer({ filename: ":memory:" })); + +layer("095_TurnItemInlineWorkOutput", (it) => { + it.effect("flags existing dynamic tools whose output renders inline", () => + Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + yield* runMigrations({ toMigrationInclusive: 94 }); + const rows: ReadonlyArray = [ + ["delegate", "dynamic_tool", { toolName: "mcp__t3_code__delegate_task" }, 1], + ["html", "dynamic_tool", { toolName: "x", output: { htmlRender: {} } }, 1], + ["mcp-app", "dynamic_tool", { toolName: "x", output: { t3McpApp: {} } }, 1], + ["image", "dynamic_tool", { toolName: "view_image", viewedImagePath: "/a.png" }, 1], + ["send", "dynamic_tool", { toolName: "mcp__t3-code__send_to_user" }, 1], + ["sent", "dynamic_tool", { toolName: "execute", output: '{"sentMessage":"Hi"}' }, 1], + ["plain", "dynamic_tool", { toolName: "mcp__docs__search", output: "ok" }, 0], + ["command", "command_execution", { output: "htmlRender" }, 0], + ]; + for (const [id, type, payload] of rows) { + yield* sql` + INSERT INTO orchestration_v2_projection_turn_items ( + turn_item_id, thread_id, ordinal, type, status, updated_at, payload_json + ) VALUES ( + ${id}, 'thread-1', 0, ${type}, 'completed', '2026-11-01T00:00:00.000Z', + ${JSON.stringify({ type, ...payload })} + ) + `; + } + + yield* runMigrations({ toMigrationInclusive: 95 }); + const flagged = yield* sql<{ readonly turn_item_id: string; readonly inline: number }>` + SELECT turn_item_id, inline_work_output AS inline + FROM orchestration_v2_projection_turn_items + `; + assert.deepStrictEqual( + new Map(flagged.map((row) => [row.turn_item_id, row.inline])), + new Map(rows.map(([id, , , inline]) => [id, inline])), + ); + }), + ); +}); diff --git a/apps/server/src/persistence/Migrations/095_TurnItemInlineWorkOutput.ts b/apps/server/src/persistence/Migrations/095_TurnItemInlineWorkOutput.ts new file mode 100644 index 0000000000000..731dbcc7df1cc --- /dev/null +++ b/apps/server/src/persistence/Migrations/095_TurnItemInlineWorkOutput.ts @@ -0,0 +1,23 @@ +import * as Effect from "effect/Effect"; +import * as SqlClient from "effect/sql/SqlClient"; + +export default Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + + // Thread windows read this flag for every row; scanning payloads on each read + // dominated thread open. Keep the predicate in step with ProjectionStore. + yield* sql` + ALTER TABLE orchestration_v2_projection_turn_items + ADD COLUMN inline_work_output INTEGER NOT NULL DEFAULT 0 + `; + yield* sql` + UPDATE orchestration_v2_projection_turn_items + SET inline_work_output = 1 + WHERE type = 'dynamic_tool' AND ( + payload_json LIKE '%htmlRender%' OR payload_json LIKE '%t3McpApp%' + OR payload_json LIKE '%"viewedImagePath"%' + OR payload_json LIKE '%"toolName":"%delegate_task"%' + OR payload_json LIKE '%"toolName":"%send_to_user"%' + OR payload_json LIKE '%sentMessage%') + `; +}); diff --git a/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts b/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts index 953a2229ce26e..4c5ea36726c4a 100644 --- a/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts +++ b/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts @@ -74,6 +74,7 @@ describe("V2 preview upgrade", () => { [92, "QueuedTurnInputIndexJsonValid"], [93, "ProviderTurnAttemptIndex"], [94, "TurnItemMessageOrdinalIndex"], + [95, "TurnItemInlineWorkOutput"], ]); assert.deepStrictEqual(yield* runMigrations(), []); assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports); @@ -191,6 +192,7 @@ describe("V2 preview upgrade", () => { [92, "QueuedTurnInputIndexJsonValid"], [93, "ProviderTurnAttemptIndex"], [94, "TurnItemMessageOrdinalIndex"], + [95, "TurnItemInlineWorkOutput"], ]); }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))), ); From 50a2f5b9f415665f48b4d71df4ff68c858578199 Mon Sep 17 00:00:00 2001 From: Valtrizt Khalifah Warri <108137942+valuin@users.noreply.github.com> Date: Sun, 15 Nov 2026 16:00:00 +0700 Subject: [PATCH 013/446] perf(server): long threads open about 2.5x faster (#34014) Co-authored-by: maria-rcks Co-authored-by: Julius Marminge From 45649628dcb7d07cf6b91d94f94d327037a2967f Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 15 Nov 2026 01:08:10 -0800 Subject: [PATCH 014/446] fix(mobile): inline markdown images page through their message's images (#37117) Co-authored-by: Claude Opus 5.5 --- .../src/features/threads/ThreadFeed.tsx | 22 +++++++++++++++---- apps/mobile/src/lib/filePreview.test.ts | 1 + 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/apps/mobile/src/features/threads/ThreadFeed.tsx b/apps/mobile/src/features/threads/ThreadFeed.tsx index 36d2293f3a2de..987ef1150a533 100644 --- a/apps/mobile/src/features/threads/ThreadFeed.tsx +++ b/apps/mobile/src/features/threads/ThreadFeed.tsx @@ -1092,7 +1092,11 @@ interface MarkdownLinkHandlers { actionId: string, reply?: MarkdownLinkReply, ) => void; - readonly onImagePress?: (href: string) => void; + /** `withGallery` pages the image through its message's images; the feed's viewer sits outside that scope. */ + readonly onImagePress?: ( + href: string, + withGallery?: (source: FilePreviewSource) => FilePreviewSource, + ) => void; readonly resolveImageSource?: MarkdownImageSourceResolver; readonly fileEmbedLabel?: (href: string, standalone: boolean) => string | null; readonly linkCustomization?: (href: string) => MarkdownLinkCustomization | undefined; @@ -1174,8 +1178,11 @@ const AssistantMarkdownContent = memo(function AssistantMarkdownContent(props: { const models = useModelMentions(props.environmentId, props.markdown); const liveMarkdown = useLiveThreadLinkLabels(props.markdown, props.environmentId); const threadIcons = useThreadChipIcons(props.environmentId, liveMarkdown); + const withGallery = useFilePreviewGallery(); + const onImagePress = props.linkHandlers.onImagePress; const linkHandlers: MarkdownLinkHandlers = { ...props.linkHandlers, + ...(onImagePress ? { onImagePress: (href: string) => onImagePress(href, withGallery) } : {}), onLinkPress: (href) => props.linkHandlers.onLinkPress(href, props.reply), onFileContextMenuAction: (href, actionId) => props.linkHandlers.onFileContextMenuAction(href, actionId, props.reply), @@ -3009,7 +3016,12 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { }; return lookup().catch(() => relativePath); }; - const onMarkdownLinkPress = (href: string, imageEmbed = false, reply?: MarkdownLinkReply) => { + const onMarkdownLinkPress = ( + href: string, + imageEmbed = false, + reply?: MarkdownLinkReply, + withGallery?: (source: FilePreviewSource) => FilePreviewSource, + ) => { markdownLinkRequest.current?.abort(); const request = new AbortController(); markdownLinkRequest.current = request; @@ -3042,7 +3054,9 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { if (request.signal.aborted) return; openPreview( scope, - media.kind === "video" ? { video: media.source } : { file: media.source }, + media.kind === "video" + ? { video: media.source } + : { file: withGallery ? withGallery(media.source) : media.source }, ); }; const resource = "resource" in media.source ? media.source.resource : null; @@ -3117,7 +3131,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { ), onLinkPress: (href, reply) => onMarkdownLinkPress(href, false, reply), linkCustomization: enrichedThreadLinkCustomization, - onImagePress: (href) => onMarkdownLinkPress(href, true), + onImagePress: (href, withGallery) => onMarkdownLinkPress(href, true, undefined, withGallery), resolveImageSource, fileContextMenu: (href) => { const skill = enrichedSkillReference(href, props.skills); diff --git a/apps/mobile/src/lib/filePreview.test.ts b/apps/mobile/src/lib/filePreview.test.ts index 2c7a7313e47e9..d175f272e3dec 100644 --- a/apps/mobile/src/lib/filePreview.test.ts +++ b/apps/mobile/src/lib/filePreview.test.ts @@ -241,6 +241,7 @@ vi.mock("../components/ui/Menu", () => ({ })); vi.mock("../components/ui/FilePreviewGallery", () => ({ FilePreviewGallery: ({ children }: { children?: ReactNode }) => children, + useFilePreviewGallery: () => (source: unknown) => source, })); vi.mock("../components/ui/FilePreviewModal", () => ({ FilePreviewModal: (props: { source: { name: string } | null }) => From bcdd6a59f499774790dae98a14149db0dfab003f Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 15 Nov 2026 10:14:54 +0100 Subject: [PATCH 015/446] fix(mobile): /model suggestion opens the model picker (#37109) Co-authored-by: Julius Marminge --- .../src/features/threads/ThreadComposer.tsx | 1 + .../use-composer-command-menu.select.test.tsx | 55 +++++++++++++++++++ .../threads/use-composer-command-menu.ts | 19 +++++-- apps/mobile/vite.config.ts | 1 + 4 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 apps/mobile/src/features/threads/use-composer-command-menu.select.test.tsx diff --git a/apps/mobile/src/features/threads/ThreadComposer.tsx b/apps/mobile/src/features/threads/ThreadComposer.tsx index fdd6b414a46e5..ce4e4b7fb0e4b 100644 --- a/apps/mobile/src/features/threads/ThreadComposer.tsx +++ b/apps/mobile/src/features/threads/ThreadComposer.tsx @@ -591,6 +591,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps offersUsageLimits: usageLimitsOffered, onUsageLimits: usageLimitsOffered && draftAttachments.length === 0 ? openUsageLimits : undefined, + onOpenModelPicker: settingsSheetPresentation.open, modelOptions: mentionModelOptions, modelProviders: props.serverConfig?.providers, ...(props.onFreshSession === undefined ? {} : { onFreshSession: props.onFreshSession }), diff --git a/apps/mobile/src/features/threads/use-composer-command-menu.select.test.tsx b/apps/mobile/src/features/threads/use-composer-command-menu.select.test.tsx new file mode 100644 index 0000000000000..395961bd91ec7 --- /dev/null +++ b/apps/mobile/src/features/threads/use-composer-command-menu.select.test.tsx @@ -0,0 +1,55 @@ +import { act } from "react"; +import { createRoot } from "react-dom/client"; +import { expect, it, vi } from "vite-plus/test"; + +vi.mock("react-native", () => ({ Alert: { alert: vi.fn() } })); +vi.mock("../../state/queries", () => ({ + useComposerPathSearch: () => ({ entries: [], isPending: false }), + useComposerPullRequestSearch: () => ({ entries: [], isPending: false, error: null }), + useThreadSearch: () => undefined, +})); +vi.mock("../../state/use-composer-drafts", () => ({ + getComposerDraftSnapshot: vi.fn(), + setComposerDraftContext: vi.fn(), + useComposerDraftSelection: () => null, +})); +vi.mock("../../lib/uuid", () => ({ uuidv4: () => "context-id" })); +vi.mock("../../state/server", () => ({ + serverEnvironment: { + providersValueAtom: vi.fn(), + refreshProviders: Symbol("refreshProviders"), + }, +})); + +import { useComposerCommandMenu } from "./use-composer-command-menu"; + +it("/model clears only its trigger and opens the model picker", async () => { + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; + const onChangeDraftMessage = vi.fn(); + const onOpenModelPicker = vi.fn(); + let menu: ReturnType | undefined; + function Probe() { + menu = useComposerCommandMenu({ + draftMessage: "fix this /mod", + ownerKey: "draft", + environmentId: null, + projectCwd: null, + selectedProviderStatus: null, + hasThread: true, + hasCompactableConversation: false, + onChangeDraftMessage, + onOpenModelPicker, + }); + return null; + } + + const root = createRoot(document.createElement("div")); + await act(async () => root.render()); + const model = menu?.items.find((item) => item.id === "cmd:model"); + if (!model) throw new Error("Expected /model in the slash menu"); + await act(async () => menu?.onSelect(model)); + + expect(onOpenModelPicker).toHaveBeenCalledOnce(); + expect(onChangeDraftMessage).toHaveBeenCalledWith("fix this "); + act(() => root.unmount()); +}); diff --git a/apps/mobile/src/features/threads/use-composer-command-menu.ts b/apps/mobile/src/features/threads/use-composer-command-menu.ts index a77f7089c97bd..7364b1cf6cf9e 100644 --- a/apps/mobile/src/features/threads/use-composer-command-menu.ts +++ b/apps/mobile/src/features/threads/use-composer-command-menu.ts @@ -256,6 +256,7 @@ export function useComposerCommandMenu({ enabled = true, onChangeDraftMessage, onUsageLimits, + onOpenModelPicker, modelOptions, modelProviders, onFreshSession, @@ -280,6 +281,8 @@ export function useComposerCommandMenu({ readonly enabled?: boolean; readonly onChangeDraftMessage: (value: string) => void; readonly onUsageLimits?: () => void; + /** `/model` opens this picker instead of inserting text. */ + readonly onOpenModelPicker: () => void; readonly modelOptions?: ReadonlyArray; readonly modelProviders?: ReadonlyArray; /** Arms a fresh session for the next message; absent where `/clear` is unavailable. */ @@ -627,11 +630,18 @@ export function useComposerCommandMenu({ return; } - if (item.type === "slash-command" && item.command === "clear" && onFreshSession) { + const clearTriggerThen = (action: () => void) => { const cleared = replaceTextRange(draftMessage, trigger.rangeStart, trigger.rangeEnd, ""); setSelection({ start: cleared.cursor, end: cleared.cursor }); onChangeDraftMessage(cleared.text); - onFreshSession(); + action(); + }; + if (item.type === "slash-command" && item.command === "model") { + clearTriggerThen(onOpenModelPicker); + return; + } + if (item.type === "slash-command" && item.command === "clear" && onFreshSession) { + clearTriggerThen(onFreshSession); return; } if ( @@ -639,10 +649,7 @@ export function useComposerCommandMenu({ item.command.name === USAGE_LIMITS_COMMAND.name && onUsageLimits ) { - const cleared = replaceTextRange(draftMessage, trigger.rangeStart, trigger.rangeEnd, ""); - setSelection({ start: cleared.cursor, end: cleared.cursor }); - onChangeDraftMessage(cleared.text); - onUsageLimits(); + clearTriggerThen(onUsageLimits); return; } diff --git a/apps/mobile/vite.config.ts b/apps/mobile/vite.config.ts index 9f29ad103b0bc..e9f1260b514c3 100644 --- a/apps/mobile/vite.config.ts +++ b/apps/mobile/vite.config.ts @@ -43,6 +43,7 @@ const reactTests = [ "src/state/use-selected-thread-git-state.test.tsx", "src/features/terminal/useTerminalExitNavigation.test.tsx", "src/features/threads/use-thread-list-now.test.tsx", + "src/features/threads/use-composer-command-menu.select.test.tsx", "src/features/threads/useThreadListV2.test.tsx", "src/features/home/home-list-options.test.tsx", "src/features/home/home-route-visibility.test.tsx", From 178570dcd3031f0e7e5c8720303a4af1f44d9ef2 Mon Sep 17 00:00:00 2001 From: Romil Date: Sun, 15 Nov 2026 20:15:24 +1100 Subject: [PATCH 016/446] fix(desktop): release cancelled Codex callback listeners during browser launch (#29734) Co-authored-by: maria-rcks --- .../desktop/src/app/CodexAuthCallback.test.ts | 79 +++++++++++++------ apps/desktop/src/app/DesktopClerk.ts | 3 +- apps/desktop/src/ipc/methods/providerAuth.ts | 9 ++- packages/shared/src/codexAuthCallback.ts | 4 +- 4 files changed, 64 insertions(+), 31 deletions(-) diff --git a/apps/desktop/src/app/CodexAuthCallback.test.ts b/apps/desktop/src/app/CodexAuthCallback.test.ts index 6d8a905885aca..e8f1ca0b80bd8 100644 --- a/apps/desktop/src/app/CodexAuthCallback.test.ts +++ b/apps/desktop/src/app/CodexAuthCallback.test.ts @@ -37,18 +37,27 @@ function callback(authorizationUrl: string) { } describe("desktop Codex callback helper", () => { - it("binds before opening sign-in, ignores a foreign response, and returns only the code callback", async () => { + it("binds before opening sign-in, ignores a foreign response, and returns only the code callback", async ({ + onTestFinished, + }) => { const authorizationUrl = request(await freePort()); const expected = callback(authorizationUrl); - const received = await receiveCodexAuthCallback(authorizationUrl, async () => { + const opening = Promise.withResolvers(); + onTestFinished(() => opening.resolve(true)); + const checkResponse = async () => { const invalid = new URL(expected); invalid.searchParams.set("state", "foreign"); expect((await fetch(invalid)).status).toBe(400); const response = await fetch(expected); expect(response.headers.get("cache-control")).toBe("no-store"); expect(await response.text()).not.toContain("test-code"); - return true; + }; + let responseChecked!: Promise; + const received = await receiveCodexAuthCallback(authorizationUrl, () => { + responseChecked = checkResponse(); + return responseChecked.then(() => opening.promise); }); + await responseChecked; expect(received).toBe(expected); }); it("returns hosted web to the exact instance and environment without putting the code in its query", async () => { @@ -61,34 +70,52 @@ describe("desktop Codex callback helper", () => { instanceId: ProviderInstanceId.make("work"), flowId: "flow-one", }; + const openBrowser = async () => { + const response = await fetch(expected, { redirect: "manual" }); + expect(response.status).toBe(303); + const delivery = response.headers.get("location")!; + expect(new URL(delivery).searchParams.has("code")).toBe(false); + expect(readCodexAuthDelivery(delivery)?.callbackUrl).toBe(expected); + expect(readCodexAuthDelivery(delivery)?.returnUrl).toBe(input.returnUrl); + return true; + }; + let opening!: Promise; await receiveCodexAuthCallback( authorizationUrl, - async () => { - const response = await fetch(expected, { redirect: "manual" }); - expect(response.status).toBe(303); - const delivery = response.headers.get("location")!; - expect(new URL(delivery).searchParams.has("code")).toBe(false); - expect(readCodexAuthDelivery(delivery)?.callbackUrl).toBe(expected); - expect(readCodexAuthDelivery(delivery)?.returnUrl).toBe(input.returnUrl); - return true; - }, + () => (opening = openBrowser()), (url) => codexAuthDeliveryUrl(input, url), ); + await opening; }); - it("cancels and releases its listener so exact-port reauthorization can run again", async () => { - const authorizationUrl = request(await freePort()); - await expect( - receiveCodexAuthCallback(authorizationUrl, async () => { - cancelCodexAuthCallback(authorizationUrl); - return true; - }), - ).rejects.toThrow("cancelled"); - expect( - await receiveCodexAuthCallback(authorizationUrl, async () => { - await fetch(callback(authorizationUrl)); - return true; - }), - ).toBe(callback(authorizationUrl)); + it("cancels and releases its listener so exact-port reauthorization can run again", async ({ + onTestFinished, + signal, + }) => { + for (const external of [false, true]) { + const authorizationUrl = request(await freePort()); + const abort = new AbortController(); + const opening = Promise.withResolvers(); + onTestFinished(() => opening.resolve(true)); + await expect( + receiveCodexAuthCallback( + authorizationUrl, + async () => { + if (external) abort.abort(); + else cancelCodexAuthCallback(authorizationUrl); + return opening.promise; + }, + undefined, + abort.signal, + ), + ).rejects.toThrow("cancelled"); + if (signal.aborted) return; + expect( + await receiveCodexAuthCallback(authorizationUrl, async () => { + await fetch(callback(authorizationUrl)); + return true; + }), + ).toBe(callback(authorizationUrl)); + } }); it("allows two accounts to complete independently", async () => { const a = request(await freePort(), "a".repeat(43)); diff --git a/apps/desktop/src/app/DesktopClerk.ts b/apps/desktop/src/app/DesktopClerk.ts index 206a143128a8e..207aa17739e59 100644 --- a/apps/desktop/src/app/DesktopClerk.ts +++ b/apps/desktop/src/app/DesktopClerk.ts @@ -178,11 +178,12 @@ export const make = Effect.gen(function* () { Effect.gen(function* () { yield* electronApp.whenReady; yield* Effect.tryPromise({ - try: () => + try: (signal) => receiveCodexAuthCallback( request.authorizationUrl, (url) => runPromise(shell.openExternal(url)), (callbackUrl) => codexAuthDeliveryUrl(request, callbackUrl), + signal, ), catch: () => new CodexAuthCallbackError({ diff --git a/apps/desktop/src/ipc/methods/providerAuth.ts b/apps/desktop/src/ipc/methods/providerAuth.ts index fbde6a409f3f2..d7f432815d2ae 100644 --- a/apps/desktop/src/ipc/methods/providerAuth.ts +++ b/apps/desktop/src/ipc/methods/providerAuth.ts @@ -23,8 +23,13 @@ export const receiveProviderAuthCallback = DesktopIpc.makeIpcMethod({ const context = yield* Effect.context(); const runPromise = Effect.runPromiseWith(context); const callbackUrl = yield* Effect.tryPromise({ - try: () => - receiveCodexAuthCallback(authorizationUrl, (url) => runPromise(shell.openExternal(url))), + try: (signal) => + receiveCodexAuthCallback( + authorizationUrl, + (url) => runPromise(shell.openExternal(url)), + undefined, + signal, + ), catch: () => new CodexAuthCallbackError({ detail: diff --git a/packages/shared/src/codexAuthCallback.ts b/packages/shared/src/codexAuthCallback.ts index e668340a27525..415e3e2d8b9fc 100644 --- a/packages/shared/src/codexAuthCallback.ts +++ b/packages/shared/src/codexAuthCallback.ts @@ -86,8 +86,8 @@ export async function receiveCodexAuthCallback( server.listen(Number(new URL(request.redirectUri).port), "127.0.0.1", resolve); }); if (abort.signal.aborted) throw new Error("Sign-in cancelled on this computer."); - if (!(await openBrowser(request.authorizationUrl))) - throw new Error("Could not open your sign-in browser."); + const opened = await Promise.race([openBrowser(request.authorizationUrl), callback.promise]); + if (!opened) throw new Error("Could not open your sign-in browser."); return await callback.promise; } finally { clearTimeout(timer); From 94edd64e74cbee05735d30f8e917ea38203ae7ec Mon Sep 17 00:00:00 2001 From: Lakshmi Tanmay Date: Sun, 15 Nov 2026 14:45:41 +0530 Subject: [PATCH 017/446] perf(server): skip compression for the desktop's local connection (#30832) Co-authored-by: maria-rcks --- apps/server/src/ws.test.ts | 108 ++++++++++++++++++++++++++++++++++++- apps/server/src/ws.ts | 7 +++ 2 files changed, 114 insertions(+), 1 deletion(-) diff --git a/apps/server/src/ws.test.ts b/apps/server/src/ws.test.ts index 660548c1dd68b..123a8a5f9362f 100644 --- a/apps/server/src/ws.test.ts +++ b/apps/server/src/ws.test.ts @@ -1,4 +1,8 @@ import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver"; +import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer"; +import { WebSocket } from "@effect/platform-node/NodeWS"; +// @effect-diagnostics-next-line nodeBuiltinImport:off -- Bind an isolated WebSocket negotiation fixture. +import * as NodeHttp from "node:http"; import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; import * as NodeServices from "@effect/platform-node/NodeServices"; import { assert, it } from "@effect/vitest"; @@ -8,9 +12,12 @@ import { AuthOrchestrationReadScope, AuthSessionId, AuthSourceControlWriteScope, + ORCHESTRATION_PROTOCOL_VERSION, + ORCHESTRATION_PROTOCOL_QUERY_PARAM, EnvironmentId, EventId, GitManagerError, + VcsProcessSpawnError, MessageId, ProjectId, ThreadId, @@ -33,6 +40,7 @@ import { type ServerConfigStreamEvent, type ServerProvider, } from "@t3tools/contracts"; +import { SOCKET_FRAME_PARTS_QUERY_PARAM } from "@t3tools/shared/socketFrameParts"; import * as Deferred from "effect/Deferred"; import type { ProviderInstance } from "@t3tools/provider-core/server/driver"; import * as DateTime from "effect/DateTime"; @@ -47,7 +55,12 @@ import * as PubSub from "effect/PubSub"; import * as Queue from "effect/Queue"; import * as Stream from "effect/Stream"; import * as TestClock from "effect/testing/TestClock"; -import { Headers } from "effect/http"; +import { Headers, HttpRouter, HttpServer } from "effect/http"; +import * as Context from "effect/Context"; +import * as FetchHttpClient from "effect/http/FetchHttpClient"; +import * as VcsProcess from "./vcs/VcsProcess.ts"; +import * as ProviderInstallationGate from "./provider/ProviderInstallationGate.ts"; +import * as Ws from "./ws.ts"; import { RpcGroup, RpcMessage, RpcServer } from "effect/rpc"; import * as RpcTest from "effect/rpc/RpcTest"; import * as SqlClient from "effect/sql/SqlClient"; @@ -891,6 +904,99 @@ const layerConfigSources = Layer.mergeAll( Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), ); +it.live.each([ + { subject: "desktop-bootstrap", extensions: "", framed: false }, + { subject: "desktop-bootstrap", extensions: "", framed: true }, + { subject: "one-time-token", extensions: "permessage-deflate", framed: false }, + { subject: "cloud-connect", extensions: "permessage-deflate", framed: false }, +])( + "negotiates compression for $subject sessions (framed=$framed)", + ({ subject, extensions, framed }) => + Effect.gen(function* () { + const services = Layer.mergeAll( + layerUnusedConfigRpcServices, + Layer.mock(ServerBrowser.ServerBrowser)({ + profileReporter: Effect.succeed(() => Effect.void), + }), + Layer.mock(EnvironmentAuth.EnvironmentAuth)({ + authenticateWebSocketUpgrade: () => + Effect.succeed({ + sessionId: AuthSessionId.make("compression-test"), + subject, + method: "bearer-access-token", + scopes: [], + }), + }), + Layer.mock(SessionStore.SessionStore)({ + cookieName: "compression-test", + legacyCookieName: undefined, + markConnected: () => Effect.void, + markDisconnected: () => Effect.void, + awaitInvalidation: () => Effect.never, + }), + Layer.mock(AnalyticsService.AnalyticsService)({ recordOwners: () => Effect.void }), + Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({ + namedProjectsRoot: process.cwd(), + }), + Layer.mock(ScratchThreadPromotion.ScratchThreadPromotion)({}), + Layer.mock(ProviderRegistry.ProviderRegistry)({}), + Layer.mock(UsageLimitSources.UsageLimitSources)({}), + Layer.mock(ExternalLauncher.ExternalLauncher)({}), + Layer.mock(RemoteOpenTargets.RemoteOpenTargets)({}), + Layer.mock(DirectEndpoints.DirectEndpoints)({}), + Layer.mock(ServerEnvironment.ServerEnvironment)({}), + Layer.mock(VcsProcess.VcsProcess)({ + run: (input) => + Effect.fail( + new VcsProcessSpawnError({ + operation: input.operation, + command: input.command, + cwd: input.cwd, + cause: "No VCS commands in the compression fixture", + }), + ), + }), + ProviderInstallationGate.layer, + FetchHttpClient.layer, + ); + const context = yield* Layer.build( + HttpRouter.serve(Ws.layer, { disableListenLog: true }).pipe( + Layer.provideMerge( + NodeHttpServer.layer(NodeHttp.createServer, { + host: "127.0.0.1", + port: 0, + websocket: { perMessageDeflate: true }, + }), + ), + Layer.provide(services), + Layer.provide(layerConfigSources), + ), + ); + const server = Context.get(context, HttpServer.HttpServer); + const origin = HttpServer.formatAddress(server.address).replace(/^http/, "ws"); + const socket = yield* Effect.acquireRelease( + Effect.sync( + () => + new WebSocket( + `${origin}/ws?${ORCHESTRATION_PROTOCOL_QUERY_PARAM}=${ORCHESTRATION_PROTOCOL_VERSION}${framed ? `&${SOCKET_FRAME_PARTS_QUERY_PARAM}=1` : ""}`, + { + origin: "t3code://app", + }, + ), + ), + (socket) => Effect.sync(() => socket.terminate()), + ); + yield* Effect.promise( + () => + new Promise((resolve, reject) => { + socket.once("open", resolve); + socket.once("error", reject); + }), + ); + assert.equal(socket.extensions, extensions); + }).pipe(Effect.scoped), +); + it.effect.each([ "pending", "cached", diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts index 09c6d86c050fd..c819d099938e2 100644 --- a/apps/server/src/ws.ts +++ b/apps/server/src/ws.ts @@ -2,6 +2,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 Context from "effect/Context"; +import * as NodeHttpServerRequest from "@effect/platform-node/NodeHttpServerRequest"; import * as SecureRandom from "effect/crypto/SecureRandom"; import * as Orchestrator from "./orchestration-v2/Orchestrator.ts"; import * as PeerLinkRequests from "./peer/PeerLinkRequests.ts"; @@ -3590,6 +3591,12 @@ export const layer = Layer.unwrap( failEnvironmentInternal("internal_error", error), ), ); + // Desktop bootstrap sessions connect to their own backend, including same-machine WSL. + if (session.subject === "desktop-bootstrap") { + delete NodeHttpServerRequest.toIncomingMessage(request).headers[ + "sec-websocket-extensions" + ]; + } const clientAnalyticsProps = readClientAnalyticsProps(request); yield* analytics.recordOwners(readClientOwners(request), "client"); const reportProfiles = yield* serverBrowser.profileReporter; From f28c74a7d04047afb5fa421815643f6db8ca23dd Mon Sep 17 00:00:00 2001 From: RVZO6 <66844656+RVZO6@users.noreply.github.com> Date: Sun, 15 Nov 2026 04:15:59 -0500 Subject: [PATCH 018/446] fix(server): outside MCP clients stop being offered thread-bound tools (#32764) Co-authored-by: Claude Opus 5.5 Co-authored-by: maria-rcks Co-authored-by: Julius Marminge --- apps/server/src/mcp/McpHttpServer.test.ts | 6 +-- apps/server/src/mcp/McpInvocationContext.ts | 11 ++++ apps/server/src/mcp/McpToolAccess.test.ts | 54 +++++++++++++++++-- apps/server/src/mcp/McpToolAccess.ts | 13 ++++- apps/server/src/mcp/toolkits/core.test.ts | 7 +-- apps/server/src/mcp/toolkits/device/tools.ts | 14 +++-- apps/server/src/mcp/toolkits/html/tools.ts | 8 +-- apps/server/src/mcp/toolkits/message/tools.ts | 3 +- .../src/mcp/toolkits/orchestrator/tools.ts | 18 ++++--- apps/server/src/mcp/toolkits/preview/tools.ts | 15 ++++-- .../src/mcp/toolkits/previewControls/tools.ts | 9 ++-- .../src/mcp/toolkits/toolSearch/tools.ts | 5 +- .../server/src/mcp/toolkits/worktree/tools.ts | 8 +-- docs/internals/environment-auth.md | 6 +++ 14 files changed, 137 insertions(+), 40 deletions(-) diff --git a/apps/server/src/mcp/McpHttpServer.test.ts b/apps/server/src/mcp/McpHttpServer.test.ts index 3c80cdf55fe1e..5801a210ba6cf 100644 --- a/apps/server/src/mcp/McpHttpServer.test.ts +++ b/apps/server/src/mcp/McpHttpServer.test.ts @@ -604,13 +604,11 @@ it.effect("refuses preview tools to a client outside a thread before they run", const click = yield* server .callTool({ name: "preview_act", arguments: { action: "click", locator: "text=Send" } }) .pipe( + Effect.flip, Effect.provideService(McpInvocationContext.McpInvocationContext, readOnly), Effect.provideService(McpSchema.McpServerClient, client), ); - expect(click.isError).toBe(true); - expect(click.content).toEqual([ - { type: "text", text: expect.stringContaining("needs an agent running inside T3 Code") }, - ]); + expect(click.message).toContain("Tool 'preview_act' not found"); }).pipe(Effect.provide(layerTest)), ); diff --git a/apps/server/src/mcp/McpInvocationContext.ts b/apps/server/src/mcp/McpInvocationContext.ts index 37323863e8b72..e306e775dc8a5 100644 --- a/apps/server/src/mcp/McpInvocationContext.ts +++ b/apps/server/src/mcp/McpInvocationContext.ts @@ -180,6 +180,17 @@ export const hiddenFromSubagents = (): boolean => { return scope?.thread?.subagent !== true; }; +/** + * `McpSchema.EnabledWhen` for tools that act as the calling thread: a client + * from outside has none. Discovery only; `McpToolAccess` still refuses the + * call, and `toLayer` requires this on every thread-bound declaration. + */ +export const hiddenFromOutsideClients = (): boolean => { + const fiber = Fiber.getCurrent(); + const scope = fiber && Context.getOrUndefined(fiber.context, McpInvocationContext); + return scope === undefined || scope.thread !== undefined; +}; + /** `McpSchema.EnabledWhen` for tools that only serve links, hidden while the beta is off. */ export const hiddenWhileLinksOff = (): boolean => { const fiber = Fiber.getCurrent(); diff --git a/apps/server/src/mcp/McpToolAccess.test.ts b/apps/server/src/mcp/McpToolAccess.test.ts index 6d63287f4eb2a..6cfd86bd3d836 100644 --- a/apps/server/src/mcp/McpToolAccess.test.ts +++ b/apps/server/src/mcp/McpToolAccess.test.ts @@ -13,6 +13,8 @@ import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; import * as Layer from "effect/Layer"; import * as Schema from "effect/Schema"; +import * as Sink from "effect/Sink"; +import * as Stream from "effect/Stream"; import { McpSchema, McpServer, Tool, Toolkit } from "effect/ai"; import { OrchestratorProjectionError } from "../orchestration-v2/Orchestrator.ts"; @@ -113,8 +115,14 @@ const probe = { }; const ProbeToolkit = Toolkit.make( Tool.make("reads", probe), - Tool.make("reads_as_caller", probe), - Tool.make("acts_as_caller", probe), + Tool.make("reads_as_caller", probe).annotate( + McpSchema.EnabledWhen, + McpInvocationContext.hiddenFromOutsideClients, + ), + Tool.make("acts_as_caller", probe).annotate( + McpSchema.EnabledWhen, + McpInvocationContext.hiddenFromOutsideClients, + ), Tool.make("writes", probe), Tool.make("writes_with_capability", probe), Tool.make("writes_threads", { @@ -254,7 +262,6 @@ it.effect.each([ ["writes_environment", readOnlyClient, {}, "capability_denied"], ["reads_as_caller", ended, {}, "ran"], - ["reads_as_caller", fullAccessClient, {}, "thread_credential_required"], ["acts_as_caller", supervised, {}, "ran"], ["acts_as_caller", ended, {}, "parent_not_active"], ["acts_as_caller", background, {}, "ran"], @@ -262,7 +269,6 @@ it.effect.each([ ["acts_as_caller", mismatchedProvider, {}, "parent_not_active"], ["acts_as_caller", archivedBackground, {}, "parent_not_active"], ["acts_as_caller", unknownOwnerBackground, {}, "parent_not_active"], - ["acts_as_caller", fullAccessClient, {}, "thread_credential_required"], ["writes", supervised, {}, "ran"], ["writes", ended, {}, "parent_not_active"], @@ -455,7 +461,12 @@ it.effect.each([ it.effect("refuses a linked environment to a tool that acts as its calling thread", () => Effect.gen(function* () { - const CallerToolkit = Toolkit.make(Tool.make("acts_as_caller", linkedProbe)); + const CallerToolkit = Toolkit.make( + Tool.make("acts_as_caller", linkedProbe).annotate( + McpSchema.EnabledWhen, + McpInvocationContext.hiddenFromOutsideClients, + ), + ); const built = yield* Layer.build( McpToolAccess.HandlersLayer.layer( McpToolAccess.toLayer(CallerToolkit, { @@ -466,3 +477,36 @@ it.effect("refuses a linked environment to a tool that acts as its calling threa expect(Exit.isFailure(built) && Cause.hasDies(built.cause)).toBe(true); }), ); + +// Discovery hides these tools. The handler must refuse on its own if that ever changes. +it.effect.each(["reads_as_caller", "acts_as_caller"] as const)( + "%s refuses an outside client behind discovery", + (tool) => + Effect.gen(function* () { + const built = yield* ProbeToolkit; + const { result } = yield* built + .handle(tool, {}) + .pipe(Stream.unwrap, Stream.run(Sink.last()), Effect.flatMap(Effect.fromOption)); + expect(result).toMatchObject({ code: "thread_credential_required" }); + }).pipe( + Effect.provide(McpToolAccess.HandlersLayer.layer(layerProbeHandlers)), + Effect.provideService(McpInvocationContext.McpInvocationContext, fullAccessClient), + Effect.provide(Layer.mock(ThreadManagement.ThreadManagementService)({})), + ), +); + +it.effect("refuses to build a thread-bound tool that outside clients would be offered", () => + Effect.gen(function* () { + const CallerToolkit = Toolkit.make(Tool.make("reads_as_caller", probe)); + const built = yield* Layer.build( + McpToolAccess.HandlersLayer.layer( + McpToolAccess.toLayer(CallerToolkit, { + reads_as_caller: McpToolAccess.readsAsCaller(() => ran), + }), + ), + ).pipe(Effect.scoped, Effect.exit); + expect(Exit.isFailure(built) && Cause.pretty(built.cause)).toContain( + "reads_as_caller acts as its calling thread", + ); + }), +); diff --git a/apps/server/src/mcp/McpToolAccess.ts b/apps/server/src/mcp/McpToolAccess.ts index f96a38d6d7341..20a9274798d0f 100644 --- a/apps/server/src/mcp/McpToolAccess.ts +++ b/apps/server/src/mcp/McpToolAccess.ts @@ -7,6 +7,7 @@ import { type RuntimeMode, type ThreadId, } from "@t3tools/contracts"; +import * as Context from "effect/Context"; import * as Effect from "effect/Effect"; import * as Option from "effect/Option"; import * as Predicate from "effect/Predicate"; @@ -14,6 +15,7 @@ import * as Ref from "effect/Ref"; import * as Struct from "effect/Struct"; import type * as Layer from "effect/Layer"; import type * as Scope from "effect/Scope"; +import { McpSchema } from "effect/ai"; import type { Tool, Toolkit } from "effect/ai"; import { @@ -420,8 +422,17 @@ const routed = Effect.fn("McpToolAccess.routed")(function* = { ...checked }; for (const [name, declaration] of Object.entries>(declarations)) { const tool = toolkit.tools[name]; - if (tool === undefined || !takesLinkedEnvironment(tool)) continue; + if (tool === undefined) continue; const remote = remoteCheckOf(declaration); + if ( + remote === undefined && + Context.getOrUndefined(tool.annotations, McpSchema.EnabledWhen) !== + McpInvocationContext.hiddenFromOutsideClients + ) + return yield* Effect.die( + `${name} acts as its calling thread, so outside clients must not be offered it.`, + ); + if (!takesLinkedEnvironment(tool)) continue; const forwarded = forwardedOf(declaration) as | Forwarded, never> | undefined; diff --git a/apps/server/src/mcp/toolkits/core.test.ts b/apps/server/src/mcp/toolkits/core.test.ts index d95e44de9e691..0e9b5cb557d41 100644 --- a/apps/server/src/mcp/toolkits/core.test.ts +++ b/apps/server/src/mcp/toolkits/core.test.ts @@ -964,22 +964,23 @@ it.effect("a read-only client reads threads and is refused every write before it ); const dispatched: Array = []; -it.effect("refuses act-as-caller tools to a client caller", () => +it.effect("hides act-as-caller tools from a client caller", () => Effect.gen(function* () { const server = yield* McpServer.McpServer; - const result = yield* server + const error = yield* server .callTool({ name: "delegate_task", arguments: { task: "Review", mode: "async" }, }) .pipe( + Effect.flip, Effect.provideService( McpInvocationContext.McpInvocationContext, clientScope("full-access"), ), Effect.provideService(McpSchema.McpServerClient, client), ); - expect(declaredFailure(result)).toMatchObject({ code: "thread_credential_required" }); + expect(error.message).toContain("Tool 'delegate_task' not found"); }).pipe( Effect.provide( layerOrchestratorToolkit.pipe( diff --git a/apps/server/src/mcp/toolkits/device/tools.ts b/apps/server/src/mcp/toolkits/device/tools.ts index 14ed87217368e..c87b579843ad8 100644 --- a/apps/server/src/mcp/toolkits/device/tools.ts +++ b/apps/server/src/mcp/toolkits/device/tools.ts @@ -12,7 +12,7 @@ import * as Schema from "effect/Schema"; import * as FileSystem from "effect/FileSystem"; import * as Path from "effect/Path"; import * as ServerConfig from "../../../config.ts"; -import { Tool, Toolkit } from "effect/ai"; +import { McpSchema, Tool, Toolkit } from "effect/ai"; import * as McpInvocationContext from "../../McpInvocationContext.ts"; import * as DeviceService from "../../../device/DeviceService.ts"; @@ -45,7 +45,8 @@ const DeviceListTool = Tool.make("device_list", { .annotate(Tool.Readonly, true) .annotate(Tool.Destructive, false) .annotate(Tool.Idempotent, true) - .annotate(Tool.OpenWorld, false); + .annotate(Tool.OpenWorld, false) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); const DeviceOpenTool = Tool.make("device_open", { description: @@ -66,7 +67,8 @@ const DeviceOpenTool = Tool.make("device_open", { .annotate(Tool.Readonly, false) .annotate(Tool.Destructive, false) .annotate(Tool.Idempotent, true) - .annotate(Tool.OpenWorld, true); + .annotate(Tool.OpenWorld, true) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); export const DeviceScreenshotTool = Tool.make("device_screenshot", { description: @@ -80,7 +82,8 @@ export const DeviceScreenshotTool = Tool.make("device_screenshot", { .annotate(Tool.Readonly, true) .annotate(Tool.Destructive, false) .annotate(Tool.Idempotent, true) - .annotate(Tool.OpenWorld, true); + .annotate(Tool.OpenWorld, true) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); const DeviceCloseTool = Tool.make("device_close", { description: @@ -94,7 +97,8 @@ const DeviceCloseTool = Tool.make("device_close", { .annotate(Tool.Readonly, false) .annotate(Tool.Destructive, true) .annotate(Tool.Idempotent, true) - .annotate(Tool.OpenWorld, true); + .annotate(Tool.OpenWorld, true) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); export const DeviceStandardToolkit = Toolkit.make(DeviceListTool, DeviceOpenTool, DeviceCloseTool); diff --git a/apps/server/src/mcp/toolkits/html/tools.ts b/apps/server/src/mcp/toolkits/html/tools.ts index 90d54d057f5f6..0cb6153025466 100644 --- a/apps/server/src/mcp/toolkits/html/tools.ts +++ b/apps/server/src/mcp/toolkits/html/tools.ts @@ -10,7 +10,7 @@ import { HTML_RENDER_TOOL_NAME, } from "@t3tools/shared/htmlRender"; import * as Schema from "effect/Schema"; -import { Tool, Toolkit } from "effect/ai"; +import { McpSchema, Tool, Toolkit } from "effect/ai"; import * as HtmlRender from "../../../htmlRender/HtmlRender.ts"; import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts"; @@ -72,7 +72,8 @@ export const HtmlPreviewTool = Tool.make("html_preview", { .annotate(Tool.Readonly, true) .annotate(Tool.Destructive, false) .annotate(Tool.Idempotent, true) - .annotate(Tool.OpenWorld, true); + .annotate(Tool.OpenWorld, true) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); // This affects only the caller's thread, not its workspace, so read-only sandboxes and plan mode may use it. const HtmlRenderTool = Tool.make(HTML_RENDER_TOOL_NAME, { @@ -109,7 +110,8 @@ const HtmlRenderTool = Tool.make(HTML_RENDER_TOOL_NAME, { .annotate(Tool.Readonly, true) .annotate(Tool.Destructive, false) .annotate(Tool.Idempotent, false) - .annotate(Tool.OpenWorld, true); + .annotate(Tool.OpenWorld, true) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); export const HtmlPreviewToolkit = Toolkit.make(HtmlPreviewTool); diff --git a/apps/server/src/mcp/toolkits/message/tools.ts b/apps/server/src/mcp/toolkits/message/tools.ts index 848cfa46976fb..418a252eb9ead 100644 --- a/apps/server/src/mcp/toolkits/message/tools.ts +++ b/apps/server/src/mcp/toolkits/message/tools.ts @@ -1,7 +1,7 @@ import { OrchestratorMcpFailure } from "@t3tools/contracts"; import { SEND_TO_USER_MAX_LENGTH, SEND_TO_USER_TOOL_NAME } from "@t3tools/shared/toolOutput"; import * as Schema from "effect/Schema"; -import { Tool, Toolkit } from "effect/ai"; +import { McpSchema, Tool, Toolkit } from "effect/ai"; import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts"; import * as McpInvocationContext from "../../McpInvocationContext.ts"; @@ -33,6 +33,7 @@ const SendToUserTool = Tool.make(SEND_TO_USER_TOOL_NAME, { .annotate(Tool.Destructive, false) .annotate(Tool.Idempotent, false) .annotate(Tool.OpenWorld, false) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients) // Claude Code defers MCP tools behind ToolSearch; Anthropic says to declare this from the first request. .annotate(Tool.Meta, { "anthropic/alwaysLoad": true }); diff --git a/apps/server/src/mcp/toolkits/orchestrator/tools.ts b/apps/server/src/mcp/toolkits/orchestrator/tools.ts index 3a84c6a8e9274..629a5352587f0 100644 --- a/apps/server/src/mcp/toolkits/orchestrator/tools.ts +++ b/apps/server/src/mcp/toolkits/orchestrator/tools.ts @@ -97,7 +97,8 @@ const DelegateTaskTool = Tool.make("delegate_task", { }) .annotate(Tool.Title, "Delegate a child task") .annotate(Tool.Destructive, true) - .annotate(Tool.OpenWorld, true); + .annotate(Tool.OpenWorld, true) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); const TaskStatusTool = Tool.make("task_status", { description: @@ -111,7 +112,8 @@ const TaskStatusTool = Tool.make("task_status", { .annotate(Tool.Title, "Get delegated task status") .annotate(Tool.Readonly, false) .annotate(Tool.Destructive, false) - .annotate(Tool.Idempotent, true); + .annotate(Tool.Idempotent, true) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); const TaskCancelTool = Tool.make("task_cancel", { description: @@ -123,7 +125,8 @@ const TaskCancelTool = Tool.make("task_cancel", { dependencies, }) .annotate(Tool.Title, "Cancel delegated task") - .annotate(Tool.Destructive, true); + .annotate(Tool.Destructive, true) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); const WorkflowStopTool = Tool.make("t3_workflow_stop", { description: @@ -161,7 +164,8 @@ const BackgroundWaitTool = Tool.make("t3_background_wait", { .annotate(Tool.Title, "Wait on background work") // Only marks which tasks keep the thread Working; Claude Plan mode blocks tools without this. .annotate(Tool.Readonly, true) - .annotate(Tool.Destructive, false); + .annotate(Tool.Destructive, false) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); const ScheduleTaskTool = Tool.make("schedule_task", { description: @@ -214,7 +218,8 @@ const RequestSecretTool = Tool.make("request_secret", { .annotate(Tool.Title, "Request a secret from the user") .annotate(Tool.Destructive, false) // Claude Code defers MCP tools behind ToolSearch; agents that never load this one ask in chat. - .annotate(Tool.Meta, { "anthropic/alwaysLoad": true }); + .annotate(Tool.Meta, { "anthropic/alwaysLoad": true }) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); const CreateThreadsTool = Tool.make("create_threads", { description: @@ -227,7 +232,8 @@ const CreateThreadsTool = Tool.make("create_threads", { }) .annotate(Tool.Title, "Create T3 threads") .annotate(Tool.Destructive, true) - .annotate(Tool.OpenWorld, true); + .annotate(Tool.OpenWorld, true) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); const ThreadListTool = Tool.make("t3_thread_list", { description: diff --git a/apps/server/src/mcp/toolkits/preview/tools.ts b/apps/server/src/mcp/toolkits/preview/tools.ts index 18c000d12bdf5..37f09c8c8223d 100644 --- a/apps/server/src/mcp/toolkits/preview/tools.ts +++ b/apps/server/src/mcp/toolkits/preview/tools.ts @@ -23,7 +23,7 @@ import { } from "@t3tools/contracts"; import * as Schema from "effect/Schema"; import * as FileSystem from "effect/FileSystem"; -import { Tool, Toolkit } from "effect/ai"; +import { McpSchema, Tool, Toolkit } from "effect/ai"; import * as McpInvocationContext from "../../McpInvocationContext.ts"; import * as PreviewAutomationBroker from "../../PreviewAutomationBroker.ts"; @@ -43,10 +43,16 @@ const presentationFields = { toolIcon: Schema.optional(ToolActivityIcon) }; const PreviewActionResult = Schema.Struct(presentationFields); const browserTool = (tool: T): T => - tool.annotate(Tool.OpenWorld, true).annotate(Tool.Destructive, true) as T; + tool + .annotate(Tool.OpenWorld, true) + .annotate(Tool.Destructive, true) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients) as T; const safeBrowserTool = (tool: T): T => - tool.annotate(Tool.OpenWorld, true).annotate(Tool.Destructive, false) as T; + tool + .annotate(Tool.OpenWorld, true) + .annotate(Tool.Destructive, false) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients) as T; const readonlyBrowserTool = (tool: T): T => safeBrowserTool(tool).annotate(Tool.Readonly, true).annotate(Tool.Idempotent, true) as T; @@ -62,7 +68,8 @@ const PreviewStatusTool = Tool.make("preview_status", { .annotate(Tool.Title, "Get preview status") .annotate(Tool.Readonly, true) .annotate(Tool.Destructive, false) - .annotate(Tool.Idempotent, true); + .annotate(Tool.Idempotent, true) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); // `url` already covers websites, so open only adds the dev-server target. const EnvironmentPortTarget = BrowserNavigationTarget.members[1]; diff --git a/apps/server/src/mcp/toolkits/previewControls/tools.ts b/apps/server/src/mcp/toolkits/previewControls/tools.ts index d2fe8d1d4b6f7..ca92f57f408b8 100644 --- a/apps/server/src/mcp/toolkits/previewControls/tools.ts +++ b/apps/server/src/mcp/toolkits/previewControls/tools.ts @@ -7,7 +7,7 @@ import { PreviewTabId, } from "@t3tools/contracts"; import * as Schema from "effect/Schema"; -import { Tool, Toolkit } from "effect/ai"; +import { McpSchema, Tool, Toolkit } from "effect/ai"; import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts"; import * as PreviewManager from "../../../preview/Manager.ts"; import * as PreviewAutomationBroker from "../../PreviewAutomationBroker.ts"; @@ -37,12 +37,15 @@ const PreviewListTool = Tool.make("t3_preview_list", { }), }) .annotate(Tool.Readonly, true) - .annotate(Tool.Destructive, false); + .annotate(Tool.Destructive, false) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); const PreviewCloseTool = Tool.make("t3_preview_close", { ...shared, description: "Close one preview tab this thread owns. Use it on a tab you opened for your own check when you finish.", parameters: Schema.Struct({ tabId: PreviewTabId }), success: Schema.Struct({}), -}).annotate(Tool.Destructive, true); +}) + .annotate(Tool.Destructive, true) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); export const PreviewControlsToolkit = Toolkit.make(PreviewListTool, PreviewCloseTool); diff --git a/apps/server/src/mcp/toolkits/toolSearch/tools.ts b/apps/server/src/mcp/toolkits/toolSearch/tools.ts index 400fae20aedb2..74741ccca557d 100644 --- a/apps/server/src/mcp/toolkits/toolSearch/tools.ts +++ b/apps/server/src/mcp/toolkits/toolSearch/tools.ts @@ -1,6 +1,6 @@ import { OrchestratorMcpFailure } from "@t3tools/contracts"; import * as Schema from "effect/Schema"; -import { Tool, Toolkit } from "effect/ai"; +import { McpSchema, Tool, Toolkit } from "effect/ai"; import * as McpInvocationContext from "../../McpInvocationContext.ts"; import * as McpToolExposure from "../../McpToolExposure.ts"; @@ -17,6 +17,7 @@ const ToolSearchTool = Tool.make(McpToolExposure.TOOL_SEARCH, { dependencies: [McpInvocationContext.McpInvocationContext, McpToolExposure.McpToolExposure], }) .annotate(Tool.Readonly, true) - .annotate(Tool.Destructive, false); + .annotate(Tool.Destructive, false) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); export const ToolSearchToolkit = Toolkit.make(ToolSearchTool); diff --git a/apps/server/src/mcp/toolkits/worktree/tools.ts b/apps/server/src/mcp/toolkits/worktree/tools.ts index 18d6840d72291..ce9dd39f7938f 100644 --- a/apps/server/src/mcp/toolkits/worktree/tools.ts +++ b/apps/server/src/mcp/toolkits/worktree/tools.ts @@ -12,7 +12,7 @@ import { import * as Schema from "effect/Schema"; import * as ProjectService from "../../../project/ProjectService.ts"; import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts"; -import { Tool, Toolkit } from "effect/ai"; +import { McpSchema, Tool, Toolkit } from "effect/ai"; import * as McpInvocationContext from "../../McpInvocationContext.ts"; import * as WorktreeMcpService from "../../WorktreeMcpService.ts"; @@ -38,7 +38,8 @@ const WorktreeHandoffTool = Tool.make("t3_worktree_handoff", { .annotate(Tool.Readonly, false) .annotate(Tool.Destructive, true) .annotate(Tool.Idempotent, false) - .annotate(Tool.OpenWorld, true); + .annotate(Tool.OpenWorld, true) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); const WorktreeStatusTool = Tool.make("t3_worktree_status", { description: @@ -56,7 +57,8 @@ const WorktreeStatusTool = Tool.make("t3_worktree_status", { .annotate(Tool.Readonly, true) .annotate(Tool.Destructive, false) .annotate(Tool.Idempotent, true) - .annotate(Tool.OpenWorld, false); + .annotate(Tool.OpenWorld, false) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); const WorktreeListTool = Tool.make("t3_worktree_list", { description: diff --git a/docs/internals/environment-auth.md b/docs/internals/environment-auth.md index ad7a96c5eb46f..973b0cc5dafaf 100644 --- a/docs/internals/environment-auth.md +++ b/docs/internals/environment-auth.md @@ -65,6 +65,12 @@ above its ceiling. Inside MCP the credential sets the limits and tool parameters only pick targets; see [threadAccess](../../apps/server/src/mcp/threadAccess.ts). +These clients have no calling thread, so `/mcp` does not offer them tools that +act as one, such as `request_secret` or `delegate_task`. Hiding is only +discovery: the handlers still refuse such a call with +`thread_credential_required`. A secret card belongs to a thread's run, so an +outside agent that needs a secret starts a thread and lets that agent ask. + Issuer and resource URLs come from the request's Host and `X-Forwarded-Proto`, so one server answers over loopback, Tailscale Serve and a T3 Connect tunnel. A proxy that rewrites Host or drops the protocol header From c8a1b5ff0b457860a7b8daad5963026cef96ac63 Mon Sep 17 00:00:00 2001 From: Simone Date: Sun, 15 Nov 2026 10:16:17 +0100 Subject: [PATCH 019/446] fix(ssh): preserve literal home paths in Include expansion (#33288) Co-authored-by: Simone <185146821+Lucenx9@users.noreply.github.com> Co-authored-by: maria-rcks --- packages/ssh/src/config.test.ts | 12 +++++++++--- packages/ssh/src/config.ts | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/ssh/src/config.test.ts b/packages/ssh/src/config.test.ts index 0ce01373d68a3..faec3c4097c78 100644 --- a/packages/ssh/src/config.test.ts +++ b/packages/ssh/src/config.test.ts @@ -14,14 +14,14 @@ function makeTempHomeDir() { } describe("ssh config", () => { - it.effect("deduplicates hosts through quoted, escaped, and Unicode Includes", () => + it.effect("deduplicates hosts through quoted, escaped, Unicode, and home-relative Includes", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; - const homeDir = yield* makeTempHomeDir(); + const homeDir = path.join(yield* makeTempHomeDir(), "home-$&-$$"); const sshDir = path.join(homeDir, ".ssh"); const quotedDir = path.join(homeDir, "quoted dir"); - yield* fs.makeDirectory(sshDir); + yield* fs.makeDirectory(sshDir, { recursive: true }); yield* fs.makeDirectory(quotedDir); yield* fs.writeFileString( path.join(quotedDir, "team#1=a.conf"), @@ -43,11 +43,16 @@ describe("ssh config", () => { `Host unicode-${index}\n HostName unicode-${index}.example.com\nHost *\n`, ); } + yield* fs.writeFileString( + path.join(sshDir, "home.conf"), + "Host home-relative\n HostName home.example.com\nHost *\n", + ); yield* fs.writeFileString( path.join(sshDir, "config"), [ `Include "${path.join(quotedDir, "team#1=a.conf")}" # comment`, `Include ${path.join(homeDir, "escaped space.conf").replaceAll(" ", "\\ ")}`, + "Include ~/.ssh/home.conf", `Include ${unicodeFiles.map((file, index) => (index % 2 === 0 ? file : `"${file}"`)).join("\t")}`, ].join("\n") + "\r", ); @@ -62,6 +67,7 @@ describe("ssh config", () => { hosts.map((host) => [host.alias, host.hostname, host.source]), [ ["escaped", "escaped.example.com", "ssh-config"], + ["home-relative", "home.example.com", "ssh-config"], ["quoted", "quoted.example.com", "ssh-config"], ...unicodeFiles.map((_, index) => [ `unicode-${index}`, diff --git a/packages/ssh/src/config.ts b/packages/ssh/src/config.ts index dc3d7fad82527..069e21f9e6099 100644 --- a/packages/ssh/src/config.ts +++ b/packages/ssh/src/config.ts @@ -67,7 +67,7 @@ function parseSshConfigLine(line: string) { } function expandHomePath(input: string, homeDir: string): string { - return input.replace(/^~(?=$|\/|\\)/u, homeDir); + return input.replace(/^~(?=$|\/|\\)/u, () => homeDir); } const resolveSshConfigIncludePattern = Effect.fnUntraced(function* ( From 2b83dd0969f40be89311ad5394da49a4d951ac6e Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Sun, 15 Nov 2026 10:26:26 +0100 Subject: [PATCH 020/446] fix(server): finish closed terminal kills before shutdown (#37113) Co-authored-by: Julius Marminge --- apps/server/src/terminal/Manager.test.ts | 24 ++++++++++++++++++++++++ apps/server/src/terminal/Manager.ts | 16 ++++++++++------ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/apps/server/src/terminal/Manager.test.ts b/apps/server/src/terminal/Manager.test.ts index e7c273569a443..3df1132e99d83 100644 --- a/apps/server/src/terminal/Manager.test.ts +++ b/apps/server/src/terminal/Manager.test.ts @@ -2583,6 +2583,30 @@ it.layer( }), ); + it.effect("force-kills a closed terminal that ignores SIGTERM when the manager shuts down", () => + Effect.gen(function* () { + const scope = yield* Scope.make(); + const { manager, ptyAdapter } = yield* createManager(5, { + processKillGraceMs: 1_000, + }).pipe(Effect.provideService(Scope.Scope, scope)); + yield* manager.open(openInput()); + const process = ptyAdapter.processes[0]!; + const sigterm = yield* Deferred.make(); + process.onKill = (signal) => { + if (signal === "SIGTERM") Deferred.doneUnsafe(sigterm, Effect.void); + }; + yield* manager.close({ threadId: "thread-1" }); + yield* Deferred.await(sigterm); + + const shutdown = yield* Scope.close(scope, Exit.void).pipe(Effect.forkChild); + yield* TestClock.adjust("1 second"); + yield* Fiber.join(shutdown); + + expect(process.killSignals).toEqual(["SIGTERM", "SIGKILL"]); + expect(process.exitListenerCount).toBe(0); + }).pipe(Effect.provide(TestClock.layer())), + ); + it.effect("attaches to a running provider terminal without resolving the provider again", () => Effect.gen(function* () { const providerInstanceId = ProviderInstanceId.make("codex_work"); diff --git a/apps/server/src/terminal/Manager.ts b/apps/server/src/terminal/Manager.ts index d034af98ee1f3..d152aaa14389a 100644 --- a/apps/server/src/terminal/Manager.ts +++ b/apps/server/src/terminal/Manager.ts @@ -3070,10 +3070,10 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func yield* Effect.addFinalizer(() => Effect.gen(function* () { - const sessions = yield* modifyManagerState( + const [sessions, pendingKills] = yield* modifyManagerState( (state) => [ - [...state.sessions.values()], + [[...state.sessions.values()], [...state.killFibers.values()]], { ...state, sessions: new Map(), @@ -3090,10 +3090,14 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func yield* runKillEscalation(session.process, session.threadId, session.terminalId); }); - yield* Effect.forEach(sessions, cleanupSession, { - concurrency: "unbounded", - discard: true, - }); + // Closing workerScope next would interrupt escalations of already stopped terminals. + yield* Effect.all( + [ + Effect.forEach(sessions, cleanupSession, { concurrency: "unbounded", discard: true }), + Fiber.awaitAll(pendingKills), + ], + { concurrency: "unbounded", discard: true }, + ); }).pipe(Effect.ignoreCause({ log: true })), ); 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 021/446] 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 022/446] 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 023/446] 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 024/446] 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 025/446] 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 026/446] 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 027/446] 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 028/446] 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 029/446] 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 030/446] 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 031/446] 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 032/446] 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 033/446] 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 034/446] =?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 035/446] 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 036/446] 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 037/446] 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 038/446] 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 039/446] 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 040/446] 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 041/446] 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 042/446] 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 043/446] 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 044/446] 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 045/446] 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 046/446] 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 047/446] 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 048/446] 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 049/446] 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 050/446] 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 051/446] 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 052/446] =?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}
diff --git a/apps/web/src/components/chat/ExpandedImagePreview.tsx b/apps/web/src/components/chat/ExpandedImagePreview.tsx index 0eec6753f8e96..ddd2990000119 100644 --- a/apps/web/src/components/chat/ExpandedImagePreview.tsx +++ b/apps/web/src/components/chat/ExpandedImagePreview.tsx @@ -27,6 +27,7 @@ export interface ExpandedImageItem { /** Revoke this owned object URL; other blob URLs belong to the timeline. */ ownsSrc?: boolean; source?: SnapShotSource; + onCopySnapshot?: (() => Promise) | undefined; autoPlay?: boolean; originalUrl?: string; srcFragment?: string; @@ -162,6 +163,7 @@ export function attachVideoThumbnail(video: HTMLVideoElement, file: File): () => export function buildExpandedImagePreview( images: ReadonlyArray, selectedImageId: string, + onCopySnapshot?: (id: string) => Promise, ): ExpandedImagePreview | null { const selected = images.find((image) => image.id === selectedImageId); if (selected?.type === "file" && selected.file && isVideoAttachment(selected)) { @@ -202,7 +204,12 @@ export function buildExpandedImagePreview( src: image.src, name: image.name, mimeType: image.mimeType, - ...(image.source?.kind === "snap-shot" ? { source: image.source } : {}), + ...(image.source?.kind === "snap-shot" + ? { + source: image.source, + onCopySnapshot: onCopySnapshot ? () => onCopySnapshot(image.id) : undefined, + } + : {}), })), index: selectedIndex, }; diff --git a/apps/web/src/components/chat/SnapShotAttachmentDetails.tsx b/apps/web/src/components/chat/SnapShotAttachmentDetails.tsx index 07f16c8794e98..826b335469d09 100644 --- a/apps/web/src/components/chat/SnapShotAttachmentDetails.tsx +++ b/apps/web/src/components/chat/SnapShotAttachmentDetails.tsx @@ -1,6 +1,6 @@ import type { SnapShotSource } from "@t3tools/contracts"; -import { ImageIcon, TextIcon } from "lucide-react"; -import { Suspense } from "react"; +import { CheckIcon, CopyIcon, ImageIcon, TextIcon } from "lucide-react"; +import { Suspense, useRef, useState } from "react"; import { useRenderedAppearance } from "../../hooks/useTheme"; import { cn } from "../../lib/utils"; @@ -8,6 +8,10 @@ import { RenderErrorBoundary } from "../RenderErrorBoundary"; import { Button } from "../ui/button"; import { Popover, PopoverPopup, PopoverTitle, PopoverTrigger } from "../ui/popover"; import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; +import { + ANCHORED_COPY_TOAST_TIMEOUT_MS, + showAnchoredCopyErrorToast, +} from "../ui/anchoredCopyToast"; import { HighlightedTokens } from "./HighlightedTokens"; export const SNAP_SHOT_ATTACHMENT_FRAME_CLASS = @@ -71,14 +75,64 @@ function snapShotIncludesAccessibility(source: SnapShotSource): boolean { return Boolean(source.accessibility || source.accessibleText?.trim()); } +export function SnapShotCopyButton({ + onCopy, + side = "top", +}: { + onCopy: () => Promise; + side?: "top" | "right" | "bottom" | "left"; +}) { + const ref = useRef(null); + const [copied, setCopied] = useState(false); + + return ( + + { + event.stopPropagation(); + void onCopy().then( + () => { + setCopied(true); + setTimeout(() => setCopied(false), ANCHORED_COPY_TOAST_TIMEOUT_MS); + }, + (error: unknown) => + showAnchoredCopyErrorToast( + ref, + error instanceof Error ? error : new Error(String(error)), + ), + ); + }} + size="xs" + variant="ghost-muted" + /> + } + > + {copied ? ( + + Copy snapshot + + ); +} + export function SnapShotContentsButton({ source, className, side = "top", + onCopySnapshot, }: { source: SnapShotSource; className?: string; side?: "top" | "right" | "bottom" | "left"; + onCopySnapshot?: (() => Promise) | undefined; }) { const includesAccessibility = snapShotIncludesAccessibility(source); const ContentsIcon = includesAccessibility ? TextIcon : ImageIcon; @@ -111,7 +165,14 @@ export function SnapShotContentsButton({
- Accessibility data +
+ Accessibility data + {onCopySnapshot && ( +
+ +
+ )} +
{accessibilityDetails ? ( Promise) | undefined; className?: string; }) { return ( @@ -157,7 +220,11 @@ export function SnapShotAttachmentDetails({
{source.appName} - +
{source.windowTitle || "Captured window"} diff --git a/apps/web/src/components/chat/TimelineUserMessageRow.tsx b/apps/web/src/components/chat/TimelineUserMessageRow.tsx index 163635a9b6748..7cd57ed18c0c1 100644 --- a/apps/web/src/components/chat/TimelineUserMessageRow.tsx +++ b/apps/web/src/components/chat/TimelineUserMessageRow.tsx @@ -1,3 +1,4 @@ +import { copySnapshot as copySnapshotToClipboard } from "./copySnapshot"; import { InlineAudioPreview } from "../files/InlineAudioPreview"; import { filePreviewKind } from "@t3tools/shared/filePreview"; import { @@ -597,6 +598,25 @@ function UserTimelineRow({ row }: { row: Extract { + const image = userImages.find((candidate) => candidate.id === id); + const record = resolvedContext.records + .map(asKnownContextRecord) + .find((candidate) => candidate?.kind === "image" && candidate.attachmentId === id); + if (!image?.previewUrl || record?.kind !== "image") + throw new Error("Snapshot is no longer available."); + const fragment = encodeComposerContextFragment({ + version: 1, + source: { environmentId: ctx.activeThreadEnvironmentId, messageId: row.message.id }, + records: [{ ...record, ...(image.source ? { source: image.source } : {}) }], + }); + if (!fragment) throw new Error("The snapshot is too large to copy."); + await copySnapshotToClipboard( + image.previewUrl, + formatComposerContextReference(record), + fragment, + ); + }; const onBodyCopyCapture = (event: React.ClipboardEvent) => { if (resolvedContext.records.length === 0 || !event.clipboardData) return; const selection = window.getSelection(); @@ -661,7 +681,7 @@ function UserTimelineRow({ row }: { row: Extract { - const preview = buildExpandedImagePreview(userImages, image.id); + const preview = buildExpandedImagePreview(userImages, image.id, copySnapshot); if (preview) onImageExpand(preview); }} onOpenFile={onFileOpen} @@ -737,7 +757,7 @@ function UserTimelineRow({ row }: { row: Extract { - const preview = buildExpandedImagePreview(regularImages, image.id); + const preview = buildExpandedImagePreview(regularImages, image.id, copySnapshot); if (!preview) return; ctx.onImageExpand(preview); }} @@ -772,7 +792,10 @@ function UserTimelineRow({ row }: { row: Extract )} {image.previewUrl && image.source?.kind === "snap-shot" ? ( - + copySnapshot(image.id)} + /> ) : null}
))} diff --git a/apps/web/src/components/chat/copySnapshot.test.ts b/apps/web/src/components/chat/copySnapshot.test.ts new file mode 100644 index 0000000000000..9ac7080f7b0f7 --- /dev/null +++ b/apps/web/src/components/chat/copySnapshot.test.ts @@ -0,0 +1,75 @@ +import { ComposerContextId, EnvironmentId } from "@t3tools/contracts"; +import { + COMPOSER_CONTEXT_CLIPBOARD_MIME, + decodeComposerContextClipboardHtml, + encodeComposerContextFragment, +} from "@t3tools/shared/composerContextClipboard"; +import { expect, it } from "vite-plus/test"; + +import { copySnapshot } from "./copySnapshot"; + +it.each([false, true])( + "preserves image bytes and snapshot metadata, custom format rejected: %s", + async (rejectCustom) => { + class Item { + constructor(readonly data: Record>) {} + } + let clipboard: Item | undefined; + const write = async ([item]: Item[]) => { + if (rejectCustom && item?.data[COMPOSER_CONTEXT_CLIPBOARD_MIME]) + throw new Error("Unsupported format"); + clipboard = item; + await Promise.all(Object.values(item!.data)); + }; + const originals = { + navigator: Object.getOwnPropertyDescriptor(globalThis, "navigator"), + ClipboardItem: Object.getOwnPropertyDescriptor(globalThis, "ClipboardItem"), + }; + using _globals = { + [Symbol.dispose]() { + for (const key of ["navigator", "ClipboardItem"] as const) { + const descriptor = originals[key]; + if (descriptor) Object.defineProperty(globalThis, key, descriptor); + else Reflect.deleteProperty(globalThis, key); + } + }, + }; + Object.defineProperty(globalThis, "navigator", { + configurable: true, + value: { clipboard: { write } }, + }); + Object.defineProperty(globalThis, "ClipboardItem", { configurable: true, value: Item }); + const fragment = encodeComposerContextFragment({ + version: 1, + source: { environmentId: EnvironmentId.make("snapshot-source") }, + records: [ + { + version: 1, + kind: "image", + contextId: ComposerContextId.make("image_snapshot"), + label: "Snapshot", + attachmentId: "snapshot", + name: "snapshot.png", + mimeType: "image/png", + sizeBytes: 3, + source: { + kind: "snap-shot", + capturedAt: "2026-01-01T00:00:00.000Z", + appName: "Finder", + windowTitle: "Downloads", + accessibleText: "Downloads", + }, + }, + ], + })!; + const image = "data:image/png;base64,AQID"; + await copySnapshot(image, "Snapshot", fragment); + const png = await clipboard!.data["image/png"]!; + expect(png.type).toBe("image/png"); + expect([...new Uint8Array(await png.arrayBuffer())]).toEqual([1, 2, 3]); + expect( + decodeComposerContextClipboardHtml(await (await clipboard!.data["text/html"]!).text()), + ).toEqual(JSON.parse(fragment)); + expect(await (await clipboard!.data["text/plain"]!).text()).toBe("Snapshot"); + }, +); diff --git a/apps/web/src/components/chat/copySnapshot.ts b/apps/web/src/components/chat/copySnapshot.ts new file mode 100644 index 0000000000000..118121e753647 --- /dev/null +++ b/apps/web/src/components/chat/copySnapshot.ts @@ -0,0 +1,33 @@ +import { + COMPOSER_CONTEXT_CLIPBOARD_MIME, + encodeComposerContextClipboardHtml, +} from "@t3tools/shared/composerContextClipboard"; + +import { readMediaPng } from "../media/mediaContent"; + +export async function copySnapshot(src: string, text: string, fragment: string): Promise { + if (!navigator.clipboard?.write || typeof ClipboardItem === "undefined") { + throw new Error("Snapshot copying requires a secure browser connection."); + } + const formats = { + "image/png": readMediaPng(src), + "text/plain": new Blob([text], { type: "text/plain" }), + "text/html": new Blob([encodeComposerContextClipboardHtml(text, fragment)], { + type: "text/html", + }), + }; + // Start writing within the click gesture, before the image fetch or decode completes. + try { + await navigator.clipboard.write([ + new ClipboardItem({ + ...formats, + [COMPOSER_CONTEXT_CLIPBOARD_MIME]: new Blob([fragment], { + type: COMPOSER_CONTEXT_CLIPBOARD_MIME, + }), + }), + ]); + } catch { + // HTML carries the same snapshot metadata when custom clipboard formats are unsupported. + await navigator.clipboard.write([new ClipboardItem(formats)]); + } +} diff --git a/apps/web/src/components/chat/useComposerContextImport.browser.test.tsx b/apps/web/src/components/chat/useComposerContextImport.browser.test.tsx index e2bf9676b8b6d..d48e965895ddd 100644 --- a/apps/web/src/components/chat/useComposerContextImport.browser.test.tsx +++ b/apps/web/src/components/chat/useComposerContextImport.browser.test.tsx @@ -48,6 +48,7 @@ function deferred() { it.each([ { kind: "file", edit: "delete" }, { kind: "image", edit: "delete" }, + { kind: "image", edit: "retain" }, { kind: "file", edit: "undo" }, { kind: "file", edit: "retain" }, { kind: "file", edit: "switch" }, @@ -124,7 +125,17 @@ it.each([ const attachment = ( kind === "file" ? { ...attachmentFields, kind: "file" as const } - : { ...attachmentFields, kind: "image" as const } + : { + ...attachmentFields, + kind: "image" as const, + source: { + kind: "snap-shot" as const, + appName: "Finder", + windowTitle: "Downloads", + capturedAt: "2026-01-01T00:00:00.000Z", + accessibleText: "Downloads", + }, + } ) satisfies ComposerContextRecord; const annotation = { version: 1, @@ -182,6 +193,10 @@ it.each([ const draft = store.getComposerDraft(target)!; expect(draft.prompt).toBe(promptRef.current); const attachments = kind === "file" ? draft.files : draft.images; + if (kind === "image" && edit === "retain") + expect(draft.images[0]?.source).toEqual( + attachment.kind === "image" ? attachment.source : undefined, + ); expect(attachments.map((file) => file.name)).toEqual( edit === "undo" || edit === "retain" || diff --git a/apps/web/src/components/chat/useComposerContextImport.ts b/apps/web/src/components/chat/useComposerContextImport.ts index 0cc66d6a71d19..265278faec747 100644 --- a/apps/web/src/components/chat/useComposerContextImport.ts +++ b/apps/web/src/components/chat/useComposerContextImport.ts @@ -246,6 +246,7 @@ export function useComposerContextImport(input: { sizeBytes: file.size, previewUrl: URL.createObjectURL(file), file, + ...(record.source ? { source: record.source } : {}), }, ], { diff --git a/packages/contracts/src/composerContext.ts b/packages/contracts/src/composerContext.ts index 982202b05bc30..9884284b3f0db 100644 --- a/packages/contracts/src/composerContext.ts +++ b/packages/contracts/src/composerContext.ts @@ -1,5 +1,7 @@ import * as Schema from "effect/Schema"; +import { SnapShotSource } from "./chatAttachment.ts"; + import { AssistantCitation } from "./assistantCitations.ts"; import { EnvironmentId, @@ -99,6 +101,7 @@ export const ImageContextRecord = Schema.Struct({ ...recordBase, kind: Schema.Literal("image"), ...attachmentBinding, + source: Schema.optional(SnapShotSource), }); export type ImageContextRecord = typeof ImageContextRecord.Type; From acddfae5139ac0bda3300af4440b8f9bbea7a7de Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 13:40:33 -0800 Subject: [PATCH 201/446] fix(desktop): opening the app refreshes the t3 launcher with Local environment off (#31522) --- apps/desktop/src/app/DesktopApp.test.ts | 422 ++++++++++++++---------- apps/desktop/src/app/DesktopApp.ts | 3 + 2 files changed, 258 insertions(+), 167 deletions(-) diff --git a/apps/desktop/src/app/DesktopApp.test.ts b/apps/desktop/src/app/DesktopApp.test.ts index aca3b4fc7fe66..c35dee1ce2a3d 100644 --- a/apps/desktop/src/app/DesktopApp.test.ts +++ b/apps/desktop/src/app/DesktopApp.test.ts @@ -1,5 +1,6 @@ import { assert, it } from "@effect/vitest"; import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; +import * as NodeServices from "@effect/platform-node/NodeServices"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; @@ -54,6 +55,7 @@ import * as DesktopWslBackend from "../wsl/DesktopWslBackend.ts"; import * as BrowserImport from "../preview/BrowserImport/BrowserImport.ts"; import * as DesktopBackendConfiguration from "../backend/DesktopBackendConfiguration.ts"; import * as DesktopCliCommand from "./DesktopCliCommand.ts"; +import * as DesktopCliShim from "./DesktopCliShim.ts"; import * as DesktopClientSettings from "../settings/DesktopClientSettings.ts"; import * as DesktopConnectionCatalogStore from "./DesktopConnectionCatalogStore.ts"; import * as DesktopIpc from "../ipc/DesktopIpc.ts"; @@ -72,6 +74,178 @@ import * as ElectronWindow from "../electron/ElectronWindow.ts"; import * as MacPermissions from "../permissions/MacPermissions.ts"; import * as PreviewManager from "../preview/Manager.ts"; +interface StubbedDesktop { + readonly localEnvironmentEnabled: boolean; + readonly environment: Partial; + readonly fileSystem: Layer.Layer; + readonly record: (event: string) => Effect.Effect; + readonly windowStarted: Deferred.Deferred; + readonly registerLinuxUrlHandler: Effect.Effect; + readonly backends?: ReadonlyArray<{ readonly stop: () => Effect.Effect }>; + readonly flushRendererHistory?: Effect.Effect; + readonly tokenRequested?: Deferred.Deferred; +} + +const runStubbedDesktop = (stub: StubbedDesktop) => + Effect.gen(function* () { + const quitting = yield* Ref.make(false); + const runApp = DesktopApp.program + .pipe( + Effect.provide(NodeCrypto.layer), + Effect.provideService(DesktopAppIdentity.DesktopAppIdentity, { + resolveUserDataPath: Effect.succeed("/isolated/userdata"), + configure: stub.record("identity"), + } as DesktopAppIdentity.DesktopAppIdentity["Service"]), + Effect.provideService(ElectronApp.ElectronApp, { + setPath: () => Effect.void, + whenReady: stub.record("ready"), + } as unknown as ElectronApp.ElectronApp["Service"]), + Effect.provideService(ElectronDialog.ElectronDialog, { + showErrorBox: () => Effect.die("unexpected fatal startup error"), + } as unknown as ElectronDialog.ElectronDialog["Service"]), + Effect.provideService(DesktopEnvironment.DesktopEnvironment, { + platform: "linux", + isDevelopment: false, + isPackaged: true, + stateDir: "/isolated/userdata", + configuredBackendPort: Option.some(3773), + defaultDesktopSettings: { serverExposureMode: "local-only" }, + ...stub.environment, + } as unknown as DesktopEnvironment.DesktopEnvironment["Service"]), + Effect.provideService(DesktopPreReadyPlatform.DesktopPreReadyElectronOptions, { + linux: null, + linuxPasswordStoreCommandLine: "basic", + }), + Effect.provideService(DesktopShellEnvironment.DesktopShellEnvironment, { + installIntoProcess: Effect.succeed(true), + awaitDiscovery: Effect.succeed(true), + awaitUserShellDiscovery: Effect.succeed(true), + updates: Stream.empty, + }), + Effect.provideService(DesktopAppSettings.DesktopAppSettings, { + load: Effect.void, + get: Effect.succeed({ + localEnvironmentEnabled: stub.localEnvironmentEnabled, + serverExposureMode: "local-only", + }), + } as unknown as DesktopAppSettings.DesktopAppSettings["Service"]), + Effect.provideService(DesktopLifecycle.DesktopLifecycle, { + register: Effect.void, + relaunch: () => Effect.void, + }), + Effect.provideService(DesktopClerk.DesktopClerk, { + configure: Effect.void, + } as DesktopClerk.DesktopClerk["Service"]), + Effect.provideService(ElectronSafeStorage.ElectronSafeStorage, { + selectedStorageBackend: Effect.succeedNone, + } as unknown as ElectronSafeStorage.ElectronSafeStorage["Service"]), + Effect.provideService(PreviewPasskeys.PreviewPasskeys, { + configure: Effect.void, + } as PreviewPasskeys.PreviewPasskeys["Service"]), + Effect.provideService(DesktopApplicationMenu.DesktopApplicationMenu, { + configure: stub.record("menu"), + }), + Effect.provideService(DesktopUpdates.DesktopUpdates, { + configure: Effect.void, + } as unknown as DesktopUpdates.DesktopUpdates["Service"]), + Effect.provideService(DesktopLinuxUrlHandler.DesktopLinuxUrlHandler, { + register: stub.registerLinuxUrlHandler, + }), + ) + .pipe( + Effect.provideService(ElectronProtocol.ElectronProtocol, { + registerDesktopProtocol: () => Effect.void, + } as unknown as ElectronProtocol.ElectronProtocol["Service"]), + Effect.provideService(DesktopLegacyLocalStorage.DesktopLegacyLocalStorage, { + load: () => Effect.void, + } as unknown as DesktopLegacyLocalStorage.DesktopLegacyLocalStorage["Service"]), + Effect.provideService(DesktopSnapShot.DesktopSnapShot, { + initialize: Effect.void, + } as DesktopSnapShot.DesktopSnapShot["Service"]), + Effect.provideService(DesktopState.DesktopState, { + quitting, + } as DesktopState.DesktopState["Service"]), + Effect.provideService(DesktopWindow.DesktopWindow, { + ensureMain: stub + .record("window") + .pipe(Effect.andThen(Deferred.succeed(stub.windowStarted, undefined))), + createMainIfBackendReady: stub + .record("window") + .pipe(Effect.andThen(Deferred.succeed(stub.windowStarted, undefined))), + } as unknown as DesktopWindow.DesktopWindow["Service"]), + Effect.provideService(DesktopBackendPool.DesktopBackendPool, { + primary: Effect.succeed({ + start: stub.record("backend"), + currentConfig: Effect.succeedSome({ preflightFailure: Option.none() }), + }), + list: Effect.succeed(stub.backends ?? []), + } as unknown as DesktopBackendPool.DesktopBackendPool["Service"]), + Effect.provideService(DesktopServerExposure.DesktopServerExposure, { + configureFromSettings: () => Effect.succeed({ mode: "local-only" }), + backendConfig: Effect.succeed({ httpBaseUrl: new URL("http://127.0.0.1:3773") }), + } as unknown as DesktopServerExposure.DesktopServerExposure["Service"]), + Effect.provideService(DesktopWslBackend.DesktopWslBackend, { + reconcile: Effect.void, + } as DesktopWslBackend.DesktopWslBackend["Service"]), + Effect.provideService(DesktopAppActivation.DesktopAppActivation, { + start: Effect.void, + } as unknown as DesktopAppActivation.DesktopAppActivation["Service"]), + Effect.provideService(DesktopRendererHistory.DesktopRendererHistory, { + shutdown: stub.flushRendererHistory ?? Effect.void, + } as DesktopRendererHistory.DesktopRendererHistory["Service"]), + ); + return yield* runApp.pipe( + Effect.provide( + Layer.mergeAll( + Layer.mock(BrowserImport.BrowserImport, {}), + Layer.mock(ChildProcessSpawner.ChildProcessSpawner, {}), + Layer.mock(DesktopBackendConfiguration.DesktopBackendConfiguration, {}), + Layer.mock(DesktopCliCommand.DesktopCliCommand, {}), + Layer.mock(DesktopClientSettings.DesktopClientSettings, {}), + Layer.mock(DesktopConnectionCatalogStore.DesktopConnectionCatalogStore, {}), + Layer.mock(DesktopIpc.DesktopIpc, {}), + Layer.mock(DesktopLocalEnvironmentAuth.DesktopLocalEnvironmentAuth, { + getBearerToken: stub + .record("token") + .pipe( + Effect.andThen( + stub.tokenRequested + ? Deferred.succeed(stub.tokenRequested, undefined) + : Effect.void, + ), + Effect.as("token"), + ), + }), + Layer.mock(DesktopManagedPolicy.DesktopManagedPolicy, {}), + Layer.succeed(DesktopPrimaryPaste.DesktopPrimaryPaste, { enabled: true }), + Layer.mock(DesktopSshEnvironment.DesktopSshEnvironment, {}), + Layer.mock(DesktopSshPasswordPrompts.DesktopSshPasswordPrompts, {}), + Layer.mock(DesktopTelemetryPublisher.DesktopTelemetryPublisher, {}), + Layer.mock(DesktopWebLinks.DesktopWebLinks, {}), + Layer.mock(DesktopWslEnvironment.DesktopWslEnvironment, {}), + Layer.mock(ElectronMenu.ElectronMenu, {}), + Layer.mock(ElectronShell.ElectronShell, {}), + Layer.mock(ElectronTheme.ElectronTheme, {}), + Layer.mock(ElectronWindow.ElectronWindow, {}), + Layer.mock(MacPermissions.MacPermissions, {}), + Layer.mock(PreviewManager.PreviewManager, { + isBrowserPartition: () => { + throw new Error("unexpected browser partition lookup"); + }, + }), + stub.fileSystem, + Layer.mock(NetService.NetService, {}), + Path.layer, + Layer.succeed( + HttpClient.HttpClient, + HttpClient.make(() => Effect.die("unexpected HTTP request")), + ), + ), + ), + Effect.forkScoped, + ); + }); + it.effect.each([false, true])( "stops backends together before flushing history and cancels Linux registration, stop defect=%s", (stopDefect) => @@ -85,178 +259,42 @@ it.effect.each([false, true])( const releaseStops = yield* Deferred.make(); const historyStarted = yield* Deferred.make(); const releaseHistory = yield* Deferred.make(); - const quitting = yield* Ref.make(false); const tokenRequested = yield* Deferred.make(); const events: string[] = []; - const record = (event: string) => - Effect.sync(() => { - events.push(event); - }); - const runApp = DesktopApp.program - .pipe( - Effect.provide(NodeCrypto.layer), - Effect.provideService(DesktopAppIdentity.DesktopAppIdentity, { - resolveUserDataPath: Effect.succeed("/isolated/userdata"), - configure: record("identity"), - } as DesktopAppIdentity.DesktopAppIdentity["Service"]), - Effect.provideService(ElectronApp.ElectronApp, { - setPath: () => Effect.void, - whenReady: record("ready"), - } as unknown as ElectronApp.ElectronApp["Service"]), - Effect.provideService(ElectronDialog.ElectronDialog, { - showErrorBox: () => Effect.die("unexpected fatal startup error"), - } as unknown as ElectronDialog.ElectronDialog["Service"]), - Effect.provideService(DesktopEnvironment.DesktopEnvironment, { - platform: "linux", - isDevelopment: false, - isPackaged: true, - stateDir: "/isolated/userdata", - configuredBackendPort: Option.some(3773), - defaultDesktopSettings: { serverExposureMode: "local-only" }, - } as unknown as DesktopEnvironment.DesktopEnvironment["Service"]), - Effect.provideService(DesktopPreReadyPlatform.DesktopPreReadyElectronOptions, { - linux: null, - linuxPasswordStoreCommandLine: "basic", - }), - Effect.provideService(DesktopShellEnvironment.DesktopShellEnvironment, { - installIntoProcess: Effect.succeed(true), - awaitDiscovery: Effect.succeed(true), - awaitUserShellDiscovery: Effect.succeed(true), - updates: Stream.empty, + const app = yield* runStubbedDesktop({ + localEnvironmentEnabled: true, + environment: {}, + fileSystem: FileSystem.layerNoop({}), + record: (event) => + Effect.sync(() => { + events.push(event); }), - Effect.provideService(DesktopAppSettings.DesktopAppSettings, { - load: Effect.void, - get: Effect.succeed({ - localEnvironmentEnabled: true, - serverExposureMode: "local-only", - }), - } as unknown as DesktopAppSettings.DesktopAppSettings["Service"]), - Effect.provideService(DesktopLifecycle.DesktopLifecycle, { - register: Effect.void, - relaunch: () => Effect.void, - }), - Effect.provideService(DesktopClerk.DesktopClerk, { - configure: Effect.void, - } as DesktopClerk.DesktopClerk["Service"]), - Effect.provideService(ElectronSafeStorage.ElectronSafeStorage, { - selectedStorageBackend: Effect.succeedNone, - } as unknown as ElectronSafeStorage.ElectronSafeStorage["Service"]), - Effect.provideService(PreviewPasskeys.PreviewPasskeys, { - configure: Effect.void, - } as PreviewPasskeys.PreviewPasskeys["Service"]), - Effect.provideService(DesktopApplicationMenu.DesktopApplicationMenu, { - configure: record("menu"), - }), - Effect.provideService(DesktopUpdates.DesktopUpdates, { - configure: Effect.void, - } as unknown as DesktopUpdates.DesktopUpdates["Service"]), - Effect.provideService(DesktopLinuxUrlHandler.DesktopLinuxUrlHandler, { - register: Deferred.succeed(registrationStarted, undefined).pipe( - Effect.andThen(Deferred.await(releaseRegistration)), - Effect.ensuring(Deferred.succeed(registrationStopped, undefined)), - ), - }), - ) - .pipe( - Effect.provideService(ElectronProtocol.ElectronProtocol, { - registerDesktopProtocol: () => Effect.void, - } as unknown as ElectronProtocol.ElectronProtocol["Service"]), - Effect.provideService(DesktopLegacyLocalStorage.DesktopLegacyLocalStorage, { - load: () => Effect.void, - } as unknown as DesktopLegacyLocalStorage.DesktopLegacyLocalStorage["Service"]), - Effect.provideService(DesktopSnapShot.DesktopSnapShot, { - initialize: Effect.void, - } as DesktopSnapShot.DesktopSnapShot["Service"]), - Effect.provideService(DesktopState.DesktopState, { - quitting, - } as DesktopState.DesktopState["Service"]), - Effect.provideService(DesktopWindow.DesktopWindow, { - ensureMain: record("window").pipe( - Effect.andThen(Deferred.succeed(windowStarted, undefined)), - ), - } as unknown as DesktopWindow.DesktopWindow["Service"]), - Effect.provideService(DesktopBackendPool.DesktopBackendPool, { - primary: Effect.succeed({ - start: record("backend"), - currentConfig: Effect.succeedSome({ preflightFailure: Option.none() }), - }), - list: Effect.succeed([ - { - stop: () => - Deferred.succeed(firstStopStarted, undefined).pipe( - Effect.andThen(Deferred.await(releaseStops)), - Effect.andThen(stopDefect ? Effect.die("stop defect") : Effect.void), - ), - }, - { - stop: () => - Deferred.succeed(secondStopStarted, undefined).pipe( - Effect.andThen(Deferred.await(releaseStops)), - ), - }, - ]), - } as unknown as DesktopBackendPool.DesktopBackendPool["Service"]), - Effect.provideService(DesktopServerExposure.DesktopServerExposure, { - configureFromSettings: () => Effect.succeed({ mode: "local-only" }), - backendConfig: Effect.succeed({ httpBaseUrl: new URL("http://127.0.0.1:3773") }), - } as unknown as DesktopServerExposure.DesktopServerExposure["Service"]), - Effect.provideService(DesktopWslBackend.DesktopWslBackend, { - reconcile: Effect.void, - } as DesktopWslBackend.DesktopWslBackend["Service"]), - Effect.provideService(DesktopAppActivation.DesktopAppActivation, { - start: Effect.void, - } as unknown as DesktopAppActivation.DesktopAppActivation["Service"]), - Effect.provideService(DesktopRendererHistory.DesktopRendererHistory, { - shutdown: Deferred.succeed(historyStarted, undefined).pipe( - Effect.andThen(Deferred.await(releaseHistory)), - Effect.asVoid, - ), - } as DesktopRendererHistory.DesktopRendererHistory["Service"]), - ); - const app = yield* runApp.pipe( - Effect.provide( - Layer.mergeAll( - Layer.mock(BrowserImport.BrowserImport, {}), - Layer.mock(ChildProcessSpawner.ChildProcessSpawner, {}), - Layer.mock(DesktopBackendConfiguration.DesktopBackendConfiguration, {}), - Layer.mock(DesktopCliCommand.DesktopCliCommand, {}), - Layer.mock(DesktopClientSettings.DesktopClientSettings, {}), - Layer.mock(DesktopConnectionCatalogStore.DesktopConnectionCatalogStore, {}), - Layer.mock(DesktopIpc.DesktopIpc, {}), - Layer.mock(DesktopLocalEnvironmentAuth.DesktopLocalEnvironmentAuth, { - getBearerToken: record("token").pipe( - Effect.andThen(Deferred.succeed(tokenRequested, undefined)), - Effect.as("token"), + windowStarted, + tokenRequested, + registerLinuxUrlHandler: Deferred.succeed(registrationStarted, undefined).pipe( + Effect.andThen(Deferred.await(releaseRegistration)), + Effect.ensuring(Deferred.succeed(registrationStopped, undefined)), + ), + backends: [ + { + stop: () => + Deferred.succeed(firstStopStarted, undefined).pipe( + Effect.andThen(Deferred.await(releaseStops)), + Effect.andThen(stopDefect ? Effect.die("stop defect") : Effect.void), ), - }), - Layer.mock(DesktopManagedPolicy.DesktopManagedPolicy, {}), - Layer.succeed(DesktopPrimaryPaste.DesktopPrimaryPaste, { enabled: true }), - Layer.mock(DesktopSshEnvironment.DesktopSshEnvironment, {}), - Layer.mock(DesktopSshPasswordPrompts.DesktopSshPasswordPrompts, {}), - Layer.mock(DesktopTelemetryPublisher.DesktopTelemetryPublisher, {}), - Layer.mock(DesktopWebLinks.DesktopWebLinks, {}), - Layer.mock(DesktopWslEnvironment.DesktopWslEnvironment, {}), - Layer.mock(ElectronMenu.ElectronMenu, {}), - Layer.mock(ElectronShell.ElectronShell, {}), - Layer.mock(ElectronTheme.ElectronTheme, {}), - Layer.mock(ElectronWindow.ElectronWindow, {}), - Layer.mock(MacPermissions.MacPermissions, {}), - Layer.mock(PreviewManager.PreviewManager, { - isBrowserPartition: () => { - throw new Error("unexpected browser partition lookup"); - }, - }), - FileSystem.layerNoop({}), - Layer.mock(NetService.NetService, {}), - Path.layer, - Layer.succeed( - HttpClient.HttpClient, - HttpClient.make(() => Effect.die("unexpected HTTP request")), - ), - ), + }, + { + stop: () => + Deferred.succeed(secondStopStarted, undefined).pipe( + Effect.andThen(Deferred.await(releaseStops)), + ), + }, + ], + flushRendererHistory: Deferred.succeed(historyStarted, undefined).pipe( + Effect.andThen(Deferred.await(releaseHistory)), + Effect.asVoid, ), - Effect.forkScoped, - ); + }); yield* Deferred.await(registrationStarted); yield* Deferred.await(windowStarted); yield* Deferred.await(tokenRequested); @@ -283,3 +321,53 @@ it.effect.each([false, true])( assert.isTrue(yield* Deferred.isDone(registrationStopped)); }).pipe(Effect.provide(DesktopShutdown.layer)), ); + +it.effect("points a stale t3 launcher at the updated AppImage with Local environment off", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-desktop-app-" }); + const launcher = DesktopCliShim.launcherPath({ + path, + baseDir, + platform: "linux", + } as DesktopEnvironment.DesktopEnvironment["Service"]); + const removedAppImage = path.join(baseDir, "T3-Code-0.1.6-x86_64.AppImage"); + const updatedAppImage = path.join(baseDir, "T3-Code-0.1.7-x86_64.AppImage"); + yield* fs.makeDirectory(path.dirname(launcher), { recursive: true }); + yield* fs.writeFileString( + launcher, + DesktopCliShim.renderCliShim({ + target: { kind: "appimage", appImage: removedAppImage, executableName: "t3code" }, + shimPath: launcher, + t3Home: baseDir, + }), + ); + yield* fs.writeFileString(updatedAppImage, "", { mode: 0o755 }); + const windowStarted = yield* Deferred.make(); + + const app = yield* runStubbedDesktop({ + localEnvironmentEnabled: false, + environment: { + path, + baseDir, + serverRoot: path.join(baseDir, "resources", "app.asar"), + appImagePath: Option.some(updatedAppImage), + }, + fileSystem: Layer.succeed(FileSystem.FileSystem, fs), + record: () => Effect.void, + windowStarted, + registerLinuxUrlHandler: Effect.void, + }); + yield* Deferred.await(windowStarted); + yield* (yield* DesktopShutdown.DesktopShutdown).request; + yield* Fiber.join(app); + + const content = yield* fs.readFileString(launcher); + assert.include( + content, + `app='${updatedAppImage}'`, + "startup left the t3 launcher pointing at the removed AppImage", + ); + }).pipe(Effect.provide(Layer.mergeAll(DesktopShutdown.layer, NodeServices.layer))), +); diff --git a/apps/desktop/src/app/DesktopApp.ts b/apps/desktop/src/app/DesktopApp.ts index f69bf965d0058..56fe979cf8c67 100644 --- a/apps/desktop/src/app/DesktopApp.ts +++ b/apps/desktop/src/app/DesktopApp.ts @@ -15,6 +15,7 @@ import { installDesktopIpcHandlers } from "../ipc/DesktopIpcHandlers.ts"; import * as DesktopAppActivation from "./DesktopAppActivation.ts"; import * as DesktopAppIdentity from "./DesktopAppIdentity.ts"; import * as DesktopClerk from "./DesktopClerk.ts"; +import * as DesktopCliShim from "./DesktopCliShim.ts"; import * as DesktopCliCommand from "./DesktopCliCommand.ts"; import * as DesktopApplicationMenu from "../window/DesktopApplicationMenu.ts"; import * as DesktopWindow from "../window/DesktopWindow.ts"; @@ -301,6 +302,8 @@ const bootstrap = Effect.fn("desktop.bootstrap")(function* (backendStarted: bool ); if (!settings.localEnvironmentEnabled) { + // The backend config refreshes the launcher when the local environment runs; an update may move the app. + yield* DesktopCliShim.install; if (!(yield* Ref.get(state.quitting))) { yield* desktopWindow.createMainIfBackendReady; } From a6f7981abba71a51b299bd0ecbf7577d20660541 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 13:41:38 -0800 Subject: [PATCH 202/446] fix(pi): ask_user_question single-select questions render as a structured card (#32125) --- .../provider-pi/src/server/adapter.test.ts | 289 ++++++++++++++++++ packages/provider-pi/src/server/adapter.ts | 147 ++++++++- 2 files changed, 434 insertions(+), 2 deletions(-) diff --git a/packages/provider-pi/src/server/adapter.test.ts b/packages/provider-pi/src/server/adapter.test.ts index ff9f9c22ef976..31d0a70cae48a 100644 --- a/packages/provider-pi/src/server/adapter.test.ts +++ b/packages/provider-pi/src/server/adapter.test.ts @@ -4895,6 +4895,295 @@ describe("PiAdapterV2", () => { }).pipe(Effect.scoped, Effect.provide(layerTest)), ); + it.effect("renders an ask_user_question select from the tool call's structured question", () => + Effect.gen(function* () { + const fake = yield* makeFakePi; + const { runtime, takeEvent } = yield* openRuntime(fake); + const providerThread = yield* runtime.ensureThread({ + threadId: THREAD_ID, + modelSelection: modelSelection("default"), + runtimePolicy, + }); + yield* startTurn(runtime, providerThread); + yield* fake.takeRequest("prompt"); + yield* fake.emit({ + type: "tool_execution_start", + toolCallId: "ask-call", + toolName: "ask_user_question", + args: { + questions: [ + { + header: "Area", + question: "Which area should we work on?", + options: [ + { + label: "Docker/Compose", + description: "Review or edit Compose projects", + preview: "services:\n web: {}", + }, + { label: "Home Assistant", description: "Automations, dashboards or YAML" }, + ], + }, + ], + }, + }); + const dockerLine = "1. Docker/Compose — Review or edit Compose projects"; + yield* fake.emit({ + type: "extension_ui_request", + id: "ui-ask", + method: "select", + title: + "[Area] Which area should we work on?\n\n--- 1. Docker/Compose preview ---\nservices:\n web: {}", + options: [ + dockerLine, + "2. Home Assistant — Automations, dashboards or YAML", + "3. Type something.", + ], + }); + const event = yield* takeEvent( + (event) => + event.type === "turn_item.updated" && event.turnItem.type === "user_input_request", + ); + if (event.type !== "turn_item.updated" || event.turnItem.type !== "user_input_request") { + return assert.fail("expected a user input request"); + } + const question = decodeUserInputQuestions(event.turnItem.questions)[0]; + assert.deepEqual( + { + header: question?.header, + question: question?.question, + options: question?.options.map(({ label, description }) => ({ label, description })), + }, + { + header: "Area", + question: + "Which area should we work on?\n\n--- 1. Docker/Compose preview ---\nservices:\n web: {}", + options: [ + { label: "Docker/Compose", description: "Review or edit Compose projects" }, + { label: "Home Assistant", description: "Automations, dashboards or YAML" }, + ], + }, + ); + yield* runtime.respondToRuntimeRequest({ + requestId: event.turnItem.requestId, + answers: { [question!.id]: question!.options[0]!.value }, + }); + const response = yield* fake.takeRequest("extension_ui_response"); + assert.equal(response["id"], "ui-ask"); + assert.equal(response["value"], dockerLine); + }).pipe(Effect.scoped, Effect.provide(layerTest)), + ); + + it.effect("matches an ask_user_question select to the question with the same full title", () => + Effect.gen(function* () { + const fake = yield* makeFakePi; + const { runtime, takeEvent } = yield* openRuntime(fake); + const providerThread = yield* runtime.ensureThread({ + threadId: THREAD_ID, + modelSelection: modelSelection("default"), + runtimePolicy, + }); + yield* startTurn(runtime, providerThread); + yield* fake.takeRequest("prompt"); + const options = [ + { label: "Yes", description: "Go ahead" }, + { label: "No", description: "Stop here" }, + ]; + yield* fake.emit({ + type: "tool_execution_start", + toolCallId: "ask-call", + toolName: "ask_user_question", + // Some models JSON-encode `questions`; the extension decodes it before asking. + args: { + questions: JSON.stringify([ + { header: "Deploy", question: "Proceed?", options }, + { header: "Deploy", question: "Proceed? Only for staging?", options }, + ]), + }, + }); + yield* fake.emit({ + type: "extension_ui_request", + id: "ui-ask", + method: "select", + title: "[Deploy] Proceed? Only for staging?", + options: ["1. Yes — Go ahead", "2. No — Stop here", "3. Type something."], + }); + const event = yield* takeEvent( + (event) => + event.type === "turn_item.updated" && event.turnItem.type === "user_input_request", + ); + if (event.type !== "turn_item.updated" || event.turnItem.type !== "user_input_request") { + return assert.fail("expected a user input request"); + } + const question = decodeUserInputQuestions(event.turnItem.questions)[0]; + assert.equal(question?.question, "Proceed? Only for staging?"); + // Option lines that differ from the recorded call keep the raw select. + const unmatchedLines = ["1. Yes — Go", "2. No — Stop here", "3. Type something."]; + yield* fake.emit({ + type: "extension_ui_request", + id: "ui-unmatched", + method: "select", + title: "[Deploy] Proceed?", + options: unmatchedLines, + }); + const unmatched = yield* takeEvent( + (event) => + event.type === "turn_item.updated" && event.turnItem.type === "user_input_request", + ); + if ( + unmatched.type !== "turn_item.updated" || + unmatched.turnItem.type !== "user_input_request" + ) { + return assert.fail("expected a user input request"); + } + const unmatchedQuestion = decodeUserInputQuestions(unmatched.turnItem.questions)[0]; + assert.deepEqual( + { + header: unmatchedQuestion?.header, + question: unmatchedQuestion?.question, + options: unmatchedQuestion?.options.map(({ label, value }) => [label, value]), + }, + { + header: "Question", + question: "[Deploy] Proceed?", + options: unmatchedLines.map((line) => [line, line]), + }, + ); + }).pipe(Effect.scoped, Effect.provide(layerTest)), + ); + + it.effect("answers each ask_user_question custom text through its follow-up input", () => + Effect.gen(function* () { + const fake = yield* makeFakePi; + const { runtime, takeEvent } = yield* openRuntime(fake); + const providerThread = yield* runtime.ensureThread({ + threadId: THREAD_ID, + modelSelection: modelSelection("default"), + runtimePolicy, + }); + yield* startTurn(runtime, providerThread); + yield* fake.takeRequest("prompt"); + const args = { + questions: [ + { + question: "How many replicas?", + options: [ + { label: "One", description: "Single instance" }, + { label: "Three", description: "High availability" }, + ], + }, + ], + }; + // Parallel questionnaires can ask the same question. + yield* fake.emit({ + type: "tool_execution_start", + toolCallId: "ask-call", + toolName: "ask_user_question", + args, + }); + yield* fake.emit({ + type: "tool_execution_start", + toolCallId: "ask-call-parallel", + toolName: "ask_user_question", + args, + }); + const lines = ["1. One — Single instance", "2. Three — High availability"]; + const followUpTitle = `How many replicas?\n\n${lines.join("\n")}\n\nType your answer:`; + const takeWaitingRequest = Effect.fnUntraced(function* () { + const event = yield* takeEvent( + (event) => + event.type === "turn_item.updated" && + event.turnItem.type === "user_input_request" && + event.turnItem.status === "waiting", + ); + if (event.type !== "turn_item.updated" || event.turnItem.type !== "user_input_request") { + return assert.fail("expected a user input request"); + } + return event.turnItem; + }); + const answerWithCustomText = Effect.fnUntraced(function* (id: string, text: string) { + yield* fake.emit({ + type: "extension_ui_request", + id, + method: "select", + title: "How many replicas?", + options: [...lines, "3. Type something."], + }); + const request = yield* takeWaitingRequest(); + yield* runtime.respondToRuntimeRequest({ + requestId: request.requestId, + answers: { [request.questions[0]!.id]: text }, + }); + const selectResponse = yield* fake.takeRequest("extension_ui_response"); + assert.equal(selectResponse["value"], "3. Type something."); + }); + // The extension reads a bare number sent as the select's value as that option. + yield* answerWithCustomText("ui-ask", "2"); + yield* answerWithCustomText("ui-ask-parallel", "5"); + // Parallel dialogs can arrive before the follow-up input, including a multi-select of the + // same question, which the extension asks as an input with a non-empty placeholder. + const multiSelectTitle = `How many replicas?\n\n${lines.join("\n")}\n\nEnter the numbers of all that apply.`; + yield* fake.emit({ + type: "extension_ui_request", + id: "ui-multi", + method: "input", + title: multiSelectTitle, + placeholder: "1,3; custom answer", + }); + // Another extension's blank-placeholder input that only shares the question is asked. + const unrelatedTitle = "How many replicas?\n\nEnter the release note:"; + yield* fake.emit({ + type: "extension_ui_request", + id: "ui-unrelated", + method: "input", + title: unrelatedTitle, + placeholder: "", + }); + yield* fake.emit({ + type: "extension_ui_request", + id: "ui-other", + method: "input", + title: "Anything else?", + }); + assert.equal((yield* takeWaitingRequest()).title, multiSelectTitle); + assert.equal((yield* takeWaitingRequest()).title, unrelatedTitle); + assert.equal((yield* takeWaitingRequest()).title, "Anything else?"); + for (const [id, value] of [ + ["ui-custom", "2"], + ["ui-custom-parallel", "5"], + ]) { + yield* fake.emit({ + type: "extension_ui_request", + id, + method: "input", + title: followUpTitle, + placeholder: "", + }); + const inputResponse = yield* fake.takeRequest("extension_ui_response"); + assert.deepEqual({ id: inputResponse["id"], value: inputResponse["value"] }, { id, value }); + } + // An answer staged when the turn ends is dropped, so a later matching input is asked. + yield* answerWithCustomText("ui-ask-late", "7"); + yield* fake.emit({ type: "agent_end", messages: [], willRetry: false }); + yield* fake.emit({ type: "agent_settled" }); + yield* takeEvent((event) => event.type === "turn.terminal"); + yield* fake.emit({ + type: "extension_ui_request", + id: "ui-custom-late", + method: "input", + title: followUpTitle, + placeholder: "", + }); + yield* fake.emit({ + type: "extension_ui_request", + id: "ui-after", + method: "input", + title: "Anything else?", + }); + assert.equal((yield* takeWaitingRequest()).title, followUpTitle); + }).pipe(Effect.scoped, Effect.provide(layerTest)), + ); + it.effect.each(["Which database?\n1. Postgres\n2. Type something.\nType your answer:", " "])( "asks an input dialog's title when its placeholder is blank, with title '%s'", (title) => diff --git a/packages/provider-pi/src/server/adapter.ts b/packages/provider-pi/src/server/adapter.ts index 6694623d98883..418db7219cb95 100644 --- a/packages/provider-pi/src/server/adapter.ts +++ b/packages/provider-pi/src/server/adapter.ts @@ -409,6 +409,17 @@ interface PendingPiPrompt { runtimeRequest: OrchestrationV2RuntimeRequest; readonly node: OrchestrationV2ExecutionNode; readonly turnItem: OrchestrationV2TurnItem; + readonly customAnswerRoute: PiCustomAnswerRoute | undefined; +} + +/** + * The ask_user_question extension misreads typed text sent as a select value, so a custom answer + * picks the select's last "Type something." line and answers the input dialog that follows. + */ +interface PiCustomAnswerRoute { + readonly lines: ReadonlyArray; + readonly sentinel: string; + readonly followUpTitlePrefix: string; } function piApprovalRequestKind(title: string): "command" | "file-change" { @@ -601,6 +612,9 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* ( ProviderAdapter.ProviderAdapterV2Error | Cause.Done >(); const pendingPrompts = new Map(); + // Custom answers waiting for their ask_user_question follow-up input, keyed by title prefix. + // FIFO per prefix: the extension asks each follow-up right after its own select is answered. + const customAnswerFollowUps = new Map(); const sessionApprovals = new Set(); // Async pi-subagents runs launched in this session, by async id, until they settle. const piAsyncRuns = new Map< @@ -1685,6 +1699,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* ( ) { const pending = Array.from(pendingPrompts.values()); pendingPrompts.clear(); + customAnswerFollowUps.clear(); yield* Effect.forEach(pending, (prompt) => cancelPrompt(prompt, resolvedAt), { discard: true, }); @@ -1777,6 +1792,24 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* ( return; } if (nativeRequestId === undefined) return; + // The extension's follow-up input has an empty placeholder; its multi-select input does not. + const inputTitle = + method === "input" && event["placeholder"] === "" + ? (recordString(event, "title") ?? "") + : ""; + const followUpPrefix = Array.from(customAnswerFollowUps.keys()) + .filter((prefix) => inputTitle.startsWith(prefix)) + .reduce( + (longest, prefix) => (prefix.length > (longest?.length ?? -1) ? prefix : longest), + undefined, + ); + if (followUpPrefix !== undefined) { + const queued = customAnswerFollowUps.get(followUpPrefix) ?? []; + const value = queued.shift() ?? ""; + if (queued.length === 0) customAnswerFollowUps.delete(followUpPrefix); + yield* connection.send({ type: "extension_ui_response", id: nativeRequestId, value }); + return; + } const approvalTitle = recordString(event, "title") ?? ""; const approvalKey = `${approvalTitle.length}:${approvalTitle}${recordString(event, "message") ?? ""}`; if (method === "confirm" && sessionApprovals.has(approvalKey)) { @@ -1851,6 +1884,10 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* ( completedAt: null, updatedAt: createdAt, }; + const askUser = + method === "select" && turn !== null + ? piAskUserSingleSelectQuestion(nativeRequestId, title, event, turn.toolArgs) + : undefined; const turnItem: OrchestrationV2TurnItem = method === "confirm" ? { @@ -1864,7 +1901,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* ( ...itemBase, type: "user_input_request", requestId, - questions: [piQuestion(nativeRequestId, method, title, event)], + questions: [askUser?.question ?? piQuestion(nativeRequestId, method, title, event)], }; const pending: PendingPiPrompt = { nativeRequestId, @@ -1876,6 +1913,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* ( runtimeRequest, node, turnItem, + customAnswerRoute: askUser?.customAnswerRoute, }; pendingPrompts.set(String(requestId), pending); yield* emit({ @@ -3575,7 +3613,20 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* ( `Pi extension request ${requestInput.requestId} has expired`, ); } - const response = piUiResponse(pending, requestInput.decision, requestInput.answers); + let response = piUiResponse(pending, requestInput.decision, requestInput.answers); + const route = pending.customAnswerRoute; + if ( + route !== undefined && + typeof response["value"] === "string" && + response["value"].trim().length > 0 && + !route.lines.includes(response["value"]) + ) { + customAnswerFollowUps.set(route.followUpTitlePrefix, [ + ...(customAnswerFollowUps.get(route.followUpTitlePrefix) ?? []), + response["value"], + ]); + response = { value: route.sentinel }; + } yield* connection.send({ type: "extension_ui_response", id: pending.nativeRequestId, @@ -4170,6 +4221,98 @@ function piQuestion( }; } +/** The extension normalizes line terminators in each question field before building the dialog. */ +const rpivField = (input: unknown, key: string) => + recordString(input, key)?.replace(/\r\n/g, "\n").replace(/\r/g, ""); + +const rpivOptionLine = (option: unknown, index: number) => + // oxlint-disable-next-line t3code/no-em-dash-copy -- Matches the extension's option line format. + `${index + 1}. ${rpivField(option, "label")} — ${rpivField(option, "description")}`; + +/** The extension folds option previews into the select title, the only place clients show them. */ +const rpivPreviewBlock = (options: ReadonlyArray) => { + const blocks = options.flatMap((option, index) => { + const preview = rpivField(option, "preview"); + return preview + ? [`--- ${index + 1}. ${rpivField(option, "label")} preview ---\n${preview.slice(0, 600)}`] + : []; + }); + return blocks.length > 0 ? `\n\n${blocks.join("\n\n")}` : ""; +}; + +/** Mirrors the extension's `prepareQuestionArguments`: some models JSON-encode nested args. */ +const rpivDecoded = (value: unknown, isShape: (parsed: unknown) => boolean) => { + if (typeof value !== "string") return value; + try { + const parsed: unknown = JSON.parse(value); + return isShape(parsed) ? parsed : value; + } catch { + return value; + } +}; + +/** + * @juicesharp/rpiv-ask-user-question asks a single-select question over RPC as a select of + * "N. label — description" lines plus a trailing "Type something." entry. Rebuild the question + * from the tool call's arguments; each option answers with its original line, which the extension + * maps back to that option. + */ +function piAskUserSingleSelectQuestion( + questionId: string, + title: string, + event: PiRpcRecord, + toolArgs: ReadonlyMap, +): + | { question: OrchestrationV2UserInputQuestion; customAnswerRoute: PiCustomAnswerRoute } + | undefined { + const lines = event["options"]; + if (!Array.isArray(lines) || lines.length < 2) return undefined; + const sentinel = lines.at(-1); + if (typeof sentinel !== "string") return undefined; + for (const args of toolArgs.values()) { + const questions = rpivDecoded(recordField(args, "questions"), Array.isArray); + if (!Array.isArray(questions)) continue; + for (const entry of questions) { + const question = rpivDecoded(entry, Predicate.isObject); + const text = rpivField(question, "question"); + const header = rpivField(question, "header"); + if (recordField(question, "multiSelect") === true || !text?.trim()) continue; + const prefix = header ? `[${header}] ` : ""; + if (!title.startsWith(`${prefix}${text}`)) continue; + const encodedOptions = rpivDecoded(recordField(question, "options"), Array.isArray); + if (!Array.isArray(encodedOptions) || encodedOptions.length !== lines.length - 1) continue; + const options = encodedOptions.map((option) => rpivDecoded(option, Predicate.isObject)); + const body = `${text}${rpivPreviewBlock(options)}`; + if (title !== `${prefix}${body}`) continue; + if (!options.every((option, index) => lines[index] === rpivOptionLine(option, index))) { + continue; + } + return { + question: { + id: questionId, + header: header?.trim() || "Question", + question: body.trim(), + options: options.map((option, index) => { + const label = rpivField(option, "label")?.trim() || `Option ${index + 1}`; + return { + label, + description: rpivField(option, "description")?.trim() || label, + value: rpivOptionLine(option, index), + }; + }), + }, + // The follow-up repeats the option lines; only its trailing instruction is localized. + customAnswerRoute: { + lines, + sentinel, + followUpTitlePrefix: `${prefix}${text}\n\n${lines.slice(0, -1).join("\n")}\n\n`, + }, + }; + } + } + return undefined; +} + function piUiResponse( pending: PendingPiPrompt, decision: ProviderApprovalDecision | undefined, From a10f9e41d378de696f1bb2f1c69580c5df59af18 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 13:42:44 -0800 Subject: [PATCH 203/446] fix(server): pull request links whose reads keep failing back off instead of re-reading every minute (#31969) --- .../PullRequestSyncReactor.test.ts | 88 +++++++++++++++++++ .../PullRequestSyncReactor.ts | 19 +++- 2 files changed, 106 insertions(+), 1 deletion(-) diff --git a/apps/server/src/orchestration-v2/PullRequestSyncReactor.test.ts b/apps/server/src/orchestration-v2/PullRequestSyncReactor.test.ts index bb0b3380c38c4..4cb350a380a84 100644 --- a/apps/server/src/orchestration-v2/PullRequestSyncReactor.test.ts +++ b/apps/server/src/orchestration-v2/PullRequestSyncReactor.test.ts @@ -325,6 +325,24 @@ const sweepAgain = Effect.fn("sweepPullRequestSyncHarness")(function* ( yield* reactor.drain; }); +const sweepFor = Effect.fn("sweepPullRequestSyncHarnessFor")(function* ( + fixture: Harness, + reactor: PullRequestSyncReactor.PullRequestSyncReactor["Service"], + minutes: number, +) { + for (let minute = 0; minute < minutes; minute++) yield* sweepAgain(fixture, reactor); +}); + +const failingGitLabLink = makeLink(13, null, { + host: "gitlab.com", + url: "https://gitlab.com/owner/repository/-/merge_requests/13", +}); + +const gitLabCliFailure = new PullRequestOperationError({ + operation: "summary", + detail: "GitLab CLI command failed", +}); + function runUpdated( threadId: ThreadId, status: OrchestrationV2Run["status"], @@ -1839,6 +1857,76 @@ describe("PullRequestSyncReactor", () => { }), ); + it.effect( + "caps the failed-read backoff at the slow cadence and restarts it after a success", + () => + Effect.scoped( + Effect.gen(function* () { + yield* TestClock.setTime(Date.parse(NOW)); + const failing = yield* Ref.make(true); + const fixture = yield* makeHarness({ + snapshot: makeSnapshot([makeThread("one", { pullRequests: [failingGitLabLink] })]), + summary: (input) => + Ref.get(failing).pipe( + Effect.flatMap((fails) => + fails ? Effect.fail(gitLabCliFailure) : Effect.succeed(makeSummary(input)), + ), + ), + }); + yield* Effect.gen(function* () { + const reactor = yield* startAndSweep(fixture); + const readMinutes = [0]; + const sweepMinutes = Effect.fnUntraced(function* (from: number, to: number) { + for (let minute = from; minute <= to; minute++) { + const before = (yield* Ref.get(fixture.summaryCalls)).length; + yield* sweepAgain(fixture, reactor); + if ((yield* Ref.get(fixture.summaryCalls)).length > before) + readMinutes.push(minute); + } + }); + + yield* sweepMinutes(1, 60); + assert.deepStrictEqual(readMinutes, [0, 1, 3, 7, 15, 30, 45, 60]); + + yield* Ref.set(failing, false); + yield* sweepMinutes(61, 75); + yield* Ref.set(failing, true); + readMinutes.length = 0; + yield* sweepMinutes(76, 80); + assert.deepStrictEqual(readMinutes, [76, 77, 79]); + }).pipe(Effect.provide(fixture.layer)); + }), + ), + ); + + it.effect("reads a backed-off link at once when a refresh asks, then keeps backing off", () => + Effect.scoped( + Effect.gen(function* () { + yield* TestClock.setTime(Date.parse(NOW)); + const fixture = yield* makeHarness({ + snapshot: makeSnapshot([makeThread("one", { pullRequests: [failingGitLabLink] })]), + summary: () => Effect.fail(gitLabCliFailure), + }); + yield* Effect.gen(function* () { + const reactor = yield* startAndSweep(fixture); + yield* sweepFor(fixture, reactor, 4); + const backedOffReads = (yield* Ref.get(fixture.summaryCalls)).length; + + yield* reactor.requestSync(failingGitLabLink); + yield* Queue.take(fixture.snapshotReads); + yield* reactor.drain; + assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, backedOffReads + 1); + + // The refresh failed at minute 4 with the backoff doubled to 8 minutes. + yield* sweepFor(fixture, reactor, 7); + assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, backedOffReads + 1); + yield* sweepAgain(fixture, reactor); + assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, backedOffReads + 2); + }).pipe(Effect.provide(fixture.layer)); + }), + ), + ); + it.effect.each([ "gh pr merge 7 --squash 2>&1 | tail -3", "cafe pr merge 7 --squash", diff --git a/apps/server/src/orchestration-v2/PullRequestSyncReactor.ts b/apps/server/src/orchestration-v2/PullRequestSyncReactor.ts index 96f0be4e59dc6..d962ee8f672d0 100644 --- a/apps/server/src/orchestration-v2/PullRequestSyncReactor.ts +++ b/apps/server/src/orchestration-v2/PullRequestSyncReactor.ts @@ -48,6 +48,8 @@ export const QUIET_SYNC_INTERVAL_MS = 5 * 60 * 1_000; // A pull request closed this long ago is rarely reopened, so the sweep stops reading it, as it // does merged ones. A reader that sees it open, or a requested refresh, still reads it at once. const CLOSED_FINAL_AFTER_MS = 24 * 60 * 60 * 1_000; +// A link whose host read failed waits a sweep, then twice as long after each further failure. +const FAILED_READ_BACKOFF_MS = 60 * 1_000; const isChangeRequestCloseCommand = (command: string) => sourceControlDefinitions.definitions.some( @@ -369,6 +371,7 @@ export const make = Effect.gen(function* () { // linking dozens of pull requests) is read together and shares the summary batches. let requestedSweepQueued = false; const retryStacks = new Set(); + const failedReads = new Map(); // Group keys the host answered not-found for, or this server cannot read (no CLI, signed out, // unsupported host); they are read on the slow cadence until a read succeeds. const missing = new Set(); @@ -376,6 +379,7 @@ export const make = Effect.gen(function* () { const pausedUntil = new Map(); const isDue = (key: string, entries: ReadonlyArray, nowMs: number): boolean => { + if ((failedReads.get(key)?.retryAtMs ?? 0) > nowMs) return false; if (requested.has(key)) return true; const active = entries.filter((entry) => isUnsettled(entry.thread)); if (active.length === 0) return false; @@ -425,12 +429,17 @@ export const make = Effect.gen(function* () { // A host-level refresh reaches every project, each retaining its own pending retry. for (const [key, entries] of groups) { - if (pendingRequests.has(threadPullRequestKeyOf(entries[0]!.link))) requested.add(key); + if (!pendingRequests.has(threadPullRequestKeyOf(entries[0]!.link))) continue; + requested.add(key); + // A new request reads at once; a failure after it backs off further, not from the start. + const failed = failedReads.get(key); + if (failed !== undefined) failedReads.set(key, { ...failed, retryAtMs: nowMs }); } pendingRequests.clear(); for (const key of lastSyncedAt.keys()) if (!groups.has(key)) lastSyncedAt.delete(key); for (const key of retryStacks) if (!groups.has(key)) retryStacks.delete(key); + for (const key of failedReads.keys()) if (!groups.has(key)) failedReads.delete(key); for (const key of missing) if (!groups.has(key)) missing.delete(key); for (const key of requested.keys()) if (!groups.has(key)) requested.delete(key); @@ -599,6 +608,7 @@ export const make = Effect.gen(function* () { } // The host answered, so the cadence clock ticks even if a dispatch below is rejected. lastSyncedAt.set(key, nowMs); + failedReads.delete(key); requested.delete(key); yield* Effect.forEach( entries, @@ -624,6 +634,13 @@ export const make = Effect.gen(function* () { const retryAt = rateLimitRetryAt(cause); if (retryAt !== undefined) { pausedUntil.set(pauseKey, Math.max(retryAt, pausedUntil.get(pauseKey) ?? 0)); + } else { + const previous = failedReads.get(key); + const backoffMs = + previous === undefined + ? FAILED_READ_BACKOFF_MS + : Math.min(previous.backoffMs * 2, SLOW_SYNC_INTERVAL_MS); + failedReads.set(key, { retryAtMs: nowMs + backoffMs, backoffMs }); } const reason = skipReason(cause); const skip = skips.get(reason); From c24255826be5aabb0f32ee18f6ac7dec0b3f1347 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 13:45:07 -0800 Subject: [PATCH 204/446] feat(web): dictation keeps recording into its thread when you switch away (#35761) Co-authored-by: Claude Opus 5.5 --- .../components/AppRouteModal.browser.test.tsx | 2 + apps/web/src/components/AppRouteModal.tsx | 2 + apps/web/src/components/ThreadRouteView.tsx | 6 +- apps/web/src/components/chat/ChatComposer.tsx | 30 +- .../components/chat/ComposerSpeechButton.tsx | 17 +- .../chat/GlobalVoiceInputIndicator.tsx | 52 +++ .../src/components/chat/backgroundThread.ts | 3 + .../VoiceSettingsPanel.browser.test.tsx | 1 + apps/web/src/composerDraftStore.ts | 21 + apps/web/src/routes/_chat.tsx | 2 + ...useEnvironmentSpeechInput.browser.test.tsx | 81 +++- .../src/speech/useEnvironmentSpeechInput.ts | 320 +++++-------- apps/web/src/speech/voiceInputSession.ts | 424 ++++++++++++++++++ 13 files changed, 755 insertions(+), 206 deletions(-) create mode 100644 apps/web/src/components/chat/GlobalVoiceInputIndicator.tsx create mode 100644 apps/web/src/speech/voiceInputSession.ts diff --git a/apps/web/src/components/AppRouteModal.browser.test.tsx b/apps/web/src/components/AppRouteModal.browser.test.tsx index 004e07744e22e..c0864d31c178f 100644 --- a/apps/web/src/components/AppRouteModal.browser.test.tsx +++ b/apps/web/src/components/AppRouteModal.browser.test.tsx @@ -5,6 +5,8 @@ import { render } from "../../test/render"; import { useCommitOnBlur } from "../hooks/useCommitOnBlur"; import { AppRouteModal } from "./AppRouteModal"; +vi.mock("./chat/GlobalVoiceInputIndicator", () => ({ GlobalVoiceInputIndicator: () => null })); + function CommitOnBlurField({ onCommit }: { onCommit: (next: string) => void }) { return ; } diff --git a/apps/web/src/components/AppRouteModal.tsx b/apps/web/src/components/AppRouteModal.tsx index 0c8a8a670d2f6..f4cde43a8cccc 100644 --- a/apps/web/src/components/AppRouteModal.tsx +++ b/apps/web/src/components/AppRouteModal.tsx @@ -4,6 +4,7 @@ import { useRef, useState, type CSSProperties, type ReactNode } from "react"; import { useNativeTitlebarHeight } from "../lib/windowControlsOverlay"; import { useCloseAppModal } from "./sidebar/mainAppLocation"; +import { GlobalVoiceInputIndicator } from "./chat/GlobalVoiceInputIndicator"; import { Button } from "./ui/button"; import { DialogBackdrop } from "./ui/dialog"; import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip"; @@ -89,6 +90,7 @@ export function AppRouteModal({ label, children }: { label: string; children: Re className="absolute inset-x-0 top-0 hidden h-(--app-modal-titlebar-height) [-webkit-app-region:drag] max-sm:block" /> {children} + diff --git a/apps/web/src/components/ThreadRouteView.tsx b/apps/web/src/components/ThreadRouteView.tsx index a7b7da0a6b53b..5217b068f9a35 100644 --- a/apps/web/src/components/ThreadRouteView.tsx +++ b/apps/web/src/components/ThreadRouteView.tsx @@ -8,7 +8,7 @@ import { Navigate, useNavigate } from "@tanstack/react-router"; import { useEffect, useState } from "react"; import ChatView from "./ChatView"; -import { BackgroundThreadContext } from "./chat/backgroundThread"; +import { BackgroundThreadContext, HiddenThreadContext } from "./chat/backgroundThread"; import { SidebarInset } from "./ui/sidebar"; import { useComposerDraftStore } from "../composerDraftStore"; import { useSidebarPendingFileDropStore } from "../sidebarPendingFileDropStore"; @@ -229,7 +229,9 @@ export function ThreadRouteView({ className="h-svh min-h-0 overflow-hidden overscroll-y-none md:h-dvh" inert={background} > - {view} + + {view} + {background ? null : redirect} ); diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index 8fb98edc14466..69250e47b89c3 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -4,7 +4,7 @@ import { useComposerTypingGuard } from "./useComposerTypingGuard"; import { insertAttachmentReferencesForDraft } from "./composerAttachmentReferences"; import type { ComposerAttachmentInsertion } from "../composerAttachmentInsertion"; import { DESKTOP_PASTE_AS_TEXT_EVENT } from "../../lib/desktopPasteAsText"; -import { BackgroundThreadContext } from "./backgroundThread"; +import { BackgroundThreadContext, HiddenThreadContext } from "./backgroundThread"; import { resolveCompatibleRuntimeMode, runtimeModeConfig, @@ -107,9 +107,12 @@ import { composerFileNeedsReattach, composerDraftHasUserContent, composerTargetKey, + readComposerDraftPrompt, + resolveLiveComposerDraftTarget, useComposerDraftStore, useComposerThreadDraft, } from "../../composerDraftStore"; +import { buildDraftThreadRouteParams, buildThreadRouteParams } from "../../threadRoutes"; import { ComposerStashBadge } from "./ComposerStashBadge"; import { ComposerStashMenu } from "./ComposerStashMenu"; import { useComposerTriggerState } from "./useComposerTriggerState"; @@ -1052,6 +1055,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) }); const backgroundThread = use(BackgroundThreadContext); + const hiddenThread = use(HiddenThreadContext); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to keydown, window blur and the desktop paste-as-text event to arm plain-text paste useEffect(() => { // A background composer listens on its own form, so it never hears the main composer's keys. @@ -1846,6 +1850,29 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) projectName, ownerKey: questionAttachmentTarget ?? composerDraftTargetKey, disabled: dictationDisabled, + label: activeThread?.title ?? null, + presented: !hiddenThread, + // A question's answer lives only in this composer, so only the thread draft outlives it. + persistentDraft: questionAttachmentTarget + ? undefined + : { + readStoredText: () => readComposerDraftPrompt(composerDraftTarget), + commitStoredText: (text) => { + const live = resolveLiveComposerDraftTarget(composerDraftTarget); + if (live !== null) setComposerDraftPrompt(live, text); + }, + // The route, not the draft target: a queued-message edit has a draft id but no route. + open: () => + void (routeKind === "draft" && typeof composerDraftTarget === "string" + ? navigate({ + to: "/draft/$draftId", + params: buildDraftThreadRouteParams(composerDraftTarget), + }) + : navigate({ + to: "/$environmentId/$threadId", + params: buildThreadRouteParams(routeThreadRef), + })), + }, readDraft: () => { const snapshot = readComposerSnapshot(); return { @@ -3828,6 +3855,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) finishShortcutLabel={dictationShortcutLabel} cancelShortcutLabel="Esc" level={speechInput.level} + recordingStartedAt={speechInput.recordingStartedAt} disabled={dictationDisabled} onStart={() => void speechInput.start()} onStop={() => void speechInput.stop()} diff --git a/apps/web/src/components/chat/ComposerSpeechButton.tsx b/apps/web/src/components/chat/ComposerSpeechButton.tsx index ca9ef2a278ad6..9f7eb4f37ab74 100644 --- a/apps/web/src/components/chat/ComposerSpeechButton.tsx +++ b/apps/web/src/components/chat/ComposerSpeechButton.tsx @@ -43,18 +43,25 @@ function formatElapsed(seconds: number) { return `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, "0")}`; } -function RecordingStatus(props: { level: number; recordingLimitSeconds?: number | undefined }) { - const [elapsedSeconds, setElapsedSeconds] = useState(0); +function RecordingStatus(props: { + level: number; + recordingStartedAt?: number | null | undefined; + recordingLimitSeconds?: number | undefined; +}) { + const { recordingStartedAt } = props; + const [elapsedSeconds, setElapsedSeconds] = useState(() => + recordingStartedAt ? Math.max(0, Math.floor((Date.now() - recordingStartedAt) / 1_000)) : 0, + ); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- owns the timer displaying the native recording duration useEffect(() => { - const startedAt = Date.now(); + const startedAt = recordingStartedAt ?? Date.now(); const timer = window.setInterval( () => setElapsedSeconds(Math.floor((Date.now() - startedAt) / 1_000)), 250, ); return () => window.clearInterval(timer); - }, []); + }, [recordingStartedAt]); return ( <> @@ -125,6 +132,7 @@ export function ComposerSpeechRecordingPill(props: { finishShortcutLabel?: string | null; cancelShortcutLabel?: string | null; level: number; + recordingStartedAt?: number | null | undefined; recordingLimitSeconds?: number | undefined; /** Compact matches the `xs` buttons beside text-field editors. */ size?: "default" | "compact"; @@ -181,6 +189,7 @@ export function ComposerSpeechRecordingPill(props: { {recording ? ( ) : ( diff --git a/apps/web/src/components/chat/GlobalVoiceInputIndicator.tsx b/apps/web/src/components/chat/GlobalVoiceInputIndicator.tsx new file mode 100644 index 0000000000000..3bc724fe1962a --- /dev/null +++ b/apps/web/src/components/chat/GlobalVoiceInputIndicator.tsx @@ -0,0 +1,52 @@ +import { useVoiceTranscriptionDestination } from "../../speech/useEnvironmentSpeechInput"; +import { + useRetainedVoiceInputSession, + useVoiceInputSessionActivity, + useVoiceInputSessionSnapshot, + useVoiceOwnerPresented, +} from "../../speech/voiceInputSession"; +import { Button } from "../ui/button"; +import { ComposerSpeechRecordingPill } from "./ComposerSpeechButton"; + +/** + * Keeps the chosen destination's dictation alive across navigation and, while the draft it + * records into is off screen, shows it so it can be finished, cancelled, or returned to. + */ +export function GlobalVoiceInputIndicator({ hidden = false }: { hidden?: boolean }) { + const { prepared, grantId, cloudProvider, cloudModel } = useVoiceTranscriptionDestination(); + const session = useRetainedVoiceInputSession({ prepared, grantId, cloudProvider, cloudModel }); + const { state, target, recordingStartedAt } = useVoiceInputSessionSnapshot(session); + const presented = useVoiceOwnerPresented(target?.ownerKey ?? null); + const { level } = useVoiceInputSessionActivity( + hidden || presented ? null : session, + target?.ownerKey ?? null, + ); + if (hidden || !session || !target || state.phase === "idle" || presented) return null; + + return ( +
+
+ + void session.start(target)} + onStop={() => void session.stop()} + onCancel={() => session.cancel()} + onSkipPostProcessing={() => session.skipPostProcessing()} + /> +
+
+ ); +} diff --git a/apps/web/src/components/chat/backgroundThread.ts b/apps/web/src/components/chat/backgroundThread.ts index b3dfee09a663c..e402bc5e62165 100644 --- a/apps/web/src/components/chat/backgroundThread.ts +++ b/apps/web/src/components/chat/backgroundThread.ts @@ -5,3 +5,6 @@ import { createContext } from "react"; * parent's panel; its global shortcuts and listeners stand down. */ export const BackgroundThreadContext = createContext(false); + +/** True only for a thread kept mounted out of sight under Settings or Usage. */ +export const HiddenThreadContext = createContext(false); diff --git a/apps/web/src/components/settings/VoiceSettingsPanel.browser.test.tsx b/apps/web/src/components/settings/VoiceSettingsPanel.browser.test.tsx index 4c8172333ff98..c0107483461cc 100644 --- a/apps/web/src/components/settings/VoiceSettingsPanel.browser.test.tsx +++ b/apps/web/src/components/settings/VoiceSettingsPanel.browser.test.tsx @@ -328,6 +328,7 @@ it("tests the selected model after download and preserves its transcript across state: { phase: "idle", error: null, errorAction: null }, preview: null, level: 0, + recordingStartedAt: null, preparing: false, blocksSubmission: false, freezesEditor: false, diff --git a/apps/web/src/composerDraftStore.ts b/apps/web/src/composerDraftStore.ts index 4066715c53b2c..3c64aa0f65817 100644 --- a/apps/web/src/composerDraftStore.ts +++ b/apps/web/src/composerDraftStore.ts @@ -1660,6 +1660,27 @@ function getComposerDraftState( return state.draftsByThreadKey[threadKey] ?? null; } +/** Where a draft lives now: its thread once promoted, or null once it was discarded. */ +export function resolveLiveComposerDraftTarget( + target: ComposerThreadTarget, +): ComposerThreadTarget | null { + if (typeof target !== "string") return target; + const state = composerDraftStore.getState(); + const promoted = state.promotedDraftRedirects[target]; + if (promoted) return promoted; + return state.draftThreadsByThreadKey[target] || state.draftsByThreadKey[target] ? target : null; +} + +/** The stored prompt for a target, empty when it has no draft, or null when the target is gone. */ +export function readComposerDraftPrompt(target: ComposerThreadTarget): string | null { + const live = resolveLiveComposerDraftTarget(target); + if (live === null) return null; + const state = composerDraftStore.getState(); + const threadKey = resolveComposerDraftKey(state, live); + if (!threadKey) return null; + return state.draftsByThreadKey[threadKey]?.prompt ?? ""; +} + function draftThreadHasOpenPreview(draftThread: DraftThreadState | undefined): boolean { if (draftThread === undefined) return false; const ref = scopeThreadRef(draftThread.environmentId, draftThread.threadId); diff --git a/apps/web/src/routes/_chat.tsx b/apps/web/src/routes/_chat.tsx index ad04d04d29493..c65ed390288f8 100644 --- a/apps/web/src/routes/_chat.tsx +++ b/apps/web/src/routes/_chat.tsx @@ -1,6 +1,7 @@ import { Outlet, createFileRoute, redirect } from "@tanstack/react-router"; import { ThreadRouteView } from "../components/ThreadRouteView"; +import { GlobalVoiceInputIndicator } from "../components/chat/GlobalVoiceInputIndicator"; import { isAppModalPage } from "../components/sidebar/mainAppLocation"; import { SidebarInset } from "../components/ui/sidebar"; import { resolveThreadRouteTarget } from "../threadRoutes"; @@ -24,6 +25,7 @@ function ChatRouteLayout() { )} {modalOpen ? : null} +