From 3ac8b1f587ed34ed038030055a66e2ac52a35216 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 13 Nov 2026 08:43:07 -0800 Subject: [PATCH 001/250] fix(acp): bound unread notification retention by size (#23217) --- packages/effect-acp/src/protocol.test.ts | 42 ++++++++++++++++++++++++ packages/effect-acp/src/protocol.ts | 17 ++++++---- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/packages/effect-acp/src/protocol.test.ts b/packages/effect-acp/src/protocol.test.ts index 27f949c3a7807..19f58292ac469 100644 --- a/packages/effect-acp/src/protocol.test.ts +++ b/packages/effect-acp/src/protocol.test.ts @@ -239,6 +239,48 @@ it.layer(NodeServices.layer)("effect-acp protocol", (it) => { }), ); + it.effect("evicts unread raw notifications while a slow reader handles an earlier one", () => + Effect.gen(function* () { + const { stdio, input } = yield* makeInMemoryStdio(); + const handled = yield* Queue.unbounded(); + const transport = yield* AcpProtocol.makeAcpPatchedProtocol({ + stdio, + serverRequestMethods: new Set(), + onNotification: (notification) => + Queue.offer(handled, decodeIndex(notification.params).index).pipe(Effect.asVoid), + }); + const send = (index: number, textLength: number) => + Queue.offer(input, largeNotification(index, textLength)).pipe( + Effect.andThen(Queue.take(handled)), + ); + + yield* send(1, 6 * MIB); + yield* send(2, 6 * MIB); + const entered = yield* Deferred.make(); + const release = yield* Deferred.make(); + const reader = yield* transport.incoming.pipe( + Stream.map((notification) => decodeIndex(notification.params).index), + Stream.tap((index) => + index === 1 + ? Deferred.succeed(entered, undefined).pipe(Effect.andThen(Deferred.await(release))) + : Effect.void, + ), + Stream.take(4), + Stream.runCollect, + Effect.forkScoped, + ); + yield* Deferred.await(entered); + + // Notification 2 is still unread, so 3 and 4 push it past the budget. + yield* send(3, 6 * MIB); + yield* send(4, 6 * MIB); + yield* send(5, 1); + yield* Deferred.succeed(release, undefined); + + assert.deepEqual(yield* Fiber.join(reader), [1, 3, 4, 5]); + }), + ); + it.effect("delivers every raw notification to a reader that keeps up, then ends", () => Effect.gen(function* () { const { stdio, input } = yield* makeInMemoryStdio(); diff --git a/packages/effect-acp/src/protocol.ts b/packages/effect-acp/src/protocol.ts index 8ae077e4c5cd9..490c27d8e0b08 100644 --- a/packages/effect-acp/src/protocol.ts +++ b/packages/effect-acp/src/protocol.ts @@ -313,9 +313,8 @@ export const makeAcpPatchedProtocol = Effect.fn("makeAcpPatchedProtocol")(functi const serverQueue = yield* Queue.unbounded(); const clientQueue = yield* Queue.unbounded(); const rawNotifications = yield* Queue.unbounded>(); - // JSON length of the notifications offered since the queue was last empty. - // `incoming` takes the whole backlog on every pull, so an empty queue holds - // nothing, and a read can only make this an overestimate. + // JSON length of the notifications still in the queue. `incoming` takes one + // at a time, so a slow reader holds at most one outside this budget. let rawNotificationsJsonLength = 0; // Raw readers finish with the connection: termination ends the queue, and so // does closing the scope, which interrupts the reader before it terminates. @@ -494,7 +493,6 @@ export const makeAcpPatchedProtocol = Effect.fn("makeAcpPatchedProtocol")(functi // so readers never see more than the limits allow. const bufferRawNotification = (notification: AcpIncomingNotification, lineLength: number) => Effect.sync(() => { - if (Queue.sizeUnsafe(rawNotifications) === 0) rawNotificationsJsonLength = 0; if (!Queue.offerUnsafe(rawNotifications, { notification, lineLength })) return; rawNotificationsJsonLength += lineLength; while ( @@ -1101,8 +1099,15 @@ export const makeAcpPatchedProtocol = Effect.fn("makeAcpPatchedProtocol")(functi clientProtocol, serverProtocol, get incoming() { - return Stream.fromQueue(rawNotifications).pipe( - Stream.map(({ notification }) => notification), + return Stream.fromPull( + Effect.succeed( + Queue.take(rawNotifications).pipe( + Effect.map(({ notification, lineLength }) => { + rawNotificationsJsonLength -= lineLength; + return [notification] as const; + }), + ), + ), ); }, request: sendRequest, From 00f617db4e97dbfc4d713f69d9f90394e7d3b12a Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 13 Nov 2026 08:43:22 -0800 Subject: [PATCH 002/250] fix(client-runtime): recover subscriptions that stop on a healthy connection (#24400) --- .../client-runtime/src/rpc/client.test.ts | 138 +++++++++++++++++- packages/client-runtime/src/rpc/client.ts | 56 ++++++- .../client-runtime/src/state/runtime.test.ts | 104 +++++++++++++ 3 files changed, 289 insertions(+), 9 deletions(-) diff --git a/packages/client-runtime/src/rpc/client.test.ts b/packages/client-runtime/src/rpc/client.test.ts index 1d0005e35f0a4..de99737a28dd0 100644 --- a/packages/client-runtime/src/rpc/client.test.ts +++ b/packages/client-runtime/src/rpc/client.test.ts @@ -7,15 +7,17 @@ import { } from "@t3tools/contracts"; import { describe, expect, it } from "@effect/vitest"; import * as Cause from "effect/Cause"; +import * as Clock from "effect/Clock"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; import * as Fiber from "effect/Fiber"; import * as Option from "effect/Option"; 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 * as TestClock from "effect/testing/TestClock"; import * as SubscriptionRef from "effect/SubscriptionRef"; +import * as TestClock from "effect/testing/TestClock"; import { RpcClientError } from "effect/rpc"; import { @@ -45,6 +47,8 @@ const INSTALL_DOWNLOADING: RelayClientInstallProgressEvent = { stage: "downloading", }; +const decodeNumber = Schema.decodeUnknownEffect(Schema.Number); + function session(client: WsRpcProtocolClient): RpcSession.RpcSession { return { client, @@ -265,6 +269,8 @@ describe("environment RPC", () => { while (subscriptions.length < 2) { yield* Queue.take(subscriptionChanges); } + // The pending same-session retry must not resubscribe the replaced session. + yield* TestClock.adjust("1 minute"); yield* Fiber.interrupt(subscriptionFiber); expect(subscriptions).toEqual(["first", "second"]); @@ -272,6 +278,136 @@ describe("environment RPC", () => { }), ); + // The session stays healthy (`closed` never resolves), as when one stream chunk fails to decode, + // so the supervisor will not replace it and only resubscribing restores delivery. + const resumesOnTheSameSession = Effect.fn("TestEnvironmentRpc.resumesOnTheSameSession")( + function* (streamFailure: Stream.Stream) { + const resumedValue = { source: "resumed" } as unknown as ServerLifecycleStreamEvent; + const firstSubscribed = yield* Deferred.make(); + let subscriptions = 0; + const client = { + [WS_METHODS.subscribeServerLifecycle]: () => { + subscriptions += 1; + return subscriptions === 1 + ? Stream.fromEffect(Deferred.succeed(firstSubscribed, undefined)).pipe( + Stream.drain, + Stream.concat(streamFailure), + ) + : Stream.make(resumedValue).pipe(Stream.concat(Stream.never)); + }, + } as unknown as WsRpcProtocolClient; + const { activeSession, retryCount, supervisor } = yield* makeHarness(); + + yield* SubscriptionRef.set(activeSession, Option.some(session(client))); + const resultFiber = yield* subscribe(WS_METHODS.subscribeServerLifecycle, {}).pipe( + Stream.runHead, + Effect.timeoutOption("2 seconds"), + Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), + Effect.forkChild, + ); + yield* Deferred.await(firstSubscribed); + yield* TestClock.adjust("2 seconds"); + + expect( + yield* Fiber.join(resultFiber), + "durable subscription stopped delivering after its stream ended on a healthy session", + ).toEqual(Option.some(Option.some(resumedValue))); + expect(subscriptions).toBe(2); + expect(yield* Ref.get(retryCount)).toBe(0); + }, + ); + + it.effect("resubscribes on the same session after a transport failure", () => + resumesOnTheSameSession( + Stream.fail( + new RpcClientError.RpcClientError({ + reason: new RpcClientError.RpcClientDefect({ + message: "stream failed", + cause: new Error("stream failed"), + }), + }), + ), + ), + ); + + it.effect("resubscribes on the same session after a stream chunk fails to decode", () => + // Mirrors RpcClient, which decodes each chunk with `Effect.orDie`. + resumesOnTheSameSession( + Stream.fromEffect(decodeNumber("not a number").pipe(Effect.orDie)).pipe(Stream.drain), + ), + ); + + it.effect( + "backs off a same-session stream that keeps failing to decode after its first value", + () => + Effect.gen(function* () { + // Like the shell stream: a valid value (metadata) arrives, then a chunk fails to decode. + const value = { source: "metadata" } as unknown as ServerLifecycleStreamEvent; + const decodeDefect = decodeNumber("not a number").pipe(Effect.orDie); + const subscriptions = yield* Queue.unbounded<{ + readonly session: "first" | "second"; + readonly at: number; + }>(); + let secondSubscriptions = 0; + const failingClient = (name: "first" | "second") => + ({ + [WS_METHODS.subscribeServerLifecycle]: () => + Stream.unwrap( + Effect.gen(function* () { + yield* Queue.offer(subscriptions, { + session: name, + at: yield* Clock.currentTimeMillis, + }); + if (name === "second" && ++secondSubscriptions > 2) return Stream.never; + return Stream.make(value).pipe( + Stream.concat(Stream.fromEffect(decodeDefect).pipe(Stream.drain)), + ); + }), + ), + }) as unknown as WsRpcProtocolClient; + const { activeSession, retryCount, supervisor } = yield* makeHarness(); + + yield* SubscriptionRef.set(activeSession, Option.some(session(failingClient("first")))); + const subscriptionFiber = yield* subscribe( + WS_METHODS.subscribeServerLifecycle, + {}, + { + onExpectedFailure: () => Effect.void, + retryExpectedFailureAfter: "250 millis", + }, + ).pipe( + Stream.runDrain, + Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), + Effect.forkChild, + ); + + const expectedFirstSessionDelays = [250, 500, 1_000, 2_000, 4_000, 8_000, 16_000, 30_000]; + const firstSessionDelays: number[] = []; + let previous = yield* Queue.take(subscriptions); + for (const delay of expectedFirstSessionDelays) { + yield* TestClock.adjust(delay); + const next = yield* Queue.take(subscriptions); + firstSessionDelays.push(next.at - previous.at); + previous = next; + } + + // A replacement session starts a fresh backoff. + yield* SubscriptionRef.set(activeSession, Option.some(session(failingClient("second")))); + const replaced = yield* Queue.take(subscriptions); + yield* TestClock.adjust(250); + const retried = yield* Queue.take(subscriptions); + yield* Fiber.interrupt(subscriptionFiber); + + expect( + firstSessionDelays, + "same-session decode retries reset to the base delay after every first value", + ).toEqual(expectedFirstSessionDelays); + expect([replaced.session, retried.session]).toEqual(["second", "second"]); + expect(retried.at - replaced.at).toBe(250); + expect(yield* Ref.get(retryCount)).toBe(0); + }), + ); + it.effect("surfaces domain subscription failures without reconnecting", () => Effect.gen(function* () { const domainError = new Error("terminal subscription rejected"); diff --git a/packages/client-runtime/src/rpc/client.ts b/packages/client-runtime/src/rpc/client.ts index 4e3dad24e7a12..ee70400ca47a5 100644 --- a/packages/client-runtime/src/rpc/client.ts +++ b/packages/client-runtime/src/rpc/client.ts @@ -114,6 +114,7 @@ export const isAuthorizationFailure = (cause: Cause.Cause) => ); const MAX_EXPECTED_FAILURE_RETRY_DELAY_MS = 30_000; +const STREAM_FAILURE_RETRY_DELAY = Duration.seconds(1); export const subscriptionRetryDelay = (base: Duration.Input, retries: number) => Duration.millis( @@ -318,6 +319,8 @@ function subscribeDynamicMapped( EnvironmentRpcStreamFailure >; let expectedFailureRetries = 0; + // Not reset by a first value: a stream can emit before every chunk failure. + let streamFailureRetries = 0; const subscribeToSession = (): Stream.Stream> => Stream.suspend(() => Stream.unwrap( @@ -354,6 +357,11 @@ function subscribeDynamicMapped( : Effect.void, ), Stream.catchCause((cause) => { + const retryAfter = (base: Duration.Input, retries: number) => + Stream.fromEffect(Effect.sleep(subscriptionRetryDelay(base, retries))).pipe( + Stream.drain, + Stream.concat(subscribeToSession()), + ); const hasOnlyExpectedFailures = cause.reasons.length > 0 && cause.reasons.every((reason) => reason._tag === "Fail"); @@ -362,7 +370,22 @@ function subscribeDynamicMapped( cause.reasons.every( (reason) => reason._tag === "Fail" && isRpcClientError(reason.error), ); - if (isTransportFailure) { + // The RPC client dies with a SchemaError when one stream chunk fails to + // decode, ending only that request while the socket stays open. A caller + // with `onDefect` owns its defect recovery. + const isChunkDecodeDefect = + options?.onDefect === undefined && + cause.reasons.length > 0 && + cause.reasons.every( + (reason) => reason._tag === "Die" && Schema.isSchemaError(reason.defect), + ); + // Both end one stream while the session can stay healthy, so nothing else + // would replace it. A dead session is replaced by the supervisor, which + // interrupts this retry through switchMap. A finite search must settle + // rather than restart, so it keeps waiting for the next session. + const resubscribesOnSameSession = + tag !== ORCHESTRATION_V2_WS_METHODS.searchThreadStream; + if (isTransportFailure && !resubscribesOnSameSession) { return Stream.fromEffect( Effect.logWarning( "Durable RPC subscription lost its transport; waiting for the next session.", @@ -374,6 +397,29 @@ function subscribeDynamicMapped( ), ).pipe(Stream.drain); } + if ( + resubscribesOnSameSession && + (isTransportFailure || isChunkDecodeDefect) + ) { + return Stream.fromEffect( + Effect.logWarning( + "Durable RPC subscription stream ended; resubscribing.", + { + cause: Cause.pretty(cause), + method: tag, + environmentId: supervisor.target.environmentId, + }, + ), + ).pipe( + Stream.drain, + Stream.concat( + retryAfter( + options?.retryExpectedFailureAfter ?? STREAM_FAILURE_RETRY_DELAY, + streamFailureRetries++, + ), + ), + ); + } if (hasOnlyExpectedFailures && options?.onExpectedFailure !== undefined) { const handled = Stream.fromEffect(options.onExpectedFailure(cause)).pipe( Stream.drain, @@ -384,16 +430,10 @@ function subscribeDynamicMapped( ) { return handled; } - const retryDelay = subscriptionRetryDelay( - options.retryExpectedFailureAfter, - expectedFailureRetries, - ); - expectedFailureRetries += 1; return handled.pipe( Stream.concat( - Stream.fromEffect(Effect.sleep(retryDelay)).pipe(Stream.drain), + retryAfter(options.retryExpectedFailureAfter, expectedFailureRetries++), ), - Stream.concat(subscribeToSession()), ); } // A reported defect parks until the next session or resubscribe. Failing would diff --git a/packages/client-runtime/src/state/runtime.test.ts b/packages/client-runtime/src/state/runtime.test.ts index 0d2c1ba56088d..63f00b28fb173 100644 --- a/packages/client-runtime/src/state/runtime.test.ts +++ b/packages/client-runtime/src/state/runtime.test.ts @@ -18,6 +18,7 @@ import { WS_METHODS, } from "@t3tools/contracts"; import * as Cause from "effect/Cause"; +import * as Clock from "effect/Clock"; import * as DateTime from "effect/DateTime"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; @@ -29,6 +30,7 @@ import * as Queue from "effect/Queue"; import * as Schema from "effect/Schema"; import * as Stream from "effect/Stream"; import * as SubscriptionRef from "effect/SubscriptionRef"; +import * as TestClock from "effect/testing/TestClock"; import type { HttpClient } from "effect/http"; import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity"; @@ -43,6 +45,7 @@ import { import * as EnvironmentRegistry from "../connection/registry.ts"; import * as EnvironmentSupervisor from "../connection/supervisor.ts"; import { EnvironmentRpcUnavailableError } from "../rpc/client.ts"; +import type { WsRpcProtocolClient } from "../rpc/protocol.ts"; import type * as RpcSession from "../rpc/session.ts"; import { createOrchestrationEnvironmentAtoms } from "./orchestration.ts"; import { createReviewEnvironmentAtoms } from "./review.ts"; @@ -54,6 +57,7 @@ import { createTerminalEnvironmentAtoms } from "./terminal.ts"; import { createAtomCommandScheduler, createEnvironmentQueryAtomFamily, + createEnvironmentRpcSubscriptionAtomFamily, selectEnvironmentQueryData, createEnvironmentSubscriptionAtomFamily, createRuntimeCommand, @@ -70,6 +74,8 @@ const QUERY_ENVIRONMENT = new PrimaryConnectionTarget({ wsBaseUrl: "wss://query.example.test", }); +const decodeNumber = Schema.decodeUnknownEffect(Schema.Number); + const QUERY_RPC_SESSION = {} as RpcSession.RpcSession; class TestQueryError extends Schema.TaggedError()("TestQueryError", { @@ -393,6 +399,104 @@ describe("finite environment subscription lifecycle", () => { expect(observed.some((result) => AsyncResult.isFailure(result))).toBe(false); }), ); + it.effect( + "surfaces a decode defect from a finite RPC subscription instead of resubscribing", + () => + Effect.scoped( + Effect.gen(function* () { + const firstSubscribed = yield* Deferred.make(); + const resubscribed = yield* Deferred.make(); + const surfaced = yield* Deferred.make>(); + let subscriptions = 0; + // Mirrors RpcClient, which decodes each chunk with `Effect.orDie`. + const decodeDefect = decodeNumber("not a number").pipe(Effect.orDie); + const client = { + [ORCHESTRATION_V2_WS_METHODS.searchThreadStream]: () => { + subscriptions += 1; + Deferred.doneUnsafe(subscriptions === 1 ? firstSubscribed : resubscribed, Exit.void); + return Stream.fromEffect(decodeDefect).pipe(Stream.drain); + }, + } as unknown as WsRpcProtocolClient; + const rpcSession = { + client, + initialConfig: Effect.never, + subscribeServerConfig: () => Stream.never, + ready: Effect.void, + probe: Effect.void, + closed: Effect.never, + } as unknown as RpcSession.RpcSession; + const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ + target: QUERY_ENVIRONMENT, + state: yield* SubscriptionRef.make(queryConnectionState()), + session: yield* SubscriptionRef.make(Option.some(rpcSession)), + prepared: yield* SubscriptionRef.make>(Option.none()), + connect: Effect.void, + disconnect: Effect.void, + retryNow: Effect.void, + } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]); + const environmentRegistry = EnvironmentRegistry.EnvironmentRegistry.of({ + followStream: (_environmentId: EnvironmentId, stream: Stream.Stream) => + Stream.provideService( + stream, + EnvironmentSupervisor.EnvironmentSupervisor, + supervisor, + ), + } as unknown as EnvironmentRegistry.EnvironmentRegistry["Service"]); + // Atoms run their own fibers; share the test clock so a pending retry is driven by it. + const runtime = Atom.runtime( + Layer.mergeAll( + Layer.succeed(EnvironmentRegistry.EnvironmentRegistry, environmentRegistry), + Layer.succeed(Clock.Clock, yield* Clock.Clock), + ), + ); + // Same options as the progressive thread find in orchestration.ts. + const family = createEnvironmentRpcSubscriptionAtomFamily(runtime, { + label: "test.finite-rpc-subscription", + tag: ORCHESTRATION_V2_WS_METHODS.searchThreadStream, + completeWhen: (result) => result.complete !== false, + idleTtlMs: 0, + }); + const atom = family({ + environmentId: QUERY_ENVIRONMENT.environmentId, + input: {} as never, + }); + const registry = AtomRegistry.make(); + const unsubscribe = registry.subscribe( + atom, + (result) => { + if (AsyncResult.isFailure(result)) + Deferred.doneUnsafe(surfaced, Exit.succeed(result.cause)); + }, + { immediate: true }, + ); + yield* Effect.addFinalizer(() => + Effect.sync(() => { + unsubscribe(); + registry.dispose(); + }), + ); + yield* Deferred.await(firstSubscribed); + // Lets any same-session retry run, so its resubscription can win the race below. + yield* TestClock.adjust("1 minute"); + const outcome = yield* Effect.raceFirst( + Deferred.await(surfaced).pipe( + Effect.map((cause) => ({ _tag: "surfaced", cause }) as const), + ), + Deferred.await(resubscribed).pipe(Effect.as({ _tag: "resubscribed" } as const)), + ); + + expect( + outcome._tag, + "finite subscription resubscribed after a decode defect instead of failing its consumer", + ).toBe("surfaced"); + if (outcome._tag === "surfaced") { + expect(Cause.hasDies(outcome.cause)).toBe(true); + expect(Schema.isSchemaError(Cause.squash(outcome.cause))).toBe(true); + } + expect(subscriptions).toBe(1); + }), + ), + ); it.effect.each([ { completedTtlMs: undefined, subscriptions: 2 }, { completedTtlMs: 60_000, subscriptions: 1 }, From e41941779a6cebb6fc7048466607a394ce2871f5 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 13 Nov 2026 08:45:15 -0800 Subject: [PATCH 003/250] fix(server): stopping the service during the pre-update backup ends the copy (#34854) --- apps/server/src/serviceLauncher.test.ts | 50 +++++++++++++++++++++++++ apps/server/src/serviceLauncher.ts | 32 +++++++++++----- 2 files changed, 73 insertions(+), 9 deletions(-) diff --git a/apps/server/src/serviceLauncher.test.ts b/apps/server/src/serviceLauncher.test.ts index c7d6c5e5d8f3a..ac699b9c7d6cb 100644 --- a/apps/server/src/serviceLauncher.test.ts +++ b/apps/server/src/serviceLauncher.test.ts @@ -1586,6 +1586,56 @@ process.send({ type: "request-update", targetVersion: "1.1.0", dbPath: paths.dat }), ); + it.effect("starts no trial when stopped during the pre-update backup", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-backup-stop-" }); + const statePath = path.join(root, "runtime", "service-state.json"); + const childSource = "setInterval(() => {}, 1000);\n"; + for (const version of ["1.0.0", "1.1.0"]) { + yield* writeFakeRuntime( + fs, + path, + path.join(root, "runtime", "versions", version), + childSource, + ); + } + const dbPath = path.join(root, "userdata", "state.sqlite"); + yield* fs.makeDirectory(path.dirname(dbPath), { recursive: true }); + yield* fs.writeFileString(dbPath, "database"); + const pending = { + protocol: SERVICE_LAUNCHER_PROTOCOL, + activeVersion: "1.0.0", + update: { + id: "update-1", + fromVersion: "1.0.0", + targetVersion: "1.1.0", + dbPath, + status: "pending", + }, + } as const; + yield* Effect.promise(() => writeServiceState(statePath, pending)); + const childCount = spawnedChildren.length; + const launcher = new Launcher(root, pending); + + // Recovery is already queued, so this stop lands while the backup runs. + const running = launcher.run(); + yield* Effect.promise(() => launcher.stop("SIGTERM")); + yield* Effect.promise(() => running); + + // A trial started here arms its 30-minute prepared timer, which keeps the stopped launcher alive. + assert.deepEqual( + spawnedChildren.slice(childCount).map((child) => child.spawnargs.join(" ")), + [], + ); + assert.deepEqual(yield* Effect.promise(() => readServiceState(statePath)), pending); + const backupDir = path.join(root, "runtime", "db-backup", "update-1"); + assert.isFalse(yield* fs.exists(backupDir)); + assert.isFalse(yield* fs.exists(`${backupDir}.staging`)); + }), + ); + it.effect("records no update when the server exits during the backup check", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; diff --git a/apps/server/src/serviceLauncher.ts b/apps/server/src/serviceLauncher.ts index c07697007fe57..1a6c07cf43659 100644 --- a/apps/server/src/serviceLauncher.ts +++ b/apps/server/src/serviceLauncher.ts @@ -326,14 +326,18 @@ async function syncDirectory(directory: string): Promise { } } -function runCp(args: ReadonlyArray): Promise { +function runCp(args: ReadonlyArray, signal?: AbortSignal): Promise { return new Promise((resolve) => { - NodeChildProcess.execFile("cp", args, (error) => resolve(error === null)); + NodeChildProcess.execFile("cp", args, { signal }, (error) => resolve(error === null)); }); } // macOS fs.copyFile clone flags fall back to full copies. -async function copyDatabaseFile(source: string, destination: string): Promise { +async function copyDatabaseFile( + source: string, + destination: string, + signal?: AbortSignal, +): Promise { // oxlint-disable-next-line t3code/no-global-process-runtime -- Standalone launcher has no Effect runtime. const platform = process.platform; // cp -c replaces files and breaks hard links; copy into hard-linked destinations instead. @@ -346,7 +350,9 @@ async function copyDatabaseFile(source: string, destination: string): Promise { +async function backupDatabaseOnce( + baseDir: string, + pending: PendingServiceUpdate, + signal: AbortSignal, +): Promise { const backupDir = databaseBackupDir(baseDir, pending.id); if (await pathExists(backupDir)) return; @@ -534,10 +544,11 @@ async function backupDatabaseOnce(baseDir: string, pending: PendingServiceUpdate await NodeFSP.mkdir(stagingDir, { recursive: true, mode: 0o700 }); try { for (const suffix of DB_FILE_SUFFIXES) { + signal.throwIfAborted(); const source = `${pending.dbPath}${suffix}`; if (suffix !== "" && !(await pathExists(source))) continue; const destination = databaseBackupFile(stagingDir, suffix); - await copyDatabaseFile(source, destination); + await copyDatabaseFile(source, destination, signal); await syncFile(destination); } await NodeFSP.rename(stagingDir, backupDir); @@ -830,6 +841,7 @@ export class Launcher { #timer: NodeJS.Timeout | undefined; #transitions: Promise = Promise.resolve(); #stopRequested = false; + readonly #stopSignal = new AbortController(); #stopping = false; #done = false; readonly #completion = Promise.withResolvers(); @@ -948,6 +960,7 @@ export class Launcher { return; } this.#stopRequested = true; + this.#stopSignal.abort(); this.#clearTimer(); this.#enqueue(async () => { // Let in-progress updates start replacements first; replacements observe the marker and own pre-activation tunnel cleanup. @@ -1043,11 +1056,14 @@ export class Launcher { async #startTrial(pending: PendingServiceUpdate): Promise { try { - await backupDatabaseOnce(this.#baseDir, pending); + await backupDatabaseOnce(this.#baseDir, pending, this.#stopSignal.signal); } catch { + // A stop aborts the copy and leaves the update pending for the next start. + if (this.#stopRequested) return; await this.#returnToPrevious(pending, "failed", "db-backup-failed"); return; } + if (this.#stopRequested) return; try { await this.#startChild(pending.targetVersion, "trial", pending); } catch { @@ -1202,8 +1218,6 @@ export class Launcher { this.#child = null; await this.#terminate(child.process); await finishOnlineBackup?.(); - // A stop during a minutes-long copy should not start a trial; restart resumes the pending update. - if (this.#stopRequested) return; await this.#startTrial(pending); } From 928dd049e1af868bc93ce12498270bd9ec9292c2 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 13 Nov 2026 08:45:32 -0800 Subject: [PATCH 004/250] fix(server): preview browser downloads Chrome 157.0.8103.78 with its security fixes (#34962) --- apps/server/src/preview/PreviewBrowser.ts | 26 +++++++++++------------ 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/apps/server/src/preview/PreviewBrowser.ts b/apps/server/src/preview/PreviewBrowser.ts index 50ba7b91c56fa..203ec112cccb7 100644 --- a/apps/server/src/preview/PreviewBrowser.ts +++ b/apps/server/src/preview/PreviewBrowser.ts @@ -25,31 +25,31 @@ import { openZipArchive } from "../zipArchive.ts"; // https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json // Never pin below 156.0.8078.70: the shell has no CRLSet and older SPKI blocklists trust // compromised eMudhra intermediates (crrev.com/c/8627687). -const VERSION = "157.0.8103.38"; +const VERSION = "157.0.8103.78"; const ARCHIVES = { linux64: { - bytes: 128_168_542, - sha256: "deafffa74660b34853a4119c361fd427ac8c9fcb62db51ddb8a797cd83a4ba2d", + bytes: 127_965_683, + sha256: "f90329720fb61e5a76da9b6b509b6ad12a484c9c5df0359403e935177c6d153a", }, "linux-arm64": { - bytes: 128_171_139, - sha256: "b9b910a57b2a15eb3e55000a2339f93a973934a413ebfc6e585a50ec6c42fd6c", + bytes: 128_047_138, + sha256: "912fbf877e53de34d7b3cca5104f3b95607d41bd35412ccc6cd2a599ff0c0dbf", }, "mac-arm64": { - bytes: 104_773_711, - sha256: "e6a6b13551aa12f951d0891d79282606b13742837c8fa0505e1272bae116ce82", + bytes: 104_822_985, + sha256: "17bf66d53c1b1104dcb641c5b331cd4ac5d200c778f5feed0934a0b92deaf8d6", }, "mac-x64": { - bytes: 110_442_262, - sha256: "79821745d0a72d2ba752d981284ed0a9e6fa320dad133596941a17dba6186de3", + bytes: 110_132_547, + sha256: "f1a6a9561eaf1f409849538de663695d36d72fbf1ebca4d42e47c7b5f2a157f3", }, win32: { - bytes: 120_390_416, - sha256: "35e0d45c0136448f7b587f6fb7195a0686127a4dfdc23ef3b0e04b5e9ffe16cd", + bytes: 120_403_126, + sha256: "d9829c59dcc79d9e6e920f8ab841f2a9a055dcf54229d756ac0b42dfec303910", }, win64: { - bytes: 126_513_060, - sha256: "1ff4540855478ba0848e03f1ef8d455f77d058b38285df91bc34273788a73446", + bytes: 126_047_001, + sha256: "2138c1583d25d6f3abec5269943b2f430207d87a539895aa9e90cc4697ec0e1f", }, } as const; From d0957bc4b0495789e28769bad62834f5f912d94e Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 13 Nov 2026 08:47:03 -0800 Subject: [PATCH 005/250] fix(opencode): restarted server stops an orphaned OpenCode server started through a wrapper (#34959) --- .../src/server/OpenCodeServerLedger.test.ts | 51 +++++++++++++++---- .../src/server/OpenCodeServerLedger.ts | 17 +++++-- 2 files changed, 53 insertions(+), 15 deletions(-) diff --git a/packages/provider-opencode/src/server/OpenCodeServerLedger.test.ts b/packages/provider-opencode/src/server/OpenCodeServerLedger.test.ts index 1310cdd8b36e0..3fcb52c898d68 100644 --- a/packages/provider-opencode/src/server/OpenCodeServerLedger.test.ts +++ b/packages/provider-opencode/src/server/OpenCodeServerLedger.test.ts @@ -56,7 +56,6 @@ const spawnGroupLeader = (args: ReadonlyArray, script: string) => if (pid === undefined) return yield* Effect.die("spawn failed"); const grouped = yield* Deferred.make(); const exited = yield* Deferred.make(); - const printed = yield* Deferred.make(); child.once("exit", (_code, signal) => Deferred.doneUnsafe(exited, Effect.succeed(signal))); // "close" fires after stdout drains, so a guard line always lands first. It only fires before // "grouped" when the shell died without starting the script, such as from a signal. @@ -77,12 +76,9 @@ const spawnGroupLeader = (args: ReadonlyArray, script: string) => Effect.succeed(`\`ps -o pgid= -p ${pid}\` printed ${JSON.stringify(output)}`), ); } - if (line === "ready") Deferred.doneUnsafe(printed, Effect.void); }); const result = yield* Deferred.await(grouped); - return result === true - ? { pid, exited: Deferred.await(exited), printed: Deferred.await(printed) } - : result; + return result === true ? { pid, exited: Deferred.await(exited) } : result; }); // A pid colliding with a live group id is rare, so a few retries clear it. Repeated failures @@ -152,18 +148,21 @@ describe.each(observedPlatforms)("OpenCodeServerLedger observing as %s", (platfo const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; const stateDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-opencode-ledger-" }); - // The inner shell prints once it has exec'd. Until then it still carries the wrapper's - // command line, and recording that would make the reaper skip the group as recycled. + const gate = path.join(stateDir, "gate"); + const ready = path.join(stateDir, "ready"); + NodeChildProcess.execFileSync("mkfifo", [gate, ready]); + // The wrapper's child is recorded before it execs and stopped after. const wrapper = yield* spawnGroup( SERVE_ARGS, - '/bin/sh -c "echo ready; sleep 600 & wait" "$0" "$@" & exit 0', + `(read _ < '${gate}'; exec /bin/sh -c 'echo > "${ready}"; sleep 600 & wait' "$0" "$@") & exit 0`, ); yield* wrapper.exited; - yield* wrapper.printed; expect(groupExists(wrapper.pid)).toBe(true); yield* recordFromDeadServer(stateDir, wrapper); expect(yield* fs.readDirectory(path.join(stateDir, "opencode-servers"))).toHaveLength(1); + yield* fs.writeFileString(gate, "\n"); + yield* fs.readFileString(ready); const restarted = yield* OpenCodeServerLedger.make({ stateDir }); yield* restarted.reapOrphans; @@ -173,6 +172,38 @@ describe.each(observedPlatforms)("OpenCodeServerLedger observing as %s", (platfo }).pipe(provideHost), ); + it.live("stops a group recorded by an older server without serve arguments", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const stateDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-opencode-ledger-" }); + const orphan = yield* spawnGroup(SERVE_ARGS); + yield* recordFromDeadServer(stateDir, orphan); + const entryPath = path.join(stateDir, "opencode-servers", `${orphan.pid}.json`); + const { args: _args, ...legacy } = JSON.parse(yield* fs.readFileString(entryPath)); + yield* fs.writeFileString(entryPath, JSON.stringify(legacy)); + + const restarted = yield* OpenCodeServerLedger.make({ stateDir }); + yield* restarted.reapOrphans; + + expect(groupExists(orphan.pid)).toBe(false); + }).pipe(provideHost), + ); + + it.live("stops a group whose wrapper appends its own options", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const stateDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-opencode-ledger-" }); + const orphan = yield* spawnGroup([...SERVE_ARGS, "--log-level=ERROR"]); + yield* recordFromDeadServer(stateDir, orphan); + + const restarted = yield* OpenCodeServerLedger.make({ stateDir }); + yield* restarted.reapOrphans; + + expect(groupExists(orphan.pid)).toBe(false); + }).pipe(provideHost), + ); + it.live("leaves a recycled pid alone when its start time does not match", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; @@ -201,7 +232,7 @@ describe.each(observedPlatforms)("OpenCodeServerLedger observing as %s", (platfo yield* recordFromDeadServer(stateDir, unrelated); const entryPath = path.join(stateDir, "opencode-servers", `${unrelated.pid}.json`); const entry = yield* fs.readFileString(entryPath); - yield* fs.writeFileString(entryPath, entry.replace("--port=4096", "--port=4097")); + yield* fs.writeFileString(entryPath, entry.replaceAll("--port=4096", "--port=4097")); const restarted = yield* OpenCodeServerLedger.make({ stateDir }); yield* restarted.reapOrphans; diff --git a/packages/provider-opencode/src/server/OpenCodeServerLedger.ts b/packages/provider-opencode/src/server/OpenCodeServerLedger.ts index af90d19acd67d..fb3ea7645e9bf 100644 --- a/packages/provider-opencode/src/server/OpenCodeServerLedger.ts +++ b/packages/provider-opencode/src/server/OpenCodeServerLedger.ts @@ -21,6 +21,7 @@ const OpenCodeServerEntry = Schema.Struct({ pid: Schema.Int, startTime: Schema.String, command: Schema.String, + args: Schema.optionalKey(Schema.Array(Schema.String)), port: Schema.Int, stateDir: Schema.String, owner: ProcessIdentity, @@ -73,6 +74,11 @@ const parseDarwinPs = (output: string): ReadonlyArray => ]; }); +// A wrapper's forked child keeps the wrapper's command line until it execs; the +// serve arguments survive the exec, so they identify the server either way. +const runsServe = (observed: ObservedProcess, args: ReadonlyArray) => + observed.command.endsWith(` ${args.join(" ")}`); + const signalGroup = (pgid: number, signal: NodeJS.Signals) => { try { signalProcessGroup(pgid, signal); @@ -193,9 +199,7 @@ export const make = Effect.fn("OpenCodeServerLedger.make")(function* (input: { leader !== undefined && !leader.zombie && leader.pgid === pgid ? [leader] : (yield* observeGroup(pgid)).filter((candidate) => !candidate.zombie); - const member = - members.find((candidate) => candidate.command.endsWith(` ${server.args.join(" ")}`)) ?? - members[0]; + const member = members.find((candidate) => runsServe(candidate, server.args)) ?? members[0]; if (owner === undefined || member === undefined) { if (groupExists(pgid)) { yield* Effect.logWarning( @@ -212,6 +216,7 @@ export const make = Effect.fn("OpenCodeServerLedger.make")(function* (input: { pid: member.pid, startTime: member.startTime, command: member.command, + args: server.args, port: server.port, stateDir: input.stateDir, owner, @@ -239,7 +244,8 @@ export const make = Effect.fn("OpenCodeServerLedger.make")(function* (input: { ); // Signals only a group whose recorded member still has the same pid, start - // time, group, and full command line, so a recycled pid is never touched. + // time, and group and still has the same command line or serve arguments, so + // a recycled pid is never touched. const stopOrphan = Effect.fn("OpenCodeServerLedger.stopOrphan")(function* ( entry: OpenCodeServerEntry, ) { @@ -249,7 +255,8 @@ export const make = Effect.fn("OpenCodeServerLedger.make")(function* (input: { observed.zombie || observed.startTime !== entry.startTime || observed.pgid !== entry.pgid || - observed.command !== entry.command + (observed.command !== entry.command && + (entry.args === undefined || !runsServe(observed, entry.args))) ) { return; } From c398b46af0ec2aa866c5d92f48ed65c0c385cb78 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 13 Nov 2026 08:47:22 -0800 Subject: [PATCH 006/250] fix(server): Codex text generation runs when launch args define an MCP server (#36223) --- .../CodexTextGeneration.test.ts | 77 ++++++++++++++++++- .../src/textGeneration/CodexTextGeneration.ts | 21 ++++- 2 files changed, 93 insertions(+), 5 deletions(-) diff --git a/apps/server/src/textGeneration/CodexTextGeneration.test.ts b/apps/server/src/textGeneration/CodexTextGeneration.test.ts index ae5c32bf57432..e6e43d606e27f 100644 --- a/apps/server/src/textGeneration/CodexTextGeneration.test.ts +++ b/apps/server/src/textGeneration/CodexTextGeneration.test.ts @@ -58,6 +58,7 @@ interface FakeCodexInput { mcpServers?: ReadonlyArray<{ name: string; enabled: boolean }> | number; requiredCwd?: string; requireInstructions?: string; + requireMcpServersOff?: ReadonlyArray; } function makeFakeCodexBinary(dir: string, input: FakeCodexInput) { @@ -73,6 +74,7 @@ function makeFakeCodexBinary(dir: string, input: FakeCodexInput) { mcpServers: input.mcpServers ?? [], requiredCwd: input.requiredCwd ?? null, requireInstructions: input.requireInstructions ?? null, + requireMcpServersOff: input.requireMcpServersOff ?? [], stderr: input.stderr ?? null, output: input.output, exitCode: input.exitCode ?? 0, @@ -129,6 +131,33 @@ function makeFakeCodexBinary(dir: string, input: FakeCodexInput) { ' process.stderr.write(message + "\\n");', " process.exit(code);", "}", + "// Like Codex: a later mcp_servers={...} override replaces servers earlier -c flags defined.", + "const servers = new Map();", + "const offServers = new Set();", + "for (let index = 0; index < args.length; index += 1) {", + ' if (args[index] !== "-c" && args[index] !== "--config") continue;', + ' const value = args[index + 1] ?? "";', + ' if (value.startsWith("mcp_servers={")) {', + ' const named = [...value.matchAll(/"([^"]+)"=\\{enabled=false\\}/g)].map((match) => match[1]);', + " for (const name of named) offServers.add(name);", + " for (const name of [...servers.keys()]) {", + " if (named.includes(name)) servers.set(name, false);", + " else servers.delete(name);", + " }", + ' } else if (value.startsWith("mcp_servers.")) {', + ' const [name, key] = value.slice("mcp_servers.".length).split(/[.=]/);', + " if (value === `mcp_servers.${name}.enabled=false`) offServers.add(name);", + " // An enabled flag alone toggles a server another layer (config.toml) defines.", + ' if (key === "enabled" && !servers.has(name)) continue;', + ' servers.set(name, servers.get(name) === true || key === "url" || key === "command");', + " }", + "}", + "for (const [name, hasTransport] of servers) {", + ' if (!hasTransport) fail("Error loading config.toml: invalid transport in `mcp_servers." + name + "`", 12);', + "}", + "for (const name of check.requireMcpServersOff) {", + ' if (!offServers.has(name)) fail("MCP server left on: " + name, 13);', + "}", 'if (check.requiredCwd !== null && process.cwd() !== check.requiredCwd) fail("cannot inspect checkout", 10);', 'if (check.requireInstructions !== null && !instructions.includes(check.requireInstructions)) fail("missing writer task", 11);', "if (check.requireArg !== null && !originalArgs.includes(` ${check.requireArg} `)) {", @@ -365,7 +394,7 @@ it.layer(layerCodexTextGenerationTest)("CodexTextGeneration", (it) => { { name: "already-off", enabled: false }, ], requireArg: - '--config features.plugins=false --config features.apps=false --config mcp_servers={"augmenter"={enabled=false},"my.server"={enabled=false}}', + 'exec --config mcp_servers={"augmenter"={enabled=false},"my.server"={enabled=false}} --config developer_instructions="Use', }, (textGeneration) => Effect.gen(function* () { @@ -384,6 +413,52 @@ it.layer(layerCodexTextGenerationTest)("CodexTextGeneration", (it) => { ), ); + it.effect("turns off an MCP server that launch args define without dropping its transport", () => + withFakeCodexEnv( + { + output: JSON.stringify({ title: "Remove default shapes" }), + launchArgs: `-c 'mcp_servers.t3-code.url="http://127.0.0.1:1/mcp"' -c 'mcp_servers.t3-code.enabled=true'`, + mcpServers: [{ name: "t3-code", enabled: true }], + requireMcpServersOff: ["t3-code"], + requireArg: + "--config features.apps=false --config mcp_servers.t3-code.enabled=false --ephemeral", + }, + (textGeneration) => + Effect.gen(function* () { + const result = yield* textGeneration.generateThreadTitle({ + cwd: process.cwd(), + message: "Remove default shapes from characters", + modelSelection: DEFAULT_TEST_MODEL_SELECTION, + }); + expect(result.title).toBe("Remove default shapes"); + }), + ), + ); + + it.effect("keeps MCP servers off when launch args set their own mcp_servers table", () => + withFakeCodexEnv( + { + output: JSON.stringify({ title: "Remove default shapes" }), + launchArgs: `-c 'mcp_servers={"my.server"={command="echo",enabled=true}}'`, + mcpServers: [ + { name: "my.server", enabled: true }, + { name: "my server", enabled: true }, + ], + requireArg: + '--config features.apps=false --config mcp_servers={"my.server"={enabled=false},"my server"={enabled=false}} --config mcp_servers.my server.enabled=false --ephemeral', + }, + (textGeneration) => + Effect.gen(function* () { + const result = yield* textGeneration.generateThreadTitle({ + cwd: process.cwd(), + message: "Remove default shapes from characters", + modelSelection: DEFAULT_TEST_MODEL_SELECTION, + }); + expect(result.title).toBe("Remove default shapes"); + }), + ), + ); + it.effect("still generates when Codex cannot list its MCP servers", () => withFakeCodexEnv( { diff --git a/apps/server/src/textGeneration/CodexTextGeneration.ts b/apps/server/src/textGeneration/CodexTextGeneration.ts index 4de5392502c8c..49c60166111ba 100644 --- a/apps/server/src/textGeneration/CodexTextGeneration.ts +++ b/apps/server/src/textGeneration/CodexTextGeneration.ts @@ -55,6 +55,11 @@ const codexMcpServersOff = (names: ReadonlyArray): string | null => names.length === 0 ? null : `mcp_servers={${names.map((name) => `${JSON.stringify(name)}={enabled=false}`).join(",")}}`; +// Codex splits -c keys at the first "=" and on every dot, so only those names take a dotted flag. +const codexMcpServerOffFlags = (names: ReadonlyArray) => + names + .filter((name) => !name.includes(".") && !name.includes("=")) + .flatMap((name) => ["--config", `mcp_servers.${name}.enabled=false`]); const encodeJsonString = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown)); export const makeCodexTextGeneration = Effect.fn("makeCodexTextGeneration")(function* ( codexConfig: CodexSettings, @@ -291,20 +296,28 @@ export const makeCodexTextGeneration = Effect.fn("makeCodexTextGeneration")(func } : {}), }; - const mcpServersOff = yield* listEnabledMcpServers({ + const enabledMcpServers = yield* listEnabledMcpServers({ binary, cwd, env, launchArgs: codexMcpListLaunchArgs(launchArgs), - }).pipe(Effect.map(codexMcpServersOff)); + }); + const mcpServersOff = codexMcpServersOff(enabledMcpServers); + const execLaunchArgs = codexExecLaunchArgs(launchArgs); + // A launch-arg mcp_servers={...} table replaces ours and can re-enable dotted names. + const launchArgsSetMcpTable = execLaunchArgs.some((arg) => /\bmcp_servers\s*=/.test(arg)); const spawnCommand = yield* resolveSpawnCommand( binary, [ "exec", - ...codexExecLaunchArgs(launchArgs), + // Before launch args: a later table would replace servers their -c flags define. + ...(mcpServersOff ? ["--config", mcpServersOff] : []), + ...execLaunchArgs, // A user's MCP server could act on the text-generation prompt. ...CODEX_NO_PLUGIN_ARGS, - ...(mcpServersOff ? ["--config", mcpServersOff] : []), + ...(mcpServersOff && launchArgsSetMcpTable ? ["--config", mcpServersOff] : []), + // After launch args, so a launch arg's enabled=true cannot turn a server back on. + ...codexMcpServerOffFlags(enabledMcpServers), "--ephemeral", "--skip-git-repo-check", "-s", From 759e649ea8b306ae3ea8f97ed14e69158204ef8f Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 13 Nov 2026 08:49:14 -0800 Subject: [PATCH 007/250] fix(server): agent recordings keep up with the page instead of the encoder (#36215) --- apps/server/src/preview/ServerBrowser.test.ts | 38 +++++++++++++--- apps/server/src/preview/ServerBrowser.ts | 45 ++++++++++++------- apps/server/src/preview/ServerBrowserPage.ts | 3 +- 3 files changed, 63 insertions(+), 23 deletions(-) diff --git a/apps/server/src/preview/ServerBrowser.test.ts b/apps/server/src/preview/ServerBrowser.test.ts index d2897987dc1f6..83f6db232cca6 100644 --- a/apps/server/src/preview/ServerBrowser.test.ts +++ b/apps/server/src/preview/ServerBrowser.test.ts @@ -360,12 +360,10 @@ function makeEncoderPage() { document: { createElement: (tag: string) => (tag === "video" ? video : canvas) }, Path2D: function Path2D() {}, MediaRecorder: FakeMediaRecorder, - createImageBitmap: async (blob: Blob) => ({ - width: 1280, - height: 800, - pixels: await blob.text(), - close() {}, - }), + createImageBitmap: async (blob: Blob) => { + await decodeGate?.promise; + return { width: 1280, height: 800, pixels: await blob.text(), close() {} }; + }, atob, btoa, Blob, @@ -386,6 +384,7 @@ function makeEncoderPage() { const contexts: ReturnType[] = []; let encoderPageOverride: Page | undefined; const encoderStreams: Array<{ frameRate: number | undefined; bitsPerSecond: number }> = []; +let decodeGate: PromiseWithResolvers | null = null; const contextRequests: Array<{ profileId: string; isolated: boolean }> = []; let contextGate: PromiseWithResolvers | null = null; let heldStill: (() => Promise) | null = null; @@ -571,6 +570,7 @@ beforeEach(() => { contexts.length = 0; encoderPageOverride = undefined; encoderStreams.length = 0; + decodeGate = null; contextRequests.length = 0; contextGate = null; heldStill = null; @@ -1077,6 +1077,32 @@ it.effect.each([ ).pipe(Effect.provide(layer)), ); +// Chrome stops sending screencast frames while too many are unacked. +it.effect("recordings ack screencast frames without waiting for the encoder to decode them", () => + Effect.scoped( + Effect.gen(function* () { + const { broker, tabId } = yield* ready; + yield* broker.invoke({ scope, tabId, operation: "recordingStart", input: {} }); + const session = contexts[0]!.sessions.find((candidate) => + candidate.send.mock.calls.some(([method]) => method === "Page.startScreencast"), + )!; + const gate = Promise.withResolvers(); + decodeGate = gate; + yield* Effect.addFinalizer(() => Effect.sync(() => gate.resolve())); + for (const sessionId of [1, 2, 3]) { + session.emit("Page.screencastFrame", { + data: Buffer.from(`frame ${sessionId}`).toString("base64"), + sessionId, + metadata: { deviceWidth: 1280 }, + }); + } + expect( + session.send.mock.calls.filter(([method]) => method === "Page.screencastFrameAck"), + ).toHaveLength(3); + }), + ).pipe(Effect.provide(layer)), +); + it.effect.each([{ truncated: false }, { truncated: true }])( "saves a large recording as a binary download and removes its staged file (truncated: $truncated)", ({ truncated }) => diff --git a/apps/server/src/preview/ServerBrowser.ts b/apps/server/src/preview/ServerBrowser.ts index fdffcc49414ed..ee9339c0a9ce3 100644 --- a/apps/server/src/preview/ServerBrowser.ts +++ b/apps/server/src/preview/ServerBrowser.ts @@ -1986,24 +1986,37 @@ const make = Effect.gen(function* () { const opened = await tab.page.context().newCDPSession(tab.page); session = opened; const framesInFlight = new Set>(); + // Chrome withholds frames while acks are outstanding, so ack on arrival and let a + // slow encoder skip to the newest frame instead of throttling the capture. + let pendingFrame: readonly [string, number] | null = null; + const deliverPending = async () => { + while (pendingFrame) { + const next = pendingFrame; + pendingFrame = null; + const accepted = await encoder + .evaluate( + ([data, width]) => + (globalThis as unknown as EncoderWindow).__t3Recorder.frame(data, width), + next, + ) + .catch(() => true); + if (!accepted) { + pendingFrame = null; + await opened.send("Page.stopScreencast").catch(constVoid); + } + } + }; opened.on("Page.screencastFrame", (frame) => { + void opened + .send("Page.screencastFrameAck", { sessionId: frame.sessionId }) + .catch(constVoid); const cssWidth = tab.page.viewportSize()?.width ?? frame.metadata.deviceWidth; - const delivered: Promise = encoder - .evaluate( - ([data, width]) => - (globalThis as unknown as EncoderWindow).__t3Recorder.frame(data, width), - [frame.data, cssWidth] as const, - ) - .then(async (accepted) => { - if (!accepted) await opened.send("Page.stopScreencast"); - }) - .catch(constVoid) - .finally(() => { - framesInFlight.delete(delivered); - void opened - .send("Page.screencastFrameAck", { sessionId: frame.sessionId }) - .catch(constVoid); - }); + const delivering = framesInFlight.size > 0; + pendingFrame = [frame.data, cssWidth]; + if (delivering) return; + const delivered: Promise = deliverPending().finally(() => + framesInFlight.delete(delivered), + ); framesInFlight.add(delivered); }); await opened.send("Page.startScreencast", RECORDING_SCREENCAST); diff --git a/apps/server/src/preview/ServerBrowserPage.ts b/apps/server/src/preview/ServerBrowserPage.ts index 65a283dc3cca4..418b644f52fb7 100644 --- a/apps/server/src/preview/ServerBrowserPage.ts +++ b/apps/server/src/preview/ServerBrowserPage.ts @@ -1103,7 +1103,8 @@ export const recordingEncoderScript = (frameRate: BrowserRecordingFrameRate) => window.__t3Recorder = { async frame(base64, cssWidth) { if (tooLarge) return false; - const bytes = Uint8Array.from(atob(base64), (char) => char.charCodeAt(0)); + // A per-character atob copy costs ~30 ms on a 3200x1800 frame and throttles recordings. + const bytes = Uint8Array.fromBase64(base64); const bitmap = await createImageBitmap(new Blob([bytes], { type: "image/jpeg" })); if (tooLarge) { bitmap.close(); return false; } if (!recorder) { From 9b6dde9a2384b883dda71fd31cab21616caf436c Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 13 Nov 2026 08:49:28 -0800 Subject: [PATCH 008/250] fix(server): session import finds compressed Codex rollouts (#34947) --- .../src/project/AgentSessionScanner.test.ts | 102 ++++++++++++++++++ .../server/src/project/AgentSessionScanner.ts | 73 +++++++++++-- 2 files changed, 166 insertions(+), 9 deletions(-) diff --git a/apps/server/src/project/AgentSessionScanner.test.ts b/apps/server/src/project/AgentSessionScanner.test.ts index 30db62847e8e1..48b52d97ad132 100644 --- a/apps/server/src/project/AgentSessionScanner.test.ts +++ b/apps/server/src/project/AgentSessionScanner.test.ts @@ -5,6 +5,7 @@ import * as NodeChildProcess from "node:child_process"; import * as NodeFS from "node:fs"; import * as NodeOS from "node:os"; import * as NodeSqlite from "node:sqlite"; +import * as NodeZlib from "node:zlib"; import { describe, expect, it } from "@effect/vitest"; import { AgentSessionImportSource, @@ -174,6 +175,21 @@ const writeTranscript = Effect.fn("AgentSessionScanner.test.writeTranscript")(fu yield* fileSystem.utimes(input.filePath, seconds, seconds); }); +const writeCompressedTranscript = Effect.fn("AgentSessionScanner.test.writeCompressedTranscript")( + function* (input: { + readonly filePath: string; + readonly contents: string; + readonly mtimeMs: number; + }) { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + yield* fileSystem.makeDirectory(path.dirname(input.filePath), { recursive: true }); + yield* fileSystem.writeFile(input.filePath, NodeZlib.zstdCompressSync(input.contents)); + const seconds = input.mtimeMs / 1000; + yield* fileSystem.utimes(input.filePath, seconds, seconds); + }, +); + const claudeSessionLine = (cwd: string) => `${JSON.stringify({ type: "user", cwd, sessionId: "s1" })}\n${JSON.stringify({ type: "assistant" })}\n`; @@ -1244,6 +1260,32 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { const result = yield* runScan({ claudeHomePath, codexHomePath }); + expect(result.candidates.map(({ path, threadCount }) => ({ path, threadCount }))).toEqual([ + { path: workspace, threadCount: 1 }, + ]); + }), + ); + it.effect("discovers projects whose Codex rollouts are zstd-compressed", () => + Effect.gen(function* () { + const path = yield* Path.Path; + const claudeHomePath = yield* makeTempDir("t3code-claude-home-"); + const codexHomePath = yield* makeTempDir("t3code-codex-home-"); + const workspace = yield* makeTempDir("t3code-workspace-"); + yield* writeCompressedTranscript({ + filePath: path.join( + codexHomePath, + "sessions", + "2026", + "05", + "03", + "rollout-2026-05-03T00-00-00-019a5f00-0000-7000-8000-00000000000b.jsonl.zst", + ), + contents: codexRolloutLine(workspace), + mtimeMs: Date.parse("2026-05-03T00:00:00.000Z"), + }); + + const result = yield* runScan({ claudeHomePath, codexHomePath }); + expect(result.candidates.map(({ path, threadCount }) => ({ path, threadCount }))).toEqual([ { path: workspace, threadCount: 1 }, ]); @@ -1252,6 +1294,66 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { }); describe("recentThreads", () => { + it.effect("imports Codex conversations stored only as zstd-compressed rollouts", () => + Effect.gen(function* () { + const path = yield* Path.Path; + const nowMs = Date.parse("2026-08-24T12:00:00.000Z"); + yield* TestClock.setTime(nowMs); + const claudeHomePath = yield* makeTempDir("t3code-claude-home-"); + const codexHomePath = yield* makeTempDir("t3code-codex-home-"); + const workspace = yield* makeTempDir("t3code-workspace-"); + yield* writeCompressedTranscript({ + filePath: path.join( + codexHomePath, + "sessions", + "2026", + "08", + "20", + "rollout-2026-08-20T10-00-00-019a5f00-0000-7000-8000-00000000000c.jsonl.zst", + ), + contents: [ + encodeTranscriptRecord({ + type: "session_meta", + payload: { id: "019a5f00-0000-7000-8000-00000000000c", cwd: workspace }, + }), + encodeTranscriptRecord({ + type: "event_msg", + timestamp: "2026-08-20T10:00:00.000Z", + payload: { type: "user_message", message: "Compressed prompt" }, + }), + encodeTranscriptRecord({ + type: "response_item", + timestamp: "2026-08-20T10:01:00.000Z", + payload: { + type: "message", + role: "assistant", + content: [{ type: "output_text", text: "Compressed answer" }], + }, + }), + ].join("\n"), + mtimeMs: nowMs - 4 * 24 * 60 * 60 * 1000, + }); + + const threads = yield* runRecentThreads({ + claudeHomePath, + codexHomePath, + workspaceRoot: workspace, + }); + + expect( + threads.map((thread) => ({ + id: thread.providerSessionId, + messages: thread.messages.map((message) => message.text), + })), + ).toEqual([ + { + id: "019a5f00-0000-7000-8000-00000000000c", + messages: ["Compressed prompt", "Compressed answer"], + }, + ]); + }), + ); + it.effect("imports recent Claude, Codex, and Pi sessions for the selected project only", () => Effect.gen(function* () { const path = yield* Path.Path; diff --git a/apps/server/src/project/AgentSessionScanner.ts b/apps/server/src/project/AgentSessionScanner.ts index d9ca41b8f26b1..b47cef441c925 100644 --- a/apps/server/src/project/AgentSessionScanner.ts +++ b/apps/server/src/project/AgentSessionScanner.ts @@ -1,5 +1,6 @@ import * as NodeOS from "node:os"; import * as NodeSqlite from "node:sqlite"; +import * as NodeZlib from "node:zlib"; import { AgentSessionImportSource, @@ -17,11 +18,13 @@ import { import * as Clock from "effect/Clock"; import * as Context from "effect/Context"; import * as DateTime from "effect/DateTime"; +import * as NodeStream from "@effect/platform-node/NodeStream"; import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as Path from "effect/Path"; +import * as Pull from "effect/Pull"; import * as Schema from "effect/Schema"; import * as Semaphore from "effect/Semaphore"; import * as Stream from "effect/Stream"; @@ -177,7 +180,11 @@ const decodeCodexHistoryBase = Schema.decodeUnknownOption( ); // Original rollouts end in their thread ID, rewound ones in `_`. const CODEX_ROLLOUT_ID_PATTERN = - /-([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})(?:_([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}))?\.jsonl$/i; + /-([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})(?:_([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}))?\.jsonl(?:\.zst)?$/i; +// Codex zstd-compresses older rollouts; their byte limits and offsets count decompressed bytes. +const COMPRESSED_TRANSCRIPT_SUFFIX = ".jsonl.zst"; +const isCompressedTranscript = (filePath: string) => + filePath.endsWith(COMPRESSED_TRANSCRIPT_SUFFIX); // Codex records a chat's project only here; moving a chat leaves its rollout cwd unchanged. const CODEX_STATE_DATABASE = "state_5.sqlite"; const decodeCodexSessionIndexEntry = Schema.decodeUnknownOption( @@ -861,6 +868,33 @@ export const make = Effect.gen(function* () { const statOption = (target: string) => fileSystem.stat(target).pipe(Effect.asSome, Effect.orElseSucceed(Option.none)); + const transcriptChunkReader = Effect.fn("AgentSessionScanner.transcriptChunkReader")(function* ( + file: FileSystem.File, + filePath: string, + ) { + if (!isCompressedTranscript(filePath)) return (size: number) => file.readAlloc(size); + const pull = yield* Stream.toPull( + fileSystem + .stream(filePath) + .pipe(NodeStream.pipeThroughSimple(() => NodeZlib.createZstdDecompress())), + ); + let pending: Array = []; + let done = false; + // Untraced: called once per chunk inside the caller's traced read. + return Effect.fnUntraced(function* (size: number) { + while (pending.length === 0) { + if (done) return Option.none(); + const next = yield* pull.pipe(Pull.catchDone(() => Effect.undefined)); + if (next === undefined) done = true; + else pending = [...next]; + } + const chunk = pending.shift(); + if (chunk === undefined) return Option.none(); + if (chunk.byteLength > size) pending.unshift(chunk.subarray(size)); + return Option.some(chunk.subarray(0, size)); + }); + }); + const directoryIdentity = Effect.fn("AgentSessionScanner.directoryIdentity")(function* ( target: string, knownStats?: FileSystem.File.Info, @@ -1037,11 +1071,15 @@ export const make = Effect.gen(function* () { fileSystem.open(transcript.filePath, { flag: "r" }).pipe( Effect.flatMap((file) => Effect.gen(function* () { + const readChunk = yield* transcriptChunkReader(file, transcript.filePath); + const size = isCompressedTranscript(transcript.filePath) + ? Number.POSITIVE_INFINITY + : transcript.identity.size; const decoder = new TextDecoder(); let remaining = ""; let bytesRead = 0; let recordsRead = 0; - const maxBytes = Math.min(MAX_TRANSCRIPT_SCAN_BYTES, transcript.identity.size); + const maxBytes = Math.min(MAX_TRANSCRIPT_SCAN_BYTES, size); const reserveRecord = () => { if ( recordsRead === MAX_METADATA_RECORDS_PER_TRANSCRIPT || @@ -1073,7 +1111,7 @@ export const make = Effect.gen(function* () { ); budget.operationsRemaining -= 1; budget.bytesRemaining -= readSize; - const next = yield* file.readAlloc(readSize); + const next = yield* readChunk(readSize); if (Option.isNone(next)) { return readLastRecord(); } @@ -1090,7 +1128,7 @@ export const make = Effect.gen(function* () { } } - if (bytesRead < transcript.identity.size) { + if (bytesRead < size) { budget.truncated = true; return null; } @@ -1107,8 +1145,12 @@ export const make = Effect.gen(function* () { source: AgentSessionSource, prefix?: { readonly endByteOffset: number; readonly historyBytes: number }, ) { - const readBytes = prefix?.endByteOffset ?? expected.size; - if (readBytes > expected.size || readBytes > MAX_IMPORTED_TRANSCRIPT_BYTES) return null; + const compressed = isCompressedTranscript(filePath); + const readBytes = + prefix?.endByteOffset ?? (compressed ? MAX_IMPORTED_TRANSCRIPT_BYTES : expected.size); + if ((!compressed && readBytes > expected.size) || readBytes > MAX_IMPORTED_TRANSCRIPT_BYTES) { + return null; + } return yield* Effect.scoped( fileSystem.open(filePath, { flag: "r" }).pipe( @@ -1117,6 +1159,7 @@ export const make = Effect.gen(function* () { if (!sameTranscriptIdentity(expected, transcriptIdentity(filePath, yield* file.stat))) { return null; } + const readChunk = yield* transcriptChunkReader(file, filePath); const records: Array = []; const claudeHistory = createClaudeHistoryWindow(); let cwd = Option.none(); @@ -1177,10 +1220,12 @@ export const make = Effect.gen(function* () { }; while (bytesRead < readBytes) { - const next = yield* file.readAlloc( + const next = yield* readChunk( Math.min(TRANSCRIPT_PREFIX_BYTES, readBytes - bytesRead), ); if (Option.isNone(next)) { + // A compressed rollout's decompressed length is only known at its end. + if (compressed && prefix === undefined) break; return null; } @@ -1398,7 +1443,12 @@ export const make = Effect.gen(function* () { } const directory = path.join(sessionsDir, year, month, day); for (const entry of (yield* readDirectory(directory)).toSorted().toReversed()) { - if (!entry.startsWith("rollout-") || !entry.endsWith(".jsonl")) continue; + if ( + !entry.startsWith("rollout-") || + !(entry.endsWith(".jsonl") || isCompressedTranscript(entry)) + ) { + continue; + } if (operationsRemaining <= 0) { truncated = true; break; @@ -2024,7 +2074,12 @@ export const make = Effect.gen(function* () { { source: candidate.source, providerInstanceId: candidate.providerInstanceId, - fallbackSessionId: path.basename(transcript.filePath, ".jsonl"), + fallbackSessionId: path.basename( + transcript.filePath, + isCompressedTranscript(transcript.filePath) + ? COMPRESSED_TRANSCRIPT_SUFFIX + : ".jsonl", + ), lastActiveAtMs: transcript.mtimeMs, }, inheritedRecords === null From 148612e181446ed6b1e87dd013f5ad7f488ba01d Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 13 Nov 2026 08:50:54 -0800 Subject: [PATCH 009/250] fix(opencode): usage scans reuse unchanged rows and run off the event loop (#34897) --- .../src/server/usage.test.ts | 93 ++++++++++++++++++ .../provider-opencode/src/server/usage.ts | 98 ++++++++++++++++--- 2 files changed, 179 insertions(+), 12 deletions(-) diff --git a/packages/provider-opencode/src/server/usage.test.ts b/packages/provider-opencode/src/server/usage.test.ts index d211563738ad5..02d1e5dc8cc28 100644 --- a/packages/provider-opencode/src/server/usage.test.ts +++ b/packages/provider-opencode/src/server/usage.test.ts @@ -1,3 +1,4 @@ +import type * as NodeWorkerThreads from "node:worker_threads"; import * as NodeServices from "@effect/platform-node/NodeServices"; import { assert, describe, it } from "@effect/vitest"; import { DEFAULT_SERVER_SETTINGS, ProviderInstanceId } from "@t3tools/contracts"; @@ -13,6 +14,27 @@ import { vi } from "vite-plus/test"; import { openCodeUsageReader, readOpenCodeUsage } from "./usage.ts"; +const readerWorkers = vi.hoisted(() => ({ started: 0, enabled: false })); + +// Statement spies only see the main thread, so reader workers fail to start unless a test enables them. +vi.mock("node:worker_threads", async (importOriginal) => { + const actual = await importOriginal(); + class ReaderWorker extends actual.Worker { + constructor(...args: ConstructorParameters) { + super( + ...((readerWorkers.enabled + ? args + : [ + "throw new Error('reader workers disabled');", + { eval: true }, + ]) as ConstructorParameters), + ); + readerWorkers.started++; + } + } + return { ...actual, Worker: ReaderWorker }; +}); + const openDatabase = (filename: string) => Layer.build(NodeSqliteClient.layer({ filename })).pipe( Effect.map(Context.get(SqlClient.SqlClient)), @@ -141,6 +163,7 @@ describe("readOpenCodeUsage", () => { ); } let statements = 0; + let bodyReads = 0; const originalAll = NodeSqlite.StatementSync.prototype.all; yield* Effect.acquireRelease( Effect.sync(() => @@ -149,6 +172,7 @@ describe("readOpenCodeUsage", () => { ...params ) { statements++; + if (this.sourceSQL.includes("data ->")) bodyReads++; return originalAll.apply(this, params); }), ), @@ -161,6 +185,51 @@ describe("readOpenCodeUsage", () => { ["opencode:a", "opencode:b", "opencode:c"], ); assert.isBelow(statements, 10); + + bodyReads = 0; + const repeated = yield* readOpenCodeUsage(dir, sinceMs); + assert.deepStrictEqual( + repeated.files.flatMap((file) => file.records).map((record) => record.dedupeKey), + ["opencode:a", "opencode:b", "opencode:c"], + ); + // An unchanged database must not have its message bodies read and parsed again. + assert.strictEqual(bodyReads, 0); + }).pipe(Effect.provide(NodeServices.layer)), + ); + + it.effect("does not reuse rows from a replaced or restored database file", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-replaced-" }); + const write = (name: string, input: number) => + Effect.scoped( + Effect.gen(function* () { + const db = yield* openDatabase(path.join(dir, name)); + yield* db.unsafe("PRAGMA journal_mode = WAL"); + yield* db.unsafe( + "CREATE TABLE message (id TEXT, session_id TEXT, time_created INTEGER, time_updated INTEGER, data TEXT)", + ); + yield* db.unsafe( + "INSERT INTO message(rowid, id, session_id, time_created, time_updated, data) VALUES (1, 'a', 's', 1, 1, ?)", + [JSON.stringify({ modelID: "m", tokens: { input } })], + ); + }), + ); + const input = Effect.map( + readOpenCodeUsage(dir, 0), + (result) => result.files[0]?.records[0]?.totals.uncachedInputTokens, + ); + yield* write("opencode.db", 100); + assert.strictEqual(yield* input, 100); + yield* write("replacement.db", 900); + yield* fileSystem.rename(path.join(dir, "replacement.db"), path.join(dir, "opencode.db")); + assert.strictEqual(yield* input, 900); + // A backup restored in place keeps the inode and birth time, like `cp -p`. + yield* write("backup.db", 500); + yield* fileSystem.copyFile(path.join(dir, "backup.db"), path.join(dir, "opencode.db")); + yield* fileSystem.utimes(path.join(dir, "opencode.db"), 0, 0); + assert.strictEqual(yield* input, 500); }).pipe(Effect.provide(NodeServices.layer)), ); @@ -188,6 +257,27 @@ describe("readOpenCodeUsage", () => { const legacy = path.join(dir, "storage", "message", message.sessionID); yield* fileSystem.makeDirectory(legacy, { recursive: true }); yield* fileSystem.writeFileString(path.join(legacy, "msg-1.json"), JSON.stringify(message)); + yield* Effect.acquireRelease( + Effect.sync(() => { + readerWorkers.enabled = true; + readerWorkers.started = 0; + }), + () => Effect.sync(() => (readerWorkers.enabled = false)), + ); + let mainThreadBodyReads = 0; + const originalAll = NodeSqlite.StatementSync.prototype.all; + yield* Effect.acquireRelease( + Effect.sync(() => + vi.spyOn(NodeSqlite.StatementSync.prototype, "all").mockImplementation(function ( + this: NodeSqlite.StatementSync, + ...params + ) { + if (this.sourceSQL.includes("data ->")) mainThreadBodyReads++; + return originalAll.apply(this, params); + }), + ), + (spy) => Effect.sync(() => spy.mockRestore()), + ); const first = yield* readOpenCodeUsage(dir, 0); assert.isFalse(first.error); const records = first.files.flatMap((file) => file.records); @@ -212,6 +302,9 @@ describe("readOpenCodeUsage", () => { ["opencode:msg-2"], ); assert.isAbove(Number((yield* fileSystem.stat(path.join(dir, "opencode.db-wal"))).size), 0); + // A large database must not be read on the server's event loop. + assert.isAbove(readerWorkers.started, 0); + assert.strictEqual(mainThreadBodyReads, 0); }).pipe(Effect.provide(NodeServices.layer)), ); diff --git a/packages/provider-opencode/src/server/usage.ts b/packages/provider-opencode/src/server/usage.ts index 4ae7b6df358e9..8d9bb28c70b34 100644 --- a/packages/provider-opencode/src/server/usage.ts +++ b/packages/provider-opencode/src/server/usage.ts @@ -11,6 +11,7 @@ import * as FileSystem from "effect/FileSystem"; import * as Option from "effect/Option"; import * as Path from "effect/Path"; import type * as PlatformError from "effect/PlatformError"; +import * as Semaphore from "effect/Semaphore"; import * as SqlClient from "effect/sql/SqlClient"; import type { OpenCodeSettings } from "../settings.ts"; @@ -93,6 +94,18 @@ export interface OpenCodeUsageReadResult { readonly error: boolean; } +interface CachedRow { + readonly updated: unknown; + readonly size: unknown; + readonly created: unknown; + readonly record: UsageRecord | null; +} + +// Parsed rows by `${databasePath}\0${table}`, then rowid, so unchanged rows are not read again. +const rowCache = new Map }>(); +// Concurrent scans of the same database would each read every uncached row. +const scanLock = Semaphore.makeUnsafe(1); + const isNotFound = (cause: PlatformError.PlatformError) => cause.reason._tag === "NotFound"; const MESSAGE_PAGE_SIZE = 64; @@ -116,6 +129,7 @@ const LEGACY_STAT_BATCH = 256; export const readOpenCodeUsage = Effect.fn("readOpenCodeUsage")(function* ( root: string, sinceMs: number, + retentionCutoffMs = sinceMs, ): Effect.fn.Return { const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; @@ -164,6 +178,22 @@ export const readOpenCodeUsage = Effect.fn("readOpenCodeUsage")(function* ( found = true; const file = { path: path.join(root, name), records: [] as UsageRecord[] }; files.push(file); + // Taken before the snapshot opens, so a write that races the scan invalidates its rows. + const [main, wal] = yield* Effect.forEach([file.path, `${file.path}-wal`], (target) => + Effect.option(fileSystem.stat(target)), + ); + const stamp = (info: Option.Option) => + Option.match(info, { + onNone: () => "none", + onSome: (stat) => `${stat.size}:${Option.getOrNull(stat.mtime)?.getTime()}`, + }); + // A replaced or restored database can repeat rowids and change markers, so every key includes + // the main file; WAL commits leave it alone until a checkpoint. + const identity = Option.match(main!, { + onNone: () => "none", + onSome: (stat) => + `${stat.dev}:${Option.getOrNull(stat.ino)}:${Option.getOrNull(stat.birthtime)?.getTime()}/${stamp(main!)}`, + }); yield* Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; // Fail promptly on live-writer locks instead of stalling the server. @@ -182,30 +212,53 @@ export const readOpenCodeUsage = Effect.fn("readOpenCodeUsage")(function* ( ), ); const timestamp = columns.has("time_created") ? "time_created" : "NULL"; + const updated = columns.has("time_updated") ? "time_updated" : "NULL"; + // Without time_updated an in-place edit can keep the length, so any database write counts. + const version = updated === "NULL" ? `${identity}/${stamp(wal!)}` : identity; const predicates = table === "session_message" ? ["type = 'assistant'"] : []; if (timestamp !== "NULL") predicates.push("time_created >= ?"); - // List only rowids so SQLite can use time indexes without reading message bodies. - const listed = (yield* sql.unsafe<{ readonly row_id: number }>( - `SELECT rowid AS row_id FROM ${table} + // List rowids and change markers so SQLite can use time indexes without reading message + // bodies; octet_length comes from the record header. + const listed = (yield* sql.unsafe<{ + readonly row_id: number; + readonly updated: unknown; + readonly size: unknown; + readonly created: unknown; + }>( + `SELECT rowid AS row_id, ${updated} AS updated, octet_length(data) AS size, + ${timestamp} AS created + FROM ${table} ${predicates.length ? `WHERE ${predicates.join(" AND ")}` : ""}`, timestamp === "NULL" ? [] : [sinceMs], )).toSorted((a, b) => a.row_id - b.row_id); databaseRows += listed.length; + const key = `${file.path}\0${table}`; + const previous = rowCache.get(key); + const reusable = previous?.version === version ? previous.rows : undefined; + const cached = new Map(); + const misses: typeof listed = []; + for (const row of listed) { + const hit = reusable?.get(row.row_id); + if (hit && hit.updated === row.updated && hit.size === row.size) + cached.set(row.row_id, hit); + else misses.push(row); + } // Filter and project in SQLite: user diffs and assistant bodies can be hundreds of MB. const validAssistant = `CASE WHEN typeof(data) = 'text' AND json_valid(data) THEN json_type(data, '$.role') IS NULL OR data ->> '$.role' = 'assistant' END`; // Omit per-page spans; the enclosing scan records row and page counts. - for (let start = 0; start < listed.length; start += MESSAGE_PAGE_SIZE) { - const page = listed.slice(start, start + MESSAGE_PAGE_SIZE); + for (let start = 0; start < misses.length; start += MESSAGE_PAGE_SIZE) { + const page = misses.slice(start, start + MESSAGE_PAGE_SIZE); databasePages++; const rows = yield* sql .unsafe<{ + readonly row_id: number; readonly id: unknown; readonly session_id: unknown; readonly data: unknown; readonly created: unknown; }>( - `SELECT id, session_id, ${timestamp} AS created, json_object( + `SELECT rowid AS row_id, id, session_id, ${timestamp} AS created, json_object( 'tokens', data -> '$.tokens', 'cost', data -> '$.cost', 'model', data -> '$.model', 'modelID', data -> '$.modelID', 'providerID', data -> '$.providerID', @@ -217,9 +270,10 @@ export const readOpenCodeUsage = Effect.fn("readOpenCodeUsage")(function* ( page.map((row) => row.row_id), ) .pipe(Effect.withTracerEnabled(false)); + const parsed = new Map(); for (const row of rows) { - append( - file.records, + parsed.set( + row.row_id, parseOpenCodeMessage(text(row.data), { id: text(row.id), sessionId: text(row.session_id), @@ -227,12 +281,28 @@ export const readOpenCodeUsage = Effect.fn("readOpenCodeUsage")(function* ( }), ); } + // Rows the SQL filter dropped are cached as empty so they are not read again either. + for (const row of page) { + cached.set(row.row_id, { ...row, record: parsed.get(row.row_id) ?? null }); + } yield* Effect.yieldNow; } + for (const row of listed) append(file.records, cached.get(row.row_id)!.record); + // Rows before this window were not listed; keep them for a wider window until retention. + for (const [rowId, entry] of reusable ?? []) { + const created = entry.created; + if (typeof created === "number" && created < sinceMs && created >= retentionCutoffMs) + cached.set(rowId, entry); + } + rowCache.set(key, { version, rows: cached }); } }).pipe( (read) => Effect.flatMap(SqlClient.SqlClient, (sql) => sql.withTransaction(read)), - Effect.provide(NodeSqliteClient.layer({ filename: file.path, readonly: true })), + // Run the scan on a reader worker so large databases do not block the server's event loop. + NodeSqliteClient.readOnly, + Effect.provide( + NodeSqliteClient.layer({ filename: file.path, readonly: true, readerWorker: true }), + ), Effect.catchTags({ SqlError: () => { error = true; @@ -312,7 +382,7 @@ export const readOpenCodeUsage = Effect.fn("readOpenCodeUsage")(function* ( "usage.database_pages": databasePages, }); return { files, missing: !found && !error, error }; -}); +}, scanLock.withPermit); const resolveOpenCodeDataDirs = Effect.fn("resolveOpenCodeDataDirs")(function* ( environment: NodeJS.ProcessEnv, @@ -346,7 +416,11 @@ export type OpenCodeUsageReaderEnv = FileSystem.FileSystem | Path.Path; export const openCodeUsageReader: ProviderUsageReader = { kind: "scan", provider: "opencode", - scan: Effect.fn("openCodeUsageReader.scan")(function* ({ instances, windowStartMs }) { + scan: Effect.fn("openCodeUsageReader.scan")(function* ({ + instances, + windowStartMs, + retentionCutoffMs, + }) { const roots = new Set(); for (const instance of instances) { for (const root of yield* resolveOpenCodeDataDirs(instance.environment)) roots.add(root); @@ -354,7 +428,7 @@ export const openCodeUsageReader: ProviderUsageReader - readOpenCodeUsage(dir, windowStartMs).pipe( + readOpenCodeUsage(dir, windowStartMs, retentionCutoffMs).pipe( Effect.map((result) => ({ dir, files: result.missing && !result.error ? null : result.files, From d3ef7e8e527ce2076ee858fc4c5360955da54b8a Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 13 Nov 2026 08:51:25 -0800 Subject: [PATCH 010/250] fix(cursor): send the catalog's default model parameters on every Cursor run (#28722) --- .../provider-cursor/src/server/adapter.ts | 9 ++- .../provider-cursor/src/server/driver.test.ts | 2 +- packages/provider-cursor/src/server/driver.ts | 1 + .../src/server/sdkModel.test.ts | 73 +++++++++++++++++++ .../provider-cursor/src/server/sdkModel.ts | 20 ++++- .../src/server/textGeneration.ts | 9 ++- 6 files changed, 106 insertions(+), 8 deletions(-) create mode 100644 packages/provider-cursor/src/server/sdkModel.test.ts diff --git a/packages/provider-cursor/src/server/adapter.ts b/packages/provider-cursor/src/server/adapter.ts index 1e160be188f08..6b3be72833b97 100644 --- a/packages/provider-cursor/src/server/adapter.ts +++ b/packages/provider-cursor/src/server/adapter.ts @@ -307,6 +307,7 @@ const CURSOR_AGENT_SETTING_SOURCES = [ export function makeCursorAgentOptions(input: { readonly apiKey?: string; readonly modelSelection: ModelSelection; + readonly models?: ReadonlyArray; readonly runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy; readonly threadId: ThreadId; readonly mcpSession: McpProviderSession.McpProviderSessionConfig | undefined; @@ -315,7 +316,7 @@ export function makeCursorAgentOptions(input: { const policy = cursorRuntimeAgentPolicy(input.runtimePolicy, input.platform); const mcpServers = cursorMcpServers(input.mcpSession); return { - model: cursorSdkModelSelection(input.modelSelection), + model: cursorSdkModelSelection(input.modelSelection, input.models), name: `T3 Code ${input.threadId}`, mode: input.runtimePolicy.interactionMode === "plan" ? "plan" : "agent", ...(input.apiKey === undefined ? {} : { apiKey: input.apiKey }), @@ -2296,6 +2297,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* ( const options = makeCursorAgentOptions({ ...(apiKey === undefined ? {} : { apiKey }), modelSelection: openInput.modelSelection, + models: adapterOptions.getModels?.() ?? [], runtimePolicy: openInput.runtimePolicy, platform, threadId: openInput.threadId, @@ -2486,7 +2488,10 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* ( message, summarize: compacted, options: { - model: cursorSdkModelSelection(turnInput.modelSelection), + model: cursorSdkModelSelection( + turnInput.modelSelection, + adapterOptions.getModels?.() ?? [], + ), mode: turnInput.runtimePolicy.interactionMode === "plan" ? "plan" : "agent", ...(Object.keys(turnMcpServers).length === 0 ? {} diff --git a/packages/provider-cursor/src/server/driver.test.ts b/packages/provider-cursor/src/server/driver.test.ts index 6ff8bd7685648..213d413b6dee2 100644 --- a/packages/provider-cursor/src/server/driver.test.ts +++ b/packages/provider-cursor/src/server/driver.test.ts @@ -389,7 +389,7 @@ it.layer(layerTest)("CursorDriver", (it) => { allowance: DEFAULT_HANDOFF_TOKEN_CAP, }, catalogDefault: { - model: { id: "claude-opus-5-5" }, + model: { id: "claude-opus-5-5", params: [{ id: "context", value: "1m" }] }, window: 1_000_000, allowance: DEFAULT_HANDOFF_TOKEN_CAP, }, diff --git a/packages/provider-cursor/src/server/driver.ts b/packages/provider-cursor/src/server/driver.ts index ef50abb0a93b4..85ab45b107d71 100644 --- a/packages/provider-cursor/src/server/driver.ts +++ b/packages/provider-cursor/src/server/driver.ts @@ -208,6 +208,7 @@ export const CursorDriver: ProviderDriver< processEnv, auth.requireApiKey, auth.withAccess, + () => catalogModels, ); const checkProvider = auth.readApiKey.pipe( diff --git a/packages/provider-cursor/src/server/sdkModel.test.ts b/packages/provider-cursor/src/server/sdkModel.test.ts new file mode 100644 index 0000000000000..4ad129e1ae239 --- /dev/null +++ b/packages/provider-cursor/src/server/sdkModel.test.ts @@ -0,0 +1,73 @@ +import { ProviderInstanceId } from "@t3tools/contracts"; +import { describe, expect, it } from "vite-plus/test"; + +import { cursorSdkModelSelection } from "./sdkModel.ts"; +import { buildCursorDiscoveredModelsFromSdk } from "./status.ts"; + +const instanceId = ProviderInstanceId.make("cursor"); +const models = buildCursorDiscoveredModelsFromSdk([ + { + id: "grok-4.5", + displayName: "Grok 4.5", + parameters: [ + { + id: "effort", + displayName: "Effort", + values: [{ value: "low" }, { value: "medium" }, { value: "high" }], + }, + { id: "fast", displayName: "Fast", values: [{ value: "false" }, { value: "true" }] }, + ], + variants: [ + { + displayName: "Grok 4.5 High Fast", + isDefault: true, + params: [ + { id: "effort", value: "high" }, + { id: "fast", value: "true" }, + ], + }, + ], + }, +]); + +describe("cursorSdkModelSelection", () => { + it.each([ + { + name: "fills unset catalog parameters and keeps Fast off", + options: undefined, + params: [ + { id: "effort", value: "high" }, + { id: "fast", value: "false" }, + ], + }, + { + name: "keeps explicit options", + options: [ + { id: "effort", value: "low" }, + { id: "fastMode", value: true }, + ], + params: [ + { id: "effort", value: "low" }, + { id: "fast", value: "true" }, + ], + }, + ])("$name", ({ options, params }) => { + const selection = cursorSdkModelSelection( + { instanceId, model: "grok-4.5", ...(options === undefined ? {} : { options }) }, + models, + ); + expect({ + ...selection, + params: selection.params?.toSorted((a, b) => a.id.localeCompare(b.id)), + }).toEqual({ + id: "grok-4.5", + params, + }); + }); + + it("sends no parameters for a model missing from the catalog", () => { + expect(cursorSdkModelSelection({ instanceId, model: "auto" }, models)).toEqual({ + id: "default", + }); + }); +}); diff --git a/packages/provider-cursor/src/server/sdkModel.ts b/packages/provider-cursor/src/server/sdkModel.ts index fec0bafc45eee..ec2a53b6aea71 100644 --- a/packages/provider-cursor/src/server/sdkModel.ts +++ b/packages/provider-cursor/src/server/sdkModel.ts @@ -55,13 +55,27 @@ export function cursorContextWindowTokens( return Number.isSafeInteger(tokens) && tokens > 0 ? tokens : undefined; } -export function cursorSdkModelSelection(modelSelection: ModelSelection): CursorSdkModelSelection { +export function cursorSdkModelSelection( + modelSelection: ModelSelection, + models: ReadonlyArray = [], +): CursorSdkModelSelection { + const explicit = modelSelection.options ?? []; + const descriptors = + models.find((model) => model.slug === modelSelection.model)?.capabilities?.optionDescriptors ?? + []; + // Some Cursor plans reject runs that leave a catalog parameter unset; T3 keeps Fast off until picked. + const defaults = descriptors.flatMap((descriptor) => { + if (explicit.some((option) => option.id === descriptor.id)) return []; + const value = descriptor.id === "fastMode" ? false : getProviderOptionCurrentValue(descriptor); + return value === undefined ? [] : [{ id: descriptor.id, value }]; + }); + const options = [...explicit, ...defaults]; return { id: modelSelection.model === "auto" ? "default" : modelSelection.model, - ...(modelSelection.options === undefined || modelSelection.options.length === 0 + ...(options.length === 0 ? {} : { - params: modelSelection.options.map((option): ModelParameterValue => ({ + params: options.map((option): ModelParameterValue => ({ id: cursorSdkParameterId(option.id), value: String(option.value), })), diff --git a/packages/provider-cursor/src/server/textGeneration.ts b/packages/provider-cursor/src/server/textGeneration.ts index 278839558ee92..82982dfabbb5f 100644 --- a/packages/provider-cursor/src/server/textGeneration.ts +++ b/packages/provider-cursor/src/server/textGeneration.ts @@ -5,7 +5,11 @@ import * as Effect from "effect/Effect"; import * as Option from "effect/Option"; import * as Schema from "effect/Schema"; -import { type ProviderSetupError, TextGenerationError } from "@t3tools/contracts"; +import { + type ProviderSetupError, + type ServerProviderModel, + TextGenerationError, +} from "@t3tools/contracts"; import type { CursorSettings } from "../settings.ts"; import * as TextGenerationOperations from "@t3tools/provider-core/server/textGenerationOperations"; @@ -51,6 +55,7 @@ export const makeCursorTextGeneration = Effect.fn("makeCursorTextGeneration")(fu environment?: NodeJS.ProcessEnv, resolveApiKey?: Effect.Effect, withAccess?: CursorAuth["withAccess"], + getModels?: () => ReadonlyArray, ) { const fs = yield* FileSystem.FileSystem; const sdk = yield* CursorSdk.CursorSdk; @@ -117,7 +122,7 @@ export const makeCursorTextGeneration = Effect.fn("makeCursorTextGeneration")(fu const agentOptions = { apiKey, mode: "plan", - model: cursorSdkModelSelection(modelSelection), + model: cursorSdkModelSelection(modelSelection, getModels?.() ?? []), local: { cwd, store, From 0568a63aac2d8c7617917686ceb93701dd37fcdc Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 13 Nov 2026 08:52:07 -0800 Subject: [PATCH 011/250] =?UTF-8?q?fix(server):=20agents=20point=20to=20Se?= =?UTF-8?q?ttings=20=E2=86=92=20Labs=20when=20the=20link=20tools=20are=20h?= =?UTF-8?q?idden=20(#30371)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/server/src/provider/agentContextBudget.json | 4 ++-- evals/mcp-tool-use/cases/orchestration.ndjson | 2 ++ .../provider-core/src/server/orchestrationInstructions.ts | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/apps/server/src/provider/agentContextBudget.json b/apps/server/src/provider/agentContextBudget.json index 65f2b6b6c451e..e7901a8656ebe 100644 --- a/apps/server/src/provider/agentContextBudget.json +++ b/apps/server/src/provider/agentContextBudget.json @@ -1,5 +1,5 @@ { - "claudeSystemAppendBytes": 5597, - "codexAdditionalContextBytes": 7327, + "claudeSystemAppendBytes": 5594, + "codexAdditionalContextBytes": 7324, "mcpToolListJsonBytes": 48094 } diff --git a/evals/mcp-tool-use/cases/orchestration.ndjson b/evals/mcp-tool-use/cases/orchestration.ndjson index 28e89df5597e8..ead48236bd922 100644 --- a/evals/mcp-tool-use/cases/orchestration.ndjson +++ b/evals/mcp-tool-use/cases/orchestration.ndjson @@ -34,6 +34,8 @@ {"id": "two-children-same-model", "area": "orchestration", "kind": "bad", "pattern": "named-model rule read as a cap", "experience": "Asked for two Sol xhigh subagents, an Astra parent started one and refused the second, citing 'One child per named model' (#24967).", "rule": "Before delegate_task ... One child per named model. (intended: every named model gets a child; read as a cap of one child per model)", "privacy": "commit-safe", "setup": {"role": "top", "mocks": {"orchestrator_capabilities": {"parentThreadId": "33333333-3333-4333-8333-333333333333", "inheritedProviderInstanceId": "claudeAgent", "inheritedModel": "claude-sonnet-5-5", "inheritedOptions": [{"id": "effort", "value": "high"}], "runtimeMode": "full-access", "interactionMode": "default", "features": {"appOwnedSubagents": true, "asyncPolling": true, "cancellation": true, "batchThreadCreation": true, "threadManagement": true, "incrementalThreadRead": true, "scheduledTasks": true}, "providers": [{"providerInstanceId": "claudeAgent", "driverKind": "claudeAgent", "displayName": "Claude", "models": [{"id": "claude-opus-5-5", "label": "Claude Opus 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}, {"id": "fastMode", "type": "boolean", "default": false}]}, {"id": "claude-sonnet-5-5", "label": "Claude Sonnet 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}]}, {"id": "claude-haiku-5-5", "label": "Claude Haiku 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}]}]}, {"providerInstanceId": "codex", "driverKind": "codex", "displayName": "Codex", "models": [{"id": "gpt-6.1-sol", "label": "GPT-6.1 Sol", "options": [{"id": "reasoningEffort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}, {"id": "serviceTier", "values": ["default", "priority"], "default": "default"}]}]}, {"providerInstanceId": "muse", "driverKind": "muse", "displayName": "Muse Code", "runtimeModes": ["approval-required", "full-access"], "models": [{"id": "muse-wonder-full", "label": "Muse Wonder Full", "options": [{"id": "reasoningEffort", "values": ["minimal", "low", "medium", "high", "xhigh", "max"], "default": "high"}]}]}, {"providerInstanceId": "grok", "driverKind": "grok", "displayName": "Grok", "models": [{"id": "grok-4.7", "label": "Grok 4.7", "options": []}]}]}, "delegate_task": "default"}}, "prompt": "Launch two Sol xhigh subagents. Give each a trivial task that only replies OK.", "expect": {"calls": [{"tool": "delegate_task", "args": {"target.model": "gpt-6.1-sol", "target.options.reasoningEffort": "xhigh"}}], "minCalls": {"delegate_task": 2}, "callsMustNot": [{"tool": "delegate_task", "args": {"target.model": "~muse"}}, {"tool": "delegate_task", "args": {"target.model": "~claude"}}]}, "notes": "From #24967. One child plus the parent doing the work fails, and so does a second model in place of Sol."} {"id": "worker-counts-per-model", "area": "orchestration", "kind": "bad", "pattern": "named-model rule read as a cap", "experience": "Asked for 20 Sol workers and 20 Luna workers, the agent started one worker per model (#25359).", "rule": "Before delegate_task ... One child per named model. (intended: every named model gets a child; read as a cap of one child per model)", "privacy": "commit-safe", "setup": {"role": "top", "mocks": {"orchestrator_capabilities": {"parentThreadId": "33333333-3333-4333-8333-333333333333", "inheritedProviderInstanceId": "claudeAgent", "inheritedModel": "claude-sonnet-5-5", "inheritedOptions": [{"id": "effort", "value": "high"}], "runtimeMode": "full-access", "interactionMode": "default", "features": {"appOwnedSubagents": true, "asyncPolling": true, "cancellation": true, "batchThreadCreation": true, "threadManagement": true, "incrementalThreadRead": true, "scheduledTasks": true}, "providers": [{"providerInstanceId": "claudeAgent", "driverKind": "claudeAgent", "displayName": "Claude", "models": [{"id": "claude-opus-5-5", "label": "Claude Opus 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}, {"id": "fastMode", "type": "boolean", "default": false}]}, {"id": "claude-sonnet-5-5", "label": "Claude Sonnet 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}]}, {"id": "claude-haiku-5-5", "label": "Claude Haiku 5.5", "options": [{"id": "effort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}]}]}, {"providerInstanceId": "codex", "driverKind": "codex", "displayName": "Codex", "models": [{"id": "gpt-6.1-sol", "label": "GPT-6.1 Sol", "options": [{"id": "reasoningEffort", "values": ["low", "medium", "high", "xhigh", "max"], "default": "high"}, {"id": "serviceTier", "values": ["default", "priority"], "default": "default"}]}]}, {"providerInstanceId": "muse", "driverKind": "muse", "displayName": "Muse Code", "runtimeModes": ["approval-required", "full-access"], "models": [{"id": "muse-wonder-full", "label": "Muse Wonder Full", "options": [{"id": "reasoningEffort", "values": ["minimal", "low", "medium", "high", "xhigh", "max"], "default": "high"}]}]}, {"providerInstanceId": "grok", "driverKind": "grok", "displayName": "Grok", "models": [{"id": "grok-4.7", "label": "Grok 4.7", "options": []}]}]}, "delegate_task": "default"}, "world": {"files": {"tests/flaky/retry.test.ts": "it('retry is flaky', async () => { /* fails 1 in 5 runs */ });\n", "tests/flaky/upload.test.ts": "it('upload is flaky', async () => { /* fails 1 in 5 runs */ });\n", "tests/flaky/session.test.ts": "it('session is flaky', async () => { /* fails 1 in 5 runs */ });\n", "tests/flaky/search.test.ts": "it('search is flaky', async () => { /* fails 1 in 5 runs */ });\n", "tests/flaky/billing.test.ts": "it('billing is flaky', async () => { /* fails 1 in 5 runs */ });\n", "tests/flaky/webhook.test.ts": "it('webhook is flaky', async () => { /* fails 1 in 5 runs */ });\n"}}}, "prompt": "Six tests in tests/flaky/ fail now and then. Start 3 Muse Wonder Full workers and 3 Grok workers, one test file each, to find and fix the flake.", "expect": {"calls": [{"tool": "delegate_task", "args": {"target.model": "muse-wonder-full"}}, {"tool": "delegate_task", "args": {"target.model": "grok-4.7"}}], "minCalls": {"delegate_task": 6}}, "notes": "Scaled down from the real 20 Sol + 20 Luna ask. Muse and Grok need delegate_task from every parent, so a native Sol helper cannot pass or fail it."} {"id": "linked-thread-chip-reads-there", "area": "orchestration", "kind": "guard", "pattern": "thread chip from a linked machine read on this one", "experience": "A thread chip from a linked machine names its environmentId; an agent that reads it without passing that id reads this machine, where the thread does not exist.", "rule": "The thread chip names its environmentId; t3_thread_read takes it.", "privacy": "commit-safe", "setup": {"role": "top", "linkedEnvironments": true, "mocks": {"t3_environment_links": {"environmentId": "environment-laptop", "links": [{"environmentId": "environment-vps", "label": "Hetzner VPS", "status": "reachable", "access": "auto-accept-edits", "expiresAt": "2026-12-01T00:00:00.000Z", "lastError": null}], "notListed": "Not listed: not linked yet. Call t3_environment_link with a hint from the user's words."}, "t3_thread_read": {"thread": {"threadId": "thread-deploy-api", "title": "Deploy the API", "status": "idle"}, "items": [{"position": 1, "createdBy": "user", "text": "Deploy the API to the VPS and check the health endpoint."}, {"position": 2, "createdBy": "assistant", "text": "Deployed. /health returns 200, but /health/db times out after 5s: the API can't reach Postgres on 10.0.0.5:5432."}], "nextPosition": 3, "hasMore": false}}}, "prompt": "Pick up where [Deploy the API](t3-context://v1/thread/thread_deploy-api) left off. What is still failing?", "promptContext": {"records": [{"version": 1, "kind": "thread", "contextId": "thread_deploy-api", "label": "Deploy the API", "environmentId": "environment-vps", "threadId": "thread-deploy-api", "title": "Deploy the API"}]}, "expect": {"calls": [{"tool": "t3_thread_read", "args": {"threadId": "thread-deploy-api", "environmentId": "environment-vps"}}], "notCalls": ["t3_environment_link"]}, "source": {"request": "akj on #17130: attach a linked machine's thread without dragging"}} +{"id": "vps-delegate-links-off-names-setting", "area": "orchestration", "kind": "bad", "pattern": "hidden link tools, setting never named", "experience": "With Linked environments off, the default, an agent asked to delegate to the user's VPS searched for t3_environment_link, then said the tool was not in this build and never named the setting that turns it on.", "rule": "The link tools are absent while Settings → Labs → Linked environments is off; the agent tells the user to turn it on there.", "privacy": "commit-safe", "setup": {"role": "top"}, "prompt": "Delegate this to my vps: run `uname -a` there and tell me the result. Do not edit any files here.", "expect": {"textMust": ["Labs", "Linked environments"], "notCalls": ["delegate_task", "t3_environment_links", "t3_environment_link"]}, "source": {"issue": "pingdotgg/t3code#30358"}} +{"id": "vps-delegate-links-on-calls-link", "area": "orchestration", "kind": "control", "pattern": "unlinked machine with Linked environments on", "experience": "With Linked environments on, a machine the user names that is not linked gets a t3_environment_link card, not advice to turn the setting on.", "rule": "A machine the user names that is not linked: call t3_environment_link with a hint from their words.", "privacy": "commit-safe", "setup": {"role": "top", "linkedEnvironments": true, "mocks": {"t3_environment_links": {"environmentId": "environment-laptop", "links": [], "notListed": "Not listed: not linked yet. Call t3_environment_link with a hint from the user's words."}, "t3_environment_link": {"status": "pending", "turnItemId": "turn-item-link-card-1"}}}, "prompt": "Delegate this to my vps: run `uname -a` there and tell me the result. Do not edit any files here.", "expect": {"calls": [{"tool": "t3_environment_link"}], "textMustNot": ["Labs", "([Tt]urn on|[Ee]nable)[^.\\n]*[Ll]inked environments"]}, "source": {"issue": "pingdotgg/t3code#30358"}} {"id": "background-wait-unmatched-id", "area": "orchestration", "kind": "guard", "pattern": "waits on a task ID that matches no background work, then ends the turn as if waiting", "experience": "One agent called t3_background_wait with the literal ID 'placeholder', another with a Claude subagent ID. The tool returned a normal result listing the ID under `unmatched`, so the agent said it would continue when the work came back and ended its turn. Nothing ever woke it.", "rule": "To wait for a background command or Monitor you started (tests, a build), call `t3_background_wait` with its task ID before you end your turn.", "privacy": "commit-safe", "setup": {"role": "top", "harness": "claude", "priorTurns": [{"role": "user", "text": "Run the full test suite in the background and fix whatever fails. It takes about 15 minutes."}, {"role": "assistant", "toolCalls": [{"tool": "Bash", "input": {"command": "vp test run", "description": "Run the full test suite", "run_in_background": true}, "result": "Command running in background with ID: bq7m2x9k. Output is being written to: /tmp/claude/tasks/bq7m2x9k.output"}, {"tool": "t3_background_wait", "input": {"taskIds": ["placeholder"]}, "result": "default"}]}], "world": {"backgroundTasks": ["bq7m2x9k"], "tests": "The suite started a minute ago and runs for about 15 minutes. Until then /tmp/claude/tasks/bq7m2x9k.output lists 38 passing test files and no failures yet."}}, "prompt": "I'm heading out for a bit. Tell me what failed when it's done.", "expect": {"calls": [{"tool": "t3_background_wait", "args": {"taskIds": ["bq7m2x9k"]}}]}, "source": {"issue": "#29772"}, "notes": "The seeded wait gets T3's real result for an ID the session does not know. The tests are still running, so the only way to keep the thread waiting is a wait on bq7m2x9k. In replay, Sonnet 5.5 and Opus 5.5 also recover from main's success result, so this guards the recovery rather than reproducing the incident. Claude only: t3_background_wait is a Claude tool.", "maxSteps": 8} {"id": "background-wait-matched-id", "area": "orchestration", "kind": "control", "pattern": "waits on the right task ID, then ends the turn", "experience": "With the background command's own ID registered, ending the turn is correct: T3 keeps the thread working and wakes it when the tests finish.", "rule": "To wait for a background command or Monitor you started (tests, a build), call `t3_background_wait` with its task ID before you end your turn.", "privacy": "commit-safe", "setup": {"role": "top", "harness": "claude", "priorTurns": [{"role": "user", "text": "Run the full test suite in the background and fix whatever fails. It takes about 15 minutes."}, {"role": "assistant", "toolCalls": [{"tool": "Bash", "input": {"command": "vp test run", "description": "Run the full test suite", "run_in_background": true}, "result": "Command running in background with ID: bq7m2x9k. Output is being written to: /tmp/claude/tasks/bq7m2x9k.output"}, {"tool": "t3_background_wait", "input": {"taskIds": ["bq7m2x9k"]}, "result": "default"}]}], "world": {"backgroundTasks": ["bq7m2x9k"], "tests": "The suite started a minute ago and runs for about 15 minutes. Until then /tmp/claude/tasks/bq7m2x9k.output lists 38 passing test files and no failures yet."}}, "prompt": "I'm heading out for a bit. Tell me what failed when it's done.", "expect": {"maxCalls": {"t3_background_wait": 1, "Bash": 3}, "notCalls": ["ScheduleWakeup"]}, "source": {"issue": "#29772"}, "notes": "Same thread as background-wait-unmatched-id, with a wait T3 matched. Guards against a fix that makes agents distrust a good wait and poll; a few looks at the output are ordinary. Claude only.", "maxSteps": 8} {"id": "settle-this-thread-after-cleanup", "area": "orchestration", "kind": "bad", "pattern": "settle read as plain English for finish", "experience": "Asked to clean up scratch files and settle this thread, the agent deleted the files and ended the turn without calling t3_thread_organize (#26749).", "rule": "A request to settle this thread calls t3_thread_organize with action settle.", "privacy": "commit-safe", "notes": "From #26749. Claude defers MCP tools, so only the tool's name is visible until ToolSearch loads it; 'settle' appears only in the t3_thread_organize description.", "setup": {"role": "top", "world": {"files": {"scratch/run-1.log": "ok\n", "scratch/run-2.log": "ok\n", "src/index.ts": "export const x = 1;\n"}}, "mocks": {"t3_thread_organize": {"settlesWhenTurnEnds": true}}}, "prompt": "Clean up the scratch files and settle this thread.", "expect": {"calls": [{"tool": "t3_thread_organize", "args": {"action": "settle"}}], "notCalls": ["t3_thread_update"]}} diff --git a/packages/provider-core/src/server/orchestrationInstructions.ts b/packages/provider-core/src/server/orchestrationInstructions.ts index bb4d33d2c56e2..4ce08d71119fe 100644 --- a/packages/provider-core/src/server/orchestrationInstructions.ts +++ b/packages/provider-core/src/server/orchestrationInstructions.ts @@ -13,8 +13,8 @@ Ordinary coding tasks need no T3 tools. - As a subagent or child, do the work yourself. Start no subagents unless your task says to.`, `- \`t3_thread_launch\` and \`create_threads\` make new top-level threads, only when the user asks for a new thread. "Subagent", "delegate", and "parallel" never mean one. Worktree thread: set \`workspaceStrategy\`. Honor a requested base. Otherwise independent work uses the default branch as \`remote/branch\` with \`startFromOrigin:true\`; with no remote, use the local default. Required unpublished commits and dependent work use their local base. Shell worktrees don't bind it, and no checkout setup in its prompt. No \`workspaceStrategy\`: project root, not your worktree. - Given a thread id, \`t3_thread_read\` it before you answer about it. Asked to settle a thread, this one too: \`t3_thread_organize\`. -- Another machine: \`t3_environment_links\` lists linked ones. Pass its \`environmentId\` to a T3 tool to act there. Ids are then that machine's, so pass \`threadId\`. -- A machine the user names that isn't linked ("delegate this to my vps"): call \`t3_environment_link\` with a \`hint\` from their words. Never ask for a pairing code in chat. +- Another machine: \`t3_environment_links\` lists linked ones. Pass its \`environmentId\` and that machine's \`threadId\` to T3 tools. +- Unlinked ("delegate this to my vps"): \`t3_environment_link\` with a \`hint\` from the user's words, never a pairing code in chat. No link tools: say to enable Settings → Labs → Linked environments. - In your reply, mention a thread by its \`link\`, verbatim, never a plain title or bare id. - A login, token, PIN, pairing code or sudo password only the user can give: call \`request_secret\`, not a chat ask or a handoff. Not for access you have. Sudo: if \`sudo -n \` needs a password, use \`delivery: "file"\` and run \`sudo -S -p '' < "$file"\`; the password takes stdin, so give other input as a file argument. Asked only to schedule: \`schedule_task\` without running its prompt. - T3 tools may load late. Try the name once before you call it absent.`, From 139ec8080f37781bf4576f63186022b0a8472b71 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 13 Nov 2026 08:52:21 -0800 Subject: [PATCH 012/250] fix(server): Claude no longer dispatches skill mentions from handoff history (#34497) --- .../Adapters/ClaudeAdapterV2.test.ts | 50 +++++++++++++++++++ .../Adapters/ClaudeAdapterV2.ts | 30 ++++++++--- .../ProviderTurnStartService.ts | 7 ++- .../provider/Drivers/ClaudeSkillDispatch.ts | 4 +- .../src/server/ProviderAdapter.ts | 2 + 5 files changed, 81 insertions(+), 12 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 18a16e2f63102..155370da1757d 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -109,6 +109,7 @@ import * as ClaudeAdapterV2 from "./ClaudeAdapterV2.ts"; import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; import * as ProviderInstallationGate from "../../provider/ProviderInstallationGate.ts"; import { ClaudeOrchestratorReplayHarness } from "./ClaudeAdapterV2.testkit.ts"; +import { withInlineContext } from "../ContextHandoffDelivery.ts"; import { provideDeterministicTestRuntime } from "../testkit/DeterministicRuntime.ts"; import { runOrchestratorV2Scenario } from "../testkit/OrchestratorScenario.ts"; import { layerProviderReplay } from "../testkit/ProviderReplayHarness.ts"; @@ -5483,6 +5484,55 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); + // regression: #34393 a provider switch replayed a $skill from the handoff history + it.effect("dispatches only skills from the user's text, not from inline handoff history", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const homePath = yield* fs.makeTempDirectoryScoped({ prefix: "t3-claude-skill-handoff-" }); + const directory = path.join(homePath, "skills", "review"); + yield* fs.makeDirectory(directory, { recursive: true }); + yield* fs.writeFileString( + path.join(directory, "SKILL.md"), + "---\nname: review\n---\nFollow the review instructions.\n", + ); + const harness = yield* makeWakeHarnessWithOptions({ + settings: { ...DEFAULT_CLAUDE_SETTINGS, homePath }, + }); + const history = "Context handoff:\n[Historical user; user_message]\n$review the diff"; + const text = withInlineContext(history, "continue"); + yield* harness.runtime.startTurn({ + ...makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make("skill-handoff"), + text, + attachments: [], + runtimePolicy: { ...CLAUDE_TEST_RUNTIME_POLICY, cwd: homePath }, + }), + inlineContextLength: text.length - "continue".length, + }); + + const content = harness.offeredMessages[0]?.message.content; + const blocks = + typeof content === "string" + ? [content] + : (content ?? []).map((block) => (block.type === "text" ? block.text : "")); + assert.isFalse(blocks.some((text) => text.startsWith("/review"))); + assert.include(blocks.join("\n"), "$review the diff"); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + // regression: #15475 MCP title arriving after the tool starts must survive to completion it.effect.each([ { isError: false, title: "Check weather" }, diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 64537c7e5fa8f..b3c1fd1c1fed4 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -2631,12 +2631,15 @@ export function makeClaudeUserMessage(input: { readonly text: string; readonly priority?: SDKUserMessage["priority"]; readonly skills?: ReadonlyMap; + readonly skillMentionsFrom?: number; readonly uuid?: SDKUserMessage["uuid"]; }): SDKUserMessage { // Claude expands a skill only when `/name` starts the last text block. // Keep the whole request before that block; see ClaudeSkillDispatch. const dispatch = - input.skills === undefined ? undefined : planClaudeSkillDispatch(input.text, input.skills); + input.skills === undefined + ? undefined + : planClaudeSkillDispatch(input.text, input.skills, input.skillMentionsFrom); // Claude runs `/goal` only when it opens the prompt, so a goal stays one block. const isGoalCommand = parseGoalCommand({ text: input.text }) !== null; return { @@ -2801,11 +2804,15 @@ const makeClaudeUserMessageWithAttachments = Effect.fnUntraced(function* (input: readonly fileSystem: FileSystem.FileSystem; readonly pdfReservation: Effect.Success>; readonly skills?: ReadonlyMap; + readonly skillMentionsFrom?: number; }) { if (input.attachments.length === 0) { const message = makeClaudeUserMessage({ text: input.text, ...(input.skills === undefined ? {} : { skills: input.skills }), + ...(input.skillMentionsFrom === undefined + ? {} + : { skillMentionsFrom: input.skillMentionsFrom }), ...(input.priority === undefined ? {} : { priority: input.priority }), ...(input.uuid === undefined ? {} : { uuid: input.uuid }), }); @@ -2829,7 +2836,7 @@ const makeClaudeUserMessageWithAttachments = Effect.fnUntraced(function* (input: const dispatch = input.skills === undefined ? undefined - : planClaudeSkillDispatch(textWithAttachmentPaths, input.skills); + : planClaudeSkillDispatch(textWithAttachmentPaths, input.skills, input.skillMentionsFrom); const content: Array = []; // Base64 adds a third to the payload. Leave room below Anthropic's 32 MB // request limit for text, instructions, and SDK framing. The SDK does not @@ -12422,15 +12429,24 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( pdfBudgets.get(nativeThreadId)!, persistAttachmentBudget(nativeThreadId), ); + const promptText = applyClaudePromptEffortPrefix( + turnInput.message.text, + compileClaudeModelSelection(turnInput.modelSelection, modelCatalog).promptEffort, + ); const userMessage = promptUuid === null ? null : yield* makeClaudeUserMessageWithAttachments({ - text: applyClaudePromptEffortPrefix( - turnInput.message.text, - compileClaudeModelSelection(turnInput.modelSelection, modelCatalog) - .promptEffort, - ), + text: promptText, + // Mentions in inline handoff history are history, not this turn's skill. + ...(turnInput.inlineContextLength === undefined + ? {} + : { + skillMentionsFrom: + promptText.length - + turnInput.message.text.slice(turnInput.inlineContextLength).trimEnd() + .length, + }), attachments: turnInput.message.attachments, attachmentsDir, fileSystem, diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts index 8f01d117e1739..ddb9416682710 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts @@ -1639,12 +1639,11 @@ export const layer: Layer.Layer< .filter((part) => part !== "") .join("\n\n"); const { restartContinuationOfRunId: _resumedRunId, ...promptedInput } = turnInput; + const text = goalCommand ? userText : withInlineContext(context, userText); yield* start({ ...(noteContinuation ? promptedInput : turnInput), - message: { - ...turnInput.message, - text: goalCommand ? userText : withInlineContext(context, userText), - }, + message: { ...turnInput.message, text }, + ...(text === userText ? {} : { inlineContextLength: text.length - userText.length }), }).pipe( // Clear pending handoff markers for refused prompts so the next turn retains the native session. Effect.tapError((error) => diff --git a/apps/server/src/provider/Drivers/ClaudeSkillDispatch.ts b/apps/server/src/provider/Drivers/ClaudeSkillDispatch.ts index 6ce141eec733e..bdf2ed1cae61a 100644 --- a/apps/server/src/provider/Drivers/ClaudeSkillDispatch.ts +++ b/apps/server/src/provider/Drivers/ClaudeSkillDispatch.ts @@ -22,12 +22,14 @@ export interface ClaudeSkillDispatch { export function planClaudeSkillDispatch( prompt: string, skills: ReadonlyMap, + /** Mentions before this offset, such as inline handoff history, stay plain text. */ + mentionsFrom = 0, ): ClaudeSkillDispatch | undefined { const mentions = [...prompt.matchAll(SKILL_MENTION_PATTERN)].flatMap((match) => { const name = match[2] ?? ""; const skill = skills.get(name); - if (!skill) return []; const start = (match.index ?? 0) + (match[1]?.length ?? 0); + if (!skill || start < mentionsFrom) return []; return [{ name, skill, start, end: (match.index ?? 0) + match[0].length }]; }); const last = mentions.at(-1); diff --git a/packages/provider-core/src/server/ProviderAdapter.ts b/packages/provider-core/src/server/ProviderAdapter.ts index fc83ac128324e..5b1b6301fa48b 100644 --- a/packages/provider-core/src/server/ProviderAdapter.ts +++ b/packages/provider-core/src/server/ProviderAdapter.ts @@ -535,6 +535,8 @@ export interface ProviderAdapterV2TurnInput { readonly rootNodeId: NodeId; readonly providerThread: OrchestrationV2ProviderThread; readonly message: ProviderAdapterV2TurnMessage; + /** Leading characters of `message.text` that are inline handoff context, not the user's words. */ + readonly inlineContextLength?: number; readonly modelSelection: ModelSelection; readonly runtimePolicy: ProviderAdapterV2RuntimePolicy; readonly appContext?: ReadonlyArray<{ readonly key: string; readonly text: string }>; From 4b316fd5445b8921985b9ac2b1b8075fbe08a688 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 13 Nov 2026 08:52:45 -0800 Subject: [PATCH 013/250] fix(web): clear deleted checkout labels from the sidebar (#25063) --- apps/server/src/ws.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts index 66512ab6a22c0..1f2a148734d4e 100644 --- a/apps/server/src/ws.ts +++ b/apps/server/src/ws.ts @@ -3157,7 +3157,15 @@ export const layerWsRpc = ( [WS_METHODS.vcsCreateWorktree]: (input) => gitWorkflow.createWorktree(input).pipe(Effect.tap(() => refreshGitStatus(input.cwd))), [WS_METHODS.vcsRemoveWorktree]: (input) => - gitWorkflow.removeWorktree(input).pipe(Effect.tap(() => refreshGitStatus(input.cwd))), + gitWorkflow.removeWorktree(input).pipe( + Effect.tap(() => refreshGitStatus(input.cwd)), + // Subscribers of the removed checkout learn it is no longer a repository. + Effect.tap(() => + vcsStatusBroadcaster + .refreshLocalStatus(input.path) + .pipe(Effect.ignoreCause({ log: true }), Effect.forkDetach, Effect.asVoid), + ), + ), [WS_METHODS.vcsCreateRef]: (input) => gitWorkflow.createRef(input).pipe(Effect.tap(() => refreshGitStatus(input.cwd))), [WS_METHODS.vcsSwitchRef]: (input) => From 4582a3bc682d1258ae79ae4c0024a054dd5e64f2 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 13 Nov 2026 08:55:29 -0800 Subject: [PATCH 014/250] fix(server): MacPorts-owned agent CLIs are no longer updated through npm or their own updater (#36229) --- .../src/server/maintenanceResolver.test.ts | 49 +++++++++++++++++++ .../src/server/maintenanceResolver.ts | 33 +++++++++++++ 2 files changed, 82 insertions(+) diff --git a/packages/provider-core/src/server/maintenanceResolver.test.ts b/packages/provider-core/src/server/maintenanceResolver.test.ts index 0d4ad83f88e61..3475881732fae 100644 --- a/packages/provider-core/src/server/maintenanceResolver.test.ts +++ b/packages/provider-core/src/server/maintenanceResolver.test.ts @@ -1634,6 +1634,55 @@ it.layer( }), ); + for (const fixture of [ + { + name: "an npm-shaped package", + definition: packageToolUpdate, + target: ["lib", "node_modules", "@example", "package-tool", "bin", "package-tool.exe"], + }, + { + name: "a launcher script", + definition: nativePackageToolUpdate, + target: ["share", "native-package-tool", "native-package-tool"], + }, + ]) { + it.effect.skipIf(!symlinksSupported || windowsHost)( + `leaves ${fixture.name} owned by MacPorts to MacPorts`, + () => + Effect.gen(function* () { + const prefix = yield* makeTempDir("t3-macports"); + writeExecutable(NodePath.join(prefix, "bin", "port")); + const target = NodePath.join(prefix, ...fixture.target); + writeExecutable(target); + const launcher = NodePath.join(prefix, "bin", NodePath.basename(target, ".exe")); + NodeFS.symlinkSync(target, launcher); + + const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect( + fixture.definition, + { binaryPath: launcher, env: { PATH: NodePath.join(prefix, "bin") } }, + ).pipe( + Effect.provideService(HostProcess.Platform, "darwin"), + Effect.provideService( + ChildProcessSpawner.ChildProcessSpawner, + stdoutSpawner((command, args) => + NodePath.basename(command) === "port" && args.includes("provides") + ? args.includes("-q") + ? "package-tool\n" + : args + .filter((arg) => arg.startsWith("/")) + .map((file) => `${file} is provided by: package-tool\n`) + .join("") + : "", + ), + ), + ); + + expect(capabilities.update).toBeNull(); + expect(capabilities.manualCommand).toBe("sudo port upgrade package-tool"); + }), + ); + } + it("quotes copyable command words the shell would split", () => { const posix = makeProviderMaintenanceCapabilities({ provider: driver("packageTool"), diff --git a/packages/provider-core/src/server/maintenanceResolver.ts b/packages/provider-core/src/server/maintenanceResolver.ts index 88730be615d78..1d7125325381e 100644 --- a/packages/provider-core/src/server/maintenanceResolver.ts +++ b/packages/provider-core/src/server/maintenanceResolver.ts @@ -802,6 +802,15 @@ export const resolvePackageManagedProviderMaintenance = Effect.fn( if (commandPaths.some(isNixStoreCommandPath)) { return manual; } + const macPortsName = yield* resolveMacPortsOwner(context); + if (macPortsName !== null) { + return makeManualOnlyProviderMaintenanceCapabilities({ + provider: definition.provider, + packageName, + installPath: context.realCommandPath, + ...(macPortsName ? { manualCommand: `sudo port upgrade ${macPortsName}` } : {}), + }); + } const nativeUpdate = definition.nativeUpdate; const nativeEnv = nativeUpdate?.env; @@ -1020,6 +1029,30 @@ const isMiseWrapperScript = Effect.fn("isMiseWrapperScript")(function* ( return script.startsWith("#!") && /\bmise\s+(?:x|exec)\b/.test(script); }); +/** + * The port that installed the executable, "" when MacPorts owns it under an unusable name, + * or null when it does not. MacPorts prefixes are root-owned and its Claude port disables + * `claude update`, so neither npm nor the native updater can upgrade these installs. + */ +const resolveMacPortsOwner = Effect.fn("resolveMacPortsOwner")(function* ( + context: ProviderMaintenanceResolutionContext, +) { + if (context.platform === "win32") return null; + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const siblingPort = path.join(path.dirname(context.resolvedCommandPath), "port"); + const portPath = (yield* fileSystem.exists(siblingPort).pipe(Effect.orElseSucceed(() => false))) + ? siblingPort + : yield* resolveCommandPath("port", { env: context.env }); + if (!portPath) return null; + const prefix = `${normalizeCommandPath(path.dirname(path.dirname(portPath)))}/`; + if (!normalizeCommandPath(context.realCommandPath).startsWith(prefix)) return null; + const output = yield* runProbe(portPath, ["provides", context.realCommandPath], context.env); + const name = output ? /\bis provided by: (\S+)/.exec(output)?.[1] : undefined; + if (name === undefined) return null; + return /^[\w.+-]+$/.test(name) ? name : ""; +}); + // Volta bin links only identify its shim; the package image proves ownership. const isVoltaPackageInstall = Effect.fn("isVoltaPackageInstall")(function* ( context: ProviderMaintenanceResolutionContext, From 286eeea19f8b9b06f84c61c6a2f46fba65c52313 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 13 Nov 2026 08:56:04 -0800 Subject: [PATCH 015/250] test(desktop): AppImage shim test no longer writes into its temp dir after the shim exits (#36235) --- apps/desktop/src/app/DesktopCliShim.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/desktop/src/app/DesktopCliShim.test.ts b/apps/desktop/src/app/DesktopCliShim.test.ts index d43e7a8388b83..3e8b4967dfde5 100644 --- a/apps/desktop/src/app/DesktopCliShim.test.ts +++ b/apps/desktop/src/app/DesktopCliShim.test.ts @@ -56,7 +56,7 @@ const fakeAppImage = (canMount = true, app = REPORTER) => { `echo $$ >'${root}/mounter.pid'`, canMount ? `echo '${image}'` : 'echo "Cannot mount AppImage"; exit 1', // Like the FUSE runtime, any of these unmounts. fd 3 reports to the test when it is open. - `trap 'echo unmounted >&3 2>/dev/null; touch "${root}/unmounted"; exit 0' TERM HUP INT`, + "trap 'echo unmounted >&3 2>/dev/null; exit 0' TERM HUP INT", "trap 'echo mounted >&3 2>/dev/null' WINCH", "while :; do sleep 0.05; done", "", From f62fa296f065b6399fb68edf1863e6f4c831641b Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 13 Nov 2026 09:01:05 -0800 Subject: [PATCH 016/250] fix(web): Add to another machine can find ignored files past the first 200 (#36228) --- .../src/environment/ServerEnvironment.ts | 1 + .../src/workspace/WorkspaceEntries.test.ts | 14 +++ apps/server/src/workspace/WorkspaceEntries.ts | 6 +- .../components/AddProjectToMachineDialog.tsx | 116 ++++++++++++------ apps/web/src/lib/addProjectToMachine.ts | 3 + packages/contracts/src/environment.ts | 2 + packages/contracts/src/project.ts | 2 + 7 files changed, 108 insertions(+), 36 deletions(-) diff --git a/apps/server/src/environment/ServerEnvironment.ts b/apps/server/src/environment/ServerEnvironment.ts index 912a30f563684..2c79189878e8a 100644 --- a/apps/server/src/environment/ServerEnvironment.ts +++ b/apps/server/src/environment/ServerEnvironment.ts @@ -274,6 +274,7 @@ export const make = Effect.gen(function* () { projectCloneTracking: true, projectOrganization: true, ignoredFileListing: true, + ignoredFileSearch: true, triage: true, oneTimeSchedules: true, freshContext: true, diff --git a/apps/server/src/workspace/WorkspaceEntries.test.ts b/apps/server/src/workspace/WorkspaceEntries.test.ts index 348b5f877ac24..d20e378bae053 100644 --- a/apps/server/src/workspace/WorkspaceEntries.test.ts +++ b/apps/server/src/workspace/WorkspaceEntries.test.ts @@ -433,6 +433,20 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => { }); }), ); + + it.effect("finds ignored files that sort past the listing limit", () => + Effect.gen(function* () { + const cwd = yield* makeTempDir({ git: true }); + yield* writeTextFile(cwd, ".gitignore", "*.secret\n"); + for (let index = 1; index <= 205; index += 1) { + yield* writeTextFile(cwd, `file-${String(index).padStart(3, "0")}.secret`, "s"); + } + + const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; + const listed = yield* workspaceEntries.listIgnoredFiles({ cwd, query: "file-205" }); + expect(listed.files).toContainEqual({ path: "file-205.secret", byteLength: 1 }); + }), + ); }); describe("search", () => { diff --git a/apps/server/src/workspace/WorkspaceEntries.ts b/apps/server/src/workspace/WorkspaceEntries.ts index cac706f7ca4a2..43c8fae4dfcaf 100644 --- a/apps/server/src/workspace/WorkspaceEntries.ts +++ b/apps/server/src/workspace/WorkspaceEntries.ts @@ -576,9 +576,13 @@ export const make = Effect.gen(function* () { }) .pipe(Effect.orElseSucceed(() => undefined)); if (!result || result.exitCode !== 0) return { files: [], truncated: false }; + // Filter before the cap so files sorting past it stay reachable by search. + const query = input.query?.toLowerCase() ?? ""; const paths = result.stdout .split("\0") - .filter((entry) => entry.length > 0 && !entry.endsWith("/")) + .filter( + (entry) => entry.length > 0 && !entry.endsWith("/") && entry.toLowerCase().includes(query), + ) .toSorted(); const files = yield* Effect.promise(() => Promise.all( diff --git a/apps/web/src/components/AddProjectToMachineDialog.tsx b/apps/web/src/components/AddProjectToMachineDialog.tsx index 3d9c064cfcb23..3bb70c8b609eb 100644 --- a/apps/web/src/components/AddProjectToMachineDialog.tsx +++ b/apps/web/src/components/AddProjectToMachineDialog.tsx @@ -26,6 +26,7 @@ import { useAddProjectTargets } from "../hooks/useAddProjectTargets"; import { useNewThreadHandler } from "../hooks/useHandleNewThread"; import { useLogicalProjectEnvironments } from "../hooks/useLogicalProjectEnvironments"; import { + COPIED_FILES_MAX_COUNT, COPYABLE_FILE_MAX_BYTES, addProjectToMachine, isDefaultCopiedFile, @@ -35,6 +36,7 @@ import { newProjectId } from "../lib/utils"; import { readProject, subscribeProject, useProject } from "../state/entities"; import { useEnvironments } from "../state/environments"; import { projectEnvironment } from "../state/projects"; +import { useDebouncedValue } from "../state/queries"; import { useEnvironmentQuery } from "../state/query"; import { EnvironmentMachineIcon } from "./EnvironmentMachineIcon"; import { Button } from "./ui/button"; @@ -121,8 +123,10 @@ function AddProjectToMachineDialog({ request }: { request: AddProjectToMachineRe const [pathOverride, setPathOverride] = useState(null); const destinationPath = pathOverride ?? defaultPath; - const canListFiles = source?.serverConfig?.environment.capabilities.ignoredFileListing === true; - const ignoredFiles = useEnvironmentQuery( + const capabilities = source?.serverConfig?.environment.capabilities; + const canListFiles = capabilities?.ignoredFileListing === true; + const canSearchFiles = canListFiles && capabilities?.ignoredFileSearch === true; + const allIgnoredFiles = useEnvironmentQuery( project && canListFiles ? projectEnvironment.listIgnoredFiles({ environmentId: project.environmentId, @@ -130,13 +134,29 @@ function AddProjectToMachineDialog({ request }: { request: AddProjectToMachineRe }) : null, ); + const [fileQuery, setFileQuery] = useState(""); + const sentFileQuery = useDebouncedValue(fileQuery.trim(), 120); + const searching = canSearchFiles && sentFileQuery.length > 0; + const searchedFiles = useEnvironmentQuery( + project && searching + ? projectEnvironment.listIgnoredFiles({ + environmentId: project.environmentId, + input: { cwd: project.workspaceRoot, query: sentFileQuery }, + }) + : null, + ); + const ignoredFiles = searching ? searchedFiles : allIgnoredFiles; const files = ignoredFiles.data?.files ?? []; - const [selected, setSelected] = useState | null>(null); - const selectedFiles = selected ?? defaultSelection(files); + // Keyed by path with the file kept, so picks made under one search survive the next. + const [selected, setSelected] = useState | null>(null); + const selectedFiles = selected ?? defaultSelection(allIgnoredFiles.data?.files ?? []); const copyable = files.filter((file) => file.byteLength <= COPYABLE_FILE_MAX_BYTES); - const selectedBytes = files - .filter((file) => selectedFiles.has(file.path)) - .reduce((total, file) => total + file.byteLength, 0); + const allCopyableSelected = copyable.every((file) => selectedFiles.has(file.path)); + const atSelectionLimit = selectedFiles.size >= COPIED_FILES_MAX_COUNT; + const selectedBytes = [...selectedFiles.values()].reduce( + (total, file) => total + file.byteLength, + 0, + ); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(null); @@ -146,11 +166,19 @@ function AddProjectToMachineDialog({ request }: { request: AddProjectToMachineRe const unavailable = project === null || target === null || - (canListFiles && ignoredFiles.data === null && ignoredFiles.error === null); - const toggleFile = (path: string, checked: boolean) => { - const next = new Set(selectedFiles); - if (checked) next.add(path); - else next.delete(path); + (canListFiles && allIgnoredFiles.data === null && allIgnoredFiles.error === null); + const toggleFile = (file: ProjectIgnoredFile, checked: boolean) => { + const next = new Map(selectedFiles); + if (!checked) next.delete(file.path); + else if (!atSelectionLimit) next.set(file.path, file); + setSelected(next); + }; + const toggleShownFiles = () => { + const next = new Map(selectedFiles); + for (const file of copyable) { + if (allCopyableSelected) next.delete(file.path); + else if (next.size < COPIED_FILES_MAX_COUNT) next.set(file.path, file); + } setSelected(next); }; @@ -198,7 +226,7 @@ function AddProjectToMachineDialog({ request }: { request: AddProjectToMachineRe title: project.title, remoteUrl, destinationPath: destinationPath.trim(), - files: files.filter((file) => selectedFiles.has(file.path)).map((file) => file.path), + files: [...selectedFiles.keys()], }); close(); openClonedProject(scopeProjectRef(target.environmentId, projectId)); @@ -334,33 +362,41 @@ function AddProjectToMachineDialog({ request }: { request: AddProjectToMachineRe {copyable.length > 0 ? ( <> - {selectedFiles.size} of {files.length} + {selectedFiles.size} of {allIgnoredFiles.data?.files.length ?? 0} {selectedBytes > 0 ? ` · ${formatBytes(selectedBytes)}` : ""} - ) : null} + {canSearchFiles && (allIgnoredFiles.data?.truncated || fileQuery.length > 0) ? ( + setFileQuery(event.target.value)} + // Enter would otherwise submit the clone. + onKeyDown={(event) => { + if (event.key === "Enter") event.preventDefault(); + }} + spellCheck={false} + autoComplete="off" + /> + ) : null} {ignoredFiles.error ? (

{ignoredFiles.error}

) : files.length === 0 ? (

- {ignoredFiles.isSuccess - ? "No ignored files to copy." - : "Finding ignored files…"} + {!ignoredFiles.isSuccess + ? "Finding ignored files…" + : searching + ? "No ignored files match." + : "No ignored files to copy."}

) : (
@@ -369,7 +405,8 @@ function AddProjectToMachineDialog({ request }: { request: AddProjectToMachineRe key={file.path} file={file} checked={selectedFiles.has(file.path)} - onCheckedChange={(checked) => toggleFile(file.path, checked)} + disabled={atSelectionLimit && !selectedFiles.has(file.path)} + onCheckedChange={(checked) => toggleFile(file, checked)} /> ))}
@@ -377,6 +414,7 @@ function AddProjectToMachineDialog({ request }: { request: AddProjectToMachineRe {ignoredFiles.data?.truncated ? (

Showing the first {files.length} files. + {canSearchFiles ? " Search to find the rest." : ""}

) : null}

@@ -415,29 +453,37 @@ function projectSubdirectory(repositoryRoot: string | undefined, workspaceRoot: : ""; } -function defaultSelection(files: readonly ProjectIgnoredFile[]): ReadonlySet { - return new Set( +function defaultSelection( + files: readonly ProjectIgnoredFile[], +): ReadonlyMap { + return new Map( files .filter( (file) => file.byteLength <= COPYABLE_FILE_MAX_BYTES && isDefaultCopiedFile(file.path), ) - .map((file) => file.path), + .map((file) => [file.path, file]), ); } function IgnoredFileRow({ file, checked, + disabled, onCheckedChange, }: { file: ProjectIgnoredFile; checked: boolean; + disabled: boolean; onCheckedChange: (checked: boolean) => void; }) { const tooLarge = file.byteLength > COPYABLE_FILE_MAX_BYTES; return (