diff --git a/.github/fork-manifest.txt b/.github/fork-manifest.txt index d1559ee5b916d..098566b4a47ca 100644 --- a/.github/fork-manifest.txt +++ b/.github/fork-manifest.txt @@ -337,6 +337,8 @@ apps/mobile/src/native/NativeScrollViewMarker.tsx export function renderNativeSc # Fork: legacy iPad navigation preserves sidebar destinations and native pop state. apps/mobile/src/native/createV5StackNavigator.ios.tsx Fork: apps/mobile/src/native/createV5StackNavigator.ios.tsx deviceType === "pad", +apps/mobile/src/native/createV5StackNavigator.ios.test.tsx Fork: +apps/mobile/src/native/createV5StackNavigator.ios.test.tsx vi.mock("./native-layout-metrics" # Fork: preserve iPad Settings detail projection and native Back reconciliation. apps/mobile/src/native/workspace-stack-projection.test.ts Fork: @@ -745,7 +747,7 @@ packages/source-control-azure-devops/src/server/AzureDevOpsCli.test.ts Effect.pr # Fork: Use the project account credential for forge requests instead of the machine login; preserve account-scoped authorization and commit identity. packages/source-control-azure-devops/src/server/AzureDevOpsCli.ts Fork: -packages/source-control-azure-devops/src/server/AzureDevOpsCli.ts Effect.flatMap(Effect.service(SourceControlHost.AccountCredential), (account) => +packages/source-control-azure-devops/src/server/AzureDevOpsCli.ts const account = yield* Effect.service(SourceControlHost.AccountCredential); # Fork: Use the project account credential for forge requests instead of the machine login; preserve account-scoped authorization and commit identity. packages/source-control-bitbucket/src/server/BitbucketApi.test.ts Fork: diff --git a/.github/scripts/pr-limits.cjs b/.github/scripts/pr-limits.cjs index 77c09785e4104..f3eb528a08042 100644 --- a/.github/scripts/pr-limits.cjs +++ b/.github/scripts/pr-limits.cjs @@ -100,14 +100,22 @@ function parseExemptions(text) { } // VOUCHED.td: one `github:login` per line, `#` comments, and `-github:login reason` to denounce. -function vouchedLogins(text) { - const logins = new Set(); +// A login listed both ways is an error: pr-vouch.yml's action reads its first line and +// issue-vouch.yml its last, so no answer here would agree with both labels. +function parseVouchList(text) { + const vouched = new Set(); + const denounced = new Set(); for (const raw of text.split("\n")) { - const entry = raw.trim().split(/\s+/)[0]; - if (entry.startsWith("github:")) logins.add(entry.slice("github:".length).toLowerCase()); + const match = /^(-?)github:(\S+)/.exec(raw.trim()); + if (!match) continue; + const login = match[2].toLowerCase(); + const [listed, other] = match[1] ? [denounced, vouched] : [vouched, denounced]; + if (other.has(login)) + throw new Error(`${VOUCHED_PATH}: ${login} is both vouched and denounced`); + listed.add(login); } - if (logins.size === 0) throw new Error(`${VOUCHED_PATH}: no entries`); - return logins; + if (vouched.size === 0) throw new Error(`${VOUCHED_PATH}: no entries`); + return { vouched, denounced }; } function labeledAt(pr, name) { @@ -278,7 +286,8 @@ async function enforceOpenLimit({ github, context, core, dryRun }) { // lookups as "not vouched". Every read below throws on failure, and a throw closes nothing. if (pr.user.type === "Bot") return; if (parseExemptions(fs.readFileSync(EXEMPTIONS_PATH, "utf8")).has(author.toLowerCase())) return; - if (vouchedLogins(fs.readFileSync(VOUCHED_PATH, "utf8")).has(author.toLowerCase())) return; + const { vouched, denounced } = parseVouchList(fs.readFileSync(VOUCHED_PATH, "utf8")); + if (vouched.has(author.toLowerCase())) return; const { data: access } = await github.rest.repos.getCollaboratorPermissionLevel({ ...context.repo, username: author, @@ -289,6 +298,21 @@ async function enforceOpenLimit({ github, context, core, dryRun }) { const reopened = context.payload.action === "reopened"; if (reopened && context.payload.sender.login.toLowerCase() !== author.toLowerCase()) return; + if (denounced.has(author.toLowerCase())) { + if (dryRun) { + core.notice(`Dry run. PR #${pr.number} would close: ${author} is denounced.`); + return; + } + await closeWithComment( + github, + context, + pr.number, + `Closing: ${author} is banned from contributing here, so this PR will not be reviewed. See [bans](${guideLink(context, "bans")}).`, + ); + core.info(`PR #${pr.number}: closed, ${author} is denounced.`); + return; + } + const q = `repo:${context.repo.owner}/${context.repo.repo} is:pr is:open author:${author} sort:created-asc`; const { search } = await read(github, AUTHOR_OPEN_PRS_QUERY, { q }); // A new PR counts only the older ones, so two opened together cannot close each other. diff --git a/.github/workflows/issue-vouch.yml b/.github/workflows/issue-vouch.yml index bbb6e95c9ec60..d65882593c649 100644 --- a/.github/workflows/issue-vouch.yml +++ b/.github/workflows/issue-vouch.yml @@ -40,9 +40,14 @@ jobs: ref: context.payload.repository.default_branch, }); const statusByUser = new Map(); + const listedBothWays = new Set(); for (const raw of Buffer.from(file.content, "base64").toString("utf8").split("\n")) { const match = /^(-?)github:(\S+)/.exec(raw.trim()); - if (match) statusByUser.set(match[2].toLowerCase(), match[1] ? "denounced" : "vouched"); + if (!match) continue; + const login = match[2].toLowerCase(); + const status = match[1] ? "denounced" : "vouched"; + if (statusByUser.has(login) && statusByUser.get(login) !== status) listedBothWays.add(login); + statusByUser.set(login, status); } const issues = @@ -74,4 +79,32 @@ jobs: await github.rest.issues.addLabels({ owner, repo, issue_number: issue.number, labels: [next] }); core.info(`Issue #${issue.number} (${issue.user.login}): ${status} -> ${next}`); } + + // Only a newly opened issue is closed. One filed before its author was denounced + // may already have a fix on the way, so the sweep leaves those to maintainers. + // Someone else reopening it is a maintainer keeping it open. + const reopenedByOther = + context.payload.action === "reopened" && + context.payload.sender.login.toLowerCase() !== issue.user.login.toLowerCase(); + // The PR rules refuse a list that names a login both ways, so nothing closes for one here. + const trusted = + issue.user.type === "Bot" || + ["OWNER", "MEMBER", "COLLABORATOR"].includes(issue.author_association) || + listedBothWays.has(issue.user.login.toLowerCase()); + if (status === "denounced" && context.eventName === "issues" && !reopenedByOther && !trusted) { + await github.rest.issues.createComment({ + owner, + repo, + issue_number: issue.number, + body: `Closing: ${issue.user.login} is banned from contributing here, so this issue will not be triaged. See [bans](https://github.com/${owner}/${repo}/blob/main/CONTRIBUTING.md#bans).`, + }); + await github.rest.issues.update({ + owner, + repo, + issue_number: issue.number, + state: "closed", + state_reason: "not_planned", + }); + core.info(`Issue #${issue.number}: closed, ${issue.user.login} is denounced.`); + } } diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 63e71dc1e250c..0c886bd493389 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -115,6 +115,9 @@ the open PR limit. - If you make our lives harder as reviewers and maintainers, you will be banned. Examples are repeated pings, a fight over a closure, and a second account to get around a limit. +A banned account is denounced in [.github/VOUCHED.td](.github/VOUCHED.td). Any issue or PR it opens +is closed when it is opened. + ## When your PR is closed diff --git a/apps/desktop/src/app/CodexAuthCallback.test.ts b/apps/desktop/src/app/CodexAuthCallback.test.ts index 6d8a905885aca..e8f1ca0b80bd8 100644 --- a/apps/desktop/src/app/CodexAuthCallback.test.ts +++ b/apps/desktop/src/app/CodexAuthCallback.test.ts @@ -37,18 +37,27 @@ function callback(authorizationUrl: string) { } describe("desktop Codex callback helper", () => { - it("binds before opening sign-in, ignores a foreign response, and returns only the code callback", async () => { + it("binds before opening sign-in, ignores a foreign response, and returns only the code callback", async ({ + onTestFinished, + }) => { const authorizationUrl = request(await freePort()); const expected = callback(authorizationUrl); - const received = await receiveCodexAuthCallback(authorizationUrl, async () => { + const opening = Promise.withResolvers(); + onTestFinished(() => opening.resolve(true)); + const checkResponse = async () => { const invalid = new URL(expected); invalid.searchParams.set("state", "foreign"); expect((await fetch(invalid)).status).toBe(400); const response = await fetch(expected); expect(response.headers.get("cache-control")).toBe("no-store"); expect(await response.text()).not.toContain("test-code"); - return true; + }; + let responseChecked!: Promise; + const received = await receiveCodexAuthCallback(authorizationUrl, () => { + responseChecked = checkResponse(); + return responseChecked.then(() => opening.promise); }); + await responseChecked; expect(received).toBe(expected); }); it("returns hosted web to the exact instance and environment without putting the code in its query", async () => { @@ -61,34 +70,52 @@ describe("desktop Codex callback helper", () => { instanceId: ProviderInstanceId.make("work"), flowId: "flow-one", }; + const openBrowser = async () => { + const response = await fetch(expected, { redirect: "manual" }); + expect(response.status).toBe(303); + const delivery = response.headers.get("location")!; + expect(new URL(delivery).searchParams.has("code")).toBe(false); + expect(readCodexAuthDelivery(delivery)?.callbackUrl).toBe(expected); + expect(readCodexAuthDelivery(delivery)?.returnUrl).toBe(input.returnUrl); + return true; + }; + let opening!: Promise; await receiveCodexAuthCallback( authorizationUrl, - async () => { - const response = await fetch(expected, { redirect: "manual" }); - expect(response.status).toBe(303); - const delivery = response.headers.get("location")!; - expect(new URL(delivery).searchParams.has("code")).toBe(false); - expect(readCodexAuthDelivery(delivery)?.callbackUrl).toBe(expected); - expect(readCodexAuthDelivery(delivery)?.returnUrl).toBe(input.returnUrl); - return true; - }, + () => (opening = openBrowser()), (url) => codexAuthDeliveryUrl(input, url), ); + await opening; }); - it("cancels and releases its listener so exact-port reauthorization can run again", async () => { - const authorizationUrl = request(await freePort()); - await expect( - receiveCodexAuthCallback(authorizationUrl, async () => { - cancelCodexAuthCallback(authorizationUrl); - return true; - }), - ).rejects.toThrow("cancelled"); - expect( - await receiveCodexAuthCallback(authorizationUrl, async () => { - await fetch(callback(authorizationUrl)); - return true; - }), - ).toBe(callback(authorizationUrl)); + it("cancels and releases its listener so exact-port reauthorization can run again", async ({ + onTestFinished, + signal, + }) => { + for (const external of [false, true]) { + const authorizationUrl = request(await freePort()); + const abort = new AbortController(); + const opening = Promise.withResolvers(); + onTestFinished(() => opening.resolve(true)); + await expect( + receiveCodexAuthCallback( + authorizationUrl, + async () => { + if (external) abort.abort(); + else cancelCodexAuthCallback(authorizationUrl); + return opening.promise; + }, + undefined, + abort.signal, + ), + ).rejects.toThrow("cancelled"); + if (signal.aborted) return; + expect( + await receiveCodexAuthCallback(authorizationUrl, async () => { + await fetch(callback(authorizationUrl)); + return true; + }), + ).toBe(callback(authorizationUrl)); + } }); it("allows two accounts to complete independently", async () => { const a = request(await freePort(), "a".repeat(43)); diff --git a/apps/desktop/src/app/DesktopApp.ts b/apps/desktop/src/app/DesktopApp.ts index 0e7f949bc042c..f69bf965d0058 100644 --- a/apps/desktop/src/app/DesktopApp.ts +++ b/apps/desktop/src/app/DesktopApp.ts @@ -308,11 +308,9 @@ const bootstrap = Effect.fn("desktop.bootstrap")(function* (backendStarted: bool } const primaryBackend = yield* (yield* DesktopBackendPool.DesktopBackendPool).primary; - // WSL-only fallback can change the primary endpoint before readiness. const config = yield* primaryBackend.currentConfig; if ( !environment.isDevelopment && - !settings.wslOnly && Option.isSome(config) && Option.isNone(config.value.preflightFailure) ) { diff --git a/apps/desktop/src/app/DesktopClerk.ts b/apps/desktop/src/app/DesktopClerk.ts index 206a143128a8e..207aa17739e59 100644 --- a/apps/desktop/src/app/DesktopClerk.ts +++ b/apps/desktop/src/app/DesktopClerk.ts @@ -178,11 +178,12 @@ export const make = Effect.gen(function* () { Effect.gen(function* () { yield* electronApp.whenReady; yield* Effect.tryPromise({ - try: () => + try: (signal) => receiveCodexAuthCallback( request.authorizationUrl, (url) => runPromise(shell.openExternal(url)), (callbackUrl) => codexAuthDeliveryUrl(request, callbackUrl), + signal, ), catch: () => new CodexAuthCallbackError({ diff --git a/apps/desktop/src/app/DesktopConnectionCatalogStore.test.ts b/apps/desktop/src/app/DesktopConnectionCatalogStore.test.ts index 5dfde9103f152..b58b583fd6cd5 100644 --- a/apps/desktop/src/app/DesktopConnectionCatalogStore.test.ts +++ b/apps/desktop/src/app/DesktopConnectionCatalogStore.test.ts @@ -300,4 +300,66 @@ describe("DesktopConnectionCatalogStore", () => { assert.deepStrictEqual(yield* store.get, Option.some('{"schemaVersion":1,"targets":[]}')); }).pipe(Effect.provide(NodeServices.layer)), ); + + const emptyCatalog = + '{"schemaVersion":1,"targets":[],"profiles":[],"credentials":[],"remoteDpopTokens":[],"disabledEnvironmentIds":[]}'; + const savedFilesHolding = Effect.fn(function* (stateDir: string, contents: string) { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const kept: string[] = []; + for (const name of yield* fileSystem.readDirectory(stateDir)) { + const file = path.join(stateDir, name); + if ((yield* fileSystem.stat(file)).type !== "File") continue; + if ((yield* fileSystem.readFileString(file)) === contents) kept.push(name); + } + return kept; + }); + + it.effect("keeps a catalog this build cannot decode when the next save replaces it", () => + withStore( + Effect.gen(function* () { + const path = yield* Path.Path; + const environment = yield* DesktopEnvironment.DesktopEnvironment; + const fileSystem = yield* FileSystem.FileSystem; + const store = yield* DesktopConnectionCatalogStore.DesktopConnectionCatalogStore; + const catalogPath = path.join(environment.stateDir, "connection-catalog.json"); + assert.isTrue( + yield* store.set( + '{"schemaVersion":1,"targets":[{"_tag":"FutureConnectionTarget","environmentId":"remote"}],"profiles":[],"credentials":[],"remoteDpopTokens":[],"disabledEnvironmentIds":[]}', + ), + ); + const original = yield* fileSystem.readFileString(catalogPath); + + yield* store.get; + assert.isTrue(yield* store.set(emptyCatalog)); + + assert.isNotEmpty(yield* savedFilesHolding(environment.stateDir, original)); + }), + ), + ); + + it.effect("keeps a catalog that no longer decrypts when the next save replaces it", () => + Effect.gen(function* () { + const path = yield* Path.Path; + const fileSystem = yield* FileSystem.FileSystem; + const baseDir = yield* fileSystem.makeTempDirectoryScoped({ + prefix: "t3-desktop-connection-catalog-test-", + }); + const failDecrypt = yield* Ref.make(false); + const store = yield* DesktopConnectionCatalogStore.DesktopConnectionCatalogStore.pipe( + Effect.provide(layerFor(baseDir, true, failDecrypt)), + ); + const stateDir = path.join(baseDir, "userdata"); + assert.isTrue(yield* store.set('{"schemaVersion":1,"targets":[]}')); + const original = yield* fileSystem.readFileString( + path.join(stateDir, "connection-catalog.json"), + ); + + yield* Ref.set(failDecrypt, true); + yield* store.get.pipe(Effect.flip); + assert.isTrue(yield* store.set(emptyCatalog)); + + assert.isNotEmpty(yield* savedFilesHolding(stateDir, original)); + }).pipe(Effect.provide(NodeServices.layer), Effect.scoped), + ); }); diff --git a/apps/desktop/src/app/DesktopConnectionCatalogStore.ts b/apps/desktop/src/app/DesktopConnectionCatalogStore.ts index 3240df3636e38..5db49e5f9504c 100644 --- a/apps/desktop/src/app/DesktopConnectionCatalogStore.ts +++ b/apps/desktop/src/app/DesktopConnectionCatalogStore.ts @@ -15,6 +15,7 @@ import { fromLenientJson } from "@t3tools/shared/schemaJson"; import * as Context from "effect/Context"; import * as SecureRandom from "effect/crypto/SecureRandom"; import * as Effect from "effect/Effect"; +import * as Clock from "effect/Clock"; import * as Base64 from "effect/encoding/Base64"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; @@ -45,6 +46,9 @@ const RuntimeConnectionCatalogDocumentJson = Schema.fromJsonString( const encodeRuntimeConnectionCatalogDocumentJson = Schema.encodeEffect( RuntimeConnectionCatalogDocumentJson, ); +const decodeRuntimeConnectionCatalogDocumentJson = Schema.decodeEffect( + RuntimeConnectionCatalogDocumentJson, +); const DesktopConnectionCatalogStoreWriteOperation = Schema.Literals([ "create-temporary-file-name", @@ -52,6 +56,7 @@ const DesktopConnectionCatalogStoreWriteOperation = Schema.Literals([ "create-directory", "write-temporary-file", "replace-catalog-file", + "keep-unreadable-catalog", ]); const DesktopConnectionCatalogStoreMigrationOperation = Schema.Literals([ @@ -395,6 +400,57 @@ export const make = Effect.gen(function* () { ), ); + const decryptDocument = (document: EncryptedConnectionCatalogDocument) => + decodeSecretBytes(catalogPath, document.encryptedCatalog).pipe( + Effect.flatMap((encryptedCatalog) => + safeStorage.decryptString(encryptedCatalog).pipe( + Effect.mapError( + (cause) => + new DesktopConnectionCatalogStoreProtectionError({ + operation: "decrypt-catalog", + catalogPath, + cause, + }), + ), + ), + ), + ); + + // A catalog this build cannot decrypt or decode may still hold another build's saved + // environments and credentials, so a save copies it aside instead of destroying it. + const keepUnreadableCatalog = Effect.gen(function* () { + const readable = yield* readDocument(fileSystem, catalogPath).pipe( + Effect.flatMap( + Option.match({ + onNone: () => Effect.succeed(true), + onSome: (document) => + decryptDocument(document).pipe( + Effect.flatMap(decodeRuntimeConnectionCatalogDocumentJson), + Effect.as(true), + ), + }), + ), + Effect.orElseSucceed(() => false), + ); + if (readable) { + return; + } + const keptPath = `${catalogPath}.unreadable-${yield* Clock.currentTimeMillis}`; + yield* fileSystem.copyFile(catalogPath, keptPath).pipe( + Effect.mapError( + (cause) => + new DesktopConnectionCatalogStoreWriteError({ + operation: "keep-unreadable-catalog", + path: keptPath, + cause, + }), + ), + ); + yield* Effect.logWarning("Kept an unreadable connection catalog before replacing it.", { + keptPath, + }); + }); + const writeCatalog = Effect.fn("desktop.connectionCatalogStore.writeCatalog")(function* ( catalog: string, ) { @@ -470,26 +526,13 @@ export const make = Effect.gen(function* () { if (!(yield* encryptionAvailable)) { return Option.none(); } - const decrypted = yield* decodeSecretBytes(catalogPath, document.value.encryptedCatalog).pipe( - Effect.flatMap((encryptedCatalog) => - safeStorage.decryptString(encryptedCatalog).pipe( - Effect.mapError( - (cause) => - new DesktopConnectionCatalogStoreProtectionError({ - operation: "decrypt-catalog", - catalogPath, - cause, - }), - ), - ), - ), - ); - return Option.some(decrypted); + return Option.some(yield* decryptDocument(document.value)); }).pipe(Effect.withSpan("desktop.connectionCatalogStore.get")), set: Effect.fn("desktop.connectionCatalogStore.set")(function* (catalog) { if (!(yield* encryptionAvailable)) { return false; } + yield* keepUnreadableCatalog; yield* writeCatalog(catalog); return true; }), diff --git a/apps/desktop/src/app/DesktopStartupRecovery.ts b/apps/desktop/src/app/DesktopStartupRecovery.ts index 3a9b75b1e6c08..00497d7c0df53 100644 --- a/apps/desktop/src/app/DesktopStartupRecovery.ts +++ b/apps/desktop/src/app/DesktopStartupRecovery.ts @@ -219,7 +219,6 @@ export const listen: Effect.Effect< const download = yield* updates.download; if (!download.completed) return; } - // Waits for a running background check; plain install would refuse. yield* updates.installPrepared(current.version); return; } diff --git a/apps/desktop/src/app/stdioErrors.test.ts b/apps/desktop/src/app/stdioErrors.test.ts new file mode 100644 index 0000000000000..ccfa3ed951178 --- /dev/null +++ b/apps/desktop/src/app/stdioErrors.test.ts @@ -0,0 +1,22 @@ +import * as NodeOS from "node:os"; +import { assert, it } from "vite-plus/test"; + +import { handleStdioError } from "./stdioErrors.ts"; + +const errorWithCode = (code: string, errno?: number) => + Object.assign(new Error(code), { code, errno }); +const EDQUOT_ERRNO = NodeOS.constants.errno.EDQUOT; + +it.each(["EPIPE", "ENOSPC"])("ignores %s on console output", (code) => { + assert.doesNotThrow(() => handleStdioError(errorWithCode(code))); +}); + +// Node reports EDQUOT as UNKNOWN because libuv has no name for it. +it.skipIf(EDQUOT_ERRNO === undefined)("ignores EDQUOT on console output", () => { + assert.doesNotThrow(() => handleStdioError(errorWithCode("UNKNOWN", -(EDQUOT_ERRNO ?? 0)))); +}); + +it("rethrows other console output errors", () => { + assert.throws(() => handleStdioError(errorWithCode("EIO")), "EIO"); + assert.throws(() => handleStdioError(errorWithCode("UNKNOWN", -5)), "UNKNOWN"); +}); diff --git a/apps/desktop/src/app/stdioErrors.ts b/apps/desktop/src/app/stdioErrors.ts new file mode 100644 index 0000000000000..b23c0e89e79a8 --- /dev/null +++ b/apps/desktop/src/app/stdioErrors.ts @@ -0,0 +1,13 @@ +import * as NodeOS from "node:os"; + +// libuv has no name for EDQUOT, so Node reports it as code "UNKNOWN" with the raw errno. +const EDQUOT_ERRNO = NodeOS.constants.errno.EDQUOT; + +// Redirected console output can fail when its log volume is full. +export function handleStdioError(error: unknown): void { + if (typeof error === "object" && error !== null) { + if ("code" in error && (error.code === "EPIPE" || error.code === "ENOSPC")) return; + if (EDQUOT_ERRNO !== undefined && "errno" in error && error.errno === -EDQUOT_ERRNO) return; + } + throw error; +} diff --git a/apps/desktop/src/backend/DesktopBackendConfiguration.test.ts b/apps/desktop/src/backend/DesktopBackendConfiguration.test.ts index 70f7ed87d2214..ab57d5db32372 100644 --- a/apps/desktop/src/backend/DesktopBackendConfiguration.test.ts +++ b/apps/desktop/src/backend/DesktopBackendConfiguration.test.ts @@ -385,7 +385,7 @@ describe("DesktopBackendConfiguration", () => { runtimeId: string; sha256: string; }> = []; - const observedProbeRoots: string[] = []; + let probeCount = 0; let legacyCleanupCount = 0; const linuxAppRoot = "/home/test/.t3/wsl-runtime/1.2.3-x64"; const resolvedPath = "/home/test/.local/bin:/usr/bin:/bin"; @@ -406,8 +406,8 @@ describe("DesktopBackendConfiguration", () => { }); return { ok: true, linuxAppRoot }; }, - probeRuntime: (_distro, root) => { - observedProbeRoots.push(root); + probeRuntime: () => { + probeCount += 1; return { ok: true, resolvedPath }; }, ensureNodePty: () => { @@ -428,7 +428,7 @@ describe("DesktopBackendConfiguration", () => { sha256: archiveHash, }, ]); - assert.deepEqual(observedProbeRoots, [linuxAppRoot]); + assert.equal(probeCount, 1); assert.equal( config.entryPath, path.join(baseDir, "server.asar/apps/server/dist/bin.mjs"), @@ -523,10 +523,10 @@ describe("DesktopBackendConfiguration", () => { ); }); - it.effect("resolveWsl retires a staged runtime whose executable does not start", () => { + it.effect("resolveWsl retires a staged runtime whose launch probe fails", () => { const archiveHash = "c".repeat(64); const stagedAppRoot = `/home/test/.t3/wsl-runtime/sha256-${archiveHash}`; - const observedProbeRoots: string[] = []; + let probeCount = 0; const observedNodePtyRoots: string[] = []; const invalidatedRuntimeIds: string[] = []; return withPackagedWslHarness( @@ -538,9 +538,9 @@ describe("DesktopBackendConfiguration", () => { Effect.sync(() => { invalidatedRuntimeIds.push(runtimeId); }), - probeRuntime: (_distro, root) => { - observedProbeRoots.push(root); - return { ok: false, reason: `${root}/t3 --version failed (exit 127)` }; + probeRuntime: () => { + probeCount += 1; + return { ok: false, reason: "WSL login-shell PATH could not be resolved" }; }, ensureNodePty: (_distro, root) => { observedNodePtyRoots.push(root); @@ -553,7 +553,7 @@ describe("DesktopBackendConfiguration", () => { const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration; const config = yield* configuration.resolveWsl({ port: 5000, distro: "Ubuntu" }); - assert.deepEqual(observedProbeRoots, [stagedAppRoot]); + assert.equal(probeCount, 1); assert.deepEqual(observedNodePtyRoots, [mountedAppRoot]); assert.include(config.args, "/usr/bin/node"); assert.include(config.args, `${mountedAppRoot}/apps/server/dist/bin.mjs`); @@ -578,7 +578,7 @@ describe("DesktopBackendConfiguration", () => { Effect.sync(() => { invalidatedRuntimeIds.push(runtimeId); }), - probeRuntime: () => ({ ok: false, reason: "t3 --version failed (exit 1)" }), + probeRuntime: () => ({ ok: false, reason: "WSL login-shell PATH could not be resolved" }), ensureNodePty: () => ({ ok: false, reason: "WSL backend preflight timed out while probing for Node.js.", diff --git a/apps/desktop/src/backend/DesktopBackendConfiguration.ts b/apps/desktop/src/backend/DesktopBackendConfiguration.ts index 5f499e6f4ee37..82f0a30a40cd9 100644 --- a/apps/desktop/src/backend/DesktopBackendConfiguration.ts +++ b/apps/desktop/src/backend/DesktopBackendConfiguration.ts @@ -424,7 +424,7 @@ const runWslPreflight = Effect.fn("desktop.backendConfiguration.wslPreflight")(f const runtime = yield* wslEnv.prepareRuntime(runningDistro, input.runtimeArchive); if (runtime.ok) { // Staged runtimes bundle Node and node-pty; provider PATH discovery must not require them. - const stagedProbe = yield* wslEnv.probeRuntime(runningDistro, runtime.linuxAppRoot); + const stagedProbe = yield* wslEnv.probeRuntime(runningDistro); if (stagedProbe.ok) { yield* wslServerTree.cleanupLegacy; return { @@ -437,7 +437,7 @@ const runWslPreflight = Effect.fn("desktop.backendConfiguration.wslPreflight")(f } as const; } yield* Effect.logWarning( - "The staged WSL runtime did not start; retrying from the mounted server tree.", + "The staged WSL runtime probe failed; retrying from the mounted server tree.", { reason: stagedProbe.reason }, ); stagedFailure = { runtimeId: input.runtimeArchive.runtimeId, reason: stagedProbe.reason }; diff --git a/apps/desktop/src/backend/DesktopBackendManager.test.ts b/apps/desktop/src/backend/DesktopBackendManager.test.ts index 06a1f768f4bd4..ff5b7b8c08e4b 100644 --- a/apps/desktop/src/backend/DesktopBackendManager.test.ts +++ b/apps/desktop/src/backend/DesktopBackendManager.test.ts @@ -11,6 +11,7 @@ import { DATABASE_MIGRATIONS_STARTED_MESSAGE, } from "@t3tools/shared/databaseMigrationLog"; import { assert, describe, it } from "@effect/vitest"; +import * as Clock from "effect/Clock"; import * as Deferred from "effect/Deferred"; import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; @@ -239,6 +240,43 @@ describe("DesktopBackendManager", () => { }), ); + it.effect("notices a WSL backend soon after it listens despite slow refused connects", () => + Effect.gen(function* () { + const ready = yield* Deferred.make(); + const exit = yield* Deferred.make(); + const instance = yield* makeTestInstance({ + config: { ...baseConfig, httpBaseUrl: new URL("http://172.20.84.98:3773") }, + onReady: Deferred.succeed(ready, undefined).pipe(Effect.asVoid), + spawnerLayer: Layer.succeed( + ChildProcessSpawner.ChildProcessSpawner, + ChildProcessSpawner.make(() => + Effect.acquireRelease( + Effect.succeed(makeProcess({ exitCode: Deferred.await(exit) })), + () => Deferred.succeed(exit, ChildProcessSpawner.ExitCode(0)), + ), + ), + ), + // Before the server listens at 300 ms, each refused connect takes a second to fail. + httpClientLayer: layerHttpClient((request) => + Clock.currentTimeMillis.pipe( + Effect.flatMap((now) => + now < 300 + ? Effect.sleep(Duration.seconds(1)).pipe( + Effect.as(responseForRequest(request, 503)), + ) + : Effect.succeed(responseForRequest(request, 200)), + ), + ), + ), + }); + yield* instance.start; + yield* TestClock.adjust(Duration.millis(600)); + + assert.isTrue(yield* Deferred.isDone(ready)); + yield* instance.stop(); + }), + ); + it.effect("spawns the backend with fd3 bootstrap and fd4 telemetry", () => Effect.gen(function* () { let spawnedCommand: ChildProcess.Command | undefined; diff --git a/apps/desktop/src/backend/DesktopBackendManager.ts b/apps/desktop/src/backend/DesktopBackendManager.ts index 4acaa8a3c18b8..8b4f154b5d4de 100644 --- a/apps/desktop/src/backend/DesktopBackendManager.ts +++ b/apps/desktop/src/backend/DesktopBackendManager.ts @@ -344,8 +344,26 @@ const closeRun = ( ); }; +const LOOPBACK_HOSTNAMES = new Set(["127.0.0.1", "localhost", "[::1]"]); +// Windows takes about a second to fail a refused connection to a non-loopback address such as a +// WSL distro IP, so one sequential poller notices readiness up to a second late. +const STAGGERED_READINESS_POLLERS = 4; +const READINESS_POLLER_STAGGER = Duration.millis(250); + const waitForHttpReady = ( options: BackendProcessContext & { readonly timeout: Duration.Duration }, +): Effect.Effect => { + const poll = pollHttpReady(options); + if (LOOPBACK_HOSTNAMES.has(options.httpBaseUrl.hostname)) return poll; + return Effect.raceAll( + Array.from({ length: STAGGERED_READINESS_POLLERS }, (_, index) => + Effect.sleep(Duration.times(READINESS_POLLER_STAGGER, index)).pipe(Effect.andThen(poll)), + ), + ); +}; + +const pollHttpReady = ( + options: BackendProcessContext & { readonly timeout: Duration.Duration }, ): Effect.Effect => { const readinessUrl = new URL(BACKEND_READINESS_PATH, options.httpBaseUrl); return waitForHttpReadyShared({ diff --git a/apps/desktop/src/backend/DesktopBackendPool.test.ts b/apps/desktop/src/backend/DesktopBackendPool.test.ts index 8e9f4320c08c7..005a1cde1224a 100644 --- a/apps/desktop/src/backend/DesktopBackendPool.test.ts +++ b/apps/desktop/src/backend/DesktopBackendPool.test.ts @@ -112,6 +112,10 @@ function layerPool( labelRef: Ref.Ref, options: { readonly config?: DesktopBackendStartConfig; + readonly resolvePrimary?: Effect.Effect; + readonly onMainContentsCommand?: ( + command: DesktopWindow.MainWindowContentsCommand, + ) => Effect.Effect; readonly fileSystem?: Partial; readonly spawner?: ChildProcessSpawner.ChildProcessSpawner["Service"]; readonly httpClient?: HttpClient.HttpClient; @@ -180,9 +184,11 @@ function layerPool( }), DesktopBrowserHost.layer, Layer.succeed(DesktopBackendConfiguration.DesktopBackendConfiguration, { - resolvePrimary: options.config - ? Effect.succeed(options.config) - : Effect.die("unexpected primary config resolve"), + resolvePrimary: + options.resolvePrimary ?? + (options.config + ? Effect.succeed(options.config) + : Effect.die("unexpected primary config resolve")), resolvePrimaryLabel: Ref.get(labelRef), resolveWsl: () => Effect.die("unexpected WSL config resolve"), currentBootstrapToken: Effect.die("unexpected bootstrap token read"), @@ -190,7 +196,12 @@ function layerPool( DesktopAppSettings.layerTest(), DesktopState.layer, DesktopWslEnvironment.layerTest(), - ElectronDialog.layer, + Layer.succeed(ElectronDialog.ElectronDialog, { + pickFolder: () => Effect.die("unexpected folder picker"), + pickFiles: () => Effect.die("unexpected file picker"), + showMessageBox: () => Effect.die("unexpected message box"), + showErrorBox: () => Effect.void, + } satisfies ElectronDialog.ElectronDialog["Service"]), ElectronApp.layer, Layer.succeed(DesktopWindow.DesktopWindow, { createMain: Effect.die("unexpected window create"), @@ -206,7 +217,8 @@ function layerPool( dispatchMenuAction: () => Effect.die("unexpected menu action"), dispatchSnapShotEvent: () => Effect.void, zoomMain: () => Effect.die("unexpected zoom"), - runMainContentsCommand: () => Effect.die("unexpected main contents command"), + runMainContentsCommand: + options.onMainContentsCommand ?? (() => Effect.die("unexpected main contents command")), syncAppearance: Effect.void, } satisfies DesktopWindow.DesktopWindow["Service"]), ), @@ -478,4 +490,70 @@ describe("DesktopBackendPool", () => { assert.equal(Option.isNone(yield* pool.get(id)), true); }).pipe(Effect.provide(layerPool(Ref.makeUnsafe("Windows")))), ); + + it.effect("reloads a main window that read the WSL endpoint before the Windows fallback", () => + Effect.gen(function* () { + const events: string[] = []; + let resolves = 0; + const resolvePrimary = Effect.sync(() => + resolves++ === 0 + ? { + ...runningConfig, + preflightFailure: Option.some({ + reason: "WSL still unavailable", + fatal: false, + retryLimit: 1, + }), + } + : runningConfig, + ); + const ready = yield* Deferred.make(); + yield* Effect.gen(function* () { + const primary = yield* (yield* DesktopBackendPool.DesktopBackendPool).primary; + yield* primary.start; + yield* TestClock.adjust(Duration.seconds(1)); + yield* Deferred.await(ready); + + assert.deepEqual(events, ["reload", "ready"]); + }).pipe( + Effect.provide( + layerPool(Ref.makeUnsafe("WSL"), { + resolvePrimary, + spawner: ChildProcessSpawner.make(() => + Effect.gen(function* () { + const exited = yield* Deferred.make(); + const exit = Deferred.succeed(exited, ChildProcessSpawner.ExitCode(0)).pipe( + Effect.asVoid, + ); + yield* Effect.addFinalizer(() => exit); + return ChildProcessSpawner.makeHandle({ + pid: ChildProcessSpawner.ProcessId(123), + stdout: Stream.empty, + stderr: Stream.empty, + all: Stream.empty, + exitCode: Deferred.await(exited), + isRunning: Effect.succeed(true), + kill: () => exit, + stdin: Sink.drain, + getInputFd: () => Sink.drain, + getOutputFd: () => Stream.empty, + unref: Effect.succeed(Effect.void), + }); + }), + ), + httpClient: HttpClient.make((request) => + Effect.succeed( + HttpClientResponse.fromWeb(request, new Response(null, { status: 200 })), + ), + ), + onMainContentsCommand: (command) => Effect.sync(() => void events.push(command)), + onReady: Effect.sync(() => void events.push("ready")).pipe( + Effect.andThen(Deferred.succeed(ready, undefined)), + Effect.asVoid, + ), + }), + ), + ); + }), + ); }); diff --git a/apps/desktop/src/backend/DesktopBackendPool.ts b/apps/desktop/src/backend/DesktopBackendPool.ts index d69e1ce1427fe..62c811996b1a0 100644 --- a/apps/desktop/src/backend/DesktopBackendPool.ts +++ b/apps/desktop/src/backend/DesktopBackendPool.ts @@ -167,10 +167,22 @@ export const layer = Layer.effect( // One wait at a time: each run's shutdown or readiness ends the previous run's wait. const updateMarkerWait = yield* FiberHandle.make(); + // The main window loads during startup and reads the primary endpoint once; a Windows fallback + // before the first readiness replaces the WSL endpoint it read. + let primaryWasReady = false; + let reloadMainOnReady = false; + const reloadStaleMain = Effect.suspend(() => { + primaryWasReady = true; + if (!reloadMainOnReady) return Effect.void; + reloadMainOnReady = false; + return desktopWindow.runMainContentsCommand("reload"); + }); + // Persist fatal Windows fallbacks; transient transport failures must preserve the user's WSL preference. const handlePrimaryPreflightFailure = Effect.fn("desktop.backendPool.primaryPreflightFailed")( function* (failure: DesktopBackendManager.PreflightFailure) { const { reason, fatal } = failure; + if (!primaryWasReady) reloadMainOnReady = true; if (!fatal) { yield* logBackendPoolWarning( "primary WSL preflight retry window exhausted; using Windows for this launch", @@ -215,6 +227,7 @@ export const layer = Layer.effect( // Absorb window errors so readiness can reset restartAttempt. onReady: (httpBaseUrl, pid) => FiberHandle.run(updateMarkerWait, releaseUpdateMarker(httpBaseUrl, pid)).pipe( + Effect.andThen(reloadStaleMain), Effect.andThen(desktopWindow.handleBackendReady(httpBaseUrl)), // Only once the main window exists, so the recovery window never closes into nothing. Effect.andThen(SubscriptionRef.set(primaryBackendStartup, { _tag: "Ready" })), diff --git a/apps/desktop/src/backend/DesktopLocalEnvironmentAuth.test.ts b/apps/desktop/src/backend/DesktopLocalEnvironmentAuth.test.ts index e82e3afea7303..c15ce3932bb80 100644 --- a/apps/desktop/src/backend/DesktopLocalEnvironmentAuth.test.ts +++ b/apps/desktop/src/backend/DesktopLocalEnvironmentAuth.test.ts @@ -1,11 +1,13 @@ import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; import { assert, describe, it } from "@effect/vitest"; +import * as Clock from "effect/Clock"; import * as Deferred from "effect/Deferred"; import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; import * as Fiber from "effect/Fiber"; import * as Layer from "effect/Layer"; import * as Ref from "effect/Ref"; +import * as Result from "effect/Result"; import * as HttpClient from "effect/http/HttpClient"; import * as HttpClientRequest from "effect/http/HttpClientRequest"; import * as HttpClientResponse from "effect/http/HttpClientResponse"; @@ -238,4 +240,34 @@ describe("DesktopLocalEnvironmentAuth", () => { assert.strictEqual(yield* Ref.get(requestCount), 1); }).pipe(Effect.provide(TestClock.layer())), ); + + it.effect("waits through a backend that stalls for 30 seconds after it is ready", () => + Effect.gen(function* () { + const requestCount = yield* Ref.make(0); + const layerHttpClient = Layer.succeed( + HttpClient.HttpClient, + HttpClient.make((request) => + Effect.gen(function* () { + yield* Ref.update(requestCount, (count) => count + 1); + const now = yield* Clock.currentTimeMillis; + return now < 30_000 ? yield* Effect.never : tokenResponse(request); + }), + ), + ); + const auth = yield* DesktopLocalEnvironmentAuth.DesktopLocalEnvironmentAuth.pipe( + Effect.provide( + DesktopLocalEnvironmentAuth.layer.pipe( + Layer.provide(Layer.mergeAll(layerPool, layerHttpClient, NodeCrypto.layerMac)), + ), + ), + ); + + const fiber = yield* auth.getBearerToken.pipe(Effect.result, Effect.forkChild); + yield* TestClock.adjust(Duration.seconds(45)); + const result = yield* Fiber.join(fiber); + + assert.isTrue(Result.isSuccess(result)); + assert.strictEqual(yield* Ref.get(requestCount), 4); + }).pipe(Effect.provide(TestClock.layer())), + ); }); diff --git a/apps/desktop/src/backend/DesktopLocalEnvironmentAuth.ts b/apps/desktop/src/backend/DesktopLocalEnvironmentAuth.ts index 6c7d24623d40f..89aa3cd2a678c 100644 --- a/apps/desktop/src/backend/DesktopLocalEnvironmentAuth.ts +++ b/apps/desktop/src/backend/DesktopLocalEnvironmentAuth.ts @@ -17,7 +17,8 @@ import * as HttpClient from "effect/http/HttpClient"; import * as DesktopBackendPool from "./DesktopBackendPool.ts"; -const BOOTSTRAP_TRANSIENT_RETRY_TIMEOUT = Duration.seconds(15); +// Each /oauth/token attempt may take 10 s, so a backend busy after ready needs several attempts. +const BOOTSTRAP_TRANSIENT_RETRY_TIMEOUT = Duration.seconds(60); const BOOTSTRAP_TRANSIENT_RETRY_INTERVAL = Duration.millis(500); const TRANSIENT_BOOTSTRAP_STATUS_CODES = new Set([502, 503, 504]); diff --git a/apps/desktop/src/electron/ElectronAudio.test.ts b/apps/desktop/src/electron/ElectronAudio.test.ts new file mode 100644 index 0000000000000..b38d7bbb3ae47 --- /dev/null +++ b/apps/desktop/src/electron/ElectronAudio.test.ts @@ -0,0 +1,60 @@ +import * as Effect from "effect/Effect"; +import * as HostProcess from "@t3tools/shared/HostProcess"; +import { app } from "electron"; +import { expect, vi } from "vite-plus/test"; +import { it } from "@effect/vitest"; +import * as Fiber from "effect/Fiber"; + +const { getAppMetrics, kill } = vi.hoisted(() => ({ getAppMetrics: vi.fn(), kill: vi.fn() })); +vi.mock("node:process", () => ({ kill })); +vi.mock("electron", async () => { + const { EventEmitter } = await import("node:events"); + return { app: Object.assign(new EventEmitter(), { getAppMetrics }) }; +}); + +import { restart } from "./ElectronAudio.ts"; + +const audio = { pid: 42, type: "Utility", serviceName: "audio.mojom.AudioService" }; + +it.effect("coalesces recovery and waits for its own audio utility to exit", () => + Effect.gen(function* () { + getAppMetrics.mockReturnValue([ + { pid: 1, type: "Utility", serviceName: "network.mojom.NetworkService" }, + { pid: 2, type: "Browser", serviceName: audio.serviceName }, + audio, + ]); + const started = Promise.withResolvers(); + kill.mockReset().mockImplementation(() => { + started.resolve(); + }); + const pending = yield* Effect.forkChild( + Effect.all([restart(), restart()], { concurrency: "unbounded" }), + ); + yield* Effect.promise(() => started.promise); + app.emit( + "child-process-gone", + {}, + { type: "Utility", serviceName: "network.mojom.NetworkService" }, + ); + expect(app.listenerCount("child-process-gone")).toBe(1); + app.emit("child-process-gone", {}, audio); + expect(yield* Fiber.join(pending)).toEqual([true, true]); + expect(kill.mock.calls).toEqual([[42, "SIGTERM"]]); + expect(app.listenerCount("child-process-gone")).toBe(0); + }).pipe(Effect.provideService(HostProcess.Platform, "linux")), +); + +it.effect.each(["linux", "darwin", "win32"] as const)( + "leaves other processes alone on %s", + (platform) => + Effect.gen(function* () { + getAppMetrics.mockReturnValue([ + { pid: 1, type: "Utility", serviceName: "network.mojom.NetworkService" }, + ]); + kill.mockReset(); + expect(yield* restart().pipe(Effect.provideService(HostProcess.Platform, platform))).toBe( + false, + ); + expect(kill).not.toHaveBeenCalled(); + }), +); diff --git a/apps/desktop/src/electron/ElectronAudio.ts b/apps/desktop/src/electron/ElectronAudio.ts new file mode 100644 index 0000000000000..6a7f290d6fd83 --- /dev/null +++ b/apps/desktop/src/electron/ElectronAudio.ts @@ -0,0 +1,62 @@ +// @effect-diagnostics globalTimers:off -- The shared native restart deadline must outlive individual IPC callers. +// @effect-diagnostics nodeBuiltinImport:off -- Only Electron's main process can restart its Chromium audio utility. +import * as NodeProcess from "node:process"; +import * as Effect from "effect/Effect"; +import * as Schema from "effect/Schema"; +import * as HostProcess from "@t3tools/shared/HostProcess"; +import { app, type Details, type Event } from "electron"; + +class AudioServiceRestartError extends Schema.TaggedError()( + "AudioServiceRestartError", + { cause: Schema.Defect() }, +) {} + +let pendingRestart: Promise | undefined; + +export const restart = Effect.fn("ElectronAudio.restart")(function* () { + if ((yield* HostProcess.Platform) !== "linux") return false; + return yield* Effect.tryPromise({ + try: () => { + if (pendingRestart) return pendingRestart; + const audio = app + .getAppMetrics() + .find( + (metric) => + metric.type === "Utility" && metric.serviceName === "audio.mojom.AudioService", + ); + if (!audio) return Promise.resolve(false); + // A failed PulseAudio context survives new getUserMedia calls; Chromium reconnects when its utility exits. + pendingRestart = new Promise((resolve, reject) => { + const cleanup = () => { + clearTimeout(timer); + app.removeListener("child-process-gone", onGone); + }; + const onGone = (_event: Event, details: Details) => { + if (details.type === "Utility" && details.serviceName === audio.serviceName) { + cleanup(); + resolve(true); + } + }; + const timer = setTimeout(() => { + cleanup(); + resolve(false); + }, 5_000); + app.on("child-process-gone", onGone); + try { + NodeProcess.kill(audio.pid, "SIGTERM"); + } catch (cause) { + cleanup(); + reject(cause); + } + }).finally(() => { + pendingRestart = undefined; + }); + return pendingRestart; + }, + catch: (cause) => new AudioServiceRestartError({ cause }), + }).pipe( + Effect.catch((cause) => + Effect.logWarning("Could not restart the audio service", cause).pipe(Effect.as(false)), + ), + ); +}); diff --git a/apps/desktop/src/ipc/DesktopIpcHandlers.ts b/apps/desktop/src/ipc/DesktopIpcHandlers.ts index 57d7101cf9ec7..60b72deb293a5 100644 --- a/apps/desktop/src/ipc/DesktopIpcHandlers.ts +++ b/apps/desktop/src/ipc/DesktopIpcHandlers.ts @@ -44,6 +44,7 @@ import { getWindowFullscreenState, openExternal, readMedia, + restartAudioService, openSystemSettings, checkSystemPermission, pasteAsText, @@ -109,6 +110,7 @@ export const installDesktopIpcHandlers = Effect.fn("desktop.ipc.installHandlers" yield* ipc.handle(getHostEnvironmentId); yield* ipc.handle(getLocalEnvironmentBearerToken); + yield* ipc.handle(restartAudioService); yield* ipc.handle(getClientSettings); yield* ipc.handle(setClientSettings); yield* ipc.handle(getConnectionCatalog); diff --git a/apps/desktop/src/ipc/channels.ts b/apps/desktop/src/ipc/channels.ts index b7bb5e579572f..9e5c7296a3a03 100644 --- a/apps/desktop/src/ipc/channels.ts +++ b/apps/desktop/src/ipc/channels.ts @@ -101,6 +101,7 @@ export const PREVIEW_SET_AUDIO_MUTED_CHANNEL = "desktop:preview-set-audio-muted" export const PREVIEW_FIND_IN_PAGE_CHANNEL = "desktop:preview-find-in-page"; export const PREVIEW_STOP_FIND_IN_PAGE_CHANNEL = "desktop:preview-stop-find-in-page"; export const PREVIEW_SET_SHOWN_CHANNEL = "desktop:preview-set-shown"; +export const PREVIEW_SET_CAPTURE_DRAWN_CHANNEL = "desktop:preview-set-capture-drawn"; export const PREVIEW_OPEN_DEVTOOLS_CHANNEL = "desktop:preview-open-devtools"; export const PREVIEW_CLEAR_COOKIES_CHANNEL = "desktop:preview-clear-cookies"; export const PREVIEW_CLEAR_CACHE_CHANNEL = "desktop:preview-clear-cache"; @@ -139,3 +140,5 @@ export const RECEIVE_PROVIDER_AUTH_CALLBACK_CHANNEL = "desktop:receive-provider- export const CANCEL_PROVIDER_AUTH_CALLBACK_CHANNEL = "desktop:cancel-provider-auth-callback"; export const TAKE_LEGACY_LOCAL_STORAGE_CHANNEL = "desktop:take-legacy-local-storage"; export const COMPLETE_LEGACY_LOCAL_STORAGE_CHANNEL = "desktop:complete-legacy-local-storage"; + +export const RESTART_AUDIO_SERVICE_CHANNEL = "desktop:restart-audio-service"; diff --git a/apps/desktop/src/ipc/methods/preview.ts b/apps/desktop/src/ipc/methods/preview.ts index cc810fee072ad..757e1e463f538 100644 --- a/apps/desktop/src/ipc/methods/preview.ts +++ b/apps/desktop/src/ipc/methods/preview.ts @@ -13,6 +13,7 @@ import { DesktopPreviewScreenshotArtifactSchema, DesktopPreviewSetAudioMutedInputSchema, DesktopPreviewSetShownInputSchema, + DesktopPreviewSetCaptureDrawnInputSchema, DesktopPreviewSetColorSchemeInputSchema, DesktopPreviewSetZoomFactorInputSchema, BrowserImportResult, @@ -234,6 +235,15 @@ const setShown = DesktopIpc.makeIpcMethod({ yield* manager.setShown(tabId, shown); }), }); +const setCaptureDrawn = DesktopIpc.makeIpcMethod({ + channel: IpcChannels.PREVIEW_SET_CAPTURE_DRAWN_CHANNEL, + payload: DesktopPreviewSetCaptureDrawnInputSchema, + result: Schema.Void, + handler: Effect.fn("desktop.ipc.preview.setCaptureDrawn")(function* ({ tabId, drawn }) { + const manager = yield* PreviewManager.PreviewManager; + yield* manager.setCaptureDrawn(tabId, drawn); + }), +}); const openDevTools = tabMethod( IpcChannels.PREVIEW_OPEN_DEVTOOLS_CHANNEL, "desktop.ipc.preview.openDevTools", @@ -488,6 +498,7 @@ export const methods = [ setZoomFactor, setAudioMuted, setShown, + setCaptureDrawn, openDevTools, clearCookies, clearCache, diff --git a/apps/desktop/src/ipc/methods/providerAuth.ts b/apps/desktop/src/ipc/methods/providerAuth.ts index fbde6a409f3f2..d7f432815d2ae 100644 --- a/apps/desktop/src/ipc/methods/providerAuth.ts +++ b/apps/desktop/src/ipc/methods/providerAuth.ts @@ -23,8 +23,13 @@ export const receiveProviderAuthCallback = DesktopIpc.makeIpcMethod({ const context = yield* Effect.context(); const runPromise = Effect.runPromiseWith(context); const callbackUrl = yield* Effect.tryPromise({ - try: () => - receiveCodexAuthCallback(authorizationUrl, (url) => runPromise(shell.openExternal(url))), + try: (signal) => + receiveCodexAuthCallback( + authorizationUrl, + (url) => runPromise(shell.openExternal(url)), + undefined, + signal, + ), catch: () => new CodexAuthCallbackError({ detail: diff --git a/apps/desktop/src/ipc/methods/window.test.ts b/apps/desktop/src/ipc/methods/window.test.ts index fbeda6d2f2dcd..de202529c4113 100644 --- a/apps/desktop/src/ipc/methods/window.test.ts +++ b/apps/desktop/src/ipc/methods/window.test.ts @@ -1,6 +1,8 @@ import { assert, describe, it } from "@effect/vitest"; import * as NodeServices from "@effect/platform-node/NodeServices"; import type { ManagedPolicyLimits } from "@t3tools/contracts"; +import { EDITORS, REMOTE_CAPABLE_EDITOR_IDS } from "@t3tools/contracts"; +import { resolveEditorCommand } from "@t3tools/shared/editor"; import * as HostProcess from "@t3tools/shared/HostProcess"; import { ManagedPolicyFileError } from "@t3tools/shared/managedPolicyFile"; import * as FileSystem from "effect/FileSystem"; @@ -517,3 +519,108 @@ it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")( assert.include(editors, "webstorm"); }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); + +describe("probeRemoteEditors PATH scan", () => { + const platform = HostProcess.Platform.defaultValue(); + const launcherName = (command: string) => (platform === "win32" ? `${command}.cmd` : command); + + const makeFakePath = Effect.fnUntraced(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-probe-path-" }); + const dirs = ["a", "b", "c", "d"].map((name) => path.join(root, name)); + for (const dir of dirs) yield* fs.makeDirectory(dir, { recursive: true }); + // Launchers in different PATH directories, deliberately not in REMOTE_CAPABLE_EDITOR_IDS order. + for (const [dir, command] of [ + [dirs[2]!, "webstorm"], + [dirs[0]!, "cursor"], + [dirs[3]!, "code"], + [dirs[1]!, "zeditor"], + ] as const) { + const file = path.join(dir, launcherName(command)); + yield* fs.writeFileString(file, "#!/bin/sh\n"); + if (platform !== "win32") yield* fs.chmod(file, 0o755); + } + return { + root, + env: { + HOME: root, + USERPROFILE: root, + PATH: dirs.join(platform === "win32" ? ";" : ":"), + PATHEXT: ".COM;.EXE;.BAT;.CMD", + }, + }; + }); + + const countingFileSystem = (counter: { stat: number; reads: Map }) => + Layer.effect( + FileSystem.FileSystem, + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + return FileSystem.FileSystem.of({ + ...fs, + stat: (target) => { + counter.stat += 1; + return fs.stat(target); + }, + readDirectory: (target, options) => { + counter.reads.set(target, (counter.reads.get(target) ?? 0) + 1); + return fs.readDirectory(target, options); + }, + }); + }), + ).pipe(Layer.provideMerge(NodeServices.layer)); + + const serialReference = (env: Record) => + Effect.gen(function* () { + const available: Array = []; + for (const editorId of REMOTE_CAPABLE_EDITOR_IDS) { + const editor = EDITORS.find((candidate) => candidate.id === editorId); + if (editor && Option.isSome(yield* resolveEditorCommand(editor, env))) { + available.push(editorId); + } + } + return available; + }); + + it.effect("reads each PATH directory once instead of stat-ing every candidate per editor", () => + Effect.gen(function* () { + const { env: baseEnv } = yield* makeFakePath(); + const delimiter = platform === "win32" ? ";" : ":"; + const uncachedCounter = { stat: 0, reads: new Map() }; + const cachedCounter = { stat: 0, reads: new Map() }; + const uncachedEnv = { ...baseEnv, PATH: `${baseEnv.PATH}${delimiter}${baseEnv.HOME}x1` }; + const cachedEnv = { ...baseEnv, PATH: `${baseEnv.PATH}${delimiter}${baseEnv.HOME}x2` }; + const uncached = yield* serialReference(uncachedEnv).pipe( + Effect.provide(countingFileSystem(uncachedCounter)), + ); + const cached = yield* probeRemoteEditors + .handler(undefined) + .pipe( + Effect.provideService(HostProcess.Environment, cachedEnv), + Effect.provideService(HostProcess.Platform, platform), + Effect.provide(countingFileSystem(cachedCounter)), + ); + // The host can add editors from app folders the fixture can't redirect (e.g. /Applications). + const fixtureEditors = ["cursor", "vscode", "zed", "webstorm"]; + assert.deepStrictEqual( + uncached.filter((id) => fixtureEditors.includes(id)), + fixtureEditors, + ); + assert.deepStrictEqual(cached, uncached); + assert.isAbove(uncachedCounter.stat, 0); + // On every platform the probe lists each PATH directory at most once. Only PATH entries count: + // resolveEditorCommand also reads app folders such as /Applications on macOS. + const pathReads = cachedEnv.PATH.split(delimiter) + .filter((entry) => entry.length > 0) + .map((entry) => cachedCounter.reads.get(entry) ?? 0); + assert.isAbove(Math.max(...pathReads), 0); + assert.isAtMost(Math.max(...pathReads), 1); + // Without the listings, Windows stats every PATH entry x PATHEXT candidate for each editor; + // elsewhere there is no PATHEXT, so the stat count is about the same either way. + if (platform === "win32") { + assert.isBelow(cachedCounter.stat, uncachedCounter.stat / 2); + } + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), + ); +}); diff --git a/apps/desktop/src/ipc/methods/window.ts b/apps/desktop/src/ipc/methods/window.ts index cee92e1ead38c..38887ec71d906 100644 --- a/apps/desktop/src/ipc/methods/window.ts +++ b/apps/desktop/src/ipc/methods/window.ts @@ -17,6 +17,7 @@ import { } from "@t3tools/contracts"; import { PROJECT_FAVICON_EXTENSIONS } from "@t3tools/shared/projectFavicon"; import { resolveEditorCommand } from "@t3tools/shared/editor"; +import { withPathDirectoryListings } from "@t3tools/shared/shell"; import * as HostProcess from "@t3tools/shared/HostProcess"; import { isRelayTracingEnabled } from "@t3tools/shared/otelEnvironment"; import * as FileSystem from "effect/FileSystem"; @@ -36,6 +37,7 @@ import * as DesktopAppSettings from "../../settings/DesktopAppSettings.ts"; import * as DesktopWslBackend from "../../wsl/DesktopWslBackend.ts"; import * as DesktopWslEnvironment from "../../wsl/DesktopWslEnvironment.ts"; import * as ElectronApp from "../../electron/ElectronApp.ts"; +import * as ElectronAudio from "../../electron/ElectronAudio.ts"; import * as ElectronDialog from "../../electron/ElectronDialog.ts"; import * as ElectronMenu from "../../electron/ElectronMenu.ts"; import * as ElectronShell from "../../electron/ElectronShell.ts"; @@ -427,20 +429,26 @@ export const openSystemSettings = DesktopIpc.makeIpcMethod({ }), }); +const REMOTE_EDITOR_PROBE_CONCURRENCY = 8; + export const probeRemoteEditors = DesktopIpc.makeIpcMethod({ channel: IpcChannels.PROBE_REMOTE_EDITORS_CHANNEL, payload: Schema.Undefined, result: Schema.Array(EditorId), handler: Effect.fn("desktop.ipc.window.probeRemoteEditors")(function* () { - const available: Array = []; const env = yield* HostProcess.Environment; - for (const editorId of REMOTE_CAPABLE_EDITOR_IDS) { - const editor = EDITORS.find((editor) => editor.id === editorId); - if (editor && Option.isSome(yield* resolveEditorCommand(editor, env))) { - available.push(editorId); - } - } - return available; + // Shared listings stop each editor re-stating every PATH entry x PATHEXT candidate on Windows. + const resolved = yield* Effect.forEach( + REMOTE_CAPABLE_EDITOR_IDS, + (editorId) => { + const editor = EDITORS.find((editor) => editor.id === editorId); + return editor + ? resolveEditorCommand(editor, env).pipe(Effect.map(Option.isSome)) + : Effect.succeed(false); + }, + { concurrency: REMOTE_EDITOR_PROBE_CONCURRENCY }, + ).pipe(withPathDirectoryListings); + return REMOTE_CAPABLE_EDITOR_IDS.filter((_, index) => resolved[index]); }), }); @@ -588,3 +596,10 @@ export const checkSystemPermission = DesktopIpc.makeIpcMethod({ return yield* Effect.promise(check); }), }); + +export const restartAudioService = DesktopIpc.makeIpcMethod({ + channel: IpcChannels.RESTART_AUDIO_SERVICE_CHANNEL, + payload: Schema.Void, + result: Schema.Boolean, + handler: ElectronAudio.restart, +}); diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts index 003009f7190fa..432577917a3ea 100644 --- a/apps/desktop/src/main.ts +++ b/apps/desktop/src/main.ts @@ -1,8 +1,8 @@ import * as MacPermissions from "./permissions/MacPermissions.ts"; +import { handleStdioError } from "./app/stdioErrors.ts"; + for (const stream of [process.stdout, process.stderr]) { - stream.on("error", (err: NodeJS.ErrnoException) => { - if (err.code !== "EPIPE") throw err; - }); + stream.on("error", handleStdioError); } import * as NodeHttpClient from "@effect/platform-node/NodeHttpClient"; diff --git a/apps/desktop/src/preload.ts b/apps/desktop/src/preload.ts index e0ac5f2161051..ba9c17aae2695 100644 --- a/apps/desktop/src/preload.ts +++ b/apps/desktop/src/preload.ts @@ -115,6 +115,7 @@ contextBridge.exposeInMainWorld("desktopBridge", { ipcRenderer.sendSync(IpcChannels.GET_RELAY_TELEMETRY_ENABLED_CHANNEL) === true, getPathForFile: (file: File) => webUtils.getPathForFile(file), getClientPlatform: () => clientPlatform, + restartAudioService: () => ipcRenderer.invoke(IpcChannels.RESTART_AUDIO_SERVICE_CHANNEL), setNotificationBadge: (badge) => ipcRenderer.invoke(IpcChannels.SET_NOTIFICATION_BADGE_CHANNEL, badge), onNotificationBadgeClear: (listener) => { @@ -414,6 +415,8 @@ contextBridge.exposeInMainWorld("desktopBridge", { ipcRenderer.invoke(IpcChannels.PREVIEW_STOP_FIND_IN_PAGE_CHANNEL, { tabId }), setShown: (tabId, shown) => ipcRenderer.invoke(IpcChannels.PREVIEW_SET_SHOWN_CHANNEL, { tabId, shown }), + setCaptureDrawn: (tabId, drawn) => + ipcRenderer.invoke(IpcChannels.PREVIEW_SET_CAPTURE_DRAWN_CHANNEL, { tabId, drawn }), openDevTools: (tabId) => ipcRenderer.invoke(IpcChannels.PREVIEW_OPEN_DEVTOOLS_CHANNEL, { tabId }), listBrowserImportSources: () => ipcRenderer.invoke(IpcChannels.PREVIEW_IMPORT_SOURCES_CHANNEL), diff --git a/apps/desktop/src/preview/BrowserImport/ChromiumCookies.test.ts b/apps/desktop/src/preview/BrowserImport/ChromiumCookies.test.ts index 92ab35320dab7..eb4dd3143eefb 100644 --- a/apps/desktop/src/preview/BrowserImport/ChromiumCookies.test.ts +++ b/apps/desktop/src/preview/BrowserImport/ChromiumCookies.test.ts @@ -358,6 +358,29 @@ describe("readChromiumCookieDatabase", () => { }).pipe(Effect.provide(NodeServices.layer)), ); + it.effect("reads a cookie database the browser has emptied as having no cookies", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const directory = yield* fileSystem.makeTempDirectoryScoped({ + prefix: "t3code-chromium-cookies-", + }); + const filename = `${directory}/Cookies`; + yield* Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + yield* sql`create table scratch (id integer)`; + yield* sql`drop table scratch`; + }).pipe(Effect.provide(NodeSqliteClient.layer({ filename }))); + + const result = yield* readChromiumCookieDatabase( + filename, + { cbcV10: Buffer.from("0123456789abcdef") }, + "darwin", + ); + + expect(result).toEqual({ cookies: [], undecryptable: 0, undecryptableHosts: [] }); + }).pipe(Effect.provide(NodeServices.layer), Effect.scoped), + ); + it.effect("skips partitioned cookies without breaking pre-CHIPS schemas", () => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; diff --git a/apps/desktop/src/preview/BrowserImport/ChromiumCookies.ts b/apps/desktop/src/preview/BrowserImport/ChromiumCookies.ts index 63fef9fb3c808..91cbfa03aac7d 100644 --- a/apps/desktop/src/preview/BrowserImport/ChromiumCookies.ts +++ b/apps/desktop/src/preview/BrowserImport/ChromiumCookies.ts @@ -181,6 +181,10 @@ export const readChromiumCookieDatabase = Effect.fn("ChromiumCookies.readChromiu function* (snapshotPath: string, keys: ChromiumKeyMaterial, platform: NodeJS.Platform) { const result = yield* Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; + // Chromium razes a damaged jar to a file with no tables and rebuilds it on its next write. + const tables = + yield* sql`select 1 from sqlite_master where type = 'table' and name = 'cookies'`; + if (tables.length === 0) return { rows: [], schemaVersion: 0 }; const schemaVersion = yield* sql`select value from meta where key = 'version' limit 1`.pipe( Effect.flatMap(decodeSchemaVersion), Effect.map(([row]) => row.value), diff --git a/apps/desktop/src/preview/BrowserImport/Sources.ts b/apps/desktop/src/preview/BrowserImport/Sources.ts index 20a35752ed979..6ecd546a1ad57 100644 --- a/apps/desktop/src/preview/BrowserImport/Sources.ts +++ b/apps/desktop/src/preview/BrowserImport/Sources.ts @@ -345,6 +345,12 @@ const countProfileCookies = Effect.fnUntraced(function* ( if (database === undefined) return undefined; return yield* Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; + // readChromiumCookieDatabase imports a razed jar as empty; Firefox has no such reader guard. + if (definition.engine !== "firefox") { + const tables = + yield* sql`select 1 from sqlite_master where type = 'table' and name = 'cookies'`; + if (tables.length === 0) return 0; + } const rows = definition.engine === "firefox" ? yield* sql`select count(*) as count from moz_cookies where originAttributes = ''` diff --git a/apps/desktop/src/preview/CdpRelay.test.ts b/apps/desktop/src/preview/CdpRelay.test.ts index ccc2b868720b4..564b4dc57d6f4 100644 --- a/apps/desktop/src/preview/CdpRelay.test.ts +++ b/apps/desktop/src/preview/CdpRelay.test.ts @@ -9,6 +9,7 @@ import { const makeTarget = ( send: CdpRelayTarget["send"], setDownloadDirectory: CdpRelayTarget["setDownloadDirectory"] = () => {}, + ackScreencastFrame: CdpRelayTarget["ackScreencastFrame"] = () => {}, ): CdpRelayTarget => ({ send, targetId: async () => "GUEST-TARGET", @@ -17,6 +18,7 @@ const makeTarget = ( userAgent: () => "Electron", setDownloadDirectory, receivesScreencast: () => true, + ackScreencastFrame, }); const makeRelay = (target: CdpRelayTarget, write: (raw: string) => void) => @@ -138,9 +140,13 @@ describe("CDP relay", () => { describe("CDP relay extra sessions", () => { it("gives a second page session the page's commands and events", async () => { - const send = vi.fn(async () => ({})); + const send = vi.fn(async () => ({})); const written: Array> = []; - const relay = makeRelay(makeTarget(send), (raw) => written.push(JSON.parse(raw))); + const nativeAcks: Array = []; + const relay = makeRelay( + makeTarget(send, undefined, (frameId) => nativeAcks.push(frameId)), + (raw) => written.push(JSON.parse(raw)), + ); relay.receive(JSON.stringify({ id: 1, method: "Target.setAutoAttach", params: {} })); relay.receive(JSON.stringify({ id: 2, method: "Target.attachToBrowserTarget" })); await settle(); @@ -170,8 +176,63 @@ describe("CDP relay extra sessions", () => { await settle(); expect(send).toHaveBeenLastCalledWith("Page.startScreencast", {}, undefined, extra); written.length = 0; - relay.event("Page.screencastFrame", { data: "x" }, undefined); - expect(written.map((m) => m["sessionId"])).toEqual(["t3-preview-page", extra]); + const frame = (frameId: number) => + relay.event("Page.screencastFrame", { data: "x", sessionId: frameId }, undefined); + const ack = (sessionId: string, frameId: number) => + relay.receive( + JSON.stringify({ + id: 6, + method: "Page.screencastFrameAck", + params: { sessionId: frameId }, + sessionId, + }), + ); + const delivered = () => { + const frames = written + .filter((m) => m["method"]) + .map((m) => [m["sessionId"], (m["params"] as { sessionId?: number }).sessionId]); + written.length = 0; + return frames; + }; + frame(1); + expect(delivered()).toEqual([ + ["t3-preview-page", 1], + [extra, 1], + ]); + // A session that has not acknowledged its frame gets only the newest one, on its ack. + for (let i = 2; i < 100; i++) frame(i); + expect(delivered()).toEqual([]); + // Frames every session is busy for stay unacknowledged, so Chromium pauses at its in-flight limit. + expect(nativeAcks).toEqual([1]); + ack(extra, 1); + await settle(); + expect(delivered()).toEqual([[extra, 99]]); + // Each consumer ack lets Chromium send one more frame. + expect(nativeAcks).toEqual([1, 2]); + ack(extra, 99); + await settle(); + expect(delivered()).toEqual([]); + expect(nativeAcks).toEqual([1, 2, 3]); + frame(100); + expect(delivered()).toEqual([[extra, 100]]); + ack(extra, 99); + await settle(); + frame(101); + expect(delivered()).toEqual([]); + expect(send.mock.calls.some(([method]) => method === "Page.screencastFrameAck")).toBe(false); + // A restart keeps the unacknowledged frame's slot until its own acknowledgement. + relay.receive(JSON.stringify({ id: 7, method: "Page.stopScreencast", sessionId: extra })); + relay.receive(JSON.stringify({ id: 8, method: "Page.startScreencast", sessionId: extra })); + await settle(); + delivered(); + frame(102); + expect(delivered()).toEqual([]); + ack(extra, 100); + await settle(); + expect(delivered()).toEqual([[extra, 102]]); + // Other events still reach a session while its frame is unacknowledged. + relay.event("Page.loadEventFired", { timestamp: 1 }, undefined); + expect(delivered().map(([session]) => session)).toEqual(["t3-preview-page", extra]); }); }); diff --git a/apps/desktop/src/preview/CdpRelay.ts b/apps/desktop/src/preview/CdpRelay.ts index 658e1cf70623d..e9f3cf509b446 100644 --- a/apps/desktop/src/preview/CdpRelay.ts +++ b/apps/desktop/src/preview/CdpRelay.ts @@ -11,6 +11,8 @@ export interface CdpRelayTarget { readonly sessionDetached?: (owner: string) => void; /** Whether a page session, by owner as `send` names it, gets the page's screencast frames. */ readonly receivesScreencast: (owner: string | undefined) => boolean; + /** Acknowledges a native screencast frame, letting Chromium send the next one. */ + readonly ackScreencastFrame: (frameId: number) => void; /** The page's own CDP target id; Playwright keys the main frame by it. */ readonly targetId: () => Promise; readonly url: () => string; @@ -103,6 +105,20 @@ export function createCdpRelayConnection( let downloadEventsEnabled = false; /** Relay-created page sessions share one real debugger; child frame sessions retain their own ids. */ const sessions = new Map([[PAGE_SESSION_ID, "page"]]); + // Each page session holds one unacknowledged frame and keeps the newest one skipped meanwhile. + const unackedFrames = new Map(); + const skippedFrames = new Map>(); + // Chromium keeps a few frames in flight and reuses one frame id per run, so pacing it means + // leaving frames every session is busy for unacknowledged and releasing one per consumer ack. + const heldNativeFrames: Array = []; + const releaseHeldFramesWithoutHolders = () => { + if (skippedFrames.size === 0) + for (const frameId of heldNativeFrames.splice(0)) target.ackScreencastFrame(frameId); + }; + const sendFrame = (sessionId: string, params: Record) => { + unackedFrames.set(sessionId, params["sessionId"] as number); + send({ method: "Page.screencastFrame", params, sessionId }); + }; let sessionSequence = 0; const newSession = (kind: "browser" | "page") => { const sessionId = `t3-preview-${kind}-${++sessionSequence}`; @@ -170,6 +186,9 @@ export function createCdpRelayConnection( const sessionId = command.params?.["sessionId"]; if (typeof sessionId === "string" && sessionId !== PAGE_SESSION_ID) { sessions.delete(sessionId); + unackedFrames.delete(sessionId); + skippedFrames.delete(sessionId); + releaseHeldFramesWithoutHolders(); target.sessionDetached?.(sessionId); } return {}; @@ -202,6 +221,27 @@ export function createCdpRelayConnection( } // Playwright asks for this on every page; the host emulates focus only while it is in use. if (command.method === "Emulation.setFocusEmulationEnabled") return Promise.resolve({}); + if (command.method === "Page.screencastFrameAck" && child === undefined) { + // A restart keeps the slot too, or restarting would let frames past an unacknowledged one. + const sessionId = command.sessionId!; + if (unackedFrames.get(sessionId) === command.params?.["sessionId"]) { + unackedFrames.delete(sessionId); + const skipped = skippedFrames.get(sessionId); + skippedFrames.delete(sessionId); + if ( + skipped !== undefined && + target.receivesScreencast(sessionId === PAGE_SESSION_ID ? undefined : sessionId) + ) + sendFrame(sessionId, skipped); + const released = heldNativeFrames.shift(); + if (released !== undefined) target.ackScreencastFrame(released); + } + return Promise.resolve({}); + } + if (command.method === "Page.stopScreencast" && child === undefined) { + skippedFrames.delete(command.sessionId!); + releaseHeldFramesWithoutHolders(); + } // The shared debugger keeps Runtime enabled after an earlier connection, and a repeated // enable reports no existing contexts; Playwright waits for them, so reset once per connection. if (command.method === "Runtime.enable" && child === undefined) { @@ -262,15 +302,29 @@ export function createCdpRelayConnection( send({ method, params, sessionId }); return; } - for (const [id, kind] of sessions) { - if (kind !== "page") continue; - if ( - method === "Page.screencastFrame" && - !target.receivesScreencast(id === PAGE_SESSION_ID ? undefined : id) - ) - continue; - send({ method, params, sessionId: id }); + if (method === "Page.screencastFrame") { + const frame = (params ?? {}) as Record; + const frameId = frame["sessionId"]; + if (typeof frameId !== "number") return; + let held = false; + let delivered = false; + for (const [id, kind] of sessions) { + if (kind !== "page") continue; + if (!target.receivesScreencast(id === PAGE_SESSION_ID ? undefined : id)) continue; + if (unackedFrames.has(id)) { + skippedFrames.set(id, frame); + held = true; + } else { + sendFrame(id, frame); + delivered = true; + } + } + if (held && !delivered) heldNativeFrames.push(frameId); + else target.ackScreencastFrame(frameId); + return; } + for (const [id, kind] of sessions) + if (kind === "page") send({ method, params, sessionId: id }); }, }; } diff --git a/apps/desktop/src/preview/DesktopBrowserHost.test.ts b/apps/desktop/src/preview/DesktopBrowserHost.test.ts index d6f25d66aa810..8e96129293fc8 100644 --- a/apps/desktop/src/preview/DesktopBrowserHost.test.ts +++ b/apps/desktop/src/preview/DesktopBrowserHost.test.ts @@ -26,6 +26,11 @@ const decodeCdpMessage = Schema.decodeUnknownSync( ), ); const key = { threadId: "thread-1", tabId: "tab-1" }; + +/** A host whose client already draws `key` at full detail, so its screenshots go out at once. */ +const makeDrawnHost = Effect.tap(DesktopBrowserHost.make, (host) => + Effect.sync(() => host.setCaptureDrawn(key, true)), +); /** Sends a page command whose id the fake debugger records as it receives it. */ const command = ( host: DesktopBrowserHost.DesktopBrowserHost["Service"], @@ -172,7 +177,7 @@ const watchPainting = (host: DesktopBrowserHost.DesktopBrowserHost["Service"]) = describe("DesktopBrowserHost", () => { it.effect("announces tabs already attached to a backend that starts later", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; host.attach(key, makeDebuggee().tab); expect(yield* takeEvents(host, 1)).toEqual([{ type: "attached", ...key }]); @@ -182,7 +187,7 @@ describe("DesktopBrowserHost", () => { it.effect("announces a guest still loading its first page only once that page is up", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const loading = makeDebuggee({ loading: true }); const other = { ...key, tabId: "tab-2" }; const reader = yield* takeEvents(host, 3).pipe(Effect.forkScoped); @@ -205,7 +210,7 @@ describe("DesktopBrowserHost", () => { "keeps a loading guest unannounced across a replaced navigation and a new backend", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const loading = makeDebuggee({ loading: true }); const other = { ...key, tabId: "tab-2" }; host.attach(key, loading.tab); @@ -227,7 +232,7 @@ describe("DesktopBrowserHost", () => { "announces a first page that never comes up, but never a tab closed or destroyed first", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const closed = { ...key, tabId: "tab-2" }; const destroyed = { ...key, tabId: "tab-3" }; const destroyedGuest = makeDebuggee({ loading: true }); @@ -247,7 +252,7 @@ describe("DesktopBrowserHost", () => { it.effect("re-sends shown to a backend that starts later and when a tab attaches again", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; host.attach(key, makeDebuggee().tab); host.setShown(key, true); const reader = yield* takeEvents(host, 5).pipe(Effect.forkScoped); @@ -268,7 +273,7 @@ describe("DesktopBrowserHost", () => { it.effect("replays an explicit hidden report before announcing another tab", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const other = { ...key, tabId: "tab-2" }; host.setShown(key, false); host.attach(key, makeDebuggee().tab); @@ -285,7 +290,7 @@ describe("DesktopBrowserHost", () => { it.effect("drops replies from a relay the server released", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee(); host.attach(key, debuggee.tab); const reader = yield* takeEvents(host, 2).pipe(Effect.forkScoped); @@ -313,7 +318,7 @@ describe("DesktopBrowserHost", () => { it.effect("sends a page one screenshot at a time", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee(); host.attach(key, debuggee.tab); const replies = yield* watchReplies(host); @@ -330,9 +335,38 @@ describe("DesktopBrowserHost", () => { }), ); + it.effect( + "sends a screenshot once the client draws the guest at full detail, or soon after", + () => + Effect.gen(function* () { + const host = yield* DesktopBrowserHost.make; + const debuggee = makeDebuggee(); + host.attach(key, debuggee.tab); + const replies = yield* watchReplies(host); + + yield* command(host, 1, "Page.captureScreenshot"); + yield* settle; + expect(debuggee.sent).toEqual([]); + host.setCaptureDrawn(key, true); + yield* settle; + expect(debuggee.sent).toEqual([1]); + debuggee.releaseFirst(); + expect(yield* Queue.take(replies)).toBe(1); + + // A client that never draws, such as a hidden window, only delays the capture. + host.setCaptureDrawn(key, false); + yield* command(host, 2, "Page.captureScreenshot"); + yield* settle; + expect(debuggee.sent).toEqual([1]); + yield* TestClock.adjust("1 second"); + yield* settle; + expect(debuggee.sent).toEqual([1, 2]); + }).pipe(Effect.scoped), + ); + it.effect("stops waiting on a screenshot the page navigated away from", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee(); host.attach(key, debuggee.tab); const replies = yield* watchReplies(host); @@ -357,7 +391,7 @@ describe("DesktopBrowserHost", () => { it.effect("drops screenshots a released relay left waiting", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee(); host.attach(key, debuggee.tab); @@ -377,7 +411,7 @@ describe("DesktopBrowserHost", () => { it.effect("emulates focus while the server drives a guest, then lets a hidden one throttle", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee(); host.attach(key, debuggee.tab); @@ -408,7 +442,7 @@ describe("DesktopBrowserHost", () => { it.effect("keeps a guest focused while a command it is running is still pending", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee(); host.attach(key, debuggee.tab); const replies = yield* watchReplies(host); @@ -429,7 +463,7 @@ describe("DesktopBrowserHost", () => { it.effect("lets a guest throttle once Chromium can no longer answer its pending command", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee(); host.attach(key, debuggee.tab); @@ -451,7 +485,7 @@ describe("DesktopBrowserHost", () => { it.effect("keeps a guest focused for the screenshot a navigation lets start", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee(); host.attach(key, debuggee.tab); @@ -467,7 +501,7 @@ describe("DesktopBrowserHost", () => { it.effect("never sends a screenshot a navigation superseded while focus was changing", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee({ deferFocus: true }); host.attach(key, debuggee.tab); @@ -484,7 +518,7 @@ describe("DesktopBrowserHost", () => { it.effect("keeps a guest focused when its turn starts after the agent's first command", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee(); host.attach(key, debuggee.tab); const turn = (active: boolean) => @@ -506,7 +540,7 @@ describe("DesktopBrowserHost", () => { it.effect("lets a guest throttle through a turn that only sent it housekeeping", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee(); host.attach(key, debuggee.tab); yield* host.handleCommandLine( @@ -535,7 +569,7 @@ describe("DesktopBrowserHost", () => { it.effect("keeps a guest the agent used focused through its turn's pauses", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee(); host.attach(key, debuggee.tab); const turn = (active: boolean) => @@ -563,7 +597,7 @@ describe("DesktopBrowserHost", () => { "ends a command's hold with the turn that sent it, even if Chromium never answers", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee(); host.attach(key, debuggee.tab); yield* host.handleCommandLine( @@ -584,7 +618,7 @@ describe("DesktopBrowserHost", () => { it.effect("ends the agent's own session's holds with its turn, but not a viewer's stream", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee(); host.attach(key, debuggee.tab); const replies = yield* watchReplies(host); @@ -617,7 +651,7 @@ describe("DesktopBrowserHost", () => { it.effect("paints for a screenshot queued behind one its viewer abandoned mid-capture", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee(); host.attach(key, debuggee.tab); const replies = yield* watchReplies(host); @@ -655,7 +689,7 @@ describe("DesktopBrowserHost", () => { it.effect("stops painting for a queued screenshot once its session detaches", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee(); host.attach(key, debuggee.tab); const replies = yield* watchReplies(host); @@ -694,7 +728,7 @@ describe("DesktopBrowserHost", () => { it.effect("never sends a screenshot whose session detached while focus was changing", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee({ deferFocus: true }); host.attach(key, debuggee.tab); const replies = yield* watchReplies(host); @@ -718,7 +752,7 @@ describe("DesktopBrowserHost", () => { it.effect("holds no focus for a screenshot still queued when the agent's turn ends", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee(); host.attach(key, debuggee.tab); const painting = yield* watchPainting(host); @@ -748,7 +782,7 @@ describe("DesktopBrowserHost", () => { it.effect("lets a guest throttle when Chromium refuses a viewer's screencast", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee({ refuse: (method) => method === "Page.startScreencast" }); host.attach(key, debuggee.tab); const replies = yield* watchReplies(host); @@ -769,7 +803,7 @@ describe("DesktopBrowserHost", () => { it.effect("lets a guest throttle after a person's input through a viewer that then closed", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee(); host.attach(key, debuggee.tab); const replies = yield* watchReplies(host); @@ -798,7 +832,7 @@ describe("DesktopBrowserHost", () => { it.effect("keeps a page's screencast when a child session's screencast is refused", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee({ refuse: (method, sessionId) => method === "Page.startScreencast" && sessionId !== undefined, }); @@ -820,7 +854,7 @@ describe("DesktopBrowserHost", () => { it.effect("ignores a screencast that arrives on a session after it detached", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee(); host.attach(key, debuggee.tab); const replies = yield* watchReplies(host); @@ -846,7 +880,7 @@ describe("DesktopBrowserHost", () => { it.effect("lets a guest throttle once the session waiting on its command detaches", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee(); host.attach(key, debuggee.tab); const replies = yield* watchReplies(host); @@ -873,7 +907,7 @@ describe("DesktopBrowserHost", () => { it.effect("never sends a screenshot queued by a session that has since detached", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee(); host.attach(key, debuggee.tab); const replies = yield* watchReplies(host); @@ -904,7 +938,7 @@ describe("DesktopBrowserHost", () => { it.effect("keeps a streamed guest focused until its screencast ends", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee(); host.attach(key, debuggee.tab); @@ -933,7 +967,7 @@ describe("DesktopBrowserHost", () => { it.effect("keeps a guest focused while any of its screencasts runs", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee(); host.attach(key, debuggee.tab); const replies = yield* watchReplies(host); @@ -965,7 +999,7 @@ describe("DesktopBrowserHost", () => { it.effect("paints a hidden guest only while the screenshot it is taking is pending", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee(); host.attach(key, debuggee.tab); const painting = yield* watchPainting(host); @@ -991,7 +1025,7 @@ describe("DesktopBrowserHost", () => { it.effect("stops painting for a screenshot Chromium never answers once its document goes", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee(); host.attach(key, debuggee.tab); const painting = yield* watchPainting(host); @@ -1036,7 +1070,7 @@ describe("DesktopBrowserHost", () => { it.effect("paints a streamed guest until its last screencast ends", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee(); host.attach(key, debuggee.tab); const replies = yield* watchReplies(host); @@ -1157,6 +1191,13 @@ describe("DesktopBrowserHost", () => { debuggee.refuse(6); debuggee.release(); expect([yield* Queue.take(replies), yield* Queue.take(replies)].toSorted()).toEqual([5, 6]); + yield* raw({ + id: 7, + method: "Page.screencastFrameAck", + params: { sessionId: 1 }, + sessionId: "t3-preview-page-1", + }); + expect(yield* Queue.take(replies)).toBe(7); yield* settle; frames.length = 0; debuggee.emit("Page.screencastFrame", { data: "frame", sessionId: 2 }); @@ -1236,7 +1277,7 @@ describe("DesktopBrowserHost", () => { it.effect("saves a server tab's download under its CDP guid where the server asked", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee(); host.attach(key, debuggee.tab); const replies = yield* watchReplies(host); @@ -1271,7 +1312,7 @@ describe("DesktopBrowserHost", () => { it.effect("tells a download the person clicked from one the agent's input started", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const debuggee = makeDebuggee(); host.attach(key, debuggee.tab); @@ -1335,7 +1376,7 @@ describe("DesktopBrowserHost", () => { it.effect("detaches only the tab whose CDP event is too large to relay", () => Effect.gen(function* () { - const host = yield* DesktopBrowserHost.make; + const host = yield* makeDrawnHost; const tabs = [makeDebuggee({ deferFocus: true }), makeDebuggee()]; const send = vi.spyOn(tabs[0]!.tab.debugger, "sendCommand"); const keys = [key, { threadId: "thread-2", tabId: "tab-2" }]; diff --git a/apps/desktop/src/preview/DesktopBrowserHost.ts b/apps/desktop/src/preview/DesktopBrowserHost.ts index 296be2fa6709f..ce5d7074a786c 100644 --- a/apps/desktop/src/preview/DesktopBrowserHost.ts +++ b/apps/desktop/src/preview/DesktopBrowserHost.ts @@ -30,6 +30,8 @@ const AGENT_INPUT_WINDOW_MS = 5_000; /** A viewer's pointer travelling over the stream activates nothing on the page. */ const POINTER_TRAVEL = new Set(["mouseMoved", "mouseWheel"]); const FOCUS_IDLE = "5 seconds"; +/** How long a screenshot waits for the client to draw its guest at full detail. */ +const CAPTURE_DRAW_WAIT = "1 second"; /** Well inside the server's 10 s DESKTOP_ATTACH_TIMEOUT, for a first page that never reports it is up. */ const FIRST_PAGE_TIMEOUT = "5 seconds"; const ERR_ABORTED = -3; @@ -101,6 +103,8 @@ export class DesktopBrowserHost extends Context.Service< readonly detach: (key: DesktopBrowserTabKey) => void; readonly loadFailed: (key: DesktopBrowserTabKey, failure: PreviewAutomationLoadError) => void; readonly setShown: (key: DesktopBrowserTabKey, shown: boolean | undefined) => void; + /** Whether the client draws the guest at full detail while it is captured. */ + readonly setCaptureDrawn: (key: DesktopBrowserTabKey, drawn: boolean | undefined) => void; readonly humanStartedDownload: (source: Electron.WebContents) => boolean; /** Whether what the page does now follows the agent's input, not a viewer's. */ readonly agentLedPage: (source: Electron.WebContents) => boolean; @@ -138,6 +142,8 @@ export const make = Effect.gen(function* () { // Kept apart from attachment: the renderer can show a webview before its debugger attaches, // and the server forgets `shown` on detach, for example while DevTools holds the debugger. const shownTabs = new Map(); + const drawnTabs = new Set(); + const drawWaiters = new Map void>>(); const emit = (event: DesktopBrowserEventType) => runFork(PubSub.publish(outbox, event)); const nextScreenshot = (tab: AttachedTab) => { @@ -266,12 +272,32 @@ export const make = Effect.gen(function* () { }); }; + /** + * A scaled guest rasters at its on-screen size until the client draws it at full detail, + * and a screenshot taken before that copies the blurred frame. A client that cannot draw + * (a hidden window, say) only delays the capture. + */ + const whenDrawn = (tab: AttachedTab) => + new Promise((resolve) => { + const id = keyOf(tab.key); + const waiters = drawWaiters.get(id) ?? new Set(); + drawWaiters.set(id, waiters); + const done = () => { + waiters.delete(done); + if (waiters.size === 0 && drawWaiters.get(id) === waiters) drawWaiters.delete(id); + resolve(); + }; + waiters.add(done); + runFork(Effect.sleep(CAPTURE_DRAW_WAIT).pipe(Effect.andThen(Effect.sync(done)))); + }); + /** Chromium answers a capture, and frees its wake lock, only once the guest paints a frame. */ const paintUntilAnswered = (tab: AttachedTab, send: () => Promise) => { const sent = {}; tab.capturing.add(sent); syncPainting(tab); - return send().finally(() => { + // Sent at once when already drawn, so it keeps its place among the commands after it. + return (drawnTabs.has(keyOf(tab.key)) ? send() : whenDrawn(tab).then(send)).finally(() => { tab.capturing.delete(sent); syncPainting(tab); }); @@ -368,12 +394,15 @@ export const make = Effect.gen(function* () { // Waits for focus emulation; insertText and other non-CDP input would not queue behind it. const forward = (current?: () => boolean) => { const change = tab.focusChange; - const run = () => - tab.relay !== relay || (current !== undefined && !current()) + const stale = () => tab.relay !== relay || (current !== undefined && !current()); + const sendUnlessStale = () => + stale() ? Promise.reject(new Error("The page this screenshot was for has moved on.")) - : current === undefined - ? send(method, params, sessionId) - : paintUntilAnswered(tab, () => send(method, params, sessionId)); + : send(method, params, sessionId); + const run = () => + current === undefined || stale() + ? sendUnlessStale() + : paintUntilAnswered(tab, sendUnlessStale); return change === null ? run() : change.then(run); }; return method === "Page.captureScreenshot" @@ -421,6 +450,10 @@ export const make = Effect.gen(function* () { dropSessionHolds(tab, owner); }, receivesScreencast: (owner) => tab.screencasts.has(owner), + ackScreencastFrame: (frameId) => + void debuggee + .sendCommand("Page.screencastFrameAck", { sessionId: frameId }) + .catch(() => {}), targetId: () => debuggee .sendCommand("Target.getTargetInfo") @@ -583,6 +616,17 @@ export const make = Effect.gen(function* () { if (tabs.get(id)?.stopWaiting === null) emit({ type: "shown", ...key, shown }); }; + const setCaptureDrawn = (key: DesktopBrowserTabKey, drawn: boolean | undefined) => { + const id = keyOf(key); + if (!drawn) { + drawnTabs.delete(id); + return; + } + drawnTabs.add(id); + for (const done of drawWaiters.get(id) ?? []) done(); + drawWaiters.delete(id); + }; + const loadFailed: DesktopBrowserHost["Service"]["loadFailed"] = (key, failure) => { if (tabs.has(keyOf(key))) emit({ type: "loadFailed", ...key, ...failure }); }; @@ -659,6 +703,7 @@ export const make = Effect.gen(function* () { detach, loadFailed, setShown, + setCaptureDrawn, placeDownload, humanStartedDownload, agentLedPage, diff --git a/apps/desktop/src/preview/Manager.test.ts b/apps/desktop/src/preview/Manager.test.ts index b6387a06c22d8..2091ff3c7f8f2 100644 --- a/apps/desktop/src/preview/Manager.test.ts +++ b/apps/desktop/src/preview/Manager.test.ts @@ -617,6 +617,32 @@ describe("PreviewManager", () => { webviewSend.mockClear(); }); + effectIt.effect("preserves recording artifacts saved in the same millisecond", () => + withManager((manager) => + Effect.gen(function* () { + const stored = new Map(); + const save = (path: string, bytes: Uint8Array) => { + stored.set(path, Uint8Array.from(bytes)); + return undefined; + }; + writeFile.mockImplementationOnce(save).mockImplementationOnce(save); + const firstBytes = new Uint8Array([1, 2, 3]); + const secondBytes = new Uint8Array([4, 5, 6]); + const [first, second] = yield* Effect.all( + [ + manager.saveRecording("tab_first", "video/webm", firstBytes), + manager.saveRecording("tab_second", "video/webm", secondBytes), + ], + { concurrency: "unbounded" }, + ); + + expect(first.path).not.toBe(second.path); + expect(stored.get(first.path)).toEqual(firstBytes); + expect(stored.get(second.path)).toEqual(secondBytes); + }), + ), + ); + effectIt.effect.each([false, true])( "preserves simultaneous same-name downloads with an existing destination: %s", (existing) => @@ -658,6 +684,7 @@ describe("PreviewManager", () => { attach: () => {}, detach: () => {}, setShown: () => {}, + setCaptureDrawn: () => {}, loadFailed: () => {}, humanStartedDownload: () => true, agentLedPage: () => false, @@ -2436,6 +2463,7 @@ describe("PreviewManager", () => { }), ); expect(yield* Queue.take(capturing)).toBe(true); + yield* manager.setCaptureDrawn("tab_1", true); screenshot.resolve({ data: "" }); expect(yield* Queue.take(capturing)).toBe(false); }).pipe(Effect.provide(managerLayer())), @@ -2756,6 +2784,116 @@ describe("PreviewManager", () => { ), ); + effectIt.effect( + "publishes human ownership only on transitions while retaining every recording press", + () => + withManager((manager) => + Effect.gen(function* () { + let humanInput: ((input: unknown) => void) | undefined; + const contents = makeTestPreviewWebContents(async () => ({ + toJPEG: () => Buffer.from("frame"), + getSize: () => ({ width: 800, height: 600 }), + })); + contents.ipc.on = (channel, callback) => { + if (channel === "preview:human-input") + humanInput = (input) => callback({} as Electron.IpcMainEvent, input); + return contents.ipc; + }; + fromId.mockReturnValue(contents); + yield* manager.createTab("tab_human"); + yield* manager.registerWebview("tab_human", 42); + yield* manager.startRecording("tab_human", { + showKeyPresses: true, + showMousePresses: true, + }); + const controllers: string[] = []; + const feedback = yield* Queue.unbounded(); + webviewSend.mockImplementation((channel) => { + if (channel === "preview:recording-controller") Queue.offerUnsafe(feedback, undefined); + }); + yield* manager.subscribeStateChanges((_tabId, state) => + Effect.sync(() => { + controllers.push(state.controller); + }), + ); + for (const press of [ + { kind: "pointer", x: 10, y: 20, button: 0 }, + { kind: "pointer", x: 30, y: 40, button: 0 }, + { kind: "key", key: "a", code: "KeyA" }, + ]) { + humanInput?.(press); + yield* Queue.take(feedback); + } + expect(controllers).toEqual(["human"]); + expect( + webviewSend.mock.calls.filter( + ([channel]) => channel === "preview:recording-controller", + ), + ).toEqual([ + ["preview:recording-controller", "human", { x: 10, y: 20 }], + ["preview:recording-controller", "human", { x: 30, y: 40 }], + ["preview:recording-controller", "human", undefined], + ]); + yield* TestClock.adjust(750); + expect(controllers).toEqual(["human", "none"]); + }), + ), + ); + + effectIt.effect("keeps human ownership until the last input's idle deadline", () => + withManager((manager) => + Effect.gen(function* () { + let humanInput: ((input: unknown) => void) | undefined; + const contents = makeTestPreviewWebContents(async () => ({ + toJPEG: () => Buffer.from("frame"), + getSize: () => ({ width: 800, height: 600 }), + })); + contents.ipc.on = (channel, callback) => { + if (channel === "preview:human-input") + humanInput = (input) => callback({} as Electron.IpcMainEvent, input); + return contents.ipc; + }; + fromId.mockReturnValue(contents); + yield* manager.createTab("tab_idle"); + yield* manager.registerWebview("tab_idle", 42); + const controllers: string[] = []; + yield* manager.subscribeStateChanges((_tabId, state) => + Effect.sync(() => { + controllers.push(state.controller); + }), + ); + const press = () => humanInput?.({ kind: "key", key: "a", code: "KeyA" }); + press(); + yield* TestClock.adjust(500); + press(); + yield* TestClock.adjust(749); + expect(controllers).toEqual(["human"]); + yield* TestClock.adjust(1); + expect(controllers).toEqual(["human", "none"]); + + press(); + yield* TestClock.adjust(500); + const staleInput = humanInput; + const replacement = makeTestPreviewWebContents( + async () => ({ + toJPEG: () => Buffer.from("frame"), + getSize: () => ({ width: 800, height: 600 }), + }), + 43, + ); + replacement.ipc.on = contents.ipc.on; + contents.isDestroyed = () => true; + fromId.mockImplementation((id) => (id === 43 ? replacement : null)); + yield* manager.registerWebview("tab_idle", 43); + expect(controllers.at(-1)).toBe("none"); + const count = controllers.length; + staleInput?.({ kind: "key", key: "b", code: "KeyB" }); + yield* TestClock.adjust(750); + expect(controllers).toHaveLength(count); + }), + ), + ); + effectIt.effect("gates recording decorations and isolates failed subscribers", () => withManager((manager) => Effect.gen(function* () { diff --git a/apps/desktop/src/preview/Manager.ts b/apps/desktop/src/preview/Manager.ts index 2c199e358fa5b..32ac876996c81 100644 --- a/apps/desktop/src/preview/Manager.ts +++ b/apps/desktop/src/preview/Manager.ts @@ -889,7 +889,6 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function const update = Effect.fn("PreviewManager.update")(function* ( tabId: string, patch: Partial, - humanPoint?: { readonly x: number; readonly y: number }, ) { const updatedAt = yield* currentIso; const next = yield* SynchronizedRef.modify(tabsRef, (tabs) => { @@ -905,17 +904,6 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function }); // syncTabAudible can commit after this update; emitIfCurrent prevents publishing a stale snapshot. if (Option.isSome(next)) { - if (patch.controller !== undefined && next.value.webContentsId != null) { - const capture = (yield* SynchronizedRef.get(frameCaptureSessionsRef)).get(tabId); - const webContentsId = next.value.webContentsId; - if (capture?.consumers.has("recording")) { - yield* attempt({ operation: "recording.controller", tabId }, () => { - const contents = webContents.fromId(webContentsId); - if (contents && !contents.isDestroyed()) - contents.send(RECORDING_CONTROLLER_CHANNEL, patch.controller, humanPoint); - }).pipe(Effect.ignore); - } - } yield* emitIfCurrent(tabId, next.value); } }); @@ -1108,28 +1096,13 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function ), scope, ); - const handleDebuggerMessage = Effect.fnUntraced(function* ( - method: string, - params: Record, - ) { - if (method === "Page.screencastFrame") { - const sessionId = params["sessionId"]; - if (typeof sessionId === "number") { - yield* attemptPromise( - { - operation: "ackScreencastFrame", - webContentsId: wc.id, - }, - () => wcDebugger.sendCommand("Page.screencastFrameAck", { sessionId }), - ).pipe(Effect.ignore); - } - } + const handleDebuggerMessage = Effect.fnUntraced(function* (method: string) { if (method === "Runtime.consoleAPICalled" || method === "Runtime.exceptionThrown") { yield* Queue.offer(consoleReleases, undefined); } }); - const onMessage: BrowserControlSession["onMessage"] = (_event, method, params) => { - runFork(handleDebuggerMessage(method, params)); + const onMessage: BrowserControlSession["onMessage"] = (_event, method) => { + runFork(handleDebuggerMessage(method)); }; const control: BrowserControlSession = { webContentsId: wc.id, @@ -1444,21 +1417,75 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function }), ); }; + let humanInputWorker = false; + let humanInputDeadline = 0; + let listening = true; + const publishHumanController = Effect.fn("PreviewManager.publishHumanController")(function* ( + controller: "human" | "none", + humanPoint?: { readonly x: number; readonly y: number }, + ) { + const now = yield* currentMillis; + const updatedAt = yield* currentIso; + const result = yield* SynchronizedRef.modify(tabsRef, (tabs) => { + const current = tabs.get(tabId); + if ( + !current || + current.webContentsId !== wc.id || + (controller === "human" + ? !listening || webContents.fromId(wc.id) !== wc + : current.controller !== "human" || humanInputDeadline > now) + ) + return [undefined, tabs] as const; + const changed = current.controller !== controller; + const state = changed ? { ...current, controller, updatedAt } : current; + return [ + { state, changed }, + changed + ? replaceMap(tabs, (copy) => { + copy.set(tabId, state); + }) + : tabs, + ] as const; + }); + if (!result) return; + const capture = (yield* SynchronizedRef.get(frameCaptureSessionsRef)).get(tabId); + if (capture?.consumers.has("recording")) { + yield* attempt({ operation: "recording.controller", tabId }, () => { + if (!wc.isDestroyed()) wc.send(RECORDING_CONTROLLER_CHANNEL, controller, humanPoint); + }).pipe(Effect.ignore); + } + if (result.changed) yield* emitIfCurrent(tabId, result.state); + }); + yield* Scope.addFinalizer( + scope, + Effect.gen(function* () { + listening = false; + humanInputDeadline = 0; + yield* publishHumanController("none"); + }), + ); const handleHumanInput = Effect.fn("PreviewManager.handleHumanInput")(function* ( rawSignal?: unknown, ) { - yield* update( - tabId, - { controller: "human" }, + if (!listening) return; + humanInputDeadline = (yield* currentMillis) + 750; + if (!humanInputWorker) { + humanInputWorker = true; + yield* Effect.gen(function* () { + let remaining: number; + while ((remaining = humanInputDeadline - (yield* currentMillis)) > 0) { + yield* Effect.sleep(remaining); + } + humanInputWorker = false; + yield* publishHumanController("none"); + }).pipe(Effect.forkIn(scope)); + } + yield* publishHumanController( + "human", isPreviewInputSignal(rawSignal) && rawSignal.kind === "pointer" ? { x: rawSignal.x, y: rawSignal.y } : undefined, ); - yield* Effect.sleep(750); - const tabs = yield* SynchronizedRef.get(tabsRef); - if (tabs.get(tabId)?.controller === "human") { - yield* update(tabId, { controller: "none" }); - } }); const recordingInput = (_event: unknown, input: unknown) => { if (!isRecordingInput(input)) return; @@ -1800,7 +1827,10 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function }); if (Option.isNone(tab)) return; const closedTab = tab.value; - if (closedTab.serverTab) browserHost.setShown(closedTab.serverTab, undefined); + if (closedTab.serverTab) { + browserHost.setShown(closedTab.serverTab, undefined); + browserHost.setCaptureDrawn(closedTab.serverTab, undefined); + } if (closedTab.webContentsId != null) { yield* Effect.all( [ @@ -2608,12 +2638,21 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function if (serverTab) browserHost.setShown(serverTab, shown); }); + const setCaptureDrawn = Effect.fn("PreviewManager.setCaptureDrawn")(function* ( + tabId: string, + drawn: boolean, + ) { + const serverTab = (yield* SynchronizedRef.get(tabsRef)).get(tabId)?.serverTab; + if (serverTab) browserHost.setCaptureDrawn(serverTab, drawn); + }); + // A window reload or guest crash destroys the guest without the renderer reporting it hidden. // The renderer reports again once a new guest registers. const hideDestroyedGuest = Effect.fnUntraced(function* (webContentsId: number) { for (const tab of (yield* SynchronizedRef.get(tabsRef)).values()) { if (tab.webContentsId === webContentsId && tab.serverTab) { browserHost.setShown(tab.serverTab, false); + browserHost.setCaptureDrawn(tab.serverTab, false); } } }); @@ -3476,7 +3515,8 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function data: Uint8Array, ) { const [createdAt, millis] = yield* Effect.all([currentIso, currentMillis]); - const id = `browser-recording-${millis.toString(36)}`; + const uuid = yield* random.uuidV4(); + const id = `browser-recording-${millis.toString(36)}-${uuid.slice(0, 8)}`; const extension = recordingFileExtension(mimeType); const artifactPath = path.join(resolvedArtifactDirectory, `${id}.${extension}`); yield* fileSystem.makeDirectory(resolvedArtifactDirectory, { recursive: true }).pipe( @@ -3490,7 +3530,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function }), ), ); - yield* fileSystem.writeFile(artifactPath, data).pipe( + yield* fileSystem.writeFile(artifactPath, data, { flag: "wx" }).pipe( Effect.mapError( (cause) => new PreviewOperationError({ @@ -3688,6 +3728,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function setAnnotationSendEnabled, setAudioMuted, setShown, + setCaptureDrawn, setColorScheme, setMainWindow, setForwardedShortcuts: (shortcuts: ReadonlyArray) => @@ -3904,6 +3945,7 @@ export class PreviewManager extends Context.Service< audioMuted: boolean, ) => Effect.Effect; readonly setShown: (tabId: string, shown: boolean) => Effect.Effect; + readonly setCaptureDrawn: (tabId: string, drawn: boolean) => Effect.Effect; readonly openDevTools: (tabId: string) => Effect.Effect; readonly clearCookies: ( partitions?: ReadonlyArray, @@ -4066,6 +4108,7 @@ export const make = Effect.gen(function* PreviewManagerMake() { setZoomFactor: operations.setZoomFactor, setAudioMuted: operations.setAudioMuted, setShown: operations.setShown, + setCaptureDrawn: operations.setCaptureDrawn, openDevTools: operations.openDevTools, clearCookies: Effect.fn("PreviewManager.clearCookies")(function* (partitions) { yield* browserSession diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts index 88c45f37cc467..7808682dfd82b 100644 --- a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts +++ b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts @@ -2,6 +2,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices"; import { assert, describe, it } from "@effect/vitest"; import * as HostProcess from "@t3tools/shared/HostProcess"; +import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; import * as Fiber from "effect/Fiber"; import * as Deferred from "effect/Deferred"; @@ -62,6 +63,26 @@ function makeProcess(output: string, exitCode = 0): ChildProcessSpawner.ChildPro }); } +function makeSlowProcess( + output: string, + delay: Duration.Input, +): ChildProcessSpawner.ChildProcessHandle { + const stdout = Stream.fromEffect(Effect.as(Effect.sleep(delay), textEncoder.encode(output))); + return ChildProcessSpawner.makeHandle({ + pid: ChildProcessSpawner.ProcessId(125), + stdout, + stderr: Stream.empty, + all: stdout, + exitCode: Effect.as(Effect.sleep(delay), ChildProcessSpawner.ExitCode(0)), + isRunning: Effect.succeed(true), + kill: () => Effect.void, + stdin: Sink.drain, + getInputFd: () => Sink.drain, + getOutputFd: () => Stream.empty, + unref: Effect.succeed(Effect.void), + }); +} + function makeHungProcess(): ChildProcessSpawner.ChildProcessHandle { return ChildProcessSpawner.makeHandle({ pid: ChildProcessSpawner.ProcessId(124), @@ -486,7 +507,7 @@ describe("DesktopShellEnvironment", () => { Effect.gen(function* () { const env: NodeJS.ProcessEnv = { SHELL: "/bin/zsh", - PATH: "/Users/test/.local/bin:/usr/bin", + PATH: " /Users/test/.local/bin:/usr/bin", }; const commands: ChildProcess.Command[] = []; @@ -496,7 +517,7 @@ describe("DesktopShellEnvironment", () => { handler: (command) => { commands.push(command); return envOutput({ - PATH: "/opt/homebrew/bin:/usr/bin", + PATH: "/opt/homebrew/bin:/usr/bin:/opt/bin ", SSH_AUTH_SOCK: "/tmp/secretive.sock", HOMEBREW_PREFIX: "/opt/homebrew", }); @@ -505,7 +526,7 @@ describe("DesktopShellEnvironment", () => { assert.equal(commands.length, 1); assert.equal(commands[0]?._tag === "StandardCommand" ? commands[0].command : "", "/bin/zsh"); - assert.equal(env.PATH, "/opt/homebrew/bin:/usr/bin:/Users/test/.local/bin"); + assert.equal(env.PATH, "/opt/homebrew/bin:/usr/bin:/opt/bin : /Users/test/.local/bin"); assert.equal(env.SSH_AUTH_SOCK, "/tmp/secretive.sock"); assert.equal(env.HOMEBREW_PREFIX, "/opt/homebrew"); assert.isTrue(prepared); @@ -670,6 +691,129 @@ describe("DesktopShellEnvironment", () => { }), ); + it.effect("retries the user's shell when only the fallback shell answered", () => + Effect.gen(function* () { + const env: NodeJS.ProcessEnv = { SHELL: "/usr/bin/fish", PATH: "/usr/bin" }; + const commands: string[] = []; + let fishReady = false; + yield* runShellEnvironment({ + env, + platform: "linux", + handler: (command) => { + if (command._tag !== "StandardCommand") return ""; + commands.push(command.command); + if (command.command !== "/usr/bin/fish") return envOutput({ PATH: "/usr/bin" }); + return fishReady + ? envOutput({ PATH: "/fish/bin:/usr/bin" }) + : { output: "", exitCode: 7 }; + }, + use: (service) => + Effect.gen(function* () { + assert.isTrue(yield* service.awaitDiscovery); + assert.equal(env.PATH, "/usr/bin"); + // The Linux probe checks the D-Bus socket on the real filesystem, which the clock does not await. + const published = yield* service.updates.pipe( + Stream.filter((snapshot) => snapshot.environment.PATH === "/fish/bin:/usr/bin"), + Stream.runHead, + Effect.forkChild({ startImmediately: true }), + ); + + fishReady = true; + yield* TestClock.adjust("30 seconds"); + assert.isTrue(Option.isSome(yield* Fiber.join(published))); + assert.equal(env.PATH, "/fish/bin:/usr/bin"); + return true; + }), + }).pipe(Effect.provide(Logger.layer([]))); + assert.deepEqual(commands, ["/usr/bin/fish", "/bin/bash", "/usr/bin/fish"]); + + // With no user shell to wait for, the fallback's answer is final. + commands.length = 0; + yield* runShellEnvironment({ + env: { PATH: "/usr/bin" }, + platform: "linux", + handler: (command) => { + if (command._tag === "StandardCommand") commands.push(command.command); + return envOutput({ PATH: "/usr/bin" }); + }, + use: (service) => + Effect.gen(function* () { + assert.isTrue(yield* service.awaitDiscovery); + yield* TestClock.adjust("5 minutes"); + return true; + }), + }).pipe(Effect.provide(Logger.layer([]))); + assert.deepEqual(commands, ["/bin/bash"]); + }), + ); + + it.effect("gives background retries time for a locked or slow shell, then publishes", () => + Effect.gen(function* () { + const env: NodeJS.ProcessEnv = { SHELL: "/bin/zsh", PATH: "/usr/bin" }; + // Startup fails fast, the first retry meets a shell blocked on a lock, the second a slow one. + const spawned = yield* Effect.forEach([0, 1, 2], () => Deferred.make()); + const stopped = yield* Deferred.make(); + let attempts = 0; + yield* runShellEnvironment({ + env, + platform: "darwin", + handler: () => "", + processSpawner: ChildProcessSpawner.make((command) => { + if (command._tag !== "StandardCommand" || command.command !== "/bin/zsh") { + return Effect.succeed(makeProcess("")); + } + const attempt = attempts++; + const spawn = Deferred.succeed(spawned[attempt]!, undefined); + if (attempt === 0) return spawn.pipe(Effect.as(makeProcess(""))); + if (attempt === 1) { + return spawn.pipe( + Effect.andThen( + Effect.acquireRelease(Effect.succeed(makeHungProcess()), () => + Deferred.succeed(stopped, command.options), + ), + ), + ); + } + return spawn.pipe( + Effect.as(makeSlowProcess(envOutput({ PATH: "/fresh/bin" }), "20 seconds")), + ); + }), + use: (service) => + Effect.gen(function* () { + assert.isFalse(yield* service.awaitDiscovery); + const published = yield* service.updates.pipe( + Stream.drop(1), + Stream.runHead, + Effect.forkChild({ startImmediately: true }), + ); + + yield* TestClock.adjust("30 seconds"); + yield* Deferred.await(spawned[1]!); + // pyenv waits up to 60 s on its rehash lock before the shell continues. + yield* TestClock.adjust("1 minute"); + assert.isFalse(yield* Deferred.isDone(stopped)); + yield* TestClock.adjust("1 minute"); + const stop = yield* Deferred.await(stopped); + // pyenv releases that lock in an EXIT trap, which SIGTERM runs and SIGKILL skips. + assert.equal(stop.killSignal, "SIGTERM"); + assert.isAtLeast(Duration.toMillis(stop.forceKillAfter ?? 0), 5_000); + + yield* TestClock.adjust("30 seconds"); + yield* Deferred.await(spawned[2]!); + yield* TestClock.adjust("20 seconds"); + const snapshot = yield* Fiber.join(published); + assert.equal(env.PATH, "/fresh/bin:/usr/bin"); + assert.isTrue(Option.isSome(snapshot) && snapshot.value.discovered); + assert.equal( + Option.isSome(snapshot) ? snapshot.value.environment.PATH : undefined, + "/fresh/bin:/usr/bin", + ); + return true; + }), + }); + }), + ); + it.effect("recovers the login-shell PATH when the first probe outlasts the timeout", () => Effect.gen(function* () { const env: NodeJS.ProcessEnv = { SHELL: "/bin/zsh", PATH: "/usr/bin:/bin:/usr/sbin:/sbin" }; diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.ts b/apps/desktop/src/shell/DesktopShellEnvironment.ts index 299b0902b57a5..351150aa4b987 100644 --- a/apps/desktop/src/shell/DesktopShellEnvironment.ts +++ b/apps/desktop/src/shell/DesktopShellEnvironment.ts @@ -3,6 +3,7 @@ import { applyShellEnvironment, buildEnvironmentCaptureCommand, listLoginShellCandidates, + mergePathValues, readUserLoginShell, setWindowsPath, } from "@t3tools/shared/shell"; @@ -14,6 +15,7 @@ import * as Deferred from "effect/Deferred"; import * as Path from "effect/Path"; import * as PubSub from "effect/PubSub"; import * as Ref from "effect/Ref"; +import * as Schedule from "effect/Schedule"; import * as Scope from "effect/Scope"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; @@ -27,9 +29,17 @@ import * as DesktopEnvironment from "../app/DesktopEnvironment.ts"; type EnvironmentPatch = Record; +interface ProbeBudget { + readonly timeout: Duration.Duration; + // One longer wait for the first login shell that times out. + readonly slowTimeout?: Duration.Duration; + readonly terminateGrace: Duration.Duration; +} + interface ShellEnvironmentConfig { readonly env: NodeJS.ProcessEnv; readonly platform: NodeJS.Platform; + readonly budget: ProbeBudget; } interface WindowsProbeOptions { @@ -124,6 +134,20 @@ const LOGIN_SHELL_TIMEOUT = Duration.seconds(5); const SLOW_LOGIN_SHELL_TIMEOUT = Duration.seconds(20); const LAUNCHCTL_TIMEOUT = Duration.seconds(2); const PROCESS_TERMINATE_GRACE = Duration.seconds(1); +const STARTUP_PROBE: ProbeBudget = { + timeout: LOGIN_SHELL_TIMEOUT, + slowTimeout: SLOW_LOGIN_SHELL_TIMEOUT, + terminateGrace: PROCESS_TERMINATE_GRACE, +}; +// Retries block nothing, so they outlast pyenv's 60 s wait on its rehash lock, and SIGTERM gets +// time to run the exit trap that releases it. A SIGKILL leaves the lock for two minutes. +const RETRY_PROBE: ProbeBudget = { + timeout: Duration.minutes(2), + terminateGrace: Duration.seconds(10), +}; +// A Mac asleep at launch fails the first probe on wake, and nothing else would probe again. +const DISCOVERY_RETRY_DELAY = Duration.seconds(30); +const DISCOVERY_RETRIES = 10; const trimNonEmpty = (value: string | null | undefined): Option.Option => Option.fromNullishOr(value).pipe( @@ -131,10 +155,12 @@ const trimNonEmpty = (value: string | null | undefined): Option.Option = Option.filter((entry) => entry.length > 0), ); -const pathDelimiter = (platform: NodeJS.Platform) => (platform === "win32" ? ";" : ":"); +// POSIX directory names may begin or end with whitespace; mergePathValues trims only on Windows. +const nonEmptyPath = (value: string | null | undefined): Option.Option => + Option.fromNullishOr(value).pipe(Option.filter((entry) => entry.length > 0)); const readEnvPath = (env: NodeJS.ProcessEnv): Option.Option => - trimNonEmpty(env.PATH ?? env.Path ?? env.path); + nonEmptyPath(env.PATH ?? env.Path ?? env.path); const normalizeRuntimeDir = (value: string): string => value.replace(/\/+$/u, ""); @@ -153,39 +179,16 @@ const linuxRuntimeDirCandidates = ( return candidates.filter((candidate) => candidate.length > 0); }; -const pathComparisonKey = (entry: string, platform: NodeJS.Platform) => { - const normalized = entry.trim().replace(/^"+|"+$/g, ""); - return platform === "win32" ? normalized.toLowerCase() : normalized; -}; - -const sanitizePathEntry = (entry: string, platform: NodeJS.Platform) => - platform === "win32" ? entry.replaceAll('"', "") : entry; - const mergePaths = ( platform: NodeJS.Platform, values: ReadonlyArray>, -): Option.Option => { - const delimiter = pathDelimiter(platform); - const entries: string[] = []; - const seen = new Set(); - - for (const value of values) { - if (Option.isNone(value)) continue; - - for (const entry of value.value.split(delimiter)) { - const sanitized = sanitizePathEntry(entry.trim(), platform); - if (sanitized.length === 0) continue; - - const key = pathComparisonKey(sanitized, platform); - if (key.length === 0 || seen.has(key)) continue; - - seen.add(key); - entries.push(sanitized); - } - } - - return entries.length > 0 ? Option.some(entries.join(delimiter)) : Option.none(); -}; +): Option.Option => + Option.fromNullishOr( + values.reduce( + (merged, value) => mergePathValues(merged, Option.getOrUndefined(value), platform), + undefined, + ), + ); const knownWindowsCliDirs = (env: NodeJS.ProcessEnv): ReadonlyArray => [ ...trimNonEmpty(env.APPDATA).pipe( @@ -265,6 +268,7 @@ const runCommandOutput = Effect.fn("desktop.shellEnvironment.runCommandOutput")( readonly args: ReadonlyArray; readonly env: NodeJS.ProcessEnv; readonly timeout: Duration.Duration; + readonly terminateGrace?: Duration.Duration; readonly shell?: boolean; }): Effect.fn.Return< string, @@ -283,7 +287,7 @@ const runCommandOutput = Effect.fn("desktop.shellEnvironment.runCommandOutput")( stdout: "pipe", stderr: "ignore", killSignal: "SIGTERM", - forceKillAfter: PROCESS_TERMINATE_GRACE, + forceKillAfter: input.terminateGrace ?? PROCESS_TERMINATE_GRACE, }), ); return yield* Effect.all([Stream.mkString(Stream.decodeText(child.stdout)), child.exitCode], { @@ -349,6 +353,7 @@ const readLoginShellEnvironment = ( names: ReadonlyArray, env: NodeJS.ProcessEnv, timeout: Duration.Duration, + terminateGrace: Duration.Duration, ): Effect.Effect< EnvironmentPatch, DesktopShellEnvironmentCommandTimeoutError, @@ -362,6 +367,7 @@ const readLoginShellEnvironment = ( args: ["-ilc", buildEnvironmentCaptureCommand(shell, names)], env, timeout, + terminateGrace, }).pipe(Effect.map((output) => extractEnvironment(output, names))); const readLaunchctlPath = (env: NodeJS.ProcessEnv) => @@ -378,6 +384,7 @@ const readWindowsEnvironment = Effect.fn("desktop.shellEnvironment.readWindowsEn names: ReadonlyArray, options: WindowsProbeOptions, env: NodeJS.ProcessEnv, + budget: ProbeBudget, ): Effect.fn.Return { if (names.length === 0) return {}; @@ -395,7 +402,8 @@ const readWindowsEnvironment = Effect.fn("desktop.shellEnvironment.readWindowsEn command, args, env, - timeout: LOGIN_SHELL_TIMEOUT, + timeout: budget.timeout, + terminateGrace: budget.terminateGrace, }).pipe(orEmptyOnTimeout); const environment = extractEnvironment(output, names); if (Object.keys(environment).length > 0) { @@ -413,8 +421,13 @@ const installWindowsEnvironment = Effect.fn("desktop.shellEnvironment.installWin ): Effect.fn.Return { const [noProfile, profile] = yield* Effect.all( [ - readWindowsEnvironment(["PATH"], { loadProfile: false }, config.env), - readWindowsEnvironment(WINDOWS_PROFILE_ENV_NAMES, { loadProfile: true }, config.env), + readWindowsEnvironment(["PATH"], { loadProfile: false }, config.env, config.budget), + readWindowsEnvironment( + WINDOWS_PROFILE_ENV_NAMES, + { loadProfile: true }, + config.env, + config.budget, + ), ], { concurrency: 2 }, ); @@ -438,6 +451,9 @@ const installWindowsEnvironment = Effect.fn("desktop.shellEnvironment.installWin }, ); +const listUserShells = (env: NodeJS.ProcessEnv): ReadonlyArray => + [env.SHELL?.trim(), readUserLoginShell()].filter((shell): shell is string => Boolean(shell)); + const installPosixEnvironment = Effect.fn("desktop.shellEnvironment.installPosixEnvironment")( function* ( config: ShellEnvironmentConfig, @@ -448,22 +464,28 @@ const installPosixEnvironment = Effect.fn("desktop.shellEnvironment.installPosix > { const fileSystem = yield* FileSystem.FileSystem; const shellEnvironment: EnvironmentPatch = {}; - const userShells = [config.env.SHELL?.trim(), readUserLoginShell()]; + const userShells = listUserShells(config.env); let answeredBy: string | undefined; let waitedForSlowShell = false; for (const shell of listLoginShellCandidates(config.platform, config.env.SHELL)) { const read = (timeout: Duration.Duration) => - readLoginShellEnvironment(shell, LOGIN_SHELL_ENV_NAMES, config.env, timeout); + readLoginShellEnvironment( + shell, + LOGIN_SHELL_ENV_NAMES, + config.env, + timeout, + config.budget.terminateGrace, + ); Object.assign( shellEnvironment, - yield* read(LOGIN_SHELL_TIMEOUT).pipe( + yield* read(config.budget.timeout).pipe( Effect.catchTags({ DesktopShellEnvironmentCommandTimeoutError: (error) => { - const retry = !waitedForSlowShell; + const slowTimeout = waitedForSlowShell ? undefined : config.budget.slowTimeout; waitedForSlowShell = true; return logShellEnvironmentCommandError(error).pipe( - Effect.andThen(retry ? read(SLOW_LOGIN_SHELL_TIMEOUT) : Effect.succeed({})), + Effect.andThen(slowTimeout ? read(slowTimeout) : Effect.succeed({})), ); }, }), @@ -484,7 +506,7 @@ const installPosixEnvironment = Effect.fn("desktop.shellEnvironment.installPosix ? yield* readLaunchctlPath(config.env) : Option.none(); const mergedPath = mergePaths(config.platform, [ - trimNonEmpty(shellEnvironment.PATH).pipe(Option.orElse(() => launchctlPath)), + nonEmptyPath(shellEnvironment.PATH).pipe(Option.orElse(() => launchctlPath)), readEnvPath(config.env), ]); @@ -577,7 +599,10 @@ const installPosixEnvironment = Effect.fn("desktop.shellEnvironment.installPosix } const prepared = Option.isSome(trimNonEmpty(shellEnvironment.PATH)); - return { prepared, fromUserShell: prepared && userShells.includes(answeredBy) }; + return { + prepared, + fromUserShell: prepared && answeredBy !== undefined && userShells.includes(answeredBy), + }; }, ); @@ -661,38 +686,60 @@ export const make = Effect.gen(function* () { yield* Ref.set(latest, Option.some(message)); yield* PubSub.publish(changes, message); }); - const refresh = Effect.gen(function* () { - // Refresh from the inherited baseline so removed cached paths and values do not survive. - const fresh = { ...inherited }; - const discovery = yield* installShellEnvironment({ - env: fresh, - platform: environment.platform, - }); - const { prepared } = discovery; - const patch = Object.fromEntries( - Object.keys(ShellEnvironmentPatch.fields).map((name) => [name, fresh[name] ?? null]), - ); - if (prepared || Option.isNone(yield* Ref.get(latest))) yield* publish(patch, prepared); - if (prepared) { - const cached = Object.fromEntries( - CACHED_ENV_NAMES.flatMap((name) => (fresh[name] ? [[name, fresh[name]]] : [])), - ); - const persist = Effect.gen(function* () { - const encoded = yield* encodeCache({ version: 1, key, environment: cached }); - yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true }); - yield* fileSystem.writeFileString(cachePath + ".tmp", encoded, { mode: 0o600 }); - yield* fileSystem.rename(cachePath + ".tmp", cachePath); + const probe = Effect.fn("desktop.shellEnvironment.refresh")( + function* (budget: ProbeBudget) { + // Refresh from the inherited baseline so removed cached paths and values do not survive. + const fresh = { ...inherited }; + const discovery = yield* installShellEnvironment({ + env: fresh, + platform: environment.platform, + budget, }); - yield* persist.pipe( - Effect.catch(() => Effect.logWarning("Could not cache shell environment")), + const { prepared } = discovery; + const patch = Object.fromEntries( + Object.keys(ShellEnvironmentPatch.fields).map((name) => [name, fresh[name] ?? null]), ); - } - return discovery; - }).pipe( + if (prepared || Option.isNone(yield* Ref.get(latest))) yield* publish(patch, prepared); + if (prepared) { + const cached = Object.fromEntries( + CACHED_ENV_NAMES.flatMap((name) => (fresh[name] ? [[name, fresh[name]]] : [])), + ); + const persist = Effect.gen(function* () { + const encoded = yield* encodeCache({ version: 1, key, environment: cached }); + yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true }); + yield* fileSystem.writeFileString(cachePath + ".tmp", encoded, { mode: 0o600 }); + yield* fileSystem.rename(cachePath + ".tmp", cachePath); + }); + yield* persist.pipe( + Effect.catch(() => Effect.logWarning("Could not cache shell environment")), + ); + } + return discovery; + }, Effect.provideService(FileSystem.FileSystem, fileSystem), Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), - Effect.withSpan("desktop.shellEnvironment.refresh"), + ); + // A fallback shell's PATH can miss folders the user's own shell adds, so keep asking that shell. + const awaitsUserShell = listUserShells(inherited).length > 0; + const settled = ({ prepared, fromUserShell }: ShellDiscovery) => + fromUserShell || (prepared && !awaitsUserShell); + const refresh = probe(STARTUP_PROBE).pipe( Effect.onExit((exit) => Deferred.done(discovered, exit)), + Effect.flatMap((discovery) => + settled(discovery) + ? Effect.void + : Effect.sleep(DISCOVERY_RETRY_DELAY).pipe( + Effect.andThen( + probe(RETRY_PROBE).pipe( + Effect.repeat({ + until: settled, + schedule: Schedule.spaced(DISCOVERY_RETRY_DELAY), + times: DISCOVERY_RETRIES - 1, + }), + ), + ), + ), + ), ); const installIntoProcess = yield* Effect.cached( Effect.gen(function* () { diff --git a/apps/desktop/src/updates/DesktopRemoteUpdates.test.ts b/apps/desktop/src/updates/DesktopRemoteUpdates.test.ts index 1f5321148b77b..fdac65f86a595 100644 --- a/apps/desktop/src/updates/DesktopRemoteUpdates.test.ts +++ b/apps/desktop/src/updates/DesktopRemoteUpdates.test.ts @@ -102,6 +102,29 @@ describe("DesktopRemoteUpdates", () => { assert.equal(terminals[0]?.requestId, "req-1"); assert.equal(harness.quitAndInstalls(), 0); + yield* Queue.offer(requests, request("req-retry")); + yield* settle; + assert.deepEqual( + terminalReports(reports).map((report) => report.outcome), + ["ready-to-install", "ready-to-install"], + ); + assert.equal(terminalReports(reports)[1]?.requestId, "req-retry"); + assert.equal(terminalReports(reports)[1]?.preparedRequestId, "req-1"); + assert.equal(harness.quitAndInstalls(), 0); + assert.equal(harness.downloadCount(), 1); + + yield* Queue.offer(requests, { + ...request("req-other-track"), + channel: "nightly", + targetVersion: "1.2.4-nightly.20261027.1", + }); + yield* settle; + assert.equal(terminalReports(reports)[2]?.outcome, "failed"); + assert.equal( + terminalReports(reports)[2]?.reason, + "A prepared desktop update is already in progress.", + ); + yield* Queue.offer(commits, { version: 1, type: "commitDesktopUpdate", @@ -119,6 +142,49 @@ describe("DesktopRemoteUpdates", () => { ); }); + it.effect("does not renew the preparation lease when a retry recovers its token", () => { + const harness = makeHarness(); + + return runRemoteUpdatesTest(harness, ({ reports, requests, commits }) => + Effect.gen(function* () { + harness.emit("update-downloaded", { version: "1.2.4" }); + yield* settle; + yield* Queue.offer(requests, request("req-original")); + yield* settle; + + yield* TestClock.adjust(Duration.minutes(4)); + harness.emit("update-available", { version: "1.2.4" }); + yield* settle; + yield* Queue.offer(requests, request("req-retry")); + yield* settle; + assert.equal(terminalReports(reports)[1]?.preparedRequestId, "req-original"); + + yield* TestClock.adjust(Duration.minutes(1)); + yield* Queue.offer(commits, { + version: 1, + type: "commitDesktopUpdate", + requestId: "req-original", + }); + yield* settle; + assert.equal( + terminalReports(reports)[2]?.reason, + "This desktop update is no longer prepared.", + ); + assert.equal(harness.quitAndInstalls(), 0); + + yield* Queue.offer(requests, request("req-new")); + yield* settle; + yield* Queue.offer(commits, { + version: 1, + type: "commitDesktopUpdate", + requestId: "req-new", + }); + yield* settle; + assert.equal(harness.quitAndInstalls(), 1); + }), + ); + }); + it.effect("switches to the requested track before preparing its release", () => { // In-memory settings keep the channel write off real disk I/O. const harness = makeHarness({ beforeSetUpdateChannel: Effect.void }); @@ -691,7 +757,7 @@ describe("DesktopRemoteUpdates", () => { const backgroundCheck = yield* updates.check("poll").pipe(Effect.forkChild); yield* Deferred.await(backgroundCheckStarted); - assert.equal((yield* updates.getState).status, "checking"); + assert.isTrue(yield* updates.isActionActive); yield* Queue.offer(commits, { version: 1, @@ -701,7 +767,6 @@ describe("DesktopRemoteUpdates", () => { yield* settle; assert.equal(harness.quitAndInstalls(), 0); - harness.emit("update-downloaded", { version: "1.2.4" }); yield* Deferred.succeed(releaseBackgroundCheck, undefined); yield* Fiber.join(backgroundCheck); yield* settle; diff --git a/apps/desktop/src/updates/DesktopRemoteUpdates.ts b/apps/desktop/src/updates/DesktopRemoteUpdates.ts index 560e2e78d4bb1..ce2f23ab3e781 100644 --- a/apps/desktop/src/updates/DesktopRemoteUpdates.ts +++ b/apps/desktop/src/updates/DesktopRemoteUpdates.ts @@ -32,6 +32,8 @@ const PREPARED_UPDATE_TTL = Duration.minutes(5); interface PreparedUpdate { readonly requestId: string; + readonly channel: DesktopTelemetryRequestDesktopUpdate["channel"]; + readonly targetVersion: DesktopTelemetryRequestDesktopUpdate["targetVersion"]; readonly downloadedVersion: string; readonly status: "prepared" | "committing" | "failed"; readonly failureReason?: string; @@ -67,7 +69,11 @@ export const listen: Effect.Effect< const publishReport = ( state: DesktopUpdateState, - terminal?: { readonly outcome: DesktopUpdateRemoteOutcome; readonly reason?: string }, + terminal?: { + readonly outcome: DesktopUpdateRemoteOutcome; + readonly reason?: string; + readonly preparedRequestId?: string; + }, explicitRequestId?: string, ): Effect.Effect => { const reason = normalizeRemoteUpdateReason(terminal?.reason); @@ -82,6 +88,9 @@ export const listen: Effect.Effect< ? { requestId: requestId.value } : {}), ...(terminal === undefined ? {} : { outcome: terminal.outcome }), + ...(terminal?.preparedRequestId === undefined + ? {} + : { preparedRequestId: terminal.preparedRequestId }), ...(reason ? { reason } : {}), state, }), @@ -167,8 +176,22 @@ export const listen: Effect.Effect< : ([current, current] as const), ); if (Option.isSome(prepared) && prepared.value.status !== "failed") { + const state = yield* updates.getState; + if ( + prepared.value.status === "prepared" && + prepared.value.channel === request.channel && + prepared.value.targetVersion === request.targetVersion && + state.downloadedVersion === prepared.value.downloadedVersion + ) { + yield* publishReport( + state, + { outcome: "ready-to-install", preparedRequestId: prepared.value.requestId }, + request.requestId, + ); + return; + } yield* publishReport( - yield* updates.getState, + state, { outcome: "failed", reason: "A prepared desktop update is already in progress.", @@ -301,6 +324,8 @@ export const listen: Effect.Effect< preparedUpdateRef, Option.some({ requestId: request.requestId, + channel: request.channel, + targetVersion: request.targetVersion, downloadedVersion: state.downloadedVersion, status: "prepared", expiresAt: diff --git a/apps/desktop/src/updates/DesktopUpdates.test.ts b/apps/desktop/src/updates/DesktopUpdates.test.ts index 7ee2ff5dd51c1..53bd956ca6da7 100644 --- a/apps/desktop/src/updates/DesktopUpdates.test.ts +++ b/apps/desktop/src/updates/DesktopUpdates.test.ts @@ -1,7 +1,7 @@ import * as NodeModule from "node:module"; import { assert, describe, it } from "@effect/vitest"; -import { DESKTOP_UPDATE_RESTART_MARKER_FILE } from "@t3tools/contracts"; +import { DESKTOP_UPDATE_RESTART_MARKER_FILE, DesktopUpdateStateSchema } from "@t3tools/contracts"; import { computeReleaseNotes } from "electron-updater/out/providers/GitHubProvider.js"; import * as Cause from "effect/Cause"; import * as Deferred from "effect/Deferred"; @@ -13,6 +13,7 @@ import * as Logger from "effect/Logger"; import * as Option from "effect/Option"; import * as References from "effect/References"; 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"; @@ -25,6 +26,8 @@ import * as DesktopUpdatePolicy from "./DesktopUpdatePolicy.ts"; import * as DesktopUpdates from "./DesktopUpdates.ts"; import { flushCallbacks, makeHarness } from "./updatesTestHarness.ts"; +const encodeUpdateState = Schema.encodeEffect(DesktopUpdateStateSchema); + type ReleaseFeed = Parameters[2]; type RunningVersion = Parameters[0]; @@ -125,7 +128,7 @@ describe("DesktopUpdates", () => { ).pipe(Effect.provide(harness.layer)); }); - it.effect("checks for newer releases after an update has been downloaded", () => { + it.effect("re-checks an offered update without announcing it again", () => { const harness = makeHarness(); return Effect.scoped( @@ -138,14 +141,23 @@ describe("DesktopUpdates", () => { releaseNotes: "## What's changed\n- fix: queued update", }); yield* flushCallbacks; + const offeredStates = harness.sentStates.length; + + yield* updates.check("poll"); + harness.emit("update-available", { version: "1.2.4" }); + yield* flushCallbacks; + assert.equal(harness.sentStates.length, offeredStates); + harness.emit("update-downloaded", { version: "1.2.4" }); yield* flushCallbacks; + const downloadedStates = harness.sentStates.length; const result = yield* updates.check("poll"); assert.isTrue(result.checked); harness.emit("update-available", { version: "1.2.4" }); yield* flushCallbacks; + assert.equal(harness.sentStates.length, downloadedStates); const unchangedState = yield* updates.getState; assert.equal(unchangedState.status, "downloaded"); @@ -267,15 +279,26 @@ describe("DesktopUpdates", () => { ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); - it.effect( - "rejects install while a refresh check is in progress and releases the reservation", - () => + it.effect.each([ + { action: "download", offer: "update-available", found: null, accepted: true }, + { action: "install", offer: "update-downloaded", found: null, accepted: true }, + { action: "download", offer: "update-available", found: "1.2.5", accepted: true }, + { action: "install", offer: "update-downloaded", found: "1.2.5", accepted: false }, + ] as const)( + "a $action waiting out a refresh check acts on what it found ($found)", + ({ action, offer, found, accepted }) => Effect.gen(function* () { const checkStarted = yield* Deferred.make(); const releaseCheck = yield* Deferred.make(); const harness = makeHarness({ checkForUpdates: Deferred.succeed(checkStarted, undefined).pipe( Effect.andThen(Deferred.await(releaseCheck)), + // electron-updater emits the check's outcome before its promise resolves. + Effect.andThen( + Effect.sync(() => { + if (found !== null) harness.emit("update-available", { version: found }); + }), + ), ), }); @@ -283,22 +306,20 @@ describe("DesktopUpdates", () => { Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; - harness.emit("update-downloaded", { version: "1.2.4" }); + harness.emit(offer, { version: "1.2.4" }); yield* flushCallbacks; - const checkFiber = yield* updates.check("manual").pipe(Effect.forkScoped); + const checkFiber = yield* updates.check("poll").pipe(Effect.forkScoped); yield* Deferred.await(checkStarted); - - const installResult = yield* updates.install; - assert.isFalse(installResult.accepted); + const actionFiber = yield* updates[action].pipe(Effect.forkScoped); + yield* flushCallbacks; yield* Deferred.succeed(releaseCheck, undefined); - const checkResult = yield* Fiber.join(checkFiber); - assert.isTrue(checkResult.checked); - - const followUpCheck = yield* updates.check("manual"); - assert.isTrue(followUpCheck.checked); - assert.equal(harness.checkCount(), 2); + assert.isTrue((yield* Fiber.join(checkFiber)).checked); + assert.equal((yield* Fiber.join(actionFiber)).accepted, accepted); + const state = yield* updates.getState; + assert.equal(state.availableVersion, found ?? "1.2.4"); + if (action === "download") assert.equal(state.status, "downloading"); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }), @@ -1262,6 +1283,7 @@ describe("DesktopUpdates under a managed policy", () => { assert.equal(held.status, "disabled"); assert.include(held.message ?? "", message); assert.isTrue(held.channelBlockedByPolicy); + assert.isTrue((yield* encodeUpdateState(held)).channelBlockedByPolicy); assert.isFalse((yield* updates.check("menu")).checked); assert.equal(harness.checkCount(), 0); diff --git a/apps/desktop/src/updates/DesktopUpdates.ts b/apps/desktop/src/updates/DesktopUpdates.ts index db31e0bc112ba..055c6574391a6 100644 --- a/apps/desktop/src/updates/DesktopUpdates.ts +++ b/apps/desktop/src/updates/DesktopUpdates.ts @@ -63,7 +63,7 @@ const CHANNEL_CHANGE_RETRY_DELAY = Duration.seconds(1); const CHANNEL_CHANGE_RETRIES = 30; const AUTO_UPDATE_STARTUP_DELAY = "15 seconds"; const AUTO_UPDATE_POLL_INTERVAL = "4 hours"; -const PREPARED_INSTALL_CHECK_WAIT = Duration.seconds(90); +const UPDATE_CHECK_WAIT = Duration.seconds(90); type UpdateAction = "check" | "download" | "install" | "install-recovery" | "channel"; @@ -318,6 +318,10 @@ function getCanRetryFromState(state: DesktopUpdateState): boolean { return state.availableVersion !== null || state.downloadedVersion !== null; } +function isOffered(state: DesktopUpdateState): boolean { + return state.status === "available" || state.status === "downloaded"; +} + function shouldBroadcastDownloadProgress( currentState: DesktopUpdateState, nextPercent: number, @@ -518,6 +522,23 @@ export const make = Effect.gen(function* () { ), ); + // Re-runs admit after each finished action, so a caller waiting on a check decides on its result. + const admitAfterCheck = Effect.fn("desktop.updates.admitAfterCheck")(function* ( + admit: Effect.Effect<"admitted" | "refused" | "wait-for-check">, + ) { + const actionCompletions = yield* PubSub.subscribe(finishedUpdateActions); + let admission = yield* stateMutex.withPermits(1)(admit); + while (admission === "wait-for-check") { + const finishedAction = yield* PubSub.take(actionCompletions).pipe( + Effect.timeoutOption(UPDATE_CHECK_WAIT), + ); + admission = Option.isNone(finishedAction) + ? "refused" + : yield* stateMutex.withPermits(1)(admit); + } + return admission; + }); + const applyAutoUpdaterChannel = Effect.fn("desktop.updates.applyAutoUpdaterChannel")(function* ( channel: DesktopUpdateChannel, ) { @@ -559,8 +580,10 @@ export const make = Effect.gen(function* () { if (actionReservation === "acquire" && !(yield* tryStartUpdateAction("check"))) return false; const check = Effect.gen(function* () { - const checkedAt = yield* currentIsoTimestamp; - yield* setState(reduceDesktopUpdateStateOnCheckStart(state, checkedAt)); + // An offer stays on screen while re-checking, so finding it again changes nothing. + if (!isOffered(state)) { + yield* setState(reduceDesktopUpdateStateOnCheckStart(state, yield* currentIsoTimestamp)); + } yield* logUpdaterInfo("checking for updates", { reason }); // The flag is shared updater state, so it flips only while this check holds the reservation. @@ -597,15 +620,30 @@ export const make = Effect.gen(function* () { }); const downloadAvailableUpdate = Effect.gen(function* () { - const state = yield* Ref.get(updateStateRef); - if (!(yield* Ref.get(updaterConfiguredRef)) || state.status !== "available") { - return { accepted: false, completed: false }; - } - - if (!(yield* tryStartUpdateAction("download"))) { + const admission = yield* Effect.scoped( + admitAfterCheck( + Effect.gen(function* () { + const state = yield* Ref.get(updateStateRef); + if (!(yield* Ref.get(updaterConfiguredRef)) || state.status !== "available") { + return "refused" as const; + } + const activeAction = yield* Ref.get(activeUpdateActionRef); + if (Option.isSome(activeAction)) { + return activeAction.value === "check" + ? ("wait-for-check" as const) + : ("refused" as const); + } + return (yield* tryStartUpdateAction("download")) + ? ("admitted" as const) + : ("refused" as const); + }), + ), + ); + if (admission === "refused") { return { accepted: false, completed: false }; } + const state = yield* Ref.get(updateStateRef); return yield* Effect.gen(function* () { yield* setState(reduceDesktopUpdateStateOnDownloadStart(state)); yield* electronUpdater.setDisableDifferentialDownload( @@ -798,47 +836,35 @@ export const make = Effect.gen(function* () { const installDownloadedUpdate = (expectedVersion?: string) => Effect.scoped( Effect.gen(function* () { - const actionCompletions = yield* PubSub.subscribe(finishedUpdateActions); - let admission: "admitted" | "refused" | "wait-for-check" = "wait-for-check"; - while (admission === "wait-for-check") { - admission = yield* stateMutex.withPermits(1)( - Effect.gen(function* () { - const state = yield* Ref.get(updateStateRef); - const activeAction = yield* Ref.get(activeUpdateActionRef); - const hasExpectedDownload = - state.downloadedVersion !== null && - (expectedVersion === undefined || state.downloadedVersion === expectedVersion); - if ( - (yield* Ref.get(desktopState.quitting)) || - !(yield* Ref.get(updaterConfiguredRef)) || - !hasExpectedDownload - ) { - return "refused" as const; - } - if (Option.isSome(activeAction)) { - return activeAction.value === "check" && expectedVersion !== undefined - ? ("wait-for-check" as const) - : ("refused" as const); - } - const hasInstallableDownload = - state.status === "downloaded" || - (state.status === "error" && - (state.errorContext === null || state.errorContext === "install")); - if (!hasInstallableDownload) return "refused" as const; - return (yield* tryStartUpdateAction("install")) - ? ("admitted" as const) + const admission = yield* admitAfterCheck( + Effect.gen(function* () { + const state = yield* Ref.get(updateStateRef); + const activeAction = yield* Ref.get(activeUpdateActionRef); + const hasExpectedDownload = + state.downloadedVersion !== null && + (expectedVersion === undefined || state.downloadedVersion === expectedVersion); + if ( + (yield* Ref.get(desktopState.quitting)) || + !(yield* Ref.get(updaterConfiguredRef)) || + !hasExpectedDownload + ) { + return "refused" as const; + } + if (Option.isSome(activeAction)) { + return activeAction.value === "check" + ? ("wait-for-check" as const) : ("refused" as const); - }), - ); - if (admission === "wait-for-check") { - const finishedAction = yield* PubSub.take(actionCompletions).pipe( - Effect.timeoutOption(PREPARED_INSTALL_CHECK_WAIT), - ); - if (Option.isNone(finishedAction)) { - admission = "refused"; } - } - } + const hasInstallableDownload = + state.status === "downloaded" || + (state.status === "error" && + (state.errorContext === null || state.errorContext === "install")); + if (!hasInstallableDownload) return "refused" as const; + return (yield* tryStartUpdateAction("install")) + ? ("admitted" as const) + : ("refused" as const); + }), + ); if (admission === "refused") { return { accepted: false, completed: false, failed: false }; } @@ -982,6 +1008,7 @@ export const make = Effect.gen(function* () { yield* Ref.set(lastLoggedDownloadMilestoneRef, -1); return; } + if (isOffered(state) && state.availableVersion === info.version) return; const checkedAt = yield* currentIsoTimestamp; const { releaseNotes, omittedReleaseCount } = normalizeDesktopUpdateReleaseNotes( @@ -1161,6 +1188,8 @@ export const make = Effect.gen(function* () { ), configure: Effect.gen(function* () { const context = yield* Effect.context(); + // Runs synchronously up to the first await, so a check's outcome is applied before the + // check's promise resolves and wakes the actions waiting on it. const runEffect = (effect: Effect.Effect) => { void Effect.runPromiseWith(context)(effect); }; diff --git a/apps/desktop/src/window/DesktopApplicationMenu.test.ts b/apps/desktop/src/window/DesktopApplicationMenu.test.ts index 612ec441ba99b..46004ca302295 100644 --- a/apps/desktop/src/window/DesktopApplicationMenu.test.ts +++ b/apps/desktop/src/window/DesktopApplicationMenu.test.ts @@ -3,7 +3,9 @@ import { assert, describe, it } from "@effect/vitest"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; +import type { DesktopUpdateCheckResult, DesktopUpdateState } from "@t3tools/contracts"; import * as Electron from "electron"; import { vi } from "vite-plus/test"; @@ -18,6 +20,7 @@ import * as DesktopApplicationMenu from "./DesktopApplicationMenu.ts"; import * as DesktopConfig from "../app/DesktopConfig.ts"; import * as DesktopEnvironment from "../app/DesktopEnvironment.ts"; import * as DesktopUpdates from "../updates/DesktopUpdates.ts"; +import { createInitialDesktopUpdateState } from "../updates/updateMachine.ts"; import * as DesktopWindow from "./DesktopWindow.ts"; const environmentInput = { @@ -56,27 +59,37 @@ const layerElectronApp = Layer.succeed(ElectronApp.ElectronApp, { on: () => Effect.void, } satisfies ElectronApp.ElectronApp["Service"]); -const layerElectronDialog = Layer.succeed(ElectronDialog.ElectronDialog, { - pickFolder: () => Effect.succeedNone, - pickFiles: () => Effect.succeed([]), - showMessageBox: () => Effect.succeed({ response: 0, checkboxChecked: false }), - showErrorBox: () => Effect.void, -} satisfies ElectronDialog.ElectronDialog["Service"]); - -const layerDesktopUpdates = Layer.succeed(DesktopUpdates.DesktopUpdates, { - getState: Effect.die("unexpected getState"), - isActionActive: Effect.succeed(false), - isInstallActive: Effect.succeed(false), - subscribe: Effect.die("unexpected subscribe"), - emitState: Effect.void, - disabledReason: Effect.succeedNone, - configure: Effect.void, - setChannel: () => Effect.die("unexpected setChannel"), - check: () => Effect.die("unexpected check"), - download: Effect.die("unexpected download"), - install: Effect.die("unexpected install"), - installPrepared: () => Effect.die("unexpected installPrepared"), -} satisfies DesktopUpdates.DesktopUpdates["Service"]); +interface MenuServices { + readonly checkResult?: DesktopUpdateCheckResult; + readonly shownMessageBox?: Deferred.Deferred; +} + +const layerElectronDialog = ({ shownMessageBox }: MenuServices) => + Layer.succeed(ElectronDialog.ElectronDialog, { + pickFolder: () => Effect.succeedNone, + pickFiles: () => Effect.succeed([]), + showMessageBox: (options) => + (shownMessageBox ? Deferred.succeed(shownMessageBox, options) : Effect.void).pipe( + Effect.as({ response: options.cancelId ?? 0, checkboxChecked: false }), + ), + showErrorBox: () => Effect.void, + } satisfies ElectronDialog.ElectronDialog["Service"]); + +const layerDesktopUpdates = ({ checkResult }: MenuServices) => + Layer.succeed(DesktopUpdates.DesktopUpdates, { + getState: Effect.die("unexpected getState"), + isActionActive: Effect.succeed(false), + isInstallActive: Effect.succeed(false), + subscribe: Effect.die("unexpected subscribe"), + emitState: Effect.void, + disabledReason: Effect.succeedNone, + configure: Effect.void, + setChannel: () => Effect.die("unexpected setChannel"), + check: () => (checkResult ? Effect.succeed(checkResult) : Effect.die("unexpected check")), + download: Effect.die("unexpected download"), + install: Effect.die("unexpected install"), + installPrepared: () => Effect.die("unexpected installPrepared"), + } satisfies DesktopUpdates.DesktopUpdates["Service"]); const layerDesktopWindow = (selectedAction: Deferred.Deferred) => Layer.succeed(DesktopWindow.DesktopWindow, { @@ -113,6 +126,7 @@ const configureMenu = ( selectedAction: Deferred.Deferred, applicationMenuTemplate: Deferred.Deferred, environment: Partial = {}, + services: MenuServices = {}, ) => Effect.gen(function* () { const menu = yield* DesktopApplicationMenu.DesktopApplicationMenu; @@ -122,8 +136,8 @@ const configureMenu = ( DesktopApplicationMenu.layer.pipe( Layer.provideMerge(layerElectronMenu(applicationMenuTemplate)), Layer.provideMerge(layerDesktopWindow(selectedAction)), - Layer.provideMerge(layerDesktopUpdates), - Layer.provideMerge(layerElectronDialog), + Layer.provideMerge(layerDesktopUpdates(services)), + Layer.provideMerge(layerElectronDialog(services)), Layer.provideMerge(layerElectronApp), Layer.provideMerge( DesktopEnvironment.layer({ ...environmentInput, ...environment }).pipe( @@ -134,6 +148,54 @@ const configureMenu = ( ), ); +const checkForUpdatesFromMacMenu = (state: Partial) => + Effect.gen(function* () { + const applicationMenuTemplate = + yield* Deferred.make(); + const shownMessageBox = yield* Deferred.make(); + yield* configureMenu( + yield* Deferred.make(), + applicationMenuTemplate, + { platform: "darwin" }, + { + checkResult: { + checked: true, + state: { + ...createInitialDesktopUpdateState( + "0.1.5", + { hostArch: "arm64", appArch: "arm64", runningUnderArm64Translation: false }, + "latest", + ), + enabled: true, + ...state, + }, + }, + shownMessageBox, + }, + ); + + const appMenu = (yield* Deferred.await(applicationMenuTemplate))[0]; + if (!Array.isArray(appMenu?.submenu)) { + throw new Error("Expected the macOS app menu submenu to be an array."); + } + const checkForUpdates = appMenu.submenu.find((item) => item.label === "Check for Updates..."); + if (typeof checkForUpdates?.click !== "function") { + throw new Error("Expected Check for Updates... menu item to have a click handler."); + } + + // Every stub completes synchronously, so the menu action has finished when click returns. + checkForUpdates.click( + {} as Electron.MenuItem, + {} as Electron.BrowserWindow, + {} as KeyboardEvent, + ); + const dialog = yield* Deferred.poll(shownMessageBox); + if (Option.isNone(dialog)) { + throw new Error("Check for Updates... finished without showing any dialog."); + } + return yield* dialog.value; + }); + describe("DesktopApplicationMenu", () => { it.effect("leaves the accelerator to Chromium instead of injecting a paste", () => Effect.gen(function* () { @@ -285,4 +347,25 @@ describe("DesktopApplicationMenu", () => { assert.equal(yield* Deferred.await(selectedAction), "main-reload"); }), ); + it.effect("tells the user which update the menu check found", () => + Effect.gen(function* () { + const dialog = yield* checkForUpdatesFromMacMenu({ + status: "available", + availableVersion: "0.1.7", + }); + assert.include(`${dialog.title ?? ""} ${dialog.message} ${dialog.detail ?? ""}`, "0.1.7"); + }), + ); + + it.effect("tells the user a downloaded update is ready when the menu check runs", () => + Effect.gen(function* () { + const dialog = yield* checkForUpdatesFromMacMenu({ + status: "downloaded", + availableVersion: "0.1.7", + downloadedVersion: "0.1.7", + downloadPercent: 100, + }); + assert.include(`${dialog.title ?? ""} ${dialog.message} ${dialog.detail ?? ""}`, "0.1.7"); + }), + ); }); diff --git a/apps/desktop/src/window/DesktopApplicationMenu.ts b/apps/desktop/src/window/DesktopApplicationMenu.ts index 44d2c234f8358..b4025ebfde8ef 100644 --- a/apps/desktop/src/window/DesktopApplicationMenu.ts +++ b/apps/desktop/src/window/DesktopApplicationMenu.ts @@ -64,6 +64,9 @@ const runMainContentsCommand = Effect.fn("desktop.menu.runMainContentsCommand")( yield* desktopWindow.runMainContentsCommand(command); }); +const describeUpdate = (version: string | null) => + version === null ? "A new version of T3 Code" : `T3 Code ${version}`; + const checkForUpdatesFromMenu = Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; const electronDialog = yield* ElectronDialog.ElectronDialog; @@ -77,6 +80,35 @@ const checkForUpdatesFromMenu = Effect.gen(function* () { message: `T3 Code ${updateState.currentVersion} is currently the newest version available.`, buttons: ["OK"], }); + } else if (updateState.status === "available") { + const { response } = yield* electronDialog.showMessageBox({ + type: "info", + title: "Update available", + message: `${describeUpdate(updateState.availableVersion)} is available.`, + detail: `You're running T3 Code ${updateState.currentVersion}.`, + buttons: ["Download", "Later"], + defaultId: 0, + cancelId: 1, + }); + if (response === 0) yield* updates.download; + } else if (updateState.status === "downloading") { + yield* electronDialog.showMessageBox({ + type: "info", + title: "Update downloading", + message: `${describeUpdate(updateState.availableVersion)} is already downloading.`, + buttons: ["OK"], + }); + } else if (updateState.status === "downloaded") { + const { response } = yield* electronDialog.showMessageBox({ + type: "info", + title: "Update ready", + message: `${describeUpdate(updateState.downloadedVersion ?? updateState.availableVersion)} is ready to install.`, + detail: "T3 Code will restart to install it. Any running tasks will be interrupted.", + buttons: ["Restart", "Later"], + defaultId: 0, + cancelId: 1, + }); + if (response === 0) yield* updates.install; } else if (updateState.status === "error") { yield* electronDialog.showMessageBox({ type: "warning", diff --git a/apps/desktop/src/window/DesktopWindow.test.ts b/apps/desktop/src/window/DesktopWindow.test.ts index 0ddcc7946d32b..f66586503e24d 100644 --- a/apps/desktop/src/window/DesktopWindow.test.ts +++ b/apps/desktop/src/window/DesktopWindow.test.ts @@ -246,6 +246,7 @@ function layerTest(input: { readonly previewZoomReapplies?: number[]; readonly onReveal?: (window: Electron.BrowserWindow) => void; readonly focusedWindow?: Electron.BrowserWindow; + readonly fallbackWindow?: Electron.BrowserWindow; readonly platform?: NodeJS.Platform; readonly appearanceWindows?: ReadonlyArray<{ readonly window: Electron.BrowserWindow; @@ -299,7 +300,9 @@ function layerTest(input: { Effect.as(input.window), ), main: Ref.get(input.mainWindow), - currentMainOrFirst: Ref.get(input.mainWindow), + currentMainOrFirst: Ref.get(input.mainWindow).pipe( + Effect.map(Option.orElse(() => Option.fromNullishOr(input.fallbackWindow ?? null))), + ), focusedMainOrFirst: input.focusedWindow === undefined ? Ref.get(input.mainWindow) @@ -844,7 +847,7 @@ describe("DesktopWindow", () => { }), ); - it.effect("reloads and inspects the main window even when a popup has focus", () => + it.effect("routes menu commands to the main window even when a popup has focus", () => Effect.gen(function* () { const fakeWindow = makeFakeBrowserWindow(); const popup = makeFakeBrowserWindow(); @@ -864,7 +867,10 @@ describe("DesktopWindow", () => { yield* desktopWindow.runMainContentsCommand("reload"); yield* desktopWindow.runMainContentsCommand("forceReload"); yield* desktopWindow.runMainContentsCommand("toggleDevTools"); + yield* desktopWindow.dispatchMenuAction("open-settings"); + assert.deepEqual(fakeWindow.send.mock.calls, [["desktop:menu-action", "open-settings"]]); + assert.equal(popup.send.mock.calls.length, 0); assert.equal(fakeWindow.reload.mock.calls.length, 1); assert.equal(fakeWindow.reloadIgnoringCache.mock.calls.length, 1); assert.equal(fakeWindow.toggleDevTools.mock.calls.length, 1); @@ -875,6 +881,38 @@ describe("DesktopWindow", () => { }), ); + it.effect("recreates main for Settings when only an unregistered popup survives", () => + Effect.gen(function* () { + const main = makeFakeBrowserWindow(); + const auxiliary = makeFakeBrowserWindow(); + const createCount = yield* Ref.make(0); + const mainWindow = yield* Ref.make>(Option.none()); + + yield* Effect.gen(function* () { + const windows = yield* DesktopWindow.DesktopWindow; + yield* windows.dispatchMenuAction("open-settings"); + assert.deepEqual(main.send.mock.calls, [["desktop:menu-action", "open-settings"]]); + assert.equal(auxiliary.send.mock.calls.length, 0); + assert.equal(yield* Ref.get(createCount), 1); + assert.strictEqual(yield* windows.ensureMain, main.window); + assert.equal(yield* Ref.get(createCount), 1); + }).pipe( + Effect.provide( + layerTest({ + window: main.window, + createCount, + mainWindow, + fallbackWindow: auxiliary.window, + desktopSettings: { + ...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS, + localEnvironmentEnabled: false, + }, + }), + ), + ); + }), + ); + it.effect("does not open main or splash windows once quitting has started", () => Effect.gen(function* () { const fake = makeFakeBrowserWindow(); diff --git a/apps/desktop/src/window/DesktopWindow.ts b/apps/desktop/src/window/DesktopWindow.ts index 93b35eb9b1a32..7be14bc720838 100644 --- a/apps/desktop/src/window/DesktopWindow.ts +++ b/apps/desktop/src/window/DesktopWindow.ts @@ -425,7 +425,6 @@ export const make = Effect.gen(function* () { ); const currentMainWindow = electronWindow.currentMainOrFirst.pipe(Effect.flatMap(withoutSplash)); - const focusedMainWindow = electronWindow.focusedMainOrFirst.pipe(Effect.flatMap(withoutSplash)); const zoomWindow = Effect.fn("desktop.window.zoomWindow")(function* ( window: Electron.BrowserWindow, @@ -1019,7 +1018,7 @@ export const make = Effect.gen(function* () { const ensureMain = Effect.gen(function* () { if (yield* Ref.get(desktopState.quitting)) return Option.none(); - const existingWindow = yield* currentMainWindow; + const existingWindow = yield* electronWindow.main; if (Option.isSome(existingWindow)) { return existingWindow; } @@ -1106,7 +1105,8 @@ export const make = Effect.gen(function* () { { reveal = true }: { readonly reveal?: boolean } = {}, ) { if (yield* Ref.get(desktopState.quitting)) return; - const existingWindow = yield* reveal ? focusedMainWindow : electronWindow.main; + // Only the main renderer loads the app preload that listens on these channels. + const existingWindow = yield* electronWindow.main; if (Option.isNone(existingWindow) && (!reveal || (yield* waitingForBackend))) return; const window = Option.isSome(existingWindow) ? existingWindow : yield* ensureMain; if (Option.isNone(window) || window.value.isDestroyed()) return; diff --git a/apps/desktop/src/wsl/DesktopWslEnvironment.test.ts b/apps/desktop/src/wsl/DesktopWslEnvironment.test.ts index 0787956a72b76..7f4a2507a736e 100644 --- a/apps/desktop/src/wsl/DesktopWslEnvironment.test.ts +++ b/apps/desktop/src/wsl/DesktopWslEnvironment.test.ts @@ -172,7 +172,7 @@ describe.skipIf(posixShellRunner === null)("WSL runtime install script (executed " }", ' eval "$2"', "); }", - buildWslRuntimeProbeScript(fixture.runtimeRoot), + buildWslRuntimeProbeScript(), ].join("\n"), ); @@ -207,7 +207,7 @@ describe.skipIf(posixShellRunner === null)("WSL runtime install script (executed [ "bash() { return 127; }", 'export PATH="/fixture/bin:/usr/bin:/bin"', - buildWslRuntimeProbeScript(fixture.runtimeRoot), + buildWslRuntimeProbeScript(), ].join("\n"), ); diff --git a/apps/desktop/src/wsl/DesktopWslEnvironment.ts b/apps/desktop/src/wsl/DesktopWslEnvironment.ts index 4e5e5b423f20e..5ba7d3794fad1 100644 --- a/apps/desktop/src/wsl/DesktopWslEnvironment.ts +++ b/apps/desktop/src/wsl/DesktopWslEnvironment.ts @@ -110,10 +110,7 @@ export class DesktopWslEnvironment extends Context.Service< ) => Effect.Effect; readonly pruneRuntimes: (distro: string | null, runtimeId: string) => Effect.Effect; readonly invalidateRuntime: (distro: string | null, runtimeId: string) => Effect.Effect; - readonly probeRuntime: ( - distro: string | null, - linuxAppRoot: string, - ) => Effect.Effect; + readonly probeRuntime: (distro: string | null) => Effect.Effect; readonly ensureNodePty: ( distro: string | null, linuxAppRoot: string, @@ -483,11 +480,9 @@ require("node-pty"); NODE`; // Resolve version-managed Node for providers without making runtime readiness depend on it. -export const buildWslRuntimeProbeScript = (linuxAppRoot: string) => - [ - `bash -lc ${shellQuote(`${buildWslNodeEnvPreamble()}${RESOLVED_PATH_LINE}`)} 2>/dev/null || ${RESOLVED_PATH_LINE}`, - `${shellQuote(`${linuxAppRoot}/t3`)} --version >/dev/null 2>&1`, - ].join("\n"); +// The install script already ran the staged t3, so running it again here only adds a second startup. +export const buildWslRuntimeProbeScript = () => + `bash -lc ${shellQuote(`${buildWslNodeEnvPreamble()}${RESOLVED_PATH_LINE}`)} 2>/dev/null || ${RESOLVED_PATH_LINE}`; const TOOLCHAIN_CHECK_SCRIPT = [ "for tool in node make g++ python3; do", @@ -601,30 +596,17 @@ const formatMissingToolsReason = ( const probeWslRuntimeImpl = Effect.fn("DesktopWslEnvironment.probeWslRuntimeImpl")(function* ( distro: string | null, - linuxAppRoot: string, ): Effect.fn.Return { - const probe = yield* runWslShell( - distro, - buildWslRuntimeProbeScript(linuxAppRoot), - PROBE_TIMEOUT, - { - resolveNode: false, - }, - ); + const probe = yield* runWslShell(distro, buildWslRuntimeProbeScript(), PROBE_TIMEOUT, { + resolveNode: false, + }); const transportFailureReason = formatWslShellTransportFailureReason( probe.transportFailure, - "the staged runtime", + "the login-shell PATH", ); if (transportFailureReason !== null) { return { ok: false, reason: transportFailureReason } as const; } - if (probe.exitCode !== 0) { - const trimmedTail = probe.stderr.trim().slice(-500); - return { - ok: false, - reason: `${linuxAppRoot}/t3 --version failed (exit ${probe.exitCode})${trimmedTail ? `: ${trimmedTail}` : ""}`, - } as const; - } const resolvedPath = parseResolvedPath(probe.stdout); if (resolvedPath === null) { return { @@ -1175,7 +1157,7 @@ export interface DesktopWslEnvironmentTestStub { ) => PrepareWslRuntimeResult; readonly pruneRuntimes?: (distro: string | null, runtimeId: string) => Effect.Effect; readonly invalidateRuntime?: (distro: string | null, runtimeId: string) => Effect.Effect; - readonly probeRuntime?: (distro: string | null, linuxAppRoot: string) => ProbeWslRuntimeResult; + readonly probeRuntime?: (distro: string | null) => ProbeWslRuntimeResult; readonly ensureNodePty?: ( distro: string | null, linuxAppRoot: string, @@ -1222,16 +1204,14 @@ export const layerTest = (stub: DesktopWslEnvironmentTestStub = {}) => { return stub.invalidateRuntime?.(distro, runtimeId) ?? Effect.void; }, ), - probeRuntime: Effect.fn("DesktopWslEnvironment.probeRuntime")( - function (distro, linuxAppRoot) { - return Effect.succeed( - stub.probeRuntime?.(distro, linuxAppRoot) ?? { - ok: true, - resolvedPath: "/usr/bin:/bin", - }, - ); - }, - ), + probeRuntime: Effect.fn("DesktopWslEnvironment.probeRuntime")(function (distro) { + return Effect.succeed( + stub.probeRuntime?.(distro) ?? { + ok: true, + resolvedPath: "/usr/bin:/bin", + }, + ); + }), ensureNodePty: Effect.fn("DesktopWslEnvironment.ensureNodePty")( function (distro, linuxAppRoot, options) { return Effect.succeed( @@ -1321,8 +1301,8 @@ export const layer = Layer.effect( invalidateRuntime: Effect.fn("desktop.wsl.invalidateRuntime")(function (distro, runtimeId) { return provideSpawner(invalidateWslRuntimeImpl(distro, runtimeId)); }), - probeRuntime: Effect.fn("desktop.wsl.probeRuntime")(function (distro, linuxAppRoot) { - return provideSpawner(probeWslRuntimeImpl(distro, linuxAppRoot)); + probeRuntime: Effect.fn("desktop.wsl.probeRuntime")(function (distro) { + return provideSpawner(probeWslRuntimeImpl(distro)); }), ensureNodePty: Effect.fn("desktop.wsl.ensureNodePty")( function (distro, linuxAppRoot, options) { diff --git a/apps/marketing/src/lib/releases.test.ts b/apps/marketing/src/lib/releases.test.ts new file mode 100644 index 0000000000000..6527342d674e5 --- /dev/null +++ b/apps/marketing/src/lib/releases.test.ts @@ -0,0 +1,18 @@ +import { afterEach, describe, expect, it, vi } from "vite-plus/test"; + +import { fetchLatestRelease } from "./releases"; + +afterEach(() => { + vi.unstubAllGlobals(); +}); + +describe("fetchLatestRelease", () => { + it("rejects a GitHub API error response instead of returning it as a release", async () => { + vi.stubGlobal("sessionStorage", { getItem: () => null }); + vi.stubGlobal("fetch", async () => + Response.json({ message: "API rate limit exceeded" }, { status: 403 }), + ); + + await expect(fetchLatestRelease("stable")).rejects.toThrow("403"); + }); +}); diff --git a/apps/marketing/src/lib/releases.ts b/apps/marketing/src/lib/releases.ts index 7dc63bdaed859..f50103972cdd8 100644 --- a/apps/marketing/src/lib/releases.ts +++ b/apps/marketing/src/lib/releases.ts @@ -25,12 +25,18 @@ function cacheKey(channel: ReleaseChannel) { return `t3code-${channel}-release`; } +async function fetchJson(url: string): Promise { + const response = await fetch(url); + if (!response.ok) throw new Error(`GitHub API responded ${response.status}`); + return response.json(); +} + async function fetchStable(): Promise { - return fetch(LATEST_API_URL).then((r) => r.json()); + return fetchJson(LATEST_API_URL); } async function fetchNightly(): Promise { - const list: Release[] = await fetch(LIST_API_URL).then((r) => r.json()); + const list = await fetchJson(LIST_API_URL); const nightly = Array.isArray(list) ? list.find((release) => release.tag_name?.includes("-nightly.")) : undefined; diff --git a/apps/marketing/src/pages/[...locale]/index.astro b/apps/marketing/src/pages/[...locale]/index.astro index 3d8d44568d448..e8f4b5f1b5fa5 100644 --- a/apps/marketing/src/pages/[...locale]/index.astro +++ b/apps/marketing/src/pages/[...locale]/index.astro @@ -1234,6 +1234,8 @@ const mobileEndorsementRows = [ section { padding: 56px 0; } .sec-cta { padding: 80px 0; } .container { padding-left: 20px; padding-right: 20px; } + /* A nowrap tag widens its column past the grid, which then clips the other column. */ + .harness-tag { white-space: normal; } .endorsements-head { margin-bottom: 40px; } @@ -1265,4 +1267,8 @@ const mobileEndorsementRows = [ font-size: 11px; } } + + @media (max-width: 360px) { + .harness { padding-left: 12px; padding-right: 12px; } + } diff --git a/apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift b/apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift index 96551d05bbb55..7bc50b694df5f 100644 --- a/apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift +++ b/apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift @@ -1,7 +1,8 @@ import ExpoModulesCore import UIKit +import UniformTypeIdentifiers -private struct ComposerTokenPayload: Decodable { +private struct ComposerTokenPayload: Decodable, Equatable { let type: String let source: String let label: String @@ -9,8 +10,16 @@ private struct ComposerTokenPayload: Decodable { let accent: String? let tintsIcon: Bool? let detail: String? - let start: Int - let end: Int + var start: Int + var end: Int + + func rendersLike(_ other: Self) -> Bool { + var moved = other + moved.start = start + moved.end = end + // String == accepts canonically equivalent sources, but their UTF-16 lengths differ. + return moved == self && source.utf16.elementsEqual(other.source.utf16) + } } private struct ComposerSelectionPayload: Decodable, Equatable { @@ -301,18 +310,28 @@ private final class ComposerTextView: UITextView { func loadImages(from providers: [NSItemProvider]) { let group = DispatchGroup() let lock = NSLock() - var images = [UIImage?](repeating: nil, count: providers.count) + var images = [(data: Data, fileExtension: String)?](repeating: nil, count: providers.count) for (index, provider) in providers.enumerated() { group.enter() - provider.loadObject(ofClass: UIImage.self) { object, _ in - defer { group.leave() } - guard let image = object as? UIImage else { - return - } + let store = { (image: (data: Data, fileExtension: String)?) in lock.lock() images[index] = image lock.unlock() + group.leave() + } + let loadPNG = { + _ = provider.loadObject(ofClass: UIImage.self) { object, _ in + store((object as? UIImage)?.pngData().map { ($0, "png") }) + } + } + // Decoding a GIF to UIImage keeps only its first frame, so pass its bytes through. + if provider.hasItemConformingToTypeIdentifier(UTType.gif.identifier) { + _ = provider.loadDataRepresentation(forTypeIdentifier: UTType.gif.identifier) { data, _ in + if let data { store((data, "gif")) } else { loadPNG() } + } + } else { + loadPNG() } } @@ -320,7 +339,9 @@ private final class ComposerTextView: UITextView { guard let self, !self.isReadOnly else { return } - let urls = images.compactMap { $0 }.compactMap(Self.writeTemporaryImage) + let urls = images.compactMap { $0 }.compactMap { + Self.writeTemporaryImage($0.data, fileExtension: $0.fileExtension) + } if !urls.isEmpty { self.onPasteImages?(urls) } @@ -400,9 +421,10 @@ private final class ComposerTextView: UITextView { } private static func writeTemporaryImage(_ image: UIImage) -> String? { - guard let data = image.pngData() else { - return nil - } + image.pngData().flatMap { writeTemporaryImage($0, fileExtension: "png") } + } + + private static func writeTemporaryImage(_ data: Data, fileExtension: String) -> String? { let directory = FileManager.default.temporaryDirectory .appendingPathComponent(pastedImageDirectoryName, isDirectory: true) do { @@ -411,7 +433,7 @@ private final class ComposerTextView: UITextView { withIntermediateDirectories: true ) removeStaleTemporaryImages(in: directory) - let url = directory.appendingPathComponent("\(UUID().uuidString).png") + let url = directory.appendingPathComponent("\(UUID().uuidString).\(fileExtension)") try data.write(to: url, options: .atomic) return url.absoluteString } catch { @@ -714,8 +736,10 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro hasControlledDocument = true if tokensJson != document.tokensJson { tokensJson = document.tokensJson - tokens = decode([ComposerTokenPayload].self, from: document.tokensJson) ?? [] - tokensNeedRebuild = true + let nextTokens = decode([ComposerTokenPayload].self, from: document.tokensJson) ?? [] + // Typing before a chip only moves its offsets; rebuilding then makes iOS autocorrect behind the caret. + tokensNeedRebuild = tokensNeedRebuild || !nextTokens.elementsEqual(tokens, by: { $0.rendersLike($1) }) + tokens = nextTokens } let previousSelection = sourceSelection() value = document.value @@ -739,6 +763,7 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro func setPlaceholder(_ placeholder: String) { placeholderLabel.text = placeholder + textView.accessibilityLabel = placeholder setNeedsLayout() } @@ -1330,7 +1355,7 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro private func documentMatchesExpectedTokens() -> Bool { let source = value as NSString - let expectedSources = tokens.compactMap { token -> String? in + let expectedChips = tokens.compactMap { token -> (Int, String)? in guard token.start >= 0, token.end > token.start, token.end <= source.length, @@ -1339,18 +1364,22 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro ) == token.source else { return nil } - return token.source + return (token.start, token.source) } - var renderedSources: [String] = [] + var renderedChips: [(Int, String)] = [] + var sourceOffset = 0 textView.attributedText.enumerateAttribute( .attachment, in: NSRange(location: 0, length: textView.attributedText.length) - ) { value, _, _ in + ) { value, range, _ in if let attachment = value as? ComposerTextAttachment { - renderedSources.append(attachment.source) + renderedChips.append((sourceOffset, attachment.source)) + sourceOffset += (attachment.source as NSString).length + } else { + sourceOffset += range.length } } - return renderedSources == expectedSources + return renderedChips.elementsEqual(expectedChips, by: ==) } } diff --git a/apps/mobile/modules/t3-composer-editor/tests/ios/main.swift b/apps/mobile/modules/t3-composer-editor/tests/ios/main.swift index 4435b1022161d..e9751eec843ac 100644 --- a/apps/mobile/modules/t3-composer-editor/tests/ios/main.swift +++ b/apps/mobile/modules/t3-composer-editor/tests/ios/main.swift @@ -11,13 +11,16 @@ func check(_ passed: Bool, _ message: String) { extension T3ComposerEditorView { var editor: UITextView { textView } var text: String { textView.serializedText() } + func chip(at location: Int) -> NSTextAttachment? { + textView.textStorage.attribute(.attachment, at: location, effectiveRange: nil) as? NSTextAttachment + } } -func document(_ value: String, revision: Int, resetGeneration: Int = 0, isNativeEcho: Bool = false) -> String { +func document(_ value: String, revision: Int, resetGeneration: Int = 0, isNativeEcho: Bool = false, tokensJson: String = "[]") -> String { let payload: [String: Any] = [ "value": value, "selection": NSNull(), - "tokensJson": "[]", + "tokensJson": tokensJson, "mostRecentEventCount": revision, "isNativeEcho": isNativeEcho, "resetGeneration": resetGeneration, @@ -33,10 +36,14 @@ func replace(_ view: T3ComposerEditorView, _ target: String, with replacement: S editor.replace(editor.textRange(from: start, to: end)!, withText: replacement) } -func makeEditor(_ value: String) -> T3ComposerEditorView { +func mention(_ label: String, at start: Int, source: String = "@README.md") -> String { + #"[{"type":"mention","source":"\#(source)","label":"\#(label)","start":\#(start),"end":\#(start + source.utf16.count)}]"# +} + +func makeEditor(_ value: String, tokensJson: String = "[]") -> T3ComposerEditorView { let view = T3ComposerEditorView() view.frame = CGRect(x: 0, y: 0, width: 320, height: 200) - view.setControlledDocumentJson(document(value, revision: 0)) + view.setControlledDocumentJson(document(value, revision: 0, tokensJson: tokensJson)) check(view.text == value, "Initial controlled value was not applied") return view } @@ -108,6 +115,37 @@ do { ) } +// An echo that only shifts a chip keeps the rendered text; moving or relabeling the chip redraws it. +do { + let draft = "hi! @README.md @README.md" + let view = makeEditor("hi @README.md @README.md", tokensJson: mention("README.md", at: 3)) + let rendered = view.chip(at: 3) + check(rendered != nil, "Harness could not render the chip") + view.editor.selectedRange = NSRange(location: 2, length: 0) + view.editor.insertText("!") + let revision = view.onComposerChange.payloads.last?["eventCount"] as? Int ?? 0 + view.setControlledDocumentJson(document(draft, revision: revision, isNativeEcho: true, tokensJson: mention("README.md", at: 4))) + check(view.chip(at: 4) === rendered, "An echo that only moved a chip replaced the text") + view.setControlledDocumentJson(document(draft, revision: revision, tokensJson: mention("Readme", at: 4))) + check((view.chip(at: 4) as? ComposerTextAttachment)?.label == "Readme", "A relabeled chip was not redrawn") + view.setControlledDocumentJson(document(draft, revision: revision, tokensJson: mention("Readme", at: 15))) + check(view.chip(at: 4) == nil && view.chip(at: 15) != nil, "A chip moved to another copy of its source stayed on the old one") +} + +// A chip source that changes only in Unicode normalization is rebuilt from the new text. +do { + let view = makeEditor("@\u{00E9}", tokensJson: mention("e", at: 0, source: "@\u{00E9}")) + view.setControlledDocumentJson(document("@e\u{0301}", revision: 0, tokensJson: mention("e", at: 0, source: "@e\u{0301}"))) + check(view.text.utf16.elementsEqual("@e\u{0301}".utf16), "A renormalized chip source kept the old text") +} + +// The placeholder label is hidden from VoiceOver, so the text view must carry it. +do { + let view = makeEditor("") + view.setPlaceholder("Ask anything") + check(view.editor.accessibilityLabel == "Ask anything", "VoiceOver label: \(String(describing: view.editor.accessibilityLabel))") +} + print("composer editor tests passed") private let dropEditor = ComposerTextView() @@ -174,6 +212,25 @@ for readOnly in [true, false] { check(provider.loadCount == 0, "Locked inputs must not load pasted image providers") } +// Two-frame animated GIF: a mixed paste keeps the GIF's original bytes and the still image's PNG, in order. +private let gifBytes = Data(base64Encoded: "R0lGODlhAQABAIEAAP8AAAAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh+QQACgAAACwAAAAAAQABAAAIBAABBAQAIfkEAAoAAAAsAAAAAAEAAQCBAP8AAAAAAAAAAAAACAQAAQQEADs=")! +private let gifProvider = NSItemProvider() +gifProvider.registerDataRepresentation(forTypeIdentifier: UTType.gif.identifier, visibility: .all) { completion in + completion(gifBytes, nil) + return nil +} +private let gifEditor = ComposerTextView() +gifEditor.onPasteImages = { urls in + let files = urls.compactMap(URL.init(string:)) + let contents = files.compactMap { try? Data(contentsOf: $0) } + files.forEach { try? FileManager.default.removeItem(at: $0) } + check(files.map(\.pathExtension) == ["gif", "png"], "A mixed paste must keep each image's format, in order: \(urls)") + check(contents.first == gifBytes, "A pasted animated GIF must keep its original bytes") + check(contents.count == 2 && contents[1] == image.pngData(), "The still image beside a GIF must still attach as PNG") + print("Composer GIF-paste checks passed") + exit(0) +} + // Inline provider completion places the attachment callback before this main-queue check. DispatchQueue.main.async { check(pastedImages.count == 1, "An accepted provider-based paste must emit exactly one attachment batch") @@ -190,6 +247,7 @@ DispatchQueue.main.async { "The attachment must contain the pasted image's pixels") check(pasteEditor.text == "Keep this draft", "Image paste must preserve the draft text") print("Composer provider-paste checks passed") - exit(0) + check(gifProvider.canLoadObject(ofClass: UIImage.self), "A GIF provider must take the image paste path") + gifEditor.paste(itemProviders: [gifProvider, ImmediateImageProvider(image: image)]) } dispatchMain() diff --git a/apps/mobile/modules/t3-native-controls/ios/T3NativeControlsModule.swift b/apps/mobile/modules/t3-native-controls/ios/T3NativeControlsModule.swift index af67e80afebb4..5b3d84aedcee3 100644 --- a/apps/mobile/modules/t3-native-controls/ios/T3NativeControlsModule.swift +++ b/apps/mobile/modules/t3-native-controls/ios/T3NativeControlsModule.swift @@ -10,6 +10,9 @@ public final class T3NativeControlsModule: Module { private let presentationSources = T3PresentationSources() private var videoPresentation: T3NativeVideoPresentation? private var filePresentation: T3NativeFilePresentation? + /// The last file preview that closed. Written on main, read synchronously from JS. + private let filePreviewActivity = NSLock() + private var finishedFilePreview: String? public func definition() -> ModuleDefinition { Events("videoPlaybackChange") @@ -94,6 +97,12 @@ public final class T3NativeControlsModule: Module { file.present(urls: urls, titles: titles, selectedIndex: selectedIndex, from: presenter) }.runOnQueue(.main) + // A tap can reach JS before the old viewer's close does; this tells it whether that viewer is gone. + // A presentation main has not started yet reads as still open. + Function("isFilePreviewFinished") { (identifier: String) -> Bool in + self.filePreviewActivity.withLock { self.finishedFilePreview == identifier } + } + // A nil URL marks the item as one that could not be resolved. AsyncFunction("updateFile") { (identifier: String, index: Int, url: URL?) in if self.filePresentation?.identifier == identifier { self.filePresentation?.update(index: index, url: url) } @@ -294,15 +303,22 @@ public final class T3NativeControlsModule: Module { guard filePresentation == nil, videoPresentation == nil, appContext?.utilities?.currentViewController() != nil else { throw URLError(.cannotLoadFromNetwork) } - let file = T3NativeFilePresentation(identifier: identifier, sources: presentationSources, - sourceIdentifiers: sourceIdentifiers) { [weak self] error in - self?.filePresentation = nil - if let error { promise.reject(error) } else { promise.resolve(nil) } - } + let file = T3NativeFilePresentation( + identifier: identifier, sources: presentationSources, sourceIdentifiers: sourceIdentifiers, + onFinish: { [weak self] in self?.setFinishedFilePreview(identifier) }, + completion: { [weak self] error in + self?.filePresentation = nil + if let error { promise.reject(error) } else { promise.resolve(nil) } + } + ) filePresentation = file return file } + private func setFinishedFilePreview(_ identifier: String) { + filePreviewActivity.withLock { finishedFilePreview = identifier } + } + private func presentFile(url: URL, title: String, sourceIdentifier: String, identifier: String, promise: Promise) throws { let file = try makeFilePresentation(sourceIdentifiers: [sourceIdentifier], identifier: identifier, promise: promise) diff --git a/apps/mobile/modules/t3-native-controls/ios/T3NativeFilePresentation.swift b/apps/mobile/modules/t3-native-controls/ios/T3NativeFilePresentation.swift index 791e39b2840c0..979e87edc1b0e 100644 --- a/apps/mobile/modules/t3-native-controls/ios/T3NativeFilePresentation.swift +++ b/apps/mobile/modules/t3-native-controls/ios/T3NativeFilePresentation.swift @@ -8,232 +8,217 @@ private final class FilePreviewItem: NSObject, QLPreviewItem { var previewItemTitle: String? } -private enum FilePreviewState { case loading, ready, failed } - private final class FilePreviewController: QLPreviewController { var onAppear: (() -> Void)? - // Quick Look shows a blank page for an item without a file, so loading and failure are ours to show. - private let spinner = UIActivityIndicatorView(style: .large) - private let failure = UILabel() - var state = FilePreviewState.ready { - didSet { if isViewLoaded { showState() } } - } - - override func viewDidLoad() { - super.viewDidLoad() - failure.text = "This image could not be loaded." - failure.textColor = .secondaryLabel - failure.textAlignment = .center - failure.numberOfLines = 0 - for status in [spinner, failure] as [UIView] { - status.isUserInteractionEnabled = false - status.translatesAutoresizingMaskIntoConstraints = false - view.addSubview(status) - NSLayoutConstraint.activate([ - status.centerXAnchor.constraint(equalTo: view.centerXAnchor), - status.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 64), - status.widthAnchor.constraint(lessThanOrEqualTo: view.widthAnchor, constant: -48), - ]) - } - showState() - } - - override func viewDidLayoutSubviews() { - super.viewDidLayoutSubviews() - view.bringSubviewToFront(spinner) - view.bringSubviewToFront(failure) - } + var onDismiss: (() -> Void)? override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) onAppear?() } - private func showState() { - if state == .loading { spinner.startAnimating() } else { spinner.stopAnimating() } - failure.isHidden = state != .failed + // Quick Look's own didDismiss callback can lag well behind the screen being tappable again, + // so a tap on the thumbnail in that gap would land while JS still thinks the viewer is open. + override func viewDidDisappear(_ animated: Bool) { + super.viewDidDisappear(animated) + if isBeingDismissed { onDismiss?() } } } +/// How long the gallery waits for siblings once the selected file is ready; later ones are left out. +private let siblingGraceNanoseconds: UInt64 = 1_000_000_000 +/// A load faster than this zooms straight from the thumbnail without a spinner. +private let spinnerDelayNanoseconds: UInt64 = 250_000_000 + +/// Quick Look only ever receives pages whose file is on disk: a page refreshed after it first +/// rendered blank keeps a stale scroll offset, so drags scroll the image instead of paging or dismissing. final class T3NativeFilePresentation: NSObject, QLPreviewControllerDataSource, QLPreviewControllerDelegate, UIAdaptivePresentationControllerDelegate { let identifier: String private var controller: FilePreviewController? private let completion: (Error?) -> Void + private let onFinish: () -> Void private weak var sources: T3PresentationSources? private let sourceIdentifiers: [String] - private var items: [FilePreviewItem] = [] + private var titles: [String] = [] private var urls: [URL?] = [] - private var states: [FilePreviewState] = [] + /// A prepared file, or nil while loading or after failing; `settled` tells those apart. + private var files: [URL?] = [] + private var settled: [Bool] = [] + private var downloads: [Int: Task] = [:] private var selectedIndex = 0 + /// The pages Quick Look shows, as indices into `files`. + private var pages: [Int] = [] + private var items: [FilePreviewItem] = [] private weak var presenter: UIViewController? - /// Files that arrived while their page was off screen; Quick Look may still hold the blank page. - private var staleIndices = Set() - private var currentIndexObservation: NSKeyValueObservation? - private var download: (index: Int, task: Task)? - private var placeholder: Task? + private var grace: Task? + private var spinnerDelay: Task? + private var spinner: UIView? private var openError: Error? private var dismissRequested = false private var finished = false init(identifier: String, sources: T3PresentationSources, sourceIdentifiers: [String], - completion: @escaping (Error?) -> Void) { + onFinish: @escaping () -> Void, completion: @escaping (Error?) -> Void) { self.identifier = identifier self.sources = sources self.sourceIdentifiers = sourceIdentifiers + self.onFinish = onFinish self.completion = completion super.init() } /// Downloads the file before presenting so Quick Look can refuse a format the caller should share instead. func present(url: URL, title: String, from presenter: UIViewController) { - load(urls: [url], titles: [title], selectedIndex: 0, from: presenter, opensWhileLoading: false) + present(urls: [url], titles: [title], selectedIndex: 0, from: presenter) } - /// Opens the selected image once it is ready, or a loading page if it is slow, and loads the rest - /// of the gallery behind it so a slow or broken sibling never holds the viewer back. + /// Loads the gallery in parallel and opens once the selected file is ready, with whichever + /// siblings are ready by the end of a short grace, so a slow or broken one never holds it back. func present(urls: [URL?], titles: [String], selectedIndex: Int, from presenter: UIViewController) { - load(urls: urls, titles: titles, selectedIndex: selectedIndex, from: presenter, opensWhileLoading: true) + self.titles = titles + self.urls = urls + self.selectedIndex = selectedIndex + self.presenter = presenter + files = Array(repeating: nil, count: urls.count) + settled = Array(repeating: false, count: urls.count) + spinnerDelay = Task { @MainActor [self] in + try? await Task.sleep(nanoseconds: spinnerDelayNanoseconds) + guard !Task.isCancelled else { return } + showSpinner() + } + for index in urls.indices { download(index) } } /// Delivers a URL that was still resolving when the gallery opened, or nil when it never will. func update(index: Int, url: URL?) { - guard !finished, urls.indices.contains(index), urls[index] == nil, states[index] == .loading else { return } + guard !finished, controller == nil, urls.indices.contains(index), urls[index] == nil, + !settled[index] + else { return } guard let url else { - failed(index, error: URLError(.resourceUnavailable)) + settle(index, result: .failure(URLError(.resourceUnavailable))) return } urls[index] = url - loadNext() - } - - private func load(urls: [URL?], titles: [String], selectedIndex: Int, from presenter: UIViewController, - opensWhileLoading: Bool) { - items = titles.map { title in - let item = FilePreviewItem() - item.previewItemTitle = title - return item - } - self.urls = urls - self.selectedIndex = selectedIndex - self.presenter = presenter - states = Array(repeating: .loading, count: urls.count) - if opensWhileLoading { - // A short grace keeps the zoom from the thumbnail into a loaded image when the file is quick. - placeholder = Task { @MainActor [self] in - try? await Task.sleep(nanoseconds: 250_000_000) - guard !Task.isCancelled, !finished, controller == nil else { return } - show() - } - } - loadNext() + download(index) } - /// Downloads one file at a time, nearest the page on screen first. The page on screen takes over - /// from a background download, which restarts later. - private func loadNext() { - guard !finished, !dismissRequested else { return } - let current = controller?.currentPreviewItemIndex ?? selectedIndex - guard let next = urls.indices - .filter({ urls[$0] != nil && states[$0] == .loading }) - .min(by: { abs($0 - current) < abs($1 - current) }) - else { return } - if let download { - guard next == current, download.index != current else { return } - download.task.cancel() - } - guard let url = urls[next] else { return } - let title = items[next].previewItemTitle ?? "" - let task = Task { @MainActor [self] in + private func download(_ index: Int) { + guard let url = urls[index], downloads[index] == nil else { return } + let title = titles[index] + downloads[index] = Task { @MainActor [self] in let result: Result do { result = .success(try await Self.prepareFile(url: url, title: title)) } catch { result = .failure(error) } - guard !finished, !Task.isCancelled else { - if case .success(let file) = result { - try? FileManager.default.removeItem(at: file.deletingLastPathComponent()) - } - return - } - download = nil - switch result { - case .success(let file): loaded(next, file: file) - case .failure(let error): failed(next, error: error) - } - loadNext() + downloads[index] = nil + settle(index, result: result) } - download = (next, task) } - private func loaded(_ index: Int, file: URL) { - // Quick Look knows which formats it renders; refuse before presenting so the caller - // can fall back instead of showing an "unsupported format" page. - guard QLPreviewController.canPreview(file as NSURL) else { - try? FileManager.default.removeItem(at: file.deletingLastPathComponent()) - failed(index, error: NSError( - domain: "T3NativePresentation", - code: 3, - userInfo: [NSLocalizedDescriptionKey: "This file type cannot be previewed on this device."] - )) + private func settle(_ index: Int, result: Result) { + guard !finished, controller == nil, !dismissRequested else { + if case .success(let file) = result { Self.remove(file) } return } - items[index].previewItemURL = file - states[index] = .ready - if controller != nil { refresh(index) } else if index == selectedIndex { show() } - } - - private func failed(_ index: Int, error: Error) { - guard index != selectedIndex else { + settled[index] = true + if let error = store(index, result: result), index == selectedIndex { openError = error dismiss() return } - states[index] = .failed - refresh(index) + guard files[selectedIndex] != nil else { return } + if settled.allSatisfy({ $0 }) { + show() + } else if grace == nil { + grace = Task { @MainActor [self] in + try? await Task.sleep(nanoseconds: siblingGraceNanoseconds) + guard !Task.isCancelled else { return } + show() + } + } + } + + private func store(_ index: Int, result: Result) -> Error? { + switch result { + case .success(let file): + // Quick Look knows which formats it renders; refuse before presenting so the caller + // can fall back instead of showing an "unsupported format" page. + guard QLPreviewController.canPreview(file as NSURL) else { + Self.remove(file) + return NSError( + domain: "T3NativePresentation", + code: 3, + userInfo: [NSLocalizedDescriptionKey: "This file type cannot be previewed on this device."] + ) + } + files[index] = file + return nil + case .failure(let error): + return error + } } private func show() { + guard !finished, controller == nil, !dismissRequested else { return } guard let presenter else { finish(); return } - placeholder?.cancel() + cancelLoading() + pages = files.indices.filter { files[$0] != nil } + items = pages.map { index in + let item = FilePreviewItem() + item.previewItemURL = files[index] + item.previewItemTitle = titles[index] + return item + } let preview = FilePreviewController() preview.delegate = self preview.dataSource = self - preview.currentPreviewItemIndex = selectedIndex + preview.currentPreviewItemIndex = pages.firstIndex(of: selectedIndex) ?? 0 preview.onAppear = { [weak self] in self?.resumePendingDismissal() } + preview.onDismiss = { [weak self] in self?.finish() } controller = preview - preview.state = states[selectedIndex] - currentIndexObservation = preview.observe(\.currentPreviewItemIndex) { [weak self] _, _ in - DispatchQueue.main.async { self?.currentItemChanged() } - } presenter.present(preview, animated: !UIAccessibility.isReduceMotionEnabled) { [self] in resumePendingDismissal() } preview.presentationController?.delegate = self } - private func refresh(_ index: Int) { - guard let controller else { return } - guard controller.currentPreviewItemIndex == index else { - if states[index] == .ready { staleIndices.insert(index) } - return - } - if states[index] == .ready { controller.refreshCurrentPreviewItem() } - controller.state = states[index] + /// Shows over the tapped thumbnail that a slow open is on its way. + private func showSpinner() { + guard !finished, controller == nil, spinner == nil, + sourceIdentifiers.indices.contains(selectedIndex), + let source = sources?.view(for: sourceIdentifiers[selectedIndex]), + let window = source.window + else { return } + let backing = UIView(frame: CGRect(x: 0, y: 0, width: 44, height: 44)) + backing.backgroundColor = UIColor.black.withAlphaComponent(0.55) + backing.layer.cornerRadius = 22 + backing.isUserInteractionEnabled = false + let indicator = UIActivityIndicatorView(style: .medium) + indicator.color = .white + indicator.center = CGPoint(x: 22, y: 22) + indicator.startAnimating() + backing.addSubview(indicator) + backing.center = source.convert(CGPoint(x: source.bounds.midX, y: source.bounds.midY), to: window) + window.addSubview(backing) + spinner = backing } - private func currentItemChanged() { - guard !finished, let controller, states.indices.contains(controller.currentPreviewItemIndex) else { return } - let index = controller.currentPreviewItemIndex - if staleIndices.remove(index) != nil { controller.refreshCurrentPreviewItem() } - controller.state = states[index] - loadNext() + private func cancelLoading() { + for task in downloads.values { task.cancel() } + downloads.removeAll() + grace?.cancel() + grace = nil + spinnerDelay?.cancel() + spinnerDelay = nil + spinner?.removeFromSuperview() + spinner = nil } func dismiss() { dismissRequested = true - download?.task.cancel() guard !finished else { return } guard let controller else { finish(); return } // Drain Close from viewDidAppear after opening or cancelling an interactive dismissal. @@ -257,10 +242,10 @@ final class T3NativeFilePresentation: NSObject, QLPreviewControllerDataSource, private func sourceView(for item: QLPreviewItem) -> UIView? { guard !UIAccessibility.isReduceMotionEnabled, - let index = items.firstIndex(where: { $0 === item }), - sourceIdentifiers.indices.contains(index) + let page = items.firstIndex(where: { $0 === item }), + sourceIdentifiers.indices.contains(pages[page]) else { return nil } - return sources?.view(for: sourceIdentifiers[index]) + return sources?.view(for: sourceIdentifiers[pages[page]]) } func previewController(_ controller: QLPreviewController, transitionViewFor item: QLPreviewItem) -> UIView? { @@ -278,24 +263,20 @@ final class T3NativeFilePresentation: NSObject, QLPreviewControllerDataSource, func presentationControllerDidDismiss(_ presentationController: UIPresentationController) { finish() } - private func finish(error: Error? = nil) { + private func finish() { guard !finished else { return } finished = true - download?.task.cancel() - download = nil - placeholder?.cancel() - placeholder = nil - currentIndexObservation = nil - for item in items { - if let file = item.previewItemURL { - try? FileManager.default.removeItem(at: file.deletingLastPathComponent()) - } - } + cancelLoading() + for file in files.compactMap({ $0 }) { Self.remove(file) } + files.removeAll() items.removeAll() - urls.removeAll() - states.removeAll() - let result = error ?? openError - DispatchQueue.main.async { [completion] in completion(result) } + DispatchQueue.main.async { [completion, openError] in completion(openError) } + // After queueing the completion, so a new presentation JS starts on seeing this lands behind it. + onFinish() + } + + private static func remove(_ file: URL) { + try? FileManager.default.removeItem(at: file.deletingLastPathComponent()) } /// Copy original bytes so preview and sharing do not mutate a draft or workspace file. @@ -305,6 +286,7 @@ final class T3NativeFilePresentation: NSObject, QLPreviewControllerDataSource, try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) do { let download = directory.appendingPathComponent("original") + var mimeType: String? if url.isFileURL { try FileManager.default.copyItem(at: url, to: download) } else if url.scheme == "data" { @@ -317,6 +299,7 @@ final class T3NativeFilePresentation: NSObject, QLPreviewControllerDataSource, guard let response = response as? HTTPURLResponse, (200..<300).contains(response.statusCode) else { throw URLError(.badServerResponse) } + mimeType = response.mimeType try FileManager.default.moveItem(at: temporaryFile, to: download) } try Task.checkCancellation() @@ -330,7 +313,8 @@ final class T3NativeFilePresentation: NSObject, QLPreviewControllerDataSource, type = detectedType } else if CGPDFDocument(download as CFURL) != nil { type = .pdf - } else if originalExtension.lowercased() == "svg" { + } else if originalExtension.lowercased() == "svg" || mimeType == "image/svg+xml" { + // ImageIO cannot read SVG, and served images such as QR codes often have no extension. type = .svg } let fileExtension: String diff --git a/apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/ReviewDiffCanvasDrawing.kt b/apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/ReviewDiffCanvasDrawing.kt index 7a1225cd392bf..2f1e4d9fd4a43 100644 --- a/apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/ReviewDiffCanvasDrawing.kt +++ b/apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/ReviewDiffCanvasDrawing.kt @@ -402,7 +402,7 @@ internal class ReviewDiffCanvasDrawing(context: Context) { @Suppress("LongParameterList") fun drawCode( canvas: Canvas, - content: String, + row: DiffRow, tokens: List?, codeX: Float, baseline: Float, @@ -417,12 +417,16 @@ internal class ReviewDiffCanvasDrawing(context: Context) { canvas.restore() return } - val runs = if (tokens.isNullOrEmpty()) listOf(DiffToken(content, null, 0)) else tokens + val runs = if (tokens.isNullOrEmpty()) listOf(DiffToken(row.content, null, 0)) else tokens var line = 0 var x = codeX var column = 0 - runs.forEach { run -> - configureCodePaint(run.color ?: theme.text, run.fontStyle, style) + splitAtNote(runs, row.noteStart).forEach { (run, note) -> + configureCodePaint( + if (note) theme.warningText else run.color ?: theme.text, + run.fontStyle, + style, + ) var start = 0 while (start < run.content.length) { while (line + 1 < lines.starts.size && lines.starts[line + 1] <= column + start) { @@ -434,8 +438,10 @@ internal class ReviewDiffCanvasDrawing(context: Context) { if (lineBaseline + textPaint.fontMetrics.descent >= canvas.clipBounds.top && lineBaseline + textPaint.fontMetrics.ascent <= canvas.clipBounds.bottom ) { + val segmentWidth = textPaint.measureText(run.content, start, end) + fillNote(canvas, note, x, x + segmentWidth, lineBaseline) canvas.drawText(run.content, start, end, x, lineBaseline, textPaint) - x += textPaint.measureText(run.content, start, end) + x += segmentWidth } start = end } @@ -443,6 +449,21 @@ internal class ReviewDiffCanvasDrawing(context: Context) { } } + fun noticeColor(row: DiffRow): Int = + if (row.tone == "warning") theme.warningText else theme.mutedText + + fun noticeFill(row: DiffRow): Int = + if (row.tone == "warning") theme.warningBackground else theme.background + + /** Fills behind a segment of a cut line's note; other segments are left alone. */ + private fun fillNote(canvas: Canvas, note: Boolean, left: Float, right: Float, baseline: Float) { + if (!note) return + backgroundPaint.color = theme.warningBackground + val metrics = textPaint.fontMetrics + val top = baseline + metrics.ascent + canvas.drawRect(left, top, right, baseline + metrics.descent, backgroundPaint) + } + fun drawFileHeaderPathScrollFades( canvas: Canvas, pathRect: RectF, @@ -532,3 +553,24 @@ private object ReviewDiffTypefaces { else -> regular } } + +/** Splits color runs where a cut line's note starts; the flag marks the note's runs. */ +private fun splitAtNote(runs: List, noteStart: Int?): List> { + if (noteStart == null) return runs.map { it to false } + return buildList { + var offset = 0 + for (run in runs) { + val end = offset + run.content.length + when { + end <= noteStart -> add(run to false) + offset >= noteStart -> add(run to true) + else -> { + val cut = noteStart - offset + add(run.copy(content = run.content.substring(0, cut)) to false) + add(run.copy(content = run.content.substring(cut)) to true) + } + } + offset = end + } + } +} diff --git a/apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/ReviewDiffCodeLayout.kt b/apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/ReviewDiffCodeLayout.kt index 770bc656de569..83f32788e8ab8 100644 --- a/apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/ReviewDiffCodeLayout.kt +++ b/apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/ReviewDiffCodeLayout.kt @@ -182,6 +182,20 @@ internal class CodeLayoutCache { } } } + row.noteStart?.takeIf { it in 0 until text.length }?.let { start -> + text.setSpan( + ForegroundColorSpan(theme.warningText), + start, + text.length, + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE, + ) + text.setSpan( + BackgroundColorSpan(theme.warningBackground), + start, + text.length, + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE, + ) + } return text } } diff --git a/apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/T3ReviewDiffModule.kt b/apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/T3ReviewDiffModule.kt index 63ab13dde3d12..03609e3658e84 100644 --- a/apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/T3ReviewDiffModule.kt +++ b/apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/T3ReviewDiffModule.kt @@ -4,6 +4,16 @@ import expo.modules.kotlin.Promise import expo.modules.kotlin.modules.Module import expo.modules.kotlin.modules.ModuleDefinition +private val REVIEW_DIFF_EVENTS = arrayOf( + "onDebug", + "onVisibleFileChange", + "onToggleFile", + "onToggleViewedFile", + "onPressLine", + "onToggleComment", + "onPressNotice", +) + class T3ReviewDiffModule : Module() { override fun definition() = ModuleDefinition { Name("T3ReviewDiffSurface") @@ -54,14 +64,7 @@ class T3ReviewDiffModule : Module() { view.setInitialFileId(fileId) } - Events( - "onDebug", - "onVisibleFileChange", - "onToggleFile", - "onToggleViewedFile", - "onPressLine", - "onToggleComment", - ) + Events(REVIEW_DIFF_EVENTS) AsyncFunction("scrollToFile") { view: T3ReviewDiffView, fileId: String, animated: Boolean -> view.scrollToFile(fileId, animated) diff --git a/apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/T3ReviewDiffView.kt b/apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/T3ReviewDiffView.kt index 54ff86c611a8e..91bb6719ed8fa 100644 --- a/apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/T3ReviewDiffView.kt +++ b/apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/T3ReviewDiffView.kt @@ -31,6 +31,7 @@ class T3ReviewDiffView(context: Context, appContext: AppContext) : ExpoView(cont private val onToggleViewedFile by EventDispatcher() private val onPressLine by EventDispatcher() private val onToggleComment by EventDispatcher() + private val onPressNotice by EventDispatcher() private var rows: List = emptyList() private var visibleRows: List = emptyList() private var visibleRowIndices = IntArray(0) @@ -432,6 +433,9 @@ class T3ReviewDiffView(context: Context, appContext: AppContext) : ExpoView(cont } } "comment" -> onToggleComment(mapOf("commentId" to row.id)) + "notice" -> if (gesture == "tap" && row.actionable) { + onPressNotice(mapOf("rowId" to row.id, "fileId" to row.resolvedFileId)) + } "line" -> { val payload = mutableMapOf( "rowId" to row.id, @@ -534,7 +538,13 @@ internal data class DiffRow( val wordDiffRanges: List, val commentText: String, val commentRangeLabel: String, - val commentSectionTitle: String + val commentSectionTitle: String, + /** A notice drawn in the warning colors. */ + val tone: String = "", + /** A notice that reports taps through `onPressNotice`. */ + val actionable: Boolean = false, + /** Where a cut line's note starts in `content`. */ + val noteStart: Int? = null ) { val resolvedFileId: String get() = fileId.ifEmpty { id } val isPlainAscii: Boolean = content.all { it in ' '..'~' } @@ -564,7 +574,9 @@ internal data class DiffTheme( val addBar: Int, val deleteBar: Int, val addText: Int, - val deleteText: Int + val deleteText: Int, + val warningText: Int, + val warningBackground: Int ) { companion object { fun fallback(scheme: String): DiffTheme = if (scheme == "dark") { @@ -582,6 +594,8 @@ internal data class DiffTheme( deleteBar = Color.rgb(255, 46, 63), addText = Color.rgb(94, 204, 113), deleteText = Color.rgb(255, 103, 98), + warningText = Color.rgb(250, 204, 102), + warningBackground = Color.rgb(51, 38, 13), ) } else { DiffTheme( @@ -598,6 +612,8 @@ internal data class DiffTheme( deleteBar = Color.rgb(213, 44, 54), addText = Color.rgb(25, 130, 67), deleteText = Color.rgb(190, 38, 48), + warningText = Color.rgb(140, 92, 0), + warningBackground = Color.rgb(255, 245, 214), ) } @@ -617,6 +633,8 @@ internal data class DiffTheme( deleteBar = color(json, "deleteBar", fallback.deleteBar), addText = color(json, "addText", fallback.addText), deleteText = color(json, "deleteText", fallback.deleteText), + warningText = color(json, "warningText", fallback.warningText), + warningBackground = color(json, "warningBackground", fallback.warningBackground), ) } catch (_: Exception) { fallback @@ -1189,7 +1207,7 @@ private class DiffCanvasView(context: Context) : View(context) { } private fun drawNoticeRow(canvas: Canvas, row: DiffRow, top: Int, bottom: Int) { - fill(canvas, theme.background, 0f, top.toFloat(), width.toFloat(), bottom.toFloat()) + fill(canvas, drawing.noticeFill(row), 0f, top.toFloat(), width.toFloat(), bottom.toFloat()) val iconSize = 16f * density val iconRect = RectF( style.fileHeaderHorizontalPaddingPx + 2f * density, @@ -1197,10 +1215,10 @@ private class DiffCanvasView(context: Context) : View(context) { style.fileHeaderHorizontalPaddingPx + 2f * density + iconSize, (top + bottom + iconSize) / 2f, ) - drawing.drawNoticeIcon(canvas, iconRect, theme.mutedText) + drawing.drawNoticeIcon(canvas, iconRect, drawing.noticeColor(row)) drawing.configureUiPaint( paint = textPaint, - color = theme.mutedText, + color = drawing.noticeColor(row), size = style.fileHeaderSubtextFontSizePx, weight = style.fileHeaderSubtextFontWeight, ) @@ -1313,7 +1331,7 @@ private class DiffCanvasView(context: Context) : View(context) { drawing.configureCodePaint(theme.text, 0, style) drawing.drawWordDiffRanges(canvas, row, codeX, top, firstLineBottom, lines) val baseline = lines.baseline(top, firstLineBottom, textPaint) - drawing.drawCode(canvas, row.content, tokensByRowId[row.id], codeX, baseline, style, lines) + drawing.drawCode(canvas, row, tokensByRowId[row.id], codeX, baseline, style, lines) } drawLineNumber(canvas, row, top, firstLineBottom) @@ -1488,6 +1506,9 @@ private fun parseRows(value: String, isCancelled: () -> Boolean): List commentText = row.optString("commentText"), commentRangeLabel = row.optString("commentRangeLabel"), commentSectionTitle = row.optString("commentSectionTitle"), + tone = row.optString("tone"), + actionable = row.optBoolean("actionable"), + noteStart = row.optNullableInt("noteStart"), ) } } catch (_: Exception) { diff --git a/apps/mobile/modules/t3-review-diff/ios/ReviewDiffCodeLayout.swift b/apps/mobile/modules/t3-review-diff/ios/ReviewDiffCodeLayout.swift index 7f96dc17a25e6..4c60b20c1add5 100644 --- a/apps/mobile/modules/t3-review-diff/ios/ReviewDiffCodeLayout.swift +++ b/apps/mobile/modules/t3-review-diff/ios/ReviewDiffCodeLayout.swift @@ -16,6 +16,13 @@ struct ReviewDiffCodeLineStarts: RandomAccessCollection { /// ASCII uses fixed-pitch columns. TextKit handles shaping, tabs, and Unicode highlights. /// Pass `unboundedWidth` to lay a row out on one line for horizontal scrolling. +/** A cut line's note, drawn in its own colors. */ +struct ReviewDiffCodeNote { + let range: NSRange + let color: UIColor + let background: UIColor +} + final class ReviewDiffCodeLayout: NSObject { static let unboundedWidth = CGFloat.greatestFiniteMagnitude @@ -84,7 +91,14 @@ final class ReviewDiffCodeLayout: NSObject { return layout } - func decorate(text: NSAttributedString, highlights: [NSRange], color: UIColor, version: Int) { + // A cut line's note is not code, so it takes the warning colors over any token color. + func decorate( + text: NSAttributedString, + highlights: [NSRange], + color: UIColor, + note: ReviewDiffCodeNote? = nil, + version: Int + ) { guard usesNativeLayout else { return } let layout = nativeLayout() guard layout.decorationVersion != version else { return } @@ -105,6 +119,13 @@ final class ReviewDiffCodeLayout: NSObject { storage.addAttribute(.backgroundColor, value: color, range: intersection) } } + if let note { + let intersection = NSIntersectionRange(note.range, fullRange) + if intersection.length > 0 { + storage.addAttribute(.foregroundColor, value: note.color, range: intersection) + storage.addAttribute(.backgroundColor, value: note.background, range: intersection) + } + } storage.endEditing() layout.decorationVersion = version } diff --git a/apps/mobile/modules/t3-review-diff/ios/T3ReviewDiffModule.swift b/apps/mobile/modules/t3-review-diff/ios/T3ReviewDiffModule.swift index 2fcb8a4801809..d1be707b0d9d6 100644 --- a/apps/mobile/modules/t3-review-diff/ios/T3ReviewDiffModule.swift +++ b/apps/mobile/modules/t3-review-diff/ios/T3ReviewDiffModule.swift @@ -88,6 +88,7 @@ public class T3ReviewDiffModule: Module { "onToggleViewedFile", "onPressLine", "onToggleComment", + "onPressNotice", "onPullToRefresh" ) diff --git a/apps/mobile/modules/t3-review-diff/ios/T3ReviewDiffView.swift b/apps/mobile/modules/t3-review-diff/ios/T3ReviewDiffView.swift index cc51c40a7e77f..b427c535d6f4c 100644 --- a/apps/mobile/modules/t3-review-diff/ios/T3ReviewDiffView.swift +++ b/apps/mobile/modules/t3-review-diff/ios/T3ReviewDiffView.swift @@ -19,6 +19,12 @@ private struct ReviewDiffNativeRow: Decodable, Sendable { let commentText: String? let commentRangeLabel: String? let commentSectionTitle: String? + /** A notice drawn in the warning colors. */ + let tone: String? + /** A notice that reports taps through `onPressNotice`. */ + let actionable: Bool? + /** Where a cut line's note starts in `content`, in UTF-16 units. */ + let noteStart: Int? } private struct ReviewDiffNativeWordDiffRange: Decodable, Sendable { @@ -52,6 +58,8 @@ private struct ReviewDiffNativeThemePayload: Decodable { let deleteBar: String? let addText: String? let deleteText: String? + let warningText: String? + let warningBackground: String? } private struct ReviewDiffNativeTheme { @@ -68,6 +76,8 @@ private struct ReviewDiffNativeTheme { let deleteBar: UIColor let addText: UIColor let deleteText: UIColor + let warningText: UIColor + let warningBackground: UIColor static func resolve(_ scheme: String) -> ReviewDiffNativeTheme { resolve(scheme, payload: nil) @@ -95,7 +105,10 @@ private struct ReviewDiffNativeTheme { addBar: UIColor(reviewDiffHex: payload.addBar) ?? fallback.addBar, deleteBar: UIColor(reviewDiffHex: payload.deleteBar) ?? fallback.deleteBar, addText: UIColor(reviewDiffHex: payload.addText) ?? fallback.addText, - deleteText: UIColor(reviewDiffHex: payload.deleteText) ?? fallback.deleteText + deleteText: UIColor(reviewDiffHex: payload.deleteText) ?? fallback.deleteText, + warningText: UIColor(reviewDiffHex: payload.warningText) ?? fallback.warningText, + warningBackground: UIColor(reviewDiffHex: payload.warningBackground) + ?? fallback.warningBackground ) } @@ -114,7 +127,9 @@ private struct ReviewDiffNativeTheme { addBar: UIColor(red: 0.02, green: 0.82, blue: 0.54, alpha: 1), deleteBar: UIColor(red: 1.00, green: 0.36, blue: 0.50, alpha: 1), addText: UIColor(red: 0.10, green: 0.84, blue: 0.56, alpha: 1), - deleteText: UIColor(red: 1.00, green: 0.38, blue: 0.52, alpha: 1) + deleteText: UIColor(red: 1.00, green: 0.38, blue: 0.52, alpha: 1), + warningText: UIColor(red: 0.98, green: 0.80, blue: 0.40, alpha: 1), + warningBackground: UIColor(red: 0.20, green: 0.15, blue: 0.05, alpha: 1) ) } @@ -131,7 +146,9 @@ private struct ReviewDiffNativeTheme { addBar: UIColor(red: 0.02, green: 0.80, blue: 0.52, alpha: 1), deleteBar: UIColor(red: 1.00, green: 0.34, blue: 0.48, alpha: 1), addText: UIColor(red: 0.00, green: 0.56, blue: 0.34, alpha: 1), - deleteText: UIColor(red: 0.90, green: 0.10, blue: 0.24, alpha: 1) + deleteText: UIColor(red: 0.90, green: 0.10, blue: 0.24, alpha: 1), + warningText: UIColor(red: 0.55, green: 0.36, blue: 0.00, alpha: 1), + warningBackground: UIColor(red: 1.00, green: 0.96, blue: 0.84, alpha: 1) ) } } @@ -354,6 +371,7 @@ public final class T3ReviewDiffView: ExpoView, UIScrollViewDelegate { let onToggleViewedFile = EventDispatcher() let onPressLine = EventDispatcher() let onToggleComment = EventDispatcher() + let onPressNotice = EventDispatcher() let onPullToRefresh = EventDispatcher() private let pullRefreshControl = UIRefreshControl() @@ -387,6 +405,9 @@ public final class T3ReviewDiffView: ExpoView, UIScrollViewDelegate { contentView.onToggleComment = { [weak self] commentId in self?.onToggleComment(["commentId": commentId]) } + contentView.onPressNotice = { [weak self] payload in + self?.onPressNotice(payload) + } contentView.onDrawMetrics = { [weak self] metrics in self?.emitDebug("draw-metrics", metrics) } @@ -1232,6 +1253,7 @@ private final class ReviewDiffContentView: UIView, UIGestureRecognizerDelegate { var onToggleViewedFile: ((String) -> Void)? var onPressLine: (([String: Any]) -> Void)? var onToggleComment: ((String) -> Void)? + var onPressNotice: (([String: Any]) -> Void)? var onDrawMetrics: (([String: Any]) -> Void)? private var stickyWidth: CGFloat { @@ -1573,6 +1595,11 @@ private final class ReviewDiffContentView: UIView, UIGestureRecognizerDelegate { return } + if row.kind == "notice", row.actionable == true { + onPressNotice?(["rowId": row.id, "fileId": resolvedFileId(for: row)]) + return + } + if row.kind == "line" { onPressLine?(linePressPayload(for: row, gesture: "tap")) return @@ -2218,7 +2245,8 @@ private final class ReviewDiffContentView: UIView, UIGestureRecognizerDelegate { return } - theme.background.setFill() + let warning = row.tone == "warning" + (warning ? theme.warningBackground : theme.background).setFill() context.fill(rect) let hairline = 1 / traitCollection.displayScale @@ -2232,7 +2260,8 @@ private final class ReviewDiffContentView: UIView, UIGestureRecognizerDelegate { width: iconSize, height: iconSize ) - drawNoticeIcon(rect: iconRect, color: theme.mutedText) + let noticeColor = warning ? theme.warningText : theme.mutedText + drawNoticeIcon(rect: iconRect, color: noticeColor) drawSingleLineText( row.text ?? "", @@ -2242,7 +2271,7 @@ private final class ReviewDiffContentView: UIView, UIGestureRecognizerDelegate { width: max(24, viewportWidth - iconRect.maxX - 10 - style.fileHeaderHorizontalPadding), height: fileHeaderSubtextFont.lineHeight ), - color: theme.mutedText, + color: noticeColor, font: fileHeaderSubtextFont ) } @@ -2572,6 +2601,13 @@ private final class ReviewDiffContentView: UIView, UIGestureRecognizerDelegate { NSRange(location: $0.start, length: $0.end - $0.start) }, color: (change == "add" ? theme.addBar : theme.deleteBar).withAlphaComponent(0.28), + note: row.noteStart.map { + ReviewDiffCodeNote( + range: NSRange(location: $0, length: max(0, text.length - $0)), + color: theme.warningText, + background: theme.warningBackground + ) + }, version: codeDecorationVersion ) layout.draw( diff --git a/apps/mobile/modules/t3-terminal/THIRD_PARTY_NOTICES.md b/apps/mobile/modules/t3-terminal/THIRD_PARTY_NOTICES.md index 0f8a18d50cbd0..91e74e697bab0 100644 --- a/apps/mobile/modules/t3-terminal/THIRD_PARTY_NOTICES.md +++ b/apps/mobile/modules/t3-terminal/THIRD_PARTY_NOTICES.md @@ -21,7 +21,7 @@ The Android terminal renderer vendors upstream `libghostty-vt` shared libraries The web terminal vendors a WebAssembly build from the same revision and uses the same C ABI. - Upstream project: https://github.com/ghostty-org/ghostty -- Vendored revision: `9f62873bf195e4d8a762d768a1405a5f2f7b1697` +- Vendored revision: `3ff6d08fad56f2d6b81420a27bdc56ee707b453e` - License: MIT Ghostty's MIT license applies to the vendored Android and web libraries. Keep this notice and both diff --git a/apps/mobile/modules/t3-terminal/android/src/main/jniLibs/arm64-v8a/libghostty-vt.so b/apps/mobile/modules/t3-terminal/android/src/main/jniLibs/arm64-v8a/libghostty-vt.so index e086ca8315565..9e3ed70cf88ad 100755 Binary files a/apps/mobile/modules/t3-terminal/android/src/main/jniLibs/arm64-v8a/libghostty-vt.so and b/apps/mobile/modules/t3-terminal/android/src/main/jniLibs/arm64-v8a/libghostty-vt.so differ diff --git a/apps/mobile/modules/t3-terminal/android/src/main/jniLibs/armeabi-v7a/libghostty-vt.so b/apps/mobile/modules/t3-terminal/android/src/main/jniLibs/armeabi-v7a/libghostty-vt.so index 5592142ffce8c..9174c0be80f44 100755 Binary files a/apps/mobile/modules/t3-terminal/android/src/main/jniLibs/armeabi-v7a/libghostty-vt.so and b/apps/mobile/modules/t3-terminal/android/src/main/jniLibs/armeabi-v7a/libghostty-vt.so differ diff --git a/apps/mobile/modules/t3-terminal/android/src/main/jniLibs/x86/libghostty-vt.so b/apps/mobile/modules/t3-terminal/android/src/main/jniLibs/x86/libghostty-vt.so index bacab7908336a..db7667ea3559b 100755 Binary files a/apps/mobile/modules/t3-terminal/android/src/main/jniLibs/x86/libghostty-vt.so and b/apps/mobile/modules/t3-terminal/android/src/main/jniLibs/x86/libghostty-vt.so differ diff --git a/apps/mobile/modules/t3-terminal/android/src/main/jniLibs/x86_64/libghostty-vt.so b/apps/mobile/modules/t3-terminal/android/src/main/jniLibs/x86_64/libghostty-vt.so index f6e0b646a2fb2..0a1f011f8c133 100755 Binary files a/apps/mobile/modules/t3-terminal/android/src/main/jniLibs/x86_64/libghostty-vt.so and b/apps/mobile/modules/t3-terminal/android/src/main/jniLibs/x86_64/libghostty-vt.so differ diff --git a/apps/mobile/modules/t3-terminal/scripts/build-libghostty-android.sh b/apps/mobile/modules/t3-terminal/scripts/build-libghostty-android.sh index 55d994d3b363c..b9e225eff4bcd 100755 --- a/apps/mobile/modules/t3-terminal/scripts/build-libghostty-android.sh +++ b/apps/mobile/modules/t3-terminal/scripts/build-libghostty-android.sh @@ -7,7 +7,7 @@ MODULE_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)" VENDOR_DIR="${MODULE_DIR}/../../../../native/libghostty-vt" PATCH_DIR="${SCRIPT_DIR}/libghostty-android-patches" -GHOSTTY_REVISION="${GHOSTTY_REVISION:-9f62873bf195e4d8a762d768a1405a5f2f7b1697}" +GHOSTTY_REVISION="${GHOSTTY_REVISION:-3ff6d08fad56f2d6b81420a27bdc56ee707b453e}" GHOSTTY_SOURCE_DIR="${GHOSTTY_SOURCE_DIR:-${HOME}/.cache/t3code/ghostty-${GHOSTTY_REVISION:0:8}}" GHOSTTY_ZIG_VERSION="${GHOSTTY_ZIG_VERSION:-0.15.2}" GHOSTTY_ZIG="${GHOSTTY_ZIG:-}" diff --git a/apps/mobile/src/components/ComposerEditor.ios.test.tsx b/apps/mobile/src/components/ComposerEditor.ios.test.tsx index f7ce1f9918d85..029f454b902f7 100644 --- a/apps/mobile/src/components/ComposerEditor.ios.test.tsx +++ b/apps/mobile/src/components/ComposerEditor.ios.test.tsx @@ -128,7 +128,7 @@ vi.mock("../lib/useUniwindTheme", () => ({ useUniwindTheme: () => ({}) })); vi.mock("../lib/nativeComposerTheme", () => ({ createNativeComposerTheme: () => ({}) })); vi.mock("../lib/contextChipIconSource", () => ({ contextChipIconUri: () => "" })); vi.mock("../features/settings/appearance/useScaledTextRole", () => ({ - useScaledTextRole: () => ({ fontSize: 16, lineHeight: 22 }), + useComposerTextRole: () => ({ fontSize: 16, lineHeight: 22 }), })); vi.mock("../lib/uuid", () => ({ uuidv4: () => "uuid", randomHex: () => "0000" })); vi.mock("../state/assets", () => ({ assetEnvironment: {} })); diff --git a/apps/mobile/src/components/ComposerEditor.tsx b/apps/mobile/src/components/ComposerEditor.tsx index f7118b95b425f..bae452bad7c48 100644 --- a/apps/mobile/src/components/ComposerEditor.tsx +++ b/apps/mobile/src/components/ComposerEditor.tsx @@ -76,7 +76,7 @@ export function ComposerEditor({ props.onChangeText(text); if (draftKey) { setComposerDraftContext(draftKey, restored.context); - appendComposerDraftAttachments(draftKey, restored.attachments, { allowOverflow: true }); + appendComposerDraftAttachments(draftKey, restored.attachments, { restore: true }); } }; const [selected, setSelected] = useState<{ source: string; start: number; end: number } | null>( diff --git a/apps/mobile/src/components/ComposerToolbar.tsx b/apps/mobile/src/components/ComposerToolbar.tsx index 4fd54085c45f1..fdbf27b6f2c25 100644 --- a/apps/mobile/src/components/ComposerToolbar.tsx +++ b/apps/mobile/src/components/ComposerToolbar.tsx @@ -285,6 +285,7 @@ export function ComposerActionButton(props: { }) { const { scale, smallIconSize } = useAndroidControlSizing(); const circleSize = Math.round((props.size === "large" ? 44 : 30) * scale); + const fadeIcon = props.disabled === true && props.variant !== "danger"; return ( - + {/* Fade the icon as one layer: a translucent tint, or opacity on a Tabler icon (which + copies it onto every path), stacks where the SVG strokes overlap. */} + + + ); diff --git a/apps/mobile/src/components/VideoThumbnailImage.tsx b/apps/mobile/src/components/VideoThumbnailImage.tsx index 6f6fea83063ca..f4ddb25a276fb 100644 --- a/apps/mobile/src/components/VideoThumbnailImage.tsx +++ b/apps/mobile/src/components/VideoThumbnailImage.tsx @@ -41,7 +41,7 @@ export function VideoThumbnailImage(props: { readonly contentFit?: "cover" | "contain"; }) { const { cacheKey, source } = props; - // Extraction runs only while the screen is focused; leaving it interrupts the load. + // Extraction starts only while the screen is focused; leaving it skips queued loads. const isFocused = useIsFocused(); const loaded = useAtomValue( source && isFocused ? videoThumbnailAtom({ cacheKey, source }) : noVideoThumbnail, diff --git a/apps/mobile/src/components/ui/AndroidAnchoredMenu.android.tsx b/apps/mobile/src/components/ui/AndroidAnchoredMenu.android.tsx index a17e48c2604c7..f9e3b2ac01249 100644 --- a/apps/mobile/src/components/ui/AndroidAnchoredMenu.android.tsx +++ b/apps/mobile/src/components/ui/AndroidAnchoredMenu.android.tsx @@ -61,6 +61,10 @@ export function AndroidAnchoredMenu(props: AndroidAnchoredMenuProps) { setPath([]); setOverlay(null); }; + // The in-window menu is pinned to the trigger's spot above the keyboard, which is stale once it hides. + if (anchor?.keyboardWasVisible === true && !keyboardVisible) { + close(); + } const open = () => { anchorNode?.measureInWindow((x, y, width, height) => { diff --git a/apps/mobile/src/components/ui/AppSymbol.android.tsx b/apps/mobile/src/components/ui/AppSymbol.android.tsx index 7aaeac554417a..b34c27bd471f3 100644 --- a/apps/mobile/src/components/ui/AppSymbol.android.tsx +++ b/apps/mobile/src/components/ui/AppSymbol.android.tsx @@ -140,6 +140,7 @@ import IconWifiOff from "@tabler/icons-react-native/IconWifiOff"; import IconWorld from "@tabler/icons-react-native/IconWorld"; import IconX from "@tabler/icons-react-native/IconX"; import type { AndroidSymbol, SFSymbol, SymbolViewProps } from "expo-symbols"; +import { StyleSheet, View } from "react-native"; import { withUniwind } from "uniwind"; const ANDROID_ICON_BY_SF_SYMBOL = { @@ -371,16 +372,20 @@ function AppSymbolView(props: AppSymbolViewProps) { return props.fallback ?? null; } - return ( + // react-native-svg applies style.transform to its inner path around (0,0), so a + // rotated icon draws outside its box. Transform a wrapping View instead. + const transformed = StyleSheet.flatten(props.style)?.transform !== undefined; + const icon = ( ); + return transformed ? {icon} : icon; } export const SymbolView = withUniwind(AppSymbolView); diff --git a/apps/mobile/src/components/ui/FilePreviewModal.tsx b/apps/mobile/src/components/ui/FilePreviewModal.tsx index aff82eae479cd..eba89cdb28c9b 100644 --- a/apps/mobile/src/components/ui/FilePreviewModal.tsx +++ b/apps/mobile/src/components/ui/FilePreviewModal.tsx @@ -31,6 +31,7 @@ const NativeControls = requireNativeModule<{ ): Promise; updateFile(identifier: string, index: number, uri: string | null): Promise; dismissFile(identifier: string): Promise; + isFilePreviewFinished(identifier: string): boolean; }>("T3NativeControls"); // Null tells the viewer an image failed; undefined means its URL has not arrived yet. @@ -38,10 +39,11 @@ const nativeUri = (image: FilePreviewImage) => (image.failed ? null : image.uri) function NativeFilePreview(props: FilePreviewViewerProps) { const identifier = useId(); - // Later URLs reach the open viewer through updateFile instead of presenting again. + // Later URLs reach the native presentation through updateFile instead of presenting again. const [presented] = useState(props.source); const delivered = useRef<(string | null | undefined)[]>([]); const onRequestClose = useEffectEvent(props.onRequestClose); + const onPresent = useEffectEvent((isFinished: () => boolean) => props.onPresent?.(isFinished)); const onOpenError = useEffectEvent((error: unknown) => { if (props.onOpenError) props.onOpenError(error); else Dialog.alert("Could not open preview", "The file could not be loaded. Please try again."); @@ -51,7 +53,7 @@ function NativeFilePreview(props: FilePreviewViewerProps) { useEffect(() => { let canceled = false; let presentation: Promise; - // Images open while still loading; other files must download first so Quick Look can refuse them. + // Images hand over before every URL resolves; other files must download first so Quick Look can refuse them. if (presented.kind === "image") { const { images, selectedIndex } = presented; delivered.current = images.map((image) => image.uri); @@ -71,6 +73,7 @@ function NativeFilePreview(props: FilePreviewViewerProps) { identifier, ); } + onPresent(() => NativeControls.isFilePreviewFinished(identifier)); void presentation .catch((error: unknown) => { if (!canceled) onOpenError(error); diff --git a/apps/mobile/src/components/ui/FilePreviewModal.types.ts b/apps/mobile/src/components/ui/FilePreviewModal.types.ts index 37dd9ea9e16b0..4311776b0ad9e 100644 --- a/apps/mobile/src/components/ui/FilePreviewModal.types.ts +++ b/apps/mobile/src/components/ui/FilePreviewModal.types.ts @@ -54,4 +54,6 @@ export interface FilePreviewViewerProps { readonly source: ResolvedFilePreviewSource; readonly onRequestClose: () => void; readonly onOpenError?: (error: unknown) => void; + /** Called once the viewer hands its file to a native presentation, with a check for its close. */ + readonly onPresent?: (isFinished: () => boolean) => void; } diff --git a/apps/mobile/src/components/ui/FilePreviewSession.test.tsx b/apps/mobile/src/components/ui/FilePreviewSession.test.tsx index a5b51aa5cf4b3..8a38a70605cd6 100644 --- a/apps/mobile/src/components/ui/FilePreviewSession.test.tsx +++ b/apps/mobile/src/components/ui/FilePreviewSession.test.tsx @@ -1,4 +1,4 @@ -import { act, useState } from "react"; +import { act, useEffect, useState } from "react"; import { createRoot } from "react-dom/client"; import { expect, it, vi } from "vite-plus/test"; import { @@ -14,6 +14,7 @@ const harness = vi.hoisted(() => ({ alert: vi.fn(), previews: [] as unknown[], mounts: 0, + presentationFinished: false, })); vi.mock("../../state/assets", () => ({ refreshAssetUrl: harness.refreshAssetUrl })); @@ -32,6 +33,9 @@ import { FilePreviewSession } from "./FilePreviewSession"; function Viewer(props: FilePreviewViewerProps) { useState(() => harness.mounts++); harness.previews.push(props.source); + const { onPresent } = props; + // Stands in for the native presentation starting. + useEffect(() => onPresent?.(() => harness.presentationFinished), [onPresent]); return null; } @@ -171,3 +175,70 @@ it("names the checked path when the server cannot find the file", async () => { ); expect(harness.alert.mock.calls[0]?.[1]).not.toMatch(/reconnect/i); }); + +it.each([ + // Handed to native but not yet started there, or still on screen. + ["image", false, 1], + ["image", true, 2], + // A PDF is still resolving here, so its viewer never presented and a re-render must not restart it. + ["pdf", true, 1], +] as const)( + "a re-tap of the open %s restarts the preview only once its native viewer closed (closed: %s)", + async (kind, finished, starts) => { + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; + harness.presentationFinished = finished; + harness.mounts = 0; + harness.refreshAssetUrl.mockReset(); + if (kind === "image") harness.refreshAssetUrl.mockResolvedValue("https://signed.test/a.png"); + else harness.refreshAssetUrl.mockReturnValue(new Promise(() => {})); + const selected: FilePreviewItem = { ...image("https://github.com/a.png"), kind }; + const root = createRoot(document.createElement("div")); + await using _root = { [Symbol.asyncDispose]: () => act(async () => root.unmount()) }; + const render = () => + act(async () => + root.render( + {}} + progressiveGallery + Viewer={Viewer} + />, + ), + ); + await render(); + await render(); + expect(harness.refreshAssetUrl).toHaveBeenCalledTimes(starts); + }, +); + +it("does not carry a presented viewer over to a different item that is still resolving", async () => { + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; + harness.presentationFinished = true; + harness.refreshAssetUrl.mockReset(); + harness.refreshAssetUrl.mockImplementation((_environmentId, resource) => + resource.url.endsWith("a.png") + ? Promise.resolve("https://signed.test/a.png") + : new Promise(() => {}), + ); + const shown = image("https://github.com/a.png"); + const resolving: FilePreviewItem = { ...image("https://github.com/b.pdf"), kind: "pdf" }; + const root = createRoot(document.createElement("div")); + await using _root = { [Symbol.asyncDispose]: () => act(async () => root.unmount()) }; + const render = (item: FilePreviewItem) => + act(async () => + root.render( + {}} + progressiveGallery + Viewer={Viewer} + />, + ), + ); + await render(shown); + await render(resolving); + await render(resolving); + expect( + harness.refreshAssetUrl.mock.calls.filter(([, r]) => r.url.endsWith("b.pdf")), + ).toHaveLength(1); +}); diff --git a/apps/mobile/src/components/ui/FilePreviewSession.tsx b/apps/mobile/src/components/ui/FilePreviewSession.tsx index 5df8acf001e03..a7a60ce5555b4 100644 --- a/apps/mobile/src/components/ui/FilePreviewSession.tsx +++ b/apps/mobile/src/components/ui/FilePreviewSession.tsx @@ -97,6 +97,7 @@ function ResolvedFilePreview( readonly source: FilePreviewSource; readonly onRequestClose: () => void; readonly onOpenError?: (error: unknown) => void; + readonly onPresent: (isFinished: () => boolean) => void; }, ) { const { progressiveGallery, Viewer } = props; @@ -161,6 +162,7 @@ function ResolvedFilePreview( ); @@ -205,6 +207,7 @@ function ResolvedFilePreview( }} onRequestClose={props.onRequestClose} onOpenError={onOpenError} + onPresent={props.onPresent} /> ); } @@ -220,15 +223,43 @@ export function FilePreviewSession(props: FilePreviewModalProps & FilePreviewSes ? null : { ...props.source, gallery: filePreviewGallery(props.source, group) }; usePreviewPresentation(source && filePreviewKey(source), props.onRequestClose); + // A tap can land after the native viewer closed but before its close reached the caller, which + // still holds the same item; that tap only opens a new viewer under a new key. + // Only a viewer that presented and has since closed reopens; one still resolving never does. + const [opening, setOpening] = useState<{ + readonly source: FilePreviewModalProps["source"]; + readonly count: number; + readonly presented: { readonly isFinished: () => boolean } | null; + }>({ source: props.source, count: 0, presented: null }); + if (opening.source !== props.source) { + const sameItem = + opening.source !== null && + props.source !== null && + filePreviewKey(opening.source) === filePreviewKey(props.source); + const reopened = sameItem && opening.presented?.isFinished() === true; + setOpening({ + source: props.source, + count: opening.count + (reopened ? 1 : 0), + presented: sameItem && !reopened ? opening.presented : null, + }); + } + const { count } = opening; + const onPresent = (isFinished: () => boolean) => + setOpening((current) => + current.count === count && current.presented === null + ? { ...current, presented: { isFinished } } + : current, + ); if (!source) return null; return ( ); diff --git a/apps/mobile/src/connection/environment-owned-data-cleanup.test.ts b/apps/mobile/src/connection/environment-owned-data-cleanup.test.ts new file mode 100644 index 0000000000000..efe489b0fde35 --- /dev/null +++ b/apps/mobile/src/connection/environment-owned-data-cleanup.test.ts @@ -0,0 +1,74 @@ +import { Persistence } from "@t3tools/client-runtime/platform"; +import { EnvironmentId } from "@t3tools/contracts"; +import { describe, expect, it } from "@effect/vitest"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as Logger from "effect/Logger"; +import { vi } from "vite-plus/test"; + +const harness = vi.hoisted(() => ({ + clearOutbox: vi.fn<(environmentId: string) => Promise>(), + clearDrafts: vi.fn<(environmentId: string) => Promise>(), + clearComposerErrors: vi.fn(), +})); + +vi.mock("../state/thread-outbox-removal", () => ({ + clearThreadOutboxEnvironment: harness.clearOutbox, +})); +vi.mock("../state/use-composer-drafts", () => ({ + clearComposerDraftsEnvironment: harness.clearDrafts, +})); +vi.mock("../state/thread-composer-error", () => ({ + clearThreadComposerErrorsForEnvironment: harness.clearComposerErrors, +})); + +import * as EnvironmentOwnedDataCleanup from "./environment-owned-data-cleanup"; + +const ENVIRONMENT_ID = EnvironmentId.make("environment-1"); + +describe("EnvironmentOwnedDataCleanup", () => { + it.effect("waits for the other clears and logs a bounded warning when one rejects", () => { + const warnings: Array = []; + let releaseDrafts = () => {}; + let draftsCleared = false; + // The drafts clear finishes only after the outbox failure is logged. + const logger = Logger.make(({ message }) => { + warnings.push(message); + releaseDrafts(); + }); + return Effect.gen(function* () { + harness.clearOutbox.mockRejectedValue(new Error("Some queued messages could not be read.")); + harness.clearDrafts.mockImplementation(() => + new Promise((resolve) => { + releaseDrafts = resolve; + }).then(() => { + draftsCleared = true; + }), + ); + + const cleanup = yield* Persistence.EnvironmentOwnedDataCleanup; + yield* cleanup.clear(ENVIRONMENT_ID); + + expect(draftsCleared).toBe(true); + expect(warnings).toEqual([ + [ + "Could not clear mobile environment-owned data.", + { + environmentId: ENVIRONMENT_ID, + operation: "outbox", + errorType: "error", + errorName: "Error", + stack: expect.any(String), + }, + ], + ]); + }).pipe( + Effect.provide( + Layer.merge( + EnvironmentOwnedDataCleanup.layer, + Logger.layer([logger], { mergeWithExisting: false }), + ), + ), + ); + }); +}); diff --git a/apps/mobile/src/connection/environment-owned-data-cleanup.ts b/apps/mobile/src/connection/environment-owned-data-cleanup.ts new file mode 100644 index 0000000000000..28c6cda29a4d3 --- /dev/null +++ b/apps/mobile/src/connection/environment-owned-data-cleanup.ts @@ -0,0 +1,48 @@ +import { safeErrorLogAttributes } from "@t3tools/client-runtime/errors"; +import { Persistence } from "@t3tools/client-runtime/platform"; +import type { EnvironmentId } from "@t3tools/contracts"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as Schema from "effect/Schema"; + +import { clearThreadComposerErrorsForEnvironment } from "../state/thread-composer-error"; +import { clearThreadOutboxEnvironment } from "../state/thread-outbox-removal"; +import { clearComposerDraftsEnvironment } from "../state/use-composer-drafts"; + +class EnvironmentOwnedDataClearError extends Schema.TaggedError()( + "EnvironmentOwnedDataClearError", + { cause: Schema.Defect() }, +) {} + +export const layer = Layer.succeed( + Persistence.EnvironmentOwnedDataCleanup, + Persistence.EnvironmentOwnedDataCleanup.of({ + clear: Effect.fn("Persistence.EnvironmentOwnedDataCleanup.clear")(function (environmentId) { + // Each clear settles on its own: a failure must not interrupt a clear that is still running. + const settle = ( + operation: "outbox" | "drafts", + clear: (environmentId: EnvironmentId) => Promise, + ) => + Effect.tryPromise({ + try: () => clear(environmentId), + catch: (cause) => new EnvironmentOwnedDataClearError({ cause }), + }).pipe( + Effect.catch((error) => + Effect.logWarning("Could not clear mobile environment-owned data.", { + environmentId, + operation, + ...safeErrorLogAttributes(error.cause), + }), + ), + ); + return Effect.all( + [ + settle("outbox", clearThreadOutboxEnvironment), + settle("drafts", clearComposerDraftsEnvironment), + Effect.sync(() => clearThreadComposerErrorsForEnvironment(environmentId)), + ], + { concurrency: "unbounded", discard: true }, + ); + }), + }), +); diff --git a/apps/mobile/src/connection/platform.ts b/apps/mobile/src/connection/platform.ts index 407016ab484d9..de461f35ab104 100644 --- a/apps/mobile/src/connection/platform.ts +++ b/apps/mobile/src/connection/platform.ts @@ -1,8 +1,4 @@ -import { - ClientCapabilities, - PlatformConnectionSource, - Persistence, -} from "@t3tools/client-runtime/platform"; +import { ClientCapabilities, PlatformConnectionSource } from "@t3tools/client-runtime/platform"; import { ConnectionBlockedError, ConnectionTransientError, @@ -25,10 +21,8 @@ import { authClientMetadata } from "../lib/authClientMetadata"; import * as Runtime from "../lib/runtime"; import * as MobileStorage from "../persistence/mobile-storage"; import { appAtomRegistry } from "../state/atom-registry"; -import { clearThreadOutboxEnvironment } from "../state/thread-outbox-removal"; -import { clearComposerDraftsEnvironment } from "../state/use-composer-drafts"; -import { clearThreadComposerErrorsForEnvironment } from "../state/thread-composer-error"; import { mobileApplicationActiveWakeup } from "./app-state-wakeups"; +import * as EnvironmentOwnedDataCleanup from "./environment-owned-data-cleanup"; import * as ConnectionStorage from "./storage"; function networkStatus(state: Network.NetworkState): "unknown" | "offline" | "online" { @@ -255,22 +249,6 @@ const layerPlatformConnectionSource = Layer.succeed( const layerProvidedConnectionStorage = ConnectionStorage.layer.pipe(Layer.provide(Runtime.layer)); const layerProvidedCapabilities = layerCapabilities.pipe(Layer.provide(Runtime.layer)); -const layerEnvironmentOwnedDataCleanup = Layer.succeed( - Persistence.EnvironmentOwnedDataCleanup, - Persistence.EnvironmentOwnedDataCleanup.of({ - clear: Effect.fn("Persistence.EnvironmentOwnedDataCleanup.clear")((environmentId) => - Effect.all( - [ - Effect.promise(() => clearThreadOutboxEnvironment(environmentId)), - Effect.promise(() => clearComposerDraftsEnvironment(environmentId)), - Effect.sync(() => clearThreadComposerErrorsForEnvironment(environmentId)), - ], - { concurrency: "unbounded", discard: true }, - ), - ), - }), -); - type ConnectionPlatformLayerSource = | typeof layerProvidedConnectionStorage | typeof Runtime.layer @@ -278,7 +256,7 @@ type ConnectionPlatformLayerSource = | typeof layerWakeups | typeof layerProvidedCapabilities | typeof layerPlatformConnectionSource - | typeof layerEnvironmentOwnedDataCleanup; + | typeof EnvironmentOwnedDataCleanup.layer; export const layer: Layer.Layer< Layer.Success, @@ -291,5 +269,5 @@ export const layer: Layer.Layer< layerWakeups, layerProvidedCapabilities, layerPlatformConnectionSource, - layerEnvironmentOwnedDataCleanup, + EnvironmentOwnedDataCleanup.layer, ); diff --git a/apps/mobile/src/features/connection/LinkedEnvironmentsSection.test.tsx b/apps/mobile/src/features/connection/LinkedEnvironmentsSection.test.tsx new file mode 100644 index 0000000000000..964d499fd30c4 --- /dev/null +++ b/apps/mobile/src/features/connection/LinkedEnvironmentsSection.test.tsx @@ -0,0 +1,169 @@ +import { RegistryContext } from "@effect/atom-react"; +import { + NavigationContext, + type NavigationProp, + type ParamListBase, +} from "@react-navigation/native"; +import { EnvironmentId } from "@t3tools/contracts"; +import { AtomRegistry } from "effect/reactivity"; +import { act, type ReactNode } from "react"; +import { createRoot } from "react-dom/client"; +import { expect, it, vi } from "vite-plus/test"; + +// Each listing probes every linked machine, so the test counts the fetches it starts and cuts. +const fetches = vi.hoisted(() => ({ + started: 0, + interrupted: 0, + gate: Promise.resolve(), + open: () => {}, +})); + +vi.mock("@react-navigation/native", async () => { + const { createRequire } = await import("node:module"); + const { realpathSync } = await import("node:fs"); + const require = createRequire(import.meta.url); + const nativeRequire = createRequire( + realpathSync(require.resolve("@react-navigation/native/package.json")), + ); + // Use the shipped focus hook without importing the React Native view layer. + const { NavigationContext, useFocusEffect }: typeof import("@react-navigation/native") = + nativeRequire("@react-navigation/core"); + return { NavigationContext, useFocusEffect }; +}); +const appState = vi.hoisted(() => ({ listeners: new Set<(state: string) => void>() })); + +vi.mock("react-native", () => ({ + AppState: { + addEventListener: (_event: "change", listener: (state: string) => void) => { + appState.listeners.add(listener); + return { remove: () => appState.listeners.delete(listener) }; + }, + }, + View: ({ children }: { children?: ReactNode }) => children, +})); +vi.mock("../../components/ui/AppText", () => ({ + AppText: ({ children }: { children?: ReactNode }) => children, +})); +vi.mock("../settings/components/SettingsSection", () => ({ + SettingsSection: ({ children }: { children?: ReactNode }) => children, +})); +vi.mock("../../state/entities", () => ({ + useEnvironmentServerConfig: () => ({ + environment: { capabilities: { peerLinks: true } }, + settings: { linkedEnvironmentsEnabled: true }, + }), +})); +vi.mock("../../state/session", () => ({ useEnvironmentScope: () => true })); +vi.mock("../../state/peerLinks", async () => { + const { Atom } = await import("effect/reactivity"); + const EffectModule = await import("effect/Effect"); + // Waits for the gate, as a list waits on its slowest linked machine. + const list = Atom.make( + EffectModule.gen(function* () { + fetches.started += 1; + yield* EffectModule.promise(() => fetches.gate); + return { links: [] }; + }).pipe( + EffectModule.onInterrupt(() => + EffectModule.sync(() => { + fetches.interrupted += 1; + }), + ), + ), + ); + return { peerLinkEnvironment: { list: () => list } }; +}); + +import { LinkedEnvironmentsSection } from "./LinkedEnvironmentsSection"; + +function createNavigation() { + const listeners = { focus: new Set<() => void>(), blur: new Set<() => void>() }; + let focused = true; + return { + value: { + isFocused: () => focused, + addListener: (event: keyof typeof listeners, listener: () => void) => { + listeners[event].add(listener); + return () => listeners[event].delete(listener); + }, + } as unknown as NavigationProp, + focus(next: boolean) { + focused = next; + act(() => { + for (const listener of listeners[next ? "focus" : "blur"]) listener(); + }); + }, + }; +} + +function setup() { + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; + fetches.started = 0; + fetches.interrupted = 0; + fetches.gate = new Promise((resolve) => { + fetches.open = resolve; + }); + const registry = AtomRegistry.make(); + const root = createRoot(document.createElement("div")); + const navigation = createNavigation(); + act(() => + root.render( + + + + + , + ), + ); + return { + focus: navigation.focus, + moveApp: (state: "active" | "background") => + act(() => { + for (const listener of appState.listeners) listener(state); + }), + settle: () => + act(async () => { + fetches.open(); + await fetches.gate; + }), + [Symbol.dispose]() { + act(() => root.unmount()); + registry.dispose(); + }, + }; +} + +it("asks again on focus only once the list it is waiting for has answered", async () => { + using screen = setup(); + // The first focus comes while the first fetch runs, which must not be cut and restarted. + expect(fetches).toMatchObject({ started: 1, interrupted: 0 }); + screen.focus(false); + screen.focus(true); + expect(fetches).toMatchObject({ started: 1, interrupted: 0 }); + + await screen.settle(); + screen.focus(false); + screen.focus(true); + expect(fetches).toMatchObject({ started: 2, interrupted: 0 }); +}); + +it("asks again when the app comes back to the front on this screen", async () => { + using screen = setup(); + await screen.settle(); + expect(fetches).toMatchObject({ started: 1, interrupted: 0 }); + fetches.gate = new Promise((resolve) => { + fetches.open = resolve; + }); + + screen.moveApp("background"); + screen.moveApp("active"); + expect(fetches).toMatchObject({ started: 2, interrupted: 0 }); + + // Coming back while that answer is still out must not cut it and start again. + screen.moveApp("background"); + screen.moveApp("active"); + expect(fetches).toMatchObject({ started: 2, interrupted: 0 }); +}); diff --git a/apps/mobile/src/features/connection/LinkedEnvironmentsSection.tsx b/apps/mobile/src/features/connection/LinkedEnvironmentsSection.tsx index 9153c9fa6a1c8..f447e2760277e 100644 --- a/apps/mobile/src/features/connection/LinkedEnvironmentsSection.tsx +++ b/apps/mobile/src/features/connection/LinkedEnvironmentsSection.tsx @@ -1,9 +1,10 @@ +import { RegistryContext } from "@effect/atom-react"; import { offersLinkedEnvironments } from "@t3tools/client-runtime/link-request"; import { peerLinkHealth } from "@t3tools/client-runtime/state/peerLinks"; import { useFocusEffect } from "@react-navigation/native"; import { AuthAccessReadScope, type EnvironmentId } from "@t3tools/contracts"; import * as DateTime from "effect/DateTime"; -import { useCallback, useState } from "react"; +import { useCallback, useContext, useState } from "react"; import { AppState, View } from "react-native"; import { AppText as Text } from "../../components/ui/AppText"; @@ -20,8 +21,25 @@ export function LinkedEnvironmentsSection(props: { const config = useEnvironmentServerConfig(props.environmentId); const canRead = useEnvironmentScope(props.environmentId, AuthAccessReadScope); const supported = canRead && offersLinkedEnvironments(config); - const links = useEnvironmentQuery( - supported ? peerLinkEnvironment.list({ environmentId: props.environmentId, input: {} }) : null, + const listAtom = supported + ? peerLinkEnvironment.list({ environmentId: props.environmentId, input: {} }) + : null; + const links = useEnvironmentQuery(listAtom); + const registry = useContext(RegistryContext); + // Links change outside this client, and the shared list only revalidates when built, so + // showing the screen and bringing the app back ask again. + useFocusEffect( + useCallback(() => { + if (listAtom === null) return; + const askAgain = () => { + if (!registry.get(listAtom).waiting) registry.refresh(listAtom); + }; + askAgain(); + const subscription = AppState.addEventListener("change", (state) => { + if (state === "active") askAgain(); + }); + return () => subscription.remove(); + }, [listAtom, registry]), ); const [nowMs, setNowMs] = useState(Date.now); const hasLinks = supported && (links.data?.links.length ?? 0) > 0; diff --git a/apps/mobile/src/features/connection/useConnectionController.ts b/apps/mobile/src/features/connection/useConnectionController.ts index 626892d447569..64deaaf8d1e72 100644 --- a/apps/mobile/src/features/connection/useConnectionController.ts +++ b/apps/mobile/src/features/connection/useConnectionController.ts @@ -38,7 +38,6 @@ export function useConnectionController() { const updateBearer = useAtomCommand(updateBearerConnection, { reportFailure: false }); const registerEnvironment = useAtomCommand(environmentCatalog.register, "environment register"); const removeEnvironmentMutation = useAtomCommand(environmentCatalog.remove, "environment remove"); - const retryEnvironmentMutation = useAtomCommand(environmentCatalog.retryNow, "environment retry"); const setEnvironmentEnabledMutation = useAtomCommand( environmentCatalog.setEnabled, "environment toggle", @@ -84,8 +83,6 @@ export function useConnectionController() { ); const removeEnvironment = (environmentId: EnvironmentId) => removeEnvironmentMutation(environmentId); - const retryEnvironment = (environmentId: EnvironmentId) => - retryEnvironmentMutation(environmentId); const setEnvironmentEnabled = (environmentId: EnvironmentId, enabled: boolean) => setEnvironmentEnabledMutation({ environmentId, enabled }); const updateEnvironment = ( @@ -111,7 +108,6 @@ export function useConnectionController() { connectPairingUrl, connectRelayEnvironment, removeEnvironment, - retryEnvironment, setEnvironmentEnabled, updateEnvironment, refreshRelayEnvironments, diff --git a/apps/mobile/src/features/connection/useRetryEnvironment.test.tsx b/apps/mobile/src/features/connection/useRetryEnvironment.test.tsx new file mode 100644 index 0000000000000..da94dedf2e78a --- /dev/null +++ b/apps/mobile/src/features/connection/useRetryEnvironment.test.tsx @@ -0,0 +1,108 @@ +import { RegistryContext } from "@effect/atom-react"; +import { EnvironmentId } from "@t3tools/contracts"; +import { + EnvironmentRegistry, + PrimaryConnectionTarget, + type ConnectionCatalogEntry, +} from "@t3tools/client-runtime/connection"; +import * as Effect from "effect/Effect"; +import * as Option from "effect/Option"; +import * as SubscriptionRef from "effect/SubscriptionRef"; +import { AtomRegistry } from "effect/reactivity"; +import { act, useLayoutEffect } from "react"; +import { createRoot } from "react-dom/client"; +import { describe, expect, it } from "@effect/vitest"; +import { vi } from "vite-plus/test"; + +const fixture = vi.hoisted(() => ({ + environment: undefined as + | Pick< + EnvironmentRegistry.EnvironmentRegistry["Service"], + "entries" | "retryNow" | "setEnabled" + > + | undefined, +})); + +vi.mock("../../connection/runtime", async () => { + const { Atom } = await import("effect/reactivity"); + const Layer = await import("effect/Layer"); + const { EnvironmentRegistry } = await import("@t3tools/client-runtime/connection"); + return { + connectionAtomRuntime: Atom.runtime( + Layer.suspend(() => + Layer.succeed( + EnvironmentRegistry.EnvironmentRegistry, + fixture.environment as unknown as EnvironmentRegistry.EnvironmentRegistry["Service"], + ), + ), + ), + }; +}); + +import { useRetryEnvironment } from "./useRetryEnvironment"; + +const environmentId = EnvironmentId.make("retry-environment"); + +describe("useRetryEnvironment", () => { + it.effect.each([ + { enabled: false, retries: 0 }, + { enabled: true, retries: 1 }, + ])("Retry now with the environment enabled: $enabled", ({ enabled, retries }) => + Effect.gen(function* () { + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; + const target = new PrimaryConnectionTarget({ + environmentId, + label: "Retry", + httpBaseUrl: "https://retry.test", + wsBaseUrl: "wss://retry.test", + }); + const entries = yield* SubscriptionRef.make< + ReadonlyMap + >(new Map([[environmentId, { target, profile: Option.none(), enabled }]])); + let retryCount = 0; + fixture.environment = { + entries, + retryNow: () => + Effect.sync(() => { + retryCount += 1; + }), + setEnabled: (id, next) => + SubscriptionRef.update(entries, (current) => + new Map(current).set(id, { ...current.get(id)!, enabled: next }), + ), + }; + + const registry = AtomRegistry.make(); + const root = createRoot(document.createElement("div")); + using _mounted = { + [Symbol.dispose]() { + act(() => root.unmount()); + registry.dispose(); + }, + }; + let retryEnvironment!: ReturnType; + function Probe() { + const value = useRetryEnvironment(); + useLayoutEffect(() => { + retryEnvironment = value; + }); + return null; + } + yield* Effect.promise(async () => { + await act(async () => + root.render( + + + , + ), + ); + await act(async () => { + await retryEnvironment(environmentId); + }); + }); + + const entry = (yield* SubscriptionRef.get(entries)).get(environmentId); + expect({ enabled: entry?.enabled, retries: retryCount }).toEqual({ enabled: true, retries }); + }), + ); +}); diff --git a/apps/mobile/src/features/connection/useRetryEnvironment.ts b/apps/mobile/src/features/connection/useRetryEnvironment.ts new file mode 100644 index 0000000000000..b5d8edc749230 --- /dev/null +++ b/apps/mobile/src/features/connection/useRetryEnvironment.ts @@ -0,0 +1,17 @@ +import { useAtomValue } from "@effect/atom-react"; +import type { EnvironmentId } from "@t3tools/contracts"; + +import { environmentCatalog } from "../../connection/catalog"; +import { useAtomCommand } from "../../state/use-atom-command"; + +export function useRetryEnvironment() { + const catalog = useAtomValue(environmentCatalog.catalogValueAtom); + const retryNow = useAtomCommand(environmentCatalog.retryNow, "environment retry"); + const setEnabled = useAtomCommand(environmentCatalog.setEnabled, "environment toggle"); + + return (environmentId: EnvironmentId) => + // A retry does nothing while the environment is switched off. + catalog.entries.get(environmentId)?.enabled === false + ? setEnabled({ environmentId, enabled: true }) + : retryNow(environmentId); +} diff --git a/apps/mobile/src/features/diagnostics/SettingsDiagnosticsRouteScreen.tsx b/apps/mobile/src/features/diagnostics/SettingsDiagnosticsRouteScreen.tsx index 528a18541eb46..448fd929e42e8 100644 --- a/apps/mobile/src/features/diagnostics/SettingsDiagnosticsRouteScreen.tsx +++ b/apps/mobile/src/features/diagnostics/SettingsDiagnosticsRouteScreen.tsx @@ -96,10 +96,10 @@ export function SettingsDiagnosticsRouteScreen() { {crashReportingAvailable ? : null} diff --git a/apps/mobile/src/features/diffs/nativeReviewDiffSource.ts b/apps/mobile/src/features/diffs/nativeReviewDiffSource.ts index 7f503239c21d2..4d73a7cda5c3c 100644 --- a/apps/mobile/src/features/diffs/nativeReviewDiffSource.ts +++ b/apps/mobile/src/features/diffs/nativeReviewDiffSource.ts @@ -19,6 +19,7 @@ function permanentSourceFailureReason(error: unknown): string | undefined { if (name === "GitHubDiffFileContentsUnavailableError") return "github-source-unavailable"; for (const permanent of [ "provider-unsupported", + "repository-without-project", "unsupported", "oversized", "binary", diff --git a/apps/mobile/src/features/diffs/nativeReviewDiffSurface.ts b/apps/mobile/src/features/diffs/nativeReviewDiffSurface.ts index bf8c6e8e311ca..2699bb995406c 100644 --- a/apps/mobile/src/features/diffs/nativeReviewDiffSurface.ts +++ b/apps/mobile/src/features/diffs/nativeReviewDiffSurface.ts @@ -47,6 +47,12 @@ export interface NativeReviewDiffRow { readonly commentText?: string; readonly commentRangeLabel?: string; readonly commentSectionTitle?: string; + /** A notice drawn in the warning colors. */ + readonly tone?: "warning"; + /** A notice that reports taps through `onPressNotice`. */ + readonly actionable?: boolean; + /** Where a cut line's note starts in `content`. */ + readonly noteStart?: number; } export interface NativeReviewDiffWordDiffRange { @@ -74,6 +80,8 @@ export interface NativeReviewDiffTheme { readonly deleteBar: string; readonly addText: string; readonly deleteText: string; + readonly warningText: string; + readonly warningBackground: string; } export interface NativeReviewDiffStyle { @@ -151,6 +159,9 @@ export interface NativeReviewDiffViewProps extends ViewProps { }>, ) => void; readonly onToggleComment?: (event: NativeSyntheticEvent<{ readonly commentId?: string }>) => void; + readonly onPressNotice?: ( + event: NativeSyntheticEvent<{ readonly rowId?: string; readonly fileId?: string }>, + ) => void; } export interface NativeReviewDiffViewHandle { diff --git a/apps/mobile/src/features/files/WorkspaceFilePreviewError.tsx b/apps/mobile/src/features/files/WorkspaceFilePreviewError.tsx index 63c3e7be0c65e..eccac90d9abea 100644 --- a/apps/mobile/src/features/files/WorkspaceFilePreviewError.tsx +++ b/apps/mobile/src/features/files/WorkspaceFilePreviewError.tsx @@ -2,11 +2,10 @@ import type { EnvironmentId } from "@t3tools/contracts"; import { View } from "react-native"; import { EmptyState } from "../../components/ui/EmptyState"; -import { environmentCatalog } from "../../connection/catalog"; import type { AssetUrlFailureReason } from "../../state/asset-url-state"; -import { useAtomCommand } from "../../state/use-atom-command"; import { useEnvironmentPresentation } from "../../state/presentation"; import { EnvironmentConnectionNotice } from "../connection/EnvironmentConnectionNotice"; +import { useRetryEnvironment } from "../connection/useRetryEnvironment"; export function WorkspaceFilePreviewError(props: { readonly environmentId: EnvironmentId | null; @@ -15,7 +14,7 @@ export function WorkspaceFilePreviewError(props: { }) { const { environmentId, onRetry } = props; const environment = useEnvironmentPresentation(environmentId); - const retryEnvironment = useAtomCommand(environmentCatalog.retryNow, "environment retry"); + const retryEnvironment = useRetryEnvironment(); const retryConnection = () => { if (environmentId !== null) void retryEnvironment(environmentId); onRetry(); diff --git a/apps/mobile/src/features/files/filePath.test.ts b/apps/mobile/src/features/files/filePath.test.ts index 0acf3d5877ef0..17bf3383d42ca 100644 --- a/apps/mobile/src/features/files/filePath.test.ts +++ b/apps/mobile/src/features/files/filePath.test.ts @@ -72,11 +72,18 @@ describe("needsWorkspaceFileContents", () => { }); describe("workspace preview classifiers", () => { - it("treat # and ? as literal filename characters", () => { + it("recognize preview extensions and treat # and ? as literal filename characters", () => { for (const path of ["notes/clip#1.wav", "notes/clip?1.wav", "notes#old/clip.wav"]) { expect(isAudioPreviewFile(path)).toBe(true); } - for (const path of ["docs/draft#1.md", "docs/draft?1.md", "docs#old/draft.md"]) { + for (const path of [ + "docs/draft#1.md", + "docs/draft?1.md", + "docs#old/draft.md", + "CHANGELOG.markdown", + "notes.MDOWN", + "todo.mkd", + ]) { expect(isMarkdownPreviewFile(path)).toBe(true); } }); diff --git a/apps/mobile/src/features/files/filePath.ts b/apps/mobile/src/features/files/filePath.ts index be063da994f8d..9c9d16a6021c1 100644 --- a/apps/mobile/src/features/files/filePath.ts +++ b/apps/mobile/src/features/files/filePath.ts @@ -109,7 +109,7 @@ export function isSvgImagePreviewFile(path: string): boolean { } export function isMarkdownPreviewFile(path: string): boolean { - return /\.(?:md|mdx)$/i.test(path); + return /\.(?:md|mdx|markdown|mdown|mkd)$/i.test(path); } // Fork: Installers and archives download as bytes until upstream supports download-only workspace files. diff --git a/apps/mobile/src/features/home/HomeScreen.tsx b/apps/mobile/src/features/home/HomeScreen.tsx index 8110cca26793d..6f5db6a52f59c 100644 --- a/apps/mobile/src/features/home/HomeScreen.tsx +++ b/apps/mobile/src/features/home/HomeScreen.tsx @@ -469,7 +469,6 @@ export function HomeScreen(props: HomeScreenProps) { selectedThreadKey) } project={projectByKey.get(pendingScopeKey) ?? null} - projectTitle={v2ProjectTitleByProjectKey.get(pendingScopeKey)} environmentLabel={ Object.keys(props.savedConnectionsById).length > 1 ? (props.savedConnectionsById[item.pendingTask.environmentId]?.environmentLabel ?? diff --git a/apps/mobile/src/features/home/ThreadUndoNotice.tsx b/apps/mobile/src/features/home/ThreadUndoNotice.tsx index caeb47a3c2195..544a2909124ea 100644 --- a/apps/mobile/src/features/home/ThreadUndoNotice.tsx +++ b/apps/mobile/src/features/home/ThreadUndoNotice.tsx @@ -51,7 +51,11 @@ export function ThreadUndoNotice(props: { readonly visible: boolean }) { const bottom = Platform.OS === "ios" ? foldedResting - ? Math.max(rootMetrics.safeArea.bottom, keyboardHeight) + 8 + ? // Sit level with the rail's tab pill, which floats inside the bottom safe area. + Math.max( + rootMetrics.bottomBarInset ?? rootMetrics.safeArea.bottom + 8, + keyboardHeight + 8, + ) : rootMetrics?.hasHinge && rootMetrics.verticalBarEdge !== "none" ? // The column's safe area already includes UIKit's search toolbar in this posture. workspaceSearchBottomClearance({ diff --git a/apps/mobile/src/features/home/thread-swipe-actions.tsx b/apps/mobile/src/features/home/thread-swipe-actions.tsx index 532badd9b35cb..5a0305ca23a86 100644 --- a/apps/mobile/src/features/home/thread-swipe-actions.tsx +++ b/apps/mobile/src/features/home/thread-swipe-actions.tsx @@ -512,7 +512,9 @@ export function ThreadSwipeable(props: ThreadSwipeableProps) { overshootRight renderRightActions={renderRightActions} rightThreshold={actionsWidth * 0.42} - simultaneousWith={props.simultaneousWith} + // Only iPad trackpad pans need rows to swipe while the list scrolls; on Android touch it + // lets a diagonal swipe scroll the list, which the scroll gate turns into a closed row. + simultaneousWith={Platform.OS === "ios" ? props.simultaneousWith : undefined} > { /* oxlint-disable react/refs -- The render callback receives a close/open handler; only invoking that handler accesses the ref. */ diff --git a/apps/mobile/src/features/home/useThreadListActions.test.ts b/apps/mobile/src/features/home/useThreadListActions.test.ts index 0bea39e419e21..135cf4e1d85a7 100644 --- a/apps/mobile/src/features/home/useThreadListActions.test.ts +++ b/apps/mobile/src/features/home/useThreadListActions.test.ts @@ -21,6 +21,7 @@ const state = vi.hoisted(() => ({ failedAction: null as string | null, titleOutcome: null as ThreadTitleRegenerationOutcome | null, archivedRefreshes: [] as string[], + onArchivedRefresh: undefined as (() => void) | undefined, scopes: new Map>(), shells: [] as EnvironmentThreadShell[], requests: [] as { @@ -59,6 +60,7 @@ vi.mock("../../components/ui/Dialog", () => ({ vi.mock("../archive/useArchivedThreadSnapshots", () => ({ refreshArchivedThreadsForEnvironment: (environmentId: string) => { state.archivedRefreshes.push(environmentId); + state.onArchivedRefresh?.(); }, })); vi.mock("../../state/session", () => ({ @@ -71,8 +73,9 @@ vi.mock("../../state/presentation", () => ({ vi.mock("../../state/server", () => ({ environmentServerConfigsAtom: "server-configs", })); -vi.mock("../../state/atom-registry", () => ({ - appAtomRegistry: { +vi.mock("../../state/atom-registry", async () => { + const registry = (await import("effect/reactivity")).AtomRegistry.make(); + const stub = { set: (_atom: string, value: boolean) => { state.dropBusy = value; }, @@ -103,8 +106,21 @@ vi.mock("../../state/atom-registry", () => ({ }, ]), ), - }, -})); + }; + // Real atoms such as composerDraftsAtom go to a real registry; strings are stubbed state. + return { + appAtomRegistry: { + get: (atom: unknown) => + typeof atom === "string" + ? stub.get(atom) + : registry.get(atom as Parameters[0]), + set: (atom: unknown, value: unknown) => + typeof atom === "string" + ? stub.set(atom, value as boolean) + : registry.set(atom as Parameters[0], value), + }, + }; +}); vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: (command: unknown) => command, })); @@ -164,6 +180,8 @@ vi.mock("../../state/threads", () => ({ ), })); +import { appAtomRegistry } from "../../state/atom-registry"; +import { composerDraftsAtom } from "../../state/use-composer-drafts"; import { dismissThreadUndoNotice, readThreadUndoNotice } from "./thread-undo-notice"; import { useThreadListActions } from "./useThreadListActions"; @@ -197,6 +215,7 @@ beforeEach(() => { state.failedAction = null; state.titleOutcome = null; state.archivedRefreshes = []; + state.onArchivedRefresh = undefined; state.pendingOrder = null; state.dropBusy = false; state.holdingRows = false; @@ -250,7 +269,9 @@ describe("thread list operation permissions", () => { expect(state.requests).toEqual([]); }); - it("rechecks permission after a retained delete confirmation", async () => { + it("rechecks permission after a retained delete confirmation, then clears the draft", async () => { + const draft = { text: "unsent", attachments: [] }; + appAtomRegistry.set(composerDraftsAtom, { "primary:thread": draft, "primary:other": draft }); useThreadListActions().confirmDeleteThread(makeThread()); const confirm = state.dialogs[0]?.onConfirm; expect(confirm).toBeTypeOf("function"); @@ -259,6 +280,15 @@ describe("thread list operation permissions", () => { await confirm!(); expect(state.requests).toEqual([]); + expect(Object.keys(appAtomRegistry.get(composerDraftsAtom))).toContain("primary:thread"); + + state.scopes.get(primaryEnvironmentId)!.add(AuthOrchestrationOperateScope); + const deleted = new Promise((resolve) => (state.onArchivedRefresh = resolve)); + confirm!(); + await deleted; + + expect(state.requests).toEqual([expect.objectContaining({ action: "delete" })]); + expect(Object.keys(appAtomRegistry.get(composerDraftsAtom))).toEqual(["primary:other"]); }); }); @@ -594,6 +624,19 @@ describe("thread action undo", () => { await undo(); expect(state.requests.map(({ action }) => action)).toEqual(["unpin"]); }); + + it("keeps the earlier Undo until archiving succeeds", async () => { + const actions = useThreadListActions(); + const thread = makeThread(); + await actions.settleThread(thread); + state.failedAction = "archive"; + expect(await actions.archiveThread(thread)).toBe(false); + expect(readThreadUndoNotice()?.action).toBe("Settled"); + + state.failedAction = null; + expect(await actions.archiveThread(thread)).toBe(true); + expect(readThreadUndoNotice()).toBeNull(); + }); }); describe("thread archive guard", () => { diff --git a/apps/mobile/src/features/home/useThreadListActions.ts b/apps/mobile/src/features/home/useThreadListActions.ts index 3cd180db6273f..b100b6aa5e6da 100644 --- a/apps/mobile/src/features/home/useThreadListActions.ts +++ b/apps/mobile/src/features/home/useThreadListActions.ts @@ -17,6 +17,7 @@ import { appAtomRegistry } from "../../state/atom-registry"; import { environmentServerConfigsAtom } from "../../state/server"; import { connectedEnvironmentIdsAtom } from "../../state/presentation"; import { readEnvironmentScope } from "../../state/session"; +import { clearComposerDraft } from "../../state/use-composer-drafts"; import { environmentThreadShells, steadyNavigationThreadShellsAtom, @@ -115,6 +116,7 @@ function useThreadActionExecutor( action: ThreadListAction, thread: EnvironmentThreadShell, onStarted?: () => void, + restoreUnsettledAt?: string | null, ) => { if (!checkThreadOperationPermission(thread, actionFailureTitle(action))) return false; const key = scopedThreadKey(thread.environmentId, thread.id); @@ -145,9 +147,6 @@ function useThreadActionExecutor( return false; } if (action === "unsettle") ThreadUndo.invalidate("settle", key); - if (action === "archive" || action === "delete") { - for (const kind of ["settle", "snooze", "pin"]) ThreadUndo.invalidate(kind, key); - } onStarted?.(); const result = await withThreadDismissal( key, @@ -157,7 +156,11 @@ function useThreadActionExecutor( // suppressed until real activity clears the pin server-side. await unsettleMutation({ environmentId: thread.environmentId, - input: { threadId: thread.id, reason: "user" }, + input: { + threadId: thread.id, + reason: "user", + ...(restoreUnsettledAt === undefined ? {} : { restoreUnsettledAt }), + }, }) : await ( action === "settle" @@ -177,9 +180,13 @@ function useThreadActionExecutor( Dialog.alert(actionFailureTitle(action), actionFailureMessage(action, result.cause)); return false; } + if (action === "archive" || action === "delete") { + for (const kind of ["settle", "snooze", "pin"]) ThreadUndo.invalidate(kind, key); + } if (action === "archive" || action === "unarchive" || action === "delete") { refreshArchivedThreadsForEnvironment(thread.environmentId); } + if (action === "delete") clearComposerDraft(key); onCompleted?.(action, thread); return true; })(); @@ -263,11 +270,19 @@ export function useThreadListActions(): { async (step) => { switch (step.action) { case "unsettle": - return unsettleThread(thread); + return unsettleThread(thread, step.restoreUnsettledAt); case "unsnooze": return unsnoozeThread(thread); case "pin": return pinThread(thread, step.orderKey); + case "reorderActive": { + const result = await reorderActiveMutation({ + environmentId: thread.environmentId, + input: { threadId: thread.id, orderKey: step.orderKey }, + }); + if (result._tag === "Failure") throw Cause.squash(result.cause); + return true; + } case "snooze": { const failureTitle = action === "Unpinned" ? "Could not undo unpin" : "Could not undo settle"; @@ -411,8 +426,10 @@ export function useThreadListActions(): { snoozeInFlightThreadKeys.current.delete(key); return succeeded; }; - const unsettleThread = async (thread: EnvironmentThreadShell) => - (await executeAction("unsettle", thread)) === true; + const unsettleThread = async ( + thread: EnvironmentThreadShell, + restoreUnsettledAt?: string | null, + ) => (await executeAction("unsettle", thread, undefined, restoreUnsettledAt)) === true; const pinThread = async (thread: EnvironmentThreadShell, restoreOrderKey?: string) => { if (!checkThreadOperationPermission(thread, "Could not pin thread")) return false; if (!environmentConfigLoaded(thread.environmentId)) { diff --git a/apps/mobile/src/features/layout/createWorkspaceStackNavigator.ios.tsx b/apps/mobile/src/features/layout/createWorkspaceStackNavigator.ios.tsx index cad7dbcdd572c..afdf5748e38fd 100644 --- a/apps/mobile/src/features/layout/createWorkspaceStackNavigator.ios.tsx +++ b/apps/mobile/src/features/layout/createWorkspaceStackNavigator.ios.tsx @@ -110,7 +110,10 @@ function ColumnScreen(props: { readonly selectedThreadKey?: string | null; readonly selectedPullRequestKey?: string | null; readonly onDismiss: (routeKey: string, native: boolean) => void; - readonly onAppearance: (type: "willAppear" | "didAppear" | "willDisappear", key: string) => void; + readonly onAppearance: ( + type: "willAppear" | "didAppear" | "willDisappear" | "didDisappear", + key: string, + ) => void; readonly onCompose?: () => void; readonly navigation: ViewProps["navigation"]; readonly footer?: ReactNode; @@ -155,9 +158,10 @@ function ColumnScreen(props: { props.onAppearance("willDisappear", key); props.navigation.emit({ type: "transitionStart", target: key, data: { closing: true } }); }} - onDidDisappear={() => - props.navigation.emit({ type: "transitionEnd", target: key, data: { closing: true } }) - } + onDidDisappear={() => { + props.onAppearance("didDisappear", key); + props.navigation.emit({ type: "transitionEnd", target: key, data: { closing: true } }); + }} > @@ -328,7 +332,7 @@ function WorkspaceColumns( } }; const followCollapsedSplit = ( - type: "willAppear" | "didAppear" | "willDisappear", + type: "willAppear" | "didAppear" | "willDisappear" | "didDisappear", key: string, ) => { if (!collapsed.current || layout.usesSplitView) return; @@ -558,7 +562,6 @@ function WorkspaceStackView(props: ViewProps & { readonly nativeColumns: boolean props.nativeColumns && layout.usesSplitView && !workspacePage && deviceType === "pad" } hidden={ - !workspacePage && projection.detail.length > 0 && // Fork: Keep compact iPad destination navigation visible until upstream makes sidebar utilities width-aware. !(deviceType === "pad" && projection.detail.length === 1) && diff --git a/apps/mobile/src/features/layout/workspace/WorkspaceRootScreen.tsx b/apps/mobile/src/features/layout/workspace/WorkspaceRootScreen.tsx index d27f423b1d2d7..3fa34bf2dff8f 100644 --- a/apps/mobile/src/features/layout/workspace/WorkspaceRootScreen.tsx +++ b/apps/mobile/src/features/layout/workspace/WorkspaceRootScreen.tsx @@ -102,8 +102,8 @@ function WorkspaceRootContent() { layout.variant === "medium" && state.selectedTab === "usage" ? workspaceDockBottomClearance({ bottomInset: insets.bottom, keyboardHeight }) : isDuoDevice(deviceType) - ? // The column's safe area already holds the Duo's search toolbar and keyboard. - Math.max(insets.bottom, keyboardHeight) + 16 + ? // The column's safe area holds the Duo's search toolbar, but not the sidebar's own tab bar. + Math.max(insets.bottom + (sidebarPicker ? 64 : 0), keyboardHeight) + 16 : compactTabs || deviceType === "pad" ? workspaceDockBottomClearance({ bottomInset: insets.bottom, keyboardHeight }) : Math.max(insets.bottom, keyboardHeight) + diff --git a/apps/mobile/src/features/pull-requests/PullRequestDiscussionScreen.tsx b/apps/mobile/src/features/pull-requests/PullRequestDiscussionScreen.tsx index e92debeea0f1d..9d59324c1e4a6 100644 --- a/apps/mobile/src/features/pull-requests/PullRequestDiscussionScreen.tsx +++ b/apps/mobile/src/features/pull-requests/PullRequestDiscussionScreen.tsx @@ -21,7 +21,7 @@ import { AppText as Text, AppTextInput as TextInput } from "../../components/ui/ import { AppBar } from "../../components/ui/AppBar"; import { Button } from "../../components/ui/Button"; import { GlassSurface } from "../../components/ui/GlassSurface"; -import { relativeTime } from "../../lib/time"; +import { compareTimestamps, relativeTime } from "../../lib/time"; import { composerPullRequests } from "../../state/pull-requests"; import { useAtomCommand } from "../../state/use-atom-command"; import { useEnvironmentQuery } from "../../state/query"; @@ -147,7 +147,7 @@ function Discussion({ params }: { params: PullRequestParams & { readonly discuss ...new Map( [...extra, ...(thread?.comments ?? [])].map((comment) => [comment.id, comment]), ).values(), - ].sort((left, right) => left.createdAt.localeCompare(right.createdAt)); + ].sort((left, right) => compareTimestamps(left.createdAt, right.createdAt)); const scrollRef = useRef(null); const pendingTop = useRef(null); const positionedComment = useRef(null); diff --git a/apps/mobile/src/features/pull-requests/PullRequestListFilters.tsx b/apps/mobile/src/features/pull-requests/PullRequestListFilters.tsx index fbcb62406b7a7..5bec031fb735f 100644 --- a/apps/mobile/src/features/pull-requests/PullRequestListFilters.tsx +++ b/apps/mobile/src/features/pull-requests/PullRequestListFilters.tsx @@ -41,12 +41,18 @@ function choices(field: string, options: Readonly>, selec })); } -type ProjectOrder = readonly { readonly memberProjectRefs: readonly ScopedProjectRef[] }[]; +type ProjectOrder = readonly { + readonly memberProjectRefs: readonly ScopedProjectRef[]; + readonly pinned: boolean; + readonly archived: boolean; +}[]; const sameProjectOrder = (previous: ProjectOrder, next: ProjectOrder) => previous.length === next.length && previous.every((group, index) => { const members = next[index]!.memberProjectRefs; return ( + group.pinned === next[index]!.pinned && + group.archived === next[index]!.archived && group.memberProjectRefs.length === members.length && group.memberProjectRefs.every( (ref, member) => @@ -69,7 +75,11 @@ const sidebarProjectOrderFamily = Atom.family((mode: SidebarProjectGroupingMode) threads: get(environmentThreadShells.threadShellsAtom), pendingTasks: [], projectSortOrder: "updated_at", - }).map((scope) => ({ memberProjectRefs: scope.projectRefs })), + }).map((scope) => ({ + memberProjectRefs: scope.projectRefs, + pinned: scope.pinned, + archived: scope.archived, + })), ).pipe(Atom.withEquality(sameProjectOrder)), ); diff --git a/apps/mobile/src/features/pull-requests/PullRequestMergeControls.tsx b/apps/mobile/src/features/pull-requests/PullRequestMergeControls.tsx index 2d0414b503653..3889dd87ed2f4 100644 --- a/apps/mobile/src/features/pull-requests/PullRequestMergeControls.tsx +++ b/apps/mobile/src/features/pull-requests/PullRequestMergeControls.tsx @@ -3,12 +3,14 @@ import { useAtomRefresh, useAtomSet, useAtomValue } from "@effect/atom-react"; import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; import type { PullRequestDetail, PullRequestMergeMethod } from "@t3tools/contracts"; +import { resolveProjectSettings } from "@t3tools/shared/projectSettings"; import { AsyncResult } from "effect/reactivity"; import { useRef, useState } from "react"; import { View } from "react-native"; import { AppText as Text } from "../../components/ui/AppText"; import { Button } from "../../components/ui/Button"; import { SplitButton } from "../../components/ui/SplitButton"; +import { useEnvironmentServerConfig } from "../../state/entities"; import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences"; import { composerPullRequests } from "../../state/pull-requests"; import { useAtomCommand } from "../../state/use-atom-command"; @@ -19,6 +21,7 @@ import { MERGE_METHODS, PR_ACTION_ICONS, pullRequestActionLabel, + resolveMergeMethod, } from "./pullRequestActions"; import { pullRequestStackMerge, singlePullRequestMergeAllowed } from "./pullRequestStack"; import { pullRequestTarget, type PullRequestParams } from "./usePullRequestDiff"; @@ -39,6 +42,7 @@ export function PullRequestMergeControls(props: { const permissions = usePullRequestPermissions(props.params, pr); const stateAction = usePullRequestAction(props.params, pr); const preferences = useAtomValue(mobilePreferencesAtom); + const serverSettings = useEnvironmentServerConfig(target.environmentId)?.settings; const savePreferences = useAtomSet(updateMobilePreferencesAtom); const runAction = useAtomCommand(composerPullRequests.runAction, { reportFailure: false }); const refreshDetail = useAtomRefresh(composerPullRequests.detail(target)); @@ -47,15 +51,15 @@ export function PullRequestMergeControls(props: { const [busy, setBusy] = useState(false); const pending = useRef(false); const methods = allowedMergeMethods(pr); - const preferred = - selectedMethod ?? - (AsyncResult.isSuccess(preferences) ? preferences.value.pullRequestMergeMethod : undefined) ?? - "squash"; - const method = methods.includes(preferred) - ? preferred - : methods.includes("squash") - ? "squash" - : methods[0]; + const method = resolveMergeMethod( + pr, + selectedMethod, + serverSettings + ? resolveProjectSettings(serverSettings, target.input.projectId).settings + .pullRequestMergeMethod + : null, + AsyncResult.isSuccess(preferences) ? preferences.value.pullRequestMergeMethod : undefined, + ); const scope = stack.stack ? pullRequestStackMerge(stack.stack, pr.number, terminology) : null; const canMergeSingle = singlePullRequestMergeAllowed({ supportsStackActions: stack.supportsStackActions, diff --git a/apps/mobile/src/features/pull-requests/PullRequestRouteScreen.tsx b/apps/mobile/src/features/pull-requests/PullRequestRouteScreen.tsx index e742f607c2c90..929474897af73 100644 --- a/apps/mobile/src/features/pull-requests/PullRequestRouteScreen.tsx +++ b/apps/mobile/src/features/pull-requests/PullRequestRouteScreen.tsx @@ -4,6 +4,7 @@ import { pullRequestAnchorMatchesUrl, pullRequestDiffPathMatches, pullRequestLinkCommit, + resolvePullRequestAnchorRange, resolvePullRequestReviewAnchor, } from "@t3tools/shared/pullRequestAnchor"; import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator"; @@ -12,12 +13,9 @@ import { pullRequestTabOffset } from "./pullRequestTabScroll"; import { guideFocusedRows, type GuideDiffFocus } from "./guide/guideDiffFocus"; import { PullRequestGuideContent } from "./guide/PullRequestGuideScreen"; import { useReviewGuidesEnabled } from "./guide/useReviewGuide"; -import { - filterPullRequestActivity, - DEFAULT_ACTIVITY_FILTER, - type ActivityFilter, -} from "./pullRequestActivityFilter"; +import { filterPullRequestActivity, type ActivityFilter } from "./pullRequestActivityFilter"; import { PullRequestActivityFilters } from "./PullRequestActivityFilters"; +import { usePullRequestActivityFilter } from "./usePullRequestActivityFilter"; import type { LegendListRef } from "@legendapp/list/react-native"; import { AnimatedLegendList } from "@legendapp/list/reanimated"; import Animated from "react-native-reanimated"; @@ -143,15 +141,13 @@ function usePullRequestActivityRows(params: PullRequestParams, filter: ActivityF const filtered = result.data ? filterPullRequestActivity(result.data, filter) : null; const rows = (() => { if (!filtered) return []; - const rows = pullRequestTimelineRows(filtered.activity, params, navigation, { - permissions, - onChanged: result.refresh, - }); - if (filter.newestFirst) { - const notice = rows.at(-1)?.key === "truncated" ? rows.pop() : undefined; - rows.reverse(); - if (notice) rows.push(notice); - } + const rows = pullRequestTimelineRows( + filtered.activity, + params, + navigation, + { permissions, onChanged: result.refresh }, + filter.newestFirst, + ); if (!rows.length) rows.push({ key: "empty", @@ -385,14 +381,8 @@ export function PullRequestRouteScreen({ route }: StaticScreenProps !guidesEnabled && tab === "guide" ? "overview" : tab; const section = shown(chosenSection); - const [activityFilter, setActivityFilter] = useState(() => { - const kind = parsePullRequestAnchor(params.navigationUrl)?.kind; - return kind === "timeline" - ? { ...DEFAULT_ACTIVITY_FILTER, bots: true, resolved: true } - : kind === "commits" - ? { ...DEFAULT_ACTIVITY_FILTER, kind: "commits" } - : DEFAULT_ACTIVITY_FILTER; - }); + const activity = usePullRequestActivityFilter(parsePullRequestAnchor(params.navigationUrl)?.kind); + const activityFilter = activity.filter; const activityContent = usePullRequestActivityRows(params, activityFilter); const scrollOffsets = useRef>({}); // The offset of the tab last shown, and which tab that was: a tab standing in for a hidden guide @@ -444,10 +434,10 @@ export function PullRequestRouteScreen({ route }: StaticScreenProps ({ ...filter, kind: "all", bots: true, resolved: true })); + activity.showTimeline(); } else if (target?.kind === "commits") { selectSection("activity"); - setActivityFilter((filter) => ({ ...filter, kind: "commits" })); + activity.showCommits(); } else if (target?.kind === "checks") { selectSection("overview"); // Checks head Overview, which a reused screen may have scrolled past. @@ -833,7 +823,7 @@ export function PullRequestRouteScreen({ route }: StaticScreenProps ) : null} @@ -1013,6 +1003,7 @@ function PullRequestFileRouteContent({ route }: PullRequestFileRouteProps) { !staleDiff && !detail.isPending && !result.isPending && + !result.withheldRefetching && !result.error; const activity = useEnvironmentQuery(composerPullRequests.activity(requestTarget(route.params))); const baseData = useMemo(() => getCachedNativeReviewDiffData({ parsedDiff: parsed }), [parsed]); @@ -1075,7 +1066,15 @@ function PullRequestFileRouteContent({ route }: PullRequestFileRouteProps) { : !jumpPath || requestedFile ? data.files[0] : undefined); - const range = pickedPath === null && linkAnchor?.kind === "diff" ? linkAnchor.range : null; + const linkRange = pickedPath === null && linkAnchor?.kind === "diff" ? linkAnchor.range : null; + const range = + linkRange && + resolvePullRequestAnchorRange(linkRange, (line) => + data.rows.some( + (row) => + row.fileId === targetFile?.id && row.change === "delete" && row.oldLineNumber === line, + ), + ); const focus = route.params.focus ?? (range @@ -1332,6 +1331,10 @@ function PullRequestFileRouteContent({ route }: PullRequestFileRouteProps) { }); else void viewed.toggle(file.path); }} + onPressNotice={(event) => { + const file = data.files.find((file) => file.id === event.nativeEvent.fileId); + if (file) result.loadWithheldFile(file.path); + }} onToggleComment={(event) => { const id = event.nativeEvent.commentId; if (id?.startsWith("discussion:")) diff --git a/apps/mobile/src/features/pull-requests/PullRequestStatusRows.tsx b/apps/mobile/src/features/pull-requests/PullRequestStatusRows.tsx index 03ba26f2247cf..805d61c928378 100644 --- a/apps/mobile/src/features/pull-requests/PullRequestStatusRows.tsx +++ b/apps/mobile/src/features/pull-requests/PullRequestStatusRows.tsx @@ -27,6 +27,7 @@ import { } from "./pullRequestActions"; import { checkStatus, + isFailingCheck, pullRequestBranchSummary, pullRequestChecksSummary, pullRequestReviewSummary, @@ -132,8 +133,7 @@ export function PullRequestChecksRow(props: RowProps) { exiting={PR_ROW_EXIT} className="pl-8" > - {props.pr.state === "open" && - props.pr.checks.some((check) => check.status === "failure") ? ( + {props.pr.state === "open" && props.pr.checks.some(isFailingCheck) ? ( ) : null} {action.available("approve-workflows") ? ( diff --git a/apps/mobile/src/features/pull-requests/PullRequestTimeline.tsx b/apps/mobile/src/features/pull-requests/PullRequestTimeline.tsx index 4b7ba3a968187..586d9947510f3 100644 --- a/apps/mobile/src/features/pull-requests/PullRequestTimeline.tsx +++ b/apps/mobile/src/features/pull-requests/PullRequestTimeline.tsx @@ -17,7 +17,7 @@ import { Pressable, StyleSheet, View } from "react-native"; import { parseMarkdownDocument } from "../../lib/markdownDocument"; import { AppText as Text } from "../../components/ui/AppText"; import { SymbolView, type AppSymbolName } from "../../components/ui/AppSymbol"; -import { relativeTime } from "../../lib/time"; +import { byTimestamp, relativeTime } from "../../lib/time"; import { PullRequestAvatar } from "./PullRequestPresentation"; import { PullRequestMarkdown } from "./PullRequestMarkdown"; import { PullRequestCommentFooter } from "./PullRequestReactions"; @@ -218,6 +218,7 @@ export function pullRequestTimelineRows( params: PullRequestParams, navigation: Pick, "navigate" | "dispatch">, actions: CommentActions, + newestFirst: boolean, ) { const linkTarget = parsePullRequestAnchor(params.navigationUrl); const threads = pullRequestConversationThreads(activity); @@ -320,7 +321,7 @@ export function pullRequestTimelineRows( ), })), - ].sort((a, b) => a.date.localeCompare(b.date)); + ].sort(byTimestamp(newestFirst)); if (activity.commentsTruncated || activity.reviewThreadsTruncated) events.push({ key: "truncated", diff --git a/apps/mobile/src/features/pull-requests/pullRequestActions.test.ts b/apps/mobile/src/features/pull-requests/pullRequestActions.test.ts index 2234a31081802..6862b5b31b2fb 100644 --- a/apps/mobile/src/features/pull-requests/pullRequestActions.test.ts +++ b/apps/mobile/src/features/pull-requests/pullRequestActions.test.ts @@ -5,6 +5,7 @@ import { canPerformPullRequestAction, mergeUnavailableReason, pullRequestActionLabel, + resolveMergeMethod, } from "./pullRequestActions"; const GITHUB = sourceControlClients.get("github").changeRequest; @@ -65,6 +66,22 @@ describe("mobile pull request actions", () => { expect(mergeUnavailableReason(pr, true, GITHUB)).toContain("conflicts"); }); + it("picks the chosen, project default, then last used merge method the repository allows", () => { + const pr = detail(); + expect(resolveMergeMethod(pr, null, "merge", "squash")).toBe("merge"); + expect(resolveMergeMethod(pr, "squash", "merge", "merge")).toBe("squash"); + expect(resolveMergeMethod(pr, null, "rebase", "merge")).toBe("merge"); + expect(resolveMergeMethod(pr, null, null, undefined)).toBe("squash"); + expect( + resolveMergeMethod( + detail({ mergeCapabilities: { merge: true, squash: false, rebase: false } }), + null, + null, + "rebase", + ), + ).toBe("merge"); + }); + it("names the change request with its host's term", () => { expect(pullRequestActionLabel("close", GITHUB)).toBe("Close pull request"); expect(pullRequestActionLabel("close", GITLAB)).toBe("Close merge request"); diff --git a/apps/mobile/src/features/pull-requests/pullRequestActions.ts b/apps/mobile/src/features/pull-requests/pullRequestActions.ts index 132501b60a240..967689b57d84e 100644 --- a/apps/mobile/src/features/pull-requests/pullRequestActions.ts +++ b/apps/mobile/src/features/pull-requests/pullRequestActions.ts @@ -87,6 +87,20 @@ export function allowedMergeMethods(pr: ActionDetail) { return pr.capabilities.mergeMethods.filter((method) => pr.mergeCapabilities[method]); } +export function resolveMergeMethod( + pr: ActionDetail, + selected: PullRequestMergeMethod | null, + projectDefault: PullRequestMergeMethod | null, + lastUsed: PullRequestMergeMethod | undefined, +) { + const methods = allowedMergeMethods(pr); + return ( + [selected, projectDefault, lastUsed, "squash" as const].find( + (method) => method != null && methods.includes(method), + ) ?? methods[0] + ); +} + export function allowedUpdateMethods(pr: ActionDetail) { return (pr.capabilities.updateMethods ?? []).filter((method) => pr.viewerPermissions.updateMethods?.includes(method), diff --git a/apps/mobile/src/features/pull-requests/pullRequestDiffPages.test.ts b/apps/mobile/src/features/pull-requests/pullRequestDiffPages.test.ts index 82027addc72cc..b0c91ab3ada9e 100644 --- a/apps/mobile/src/features/pull-requests/pullRequestDiffPages.test.ts +++ b/apps/mobile/src/features/pull-requests/pullRequestDiffPages.test.ts @@ -7,6 +7,8 @@ import { combinePullRequestDiffPages, getCachedPullRequestDiffPages, retainPullRequestDiffPage, + withWithheldFiles, + type WithheldFileLoad, } from "./pullRequestDiffPages"; const GITHUB = sourceControlClients.get("github").changeRequest; @@ -156,4 +158,41 @@ describe("paged mobile pull request diffs", () => { expect(refreshed).toHaveLength(1); expect(refreshed[0]?.result.patch).toContain("changed.ts"); }); + + it("swaps a loaded withheld file in under its id and says what a tap does for the rest", () => { + const pages = [{ cursor: null, result: withheldDiff() }]; + const parsed = combinePullRequestDiffPages(pages, new WeakMap(), "pr", GITHUB); + if (parsed.kind !== "files") throw new Error("Expected structured diff"); + const placeholder = parsed.files[0]; + let merged: ReviewParsedDiff = parsed; + const fileWith = (load: WithheldFileLoad | undefined, canLoad = true) => { + merged = withWithheldFiles( + parsed, + new Map(load === undefined ? [] : [["large.ts", load]]), + canLoad, + "pr", + ); + return merged.kind === "files" ? merged.files[0] : undefined; + }; + expect(fileWith(undefined)).toMatchObject({ + notice: "Diff too large to display. Tap to load it.", + noticeTone: "warning", + noticeActionable: true, + }); + expect(fileWith(undefined, false)).toMatchObject({ + notice: "Diff too large to display here.", + noticeActionable: false, + }); + expect(fileWith({ status: "loading" })).toMatchObject({ noticeActionable: false }); + expect(fileWith({ status: "error" })).toMatchObject({ + notice: "Couldn't load this diff. Tap to try again.", + noticeActionable: true, + }); + const loaded = fileWith({ status: "loaded", result: diff("large.ts") }); + expect(loaded).toMatchObject({ id: placeholder?.id, path: "large.ts" }); + expect(loaded?.notice).toBeUndefined(); + expect(loaded?.rows.length).toBeGreaterThan(0); + // The totals follow the loaded file, which may have changed since the page was read. + expect(merged).toMatchObject({ additions: 1, deletions: 1 }); + }); }); diff --git a/apps/mobile/src/features/pull-requests/pullRequestDiffPages.ts b/apps/mobile/src/features/pull-requests/pullRequestDiffPages.ts index 9abf7cfc0c074..8ec7cea70bf32 100644 --- a/apps/mobile/src/features/pull-requests/pullRequestDiffPages.ts +++ b/apps/mobile/src/features/pull-requests/pullRequestDiffPages.ts @@ -1,6 +1,10 @@ import type { ChangeRequestTerminology } from "@t3tools/client-runtime/source-control-clients"; import type { PullRequestDiffResult } from "@t3tools/contracts"; -import { buildReviewParsedDiff, type ReviewParsedDiff } from "../review/reviewModel"; +import { + buildReviewParsedDiff, + type ReviewParsedDiff, + type ReviewRenderableFile, +} from "../review/reviewModel"; export interface PullRequestDiffPage { readonly cursor: string | null; @@ -61,6 +65,7 @@ export function retainPullRequestDiffPage( (previous?.patch === result.patch && previous.truncated === result.truncated && previous.nextCursor === result.nextCursor && + previous.withheldFilesReadable === result.withheldFilesReadable && JSON.stringify(previous.omittedFileStats ?? []) === JSON.stringify(result.omittedFileStats ?? [])) ) @@ -84,7 +89,13 @@ export function combinePullRequestDiffPages( ); const files = value.files.map((file) => { const stat = omittedFileStats.get(file.path); - return stat ? { ...file, additions: stat.additions, deletions: stat.deletions } : file; + if (!stat) return file; + return { + ...file, + additions: stat.additions, + deletions: stat.deletions, + ...(file.rows.length === 0 ? { withheld: true } : {}), + }; }); value = { ...value, @@ -111,3 +122,63 @@ export function combinePullRequestDiffPages( : null, }; } + +export type WithheldFileLoad = + | { readonly status: "loading" | "error" } + | { readonly status: "loaded"; readonly result: PullRequestDiffResult }; + +const withheldFileParses = new WeakMap(); + +function parseWithheldFile(result: PullRequestDiffResult, scope: string) { + if (withheldFileParses.has(result)) return withheldFileParses.get(result) ?? null; + const parsed = buildReviewParsedDiff(result.patch, scope); + const file = + parsed.kind === "files" ? (parsed.files.find((entry) => entry.rows.length > 0) ?? null) : null; + withheldFileParses.set(result, file); + return file; +} + +function withheldFileNotice(load: WithheldFileLoad | undefined, canLoad: boolean): string { + if (load === undefined) { + return canLoad + ? "Diff too large to display. Tap to load it." + : "Diff too large to display here."; + } + if (load.status === "loading") return "Loading diff…"; + if (load.status === "error") return "Couldn't load this diff. Tap to try again."; + return "No text diff for this file."; +} + +/** + * Puts each loaded withheld file in place of its empty one, under the same id so folds and + * targets hold, and gives the rest a notice saying what a tap does. + */ +export function withWithheldFiles( + parsed: ReviewParsedDiff, + loads: ReadonlyMap, + canLoad: boolean, + scope: string, +): ReviewParsedDiff { + if (parsed.kind !== "files" || !parsed.files.some((file) => file.withheld)) return parsed; + const files = parsed.files.map((file): ReviewRenderableFile => { + if (!file.withheld) return file; + const load = loads.get(file.path); + const loaded = + load?.status === "loaded" + ? parseWithheldFile(load.result, `${scope}:withheld:${file.path}`) + : null; + if (loaded !== null) return { ...loaded, id: file.id }; + return { + ...file, + notice: withheldFileNotice(load, canLoad), + noticeTone: "warning", + noticeActionable: canLoad && (load === undefined || load.status === "error"), + }; + }); + return { + ...parsed, + files, + additions: files.reduce((total, file) => total + file.additions, 0), + deletions: files.reduce((total, file) => total + file.deletions, 0), + }; +} diff --git a/apps/mobile/src/features/pull-requests/pullRequestList.ts b/apps/mobile/src/features/pull-requests/pullRequestList.ts index 674d3b0e24fe3..5a4572dbde803 100644 --- a/apps/mobile/src/features/pull-requests/pullRequestList.ts +++ b/apps/mobile/src/features/pull-requests/pullRequestList.ts @@ -11,6 +11,7 @@ import { type PullRequestListStatsInput, } from "@t3tools/contracts"; import type { PullRequestListControls } from "../../persistence/mobile-preferences"; +import { compareTimestamps } from "../../lib/time"; export const PR_LIST_PAGE_SIZE = 30; export type { PullRequestListControls } from "../../persistence/mobile-preferences"; @@ -242,7 +243,7 @@ export function mergePullRequestListEntries( const held = entries.get(key); if (!held || (entry.observedAt ?? 0) >= (held.observedAt ?? 0)) entries.set(key, entry); } - return [...entries.values()].sort((a, b) => b.updatedAt.localeCompare(a.updatedAt)); + return [...entries.values()].sort((a, b) => compareTimestamps(b.updatedAt, a.updatedAt)); } export interface PullRequestListPages { @@ -455,10 +456,10 @@ export function filterPullRequestList< } return [...unique.values()].sort((a, b) => controls.sort === "created" - ? b.createdAt.localeCompare(a.createdAt) + ? compareTimestamps(b.createdAt, a.createdAt) : controls.sort === "oldest" - ? a.updatedAt.localeCompare(b.updatedAt) - : b.updatedAt.localeCompare(a.updatedAt), + ? compareTimestamps(a.updatedAt, b.updatedAt) + : compareTimestamps(b.updatedAt, a.updatedAt), ); } diff --git a/apps/mobile/src/features/pull-requests/pullRequestStatus.test.ts b/apps/mobile/src/features/pull-requests/pullRequestStatus.test.ts index aef40179ded64..e5cb4551db56b 100644 --- a/apps/mobile/src/features/pull-requests/pullRequestStatus.test.ts +++ b/apps/mobile/src/features/pull-requests/pullRequestStatus.test.ts @@ -97,5 +97,8 @@ describe("mobile overview check status", () => { expect( pullRequestChecksSummary({ checks: [check("success"), check("pending"), check("failure")] }), ).toMatchObject({ title: "1 check failing", count: "1 of 3" }); + expect( + pullRequestChecksSummary({ checks: [check("success"), check("cancelled")] }), + ).toMatchObject({ title: "1 check failing", tint: "accent-danger-foreground" }); }); }); diff --git a/apps/mobile/src/features/pull-requests/pullRequestStatus.ts b/apps/mobile/src/features/pull-requests/pullRequestStatus.ts index 5426266dc6893..d664aef1fc015 100644 --- a/apps/mobile/src/features/pull-requests/pullRequestStatus.ts +++ b/apps/mobile/src/features/pull-requests/pullRequestStatus.ts @@ -88,8 +88,12 @@ export function checkStatus(check: PullRequestCheck) { } } +export function isFailingCheck(check: PullRequestCheck) { + return check.status === "failure" || check.status === "cancelled"; +} + export function pullRequestChecksSummary(pr: Pick) { - const failed = pr.checks.filter((check) => check.status === "failure").length; + const failed = pr.checks.filter(isFailingCheck).length; const required = pr.checks.filter((check) => check.status === "action-required").length; const pending = pr.checks.filter((check) => check.status === "pending").length; const passed = pr.checks.filter((check) => check.status === "success").length; diff --git a/apps/mobile/src/features/pull-requests/usePullRequestActivityFilter.ts b/apps/mobile/src/features/pull-requests/usePullRequestActivityFilter.ts new file mode 100644 index 0000000000000..1f2d0d8582de1 --- /dev/null +++ b/apps/mobile/src/features/pull-requests/usePullRequestActivityFilter.ts @@ -0,0 +1,52 @@ +import { useAtomSet, useAtomValue } from "@effect/atom-react"; +import { AsyncResult } from "effect/reactivity"; +import { useState } from "react"; +import type { PullRequestActivityVisibility } from "../../persistence/mobile-preferences"; +import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences"; +import { DEFAULT_ACTIVITY_FILTER, type ActivityFilter } from "./pullRequestActivityFilter"; + +const SHOW_EVERYTHING: PullRequestActivityVisibility = { bots: true, resolved: true }; + +/** + * Bot and resolved visibility is a saved preference. A timeline link shows everything for this + * screen only, until the user picks visibility themselves. + */ +export function usePullRequestActivityFilter(link: string | undefined) { + const preferences = useAtomValue(mobilePreferencesAtom); + const savePreferences = useAtomSet(updateMobilePreferencesAtom); + const [view, setView] = useState(() => ({ + kind: link === "commits" ? ("commits" as const) : DEFAULT_ACTIVITY_FILTER.kind, + newestFirst: DEFAULT_ACTIVITY_FILTER.newestFirst, + linkVisibility: link === "timeline" ? SHOW_EVERYTHING : null, + })); + const saved = AsyncResult.isSuccess(preferences) + ? preferences.value.pullRequestActivityVisibility + : undefined; + const visibility = view.linkVisibility ?? saved ?? DEFAULT_ACTIVITY_FILTER; + const filter: ActivityFilter = { + kind: view.kind, + newestFirst: view.newestFirst, + bots: visibility.bots, + resolved: visibility.resolved, + }; + const visibilityChanged = (next: ActivityFilter) => + next.bots !== filter.bots || next.resolved !== filter.resolved; + return { + filter, + setFilter: (next: ActivityFilter) => { + if (visibilityChanged(next)) { + savePreferences({ + pullRequestActivityVisibility: { bots: next.bots, resolved: next.resolved }, + }); + } + setView((current) => ({ + kind: next.kind, + newestFirst: next.newestFirst, + linkVisibility: visibilityChanged(next) ? null : current.linkVisibility, + })); + }, + showTimeline: () => + setView((current) => ({ ...current, kind: "all", linkVisibility: SHOW_EVERYTHING })), + showCommits: () => setView((current) => ({ ...current, kind: "commits" })), + }; +} diff --git a/apps/mobile/src/features/pull-requests/usePullRequestDiff.ts b/apps/mobile/src/features/pull-requests/usePullRequestDiff.ts index 6e49993a137f3..f091e5b6847d6 100644 --- a/apps/mobile/src/features/pull-requests/usePullRequestDiff.ts +++ b/apps/mobile/src/features/pull-requests/usePullRequestDiff.ts @@ -5,8 +5,9 @@ import { type PullRequestListEntry, type PullRequestSummary, } from "@t3tools/contracts"; -import { useAtomRefresh } from "@effect/atom-react"; -import { useLayoutEffect, useMemo, useState } from "react"; +import { RegistryContext, useAtomRefresh, useAtomValue } from "@effect/atom-react"; +import { Atom } from "effect/reactivity"; +import { useContext, useLayoutEffect, useMemo, useState } from "react"; import { composerPullRequests } from "../../state/pull-requests"; import { useEnvironmentQuery } from "../../state/query"; @@ -14,7 +15,9 @@ import { usePullToRefresh } from "../../state/use-pull-to-refresh"; import { getCachedPullRequestDiffPages, retainPullRequestDiffPage, + withWithheldFiles, type PullRequestDiffPage, + type WithheldFileLoad, } from "./pullRequestDiffPages"; export type PullRequestParams = { @@ -43,6 +46,26 @@ export function pullRequestTarget(params: PullRequestParams) { } const NO_PAGES: readonly PullRequestDiffPage[] = []; +const NO_PATHS: readonly string[] = []; + +type PullRequestDiffTarget = Parameters[0]; + +// Keyed by the diff's serialized target, which the hook already holds as its scope. +const withheldFileDiff = (targetKey: string, path: string) => { + const target = JSON.parse(targetKey) as PullRequestDiffTarget; + return composerPullRequests.diff({ ...target, input: { ...target.input, path } }); +}; + +// One read per withheld file the reader asked for, so only that file's diff crosses the wire. +const withheldFileDiffsAtom = Atom.family((key: string) => { + const { targetKey, paths } = JSON.parse(key) as { + readonly targetKey: string; + readonly paths: readonly string[]; + }; + return Atom.make((get) => paths.map((path) => get(withheldFileDiff(targetKey, path)))).pipe( + Atom.withLabel(`mobile:pull-request-withheld-file-diffs:${key}`), + ); +}); export function usePullRequestDiffEntry(navigation: { readonly addListener: ( @@ -116,10 +139,46 @@ export function usePullRequestDiff( }, [cursor, key, query.data, state.key, state.pages]); if (state.key !== key || state.pages !== pages) setState({ key, cursor, pages }); - const parsed = useMemo( + const [withheldLoads, setWithheldLoads] = useState<{ + readonly key: string; + readonly paths: readonly string[]; + }>({ key: "", paths: NO_PATHS }); + const withheldPaths = withheldLoads.key === key ? withheldLoads.paths : NO_PATHS; + const withheldResults = useAtomValue( + withheldFileDiffsAtom(JSON.stringify({ targetKey: key, paths: withheldPaths })), + ); + const registry = useContext(RegistryContext); + const refreshWithheldFiles = () => { + for (const path of withheldPaths) registry.refresh(withheldFileDiff(key, path)); + }; + const loadWithheldFile = (path: string) => { + // The read is keyed without the head, so even a first load must not reuse an older one. + registry.refresh(withheldFileDiff(key, path)); + if (!withheldPaths.includes(path)) setWithheldLoads({ key, paths: [...withheldPaths, path] }); + }; + // A refetch keeps showing the last patch, whose lines may not exist at the new head. + const withheldRefetching = withheldResults.some((result) => result.waiting); + const loads = new Map(); + withheldPaths.forEach((path, index) => { + const result = withheldResults[index]; + if (result === undefined) return; + // A failed refetch still carries the last patch; show the failure instead. + loads.set( + path, + result._tag === "Failure" + ? { status: "error" } + : result._tag === "Success" + ? { status: "loaded", result: result.value } + : { status: "loading" }, + ); + }); + const pagesParsed = useMemo( () => getCachedPullRequestDiffPages(pages, key, terminology), [key, pages, terminology], ); + // The server says whether a withheld file can be read alone; GitHub may refuse the whole diff. + const withheldFilesReadable = pages[0]?.result.withheldFilesReadable === true; + const parsed = withWithheldFiles(pagesParsed, loads, withheldFilesReadable, key); const patch = useMemo(() => pages.map((page) => page.result.patch).join("\n"), [pages]); const nextCursor = pages.at(-1)?.result.nextCursor ?? null; const canLoadMore = @@ -136,15 +195,19 @@ export function usePullRequestDiff( } setState({ key, cursor: null, pages: NO_PAGES }); refreshFirstPage(); + refreshWithheldFiles(); }; const onPullRefresh = () => { setState({ key, cursor: null, pages: NO_PAGES }); onRefresh(); + refreshWithheldFiles(); }; return { ...query, refresh, parsed, + loadWithheldFile, + withheldRefetching, patch, nextCursor, canLoadMore, diff --git a/apps/mobile/src/features/pull-requests/usePullRequestList.ts b/apps/mobile/src/features/pull-requests/usePullRequestList.ts index d4fe1ab3461bd..8ba0721fd7fb5 100644 --- a/apps/mobile/src/features/pull-requests/usePullRequestList.ts +++ b/apps/mobile/src/features/pull-requests/usePullRequestList.ts @@ -1,3 +1,8 @@ +import { + editHeldPullRequestListEntries, + pullRequestHostAnswer, + pullRequestPendingEditsFor, +} from "@t3tools/client-runtime/state/pull-requests"; import { RegistryContext, useAtomMount, useAtomSubscribe, useAtomValue } from "@effect/atom-react"; import { executeAtomQuery, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; import type { EnvironmentId, PullRequestDiffStat, PullRequestListInput } from "@t3tools/contracts"; @@ -31,7 +36,10 @@ type Read = (atom: Atom.Atom) => A; /** The head the list query holds now, cached or answered. */ function currentHead(read: Read, target: ListTarget) { - return AsyncResult.getOrElse(read(composerPullRequests.list(target)), () => null); + return AsyncResult.getOrElse( + read(pullRequestHostAnswer(composerPullRequests.list(target))), + () => null, + ); } function currentPages(read: Read, target: ListTarget): PullRequestListPages | null { @@ -44,12 +52,23 @@ function currentPages(read: Read, target: ListTarget): PullRequestListPages | nu const sourcesFamily = Atom.family((key: string) => { const targets: readonly ListTarget[] = JSON.parse(key); return Atom.make((get) => - targets.map((target) => ({ - target, - result: get(composerPullRequests.list(target)), - pages: currentPages(get, target), - stats: get(pullRequestListStatsFamily(target.environmentId)), - })), + targets.map((target) => { + const pages = currentPages(get, target); + return { + target, + result: get(composerPullRequests.list(target)), + pages: + pages === null + ? null + : { + ...pages, + entries: editHeldPullRequestListEntries(target.input, pages.entries, (entry) => + get(pullRequestPendingEditsFor(target.environmentId, entry)), + ), + }, + stats: get(pullRequestListStatsFamily(target.environmentId)), + }; + }), ); }); @@ -71,7 +90,7 @@ async function loadNextPages( continuations(registry, targets).map(async ({ target, pages, input }) => { const response = await executeAtomQuery( registry, - composerPullRequests.list({ ...target, input }), + pullRequestHostAnswer(composerPullRequests.list({ ...target, input })), { signal, refresh: retry, @@ -113,11 +132,15 @@ async function refreshHeads( input: {}, }); if (signal.aborted) return; - const response = await executeAtomQuery(registry, composerPullRequests.list(target), { - refresh: true, - signal, - reportFailure: false, - }); + const response = await executeAtomQuery( + registry, + pullRequestHostAnswer(composerPullRequests.list(target)), + { + refresh: true, + signal, + reportFailure: false, + }, + ); if (signal.aborted) return; if (response._tag === "Failure") throw squashAtomCommandFailure(response); registry.set( diff --git a/apps/mobile/src/features/review/ReviewCommentCard.tsx b/apps/mobile/src/features/review/ReviewCommentCard.tsx index a65bd83e9a66d..3e2cdc9ddfb6c 100644 --- a/apps/mobile/src/features/review/ReviewCommentCard.tsx +++ b/apps/mobile/src/features/review/ReviewCommentCard.tsx @@ -12,8 +12,10 @@ import { resolveNativeReviewDiffView } from "../diffs/nativeReviewDiffSurface"; import { buildNativeReviewDiffData, buildNativeReviewSnippetRows, + buildReviewCommentPatch, createNativeReviewDiffTheme, NATIVE_REVIEW_DIFF_CONTENT_WIDTH, + reviewCommentDiffText, } from "./nativeReviewDiffAdapter"; import { buildReviewParsedDiff } from "./reviewModel"; import { REVIEW_MONO_FONT_FAMILY } from "./reviewDiffRendering"; @@ -86,7 +88,7 @@ export const ReviewCommentCard = memo(function ReviewCommentCard(props: { theme: appearanceScheme, }); const snippetTokens = useMarkdownCodeHighlight({ - code: props.comment.diff.trim(), + code: reviewCommentDiffText(props.comment.diff), language: props.comment.fenceLanguage ?? language, enabled: compactNativeRows.length === 0, theme: appearanceScheme, @@ -234,32 +236,10 @@ export const ReviewCommentCard = memo(function ReviewCommentCard(props: { ) /* oxlint-enable react/no-array-index-key */, ) - : props.comment.diff.trim()} + : reviewCommentDiffText(props.comment.diff)} ) : null} ); }); - -function buildReviewCommentPatch(comment: ReviewInlineComment): string { - if ((comment.fenceLanguage ?? "diff") !== "diff") { - return ""; - } - const diff = comment.diff.trim(); - if (!diff) { - return ""; - } - - if (diff.startsWith("diff --git ")) { - return diff; - } - - const normalizedPath = comment.filePath.replaceAll("\\", "/"); - return [ - `diff --git a/${normalizedPath} b/${normalizedPath}`, - `--- a/${normalizedPath}`, - `+++ b/${normalizedPath}`, - diff, - ].join("\n"); -} diff --git a/apps/mobile/src/features/review/ReviewSheet.tsx b/apps/mobile/src/features/review/ReviewSheet.tsx index 21bf033b761be..7cf0716277b80 100644 --- a/apps/mobile/src/features/review/ReviewSheet.tsx +++ b/apps/mobile/src/features/review/ReviewSheet.tsx @@ -26,11 +26,10 @@ import { import { PaneListItem } from "../../components/ui/PaneListItem"; import { ScreenContent } from "../../components/ui/ScreenContent"; import { SheetPane } from "../../components/ui/SheetPane"; -import { environmentCatalog } from "../../connection/catalog"; import { useEnvironmentPresentation } from "../../state/presentation"; -import { useAtomCommand } from "../../state/use-atom-command"; import { useThreadDraftForThread } from "../../state/use-thread-composer-state"; import { EnvironmentConnectionNotice } from "../connection/EnvironmentConnectionNotice"; +import { useRetryEnvironment } from "../connection/useRetryEnvironment"; import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider"; import { Inspector, @@ -351,7 +350,7 @@ export function ReviewSheet(props: ReviewSheetProps) { const { themeAppearance: selectedTheme } = useAppearancePreferences(); const { environmentId, threadId } = props.route.params; const environment = useEnvironmentPresentation(environmentId); - const retryEnvironment = useAtomCommand(environmentCatalog.retryNow, "environment retry"); + const retryEnvironment = useRetryEnvironment(); const isEnvironmentReady = environment.presentation?.connection.phase === "connected"; const { draftMessage, draftContext } = useThreadDraftForThread({ environmentId, threadId }); const reviewCache = useReviewCacheForThread({ environmentId, threadId }); diff --git a/apps/mobile/src/features/review/nativeReviewDiffAdapter.test.ts b/apps/mobile/src/features/review/nativeReviewDiffAdapter.test.ts index 7e620836baccd..06c577df1bda0 100644 --- a/apps/mobile/src/features/review/nativeReviewDiffAdapter.test.ts +++ b/apps/mobile/src/features/review/nativeReviewDiffAdapter.test.ts @@ -8,7 +8,9 @@ import { combinePullRequestDiffPages } from "../pull-requests/pullRequestDiffPag import { createPullRequestDiffHighlightFiles } from "../pull-requests/pullRequestDiffHighlightFiles"; import { buildNativeReviewDiffData, + buildReviewCommentPatch, getCachedNativeReviewDiffData, + reviewCommentDiffText, } from "./nativeReviewDiffAdapter"; import type { ReviewInlineComment } from "./reviewCommentSelection"; import { buildReviewParsedDiff, type ReviewParsedDiff } from "./reviewModel"; @@ -46,6 +48,27 @@ function filesPatch(paths: ReadonlyArray) { .join("\n"); } +describe("buildReviewCommentPatch", () => { + it("keeps a selection's trailing blank line and whitespace", () => { + const rows = (diff: string) => + buildNativeReviewDiffData( + buildReviewParsedDiff( + buildReviewCommentPatch({ ...makeComment("Insert here."), diff }), + "review-comment-trailing-blank", + ), + ).rows.filter((row) => row.kind === "line"); + + expect(rows("@@ -3,1 +2,2 @@\n+two\t\n ")).toMatchObject([ + { content: "two\t", change: "add" }, + { content: "", change: "context" }, + ]); + expect(rows("@@ -4,1 +4,1 @@\n ")).toMatchObject([{ content: "", change: "context" }]); + expect(reviewCommentDiffText("\n@@ -3,1 +2,2 @@\n+two\t\n \n")).toBe( + "@@ -3,1 +2,2 @@\n+two\t\n ", + ); + }); +}); + describe("buildNativeReviewDiffData languages", () => { it.each([ ["js", "javascript"], diff --git a/apps/mobile/src/features/review/nativeReviewDiffAdapter.ts b/apps/mobile/src/features/review/nativeReviewDiffAdapter.ts index d55aa97e305c9..63372efb661bc 100644 --- a/apps/mobile/src/features/review/nativeReviewDiffAdapter.ts +++ b/apps/mobile/src/features/review/nativeReviewDiffAdapter.ts @@ -1,3 +1,4 @@ +import { cutLineNoteStart } from "@t3tools/shared/diffCutLines"; import type { NativeReviewDiffRow, NativeReviewDiffTheme } from "../diffs/nativeReviewDiffSurface"; import type { NativeReviewDiffFile } from "../diffs/nativeReviewDiffTypes"; import * as Arr from "effect/Array"; @@ -44,6 +45,32 @@ export function buildNativeReviewSnippetRows( })); } +export function reviewCommentDiffText(diff: string): string { + return diff.trimStart().replace(/\n+$/, ""); +} + +export function buildReviewCommentPatch(comment: ReviewInlineComment): string { + if ((comment.fenceLanguage ?? "diff") !== "diff") { + return ""; + } + if (!comment.diff.trim()) { + return ""; + } + const diff = reviewCommentDiffText(comment.diff); + + if (diff.startsWith("diff --git ")) { + return diff; + } + + const normalizedPath = comment.filePath.replaceAll("\\", "/"); + return [ + `diff --git a/${normalizedPath} b/${normalizedPath}`, + `--- a/${normalizedPath}`, + `+++ b/${normalizedPath}`, + diff, + ].join("\n"); +} + function opaqueNativeHexColor(color: string, background: string): string { const hex = NATIVE_HEX_COLOR.exec(color); if (hex && !hex[4]) return color; @@ -200,6 +227,8 @@ export function createNativeReviewDiffTheme( deleteBar: terminalRed ?? "#ff2e3f", addText: "#5ECC71", deleteText: "#FF6762", + warningText: nativeColor(appTheme["--color-warning-foreground"]), + warningBackground: nativeColor(appTheme["--color-warning"]), }; } @@ -217,6 +246,8 @@ export function createNativeReviewDiffTheme( deleteBar: terminalRed ?? "#ff2e3f", addText: "#199F43", deleteText: "#D52C36", + warningText: nativeColor(appTheme["--color-warning-foreground"]), + warningBackground: nativeColor(appTheme["--color-warning"]), }; } @@ -244,7 +275,15 @@ function createNoticeRow(fileId: string, suffix: string, text: string): NativeRe } function noticeRowsForFile(file: ReviewRenderableFile): ReadonlyArray { - if (file.notice) return [createNoticeRow(file.id, "loading", file.notice)]; + if (file.notice) { + return [ + { + ...createNoticeRow(file.id, "loading", file.notice), + ...(file.noticeTone === undefined ? {} : { tone: file.noticeTone }), + ...(file.noticeActionable ? { actionable: true } : {}), + }, + ]; + } if (file.rows.length > 0) { return []; } @@ -362,6 +401,7 @@ function mapLineRow( row: ReviewRenderableLineRow, rowIndex: number, ): NativeReviewDiffRow { + const noteStart = cutLineNoteStart(row.content); return { kind: "line", id: `${file.id}:line:${rowIndex}:${row.id}`, @@ -370,6 +410,7 @@ function mapLineRow( change: row.change, oldLineNumber: row.oldLineNumber, newLineNumber: row.newLineNumber, + ...(noteStart === null ? {} : { noteStart }), }; } diff --git a/apps/mobile/src/features/review/reviewModel.ts b/apps/mobile/src/features/review/reviewModel.ts index df3c7c51324b4..3a1028dbb1730 100644 --- a/apps/mobile/src/features/review/reviewModel.ts +++ b/apps/mobile/src/features/review/reviewModel.ts @@ -56,6 +56,11 @@ export interface ReviewRenderableFile { readonly id: string; readonly cacheKey: string; readonly notice?: string; + readonly noticeTone?: "warning"; + /** The notice row reports taps, as for loading a withheld file's diff. */ + readonly noticeActionable?: boolean; + /** The host left out this file's hunks as too large; it can be loaded on its own. */ + readonly withheld?: boolean; readonly path: string; readonly previousPath: string | null; readonly changeType: ChangeTypes; diff --git a/apps/mobile/src/features/review/useReviewDiffData.ts b/apps/mobile/src/features/review/useReviewDiffData.ts index d9ec656cbc773..8368c1ec63aa3 100644 --- a/apps/mobile/src/features/review/useReviewDiffData.ts +++ b/apps/mobile/src/features/review/useReviewDiffData.ts @@ -55,7 +55,7 @@ function getCachedReviewFile( ...(loaded ?? { path: stat.path, previousPath: stat.previousPath, - changeType: "change" as const, + changeType: stat.changeType ?? ("change" as const), languageHint: null, additionLines: [], deletionLines: [], @@ -142,13 +142,7 @@ export function useReviewDiffData(input: { }); const registry = useContext(RegistryContext); const { environmentId, cwd } = input; - const scope = JSON.stringify([ - environmentId, - cwd, - source?.kind, - source?.baseRef, - source?.diffHash, - ]); + const scope = JSON.stringify([environmentId, cwd, lazySource?.kind, source?.baseRef]); const [requested, setRequested] = useState({ scope, indices: [0, 1, 2] }); const indices = useMemo( () => new Set(requested.scope === scope ? requested.indices : [0, 1, 2]), diff --git a/apps/mobile/src/features/settings/SettingsClientStorageRouteScreen.tsx b/apps/mobile/src/features/settings/SettingsClientStorageRouteScreen.tsx index c0f93d2ee324d..1b34022d4cae5 100644 --- a/apps/mobile/src/features/settings/SettingsClientStorageRouteScreen.tsx +++ b/apps/mobile/src/features/settings/SettingsClientStorageRouteScreen.tsx @@ -72,10 +72,10 @@ export function SettingsClientStorageRouteScreen() { {AsyncResult.isFailure(summaryResult) ? ( diff --git a/apps/mobile/src/features/settings/SettingsScheduledTasksRouteScreen.test.tsx b/apps/mobile/src/features/settings/SettingsScheduledTasksRouteScreen.test.tsx new file mode 100644 index 0000000000000..b92c1ec725b47 --- /dev/null +++ b/apps/mobile/src/features/settings/SettingsScheduledTasksRouteScreen.test.tsx @@ -0,0 +1,264 @@ +import { RegistryContext } from "@effect/atom-react"; +import { + EnvironmentId, + ProjectId, + ProviderInstanceId, + ScheduledTaskId, + type ScheduledTask, +} from "@t3tools/contracts"; +import { Atom } from "effect/reactivity"; +import { act, type ReactNode } from "react"; +import { createRoot, type Root } from "react-dom/client"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; + +const { Children, mocks } = vi.hoisted(() => ({ + Children: ({ children }: { children?: ReactNode }) => children, + mocks: { upsert: vi.fn(), alert: vi.fn(), tasks: [] as ScheduledTask[] }, +})); +vi.mock("react-native", () => ({ + Platform: { OS: "ios" }, + View: Children, + TextInput: () => null, + Pressable: ({ children, onPress }: { children?: ReactNode; onPress: () => void }) => ( + + ), +})); +vi.mock("@react-navigation/native", () => ({ + useNavigation: () => ({ isFocused: () => false }), + usePreventRemove: () => {}, +})); +vi.mock("react-native-safe-area-context", () => ({ useSafeAreaInsets: () => ({ bottom: 0 }) })); +vi.mock("@expo/ui/community/datetime-picker", () => ({ DateTimePicker: () => null })); +vi.mock("../../components/ui/Dialog", () => ({ alert: mocks.alert })); +vi.mock("../../components/ui/AppText", () => ({ AppText: Children })); +vi.mock("../../components/ui/AppSymbol", () => ({ SymbolView: () => null })); +vi.mock("../../components/EnvironmentMachineSymbol", () => ({ + EnvironmentMachineSymbol: () => null, + ENVIRONMENT_MACHINE_SYMBOLS: {}, +})); +vi.mock("../../components/ui/Menu", () => ({ + Menu: ({ + actions, + onPressAction, + }: { + actions: readonly { id: string; title: string }[]; + onPressAction: (event: { nativeEvent: { event: string } }) => void; + }) => + actions.map(({ id, title }) => ( + + )), +})); +vi.mock("../../components/ui/ScreenScrollView", () => ({ ScreenScrollView: Children })); +vi.mock("./components/SettingsScreen", () => ({ SettingsScreen: Children })); +vi.mock("./components/SettingsSection", () => ({ SettingsSection: Children })); +vi.mock("./components/SettingsEnvironmentFilterHeader", () => ({ + useSettingsEnvironmentMenu: () => null, +})); +vi.mock("./components/ScheduledTaskPromptField", () => ({ ScheduledTaskPromptField: () => null })); +vi.mock("../layout/workspace/useSelectWorkspaceDetail", () => ({ + useSelectWorkspaceDetail: () => vi.fn(), +})); +vi.mock("../../state/threads", () => ({ threadFamilies: {} })); +vi.mock("../../lib/copyTextWithHaptic", () => ({ tryCopyTextWithHaptic: vi.fn() })); +vi.mock("../voice-input/useVoiceInputController", () => ({ + useVoiceInputController: () => ({ blocksSubmission: false }), +})); +vi.mock("../../components/ui/Button", () => ({ + Button: ({ label, onPress }: { label: string; onPress: () => void }) => ( + + ), +})); +vi.mock("../../components/ui/SegmentedControl", () => ({ + SegmentedControl: ({ + options, + onSelect, + }: { + options: readonly { value: string; label: string }[]; + onSelect: (value: string) => void; + }) => + options.map(({ value, label }) => ( + + )), +})); +vi.mock("../../components/ui/ThemedSwitch", () => ({ + ThemedSwitch: ({ + value, + onValueChange, + }: { + value: boolean; + onValueChange: (value: boolean) => void; + }) => , +})); +vi.mock("../../state/entities", () => ({ + useProjects: () => [ + { environmentId: "environment", id: "project", title: "Project" }, + { environmentId: "target", id: "target-project", title: "Target project" }, + ], + useServerConfigs: () => new Map(), + useEnvironmentServerConfig: () => null, + useThreadShell: () => null, +})); +vi.mock("./settings-environment-filter", () => ({ + useSettingsEnvironmentFilter: () => ({ + selectedTargets: [{ environmentId: "environment", label: "Environment" }], + availableTargets: [ + { environmentId: "environment", label: "Environment" }, + { environmentId: "target", label: "Target" }, + ], + selectedProjectKey: null, + projectGroups: [], + }), +})); +vi.mock("../../lib/modelOptions", () => ({ + buildModelOptions: () => [], + resolveDefaultableModelSelection: () => null, + resolveNewTaskModelSelection: () => null, +})); +vi.mock("../../state/session", () => ({ + readEnvironmentScope: () => true, + usePreparedConnection: () => ({ _tag: "None" }), +})); +vi.mock("../../state/server", () => ({ + serverEnvironment: { + scheduledTasksLive: () => ({}), + upsertScheduledTask: { permissionAtom: () => Atom.make(true) }, + }, +})); +vi.mock("../../state/query", () => ({ + useEnvironmentQuery: () => ({ data: { tasks: mocks.tasks, threads: [] } }), +})); +vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: () => mocks.upsert })); + +import { appAtomRegistry } from "../../state/atom-registry"; +import { editDraft } from "./scheduledTaskDraft"; +import { + scheduledTaskEditorSessionAtom, + startScheduledTaskEditor, +} from "./scheduled-task-editor-state"; +import { SettingsScheduledTaskEditRouteScreen } from "./SettingsScheduledTasksRouteScreen"; + +let root: Root; +let container: HTMLDivElement; +beforeEach(() => { + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; + container = document.createElement("div"); + root = createRoot(container); + mocks.alert.mockReset(); + mocks.upsert.mockReset(); + mocks.upsert.mockResolvedValue({ _tag: "Success" }); + const task: ScheduledTask = { + id: ScheduledTaskId.make("task"), + title: "Task", + prompt: "Prompt", + enabled: true, + schedule: { type: "fixed_time", timeOfDay: "05:00" }, + projectId: ProjectId.make("project"), + threadId: null, + workspaceStrategy: { type: "root" }, + modelSelection: { + instanceId: ProviderInstanceId.make("codex"), + model: "model", + }, + runtimeMode: "full-access", + interactionMode: "default", + createdBy: "user", + creationSource: "mobile", + createdAt: "2026-10-22T00:00:00.000Z", + updatedAt: "2026-10-22T00:00:00.000Z", + nextRunAt: null, + lastRunAt: null, + lastRunStatus: "never", + lastRunError: null, + runCount: 0, + }; + mocks.tasks = [task]; + const draft = editDraft(task); + startScheduledTaskEditor({ + environmentId: EnvironmentId.make("environment"), + environmentLabel: "Environment", + draft: { ...draft, schedule: { ...draft.schedule, intervalMinutes: "60" } }, + }); + act(() => + root.render( + + + , + ), + ); +}); +afterEach(() => { + act(() => root.unmount()); + appAtomRegistry.set(scheduledTaskEditorSessionAtom, null); +}); +function button(label: string) { + const element = Array.from(container.querySelectorAll("button")).find( + (button) => button.textContent === label, + ); + if (!element) throw new Error(`Missing button: ${label}`); + return element; +} +describe("scheduled task batched edits", () => { + it("submits the selected interval when saved before React commits", async () => { + await act(async () => { + button("Interval").click(); + button("Save changes").click(); + }); + expect(mocks.alert).not.toHaveBeenCalled(); + expect(mocks.upsert).toHaveBeenCalledExactlyOnceWith({ + environmentId: "environment", + input: expect.objectContaining({ + id: "task", + requireExisting: true, + schedule: { type: "interval", everyMs: 3_600_000 }, + }), + }); + }); + it("retains the interval when enabled changes before React commits", async () => { + act(() => { + button("Interval").click(); + button("Toggle enabled").click(); + }); + expect(appAtomRegistry.get(scheduledTaskEditorSessionAtom)?.current?.draft.schedule.mode).toBe( + "interval", + ); + await act(async () => button("Save changes").click()); + expect(mocks.alert).not.toHaveBeenCalled(); + expect(mocks.upsert).toHaveBeenCalledExactlyOnceWith({ + environmentId: "environment", + input: expect.objectContaining({ + id: "task", + requireExisting: true, + enabled: false, + schedule: { type: "interval", everyMs: 3_600_000 }, + }), + }); + }); + it("keeps every webhook filter added before React commits", () => { + act(() => button("On webhook").click()); + act(() => { + button("Add filter").click(); + button("Add filter").click(); + }); + expect( + appAtomRegistry.get(scheduledTaskEditorSessionAtom)?.current?.draft.schedule.filters, + ).toHaveLength(2); + }); + it("keeps the target project when an old picker fires after changing environments", () => { + const editor = appAtomRegistry.get(scheduledTaskEditorSessionAtom)?.current; + if (!editor) throw new Error("Missing editor"); + act(() => startScheduledTaskEditor({ ...editor, draft: { ...editor.draft, task: null } })); + act(() => { + button("Select Target").click(); + button("Interval").click(); + button("Select Project").click(); + }); + expect(appAtomRegistry.get(scheduledTaskEditorSessionAtom)?.current).toMatchObject({ + environmentId: "target", + draft: { projectId: "target-project", schedule: { mode: "interval" } }, + }); + }); +}); diff --git a/apps/mobile/src/features/settings/SettingsScheduledTasksRouteScreen.tsx b/apps/mobile/src/features/settings/SettingsScheduledTasksRouteScreen.tsx index 3f552c1f0c46c..d71fbc8fd9d24 100644 --- a/apps/mobile/src/features/settings/SettingsScheduledTasksRouteScreen.tsx +++ b/apps/mobile/src/features/settings/SettingsScheduledTasksRouteScreen.tsx @@ -83,7 +83,11 @@ import { type ScheduledTaskDraft as Draft, } from "./scheduledTaskDraft"; import { settingsTargetsForProject } from "./settings-environment-filter.logic"; -import { useScheduledTaskDestinationLabel, useScheduledTaskEditor } from "./scheduled-task-editor"; +import { + useScheduledTaskDestinationLabel, + useScheduledTaskEditor, + useScheduledTaskThread, +} from "./scheduled-task-editor"; import { discardScheduledTaskEditor, markScheduledTaskEditorSaved, @@ -267,11 +271,13 @@ function SelectRow(props: { function WebhookFilterRows(props: { readonly filters: ReadonlyArray; readonly disabled: boolean; - readonly onChange: (filters: ReadonlyArray) => void; + readonly onChange: ( + update: (filters: ReadonlyArray) => ReadonlyArray, + ) => void; }) { - const update = (index: number, patch: Partial) => - props.onChange( - props.filters.map((filter, at) => (at === index ? { ...filter, ...patch } : filter)), + const update = (id: WebhookFilterDraft["id"], patch: Partial) => + props.onChange((filters) => + filters.map((filter) => (filter.id === id ? { ...filter, ...patch } : filter)), ); return ( <> @@ -284,7 +290,9 @@ function WebhookFilterRows(props: { accessibilityLabel={`Remove filter ${index + 1}`} disabled={props.disabled} hitSlop={8} - onPress={() => props.onChange(removeWebhookFilterDraft(props.filters, filter.id))} + onPress={() => + props.onChange((filters) => removeWebhookFilterDraft(filters, filter.id)) + } className="active:opacity-70" > @@ -295,7 +303,7 @@ function WebhookFilterRows(props: { value={filter.path} placeholder="body.action" disabled={props.disabled} - onChange={(path) => update(index, { path })} + onChange={(path) => update(filter.id, { path })} /> { const known = WEBHOOK_FILTER_OPS.find((option) => option.op === id); - if (known) update(index, { op: known.op }); + if (known) update(filter.id, { op: known.op }); }} /> {filter.op === "exists" ? null : ( @@ -316,7 +324,7 @@ function WebhookFilterRows(props: { value={filter.value} placeholder={filter.op === "in" ? "opened, reopened" : "opened"} disabled={props.disabled} - onChange={(value) => update(index, { value })} + onChange={(value) => update(filter.id, { value })} /> )} @@ -325,7 +333,13 @@ function WebhookFilterRows(props: { props.onChange([...props.filters, emptyWebhookFilterDraft()])} + onPress={() => + props.onChange((filters) => + filters.length < MAX_WEBHOOK_FILTERS + ? [...filters, emptyWebhookFilterDraft()] + : filters, + ) + } className="min-h-14 flex-row items-center gap-3 border-t border-border-subtle px-4 py-3 active:opacity-70" > @@ -590,8 +604,19 @@ function SettingsScheduledTaskEditorScreen({ title }: { readonly title: string } voiceInput={voiceInput} /> } - setDraft={(draft) => { - if (!saving) setEditor({ ...editor, draft }); + setDraft={(update) => { + if (!saving) + setEditor((current) => + current ? { ...current, draft: update(current.draft) } : current, + ); + }} + setEnvironmentDraft={(update) => { + if (!saving) + setEditor((current) => + current && current.environmentId === editor.environmentId + ? { ...current, draft: update(current.draft) } + : current, + ); }} onSaved={() => { saved.current = true; @@ -599,18 +624,21 @@ function SettingsScheduledTaskEditorScreen({ title }: { readonly title: string } if (navigation.isFocused()) navigation.goBack(); }} onChangeEnvironment={(target) => { - if (target.environmentId === editor.environmentId) return; - setEditor({ - environmentId: target.environmentId, - environmentLabel: target.label, - draft: { - ...draftForEnvironment(target.environmentId), - title: editor.draft.title, - prompt: editor.draft.prompt, - schedule: editor.draft.schedule, - enabled: editor.draft.enabled, - }, - }); + setEditor((current) => + !current || target.environmentId === current.environmentId + ? current + : { + environmentId: target.environmentId, + environmentLabel: target.label, + draft: { + ...draftForEnvironment(target.environmentId), + title: current.draft.title, + prompt: current.draft.prompt, + schedule: current.draft.schedule, + enabled: current.draft.enabled, + }, + }, + ); }} /> ) : ( @@ -635,6 +663,7 @@ function TaskForm({ dictationPending, promptField, setDraft, + setEnvironmentDraft, onSaved, onChangeEnvironment, }: { @@ -646,10 +675,12 @@ function TaskForm({ readonly setSaving: (saving: boolean) => void; readonly dictationPending: boolean; readonly promptField: ReactNode; - readonly setDraft: (draft: Draft) => void; + readonly setDraft: (update: (current: Draft) => Draft) => void; + readonly setEnvironmentDraft: (update: (current: Draft) => Draft) => void; readonly onSaved: () => void; readonly onChangeEnvironment: (target: SettingsTarget) => void; }) { + const { readEditor } = useScheduledTaskEditor(); const navigation = useNavigation>(); const tasks = useEnvironmentQuery( serverEnvironment.scheduledTasksLive({ environmentId, input: {} }), @@ -666,6 +697,10 @@ function TaskForm({ .sort((left, right) => Number(right.pinnedAt != null) - Number(left.pinnedAt != null)); const config = useEnvironmentServerConfig(environmentId); const destinationLabel = useScheduledTaskDestinationLabel(environmentId, draft.threadId); + const boundThread = useScheduledTaskThread(environmentId, draft.threadId); + // Bound runs use the thread's current model, so the locked row shows that one. + const shownSelection = + draft.threadId === null ? draft.modelSelection : (boundThread?.modelSelection ?? null); const modelOptions = buildModelOptions(config, null, undefined, draft.projectId); // Existing webhook tasks keep the option so they can still switch to a timer. const webhookDenial = managedCapabilityDenial(config?.managedPolicy, "webhookTasks"); @@ -699,6 +734,9 @@ function TaskForm({ draft.task?.schedule.type === "once"; const offersThreadModes = config?.environment.capabilities.scheduledTaskThreadModes === true; const save = async () => { + const current = readEditor(); + if (!current || current.environmentId !== environmentId) return; + const draft = current.draft; if ( !readEnvironmentScope(environmentId, AuthOrchestrationOperateScope) || submissionPending.current || @@ -853,7 +891,7 @@ function TaskForm({ disabled={saving} value={draft.title} placeholder="Check for issues" - onChange={(title) => setDraft({ ...draft, title })} + onChange={(title) => setDraft((current) => ({ ...current, title }))} /> {promptField} @@ -873,21 +911,23 @@ function TaskForm({ onSelect={(id) => { const project = projects.find((item) => item.id === id); if (!project) return; - const keepsExplicitModel = - draft.modelSelectionIsExplicit && - (draft.modelSelection === null || - !config?.settings || - resolveProjectProviderInstanceEnabled( - config.settings, - project.id, - draft.modelSelection.instanceId, - )); - setDraft({ - ...changeDraftProject(draft, project.id), - modelSelection: keepsExplicitModel - ? draft.modelSelection - : scheduledTaskDefaultModel(config, project), - modelSelectionIsExplicit: keepsExplicitModel, + setEnvironmentDraft((current) => { + const keepsExplicitModel = + current.modelSelectionIsExplicit && + (current.modelSelection === null || + !config?.settings || + resolveProjectProviderInstanceEnabled( + config.settings, + project.id, + current.modelSelection.instanceId, + )); + return { + ...changeDraftProject(current, project.id), + modelSelection: keepsExplicitModel + ? current.modelSelection + : scheduledTaskDefaultModel(config, project), + modelSelectionIsExplicit: keepsExplicitModel, + }; }); }} /> @@ -897,14 +937,17 @@ function TaskForm({ value={ modelOptions.find( (option) => - option.selection.instanceId === draft.modelSelection?.instanceId && - option.selection.model === draft.modelSelection?.model, + option.selection.instanceId === shownSelection?.instanceId && + option.selection.model === shownSelection?.model, )?.label ?? - draft.modelSelection?.model ?? + shownSelection?.model ?? (modelOptions.length ? "Choose model" : "No models available") } onPress={() => navigation.navigate("SettingsScheduledTaskModel")} - disabled={saving || dictationPending || environmentUnavailable} + // The saved selection only seeds new threads; bound runs ignore it. + disabled={ + saving || dictationPending || environmentUnavailable || draft.threadId !== null + } /> @@ -935,9 +978,9 @@ function TaskForm({ )} onSelect={(id) => { if (id === "new_thread") - setDraft({ ...unbindDraftFromThread(draft), threadMode: id }); + setDraft((current) => ({ ...unbindDraftFromThread(current), threadMode: id })); else if (id === "continue" || id === "fresh_session") - setDraft({ ...draft, threadMode: id }); + setDraft((current) => ({ ...current, threadMode: id })); }} /> ) : null} @@ -985,7 +1028,7 @@ function TaskForm({ ]} onSelect={(id) => { if (id === "worktree" || id === "root" || id === "existing_worktree") - setDraft({ ...draft, workspace: id }); + setDraft((current) => ({ ...current, workspace: id })); }} /> {draft.workspace === "worktree" ? ( @@ -1008,10 +1051,10 @@ function TaskForm({ value={draft.checkoutPath} borderTop onChange={(checkoutPath) => - setDraft({ - ...draft, - checkoutPath: resolvePastedPath(draft.checkoutPath, checkoutPath), - }) + setEnvironmentDraft((current) => ({ + ...current, + checkoutPath: resolvePastedPath(current.checkoutPath, checkoutPath), + })) } /> ) : null} @@ -1036,21 +1079,21 @@ function TaskForm({ onSelect={(mode) => { setTimePickerOpen(false); setRunAtPicker(null); - setDraft({ - ...draft, + setDraft((current) => ({ + ...current, prompt: - mode === "webhook" && !draft.prompt.trim() + mode === "webhook" && !current.prompt.trim() ? DEFAULT_WEBHOOK_PROMPT - : draft.prompt, + : current.prompt, schedule: { - ...draft.schedule, + ...current.schedule, mode, runAt: - mode === "once" && !draft.schedule.runAt + mode === "once" && !current.schedule.runAt ? new Date(Math.ceil(Date.now() / 3_600_000) * 3_600_000).toISOString() - : draft.schedule.runAt, + : current.schedule.runAt, }, - }); + })); }} /> @@ -1089,7 +1132,10 @@ function TaskForm({ onDismiss={() => setTimePickerOpen(false)} onValueChange={(_, selected) => { const timeOfDay = `${String(selected.getHours()).padStart(2, "0")}:${String(selected.getMinutes()).padStart(2, "0")}`; - setDraft({ ...draft, schedule: { ...draft.schedule, timeOfDay } }); + setDraft((current) => ({ + ...current, + schedule: { ...current.schedule, timeOfDay }, + })); }} /> ) : null} @@ -1118,19 +1164,21 @@ function TaskForm({ })), ]} onSelect={(id) => { - const weekdays = - id === "every_day" - ? DAYS.map((day) => day.index) - : id === "weekdays" - ? [1, 2, 3, 4, 5] - : (() => { - const day = DAYS.find((item) => String(item.index) === id); - if (!day) return draft.schedule.weekdays; - return draft.schedule.weekdays.includes(day.index) - ? draft.schedule.weekdays.filter((index) => index !== day.index) - : [...draft.schedule.weekdays, day.index]; - })(); - setDraft({ ...draft, schedule: { ...draft.schedule, weekdays } }); + setDraft((current) => { + const weekdays = + id === "every_day" + ? DAYS.map((day) => day.index) + : id === "weekdays" + ? [1, 2, 3, 4, 5] + : (() => { + const day = DAYS.find((item) => String(item.index) === id); + if (!day) return current.schedule.weekdays; + return current.schedule.weekdays.includes(day.index) + ? current.schedule.weekdays.filter((index) => index !== day.index) + : [...current.schedule.weekdays, day.index]; + })(); + return { ...current, schedule: { ...current.schedule, weekdays } }; + }); }} /> @@ -1175,10 +1223,10 @@ function TaskForm({ onValueChange={(_, selected) => { // Android shows a dialog that closes on pick; iOS stays inline. if (Platform.OS === "android") setRunAtPicker(null); - setDraft({ - ...draft, - schedule: { ...draft.schedule, runAt: selected.toISOString() }, - }); + setDraft((current) => ({ + ...current, + schedule: { ...current.schedule, runAt: selected.toISOString() }, + })); }} /> ) : null} @@ -1206,14 +1254,20 @@ function TaskForm({ disabled={saving} borderTop onChange={(maxDeliveryAgeMinutes) => - setDraft({ ...draft, schedule: { ...draft.schedule, maxDeliveryAgeMinutes } }) + setDraft((current) => ({ + ...current, + schedule: { ...current.schedule, maxDeliveryAgeMinutes }, + })) } /> - setDraft({ ...draft, schedule: { ...draft.schedule, filters } }) + onChange={(update) => + setDraft((current) => ({ + ...current, + schedule: { ...current.schedule, filters: update(current.schedule.filters) }, + })) } /> - setDraft({ ...draft, schedule: { ...draft.schedule, replayKey } }) + setDraft((current) => ({ + ...current, + schedule: { ...current.schedule, replayKey }, + })) } /> @@ -1236,7 +1293,10 @@ function TaskForm({ disabled={saving} borderTop onChange={(coalesceKey) => - setDraft({ ...draft, schedule: { ...draft.schedule, coalesceKey } }) + setDraft((current) => ({ + ...current, + schedule: { ...current.schedule, coalesceKey }, + })) } /> - setDraft({ ...draft, schedule: { ...draft.schedule, coalesceWindowSeconds } }) + setDraft((current) => ({ + ...current, + schedule: { ...current.schedule, coalesceWindowSeconds }, + })) } /> @@ -1264,7 +1327,10 @@ function TaskForm({ disabled={saving} borderTop onChange={(intervalMinutes) => - setDraft({ ...draft, schedule: { ...draft.schedule, intervalMinutes } }) + setDraft((current) => ({ + ...current, + schedule: { ...current.schedule, intervalMinutes }, + })) } /> {!(parseIntervalMinutes(draft.schedule.intervalMinutes) >= 1) ? ( @@ -1287,7 +1353,7 @@ function TaskForm({ setDraft({ ...draft, enabled })} + onValueChange={(enabled) => setDraft((current) => ({ ...current, enabled }))} /> diff --git a/apps/mobile/src/features/settings/appearance/useScaledTextRole.ts b/apps/mobile/src/features/settings/appearance/useScaledTextRole.ts index ffeb6114c17a5..c71298a89822a 100644 --- a/apps/mobile/src/features/settings/appearance/useScaledTextRole.ts +++ b/apps/mobile/src/features/settings/appearance/useScaledTextRole.ts @@ -1,4 +1,6 @@ -import { resolveScaledTextRole } from "../../../lib/appearancePreferences"; +import { Platform, useWindowDimensions } from "react-native"; + +import { resolveComposerTextRole, resolveScaledTextRole } from "../../../lib/appearancePreferences"; import { MOBILE_TYPOGRAPHY } from "../../../lib/typography"; import { useAppearancePreferences } from "./AppearancePreferencesProvider"; @@ -12,3 +14,9 @@ export function useScaledTextRole(role: keyof typeof MOBILE_TYPOGRAPHY): ScaledT const { appearance } = useAppearancePreferences(); return resolveScaledTextRole(role, appearance.baseFontSize); } + +export function useComposerTextRole(): ScaledTextRole { + const body = useScaledTextRole("body"); + const { fontScale } = useWindowDimensions(); + return resolveComposerTextRole(body, fontScale, Platform.OS); +} diff --git a/apps/mobile/src/features/settings/scheduled-task-editor.tsx b/apps/mobile/src/features/settings/scheduled-task-editor.tsx index e99600ae533b8..d79ef40896e87 100644 --- a/apps/mobile/src/features/settings/scheduled-task-editor.tsx +++ b/apps/mobile/src/features/settings/scheduled-task-editor.tsx @@ -21,22 +21,29 @@ import { export const NEW_THREAD_DESTINATION_LABEL = "New thread each run"; -export function useScheduledTaskDestinationLabel( +export function useScheduledTaskThread( environmentId: EnvironmentId | null, threadId: ThreadId | null, -): string { +) { const ref = environmentId === null || threadId === null ? null : scopeThreadRef(environmentId, threadId); const shell = useThreadShell(ref); - // The shell can omit older settled threads; their family read still carries the title. + // The shell can omit older settled threads; their family read still carries the row. const family = useEnvironmentQuery( ref !== null && shell === null ? threadFamilies.familyAtom(ref.environmentId, ref.threadId) : null, ); - const thread = shell ?? family.data?.threads.find((row) => row.id === threadId); - if (ref === null) return NEW_THREAD_DESTINATION_LABEL; - return thread?.title.trim() || `Thread ${ref.threadId}`; + return shell ?? family.data?.threads.find((row) => row.id === threadId) ?? null; +} + +export function useScheduledTaskDestinationLabel( + environmentId: EnvironmentId | null, + threadId: ThreadId | null, +): string { + const thread = useScheduledTaskThread(environmentId, threadId); + if (environmentId === null || threadId === null) return NEW_THREAD_DESTINATION_LABEL; + return thread?.title.trim() || `Thread ${threadId}`; } /** The form draft lives in an atom, so it survives while its native picker routes are on top. */ diff --git a/apps/mobile/src/features/settings/scheduledTaskPresentation.test.ts b/apps/mobile/src/features/settings/scheduledTaskPresentation.test.ts new file mode 100644 index 0000000000000..02ca0a2d11c78 --- /dev/null +++ b/apps/mobile/src/features/settings/scheduledTaskPresentation.test.ts @@ -0,0 +1,13 @@ +import { describe, expect, it } from "vite-plus/test"; + +import { formatNextScheduledTaskRun } from "./scheduledTaskPresentation"; + +describe("formatNextScheduledTaskRun", () => { + it("names the rounded unit for a run later today", () => { + const now = new Date(2026, 10, 3, 10, 0, 0).getTime(); + const at = (minutes: number) => new Date(now + minutes * 60_000).toISOString(); + expect(formatNextScheduledTaskRun(at(59), now)).toBe("Next run in 59 minutes"); + expect(formatNextScheduledTaskRun(at(59.5), now)).toBe("Next run in 1 hour"); + expect(formatNextScheduledTaskRun(at(150), now)).toBe("Next run in 3 hours"); + }); +}); diff --git a/apps/mobile/src/features/settings/scheduledTaskPresentation.ts b/apps/mobile/src/features/settings/scheduledTaskPresentation.ts index 0fbc2626f1fbb..7ebdcfc2d3b3c 100644 --- a/apps/mobile/src/features/settings/scheduledTaskPresentation.ts +++ b/apps/mobile/src/features/settings/scheduledTaskPresentation.ts @@ -38,8 +38,9 @@ export function formatNextScheduledTaskRun(nextRunAt: string, now: number): stri const days = localCalendarDay(next) - localCalendarDay(current); if (days === 0) { if (remaining < MINUTE) return "Next run in less than a minute"; - const unit = remaining < HOUR ? "minute" : "hour"; - const count = Math.round(remaining / (remaining < HOUR ? MINUTE : HOUR)); + const minutes = Math.round(remaining / MINUTE); + const unit = minutes < 60 ? "minute" : "hour"; + const count = minutes < 60 ? minutes : Math.round(remaining / HOUR); return `Next run in ${count} ${unit}${count === 1 ? "" : "s"}`; } diff --git a/apps/mobile/src/features/sharing/incoming-share-model.test.ts b/apps/mobile/src/features/sharing/incoming-share-model.test.ts index 3e798975d743c..1dea547de53b4 100644 --- a/apps/mobile/src/features/sharing/incoming-share-model.test.ts +++ b/apps/mobile/src/features/sharing/incoming-share-model.test.ts @@ -54,7 +54,7 @@ describe("incoming native shares", () => { text: "Please explain this error\n\nhttps://example.com/issue/1", attachments: [ { - id: "share-1:image:3", + id: "share-1-image-3", type: "image", name: "Screenshot.png", mimeType: "image/png", @@ -131,7 +131,7 @@ describe("incoming native shares", () => { expect(result.attachments).toEqual([ { - id: "share-report:file:0", + id: "share-report-file-0", type: "file", name: "report.pdf", mimeType: "application/pdf", @@ -165,7 +165,7 @@ describe("incoming native shares", () => { expect(result.warnings).toEqual([]); expect(result.attachments).toEqual([ { - id: "share-video:file:0", + id: "share-video-file-0", type: "file", name: video.originalName, mimeType: video.mimeType, @@ -239,7 +239,7 @@ describe("incoming native shares", () => { expect(result.attachments).toEqual([ { - id: "share-android-report:file:0", + id: "share-android-report-file-0", type: "file", name: "report", mimeType: "application/pdf", diff --git a/apps/mobile/src/features/sharing/incoming-share-model.ts b/apps/mobile/src/features/sharing/incoming-share-model.ts index aa097713923b6..aa97294e15466 100644 --- a/apps/mobile/src/features/sharing/incoming-share-model.ts +++ b/apps/mobile/src/features/sharing/incoming-share-model.ts @@ -332,7 +332,7 @@ export async function buildIncomingShareDraft(input: { } } attachments.push({ - id: `${input.id}:file:${index}`, + id: `${input.id}-file-${index}`, type: "file", name, mimeType, @@ -397,7 +397,7 @@ export async function buildIncomingShareDraft(input: { } const dataUrl = `data:${mimeType};base64,${base64}`; attachments.push({ - id: `${input.id}:image:${index}`, + id: `${input.id}-image-${index}`, type: "image", name: resolved?.originalName ?? fallbackName(uri, index, mimeType), mimeType, diff --git a/apps/mobile/src/features/shortcuts/appShortcutsRuntime.test.ts b/apps/mobile/src/features/shortcuts/appShortcutsRuntime.test.ts index c985506480b76..961c0f42fa538 100644 --- a/apps/mobile/src/features/shortcuts/appShortcutsRuntime.test.ts +++ b/apps/mobile/src/features/shortcuts/appShortcutsRuntime.test.ts @@ -2,6 +2,7 @@ import { describe, expect, it, vi } from "vite-plus/test"; vi.mock("expo-quick-actions", () => ({})); vi.mock("react-native", () => ({ Platform: { OS: "android" } })); +vi.mock("../../connection/catalog", () => ({})); vi.mock("../../persistence/imperative", () => ({})); vi.mock("../../state/atom-registry", () => ({})); vi.mock("../../state/threads", () => ({ environmentThreadShells: {} })); @@ -54,6 +55,23 @@ describe("createRecentThreadShortcuts", () => { expect(save).toHaveBeenLastCalledWith([thread("a")]); }); + it("drops threads of environments that are no longer saved", async () => { + const gone = { environmentId: "gone", threadId: "b", title: "b" }; + const kept = { environmentId: "kept", threadId: "c", title: "c" }; + const { save, publish, recents } = setup(() => Promise.resolve([thread("a"), gone, kept])); + recents.retainEnvironments(new Set(["env", "kept"])); + await recents.loaded; + expect(publish).toHaveBeenLastCalledWith([thread("a"), kept]); + + recents.retainEnvironments(new Set(["kept"])); + await recents.saved(); + expect(publish).toHaveBeenLastCalledWith([kept]); + expect(save).toHaveBeenLastCalledWith([kept]); + + recents.recordActiveThread(thread("a")); + expect(publish).toHaveBeenLastCalledWith([kept]); + }); + it("records title changes of the open thread and ignores repeats", async () => { const { publish, recents } = setup(() => Promise.resolve([])); await recents.loaded; diff --git a/apps/mobile/src/features/shortcuts/appShortcutsRuntime.ts b/apps/mobile/src/features/shortcuts/appShortcutsRuntime.ts index b182c33b07866..a8c3d7b964776 100644 --- a/apps/mobile/src/features/shortcuts/appShortcutsRuntime.ts +++ b/apps/mobile/src/features/shortcuts/appShortcutsRuntime.ts @@ -4,6 +4,7 @@ import type { NavigationState } from "@react-navigation/native"; import type { ScopedThreadRef } from "@t3tools/contracts"; import { Atom } from "effect/reactivity"; +import { environmentCatalog } from "../../connection/catalog"; import { loadRecentThreadShortcuts, saveRecentThreadShortcuts, @@ -34,6 +35,7 @@ export function createRecentThreadShortcuts(dependencies: RecentThreadShortcutsD let recents: ReadonlyArray | null = null; let persistable = false; let active: RecentThreadShortcut | null = null; + let savedEnvironmentIds: ReadonlySet | null = null; // Serialize saves so older lists cannot overwrite newer ones. let saveQueue: Promise = Promise.resolve(); @@ -53,8 +55,11 @@ export function createRecentThreadShortcuts(dependencies: RecentThreadShortcutsD }); }; + const isSaved = (thread: RecentThreadShortcut) => + savedEnvironmentIds?.has(thread.environmentId) ?? true; + const withActive = (list: ReadonlyArray) => { - if (active === null) return list; + if (active === null || !isSaved(active)) return list; const next = withRecentThreadShortcut(list, active); if (next !== list) persistable = true; return next; @@ -63,7 +68,7 @@ export function createRecentThreadShortcuts(dependencies: RecentThreadShortcutsD const loaded = dependencies.load().then( (threads) => { persistable = true; - commit(withActive(threads)); + commit(withActive(threads.filter(isSaved))); }, (error: unknown) => { console.warn("[app-shortcuts] failed to load recent threads", error); @@ -81,6 +86,12 @@ export function createRecentThreadShortcuts(dependencies: RecentThreadShortcutsD const updated = withActive(recents); if (updated !== recents) commit(updated); }, + retainEnvironments(next: ReadonlySet): void { + savedEnvironmentIds = next; + if (recents === null) return; + const kept = recents.filter(isSaved); + if (kept.length !== recents.length) commit(kept); + }, }; } @@ -142,8 +153,16 @@ export function installAppShortcuts(navigate: (href: string) => void): () => voi (active) => recents.recordActiveThread(active), { immediate: true }, ); + const unsubscribeCatalog = appAtomRegistry.subscribe( + environmentCatalog.catalogValueAtom, + (catalog) => { + if (catalog.isReady) recents.retainEnvironments(new Set(catalog.entries.keys())); + }, + { immediate: true }, + ); return () => { subscription.remove(); unsubscribe(); + unsubscribeCatalog(); }; } diff --git a/apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx b/apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx index 4b84006352162..d66ff4192ecc5 100644 --- a/apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx +++ b/apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx @@ -4,6 +4,7 @@ import * as Dialog from "../../components/ui/Dialog"; import { AuthTerminalReadScope, AuthTerminalOperateScope, + chunkTerminalWrite, DEFAULT_TERMINAL_ID, EnvironmentId, ThreadId, @@ -42,7 +43,6 @@ import { LoadingScreen } from "../../components/LoadingScreen"; import { ScreenContent } from "../../components/ui/ScreenContent"; import { Button } from "../../components/ui/Button"; import { IconButton } from "../../components/ui/IconButton"; -import { environmentCatalog } from "../../connection/catalog"; import { useEnvironmentPresentation } from "../../state/presentation"; import { terminalEnvironment } from "../../state/terminal"; import { environmentSession, readEnvironmentScope } from "../../state/session"; @@ -66,6 +66,7 @@ import { import { useThreadSelection } from "../../state/use-thread-selection"; import { useSelectedThreadProjection } from "../../state/use-thread-detail"; import { EnvironmentConnectionNotice } from "../connection/EnvironmentConnectionNotice"; +import { useRetryEnvironment } from "../connection/useRetryEnvironment"; import { TerminalSurface, type TerminalSurfaceHandle } from "./NativeTerminalSurface"; import { getMobileTerminalTheme } from "./terminalTheme"; import { useTerminalExitNavigation } from "./useTerminalExitNavigation"; @@ -383,7 +384,7 @@ function ThreadTerminal(props: { const navigation = useNavigation(); const writeTerminal = useAtomCommand(terminalEnvironment.write, "terminal write"); const clearTerminal = useAtomCommand(terminalEnvironment.clear, "terminal clear"); - const retryEnvironment = useAtomCommand(environmentCatalog.retryNow, "environment retry"); + const retryEnvironment = useRetryEnvironment(); const selectedThreadDetail = useSelectedThreadProjection(); const selectedThreadDetailWorktreePath = selectedThreadDetail?.projection.thread.worktreePath ?? null; @@ -622,15 +623,21 @@ function ThreadTerminal(props: { return false; } - const result = await writeTerminal({ - environmentId: selectedThread.environmentId, - input: { - threadId: selectedThread.id, - terminalId, - data, - }, - }); - return result._tag === "Success"; + // Android's IME delivers a system paste as one input, which can exceed one write. + const isSameShell = pasteSession.liveness(); + for (const chunk of chunkTerminalWrite(data)) { + if (!isSameShell()) return false; + const result = await writeTerminal({ + environmentId: selectedThread.environmentId, + input: { + threadId: selectedThread.id, + terminalId, + data: chunk, + }, + }); + if (result._tag !== "Success") return false; + } + return true; }; const pasteText = async (readText: () => Promise) => { @@ -908,7 +915,7 @@ function ThreadTerminal(props: { title="No terminal sessions" detail="Existing terminals will appear here when another client opens one." /> - ) : !canOperateTerminal && terminal.error !== null ? ( + ) : terminal.error !== null && (!canOperateTerminal || terminal.status === "error") ? ( ) : ( <> diff --git a/apps/mobile/src/features/terminal/terminalInput.test.ts b/apps/mobile/src/features/terminal/terminalInput.test.ts index 17fabe588115c..6e5046dab35c4 100644 --- a/apps/mobile/src/features/terminal/terminalInput.test.ts +++ b/apps/mobile/src/features/terminal/terminalInput.test.ts @@ -1,11 +1,6 @@ import { describe, expect, it } from "vite-plus/test"; -import { - chunkTerminalWrite, - encodeTerminalPaste, - resolveModifiedTerminalInput, - TERMINAL_WRITE_MAX_LENGTH, -} from "./terminalInput"; +import { encodeTerminalPaste, resolveModifiedTerminalInput } from "./terminalInput"; const byte = (code: number) => String.fromCharCode(code); const ESC = byte(0x1b); @@ -78,23 +73,3 @@ describe("encodeTerminalPaste", () => { expect(encodeTerminalPaste(`safe${ESC}[201~; rm -rf /\n`)).toBe("safe [201~; rm -rf /\r"); }); }); - -describe("chunkTerminalWrite", () => { - it("splits oversized writes so every chunk fits the contract", () => { - const chunks = chunkTerminalWrite("y".repeat(TERMINAL_WRITE_MAX_LENGTH * 2 + 5)); - expect(chunks.map((chunk) => chunk.length)).toEqual([ - TERMINAL_WRITE_MAX_LENGTH, - TERMINAL_WRITE_MAX_LENGTH, - 5, - ]); - expect(chunks.join("")).toHaveLength(TERMINAL_WRITE_MAX_LENGTH * 2 + 5); - }); - - it("does not cut a surrogate pair in half at the boundary", () => { - const data = `${"z".repeat(TERMINAL_WRITE_MAX_LENGTH - 1)}😀tail`; - const chunks = chunkTerminalWrite(data); - expect(chunks[0]).toHaveLength(TERMINAL_WRITE_MAX_LENGTH - 1); - expect(chunks[1]).toBe("😀tail"); - expect(chunks.join("")).toBe(data); - }); -}); diff --git a/apps/mobile/src/features/terminal/terminalInput.ts b/apps/mobile/src/features/terminal/terminalInput.ts index 9c5e05dcc24f0..0f09d109e3634 100644 --- a/apps/mobile/src/features/terminal/terminalInput.ts +++ b/apps/mobile/src/features/terminal/terminalInput.ts @@ -3,9 +3,6 @@ import type { ExecutionEnvironmentPlatformOs } from "@t3tools/contracts"; export type PendingModifier = "ctrl" | "meta"; export type HostPlatform = "mac" | "linux" | "windows" | "unknown"; -/** Upper bound of `TerminalWriteInput.data`; longer writes are rejected by the server. */ -export const TERMINAL_WRITE_MAX_LENGTH = 65_536; - export type ModifiedTerminalInput = | { readonly kind: "write"; readonly data: string } | { readonly kind: "paste" }; @@ -64,21 +61,6 @@ export function encodeTerminalPaste(text: string): string { return text.replace(UNSAFE_PASTE_BYTES, " ").replace(/\r\n|\n/g, "\r"); } -export function chunkTerminalWrite(data: string): ReadonlyArray { - const chunks: string[] = []; - let start = 0; - while (start < data.length) { - let end = Math.min(start + TERMINAL_WRITE_MAX_LENGTH, data.length); - const last = data.charCodeAt(end - 1); - if (end < data.length && last >= 0xd800 && last <= 0xdbff) { - end -= 1; - } - chunks.push(data.slice(start, end)); - start = end; - } - return chunks; -} - /** Returns null for unknown OSes so callers can try weaker signals. */ export function hostPlatformFromOs(os: ExecutionEnvironmentPlatformOs | null): HostPlatform | null { if (os === "darwin") return "mac"; diff --git a/apps/mobile/src/features/terminal/terminalPaste.test.ts b/apps/mobile/src/features/terminal/terminalPaste.test.ts index ec9bd9d74d933..2545085137916 100644 --- a/apps/mobile/src/features/terminal/terminalPaste.test.ts +++ b/apps/mobile/src/features/terminal/terminalPaste.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from "vite-plus/test"; -import { TERMINAL_WRITE_MAX_LENGTH } from "./terminalInput"; +import { TERMINAL_WRITE_MAX_LENGTH } from "@t3tools/contracts"; import { createTerminalPasteSession } from "./terminalPaste"; function deferred() { @@ -34,6 +34,15 @@ describe("terminal paste session", () => { expect(writes).toEqual([]); }); + it("reports a liveness change after capture, including a restart in place", () => { + const session = createTerminalPasteSession(); + session.reset(true); + const isSameShell = session.liveness(); + expect(isSameShell()).toBe(true); + session.reset(true); + expect(isSameShell()).toBe(false); + }); + it("never overlaps writes from rapid paste requests", async () => { const session = createTerminalPasteSession(); session.reset(true); diff --git a/apps/mobile/src/features/terminal/terminalPaste.ts b/apps/mobile/src/features/terminal/terminalPaste.ts index d070bdc168826..b09e365587d57 100644 --- a/apps/mobile/src/features/terminal/terminalPaste.ts +++ b/apps/mobile/src/features/terminal/terminalPaste.ts @@ -1,4 +1,6 @@ -import { chunkTerminalWrite, encodeTerminalPaste } from "./terminalInput"; +import { chunkTerminalWrite } from "@t3tools/contracts"; + +import { encodeTerminalPaste } from "./terminalInput"; interface TerminalPasteInput { readonly readText: () => Promise; @@ -8,6 +10,8 @@ interface TerminalPasteInput { export interface TerminalPasteSession { readonly reset: (active: boolean) => void; + /** True until the terminal's liveness changes (stop, start or restart) after this call. */ + readonly liveness: () => () => boolean; readonly paste: (input: TerminalPasteInput) => Promise; } @@ -21,6 +25,11 @@ export function createTerminalPasteSession(): TerminalPasteSession { liveTarget = active ? {} : null; }, + liveness() { + const target = liveTarget; + return () => liveTarget === target; + }, + async paste({ readText, write, onReadError }) { const target = liveTarget; if (target === null) { diff --git a/apps/mobile/src/features/threads/McpAppWebView.tsx b/apps/mobile/src/features/threads/McpAppWebView.tsx index cf03775b7c7fc..203343eb5a680 100644 --- a/apps/mobile/src/features/threads/McpAppWebView.tsx +++ b/apps/mobile/src/features/threads/McpAppWebView.tsx @@ -264,6 +264,8 @@ export function ThreadMcpApp(props: { ); }, [host, detailAtom]); const userActive = useRef(false); + // Teardown completions check this so a replaced document's late callback is ignored. + const currentHost = useRef(null); // Each WebView document gets its own host: the view is keyed by document, // restart generation and URL, and a changed app re-attaches this callback. @@ -399,6 +401,8 @@ export function ThreadMcpApp(props: { } if (mode === "fullscreen" && current.displayMode !== "fullscreen") { await documentHost.teardown(); + // A crash restart or reload during the wait already replaced this document. + if (currentHost.current !== documentHost) return "inline"; // Approval arrival or navigation during the wait requires reloading inline. const navigation = latest.current.navigation; if (latest.current.awaitingUser || !navigation.isFocused()) { @@ -417,7 +421,7 @@ export function ThreadMcpApp(props: { } if (mode === "inline" && current.displayMode === "fullscreen") { await documentHost.teardown(); - current.onExitFullscreen?.(); + if (currentHost.current === documentHost) current.onExitFullscreen?.(); return "inline"; } return current.displayMode ?? "inline"; @@ -460,11 +464,11 @@ export function ThreadMcpApp(props: { }, onRequestTeardown: () => { const current = latest.current.props; - if (current.displayMode === "fullscreen") { - void documentHost.teardown().then(() => current.onExitFullscreen?.()); - } else { - void documentHost.teardown().then(() => setClosed(true)); - } + void documentHost.teardown().then(() => { + if (currentHost.current !== documentHost) return; + if (current.displayMode === "fullscreen") current.onExitFullscreen?.(); + else setClosed(true); + }); }, onSizeChanged: (size) => { if (latest.current.props.displayMode === "fullscreen") return; @@ -473,7 +477,9 @@ export function ThreadMcpApp(props: { }, }); setHost(documentHost); + currentHost.current = documentHost; return () => { + if (currentHost.current === documentHost) currentHost.current = null; // An unmount cannot wait: the request goes out before the view does. void documentHost.teardown(); setHost((current) => (current === documentHost ? null : current)); diff --git a/apps/mobile/src/features/threads/NewTaskRouteScreen.tsx b/apps/mobile/src/features/threads/NewTaskRouteScreen.tsx index ac9e2ce813636..8027e1572a073 100644 --- a/apps/mobile/src/features/threads/NewTaskRouteScreen.tsx +++ b/apps/mobile/src/features/threads/NewTaskRouteScreen.tsx @@ -523,7 +523,7 @@ export function NewTaskRouteScreen({ route }: StaticScreenProps } @@ -548,7 +548,7 @@ export function NewTaskRouteScreen({ route }: StaticScreenProps diff --git a/apps/mobile/src/features/threads/QuestionAttachments.tsx b/apps/mobile/src/features/threads/QuestionAttachments.tsx index b8fa753188608..3cc26c42fd7fc 100644 --- a/apps/mobile/src/features/threads/QuestionAttachments.tsx +++ b/apps/mobile/src/features/threads/QuestionAttachments.tsx @@ -105,10 +105,7 @@ export function QuestionAttachments(props: { props.question.id, ); changeQuestionAttachmentPreparation(key, 1); - void convertPastedImagesToAttachments({ - uris, - existingCount: appAtomRegistry.get(composerDraftsAtom)[key]?.attachments.length ?? 0, - }) + void convertPastedImagesToAttachments({ uris }) .then(async (result) => { if ( scope.active && diff --git a/apps/mobile/src/features/threads/SubagentRow.tsx b/apps/mobile/src/features/threads/SubagentRow.tsx index 7c71ac2680de1..96f6f659aa5a1 100644 --- a/apps/mobile/src/features/threads/SubagentRow.tsx +++ b/apps/mobile/src/features/threads/SubagentRow.tsx @@ -4,7 +4,10 @@ import { resolveProviderInstanceDisplayName, shouldShowInstanceBadge, } from "@t3tools/client-runtime/state/provider-instance-display"; -import { resolveSubagentMetadata } from "@t3tools/client-runtime/state/subagent-display"; +import { + resolveSubagentMetadata, + subagentCardDetail, +} from "@t3tools/client-runtime/state/subagent-display"; import { formatSubagentEffort } from "@t3tools/client-runtime/state/thread-execution"; import type { EnvironmentId, OrchestrationV2Subagent } from "@t3tools/contracts"; import type { ReactNode } from "react"; @@ -15,7 +18,6 @@ import { SymbolView } from "../../components/ui/AppSymbol"; import { ProviderIcon, ProviderInstanceIcon } from "../../components/ProviderIcon"; import { cn } from "../../lib/cn"; import { useEnvironmentServerConfig, useProject, useThreadShell } from "../../state/entities"; -import { subagentCardDetail } from "./subagent-card-presentation"; import { SUBAGENT_TONE_TEXT_CLASS, SubagentStatusDot } from "./SubagentStatusDot"; import { resolveSubagentRowPresentation } from "./threadAgentsPresentation"; diff --git a/apps/mobile/src/features/threads/ThreadComposer.tsx b/apps/mobile/src/features/threads/ThreadComposer.tsx index 3b1b927ef8a9b..ce4e4b7fb0e4b 100644 --- a/apps/mobile/src/features/threads/ThreadComposer.tsx +++ b/apps/mobile/src/features/threads/ThreadComposer.tsx @@ -15,6 +15,7 @@ import { useAtomValue } from "@effect/atom-react"; import { clampFileAttachmentUploadBytes } from "@t3tools/client-runtime/state/attachments"; import { pastedTextDisposition, replaceTextSelection } from "@t3tools/client-runtime/text-paste"; import { + AuthOrchestrationOperateScope, PROVIDER_SEND_TURN_MAX_ATTACHMENTS, PROVIDER_SEND_TURN_MAX_INPUT_CHARS, type ChatAttachment, @@ -29,6 +30,7 @@ import { hasProviderUsageLimits, isUsageLimitsCommand, } from "@t3tools/shared/usageLimits"; +import { parseSideChatCommand } from "@t3tools/shared/composerTrigger"; import { StackActions, useFocusEffect, useNavigation } from "@react-navigation/native"; import type { ReactNode } from "react"; import { useEffect, useEffectEvent, useRef, useState, type RefObject } from "react"; @@ -66,6 +68,7 @@ import type { ComposerDocumentAttachment } from "../../lib/composerContext"; import { useProject, useVisibleThreadShells } from "../../state/entities"; import { useMentionableEnvironments } from "../../state/environments"; import { useLinkedEnvironments } from "../../state/peerLinks"; +import { useEnvironmentScopeDenied } from "../../state/session"; import { scopeProjectRef } from "@t3tools/client-runtime/environment"; import { AppText as Text } from "../../components/ui/AppText"; @@ -78,7 +81,12 @@ import { VideoPreviewModal, type VideoPreviewSource } from "../../components/ui/ import { GlassSurface } from "../../components/ui/GlassSurface"; import { ComposerEditor, type ComposerEditorHandle } from "../../components/ComposerEditor"; import { fileRoutePathSegments } from "../files/filePath"; -import { ComposerActionButton, ComposerToolbarRow } from "../../components/ComposerToolbar"; +import { + ComposerActionButton, + ComposerInlineControl, + ComposerToolbarRow, +} from "../../components/ComposerToolbar"; +import { formatTokens } from "@t3tools/shared/usageFormat"; import { ComposerModelControl } from "../../components/ComposerModelControl"; import { composerStripAttachments, @@ -89,7 +97,7 @@ import { isModelSelectionUnavailable, serverConfigForCwd, } from "../../lib/modelOptions"; -import { useScaledTextRole, type ScaledTextRole } from "../settings/appearance/useScaledTextRole"; +import { useComposerTextRole, type ScaledTextRole } from "../settings/appearance/useScaledTextRole"; import type { RemoteClientConnectionState } from "../../lib/connection"; import { MOBILE_TYPOGRAPHY } from "../../lib/typography"; import { Menu } from "../../components/ui/Menu"; @@ -217,12 +225,20 @@ export interface ThreadComposerProps { readonly selectedThread: EnvironmentThreadShell; readonly reportedModelSelection?: ModelSelection | null; readonly hasCompactableConversation: boolean; + /** Set while the next send compacts the thread first. */ + readonly compactBeforeSend?: { + readonly tokens: number; + readonly keepFullHistory: boolean; + readonly toggleKeepFullHistory: () => void; + } | null; /** Arms a fresh session for the next message; absent where `/clear` is unavailable. */ readonly onFreshSession?: () => void; readonly offersSideChat?: boolean; readonly serverConfig: T3ServerConfig | null; readonly queueCount: number; readonly activeThreadBusy: boolean; + /** Sending declines a pending approval and redirects the agent, so `/side` is plain text. */ + readonly redirectsApproval: boolean; readonly canStopThread: boolean; readonly environmentId: EnvironmentId; readonly projectCwd: string | null; @@ -419,7 +435,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps const composerPanel = materialTheme["--color-composer-panel"]; const navigation = useNavigation(); const foregroundColor = useUniwindTheme()["--color-foreground"]; - const bodyText = useScaledTextRole("body"); + const bodyText = useComposerTextRole(); const fallbackInputRef = useRef(null); const inputRef = props.editorRef ?? fallbackInputRef; const [isFocused, setIsFocused] = useState(false); @@ -471,6 +487,10 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps setResumingQueue(false); }; + const operateDenied = useEnvironmentScopeDenied( + props.environmentId, + AuthOrchestrationOperateScope, + ); const uploadStates = useAtomValue(composerAttachmentUploadsAtom); const attachmentsUploading = props.connectionState === "connected" && @@ -482,7 +502,10 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps }); const sendPresentation = resolveComposerSendPresentation({ editingQueuedMessage: queuedEdit !== null, - running: props.activeThreadBusy, + // A side-chat command starts a side chat; it never queues or steers the running turn. + running: + props.activeThreadBusy && + (props.redirectsApproval || parseSideChatCommand(draftMessage) === null), canSteer: props.canSteerActiveTurn, followUpBehavior: props.followUpBehavior, deliveryDeferred: @@ -568,6 +591,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps offersUsageLimits: usageLimitsOffered, onUsageLimits: usageLimitsOffered && draftAttachments.length === 0 ? openUsageLimits : undefined, + onOpenModelPicker: settingsSheetPresentation.open, modelOptions: mentionModelOptions, modelProviders: props.serverConfig?.providers, ...(props.onFreshSession === undefined ? {} : { onFreshSession: props.onFreshSession }), @@ -813,6 +837,32 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps ); + const compactBeforeSend = + props.compactBeforeSend && !showResumeAction && queuedEdit === null + ? props.compactBeforeSend + : null; + const compactTokens = compactBeforeSend ? formatTokens(compactBeforeSend.tokens) : null; + const compactControl = + compactBeforeSend && compactTokens !== null ? ( + + ) : null; + return ( props.onHeightChange?.(event.nativeEvent.layout.height)} @@ -1093,6 +1143,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps compactEditor.isExpanded ? { flex: 1, justifyContent: "flex-end" } : undefined } > + {compactControl} {modelControl} - {props.connectionState === "connected" && !props.canOperateThread ? ( + {props.connectionState === "connected" && operateDenied ? ( This connection cannot control this task. You can still edit your draft. diff --git a/apps/mobile/src/features/threads/ThreadDetailScreen.tsx b/apps/mobile/src/features/threads/ThreadDetailScreen.tsx index f94efcabbcfb1..ad3f8cb083e95 100644 --- a/apps/mobile/src/features/threads/ThreadDetailScreen.tsx +++ b/apps/mobile/src/features/threads/ThreadDetailScreen.tsx @@ -33,7 +33,6 @@ import { queuedRunEditBlockedReason } from "@t3tools/client-runtime/queued-run-e import { childThreadInputsTitle, type CodexFeedbackSubmission, - type EnvironmentThreadStatus, } from "@t3tools/client-runtime/state/threads"; import { useKeyboardScrollToEnd } from "@legendapp/list/keyboard"; import { resolveProviderSkillsForCwd } from "@t3tools/client-runtime/providerSkills"; @@ -183,8 +182,6 @@ import { PendingApprovalCard } from "./PendingApprovalCard"; import { ComposerErrorNotice } from "./ComposerErrorNotice"; import { ManagedRuntimeModeNotice } from "./ManagedRuntimeModeNotice"; import { FreshSessionNotice } from "./FreshSessionNotice"; -import { Notice } from "../../components/ui/Notice"; -import { formatTokens } from "@t3tools/shared/usageFormat"; import { ComposerFeedback } from "./ComposerFeedback"; import { ComposerUsageLimits } from "./ComposerUsageLimits"; import { PendingUserInputCard } from "./PendingUserInputCard"; @@ -204,6 +201,7 @@ import { COMPOSER_COLLAPSED_CHROME, THREAD_COMPOSER_BOTTOM_GUTTER, ThreadComposer, + type ThreadComposerProps, } from "./ThreadComposer"; import { ComposerPopoverHost } from "./ComposerPopoverHost"; import { ThreadFeed, type ThreadFeedHistoryControls } from "./ThreadFeed"; @@ -254,11 +252,7 @@ export interface ThreadDetailScreenProps { readonly extensionStatuses?: NonNullable; readonly isCompacting: boolean; /** Set while the next send compacts the thread first. */ - readonly compactBeforeSend?: { - readonly tokens: number; - readonly keepFullHistory: boolean; - readonly toggleKeepFullHistory: () => void; - } | null; + readonly compactBeforeSend?: ThreadComposerProps["compactBeforeSend"]; readonly creationState: | { readonly kind: "preparing"; @@ -276,7 +270,6 @@ export interface ThreadDetailScreenProps { readonly respondingUserInputId: RuntimeRequestId | null; readonly connectionStateLabel: EnvironmentConnectionPhase; readonly connectionAwaitingApproval?: boolean; - readonly threadSyncStatus?: EnvironmentThreadStatus; readonly historyControls?: ThreadFeedHistoryControls; readonly activeThreadBusy: boolean; readonly canStopThread: boolean; @@ -575,20 +568,6 @@ function ThreadDetailBody( ); const composerKeyboardOffset = composerBottomInset; const contentPresentationKind = props.contentPresentation.kind; - const realThreadSyncLabel = (() => { - switch (props.threadSyncStatus) { - case "empty": - case "cached": - case "synchronizing": - if (contentPresentationKind === "ready") { - return "Syncing messages..."; - } - return contentPresentationKind === "loading" ? "Loading messages..." : null; - default: - return null; - } - })(); - const threadSyncLabel = useDelayedStatus(selectedThreadKey, realThreadSyncLabel); const connectionRetrying = props.connectionStateLabel === "connecting" || props.connectionStateLabel === "reconnecting"; const retryError = connectionRetrying ? props.connectionError : null; @@ -811,18 +790,13 @@ function ThreadDetailBody( return null; } if (props.creationState?.kind === "preparing") { - if (props.worktreeSetup) return null; - return { - kind: "preparing", - label: props.creationState.preparingWorktree ? "Setting up worktree…" : "Starting…", - }; + // The feed shows worktree setup in place, so only a plain start needs the pill. + if (props.worktreeSetup || props.creationState.preparingWorktree) return null; + return { kind: "preparing", label: "Starting…" }; } if (props.creationState?.kind === "failed") { return null; } - if (threadSyncLabel !== null) { - return { kind: "syncing", label: threadSyncLabel }; - } const childInput = childThreadInputs[0]; if (childInput && contentPresentationKind === "ready") { return { @@ -1507,6 +1481,11 @@ function ThreadDetailBody( pendingHandoff={props.pendingHandoff} worktreeSetup={props.worktreeSetup} setupWorkingStartedAt={props.setupWorkingStartedAt} + preparingWorktree={ + props.creationState?.kind === "preparing" && + props.creationState.preparingWorktree && + !props.worktreeSetup + } queuedMessages={props.queuedMessages} dispatchingMessageId={props.dispatchingMessageId} // A native subagent has no composer to edit a pending message in; @@ -1715,25 +1694,6 @@ function ThreadDetailBody( {freshSessionAvailable ? ( ) : null} - {props.compactBeforeSend ? ( - - - - ) : null} {props.canOperateThread ? ( 0 && + props.activePendingUserInput === null + } canStopThread={props.canStopThread} environmentId={props.environmentId} projectCwd={props.threadCwd ?? props.projectWorkspaceRoot} diff --git a/apps/mobile/src/features/threads/ThreadFeed.tsx b/apps/mobile/src/features/threads/ThreadFeed.tsx index 80292dc03fd25..3025861f143ab 100644 --- a/apps/mobile/src/features/threads/ThreadFeed.tsx +++ b/apps/mobile/src/features/threads/ThreadFeed.tsx @@ -27,6 +27,7 @@ import { LinkRequestCard } from "./LinkRequestCard"; import { SecretRequestCard } from "./SecretRequestCard"; import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator"; import { + WorktreePreparingHeader, WorktreeWorkingHeader, WorktreeSetupCard, type WorktreeSetupCardProps, @@ -97,6 +98,7 @@ import { CHAT_LIST_ANCHOR_OFFSET, resolveChatListAnchoredEndSpace } from "@t3too import { imageMimeType } from "@t3tools/shared/image"; import { videoMimeType } from "@t3tools/shared/video"; import { SymbolView, type AppSymbolName } from "../../components/ui/AppSymbol"; +import { textDirection } from "@t3tools/shared/textDirection"; import { useFocusEffect, useNavigation } from "@react-navigation/native"; import { useNativeWorkspaceColumnsSupported } from "../../native/NativeWorkspaceColumns"; import { useDeviceType } from "../../native/native-layout-metrics"; @@ -290,7 +292,7 @@ import { import { ZoomableImagePressable } from "./ZoomableImagePressable"; import { openSettings } from "../settings/settings-navigation"; import { useSelectWorkspaceDetail } from "../layout/workspace/useSelectWorkspaceDetail"; -import { formatMessageTimestamp } from "./messageTimestamp"; +import { useMessageTimeFormatter } from "./useMessageTimeFormatter"; const WIDE_MARKDOWN_BLOCK_OPTIONS = { // iOS blockquotes and selectable text measure separately; definite bubble width matches UIKit's drawing width. @@ -323,6 +325,7 @@ export interface ThreadFeedProps { readonly toolCallVisibility?: ToolCallVisibility; readonly worktreeSetup?: WorktreeSetupCardProps | null; readonly setupWorkingStartedAt?: string | null; + readonly preparingWorktree?: boolean; readonly queuedMessages: ReadonlyArray; readonly dispatchingMessageId: MessageId | null; readonly onEditPendingMessage: ((message: QueuedThreadMessage) => void) | null; @@ -1092,7 +1095,11 @@ interface MarkdownLinkHandlers { actionId: string, reply?: MarkdownLinkReply, ) => void; - readonly onImagePress?: (href: string) => void; + /** `withGallery` pages the image through its message's images; the feed's viewer sits outside that scope. */ + readonly onImagePress?: ( + href: string, + withGallery?: (source: FilePreviewSource) => FilePreviewSource, + ) => void; readonly resolveImageSource?: MarkdownImageSourceResolver; readonly fileEmbedLabel?: (href: string, standalone: boolean) => string | null; readonly linkCustomization?: (href: string) => MarkdownLinkCustomization | undefined; @@ -1127,8 +1134,12 @@ function ArtifactTemplateCard(props: { readonly template: CodexArtifactTemplate; readonly onUse?: ((template: CodexArtifactTemplate) => void) | undefined; }) { + const direction = textDirection(props.template.displayName); return ( - + onImagePress(href, withGallery) } : {}), onLinkPress: (href) => props.linkHandlers.onLinkPress(href, props.reply), onFileContextMenuAction: (href, actionId) => props.linkHandlers.onFileContextMenuAction(href, actionId, props.reply), @@ -1671,6 +1685,7 @@ function renderFeedEntry( | "threadId" | "workspaceRoot" > & { + readonly formatMessageTime: (input: string) => string; readonly copiedRowId: string | null; readonly expandedWorkRows: Record; readonly workRowSizing: ReturnType; @@ -1916,7 +1931,7 @@ function renderFeedEntry( const presentation = resolveUserMessagePresentation(message); const renderedText = renderAssistantCitationsAsText(presentation.text); const styles = isUser ? markdownStyles.user : markdownStyles.assistant; - const timestampLabel = formatMessageTimestamp(isUser ? message.createdAt : message.updatedAt); + const timestampLabel = props.formatMessageTime(isUser ? message.createdAt : message.updatedAt); const attachments = message.attachments ?? []; const hasReviewCommentContext = presentation.text.includes(" relativePath); }; - const onMarkdownLinkPress = (href: string, imageEmbed = false, reply?: MarkdownLinkReply) => { + const onMarkdownLinkPress = ( + href: string, + imageEmbed = false, + reply?: MarkdownLinkReply, + withGallery?: (source: FilePreviewSource) => FilePreviewSource, + ) => { markdownLinkRequest.current?.abort(); const request = new AbortController(); markdownLinkRequest.current = request; @@ -3047,7 +3068,9 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { if (request.signal.aborted) return; openPreview( scope, - media.kind === "video" ? { video: media.source } : { file: media.source }, + media.kind === "video" + ? { video: media.source } + : { file: withGallery ? withGallery(media.source) : media.source }, ); }; const resource = "resource" in media.source ? media.source.resource : null; @@ -3122,7 +3145,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { ), onLinkPress: (href, reply) => onMarkdownLinkPress(href, false, reply), linkCustomization: enrichedThreadLinkCustomization, - onImagePress: (href) => onMarkdownLinkPress(href, true), + onImagePress: (href, withGallery) => onMarkdownLinkPress(href, true, undefined, withGallery), resolveImageSource, fileContextMenu: (href) => { const skill = enrichedSkillReference(href, props.skills); @@ -3350,8 +3373,10 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { const listAppearanceData = { disclosureLayout, reduceMotion, + formatMessageTime, worktreeSetup: props.worktreeSetup, setupWorkingStartedAt: props.setupWorkingStartedAt, + preparingWorktree: props.preparingWorktree, dispatchingMessageId: props.dispatchingMessageId, unsettledTurnId, copiedRowId, @@ -3436,7 +3461,9 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { const groupedFeed = groupFeed(presentedFeed, { workflowOpeningMessageId: workflowOpening?.messageId, setupAnchorIndex: - props.worktreeSetup || props.setupWorkingStartedAt ? setupAnchorIndex : undefined, + props.worktreeSetup || props.setupWorkingStartedAt || props.preparingWorktree + ? setupAnchorIndex + : undefined, }); const { contentInsetEndAdjustment } = props; const attachList = (list: LegendListRef | null) => { @@ -3760,6 +3787,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { ) : null} {renderFeedEntry(info, { userMessageGrouping: info.item.userMessageGrouping, + formatMessageTime, environmentId: props.environmentId, dispatchingMessageId: props.dispatchingMessageId, onEditPendingMessage: props.onEditPendingMessage, @@ -3805,6 +3833,8 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { ) : props.setupWorkingStartedAt && info.index === setupAnchorIndex ? ( + ) : props.preparingWorktree && info.index === setupAnchorIndex ? ( + ) : null} @@ -3901,6 +3931,8 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) { {usesNativeAutomaticInsets ? null : } {setupAnchorIndex < 0 && props.worktreeSetup ? ( + ) : setupAnchorIndex < 0 && props.preparingWorktree ? ( + ) : null} {props.historyControls ? ( diff --git a/apps/mobile/src/features/threads/ThreadNavigationSidebar.tsx b/apps/mobile/src/features/threads/ThreadNavigationSidebar.tsx index 2302f2229aca3..c273446a67ebf 100644 --- a/apps/mobile/src/features/threads/ThreadNavigationSidebar.tsx +++ b/apps/mobile/src/features/threads/ThreadNavigationSidebar.tsx @@ -393,7 +393,6 @@ function ThreadNavigationSidebarPane(props: ThreadNavigationSidebarProps) { ) === props.selectedThreadKey) } project={projectByKey.get(pendingScopeKey) ?? null} - projectTitle={projectTitleByProjectKey.get(pendingScopeKey)} environmentLabel={ Object.keys(savedConnectionsById).length > 1 ? (savedConnectionsById[item.pendingTask.environmentId]?.environmentLabel ?? null) diff --git a/apps/mobile/src/features/threads/ThreadQueueControl.tsx b/apps/mobile/src/features/threads/ThreadQueueControl.tsx index d3b3ba6006291..c251283ef8839 100644 --- a/apps/mobile/src/features/threads/ThreadQueueControl.tsx +++ b/apps/mobile/src/features/threads/ThreadQueueControl.tsx @@ -25,6 +25,7 @@ import { useSafeAreaInsets } from "react-native-safe-area-context"; import { Button } from "../../components/ui/Button"; import * as Dialog from "../../components/ui/Dialog"; +import { FilePreviewModal, type FilePreviewSource } from "../../components/ui/FilePreviewModal"; import { SheetHeader } from "../../components/ui/SheetHeader"; import { AppText as Text } from "../../components/ui/AppText"; import { SymbolView } from "../../components/ui/AppSymbol"; @@ -143,6 +144,7 @@ export function ThreadQueueSheet({ route }: StaticScreenProps) { }); const [resuming, setResuming] = useState(false); const [busyRunId, setBusyRunId] = useState(null); + const [previewFile, setPreviewFile] = useState(null); const busyRef = useRef(false); const queuedRuns = workflow?.queuedRuns ?? []; @@ -321,6 +323,7 @@ export function ThreadQueueSheet({ route }: StaticScreenProps) { {message ? ( @@ -384,6 +387,7 @@ export function ThreadQueueSheet({ route }: StaticScreenProps) { navigation.goBack()} /> {content} + setPreviewFile(null)} /> ); } @@ -436,8 +440,10 @@ function QueueRowSwipeable(props: { function QueueAttachmentThumbnails(props: { readonly environmentId: EnvironmentId; readonly attachments: ReadonlyArray; + readonly onPressPreview: (source: FilePreviewSource) => void; }) { const images = props.attachments.filter((attachment) => attachment.mimeType.startsWith("image/")); + const gallery = images.map((attachment) => queueImagePreview(props.environmentId, attachment)); const shown = images.slice(0, THUMBNAIL_LIMIT); const overflow = props.attachments.length - shown.length; if (props.attachments.length === 0) return null; @@ -448,6 +454,12 @@ function QueueAttachmentThumbnails(props: { key={attachment.id} environmentId={props.environmentId} attachment={attachment} + onPress={() => + props.onPressPreview({ + ...queueImagePreview(props.environmentId, attachment), + gallery, + }) + } /> ))} {overflow > 0 ? ( @@ -461,28 +473,60 @@ function QueueAttachmentThumbnails(props: { ); } +function queueAttachmentResource(attachment: ChatAttachment) { + return { + _tag: "attachment", + attachmentId: attachment.id, + fileName: attachment.name, + mimeType: attachment.mimeType, + disposition: "inline", + } as const; +} + +function queueImagePreview(environmentId: EnvironmentId, attachment: ChatAttachment) { + const resource = queueAttachmentResource(attachment); + return { + kind: "image", + environmentId, + resource, + name: attachment.name, + mimeType: attachment.mimeType, + actionsSource: { + name: attachment.name, + mimeType: attachment.mimeType, + environmentId, + resource, + }, + } as const; +} + function QueueAttachmentThumbnail(props: { readonly environmentId: EnvironmentId; readonly attachment: ChatAttachment; + readonly onPress: () => void; }) { - const resource = { - _tag: "attachment", - attachmentId: props.attachment.id, - fileName: props.attachment.name, - mimeType: props.attachment.mimeType, - disposition: "inline", - } as const; + const resource = queueAttachmentResource(props.attachment); const url = useAssetUrl(props.environmentId, resource); - if (url === null) { - return ; - } return ( - + // Its own press target, so tapping an image previews it instead of editing the message. + + {url === null ? ( + + ) : ( + + )} + ); } diff --git a/apps/mobile/src/features/threads/ThreadRouteScreen.tsx b/apps/mobile/src/features/threads/ThreadRouteScreen.tsx index 0bed5a25f0271..b60991731e0d7 100644 --- a/apps/mobile/src/features/threads/ThreadRouteScreen.tsx +++ b/apps/mobile/src/features/threads/ThreadRouteScreen.tsx @@ -51,6 +51,7 @@ import { vcsEnvironment } from "../../state/vcs"; import { EmptyState } from "../../components/ui/EmptyState"; import { LoadingScreen } from "../../components/LoadingScreen"; import { scopedThreadKey } from "../../lib/scopedEntities"; +import { useDelayedStatus } from "../../lib/useDelayedStatus"; import { scopeThreadRef } from "@t3tools/client-runtime/environment"; import { useThreadShell } from "../../state/entities"; import { NATIVE_LIQUID_GLASS_SUPPORTED } from "../../native/native-glass"; @@ -95,7 +96,10 @@ import { resolveLatestMergeBackRun } from "@t3tools/client-runtime/state/thread- import { threadEnvironment } from "../../state/threads"; import { EMPTY_ENVIRONMENT_THREAD_STATE } from "@t3tools/client-runtime/state/threads"; import { useProjects } from "../../state/entities"; -import { projectThreadContentPresentation } from "./threadContentPresentation"; +import { + projectThreadContentPresentation, + threadSyncStatusLabel, +} from "./threadContentPresentation"; import { Inspector, useAdaptiveWorkspaceLayout, @@ -290,8 +294,11 @@ function LiveThreadRouteController(props: ThreadRouteScreenProps) { } if (selectedThread !== null && selectedThreadKey === routeThreadKey) { - return withInspectorHeader( - , + // ThreadRouteContent draws its own inspector header so it can show the sync status. + return ( + + + ); } @@ -859,7 +866,7 @@ function ThreadRouteContent( selectedThread?.worktreePath != null || (selectedThreadCreation?.message.creation?.workspaceMode === "worktree" && selectedThreadCreation.outcome == null), - turnStarted: setupTurnStartedAt !== null, + runs: selectedThreadDetail?.runs ?? [], followUpSent: composer.selectedThreadFeed.filter( (entry) => entry.type === "message" && entry.message.role === "user", @@ -944,14 +951,6 @@ function ThreadRouteContent( serverOwned: false, }; })(); - if (!environmentId || !threadId) { - return ; - } - - if (!selectedThread) { - return ; - } - // Queued creation displays its prompt before server creation; temporary not-found is expected. const contentPresentation = creationState !== null @@ -962,6 +961,27 @@ function ThreadRouteContent( detailDeleted: selectedThreadDetailState.status === "deleted", connectionState: routeConnectionState, }); + // Shown in the header subtitle so the composer's status slot doesn't jump while history syncs. + const threadSyncLabel = useDelayedStatus( + selectedThread === null ? "" : scopedThreadKey(selectedThread.environmentId, selectedThread.id), + threadSyncStatusLabel(selectedThreadDetailState.status, contentPresentation.kind), + ); + const inspectorHeader = props.inspectorPresentation ? ( + + ) : null; + if (!environmentId || !threadId || !selectedThread) { + return ( + <> + {inspectorHeader} + + + ); + } + const serverConfig = routeEnvironmentRuntime?.serverConfig ?? null; const renderThreadRouteBody = () => ( <> @@ -977,7 +997,7 @@ function ThreadRouteContent( props.inspectorPresentation ? null : ( + {inspectorHeader} {!props.inspectorPresentation && inspectorContent ? ( setComposerDraftText(draftKey, text)} reportedModelSelection={reportedModelSelection} - onSelectModel={(option) => updateModelSelection(withRememberedModelOptions(option.selection))} + onSelectModel={(option) => + updateModelSelection( + withStoredThreadModelOptions( + withRememberedModelOptions(option.selection), + selectedThread.modelSelection, + ), + ) + } restoreSelection={withRememberedModelOptions} optionDescriptors={resolveProviderOptionDescriptors({ capabilities: currentModelOption?.capabilities, diff --git a/apps/mobile/src/features/threads/checkout-new-task-branch.test.ts b/apps/mobile/src/features/threads/checkout-new-task-branch.test.ts index bee63a9775a05..328f56bcf0066 100644 --- a/apps/mobile/src/features/threads/checkout-new-task-branch.test.ts +++ b/apps/mobile/src/features/threads/checkout-new-task-branch.test.ts @@ -135,12 +135,12 @@ async function addRemote(remoteName: string, sourceRef = branch.name) { } describe("new-task branch checkout", () => { - it("switches to a free branch and clears its historical worktree path", async () => { + it("switches to a free branch despite stale current and worktree metadata", async () => { const previousWorktree = NodePath.join(directory, "previous-worktree"); await git("worktree", "add", previousWorktree, branch.name); await exec("git", ["-C", previousWorktree, "checkout", "-b", "other-branch"]); const result = await checkoutNewTaskBranch({ - branch: { ...branch, worktreePath: previousWorktree }, + branch: { ...branch, current: true, worktreePath: previousWorktree }, project: { environmentId, workspaceRoot: cwd }, workspaceMode: "local", readRefs, diff --git a/apps/mobile/src/features/threads/checkout-new-task-branch.ts b/apps/mobile/src/features/threads/checkout-new-task-branch.ts index fb7920dcdd5f6..a4f3a306b2eb0 100644 --- a/apps/mobile/src/features/threads/checkout-new-task-branch.ts +++ b/apps/mobile/src/features/threads/checkout-new-task-branch.ts @@ -34,13 +34,10 @@ export async function checkoutNewTaskBranch(input: { Cause.fail(new Error("The selected project is unavailable. Reconnect and try again.")), ); } - if (input.workspaceMode === "worktree" || input.branch.current) { - return AsyncResult.success( - input.branch.current ? { ...input.branch, worktreePath: null } : input.branch, - ); - } + if (input.workspaceMode === "worktree") return AsyncResult.success(input.branch); - // Remote refs omit local worktrees, and historical thread metadata can be stale. + // The picked ref's current flag, remote refs' missing worktrees and historical + // thread metadata can all be stale, so Git is asked again before choosing. const localName = localBranchName(input.branch); let cursor: number | undefined; for (;;) { diff --git a/apps/mobile/src/features/threads/floating-working-control.tsx b/apps/mobile/src/features/threads/floating-working-control.tsx index 55028a58b7df2..0f83743f99d31 100644 --- a/apps/mobile/src/features/threads/floating-working-control.tsx +++ b/apps/mobile/src/features/threads/floating-working-control.tsx @@ -427,21 +427,6 @@ function FloatingStatusLabel(props: { ); } - if (props.status.kind === "syncing") { - return ( - - - - {props.status.label} - - - ); - } if (props.status.kind === "compacting") { return ; } diff --git a/apps/mobile/src/features/threads/floating-working-status.ts b/apps/mobile/src/features/threads/floating-working-status.ts index 6beb9cb6cb01a..941a875c9500c 100644 --- a/apps/mobile/src/features/threads/floating-working-status.ts +++ b/apps/mobile/src/features/threads/floating-working-status.ts @@ -10,7 +10,6 @@ export interface FloatingStatusAction { export type FloatingWorkingStatus = | { readonly kind: "working"; readonly environmentId: EnvironmentId; readonly startedAt: string } - | { readonly kind: "syncing"; readonly label: string } | { readonly kind: "compacting" } | { readonly kind: "child-input"; diff --git a/apps/mobile/src/features/threads/messageTimestamp.test.ts b/apps/mobile/src/features/threads/messageTimestamp.test.ts index 94f6010acd4a1..fa1ab4be779b3 100644 --- a/apps/mobile/src/features/threads/messageTimestamp.test.ts +++ b/apps/mobile/src/features/threads/messageTimestamp.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from "vite-plus/test"; -import { formatMessageTimestamp } from "./messageTimestamp"; +import { createMessageTimeFormatter, formatMessageTimestamp } from "./messageTimestamp"; describe("formatMessageTimestamp", () => { const now = new Date(2026, 10, 8, 20, 0).getTime(); @@ -20,3 +20,15 @@ describe("formatMessageTimestamp", () => { ); }); }); + +describe("createMessageTimeFormatter", () => { + const now = new Date(2026, 0, 1, 20, 0).getTime(); + + it("follows the system clock preference over the locale's hour cycle", () => { + const format24 = createMessageTimeFormatter(true, "en-US"); + expect(format24(new Date(2026, 0, 1, 15, 7).toISOString(), now)).toBe("15:07"); + expect(format24(new Date(2026, 0, 1, 0, 7).toISOString(), now)).toBe("00:07"); + const format12 = createMessageTimeFormatter(false, "en-GB"); + expect(format12(new Date(2026, 0, 1, 15, 7).toISOString(), now)).toMatch(/^3:07\spm$/i); + }); +}); diff --git a/apps/mobile/src/features/threads/messageTimestamp.ts b/apps/mobile/src/features/threads/messageTimestamp.ts index 5624b71c0c917..cfae85e092666 100644 --- a/apps/mobile/src/features/threads/messageTimestamp.ts +++ b/apps/mobile/src/features/threads/messageTimestamp.ts @@ -7,13 +7,28 @@ const DATE_WITH_YEAR_FORMATTER = new Intl.DateTimeFormat(undefined, { }); /** Time only for today's messages; older ones lead with the day and month. */ -export function formatMessageTimestamp(input: string, nowMs: number = Date.now()): string { +export function formatMessageTimestamp( + input: string, + nowMs: number = Date.now(), + timeFormatter: Intl.DateTimeFormat = TIME_FORMATTER, +): string { const timestamp = Date.parse(input); if (Number.isNaN(timestamp)) return ""; const date = new Date(timestamp); const now = new Date(nowMs); - const time = TIME_FORMATTER.format(date); + const time = timeFormatter.format(date); if (date.toDateString() === now.toDateString()) return time; const sameYear = date.getFullYear() === now.getFullYear(); return `${(sameYear ? DATE_FORMATTER : DATE_WITH_YEAR_FORMATTER).format(date)} ${time}`; } + +/** `is24Hour` overrides the locale's hour cycle with the system clock preference when known. */ +export function createMessageTimeFormatter(is24Hour: boolean | undefined, locale?: string) { + const timeFormatter = new Intl.DateTimeFormat(locale, { + hour: "numeric", + minute: "2-digit", + ...(is24Hour === undefined ? {} : { hourCycle: is24Hour ? "h23" : "h12" }), + }); + return (input: string, nowMs?: number): string => + formatMessageTimestamp(input, nowMs, timeFormatter); +} diff --git a/apps/mobile/src/features/threads/subagent-card-presentation.test.ts b/apps/mobile/src/features/threads/subagent-card-presentation.test.ts index 2b32f84fcb9cc..14a8e6ce99dfe 100644 --- a/apps/mobile/src/features/threads/subagent-card-presentation.test.ts +++ b/apps/mobile/src/features/threads/subagent-card-presentation.test.ts @@ -1,17 +1,8 @@ import * as DateTime from "effect/DateTime"; import { describe, expect, it } from "vite-plus/test"; -import { subagentCardDetail, subagentCardElapsed } from "./subagent-card-presentation"; +import { subagentCardElapsed } from "./subagent-card-presentation"; describe("subagent card", () => { - it.each([ - ["![screenshot](screenshots/shot.png) Verified", "screenshot Verified"], - ["[docs](https://en.wikipedia.org/wiki/T3_(protocol))", "docs"], - ["[report](outputs/report%20(final).md)", "report"], - ["[report](outputs/report\\(final.md)", "report"], - ])("strips image and link markup from %s", (detail, expected) => { - expect(subagentCardDetail(detail)).toBe(expected); - }); - const start = DateTime.makeUnsafe("2026-09-21T12:00:00Z"); const end = DateTime.makeUnsafe("2026-09-21T12:01:00Z"); const later = DateTime.makeUnsafe("2026-09-21T12:02:00Z"); diff --git a/apps/mobile/src/features/threads/subagent-card-presentation.ts b/apps/mobile/src/features/threads/subagent-card-presentation.ts index 42e75c5cf214e..239f48cf441cc 100644 --- a/apps/mobile/src/features/threads/subagent-card-presentation.ts +++ b/apps/mobile/src/features/threads/subagent-card-presentation.ts @@ -3,18 +3,6 @@ import type { OrchestrationV2Subagent } from "@t3tools/contracts"; import { formatDuration } from "@t3tools/shared/orchestrationTiming"; import * as DateTime from "effect/DateTime"; -export function subagentCardDetail(detail: string | null): string | null { - if (!detail || /^Child task ended with status\b/i.test(detail)) return null; - return ( - detail - .replace(/!?\[([^\]]+)\]\((?:\\.|[^\\()]|\((?:\\.|[^\\()])*\))*\)/g, "$1") - .replace(/`/g, "") - .replace(/^[ \t]*[-*][ \t]+/gm, "") - .replace(/\s+/g, " ") - .trim() || null - ); -} - export function subagentCardElapsed( agents: ReadonlyArray>, nowMs: number, diff --git a/apps/mobile/src/features/threads/thread-inspector-preview.tsx b/apps/mobile/src/features/threads/thread-inspector-preview.tsx index 9f434051144d9..813233d59714c 100644 --- a/apps/mobile/src/features/threads/thread-inspector-preview.tsx +++ b/apps/mobile/src/features/threads/thread-inspector-preview.tsx @@ -45,12 +45,13 @@ export type ThreadInspectorPresentation = | { readonly onClose: () => void }; export function ThreadInspectorHeader( - props: ThreadInspectorPresentation & { readonly title: string }, + props: ThreadInspectorPresentation & { readonly title: string; readonly subtitle?: string }, ) { if ("onMaximize" in props) { return ( + ); } diff --git a/apps/mobile/src/features/threads/thread-list-v2-items.tsx b/apps/mobile/src/features/threads/thread-list-v2-items.tsx index 2697321cd7155..66bf2cdf1bc62 100644 --- a/apps/mobile/src/features/threads/thread-list-v2-items.tsx +++ b/apps/mobile/src/features/threads/thread-list-v2-items.tsx @@ -80,10 +80,12 @@ import { resolveThreadListV2SnoozeMenuSelection, resolveThreadListV2SnoozePresets, threadHasUnseenCompletion, + resolveThreadListV2RowStatus, resolveThreadListV2Status, resolveThreadListV2ProviderDrivers, resolveThreadListV2SwipeActions, layoutThreadListV2RowMenuActions, + threadListV2RowAccessibilityLabel, type ThreadListV2Status, } from "./threadListV2"; import { QueuedMessageIcon } from "./queued-message-icon"; @@ -379,7 +381,6 @@ export const ThreadListV2PendingRow = memo(function ThreadListV2PendingRow(props readonly pendingTask: PendingNewTask; readonly selected?: boolean; readonly project: EnvironmentProject | null; - readonly projectTitle?: string; readonly environmentLabel: string | null; readonly environmentMachine?: EnvironmentMachineKind; readonly pane?: "screen" | "sidebar"; @@ -426,7 +427,6 @@ export const ThreadListV2PendingRow = memo(function ThreadListV2PendingRow(props }), [deleteLabel, handleDelete, pendingTask.title], ); - const projectTitle = props.projectTitle ?? props.project?.title ?? pendingTask.projectTitle ?? ""; const projectCwd = props.project?.workspaceRoot || null; // Drafts only; the reads are shared per-project atoms, and queued rows skip them. const projectDefaults = useNewTaskProjectDefaults(isDraft ? props.project : null); @@ -468,7 +468,7 @@ export const ThreadListV2PendingRow = memo(function ThreadListV2PendingRow(props faviconPath={props.project.faviconPath} projectIcon={props.project.projectIcon} size={projectIconSize} - projectTitle={projectTitle} + projectTitle={props.project.title} workspaceRoot={props.project.workspaceRoot} /> @@ -773,7 +773,8 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: { const selected = props.selected === true; const rowAppearance = getThreadListV2RowAppearance(theme, sidebarPane, selected); - const status = resolveThreadListV2Status(thread); + const rawStatus = resolveThreadListV2Status(thread); + const status = resolveThreadListV2RowStatus(thread); const isUnread = status === "ready" && threadHasUnseenCompletion(thread); const workingLabel = STATUS_LABEL_BY_STATUS[status]; const showsWoke = status === "ready" && props.wokeAt !== null; @@ -783,17 +784,24 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: { : workingLabel) ?? (isUnread ? DONE_STATUS_LABEL : undefined); const recede = shouldRecedeThreadRow({ status, selected }); const isRegeneratingTitle = thread.titleRegeneration != null; - const rowAccessibilityLabel = [ - thread.title, - providerNameLabel, - props.hasQueuedMessages ? "messages queued to send" : null, - isRegeneratingTitle ? "regenerating title" : null, - showsWoke ? "Woke" : statusLabel?.label, - props.environmentLabel, - ] - .filter(Boolean) - .join(", "); const timeLabel = props.timeLabel; + const rowAccessibilityLabel = threadListV2RowAccessibilityLabel({ + title: thread.title, + variant, + woke: showsWoke, + // Working and waiting rows show only an icon and a duration, so name them for speech. + statusLabel: + statusLabel && (statusLabel.label ?? (status === "waiting" ? "Waiting" : "Working")), + timeLabel, + snoozeWakeLabel: snoozedRow ? props.snoozeWakeLabelText : undefined, + providerNameLabel, + hasQueuedMessages: props.hasQueuedMessages === true, + regeneratingTitle: isRegeneratingTitle, + }); + // Fork: screen readers also hear the environment the row shows. + const rowAccessibilityLabelWithEnvironment = props.environmentLabel + ? `${rowAccessibilityLabel}, ${props.environmentLabel}` + : rowAccessibilityLabel; const visitThread = useAtomCommand(threadEnvironment.visit, { reportFailure: false }); const handleAcknowledgeWoke = useCallback(() => { @@ -1308,7 +1316,7 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: { ) : null} - {(status === "failed" || status === "limited") && thread.runtime?.lastError ? ( + {(rawStatus === "failed" || rawStatus === "limited") && thread.runtime?.lastError ? ( ( { ); }); + it("settles a failure the user opened to ready in the row, keeping the raw status", () => { + const failed = presentThreadShell( + environmentId, + makeRawThreadShell({ + status: "failed", + latestRunId: RunId.make("run-failed"), + latestRunCompletedAt: DateTime.makeUnsafe("2026-06-01T23:59:00.000Z"), + lastVisitedAt: DateTime.makeUnsafe(NOW), + }), + ); + + expect(resolveThreadListV2Status(failed)).toBe("failed"); + expect(resolveThreadListV2RowStatus(failed)).toBe("ready"); + expect(resolveThreadListV2RowStatus({ ...failed, lastVisitedAt: null })).toBe("failed"); + }); + it("shows a waiting guest message as Request, after the agent's own requests", () => { const thread = presentThreadShell( environmentId, @@ -210,6 +228,38 @@ describe("resolveThreadListV2Status", () => { ); }); +describe("threadListV2RowAccessibilityLabel", () => { + const row = { + title: "Fix login", + variant: "card", + woke: false, + statusLabel: undefined, + timeLabel: "5m", + snoozeWakeLabel: undefined, + providerNameLabel: "Codex", + hasQueuedMessages: false, + regeneratingTitle: false, + } as const; + + it("speaks the status or time the row shows beside its title", () => { + expect(threadListV2RowAccessibilityLabel({ ...row, statusLabel: "Approval" })).toBe( + "Fix login, Approval, Codex", + ); + expect(threadListV2RowAccessibilityLabel(row)).toBe("Fix login, 5m, Codex"); + expect(threadListV2RowAccessibilityLabel({ ...row, woke: true, statusLabel: "Done" })).toBe( + "Fix login, Woke, Codex", + ); + expect( + threadListV2RowAccessibilityLabel({ + ...row, + variant: "slim", + statusLabel: "Failed", + snoozeWakeLabel: "in 2h", + }), + ).toBe("Fix login, in 2h, Codex"); + }); +}); + describe("resolveThreadListV2SwipeActions", () => { it("offers snooze for a presented input request and moves it onto the snoozed shelf", () => { const raw = makeRawThreadShell({ diff --git a/apps/mobile/src/features/threads/threadListV2.ts b/apps/mobile/src/features/threads/threadListV2.ts index 7854f9d7fdb85..61652bd0c3599 100644 --- a/apps/mobile/src/features/threads/threadListV2.ts +++ b/apps/mobile/src/features/threads/threadListV2.ts @@ -8,7 +8,10 @@ import { snoozeWakeLabel, threadWokeAt, } from "@t3tools/client-runtime/state/thread-settled"; -import type { SnoozePreset } from "@t3tools/client-runtime/state/thread-settled"; +import { + hasUnseenRunFailure, + type SnoozePreset, +} from "@t3tools/client-runtime/state/thread-settled"; import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell"; import { resolveThreadProviderStack } from "@t3tools/client-runtime/state/models"; import { @@ -296,6 +299,46 @@ export function resolveThreadListV2Status( return "ready"; } +export function threadListV2RowAccessibilityLabel(input: { + readonly title: string; + readonly variant: "card" | "slim"; + readonly woke: boolean; + readonly statusLabel: string | undefined; + readonly timeLabel: string; + readonly snoozeWakeLabel: string | undefined; + readonly providerNameLabel: string | null; + readonly hasQueuedMessages: boolean; + readonly regeneratingTitle: boolean; +}): string { + // Mirrors the row's meta line: a card shows Woke, else its status, else the time. + const meta = + input.variant === "slim" + ? (input.snoozeWakeLabel ?? input.timeLabel) + : input.woke + ? "Woke" + : (input.statusLabel ?? input.timeLabel); + return [ + input.title, + meta, + input.providerNameLabel, + input.hasQueuedMessages ? "messages queued to send" : null, + input.regeneratingTitle ? "regenerating title" : null, + ] + .filter((part) => part !== null) + .join(", "); +} + +/** Row status: a failure the user has already opened settles to ready, like Done. */ +export function resolveThreadListV2RowStatus( + thread: Parameters[0] & + Pick, +): ThreadListV2Status { + const status = resolveThreadListV2Status(thread); + // A runtime failure without a failed run has nothing to date it, so it stays. + if (status !== "failed" || thread.latestRun?.status !== "failed") return status; + return hasUnseenRunFailure(thread) ? status : "ready"; +} + function parseTimestampMs(isoDate: string): number { const parsed = Date.parse(isoDate); return Number.isNaN(parsed) ? 0 : parsed; @@ -506,7 +549,7 @@ function resolveThreadListV2ItemTimeLabel( if (showSnoozeWakeLabel) return ""; if ( variant === "card" && - (resolveThreadListV2Status(thread) !== "ready" || + (resolveThreadListV2RowStatus(thread) !== "ready" || item.wokeAt !== null || threadHasUnseenCompletion(thread)) ) diff --git a/apps/mobile/src/features/threads/threadQueueControlPresentation.test.ts b/apps/mobile/src/features/threads/threadQueueControlPresentation.test.ts index c29ef5eea7aa5..ea03de50e4de7 100644 --- a/apps/mobile/src/features/threads/threadQueueControlPresentation.test.ts +++ b/apps/mobile/src/features/threads/threadQueueControlPresentation.test.ts @@ -1,4 +1,6 @@ +import { ComposerContextId } from "@t3tools/contracts"; import { formatAssistantCitationHref } from "@t3tools/shared/assistantCitations"; +import { formatComposerContextReference } from "@t3tools/shared/composerContextReferences"; import { describe, expect, it } from "vite-plus/test"; import { resolveThreadQueueRowControls } from "./threadQueueControlPresentation"; @@ -47,6 +49,33 @@ describe("threadQueueControlPresentation", () => { ); }); + it("shows context chips by their labels", () => { + const environment = formatComposerContextReference({ + kind: "environment", + contextId: ComposerContextId.make("environment_mac"), + label: "MacBook", + }); + const file = formatComposerContextReference({ + kind: "file", + contextId: ComposerContextId.make("report"), + label: "report.pdf", + }); + const image = formatComposerContextReference({ + kind: "image", + contextId: ComposerContextId.make("screenshot"), + label: "screenshot.png", + }); + + expect( + resolveThreadQueueRowControls({ + busy: false, + canPromoteToSteer: true, + canSendNow: false, + text: `Check ${environment} for ${file} ${image} `, + }).displayText, + ).toBe("Check MacBook for report.pdf screenshot.png"); + }); + it("keeps the row already open in the composer from being reopened or steered", () => { const editing = resolveThreadQueueRowControls({ busy: false, diff --git a/apps/mobile/src/features/threads/threadQueueControlPresentation.ts b/apps/mobile/src/features/threads/threadQueueControlPresentation.ts index a62e29e14795d..ed5482b830d82 100644 --- a/apps/mobile/src/features/threads/threadQueueControlPresentation.ts +++ b/apps/mobile/src/features/threads/threadQueueControlPresentation.ts @@ -1,6 +1,9 @@ import type { EnvironmentId, RunId, ThreadId } from "@t3tools/contracts"; import { assistantCitationsToPlainText } from "@t3tools/shared/assistantCitations"; -import { isAttachmentOnlyBootstrapPrompt } from "@t3tools/shared/composerContextReferences"; +import { + isAttachmentOnlyBootstrapPrompt, + replaceComposerContextReferences, +} from "@t3tools/shared/composerContextReferences"; export const REMOVE_QUEUED_MESSAGE_ACCESSIBILITY_LABEL = "Remove queued message"; @@ -38,7 +41,9 @@ export function resolveThreadQueueRowControls(input: { // Editing and sending keep the original text so quoted citations stay linked. displayText: isAttachmentOnlyBootstrapPrompt(input.text) ? "" - : assistantCitationsToPlainText(input.text), + : assistantCitationsToPlainText( + replaceComposerContextReferences(input.text, (reference) => reference.label), + ).trim(), isEditing, }; } diff --git a/apps/mobile/src/features/threads/use-composer-command-menu.select.test.tsx b/apps/mobile/src/features/threads/use-composer-command-menu.select.test.tsx new file mode 100644 index 0000000000000..395961bd91ec7 --- /dev/null +++ b/apps/mobile/src/features/threads/use-composer-command-menu.select.test.tsx @@ -0,0 +1,55 @@ +import { act } from "react"; +import { createRoot } from "react-dom/client"; +import { expect, it, vi } from "vite-plus/test"; + +vi.mock("react-native", () => ({ Alert: { alert: vi.fn() } })); +vi.mock("../../state/queries", () => ({ + useComposerPathSearch: () => ({ entries: [], isPending: false }), + useComposerPullRequestSearch: () => ({ entries: [], isPending: false, error: null }), + useThreadSearch: () => undefined, +})); +vi.mock("../../state/use-composer-drafts", () => ({ + getComposerDraftSnapshot: vi.fn(), + setComposerDraftContext: vi.fn(), + useComposerDraftSelection: () => null, +})); +vi.mock("../../lib/uuid", () => ({ uuidv4: () => "context-id" })); +vi.mock("../../state/server", () => ({ + serverEnvironment: { + providersValueAtom: vi.fn(), + refreshProviders: Symbol("refreshProviders"), + }, +})); + +import { useComposerCommandMenu } from "./use-composer-command-menu"; + +it("/model clears only its trigger and opens the model picker", async () => { + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; + const onChangeDraftMessage = vi.fn(); + const onOpenModelPicker = vi.fn(); + let menu: ReturnType | undefined; + function Probe() { + menu = useComposerCommandMenu({ + draftMessage: "fix this /mod", + ownerKey: "draft", + environmentId: null, + projectCwd: null, + selectedProviderStatus: null, + hasThread: true, + hasCompactableConversation: false, + onChangeDraftMessage, + onOpenModelPicker, + }); + return null; + } + + const root = createRoot(document.createElement("div")); + await act(async () => root.render()); + const model = menu?.items.find((item) => item.id === "cmd:model"); + if (!model) throw new Error("Expected /model in the slash menu"); + await act(async () => menu?.onSelect(model)); + + expect(onOpenModelPicker).toHaveBeenCalledOnce(); + expect(onChangeDraftMessage).toHaveBeenCalledWith("fix this "); + act(() => root.unmount()); +}); diff --git a/apps/mobile/src/features/threads/use-composer-command-menu.ts b/apps/mobile/src/features/threads/use-composer-command-menu.ts index a77f7089c97bd..7364b1cf6cf9e 100644 --- a/apps/mobile/src/features/threads/use-composer-command-menu.ts +++ b/apps/mobile/src/features/threads/use-composer-command-menu.ts @@ -256,6 +256,7 @@ export function useComposerCommandMenu({ enabled = true, onChangeDraftMessage, onUsageLimits, + onOpenModelPicker, modelOptions, modelProviders, onFreshSession, @@ -280,6 +281,8 @@ export function useComposerCommandMenu({ readonly enabled?: boolean; readonly onChangeDraftMessage: (value: string) => void; readonly onUsageLimits?: () => void; + /** `/model` opens this picker instead of inserting text. */ + readonly onOpenModelPicker: () => void; readonly modelOptions?: ReadonlyArray; readonly modelProviders?: ReadonlyArray; /** Arms a fresh session for the next message; absent where `/clear` is unavailable. */ @@ -627,11 +630,18 @@ export function useComposerCommandMenu({ return; } - if (item.type === "slash-command" && item.command === "clear" && onFreshSession) { + const clearTriggerThen = (action: () => void) => { const cleared = replaceTextRange(draftMessage, trigger.rangeStart, trigger.rangeEnd, ""); setSelection({ start: cleared.cursor, end: cleared.cursor }); onChangeDraftMessage(cleared.text); - onFreshSession(); + action(); + }; + if (item.type === "slash-command" && item.command === "model") { + clearTriggerThen(onOpenModelPicker); + return; + } + if (item.type === "slash-command" && item.command === "clear" && onFreshSession) { + clearTriggerThen(onFreshSession); return; } if ( @@ -639,10 +649,7 @@ export function useComposerCommandMenu({ item.command.name === USAGE_LIMITS_COMMAND.name && onUsageLimits ) { - const cleared = replaceTextRange(draftMessage, trigger.rangeStart, trigger.rangeEnd, ""); - setSelection({ start: cleared.cursor, end: cleared.cursor }); - onChangeDraftMessage(cleared.text); - onUsageLimits(); + clearTriggerThen(onUsageLimits); return; } diff --git a/apps/mobile/src/features/threads/use-worktree-setup.ts b/apps/mobile/src/features/threads/use-worktree-setup.ts index 5972dc7b28180..bce827b725413 100644 --- a/apps/mobile/src/features/threads/use-worktree-setup.ts +++ b/apps/mobile/src/features/threads/use-worktree-setup.ts @@ -9,7 +9,7 @@ export function useWorktreeSetup(input: { environmentId: EnvironmentId | null; threadId: ThreadId | null; preparing: boolean; - turnStarted: boolean; + runs: Parameters[0]["runs"]; followUpSent: boolean; }) { const key = JSON.stringify([input.environmentId, input.threadId]); @@ -35,7 +35,7 @@ export function useWorktreeSetup(input: { visible: resolveVisibleWorktreeSetup({ live: snapshot, recorded: null, - turnStarted: input.turnStarted, + runs: input.runs, followUpSent: input.followUpSent, }), }; diff --git a/apps/mobile/src/features/threads/useLinkedPullRequestActions.ts b/apps/mobile/src/features/threads/useLinkedPullRequestActions.ts index 33ac7013f19d2..90bb70dec5e71 100644 --- a/apps/mobile/src/features/threads/useLinkedPullRequestActions.ts +++ b/apps/mobile/src/features/threads/useLinkedPullRequestActions.ts @@ -11,7 +11,10 @@ import { squashAtomCommandFailure, type AtomCommandResult, } from "@t3tools/client-runtime/state/runtime"; -import { planThreadPullRequestMutation } from "@t3tools/client-runtime/thread-pull-request-compatibility"; +import { + canStartThreadPullRequestWatch, + planThreadPullRequestMutation, +} from "@t3tools/client-runtime/thread-pull-request-compatibility"; import { capitalizeTerm, sourceControlClients, @@ -43,13 +46,7 @@ export function useLinkedPullRequestActions(input: { ); const canEdit = canOperate && capabilities !== undefined; const canWatch = canOperate && capabilities?.threadPullRequestWatch === true; - const canStartWatch = - canWatch && - thread !== null && - thread.archivedAt === null && - thread.settledAt === null && - thread.settledOverride !== "settled" && - thread.lineage.relationshipToParent !== "subagent"; + const canStartWatch = canWatch && thread !== null && canStartThreadPullRequestWatch(thread); const link = useAtomCommand(threadEnvironment.linkPullRequest, { reportFailure: false }); const unlink = useAtomCommand(threadEnvironment.unlinkPullRequest, { reportFailure: false }); const watch = useAtomCommand(threadEnvironment.watchPullRequest, { reportFailure: false }); diff --git a/apps/mobile/src/features/threads/useMessageTimeFormatter.ts b/apps/mobile/src/features/threads/useMessageTimeFormatter.ts new file mode 100644 index 0000000000000..2320d369762ca --- /dev/null +++ b/apps/mobile/src/features/threads/useMessageTimeFormatter.ts @@ -0,0 +1,22 @@ +import { requireNativeModule } from "expo"; +import { useEffect, useMemo, useState } from "react"; +import { AppState, Platform } from "react-native"; +import { createMessageTimeFormatter } from "./messageTimestamp"; + +function systemUses24HourClock() { + if (Platform.OS !== "android") return undefined; + return requireNativeModule<{ is24HourFormat(): boolean }>("T3NativeControls").is24HourFormat(); +} + +export function useMessageTimeFormatter() { + const [is24Hour, setIs24Hour] = useState(systemUses24HourClock); + // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Refresh the native clock preference when Android returns to the foreground. + useEffect(() => { + if (Platform.OS !== "android") return; + const subscription = AppState.addEventListener("change", (state) => { + if (state === "active") setIs24Hour(systemUses24HourClock()); + }); + return () => subscription.remove(); + }, []); + return useMemo(() => createMessageTimeFormatter(is24Hour), [is24Hour]); +} diff --git a/apps/mobile/src/features/threads/worktree-setup-card.tsx b/apps/mobile/src/features/threads/worktree-setup-card.tsx index ce2fd4abc0b0e..b5ce9d10de107 100644 --- a/apps/mobile/src/features/threads/worktree-setup-card.tsx +++ b/apps/mobile/src/features/threads/worktree-setup-card.tsx @@ -147,6 +147,17 @@ export function WorktreeSetupCard(props: WorktreeSetupCardProps) { ); } +/** Holds the card's place until the server publishes the first setup snapshot. */ +export function WorktreePreparingHeader() { + return ( + + + + + + ); +} + export function WorktreeWorkingHeader({ startedAt }: { startedAt: string }) { const now = useVisibleSecondClock(true); return ( diff --git a/apps/mobile/src/features/threads/worktree-setup-state.test.ts b/apps/mobile/src/features/threads/worktree-setup-state.test.ts index 3598f0000023e..096be8dd7f460 100644 --- a/apps/mobile/src/features/threads/worktree-setup-state.test.ts +++ b/apps/mobile/src/features/threads/worktree-setup-state.test.ts @@ -1,6 +1,7 @@ import { ThreadId, type WorktreeSetupSnapshot } from "@t3tools/contracts"; import { resolveVisibleWorktreeSetup } from "@t3tools/client-runtime/worktree-setup"; import { describe, expect, it } from "vite-plus/test"; +import * as DateTime from "effect/DateTime"; import { resolveWorktreeSetupSnapshot } from "./worktree-setup-state"; const threadId = ThreadId.make("setup-thread"); @@ -27,7 +28,7 @@ describe("mobile worktree setup handoff", () => { resolveVisibleWorktreeSetup({ live: held, recorded: null, - turnStarted: false, + runs: [], followUpSent: false, }), ).toBe(done); @@ -35,10 +36,17 @@ describe("mobile worktree setup handoff", () => { resolveVisibleWorktreeSetup({ live: held, recorded: null, - turnStarted: true, + runs: [{ startedAt: DateTime.makeUnsafe(running.startedAt) }], followUpSent: false, }), ).toBeNull(); + const afterFirstMessageEdit = { + live: held, + recorded: null, + followUpSent: false, + runs: [{ startedAt: DateTime.makeUnsafe(running.startedAt) }, { startedAt: null }], + }; + expect(resolveVisibleWorktreeSetup(afterFirstMessageEdit)).toBeNull(); expect(resolveWorktreeSetupSnapshot(threadId, undefined, held)).toBe(done); }); @@ -53,7 +61,7 @@ describe("mobile worktree setup handoff", () => { resolveVisibleWorktreeSetup({ live: held, recorded: null, - turnStarted: false, + runs: [], followUpSent: false, }), ).toBe(settled); @@ -61,7 +69,7 @@ describe("mobile worktree setup handoff", () => { resolveVisibleWorktreeSetup({ live: held, recorded: null, - turnStarted: false, + runs: [], followUpSent: true, }), ).toBeNull(); diff --git a/apps/mobile/src/lib/appearancePreferences.test.ts b/apps/mobile/src/lib/appearancePreferences.test.ts index 1bfedc7529c7c..5b1e9c33a90e1 100644 --- a/apps/mobile/src/lib/appearancePreferences.test.ts +++ b/apps/mobile/src/lib/appearancePreferences.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from "vite-plus/test"; -import { resolveAppearancePreferences } from "./appearancePreferences"; +import { resolveAppearancePreferences, resolveComposerTextRole } from "./appearancePreferences"; describe("appearancePreferences", () => { it("migrates the legacy markdownFontSize key to baseFontSize", () => { @@ -9,4 +9,14 @@ describe("appearancePreferences", () => { resolveAppearancePreferences({ baseFontSize: 16, markdownFontSize: 18 }).baseFontSize, ).toBe(16); }); + + it("scales the iOS composer text with the system text size", () => { + const body = { fontSize: 16, lineHeight: 23 }; + expect(resolveComposerTextRole(body, 1, "ios")).toEqual(body); + expect(resolveComposerTextRole(body, 2.643, "ios")).toEqual({ + fontSize: 16 * 2.643, + lineHeight: 23 * 2.643, + }); + expect(resolveComposerTextRole(body, 2.643, "android")).toEqual(body); + }); }); diff --git a/apps/mobile/src/lib/appearancePreferences.ts b/apps/mobile/src/lib/appearancePreferences.ts index 9b0be82b2c03f..6a7485e07c722 100644 --- a/apps/mobile/src/lib/appearancePreferences.ts +++ b/apps/mobile/src/lib/appearancePreferences.ts @@ -205,6 +205,17 @@ export function resolveScaledTextRole(role: keyof typeof MOBILE_TYPOGRAPHY, base }; } +// React Native text applies the system text size itself, but the iOS composer sets a fixed +// UIFont, so it needs pre-scaled metrics. Android's editor already sizes text in sp. +export function resolveComposerTextRole( + role: { readonly fontSize: number; readonly lineHeight: number }, + fontScale: number, + os: string, +) { + if (os !== "ios") return role; + return { fontSize: role.fontSize * fontScale, lineHeight: role.lineHeight * fontScale }; +} + export function resolveNativeMarkdownTypography(baseFontSize: number): NativeMarkdownTypography { const fontSizes = resolveMarkdownFontSizes(baseFontSize); return { diff --git a/apps/mobile/src/lib/composerImages.test.ts b/apps/mobile/src/lib/composerImages.test.ts index c723293c85270..b5b10ac02c7bb 100644 --- a/apps/mobile/src/lib/composerImages.test.ts +++ b/apps/mobile/src/lib/composerImages.test.ts @@ -199,10 +199,13 @@ describe("native pasted image cleanup", () => { it("converts owned files to data-backed previews and deletes the source", async () => { const uri = "file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/id.png"; + const gif = + "file:///private/var/mobile/Containers/Data/Application/app/tmp/t3-composer-paste/anim.gif"; files.set(uri, { base64: "aGVsbG8=", deleted: false }); + files.set(gif, { base64: "R0lGODlh", deleted: false }); const { attachments } = await convertPastedImagesToAttachments({ - uris: [uri], + uris: [uri, gif], existingCount: 0, }); @@ -211,8 +214,10 @@ describe("native pasted image cleanup", () => { dataUrl: "data:image/png;base64,aGVsbG8=", previewUri: "data:image/png;base64,aGVsbG8=", }), + expect.objectContaining({ mimeType: "image/gif", dataUrl: "data:image/gif;base64,R0lGODlh" }), ]); expect(files.get(uri)?.deleted).toBe(true); + expect(files.get(gif)?.deleted).toBe(true); }); it("deletes rejected and overflow owned files without deleting user-owned files", async () => { diff --git a/apps/mobile/src/lib/composerImages.ts b/apps/mobile/src/lib/composerImages.ts index 7f7718f540ca2..6c19874e1df53 100644 --- a/apps/mobile/src/lib/composerImages.ts +++ b/apps/mobile/src/lib/composerImages.ts @@ -561,23 +561,25 @@ function isOwnedPastedImageUri(uri: string): boolean { return false; } const segments = url.pathname.split("/").filter(Boolean); - return ( - segments.at(-2) === OWNED_PASTED_IMAGE_DIRECTORY && segments.at(-1)?.endsWith(".png") === true - ); + return segments.at(-2) === OWNED_PASTED_IMAGE_DIRECTORY; } catch { return false; } } +/** Leave out existingCount when the draft store caps after its duplicate check. */ export async function convertPastedImagesToAttachments(input: { readonly uris: ReadonlyArray; - readonly existingCount: number; + readonly existingCount?: number; }): Promise<{ readonly attachments: ReadonlyArray; readonly error: string | null; }> { const { File } = await import("expo-file-system"); - const remainingSlots = PROVIDER_SEND_TURN_MAX_ATTACHMENTS - input.existingCount; + const remainingSlots = + input.existingCount === undefined + ? Number.POSITIVE_INFINITY + : PROVIDER_SEND_TURN_MAX_ATTACHMENTS - input.existingCount; const attachments: DraftComposerImageAttachment[] = []; let error: string | null = null; diff --git a/apps/mobile/src/lib/enrichedLinkPresentation.test.ts b/apps/mobile/src/lib/enrichedLinkPresentation.test.ts index 4dd5ecbd08317..771674cc4eff6 100644 --- a/apps/mobile/src/lib/enrichedLinkPresentation.test.ts +++ b/apps/mobile/src/lib/enrichedLinkPresentation.test.ts @@ -34,6 +34,8 @@ describe("Enriched link presentation", () => { "https://example.com/app.tsx", "x + y", "README.md", + "Dockerfile", + "docker/Dockerfiles", "/app/settings", ]) { expect(ENRICHED_INLINE_FILE_LINK_REGEX.test(text), text).toBe(false); diff --git a/apps/mobile/src/lib/enrichedLinkPresentation.ts b/apps/mobile/src/lib/enrichedLinkPresentation.ts index 5d2e9431339d1..f31d8659b6614 100644 --- a/apps/mobile/src/lib/enrichedLinkPresentation.ts +++ b/apps/mobile/src/lib/enrichedLinkPresentation.ts @@ -17,6 +17,7 @@ import { parseComposerContextHref, } from "@t3tools/shared/composerContextReferences"; import { hasThreadLinks, parseThreadLinkHref } from "@t3tools/shared/threadLinks"; +import { EXTENSIONLESS_FILE_NAMES } from "@t3tools/shared/markdownLinks"; export function enrichedContextLinkPresentation( href: string, @@ -91,8 +92,14 @@ export function enrichedLinkVariantPattern(url: string) { return `^${url.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}$`; } -export const ENRICHED_INLINE_FILE_LINK_REGEX = - /^(?!\S*:\/\/)(?:(?:\.{1,2}[\\/]|~[\\/]|\/(?:Users|home|tmp|var|etc|opt|mnt|Volumes|private|root|usr|bin|sbin|lib|lib64|srv|dev|proc|sys|run|boot|media|workspace|workspaces)\/|[A-Za-z]:[\\/])[^\s`]+|(?:[\p{L}\p{M}\p{N}._-]+[\\/])+[\p{L}\p{M}\p{N}._-]+\.[A-Za-z0-9_-]+(?::\d+(?::\d+|-\d+)?)?|[\p{L}\p{M}\p{N}._-]+\.[A-Za-z0-9_-]+:\d+(?::\d+|-\d+)?)$/u; +const EXTENSIONLESS_FILE_NAME_PATTERN = [...EXTENSIONLESS_FILE_NAMES] + .map((name) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")) + .join("|"); + +export const ENRICHED_INLINE_FILE_LINK_REGEX = new RegExp( + String.raw`^(?!\S*:\/\/)(?:(?:\.{1,2}[\\/]|~[\\/]|\/(?:Users|home|tmp|var|etc|opt|mnt|Volumes|private|root|usr|bin|sbin|lib|lib64|srv|dev|proc|sys|run|boot|media|workspace|workspaces)\/|[A-Za-z]:[\\/])[^\s\x60]+|(?:[\p{L}\p{M}\p{N}._-]+[\\/])+(?:[\p{L}\p{M}\p{N}._-]+\.[A-Za-z0-9_-]+|(?:${EXTENSIONLESS_FILE_NAME_PATTERN}))(?::\d+(?::\d+|-\d+)?)?|[\p{L}\p{M}\p{N}._-]+\.[A-Za-z0-9_-]+:\d+(?::\d+|-\d+)?)$`, + "u", +); export function enrichedSkillLinkRegex(skills?: ReadonlyArray) { const names = [...new Set(skills?.map((skill) => skill.name) ?? [])].filter(Boolean); diff --git a/apps/mobile/src/lib/filePreview.test.ts b/apps/mobile/src/lib/filePreview.test.ts index 8363b0ba5c958..d175f272e3dec 100644 --- a/apps/mobile/src/lib/filePreview.test.ts +++ b/apps/mobile/src/lib/filePreview.test.ts @@ -113,6 +113,7 @@ vi.mock("../components/ui/Button", () => ({ createElement("button", { onClick: props.onPress, disabled: props.disabled }, props.label), })); vi.mock("expo", () => ({ + requireNativeModule: () => ({ is24HourFormat: () => true }), useEvent: (_player: unknown, _event: unknown, initial: unknown) => initial, useReleasingSharedObjectWithLifecycle: ( lifecycle: { @@ -240,6 +241,7 @@ vi.mock("../components/ui/Menu", () => ({ })); vi.mock("../components/ui/FilePreviewGallery", () => ({ FilePreviewGallery: ({ children }: { children?: ReactNode }) => children, + useFilePreviewGallery: () => (source: unknown) => source, })); vi.mock("../components/ui/FilePreviewModal", () => ({ FilePreviewModal: (props: { source: { name: string } | null }) => diff --git a/apps/mobile/src/lib/markdownInlineFileLinks.test.ts b/apps/mobile/src/lib/markdownInlineFileLinks.test.ts index 2d167b58ec611..12b80ed408168 100644 --- a/apps/mobile/src/lib/markdownInlineFileLinks.test.ts +++ b/apps/mobile/src/lib/markdownInlineFileLinks.test.ts @@ -95,7 +95,13 @@ describe("inline code that names a file", () => { expect(rewrite("See `src/math.ts:2` and `./README.md`.")).toBe( "See [src/math.ts:2]() and [./README.md](<./README.md>).", ); - for (const path of ["src/數學.ts:2", "數學/math.ts:2", "數學.ts:2", "src/數學.ts:2-4"]) { + for (const path of [ + "src/數學.ts:2", + "數學/math.ts:2", + "數學.ts:2", + "src/數學.ts:2-4", + "docker/Dockerfile", + ]) { expect(rewrite(`See \`${path}\`.`), path).toBe(`See [${path}](<${path}>).`); } }); diff --git a/apps/mobile/src/lib/modelOptions.test.ts b/apps/mobile/src/lib/modelOptions.test.ts index bf03fed351076..73ce33f99dbf1 100644 --- a/apps/mobile/src/lib/modelOptions.test.ts +++ b/apps/mobile/src/lib/modelOptions.test.ts @@ -18,6 +18,7 @@ import { resolveDefaultableModelSelection, resolveNewTaskModelSelection, resolveSelectableModelSelection, + withStoredThreadModelOptions, type ModelOption, } from "./modelOptions"; @@ -144,6 +145,27 @@ describe("mobile model options", () => { expect(explicitOption?.selection.options).toEqual([{ id: "serviceTier", value: "priority" }]); }); + it("carries a thread's stored options onto an optionless pick of its own model", () => { + const claude = ProviderInstanceId.make("claudeAgent"); + const stored: ModelSelection = { + instanceId: claude, + model: "claude-opus-5-5", + options: [{ id: "effort", value: "high" }], + }; + expect( + withStoredThreadModelOptions({ instanceId: claude, model: stored.model }, stored), + ).toEqual(stored); + const remembered: ModelSelection = { ...stored, options: [{ id: "effort", value: "low" }] }; + expect(withStoredThreadModelOptions(remembered, stored)).toBe(remembered); + const otherInstance: ModelSelection = { + instanceId: ProviderInstanceId.make("claude_work"), + model: stored.model, + }; + expect(withStoredThreadModelOptions(otherInstance, stored)).toBe(otherInstance); + const otherModel: ModelSelection = { instanceId: claude, model: "claude-sonnet-5-5" }; + expect(withStoredThreadModelOptions(otherModel, stored)).toBe(otherModel); + }); + it("limits existing threads to their provider while new tasks keep every provider", () => { const providers = ["codex", "claudeAgent"].map((instanceId) => ({ instanceId, diff --git a/apps/mobile/src/lib/modelOptions.ts b/apps/mobile/src/lib/modelOptions.ts index 222d63a010408..e68a84b585417 100644 --- a/apps/mobile/src/lib/modelOptions.ts +++ b/apps/mobile/src/lib/modelOptions.ts @@ -287,6 +287,22 @@ export function resolveNewTaskModelSelection(input: { ); } +// Rows built after a pick on another instance carry no options for the thread's own model. +export function withStoredThreadModelOptions( + selection: ModelSelection, + stored: ModelSelection, +): ModelSelection { + if ( + (selection.options?.length ?? 0) > 0 || + !stored.options?.length || + selection.instanceId !== stored.instanceId || + selection.model !== stored.model + ) { + return selection; + } + return { ...selection, options: stored.options }; +} + export function buildModelOptions( config: T3ServerConfig | null | undefined, fallbackModelSelection: ModelSelection | null, diff --git a/apps/mobile/src/lib/time.test.ts b/apps/mobile/src/lib/time.test.ts index 8881b6bbab8b7..c9c24b9c8f97f 100644 --- a/apps/mobile/src/lib/time.test.ts +++ b/apps/mobile/src/lib/time.test.ts @@ -1,9 +1,39 @@ import { describe, expect, it } from "vite-plus/test"; -import { msUntilRelativeTimeChange, relativeTime } from "./time"; +import { byTimestamp, compareTimestamps, msUntilRelativeTimeChange, relativeTime } from "./time"; const at = "2026-10-19T12:00:00.000Z"; const timestamp = Date.parse(at); +describe("timestamp order", () => { + it("orders host timestamps by instant across offsets and fractional seconds, missing ones first", () => { + const dates = [ + "2026-07-05T00:30:00.5Z", + "2026-07-05T01:00:00+02:00", + "", + "2026-07-05T00:30:00Z", + ]; + + expect(dates.toSorted(compareTimestamps)).toEqual([ + "", + "2026-07-05T01:00:00+02:00", + "2026-07-05T00:30:00Z", + "2026-07-05T00:30:00.5Z", + ]); + }); + it("keeps equal instants in input order whichever way it sorts", () => { + const rows = [ + { id: "comment", date: "2026-07-05T00:30:00Z" }, + { id: "commit", date: "2026-07-04T20:30:00-04:00" }, + { id: "older", date: "2026-07-04T00:00:00Z" }, + ]; + const ids = (newestFirst: boolean) => + rows.toSorted(byTimestamp(newestFirst)).map((row) => row.id); + + expect(ids(false)).toEqual(["older", "comment", "commit"]); + expect(ids(true)).toEqual(["comment", "commit", "older"]); + }); +}); + describe("relative time", () => { it.each([ [59_999, "<1m", "1m"], diff --git a/apps/mobile/src/lib/time.ts b/apps/mobile/src/lib/time.ts index 8635ccb37138b..5a32e16fc75c9 100644 --- a/apps/mobile/src/lib/time.ts +++ b/apps/mobile/src/lib/time.ts @@ -1,3 +1,15 @@ +// Host timestamps may carry UTC offsets or mixed fractions, so text order is not time order. +export function compareTimestamps(left: string, right: string): number { + return (Date.parse(left) || 0) - (Date.parse(right) || 0); +} + +export function byTimestamp(newestFirst: boolean) { + return (left: { readonly date: string }, right: { readonly date: string }) => + newestFirst + ? compareTimestamps(right.date, left.date) + : compareTimestamps(left.date, right.date); +} + export function relativeTime(input: string, nowMs: number = Date.now()): string { const timestamp = Date.parse(input); if (Number.isNaN(timestamp)) { diff --git a/apps/mobile/src/lib/videoThumbnails.test.ts b/apps/mobile/src/lib/videoThumbnails.test.ts index 155a906143a26..a35e62b98ae71 100644 --- a/apps/mobile/src/lib/videoThumbnails.test.ts +++ b/apps/mobile/src/lib/videoThumbnails.test.ts @@ -5,11 +5,20 @@ vi.mock("expo-video", () => ({ createVideoPlayer: mocks.createPlayer })); let thumbnails: typeof import("./videoThumbnails"); const frame = { width: 480, height: 270 }; -const player = () => ({ - replaceAsync: vi.fn(async (): Promise => {}), - generateThumbnailsAsync: vi.fn(async () => [frame]), - release: vi.fn(), -}); +const player = () => { + const listeners = new Set<(event: { source: unknown }) => void>(); + const attach = () => listeners.forEach((listener) => listener({ source: {} })); + return { + attach, + addListener: vi.fn((_event: string, listener: (event: { source: unknown }) => void) => { + listeners.add(listener); + return { remove: () => listeners.delete(listener) }; + }), + replaceAsync: vi.fn(async (): Promise => queueMicrotask(attach)), + generateThumbnailsAsync: vi.fn(async () => [frame]), + release: vi.fn(), + }; +}; const source = () => ({ uri: "file:///clip.mp4", dispose: vi.fn() }); beforeEach(async () => { @@ -51,7 +60,7 @@ describe("video thumbnails", () => { expect(first.release).toHaveBeenCalledTimes(1); }); - it("releases an active canceled player and ignores late source loading", async () => { + it("finishes a started extraction after its reader leaves so a re-signed URL reuses it", async () => { const started = Promise.withResolvers(); const replaced = Promise.withResolvers(); const first = player(); @@ -62,21 +71,71 @@ describe("video thumbnails", () => { mocks.createPlayer.mockReturnValueOnce(first); const file = source(); const controller = new AbortController(); - const request = thumbnails.loadVideoThumbnail("canceled", async () => file, controller.signal); + const request = thumbnails.loadVideoThumbnail("video", async () => file, controller.signal); await started.promise; controller.abort(); - expect(await request).toBeNull(); + const resigned = thumbnails.loadVideoThumbnail( + "video", + async () => source(), + new AbortController().signal, + ); + replaced.resolve(); + first.attach(); + expect(await request).toBe(frame); + expect(await resigned).toBe(frame); + expect(mocks.createPlayer).toHaveBeenCalledTimes(1); expect(first.release).toHaveBeenCalledTimes(1); expect(file.dispose).toHaveBeenCalledTimes(1); - replaced.resolve(); - expect( - await thumbnails.loadVideoThumbnail( - "next", - async () => source(), - new AbortController().signal, - ), - ).toBe(frame); + }); + + it("asks for a frame only once the player has attached the item", async () => { + const first = player(); + let attached = false; + // Like iOS: replaceAsync resolves before the item attaches, and until then + // generateThumbnailsAsync finds no asset. + first.replaceAsync.mockImplementation(async () => {}); + first.generateThumbnailsAsync.mockImplementation(async () => (attached ? [frame] : [])); + mocks.createPlayer.mockReturnValueOnce(first); + const request = thumbnails.loadVideoThumbnail( + "attach", + async () => source(), + new AbortController().signal, + ); + await vi.waitFor(() => expect(first.replaceAsync).toHaveBeenCalled()); + await first.replaceAsync.mock.results[0]!.value; + attached = true; + first.attach(); + expect(await request).toBe(frame); + }); + + it("gives up on an item that never attaches without holding up the next request", async () => { + vi.useFakeTimers(); + const first = player(); + const subscriptions: Array<{ remove: ReturnType }> = []; + first.addListener.mockImplementation(() => { + const subscription = { remove: vi.fn() }; + subscriptions.push(subscription); + return subscription; + }); + first.replaceAsync.mockImplementation(async () => {}); + mocks.createPlayer.mockReturnValueOnce(first); + const file = source(); + const stuck = thumbnails.loadVideoThumbnail( + "never-attached", + async () => file, + new AbortController().signal, + ); + const next = thumbnails.loadVideoThumbnail( + "next", + async () => source(), + new AbortController().signal, + ); + await vi.advanceTimersByTimeAsync(15_000); + expect(await stuck).toBeNull(); expect(first.generateThumbnailsAsync).not.toHaveBeenCalled(); - expect(thumbnails.cachedVideoThumbnail("canceled")).toBeNull(); + expect(subscriptions[0]!.remove).toHaveBeenCalledTimes(1); + expect(first.release).toHaveBeenCalledTimes(1); + expect(file.dispose).toHaveBeenCalledTimes(1); + expect(await next).toBe(frame); }); }); diff --git a/apps/mobile/src/lib/videoThumbnails.ts b/apps/mobile/src/lib/videoThumbnails.ts index 7606a71457dba..33e4f74e42f63 100644 --- a/apps/mobile/src/lib/videoThumbnails.ts +++ b/apps/mobile/src/lib/videoThumbnails.ts @@ -10,35 +10,40 @@ export function cachedVideoThumbnail(key: string): VideoThumbnail | null { return thumbnails.get(key) ?? null; } -async function extractFrame(uri: string, signal: AbortSignal) { +async function extractFrame(uri: string) { const { createVideoPlayer } = await import("expo-video"); - if (signal.aborted) return null; const player = createVideoPlayer(null); let disposed = false; - let cancel = () => {}; let timeout: ReturnType | undefined; + let attachedSubscription: { remove(): void } | undefined; try { // Never play or change audio settings: thumbnails must leave the shared audio session alone. player.bufferOptions = { preferredForwardBufferDuration: 1 }; - const canceled = new Promise((resolve) => { - cancel = () => resolve(null); + const timedOut = new Promise((resolve) => { + timeout = setTimeout(() => resolve(null), 15_000); + }); + // iOS resolves replaceAsync before the main thread attaches the item, and until then + // generateThumbnailsAsync finds no asset and returns no frames. + const attached = new Promise((resolve) => { + attachedSubscription = player.addListener("sourceChange", ({ source }) => { + if (source) resolve(); + }); }); - signal.addEventListener("abort", cancel, { once: true }); - timeout = setTimeout(cancel, 15_000); const frame = (async () => { await player.replaceAsync({ uri, contentType: "progressive" }); - if (disposed || signal.aborted) return null; + await attached; + if (disposed) return null; const [thumbnail] = await player.generateThumbnailsAsync([0], { maxWidth: 480, maxHeight: 480, }); return thumbnail ?? null; })(); - return await Promise.race([frame, canceled]); + return await Promise.race([frame, timedOut]); } finally { disposed = true; clearTimeout(timeout); - signal.removeEventListener("abort", cancel); + attachedSubscription?.remove(); player.release(); } } @@ -62,8 +67,11 @@ export function loadVideoThumbnail( const source = await resolveSource(signal); if (!source) return null; try { - const thumbnail = await extractFrame(source.uri, signal); - if (!thumbnail || signal.aborted) return null; + if (signal.aborted) return null; + // A started extraction finishes even if its reader leaves: its requests are already in + // flight, and a re-signed URL for the same video replaces the reader, not the frame. + const thumbnail = await extractFrame(source.uri); + if (!thumbnail) return null; thumbnails.set(key, thumbnail); if (thumbnails.size > MAX_CACHED_THUMBNAILS) { thumbnails.delete(thumbnails.keys().next().value!); diff --git a/apps/mobile/src/native/T3ComposerEditor.ios.tsx b/apps/mobile/src/native/T3ComposerEditor.ios.tsx index 437bcda4d012d..56f80be2e7be8 100644 --- a/apps/mobile/src/native/T3ComposerEditor.ios.tsx +++ b/apps/mobile/src/native/T3ComposerEditor.ios.tsx @@ -27,7 +27,7 @@ import { contextChipIconUri, modelChipIcon, modelChipIconUri } from "../lib/cont import { useUniwindTheme } from "../lib/useUniwindTheme"; import { createNativeComposerTheme } from "../lib/nativeComposerTheme"; import { FONT_FAMILIES } from "../lib/fontFamilies"; -import { useScaledTextRole } from "../features/settings/appearance/useScaledTextRole"; +import { useComposerTextRole } from "../features/settings/appearance/useScaledTextRole"; import { acknowledgeComposerNativeEvent, assumeComposerControlledState, @@ -176,7 +176,7 @@ export function ComposerEditor({ }); setConfirmedTokens({ value: props.value, tokens }); } - const bodyText = useScaledTextRole("body"); + const bodyText = useComposerTextRole(); const theme = useUniwindTheme(); const fontFamily = FONT_FAMILIES.regular; diff --git a/apps/mobile/src/native/createV5StackNavigator.ios.test.tsx b/apps/mobile/src/native/createV5StackNavigator.ios.test.tsx new file mode 100644 index 0000000000000..0afe16e701263 --- /dev/null +++ b/apps/mobile/src/native/createV5StackNavigator.ios.test.tsx @@ -0,0 +1,97 @@ +import type { ParamListBase, StackNavigationState } from "@react-navigation/native"; +import { act, createContext, type ComponentProps, type ReactNode } from "react"; +import { createRoot } from "react-dom/client"; +import type { Stack as NativeStack } from "react-native-screens"; +import { expect, it, vi } from "vite-plus/test"; + +type ScreenProps = ComponentProps; +const screens = vi.hoisted(() => new Map()); + +vi.mock("react-native", () => ({ + StyleSheet: { flatten: (style: unknown) => style }, + View: ({ children }: { children?: ReactNode }) => children, +})); +vi.mock("react-native-screens", () => ({ + Stack: { + Host: ({ children }: { children?: ReactNode }) => children, + Screen: (props: ScreenProps) => { + screens.set(props.screenKey, props); + return null; + }, + }, +})); +vi.mock("@react-navigation/native", () => ({ + NavigationContext: createContext(undefined), + NavigationRouteContext: createContext(undefined), + usePreventRemoveContext: () => ({ preventedRoutes: {} }), +})); +vi.mock("@react-navigation/native-stack", () => ({ NativeStackView: () => null })); +vi.mock("./V5StackHeader.ios", () => ({ V5StackHeader: () => null })); +vi.mock("./NativeColumnContent.ios", () => ({ NativeColumnContent: () => null })); +// Fork: the fork's navigator reads the device type for iPad pops; this keeps the suite on the phone path. +vi.mock("./native-layout-metrics", () => ({ useDeviceType: () => "phone" })); +vi.mock("./NativeScrollViewMarker", () => ({ + NativeScrollEdgeEffectsContext: createContext(undefined), +})); + +import { V5CardStackView, type V5StackViewProps } from "./createV5StackNavigator.ios"; + +const home = { key: "home", name: "Home" }; +const thread = { key: "thread", name: "Thread" }; + +function fire(key: string, event: "onWillAppear" | "onWillDisappear" | "onDidDisappear") { + const handler = screens.get(key)?.[event] as (() => void) | undefined; + if (!handler) throw new Error(`No ${event} for ${key}`); + act(() => handler()); +} + +it("forgets native Back when a full-screen presentation covers the top card", () => { + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); + const state: StackNavigationState = { + key: "stack", + type: "stack", + stale: false, + index: 1, + routeNames: ["Home", "Thread"], + routes: [home, thread], + preloadedRoutes: [], + }; + const navigation = { getState: () => state, emit: vi.fn(), dispatch: vi.fn() }; + const descriptor = (route: typeof home) => ({ + route, + navigation, + options: {}, + render: () => null, + }); + const onNativePopChange = vi.fn(); + const root = createRoot(document.createElement("div")); + try { + act(() => + root.render( + descriptor(home), + } as unknown as V5StackViewProps)} + onNativePopChange={onNativePopChange} + />, + ), + ); + + // A presentation covers the thread: nothing in the stack appears beneath it. + fire("thread", "onWillDisappear"); + fire("thread", "onDidDisappear"); + expect(onNativePopChange).toHaveBeenLastCalledWith(null); + + // Native Back reveals Home, so the thread stays the card being popped. + fire("thread", "onWillDisappear"); + fire("home", "onWillAppear"); + fire("thread", "onDidDisappear"); + expect(onNativePopChange).toHaveBeenLastCalledWith("thread"); + } finally { + act(() => root.unmount()); + vi.unstubAllGlobals(); + } +}); diff --git a/apps/mobile/src/native/createV5StackNavigator.ios.tsx b/apps/mobile/src/native/createV5StackNavigator.ios.tsx index a6473c9d96afe..35cc67b82cb57 100644 --- a/apps/mobile/src/native/createV5StackNavigator.ios.tsx +++ b/apps/mobile/src/native/createV5StackNavigator.ios.tsx @@ -70,6 +70,8 @@ export function V5CardStackView( // Fork: iPad sidebar selections reuse one native detail controller until upstream separates selection from pushes. const nativePopSource = useRef(null); const [nativePopKey, setNativePopKey] = useState(null); + // Back reveals the card beneath; a full-screen presentation covers the card without revealing one. + const cardRevealed = useRef(false); const { onNativePopChange } = props; const setNativePopSource = useCallback( (key: string | null) => { @@ -193,6 +195,7 @@ export function V5CardStackView( if (preventedRoutes[route.key]?.preventRemove) descriptor.navigation.goBack(); }} onWillAppear={() => { + cardRevealed.current = true; if (index === 0) props.onRootVisibilityChange?.(true); props.navigation.emit({ type: "transitionStart", @@ -240,6 +243,7 @@ export function V5CardStackView( const state = props.navigation.getState(); // Only native Back leaves the disappearing card active; JS navigation already changed history. if (state.index > 0 && state.routes[state.index]?.key === route.key) { + cardRevealed.current = false; setNativePopSource(route.key); } props.navigation.emit({ @@ -248,13 +252,16 @@ export function V5CardStackView( data: { closing: true }, }); }} - onDidDisappear={() => + onDidDisappear={() => { + if (nativePopSource.current === route.key && !cardRevealed.current) { + setNativePopSource(null); + } props.navigation.emit({ type: "transitionEnd", target: route.key, data: { closing: true }, - }) - } + }); + }} > diff --git a/apps/mobile/src/native/workspace-stack-projection.test.ts b/apps/mobile/src/native/workspace-stack-projection.test.ts index 35117ba5457dd..563ac4152a6b0 100644 --- a/apps/mobile/src/native/workspace-stack-projection.test.ts +++ b/apps/mobile/src/native/workspace-stack-projection.test.ts @@ -290,7 +290,7 @@ describe("collapsed split Back", () => { routeParamList: {}, routeGetIdList: {}, }; - type Appearance = "willAppear" | "didAppear" | "willDisappear"; + type Appearance = "willAppear" | "didAppear" | "willDisappear" | "didDisappear"; // UIKit's appearance callbacks for one collapsed transition, against the router at each step. function replay( steps: ReadonlyArray]>, @@ -359,6 +359,30 @@ describe("collapsed split Back", () => { ).toBeNull(); }); + it("forgets the leaving detail when a full-screen presentation covers it", () => { + const state = history([home, thread]); + const leavingAfter = (steps: ReadonlyArray) => + steps.reduce( + (column, [type, key]) => + collapsedSplitTransition(column, { type, key }, state, home.key).column, + { visible: "secondary", leaving: null }, + ).leaving; + expect( + leavingAfter([ + ["willDisappear", thread.key], + ["didDisappear", thread.key], + ]), + ).toBeNull(); + // Native Back reveals the list before the detail finishes disappearing. + expect( + leavingAfter([ + ["willDisappear", thread.key], + ["willAppear", home.key], + ["didDisappear", thread.key], + ]), + ).toBe(thread.key); + }); + it("does not mistake JS navigation or a cold link for native Back", () => { const pushed = history([home, thread, files]); expect( diff --git a/apps/mobile/src/native/workspace-stack-projection.ts b/apps/mobile/src/native/workspace-stack-projection.ts index 47dbc9341e556..4a1336428b7e1 100644 --- a/apps/mobile/src/native/workspace-stack-projection.ts +++ b/apps/mobile/src/native/workspace-stack-projection.ts @@ -98,6 +98,8 @@ export function nativeStackPopAction( export interface CollapsedSplitColumn { readonly visible: "primary" | "secondary"; readonly leaving: string | null; + /** Back reveals another screen; a full-screen presentation covers the detail without one. */ + readonly revealed?: boolean; } /** @@ -107,7 +109,10 @@ export interface CollapsedSplitColumn { */ export function collapsedSplitTransition( column: CollapsedSplitColumn, - event: { readonly type: "willAppear" | "didAppear" | "willDisappear"; readonly key: string }, + event: { + readonly type: "willAppear" | "didAppear" | "willDisappear" | "didDisappear"; + readonly key: string; + }, state: Pick, primaryKey: string, settingsAsDetail = false, @@ -121,14 +126,18 @@ export function collapsedSplitTransition( return { column: state.index > 0 && state.routes[state.index]?.key === event.key - ? { ...column, leaving: event.key } + ? { ...column, leaving: event.key, revealed: false } : column, pop: null, }; case "willAppear": // A cancelled swipe, or an expansion that keeps the detail on screen. + if (column.leaving === event.key) return { column: { ...column, leaving: null }, pop: null }; + return { column: column.leaving ? { ...column, revealed: true } : column, pop: null }; + case "didDisappear": return { - column: column.leaving === event.key ? { ...column, leaving: null } : column, + column: + column.leaving === event.key && !column.revealed ? { ...column, leaving: null } : column, pop: null, }; case "didAppear": diff --git a/apps/mobile/src/persistence/mobile-preferences.ts b/apps/mobile/src/persistence/mobile-preferences.ts index f29fbc8c119b4..7a26fc2b8d694 100644 --- a/apps/mobile/src/persistence/mobile-preferences.ts +++ b/apps/mobile/src/persistence/mobile-preferences.ts @@ -79,7 +79,14 @@ const WorkspaceSelections = Schema.Struct({ }); export type WorkspaceSelections = typeof WorkspaceSelections.Type; +const PullRequestActivityVisibility = Schema.Struct({ + bots: Schema.Boolean, + resolved: Schema.Boolean, +}); +export type PullRequestActivityVisibility = typeof PullRequestActivityVisibility.Type; + const isThreadListFilters = Schema.is(ThreadListFilters); +const isPullRequestActivityVisibility = Schema.is(PullRequestActivityVisibility); const isWorkspaceSelections = Schema.is(WorkspaceSelections); const isPullRequestListPreferences = Schema.is(PullRequestListPreferences); const ThreadProject = Schema.Struct({ @@ -92,6 +99,8 @@ export interface Preferences { readonly navigationTabOrder?: readonly string[]; readonly hiddenNavigationTabs?: readonly string[]; readonly pullRequestHideTestFiles?: boolean; + /** Which hidden-by-default PR activity is shown: bot comments and resolved or dismissed ones. */ + readonly pullRequestActivityVisibility?: PullRequestActivityVisibility; readonly pullRequestMergeMethod?: PullRequestMergeMethod; readonly liveActivitiesEnabled?: boolean; readonly themeId?: MobileThemeId; @@ -181,6 +190,7 @@ function sanitizePreferences(parsed: Preferences): Preferences { navigationTabOrder?: readonly string[]; hiddenNavigationTabs?: readonly string[]; pullRequestHideTestFiles?: boolean; + pullRequestActivityVisibility?: PullRequestActivityVisibility; pullRequestMergeMethod?: PullRequestMergeMethod; liveActivitiesEnabled?: boolean; themeId?: MobileThemeId; @@ -233,6 +243,10 @@ function sanitizePreferences(parsed: Preferences): Preferences { preferences.pullRequestHideTestFiles = parsed.pullRequestHideTestFiles; } + if (isPullRequestActivityVisibility(parsed.pullRequestActivityVisibility)) { + preferences.pullRequestActivityVisibility = parsed.pullRequestActivityVisibility; + } + if (isPullRequestMergeMethod(parsed.pullRequestMergeMethod)) { preferences.pullRequestMergeMethod = parsed.pullRequestMergeMethod; } diff --git a/apps/mobile/src/state/pull-requests.ts b/apps/mobile/src/state/pull-requests.ts index 8ca65760e5913..cb8606b96b5f9 100644 --- a/apps/mobile/src/state/pull-requests.ts +++ b/apps/mobile/src/state/pull-requests.ts @@ -1,7 +1,9 @@ import { createPullRequestEnvironmentAtoms, + editHeldPullRequestListEntries, createPullRequestStackAtomFamily, patchPullRequestListEntries, + pullRequestPendingEditsAtom, } from "@t3tools/client-runtime/state/pull-requests"; import type { EnvironmentId, PullRequestListInput, PullRequestRef } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; @@ -66,11 +68,30 @@ export const composerPullRequests = createPullRequestEnvironmentAtoms(connection pages.entries, target.input, reply, + registry.get(pullRequestPendingEditsAtom(target.environmentId)), ); return entries === "refresh" || entries === pages.entries ? pages : { ...pages, entries }; }); } }), + acceptedEdit: (target, edit, registry) => + Effect.sync(() => { + for (const atom of registry.getNodes().keys()) { + const listing = + typeof atom === "string" ? undefined : pullRequestListPagesTargets.get(atom); + if (listing === undefined || listing[0] !== target.environmentId) continue; + registry.update(atom as Atom.Writable, (pages) => { + if (pages === null) return pages; + const entries = editHeldPullRequestListEntries( + listing[1], + pages.entries, + () => [edit], + registry.get(pullRequestPendingEditsAtom(target.environmentId)), + ); + return entries === pages.entries ? pages : { ...pages, entries }; + }); + } + }), // A new head can change which files GitHub still counts as viewed, so the viewed-files read is // refreshed where the new head lands. The detail atom still holds the previous answer here. detail: (target, value, registry) => diff --git a/apps/mobile/src/state/queries.debounce.test.tsx b/apps/mobile/src/state/queries.debounce.test.tsx index e2eccb4d4125c..8e67b617b6ee2 100644 --- a/apps/mobile/src/state/queries.debounce.test.tsx +++ b/apps/mobile/src/state/queries.debounce.test.tsx @@ -8,7 +8,8 @@ import { expect, it, vi } from "vite-plus/test"; const mocks = vi.hoisted(() => ({ sessionAtom: {}, - searchEntries: vi.fn(() => null), + searchEntriesAtom: {}, + searchEntries: vi.fn((): unknown => mocks.searchEntriesAtom), listRefs: vi.fn(), refresh: vi.fn(), })); @@ -35,7 +36,9 @@ vi.mock("./query", () => ({ data: atom === mocks.sessionAtom ? { authenticated: true, scopes: [AuthFilesystemReadScope] } - : null, + : atom === mocks.searchEntriesAtom + ? { entries: [{ path: "src/only-in-original.ts", kind: "file" }] } + : null, error: null, isPending: false, refresh: () => {}, @@ -53,12 +56,16 @@ function setup() { vi.useFakeTimers(); mocks.searchEntries.mockClear(); const root = createRoot(document.createElement("div")); + let latest: ReturnType | null = null; function Probe({ target }: { readonly target: ComposerPathSearchTarget }) { - useComposerPathSearch(target); + latest = useComposerPathSearch(target); return null; } return { + get result() { + return latest; + }, render(target: ComposerPathSearchTarget) { act(() => root.render()); }, @@ -128,6 +135,21 @@ it.each([ }); }); +it.each([ + { field: "environment", changed: { ...target, environmentId: EnvironmentId.make("other") } }, + { field: "project directory", changed: { ...target, cwd: "/other" } }, +])("reports settled results as pending when the $field changes", ({ changed }) => { + using harness = setup(); + harness.render(target); + act(() => vi.advanceTimersByTime(200)); + expect(harness.result?.isPending).toBe(false); + expect(harness.result?.entries).toHaveLength(1); + + harness.render(changed); + expect(harness.result?.isPending).toBe(true); + expect(harness.result?.entries).toEqual([]); +}); + it("keeps compiled branch results stable and resets pagination for a new target or refresh", () => { (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; const registry = AtomRegistry.make(); diff --git a/apps/mobile/src/state/queries.test.ts b/apps/mobile/src/state/queries.test.ts deleted file mode 100644 index dba11b2e56ea7..0000000000000 --- a/apps/mobile/src/state/queries.test.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { threadPullRequestKeyOf } from "@t3tools/client-runtime/thread-pull-requests"; -import { filterComposerPullRequestMatches } from "@t3tools/shared/composerPullRequestMatches"; -import { expect, it } from "@effect/vitest"; - -it("keeps an older exact PR in the mobile menu ahead of twenty newer substring matches", () => { - const exact = { - host: "github.com", - number: 42, - projectId: "project", - repository: "example/repo", - updatedAt: "2025-01-01", - }; - const recent = Array.from({ length: 25 }, (_, index) => ({ - ...exact, - number: 4200 + index, - updatedAt: "2026-01-01", - })); - const matches = filterComposerPullRequestMatches({ - keyOf: threadPullRequestKeyOf, - entries: [exact, ...recent, exact], - projectId: exact.projectId, - repository: exact.repository, - query: "42", - limit: 20, - }); - expect(matches).toHaveLength(20); - expect(matches[0]).toEqual(exact); - expect(matches.filter((entry) => entry.number === 42)).toHaveLength(1); -}); diff --git a/apps/mobile/src/state/queries.ts b/apps/mobile/src/state/queries.ts index 38890d7d57506..a84059d2e3606 100644 --- a/apps/mobile/src/state/queries.ts +++ b/apps/mobile/src/state/queries.ts @@ -440,9 +440,13 @@ export function useComposerPathSearch(target: ComposerPathSearchTarget) { canReadFiles && searchTarget !== null ? projectEnvironment.searchEntries(searchTarget) : null, ); const hasTarget = searchTarget !== null; + const isSameCheckout = + normalizedTarget.environmentId === debouncedTarget.environmentId && + normalizedTarget.cwd === debouncedTarget.cwd; return { - entries: result.data?.entries ?? [], + // Entries from another checkout must not be offered while the new one is debounced. + entries: isSameCheckout ? (result.data?.entries ?? []) : [], error: !hasTarget || fileAccess.isPending ? null @@ -450,6 +454,7 @@ export function useComposerPathSearch(target: ComposerPathSearchTarget) { ? result.error : (fileAccess.error ?? "This connection cannot search host files."), isPending: + !isSameCheckout || normalizedTarget.query !== debouncedTarget.query || (hasTarget && (fileAccess.isPending || result.isPending)), refresh: result.refresh, diff --git a/apps/mobile/src/state/recover-failed-thread-draft.ts b/apps/mobile/src/state/recover-failed-thread-draft.ts index 5512dccbeb266..25573ee3363c7 100644 --- a/apps/mobile/src/state/recover-failed-thread-draft.ts +++ b/apps/mobile/src/state/recover-failed-thread-draft.ts @@ -26,7 +26,7 @@ export async function recoverFailedThreadDraft(message: QueuedThreadMessage): Pr appendComposerDraftAttachments( targetKey, source.attachments.filter((attachment) => !existingIds.has(attachment.id)), - { allowOverflow: true }, + { restore: true }, ); // Recovery may exceed caps; preserve every file and leave trimming to the editor. await flushComposerDrafts(); diff --git a/apps/mobile/src/state/session.ts b/apps/mobile/src/state/session.ts index 5d6a441e9b9e4..74876954b2716 100644 --- a/apps/mobile/src/state/session.ts +++ b/apps/mobile/src/state/session.ts @@ -1,5 +1,8 @@ import { useAtomValue } from "@effect/atom-react"; -import { createEnvironmentSessionAtoms } from "@t3tools/client-runtime/state/session"; +import { + createEnvironmentSessionAtoms, + sessionDeniesScope, +} from "@t3tools/client-runtime/state/session"; import { type AuthEnvironmentScope, type AuthSessionState, @@ -25,16 +28,30 @@ export function sessionHasScope( return result._tag !== "Failure" && session !== null && sessionGrantsScope(session, scope); } +function sessionStateAtomFor(environmentId: EnvironmentId | null) { + return environmentId === null + ? EMPTY_SESSION_STATE_ATOM + : environmentSession.sessionStateAtom(environmentId); +} + +// Both hooks select a boolean: every revalidation emits a new result, and most consumers are +// list rows. export function useEnvironmentScope( environmentId: EnvironmentId | null, scope: AuthEnvironmentScope, ): boolean { - // Select the grant: every revalidation emits a new result, and most consumers are list rows. - return useAtomValue( - environmentId === null - ? EMPTY_SESSION_STATE_ATOM - : environmentSession.sessionStateAtom(environmentId), - (result) => sessionHasScope(result, scope), + return useAtomValue(sessionStateAtomFor(environmentId), (result) => + sessionHasScope(result, scope), + ); +} + +/** Unlike `!useEnvironmentScope`, stays false until the host has answered without the scope. */ +export function useEnvironmentScopeDenied( + environmentId: EnvironmentId | null, + scope: AuthEnvironmentScope, +): boolean { + return useAtomValue(sessionStateAtomFor(environmentId), (result) => + sessionDeniesScope(result, scope), ); } diff --git a/apps/mobile/src/state/thread-pr-presentation.ts b/apps/mobile/src/state/thread-pr-presentation.ts index a44f410b54ca3..a57d04fb24918 100644 --- a/apps/mobile/src/state/thread-pr-presentation.ts +++ b/apps/mobile/src/state/thread-pr-presentation.ts @@ -36,7 +36,7 @@ export interface ThreadPrPresentation { const PR_STATE_TEXT_CLASS: Record = { open: "text-adaptive-emerald-600-400", merged: "text-adaptive-violet-600-400", - closed: "text-foreground-muted", + closed: "text-adaptive-rose-600-400", }; export function presentThreadPr( @@ -101,12 +101,7 @@ export function presentThreadLinkedPullRequests( : linkedCount !== null ? `${linkedCount} linked ${changeRequest.plural}, overall ${badge.state}` : `#${link.number} ${changeRequest.singular} ${state === null ? "status pending" : isDraft ? "draft" : state}`, - textClassName: - state === null || isDraft - ? "text-foreground-muted" - : isMultiple && state === "closed" - ? "text-adaptive-rose-600-400" - : PR_STATE_TEXT_CLASS[state], + textClassName: state === null || isDraft ? "text-foreground-muted" : PR_STATE_TEXT_CLASS[state], }; } diff --git a/apps/mobile/src/state/threadOutboxDelivery.ts b/apps/mobile/src/state/threadOutboxDelivery.ts index ed9c33719a4d8..b3feeae3bb801 100644 --- a/apps/mobile/src/state/threadOutboxDelivery.ts +++ b/apps/mobile/src/state/threadOutboxDelivery.ts @@ -372,7 +372,7 @@ export async function recoverEditedCreationAfterDelivery( appendComposerDraftAttachments( draftKey, kept.attachments.filter((attachment) => !existingAttachmentIds.has(attachment.id)), - { allowOverflow: true }, + { restore: true }, ); updateComposerDraftSettings(draftKey, { ...(kept.modelSelection !== undefined ? { modelSelection: kept.modelSelection } : {}), diff --git a/apps/mobile/src/state/use-composer-attachment-actions.ts b/apps/mobile/src/state/use-composer-attachment-actions.ts index eeb8742010858..2352f3f7848eb 100644 --- a/apps/mobile/src/state/use-composer-attachment-actions.ts +++ b/apps/mobile/src/state/use-composer-attachment-actions.ts @@ -101,10 +101,7 @@ export function useComposerAttachmentActions(props: { const insertion = captureComposerDraftInsertion(key); await withDraftOperation(key, async (signal) => { try { - const result = await convertPastedImagesToAttachments({ - uris, - existingCount: countComposerDraftAttachmentsAfterSelection(key, insertion), - }); + const result = await convertPastedImagesToAttachments({ uris }); report( "Could not paste images", result.error, diff --git a/apps/mobile/src/state/use-composer-drafts.test.ts b/apps/mobile/src/state/use-composer-drafts.test.ts index f25037f40f0b4..200b2b165c6b5 100644 --- a/apps/mobile/src/state/use-composer-drafts.test.ts +++ b/apps/mobile/src/state/use-composer-drafts.test.ts @@ -28,6 +28,9 @@ import { } from "@t3tools/contracts"; import { onTestFinished, vi } from "vite-plus/test"; +import { buildIncomingShareDraft } from "../features/sharing/incoming-share-model"; +import { composerContextSendBlockReason } from "../lib/composerContext"; + const composerDraftFileMocks = vi.hoisted(() => { let document = JSON.stringify({ schemaVersion: 1, drafts: {} }); let readError: Error | null = null; @@ -458,7 +461,7 @@ describe("mobile composer drafts", () => { const restored = history.restore(text, getComposerDraftSnapshot(key)); setComposerDraftText(key, text); setComposerDraftContext(key, restored.context); - appendComposerDraftAttachments(key, restored.attachments, { allowOverflow: true }); + appendComposerDraftAttachments(key, restored.attachments, { restore: true }); }; try { changeText(""); @@ -802,6 +805,32 @@ describe("mobile composer drafts", () => { expect(getComposerDraftSnapshot(key).text).toBe("keep newly typed text pasted"); }); + it("keeps a pending paste before text typed at its caret, and the caret after that text", async () => { + const key = "environment-1:typing-at-paste-point"; + const image = { + id: "image", + type: "file" as const, + name: "picture.png", + mimeType: "text/plain", + sizeBytes: 4, + fileUri: "file:///picture.png", + }; + setComposerDraftText(key, "Before "); + const insertion = captureComposerDraftInsertion(key, { start: 7, end: 7 }); + const write = Promise.withResolvers(); + const pending = write.promise.then(() => + appendComposerDraftAttachments(key, [image], { appendReference: true, insertion }), + ); + setComposerDraftText(key, "Before yaz"); + rememberComposerDraftSelection(key, "Before yaz", { start: 10, end: 10 }); + write.resolve(); + expect(await pending).toBe(0); + const link = "[picture.png](t3-context://v1/file/image)"; + expect(getComposerDraftSnapshot(key).text).toBe(`Before ${link} yaz`); + const caret = `Before ${link} yaz`.length; + expect(captureComposerDraftInsertion(key)).toMatchObject({ start: caret, end: caret }); + }); + it.each(["attachment", "context", "imported context"])( "releases a selected file when replaced by %s", async (kind) => { @@ -1054,12 +1083,65 @@ describe("mobile composer drafts", () => { const overflowRejected = appendComposerDraftAttachments( draftKey, [makeAttachment("restored-1")], - { allowOverflow: true }, + { restore: true }, ); expect(overflowRejected).toBe(0); expect(appAtomRegistry.get(composerDraftsAtom)[draftKey]?.attachments).toHaveLength(101); }); + it("drops duplicate attachments, releases their files, and keeps them on restore", async () => { + const outboxLoad = vi.spyOn(threadOutboxManager, "load").mockResolvedValue(true); + onTestFinished(() => outboxLoad.mockRestore()); + const cleanup = Promise.withResolvers(); + composerAttachmentCleanupMocks.remove.mockImplementationOnce(async () => { + cleanup.resolve(); + }); + const makeVideo = (id: string) => ({ + id, + type: "file" as const, + name: "clip.mov", + mimeType: "video/quicktime", + sizeBytes: 42, + fileUri: `file:///documents/t3-composer-attachments/${id}.mov`, + }); + const draftKey = "new-task:environment-1:project-duplicates"; + appAtomRegistry.set(composerDraftsAtom, { + [draftKey]: { text: "", attachments: [makeVideo("held")] }, + }); + + expect( + appendComposerDraftAttachments(draftKey, [makeVideo("again"), makeVideo("again-2")]), + ).toBe(0); + expect(appAtomRegistry.get(composerDraftsAtom)[draftKey]?.attachments).toEqual([ + makeVideo("held"), + ]); + await cleanup.promise; + expect(composerAttachmentCleanupMocks.remove).toHaveBeenCalledWith(makeVideo("again").fileUri); + + appendComposerDraftAttachments(draftKey, [makeVideo("restored")], { restore: true }); + expect(appAtomRegistry.get(composerDraftsAtom)[draftKey]?.attachments).toHaveLength(2); + + // Every native paste is named pasted-image.png, so only the bytes tell two pastes apart. + const makePaste = (id: string, base64: string) => ({ + id, + type: "image" as const, + name: "pasted-image.png", + mimeType: "image/png", + sizeBytes: 3, + dataUrl: `data:image/png;base64,${base64}`, + previewUri: `data:image/png;base64,${base64}`, + }); + const pasteKey = "new-task:environment-1:project-pastes"; + appendComposerDraftAttachments(pasteKey, [makePaste("red", "AQID")]); + appendComposerDraftAttachments(pasteKey, [ + makePaste("blue", "BAUG"), + makePaste("red-2", "AQID"), + ]); + expect( + appAtomRegistry.get(composerDraftsAtom)[pasteKey]?.attachments.map(({ id }) => id), + ).toEqual(["red", "blue"]); + }); + it("keeps shared attachment files until every draft releases them", async () => { const outboxLoad = vi.spyOn(threadOutboxManager, "load").mockResolvedValue(true); onTestFinished(() => outboxLoad.mockRestore()); @@ -2203,7 +2285,7 @@ describe("mobile composer drafts", () => { it("merges shared content into a project draft without duplicating retries", () => { const draftKey = "new-task:environment-1:project-1"; const sharedAttachment = { - id: "share-1:image:0", + id: "share-1-image-0", type: "image" as const, name: "Screenshot.png", mimeType: "image/png", @@ -2238,7 +2320,7 @@ describe("mobile composer drafts", () => { it("shows a shared non-image file as a composer chip", () => { const draftKey = "new-task:environment-1:project-1"; const sharedFile = { - id: "share-1:file:0", + id: "share-1-file-0", type: "file" as const, name: "backup.json", mimeType: "application/json", @@ -2273,6 +2355,37 @@ describe("mobile composer drafts", () => { expect(mergeComposerDraftContentState(once, draftKey, recovered)).toBe(once); }); + it("lets a shared file through the send check", async () => { + const { attachments } = await buildIncomingShareDraft({ + id: "share-1", + createdAt: "2026-07-15T10:00:00.000Z", + payloads: [ + { + shareType: "file", + value: "file:///shared/report.xlsx", + mimeType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + originalName: "report.xlsx", + }, + ], + resolvedPayloads: [], + fileReader: { + readBase64: async () => "", + readSize: async () => 42, + removeOwnedFile: async () => {}, + }, + }); + const draftKey = "new-task:environment-1:project-1"; + + const merged = mergeComposerDraftContentState({}, draftKey, { + text: "", + attachments, + sourceShareId: "share-1", + })[draftKey]!; + + expect(merged.context?.records).toHaveLength(1); + expect(composerContextSendBlockReason(merged.context)).toBeNull(); + }); + it("preserves existing images when shared content exceeds the draft attachment limit", () => { const draftKey = "new-task:environment-1:project-1"; const image = (id: string) => ({ diff --git a/apps/mobile/src/state/use-composer-drafts.ts b/apps/mobile/src/state/use-composer-drafts.ts index 7fb44be817f32..0306d5e7db5ea 100644 --- a/apps/mobile/src/state/use-composer-drafts.ts +++ b/apps/mobile/src/state/use-composer-drafts.ts @@ -195,14 +195,54 @@ export function captureComposerDraftInsertion( }; } +function mapCapturedRange( + captured: ComposerDraftInsertion, + text: string, +): { start: number; end: number } | null { + if (captured.text === text) return captured; + // A replaced selection is not safe to delete after edits; only a caret follows them. + if (captured.start !== captured.end) return null; + const previous = captured.text; + const shared = Math.min(previous.length, text.length); + let prefix = 0; + while (prefix < shared && previous[prefix] === text[prefix]) prefix += 1; + let suffix = 0; + while ( + suffix < shared - prefix && + previous[previous.length - 1 - suffix] === text[text.length - 1 - suffix] + ) { + suffix += 1; + } + // Text typed at the caret lands after it, so the insertion stays before that text. + if (captured.start <= prefix) return captured; + if (captured.start < previous.length - suffix) return null; + const caret = captured.start + text.length - previous.length; + return { start: caret, end: caret }; +} + +/** Keep a caret after the insertion where it was, or the next keystroke lands before typed text. */ +function caretAfterInsertion( + draftKey: string, + previousText: string, + range: { start: number; end: number }, + inserted: number, +): { start: number; end: number } { + const caret = readComposerDraftSelection(draftKey, previousText); + if (caret && caret.start > range.end) { + const delta = range.start + inserted - range.end; + return { start: caret.start + delta, end: caret.end + delta }; + } + return { start: range.start + inserted, end: range.start + inserted }; +} + function contextInsertionRange( draftKey: string, draft: ComposerDraft, target?: ComposerDraftInsertion, ) { const captured = target ?? appAtomRegistry.get(composerDraftSelectionAtom(draftKey)); - // Append after text edits during writes; caret-only moves preserve the captured insertion range. - const selection = captured?.text === draft.text ? captured : null; + // Follow edits made during writes; append when an edit touched the captured range. + const selection = captured ? mapCapturedRange(captured, draft.text) : null; const start = Math.max(0, Math.min(selection?.start ?? draft.text.length, draft.text.length)); return { start, end: Math.max(start, Math.min(selection?.end ?? start, draft.text.length)) }; } @@ -373,10 +413,11 @@ function draftWithInsertedContext( for (const record of content.context.records) records.set(record.contextId, record); const context = referencedComposerContext(text, { version: 1, records: [...records.values()] }); if ((context?.records.length ?? 0) > COMPOSER_CONTEXT_MAX_RECORDS) return null; - rememberComposerDraftSelection(draftKey, text, { - start: start + insertion.length, - end: start + insertion.length, - }); + rememberComposerDraftSelection( + draftKey, + text, + caretAfterInsertion(draftKey, draft.text, { start, end }, insertion.length), + ); return withReferencedContextFiles(draft, text, context); } @@ -1353,11 +1394,9 @@ export function insertComposerDraftText( const draft = getComposerDraftSnapshot(draftKey); const { start, end } = contextInsertionRange(draftKey, draft, target); const text = draft.text.slice(0, start) + value + draft.text.slice(end); + const caret = caretAfterInsertion(draftKey, draft.text, { start, end }, value.length); setComposerDraftText(draftKey, text); - rememberComposerDraftSelection(draftKey, text, { - start: start + value.length, - end: start + value.length, - }); + rememberComposerDraftSelection(draftKey, text, caret); } export function appendComposerDraftText(draftKey: string, value: string): void { @@ -1373,28 +1412,51 @@ export function appendComposerDraftText(draftKey: string, value: string): void { }); } -/** Restore with allowOverflow to preserve every attachment after failed sends. */ +// Pasted images all share one name, so an image's inline bytes decide whether it is a repeat. +function isSameAttachment(a: DraftComposerAttachment, b: DraftComposerAttachment): boolean { + return ( + a.mimeType === b.mimeType && + a.sizeBytes === b.sizeBytes && + a.name === b.name && + (a.type !== "image" || b.type !== "image" || a.dataUrl === b.dataUrl) + ); +} + +/** + * Restore puts back attachments after failed sends, so it skips the duplicate check and the caps. + * Returns how many attachments the caps rejected. + */ export function appendComposerDraftAttachments( draftKey: string, - attachments: ReadonlyArray, + incoming: ReadonlyArray, options?: { - readonly allowOverflow?: boolean; + readonly restore?: boolean; readonly appendReference?: boolean; readonly insertion?: ComposerDraftInsertion; readonly maxAttachments?: number; }, ): number { - if (attachments.length === 0) { + if (incoming.length === 0) { return 0; } let rejected: ReadonlyArray = []; let removed: ReadonlyArray = []; + let duplicates: ReadonlyArray = []; updateComposerDrafts((current) => { const existing = normalizeDraft(current[draftKey]); const retained = options?.appendReference ? draftWithoutInsertionSelection(draftKey, existing, options.insertion) : existing; - const remaining = options?.allowOverflow + const seen = [...retained.attachments]; + const attachments = options?.restore + ? incoming + : incoming.filter((attachment) => { + if (seen.some((other) => isSameAttachment(other, attachment))) return false; + seen.push(attachment); + return true; + }); + duplicates = incoming.filter((attachment) => !attachments.includes(attachment)); + const remaining = options?.restore ? attachments.length : Math.max( 0, @@ -1437,7 +1499,7 @@ export function appendComposerDraftAttachments( [draftKey]: draft, }; }); - scheduleUnusedComposerAttachmentCleanup([...rejected, ...removed]); + scheduleUnusedComposerAttachmentCleanup([...rejected, ...removed, ...duplicates]); return rejected.length; } diff --git a/apps/mobile/src/state/use-remote-environment-registry.ts b/apps/mobile/src/state/use-remote-environment-registry.ts index a1488220d6952..0b4163aafbd95 100644 --- a/apps/mobile/src/state/use-remote-environment-registry.ts +++ b/apps/mobile/src/state/use-remote-environment-registry.ts @@ -9,6 +9,7 @@ import { runAtomCommand } from "@t3tools/client-runtime/state/runtime"; import { hasCloudPublicConfig } from "../features/cloud/publicConfig"; import { useConnectionController } from "../features/connection/useConnectionController"; +import { useRetryEnvironment } from "../features/connection/useRetryEnvironment"; import { environmentPresentations } from "./presentation"; import { useWorkspaceConnectionState, useWorkspaceEnvironments } from "./workspace"; import type { SavedRemoteConnection } from "../lib/connection"; @@ -166,8 +167,7 @@ export function useRemoteConnections() { const onConnectPress = (pairingUrl?: string, expectedEnvironmentId?: EnvironmentId) => pairEnvironment(pairingUrl ?? connectionPairingUrl, expectedEnvironmentId); - const onReconnectEnvironment = (environmentId: EnvironmentId) => - controller.retryEnvironment(environmentId); + const onReconnectEnvironment = useRetryEnvironment(); const onSetEnvironmentEnabled = (environmentId: EnvironmentId, enabled: boolean) => controller.setEnvironmentEnabled(environmentId, enabled); const onUpdateEnvironment = ( diff --git a/apps/mobile/src/state/use-thread-composer-state.ts b/apps/mobile/src/state/use-thread-composer-state.ts index a3a191d54cbd1..3eb19861d99b4 100644 --- a/apps/mobile/src/state/use-thread-composer-state.ts +++ b/apps/mobile/src/state/use-thread-composer-state.ts @@ -832,7 +832,7 @@ export function useThreadComposerState(input: { context: draft.context, attachments: [], }); - appendComposerDraftAttachments(threadKey, attachments, { allowOverflow: true }); + appendComposerDraftAttachments(threadKey, attachments, { restore: true }); if (freshContext) setFreshSessionPending(threadKey, true); setThreadComposerError( threadKey, diff --git a/apps/mobile/vite.config.ts b/apps/mobile/vite.config.ts index 2b688fee30ad2..e9f1260b514c3 100644 --- a/apps/mobile/vite.config.ts +++ b/apps/mobile/vite.config.ts @@ -26,6 +26,7 @@ const reactTests = [ "src/features/files/ZipArchiveView.test.tsx", "src/features/pull-requests/guide/useReviewGuide.test.tsx", "src/features/settings/SettingsServerControlsRouteScreen.test.tsx", + "src/features/settings/SettingsScheduledTasksRouteScreen.test.tsx", "src/features/settings/components/BranchNamingSettings.test.tsx", "src/features/settings/components/SettingsDragHandle.test.tsx", "src/features/threads/ThreadMarkdownImage.test.tsx", @@ -36,16 +37,20 @@ const reactTests = [ "src/native/NativeHeaderToolbar.test.tsx", "src/components/ComposerEditor.ios.test.tsx", "src/native/V5StackHeader.ios.test.tsx", + "src/native/createV5StackNavigator.ios.test.tsx", "src/native/useRetryingImageSource.test.tsx", "src/state/queries.debounce.test.tsx", "src/state/use-selected-thread-git-state.test.tsx", "src/features/terminal/useTerminalExitNavigation.test.tsx", "src/features/threads/use-thread-list-now.test.tsx", + "src/features/threads/use-composer-command-menu.select.test.tsx", "src/features/threads/useThreadListV2.test.tsx", "src/features/home/home-list-options.test.tsx", "src/features/home/home-route-visibility.test.tsx", "src/features/layout/workspace/useSearchScrollRestoration.test.tsx", + "src/features/connection/useRetryEnvironment.test.tsx", "src/features/connection/ConnectionsNewRouteScreen.test.tsx", + "src/features/connection/LinkedEnvironmentsSection.test.tsx", "src/features/devices/device-stream.browser.test.ts", "src/features/keyboard/keyboardControllerState.test.tsx", ]; diff --git a/apps/server/graphql.config.ts b/apps/server/graphql.config.ts index f042d039410db..cc659592f01a2 100644 --- a/apps/server/graphql.config.ts +++ b/apps/server/graphql.config.ts @@ -1,13 +1,9 @@ import { defineConfig } from "@effect/graphql-generator/Config"; // `schema/expo.graphql` is api.expo.dev's schema, read by introspection -// (`vp run generate:expo-schema`). The EAS Simulator operations are the ones +// (`vp run generate:expo-schema`). The Expo Simulators operations are the ones // eas-cli's `simulator:*` commands send; Expo does not document them. export default defineConfig({ schema: "./schema/expo.graphql", documents: ["src/device/**/*.graphql"], - scalars: { - // Feature gates arrive as an object of gate name to boolean. - JSONObject: "effect/Schema#Json", - }, }); diff --git a/apps/server/package.json b/apps/server/package.json index d16b8d4e19f2f..9e05831bee12e 100644 --- a/apps/server/package.json +++ b/apps/server/package.json @@ -37,7 +37,7 @@ }, "dependencies": { "@anthropic-ai/claude-agent-sdk": "^0.3.321", - "@cursor/sdk": "1.2.0", + "@cursor/sdk": "1.3.1", "@effect/platform-node": "catalog:", "@effect/platform-node-shared": "catalog:", "@ff-labs/fff-node": "0.11.0", diff --git a/apps/server/schema/expo.graphql.ts b/apps/server/schema/expo.graphql.ts index 4391c22403ec5..de836ebcb615a 100644 --- a/apps/server/schema/expo.graphql.ts +++ b/apps/server/schema/expo.graphql.ts @@ -5,9 +5,6 @@ export const Int = Schema.Number.check(Schema.isInt32()) export const ID = Schema.String -/** The `JSONObject` scalar type represents JSON objects as specified by [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf). */ -export const JSONObject = Schema.Json - /** * - `ANDROID` * - `IOS` diff --git a/apps/server/scripts/acp-mock-agent.ts b/apps/server/scripts/acp-mock-agent.ts index a97aab9cdf2b1..78373d82e1487 100644 --- a/apps/server/scripts/acp-mock-agent.ts +++ b/apps/server/scripts/acp-mock-agent.ts @@ -70,6 +70,9 @@ const failLoadSession = process.env.T3_ACP_FAIL_LOAD_SESSION === "1"; const failLoadSessionAfterConfigReplay = process.env.T3_ACP_FAIL_LOAD_SESSION_AFTER_CONFIG_REPLAY === "1"; const emitLoadReplay = process.env.T3_ACP_EMIT_LOAD_REPLAY === "1"; +const loadReplyDelayMs = Number(process.env.T3_ACP_LOAD_REPLY_DELAY_MS ?? "0"); +const loadNeverReplies = process.env.T3_ACP_LOAD_NEVER_REPLIES === "1"; +const rejectPromptUntilLoaded = process.env.T3_ACP_REJECT_PROMPT_UNTIL_LOADED === "1"; const failPrompt = process.env.T3_ACP_FAIL_PROMPT === "1"; // JSON for the `data` of an "Internal error" response to `session/new`, as // Antigravity sends when its runtime cannot reach its local harness. @@ -122,6 +125,7 @@ let currentAgent = ""; let authenticated = !requiresAuthentication; let promptCount = 0; const cancelledSessions = new Set(); +const sessionsLoading = new Set(); let configuredProvider: AcpSchema.ProviderCurrentConfig | null = null; function promptIdFromRequestMeta( @@ -687,6 +691,7 @@ const program = Effect.gen(function* () { yield* agent.handleLoadSession((request) => Effect.gen(function* () { const requestedSessionId = String(request.sessionId ?? sessionId); + sessionsLoading.add(requestedSessionId); if (loadedCommandNames !== undefined) { yield* agent.client.sessionUpdate({ sessionId: requestedSessionId, @@ -729,6 +734,13 @@ const program = Effect.gen(function* () { content: { type: "text", text: "replay" }, }, }); + if (loadNeverReplies) { + return yield* Effect.never; + } + if (Number.isFinite(loadReplyDelayMs) && loadReplyDelayMs > 0) { + yield* Effect.sleep(`${loadReplyDelayMs} millis`); + } + sessionsLoading.delete(requestedSessionId); return { configOptions: configOptions(), }; @@ -868,6 +880,12 @@ const program = Effect.gen(function* () { yield* agent.handlePrompt((request) => Effect.gen(function* () { const requestedSessionId = String(request.sessionId ?? sessionId); + if (rejectPromptUntilLoaded && sessionsLoading.has(requestedSessionId)) { + return yield* new AcpError.AcpRequestError({ + code: -32002, + errorMessage: `Resource not found: ${requestedSessionId}`, + }); + } beginAcpMockPrompt(cancelledSessions, requestedSessionId); promptCount += 1; if (idleError === "grok-background" && promptCount === 1) { diff --git a/apps/server/scripts/dev-replay-agent.ts b/apps/server/scripts/dev-replay-agent.ts index b6eea893ad6b7..2aa50e353f57a 100644 --- a/apps/server/scripts/dev-replay-agent.ts +++ b/apps/server/scripts/dev-replay-agent.ts @@ -64,6 +64,11 @@ export const SCENARIOS = [ name: "Multi turn", file: `${FIXTURES}/multi_turn/registry_transcript.ndjson`, }, + { + id: "command-output", + name: "Command output", + file: "./dev-replay/command_output_transcript.ndjson", + }, ] as const; // Session setup and vendor state stay out: they would replace the replay agent's own models. diff --git a/apps/server/scripts/dev-replay/command_output_transcript.ndjson b/apps/server/scripts/dev-replay/command_output_transcript.ndjson new file mode 100644 index 0000000000000..0bda8c67adbf5 --- /dev/null +++ b/apps/server/scripts/dev-replay/command_output_transcript.ndjson @@ -0,0 +1,6 @@ +{"type":"transcript_start","provider":"acpRegistry","protocol":"acp.ndjson-jsonrpc","version":"1","scenario":"dev_replay_command_output","metadata":{"generatedBy":"hand-written for the dev replay agent"}} +{"type":"expect_outbound","frame":{"kind":"request","method":"session/prompt","params":""}} +{"type":"emit_inbound","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"dev-replay-session","update":{"sessionUpdate":"tool_call","toolCallId":"command-output-json","kind":"execute","title":"node -e 'process.stdout.write(JSON.stringify({stdout:\"line 1\\n行 2 🚀\",stderr:\"é warning\",interrupted:false,requestId:\"keep-me\"})+\"\\n\")'","status":"in_progress","rawInput":{"command":"node -e 'process.stdout.write(JSON.stringify({stdout:\"line 1\\n行 2 🚀\",stderr:\"é warning\",interrupted:false,requestId:\"keep-me\"})+\"\\n\")'"}}}}} +{"type":"emit_inbound","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"dev-replay-session","update":{"sessionUpdate":"tool_call_update","toolCallId":"command-output-json","status":"completed","rawOutput":"{\"stdout\":\"line 1\\n行 2 🚀\",\"stderr\":\"é warning\",\"interrupted\":false,\"requestId\":\"keep-me\"}\n"}}},"afterMs":300} +{"type":"emit_inbound","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"dev-replay-session","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"The command printed one JSON line."}}}}} +{"type":"emit_inbound","frame":{"kind":"response","method":"session/prompt","result":{"stopReason":"end_turn"}}} diff --git a/apps/server/src/assets/AssetAccess.test.ts b/apps/server/src/assets/AssetAccess.test.ts index 582e828842ede..d137305317327 100644 --- a/apps/server/src/assets/AssetAccess.test.ts +++ b/apps/server/src/assets/AssetAccess.test.ts @@ -525,50 +525,53 @@ describe("AssetAccess", () => { }).pipe(Effect.provide(layerTest)), ); - it.effect("resolves home-relative media paths independently of the workspace", () => - Effect.gen(function* () { - const fs = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-media-home-" }); - const home = path.join(directory, "var", "home", "alice"); - const filePath = path.join(home, "Downloads", "repro.mp4"); - yield* fs.makeDirectory(path.dirname(filePath), { recursive: true }); - yield* fs.writeFileString(filePath, "recording bytes"); - const canonicalFile = yield* fs.realPath(filePath); - yield* Effect.gen(function* () { - for (const workspaceRoot of [ - path.join(home, "project"), - path.join(directory, "srv", "project"), - undefined, - ]) { - if (workspaceRoot) yield* fs.makeDirectory(workspaceRoot, { recursive: true }); - for (const requestedPath of ["~/Downloads/repro.mp4", "~\\Downloads/repro.mp4"]) { - const result = yield* issueAssetUrl({ - resource: { - _tag: "media-file", - threadId: ThreadId.make("thread-1"), - path: requestedPath, - }, - ...(workspaceRoot ? { workspaceRoot } : {}), - }); - const suffix = result.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length); - const separator = suffix.indexOf("/"); - const asset = yield* resolveAsset( - suffix.slice(0, separator), - suffix.slice(separator + 1), - ); - expect(asset).toMatchObject({ - kind: "file", - path: canonicalFile, - mimeType: "video/mp4", - }); - if (asset?.kind !== "file") throw new Error("Expected a resolved home media file"); - const response = HttpServerResponse.toWeb(yield* assetFileResponse(asset)); - expect(yield* Effect.promise(() => response.text())).toBe("recording bytes"); + it.effect.each(["media-file", "draft-workspace-file"] as const)( + "resolves home-relative %s paths independently of the workspace", + (tag) => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-media-home-" }); + const home = path.join(directory, "var", "home", "alice"); + const filePath = path.join(home, "Downloads", "repro.mp4"); + yield* fs.makeDirectory(path.dirname(filePath), { recursive: true }); + yield* fs.writeFileString(filePath, "recording bytes"); + const canonicalFile = yield* fs.realPath(filePath); + yield* Effect.gen(function* () { + for (const workspaceRoot of [ + path.join(home, "project"), + path.join(directory, "srv", "project"), + undefined, + ]) { + if (workspaceRoot) yield* fs.makeDirectory(workspaceRoot, { recursive: true }); + for (const requestedPath of ["~/Downloads/repro.mp4", "~\\Downloads/repro.mp4"]) { + const result = yield* issueAssetUrl({ + resource: { + ...(tag === "media-file" + ? { _tag: tag, threadId: ThreadId.make("thread-1") } + : { _tag: tag, cwd: workspaceRoot ?? directory }), + path: requestedPath, + }, + ...(workspaceRoot ? { workspaceRoot } : {}), + }); + const suffix = result.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length); + const separator = suffix.indexOf("/"); + const asset = yield* resolveAsset( + suffix.slice(0, separator), + suffix.slice(separator + 1), + ); + expect(asset).toMatchObject({ + kind: "file", + path: canonicalFile, + mimeType: "video/mp4", + }); + if (asset?.kind !== "file") throw new Error("Expected a resolved home media file"); + const response = HttpServerResponse.toWeb(yield* assetFileResponse(asset)); + expect(yield* Effect.promise(() => response.text())).toBe("recording bytes"); + } } - } - }).pipe(Effect.provideService(HostProcess.HomeDirectory, home)); - }).pipe(Effect.provide(layerTest)), + }).pipe(Effect.provideService(HostProcess.HomeDirectory, home)); + }).pipe(Effect.provide(layerTest)), ); it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")( @@ -1009,6 +1012,15 @@ describe("AssetAccess", () => { kind: "file", path: canonicalCssPath, }); + yield* fileSystem.makeDirectory(path.join(root, "clips")); + for (const mediaName of ["clips/clip.mp4", "clips/clip.webm", "clips/clip.mp3"]) { + const mediaPath = path.join(root, mediaName); + yield* fileSystem.writeFileString(mediaPath, "media bytes"); + expect(yield* resolveAsset(token, mediaName)).toMatchObject({ + kind: "file", + path: yield* fileSystem.realPath(mediaPath), + }); + } for (const extension of ["otf", "ttf", "woff", "woff2"]) { const fontName = `preview.${extension}`; const fontPath = path.join(root, fontName); diff --git a/apps/server/src/assets/AssetAccess.ts b/apps/server/src/assets/AssetAccess.ts index c841f8eecff2e..5674372bee1a9 100644 --- a/apps/server/src/assets/AssetAccess.ts +++ b/apps/server/src/assets/AssetAccess.ts @@ -94,6 +94,18 @@ const PREVIEW_ASSET_EXTENSIONS = new Set([ ".png", ".svg", ".webp", + ".m4v", + ".mp4", + ".ogv", + ".webm", + ".aac", + ".flac", + ".m4a", + ".mp3", + ".oga", + ".ogg", + ".opus", + ".wav", ".css", ".js", ".mjs", @@ -607,9 +619,10 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i } case "draft-workspace-file": { const draftWorkspaceRoot = input.workspaceRoot ?? input.resource.cwd; - const hostPath = input.resource.path.startsWith("~/") - ? expandHomePath(input.resource.path, yield* HostProcess.HomeDirectory) - : yield* toWslDrivePath(input.resource.path); + const hostPath = + input.resource.path.startsWith("~/") || input.resource.path.startsWith("~\\") + ? expandHomePath(input.resource.path, yield* HostProcess.HomeDirectory) + : yield* toWslDrivePath(input.resource.path); if (path.isAbsolute(hostPath)) { const finalized = yield* finalizeAbsoluteMediaFileAsset({ requestedPath: hostPath, diff --git a/apps/server/src/assets/NativeAppIconResolver.test.ts b/apps/server/src/assets/NativeAppIconResolver.test.ts index ecfe08788af3b..66a1171c9053c 100644 --- a/apps/server/src/assets/NativeAppIconResolver.test.ts +++ b/apps/server/src/assets/NativeAppIconResolver.test.ts @@ -1,6 +1,8 @@ import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; import * as NodeServices from "@effect/platform-node/NodeServices"; import { expect, it } from "@effect/vitest"; +import * as Digest from "effect/crypto/Digest"; +import * as Hex from "effect/encoding/Hex"; import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; @@ -28,18 +30,34 @@ it.effect("resolves the exact bundle path without Spotlight and caches the conve commands.push(command.command); if (command.command === "/usr/bin/plutil") return "AppIcon"; if (command.command === "/usr/bin/sips") { + expect(command.args.slice(0, 3)).toEqual(["-z", "128", "128"]); yield* fs.writeFileString(String(command.args.at(-1)), "converted icon"); } return ""; }), }); yield* Effect.gen(function* () { + const config = yield* ServerConfig.ServerConfig; + const canonicalBundle = yield* fs.realPath(bundle); + const sourceIcon = path.join(canonicalBundle, "Contents", "Resources", "AppIcon.icns"); + const digest = yield* Digest.Digest; + const legacyKey = yield* digest + .digest( + Digest.sha256, + new TextEncoder().encode(`${canonicalBundle}\0AppIcon\0${sourceIcon}`), + ) + .pipe(Effect.map(Hex.encode), Effect.orDie); + const cacheDirectory = path.join(config.providerStatusCacheDir, "native-app-icons"); + yield* fs.makeDirectory(cacheDirectory, { recursive: true }); + const legacyIcon = path.join(cacheDirectory, `${legacyKey}.png`); + yield* fs.writeFileString(legacyIcon, "legacy 64px icon"); const resolver = yield* NativeAppIconResolver.make; const reference = { _tag: "app-path" as const, path: bundle }; const icon = yield* resolver.resolve(reference); expect(icon).not.toBeNull(); if (icon === null) return; expect(yield* fs.readFileString(icon)).toBe("converted icon"); + expect(icon).not.toBe(legacyIcon); expect(yield* resolver.resolve(reference)).toBe(icon); expect(yield* resolver.resolve({ _tag: "app-path", path: "~/Build Output/My App.app" })).toBe( icon, diff --git a/apps/server/src/assets/NativeAppIconResolver.ts b/apps/server/src/assets/NativeAppIconResolver.ts index 0d238effee4cc..e31d7a1d8c30d 100644 --- a/apps/server/src/assets/NativeAppIconResolver.ts +++ b/apps/server/src/assets/NativeAppIconResolver.ts @@ -21,7 +21,7 @@ import * as HostProcess from "@t3tools/shared/HostProcess"; import * as ServerConfig from "../config.ts"; -const ICON_SIZE = 64; +const ICON_SIZE = 128; const COMMAND_TIMEOUT = "5 seconds"; const RESOLUTION_CACHE_TTL = Duration.hours(1); const RESOLUTION_CACHE_MAX_ENTRIES = 256; @@ -189,7 +189,7 @@ const resolveNativeAppIconUncached = Effect.fn("NativeAppIconResolver.resolveUnc ) .pipe(Effect.map(Hex.encode), Effect.orDie); const cacheDirectory = path.join(config.providerStatusCacheDir, "native-app-icons"); - const cachePath = path.join(cacheDirectory, `${cacheKey}.png`); + const cachePath = path.join(cacheDirectory, `${cacheKey}-${ICON_SIZE}.png`); if (yield* existingFile(cachePath)) return cachePath; yield* fileSystem.makeDirectory(cacheDirectory, { recursive: true }); diff --git a/apps/server/src/auth/McpOAuth.test.ts b/apps/server/src/auth/McpOAuth.test.ts index 4cd276c51133b..d72a716673e66 100644 --- a/apps/server/src/auth/McpOAuth.test.ts +++ b/apps/server/src/auth/McpOAuth.test.ts @@ -229,6 +229,46 @@ it.effect("registers loopback and https clients and never redirects for an unver expect(response.status).toBe(400); expect(yield* json(response)).toMatchObject({ error: "invalid_redirect_uri" }); } + // JSON escaping can inflate the signed client_id past what an authorize URL can carry. + const oversized = yield* handler( + at("/oauth/mcp/register", { + method: "POST", + headers: { "content-type": "application/json" }, + body: encodeJson({ + redirect_uris: Array.from( + { length: 10 }, + (_, index) => `https://bot.example/${index}/${'"'.repeat(480)}`, + ), + }), + }), + ); + expect(oversized.status).toBe(400); + const fiveLongCallbacks = yield* handler( + at("/oauth/mcp/register", { + method: "POST", + headers: { "content-type": "application/json" }, + body: encodeJson({ + client_name: "A".repeat(100), + redirect_uris: Array.from( + { length: 5 }, + (_, index) => `https://a.co/${index}/${"界".repeat(497)}`, + ), + }), + }), + ); + expect(fiveLongCallbacks.status).toBe(201); + + for (const malformed of [ + { client_name: 42, redirect_uris: [REDIRECT] }, + { redirect_uris: REDIRECT }, + ]) { + const response = yield* handler(postJson("/oauth/mcp/register", malformed)); + expect(response.status).toBe(400); + expect(response.headers.get("content-type")).toMatch(/^application\/json/); + expect(yield* json<{ error: string }>(response)).toMatchObject({ + error: expect.stringMatching(/^invalid_(client_metadata|redirect_uri)$/), + }); + } const registered = yield* register(handler); expect(registered.status).toBe(201); @@ -252,6 +292,17 @@ it.effect("registers loopback and https clients and never redirects for an unver expect(unregistered.status).toBe(400); expect(unregistered.headers.get("location")).toBeNull(); + for (const resource of [`${ORIGIN}/mcp#section`, `${ORIGIN}/mcp#`]) { + const wrongTarget = yield* handler( + at( + `/oauth/mcp/authorize?${new URLSearchParams({ ...authorizeParams(clientId), resource })}`, + ), + ); + expect(wrongTarget.status).toBe(302); + const location = new URL(wrongTarget.headers.get("location")!, ORIGIN); + expect(location.searchParams.get("error"), resource).toBe("invalid_target"); + } + const query = new URLSearchParams(authorizeParams(clientId)).toString(); const handoff = yield* handler(at(`/oauth/mcp/authorize?${query}`)); expect(handoff.status).toBe(302); @@ -411,6 +462,43 @@ it.live("signs in a hosted agent with an https callback that asked for a client ), ); +it.live("signs in Gemini, which registers six user-bound redirect URIs for one client", () => + withRoutes((handler, auth) => + Effect.gen(function* () { + const redirectUris = Array.from( + { length: 6 }, + (_, index) => + `https://oauth-redirect.googleusercontent.com/r/user_bound_custom-mcp-test${index}`, + ); + const registered = yield* handler( + at("/oauth/mcp/register", { + method: "POST", + headers: { "content-type": "application/json" }, + body: encodeJson({ + client_name: "Google", + redirect_uris: redirectUris, + token_endpoint_auth_method: "client_secret_basic", + }), + }), + ); + const client = (yield* json(registered)) as { client_id: string }; + expect(client).toMatchObject({ redirect_uris: redirectUris }); + expect(registered.status).toBe(201); + + const callbackUrl = redirectUris[5]!; + const params = authorizeParams(client.client_id, callbackUrl); + const pairing = yield* auth.issuePairingCredential(); + const approved = yield* decide(handler, params, { + _tag: "pairing-code", + access: "approval-required", + code: pairing.credential, + }); + const callback = new URL(approved.redirectTo!); + expect(`${callback.origin}${callback.pathname}`).toBe(callbackUrl); + }), + ), +); + it.live("rejects a wrong PKCE verifier and spends the code", () => withRoutes((handler, auth) => Effect.gen(function* () { diff --git a/apps/server/src/auth/McpOAuth.ts b/apps/server/src/auth/McpOAuth.ts index 426cb2213f3ef..fdb0e4044f1df 100644 --- a/apps/server/src/auth/McpOAuth.ts +++ b/apps/server/src/auth/McpOAuth.ts @@ -54,8 +54,12 @@ import { requestUrl } from "../requestUrl.ts"; const SIGNING_SECRET_NAME = "mcp-oauth-signing-key"; const AUTHORIZATION_CODE_TTL_MS = 60_000; const MAX_CLIENT_NAME_LENGTH = 100; -const MAX_REDIRECT_URIS = 5; +const MAX_REDIRECT_URIS = 10; const MAX_REDIRECT_URI_LENGTH = 512; +// The client_id rides in the authorize URL. Past the original five-URI limit, cap it to leave room +// under Node's 16 KB header cap for an encoded redirect_uri and state. +const MAX_UNBOUNDED_CLIENT_ID_REDIRECT_URIS = 5; +const MAX_CLIENT_ID_LENGTH = 10_240; const DEFAULT_CLIENT_NAME = "MCP client"; const LOOPBACK_HOSTNAMES = new Set(["localhost", "127.0.0.1", "[::1]"]); const CODE_CHALLENGE_PATTERN = /^[A-Za-z0-9_-]{43}$/; @@ -130,9 +134,11 @@ export const redirectMatches = (registered: string, presented: string): boolean const sameResource = (urls: McpOAuthUrls, presented: string | undefined): boolean => { if (presented === undefined) return true; + // URL.hash misses empty fragments, which RFC 8707 §2 also forbids. + if (presented.includes("#")) return false; try { const url = new URL(presented); - return url.hash === "" && `${url.origin}${url.pathname.replace(/\/+$/u, "")}` === urls.resource; + return `${url.origin}${url.pathname.replace(/\/+$/u, "")}` === urls.resource; } catch { return false; } @@ -348,8 +354,18 @@ const make = Effect.gen(function* () { "Linked environments is off on that machine. Turn it on in its Settings → Labs.", }); } + const clientId = yield* signClientId(name, redirectUris, peerLink); + if ( + redirectUris.length > MAX_UNBOUNDED_CLIENT_ID_REDIRECT_URIS && + clientId.length > MAX_CLIENT_ID_LENGTH + ) { + return yield* new AuthMcpRegistrationError({ + error: "invalid_redirect_uri", + error_description: "Redirect URIs are too long to fit in an authorization request.", + }); + } return { - clientId: yield* signClientId(name, redirectUris, peerLink), + clientId, name, redirectUris, peerLink, diff --git a/apps/server/src/auth/PairingGrantStore.test.ts b/apps/server/src/auth/PairingGrantStore.test.ts index 5184beb096807..3f95ce46ec695 100644 --- a/apps/server/src/auth/PairingGrantStore.test.ts +++ b/apps/server/src/auth/PairingGrantStore.test.ts @@ -70,6 +70,22 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("PairingGrantStor ), ); + it.effect("accepts a one-time token typed in hyphen- or space-separated groups", () => + Effect.gen(function* () { + const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore; + const issued = yield* bootstrapCredentials.issueOneTimeToken(); + const formatted = issued.credential.match(/.{1,4}/g)!.join("-"); + const consumed = yield* bootstrapCredentials.consume(formatted.toLowerCase()); + const second = yield* bootstrapCredentials.issueOneTimeToken(); + const spaced = yield* bootstrapCredentials.consume( + second.credential.match(/.{1,4}/g)!.join(" "), + ); + + expect(consumed.method).toBe("one-time-token"); + expect(spaced.method).toBe("one-time-token"); + }).pipe(Effect.provide(layerPairingGrantStore())), + ); + it.effect("atomically consumes a one-time token when multiple requests race", () => Effect.gen(function* () { const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore; diff --git a/apps/server/src/auth/PairingGrantStore.ts b/apps/server/src/auth/PairingGrantStore.ts index f5f558d57526a..62d78d3495013 100644 --- a/apps/server/src/auth/PairingGrantStore.ts +++ b/apps/server/src/auth/PairingGrantStore.ts @@ -637,8 +637,10 @@ const make = Effect.gen(function* () { } // Only credentials in the issued pairing-code formats are case-insensitive. - const pairingCredential = PAIRING_TOKEN_PATTERN.test(credential) - ? credential.toUpperCase() + // People type codes in groups such as XXXX-XXXX-XXXX. + const compactCredential = credential.replace(/[\s-]/g, ""); + const pairingCredential = PAIRING_TOKEN_PATTERN.test(compactCredential) + ? compactCredential.toUpperCase() : (parseMcpApprovalCode(credential)?.code ?? credential); // Check scopes in the UPDATE so rejected requests cannot spend one-time links. diff --git a/apps/server/src/auth/RpcAuthorization.test.ts b/apps/server/src/auth/RpcAuthorization.test.ts index 4bed3b61134ce..4d2e3ebba5df1 100644 --- a/apps/server/src/auth/RpcAuthorization.test.ts +++ b/apps/server/src/auth/RpcAuthorization.test.ts @@ -77,6 +77,10 @@ describe("RPC authorization scopes", () => { } }); + it("still lets readers load settings the way installed Swift clients ask", () => { + expect(requiredScopeForRpcMethod("server.getSettings")).toBe(AuthOrchestrationReadScope); + }); + it("rejects unknown RPC method names", () => { for (const method of ["server.notRegistered", "toString", "constructor"]) { expect(() => requiredScopeForRpcMethod(method)).toThrow( diff --git a/apps/server/src/auth/RpcAuthorization.ts b/apps/server/src/auth/RpcAuthorization.ts index 2bf15ed69a82a..aa26cbb7a79ef 100644 --- a/apps/server/src/auth/RpcAuthorization.ts +++ b/apps/server/src/auth/RpcAuthorization.ts @@ -91,6 +91,7 @@ export const RPC_REQUIRED_SCOPES = { [WS_METHODS.serverRemoveKeybinding]: AuthSettingsWriteScope, [WS_METHODS.serverGetStorageCleanupReport]: AuthOrchestrationReadScope, [WS_METHODS.serverGetDatabaseStorage]: AuthOrchestrationReadScope, + [WS_METHODS.serverGetSettings]: AuthOrchestrationReadScope, [WS_METHODS.peerLinksList]: AuthAccessReadScope, [WS_METHODS.serverUpdateSettings]: AuthSettingsWriteScope, [WS_METHODS.serverSearchAcpRegistry]: AuthOrchestrationReadScope, diff --git a/apps/server/src/auth/ServerSecretStore.test.ts b/apps/server/src/auth/ServerSecretStore.test.ts index 08a33e2fde3c5..fa216177f494a 100644 --- a/apps/server/src/auth/ServerSecretStore.test.ts +++ b/apps/server/src/auth/ServerSecretStore.test.ts @@ -113,6 +113,55 @@ const layerConcurrentCreateSecretStore = () => Layer.provideMerge(layerConcurrentReadMissFileSystem), ); +// Both callers read the empty secret before either repairs it, and the second removal of the +// secret file waits until the first caller has returned its key. +const layerConcurrentEmptyRepairFileSystem = (firstReturned: Deferred.Deferred) => + Layer.effect( + FileSystem.FileSystem, + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const readCountRef = yield* Ref.make(0); + const removeCountRef = yield* Ref.make(0); + const readBarrier = yield* Deferred.make(); + const isSecretPath = (path: string) => /[\\/]session-signing-key\.bin$/.test(path); + + return { + ...fileSystem, + readFile: (path) => + isSecretPath(path) + ? Ref.updateAndGet(readCountRef, (count) => count + 1).pipe( + Effect.flatMap((count) => + count > 2 + ? fileSystem.readFile(path) + : Effect.gen(function* () { + if (count === 2) { + yield* Deferred.succeed(readBarrier, void 0); + } + yield* Deferred.await(readBarrier); + return yield* fileSystem.readFile(path); + }), + ), + ) + : fileSystem.readFile(path), + remove: (path, options) => + isSecretPath(path) + ? Ref.updateAndGet(removeCountRef, (count) => count + 1).pipe( + Effect.flatMap((count) => + count > 1 ? Deferred.await(firstReturned) : Effect.void, + ), + Effect.flatMap(() => fileSystem.remove(path, options)), + ) + : fileSystem.remove(path, options), + } satisfies FileSystem.FileSystem; + }), + ).pipe(Layer.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))); + +const layerConcurrentEmptyRepairSecretStore = (firstReturned: Deferred.Deferred) => + ServerSecretStore.layer.pipe( + Layer.provide(layerServerConfig()), + Layer.provideMerge(layerConcurrentEmptyRepairFileSystem(firstReturned)), + ); + it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("ServerSecretStore.layer", (it) => { it.effect("reuses an existing secret instead of regenerating it", () => Effect.gen(function* () { @@ -125,6 +174,40 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("ServerSecretStor }).pipe(Effect.provide(layerServerSecretStore())), ); + it.effect("replaces an empty persisted secret with a fresh random one", () => + Effect.gen(function* () { + const secretStore = yield* ServerSecretStore.ServerSecretStore; + yield* secretStore.set("session-signing-key", new Uint8Array()); + + const secret = yield* secretStore.getOrCreateRandom("session-signing-key", 32); + const persisted = yield* secretStore.get("session-signing-key"); + + assert.equal(secret.byteLength, 32); + assert.deepEqual(Array.from(Option.getOrThrow(persisted)), Array.from(secret)); + }).pipe(Effect.provide(layerServerSecretStore())), + ); + + it.effect("keeps the repaired secret when concurrent callers find it empty", () => + Effect.gen(function* () { + const firstReturned = yield* Deferred.make(); + yield* Effect.gen(function* () { + const secretStore = yield* ServerSecretStore.ServerSecretStore; + yield* secretStore.set("session-signing-key", new Uint8Array()); + const getOrCreate = secretStore + .getOrCreateRandom("session-signing-key", 32) + .pipe(Effect.tap(() => Deferred.succeed(firstReturned, void 0))); + + const [first, second] = yield* Effect.all([getOrCreate, getOrCreate], { + concurrency: "unbounded", + }); + const persisted = Option.getOrThrow(yield* secretStore.get("session-signing-key")); + + assert.deepEqual(Array.from(first), Array.from(persisted)); + assert.deepEqual(Array.from(second), Array.from(persisted)); + }).pipe(Effect.provide(layerConcurrentEmptyRepairSecretStore(firstReturned))); + }), + ); + it.effect("returns the persisted secret when concurrent creators race", () => Effect.gen(function* () { const secretStore = yield* ServerSecretStore.ServerSecretStore; diff --git a/apps/server/src/auth/ServerSecretStore.ts b/apps/server/src/auth/ServerSecretStore.ts index e950acb38a71d..cabded3d0c7b0 100644 --- a/apps/server/src/auth/ServerSecretStore.ts +++ b/apps/server/src/auth/ServerSecretStore.ts @@ -8,6 +8,7 @@ import * as Path from "effect/Path"; import * as Predicate from "effect/Predicate"; import * as PlatformError from "effect/PlatformError"; import * as Schema from "effect/Schema"; +import * as Semaphore from "effect/Semaphore"; import * as ServerConfig from "../config.ts"; @@ -169,6 +170,9 @@ export const make = Effect.gen(function* () { ); const resolveSecretPath = (name: string) => path.join(serverConfig.secretsDir, `${name}.bin`); + const emptySecretRepair = yield* Semaphore.make(1); + const isMissingOrNonEmpty = (secret: Option.Option) => + Option.isNone(secret) || secret.value.byteLength > 0; const get: ServerSecretStore["Service"]["get"] = (name) => fileSystem.readFile(resolveSecretPath(name)).pipe( @@ -263,6 +267,19 @@ export const make = Effect.gen(function* () { const getOrCreateRandom: ServerSecretStore["Service"]["getOrCreateRandom"] = (name, bytes) => get(name).pipe( + // Older builds could persist a 0-byte secret, which is never a usable key. Re-read under + // the permit, and keep it until an interrupted removal settles, so a caller that saw the + // empty file cannot remove a key another caller created. + Effect.filterOrElse(isMissingOrNonEmpty, () => + emptySecretRepair.withPermits(1)( + get(name).pipe( + Effect.filterOrElse(isMissingOrNonEmpty, () => + remove(name).pipe(Effect.as(Option.none())), + ), + Effect.uninterruptible, + ), + ), + ), Effect.flatMap( Option.match({ onSome: Effect.succeed, diff --git a/apps/server/src/auth/http.test.ts b/apps/server/src/auth/http.test.ts index 3a27fa9baba6b..28cc45de42c38 100644 --- a/apps/server/src/auth/http.test.ts +++ b/apps/server/src/auth/http.test.ts @@ -125,6 +125,18 @@ it.effect("sets the selected browser session cookies through the HTTP route", () expect(devCookies).toContainEqual( expect.stringMatching(/^t3_session_[^=]*=;.*Max-Age=0/), ); + expect(devCookies.filter((cookie) => /;\s*Secure/i.test(cookie))).toEqual([]); + const httpsResponse = await environmentA.handler( + postJson( + "/api/auth/browser-session", + { credential: DEV_TOKEN }, + { "x-forwarded-proto": "https" }, + ), + requestContext, + ); + const httpsCookies = httpsResponse.headers.getSetCookie(); + expect(httpsCookies).toHaveLength(2); + for (const cookie of httpsCookies) expect(cookie).toMatch(/;\s*Secure/i); const devCookieHeader = devCookie?.split(";", 1)[0] ?? ""; const environmentBSession = await environmentB.handler( new Request("http://127.0.0.1/api/auth/session", { diff --git a/apps/server/src/auth/http.ts b/apps/server/src/auth/http.ts index 9c9a05e6b7ce9..b8b49a1d1b347 100644 --- a/apps/server/src/auth/http.ts +++ b/apps/server/src/auth/http.ts @@ -26,6 +26,7 @@ import { causeErrorTag } from "@t3tools/shared/observability"; import * as Clock from "effect/Clock"; import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; +import * as Option from "effect/Option"; import * as Schema from "effect/Schema"; import * as Layer from "effect/Layer"; import * as Cookies from "effect/http/Cookies"; @@ -201,13 +202,24 @@ export const failEnvironmentInternal = return yield* new EnvironmentInternalError({ code: "internal_error", reason, traceId }); }); -const appendSessionCookie = (cookieName: string, token: string, expiresAt: DateTime.DateTime) => +// Plaintext LAN servers still need the cookie; TLS arrives through a proxy that sets X-Forwarded-Proto. +const sessionCookieOptions = (request: HttpServerRequest.HttpServerRequest) => ({ + httpOnly: true, + path: "/", + sameSite: "lax" as const, + secure: Option.exists(requestUrl(request), (url) => url.protocol === "https:"), +}); + +const appendSessionCookie = ( + request: HttpServerRequest.HttpServerRequest, + cookieName: string, + token: string, + expiresAt: DateTime.DateTime, +) => Effect.fromResult( Cookies.set(Cookies.empty, cookieName, token, { + ...sessionCookieOptions(request), expires: DateTime.toDate(expiresAt), - httpOnly: true, - path: "/", - sameSite: "lax", }), ).pipe( Effect.catch(() => failEnvironmentInternal("browser_session_cookie_failed")), @@ -287,7 +299,12 @@ export const layer = HttpApiBuilder.group( result.sessionMethod === "browser-session-cookie" && result.expiresAt ) { - yield* appendSessionCookie(sessions.cookieName, credential.token, result.expiresAt); + yield* appendSessionCookie( + request, + sessions.cookieName, + credential.token, + result.expiresAt, + ); yield* appendCredentialResponseHeaders; } return result; @@ -319,19 +336,17 @@ export const layer = HttpApiBuilder.group( const cookieName = result.cookieName ?? sessions.cookieName; const selectedCookie = yield* Effect.fromResult( Cookies.set(Cookies.empty, cookieName, result.sessionToken, { + ...sessionCookieOptions(request), expires: DateTime.toDate(result.response.expiresAt), - httpOnly: true, - path: "/", - sameSite: "lax", }), ).pipe(Effect.catch(() => failEnvironmentInternal("browser_session_cookie_failed"))); const sessionCookies = result.expireNormalCookie ? yield* Effect.fromResult( - Cookies.expireCookie(selectedCookie, sessions.cookieName, { - httpOnly: true, - path: "/", - sameSite: "lax", - }), + Cookies.expireCookie( + selectedCookie, + sessions.cookieName, + sessionCookieOptions(request), + ), ).pipe(Effect.catch(() => failEnvironmentInternal("browser_session_cookie_failed"))) : selectedCookie; diff --git a/apps/server/src/auth/mcpOAuthHttp.ts b/apps/server/src/auth/mcpOAuthHttp.ts index a411c635e30e4..6e10362000adf 100644 --- a/apps/server/src/auth/mcpOAuthHttp.ts +++ b/apps/server/src/auth/mcpOAuthHttp.ts @@ -2,6 +2,8 @@ import { AuthMcpApprovalError, AuthMcpAuthorizationRequest, AuthMcpClientAccess, + AuthMcpClientRegistration, + AuthMcpRegistrationError, EnvironmentHttpApi, mcpClientScopes, } from "@t3tools/contracts"; @@ -42,6 +44,10 @@ const noStore = HttpEffect.appendPreResponseHandler((_request, response) => const requestUrls = Effect.map(HttpServerRequest.HttpServerRequest, McpOAuth.requestUrls); const decodeAuthorizationRequest = Schema.decodeEffect(AuthMcpAuthorizationRequest); +// Decoded by the handler so malformed metadata gets the RFC 7591 error, not HttpApi's empty 400. +const decodeClientRegistration = Schema.decodeUnknownEffect( + Schema.fromJsonString(AuthMcpClientRegistration), +); /** * An unverified client or redirect is reported, never redirected; any other problem goes back to @@ -100,8 +106,18 @@ export const layer = HttpApiBuilder.group( .handle("authorizationServer", () => requestUrls.pipe(Effect.map(McpOAuth.authorizationServerMetadata)), ) - .handle("register", ({ payload }) => - oauth.register(payload).pipe( + .handleRaw("register", ({ request }) => + request.text.pipe( + Effect.orDie, + Effect.flatMap(decodeClientRegistration), + Effect.mapError( + (cause) => + new AuthMcpRegistrationError({ + error: "invalid_client_metadata", + error_description: `Invalid client metadata: ${cause.message}`, + }), + ), + Effect.flatMap(oauth.register), Effect.map((client) => ({ client_id: client.clientId, client_name: client.name, diff --git a/apps/server/src/auth/utils.test.ts b/apps/server/src/auth/utils.test.ts index 30784ab33ceb0..f9c228aeb418b 100644 --- a/apps/server/src/auth/utils.test.ts +++ b/apps/server/src/auth/utils.test.ts @@ -24,6 +24,29 @@ describe("deriveAuthClientMetadata", () => { }); }); + it("records the forwarded client address of a request proxied by Tailscale Serve", () => { + const metadata = deriveAuthClientMetadata({ + request: { + headers: { + "x-forwarded-for": "100.64.0.2", + "x-forwarded-host": "host-a.tailnet-name.ts.net", + "x-forwarded-proto": "https", + }, + source: { remoteAddress: "127.0.0.1" }, + } as never, + }); + + expect(metadata.ipAddress).toBe("100.64.0.2"); + + const direct = deriveAuthClientMetadata({ + request: { + headers: { "x-forwarded-for": "100.64.0.2" }, + source: { remoteAddress: "192.168.1.20" }, + } as never, + }); + expect(direct.ipAddress).toBe("192.168.1.20"); + }); + it.each([ [ "Edge on Android", diff --git a/apps/server/src/auth/utils.ts b/apps/server/src/auth/utils.ts index c1b8dc92fbe00..12c27cc1fdf57 100644 --- a/apps/server/src/auth/utils.ts +++ b/apps/server/src/auth/utils.ts @@ -166,12 +166,25 @@ function readRemoteAddressFromSource(source: unknown): string | undefined { return normalizeIpAddress(candidate.socket?.remoteAddress ?? candidate.remoteAddress); } +const isLoopbackIpAddress = (address: string): boolean => + (NodeNet.isIPv4(address) && address.startsWith("127.")) || address === "::1"; + +// A loopback peer is a local reverse proxy (Tailscale Serve); trust only the entry it appended. +function resolveClientIpAddress(request: HttpServerRequest.HttpServerRequest): string | undefined { + const peer = readRemoteAddressFromSource(request.source); + if (!peer || !isLoopbackIpAddress(peer)) { + return peer; + } + const forwarded = normalizeIpAddress(request.headers["x-forwarded-for"]?.split(",").at(-1)); + return forwarded && NodeNet.isIP(forwarded) !== 0 ? forwarded : peer; +} + export function deriveAuthClientMetadata(input: { readonly request: HttpServerRequest.HttpServerRequest; readonly presented?: AuthClientPresentationMetadata; }): AuthClientMetadata { const userAgent = normalizeNonEmptyString(input.request.headers["user-agent"]); - const ipAddress = readRemoteAddressFromSource(input.request.source); + const ipAddress = resolveClientIpAddress(input.request); const os = input.presented?.os ?? inferOs(userAgent); const browser = inferBrowser(userAgent); return { diff --git a/apps/server/src/binCli.test.ts b/apps/server/src/binCli.test.ts new file mode 100644 index 0000000000000..964f8c4f23f27 --- /dev/null +++ b/apps/server/src/binCli.test.ts @@ -0,0 +1,122 @@ +// @effect-diagnostics nodeBuiltinImport:off -- Exercises the CLI's real stdout and stderr through a subprocess and an HTTP fixture. +import * as NodeHttp from "node:http"; +import * as NodeURL from "node:url"; + +import * as NodeServices from "@effect/platform-node/NodeServices"; +import { assert, it } from "@effect/vitest"; +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; +import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; +import * as Stream from "effect/Stream"; +import { ChildProcess, ChildProcessSpawner } from "effect/process"; +import * as SqlClient from "effect/sql/SqlClient"; + +import { migrationManifest, runMigrations } from "./persistence/Migrations.ts"; +import { + makePersistedServerRuntimeState, + persistServerRuntimeState, +} from "./serverRuntimeState.ts"; + +it.live("keeps migration warnings out of JSON thread output", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-json-" }); + const stateDir = `${root}/userdata`; + yield* fs.makeDirectory(stateDir); + yield* Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + yield* runMigrations(); + const futureId = Math.max(...migrationManifest.map(([id]) => id)) + 1; + yield* sql` + INSERT INTO effect_sql_migrations (migration_id, name) + VALUES (${futureId}, 'FutureMigration') + `; + }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: `${stateDir}/statev2.sqlite` }))); + + const server = yield* Effect.acquireRelease( + Effect.promise(async () => { + const server = NodeHttp.createServer(async (request, response) => { + let body = ""; + for await (const chunk of request) body += String(chunk); + if (request.method === "DELETE") { + response.writeHead(204).end(); + return; + } + const message = JSON.parse(body) as { + id?: string; + method: string; + params?: { name: string }; + }; + if (message.id === undefined) { + response.writeHead(202).end(); + return; + } + const result = + message.method === "initialize" + ? { protocolVersion: "2025-06-18" } + : { + structuredContent: + message.params?.name === "t3_project_list" + ? { + projects: [{ id: "project-1", title: "Test", workspaceRoot: root }], + nextCursor: null, + } + : { + projectId: "project-1", + currentThreadId: null, + threads: [], + nextCursor: null, + total: 0, + }, + }; + response.writeHead(200, { + "content-type": "application/json", + "mcp-session-id": "cli-json-test", + }); + response.end(JSON.stringify({ jsonrpc: "2.0", id: message.id, result })); + }); + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); + return server; + }), + (server) => + Effect.promise(() => new Promise((resolve) => server.close(() => resolve()))), + ); + const address = server.address(); + assert(address !== null && typeof address !== "string"); + yield* persistServerRuntimeState({ + path: `${stateDir}/server-runtime.json`, + state: yield* makePersistedServerRuntimeState({ + config: { host: "127.0.0.1", devUrl: undefined }, + port: address.port, + }), + }); + + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; + const child = yield* spawner.spawn( + ChildProcess.make( + process.execPath, + [ + NodeURL.fileURLToPath(new URL("./bin.ts", import.meta.url)), + "thread", + "list", + "--project", + "project-1", + "--json", + "--base-dir", + root, + ], + { env: { T3CODE_HOME: root } }, + ), + ); + const { stdout, stderr, exitCode } = yield* Effect.all( + { + stdout: child.stdout.pipe(Stream.decodeText(), Stream.mkString), + stderr: child.stderr.pipe(Stream.decodeText(), Stream.mkString), + exitCode: child.exitCode, + }, + { concurrency: "unbounded" }, + ); + assert.strictEqual(Number(exitCode), 0, stderr); + assert.deepStrictEqual(JSON.parse(stdout), []); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), +); diff --git a/apps/server/src/cli/config.test.ts b/apps/server/src/cli/config.test.ts index 5e09dda2c164f..ddbc49e1ced74 100644 --- a/apps/server/src/cli/config.test.ts +++ b/apps/server/src/cli/config.test.ts @@ -214,6 +214,59 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => { }), ); + it.effect("names the pair command for the refused server's home", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const child = yield* liveProcess; + assert(child.pid !== undefined); + for (const source of ["flag", "T3CODE_HOME", "default"] as const) { + const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-preflight-pair-" }); + const baseDir = source === "default" ? path.join(home, ".t3") : home; + const stateDir = path.join(baseDir, "userdata"); + yield* fs.makeDirectory(stateDir, { recursive: true }); + yield* fs.writeFileString( + path.join(stateDir, "server-runtime.json"), + yield* encodeUnknownJson({ + version: 1, + pid: child.pid, + port: 3773, + origin: "http://127.0.0.1:3773", + startedAt: "2026-10-01T00:00:00.000Z", + }), + ); + const error = yield* resolveServerConfig( + { + ...minimalWebFlags(baseDir), + baseDir: source === "flag" ? Option.some(baseDir) : Option.none(), + cwd: Option.some(path.join(home, "project")), + }, + Option.none(), + { rejectRunningServer: true }, + ).pipe( + Effect.provide( + Layer.merge( + NetService.layer, + ConfigProvider.layer( + ConfigProvider.fromEnv({ + env: source === "T3CODE_HOME" ? { T3CODE_HOME: baseDir } : {}, + }), + ), + ), + ), + Effect.provideService(HostProcess.HomeDirectory, home), + Effect.provideService(HostProcess.Platform, "linux"), + Effect.flip, + ); + // A temp dir with spaces or backslashes is single-quoted for the shell. + const homePattern = baseDir.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); + expect(String(error)).toMatch( + new RegExp(`pair --base-dir (${homePattern}|'${homePattern}')`), + ); + } + }), + ); + it.effect("enables a trimmed reusable auth token only for web dev mode", () => Effect.gen(function* () { const baseDir = yield* FileSystem.FileSystem.pipe( diff --git a/apps/server/src/cli/config.ts b/apps/server/src/cli/config.ts index a7b9584d1a7e6..25d6c6eb62dd6 100644 --- a/apps/server/src/cli/config.ts +++ b/apps/server/src/cli/config.ts @@ -27,6 +27,7 @@ import { resolveServiceLauncherMode } from "../cloud/serviceLauncherClient.ts"; import * as ServerConfig from "../config.ts"; import { expandHomePath, resolveBaseDir } from "../os-jank.ts"; import { isProcessAlive, readPersistedServerRuntimeState } from "../serverRuntimeState.ts"; +import { resolveHostCliCommand } from "./invocation.ts"; const modeFlag = Flag.Literals("mode", ServerConfig.RuntimeMode.literals).pipe( Flag.withDescription("Runtime mode. `desktop` keeps loopback defaults unless overridden."), @@ -355,8 +356,13 @@ export const resolveServerConfig = Effect.fn("config.resolveServerConfig")(funct runtime.value.pid !== process.pid && isProcessAlive(runtime.value.pid) ) { + // Bare `t3 pair` prefers a linked worktree's `.t3`, so always name this home. + const { command: pairCommand } = yield* resolveHostCliCommand("pair", [ + "--base-dir", + baseDir, + ]); return yield* new CliError.UserError({ - cause: `A T3 Code server is already running for ${baseDir} (pid ${runtime.value.pid}, ${runtime.value.origin}). Connect to that server, stop it before starting another, or use a different --base-dir.`, + cause: `A T3 Code server is already running for ${baseDir} (pid ${runtime.value.pid}, ${runtime.value.origin}). To open it, run \`${pairCommand}\` and open the pairing URL it prints. To start another server, stop this one first or use a different --base-dir.`, }); } } diff --git a/apps/server/src/cli/connect.test.ts b/apps/server/src/cli/connect.test.ts index 2a51d059df67f..4fbec456da093 100644 --- a/apps/server/src/cli/connect.test.ts +++ b/apps/server/src/cli/connect.test.ts @@ -3,6 +3,7 @@ import * as NodeHttp from "node:http"; import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; import * as NodeServices from "@effect/platform-node/NodeServices"; +import * as HostProcess from "@t3tools/shared/HostProcess"; import * as NetService from "@t3tools/shared/Net"; import * as RelayClient from "@t3tools/shared/relayClient"; import { assert, it } from "@effect/vitest"; @@ -22,6 +23,7 @@ import { persistServerRuntimeState, } from "../serverRuntimeState.ts"; import { acquireRelayClientForLink } from "./connect.ts"; +import { resolveHostCliCommand } from "./invocation.ts"; const managedExecutable = { status: "available", @@ -59,6 +61,40 @@ it.effect("does not install the relay client when the user declines the managed }), ); +it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer))( + "t3 connect next steps", + (it) => { + it.effect.each([ + { subcommand: "status", hint: "connect link" }, + { subcommand: "publish", hint: "connect login" }, + ])("t3 connect $subcommand names `t3 $hint` with --base-dir", ({ subcommand, hint }) => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-connect-hint-test-" }); + + yield* Command.runWith(makeCli({ cloudEnabled: true }), { version: "0.0.0" })([ + "connect", + subcommand, + "--base-dir", + baseDir, + ]); + + const { command } = yield* resolveHostCliCommand(hint, ["--base-dir", baseDir]); + assert.include((yield* TestConsole.logLines).join("\n"), `\`${command}\``); + }).pipe( + Effect.scoped, + Effect.provideService(HostProcess.Environment, {}), + Effect.provide( + Layer.mergeAll( + ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })), + Layer.fresh(TestConsole.layer), + ), + ), + ), + ); + }, +); + it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer))( "t3 connect unlink and status", (it) => { diff --git a/apps/server/src/cli/connect.ts b/apps/server/src/cli/connect.ts index 8b3a3449dff6f..322b17242d9a9 100644 --- a/apps/server/src/cli/connect.ts +++ b/apps/server/src/cli/connect.ts @@ -56,7 +56,7 @@ import { readPersistedServerRuntimeState, } from "../serverRuntimeState.ts"; import { projectLocationFlags, resolveCliAuthConfig } from "./config.ts"; -import { resolveCliCommand } from "./invocation.ts"; +import { resolveHostCliHint } from "./invocation.ts"; import * as CliService from "./service.ts"; const jsonFlag = Flag.Boolean("json").pipe( @@ -184,7 +184,11 @@ function formatRelayClientStatus(executable: RelayClient.RelayClientStatus): Rea } } -function formatCloudStatus(status: CloudCliStatus, options?: { readonly json?: boolean }): string { +function formatCloudStatus( + status: CloudCliStatus, + commands: { readonly link: string; readonly serviceStatus: string }, + options?: { readonly json?: boolean }, +): string { if (options?.json) { return JSON.stringify(status, null, 2); } @@ -198,11 +202,11 @@ function formatCloudStatus(status: CloudCliStatus, options?: { readonly json?: b : "not provisioned"; const publishOnly = status.desired && status.linkMode === "publish_only"; const nextStep = !status.authenticated - ? "Run `t3 connect link` to authorize and enable T3 Connect." + ? `Run \`${commands.link}\` to authorize and enable T3 Connect.` : status.revoked - ? "Run `t3 connect link` to link this environment again." + ? `Run \`${commands.link}\` to link this environment again.` : !status.desired - ? "Run `t3 connect link` to enable T3 Connect." + ? `Run \`${commands.link}\` to enable T3 Connect.` : !status.linked ? publishOnly ? "Start T3 to provision the environment link." @@ -227,7 +231,7 @@ function formatCloudStatus(status: CloudCliStatus, options?: { readonly json?: b : status.reachability === null ? [ "", - "No running server answered, so reachability was not checked. Check the background service with `t3 service status`.", + `No running server answered, so reachability was not checked. Check the background service with \`${commands.serviceStatus}\`.`, ] : status.reachability.status === "reachable" ? [ @@ -667,7 +671,7 @@ const connectLinkCommand = Command.make("link", { yield* Console.log("T3 Connect\n"); const linked = yield* linkEnvironmentForConnect(flags); if (linked) { - const serveCommand = yield* resolveCliCommand("serve"); + const serveCommand = yield* resolveHostCliHint("serve", flags.baseDir); yield* Console.log( flags.publishOnly ? `✓ Authorized${connectedAs(linked.identity)}\n\nNext\n Start T3 to publish agent activity (no managed tunnel).` @@ -732,7 +736,11 @@ const connectStatusCommand = Command.make("status", { linkMode, reachability, }; - yield* Console.log(formatCloudStatus(status, { json: flags.json })); + const commands = { + link: yield* resolveHostCliHint("connect link", flags.baseDir), + serviceStatus: yield* resolveHostCliHint("service status", flags.baseDir), + }; + yield* Console.log(formatCloudStatus(status, commands, { json: flags.json })); }), { quietLogs: flags.json, @@ -782,8 +790,9 @@ const connectPublishCommand = Command.make("publish", { } if (!(yield* tokens.hasCredential)) { + const loginCommand = yield* resolveHostCliHint("connect login", flags.baseDir); yield* Console.log( - "Run `t3 connect login` first so this environment can be authorized to publish.", + `Run \`${loginCommand}\` first so this environment can be authorized to publish.`, ); return; } diff --git a/apps/server/src/cli/install.test.ts b/apps/server/src/cli/install.test.ts index 80b3041d24e77..104718a568cd8 100644 --- a/apps/server/src/cli/install.test.ts +++ b/apps/server/src/cli/install.test.ts @@ -9,6 +9,7 @@ import * as Path from "effect/Path"; import * as TestClock from "effect/testing/TestClock"; import packageJson from "../../package.json" with { type: "json" }; +import { posixLauncherScript } from "../cloud/pinnedRuntime.ts"; import { installCli, isForegroundProcStat, isForegroundPs, offerCliInstall } from "./install.ts"; const VERSION = packageJson.version; @@ -47,7 +48,7 @@ const setup = Effect.gen(function* () { }); it.layer(NodeServices.layer)("t3 install", (it) => { - it.effect("links the running runtime, records it, and never replaces another t3", () => + it.effect("installs the running runtime, records it, and never replaces another t3", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; @@ -57,7 +58,7 @@ it.layer(NodeServices.layer)("t3 install", (it) => { yield* fs.makeDirectory(path.dirname(link), { recursive: true }); yield* fs.symlink(older, link); const installed = yield* provide(installCli()); - assert.equal(yield* fs.readLink(link), executable); + assert.equal(yield* fs.readFileString(link), posixLauncherScript(executable)); assert.isFalse(installed.onPath); assert.include(installed.pathCommand, "$HOME/bin"); @@ -96,30 +97,25 @@ it.layer(NodeServices.layer)("t3 install", (it) => { yield* fs.remove(link); yield* fs.symlink(path.join(root, ".t3/bin/t3"), link); yield* provide(installCli()); - assert.equal(yield* fs.readLink(link), executable); + assert.equal(yield* fs.readFileString(link), posixLauncherScript(executable)); - // A failed swap over our own link leaves a staging path it did not create alone. - yield* fs.remove(link); - yield* fs.symlink(older, link); - const staging = `${link}.${process.pid}.tmp`; - yield* fs.writeFileString(staging, "someone else's\n"); - const linkFailed = yield* provide(installCli()).pipe(Effect.flip); - assert.include(linkFailed.message, "Could not link"); - assert.equal(yield* fs.readFileString(staging), "someone else's\n"); - yield* fs.remove(staging); + // A script an earlier install wrote is ours too. + yield* fs.writeFileString(link, posixLauncherScript(older)); + yield* provide(installCli()); + assert.equal(yield* fs.readFileString(link), posixLauncherScript(executable)); // An empty spot is created exclusively: a file that appears first is kept. yield* fs.remove(link); const racing = installCli().pipe( Effect.provideService(FileSystem.FileSystem, { ...fs, - symlink: (target, path) => + link: (from, to) => fs - .writeFileString(path, "#!/bin/sh\necho raced\n") - .pipe(Effect.andThen(fs.symlink(target, path))), + .writeFileString(to, "#!/bin/sh\necho raced\n") + .pipe(Effect.andThen(fs.link(from, to))), }), ); - assert.include((yield* provide(racing).pipe(Effect.flip)).message, "Could not link"); + assert.include((yield* provide(racing).pipe(Effect.flip)).message, "Could not write"); assert.equal(yield* fs.readFileString(link), "#!/bin/sh\necho raced\n"); yield* fs.remove(link); @@ -235,7 +231,7 @@ it.layer(NodeServices.layer)("t3 install", (it) => { yield* fs.writeFileString(path.join(runtimeDir, "install-prompt-declined"), "0.0\n"); yield* offer(true); assert.equal(asked, 2); - assert.equal(yield* fs.readLink(link), executable); + assert.equal(yield* fs.readFileString(link), posixLauncherScript(executable)); yield* fs.remove(path.join(runtimeDir, "install-prompt-declined")); yield* offer(true); diff --git a/apps/server/src/cli/install.ts b/apps/server/src/cli/install.ts index ed3c8876a688c..b95e1ffefb359 100644 --- a/apps/server/src/cli/install.ts +++ b/apps/server/src/cli/install.ts @@ -15,7 +15,9 @@ import packageJson from "../../package.json" with { type: "json" }; import { generatedWindowsShimTarget, isVersionExecutable, + readPosixLauncherTarget, recordPinnedRuntimeLauncher, + writePosixLauncher, } from "../cloud/pinnedRuntime.ts"; import { detectCliRunner, resolveCliCommand, resolveEntryPath } from "./invocation.ts"; @@ -41,7 +43,7 @@ const standaloneRuntime = Effect.gen(function* () { return Option.some({ executable, versionsDir, runtimeDir }); }); -/** Links this runtime as the `t3` command the way the install scripts do, replacing only our own link. */ +/** Installs this runtime as the `t3` command the way the install scripts do, replacing only our own launcher. */ export const installCli = Effect.fn("cli.install.install")(function* () { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; @@ -84,7 +86,10 @@ export const installCli = Effect.fn("cli.install.install")(function* () { Effect.flatMap((target) => fs.realPath(link).pipe(Effect.orElseSucceed(() => path.resolve(binDir, target))), ), + // Not a link: the script this command and the install scripts write. + Effect.catch(() => readPosixLauncherTarget(link)), Effect.option, + Effect.map(Option.filter((target) => target !== undefined)), ); // A dangling symlink is occupied too: writing the shim would follow it. const occupied = @@ -115,22 +120,10 @@ export const installCli = Effect.fn("cli.install.install")(function* () { yield* fs .writeFileString(link, `@echo off\r\n"${executable}" %*`, occupied ? {} : { flag: "wx" }) .pipe(Effect.mapError(failed(`Could not write ${link}.`))); - } else if (!occupied) { - yield* fs.symlink(executable, link).pipe(Effect.mapError(failed(`Could not link ${link}.`))); } else { - // Swap a temporary link into place so a running `t3` never sees the command missing. - const staging = `${link}.${process.pid}.tmp`; - // Clean up only a staging link this call made, never a file that was already there. - yield* fs - .symlink(executable, staging) - .pipe( - Effect.andThen( - fs - .rename(staging, link) - .pipe(Effect.tapError(() => fs.remove(staging, { force: true }).pipe(Effect.ignore))), - ), - Effect.mapError(failed(`Could not link ${link}.`)), - ); + yield* writePosixLauncher({ launcher: link, executable, exclusive: !occupied }).pipe( + Effect.mapError(failed(`Could not write ${link}.`)), + ); } // Pruning keeps what recorded launchers point at; a background service cannot see binDir otherwise. diff --git a/apps/server/src/cli/invocation.test.ts b/apps/server/src/cli/invocation.test.ts index 4d41bf40f1a07..8310649aa77cb 100644 --- a/apps/server/src/cli/invocation.test.ts +++ b/apps/server/src/cli/invocation.test.ts @@ -6,6 +6,7 @@ import * as FileSystem from "effect/FileSystem"; import * as Path from "effect/Path"; import packageJson from "../../package.json" with { type: "json" }; +import { posixLauncherScript } from "../cloud/pinnedRuntime.ts"; import { resolveHostCliCommand, resolveRootCliCommand, @@ -291,7 +292,7 @@ it.layer(NodeServices.layer)("manual server installation ownership", (it) => { }), ); - it.effect("names the launcher symlink that started a downloaded runtime", () => + it.effect("names the launcher that started a downloaded runtime", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; @@ -310,13 +311,14 @@ it.layer(NodeServices.layer)("manual server installation ownership", (it) => { yield* fs.makeDirectory(path.join(root, "shadow-dir/t3"), { recursive: true }); yield* fs.makeDirectory(path.join(root, "shadow-file")); yield* fs.writeFileString(path.join(root, "shadow-file/t3"), ""); - const resolve = (invokedAs: string) => + const resolve = (invokedAs: string, PATH = "shadow-dir:shadow-file:bin") => resolveServerInstallation.pipe( Effect.provideService(HostProcess.ExecutablePath, executable), Effect.provideService(HostProcess.IsExecutable, true), Effect.provideService(HostProcess.InvokedAs, invokedAs), Effect.provideService(HostProcess.WorkingDirectory, root), - Effect.provideService(HostProcess.Environment, { PATH: "shadow-dir:shadow-file:bin" }), + Effect.provideService(HostProcess.Environment, { PATH }), + Effect.provideService(HostProcess.HomeDirectory, root), Effect.provideService(HostProcess.Platform, "linux"), ); const baseDir = path.join(root, ".t3"); @@ -325,6 +327,19 @@ it.layer(NodeServices.layer)("manual server installation ownership", (it) => { expect(yield* resolve(launcher)).toEqual({ kind: "t3-launcher", launcher, baseDir }); expect(yield* resolve(executable)).toBeNull(); expect(yield* resolve(copy)).toBeNull(); + + // The script launcher execs the runtime by absolute path, so argv0 names the runtime. + yield* fs.remove(launcher); + yield* fs.writeFileString( + launcher, + posixLauncherScript(path.join(root, ".t3/runtime/versions/1.0.0/t3")), + { mode: 0o755 }, + ); + expect(yield* resolve(executable, path.join(root, "bin"))).toEqual({ + kind: "t3-launcher", + launcher, + baseDir, + }); }), ); diff --git a/apps/server/src/cli/invocation.ts b/apps/server/src/cli/invocation.ts index 71738820ccdeb..4804d44f0e324 100644 --- a/apps/server/src/cli/invocation.ts +++ b/apps/server/src/cli/invocation.ts @@ -9,7 +9,11 @@ import * as HostProcess from "@t3tools/shared/HostProcess"; import { resolveCommandPath } from "@t3tools/shared/shell"; import packageJson from "../../package.json" with { type: "json" }; -import { resolveLauncherPath } from "../cloud/pinnedRuntime.ts"; +import { + findPosixLauncher, + readPosixLauncherTarget, + resolveLauncherPath, +} from "../cloud/pinnedRuntime.ts"; const PINNED_RUNTIME_ENTRY = /^(.*)\/runtime\/versions\/[^/]+\/t3$/; @@ -72,12 +76,23 @@ export const resolveServerInstallation = Effect.gen(function* () { const entry = yield* fs.realPath(yield* resolveEntryPath); const runtime = PINNED_RUNTIME_ENTRY.exec(entry); if (runtime) { - const launcher = yield* resolveLauncherPath; + const launchedAs = yield* resolveLauncherPath; + if (launchedAs === undefined) return null; + const link = yield* fs.readLink(launchedAs).pipe(Effect.option); + // A launcher script execs the runtime itself, so look for the script that runs it. + const launcher = Option.isSome(link) + ? launchedAs + : yield* findPosixLauncher({ + launchedAs, + versionsDir: path.join(runtime[1] || "/", "runtime/versions"), + }); + // None for a runtime started directly, as SSH launches do, or through a copy. if (launcher === undefined) return null; - // Fails for a runtime started directly, as SSH launches do, or through a copy. - const link = yield* fs.readLink(launcher); - // Keep the home as the link spells it; `t3 update` only repoints links into that spelling. - const linked = PINNED_RUNTIME_ENTRY.exec(path.resolve(path.dirname(launcher), link)); + const target = Option.isSome(link) + ? path.resolve(path.dirname(launcher), link.value) + : yield* readPosixLauncherTarget(launcher); + // Keep the home as the launcher spells it; `t3 update` only repoints launchers into that spelling. + const linked = target === undefined ? null : PINNED_RUNTIME_ENTRY.exec(target); if (!linked || (yield* fs.realPath(linked[1] || "/")) !== (runtime[1] || "/")) return null; return { kind: "t3-launcher", @@ -170,7 +185,8 @@ export const resolveHostCliCommand = Effect.fn("invocation.resolveHostCliCommand ) { const platform = yield* HostProcess.Platform; const withArgs = (prefix: string) => - [prefix, ...args.map((arg) => hostShellWord(platform, arg))].join(" "); + // An empty subcommand names the root `t3` command. + [prefix.trimEnd(), ...args.map((arg) => hostShellWord(platform, arg))].join(" "); const command = yield* resolveCliCommand(subcommand); if (command !== `t3 ${subcommand}`) return { command: withArgs(command), launcher: false }; const onPath = yield* resolveCommandPath("t3"); @@ -200,6 +216,17 @@ export const resolveHostCliCommand = Effect.fn("invocation.resolveHostCliCommand return { command: withArgs(`${call} ${subcommand}`), launcher: true }; }); +/** A hint's command, run against the caller's `--base-dir` when one was given. */ +export const resolveHostCliHint = ( + subcommand: string, + baseDir: Option.Option, + args: ReadonlyArray = [], +) => + resolveHostCliCommand(subcommand, [ + ...Option.match(baseDir, { onNone: () => [], onSome: (dir) => ["--base-dir", dir] }), + ...args, + ]).pipe(Effect.map(({ command }) => command)); + /** * `sudo` resets PATH on most distributions, which drops a user-installed Node * (nvm, fnm, a tarball) and with it `npx` or a global `t3`, so the command diff --git a/apps/server/src/cli/pair.ts b/apps/server/src/cli/pair.ts index 622b47f91a65e..4b563155c6bfe 100644 --- a/apps/server/src/cli/pair.ts +++ b/apps/server/src/cli/pair.ts @@ -161,10 +161,14 @@ export class ServePortOccupiedError extends Schema.TaggedError +const resolveDirectPairingBaseUrl = ( + state: PersistedServerRuntimeState, + platform: NodeJS.Platform, +): string => state.devUrl ?? resolveHeadlessConnectionString(state.host, state.port, { privateNetworkOnly: state.privateNetworkOnly, + platform, }); export class DevServerNotProxiableError extends Schema.TaggedError()( @@ -562,7 +566,7 @@ export const pairCommand = Command.make("pair", { pairingBaseUrl = resolved.baseUrl; notes.push(...resolved.notes); } else { - pairingBaseUrl = resolveDirectPairingBaseUrl(target.state); + pairingBaseUrl = resolveDirectPairingBaseUrl(target.state, yield* HostProcess.Platform); if (isLoopbackHost(new URL(pairingBaseUrl).hostname)) { notes.push( "This URL is only reachable from this machine. To reach it from your other devices, run `t3 up`, or re-run with --tailscale.", diff --git a/apps/server/src/cli/project.test.ts b/apps/server/src/cli/project.test.ts index bca02daaf2916..5e33ad84bc120 100644 --- a/apps/server/src/cli/project.test.ts +++ b/apps/server/src/cli/project.test.ts @@ -89,7 +89,10 @@ const makeConfig = (baseDir: string) => } satisfies ServerConfig.ServerConfig["Service"]; }); -const readProjects = (baseDir: string) => +const withProjectService = ( + baseDir: string, + use: (projects: ProjectService.ProjectService["Service"]) => Effect.Effect, +) => Effect.gen(function* () { const config = yield* makeConfig(baseDir); const layer = RuntimeLayer.layerProjectService.pipe( @@ -108,12 +111,12 @@ const readProjects = (baseDir: string) => Layer.provide(ServerConfig.layer(config)), Layer.provide(Layer.succeed(References.MinimumLogLevel, config.logLevel)), ); - return yield* ProjectService.ProjectService.pipe( - Effect.flatMap((projects) => projects.snapshot), - Effect.provide(layer), - ); + return yield* ProjectService.ProjectService.pipe(Effect.flatMap(use), Effect.provide(layer)); }); +const readProjects = (baseDir: string) => + withProjectService(baseDir, (projects) => projects.snapshot); + it.effect("adds, renames, and removes projects through the V2 project CLI domain", () => Effect.gen(function* () { const baseDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-v2-project-cli-")); @@ -237,6 +240,36 @@ const makeProjectLookupFixture = Effect.fn("ProjectCliTest.makeProjectLookupFixt return { baseDir, workspaceRoot, project: project! }; }); +it.effect.each([ + { label: "keeps its title", titleArgs: [], expectedTitle: undefined }, + { label: "applies --title", titleArgs: ["--title", " Back again "], expectedTitle: "Back again" }, +])( + "adding an archived project's folder again unarchives it and $label", + ({ titleArgs, expectedTitle }) => + Effect.gen(function* () { + const { baseDir, workspaceRoot, project } = yield* makeProjectLookupFixture(); + yield* withProjectService(baseDir, (projects) => + projects.update({ + commandId: CommandId.make("project-cli-archive"), + projectId: project.id, + archived: true, + }), + ); + + yield* runCli(["project", "add", workspaceRoot, ...titleArgs, "--base-dir", baseDir]); + + const projects = (yield* readProjects(baseDir)).projects; + assert.deepEqual( + projects.map((entry) => ({ + id: entry.id, + title: entry.title, + archivedAt: entry.archivedAt, + })), + [{ id: project.id, title: expectedTitle ?? project.title, archivedAt: null }], + ); + }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), Effect.scoped), +); + const makeThreadPersistenceLayer = Effect.fn("ProjectCliTest.makeThreadPersistenceLayer")( function* (baseDir: string) { const config = yield* makeConfig(baseDir); diff --git a/apps/server/src/cli/project.ts b/apps/server/src/cli/project.ts index da63d65975ae5..3e59c5bd4735c 100644 --- a/apps/server/src/cli/project.ts +++ b/apps/server/src/cli/project.ts @@ -491,6 +491,20 @@ const projectAddCommand = Command.make("add", { const existingProject = snapshot.projects.find( (project) => project.deletedAt === null && project.workspaceRoot === workspaceRoot, ); + // Re-adding an archived project's folder restores it, matching the clients. + if (existingProject?.archivedAt != null) { + const title = Option.isSome(flags.title) + ? yield* resolveProjectTitle(workspaceRoot, flags.title.value) + : undefined; + yield* dispatch({ + type: "project.update", + commandId: CommandId.make(yield* projectCommandUuid), + projectId: existingProject.id, + archived: false, + ...(title === undefined ? {} : { title }), + }); + return `Unarchived project ${existingProject.id} (${title ?? existingProject.title}) at ${workspaceRoot}.`; + } if (existingProject) { return yield* new ProjectAlreadyExistsError({ operation: "addProject", diff --git a/apps/server/src/cli/service.test.ts b/apps/server/src/cli/service.test.ts index 7dd4e852adba8..ada548ba2bf39 100644 --- a/apps/server/src/cli/service.test.ts +++ b/apps/server/src/cli/service.test.ts @@ -12,6 +12,7 @@ import { afterEach, vi } from "vite-plus/test"; import packageJson from "../../package.json" with { type: "json" }; import * as BootService from "../cloud/bootService.ts"; +import { resolveHostCliCommand } from "./invocation.ts"; import { serviceCommand } from "./service.ts"; afterEach(() => vi.restoreAllMocks()); @@ -135,11 +136,14 @@ it.layer(Layer.mergeAll(NodeServices.layer, NetService.layer))("service commands Layer.succeed(BootService.BootService, service), ); - yield* Command.runWith(serviceCommand, { version: packageJson.version })([ + const [lines, up] = yield* Command.runWith(serviceCommand, { version: packageJson.version })([ "install", "--base-dir", baseDir, ]).pipe( + Effect.andThen( + Effect.all([TestConsole.logLines, resolveHostCliCommand("up", ["--base-dir", baseDir])]), + ), Effect.provideService(HostProcess.Environment, {}), Effect.provide( Layer.mergeAll( @@ -149,6 +153,9 @@ it.layer(Layer.mergeAll(NodeServices.layer, NetService.layer))("service commands ), ); expect(yield* fs.readFileString(`${baseDir}/service.env`)).toBe(input.expected); + expect(lines.map(String).filter((line) => line.includes("other devices"))).toEqual( + input.installed ? [] : [`Run \`${up.command}\` to choose how your other devices reach it.`], + ); }), ); @@ -221,6 +228,55 @@ it.layer(Layer.mergeAll(NodeServices.layer, NetService.layer))("service commands }), ); + it.effect.each([ + { + label: "a missing service", + status: { ...status, installed: false }, + hint: "service install", + args: [], + }, + { label: "a newer service", status: newerServiceStatus, hint: "update", args: ["999.0.0"] }, + { + label: "a stopped service", + status: { ...status, problems: ["service-stopped"] as const }, + hint: "service install", + args: [], + }, + { + label: "a restart-pending service", + status: { ...status, problems: ["restart-pending"] as const }, + hint: "service restart", + args: [], + }, + ])("status of $label names its next step with --base-dir", (input) => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-cli-test-" }); + vi.spyOn(BootService, "layer").mockReturnValue( + Layer.succeed(BootService.BootService, makeTestService(input.status).service), + ); + + const [lines, next] = yield* Command.runWith(serviceCommand, { + version: packageJson.version, + })(["status", "--base-dir", baseDir]).pipe( + Effect.andThen( + Effect.all([ + TestConsole.logLines, + resolveHostCliCommand(input.hint, ["--base-dir", baseDir, ...input.args]), + ]), + ), + Effect.provideService(HostProcess.Environment, {}), + Effect.provide( + Layer.mergeAll( + ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })), + Layer.fresh(TestConsole.layer), + ), + ), + ); + expect(lines.join("\n")).toContain(`\`${next.command}\``); + }), + ); + it.effect.each([ { label: "this home's service", otherHome: false, unreadable: false, removed: [true] }, { label: "another home's service", otherHome: true, unreadable: false, removed: [] }, diff --git a/apps/server/src/cli/service.ts b/apps/server/src/cli/service.ts index 2a34eb09dd08e..4902c47d06172 100644 --- a/apps/server/src/cli/service.ts +++ b/apps/server/src/cli/service.ts @@ -16,7 +16,7 @@ import { compareExactServiceVersions, SERVICE_ENV_FILE } from "../cloud/serviceP import * as ServerConfig from "../config.ts"; import * as ProcessRunner from "../processRunner.ts"; import { projectLocationFlags, resolveCliAuthConfig } from "./config.ts"; -import { resolveEntryPath } from "./invocation.ts"; +import { resolveEntryPath, resolveHostCliHint } from "./invocation.ts"; export const layer = (config: ServerConfig.ServerConfig["Service"]) => BootService.layer({ @@ -174,13 +174,14 @@ function formatProtection( function formatServiceStatus( status: BootService.BootServiceStatus, cliVersion: string, + commands: { readonly install: string; readonly restart: string; readonly update: string }, protection?: ServiceProtection.ServiceProtectionState, ): string { if (!status.supported) { return "T3 Code service\n Status: unavailable on this machine\n Supported on: Linux with systemd, macOS with launchd, Windows with Task Scheduler"; } if (!status.installed) { - return "T3 Code service\n Status: not installed\n Next: Run `t3 service install`."; + return `T3 Code service\n Status: not installed\n Next: Run \`${commands.install}\`.`; } if (status.otherBaseDir !== undefined) { return [ @@ -192,7 +193,7 @@ function formatServiceStatus( } const installedVersion = status.installedVersion ?? cliVersion; const problems = (status.problems ?? []).map( - (problem) => ` [${problem}] ${BootService.formatBootServiceProblem(problem)}`, + (problem) => ` [${problem}] ${BootService.formatBootServiceProblem(problem, commands)}`, ); if ( !status.current && @@ -206,7 +207,7 @@ function formatServiceStatus( ` Logs: ${status.logPath}`, ...formatProtection(protection), ...problems, - ` Next: Run \`t3 update ${installedVersion}\` to match it, or pass \`--allow-downgrade\` to \`t3 service install\` explicitly.`, + ` Next: Run \`${commands.update}\` to match it, or pass \`--allow-downgrade\` to \`${commands.install}\` explicitly.`, ].join("\n"); } return [ @@ -216,7 +217,7 @@ function formatServiceStatus( ` Logs: ${status.logPath}`, ...formatProtection(protection), ...problems, - ...(status.current ? [] : [" Next: Run `t3 service install` to repair it."]), + ...(status.current ? [] : [` Next: Run \`${commands.install}\` to repair it.`]), ].join("\n"); } @@ -254,7 +255,8 @@ const serviceInstallCommand = Command.make("install", serviceReconcileFlags).pip } yield* Console.log(formatServiceChange(result)); if (!result.previouslyInstalled) { - yield* Console.log("Run `t3 up` to choose how your other devices reach it."); + const upCommand = yield* resolveHostCliHint("up", flags.baseDir); + yield* Console.log(`Run \`${upCommand}\` to choose how your other devices reach it.`); } }), ), @@ -358,7 +360,14 @@ const serviceStatusCommand = Command.make("status", projectLocationFlags).pipe( Effect.provide(ProcessRunner.layer), ) : undefined; - yield* Console.log(formatServiceStatus(status, packageJson.version, protection)); + const commands = { + install: yield* resolveHostCliHint("service install", flags.baseDir), + restart: yield* resolveHostCliHint("service restart", flags.baseDir), + update: yield* resolveHostCliHint("update", flags.baseDir, [ + status.installedVersion ?? packageJson.version, + ]), + }; + yield* Console.log(formatServiceStatus(status, packageJson.version, commands, protection)); }), ), ), diff --git a/apps/server/src/cli/thread.ts b/apps/server/src/cli/thread.ts index 5218d0aa2576f..98d18a6c9d240 100644 --- a/apps/server/src/cli/thread.ts +++ b/apps/server/src/cli/thread.ts @@ -216,11 +216,12 @@ export type ThreadTools = ReturnType; * outside agent does, and revokes that session when the command ends. */ const withThreadTools = Effect.fn("cli.thread.withThreadTools")(function* ( - flags: { readonly baseDir: Option.Option }, + flags: { readonly baseDir: Option.Option; readonly json: boolean }, run: (tools: ThreadTools) => Effect.Effect, ) { const logLevel = yield* GlobalFlag.LogLevel; - const config = yield* resolveCliAuthConfig(flags, logLevel); + const config = yield* resolveCliAuthConfig(flags, logLevel, { quietLogs: flags.json }); + const minimumLogLevel = flags.json ? "Error" : config.logLevel; return yield* Effect.gen(function* () { const runtimeState = yield* readLiveServerRuntimeState(config.serverRuntimeStatePath); if (Option.isNone(runtimeState)) { @@ -248,10 +249,11 @@ const withThreadTools = Effect.fn("cli.thread.withThreadTools")(function* { assert.isUndefined(yield* findOwnedLauncher({ launchedAs: theirs, versionsDir })); assert.isUndefined(yield* findOwnedLauncher({ launchedAs: copy, versionsDir })); assert.isUndefined(yield* findOwnedLauncher({ launchedAs: undefined, versionsDir })); + // A script launcher execs by absolute path, so argv0 names the executable. + const scriptDir = path.join(root, "script-bin"); + const script = path.join(scriptDir, "t3"); + yield* fs.makeDirectory(scriptDir); + yield* fs.writeFileString(script, posixLauncherScript(exe), { mode: 0o755 }); + assert.equal( + yield* findOwnedLauncher({ launchedAs: exe, versionsDir }).pipe( + Effect.provideService(HostProcess.Environment, { PATH: scriptDir }), + ), + script, + ); + const editedScript = path.join(scriptDir, "t3-edited"); + yield* fs.writeFileString(editedScript, `${posixLauncherScript(exe)}# mine\n`); // On Windows, only the shim exactly as written is ours; a user's wrapper stays. const shimDir = path.join(root, "win-bin"); @@ -130,11 +144,25 @@ it.layer(NodeServices.layer)("t3 uninstall launcher", (it) => { yield* fs.symlink(path.join(versionsDir, "1.0.0/node"), sibling); yield* fs.writeFileString( path.join(root, "runtime/launchers"), - [ours, theirs, copy, throughAlias, pruned, sibling, path.join(root, "missing")].join("\n") + - "\n", + [ + ours, + theirs, + copy, + throughAlias, + pruned, + sibling, + script, + editedScript, + path.join(root, "missing"), + ].join("\n") + "\n", ); for (const dir of [versionsDir, path.join(root, "alias/runtime/versions")]) { - assert.deepStrictEqual(yield* findRecordedLaunchers(dir), [ours, throughAlias, pruned]); + assert.deepStrictEqual(yield* findRecordedLaunchers(dir), [ + ours, + throughAlias, + pruned, + script, + ]); } // On Windows only an unedited t3.cmd is ours; a hand-edited wrapper belongs to the user. diff --git a/apps/server/src/cli/uninstall.ts b/apps/server/src/cli/uninstall.ts index bfbc45bf56152..609ece7265662 100644 --- a/apps/server/src/cli/uninstall.ts +++ b/apps/server/src/cli/uninstall.ts @@ -13,9 +13,11 @@ import { Command, Flag, GlobalFlag, Prompt } from "effect/cli"; import * as BootService from "../cloud/bootService.ts"; import { + findPosixLauncher, generatedWindowsShimTarget, isVersionExecutable, pinnedRuntimeVersionsDir, + readPosixLauncherTarget, resolveLauncherPath, } from "../cloud/pinnedRuntime.ts"; import { projectLocationFlags, resolveCliAuthConfig } from "./config.ts"; @@ -58,13 +60,10 @@ export const findOwnedLauncher = Effect.fn("cli.uninstall.find_launcher")(functi ? shimPath : undefined; } - const linkTarget = yield* fs.readLink(input.launchedAs).pipe(Effect.option); - if (Option.isNone(linkTarget)) return undefined; - const resolved = path.resolve(path.dirname(input.launchedAs), linkTarget.value); - return isVersionExecutable(path, input.versionsDir, resolved) ? input.launchedAs : undefined; + return yield* findPosixLauncher({ launchedAs: input.launchedAs, versionsDir: input.versionsDir }); }); -/** Whether `launcher` is a link or unedited `t3.cmd` into this home's runtime/versions. */ +/** Whether `launcher` is a link, unedited `t3` script, or unedited `t3.cmd` into this home's runtime/versions. */ const ownsLauncher = Effect.fn("cli.uninstall.owns_launcher")(function* ( launcher: string, versionsDir: string, @@ -91,6 +90,7 @@ const ownsLauncher = Effect.fn("cli.uninstall.owns_launcher")(function* ( Effect.orElseSucceed(() => undefined), ), yield* fs.realPath(launcher).pipe(Effect.orElseSucceed(() => undefined)), + yield* readPosixLauncherTarget(launcher), ]; return targets.some( (target) => diff --git a/apps/server/src/cli/up.test.ts b/apps/server/src/cli/up.test.ts index 05d4185593a37..9a38208af9829 100644 --- a/apps/server/src/cli/up.test.ts +++ b/apps/server/src/cli/up.test.ts @@ -166,7 +166,8 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer)) readonly reportStatus?: boolean; /** Runs as if in a terminal, answering each question with this text. */ readonly answer?: string; - readonly prints?: string; + /** Lines the run prints, with `` standing for the test's home. */ + readonly prints?: ReadonlyArray; readonly omits?: string; readonly json?: object; readonly env: string; @@ -186,7 +187,7 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer)) }, { label: "the local network opens", - prints: "Run `t3 pair --base-dir `.", + prints: ["Run `t3 pair --base-dir `."], installed: true, env: "T3CODE_LAN=false\n", args: ["--lan"], @@ -205,6 +206,7 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer)) }, { label: "a route turns off with no service to run", + prints: ["Run `t3 --base-dir ` to use T3 Code here."], installed: false, env: "T3CODE_LAN=true\n", args: ["--no-lan"], @@ -225,7 +227,7 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer)) { label: "a failed update keeps service.env matching the old service", // The old service still runs, so no foreground server is offered. - prints: "Fix that, then run `t3 up --base-dir ` again.", + prints: ["Fix that, then run `t3 up --base-dir ` again."], installed: true, status: { current: false }, installFails: true, @@ -283,12 +285,26 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer)) restarts: 0, envAfter: "", }, + { + label: "status hints keep the caller's --base-dir", + installed: true, + reportStatus: true, + prints: [ + "Turn it off: `t3 up --base-dir --no-lan`.", + "Change access: `t3 up --base-dir `.", + ], + env: "", + args: [], + installs: 0, + restarts: 0, + envAfter: "", + }, { label: "a host override gets no --no-lan hint it would ignore", installed: true, reportStatus: true, - prints: "overridden · T3CODE_HOST=0.0.0.0", - omits: "t3 up --no-lan", + prints: ["overridden · T3CODE_HOST=0.0.0.0"], + omits: "--no-lan", env: "T3CODE_HOST=0.0.0.0\n", args: [], installs: 0, @@ -386,8 +402,8 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer)) ), ), ); - if (input.prints !== undefined) { - expect(lines.join("\n")).toContain(input.prints.replace("", baseDir)); + for (const line of input.prints ?? []) { + expect(lines.join("\n")).toContain(line.replace("", baseDir)); } if (input.omits !== undefined) { expect(lines.join("\n")).not.toContain(input.omits); diff --git a/apps/server/src/cli/up.ts b/apps/server/src/cli/up.ts index 27a972d7bfe3f..a56e478c7f45c 100644 --- a/apps/server/src/cli/up.ts +++ b/apps/server/src/cli/up.ts @@ -125,7 +125,25 @@ const CHOICES = { lan: { title: "Local network", description: "LAN and tailnet. Pairing code required." }, } satisfies Record; -const formatSetup = (setup: Setup) => { +/** The commands hints name, run with the caller's --base-dir and launcher. */ +const resolveHints = Effect.fn("cli.up.resolve_hints")(function* (baseDir: Option.Option) { + const baseDirArgs = Option.match(baseDir, { + onNone: () => [], + onSome: (dir) => ["--base-dir", dir], + }); + const command = (subcommand: string, args: ReadonlyArray = []) => + Effect.map(resolveHostCliCommand(subcommand, [...baseDirArgs, ...args]), (r) => r.command); + return { + run: yield* command(""), + pair: yield* command("pair"), + up: yield* command("up"), + upNoLan: yield* command("up", ["--no-lan"]), + connect: yield* command("connect"), + }; +}); +type Hints = Effect.Success>; + +const formatSetup = (setup: Setup, hints: Hints) => { const { service } = setup; // This home's access settings reach no service, so only the service is shown. if (service.otherBaseDir !== undefined) { @@ -152,7 +170,7 @@ const formatSetup = (setup: Setup) => { setup.connect ? "on · your T3 account" : setup.connectRevoked - ? "off · link revoked. Run `t3 connect`." + ? `off · link revoked. Run \`${hints.connect}\`.` : "off", ] as const, ]), @@ -172,7 +190,7 @@ const formatSetup = (setup: Setup) => { ? ["Anyone on your LAN or tailnet can reach the port. A pairing code is the only lock."] : []), ...(setup.lanFromOldDefault - ? ["Older installs keep LAN access on. Turn it off: `t3 up --no-lan`."] + ? [`Older installs keep LAN access on. Turn it off: \`${hints.upNoLan}\`.`] : []), ]; return [ @@ -181,18 +199,18 @@ const formatSetup = (setup: Setup) => { ].join("\n"); }; -const formatNextSteps = (routes: Routes, serviceInstalled: boolean, pairCommand: string) => +const formatNextSteps = (routes: Routes, serviceInstalled: boolean, hints: Hints) => [ ...(routes.connect ? ["On your other device, sign in to T3 Connect with the same account."] : []), - ...(routes.lan ? [`Run \`${pairCommand}\`. Use the code on your other device.`] : []), + ...(routes.lan ? [`Run \`${hints.pair}\`. Use the code on your other device.`] : []), ...(routes.connect || routes.lan ? [] : [ serviceInstalled ? "Remote access is off. T3 Code still runs here in the background." - : "Remote access is off. Run `t3` to use T3 Code here.", + : `Remote access is off. Run \`${hints.run}\` to use T3 Code here.`, ]), ].join("\n"); @@ -211,7 +229,7 @@ const setUp = Effect.fn("cli.up")(function* (flags: { onNone: () => [], onSome: (dir) => ["--base-dir", dir], }); - const { command: pairCommand } = yield* resolveHostCliCommand("pair", baseDirArgs); + const hints = yield* resolveHints(flags.baseDir); const before = yield* readSetup(); // Before any question or write: install would refuse only after both. if (before.service.otherBaseDir !== undefined) { @@ -237,7 +255,8 @@ const setUp = Effect.fn("cli.up")(function* (flags: { const interactive = !flags.yes && interactiveTerminal(); let routes = plan.routes; if (plan.ask.length > 0 && interactive) { - if (before.service.installed) yield* Console.log(`Current setup\n${formatSetup(before)}\n`); + if (before.service.installed) + yield* Console.log(`Current setup\n${formatSetup(before, hints)}\n`); const picked = yield* Prompt.run( Prompt.MultiSelect({ message: "Allow connections through:", @@ -274,7 +293,7 @@ const setUp = Effect.fn("cli.up")(function* (flags: { yield* disconnectCloud({ clearAuthorization: false }); } if (current.lan) yield* CliService.writeServiceEnv(baseDir, { T3CODE_LAN: "false" }); - return yield* Console.log(formatNextSteps(routes, false, pairCommand)); + return yield* Console.log(formatNextSteps(routes, false, hints)); } const { installedVersion } = before.service; const newer = @@ -384,9 +403,8 @@ const setUp = Effect.fn("cli.up")(function* (flags: { if ("failed" in result && before.service.installed) { // The old service still runs, so a foreground server would clash with it. yield* restoreServiceEnv; - const { command } = yield* resolveHostCliCommand("up", baseDirArgs); yield* Console.log( - `Service setup failed: ${result.failed} Fix that, then run \`${command}\` again.`, + `Service setup failed: ${result.failed} Fix that, then run \`${hints.up}\` again.`, ); } else if ("failed" in result) { yield* printServeCommand(`Service setup failed: ${result.failed}`); @@ -429,10 +447,10 @@ const setUp = Effect.fn("cli.up")(function* (flags: { const after = yield* readSetup(); yield* Console.log( // From what took effect: a failed service step can undo a choice. - `\nThis machine\n${formatSetup(after)}\n\n${formatNextSteps( + `\nThis machine\n${formatSetup(after, hints)}\n\n${formatNextSteps( { connect: after.connect === true, lan: after.lan }, after.service.installed, - pairCommand, + hints, )}`, ); }); @@ -536,12 +554,12 @@ export const statusCommand = Command.make("status", { ), ), ); + if (flags.json) return yield* Console.log(setupJson(setup)); + const hints = yield* resolveHints(flags.baseDir); yield* Console.log( - flags.json - ? setupJson(setup) - : setup.service.otherBaseDir !== undefined - ? `This machine\n${formatSetup(setup)}` - : `This machine\n${formatSetup(setup)}\n\nChange access: \`t3 up\`.`, + setup.service.otherBaseDir !== undefined + ? `This machine\n${formatSetup(setup, hints)}` + : `This machine\n${formatSetup(setup, hints)}\n\nChange access: \`${hints.up}\`.`, ); }), ), diff --git a/apps/server/src/cli/update.test.ts b/apps/server/src/cli/update.test.ts index 2fc2e29b6223c..464dfd0523a19 100644 --- a/apps/server/src/cli/update.test.ts +++ b/apps/server/src/cli/update.test.ts @@ -12,6 +12,7 @@ import * as Path from "effect/Path"; import * as TestClock from "effect/testing/TestClock"; import * as TestConsole from "effect/testing/TestConsole"; import { HttpClient, HttpClientResponse } from "effect/http"; +import * as ChildProcess from "effect/process/ChildProcess"; import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; import { afterEach, vi } from "vite-plus/test"; @@ -19,7 +20,9 @@ import packageJson from "../../package.json" with { type: "json" }; import * as BootService from "../cloud/bootService.ts"; import { pinnedRuntimePaths, + posixLauncherScript, prunePinnedRuntimes, + readPosixLauncherTarget, resolveLauncherPath, } from "../cloud/pinnedRuntime.ts"; import * as ManagedPolicy from "../policy/ManagedPolicy.ts"; @@ -196,7 +199,7 @@ it.layer(NodeServices.layer)("t3 update under a managed policy", (it) => { }); it.layer(NodeServices.layer)("t3 update launcher", (it) => { - it.effect("repoints a symlink that lives in a runtime versions tree", () => + it.effect("replaces a legacy symlink with a script that execs the new version", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; @@ -218,8 +221,81 @@ it.layer(NodeServices.layer)("t3 update launcher", (it) => { }); assert.deepStrictEqual(Option.getOrUndefined(repointed), launcher); - assert.equal(yield* fs.readLink(launcher), newExe); - }).pipe(Effect.provideService(HostProcess.Platform, "linux")), + assert.equal(yield* fs.readFileString(launcher), posixLauncherScript(newExe)); + // Replaced, not written through. + assert.equal(yield* fs.readFileString(oldExe), ""); + }).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")), + ); + + it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")( + "finds the script that ran this executable and rewrites it, keeping aliases and arguments", + () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; + const root = yield* fs.realPath( + yield* fs.makeTempDirectoryScoped({ prefix: "t3-update-" }), + ); + // A quote and a space in the home path exercise the script's quoting. + const home = path.join(root, "it's home"); + const entry = (version: string) => path.join(home, "runtime/versions", version, "t3"); + for (const version of ["1.0.0", "2.0.0"]) { + yield* fs.makeDirectory(path.dirname(entry(version)), { recursive: true }); + yield* fs.writeFileString(entry(version), '#!/bin/sh\nprintf \'[%s]\\n\' "$0" "$@"\n', { + mode: 0o755, + }); + } + const launcher = path.join(root, "bin/t3"); + const alias = path.join(root, "alias/t3"); + yield* fs.makeDirectory(path.dirname(launcher), { recursive: true }); + yield* fs.makeDirectory(path.dirname(alias), { recursive: true }); + yield* fs.writeFileString(launcher, posixLauncherScript(entry("1.0.0")), { mode: 0o755 }); + yield* fs.symlink(launcher, alias); + const repoint = (environment: Record) => + repointLauncher({ + // The script execs by absolute path, so argv0 names the executable. + launchedAs: entry("1.0.0"), + baseDir: home, + targetEntryPath: entry("2.0.0"), + }).pipe(Effect.provideService(HostProcess.Environment, environment)); + + assert.equal( + Option.getOrUndefined(yield* repoint({ PATH: path.dirname(alias) })), + launcher, + ); + assert.equal(yield* fs.readLink(alias), launcher); + const args = ["two words", "it's quoted", "", "*", "$HOME"]; + assert.equal( + yield* spawner.string(ChildProcess.make(alias, args, { cwd: root })), + [entry("2.0.0"), ...args].map((arg) => `[${arg}]\n`).join(""), + ); + + // Like the shell, a relative PATH entry resolves from the working directory, and a + // script nobody can execute is passed over. + const unrunnable = path.join(root, "unrunnable/t3"); + yield* fs.makeDirectory(path.dirname(unrunnable), { recursive: true }); + yield* fs.writeFileString(unrunnable, posixLauncherScript(entry("2.0.0")), { mode: 0o644 }); + yield* fs.writeFileString(launcher, posixLauncherScript(entry("2.0.0")), { mode: 0o755 }); + const relative = yield* repointLauncher({ + launchedAs: entry("2.0.0"), + baseDir: home, + targetEntryPath: entry("1.0.0"), + }).pipe( + Effect.provideService(HostProcess.Environment, { + PATH: `${path.dirname(unrunnable)}:bin`, + }), + Effect.provideService(HostProcess.WorkingDirectory, root), + ); + assert.equal(Option.getOrUndefined(relative), launcher); + assert.equal(yield* fs.readFileString(unrunnable), posixLauncherScript(entry("2.0.0"))); + + // An edited script is the user's own wrapper now. + const edited = `${posixLauncherScript(entry("1.0.0"))}# mine\n`; + yield* fs.writeFileString(launcher, edited); + assert.isTrue(Option.isNone(yield* repoint({ PATH: path.dirname(launcher) }))); + assert.equal(yield* fs.readFileString(launcher), edited); + }).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")), ); it.effect.each([ @@ -280,10 +356,9 @@ it.layer(NodeServices.layer)("t3 update launcher", (it) => { ); assert.deepStrictEqual(removed, ["1.0.0"]); - // macOS temp dirs sit behind the /var -> /private/var symlink, so compare canonical paths. assert.equal( - yield* fs.realPath(launcher), - yield* fs.realPath(path.join(root, "runtime/versions/2.0.0/t3")), + yield* readPosixLauncherTarget(launcher), + path.join(root, "runtime/versions/2.0.0/t3"), ); }).pipe(Effect.provideService(HostProcess.Platform, "linux")), ); @@ -363,7 +438,7 @@ it.layer(NodeServices.layer)("t3 update launcher", (it) => { Effect.exit, ); - const repointed = (yield* fs.readLink(launcher)) === newExe; + const repointed = (yield* readPosixLauncherTarget(launcher)) === newExe; const recorded = (yield* fs .readFileString(launchersFile) .pipe(Effect.orElseSucceed(() => ""))) diff --git a/apps/server/src/cli/update.ts b/apps/server/src/cli/update.ts index 9a4fbc67b4ef0..82f2dbbde3b61 100644 --- a/apps/server/src/cli/update.ts +++ b/apps/server/src/cli/update.ts @@ -24,6 +24,7 @@ import packageJson from "../../package.json" with { type: "json" }; import * as BootService from "../cloud/bootService.ts"; import { ensurePinnedRuntimeInstalled, + findPosixLauncher, launcherOwnsVersionsDir, pinnedRuntimeCommand, isUnpublishedRelease, @@ -34,6 +35,7 @@ import { recordPinnedRuntimeLauncher, resolveLauncherPath, windowsShimTarget, + writePosixLauncher, } from "../cloud/pinnedRuntime.ts"; import { compareExactServiceVersions, isExactServiceVersion } from "../cloud/serviceProtocol.ts"; import * as ManagedPolicy from "../policy/ManagedPolicy.ts"; @@ -46,6 +48,7 @@ import * as CliService from "./service.ts"; export class CliUpdateError extends Schema.TaggedError()("CliUpdateError", { reason: Schema.String, + cause: Schema.optional(Schema.Defect()), }) { override get message(): string { return this.reason; @@ -179,20 +182,20 @@ export const repointLauncher = Effect.fn("cli.update.repoint_launcher")(function return Option.some(shimPath); } - const linkTarget = yield* fs.readLink(input.launchedAs).pipe(Effect.option); - if (Option.isNone(linkTarget)) return Option.none(); - const resolvedTarget = path.resolve(path.dirname(input.launchedAs), linkTarget.value); - if (!ownsTarget(resolvedTarget)) return Option.none(); - yield* remember(input.launchedAs); - const tempLink = `${input.launchedAs}.${process.pid}.tmp`; - yield* fs.symlink(input.targetEntryPath, tempLink).pipe( - Effect.andThen(fs.rename(tempLink, input.launchedAs)), + const launcher = yield* findPosixLauncher({ launchedAs: input.launchedAs, versionsDir }); + if (launcher === undefined) return Option.none(); + yield* remember(launcher); + yield* writePosixLauncher({ + launcher, + executable: input.targetEntryPath, + exclusive: false, + }).pipe( Effect.mapError( - () => - new CliUpdateError({ reason: `Could not repoint the t3 launcher at ${input.launchedAs}.` }), + (cause) => + new CliUpdateError({ reason: `Could not repoint the t3 launcher at ${launcher}.`, cause }), ), ); - return Option.some(input.launchedAs); + return Option.some(launcher); }); /** Windows executables see their own path, so find the installer shim by matching t3.cmd targets on PATH. */ diff --git a/apps/server/src/cloud/CliState.test.ts b/apps/server/src/cloud/CliState.test.ts index 182bcd43f367a..ba1bbcb976f49 100644 --- a/apps/server/src/cloud/CliState.test.ts +++ b/apps/server/src/cloud/CliState.test.ts @@ -14,6 +14,7 @@ import { CLOUD_LINK_REVOKED, CLOUD_LINKED_USER_ID, CLOUD_MINT_PUBLIC_KEY, + HOLD_WEBHOOKS_WHILE_OFFLINE_SECRET, PUBLISH_AGENT_ACTIVITY_SECRET, RELAY_ENVIRONMENT_CREDENTIAL_SECRET, RELAY_ISSUER_SECRET, @@ -29,6 +30,7 @@ const persistedCloudLinkSecrets = [ CLOUD_ENDPOINT_RUNTIME_CONFIG, CLOUD_ENDPOINT_CONFIRMED_ORIGIN, PUBLISH_AGENT_ACTIVITY_SECRET, + HOLD_WEBHOOKS_WHILE_OFFLINE_SECRET, ] as const; const layerTest = () => diff --git a/apps/server/src/cloud/CliState.ts b/apps/server/src/cloud/CliState.ts index b89f3240fbd27..8c4d9d05c748c 100644 --- a/apps/server/src/cloud/CliState.ts +++ b/apps/server/src/cloud/CliState.ts @@ -8,6 +8,7 @@ import { CLOUD_LINK_REVOKED, CLOUD_LINKED_USER_ID, CLOUD_MINT_PUBLIC_KEY, + HOLD_WEBHOOKS_WHILE_OFFLINE_SECRET, PUBLISH_AGENT_ACTIVITY_SECRET, RELAY_ENVIRONMENT_CREDENTIAL_SECRET, RELAY_ISSUER_SECRET, @@ -85,6 +86,7 @@ export const clearPersistedCloudLink = Effect.gen(function* () { secrets.remove(CLOUD_ENDPOINT_RUNTIME_CONFIG), secrets.remove(CLOUD_ENDPOINT_CONFIRMED_ORIGIN), secrets.remove(PUBLISH_AGENT_ACTIVITY_SECRET), + secrets.remove(HOLD_WEBHOOKS_WHILE_OFFLINE_SECRET), secrets.remove(CLOUD_LINK_REVOKED), ], { concurrency: "unbounded" }, diff --git a/apps/server/src/cloud/ManagedEndpointRuntime.test.ts b/apps/server/src/cloud/ManagedEndpointRuntime.test.ts index dae9d870c8a75..c7caf65264631 100644 --- a/apps/server/src/cloud/ManagedEndpointRuntime.test.ts +++ b/apps/server/src/cloud/ManagedEndpointRuntime.test.ts @@ -180,6 +180,21 @@ describe("CloudManagedEndpointRuntime", () => { expect( ManagedEndpointRuntime.classifyRelayClientOutput("2026-06-17T02:00:00Z PNC runtime panic"), ).toBe("warning"); + for (const prefix of ["", "Request failed "]) { + expect( + ManagedEndpointRuntime.classifyRelayClientOutput( + `2026-06-17T02:00:00Z ERR ${prefix}error="Incoming request ended abruptly: context canceled" connIndex=0`, + ), + ).toBe("debug"); + } + for (const line of [ + '2026-06-17T02:00:00Z ERR error="Incoming request ended abruptly: context deadline exceeded" connIndex=0', + '2026-06-17T02:00:00Z ERR Failed to serve tunnel connection error="context canceled"', + '2026-06-17T02:00:00Z FTL error="Incoming request ended abruptly: context canceled"', + '2026-06-17T02:00:00Z PNC error="Incoming request ended abruptly: context canceled"', + ]) { + expect(ManagedEndpointRuntime.classifyRelayClientOutput(line)).toBe("warning"); + } }); it("classifies connection unregistration apart from registration and routine output", () => { diff --git a/apps/server/src/cloud/ManagedEndpointRuntime.ts b/apps/server/src/cloud/ManagedEndpointRuntime.ts index 6d14e23182344..ffc533486d3ea 100644 --- a/apps/server/src/cloud/ManagedEndpointRuntime.ts +++ b/apps/server/src/cloud/ManagedEndpointRuntime.ts @@ -168,6 +168,14 @@ export function classifyRelayClientOutput( if (/\bRegistered tunnel connection\b/iu.test(line)) { return "connected"; } + // cloudflared logs a browser closing its request mid-stream as ERR; the tunnel itself is fine. + if ( + /\bERR\s+(?:Request failed\s+)?error="Incoming request ended abruptly: context canceled"(?:\s|$)/u.test( + line, + ) + ) { + return "debug"; + } // cloudflared uses zerolog FTL/PNC for fatal/panic; classify them above debug too. if (/\b(?:ERR|WRN|FTL|PNC)\b/u.test(line)) { return "warning"; diff --git a/apps/server/src/cloud/bootService.ts b/apps/server/src/cloud/bootService.ts index 0a884948a97af..0f94aaad95bac 100644 --- a/apps/server/src/cloud/bootService.ts +++ b/apps/server/src/cloud/bootService.ts @@ -652,7 +652,13 @@ const BootServiceProblem = Schema.Literals([ ]); type BootServiceProblem = typeof BootServiceProblem.Type; -export function formatBootServiceProblem(problem: BootServiceProblem): string { +export function formatBootServiceProblem( + problem: BootServiceProblem, + commands: { readonly install: string; readonly restart: string } = { + install: "t3 service install", + restart: "t3 service restart", + }, +): string { switch (problem) { case "user-manager-unavailable": return "Cannot reach the systemd user manager. Run `systemctl --user status` in a login session for the service user. Install your distribution's systemd user-session support if it is missing; do not run T3 with sudo."; @@ -661,11 +667,11 @@ export function formatBootServiceProblem(problem: BootServiceProblem): string { case "linger-disabled": return 'Lingering is disabled. T3 Code will stop when your last login session ends and will not start at boot. Run `sudo loginctl enable-linger "$(id -un)"` on this machine, then retry the service command as your normal user.'; case "service-disabled": - return "The service is not enabled to start automatically. Run `t3 service install` to repair it."; + return `The service is not enabled to start automatically. Run \`${commands.install}\` to repair it.`; case "service-stopped": - return "The service is not running. Check the service log (on Linux, also `systemctl --user status t3code.service`), then run `t3 service install`."; + return `The service is not running. Check the service log (on Linux, also \`systemctl --user status t3code.service\`), then run \`${commands.install}\`.`; case "restart-pending": - return "A newer version is installed but the service is still running the previous one. Run `t3 service restart` to switch."; + return `A newer version is installed but the service is still running the previous one. Run \`${commands.restart}\` to switch.`; } } diff --git a/apps/server/src/cloud/pinnedRuntime.ts b/apps/server/src/cloud/pinnedRuntime.ts index b698be2727752..5f7e170530d79 100644 --- a/apps/server/src/cloud/pinnedRuntime.ts +++ b/apps/server/src/cloud/pinnedRuntime.ts @@ -514,6 +514,107 @@ export const windowsShimTarget = (contents: string) => /^"([^"]+)"/m.exec(conten export const generatedWindowsShimTarget = (contents: string) => /^@echo off\r?\n"([^"]+)" %\*\s*$/.exec(contents)?.[1]; +/** + * The `t3` script the install scripts and `t3 update` write on macOS and Linux. Exec by absolute + * path: through a symlink, argv0 is the link, and the Cursor SDK looks for its helpers beside it. + */ +export const posixLauncherScript = (executable: string) => + `#!/bin/sh\nexec '${executable.replaceAll("'", "'\\''")}' "$@"\n`; + +/** The executable a script exactly as {@link posixLauncherScript} writes it runs; never a user's wrapper. */ +export const readPosixLauncherTarget = Effect.fn("cloud.pinned_runtime.read_posix_launcher")( + function* (launcher: string) { + const fs = yield* FileSystem.FileSystem; + // Stat first so a runtime binary found the same way is never read whole. + const info = yield* fs.stat(launcher).pipe(Effect.option); + if (Option.isNone(info) || info.value.type !== "File" || info.value.size > 4096n) { + return undefined; + } + const contents = yield* fs.readFileString(launcher).pipe(Effect.option); + if (Option.isNone(contents)) return undefined; + const quoted = /^#!\/bin\/sh\nexec '((?:[^']|'\\'')*)' "\$@"\n$/.exec(contents.value)?.[1]; + return quoted?.replaceAll("'\\''", "'"); + }, +); + +/** + * The `t3` this executable was started through on macOS or Linux: a legacy symlink into this + * home's runtime/versions, or, since a script execs the executable and argv0 names it, an unedited + * {@link posixLauncherScript} that runs it. + */ +export const findPosixLauncher = Effect.fn("cloud.pinned_runtime.find_posix_launcher")( + function* (input: { readonly launchedAs: string; readonly versionsDir: string }) { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const environment = yield* HostProcess.Environment; + const linkTarget = yield* fs.readLink(input.launchedAs).pipe(Effect.option); + if (Option.isSome(linkTarget)) { + const resolved = path.resolve(path.dirname(input.launchedAs), linkTarget.value); + return isVersionExecutable(path, input.versionsDir, resolved) ? input.launchedAs : undefined; + } + const realPath = (file: string) => + fs.realPath(file).pipe(Effect.orElseSucceed(() => undefined)); + const executable = yield* realPath(input.launchedAs); + const realVersionsDir = yield* realPath(input.versionsDir); + if ( + executable === undefined || + realVersionsDir === undefined || + !isVersionExecutable(path, realVersionsDir, executable) + ) { + return undefined; + } + const recorded = yield* fs + .readFileString(path.join(path.dirname(input.versionsDir), "launchers")) + .pipe(Effect.orElseSucceed(() => "")); + const cwd = yield* HostProcess.WorkingDirectory; + const candidates = [ + // PATH first, resolved like the shell does: it decides which t3 the user runs. + ...(environment["PATH"] ?? "") + .split(":") + .filter((directory) => directory.length > 0) + .map((directory) => path.resolve(cwd, directory, "t3")), + path.join( + environment["T3CODE_INSTALL_BIN_DIR"] || + path.join(yield* HostProcess.HomeDirectory, ".local", "bin"), + "t3", + ), + ...recorded.split(/\r?\n/), + ].filter((candidate) => path.isAbsolute(candidate)); + for (const candidate of candidates) { + // The script itself, so an alias that links to it keeps working. + const script = yield* realPath(candidate); + if (script === undefined) continue; + // Like the shell, pass over a script nobody can execute. + const info = yield* fs.stat(script).pipe(Effect.option); + if (Option.isNone(info) || (info.value.mode & 0o111) === 0) continue; + const target = yield* readPosixLauncherTarget(script); + if (target !== undefined && (yield* realPath(target)) === executable) return script; + } + return undefined; + }, +); + +/** Writes {@link posixLauncherScript} at `launcher` in one rename, replacing a legacy symlink rather than writing through it. */ +export const writePosixLauncher = Effect.fn("cloud.pinned_runtime.write_posix_launcher")( + function* (input: { + readonly launcher: string; + readonly executable: string; + /** Fail instead of replacing a file that is there, even one created after the caller looked. */ + readonly exclusive: boolean; + }) { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const staging = yield* fs.makeTempFileScoped({ + directory: path.dirname(input.launcher), + prefix: ".t3-", + }); + yield* fs.writeFileString(staging, posixLauncherScript(input.executable)); + yield* fs.chmod(staging, 0o755); + yield* input.exclusive ? fs.link(staging, input.launcher) : fs.rename(staging, input.launcher); + }, + Effect.scoped, +); + /** Where the `t3` launchers written by the install scripts and `t3 update` point. */ const shellLauncherTargets = Effect.fn("cloud.pinned_runtime.shell_launcher_targets")(function* ( baseDir: string, @@ -544,7 +645,7 @@ const shellLauncherTargets = Effect.fn("cloud.pinned_runtime.shell_launcher_targ const unrecorded: Array = []; const targets: Array = []; for (const launcher of [...recorded, ...found]) { - const target = + const resolved = platform === "win32" ? yield* fs.readFileString(launcher).pipe( Effect.map(windowsShimTarget), @@ -552,13 +653,20 @@ const shellLauncherTargets = Effect.fn("cloud.pinned_runtime.shell_launcher_targ ) : // Fully resolved, through chained links and a symlinked T3 home alike. yield* fs.realPath(launcher).pipe(Effect.orElseSucceed(() => undefined)); - if (target === undefined) continue; + if (resolved === undefined) continue; + const scriptTarget = + platform === "win32" ? undefined : yield* readPosixLauncherTarget(resolved); + const target = + scriptTarget === undefined + ? resolved + : yield* fs.realPath(scriptTarget).pipe(Effect.orElseSucceed(() => scriptTarget)); // A runtime's own entry, on the macOS service PATH or recorded by an earlier prune, is no launcher. // It is named as `versions//t3`, and neither it nor its directory is a link. const entryDir = path.dirname(path.resolve(launcher)); const parent = path.dirname(entryDir); if ( platform !== "win32" && + scriptTarget === undefined && (parent === versionsDir || parent === realVersionsDir) && target === path.join(realVersionsDir, path.basename(entryDir), path.basename(launcher)) ) { diff --git a/apps/server/src/cloud/serviceLauncherClient.test.ts b/apps/server/src/cloud/serviceLauncherClient.test.ts index f8de584ff2c8b..338ff32a3c16e 100644 --- a/apps/server/src/cloud/serviceLauncherClient.test.ts +++ b/apps/server/src/cloud/serviceLauncherClient.test.ts @@ -14,6 +14,7 @@ import { type ServiceLauncherChildMessage, type ServiceLauncherParentMessage, } from "./serviceProtocol.ts"; +import { posixLauncherScript } from "./pinnedRuntime.ts"; import * as ServiceLauncherClient from "./serviceLauncherClient.ts"; class FakeLauncherProcess { @@ -98,7 +99,7 @@ it.effect.each([3, SERVICE_LAUNCHER_PROTOCOL])( }; host.emit({ type: "committed", updateId: committed.id }); expect(yield* Fiber.join(prepared)).toEqual(committed); - assert.equal(yield* fs.readLink(installed), exe("1.1.0")); + assert.equal(yield* fs.readFileString(installed), posixLauncherScript(exe("1.1.0"))); // A later start replays the stored outcome; user pins made since then stay put. yield* fs.remove(installed); @@ -224,7 +225,7 @@ it.layer(NodeServices.layer)("self-update launchers", (it) => { yield* fs.writeFileString(file, ""); } yield* fs.makeDirectory(path.dirname(installed), { recursive: true }); - yield* fs.symlink(exe("1.5.0"), installed); + yield* fs.writeFileString(installed, posixLauncherScript(exe("1.5.0")), { mode: 0o755 }); yield* fs.makeDirectory(path.dirname(onPath), { recursive: true }); yield* fs.symlink(exe("1.0.0"), onPath); @@ -236,8 +237,9 @@ it.layer(NodeServices.layer)("self-update launchers", (it) => { }), ); - assert.equal(yield* fs.readLink(installed), exe("2.0.0")); - assert.equal(yield* fs.readLink(onPath), exe("2.0.0")); + // The script is rewritten and the legacy symlink becomes one. + assert.equal(yield* fs.readFileString(installed), posixLauncherScript(exe("2.0.0"))); + assert.equal(yield* fs.readFileString(onPath), posixLauncherScript(exe("2.0.0"))); }).pipe(Effect.provideService(HostProcess.Platform, "linux")), ); @@ -291,7 +293,7 @@ it.layer(NodeServices.layer)("self-update launchers", (it) => { assert.lengthOf(warnings, 1); assert.include(JSON.stringify(warnings), installed); assert.equal(yield* fs.readLink(installed), exe("1.0.0")); - assert.equal(yield* fs.readLink(onPath), exe("2.0.0")); + assert.equal(yield* fs.readFileString(onPath), posixLauncherScript(exe("2.0.0"))); }).pipe(Effect.provideService(HostProcess.Platform, "linux")), ); diff --git a/apps/server/src/cloud/serviceLauncherClient.ts b/apps/server/src/cloud/serviceLauncherClient.ts index d29c565b644ff..e8f8941f3f9e8 100644 --- a/apps/server/src/cloud/serviceLauncherClient.ts +++ b/apps/server/src/cloud/serviceLauncherClient.ts @@ -10,7 +10,12 @@ import * as Path from "effect/Path"; import * as Schema from "effect/Schema"; import packageJson from "../../package.json" with { type: "json" }; -import { pinnedRuntimePaths, pinnedRuntimeVersionsDir } from "./pinnedRuntime.ts"; +import { + pinnedRuntimePaths, + pinnedRuntimeVersionsDir, + readPosixLauncherTarget, + writePosixLauncher, +} from "./pinnedRuntime.ts"; import { decodeServiceLauncherContext, decodeServiceLauncherParentMessage, @@ -110,7 +115,7 @@ export class ServiceLauncherClient extends Context.Service< readonly targetVersion: string; readonly dbPath: string; }) => Effect.Effect; - /** Owned `t3` symlinks move to the new runtime only when this start commits the trial. */ + /** Owned `t3` launchers move to the new runtime only when this start commits the trial. */ readonly prepareTrial: (input: { readonly baseDir: string; }) => Effect.Effect; @@ -189,11 +194,12 @@ export const repointLaunchers = Effect.fn("cloud.service_launcher_client.repoint () => Effect.succeedNone, ), ); - if (Option.isNone(linkTarget)) return; - const relative = path.relative( - versionsDir, - path.resolve(path.dirname(launchedAs), linkTarget.value), - ); + // A legacy symlink, or the script install.sh and `t3 update` write. + const current = Option.isSome(linkTarget) + ? path.resolve(path.dirname(launchedAs), linkTarget.value) + : yield* readPosixLauncherTarget(launchedAs); + if (current === undefined) return; + const relative = path.relative(versionsDir, current); if ( relative.length === 0 || relative === ".." || @@ -201,13 +207,11 @@ export const repointLaunchers = Effect.fn("cloud.service_launcher_client.repoint path.isAbsolute(relative) ) return; - const temporary = yield* fs.makeTempDirectoryScoped({ - directory: path.dirname(launchedAs), - prefix: ".t3-launcher-", + yield* writePosixLauncher({ + launcher: launchedAs, + executable: target.entryPath, + exclusive: false, }); - const link = path.join(temporary, "t3"); - yield* fs.symlink(target.entryPath, link); - yield* fs.rename(link, launchedAs); }), ).pipe( Effect.catchCause((cause) => diff --git a/apps/server/src/desktopUpdate/DesktopAppUpdate.test.ts b/apps/server/src/desktopUpdate/DesktopAppUpdate.test.ts index 56c08a84618af..b38dc6efb66d3 100644 --- a/apps/server/src/desktopUpdate/DesktopAppUpdate.test.ts +++ b/apps/server/src/desktopUpdate/DesktopAppUpdate.test.ts @@ -42,6 +42,7 @@ function report( terminal?: { readonly outcome: DesktopUpdateStatusReport["outcome"]; readonly reason?: string; + readonly preparedRequestId?: string; }, ): DesktopUpdateStatusReport { return { @@ -50,6 +51,9 @@ function report( requestId, ...(terminal?.outcome === undefined ? {} : { outcome: terminal.outcome }), ...(terminal?.reason === undefined ? {} : { reason: terminal.reason }), + ...(terminal?.preparedRequestId === undefined + ? {} + : { preparedRequestId: terminal.preparedRequestId }), state, }; } @@ -133,6 +137,28 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("desktop app upda }), ); + it.effect("commits the original preparation when a retry joins its downloaded update", () => + Effect.gen(function* () { + const { service } = yield* makeHarness({ + reports: (requestId) => [ + report(requestId, makeState({ status: "downloaded", downloadedVersion: "1.2.4" }), { + outcome: "ready-to-install", + preparedRequestId: "original-preparation", + }), + report("original-preparation", makeState({ status: "downloaded" }), { + outcome: "failed", + reason: "installer refused", + }), + ], + }); + const prepared = yield* service.run(() => Effect.void); + expect(prepared.desktopUpdateToken).toBe("original-preparation"); + expect( + (yield* service.commit(prepared.desktopUpdateToken ?? "missing").pipe(Effect.flip)).reason, + ).toBe("installer refused"); + }), + ); + it.effect("succeeds without an install when a track switch finds the host current", () => Effect.gen(function* () { const { service } = yield* makeHarness({ diff --git a/apps/server/src/desktopUpdate/DesktopAppUpdate.ts b/apps/server/src/desktopUpdate/DesktopAppUpdate.ts index 38cf7cab0ff2a..4963c09b2f6e5 100644 --- a/apps/server/src/desktopUpdate/DesktopAppUpdate.ts +++ b/apps/server/src/desktopUpdate/DesktopAppUpdate.ts @@ -121,7 +121,11 @@ export const make = Effect.fn("desktopUpdate.desktopAppUpdate.make")(function* ( targetVersion, }); yield* Ref.set(inFlight, false); - return { targetVersion, method: "desktop-app" as const, desktopUpdateToken: requestId }; + return { + targetVersion, + method: "desktop-app" as const, + desktopUpdateToken: report.preparedRequestId ?? requestId, + }; } if (report.outcome === "up-to-date") { // A switch's host already on the newest build of its track only saved the track. diff --git a/apps/server/src/device/DeviceHost.ts b/apps/server/src/device/DeviceHost.ts index eafa3b6319de3..898b028c4329c 100644 --- a/apps/server/src/device/DeviceHost.ts +++ b/apps/server/src/device/DeviceHost.ts @@ -139,7 +139,7 @@ export interface DeviceOperations { deviceId: string, ) => Effect.Effect; /** - * Set by hosts that end devices on their own (EAS stops sessions at their + * Set by hosts that end devices on their own (Expo stops sessions at their * limits), so a session on a device the host stops listing is dropped. */ readonly endsDevices?: boolean; diff --git a/apps/server/src/device/DeviceHubProxy.ts b/apps/server/src/device/DeviceHubProxy.ts index 56132433c8869..0d606df1d513a 100644 --- a/apps/server/src/device/DeviceHubProxy.ts +++ b/apps/server/src/device/DeviceHubProxy.ts @@ -155,7 +155,7 @@ const proxyHttp = Effect.fn("DeviceHubProxy.proxyHttp")(function* ( const response = yield* httpClient.execute(upstreamRequest); const headers: Record = {}; for (const [name, value] of Object.entries(response.headers)) { - // A cookie from an upstream such as an EAS session's preview host would land on T3's origin. + // A cookie from an upstream such as an Expo Simulators session's preview host would land on T3's origin. if ( name === "content-encoding" || name === "transfer-encoding" || diff --git a/apps/server/src/device/DeviceService.test.ts b/apps/server/src/device/DeviceService.test.ts index 71c7b6994ca86..2d7255e2921cb 100644 --- a/apps/server/src/device/DeviceService.test.ts +++ b/apps/server/src/device/DeviceService.test.ts @@ -347,7 +347,7 @@ const fixture = Effect.fn("fixture")(function* ( }), ), ); - // The host ending a device on its own, as EAS does at a session's limits. + // The host ending a device on its own, as Expo does at a session's limits. const endDevice = () => { booted = false; }; @@ -677,77 +677,106 @@ const layerSecretResolver = Layer.succeed(SecretResolver.SecretResolver, { invalidate: () => Effect.void, }); -it.effect.each([1, 9009])( - "Test connection preserves Windows exit %s guidance across the wire", - (exitCode) => - Effect.gen(function* () { - const { host } = yield* fixture(); - const service = yield* DeviceService.make.pipe( - Effect.provideService(DeviceHost.DeviceHost, host), - Effect.provideService(ProcessRunner.ProcessRunner, { - run: () => Effect.die("Testing SSH does not install local tools"), - }), - Effect.provideService( - HttpClient.HttpClient, - HttpClient.make(() => Effect.die("Testing SSH makes no hub requests")), - ), - Effect.provideService( - ChildProcessSpawner.ChildProcessSpawner, - ChildProcessSpawner.make((command) => { - if (command._tag !== "StandardCommand") return Effect.die("Unexpected command"); - const config = command.args.includes("-G"); - const windows = command.args.includes("echo %OS% $env:OS"); - return Effect.succeed( - ChildProcessSpawner.makeHandle({ - pid: ChildProcessSpawner.ProcessId(123), - stdout: Stream.make( - new TextEncoder().encode( - config ? "hostname 192.0.2.1\nport 22\n" : windows ? "Windows_NT\r\n" : "", - ), - ), - stderr: Stream.make(new TextEncoder().encode("private SSH diagnostics")), - all: Stream.empty, - exitCode: Effect.succeed( - ChildProcessSpawner.ExitCode(config || windows ? 0 : exitCode), +const WINDOWS_HOST_GUIDANCE = new SshDeviceHost.SshWindowsHostError({ cause: null }).message; + +it.effect.each([ + { + name: "Windows exit 1", + exitCode: 1, + stderr: "private SSH diagnostics", + reason: WINDOWS_HOST_GUIDANCE, + }, + { + name: "Windows exit 9009", + exitCode: 9009, + stderr: "private SSH diagnostics", + reason: WINDOWS_HOST_GUIDANCE, + }, + { + name: "untrusted host key", + exitCode: 255, + stderr: "Host key verification failed.\r\n", + reason: + "SSH encountered an untrusted host key while connecting to probe.test from the machine running this T3 Code server. Connect once with ssh from there to identify the host and verify its key before accepting it.", + }, + { + name: "unrecognized SSH failure", + exitCode: 255, + stderr: "private SSH diagnostics", + reason: "Could not connect to this host over SSH.", + }, +])("Test connection preserves $name guidance across the wire", ({ exitCode, stderr, reason }) => + Effect.gen(function* () { + const { host } = yield* fixture(); + const service = yield* DeviceService.make.pipe( + Effect.provideService(DeviceHost.DeviceHost, host), + Effect.provideService(ProcessRunner.ProcessRunner, { + run: () => Effect.die("Testing SSH does not install local tools"), + }), + Effect.provideService( + HttpClient.HttpClient, + HttpClient.make(() => Effect.die("Testing SSH makes no hub requests")), + ), + Effect.provideService( + ChildProcessSpawner.ChildProcessSpawner, + ChildProcessSpawner.make((command) => { + if (command._tag !== "StandardCommand") return Effect.die("Unexpected command"); + const config = command.args.includes("-G"); + const windows = command.args.includes("echo %OS% $env:OS"); + return Effect.succeed( + ChildProcessSpawner.makeHandle({ + pid: ChildProcessSpawner.ProcessId(123), + stdout: Stream.make( + new TextEncoder().encode( + config ? "hostname 192.0.2.1\nport 22\n" : windows ? "Windows_NT\r\n" : "", ), - isRunning: Effect.succeed(false), - kill: () => Effect.void, - stdin: Sink.drain, - getInputFd: () => Sink.drain, - getOutputFd: () => Stream.empty, - unref: Effect.succeed(Effect.void), - }), - ); - }), - ), - ); - const error = yield* service - .testHost({ kind: "ssh", id: "windows", label: "Windows test", target: "windows.test" }) - .pipe(Effect.flip); - expect(error).toMatchObject({ - _tag: "DeviceHostUnavailableError", - hostId: "windows", - hostLabel: "Windows test", - reason: - "Connected over SSH, but this host runs Windows, and SSH device hosts currently need macOS or Linux. To use this machine's devices, run T3 Code on it and connect to that environment.", - }); - const codec = Schema.fromJsonString(DeviceError); - const decoded = yield* Schema.decodeEffect(codec)(yield* Schema.encodeEffect(codec)(error)); - expect(decoded.message).toBe( - "Windows test is unavailable: Connected over SSH, but this host runs Windows, and SSH device hosts currently need macOS or Linux. To use this machine's devices, run T3 Code on it and connect to that environment.", - ); - expect(decoded.message).not.toContain("private SSH diagnostics"); - }).pipe( - Effect.provide( - Layer.mergeAll( - ServerSettings.layerTest(), - layerSecretResolver, - ServerConfig.layerTest(process.cwd(), { prefix: "t3-device-probe-" }), - NodeCrypto.layer, - NetService.layer, - ).pipe(Layer.provideMerge(NodeServices.layer)), + ), + stderr: Stream.make(new TextEncoder().encode(stderr)), + all: Stream.empty, + exitCode: Effect.succeed( + ChildProcessSpawner.ExitCode(config || windows ? 0 : exitCode), + ), + isRunning: Effect.succeed(false), + kill: () => Effect.void, + stdin: Sink.drain, + getInputFd: () => Sink.drain, + getOutputFd: () => Stream.empty, + unref: Effect.succeed(Effect.void), + }), + ); + }), ), + ); + const error = yield* service + .testHost({ + kind: "ssh", + id: "probe", + label: "Probe test", + target: "probe.test", + port: 2222, + }) + .pipe(Effect.flip); + expect(error).toMatchObject({ + _tag: "DeviceHostUnavailableError", + hostId: "probe", + hostLabel: "Probe test", + reason, + }); + const codec = Schema.fromJsonString(DeviceError); + const decoded = yield* Schema.decodeEffect(codec)(yield* Schema.encodeEffect(codec)(error)); + expect(decoded.message).toBe(`Probe test is unavailable: ${reason}`); + expect(decoded.message).not.toContain("private SSH diagnostics"); + }).pipe( + Effect.provide( + Layer.mergeAll( + ServerSettings.layerTest(), + layerSecretResolver, + ServerConfig.layerTest(process.cwd(), { prefix: "t3-device-probe-" }), + NodeCrypto.layer, + NetService.layer, + ).pipe(Layer.provideMerge(NodeServices.layer)), ), + ), ); it.effect("publishes boot progress and does not restore sessions after support is disabled", () => diff --git a/apps/server/src/device/DeviceService.ts b/apps/server/src/device/DeviceService.ts index 78b31b87aefd3..cf8aece6745e0 100644 --- a/apps/server/src/device/DeviceService.ts +++ b/apps/server/src/device/DeviceService.ts @@ -90,6 +90,7 @@ export const DEVICE_HUB_ROUTE_PREFIX = "/api/device-hub"; const isSshCommandError = Schema.is(SshCommandError); const sshProbeFailureMessage = (error: DeviceHost.DeviceHostError): string => { + if (SshDeviceHost.isSshConnectError(error.cause)) return error.cause.message; const windows = SshDeviceHost.isSshWindowsHostError(error.cause) ? error.cause : null; const cause = windows ? windows.cause : error.cause; if ( @@ -163,7 +164,7 @@ export class DeviceService extends Context.Service< readonly testHost: ( config: SshDeviceHostConfig, ) => Effect.Effect; - /** Who an Expo credential signs in as and which EAS projects it can run sessions under. */ + /** Who an Expo credential signs in as and which Expo projects it can run sessions under. */ readonly easAccount: ( input: DeviceEasAccountInput, ) => Effect.Effect; @@ -598,7 +599,7 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function* ), ...devices.filter((device) => !incompletePlatforms.includes(device.platform)), ], - // A host that ends devices itself (EAS stops sessions at their limits) + // A host that ends devices itself (Expo stops sessions at their limits) // leaves sessions on devices it no longer lists with nothing to stream. // Other hosts can miss a device for one listing, so theirs stay. sessions: ready.devices.endsDevices @@ -1127,7 +1128,7 @@ export const makeWithHosts = Effect.fn("DeviceService.makeWithHosts")(function* easAccount: Effect.fn("DeviceService.easAccount")(function () { return Effect.fail( new DeviceEasAccountError({ - reason: "EAS accounts are unavailable in this device service.", + reason: "Expo accounts are unavailable in this device service.", }), ); }), @@ -1406,7 +1407,10 @@ export const make = Effect.gen(function* () { return yield* SshDeviceHost.probe(host).pipe(Effect.provide(probeContext)); }).pipe( Effect.mapError((error) => - error._tag === "DeviceHostError" && SshDeviceHost.isSshWindowsHostError(error.cause) + error._tag === "DeviceHostError" && + (isSshCommandError(error.cause) || + SshDeviceHost.isSshConnectError(error.cause) || + SshDeviceHost.isSshWindowsHostError(error.cause)) ? new DeviceHostUnavailableError({ hostId: host.id, hostLabel: host.label, diff --git a/apps/server/src/device/EasDeviceHost.test.ts b/apps/server/src/device/EasDeviceHost.test.ts index 3c083cdcbb7f8..35c0d5cf9af1d 100644 --- a/apps/server/src/device/EasDeviceHost.test.ts +++ b/apps/server/src/device/EasDeviceHost.test.ts @@ -26,7 +26,7 @@ const APP_ID = "d763fcb8-d37c-41ea-a773-b54a0ab4a454"; const config = (overrides: Partial = {}): EasDeviceHostConfig => ({ kind: "eas", id: "eas", - label: "EAS Simulator", + label: "Expo Simulators", appId: APP_ID, accessToken: "expo-token", maxSessionMinutes: 25, @@ -131,7 +131,15 @@ const fakeExpo = ( if (options.rejectCreate) return HttpClientResponse.fromWeb( request, - Response.json({ errors: [{ message: "Not allowed" }] }), + Response.json({ + errors: [ + { + message: "Device run sessions are not enabled for this account.", + extensions: { errorType: "USER" }, + }, + ], + data: null, + }), ); } if (operation === "T3StopEasSimulatorSession" && options.failStop?.()) { @@ -140,6 +148,24 @@ const fakeExpo = ( if (operation === "T3ActiveEasSimulatorSessions" && options.failList?.()) { return HttpClientResponse.fromWeb(request, new Response("bad gateway", { status: 502 })); } + // The account's feature gate is off, as for an Expo admin whom the create still admits. + if (body.query.includes("accountFeatureGates")) { + return HttpClientResponse.fromWeb( + request, + Response.json({ + data: { + app: { + byId: { + ownerAccount: { + name: "pingdotgg", + accountFeatureGates: { "device-run-sessions": false }, + }, + }, + }, + }, + }), + ); + } const sessionGate = operation === "T3EasSimulatorSession" ? options.sessionGate?.() : undefined; if (sessionGate && !sessionGated) { @@ -148,73 +174,62 @@ const fakeExpo = ( yield* Deferred.await(sessionGate.release); } const data = - operation === "T3EasSimulatorAvailability" - ? { - app: { - byId: { - ownerAccount: { - name: "pingdotgg", - accountFeatureGates: { "device-run-sessions": true }, + operation === "T3CreateEasSimulatorSession" + ? { deviceRunSession: { createDeviceRunSession: { id: `session-${++created}` } } } + : operation === "T3ActiveEasSimulatorSessions" + ? { + app: { + byId: { + deviceRunSessionsPaginated: { + edges: [ + ...(options.omitCreatedFromActive + ? [] + : [{ node: { id: "orphan", status: "NEW", name: createdName } }]), + { + node: { + id: "someone-else", + status: "IN_PROGRESS", + name: "T3 Code · Expo Simulators · 000000", + }, + }, + ...(options.active ?? []).map((node) => ({ node })), + ...(options.activeIncludesCreated && created > 0 + ? [ + { + node: { + id: `session-${created}`, + status: "STARTING", + name: createdName, + }, + }, + ] + : []), + ], + }, }, }, - }, - } - : operation === "T3CreateEasSimulatorSession" - ? { deviceRunSession: { createDeviceRunSession: { id: `session-${++created}` } } } - : operation === "T3ActiveEasSimulatorSessions" + } + : operation === "T3EasSimulatorSession" ? { - app: { + deviceRunSessions: { byId: { - deviceRunSessionsPaginated: { - edges: [ - ...(options.omitCreatedFromActive - ? [] - : [{ node: { id: "orphan", status: "NEW", name: createdName } }]), - { - node: { - id: "someone-else", - status: "IN_PROGRESS", - name: "T3 Code · EAS Simulator · 000000", - }, - }, - ...(options.active ?? []).map((node) => ({ node })), - ...(options.activeIncludesCreated && created > 0 - ? [ - { - node: { - id: `session-${created}`, - status: "STARTING", - name: createdName, - }, - }, - ] - : []), - ], + id: body.variables.id, + status: options.status?.() ?? "IN_PROGRESS", + remoteConfig: { + __typename: "AgentDeviceRunSessionRemoteConfig", + agentDeviceRemoteSessionUrl: "https://agent.test", + agentDeviceRemoteSessionToken: "agent-token", + webPreviewToken: "preview-token", + previewApiUrl: "https://preview.test?token=preview-token", }, }, }, } - : operation === "T3EasSimulatorSession" - ? { - deviceRunSessions: { - byId: { - id: body.variables.id, - status: options.status?.() ?? "IN_PROGRESS", - remoteConfig: { - __typename: "AgentDeviceRunSessionRemoteConfig", - agentDeviceRemoteSessionUrl: "https://agent.test", - agentDeviceRemoteSessionToken: "agent-token", - webPreviewToken: "preview-token", - previewApiUrl: "https://preview.test?token=preview-token", - }, - }, - }, - } - : { - deviceRunSession: { - ensureDeviceRunSessionStopped: { id: body.variables.id, status: "STOPPED" }, - }, - }; + : { + deviceRunSession: { + ensureDeviceRunSessionStopped: { id: body.variables.id, status: "STOPPED" }, + }, + }; return HttpClientResponse.fromWeb(request, Response.json({ data })); }), ); @@ -260,7 +275,7 @@ const makeHost = ( Effect.provideService(Scope.Scope, scope), ); -describe("EAS Simulator host", () => { +describe("Expo Simulators host", () => { it.effect("starts a bounded session and routes its stream and agent to that session", () => Effect.gen(function* () { const expo = fakeExpo(); @@ -433,13 +448,13 @@ describe("EAS Simulator host", () => { yield* ready.devices.boot(catalogDevice!); const refused = yield* ready.devices.boot(catalogDevice!).pipe(Effect.flip); expect(refused.message).toBe( - "EAS Simulator is unavailable: EAS Simulator is limited to 1 running session. Close one in the Device panel first.", + "Expo Simulators is unavailable: Expo Simulators is limited to 1 running session. Close one in the Device panel first.", ); expect(expo.named("T3CreateEasSimulatorSession")).toHaveLength(1); }).pipe(Effect.scoped, Effect.provide(layer)), ); - it.effect("tells concurrent sessions apart although EAS gives each the same UDID", () => + it.effect("tells concurrent sessions apart although Expo gives each the same UDID", () => Effect.gen(function* () { const expo = fakeExpo(); const scope = yield* Scope.make(); @@ -535,6 +550,17 @@ describe("EAS Simulator host", () => { }).pipe(Effect.scoped, Effect.provide(layer)), ); + it.effect("leaves admission to Expo's create although the account's gate is off", () => + Effect.gen(function* () { + const expo = fakeExpo(); + const host = yield* makeHost(expo, config(), yield* Scope.make()); + const ready = yield* host.ensureReady(() => Effect.void); + const [catalogDevice] = (yield* ready.devices.list).devices; + expect(yield* ready.devices.boot(catalogDevice!)).toBe("session-1"); + expect(expo.named("T3CreateEasSimulatorSession")).toHaveLength(1); + }).pipe(Effect.scoped, Effect.provide(layer)), + ); + it.effect("stops the session a refused create made anyway", () => Effect.gen(function* () { // Expo refuses the create but lists a session by its name. @@ -543,7 +569,9 @@ describe("EAS Simulator host", () => { const ready = yield* host.ensureReady(() => Effect.void); const [catalogDevice] = (yield* ready.devices.list).devices; const failure = yield* ready.devices.boot(catalogDevice!).pipe(Effect.flip); - expect(failure.message).toContain("Not allowed"); + expect(failure.message).toBe( + "Expo Simulators is unavailable: Expo could not start a simulator session: Device run sessions are not enabled for this account.", + ); expect(expo.stopped()).toEqual(["orphan"]); }).pipe(Effect.scoped, Effect.provide(layer)), ); @@ -610,7 +638,7 @@ describe("EAS Simulator host", () => { }).pipe(Effect.scoped, Effect.provide(layer)), ); - it.effect("asks for an EAS project before starting anything when none can be found", () => + it.effect("asks for an Expo project before starting anything when none can be found", () => Effect.gen(function* () { const expo = fakeExpo(); const host = yield* makeHost(expo, config({ appId: undefined }), yield* Scope.make()); diff --git a/apps/server/src/device/EasDeviceHost.ts b/apps/server/src/device/EasDeviceHost.ts index 719cfbf435a8a..a41b32a347cdc 100644 --- a/apps/server/src/device/EasDeviceHost.ts +++ b/apps/server/src/device/EasDeviceHost.ts @@ -1,5 +1,5 @@ /** - * Cloud simulators from EAS Simulator. A device exists only while a session + * Cloud simulators from Expo Simulators. A device exists only while a session * runs, and every session bills the Expo account until it stops, so this host * bounds each one, stops all of them when it stops, and stops sessions a * previous server left behind. It never touches sessions it did not start. @@ -59,14 +59,14 @@ const decodeServeEmuApi = Schema.decodeUnknownEffect( */ const PLATFORMS = { ios: { - name: "iPhone (EAS Simulator)", + name: "iPhone (Expo Simulator)", hubPrefix: "/vendor/serve-sim", previewPrefix: "", protocol: "serve-sim", remoteDeviceId: (api: unknown) => decodeServeSimApi(api).pipe(Effect.map((api) => api.device)), }, android: { - name: "Android (EAS Simulator)", + name: "Android (Expo Simulator)", hubPrefix: "/vendor/serve-emu", previewPrefix: "/vendor/serve-emu", protocol: "serve-emu", @@ -74,7 +74,7 @@ const PLATFORMS = { }, } as const; -/** EAS picks the simulator model; one entry starts a session. */ +/** Expo picks the simulator model; one entry starts a session. */ const CATALOG: ReadonlyArray = [ { id: "eas-ios", @@ -163,7 +163,7 @@ export const make = Effect.fn("EasDeviceHost.make")(function* (config: EasDevice >(); const hostId = config.id; const ledgerPath = path.join(server.stateDir, "device", "eas", `${hostId}.json`); - // Keyed by Expo's session id, which is also the device id: every EAS + // Keyed by Expo's session id, which is also the device id: every Expo // simulator reports the same remote device id, so that id cannot tell sessions apart. const sessions = new Map(); const checkedAt = new Map(); @@ -239,7 +239,7 @@ export const make = Effect.fn("EasDeviceHost.make")(function* (config: EasDevice Effect.flatMap(decodeLedger), Effect.mapError((cause) => unavailable( - `The record of running EAS sessions at ${ledgerPath} could not be read. Fix or remove it.`, + `The record of running Expo Simulators sessions at ${ledgerPath} could not be read. Fix or remove it.`, cause, ), ), @@ -256,7 +256,7 @@ export const make = Effect.fn("EasDeviceHost.make")(function* (config: EasDevice Effect.provide(services), Effect.mapError((cause) => unavailable( - `The record of running EAS sessions at ${ledgerPath} could not be written.`, + `The record of running Expo Simulators sessions at ${ledgerPath} could not be written.`, cause, ), ), @@ -267,13 +267,13 @@ export const make = Effect.fn("EasDeviceHost.make")(function* (config: EasDevice entries.map((entry) => (entry.name === name ? { ...entry, refused: true } : entry)), ).pipe( Effect.catch((cause) => - Effect.logWarning("Could not update the EAS session record", { name, cause }), + Effect.logWarning("Could not update the Expo Simulators session record", { name, cause }), ), ); const forgetEntry = (name: string) => writeLedger((entries) => entries.filter((entry) => entry.name !== name)).pipe( Effect.catch((cause) => - Effect.logWarning("Could not update the EAS session record", { name, cause }), + Effect.logWarning("Could not update the Expo Simulators session record", { name, cause }), ), ); @@ -282,7 +282,7 @@ export const make = Effect.fn("EasDeviceHost.make")(function* (config: EasDevice expo.stop(sessionId).pipe( Effect.andThen(forgetEntry(name)), Effect.tapError((cause) => - Effect.logWarning("Could not stop EAS Simulator session", { sessionId, cause }), + Effect.logWarning("Could not stop Expo Simulators session", { sessionId, cause }), ), ); @@ -403,7 +403,7 @@ export const make = Effect.fn("EasDeviceHost.make")(function* (config: EasDevice }, reapLock.withPermit, Effect.catch((cause) => - Effect.logWarning("Could not stop recorded EAS Simulator sessions", { cause }), + Effect.logWarning("Could not stop recorded Expo Simulators sessions", { cause }), ), ); @@ -439,7 +439,9 @@ export const make = Effect.fn("EasDeviceHost.make")(function* (config: EasDevice hostId, deviceId: sessionId, reason: "launch_failed", - cause: new Error(`EAS Simulator session ended (${session.status}) before it was ready`), + cause: new Error( + `Expo Simulators session ended (${session.status}) before it was ready`, + ), }); } if (session.status === "IN_PROGRESS" && session.endpoints) return session.endpoints; @@ -456,7 +458,7 @@ export const make = Effect.fn("EasDeviceHost.make")(function* (config: EasDevice hostId, deviceId: sessionId, reason: "timeout", - cause: new Error("EAS Simulator session did not start in time"), + cause: new Error("Expo Simulators session did not start in time"), }), ), }), @@ -504,15 +506,7 @@ export const make = Effect.fn("EasDeviceHost.make")(function* (config: EasDevice ); if (!appId) { return yield* unavailable( - "No EAS project to run the session under. Choose one for this host in Settings, or open the device from a thread whose project names extra.eas.projectId in app.json or its app config.", - ); - } - const access = yield* expo - .availability(appId) - .pipe(Effect.mapError((error) => unavailable(error.message, error))); - if (!access.available) { - return yield* unavailable( - `EAS Simulator is not enabled for the ${access.account} Expo account.`, + "No Expo project to run the session under. Choose one for this host in Settings, or open the device from a thread whose project names extra.eas.projectId in app.json or its app config.", ); } const entry = { name, appId, recordedAt: yield* Clock.currentTimeMillis }; @@ -593,7 +587,7 @@ export const make = Effect.fn("EasDeviceHost.make")(function* (config: EasDevice hostId, deviceId: sessionId, reason: "launch_failed", - cause: new Error("EAS Simulator returned no preview endpoint"), + cause: new Error("Expo Simulators returned no preview endpoint"), }); } const origin = new URL(endpoints.preview.url).origin; @@ -767,7 +761,7 @@ export const make = Effect.fn("EasDeviceHost.make")(function* (config: EasDevice new DeviceOperationError({ operation: "screenshot", reason: "request_failed", - cause: new Error("The EAS Simulator session is not running"), + cause: new Error("The Expo Simulator is not running"), }), ), onSome: (session) => @@ -795,7 +789,7 @@ export const make = Effect.fn("EasDeviceHost.make")(function* (config: EasDevice }), agentEndpoint: (deviceId) => Option.match(sessionFor(deviceId), { - onNone: () => Effect.fail(unavailable("This EAS Simulator device is no longer running.")), + onNone: () => Effect.fail(unavailable("This Expo Simulator is no longer running.")), onSome: (session) => Effect.succeed({ ...session.agent, agentDeviceId: session.remoteDeviceId }), }), @@ -858,11 +852,11 @@ export const make = Effect.fn("EasDeviceHost.make")(function* (config: EasDevice onNone: () => Effect.void, onSome: (running) => releaseAgentDeviceSession(http, running.agent)(session), }), - // Device settings run simctl on the simulator's own machine, which EAS does not expose. + // Device settings run simctl on the simulator's own machine, which Expo does not expose. run: () => Effect.succeed({ stdout: "", - stderr: "EAS Simulator devices cannot run host commands.", + stderr: "Devices from Expo Simulators cannot run host commands.", code: 127, }), helpers: { serveSimAxSettings: null, serveSimCli: null }, diff --git a/apps/server/src/device/ExpoApi.graphql b/apps/server/src/device/ExpoApi.graphql index af9299bfa76f4..a5eaa0d069c92 100644 --- a/apps/server/src/device/ExpoApi.graphql +++ b/apps/server/src/device/ExpoApi.graphql @@ -21,17 +21,6 @@ query T3ExpoViewer { } } -query T3EasSimulatorAvailability($appId: String!, $filter: [String!]) { - app { - byId(appId: $appId) { - ownerAccount { - name - accountFeatureGates(filter: $filter) - } - } - } -} - mutation T3CreateEasSimulatorSession($input: CreateDeviceRunSessionInput!) { deviceRunSession { createDeviceRunSession(deviceRunSessionInput: $input) { diff --git a/apps/server/src/device/ExpoApi.graphql.ts b/apps/server/src/device/ExpoApi.graphql.ts index 6c71e029285b6..f9501f1cc0dd0 100644 --- a/apps/server/src/device/ExpoApi.graphql.ts +++ b/apps/server/src/device/ExpoApi.graphql.ts @@ -24,18 +24,18 @@ export interface AccountAppsConnection extends Schema.Struct.Type {} +export interface Account extends Schema.Struct.Type {} /** Represents a human SSO (not robot) actor. */ export const SSOUser = Schema.Struct({ __typename: Schema.Literal("SSOUser"), username: Schema.String, - accounts: Schema.Array(AccountsAccount) + accounts: Schema.Array(Account) }) export interface SSOUser extends Schema.Struct.Type {} @@ -43,7 +43,7 @@ export interface SSOUser extends Schema.Struct.Type {} export const User = Schema.Struct({ __typename: Schema.Literal("User"), username: Schema.String, - accounts: Schema.Array(AccountsAccount) + accounts: Schema.Array(Account) }) export interface User extends Schema.Struct.Type {} @@ -52,7 +52,7 @@ export const Robot = Schema.Struct({ __typename: Schema.Literal("Robot"), firstName: Schema.NullOr(Schema.String), /** Associated accounts */ - accounts: Schema.Array(AccountsAccount) + accounts: Schema.Array(Account) }) export interface Robot extends Schema.Struct.Type {} @@ -60,7 +60,7 @@ export interface Robot extends Schema.Struct.Type {} export const ActorOther = Schema.Struct({ __typename: GraphQL.otherTypename()(["SSOUser", "User", "Robot"]), /** Associated accounts */ - accounts: Schema.Array(AccountsAccount) + accounts: Schema.Array(Account) }) export interface ActorOther extends Schema.Struct.Type {} @@ -91,54 +91,6 @@ export declare namespace T3ExpoViewer { export type Result = typeof T3ExpoViewer.result.Type } -/** - * An account is a container owning projects, credentials, billing and other organization - * data and settings. Actors may own and be members of accounts. - */ -export const OwnerAccountAccount = Schema.Struct({ - name: Schema.String, - /** Server account feature gate values for this account, optionally filtering by desired gates. */ - accountFeatureGates: Shared.JSONObject -}) -export interface OwnerAccountAccount extends Schema.Struct.Type {} - -/** Represents an Exponent App (or Experience in legacy terms) */ -export const T3EasSimulatorAvailabilityAppByIdApp = Schema.Struct({ - ownerAccount: OwnerAccountAccount -}) -export interface T3EasSimulatorAvailabilityAppByIdApp extends Schema.Struct.Type {} - -export const T3EasSimulatorAvailabilityAppAppQuery = Schema.Struct({ - /** Look up app by app id */ - byId: T3EasSimulatorAvailabilityAppByIdApp -}) -export interface T3EasSimulatorAvailabilityAppAppQuery extends Schema.Struct.Type {} - -/** `query T3EasSimulatorAvailability` */ -export const T3EasSimulatorAvailability = GraphQL.query("T3EasSimulatorAvailability", { - document: { - variables: [ - { name: "appId", body: "String!" }, - { name: "filter", body: "[String!]" } - ], - selections: [ - { name: "app", body: ["{byId(appId:$", "appId", "){ownerAccount{name accountFeatureGates(filter:$", "filter", ")}}}"] } - ], - fragments: [] - }, - variables: { - appId: Schema.String, - filter: Schema.optional(Schema.NullOr(Schema.Array(Schema.String))) - }, - result: Schema.Struct({ - app: T3EasSimulatorAvailabilityAppAppQuery - }) -}) -export declare namespace T3EasSimulatorAvailability { - export type Variables = typeof T3EasSimulatorAvailability.variables.Type - export type Result = typeof T3EasSimulatorAvailability.result.Type -} - export const CreateDeviceRunSessionDeviceRunSession = Schema.Struct({ id: Shared.ID }) @@ -250,16 +202,16 @@ export const AppDeviceRunSessionsConnection = Schema.Struct({ export interface AppDeviceRunSessionsConnection extends Schema.Struct.Type {} /** Represents an Exponent App (or Experience in legacy terms) */ -export const T3ActiveEasSimulatorSessionsAppByIdApp = Schema.Struct({ +export const ByIdApp = Schema.Struct({ deviceRunSessionsPaginated: AppDeviceRunSessionsConnection }) -export interface T3ActiveEasSimulatorSessionsAppByIdApp extends Schema.Struct.Type {} +export interface ByIdApp extends Schema.Struct.Type {} -export const T3ActiveEasSimulatorSessionsAppAppQuery = Schema.Struct({ +export const AppQuery = Schema.Struct({ /** Look up app by app id */ - byId: T3ActiveEasSimulatorSessionsAppByIdApp + byId: ByIdApp }) -export interface T3ActiveEasSimulatorSessionsAppAppQuery extends Schema.Struct.Type {} +export interface AppQuery extends Schema.Struct.Type {} /** `query T3ActiveEasSimulatorSessions` */ export const T3ActiveEasSimulatorSessions = GraphQL.query("T3ActiveEasSimulatorSessions", { @@ -278,7 +230,7 @@ export const T3ActiveEasSimulatorSessions = GraphQL.query("T3ActiveEasSimulatorS filter: Schema.optional(Schema.NullOr(Shared.DeviceRunSessionFilterInput)) }, result: Schema.Struct({ - app: T3ActiveEasSimulatorSessionsAppAppQuery + app: AppQuery }) }) export declare namespace T3ActiveEasSimulatorSessions { @@ -327,4 +279,4 @@ export declare namespace T3StopEasSimulatorSession { } /** Every operation in ExpoApi.graphql. */ -export const ExpoApiGraphQLGroup = GraphQLGroup.make(T3ExpoViewer, T3EasSimulatorAvailability, T3CreateEasSimulatorSession, T3EasSimulatorSession, T3ActiveEasSimulatorSessions, T3StopEasSimulatorSession) +export const ExpoApiGraphQLGroup = GraphQLGroup.make(T3ExpoViewer, T3CreateEasSimulatorSession, T3EasSimulatorSession, T3ActiveEasSimulatorSessions, T3StopEasSimulatorSession) diff --git a/apps/server/src/device/ExpoApi.test.ts b/apps/server/src/device/ExpoApi.test.ts index 730d34d38d9f8..6c29270251ff1 100644 --- a/apps/server/src/device/ExpoApi.test.ts +++ b/apps/server/src/device/ExpoApi.test.ts @@ -151,11 +151,9 @@ describe("Expo API", () => { respond(Response.json({ errors: [{ message: "Feature not enabled" }] })), credential, ) - .availability("app-1") + .stop("session-1") .pipe(Effect.flip); - expect(graphQl.message).toBe( - "Expo could not check EAS Simulator access: Feature not enabled", - ); + expect(graphQl.message).toBe("Expo could not stop a simulator session: Feature not enabled"); const rejected = yield* makeExpoApi(respond(new Response("no", { status: 401 })), credential) .stop("session-1") .pipe(Effect.flip); diff --git a/apps/server/src/device/ExpoApi.ts b/apps/server/src/device/ExpoApi.ts index 90a276eeffb99..1cc61daff7fc0 100644 --- a/apps/server/src/device/ExpoApi.ts +++ b/apps/server/src/device/ExpoApi.ts @@ -1,5 +1,5 @@ /** - * Expo's GraphQL API for EAS Simulator sessions. The operations in + * Expo's GraphQL API for Expo Simulators sessions. The operations in * `ExpoApi.graphql` are the ones eas-cli's `simulator:*` commands send; their * client is generated from `schema/expo.graphql` (`vp run generate`). */ @@ -152,9 +152,6 @@ export interface CreateEasSessionInput { readonly maxIdleTimeMinutes: number; } -const DEVICE_RUN_SESSIONS_GATE = "device-run-sessions"; -const isFeatureGates = Schema.is(Schema.Record(Schema.String, Schema.Unknown)); - /** Sends every operation with the resolved credential. */ class ExpoAuth extends GraphQLMiddleware.Service()("t3/server/device/ExpoApi/ExpoAuth") {} @@ -312,18 +309,6 @@ export const makeExpoApi = (http: HttpClient.HttpClient, credential: ExpoCredent }), ), ), - availability: (appId: string) => - call("check EAS Simulator access", (expo) => - expo.T3EasSimulatorAvailability({ appId, filter: [DEVICE_RUN_SESSIONS_GATE] }), - ).pipe( - Effect.map(({ app }) => { - const gates = app.byId.ownerAccount.accountFeatureGates; - return { - account: app.byId.ownerAccount.name, - available: isFeatureGates(gates) && gates[DEVICE_RUN_SESSIONS_GATE] === true, - }; - }), - ), create: (input: CreateEasSessionInput) => call("start a simulator session", (expo) => expo.T3CreateEasSimulatorSession({ diff --git a/apps/server/src/device/SshDeviceHost.test.ts b/apps/server/src/device/SshDeviceHost.test.ts index 8249afb1e51c1..cab2784a6b243 100644 --- a/apps/server/src/device/SshDeviceHost.test.ts +++ b/apps/server/src/device/SshDeviceHost.test.ts @@ -530,16 +530,79 @@ it.effect.each([ windows: false, }, { - name: "a connection failure", + name: "an unrecognized SSH failure", exitCode: 255, stderr: "Connection refused\n", shellEcho: "Windows_NT\n", nodePlatform: "win32", windows: false, }, + { + name: "a ProxyCommand permission error", + exitCode: 255, + stderr: "bash: /usr/local/bin/proxy: Permission denied\n", + shellEcho: "", + nodePlatform: null, + windows: false, + }, + { + name: "an untrusted host key", + exitCode: 255, + stderr: "Host key verification failed.\r\n", + shellEcho: "Windows_NT\n", + nodePlatform: "win32", + windows: false, + connect: { reason: "host_key_unknown", detail: "Host key verification failed." }, + }, + { + name: "a changed host key", + exitCode: 255, + stderr: + "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\nIT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!\nHost key verification failed.\n", + shellEcho: "", + nodePlatform: null, + windows: false, + connect: { + reason: "host_key_changed", + detail: "@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @", + }, + }, + { + name: "a rejected login", + exitCode: 255, + stderr: "me@test.example: Permission denied (publickey).\n", + shellEcho: "", + nodePlatform: null, + windows: false, + connect: { reason: "auth", detail: "me@test.example: Permission denied (publickey)." }, + }, + { + name: "an unresolvable host name", + exitCode: 255, + stderr: "ssh: Could not resolve hostname test.example: Name or service not known\n", + shellEcho: "", + nodePlatform: null, + windows: false, + connect: { + reason: "resolve", + detail: "ssh: Could not resolve hostname test.example: Name or service not known", + }, + }, + { + name: "a refused connection", + exitCode: 255, + stderr: "ssh: connect to host test.example port 22: Connection refused\n", + shellEcho: "", + nodePlatform: null, + windows: false, + connect: { + reason: "connect", + detail: "ssh: connect to host test.example port 22: Connection refused", + }, + }, ])( "probe failure on $name keeps the SSH cause", - ({ exitCode, stderr, shellEcho, nodePlatform, windows }) => + ({ exitCode, stderr, shellEcho, nodePlatform, windows, connect }) => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const home = yield* fs.makeTempDirectoryScoped(); @@ -589,7 +652,11 @@ it.effect.each([ expect(failure).toMatchObject({ _tag: "DeviceHostError", step: "probe" }); const sshFailure = { _tag: "SshCommandError", exitCode, stderr }; expect(failure.cause).toMatchObject( - windows ? { _tag: "SshWindowsHostError", cause: sshFailure } : sshFailure, + windows + ? { _tag: "SshWindowsHostError", cause: sshFailure } + : connect + ? { _tag: "SshConnectError", ...connect, target: "test.example", cause: sshFailure } + : sshFailure, ); // A transport failure has no remote shell to ask. if (exitCode === 255) expect(shellChecks).toEqual([]); diff --git a/apps/server/src/device/SshDeviceHost.ts b/apps/server/src/device/SshDeviceHost.ts index c1d57869ead95..d6946b419429a 100644 --- a/apps/server/src/device/SshDeviceHost.ts +++ b/apps/server/src/device/SshDeviceHost.ts @@ -41,6 +41,33 @@ export class SshWindowsHostError extends Schema.TaggedError } export const isSshWindowsHostError = Schema.is(SshWindowsHostError); +/** ssh itself refused the connection before the host bootstrap ran. */ +export class SshConnectError extends Schema.TaggedError()("SshConnectError", { + reason: Schema.Literals(["host_key_unknown", "host_key_changed", "auth", "resolve", "connect"]), + target: Schema.String, + /** The ssh diagnostic line that identified the reason. */ + detail: Schema.String, + cause: Schema.Defect(), +}) { + override get message(): string { + const here = "the machine running this T3 Code server"; + const detail = this.detail.replace(/\.$/u, ""); + switch (this.reason) { + case "host_key_unknown": + return `SSH encountered an untrusted host key while connecting to ${this.target} from ${here}. Connect once with ssh from there to identify the host and verify its key before accepting it.`; + case "host_key_changed": + return `SSH encountered a changed host key while connecting to ${this.target} from ${here}. Connect once with ssh from there to identify the affected host. If the change is expected, remove that host's old key with \`ssh-keygen -R\`, then verify and accept its new key.`; + case "auth": + return `SSH login failed while connecting to ${this.target}: ${detail}. SSH from ${here} must log in without a prompt.`; + case "resolve": + return `Could not resolve the host name ${this.target} from ${here}: ${detail}.`; + case "connect": + return `Could not reach ${this.target} from ${here}: ${detail}. Check that the host is on, its SSH server is running, and the address and port are right.`; + } + } +} +export const isSshConnectError = Schema.is(SshConnectError); + const Probe = Schema.Struct({ nodePath: Schema.String, tools: Schema.optional(DeviceToolVersions), @@ -92,6 +119,41 @@ const bootstrap = ( const isSshCommandError = Schema.is(SshCommandError); +// ssh exits 255 for its own failures; the remote bootstrap never does. +const SSH_CONNECT_FAILURES: ReadonlyArray = [ + ["host_key_changed", /REMOTE HOST IDENTIFICATION HAS CHANGED/u], + ["host_key_unknown", /^Host key verification failed\.$/u], + [ + "auth", + /^\S+: (?:Permission denied \(|No supported authentication methods)|: Too many authentication failures$/u, + ], + ["resolve", /^ssh: Could not resolve hostname /u], + ["connect", /^ssh: connect to host /u], +]; +const explainSshConnectFailure = ( + config: SshDeviceHostConfig, + error: DeviceHost.DeviceHostError, +): DeviceHost.DeviceHostError => { + const cause = error.cause; + if (!isSshCommandError(cause) || cause.exitCode !== 255) return error; + const lines = cause.stderr.split(/\r?\n/u).map((line) => line.trim()); + for (const [reason, pattern] of SSH_CONNECT_FAILURES) { + const detail = lines.find((line) => pattern.test(line)); + if (detail === undefined) continue; + return new DeviceHost.DeviceHostError({ + hostId: config.id, + step: error.step, + cause: new SshConnectError({ + reason, + target: config.target, + detail, + cause, + }), + }); + } + return error; +}; + // cmd expands only %OS% and PowerShell only $env:OS; POSIX shells expand neither to Windows_NT. const explainWindowsHost = Effect.fn("SshDeviceHost.explainWindowsHost")(function* ( config: SshDeviceHostConfig, @@ -154,7 +216,10 @@ export const probe = Effect.fn("SshDeviceHost.probe")(function* ( owner?: string, ) { const result = yield* bootstrap(config, owner ?? (yield* ownerFor(config.id)), "probe").pipe( - Effect.catchTags({ DeviceHostError: (error) => explainWindowsHost(config, error) }), + Effect.catchTags({ + DeviceHostError: (error) => + explainWindowsHost(config, explainSshConnectFailure(config, error)), + }), ); const value = yield* decodeProbe(result.stdout.trim()).pipe( Effect.mapError( diff --git a/apps/server/src/device/easProjectId.test.ts b/apps/server/src/device/easProjectId.test.ts index 42139f4eff1e1..30aed92d4d631 100644 --- a/apps/server/src/device/easProjectId.test.ts +++ b/apps/server/src/device/easProjectId.test.ts @@ -165,6 +165,103 @@ it.effect.each([ }, expected: Option.some(ID_A), }, + { + case: "reads app.json's id through a typed variant config that spreads parts of config", + files: { + "app.config.js": `/** @param {import('expo/config').ConfigContext} context */ + module.exports = ({ config }) => ({ + ...config, + ios: { ...config.ios, infoPlist: { ...config?.ios.infoPlist, CFBundleDisplayName: "Dev" } }, + plugins: [...config.plugins, "expo-dev-client"], + });`, + "app.json": JSON.stringify({ expo: { extra: { eas: { projectId: ID_A } } } }), + }, + expected: Option.some(ID_A), + }, + { + case: "reads app.json's id through a config typed in a multi-line JSDoc or line comment", + files: { + "app.config.js": `// Types: import('expo/config').ExpoConfig + /** + * The dev variant of the app. + * @param {import('expo/config').ConfigContext} context + */ + module.exports = ({ config }) => ({ ...config, name: "Dev" });`, + "app.json": JSON.stringify({ expo: { extra: { eas: { projectId: ID_A } } } }), + }, + expected: Option.some(ID_A), + }, + { + case: "gives no answer when a config spreads a part of config over the top, which may bring its own extra", + files: { + "app.config.js": `module.exports = ({ config }) => ({ ...config, ...config.preview });`, + "app.json": JSON.stringify({ + expo: { + extra: { eas: { projectId: ID_A } }, + preview: { extra: { eas: { projectId: ID_B } } }, + }, + }), + }, + expected: Option.none(), + }, + { + case: "gives no answer when a config wraps a part of config under another key, as Expo reads expo", + files: { + "app.config.js": `module.exports = ({ config }) => + process.env.APP_VARIANT === "preview" ? { expo: { ...config.preview } } : { ...config };`, + "app.json": JSON.stringify({ + expo: { + extra: { eas: { projectId: ID_A } }, + preview: { extra: { eas: { projectId: ID_B } } }, + }, + }), + }, + expected: Option.none(), + }, + { + case: "gives no answer when a config spreads a part into an expo entry, which Expo reads as the config", + files: { + "app.config.js": `module.exports = ({ config }) => ({ ...config, expo: { ...config.preview.expo } });`, + "app.json": JSON.stringify({ + expo: { + extra: { eas: { projectId: ID_A } }, + preview: { expo: { extra: { eas: { projectId: ID_B } } } }, + }, + }), + }, + expected: Option.none(), + }, + { + case: "gives no answer when a config spreads a part into a computed key", + files: { + "app.config.js": `module.exports = ({ config }) => ({ ...config, [[...config.keys]]: config.preview });`, + "app.json": JSON.stringify({ + expo: { + keys: ["extra"], + extra: { eas: { projectId: ID_A } }, + preview: { eas: { projectId: ID_B } }, + }, + }), + }, + expected: Option.none(), + }, + { + case: "gives no answer when a template line that only looks like a comment replaces extra", + files: { + "app.config.js": `module.exports = ({ config }) => { + const banner = \` +/* \${(config.extra = config.preview.extra)} */\`; + return { ...config, name: banner }; + };`, + "app.json": JSON.stringify({ + expo: { + extra: { eas: { projectId: ID_A } }, + preview: { extra: { eas: { projectId: ID_B } } }, + }, + }), + }, + expected: Option.none(), + }, { case: "reads a project id that follows spreads", files: { @@ -246,7 +343,7 @@ it.effect("gives no answer when an app's config is there but cannot be read", () }).pipe(Effect.scoped, Effect.provide(layer)), ); -it.effect("does not guess between apps that name different EAS projects", () => +it.effect("does not guess between apps that name different Expo projects", () => Effect.gen(function* () { const root = yield* workspace({ "apps/one/app.json": JSON.stringify({ expo: { extra: { eas: { projectId: ID_A } } } }), diff --git a/apps/server/src/device/easProjectId.ts b/apps/server/src/device/easProjectId.ts index 524dd6d9519c0..e14f9d5871910 100644 --- a/apps/server/src/device/easProjectId.ts +++ b/apps/server/src/device/easProjectId.ts @@ -33,7 +33,7 @@ const SPREAD_LATER = /\.\.\./; // Space, a comment, or an opening parenthesis: what may sit between `...` // and the thing spread. Sticky, so the scan moves through the file once. const SPREAD_FILLER = /(?:\s+|\/\/[^\n\r\u2028\u2029]*|\/\*(?:[^*]|\*(?!\/))*\*\/|\()/y; -// `config`'s `extra` and `extra.eas` entries, the holders of the EAS id +// `config`'s `extra` and `extra.eas` entries, the holders of the Expo project id // (dotted, optional-chained or in brackets). const EXTRA_OF_CONFIG = String.raw`config(?:\s*\??\.\s*extra|\s*(?:\?\.)?\s*\[\s*["'\x60]extra["'\x60]\s*\])`; const EAS_OF_CONFIG = @@ -48,10 +48,21 @@ const HOLDER_AT = { // `config` and no more of it: not an identifier continuing (`$`, Unicode // letters and a `\u` escape do, where `\b` would not), nor a property. const CONFIG_AT = new RegExp("config" + HOLDER_END, "uy"); +// A part of `config` other than `extra`, such as `config.ios`; the last name is +// captured. Spread at the top it could bring its own `extra` (`...config.preview`). +const CONFIG_PART_AT = new RegExp( + String.raw`config(?:\s*\??\.\s*(?!extra(?![\p{ID_Continue}$]))([\p{ID_Start}$_][\p{ID_Continue}$\u200c\u200d]*))+` + + HOLDER_END, + "uy", +); +// So a part keeps the id only as the first item of the object or array entry it +// names (`ios: { ...config.ios`, `plugins: [...config.plugins`); the key is captured. +const PART_OPENER = + /(?<=[{,]\s*["']?([\p{ID_Start}$_][\p{ID_Continue}$\u200c\u200d]*)["']?\s*:\s*[[{]\s*)/uy; /** - * Only Expo's `config` and its own `extra`/`extra.eas` spreads can preserve app.json's id. - * Other spreads may replace it; no spreads leaves inheritance unclear. + * Only Expo's `config`, its own `extra`/`extra.eas`, and parts kept in their own entry can + * preserve app.json's id. Other spreads may replace it; no spreads leaves inheritance unclear. * Skip comments whole after spreads to keep scanning linear. */ const staticConfigUse = (source: string): "spreads" | "replaces" | "unclear" => { @@ -72,18 +83,24 @@ const staticConfigUse = (source: string): "spreads" | "replaces" | "unclear" => spreads++; continue; } + CONFIG_PART_AT.lastIndex = at; + const part = CONFIG_PART_AT.exec(source); + PART_OPENER.lastIndex = next; + const opener = part && PART_OPENER.exec(source); + // Expo reads a returned `expo` entry as the whole config. + if (opener && opener[1] === part[1] && opener[1] !== "expo") continue; HOLDER_AT.extra.lastIndex = at; HOLDER_AT.eas.lastIndex = at; if (!HOLDER_AT.extra.test(source) && !HOLDER_AT.eas.test(source)) return "replaces"; } }; const ASSIGNS_OVER = /\bObject\s*\.\s*assign\b|\bassign\s*\(/; -// A config may replace the object holding the entry, or the whole config, -// from somewhere this scan cannot read: an assignment to `.extra` or `.eas`, -// (as a property or in brackets), a `delete` of anything, or another file it -// requires or imports. +// A config may replace its entry or itself from where this scan cannot read: +// assigning `.extra` or `.eas` (dotted or bracketed), a `delete`, or a file it +// requires or imports. Typing it as `import('expo/config').ConfigContext`, in a +// comment or not, loads only Expo's own module. const REPLACES_HOLDER = - /(?:\.\s*(?:extra|eas)|\[\s*["'`](?:extra|eas)["'`]\s*\])\s*=(?!=)|\bdelete\b|\brequire\s*\(|\bimport\s*\(/; + /(?:\.\s*(?:extra|eas)|\[\s*["'`](?:extra|eas)["'`]\s*\])\s*=(?!=)|\bdelete\b|\brequire\s*\(|\bimport\s*\((?!\s*(["'])expo\/config\1\s*\)\s*\.\s*[A-Z])/; // An `extra`/`eas` object must start by spreading the matching `config` entry to keep app.json's id. // Spreading the whole config there puts the id elsewhere; shorthand entries replace it too. // Stop reading once entries exceed MAX_MENTIONS. @@ -197,7 +214,7 @@ const projectIdsOf = Effect.fn("easProjectId.projectIdsOf")(function* (dir: stri }); /** - * The EAS project of a workspace's Expo app: its root, or one app below + * The Expo project of a workspace's Expo app: its root, or one app below * `apps/` or `packages/`. None when there is none, a config is ambiguous, or * the apps disagree, so the caller asks for an explicit choice instead of * billing the wrong project. diff --git a/apps/server/src/device/localSshDeviceHost.test.ts b/apps/server/src/device/localSshDeviceHost.test.ts index 4021be704a06e..623f2583acd27 100644 --- a/apps/server/src/device/localSshDeviceHost.test.ts +++ b/apps/server/src/device/localSshDeviceHost.test.ts @@ -83,7 +83,7 @@ it.effect("removes only self targets from a fanned-out host list", () => const eas = { kind: "eas" as const, id: "eas", - label: "EAS Simulator", + label: "Expo Simulators", accessToken: "", maxSessionMinutes: 30, idleMinutes: 10, diff --git a/apps/server/src/environment/DirectEndpoints.test.ts b/apps/server/src/environment/DirectEndpoints.test.ts index 6548b2ccd8c0e..09ecc1ee8c8c9 100644 --- a/apps/server/src/environment/DirectEndpoints.test.ts +++ b/apps/server/src/environment/DirectEndpoints.test.ts @@ -137,6 +137,36 @@ describe("resolveBoundEndpoints", () => { ]); }); + it("skips NixOS container and systemd-nspawn veths, which only this machine reaches", () => { + const interfaces = { + ...INTERFACES, + "ve-jellyfin": virtualInterface("192.168.53.193"), + "ve-qbittorrent": virtualInterface("192.168.55.225"), + "vb-jellyfin": virtualInterface("192.168.100.1"), + "vz-machine": virtualInterface("192.168.101.1"), + }; + expect( + resolveBoundEndpoints({ platform: "linux", host: "0.0.0.0", port: 3773, interfaces }), + ).toEqual([ + { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" }, + { kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" }, + ]); + }); + + it("keeps a Windows alias matching a Linux container prefix eligible", () => { + const interfaces = { + ...INTERFACES, + "ve-office": virtualInterface("192.168.1.30"), + }; + expect( + resolveBoundEndpoints({ platform: "win32", host: "0.0.0.0", port: 3773, interfaces }), + ).toEqual([ + { kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" }, + { kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" }, + { kind: "lan", httpBaseUrl: "http://192.168.1.30:3773/" }, + ]); + }); + it("omits Windows host-only adapters without excluding guest NICs or explicit binds", () => { const interfaces = { ...INTERFACES, @@ -216,6 +246,17 @@ describe("resolveBoundEndpoints", () => { ).toEqual([{ kind: "tailnet", httpBaseUrl: "http://100.101.102.103:3773/" }]); }); + it("reports an explicit bind to Tailscale's IPv6 address as the tailnet route", () => { + expect( + resolveBoundEndpoints({ + platform: "linux", + host: "fd7a:115c:a1e0::1", + port: 3773, + interfaces: INTERFACES, + }), + ).toEqual([{ kind: "tailnet", httpBaseUrl: "http://[fd7a:115c:a1e0::1]:3773/" }]); + }); + it("never reports an IPv4-mapped host, which can name the client's own loopback", () => { expect( resolveBoundEndpoints({ diff --git a/apps/server/src/environment/DirectEndpoints.ts b/apps/server/src/environment/DirectEndpoints.ts index 1f7d9f705620c..4508d208e75e6 100644 --- a/apps/server/src/environment/DirectEndpoints.ts +++ b/apps/server/src/environment/DirectEndpoints.ts @@ -24,7 +24,7 @@ import { import * as ServerConfig from "../config.ts"; import * as ServerEnvironment from "./ServerEnvironment.ts"; -import { isLoopbackHost, isPrivateNetworkAddress, VIRTUAL_INTERFACE } from "../startupAccess.ts"; +import { isLoopbackHost, isPrivateNetworkAddress, isVirtualInterface } from "../startupAccess.ts"; type NetworkInterfacesMap = ReturnType; @@ -40,6 +40,7 @@ export class DirectEndpoints extends Context.Service< >()("t3/environment/DirectEndpoints") {} // Credentials travel over HTTP; public IPs leak them and hostnames can resolve differently per client. +// Wildcard binds stay IPv4: the private-network listener check accepts only IPv4 peers. const isAdvertisableAddress = (address: string): boolean => NodeNet.isIPv4(address) && !address.startsWith("127.") && isPrivateNetworkAddress(address); @@ -70,7 +71,9 @@ export function resolveBoundEndpoints(input: { ); const addresses = isWildcardHost(input.host) ? Object.entries(input.interfaces) - .flatMap(([name, entries]) => (VIRTUAL_INTERFACE.test(name) ? [] : (entries ?? []))) + .flatMap(([name, entries]) => + isVirtualInterface(name, input.platform) ? [] : (entries ?? []), + ) .filter( (entry) => !entry.internal && @@ -79,7 +82,10 @@ export function resolveBoundEndpoints(input: { !(input.platform === "win32" && WINDOWS_HOST_ADAPTER_MAC.test(entry.mac)), ) .map((entry) => entry.address) - : [input.host!].filter(isAdvertisableAddress); + : [input.host!].filter( + (address) => + isAdvertisableAddress(address) || (NodeNet.isIPv6(address) && isTailnetHost(address)), + ); return [...new Set(addresses)] .filter((address) => !input.onCloudVm || tailscaleAddresses.has(address)) .map((address) => ({ diff --git a/apps/server/src/environment/ServerEnvironmentLabel.test.ts b/apps/server/src/environment/ServerEnvironmentLabel.test.ts index 7e0cf0a99607e..6cf235d67814b 100644 --- a/apps/server/src/environment/ServerEnvironmentLabel.test.ts +++ b/apps/server/src/environment/ServerEnvironmentLabel.test.ts @@ -50,3 +50,54 @@ it.effect("falls back to the hostname when hostnamectl never answers", () => assert.equal(yield* Fiber.join(label), "devbox"); }), ); + +const exitsWithFailure = ProcessRunner.ProcessRunner.of({ + run: () => + Effect.succeed({ + stdout: "", + stderr: "", + code: ChildProcessSpawner.ExitCode(1), + timedOut: false, + stdoutTruncated: false, + stderrTruncated: false, + stdoutInvalidUtf8: false, + stderrInvalidUtf8: false, + }), +}); + +it.effect("qualifies the inherited Windows hostname only inside WSL", () => + Effect.gen(function* () { + const cases = [ + { + kernel: "6.6.87.2-microsoft-standard-WSL2", + machineInfo: null, + expected: "DESKTOP-PC WSL (Ubuntu)", + }, + { kernel: "6.8.0-45-generic", machineInfo: null, expected: "DESKTOP-PC" }, + { + kernel: "6.6.87.2-microsoft-standard-WSL2", + machineInfo: 'PRETTY_HOSTNAME="Work WSL"', + expected: "Work WSL", + }, + ]; + for (const { kernel, machineInfo, expected } of cases) { + const files: Record = { + "/proc/sys/kernel/osrelease": `${kernel}\n`, + "/etc/machine-info": machineInfo, + }; + const label = yield* resolveServerEnvironmentLabel({ cwdBaseName: "project" }).pipe( + Effect.provideService(HostProcess.Platform, "linux"), + Effect.provideService(HostProcess.Hostname, "DESKTOP-PC"), + Effect.provideService(HostProcess.Environment, { WSL_DISTRO_NAME: "Ubuntu" }), + Effect.provideService(ProcessRunner.ProcessRunner, exitsWithFailure), + Effect.provide( + FileSystem.layerNoop({ + exists: (path) => Effect.succeed(files[path] != null), + readFileString: (path) => Effect.succeed(files[path] ?? ""), + }), + ), + ); + assert.equal(label, expected, kernel); + } + }), +); diff --git a/apps/server/src/environment/ServerEnvironmentLabel.ts b/apps/server/src/environment/ServerEnvironmentLabel.ts index fc59928cc5bc6..b56c811d519cc 100644 --- a/apps/server/src/environment/ServerEnvironmentLabel.ts +++ b/apps/server/src/environment/ServerEnvironmentLabel.ts @@ -181,6 +181,17 @@ const resolveFriendlyHostLabel = Effect.fn("resolveFriendlyHostLabel")(function* return null; }); +const isWsl = Effect.fn("isWsl")(function* () { + if ((yield* HostProcess.Platform) !== "linux") { + return false; + } + const fileSystem = yield* FileSystem.FileSystem; + const kernelRelease = yield* fileSystem + .readFileString("/proc/sys/kernel/osrelease") + .pipe(Effect.orElseSucceed(() => "")); + return kernelRelease.toLowerCase().includes("microsoft"); +}); + export const resolveServerEnvironmentLabel = Effect.fn("resolveServerEnvironmentLabel")(function* ( input: ResolveServerEnvironmentLabelInput, ) { @@ -191,6 +202,11 @@ export const resolveServerEnvironmentLabel = Effect.fn("resolveServerEnvironment const hostname = normalizeLabel(yield* HostProcess.Hostname); if (hostname) { + // WSL inherits the Windows hostname, so qualify it to tell the two environments apart. + if (yield* isWsl()) { + const distro = normalizeLabel((yield* HostProcess.Environment).WSL_DISTRO_NAME); + return distro ? `${hostname} WSL (${distro})` : `${hostname} WSL`; + } return hostname; } diff --git a/apps/server/src/environmentTheme.test.ts b/apps/server/src/environmentTheme.test.ts index 1e8b538a665ae..241c89546c228 100644 --- a/apps/server/src/environmentTheme.test.ts +++ b/apps/server/src/environmentTheme.test.ts @@ -2,6 +2,7 @@ import { DEFAULT_SERVER_SETTINGS, EnvironmentThemeFile, ServerSettingsError, + THEME_CSS_MAX_LENGTH, } from "@t3tools/contracts"; import * as NodeServices from "@effect/platform-node/NodeServices"; import { assert, it } from "@effect/vitest"; @@ -20,6 +21,12 @@ import { ServerSettingsService } from "./serverSettings.ts"; import { symlinksSupported } from "@t3tools/shared/testing/symlinks"; const encodeThemeFile = Schema.encodeSync(Schema.fromJsonString(EnvironmentThemeFile)); +/** Writes non-ASCII characters as `\uXXXX` escapes, as encoders such as Python's json.dumps do. */ +const encodeThemeFileEscaped = (theme: EnvironmentThemeFile) => + encodeThemeFile(theme).replace( + /[\u0080-\uffff]/g, + (char) => `\\u${char.charCodeAt(0).toString(16).padStart(4, "0")}`, + ); const NIGHTFALL_THEME: EnvironmentThemeFile = { name: "Nightfall", @@ -190,7 +197,10 @@ it.layer(NodeServices.layer)("environment theme", (it) => { ); it.effect("does not replace a theme file it could not put back", () => { - const oversized = JSON.stringify({ ...NIGHTFALL_THEME, padding: "x".repeat(40_000) }); + const oversized = JSON.stringify({ + ...NIGHTFALL_THEME, + padding: "x".repeat(EnvironmentTheme.MAX_THEME_FILE_BYTES), + }); return withEnvironmentThemes( { [`${REFUSED_THEME_ID}.json`]: oversized }, Effect.gen(function* () { @@ -298,14 +308,14 @@ it.layer(NodeServices.layer)("environment theme", (it) => { const large = (name: string): EnvironmentThemeFile => ({ ...NIGHTFALL_THEME, name, - css: `/* ${"x".repeat(19_900)} */`, + css: `/* ${"界".repeat(THEME_CSS_MAX_LENGTH - 6)} */`, }); return withEnvironmentThemes( { ...Object.fromEntries( - Array.from({ length: 9 }, (_, index) => [ + Array.from({ length: 6 }, (_, index) => [ `aa-${index}.json`, - encodeThemeFile(large(`Large ${index}`)), + encodeThemeFileEscaped(large(`Large ${index}`)), ]), ), "zz-look.json": encodeThemeFile(NIGHTFALL_THEME), @@ -341,7 +351,10 @@ it.layer(NodeServices.layer)("environment theme", (it) => { withEnvironmentThemes( { ...Object.fromEntries( - Array.from({ length: 7 }, (_, index) => [`junk-${index}.json`, "{".repeat(30_000)]), + Array.from({ length: 7 }, (_, index) => [ + `junk-${index}.json`, + "{".repeat(EnvironmentTheme.MAX_THEME_FILE_BYTES), + ]), ), "zz-valid.json": encodeThemeFile(NIGHTFALL_THEME), }, diff --git a/apps/server/src/environmentTheme.ts b/apps/server/src/environmentTheme.ts index b658870f2e960..1e7f4d3052301 100644 --- a/apps/server/src/environmentTheme.ts +++ b/apps/server/src/environmentTheme.ts @@ -9,6 +9,7 @@ import { EnvironmentThemeFile, EnvironmentThemeId, environmentThemeFileHasColors, + THEME_CSS_MAX_LENGTH, } from "@t3tools/contracts"; import { fromJsonStringPretty } from "@t3tools/shared/schemaJson"; import { themeCssLoadsRemoteFiles } from "@t3tools/shared/themeCss"; @@ -44,9 +45,12 @@ const isEnvironmentThemeId = Schema.is(EnvironmentThemeId); const THEME_FILE_SUFFIX = ".json"; const MAX_THEME_FILES = 32; -/** Shared with publishing so it cannot accept files the watcher skips. */ -export const MAX_THEME_FILE_BYTES = 32 * 1024; -const MAX_THEME_TOTAL_BYTES = 192 * 1024; +/** + * Shared with publishing so it cannot accept files the watcher skips. Fits the longest CSS in any + * JSON encoding, at up to 6 bytes per character for a `\uXXXX` escape, plus the colors. + */ +export const MAX_THEME_FILE_BYTES = 6 * THEME_CSS_MAX_LENGTH + 16 * 1024; +const MAX_THEME_TOTAL_BYTES = 6 * MAX_THEME_FILE_BYTES; interface PublishedThemes { readonly seq: number; diff --git a/apps/server/src/git/GitManager.test.ts b/apps/server/src/git/GitManager.test.ts index d3a3fd4c100eb..6b9a1cb03e0e7 100644 --- a/apps/server/src/git/GitManager.test.ts +++ b/apps/server/src/git/GitManager.test.ts @@ -469,9 +469,9 @@ function createFakeSourceControlProvider(scenario: FakeProviderScenario = {}): { : { headBranchProbe: scenario.headBranchProbe ?? - (({ headSelectors, defaultBranchHeadSelector }) => ({ - headSelectors: defaultBranchHeadSelector - ? [defaultBranchHeadSelector] + (({ headSelectors, ownerHeadSelector }) => ({ + headSelectors: ownerHeadSelector + ? [ownerHeadSelector] : headSelectors.filter((selector) => !selector.includes(":")), limit: 100, })), @@ -988,6 +988,54 @@ it.layer(layerGitManagerTest)("GitManager", (it) => { }), ); + it.effect("status reads a remote's first URL when it has a second push URL", () => + Effect.gen(function* () { + const repoDir = yield* makeTempDir("t3code-git-manager-"); + yield* initRepo(repoDir); + yield* runGit(repoDir, ["checkout", "-b", "feature/mirrored"]); + const remoteDir = yield* createBareRemote(); + yield* runGit(repoDir, ["remote", "add", "origin", remoteDir]); + yield* runGit(repoDir, ["push", "-u", "origin", "feature/mirrored"]); + yield* configureVisibleRemoteUrlWithLocalRewrite( + repoDir, + "origin", + "git@github.com:pingdotgg/codething-mvp.git", + remoteDir, + ); + // `git remote set-url --add`: Git fetches from the first URL and also pushes to this one. + yield* runGit(repoDir, [ + "config", + "--add", + "remote.origin.url", + "git@gitlab.com:mirror-owner/codething-mvp.git", + ]); + + const { manager } = yield* makeManager({ + providerScenario: { + prListSequence: [ + [ + { + number: 16, + title: "Mirrored branch", + url: "https://github.com/pingdotgg/codething-mvp/pull/16", + baseRefName: "main", + headRefName: "feature/mirrored", + isCrossRepository: false, + headRepositoryNameWithOwner: "pingdotgg/codething-mvp", + headRepositoryOwnerLogin: "pingdotgg", + }, + ], + ], + }, + }); + + const status = yield* manager.status({ cwd: repoDir }); + + expect(status.sourceControlProvider?.kind).toBe("github"); + expect(status.pr?.number).toBe(16); + }), + ); + it.effect("status reports the latest of a branch's closed and merged PRs", () => Effect.gen(function* () { const repoDir = yield* makeTempDir("t3code-git-manager-"); @@ -2284,6 +2332,72 @@ it.layer(layerGitManagerTest)("GitManager", (it) => { }), ); + it.effect("finds a fork branch's open PR past a full page of other forks' PRs", () => + Effect.gen(function* () { + const repoDir = yield* makeTempDir("t3code-git-manager-"); + yield* initRepo(repoDir); + const originDir = yield* createBareRemote(); + const forkDir = yield* createBareRemote(); + yield* runGit(repoDir, ["remote", "add", "origin", originDir]); + yield* runGit(repoDir, ["push", "-u", "origin", "main"]); + yield* runGit(repoDir, ["remote", "add", "fork", forkDir]); + yield* runGit(repoDir, ["checkout", "-b", "feature"]); + yield* runGit(repoDir, ["push", "-u", "fork", "feature"]); + yield* configureVisibleRemoteUrlWithLocalRewrite( + repoDir, + "origin", + "git@github.com:team/repository.git", + originDir, + ); + yield* configureVisibleRemoteUrlWithLocalRewrite( + repoDir, + "fork", + "git@github.com:alice/repository.git", + forkDir, + ); + const otherForks: FakeChangeRequest[] = Array.from({ length: 100 }, (_, index) => ({ + number: 200 - index, + title: "Update docs", + url: `https://github.com/team/repository/pull/${200 - index}`, + baseRefName: "main", + headRefName: "feature", + state: "open", + isCrossRepository: true, + headRepositoryNameWithOwner: `fork-${index + 1}/repository`, + headRepositoryOwnerLogin: `fork-${index + 1}`, + })); + const { manager, providerCalls } = yield* makeManager({ + providerScenario: { + prListSequenceByHeadSelector: { feature: [otherForks, otherForks] }, + prListByHeadSelector: { + "alice:feature": [ + { + number: 1, + title: "This fork", + url: "https://github.com/team/repository/pull/1", + baseRefName: "main", + headRefName: "feature", + state: "open", + isCrossRepository: true, + headRepositoryNameWithOwner: "alice/repository", + headRepositoryOwnerLogin: "alice", + }, + ], + }, + }, + }); + + expect(yield* manager.branchPullRequest({ cwd: repoDir, branch: "feature" })).toMatchObject({ + number: 1, + state: "open", + }); + expect(listed(providerCalls).map((call) => [call.headSelector, call.state])).toEqual([ + ["feature", "all"], + ["alice:feature", "open"], + ]); + }), + ); + it.effect.each([ { remoteUrl: "git@gitlab.com:Group/Subgroup/Fork.git", setUpstream: true }, { remoteUrl: "git@gitlab.com:Group/Subgroup/Fork.git", setUpstream: false }, @@ -3124,6 +3238,31 @@ it.layer(layerGitManagerTest)("GitManager", (it) => { }), ); + it.effect("lowercases a capitalized Conventional Commits description", () => + Effect.gen(function* () { + const repoDir = yield* makeTempDir("t3code-git-manager-"); + yield* initRepo(repoDir); + const subjects = ["ci: Run E2E on pull requests", "feat(web)!: A new layout"]; + const queued = [...subjects]; + + const { manager } = yield* makeManager({ + serverSettings: { sourceControlWritingStyle: { mode: "conventional_commits" as const } }, + textGeneration: { + generateCommitMessage: () => Effect.succeed({ subject: queued.shift() ?? "", body: "" }), + }, + }); + + const committed: Array = []; + for (const [index] of subjects.entries()) { + NodeFS.writeFileSync(NodePath.join(repoDir, "README.md"), `change ${index}\n`); + yield* runStackedAction(manager, { cwd: repoDir, action: "commit" }); + committed.push((yield* runGit(repoDir, ["log", "-1", "--pretty=%s"])).stdout.trim()); + } + + expect(committed).toEqual(["ci: run E2E on pull requests", "feat(web)!: a new layout"]); + }), + ); + it.effect("includes CLAUDE.md for Claude writers only when it differs from AGENTS.md", () => Effect.gen(function* () { const repoDir = yield* makeTempDir("t3code-git-manager-"); @@ -3818,6 +3957,56 @@ it.layer(layerGitManagerTest)("GitManager", (it) => { }), ); + it.effect("finishes a pushed commit when the final status read fails", () => + Effect.gen(function* () { + const repoDir = yield* makeTempDir("t3code-git-manager-"); + yield* initRepo(repoDir); + yield* runGit(repoDir, ["checkout", "-b", "feature/status-after-push"]); + const remoteDir = yield* createBareRemote(); + yield* runGit(repoDir, ["remote", "add", "origin", remoteDir]); + NodeFS.writeFileSync(NodePath.join(repoDir, "change.txt"), "change\n"); + const gate = { + matches: (args: ReadonlyArray) => args[0] === "push", + reached: yield* Deferred.make(), + release: yield* Deferred.make(), + }; + const events: GitActionProgressEvent[] = []; + const { manager } = yield* makeManager({ gitSpawnGate: gate }); + + const action = yield* Effect.forkChild( + runStackedAction( + manager, + { cwd: repoDir, action: "commit_push", commitMessage: "Status after push" }, + { + progressReporter: { + publish: (event) => + Effect.sync(() => { + events.push(event); + }), + }, + }, + ), + ); + yield* Deferred.await(gate.reached); + NodeFS.writeFileSync(NodePath.join(repoDir, ".git", "index.lock"), ""); + yield* Deferred.succeed(gate.release, undefined); + const result = yield* Fiber.join(action); + + expect(result.push.status).toBe("pushed"); + expect(result.toast.cta).toEqual({ kind: "none" }); + expect(events.at(-1)?.kind).toBe("action_finished"); + expect( + (yield* runGit(remoteDir, [ + "--git-dir=.", + "log", + "-1", + "--pretty=%s", + "feature/status-after-push", + ])).stdout.trim(), + ).toBe("Status after push"); + }), + ); + it.effect("hands the branch base to the host when a failed first push is retried", () => Effect.gen(function* () { const repoDir = yield* makeTempDir("t3code-git-manager-"); @@ -4696,9 +4885,15 @@ it.layer(layerGitManagerTest)("GitManager", (it) => { const preview = yield* driver.getReviewDiffPreview({ cwd: worktreePath }); const changes = preview.sources.find((source) => source.kind === "branch-range")!; - expect - .soft(changes.files) - .toEqual([{ path: "feature.txt", previousPath: null, additions: 1, deletions: 0 }]); + expect.soft(changes.files).toEqual([ + { + path: "feature.txt", + previousPath: null, + changeType: "new", + additions: 1, + deletions: 0, + }, + ]); yield* runGit(worktreePath, ["push", "-u", "origin", "feature/remote-base"]); const generatedInputs: TextGeneration.PrContentGenerationInput[] = []; diff --git a/apps/server/src/git/GitManager.ts b/apps/server/src/git/GitManager.ts index 5793056a28041..0c0838d923d39 100644 --- a/apps/server/src/git/GitManager.ts +++ b/apps/server/src/git/GitManager.ts @@ -19,6 +19,7 @@ import * as PubSub from "effect/PubSub"; import * as Ref from "effect/Ref"; import * as Schema from "effect/Schema"; import * as Scope from "effect/Scope"; +import * as Semaphore from "effect/Semaphore"; import * as Stream from "effect/Stream"; import { GitActionProgressEvent, @@ -105,6 +106,11 @@ export interface GitRunStackedActionOptions { readonly progressReporter?: GitActionProgressReporter; } +export interface GitLocalStatusOptions { + /** Defaults to true. The branch-wide totals cost about half of a local status read. */ + readonly includeBranchChanges?: boolean; +} + export interface GitRemoteStatusOptions extends GitVcsDriver.GitRemoteStatusOptions { readonly refreshMissingPullRequest?: boolean; } @@ -135,6 +141,7 @@ export class GitManager extends Context.Service< ) => Effect.Effect; readonly localStatus: ( input: VcsStatusInput, + options?: GitLocalStatusOptions, ) => Effect.Effect; readonly remoteStatus: ( input: VcsStatusInput, @@ -193,6 +200,9 @@ const PR_LOOKUP_FAILURE_MAX_TTL = Duration.minutes(15); const PR_LOOKUP_CACHE_CAPACITY = 2_048; const PR_LOOKUP_GIT_STATE_TTL = Duration.minutes(5); const SETTLED_PR_LOOKUP_RECHECK_INTERVAL = Duration.minutes(15); +// Shared by every background caller (backfill, settlement, cleanup), so startup discovery +// cannot fill the git and forge process slots that foreground work also needs. +const BRANCH_PR_LOOKUP_CONCURRENCY = 4; const PR_THREAD_CONTEXT_BYTES = 16_000; const PR_THREAD_COMMAND_OUTPUT_CHARS = 2_000; const isSourceControlProviderError = Schema.is(SourceControlProviderError); @@ -532,17 +542,30 @@ function summarizeGitActionResult( return { title: "Done" }; } -function sanitizeCommitMessage(generated: { - subject: string; - body: string; - branch?: string | undefined; -}): { +// A backstop for models that ignore the preset's lowercase instruction; acronyms and names like GitHub are left alone. +const CONVENTIONAL_CAPITALIZED_DESCRIPTION = + /^([a-z]+(?:\([^)\n]*\))?!?: )([A-Z][a-z]*)(?![A-Za-z0-9])/; + +function sanitizeCommitMessage( + generated: { + subject: string; + body: string; + branch?: string | undefined; + }, + conventionalCommits: boolean, +): { subject: string; body: string; branch?: string | undefined; } { const rawSubject = generated.subject.trim().split(/\r?\n/g)[0]?.trim() ?? ""; - const subject = rawSubject.replace(/[.]+$/g, "").trim(); + const trimmedSubject = rawSubject.replace(/[.]+$/g, "").trim(); + const subject = conventionalCommits + ? trimmedSubject.replace( + CONVENTIONAL_CAPITALIZED_DESCRIPTION, + (_, prefix: string, word: string) => prefix + word.toLowerCase(), + ) + : trimmedSubject; const safeSubject = subject.length > 0 ? subject.slice(0, 72).trimEnd() : "Update project files"; return { subject: safeSubject, @@ -610,10 +633,10 @@ function headBranchProbe( provider: SourceControlProvider.SourceControlProvider["Service"], headSelectors: ReadonlyArray, state: "open" | "all", - defaultBranchHeadSelector?: string, + ownerHeadSelector?: string, ) { return ( - provider.headBranchProbe?.({ headSelectors, state, defaultBranchHeadSelector }) ?? { + provider.headBranchProbe?.({ headSelectors, state, ownerHeadSelector }) ?? { headSelectors, limit: state === "open" ? 1 : 20, } @@ -721,6 +744,16 @@ export const make = Effect.gen(function* () { const path = yield* Path.Path; const sourceControlProvider = (cwd: string) => sourceControlProviders.resolve({ cwd }); + // Git fetches from a remote's first URL; `config --get` returns the last of several. + const readRemoteUrl = (cwd: string, remoteName: string) => + gitCore + .execute({ + operation: "GitManager.readRemoteUrl", + cwd, + args: ["config", "--get-all", `remote.${remoteName}.url`], + allowNonZeroExit: true, + }) + .pipe(Effect.map((result) => result.stdout.split("\n", 1)[0]?.trim() || null)); const changeRequestTermsFor = Effect.fn("GitManager.changeRequestTermsFor")(function* ( cwd: string, ) { @@ -1005,7 +1038,7 @@ export const make = Effect.gen(function* () { repository: repositoryNameWithOwner, ...pullRequestHostOptions(pullRequest.url), }); - const originRemoteUrl = yield* gitCore.readConfigValue(cwd, "remote.origin.url"); + const originRemoteUrl = yield* readRemoteUrl(cwd, "origin"); const remoteUrl = shouldPreferSshRemote(originRemoteUrl) ? cloneUrls.sshUrl : cloneUrls.url; const preferredRemoteName = pullRequest.headRepositoryOwnerLogin?.trim() || @@ -1090,7 +1123,7 @@ export const make = Effect.gen(function* () { ? Effect.fail(cause) : Effect.gen(function* () { const remoteName = yield* gitCore.resolvePrimaryRemoteName(cwd); - const remoteUrl = yield* gitCore.readConfigValue(cwd, `remote.${remoteName}.url`); + const remoteUrl = yield* readRemoteUrl(cwd, remoteName); const pullRequestKey = pullRequestRepositoryKey(pullRequest.url); if ( remoteUrl === null || @@ -1119,7 +1152,7 @@ export const make = Effect.gen(function* () { repository: repositoryNameWithOwner, ...pullRequestHostOptions(pullRequest.url), }); - const originRemoteUrl = yield* gitCore.readConfigValue(cwd, "remote.origin.url"); + const originRemoteUrl = yield* readRemoteUrl(cwd, "origin"); const remoteUrl = shouldPreferSshRemote(originRemoteUrl) ? cloneUrls.sshUrl : cloneUrls.url; const preferredRemoteName = pullRequest.headRepositoryOwnerLogin?.trim() || @@ -1193,9 +1226,12 @@ export const make = Effect.gen(function* () { behindCount: 0, aheadOfDefaultCount: 0, }; - const readLocalStatus = Effect.fn("readLocalStatus")(function* (cwd: string) { + const readLocalStatus = Effect.fn("readLocalStatus")(function* ( + cwd: string, + includeBranchChanges: boolean, + ) { const details = yield* gitCore - .statusDetailsLocal(cwd, { includeDivergence: false, includeBranchChanges: true }) + .statusDetailsLocal(cwd, { includeDivergence: false, includeBranchChanges }) .pipe( Effect.catchIf(isNotGitRepositoryError, () => Effect.succeed(nonRepositoryStatusDetails)), ); @@ -1214,13 +1250,22 @@ export const make = Effect.gen(function* () { ...(details.branchChanges ? { branchChanges: details.branchChanges } : {}), } satisfies VcsStatusLocalResult; }); - const localStatusResultCache = yield* Cache.makeWith(readLocalStatus, { - capacity: STATUS_RESULT_CACHE_CAPACITY, - timeToLive: (exit) => (Exit.isSuccess(exit) ? STATUS_RESULT_CACHE_TTL : Duration.zero), - }); + const makeLocalStatusResultCache = (includeBranchChanges: boolean) => + Cache.makeWith((cwd: string) => readLocalStatus(cwd, includeBranchChanges), { + capacity: STATUS_RESULT_CACHE_CAPACITY, + timeToLive: (exit) => (Exit.isSuccess(exit) ? STATUS_RESULT_CACHE_TTL : Duration.zero), + }); + // Separate entries, so a read without totals is never served to a caller that shows them. + const localStatusResultCache = yield* makeLocalStatusResultCache(true); + const localStatusWithoutBranchChangesCache = yield* makeLocalStatusResultCache(false); const invalidateLocalStatusResultCache = (cwd: string) => normalizeStatusCacheKey(cwd).pipe( - Effect.flatMap((cacheKey) => Cache.invalidate(localStatusResultCache, cacheKey)), + Effect.flatMap((cacheKey) => + Effect.all([ + Cache.invalidate(localStatusResultCache, cacheKey), + Cache.invalidate(localStatusWithoutBranchChangesCache, cacheKey), + ]), + ), ); const prLookupEpochByCwd = new Map(); const prLookupEpoch = (cwd: string) => prLookupEpochByCwd.get(cwd) ?? 0; @@ -1529,6 +1574,8 @@ export const make = Effect.gen(function* () { const readConfigValueNullable = (cwd: string, key: string) => gitCore.readConfigValue(cwd, key).pipe(Effect.orElseSucceed(() => null)); + const readRemoteUrlNullable = (cwd: string, remoteName: string) => + readRemoteUrl(cwd, remoteName).pipe(Effect.orElseSucceed(() => null)); // Whether the remote's host creates change requests in this remote's repository. A host's tools // can let configuration, such as gh's default repository, name another one. @@ -1562,8 +1609,8 @@ export const make = Effect.gen(function* () { ? "origin" : ((yield* readConfigValueNullable(cwd, `branch.${branch}.remote`)) ?? "origin"); const remoteUrl = - (yield* readConfigValueNullable(cwd, `remote.${preferredRemoteName}.url`)) ?? - (yield* readConfigValueNullable(cwd, "remote.origin.url")); + (yield* readRemoteUrlNullable(cwd, preferredRemoteName)) ?? + (yield* readRemoteUrlNullable(cwd, "origin")); const provider = remoteUrl ? sourceControlDefinitions.recognizeRemote(remoteUrl) : null; if (!remoteUrl || provider?.kind !== "unknown") return provider; @@ -1589,7 +1636,7 @@ export const make = Effect.gen(function* () { }; } - const remoteUrl = yield* readConfigValueNullable(cwd, `remote.${remoteName}.url`); + const remoteUrl = yield* readRemoteUrlNullable(cwd, remoteName); let repositoryNameWithOwner = parseRepositoryNameWithOwnerFromRemoteUrl(remoteUrl); if ( remoteUrl !== null && @@ -1845,7 +1892,7 @@ export const make = Effect.gen(function* () { remoteName = matches[0]; } if (remoteName === undefined) return null; - const remoteUrl = yield* readConfigValueNullable(cwd, `remote.${remoteName}.url`); + const remoteUrl = yield* readRemoteUrlNullable(cwd, remoteName); return remoteUrl === null ? null : remoteName; }); @@ -1951,9 +1998,10 @@ export const make = Effect.gen(function* () { | "headRepositoryOwnerLogin" | "isCrossRepository" >, + ownerHeadSelector?: string, ) { const provider = yield* sourceControlProvider(cwd); - const probe = headBranchProbe(provider, headContext.headSelectors, "open"); + const probe = headBranchProbe(provider, headContext.headSelectors, "open", ownerHeadSelector); for (const headSelector of probe.headSelectors) { const pullRequests = yield* provider.listChangeRequests({ cwd, @@ -1994,6 +2042,7 @@ export const make = Effect.gen(function* () { ? `${headContext.headRepositoryOwnerLogin}:${defaultBranch}` : undefined, ); + let pageWasFull = false; for (const headSelector of probe.headSelectors) { const pullRequests = yield* provider.listChangeRequests({ cwd, @@ -2001,6 +2050,7 @@ export const make = Effect.gen(function* () { state: "all", limit: probe.limit, }); + pageWasFull ||= pullRequests.length >= probe.limit; for (const pr of pullRequests.map(toPullRequestInfo)) { if (!matchesBranchHeadContext(pr, headContext)) { @@ -2016,6 +2066,17 @@ export const make = Effect.gen(function* () { if (latestOpenPr) { return latestOpenPr; } + // Forks reusing the head name can fill the page before the branch's own open PR. + if (pageWasFull) { + const openPr = yield* findOpenPr( + cwd, + headContext, + headContext.headRepositoryOwnerLogin + ? `${headContext.headRepositoryOwnerLogin}:${headContext.headBranch}` + : undefined, + ); + if (openPr) return openPr; + } return parsed[0] ?? null; }); const buildCompletionToast = Effect.fn("buildCompletionToast")(function* ( @@ -2032,16 +2093,17 @@ export const make = Effect.gen(function* () { hasUpstream: boolean; } | null = null; - if (result.action !== "commit") { - const finalStatus = yield* gitCore.statusDetails(cwd); - if (finalStatus.branch) { - finalBranchContext = { - branch: finalStatus.branch, - upstreamRef: finalStatus.upstreamRef, - hasUpstream: finalStatus.hasUpstream, - }; - currentBranchIsDefault = finalStatus.isDefaultBranch; - } + const finalStatus = + result.action === "commit" + ? null + : yield* gitCore.statusDetails(cwd).pipe(Effect.orElseSucceed(() => null)); + if (finalStatus?.branch) { + finalBranchContext = { + branch: finalStatus.branch, + upstreamRef: finalStatus.upstreamRef, + hasUpstream: finalStatus.hasUpstream, + }; + currentBranchIsDefault = finalStatus.isDefaultBranch; } const explicitResultPr = @@ -2093,6 +2155,7 @@ export const make = Effect.gen(function* () { } : (result.action === "push" || result.action === "commit_push") && result.push.status === "pushed" && + finalStatus !== null && !currentBranchIsDefault ? { kind: "run_action" as const, @@ -2235,7 +2298,11 @@ export const make = Effect.gen(function* () { modelSelection: input.settings.modelSelection, projectId: input.settings.projectId, }) - .pipe(Effect.map((result) => sanitizeCommitMessage(result))); + .pipe( + Effect.map((result) => + sanitizeCommitMessage(result, policy?.kind === "conventional_commits"), + ), + ); return { subject: generated.subject, @@ -2541,9 +2608,14 @@ export const make = Effect.gen(function* () { }); const localStatus: GitManager["Service"]["localStatus"] = Effect.fn("localStatus")( - function* (input) { + function* (input, options) { const cacheKey = yield* normalizeStatusCacheKey(input.cwd); - return yield* Cache.get(localStatusResultCache, cacheKey); + return yield* Cache.get( + options?.includeBranchChanges === false + ? localStatusWithoutBranchChangesCache + : localStatusResultCache, + cacheKey, + ); }, ); const remoteStatus: GitManager["Service"]["remoteStatus"] = Effect.fn("remoteStatus")( @@ -2599,6 +2671,7 @@ export const make = Effect.gen(function* () { prLookupGitStateByKey.set(entryKey, { readAtMs, value }); return value; }); + const branchPrLookups = yield* Semaphore.make(BRANCH_PR_LOOKUP_CONCURRENCY); const settledPrLookupByBranchKey = new Map< string, { readonly url: string; readonly fingerprint: string; readonly confirmedAtMs: number } @@ -2629,7 +2702,13 @@ export const make = Effect.gen(function* () { const readRemoteUrlKey = (remoteName: string | null) => remoteName === null ? Effect.succeed(null) - : readConfig(`remote.${remoteName}.url`).pipe( + : rememberPrLookupGitState( + cwd, + `remoteUrl\u0000${remoteName}`, + fresh, + readRemoteUrl(cwd, remoteName), + ).pipe( + Effect.orElseSucceed(() => null), Effect.map((remoteUrl) => remoteUrl ? sourceControlDefinitions.repositoryKeyFromRemote(remoteUrl) : null, ), @@ -2899,7 +2978,7 @@ export const make = Effect.gen(function* () { }); } return confirmed; - }); + }, branchPrLookups.withPermits(1)); const checkedOutPullRequest: GitManager["Service"]["checkedOutPullRequest"] = Effect.fn( "checkedOutPullRequest", diff --git a/apps/server/src/git/GitWorkflowService.ts b/apps/server/src/git/GitWorkflowService.ts index a614ebdfb5687..287207840ad6a 100644 --- a/apps/server/src/git/GitWorkflowService.ts +++ b/apps/server/src/git/GitWorkflowService.ts @@ -51,6 +51,7 @@ export class GitWorkflowService extends Context.Service< ) => Effect.Effect; readonly localStatus: ( input: VcsStatusInput, + options?: GitManager.GitLocalStatusOptions, ) => Effect.Effect; readonly currentBranch: (cwd: string) => Effect.Effect; readonly upstreamDivergence: (cwd: string) => Effect.Effect< @@ -67,6 +68,7 @@ export class GitWorkflowService extends Context.Service< ) => Effect.Effect; readonly invalidateLocalStatus: (cwd: string) => Effect.Effect; readonly invalidateRemoteStatus: (cwd: string) => Effect.Effect; + readonly readRefSnapshot: (cwd: string) => Effect.Effect; readonly invalidateStatus: (cwd: string) => Effect.Effect; /** The checkout's own HEAD file, inside a linked worktree's admin directory when there is one. */ readonly headPath: (cwd: string) => Effect.Effect; @@ -121,6 +123,10 @@ export class GitWorkflowService extends Context.Service< { readonly commitSha: string; readonly remoteRefName: string } | null, GitCommandError >; + readonly resolveDefaultBranchName: ( + cwd: string, + remoteName: string, + ) => Effect.Effect; readonly removeWorktree: ( input: VcsRemoveWorktreeInput, ) => Effect.Effect; @@ -336,11 +342,11 @@ export const make = Effect.gen(function* () { ), ); }), - localStatus: Effect.fn("GitWorkflowService.localStatus")(function (input) { + localStatus: Effect.fn("GitWorkflowService.localStatus")(function (input, options) { return detectGitRepositoryForStatus("GitWorkflowService.localStatus", input.cwd).pipe( Effect.flatMap((isGitRepository) => isGitRepository - ? gitManager.localStatus(input) + ? gitManager.localStatus(input, options) : Effect.succeed(nonRepositoryLocalStatus()), ), ); @@ -390,6 +396,7 @@ export const make = Effect.gen(function* () { }), invalidateLocalStatus: gitManager.invalidateLocalStatus, invalidateRemoteStatus: gitManager.invalidateRemoteStatus, + readRefSnapshot: git.readRefSnapshot, invalidateStatus: gitManager.invalidateStatus, headPath: Effect.fn("GitWorkflowService.headPath")(function (cwd) { return git @@ -497,6 +504,13 @@ export const make = Effect.gen(function* () { ); }, ), + resolveDefaultBranchName: Effect.fn("GitWorkflowService.resolveDefaultBranchName")( + function (cwd, remoteName) { + return ensureGitCommand("GitWorkflowService.resolveDefaultBranchName", cwd).pipe( + Effect.andThen(git.resolveDefaultBranchName(cwd, remoteName)), + ); + }, + ), removeWorktree: Effect.fn("GitWorkflowService.removeWorktree")(function (input) { return ensureGitCommand("GitWorkflowService.removeWorktree", input.cwd).pipe( Effect.andThen(git.removeWorktree(input)), diff --git a/apps/server/src/guest/GuestShareService.test.ts b/apps/server/src/guest/GuestShareService.test.ts index ae687c437b681..b377cd1ce00b0 100644 --- a/apps/server/src/guest/GuestShareService.test.ts +++ b/apps/server/src/guest/GuestShareService.test.ts @@ -37,6 +37,7 @@ import { resolveAttachmentPathById, sweepStalePendingAttachments, } from "../attachmentStore.ts"; +import { resolveAttachmentRelativePath } from "../attachmentPaths.ts"; import * as ServerSecretStore from "../auth/ServerSecretStore.ts"; import { CloudManagedEndpointRuntime } from "../cloud/ManagedEndpointRuntime.ts"; import * as ServerConfig from "../config.ts"; @@ -51,6 +52,7 @@ import * as ProjectionStore from "../orchestration-v2/ProjectionStore.ts"; import * as ProviderAdapterRegistry from "../orchestration-v2/ProviderAdapterRegistry.ts"; import * as ResourceCleanupService from "../orchestration-v2/ResourceCleanupService.ts"; import * as ProviderReplayHarness from "../orchestration-v2/testkit/ProviderReplayHarness.ts"; +import * as DocumentText from "../provider/DocumentText.ts"; import * as ProviderRegistry from "../provider/ProviderRegistry.ts"; import * as ServerSettings from "../serverSettings.ts"; import * as AnalyticsService from "../telemetry/AnalyticsService.ts"; @@ -148,6 +150,7 @@ const layerTest = GuestShareService.layer.pipe( Layer.provide(layerConnect), Layer.provideMerge(layerAnalytics), Layer.provide(ServerSecretStore.layer), + Layer.provide(DocumentText.layer), Layer.provideMerge(layerConfig), Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), ); @@ -447,6 +450,10 @@ const writeItem = ( | Pick< Extract, "type" | "status" | "fileName" | "diffStr" | "oldStr" | "newStr" + > + | Pick< + Extract, + "type" | "status" | "toolName" | "input" | "output" >, ) => Effect.gen(function* () { @@ -537,6 +544,80 @@ it.effect("gives the guest a command's detail and output, but no file diffs", () }).pipe(Effect.provide(layerTest)), ); +it.effect("gives a fork's guest the commands the fork shows from its source thread", () => + Effect.gen(function* () { + const { orchestrator, guestShares } = yield* shareThread; + const command = yield* writeItem("item:command", { + type: "command_execution", + status: "completed", + input: "ls /work/src", + output: "/work/src/auth.ts\n", + }); + const { attachmentsDir } = yield* ServerConfig.ServerConfig; + const page = createAttachmentId(threadId, "html")!; + yield* (yield* FileSystem.FileSystem).writeFileString( + resolveAttachmentRelativePath({ attachmentsDir, relativePath: `${page}.html` })!, + "

Plan

", + ); + const render = yield* writeItem("item:render", { + type: "dynamic_tool", + status: "completed", + toolName: "mcp__t3-code__html_render", + input: {}, + output: [ + { + type: "text", + text: JSON.stringify({ htmlRender: { attachmentId: page, title: "Plan", height: 300 } }), + }, + ], + }); + const eventSink = yield* EventSink.EventSinkV2; + const now = yield* DateTime.now; + const run = (yield* orchestrator.getThreadRecords(threadId, ["runs"])).runs[0]!; + yield* eventSink.write({ + events: [ + { + id: EventId.make("event:run-done"), + type: "run.updated", + threadId, + runId: run.id, + providerInstanceId: instanceId, + occurredAt: now, + payload: { ...run, status: "completed", completedAt: now }, + }, + ], + }); + const forkId = ThreadId.make("thread:guest-share-fork"); + yield* orchestrator.dispatch({ + type: "thread.fork", + commandId: CommandId.make("fork"), + sourceThreadId: threadId, + targetThreadId: forkId, + sourcePoint: { type: "run", runId: run.id }, + createdBy: "user", + creationSource: "web", + }); + const { url } = yield* guestShares.create({ threadId: forkId }); + const { token: session } = yield* guestShares.redeem({ + credential: url.slice(url.indexOf("#") + 1), + name: "Sam", + }); + + const detail = (yield* guestShares.item(session, command)).item; + assert.deepInclude(detail, { input: "ls /work/src", output: "/work/src/auth.ts\n" }); + const frames = Array.from( + yield* Stream.runCollect(yield* guestShares.commandOutput(session, command)), + ); + assert.deepEqual(frames, [ + { kind: "replace", text: "/work/src/auth.ts\n", truncated: false, running: false }, + ]); + assert.deepEqual(yield* guestShares.render(session, render), { + path: resolveAttachmentPathById({ attachmentsDir, attachmentId: page })!, + title: "Plan", + }); + }).pipe(Effect.provide(layerTest)), +); + it.effect("pages the guest back through turns older than the stream's snapshot", () => Effect.gen(function* () { const { guestShares, credential } = yield* shareThread; @@ -731,6 +812,11 @@ it.effect("refuses too many files, too many bytes, and images that are not what contentType: "text/html", bytes: new TextEncoder().encode(""), }, + { + name: "notes.rtf", + contentType: "application/rtf", + bytes: new TextEncoder().encode(String.raw`{\rtf1 Available}`), + }, ]), ); const [message] = yield* waitingMessages; @@ -744,26 +830,46 @@ it.effect("refuses too many files, too many bytes, and images that are not what [ { type: "image", name: "screen.png", mimeType: "image/png", sizeBytes: PNG.length }, { type: "file", name: "notes_fdp.html", mimeType: "text/html", sizeBytes: 3 }, + { type: "file", name: "notes.rtf", mimeType: "application/rtf", sizeBytes: 17 }, ], ); + // Agents read a guest's documents through the same extracted text as the owner's uploads. + const { attachmentsDir } = yield* ServerConfig.ServerConfig; + const document = resolveAttachmentPath({ + attachmentsDir, + attachment: message!.attachments![2]!, + }); + assert.equal( + yield* (yield* FileSystem.FileSystem).readFileString(`${document}.txt`), + "Available", + ); }).pipe(Effect.scoped, Effect.provide(layerTest)), ); it.effect("caps the bytes of files waiting in one share", () => Effect.gen(function* () { const { guestShares, session, shareId } = yield* joinedGuest; - const submit = (sizeBytes: number) => + const submit = (sizeBytes: number, ...more: Parameters[0]) => Effect.gen(function* () { return guestShares.submit( session, { shareId, text: "Look" }, - yield* uploadsOf([png("big.png", sizeBytes)]), + yield* uploadsOf([png("big.png", sizeBytes), ...more]), ); }).pipe(Effect.flatten); yield* submit(9 * MIB); yield* submit(9 * MIB); - const error = yield* Effect.flip(submit(9 * MIB)); + const before = yield* storedFiles; + const error = yield* Effect.flip( + submit(9 * MIB, { + name: "notes.rtf", + contentType: "application/rtf", + bytes: new TextEncoder().encode(String.raw`{\rtf1 Available}`), + }), + ); assert.equal(error.reason, "files_busy"); + // The refused message keeps neither its files nor their extracted text. + assert.deepEqual(yield* storedFiles, before); // Text alone still goes through. yield* guestShares.submit(session, { shareId, text: "One more thing" }, []); assert.equal((yield* waitingMessages).length, 3); diff --git a/apps/server/src/guest/GuestShareService.ts b/apps/server/src/guest/GuestShareService.ts index 36be1d1e21c27..b5b21a7af2ae7 100644 --- a/apps/server/src/guest/GuestShareService.ts +++ b/apps/server/src/guest/GuestShareService.ts @@ -55,6 +55,7 @@ import { projectTurnItemForWire, threadStreamEventItem, } from "../orchestration-v2/WireProjection.ts"; +import * as DocumentText from "../provider/DocumentText.ts"; import * as ProviderRegistry from "../provider/ProviderRegistry.ts"; import * as ServerSettings from "../serverSettings.ts"; import * as AnalyticsService from "../telemetry/AnalyticsService.ts"; @@ -227,6 +228,7 @@ const make = Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; const random = yield* SecureRandom.SecureRandom; const analytics = yield* AnalyticsService.AnalyticsService; + const documents = yield* DocumentText.DocumentText; const signer = yield* PayloadSigner.make(SIGNING_SECRET_NAME); /** A truncated HMAC keeps invite links short. 128 bits is still far beyond guessing range. */ const inviteSignature = (shareId: string) => @@ -393,7 +395,12 @@ const make = Effect.gen(function* () { const path = resolveAttachmentPath({ attachmentsDir, attachment }); return path === null ? Effect.void - : fileSystem.remove(path, { force: true }).pipe(Effect.ignore); + : fileSystem + .remove(path, { force: true }) + .pipe( + Effect.andThen(fileSystem.remove(`${path}.txt`, { force: true })), + Effect.ignore, + ); }, { discard: true }, ); @@ -439,6 +446,7 @@ const make = Effect.gen(function* () { yield* fileSystem.makeDirectory(attachmentsDir, { recursive: true }); yield* fileSystem.copyFile(uploads[index]!.path, path); stored.push(attachment); + yield* documents.ensure(path); }).pipe(Effect.mapError(() => uploadFailed)), { discard: true }, ).pipe(Effect.onError(() => removeStored(stored))); @@ -631,17 +639,26 @@ const make = Effect.gen(function* () { itemId: string, ) { const { thread } = yield* authorize(session, "session"); - const item = yield* orchestrator - .getTurnItem({ threadId: thread.id, itemId: TurnItemId.make(itemId) }) - .pipe(Effect.mapError(() => unavailable)); + const id = TurnItemId.make(itemId); + const item = + (yield* orchestrator + .getTurnItem({ threadId: thread.id, itemId: id }) + .pipe(Effect.mapError(() => unavailable))) ?? + // A fork shows its source's earlier items, which stay in the source thread. + (thread.forkedFrom === null + ? null + : yield* orchestrator.getTimelinePage(thread.id, { itemId: id, limit: 1 }).pipe( + Effect.map((page) => page.items[0]?.item ?? null), + Effect.mapError(() => unavailable), + )); return item === null ? yield* unavailable : { thread, item }; }); const render = Effect.fn("GuestShareService.render")(function* (session: string, itemId: string) { - const { thread, item } = yield* threadItem(session, itemId); + const { item } = yield* threadItem(session, itemId); if (item.type !== "dynamic_tool") return yield* unavailable; const reference = htmlRenderFromToolItem(item); - const [attachmentId] = threadHtmlRenderAttachmentIds(thread.id, [item]); + const [attachmentId] = threadHtmlRenderAttachmentIds(item.threadId, [item]); const path = attachmentId === undefined ? null @@ -677,9 +694,9 @@ const make = Effect.gen(function* () { session: string, itemId: string, ) { - const { thread, item } = yield* threadItem(session, itemId); + const { item } = yield* threadItem(session, itemId); if (item.type !== "command_execution") return yield* unavailable; - return commandOutputs.subscribe({ threadId: thread.id, itemId: item.id }).pipe( + return commandOutputs.subscribe({ threadId: item.threadId, itemId: item.id }).pipe( // A lost hub stream ends the output; the row reloads it when reopened. Stream.catchCause(() => Stream.empty), Stream.interruptWhen(untilRevoked(session)), diff --git a/apps/server/src/htmlRender/HtmlRender.test.ts b/apps/server/src/htmlRender/HtmlRender.test.ts index a5a89e3d19707..4ef5d1d1db58e 100644 --- a/apps/server/src/htmlRender/HtmlRender.test.ts +++ b/apps/server/src/htmlRender/HtmlRender.test.ts @@ -242,6 +242,11 @@ describe("HtmlRender", () => { const reused = yield* htmlRender.publish({ threadId, ...page }); yield* mcpSessions.set({ ...session, browserToolsAvailable: true }); expect(yield* mcpSessions.takeHtmlRender(threadId, page)).toEqual(reused); + const first = yield* htmlRender.publish({ threadId, ...page }); + const second = yield* htmlRender.publish({ threadId, ...page }); + expect(yield* mcpSessions.takeHtmlRender(threadId, page)).toEqual(first); + expect(yield* mcpSessions.takeHtmlRender(threadId, page)).toEqual(second); + expect(yield* mcpSessions.takeHtmlRender(threadId, page)).toBe(undefined); yield* htmlRender.publish({ threadId, ...page }); yield* mcpSessions.set({ diff --git a/apps/server/src/http.ts b/apps/server/src/http.ts index bbff00acb6f78..8a4bebbec83c4 100644 --- a/apps/server/src/http.ts +++ b/apps/server/src/http.ts @@ -52,6 +52,7 @@ import { browserApiCorsAllowedHeaders, browserApiCorsAllowedMethods, DESKTOP_RENDERER_ORIGINS, + MCP_HTTP_PATH, } from "./httpCors.ts"; import { requestUrl } from "./requestUrl.ts"; @@ -258,7 +259,7 @@ export const layerBrowserApiCors = Layer.unwrap( const config = yield* ServerConfig.ServerConfig; const devOrigin = config.devUrl?.origin; // Credentialed dev requests need explicit Vite/Electron origins; packaged desktop uses the default wildcard. - return HttpRouter.cors({ + const cors = HttpMiddleware.cors({ ...(devOrigin ? { allowedOrigins: [devOrigin, ...DESKTOP_RENDERER_ORIGINS, ...config.devAllowedOrigins], @@ -269,6 +270,15 @@ export const layerBrowserApiCors = Layer.unwrap( allowedHeaders: browserApiCorsAllowedHeaders, maxAge: 600, }); + return HttpRouter.middleware( + (httpEffect) => { + const withCors = cors(httpEffect); + return Effect.flatMap(HttpServerRequest.HttpServerRequest, (request) => + request.url.split("?", 1)[0] === MCP_HTTP_PATH ? httpEffect : withCors, + ); + }, + { global: true }, + ); }), ); diff --git a/apps/server/src/httpCors.ts b/apps/server/src/httpCors.ts index 18b537f17abad..b96e58ead9fca 100644 --- a/apps/server/src/httpCors.ts +++ b/apps/server/src/httpCors.ts @@ -11,3 +11,6 @@ export const browserApiCorsAllowedHeaders = [ ] as const; export const DESKTOP_RENDERER_ORIGINS = ["t3code://app", "t3code-dev://app"] as const; + +/** MCP refuses every browser Origin with 403, so the browser CORS layer must not answer for it. */ +export const MCP_HTTP_PATH = "/mcp"; diff --git a/apps/server/src/imageMime.test.ts b/apps/server/src/imageMime.test.ts index 5d427b957265a..87fb46d912939 100644 --- a/apps/server/src/imageMime.test.ts +++ b/apps/server/src/imageMime.test.ts @@ -10,16 +10,22 @@ describe("imageMime", () => { }); }); - it("parses base64 data URL with spaces in payload", () => { - expect(parseBase64DataUrl("data:image/png;base64,SGVs bG8=\n")).toEqual({ - mimeType: "image/png", - base64: "SGVsbG8=", - }); - }); + it.each(["\t", "\n", "\f", "\r", " "])( + "parses base64 data URL with ASCII whitespace %j", + (space) => { + expect(parseBase64DataUrl(`data:image/png;base64,SGVs${space}bG8=\n`)).toEqual({ + mimeType: "image/png", + base64: "SGVsbG8=", + }); + }, + ); it("rejects payload with characters outside the base64 alphabet", () => { expect(parseBase64DataUrl("data:image/png;base64,SGVs!bG8=")).toBeNull(); expect(parseBase64DataUrl("data:image/png;base64,SGVs,bG8=")).toBeNull(); + for (const space of ["\v", "\u00a0", "\u2003"]) { + expect(parseBase64DataUrl(`data:image/png;base64,SGVs${space}bG8=`)).toBeNull(); + } }); // regression: #3952 image upload crashed dispatchCommand with a stack overflow @@ -43,7 +49,7 @@ describe("imageMime", () => { it("rejects empty and whitespace-only payloads", () => { expect(parseBase64DataUrl("data:image/png;base64,")).toBeNull(); - expect(parseBase64DataUrl("data:image/png;base64, \r\n")).toBeNull(); + expect(parseBase64DataUrl("data:image/png;base64, \t\r\n\f")).toBeNull(); }); it("parses a case-insensitive scheme and mime type", () => { diff --git a/apps/server/src/imageMime.ts b/apps/server/src/imageMime.ts index 338bbd62ce592..4c985dccb8235 100644 --- a/apps/server/src/imageMime.ts +++ b/apps/server/src/imageMime.ts @@ -42,7 +42,7 @@ function isBase64Char(code: number): boolean { } function isBase64Whitespace(code: number): boolean { - return code === 0x0d || code === 0x0a || code === 0x20; + return code === 0x09 || code === 0x0a || code === 0x0c || code === 0x0d || code === 0x20; } // V8 regexes use the JS stack; scanning image payloads can overflow a deep fiber stack. diff --git a/apps/server/src/mcp/McpDeviceToolkit.test.ts b/apps/server/src/mcp/McpDeviceToolkit.test.ts index 1904a24010bee..11a4e9ca589e5 100644 --- a/apps/server/src/mcp/McpDeviceToolkit.test.ts +++ b/apps/server/src/mcp/McpDeviceToolkit.test.ts @@ -135,7 +135,7 @@ const layerDeviceServiceMock = Layer.mock(DeviceService.DeviceService)({ screenshot: () => Effect.succeed({ device, png }), close: () => Effect.void, testHost: () => Effect.die("not used"), - // As for an EAS device, agent-device knows the device by a different id than T3. + // As for an Expo device, agent-device knows the device by a different id than T3. agentTarget: () => Effect.succeed({ agentDeviceId: "SIM-UDID", @@ -235,7 +235,7 @@ it.effect("registers the device tools and returns the screenshot as image conten expect(denied.content).toEqual([ { type: "text", - text: "Agent device access is turned off for this environment. Ask the user to turn on Agent device access in Settings > Integrations > Devices; it applies when the agent session next starts, such as in a new thread.", + text: "Agent device access is turned off for this environment. Ask the user to turn on the Device hub and Agent device access in Settings > Integrations > Devices; it applies when the agent session next starts, such as in a new thread.", }, ]); }), diff --git a/apps/server/src/mcp/McpHttpServer.test.ts b/apps/server/src/mcp/McpHttpServer.test.ts index 3802a5c2e4023..5801a210ba6cf 100644 --- a/apps/server/src/mcp/McpHttpServer.test.ts +++ b/apps/server/src/mcp/McpHttpServer.test.ts @@ -43,11 +43,13 @@ import * as ServerSecretStore from "../auth/ServerSecretStore.ts"; import * as ServerEnvironment from "../environment/ServerEnvironment.ts"; import * as Sqlite from "../persistence/Sqlite.ts"; import * as ManagedPolicy from "../policy/ManagedPolicy.ts"; +import * as PreviewManager from "../preview/Manager.ts"; import * as DeviceService from "../device/DeviceService.ts"; import * as PullRequestReviewGuideService from "../pullRequest/PullRequestReviewGuideService.ts"; import * as PullRequestService from "../pullRequest/PullRequestService.ts"; import * as ServerConfig from "../config.ts"; +import * as ServerHttp from "../http.ts"; import * as ServerSettings from "../serverSettings.ts"; import { advertisedJsonSchema } from "./advertisedJsonSchema.ts"; import * as McpHttpServer from "./McpHttpServer.ts"; @@ -59,7 +61,9 @@ import * as OrchestratorMcpService from "./OrchestratorMcpService.ts"; import * as PreviewAutomationBroker from "./PreviewAutomationBroker.ts"; import { OrchestratorToolkit } from "./toolkits/orchestrator/tools.ts"; import { PreviewToolkit } from "./toolkits/preview/tools.ts"; +import { PreviewControlsToolkit } from "./toolkits/previewControls/tools.ts"; import { ProjectToolkit } from "./toolkits/project/tools.ts"; +import { ThreadToolkit } from "./toolkits/thread/tools.ts"; const environmentId = EnvironmentId.make("environment-mcp-test"); const threadId = ThreadId.make("thread-mcp-test"); @@ -273,13 +277,32 @@ it.effect.each([ ), ); +it.effect("rejects a metadata-only response when the screenshot is saved", () => + Effect.scoped( + Effect.gen(function* () { + yield* serveSnapshots("mcp-missing-pixels", { + ...snapshotResult, + screenshot: { width: 10, height: 5 }, + }); + const result = yield* callSnapshot({ save: true }); + expect(result.isError).toBe(true); + expect(result.content).toEqual([ + { type: "text", text: expect.stringContaining("did not contain requested PNG pixels") }, + ]); + }), + ).pipe(Effect.provide(layerTest)), +); + const layerRecordingTools = (calls: Array) => { const RecordingToolkit = Toolkit.make( OrchestratorToolkit.tools.delegate_task, OrchestratorToolkit.tools.t3_thread_list, OrchestratorToolkit.tools.t3_thread_read, OrchestratorToolkit.tools.t3_thread_wait, + ProjectToolkit.tools.t3_project_list, ProjectToolkit.tools.t3_project_update, + ThreadToolkit.tools.t3_queue, + PreviewControlsToolkit.tools.t3_preview_list, ProjectToolkit.tools.t3_thread_launch, ); const record = McpToolAccess.reads((params: unknown) => @@ -296,7 +319,10 @@ const layerRecordingTools = (calls: Array) => { t3_thread_list: record, t3_thread_read: record, t3_thread_wait: record, + t3_project_list: record, t3_project_update: record, + t3_queue: record, + t3_preview_list: record, t3_thread_launch: record, }), ).pipe( @@ -306,6 +332,8 @@ const layerRecordingTools = (calls: Array) => { McpToolAccessTestkit.liveThreadsLayer, Layer.mock(OrchestratorMcpService.OrchestratorMcpService)({}), Layer.mock(ProjectService.ProjectService)({}), + Layer.mock(PreviewManager.PreviewManager)({}), + Layer.mock(PreviewAutomationBroker.PreviewAutomationBroker)({}), Layer.mock(ThreadLaunchService.ThreadLaunchService)({}), Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({ namedProjectsRoot: process.cwd(), @@ -418,7 +446,9 @@ it.effect.each([ tabId: alternateTabId, threadId, }); - expect(event.request.input).toEqual({}); + expect(event.request.input).toEqual( + images && requests !== 7 ? {} : { pixelsRequired: false }, + ); return broker.respond({ clientId: "mcp-image-option-client", connectionId: event.connectionId, @@ -427,7 +457,8 @@ it.effect.each([ result: { ...page, title: `Snapshot ${requests}`, - screenshot: { ...screenshot, data: png }, + screenshot: + images || requests === 7 ? { ...screenshot, data: png } : { width: 1, height: 1 }, }, }); }).pipe(Effect.forkScoped); @@ -545,11 +576,12 @@ it.effect("names the screenshot destination when saving it fails", () => Effect.gen(function* () { const config = yield* ServerConfig.ServerConfig; yield* (yield* FileSystem.FileSystem).writeFileString(config.browserArtifactsDir, "occupied"); - yield* serveSnapshots("mcp-save-failure-client", snapshotResult); + const inputs = yield* serveSnapshots("mcp-save-failure-client", snapshotResult); const snapshot = yield* callSnapshot({ tabId: alternateTabId, save: true }); const text = snapshot.content[0]?.type === "text" ? snapshot.content[0].text : ""; + expect(inputs).toEqual([{ fullScreenshot: true }]); expect(snapshot.isError).toBe(true); expect(text).toMatch(/^Preview snapshot failed: Could not save preview screenshot to /); expect(text).toContain(config.browserArtifactsDir); @@ -572,13 +604,11 @@ it.effect("refuses preview tools to a client outside a thread before they run", const click = yield* server .callTool({ name: "preview_act", arguments: { action: "click", locator: "text=Send" } }) .pipe( + Effect.flip, Effect.provideService(McpInvocationContext.McpInvocationContext, readOnly), Effect.provideService(McpSchema.McpServerClient, client), ); - expect(click.isError).toBe(true); - expect(click.content).toEqual([ - { type: "text", text: expect.stringContaining("needs an agent running inside T3 Code") }, - ]); + expect(click.message).toContain("Tool 'preview_act' not found"); }).pipe(Effect.provide(layerTest)), ); @@ -729,18 +759,28 @@ it.effect.each(["limit", "runLimit", "maxCharsPerItem"])( }, ); -it.effect("clamps a t3_thread_list limit above its maximum instead of rejecting the call", () => { - const calls: Array = []; - return Effect.gen(function* () { - yield* callTool("t3_thread_list", { allProjects: true, limit: 200 }); - yield* callTool("t3_thread_list", { allProjects: true, limit: 7 }); +const clampedListLimits = [ + { tool: "t3_thread_list", input: { allProjects: true }, maximum: 100 }, + { tool: "t3_project_list", input: {}, maximum: 100 }, + { tool: "t3_queue", input: { action: "list" }, maximum: 100 }, + { tool: "t3_preview_list", input: {}, maximum: 50 }, +]; - expect(calls).toEqual([ - { allProjects: true, limit: 100 }, - { allProjects: true, limit: 7 }, - ]); - }).pipe(Effect.provide(layerRecordingTools(calls))); -}); +it.effect.each(clampedListLimits)( + "clamps a $tool limit above its maximum instead of rejecting the call", + ({ tool, input, maximum }) => { + const calls: Array = []; + return Effect.gen(function* () { + yield* callTool(tool, { ...input, limit: maximum * 2 }); + yield* callTool(tool, { ...input, limit: 7 }); + + expect(calls).toEqual([ + { ...input, limit: maximum }, + { ...input, limit: 7 }, + ]); + }).pipe(Effect.provide(layerRecordingTools(calls))); + }, +); it("keeps cross-tool routing out of the delegate_task description", () => { const description = OrchestratorToolkit.tools.delegate_task.description ?? ""; @@ -1216,6 +1256,12 @@ it.effect("admits provider and OAuth client credentials and points only clients ), }), ), + Layer.provide(ServerHttp.layerBrowserApiCors), + Layer.provide( + ServerConfig.layerTest(process.cwd(), { prefix: "t3-mcp-origin-test-" }).pipe( + Layer.provide(NodeServices.layer), + ), + ), ); const traced = recordSpans(); yield* HttpRouter.serve(serverLayer, { disableListenLog: true, disableLogger: true }).pipe( @@ -1298,6 +1344,30 @@ it.effect("admits provider and OAuth client credentials and points only clients (span) => span.name.startsWith("mcp.tool.") || span.name.startsWith("McpServer."), ), ).toEqual([]); + + // A present, disallowed Origin is refused with 403 before credentials or CORS answer. + const foreignOrigin = (method: "POST" | "OPTIONS", token: string | undefined) => + httpClient.execute( + HttpClientRequest.make(method)("/mcp", { + headers: { + origin: "https://untrusted.invalid", + accept: "application/json, text/event-stream", + "access-control-request-method": "POST", + ...(token === undefined ? {} : { authorization: `Bearer ${token}` }), + }, + ...(method === "POST" + ? { + body: HttpBody.text( + `{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"probe","version":"1.0.0"}}}`, + "application/json", + ), + } + : {}), + }), + ); + expect((yield* foreignOrigin("POST", providerToken)).status).toBe(403); + expect((yield* foreignOrigin("POST", undefined)).status).toBe(403); + expect((yield* foreignOrigin("OPTIONS", undefined)).status).toBe(403); }), ).pipe(Effect.provide(NodeHttpServer.layerTest)), ); diff --git a/apps/server/src/mcp/McpHttpServer.ts b/apps/server/src/mcp/McpHttpServer.ts index 28dad0f14fb6c..5afd3c0218126 100644 --- a/apps/server/src/mcp/McpHttpServer.ts +++ b/apps/server/src/mcp/McpHttpServer.ts @@ -91,6 +91,7 @@ import * as HtmlHandlers from "./toolkits/html/handlers.ts"; import { MessageToolkit } from "./toolkits/message/tools.ts"; import * as MessageHandlers from "./toolkits/message/handlers.ts"; import { HtmlPreviewTool, HtmlPreviewToolkit, HtmlRenderToolkit } from "./toolkits/html/tools.ts"; +import { MCP_HTTP_PATH } from "../httpCors.ts"; import { requestUrl } from "../requestUrl.ts"; const mcpResourceMetadataUrl = (request: HttpServerRequest.HttpServerRequest) => @@ -281,7 +282,10 @@ const makeMcpAuthMiddleware = Effect.gen(function* () { }); // Untraced: a span here would wrap the tool call; the HTTP server span already covers it. return Effect.fnUntraced(function* (httpEffect) { - const admitted = yield* authenticateRequest(yield* HttpServerRequest.HttpServerRequest); + const request = yield* HttpServerRequest.HttpServerRequest; + // The transport allows no browser Origin; refuse it before credentials, as the MCP spec requires. + if (request.headers.origin !== undefined) return HttpServerResponse.empty({ status: 403 }); + const admitted = yield* authenticateRequest(request); if (HttpServerResponse.isHttpServerResponse(admitted)) return admitted; return yield* httpEffect.pipe( Effect.provideService(McpInvocationContext.McpInvocationContext, admitted), @@ -576,7 +580,9 @@ const previewSnapshotFailure = Effect.fn("previewSnapshotFailure")(function ( : "PreviewSnapshotError"; // Preview errors contain server-authored recovery guidance, never page output. const message = - isPreviewAutomationError(firstFailure) || isPreviewScreenshotSaveError(firstFailure) + isPreviewAutomationError(firstFailure) || + isPreviewScreenshotSaveError(firstFailure) || + isOrchestratorMcpFailure(firstFailure) ? firstFailure.message : parameterValidationMessage(firstFailure); const result = new McpSchema.CallToolResult({ @@ -643,17 +649,28 @@ const registerPreviewSnapshot = Effect.fn("McpHttpServer.registerPreviewSnapshot const snapshot = encodedResult as SnapshotMetadata & { readonly url: string; readonly screenshot: { - readonly mimeType: "image/png"; - readonly data: string; + readonly mimeType?: "image/png"; + readonly data?: string; readonly width: number; readonly height: number; }; readonly fullScreenshot?: { readonly data: string }; }; const { screenshot, fullScreenshot, ...page } = snapshot; - const png = new Uint8Array(Buffer.from(screenshot.data, "base64")); + const includeImage = payload?.includeImage === true; + const needsPixels = payload?.save === true || includeImage; + if (needsPixels && (screenshot.mimeType !== "image/png" || !screenshot.data)) { + return yield* new OrchestratorMcpFailure({ + code: "invalid_request", + message: "Preview snapshot did not contain requested PNG pixels.", + }); + } + const png = + needsPixels && screenshot.data + ? new Uint8Array(Buffer.from(screenshot.data, "base64")) + : undefined; const screenshotPath = - payload?.save === true + payload?.save === true && png ? yield* saveScreenshot( snapshot.url, fullScreenshot @@ -662,7 +679,6 @@ const registerPreviewSnapshot = Effect.fn("McpHttpServer.registerPreviewSnapshot ) : undefined; // Providers replay images on later requests, and some reject them; include images only when requested. - const includeImage = payload?.includeImage === true; if (screenshotPath !== undefined && !includeImage) { const saved = { url: cutText(snapshot.url, MAX_SNAPSHOT_IDENTIFIER_CHARS), @@ -677,7 +693,7 @@ const registerPreviewSnapshot = Effect.fn("McpHttpServer.registerPreviewSnapshot const metadata = { ...page, screenshot: { - mimeType: screenshot.mimeType, + mimeType: "image/png" as const, width: screenshot.width, height: screenshot.height, }, @@ -706,8 +722,8 @@ const registerPreviewSnapshot = Effect.fn("McpHttpServer.registerPreviewSnapshot text: `Snapshot text was bounded. Omitted: ${bounded.omitted.join("; ")}.`, }, ]), - ...(includeImage - ? [{ type: "image" as const, data: png, mimeType: screenshot.mimeType }] + ...(includeImage && png + ? [{ type: "image" as const, data: png, mimeType: "image/png" as const }] : []), ], }); @@ -1320,7 +1336,7 @@ export const layerMcpTransport = (registrations: Layer.Layer { return scope?.thread?.subagent !== true; }; +/** + * `McpSchema.EnabledWhen` for tools that act as the calling thread: a client + * from outside has none. Discovery only; `McpToolAccess` still refuses the + * call, and `toLayer` requires this on every thread-bound declaration. + */ +export const hiddenFromOutsideClients = (): boolean => { + const fiber = Fiber.getCurrent(); + const scope = fiber && Context.getOrUndefined(fiber.context, McpInvocationContext); + return scope === undefined || scope.thread !== undefined; +}; + /** `McpSchema.EnabledWhen` for tools that only serve links, hidden while the beta is off. */ export const hiddenWhileLinksOff = (): boolean => { const fiber = Fiber.getCurrent(); diff --git a/apps/server/src/mcp/McpToolAccess.test.ts b/apps/server/src/mcp/McpToolAccess.test.ts index 6d63287f4eb2a..6cfd86bd3d836 100644 --- a/apps/server/src/mcp/McpToolAccess.test.ts +++ b/apps/server/src/mcp/McpToolAccess.test.ts @@ -13,6 +13,8 @@ import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; import * as Layer from "effect/Layer"; import * as Schema from "effect/Schema"; +import * as Sink from "effect/Sink"; +import * as Stream from "effect/Stream"; import { McpSchema, McpServer, Tool, Toolkit } from "effect/ai"; import { OrchestratorProjectionError } from "../orchestration-v2/Orchestrator.ts"; @@ -113,8 +115,14 @@ const probe = { }; const ProbeToolkit = Toolkit.make( Tool.make("reads", probe), - Tool.make("reads_as_caller", probe), - Tool.make("acts_as_caller", probe), + Tool.make("reads_as_caller", probe).annotate( + McpSchema.EnabledWhen, + McpInvocationContext.hiddenFromOutsideClients, + ), + Tool.make("acts_as_caller", probe).annotate( + McpSchema.EnabledWhen, + McpInvocationContext.hiddenFromOutsideClients, + ), Tool.make("writes", probe), Tool.make("writes_with_capability", probe), Tool.make("writes_threads", { @@ -254,7 +262,6 @@ it.effect.each([ ["writes_environment", readOnlyClient, {}, "capability_denied"], ["reads_as_caller", ended, {}, "ran"], - ["reads_as_caller", fullAccessClient, {}, "thread_credential_required"], ["acts_as_caller", supervised, {}, "ran"], ["acts_as_caller", ended, {}, "parent_not_active"], ["acts_as_caller", background, {}, "ran"], @@ -262,7 +269,6 @@ it.effect.each([ ["acts_as_caller", mismatchedProvider, {}, "parent_not_active"], ["acts_as_caller", archivedBackground, {}, "parent_not_active"], ["acts_as_caller", unknownOwnerBackground, {}, "parent_not_active"], - ["acts_as_caller", fullAccessClient, {}, "thread_credential_required"], ["writes", supervised, {}, "ran"], ["writes", ended, {}, "parent_not_active"], @@ -455,7 +461,12 @@ it.effect.each([ it.effect("refuses a linked environment to a tool that acts as its calling thread", () => Effect.gen(function* () { - const CallerToolkit = Toolkit.make(Tool.make("acts_as_caller", linkedProbe)); + const CallerToolkit = Toolkit.make( + Tool.make("acts_as_caller", linkedProbe).annotate( + McpSchema.EnabledWhen, + McpInvocationContext.hiddenFromOutsideClients, + ), + ); const built = yield* Layer.build( McpToolAccess.HandlersLayer.layer( McpToolAccess.toLayer(CallerToolkit, { @@ -466,3 +477,36 @@ it.effect("refuses a linked environment to a tool that acts as its calling threa expect(Exit.isFailure(built) && Cause.hasDies(built.cause)).toBe(true); }), ); + +// Discovery hides these tools. The handler must refuse on its own if that ever changes. +it.effect.each(["reads_as_caller", "acts_as_caller"] as const)( + "%s refuses an outside client behind discovery", + (tool) => + Effect.gen(function* () { + const built = yield* ProbeToolkit; + const { result } = yield* built + .handle(tool, {}) + .pipe(Stream.unwrap, Stream.run(Sink.last()), Effect.flatMap(Effect.fromOption)); + expect(result).toMatchObject({ code: "thread_credential_required" }); + }).pipe( + Effect.provide(McpToolAccess.HandlersLayer.layer(layerProbeHandlers)), + Effect.provideService(McpInvocationContext.McpInvocationContext, fullAccessClient), + Effect.provide(Layer.mock(ThreadManagement.ThreadManagementService)({})), + ), +); + +it.effect("refuses to build a thread-bound tool that outside clients would be offered", () => + Effect.gen(function* () { + const CallerToolkit = Toolkit.make(Tool.make("reads_as_caller", probe)); + const built = yield* Layer.build( + McpToolAccess.HandlersLayer.layer( + McpToolAccess.toLayer(CallerToolkit, { + reads_as_caller: McpToolAccess.readsAsCaller(() => ran), + }), + ), + ).pipe(Effect.scoped, Effect.exit); + expect(Exit.isFailure(built) && Cause.pretty(built.cause)).toContain( + "reads_as_caller acts as its calling thread", + ); + }), +); diff --git a/apps/server/src/mcp/McpToolAccess.ts b/apps/server/src/mcp/McpToolAccess.ts index f96a38d6d7341..20a9274798d0f 100644 --- a/apps/server/src/mcp/McpToolAccess.ts +++ b/apps/server/src/mcp/McpToolAccess.ts @@ -7,6 +7,7 @@ import { type RuntimeMode, type ThreadId, } from "@t3tools/contracts"; +import * as Context from "effect/Context"; import * as Effect from "effect/Effect"; import * as Option from "effect/Option"; import * as Predicate from "effect/Predicate"; @@ -14,6 +15,7 @@ import * as Ref from "effect/Ref"; import * as Struct from "effect/Struct"; import type * as Layer from "effect/Layer"; import type * as Scope from "effect/Scope"; +import { McpSchema } from "effect/ai"; import type { Tool, Toolkit } from "effect/ai"; import { @@ -420,8 +422,17 @@ const routed = Effect.fn("McpToolAccess.routed")(function* = { ...checked }; for (const [name, declaration] of Object.entries>(declarations)) { const tool = toolkit.tools[name]; - if (tool === undefined || !takesLinkedEnvironment(tool)) continue; + if (tool === undefined) continue; const remote = remoteCheckOf(declaration); + if ( + remote === undefined && + Context.getOrUndefined(tool.annotations, McpSchema.EnabledWhen) !== + McpInvocationContext.hiddenFromOutsideClients + ) + return yield* Effect.die( + `${name} acts as its calling thread, so outside clients must not be offered it.`, + ); + if (!takesLinkedEnvironment(tool)) continue; const forwarded = forwardedOf(declaration) as | Forwarded, never> | undefined; diff --git a/apps/server/src/mcp/OrchestratorMcpService.activity.test.ts b/apps/server/src/mcp/OrchestratorMcpService.activity.test.ts index cd8c5064476b2..742b5969b64cb 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.activity.test.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.activity.test.ts @@ -210,6 +210,7 @@ it("readThread preserves caller access errors and prefers activity-run status ov threadId === parentThreadId ? ({ ...projection.thread, + pinnedAt: now, blockedOnThreadWait: true, pendingBackgroundTasks, } as unknown as OrchestrationV2ThreadShell) @@ -244,6 +245,7 @@ it("readThread preserves caller access errors and prefers activity-run status ov expect(result.thread.status).toBe("running"); expect(result.thread.latestRunId).toBe(cancelledRunId); expect(result.thread.activeRunId).toBe(activeRunId); + expect(result.thread.pinned).toBe(true); expect(result.thread.blockedOnThreadWait).toBe(true); expect(result.thread.pendingBackgroundTasks).toEqual([ { taskId: "task-subagent", kind: "subagent", childThreadId }, diff --git a/apps/server/src/mcp/OrchestratorMcpService.test.ts b/apps/server/src/mcp/OrchestratorMcpService.test.ts index fb087724dbdb7..40b5584e95ac4 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.test.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.test.ts @@ -10,6 +10,8 @@ import { NodeId, ORCHESTRATOR_MCP_RUNNING_TASK_NEXT, type OrchestratorMcpCapabilitiesResult, + type OrchestratorMcpListScheduledTasksInput, + type Project, OrchestratorMcpTarget, type OrchestrationV2ThreadShell, type ScheduledTask, @@ -257,6 +259,8 @@ describe("OrchestratorMcpService", () => { const parentThreadId = ThreadId.make("thread:mcp-restart-parent"); const childThreadId = ThreadId.make("thread:mcp-restart-child"); const taskId = NodeId.make("node:mcp-restart-task"); + const cutRunId = RunId.make("run:mcp-restart-child"); + const continuationRunId = RunId.make("run:mcp-restart-continuation"); const dispatched = yield* Ref.make(0); let awaitsRestart = true; let readFails = true; @@ -282,10 +286,12 @@ describe("OrchestratorMcpService", () => { thread: { id: childThreadId, title: "Child" }, runs: [ { - id: RunId.make("run:mcp-restart-child"), + id: cutRunId, ordinal: 1, status: "cancelled", delegatedTaskId: taskId, + startedAt: DateTime.makeUnsafe("2026-10-03T10:00:00Z"), + completedAt: DateTime.makeUnsafe("2026-10-03T10:05:00Z"), }, { id: RunId.make("run:mcp-restart-later"), ordinal: 2, status: "running" }, ], @@ -297,12 +303,17 @@ describe("OrchestratorMcpService", () => { runtimeRequests: [], turnItems: [], } as unknown as OrchestrationV2ThreadProjection; + let childRuns = childProjection.runs; const layerDependencies = Layer.mergeAll( NodeServices.layer, NodeCrypto.layer, Layer.mock(ThreadManagementService.ThreadManagementService)({ getThreadRecords: (threadId) => - Effect.succeed(threadId === parentThreadId ? parentProjection : childProjection), + Effect.succeed( + threadId === parentThreadId + ? parentProjection + : { ...childProjection, runs: childRuns }, + ), delegatedTaskResultPending: () => readFails ? Effect.fail(new OrchestratorProjectionError({ threadId: childThreadId })) @@ -342,11 +353,63 @@ describe("OrchestratorMcpService", () => { assert.equal(held.status, "running"); assert.equal(held.workState, "working"); assert.isNull(held.summary); + assert.isNull(held.latestTerminalRunId); + assert.isNull(held.latestTerminalStatus); assert.equal(yield* Ref.get(dispatched), 0); + + const continuationRun = { + id: continuationRunId, + ordinal: 3, + status: "running", + delegatedTaskId: taskId, + startedAt: DateTime.makeUnsafe("2026-10-03T10:06:00Z"), + restartContinuationOfRunId: cutRunId, + } as unknown as (typeof childRuns)[number]; + childRuns = [...childProjection.runs, continuationRun]; + const continuing = yield* service.taskStatus(scope, taskId); + assert.equal(continuing.status, "running"); + assert.isNull(continuing.latestTerminalRunId); + assert.isNull(continuing.latestTerminalStatus); + + // A second restart cut the continuation, and its own continuation declined. + childRuns = [ + ...childProjection.runs, + { + ...continuationRun, + status: "cancelled", + completedAt: DateTime.makeUnsafe("2026-10-03T10:10:00Z"), + }, + ]; awaitsRestart = false; const settled = yield* service.taskStatus(scope, taskId); assert.equal(settled.status, "cancelled"); + assert.equal(settled.latestTerminalRunId, continuationRunId); + assert.equal(settled.latestTerminalStatus, "cancelled"); assert.equal(yield* Ref.get(dispatched), 1); + + // A continuation cancelled before it started leaves the cut run as the result. + childRuns = [ + ...childProjection.runs, + { ...continuationRun, status: "cancelled", startedAt: null }, + ]; + const unstarted = yield* service.taskStatus(scope, taskId); + assert.equal(unstarted.latestTerminalRunId, cutRunId); + assert.equal(unstarted.latestTerminalStatus, "cancelled"); + + // That unstarted continuation was itself continued, so the whole chain is still live. + childRuns = [ + ...childProjection.runs, + { ...continuationRun, status: "cancelled", startedAt: null }, + { + ...continuationRun, + id: RunId.make("run:mcp-restart-continuation-2"), + ordinal: 4, + restartContinuationOfRunId: continuationRunId, + }, + ]; + const chained = yield* service.taskStatus(scope, taskId); + assert.isNull(chained.latestTerminalRunId); + assert.isNull(chained.latestTerminalStatus); }).pipe(Effect.provide(layerOrchestratorService.pipe(Layer.provide(layerDependencies)))); }), ); @@ -1634,6 +1697,151 @@ describe("OrchestratorMcpService provider resolution", () => { }), ); + it.effect.each([ + { name: "default", timeoutMs: undefined, budgetMs: 30_000 }, + { name: "capped", timeoutMs: 3_600_000, budgetMs: 45_000 }, + { name: "shorter requested", timeoutMs: 500, budgetMs: 500 }, + ])("returns task and thread handles at the $name wait budget", (scenario) => + Effect.gen(function* () { + const subscribed = yield* Deferred.make(); + const returned = yield* Deferred.make(); + const dispatched = yield* Ref.make>([]); + const threadWaitBudgets = yield* Ref.make>([]); + const task = { + id: taskId, + threadId: parentThreadId, + runId: parentRunId, + parentNodeId, + origin: "app_owned", + providerInstanceId: codexInstanceId, + driver: "codex", + childThreadId, + model: "gpt-5.4", + status: "running", + result: null, + }; + const dependencies = Layer.mergeAll( + NodeServices.layer, + NodeCrypto.layer, + Layer.mock(ThreadManagementService.ThreadManagementService)({ + hasReceipt: () => Effect.succeed(false), + assertProviderInstanceEnabled: () => Effect.void, + isProviderInstanceEnabledForProject: () => Effect.succeed(true), + getThreadRecords: (threadId) => + Effect.succeed( + threadId === parentThreadId ? parentProjection([task]) : childProjection, + ), + getThreadEventSequence: () => Effect.succeed(0), + delegatedTaskResultPending: () => Effect.succeed(false), + streamStoredEventsFrom: () => + Stream.fromEffect(Deferred.succeed(subscribed, undefined)).pipe( + Stream.drain, + Stream.concat(Stream.never), + ), + waitForThread: (input) => + Ref.update(threadWaitBudgets, (budgets) => [...budgets, input.timeoutMs]).pipe( + Effect.as({ threadId: parentThreadId, run: null, timedOut: true }), + ), + dispatch: (command) => + Ref.update(dispatched, (commands) => [...commands, command]).pipe( + Effect.as({ + sequence: 1, + storedEvents: [{ event: { type: "subagent.updated", payload: task } }], + } as never), + ), + }), + ServerSettings.layerTest(), + providerRegistryLayer([ + providerSnapshot({ + instanceId: codexInstanceId, + driver: ProviderDriverKind.make("codex"), + model: "gpt-5.4", + }), + ]), + adapterRegistryLayer([codexInstanceId]), + Layer.mock(ProjectService.ProjectService)({}), + Layer.mock(SecretRequests.SecretRequests)({}), + Layer.mock(RemoteDelegation.RemoteDelegation)({}), + Layer.mock(ScheduledTaskService.ScheduledTaskService)({}), + ); + yield* Effect.gen(function* () { + const service = yield* OrchestratorMcpService.OrchestratorMcpService; + const budget = scenario.timeoutMs === undefined ? {} : { timeoutMs: scenario.timeoutMs }; + const waiting = yield* service + .delegateTask(scope, { task: "Review the change.", mode: "wait", ...budget }) + .pipe( + Effect.tap(() => Deferred.succeed(returned, undefined)), + Effect.forkChild, + ); + yield* Deferred.await(subscribed); + yield* TestClock.adjust(Duration.millis(scenario.budgetMs - 1)); + assert.isFalse(yield* Deferred.isDone(returned)); + yield* TestClock.adjust(Duration.millis(1)); + const result = yield* Fiber.join(waiting); + assert.include(result, { taskId, childThreadId, status: "running", waitTimedOut: true }); + assert.include((yield* Ref.get(dispatched)).at(-1), { + type: "delegated_task.wake-policy", + completionWake: "always", + }); + + yield* service.waitForThread(scope, { threadId: parentThreadId, ...budget }); + assert.deepEqual(yield* Ref.get(threadWaitBudgets), [scenario.budgetMs]); + }).pipe(Effect.provide(layerOrchestratorService.pipe(Layer.provide(dependencies)))); + }), + ); + + it.effect.each([ + { name: "ten-minute default", timeoutMs: undefined, budgetMs: 600_000 }, + { name: "one-hour maximum", timeoutMs: 3_600_000, budgetMs: 3_600_000 }, + ])("keeps a secret request pending for its $name", (scenario) => + Effect.gen(function* () { + const pending = yield* Deferred.make(); + const returned = yield* Deferred.make(); + const statuses = yield* Ref.make>([]); + const dependencies = Layer.mergeAll( + NodeServices.layer, + NodeCrypto.layer, + Layer.mock(ThreadManagementService.ThreadManagementService)({ + getThreadRecords: () => Effect.succeed({ ...parentProjection([]), turnItems: [] }), + dispatch: (command) => { + if (command.type !== "secret_request.record") return Effect.die("Unexpected command"); + return Ref.update(statuses, (values) => [...values, command.secretStatus]).pipe( + Effect.andThen(Deferred.succeed(pending, undefined)), + Effect.as({} as never), + ); + }, + }), + ServerSettings.layerTest(), + providerRegistryLayer([]), + adapterRegistryLayer([]), + Layer.mock(ProjectService.ProjectService)({}), + Layer.mock(SecretRequests.SecretRequests)({}), + Layer.mock(RemoteDelegation.RemoteDelegation)({}), + Layer.mock(ScheduledTaskService.ScheduledTaskService)({}), + ); + yield* Effect.gen(function* () { + const service = yield* OrchestratorMcpService.OrchestratorMcpService; + const waiting = yield* service + .requestSecret(scope, { + label: "Token", + reason: "Connect the account.", + ...(scenario.timeoutMs === undefined ? {} : { timeoutMs: scenario.timeoutMs }), + }) + .pipe( + Effect.tap(() => Deferred.succeed(returned, undefined)), + Effect.forkChild, + ); + yield* Deferred.await(pending); + yield* TestClock.adjust(Duration.millis(scenario.budgetMs - 1)); + assert.isFalse(yield* Deferred.isDone(returned)); + assert.deepEqual(yield* Ref.get(statuses), ["pending"]); + yield* TestClock.adjust(Duration.millis(1)); + assert.include(yield* Fiber.join(waiting), { status: "timed_out" }); + assert.deepEqual(yield* Ref.get(statuses), ["pending", "cancelled"]); + }).pipe(Effect.provide(layerOrchestratorService.pipe(Layer.provide(dependencies)))); + }), + ); + it.effect( "advertises orchestration capability from registered adapters rather than a driver allowlist", () => @@ -2253,7 +2461,7 @@ describe("OrchestratorMcpService provider resolution", () => { cause: new Error("refused by the orchestrator"), }) as never, ) - : Effect.succeed({} as never), + : Effect.succeed({ storedEvents: [] } as never), }), ServerSettings.layerTest(), providerRegistryLayer([ @@ -2283,6 +2491,68 @@ describe("OrchestratorMcpService provider resolution", () => { }), ); + it.effect("an unadvertised model refusal names advertised slugs to retry with", () => + Effect.gen(function* () { + const codex = providerSnapshot({ + instanceId: codexInstanceId, + driver: ProviderDriverKind.make("codex"), + }); + const layerDependencies = Layer.mergeAll( + NodeServices.layer, + NodeCrypto.layer, + Layer.mock(ThreadManagementService.ThreadManagementService)({ + hasReceipt: () => Effect.succeed(false), + getThreadRecords: () => Effect.succeed(parentProjection([])), + }), + ServerSettings.layerTest(), + providerRegistryLayer([ + { + ...codex, + models: [ + { slug: "claude-opus-4-1", isLegacy: true }, + ...Array.from({ length: 9 }, (_, index) => ({ slug: `claude-opus-5-${index + 1}` })), + ].map((model) => ({ ...model, name: model.slug, isCustom: false, capabilities: null })), + }, + ]), + adapterRegistryLayer([codexInstanceId]), + Layer.mock(ProjectService.ProjectService)({}), + Layer.mock(SecretRequests.SecretRequests)({}), + Layer.mock(RemoteDelegation.RemoteDelegation)({}), + Layer.mock(ScheduledTaskService.ScheduledTaskService)({}), + ); + + yield* Effect.gen(function* () { + const service = yield* OrchestratorMcpService.OrchestratorMcpService; + const refuse = (model: string) => + service + .delegateTask(scope, { + task: "Summarize the diff.", + target: { providerInstanceId: codexInstanceId, model }, + mode: "async", + clientRequestId: `delegate-unadvertised-${model}`, + }) + .pipe(Effect.flip); + + const prefixed = yield* refuse("anthropic/claude-opus-5-3"); + assert.equal(prefixed.code, "model_unavailable"); + assert.equal( + prefixed.message, + "Model anthropic/claude-opus-5-3 is not advertised by provider codex. " + + "Did you mean claude-opus-5-3?", + ); + + const unmatched = yield* refuse("opus-5.5"); + assert.equal( + unmatched.message, + "Model opus-5.5 is not advertised by provider codex. Advertised models: " + + "claude-opus-5-1, claude-opus-5-2, claude-opus-5-3, claude-opus-5-4, claude-opus-5-5, " + + "claude-opus-5-6, claude-opus-5-7, claude-opus-5-8 and 2 more; " + + "orchestrator_capabilities with search finds them.", + ); + }).pipe(Effect.provide(layerOrchestratorService.pipe(Layer.provide(layerDependencies)))); + }), + ); + it.effect("driver-only delegation skips an instance the project turned off", () => Effect.gen(function* () { const altInstanceId = ProviderInstanceId.make("codex-alt"); @@ -2637,6 +2907,17 @@ describe("OrchestratorMcpService provider resolution", () => { }); const titled = yield* orchestrator.getThreadProjection(emojiAtLimit.threads[0]!.threadId); assert.equal(titled.thread.title, `${"a".repeat(76)}...`); + // One commit holds a prompted thread and its run, so no client sees it idle between. + const promptedCommits = yield* orchestrator + .streamStoredEventsFrom({ threadId: titled.thread.id }) + .pipe( + Stream.filter( + ({ event }) => event.type === "thread.created" || event.type === "run.created", + ), + Stream.take(2), + Stream.runCollect, + ); + assert.equal(promptedCommits[0]?.commandId, promptedCommits[1]?.commandId); yield* settings.updateSettings({ projectSettingsOverrides: { @@ -3133,8 +3414,17 @@ describe("OrchestratorMcpService provider resolution", () => { tasks: ReadonlyArray, boundThread: OrchestrationV2ThreadShell | null, upserted: Ref.Ref, + saved: ScheduledTask = tasks[0]!, ) => - layerOrchestratorService.pipe( + OrchestratorMcpService.layer.pipe( + Layer.provide(Layer.mock(ThreadLaunchService.ThreadLaunchService)({})), + Layer.provide( + Layer.mock(GitVcsDriver.GitVcsDriver)({ + resolvePrimaryRemoteName: () => Effect.succeed("origin"), + resolveDefaultBranchName: () => Effect.succeed("main"), + resolveCommit: () => Effect.succeed({ commitSha: "abc123" }), + }), + ), Layer.provide( Layer.mergeAll( NodeServices.layer, @@ -3148,109 +3438,89 @@ describe("OrchestratorMcpService provider resolution", () => { Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({ list: () => Effect.succeed([]), }), - Layer.mock(ProjectService.ProjectService)({}), + Layer.mock(ProjectService.ProjectService)({ + getById: (id) => + Effect.succeedSome({ + id, + workspaceRoot: "/repo/scheduled", + defaultModelSelection: saved.modelSelection, + } as Project), + }), Layer.mock(SecretRequests.SecretRequests)({}), Layer.mock(RemoteDelegation.RemoteDelegation)({}), Layer.mock(ScheduledTaskService.ScheduledTaskService)({ list: () => Effect.succeed({ tasks }), upsert: () => - Ref.update(upserted, (count) => count + 1).pipe(Effect.as({ task: tasks[0]! })), + Ref.update(upserted, (count) => count + 1).pipe(Effect.as({ task: saved })), }), ), ), ); - it.effect("hides a webhook URL from a caller below the task's modes", () => + it.effect("lists a webhook task without its URL to a caller that may run it", () => Effect.gen(function* () { const upserted = yield* Ref.make(0); const listed = yield* OrchestratorMcpService.OrchestratorMcpService.pipe( Effect.flatMap((mcp) => mcp.listScheduledTasks(supervisedClient, { projectId })), - Effect.provide( - service( - [ - task({ runtimeMode: "full-access" }), - task({ id: ScheduledTaskId.make("scheduled-task:supervised") }), - ], - null, - upserted, - ), - ), + Effect.provide(service([task({})], null, upserted)), ); assert.deepEqual( - listed.tasks.map((summary) => summary.webhookUrl), - [undefined, "https://t3.example/hooks/secret"], + listed.tasks.map((summary) => [summary.webhookUrl, summary.webhookSignature]), + [[undefined, "none"]], ); }), ); - it.effect("hides a webhook URL from a thread whose turn has ended", () => + it.effect("lists only the named task within the requested project", () => Effect.gen(function* () { - const callerId = ThreadId.make("thread:scheduled-ended"); - const shell = liveThreadShell(callerId, { activeRunId: null }); - const listed = yield* OrchestratorMcpService.OrchestratorMcpService.pipe( - Effect.flatMap((mcp) => - mcp.listScheduledTasks( - { - ...supervisedClient, - requestNamespace: "provider:scheduled-ended", - thread: { - threadId: callerId, - providerSessionId: "provider:scheduled-ended", - providerInstanceId: shell.providerInstanceId, - }, - client: undefined, - }, - { projectId }, - ), - ), - Effect.provide( - layerOrchestratorService.pipe( - Layer.provide( - Layer.mergeAll( - NodeServices.layer, - NodeCrypto.layer, - Layer.mock(ThreadManagementService.ThreadManagementService)({ - getThreadShell: () => Effect.succeed(null), - getThreadRecords: () => Effect.succeed(idleThreadProjection(shell)), - }), - ServerSettings.layerTest(), - Layer.mock(ProviderRegistry.ProviderRegistry)({ - getProviders: Effect.succeed([]), - }), - Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({ - list: () => Effect.succeed([]), - }), - Layer.mock(ProjectService.ProjectService)({}), - Layer.mock(SecretRequests.SecretRequests)({}), - Layer.mock(RemoteDelegation.RemoteDelegation)({}), - Layer.mock(ScheduledTaskService.ScheduledTaskService)({ - list: () => Effect.succeed({ tasks: [task({})] }), - }), - ), - ), - ), - ), + const upserted = yield* Ref.make(0); + const first = task({}); + const second = task({ id: ScheduledTaskId.make("scheduled-task:second") }); + const elsewhere = task({ + id: ScheduledTaskId.make("scheduled-task:elsewhere"), + projectId: ProjectId.make("project:elsewhere"), + }); + const mcp = yield* OrchestratorMcpService.OrchestratorMcpService.pipe( + Effect.provide(service([first, second, elsewhere], null, upserted)), ); - assert.equal(listed.tasks[0]?.webhookUrl, undefined); + const ids = (input: OrchestratorMcpListScheduledTasksInput) => + mcp + .listScheduledTasks(supervisedClient, input) + .pipe(Effect.map(({ tasks }) => tasks.map((summary) => summary.scheduledTaskId))); + assert.deepEqual(yield* ids({ scheduledTaskId: second.id }), [second.id]); + assert.deepEqual(yield* ids({ projectId, scheduledTaskId: elsewhere.id }), []); }), ); - it.effect("never shows a read-only client a webhook URL", () => + it.effect("returns a webhook URL when creating a task, not when updating it", () => Effect.gen(function* () { const upserted = yield* Ref.make(0); - const listed = yield* OrchestratorMcpService.OrchestratorMcpService.pipe( + const hook = task({}); + const timer = task({ + schedule: { type: "interval", everyMs: 3_600_000 }, + webhook: undefined, + }); + const created = yield* OrchestratorMcpService.OrchestratorMcpService.pipe( Effect.flatMap((mcp) => - mcp.listScheduledTasks( - { - ...supervisedClient, - client: { sessionId: "scheduled", label: "Claude Code", access: "read-only" }, - }, - { projectId }, - ), + mcp.scheduleTask(supervisedClient, { + projectId, + prompt: hook.prompt, + schedule: { type: "webhook" }, + }), ), - Effect.provide(service([task({})], null, upserted)), + Effect.provide(service([hook], null, upserted)), ); - assert.equal(listed.tasks[0]?.webhookUrl, undefined); + assert.equal(created.webhookUrl, "https://t3.example/hooks/secret"); + const converted = yield* OrchestratorMcpService.OrchestratorMcpService.pipe( + Effect.flatMap((mcp) => + mcp.updateScheduledTask(supervisedClient, { + scheduledTaskId: timer.id, + schedule: { type: "webhook" }, + }), + ), + Effect.provide(service([timer], null, upserted, hook)), + ); + assert.equal(converted.webhookUrl, undefined); }), ); @@ -3375,7 +3645,7 @@ describe("OrchestratorMcpService provider resolution", () => { git: Partial, saved: Ref.Ref>, task: ScheduledTask = existing, - modes?: Ref.Ref>, + inputs?: Ref.Ref>, ) => OrchestratorMcpService.layer.pipe( Layer.provide(Layer.mock(ThreadLaunchService.ThreadLaunchService)({})), @@ -3407,9 +3677,9 @@ describe("OrchestratorMcpService provider resolution", () => { upsert: (input) => Ref.update(saved, (strategies) => [...strategies, input.workspaceStrategy]).pipe( Effect.andThen( - modes === undefined + inputs === undefined ? Effect.void - : Ref.update(modes, (all) => [...all, input.threadMode]), + : Ref.update(inputs, (all) => [...all, input]), ), Effect.as({ task }), ), @@ -3438,16 +3708,48 @@ describe("OrchestratorMcpService provider resolution", () => { }), ); + it.effect.each([ + { + name: "drops an emoji the limit splits", + prompt: `${"a".repeat(79)}😀 rest`, + title: "a".repeat(79), + }, + { + name: "keeps an emoji that ends at the limit", + prompt: `${"a".repeat(78)}😀 rest`, + title: `${"a".repeat(78)}😀`, + }, + ])("a prompt-derived title $name", ({ prompt, title }) => + Effect.gen(function* () { + const inputs = yield* Ref.make>([]); + const mcp = yield* OrchestratorMcpService.OrchestratorMcpService.pipe( + Effect.provide( + service( + masterRepository, + yield* Ref.make>([]), + existing, + inputs, + ), + ), + ); + yield* mcp.scheduleTask(client, { projectId, prompt, schedule }); + assert.deepEqual( + (yield* Ref.get(inputs)).map((input) => input.title), + [title], + ); + }), + ); + it.effect("keeps one thread when fresh sessions are turned off before the first run", () => Effect.gen(function* () { - const modes = yield* Ref.make>([]); + const inputs = yield* Ref.make>([]); const mcp = yield* OrchestratorMcpService.OrchestratorMcpService.pipe( Effect.provide( service( masterRepository, yield* Ref.make>([]), { ...existing, threadMode: "fresh_session" }, - modes, + inputs, ), ), ); @@ -3459,7 +3761,10 @@ describe("OrchestratorMcpService provider resolution", () => { scheduledTaskId: existing.id, bindToCurrentThread: false, }); - assert.deepEqual(yield* Ref.get(modes), ["continue", "new_thread"]); + assert.deepEqual( + (yield* Ref.get(inputs)).map((input) => input.threadMode), + ["continue", "new_thread"], + ); }), ); @@ -3467,7 +3772,7 @@ describe("OrchestratorMcpService provider resolution", () => { "refuses unbinding into fresh sessions in one call, which would keep the thread", () => Effect.gen(function* () { - const modes = yield* Ref.make>([]); + const inputs = yield* Ref.make>([]); const mcp = yield* OrchestratorMcpService.OrchestratorMcpService.pipe( Effect.provide( service( @@ -3478,7 +3783,7 @@ describe("OrchestratorMcpService provider resolution", () => { threadId: ThreadId.make("thread:scheduled-base"), threadMode: "continue", }, - modes, + inputs, ), ), ); @@ -3490,7 +3795,7 @@ describe("OrchestratorMcpService provider resolution", () => { }) .pipe(Effect.flip); assert.include(refused.message, "first unbind it"); - assert.deepEqual(yield* Ref.get(modes), []); + assert.deepEqual(yield* Ref.get(inputs), []); }), ); @@ -4108,6 +4413,7 @@ describe("OrchestratorMcpService thread lookup", () => { client: { sessionId: "thread-lookup", label: "Claude Code", access: "full-access" }, capabilities: new Set(["orchestration"]), issuedAt: 1, + linkedEnvironments: false, }; const service = ( shells: ReadonlyArray, @@ -4183,6 +4489,36 @@ describe("OrchestratorMcpService thread lookup", () => { }), ); + it.effect("tells an agent steering a finished thread to start a new turn instead", () => + Effect.gen(function* () { + const sent = yield* Ref.make(0); + return yield* Effect.gen(function* () { + const mcp = yield* OrchestratorMcpService.OrchestratorMcpService; + for (const mode of ["steer", "restart"] as const) { + const error = yield* mcp + .sendToThread(client, { threadId: launched.id, message: "hello", mode }) + .pipe(Effect.flip); + assert.equal(error.code, "thread_not_sendable"); + assert.include(error.message, 'mode: "auto"'); + assert.include(error.message, '"queue"'); + } + }).pipe( + Effect.provide( + service([launched], sent, { + getProjectThreadRecords: () => Effect.succeed(idleThreadProjection(launched)), + sendToThread: (input) => + Effect.fail( + new ThreadManagementService.ThreadManagementNoSteerableRunError({ + threadId: input.threadId, + mode: input.mode === "restart" ? "restart" : "steer", + }), + ), + }), + ), + ); + }), + ); + it.effect("records the linked environment's thread that sent a message", () => Effect.gen(function* () { const target = liveThreadShell(ThreadId.make("thread:target")); @@ -4296,11 +4632,18 @@ describe("OrchestratorMcpService thread list", () => { capabilities: new Set(["orchestration"]), issuedAt: 1, }; - const shell = (id: string, projectId: string, snoozed: boolean, waiting = false) => ({ + const shell = ( + id: string, + projectId: string, + snoozed: boolean, + waiting = false, + pinned = false, + ) => ({ ...liveThreadShell(ThreadId.make(id)), projectId: ProjectId.make(projectId), status: "completed" as const, snoozedUntil: snoozed ? DateTime.makeUnsafe("2099-01-01T00:00:00Z") : null, + pinnedAt: pinned ? DateTime.makeUnsafe("2026-01-01T00:00:00Z") : null, blockedOnThreadWait: waiting, pendingBackgroundTasks: waiting ? [ @@ -4319,7 +4662,7 @@ describe("OrchestratorMcpService thread list", () => { const shells = [ shell("thread:a-snoozed", "project:a", true), shell("thread:a-awake", "project:a", false), - shell("thread:b-snoozed", "project:b", true, true), + shell("thread:b-snoozed", "project:b", true, true, true), { ...shell("thread:a-awaiting-command", "project:a", false), activityRunStatus: null, @@ -4392,6 +4735,10 @@ describe("OrchestratorMcpService thread list", () => { ["thread:b-snoozed", "project:b"], ], ); + assert.deepStrictEqual( + listed.threads.map((thread) => thread.pinned), + [false, true], + ); assert.deepStrictEqual( listed.threads.map((thread) => [thread.blockedOnThreadWait, thread.pendingBackgroundTasks]), [ diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.ts index d7ef5f788f0ba..cf5efb0ab333d 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.ts @@ -108,6 +108,7 @@ import { runOutcome, } from "../orchestration-v2/RestartBackgroundNote.ts"; import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts"; +import { placeholderThreadTitle } from "../orchestration-v2/placeholderThreadTitle.ts"; import * as ThreadLaunchService from "../orchestration-v2/ThreadLaunchService.ts"; import * as GitVcsDriver from "../vcs/GitVcsDriver.ts"; import { isSnoozed } from "../orchestration-v2/ThreadSettlementService.ts"; @@ -131,15 +132,16 @@ import { runtimeModeRank, taskPrompt, } from "./delegatedTaskModes.ts"; -import { isLiveCallerShell, threadNotFound } from "./threadAccess.ts"; +import { assertTargetWithinLimits, isLiveCallerShell, threadNotFound } from "./threadAccess.ts"; import * as SecretRequests from "../secrets/SecretRequests.ts"; import * as PeerForwarding from "../peer/PeerForwarding.ts"; import * as RemoteDelegation from "../peer/RemoteDelegation.ts"; -const DEFAULT_WAIT_TIMEOUT_MS = 10 * 60 * 1_000; -// Nothing is written while a task wait blocks, and Node fetch drops a silent call at 300 s. -const DEFAULT_TASK_WAIT_TIMEOUT_MS = 4 * 60 * 1_000; -const MAX_WAIT_TIMEOUT_MS = 60 * 60 * 1_000; +// Return recoverable handles before common MCP client timeouts expire. +const DEFAULT_WAIT_TIMEOUT_MS = 30_000; +const MAX_WAIT_TIMEOUT_MS = 45_000; +const DEFAULT_SECRET_REQUEST_TIMEOUT_MS = 10 * 60 * 1_000; +const MAX_SECRET_REQUEST_TIMEOUT_MS = 60 * 60 * 1_000; const TASK_WAKE_EVENTS = [ { thread: "parent", eventType: "subagent.updated" }, { thread: "child", eventType: "run.updated" }, @@ -250,9 +252,13 @@ function threadManagementFailure(error: unknown): OrchestratorMcpFailure { case "ThreadManagementRunNotFoundError": return failure("run_not_found", error.message); case "ThreadManagementThreadArchivedError": - case "ThreadManagementNoSteerableRunError": case "ThreadManagementWorktreeRestoreError": return failure("thread_not_sendable", error.message); + case "ThreadManagementNoSteerableRunError": + return failure( + "thread_not_sendable", + `${error.message} Send with mode: "auto" or "queue" to start a new turn there.`, + ); case "ThreadManagementThreadNotInterruptibleError": return failure("thread_not_interruptible", error.message); case "ThreadManagementProjectionLoadError": @@ -352,6 +358,26 @@ function listCapabilityModels(input: { return { listed, omitted: visible.length - listed.length }; } +/** Names advertised slugs to retry with: same last `/` segment first, else a short list. */ +function advertisedModelsHint(models: ReadonlyArray, requested: string) { + const lastSegment = (slug: string) => slug.slice(slug.lastIndexOf("/") + 1).toLowerCase(); + const wanted = lastSegment(requested); + const close = models.filter((model) => lastSegment(model.slug) === wanted); + if (close.length > 0) { + return `Did you mean ${close + .slice(0, LISTED_MODEL_LIMIT) + .map((model) => model.slug) + .join(", ")}?`; + } + const listed = models + .toSorted((a, b) => Number(a.isLegacy ?? false) - Number(b.isLegacy ?? false)) + .slice(0, LISTED_MODEL_LIMIT); + const omitted = models.length - listed.length; + return `Advertised models: ${listed.map((model) => model.slug).join(", ")}${ + omitted > 0 ? ` and ${omitted} more; orchestrator_capabilities with search finds them` : "" + }.`; +} + /** Option ids and allowed values are all a delegating agent needs; labels are UI copy. */ function capabilityModelOption(descriptor: ProviderOptionDescriptor): OrchestratorMcpModelOption { const defaultValue = getProviderOptionCurrentValue(descriptor); @@ -375,7 +401,7 @@ export const requireAdvertisedModel = (provider: ServerProvider, model: string) ? Effect.fail( failure( "model_unavailable", - `Model ${model} is not advertised by provider ${provider.instanceId}.`, + `Model ${model} is not advertised by provider ${provider.instanceId}. ${advertisedModelsHint(provider.models, model)}`, ), ) : Effect.void; @@ -675,17 +701,38 @@ function pageIncludesTerminalTaskResult(input: { function latestTerminalResultRun( projection: Pick, delegatedRun: OrchestrationV2Run | undefined, + awaitingContinuationRun: OrchestrationV2Run | undefined, ): OrchestrationV2Run | undefined { const monitorRunIds = new Set( projection.messages .filter((message) => message.notification?.source.kind === "monitor") .map((message) => message.runId), ); + // A restart-cut run, and each cut run before it in the chain, has no outcome while a + // continuation is pending, live, or ran to a result. + const runsById = new Map(projection.runs.map((run) => [run.id, run])); + const supersededRunIds = new Set(); + const supersede = (runId: RunId | undefined) => { + let id = runId; + while (id !== undefined && !supersededRunIds.has(id)) { + supersededRunIds.add(id); + id = runsById.get(id)?.restartContinuationOfRunId; + } + }; + supersede(awaitingContinuationRun?.id); + for (const run of projection.runs) { + if ( + !ThreadManagementService.isTerminalRunStatus(run.status) || + (run.status !== "rolled_back" && run.startedAt !== null) + ) + supersede(run.restartContinuationOfRunId); + } return projection.runs .filter( (run) => ThreadManagementService.isTerminalRunStatus(run.status) && !monitorRunIds.has(run.id) && + !supersededRunIds.has(run.id) && run.status !== "rolled_back" && (run.id === delegatedRun?.id || run.startedAt !== null), ) @@ -778,7 +825,7 @@ function threadTitle(input: { readonly index: number; }): string { const detail = input.title?.trim() || input.prompt?.trim(); - if (!detail) return `${input.parentTitle} thread ${input.index + 1}`; + if (!detail) return placeholderThreadTitle(input.parentTitle, input.index); if (detail.length <= 80) return detail; let end = 77; const previous = detail.charCodeAt(end - 1); @@ -901,9 +948,11 @@ function listItemFromShell( ...threadPullRequests(shell.pullRequests), ...threadSettlement(shell), ...threadSnooze(shell, nowMs), + pinned: shell.pinnedAt != null, ...threadBackgroundWork(shell), parentThreadId: shell.lineage.parentThreadId, relationshipToParent: shell.lineage.relationshipToParent, + ...(shell.delegatedFrom == null ? {} : { delegatedFrom: shell.delegatedFrom }), itemCount: shell.visibleItemCount, createdAt: DateTime.formatIso(shell.createdAt), updatedAt: DateTime.formatIso(shell.updatedAt), @@ -955,6 +1004,9 @@ function threadDetail( worktreePath: projection.thread.worktreePath, parentThreadId: projection.thread.lineage.parentThreadId, relationshipToParent: projection.thread.lineage.relationshipToParent, + ...(projection.thread.delegatedFrom == null + ? {} + : { delegatedFrom: projection.thread.delegatedFrom }), runCount: projection.runs.length, itemCount, pendingRequestCount: projection.runtimeRequests.filter( @@ -963,6 +1015,7 @@ function threadDetail( archived: projection.thread.archivedAt !== null, ...threadSettlement(projection.thread), ...threadSnooze(shell, nowMs), + pinned: shell.pinnedAt != null, ...threadBackgroundWork(shell), createdAt: DateTime.formatIso(projection.thread.createdAt), updatedAt: DateTime.formatIso(projection.thread.updatedAt), @@ -1315,13 +1368,19 @@ const make = Effect.gen(function* () { : Effect.fail(failure("target_required", missingMessage)); const loadTargetThread = Effect.fn("OrchestratorMcpService.loadTargetThread")(function* ( + scope: McpInvocationScope, threadId: ThreadId, ) { const shell = yield* threadManagement .getThreadShell(threadId) .pipe(Effect.mapError(threadManagementFailure)); if (shell === null || shell.deletedAt !== null) { - return yield* threadNotFound(threadManagement, threadId, `Thread ${threadId} was not found.`); + return yield* threadNotFound( + threadManagement, + scope, + threadId, + `Thread ${threadId} was not found.`, + ); } // Keeps legacy import and project validation; callers read only the thread row. return yield* threadManagement @@ -1337,7 +1396,7 @@ const make = Effect.gen(function* () { const target: Pick = caller.parent !== undefined && threadId === caller.parent.thread.id ? caller.parent - : yield* loadTargetThread(threadId); + : yield* loadTargetThread(scope, threadId); return { ...caller, target } as const; }); @@ -1398,6 +1457,7 @@ const make = Effect.gen(function* () { if (shell === null || shell.deletedAt !== null) { return yield* threadNotFound( threadManagement, + scope, threadId, `Thread ${threadId} is no longer available.`, ); @@ -1637,7 +1697,6 @@ const make = Effect.gen(function* () { ) .pipe(Effect.mapError(threadManagementFailure)); const childRun = delegatedTaskRun(childControls, task); - const terminalRun = latestTerminalResultRun(childControls, childRun); const progress = delegatedTaskProgress(childControls); const followupRuns = childRun?.delegatedTaskId === task.id ? delegatedTaskRuns(childControls, task.id) : undefined; @@ -1651,6 +1710,17 @@ const make = Effect.gen(function* () { : delegatedTaskAwaitsWork(followupRuns, childControls) ? "waiting_for_children" : "result_available"; + const heldForRestart = + task.result === null && + taskWorkState === "result_available" && + (yield* threadManagement + .delegatedTaskResultPending(task.childThreadId, childRun?.delegatedTaskId) + .pipe(Effect.mapError(threadManagementFailure))); + const terminalRun = latestTerminalResultRun( + childControls, + childRun, + heldForRestart ? taskResultRun : undefined, + ); const resultRunIds = [ ...new Set( [taskResultRun?.id, terminalRun?.id].filter((id): id is RunId => id !== undefined), @@ -1669,12 +1739,6 @@ const make = Effect.gen(function* () { messages: [...childControls.messages, ...resultRecords.messages], turnItems: resultRecords.turnItems, }; - const heldForRestart = - task.result === null && - taskWorkState === "result_available" && - (yield* threadManagement - .delegatedTaskResultPending(task.childThreadId, childRun?.delegatedTaskId) - .pipe(Effect.mapError(threadManagementFailure))); const workState = task.result !== null ? "result_available" : heldForRestart ? "working" : taskWorkState; const runStatus = taskStatusForRun(taskResultRun ?? childRun); @@ -1828,7 +1892,7 @@ const make = Effect.gen(function* () { ): Effect.fn.Return { const timeoutMs = Math.min( MAX_WAIT_TIMEOUT_MS, - Math.max(1, requestedTimeoutMs ?? DEFAULT_TASK_WAIT_TIMEOUT_MS), + Math.max(1, requestedTimeoutMs ?? DEFAULT_WAIT_TIMEOUT_MS), ); const upgradeWake = threadManagement .dispatch({ @@ -2082,9 +2146,8 @@ const make = Effect.gen(function* () { interactionModeRank(modes.interactionMode) <= interactionModeRank(limits.interactionMode); /** - * Its webhook URL starts runs, so only a caller that may start one sees it: never a client - * approved for read-only access, and a thread caller only with a live turn, at modes covering - * every mode the task runs at. + * Creation returns a webhook URL only to callers allowed to start its runs: never a read-only + * client, and a thread caller only with a live turn at modes covering every mode the task runs at. */ const summarizeScheduledTask = Effect.fn("OrchestratorMcpService.summarizeScheduledTask")( function* ( @@ -2161,9 +2224,11 @@ const make = Effect.gen(function* () { } const modelSelection = parent?.thread.modelSelection ?? (yield* projectDefaultModelSelection(project)); - const derivedTitle = input.prompt.split("\n")[0]?.trim() ?? ""; - const title = - input.title ?? (derivedTitle.length > 0 ? derivedTitle.slice(0, 80) : "Scheduled task"); + // Never end on a lone high surrogate, which renders as a broken character. + const derivedTitle = (input.prompt.split("\n")[0]?.trim() ?? "") + .slice(0, 80) + .replace(/[\uD800-\uDBFF]$/, ""); + const title = input.title ?? (derivedTitle.length > 0 ? derivedTitle : "Scheduled task"); const upsertInput: ScheduledTaskUpsertInput = { title, prompt: input.prompt, @@ -2205,7 +2270,7 @@ const make = Effect.gen(function* () { }), listScheduledTasks: Effect.fn("OrchestratorMcpService.listScheduledTasks")( function* (scope, input) { - const { parent, limits } = yield* loadCaller(scope, ["runs"]); + const { parent } = yield* loadCaller(scope, []); const projectId = input.projectId ?? parent?.thread.projectId; const { tasks } = yield* scheduledTasks .list() @@ -2215,10 +2280,13 @@ const make = Effect.gen(function* () { ), ); return { - tasks: yield* Effect.forEach( - tasks.filter((task) => projectId === undefined || task.projectId === projectId), - (task) => summarizeScheduledTask(scope, { parent, limits }, task), - ), + tasks: tasks + .filter( + (task) => + (projectId === undefined || task.projectId === projectId) && + (input.scheduledTaskId === undefined || task.id === input.scheduledTaskId), + ) + .map((task) => scheduledTaskSummary(task, false)), }; }, ), @@ -2295,7 +2363,7 @@ const make = Effect.gen(function* () { failure("orchestration_error", `Could not update scheduled task: ${error.message}`), ), ); - return yield* summarizeScheduledTask(scope, { parent, limits }, task); + return scheduledTaskSummary(task, false); }, ), deleteScheduledTask: Effect.fn("OrchestratorMcpService.deleteScheduledTask")( @@ -2423,7 +2491,10 @@ const make = Effect.gen(function* () { }).pipe( Effect.timeoutOption( Duration.millis( - Math.min(input.timeoutMs ?? DEFAULT_WAIT_TIMEOUT_MS, MAX_WAIT_TIMEOUT_MS), + Math.min( + input.timeoutMs ?? DEFAULT_SECRET_REQUEST_TIMEOUT_MS, + MAX_SECRET_REQUEST_TIMEOUT_MS, + ), ), ), Effect.onExit((exit) => (Exit.isSuccess(exit) ? Effect.void : closeCard)), @@ -2992,7 +3063,20 @@ const make = Effect.gen(function* () { title: request.title, index, }); - yield* threadManagement + const initialMessage = + request.prompt === undefined + ? undefined + : { + senderThreadId: scope.thread.threadId, + messageId: stableMessageId({ + scope, + requestKey: key, + index, + }), + text: request.prompt, + attachments: [], + }; + const created = yield* threadManagement .dispatch({ type: "thread.create", createdBy: "agent", @@ -3009,6 +3093,7 @@ const make = Effect.gen(function* () { ...(parent.thread.linkOrigin === undefined ? {} : { linkOrigin: parent.thread.linkOrigin }), + ...(initialMessage === undefined ? {} : { initialMessage }), }) .pipe( Effect.mapError((error) => @@ -3022,9 +3107,14 @@ const make = Effect.gen(function* () { const resolvedModelSelection = (yield* threadManagement .getThreadRecords(threadId, []) .pipe(Effect.mapError(threadManagementFailure))).thread.modelSelection; - if (request.prompt !== undefined) { + // A replayed creation that carried no prompt has no run yet. + if ( + initialMessage !== undefined && + !created.storedEvents.some(({ event }) => event.type === "run.created") + ) { yield* threadManagement .dispatch({ + ...initialMessage, type: "message.dispatch", createdBy: "agent", creationSource: "mcp", @@ -3035,14 +3125,6 @@ const make = Effect.gen(function* () { index, }), threadId, - senderThreadId: scope.thread.threadId, - messageId: stableMessageId({ - scope, - requestKey: key, - index, - }), - text: request.prompt, - attachments: [], modelSelection: resolvedModelSelection, dispatchMode: { type: "start_immediately" }, }) @@ -3307,8 +3389,7 @@ const make = Effect.gen(function* () { sendToThread: Effect.fn("OrchestratorMcpService.sendToThread")(function* (scope, input) { const { parent, limits, target } = yield* loadScopedThread(scope, input.threadId, ["runs"]); yield* assertLiveCallerForOtherThread(scope, parent, target); - yield* resolveRuntimeMode(limits.runtimeMode, target.thread.runtimeMode); - yield* resolveInteractionMode(limits.interactionMode, target.thread.interactionMode); + yield* assertTargetWithinLimits(limits, target.thread); const mode = input.mode ?? "auto"; const key = yield* requestKey(input.clientRequestId); @@ -3412,8 +3493,7 @@ const make = Effect.gen(function* () { interruptThread: Effect.fn("OrchestratorMcpService.interruptThread")(function* (scope, input) { const { parent, limits, target } = yield* loadScopedThread(scope, input.threadId, ["runs"]); yield* assertLiveCallerForOtherThread(scope, parent, target); - yield* resolveRuntimeMode(limits.runtimeMode, target.thread.runtimeMode); - yield* resolveInteractionMode(limits.interactionMode, target.thread.interactionMode); + yield* assertTargetWithinLimits(limits, target.thread); const key = yield* requestKey(input.clientRequestId); // A peer from before `stop` drops it when decoding, so it interrupts // the run instead, as this call did then. diff --git a/apps/server/src/mcp/ThreadMetadataMcpService.ts b/apps/server/src/mcp/ThreadMetadataMcpService.ts index 44f0a610e585a..355fba952653f 100644 --- a/apps/server/src/mcp/ThreadMetadataMcpService.ts +++ b/apps/server/src/mcp/ThreadMetadataMcpService.ts @@ -142,7 +142,12 @@ const make = Effect.gen(function* () { ), ); if (shell === null || shell.deletedAt !== null) { - return yield* threadNotFound(threadManagement, threadId, `Thread ${threadId} was not found.`); + return yield* threadNotFound( + threadManagement, + scope, + threadId, + `Thread ${threadId} was not found.`, + ); } const requestKey = input.clientRequestId === undefined ? yield* random.uuidV4() : input.clientRequestId; diff --git a/apps/server/src/mcp/advertisedJsonSchema.test.ts b/apps/server/src/mcp/advertisedJsonSchema.test.ts index 09a90556a9dd0..6ab74f96db697 100644 --- a/apps/server/src/mcp/advertisedJsonSchema.test.ts +++ b/apps/server/src/mcp/advertisedJsonSchema.test.ts @@ -3,6 +3,7 @@ import * as Schema from "effect/Schema"; import { Tool } from "effect/ai"; import { advertisedJsonSchema } from "./advertisedJsonSchema.ts"; +import { ThemeToolkit } from "./toolkits/theme/tools.ts"; it("drops JSON noise, merges tagged unions, and keeps null where it is required", () => { const Every = Schema.Struct({ ms: Schema.Int }); @@ -43,3 +44,18 @@ it("drops JSON noise, merges tagged unions, and keeps null where it is required" required: ["beforeRunId", "every"], }); }); + +it("advertises only string enum values, which Gemini requires", () => { + const enumValues: Array = []; + const collect = (node: unknown): void => { + if (Array.isArray(node)) return node.forEach(collect); + if (typeof node !== "object" || node === null) return; + for (const [key, value] of Object.entries(node)) { + if (key === "enum" && Array.isArray(value)) enumValues.push(...value); + else collect(value); + } + }; + collect(advertisedJsonSchema(ThemeToolkit.tools.t3_theme_set)); + + expect(enumValues.filter((value) => typeof value !== "string")).toEqual([]); +}); diff --git a/apps/server/src/mcp/advertisedJsonSchema.ts b/apps/server/src/mcp/advertisedJsonSchema.ts index 10001307f2d6c..8b8737a529183 100644 --- a/apps/server/src/mcp/advertisedJsonSchema.ts +++ b/apps/server/src/mcp/advertisedJsonSchema.ts @@ -21,6 +21,10 @@ const SAFE_INTEGER_BOUNDS: Record = { minimum: Number.MIN_SAFE_INTEGER, }; +// Gemini rejects function declarations whose enums hold anything but strings. +const isNonStringEnum = (key: string, value: Json) => + key === "enum" && Array.isArray(value) && value.some((item) => typeof item !== "string"); + const tagOf = (branch: JsonObject, key: string): Json => { const property = Predicate.isObject(branch.properties) ? branch.properties[key] : undefined; return Predicate.isObject(property) && Array.isArray(property.enum) && property.enum.length === 1 @@ -75,6 +79,7 @@ const compact = (schema: Json, optional: boolean): Json => { for (const [key, value] of Object.entries(schema)) { if (key === "additionalProperties" && value === true) continue; if (SAFE_INTEGER_BOUNDS[key] === value) continue; + if (isNonStringEnum(key, value)) continue; result[key] = key === "properties" && Predicate.isObject(value) ? Object.fromEntries( diff --git a/apps/server/src/mcp/linkOrigin.ts b/apps/server/src/mcp/linkOrigin.ts index 7373fd51d2bd4..742ac2cea3624 100644 --- a/apps/server/src/mcp/linkOrigin.ts +++ b/apps/server/src/mcp/linkOrigin.ts @@ -29,7 +29,7 @@ export const callerLinkOrigin = ( : undefined); /** The link that fences a caller, if any: one paired below full access. */ -const fencingLinkOrigin = (caller: Pick) => { +export const fencingLinkOrigin = (caller: Pick) => { const origin = callerLinkOrigin(caller); return origin !== undefined && linkOriginIsFenced(origin) ? origin : undefined; }; diff --git a/apps/server/src/mcp/threadAccess.test.ts b/apps/server/src/mcp/threadAccess.test.ts index aa107711f334e..914b42426540b 100644 --- a/apps/server/src/mcp/threadAccess.test.ts +++ b/apps/server/src/mcp/threadAccess.test.ts @@ -1,40 +1,140 @@ import { expect, it } from "@effect/vitest"; -import { EnvironmentId, ThreadId } from "@t3tools/contracts"; +import { + EnvironmentId, + type OrchestrationV2ThreadShell, + ProviderInstanceId, + ThreadId, +} from "@t3tools/contracts"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as ThreadManagement from "../orchestration-v2/ThreadManagementService.ts"; import * as McpInvocationContext from "./McpInvocationContext.ts"; import { liveThreadShell } from "./McpToolAccess.testkit.ts"; -import { readThread } from "./threadAccess.ts"; +import { assertTargetWithinLimits, readThread } from "./threadAccess.ts"; const bareId = ThreadId.make("5d0b4a3e-8c1f-4a52-9d37-2f6c7e1b9a04"); +const claudeCode: McpInvocationContext.McpClientCaller = { + sessionId: "thread-access", + label: "Claude Code", + access: "full-access", +}; const client: McpInvocationContext.McpInvocationScope = { environmentId: EnvironmentId.make("environment:thread-access"), requestNamespace: "client:thread-access", thread: undefined, - client: { sessionId: "thread-access", label: "Claude Code", access: "full-access" }, + client: claudeCode, capabilities: new Set(["orchestration"]), issuedAt: 1, }; -const readBare = (existing: ReadonlyArray) => - readThread(bareId).pipe( +const linksHint = + "Thread ids belong to one environment: for a thread on a linked one, find it with `t3_environment_links` and pass its `environmentId` to a tool that takes one."; + +const readBare = ( + existing: ReadonlyArray, + scope: McpInvocationContext.McpInvocationScope = client, + threadId: ThreadId = bareId, +) => { + const shells = existing.map((shell) => + typeof shell === "string" ? liveThreadShell(shell) : shell, + ); + return readThread(threadId).pipe( Effect.flip, - Effect.provideService(McpInvocationContext.McpInvocationContext, client), + Effect.provideService(McpInvocationContext.McpInvocationContext, scope), Effect.provide( Layer.mock(ThreadManagement.ThreadManagementService)({ getThreadShell: (threadId) => - Effect.succeed(existing.includes(threadId) ? liveThreadShell(threadId) : null), + Effect.succeed(shells.find((shell) => shell.id === threadId) ?? null), }), ), ); +}; it.effect("points a bare uuid at the mcp: thread it belongs to", () => Effect.gen(function* () { const error = yield* readBare([ThreadId.make(`mcp:${bareId}`)]); expect(error.code).toBe("thread_not_found"); - expect(error.message).toBe(`The thread was not found. Did you mean \`mcp:${bareId}\`?`); + expect(error.message).toBe( + `The thread was not found. Did you mean \`mcp:${bareId}\`? ${linksHint}`, + ); + }), +); + +it.effect("tells a caller with linked environments that the thread may be on one of them", () => + Effect.gen(function* () { + const error = yield* readBare([], { ...client, linkedEnvironments: true }); + + expect(error.code).toBe("thread_not_found"); + expect(error.message).toContain("t3_environment_links"); + expect(error.message).toContain("environmentId"); + + const linksOff = yield* readBare([], { ...client, linkedEnvironments: false }); + expect(linksOff.message).toBe("The thread was not found."); + const peerLink = yield* readBare([], { + ...client, + linkedEnvironments: true, + client: { ...claudeCode, linked: true }, + }); + expect(peerLink.message).toBe("The thread was not found."); + + // A thread a fenced link started may not use this environment's links. + const fencedThread = ThreadId.make("thread:started-by-fenced-link"); + const fenced = yield* readBare( + [ + { + ...liveThreadShell(fencedThread), + linkOrigin: { sessionId: "peer-session", label: "Laptop", access: "auto" }, + }, + ], + { + ...client, + linkedEnvironments: true, + client: undefined, + thread: { + threadId: fencedThread, + providerSessionId: "provider-session", + providerInstanceId: ProviderInstanceId.make("codex"), + }, + }, + ); + expect(fenced.message).toBe("The thread was not found."); + }), +); + +it.effect("points an unencoded delegated-task id at the encoded thread it belongs to", () => + Effect.gen(function* () { + const commandId = `command:mcp:${bareId}:delegate-task:0e7d2c41-6b9a-4f3e-8d15-a2c4b6e8f019`; + const encoded = ThreadId.make(`thread:delegated-task:${encodeURIComponent(commandId)}`); + const error = yield* readBare( + [encoded], + client, + ThreadId.make(`thread:delegated-task:${commandId}`), + ); + + expect(error.code).toBe("thread_not_found"); + expect(error.message).toContain(`\`${encoded}\``); + }), +); + +it.effect("identifies the target and effective caller ceiling when a thread write is refused", () => + Effect.gen(function* () { + for (const [runtimeMode, interactionMode, code] of [ + ["approval-required", "default", "runtime_mode_escalation_denied"], + ["full-access", "plan", "interaction_mode_escalation_denied"], + ["approval-required", "plan", "runtime_mode_escalation_denied"], + ] as const) { + const target = liveThreadShell(bareId, { runtimeMode: "full-access" }); + const error = yield* assertTargetWithinLimits({ runtimeMode, interactionMode }, target).pipe( + Effect.flip, + ); + + expect(error.code).toBe(code); + expect(error.message).toContain(bareId); + expect(error.message).toContain("full-access/default"); + expect(error.message).toContain(`${runtimeMode}/${interactionMode}`); + expect(error.message).not.toMatch(/child|parent mode/i); + } }), ); diff --git a/apps/server/src/mcp/threadAccess.ts b/apps/server/src/mcp/threadAccess.ts index 416592a57fe79..8cce38949cdd0 100644 --- a/apps/server/src/mcp/threadAccess.ts +++ b/apps/server/src/mcp/threadAccess.ts @@ -18,26 +18,53 @@ import * as Effect from "effect/Effect"; import type { OrchestratorV2Error } from "../orchestration-v2/Orchestrator.ts"; import * as ThreadManagement from "../orchestration-v2/ThreadManagementService.ts"; import { resolveInteractionMode, resolveRuntimeMode } from "./delegatedTaskModes.ts"; +import { fencingLinkOrigin } from "./linkOrigin.ts"; import * as McpInvocationContext from "./McpInvocationContext.ts"; +const delegatedTaskPrefix = "thread:delegated-task:"; + +const suggestedThreadId = (threadId: ThreadId) => { + if (!threadId.startsWith(delegatedTaskPrefix)) return ThreadId.make(`mcp:${threadId}`); + const rest = threadId.slice(delegatedTaskPrefix.length); + // encodeURIComponent throws on lone surrogates, which a JSON string can carry. + if (!rest.isWellFormed()) return null; + const encoded = encodeURIComponent(rest); + return encoded === rest ? null : ThreadId.make(`${delegatedTaskPrefix}${encoded}`); +}; + /** - * Threads an agent starts over MCP are `mcp:`, and agents often paste the - * bare uuid. The id is never rewritten, so permission checks keyed on it still - * hold; the miss only names a live thread the caller could read by its full id. + * Agents paste bare uuids for `mcp:` threads and unencoded delegated-task ids. + * The id is never rewritten, so permission checks keyed on it still hold; the miss + * only names a live thread the caller could read by its full id. Ids from another + * machine miss here too. The link hint is static: listing links probes peers. */ export const threadNotFound = Effect.fn("OrchestratorMcpService.threadNotFound")(function* ( threads: Pick, + scope: McpInvocationContext.McpInvocationScope, threadId: ThreadId, message: string, ) { - const prefixed = ThreadId.make(`mcp:${threadId}`); - const shell = yield* threads.getThreadShell(prefixed).pipe(Effect.orElseSucceed(() => null)); + const suggested = suggestedThreadId(threadId); + const shell = + suggested === null + ? null + : yield* threads.getThreadShell(suggested).pipe(Effect.orElseSucceed(() => null)); + const mcpHint = + shell === null || shell.deletedAt !== null ? "" : ` Did you mean \`${suggested}\`?`; + const caller = + scope.thread === undefined + ? null + : yield* threads.getThreadShell(scope.thread.threadId).pipe(Effect.orElseSucceed(() => null)); + // A peer link's caller would be pointed at this machine's links, and a fenced one may not use them. + const linksHint = + scope.linkedEnvironments === false || + scope.client?.linked === true || + fencingLinkOrigin({ scope, caller: caller ?? undefined }) !== undefined + ? "" + : " Thread ids belong to one environment: for a thread on a linked one, find it with `t3_environment_links` and pass its `environmentId` to a tool that takes one."; return yield* new OrchestratorMcpFailure({ code: "thread_not_found", - message: - shell === null || shell.deletedAt !== null - ? message - : `${message} Did you mean \`${prefixed}\`?`, + message: `${message}${mcpHint}${linksHint}`, }); }); @@ -117,11 +144,18 @@ export const loadCaller = Effect.fn("mcp.loadCaller")(function* () { export const assertTargetWithinLimits = ( limits: CallerLimits, - target: { readonly runtimeMode: RuntimeMode; readonly interactionMode: ProviderInteractionMode }, + target: Pick, ) => resolveRuntimeMode(limits.runtimeMode, target.runtimeMode).pipe( Effect.andThen(resolveInteractionMode(limits.interactionMode, target.interactionMode)), Effect.asVoid, + Effect.mapError( + (error) => + new OrchestratorMcpFailure({ + code: error.code, + message: `Thread "${target.id}" runs in ${target.runtimeMode}/${target.interactionMode}, above this caller's ${limits.runtimeMode}/${limits.interactionMode} limit. For linked calls, this limit is also capped by the link's approved access. This call does not create an approval request. Have the user act directly in the target thread through an authorized client.`, + }), + ), ); /** Background subagents retain credentials until provider exit; archived threads and other instances cannot act. */ @@ -190,7 +224,12 @@ export const readThread = Effect.fn("mcp.readThread")(function* < const targetId = yield* resolveThreadId(context, threadId); const shell = yield* context.threads.getThreadShell(targetId).pipe(Effect.mapError(unavailable)); if (shell === null || shell.deletedAt !== null) { - return yield* threadNotFound(context.threads, targetId, "The thread was not found."); + return yield* threadNotFound( + context.threads, + context.scope, + targetId, + "The thread was not found.", + ); } const projection = yield* context.threads .getProjectThreadRecords({ projectId: shell.projectId, threadId: targetId }, fields, { diff --git a/apps/server/src/mcp/toolkits/core.test.ts b/apps/server/src/mcp/toolkits/core.test.ts index 35a18f5f0b460..81614e5eb4274 100644 --- a/apps/server/src/mcp/toolkits/core.test.ts +++ b/apps/server/src/mcp/toolkits/core.test.ts @@ -19,9 +19,12 @@ import { RuntimeRequestId, ThreadId, TurnItemId, + type OrchestrationV2ServerCommand, type OrchestrationV2ProjectedTurnItem, type OrchestrationV2ThreadShell, } from "@t3tools/contracts"; +import { pinOrderKeyBetween } from "@t3tools/shared/pinOrderKey"; +import * as DateTime from "effect/DateTime"; import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; @@ -447,6 +450,7 @@ it.effect("records only failed calls of tools that hand off no work", () => { Effect.succeed( McpToolAccessTestkit.idleThreadProjection(McpToolAccessTestkit.liveThreadShell(id)), ), + listActiveShellIndex: () => Effect.succeed([]), dispatch: () => Effect.succeed({ sequence: 1 } as never), }), ), @@ -756,6 +760,7 @@ it.effect("a client caller targets any thread within its ceiling and cannot act deletedAt: null, }, } as never), + listActiveShellIndex: () => Effect.succeed([]), dispatch: () => Effect.succeed({ sequence: 7, storedEvents: [] }), }), ), @@ -764,6 +769,78 @@ it.effect("a client caller targets any thread within its ceiling and cannot act ), ); +it.effect("an MCP pin lands above every existing pin, as a client pin does", () => { + const dispatched: Array = []; + let targetPinnedAt: DateTime.Utc | null = null; + const pinnedAt = DateTime.makeUnsafe("2026-11-10T00:00:00.000Z"); + const indexEntry = (id: string, pinned: boolean, pinOrderKey: string | null) => ({ + ...McpToolAccessTestkit.liveThreadShell(ThreadId.make(id)), + pinnedAt: pinned ? pinnedAt : null, + pinOrderKey, + mayBeLive: false, + hasRemoteTasks: false, + }); + return Effect.gen(function* () { + const server = yield* McpServer.McpServer; + const pin = server + .callTool({ + name: "t3_thread_organize", + arguments: { action: "pin", threadId: "pin-target" }, + }) + .pipe( + Effect.provideService(McpInvocationContext.McpInvocationContext, clientScope("auto")), + Effect.provideService(McpSchema.McpServerClient, client), + ); + + const first = yield* pin; + expect(first.isError).toBe(false); + const orderKey = (dispatched[0] as { readonly orderKey?: string }).orderKey; + expect(dispatched[0]).toMatchObject({ type: "thread.pin", threadId: "pin-target" }); + // The smallest key among pinned threads is "g"; the unpinned "b" and keyless pin are ignored. + expect(orderKey).toBe(pinOrderKeyBetween(null, "g")); + + // A thread the handler last saw pinned still gets a key: an unpin can commit + // before this command, and the reducer ignores the key if the pin holds. + targetPinnedAt = pinnedAt; + const again = yield* pin; + expect(again.isError).toBe(false); + expect(dispatched[1]).toMatchObject({ type: "thread.pin", orderKey }); + }).pipe( + Effect.provide( + layerThreadToolkit.pipe( + Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), + Layer.provide(NodeCrypto.layer), + Layer.provide( + Layer.mock(ThreadManagement.ThreadManagementService)({ + getThreadShell: (id) => + Effect.succeed(McpToolAccessTestkit.liveThreadShell(id, { runtimeMode: "auto" })), + getProjectThreadRecords: ({ threadId: id }) => { + const projection = McpToolAccessTestkit.idleThreadProjection( + McpToolAccessTestkit.liveThreadShell(id, { runtimeMode: "auto" }), + ); + return Effect.succeed({ + ...projection, + thread: { ...projection.thread, pinnedAt: targetPinnedAt }, + }); + }, + listActiveShellIndex: () => + Effect.succeed([ + indexEntry("pinned-late", true, "t"), + indexEntry("pinned-top", true, "g"), + indexEntry("pinned-keyless", true, null), + indexEntry("unpinned-stale-key", false, "b"), + ]), + dispatch: (command) => { + dispatched.push(command); + return Effect.succeed({ sequence: dispatched.length, storedEvents: [] }); + }, + }), + ), + ), + ), + ); +}); + it.effect("search names the calling thread, so an agent can tell its own match apart", () => Effect.gen(function* () { const server = yield* McpServer.McpServer; @@ -887,22 +964,23 @@ it.effect("a read-only client reads threads and is refused every write before it ); const dispatched: Array = []; -it.effect("refuses act-as-caller tools to a client caller", () => +it.effect("hides act-as-caller tools from a client caller", () => Effect.gen(function* () { const server = yield* McpServer.McpServer; - const result = yield* server + const error = yield* server .callTool({ name: "delegate_task", arguments: { task: "Review", mode: "async" }, }) .pipe( + Effect.flip, Effect.provideService( McpInvocationContext.McpInvocationContext, clientScope("full-access"), ), Effect.provideService(McpSchema.McpServerClient, client), ); - expect(declaredFailure(result)).toMatchObject({ code: "thread_credential_required" }); + expect(error.message).toContain("Tool 'delegate_task' not found"); }).pipe( Effect.provide( layerOrchestratorToolkit.pipe( @@ -1048,6 +1126,54 @@ it.effect.each([ ), ); +it.effect("run_scheduled_task_now reports why a started run failed to dispatch", () => + Effect.gen(function* () { + const server = yield* McpServer.McpServer; + const result = yield* server + .callTool({ name: "run_scheduled_task_now", arguments: { taskId: "task-full-access" } }) + .pipe( + Effect.provideService( + McpInvocationContext.McpInvocationContext, + clientScope("full-access"), + ), + Effect.provideService(McpSchema.McpServerClient, client), + ); + expect(result.isError).toBe(false); + expect(result.structuredContent).toMatchObject({ + threadId: null, + lastRunStatus: "failed", + lastRunError: "the provider is disabled", + }); + }).pipe( + Effect.provide( + McpHttpServer.layerThreadToolkit.pipe( + Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), + Layer.provide(NodeCrypto.layer), + Layer.provide(Layer.mock(ThreadManagement.ThreadManagementService)({})), + Layer.provide(Layer.mock(ThreadSearch.ThreadSearch)({})), + Layer.provide(Layer.mock(PeerForwarding.PeerForwarding)({})), + Layer.provide(Layer.mock(PeerLinkRequests.PeerLinkRequests)({})), + Layer.provide(Layer.mock(PeerLinks.PeerLinks)({})), + Layer.provide( + Layer.mock(ScheduledTaskService.ScheduledTaskService)({ + list: () => + Effect.succeed({ tasks: [scheduledTask("task-full-access", "full-access")] }), + runNow: () => + Effect.succeed({ + threadId: null, + task: { + ...(scheduledTask("task-full-access", "full-access") as object), + lastRunStatus: "failed" as const, + lastRunError: "the provider is disabled", + }, + } as never), + }), + ), + ), + ), + ), +); + function scheduledTask(id: string, runtimeMode: "auto" | "full-access"): never { return { id, diff --git a/apps/server/src/mcp/toolkits/device/handlers.ts b/apps/server/src/mcp/toolkits/device/handlers.ts index d4f0a4fac6a67..099a9bae9e2d3 100644 --- a/apps/server/src/mcp/toolkits/device/handlers.ts +++ b/apps/server/src/mcp/toolkits/device/handlers.ts @@ -49,7 +49,7 @@ export function agentDeviceQuickStart( .filter(Boolean) .join(" ") : host?.kind === "eas" - ? "This is an EAS Simulator session in the cloud. It bills the Expo account per minute until it stops, so call device_close with shutdown: true when you are done. It starts with no apps installed: install-from-source installs a build from a URL (iOS needs a .tar.gz containing a .app), and install uploads one from this machine." + ? "This is an Expo Simulators session in the cloud. It bills the Expo account per minute until it stops, so call device_close with shutdown: true when you are done. It starts with no apps installed: install-from-source installs a build from a URL (iOS needs a .tar.gz containing a .app), and install uploads one from this machine." : device.platform === "ios" ? "First use builds an XCTest runner and can take a couple of minutes; later commands are fast." : "The Android snapshot helper installs itself on first use."; @@ -89,7 +89,7 @@ const requireDeviceAccess = McpInvocationContext.requireThreadMcpCapability("dev () => new DeviceToolUnavailableError({ reason: - "Agent device access is turned off for this environment. Ask the user to turn on Agent device access in Settings > Integrations > Devices; it applies when the agent session next starts, such as in a new thread.", + "Agent device access is turned off for this environment. Ask the user to turn on the Device hub and Agent device access in Settings > Integrations > Devices; it applies when the agent session next starts, such as in a new thread.", }), ), ); @@ -275,7 +275,7 @@ const handlers = { after.devices.find( (candidate) => candidate.hostId === session.hostId && candidate.id === session.deviceId, ) ?? target; - // Target the opened device: an EAS device and its agent-device daemon + // Target the opened device: an Expo device and its agent-device daemon // exist only once its session runs, and Android ids change on boot. const opened = { threadId: scope.thread.threadId, diff --git a/apps/server/src/mcp/toolkits/device/tools.ts b/apps/server/src/mcp/toolkits/device/tools.ts index 0a79af2efb0f6..c87b579843ad8 100644 --- a/apps/server/src/mcp/toolkits/device/tools.ts +++ b/apps/server/src/mcp/toolkits/device/tools.ts @@ -12,7 +12,7 @@ import * as Schema from "effect/Schema"; import * as FileSystem from "effect/FileSystem"; import * as Path from "effect/Path"; import * as ServerConfig from "../../../config.ts"; -import { Tool, Toolkit } from "effect/ai"; +import { McpSchema, Tool, Toolkit } from "effect/ai"; import * as McpInvocationContext from "../../McpInvocationContext.ts"; import * as DeviceService from "../../../device/DeviceService.ts"; @@ -45,7 +45,8 @@ const DeviceListTool = Tool.make("device_list", { .annotate(Tool.Readonly, true) .annotate(Tool.Destructive, false) .annotate(Tool.Idempotent, true) - .annotate(Tool.OpenWorld, false); + .annotate(Tool.OpenWorld, false) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); const DeviceOpenTool = Tool.make("device_open", { description: @@ -58,7 +59,7 @@ const DeviceOpenTool = Tool.make("device_open", { FileSystem.FileSystem, Path.Path, ServerConfig.ServerConfig, - // The thread's project can name the EAS project a cloud device runs under. + // The thread's project can name the Expo project a cloud device runs under. ProjectService.ProjectService, ], }) @@ -66,7 +67,8 @@ const DeviceOpenTool = Tool.make("device_open", { .annotate(Tool.Readonly, false) .annotate(Tool.Destructive, false) .annotate(Tool.Idempotent, true) - .annotate(Tool.OpenWorld, true); + .annotate(Tool.OpenWorld, true) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); export const DeviceScreenshotTool = Tool.make("device_screenshot", { description: @@ -80,7 +82,8 @@ export const DeviceScreenshotTool = Tool.make("device_screenshot", { .annotate(Tool.Readonly, true) .annotate(Tool.Destructive, false) .annotate(Tool.Idempotent, true) - .annotate(Tool.OpenWorld, true); + .annotate(Tool.OpenWorld, true) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); const DeviceCloseTool = Tool.make("device_close", { description: @@ -94,7 +97,8 @@ const DeviceCloseTool = Tool.make("device_close", { .annotate(Tool.Readonly, false) .annotate(Tool.Destructive, true) .annotate(Tool.Idempotent, true) - .annotate(Tool.OpenWorld, true); + .annotate(Tool.OpenWorld, true) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); export const DeviceStandardToolkit = Toolkit.make(DeviceListTool, DeviceOpenTool, DeviceCloseTool); diff --git a/apps/server/src/mcp/toolkits/html/tools.ts b/apps/server/src/mcp/toolkits/html/tools.ts index 90d54d057f5f6..0cb6153025466 100644 --- a/apps/server/src/mcp/toolkits/html/tools.ts +++ b/apps/server/src/mcp/toolkits/html/tools.ts @@ -10,7 +10,7 @@ import { HTML_RENDER_TOOL_NAME, } from "@t3tools/shared/htmlRender"; import * as Schema from "effect/Schema"; -import { Tool, Toolkit } from "effect/ai"; +import { McpSchema, Tool, Toolkit } from "effect/ai"; import * as HtmlRender from "../../../htmlRender/HtmlRender.ts"; import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts"; @@ -72,7 +72,8 @@ export const HtmlPreviewTool = Tool.make("html_preview", { .annotate(Tool.Readonly, true) .annotate(Tool.Destructive, false) .annotate(Tool.Idempotent, true) - .annotate(Tool.OpenWorld, true); + .annotate(Tool.OpenWorld, true) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); // This affects only the caller's thread, not its workspace, so read-only sandboxes and plan mode may use it. const HtmlRenderTool = Tool.make(HTML_RENDER_TOOL_NAME, { @@ -109,7 +110,8 @@ const HtmlRenderTool = Tool.make(HTML_RENDER_TOOL_NAME, { .annotate(Tool.Readonly, true) .annotate(Tool.Destructive, false) .annotate(Tool.Idempotent, false) - .annotate(Tool.OpenWorld, true); + .annotate(Tool.OpenWorld, true) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); export const HtmlPreviewToolkit = Toolkit.make(HtmlPreviewTool); diff --git a/apps/server/src/mcp/toolkits/message/tools.ts b/apps/server/src/mcp/toolkits/message/tools.ts index 848cfa46976fb..418a252eb9ead 100644 --- a/apps/server/src/mcp/toolkits/message/tools.ts +++ b/apps/server/src/mcp/toolkits/message/tools.ts @@ -1,7 +1,7 @@ import { OrchestratorMcpFailure } from "@t3tools/contracts"; import { SEND_TO_USER_MAX_LENGTH, SEND_TO_USER_TOOL_NAME } from "@t3tools/shared/toolOutput"; import * as Schema from "effect/Schema"; -import { Tool, Toolkit } from "effect/ai"; +import { McpSchema, Tool, Toolkit } from "effect/ai"; import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts"; import * as McpInvocationContext from "../../McpInvocationContext.ts"; @@ -33,6 +33,7 @@ const SendToUserTool = Tool.make(SEND_TO_USER_TOOL_NAME, { .annotate(Tool.Destructive, false) .annotate(Tool.Idempotent, false) .annotate(Tool.OpenWorld, false) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients) // Claude Code defers MCP tools behind ToolSearch; Anthropic says to declare this from the first request. .annotate(Tool.Meta, { "anthropic/alwaysLoad": true }); diff --git a/apps/server/src/mcp/toolkits/orchestrator/tools.ts b/apps/server/src/mcp/toolkits/orchestrator/tools.ts index d4b6c2b67d05c..629a5352587f0 100644 --- a/apps/server/src/mcp/toolkits/orchestrator/tools.ts +++ b/apps/server/src/mcp/toolkits/orchestrator/tools.ts @@ -97,7 +97,8 @@ const DelegateTaskTool = Tool.make("delegate_task", { }) .annotate(Tool.Title, "Delegate a child task") .annotate(Tool.Destructive, true) - .annotate(Tool.OpenWorld, true); + .annotate(Tool.OpenWorld, true) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); const TaskStatusTool = Tool.make("task_status", { description: @@ -111,7 +112,8 @@ const TaskStatusTool = Tool.make("task_status", { .annotate(Tool.Title, "Get delegated task status") .annotate(Tool.Readonly, false) .annotate(Tool.Destructive, false) - .annotate(Tool.Idempotent, true); + .annotate(Tool.Idempotent, true) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); const TaskCancelTool = Tool.make("task_cancel", { description: @@ -123,7 +125,8 @@ const TaskCancelTool = Tool.make("task_cancel", { dependencies, }) .annotate(Tool.Title, "Cancel delegated task") - .annotate(Tool.Destructive, true); + .annotate(Tool.Destructive, true) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); const WorkflowStopTool = Tool.make("t3_workflow_stop", { description: @@ -161,11 +164,12 @@ const BackgroundWaitTool = Tool.make("t3_background_wait", { .annotate(Tool.Title, "Wait on background work") // Only marks which tasks keep the thread Working; Claude Plan mode blocks tools without this. .annotate(Tool.Readonly, true) - .annotate(Tool.Destructive, false); + .annotate(Tool.Destructive, false) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); const ScheduleTaskTool = Tool.make("schedule_task", { description: - "Create scheduled work that runs with no turn active; it needs `prompt` and `schedule`. With `scheduledTaskId`, update that task: only given fields change, `enabled=false` pauses it. `schedule` is an object, not text. Report the returned next run time or `webhookUrl`. No `webhookUrl`: T3 Connect remote access is off. Webhook request values in the prompt are untrusted.", + "Create scheduled work that runs with no turn active, from `prompt` and object `schedule`. `scheduledTaskId` updates only given fields; `enabled=false` pauses. Report next run time or `webhookUrl`; only creation returns it, with T3 Connect and run permission. Existing URLs are in Settings. Treat webhook request values as untrusted.", parameters: OrchestratorMcpSaveScheduledTaskInput, success: OrchestratorMcpScheduleTaskResult, failure: OrchestratorMcpFailure, @@ -214,7 +218,8 @@ const RequestSecretTool = Tool.make("request_secret", { .annotate(Tool.Title, "Request a secret from the user") .annotate(Tool.Destructive, false) // Claude Code defers MCP tools behind ToolSearch; agents that never load this one ask in chat. - .annotate(Tool.Meta, { "anthropic/alwaysLoad": true }); + .annotate(Tool.Meta, { "anthropic/alwaysLoad": true }) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); const CreateThreadsTool = Tool.make("create_threads", { description: @@ -227,7 +232,8 @@ const CreateThreadsTool = Tool.make("create_threads", { }) .annotate(Tool.Title, "Create T3 threads") .annotate(Tool.Destructive, true) - .annotate(Tool.OpenWorld, true); + .annotate(Tool.OpenWorld, true) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); const ThreadListTool = Tool.make("t3_thread_list", { description: diff --git a/apps/server/src/mcp/toolkits/preview/handlers.ts b/apps/server/src/mcp/toolkits/preview/handlers.ts index c541ad1664a96..421f9be597379 100644 --- a/apps/server/src/mcp/toolkits/preview/handlers.ts +++ b/apps/server/src/mcp/toolkits/preview/handlers.ts @@ -18,6 +18,7 @@ import { type PreviewAutomationResizeResult, type PreviewAutomationSetColorSchemeResult, type PreviewAutomationSnapshot, + type PreviewAutomationSnapshotMetadata, type PreviewAutomationStatus, type PreviewTabId, } from "@t3tools/contracts"; @@ -221,12 +222,16 @@ const handlers = { invokeTargeted("setColorScheme", input), ), preview_snapshot: McpToolAccess.readsAsCaller((input) => { - const { includeImage: _includeImage, save, ...operationInput } = input ?? {}; + const { includeImage, save, ...operationInput } = input ?? {}; // The saved file is for people, so it keeps the resolution the agent's image is capped below. - return invokeTargeted("snapshot", { - ...operationInput, - ...(save === true ? { fullScreenshot: true } : {}), - }); + return invokeTargeted( + "snapshot", + { + ...operationInput, + ...(save === true ? { fullScreenshot: true } : {}), + ...(includeImage === true || save === true ? {} : { pixelsRequired: false }), + }, + ); }), preview_act: McpToolAccess.actsAsCaller(({ action, ...input }) => decodeActInput[action](input).pipe( diff --git a/apps/server/src/mcp/toolkits/preview/tools.ts b/apps/server/src/mcp/toolkits/preview/tools.ts index b2c9afa11bb2a..37f09c8c8223d 100644 --- a/apps/server/src/mcp/toolkits/preview/tools.ts +++ b/apps/server/src/mcp/toolkits/preview/tools.ts @@ -16,13 +16,14 @@ import { PreviewAutomationSetColorSchemeInput, PreviewAutomationSetColorSchemeResult, PreviewAutomationSnapshot, + PreviewAutomationSnapshotMetadata, PreviewAutomationStatus, PreviewAutomationTabTargetInput, PreviewAutomationWaitForInput, } from "@t3tools/contracts"; import * as Schema from "effect/Schema"; import * as FileSystem from "effect/FileSystem"; -import { Tool, Toolkit } from "effect/ai"; +import { McpSchema, Tool, Toolkit } from "effect/ai"; import * as McpInvocationContext from "../../McpInvocationContext.ts"; import * as PreviewAutomationBroker from "../../PreviewAutomationBroker.ts"; @@ -42,10 +43,16 @@ const presentationFields = { toolIcon: Schema.optional(ToolActivityIcon) }; const PreviewActionResult = Schema.Struct(presentationFields); const browserTool = (tool: T): T => - tool.annotate(Tool.OpenWorld, true).annotate(Tool.Destructive, true) as T; + tool + .annotate(Tool.OpenWorld, true) + .annotate(Tool.Destructive, true) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients) as T; const safeBrowserTool = (tool: T): T => - tool.annotate(Tool.OpenWorld, true).annotate(Tool.Destructive, false) as T; + tool + .annotate(Tool.OpenWorld, true) + .annotate(Tool.Destructive, false) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients) as T; const readonlyBrowserTool = (tool: T): T => safeBrowserTool(tool).annotate(Tool.Readonly, true).annotate(Tool.Idempotent, true) as T; @@ -61,7 +68,8 @@ const PreviewStatusTool = Tool.make("preview_status", { .annotate(Tool.Title, "Get preview status") .annotate(Tool.Readonly, true) .annotate(Tool.Destructive, false) - .annotate(Tool.Idempotent, true); + .annotate(Tool.Idempotent, true) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); // `url` already covers websites, so open only adds the dev-server target. const EnvironmentPortTarget = BrowserNavigationTarget.members[1]; @@ -151,7 +159,7 @@ export const PreviewSnapshotTool = safeBrowserTool( }), ), }), - success: PreviewAutomationSnapshot, + success: Schema.Union([PreviewAutomationSnapshot, PreviewAutomationSnapshotMetadata]), failure: PreviewToolFailure, dependencies, }).annotate(Tool.Title, "Inspect browser page"), diff --git a/apps/server/src/mcp/toolkits/previewControls/tools.ts b/apps/server/src/mcp/toolkits/previewControls/tools.ts index 0d3ea5a853ee2..ca92f57f408b8 100644 --- a/apps/server/src/mcp/toolkits/previewControls/tools.ts +++ b/apps/server/src/mcp/toolkits/previewControls/tools.ts @@ -1,4 +1,5 @@ import { + clampedPositiveInt, NonNegativeInt, OrchestratorMcpFailure, PreviewAutomationError, @@ -6,7 +7,7 @@ import { PreviewTabId, } from "@t3tools/contracts"; import * as Schema from "effect/Schema"; -import { Tool, Toolkit } from "effect/ai"; +import { McpSchema, Tool, Toolkit } from "effect/ai"; import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts"; import * as PreviewManager from "../../../preview/Manager.ts"; import * as PreviewAutomationBroker from "../../PreviewAutomationBroker.ts"; @@ -28,7 +29,7 @@ const PreviewListTool = Tool.make("t3_preview_list", { "List this thread's preview tabs. Only `runtime:'server'` entries can be automated here. Others are in the user's local desktop app. Never pass a non-server `tabId` to a `preview_*` tool. Tell the user that tab is on their local desktop and this environment cannot reach it.", parameters: Schema.Struct({ cursor: Schema.optional(NonNegativeInt), - limit: Schema.optional(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 50 }))), + limit: Schema.optional(clampedPositiveInt(50)), }), success: Schema.Struct({ ...PreviewListResult.fields, @@ -36,12 +37,15 @@ const PreviewListTool = Tool.make("t3_preview_list", { }), }) .annotate(Tool.Readonly, true) - .annotate(Tool.Destructive, false); + .annotate(Tool.Destructive, false) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); const PreviewCloseTool = Tool.make("t3_preview_close", { ...shared, description: "Close one preview tab this thread owns. Use it on a tab you opened for your own check when you finish.", parameters: Schema.Struct({ tabId: PreviewTabId }), success: Schema.Struct({}), -}).annotate(Tool.Destructive, true); +}) + .annotate(Tool.Destructive, true) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); export const PreviewControlsToolkit = Toolkit.make(PreviewListTool, PreviewCloseTool); diff --git a/apps/server/src/mcp/toolkits/project/handlers.test.ts b/apps/server/src/mcp/toolkits/project/handlers.test.ts index c65c2c83746a6..984a3a7417785 100644 --- a/apps/server/src/mcp/toolkits/project/handlers.test.ts +++ b/apps/server/src/mcp/toolkits/project/handlers.test.ts @@ -55,6 +55,7 @@ import { ProjectToolkit } from "./tools.ts"; const clientLaunchHarness = (input: { readonly runtimeModeCeiling: RuntimeMode; readonly defaultRuntimeMode?: RuntimeMode; + readonly newWorktreesStartFromOrigin?: boolean; readonly fallbackModel?: ModelSelection; readonly providers?: readonly ServerProvider[]; /** Where Settings saves a project's model; the legacy project field stays null. */ @@ -84,6 +85,9 @@ const clientLaunchHarness = (input: { ...(input.defaultRuntimeMode === undefined ? {} : { defaultRuntimeMode: input.defaultRuntimeMode }), + ...(input.newWorktreesStartFromOrigin === undefined + ? {} + : { newWorktreesStartFromOrigin: input.newWorktreesStartFromOrigin }), ...(input.overrideModel === undefined ? {} : { @@ -171,7 +175,10 @@ const clientLaunchHarness = (input: { ), }), Layer.mock(PeerForwarding.PeerForwarding)({}), - Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({ namedProjectsRoot: "/projects" }), + Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({ + namedProjectsRoot: "/projects", + isScratchProject: () => Effect.succeed(false), + }), DocumentText.layer.pipe(Layer.provideMerge(NodeServices.layer)), ).pipe( Layer.provideMerge(GitVcsDriver.layer), @@ -400,7 +407,9 @@ it.effect("a launch with a model its provider does not advertise creates no thre ] as const) { expect(yield* launchWith(instanceId, model)).toMatchObject({ code: "model_unavailable", - message: `Model ${model} is not advertised by provider ${instanceId}.`, + message: expect.stringContaining( + `Model ${model} is not advertised by provider ${instanceId}. Advertised models: `, + ), }); } expect(launched).toHaveLength(0); @@ -609,6 +618,51 @@ it.effect("a launch into a new worktree creates nothing when its base ref has no }), ); +it.effect("a worktree launch that omits startFromOrigin follows the new-worktree setting", () => + Effect.gen(function* () { + const cases = [ + { setting: true, given: undefined, expected: true }, + { setting: false, given: undefined, expected: false }, + { setting: true, given: false, expected: false }, + ]; + for (const { setting, given, expected } of cases) { + const launched: Array = []; + const checked: Array = []; + const { projectId, dependencies } = clientLaunchHarness({ + runtimeModeCeiling: "auto", + launched, + newWorktreesStartFromOrigin: setting, + checkWorktreeBase: ({ workspaceStrategy }) => + Effect.sync(() => { + checked.push(workspaceStrategy); + }), + }); + const toolkit = yield* ProjectToolkit.pipe( + Effect.provide( + McpToolAccess.HandlersLayer.layer(ProjectHandlers.layer).pipe( + Layer.provide(dependencies), + ), + ), + ); + yield* toolkit + .handle("t3_thread_launch", { + title: "Fix", + projectId, + workspaceStrategy: { + type: "worktree", + baseRef: "main", + ...(given === undefined ? {} : { startFromOrigin: given }), + }, + }) + .pipe(Stream.unwrap, Stream.runCollect, Effect.provide(dependencies)); + + const strategy = { type: "worktree", baseRef: "main", startFromOrigin: expected }; + expect(checked).toEqual([strategy]); + expect(launched.map((launch) => launch.workspaceStrategy)).toEqual([strategy]); + } + }), +); + it.effect("a launch into a new worktree creates nothing when its branch collides", () => Effect.gen(function* () { const launched: Array = []; @@ -840,6 +894,22 @@ it.effect("an agent's clone is not cut off while git is still transferring", () }).pipe(Effect.provide(NodeServices.layer)), ); +const deletedProject = (id: ProjectId) => ({ + id, + title: "client-target", + workspaceRoot: "/projects/client-target", + repositoryIdentity: null, + faviconPath: null, + defaultModelSelection: null, + defaultThreadEnvMode: null, + autoPull: false, + projectIcon: null, + scripts: [], + createdAt: "2026-01-01T00:00:00.000Z", + updatedAt: "2026-01-01T00:00:00.000Z", + deletedAt: "2026-01-01T00:00:00.000Z", +}); + it.effect("deleting a project through MCP also discards its clone record", () => Effect.gen(function* () { const discarded: Array = []; @@ -848,22 +918,7 @@ it.effect("deleting a project through MCP also discards its clone record", () => harness.dependencies, Layer.mergeAll( Layer.mock(Project.ProjectService)({ - delete: (input) => - Effect.succeed({ - id: input.projectId, - title: "client-target", - workspaceRoot: "/projects/client-target", - repositoryIdentity: null, - faviconPath: null, - defaultModelSelection: null, - defaultThreadEnvMode: null, - autoPull: false, - projectIcon: null, - scripts: [], - createdAt: "2026-01-01T00:00:00.000Z", - updatedAt: "2026-01-01T00:00:00.000Z", - deletedAt: "2026-01-01T00:00:00.000Z", - }), + delete: (input) => Effect.succeed(deletedProject(input.projectId)), }), Layer.mock(ProjectCloneTracker.ProjectCloneTracker)({ discard: (projectId) => Effect.sync(() => void discarded.push(projectId)), @@ -1001,6 +1056,45 @@ it.effect("project git account tools list accounts without credentials and route }), ); +it.effect("an agent cannot delete the built-in scratch project, even with force", () => + Effect.gen(function* () { + const deleted: Array = []; + const harness = clientLaunchHarness({ runtimeModeCeiling: "full-access", launched: [] }); + const dependencies = Layer.merge( + harness.dependencies, + Layer.mergeAll( + Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({ + namedProjectsRoot: "/projects", + isScratchProject: (projectId) => Effect.succeed(projectId === harness.projectId), + }), + Layer.mock(Project.ProjectService)({ + delete: (input) => + Effect.sync(() => { + deleted.push(input.projectId); + return deletedProject(input.projectId); + }), + }), + Layer.mock(ProjectCloneTracker.ProjectCloneTracker)({ discard: () => Effect.void }), + ), + ); + const toolkit = yield* ProjectToolkit.pipe( + Effect.provide( + McpToolAccess.HandlersLayer.layer(ProjectHandlers.layer).pipe(Layer.provide(dependencies)), + ), + ); + + const refused = yield* toolkit + .handle("t3_project_delete", { projectId: harness.projectId, force: true }) + .pipe(Stream.unwrap, Stream.runCollect, Effect.provide(dependencies)); + + expect(deleted).toEqual([]); + expect(refused.at(-1)?.result).toMatchObject({ + code: "invalid_request", + message: expect.stringMatching(/scratch|no project/i), + }); + }), +); + it.effect("an update the project refuses tells the agent why", () => Effect.gen(function* () { const harness = clientLaunchHarness({ runtimeModeCeiling: "full-access", launched: [] }); diff --git a/apps/server/src/mcp/toolkits/project/handlers.ts b/apps/server/src/mcp/toolkits/project/handlers.ts index 4f0cd75677c3a..d3a5de28a65bb 100644 --- a/apps/server/src/mcp/toolkits/project/handlers.ts +++ b/apps/server/src/mcp/toolkits/project/handlers.ts @@ -192,8 +192,28 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, { if (provider !== undefined) yield* OrchestrationMcp.requireAdvertisedModel(provider, model); } + const projectSettings = + project === undefined + ? undefined + : yield* ServerSettings.ServerSettingsService.pipe( + Effect.flatMap((settings) => settings.getSettings), + Effect.mapError(unavailable), + Effect.map( + (settings) => resolveProjectSettings(settings, projectId, project).settings, + ), + ); + // An omitted startFromOrigin follows the new-worktree setting, as the composer and + // t3_worktree_handoff do. Otherwise a stale local branch silently becomes the base. + const strategy = + projectSettings !== undefined && + input.workspaceStrategy?.type === "worktree" && + input.workspaceStrategy.startFromOrigin === undefined + ? { + ...input.workspaceStrategy, + startFromOrigin: projectSettings.newWorktreesStartFromOrigin, + } + : input.workspaceStrategy; // Reject unresolved base refs and blocked branch names before accepting a launch; worktree provisioning happens later. - const strategy = input.workspaceStrategy; if (!replaying && strategy?.type === "worktree") yield* ThreadLaunch.ThreadLaunchService.pipe( Effect.flatMap((launches) => @@ -210,16 +230,7 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, { // A caller outside a thread has nothing to inherit, so it starts with the // project's effective defaults, as the composer does. Its mode stays // within the ceiling it was approved with. - const projectDefaults = - caller === undefined && project !== undefined - ? yield* ServerSettings.ServerSettingsService.pipe( - Effect.flatMap((settings) => settings.getSettings), - Effect.mapError(unavailable), - Effect.map( - (settings) => resolveProjectSettings(settings, projectId, project).settings, - ), - ) - : undefined; + const projectDefaults = caller === undefined ? projectSettings : undefined; const modelSelection = input.modelSelection ?? launchedModel ?? @@ -266,7 +277,7 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, { modelSelection, runtimeMode: startedRuntimeMode, interactionMode, - workspaceStrategy: input.workspaceStrategy ?? { type: "root" }, + workspaceStrategy: strategy ?? { type: "root" }, ...(input.message === undefined && attachments.length === 0 ? {} : { @@ -480,6 +491,13 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, { ), t3_project_delete: McpToolAccess.writesEnvironment((input) => Effect.gen(function* () { + const folders = yield* ManagedProjectFolders.ManagedProjectFolders; + if (yield* folders.isScratchProject(input.projectId)) + return yield* new OrchestratorMcpFailure({ + code: "invalid_request", + message: + '"No project" is the built-in scratch project and cannot be deleted through MCP. The user can remove it in Settings.', + }); const projects = yield* Project.ProjectService; const clones = yield* ProjectCloneTracker.ProjectCloneTracker; return yield* mutateProjectWithClone(projects, clones, { diff --git a/apps/server/src/mcp/toolkits/project/tools.ts b/apps/server/src/mcp/toolkits/project/tools.ts index 66864d6a77205..309fd964f075a 100644 --- a/apps/server/src/mcp/toolkits/project/tools.ts +++ b/apps/server/src/mcp/toolkits/project/tools.ts @@ -1,6 +1,7 @@ // Fork: Keep t3_project_git_account and project-read account metadata, including the user-confirmation requirement and credential redaction. import { McpAttachmentInput } from "../attachment/input.ts"; import { + clampedPositiveInt, NonNegativeInt, ModelSelection, TrimmedNonEmptyString, @@ -60,7 +61,7 @@ const ProjectListTool = Tool.make("t3_project_list", { parameters: Schema.Struct({ environmentId: OrchestratorMcpEnvironmentTarget, cursor: Schema.optional(NonNegativeInt), - limit: Schema.optional(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 100 }))), + limit: Schema.optional(clampedPositiveInt(100)), }), success: Schema.Struct({ projects: Schema.Array(Project), @@ -120,7 +121,11 @@ const ProjectDeleteTool = Tool.make("t3_project_delete", { projectId: ProjectId, force: Schema.optionalKey(Schema.Boolean), }), - dependencies: [...shared.dependencies, ProjectCloneTracker.ProjectCloneTracker], + dependencies: [ + ...shared.dependencies, + ProjectCloneTracker.ProjectCloneTracker, + ManagedProjectFolders.ManagedProjectFolders, + ], }).annotate(Tool.Destructive, true); const ProjectCloneTool = Tool.make("t3_project_clone", { ...shared, diff --git a/apps/server/src/mcp/toolkits/pullRequests/handlers.test.ts b/apps/server/src/mcp/toolkits/pullRequests/handlers.test.ts index e797d6bcf5c2b..3c454cbe6e48c 100644 --- a/apps/server/src/mcp/toolkits/pullRequests/handlers.test.ts +++ b/apps/server/src/mcp/toolkits/pullRequests/handlers.test.ts @@ -248,7 +248,9 @@ describe("pull request toolkit handlers", () => { const error = yield* harness .call("link_pull_request", { threadId: bareId, number: 123 }) .pipe(Effect.flip); - expect(error.message).toBe(`Thread ${bareId} was not found. Did you mean \`mcp:${bareId}\`?`); + expect(error.message).toBe( + `Thread ${bareId} was not found. Did you mean \`mcp:${bareId}\`? Thread ids belong to one environment: for a thread on a linked one, find it with \`t3_environment_links\` and pass its \`environmentId\` to a tool that takes one.`, + ); expect(error).toMatchObject({ _tag: "OrchestratorMcpFailure", code: "thread_not_found" }); expect(yield* Ref.get(harness.commands)).toEqual([]); }), @@ -724,6 +726,36 @@ describe("pull request guide tools", () => { }), ); + it.effect( + "reads a guide on a host readable without a checkout through the caller's project", + () => + Effect.gen(function* () { + const api = makeProject({ + canonicalKey: "bitbucket.org/acme/api", + locator: { + source: "git-remote", + remoteName: "origin", + remoteUrl: "git@bitbucket.org:acme/api.git", + }, + provider: SourceControlProviderKind.make("bitbucket"), + displayName: "acme/api", + owner: "acme", + name: "api", + }); + const harness = yield* makeHarness({ project: api }); + const result = yield* harness.call("read_pull_request_guide", { + url: "https://bitbucket.org/acme/web/pull-requests/123", + }); + expect(result).toMatchObject({ + host: "bitbucket.org", + repository: "acme/web", + number: 123, + guide: null, + currentHeadSha: "head-1", + }); + }), + ); + it.effect("finds an Azure DevOps checkout for a pull request named by its full path", () => Effect.gen(function* () { const azure = { diff --git a/apps/server/src/mcp/toolkits/pullRequests/handlers.ts b/apps/server/src/mcp/toolkits/pullRequests/handlers.ts index 84e65ce877f69..884dd09a3dea4 100644 --- a/apps/server/src/mcp/toolkits/pullRequests/handlers.ts +++ b/apps/server/src/mcp/toolkits/pullRequests/handlers.ts @@ -174,7 +174,7 @@ const make = Effect.gen(function* () { Effect.mapError((cause) => new Failure({ cause })), ); if (Option.isNone(thread) || thread.value.deletedAt !== null) { - return yield* threadNotFound(engine, threadId, `Thread ${threadId} was not found.`); + return yield* threadNotFound(engine, scope, threadId, `Thread ${threadId} was not found.`); } return thread.value; }); @@ -208,7 +208,7 @@ const make = Effect.gen(function* () { return yield* watches.setWatching({ threadId, target: input, watching }).pipe( Effect.catchTags({ PullRequestWatchThreadNotFoundError: (error) => - threadNotFound(engine, error.threadId, error.message), + threadNotFound(engine, scope, error.threadId, error.message), }), ); }); @@ -244,10 +244,17 @@ const make = Effect.gen(function* () { input, target, project: projectForPullRequest(known, target, callerProject), + callerProject, failure: null, })), Effect.catch((error) => - Effect.succeed({ input, target: null, project: undefined, failure: error.message }), + Effect.succeed({ + input, + target: null, + project: undefined, + callerProject, + failure: error.message, + }), ), ), ); @@ -492,18 +499,26 @@ const make = Effect.gen(function* () { if (entry !== undefined && entry.target === null) { return yield* new PullRequestGuideFailedError({ detail: entry.failure ?? "" }); } - if (entry === undefined || entry.target === null || entry.project === undefined) { + // Hosts readable without a checkout (GitHub, Bitbucket, Forgejo) are read through the + // caller's project; PullRequestService decides whether the host allows that. + const project = entry?.project ?? entry?.callerProject; + if (entry === undefined || entry.target === null || project === undefined) { return yield* new PullRequestProjectNotFoundError({ repository: entry?.target.repository ?? input.repository ?? "", }); } - const result = yield* reviewGuides.get(guideReference(entry.project, entry.target)).pipe( - Effect.mapError( - (error) => - new PullRequestGuideFailedError({ - detail: guideFailureDetail(error), - cause: error, - }), + const target = entry.target; + const result = yield* reviewGuides.get(guideReference(project, target)).pipe( + Effect.mapError((error) => + entry.project === undefined && + error._tag === "PullRequestUnavailableError" && + (error.reason === "provider-unsupported" || + error.reason === "repository-without-project") + ? new PullRequestProjectNotFoundError({ repository: target.repository }) + : new PullRequestGuideFailedError({ + detail: guideFailureDetail(error), + cause: error, + }), ), ); return { diff --git a/apps/server/src/mcp/toolkits/thread/handlers.ts b/apps/server/src/mcp/toolkits/thread/handlers.ts index 88bb91bf62b77..972174638e10f 100644 --- a/apps/server/src/mcp/toolkits/thread/handlers.ts +++ b/apps/server/src/mcp/toolkits/thread/handlers.ts @@ -9,6 +9,7 @@ import { } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; import { modelSelectionCommandType } from "@t3tools/shared/model"; +import { topOfPinnedRunOrderKey } from "@t3tools/shared/pinOrderKey"; import * as McpInvocationContext from "../../McpInvocationContext.ts"; import * as McpToolAccess from "../../McpToolAccess.ts"; @@ -19,6 +20,7 @@ import { readThread, unavailable, } from "../../threadAccess.ts"; +import type * as ThreadManagement from "../../../orchestration-v2/ThreadManagementService.ts"; import * as ThreadSearch from "../../../orchestration-v2/ThreadSearch.ts"; import * as ScheduledTasks from "../../../scheduledTasks/ScheduledTaskService.ts"; import { queuedRunsInDeliveryOrder } from "../../../orchestration-v2/QueuedRunOrder.ts"; @@ -50,6 +52,20 @@ const dispatch = Effect.fn("mcp.dispatchThreadCommand")(function* ( return { sequence: result.sequence }; }); +/** + * Places an agent pin at the top of the Pinned section, as a client pin is placed. + * Without a key the sidebar sorts the pin below every keyed pin. Ordering is + * best effort: if the index cannot be read, the pin still lands, keyless. + */ +const topOfPinnedRunKey = Effect.fn("mcp.topOfPinnedRunKey")(function* ( + threads: Pick, +) { + const index = yield* threads.listActiveShellIndex().pipe(Effect.orElseSucceed(() => null)); + if (index === null) return undefined; + const pinnedKeys = index.flatMap((entry) => (entry.pinnedAt == null ? [] : [entry.pinOrderKey])); + return topOfPinnedRunOrderKey(pinnedKeys) ?? undefined; +}); + const readQuestion = Effect.fn("mcp.readQuestion")(function* (input: { threadId?: ThreadId | undefined; requestId: RuntimeRequestId; @@ -118,6 +134,7 @@ export const layer = McpToolAccess.toLayer(ThreadToolkit, { link: McpInvocationContext.threadLinkFor(scope, threadId), }), lastRunStatus: task.lastRunStatus, + lastRunError: task.lastRunError, runCount: task.runCount, nextRunAt: task.nextRunAt, }; @@ -369,6 +386,14 @@ export const layer = McpToolAccess.toLayer(ThreadToolkit, { case "mark_unread": command = { ...common, type: "thread.mark-unread" }; break; + case "pin": { + // Always send a key, as the client does: the reducer ignores it for a + // thread that is already pinned, and a projection read before this + // command can be stale if an unpin commits in between. + const orderKey = yield* topOfPinnedRunKey(threads); + command = { ...common, type: "thread.pin", ...(orderKey ? { orderKey } : {}) }; + break; + } default: command = { ...common, type: `thread.${input.action}` }; } diff --git a/apps/server/src/mcp/toolkits/thread/tools.ts b/apps/server/src/mcp/toolkits/thread/tools.ts index 4c59ea4f2028f..d6c9a34565ddb 100644 --- a/apps/server/src/mcp/toolkits/thread/tools.ts +++ b/apps/server/src/mcp/toolkits/thread/tools.ts @@ -19,6 +19,7 @@ import { RunId, NonNegativeInt, ProjectId, + clampedPositiveInt, } from "@t3tools/contracts"; import * as SecureRandom from "effect/crypto/SecureRandom"; import * as Schema from "effect/Schema"; @@ -99,7 +100,7 @@ const QueueTool = Tool.make("t3_queue", { beforeRunId: Schema.optional(Schema.NullOr(RunId)), targetRunId: Schema.optional(RunId), cursor: Schema.optional(NonNegativeInt), - limit: Schema.optional(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 100 }))), + limit: Schema.optional(clampedPositiveInt(100)), }), success: Schema.Union([ Schema.Struct({ items: Schema.Array(queueEntry), nextCursor: Schema.NullOr(NonNegativeInt) }), @@ -231,6 +232,7 @@ const ScheduledTaskRunTool = Tool.make("run_scheduled_task_now", { threadId: ScheduledTask.fields.threadId, link: Schema.optional(Schema.String), lastRunStatus: ScheduledTask.fields.lastRunStatus, + lastRunError: ScheduledTask.fields.lastRunError, runCount: NonNegativeInt, nextRunAt: ScheduledTask.fields.nextRunAt, }), diff --git a/apps/server/src/mcp/toolkits/toolSearch/tools.ts b/apps/server/src/mcp/toolkits/toolSearch/tools.ts index 400fae20aedb2..74741ccca557d 100644 --- a/apps/server/src/mcp/toolkits/toolSearch/tools.ts +++ b/apps/server/src/mcp/toolkits/toolSearch/tools.ts @@ -1,6 +1,6 @@ import { OrchestratorMcpFailure } from "@t3tools/contracts"; import * as Schema from "effect/Schema"; -import { Tool, Toolkit } from "effect/ai"; +import { McpSchema, Tool, Toolkit } from "effect/ai"; import * as McpInvocationContext from "../../McpInvocationContext.ts"; import * as McpToolExposure from "../../McpToolExposure.ts"; @@ -17,6 +17,7 @@ const ToolSearchTool = Tool.make(McpToolExposure.TOOL_SEARCH, { dependencies: [McpInvocationContext.McpInvocationContext, McpToolExposure.McpToolExposure], }) .annotate(Tool.Readonly, true) - .annotate(Tool.Destructive, false); + .annotate(Tool.Destructive, false) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); export const ToolSearchToolkit = Toolkit.make(ToolSearchTool); diff --git a/apps/server/src/mcp/toolkits/worktree/tools.ts b/apps/server/src/mcp/toolkits/worktree/tools.ts index 18d6840d72291..ce9dd39f7938f 100644 --- a/apps/server/src/mcp/toolkits/worktree/tools.ts +++ b/apps/server/src/mcp/toolkits/worktree/tools.ts @@ -12,7 +12,7 @@ import { import * as Schema from "effect/Schema"; import * as ProjectService from "../../../project/ProjectService.ts"; import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts"; -import { Tool, Toolkit } from "effect/ai"; +import { McpSchema, Tool, Toolkit } from "effect/ai"; import * as McpInvocationContext from "../../McpInvocationContext.ts"; import * as WorktreeMcpService from "../../WorktreeMcpService.ts"; @@ -38,7 +38,8 @@ const WorktreeHandoffTool = Tool.make("t3_worktree_handoff", { .annotate(Tool.Readonly, false) .annotate(Tool.Destructive, true) .annotate(Tool.Idempotent, false) - .annotate(Tool.OpenWorld, true); + .annotate(Tool.OpenWorld, true) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); const WorktreeStatusTool = Tool.make("t3_worktree_status", { description: @@ -56,7 +57,8 @@ const WorktreeStatusTool = Tool.make("t3_worktree_status", { .annotate(Tool.Readonly, true) .annotate(Tool.Destructive, false) .annotate(Tool.Idempotent, true) - .annotate(Tool.OpenWorld, false); + .annotate(Tool.OpenWorld, false) + .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients); const WorktreeListTool = Tool.make("t3_worktree_list", { description: diff --git a/apps/server/src/observability/RpcInstrumentation.test.ts b/apps/server/src/observability/RpcInstrumentation.test.ts new file mode 100644 index 0000000000000..50617b51af749 --- /dev/null +++ b/apps/server/src/observability/RpcInstrumentation.test.ts @@ -0,0 +1,41 @@ +import { assert, it } from "@effect/vitest"; +import { WS_METHODS } from "@t3tools/contracts"; +import * as Effect from "effect/Effect"; +import * as Option from "effect/Option"; +import type * as Tracer from "effect/Tracer"; +import * as Headers from "effect/http/Headers"; +import * as Rpc from "effect/rpc/Rpc"; +import { RequestId } from "effect/rpc/RpcMessage"; + +import { RpcInstrumentation, rpcInstrumentationLayer } from "./RpcInstrumentation.ts"; + +it.effect("starts each RPC in its own trace, linked to the connection span", () => + Effect.gen(function* () { + const middleware = yield* RpcInstrumentation; + let rpcSpan: Tracer.Span | undefined; + const handler = Effect.gen(function* () { + rpcSpan = yield* Effect.currentSpan; + }).pipe(Effect.orDie, Effect.asVoid); + + const connection = yield* Effect.gen(function* () { + const span = yield* Effect.currentSpan; + yield* middleware(handler as never, { + client: new Rpc.ServerClient(0), + requestId: RequestId(1), + rpc: Rpc.make(WS_METHODS.reviewGetDiffPreview), + payload: {}, + headers: Headers.empty, + }); + return span; + }).pipe(Effect.withSpan("GET /ws"), Effect.orDie); + + assert.isDefined(rpcSpan); + assert.equal(rpcSpan!.name, "ws.rpc.review.getDiffPreview"); + assert.isTrue(Option.isNone(rpcSpan!.parent)); + assert.notEqual(rpcSpan!.traceId, connection.traceId); + assert.deepEqual( + rpcSpan!.links.map((link) => link.span.spanId), + [connection.spanId], + ); + }).pipe(Effect.provide(rpcInstrumentationLayer)), +); diff --git a/apps/server/src/observability/RpcInstrumentation.ts b/apps/server/src/observability/RpcInstrumentation.ts index 271f52865fe64..5e4205a094277 100644 --- a/apps/server/src/observability/RpcInstrumentation.ts +++ b/apps/server/src/observability/RpcInstrumentation.ts @@ -2,6 +2,7 @@ import { ORCHESTRATION_V2_WS_METHODS, WS_METHODS, type WsRpcGroup } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; import * as References from "effect/References"; import type * as RpcGroup from "effect/rpc/RpcGroup"; import * as RpcMiddleware from "effect/rpc/RpcMiddleware"; @@ -69,6 +70,7 @@ const RPC_AGGREGATES = { [WS_METHODS.peerLinksLink]: "peerLinks", [WS_METHODS.peerLinksUnlink]: "peerLinks", [WS_METHODS.peerLinksAnswerRequest]: "peerLinks", + [WS_METHODS.serverGetSettings]: "server", [WS_METHODS.serverUpdateSettings]: "server", [WS_METHODS.serverSearchAcpRegistry]: "server", [WS_METHODS.serverPrepareAcpRegistryAgent]: "server", @@ -262,13 +264,25 @@ export const rpcInstrumentationLayer = Layer.succeed(RpcInstrumentation)((effect if (RPC_METHODS_WITH_TRACING_DISABLED.has(method)) { return measured.pipe(Effect.provideService(References.TracerEnabled, false)); } - return measured.pipe( - Effect.withSpan(`${RPC_SPAN_PREFIX}.${method}`, { - attributes: { - ...DEFAULT_RPC_SPAN_ATTRIBUTES, - "rpc.method": method, - "rpc.aggregate": RPC_AGGREGATES[method as WsRpcMethod], - }, - }), + // The ambient span is the socket's `GET /ws` request, open for the whole connection. Parenting + // every call to it grows one trace past the collector's per-trace limit, which then drops it. + return Effect.currentParentSpan.pipe( + Effect.option, + Effect.flatMap((connection) => + measured.pipe( + Effect.withSpan(`${RPC_SPAN_PREFIX}.${method}`, { + root: true, + links: Option.match(connection, { + onNone: () => [], + onSome: (span) => [{ span, attributes: {} }], + }), + attributes: { + ...DEFAULT_RPC_SPAN_ATTRIBUTES, + "rpc.method": method, + "rpc.aggregate": RPC_AGGREGATES[method as WsRpcMethod], + }, + }), + ), + ), ); }); diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts index b7808ef897ee7..d71b0f7b5e2b8 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts @@ -1961,7 +1961,15 @@ describe("AcpAdapterV2", () => { scenario: "OMP unrelated form uses generic mapping", settings: OMP_SETTINGS, schema: { type: "object", properties: { name: { type: "string" } } }, - questions: [{ id: "name", header: "Question 1", question: "Your answer?", options: [] }], + questions: [ + { + id: "name", + header: "Question 1", + question: "Your answer?", + options: [], + required: false, + }, + ], answers: { name: "Ada" }, result: { action: "accept", content: { name: "Ada" } }, }, @@ -2004,7 +2012,15 @@ describe("AcpAdapterV2", () => { { scenario: "falls back for an unrelated form", schema: { type: "object", properties: { name: { type: "string" } } }, - questions: [{ id: "name", header: "Question 1", question: "Your answer?", options: [] }], + questions: [ + { + id: "name", + header: "Question 1", + question: "Your answer?", + options: [], + required: false, + }, + ], answers: { name: "Ada" }, result: { action: "accept", content: { name: "Ada" } }, }, @@ -2456,12 +2472,7 @@ describe("AcpAdapterV2", () => { }, stopReason === "error", ); - const expectedStatus = - stopReason === "error" - ? "failed" - : stopReason === "cancelled" - ? "cancelled" - : "completed"; + const expectedStatus = stopReason === "cancelled" ? "cancelled" : "failed"; yield* reportNextPromptStopped(stopReason); assertReinjectedInstructions(yield* runTurn("Start work.", expectedStatus), "Start work."); assertReinjectedInstructions( @@ -3080,6 +3091,131 @@ describe("AcpAdapterV2", () => { }).pipe(Effect.provide(layerTest)), ); + it.live("replaces and removes identified ACP plans across turns", () => + Effect.gen(function* () { + const instanceId = ProviderInstanceId.make("acp-cross-turn-plans"); + const threadId = ThreadId.make("acp-cross-turn-plans"); + let notify: + | Parameters[0] + | undefined; + let ordinal = 0; + const adapter = yield* makeAcpAdapterV2({ + instanceId, + selfInvocation: yield* resolveSelfInvocation(), + flavor: { + driver: ACP_TEST_DRIVER, + capabilities: AcpProviderCapabilitiesV2, + makeRuntime: makeMockRuntime({ + childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner, + mockAgentPath: yield* bundledMockAgentPath, + wrapRuntime: (runtime) => ({ + ...runtime, + handleSessionUpdate: (handler) => + Effect.sync(() => { + notify = handler; + }).pipe(Effect.andThen(runtime.handleSessionUpdate(handler))), + prompt: () => + Effect.gen(function* () { + ordinal++; + const send = (update: EffectAcpSchema.SessionUpdate) => + notify!({ sessionId: "mock-session-1", update }); + const plan = (id: string, text: string) => + send({ + sessionUpdate: "plan_update", + plan: { + type: "items", + planId: id, + entries: [{ content: text, priority: "medium", status: "pending" }], + }, + }); + yield* send({ sessionUpdate: "state_update", state: "running" }); + if (ordinal === 1) { + yield* plan("control", "First"); + yield* plan("control", "Replacement"); + yield* send({ sessionUpdate: "plan_removed", planId: "control" }); + yield* plan("later-remove", "Remove on next turn"); + yield* plan("later-replace", "Old contents"); + } else { + yield* send({ sessionUpdate: "plan_removed", planId: "later-remove" }); + yield* plan("later-replace", "New contents"); + } + yield* send({ + sessionUpdate: "plan", + entries: [{ content: "Anonymous plan", priority: "medium", status: "pending" }], + }); + yield* send({ + sessionUpdate: "state_update", + state: "idle", + stopReason: "end_turn", + }); + return { stopReason: "end_turn" as const }; + }), + }), + }), + }, + }); + const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ + runtimeMode: "full-access", + interactionMode: "default", + cwd: process.cwd(), + }); + const modelSelection = { instanceId, model: "default" } as const; + const runtime = yield* adapter.openSession({ + threadId, + providerSessionId: ProviderSessionId.make("acp-cross-turn-plans"), + modelSelection, + runtimePolicy, + }); + const providerThread = yield* runtime.ensureThread({ + threadId, + modelSelection, + runtimePolicy, + }); + const now = yield* DateTime.now; + const run = (ordinal: number) => + Effect.gen(function* () { + yield* runtime.startTurn( + makeTurnInput({ threadId, providerThread, instanceId, runtimePolicy, now, ordinal }), + ); + return Array.from( + yield* runtime.events.pipe( + Stream.takeUntil((event) => event.type === "turn.terminal"), + Stream.runCollect, + ), + ).flatMap((event) => (event.type === "plan.updated" ? [event.plan] : [])); + }); + const first = yield* run(1); + assert.equal(first.length, 6); + assert.equal(first[0]!.id, first[1]!.id); + assert.equal(first[1]!.id, first[2]!.id); + assert.equal(first[1]!.kind, "todo_list"); + assert.deepEqual(first[1]!.kind === "todo_list" ? first[1]!.steps.map((s) => s.text) : [], [ + "Replacement", + ]); + assert.equal(first[2]!.status, "superseded"); + const second = yield* run(2); + assert.equal(second.length, 3); + assert.notEqual(first[5]!.id, second[2]!.id); + const removed = second.find((p) => p.id === first[3]!.id && p.status === "superseded"); + const replacement = second.find( + (p) => p.kind === "todo_list" && p.steps.some((s) => s.text === "New contents"), + ); + assert.isDefined( + removed, + "plan_removed on a later turn must supersede the existing session plan", + ); + assert.equal(removed?.runId, first[3]!.runId); + assert.equal(replacement?.runId, first[4]!.runId); + assert.equal(removed?.nodeId, first[3]!.nodeId); + assert.equal(replacement?.nodeId, first[4]!.nodeId); + assert.equal( + replacement?.id, + first[4]!.id, + "the same session plan must be replaced, not duplicated", + ); + }).pipe(Effect.provide(layerTest), Effect.scoped), + ); + it.effect("starts a new replay message after ACP v2 plan boundaries", () => Effect.gen(function* () { const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; @@ -3583,14 +3719,14 @@ describe("AcpAdapterV2", () => { "cognition.ai/subagent_context": { parentAgentId: "child-a" }, }, }, - { - sessionUpdate: "agent_message_chunk", - content: { type: "text", text: "ONE" }, + ...["O", "N", "E"].map((text) => ({ + sessionUpdate: "agent_message_chunk" as const, + content: { type: "text" as const, text }, _meta: { "cognition.ai/streamingMessageId": "child-result", "cognition.ai/subagent_context": { parentAgentId: "child-a" }, }, - }, + })), { sessionUpdate: "tool_call", toolCallId: "child-later", @@ -3743,6 +3879,15 @@ describe("AcpAdapterV2", () => { (item) => item.threadId === task?.childThreadId && item.type === "assistant_message", ); assert.equal(new Set(childAnswers.map((item) => item.ordinal)).size, 2); + // Later chunks coalesce; the subagent's completion flushes the final text. + assert.deepEqual( + childAnswers.flatMap((item) => + item.type === "assistant_message" && item.text !== "Checking the code." + ? [item.text] + : [], + ), + ["O", "ONE"], + ); const parentTools = items.filter( (item) => item.threadId === threadId && item.type === "dynamic_tool", ); @@ -3751,6 +3896,115 @@ describe("AcpAdapterV2", () => { }).pipe(Effect.provide(layerTest)), ); + it.effect("emits coalesced subagent text before a failed turn's terminal", () => + Effect.gen(function* () { + const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; + const path = yield* Path.Path; + const mockAgentPath = yield* path.fromFileUrl( + new URL("../../../scripts/acp-mock-agent.ts", import.meta.url), + ); + type Runtime = AcpSessionRuntime.AcpSessionRuntime["Service"]; + let handler: Parameters[0] | undefined; + const instanceId = ProviderInstanceId.make("devin-failed-flush"); + const adapter = yield* makeAcpAdapterV2({ + instanceId, + selfInvocation: yield* resolveSelfInvocation(), + flavor: { + driver: ACP_TEST_DRIVER, + capabilities: AcpProviderCapabilitiesV2, + normalizeSessionUpdate: normalizeDevinSessionUpdate, + normalizeToolCall: normalizeDevinToolCall, + extractSubagentUpdate: extractDevinSubagentUpdate, + makeRuntime: makeMockRuntime({ + childProcessSpawner, + mockAgentPath, + wrapRuntime: (runtime) => ({ + ...runtime, + handleSessionUpdate: (next) => + Effect.sync(() => { + handler = next; + }).pipe(Effect.andThen(runtime.handleSessionUpdate(next))), + prompt: () => + Effect.gen(function* () { + const updates = [ + { + sessionUpdate: "tool_call_update", + toolCallId: "child-a", + status: "in_progress", + _meta: { + "cognition.ai/subagent_started": { + agentId: "child-a", + title: "Answer", + task: "Reply ONE.", + }, + }, + }, + ...["O", "N", "E"].map((text) => ({ + sessionUpdate: "agent_message_chunk" as const, + content: { type: "text" as const, text }, + _meta: { + "cognition.ai/streamingMessageId": "child-result", + "cognition.ai/subagent_context": { parentAgentId: "child-a" }, + }, + })), + ] satisfies Array; + for (const update of updates) + yield* handler!({ sessionId: "mock-session-1", update }); + return yield* new EffectAcpErrors.AcpRequestError({ + code: -32603, + errorMessage: "prompt failed", + }); + }), + }), + }), + }, + }); + const threadId = ThreadId.make("devin-failed-flush-parent"); + const modelSelection = { instanceId, model: "default" }; + const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ + runtimeMode: "full-access", + interactionMode: "default", + cwd: process.cwd(), + }); + const runtime = yield* adapter.openSession({ + threadId, + providerSessionId: ProviderSessionId.make("devin-failed-flush-session"), + modelSelection, + runtimePolicy, + }); + const providerThread = yield* runtime.ensureThread({ + threadId, + modelSelection, + runtimePolicy, + }); + yield* runtime.startTurn( + makeTurnInput({ + threadId, + providerThread, + instanceId, + runtimePolicy, + now: yield* DateTime.now, + }), + ); + const events = Array.from( + yield* runtime.events.pipe( + Stream.takeUntil((event) => event.type === "turn.terminal"), + Stream.runCollect, + ), + ); + const terminal = events.at(-1); + assert.equal(terminal?.type === "turn.terminal" ? terminal.status : undefined, "failed"); + const childText = events.flatMap((event) => + event.type === "turn_item.updated" && + event.turnItem.type === "assistant_message" && + event.turnItem.threadId !== threadId + ? [event.turnItem.text] + : [], + ); + assert.equal(childText.at(-1), "ONE"); + }).pipe(Effect.provide(layerTest), Effect.scoped), + ); + it.effect("routes interleaved Copilot subagent output into subagent threads", () => Effect.gen(function* () { const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; @@ -5639,6 +5893,22 @@ describe("AcpAdapterV2", () => { ], optionId: "allow-once", }, + { + label: "answers Always allow with the agent's option id unchanged when it has spaces", + options: [ + { optionId: " opaque-choice ", name: "Allow once", kind: "allow_once" }, + { optionId: "reject-once", name: "Reject", kind: "reject_once" }, + ], + optionId: " opaque-choice ", + }, + { + label: "answers Always allow with a whitespace-only option id instead of cancelling", + options: [ + { optionId: " ", name: "Allow once", kind: "allow_once" }, + { optionId: "reject-once", name: "Reject", kind: "reject_once" }, + ], + optionId: " ", + }, ] as const)("$label", ({ options, optionId }) => Effect.gen(function* () { const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; @@ -9191,9 +9461,9 @@ describe("AcpAdapterV2", () => { }).pipe(Effect.provide(layerTest)), ); - it.effect( - "staggered pre-settle completion keeps midTurn marks until last background task ends post-finalize", - () => + it.effect.each(["unpolled", "polled"] as const)( + "staggered pre-settle completion waits for native wake output when task B is %s", + (taskB) => Effect.gen(function* () { const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; const idAllocator = yield* IdAllocator.IdAllocatorV2; @@ -9345,6 +9615,16 @@ describe("AcpAdapterV2", () => { 0, "pre-settle unhandled completion must not offer while the turn is active", ); + if (taskB === "polled") { + yield* sessionUpdateHandler!({ + sessionId: "mock-session-1", + update: { + sessionUpdate: "tool_call_update", + toolCallId: "tool-call-monitor-b", + status: "completed", + }, + }); + } yield* Deferred.succeed(promptGate, { stopReason: "end_turn" }); const providerTurnId = idAllocator.derive.providerTurn({ @@ -9362,35 +9642,44 @@ describe("AcpAdapterV2", () => { assert.lengthOf( continuationRequests, 0, - "finalize must not offer while B is still running", + "finalize must not offer without native wake output", ); - const bEndNotice = { + if (taskB === "unpolled") { + const bEndNotice = { + sessionId: "mock-session-1", + update: { + sessionUpdate: "user_message_chunk" as const, + content: { + type: "text" as const, + text: 'Monitor "task-monitor-b" ended: [monitor ended: exit 0]', + }, + }, + }; + assert.isFalse( + acpPostSettleWakeEvidence(bEndNotice, { + extractBackgroundToolMutation: (text) => + text.includes('Monitor "task-monitor-b" ended') + ? [{ taskId: "task-monitor-b", status: "completed", appendOutput: "" }] + : [], + }), + "mutation-only end-notice must not count as post-settle wake evidence", + ); + yield* sessionUpdateHandler!(bEndNotice); + assert.lengthOf(continuationRequests, 0, "a task end notice alone cannot start a wake"); + } + assert.isTrue(yield* runtime.hasPendingBackgroundWork!); + yield* sessionUpdateHandler!({ sessionId: "mock-session-1", update: { - sessionUpdate: "user_message_chunk" as const, - content: { - type: "text" as const, - text: 'Monitor "task-monitor-b" ended: [monitor ended: exit 0]', - }, + sessionUpdate: "agent_message_chunk", + content: { type: "text", text: "Both background tasks finished." }, }, - }; - assert.isFalse( - acpPostSettleWakeEvidence(bEndNotice, { - extractBackgroundToolMutation: (text) => - text.includes('Monitor "task-monitor-b" ended') - ? [{ taskId: "task-monitor-b", status: "completed", appendOutput: "" }] - : [], - }), - "mutation-only end-notice must not count as post-settle wake evidence", - ); - yield* sessionUpdateHandler!(bEndNotice); - yield* Effect.yieldNow; - yield* Effect.yieldNow; + }); assert.lengthOf( continuationRequests, 1, - "exactly one continuation when the last running task ends post-finalize with kept midTurn marks", + "the native report must start exactly one continuation", ); }).pipe(Effect.provide(layerTest)), ); @@ -9917,8 +10206,6 @@ describe("AcpAdapterV2", () => { | undefined; const promptStarted = yield* Deferred.make(); const promptGate = yield* Deferred.make(); - const promptHeld = yield* Deferred.make(); - const finalizeScheduled = yield* Deferred.make(); const continuationOffered = yield* Deferred.make(); const continuationRequests: Array = []; @@ -9936,12 +10223,6 @@ describe("AcpAdapterV2", () => { const adapter = yield* makeAcpAdapterV2({ instanceId, selfInvocation, - testHooks: { - afterPromptSettledWithBackgroundWork: () => - Deferred.succeed(promptHeld, undefined).pipe(Effect.asVoid), - onDeferredFinalizeScheduled: (debounce) => - Deferred.succeed(finalizeScheduled, debounce).pipe(Effect.asVoid), - }, flavor: { driver: ACP_TEST_DRIVER, capabilities: AcpProviderCapabilitiesV2, @@ -10007,11 +10288,15 @@ describe("AcpAdapterV2", () => { runtimePolicy, }); const now = yield* DateTime.now; - yield* runtime - .startTurn( - makeTurnInput({ threadId, providerThread, instanceId, runtimePolicy, now, ordinal: 1 }), - ) - .pipe(Effect.forkScoped); + const turnInput = makeTurnInput({ + threadId, + providerThread, + instanceId, + runtimePolicy, + now, + ordinal: 1, + }); + yield* runtime.startTurn(turnInput).pipe(Effect.forkScoped); yield* Deferred.await(promptStarted); assert.isDefined(sessionUpdateHandler); assert.isDefined(taskCompleted); @@ -10045,6 +10330,11 @@ describe("AcpAdapterV2", () => { yield* complete("early"); assert.lengthOf(continuationRequests, 0); + yield* update({ + sessionUpdate: "agent_message_chunk", + content: { type: "text", text: "EARLY_SUCCESS_DONE" }, + }); + let backgroundItem: ProviderAdapter.ProviderAdapterV2Event | undefined; let checkpoint = 0; const assertCommand = Effect.fnUntraced(function* (expectedStatus: string, output: string) { @@ -10070,9 +10360,19 @@ describe("AcpAdapterV2", () => { } }); yield* Deferred.succeed(promptGate, { stopReason: "end_turn" }); - yield* Deferred.await(promptHeld); + while (true) { + const event = yield* Queue.take(events); + if ( + event.type === "turn_item.updated" && + event.turnItem.nativeItemRef?.nativeId === "early" + ) + backgroundItem = event; + if (event.type !== "turn.terminal") continue; + assert.equal(event.status, "completed", "the running blocker does not hold the root"); + break; + } yield* assertCommand(status, "EARLY_BG_DONE\n"); - assert.lengthOf(continuationRequests, 0, "the other command still holds the root open"); + assert.lengthOf(continuationRequests, 0, "the running blocker defers the wake"); const replayAck = () => update({ @@ -10100,16 +10400,37 @@ describe("AcpAdapterV2", () => { } } yield* complete("blocker", { exit_code: 0, output: "BLOCKER_DONE\n" }); - yield* TestClock.adjust(yield* Deferred.await(finalizeScheduled)); while (true) { const event = yield* Queue.take(events); - if (event.type !== "turn.terminal") continue; - assert.equal(event.status, "completed"); + if (event.type !== "turn_item.updated") continue; + if (event.turnItem.nativeItemRef?.nativeId !== "blocker") continue; + if (event.turnItem.status !== "completed") continue; + assert.equal(event.turnItem.runId, turnInput.runId, "the row stays on its run"); + if (event.turnItem.type === "command_execution") { + assert.equal(event.turnItem.output, "BLOCKER_DONE\n"); + } break; } - yield* Deferred.await(continuationOffered); yield* complete("early"); yield* complete("blocker"); + assert.lengthOf( + continuationRequests, + 0, + "a task completion without native wake output must not replace the root answer", + ); + assert.isTrue(yield* runtime.hasPendingBackgroundWork!); + + yield* update({ + sessionUpdate: "agent_thought_chunk", + content: { type: "text", text: "Preparing the background report" }, + }); + yield* complete("early"); + assert.lengthOf(continuationRequests, 0, "reasoning alone is not a result-bearing wake"); + yield* update({ + sessionUpdate: "agent_message_chunk", + content: { type: "text", text: "LATE_NATIVE_ANSWER" }, + }); + yield* Deferred.await(continuationOffered); assert.lengthOf( continuationRequests, 1, @@ -10118,6 +10439,243 @@ describe("AcpAdapterV2", () => { }).pipe(Effect.provide(layerTest)), ); + // A dev server started as a background shell never exits, so it must not hold the root turn. + const startGrokBackgroundShellTurn = Effect.fnUntraced(function* (name: string) { + const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; + const path = yield* Path.Path; + const selfInvocation = yield* resolveSelfInvocation(); + const mockAgentPath = yield* path.fromFileUrl( + new URL("../../../scripts/acp-mock-agent.ts", import.meta.url), + ); + type RuntimeService = AcpSessionRuntime.AcpSessionRuntime["Service"]; + let sessionUpdateHandler: Parameters[0] | undefined; + let taskCompleted: + | ((payload: unknown) => Effect.Effect) + | undefined; + const promptStarted = yield* Deferred.make(); + const promptGate = yield* Deferred.make(); + const promptHeld = yield* Deferred.make(); + const finalizeScheduled = yield* Deferred.make(); + const instanceId = ProviderInstanceId.make("acp-test"); + const grokFlavor = makeGrokAcpAdapterFlavor( + { + instanceId, + settings: DEFAULT_GROK_SETTINGS, + environment: {}, + hostPlatform: yield* HostProcess.Platform, + selfInvocation, + }, + yield* HostProcess.HomeDirectory, + ); + const adapter = yield* makeAcpAdapterV2({ + instanceId, + selfInvocation, + testHooks: { + afterPromptSettledWithBackgroundWork: () => + Deferred.succeed(promptHeld, undefined).pipe(Effect.asVoid), + onDeferredFinalizeScheduled: (debounce) => + Deferred.succeed(finalizeScheduled, debounce).pipe(Effect.asVoid), + }, + flavor: { + driver: ACP_TEST_DRIVER, + capabilities: AcpProviderCapabilitiesV2, + enablePostSettleContinuation: true, + deferFinalizeForBackgroundWork: grokFlavor.deferFinalizeForBackgroundWork!, + normalizeToolCall: grokFlavor.normalizeToolCall!, + extractBackgroundTaskId: grokFlavor.extractBackgroundTaskId!, + isBackgroundCommandTool: grokFlavor.isBackgroundCommandTool!, + isPersistentBackgroundTool: grokFlavor.isPersistentBackgroundTool!, + registerExtensions: ({ runtime, applyBackgroundTaskMutation }) => + registerXAiBackgroundTaskTracking(runtime, applyBackgroundTaskMutation), + makeRuntime: makeMockRuntime({ + childProcessSpawner, + mockAgentPath, + wrapRuntime: (runtime) => ({ + ...runtime, + handleSessionUpdate: (handler) => + Effect.sync(() => { + sessionUpdateHandler = handler; + }).pipe(Effect.andThen(runtime.handleSessionUpdate(handler))), + handleExtNotification: (method, schema, handler) => + Effect.sync(() => { + if (method === "x.ai/task_completed") { + taskCompleted = (payload) => handler(Schema.decodeUnknownSync(schema)(payload)); + } + }).pipe(Effect.andThen(runtime.handleExtNotification(method, schema, handler))), + prompt: () => + Deferred.succeed(promptStarted, undefined).pipe( + Effect.andThen(Deferred.await(promptGate)), + ), + }), + }), + }, + }); + const threadId = ThreadId.make(`thread-acp-${name}`); + const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ + runtimeMode: "full-access", + interactionMode: "default", + cwd: process.cwd(), + }); + const modelSelection = { instanceId, model: "default" } as const; + const runtime = yield* adapter.openSession({ + threadId, + providerSessionId: ProviderSessionId.make(`provider-session-acp-${name}`), + modelSelection, + runtimePolicy, + }); + const events = yield* Queue.unbounded(); + yield* runtime.events.pipe( + Stream.runForEach((event) => Queue.offer(events, event)), + Effect.forkScoped, + ); + const providerThread = yield* runtime.ensureThread({ + threadId, + modelSelection, + runtimePolicy, + }); + const turnInput = makeTurnInput({ + threadId, + providerThread, + instanceId, + runtimePolicy, + now: yield* DateTime.now, + }); + yield* runtime.startTurn(turnInput).pipe(Effect.forkScoped); + yield* Deferred.await(promptStarted); + assert.isDefined(sessionUpdateHandler); + assert.isDefined(taskCompleted); + const update = (update: EffectAcpSchema.SessionUpdate) => + sessionUpdateHandler!({ sessionId: "mock-session-1", update }); + const complete = (taskId: string, snapshot?: { output?: string; exit_code?: number }) => + taskCompleted!({ + sessionId: "mock-session-1", + update: { sessionUpdate: "task_completed", task_id: taskId, task_snapshot: snapshot }, + }); + const startDevServer = Effect.gen(function* () { + yield* update({ + sessionUpdate: "tool_call", + toolCallId: "call-dev-server", + title: "run_terminal_command", + kind: "execute", + status: "in_progress", + rawInput: { command: "vp run dev" }, + }); + yield* update({ + sessionUpdate: "tool_call_update", + toolCallId: "call-dev-server", + status: "completed", + rawOutput: { + type: "BackgroundTaskStarted", + task_id: "call-dev-server", + task_type: "bash", + status: "running", + command: "vp run dev", + }, + }); + }); + /** Settles the prompt and returns the dev server rows emitted up to the turn terminal. */ + const settle = Effect.gen(function* () { + const rows: Array = []; + const takeTerminal = Effect.gen(function* () { + while (true) { + const event = yield* Queue.take(events); + if ( + event.type === "turn_item.updated" && + event.turnItem.nativeItemRef?.nativeId === "call-dev-server" + ) + rows.push(event.turnItem); + if (event.type === "turn.terminal") return event.status; + } + }); + yield* Deferred.succeed(promptGate, { stopReason: "end_turn" }); + const status = yield* Effect.raceAll([ + takeTerminal, + Deferred.await(finalizeScheduled).pipe( + Effect.flatMap(TestClock.adjust), + Effect.andThen(takeTerminal), + ), + // The hold never ends on its own, so report it instead of waiting forever. + Deferred.await(promptHeld).pipe(Effect.as("held open by the background shell")), + ]); + return { status, rows }; + }); + /** Dev server rows emitted before a usage update fences them. */ + const takeRows = Effect.gen(function* () { + yield* update({ sessionUpdate: "usage_update", used: 10, size: 100 }); + const rows: Array = []; + while (true) { + const event = yield* Queue.take(events); + if (event.type === "provider_thread.updated") return rows; + if ( + event.type === "turn_item.updated" && + event.turnItem.nativeItemRef?.nativeId === "call-dev-server" + ) + rows.push(event.turnItem); + } + }); + return { update, complete, startDevServer, settle, takeRows, runId: turnInput.runId }; + }); + + it.effect("completes a settled Grok turn while a background shell keeps running", () => + Effect.gen(function* () { + const harness = yield* startGrokBackgroundShellTurn("grok-dev-server"); + yield* harness.startDevServer; + yield* harness.update({ + sessionUpdate: "agent_message_chunk", + content: { type: "text", text: "The dev server is running." }, + }); + assert.equal((yield* harness.settle).status, "completed"); + }).pipe(Effect.provide(layerTest), Effect.scoped), + ); + + it.effect("keeps a streaming Grok background shell running on its run after settling", () => + Effect.gen(function* () { + const harness = yield* startGrokBackgroundShellTurn("grok-dev-server-progress"); + yield* harness.startDevServer; + // Progress replaces the BackgroundTaskStarted output that carried the task identity. + yield* harness.update({ + sessionUpdate: "tool_call_update", + toolCallId: "call-dev-server", + status: "in_progress", + rawOutput: { type: "Text", text: "VITE ready\n" }, + }); + const settled = yield* harness.settle; + assert.equal(settled.status, "completed"); + assert.notInclude( + settled.rows.map((row) => row.status), + "completed", + "the running dev server was finalized as a foreground tool", + ); + + yield* harness.update({ + sessionUpdate: "tool_call_update", + toolCallId: "call-dev-server", + status: "in_progress", + rawOutput: { type: "Text", text: "VITE ready\nhmr update\n" }, + }); + yield* harness.complete("call-dev-server", { exit_code: 0, output: "DEV_SERVER_DONE\n" }); + const row = (yield* harness.takeRows).at(-1); + assert.equal(row?.status, "completed"); + assert.equal(row?.runId, harness.runId); + assert.equal(row?.type === "command_execution" ? row.output : undefined, "DEV_SERVER_DONE\n"); + }).pipe(Effect.provide(layerTest), Effect.scoped), + ); + + it.effect("applies a richer Grok background shell failure after a sparse completion", () => + Effect.gen(function* () { + const harness = yield* startGrokBackgroundShellTurn("grok-dev-server-failure"); + yield* harness.startDevServer; + assert.equal((yield* harness.settle).status, "completed"); + + yield* harness.complete("call-dev-server"); + yield* harness.complete("call-dev-server", { exit_code: 1, output: "EADDRINUSE\n" }); + const row = (yield* harness.takeRows).at(-1); + assert.equal(row?.status, "failed"); + assert.equal(row?.runId, harness.runId); + assert.equal(row?.type === "command_execution" ? row.output : undefined, "EADDRINUSE\n"); + }).pipe(Effect.provide(layerTest), Effect.scoped), + ); + it.live("durably poisons start and resume when required hard teardown is unavailable", () => Effect.gen(function* () { const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; diff --git a/apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts index 70f45bc8a46c0..ee82cc06de836 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpRegistryAdapterV2.test.ts @@ -234,6 +234,7 @@ const openClaudeReplaySession = Effect.fn("openClaudeReplaySession")(function* ( }).pipe(Effect.andThen(Queue.offer(offered, undefined)), Effect.asVoid), }; const instanceId = ProviderInstanceId.make("claude-lifecycle"); + const outgoing = yield* Queue.unbounded(); let onTermination: NonNullable< AcpSessionRuntime.AcpSessionRuntimeOptions["onTermination"] > = () => Effect.die("Runtime has not started"); @@ -255,7 +256,17 @@ const openClaudeReplaySession = Effect.fn("openClaudeReplaySession")(function* ( ...(continuationEnabled ? { continuationRequests } : {}), makeRuntime: (runtimeInput: Parameters[0]) => { onTermination = runtimeInput.onTermination; - return replayRuntime(runtimeInput); + return replayRuntime({ + ...runtimeInput, + protocolLogging: { + logIncoming: false, + logOutgoing: true, + logger: (event) => + event.stage === "raw" && typeof event.payload === "string" + ? Queue.offer(outgoing, JSON.parse(event.payload)).pipe(Effect.asVoid) + : Effect.void, + }, + }); }, }; const adapter = yield* makeAcpRegistryAdapterV2(adapterOptions).pipe( @@ -365,6 +376,7 @@ const openClaudeReplaySession = Effect.fn("openClaudeReplaySession")(function* ( terminal, offers, offered, + outgoing, terminate: () => onTermination( new EffectAcpErrors.AcpTransportError({ @@ -1259,7 +1271,7 @@ describe("AcpRegistryAdapterV2", () => { }).pipe(Effect.provide(layerTest)), ); - it.effect("gives Copilot T3's MCP server over HTTP when it advertises HTTP", () => + it.effect("gives HTTP-capable agents T3's MCP server over HTTP", () => Effect.gen(function* () { const selfInvocation = yield* resolveSelfInvocation(); const mockAgentPath = yield* (yield* Path.Path).fromFileUrl( @@ -1345,6 +1357,14 @@ describe("AcpRegistryAdapterV2", () => { }; return { new: sent("session/new"), load: sent("session/load") }; }); + const plainHttp = (authorization: string) => [ + { + type: "http", + name: "t3-code", + url: endpoint, + headers: [{ name: "Authorization", value: authorization }], + }, + ]; const http = (authorization: string) => [ { type: "http", @@ -1389,6 +1409,30 @@ describe("AcpRegistryAdapterV2", () => { new: stdio("Bearer opened-token"), load: stdio("Bearer rotated-token"), }); + // Command Code cannot start the stdio bridge on Windows: it spawns MCP commands through + // cmd.exe, which splits the unquoted T3 executable path at the space. + const commandCode = { + source: "local", + commandPath: "C:\\tools\\cmdc.exe", + commandArgs: ["acp"], + }; + // Command Code is not Copilot: no deferred tool-exposure header. + assert.deepEqual(yield* sessionMcpServers(commandCode, { T3_ACP_MCP_HTTP: "1" }), { + new: plainHttp("Bearer opened-token"), + load: plainHttp("Bearer rotated-token"), + }); + assert.deepEqual(yield* sessionMcpServers(commandCode), { + new: stdio("Bearer opened-token"), + load: stdio("Bearer rotated-token"), + }); + // Any other local agent keeps the stdio bridge. + assert.deepEqual( + yield* sessionMcpServers( + { source: "local", commandPath: "C:\\tools\\node.exe", commandArgs: ["agent.mjs"] }, + { T3_ACP_MCP_HTTP: "1" }, + ), + { new: stdio("Bearer opened-token"), load: stdio("Bearer rotated-token") }, + ); }).pipe(Effect.provide(layerTest)), ); @@ -1857,6 +1901,102 @@ describe("AcpRegistryAdapterV2", () => { }).pipe(Effect.provide(layerTest)), ); + // Copilot wakes its root agent when a background subagent finishes, after the run settled. + it.effect("does not drop Copilot's root reply when a finished background agent wakes it", () => + Effect.gen(function* () { + const sessionId = "copilot-session"; + const sessionEvent = (type: string) => ({ + type: "emit_inbound", + frame: { + kind: "notification", + method: "github.com/copilot/sessionEvent", + params: { sessionId, type, data: {} }, + }, + }); + const update = (sessionUpdate: Record) => + claudeFrames.update(sessionId, sessionUpdate); + const session = yield* openClaudeReplaySession( + [ + { + type: "expect_outbound", + frame: { kind: "request", method: "initialize", params: "" }, + }, + { + type: "emit_inbound", + frame: { + kind: "response", + method: "initialize", + result: { + protocolVersion: 1, + agentInfo: { name: "Copilot", version: "1.0.112" }, + agentCapabilities: { loadSession: true }, + authMethods: [], + }, + }, + }, + { + type: "expect_outbound", + frame: { kind: "request", method: "session/new", params: "" }, + }, + { + type: "emit_inbound", + frame: { kind: "response", method: "session/new", result: { sessionId } }, + }, + { + type: "expect_outbound", + frame: { kind: "request", method: "session/prompt", params: "" }, + }, + sessionEvent("assistant.turn_start"), + update({ + sessionUpdate: "agent_message_chunk", + content: { type: "text", text: "Agents are still working." }, + }), + sessionEvent("assistant.idle"), + update({ sessionUpdate: "session_info_update", title: "after foreground idle" }), + // The background agent finishes; Copilot reads it and starts a new root turn. + sessionEvent("assistant.turn_start"), + update({ + sessionUpdate: "tool_call", + toolCallId: "read-agent-1", + title: "read_agent", + kind: "other", + status: "completed", + rawInput: { agent_id: "agent-1" }, + }), + update({ + sessionUpdate: "agent_message_chunk", + content: { type: "text", text: "Agent 1 finished" }, + }), + sessionEvent("assistant.idle"), + { + type: "emit_inbound", + frame: { + kind: "response", + method: "session/prompt", + result: { stopReason: "end_turn" }, + }, + }, + update({ sessionUpdate: "session_info_update", title: "after wake idle" }), + ], + undefined, + true, + "github-copilot-cli", + ); + yield* session.runtime.startTurn(session.turnInput(1)); + const foreground = yield* session.throughMarker("after foreground idle"); + assert.isDefined(foreground.find((event) => event.type === "turn.terminal")); + const wake = yield* session.throughMarker("after wake idle"); + assert.isTrue( + session.offers.length > 0 || JSON.stringify(wake).includes("Agent 1 finished"), + "the woken root reply is shown or opens a continuation run", + ); + yield* session.runtime.startTurn(session.turnInput(2, { continuation: true })); + const continuation = yield* session.until((event) => event.type === "turn.terminal"); + assert.include(JSON.stringify(continuation), "Agent 1 finished"); + assert.include(JSON.stringify(continuation.at(-1)), '"status":"completed"'); + }).pipe(Effect.provide(layerTest)), + ); + it.effect.each([ { kind: "approval", stopReason: "end_turn", status: "completed" }, { kind: "question", stopReason: "error", status: "failed" }, @@ -2242,6 +2382,103 @@ describe("AcpRegistryAdapterV2", () => { }).pipe(Effect.provide(layerTest)), ); + it.effect("approves Devin's plan without choosing a bypass option it lists first", () => + Effect.gen(function* () { + const session = yield* openClaudeReplaySession( + [ + ...claudeFrames.open.map((entry) => + entry.frame.method === "initialize" && entry.type === "emit_inbound" + ? { + ...entry, + frame: { + ...entry.frame, + result: { protocolVersion: 1, agentCapabilities: {}, authMethods: [] }, + }, + } + : entry, + ), + { + type: "expect_outbound", + frame: { kind: "request", method: "session/prompt", params: "" }, + }, + claudeFrames.update(CLAUDE_ROOT_SESSION, { + sessionUpdate: "tool_call", + toolCallId: "exit-plan", + title: "Exit plan mode", + kind: "switch_mode", + status: "pending", + }), + { + type: "emit_inbound", + frame: { + kind: "request", + method: "session/request_permission", + params: { + sessionId: CLAUDE_ROOT_SESSION, + toolCall: { toolCallId: "exit-plan" }, + // Devin gives both plan approvals allow_once and does not promise their order. + options: [ + { + optionId: "plan_bypass", + name: "Yes, implement plan and bypass permissions", + kind: "allow_once", + }, + { + optionId: "plan_accept_edits", + name: "Yes, implement plan and accept edits", + kind: "allow_once", + }, + { optionId: "reject_once", name: "No, plan needs changes", kind: "reject_once" }, + ], + }, + }, + }, + { + type: "expect_outbound", + frame: { + kind: "response", + method: "session/request_permission", + result: { outcome: { outcome: "selected", optionId: "" } }, + }, + }, + { + type: "emit_inbound", + frame: { + kind: "response", + method: "session/prompt", + result: { stopReason: "end_turn" }, + }, + }, + claudeFrames.closeRequest, + claudeFrames.closed, + ], + undefined, + false, + "devin", + ); + yield* session.runtime.startTurn(session.turnInput(1)).pipe(Effect.forkScoped); + const seen = yield* session.until( + (event) => + event.type === "runtime_request.updated" && event.runtimeRequest.status === "pending", + ); + const request = seen.at(-1)!; + if (request.type !== "runtime_request.updated") throw new Error("Missing request"); + yield* session.runtime.respondToRuntimeRequest({ + requestId: request.runtimeRequest.id, + decision: "accept", + }); + let answer: unknown; + while (answer === undefined) { + const frame = (yield* Queue.take(session.outgoing)) as { + readonly result?: { readonly outcome?: { readonly optionId?: string } }; + }; + answer = frame.result?.outcome; + } + assert.deepEqual(answer, { outcome: "selected", optionId: "plan_accept_edits" }); + assert.equal((yield* session.terminal).status, "completed"); + }).pipe(Effect.provide(layerTest), Effect.scoped), + ); + it.effect("keeps stopped child tools interrupted when a late output chunk arrives", () => Effect.gen(function* () { const { update } = claudeFrames; @@ -2685,6 +2922,42 @@ describe("AcpRegistryAdapterV2", () => { }).pipe(Effect.provide(layerTest)), ); + it.effect("writes a child's coalesced text before a restart resets it", () => + Effect.gen(function* () { + const { update, childState, idle, prompt } = claudeFrames; + const reply = (text: string) => + update("child-1", { + sessionUpdate: "agent_message_chunk", + messageId: "reply", + content: { type: "text", text }, + }); + const session = yield* openClaudeReplaySession([ + ...claudeFrames.open, + ...prompt(1), + ...claudeChild.slice(0, -1), + reply("Review "), + reply("done."), + // A working grandchild keeps the idle child from publishing (and flushing) its status. + childState("child-1", "grandchild", "running"), + childState(CLAUDE_ROOT_SESSION, "child-1", "idle"), + childState(CLAUDE_ROOT_SESSION, "child-1", "running"), + idle("end_turn"), + claudeFrames.closeRequest, + claudeFrames.closed, + ]); + yield* session.runtime.startTurn(session.turnInput(1)); + const events = yield* session.until((event) => event.type === "turn.terminal"); + assert.isTrue( + events.some( + (event) => + event.type === "message.updated" && + event.message.role === "assistant" && + event.message.text === "Review done.", + ), + ); + }).pipe(Effect.provide(layerTest), Effect.scoped), + ); + it.effect("fails active Claude work and releases its children when the process exits", () => Effect.gen(function* () { const session = yield* openClaudeReplaySession([ @@ -2755,6 +3028,65 @@ describe("AcpRegistryAdapterV2", () => { }).pipe(Effect.provide(layerTest)), ); + it.effect.each([ + { stopReason: "refusal", childUpdate: "state_update" }, + { stopReason: "max_tokens", childUpdate: "subagent_update" }, + ])( + "fails Claude's turn and child and closes its running tool when they stop with $stopReason via $childUpdate", + ({ stopReason, childUpdate }) => + Effect.gen(function* () { + const { update, idle, prompt } = claudeFrames; + const session = yield* openClaudeReplaySession([ + ...claudeFrames.open, + ...prompt(1), + ...claudeChild, + update(CLAUDE_ROOT_SESSION, { + sessionUpdate: "tool_call_update", + toolCallId: "root-tool", + name: "Bash", + title: "Read package", + kind: "execute", + status: "in_progress", + rawInput: { command: "cat package.json" }, + }), + update("child-1", { sessionUpdate: "state_update", state: "running" }), + childUpdate === "state_update" + ? update("child-1", { sessionUpdate: "state_update", state: "idle", stopReason }) + : update(CLAUDE_ROOT_SESSION, { + sessionUpdate: "subagent_update", + sessionId: "child-1", + state: { state: "idle", stopReason }, + }), + idle(stopReason), + ]); + yield* session.runtime.startTurn(session.turnInput(1)); + const events = yield* session.until((event) => event.type === "turn.terminal"); + const terminal = events.at(-1)!; + if (terminal.type !== "turn.terminal") throw new Error("Missing root terminal"); + assert.equal(terminal.status, "failed"); + assert.equal(terminal.failure?.code, stopReason); + const child = events.findLast((event) => event.type === "subagent.updated"); + if (child?.type !== "subagent.updated") throw new Error("Missing child update"); + assert.equal(child.subagent.status, "failed"); + assert.isNotNull(child.subagent.result, "the child shows why it stopped"); + assert.isFalse( + events.some( + (event) => + event.type === "message.updated" && + event.message.threadId === child.subagent.childThreadId && + event.message.role === "assistant", + ), + "the child's conversation has no reply it didn't send", + ); + const tool = events.findLast( + (event) => + event.type === "turn_item.updated" && event.turnItem.type === "command_execution", + ); + if (tool?.type !== "turn_item.updated") throw new Error("Missing tool update"); + assert.equal(tool.turnItem.status, "failed"); + }).pipe(Effect.provide(layerTest), Effect.scoped), + ); + it.effect("keeps Claude activity after a refused Stop and closes it on retry", () => Effect.gen(function* () { const gate = makeProviderReplayGate(["close-response", "queued-close-response"]); diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 92f0e4205b639..419b5a340389b 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -213,13 +213,14 @@ function makeClaudeTestAppThread(input: { readonly threadId: ThreadId; readonly providerThread: OrchestrationV2ProviderThread; readonly now: DateTime.Utc; + readonly title?: string; }): OrchestrationV2AppThread { return { createdBy: "user", creationSource: "web", id: input.threadId, projectId: ProjectId.make(`project-${input.threadId}`), - title: "Claude attachment test", + title: input.title ?? "Claude attachment test", providerInstanceId: ProviderInstanceId.make(ClaudeAdapterV2.CLAUDE_PROVIDER), modelSelection: CLAUDE_TEST_MODEL_SELECTION, runtimeMode: "full-access", @@ -252,6 +253,7 @@ function makeClaudeTestTurnInput(input: { readonly attachments: ProviderAdapter.ProviderAdapterV2TurnInput["message"]["attachments"]; readonly providerTurnOrdinal?: number; readonly nativeThreadHasTurns?: boolean; + readonly title?: string; readonly messageCreatedBy?: ProviderAdapter.ProviderAdapterV2TurnInput["message"]["createdBy"]; readonly messageCreationSource?: ProviderAdapter.ProviderAdapterV2TurnInput["message"]["creationSource"]; readonly modelSelection?: ModelSelection; @@ -755,6 +757,39 @@ describe("ClaudeAdapterV2 MCP query overrides", () => { }); }); +describe("ClaudeAdapterV2 SDK warnings", () => { + it("drops only the SDK's canUseTool shadowing warning while opening a query", async () => { + using emitWarning = vi.spyOn(process, "emitWarning").mockImplementation(() => {}); + for (const options of [ + { permissionMode: "default", allowedTools: [ClaudeAdapterV2.CLAUDE_T3_MCP_TOOL_WILDCARD] }, + { permissionMode: "bypassPermissions", allowDangerouslySkipPermissions: true }, + ] satisfies ReadonlyArray) { + const query = ClaudeAdapterV2.withoutClaudeCanUseToolShadowedWarning(() => + ClaudeSdk.query({ + prompt: "", + options: { + ...options, + canUseTool: async (_toolName, input) => ({ behavior: "allow", updatedInput: input }), + pathToClaudeCodeExecutable: "/nonexistent/claude", + }, + }), + ); + await query.next().catch(() => undefined); + } + ClaudeAdapterV2.withoutClaudeCanUseToolShadowedWarning(() => + process.emitWarning("unrelated", { code: "UNRELATED_WARNING" }), + ); + + assert.deepEqual( + emitWarning.mock.calls.map(([, options]) => + typeof options === "object" ? options.code : options, + ), + ["UNRELATED_WARNING"], + ); + assert.strictEqual(process.emitWarning, emitWarning); + }); +}); + describe("ClaudeAdapterV2 native protocol logging", () => { it("injects thread-scoped MCP configuration without logging the credential", () => { const threadId = ThreadId.make("thread-claude-mcp"); @@ -968,6 +1003,22 @@ describe("ClaudeAdapterV2 session permissions", () => { assert.equal(result.decisionClassification, "user_reject"); assert.equal(result.message, "User declined tool execution."); assert.equal(result.interrupt, undefined); + + // regression: #27854 bare feedback read to Claude as a tool error, not the user's words + const withFeedback = ClaudeAdapterV2.permissionResultFromDecision({ + toolName: "Bash", + decision: "decline", + toolInput: { command: "make" }, + toolUseID: "denied-build", + feedback: " Use the dry-run flag first. ", + }); + assert.equal(withFeedback.behavior, "deny"); + if (withFeedback.behavior !== "deny") return; + assert.equal(withFeedback.decisionClassification, "user_reject"); + assert.equal( + withFeedback.message, + "The user declined this tool call and said:\nUse the dry-run flag first.", + ); }); it("forces suggested permission updates to session scope", () => { @@ -1078,6 +1129,7 @@ describe("ClaudeAdapterV2 Auto-accept edits", () => { setModel: () => Effect.void, applyFlagSettings: () => Effect.void, setPermissionMode: () => Effect.void, + renameSession: () => Effect.void, backgroundTasks: () => Effect.succeed(false), mcpServerStatus: Effect.succeed([]), readMcpResource: () => Effect.die("unexpected MCP resource read"), @@ -1200,6 +1252,7 @@ describe("ClaudeAdapterV2 append-system-prompt-file launch argument", () => { setModel: () => Effect.void, applyFlagSettings: () => Effect.void, setPermissionMode: () => Effect.void, + renameSession: () => Effect.void, backgroundTasks: () => Effect.succeed(false), mcpServerStatus: Effect.succeed([]), readMcpResource: () => Effect.die("unexpected MCP resource read"), @@ -1252,6 +1305,36 @@ describe("ClaudeAdapterV2 append-system-prompt-file launch argument", () => { const appendSystemPrompt = (options: ClaudeAdapterV2.ClaudeAgentSdkQueryOptions | undefined) => (options?.systemPrompt as { readonly append?: string } | undefined)?.append; + // regression: #35594 append text starting with "--" was read as a missing value + it.effect("sends append launch text that starts with two hyphens to Claude", () => + Effect.scoped( + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-claude-append-cwd-" }); + const plain = yield* startTurnWithLaunchArgs("", cwd); + const { exit, opened } = yield* startTurnWithLaunchArgs( + '--append-system-prompt "-- Always answer in English"', + cwd, + ); + assert.isTrue(Exit.isSuccess(exit)); + assert.lengthOf(opened, 1); + assert.equal( + appendSystemPrompt(opened[0]), + `${appendSystemPrompt(plain.opened[0])}\n\n-- Always answer in English`, + ); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ), + ); + it.effect("sends append launch arguments to Claude after the T3 instructions", () => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; @@ -1458,6 +1541,7 @@ describe("ClaudeAdapterV2 provider installation gate", () => { setModel: () => Effect.void, applyFlagSettings: () => Effect.void, setPermissionMode: () => Effect.void, + renameSession: () => Effect.void, backgroundTasks: () => Effect.succeed(false), mcpServerStatus: Effect.succeed([]), readMcpResource: () => Effect.die("unexpected MCP resource read"), @@ -1645,6 +1729,7 @@ const captureSdkExecutablePaths = Effect.fn("captureSdkExecutablePaths")(functio setModel: () => Effect.void, applyFlagSettings: () => Effect.void, setPermissionMode: () => Effect.void, + renameSession: () => Effect.void, backgroundTasks: () => Effect.succeed(false), mcpServerStatus: Effect.succeed([]), readMcpResource: () => Effect.die("unexpected MCP resource read"), @@ -1838,6 +1923,7 @@ describe("ClaudeAdapterV2 resume compaction", () => { setModel: () => Effect.void, applyFlagSettings: () => Effect.void, setPermissionMode: () => Effect.void, + renameSession: () => Effect.void, backgroundTasks: () => Effect.succeed(false), mcpServerStatus: Effect.succeed([]), readMcpResource: () => Effect.die("unexpected MCP resource read"), @@ -2222,6 +2308,7 @@ describe("ClaudeAdapterV2 attachments", () => { setModel: () => Effect.void, applyFlagSettings: () => Effect.void, setPermissionMode: () => Effect.void, + renameSession: () => Effect.void, backgroundTasks: () => Effect.succeed(false), mcpServerStatus: Effect.succeed([]), readMcpResource: () => Effect.die("unexpected MCP resource read"), @@ -2763,6 +2850,7 @@ describe("ClaudeAdapterV2 native fork", () => { setModel: () => Effect.void, applyFlagSettings: () => Effect.void, setPermissionMode: () => Effect.void, + renameSession: () => Effect.void, backgroundTasks: () => Effect.succeed(false), mcpServerStatus: Effect.succeed([]), readMcpResource: () => Effect.die("unexpected MCP resource read"), @@ -3111,6 +3199,7 @@ describe("ClaudeAdapterV2 native session identity", () => { setModel: () => Effect.void, applyFlagSettings: () => Effect.void, setPermissionMode: () => Effect.void, + renameSession: () => Effect.void, backgroundTasks: () => Effect.succeed(false), mcpServerStatus: Effect.succeed([]), readMcpResource: () => Effect.die("unexpected MCP resource read"), @@ -3443,6 +3532,11 @@ describe("ClaudeAdapterV2 background wake turns", () => { readonly close?: (sdkMessages: Queue.Queue) => Effect.Effect; readonly interrupt?: Effect.Effect; readonly setPermissionMode?: ClaudeAdapterV2.ClaudeAgentSdkQuerySession["setPermissionMode"]; + readonly setModel?: ( + model: string, + offerAndWait: (message: SDKMessage) => Effect.Effect, + ) => Effect.Effect; + readonly renameSession?: ClaudeAdapterV2.ClaudeAgentSdkQuerySession["renameSession"]; readonly applyFlagSettings?: ClaudeAdapterV2.ClaudeAgentSdkQuerySession["applyFlagSettings"]; readonly stopTask?: ( taskId: string, @@ -3490,6 +3584,7 @@ describe("ClaudeAdapterV2 background wake turns", () => { const changes = yield* makeChangeSignal; const offeredMessages: Array = []; const permissionModeChanges: Array = []; + const renameCalls: Array = []; const liveSelectionChanges: Array = []; const continuationRequests: Array = []; @@ -3583,6 +3678,10 @@ describe("ClaudeAdapterV2 background wake turns", () => { }).pipe( Effect.andThen(() => options?.setPermissionMode?.(mode) ?? Effect.void), ), + renameSession: (title) => + Effect.sync(() => { + renameCalls.push(title); + }).pipe(Effect.andThen(() => options?.renameSession?.(title) ?? Effect.void)), backgroundTasks: options?.backgroundTasks ?? (() => Effect.succeed(false)), mcpServerStatus: Effect.succeed([]), readMcpResource: () => Effect.die("unexpected MCP resource read"), @@ -3606,7 +3705,9 @@ describe("ClaudeAdapterV2 background wake turns", () => { setModel: (model) => Effect.sync(() => { liveSelectionChanges.push({ model }); - }), + }).pipe( + Effect.andThen(() => options?.setModel?.(model, offerAndWait) ?? Effect.void), + ), applyFlagSettings: (settings) => Effect.sync(() => { liveSelectionChanges.push(settings); @@ -3617,6 +3718,10 @@ describe("ClaudeAdapterV2 background wake turns", () => { Effect.sync(() => { permissionModeChanges.push(mode); }).pipe(Effect.andThen(() => options?.setPermissionMode?.(mode) ?? Effect.void)), + renameSession: (title) => + Effect.sync(() => { + renameCalls.push(title); + }).pipe(Effect.andThen(() => options?.renameSession?.(title) ?? Effect.void)), backgroundTasks: options?.backgroundTasks ?? (() => Effect.succeed(false)), mcpServerStatus: options?.mcp?.mcpServerStatus ?? Effect.succeed([]), readMcpResource: @@ -3689,6 +3794,7 @@ describe("ClaudeAdapterV2 background wake turns", () => { event.type === "turn.terminal", ); return { + adapter, attachmentsDir, runtime, providerThread, @@ -3703,6 +3809,7 @@ describe("ClaudeAdapterV2 background wake turns", () => { return uuid; }, permissionModeChanges, + renameCalls, liveSelectionChanges, continuationRequests, continuationReceipts, @@ -3724,6 +3831,276 @@ describe("ClaudeAdapterV2 background wake turns", () => { }); const makeWakeHarness = makeWakeHarnessWithOptions(); + describe("ClaudeAdapterV2 session title sync", () => { + type TitleSyncHarness = Effect.Success>; + const startTurn = Effect.fnUntraced(function* ( + harness: TitleSyncHarness, + title: string, + ordinal: number, + nativeThreadHasTurns?: boolean, + runtimeOverride?: TitleSyncHarness["runtime"], + providerThreadOverride?: OrchestrationV2ProviderThread, + ) { + const runtime = runtimeOverride ?? harness.runtime; + const providerThread = providerThreadOverride ?? harness.providerThread; + yield* runtime.startTurn( + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make(`attempt-title-sync-${ordinal}`), + providerTurnOrdinal: ordinal, + title, + text: "What is the sentinel?", + attachments: [], + ...(nativeThreadHasTurns === undefined ? {} : { nativeThreadHasTurns }), + }), + ); + }); + const finishTurn = Effect.fnUntraced(function* (harness: TitleSyncHarness, ordinal: number) { + yield* harness.offerAndWait( + makeResultFrame({ uuid: `title-result-${ordinal}`, result: "Done." }), + ); + yield* Queue.take(harness.terminalReceipts); + }); + + it.effect("renames the native session only when the thread title changed", () => + Effect.gen(function* () { + const harness = yield* makeWakeHarnessWithOptions(); + yield* startTurn(harness, "First title", 1); + assert.deepEqual(harness.renameCalls, []); + yield* finishTurn(harness, 1); + + yield* startTurn(harness, "First title", 2); + assert.deepEqual(harness.renameCalls, []); + yield* finishTurn(harness, 2); + + yield* startTurn(harness, "Renamed title", 3); + assert.deepEqual(harness.renameCalls, ["Renamed title"]); + yield* finishTurn(harness, 3); + + yield* startTurn(harness, "Renamed title", 4); + assert.deepEqual(harness.renameCalls, ["Renamed title"]); + yield* finishTurn(harness, 4); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + + it.effect("a native /rename retitles the thread without renaming it back", () => + Effect.gen(function* () { + const harness = yield* makeWakeHarnessWithOptions(); + yield* startTurn(harness, "First title", 1); + yield* harness.offerAndWait( + claudeSdkFrame({ + type: "system", + subtype: "session_title_changed", + title: "Native title", + uuid: "title-changed-1", + session_id: WAKE_NATIVE_SESSION, + }), + ); + yield* harness.offerAndWait({ + ...makeResultFrame({ + uuid: "title-result-1", + result: "Session renamed to: Native title", + numTurns: 0, + }), + local_command: "rename", + } as SDKMessage); + yield* Queue.take(harness.terminalReceipts); + assert.deepEqual( + harness.events.flatMap((event) => + event.type === "app_thread.created" ? [event.appThread.title] : [], + ), + ["Native title"], + ); + + yield* startTurn(harness, "Native title", 2); + assert.deepEqual(harness.renameCalls, []); + yield* finishTurn(harness, 2); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + + it.effect("renames a resumed session whose stored title is stale", () => + Effect.gen(function* () { + const harness = yield* makeWakeHarnessWithOptions(); + yield* startTurn(harness, "First title", 1, true); + assert.deepEqual(harness.renameCalls, ["First title"]); + yield* finishTurn(harness, 1); + + yield* startTurn(harness, "First title", 2, true); + assert.deepEqual(harness.renameCalls, ["First title"]); + yield* finishTurn(harness, 2); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + + it.effect( + "a rename blocked past the deadline still offers the prompt and retries next turn", + () => + Effect.gen(function* () { + const entered = yield* Deferred.make(); + let attempts = 0; + const harness = yield* makeWakeHarnessWithOptions({ + renameSession: () => + ++attempts === 1 + ? Deferred.succeed(entered, undefined).pipe(Effect.andThen(Effect.never)) + : Effect.void, + }); + const firstTurn = yield* Effect.forkScoped(startTurn(harness, "First title", 1, true)); + yield* Deferred.await(entered); + yield* TestClock.adjust("10 seconds"); + yield* Fiber.join(firstTurn); + assert.deepEqual(harness.renameCalls, ["First title"]); + assert.equal(harness.offeredMessages.length, 1); + assert.equal(attempts, 1); + + yield* finishTurn(harness, 1); + assert.deepEqual(harness.renameCalls, ["First title"]); + + yield* startTurn(harness, "First title", 2, true); + assert.deepEqual(harness.renameCalls, ["First title", "First title"]); + assert.equal(attempts, 2); + yield* finishTurn(harness, 2); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + + it.effect("renames when reverting to an earlier title after a failed or timed out update", () => + Effect.gen(function* () { + const harness = yield* makeWakeHarnessWithOptions({ + renameSession: (title) => + title === "Title B" + ? Effect.fail( + new ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError({ + method: "renameSession", + cause: new Error("timed out or refused"), + }), + ) + : Effect.void, + }); + yield* startTurn(harness, "Title A", 1, true); + yield* finishTurn(harness, 1); + assert.deepEqual(harness.renameCalls, ["Title A"]); + + yield* startTurn(harness, "Title B", 2, true); + yield* finishTurn(harness, 2); + assert.deepEqual(harness.renameCalls, ["Title A", "Title B"]); + + yield* startTurn(harness, "Title A", 3, true); + yield* finishTurn(harness, 3); + assert.deepEqual(harness.renameCalls, ["Title A", "Title B", "Title A"]); + + yield* startTurn(harness, "Title A", 4, true); + yield* finishTurn(harness, 4); + assert.deepEqual(harness.renameCalls, ["Title A", "Title B", "Title A"]); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + + it.effect( + "preserves the last-applied title across provider-session recreation so a native /rename survives", + () => + Effect.gen(function* () { + const harness = yield* makeWakeHarnessWithOptions({ freshQueueOnReopen: true }); + yield* startTurn(harness, "First title", 1); + assert.deepEqual(harness.renameCalls, []); + yield* finishTurn(harness, 1); + + const lastEvent = harness.events.findLast( + ( + e, + ): e is Extract< + ProviderAdapter.ProviderAdapterV2Event, + { type: "provider_thread.updated" } + > => e.type === "provider_thread.updated", + ); + assert.equal(lastEvent?.providerThread.nativeMetadata?.title, "First title"); + const threadAfterTurn1 = lastEvent!.providerThread; + + const runtime2 = yield* harness.adapter.openSession({ + threadId: harness.threadId, + providerSessionId: ProviderSessionId.make("provider-session-claude-wake-2"), + modelSelection: CLAUDE_TEST_MODEL_SELECTION, + runtimePolicy: CLAUDE_TEST_RUNTIME_POLICY, + }); + yield* runtime2.events.pipe( + Stream.runForEach((event) => + Effect.gen(function* () { + harness.events.push(event); + if (event.type === "turn.terminal") { + yield* Queue.offer(harness.terminalReceipts, event); + } + }), + ), + Effect.forkScoped, + ); + const resumedThread = yield* runtime2.resumeThread({ + providerThread: threadAfterTurn1, + }); + assert.equal(resumedThread.nativeMetadata?.title, "First title"); + + yield* startTurn(harness, "First title", 2, true, runtime2, resumedThread); + assert.deepEqual(harness.renameCalls, []); + yield* finishTurn(harness, 2); + + yield* startTurn(harness, "Second title", 3, true, runtime2, resumedThread); + assert.deepEqual(harness.renameCalls, ["Second title"]); + yield* finishTurn(harness, 3); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + }); + it.effect.each([false, true])( "resumes a stopped query with empty SDK content and a fresh native process, buffered: %s", (buffered) => @@ -6879,7 +7256,7 @@ describe("ClaudeAdapterV2 background wake turns", () => { }).pipe(Effect.scoped, Effect.provide(testLayer)), ); - it.effect("declines URL and value forms without a card", () => + it.effect("cancels URL and value forms without a card", () => Effect.gen(function* () { const { elicit, runtimeRequests } = yield* startElicitationTurn(); for (const request of [ @@ -6893,7 +7270,7 @@ describe("ClaudeAdapterV2 background wake turns", () => { }, }, ] satisfies ReadonlyArray) { - assert.deepEqual(yield* Fiber.join(yield* elicit(request)), { action: "decline" }); + assert.deepEqual(yield* Fiber.join(yield* elicit(request)), { action: "cancel" }); } assert.isEmpty(runtimeRequests()); }).pipe(Effect.scoped, Effect.provide(testLayer)), @@ -11409,6 +11786,10 @@ describe("ClaudeAdapterV2 background wake turns", () => { }), ); yield* harness.awaitTerminal(1); + // A recovered retry must not fire its budget later. + yield* TestClock.adjust("10 minutes"); + assert.lengthOf(harness.terminalEvents(), 1); + assert.equal(harness.terminalEvents()[0]?.status, "completed"); }).pipe( Effect.provide( Layer.mergeAll( @@ -11421,33 +11802,197 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); - it.effect.each(["api_error", "overloaded_status"] as const)( - "fails a success-shaped Claude result with %s", - (terminalReason) => + const offerApiRetry = (harness: Effect.Success, uuid: string) => + Effect.gen(function* () { + yield* Queue.offer( + harness.sdkMessages, + claudeSdkFrame({ + type: "system", + subtype: "api_retry", + attempt: 1, + max_retries: 10, + retry_delay_ms: 600, + error_status: 503, + error: "server_error", + uuid, + session_id: WAKE_NATIVE_SESSION, + }), + ); + yield* harness.until(() => + harness.events.some( + (event) => + event.type === "turn_item.updated" && + event.turnItem.type === "error" && + event.turnItem.status === "running", + ), + ); + }); + + it.effect.each(["closed", "interrupt-hangs", "unconfirmed", "close-hangs", "synthetic"] as const)( + "fails a turn whose API retries outlast the retry budget: %s", + (response) => Effect.gen(function* () { - const harness = yield* makeWakeHarness; - const now = yield* DateTime.now; + const interruptSent = yield* Deferred.make(); + const closeRequested = yield* Deferred.make(); + const releaseClose = yield* Deferred.make(); + const harness = yield* makeWakeHarnessWithOptions({ + interrupt: Deferred.succeed(interruptSent, undefined).pipe( + Effect.andThen(response === "interrupt-hangs" ? Effect.never : Effect.void), + ), + close: (sdkMessages) => + Deferred.succeed(closeRequested, undefined).pipe( + Effect.andThen( + response === "close-hangs" + ? Deferred.await(releaseClose).pipe(Effect.andThen(Queue.shutdown(sdkMessages))) + : response === "unconfirmed" + ? Effect.void + : Queue.shutdown(sdkMessages), + ), + ), + }); + yield* Effect.addFinalizer(() => Deferred.succeed(releaseClose, undefined)); yield* harness.runtime.startTurn( makeClaudeTestTurnInput({ threadId: harness.threadId, providerThread: harness.providerThread, - now, - attemptId: RunAttemptId.make("attempt-structured-terminal-failure"), - text: "Complete the task.", + now: yield* DateTime.now, + attemptId: RunAttemptId.make("attempt-claude-retry-budget"), + text: "Summarize the repo.", attachments: [], }), ); - yield* Queue.offer( - harness.sdkMessages, - makeResultFrame({ - uuid: "00000000-0000-4000-8000-000000000205", - result: "Provider failure details.", - isError: false, - ...(terminalReason === "overloaded_status" - ? { apiErrorStatus: 529 } - : { terminalReason }), - }), - ); + yield* offerApiRetry(harness, "00000000-0000-4000-8000-000000000211"); + if (response === "synthetic") { + // The CLI reports the API error as a assistant frame; that is not recovery. + yield* Queue.offer( + harness.sdkMessages, + claudeSdkFrame({ + type: "assistant", + message: { + model: "", + id: "retry-notice", + type: "message", + role: "assistant", + content: [{ type: "text", text: "API Error: 503" }], + stop_reason: "end_turn", + stop_sequence: null, + usage: { + input_tokens: 0, + output_tokens: 0, + cache_creation_input_tokens: 0, + cache_read_input_tokens: 0, + }, + }, + parent_tool_use_id: null, + uuid: "retry-notice", + session_id: WAKE_NATIVE_SESSION, + }), + ); + yield* harness.until(() => + harness.events.some( + (event) => + event.type === "turn_item.updated" && event.turnItem.type === "assistant_message", + ), + ); + } + yield* TestClock.adjust("9 minutes"); + assert.lengthOf(harness.terminalEvents(), 0); + yield* TestClock.adjust("1 minute"); + yield* Deferred.await(interruptSent); + if (response !== "closed" && response !== "synthetic") { + yield* TestClock.adjust("20 seconds"); + } + const terminal = yield* Queue.take(harness.terminalReceipts); + assert.equal(terminal.status, "failed"); + if (terminal.status !== "failed") return; + assert.equal(terminal.failure.code, "api_error_503"); + assert.equal(terminal.retry?.attempt, 1); + assert.lengthOf(harness.terminalEvents(), 1); + assert.isTrue(yield* Deferred.isDone(closeRequested), "the query must be closed"); + }).pipe( + Effect.scoped, + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + + it.effect("keeps a user Stop interrupted when the retry budget passes before it settles", () => + Effect.gen(function* () { + const harness = yield* makeWakeHarness; + yield* harness.runtime.startTurn( + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make("attempt-claude-retry-budget-user-stop"), + text: "Summarize the repo.", + attachments: [], + }), + ); + yield* offerApiRetry(harness, "00000000-0000-4000-8000-000000000212"); + const active = harness.events.find( + (event) => + event.type === "provider_turn.updated" && event.providerTurn.status === "running", + ); + if (active?.type !== "provider_turn.updated") + return yield* Effect.die("Missing running native turn"); + // The CLI has not confirmed the Stop when the budget passes. + yield* harness.runtime + .interruptTurn({ + providerThread: harness.providerThread, + providerTurnId: active.providerTurn.id, + }) + .pipe(Effect.ignore, Effect.forkScoped); + yield* TestClock.adjust("10 minutes"); + yield* Queue.shutdown(harness.sdkMessages); + assert.equal((yield* Queue.take(harness.terminalReceipts)).status, "interrupted"); + assert.lengthOf(harness.terminalEvents(), 1); + }).pipe( + Effect.scoped, + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + + it.effect.each(["api_error", "overloaded_status"] as const)( + "fails a success-shaped Claude result with %s", + (terminalReason) => + Effect.gen(function* () { + const harness = yield* makeWakeHarness; + const now = yield* DateTime.now; + yield* harness.runtime.startTurn( + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now, + attemptId: RunAttemptId.make("attempt-structured-terminal-failure"), + text: "Complete the task.", + attachments: [], + }), + ); + yield* Queue.offer( + harness.sdkMessages, + makeResultFrame({ + uuid: "00000000-0000-4000-8000-000000000205", + result: "Provider failure details.", + isError: false, + ...(terminalReason === "overloaded_status" + ? { apiErrorStatus: 529 } + : { terminalReason }), + }), + ); const terminal = yield* Queue.take(harness.terminalReceipts); assert.equal(terminal.status, "failed"); if (terminal.status !== "failed") return; @@ -11970,14 +12515,48 @@ describe("ClaudeAdapterV2 background wake turns", () => { const roster = providerThreadRosterEvents(harness.events).at(-1)?.providerThread .pendingBackgroundTasks; + const monitorItem = harness.events.find( + (event) => + event.type === "turn_item.updated" && + event.turnItem.nativeItemRef?.nativeId === "toolu_monitor_0", + ); + assert.equal(monitorItem?.type, "turn_item.updated"); + if (monitorItem?.type !== "turn_item.updated") return; assert.deepEqual(roster, [ { taskId: longRunning.taskId, kind: "monitor", + awaited: true, description: "Monitor 0", nativeItemId: "toolu_monitor_0", + command: "sleep 8 && echo MONITOR_DONE", + itemId: monitorItem.turnItem.id, + sourceThreadId: harness.threadId, }, ]); + assert.isDefined(harness.runtime.awaitBackgroundTasks); + const released = yield* harness.runtime.awaitBackgroundTasks!({ + providerThread: harness.providerThread, + taskIds: [longRunning.taskId], + awaited: false, + }); + assert.deepEqual(released.matched, [longRunning.taskId]); + yield* harness.offerAndWait( + claudeSdkFrame({ + type: "system", + subtype: "background_tasks_changed", + tasks: [ + { task_id: longRunning.taskId, task_type: "local_bash", description: "Monitor 0" }, + ], + uuid: nextUuid(), + session_id: WAKE_NATIVE_SESSION, + }), + ); + assert.equal( + providerThreadRosterEvents(harness.events).at(-1)?.providerThread + .pendingBackgroundTasks?.[0]?.awaited, + undefined, + ); }).pipe( Effect.provide( Layer.mergeAll( @@ -12173,6 +12752,11 @@ describe("ClaudeAdapterV2 background wake turns", () => { assert.equal(harness.terminalEvents()[settled + 1]?.status, "completed"); assert.include(assistantTexts(), "Scheduled answer."); assert.lengthOf(harness.continuationRequests, 2); + assert.deepInclude( + providerThreadRosterEvents(harness.events).at(-1)?.providerThread + .pendingBackgroundTasks?.[0], + { taskId, kind: "monitor", awaited: true }, + ); }).pipe( Effect.provide( Layer.mergeAll( @@ -12773,15 +13357,47 @@ describe("ClaudeAdapterV2 background wake turns", () => { attachments: [], }), ); - acknowledge = harness.offerAndWait( - makeResultFrame({ - uuid: "00000000-0000-4000-8000-000000000620", - result: "", - subtype: "error_during_execution", - errors: ["Error: Request was aborted."], - terminalReason: "aborted_streaming", - }), - ); + // Real Claude answers Stop with an error result for the running tool, then the result. + acknowledge = harness + .offerAndWait( + claudeSdkFrame({ + type: "user", + uuid: "00000000-0000-4000-8000-000000000619", + session_id: WAKE_NATIVE_SESSION, + parent_tool_use_id: null, + message: { + role: "user", + content: [ + { + type: "tool_result", + tool_use_id: "tool-running-bash", + is_error: true, + content: "The user doesn't want to proceed with this tool use.", + }, + { + type: "tool_result", + tool_use_id: "tool-failed-bash", + is_error: true, + content: "Process exited with code 1", + }, + ], + }, + tool_result_meta: [{ id: "tool-running-bash", non_execution_kind: "user-rejected" }], + }), + ) + .pipe( + Effect.andThen( + harness.offerAndWait( + makeResultFrame({ + uuid: "00000000-0000-4000-8000-000000000620", + result: "", + subtype: "error_during_execution", + errors: ["Error: Request was aborted."], + terminalReason: "aborted_tools", + }), + ), + ), + ); yield* harness.until(() => harness.events.some( (event) => @@ -12794,6 +13410,37 @@ describe("ClaudeAdapterV2 background wake turns", () => { ); if (active?.type !== "provider_turn.updated") return yield* Effect.die("Missing running native turn"); + yield* harness.offerAndWait( + claudeSdkFrame({ + type: "assistant", + uuid: "00000000-0000-4000-8000-000000000618", + session_id: WAKE_NATIVE_SESSION, + parent_tool_use_id: null, + message: { + id: "msg_running_bash", + model: "claude-sonnet-4-6", + type: "message", + role: "assistant", + content: [ + { + type: "tool_use", + id: "tool-running-bash", + name: "Bash", + input: { command: "sleep 60" }, + }, + { + type: "tool_use", + id: "tool-failed-bash", + name: "Bash", + input: { command: "exit 1" }, + }, + ], + stop_reason: "tool_use", + stop_sequence: null, + usage: { input_tokens: 1, output_tokens: 1 }, + }, + }), + ); yield* harness.runtime.interruptTurn({ providerThread: harness.providerThread, providerTurnId: active.providerTurn.id, @@ -12802,6 +13449,24 @@ describe("ClaudeAdapterV2 background wake turns", () => { assert.equal((yield* Queue.take(harness.terminalReceipts)).status, "interrupted"); assert.equal(closes, 1); assert.lengthOf(harness.terminalEvents(), 1); + const stoppedTool = harness.events.findLast( + (event) => + event.type === "turn_item.updated" && + event.turnItem.nativeItemRef?.nativeId === "tool-running-bash", + ); + const failedTool = harness.events.findLast( + (event) => + event.type === "turn_item.updated" && + event.turnItem.nativeItemRef?.nativeId === "tool-failed-bash", + ); + assert.equal( + failedTool?.type === "turn_item.updated" ? failedTool.turnItem.status : undefined, + "failed", + ); + assert.equal( + stoppedTool?.type === "turn_item.updated" ? stoppedTool.turnItem.status : undefined, + "interrupted", + ); }).pipe( Effect.provide( Layer.mergeAll( @@ -13064,6 +13729,7 @@ describe("ClaudeAdapterV2 background wake turns", () => { setModel: () => Effect.void, applyFlagSettings: () => Effect.void, setPermissionMode: () => Effect.void, + renameSession: () => Effect.void, backgroundTasks: () => Effect.succeed(false), mcpServerStatus: Effect.succeed([]), readMcpResource: () => Effect.die("unexpected MCP resource read"), @@ -13215,6 +13881,10 @@ describe("ClaudeAdapterV2 background wake turns", () => { const openAdapterWhoseFirstQueryEndsBeforeItsFirstFrame = Effect.fnUntraced(function* ( name: string, + firstQueryExit: Stream.Stream< + SDKMessage, + ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError + > = Stream.empty, ) { const fileSystem = yield* FileSystem.FileSystem; const idAllocator = yield* IdAllocator.IdAllocatorV2; @@ -13253,12 +13923,15 @@ describe("ClaudeAdapterV2 background wake turns", () => { // The first CLI exits without writing a frame. messages: opened === 1 - ? Stream.empty.pipe(Stream.ensuring(Deferred.succeed(deadStreamEnded, undefined))) + ? firstQueryExit.pipe( + Stream.ensuring(Deferred.succeed(deadStreamEnded, undefined)), + ) : Stream.fromQueue(sdkMessages), offer: () => Effect.void, setModel: () => Effect.void, applyFlagSettings: () => Effect.void, setPermissionMode: () => Effect.void, + renameSession: () => Effect.void, backgroundTasks: () => Effect.succeed(false), mcpServerStatus: Effect.succeed([]), readMcpResource: () => Effect.die("unexpected MCP resource read"), @@ -13301,6 +13974,43 @@ describe("ClaudeAdapterV2 background wake turns", () => { return { runtime, turnInput }; }); + it.effect("shows Claude's root refusal when its query exits before the turn starts", () => + Effect.scoped( + Effect.gen(function* () { + const harness = yield* openAdapterWhoseFirstQueryEndsBeforeItsFirstFrame( + "claude-query-refuses-root-before-frame", + Stream.fail( + new ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError({ + method: "fromAsyncIterable", + cause: new ClaudeAdapterV2.ClaudeCodeRootRefusalError({ + cause: new Error("Claude Code process exited with code 1"), + }), + }), + ), + ); + const started = yield* harness.runtime + .startTurn(harness.turnInput("a", 1)) + .pipe(Effect.exit); + assert.isTrue(Exit.isFailure(started)); + if (Exit.isFailure(started)) { + assert.strictEqual( + makeProviderFailure({ cause: started.cause, class: "transport_error" }).message, + "Claude Code exited before it started: --dangerously-skip-permissions cannot be used with root/sudo privileges for security reasons", + ); + } + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ), + ); + it.effect("fails a turn whose Claude query ends before its first frame", () => Effect.gen(function* () { const harness = yield* openAdapterWhoseFirstQueryEndsBeforeItsFirstFrame( @@ -13389,6 +14099,7 @@ describe("ClaudeAdapterV2 background wake turns", () => { setModel: () => Effect.void, applyFlagSettings: () => Effect.void, setPermissionMode: () => Effect.void, + renameSession: () => Effect.void, backgroundTasks: () => Effect.succeed(false), mcpServerStatus: Effect.succeed([]), readMcpResource: () => Effect.die("unexpected MCP resource read"), @@ -16215,6 +16926,70 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); + it.effect("does not count the echo of a live model switch as background work", () => + Effect.gen(function* () { + const idleUserFrame = (uuid: string, content: string, isReplay: boolean) => + claudeSdkFrame({ + type: "user", + message: { role: "user", content }, + parent_tool_use_id: null, + ...(isReplay ? { isReplay } : {}), + uuid, + session_id: WAKE_NATIVE_SESSION, + }); + // As the CLI does, answer setModel with its echo before the next turn attaches. + const harness = yield* makeWakeHarnessWithOptions({ + setModel: (model, offerAndWait) => + offerAndWait( + idleUserFrame( + "00000000-0000-4000-8000-000000000b01", + `Set model to \`${model}\``, + true, + ), + ), + }); + const now = yield* DateTime.now; + const turn = (ordinal: number, model: string) => + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now, + attemptId: RunAttemptId.make(`attempt-claude-model-switch-echo:${ordinal}`), + text: `Request ${ordinal}`, + attachments: [], + providerTurnOrdinal: ordinal, + modelSelection: { instanceId: ClaudeAdapterV2.CLAUDE_DEFAULT_INSTANCE_ID, model }, + }); + const settle = Effect.gen(function* () { + yield* harness.offerAndWait(turnOneResult); + yield* Queue.take(harness.terminalReceipts); + }); + yield* harness.runtime.startTurn(turn(1, "claude-opus-5-5")); + yield* settle; + yield* harness.runtime.startTurn(turn(2, "claude-sonnet-4-6")); + yield* settle; + + assert.deepInclude(harness.liveSelectionChanges, { model: "claude-sonnet-4-6" }); + assert.lengthOf(harness.continuationRequests, 0); + assert.isFalse(yield* harness.hasPendingBackgroundWork); + + // A root user frame the CLI produced on its own still marks a wake. + yield* harness.offerAndWait( + idleUserFrame("00000000-0000-4000-8000-000000000b02", "A background task finished.", false), + ); + assert.isTrue(yield* harness.hasPendingBackgroundWork); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + it.effect( "emits one interrupted terminal for a positive task-notification result racing interrupt", () => @@ -16589,6 +17364,7 @@ describe("ClaudeAdapterV2 background wake turns", () => { setModel: () => Effect.void, applyFlagSettings: () => Effect.void, setPermissionMode: () => Effect.void, + renameSession: () => Effect.void, backgroundTasks: () => Effect.succeed(false), stopTask: () => Effect.void, mcpServerStatus: Effect.die("unexpected MCP server status"), @@ -19847,6 +20623,7 @@ describe("ClaudeAdapterV2 background wake turns", () => { setModel: () => Effect.void, applyFlagSettings: () => Effect.void, setPermissionMode: () => Effect.void, + renameSession: () => Effect.void, backgroundTasks: () => Effect.succeed(false), mcpServerStatus: Effect.succeed([]), readMcpResource: () => Effect.die("unexpected MCP resource read"), @@ -20024,6 +20801,7 @@ describe("ClaudeAdapterV2 background wake turns", () => { setModel: () => Effect.void, applyFlagSettings: () => Effect.void, setPermissionMode: () => Effect.void, + renameSession: () => Effect.void, backgroundTasks: () => Effect.succeed(false), mcpServerStatus: Effect.succeed([]), readMcpResource: () => Effect.die("unexpected MCP resource read"), @@ -20201,6 +20979,7 @@ describe("ClaudeAdapterV2 background wake turns", () => { setModel: () => Effect.void, applyFlagSettings: () => Effect.void, setPermissionMode: () => Effect.void, + renameSession: () => Effect.void, backgroundTasks: () => Effect.succeed(false), mcpServerStatus: Effect.succeed([]), readMcpResource: () => Effect.die("unexpected MCP resource read"), @@ -20399,6 +21178,7 @@ describe("ClaudeAdapterV2 background wake turns", () => { setModel: () => Effect.void, applyFlagSettings: () => Effect.void, setPermissionMode: () => Effect.void, + renameSession: () => Effect.void, backgroundTasks: () => Effect.succeed(false), mcpServerStatus: Effect.succeed([]), readMcpResource: () => Effect.die("unexpected MCP resource read"), @@ -20545,6 +21325,7 @@ describe("ClaudeAdapterV2 background wake turns", () => { setModel: () => Effect.void, applyFlagSettings: () => Effect.void, setPermissionMode: () => Effect.void, + renameSession: () => Effect.void, backgroundTasks: () => Effect.succeed(false), mcpServerStatus: Effect.succeed([]), readMcpResource: () => Effect.die("unexpected MCP resource read"), @@ -20666,6 +21447,7 @@ describe("ClaudeAdapterV2 background wake turns", () => { setModel: () => Effect.void, applyFlagSettings: () => Effect.void, setPermissionMode: () => Effect.void, + renameSession: () => Effect.void, backgroundTasks: () => Effect.succeed(false), mcpServerStatus: Effect.succeed([]), readMcpResource: () => Effect.die("unexpected MCP resource read"), @@ -20830,8 +21612,8 @@ describe("ClaudeAdapterV2 background wake turns", () => { }); const workflowSnapshot = (input: { readonly uuid: string; - readonly state: "queued" | "start" | "done"; - readonly secondState?: "queued" | "start" | "done"; + readonly state: "queued" | "start" | "done" | "error"; + readonly secondState?: "queued" | "start" | "done" | "error"; readonly startedAt?: number; readonly lastProgressAt?: number; readonly durationMs?: number; @@ -22609,6 +23391,185 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); + it.effect("shows a workflow member's tool as stopped when Stop cancels the workflow", () => + Effect.scoped( + Effect.gen(function* () { + const harness = yield* makeWorkflowHarness; + const transcriptPath = NodePath.join(harness.transcriptDir, "agent-a1.jsonl"); + NodeFS.writeFileSync( + transcriptPath, + memberAssistantLine("line-bash", "msg_a1", [ + { type: "tool_use", id: "toolu_a1_ping", name: "Bash", input: { command: "ping" } }, + ]), + ); + yield* harness.runtime.startTurn( + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make("attempt-claude-workflow-member-stopped"), + text: "Run the alpha workflow.", + attachments: [], + }), + ); + yield* launchWorkflow({ harness, snapshotUuid: "00000000-0000-4000-8000-000000001375" }); + const threadId = workflowMemberEvents(harness.events, 1)[0]?.subagent.childThreadId; + const errorLine = (uuid: string, toolUseId: string, output: string, denial?: object) => + transcriptLine({ + type: "user", + uuid, + message: { + role: "user", + content: [ + { tool_use_id: toolUseId, type: "tool_result", content: output, is_error: true }, + ], + }, + ...denial, + }); + const denial = { + toolUseResult: "User rejected tool use", + toolDenialKind: "user-rejected", + }; + NodeFS.appendFileSync( + transcriptPath, + errorLine("line-ping-failed", "toolu_a1_ping", "Exit code 1"), + ); + yield* harness.offerAndWait( + workflowSnapshot({ uuid: "00000000-0000-4000-8000-000000001378", state: "start" }), + ); + // A declined approval written before Stop but first read after it stays a failure. + NodeFS.appendFileSync( + transcriptPath, + memberAssistantLine("line-rm", "msg_a1_rm", [ + { type: "tool_use", id: "toolu_a1_rm", name: "Bash", input: { command: "rm" } }, + ]) + + errorLine("line-rm-declined", "toolu_a1_rm", "Declined", denial) + + memberAssistantLine("line-sleep", "msg_a1_sleep", [ + { type: "tool_use", id: "toolu_a1_sleep", name: "Bash", input: { command: "sleep" } }, + ]), + ); + yield* TestClock.setTime(Date.parse("2026-10-17T06:00:00.000Z")); + yield* harness.runtime.stopTask!({ + providerThread: harness.providerThread, + taskId: WORKFLOW_TASK_ID, + }); + // Claude writes the Stop rejection while the member still reads as running, then reports it as an error. + NodeFS.appendFileSync( + transcriptPath, + errorLine("line-sleep-stopped", "toolu_a1_sleep", "Rejected", { + ...denial, + timestamp: "2026-10-17T06:00:01.000Z", + }), + ); + yield* harness.offerAndWait( + workflowSnapshot({ uuid: "00000000-0000-4000-8000-000000001379", state: "start" }), + ); + yield* harness.offerAndWait( + workflowSnapshot({ + uuid: "00000000-0000-4000-8000-000000001380", + state: "error", + // Claude's last progress tick for a member blocked in one tool predates the Stop. + lastProgressAt: Date.parse("2026-10-17T05:59:00.000Z"), + }), + ); + yield* harness.offerAndWait( + workflowNotification({ uuid: "00000000-0000-4000-8000-000000001376", status: "stopped" }), + ); + assert.equal(workflowMemberEvents(harness.events, 1).at(-1)?.subagent.status, "cancelled"); + assert.deepEqual(latestMemberThreadItems(harness.events, threadId).map(describeItem), [ + "user_message", + "command failed: Exit code 1", + "command failed: Declined", + "command interrupted: Rejected", + ]); + yield* Queue.offer( + harness.sdkMessages, + makeResultFrame({ uuid: "00000000-0000-4000-8000-000000001377", result: "Done." }), + ); + yield* Queue.take(harness.terminalReceipts); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ), + ); + + it.effect("keeps genuine member failures failed around a workflow Stop", () => + Effect.gen(function* () { + let stopCalls = 0; + const harness = yield* makeWorkflowHarnessWithOptions({ + stopTask: () => + ++stopCalls === 1 + ? Effect.fail( + new ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError({ + method: "stopTask", + cause: new Error("rejected"), + }), + ) + : Effect.void, + }); + yield* harness.runtime.startTurn( + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make("attempt-claude-workflow-stop-failures"), + text: "Run the alpha workflow.", + attachments: [], + }), + ); + yield* launchWorkflow({ harness, snapshotUuid: "00000000-0000-4000-8000-000000001390" }); + const memberStatus = (index: number) => + workflowMemberEvents(harness.events, index).at(-1)?.subagent.status; + const stop = harness.runtime.stopTask!({ + providerThread: harness.providerThread, + taskId: WORKFLOW_TASK_ID, + }).pipe(Effect.exit); + yield* harness.offerAndWait( + workflowSnapshot({ + uuid: "00000000-0000-4000-8000-000000001391", + state: "error", + secondState: "start", + }), + ); + assert.equal(memberStatus(1), "failed"); + assert.isTrue(Exit.isFailure(yield* stop)); + yield* harness.offerAndWait( + workflowSnapshot({ + uuid: "00000000-0000-4000-8000-000000001392", + state: "error", + secondState: "error", + }), + ); + assert.equal(memberStatus(2), "failed"); + assert.isTrue(Exit.isSuccess(yield* stop)); + yield* harness.offerAndWait( + workflowSnapshot({ uuid: "00000000-0000-4000-8000-000000001393", state: "error" }), + ); + assert.deepEqual([memberStatus(1), memberStatus(2)], ["failed", "failed"]); + yield* Queue.offer( + harness.sdkMessages, + makeResultFrame({ uuid: "00000000-0000-4000-8000-000000001394", result: "Done." }), + ); + yield* Queue.take(harness.terminalReceipts); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + it.effect("reads a settled workflow member's transcript to its end past the read cap", () => Effect.gen(function* () { const harness = yield* makeWorkflowHarness; @@ -22805,6 +23766,79 @@ describe("ClaudeAdapterV2 background wake turns", () => { ), ); + it.effect("ends a goal check without re-prompting while T3 work will wake the thread", () => + Effect.gen(function* () { + const harness = yield* makeWakeHarness; + const mcpSessions = yield* McpProviderSessions.McpProviderSessions; + yield* mcpSessions.set({ + environmentId: EnvironmentId.make("environment-goal-wake"), + threadId: harness.threadId, + providerSessionId: "mcp-session-goal-wake", + providerInstanceId: ProviderInstanceId.make("claudeAgent"), + endpoint: "http://127.0.0.1:43123/mcp", + authorizationHeader: "Bearer secret-claude-token", + browserToolsAvailable: true, + hasPendingWake: Effect.succeed(true), + }); + const stop = Effect.promise(() => + Promise.all( + (harness.getOpenedOptions()?.hooks?.Stop ?? []).flatMap((matcher) => + matcher.hooks.map((hook) => + hook( + { + hook_event_name: "Stop", + stop_hook_active: true, + session_id: WAKE_NATIVE_SESSION, + transcript_path: "/transcript.jsonl", + cwd: "/workspace", + }, + undefined, + { signal: new AbortController().signal }, + ), + ), + ), + ), + ); + const condition = "the delegated review is addressed"; + yield* harness.runtime.startTurn( + makeClaudeTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make("goal-wake-attempt"), + text: `/goal ${condition}`, + attachments: [], + }), + ); + // Without a goal, Stop keeps Claude Code's own behavior. + assert.deepEqual(yield* stop, [{}]); + yield* harness.offerAndWait(syntheticFrame("goal-wake-set", `Goal set: ${condition}`)); + assert.deepEqual(yield* stop, [{ continue: false }]); + // The turn ends without an error and the goal waits for the wake. + yield* Queue.offer( + harness.sdkMessages, + makeResultFrame({ + uuid: "goal-wake-result", + result: "Waiting for the review.", + terminalReason: "stop_hook_prevented", + }), + ); + const terminal = yield* Queue.take(harness.terminalReceipts); + assert.equal(terminal.status, "completed"); + assert.equal(goalStatuses(harness.events).at(-1)?.status, "active"); + }).pipe( + Effect.scoped, + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + it.effect( "defers goal completion until an ambient subagent finishes and Claude checks again", () => diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.testkit.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.testkit.ts index 9977ea942a21a..feac31a89af97 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.testkit.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.testkit.ts @@ -838,6 +838,8 @@ function makeReplayQueryRunner( mode, }); }), + // Recorded sessions already hold their title, and no replay asserts rename frames. + renameSession: () => Effect.void, stopTask: (taskId) => replayEffect(() => { assertNextOutboundFrame({ type: "query.stop_task", taskId }); diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index f7f4d0c221d75..c3041a4cb9162 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -85,6 +85,7 @@ import { } from "@t3tools/shared/mcpApp"; import { AgentScope } from "@t3tools/shared/AgentScope"; import * as ProviderProcesses from "../../process/providerProcesses.ts"; +import * as SpawnWorker from "../../process/SpawnWorker.ts"; import * as HostProcess from "@t3tools/shared/HostProcess"; import { applyClaudePromptEffortPrefix } from "@t3tools/shared/model"; import { compareSemverVersions } from "@t3tools/shared/semver"; @@ -229,6 +230,7 @@ import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; import { makeProviderFailure, makeProviderRetryTurnItem, + PROVIDER_RETRY_BUDGET_MS, } from "@t3tools/provider-core/server/failure"; import { turnScopedSelectionTransition } from "@t3tools/provider-core/server/selectionTransition"; import { providerMessageTextWithAttachmentPaths } from "@t3tools/provider-core/server/attachmentPrompt"; @@ -355,6 +357,9 @@ function mergeClaudeStreamUsage( }; } export const CLAUDE_DEFAULT_INSTANCE_ID = defaultInstanceIdForDriver(CLAUDE_PROVIDER); +// The Claude CLI takes the next token as this flag's value even when it starts with `--`. +const claudeLaunchArgParseOptions = { valueFlags: ["append-system-prompt"] }; + const CLAUDE_SCRIPT_EXTENSIONS = [".js", ".mjs", ".tsx", ".ts", ".jsx"]; // The SDK has no control-request deadline; the CLI waits up to 30 s per MCP server. const CLAUDE_MCP_REGISTRATION_TIMEOUT = "90 seconds"; @@ -668,6 +673,7 @@ export interface ClaudeAgentSdkQuerySession { readonly setPermissionMode: ( mode: PermissionMode, ) => Effect.Effect; + readonly renameSession: (title: string) => Effect.Effect; readonly stopTask: (taskId: string) => Effect.Effect; readonly backgroundTasks: ( toolUseId: string, @@ -862,7 +868,7 @@ function closeClaudeQuery(queryRuntime: ClaudeQuery) { async function sendClaudeControlRequest( queryRuntime: ClaudeQuery, request: { - readonly subtype: "mcp_call" | "list_permission_rules"; + readonly subtype: "mcp_call" | "list_permission_rules" | "rename_session"; readonly [key: string]: unknown; }, ): Promise { @@ -1083,6 +1089,14 @@ export type ClaudeAgentSdkProtocolLogEvent = readonly mode: PermissionMode; }; } + | { + readonly direction: "outgoing"; + readonly stage: "decoded"; + readonly payload: { + readonly type: "query.rename_session"; + readonly title: string; + }; + } | { readonly direction: "outgoing"; readonly stage: "decoded"; @@ -1343,6 +1357,26 @@ const forkClaudeSessionInWorker = Effect.fn("forkClaudeSessionInWorker")(functio ); }); +const CLAUDE_CAN_USE_TOOL_SHADOWED_WARNING = "CLAUDE_SDK_CAN_USE_TOOL_SHADOWED"; + +// query() warns when canUseTool meets bypassPermissions or bare allowedTools. T3 installs +// canUseTool in every mode on purpose (AskUserQuestion, ask rules), so it is per-session noise. +export function withoutClaudeCanUseToolShadowedWarning
(run: () => A): A { + const emitWarning = process.emitWarning; + process.emitWarning = function (this: NodeJS.Process, ...args: Parameters) { + const [, options] = args; + if (typeof options === "object" && options.code === CLAUDE_CAN_USE_TOOL_SHADOWED_WARNING) { + return; + } + return Reflect.apply(emitWarning, this, args); + } as typeof emitWarning; + try { + return run(); + } finally { + process.emitWarning = emitWarning; + } +} + export const layerQueryRunner: Layer.Layer< ClaudeAgentSdkQueryRunner, never, @@ -1516,12 +1550,11 @@ export const layerQueryRunner: Layer.Layer< } return child; } - const child = NodeChildProcess.spawn(spawnOptions.command, spawnOptions.args, { + const child = SpawnWorker.spawnPiped(spawnOptions.command, spawnOptions.args, { cwd: spawnOptions.cwd, env: spawnOptions.env, signal: spawnOptions.signal, windowsHide: true, - stdio: ["pipe", "pipe", "pipe"], }); child.stderr .setEncoding("utf8") @@ -1552,10 +1585,12 @@ export const layerQueryRunner: Layer.Layer< const queryRuntime = yield* Effect.uninterruptibleMask((restore) => Effect.try({ try: () => - query({ - prompt, - options: { ...options, spawnClaudeCodeProcess }, - }), + withoutClaudeCanUseToolShadowedWarning(() => + query({ + prompt, + options: { ...options, spawnClaudeCodeProcess }, + }), + ), catch: (cause) => queryRunnerError(cause, "query"), }).pipe( Effect.tap((queryRuntime) => @@ -1747,6 +1782,28 @@ export const layerQueryRunner: Layer.Layer< }), ), ), + renameSession: (title) => + Effect.tryPromise({ + try: () => + sendClaudeControlRequest(queryRuntime, { + subtype: "rename_session", + title, + source: "host", + }), + catch: (cause) => queryRunnerError(cause, "renameSession"), + }).pipe( + Effect.asVoid, + Effect.tap(() => + logProtocolEvent({ + direction: "outgoing", + stage: "decoded", + payload: { + type: "query.rename_session", + title, + }, + }), + ), + ), stopTask: (taskId) => Effect.tryPromise({ try: () => queryRuntime.stopTask(taskId), @@ -2102,6 +2159,7 @@ export function makeClaudeQueryOptions(input: { readonly canUseTool?: CanUseTool; readonly onUserDialog?: ClaudeQueryOptions["onUserDialog"]; readonly onElicitation?: ClaudeQueryOptions["onElicitation"]; + readonly onStop?: HookCallback; readonly supportedDialogKinds?: ClaudeQueryOptions["supportedDialogKinds"]; readonly allowDangerouslySkipPermissions?: boolean; readonly appendSystemPromptFileText?: string; @@ -2119,7 +2177,9 @@ export function makeClaudeQueryOptions(input: { // The SDK replaces this flag with options.settings, so it arrives read as sdkSettings. settings: _launchArgSettings, ...extraArgs - } = input.settings === undefined ? {} : parseCliArgs(input.settings.launchArgs).flags; + } = input.settings === undefined + ? {} + : parseCliArgs(input.settings.launchArgs, claudeLaunchArgParseOptions).flags; // Claude Code overwrites its CLI append text with the SDK's initialize // appendSystemPrompt, so launch-argument text has to travel in that value. // The CLI puts file text before inline text and trims one trailing newline. @@ -2254,8 +2314,15 @@ export function makeClaudeQueryOptions(input: { ...launchAppendSystemPrompt, ].join("\n\n"), }, - ...(deviceInstructions - ? { hooks: { SubagentStart: [{ hooks: [subagentDeviceInstructions] }] } } + ...(deviceInstructions || input.onStop !== undefined + ? { + hooks: { + ...(deviceInstructions + ? { SubagentStart: [{ hooks: [subagentDeviceInstructions] }] } + : {}), + ...(input.onStop === undefined ? {} : { Stop: [{ hooks: [input.onStop] }] }), + }, + } : {}), ...(Object.keys(extraArgs).length === 0 ? {} : { extraArgs }), }; @@ -2282,7 +2349,7 @@ const readClaudeAppendSystemPromptFile = Effect.fnUntraced(function* (input: { readonly path: Path.Path; }) { if (input.settings === undefined) return undefined; - const flags = parseCliArgs(input.settings.launchArgs).flags; + const flags = parseCliArgs(input.settings.launchArgs, claudeLaunchArgParseOptions).flags; // parseCliArgs gives null for a flag with no following value. An explicit // `--append-system-prompt ""` is a value the CLI accepts, so it stays. if (flags["append-system-prompt"] === null) { @@ -2319,7 +2386,7 @@ const decodeClaudeLaunchArgSettings = Schema.decodeUnknownEffect( * relative to the thread's cwd (the project or its worktree). * makeClaudeQueryOptions merges the result beneath T3's own settings. */ -const readClaudeLaunchArgSettings = Effect.fn("ClaudeAdapterV2.readLaunchArgSettings")( +export const readClaudeLaunchArgSettings = Effect.fn("ClaudeAdapterV2.readLaunchArgSettings")( function* (input: { readonly settings: ClaudeSettings | undefined; readonly cwd: string | null; @@ -2327,7 +2394,8 @@ const readClaudeLaunchArgSettings = Effect.fn("ClaudeAdapterV2.readLaunchArgSett readonly path: Path.Path; }) { if (input.settings === undefined) return undefined; - const value = parseCliArgs(input.settings.launchArgs).flags.settings; + const value = parseCliArgs(input.settings.launchArgs, claudeLaunchArgParseOptions).flags + .settings; if (value === undefined) return undefined; const trimmed = value?.trim() ?? ""; if (trimmed.length === 0) return yield* new ClaudeSettingsLaunchArgError({}); @@ -2372,7 +2440,7 @@ export const CLAUDE_READ_ONLY_T3_MCP_ALLOWED_TOOLS: ReadonlyArray = [ "mcp__t3-code__send_to_user", ]; -// T3 waits can block for an hour; exceed Claude's default 60 s timeout. +// request_secret can wait an hour; exceed Claude's default 60 s timeout. export const CLAUDE_T3_MCP_TOOL_TIMEOUT_MS = 65 * 60 * 1_000; // allowedTools pre-approves calls; tools controls availability. Pre-approve T3 tools for headless modes, @@ -2534,6 +2602,12 @@ function resultTextFromSdkMessage( }; } +function normalizeClaudeSessionTitle(title: string | null | undefined): string | null { + if (title === null || title === undefined) return null; + const trimmed = title.trim(); + return trimmed.length > 0 ? trimmed : null; +} + function makeProviderThread(input: { readonly idAllocator: IdAllocator.IdAllocatorV2["Service"]; readonly providerInstanceId: ProviderInstanceId; @@ -3639,6 +3713,14 @@ const CLAUDE_SCHEDULED_WAKEUP_TASK: OrchestrationV2PendingBackgroundTask = { // most 60 s before the minute-rounded scheduledFor it reports; the rest is slack. const CLAUDE_SCHEDULED_WAKEUP_EARLY_FIRE_MARGIN_MS = 120_000; +// The CLI answers a control request such as setModel with a replayed local-command user frame. +// It belongs to no turn, so on an idle session it is not the start of a wake. +function isClaudeLocalCommandEcho(message: SDKMessage): boolean { + if (message.type !== "user" || !("isReplay" in message) || !message.isReplay) return false; + const content = message.message.content; + return typeof content === "string" && content.startsWith(""); +} + function claudePendingBackgroundTasksFromRoster( roster: ReadonlyMap, awaitedTaskIds: ReadonlySet, @@ -4216,13 +4298,14 @@ export function permissionResultFromDecision(input: { }; } + const feedback = input.feedback?.trim(); return { behavior: "deny", - message: - input.feedback?.trim() || - (input.decision === "cancel" + message: feedback + ? `The user declined this tool call and said:\n${feedback}` + : input.decision === "cancel" ? "User cancelled tool execution." - : "User declined tool execution."), + : "User declined tool execution.", toolUseID: input.toolUseID, decisionClassification: "user_reject", ...(input.decision === "cancel" ? { interrupt: true } : {}), @@ -4815,6 +4898,7 @@ function formatClaudeUsageLimitWait(waitMs: number): string { } interface ActiveClaudeTurnContext { + retryBudgetFailure?: OrchestrationV2ProviderFailure; readonly queryPolicy: ClaudeRuntimeQueryPolicy; readonly input: ProviderAdapter.ProviderAdapterV2TurnInput; readonly autoCompactWindow: string; @@ -4892,6 +4976,8 @@ interface ActiveClaudeTurnContext { // Set while this turn's latest compaction status reports a failure, with // Claude's error text when it sent one. compactionFailure: { readonly message: string | undefined } | null; + // The title a `session_title_changed` frame reported; a `/rename` result applies it to the thread. + nativeSessionTitle: string | null; // Steers end segments whose usage is excluded from the final result; bill them too, oldest first. readonly steeredSegmentUsage: Array; // Set as finalization starts: a steer admitted later would stay pending @@ -4970,6 +5056,8 @@ interface ClaudeLiveQueryContext { claudeCodeVersion: string | undefined; permissionMode: PermissionMode; stopping: boolean; + // A turn that had not attached when the CLI exited fails its start with this cause. + exitCause: Cause.Cause | undefined; readonly subagentsFromEarlierProcesses: ReadonlySet; // Subagents this process still ran when a replacement took over; only its exit can end them. handedOffSubagents: ReadonlySet; @@ -4999,6 +5087,7 @@ interface ClaudeSubagentProjectionTarget { readonly childRootNodeId: OrchestrationV2ExecutionNode["id"]; readonly nextOrdinal: () => number; readonly toolCalls: Map; + readonly stopRequestedAt?: number | undefined; } /** Claude writes blocks separately with stop_reason null until the last. @@ -5786,6 +5875,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( new Map(), ); const goalsByNativeThread = new Map(); + // The title T3 last applied to each native session, so a turn renames it only when the + // thread title changed. A native `/rename` T3 never applied survives later turns. + const appliedSessionTitles = new Map(); const goalCheckedTurns = new Set(); // Background subagents can finish after root settlement; retain their registry for wake evidence and hydration. const sessionSubagentsByTaskId = yield* Ref.make(new Map()); @@ -5799,6 +5891,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( string, Deferred.Deferred >(); + // Claude reports a stopped workflow's members as failed; this is the only evidence it was Stop. + const workflowStopRequestedAt = new Map(); // Buffered idle outcomes must not settle a resumed child again. const appliedSubagentOutcomes = new WeakSet(); // Subagent frames can precede the task_started that registers their owner. @@ -6072,27 +6166,47 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( const runtimeContext = yield* Effect.context(); const runPromise = Effect.runPromiseWith(runtimeContext); - const emitProviderEvent = (event: ProviderAdapter.ProviderAdapterV2Event) => { - if (event.type === "provider_thread.updated") { - const budget = pdfBudgets.get(event.providerThread.nativeThreadRef?.nativeId ?? ""); - if (budget !== undefined) { - event = { - ...event, - providerThread: { - ...event.providerThread, - nativeMetadata: { - ...event.providerThread.nativeMetadata, + // Overlays state the adapter owns per native session onto persisted provider metadata. + const withAdapterNativeMetadata = ( + providerThread: OrchestrationV2ProviderThread, + nativeThreadId: string, + ): OrchestrationV2ProviderThread => { + const budget = pdfBudgets.get(nativeThreadId); + const hasAppliedTitle = appliedSessionTitles.has(nativeThreadId); + const recordedTitle = providerThread.nativeMetadata?.title; + if (budget === undefined && !hasAppliedTitle && recordedTitle === undefined) { + return providerThread; + } + return { + ...providerThread, + nativeMetadata: { + ...providerThread.nativeMetadata, + ...(budget === undefined + ? {} + : { claudeAttachmentBudget: { bytesUsed: budget.committed.bytes, pagesUsed: budget.committed.pages, }, - }, - }, - }; - } - const nativeSessionId = clearedNativeSessions.get( - event.providerThread.nativeThreadRef?.nativeId ?? "", - ); + }), + // A cleared title makes the next turn after a restart retry a rename that failed. + ...(hasAppliedTitle + ? { title: normalizeClaudeSessionTitle(appliedSessionTitles.get(nativeThreadId)) } + : recordedTitle === undefined + ? {} + : { title: null }), + }, + }; + }; + + const emitProviderEvent = (event: ProviderAdapter.ProviderAdapterV2Event) => { + if (event.type === "provider_thread.updated") { + const nativeThreadId = event.providerThread.nativeThreadRef?.nativeId ?? ""; + event = { + ...event, + providerThread: withAdapterNativeMetadata(event.providerThread, nativeThreadId), + }; + const nativeSessionId = clearedNativeSessions.get(nativeThreadId); if (nativeSessionId !== undefined) { event = { ...event, @@ -6220,6 +6334,51 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( }).pipe(Effect.withSpan("ClaudeAdapterV2.completeProviderRetry")); }); + // A hung request can take many minutes per attempt, so the budget spans the + // whole retry window rather than counting attempts. + const failExpiredProviderRetry = Effect.fn("ClaudeAdapterV2.failExpiredProviderRetry")( + function* (context: ActiveClaudeTurnContext, startedAt: DateTime.Utc) { + const isCurrentRetry = Effect.gen(function* () { + const retry = (yield* Ref.get(providerRetries)).get(context.providerTurnId); + return retry?.startedAt === startedAt && (yield* Ref.get(activeTurn)) === context + ? retry + : undefined; + }); + const retry = yield* isCurrentRetry; + // A user Stop already in flight keeps its interrupted outcome. + if ( + retry === undefined || + (yield* Ref.get(interruptedTurns)).has(context.providerTurnId) + ) + return; + context.retryBudgetFailure = { ...retry.failure, retryable: false }; + const liveQuery = yield* Ref.get(queryContext); + yield* interruptTurn({ + providerThread: context.input.providerThread, + providerTurnId: context.providerTurnId, + }).pipe( + Effect.timeoutOption(CLAUDE_STOP_CONFIRM_TIMEOUT_MS), + Effect.catch((cause) => + Effect.logWarning("orchestration-v2.claude-retry-budget-stop-failed", { cause }), + ), + ); + if (liveQuery === null || (yield* isCurrentRetry) === undefined) return; + // The CLI did not confirm the stop; settle the turn without it. + const ownedLiveQuery = yield* Ref.modify(queryContext, (current) => + current?.query === liveQuery.query ? [true, null] : [false, current], + ); + if (!ownedLiveQuery) return; + liveQuery.stopping = true; + yield* liveQuery.query.close.pipe( + Effect.timeoutOption(CLAUDE_STOP_CONFIRM_TIMEOUT_MS), + Effect.ignore, + ); + yield* finalizeActiveTurnAfterQueryExit(liveQuery); + yield* endSubagentCallsOfClosedQuery(liveQuery.nativeThreadId, "interrupted"); + yield* Deferred.succeed(liveQuery.closed, undefined); + }, + ); + const rememberProviderThread = /* Provider events include streaming deltas; avoid a span for each update. */ Effect.fnUntraced(function* (providerThread: OrchestrationV2ProviderThread) { @@ -6227,8 +6386,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( if (nativeThreadId === undefined || nativeThreadId === null) { return; } + const enriched = withAdapterNativeMetadata(providerThread, nativeThreadId); yield* Ref.update(lastProviderThreadByNativeThread, (current) => - new Map(current).set(nativeThreadId, providerThread), + new Map(current).set(nativeThreadId, enriched), ); }); @@ -6512,11 +6672,23 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( nativeThreadId: string, task: OrchestrationV2PendingBackgroundTask, ) { - yield* Ref.update(pendingBackgroundTasksByNativeThread, (current) => { - const roster = new Map(rosterForNativeThread(current, nativeThreadId)); - roster.set(task.taskId, task); - return new Map(current).set(nativeThreadId, roster); - }); + const startedMonitor = yield* Ref.modify( + pendingBackgroundTasksByNativeThread, + (current) => { + const roster = new Map(rosterForNativeThread(current, nativeThreadId)); + const startedMonitor = + task.kind === "monitor" && roster.get(task.taskId)?.kind !== "monitor"; + roster.set(task.taskId, task); + return [startedMonitor, new Map(current).set(nativeThreadId, roster)] as const; + }, + ); + if (startedMonitor) { + yield* addTaskIdsToNativeThreadSet( + awaitedBackgroundTasksByNativeThread, + nativeThreadId, + [task.taskId], + ); + } yield* rememberOpaqueTasks([task]); yield* markWakeEligibleOpaqueBackgroundTasks(nativeThreadId, [task.taskId]); }); @@ -7146,13 +7318,19 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( return; const memberKey = `${input.coordinator.childThreadId}:agent:${member.index}`; const previous = seen.get(memberKey); + const stopRequestedAt = workflowStopRequestedAt.get(taskId); const status = (member.state === "queued" || member.state === "running") && input.coordinator.task.status !== "running" ? input.coordinator.task.status : member.state === "queued" ? "pending" - : member.state; + : member.state === "failed" && + stopRequestedAt !== undefined && + // lastProgressAt (completedAt) is the last tick, often before Stop for a blocked tool. + previous?.status !== "failed" + ? "cancelled" + : member.state; const settled = status !== "pending" && status !== "running"; const prompt = member.prompt ?? member.label; const model = member.model ?? parentThread.modelSelection.model; @@ -7310,6 +7488,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( childRootNodeId, nextOrdinal: () => ++nextItemOrdinal, toolCalls, + stopRequestedAt, }; const endOpenToolCalls = Effect.fnUntraced(function* () { if (toolCalls.size === 0) return; @@ -8390,12 +8569,20 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( readonly toolResult: ClaudeToolResultContentBlock; readonly output: ClaudeNativeToolOutput; readonly completedAt: DateTime.Utc; + readonly stopped?: boolean; }) { const { toolCall } = input; const toolNonExecutionKind = claudeToolNonExecutionKind( input.message, input.toolResult.tool_use_id, ); + // Stop denial metadata distinguishes cancelled work from a real error + // that happens to arrive after the turn was interrupted. + const stopped = + input.stopped === true || + (toolNonExecutionKind === "user-rejected" && + input.context !== null && + (yield* Ref.get(interruptedTurns)).has(input.context.providerTurnId)); yield* stopTaskOutputTail(toolCall.nativeItemId); yield* emitToolCallArtifacts( buildToolCallArtifacts({ @@ -8414,7 +8601,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( toolNonExecutionKind === "cancelled" || toolNonExecutionKind === "interrupted" ? toolNonExecutionKind : isClaudeToolResultError(input.toolResult) - ? "failed" + ? stopped + ? "interrupted" + : "failed" : "completed", ...(toolNonExecutionKind === undefined ? {} : { toolNonExecutionKind }), startedAt: toolCall.startedAt, @@ -8509,6 +8698,11 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( toolResult, output, completedAt: at, + // A denial written before Stop (a declined approval) is not the Stop's. + stopped: + target.stopRequestedAt !== undefined && + field(line, "toolDenialKind") !== undefined && + DateTime.toEpochMillis(at) >= target.stopRequestedAt, }); } if (line.type !== "assistant") return false; @@ -8886,7 +9080,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( } }); - const finalizeActiveTurn = Effect.fnUntraced(function* (input: { + const finalizeActiveTurn = Effect.fnUntraced(function* (requested: { readonly context: ActiveClaudeTurnContext; readonly status: Extract< OrchestrationV2ProviderTurn["status"], @@ -8897,6 +9091,12 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( readonly threadDisposition?: "reusable" | "broken"; readonly result?: SDKResultMessage; }) { + // A turn stopped by the retry budget settles as failed with the retry's error. + const retryBudgetFailure = requested.context.retryBudgetFailure; + const input = + requested.status === "interrupted" && retryBudgetFailure !== undefined + ? { ...requested, status: "failed" as const, failure: retryBudgetFailure } + : requested; input.context.steersClosed = true; yield* reasoningDeltas.flushTurn(input.context.nativeTurnId); if (input.status === "completed") { @@ -9971,7 +10171,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( ? undefined : findToolCall(input.activeContext, message.tool_use_id); const command = - launch?.classification.itemType === "command_execution" + launch?.classification.itemType === "command_execution" || + launch?.toolName === "Monitor" ? firstStringInputField(launch.input, ["command"]) : undefined; yield* upsertPendingBackgroundTask(input.nativeThreadId, { @@ -10228,6 +10429,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( (yield* Ref.get(activeTurn)) ?? (yield* settledTurnForSubagentFrame(liveQuery.nativeThreadId, message)); if (context === null) { + if (isClaudeLocalCommandEcho(message)) { + return; + } yield* applyWorkflowProgressWithoutTurn(message); // Buffer wake evidence before clearing the task from the roster. if (message.type === "system" && message.subtype === "task_notification") { @@ -10754,12 +10958,20 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( updatedAt, }), }); + if (previous === undefined) { + yield* failExpiredProviderRetry(context, state.startedAt).pipe( + Effect.delay(PROVIDER_RETRY_BUDGET_MS), + Effect.forkIn(sessionScope), + ); + } return; } if (message.type === "assistant" && input.replayed !== true) { - const now = yield* DateTime.now; - yield* completeProviderRetry(context, now); + if (message.message.model !== "") { + const now = yield* DateTime.now; + yield* completeProviderRetry(context, now); + } // CLI-generated frames report zero usage, not context usage. if ( message.parent_tool_use_id === null && @@ -10969,6 +11181,35 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( } } + if ( + message.type === "system" && + (message.subtype as string) === "session_title_changed" && + "title" in message && + typeof message.title === "string" + ) { + context.nativeSessionTitle = normalizeClaudeSessionTitle(message.title); + } + + if ( + message.type === "result" && + message.subtype === "success" && + !message.is_error && + message.local_command === "rename" && + context.nativeSessionTitle !== null + ) { + const title = context.nativeSessionTitle; + context.nativeSessionTitle = null; + // The native session already has this title; the next turn must not send it back. + appliedSessionTitles.set(liveQuery.nativeThreadId, title); + if (title !== context.input.appThread.title) { + yield* emitProviderEvent({ + type: "app_thread.created", + driver: CLAUDE_PROVIDER, + appThread: { ...context.input.appThread, title }, + }); + } + } + if (message.type === "system" && message.subtype === "task_progress") { const progress = message.description.trim(); const isBackgroundTask = yield* hasPendingBackgroundTaskOnNativeThread( @@ -12022,14 +12263,17 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( const context = yield* Ref.get(activeTurn); if (acceptance === undefined || context === null) { yield* Effect.logWarning( - "Declined an MCP elicitation the approval card cannot answer.", + "Cancelled an MCP elicitation the approval card cannot answer.", { serverName: request.serverName, mode: request.mode, activeTurn: context !== null, }, ); - return { action: "decline" } satisfies ElicitationResult; + // No card was shown, so the user made no choice. MCP reserves + // "decline" for an explicit no; "cancel" tells the server the + // request was dismissed and may be asked another way. + return { action: "cancel" } satisfies ElicitationResult; } const grantKey = `${request.serverName}\u0000${request.message}`; if ((yield* Ref.get(mcpElicitationSessionGrants)).has(grantKey)) { @@ -12295,6 +12539,16 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( queryPolicy: Effect.Success>, ) { const mcpSession = yield* mcpSessions.read(turnInput.threadId); + // Claude Code defers its goal Stop hook only for its own background tasks, so a goal + // re-prompts in a loop while T3 work that will wake this thread is still running. + const onStop: HookCallback = () => + runPromise( + Effect.gen(function* () { + if (goalsByNativeThread.get(nativeThreadId)?.status !== "active") return {}; + const pendingWake = (yield* mcpSessions.read(turnInput.threadId))?.hasPendingWake; + return pendingWake !== undefined && (yield* pendingWake) ? { continue: false } : {}; + }), + ); const mcpOverrides = claudeMcpQueryOverrides({ mcpSession, readOnlySandbox: @@ -12469,6 +12723,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( canUseTool, onUserDialog, onElicitation, + onStop, supportedDialogKinds: ["resume_return"], ...(appendSystemPromptFileText === undefined ? {} : { appendSystemPromptFileText }), ...(toolSearchOverride === undefined ? {} : { toolSearchOverride }), @@ -12508,6 +12763,11 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( updated.add(nativeThreadId); return updated; }); + // A new session's spawn applied this turn's title. A resume keeps the session's stored + // title, so the last applied one stays authoritative until a turn compares again. + if (!shouldResume) { + appliedSessionTitles.set(nativeThreadId, turnInput.appThread.title); + } yield* resetBackgroundTaskStateForNativeThreadProcess(nativeThreadId, { status: "active", }); @@ -12529,6 +12789,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( claudeCodeVersion: undefined, permissionMode: queryOptions.permissionMode, stopping: false, + exitCause: undefined, handedOffSubagents: new Set(), subagentsFromEarlierProcesses: new Set( [...(yield* Ref.get(sessionSubagentsByTaskId)).values()].filter( @@ -12608,6 +12869,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( Effect.exit, Effect.flatMap( Effect.fnUntraced(function* (exit: ClaudeQueryStreamExit) { + if (exit._tag === "Failure") context.exitCause = exit.cause; const heldContext = yield* Ref.get(activeTurn); if ( (yield* Ref.get(queryContext))?.query === querySession && @@ -12707,6 +12969,12 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( const used = turnInput.providerThread.nativeMetadata?.claudeAttachmentBudget; pdfBudgets.set(nativeThreadId, makeClaudeAttachmentBudget(used)); } + if (!appliedSessionTitles.has(nativeThreadId)) { + const recordedTitle = turnInput.providerThread.nativeMetadata?.title; + if (recordedTitle !== undefined && recordedTitle !== null) { + appliedSessionTitles.set(nativeThreadId, recordedTitle); + } + } const [skills, queryPolicy] = yield* Effect.all( [ promptUuid === null @@ -12756,6 +13024,24 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( Effect.provideService(Path.Path, path), ); const querySession = yield* openQuery(turnInput, nativeThreadId, queryPolicy); + // `claude --resume` shows the native title. A failed or timed-out rename forgets the + // applied title so the next turn, even after a restart, sends it again. + if (appliedSessionTitles.get(nativeThreadId) !== turnInput.appThread.title) { + const renamed = yield* querySession.query + .renameSession(turnInput.appThread.title) + .pipe(Effect.timeout("10 seconds"), Effect.isSuccess); + if (renamed) { + appliedSessionTitles.set(nativeThreadId, turnInput.appThread.title); + } else { + appliedSessionTitles.delete(nativeThreadId); + } + const remembered = (yield* Ref.get(lastProviderThreadByNativeThread)).get( + nativeThreadId, + ); + if (remembered !== undefined) { + yield* rememberProviderThread(remembered); + } + } const autoCompactWindow = yield* resolveAutoCompactWindow( querySession.settingsCwd, turnInput.modelSelection, @@ -12803,6 +13089,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( rateLimitResetTimes: new Map(), latestAssistantRateLimit: null, compactionFailure: null, + nativeSessionTitle: null, steeredSegmentUsage: [], steersClosed: false, steeredLedgerUsage: { inputTokens: 0, outputTokens: 0 }, @@ -12837,6 +13124,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( return yield* new ProviderAdapter.ProviderAdapterProtocolError({ driver: CLAUDE_PROVIDER, detail: CLAUDE_QUERY_ENDED_BEFORE_TURN_DETAIL, + ...(querySession.exitCause === undefined + ? {} + : { cause: querySession.exitCause }), }); } if ( @@ -13519,14 +13809,22 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( function* (threadInput: ProviderAdapter.ProviderAdapterV2EnsureThreadInput) { const createdAt = yield* DateTime.now; const nativeThreadId = yield* queryRunner.allocateSessionId; - return makeProviderThread({ + if (threadInput.title !== undefined) { + appliedSessionTitles.set(nativeThreadId, threadInput.title); + } + const providerThread = makeProviderThread({ idAllocator, providerInstanceId: adapterOptions.instanceId, appThreadId: threadInput.threadId, providerSessionId: input.providerSessionId, nativeThreadId, + ...(threadInput.title !== undefined + ? { nativeMetadata: { title: normalizeClaudeSessionTitle(threadInput.title) } } + : {}), now: createdAt, }); + yield* rememberProviderThread(providerThread); + return providerThread; }, (effect, threadInput) => effect.pipe( @@ -13543,6 +13841,12 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( resumeThread: Effect.fn("ClaudeAdapterV2.resumeThread")( function* (threadInput: { readonly providerThread: OrchestrationV2ProviderThread }) { const nativeThreadId = threadInput.providerThread.nativeThreadRef?.nativeId; + if (nativeThreadId !== undefined && nativeThreadId !== null) { + const recordedTitle = threadInput.providerThread.nativeMetadata?.title; + if (recordedTitle !== undefined && recordedTitle !== null) { + appliedSessionTitles.set(nativeThreadId, recordedTitle); + } + } if ( nativeThreadId !== undefined && nativeThreadId !== null && @@ -13568,12 +13872,26 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( } } const updatedAt = yield* DateTime.now; - return { + const appliedTitle = + nativeThreadId !== undefined && nativeThreadId !== null + ? appliedSessionTitles.get(nativeThreadId) + : undefined; + const providerThread: OrchestrationV2ProviderThread = { ...threadInput.providerThread, providerSessionId: input.providerSessionId, status: "idle" as const, + ...(appliedTitle !== undefined + ? { + nativeMetadata: { + ...threadInput.providerThread.nativeMetadata, + title: normalizeClaudeSessionTitle(appliedTitle), + }, + } + : {}), updatedAt, }; + yield* rememberProviderThread(providerThread); + return providerThread; }, (effect, threadInput) => effect.pipe( @@ -13724,6 +14042,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( detail: "No running workflow task belongs to this provider thread", }); } + workflowStopRequestedAt.set(taskId, DateTime.toEpochMillis(yield* DateTime.now)); yield* liveQuery.query.stopTask(taskId).pipe( Effect.mapError( (cause) => @@ -13733,6 +14052,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( cause, }), ), + Effect.onError(() => Effect.sync(() => workflowStopRequestedAt.delete(taskId))), ); }), awaitBackgroundTasks: Effect.fn("ClaudeAdapterV2.awaitBackgroundTasks")( diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts index 76c10382b3cb8..eb70b52ee47f1 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts @@ -225,7 +225,15 @@ describe("CodexAdapterV2 runtime policy", () => { assert.equal(auto.approvalPolicy, "on-request"); assert.equal(auto.approvalsReviewer, "auto_review"); assert.equal(auto.sandboxPolicy?.type, "workspaceWrite"); - assert.equal(fullAccess.approvalPolicy, "never"); + assert.deepEqual(fullAccess.approvalPolicy, { + granular: { + mcp_elicitations: true, + request_permissions: false, + rules: false, + sandbox_approval: false, + skill_approval: false, + }, + }); assert.equal(fullAccess.approvalsReviewer, "user"); assert.equal(fullAccess.sandboxPolicy?.type, "dangerFullAccess"); }), @@ -657,6 +665,10 @@ function codexReplayPreamble(input: { readonly prompt: string; readonly sentPrompt?: string; readonly effort?: string; + readonly turnPolicy?: Pick< + typeof CodexClient.V2TurnStartParams.Type, + "approvalPolicy" | "sandboxPolicy" + >; }): Array { return [ { @@ -754,6 +766,7 @@ function codexReplayPreamble(input: { approvalPolicy: "never", approvalsReviewer: "user", sandboxPolicy: { type: "dangerFullAccess" }, + ...input.turnPolicy, summary: "auto", ...(input.effort === undefined ? {} : { effort: input.effort }), collaborationMode: { @@ -1603,6 +1616,135 @@ describe("CodexAdapterV2 post-settle continuation", () => { }; }); + it.effect.each([ + { runtimeMode: "full-access", turnIdKind: "present" }, + { runtimeMode: "auto-accept-edits", turnIdKind: "null" }, + ] as const)( + "handles MCP access approvals in $runtimeMode with a $turnIdKind turn id", + ({ runtimeMode, turnIdKind }) => + Effect.gen(function* () { + const nativeThreadId = "mcp-approval-thread"; + const nativeTurnId = "mcp-approval-turn"; + const prompt = "Read the dashboard in Chrome."; + const pending = yield* Queue.unbounded(); + const harness = yield* makeCodexReplayHarness( + makeCodexReplayTranscript({ + scenario: `mcp-approval-${turnIdKind}`, + entries: [ + ...codexReplayPreamble({ + nativeThreadId, + nativeTurnId, + prompt, + ...(runtimeMode === "full-access" + ? {} + : { + turnPolicy: { + approvalPolicy: "on-request", + sandboxPolicy: { type: "workspaceWrite" }, + }, + }), + }), + { + type: "emit_inbound", + frame: { + id: 0, + method: "mcpServer/elicitation/request", + params: { + threadId: nativeThreadId, + turnId: turnIdKind === "null" ? null : nativeTurnId, + serverName: "browser", + mode: "form", + message: "Allow ChatGPT to use Google Chrome?", + requestedSchema: { + type: "object", + properties: { approval: { type: "string", enum: ["once", "always"] } }, + required: ["approval"], + }, + }, + }, + }, + { + type: "expect_outbound", + frame: { id: 0, result: { action: "accept", content: { approval: "once" } } }, + }, + { + type: "emit_inbound", + frame: { + method: "turn/completed", + params: { + threadId: nativeThreadId, + turn: makeCodexReplayTurn({ id: nativeTurnId, status: "completed" }), + }, + }, + }, + ], + }), + (event) => + event.type === "runtime_request.updated" && event.runtimeRequest.status === "pending" + ? Queue.offer(pending, event.runtimeRequest) + : Effect.void, + ); + const turnInput = makeCodexTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now: yield* DateTime.now, + attemptId: RunAttemptId.make(`mcp-approval-${turnIdKind}-attempt`), + text: prompt, + }); + yield* harness.runtime.startTurn({ + ...turnInput, + runtimePolicy: { ...turnInput.runtimePolicy, runtimeMode }, + }); + if (runtimeMode === "full-access") { + const request = yield* Effect.raceFirst( + harness.firstTerminal.pipe(Effect.as(undefined)), + Queue.take(pending), + ); + assert.isUndefined(request, "Full access must not wait for an MCP approval"); + assert.deepEqual( + harness.terminalEvents().map((event) => event.status), + ["completed"], + ); + return; + } + const request = yield* Effect.raceFirst( + Queue.take(pending), + harness.firstTerminal.pipe(Effect.andThen(Effect.die("MCP approval was not presented"))), + ); + assert.equal(request.kind, "mcp-elicitation"); + const approvals = harness.events.flatMap((event) => + event.type === "turn_item.updated" && + event.turnItem.type === "approval_request" && + event.turnItem.requestId === request.id + ? [event.turnItem] + : [], + ); + assert.deepEqual( + approvals.map((approval) => [approval.threadId, approval.appName, approval.prompt]), + [[harness.threadId, "Google Chrome", "Allow ChatGPT to use Google Chrome?"]], + ); + yield* harness.runtime.respondToRuntimeRequest({ + requestId: request.id, + decision: "accept", + }); + yield* harness.firstTerminal; + assert.deepEqual( + harness.terminalEvents().map((event) => event.status), + ["completed"], + ); + }).pipe( + Effect.scoped, + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ); + it.effect.each(["native", "ordinary"] as const)( "resumes Codex without a prompt only for an existing conversation (%s)", (mode) => @@ -2006,13 +2148,13 @@ describe("CodexAdapterV2 post-settle continuation", () => { url: "http://127.0.0.1:43123/mcp", tool_timeout_sec: 3_900, http_headers: { Authorization: "Bearer secret-codex-token" }, - disabled_tools: ["t3_thread_wait"], + disabled_tools: ["t3_thread_wait", "delegate_task"], }, "mcp_servers.t3-code-direct": { url: "http://127.0.0.1:43123/mcp", tool_timeout_sec: 3_900, http_headers: { Authorization: "Bearer secret-codex-token" }, - enabled_tools: ["t3_thread_wait"], + enabled_tools: ["t3_thread_wait", "delegate_task"], omit_tools_from: ["code_mode", "deferred"], }, }, @@ -3312,19 +3454,27 @@ describe("CodexAdapterV2 post-settle continuation", () => { ), ); - it.effect.each(["failed", "interrupted", "Stop"] as const)( - "settles unfinished compaction on %s without changing completed compaction", - (outcome) => + it.effect.each([ + { itemType: "contextCompaction", outcome: "failed" }, + { itemType: "contextCompaction", outcome: "interrupted" }, + { itemType: "contextCompaction", outcome: "Stop" }, + { itemType: "webSearch", outcome: "completed" }, + { itemType: "webSearch", outcome: "failed" }, + { itemType: "webSearch", outcome: "interrupted" }, + { itemType: "webSearch", outcome: "Stop" }, + ] as const)( + "settles unfinished $itemType on $outcome without changing completed items", + ({ itemType, outcome }) => Effect.gen(function* () { - const nativeThreadId = "compact-thread"; - const nativeTurnId = "compact-turn"; + const nativeThreadId = "unfinished-item-thread"; + const nativeTurnId = "unfinished-item-turn"; const error = { - message: "Remote compaction stream disconnected", + message: "Remote item stream disconnected", codexErrorInfo: "other", additionalDetails: null, }; const transcript = makeCodexReplayTranscript({ - scenario: `automatic-compaction-${outcome}`, + scenario: `unfinished-${itemType}-${outcome}`, entries: [ ...codexReplayPreamble({ nativeThreadId, nativeTurnId, prompt: "Continue." }), ...( @@ -3341,7 +3491,11 @@ describe("CodexAdapterV2 post-settle continuation", () => { params: { threadId: nativeThreadId, turnId: nativeTurnId, - item: { type: "contextCompaction", id }, + item: { + type: itemType, + id, + ...(itemType === "webSearch" ? { query: "T3 Code", action: null } : {}), + }, ...timestamps, }, }, @@ -3381,13 +3535,13 @@ describe("CodexAdapterV2 post-settle continuation", () => { ]), ], }); - const compactionStarted = yield* Deferred.make(); + const itemStarted = yield* Deferred.make(); const harness = yield* makeCodexReplayHarness(transcript, (event) => event.type === "turn_item.updated" && event.turnItem.nativeItemRef?.nativeId === "unfinished-item" && event.turnItem.status === "running" && event.turnItem.providerTurnId !== null - ? Deferred.succeed(compactionStarted, event.turnItem.providerTurnId) + ? Deferred.succeed(itemStarted, event.turnItem.providerTurnId) : Effect.void, ); yield* TestClock.setTime(1782622445000); @@ -3396,12 +3550,12 @@ describe("CodexAdapterV2 post-settle continuation", () => { threadId: harness.threadId, providerThread: harness.providerThread, now: yield* DateTime.now, - attemptId: RunAttemptId.make(`compact-${outcome}-attempt`), + attemptId: RunAttemptId.make(`unfinished-${itemType}-${outcome}-attempt`), text: "Continue.", }), ); if (outcome === "Stop") { - const providerTurnId = yield* Deferred.await(compactionStarted); + const providerTurnId = yield* Deferred.await(itemStarted); const interrupt = yield* harness.runtime .interruptTurn({ providerThread: harness.providerThread, providerTurnId }) .pipe(Effect.exit); @@ -3409,7 +3563,8 @@ describe("CodexAdapterV2 post-settle continuation", () => { } yield* harness.firstTerminal; const items = harness.events.flatMap((event) => - event.type === "turn_item.updated" && event.turnItem.type === "compaction" + event.type === "turn_item.updated" && + event.turnItem.type === (itemType === "webSearch" ? "web_search" : "compaction") ? [event.turnItem] : [], ); @@ -3417,13 +3572,26 @@ describe("CodexAdapterV2 post-settle continuation", () => { const unfinished = items.filter( (item) => item.nativeItemRef?.nativeId === "unfinished-item", ); - const status = outcome === "failed" ? "failed" : "interrupted"; + const status = + outcome === "completed" ? "cancelled" : outcome === "failed" ? "failed" : "interrupted"; assert.deepEqual( unfinished.map((item) => item.status), ["running", status], ); assert.equal(unfinished[0]?.id, unfinished[1]?.id); assert.deepEqual(unfinished[1]?.startedAt, DateTime.makeUnsafe(1782622443000)); + if (unfinished[1]?.type === "web_search") { + assert.deepEqual(unfinished[1].patterns, ["T3 Code"]); + assert.deepEqual( + harness.events.flatMap((event) => + event.type === "node.updated" && + event.node.nativeItemRef?.nativeId === "unfinished-item" + ? [event.node.status] + : [], + ), + ["running", status], + ); + } assert.deepEqual( unfinished[1]?.completedAt, DateTime.makeUnsafe(outcome === "Stop" ? 1782622445000 : 1782622450000), @@ -3433,8 +3601,14 @@ describe("CodexAdapterV2 post-settle continuation", () => { ["running", "completed"], ); assert.deepEqual(completed[1]?.startedAt, DateTime.makeUnsafe(1782622441000)); - assert.deepEqual(completed[1]?.completedAt, DateTime.makeUnsafe(1782622442000)); - assert.equal(harness.terminalEvents()[0]?.status, status); + assert.deepEqual( + completed[1]?.completedAt, + DateTime.makeUnsafe(itemType === "webSearch" ? 1782622445000 : 1782622442000), + ); + assert.equal( + harness.terminalEvents()[0]?.status, + outcome === "Stop" ? "interrupted" : outcome, + ); if (outcome === "failed") { assert.equal(harness.terminalEvents()[0]?.failure?.message, error.message); } @@ -4596,27 +4770,30 @@ describe("CodexAdapterV2 post-settle continuation", () => { const scenario = "codex-provider-retry-stall"; const nativeThreadId = `native-${scenario}-thread`; const nativeTurnId = `native-${scenario}-turn`; + const retryNotice = (attempt: number) => ({ + type: "emit_inbound" as const, + label: `error/retry-${attempt}`, + frame: { + method: "error", + params: { + threadId: nativeThreadId, + turnId: nativeTurnId, + willRetry: true, + error: { + message: `Reconnecting... ${attempt}/5`, + additionalDetails: "exceeded retry limit, last status: 429 Too Many Requests", + codexErrorInfo: { responseStreamDisconnected: { httpStatusCode: 429 } }, + }, + }, + }, + }); const transcript = makeCodexReplayTranscript({ scenario, entries: [ ...codexReplayPreamble({ nativeThreadId, nativeTurnId, prompt: "Continue." }), - { - type: "emit_inbound", - label: "error/retry", - frame: { - method: "error", - params: { - threadId: nativeThreadId, - turnId: nativeTurnId, - willRetry: true, - error: { - message: "Reconnecting... 1/5", - additionalDetails: "exceeded retry limit, last status: 429 Too Many Requests", - codexErrorInfo: { responseStreamDisconnected: { httpStatusCode: 429 } }, - }, - }, - }, - }, + retryNotice(1), + // A later notice in the same retry window must not push the deadline back. + { ...retryNotice(2), afterMs: 5 * 60 * 1000 }, { type: "expect_outbound", label: "turn/interrupt", @@ -4675,7 +4852,7 @@ describe("CodexAdapterV2 post-settle continuation", () => { terminal.failure.message, "exceeded retry limit, last status: 429 Too Many Requests", ); - assert.equal(terminal.retry?.attempt, 1); + assert.equal(terminal.retry?.attempt, 2); assert.equal(terminal.threadDisposition, "reusable"); }).pipe( Effect.provide( @@ -6989,6 +7166,15 @@ describe("CodexAdapterV2 post-settle continuation", () => { expectedClass: "usage_limit", }, { name: "retry", code: "usageLimitExceeded", notification: true, expectedClass: "usage_limit" }, + // A proxy such as CLIProxyAPI strips the snapshot, so only the error text carries the reset. + { + name: "message-reset", + code: "usageLimitExceeded", + message: + "You’ve hit your usage limit. Visit https://chatgpt.com/codex/settings/usage to purchase more credits or try again at Nov 11th, 2033 3:37 PM.", + notification: false, + expectedClass: "usage_limit", + }, { name: "exhausted-429", code: "responseTooManyFailedAttempts", @@ -7003,6 +7189,37 @@ describe("CodexAdapterV2 post-settle continuation", () => { notification: false, expectedClass: "provider_error", }, + ...([false, true] as const).map( + (notification) => + ({ + name: `disconnected-${notification ? "notification" : "completion"}`, + code: "responseStreamDisconnected", + errorInfo: { responseStreamDisconnected: { httpStatusCode: null } }, + notification, + expectedClass: "transport_error", + }) as const, + ), + { + name: "connection-failed", + code: "responseStreamConnectionFailed", + errorInfo: { responseStreamConnectionFailed: { httpStatusCode: null } }, + notification: false, + expectedClass: "transport_error", + }, + { + name: "http-connection-failed", + code: "httpConnectionFailed", + errorInfo: { httpConnectionFailed: { httpStatusCode: null } }, + notification: false, + expectedClass: "transport_error", + }, + { + name: "http-provider-failure", + code: "httpConnectionFailed", + errorInfo: { httpConnectionFailed: { httpStatusCode: 503 } }, + notification: false, + expectedClass: "provider_error", + }, { name: "chatgpt-workspace-limit", code: "other", @@ -7037,6 +7254,14 @@ describe("CodexAdapterV2 post-settle continuation", () => { expectedCode: "subscription_sharing_usage_limit_exceeded", expectedMessage: "Usage settings", }, + { + name: "terminal-disconnection", + code: "responseStreamDisconnected", + notification: false, + terminalMessage: "Connection lost.", + terminalErrorInfo: { responseStreamDisconnected: { httpStatusCode: null } }, + expectedClass: "transport_error", + }, { name: "sharing-code-mention-retry", code: "responseStreamDisconnected", @@ -7046,6 +7271,71 @@ describe("CodexAdapterV2 post-settle continuation", () => { willRetry: true, expectedClass: "transport_error", }, + ...( + [ + { + name: "generic-disconnection", + status: null, + terminalMessage: "Connection lost.", + expectedClass: "transport_error", + }, + { + name: "generic-replacement", + status: null, + terminalMessage: "A different failure.", + expectedClass: "provider_error", + }, + { + name: "generic-http-status", + status: 503, + terminalMessage: "Connection lost.", + expectedClass: "provider_error", + }, + { + name: "generic-truncated-replacement", + status: null, + details: `${"Connection lost. ".repeat(300)}Network cause.`, + terminalMessage: `${"Connection lost. ".repeat(300)}Different cause.`, + expectedClass: "provider_error", + }, + { + name: "generic-terminal-status", + status: null, + terminalMessage: "Connection lost.", + terminalErrorInfo: { responseStreamDisconnected: { httpStatusCode: 503 } }, + expectedClass: "provider_error", + }, + { + name: "generic-exhaustion", + status: null, + terminalMessage: "Connection lost.", + terminalErrorInfo: { responseTooManyFailedAttempts: { httpStatusCode: null } }, + completionErrorInfo: { responseTooManyFailedAttempts: { httpStatusCode: null } }, + expectedClass: "transport_error", + }, + { + name: "generic-blank-details", + status: null, + details: " ", + terminalMessage: "Provider stopped this request.", + expectedClass: "transport_error", + }, + { + name: "generic-exhaustion-status", + status: null, + terminalMessage: "Connection lost.", + terminalErrorInfo: { responseTooManyFailedAttempts: { httpStatusCode: 503 } }, + completionErrorInfo: { responseTooManyFailedAttempts: { httpStatusCode: null } }, + expectedClass: "provider_error", + }, + ] as const + ).map((scenario) => ({ + ...scenario, + code: "responseStreamDisconnected" as const, + errorInfo: { responseStreamDisconnected: { httpStatusCode: scenario.status } }, + notification: true, + details: "details" in scenario ? scenario.details : "Connection lost.", + })), ] as const)("classifies Codex terminal failures from $name evidence", (scenario) => Effect.gen(function* () { const nativeThreadId = `native-limit-${scenario.name}`; @@ -7169,7 +7459,10 @@ describe("CodexAdapterV2 post-settle continuation", () => { params: { threadId: nativeThreadId, turnId: nativeTurnId, - willRetry: scenario.name === "retry" || "willRetry" in scenario, + willRetry: + scenario.name === "retry" || + "willRetry" in scenario || + "terminalMessage" in scenario, error: { message, codexErrorInfo: errorInfo, @@ -7185,6 +7478,27 @@ describe("CodexAdapterV2 post-settle continuation", () => { }, ] : []), + ...("terminalMessage" in scenario + ? [ + { + type: "emit_inbound" as const, + label: "error/terminal", + frame: { + method: "error", + params: { + threadId: nativeThreadId, + turnId: nativeTurnId, + willRetry: false, + error: { + message: scenario.terminalMessage, + codexErrorInfo: + "terminalErrorInfo" in scenario ? scenario.terminalErrorInfo : "other", + }, + }, + }, + }, + ] + : []), { type: "emit_inbound", label: "turn/completed", @@ -7195,12 +7509,24 @@ describe("CodexAdapterV2 post-settle continuation", () => { turn: { ...makeCodexReplayTurn({ id: nativeTurnId, status: "failed" }), error: { - message: scenario.name === "replacement" ? "A different failure." : message, - ...(scenario.notification && - scenario.name !== "matching-details" && - !("details" in scenario) - ? {} - : { codexErrorInfo: errorInfo }), + message: + "terminalMessage" in scenario + ? scenario.terminalMessage + : scenario.name === "replacement" + ? "A different failure." + : message, + ...("terminalMessage" in scenario + ? { + codexErrorInfo: + "completionErrorInfo" in scenario + ? scenario.completionErrorInfo + : "other", + } + : scenario.notification && + scenario.name !== "matching-details" && + !("details" in scenario) + ? {} + : { codexErrorInfo: errorInfo }), }, }, }, @@ -7309,6 +7635,16 @@ describe("CodexAdapterV2 post-settle continuation", () => { if (terminal?.status !== "failed") return; assert.equal(terminal.failure.class, scenario.expectedClass); assert.equal(terminal.threadDisposition, "reusable"); + if (scenario.name === "message-reset") + assert.equal( + terminal.failure.resetAt, + DateTime.formatIso( + DateTime.makeZonedUnsafe( + { year: 2033, month: 11, day: 11, hour: 15, minute: 38 }, + { timeZone: DateTime.zoneMakeLocal(), adjustForTimeZone: true }, + ), + ), + ); if (scenario.name === "deferred-reset" || scenario.name === "first-request-reset") assert.equal(terminal.failure.resetAt, resetAt); if (scenario.name === "newer-reset-during-read") @@ -7358,34 +7694,66 @@ describe("CodexAdapterV2 post-settle continuation", () => { // regression: #17501 Codex 429 retry exhaustion classifies as a usage limit it.effect.each([ { + code: "responseTooManyFailedAttempts", notificationStatus: 500, completionStatus: 429, willRetry: false, expectedClass: "usage_limit", }, { + code: "responseTooManyFailedAttempts", notificationStatus: 429, completionStatus: 500, willRetry: true, expectedClass: "provider_error", }, { + code: "responseTooManyFailedAttempts", notificationStatus: 429, completionStatus: null, willRetry: false, expectedClass: "usage_limit", }, { + code: "responseTooManyFailedAttempts", notificationStatus: 429, completionStatus: null, willRetry: true, expectedClass: "usage_limit", }, + { + code: "responseStreamDisconnected", + notificationStatus: null, + completionStatus: 503, + willRetry: false, + expectedClass: "provider_error", + }, + { + code: "responseStreamDisconnected", + notificationStatus: 503, + completionStatus: null, + willRetry: false, + expectedClass: "transport_error", + }, + { + code: "responseStreamDisconnected", + notificationStatus: null, + completionStatus: 503, + willRetry: true, + expectedClass: "provider_error", + }, + { + code: "responseStreamDisconnected", + notificationStatus: null, + completionStatus: null, + willRetry: false, + expectedClass: "transport_error", + }, ] as const)( - "honors completion HTTP status from $notificationStatus to $completionStatus (retry: $willRetry)", - ({ notificationStatus, completionStatus, expectedClass, willRetry }) => + "honors $code completion HTTP status from $notificationStatus to $completionStatus (retry: $willRetry)", + ({ code, notificationStatus, completionStatus, expectedClass, willRetry }) => Effect.gen(function* () { - const name = `${notificationStatus}-${completionStatus}-${willRetry}`; + const name = `${code}-${notificationStatus}-${completionStatus}-${willRetry}`; const nativeThreadId = `native-exhaustion-status-${name}`; const nativeTurnId = `turn-exhaustion-status-${name}`; const message = "Provider stopped this request."; @@ -7406,7 +7774,7 @@ describe("CodexAdapterV2 post-settle continuation", () => { error: { message, codexErrorInfo: { - responseTooManyFailedAttempts: { httpStatusCode: notificationStatus }, + [code]: { httpStatusCode: notificationStatus }, }, additionalDetails, }, @@ -7425,7 +7793,7 @@ describe("CodexAdapterV2 post-settle continuation", () => { error: { message, codexErrorInfo: { - responseTooManyFailedAttempts: { httpStatusCode: completionStatus }, + [code]: { httpStatusCode: completionStatus }, }, }, }, @@ -7449,9 +7817,16 @@ describe("CodexAdapterV2 post-settle continuation", () => { assert.equal(terminal?.status, "failed"); if (terminal?.status !== "failed") return; assert.equal(terminal.failure.class, expectedClass); - assert.equal(terminal.failure.code, "responseTooManyFailedAttempts"); + assert.equal(terminal.failure.code, code); assert.equal(terminal.threadDisposition, "reusable"); - const notificationClass = notificationStatus === 429 ? "usage_limit" : "provider_error"; + const notificationClass = + code === "responseTooManyFailedAttempts" + ? notificationStatus === 429 + ? "usage_limit" + : "provider_error" + : notificationStatus === null + ? "transport_error" + : "provider_error"; assert.equal( terminal.failure.message, expectedClass === notificationClass ? additionalDetails : message, @@ -7755,6 +8130,124 @@ describe("CodexAdapterV2 post-settle continuation", () => { ], }); + it.effect("resumes an unloaded native thread and retries an MCP resource read", () => + Effect.scoped( + Effect.gen(function* () { + const scenario = "mcp-resource-thread-recovery"; + const nativeThreadId = `native-${scenario}`; + const preamble = codexReplayPreamble({ + nativeThreadId, + nativeTurnId: "unused-turn", + prompt: "unused-prompt", + }).slice(0, 5); + + const resourceResult = { + contents: [ + { + uri: MCP_APP_RESOURCE, + mimeType: "text/html;profile=mcp-app", + text: MCP_APP_HTML, + _meta: { ui: { csp: { connectDomains: ["https://api.weather.test"] } } }, + }, + ], + }; + + const transcript = makeCodexReplayTranscript({ + scenario, + entries: [ + ...preamble, + { + type: "expect_outbound", + label: "initial MCP resource read", + frame: { + id: 3, + method: "mcpServer/resource/read", + params: { + threadId: nativeThreadId, + server: "weather", + uri: MCP_APP_RESOURCE, + }, + }, + }, + { + type: "emit_inbound", + label: "native thread is unloaded", + frame: { + id: 3, + error: { + code: -32600, + message: `thread not found: ${nativeThreadId}`, + }, + }, + }, + { + type: "expect_outbound", + label: "resume unloaded native thread", + frame: { + id: 4, + method: "thread/resume", + params: { + threadId: nativeThreadId, + excludeTurns: true, + config: CodexAdapterV2.CODEX_THREAD_CONFIG, + }, + }, + }, + { + type: "emit_inbound", + label: "native thread resumed", + frame: { + id: 4, + result: { + thread: { id: nativeThreadId, updatedAt: 1782622450 }, + }, + }, + }, + { + type: "expect_outbound", + label: "retry MCP resource read", + frame: { + id: 5, + method: "mcpServer/resource/read", + params: { + threadId: nativeThreadId, + server: "weather", + uri: MCP_APP_RESOURCE, + }, + }, + }, + { + type: "emit_inbound", + label: "MCP resource read succeeds", + frame: { id: 5, result: resourceResult }, + }, + ], + }); + + const harness = yield* makeCodexReplayHarness(transcript); + const mcpApps = harness.runtime.mcpApps; + assert.ok(mcpApps, "Codex runtime should expose MCP apps"); + + const result = yield* mcpApps.readResource({ + providerThread: harness.providerThread, + server: "weather", + uri: MCP_APP_RESOURCE, + }); + + assert.deepEqual(result, resourceResult); + }).pipe( + Effect.provide( + Layer.mergeAll( + IdAllocator.layer, + McpProviderSessions.layer, + NodeServices.layer, + NodeCrypto.layer, + ), + ), + ), + ), + ); + it.effect("captures an MCP app's resource and holds the turn open until it lands", () => Effect.gen(function* () { const appCaptured = yield* Deferred.make(); diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts index 06cb3079dad8b..041a3fc78258e 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts @@ -27,6 +27,8 @@ import { import { codexRateLimitsToUpdate, mergeCodexRateLimits, + codexMessageResetAt, + codexTimeZone, codexUsageLimitResetAt, type CodexRateLimitSnapshot, } from "../../provider/codexUsageLimits.ts"; @@ -146,6 +148,7 @@ import { makeProviderFailure, makeProviderFailureTurnItem, makeProviderRetryTurnItem, + PROVIDER_RETRY_BUDGET_MS, } from "@t3tools/provider-core/server/failure"; import { turnScopedSelectionTransition } from "@t3tools/provider-core/server/selectionTransition"; import { @@ -164,8 +167,6 @@ const CODEX_PROVIDER = ProviderDriverKind.make("codex"); const CODEX_CAPACITY_MAX_RETRIES = 5; // How long Stop waits for Codex to end the turn before the user is offered Force stop. const CODEX_STOP_CONFIRM_TIMEOUT_MS = 10_000; -// Codex can announce a retry and never resume. Recovered retries resume model output within ~75s. -const CODEX_PROVIDER_RETRY_STALL_TIMEOUT = "10 minutes"; export const CODEX_DRIVER_KIND = CODEX_PROVIDER; export const CODEX_DEFAULT_INSTANCE_ID = defaultInstanceIdForDriver(CODEX_DRIVER_KIND); @@ -380,6 +381,14 @@ function isCodexActiveWriterError(error: unknown): boolean { ); } +function isCodexThreadNotFoundError(error: unknown): boolean { + return ( + isCodexAppServerRequestError(error) && + error.code === -32600 && + error.errorMessage.startsWith("thread not found:") + ); +} + function codexTimestamp(seconds: number | null | undefined): DateTime.Utc { return seconds === null || seconds === undefined ? DateTime.nowUnsafe() @@ -759,7 +768,16 @@ function codexRuntimeModeTurnDefaults(runtimeMode: RuntimeMode): { }; case "full-access": return { - approvalPolicy: "never", + // `never` also rejects browser and app access prompts before the client sees them. + approvalPolicy: { + granular: { + mcp_elicitations: true, + request_permissions: false, + rules: false, + sandbox_approval: false, + skill_approval: false, + }, + }, approvalsReviewer: "user", sandboxPolicy: { type: "dangerFullAccess", @@ -1197,6 +1215,35 @@ function codexUsageLimitError( ); } +function codexTerminalFailureClass( + errorInfo: CodexErrorInfo | null | undefined, + code: string | null, +) { + if (codexUsageLimitError(code, errorInfo)) return "usage_limit"; + if (typeof errorInfo === "object" && errorInfo !== null) { + const transport = + "responseStreamDisconnected" in errorInfo + ? errorInfo.responseStreamDisconnected + : "responseStreamConnectionFailed" in errorInfo + ? errorInfo.responseStreamConnectionFailed + : "httpConnectionFailed" in errorInfo + ? errorInfo.httpConnectionFailed + : undefined; + if (transport !== undefined && transport.httpStatusCode == null) return "transport_error"; + } + return "provider_error"; +} + +function codexTerminalErrorNeedsEvidence( + errorInfo: CodexErrorInfo | null | undefined, + code: string | null, +) { + return ( + code === "other" || + (code === "responseTooManyFailedAttempts" && codexExhaustionHttpStatus(errorInfo) === null) + ); +} + /** * ChatGPT sharing reports its own reason inside Codex's generic turn error, so * the code it names replaces Codex's, and clients pick a recovery from it. Its @@ -1220,11 +1267,15 @@ function withCodexManagedFailure( function failureResetAt( failure: OrchestrationV2ProviderFailure, snapshot: CodexRateLimitSnapshot | undefined, + /** Read the failure text against when Codex reported it; omit to use the snapshot only. */ + text?: { readonly failedAt: DateTime.Utc; readonly zone: DateTime.TimeZone }, ) { // Sharing caps have no reset timestamp in the ordinary Codex allowance snapshot. - return classifyCodexManagedError(failure.code)?.usageLimit - ? null - : codexUsageLimitResetAt(snapshot); + if (classifyCodexManagedError(failure.code)?.usageLimit) return null; + return ( + codexUsageLimitResetAt(snapshot) ?? + (text === undefined ? null : codexMessageResetAt(failure.message, text.failedAt, text.zone)) + ); } interface ActiveCodexTurnContext { @@ -1235,6 +1286,7 @@ interface ActiveCodexTurnContext { latestProviderFailure?: { readonly nativeMessage: string; readonly nativeCode: string | null; + readonly errorInfo?: CodexErrorInfo | null; readonly failure: OrchestrationV2ProviderFailure; }; readonly nativeStartReady?: Deferred.Deferred; @@ -1253,6 +1305,7 @@ interface ActiveCodexTurnContext { readonly subagent: CodexSubagentThreadContext | null; readonly startedAt: DateTime.Utc; readonly runningCompactionItemIds: Set; + readonly runningWebSearchItems: Map; // Modern approval requests omit paths; keep the kinds without retaining patch bodies. readonly fileChangeKindsByItem: Map; readonly messagePhases: Map; @@ -1266,6 +1319,8 @@ interface ActiveCodexProviderRetry { readonly context?: ActiveCodexTurnContext; readonly nativeMessage: string; readonly nativeCode: string | null; + readonly nativeDetails?: string; + readonly errorInfo?: CodexErrorInfo | null; readonly retry: OrchestrationV2ProviderRetry; readonly failure: OrchestrationV2ProviderFailure; readonly startedAt: DateTime.Utc; @@ -1443,12 +1498,12 @@ export const CODEX_THREAD_CONFIG = { "features.concurrent_reasoning_summaries": true, } as const; -// T3 waits can block for an hour; keep Codex's MCP timeout above that. +// request_secret can wait an hour; keep Codex's MCP timeout above that. const CODEX_T3_MCP_TOOL_TIMEOUT_SEC = 65 * 60; // Holds T3 tools Codex must call directly, never from Code Mode. Calls arrive as // `t3-code-direct.`; t3McpToolPresentation.ts must keep resolving that form. const CODEX_T3_DIRECT_MCP_SERVER = "t3-code-direct"; -const CODEX_DIRECT_ONLY_T3_TOOL = "t3_thread_wait"; +const CODEX_DIRECT_ONLY_T3_TOOLS = ["t3_thread_wait", "delegate_task"]; function codexThreadRuntimeParams(input: { readonly threadId: ThreadId | null; @@ -1482,12 +1537,12 @@ function codexThreadRuntimeParams(input: { ? {} : { // Override only T3's servers so launch-argument MCP servers survive. - "mcp_servers.t3-code": { ...t3Server, disabled_tools: [CODEX_DIRECT_ONLY_T3_TOOL] }, - // Code Mode runs a blocking wait as a nested call, and the model keeps working - // around it. A second, direct-only registration makes it a plain tool call. + "mcp_servers.t3-code": { ...t3Server, disabled_tools: CODEX_DIRECT_ONLY_T3_TOOLS }, + // Code Mode runs a blocking wait or delegate wait as a nested call, and the model + // keeps polling it. A second, direct-only registration makes it a plain tool call. [`mcp_servers.${CODEX_T3_DIRECT_MCP_SERVER}`]: { ...t3Server, - enabled_tools: [CODEX_DIRECT_ONLY_T3_TOOL], + enabled_tools: CODEX_DIRECT_ONLY_T3_TOOLS, omit_tools_from: ["code_mode", "deferred"], }, }), @@ -1901,6 +1956,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( input.accountOverlay, ), }); + const codexZone = codexTimeZone(resolvedRuntime?.environment ?? adapterOptions.environment); const additionalContextByThread = yield* Ref.make( new Map>(), ); @@ -2419,6 +2475,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( fileChangeKindsByItem: new Map(), messagePhases: new Map(), runningCompactionItemIds: new Set(), + runningWebSearchItems: new Map(), }; const nativeThreadId = input.turnInput.providerThread.nativeThreadRef?.nativeId; const continuation = @@ -2487,19 +2544,25 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( ); }); - // Streaming updates repeatedly await this context, including retries before registration. - const awaitActiveTurn = Effect.fnUntraced(function* ( - nativeTurnId: string, + // Native events can arrive before their turn registers, so lookups retry briefly. + const awaitRegisteredTurn = Effect.fnUntraced(function* ( + lookup: Effect.Effect, attemptsRemaining = 1_000, ): Effect.fn.Return { - const context = (yield* Ref.get(activeTurns)).get(nativeTurnId); + const context = yield* lookup; if (context !== undefined || attemptsRemaining <= 0) { return context; } yield* Effect.yieldNow; - return yield* awaitActiveTurn(nativeTurnId, attemptsRemaining - 1); + return yield* awaitRegisteredTurn(lookup, attemptsRemaining - 1); }); + // Streaming updates repeatedly await this context; avoid a span for each update. + const awaitActiveTurn = (nativeTurnId: string) => + awaitRegisteredTurn( + Ref.get(activeTurns).pipe(Effect.map((turns) => turns.get(nativeTurnId))), + ); + const resolveItemEventContext = /* Provider events include streaming deltas; avoid a span for each update. */ Effect.fnUntraced(function* (nativeTurnId: string) { @@ -2742,7 +2805,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( CodexAppServerInputStreamEndedError: () => Effect.succeed({ terminated: true }), // Unloading kills terminals, so "thread not found" means nothing remains to stop. CodexAppServerRequestError: (error) => - error.code === -32600 && error.errorMessage.startsWith("thread not found:") + isCodexThreadNotFoundError(error) ? Effect.succeed({ terminated: true }) : Effect.fail(error), }), @@ -3048,6 +3111,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( fileChangeKindsByItem: new Map(), messagePhases: new Map(), runningCompactionItemIds: new Set(), + runningWebSearchItems: new Map(), }; beginTurnTokenUsage(activeContext); endSessionApprovalsOnModeChange(activeContext); @@ -4092,10 +4156,11 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( function* (input: { readonly context: ActiveCodexTurnContext; readonly item: CodexWebSearchItem; - readonly completed: boolean; + readonly status: "running" | "completed" | "cancelled" | "failed" | "interrupted"; + readonly updatedAt?: DateTime.Utc; }) { - const updatedAt = yield* DateTime.now; - const completedAt = input.completed ? updatedAt : null; + const updatedAt = input.updatedAt ?? (yield* DateTime.now); + const completedAt = input.status === "running" ? null : updatedAt; const nodeId = idAllocator.derive.nodeFromProviderItem({ driver: CODEX_PROVIDER, nativeItemId: input.item.id, @@ -4106,7 +4171,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( }); const { ordinal, startedAt } = yield* resolveItemPosition(input.context, input.item.id); const patterns = webSearchPatterns(input.item); - const status = input.completed ? "completed" : "running"; + const { status } = input; const node: OrchestrationV2ExecutionNode = { id: nodeId, threadId: input.context.projectionThreadId, @@ -4846,6 +4911,8 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( context, nativeItemId: `turn-plan:${payload.turnId}`, status: "active", + // Each update is a complete snapshot; nothing later completes the item. + completed: true, ...(explanation === undefined ? {} : { explanation }), steps, }); @@ -5124,13 +5191,17 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( context.latestProviderFailure = { nativeMessage: payload.error.message, nativeCode: notificationCode, + ...(payload.error.codexErrorInfo === undefined + ? {} + : { errorInfo: payload.error.codexErrorInfo }), failure: withCodexManagedFailure( makeProviderFailure({ message: payload.error.additionalDetails?.trim() || payload.error.message, code: notificationCode, - class: codexUsageLimitError(notificationCode, payload.error.codexErrorInfo) - ? "usage_limit" - : "provider_error", + class: codexTerminalFailureClass( + payload.error.codexErrorInfo, + notificationCode, + ), }), payload.error, ), @@ -5171,6 +5242,12 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( ...(previous?.context === undefined ? {} : { context: previous.context }), nativeMessage: payload.error.message, nativeCode: code, + nativeDetails: additionalDetails + ? (payload.error.additionalDetails ?? additionalDetails) + : payload.error.message, + ...(payload.error.codexErrorInfo === undefined + ? {} + : { errorInfo: payload.error.codexErrorInfo }), retry, failure, startedAt: previous?.startedAt ?? updatedAt, @@ -5182,7 +5259,14 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( return updated; }); yield* failStalledProviderRetry(context, state, payload.error.codexErrorInfo).pipe( - Effect.delay(CODEX_PROVIDER_RETRY_STALL_TIMEOUT), + // Every notice aims at the same deadline, so repeated notices cannot extend it. + Effect.delay( + Math.max( + 0, + PROVIDER_RETRY_BUDGET_MS - + (DateTime.toEpochMillis(updatedAt) - DateTime.toEpochMillis(state.startedAt)), + ), + ), Effect.forkIn(scope, { startImmediately: true }), ); yield* emitProviderEvent({ @@ -5408,10 +5492,11 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( return; } + context.runningWebSearchItems.set(payload.item.id, payload.item); const artifacts = yield* buildWebSearchArtifacts({ context, item: payload.item, - completed: false, + status: "running", }); yield* emitProviderEvent({ type: "node.updated", @@ -5592,10 +5677,11 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( } if (payload.item.type === "webSearch") { + context.runningWebSearchItems.delete(payload.item.id); const artifacts = yield* buildWebSearchArtifacts({ context, item: payload.item, - completed: true, + status: "completed", }); yield* emitProviderEvent({ type: "node.updated", @@ -5982,29 +6068,34 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( yield* client.handleServerRequest("mcpServer/elicitation/request", (payload) => Effect.gen(function* () { - if (toMcpElicitationResponse(payload, "accept").action !== "accept") { - yield* Effect.logWarning("Declined an unsupported MCP elicitation.", { + const acceptance = toMcpElicitationResponse(payload, "accept"); + if (acceptance.action !== "accept") { + yield* Effect.logWarning("Cancelled an unsupported MCP elicitation.", { serverName: payload.serverName, mode: payload.mode, }); return { - action: "decline", + action: "cancel", } satisfies CodexSchema.McpServerElicitationRequestResponse; } const context = payload.turnId === undefined || payload.turnId === null - ? undefined + ? yield* awaitRegisteredTurn(findActiveTurnByNativeThreadId(payload.threadId)) : yield* awaitActiveTurn(payload.turnId); if (context === undefined) { yield* Effect.logWarning( - "Declined an MCP elicitation without an active Codex turn context.", + "Cancelled an MCP elicitation without an active Codex turn context.", { serverName: payload.serverName }, ); return { - action: "decline", + action: "cancel", } satisfies CodexSchema.McpServerElicitationRequestResponse; } + if (context.input.runtimePolicy.runtimeMode === "full-access") { + return acceptance; + } + const nativeRequestId = payload.mode === "url" ? payload.elicitationId @@ -6257,6 +6348,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( function* (input: { readonly context: ActiveCodexTurnContext; readonly status: OrchestrationV2ProviderTurn["status"]; + readonly completedAt: DateTime.Utc; readonly failureMessage?: string; readonly failureErrorInfo?: CodexErrorInfo; readonly providerRetry?: ActiveCodexProviderRetry; @@ -6268,19 +6360,20 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( input.failureErrorInfo === undefined ? undefined : codexErrorInfoCode(input.failureErrorInfo); - const failureClass = codexUsageLimitError(failureCode ?? null, input.failureErrorInfo) - ? "usage_limit" - : "provider_error"; - // The notification carries the richer message, so it wins when the - // completion repeats it. An explicit exhaustion status on the - // completion still decides the class; a missing one is not evidence. - const exhaustionStatus = codexExhaustionHttpStatus(input.failureErrorInfo); - const failure = + const failureClass = codexTerminalFailureClass( + input.failureErrorInfo, + failureCode ?? null, + ); + // Retry exhaustion without a status cannot replace the notification's evidence. + const completionHasClass = + failureCode != null && + !codexTerminalErrorNeedsEvidence(input.failureErrorInfo, failureCode); + let failure = previousFailure !== undefined && (input.failureMessage === undefined || input.failureMessage === previousFailure.nativeMessage) && (failureCode === undefined || failureCode === previousFailure.nativeCode) && - (exhaustionStatus === null || previousFailure.failure.class === failureClass) + (!completionHasClass || previousFailure.failure.class === failureClass) ? previousFailure.failure : withCodexManagedFailure( makeProviderFailure({ @@ -6290,6 +6383,28 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( }), input.failureMessage, ); + const retry = input.providerRetry; + const notification = input.context.latestProviderFailure; + // Codex 0.164 reports an exhausted stream error as `other` with the same details. + if ( + codexTerminalErrorNeedsEvidence(input.failureErrorInfo, failure.code) && + failure.class === "provider_error" && + (notification === undefined || + notification.failure.class === "transport_error" || + codexTerminalErrorNeedsEvidence( + notification.errorInfo, + notification.nativeCode, + )) && + ((retry !== undefined && + input.failureMessage !== undefined && + input.failureMessage === retry.nativeDetails && + codexTerminalFailureClass(retry.errorInfo, retry.nativeCode) === + "transport_error") || + (notification?.failure.class === "transport_error" && + input.failureMessage === notification.nativeMessage)) + ) { + failure = { ...failure, class: "transport_error" }; + } return { type: "turn.terminal", driver: CODEX_PROVIDER, @@ -6305,7 +6420,10 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( failure.class === "usage_limit" ? { ...failure, - resetAt: failureResetAt(failure, yield* Ref.get(rateLimitSnapshot)), + resetAt: failureResetAt(failure, yield* Ref.get(rateLimitSnapshot), { + failedAt: input.completedAt, + zone: codexZone, + }), } : failure, ...(input.providerRetry === undefined @@ -6413,6 +6531,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( readonly context: ActiveCodexTurnContext; readonly nativeTurnId: string; readonly status: OrchestrationV2ProviderTurn["status"]; + readonly completedAt: DateTime.Utc; readonly failureMessage?: string; readonly failureErrorInfo?: CodexErrorInfo; readonly providerRetry?: ActiveCodexProviderRetry; @@ -6873,14 +6992,34 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( ); yield* terminalizeMcpAppCaptures(input.context, input.nativeTurnId); } - const dynamicToolStatus: "cancelled" | "interrupted" | "failed" = + const unfinishedToolStatus: "cancelled" | "interrupted" | "failed" = input.status === "interrupted" || input.status === "failed" ? input.status : "cancelled"; + // A completed turn does not establish whether an unfinished search succeeded. + for (const item of input.context.runningWebSearchItems.values()) { + const artifacts = yield* buildWebSearchArtifacts({ + context: input.context, + item, + status: unfinishedToolStatus, + updatedAt: input.completedAt, + }); + yield* emitProviderEvent({ + type: "node.updated", + driver: CODEX_PROVIDER, + node: artifacts.node, + }); + yield* emitProviderEvent({ + type: "turn_item.updated", + driver: CODEX_PROVIDER, + turnItem: artifacts.turnItem, + }); + } + input.context.runningWebSearchItems.clear(); yield* terminalizeRunningDynamicTools( input.context, input.nativeTurnId, - dynamicToolStatus, + unfinishedToolStatus, input.completedAt, input.status === "interrupted" || input.status === "failed", ); @@ -7224,6 +7363,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( fileChangeKindsByItem: new Map(), messagePhases: new Map(), runningCompactionItemIds: new Set(), + runningWebSearchItems: new Map(), }; const providerTurn = { id: context.providerTurnId, @@ -7617,17 +7757,45 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( detail, payload: cause, }); + // Codex unloads idle threads; MCP requests against one fail until it is resumed. + const withLoadedThread = ( + providerThread: OrchestrationV2ProviderThread, + threadId: string, + request: Effect.Effect, + ) => + request.pipe( + Effect.catchIf(isCodexThreadNotFoundError, () => + readMcpSession(providerThread.appThreadId).pipe( + Effect.flatMap((mcpSession) => + resumeNativeThread(threadId, { + excludeTurns: true, + ...codexThreadRuntimeParams({ + threadId: providerThread.appThreadId, + mcpSession, + modelSelection: input.modelSelection, + runtimePolicy: input.runtimePolicy, + }), + }), + ), + Effect.andThen(request), + ), + ), + ); const mcpApps: ProviderAdapter.ProviderAdapterV2McpApps = { listTools: Effect.fn("CodexAdapterV2.listTools")( function* (toolsInput) { const threadId = yield* getNativeThreadId(toolsInput.providerThread); - const response = yield* ensureInitialized.pipe( - Effect.andThen( - client.request("mcpServerStatus/list", { - threadId, - serverName: toolsInput.server, - detail: "toolsAndAuthOnly", - }), + const response = yield* withLoadedThread( + toolsInput.providerThread, + threadId, + ensureInitialized.pipe( + Effect.andThen( + client.request("mcpServerStatus/list", { + threadId, + serverName: toolsInput.server, + detail: "toolsAndAuthOnly", + }), + ), ), ); const server = response.data.find((entry) => entry.name === toolsInput.server); @@ -7638,14 +7806,18 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( callTool: Effect.fn("CodexAdapterV2.callTool")( function* (callInput) { const threadId = yield* getNativeThreadId(callInput.providerThread); - const response = yield* ensureInitialized.pipe( - Effect.andThen( - client.request("mcpServer/tool/call", { - threadId, - server: callInput.server, - tool: callInput.tool, - arguments: callInput.arguments as Schema.Json, - }), + const response = yield* withLoadedThread( + callInput.providerThread, + threadId, + ensureInitialized.pipe( + Effect.andThen( + client.request("mcpServer/tool/call", { + threadId, + server: callInput.server, + tool: callInput.tool, + arguments: callInput.arguments as Schema.Json, + }), + ), ), ); return { @@ -7662,13 +7834,17 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( readResource: Effect.fn("CodexAdapterV2.readResource")( function* (readInput) { const threadId = yield* getNativeThreadId(readInput.providerThread); - const response = yield* ensureInitialized.pipe( - Effect.andThen( - client.request("mcpServer/resource/read", { - threadId, - server: readInput.server, - uri: readInput.uri, - }), + const response = yield* withLoadedThread( + readInput.providerThread, + threadId, + ensureInitialized.pipe( + Effect.andThen( + client.request("mcpServer/resource/read", { + threadId, + server: readInput.server, + uri: readInput.uri, + }), + ), ), ); return { contents: response.contents }; diff --git a/apps/server/src/orchestration-v2/Adapters/FxAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/FxAdapterV2.test.ts index 7a39e5468cfb6..6e21a3f4428e1 100644 --- a/apps/server/src/orchestration-v2/Adapters/FxAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/FxAdapterV2.test.ts @@ -277,7 +277,11 @@ describe("FxAdapterV2", () => { runtimeMode: "auto", frames: [ ...initialize, - outbound("session/new", { cwd: "", mcpServers: "" }), + outbound("session/new", { + cwd: "", + mcpServers: "", + _meta: { fx: { profileMcpServers: true } }, + }), answer("session/new", fxSession({ model: FX_DEFAULT_MODEL, modes })), ...setConfig( "mode", @@ -298,7 +302,12 @@ describe("FxAdapterV2", () => { loadSessionId: SESSION_ID, frames: [ ...initialize, - outbound("session/load"), + outbound("session/load", { + sessionId: SESSION_ID, + cwd: "", + mcpServers: "", + _meta: { fx: { profileMcpServers: true } }, + }), answer("session/load", fxSession({ model: NAMED_MODEL, modes: FX_0_0_13_MODES })), // Config id `model`, never fx's `provider`, and never T3's "default". ...setConfig( diff --git a/apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts index acf2f628f5126..89458b1deeeda 100644 --- a/apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts @@ -43,6 +43,7 @@ import { TestClock } from "effect/testing"; import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"; import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions"; import { buildRuntimeInstructions } from "@t3tools/provider-core/server/runtimeInstructions"; +import { htmlRenderFromToolItem } from "@t3tools/shared/toolOutput"; import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; import * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests"; import { @@ -677,11 +678,27 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => { const { runtime, thread } = yield* resumed([ out("session.prompt", { sessionID: SESSION, text: "" }), promptAccepted, + event("session.retry.scheduled", { + sessionID: SESSION, + assistantMessageID: "msg_0eb735d5b001oAFVeY5jz3WD4Z", + attempt: 2, + at: 2_001, + error, + }), event("session.execution.failed", { sessionID: SESSION, error }), ]); - const terminal = yield* terminalOf(runtime).pipe(Effect.forkScoped); + const collected = yield* runtime.events.pipe( + Stream.takeUntil((event) => event.type === "turn.terminal"), + Stream.runCollect, + Effect.forkScoped, + ); yield* runtime.startTurn(turnInput(thread)); - const ended = yield* Fiber.join(terminal); + const seen = yield* Fiber.join(collected); + assert.deepInclude(errorRows(seen)[0], { + status: "running", + failure: { class: failureClass, code: error.type, message: error.message, retryable: true }, + }); + const ended = seen.find((event) => event.type === "turn.terminal"); assert.equal(ended?.status, "failed"); assert.deepEqual(ended?.failure, { class: failureClass, @@ -4854,6 +4871,147 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => { }), ); + it.effect("shows each page a Code Mode script published, whatever the script returned", () => + Effect.gen(function* () { + const mcpSessions = yield* McpProviderSessions.McpProviderSessions; + yield* Effect.addFinalizer(() => mcpSessions.clear(threadId)); + const server = "t3-code-thread_opencode2-adapter"; + const pages = [ + { html: "

orbits

", title: "Orbits", height: 480 }, + { html: "

scale

", title: "Scale", height: 1490 }, + ]; + const references = pages.map((page, index) => ({ + attachmentId: `thread_opencode2-adapter-0000000${index}-0000-4000-8000-000000000000-html`, + title: page.title, + height: page.height, + })); + yield* Effect.forEach(pages, (page, index) => + mcpSessions.recordHtmlRender(threadId, page, references[index]!), + ); + const tool = { sessionID: SESSION, assistantMessageID: "msg_assistant", id: "call-execute" }; + const { runtime, thread } = yield* resumed([ + out("session.prompt", { sessionID: SESSION, text: "" }), + promptAccepted, + event("session.tool.input.started", { ...tool, name: "execute" }), + event("session.tool.called", { ...tool, input: { code: "`, + ); + const field = page.locator("#host #field"); + const box = (await field.boundingBox())!; + await page.mouse.click(box.x + box.width / 2, box.y + box.height / 2); + await ServerBrowserPage.type(page, { text: "shadow" }); + expect(await field.inputValue()).toBe("shadow"); + + // Refused for now: focus inside a frame cannot be checked from the top document. + const inner = page.frameLocator("iframe").locator("#inner"); + await inner.click(); + await expect(ServerBrowserPage.type(page, { text: "frame" })).rejects.toMatchObject({ + tag: "PreviewAutomationTargetNotEditableError", + }); + expect(await inner.inputValue()).toBe(""); + }); + it("shows dotted fields as dots wherever their value appears, keeping text values and refs", async () => { await page.setContent(`
password:
@@ -591,7 +628,7 @@ describe("server browser snapshot screenshot", () => { const screenshotOf = async (html: string) => { const page = await context.newPage(); await page.setContent(html); - const { screenshot, fullScreenshot } = await ServerBrowserPage.snapshot({ + const input = { page, cdp: await context.newCDPSession(page), renderScale: 2, @@ -599,7 +636,8 @@ describe("server browser snapshot screenshot", () => { consoleEntries: [], networkEntries: [], actionTimeline: [], - }); + }; + const { screenshot, fullScreenshot } = await ServerBrowserPage.snapshot(input); // The server has no DOM types, so the decoder runs as page source. const points = [ [0.15, 0.75], @@ -618,7 +656,7 @@ describe("server browser snapshot screenshot", () => { Array.from(context.getImageData(x * bitmap.width, y * bitmap.height, 1, 1).data), ); })()`)) as Array<[number, number, number, number]>; - return { screenshot, fullScreenshot, pixels }; + return { screenshot, fullScreenshot, pixels, input }; }; const colorOf = ([red, green, blue]: readonly [number, number, number, number]) => blue - red > 100 ? "blue" : red - blue > 100 ? "red" : `rgb(${red}, ${green}, ${blue})`; @@ -633,13 +671,20 @@ describe("server browser snapshot screenshot", () => { }); it("captures a plain page at the shrunk size with its content in place", async () => { - const { screenshot, fullScreenshot, pixels } = await screenshotOf(halves("")); + const { screenshot, fullScreenshot, pixels, input } = await screenshotOf(halves("")); + const metadata = await ServerBrowserPage.snapshotMetadata(input); expect({ size: [screenshot.width, screenshot.height], fullSize: [fullScreenshot?.width, fullScreenshot?.height], + metadataSize: metadata.screenshot, colors: pixels.map(colorOf), - }).toEqual({ size: [1280, 800], fullSize: [2560, 1600], colors: ["blue", "red", "red"] }); + }).toEqual({ + size: [1280, 800], + fullSize: [2560, 1600], + metadataSize: { width: 1280, height: 800 }, + colors: ["blue", "red", "red"], + }); }); it("records the destination of a finite entrance animation without waiting on infinite ones", async () => { @@ -765,6 +810,52 @@ describe("server browser drag", () => { }); }); +describe("server browser action timeout", () => { + type StepOptions = { readonly timeout?: number }; + it.each([ + { + action: "hover", + act: (page: Page) => ServerBrowserPage.hover(page, { selector: "#a", timeoutMs: 9_000 }), + last: ["#a.hover 1"], + }, + { + action: "drag", + act: (page: Page) => + ServerBrowserPage.drag(page, { source: "#a", target: "#b", timeoutMs: 9_000 }), + last: ["#a.dragTo 1", "#b.boundingBox 1"], + }, + ])("gives the $action's steps one shared timeout", async ({ act, last }) => { + let now = 0; + using _now = vi.spyOn(Date, "now").mockImplementation(() => now); + const steps: Array = []; + const box = { x: 0, y: 0, width: 20, height: 20 }; + const page = { + locator: (selector: string) => { + const record = (step: string, options: StepOptions) => + steps.push(`${selector}.${step} ${options.timeout}`); + return { + // Each step that finds the target takes 5 of the 9 seconds. + scrollIntoViewIfNeeded: async (options: StepOptions) => { + record("scrollIntoViewIfNeeded", options); + now += 5_000; + }, + boundingBox: async (options: StepOptions) => { + record("boundingBox", options); + now += 5_000; + return box; + }, + hover: async (options: StepOptions) => record("hover", options), + dragTo: async (_target: unknown, options: StepOptions) => record("dragTo", options), + }; + }, + } as unknown as Page; + + await act(page); + + expect(steps).toEqual(["#a.scrollIntoViewIfNeeded 9000", "#a.boundingBox 4000", ...last]); + }); +}); + describe("server browser click", () => { it("clicks a target the page replaced while it was being scrolled into view", async () => { let scrolls = 0; @@ -860,6 +951,55 @@ describe("server browser evaluate", () => { }); }); +describe("server browser type", () => { + // As on a desktop tab whose Playwright utility world never answers. + const stalledPage = (focusAnswer: () => Promise, typed: string[]) => + ({ + locator: () => ({ last: () => ({ count: () => new Promise(() => {}) }) }), + evaluate: focusAnswer, + keyboard: { insertText: async (text: string) => void typed.push(text) }, + }) as unknown as Page; + + it("types into the focused field when Playwright's utility world never answers", async () => { + vi.useFakeTimers(); + try { + const typed: string[] = []; + let outcome: unknown = "pending"; + const page = stalledPage(async () => true, typed); + void ServerBrowserPage.type(page, { text: "hello", timeoutMs: 300 }).then( + () => (outcome = "typed"), + (error: unknown) => (outcome = error), + ); + await vi.advanceTimersByTimeAsync(300); + expect(outcome).toBe("typed"); + expect(typed).toEqual(["hello"]); + } finally { + vi.useRealTimers(); + } + }); + + it("stops at its timeout without typing when the page never answers", async () => { + vi.useFakeTimers(); + try { + const typed: string[] = []; + let outcome: unknown = "pending"; + const page = stalledPage(() => new Promise(() => {}), typed); + void ServerBrowserPage.type(page, { text: "hello", timeoutMs: 300 }).then( + () => (outcome = "typed"), + (error: unknown) => (outcome = error), + ); + await vi.advanceTimersByTimeAsync(300); + expect(outcome).toMatchObject({ + tag: "PreviewAutomationTimeoutError", + detail: { timeoutMs: 300 }, + }); + expect(typed).toEqual([]); + } finally { + vi.useRealTimers(); + } + }); +}); + describe("server browser unresponsive script", () => { it("leaves a page that answers alone", async () => { const send = vi.fn(async (_method: string) => ({})); diff --git a/apps/server/src/preview/ServerBrowserPage.ts b/apps/server/src/preview/ServerBrowserPage.ts index bcfbc296a2cb0..ac37b9914ef76 100644 --- a/apps/server/src/preview/ServerBrowserPage.ts +++ b/apps/server/src/preview/ServerBrowserPage.ts @@ -14,6 +14,7 @@ import { type PreviewAutomationSelectInput, type PreviewAutomationSelectResult, type PreviewAutomationSnapshot, + type PreviewAutomationSnapshotMetadata, type PreviewAutomationSnapshotStage, type PreviewAutomationTypeInput, type PreviewAutomationUploadInput, @@ -483,6 +484,37 @@ const pngSize = (base64: string) => { return { width: header.readUInt32BE(16), height: header.readUInt32BE(20) }; }; +export const jpegSize = (base64: string) => { + const bytes = Buffer.from(base64, "base64"); + if (bytes.length < 4 || bytes.readUInt16BE(0) !== 0xffd8) + throw new Error("Invalid JPEG screenshot."); + let offset = 2; + while (offset < bytes.length) { + if (bytes[offset++] !== 0xff) throw new Error("Invalid JPEG screenshot."); + while (bytes[offset] === 0xff) offset++; + const marker = bytes[offset++]; + if (marker === undefined || marker === 0xd9 || marker === 0xda) break; + if (marker === 0x01 || (marker >= 0xd0 && marker <= 0xd7)) continue; + if (offset + 2 > bytes.length) break; + const length = bytes.readUInt16BE(offset); + if (length < 2 || offset + length > bytes.length) break; + if (marker >= 0xc0 && marker <= 0xcf && ![0xc4, 0xc8, 0xcc].includes(marker)) { + if (length < 7) break; + const height = bytes.readUInt16BE(offset + 3); + const width = bytes.readUInt16BE(offset + 5); + if (width && height) return { width, height }; + break; + } + offset += length; + } + throw new Error("Invalid JPEG screenshot."); +}; + +const scaledSize = (size: { readonly width: number; readonly height: number }, scale: number) => + scale < 1 + ? { width: Math.round(size.width * scale), height: Math.round(size.height * scale) } + : size; + /** * A page's display ratio and CSS viewport, read in an isolated world since page script can * reassign `devicePixelRatio`, `innerWidth`, and `innerHeight` in its own, and its page zoom. @@ -634,7 +666,8 @@ const capture = async ( input: SnapshotInput, deadline: number, stages: SnapshotStages, -): Promise => { + metadataOnly: boolean, +) => { const { running } = stages; const track = async
(stage: SnapshotStage, operation: () => Promise) => { running.set(stage, (running.get(stage) ?? 0) + 1); @@ -672,15 +705,21 @@ const capture = async ( > >, ), + // A cheap real frame still sizes the screenshot; computing it from metrics drifted from the PNG. track("screenshot", () => - captureViewport(input.cdp, { format: "png" }).then(async (full) => - scale < 1 - ? { - data: await shrinkPng(input.cdp, full, scale), - full: input.fullScreenshot ? full : undefined, - } - : { data: full, full: undefined }, - ), + metadataOnly + ? captureViewport(input.cdp, { format: "jpeg", quality: 10 }).then((data) => ({ + data, + full: undefined, + })) + : captureViewport(input.cdp, { format: "png" }).then(async (full) => + scale < 1 + ? { + data: await shrinkPng(input.cdp, full, scale), + full: input.fullScreenshot ? full : undefined, + } + : { data: full, full: undefined }, + ), ), ]); void pageAndScreenshot.catch(constVoid); @@ -731,11 +770,10 @@ const capture = async ( consoleEntries: [...input.consoleEntries], networkEntries: [...input.networkEntries], actionTimeline: [...input.actionTimeline], - screenshot: { - mimeType: "image/png", - data, - ...pngSize(data), - }, + // Reports the size the PNG path would have shrunk to. + screenshot: metadataOnly + ? scaledSize(jpegSize(data), scale) + : { mimeType: "image/png" as const, data, ...pngSize(data) }, ...(full !== undefined ? { fullScreenshot: { data: full, ...pngSize(full) } } : {}), }; }; @@ -743,7 +781,7 @@ const capture = async ( /** Snapshots begun per page; observers' snapshots aren't queued, so two can overlap. */ const snapshotTurns = new WeakMap(); -export const snapshot = async (input: SnapshotInput): Promise => { +const runSnapshot = async (input: SnapshotInput, metadataOnly: boolean) => { const deadline = Date.now() + SNAPSHOT_TIMEOUT_MS; const stages: SnapshotStages = { running: new Map(), timedOut: new Set() }; let timer: ReturnType | undefined; @@ -753,11 +791,11 @@ export const snapshot = async (input: SnapshotInput): Promise + const captured = capture(input, deadline, stages, metadataOnly).catch((cause: unknown) => Date.now() < deadline && snapshotTurns.get(input.page) === turn && !(cause instanceof Error && cause.message === VALUE_TOO_LONG) - ? capture(input, deadline, stages) + ? capture(input, deadline, stages, metadataOnly) : Promise.reject(cause), ); try { @@ -769,6 +807,12 @@ export const snapshot = async (input: SnapshotInput): Promise + runSnapshot(input, false) as Promise; + +export const snapshotMetadata = (input: SnapshotInput) => + runSnapshot(input, true) as Promise; + /** Return when a dialog opens; waiting for the action to finish would block until dialog resolution. */ export const click = async ( page: Page, @@ -852,6 +896,76 @@ const clickTarget = async ( return point; }; +type FocusCandidate = { + readonly tagName: string; + readonly type?: string; + readonly disabled?: boolean; + readonly readOnly?: boolean; + readonly isContentEditable?: boolean; + readonly shadowRoot?: { readonly activeElement: FocusCandidate | null } | null; + readonly focus?: () => void; + getRootNode(): { readonly activeElement?: FocusCandidate | null }; + contains(other: FocusCandidate): boolean; +}; + +// Serialized into the page's main world, so it must not reference module scope. +const focusTextTarget = (target: FocusCandidate | null) => { + let element = + target ?? + (globalThis as unknown as { document: { activeElement: FocusCandidate | null } }).document + .activeElement; + if (target === null) { + while (element?.shadowRoot?.activeElement) element = element.shadowRoot.activeElement; + // Focus inside a frame cannot be checked from here, and an editable frame would pass as contentEditable. + if (element && ["IFRAME", "FRAME", "FENCEDFRAME", "OBJECT", "EMBED"].includes(element.tagName)) + return false; + } + if (element == null) return false; + // Require an enabled, focused text target to avoid losing input or sending it to the previous field. + const nonText = [ + "button", + "checkbox", + "color", + "file", + "hidden", + "image", + "radio", + "range", + "reset", + "submit", + ]; + const textControl = + element.tagName === "TEXTAREA" || + (element.tagName === "INPUT" && !nonText.includes(element.type ?? "text")); + if (!(textControl || element.isContentEditable) || element.disabled || element.readOnly) { + return false; + } + element.focus?.(); + const active = element.getRootNode().activeElement; + return active != null && (active === element || element.contains(active)); +}; + +// A `*:focus` locator needs Playwright's utility world, which some desktop tabs never get. +const focusedTextField = async (page: Page, timeout: number) => { + let timer: ReturnType | undefined; + const expired = new Promise((_, reject) => { + timer = setTimeout( + () => + reject( + new ServerBrowserOperationError({ + tag: "PreviewAutomationTimeoutError", + message: `The page did not report its focused element within ${timeout}ms, so no text was typed.`, + detail: { timeoutMs: timeout }, + }), + ), + timeout, + ); + }); + return await Promise.race([page.evaluate(focusTextTarget, null), expired]).finally(() => + clearTimeout(timer), + ); +}; + export const type = async (page: Page, input: PreviewAutomationTypeInput) => { const timeout = input.timeoutMs ?? DEFAULT_TIMEOUT_MS; const locator = targetLocator(page, input); @@ -859,46 +973,10 @@ export const type = async (page: Page, input: PreviewAutomationTypeInput) => { await locator.fill(input.text, { timeout }); return; } - // A shadow host also matches :focus; use its innermost focused descendant. - const target = locator ?? page.locator("*:focus").last(); const focused = - (locator !== null || (await target.count()) > 0) && - (await target.evaluate( - (element) => { - // Require an enabled, focused text target to avoid losing input or sending it to the previous field. - const control = element as unknown as { - readonly type?: string; - readonly disabled?: boolean; - readonly readOnly?: boolean; - readonly isContentEditable?: boolean; - readonly focus?: () => void; - }; - const nonText = [ - "button", - "checkbox", - "color", - "file", - "hidden", - "image", - "radio", - "range", - "reset", - "submit", - ]; - const textControl = - element.tagName === "TEXTAREA" || - (element.tagName === "INPUT" && !nonText.includes(control.type ?? "text")); - if (!(textControl || control.isContentEditable) || control.disabled || control.readOnly) { - return false; - } - control.focus?.(); - const root = element.getRootNode() as { readonly activeElement?: typeof element | null }; - const active = root.activeElement; - return active != null && (active === element || element.contains(active)); - }, - undefined, - { timeout }, - )); + locator === null + ? await focusedTextField(page, timeout) + : await locator.evaluate(focusTextTarget, undefined, { timeout }); if (focused !== true) { throw new ServerBrowserOperationError({ tag: "PreviewAutomationTargetNotEditableError", @@ -917,14 +995,15 @@ export const hover = async ( input: PreviewAutomationHoverInput, pointer: PointerReporter = noPointer, ) => { - const timeout = input.timeoutMs ?? DEFAULT_TIMEOUT_MS; + const deadline = Date.now() + (input.timeoutMs ?? DEFAULT_TIMEOUT_MS); + const remaining = () => Math.max(1, deadline - Date.now()); const locator = targetLocator(page, input); - await pointer(await targetPoint(page, locator, input, () => timeout), "move"); + await pointer(await targetPoint(page, locator, input, remaining), "move"); if (locator === null) { await page.mouse.move(input.x ?? 0, input.y ?? 0); return; } - await locator.hover({ timeout }); + await locator.hover({ timeout: remaining() }); }; export const select = async ( @@ -967,17 +1046,18 @@ export const drag = async ( input: PreviewAutomationDragInput, pointer: PointerReporter = noPointer, ) => { - const timeout = input.timeoutMs ?? DEFAULT_TIMEOUT_MS; + const deadline = Date.now() + (input.timeoutMs ?? DEFAULT_TIMEOUT_MS); + const remaining = () => Math.max(1, deadline - Date.now()); const source = targetLocator(page, { locator: input.source })!; const target = targetLocator(page, { locator: input.target })!; - await pointer(await targetPoint(page, source, {}, () => timeout), "move"); + await pointer(await targetPoint(page, source, {}, remaining), "move"); // Observe drag rejection immediately; an unhandled rejection during cursor movement exits the server. // Chromium delivers a single move before dragstart; steps give the page mousemoves during the drag. - const dragFailure = source.dragTo(target, { timeout, steps: 5 }).then( + const dragFailure = source.dragTo(target, { timeout: remaining(), steps: 5 }).then( () => null, (error: unknown) => ({ error }), ); - const end = await target.boundingBox({ timeout }).catch(() => null); + const end = await target.boundingBox({ timeout: remaining() }).catch(() => null); if (end) await pointer({ x: end.x + end.width / 2, y: end.y + end.height / 2 }, "move"); const failure = await dragFailure; if (failure) throw failure.error; diff --git a/apps/server/src/process/SpawnWorker.test.ts b/apps/server/src/process/SpawnWorker.test.ts new file mode 100644 index 0000000000000..27d31aa69c179 --- /dev/null +++ b/apps/server/src/process/SpawnWorker.test.ts @@ -0,0 +1,172 @@ +// @effect-diagnostics nodeBuiltinImport:off processEnvInEffect:off - fixtures need real paths, the process resource list and the live environment. +import * as NodeOS from "node:os"; +import * as NodePath from "node:path"; +import * as NodeServices from "@effect/platform-node/NodeServices"; +import { describe, expect, it } from "@effect/vitest"; +import { vi } from "vite-plus/test"; +import * as Deferred from "effect/Deferred"; +import * as Effect from "effect/Effect"; +import * as Fiber from "effect/Fiber"; +import * as Layer from "effect/Layer"; +import * as Result from "effect/Result"; +import * as Stream from "effect/Stream"; +import * as ChildProcess from "effect/process/ChildProcess"; +import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; +import * as HostProcess from "@t3tools/shared/HostProcess"; + +import * as SpawnWorker from "./SpawnWorker.ts"; + +const envChdir = vi.hoisted(() => ({ supported: true })); +vi.mock("@t3tools/shared/envTrampoline", async (importOriginal) => ({ + ...(await importOriginal()), + supportsEnvChdir: () => Promise.resolve(envChdir.supported), +})); + +const layer = SpawnWorker.layer.pipe(Layer.provide(NodeServices.layer)); +const text = (stream: Stream.Stream) => + stream.pipe(Stream.decodeText(), Stream.mkString); +const node = (script: string, args: ReadonlyArray = [], options = {}) => + ChildProcess.make(process.execPath, ["-e", script, ...args], options); + +describe("SpawnWorker", () => { + it.live("runs children on a launcher thread where the server thread would wait on them", () => + Effect.gen(function* () { + for (const [platform, hasEnvChdir, onServerThread] of [ + ["win32", true, false], + ["linux", true, true], + ["linux", false, false], + ] as const) { + envChdir.supported = hasEnvChdir; + const launched = Effect.gen(function* () { + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; + const child = yield* spawner.spawn( + node("process.stdout.write('ready');process.stdin.resume()"), + ); + expect(yield* child.stdout.pipe(Stream.take(1), text)).toBe("ready"); + // The thread that called spawn owns the native process handle. + expect(process.getActiveResourcesInfo().includes("ProcessWrap")).toBe(onServerThread); + expect(yield* child.isRunning).toBe(true); + }); + yield* launched.pipe( + Effect.scoped, + Effect.provide(SpawnWorker.layerServer.pipe(Layer.provide(NodeServices.layer))), + Effect.provideService(HostProcess.Platform, platform), + ); + } + }), + ); + + it.live("preserves stdin, large output, env, cwd, literal args and additional descriptors", () => + Effect.gen(function* () { + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; + const literal = "quote' \" $HOME; * & %PATH%"; + const cwd = NodeOS.tmpdir(); + const script = ` + const fs = require('node:fs'); + const input = fs.readFileSync(0, 'utf8'); + fs.writeSync(1, JSON.stringify([input, process.cwd(), process.env.SPAWN_WORKER_TEST, process.argv[1]]) + '\\n'); + fs.writeSync(1, 'a'.repeat(1024 * 1024)); + fs.writeSync(2, 'b'.repeat(1024 * 1024)); + `; + for (let i = 0; i < 3; i++) { + const child = yield* spawner.spawn( + node(script, [literal], { + cwd, + env: { SPAWN_WORKER_TEST: "inherited value" }, + extendEnv: true, + stdin: Stream.encodeText(Stream.make("stdin value")), + }), + ); + const [stdout, stderr, code] = yield* Effect.all( + [text(child.stdout), text(child.stderr), child.exitCode], + { concurrency: "unbounded" }, + ); + expect(stdout).toBe( + JSON.stringify(["stdin value", NodePath.resolve(cwd), "inherited value", literal]) + + "\n" + + "a".repeat(1024 * 1024), + ); + expect(stderr).toBe("b".repeat(1024 * 1024)); + expect(code).toBe(0); + } + // The launcher threads already exist, so this needs the server's environment at launch time. + process.env.SPAWN_WORKER_LATE = "set after start"; + const late = yield* spawner.spawn( + node("process.stdout.write(process.env.SPAWN_WORKER_LATE)"), + ); + expect(yield* text(late.stdout)).toBe("set after start"); + delete process.env.SPAWN_WORKER_LATE; + const extra = yield* spawner.spawn( + node("require('node:fs').writeSync(3, 'fd3 value')", [], { + additionalFds: { fd3: { type: "output" } }, + }), + ); + const [fd3, extraCode] = yield* Effect.all([text(extra.getOutputFd(3)), extra.exitCode], { + concurrency: "unbounded", + }); + expect(fd3).toBe("fd3 value"); + expect(extraCode).toBe(0); + const reader = yield* spawner.spawn( + node("process.stdin.on('end', () => process.stdout.write('eof')).resume()"), + ); + yield* Stream.fail("abandoned input").pipe(Stream.run(reader.stdin), Effect.ignore); + expect(yield* text(reader.stdout)).toBe("eof"); + const pipeline = node("process.stdout.write('piped')").pipe( + ChildProcess.pipeTo( + node("process.stdin.on('data', (d) => process.stdout.write(String(d).toUpperCase()))"), + ), + ); + const piped = yield* spawner.spawn(pipeline); + expect(yield* text(piped.stdout)).toBe("PIPED"); + expect(yield* piped.exitCode).toBe(0); + }).pipe(Effect.scoped, Effect.provide(layer)), + ); + + it.live("reports launch errors and exit codes, and keeps launching after either", () => + Effect.gen(function* () { + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; + const missing = yield* spawner + .spawn(ChildProcess.make("t3-missing-command-25245")) + .pipe(Effect.result); + expect(Result.isFailure(missing)).toBe(true); + if (Result.isFailure(missing)) expect(missing.failure.reason._tag).toBe("NotFound"); + const failed = yield* spawner.spawn(node("process.exit(23)")); + expect(yield* failed.exitCode).toBe(23); + const healthy = yield* spawner.spawn(node("process.stdout.write('ok')")); + expect(yield* text(healthy.stdout)).toBe("ok"); + expect(yield* healthy.exitCode).toBe(0); + }).pipe(Effect.scoped, Effect.provide(layer)), + ); + + it.live("kills running commands, detached or not, and on scope interruption", () => + Effect.gen(function* () { + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; + for (const detached of [true, false]) { + const child = yield* spawner.spawn( + node("process.stdout.write(String(process.pid));process.stdin.resume()", [], { + detached, + }), + ); + const pid = yield* child.stdout.pipe(Stream.take(1), text); + expect(Number(pid)).toBe(child.pid); + yield* child.kill(); + expect(yield* child.isRunning).toBe(false); + } + const started = yield* Deferred.make(); + const running = yield* Effect.gen(function* () { + const scoped = yield* spawner.spawn(node("process.stdin.resume()")); + yield* Deferred.succeed(started, scoped); + return yield* Effect.never; + }).pipe(Effect.scoped, Effect.forkChild); + const scoped = yield* Deferred.await(started); + yield* Fiber.interrupt(running); + expect(yield* scoped.isRunning).toBe(false); + // Direct callers such as the Claude adapter pass an AbortSignal, which cannot cross to a worker. + const direct = SpawnWorker.spawnPiped(process.execPath, ["-e", "process.stdin.resume()"], { + signal: AbortSignal.abort(), + }); + const exited = new Promise((resolve) => direct.once("exit", () => resolve(direct.killed))); + expect(yield* Effect.promise(() => exited)).toBe(true); + }).pipe(Effect.scoped, Effect.provide(layer)), + ); +}); diff --git a/apps/server/src/process/SpawnWorker.ts b/apps/server/src/process/SpawnWorker.ts new file mode 100644 index 0000000000000..a43868b6d7b41 --- /dev/null +++ b/apps/server/src/process/SpawnWorker.ts @@ -0,0 +1,541 @@ +// @effect-diagnostics nodeBuiltinImport:off - worker threads and Node streams implement the platform spawner boundary. +import * as NodeEvents from "node:events"; +import type * as NodeChildProcess from "node:child_process"; +import * as NodeStream from "node:stream"; +import * as NodeWorkerThreads from "node:worker_threads"; +import * as NodeChildProcessSpawner from "@effect/platform-node-shared/NodeChildProcessSpawner"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import { supportsEnvChdir } from "@t3tools/shared/envTrampoline"; +import * as HostProcess from "@t3tools/shared/HostProcess"; + +// Windows runs CreateProcess synchronously on the calling thread, and antivirus or a busy disk can +// hold it for seconds; Linux's spawn waits for chdir and the PATH search, which a hung mount +// stalls. Workers own the children, so only the worker waits. +const MAX_WORKERS = 4; +const MAX_IDLE_WORKERS = 1; + +type ToWorker = + | { + readonly type: "spawn"; + readonly id: number; + readonly command: string; + readonly args: ReadonlyArray; + readonly options: NodeChildProcess.SpawnOptions; + } + | { readonly type: "write"; readonly id: number; readonly fd: number; readonly data: Uint8Array } + | { readonly type: "end"; readonly id: number; readonly fd: number } + | { readonly type: "flow"; readonly id: number; readonly fd: number; readonly paused: boolean } + | { readonly type: "destroy"; readonly id: number; readonly fd: number } + | { readonly type: "kill"; readonly id: number; readonly signal: NodeJS.Signals | number }; + +interface SerializedError { + readonly message: string; + readonly [key: string]: unknown; +} + +type FromWorker = + | { readonly type: "spawn"; readonly id: number; readonly pid: number | undefined } + | { readonly type: "error"; readonly id: number; readonly error: SerializedError } + | { readonly type: "data"; readonly id: number; readonly fd: number; readonly data: Uint8Array } + | { + readonly type: "written"; + readonly id: number; + readonly fd: number; + readonly error?: SerializedError; + } + | { readonly type: "eof"; readonly id: number; readonly fd: number } + | { readonly type: "finished"; readonly id: number; readonly fd: number } + | { + readonly type: "stream-error"; + readonly id: number; + readonly fd: number; + readonly error: SerializedError; + } + | { + readonly type: "exit"; + readonly id: number; + readonly code: number | null; + readonly signal: NodeJS.Signals | null; + } + | { readonly type: "close"; readonly id: number }; + +// Inline source runs from bundles, asar archives and single-executable builds without module files. +const workerSource = ` +const { parentPort } = require("node:worker_threads"); +const { spawn } = require("node:child_process"); +const children = new Map(); +const post = (message, transfer) => parentPort.postMessage(message, transfer); +const serialize = (error) => { + const out = { message: String(error && error.message) }; + for (const key of ["code", "errno", "syscall", "path", "spawnargs"]) { + if (error && error[key] !== undefined) out[key] = error[key]; + } + return out; +}; +parentPort.on("message", (request) => { + const { id } = request; + if (request.type === "spawn") { + let child; + try { + child = spawn(request.command, request.args, request.options); + } catch (error) { + post({ type: "error", id, error: serialize(error) }); + return; + } + children.set(id, child); + child.on("spawn", () => post({ type: "spawn", id, pid: child.pid })); + child.on("error", (error) => post({ type: "error", id, error: serialize(error) })); + child.on("exit", (code, signal) => post({ type: "exit", id, code, signal })); + // Output can still arrive after exit; close follows the last of it. + child.on("close", () => { + children.delete(id); + post({ type: "close", id }); + }); + child.stdio?.forEach((stream, fd) => { + if (!stream) return; + stream.on("error", (error) => post({ type: "stream-error", id, fd, error: serialize(error) })); + if (fd === 0) return; + // Cloning a Buffer copies its whole backing store; copy the bytes once and transfer them. + stream.on("data", (chunk) => { + const data = new Uint8Array(chunk); + post({ type: "data", id, fd, data }, [data.buffer]); + }); + stream.on("end", () => post({ type: "eof", id, fd })); + }); + return; + } + const child = children.get(id); + if (child === undefined) { + if (request.type === "write") post({ type: "written", id, fd: request.fd, error: { message: "write after exit", code: "EPIPE" } }); + if (request.type === "end") post({ type: "finished", id, fd: request.fd }); + return; + } + const stream = child.stdio?.[request.fd]; + switch (request.type) { + case "write": + if (!stream || stream.destroyed) { + post({ type: "written", id, fd: request.fd, error: { message: "write after end", code: "EPIPE" } }); + return; + } + try { + stream.write(request.data, (error) => post({ type: "written", id, fd: request.fd, error: error ? serialize(error) : undefined })); + } catch (error) { + post({ type: "written", id, fd: request.fd, error: serialize(error) }); + } + return; + case "end": + if (!stream || stream.destroyed || stream.writableFinished) post({ type: "finished", id, fd: request.fd }); + else stream.end(() => post({ type: "finished", id, fd: request.fd })); + return; + case "flow": + if (request.paused) stream?.pause(); + else stream?.resume(); + return; + case "destroy": + stream?.destroy(); + return; + case "kill": + try { + child.kill(request.signal); + } catch {} + return; + } +}); +`; + +const toError = (serialized: SerializedError) => + Object.assign(new Error(serialized.message), serialized); + +const toBuffer = (data: Uint8Array) => Buffer.from(data.buffer, data.byteOffset, data.byteLength); + +class WorkerChild + extends NodeEvents.EventEmitter + implements NodeChildProcessSpawner.SpawnedChildProcess +{ + pid: number | undefined; + exitCode: number | null = null; + signalCode: NodeJS.Signals | null = null; + stdin: NodeStream.Writable | null = null; + stdout: NodeStream.Readable | null = null; + stderr: NodeStream.Readable | null = null; + readonly stdio: Array = []; + referenced = true; + killed = false; + spawned = false; + exited = false; + readonly #writes = new Map void>>(); + readonly #finals = new Map void>(); + readonly #paused = new Set(); + + readonly worker: SpawnWorker; + readonly id: number; + + constructor(worker: SpawnWorker, id: number, stdio: ReadonlyArray) { + super(); + this.worker = worker; + this.id = id; + stdio.forEach((option, fd) => { + if (option !== "pipe" && option !== "overlapped") return; + const stream = this.#relay(fd); + this.stdio[fd] = stream; + if (fd === 0) this.stdin = stream as NodeStream.Writable; + else if (fd === 1) this.stdout = stream as NodeStream.Readable; + else if (fd === 2) this.stderr = stream as NodeStream.Readable; + }); + } + + #relay(fd: number): NodeStream.Readable | NodeStream.Writable { + const read = () => { + if (this.#paused.delete(fd)) + this.worker.post({ type: "flow", id: this.id, fd, paused: false }); + }; + const write = ( + chunk: Buffer, + _encoding: BufferEncoding, + callback: (error?: Error | null) => void, + ) => { + if (this.worker.closed) { + callback(Object.assign(new Error("write EPIPE"), { code: "EPIPE" })); + return; + } + const pending = this.#writes.get(fd) ?? []; + pending.push(callback); + this.#writes.set(fd, pending); + const data = new Uint8Array(chunk); + this.worker.post({ type: "write", id: this.id, fd, data }, [data.buffer]); + }; + const final = (callback: (error?: Error | null) => void) => { + this.#finals.set(fd, callback); + this.worker.post({ type: "end", id: this.id, fd }); + }; + // A caller that destroys its end closes the child pipe, as it would with a real pipe. + const destroy = (error: Error | null, callback: (error?: Error | null) => void) => { + if (!this.exited) this.worker.post({ type: "destroy", id: this.id, fd }); + callback(error); + }; + if (fd === 0) return new NodeStream.Writable({ write, final, destroy }); + if (fd <= 2) return new NodeStream.Readable({ read, destroy }); + return new NodeStream.Duplex({ read, write, final, destroy, allowHalfOpen: true }); + } + + onData(fd: number, data: Uint8Array): void { + const stream = this.stdio[fd]; + if (!stream || !("push" in stream)) return; + if (!stream.push(toBuffer(data)) && !this.#paused.has(fd)) { + this.#paused.add(fd); + this.worker.post({ type: "flow", id: this.id, fd, paused: true }); + } + } + + onEof(fd: number): void { + const stream = this.stdio[fd]; + if (stream && "push" in stream) stream.push(null); + } + + onFinished(fd: number): void { + const callback = this.#finals.get(fd); + this.#finals.delete(fd); + callback?.(); + } + + onWritten(fd: number, error: SerializedError | undefined): void { + const callback = this.#writes.get(fd)?.shift(); + callback?.(error === undefined ? null : toError(error)); + } + + onStreamError(fd: number, error: SerializedError): void { + this.stdio[fd]?.destroy(toError(error)); + } + + kill(signal: NodeJS.Signals | number = "SIGTERM"): boolean { + if (this.exited) return false; + this.killed = true; + this.worker.post({ type: "kill", id: this.id, signal }); + return true; + } + + ref(): void { + this.referenced = true; + this.worker.syncRef(); + } + + unref(): void { + this.referenced = false; + this.worker.syncRef(); + } + + exit(code: number | null, signal: NodeJS.Signals | null): void { + if (this.exited) return; + this.exited = true; + this.exitCode = code; + this.signalCode = signal; + // Node destroys stdin on exit; the Effect sink may already have removed its error listener. + this.stdin?.on("error", () => {}); + this.stdin?.destroy(); + this.emit("exit", code, signal); + } + + /** The worker is gone: end output so readers finish, and let the exit report the kill. */ + abandon(): void { + // Not destroy(error): Effect leaves an unread stderr's pipe without an error listener. + for (const stream of this.stdio) { + if (stream && "push" in stream && !stream.readableEnded) stream.push(null); + else if (stream && !("push" in stream)) stream.destroy(); + } + } +} + +class SpawnWorker { + readonly worker: NodeWorkerThreads.Worker; + readonly children = new Map(); + readonly pool: SpawnWorkerPool; + /** Launches sent but not yet answered; the worker is inside CreateProcess for one of them. */ + launching = 0; + closed = false; + + constructor(pool: SpawnWorkerPool) { + this.pool = pool; + this.worker = new NodeWorkerThreads.Worker(workerSource, { eval: true }); + this.worker.on("message", (message: FromWorker) => this.onMessage(message)); + this.worker.on("error", (error) => this.fail(error)); + this.worker.on("exit", () => this.fail(new Error("The process launcher thread exited."))); + this.worker.unref(); + } + + post(message: ToWorker, transfer?: ReadonlyArray): void { + if (!this.closed) this.worker.postMessage(message, transfer); + } + + spawn( + id: number, + command: string, + args: ReadonlyArray, + options: NodeChildProcess.SpawnOptions, + ): WorkerChild { + const io = options.stdio; + const stdio = + typeof io === "string" || io === undefined ? [io ?? "pipe", io ?? "pipe", io ?? "pipe"] : io; + const child = new WorkerChild(this, id, stdio); + // An AbortSignal cannot cross to the worker, so this thread kills on abort as Node would. + const { signal, ...rest } = options; + if (signal !== undefined) { + const abort = () => child.kill(); + if (signal.aborted) queueMicrotask(abort); + else { + signal.addEventListener("abort", abort, { once: true }); + child.once("exit", () => signal.removeEventListener("abort", abort)); + } + } + this.children.set(id, child); + this.launching++; + this.syncRef(); + this.post({ + type: "spawn", + id, + command, + args: [...args], + // A worker's process.env is a copy from when it started; children get the server's current one. + options: { ...rest, stdio, env: rest.env ?? { ...process.env } }, + }); + return child; + } + + syncRef(): void { + if (this.closed) return; + const referenced = + this.launching > 0 || [...this.children.values()].some((child) => child.referenced); + if (referenced) this.worker.ref(); + else this.worker.unref(); + } + + onMessage(message: FromWorker): void { + const child = this.children.get(message.id); + if (child === undefined) return; + switch (message.type) { + case "spawn": + this.launching--; + child.spawned = true; + child.pid = message.pid; + child.emit("spawn"); + break; + case "error": + if (!child.spawned) { + this.launching--; + child.exited = true; + this.release(child); + } + child.emit("error", toError(message.error)); + break; + case "data": + child.onData(message.fd, message.data); + break; + case "written": + child.onWritten(message.fd, message.error); + break; + case "eof": + child.onEof(message.fd); + break; + case "finished": + child.onFinished(message.fd); + break; + case "stream-error": + child.onStreamError(message.fd, message.error); + break; + case "exit": + child.exit(message.code, message.signal); + break; + case "close": + child.emit("close", child.exitCode, child.signalCode); + this.release(child); + break; + } + } + + release(child: WorkerChild): void { + this.children.delete(child.id); + this.syncRef(); + this.pool.release(this); + } + + fail(error: Error): void { + if (this.closed) return; + this.close(); + for (const child of this.children.values()) { + if (child.exited) { + child.abandon(); + continue; + } + if (!child.spawned) { + child.exited = true; + child.emit("error", error); + continue; + } + // The worker owned the only handle, so try to end the whole tree before reporting the exit. + const finish = () => { + child.exit(null, "SIGKILL"); + child.abandon(); + }; + if (child.pid === undefined) finish(); + else this.pool.killTree(child.pid, finish); + } + this.children.clear(); + } + + close(): void { + if (this.closed) return; + this.closed = true; + this.pool.workers.delete(this); + void this.worker.terminate(); + } +} + +class SpawnWorkerPool { + readonly workers = new Set(); + readonly platform: NodeJS.Platform; + #nextId = 0; + + constructor(platform: NodeJS.Platform) { + this.platform = platform; + } + + readonly spawn = ( + command: string, + args: ReadonlyArray, + options: NodeChildProcess.SpawnOptions, + ) => { + const workers = [...this.workers]; + // A worker inside CreateProcess cannot start another launch until it returns. + const worker = + workers.find((candidate) => candidate.launching === 0) ?? + (workers.length < MAX_WORKERS + ? this.#add() + : workers.reduce((best, candidate) => + candidate.launching < best.launching ? candidate : best, + )); + return worker.spawn(++this.#nextId, command, args, options); + }; + + killTree(pid: number, done: () => void): void { + const killPid = () => { + try { + process.kill(pid, "SIGKILL"); + } catch {} + }; + if (this.platform !== "win32") { + killPid(); + done(); + return; + } + let settled = false; + const finish = (killed: boolean) => { + if (settled) return; + settled = true; + // As Effect's own kill does when taskkill fails, end at least the direct child. + if (!killed) killPid(); + done(); + }; + this.spawn("taskkill", ["/pid", String(pid), "/T", "/F"], { + stdio: "ignore", + windowsHide: true, + }) + .on("error", () => finish(false)) + .on("exit", (code) => finish(code === 0)); + } + + #add(): SpawnWorker { + const worker = new SpawnWorker(this); + this.workers.add(worker); + return worker; + } + + release(worker: SpawnWorker): void { + if (worker.children.size > 0 || worker.launching > 0) return; + const idle = [...this.workers].filter( + (candidate) => candidate.children.size === 0 && candidate.launching === 0, + ); + if (idle.length > MAX_IDLE_WORKERS) worker.close(); + } + + close(): void { + for (const worker of this.workers) worker.close(); + } +} + +let sharedPool: SpawnWorkerPool | undefined; + +/** A piped `child_process.spawn` for Windows launches outside Effect's spawner. */ +export const spawnPiped = ( + command: string, + args: ReadonlyArray, + options: Omit, +) => + (sharedPool ??= new SpawnWorkerPool("win32")).spawn(command, args, { + ...options, + stdio: ["pipe", "pipe", "pipe"], + }) as WorkerChild & { + readonly stdin: NodeStream.Writable; + readonly stdout: NodeStream.Readable; + readonly stderr: NodeStream.Readable; + }; + +export const layer = Layer.unwrap( + Effect.gen(function* () { + const platform = yield* HostProcess.Platform; + const pool = yield* Effect.acquireRelease( + Effect.sync(() => new SpawnWorkerPool(platform)), + (pool) => Effect.sync(() => pool.close()), + ); + return NodeChildProcessSpawner.layerWithSpawn(pool.spawn); + }), +); + +/** Workers on Windows, and on Linux where `NonBlockingSpawner` has no GNU `env -C` to use. */ +export const layerServer = Layer.unwrap( + Effect.gen(function* () { + const platform = yield* HostProcess.Platform; + if (platform === "win32") return layer; + if (platform === "linux" && !(yield* Effect.promise(supportsEnvChdir))) return layer; + return NodeChildProcessSpawner.layer; + }), +); diff --git a/apps/server/src/process/agentScope.test.ts b/apps/server/src/process/agentScope.test.ts index a44ff15359f35..49674354b2532 100644 --- a/apps/server/src/process/agentScope.test.ts +++ b/apps/server/src/process/agentScope.test.ts @@ -12,6 +12,7 @@ import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Logger from "effect/Logger"; import { ChildProcess, ChildProcessSpawner } from "effect/process"; +import { vi } from "vite-plus/test"; import * as ServerConfig from "../config.ts"; import { makeCodexAppServerSpawnCommand } from "../orchestration-v2/Adapters/CodexAdapterV2.ts"; @@ -19,6 +20,13 @@ import * as ProcessRunner from "../processRunner.ts"; import * as ProviderProcesses from "./providerProcesses.ts"; import { classifyScope, make, ownedAgentSlice, type ScopeState } from "./agentScope.ts"; +const GiB = 1024 ** 3; +const memory = vi.hoisted(() => ({ total: 16 * 1024 ** 3 })); +vi.mock("node:os", async (importOriginal) => ({ + ...(await importOriginal()), + totalmem: () => memory.total, +})); + const running: ScopeState = { loadState: "loaded", activeState: "active", @@ -49,6 +57,7 @@ const withFakeSystemd = ( readonly scope: Effect.Success; readonly units: Map; readonly calls: ReadonlyArray>; + readonly failingVerbs: Set; readonly stateDir: string; readonly cgroupRoot: string; }) => Effect.Effect, @@ -73,6 +82,7 @@ const withFakeSystemd = ( const cgroupRoot = NodePath.join(bin, "cgroup"); const units = new Map(); const calls: Array> = []; + const failingVerbs = new Set(); const output = (stdout: string, code = 0): ProcessRunner.ProcessRunOutput => ({ stdout, stderr: "", @@ -91,6 +101,7 @@ const withFakeSystemd = ( return output("", 1); } const [, verb, unit = ""] = args; + if (failingVerbs.has(verb!)) return output("", 1); if (stopFails && verb === "stop") { return { ...output(""), @@ -127,7 +138,7 @@ const withFakeSystemd = ( fileSystem.writeFileString(path.replace("/sys/fs/cgroup", cgroupRoot), data, options), }), ); - return yield* body({ scope, units, calls, stateDir, cgroupRoot }).pipe( + return yield* body({ scope, units, calls, failingVerbs, stateDir, cgroupRoot }).pipe( Effect.ensuring(Effect.sync(() => NodeFS.rmSync(bin, { recursive: true, force: true }))), ); }).pipe( @@ -320,6 +331,40 @@ describe("AgentScope service", () => { ), ); + it.effect("follows a live memory change at the next launch", () => { + memory.total = 16 * GiB; + return withFakeSystemd(({ scope, calls, failingVerbs }) => + Effect.gen(function* () { + const launch = scope.wrap({ command: "agent", args: [], name: "t", threadId: "a" }); + const memoryLimits = () => + calls + .filter( + ([, verb, , slice]) => verb === "set-property" && slice === "app-t3code-agents.slice", + ) + .map((args) => args.slice(4)); + yield* launch; + yield* launch; + expect(memoryLimits()).toEqual([[`MemoryHigh=${10 * GiB}`, `MemoryMax=${12 * GiB}`]]); + + // A VM balloon or `pct set --memory` grows RAM while the server keeps running. + memory.total = 22 * GiB; + failingVerbs.add("set-property"); + yield* launch; + yield* launch; + failingVerbs.delete("set-property"); + yield* launch; + yield* launch; + const grown = [`MemoryHigh=${16 * GiB}`, `MemoryMax=${18 * GiB}`]; + expect(memoryLimits()).toEqual([ + [`MemoryHigh=${10 * GiB}`, `MemoryMax=${12 * GiB}`], + grown, + grown, + grown, + ]); + }), + ); + }); + it.effect("stops agents a killed server left before startup completes", () => withFakeSystemd( ({ scope, calls, stateDir }) => diff --git a/apps/server/src/process/agentScope.ts b/apps/server/src/process/agentScope.ts index 7adc4c453be08..1e52e680b6f8d 100644 --- a/apps/server/src/process/agentScope.ts +++ b/apps/server/src/process/agentScope.ts @@ -14,6 +14,7 @@ import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Path from "effect/Path"; +import * as Semaphore from "effect/Semaphore"; import * as ServerConfig from "../config.ts"; import * as ProcessRunner from "../processRunner.ts"; @@ -193,20 +194,44 @@ export const make = Effect.gen(function* () { slice, ...properties, ]); - if (result._tag === "None" || result.value.code !== 0) { - yield* Effect.logWarning("Could not set resource limits on an agents slice", { - slice, - properties, - stderr: result._tag === "Some" ? result.value.stderr.trim() : undefined, - }); - } + if (result._tag === "Some" && result.value.code === 0) return true; + yield* Effect.logWarning("Could not set resource limits on an agents slice", { + slice, + properties, + stderr: result._tag === "Some" ? result.value.stderr.trim() : undefined, + }); + return false; }); - const limits = agentSliceMemoryLimits(NodeOS.totalmem()); - yield* setProperties(AGENT_SLICE, [`MemoryHigh=${limits.high}`, `MemoryMax=${limits.max}`]); + // VMs and containers can resize RAM live, so each launch re-checks the total + // the limits came from. A failed update is retried by the next launch. + let appliedTotal: number | undefined; + const memoryLimitsLock = yield* Semaphore.make(1); + const applyMemoryLimits = memoryLimitsLock.withPermits(1)( + Effect.gen(function* () { + const total = NodeOS.totalmem(); + if (total === appliedTotal) return; + const limits = agentSliceMemoryLimits(total); + const applied = yield* setProperties(AGENT_SLICE, [ + `MemoryHigh=${limits.high}`, + `MemoryMax=${limits.max}`, + ]); + if (!applied) return; + if (appliedTotal !== undefined) { + yield* Effect.logInfo("Agent memory limits follow a memory change", { + slice: AGENT_SLICE, + previousTotal: appliedTotal, + total, + ...limits, + }); + } + appliedTotal = total; + }), + ); + yield* applyMemoryLimits; yield* setProperties(AGENTS_PARENT_SLICE, AGENTS_PARENT_PROPERTIES); yield* Effect.logInfo("Agent scopes are on", { slice: AGENT_SLICE, - ...limits, + ...agentSliceMemoryLimits(NodeOS.totalmem()), oomPolicy: supportsOomPolicy, }); return { @@ -214,6 +239,7 @@ export const make = Effect.gen(function* () { systemctl, runtimeDir, supportsOomPolicy, + applyMemoryLimits, }; }), ); @@ -403,6 +429,7 @@ export const make = Effect.gen(function* () { if (resolved === null) return { command, args }; const scope = yield* systemd; if (scope === undefined) return agentScopeCommand({ command: resolved, args }); + yield* scope.applyMemoryLimits; const unit = `t3code-${name}-${NodeCrypto.randomUUID().slice(0, 8)}.scope`; // A continuing shared scope cannot attribute a tool's OOM to one thread. // Clear the launch thread's previous scope instead. diff --git a/apps/server/src/process/externalLauncher.test.ts b/apps/server/src/process/externalLauncher.test.ts index 4f6cabb783be9..f215fe4ebc6a2 100644 --- a/apps/server/src/process/externalLauncher.test.ts +++ b/apps/server/src/process/externalLauncher.test.ts @@ -105,36 +105,47 @@ const layerTest = (input: { }; // macOS command lookup cannot search a Windows drive-letter PATH. -it.effect.skipIf(windowsHost)( - "opens home-relative app bundles using the server home directory", - () => - Effect.gen(function* () { - const fs = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - const bin = yield* fs.makeTempDirectoryScoped({ prefix: "t3-app-open-" }); - yield* fs.writeFileString(path.join(bin, "open"), "#!/bin/sh\n"); - yield* fs.chmod(path.join(bin, "open"), 0o755); - const spawned: ChildProcess.StandardCommand[] = []; - yield* Effect.gen(function* () { - const launcher = yield* ExternalLauncher.ExternalLauncher; - for (const cwd of ["~/Applications/My App.app", "/Applications/My App.app"]) { - yield* launcher.launchEditor({ editor: "file-manager", cwd }); - } - }).pipe( - Effect.provide( - layerTest({ - platform: "darwin", - env: { PATH: bin }, - onSpawn: (command) => spawned.push(command), - }), - ), - Effect.provideService(HostProcess.HomeDirectory, "/Users/server"), - ); - assert.deepEqual( - spawned.map((command) => command.args), - [["/Users/server/Applications/My App.app"], ["/Applications/My App.app"]], - ); - }).pipe(Effect.provide(NodeServices.layer)), +it.effect.skipIf(windowsHost)("opens home-relative targets using the server home directory", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const bin = yield* fs.makeTempDirectoryScoped({ prefix: "t3-app-open-" }); + for (const name of ["open", "code", "idea"]) { + yield* fs.writeFileString(path.join(bin, name), "#!/bin/sh\n"); + yield* fs.chmod(path.join(bin, name), 0o755); + } + const spawned: ChildProcess.StandardCommand[] = []; + yield* Effect.gen(function* () { + const launcher = yield* ExternalLauncher.ExternalLauncher; + for (const cwd of ["~/Applications/My App.app", "/Applications/My App.app"]) { + yield* launcher.launchEditor({ editor: "file-manager", cwd }); + } + yield* launcher.launchEditor({ editor: "vscode", cwd: "~/notes/todo.md:3" }); + yield* launcher.launchEditor({ + editor: "idea", + cwd: "~/notes/todo.md:3", + projectRoot: "~/notes", + }); + }).pipe( + Effect.provide( + layerTest({ + platform: "darwin", + env: { PATH: bin }, + onSpawn: (command) => spawned.push(command), + }), + ), + Effect.provideService(HostProcess.HomeDirectory, "/Users/server"), + ); + assert.deepEqual( + spawned.map((command) => command.args), + [ + ["/Users/server/Applications/My App.app"], + ["/Applications/My App.app"], + ["--goto", "/Users/server/notes/todo.md:3"], + ["/Users/server/notes", "--line", "3", "/Users/server/notes/todo.md"], + ], + ); + }).pipe(Effect.provide(NodeServices.layer)), ); it.effect.skipIf(windowsHost)( @@ -300,6 +311,69 @@ it.effect.skipIf(windowsHost)( }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); +// Windows drive letters cannot resolve the POSIX xdg stubs. +it.effect.skipIf(windowsHost)( + "a Linux server without a display offers and opens the file manager on the desktop session's display", + () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" }); + for (const name of ["xdg-open", "xdg-mime"]) { + const filePath = path.join(binDir, name); + yield* fileSystem.writeFileString(filePath, "#!/bin/sh\n"); + yield* fileSystem.chmod(filePath, 0o755); + } + + const fileManager = (sessionEnv: ReadonlyArray) => + Effect.gen(function* () { + const spawned: ChildProcess.StandardCommand[] = []; + const result = yield* Effect.gen(function* () { + const launcher = yield* ExternalLauncher.ExternalLauncher; + const editors = yield* launcher.resolveAvailableEditors(); + const kind = yield* launcher.resolveFileManagerRevealKind(); + const opened = yield* launcher + .launchEditor({ editor: "file-manager", cwd: "/workspace" }) + .pipe(Effect.exit); + return { offered: editors.includes("file-manager"), kind, opened }; + }).pipe( + Effect.provide( + layerTest({ + platform: "linux", + env: { PATH: binDir }, + onSpawn: (command) => spawned.push(command), + spawnResult: (command) => + command.command === "busctl" + ? { stdout: JSON.stringify({ type: "as", data: sessionEnv }) } + : command.command === "xdg-mime" + ? { stdout: "org.kde.dolphin.desktop\n" } + : undefined, + }), + ), + Effect.provideService(HostProcess.Environment, { PATH: binDir }), + ); + const open = spawned.find((command) => command.command === "xdg-open"); + return { ...result, open: open && { args: open.args, env: open.options.env } }; + }); + + const session = yield* fileManager(["DISPLAY=:0", "HOME=/home/user"]); + assert.equal(session.offered, true); + assert.equal(session.kind, "files"); + assert.equal(Exit.isSuccess(session.opened), true); + assert.deepEqual(session.open, { + args: ["/workspace"], + env: { PATH: binDir, DISPLAY: ":0" }, + }); + + // No one is logged in to a desktop, so there is no screen to open it on. + const headless = yield* fileManager(["HOME=/home/user"]); + assert.equal(headless.offered, false); + assert.equal(headless.kind, undefined); + assert.equal(Exit.isFailure(headless.opened), true); + assert.equal(headless.open, undefined); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), +); + it.effect("launches an installed editor with platform-safe arguments", () => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; @@ -343,6 +417,39 @@ it.effect("launches an installed editor with platform-safe arguments", () => }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); +const wslpathSpawner = (out: string | undefined) => + Layer.succeed( + ChildProcessSpawner.ChildProcessSpawner, + ChildProcessSpawner.make((command) => + Effect.sync(() => { + if (!ChildProcess.isStandardCommand(command)) { + throw new Error("Expected a standard command"); + } + assert.equal(command.command, "wslpath"); + return makeMockDetachedHandle( + out === undefined ? { exitCode: 1 } : { exitCode: 0, stdout: `${out}\n` }, + ); + }), + ), + ); + +it.effect.each([ + // wslpath is mount-aware: a drive remapped in /etc/wsl.conf keeps its real letter. + { target: "/mnt/d/project", wslpath: "E:\\project", expected: "E:\\project" }, + // wslpath missing or failing falls back to the hand-built UNC path. + { + target: "/home/user/repo", + wslpath: undefined, + expected: "\\\\wsl.localhost\\Ubuntu-24.04\\home\\user\\repo", + }, + { target: "/", wslpath: undefined, expected: "\\\\wsl.localhost\\Ubuntu-24.04" }, +])("resolveWslFileManagerPath resolves $target to $expected", ({ target, wslpath, expected }) => + Effect.gen(function* () { + const result = yield* ExternalLauncher.resolveWslFileManagerPath(target, "Ubuntu-24.04"); + assert.equal(result, expected); + }).pipe(Effect.provide(wslpathSpawner(wslpath))), +); + it.live.skipIf(!windowsHost)("opens Windows folders through the default shell handler", () => Effect.gen(function* () { let spawned: ChildProcess.StandardCommand | undefined; @@ -796,17 +903,32 @@ it.effect.skipIf(windowsHost)("does not offer the OpenZFS zed daemon as Zed on L ); yield* fs.chmod(zfsZed, 0o755); const editor = EDITORS.find((editor) => editor.id === "zed")!; - const resolve = resolveEditorCommand(editor, { HOME: home, PATH: binDir }).pipe( - Effect.provideService(HostProcess.Platform, "linux"), - Effect.provideService(Path.Path, withSystemBin(path, systemBin)), - ); - assert.deepEqual(yield* resolve, Option.none()); + const laterBin = path.join(home, "later-bin"); + const resolveOn = (platform: NodeJS.Platform) => + resolveEditorCommand(editor, { HOME: home, PATH: `${binDir}:${laterBin}` }).pipe( + Effect.provideService(HostProcess.Platform, platform), + Effect.provideService(Path.Path, withSystemBin(path, systemBin)), + ); + assert.deepEqual(yield* resolveOn("linux"), Option.none()); + assert.deepEqual(yield* resolveOn("darwin"), Option.none()); const zedEditor = path.join(home, ".local/bin/zed"); yield* fs.makeDirectory(path.dirname(zedEditor), { recursive: true }); yield* fs.writeFileString(zedEditor, "#!/bin/sh\necho 'Zed 0.200.0'\n"); yield* fs.chmod(zedEditor, 0o755); - assert.deepEqual(yield* resolve, Option.some({ command: zedEditor, baseArgs: [] })); + assert.deepEqual(yield* resolveOn("linux"), Option.some({ command: zedEditor, baseArgs: [] })); + + yield* fs.remove(zedEditor); + const laterZed = path.join(laterBin, "zed"); + yield* fs.makeDirectory(laterBin, { recursive: true }); + yield* fs.writeFileString(laterZed, "#!/bin/sh\necho 'Zed 0.200.0'\n"); + yield* fs.chmod(laterZed, 0o755); + for (const platform of ["linux", "darwin"] as const) { + assert.deepEqual( + yield* resolveOn(platform), + Option.some({ command: laterZed, baseArgs: [] }), + ); + } }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); @@ -829,7 +951,7 @@ it.effect.skipIf(windowsHost)("does not open Zed with the OpenZFS zed daemon on Effect.provide( layerTest({ platform: "linux", - env: { HOME: home, PATH: binDir }, + env: { HOME: home, PATH: binDir, DISPLAY: ":0" }, path: withSystemBin(path, systemBin), onSpawn: (command) => spawned.push(command), spawnResult: (command) => (command.args[0] === "--version" ? { exitCode: 1 } : undefined), diff --git a/apps/server/src/process/externalLauncher.ts b/apps/server/src/process/externalLauncher.ts index bb38a9d56afd4..2bdb7bd3db4db 100644 --- a/apps/server/src/process/externalLauncher.ts +++ b/apps/server/src/process/externalLauncher.ts @@ -371,11 +371,41 @@ const fileManagerRevealKindForPlatform = Effect.fn( return hasGraphicalLinuxSession(env) ? "files" : undefined; }); -function resolveWslFileManagerPath(target: string, distroName: string): string { +function wslUncFallbackPath(target: string, distroName: string): string { const relativePath = target.replace(/^\/+/, "").replaceAll("/", "\\"); return `\\\\wsl.localhost\\${distroName}${relativePath.length > 0 ? `\\${relativePath}` : ""}`; } +// wslpath -w is mount-aware, so a drive remapped in /etc/wsl.conf resolves to the +// real letter and a Linux path resolves to the \\wsl.localhost UNC path. Fall back +// to the UNC form by hand when wslpath is missing or fails. +export const resolveWslFileManagerPath = Effect.fn("externalLauncher.resolveWslFileManagerPath")( + function* ( + target: string, + distroName: string, + ): Effect.fn.Return { + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; + return yield* spawner + .spawn(ChildProcess.make("wslpath", ["-w", target], { stdin: "ignore", stderr: "ignore" })) + .pipe( + Effect.flatMap((handle) => + Effect.all([handle.stdout.pipe(Stream.decodeText(), Stream.mkString), handle.exitCode], { + concurrency: "unbounded", + }), + ), + Effect.map(([stdout, exitCode]) => { + const translated = stdout.trim(); + return exitCode === 0 && translated.length > 0 + ? translated + : wslUncFallbackPath(target, distroName); + }), + Effect.scoped, + Effect.timeout("2 seconds"), + Effect.orElseSucceed(() => wslUncFallbackPath(target, distroName)), + ); + }, +); + function buildBrowserLaunch( target: string, platform: NodeJS.Platform, @@ -443,16 +473,23 @@ const resolveBrowserLaunch = Effect.fn("externalLauncher.resolveBrowserLaunch")( return buildBrowserLaunch(target, platform, env); }); +// Editors and the file manager open on the desktop session, so a boot-started +// server discovers and launches them with that session's display. +const readEditorEnv = Effect.fn("externalLauncher.readEditorEnv")(function* () { + const env = { ...(yield* readBrowserLaunchEnv), ...(yield* readCommandLookupEnv) }; + return hasGraphicalLinuxSession(env) ? env : { ...env, ...(yield* readGraphicalSessionEnv()) }; +}); + const resolveAvailableEditors = Effect.fn("externalLauncher.resolveAvailableEditors")(function* () { const platform = yield* HostProcess.Platform; - const env = { ...(yield* readBrowserLaunchEnv), ...(yield* readCommandLookupEnv) }; + const env = yield* readEditorEnv(); return yield* buildAvailableEditors(platform, env).pipe(withPathDirectoryListings); }); const resolveFileManagerRevealKind = Effect.fn("externalLauncher.resolveFileManagerRevealKind")( function* () { const platform = yield* HostProcess.Platform; - const env = { ...(yield* readBrowserLaunchEnv), ...(yield* readCommandLookupEnv) }; + const env = yield* readEditorEnv(); return yield* fileManagerRevealKindForPlatform(platform, env); }, ); @@ -478,7 +515,7 @@ const resolveEditorLaunch = Effect.fn("resolveEditorLaunch")(function* ( FileSystem.FileSystem | Path.Path | ChildProcessSpawner.ChildProcessSpawner > { const platform = yield* HostProcess.Platform; - const env = { ...(yield* readBrowserLaunchEnv), ...(yield* readCommandLookupEnv) }; + const env = yield* readEditorEnv(); yield* Effect.annotateCurrentSpan({ "externalLauncher.editor": input.editor, "externalLauncher.cwd": input.cwd, @@ -524,7 +561,7 @@ const resolveEditorLaunch = Effect.fn("resolveEditorLaunch")(function* ( return yield* new ExternalLauncherUnsupportedEditorError({ editor: input.editor }); } - const target = expandHomePath(input.cwd, yield* HostProcess.HomeDirectory); + const target = input.cwd; if (input.reveal === true) { return yield* resolveFileManagerRevealLaunch(target, platform, env, command); } @@ -547,7 +584,7 @@ const resolveEditorLaunch = Effect.fn("resolveEditorLaunch")(function* ( command, args: command === "explorer.exe" && env.WSL_DISTRO_NAME !== undefined - ? [resolveWslFileManagerPath(target, env.WSL_DISTRO_NAME)] + ? [yield* resolveWslFileManagerPath(target, env.WSL_DISTRO_NAME)] : [target], }; }); @@ -611,13 +648,18 @@ const resolveFileManagerRevealLaunch = Effect.fn("resolveFileManagerRevealLaunch shouldUseWindowsHostFromWsl(platform, env) && env.WSL_DISTRO_NAME !== undefined ) { - const explorerTarget = resolveWslFileManagerPath(target, env.WSL_DISTRO_NAME); if (yield* isCommandAvailable(WSL_POWERSHELL_COMMAND, { env })) { // WSL names may contain quotes that Explorer's /select cannot encode. // Open the parent through the shell so replacement managers still apply. const path = yield* Path.Path; // .NET path parsing rejects characters that are legal in WSL filenames. - const parent = resolveWslFileManagerPath(path.dirname(target), env.WSL_DISTRO_NAME); + const [explorerTarget, parent] = yield* Effect.all( + [ + resolveWslFileManagerPath(target, env.WSL_DISTRO_NAME), + resolveWslFileManagerPath(path.dirname(target), env.WSL_DISTRO_NAME), + ], + { concurrency: "unbounded" }, + ); const source = explorerTarget.includes('"') ? `Invoke-Item -LiteralPath ${escapePowerShellStringLiteral(parent)}` : buildFileManagerRevealPowerShellSource(explorerTarget, parent); @@ -632,7 +674,7 @@ const resolveFileManagerRevealLaunch = Effect.fn("resolveFileManagerRevealLaunch editor: "file-manager", target, command: "explorer.exe", - args: [resolveWslFileManagerPath(path.dirname(target), env.WSL_DISTRO_NAME)], + args: [yield* resolveWslFileManagerPath(path.dirname(target), env.WSL_DISTRO_NAME)], }; } @@ -731,7 +773,12 @@ const launchEditor = Effect.fn("externalLauncher.launchEditor")(function* ( input: LaunchEditorInput, hasStockFolderVerb: Effect.Effect, ) { - const launch = yield* resolveEditorLaunch(input); + // Clients send `~/` host paths as written, and launches run without a shell to expand them. + const home = yield* HostProcess.HomeDirectory; + const cwd = expandHomePath(input.cwd, home); + const projectRoot = + input.projectRoot === undefined ? undefined : expandHomePath(input.projectRoot, home); + const launch = yield* resolveEditorLaunch({ ...input, cwd, projectRoot }); if (input.editor === "file-manager" && input.reveal === true) { const platform = yield* HostProcess.Platform; if ( diff --git a/apps/server/src/processRunner.test.ts b/apps/server/src/processRunner.test.ts index 3754f25a4c775..6768fa23c763b 100644 --- a/apps/server/src/processRunner.test.ts +++ b/apps/server/src/processRunner.test.ts @@ -2,6 +2,8 @@ import { isWindowsCommandNotFound } from "@t3tools/provider-core/server/snapshot import { describe, expect, it } from "@effect/vitest"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; +import * as Exit from "effect/Exit"; +import * as Cause from "effect/Cause"; import * as Layer from "effect/Layer"; import * as PlatformError from "effect/PlatformError"; import * as Sink from "effect/Sink"; @@ -143,6 +145,29 @@ describe("runProcess", () => { }), ); + it.effect("reports child startup before delivering cancellation during acquisition", () => + Effect.gen(function* () { + let started = false; + const spawner = makeSpawner(() => + Effect.withFiber((fiber) => + Effect.sync(() => { + fiber.interruptUnsafe(); + return makeHandle({}); + }), + ), + ); + const result = yield* runWith(spawner)({ + command: "git", + args: ["restore"], + onSpawn: () => { + started = true; + }, + }).pipe(Effect.exit); + expect(Exit.isFailure(result) && Cause.hasInterruptsOnly(result.cause)).toBe(true); + expect(started).toBe(true); + }).pipe(Effect.scoped), + ); + it.effect("fails when output exceeds max buffer in default mode", () => Effect.gen(function* () { const spawner = makeSpawner(() => Effect.succeed(makeHandle({ stdout: "x".repeat(2048) }))); diff --git a/apps/server/src/processRunner.ts b/apps/server/src/processRunner.ts index a345c67eca881..4c7efd3cc9942 100644 --- a/apps/server/src/processRunner.ts +++ b/apps/server/src/processRunner.ts @@ -26,6 +26,8 @@ export interface ProcessRunInput { readonly stdin?: string | undefined; /** Receives every stdout chunk, including bytes beyond the buffered output limit. */ readonly onStdoutChunk?: ((chunk: Uint8Array) => void) | undefined; + /** Runs after successful spawn, before cancellation can interrupt acquisition. */ + readonly onSpawn?: (() => void) | undefined; readonly maxOutputBytes?: number | undefined; readonly outputMode?: "error" | "truncate" | undefined; readonly truncatedMarker?: string | undefined; @@ -290,7 +292,7 @@ const runProcessCore = Effect.fn("processRunner.runProcessCore")(function* ( cause, }); - const child = yield* spawner + const spawn = spawner .spawn( ChildProcess.make(spawnCommand.command, spawnCommand.args, { ...((input.spawnCwd ?? input.cwd) ? { cwd: input.spawnCwd ?? input.cwd } : {}), @@ -309,6 +311,12 @@ const runProcessCore = Effect.fn("processRunner.runProcessCore")(function* ( // Synchronous spawn throws arrive as defects; recover them too so callers see typed spawn failures. Effect.catchDefect((cause) => Effect.fail(toSpawnError(cause))), ); + const child = yield* input.onSpawn === undefined + ? spawn + : spawn.pipe( + Effect.tap(() => Effect.sync(() => input.onSpawn?.())), + Effect.uninterruptible, + ); const stdin = input.stdin; const onStdoutChunk = input.onStdoutChunk; diff --git a/apps/server/src/project/AgentSessionImporter.test.ts b/apps/server/src/project/AgentSessionImporter.test.ts index 4c6109412d961..18c0946144d4a 100644 --- a/apps/server/src/project/AgentSessionImporter.test.ts +++ b/apps/server/src/project/AgentSessionImporter.test.ts @@ -16,7 +16,6 @@ import { type OrchestrationV2ProviderThread, } from "@t3tools/contracts"; import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; -import * as KeyedLock from "@t3tools/shared/KeyedLock"; import * as AcpRegistryRuntimeCoordinator from "@t3tools/provider-acp-registry/server/AcpRegistryRuntimeCoordinator"; import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"; import * as Context from "effect/Context"; @@ -185,10 +184,10 @@ const seedOwner = Effect.fn(function* ( }); const makeImporter = Effect.fn(function* ( - candidates: ReadonlyArray>, + candidates: ReadonlyArray, beforeTransaction: Effect.Effect = Effect.void, getAdapter?: ProviderAdapterRegistry.ProviderAdapterRegistryV2Shape["get"], - threadCommands?: KeyedLock.KeyedLock, + threadCommands?: ThreadCommandExecutor.ThreadCommandExecutor["Service"], settings: Layer.Layer< ServerSettings.ServerSettingsService, ServerSettingsError @@ -271,7 +270,7 @@ const makeImporter = Effect.fn(function* ( ), Layer.succeed( ThreadCommandExecutor.ThreadCommandExecutor, - threadCommands ?? (yield* KeyedLock.make()), + threadCommands ?? (yield* ThreadCommandExecutor.make), ), ), ), @@ -484,8 +483,8 @@ it.effect( expect(yield* runtimes.list()).toEqual([]); yield* sql`DROP TRIGGER fail_runtime`; - expect(yield* importer.run).toEqual({ importedCount: 1, skippedCount: 0 }); - expect(yield* importer.run).toEqual({ importedCount: 1, skippedCount: 0 }); + expect(yield* importer.run).toEqual({ importedCount: 0, skippedCount: 0 }); + expect(yield* importer.run).toEqual({ importedCount: 0, skippedCount: 0 }); expect(yield* events.read({ threadId }).pipe(Stream.runCollect)).toEqual(committed); expect(importer.published).toHaveLength(6); expect( @@ -501,6 +500,49 @@ it.effect( }).pipe(Effect.scoped, Effect.provide(persistence)), ); +it.effect("counts only new threads when re-importing cached and recovered transcripts", () => + Effect.gen(function* () { + const events = yield* EventStore.EventStoreV2; + const runtimes = yield* ProviderSessionRuntime.ProviderSessionRuntimeRepository; + const cached = candidate( + CLAUDE_MODEL_SELECTION, + "020e7f00-dce6-406b-8ec6-ed90498945d6", + "claudeAgent", + ); + const recovered = candidate(CODEX_MODEL_SELECTION, "recovered"); + const initial = yield* makeImporter([cached, recovered]); + expect(yield* initial.run).toEqual({ importedCount: 2, skippedCount: 0 }); + const before = yield* events.read({ threadId: importId(recovered) }).pipe(Stream.runCollect); + // The file changed on disk but still holds the imported messages. + recovered.source.size += 100; + recovered.source.mtimeMs += 1; + const duplicate = { ...recovered.source, filePath: "/tmp/duplicate.jsonl" }; + const fresh = candidate(CODEX_MODEL_SELECTION, "fresh"); + const repeated = yield* makeImporter([ + { _tag: "AlreadyImported", source: cached.source }, + recovered, + { _tag: "Duplicate", source: duplicate }, + fresh, + { _tag: "Skipped" }, + ]); + + expect(yield* repeated.run).toEqual({ importedCount: 1, skippedCount: 1 }); + expect(yield* events.read({ threadId: importId(recovered) }).pipe(Stream.runCollect)).toEqual( + before, + ); + expect( + new Set( + repeated.published.flatMap((stored) => ("event" in stored ? [stored.event.threadId] : [])), + ), + ).toEqual(new Set([importId(fresh)])); + expect( + Option.getOrThrow(yield* runtimes.getByThreadId({ threadId: importId(recovered) })), + ).toMatchObject({ + runtimePayload: { importedTranscripts: [recovered.source, duplicate] }, + }); + }).pipe(Effect.scoped, Effect.provide(persistence)), +); + it.effect("does not certify new native history after a partial import retry", () => Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; @@ -773,7 +815,7 @@ const importDuringCommand = Effect.fn(function* ( const projections = yield* ProjectionStore.ProjectionStoreV2; const sink = yield* EventSink.EventSinkV2; const threadId = yield* seedOwner(candidate(CODEX_MODEL_SELECTION, name)); - const lock = yield* KeyedLock.make(); + const lock = yield* ThreadCommandExecutor.make; const loaded = yield* Deferred.make(); const importerCommitting = yield* Deferred.make(); const commandWritten = yield* Deferred.make(); @@ -1000,7 +1042,7 @@ it.effect("repeated imports reach linked-worktree sessions behind the scan budge }); expect(yield* importer.run).toEqual({ importedCount: 100, skippedCount: 1 }); - expect(yield* importer.run).toEqual({ importedCount: 101, skippedCount: 0 }); + expect(yield* importer.run).toEqual({ importedCount: 1, skippedCount: 0 }); const oldest = yield* ProjectionStore.ProjectionStoreV2.pipe( Effect.flatMap((projections) => projections.getThread(ThreadId.make("import:codex:session-100")), diff --git a/apps/server/src/project/AgentSessionImporter.ts b/apps/server/src/project/AgentSessionImporter.ts index 0ebd6688a28be..0f8736986a2df 100644 --- a/apps/server/src/project/AgentSessionImporter.ts +++ b/apps/server/src/project/AgentSessionImporter.ts @@ -279,7 +279,6 @@ const make = Effect.gen(function* () { ); if (outcome._tag === "AlreadyImported") { importedThreadIds.add(threadId); - importedCount += 1; return; } if (outcome._tag === "Duplicate") { @@ -350,7 +349,8 @@ const make = Effect.gen(function* () { } yield* persistRuntime; yield* runtimes.recordImportedTranscript({ threadId, source }); - return true; + importedThreadIds.add(threadId); + return null; } const model = thread.model ?? DEFAULT_MODEL_BY_PROVIDER[driver] ?? DEFAULT_MODEL; diff --git a/apps/server/src/project/AgentSessionScanner.test.ts b/apps/server/src/project/AgentSessionScanner.test.ts index 6dac5a1b0b6c9..ce75e456158a8 100644 --- a/apps/server/src/project/AgentSessionScanner.test.ts +++ b/apps/server/src/project/AgentSessionScanner.test.ts @@ -2024,6 +2024,144 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { }), ); + it.effect("ignores sessions without visible messages without spending the import budget", () => + 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-"); + const codexRollout = (name: string) => + path.join(codexHomePath, "sessions", "2026", "08", "24", `rollout-${name}.jsonl`); + // Newer empty sessions would exhaust the 100-transcript import budget before any real one. + const emptySessions = Array.from({ length: 100 }, (_, index) => + index % 2 === 0 + ? { + filePath: codexRollout(`empty-${index}`), + records: [ + { + type: "session_meta", + payload: { id: `codex-empty-${index}`, cwd: workspace, source: "vscode" }, + }, + { + type: "event_msg", + payload: { + type: "thread_settings_applied", + thread_settings: { cwd: workspace }, + }, + }, + ], + } + : { + filePath: path.join( + claudeHomePath, + "projects", + "-slug", + `claude-empty-${index}.jsonl`, + ), + records: [ + { type: "system", subtype: "informational", content: "Status", cwd: workspace }, + { + type: "user", + isMeta: true, + message: { + role: "user", + content: "Caveat", + }, + cwd: workspace, + }, + ], + }, + ); + const transcripts = [ + ...emptySessions, + { + // Without session_meta there is no resumable session ID. + filePath: codexRollout("unusable"), + records: [ + { type: "turn_context", payload: { cwd: workspace } }, + { + type: "event_msg", + payload: { type: "user_message", message: "Import this prompt" }, + }, + ], + }, + { + filePath: codexRollout("real"), + records: [ + { type: "session_meta", payload: { id: "real-session", cwd: workspace } }, + { + type: "event_msg", + payload: { type: "user_message", message: "Import this prompt" }, + }, + ], + }, + ]; + for (const [index, { filePath, records }] of transcripts.entries()) { + yield* writeTranscript({ + filePath, + contents: records.map((record) => encodeTranscriptRecord(record)).join("\n"), + mtimeMs: nowMs - index * 60_000, + }); + } + + const outcomes = yield* runRecentThreadOutcomes({ + claudeHomePath, + codexHomePath, + workspaceRoot: workspace, + }); + + expect( + outcomes.map((outcome) => + outcome._tag === "Importable" ? outcome.thread.providerSessionId : outcome._tag, + ), + ).toEqual(["Skipped", "real-session"]); + }), + ); + + it.effect("skips a rewound Codex rollout whose inherited history is unavailable", () => + 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-"); + const threadId = "019a0000-0000-7000-8000-000000000001"; + const rolloutId = "019a0000-0000-7000-8000-000000000002"; + // The ancestor rollout is missing, and this rollout added no messages of its own. + yield* writeTranscript({ + filePath: path.join( + codexHomePath, + "sessions", + "2026", + "08", + "24", + `rollout-2026-08-24T11-00-00-${threadId}_${rolloutId}.jsonl`, + ), + contents: encodeTranscriptRecord({ + type: "session_meta", + payload: { + id: threadId, + cwd: workspace, + history_mode: "paginated", + history_base: { thread_id: threadId, end_ordinal_exclusive: 3, end_byte_offset: 512 }, + }, + }), + mtimeMs: nowMs, + }); + + const outcomes = yield* runRecentThreadOutcomes({ + claudeHomePath, + codexHomePath, + workspaceRoot: workspace, + }); + + expect(outcomes).toEqual([{ _tag: "Skipped" }]); + }), + ); + it.effect("skips excessive records without blocking an older valid transcript", () => Effect.gen(function* () { const path = yield* Path.Path; @@ -3475,6 +3613,7 @@ describe("parseAgentSessionTranscript", () => { "\nGitHub\n", "# AGENTS.md instructions for /tmp/project\n\n\nRules\n", '\nTab\n', + '{"page_id":null}', "Fix the import titles", ].join("\n"); const thread = AgentSessionScanner.parseAgentSessionTranscript({ @@ -3495,6 +3634,27 @@ describe("parseAgentSessionTranscript", () => { expect(thread?.messages.map((message) => message.text)).toEqual([prompt]); }); + it.each([ + "Explain in this transcript", + "Keep this incomplete example", + ])("keeps Codex page markup that is not a complete leading block: %s", (prompt) => { + const thread = AgentSessionScanner.parseAgentSessionTranscript({ + contents: [ + encodeTranscriptRecord({ type: "session_meta", payload: { id: "codex-session" } }), + encodeTranscriptRecord({ + type: "event_msg", + payload: { type: "user_message", message: prompt }, + }), + ].join("\n"), + source: "codex", + providerInstanceId: ProviderInstanceId.make("codex"), + fallbackSessionId: "fallback", + lastActiveAtMs: Date.parse("2026-08-25T08:00:00.000Z"), + }); + + expect(thread?.title).toBe(prompt); + }); + it("skips sessions without a visible user message", () => { const thread = AgentSessionScanner.parseAgentSessionTranscript({ contents: JSON.stringify({ diff --git a/apps/server/src/project/AgentSessionScanner.ts b/apps/server/src/project/AgentSessionScanner.ts index 92ccc4efcd746..7a89f9c687de0 100644 --- a/apps/server/src/project/AgentSessionScanner.ts +++ b/apps/server/src/project/AgentSessionScanner.ts @@ -369,7 +369,7 @@ function foldClaudeFields(fields: ClaudeFields, record: DecodedTranscriptRecord) // Codex records setup context as user text ahead of the real request. Skip // these leading blocks when deriving a title; imported messages keep them. const CODEX_TITLE_PREAMBLE = - /^(?:\s*(?:<(recommended_plugins|environment_context|user_instructions|INSTRUCTIONS|in-app-browser-context)(?:\s[^>]*)?>[\s\S]*?<\/\1>|# AGENTS\.md instructions\b[^\n]*|## My request for Codex:))+/; + /^(?:\s*(?:<(recommended_plugins|environment_context|user_instructions|INSTRUCTIONS|in-app-browser-context|external_codex_apps_open_page)(?:\s[^>]*)?>[\s\S]*?<\/\1>|# AGENTS\.md instructions\b[^\n]*|## My request for Codex:))+/; // Codex appends an entry per rename; like Codex, the newest nonempty name wins. function parseCodexThreadNames(contents: string): ReadonlyMap { @@ -447,7 +447,8 @@ export function parseAgentSessionTranscript( input.source === "claudeAgent" ? records.reduce(foldClaudeFields, emptyClaudeFields) : emptyClaudeFields; - return parseAgentSessionRecords(input, records, claudeFields, 0); + const thread = parseAgentSessionRecords(input, records, claudeFields, 0); + return thread === "empty" ? null : thread; } function parseAgentSessionRecords( @@ -455,7 +456,7 @@ function parseAgentSessionRecords( records: ReadonlyArray, claudeFields: ClaudeFields, droppedMessageCount: number, -): AgentSessionThread | null { +): AgentSessionThread | "empty" | null { const fallbackTimestamp = DateTime.formatIso(DateTime.makeUnsafe(input.lastActiveAtMs)); // Claude filenames are session IDs. Codex rollout filenames include extra // timestamp text, so only transcript metadata can provide a resumable ID. @@ -648,6 +649,7 @@ function parseAgentSessionRecords( }); } + if (messages.length === 0 && droppedMessageCount === 0) return "empty"; const visibleMessages = messages.map( ({ codexResponseUser: _codexResponseUser, ...message }) => message, ); @@ -2139,6 +2141,15 @@ export const make = Effect.gen(function* () { snapshot.claudeFields, snapshot.droppedMessageCount, ); + if (parsedThread === "empty") { + // null means an unreadable history base, which may hold the conversation. + if (candidate.source === "codex" && inheritedRecords === null) { + return Option.some({ _tag: "Skipped" }); + } + // Never recorded as imported, so charging them would starve real sessions every run. + transcriptsRemaining += 1; + return Option.none(); + } if (parsedThread === null) { return Option.some({ _tag: "Skipped" }); } diff --git a/apps/server/src/project/ManagedProjectFolders.test.ts b/apps/server/src/project/ManagedProjectFolders.test.ts index 011b54a6851b3..07da49c732539 100644 --- a/apps/server/src/project/ManagedProjectFolders.test.ts +++ b/apps/server/src/project/ManagedProjectFolders.test.ts @@ -1,7 +1,13 @@ import { assert, it } from "@effect/vitest"; import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; import * as NodeServices from "@effect/platform-node/NodeServices"; -import { CommandId, ProjectId, ThreadId, VcsProcessSpawnError } from "@t3tools/contracts"; +import { + CommandId, + ProjectId, + ThreadId, + VcsProcessSpawnError, + VcsProcessTimeoutError, +} from "@t3tools/contracts"; import * as HostProcess from "@t3tools/shared/HostProcess"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; @@ -174,6 +180,44 @@ it.effect("offers a Scratch folder when Git is not installed", () => ), ); +it.effect("offers the Scratch folder once a Git check that timed out is asked again", () => + withScratch( + ({ baseDir }) => + Effect.gen(function* () { + const path = yield* Path.Path; + const scratch = yield* ManagedProjectFolders.ManagedProjectFolders; + assert.isTrue(Option.isNone(yield* scratch.scratchRoot)); + assert.equal(yield* requireRoot, path.resolve(baseDir, "scratch")); + }), + { + vcsProcess: Layer.effect( + VcsProcess.VcsProcess, + Effect.map(VcsProcess.make, (real) => { + let timedOut = false; + return VcsProcess.VcsProcess.of({ + run: (input) => + Effect.suspend(() => { + if (timedOut) return real.run(input); + timedOut = true; + return Effect.fail( + new VcsProcessTimeoutError({ + operation: input.operation, + command: input.command, + cwd: input.cwd, + timeoutMs: 5_000, + }), + ); + }), + }); + }), + ).pipe( + Layer.provide(ProcessRunner.layer), + Layer.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), + ), + }, + ), +); + it.effect("creates one Scratch project, even for concurrent first requests", () => withScratch(() => Effect.gen(function* () { @@ -274,6 +318,15 @@ it.effect("gives each Scratch thread its own folder, named from its message", () ); assert.equal(path.dirname(pasted), root); assert.isAtMost(path.basename(pasted).length, 80); + + const accented = Option.getOrThrow( + yield* scratch.folderForThread({ + projectId, + threadId: ThreadId.make("thread-diacritics"), + text: "tìm kiếm skill", + }), + ); + assert.match(path.basename(accented), /^\d{4}-\d{2}-\d{2}-tim-kiem-skill-/); }), ), ); diff --git a/apps/server/src/project/ManagedProjectFolders.ts b/apps/server/src/project/ManagedProjectFolders.ts index 007f784374c15..d526c381fb404 100644 --- a/apps/server/src/project/ManagedProjectFolders.ts +++ b/apps/server/src/project/ManagedProjectFolders.ts @@ -6,7 +6,7 @@ import { type ThreadId, VcsProcessSpawnError, } from "@t3tools/contracts"; -import { newProjectFolderName } from "@t3tools/shared/path"; +import { foldLatinDiacritics, newProjectFolderName } from "@t3tools/shared/path"; import * as Cause from "effect/Cause"; import * as Context from "effect/Context"; import * as SecureRandom from "effect/crypto/SecureRandom"; @@ -133,7 +133,7 @@ export class ManagedProjectFolders extends Context.Service< >()("t3/project/ManagedProjectFolders") {} const folderWords = (text: string) => - text + foldLatinDiacritics(text) .toLowerCase() .split(/[^a-z0-9]+/) .filter(Boolean) @@ -268,15 +268,18 @@ const make = Effect.gen(function* () { return Effect.logWarning(unavailable.message).pipe(Effect.as(Result.fail(unavailable))); }, ), - Effect.catchCause((cause) => - Cause.hasInterrupts(cause) - ? Effect.interrupt - : Effect.succeed(Result.fail(new ScratchUnavailableError({ baseDir: config.baseDir }))), - ), ), - Duration.infinity, + // A failed git check (say, a timeout on a busy machine) is not an answer; ask again next time. + Exit.match({ onSuccess: () => Duration.infinity, onFailure: () => Duration.zero }), + ); + const scratchProbe = probe.pipe( + Effect.catchCause((cause) => + Cause.hasInterrupts(cause) + ? Effect.interrupt + : Effect.succeed(Result.fail(new ScratchUnavailableError({ baseDir: config.baseDir }))), + ), + Effect.onInterrupt(() => invalidate), ); - const scratchProbe = probe.pipe(Effect.onInterrupt(() => invalidate)); // A symlinked scratch folder lets the No project live at either the link or its target. const scratchProjectRoot = (root: string) => fileSystem.realPath(root).pipe( diff --git a/apps/server/src/project/ProjectService.ts b/apps/server/src/project/ProjectService.ts index 4bdc8386438ca..1db031456f2d7 100644 --- a/apps/server/src/project/ProjectService.ts +++ b/apps/server/src/project/ProjectService.ts @@ -538,7 +538,7 @@ export const make = Effect.gen(function* () { projectThreads, (thread) => threadCommands - .withLock(thread.id, deleteChildThread(input, thread.id)) + .withDeletionLock(threadCommands.withLock(thread.id, deleteChildThread(input, thread.id))) .pipe( Effect.mapError( (cause) => diff --git a/apps/server/src/project/ProjectSetupScriptRunner.test.ts b/apps/server/src/project/ProjectSetupScriptRunner.test.ts index 0e284818103d5..42c231f8123d0 100644 --- a/apps/server/src/project/ProjectSetupScriptRunner.test.ts +++ b/apps/server/src/project/ProjectSetupScriptRunner.test.ts @@ -179,6 +179,18 @@ describe("ProjectSetupScriptRunner", () => { }), ); + it.effect("drops whole escape sequences from output lines", () => + Effect.gen(function* () { + const { outputLines, emit } = yield* setup(); + yield* emit({ + ...event, + type: "output", + data: "\x1b7\x1b[24;0fProgress: [ 40%]\x1b8\r\n\x1b%G\x1b#8\x1b[4:3mdone\x1bM\r\n", + }); + expect(yield* Queue.takeAll(outputLines)).toEqual(["Progress: [ 40%]", "done"]); + }), + ); + it.effect("types into a quiet shell that has no line editor", () => Effect.gen(function* () { const { writes } = yield* setup(); diff --git a/apps/server/src/project/ProjectSetupScriptRunner.ts b/apps/server/src/project/ProjectSetupScriptRunner.ts index 3c159aab20d0c..fb1db33c289c4 100644 --- a/apps/server/src/project/ProjectSetupScriptRunner.ts +++ b/apps/server/src/project/ProjectSetupScriptRunner.ts @@ -146,12 +146,10 @@ function completionSentinelPattern(token: string): RegExp { return new RegExp(`${COMPLETION_SENTINEL_PREFIX}_${token}:(-?\\d+)`); } +// ECMA-48: CSI, OSC, then any other escape as intermediates (0x20-0x2F) and one final byte. function stripTerminalControl(text: string): string { return text - .replace( - /\x1b\[[0-9;?]*[ -/]*[@-~]|\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)|\x1b[()][A-Za-z0-9]|\x1b[=>]/g, - "", - ) + .replace(/\x1b\[[0-?]*[ -/]*[@-~]|\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)|\x1b[ -/]*[0-~]/g, "") .replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, ""); } diff --git a/apps/server/src/project/RepositoryIdentityResolver.ts b/apps/server/src/project/RepositoryIdentityResolver.ts index 4bb4e024ae151..826529e704b23 100644 --- a/apps/server/src/project/RepositoryIdentityResolver.ts +++ b/apps/server/src/project/RepositoryIdentityResolver.ts @@ -1,7 +1,7 @@ import type { RepositoryIdentity, SourceControlProviderError } from "@t3tools/contracts"; import { parseGitRemoteHost } from "@t3tools/source-control-core/shared/definition"; import { repositoryPathFromRemoteUrl } from "@t3tools/source-control-core/server/SourceControlProvider"; -import { normalizeGitRemoteUrl } from "@t3tools/shared/git"; +import { canonicalRepositoryPath, normalizeGitRemoteUrl } from "@t3tools/shared/git"; import * as Cache from "effect/Cache"; import * as Context from "effect/Context"; import * as Data from "effect/Data"; @@ -126,8 +126,9 @@ function repositoryPathOf(canonicalKey: string, remoteUrl: string): string { if (displayPath === null) return canonicalPath; // A host that names repositories literally keeps the `.git` the display path drops. return ( - [displayPath, `${displayPath}.git`].find((path) => path.toLowerCase() === canonicalPath) ?? - canonicalPath + [displayPath, `${displayPath}.git`].find( + (path) => canonicalRepositoryPath(path) === canonicalRepositoryPath(canonicalPath), + ) ?? canonicalPath ); } diff --git a/apps/server/src/provider/ClaudeCapabilitiesProbe.test.ts b/apps/server/src/provider/ClaudeCapabilitiesProbe.test.ts index e2622bc9c2a65..b89cffa423874 100644 --- a/apps/server/src/provider/ClaudeCapabilitiesProbe.test.ts +++ b/apps/server/src/provider/ClaudeCapabilitiesProbe.test.ts @@ -30,7 +30,7 @@ it("drops strict MCP mode, and only that, next to an administrator's managed-mcp environment: { HOME: "/home/user" }, cwd: undefined, managedMcpConfig: true, - spawnThroughEnv: false, + spawnVia: undefined, }); // Claude exits before initializing when handed --strict-mcp-config here. diff --git a/apps/server/src/provider/ClaudeModelCatalog.test.ts b/apps/server/src/provider/ClaudeModelCatalog.test.ts index 2be0e69bf02b1..bbcc02f901329 100644 --- a/apps/server/src/provider/ClaudeModelCatalog.test.ts +++ b/apps/server/src/provider/ClaudeModelCatalog.test.ts @@ -137,6 +137,13 @@ describe("Claude model catalog", () => { formatClaudeVersionUpgradeMessage(catalog, "3.1.9"), "Claude Code v3.1.9 is too old for Claude Synthetic Next. Upgrade to v3.2.0 or newer to access it.", ); + // Claude Code before 2.1.321 does not know claude-fable-5-2 and runs it with a 200K window. + assert.notInclude( + resolveClaudeModelsForVersion(BUNDLED_CLAUDE_MODEL_CATALOG, "2.1.320").map( + (model) => model.slug, + ), + "claude-fable-5-2", + ); }); it("resolves aliases and declarative adapter mappings", () => { diff --git a/apps/server/src/provider/ClaudeProvider.ts b/apps/server/src/provider/ClaudeProvider.ts index 0fb7ea8b2e1fd..b657a2b813feb 100644 --- a/apps/server/src/provider/ClaudeProvider.ts +++ b/apps/server/src/provider/ClaudeProvider.ts @@ -23,6 +23,7 @@ import { ChildProcess, ChildProcessSpawner } from "effect/process"; import { createModelCapabilities } from "@t3tools/shared/model"; import { envTrampoline, supportsEnvChdir } from "@t3tools/shared/envTrampoline"; import * as HostProcess from "@t3tools/shared/HostProcess"; +import * as SpawnWorker from "../process/SpawnWorker.ts"; import { resolveSpawnCommand } from "@t3tools/shared/shell"; import { query as claudeQuery, @@ -177,11 +178,16 @@ function formatClaudeSubscriptionAuthLabel(subscriptionType: string): string { return `Claude ${subscriptionLabel} Subscription`; } +function hasApiKey(apiKeySource: string | undefined): boolean { + return Boolean(apiKeySource) && apiKeySource !== "none"; +} + function claudeAuthMetadata(input: { readonly subscriptionType: string | undefined; readonly authMethod: string | undefined; + readonly apiKeySource: string | undefined; }): { readonly type: string; readonly label: string } | undefined { - if (normalizeClaudeAuthMethod(input.authMethod) === "apiKey") { + if (normalizeClaudeAuthMethod(input.authMethod) === "apiKey" || hasApiKey(input.apiKeySource)) { return { type: "apiKey", label: "Claude API Key", @@ -216,8 +222,7 @@ function claudeAuthStatus( } if (capabilities.tokenSource !== "none") return "authenticated"; // API keys can authenticate with tokenSource: none; also check key and account fields. - const hasApiKey = Boolean(capabilities.apiKeySource) && capabilities.apiKeySource !== "none"; - return hasApiKey || capabilities.email || capabilities.subscriptionType + return hasApiKey(capabilities.apiKeySource) || capabilities.email || capabilities.subscriptionType ? "authenticated" : "unauthenticated"; } @@ -255,16 +260,27 @@ export function spawnClaudeProbeThroughEnv({ }); } +/** Windows runs CreateProcess on the calling thread, so the probe launches from a launcher thread. */ +function spawnClaudeProbeOffThread({ command, args, cwd, env, signal }: ClaudeSpawnOptions) { + const child = SpawnWorker.spawnPiped(command, args, { cwd, env, signal, windowsHide: true }); + // The probe ignores stderr; drain it so a full pipe cannot stall the CLI. + child.stderr.resume(); + return child; +} + export function buildClaudeCapabilitiesProbeQueryOptions(input: { readonly executablePath: string; readonly abortController: AbortController; readonly environment: NodeJS.ProcessEnv; readonly cwd: string | undefined; readonly managedMcpConfig: boolean; - readonly spawnThroughEnv: boolean; + readonly spawnVia: "env" | "launcher-thread" | undefined; }): ClaudeQueryOptions { return { - ...(input.spawnThroughEnv ? { spawnClaudeCodeProcess: spawnClaudeProbeThroughEnv } : {}), + ...(input.spawnVia === "env" ? { spawnClaudeCodeProcess: spawnClaudeProbeThroughEnv } : {}), + ...(input.spawnVia === "launcher-thread" + ? { spawnClaudeCodeProcess: spawnClaudeProbeOffThread } + : {}), persistSession: false, pathToClaudeCodeExecutable: input.executablePath, abortController: input.abortController, @@ -494,8 +510,13 @@ const probeClaudeCapabilities = ( ...(yield* readClaudeSettingsEnv(claudeSettings, cwd, claudeEnvironment)), }) : 0; - const spawnThroughEnv = - (yield* HostProcess.Platform) === "linux" && (yield* Effect.promise(supportsEnvChdir)); + const platform = yield* HostProcess.Platform; + const spawnVia = + platform === "win32" + ? "launcher-thread" + : platform === "linux" && (yield* Effect.promise(supportsEnvChdir)) + ? "env" + : undefined; return yield* Effect.tryPromise({ try: async () => { const q = claudeQuery({ @@ -509,7 +530,7 @@ const probeClaudeCapabilities = ( environment: claudeEnvironment, cwd, managedMcpConfig, - spawnThroughEnv, + spawnVia, }), }); const init = await q.initializationResult(); @@ -844,6 +865,7 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")( claudeAuthMetadata({ subscriptionType: capabilities.subscriptionType, authMethod: capabilities.tokenSource, + apiKeySource: capabilities.apiKeySource, }); const usageCheckedAt = capabilities.usageCheckedAt ?? checkedAt; const usageLimits = !capabilities.usage diff --git a/apps/server/src/provider/CodexMcpElicitation.test.ts b/apps/server/src/provider/CodexMcpElicitation.test.ts index 66dd7ddb6e7d0..60a4b7fe8f72e 100644 --- a/apps/server/src/provider/CodexMcpElicitation.test.ts +++ b/apps/server/src/provider/CodexMcpElicitation.test.ts @@ -146,7 +146,7 @@ describe("Codex MCP elicitation approvals", () => { } satisfies EffectCodexSchema.McpServerElicitationRequestParams; NodeAssert.deepStrictEqual(toMcpElicitationResponse(inputRequest, "accept"), { - action: "decline", + action: "cancel", }); }); @@ -162,7 +162,7 @@ describe("Codex MCP elicitation approvals", () => { } satisfies EffectCodexSchema.McpServerElicitationRequestParams; NodeAssert.deepStrictEqual(toMcpElicitationResponse(urlRequest, "accept"), { - action: "decline", + action: "cancel", }); }); diff --git a/apps/server/src/provider/CodexMcpElicitation.ts b/apps/server/src/provider/CodexMcpElicitation.ts index eafe09e07e706..c8362dad1e84b 100644 --- a/apps/server/src/provider/CodexMcpElicitation.ts +++ b/apps/server/src/provider/CodexMcpElicitation.ts @@ -185,8 +185,10 @@ export function toMcpElicitationResponse( return { action: decision }; } + // T3 cannot answer these itself. `decline` reads as the user's refusal, and Codex's browser + // plugin saves it as a thread-wide site denial; `cancel` leaves no saved decision. if (payload.mode === "url") { - return { action: "decline" }; + return { action: "cancel" }; } const persist = @@ -217,7 +219,7 @@ export function toMcpElicitationResponse( } if (form?.required?.some((key) => !Object.hasOwn(content, key))) { - return { action: "decline" }; + return { action: "cancel" }; } return { diff --git a/apps/server/src/provider/CodexProvider.test.ts b/apps/server/src/provider/CodexProvider.test.ts index ad15bf0896eb2..a70ea15a73570 100644 --- a/apps/server/src/provider/CodexProvider.test.ts +++ b/apps/server/src/provider/CodexProvider.test.ts @@ -1,12 +1,150 @@ import { assert, it } from "@effect/vitest"; +import * as Cause from "effect/Cause"; +import * as Deferred from "effect/Deferred"; +import * as Effect from "effect/Effect"; +import * as Exit from "effect/Exit"; +import * as Queue from "effect/Queue"; +import * as Scope from "effect/Scope"; +import * as Sink from "effect/Sink"; +import * as Stream from "effect/Stream"; +import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; +import * as TestClock from "effect/testing/TestClock"; +import * as HostProcess from "@t3tools/shared/HostProcess"; import { applyPreferredCodexDefaultModel, codexCustomEndpoint, codexRoutesSpendNativeQuota, + makeCodexSkillsProbe, mapCodexModelCapabilities, } from "./CodexProvider.ts"; +function makeFakeCodexAppServers() { + const helpers: Array<{ + readonly codexHome: unknown; + readonly crash: Effect.Effect; + readonly requests: Array; + closed: boolean; + }> = []; + const spawner = ChildProcessSpawner.make((command) => + Effect.gen(function* () { + const output = yield* Queue.unbounded(); + const exited = yield* Deferred.make(); + const crash = Deferred.succeed(exited, ChildProcessSpawner.ExitCode(1)).pipe( + Effect.andThen(Queue.end(output)), + Effect.asVoid, + ); + const helper = { + codexHome: command._tag === "StandardCommand" ? command.options.env?.CODEX_HOME : undefined, + crash, + requests: [] as Array, + closed: false, + }; + helpers.push(helper); + yield* Effect.addFinalizer(() => Effect.sync(() => (helper.closed = true))); + const respond = (id: unknown, result: unknown) => + Queue.offer(output, new TextEncoder().encode(`${JSON.stringify({ id, result })}\n`)); + return ChildProcessSpawner.makeHandle({ + pid: ChildProcessSpawner.ProcessId(900_000_000 + helpers.length), + exitCode: Deferred.await(exited), + isRunning: Effect.map(Deferred.isDone(exited), (done) => !done), + kill: () => crash, + unref: Effect.succeed(Effect.void), + stdin: Sink.forEach((chunk: string | Uint8Array) => + Effect.forEach( + (typeof chunk === "string" ? chunk : new TextDecoder().decode(chunk)) + .split("\n") + .filter(Boolean), + (line) => { + const frame = JSON.parse(line) as { + readonly id?: number; + readonly method: string; + readonly params?: { readonly cwds: ReadonlyArray }; + }; + if (frame.id === undefined) return Effect.void; + if (frame.method === "initialize") { + return respond(frame.id, { + codexHome: "/codex-home", + platformFamily: "unix", + platformOs: "linux", + userAgent: "codex/0.0.0", + }); + } + helper.requests.push(frame.params); + return respond(frame.id, { + data: (frame.params?.cwds ?? []).map((cwd) => ({ + cwd, + errors: [], + skills: [ + { + name: `skill-in-${cwd}`, + path: `${cwd}/SKILL.md`, + enabled: true, + description: "", + scope: "repo", + }, + ], + })), + }); + }, + { discard: true }, + ), + ), + stdout: Stream.fromQueue(output), + stderr: Stream.empty, + all: Stream.empty, + getInputFd: () => Sink.drain, + getOutputFd: () => Stream.empty, + }); + }), + ); + return { helpers, spawner }; +} + +it.effect("reuses one Codex skills helper per launch until it idles out", () => + Effect.gen(function* () { + const { helpers, spawner } = makeFakeCodexAppServers(); + const providerScope = yield* Scope.make(); + const probe = yield* makeCodexSkillsProbe().pipe( + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), + Scope.provide(providerScope), + ); + const read = (cwd: string, homePath = "/home-a") => + probe({ binaryPath: "codex", homePath, cwd, environment: {} }).pipe( + Effect.map((skills) => skills.map((skill) => skill.name)), + ); + + assert.deepStrictEqual(yield* read("/alpha"), ["skill-in-/alpha"]); + assert.deepStrictEqual(yield* read("/beta"), ["skill-in-/beta"]); + assert.strictEqual(helpers.length, 1); + assert.deepStrictEqual(helpers[0]!.requests, [ + { cwds: ["/alpha"], forceReload: true }, + { cwds: ["/beta"], forceReload: true }, + ]); + + // Another home is another launch identity; the previous helper retires at once. + assert.deepStrictEqual(yield* read("/alpha", "/home-b"), ["skill-in-/alpha"]); + assert.strictEqual(helpers.length, 2); + assert.strictEqual(helpers[0]!.closed, true); + assert.strictEqual(helpers[1]!.codexHome, "/home-b"); + + // A helper that exits is replaced within the same read. + yield* helpers[1]!.crash; + assert.deepStrictEqual(yield* read("/beta", "/home-b"), ["skill-in-/beta"]); + assert.strictEqual(helpers.length, 3); + + yield* TestClock.adjust("29 seconds"); + assert.strictEqual(helpers[2]!.closed, false); + yield* TestClock.adjust("1 second"); + assert.strictEqual(helpers[2]!.closed, true); + assert.deepStrictEqual(yield* read("/alpha", "/home-b"), ["skill-in-/alpha"]); + assert.strictEqual(helpers.length, 4); + + yield* Scope.close(providerScope, Exit.void); + assert.strictEqual(helpers[3]!.closed, true); + }).pipe(Effect.provideService(HostProcess.Platform, "linux")), +); + it("maps current Codex model capability fields", () => { const capabilities = mapCodexModelCapabilities({ additionalSpeedTiers: [], diff --git a/apps/server/src/provider/CodexProvider.ts b/apps/server/src/provider/CodexProvider.ts index df105d3ba20fe..7dcf9079f82b3 100644 --- a/apps/server/src/provider/CodexProvider.ts +++ b/apps/server/src/provider/CodexProvider.ts @@ -3,10 +3,12 @@ import { pipe } from "effect/Function"; import * as DateTime from "effect/DateTime"; import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; +import * as Equal from "effect/Equal"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as Path from "effect/Path"; +import * as RcMap from "effect/RcMap"; import * as Result from "effect/Result"; import * as Schema from "effect/Schema"; import * as Schedule from "effect/Schedule"; @@ -839,6 +841,52 @@ export const probeCodexSkillsForCwd = Effect.fn("probeCodexSkillsForCwd")(functi return parseCodexSkillsListResponse(skillsResponse, input.cwd); }); +// Long enough for a burst of workspace switches; short because the helper holds the user's +// binary open, and Windows refuses to overwrite or delete a running executable. +const CODEX_SKILLS_HELPER_IDLE_TTL = "30 seconds"; + +/** Reuses one app-server per launch identity; each read passes its cwd and rescans from disk. */ +export const makeCodexSkillsProbe = Effect.fn("makeCodexSkillsProbe")(function* () { + const helpers = yield* RcMap.make({ + lookup: (launch: { + readonly binaryPath: string; + readonly homePath?: string; + readonly launchArgs?: string; + readonly environment: NodeJS.ProcessEnv; + }) => + withCodexAppServerClient({ ...launch, cwd: process.cwd() }).pipe( + Effect.map(({ client }) => client), + ), + idleTimeToLive: CODEX_SKILLS_HELPER_IDLE_TTL, + }); + return Effect.fn("CodexSkillsProbe.probe")(function* (input: { + readonly binaryPath: string; + readonly homePath?: string; + readonly launchArgs?: string; + readonly cwd: string; + readonly environment?: NodeJS.ProcessEnv; + }) { + const { cwd, ...rest } = input; + const launch = { + ...rest, + environment: providerProbeEnvironment(rest.environment ?? process.env), + }; + // Retire helpers for a previous binary, home, credentials, or launch options. + const stale = Array.from(yield* RcMap.keys(helpers)).filter( + (key) => !Equal.equals(key, launch), + ); + yield* Effect.forEach(stale, (key) => RcMap.invalidate(helpers, key), { discard: true }); + const skillsResponse = yield* RcMap.get(helpers, launch).pipe( + Effect.flatMap((client) => client.request("skills/list", { cwds: [cwd], forceReload: true })), + Effect.scoped, + // RcMap keeps a failed spawn, and a dead helper keeps failing; drop it so the retry respawns. + Effect.onError(() => RcMap.invalidate(helpers, launch)), + Effect.retry({ times: 1 }), + ); + return parseCodexSkillsListResponse(skillsResponse, cwd); + }); +}); + const emptyCodexModelsFromSettings = (codexSettings: CodexSettings): ServerProvider["models"] => appendCustomCodexModels([], codexSettings.customModels); diff --git a/apps/server/src/provider/Drivers/AntigravityUsage.ts b/apps/server/src/provider/Drivers/AntigravityUsage.ts index 5cb55ed77ac09..2eb9744a3e42a 100644 --- a/apps/server/src/provider/Drivers/AntigravityUsage.ts +++ b/apps/server/src/provider/Drivers/AntigravityUsage.ts @@ -15,7 +15,11 @@ import * as Layer from "effect/Layer"; import * as Path from "effect/Path"; import { resolveAntigravityInstanceDirectories } from "../antigravityAuthSupport.ts"; -import { makeAntigravityUsageCache, readAntigravityUsage } from "./antigravityUsageReader.ts"; +import { + antigravityRateModel, + makeAntigravityUsageCache, + readAntigravityUsage, +} from "./antigravityUsageReader.ts"; export class AntigravityUsage extends Context.Service< AntigravityUsage, @@ -123,6 +127,7 @@ export const layer = Layer.effect(AntigravityUsage, make); export const antigravityUsageReader: ProviderUsageReader = { kind: "scan", provider: "antigravity", + rateModel: antigravityRateModel, scan: ({ instances, windowStartMs }) => AntigravityUsage.pipe(Effect.flatMap((usage) => usage.scan({ instances, windowStartMs }))), }; diff --git a/apps/server/src/provider/Drivers/ClaudeExecutable.test.ts b/apps/server/src/provider/Drivers/ClaudeExecutable.test.ts index e1319caf69b16..b6dc61470e497 100644 --- a/apps/server/src/provider/Drivers/ClaudeExecutable.test.ts +++ b/apps/server/src/provider/Drivers/ClaudeExecutable.test.ts @@ -9,6 +9,9 @@ const NPM_DIR = "C:\\Users\\dev\\AppData\\Roaming\\npm"; const NPM_SHIM = `${NPM_DIR}\\claude.cmd`; const NPM_PACKAGE_EXE = `${NPM_DIR}\\node_modules\\@anthropic-ai\\claude-code\\bin\\claude.exe`; const NPM_PACKAGE_CLI = `${NPM_DIR}\\node_modules\\@anthropic-ai\\claude-code\\cli.js`; +const WINGET_DIR = "C:\\Users\\dev\\AppData\\Local\\Microsoft\\WinGet"; +const WINGET_SHIM = `${WINGET_DIR}\\Links\\claude.cmd`; +const WINGET_EXE = `${WINGET_DIR}\\Packages\\Anthropic.ClaudeCode_Microsoft.Winget.Source_8wekyb3d8bbwe\\claude.exe`; function withWindowsResolution(input: { readonly resolvedCommand: string | undefined; @@ -37,6 +40,16 @@ describe("resolveClaudeSdkExecutablePath", () => { }), ); + it.effect("follows the WinGet launcher to the package's native binary", () => + Effect.gen(function* () { + expect( + yield* resolveClaudeSdkExecutablePath("claude", {}).pipe( + withWindowsResolution({ resolvedCommand: WINGET_SHIM, existingFiles: [WINGET_EXE] }), + ), + ).toBe(WINGET_EXE); + }), + ); + it.effect("follows .bat and .ps1 launcher shims the same way", () => Effect.gen(function* () { for (const shim of [`${NPM_DIR}\\claude.bat`, `${NPM_DIR}\\claude.ps1`]) { diff --git a/apps/server/src/provider/Drivers/ClaudeExecutable.ts b/apps/server/src/provider/Drivers/ClaudeExecutable.ts index 5e4cbb4db6efe..5797066446b99 100644 --- a/apps/server/src/provider/Drivers/ClaudeExecutable.ts +++ b/apps/server/src/provider/Drivers/ClaudeExecutable.ts @@ -13,10 +13,14 @@ const WINDOWS_SHIM_EXTENSIONS: ReadonlySet = new Set([".cmd", ".bat", ". export const isWindowsClaudeLauncherShimPath = (filePath: string): boolean => WINDOWS_SHIM_EXTENSIONS.has(NodePath.win32.extname(filePath).toLowerCase()); -/** New packages provide bin/claude.exe; older packages need a JavaScript runtime for cli.js. */ -const NPM_PACKAGE_ENTRY_CANDIDATES = [ +/** + * npm: new packages provide bin/claude.exe; older ones need a JavaScript runtime for cli.js. + * WinGet: Links\claude.cmd forwards to the package's native executable under ..\Packages. + */ +const PACKAGE_ENTRY_CANDIDATES = [ ["node_modules", "@anthropic-ai", "claude-code", "bin", "claude.exe"], ["node_modules", "@anthropic-ai", "claude-code", "cli.js"], + ["..", "Packages", "Anthropic.ClaudeCode_Microsoft.Winget.Source_8wekyb3d8bbwe", "claude.exe"], ] as const; export type ExecutableFileCheck = (filePath: string) => boolean; @@ -53,7 +57,7 @@ export const resolveClaudeSdkExecutablePath = Effect.fn("resolveClaudeSdkExecuta } const shimDirectory = NodePath.win32.dirname(resolved); - for (const entrySegments of NPM_PACKAGE_ENTRY_CANDIDATES) { + for (const entrySegments of PACKAGE_ENTRY_CANDIDATES) { const candidate = NodePath.win32.join(shimDirectory, ...entrySegments); if (isFile(candidate)) { return candidate; diff --git a/apps/server/src/provider/Drivers/ClaudeSkills.test.ts b/apps/server/src/provider/Drivers/ClaudeSkills.test.ts index 947d5f1a67eac..349294baa8c88 100644 --- a/apps/server/src/provider/Drivers/ClaudeSkills.test.ts +++ b/apps/server/src/provider/Drivers/ClaudeSkills.test.ts @@ -587,6 +587,96 @@ it.layer(NodeServices.layer)("discoverClaudeSkills", (it) => { }), ); + it.effect("discovers inline plugins from CLAUDE_CODE_PLUGIN_DIRS over installed namesakes", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const workspace = yield* fs.makeTempDirectoryScoped({ prefix: "t3-claude-skills-" }); + const configDir = path.join(workspace, "config"); + const named = path.join(workspace, "named"); + const unnamed = path.join(workspace, "unnamed"); + const inherited = path.join(workspace, "inherited"); + const muted = path.join(workspace, "muted"); + const installed = path.join(workspace, "installed-acme"); + yield* fs.makeDirectory(path.join(named, ".claude-plugin"), { recursive: true }); + yield* fs.writeFileString( + path.join(named, ".claude-plugin", "plugin.json"), + '{"name":"acme"}', + ); + yield* writeSkill(path.join(named, "skills"), "qa", "---\ndescription: QA a ticket\n---\n"); + yield* writeSkill(path.join(installed, "skills"), "old", "# Old"); + yield* writeSkill(path.join(unnamed, "skills"), "lint", "# Lint"); + yield* writeSkill(path.join(muted, "skills"), "hidden", "# Hidden"); + const quiet = path.join(workspace, "quiet"); + yield* fs.makeDirectory(path.join(quiet, ".claude-plugin"), { recursive: true }); + yield* fs.writeFileString( + path.join(quiet, ".claude-plugin", "plugin.json"), + '{"name":"quiet","defaultEnabled":false}', + ); + yield* writeSkill(path.join(quiet, "skills"), "hush", "# Hush"); + const bundle = path.join(workspace, "bundle"); + yield* fs.makeDirectory(path.join(bundle, "tools", ".claude-plugin"), { recursive: true }); + yield* fs.writeFileString( + path.join(bundle, "tools", ".claude-plugin", "plugin.json"), + '{"name":"tools"}', + ); + yield* writeSkill(path.join(bundle, "tools", "skills"), "fmt", "# Fmt"); + yield* writeSkill(path.join(bundle, "loose", "skills"), "ignored", "# Ignored"); + yield* writeSkill(path.join(inherited, "skills"), "stale", "# Stale"); + yield* writeSkill(path.join(workspace, "relative", "skills"), "skipped", "# Skipped"); + yield* fs.makeDirectory(path.join(configDir, "plugins"), { recursive: true }); + yield* fs.writeFileString( + path.join(configDir, "plugins", "installed_plugins.json"), + JSON.stringify({ plugins: { "acme@shop": [{ scope: "user", installPath: installed }] } }), + ); + yield* fs.writeFileString( + path.join(configDir, "settings.json"), + JSON.stringify({ + enabledPlugins: { "acme@shop": true, "muted@inline": false }, + env: { + CLAUDE_CODE_PLUGIN_DIRS: [named, "relative", ` ${unnamed} `, muted, quiet, bundle].join( + ";", + ), + }, + }), + ); + + const skills = yield* discoverClaudeSkills({ homePath: configDir }, workspace, { + CLAUDE_CODE_PLUGIN_DIRS: inherited, + }); + assert.deepEqual( + skills.map(({ name, scope, description }) => ({ name, scope, description })), + [ + { name: "acme:qa", scope: "user", description: "QA a ticket" }, + { name: "tools:fmt", scope: "user", description: undefined }, + { name: "unnamed:lint", scope: "user", description: undefined }, + ], + ); + + // Managed enabledPlugins entries lock their plugin name against inline copies. + const programFiles = path.join(workspace, "program-files"); + yield* fs.makeDirectory(path.join(programFiles, "ClaudeCode"), { recursive: true }); + yield* fs.writeFileString( + path.join(programFiles, "ClaudeCode", "managed-settings.json"), + '{"enabledPlugins":{"acme@shop":true}}', + ); + assert.deepEqual( + (yield* discoverClaudeSkills({ homePath: path.join(workspace, "empty") }, workspace, { + CLAUDE_CODE_PLUGIN_DIRS: [named, unnamed].join(";"), + ProgramFiles: programFiles, + })).map((skill) => skill.name), + ["unnamed:lint"], + ); + + assert.deepEqual( + (yield* discoverClaudeSkills({ homePath: path.join(workspace, "empty") }, workspace, { + CLAUDE_CODE_PLUGIN_DIRS: inherited, + })).map((skill) => skill.name), + ["inherited:stale"], + ); + }), + ); + it.effect("discovers user and project skills with frontmatter metadata", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; diff --git a/apps/server/src/provider/Drivers/ClaudeSkills.ts b/apps/server/src/provider/Drivers/ClaudeSkills.ts index a5df2671540f8..4fdd3565d11b6 100644 --- a/apps/server/src/provider/Drivers/ClaudeSkills.ts +++ b/apps/server/src/provider/Drivers/ClaudeSkills.ts @@ -129,6 +129,20 @@ function claudeManagedDirectory( return "/etc/claude-code"; } +function claudeManagedSettingsPaths( + path: Path.Path, + platform: NodeJS.Platform, + environment: NodeJS.ProcessEnv, +): ReadonlyArray { + const managedDirectory = claudeManagedDirectory(path, platform, environment); + const programData = + platform === "win32" ? windowsEnvironmentValue(environment, "ProgramData") : undefined; + return [ + ...(programData ? [path.join(programData, "ClaudeCode", "managed-settings.json")] : []), + ...(managedDirectory ? [path.join(managedDirectory, "managed-settings.json")] : []), + ]; +} + /** Claude Code settings files, lowest precedence first. */ function claudeSettingsPaths( path: Path.Path, @@ -138,9 +152,6 @@ function claudeSettingsPaths( environment: NodeJS.ProcessEnv, repositoryRoot?: string, ): ReadonlyArray { - const managedDirectory = claudeManagedDirectory(path, platform, environment); - const programData = - platform === "win32" ? windowsEnvironmentValue(environment, "ProgramData") : undefined; const root = repositoryRoot !== undefined && repositoryRoot !== cwd ? repositoryRoot : undefined; return [ path.join(configDirPath, "settings.json"), @@ -151,8 +162,7 @@ function claudeSettingsPaths( ] : []), ...(root ? [path.join(root, ".claude", "settings.local.json")] : []), - ...(programData ? [path.join(programData, "ClaudeCode", "managed-settings.json")] : []), - ...(managedDirectory ? [path.join(managedDirectory, "managed-settings.json")] : []), + ...claudeManagedSettingsPaths(path, platform, environment), ]; } @@ -261,6 +271,8 @@ const readSkillSettings = Effect.fn("readSkillSettings")(function* ( const platform = yield* HostProcess.Platform; const overridesByName = new Map(); const enabledPlugins = new Map(); + const managedPluginNames = new Set(); + const managedPaths = new Set(claudeManagedSettingsPaths(path, platform, environment)); let syncedPluginsHidden = false; const repositoryRoot = cwd === undefined ? undefined : yield* findRepositoryRoot(cwd); const projectSettingsPath = @@ -296,6 +308,9 @@ const readSkillSettings = Effect.fn("readSkillSettings")(function* ( for (const [key, enabled] of Object.entries(parsed?.enabledPlugins ?? {})) { if (typeof enabled === "boolean") { enabledPlugins.set(key, enabled); + if (managedPaths.has(settingsPath) && key.includes("@")) { + managedPluginNames.add(pluginNameKey(key.slice(0, key.indexOf("@")))); + } } else { // Extended formats replace lower-precedence values without enabling the plugin here. enabledPlugins.delete(key); @@ -307,7 +322,12 @@ const readSkillSettings = Effect.fn("readSkillSettings")(function* ( } } - return { skillOverrides: overridesByName, enabledPlugins, syncedPluginsHidden }; + return { + skillOverrides: overridesByName, + enabledPlugins, + managedPluginNames, + syncedPluginsHidden, + }; }); const InstalledPlugins = Schema.fromJsonString( @@ -324,6 +344,7 @@ const PluginManifest = Schema.fromJsonString( Schema.Struct({ name: Schema.optional(Schema.NonEmptyString), skills: Schema.optional(Schema.Union([Schema.String, Schema.Array(Schema.String)])), + defaultEnabled: Schema.optional(Schema.Unknown), }), ); @@ -444,10 +465,66 @@ const resolveSyncedPluginBucket = Effect.fn("resolveSyncedPluginBucket")(functio return `${organizationId.toLowerCase()}_${accountId.toLowerCase()}`; }); +/** A plugin folder itself, or else the manifest-bearing plugins directly inside it. */ +const expandInlinePluginDirectory = Effect.fn("expandInlinePluginDirectory")(function* ( + directory: string, +) { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const exists = (candidate: string) => + fileSystem.exists(candidate).pipe(Effect.orElseSucceed(() => false)); + for (const component of [path.join(".claude-plugin", "plugin.json"), "skills", "SKILL.md"]) { + if (yield* exists(path.join(directory, component))) return [directory]; + } + const children = yield* fileSystem + .readDirectory(directory) + .pipe(Effect.orElseSucceed((): ReadonlyArray => [])); + return yield* Effect.filter( + [...children].sort().map((child) => path.join(directory, child)), + (child) => exists(path.join(child, ".claude-plugin", "plugin.json")), + ); +}); + +/** Plugin folders Claude Code loads inline from CLAUDE_CODE_PLUGIN_DIRS, and their plugin names. */ +const discoverInlinePluginSkillRoots = Effect.fn("discoverInlinePluginSkillRoots")(function* ( + pluginDirectories: string | undefined, + enabledPlugins: ReadonlyMap, + managedPluginNames: ReadonlySet, +) { + const path = yield* Path.Path; + const home = yield* HostProcess.HomeDirectory; + const platform = yield* HostProcess.Platform; + const roots: Array = []; + const names = new Set(); + const seen = new Set(); + for (const entry of (pluginDirectories ?? "").split(platform === "win32" ? ";" : ":")) { + const directory = expandHomePath(entry.trim(), home); + // Claude skips relative entries rather than resolving them against its cwd. + if (!directory || !path.isAbsolute(directory)) continue; + for (const installPath of yield* expandInlinePluginDirectory(path.resolve(directory))) { + if (seen.has(installPath)) continue; + seen.add(installPath); + const manifest = yield* readPluginManifest(installPath); + const prefix = manifest?.name ?? path.basename(installPath); + // A `@inline` setting wins; otherwise the manifest's `defaultEnabled` decides. + const setting = enabledPlugins.get(`${prefix}@inline`); + if (setting === false || (setting === undefined && manifest?.defaultEnabled === false)) { + continue; + } + // Managed enabledPlugins entries, true or false, lock the name against inline copies. + if (managedPluginNames.has(pluginNameKey(prefix))) continue; + names.add(pluginNameKey(prefix)); + roots.push(...(yield* pluginSkillRoots(installPath, "user", prefix, manifest))); + } + } + return { roots, names }; +}); + const discoverPluginSkillRoots = Effect.fn("discoverPluginSkillRoots")(function* ( configDirPath: string, cwd: string | undefined, enabledPlugins: ReadonlyMap, + inlinePluginNames: ReadonlySet, syncedBucket: string | undefined, environment: NodeJS.ProcessEnv, ) { @@ -465,7 +542,8 @@ const discoverPluginSkillRoots = Effect.fn("discoverPluginSkillRoots")(function* Effect.orElseSucceed(() => undefined), ); const roots: Array = []; - const claimedPluginNames = new Set(); + // An enabled inline plugin replaces installed and synced plugins of the same name. + const claimedPluginNames = new Set(inlinePluginNames); // The CLI's process.cwd() resolves symlinks; registry projectPath values remain lexical. const workspace = cwd === undefined @@ -505,7 +583,7 @@ const discoverPluginSkillRoots = Effect.fn("discoverPluginSkillRoots")(function* const installPath = path.resolve(cwd ?? ".", install.installPath); const manifest = yield* readPluginManifest(installPath); const prefix = manifest?.name ?? key.split("@")[0]; - if (!prefix) continue; + if (!prefix || inlinePluginNames.has(pluginNameKey(prefix))) continue; // A registry record whose install is gone loads nothing and claims no namespace. if (Option.isSome(existing)) claimedPluginNames.add(pluginNameKey(prefix)); roots.push(...(yield* pluginSkillRoots(installPath, scope, prefix, manifest))); @@ -571,6 +649,7 @@ const resolveClaudeConfigDirPath = Effect.fn("resolveClaudeConfigDirPath")(funct const SettingsEnv = Schema.Struct({ ANTHROPIC_BASE_URL: Schema.optional(Schema.String), CLAUDE_CODE_ENTRYPOINT: Schema.optional(Schema.String), + CLAUDE_CODE_PLUGIN_DIRS: Schema.optional(Schema.String), // Unquoted JSON values must not fail the decode and drop ANTHROPIC_BASE_URL with them. CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY: Schema.optional(Schema.Unknown), CLAUDE_CODE_GATEWAY_MODEL_DISCOVERY_TIMEOUT_MS: Schema.optional(Schema.Unknown), @@ -648,11 +727,8 @@ export const discoverClaudeSkills = Effect.fn("discoverClaudeSkills")(function* const path = yield* Path.Path; const env = environment ?? process.env; const configDirPath = yield* resolveClaudeConfigDirPath(config, env, cwd); - const { skillOverrides, enabledPlugins, syncedPluginsHidden } = yield* readSkillSettings( - configDirPath, - cwd, - env, - ); + const { skillOverrides, enabledPlugins, managedPluginNames, syncedPluginsHidden } = + yield* readSkillSettings(configDirPath, cwd, env); const managedDirectory = claudeManagedDirectory(path, yield* HostProcess.Platform, env); // Most installs have no synced folder; skip reading `.claude.json` for them. const hasSyncedPlugins = @@ -663,6 +739,15 @@ export const discoverClaudeSkills = Effect.fn("discoverClaudeSkills")(function* const syncedBucket = hasSyncedPlugins ? yield* resolveSyncedPluginBucket(config, configDirPath, env) : undefined; + // Claude applies its settings `env` over the inherited environment. + const inlinePluginDirectories = + (yield* readClaudeSettingsEnv(config, cwd, env)).CLAUDE_CODE_PLUGIN_DIRS ?? + env.CLAUDE_CODE_PLUGIN_DIRS; + const inlinePlugins = yield* discoverInlinePluginSkillRoots( + inlinePluginDirectories, + enabledPlugins, + managedPluginNames, + ); const roots: ReadonlyArray = [ ...(managedDirectory @@ -676,7 +761,15 @@ export const discoverClaudeSkills = Effect.fn("discoverClaudeSkills")(function* directory: path.join(path.resolve(cwd ?? ".", directory), ".claude", "skills"), scope: "project" as const, })), - ...(yield* discoverPluginSkillRoots(configDirPath, cwd, enabledPlugins, syncedBucket, env)), + ...inlinePlugins.roots, + ...(yield* discoverPluginSkillRoots( + configDirPath, + cwd, + enabledPlugins, + inlinePlugins.names, + syncedBucket, + env, + )), ]; const skillsByName = new Map(); diff --git a/apps/server/src/provider/Drivers/CodexDriver.ts b/apps/server/src/provider/Drivers/CodexDriver.ts index f8a753169b54a..03245ba46d6c1 100644 --- a/apps/server/src/provider/Drivers/CodexDriver.ts +++ b/apps/server/src/provider/Drivers/CodexDriver.ts @@ -29,7 +29,7 @@ import * as ResetCreditCoordinator from "../resetCreditCoordinator.ts"; import { checkCodexProviderStatus, makePendingCodexProvider, - probeCodexSkillsForCwd, + makeCodexSkillsProbe, withCodexAppServerClient, } from "../CodexProvider.ts"; import { resolveCodexLaunchArgs } from "../codexLaunchArgs.ts"; @@ -321,22 +321,21 @@ export const CodexDriver: ProviderDriver< processEnv, snapshot.getSnapshot.pipe(Effect.map((value) => value.models)), ); + const probeSkills = yield* makeCodexSkillsProbe().pipe( + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), + ); const snapshotForCwd = (cwd: string) => !effectiveConfig.enabled ? snapshot.getSnapshot : Effect.all([ snapshot.getSnapshot, - probeCodexSkillsForCwd({ + probeSkills({ binaryPath: effectiveConfig.binaryPath, homePath: effectiveConfig.homePath, launchArgs: resolveCodexLaunchArgs(effectiveConfig.launchArgs, processEnv), cwd, environment: processEnv, - }).pipe( - Effect.scoped, - Effect.timeout(resolveProviderProbeTimeoutMs("codex", 20_000, processEnv)), - Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), - ), + }).pipe(Effect.timeout(resolveProviderProbeTimeoutMs("codex", 20_000, processEnv))), ]).pipe( Effect.map(([machineSnapshot, skills]) => ({ ...machineSnapshot, skills })), Effect.mapError( diff --git a/apps/server/src/provider/Drivers/antigravityUsageReader.ts b/apps/server/src/provider/Drivers/antigravityUsageReader.ts index 4d473f50c9d67..532233f7facce 100644 --- a/apps/server/src/provider/Drivers/antigravityUsageReader.ts +++ b/apps/server/src/provider/Drivers/antigravityUsageReader.ts @@ -129,18 +129,19 @@ const MODEL_IDS: Record = { }; // Effort and routing variants share the base model's price. -function withRateModel(record: UsageRecord): UsageRecord { - const base = record.model.replace( - /-(?:extra-low|low|medium|high|tiered|control|thinking|agent)$/, - "", - ); - const rateModel = /^gemini-3-flash-(?:agent(?:-[ab])?|[ab])$/.test(record.model) +export const antigravityRateModel = (model: string) => { + const base = model.replace(/-(?:extra-low|low|medium|high|tiered|control|thinking|agent)$/, ""); + return /^gemini-3-flash-(?:agent(?:-[ab])?|[ab])$/.test(model) ? "gemini-3.5-flash" - : record.model === "gemini-3.7-flash-safety-le" + : model === "gemini-3.7-flash-safety-le" ? "gemini-3.7-flash" : base === "gemini-3-flash" ? "gemini-3-flash-preview" : base; +}; + +function withRateModel(record: UsageRecord): UsageRecord { + const rateModel = antigravityRateModel(record.model); return rateModel === record.model ? record : { ...record, rateModel }; } diff --git a/apps/server/src/provider/EventNdjsonLogger.test.ts b/apps/server/src/provider/EventNdjsonLogger.test.ts index eed20381b46cb..0bb55b9bcb39b 100644 --- a/apps/server/src/provider/EventNdjsonLogger.test.ts +++ b/apps/server/src/provider/EventNdjsonLogger.test.ts @@ -255,6 +255,65 @@ it.layer(NodeServices.layer)("EventNdjsonLogger", (it) => { }), ); + it.effect("drops Cursor streaming deltas while keeping its lifecycle updates", () => + Effect.gen(function* () { + const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-provider-log-")); + const basePath = NodePath.join(tempDir, "events.log"); + const threadId = ThreadId.make("cursor-deltas"); + const cursorUpdate = (update: object) => ({ + provider: "cursor", + kind: "protocol", + event: { + direction: "incoming", + stage: "decoded", + payload: { type: "interaction.update", runId: "run-1", update }, + }, + }); + const toolCall = { type: "shell", args: { command: "ls" } }; + const transient = [ + { type: "text-delta", text: "Hel" }, + { type: "thinking-delta", text: "Hmm" }, + { type: "token-delta", tokens: 3 }, + { type: "shell-output-delta", event: { stdout: "a.ts\n" } }, + { type: "partial-tool-call", callId: "call-1", modelCallId: "m-1", toolCall }, + { + type: "tool-call-delta", + callId: "call-1", + modelCallId: "m-1", + taskUpdate: { type: "text-delta", text: "Sub" }, + }, + ].map(cursorUpdate); + const retained = [ + { type: "tool-call-started", callId: "call-1", modelCallId: "m-1", toolCall }, + { + type: "tool-call-delta", + callId: "call-1", + modelCallId: "m-1", + taskUpdate: { type: "tool-call-completed", callId: "call-2", toolCall }, + }, + { type: "tool-call-completed", callId: "call-1", modelCallId: "m-1", toolCall }, + { type: "turn-ended" }, + ].map(cursorUpdate); + + try { + const store = yield* makeEventNdjsonLogStore(basePath, { batchWindowMs: 0 }); + const native = store.logger("native"); + for (const event of [...transient, ...retained]) { + yield* native.write(event, threadId); + } + yield* store.close(); + + const records = NodeFS.readFileSync(ownedLogPath(basePath, "cursor-deltas"), "utf8") + .trim() + .split("\n") + .map((line) => decodeUnknownJson(parseLogLine(line).payload)); + assert.deepEqual(records, retained); + } finally { + NodeFS.rmSync(tempDir, { recursive: true, force: true }); + } + }), + ); + it.effect("bounds oversized records while retaining failure details", () => Effect.gen(function* () { const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-provider-log-")); diff --git a/apps/server/src/provider/EventNdjsonLogger.ts b/apps/server/src/provider/EventNdjsonLogger.ts index 6a1d3844b2373..5a7b3578578f1 100644 --- a/apps/server/src/provider/EventNdjsonLogger.ts +++ b/apps/server/src/provider/EventNdjsonLogger.ts @@ -63,6 +63,13 @@ const transientNativeMethods = new Set([ "turn/diff/updated", ]); const transientAcpUpdates = new Set(["agent_message_chunk", "agent_thought_chunk"]); +const transientCursorUpdates = new Set([ + "text-delta", + "thinking-delta", + "token-delta", + "shell-output-delta", + "partial-tool-call", +]); export type EventNdjsonStream = "native" | "canonical" | "orchestration"; @@ -219,6 +226,18 @@ export function shouldPersistProviderEvent(stream: EventNdjsonStream, event: unk } } + if (nativeType === "interaction.update") { + let update = Reflect.get(nativeEvent, "update"); + if (typeof update !== "object" || update === null) return true; + // Subagent updates arrive wrapped; keep the wrapper when it carries a lifecycle record. + if (Reflect.get(update, "type") === "tool-call-delta") { + update = Reflect.get(update, "taskUpdate"); + if (typeof update !== "object" || update === null) return true; + } + const updateType = Reflect.get(update, "type"); + return typeof updateType !== "string" || !transientCursorUpdates.has(updateType); + } + const payload = Reflect.get(nativeEvent, "payload"); if (typeof payload !== "object" || payload === null) return true; diff --git a/apps/server/src/provider/ProviderRegistry.test.ts b/apps/server/src/provider/ProviderRegistry.test.ts index 564a90f40e6ce..c42d78b237d42 100644 --- a/apps/server/src/provider/ProviderRegistry.test.ts +++ b/apps/server/src/provider/ProviderRegistry.test.ts @@ -1309,7 +1309,7 @@ it.layer( ); }); - it("retains stale OpenCode models when a refresh fails", () => { + it("retains stale OpenCode models when a refresh fails or lists none", () => { const previousProvider = { instanceId: ProviderInstanceId.make("opencode"), driver: ProviderDriverKind.make("opencode"), @@ -1359,6 +1359,21 @@ it.layer( ProviderRegistry.mergeProviderSnapshot(previousProvider, refreshedProvider).skills, previousProvider.skills, ); + + const emptyCatalogProvider = { + ...refreshedProvider, + status: "warning", + message: "OpenCode 2 is running, but it did not list any models yet.", + slashCommands: [], + skills: [], + } satisfies ServerProvider; + const mergedEmptyCatalog = ProviderRegistry.mergeProviderSnapshot( + previousProvider, + emptyCatalogProvider, + ); + assert.deepStrictEqual(mergedEmptyCatalog.models, [...previousProvider.models]); + assert.deepStrictEqual(mergedEmptyCatalog.slashCommands, previousProvider.slashCommands); + assert.deepStrictEqual(mergedEmptyCatalog.skills, previousProvider.skills); }); describe("Pi model inventories", () => { @@ -4261,6 +4276,7 @@ it.layer( ); assert.strictEqual(status.status, "ready"); assert.strictEqual(status.auth.status, "authenticated"); + assert.strictEqual(status.auth.label, "Claude API Key"); }).pipe( Effect.provide( layerMockSpawner((args) => { diff --git a/apps/server/src/provider/ProviderRegistry.ts b/apps/server/src/provider/ProviderRegistry.ts index ba9ce9b2b6af7..8a7ff0ac7fd37 100644 --- a/apps/server/src/provider/ProviderRegistry.ts +++ b/apps/server/src/provider/ProviderRegistry.ts @@ -220,7 +220,8 @@ const shouldRetainMissingProviderModels = (provider: ServerProvider): boolean => const isAntigravity = provider.driver === ProviderDriverKind.make("antigravity"); const isCodex = provider.driver === ProviderDriverKind.make("codex"); - if (!isAntigravity && !isCodex && provider.driver !== ProviderDriverKind.make("opencode")) { + const isOpenCode = provider.driver === ProviderDriverKind.make("opencode"); + if (!isAntigravity && !isCodex && !isOpenCode) { return true; } @@ -236,9 +237,10 @@ const shouldRetainMissingProviderModels = (provider: ServerProvider): boolean => isAntigravity && provider.status === "warning" && provider.auth.status === "unknown"; const isPendingInitialProbe = provider.enabled && !provider.installed && provider.status === "warning"; + // A cold OpenCode server can finish its probe as a warning before it lists any models. const isIncompleteInstalledProbe = provider.installed && - (provider.status === "error" || (isCodex && provider.status === "warning")); + (provider.status === "error" || ((isCodex || isOpenCode) && provider.status === "warning")); return isPendingAntigravityAuthentication || isPendingInitialProbe || isIncompleteInstalledProbe; }; diff --git a/apps/server/src/provider/acp/AcpJsonRpcConnection.test.ts b/apps/server/src/provider/acp/AcpJsonRpcConnection.test.ts index f06c4f47570dd..5dacd1f2d9267 100644 --- a/apps/server/src/provider/acp/AcpJsonRpcConnection.test.ts +++ b/apps/server/src/provider/acp/AcpJsonRpcConnection.test.ts @@ -15,13 +15,16 @@ import * as Exit from "effect/Exit"; import * as Fiber from "effect/Fiber"; import * as Scope from "effect/Scope"; import * as Result from "effect/Result"; +import * as Scheduler from "effect/Scheduler"; import * as Stream from "effect/Stream"; +import * as TestClock from "effect/testing/TestClock"; import { ChildProcessSpawner } from "effect/process"; import { describe, expect } from "vite-plus/test"; import * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime"; import { AgentScope } from "@t3tools/shared/AgentScope"; import * as EffectAcpErrors from "effect-acp/errors"; +import type * as EffectAcpProtocol from "effect-acp/protocol"; const __dirname = NodePath.dirname(NodeURL.fileURLToPath(import.meta.url)); const mockAgentPath = NodePath.join(__dirname, "../../../scripts/acp-mock-agent.ts"); @@ -44,6 +47,136 @@ const setProcessEnv = (name: string, value: string) => { }; }; +const isPromptStarted = (event: AcpSessionRuntime.AcpSessionRequestLogEvent) => + event.method === "session/prompt" && event.status === "started"; + +const isOutgoing = (tag: string) => (event: EffectAcpProtocol.AcpProtocolLogEvent) => + event.direction === "outgoing" && + event.stage === "decoded" && + typeof event.payload === "object" && + event.payload !== null && + Reflect.get(event.payload, "tag") === tag; + +// The methods the mock agent read, in order, keeping only the given ones. +const readReceived = (requestLogPath: string, methods: ReadonlyArray) => + NodeFS.readFileSync(requestLogPath, "utf8") + .split("\n") + .filter((line) => line.length > 0) + .map((line) => (JSON.parse(line) as { readonly method?: string }).method) + .filter((method) => method !== undefined && methods.includes(method)); + +// Prompts, then cancels while the prompt is held on its way out: in its started request record, +// in its outgoing protocol log record, or by its fiber yielding every few operations. The cancel +// comes on dispatch or from inside the held record. Returns the prompt's exit +// and the order in which the agent read the prompt, the cancel and the request that ends it. +const promptThenCancel = (input: { + readonly cancelBehavior: "interrupt" | "wait-for-prompt"; + readonly holdPrompt: { + readonly in: "request-log" | "protocol-log" | "scheduler"; + readonly cancelOn: "dispatch" | "log"; + }; +}) => + Effect.gen(function* () { + const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "acp-runtime-")); + yield* Effect.addFinalizer(() => + Effect.sync(() => NodeFS.rmSync(tempDir, { recursive: true, force: true })), + ); + const requestLogPath = NodePath.join(tempDir, "requests.ndjson"); + const { holdPrompt } = input; + const promptHeld = yield* Deferred.make(); + const promptReleased = yield* Deferred.make(); + yield* Effect.addFinalizer(() => Deferred.succeed(promptReleased, undefined)); + const testScope = yield* Scope.Scope; + const runtimeMade = yield* Deferred.make(); + const cancelledFromLog = yield* Deferred.make>(); + const hold = ( + holdPrompt.cancelOn === "log" + ? Deferred.await(runtimeMade).pipe( + Effect.flatMap((made) => + made.cancel.pipe(Effect.forkIn(testScope, { startImmediately: true })), + ), + Effect.flatMap((fiber) => Deferred.succeed(cancelledFromLog, fiber)), + ) + : Effect.void + ).pipe( + Effect.andThen(Deferred.succeed(promptHeld, undefined)), + Effect.andThen(Deferred.await(promptReleased)), + ); + const runtime = yield* AcpSessionRuntime.make({ + ...mockRuntimeOptions, + spawn: { + ...mockRuntimeOptions.spawn, + env: { + T3_ACP_COMPLETE_FIRST_PROMPT_ON_CANCEL: "1", + T3_ACP_REQUEST_LOG_PATH: requestLogPath, + }, + }, + cancelBehavior: input.cancelBehavior, + interruptPromptOnCancel: false, + protocolLogging: { + logOutgoing: holdPrompt.in === "protocol-log", + logger: (event) => (isOutgoing("session/prompt")(event) ? hold : Effect.void), + }, + requestLogger: (event) => + holdPrompt.in === "request-log" && isPromptStarted(event) ? hold : Effect.void, + }); + yield* Deferred.succeed(runtimeMade, runtime); + const cancelReceived = yield* Deferred.make(); + yield* runtime.getEvents().pipe( + Stream.runForEach((event) => { + if (event._tag === "EventStreamBarrier") { + return Deferred.succeed(event.acknowledge, undefined); + } + if (event._tag === "ThoughtDelta" && event.text === "native-cancel-received") { + return Deferred.succeed(cancelReceived, undefined); + } + return Effect.void; + }), + Effect.forkChild, + ); + yield* runtime.start(); + const dispatched = yield* Deferred.make(); + const prompt = yield* runtime + .prompt({ prompt: [{ type: "text", text: "first" }] }, { dispatched }) + .pipe( + holdPrompt.in === "scheduler" + ? Effect.provideService(Scheduler.MaxOpsBeforeYield, 3) + : (effect) => effect, + Effect.forkChild, + ); + // A round trip: whatever was queued before it reaches the agent first. + const roundTrip = runtime.request("_test/environment", {}); + let cancel: Fiber.Fiber; + if (holdPrompt.in === "scheduler") { + yield* Deferred.await(dispatched); + cancel = yield* runtime.cancel.pipe(Effect.forkChild({ startImmediately: true })); + } else { + yield* Deferred.await(promptHeld); + if (holdPrompt.cancelOn === "dispatch") { + cancel = yield* Deferred.await(dispatched).pipe( + Effect.andThen(runtime.cancel), + Effect.forkChild({ startImmediately: true }), + ); + } else { + cancel = yield* Deferred.await(cancelledFromLog); + } + yield* roundTrip; + yield* Deferred.succeed(promptReleased, undefined); + } + yield* Deferred.await(cancelReceived); + yield* runtime.request("_test/finish-cancel", {}); + // The agent answers it only after reading every earlier line. + yield* roundTrip; + const received = readReceived(requestLogPath, [ + "session/prompt", + "session/cancel", + "_test/finish-cancel", + ]); + const promptExit = yield* Fiber.await(prompt); + yield* Fiber.join(cancel); + return { received, promptExit }; + }).pipe(Effect.scoped); + describe("AcpSessionRuntime", () => { it.effect.each(["session/new", "session/resume"] as const)( "buffers root metadata while %s startup is still pending", @@ -1153,6 +1286,72 @@ describe("AcpSessionRuntime", () => { ), ); + // regression: #37106 prompt sent before session/load replied + it.live("does not prompt before session/load replies after replay goes idle", () => + Effect.gen(function* () { + const runtime = yield* AcpSessionRuntime.AcpSessionRuntime; + yield* runtime.start(); + + const result = yield* runtime.prompt({ + prompt: [{ type: "text", text: "hi" }], + }); + expect(result.stopReason).toBe("end_turn"); + }).pipe( + Effect.provide( + AcpSessionRuntime.layer({ + authMethodId: "test", + spawn: { + command: mockAgentCommand, + args: mockAgentArgs, + env: { + T3_ACP_EMIT_LOAD_REPLAY: "1", + T3_ACP_LOAD_REPLY_DELAY_MS: "500", + T3_ACP_REJECT_PROMPT_UNTIL_LOADED: "1", + }, + }, + cwd: process.cwd(), + resumeSessionId: "mock-session-1", + sessionLoadReplayIdleGap: "50 millis", + clientInfo: { name: "t3-test", version: "0.0.0" }, + }), + ), + Effect.scoped, + Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), + ), + ); + + it.live("treats replay idle as loaded when the agent opts in", () => + Effect.gen(function* () { + const runtime = yield* AcpSessionRuntime.AcpSessionRuntime; + const started = yield* runtime.start(); + + expect(started.sessionSetupResult._meta).toMatchObject({ + t3SessionLoadReady: "replay_idle", + }); + }).pipe( + Effect.provide( + AcpSessionRuntime.layer({ + authMethodId: "test", + spawn: { + command: mockAgentCommand, + args: mockAgentArgs, + env: { + T3_ACP_EMIT_LOAD_REPLAY: "1", + T3_ACP_LOAD_NEVER_REPLIES: "1", + }, + }, + cwd: process.cwd(), + resumeSessionId: "mock-session-1", + sessionLoadReplayIdleGap: "50 millis", + sessionLoadReplayIdleReadiness: true, + clientInfo: { name: "t3-test", version: "0.0.0" }, + }), + ), + Effect.scoped, + Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), + ), + ); + it.effect("rejects invalid config option values before sending session/set_config_option", () => { const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "acp-runtime-")); const requestLogPath = NodePath.join(tempDir, "requests.ndjson"); @@ -1239,4 +1438,104 @@ describe("AcpSessionRuntime", () => { Effect.ensuring(Effect.sync(() => NodeFS.rmSync(tempDir, { recursive: true, force: true }))), ); }); + + it.effect.each( + (["interrupt", "wait-for-prompt"] as const).flatMap((cancelBehavior) => + ( + [ + { in: "request-log", cancelOn: "dispatch" }, + { in: "protocol-log", cancelOn: "log" }, + { in: "scheduler", cancelOn: "dispatch" }, + ] as const + ).map((holdPrompt) => ({ cancelBehavior, holdPrompt })), + ), + )( + "sends session/prompt before a cancel issued while the prompt is held in the $holdPrompt.in ($cancelBehavior, cancel on $holdPrompt.cancelOn)", + ({ cancelBehavior, holdPrompt }) => + Effect.gen(function* () { + const run = yield* promptThenCancel({ cancelBehavior, holdPrompt }); + expect(run.received).toEqual(["session/prompt", "session/cancel", "_test/finish-cancel"]); + expect(run.promptExit).toEqual( + Exit.succeed({ stopReason: "cancelled", _meta: { nativeCancel: true } }), + ); + }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), + ); + + it.effect("sends a cancel after the cancel timeout when its prompt never goes out", () => + Effect.gen(function* () { + const runtime = yield* AcpSessionRuntime.make({ + ...mockRuntimeOptions, + interruptPromptOnCancel: false, + cancelTimeout: "5 seconds", + requestLogger: (event) => (isPromptStarted(event) ? Effect.never : Effect.void), + }); + yield* runtime.start(); + const dispatched = yield* Deferred.make(); + yield* runtime + .prompt({ prompt: [{ type: "text", text: "first" }] }, { dispatched }) + .pipe(Effect.forkChild); + yield* Deferred.await(dispatched); + const cancel = yield* runtime.cancel.pipe(Effect.forkChild({ startImmediately: true })); + yield* TestClock.adjust("5 seconds"); + yield* Fiber.join(cancel); + }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), + ); + + it.effect( + "sends a cancel before the next prompt when the prompt it waited for never went out", + () => + Effect.gen(function* () { + const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "acp-runtime-")); + yield* Effect.addFinalizer(() => + Effect.sync(() => NodeFS.rmSync(tempDir, { recursive: true, force: true })), + ); + const requestLogPath = NodePath.join(tempDir, "requests.ndjson"); + let promptsStarted = 0; + const runtime = yield* AcpSessionRuntime.make({ + ...mockRuntimeOptions, + spawn: { ...mockRuntimeOptions.spawn, env: { T3_ACP_REQUEST_LOG_PATH: requestLogPath } }, + interruptPromptOnCancel: false, + // The cancel spends a test-clock second in its outgoing record, the window in which the + // next prompt could overtake it. + protocolLogging: { + logOutgoing: true, + logger: (event) => + isOutgoing("session/cancel")(event) ? Effect.sleep("1 second") : Effect.void, + }, + requestLogger: (event) => + isPromptStarted(event) && promptsStarted++ === 0 ? Effect.never : Effect.void, + }); + yield* runtime.getEvents().pipe( + Stream.runForEach((event) => + event._tag === "EventStreamBarrier" + ? Deferred.succeed(event.acknowledge, undefined) + : Effect.void, + ), + Effect.forkChild, + ); + yield* runtime.start(); + const dispatched = yield* Deferred.make(); + const first = yield* runtime + .prompt({ prompt: [{ type: "text", text: "first" }] }, { dispatched }) + .pipe(Effect.forkChild); + yield* Deferred.await(dispatched); + const cancel = yield* runtime.cancel.pipe(Effect.forkChild({ startImmediately: true })); + const next = yield* runtime + .prompt({ prompt: [{ type: "text", text: "next" }] }) + .pipe(Effect.forkChild); + // The first prompt ends before it is queued, which releases the cancel's wait. + yield* Effect.sleep("1 second").pipe( + Effect.andThen(Fiber.interrupt(first)), + Effect.forkChild, + ); + yield* TestClock.adjust("1 second"); + yield* TestClock.adjust("1 second"); + yield* Fiber.join(cancel); + yield* Fiber.join(next); + expect(readReceived(requestLogPath, ["session/prompt", "session/cancel"])).toEqual([ + "session/cancel", + "session/prompt", + ]); + }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), + ); }); diff --git a/apps/server/src/provider/acp/AcpSessionRuntime.processTree.test.ts b/apps/server/src/provider/acp/AcpSessionRuntime.processTree.test.ts index 0c6bddbbf8567..6b9afa7f30c50 100644 --- a/apps/server/src/provider/acp/AcpSessionRuntime.processTree.test.ts +++ b/apps/server/src/provider/acp/AcpSessionRuntime.processTree.test.ts @@ -1,6 +1,7 @@ // @effect-diagnostics nodeBuiltinImport:off import * as NodeChildProcess from "node:child_process"; import * as NodeFS from "node:fs"; +import * as NodeModule from "node:module"; import * as NodePath from "node:path"; import { describe, expect, it, vi } from "@effect/vitest"; @@ -813,6 +814,33 @@ describe("terminatePosixOwnedProcessTree", () => { }), ); + it.skipIf(HostProcess.Platform.defaultValue() !== "linux")( + "snapshots /proc when a process exits while its entry is being typed", + () => { + const fs = NodeModule.createRequire(import.meta.url)("node:fs") as typeof NodeFS; + const realReaddirSync = fs.readdirSync; + // Node lstat()s a dirent of unknown type; a process that exits meanwhile makes it throw. + fs.readdirSync = ((path: NodeFS.PathLike, options?: unknown) => { + if (path === "/proc" && typeof options === "object" && options !== null) { + throw Object.assign(new Error("ENOENT: no such file or directory, lstat '/proc/4563'"), { + code: "ENOENT", + syscall: "lstat", + path: "/proc/4563", + }); + } + return Reflect.apply(realReaddirSync, fs, [path, options]); + }) as typeof NodeFS.readdirSync; + NodeModule.syncBuiltinESMExports(); + try { + const snapshot = AcpSessionRuntime.makePosixProcessTreeController("linux").snapshot(); + expect(snapshot.some((entry) => entry.pid === process.pid)).toBe(true); + } finally { + fs.readdirSync = realReaddirSync; + NodeModule.syncBuiltinESMExports(); + } + }, + ); + it("fails closed where a stable POSIX identity provider is unavailable", () => { try { AcpSessionRuntime.makePosixProcessTreeController("darwin"); diff --git a/apps/server/src/provider/agentContextBudget.json b/apps/server/src/provider/agentContextBudget.json index c83ec4d169aad..a03f9aea77034 100644 --- a/apps/server/src/provider/agentContextBudget.json +++ b/apps/server/src/provider/agentContextBudget.json @@ -1,5 +1,5 @@ { "claudeSystemAppendBytes": 5930, "codexAdditionalContextBytes": 7324, - "mcpToolListJsonBytes": 50569 + "mcpToolListJsonBytes": 50515 } diff --git a/apps/server/src/provider/codexLaunchArgs.test.ts b/apps/server/src/provider/codexLaunchArgs.test.ts index 8f4e1b7ca6d6a..e74b29d44818c 100644 --- a/apps/server/src/provider/codexLaunchArgs.test.ts +++ b/apps/server/src/provider/codexLaunchArgs.test.ts @@ -38,6 +38,15 @@ describe("codexExecLaunchArgs", () => { ); }); + it("drops launch args that enable user hooks", () => { + NodeAssert.deepStrictEqual( + codexExecLaunchArgs( + "--enable hooks --enable=hooks --enable codex_hooks --enable=codex_hooks --enable foo --disable hooks", + ), + ["--enable", "foo", "--disable", "hooks"], + ); + }); + it("does not pair value-taking flags with adjacent flags", () => { NodeAssert.deepStrictEqual(codexExecLaunchArgs("--config --strict-config --enable --disable"), [ "--strict-config", diff --git a/apps/server/src/provider/codexLaunchArgs.ts b/apps/server/src/provider/codexLaunchArgs.ts index 31ff37e31deb5..e49314ee83e0d 100644 --- a/apps/server/src/provider/codexLaunchArgs.ts +++ b/apps/server/src/provider/codexLaunchArgs.ts @@ -62,8 +62,17 @@ const filterLaunchArgs = (launchArgs: string | undefined, allowed: LaunchArgFilt return kept; }; -export const codexExecLaunchArgs = (launchArgs?: string) => - filterLaunchArgs(launchArgs, EXEC_FILTER); +const HOOK_FEATURES = new Set(["hooks", "codex_hooks"]); + +// Feature toggles beat config overrides, so a launch arg's hook enable would undo ours. +export const codexExecLaunchArgs = (launchArgs?: string) => { + const args = filterLaunchArgs(launchArgs, EXEC_FILTER); + return args.filter((arg, index) => { + if (arg.startsWith("--enable=")) return !HOOK_FEATURES.has(arg.slice("--enable=".length)); + if (arg === "--enable") return !HOOK_FEATURES.has(args[index + 1] ?? ""); + return !(args[index - 1] === "--enable" && HOOK_FEATURES.has(arg)); + }); +}; export const codexMcpListLaunchArgs = (launchArgs?: string) => filterLaunchArgs(launchArgs, MCP_LIST_FILTER); diff --git a/apps/server/src/provider/codexUsageLimits.test.ts b/apps/server/src/provider/codexUsageLimits.test.ts index b560aba5d3c28..3a5cae4a9d2c1 100644 --- a/apps/server/src/provider/codexUsageLimits.test.ts +++ b/apps/server/src/provider/codexUsageLimits.test.ts @@ -1,12 +1,15 @@ +import * as DateTime from "effect/DateTime"; import { ProviderDriverKind, ProviderInstanceId } from "@t3tools/contracts"; import { exhaustedUsageWindow } from "@t3tools/shared/usageLimits"; import * as CodexErrors from "effect-codex-app-server/errors"; import { describe, expect, it } from "vite-plus/test"; import { + codexMessageResetAt, codexRateLimitsFailureMessage, codexRateLimitsToLimits, codexRateLimitsToUpdate, + codexTimeZone, mergeCodexRateLimits, } from "./codexUsageLimits.ts"; @@ -313,3 +316,61 @@ describe("codexUsageLimitResetAt", () => { ).toBeNull(); }); }); + +describe("codexMessageResetAt", () => { + const dhaka = DateTime.zoneMakeNamedUnsafe("Asia/Dhaka"); + const at = (iso: string) => DateTime.makeUnsafe(iso); + const failedAt = at("2026-11-07T03:00:00Z"); // 09:00 in Dhaka + + // Codex prints local time without seconds, so the parsed reset is the minute after. + it("reads Codex's same-day and dated reset text in Codex's zone", () => { + expect( + codexMessageResetAt("You’ve hit your usage limit. Try again at 3:37 PM.", failedAt, dhaka), + ).toBe("2026-11-07T09:38:00.000Z"); + expect( + codexMessageResetAt("Upgrade, or try again at Dec 1st, 2026 12:05 AM.", failedAt, dhaka), + ).toBe("2026-11-30T18:06:00.000Z"); + expect(codexMessageResetAt("Try again at 12:30 PM.", failedAt, dhaka)).toBe( + "2026-11-07T06:31:00.000Z", + ); + }); + + it("dates today's form by when Codex failed, across local midnight", () => { + expect(codexMessageResetAt("Try again at 11:59 PM.", at("2026-11-07T17:59:59Z"), dhaka)).toBe( + "2026-11-07T18:00:00.000Z", + ); + }); + + it("takes the later instant of a repeated DST hour", () => { + // 1:30 AM occurs twice in New York on Nov 1st, 2026: 05:30Z (EDT) and 06:30Z (EST). + const newYork = DateTime.zoneMakeNamedUnsafe("America/New_York"); + for (const iso of ["2026-11-01T05:15:00Z", "2026-11-01T06:15:00Z"]) { + expect(codexMessageResetAt("Try again at 1:30 AM.", at(iso), newYork)).toBe( + "2026-11-01T06:31:00.000Z", + ); + } + }); + + it("leaves the reset unknown without reset text or once it has passed", () => { + expect( + codexMessageResetAt("You’ve hit your usage limit. Try again later.", failedAt, dhaka), + ).toBeNull(); + expect( + codexMessageResetAt( + "exceeded retry limit, last status: 429 Too Many Requests", + failedAt, + dhaka, + ), + ).toBeNull(); + expect(codexMessageResetAt("Try again at 8:15 AM.", failedAt, dhaka)).toBeNull(); + }); +}); + +describe("codexTimeZone", () => { + it("uses Codex's TZ and falls back to the server's zone", () => { + const local = DateTime.zoneToString(DateTime.zoneMakeLocal()); + expect(DateTime.zoneToString(codexTimeZone({ TZ: ":Asia/Dhaka" }))).toBe("Asia/Dhaka"); + expect(DateTime.zoneToString(codexTimeZone({ TZ: "not/a-zone" }))).toBe(local); + expect(DateTime.zoneToString(codexTimeZone({}))).toBe(local); + }); +}); diff --git a/apps/server/src/provider/codexUsageLimits.ts b/apps/server/src/provider/codexUsageLimits.ts index d4f9c621119df..526852332033d 100644 --- a/apps/server/src/provider/codexUsageLimits.ts +++ b/apps/server/src/provider/codexUsageLimits.ts @@ -201,3 +201,51 @@ export function codexUsageLimitResetAt( null, ); } + +const CODEX_RESET_TEXT = + /[Tt]ry again at (?:([A-Z][a-z]{2}) (\d{1,2})(?:st|nd|rd|th), (\d{4}) )?(\d{1,2}):(\d{2}) ([AP]M)\./; +const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; + +/** The zone Codex prints reset times in: its own `TZ`, else the server's. */ +export function codexTimeZone( + environment: Readonly>, +): DateTime.TimeZone { + return Option.getOrElse( + DateTime.zoneMakeNamed(environment.TZ?.replace(/^:/, "") ?? ""), + DateTime.zoneMakeLocal, + ); +} + +/** + * Codex writes a usage limit's reset into its error text in its own local time: + * "Try again at 3:37 PM." for today, else "try again at Nov 11th, 2026 3:37 PM.". + * It is the only reset left when a proxy such as CLIProxyAPI strips the rate-limit snapshot. + * `failedAt` is when Codex reported the failure, which fixes the date of today's form. + */ +export function codexMessageResetAt( + message: string, + failedAt: DateTime.DateTime, + zone: DateTime.TimeZone, +): string | null { + const match = CODEX_RESET_TEXT.exec(message); + if (!match) return null; + const [, month, day, year, hour, minute, meridiem] = match; + const monthIndex = month === undefined ? undefined : MONTHS.indexOf(month); + if (monthIndex === -1) return null; + const today = DateTime.toParts(DateTime.setZone(failedAt, zone)); + const reset = DateTime.makeZoned( + { + year: monthIndex === undefined ? today.year : Number(year), + month: monthIndex === undefined ? today.month : monthIndex + 1, + day: monthIndex === undefined ? today.day : Number(day), + hour: (Number(hour) % 12) + (meridiem === "PM" ? 12 : 0), + minute: Number(minute), + }, + // The repeated hour at a DST fall-back takes its later instant so a resume never fires early. + { timeZone: zone, adjustForTimeZone: true, disambiguation: "later" }, + ); + if (Option.isNone(reset)) return null; + // Codex drops the seconds, so the next minute is the first one past the reset. + const resetAt = DateTime.add(reset.value, { minutes: 1 }); + return DateTime.isGreaterThan(resetAt, failedAt) ? DateTime.formatIso(resetAt) : null; +} diff --git a/apps/server/src/provider/documentTextWorker.ts b/apps/server/src/provider/documentTextWorker.ts index 81008be757dac..5957fdd3cd892 100644 --- a/apps/server/src/provider/documentTextWorker.ts +++ b/apps/server/src/provider/documentTextWorker.ts @@ -14,9 +14,10 @@ const CELL_END = Symbol("cell-end"); type TextPart = string | typeof CELL_START | typeof CELL_END | { fallback: string }; const TRUNCATED = "\n\n[Extracted text truncated at document extraction limits.]"; -// Parts the document never displays: tracked deletions and moves, field codes, phonetic guides, -// paragraph properties such as tab-stop definitions. +// Parts the document never displays: the XHTML head, tracked deletions and moves, field codes, +// phonetic guides, paragraph properties such as tab-stop definitions. const HIDDEN = new Set([ + "head", "script", "style", "binary-data", @@ -41,12 +42,43 @@ interface XmlNode { hasTextChoice?: boolean; } -function xml(source: string, odfSpaces = false, htmlEntities = false): XmlNode { +// ODF 1.3 Part 3 §6.1.2: paragraph character data turns TAB, CR and LF into spaces, collapses +// space runs and trims the ends; only , and keep literal ones. +function collapseOdfWhitespace(paragraph: XmlNode) { + let afterSpace = true; + let tail: { node: XmlNode; index: number; value: string } | undefined; + const visit = (node: XmlNode) => { + for (const [index, child] of node.children.entries()) { + if (typeof child !== "string") { + if (HIDDEN.has(child.name)) continue; + if (child.spaceCount === undefined && !["tab", "line-break", "p", "h"].includes(child.name)) + visit(child); + else { + afterSpace = false; + tail = undefined; + } + continue; + } + let value = child.replaceAll(/[ \t\r\n]+/g, " "); + if (afterSpace && value.startsWith(" ")) value = value.slice(1); + if (value) { + afterSpace = value.endsWith(" "); + tail = { node, index, value }; + } + node.children[index] = value; + } + }; + visit(paragraph); + if (tail && afterSpace) tail.node.children[tail.index] = tail.value.slice(0, -1); +} + +function xml(source: string, extension: string): XmlNode { + const odf = [".odt", ".ods", ".odp"].includes(extension); const root: XmlNode = { name: "root", attrs: {}, children: [] }; const stack = [root]; let nodes = 0; const parser = new SaxesParser(); - if (htmlEntities) Object.assign(parser.ENTITIES, characterEntities); + if (extension === ".epub") Object.assign(parser.ENTITIES, characterEntities); parser.on("opentag", (tag) => { if (++nodes > 200_000 || stack.length > 128) throw new Truncation("XML structure limit"); const node: XmlNode = { @@ -59,20 +91,25 @@ function xml(source: string, odfSpaces = false, htmlEntities = false): XmlNode { ), children: [], }; - if (odfSpaces && node.name === "s") node.spaceCount = repeatCount(node.attrs.c); + if (odf && node.name === "s") node.spaceCount = repeatCount(node.attrs.c); stack.at(-1)!.children.push(node); stack.push(node); }); parser.on("closetag", () => { const node = stack.pop()!; const parent = stack.at(-1)!; + if (odf && (node.name === "p" || node.name === "h")) collapseOdfWhitespace(node); + if (extension === ".docx" && node.name === "del") { + parent.children.pop(); + return; + } // Markup-compatibility fallbacks repeat their mc:Choice content, e.g. Word text boxes. // Keep one when no Choice carries text, such as a chart with a VML caption. if (node.name === "Fallback" && parent.hasTextChoice) { parent.children.pop(); return; } - if (!node.hasText) return; + if (!node.hasText && node.name !== "noBreakHyphen") return; parent.hasText = true; if (node.name === "Choice") parent.hasTextChoice = true; }); @@ -99,6 +136,7 @@ function* text(node: XmlNode): Generator { if (HIDDEN.has(node.name)) return; if (["tab", "cell"].includes(node.name)) yield "\t"; else if (["br", "line-break", "cr"].includes(node.name)) yield "\n"; + else if (node.name === "noBreakHyphen") yield "\u2011"; else if (node.spaceCount !== undefined) { yield " ".repeat(Math.min(MAX_OUTPUT, node.spaceCount)); if (node.spaceCount > MAX_OUTPUT) throw new Truncation("Space repeat limit"); @@ -108,6 +146,23 @@ function* text(node: XmlNode): Generator { else yield* text(child); } } +// SpreadsheetML `t` and `v` values are ST_Xstring: `_xHHHH_` is one UTF-16 code unit, and an +// escaped lead underscore (`_x005F_xHHHH_`) keeps the literal `_xHHHH_`. Decoded in one pass. +function* xstringText(node: XmlNode): Generator { + if (HIDDEN.has(node.name)) return; + if (node.name !== "t" && node.name !== "v") { + for (const child of children(node)) yield* xstringText(child); + return; + } + yield node.children + .filter((child) => typeof child === "string") + .join("") + .replaceAll( + /_x005[Ff]_(x[\dA-Fa-f]{4}_)|_x([\dA-Fa-f]{4})_/g, + (_, literal?: string, hex?: string) => + literal ? `_${literal}` : String.fromCharCode(Number.parseInt(hex!, 16)), + ); +} function* paragraphs(node: XmlNode): Generator { if (HIDDEN.has(node.name)) return; for (const child of node.children) { @@ -164,7 +219,7 @@ function odfValue(cell: XmlNode): string { } } function hasCellText(cell: XmlNode): boolean { - if (odfValue(cell).trim()) return true; + if (odfValue(cell).trim() || cell.name === "noBreakHyphen") return true; // Space markers can have enormous repeat counts; emptiness never requires expanding them. if (HIDDEN.has(cell.name) || cell.spaceCount !== undefined) return false; return cell.children.some((child) => @@ -342,6 +397,10 @@ function* rtf(source: string): Generator { ["ldblquote", "“"], ["rdblquote", "”"], ["bullet", "•"], + ["zwj", "\u200d"], + ["zwnj", "\u200c"], + ["ltrmark", "\u200e"], + ["rtlmark", "\u200f"], ["~", "\u00a0"], ["_", "\u2011"], ["-", ""], @@ -437,7 +496,7 @@ async function* document(path: string, extension: string): AsyncGenerator typeof child === "string") ?? ""; if (cell.attrs.t === "s") { const shared = strings[Number(raw)]; - if (shared) yield* inCell(text(shared)); - } else if (cell.attrs.t === "inlineStr") yield* inCell(text(cell)); + if (shared) yield* inCell(xstringText(shared)); + } else if (cell.attrs.t === "inlineStr") yield* inCell(xstringText(cell)); + else if (cell.attrs.t === "b" && (raw.trim() === "0" || raw.trim() === "1")) + yield* inCell([raw.trim() === "1" ? "true" : "false"]); else { - if (value && hasCellText(value)) yield* inCell(text(value)); + if (value && hasCellText(value)) yield* inCell(xstringText(value)); else for (const formula of all(cell, "f")) yield* inCell(text(formula)); const formatId = formats[Number(cell.attrs.s ?? 0)]?.attrs.numFmtId; const format = formatId ? customFormats.get(formatId) : undefined; diff --git a/apps/server/src/provider/model-manifest.json b/apps/server/src/provider/model-manifest.json index e13e640aa1616..01727cc2680b0 100644 --- a/apps/server/src/provider/model-manifest.json +++ b/apps/server/src/provider/model-manifest.json @@ -1,6 +1,6 @@ { "version": 1, - "updatedAt": "2026-11-12T18:25:36Z", + "updatedAt": "2026-11-14T15:58:47Z", "compatibility": [ { "driver": "codex", @@ -771,7 +771,7 @@ "aliases": ["fable", "fable-5.2", "claude-fable-5.2"], "status": "current", "profile": "fable-5", - "adapter": { "claudeCode": { "minVersion": "2.1.290" } } + "adapter": { "claudeCode": { "minVersion": "2.1.321" } } }, { "slug": "claude-fable-5-1", diff --git a/apps/server/src/provider/testFixtures/grok-text-mock-agent.mjs b/apps/server/src/provider/testFixtures/grok-text-mock-agent.mjs index 357bdb7fe564d..b8cb949a5fb71 100644 --- a/apps/server/src/provider/testFixtures/grok-text-mock-agent.mjs +++ b/apps/server/src/provider/testFixtures/grok-text-mock-agent.mjs @@ -5,12 +5,35 @@ const sessionId = "grok-text-session"; let currentModelId = "grok-mock-default"; const models = ["grok-mock-default", "grok-mock-alt"]; const write = (message) => process.stdout.write(`${JSON.stringify(message)}\n`); +const permissionRequestId = 9001; +let pendingPromptId; +const completePrompt = (id) => { + write({ + jsonrpc: "2.0", + method: "session/update", + params: { + sessionId, + update: { + sessionUpdate: "agent_message_chunk", + content: { type: "text", text: process.env.T3_ACP_PROMPT_RESPONSE_TEXT ?? "" }, + }, + }, + }); + write({ jsonrpc: "2.0", id, result: { stopReason: "end_turn" } }); +}; for await (const line of NodeReadline.createInterface({ input: process.stdin })) { const request = JSON.parse(line); if (process.env.T3_ACP_REQUEST_LOG_PATH) { NodeFS.appendFileSync(process.env.T3_ACP_REQUEST_LOG_PATH, `${line}\n`); } + if (request.method === undefined) { + if (request.id === permissionRequestId && pendingPromptId !== undefined) { + completePrompt(pendingPromptId); + pendingPromptId = undefined; + } + continue; + } if (request.id === undefined) continue; let result; switch (request.method) { @@ -43,19 +66,25 @@ for await (const line of NodeReadline.createInterface({ input: process.stdin })) result = {}; break; case "session/prompt": - write({ - jsonrpc: "2.0", - method: "session/update", - params: { - sessionId, - update: { - sessionUpdate: "agent_message_chunk", - content: { type: "text", text: process.env.T3_ACP_PROMPT_RESPONSE_TEXT ?? "" }, + if (process.env.T3_ACP_REQUEST_PERMISSION) { + pendingPromptId = request.id; + write({ + jsonrpc: "2.0", + id: permissionRequestId, + method: "session/request_permission", + params: { + sessionId, + toolCall: { toolCallId: "tool-1", kind: "execute", title: "Run `git status`" }, + options: [ + { optionId: "allow-once", name: "Yes", kind: "allow_once" }, + { optionId: "reject-once", name: "No", kind: "reject_once" }, + ], }, - }, - }); - result = { stopReason: "end_turn" }; - break; + }); + continue; + } + completePrompt(request.id); + continue; default: write({ jsonrpc: "2.0", diff --git a/apps/server/src/provider/userInputAttachments.test.ts b/apps/server/src/provider/userInputAttachments.test.ts index 056f2b7bd97f5..4087601b58c0b 100644 --- a/apps/server/src/provider/userInputAttachments.test.ts +++ b/apps/server/src/provider/userInputAttachments.test.ts @@ -285,6 +285,11 @@ describe("question answer paths", () => { String.raw`{\rtf1\ansi A\enspace B\emspace C\qmspace D\~E\_F\-G}`, "A\u2002B\u2003C\u2005D\u00a0E\u2011FG", ], + [ + "joiner and direction marks", + String.raw`{\rtf1\ansi A\zwj B\zwnj C\ltrmark D\rtlmark E}`, + "A\u200dB\u200cC\u200eD\u200fE", + ], [ "text symbol fallback", String.raw`{\rtf1\ansi\uc10\u945\endash\emdash\enspace\emspace\qmspace\lquote\rquote\ldblquote\rdblquote\bullet Z}`, @@ -690,28 +695,32 @@ describe("question answer paths", () => { bounded: true, parts: { "word/document.xml": - 'Report & totalsNorth1234BeforeInnerAfterNeighbor', + 'Report & totalsNorth1234BeforeInnerAfterNeighbor', }, - expected: ["## Report & totals", "North\t12 34", "Before Inner After\tNeighbor"], + expected: [ + "## Report & totals", + "North\t12 34", + "Before Inner After\tNeighbor\t\u2011\n\u2011", + ], }, { ext: "docx", bounded: true, parts: { "word/document.xml": - 'FirstSecondThird', + 'FirstSecondThirdparty‑End', }, - expected: ["First\nSecond\nThird"], + expected: ["First\nSecond\nThird\u2011party\u2011End"], }, { ext: "docx", bounded: true, parts: { "word/document.xml": - 'Intro PAGEREF _Toc1 \\h 3Fee $500$700Moveddue', + 'Intro PAGEREF _Toc1 \\h 3Fee $500$700Moveddue', }, expected: ["Intro 3\nFee $700\tdue"], - absent: ["PAGEREF", "$500", "Moved"], + absent: ["PAGEREF", "$500", "Moved", "\u2011"], }, { ext: "odt", @@ -748,6 +757,18 @@ describe("question answer paths", () => { ], absent: ["SUM(0,0)", "[Extracted text truncated"], }, + { + ext: "xlsx", + bounded: true, + parts: { + "xl/workbook.xml": '', + "xl/_rels/workbook.xml.rels": + '', + "xl/worksheets/a.xml": + '1012', + }, + expected: ["true\tfalse\t1\t2"], + }, { ext: "xlsx", parts: { @@ -788,6 +809,22 @@ describe("question answer paths", () => { expected: ["## Sheet: Phonetics", "東京\t東京"], absent: ["とうきょう"], }, + { + ext: "xlsx", + bounded: true, + parts: { + "xl/workbook.xml": '', + "xl/_rels/workbook.xml.rels": + '', + "xl/sharedStrings.xml": + "first_x000D_\nsecond_x005F_x0041__x005F_x005F_x0041_Bold_x000D__x005F_x000D_&_xnothex_ cap_X0041_ _x000d_ tail", + "xl/worksheets/a.xml": + '01234_x005F_x005F_first_x000D_secondA1Total_x000D_due', + }, + expected: [ + "## Sheet: Escapes\nfirst second\t_x0041_\t_x005F_x0041_\tBold _x000D_\t&_xnothex_ cap_X0041_ tail\n_x005F_\tfirst second\tTotal due", + ], + }, { ext: "xlsx", bounded: true, @@ -891,9 +928,11 @@ describe("question answer paths", () => { ext: "odt", parts: { "content.xml": - 'Orchard19violetcrates', + '\n Orchard\n19violetcrates \n\t shipped todayDockGate\u00a0B SecondAconsole.log(1);\n B', }, - expected: ["## Orchard", "19 violet crates"], + expected: [ + "## Orchard\n19 violet crates shipped today\tDock\nGate\u00a0B\nSecond\nA B", + ], }, { ext: "ods", @@ -940,9 +979,9 @@ describe("question answer paths", () => { ext: "odp", parts: { "content.xml": - "FirstSecond", + "\n First\n\t slide Second", }, - expected: ["## Slide 1", "First", "## Slide 2", "Second"], + expected: ["## Slide 1\nFirst slide\n\n## Slide 2\nSecond"], }, { ext: "ods", @@ -1037,13 +1076,13 @@ describe("question answer paths", () => { "OEBPS/book.opf": '', "OEBPS/a.xhtml": - '

Last chapter

Price 10 8

', + '

Last chapter

Price 10 8 old

', "OEBPS/b.xhtml": - "

First chapter

Larkspur

Direct div
Direct section
  • Direct item emphasis trailing
  • Account: A
    Balance: 731Currency: USD
    HeadingTotal
    Important value731
    Before
    Inner
    After
    Neighbor
    ", + 'My Novel

    First chapter

    Larkspur

    Direct div
    Direct section
  • Direct item emphasis trailing
  • Account: A
    Balance: 731Currency: USD
    HeadingTotal
    Important value731
    Before
    Inner
    After
    Neighbor
    ', }, expected: [ "## Chapter 1", - "# First chapter", + "\n# First chapter", "Larkspur", "Direct div", "Direct section", @@ -1054,8 +1093,9 @@ describe("question answer paths", () => { "Before Inner After\tNeighbor", "## Chapter 2", "Last chapter", - "Price 10 8", + "Price 10 8 old", ], + absent: ["My Novel", "Hidden Author"], }, ]; for (const [caseIndex, item] of cases.entries()) { diff --git a/apps/server/src/pullRequest/PullRequestService.test.ts b/apps/server/src/pullRequest/PullRequestService.test.ts index 5c01f3d795924..a941a2cbd8422 100644 --- a/apps/server/src/pullRequest/PullRequestService.test.ts +++ b/apps/server/src/pullRequest/PullRequestService.test.ts @@ -2486,7 +2486,10 @@ it.effect.each([ }), ); -it.effect("refuses Azure cross-organization reads and writes without its checkout", () => +it.effect.each([ + { case: "another organization", repository: "org-b/project/_git/web" }, + { case: "a sibling repository", repository: "org-a/project/_git/web-e2e" }, +])("refuses Azure reads and writes in $case without its checkout", ({ repository }) => Effect.gen(function* () { const service = yield* makeService({ projects: [ @@ -2510,15 +2513,20 @@ it.effect("refuses Azure cross-organization reads and writes without its checkou const reference = { projectId: "org-a" as ProjectId, host: "dev.azure.com", - repository: "org-b/project/_git/web", + repository, number: 7, }; const readError = yield* Effect.flip( service.summary(reference, { recoverTransientFailure: false }), ); const writeError = yield* Effect.flip(service.runAction({ ...reference, action: "close" })); - assert.strictEqual(readError._tag, "PullRequestUnavailableError"); - assert.strictEqual(writeError._tag, "PullRequestUnavailableError"); + for (const error of [readError, writeError]) { + expect(error).toMatchObject({ + _tag: "PullRequestUnavailableError", + reason: "repository-without-project", + message: `${repository} is not a project in T3 Code. Add it as a project to read its change requests.`, + }); + } }), ); @@ -3128,7 +3136,13 @@ it.effect("carries a slice on without sending the rows it already sent", () => cursors: { "github.com acme/web": "2026-07-02T00:00:00Z|1|3" }, }); - assert.deepStrictEqual(cursors, [{ updatedBefore: "2026-07-02T00:00:00Z", delivered: 1 }]); + assert.deepStrictEqual(cursors, [ + { + updatedBefore: "2026-07-02T00:00:00Z", + delivered: 1, + seenAt: [{ repository: "acme/web", number: 3 }], + }, + ]); assert.deepStrictEqual( result.entries.map((entry) => entry.number), [4], @@ -3354,6 +3368,40 @@ it.effect("reads the fresh diff when detail or summary discovers a changed revis }), ); +it.effect("keeps a withheld file's read out of the page's cached diff", () => + Effect.gen(function* () { + const provider = fakePullRequestProvider(SourceControlProviderKind.make("github"), { + getDiff: (input) => + Effect.succeed({ + patch: input.path === undefined ? "page" : `file ${input.path}`, + truncated: input.path === undefined, + nextCursor: null, + }), + }); + const service = yield* makeService({ + projects: [project({ id: "p1", title: "web", workspaceRoot: "/a", repository: "acme/web" })], + providers: [provider], + }); + const ref = { projectId: "p1" as ProjectId, repository: "acme/web", number: 10 }; + assert.strictEqual((yield* service.diff(ref)).patch, "page"); + assert.strictEqual( + (yield* service.diff({ ...ref, path: "sdk.patch" })).patch, + "file sdk.patch", + ); + assert.strictEqual((yield* service.diff(ref)).patch, "page"); + + // A host that would answer with its whole diff must not be cached as one file's. + const wholeDiffOnly = yield* makeService({ + projects: [project({ id: "p1", title: "web", workspaceRoot: "/a", repository: "acme/web" })], + providers: [ + { ...provider, capabilities: { ...provider.capabilities, withheldFileDiff: false } }, + ], + }); + const error = yield* Effect.flip(wholeDiffOnly.diff({ ...ref, path: "sdk.patch" })); + assert.strictEqual(error._tag, "PullRequestOperationError"); + }), +); + it.effect("keeps routed reads separate when the GitHub account changes", () => Effect.gen(function* () { for (const operation of ["summary", "detail", "checks", "diff", "filesViewed"] as const) { @@ -3563,6 +3611,7 @@ it.effect( Effect.succeed({ ...changeRequest(1, "2026-07-02T00:00:00Z"), isDraft: true, + rebaseRequired: false, reviewDecision: "approved", checksState: "passing", }), @@ -3572,6 +3621,7 @@ it.effect( deletions: 3, changedFiles: 5, mergeability: "conflicting", + rebaseRequired: true, }), }), ], @@ -3579,6 +3629,8 @@ it.effect( yield* service.summary(reference); const detail = yield* service.detail(reference); const summary = yield* service.summary(reference); + assert.strictEqual(detail.rebaseRequired, true); + assert.strictEqual(summary.rebaseRequired, true); assert.strictEqual(summary.isDraft, false); assert.deepStrictEqual(summary.author, detail.author); assert.strictEqual(summary.additions, 14); diff --git a/apps/server/src/pullRequest/PullRequestService.ts b/apps/server/src/pullRequest/PullRequestService.ts index 15d658df5ad97..de567fc3e9582 100644 --- a/apps/server/src/pullRequest/PullRequestService.ts +++ b/apps/server/src/pullRequest/PullRequestService.ts @@ -1162,6 +1162,19 @@ export const make = Effect.gen(function* () { }; } } + const owner = + publicHost ?? sourceControlDefinitions.findByRepositoryKey(referenceKey); + const readable = owner === undefined ? null : registry.get(owner.kind); + if ( + readable !== null && + !readable.readsPublicHostWithoutCheckout && + readable.servesHost?.(host) !== false + ) { + return yield* new PullRequestUnavailableError({ + reason: "repository-without-project", + repository, + }); + } return yield* new PullRequestUnavailableError({ reason: "provider-unsupported" }); }); } @@ -1400,6 +1413,9 @@ export const make = Effect.gen(function* () { state: input.item.state, isDraft: input.item.isDraft, mergeability: input.item.mergeability, + ...(input.item.rebaseRequired === undefined + ? {} + : { rebaseRequired: input.item.rebaseRequired }), additions: input.item.additions, deletions: input.item.deletions, createdAt: input.item.createdAt, @@ -1653,7 +1669,18 @@ export const make = Effect.gen(function* () { filters: input.filters, ...(cursor === undefined ? {} - : { cursor: { updatedBefore: cursor.updatedBefore, delivered: cursor.delivered } }), + : { + cursor: { + updatedBefore: cursor.updatedBefore, + delivered: cursor.delivered, + seenAt: chunk.flatMap((project) => + (cursorOf(project)?.seenAt ?? []).map((number) => ({ + repository: project.repository, + number, + })), + ), + }, + }), }).pipe( observeRead, Effect.flatMap(({ value: page, observedAt }) => { @@ -1884,6 +1911,9 @@ export const make = Effect.gen(function* () { ...(changeRequest.mergeability === undefined ? {} : { mergeability: changeRequest.mergeability }), + ...(changeRequest.rebaseRequired === undefined + ? {} + : { rebaseRequired: changeRequest.rebaseRequired }), ...(changeRequest.stack === undefined ? {} : { stack: changeRequest.stack }), ...(changeRequest.headSha?.trim() ? { headSha: changeRequest.headSha.trim() } : {}), ...(changeRequest.autoMergeEnabled === undefined @@ -1983,6 +2013,9 @@ export const make = Effect.gen(function* () { state: changeRequest.state, isDraft: changeRequest.isDraft, mergeability: changeRequest.mergeability, + ...(changeRequest.rebaseRequired === undefined + ? {} + : { rebaseRequired: changeRequest.rebaseRequired }), additions: changeRequest.additions, deletions: changeRequest.deletions, changedFiles: changeRequest.changedFiles, @@ -2119,23 +2152,31 @@ export const make = Effect.gen(function* () { const diffUncached: PullRequestService["Service"]["diff"] = (input) => requireProject(input).pipe( Effect.flatMap((project) => - project.api.capabilities.diff - ? project.api - .getDiff({ - cwd: project.project.workspaceRoot, - repository: project.repository, - host: project.host, - number: input.number, - ...(input.cursor === undefined ? {} : { cursor: input.cursor }), - ...(input.commit === undefined ? {} : { commit: input.commit }), - }) - .pipe(Effect.mapError(toPullRequestError("diff"))) - : Effect.fail( + input.path !== undefined && project.api.capabilities.withheldFileDiff !== true + ? Effect.fail( new PullRequestOperationError({ operation: "diff", - detail: "This host cannot provide a diff for a change request.", + detail: "This host cannot read one file of a change request's diff.", }), - ), + ) + : project.api.capabilities.diff + ? project.api + .getDiff({ + cwd: project.project.workspaceRoot, + repository: project.repository, + host: project.host, + number: input.number, + ...(input.path === undefined ? {} : { path: input.path }), + ...(input.cursor === undefined ? {} : { cursor: input.cursor }), + ...(input.commit === undefined ? {} : { commit: input.commit }), + }) + .pipe(Effect.mapError(toPullRequestError("diff"))) + : Effect.fail( + new PullRequestOperationError({ + operation: "diff", + detail: "This host cannot provide a diff for a change request.", + }), + ), ), ); @@ -3422,6 +3463,7 @@ export const make = Effect.gen(function* () { deletions: detail.deletions, changedFiles: detail.changedFiles, mergeability: detail.mergeability, + ...(detail.rebaseRequired === undefined ? {} : { rebaseRequired: detail.rebaseRequired }), headBranch: detail.headBranch, baseBranch: detail.baseBranch, closedAt: detail.closedAt, @@ -3585,16 +3627,26 @@ export const make = Effect.gen(function* () { const diffCache = yield* Cache.makeWith( (key: string) => { - const [reference, cursor, commit] = JSON.parse(key) as [string, string | null, string | null]; + const [reference, cursor, commit, , path] = JSON.parse(key) as [ + string, + string | null, + string | null, + string | null, + string | null, + ]; return diffUncached({ ...refOfCacheKey(reference), + ...(path === null ? {} : { path }), ...(cursor === null ? {} : { cursor }), ...(commit === null ? {} : { commit }), } as PullRequestDiffInput).pipe( observeRead, Effect.tap(({ value, observedAt }) => { // Keep the loader's epoch and read time if a refresh occurs during the provider read. - diffFiles.record(JSON.stringify([reference, cursor, commit]), value, observedAt); + // One withheld file is not the page's file list. + if (path === null) { + diffFiles.record(JSON.stringify([reference, cursor, commit]), value, observedAt); + } return Effect.void; }), ); @@ -3641,6 +3693,7 @@ export const make = Effect.gen(function* () { input.commit === undefined ? (lastGoodSummary.peek(refCacheKey(input))?.updatedAt ?? null) : null, + input.path ?? null, ]); const evictDiffs = Cache.entries(diffCache).pipe( Effect.flatMap((entries) => diff --git a/apps/server/src/relay/AgentAwarenessRelay.test.ts b/apps/server/src/relay/AgentAwarenessRelay.test.ts index 68e2289524c09..10388e99fe7c5 100644 --- a/apps/server/src/relay/AgentAwarenessRelay.test.ts +++ b/apps/server/src/relay/AgentAwarenessRelay.test.ts @@ -753,6 +753,37 @@ describe("AgentAwarenessRelay", () => { }), ); + it.effect( + "clears a completion that landed while snoozed, even when processed after the wake", + () => + Effect.gen(function* () { + yield* TestClock.setTime(DateTime.toEpochMillis(DateTime.makeUnsafe(NOW))); + const { relay, currentShell, publications } = yield* makeTestRelay(); + const snooze = { + snoozedAt: DateTime.makeUnsafe(NOW), + snoozedUntil: DateTime.makeUnsafe("2026-09-04T13:00:00.000Z"), + }; + yield* Ref.set(currentShell, shell(snooze)); + yield* relay.publishThread(THREAD_ID); + yield* TestClock.adjust("2 hours"); + yield* Ref.set( + currentShell, + shell({ + ...snooze, + status: "completed", + latestRunCompletedAt: DateTime.makeUnsafe("2026-09-04T12:30:00.000Z"), + }), + ); + yield* relay.publishThread(THREAD_ID); + yield* TestClock.adjust("5 seconds"); + yield* relay.drain; + assert.deepEqual( + publications.map((publication) => publication.state?.phase ?? null), + ["running", null], + ); + }), + ); + it.effect("still publishes a completion when the thread settles during the hold", () => Effect.gen(function* () { const { relay, currentShell, publications } = yield* makeTestRelay(); diff --git a/apps/server/src/relay/AgentAwarenessRelay.ts b/apps/server/src/relay/AgentAwarenessRelay.ts index 76f854548deb6..ef2e34799742c 100644 --- a/apps/server/src/relay/AgentAwarenessRelay.ts +++ b/apps/server/src/relay/AgentAwarenessRelay.ts @@ -51,6 +51,7 @@ import { import { EnvironmentSigningKey } from "../cloud/environmentKeys.ts"; import * as ServerEnvironment from "../environment/ServerEnvironment.ts"; import * as ThreadManagement from "../orchestration-v2/ThreadManagementService.ts"; +import { isSnoozed } from "../orchestration-v2/ThreadSettlementService.ts"; import * as ProjectService from "../project/ProjectService.ts"; import { forkParked } from "../serverActivation.ts"; @@ -360,12 +361,27 @@ function resolveAgentAwarenessRelayPublishSnapshot(input: { state?.phase !== projectThreadAwarenessV2(awarenessInput)?.phase; return { projectId: input.thread.value.projectId, - state: sanitizeRelayAgentActivityState(state), + state: snoozedCompletion(input.thread.value, state) + ? null + : sanitizeRelayAgentActivityState(state), subagentWait: childDrivesWait ? input.pendingSubagentRequest : null, reason: "snapshot", }; } +// Checked at completion time, so a completion processed after the wake stays silent too. +function snoozedCompletion( + thread: AgentAwarenessThread, + state: { readonly phase: string } | null, +): boolean { + if (state?.phase !== "completed" || thread.latestRunCompletedAt == null) return false; + const completedAt = DateTime.toEpochMillis(thread.latestRunCompletedAt); + if (thread.snoozedAt != null && completedAt < DateTime.toEpochMillis(thread.snoozedAt)) { + return false; + } + return isSnoozed(thread, completedAt); +} + function terminalWorkSinceStart(thread: AgentAwarenessThread, startedAt: number): boolean { return ( thread.latestRunCompletedAt != null && diff --git a/apps/server/src/scheduledTasks/ScheduledTaskService.interval.test.ts b/apps/server/src/scheduledTasks/ScheduledTaskService.interval.test.ts index ecc761d1d0b47..86d6f26e06c44 100644 --- a/apps/server/src/scheduledTasks/ScheduledTaskService.interval.test.ts +++ b/apps/server/src/scheduledTasks/ScheduledTaskService.interval.test.ts @@ -26,6 +26,7 @@ const dependencies = Layer.mergeAll( ManagedPolicy.layerTest(), Layer.mock(ThreadLaunchService.ThreadLaunchService)({ launch: () => Effect.die("synthetic dispatch failure"), + isNonGitProject: () => Effect.succeed(false), }), Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({ namedProjectsRoot: "/projects", diff --git a/apps/server/src/scheduledTasks/ScheduledTaskService.test.ts b/apps/server/src/scheduledTasks/ScheduledTaskService.test.ts index ee4385ff2e0af..1335e369484a5 100644 --- a/apps/server/src/scheduledTasks/ScheduledTaskService.test.ts +++ b/apps/server/src/scheduledTasks/ScheduledTaskService.test.ts @@ -4,6 +4,7 @@ import * as NodeUtil from "node:util"; import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; import { assert, it, vi } from "@effect/vitest"; import { + CommandId, ProjectId, ProviderInstanceId, ScheduledTaskError, @@ -29,6 +30,7 @@ import * as TestClock from "effect/testing/TestClock"; import * as ThreadLaunchService from "../orchestration-v2/ThreadLaunchService.ts"; import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts"; +import { OrchestratorDispatchError } from "../orchestration-v2/Orchestrator.ts"; import * as SecretRequests from "../secrets/SecretRequests.ts"; import * as ManagedProjectFolders from "../project/ManagedProjectFolders.ts"; import * as SqlitePersistence from "../persistence/Sqlite.ts"; @@ -663,6 +665,67 @@ it.effect("a fresh-session task launches one thread, then starts each later run }).pipe(Effect.provide(SqlitePersistence.layerMemory)), ); +it.effect("a refused dispatch records its refusal, and opaque causes stay generic", () => + Effect.gen(function* () { + const refusal = "Thread thread:refused was stopped."; + const refused = (cause: unknown) => + new OrchestratorDispatchError({ + commandId: CommandId.make("command:refused"), + commandType: "message.dispatch", + cause, + }); + const generic = refused(undefined).message; + const cases = [ + [refusal, refusal], + [" ", generic], + [new Error("private-nested-secret"), generic], + ] as const; + const failure = yield* Ref.make(undefined); + const layer = ScheduledTaskService.layer.pipe( + Layer.provide( + Layer.mergeAll( + ManagedPolicy.layerTest(), + Layer.mock(ThreadLaunchService.ThreadLaunchService)({}), + Layer.mock(ThreadManagementService.ThreadManagementService)({ + getThreadShell: () => Effect.succeed(threadShell(ProjectId.make("project:test"))), + sendToThread: () => + Ref.get(failure).pipe(Effect.flatMap((cause) => Effect.fail(refused(cause)))), + }), + Layer.mock(SecretRequests.SecretRequests)({}), + Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({ + namedProjectsRoot: "/projects", + isScratchProject: () => Effect.succeed(false), + }), + NodeCrypto.layer, + Layer.succeed(Scheduler.Scheduler, { register: () => Effect.void }), + ), + ), + ); + yield* Effect.gen(function* () { + const service = yield* ScheduledTaskService.ScheduledTaskService; + const { task } = yield* service.upsert({ + id: ScheduledTaskId.make("task:refused"), + title: "Refused dispatch", + prompt: "Check progress", + enabled: true, + schedule: { type: "interval", everyMs: 60_000 }, + projectId: ProjectId.make("project:test"), + threadId: ThreadId.make("thread:refused"), + workspaceStrategy: { type: "root" }, + modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" }, + runtimeMode: "full-access", + interactionMode: "default", + }); + for (const [cause, expected] of cases) { + yield* Ref.set(failure, cause); + const result = yield* service.runNow({ id: task.id }); + assert.equal(result.task.lastRunStatus, "failed"); + assert.equal(result.task.lastRunError, expected); + } + }).pipe(Effect.provide(layer)); + }).pipe(Effect.provide(SqlitePersistence.layerMemory)), +); + it.effect("a first run whose thread was deleted before it bound removes the task", () => Effect.gen(function* () { const layer = ScheduledTaskService.layer.pipe( diff --git a/apps/server/src/scheduledTasks/ScheduledTaskService.ts b/apps/server/src/scheduledTasks/ScheduledTaskService.ts index 5af0aba39e6ed..9ed134be2da5a 100644 --- a/apps/server/src/scheduledTasks/ScheduledTaskService.ts +++ b/apps/server/src/scheduledTasks/ScheduledTaskService.ts @@ -53,6 +53,8 @@ import * as ThreadLaunchService from "../orchestration-v2/ThreadLaunchService.ts import * as ManagedProjectFolders from "../project/ManagedProjectFolders.ts"; import * as Metrics from "../observability/Metrics.ts"; import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts"; +import { OrchestratorDispatchError } from "../orchestration-v2/Orchestrator.ts"; +import { userFacingDispatchErrorMessage } from "../orchestration-v2/UserFacingErrors.ts"; import * as SecretRequests from "../secrets/SecretRequests.ts"; import * as ManagedPolicy from "../policy/ManagedPolicy.ts"; import * as DueHint from "../scheduling/DueHint.ts"; @@ -79,6 +81,7 @@ import { constantTimeEquals, verifyWebhookSignature } from "./webhookVerificatio export const WEBHOOK_ROUTE_PREFIX = "/api/hooks"; const isScheduledTaskError = Schema.is(ScheduledTaskError); +const isOrchestratorDispatchError = Schema.is(OrchestratorDispatchError); const PROMPT_TOO_LONG = "The filled-in prompt is too long."; const WEBHOOK_DELIVERY_RETENTION = 50; const WEBHOOK_DEDUPE_DELIVERY_RETENTION = 20; @@ -364,6 +367,14 @@ function errorMessage(error: unknown): string { return failure instanceof Error ? failure.message : String(failure); } +function dispatchErrorMessage(cause: Cause.Cause): string { + const failure = Cause.squash(cause); + // Direct orchestration refusals are user-facing; nested defects can contain private data. + return isOrchestratorDispatchError(failure) && typeof failure.cause === "string" + ? (userFacingDispatchErrorMessage(failure.cause) ?? errorMessage(failure)) + : errorMessage(failure); +} + function webhookPath(taskId: string, token: string): string { return `${WEBHOOK_ROUTE_PREFIX}/${encodeURIComponent(taskId)}/${token}`; } @@ -1098,7 +1109,7 @@ export const layer = Layer.effect( if (webhook !== undefined && runSucceeded) yield* Ref.set(webhook.dispatched, true); const completedAt = yield* localNow; const lastRunStatus = runSucceeded ? ("succeeded" as const) : ("failed" as const); - const lastRunError = runSucceeded ? null : errorMessage(result.cause); + const lastRunError = runSucceeded ? null : dispatchErrorMessage(result.cause); const threadId = runSucceeded ? result.value : null; // One transaction, so an edit cannot land between reading the schedule and // writing the next run computed from it. The origin is read first: it reads @@ -1511,9 +1522,11 @@ export const layer = Layer.effect( return { token, secret, secretChanged }; }) : { token: null, secret: null, secretChanged: true }; + // A folder that is not a Git repository, Scratch included, cannot hold a worktree. const workspaceStrategy: ScheduledTask["workspaceStrategy"] = input.workspaceStrategy.type === "worktree" && - (yield* managedFolders.isScratchProject(input.projectId)) + ((yield* managedFolders.isScratchProject(input.projectId)) || + (yield* threadLaunch.isNonGitProject(input.projectId))) ? { type: "root" } : input.workspaceStrategy; const scheduleUnchanged = diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts index f578fe4a85692..f4f9c9987ad1a 100644 --- a/apps/server/src/server.ts +++ b/apps/server/src/server.ts @@ -45,6 +45,7 @@ import { fixPath } from "./os-jank.ts"; import * as Ws from "./ws.ts"; import * as AgentScopeLive from "./process/agentScope.ts"; import * as ExternalLauncher from "./process/externalLauncher.ts"; +import * as SpawnWorker from "./process/SpawnWorker.ts"; import * as NodePtyAdapter from "./terminal/NodePtyAdapter.ts"; import * as PullRequestHttp from "./pullRequest/http.ts"; import * as SpeechModelStore from "./speech/SpeechModelStore.ts"; @@ -131,6 +132,7 @@ import * as KnownWorkspaces from "./workspace/KnownWorkspaces.ts"; import * as WorkspaceFileSystem from "./workspace/WorkspaceFileSystem.ts"; import * as WorkspacePaths from "./workspace/WorkspacePaths.ts"; import * as GitVcsDriver from "./vcs/GitVcsDriver.ts"; +import * as VcsAutomaticFetchInterval from "./vcs/VcsAutomaticFetchInterval.ts"; import * as VcsDriverRegistry from "./vcs/VcsDriverRegistry.ts"; import * as VcsProjectConfig from "./vcs/VcsProjectConfig.ts"; import * as VcsProcess from "./vcs/VcsProcess.ts"; @@ -349,6 +351,9 @@ const layerRelayClient = Layer.unwrap( }), ).pipe(Layer.provide(ManagedPolicy.layer)); +// NodeServices and NodeHttpServer each output Node's own spawner, so each is followed by this one. +const layerSpawner = SpawnWorker.layerServer.pipe(Layer.provide(NodeServices.layer)); + const layerHttpServer = Layer.unwrap( Effect.gen(function* () { const config = yield* ServerConfig.ServerConfig; @@ -385,7 +390,7 @@ const layerHttpServer = Layer.unwrap( // `threshold` when context takeover is disabled). websocket: { perMessageDeflate: true }, }, - ); + ).pipe(Layer.merge(layerSpawner)); }), ).pipe( Layer.provide(ManagedPolicy.layer), @@ -395,7 +400,7 @@ const layerHttpServer = Layer.unwrap( const layerPlatformServices = DocumentText.layer.pipe( Layer.provideMerge(NonBlockingSpawner.layer), - Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), + Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, layerSpawner)), ); const layerPersistence = Layer.empty.pipe(Layer.provideMerge(SqlitePersistence.layerConfig)); @@ -496,7 +501,6 @@ const layerVcs = Layer.empty.pipe( Layer.provideMerge( VcsStatusBroadcaster.layer.pipe( Layer.provide(layerGitWorkflow), - Layer.provide(VcsStatusBroadcaster.layerAutomaticFetchInterval), // The orchestration runtime also consumes the broadcaster (run // finalization), so the policy cannot read the store from the runtime's // output. @@ -509,7 +513,10 @@ const layerVcs = Layer.empty.pipe( const layerCheckpointStore = CheckpointStore.layer.pipe(Layer.provide(layerVcsDriverRegistry)); -const layerPortScanner = PortScanner.layer.pipe(Layer.provide(ProcessRunner.layer)); +const layerPortScanner = PortScanner.layer.pipe( + Layer.provide(ProcessRunner.layer), + Layer.provide(layerBackground), +); const layerTerminal = TerminalManager.layer( ProviderInstanceRegistryHydration.deriveProviderInstanceConfigMap, @@ -816,6 +823,9 @@ const layerRuntimeDependencies = layerRuntimeCoreDependencies.pipe( Layer.provideMerge(RemoteOpenTargets.layer), Layer.provideMerge(DirectEndpoints.layer.pipe(Layer.provide(layerServerEnvironment))), Layer.provideMerge(ServerLifecycleEvents.layer), + // GitVcsDriver.layer is memoized and some of its build sites live outside this file, so the + // fetch interval goes in context here, before the first build captures it. + Layer.provideMerge(VcsAutomaticFetchInterval.layer.pipe(Layer.provide(layerServerSettings))), Layer.provide(NetService.layer), ); @@ -1367,6 +1377,6 @@ const layerMakeServer = Layer.unwrap( ).pipe(Layer.provide(ProcessRunner.layer)); export const runServer = Layer.launch( - // For the layers above whose spawner requirement is met by the caller's runtime. - layerMakeServer.pipe(Layer.provide(NonBlockingSpawner.layer)), + // For the layers above whose spawner requirement is not met by one of them. + layerMakeServer.pipe(Layer.provide(NonBlockingSpawner.layer.pipe(Layer.provide(layerSpawner)))), ); diff --git a/apps/server/src/serverSettings.test.ts b/apps/server/src/serverSettings.test.ts index eff121966a929..28ce2080a9b91 100644 --- a/apps/server/src/serverSettings.test.ts +++ b/apps/server/src/serverSettings.test.ts @@ -1485,7 +1485,7 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings" { kind: "eas", id: "eas", - label: "EAS Simulator", + label: "Expo Simulators", accessToken: "expo-secret", maxSessionMinutes: 30, idleMinutes: 10, @@ -1807,7 +1807,7 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings" { kind: "eas", id: "eas", - label: "EAS Simulator", + label: "Expo Simulators", accessToken: easReference, maxSessionMinutes: 30, idleMinutes: 10, @@ -1927,7 +1927,7 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings" { kind: "eas", id: "eas", - label: "EAS Simulator", + label: "Expo Simulators", accessToken: personal("Expo/token"), maxSessionMinutes: 30, idleMinutes: 10, diff --git a/apps/server/src/settingsSecrets.ts b/apps/server/src/settingsSecrets.ts index a68519923d19f..ae6d66450e2c2 100644 --- a/apps/server/src/settingsSecrets.ts +++ b/apps/server/src/settingsSecrets.ts @@ -250,7 +250,7 @@ function mapSecretFieldGroups( ) as EasDeviceHostConfig, ); - // Its own prefix: an EAS and a Simlock host may share an id. + // Its own prefix: an Expo and a Simlock host may share an id. const simlockDeviceHosts = settings.simlockDeviceHosts.map( (host) => mapFields( diff --git a/apps/server/src/speech/SpeechService.test.ts b/apps/server/src/speech/SpeechService.test.ts index 2e05132cd6eea..3f580b0743b40 100644 --- a/apps/server/src/speech/SpeechService.test.ts +++ b/apps/server/src/speech/SpeechService.test.ts @@ -450,14 +450,18 @@ it.effect("honors exact aliases and phrase boundaries in batch and streamed text { term: "McDonald’s", aliases: [] }, { term: "King’s Cross", aliases: [] }, { term: "C#", aliases: [] }, + { term: "React", aliases: ["瑞安特"] }, + { term: "Kubernetes", aliases: [] }, ], }; const spoken = 'a test with a i and ay eye. Robert, Roberts. "the publisher" and the publisher. (git hub), git (hub). ' + - "git hub's API, git hub’s API, git’s hub, mac donald's menu, king's cross. vitamin c and plan c."; + "git hub's API, git hub’s API, git’s hub, mac donald's menu, king's cross. vitamin c and plan c. " + + "我用github和瑞安特,部署到kubernetis上。Robert用git hub。githubで管理する。"; const expected = "a test with AI and AI. Bob, Roberts. O'Reilly and the publisher. (GitHub), git (hub). " + - "GitHub's API, GitHub’s API, git’s hub, McDonald’s menu, King’s Cross. vitamin c and plan c."; + "GitHub's API, GitHub’s API, git’s hub, McDonald’s menu, King’s Cross. vitamin c and plan c. " + + "我用GitHub和React,部署到Kubernetes上。Bob用GitHub。GitHubで管理する。"; native.transcribe.mockResolvedValueOnce({ text: spoken }); native.finish.mockResolvedValueOnce(spoken); expect(yield* speech.transcribe(pcm(), options)).toBe(expected); diff --git a/apps/server/src/speech/customWords.ts b/apps/server/src/speech/customWords.ts index 98c55e883749a..981632c46172c 100644 --- a/apps/server/src/speech/customWords.ts +++ b/apps/server/src/speech/customWords.ts @@ -1,5 +1,11 @@ import type { SpeechCustomWords } from "@t3tools/contracts"; +// These scripts are written without spaces, so a neighbor in one of them still ends a word. +const UNSPACED_SCRIPTS = "\\p{scx=Han}\\p{scx=Hiragana}\\p{scx=Katakana}"; +const LETTER = `[[\\p{L}\\p{M}\\p{N}]--[${UNSPACED_SCRIPTS}]]`; +const WORD_CHARACTER = `[[\\p{L}\\p{M}\\p{N}\\p{Pc}]--[${UNSPACED_SCRIPTS}]]`; +const TOKEN = new RegExp(`${LETTER}(?:[^\\s${UNSPACED_SCRIPTS}]*${LETTER})?`, "gv"); + /** Fuzzy-matches preferred terms and rewrites aliases to their preferred spelling. */ export function makeSpeechTextCorrector( words: readonly string[], @@ -112,7 +118,7 @@ export function makeSpeechTextCorrector( const apply = (text: string): string => { if (keys.length === 0) return text; // Leave surrounding punctuation in the source; only horizontal whitespace can join words. - const tokens = [...text.matchAll(/[\p{L}\p{M}\p{N}](?:\S*[\p{L}\p{M}\p{N}])?/gu)] + const tokens = [...text.matchAll(TOKEN)] .filter(([word]) => /^[\p{L}\p{M}\p{N}'’-]+$/u.test(word)) .map((match) => ({ value: match[0], @@ -168,10 +174,9 @@ export function makeSpeechTextCorrector( const lookup = new Map( spellings.map(({ spelling, term }) => [spelling.toLocaleLowerCase(), term]), ); - const word = "[\\p{L}\\p{M}\\p{N}\\p{Pc}]"; const pattern = new RegExp( - `(? escapePattern(spelling)).join("|")})(?!${word})`, - "giu", + `(? escapePattern(spelling)).join("|")})(?!${WORD_CHARACTER})`, + "giv", ); return (text: string) => { let output = ""; diff --git a/apps/server/src/speech/postProcessing.test.ts b/apps/server/src/speech/postProcessing.test.ts new file mode 100644 index 0000000000000..3dd64c9b1bcc7 --- /dev/null +++ b/apps/server/src/speech/postProcessing.test.ts @@ -0,0 +1,81 @@ +import * as NodeServices from "@effect/platform-node/NodeServices"; +import { assert, it } from "@effect/vitest"; +import { ProviderInstanceId, type ModelSelection } from "@t3tools/contracts"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as Ref from "effect/Ref"; + +import * as ProviderRegistry from "../provider/ProviderRegistry.ts"; +import * as ServerSettings from "../serverSettings.ts"; +import * as TextGeneration from "../textGeneration/TextGeneration.ts"; +import * as SpeechPostProcessing from "./postProcessing.ts"; + +const textGenerationSelection = { + instanceId: ProviderInstanceId.make("fakeapi"), + model: "fake-model", +}; +const acpSelection = { instanceId: ProviderInstanceId.make("acp_local"), model: "default" }; + +it.effect("cleanup pinned to a provider that cannot generate text uses the text model", () => + Effect.gen(function* () { + const used = yield* Ref.make(null); + const layer = SpeechPostProcessing.layer.pipe( + Layer.provide( + Layer.succeed(TextGeneration.TextGeneration, { + generateTranscriptionPostProcessing: (input: { modelSelection: ModelSelection }) => + Ref.set(used, input.modelSelection).pipe(Effect.as({ transcription: "Hello, world." })), + } as unknown as TextGeneration.TextGeneration["Service"]), + ), + Layer.provide( + Layer.succeed(ProviderRegistry.ProviderRegistry, { + getProviders: Effect.succeed([ + { + instanceId: acpSelection.instanceId, + driver: "acpRegistry", + enabled: true, + installed: true, + supportsTextGeneration: false, + }, + ]), + } as unknown as ProviderRegistry.ProviderRegistry["Service"]), + ), + Layer.provide( + ServerSettings.layerTest({ + providerInstances: { + [acpSelection.instanceId]: { + driver: "acpRegistry", + enabled: true, + config: { source: "local", commandPath: "acp-agent" }, + }, + }, + textGenerators: { + [textGenerationSelection.instanceId]: { + kind: "openaiCompatible", + baseUrl: "http://127.0.0.1:18999/v1", + enabled: true, + }, + }, + textGenerationModelSelection: textGenerationSelection, + speechPostProcessingModelSelection: acpSelection, + }), + ), + Layer.provide(NodeServices.layer), + ); + yield* Effect.gen(function* () { + const postProcessing = yield* SpeechPostProcessing.SpeechPostProcessing; + yield* postProcessing.process({ + options: { + speechCustomWords: [], + speechPostProcessingPrompt: { mode: "default", customInstructions: "" }, + }, + transcript: "um hello world", + draft: { text: "", selection: { start: 0, end: 0 } }, + } as never); + }).pipe(Effect.provide(layer)); + const selection = yield* Ref.get(used); + assert.deepStrictEqual( + { instanceId: selection?.instanceId, model: selection?.model }, + textGenerationSelection, + ); + }), +); diff --git a/apps/server/src/speech/postProcessing.ts b/apps/server/src/speech/postProcessing.ts index 3208afd057287..92f4af17696c3 100644 --- a/apps/server/src/speech/postProcessing.ts +++ b/apps/server/src/speech/postProcessing.ts @@ -8,6 +8,7 @@ import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import { resolveSpeechPostProcessingModelSelection } from "@t3tools/shared/serverSettings"; +import * as ProviderRegistry from "../provider/ProviderRegistry.ts"; import * as ServerSettings from "../serverSettings.ts"; import * as TextGeneration from "../textGeneration/TextGeneration.ts"; import { buildTranscriptionPostProcessingPrompt } from "../textGeneration/TranscriptionPostProcessing.ts"; @@ -27,11 +28,13 @@ const make = Effect.gen(function* () { const settingsService = yield* ServerSettings.ServerSettingsService; const fileSystem = yield* FileSystem.FileSystem; const textGeneration = yield* TextGeneration.TextGeneration; + const providerRegistry = yield* ProviderRegistry.ProviderRegistry; return SpeechPostProcessing.of({ process: Effect.fn("SpeechPostProcessing.process")( function* (input) { if (input.transcript.trim().length === 0) return { text: input.transcript }; const settings = yield* settingsService.getSettings; + const providers = yield* providerRegistry.getProviders; const { mode, customInstructions } = input.options.speechPostProcessingPrompt; const instructions = (mode === "custom" ? customInstructions.trim() : "") || @@ -46,7 +49,7 @@ const make = Effect.gen(function* () { input.draft, input.options.speechCorrectionWord, ), - modelSelection: resolveSpeechPostProcessingModelSelection(settings), + modelSelection: resolveSpeechPostProcessingModelSelection(settings, providers), }); const text = generated.transcription.trim(); return { diff --git a/apps/server/src/startupAccess.test.ts b/apps/server/src/startupAccess.test.ts index a48bb5263f8ec..159778b3c7e03 100644 --- a/apps/server/src/startupAccess.test.ts +++ b/apps/server/src/startupAccess.test.ts @@ -21,6 +21,7 @@ const ipv4 = (address: string, internal = false): NodeOS.NetworkInterfaceInfo => it("resolves wildcard hosts to a concrete external interface when one is available", () => { const connectionString = resolveHeadlessConnectionString("0.0.0.0", 3773, { interfaces: { en0: [ipv4("192.168.1.42")], lo0: [ipv4("127.0.0.1", true)] }, + platform: "linux", }); expect(connectionString).toBe("http://192.168.1.42:3773"); @@ -34,16 +35,21 @@ it("skips public and container addresses for a private-network listener", () => eth1: [ipv4("10.0.0.5")], }; - expect(resolveHeadlessConnectionString("0.0.0.0", 3773, { interfaces })).toBe( + expect(resolveHeadlessConnectionString("0.0.0.0", 3773, { interfaces, platform: "linux" })).toBe( "http://203.0.113.7:3773", ); expect( - resolveHeadlessConnectionString("0.0.0.0", 3773, { interfaces, privateNetworkOnly: true }), + resolveHeadlessConnectionString("0.0.0.0", 3773, { + interfaces, + privateNetworkOnly: true, + platform: "linux", + }), ).toBe("http://10.0.0.5:3773"); expect( resolveHeadlessConnectionString("0.0.0.0", 3773, { interfaces: { eth0: [ipv4("203.0.113.7")] }, privateNetworkOnly: true, + platform: "linux", }), ).toBe("http://localhost:3773"); }); @@ -53,10 +59,21 @@ it("prefers a LAN address over a tailnet one for a private-network pairing URL", resolveHeadlessConnectionString("0.0.0.0", 3773, { interfaces: { tailscale0: [ipv4("100.101.102.103")], en0: [ipv4("192.168.1.42")] }, privateNetworkOnly: true, + platform: "linux", }), ).toBe("http://192.168.1.42:3773"); }); +it("skips NixOS container and systemd-nspawn veths for a private-network pairing URL", () => { + expect( + resolveHeadlessConnectionString("0.0.0.0", 3773, { + interfaces: { "ve-jellyfin": [ipv4("192.168.53.193")], eth0: [ipv4("192.168.0.18")] }, + privateNetworkOnly: true, + platform: "linux", + }), + ).toBe("http://192.168.0.18:3773"); +}); + it("targets the bound interface when the listener binds a specific address", () => { const hostFor = (ip: string) => loopbackUrlHostForAddress(NetAddress.inetAddressFromIpStringUnsafe(ip, 3773)); diff --git a/apps/server/src/startupAccess.ts b/apps/server/src/startupAccess.ts index f393faecebf91..1720300bd7beb 100644 --- a/apps/server/src/startupAccess.ts +++ b/apps/server/src/startupAccess.ts @@ -7,6 +7,7 @@ import { isSharedAddressSpaceHost, isWildcardHost, } from "@t3tools/shared/hostClassification"; +import * as HostProcess from "@t3tools/shared/HostProcess"; import { QrCode } from "@t3tools/shared/qrCode"; import { buildPairingAppLink } from "@t3tools/shared/remote"; import * as Effect from "effect/Effect"; @@ -44,9 +45,19 @@ export const isLoopbackHost = (host: string | undefined): boolean => { * host bridge such as `br0` or Proxmox's `vmbr0` carries the LAN address, so * it stays listed. */ -export const VIRTUAL_INTERFACE = +const VIRTUAL_INTERFACE = /^(docker|br-|veth|virbr|vmnet|vboxnet|vEthernet|podman|cni|flannel|cali|lxcbr|lxdbr|bridge[1-9]\d\d)/; +/** + * systemd-nspawn's `ve-*`/`vz-*` and NixOS containers' `vb-*` name host-side + * veths after the machine. Windows aliases are user-renameable, so these + * prefixes only apply to Linux interface discovery. + */ +const LINUX_CONTAINER_INTERFACE = /^(ve-|vb-|vz-)/; + +export const isVirtualInterface = (name: string, platform: NodeJS.Platform): boolean => + VIRTUAL_INTERFACE.test(name) || (platform === "linux" && LINUX_CONTAINER_INTERFACE.test(name)); + /** * Loopback or private-range IPv4 (LAN, tailnet, CGNAT), not link-local, in * plain or IPv4-mapped IPv6 form. @@ -86,6 +97,7 @@ const resolveHeadlessConnectionHost = ( host: string | undefined, interfaces: NetworkInterfacesMap, privateNetworkOnly: boolean, + platform: NodeJS.Platform, ): string => { if (!host) { return "localhost"; @@ -96,7 +108,7 @@ const resolveHeadlessConnectionHost = ( } const interfaceEntries = Object.entries(interfaces).flatMap(([name, entries]) => - privateNetworkOnly && VIRTUAL_INTERFACE.test(name) ? [] : (entries ?? []), + privateNetworkOnly && isVirtualInterface(name, platform) ? [] : (entries ?? []), ); const externalIpv4Entries = interfaceEntries.filter( (entry) => @@ -128,12 +140,14 @@ export const resolveHeadlessConnectionString = ( options: { readonly interfaces?: NetworkInterfacesMap; readonly privateNetworkOnly?: boolean | undefined; - } = {}, + readonly platform: NodeJS.Platform; + }, ): string => { const connectionHost = resolveHeadlessConnectionHost( host, options.interfaces ?? NodeOS.networkInterfaces(), options.privateNetworkOnly === true, + options.platform, ); return `http://${formatHostForUrl(connectionHost)}:${port}`; }; @@ -196,10 +210,11 @@ export const issueHeadlessServeAccessInfo = Effect.fn("issueHeadlessServeAccessI const serverConfig = yield* ServerConfig.ServerConfig; const httpServer = yield* HttpServer.HttpServer; const serverAuth = yield* EnvironmentAuth.EnvironmentAuth; + const platform = yield* HostProcess.Platform; const connectionString = resolveHeadlessConnectionString( serverConfig.host, resolveListeningPort(httpServer.address, serverConfig.port), - { privateNetworkOnly: serverConfig.privateNetworkOnly }, + { privateNetworkOnly: serverConfig.privateNetworkOnly, platform }, ); const issued = yield* serverAuth.issueStartupPairingCredential(); diff --git a/apps/server/src/storageCleanup.test.ts b/apps/server/src/storageCleanup.test.ts index 2a98c2ac399e6..cf56bde07f214 100644 --- a/apps/server/src/storageCleanup.test.ts +++ b/apps/server/src/storageCleanup.test.ts @@ -2,6 +2,8 @@ import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; import * as NodeServices from "@effect/platform-node/NodeServices"; import * as Effect from "effect/Effect"; import * as Context from "effect/Context"; +import type * as Digest from "effect/crypto/Digest"; +import * as Base64Url from "effect/encoding/Base64Url"; import * as Deferred from "effect/Deferred"; import * as Fiber from "effect/Fiber"; import * as FileSystem from "effect/FileSystem"; @@ -14,6 +16,8 @@ import * as Stream from "effect/Stream"; import { SCAN_CACHE_FILE_NAME } from "./usage/usageScanCache.ts"; import { TestClock } from "effect/testing"; import * as SqlClient from "effect/sql/SqlClient"; +import * as Sqlite from "./persistence/Sqlite.ts"; +import { checkpointRefForScopeOrdinal } from "./orchestration-v2/CheckpointService.ts"; import { DEFAULT_SERVER_SETTINGS, type WorktreeKeepWhen, @@ -26,17 +30,20 @@ import * as ServerConfig from "./config.ts"; import * as StorageCleanup from "./storageCleanup.ts"; import * as GitVcsDriver from "./vcs/GitVcsDriver.ts"; import * as GitManager from "./git/GitManager.ts"; +import * as GitWorkflowService from "./git/GitWorkflowService.ts"; import * as Settings from "./serverSettings.ts"; import * as ProjectionStore from "./orchestration-v2/ProjectionStore.ts"; import * as ProviderSessionManager from "./orchestration-v2/ProviderSessionManager.ts"; import * as ProjectStore from "./orchestration-v2/ProjectStore.ts"; import * as Orchestrator from "./orchestration-v2/Orchestrator.ts"; +import * as ThreadManagement from "./orchestration-v2/ThreadManagementService.ts"; import * as TerminalManager from "./terminal/Manager.ts"; import { CodexProviderCapabilitiesV2 } from "./orchestration-v2/Adapters/CodexAdapterV2.ts"; import { ServerActivation } from "./serverActivation.ts"; import { withWorkspaceLease } from "./workspace/workspaceLease.ts"; import { describe, expect, it } from "@effect/vitest"; import { + CheckpointScopeId, EventId, ProjectId, ProviderDriverKind, @@ -46,6 +53,7 @@ import { RuntimeRequestId, ThreadId, type OrchestrationV2DomainEvent, + type OrchestrationV2ServerCommand, type OrchestrationV2ThreadShell, } from "@t3tools/contracts"; import * as DateTime from "effect/DateTime"; @@ -325,6 +333,7 @@ const cleanupFixture = ( worktreeAfterDays: 8, worktreeOnDelete: true, }, + sqlOverride?: SqlClient.SqlClient, ) => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; @@ -427,6 +436,31 @@ const cleanupFixture = ( getThreadShell: (threadId: ThreadId) => Effect.sync(() => threads.find((thread) => thread.id === threadId) ?? null), } as unknown as ProjectionStore.ProjectionStoreV2["Service"]), + Effect.provideService(ThreadManagement.ThreadManagementService, { + getShellSnapshot: (input?: { threadIds?: ReadonlyArray }) => + Effect.sync(() => ({ + threads: threads.filter((thread) => input?.threadIds?.includes(thread.id) ?? true), + archivedThreads: [], + })), + dispatch: (command: OrchestrationV2ServerCommand) => + Effect.sync(() => { + if (command.type === "thread.metadata.update" && command.branch != null) { + const branch = command.branch; + threads = threads.map((thread) => + thread.id === command.threadId && thread.branch === command.expectedBranch + ? { ...thread, branch } + : thread, + ); + } + return { sequence: 0, storedEvents: [] }; + }), + } as unknown as ThreadManagement.ThreadManagementService["Service"]), + Effect.provideService(GitWorkflowService.GitWorkflowService, { + currentBranch: (cwd: string) => + command(cwd, ["branch", "--show-current"]).pipe( + Effect.map((result) => result.stdout.trim() || null), + ), + } as unknown as GitWorkflowService.GitWorkflowService["Service"]), Effect.provideService(ProviderSessionManager.ProviderSessionManagerV2, { isLive: () => Effect.sync(() => { @@ -437,41 +471,49 @@ const cleanupFixture = ( Effect.provideService(Orchestrator.OrchestratorV2, { streamDomainEvents: Stream.suspend(() => events), } as unknown as Orchestrator.OrchestratorV2["Service"]), - Effect.provideService(SqlClient.SqlClient, ((strings: TemplateStringsArray) => - Effect.sync(() => - openSessionCwd !== null && strings.join("").includes("projection_provider_sessions") - ? [ - { - payload_json: JSON.stringify({ - id: "session-1", - driver: "codex", - providerInstanceId: "codex", - status: "ready", - cwd: openSessionCwd, - model: null, - capabilities: CodexProviderCapabilitiesV2, - createdAt: "2026-06-01T12:00:00.000Z", - updatedAt: "2026-06-01T12:00:00.000Z", - lastError: null, - }), - }, - ] - : outboxStatus !== null && strings.join("").includes("orchestration_v2_effect_outbox") - ? [{ status: outboxStatus }] - : strings.join("").includes("deleted_at IS NOT NULL") - ? deletedThreads.map((thread) => ({ - payload_json: JSON.stringify(thread), - workspaceRoot: repo, - })) - : strings.join("").includes('AS "worktreePath"') + Effect.provideService( + SqlClient.SqlClient, + sqlOverride ?? + (((strings: TemplateStringsArray) => + Effect.sync(() => + strings.join("").includes("retained_checkpoint_threads") + ? [] + : openSessionCwd !== null && + strings.join("").includes("projection_provider_sessions") ? [ - ...threads.flatMap((thread) => thread.worktreePath ?? []), - ...storedWorktreePaths, - ].map((worktreePath) => ({ worktreePath })) - : strings.join("").includes("scheduled_tasks") - ? scheduledThreadIds.map((threadId) => ({ threadId })) - : [], - )) as unknown as SqlClient.SqlClient), + { + payload_json: JSON.stringify({ + id: "session-1", + driver: "codex", + providerInstanceId: "codex", + status: "ready", + cwd: openSessionCwd, + model: null, + capabilities: CodexProviderCapabilitiesV2, + createdAt: "2026-06-01T12:00:00.000Z", + updatedAt: "2026-06-01T12:00:00.000Z", + lastError: null, + }), + }, + ] + : outboxStatus !== null && + strings.join("").includes("orchestration_v2_effect_outbox") + ? [{ status: outboxStatus }] + : strings.join("").includes("deleted_at IS NOT NULL") + ? deletedThreads.map((thread) => ({ + payload_json: JSON.stringify(thread), + workspaceRoot: repo, + })) + : strings.join("").includes('AS "worktreePath"') + ? [ + ...threads.flatMap((thread) => thread.worktreePath ?? []), + ...storedWorktreePaths, + ].map((worktreePath) => ({ worktreePath })) + : strings.join("").includes("scheduled_tasks") + ? scheduledThreadIds.map((threadId) => ({ threadId })) + : [], + )) as unknown as SqlClient.SqlClient), + ), Effect.provideService(GitManager.GitManager, { invalidateStatus: () => Effect.void, } as unknown as GitManager.GitManager["Service"]), @@ -530,6 +572,7 @@ const cleanupFixture = ( setThreads: (next: typeof threads) => { threads = next; }, + threadBranches: () => threads.map((thread) => thread.branch), setOnLiveCheck: (hook: Effect.Effect) => { onLiveCheck = hook; }, @@ -606,11 +649,21 @@ const cleanupTestLayer = GitVcsDriver.layer.pipe( Layer.provideMerge(ServerConfig.layerTest(process.cwd(), { prefix: "storage-cleanup-" })), Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), ); +const annexWorktree = Effect.fnUntraced(function* ( + fixture: Effect.Success>, + worktree: string, +) { + const adminDir = (yield* fixture.command(worktree, ["rev-parse", "--absolute-git-dir"])).stdout; + yield* fixture.fs.remove(`${worktree}/.git`); + yield* fixture.fs.symlink(adminDir.trim(), `${worktree}/.git`); +}); + const runCleanupTest = ( effect: Effect.Effect< A, E, | FileSystem.FileSystem + | Digest.Digest | ServerConfig.ServerConfig | GitVcsDriver.GitVcsDriver | Path.Path @@ -618,6 +671,182 @@ const runCleanupTest = ( >, ) => effect.pipe(Effect.provide(cleanupTestLayer)); +const legacyCheckpointRef = (threadId: string, turn: number) => + `refs/t3/checkpoints/${Base64Url.encode(threadId)}/turn/${turn}`; + +const checkpointCleanupFixture = Effect.fn("test.checkpointCleanupFixture")(function* () { + const sql = yield* SqlClient.SqlClient; + const git = yield* GitVcsDriver.GitVcsDriver; + const fixture = yield* cleanupFixture(DEFAULT_SERVER_SETTINGS.storageCleanup, sql); + const { repo, worktree, command } = fixture; + const timestamp = DateTime.formatIso(at(0)); + yield* sql`INSERT INTO projection_projects ${sql.insert({ + project_id: "project-1", + title: "Checkpoint cleanup", + workspace_root: repo, + scripts_json: "[]", + created_at: timestamp, + updated_at: timestamp, + deleted_at: timestamp, + })}`; + const addThread = Effect.fn("test.addCheckpointThread")(function* ( + id: string, + options: { deleted?: boolean; archived?: boolean; source?: string; legacyOnly?: boolean } = {}, + ) { + const scopeId = CheckpointScopeId.make(`scope:${id}`); + yield* sql`INSERT INTO orchestration_v2_projection_threads ${sql.insert({ + thread_id: id, + project_id: "project-1", + title: id, + default_provider: "codex", + runtime_mode: "full-access", + interaction_mode: "default", + created_at: timestamp, + updated_at: timestamp, + archived_at: options.archived ? timestamp : null, + deleted_at: options.deleted ? timestamp : null, + payload_json: JSON.stringify({ + worktreePath: worktree, + forkedFrom: options.source ? { type: "run", threadId: options.source } : null, + }), + })}`; + if (!options.legacyOnly) + yield* sql`INSERT INTO orchestration_v2_projection_checkpoint_scopes ${sql.insert({ + scope_id: scopeId, + thread_id: id, + node_id: `node:${id}`, + kind: "root_run", + ordinal_within_parent: 0, + advances_app_run_count: 1, + created_at: timestamp, + payload_json: JSON.stringify({ cwd: worktree }), + })}`; + const refs: string[] = options.legacyOnly + ? [legacyCheckpointRef(id, 0)] + : [ + yield* checkpointRefForScopeOrdinal({ scopeId, ordinalWithinScope: 0 }), + yield* checkpointRefForScopeOrdinal({ scopeId, ordinalWithinScope: 1 }), + legacyCheckpointRef(id, 0), + ]; + for (const ref of refs) yield* command(repo, ["update-ref", ref, "HEAD"]); + return refs; + }); + const listRefs = () => + git + .execute({ + operation: "cleanup-test.list-checkpoints", + cwd: repo, + args: ["for-each-ref", "--format=%(refname)", "refs/t3/"], + maxOutputBytes: 16 * 1024 * 1024, + }) + .pipe(Effect.map(({ stdout }) => stdout.trim().split("\n").filter(Boolean).sort())); + return { ...fixture, sql, addThread, listRefs }; +}); + +describe("checkpoint ref cleanup", () => { + it.live("prunes refs owned by deleted threads after their worktree is removed", () => + runCleanupTest( + Effect.gen(function* () { + const { service, repo, worktree, command, addThread, listRefs } = + yield* checkpointCleanupFixture(); + yield* addThread("deleted", { deleted: true }); + yield* addThread("imported-deleted", { deleted: true, legacyOnly: true }); + const retained = [ + ...(yield* addThread("active")), + ...(yield* addThread("archived", { archived: true })), + legacyCheckpointRef("unknown-owner", 0), + ]; + yield* command(repo, ["update-ref", retained.at(-1)!, "HEAD"]); + yield* command(repo, ["pack-refs", "--all"]); + yield* command(repo, ["worktree", "remove", worktree]); + yield* service.runNow; + expect(yield* listRefs()).toEqual(retained.sort()); + }).pipe(Effect.provide(Sqlite.layerMemory)), + ), + ); + + it.live("keeps deleted fork ancestry while a live or archived descendant needs it", () => + runCleanupTest( + Effect.gen(function* () { + const { service, sql, addThread, listRefs } = yield* checkpointCleanupFixture(); + const root = yield* addThread("root", { deleted: true }); + const intermediate = yield* addThread("intermediate", { deleted: true, source: "root" }); + const child = yield* addThread("child", { archived: true, source: "intermediate" }); + yield* service.runNow; + expect(yield* listRefs()).toEqual([...root, ...intermediate, ...child].sort()); + yield* sql`UPDATE orchestration_v2_projection_threads SET deleted_at = 'deleted' WHERE thread_id = 'child'`; + yield* service.runNow; + expect(yield* listRefs()).toEqual([]); + }).pipe(Effect.provide(Sqlite.layerMemory)), + ), + ); + + it.live("waits for a deleted thread's pending checkpoint effects", () => + runCleanupTest( + Effect.gen(function* () { + const { service, sql, addThread, listRefs } = yield* checkpointCleanupFixture(); + const refs = yield* addThread("capturing", { deleted: true }); + yield* sql`INSERT INTO orchestration_v2_effect_outbox ${sql.insert({ + effect_id: "capture", + command_id: "capture", + thread_id: "capturing", + effect_type: "checkpoint.capture", + payload_json: "{}", + status: "running", + available_at: "2026-01-01T00:00:00Z", + created_at: "2026-01-01T00:00:00Z", + updated_at: "2026-01-01T00:00:00Z", + })}`; + yield* service.runNow; + expect(yield* listRefs()).toEqual(refs.toSorted()); + yield* sql`UPDATE orchestration_v2_effect_outbox SET status = 'succeeded'`; + yield* service.runNow; + expect(yield* listRefs()).toEqual([]); + }).pipe(Effect.provide(Sqlite.layerMemory)), + ), + ); + + it.live("deletes a symbolic checkpoint ref without its target or neighbouring refs", () => + runCleanupTest( + Effect.gen(function* () { + const { service, repo, command, addThread, listRefs } = yield* checkpointCleanupFixture(); + const refs = yield* addThread("deleted", { deleted: true }); + yield* command(repo, ["symbolic-ref", refs[0]!, "refs/heads/main"]); + const retained = [ + "refs/t3/unrelated/0", + `${refs[1]}-other`, + refs[1]!.replace(/1$/, "2/nested"), + ]; + for (const ref of retained) yield* command(repo, ["update-ref", ref, "HEAD"]); + yield* service.runNow; + expect(yield* listRefs()).toEqual(retained.sort()); + yield* command(repo, ["show-ref", "--verify", "refs/heads/main"]); + }).pipe(Effect.provide(Sqlite.layerMemory)), + ), + ); + + it.live("cleans more checkpoint refs than default git output holds", () => + runCleanupTest( + Effect.gen(function* () { + const { service, repo, addThread, listRefs } = yield* checkpointCleanupFixture(); + const git = yield* GitVcsDriver.GitVcsDriver; + const prefix = (yield* addThread("many-checkpoints", { deleted: true }))[0]!.slice(0, -1); + yield* git.execute({ + operation: "cleanup-test.seed-checkpoints", + cwd: repo, + args: ["update-ref", "--stdin"], + stdin: Array.from( + { length: 12_283 }, + (_, ordinal) => `update ${prefix}${ordinal} HEAD\n`, + ).join(""), + }); + yield* service.runNow; + expect(yield* listRefs()).toEqual([]); + }).pipe(Effect.provide(Sqlite.layerMemory)), + ), + ); +}); + it.live.each(["repository", "uppercase", "bare"] as const)( "keeps a worktree holding an ignored nested %s", (kind) => @@ -733,8 +962,26 @@ it.live("keeps an ignored repository inside a registered submodule", () => it.live("removes clean submodules recursively, including deinitialized repositories", () => runCleanupTest( Effect.gen(function* () { - const { service, fs, repo, worktree, submodule, command, recordSubmodules } = - yield* submoduleCleanupFixture(); + const fs = yield* FileSystem.FileSystem; + const git = yield* GitVcsDriver.GitVcsDriver; + let editedDuringClean = false; + const { service, repo, worktree, submodule, command, recordSubmodules, setPolicy } = + yield* submoduleCleanupFixture().pipe( + Effect.provideService(GitVcsDriver.GitVcsDriver, { + ...git, + execute: (input) => + git.execute(input).pipe( + Effect.tap(() => { + if (input.args[0] !== "clean") return Effect.void; + editedDuringClean = true; + return fs.writeFileString(`${input.cwd}/tracked.txt`, "late edit\n"); + }), + ), + }), + ); + setPolicy("tracked-changes"); + yield* fs.writeFileString(`${worktree}/notes.txt`, "untracked\n"); + yield* fs.writeFileString(`${worktree}/.env`, "ignored\n"); yield* command(submodule, [ "-c", "protocol.file.allow=always", @@ -776,8 +1023,22 @@ it.live("removes clean submodules recursively, including deinitialized repositor yield* command(legacy, ["submodule", "deinit", "stored"]); yield* recordSubmodules(); yield* command(submodule, ["submodule", "deinit", "stored"]); + const checkpoint = (yield* command(submodule, [ + "commit-tree", + "HEAD^{tree}", + "-p", + "HEAD", + "-m", + "checkpoint", + ])).stdout.trim(); + yield* command(submodule, [ + "update-ref", + "refs/t3/orchestration-v2/checkpoints/test", + checkpoint, + ]); const admin = (yield* command(worktree, ["rev-parse", "--absolute-git-dir"])).stdout.trim(); const report = yield* service.runNow; + expect(editedDuringClean).toBe(false); expect(report.entries.find((entry) => entry.path === worktree)).toMatchObject({ outcome: "removed", }); @@ -883,15 +1144,21 @@ it.live("keeps unpushed submodule history after checkout and deinitialization", it.live("keeps submodules when stored repository metadata is malformed", () => runCleanupTest( Effect.gen(function* () { - const { service, fs, worktree, submodule, child, command } = yield* submoduleCleanupFixture(); + const { service, fs, worktree, submodule, child, command, setPolicy } = + yield* submoduleCleanupFixture(); + setPolicy("tracked-changes"); const childGit = (yield* command(child, ["rev-parse", "--absolute-git-dir"])).stdout.trim(); yield* command(submodule, ["submodule", "deinit", "child"]); yield* fs.writeFileString(`${childGit}/HEAD`, "not a ref\n"); + yield* fs.writeFileString(`${worktree}/notes.txt`, "keep while the worktree stays\n"); expect( (yield* service.runNow).entries.find((entry) => entry.path === worktree), ).toMatchObject({ outcome: "kept", reason: expect.stringContaining("inspected") }); expect(yield* fs.exists(worktree)).toBe(true); expect(yield* fs.readFileString(`${childGit}/HEAD`)).toBe("not a ref\n"); + expect(yield* fs.readFileString(`${worktree}/notes.txt`)).toBe( + "keep while the worktree stays\n", + ); }), ), ); @@ -1080,6 +1347,8 @@ describe("subagent worktree ownership", () => { it.effect.each([ "idle", "grandchild", + "branchless", + "branchless-running", "null-branch", "stale-branch", "isolated-owner", @@ -1111,7 +1380,7 @@ describe("subagent worktree ownership", () => { }, }); const root = shell({ - branch: "feature", + branch: ["branchless", "branchless-running"].includes(scenario) ? null : "feature", worktreePath: fixture.worktree, settledAt: scenario === "child-settled-only" ? null : at(-20 * DAY_MS), ...(scenario === "isolated-owner" || scenario === "fork-owner" @@ -1153,7 +1422,7 @@ describe("subagent worktree ownership", () => { : "subagent", ...(scenario === "side-chat" ? { sideChat: true } : {}), }, - status: scenario === "running" ? "running" : "completed", + status: ["running", "branchless-running"].includes(scenario) ? "running" : "completed", pendingBackgroundTasks: scenario === "pending" ? [{ taskId: "task", kind: "command" }] : [], latestRunCompletedAt: scenario === "recent-activity" || scenario === "recent-inactive" @@ -1202,7 +1471,7 @@ describe("subagent worktree ownership", () => { outcome: removed ? "removed" : "kept", reason: removed ? "Settled for 20 days" - : scenario === "running" || scenario === "pending" + : ["running", "pending", "branchless-running"].includes(scenario) ? "Thread is running or has pending work" : shared ? "Shared by 2 threads" @@ -1214,7 +1483,9 @@ describe("subagent worktree ownership", () => { ? "Inactive for 2 of 8 days" : ["recent-grace", "recent-settle", "child-settled-now"].includes(scenario) ? "Settled moments ago" - : "Not settled", + : scenario === "branchless" + ? "Thread has no branch" + : "Not settled", }); }).pipe(Effect.provide(cleanupTestLayer)), ); @@ -1370,6 +1641,28 @@ describe("deleted thread worktrees", () => { }).pipe(Effect.provide(cleanupTestLayer)), ); + it.effect("leaves a deleted owner's subagent on the owner's saved branch", () => + Effect.gen(function* () { + yield* TestClock.setTime(NOW_MS); + const fixture = yield* cleanupFixture({ + ...DEFAULT_SERVER_SETTINGS.storageCleanup, + worktreeAfterDays: null, + worktreeOnDelete: true, + }); + const owner = parent(fixture.worktree); + fixture.setDeletedThreads([owner]); + fixture.setThreads([subagent(owner)]); + yield* fixture.command(fixture.worktree, ["switch", "-c", "renamed"]); + expect((yield* fixture.service.runNow).entries[0]).toMatchObject({ + threadId: owner.id, + outcome: "kept", + reason: "Repository or branch changed", + }); + // The deleted owner cannot follow, so moving its subagent would split their records. + expect(fixture.threadBranches()).toEqual(["feature"]); + }).pipe(Effect.provide(cleanupTestLayer), Effect.scoped), + ); + it.effect("applies the remaining subagent's rules when deletion cleanup is off", () => Effect.gen(function* () { yield* TestClock.setTime(NOW_MS); @@ -1815,7 +2108,7 @@ describe("orphan worktree reports", () => { ), ); - it.live.each(["nested", "direct"] as const)( + it.live.each(["nested", "direct", "annexed"] as const)( "reports a linked orphan's size without removing its files (%s)", (location) => runCleanupTest( @@ -1824,8 +2117,13 @@ describe("orphan worktree reports", () => { fixture.setThreads([]); const orphan = `${fixture.config.worktreesDir}/${location === "nested" ? "repository/" : ""}orphan`; yield* fixture.command(fixture.repo, ["worktree", "add", "-b", "orphan", orphan]); - const sizes = yield* Effect.forEach(yield* fixture.fs.readDirectory(orphan), (name) => - allocatedBytes(fixture.fs, `${orphan}/${name}`), + if (location === "annexed") yield* annexWorktree(fixture, orphan); + const sizes = yield* Effect.forEach( + // Links are not measured, and the annexed `.git` is one. + (yield* fixture.fs.readDirectory(orphan)).filter( + (name) => location !== "annexed" || name !== ".git", + ), + (name) => allocatedBytes(fixture.fs, `${orphan}/${name}`), ); const outside = yield* fixture.fs.makeTempDirectoryScoped(); yield* fixture.fs.writeFileString(`${outside}/external`, "outside data"); @@ -2298,7 +2596,8 @@ describe("storage cleanup reports and local file policies", () => { occurredAt: at(0), payload: { ...shell({ deletedAt: at(0) }), lastVisitedAt: null }, }); - expect(fixture.snapshotReads()).toBe(startupReads + 3 * Number(worktreeOnDelete)); + // Deletion always sweeps so the deleted thread's checkpoint refs are pruned. + expect(fixture.snapshotReads()).toBe(startupReads + 2 * Number(worktreeOnDelete) + 1); expect(fixture.sessionLiveChecks()).toBe(worktreeOnDelete ? 6 : 0); }), ), @@ -2678,6 +2977,17 @@ describe("storage cleanup reports and local file policies", () => { }), ), ); + it.live("removes a worktree whose .git file git-annex replaced with a symlink", () => + runCleanupTest( + Effect.gen(function* () { + const fixture = yield* cleanupFixture(); + const { service, worktree, fs } = fixture; + yield* annexWorktree(fixture, worktree); + expect((yield* service.runNow).entries[0]).toMatchObject({ outcome: "removed" }); + expect(yield* fs.exists(worktree)).toBe(false); + }), + ), + ); it.live("keeps a worktree when the inspection cannot read its directories", () => runCleanupTest( Effect.gen(function* () { @@ -2700,6 +3010,36 @@ describe("storage cleanup reports and local file policies", () => { }), ), ); + it.effect("removes a worktree owned by a repository other than the project root", () => + runCleanupTest( + Effect.gen(function* () { + yield* TestClock.setTime(NOW_MS); + const fixture = yield* cleanupFixture(); + const { fs, command, config } = fixture; + const oldRepo = yield* fs.realPath( + yield* fs.makeTempDirectoryScoped({ prefix: "cleanup-old-repo-" }), + ); + yield* command(oldRepo, ["init", "-b", "main"]); + yield* command(oldRepo, ["config", "user.email", "cleanup@example.test"]); + yield* command(oldRepo, ["config", "user.name", "Cleanup test"]); + yield* fs.writeFileString(`${oldRepo}/tracked.txt`, "original\n"); + yield* command(oldRepo, ["add", "."]); + yield* command(oldRepo, ["commit", "-m", "initial"]); + yield* fs.makeDirectory(`${config.worktreesDir}/old-repo`, { recursive: true }); + const oldWorktree = `${config.worktreesDir}/old-repo/feature`; + yield* command(oldRepo, ["worktree", "add", "-b", "feature", oldWorktree]); + fixture.setThreads([shell({ branch: "feature", worktreePath: oldWorktree })]); + const report = yield* fixture.service.runNow; + expect(report.entries.find((entry) => entry.path === oldWorktree)).toMatchObject({ + outcome: "removed", + }); + expect(yield* fs.exists(oldWorktree)).toBe(false); + expect((yield* command(oldRepo, ["worktree", "list", "--porcelain"])).stdout).not.toContain( + oldWorktree, + ); + }), + ), + ); it.effect.each([ [ "its owner's branch is cleared", @@ -2758,6 +3098,20 @@ describe("storage cleanup reports and local file policies", () => { }), ), ); + it.live("follows a branch switched while nothing watched the worktree", () => + runCleanupTest( + Effect.gen(function* () { + const { service, worktree, command, threadBranches } = yield* cleanupFixture(); + yield* command(worktree, ["switch", "-c", "renamed"]); + expect((yield* service.runNow).entries[0]).toMatchObject({ + outcome: "kept", + reason: "Repository or branch changed", + }); + expect(threadBranches()).toEqual(["renamed"]); + expect((yield* service.runNow).entries[0]).toMatchObject({ outcome: "removed" }); + }), + ), + ); it.live("does not measure a worktree kept for an open provider session", () => runCleanupTest( Effect.gen(function* () { @@ -2809,7 +3163,8 @@ describe("storage cleanup reports and local file policies", () => { it.live("compares the checked-out branch without a full status", () => runCleanupTest( Effect.gen(function* () { - const { service, fs, repo, worktree, command, fullStatusCalls } = yield* cleanupFixture(); + const { service, fs, repo, worktree, command, fullStatusCalls, threadBranches } = + yield* cleanupFixture(); const changed = { outcome: "kept", reason: "Repository or branch changed" }; yield* command(worktree, ["checkout", "--detach"]); expect((yield* service.runNow).entries[0]).toMatchObject(changed); @@ -2819,6 +3174,8 @@ describe("storage cleanup reports and local file policies", () => { // Newer Git refuses to create a tag named HEAD, but still reads one. yield* command(worktree, ["update-ref", "refs/tags/HEAD", "refs/heads/feature"]); yield* command(worktree, ["switch", "feature"]); + expect((yield* service.runNow).entries[0]).toMatchObject(changed); + expect(threadBranches()).toEqual(["feature"]); const index = (yield* command(worktree, ["rev-parse", "--git-path", "index"])).stdout; yield* fs.writeFileString(`${index.trim()}.lock`, ""); const locked = (yield* service.runNow).entries[0]; diff --git a/apps/server/src/storageCleanup.ts b/apps/server/src/storageCleanup.ts index 257fef03ae52d..43ff8254daa6d 100644 --- a/apps/server/src/storageCleanup.ts +++ b/apps/server/src/storageCleanup.ts @@ -1,10 +1,12 @@ // @effect-diagnostics nodeBuiltinImport:off import * as NodeFSP from "node:fs/promises"; import { + CheckpointScopeId, GitCommandError, OrchestrationV2AppThreadJson, OrchestrationV2ProviderSessionJson, } from "@t3tools/contracts"; +import * as Base64Url from "effect/encoding/Base64Url"; import * as Schema from "effect/Schema"; import * as SqlClient from "effect/sql/SqlClient"; import type { @@ -40,20 +42,23 @@ import { parse as parseYaml } from "yaml"; import * as ServerConfig from "./config.ts"; import * as GitManager from "./git/GitManager.ts"; +import * as GitWorkflowService from "./git/GitWorkflowService.ts"; import * as ProjectStore from "./orchestration-v2/ProjectStore.ts"; import { scratchFolderRoot } from "./project/ManagedProjectFolders.ts"; import * as Orchestrator from "./orchestration-v2/Orchestrator.ts"; import * as ProjectionStore from "./orchestration-v2/ProjectionStore.ts"; import * as ProviderSessionManager from "./orchestration-v2/ProviderSessionManager.ts"; +import { checkpointRefForScopeOrdinal } from "./orchestration-v2/CheckpointService.ts"; import { threadHasQueuedTurnStart } from "./orchestration-v2/ThreadSettlementService.ts"; import { MAX_OVERDUE_ARM_MS } from "./orchestration-v2/UsageLimitRecoveryWorker.ts"; +import * as WorktreeBranchSync from "./orchestration-v2/WorktreeBranchSync.ts"; import { forkParked } from "./serverActivation.ts"; import * as Settings from "./serverSettings.ts"; import * as TerminalManager from "./terminal/Manager.ts"; import { isFilesystemRoot, managedWorktreesDirectories } from "./worktreesDirectory.ts"; import * as GitVcsDriver from "./vcs/GitVcsDriver.ts"; import { isNonRepositoryGitStderr } from "./vcs/GitVcsDriverCore.ts"; -import { removeLinkedWorktree } from "./vcs/worktreeRemoval.ts"; +import { isLinkedWorktree, removeLinkedWorktree } from "./vcs/worktreeRemoval.ts"; import { withWorkspaceLease } from "./workspace/workspaceLease.ts"; import * as HostProcess from "@t3tools/shared/HostProcess"; import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; @@ -321,6 +326,8 @@ const make = Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; const git = yield* GitVcsDriver.GitVcsDriver; const gitManager = yield* GitManager.GitManager; + const gitWorkflow = yield* GitWorkflowService.GitWorkflowService; + const reconcileBranch = yield* WorktreeBranchSync.makeReconcile; const terminals = yield* TerminalManager.TerminalManager; const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; @@ -584,7 +591,7 @@ const make = Effect.gen(function* () { const modified = Option.getOrNull((yield* fs.stat(worktreePath)).mtime); if (modified !== null && modified.getTime() > now - ORPHAN_WORKTREE_GRACE_MS) return; const gitPath = path.join(worktreePath, ".git"); - const linked = (yield* fs.exists(gitPath)) && (yield* fs.stat(gitPath)).type === "File"; + const linked = (yield* fs.exists(gitPath)) && (yield* isLinkedWorktree(worktreePath)); reported.add(key); const remaining = measurementDeadline - (yield* Clock.currentTimeMillis); if (!orphanSizes.has(key) && (firstMeasurement || remaining > 0)) { @@ -629,7 +636,7 @@ const make = Effect.gen(function* () { (worktree) => Effect.gen(function* () { if (pathKey(yield* fs.realPath(worktree)) !== pathKey(worktree)) return false; - return (yield* fs.stat(path.join(worktree, ".git"))).type === "File"; + return yield* isLinkedWorktree(worktree); }).pipe(Effect.orElseSucceed(() => false)), ); }); @@ -640,7 +647,7 @@ const make = Effect.gen(function* () { if (referenced.has(pathKey(projectDirectory))) return; const gitPath = path.join(projectDirectory, ".git"); if (yield* fs.exists(gitPath)) { - if ((yield* fs.stat(gitPath)).type === "File") yield* report(projectDirectory); + if (yield* isLinkedWorktree(projectDirectory)) yield* report(projectDirectory); return; } for (const worktreePath of yield* directories(projectDirectory, root)) { @@ -707,6 +714,21 @@ const make = Effect.gen(function* () { return name !== branch; }); + // Nothing watches an idle worktree, so its threads still record the branch it left. + // Record the checked-out one so the next sweep can apply the rules to it. + const followBranch = Effect.fn("StorageCleanup.followBranch")( + function* (worktreePath: string) { + const cwd = yield* fs.realPath(worktreePath); + yield* reconcileBranch({ cwd, refName: yield* gitWorkflow.currentBranch(cwd) }); + }, + (effect, worktreePath) => + effect.pipe( + Effect.catchCause((cause) => + Effect.logWarning("Failed to reconcile worktree branch", { cwd: worktreePath, cause }), + ), + ), + ); + // Git status and non-forced worktree removal both skip assume-unchanged and skip-worktree files. const hiddenTrackedChange = Effect.fn("StorageCleanup.hiddenTrackedChange")(function* ( cwd: string, @@ -800,6 +822,21 @@ const make = Effect.gen(function* () { return changed === undefined ? null : reason(changed.file); }); + // A worktree can outlive its project moving to another clone. Git removes a + // worktree through the repository that registered it, not through the + // project's current checkout, so resolve the owning repository first. + const worktreeRepository = Effect.fn("StorageCleanup.worktreeRepository")(function* ( + worktreePath: string, + ) { + const commonDir = yield* git.execute({ + operation: "StorageCleanup.worktreeRepository", + cwd: worktreePath, + args: ["rev-parse", "--git-common-dir"], + }); + const resolved = path.resolve(worktreePath, commonDir.stdout.trim()); + return path.basename(resolved) === ".git" ? path.dirname(resolved) : resolved; + }); + const localChangesReason = Effect.fn("StorageCleanup.localChangesReason")(function* ( cwd: string, rules: WorktreeCleanupRules, @@ -897,6 +934,8 @@ const make = Effect.gen(function* () { "rev-list", "--max-count=1", "HEAD", + // Checkpoint refs hold workspace snapshots, not commits a user could lose. + "--exclude=refs/t3/*", "--all", "--reflog", "--not", @@ -1045,10 +1084,10 @@ const make = Effect.gen(function* () { return "worktree is a symbolic link"; if (!roots.some((root) => inside(root, realPath))) return "outside the managed worktree folder"; - const gitLink = yield* fs - .stat(path.join(worktreePath, ".git")) - .pipe(Effect.catchReason("PlatformError", "NotFound", () => Effect.undefined)); - if (gitLink?.type !== "File") return "not a linked worktree"; + const linked = yield* isLinkedWorktree(worktreePath).pipe( + Effect.catchReason("PlatformError", "NotFound", () => Effect.succeed(false)), + ); + if (!linked) return "not a linked worktree"; return null; }); const deletedRows = yield* sql<{ payload_json: string; workspaceRoot: string }>` @@ -1186,11 +1225,9 @@ const make = Effect.gen(function* () { if (members.length > 1 && worktreeOwner(members) === undefined) return keep(`shared by ${members.length} threads`); if (project === undefined) return keep("project is unavailable"); - if ( - (thread.branch === null && !scratch) || - !group.every((member) => storageCleanupThreadIdle(member, now, serverSettings)) - ) + if (!group.every((member) => storageCleanupThreadIdle(member, now, serverSettings))) return keep(waitForStop("thread is running or has pending work")); + if (thread.branch === null && !scratch) return keep("thread has no branch"); if (group.some((member) => member.branch !== thread.branch)) return keep("a subagent thread's branch differs from this worktree's branch"); if (yield* hasTerminal(worktreePaths)) return keep(waitForStop("open terminal")); @@ -1390,8 +1427,11 @@ const make = Effect.gen(function* () { if (pathReason !== null) return keep(pathReason); if (yield* containsProjectRoot(worktreePaths, [project, ...snapshot.projects])) return keep("contains a project checkout"); - if (yield* branchChanged(worktreePath, thread.branch)) + if (yield* branchChanged(worktreePath, thread.branch)) { + // A deleted owner cannot follow, so its remaining subagents stay on its branch. + if (!deleted) yield* followBranch(worktreePath); return keep("repository or branch changed"); + } const changes = yield* localChangesReason(worktreePath, settings); if (changes !== null) return keep(changes); const head = yield* git.resolveCommit({ cwd: worktreePath, revision: "HEAD" }); @@ -1519,23 +1559,24 @@ const make = Effect.gen(function* () { const finalChanges = yield* localChangesReason(worktreePath, settings); if (finalChanges !== null) return keep(finalChanges); if (yield* settingsChangedSinceCheck) return keep("settings changed since check"); - // Clean only untracked files. Ordinary worktrees retain Git's final tracked-edit check. - if (settings.worktreeKeepWhen === "tracked-changes") - yield* cleanupGit(worktreePath, ["clean", "-ffdx"], "git clean"); const submodules = yield* submoduleRemoval(worktreePath, settings); if (submodules.reason !== null) return keep(submodules.reason); if (submodules.force) { const changes = yield* localChangesReason(worktreePath, settings); if (changes !== null) return keep(changes); } + const repository = yield* worktreeRepository(worktreePath); + // Forced removal already discards local files; do not widen its final-check window. + if (settings.worktreeKeepWhen === "tracked-changes" && !submodules.force) + yield* cleanupGit(worktreePath, ["clean", "-ffdx"], "git clean"); yield* removeLinkedWorktree({ operation: "StorageCleanup.cleanupGit", - cwd: project.workspaceRoot, + cwd: repository, worktreePath, // Git requires force for submodules; recursive checks above cannot make removal atomic. force: submodules.force, execute: git.execute, - run: (args) => cleanupGit(project.workspaceRoot, [...args], "git worktree remove"), + run: (args) => cleanupGit(repository, [...args], "git worktree remove"), }); entries.push({ ...entry, outcome: "removed", reason: sentence(removalReason), bytes }); yield* gitManager.invalidateStatus(project.workspaceRoot); @@ -1608,6 +1649,115 @@ const make = Effect.gen(function* () { }); const reportRef = yield* SubscriptionRef.make(null); + const cleanCheckpointRefs = Effect.fn("StorageCleanup.cleanCheckpointRefs")(function* () { + // Forks display their source history even after its thread is deleted. Keep + // that ancestry, including deleted intermediate forks and archived children. + const rows = yield* sql<{ + threadId: string; + worktreePath: string | null; + workspaceRoot: string | null; + scopeId: string | null; + cwd: string | null; + }>` + WITH RECURSIVE retained_checkpoint_threads(thread_id) AS ( + SELECT thread_id FROM orchestration_v2_projection_threads WHERE deleted_at IS NULL + UNION + SELECT json_extract(t.payload_json, '$.forkedFrom.threadId') + FROM orchestration_v2_projection_threads t + JOIN retained_checkpoint_threads retained ON retained.thread_id = t.thread_id + WHERE json_extract(t.payload_json, '$.forkedFrom.type') = 'run' + ) + SELECT t.thread_id AS "threadId", + json_extract(t.payload_json, '$.worktreePath') AS "worktreePath", + p.workspace_root AS "workspaceRoot", s.scope_id AS "scopeId", + json_extract(s.payload_json, '$.cwd') AS cwd + FROM orchestration_v2_projection_threads t + LEFT JOIN projection_projects p ON p.project_id = t.project_id + LEFT JOIN orchestration_v2_projection_checkpoint_scopes s ON s.thread_id = t.thread_id + WHERE t.deleted_at IS NOT NULL + AND NOT EXISTS (SELECT 1 FROM retained_checkpoint_threads WHERE thread_id = t.thread_id) + AND NOT EXISTS ( + SELECT 1 FROM orchestration_v2_effect_outbox e + WHERE e.thread_id = t.thread_id AND e.status IN ('pending', 'running') + ) + `.pipe(NodeSqliteClient.readOnly); + const targets = new Map>(); + for (const row of rows) { + // Derive ownership from tombstones, never from absence in this database: + // another T3 home can own refs in the same Git repository. + const prefixes = [`refs/t3/checkpoints/${Base64Url.encode(row.threadId)}/turn/`]; + if (row.scopeId !== null) { + prefixes.push( + (yield* checkpointRefForScopeOrdinal({ + scopeId: CheckpointScopeId.make(row.scopeId), + ordinalWithinScope: 0, + })).slice(0, -1), + ); + } + // A worktree may already be gone; its refs live in the shared repository. + for (const cwd of [row.cwd, row.worktreePath, row.workspaceRoot]) { + if (cwd === null) continue; + const key = path.resolve(cwd); + const owned = targets.get(key) ?? new Set(); + for (const prefix of prefixes) owned.add(prefix); + targets.set(key, owned); + } + } + const repositories = new Map }>(); + for (const [cwd, prefixes] of targets) { + yield* Effect.gen(function* () { + if (!(yield* fs.exists(cwd))) return; + const result = yield* git.execute({ + operation: "StorageCleanup.checkpointRepository", + cwd, + args: ["rev-parse", "--path-format=absolute", "--git-common-dir"], + allowNonZeroExit: true, + }); + if (result.exitCode !== 0) return; + const commonDir = yield* fs.realPath(result.stdout.trim()); + const repository = repositories.get(commonDir) ?? { cwd, prefixes: new Set() }; + for (const prefix of prefixes) repository.prefixes.add(prefix); + repositories.set(commonDir, repository); + }).pipe( + Effect.catch((cause) => + Effect.logWarning("checkpoint repository inspection failed", { cause }), + ), + ); + } + for (const { cwd, prefixes } of repositories.values()) { + yield* Effect.gen(function* () { + const result = yield* git.execute({ + operation: "StorageCleanup.listCheckpointRefs", + cwd, + args: [ + "for-each-ref", + "--format=%(refname)", + "refs/t3/checkpoints/", + "refs/t3/orchestration-v2/checkpoints/", + ], + maxOutputBytes: 16 * 1024 * 1024, + }); + const refs = result.stdout.split("\n").filter((ref) => { + const ordinalStart = ref.lastIndexOf("/") + 1; + return ( + prefixes.has(ref.slice(0, ordinalStart)) && + /^(0|[1-9]\d*)$/.test(ref.slice(ordinalStart)) + ); + }); + if (refs.length === 0) return; + yield* git.execute({ + operation: "StorageCleanup.deleteCheckpointRefs", + cwd, + // Remove a symbolic checkpoint itself, never its branch/tag target. + args: ["update-ref", "--no-deref", "-z", "--stdin"], + stdin: refs.map((ref) => `delete ${ref}\0\0`).join(""), + }); + yield* Effect.logInfo("storage cleanup removed checkpoint refs", { refCount: refs.length }); + }).pipe( + Effect.catch((cause) => Effect.logWarning("checkpoint ref cleanup failed", { cause })), + ); + } + }); // Only deleted threads' worktrees wait on session stops: any stop while one waits, or a stop inside one kept for another reason. // A deletion stays pending until a sweep that started after it finishes. let deletedWorktreeWaits = true; @@ -1622,6 +1772,9 @@ const make = Effect.gen(function* () { const serverSettings = yield* settingsService.getSettings; const settings = serverSettings.storageCleanup; const entries: StorageCleanupReportEntry[] = []; + yield* cleanCheckpointRefs().pipe( + Effect.catch((cause) => Effect.logWarning("checkpoint ref cleanup failed", { cause })), + ); const deletedWorktrees = yield* cleanWorktrees(serverSettings, now, entries).pipe( Effect.catch((error) => Effect.gen(function* () { @@ -1800,6 +1953,7 @@ const make = Effect.gen(function* () { ) return Effect.void; } + if (event.type === "thread.deleted") return requestSweep; if (!anyWorktreePolicy(lastSettings, (rules) => rules.worktreeOnDelete)) return Effect.void; return event.type === "provider-session.updated" && event.payload.status === "error" ? providerSessions diff --git a/apps/server/src/terminal/Manager.test.ts b/apps/server/src/terminal/Manager.test.ts index 6ad7d7670ea16..73e79257c4e1e 100644 --- a/apps/server/src/terminal/Manager.test.ts +++ b/apps/server/src/terminal/Manager.test.ts @@ -247,6 +247,7 @@ interface CreateManagerOptions { ReadonlyArray<{ readonly pid: number; readonly ppid: number; readonly name: string }>, never >; + foregroundProcessGroup?: (terminalPid: number) => Effect.Effect; subprocessPollIntervalMs?: number; processKillGraceMs?: number; registerTerminalProcesses?: Parameters< @@ -301,6 +302,8 @@ const createManager = ( ? { subprocessInspector: options.subprocessInspector } : {}), ...(options.processTable !== undefined ? { processTable: options.processTable } : {}), + // Fixture pids are not real processes; default to a shell idle at its prompt. + foregroundProcessGroup: options.foregroundProcessGroup ?? ((pid) => Effect.succeed(pid)), ...(options.subprocessPollIntervalMs !== undefined ? { subprocessPollIntervalMs: options.subprocessPollIntervalMs } : {}), @@ -960,6 +963,51 @@ it.layer( }), ); + it.effect("keeps a terminal whose shell was replaced by exec open on settlement", () => + Effect.gen(function* () { + const { manager, ptyAdapter } = yield* createManager(5, { + shellResolver: () => "/bin/bash", + processTable: Effect.succeed([{ pid: 9000, ppid: 1, name: "node" }]), + }).pipe(Effect.provide(layerWithHostPlatform("linux"))); + yield* manager.open(openInput()); + + yield* manager.closeOnSettle({ threadId: "thread-1" }); + + expect(ptyAdapter.processes[0]?.killed).toBe(false); + }), + ); + + it.effect.each([ + { label: "idle prompt", platform: "linux", foregroundProcessGroup: 9000, killed: true }, + { label: "foreground job", platform: "linux", foregroundProcessGroup: 100, killed: false }, + { + label: "unknown foreground group", + platform: "linux", + foregroundProcessGroup: null, + killed: false, + }, + { label: "idle prompt", platform: "darwin", foregroundProcessGroup: 9000, killed: true }, + { label: "foreground job", platform: "darwin", foregroundProcessGroup: 100, killed: false }, + ] as const)( + "settles a shell with a childless shell-named child by its foreground group: $label ($platform)", + ({ platform, foregroundProcessGroup, killed }) => + Effect.gen(function* () { + const { manager, ptyAdapter } = yield* createManager(5, { + shellResolver: () => "/bin/bash", + processTable: Effect.succeed([ + { pid: 9000, ppid: 1, name: "bash" }, + { pid: 100, ppid: 9000, name: "bash" }, + ]), + foregroundProcessGroup: () => Effect.succeed(foregroundProcessGroup), + }).pipe(Effect.provide(layerWithHostPlatform(platform))); + yield* manager.open(openInput()); + + yield* manager.closeOnSettle({ threadId: "thread-1" }); + + expect(ptyAdapter.processes[0]?.killed).toBe(killed); + }), + ); + it.effect("labels a running command by its whole process name, spaces included", () => Effect.gen(function* () { const { manager } = yield* createManager(5, { @@ -1214,6 +1262,37 @@ it.layer( }), ); + it.effect("clears a terminal that keeps printing while its history is saved", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + let printWhileSaving = Effect.void; + const printing = FileSystem.FileSystem.of({ + ...fs, + writeFileString: (filePath, data, options) => + printWhileSaving.pipe(Effect.andThen(fs.writeFileString(filePath, data, options))), + }); + const { manager, ptyAdapter, getEvents } = yield* createManager().pipe( + Effect.provideService(FileSystem.FileSystem, printing), + ); + yield* manager.open(openInput()); + const process = ptyAdapter.processes[0]!; + // Each save sees fresh output, so the history worker never goes idle. + printWhileSaving = Effect.gen(function* () { + const printed = yield* Deferred.make(); + const unsubscribe = yield* manager.subscribe((event) => + event.type === "output" ? Deferred.succeed(printed, undefined) : Effect.void, + ); + process.emitData("tick\n"); + yield* Deferred.await(printed); + unsubscribe(); + }); + + yield* manager.clear({ threadId: "thread-1", terminalId: DEFAULT_TERMINAL_ID }); + + expect((yield* getEvents).map((event) => event.type)).toContain("cleared"); + }), + ); + it.effect("restores a log tail that starts inside a control sequence without its remainder", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; @@ -1926,6 +2005,41 @@ it.layer( }), ); + it.effect("keeps a startup failure in the snapshot every later attachment receives", () => + Effect.gen(function* () { + const { manager, ptyAdapter } = yield* createManager(); + ptyAdapter.spawnFailures.push(new Error("forkpty(3) failed.")); + const startupFailure = { + type: "snapshot", + snapshot: { + status: "error", + error: expect.stringContaining("Failed to spawn PTY process"), + }, + }; + const record = (events: TerminalAttachStreamEvent[]) => (event: TerminalAttachStreamEvent) => + Effect.sync(() => events.push(event)); + + const creating: TerminalAttachStreamEvent[] = []; + (yield* manager.attachStream(openInput(), record(creating)))(); + const resumed: TerminalAttachStreamEvent[] = []; + (yield* manager.attachStream({ ...openInput(), resume: true }, record(resumed)))(); + const observed: TerminalAttachStreamEvent[] = []; + (yield* manager.observeStream( + { threadId: "thread-1", terminalId: DEFAULT_TERMINAL_ID }, + record(observed), + ))(); + expect([creating, resumed, observed]).toMatchObject([ + [startupFailure], + [startupFailure], + [startupFailure], + ]); + + const restarted = yield* manager.open(openInput()); + expect(restarted.status).toBe("running"); + expect(restarted).not.toHaveProperty("error"); + }), + ); + it.effect("retries with fallback shells when preferred shell spawn fails", () => Effect.gen(function* () { const platform = yield* HostProcess.Platform; @@ -1963,6 +2077,59 @@ it.layer( }), ); + it.effect("starts an existing $SHELL whose path contains spaces before a dash", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const { join } = yield* Path.Path; + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-shell-path-" }); + const bin = join(root, "Jane -Doe Smith", ".local", "bin"); + yield* fs.makeDirectory(bin, { recursive: true }); + yield* fs.writeFileString(join(bin, "fish"), ""); + // Temp dirs are not POSIX-absolute on Windows, so stat maps a fixed POSIX prefix onto them. + const fixtureFs = FileSystem.FileSystem.of({ + ...fs, + stat: (filePath) => + fs.stat( + filePath.startsWith("/shell-fixture/") + ? join(root, filePath.slice("/shell-fixture/".length)) + : filePath, + ), + }); + const { manager, ptyAdapter } = yield* createManager(5, { + env: { SHELL: "/shell-fixture/Jane -Doe Smith/.local/bin/fish -l" }, + }).pipe( + Effect.provide(layerWithHostPlatform("darwin")), + Effect.provideService(FileSystem.FileSystem, fixtureFs), + ); + + yield* manager.open(openInput()); + + expect(ptyAdapter.spawnInputs.map((input) => input.shell)).toEqual([ + "/shell-fixture/Jane -Doe Smith/.local/bin/fish", + ]); + }), + ); + + it.effect("keeps missing or quoted $SHELL paths that contain spaces", () => + Effect.gen(function* () { + for (const [shell, expected] of [ + ["/Users/Jane Doe/.local/bin/fish", "/Users/Jane Doe/.local/bin/fish"], + ['"/opt/my -shell/fish" -l', "/opt/my -shell/fish"], + ]) { + const { manager, ptyAdapter } = yield* createManager(5, { + env: { SHELL: shell }, + }).pipe(Effect.provide(layerWithHostPlatform("darwin"))); + + yield* manager.open(openInput()); + + expect( + ptyAdapter.spawnInputs.map((input) => input.shell), + shell, + ).toEqual([expected]); + } + }), + ); + it.effect( "writes completion syntax for the fallback shell instead of the configured fish shell", () => @@ -2621,6 +2788,30 @@ it.layer( }), ); + it.effect("force-kills a closed terminal that ignores SIGTERM when the manager shuts down", () => + Effect.gen(function* () { + const scope = yield* Scope.make(); + const { manager, ptyAdapter } = yield* createManager(5, { + processKillGraceMs: 1_000, + }).pipe(Effect.provideService(Scope.Scope, scope)); + yield* manager.open(openInput()); + const process = ptyAdapter.processes[0]!; + const sigterm = yield* Deferred.make(); + process.onKill = (signal) => { + if (signal === "SIGTERM") Deferred.doneUnsafe(sigterm, Effect.void); + }; + yield* manager.close({ threadId: "thread-1" }); + yield* Deferred.await(sigterm); + + const shutdown = yield* Scope.close(scope, Exit.void).pipe(Effect.forkChild); + yield* TestClock.adjust("1 second"); + yield* Fiber.join(shutdown); + + expect(process.killSignals).toEqual(["SIGTERM", "SIGKILL"]); + expect(process.exitListenerCount).toBe(0); + }).pipe(Effect.provide(TestClock.layer())), + ); + it.effect("attaches to a running provider terminal without resolving the provider again", () => Effect.gen(function* () { const providerInstanceId = ProviderInstanceId.make("codex_work"); diff --git a/apps/server/src/terminal/Manager.ts b/apps/server/src/terminal/Manager.ts index 5b39639fd7367..fec50b0133db1 100644 --- a/apps/server/src/terminal/Manager.ts +++ b/apps/server/src/terminal/Manager.ts @@ -271,6 +271,7 @@ interface TerminalSubprocessInspectResult { interface TerminalSubprocessInspector { ( terminalPid: number, + shell: string | null, ): Effect.Effect; } @@ -339,6 +340,7 @@ interface TerminalSessionState { keys: ReadonlySet; } | null; stopOnSettle: boolean; + startupError: string | null; } interface TerminalGrid { @@ -434,6 +436,7 @@ function snapshot(session: TerminalSessionState): TerminalSessionSnapshot { sequence: session.eventSequence, cols: session.cols, rows: session.rows, + ...(session.startupError === null ? {} : { error: session.startupError }), }; } @@ -550,10 +553,10 @@ function defaultShellResolver(platform: NodeJS.Platform, env: NodeJS.ProcessEnv) return env.SHELL ?? "bash"; } -function normalizeShellCommand( +const normalizeShellCommand = Effect.fn("terminal.normalizeShellCommand")(function* ( value: string | undefined, platform: NodeJS.Platform, -): string | null { +) { if (!value) return null; const trimmed = value.trim(); if (trimmed.length === 0) return null; @@ -562,10 +565,24 @@ function normalizeShellCommand( return trimmed; } - const firstToken = trimmed.split(/\s+/g)[0]?.trim(); - if (!firstToken) return null; - return firstToken.replace(/^['"]|['"]$/g, ""); -} + const quoted = /^(['"])(.*?)\1(?:\s|$)/.exec(trimmed); + if (quoted) return quoted[2] || null; + + if (trimmed.startsWith("/") && /\s/.test(trimmed)) { + const fs = yield* FileSystem.FileSystem; + const ends = [...trimmed.matchAll(/\s+/g)].map((match) => match.index); + // Unquoted paths are ambiguous; prefer the longest existing file over flag syntax. + for (const end of [trimmed.length, ...ends.reverse()]) { + const candidate = trimmed.slice(0, end); + const info = yield* fs + .stat(candidate) + .pipe(Effect.catchTags({ PlatformError: () => Effect.succeed(null) })); + if (info?.type === "File") return candidate; + } + } + + return trimmed.split(/\s+(?=-)/)[0]?.replace(/^['"]|['"]$/g, "") || null; +}); function basenameForPlatform(command: string, platform: NodeJS.Platform): string { const normalized = @@ -641,13 +658,13 @@ function uniqueShellCandidates(candidates: Array): ShellC return ordered; } -function resolveShellCandidates( +const resolveShellCandidates = Effect.fn("terminal.resolveShellCandidates")(function* ( shellResolver: () => string, platform: NodeJS.Platform, env: NodeJS.ProcessEnv, -): ShellCandidate[] { +) { const requested = shellCandidateFromCommand( - normalizeShellCommand(shellResolver(), platform), + yield* normalizeShellCommand(shellResolver(), platform), platform, ); @@ -665,7 +682,7 @@ function resolveShellCandidates( return uniqueShellCandidates([ requested, - shellCandidateFromCommand(normalizeShellCommand(env.SHELL, platform), platform), + shellCandidateFromCommand(yield* normalizeShellCommand(env.SHELL, platform), platform), shellCandidateFromCommand("/bin/zsh", platform), shellCandidateFromCommand("/bin/bash", platform), shellCandidateFromCommand("/bin/sh", platform), @@ -673,7 +690,7 @@ function resolveShellCandidates( shellCandidateFromCommand("bash", platform), shellCandidateFromCommand("sh", platform), ]); -} +}); function isRetryableShellSpawnError(error: PtyAdapter.PtySpawnError): boolean { const queue: unknown[] = [error]; @@ -773,21 +790,48 @@ function processTableSnapshotFromProcesses( return { childrenByParent, commandById }; } +// A root with one of these names is still an interactive shell, e.g. after `.bashrc` runs `exec zsh`. +const INTERACTIVE_SHELL_NAMES = new Set([ + "bash", + "zsh", + "fish", + "sh", + "dash", + "ksh", + "mksh", + "tcsh", + "csh", + "nu", +]); + function deriveSubprocessInspectResult( snapshot: TerminalProcessTableSnapshot, terminalPid: number, + shell: string | null, platform: NodeJS.Platform, + foregroundProcessGroup?: number | null, ): TerminalSubprocessInspectResult { const commandName = (pid: number) => normalizeChildCommandName(snapshot.commandById.get(pid) ?? "", platform); const shellName = commandName(terminalPid); - // Async prompt themes fork childless shell helpers that are not user commands. - const childPid = (snapshot.childrenByParent.get(terminalPid) ?? []).find( - (pid) => - shellName === null || - commandName(pid) !== shellName || - (snapshot.childrenByParent.get(pid)?.length ?? 0) > 0, - ); + // `exec ` replaces the shell, leaving the command itself as the root. + const rootIsCommand = + platform !== "win32" && + shell !== null && + shellName !== null && + shellName !== normalizeChildCommandName(shell, platform) && + !INTERACTIVE_SHELL_NAMES.has(shellName.replace(/^-/, "")); + // Async prompt themes fork childless shell helpers that are not user commands; + // a foreground job owns the terminal's process group, and an unknown one counts as foreground. + const childPid = rootIsCommand + ? terminalPid + : (snapshot.childrenByParent.get(terminalPid) ?? []).find( + (pid) => + shellName === null || + commandName(pid) !== shellName || + (snapshot.childrenByParent.get(pid)?.length ?? 0) > 0 || + (foregroundProcessGroup !== undefined && foregroundProcessGroup !== terminalPid), + ); if (childPid === undefined) { return { hasRunningSubprocess: false, childCommand: null, processIds: [] }; } @@ -810,6 +854,12 @@ function deriveSubprocessInspectResult( }; } +// Field 8 of /proc//stat is the tty's foreground process group; comm (field 2) may hold spaces or parens. +function parseProcStatForegroundProcessGroup(stat: string): number | null { + const tpgid = Number(stat.slice(stat.lastIndexOf(")") + 2).split(" ")[5]); + return Number.isInteger(tpgid) ? tpgid : null; +} + const POSIX_PS_ABSOLUTE_PATHS = ["/bin/ps", "/usr/bin/ps"] as const; // Resolve ps once; bare-name spawns repeatedly probe long PATHs during one-second polling. @@ -1627,6 +1677,7 @@ interface TerminalManagerOptions { ReadonlyArray, TerminalSubprocessCheckError >; + foregroundProcessGroup?: (terminalPid: number) => Effect.Effect; subprocessPollIntervalMs?: number; processKillGraceMs?: number; maxRetainedInactiveSessions?: number; @@ -1825,6 +1876,30 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func snapshotSucceeded: Result.isSuccess(fallback.snapshot), })), ); + const foregroundProcessGroup = + options.foregroundProcessGroup ?? + ((terminalPid: number) => + platform === "linux" + ? fileSystem.readFileString(`/proc/${terminalPid}/stat`).pipe( + Effect.map(parseProcStatForegroundProcessGroup), + Effect.orElseSucceed(() => null), + ) + : processRunner + .run({ + command: posixPsCommand ?? "ps", + args: ["-o", "tpgid=", "-p", String(terminalPid)], + timeout: CLOSE_IDLE_QUERY_TIMEOUT.posix, + timeoutBehavior: "timedOutResult", + }) + .pipe( + Effect.map((result) => { + const tpgid = Number(result.stdout.trim()); + return result.code === 0 && !result.timedOut && Number.isInteger(tpgid) && tpgid > 0 + ? tpgid + : null; + }), + Effect.orElseSucceed(() => null), + )); const customSubprocessInspector = options.subprocessInspector; const acquireSubprocessInspector = ( fallbackTimeout: Duration.Input, @@ -1845,8 +1920,20 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func inspector: Result.map( snapshot, (table): TerminalSubprocessInspector => - (terminalPid) => - Effect.succeed(deriveSubprocessInspectResult(table, terminalPid, platform)), + (terminalPid, shell) => { + const result = deriveSubprocessInspectResult(table, terminalPid, shell, platform); + // Only shell-named childless children are ambiguous; read the foreground group for those alone. + if ( + result.hasRunningSubprocess || + platform === "win32" || + (table.childrenByParent.get(terminalPid)?.length ?? 0) === 0 + ) { + return Effect.succeed(result); + } + return Effect.map(foregroundProcessGroup(terminalPid), (group) => + deriveSubprocessInspectResult(table, terminalPid, shell, platform, group), + ); + }, ), snapshotSucceeded, queryElapsedMs, @@ -2814,6 +2901,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func const startingAt = yield* nowIso; yield* modifyManagerState((state) => { session.status = "starting"; + session.startupError = null; session.shell = null; session.cwd = input.cwd; session.worktreePath = input.worktreePath ?? null; @@ -2837,7 +2925,11 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func increment(terminalSessionsTotal, { lifecycle: eventType }).pipe( Effect.andThen( Effect.gen(function* () { - const shellCandidates = resolveShellCandidates(shellResolver, platform, baseEnv); + const shellCandidates = yield* resolveShellCandidates( + shellResolver, + platform, + baseEnv, + ).pipe(Effect.provideService(FileSystem.FileSystem, fileSystem)); const terminalEnv = createTerminalSpawnEnv( baseEnv, session.runtimeEnv, @@ -2948,6 +3040,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func { const error = startResult.failure; + const message = error.message; if (ptyProcess) { yield* startKillEscalation(ptyProcess, session.threadId, session.terminalId); } @@ -2955,6 +3048,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func yield* modifyManagerState((state) => { cleanupProcessHandles(session); session.status = "error"; + session.startupError = message; session.pid = null; session.process = null; session.hasRunningSubprocess = false; @@ -2972,7 +3066,6 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func yield* evictInactiveSessionsIfNeeded(); - const message = error.message; yield* publishEvent({ type: "error", threadId: session.threadId, @@ -3091,7 +3184,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func let mark = candidate.mark; const previous = quietSessions.get(session); quietSessions.delete(session); - const inspectResult = yield* subprocessInspector(terminalPid).pipe( + const inspectResult = yield* subprocessInspector(terminalPid, session.shell).pipe( Effect.asSome, Effect.catch((reason) => Effect.logWarning("failed to check terminal subprocess activity", { @@ -3222,10 +3315,10 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func yield* Effect.addFinalizer(() => Effect.gen(function* () { - const sessions = yield* modifyManagerState( + const [sessions, pendingKills] = yield* modifyManagerState( (state) => [ - [...state.sessions.values()], + [[...state.sessions.values()], [...state.killFibers.values()]], { ...state, sessions: new Map(), @@ -3242,10 +3335,14 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func yield* runKillEscalation(session.process, session.threadId, session.terminalId); }); - yield* Effect.forEach(sessions, cleanupSession, { - concurrency: "unbounded", - discard: true, - }); + // Closing workerScope next would interrupt escalations of already stopped terminals. + yield* Effect.all( + [ + Effect.forEach(sessions, cleanupSession, { concurrency: "unbounded", discard: true }), + Fiber.awaitAll(pendingKills), + ], + { concurrency: "unbounded", discard: true }, + ); }).pipe(Effect.ignoreCause({ log: true })), ); @@ -3301,6 +3398,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func gitAccountProjectId: input.gitAccountProjectId ?? null, gitAccountEnv: null, stopOnSettle: input.stopOnSettle ?? false, + startupError: null, }; const createdSession = session; @@ -3834,7 +3932,12 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func session.pendingProcessEventIndex = 0; session.processEventDrainRunning = false; const eventStamp = advanceEventSequence(session); - yield* persistHistory(input.threadId, terminalId, session.history); + // A drain waits for the key to go idle, which never happens while the shell keeps + // printing; every reader of the file flushes it first. + yield* persistWorker.enqueue(toSessionKey(input.threadId, terminalId), { + history: session.history, + immediate: true, + }); yield* publishEvent({ type: "cleared", threadId: input.threadId, @@ -3888,7 +3991,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func yield* Effect.forEach( running, (session) => - inspector(session.pid).pipe( + inspector(session.pid, session.shell).pipe( Effect.flatMap((result) => result.hasRunningSubprocess || terminalActivityMark(session) !== marks.get(session.terminalId) diff --git a/apps/server/src/terminal/OutputProtocol.test.ts b/apps/server/src/terminal/OutputProtocol.test.ts index 6d851b93d374d..7ae536605e8fa 100644 --- a/apps/server/src/terminal/OutputProtocol.test.ts +++ b/apps/server/src/terminal/OutputProtocol.test.ts @@ -218,6 +218,9 @@ describe("terminal output stream", () => { Effect.forkScoped, ); const write = yield* Deferred.await(receive); + // Each event arrives after the batching window, so it is sent on its own. + const publish = (event: TerminalAttachStreamEvent) => + TestClock.adjust("2 millis").pipe(Effect.andThen(publisher.publish(event))); const takeChunk = Effect.fnUntraced(function* (queue: typeof stalled) { const response = yield* Queue.take(queue); assert.equal(response._tag, "Chunk"); @@ -234,7 +237,7 @@ describe("terminal output stream", () => { // Eight unacknowledged chunks fill only client 0's output window. for (let index = 0; index < 7; index++) { const event = output("before-stall-" + index); - yield* publisher.publish(event); + yield* publish(event); assert.deepEqual(yield* takeChunk(stalled), [event]); assert.deepEqual(yield* takeChunk(healthy), [event]); yield* write(1, { _tag: "Ack", requestId: "2" }); @@ -242,7 +245,7 @@ describe("terminal output stream", () => { assert.equal(publisher.listeners.size, 2); for (let index = 0; index < 100; index++) { const event = output(index + ":" + "界".repeat(1365)); - yield* publisher.publish(event); + yield* publish(event); assert.deepEqual(yield* takeChunk(healthy), [event]); yield* write(1, { _tag: "Ack", requestId: "2" }); if (index === 31) assert.equal(publisher.listeners.size, 2); @@ -257,7 +260,7 @@ describe("terminal output stream", () => { yield* write(0, { _tag: "Ack", requestId: "1" }); } const after = output("unique output after recovery"); - yield* publisher.publish(after); + yield* publish(after); assert.deepEqual(yield* takeChunk(healthy), [after]); assert.deepEqual(yield* takeChunk(stalled), [after]); assert.equal(yield* Queue.size(stalled), 0); @@ -327,7 +330,48 @@ describe("terminal output stream", () => { }), ); - it.effect("caps each delivered batch at the output window in UTF-8 bytes", () => + it.effect("emits idle output immediately and batches a burst until a fixed deadline", () => + Effect.gen(function* () { + const publisher = makePublisher(); + const pull = yield* Stream.toPull(terminalOutputStream(attach(publisher, ""))); + assert.deepEqual(yield* pull, [snapshot("")]); + + yield* TestClock.adjust("10 millis"); + yield* publisher.publish(output("a")); + assert.deepEqual(yield* pull, [output("a")]); + + yield* publisher.publish(output("b")); + const burst = yield* pull.pipe(Effect.forkChild({ startImmediately: true })); + yield* TestClock.adjust("1 millis"); + assert.isUndefined(burst.pollUnsafe()); + // Arrivals inside the window never push the deadline back. + yield* publisher.publish(output("c")); + yield* publisher.publish(output("d")); + yield* TestClock.adjust("1 millis"); + assert.deepEqual(yield* Fiber.join(burst), [output("b"), output("c"), output("d")]); + }).pipe(Effect.scoped), + ); + + it.effect("flushes pending output early, in order, when a lifecycle event arrives", () => + Effect.gen(function* () { + const publisher = makePublisher(); + const pull = yield* Stream.toPull(terminalOutputStream(attach(publisher, ""))); + assert.deepEqual(yield* pull, [snapshot("")]); + + yield* publisher.publish(output("last words")); + const waiting = yield* pull.pipe(Effect.forkChild({ startImmediately: true })); + assert.isUndefined(waiting.pollUnsafe()); + const closed: TerminalAttachStreamEvent = { + type: "closed", + threadId: "thread-1", + terminalId: "default", + }; + yield* publisher.publish(closed); + assert.deepEqual(yield* Fiber.join(waiting), [output("last words"), closed]); + }).pipe(Effect.scoped), + ); + + it.effect("flushes full batches early and caps each at the output window in UTF-8 bytes", () => Effect.gen(function* () { const publisher = makePublisher(); const pull = yield* Stream.toPull(terminalOutputStream(attach(publisher, ""))); @@ -336,8 +380,9 @@ describe("terminal output stream", () => { const data = "界".repeat(1024); for (let index = 0; index < 1024; index++) yield* publisher.publish(output(data)); + // Twenty 3 KiB events fill a 64 KiB batch, so full batches skip the deadline. let delivered = 0; - while (delivered < 1024) { + while (delivered < 1020) { const batch = yield* pull; assert.isAtMost( batch.reduce( @@ -348,7 +393,11 @@ describe("terminal output stream", () => { ); delivered += batch.length; } - assert.equal(delivered, 1024); + assert.equal(delivered, 1020); + const tail = yield* pull.pipe(Effect.forkChild({ startImmediately: true })); + assert.isUndefined(tail.pollUnsafe()); + yield* TestClock.adjust("2 millis"); + assert.lengthOf(yield* Fiber.join(tail), 4); }), ); diff --git a/apps/server/src/terminal/OutputProtocol.ts b/apps/server/src/terminal/OutputProtocol.ts index 0edb5aff4978d..84f6e40923417 100644 --- a/apps/server/src/terminal/OutputProtocol.ts +++ b/apps/server/src/terminal/OutputProtocol.ts @@ -6,6 +6,8 @@ import { } from "@t3tools/contracts"; import type * as Arr from "effect/Array"; import * as Cause from "effect/Cause"; +import * as Clock from "effect/Clock"; +import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; import * as Latch from "effect/Latch"; import * as Stream from "effect/Stream"; @@ -137,6 +139,9 @@ export function withOutputWindow( } const MAX_QUEUED_OUTPUT = 8 * 1024 * 1024; +// Output that follows an emission this closely waits and rides along in one batch, +// so a PTY that delivers one line per callback (ConPTY) is not sent one frame per line. +const OUTPUT_WINDOW_NANOS = 2_000_000n; type Listener
    = (event: A) => Effect.Effect; // Acquisition must register cleanup synchronously with installing the listener. @@ -152,11 +157,15 @@ export function terminalOutputStream { return Stream.unwrap( Effect.gen(function* () { + const clock = yield* Clock.Clock; const ready = yield* Latch.make(); let pending: Array = []; let sizes: number[] = []; let head = 0; let queued = 0; + // Pending events other than output; any of them flushes the window early. + let lifecycle = 0; + let deadline = 0n; let overflowed = false; let subscription = 0; let unsubscribe: (() => void) | null = null; @@ -168,7 +177,9 @@ export function terminalOutputStream lifecycle > 0 || queued >= MAX_PENDING_BYTES; yield* Effect.addFinalizer(() => Effect.sync(release)); const listen = (from: Subscribe) => @@ -178,16 +189,19 @@ export function terminalOutputStream Effect.sync(() => { if (id !== subscription) return; + const wasEmpty = head === pending.length; const size = queuedSize(event); pending.push(event); sizes.push(size); queued += size; + if (event.type !== "output") lifecycle += 1; // A single snapshot must fit even when history fills the budget. if (queued - sizes[head]! > maxQueued) { release(); overflowed = true; } - ready.openUnsafe(); + // The current or next pull handles the pending output deadline. + if (wasEmpty || overflowed || urgent()) ready.openUnsafe(); }), (stop) => { // Overflow or cancellation may invalidate acquisition before registration. @@ -204,6 +218,7 @@ export function terminalOutputStream => { @@ -228,9 +243,16 @@ export function terminalOutputStream= deadline || urgent()) { + deadline = now + OUTPUT_WINDOW_NANOS; + return Effect.succeed(takeBatch()); + } + return Effect.raceFirst(ready.await, Effect.sleep(Duration.nanos(deadline - now))).pipe( + Effect.andThen(pull), + ); }, ); diff --git a/apps/server/src/terminal/nodePtyFlushDataAndCleanUp.test.ts b/apps/server/src/terminal/nodePtyFlushDataAndCleanUp.test.ts new file mode 100644 index 0000000000000..7dc0861c7f53c --- /dev/null +++ b/apps/server/src/terminal/nodePtyFlushDataAndCleanUp.test.ts @@ -0,0 +1,131 @@ +import * as NodeModule from "node:module"; + +import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; + +const requireCjs = NodeModule.createRequire(import.meta.url); +const { + WindowsPtyAgent, +}: { + WindowsPtyAgent: { + prototype: { + _flushDataAndCleanUp(this: FakeAgent): void; + _cleanUpProcess(this: FakeAgent): void; + }; + }; +} = requireCjs("node-pty/lib/windowsPtyAgent.js"); + +interface FakeAgent { + _useConptyDll: boolean; + _closeTimeout?: unknown; + _inSocket: { readable: boolean }; + _outSocket: { readable: boolean; destroy: () => void }; + _cleanUpProcess: () => void; +} + +// The sockets are stand-ins; the timer logic and _cleanUpProcess are node-pty's own (as patched by T3). +const makeAgent = (): FakeAgent => { + const agent: FakeAgent = { + _useConptyDll: false, + _inSocket: { readable: true }, + _outSocket: { readable: true, destroy: vi.fn() }, + _cleanUpProcess: () => WindowsPtyAgent.prototype._cleanUpProcess.call(agent), + }; + return agent; +}; + +const flush = (agent: FakeAgent) => WindowsPtyAgent.prototype._flushDataAndCleanUp.call(agent); + +// A minimal event loop that mirrors Node's phases: expired timers run first, then immediates (the check phase). +// node-pty calls the global timer functions at call time, so replacing them lets the test place a "poll phase" +// (delivery of already-buffered pipe data) exactly between the two. +interface Timer { + readonly id: number; + readonly dueAt: number; + readonly run: () => void; +} +let now = 0; +let nextId = 0; +let timers: Timer[] = []; +let immediates: Array<() => void> = []; + +const runExpiredTimers = () => { + for (const timer of timers.filter((t) => t.dueAt <= now).toSorted((a, b) => a.dueAt - b.dueAt)) { + timers = timers.filter((t) => t.id !== timer.id); + timer.run(); + } +}; +const runImmediates = () => { + const batch = immediates; + immediates = []; + for (const run of batch) run(); +}; +/** One loop iteration after `ms` of wall time; `poll` runs between the timers and check phases. */ +const loopIteration = (ms: number, poll?: () => void) => { + now += ms; + runExpiredTimers(); + poll?.(); + runImmediates(); +}; + +// ConPTY never closes the output pipe after the shell exits, so node-pty ends the session on a timer. +// T3 shortens it (patches/node-pty@1.2.0-beta.15.patch) so the terminal reports "exited" about 0.9 s sooner. +describe("node-pty Windows exit flush", () => { + beforeEach(() => { + now = 0; + nextId = 0; + timers = []; + immediates = []; + vi.stubGlobal("setTimeout", (run: () => void, ms: number) => { + const id = ++nextId; + timers.push({ id, dueAt: now + ms, run }); + return id; + }); + vi.stubGlobal("clearTimeout", (id: number) => { + timers = timers.filter((t) => t.id !== id); + }); + vi.stubGlobal("setImmediate", (run: () => void) => { + immediates.push(run); + return ++nextId; + }); + }); + afterEach(() => { + vi.unstubAllGlobals(); + }); + + it("closes the output socket 100 ms after the exit, not a second later", () => { + const agent = makeAgent(); + flush(agent); + + loopIteration(99); + expect(agent._outSocket.destroy).not.toHaveBeenCalled(); + + loopIteration(1); + expect(agent._outSocket.destroy).toHaveBeenCalledTimes(1); + expect(agent._outSocket.readable).toBe(false); + expect(agent._inSocket.readable).toBe(false); + }); + + // Timers run before the poll phase. After a main-thread stall longer than the window, output that is already + // buffered in the pipe is read only after the timer has expired, so the cleanup has to wait one loop turn for it. + it("keeps the socket open when buffered output is read right after a stalled timer expires", () => { + const agent = makeAgent(); + flush(agent); + + loopIteration(150, () => flush(agent)); + expect(agent._outSocket.destroy).not.toHaveBeenCalled(); + + loopIteration(99); + expect(agent._outSocket.destroy).not.toHaveBeenCalled(); + + loopIteration(1); + expect(agent._outSocket.destroy).toHaveBeenCalledTimes(1); + }); + + it("closes after a stall when no output was buffered", () => { + const agent = makeAgent(); + flush(agent); + + loopIteration(150); + expect(agent._outSocket.destroy).toHaveBeenCalledTimes(1); + }); +}); diff --git a/apps/server/src/textGeneration/AntigravityTextGeneration.test.ts b/apps/server/src/textGeneration/AntigravityTextGeneration.test.ts index 783417693c722..5a410dbf342f7 100644 --- a/apps/server/src/textGeneration/AntigravityTextGeneration.test.ts +++ b/apps/server/src/textGeneration/AntigravityTextGeneration.test.ts @@ -549,6 +549,7 @@ it.layer(NodeServices.layer)("AntigravityTextGeneration", (it) => { { name: "hooks.json", value: "{}", available: true }, { name: "hooks.json", value: '{"hooks":{}}', available: true }, { name: "mcp_config.json", value: '{"mcpServers":{}}', available: true }, + { name: "mcp_config.json", value: "", available: true }, ])("checks $name before starting a helper, available=$available", ({ name, value, available }) => Effect.gen(function* () { const fixture = yield* makeFixture(); diff --git a/apps/server/src/textGeneration/AntigravityTextGeneration.ts b/apps/server/src/textGeneration/AntigravityTextGeneration.ts index b7e7cfaeb40e8..b67bad85faf03 100644 --- a/apps/server/src/textGeneration/AntigravityTextGeneration.ts +++ b/apps/server/src/textGeneration/AntigravityTextGeneration.ts @@ -79,14 +79,20 @@ export const isAntigravityTextGenerationAvailable = Effect.fn( if (info.type !== "File" || info.size > 64_000n) { return false; } + // CLI 1.5.8 writes a 0-byte mcp_config.json before any server exists. const empty = yield* fs.readFileString(configurationPath).pipe( - Effect.flatMap(decodeConfiguration), - Effect.flatMap((configuration) => - decodeConfigurationObject( - configuration[name === "hooks.json" ? "hooks" : "mcpServers"] ?? configuration, - ), + Effect.flatMap((text) => + text.trim().length === 0 + ? Effect.succeed(true) + : decodeConfiguration(text).pipe( + Effect.flatMap((configuration) => + decodeConfigurationObject( + configuration[name === "hooks.json" ? "hooks" : "mcpServers"] ?? configuration, + ), + ), + Effect.map((configuration) => Object.keys(configuration).length === 0), + ), ), - Effect.map((configuration) => Object.keys(configuration).length === 0), Effect.orElseSucceed(() => false), ); if (!empty) return false; diff --git a/apps/server/src/textGeneration/ClaudeTextGeneration.test.ts b/apps/server/src/textGeneration/ClaudeTextGeneration.test.ts index a7041d5d249e5..d2e165ea3b2b5 100644 --- a/apps/server/src/textGeneration/ClaudeTextGeneration.test.ts +++ b/apps/server/src/textGeneration/ClaudeTextGeneration.test.ts @@ -234,6 +234,35 @@ it.layer(layerClaudeTextGenerationTest)("ClaudeTextGeneration", (it) => { }), ); + it.effect("passes --settings from the instance launch args to Claude text generation", () => + withFakeClaudeEnv( + { + output: JSON.stringify({ + structured_output: { + title: "Use instance settings", + }, + }), + argsMustContain: `"env":{"ANTHROPIC_VERTEX_PROJECT_ID":"vertex-project-a"}`, + claudeConfig: { + launchArgs: `--settings '{"env":{"ANTHROPIC_VERTEX_PROJECT_ID":"vertex-project-a"}}'`, + }, + }, + (textGeneration) => + Effect.gen(function* () { + const generated = yield* textGeneration.generateThreadTitle({ + cwd: process.cwd(), + message: "thread title", + modelSelection: { + instanceId: ProviderInstanceId.make("claudeAgent"), + model: SYNTHETIC_CLAUDE_STANDARD_MODEL, + }, + }); + + expect(generated.title).toBe(sanitizeThreadTitle("Use instance settings")); + }), + ), + ); + it.effect("retries without --no-session-persistence when the Claude CLI rejects it", () => withFakeClaudeEnv( { diff --git a/apps/server/src/textGeneration/ClaudeTextGeneration.ts b/apps/server/src/textGeneration/ClaudeTextGeneration.ts index 756a068926a60..a523a3873a2c5 100644 --- a/apps/server/src/textGeneration/ClaudeTextGeneration.ts +++ b/apps/server/src/textGeneration/ClaudeTextGeneration.ts @@ -1,6 +1,7 @@ import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import * as Option from "effect/Option"; +import * as Path from "effect/Path"; import * as Schema from "effect/Schema"; import * as Scope from "effect/Scope"; import * as Stream from "effect/Stream"; @@ -33,6 +34,7 @@ import { hasClaudeManagedMcpConfig, makeClaudeEnvironment, } from "../provider/Drivers/ClaudeHome.ts"; +import { readClaudeLaunchArgSettings } from "../orchestration-v2/Adapters/ClaudeAdapterV2.ts"; const CLAUDE_TIMEOUT_MS = 180_000; /** `--tools` takes bare names; patterns go in the allow list. */ @@ -105,6 +107,7 @@ export const makeClaudeTextGeneration = Effect.fn("makeClaudeTextGeneration")(fu ) { const commandSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; const claudeEnvironment = yield* makeClaudeEnvironment(claudeSettings, environment); const scopedModelCatalog = modelCatalog.pipe( Effect.map((catalog) => scopeClaudeModelCatalog(catalog, claudeSettings.customModels)), @@ -200,7 +203,18 @@ export const makeClaudeTextGeneration = Effect.fn("makeClaudeTextGeneration")(fu : undefined; const fastMode = fastModeDescriptor?.type === "boolean" ? fastModeDescriptor.currentValue : undefined; + const launchArgSettings = yield* readClaudeLaunchArgSettings({ + settings: claudeSettings, + cwd, + fileSystem, + path, + }).pipe( + Effect.mapError( + (cause) => new TextGenerationError({ operation, detail: cause.message, cause }), + ), + ); const settings = { + ...launchArgSettings, disableAllHooks: true, ...(typeof thinking === "boolean" ? { alwaysThinkingEnabled: thinking } : {}), ...(fastMode ? { fastMode: true } : {}), diff --git a/apps/server/src/textGeneration/CodexTextGeneration.test.ts b/apps/server/src/textGeneration/CodexTextGeneration.test.ts index e6e43d606e27f..1a1a5c43b0a0d 100644 --- a/apps/server/src/textGeneration/CodexTextGeneration.test.ts +++ b/apps/server/src/textGeneration/CodexTextGeneration.test.ts @@ -499,6 +499,26 @@ it.layer(layerCodexTextGenerationTest)("CodexTextGeneration", (it) => { ), ); + it.effect("keeps user hooks off over launch args that turn them on", () => + withFakeCodexEnv( + { + output: JSON.stringify({ title: "Hookless title" }), + launchArgs: "--config features.hooks=true --enable hooks", + requireArg: "--config features.codex_hooks=false --config features.hooks=false", + forbidArg: "--enable", + }, + (textGeneration) => + Effect.gen(function* () { + const result = yield* textGeneration.generateThreadTitle({ + cwd: process.cwd(), + message: "Describe this change", + modelSelection: DEFAULT_TEST_MODEL_SELECTION, + }); + expect(result.title).toBe("Hookless title"); + }), + ), + ); + it.effect("passes exec-safe launch args into codex exec", () => withFakeCodexEnv( { diff --git a/apps/server/src/textGeneration/CodexTextGeneration.ts b/apps/server/src/textGeneration/CodexTextGeneration.ts index 49c60166111ba..fc741aaaeb262 100644 --- a/apps/server/src/textGeneration/CodexTextGeneration.ts +++ b/apps/server/src/textGeneration/CodexTextGeneration.ts @@ -326,6 +326,11 @@ export const makeCodexTextGeneration = Effect.fn("makeCodexTextGeneration")(func model, "--config", `model_reasoning_effort="${reasoningEffort}"`, + // Current Codex gives hooks precedence over the historical codex_hooks alias. + "--config", + "features.codex_hooks=false", + "--config", + "features.hooks=false", ...(serviceTier ? ["--config", `service_tier="${serviceTier}"`] : []), "--output-schema", schemaPath, diff --git a/apps/server/src/textGeneration/GrokTextGeneration.test.ts b/apps/server/src/textGeneration/GrokTextGeneration.test.ts index 26ab9e79c7103..adb911623f800 100644 --- a/apps/server/src/textGeneration/GrokTextGeneration.test.ts +++ b/apps/server/src/textGeneration/GrokTextGeneration.test.ts @@ -35,7 +35,7 @@ function makeAcpGrokWrapper(dir: string, env: Record): string { env, source: execScriptSource({ scriptPath: mockAgentPath, - expectedArgs: ["agent", "stdio"], + expectedArgs: ["--permission-mode", "default", "agent", "stdio"], }), }); } @@ -58,14 +58,19 @@ function withFakeAcpGrok( }).pipe(Effect.scoped); } -function readJsonRpcRequests( - filePath: string, -): ReadonlyArray<{ readonly method?: string; readonly params?: Record }> { +function readJsonRpcRequests(filePath: string): ReadonlyArray<{ + readonly id?: unknown; + readonly method?: string; + readonly params?: Record; +}> { return NodeFS.readFileSync(filePath, "utf8") .trim() .split("\n") .filter((line) => line.length > 0) - .map((line) => JSON.parse(line) as { method?: string; params?: Record }); + .map( + (line) => + JSON.parse(line) as { id?: unknown; method?: string; params?: Record }, + ); } it.layer(layerGrokTextGenerationTest)("GrokTextGeneration", (it) => { @@ -78,6 +83,7 @@ it.layer(layerGrokTextGenerationTest)("GrokTextGeneration", (it) => { return withFakeAcpGrok( { T3_ACP_REQUEST_LOG_PATH: requestLogPath, + T3_ACP_REQUEST_PERMISSION: "1", T3_ACP_PROMPT_RESPONSE_TEXT: JSON.stringify({ subject: "Add Grok provider", body: "Wire up the ACP runtime and headless text generation path.", @@ -110,6 +116,9 @@ it.layer(layerGrokTextGenerationTest)("GrokTextGeneration", (it) => { request.params?.modelId === "grok-mock-alt", ), ).toBe(true); + expect(requests.find((request) => request.id === 9001)).toMatchObject({ + result: { outcome: { outcome: "cancelled" } }, + }); }), ); }); diff --git a/apps/server/src/textGeneration/TextGenerationPresets.ts b/apps/server/src/textGeneration/TextGenerationPresets.ts index b53ecc543597d..2dd3b95321290 100644 --- a/apps/server/src/textGeneration/TextGenerationPresets.ts +++ b/apps/server/src/textGeneration/TextGenerationPresets.ts @@ -3,7 +3,7 @@ import type { TextGenerationPolicy } from "@t3tools/provider-core/server/textGen export const conventionalCommitsTextGenerationPolicy: TextGenerationPolicy = { kind: "conventional_commits", commitInstructions: - "Use Conventional Commits when generating commit subjects. Prefer the narrowest accurate type and include a scope only when it is obvious from the diff.", + "Use Conventional Commits when generating commit subjects. Prefer the narrowest accurate type and include a scope only when it is obvious from the diff. Start the description after the colon with a lowercase word, never a proper noun or acronym, because commitlint rejects a capitalized description.", changeRequestInstructions: "Keep the change request title concise. Do not force Conventional Commit syntax into the title unless the repository already uses it.", inferRepositoryConventions: false, diff --git a/apps/server/src/usage/UsageService.test.ts b/apps/server/src/usage/UsageService.test.ts index c9571c3dfa531..3d0048d19ad21 100644 --- a/apps/server/src/usage/UsageService.test.ts +++ b/apps/server/src/usage/UsageService.test.ts @@ -215,6 +215,7 @@ const layerService = (input: { Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), Layer.provideMerge(Layer.succeed(HostProcess.Platform, input.platform ?? "linux")), Layer.provideMerge(Layer.succeed(HostProcess.Hostname, input.hostname ?? "usage-test-host")), + Layer.provideMerge(Layer.succeed(HostProcess.HomeDirectory, input.home)), Layer.provideMerge(ServerSettings.layerTest(input.settings)), Layer.provideMerge( Layer.succeed( @@ -1227,6 +1228,76 @@ describe("UsageService", () => { }), ); + it.effect("prices Pi usage mapped onto a Pi model at that model's rate", () => + Effect.gen(function* () { + const { home, settings } = yield* setup; + const dir = NodePath.join(home, "pi", "sessions", "project"); + const message = (id: string, model: string) => ({ + type: "message", + id, + timestamp: "2026-08-01T10:00:00Z", + message: { + role: "assistant", + provider: "anthropic", + model, + usage: { input: 10, output: 5 }, + }, + }); + yield* Effect.promise(async () => { + await NodeFSP.mkdir(dir, { recursive: true }); + await NodeFSP.writeFile( + NodePath.join(dir, "session.jsonl"), + [ + { type: "session", id: "session" }, + message("released", "actual-model"), + message("preview", "actual-model-preview"), + ] + .map((line) => encodeUnknownJsonString(line)) + .join("\n") + "\n", + ); + }); + const summary = yield* Effect.gen(function* () { + const service = yield* UsageService.make; + return yield* service.readSummary(WINDOW); + }).pipe( + Effect.scoped, + Effect.provide( + layerService({ + prefix: "usage-pi-model-alias", + home, + ratesDocument: { + "actual-model": { input_cost_per_token: 1, output_cost_per_token: 4 }, + }, + settings: { + ...settings, + usageModelAliases: { "anthropic/actual-model-preview": "anthropic/actual-model" }, + }, + }), + ), + ); + assert.deepStrictEqual( + summary.buckets + .filter((bucket) => bucket.provider === "pi") + .map(({ model, records, unpricedRecords, costUsd, costSource }) => ({ + model, + records, + unpricedRecords, + costUsd, + costSource, + })), + [ + { + model: "anthropic/actual-model", + records: 2, + unpricedRecords: 0, + costUsd: 60, + costSource: "modelPriced", + }, + ], + ); + }), + ); + it.effect( "reads Muse inference history once, persists appended usage, and reprices unknown models", () => @@ -2494,6 +2565,34 @@ describe("UsageService", () => { }), ); + it.effect("keeps default-home history after the default account moves to a custom home", () => + Effect.gen(function* () { + const { transcript, settings, home } = yield* setup; + const defaultHome = NodePath.join(home, "default-claude"); + const defaultProjects = NodePath.join(defaultHome, "projects", "proj"); + yield* Effect.promise(async () => { + await NodeFSP.writeFile(transcript, claudeLine(1, 5)); + await NodeFSP.mkdir(defaultProjects, { recursive: true }); + await NodeFSP.writeFile( + NodePath.join(defaultProjects, "earlier.jsonl"), + claudeLine(2, 7, "claude-fable-5", "earlier-session"), + ); + }); + const summary = yield* UsageService.make.pipe( + Effect.flatMap((service) => service.readSummary(WINDOW)), + Effect.provide( + layerService({ + prefix: "usage-service-default-home-test", + home, + environment: { CLAUDE_CONFIG_DIR: defaultHome }, + settings, + }), + ), + ); + assert.strictEqual(totalOutputTokens(summary), 12); + }), + ); + it.effect( "uses explicit account settings before environment and default homes, then refreshes them", () => @@ -3208,7 +3307,7 @@ describe("UsageService", () => { 60 + 20 * (version === 4 ? 1 : 2), ); yield* upgraded.awaitPersisted; - assert.strictEqual(SCAN_CACHE_FILE_NAME, "usage-scan-cache-v16.json"); + assert.strictEqual(SCAN_CACHE_FILE_NAME, "usage-scan-cache-v17.json"); assert.deepStrictEqual( (yield* Effect.promise(() => NodeFSP.readdir(stateDir))) .filter((name) => name.startsWith("usage-scan-cache")) @@ -3772,6 +3871,56 @@ describe("UsageService", () => { }), ); + it.effect("refetches cached rates early for a model they do not list", () => + Effect.gen(function* () { + yield* TestClock.setTime(Date.parse("2026-08-02T00:00:00Z")); + const { transcript, settings, home } = yield* setup; + yield* Effect.promise(() => + NodeFSP.writeFile( + transcript, + claudeLine(1, 5, "claude-new-model") + claudeLine(2, 5, "claude-unknown-model"), + ), + ); + const requests: string[] = []; + let ratesDocument: Record = { + "claude-fable-5": { input_cost_per_token: 1e-5, output_cost_per_token: 5e-5 }, + }; + const serviceLayer = layerService({ + prefix: "usage-service-unlisted-model-test", + home, + settings, + onRatesFetch: (request) => requests.push(request.url), + ratesResponse: () => Effect.succeed(Response.json(ratesDocument)), + }); + const newModelCost = (summary: UsageSummary) => + summary.buckets.find((bucket) => bucket.model === "claude-new-model")?.costUsd; + + // Rates fetched for this scan are fresh: no second request for the models they lack. + const first = yield* (yield* UsageService.make.pipe( + Effect.provide(serviceLayer), + )).readSummary(WINDOW); + assert.strictEqual(newModelCost(first), 0); + assert.strictEqual(requests.length, 1); + + // An hour later LiteLLM lists the new model; a restarted server starts from the cached table. + ratesDocument = { + ...ratesDocument, + "claude-new-model": { input_cost_per_token: 2e-5, output_cost_per_token: 1e-4 }, + }; + yield* TestClock.adjust(Duration.hours(1)); + const service = yield* UsageService.make.pipe(Effect.provide(serviceLayer)); + const refreshed = yield* service.readSummary(WINDOW); + assert.strictEqual(refreshed.pricing.status, "fresh"); + assert.closeTo(newModelCost(refreshed) ?? -1, 10 * 2e-5 + 5 * 1e-4, 1e-12); + assert.strictEqual(requests.length, 2); + + // A model the fresh table still lacks does not refetch again before the TTL. + yield* TestClock.adjust(Duration.hours(1)); + yield* service.readSummary(WINDOW); + assert.strictEqual(requests.length, 2); + }), + ); + it.effect("does not orphan an in-flight scan when its first caller is interrupted", () => Effect.gen(function* () { const { settings, home } = yield* setup; diff --git a/apps/server/src/usage/UsageService.ts b/apps/server/src/usage/UsageService.ts index 638d2fe2d2d97..a59fbc66a02ca 100644 --- a/apps/server/src/usage/UsageService.ts +++ b/apps/server/src/usage/UsageService.ts @@ -40,7 +40,12 @@ import type { UsageRecord, } from "@t3tools/provider-core/server/usage"; import { canonicalModelFilter, resolveModelAliases, UsageAggregator } from "./usageAggregation.ts"; -import { createOverrideRateTable, parseRateTable, type RateTable } from "./usagePricing.ts"; +import { + createOverrideRateTable, + isUnlistedModel, + parseRateTable, + type RateTable, +} from "./usagePricing.ts"; import { DESKTOP_HOST_SCOPE_ENV, listTranscriptFiles, @@ -85,6 +90,12 @@ const scanReaders = BUILT_IN_USAGE_DRIVERS.flatMap((driver) => driver.usage?.kind === "scan" ? [{ driver, reader: driver.usage }] : [], ); +const providerRateModels = new Map( + BUILT_IN_USAGE_DRIVERS.flatMap(({ usage }) => + usage?.rateModel === undefined ? [] : [[usage.provider, usage.rateModel] as const], + ), +); + const transcriptFormats = new Map( transcriptReaders.map(({ reader }) => [reader.provider, reader.format] as const), ); @@ -312,6 +323,24 @@ export const make = Effect.gen(function* () { const ensureRates = (force: boolean) => ratesLock.withPermit(loadRates(force)); + // Rates are kept for RATES_TTL_MS, so a model released after the table was fetched would stay unpriced + // that long. A scan that meets a model the table does not list refetches early: once per model against + // a fresh table, with loadRates keeping RATES_REFRESH_FLOOR_MS between attempts. + const checkedUnlistedModels = new Set(); + const refreshForUnlistedModels = Effect.fn("UsageService.refreshForUnlistedModels")(function* ( + models: ReadonlySet, + ) { + const unlisted = [...models].filter( + (model) => !checkedUnlistedModels.has(model) && isUnlistedModel(rates, model), + ); + if (unlisted.length === 0) return; + yield* ensureRates(true); + const now = yield* Clock.currentTimeMillis; + // A failed fetch leaves the models unchecked, so a later scan tries again. + if (ratesFetchedAtMs === null || now - ratesFetchedAtMs >= RATES_REFRESH_FLOOR_MS) return; + for (const model of unlisted) checkedUnlistedModels.add(model); + }); + const refreshRates = ensureRates(true).pipe( Effect.map(pricing), Effect.withSpan("UsageService.refreshRates"), @@ -337,9 +366,8 @@ export const make = Effect.gen(function* () { > = Object.entries(settings.providerInstances) .filter(([, instance]) => instance.driver === driver.driverKind) .map(([id, instance]) => [ProviderInstanceId.make(id), instance, true] as const); - if (!Object.hasOwn(settings.providerInstances, driver.driverKind)) { - entries.push([ProviderInstanceId.make(driver.driverKind), {}, false]); - } + // History in the driver's default home stays counted after every instance moves elsewhere; readers dedupe shared directories. + entries.push([ProviderInstanceId.make(driver.driverKind), {}, false]); const decodeConfig = Schema.decodeUnknownOption(driver.configSchema); return yield* Effect.forEach( entries, @@ -881,19 +909,8 @@ export const make = Effect.gen(function* () { { concurrency: 2 }, ); - const summaryPricing = pricing(); - const aggregator = new UsageAggregator({ - timeZone: input.timeZone, - sinceDay: input.sinceDay, - untilDay: input.untilDay, - resolution: input.resolution ?? "day", - ...hourlyWindow, - rates, - priceOverrides: createOverrideRateTable(settings.usagePriceOverrides), - modelAliases: resolveModelAliases(settings.usageModelAliases), - ...(input.modelFilter === undefined ? {} : { modelFilter: input.modelFilter }), - }); - + const priceOverrides = createOverrideRateTable(settings.usagePriceOverrides); + const modelAliases = resolveModelAliases(settings.usageModelAliases); const sources: UsageSource[] = []; // Deleted transcripts' saved usage still aggregates and deduplicates normally. @@ -959,6 +976,37 @@ export const make = Effect.gen(function* () { for (const filePath of ordered) fileCache.set(filePath, entries.get(filePath)!); cacheDirty = true; } + + // The models priced below, as UsageAggregator resolves them: an alias replaces rateModel. + const rateModels = new Set(); + for (const files of filesByDir) { + for (const file of files) { + for (const record of file.records) { + const alias = modelAliases.get(record.model); + const model = alias ?? record.model; + if (priceOverrides.has(model.trim())) continue; + rateModels.add(alias === undefined ? (record.rateModel ?? model) : model); + } + } + } + // A saved answer prices with saved rates and the full scan after it refetches. + if (!fromMemory) yield* refreshForUnlistedModels(rateModels); + + const summaryPricing = pricing(); + + const aggregator = new UsageAggregator({ + timeZone: input.timeZone, + sinceDay: input.sinceDay, + untilDay: input.untilDay, + resolution: input.resolution ?? "day", + ...hourlyWindow, + rates, + priceOverrides, + modelAliases, + rateModel: (provider, model) => providerRateModels.get(provider)?.(model), + ...(input.modelFilter === undefined ? {} : { modelFilter: input.modelFilter }), + }); + const windowEndMs = hourlyWindow?.untilTimeMs ?? Date.parse(`${input.untilDay}T00:00:00Z`) + 86400000 + MTIME_SLACK_MS; diff --git a/apps/server/src/usage/cliproxyApi.test.ts b/apps/server/src/usage/cliproxyApi.test.ts index 431af7b5971ff..5002d2223d1ba 100644 --- a/apps/server/src/usage/cliproxyApi.test.ts +++ b/apps/server/src/usage/cliproxyApi.test.ts @@ -437,6 +437,26 @@ describe("CLIProxyAPI built-in management API", () => { }), ); + it.effect("keeps a Claude account whose window reports no utilization", () => + Effect.gen(function* () { + const test = fixture({ + accounts: [{ ...accounts[0]!, provider: "claude" }], + upstream: () => ({ + status: 200, + body: { + five_hour: { utilization: null, resets_at: null }, + seven_day: { utilization: 50, resets_at: "2099-01-01T00:00:00Z" }, + }, + }), + }); + const result = yield* (yield* test.api).readAccounts(config); + expect(result[0]?.usageLimits.unavailable).toBeUndefined(); + expect( + result[0]?.usageLimits.windows.map((window) => [window.id, window.usedPercent]), + ).toEqual([["seven_day", 50]]); + }), + ); + const antigravitySummary = { buckets: [ { diff --git a/apps/server/src/usage/cliproxyApi.ts b/apps/server/src/usage/cliproxyApi.ts index 030983f50e33d..537a2b436f4a4 100644 --- a/apps/server/src/usage/cliproxyApi.ts +++ b/apps/server/src/usage/cliproxyApi.ts @@ -70,8 +70,9 @@ const CodexUsage = Schema.Struct({ }), ), }); +// The usage endpoint can report a window without a utilization, as the CLI's own schema allows. const ClaudeWindow = Schema.Struct({ - utilization: Schema.Number, + utilization: Schema.NullOr(Schema.Number), resets_at: Schema.NullOr(Schema.String), }); const ClaudeUsage = Schema.Struct({ diff --git a/apps/server/src/usage/usageAggregation.test.ts b/apps/server/src/usage/usageAggregation.test.ts index f99615085bd2a..58953fbcfafea 100644 --- a/apps/server/src/usage/usageAggregation.test.ts +++ b/apps/server/src/usage/usageAggregation.test.ts @@ -141,6 +141,32 @@ describe("UsageAggregator", () => { } }); + it("keeps a live Grok turn over a saved pre-v17 maximum in either source order", () => { + const turn = (timestampMs: number, outputTokens: number) => + record({ + provider: "grok", + model: "grok-4.5", + timestampMs, + dedupeKey: `s:p:${timestampMs}:grok-4.5`, + reportedCostUsd: outputTokens / 10, + totals: { ...record().totals, uncachedInputTokens: 0, outputTokens }, + }); + const first = turn(Date.parse("2026-08-08T10:00:00Z"), 10); + const resumed = turn(Date.parse("2026-08-08T11:00:00Z"), 20); + const savedMaximum = { ...first, totals: resumed.totals, reportedCostUsd: 2 }; + + const expected = aggregate([first, resumed]).buckets; + + for (const order of [ + [first, resumed, savedMaximum], + [savedMaximum, first, resumed], + ]) { + const result = aggregate(order); + expect(result.duplicatesDropped).toBe(1); + expect(result.buckets).toEqual(expected); + } + }); + it("does not count a key whose first copy fell outside the window", () => { const result = aggregate([ record({ dedupeKey: "msg_1:", timestampMs: Date.parse("2026-07-01T12:00:00Z") }), @@ -251,6 +277,7 @@ describe("UsageAggregator", () => { untilDay: "2026-08-31", rates, modelAliases: resolveModelAliases({ "example-preview": "claude-fable-5" }), + rateModel: () => "claude-fable", }); aggregator.add(record()); aggregator.add(record({ model: "example-preview", rateModel: "example-preview-high" })); @@ -266,20 +293,24 @@ describe("UsageAggregator", () => { describe("resolveModelAliases", () => { it("follows chains to the final model and drops chains that enter a loop", () => { - expect( - resolveModelAliases({ + expect([ + ...resolveModelAliases({ "preview[1m]": "preview", preview: "example-model", + shared: "preview", + intoLoop: "loop", loop: "back", back: "loop", - intoLoop: "loop", + laterIntoLoop: "back", + deepIntoLoop: "intoLoop", self: "self", + validAfterLoops: "shared", }), - ).toEqual( - new Map([ - ["preview[1m]", "example-model"], - ["preview", "example-model"], - ]), - ); + ]).toEqual([ + ["preview[1m]", "example-model"], + ["preview", "example-model"], + ["shared", "example-model"], + ["validAfterLoops", "example-model"], + ]); }); }); diff --git a/apps/server/src/usage/usageAggregation.ts b/apps/server/src/usage/usageAggregation.ts index 9ff9cb9369462..f4c39753548fd 100644 --- a/apps/server/src/usage/usageAggregation.ts +++ b/apps/server/src/usage/usageAggregation.ts @@ -10,7 +10,7 @@ import type { } from "@t3tools/contracts"; import { EMPTY_TOTALS, type UsageRecord } from "@t3tools/provider-core/server/usage"; -import { cacheSavingsUsd, priceUsage, type RateTable } from "./usagePricing.ts"; +import { cacheSavingsUsd, lookupRate, priceUsage, type RateTable } from "./usagePricing.ts"; // en-CA gives ISO-ordered parts in the requested zone; Date getters are host-local. function makeDayFormatter(timeZone: string): (timestampMs: number) => string { @@ -70,6 +70,7 @@ export interface AggregateOptions { readonly rates: RateTable; readonly priceOverrides?: RateTable; readonly modelAliases?: ReadonlyMap; + readonly rateModel?: (provider: UsageRecord["provider"], model: string) => string | undefined; readonly modelFilter?: UsageModelFilter; readonly resolution?: UsageResolution; readonly sinceTimeMs?: number; @@ -79,15 +80,26 @@ export interface AggregateOptions { export function resolveModelAliases( aliases: Readonly>, ): ReadonlyMap { - const resolved = new Map(); - for (const model of Object.keys(aliases)) { - const seen = new Set([model]); - let target = aliases[model]!; - while (Object.hasOwn(aliases, target) && !seen.has(target)) { - seen.add(target); + const models = Object.keys(aliases); + const memo = new Map(); + for (const model of models) { + if (memo.has(model)) continue; + const path: string[] = []; + let target = model; + while (Object.hasOwn(aliases, target) && !memo.has(target)) { + path.push(target); + // Null marks both the active walk and paths that end in a cycle. + memo.set(target, null); target = aliases[target]!; } - if (!Object.hasOwn(aliases, target)) resolved.set(model, target); + const terminal = Object.hasOwn(aliases, target) ? (memo.get(target) ?? null) : target; + for (const alias of path) memo.set(alias, terminal); + } + + const resolved = new Map(); + for (const model of models) { + const target = memo.get(model); + if (target !== null && target !== undefined) resolved.set(model, target); } return resolved; } @@ -170,6 +182,30 @@ export function mergeUsageCopies(held: UsageRecord, copy: UsageRecord): UsageRec }; } +/** + * Grok reports each turn once, complete, so real copies are equal. A larger one is a deleted + * transcript's pre-v17 maximum over turns that shared a prompt id, whichever source it came from. + */ +function smallestGrokCopy(held: UsageRecord, copy: UsageRecord): UsageRecord { + const a = held.totals; + const b = copy.totals; + const reportedCostUsd = + held.reportedCostUsd === null || copy.reportedCostUsd === null + ? (held.reportedCostUsd ?? copy.reportedCostUsd) + : Math.min(held.reportedCostUsd, copy.reportedCostUsd); + return { + ...held, + totals: { + uncachedInputTokens: Math.min(a.uncachedInputTokens, b.uncachedInputTokens), + cachedInputTokens: Math.min(a.cachedInputTokens, b.cachedInputTokens), + cacheCreationTokens: Math.min(a.cacheCreationTokens, b.cacheCreationTokens), + outputTokens: Math.min(a.outputTokens, b.outputTokens), + reasoningTokens: Math.min(a.reasoningTokens, b.reasoningTokens), + }, + reportedCostUsd, + }; +} + interface Contribution { record: UsageRecord; /** Null when the first copy fell outside the window. */ @@ -217,7 +253,12 @@ export class UsageAggregator { const held = this.#seen.get(record.dedupeKey); if (held !== undefined) { this.#duplicatesDropped += 1; - if (held.bucket !== null) held.record = mergeUsageCopies(held.record, record); + if (held.bucket !== null) { + held.record = + record.provider === "grok" + ? smallestGrokCopy(held.record, record) + : mergeUsageCopies(held.record, record); + } return false; } } @@ -301,12 +342,16 @@ export class UsageAggregator { if (priced.costSource === "providerReported") bucket.providerReportedRecords += 1; } - // Aliases use the target's rate, replacing provider-specific rateModel. + // Aliases use the target's rate. Only an unpriced target goes through the provider's own rate naming. #mapModel(record: UsageRecord): UsageRecord { const model = this.#options.modelAliases?.get(record.model); if (model === undefined) return record; const { rateModel: _rateModel, ...rest } = record; - return { ...rest, model }; + const rateModel = + lookupRate(this.#options.rates, model) === null + ? this.#options.rateModel?.(record.provider, model) + : undefined; + return rateModel === undefined ? { ...rest, model } : { ...rest, model, rateModel }; } // Time-ordered records usually reuse the previous bucket; avoid rebuilding its key. diff --git a/apps/server/src/usage/usagePricing.ts b/apps/server/src/usage/usagePricing.ts index 21a1bd02bb178..a9ffc691643fd 100644 --- a/apps/server/src/usage/usagePricing.ts +++ b/apps/server/src/usage/usagePricing.ts @@ -310,6 +310,14 @@ export function lookupRate(table: RateTable, model: string): ModelRate | null { return rate; } +/** A priceable model the table has no rate for; a newer table may list it. */ +export function isUnlistedModel(table: RateTable, model: string): boolean { + const bareName = bareModelName(stripVariantSuffix(normalizeRateKey(model))); + return ( + bareName.length > 0 && !UNPRICEABLE_MODELS.has(bareName) && lookupRate(table, model) === null + ); +} + function resolveRate(table: RateTable, model: string): ModelRate | null { const key = stripVariantSuffix(normalizeRateKey(model)); const bareName = bareModelName(key); diff --git a/apps/server/src/usage/usageScanCache.test.ts b/apps/server/src/usage/usageScanCache.test.ts index 09348d5818a2e..36864768eb525 100644 --- a/apps/server/src/usage/usageScanCache.test.ts +++ b/apps/server/src/usage/usageScanCache.test.ts @@ -339,6 +339,29 @@ describe("scan cache round trip", () => { }); }); + it.each(["s:p:grok-4.5", "s:p:1786000000000:grok-4.5"])( + "re-parses v16 Grok transcripts with key %s", + (dedupeKey) => { + const original = cacheWith([ + ["/grok/updates.jsonl", 100, [record({ provider: "grok", model: "grok-4.5", dedupeKey })]], + ]); + original.set("/grok/updates.jsonl", { + ...original.get("/grok/updates.jsonl")!, + provider: "grok", + }); + const encoded = parse(makeScanCacheWriter()(original, {})); + const restored = decodeScanCache({ ...encoded, version: 16 }, TEST_FORMAT_MAP); + expect(restored.get("/grok/updates.jsonl")).toEqual({ + ...original.get("/grok/updates.jsonl"), + size: -1, + position: { resumeOffset: 0, guardLength: 0, guardHash: 0, state: null }, + records: [ + record({ provider: "grok", model: "grok-4.5", dedupeKey: "s:p:1786000000000:grok-4.5" }), + ], + }); + }, + ); + it("rewrites only changed entries and still restores the whole cache", () => { const write = makeScanCacheWriter(); const cache = cacheWith([ diff --git a/apps/server/src/usage/usageScanCache.ts b/apps/server/src/usage/usageScanCache.ts index e75691d2b8465..843b414b97f32 100644 --- a/apps/server/src/usage/usageScanCache.ts +++ b/apps/server/src/usage/usageScanCache.ts @@ -10,7 +10,7 @@ import { CLAUDE_SYNTHETIC_MODEL } from "../provider/Drivers/claudeUsage.ts"; import { mergeUsageCopies } from "./usageAggregation.ts"; import { GUARD_LENGTH, type TranscriptParsePosition } from "./usageTranscriptReader.ts"; -const USAGE_SCAN_CACHE_VERSION = 16 as const; +const USAGE_SCAN_CACHE_VERSION = 17 as const; const SPEED_COMPATIBLE_SINCE_VERSION = 4; /** @@ -18,8 +18,9 @@ const SPEED_COMPATIBLE_SINCE_VERSION = 4; * replace it, dropping saved usage for deleted transcripts. Separate files keep * the newer one; older files are removed once it is saved. */ -export const SCAN_CACHE_FILE_NAME = "usage-scan-cache-v16.json"; +export const SCAN_CACHE_FILE_NAME = "usage-scan-cache-v17.json"; export const LEGACY_SCAN_CACHE_FILE_NAMES = [ + "usage-scan-cache-v16.json", "usage-scan-cache-v15.json", "usage-scan-cache-v14.json", "usage-scan-cache-v13.json", @@ -264,7 +265,16 @@ export function decodeScanCache( ...(version >= 7 && unresolvedServiceTierTurnId != null ? { unresolvedServiceTierTurnId } : {}), - dedupeKey: typeof dedupeKey === "string" ? dedupeKey : null, + dedupeKey: + typeof dedupeKey !== "string" + ? null + : // Saved copies of moved transcripts must still match the re-parsed v17 key. + provider === "grok" && + version < 17 && + dedupeKey.endsWith(`:${model}`) && + !dedupeKey.endsWith(`:${timestampMs}:${model}`) + ? `${dedupeKey.slice(0, -model.length)}${timestampMs}:${model}` + : dedupeKey, }); } return records; @@ -301,8 +311,10 @@ export function decodeScanCache( // Keep history for deleted rollouts, but re-parse incompatible reducer state. // v13 adds Claude 1-hour cache writes; v14 uses Codex request ledgers. // v15 repairs skipped Codex requests whose counters rewound; clean v14 entries stay served. + // v17 keys Grok turns by time, restoring turns previously dropped as repeats. const legacy = (entry.p === "claude" && version < 13) || + (entry.p === "grok" && version < 17) || (format.state !== undefined && (version < 6 || (entry.p === "codex" && (version < 14 || (version < 15 && malformedRecords > 0))))); diff --git a/apps/server/src/usage/usageTranscriptStreaming.test.ts b/apps/server/src/usage/usageTranscriptStreaming.test.ts index 856ba2ac5b9d7..635b5ae4e6bec 100644 --- a/apps/server/src/usage/usageTranscriptStreaming.test.ts +++ b/apps/server/src/usage/usageTranscriptStreaming.test.ts @@ -254,7 +254,7 @@ describe("large usage records", () => { outputTokens: 99, reasoningTokens: 10, }, - dedupeKey: "s1:p1:grok-4.5-build", + dedupeKey: "s1:p1:1785578400123:grok-4.5-build", reportedCostUsd: 0.25, }); }); diff --git a/apps/server/src/vcs/GitVcsDriver.test.ts b/apps/server/src/vcs/GitVcsDriver.test.ts index 3e95a3658ef69..1acbf036fb52c 100644 --- a/apps/server/src/vcs/GitVcsDriver.test.ts +++ b/apps/server/src/vcs/GitVcsDriver.test.ts @@ -400,6 +400,49 @@ it.effect.each(["failure", "interrupt"] as const)( }).pipe(Effect.scoped, Effect.provide(layerGitContract)), ); +it.effect.each([ + { stop: "before git restore spawns", gate: "restore", index: "staged", file: "later\n" }, + { stop: "after git restore ran", gate: "clean", index: "head", file: "unstaged\n" }, +] as const)( + "checkpoint restore interrupted $stop leaves the $index index", + ({ gate, index, file }) => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const driver = yield* GitVcsDriver.makeVcsDriverShape(); + const liveProcess = yield* VcsProcess.VcsProcess; + const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-restore-interrupt-" }); + const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd); + yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef }); + yield* fs.writeFileString(path.join(cwd, "file.txt"), "later\n"); + yield* git(["add", "file.txt"]); + const stagedTree = (yield* git(["write-tree"])).stdout; + const entered = yield* Deferred.make(); + const gatedDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe( + Effect.provideService(VcsProcess.VcsProcess, { + run: (input) => + input.operation === "GitVcsDriver.checkpoints.restoreCheckpoint" && + input.args.includes(gate) + ? Deferred.succeed(entered, undefined).pipe(Effect.andThen(Effect.never)) + : liveProcess.run(input), + }), + ); + const fiber = yield* gatedDriver.checkpoints + .restoreCheckpoint({ cwd, checkpointRef }) + .pipe(Effect.exit, Effect.forkScoped); + yield* Deferred.await(entered); + yield* Fiber.interrupt(fiber); + const result = yield* Fiber.await(fiber); + assert.isTrue(Exit.isFailure(result) && Cause.hasInterruptsOnly(result.cause)); + + assert.strictEqual( + (yield* git(["write-tree"])).stdout, + index === "head" ? (yield* git(["rev-parse", "HEAD^{tree}"])).stdout : stagedTree, + ); + assert.strictEqual(yield* fs.readFileString(path.join(cwd, "file.txt")), file); + }).pipe(Effect.scoped, Effect.provide(layerGitContract)), +); + it.effect("checkpoint publication falls back to rename where hardlinks are unavailable", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; @@ -1519,6 +1562,8 @@ it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")( "write-tree", "commit-tree", "update-ref", + // Without a parsed top level, capture cannot rule out submodules. + "submodule", ]); assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "unstaged\n"); assert.deepEqual(yield* fileSystem.readFile(path.join(cwd, ".git", "index")), originalIndex); @@ -1757,6 +1802,73 @@ it.effect("sparse restore rewrites manual skip-worktree files and keeps exclusio }).pipe(Effect.scoped, Effect.provide(layerGitContract)), ); +it.effect("restores and deletes checkpoints inside populated submodules", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const driver = yield* GitVcsDriver.makeVcsDriverShape(); + const git = (cwd: string, args: ReadonlyArray) => + driver.execute({ operation: "test", cwd, args }); + const makeRepository = Effect.fn("makeRepository")(function* (prefix: string) { + const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix }); + yield* git(cwd, ["init"]); + yield* git(cwd, ["config", "user.email", "test@test.com"]); + yield* git(cwd, ["config", "user.name", "Test"]); + yield* fileSystem.writeFileString(path.join(cwd, "file.txt"), "original\n"); + yield* git(cwd, ["add", "."]); + yield* git(cwd, ["commit", "-m", "initial"]); + return cwd; + }); + const nested = yield* makeRepository("t3-submodule-nested-"); + const library = yield* makeRepository("t3-submodule-library-"); + const addSubmodule = (cwd: string, url: string, name: string) => + git(cwd, ["-c", "protocol.file.allow=always", "submodule", "add", url, name]).pipe( + Effect.andThen(git(cwd, ["commit", "-m", `add ${name}`])), + ); + yield* addSubmodule(library, nested, "nested"); + const root = yield* makeRepository("t3-submodule-root-"); + yield* addSubmodule(root, library, "library"); + yield* git(root, [ + "-c", + "protocol.file.allow=always", + "submodule", + "update", + "--init", + "--recursive", + ]); + const files = ["file.txt", "library/file.txt", "library/nested/file.txt"].map((file) => + path.join(root, file), + ); + const checkpointRef = CheckpointRef.make("refs/t3/checkpoints/submodules"); + yield* fileSystem.writeFileString(files[1]!, "captured\n"); + yield* driver.checkpoints.captureCheckpoint({ cwd: root, checkpointRef }); + + for (const file of files) { + yield* fileSystem.writeFileString(file, "changed\n"); + yield* fileSystem.writeFileString(`${file}.added`, "new\n"); + } + assert.isTrue( + yield* driver.checkpoints.restoreCheckpoint({ + cwd: root, + checkpointRef, + fallbackToHead: false, + }), + ); + + assert.deepStrictEqual( + yield* Effect.forEach(files, (file) => fileSystem.readFileString(file)), + ["original\n", "captured\n", "original\n"], + ); + for (const file of files) assert.isFalse(yield* fileSystem.exists(`${file}.added`)); + + yield* driver.checkpoints.deleteCheckpointRefs({ cwd: root, checkpointRefs: [checkpointRef] }); + for (const cwd of [root, path.join(root, "library"), path.join(root, "library", "nested")]) { + const ref = yield* git(cwd, ["for-each-ref", "refs/t3"]); + assert.strictEqual(ref.stdout, ""); + } + }).pipe(Effect.scoped, Effect.provide(layerGitContract)), +); + // The real VCS process, spawning `command` wherever the driver runs git. const vcsProcessSpawning = (command: string) => Layer.effect( diff --git a/apps/server/src/vcs/GitVcsDriver.ts b/apps/server/src/vcs/GitVcsDriver.ts index f51b008040904..16a96f508bc3f 100644 --- a/apps/server/src/vcs/GitVcsDriver.ts +++ b/apps/server/src/vcs/GitVcsDriver.ts @@ -5,6 +5,7 @@ import * as Context from "effect/Context"; import * as SecureRandom from "effect/crypto/SecureRandom"; import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; +import * as Exit from "effect/Exit"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; @@ -124,7 +125,10 @@ export interface ExecuteGitProgress { } export interface CreateWorktreeProgress { - /** Git refuses existing paths; claimed paths belong to this call and can be removed on cancel. */ + /** + * Runs uninterruptibly once the worktree exists; from then on the caller owns its cleanup. + * For new-branch calls on Git supporting add --reason, cancellation before the claim removes what it made. + */ readonly onWorktreeClaimed?: (path: string) => Effect.Effect; readonly onCheckoutProgress?: (input: { percent: number; @@ -335,6 +339,8 @@ export class GitVcsDriver extends Context.Service< cwd: string, options?: GitRemoteStatusOptions, ) => Effect.Effect; + /** Refs, HEAD and shallow boundaries. Null when the snapshot cannot be read. */ + readonly readRefSnapshot: (cwd: string) => Effect.Effect; readonly prepareCommitContext: ( cwd: string, filePaths?: readonly string[], @@ -571,6 +577,7 @@ const gitCommand = Effect.fn("GitVcsDriver.gitCommand")( args: ReadonlyArray, options?: { readonly stdin?: string; + readonly onSpawn?: () => void; readonly env?: NodeJS.ProcessEnv; readonly allowNonZeroExit?: boolean; readonly timeoutMs?: number; @@ -586,6 +593,7 @@ const gitCommand = Effect.fn("GitVcsDriver.gitCommand")( cwd, spawnCwd: globalThis.process.cwd(), ...(options?.stdin !== undefined ? { stdin: options.stdin } : {}), + ...(options?.onSpawn !== undefined ? { onSpawn: options.onSpawn } : {}), ...(options?.env !== undefined ? { env: options.env } : {}), ...(options?.allowNonZeroExit !== undefined ? { allowNonZeroExit: options.allowNonZeroExit } @@ -657,6 +665,7 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* ( const execute: VcsDriver.VcsDriver["Service"]["execute"] = (input) => gitCommand(vcsProcess, input.operation, input.cwd, input.args, { ...(input.stdin !== undefined ? { stdin: input.stdin } : {}), + ...(input.onSpawn !== undefined ? { onSpawn: input.onSpawn } : {}), ...(input.env !== undefined ? { env: input.env } : {}), ...(input.allowNonZeroExit !== undefined ? { allowNonZeroExit: input.allowNonZeroExit } : {}), ...(input.timeoutMs !== undefined ? { timeoutMs: input.timeoutMs } : {}), @@ -930,6 +939,7 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* ( "--git-common-dir", "--git-path", "index", + "--show-toplevel", "--show-prefix", "--verify", "--quiet", @@ -940,11 +950,12 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* ( const headExists = result.exitCode === 0; const headChecked = headExists || result.exitCode === 1; const lines = result.stdout.split("\n"); - const [gitCommonDir = "", indexPath = "", prefix = "", head = ""] = lines; - if (headChecked && lines.length === (headExists ? 5 : 4) && lines.at(-1) === "") { + const [gitCommonDir = "", indexPath = "", topLevel = "", prefix = "", head = ""] = lines; + if (headChecked && lines.length === (headExists ? 6 : 5) && lines.at(-1) === "") { return { gitCommonDir: gitCommonDir.trim(), indexPath: indexPath.trim(), + topLevel: topLevel.trim(), prefix, headExists, head: headExists ? head : undefined, @@ -961,12 +972,26 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* ( return { gitCommonDir: fallbackCommonDir, indexPath: undefined, + topLevel: undefined, prefix: prefixResult.stdout.replace(/\n$/, ""), headExists: headChecked ? headExists : yield* hasHeadCommit(cwd), head: undefined, }; }); + // $displaypath is relative to cwd, so "../" marks submodules outside a nested workspace. + const listSubmodules = Effect.fn("GitVcsDriver.listSubmodules")(function* (cwd: string) { + const result = yield* execute({ + operation: "GitVcsDriver.checkpoints.listSubmodules", + cwd, + args: ["submodule", "foreach", "--quiet", 'printf "%s\\0" "$displaypath"'], + }); + return result.stdout + .split("\0") + .filter((entry) => entry !== "" && !entry.startsWith("../")) + .map((entry) => path.join(cwd, entry)); + }); + // Git defaults and macOS writeout-only can leave empty refs after crashes; flush before publication. // Set fsync first: Git 2.36 ignores unknown batch without resetting the method. // Batch fully flushes refs and equals fsync on macOS HFS+/APFS and Windows NTFS/ReFS. @@ -1057,9 +1082,8 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* ( "-c", "splitIndex.sharedIndexExpire=never", ]; - const { gitCommonDir, indexPath, prefix, headExists, head } = yield* resolveCaptureRepository( - input.cwd, - ); + const { gitCommonDir, indexPath, topLevel, prefix, headExists, head } = + yield* resolveCaptureRepository(input.cwd); // Read before the snapshot and pinned to the same HEAD: a switch that lands while files are // being staged then belongs to the next checkpoint instead of being applied to this one. const headMoves = head === undefined ? [] : yield* readHeadMoves(input.cwd); @@ -1702,6 +1726,40 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* ( args: [...durableWrite, "update-ref", input.checkpointRef, commitOid], }); }).pipe(Effect.ensuring(cleanupTempIndex)); + + // The tree records only each submodule's HEAD, so submodules keep their own ref. + const hasSubmodules = + topLevel === undefined || + (yield* fileSystem + .exists(path.join(topLevel, ".gitmodules")) + .pipe(Effect.orElseSucceed(() => true))); + if (!hasSubmodules) return; + // A submodule that cannot be captured drops its ref, so restore leaves it alone. + yield* listSubmodules(input.cwd).pipe( + Effect.flatMap((submodules) => + Effect.forEach( + submodules, + (cwd) => + checkpoints + .captureCheckpoint({ cwd, checkpointRef: input.checkpointRef }) + .pipe( + Effect.catch((cause) => + Effect.logWarning("submodule checkpoint capture failed", { cwd, cause }).pipe( + Effect.andThen( + checkpoints + .deleteCheckpointRefs({ cwd, checkpointRefs: [input.checkpointRef] }) + .pipe(Effect.ignore), + ), + ), + ), + ), + { discard: true }, + ), + ), + Effect.catch((cause) => + Effect.logWarning("submodule checkpoint capture failed", { cwd: input.cwd, cause }), + ), + ); }, Effect.scoped), hasCheckpointRef: (input) => @@ -1727,15 +1785,48 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* ( cwd: input.cwd, args: ["ls-files", "--cached", `--with-tree=${commitOid}`, "-z", "--", "."], }); - // An empty index and checkpoint have nothing for git restore's pathspec to match. - if (tracked.stdout.length > 0) { - const sparseConfig = yield* execute({ + const sparseConfig = + tracked.stdout.length > 0 + ? yield* execute({ + operation, + cwd: input.cwd, + args: ["config", "--bool", "core.sparseCheckout"], + allowNonZeroExit: true, + }) + : null; + const sparseCheckout = sparseConfig?.stdout.trim() === "true"; + // Restoring away the last tracked file can remove a nested workspace directory. + const recreateWorkspace = fileSystem.makeDirectory(input.cwd, { recursive: true }).pipe( + Effect.mapError( + (cause) => + new VcsProcessExitError({ + operation, + command: "git restore", + cwd: input.cwd, + exitCode: 0, + detail: `Could not recreate the checkpoint workspace: ${cause.message}`, + }), + ), + ); + const resetIndex = Effect.gen(function* () { + // On Windows clean can remove './' and exit 0. + yield* recreateWorkspace; + yield* execute({ operation, cwd: input.cwd, - args: ["config", "--bool", "core.sparseCheckout"], - allowNonZeroExit: true, + args: [...longPaths, "reset", "--quiet", "--", "."], }); - const sparseCheckout = sparseConfig.stdout.trim() === "true"; + }); + let mutationStarted = false; + const onSpawn = () => { + mutationStarted = true; + }; + // Queued commands preserve user staging; spawned children stop before this scope resets it. + yield* Effect.addFinalizer((exit) => + mutationStarted && Exit.hasInterrupts(exit) ? resetIndex.pipe(Effect.ignore) : Effect.void, + ); + // An empty index and checkpoint have nothing for git restore's pathspec to match. + if (tracked.stdout.length > 0) { const restore = [...longPaths, "restore", "--source", commitOid, "--worktree", "--staged"]; // restore leaves skip-worktree paths alone. Outside a sparse checkout every such bit was // set by hand and capture recorded the file, so override them all. @@ -1743,6 +1834,7 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* ( operation, cwd: input.cwd, args: [...restore, ...(sparseCheckout ? [] : ["--ignore-skip-worktree-bits"]), "--", "."], + onSpawn, }); if (sparseCheckout) { // Sparse exclusions stay excluded; only skip-worktree paths the rules select are manual. @@ -1814,28 +1906,6 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* ( } } } - // Restoring away the last tracked file can remove a nested workspace directory. - const recreateWorkspace = fileSystem.makeDirectory(input.cwd, { recursive: true }).pipe( - Effect.mapError( - (cause) => - new VcsProcessExitError({ - operation, - command: "git restore", - cwd: input.cwd, - exitCode: 0, - detail: `Could not recreate the checkpoint workspace: ${cause.message}`, - }), - ), - ); - const resetIndex = Effect.gen(function* () { - // On Windows clean can remove './' and exit 0. - yield* recreateWorkspace; - yield* execute({ - operation, - cwd: input.cwd, - args: [...longPaths, "reset", "--quiet", "--", "."], - }); - }); yield* recreateWorkspace; const clean = execute({ operation, @@ -1843,6 +1913,7 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* ( args: [...longPaths, "clean", "-fd", "--", "."], env: { LC_ALL: "C" }, allowNonZeroExit: true, + onSpawn, }); let cleaned = yield* clean; // Removing a new .gitignore exposes the files it hid; clean again until nothing is left. @@ -1895,8 +1966,34 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* ( } yield* resetIndex; + // A submodule without this ref was not captured, and restoring it to HEAD would lose edits. + // The root is already restored, so a failing submodule must not hide that from the caller. + yield* listSubmodules(input.cwd).pipe( + Effect.flatMap((submodules) => + Effect.forEach( + submodules, + (cwd) => + checkpoints + .restoreCheckpoint({ + cwd, + checkpointRef: input.checkpointRef, + fallbackToHead: false, + }) + .pipe( + Effect.catch((cause) => + Effect.logWarning("submodule checkpoint restore failed", { cwd, cause }), + ), + ), + { discard: true }, + ), + ), + Effect.catch((cause) => + Effect.logWarning("submodule checkpoint restore failed", { cwd: input.cwd, cause }), + ), + ); + return true; - }), + }, Effect.scoped), diffCheckpoints: Effect.fn("GitVcsDriver.checkpoints.diffCheckpoints")(function* (input) { const operation = "GitVcsDriver.checkpoints.diffCheckpoints"; @@ -2102,6 +2199,10 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* ( }), { discard: true }, ); + const submodules = yield* listSubmodules(input.cwd).pipe(Effect.orElseSucceed(() => [])); + for (const cwd of submodules) { + yield* checkpoints.deleteCheckpointRefs({ cwd, checkpointRefs: input.checkpointRefs }); + } }, ), }; diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts index 07c3d8a21e47c..f8211e6e3450d 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.test.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts @@ -1,6 +1,8 @@ // Fork: Keep regression coverage for run each project as its own git account (#1). // @effect-diagnostics nodeBuiltinImport:off - realpathSync.native resolves Windows 8.3 short names, which the Effect realPath does not. +import * as NodeChildProcess from "node:child_process"; import * as NodeFS from "node:fs"; +import * as NodeNet from "node:net"; import * as NodePath from "node:path"; import * as NodeURL from "node:url"; import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; @@ -11,6 +13,7 @@ import { afterAll, vi } from "vite-plus/test"; import * as Cause from "effect/Cause"; import * as Clock from "effect/Clock"; import * as Deferred from "effect/Deferred"; +import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; import * as FileSystem from "effect/FileSystem"; @@ -49,6 +52,7 @@ import { windowsLongPathConfigEnv, } from "./GitVcsDriverCore.ts"; import * as GitVcsDriver from "./GitVcsDriver.ts"; +import { VcsAutomaticFetchInterval } from "./VcsAutomaticFetchInterval.ts"; import { gitConfigEnv } from "./VcsProcess.ts"; const encodeGitCommandError = Schema.encodeEffect(Schema.fromJsonString(GitCommandError)); @@ -343,6 +347,11 @@ const git = ( return result.stdout.trim(); }); +const [, gitMajor, gitMinor] = + /(\d+)\.(\d+)/u.exec(NodeChildProcess.execFileSync("git", ["--version"], { encoding: "utf8" })) ?? + []; +const gitHasReftable = Number(gitMajor) > 2 || (Number(gitMajor) === 2 && Number(gitMinor) >= 45); + const gitWithStdin = ( cwd: string, args: ReadonlyArray, @@ -389,14 +398,15 @@ const initRepoWithCommit = ( return { initialBranch: head.trim().replace(/^ref: refs\/heads\//, "") }; }); -const commitInitialRepo = (cwd: string) => +const commitInitialRepo = (cwd: string, initArgs: ReadonlyArray = []) => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; const pathService = yield* Path.Path; - yield* git(cwd, ["init"]); + yield* git(cwd, ["init", ...initArgs]); + // Newer Git detaches auto-maintenance after commit; its lock file would race the template copy. yield* fileSystem.writeFileString( pathService.join(cwd, ".git", "config"), - "[user]\n\temail = test@test.com\n\tname = Test\n", + "[user]\n\temail = test@test.com\n\tname = Test\n[maintenance]\n\tauto = false\n", { flag: "a" }, ); yield* writeTextFile(cwd, "README.md", "# test\n"); @@ -551,7 +561,10 @@ it.effect.each([ assert.equal(created.failure.exitCode, hookExitCode); } const metadataQueries = spawnedArgs.filter((args) => args.includes("--git-common-dir")); - assert.deepStrictEqual(metadataQueries, [["rev-parse", "--git-common-dir"]]); + assert.deepStrictEqual( + metadataQueries, + Array.from({ length: 2 }, () => ["rev-parse", "--git-common-dir"]), + ); assert.isFalse(spawnedArgs.some((args) => args.includes("symbolic-ref"))); invalidationFailure = "none"; @@ -614,7 +627,7 @@ it.effect.each(["branch", "detached", "nested", "aliased-root", "aliased-nested" newRefName: "feature/cold", }); - assert.equal(spawnedArgs.filter((args) => args.includes("--git-common-dir")).length, 1); + assert.equal(spawnedArgs.filter((args) => args.includes("--git-common-dir")).length, 2); assert.isFalse(spawnedArgs.some((args) => args.includes("--show-toplevel"))); assert.isFalse(spawnedArgs.some((args) => args.includes("symbolic-ref"))); spawnedArgs.length = 0; @@ -676,7 +689,10 @@ it.effect.each(["bare", "unborn"] as const)( assert.equal(result.failure.operation, "GitVcsDriver.createWorktree.createBranch"); assert.equal(result.failure.detail, "git branch failed"); } - assert.equal(spawnedArgs.filter((args) => args.includes("--git-common-dir")).length, 1); + assert.equal( + spawnedArgs.filter((args) => args.includes("--git-common-dir")).length, + state === "bare" ? 2 : 1, + ); assert.isFalse(spawnedArgs.some((args) => args[0] === "symbolic-ref")); spawnedArgs.length = 0; const refs = yield* driver.listRefs({ cwd }); @@ -1109,6 +1125,287 @@ describe("pullCurrentBranch", () => { }; }); + it.effect("resolves a status waiter when Pull claims its queued fetch", () => + Effect.scoped( + Effect.gen(function* () { + const { cwd, remoteHead } = yield* cloneBehindRemote; + const blockers: string[] = []; + for (let index = 0; index < 4; index++) { + const blocker = yield* cloneBehindRemote; + blockers.push(NodeFS.realpathSync.native(blocker.cwd)); + } + const targetPath = NodeFS.realpathSync.native(cwd); + const delegate = yield* ChildProcessSpawner.ChildProcessSpawner; + const release = yield* Deferred.make(); + const blockersStarted = yield* Deferred.make(); + const targetEntered = yield* Deferred.make(); + let running = 0; + let targetFetches = 0; + let waiterSucceeded = false; + const spawner = ChildProcessSpawner.make((command) => + Effect.gen(function* () { + const handle = yield* delegate.spawn(command); + if (!ChildProcess.isStandardCommand(command) || !command.args.includes("fetch")) + return handle; + const commandCwd = String(command.options.cwd); + if (commandCwd === targetPath) targetFetches++; + if (blockers.includes(commandCwd)) { + running++; + if (running === 4) yield* Deferred.succeed(blockersStarted, undefined); + yield* Deferred.await(release); + } + return handle; + }), + ); + const driver = yield* makeGitVcsDriverCore().pipe( + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), + ); + const tracer = Tracer.make({ + span(options) { + const span = new Tracer.NativeSpan(options); + if (options.name === "GitVcsDriver.fetchRemoteForStatus.wait") { + const attribute = span.attribute.bind(span); + span.attribute = (key, value) => { + attribute(key, value); + if (key === "statusFetch.joining") Deferred.doneUnsafe(targetEntered, Effect.void); + }; + const end = span.end.bind(span); + span.end = (endTime, exit) => { + waiterSucceeded = Exit.isSuccess(exit); + end(endTime, exit); + }; + } + return span; + }, + }); + yield* Effect.gen(function* () { + for (const blocker of blockers) { + yield* driver.statusDetailsRemote(blocker).pipe(Effect.forkChild); + } + yield* Deferred.await(blockersStarted); + const status = yield* driver + .statusDetailsRemote(cwd) + .pipe(Effect.withTracer(tracer), Effect.forkChild({ startImmediately: true })); + yield* Deferred.await(targetEntered); + const pull = yield* driver.pullCurrentBranch(cwd); + const refreshed = yield* Fiber.join(status); + assert.equal(pull.status, "pulled"); + assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), remoteHead); + assert.isTrue(waiterSucceeded); + assert.equal(refreshed.behindCount, 0); + assert.equal(targetFetches, 0); + }).pipe(Effect.ensuring(Deferred.succeed(release, undefined))); + }), + ).pipe(Effect.provide(layerTest)), + ); + + const claimedFetchFallbacks = ["diverged", "interrupted", "upstream-only"] as const; + + it.effect.each(claimedFetchFallbacks)( + "falls back to the claimed status fetch when Pull is %s", + (failure) => + Effect.scoped( + Effect.gen(function* () { + const { cwd, updater, remoteHead } = yield* cloneBehindRemote; + if (failure === "upstream-only") { + yield* git(updater, ["push", "origin", "HEAD:refs/heads/other"]); + yield* writeTextFile(cwd, ".git/refs/remotes/origin/other.lock", ""); + } + if (failure === "diverged") { + yield* writeTextFile(cwd, "local.txt", "local\n"); + yield* git(cwd, ["add", "local.txt"]); + yield* git(cwd, ["commit", "-m", "local commit"]); + } + const blockers: string[] = []; + for (let index = 0; index < 4; index++) { + const blocker = yield* cloneBehindRemote; + blockers.push(NodeFS.realpathSync.native(blocker.cwd)); + } + const targetPath = NodeFS.realpathSync.native(cwd); + const delegate = yield* ChildProcessSpawner.ChildProcessSpawner; + const release = yield* Deferred.make(); + const blockersStarted = yield* Deferred.make(); + const targetEntered = yield* Deferred.make(); + const targetFetchStarted = yield* Deferred.make(); + const pullBeforeFetch = yield* Deferred.make(); + const releasePull = yield* Deferred.make(); + let running = 0; + let targetFetches = 0; + let waiterSucceeded = false; + let targetFetchExitCode: number | undefined; + const spawner = ChildProcessSpawner.make((command) => + Effect.gen(function* () { + if (ChildProcess.isStandardCommand(command)) { + const commandCwd = NodeFS.realpathSync.native(String(command.options.cwd)); + if ( + commandCwd === targetPath && + failure === "interrupted" && + command.args[0] === "rev-parse" && + command.args[1] === "HEAD" && + command.args.length === 2 + ) { + yield* Deferred.succeed(pullBeforeFetch, undefined); + yield* Deferred.await(releasePull); + } + } + const handle = yield* delegate.spawn(command); + if (!ChildProcess.isStandardCommand(command) || !command.args.includes("fetch")) + return handle; + const commandCwd = String(command.options.cwd); + if (commandCwd === targetPath) { + targetFetches++; + yield* Deferred.succeed(targetFetchStarted, undefined); + return ChildProcessSpawner.makeHandle({ + ...handle, + exitCode: handle.exitCode.pipe( + Effect.tap((exitCode) => + Effect.sync(() => { + targetFetchExitCode = exitCode; + }), + ), + ), + }); + } + if (blockers.includes(commandCwd)) { + running++; + if (running === 4) yield* Deferred.succeed(blockersStarted, undefined); + yield* Deferred.await(release); + } + return handle; + }), + ); + const driver = yield* makeGitVcsDriverCore().pipe( + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), + ); + const tracer = Tracer.make({ + span(options) { + const span = new Tracer.NativeSpan(options); + if (options.name === "GitVcsDriver.fetchRemoteForStatus.wait") { + const attribute = span.attribute.bind(span); + span.attribute = (key, value) => { + attribute(key, value); + if (key === "statusFetch.joining") + Deferred.doneUnsafe(targetEntered, Effect.void); + }; + const end = span.end.bind(span); + span.end = (endTime, exit) => { + waiterSucceeded = Exit.isSuccess(exit); + end(endTime, exit); + }; + } + return span; + }, + }); + yield* Effect.gen(function* () { + for (const blocker of blockers) { + yield* driver.statusDetailsRemote(blocker).pipe(Effect.forkChild); + } + yield* Deferred.await(blockersStarted); + const status = yield* driver + .statusDetailsRemote(cwd) + .pipe(Effect.withTracer(tracer), Effect.forkChild({ startImmediately: true })); + yield* Deferred.await(targetEntered); + if (failure === "interrupted") { + const pull = yield* driver.pullCurrentBranch(cwd).pipe(Effect.forkChild); + yield* Deferred.await(pullBeforeFetch); + yield* Fiber.interrupt(pull); + const exit = yield* Fiber.await(pull); + assert.isTrue(Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause)); + } else if (failure === "upstream-only") { + assert.equal((yield* driver.pullCurrentBranch(cwd)).status, "pulled"); + yield* (yield* FileSystem.FileSystem).remove( + `${cwd}/.git/refs/remotes/origin/other.lock`, + ); + } else { + const error = yield* Effect.flip(driver.pullCurrentBranch(cwd)); + assert.equal(error.operation, "GitVcsDriver.pullCurrentBranch.pull"); + } + assert.equal(targetFetches, 0); + yield* Deferred.succeed(release, undefined); + yield* Deferred.await(targetFetchStarted); + assert.isTrue((yield* Fiber.join(status)).isRepo); + assert.isTrue(waiterSucceeded); + assert.equal(targetFetches, 1); + assert.equal(targetFetchExitCode, 0); + if (failure === "upstream-only") { + assert.equal(yield* git(cwd, ["rev-parse", "refs/remotes/origin/other"]), remoteHead); + } + }).pipe( + Effect.ensuring( + Effect.all([ + Deferred.succeed(release, undefined), + Deferred.succeed(releasePull, undefined), + ]), + ), + ); + }), + ).pipe(Effect.provide(layerTest)), + ); + + it.effect("waits for a running status fetch before starting Pull", () => + Effect.scoped( + Effect.gen(function* () { + const { cwd, remoteHead } = yield* cloneBehindRemote; + const targetPath = NodeFS.realpathSync.native(cwd); + const delegate = yield* ChildProcessSpawner.ChildProcessSpawner; + const fetchStarted = yield* Deferred.make(); + const releaseFetch = yield* Deferred.make(); + const pullResolvedCommonDir = yield* Deferred.make(); + const pullStarted = yield* Deferred.make(); + const events: string[] = []; + const spawner = ChildProcessSpawner.make((command) => + Effect.gen(function* () { + if (!ChildProcess.isStandardCommand(command)) return yield* delegate.spawn(command); + const target = NodeFS.realpathSync.native(String(command.options.cwd)) === targetPath; + if (target && command.args.includes("pull")) { + events.push("pull-spawn"); + yield* Deferred.succeed(pullStarted, undefined); + } + const handle = yield* delegate.spawn(command); + if (target && command.args.includes("fetch")) { + yield* Deferred.succeed(fetchStarted, undefined); + yield* Deferred.await(releaseFetch); + yield* handle.exitCode; + events.push("fetch-exit"); + } + return handle; + }), + ); + const driver = yield* makeGitVcsDriverCore().pipe( + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), + ); + const tracer = Tracer.make({ + span(options) { + const span = new Tracer.NativeSpan(options); + if (options.name === "resolveGitCommonDir") { + const end = span.end.bind(span); + span.end = (endTime, exit) => { + end(endTime, exit); + Deferred.doneUnsafe(pullResolvedCommonDir, Effect.void); + }; + } + return span; + }, + }); + yield* Effect.gen(function* () { + yield* driver.statusDetailsRemote(cwd).pipe(Effect.forkChild); + yield* Deferred.await(fetchStarted); + const pull = yield* driver + .pullCurrentBranch(cwd) + .pipe(Effect.withTracer(tracer), Effect.forkChild({ startImmediately: true })); + yield* Deferred.await(pullResolvedCommonDir); + assert.isFalse(yield* Deferred.isDone(pullStarted)); + assert.isUndefined(pull.pollUnsafe()); + yield* Deferred.succeed(releaseFetch, undefined); + const result = yield* Fiber.join(pull); + assert.equal(result.status, "pulled"); + assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), remoteHead); + assert.deepEqual(events, ["fetch-exit", "pull-spawn"]); + }).pipe(Effect.ensuring(Deferred.succeed(releaseFetch, undefined))); + }), + ).pipe(Effect.provide(layerTest)), + ); + it.effect("still fetches other branches on the first pull", () => Effect.gen(function* () { const driver = yield* GitVcsDriver.GitVcsDriver; @@ -1555,6 +1852,53 @@ it.effect("reads worktrees from git that rejects worktree list -z", () => ), ); +it.effect("commits selected files with git that rejects --pathspec-from-file", () => + Effect.scoped( + Effect.gen(function* () { + const delegate = yield* ChildProcessSpawner.ChildProcessSpawner; + // Git before 2.25 has no `add --pathspec-from-file` and exits with a usage error. + const oldGitSpawner = ChildProcessSpawner.make((command) => { + if (!ChildProcess.isStandardCommand(command)) { + return Effect.die("expected a standard Git command"); + } + if (command.args.at(-1) === "version") { + return Effect.succeed(makeSuccessfulHandle("git version 2.24.4\n")); + } + if (command.args.some((arg) => String(arg).startsWith("--pathspec-from-file"))) { + return Effect.succeed(makeFailedHandle("error: unknown option `pathspec-from-file=-'")); + } + return delegate.spawn(command); + }); + const driver = yield* makeGitVcsDriverCore().pipe( + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, oldGitSpawner), + ); + yield* Effect.gen(function* () { + const cwd = yield* makeTmpDir(); + yield* initRepoWithCommit(cwd); + const selected = ["selected[1].txt", "-dash.txt", "with space.txt"]; + for (const file of selected) yield* writeTextFile(cwd, file, "selected\n"); + yield* writeTextFile(cwd, "left.txt", "left\n"); + + yield* driver.commit(cwd, "Selected", "", { stage: { filePaths: selected } }); + + assert.sameMembers( + (yield* git(cwd, ["show", "--pretty=", "--name-only", "-z", "HEAD"])) + .split("\0") + .filter(Boolean), + selected, + ); + assert.equal(yield* git(cwd, ["status", "--porcelain"]), "?? left.txt"); + }).pipe(Effect.provideService(GitVcsDriver.GitVcsDriver, driver)); + }), + ).pipe( + Effect.provide( + layerServerConfig.pipe( + Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), + ), + ), + ), +); + it.effect("does not report a separate git dir as the worktree of its branch", () => Effect.scoped( Effect.gen(function* () { @@ -1772,6 +2116,37 @@ it.effect("does not fetch for status when the branch tracks a local branch", () ).pipe(Effect.provide(layerTest)), ); +it.effect("does not fetch for status when the automatic fetch interval is zero", () => + Effect.scoped( + Effect.gen(function* () { + const delegate = yield* ChildProcessSpawner.ChildProcessSpawner; + const fetches: Array> = []; + const spawner = ChildProcessSpawner.make((command) => { + if (ChildProcess.isStandardCommand(command) && command.args.includes("fetch")) { + fetches.push(command.args.map(String)); + } + return delegate.spawn(command); + }); + const driver = yield* makeGitVcsDriverCore().pipe( + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), + Effect.provideService(VcsAutomaticFetchInterval, Effect.succeed(Duration.zero)), + ); + const cwd = yield* makeTmpDir(); + const remote = yield* makeTmpDir("git-vcs-driver-remote-"); + const { initialBranch } = yield* initRepoWithCommit(cwd); + yield* git(remote, ["init", "--bare"]); + yield* git(cwd, ["remote", "add", "origin", remote]); + yield* git(cwd, ["push", "--set-upstream", "origin", initialBranch]); + + yield* driver.statusDetails(cwd); + const status = yield* driver.statusDetailsRemote(cwd); + + assert.equal(status.upstreamRef, `origin/${initialBranch}`); + assert.deepStrictEqual(fetches, []); + }), + ).pipe(Effect.provide(layerTest)), +); + it.effect("lets a worktree checkout that is still working run past five minutes", () => Effect.gen(function* () { const checkoutStarted = yield* Deferred.make(); @@ -1780,7 +2155,8 @@ it.effect("lets a worktree checkout that is still working run past five minutes" Effect.gen(function* () { if (!ChildProcess.isStandardCommand(command)) return yield* Effect.die("expected Git command"); - if (!command.args.includes("worktree")) return makeSuccessfulHandle(""); + if (!command.args.includes("worktree") || command.args.includes("-h")) + return makeSuccessfulHandle(""); yield* Deferred.succeed(checkoutStarted, undefined); return ChildProcessSpawner.makeHandle({ ...makeSuccessfulHandle(""), @@ -2230,6 +2606,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { const spawner = ChildProcessSpawner.make((command) => { if ( ChildProcess.isStandardCommand(command) && + !command.args.includes("-h") && (command.args.includes("worktree") || command.args[0] === "branch" || command.args[0] === "checkout") @@ -2878,7 +3255,13 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { }); const source = dirty.sources.find((source) => source.kind === "working-tree")!; assert.deepStrictEqual(source.files, [ - { path: "untracked.txt", previousPath: null, additions: 1, deletions: 0 }, + { + path: "untracked.txt", + previousPath: null, + changeType: "new", + additions: 1, + deletions: 0, + }, ]); assert.include(source.diff, "+untracked content"); }), @@ -2984,7 +3367,13 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { assert.isTrue(metadata.sources[0]!.patchesDeferred); assert.isEmpty(metadata.sources[0]!.diff); assert.deepStrictEqual(metadata.sources[0]!.files, [ - { path: "README.md", previousPath: null, additions: 1, deletions: 1 }, + { + path: "README.md", + previousPath: null, + changeType: "change", + additions: 1, + deletions: 1, + }, ]); }), ); @@ -3030,6 +3419,35 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { }), ); + it.effect("keeps the review working beside a nested repository with no commits", () => + Effect.gen(function* () { + const cwd = yield* makeTmpDir(); + yield* initRepoWithCommit(cwd); + const driver = yield* GitVcsDriver.GitVcsDriver; + yield* git(cwd, ["init", "scaffold"]); + const committed = `${cwd}/committed`; + yield* (yield* FileSystem.FileSystem).makeDirectory(committed); + yield* initRepoWithCommit(committed); + yield* writeTextFile(cwd, "README.md", "# tracked change\n"); + yield* writeTextFile(cwd, "untracked.txt", "untracked contents\n"); + + const preview = yield* driver.getReviewDiffPreview({ cwd, ignoreWhitespace: false }); + const diff = preview.sources.find((source) => source.kind === "working-tree")?.diff ?? ""; + + assert.include(diff, "+# tracked change"); + assert.include(diff, "+untracked contents"); + assert.notInclude(diff, "scaffold"); + assert.include(diff, "diff --git a/committed b/committed"); + + const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true }); + assert.deepStrictEqual(status.branchChanges, { + baseRef: null, + insertions: 3, + deletions: 1, + }); + }), + ); + it.effect("keeps untracked filenames with pathspec magic in the review", () => Effect.gen(function* () { const cwd = yield* makeTmpDir(); @@ -3054,7 +3472,13 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { }); const scopedSource = scoped.sources.find((source) => source.kind === "working-tree")!; assert.deepStrictEqual(scopedSource.files, [ - { path: ":(exclude)after.ts", previousPath: null, additions: 1, deletions: 0 }, + { + path: ":(exclude)after.ts", + previousPath: null, + changeType: "new", + additions: 1, + deletions: 0, + }, ]); assert.include(scopedSource.diff, "+literal pathspec contents"); assert.notInclude(scopedSource.diff, "ordinary.ts"); @@ -3077,7 +3501,13 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { const source = preview.sources.find((candidate) => candidate.kind === "working-tree")!; assert.deepStrictEqual(source.files, [ - { path: "excluded/new.ts", previousPath: null, additions: 1, deletions: 0 }, + { + path: "excluded/new.ts", + previousPath: null, + changeType: "new", + additions: 1, + deletions: 0, + }, ]); assert.include(source.diff, "+outside the cone"); }), @@ -3120,7 +3550,13 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { const source = preview.sources.find((candidate) => candidate.kind === "working-tree")!; assert.deepStrictEqual(source.files, [ - { path: "after.ts", previousPath: "before.ts", additions: 1, deletions: 1 }, + { + path: "after.ts", + previousPath: "before.ts", + changeType: "rename-changed", + additions: 1, + deletions: 1, + }, ]); const scoped = yield* driver.getReviewDiffPreview({ cwd, @@ -3240,7 +3676,15 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { const preview = yield* driver.getReviewDiffPreview({ cwd }); assert.deepStrictEqual( preview.sources.find((source) => source.kind === "working-tree")!.files, - [{ path: "untracked.txt", previousPath: null, additions: 1, deletions: 0 }], + [ + { + path: "untracked.txt", + previousPath: null, + changeType: "new", + additions: 1, + deletions: 0, + }, + ], ); assert.strictEqual(yield* fs.readFileString(path.join(cwd, ".git/filter-runs")), ""); assert.deepStrictEqual(yield* fs.readFile(indexPath), originalIndex); @@ -3280,8 +3724,20 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { const preview = yield* driver.getReviewDiffPreview({ cwd }); const dirty = preview.sources.find((source) => source.kind === "working-tree")!; assert.deepStrictEqual(dirty.files, [ - { path: "tracked.txt", previousPath: null, additions: 1, deletions: 1 }, - { path: "untracked.txt", previousPath: null, additions: 1, deletions: 0 }, + { + path: "tracked.txt", + previousPath: null, + changeType: "change", + additions: 1, + deletions: 1, + }, + { + path: "untracked.txt", + previousPath: null, + changeType: "new", + additions: 1, + deletions: 0, + }, ]); assert.include(dirty.diff, "-before"); assert.include(dirty.diff, "+after!"); @@ -3345,14 +3801,44 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { } assert.deepStrictEqual(branch.files, [ - { path: "a-large.txt", previousPath: null, additions: 4000, deletions: 0 }, - { path: "untracked.txt", previousPath: null, additions: 4000, deletions: 0 }, - { path: "z-last.txt", previousPath: null, additions: 1, deletions: 0 }, + { + path: "a-large.txt", + previousPath: null, + changeType: "new", + additions: 4000, + deletions: 0, + }, + { + path: "untracked.txt", + previousPath: null, + changeType: "new", + additions: 4000, + deletions: 0, + }, + { path: "z-last.txt", previousPath: null, changeType: "new", additions: 1, deletions: 0 }, ]); assert.deepStrictEqual(dirty.files, [ - { path: "a-large.txt", previousPath: null, additions: 4000, deletions: 4000 }, - { path: "untracked.txt", previousPath: null, additions: 4000, deletions: 0 }, - { path: "z-last.txt", previousPath: null, additions: 1, deletions: 1 }, + { + path: "a-large.txt", + previousPath: null, + changeType: "change", + additions: 4000, + deletions: 4000, + }, + { + path: "untracked.txt", + previousPath: null, + changeType: "new", + additions: 4000, + deletions: 0, + }, + { + path: "z-last.txt", + previousPath: null, + changeType: "change", + additions: 1, + deletions: 1, + }, ]); }), ); @@ -3363,10 +3849,12 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { const { initialBranch } = yield* initRepoWithCommit(cwd); const driver = yield* GitVcsDriver.GitVcsDriver; yield* writeTextFile(cwd, "mode-only.sh", "echo unchanged\n"); - yield* git(cwd, ["add", "mode-only.sh"]); + yield* writeTextFile(cwd, "removed.txt", "removed\n"); + yield* git(cwd, ["add", "mode-only.sh", "removed.txt"]); yield* git(cwd, ["commit", "-m", "add executable candidate"]); yield* git(cwd, ["checkout", "-b", "feature/paths"]); yield* git(cwd, ["mv", "README.md", "renamed.md"]); + yield* git(cwd, ["rm", "removed.txt"]); yield* writeTextFile(cwd, "[literal].txt", "literal\n"); yield* writeTextFile(cwd, " leading.txt", "whitespace path\n"); yield* writeTextFile(cwd, "l.txt", "other\n"); @@ -3406,12 +3894,28 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { assert.deepInclude(branch.files ?? [], { path: "renamed.md", previousPath: "README.md", + changeType: "rename-pure", + additions: 0, + deletions: 0, + }); + assert.deepInclude(branch.files ?? [], { + path: "removed.txt", + previousPath: null, + changeType: "deleted", + additions: 0, + deletions: 1, + }); + assert.deepInclude(branch.files ?? [], { + path: "mode-only.sh", + previousPath: null, + changeType: "change", additions: 0, deletions: 0, }); assert.deepInclude(branch.files ?? [], { path: "binary.dat", previousPath: null, + changeType: "new", additions: 0, deletions: 0, }); @@ -3419,6 +3923,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { assert.deepInclude(branch.files ?? [], { path: "tab\tand\nnewline.txt", previousPath: null, + changeType: "new", additions: 1, deletions: 0, }); @@ -3437,8 +3942,14 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { const preview = yield* driver.getReviewDiffPreview({ cwd }); const dirty = preview.sources.find((source) => source.kind === "working-tree")!; assert.deepStrictEqual(dirty.files, [ - { path: "staged.txt", previousPath: null, additions: 1, deletions: 0 }, - { path: "untracked.txt", previousPath: null, additions: 1, deletions: 0 }, + { path: "staged.txt", previousPath: null, changeType: "new", additions: 1, deletions: 0 }, + { + path: "untracked.txt", + previousPath: null, + changeType: "new", + additions: 1, + deletions: 0, + }, ]); assert.include(dirty.diff, "b/staged.txt"); assert.include(dirty.diff, "b/untracked.txt"); @@ -3622,38 +4133,127 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { }), ); - it.effect("Changes combines commits, uncommitted edits, and untracked files", () => + it.effect("loads working-tree contents as Git diffs them under the ident attribute", () => Effect.gen(function* () { const cwd = yield* makeTmpDir(); - const { initialBranch } = yield* initRepoWithCommit(cwd); + yield* initRepoWithCommit(cwd); const driver = yield* GitVcsDriver.GitVcsDriver; - const sourceFiles = ( - preview: ReviewDiffPreviewResult, - kind: "working-tree" | "branch-range", - ) => preview.sources.find((source) => source.kind === kind)!.files; - yield* git(cwd, ["checkout", "-b", "feature/combined"]); - yield* writeTextFile(cwd, "README.md", "# test\ncommitted\n"); - yield* git(cwd, ["commit", "-am", "commit edit"]); - - const clean = yield* driver.getReviewDiffPreview({ cwd, baseRef: initialBranch }); - assert.deepStrictEqual(sourceFiles(clean, "working-tree"), []); - assert.deepStrictEqual(sourceFiles(clean, "branch-range"), [ - { path: "README.md", previousPath: null, additions: 1, deletions: 0 }, - ]); + const fileSystem = yield* FileSystem.FileSystem; + const filePath = (yield* Path.Path).join(cwd, "README.md"); + const committed = "$Id$\nfirst\nsecond\n"; + yield* writeTextFile(cwd, ".gitattributes", "README.md ident\n"); + yield* writeTextFile(cwd, "README.md", committed); + yield* writeTextFile(cwd, "PLAIN.md", "plain\n"); + yield* git(cwd, ["add", "."]); + yield* git(cwd, ["commit", "-m", "add ident keyword"]); + // Checkout smudges the keyword to `$Id: $`. + yield* fileSystem.remove(filePath); + yield* git(cwd, ["checkout-index", "--force", "README.md"]); + const smudged = yield* fileSystem.readFileString(filePath); + assert.match(smudged, /^\$Id: [0-9a-f]{40} \$\n/); + yield* writeTextFile(cwd, "README.md", smudged.replace("first", "edited")); + yield* writeTextFile(cwd, "PLAIN.md", "$Id: literal $\n"); + const disk = yield* fileSystem.readFile(filePath); - yield* writeTextFile(cwd, "README.md", "# test\ncommitted\ndirty\n"); - yield* writeTextFile(cwd, "new.txt", "new\n"); + const contents = yield* driver.getReviewDiffFileContents( + makeReviewDiffFileContentsInput(cwd), + ); + const plain = yield* driver.getReviewDiffFileContents( + makeReviewDiffFileContentsInput(cwd, { oldPath: "PLAIN.md", newPath: "PLAIN.md" }), + ); + + assert.deepStrictEqual(contents, { + oldContents: committed, + newContents: "$Id$\nedited\nsecond\n", + }); + assert.strictEqual(plain.newContents, "$Id: literal $\n"); + yield* writeTextFile(cwd, "README.md", "$Id: x $Id: y $\n"); + const sharedDollar = yield* driver.getReviewDiffFileContents( + makeReviewDiffFileContentsInput(cwd), + ); + assert.strictEqual(sharedDollar.newContents, "$Id$Id: y $\n"); + yield* git(cwd, ["config", "filter.unrelated.clean", "touch filter-ran; cat"]); + yield* writeTextFile(cwd, ".gitattributes", "README.md ident text eol=lf\n"); + yield* writeTextFile( + cwd, + "README.md", + smudged.replace("first", "edited").replaceAll("\n", "\r\n"), + ); + const crlf = yield* driver.getReviewDiffFileContents(makeReviewDiffFileContentsInput(cwd)); + assert.strictEqual(crlf.newContents, "$Id$\r\nedited\r\nsecond\r\n"); + yield* fileSystem.writeFile(filePath, disk); + yield* writeTextFile(cwd, ".gitattributes", "README.md ident=set\n"); + const literal = yield* driver.getReviewDiffFileContents( + makeReviewDiffFileContentsInput(cwd), + ); + assert.strictEqual(literal.newContents, smudged.replace("first", "edited")); + yield* writeTextFile(cwd, ".gitattributes", "README.md ident filter=probe\n"); + yield* git(cwd, ["config", "filter.probe.clean", "touch filter-ran; cat"]); + const filtered = yield* driver.getReviewDiffFileContents( + makeReviewDiffFileContentsInput(cwd), + ); + assert.strictEqual(filtered.newContents, literal.newContents); + yield* writeTextFile(cwd, ".gitattributes", "README.md ident filter=unspecified\n"); + yield* git(cwd, ["config", "filter.unspecified.clean", "touch filter-ran; cat"]); + const reservedFilter = yield* driver.getReviewDiffFileContents( + makeReviewDiffFileContentsInput(cwd), + ); + assert.strictEqual(reservedFilter.newContents, literal.newContents); + assert.isFalse(yield* fileSystem.exists((yield* Path.Path).join(cwd, "filter-ran"))); + assert.deepStrictEqual(yield* fileSystem.readFile(filePath), disk); + }), + ); + + it.effect("Changes combines commits, uncommitted edits, and untracked files", () => + Effect.gen(function* () { + const cwd = yield* makeTmpDir(); + const { initialBranch } = yield* initRepoWithCommit(cwd); + const driver = yield* GitVcsDriver.GitVcsDriver; + const sourceFiles = ( + preview: ReviewDiffPreviewResult, + kind: "working-tree" | "branch-range", + ) => preview.sources.find((source) => source.kind === kind)!.files; + yield* git(cwd, ["checkout", "-b", "feature/combined"]); + yield* writeTextFile(cwd, "README.md", "# test\ncommitted\n"); + yield* git(cwd, ["commit", "-am", "commit edit"]); + + const clean = yield* driver.getReviewDiffPreview({ cwd, baseRef: initialBranch }); + assert.deepStrictEqual(sourceFiles(clean, "working-tree"), []); + assert.deepStrictEqual(sourceFiles(clean, "branch-range"), [ + { + path: "README.md", + previousPath: null, + changeType: "change", + additions: 1, + deletions: 0, + }, + ]); + + yield* writeTextFile(cwd, "README.md", "# test\ncommitted\ndirty\n"); + yield* writeTextFile(cwd, "new.txt", "new\n"); const dirty = yield* driver.getReviewDiffPreview({ cwd, baseRef: initialBranch }); const changes = dirty.sources.find((source) => source.kind === "branch-range")!; assert.deepStrictEqual(changes.files, [ - { path: "README.md", previousPath: null, additions: 2, deletions: 0 }, - { path: "new.txt", previousPath: null, additions: 1, deletions: 0 }, + { + path: "README.md", + previousPath: null, + changeType: "change", + additions: 2, + deletions: 0, + }, + { path: "new.txt", previousPath: null, changeType: "new", additions: 1, deletions: 0 }, ]); assert.strictEqual(changes.diff.match(/^diff --git a\/README\.md /gm)?.length, 1); assert.include(changes.diff, "+dirty"); assert.deepStrictEqual(sourceFiles(dirty, "working-tree"), [ - { path: "README.md", previousPath: null, additions: 1, deletions: 0 }, - { path: "new.txt", previousPath: null, additions: 1, deletions: 0 }, + { + path: "README.md", + previousPath: null, + changeType: "change", + additions: 1, + deletions: 0, + }, + { path: "new.txt", previousPath: null, changeType: "new", additions: 1, deletions: 0 }, ]); }), ); @@ -3682,7 +4282,13 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { const changes = preview.sources.find((source) => source.kind === "branch-range")!; assert.strictEqual(changes.baseRef, "origin/main"); assert.deepStrictEqual(changes.files, [ - { path: "unpushed.txt", previousPath: null, additions: 1, deletions: 0 }, + { + path: "unpushed.txt", + previousPath: null, + changeType: "new", + additions: 1, + deletions: 0, + }, ]); }), ); @@ -3721,7 +4327,13 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { const changes = preview.sources.find((source) => source.kind === "branch-range")!; assert.strictEqual(changes.baseRef, baseRef); assert.deepStrictEqual(changes.files, [ - { path: "feature.txt", previousPath: null, additions: 1, deletions: 0 }, + { + path: "feature.txt", + previousPath: null, + changeType: "new", + additions: 1, + deletions: 0, + }, ]); } @@ -3755,8 +4367,20 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { const changes = preview.sources.find((source) => source.kind === "branch-range")!; assert.strictEqual(changes.baseRef, "refs/remotes/upstream/main"); assert.deepStrictEqual(changes.files, [ - { path: "feature.txt", previousPath: null, additions: 1, deletions: 0 }, - { path: "upstream.txt", previousPath: null, additions: 1, deletions: 1 }, + { + path: "feature.txt", + previousPath: null, + changeType: "new", + additions: 1, + deletions: 0, + }, + { + path: "upstream.txt", + previousPath: null, + changeType: "change", + additions: 1, + deletions: 1, + }, ]); const contents = yield* driver.getReviewDiffFileContents( makeReviewDiffFileContentsInput(cwd, { @@ -3811,7 +4435,11 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { // Base config reads start only after status completes; hold every one until the rename lands. const spawner = ChildProcessSpawner.make((command) => Effect.gen(function* () { - if (ChildProcess.isStandardCommand(command) && command.args[0] === "config") { + if ( + ChildProcess.isStandardCommand(command) && + command.args[0] === "config" && + !command.args.includes("status.showUntrackedFiles") + ) { if (!(yield* Ref.getAndSet(renameClaimed, true))) { yield* git(cwd, ["branch", "-m", "t3/temporary", "feature/named"]).pipe( Effect.provideService(GitVcsDriver.GitVcsDriver, outer), @@ -3868,7 +4496,13 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { const changes = preview.sources.find((source) => source.kind === "branch-range")!; assert.strictEqual(changes.baseRef, baseRef); assert.deepStrictEqual(changes.files, [ - { path: "README.md", previousPath: null, additions: 1, deletions: 0 }, + { + path: "README.md", + previousPath: null, + changeType: "change", + additions: 1, + deletions: 0, + }, ]); const scoped = yield* driver.getReviewDiffPreview({ cwd, @@ -3917,7 +4551,15 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { const explicit = yield* driver.getReviewDiffPreview({ cwd, baseRef: initialBranch }); assert.deepStrictEqual( explicit.sources.find((source) => source.kind === "branch-range")!.files, - [{ path: "detached.txt", previousPath: null, additions: 1, deletions: 0 }], + [ + { + path: "detached.txt", + previousPath: null, + changeType: "new", + additions: 1, + deletions: 0, + }, + ], ); const implicit = yield* driver.getReviewDiffPreview({ cwd }); assert.deepStrictEqual( @@ -3947,7 +4589,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { const changes = preview.sources.find((source) => source.kind === "branch-range")!; assert.isNull(changes.baseRef); assert.deepStrictEqual(changes.files, [ - { path: "new.txt", previousPath: null, additions: 2, deletions: 0 }, + { path: "new.txt", previousPath: null, changeType: "new", additions: 2, deletions: 0 }, ]); const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true }); assert.deepStrictEqual(status.branchChanges, { @@ -3978,7 +4620,15 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { const preview = yield* driver.getReviewDiffPreview({ cwd, baseRef: initialBranch }); assert.deepStrictEqual( preview.sources.find((source) => source.kind === "branch-range")!.files, - [{ path: "feature.txt", previousPath: null, additions: 1, deletions: 0 }], + [ + { + path: "feature.txt", + previousPath: null, + changeType: "new", + additions: 1, + deletions: 0, + }, + ], ); } }), @@ -4104,6 +4754,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { { path: "repeated.txt", previousPath: null, + changeType: "change", additions: Number(additions), deletions: Number(removals), }, @@ -4273,6 +4924,13 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { yield* git(cwd, ["checkout", "-b", "feature/many-untracked"]); yield* writeTextFile(cwd, ".git/info/exclude", "ignored.txt\n"); yield* writeTextFile(cwd, "bulk[1]/ignored.txt", "ignored\n"); + const clean = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true }); + assert.deepStrictEqual(clean.branchChanges, { + baseRef: "main", + insertions: 0, + deletions: 0, + }); + assert.deepStrictEqual(untrackedScanRoots, []); yield* Effect.forEach( Array.from({ length: 1_001 }, (_, index) => `bulk[1]/${index}.txt`), (file) => writeTextFile(cwd, file, "one\ntwo\n"), @@ -4297,13 +4955,15 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { deletions: 0, }); assert.deepStrictEqual(untrackedScanRoots, [ - scenario === "normal" ? [":(top,literal)bulk[1]/"] : [], + scenario === "normal" || scenario === "no" ? [":(top,literal)bulk[1]/"] : [], ]); - if (scenario === "normal") { + if (scenario === "normal" || scenario === "no") { assert.deepStrictEqual( status.workingTree.files.map((file) => file.path), ["bulk[1]/"], ); + } else if (scenario === "all") { + assert.lengthOf(status.workingTree.files, 1_001); } } }), @@ -4402,8 +5062,20 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { assert.deepStrictEqual( preview.sources.find((source) => source.kind === "working-tree")?.files, [ - { path: "asset.data", previousPath: null, additions: 0, deletions: 1 }, - { path: "ordinary.txt", previousPath: null, additions: 1, deletions: 0 }, + { + path: "asset.data", + previousPath: null, + changeType: "deleted", + additions: 0, + deletions: 1, + }, + { + path: "ordinary.txt", + previousPath: null, + changeType: "new", + additions: 1, + deletions: 0, + }, ], ); }), @@ -4569,7 +5241,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { }), ); - it.effect("reports remote status on unborn HEAD without failing", () => + it.effect("reports status on unborn HEAD without failing or dropping its upstream", () => Effect.gen(function* () { const cwd = yield* makeTmpDir(); const driver = yield* GitVcsDriver.GitVcsDriver; @@ -4583,6 +5255,32 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { assert.equal(status.hasUpstream, false); assert.equal(status.aheadCount, 0); assert.equal(status.behindCount, 0); + + const seed = yield* makeTmpDir(); + const remote = yield* makeTmpDir("git-remote-"); + yield* initRepoWithCommit(seed); + yield* git(remote, ["init", "--bare"]); + yield* git(seed, ["push", remote, `HEAD:refs/heads/${initialBranch}`]); + yield* git(cwd, ["remote", "add", "origin", remote]); + yield* git(cwd, ["fetch", "origin"]); + yield* git(cwd, ["config", `branch.${initialBranch}.remote`, "origin"]); + yield* git(cwd, ["config", `branch.${initialBranch}.merge`, `refs/heads/${initialBranch}`]); + assert.equal((yield* driver.statusDetailsLocal(cwd)).hasUpstream, true); + }), + ); + + it.effect("reports no branch while a clone has not checked out HEAD yet", () => + Effect.gen(function* () { + const cwd = yield* makeTmpDir(); + const driver = yield* GitVcsDriver.GitVcsDriver; + yield* driver.initRepo({ cwd }); + // `git clone` points HEAD here until checkout; status then prints `branch.head (unknown)`. + yield* (yield* FileSystem.FileSystem).writeFileString( + NodePath.join(cwd, ".git", "HEAD"), + "ref: refs/heads/.invalid\n", + ); + + assert.equal((yield* driver.statusDetailsLocal(cwd)).branch, null); }), ); @@ -4653,6 +5351,100 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { }), ); + it.effect("a ref snapshot detects branch identity, ref and ancestry changes", () => + Effect.gen(function* () { + const cwd = yield* makeTmpDir(); + const remote = yield* makeTmpDir("git-vcs-driver-remote-"); + const updater = yield* makeTmpDir("git-vcs-driver-updater-"); + const { initialBranch } = yield* initRepoWithCommit(cwd); + yield* git(remote, ["init", "--bare"]); + yield* git(cwd, ["remote", "add", "origin", remote]); + yield* git(cwd, ["push", "-u", "origin", initialBranch]); + yield* git(updater, ["clone", remote, "."]); + yield* git(updater, ["config", "user.email", "test@test.com"]); + yield* git(updater, ["config", "user.name", "Test"]); + const driver = yield* GitVcsDriver.GitVcsDriver; + const snapshot = driver.readRefSnapshot(cwd); + + // Newer Git can create origin/HEAD on the first fetch. + yield* git(cwd, ["fetch", "origin"]); + const initial = yield* snapshot; + assert.isNotNull(initial); + yield* git(cwd, ["fetch", "origin"]); + assert.equal(yield* snapshot, initial); + + yield* git(cwd, ["branch", "same-commit"]); + const beforeCheckout = yield* snapshot; + const refsBeforeCheckout = yield* git(cwd, ["show-ref", "--head"]); + yield* git(cwd, ["checkout", "same-commit"]); + assert.equal(yield* git(cwd, ["show-ref", "--head"]), refsBeforeCheckout); + const afterCheckout = yield* snapshot; + assert.isNotNull(afterCheckout); + assert.notEqual(afterCheckout, beforeCheckout); + yield* git(cwd, ["checkout", "--detach"]); + assert.equal(yield* git(cwd, ["show-ref", "--head"]), refsBeforeCheckout); + const detached = yield* snapshot; + assert.isNotNull(detached); + assert.notEqual(detached, beforeCheckout); + assert.notEqual(detached, afterCheckout); + assert.equal(yield* snapshot, detached); + yield* git(cwd, ["checkout", initialBranch]); + assert.equal(yield* snapshot, beforeCheckout); + yield* git(cwd, ["branch", "-d", "same-commit"]); + + yield* writeTextFile(updater, "remote.txt", "remote\n"); + yield* git(updater, ["add", "remote.txt"]); + yield* git(updater, ["commit", "-m", "remote commit"]); + yield* git(updater, ["push", "origin", initialBranch]); + assert.equal(yield* snapshot, initial); + yield* git(cwd, ["fetch", "origin"]); + const fetched = yield* snapshot; + assert.notEqual(fetched, initial); + + const shallow = yield* makeTmpDir("git-vcs-driver-shallow-"); + yield* git(shallow, ["clone", "--depth=1", NodeURL.pathToFileURL(remote).href, "."]); + const shallowRefs = yield* git(shallow, ["show-ref", "--head"]); + const shallowSnapshot = yield* driver.readRefSnapshot(shallow); + assert.isNotNull(shallowSnapshot); + const shallowWorktree = yield* makeTmpDir("git-vcs-driver-shallow-worktree-"); + yield* git(shallow, ["worktree", "add", "--detach", shallowWorktree]); + const worktreeSnapshot = yield* driver.readRefSnapshot(shallowWorktree); + assert.isNotNull(worktreeSnapshot); + yield* git(shallow, ["fetch", "--unshallow", "origin"]); + assert.equal(yield* git(shallow, ["show-ref", "--head"]), shallowRefs); + const unshallowSnapshot = yield* driver.readRefSnapshot(shallow); + const unshallowWorktreeSnapshot = yield* driver.readRefSnapshot(shallowWorktree); + assert.isNotNull(unshallowSnapshot); + assert.isNotNull(unshallowWorktreeSnapshot); + assert.notEqual(unshallowSnapshot, shallowSnapshot); + assert.notEqual(unshallowWorktreeSnapshot, worktreeSnapshot); + + yield* git(cwd, ["checkout", "--detach"]); + yield* git(cwd, ["commit", "--allow-empty", "-m", "detached"]); + assert.notEqual(yield* snapshot, fetched); + + const head = yield* git(cwd, ["rev-parse", "HEAD"]); + yield* driver.execute({ + operation: "GitVcsDriver.test.git", + cwd, + args: ["update-ref", "--stdin"], + stdin: Array.from( + { length: 16_000 }, + (_, index) => `create refs/heads/snapshot-${String(index).padStart(5, "0")} ${head}\n`, + ).join(""), + }); + yield* git(cwd, ["pack-refs", "--all"]); + const large = yield* snapshot; + assert.isNotNull(large); + assert.isAbove(Buffer.byteLength(large ?? "", "utf8"), 1_000_000); + assert.equal(yield* snapshot, large); + yield* git(cwd, ["update-ref", "refs/heads/snapshot-00000", "HEAD~1"]); + assert.notEqual(yield* snapshot, large); + + assert.isNull(yield* driver.readRefSnapshot(yield* makeTmpDir())); + }), + ); + it.effect("does not start Git auto-maintenance from background upstream fetches", () => Effect.gen(function* () { const cwd = yield* makeTmpDir(); @@ -4763,7 +5555,12 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { const defaultBranch = yield* driver.resolveDefaultBranchName(cwd, "origin"); assert.notEqual(defaultBranch, "development"); - assert.isNull(yield* driver.resolveWorktreeBaseRemote({ cwd, refName: "origin/HEAD" })); + for (const refName of ["origin/HEAD", "HEAD~1", "main^", "@{-1}", "-", ":/fix"]) + assert.isNull(yield* driver.resolveWorktreeBaseRemote({ cwd, refName })); + assert.deepEqual( + yield* driver.resolveWorktreeBaseRemote({ cwd, refName: "feature/a\u00a0b" }), + { remoteName: "origin", branchName: "feature/a\u00a0b" }, + ); }), ); @@ -4902,25 +5699,21 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { }), ); - it.effect("reports staged file counts on unborn HEAD without failing", () => + it.effect("reports net file counts from the empty tree on unborn HEAD", () => Effect.gen(function* () { const cwd = yield* makeTmpDir(); const driver = yield* GitVcsDriver.GitVcsDriver; yield* driver.initRepo({ cwd }); - yield* git(cwd, ["config", "user.email", "test@test.com"]); - yield* git(cwd, ["config", "user.name", "Test"]); yield* writeTextFile(cwd, "initial.ts", "// first file\n"); yield* git(cwd, ["add", "initial.ts"]); + yield* writeTextFile(cwd, "initial.ts", "// edited after staging\n"); - const status = yield* driver.statusDetails(cwd); + const status = yield* driver.statusDetailsLocal(cwd); assert.equal(status.isRepo, true); - assert.equal(status.workingTree.files.length, 1); - const file = status.workingTree.files[0]; - if (file) { - assert.equal(file.path, "initial.ts"); - assert.equal(file.insertions, 1); - } + assert.deepStrictEqual(status.workingTree.files, [ + { path: "initial.ts", insertions: 1, deletions: 0 }, + ]); }), ); }); @@ -5328,6 +6121,172 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { }); describe("worktree operations", () => { + it.effect.each(["current", "legacy"] as const)( + "preserves another creator's worktree on its new branch at the same path (%s)", + (version) => + Effect.gen(function* () { + const cwd = yield* makeTmpDir(); + const { initialBranch } = yield* initRepoWithCommit(cwd); + const path = NodePath.join(yield* makeTmpDir("git-worktrees-"), "contested"); + const delegate = yield* ChildProcessSpawner.ChildProcessSpawner; + const reachedAdd = yield* Deferred.make(); + let paused = false; + const spawner = ChildProcessSpawner.make((command) => + Effect.gen(function* () { + if ( + ChildProcess.isStandardCommand(command) && + command.args.includes("worktree") && + command.args.includes("add") + ) { + if (command.args.includes("-h")) { + return version === "legacy" + ? makeFailedHandle("usage: git worktree add [--lock]") + : yield* delegate.spawn(command); + } + if (!paused) { + paused = true; + yield* Deferred.succeed(reachedAdd, undefined); + return yield* Effect.never; + } + } + return yield* delegate.spawn(command); + }), + ); + const driver = yield* makeGitVcsDriverCore().pipe( + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), + Effect.provide(layerServerConfig), + ); + const creator = yield* driver + .createWorktree({ + cwd, + path, + refName: initialBranch, + newRefName: "contested", + }) + .pipe(Effect.forkChild); + yield* Deferred.await(reachedAdd); + yield* git(cwd, ["worktree", "add", "--", path, "contested"]); + yield* writeTextFile(path, "owned-by-B.txt", "keep this work\n"); + yield* Fiber.interrupt(creator); + assert.include(yield* git(cwd, ["worktree", "list", "--porcelain"]), path); + assert.equal(yield* git(path, ["branch", "--show-current"]), "contested"); + assert.equal( + yield* (yield* FileSystem.FileSystem).readFileString( + NodePath.join(path, "owned-by-B.txt"), + ), + "keep this work\n", + ); + const created = yield* driver.createWorktree({ + cwd, + path: `${path}-retry`, + refName: initialBranch, + newRefName: "retry", + }); + assert.equal(yield* git(created.worktree.path, ["branch", "--show-current"]), "retry"); + assert.notInclude(yield* git(cwd, ["worktree", "list", "--porcelain"]), "locked"); + }), + ); + + it.effect.each(["main", "linked"] as const)( + "removes its worktree and branch when cancelled before the claim (%s)", + (checkout) => + Effect.gen(function* () { + const repo = yield* makeTmpDir(); + const { initialBranch } = yield* initRepoWithCommit(repo); + const cwd = + checkout === "main" + ? repo + : NodePath.join(yield* makeTmpDir("git-linked-"), "checkout"); + if (checkout === "linked") { + yield* git(repo, ["config", "worktree.useRelativePaths", "true"]); + yield* git(repo, ["worktree", "add", "--detach", cwd]); + } + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const driver = yield* GitVcsDriver.GitVcsDriver; + const worktreePath = path.join(yield* makeTmpDir("git-worktrees-"), "cancelled ü"); + const hookPath = path.join(repo, ".git/hooks/post-checkout"); + const connected = Promise.withResolvers(); + const sockets = new Set(); + const server = yield* Effect.acquireRelease( + Effect.sync(() => + NodeNet.createServer((socket) => { + sockets.add(socket); + connected.resolve(); + }), + ), + (server) => + Effect.sync(() => { + for (const socket of sockets) socket.destroy(); + server.close(); + }), + ); + yield* Effect.promise( + () => + new Promise((resolve, reject) => { + server.once("error", reject); + server.listen(0, "127.0.0.1", resolve); + }), + ); + const address = server.address(); + assert.isTrue(address !== null && typeof address !== "string"); + if (address === null || typeof address === "string") return; + const gate = path.join(cwd, "hook-gate.cjs"); + yield* fs.writeFileString( + gate, + `const s = require('node:net').connect(${address.port}, '127.0.0.1'); s.on('connect', () => process.stderr.write('Updating files: 50% (1/1000)\\n')); s.resume();`, + ); + yield* writeTextFile( + repo, + ".git/hooks/post-checkout", + `#!/bin/sh\nexec "${process.execPath.replaceAll("\\", "/")}" "${gate.replaceAll("\\", "/")}"\n`, + ); + yield* fs.chmod(hookPath, 0o755); + const hookStarted = yield* Deferred.make(); + let claims = 0; + const input = { + cwd, + path: worktreePath, + refName: initialBranch, + newRefName: "t3/cancel", + }; + const worktrees = git(cwd, ["worktree", "list", "--porcelain"]); + + const cancelled = yield* driver + .createWorktree(input, { + progress: { + onCheckoutProgress: ({ total }) => + total === 1000 ? Deferred.succeed(hookStarted, undefined) : Effect.void, + onWorktreeClaimed: () => Effect.sync(() => void claims++), + }, + }) + .pipe(Effect.forkChild); + yield* Deferred.await(hookStarted); + yield* Effect.promise(() => connected.promise); + assert.include(yield* worktrees, worktreePath); + yield* Fiber.interrupt(cancelled); + + assert.equal(claims, 0); + assert.notInclude(yield* worktrees, worktreePath); + assert.equal(yield* git(cwd, ["branch", "--list", "t3/cancel"]), ""); + assert.isFalse(yield* fs.exists(worktreePath)); + + // A retry gets the same path and branch, and a claimed worktree outlives a later cancel. + yield* fs.remove(hookPath); + const claimed = yield* Deferred.make(); + const retry = yield* driver + .createWorktree(input, { + progress: { onWorktreeClaimed: () => Deferred.succeed(claimed, undefined) }, + }) + .pipe(Effect.forkChild); + yield* Deferred.await(claimed); + yield* Fiber.interrupt(retry); + assert.include(yield* worktrees, worktreePath); + assert.notInclude(yield* worktrees, "locked"); + assert.equal(yield* git(worktreePath, ["branch", "--show-current"]), "t3/cancel"); + }), + ); + it.effect("uses parallel checkout without skipping filters or hooks", () => Effect.gen(function* () { const cwd = yield* makeTmpDir(); @@ -5384,6 +6343,19 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { `${expected}\n`, ); } + yield* writeTextFile( + cwd, + ".git/hooks/post-checkout", + '#!/bin/sh\ngit worktree unlock "$PWD" 2>/dev/null || :\ngit worktree lock --reason user-kept "$PWD"\n', + ); + const lockedPath = path.join(yield* makeTmpDir("git-worktrees-"), "hook-locked"); + yield* driver.createWorktree({ + cwd, + path: lockedPath, + refName: initialBranch, + newRefName: "feature/hook-locked", + }); + assert.include(yield* git(cwd, ["worktree", "list", "--porcelain"]), "locked user-kept"); }), ); @@ -5694,6 +6666,44 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { }), ); + it.effect("gives parallel renames to one generated name distinct branches", () => + Effect.gen(function* () { + const cwd = yield* makeTmpDir(); + const { initialBranch } = yield* initRepoWithCommit(cwd); + const path = yield* Path.Path; + const worktrees = yield* makeTmpDir("git-worktrees-"); + const driver = yield* GitVcsDriver.GitVcsDriver; + // A multi-model send launches one worktree per model with the same message, so their + // generated names often match and the renames can land together. + const temporaryBranches = ["t3/aaaa1111", "t3/bbbb2222", "t3/cccc3333"]; + yield* Effect.forEach(temporaryBranches, (branch) => + driver.createWorktree({ + cwd, + path: path.join(worktrees, branch.replace("/", "-")), + refName: initialBranch, + newRefName: branch, + }), + ); + + const renamed = yield* Effect.forEach( + temporaryBranches, + (branch) => + driver.renameBranch({ + cwd: path.join(worktrees, branch.replace("/", "-")), + oldBranch: branch, + newBranch: "feat/add-search", + }), + { concurrency: "unbounded" }, + ); + + assert.deepStrictEqual(renamed.map((result) => result.branch).toSorted(), [ + "feat/add-search", + "feat/add-search-1", + "feat/add-search-2", + ]); + }), + ); + it.effect.each(["release/v2", "refs/heads/release/v2", "deadbeef"])( "records the branch name when creating a worktree from %s", (baseRef) => @@ -5902,12 +6912,16 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { const fileSystem = yield* FileSystem.FileSystem; const pathService = yield* Path.Path; + const submoduleRepo = yield* makeTmpDir("git-submodule-"); + yield* initRepoWithCommit(submoduleRepo); + const cwd = yield* makeTmpDir(); const { initialBranch } = yield* initRepoWithCommit(cwd); - yield* writeTextFile( - cwd, - ".gitmodules", - '[submodule "missing"]\n\tpath = missing\n\turl = /nonexistent/repo.git\n', + yield* git(cwd, ["submodule", "add", NodeURL.pathToFileURL(submoduleRepo).href, "shared"]); + yield* fileSystem.writeFileString( + pathService.join(cwd, ".gitmodules"), + (yield* fileSystem.readFileString(pathService.join(cwd, ".gitmodules"))) + + '[submodule "missing"]\n\tpath = missing\n\turl = /nonexistent/repo.git\n', ); yield* git(cwd, ["add", "."]); const gitlink = yield* git(cwd, ["rev-parse", "HEAD"]); @@ -5940,7 +6954,9 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { assert.lengthOf(results, 1); assert.isFalse(results[0]!.ok); assert.include(results[0]!.detail, "GitVcsDriver.createWorktree.updateSubmodules"); - }), + // A submodule that did clone must not be left as a bare gitdir that reads as modified. + assert.equal(yield* git(worktreePath, ["status", "--porcelain"]), ""); + }).pipe(withGitFileProtocol), ); it.effect("creates worktrees under the configured worktrees directory", () => @@ -6262,6 +7278,41 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { }), ); + it.effect.skipIf(!symlinksSupported)( + "removes a worktree whose .git file git-annex replaced with a symlink, keeping the symlink when refused", + () => + Effect.gen(function* () { + const cwd = yield* makeTmpDir(); + const { initialBranch } = yield* initRepoWithCommit(cwd); + const pathService = yield* Path.Path; + const worktreePath = pathService.join(yield* makeTmpDir("git-worktrees-"), "annexed"); + const driver = yield* GitVcsDriver.GitVcsDriver; + yield* driver.createWorktree({ + cwd, + path: worktreePath, + refName: initialBranch, + newRefName: "feature/annexed", + }); + const gitFile = pathService.join(worktreePath, ".git"); + const adminDir = (yield* git(worktreePath, ["rev-parse", "--absolute-git-dir"])).trim(); + NodeFS.rmSync(gitFile); + NodeFS.symlinkSync(adminDir, gitFile); + yield* writeTextFile(worktreePath, "notes.txt", "draft\n"); + + const refused = yield* Effect.result(driver.removeWorktree({ cwd, path: worktreePath })); + + assert.isTrue(Result.isFailure(refused)); + assert.isTrue(NodeFS.lstatSync(gitFile).isSymbolicLink()); + assert.equal(NodeFS.realpathSync(gitFile), NodeFS.realpathSync(adminDir)); + NodeFS.rmSync(pathService.join(worktreePath, "notes.txt")); + + yield* driver.removeWorktree({ cwd, path: worktreePath }); + + assert.isFalse(NodeFS.existsSync(worktreePath)); + assert.isFalse(NodeFS.existsSync(adminDir)); + }), + ); + it.effect("leaves a folder alone that only shares the worktree's short name", () => Effect.gen(function* () { const cwd = yield* makeTmpDir(); @@ -7049,30 +8100,49 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { ); it.effect.each([ - { location: "root", conflict: false }, - { location: "nested", conflict: false }, - { location: "worktree", conflict: false }, - { location: "root", conflict: true }, - { location: "nested", conflict: true }, - { location: "worktree", conflict: true }, + { location: "root", conflict: false, encoding: "utf8" }, + { location: "nested", conflict: false, encoding: "utf8" }, + { location: "worktree", conflict: false, encoding: "utf8" }, + { location: "root", conflict: true, encoding: "utf8" }, + { location: "nested", conflict: true, encoding: "utf8" }, + { location: "worktree", conflict: true, encoding: "utf8" }, + { location: "root", conflict: true, encoding: "utf16le" }, + { location: "root", conflict: true, encoding: "utf16le", large: true }, ])( - "rejects selected-file merge commits without mutation ($location, conflict: $conflict)", - ({ location, conflict }) => + "rejects selected-file merge commits without mutation ($location, conflict: $conflict, $encoding)", + ({ location, conflict, encoding, large }) => Effect.gen(function* () { let cwd = yield* makeTmpDir(); const { initialBranch } = yield* initRepoWithCommit(cwd); + const writeReadme = (text: string) => + Effect.sync(() => + NodeFS.writeFileSync( + NodePath.join(cwd, "README.md"), + encoding === "utf16le" ? Buffer.from(`\ufeff${text}`, "utf16le") : text, + ), + ); + if (encoding === "utf16le") { + yield* writeTextFile( + cwd, + ".gitattributes", + "README.md text working-tree-encoding=UTF-16\n", + ); + yield* writeReadme("base\n"); + yield* git(cwd, ["add", "."]); + yield* git(cwd, ["commit", "-m", "Declare UTF-16 encoding"]); + } const driver = yield* GitVcsDriver.GitVcsDriver; const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; yield* git(cwd, ["checkout", "-b", "feature"]); yield* writeTextFile(cwd, "feature.txt", "feature\n"); - if (conflict) yield* writeTextFile(cwd, "README.md", "feature version\n"); + if (conflict) yield* writeReadme("feature version\n"); yield* git(cwd, ["add", "."]); yield* git(cwd, ["commit", "-m", "Feature"]); const featureHead = yield* git(cwd, ["rev-parse", "HEAD"]); yield* git(cwd, ["checkout", initialBranch]); yield* writeTextFile(cwd, "main.txt", "main\n"); - if (conflict) yield* writeTextFile(cwd, "README.md", "main version\n"); + if (conflict) yield* writeReadme("main version\n"); yield* git(cwd, ["add", "."]); yield* git(cwd, ["commit", "-m", "Main"]); const mainHead = yield* git(cwd, ["rev-parse", "HEAD"]); @@ -7121,6 +8191,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), mainHead); assert.equal(yield* git(cwd, ["ls-files", "--unmerged"]), unmerged); + const resolvedText = `Heading\n=======\n\nresolved${large ? "x".repeat(17 * 1024 * 1024) : ""}\n`; if (conflict) { const unresolved = yield* driver .commit(commitCwd, "Unresolved merge", "", { stage: {} }) @@ -7129,8 +8200,10 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), mainHead); assert.equal(yield* git(cwd, ["ls-files", "--unmerged"]), unmerged); // A lone "=======" is a heading underline, not a conflict boundary. - yield* writeTextFile(cwd, "README.md", "Heading\n=======\n\nresolved\n"); - assert.isNotNull(yield* driver.prepareCommitContext(commitCwd)); + yield* writeReadme(resolvedText); + assert.isNotNull( + yield* driver.prepareCommitContext(commitCwd, undefined, { includePatch: false }), + ); } yield* driver.commit(commitCwd, "Finish merge", "", { stage: {} }); assert.equal( @@ -7140,7 +8213,12 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { assert.isFalse(NodeFS.existsSync(path.join(gitDir, "MERGE_HEAD"))); assert.equal(yield* git(cwd, ["show", "HEAD:feature.txt"]), "feature"); assert.equal(yield* git(cwd, ["show", "HEAD:unrelated.txt"]), "unrelated"); - if (conflict) + if (large) { + assert.equal( + yield* git(cwd, ["cat-file", "-s", "HEAD:README.md"]), + String(Buffer.byteLength(resolvedText)), + ); + } else if (conflict) assert.equal( yield* git(cwd, ["show", "HEAD:README.md"]), "Heading\n=======\n\nresolved", @@ -7179,41 +8257,108 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { ); it.effect.each([ - { operation: "cherry-pick", head: "CHERRY_PICK_HEAD" }, - { operation: "revert", head: "REVERT_HEAD" }, - ])("rejects selected-file commits during a $operation", ({ operation, head }) => + { operation: "cherry-pick", head: "CHERRY_PICK_HEAD", refs: "files" }, + { operation: "revert", head: "REVERT_HEAD", refs: "files" }, + // Reftable keeps CHERRY_PICK_HEAD and REVERT_HEAD as refs with no file under .git. + { operation: "cherry-pick", head: "CHERRY_PICK_HEAD", refs: "reftable" }, + { operation: "revert", head: "REVERT_HEAD", refs: "reftable" }, + ])( + "rejects selected-file commits during a $operation ($refs)", + ({ operation, head, refs }, ctx) => + Effect.gen(function* () { + if (refs === "reftable" && !gitHasReftable) + return ctx.skip("Needs Git 2.45 or later for reftable."); + const cwd = yield* makeTmpDir(); + if (refs === "reftable") yield* commitInitialRepo(cwd, ["--ref-format=reftable"]); + else yield* initRepoWithCommit(cwd); + // Reftable leaves .git/HEAD as a stub, so ask Git for the branch. + const initialBranch = yield* git(cwd, ["symbolic-ref", "--short", "HEAD"]); + const driver = yield* GitVcsDriver.GitVcsDriver; + yield* git(cwd, ["checkout", "-b", "other"]); + yield* writeTextFile(cwd, "README.md", "other version\n"); + yield* writeTextFile(cwd, "picked.txt", "picked\n"); + yield* git(cwd, ["add", "."]); + yield* git(cwd, ["commit", "-m", "Other"]); + if (operation === "cherry-pick") yield* git(cwd, ["checkout", initialBranch]); + yield* writeTextFile(cwd, "README.md", "main version\n"); + yield* git(cwd, ["commit", "-am", "Main"]); + const originalHead = yield* git(cwd, ["rev-parse", "HEAD"]); + yield* git(cwd, ["branch", head]); + const result = yield* driver.execute({ + operation: `test.${operation}`, + cwd, + args: [operation, operation === "cherry-pick" ? "other" : "HEAD~1"], + allowNonZeroExit: true, + }); + assert.equal(result.exitCode, 1); + yield* writeTextFile(cwd, "README.md", "resolved version\n"); + yield* git(cwd, ["add", "README.md"]); + const selected = operation === "cherry-pick" ? "picked.txt" : "README.md"; + + const commit = yield* driver + .commit(cwd, "Partial", "", { stage: { filePaths: [selected] } }) + .pipe(Effect.result); + + assert.isTrue(Result.isFailure(commit), `Selected-file ${operation} commit succeeded`); + assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), originalHead); + assert.equal( + yield* git(cwd, [ + "-c", + "core.warnAmbiguousRefs=false", + "rev-parse", + "--verify", + "--symbolic-full-name", + head, + ]), + head, + ); + }), + ); + + it.effect("rejects selected-file commits during a multi-commit no-commit cherry-pick", () => Effect.gen(function* () { const cwd = yield* makeTmpDir(); const { initialBranch } = yield* initRepoWithCommit(cwd); const driver = yield* GitVcsDriver.GitVcsDriver; - yield* git(cwd, ["checkout", "-b", "other"]); - yield* writeTextFile(cwd, "README.md", "other version\n"); - yield* writeTextFile(cwd, "picked.txt", "picked\n"); + const path = yield* Path.Path; + yield* writeTextFile(cwd, "a.txt", "base a\n"); + yield* writeTextFile(cwd, "b.txt", "base b\n"); yield* git(cwd, ["add", "."]); - yield* git(cwd, ["commit", "-m", "Other"]); - if (operation === "cherry-pick") yield* git(cwd, ["checkout", initialBranch]); - yield* writeTextFile(cwd, "README.md", "main version\n"); + yield* git(cwd, ["commit", "-m", "Base"]); + yield* git(cwd, ["checkout", "-b", "feature"]); + yield* writeTextFile(cwd, "a.txt", "feature a\n"); + yield* writeTextFile(cwd, "b.txt", "feature b\n"); + yield* git(cwd, ["commit", "-am", "Feature one"]); + yield* writeTextFile(cwd, "later.txt", "later\n"); + yield* git(cwd, ["add", "."]); + yield* git(cwd, ["commit", "-m", "Feature two"]); + yield* git(cwd, ["checkout", initialBranch]); + yield* writeTextFile(cwd, "a.txt", "main a\n"); + yield* writeTextFile(cwd, "b.txt", "main b\n"); yield* git(cwd, ["commit", "-am", "Main"]); - const originalHead = yield* git(cwd, ["rev-parse", "HEAD"]); - const result = yield* driver.execute({ - operation: `test.${operation}`, + const head = yield* git(cwd, ["rev-parse", "HEAD"]); + const started = yield* driver.execute({ + operation: "test.sequencer", cwd, - args: [operation, operation === "cherry-pick" ? "other" : "HEAD~1"], + args: ["cherry-pick", "--no-commit", "feature~1", "feature"], allowNonZeroExit: true, }); - assert.equal(result.exitCode, 1); - yield* writeTextFile(cwd, "README.md", "resolved version\n"); - yield* git(cwd, ["add", "README.md"]); + assert.equal(started.exitCode, 1); + yield* writeTextFile(cwd, "a.txt", "resolved a\n"); const gitDir = yield* git(cwd, ["rev-parse", "--absolute-git-dir"]); - const selected = operation === "cherry-pick" ? "picked.txt" : "README.md"; - - const commit = yield* driver - .commit(cwd, "Partial", "", { stage: { filePaths: [selected] } }) - .pipe(Effect.result); + assert.isFalse(NodeFS.existsSync(path.join(gitDir, "CHERRY_PICK_HEAD"))); + const names = ["index", "sequencer/todo", "sequencer/head"]; + const before = names.map((name) => NodeFS.readFileSync(path.join(gitDir, name))); - assert.isTrue(Result.isFailure(commit), `Selected-file ${operation} commit succeeded`); - assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), originalHead); - assert.isTrue(NodeFS.existsSync(`${gitDir}/${head}`)); + const error = yield* driver + .commit(cwd, "Partial", "", { stage: { filePaths: ["a.txt"] } }) + .pipe(Effect.flip); + assert.include(error.detail, "Cannot commit selected files during a cherry-pick or revert"); + assert.deepEqual( + names.map((name) => NodeFS.readFileSync(path.join(gitDir, name))), + before, + ); + assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), head); }), ); @@ -7244,6 +8389,53 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { }), ); + it.effect("keeps other files conflicted through a selected-file commit", () => + Effect.gen(function* () { + const cwd = yield* makeTmpDir(); + yield* initRepoWithCommit(cwd); + const driver = yield* GitVcsDriver.GitVcsDriver; + // git stash pop leaves README.md conflicted with no merge in progress. + yield* writeTextFile(cwd, "README.md", "stashed version\n"); + yield* git(cwd, ["stash"]); + yield* writeTextFile(cwd, "README.md", "committed version\n"); + yield* git(cwd, ["commit", "-am", "Change README"]); + const pop = yield* driver.execute({ + operation: "test.stashPop", + cwd, + args: ["stash", "pop"], + allowNonZeroExit: true, + }); + assert.equal(pop.exitCode, 1); + yield* writeTextFile(cwd, "other.txt", "other\n"); + const head = yield* git(cwd, ["rev-parse", "HEAD"]); + const unmerged = yield* git(cwd, ["ls-files", "--unmerged"]); + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const nested = path.join(cwd, "nested"); + yield* fs.makeDirectory(nested); + // A formatting hook stages its edit into the temporary index that --only commits from. + yield* writeTextFile( + cwd, + ".git/hooks/pre-commit", + "#!/bin/sh\necho formatted >> other.txt\ngit add other.txt\n", + ); + yield* fs.chmod(path.join(cwd, ".git/hooks/pre-commit"), 0o755); + + yield* driver.commit(cwd, "Add other", "", { stage: { filePaths: ["other.txt"] } }); + + assert.equal(yield* git(cwd, ["rev-parse", "HEAD~1"]), head); + assert.equal(yield* git(cwd, ["show", "--pretty=", "--name-only", "HEAD"]), "other.txt"); + assert.equal(yield* git(cwd, ["show", "HEAD:other.txt"]), "other\nformatted"); + assert.equal(yield* git(cwd, ["show", ":other.txt"]), "other\nformatted"); + assert.equal(yield* git(cwd, ["ls-files", "--unmerged"]), unmerged); + const all = yield* driver.prepareCommitContext(nested).pipe(Effect.flip); + assert.equal( + all.detail, + "Cannot commit with unresolved merge conflicts in README.md. Resolve them, then commit.", + ); + }), + ); + const commitWithHooks = (hooks: Record) => Effect.gen(function* () { const cwd = yield* makeTmpDir(); @@ -7538,7 +8730,13 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { const preview = yield* driver.getReviewDiffPreview({ cwd: worktreePath }); const changes = preview.sources.find((source) => source.kind === "branch-range")!; assert.deepStrictEqual(changes.files, [ - { path: "feature.txt", previousPath: null, additions: 1, deletions: 0 }, + { + path: "feature.txt", + previousPath: null, + changeType: "new", + additions: 1, + deletions: 0, + }, ]); assert.include(changes.diff, "+feature work"); assert.notInclude(changes.diff, "upstream-only work"); @@ -7745,6 +8943,92 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { }), ); + it.effect("republishes a branch whose upstream was deleted", () => + Effect.gen(function* () { + const cwd = yield* makeTmpDir(); + const remote = yield* makeTmpDir("git-remote-"); + yield* initRepoWithCommit(cwd); + yield* git(remote, ["init", "--bare"]); + yield* git(cwd, ["remote", "add", "origin", remote]); + yield* (yield* GitVcsDriver.GitVcsDriver).createRef({ + cwd, + refName: "feature/push", + }); + yield* (yield* GitVcsDriver.GitVcsDriver).switchRef({ + cwd, + refName: "feature/push", + }); + yield* writeTextFile(cwd, "feature.txt", "feature\n"); + yield* git(cwd, ["add", "-A"]); + yield* (yield* GitVcsDriver.GitVcsDriver).commit(cwd, "Add feature", ""); + + yield* (yield* GitVcsDriver.GitVcsDriver).pushCurrentBranch(cwd, null); + + yield* git(cwd, ["push", "origin", "--delete", "feature/push"]); + yield* writeTextFile(cwd, "followup.txt", "followup\n"); + yield* git(cwd, ["add", "-A"]); + yield* (yield* GitVcsDriver.GitVcsDriver).commit(cwd, "Add followup", ""); + const republished = yield* (yield* GitVcsDriver.GitVcsDriver).pushCurrentBranch(cwd, null); + assert.deepInclude(republished, { status: "pushed", branch: "feature/push" }); + assert.equal( + yield* git(remote, ["--git-dir=.", "rev-parse", "refs/heads/feature/push"]), + yield* git(cwd, ["rev-parse", "HEAD"]), + ); + + const fork = yield* makeTmpDir("git-fork-"); + yield* git(fork, ["init", "--bare"]); + yield* git(cwd, ["remote", "add", "fork", fork]); + yield* git(cwd, ["remote", "add", "feature", fork]); + yield* git(cwd, ["push", "-u", "fork", "feature/push"]); + yield* git(cwd, ["push", "fork", "--delete", "feature/push"]); + yield* writeTextFile(cwd, "fork.txt", "fork\n"); + yield* git(cwd, ["add", "-A"]); + yield* (yield* GitVcsDriver.GitVcsDriver).commit(cwd, "Add fork change", ""); + assert.deepInclude( + yield* (yield* GitVcsDriver.GitVcsDriver).resolvePushTarget(cwd, { + branch: "feature/push", + hasUpstream: false, + }), + { remoteName: "fork", branchName: "feature/push" }, + ); + const forkRepublished = yield* (yield* GitVcsDriver.GitVcsDriver).pushCurrentBranch( + cwd, + null, + ); + assert.deepInclude(forkRepublished, { upstreamBranch: "fork/feature/push" }); + assert.equal( + yield* git(fork, ["--git-dir=.", "rev-parse", "refs/heads/feature/push"]), + yield* git(cwd, ["rev-parse", "HEAD"]), + ); + + yield* git(cwd, ["config", "branch.feature/push.remote", "."]); + yield* git(cwd, ["config", "branch.feature/push.merge", "refs/heads/push"]); + yield* writeTextFile(cwd, "local.txt", "local\n"); + yield* git(cwd, ["add", "-A"]); + yield* (yield* GitVcsDriver.GitVcsDriver).commit(cwd, "Add local change", ""); + const localUpstreamGone = yield* (yield* GitVcsDriver.GitVcsDriver).pushCurrentBranch( + cwd, + null, + ); + assert.deepInclude(localUpstreamGone, { upstreamBranch: "origin/feature/push" }); + + yield* git(cwd, ["push", "fork", "HEAD:refs/heads/release"]); + yield* git(cwd, ["branch", "-u", "fork/release"]); + yield* git(cwd, ["push", "fork", "--delete", "release"]); + yield* writeTextFile(cwd, "release.txt", "release\n"); + yield* git(cwd, ["add", "-A"]); + yield* (yield* GitVcsDriver.GitVcsDriver).commit(cwd, "Add release change", ""); + yield* (yield* GitVcsDriver.GitVcsDriver).pushCurrentBranch(cwd, null); + assert.equal( + yield* (yield* GitVcsDriver.GitVcsDriver).readConfigValue( + cwd, + "branch.feature/push.t3-merge-base", + ), + "release", + ); + }), + ); + it.effect("rebases a pushed branch's pulls when branch.autoSetupRebase asks for it", () => Effect.gen(function* () { const cwd = yield* makeTmpDir(); @@ -7848,6 +9132,16 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { yield* git(originRemote, ["--git-dir=.", "rev-parse", "refs/heads/feature/push"]), publishedHead, ); + + const pushOnlyRemote = yield* makeTmpDir("git-push-only-remote-"); + yield* git(pushOnlyRemote, ["init", "--bare"]); + yield* git(cwd, ["remote", "set-url", "--push", "origin", pushOnlyRemote]); + const pushedToPushUrl = yield* driver.pushCurrentBranch(cwd, null); + assert.deepInclude(pushedToPushUrl, { status: "pushed", branch }); + assert.equal( + yield* git(pushOnlyRemote, ["--git-dir=.", "rev-parse", "refs/heads/feature/push"]), + head, + ); }), ); @@ -7891,14 +9185,15 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { ); it.effect.each([ - { baseBranch: "dev", local: false }, - { baseBranch: "dev", local: true }, - { baseBranch: "feature/base", local: true }, - { baseBranch: "upstream", local: true }, - { baseBranch: "upstream/feature/x", local: true }, + { baseBranch: "dev", local: false, branch: "feature/x", deleted: false }, + { baseBranch: "dev", local: true, branch: "feature/x", deleted: false }, + { baseBranch: "feature/base", local: true, branch: "feature/x", deleted: false }, + { baseBranch: "upstream", local: true, branch: "feature/x", deleted: false }, + { baseBranch: "upstream/feature/x", local: true, branch: "feature/x", deleted: false }, + { baseBranch: "dev", local: false, branch: "origin/dev", deleted: true }, ])( - "publishes a branch tracking $baseBranch (local=$local) under its own name, not the base", - ({ baseBranch, local }) => + "publishes $branch tracking $baseBranch (local=$local, deleted=$deleted) without updating the base", + ({ baseBranch, local, branch, deleted }) => Effect.gen(function* () { const cwd = yield* makeTmpDir(); const remote = yield* makeTmpDir("git-remote-"); @@ -7923,55 +9218,78 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { "checkout", "--track", "-b", - "feature/x", + branch, local ? `refs/heads/${baseBranch}` : `refs/remotes/upstream/${baseBranch}`, ]); assert.equal( - yield* driver.readConfigValue(cwd, "branch.feature/x.remote"), + yield* driver.readConfigValue(cwd, `branch.${branch}.remote`), local ? "." : "upstream", ); yield* writeTextFile(cwd, "feature.txt", "feature\n"); yield* git(cwd, ["add", "-A"]); yield* driver.commit(cwd, "Add feature", ""); + if (deleted) { + yield* git(cwd, ["push", "upstream", "--delete", baseBranch]); + } + const publishBranch = branch === "origin/dev" ? "dev" : branch; + assert.deepInclude( + yield* driver.resolvePushTarget(cwd, { branch, hasUpstream: !deleted }), + { remoteName: "origin", branchName: publishBranch }, + ); const pushed = yield* driver.pushCurrentBranch(cwd, null); assert.deepInclude(pushed, { status: "pushed", - branch: "feature/x", - upstreamBranch: "origin/feature/x", + branch, + upstreamBranch: `origin/${publishBranch}`, setUpstream: true, }); assert.equal( - yield* git(remote, ["--git-dir=.", "log", "-1", "--pretty=%s", "feature/x"]), + yield* git(remote, ["--git-dir=.", "log", "-1", "--pretty=%s", publishBranch]), "Add feature", ); + if (publishBranch !== baseBranch) { + assert.equal( + yield* git(remote, ["--git-dir=.", "rev-parse", `refs/heads/${baseBranch}`]), + devSha, + ); + } assert.equal( - yield* git(remote, ["--git-dir=.", "rev-parse", `refs/heads/${baseBranch}`]), - devSha, - ); - assert.equal( - yield* git(upstream, ["--git-dir=.", "rev-parse", `refs/heads/${baseBranch}`]), - upstreamDevSha, + yield* git(upstream, [ + "--git-dir=.", + "for-each-ref", + "--format=%(objectname)", + `refs/heads/${baseBranch}`, + ]), + deleted ? "" : upstreamDevSha, ); assert.equal( - yield* git(cwd, ["rev-parse", "--abbrev-ref", "@{upstream}"]), - "origin/feature/x", + yield* git(cwd, ["rev-parse", "--symbolic-full-name", "@{upstream}"]), + `refs/remotes/origin/${publishBranch}`, ); assert.equal( - yield* driver.readConfigValue(cwd, "branch.feature/x.t3-merge-base"), + yield* driver.readConfigValue(cwd, `branch.${branch}.t3-merge-base`), baseBranch, ); assert.equal( - yield* driver.readConfigValue(cwd, "branch.feature/x.t3-merge-base-ref"), + yield* driver.readConfigValue(cwd, `branch.${branch}.t3-merge-base-ref`), local ? `refs/heads/${baseBranch}` : `refs/remotes/upstream/${baseBranch}`, ); assert.equal(yield* git(cwd, ["rev-parse", `refs/heads/${baseBranch}`]), upstreamDevSha); + if (deleted) return; + const preview = yield* driver.getReviewDiffPreview({ cwd }); const changes = preview.sources.find((source) => source.kind === "branch-range")!; assert.deepStrictEqual(changes.files, [ - { path: "feature.txt", previousPath: null, additions: 1, deletions: 0 }, + { + path: "feature.txt", + previousPath: null, + changeType: "new", + additions: 1, + deletions: 0, + }, ]); assert.equal( changes.baseRef, @@ -8116,6 +9434,32 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { yield* git(remote, ["--git-dir=.", "log", "-1", "--pretty=%s", "effect-atom"]), "Add alias update", ); + + yield* git(cwd, ["push", "my-org/upstream", "--delete", "effect-atom"]); + assert.deepInclude( + yield* driver.resolvePushTarget(cwd, { + branch: "upstream/effect-atom", + hasUpstream: false, + }), + { remoteName: "my-org/upstream", branchName: "effect-atom" }, + ); + assert.deepInclude(yield* driver.pushCurrentBranch(cwd, null), { + upstreamBranch: "my-org/upstream/effect-atom", + setUpstream: true, + }); + assert.equal( + yield* git(remote, ["--git-dir=.", "rev-parse", "refs/heads/effect-atom"]), + yield* git(cwd, ["rev-parse", "HEAD"]), + ); + assert.equal( + yield* git(remote, [ + "--git-dir=.", + "for-each-ref", + "--format=%(refname)", + "refs/heads/upstream/", + ]), + "", + ); }), ); @@ -8249,29 +9593,73 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => { }), ); - it.effect("skips an up-to-date tracked branch the push remote already has", () => + it.effect.each([ + { pushUrls: [], status: "skipped_up_to_date" }, + { pushUrls: ["mirror"], status: "pushed" }, + { pushUrls: ["fork", "mirror"], status: "pushed" }, + ] as const)( + "skips an up-to-date tracked branch only when every push URL has it (pushurl $pushUrls)", + ({ pushUrls, status }) => + Effect.gen(function* () { + const cwd = yield* makeTmpDir(); + const upstreamRemote = yield* makeTmpDir("git-upstream-remote-"); + const remotes = { + fork: yield* makeTmpDir("git-fork-remote-"), + mirror: yield* makeTmpDir("git-mirror-remote-"), + }; + yield* initRepoWithCommit(cwd); + const driver = yield* GitVcsDriver.GitVcsDriver; + yield* git(upstreamRemote, ["init", "--bare"]); + yield* git(remotes.fork, ["init", "--bare"]); + yield* git(remotes.mirror, ["init", "--bare"]); + yield* git(cwd, ["remote", "add", "upstream", upstreamRemote]); + yield* git(cwd, ["remote", "add", "fork", remotes.fork]); + yield* git(cwd, ["checkout", "-b", "topic"]); + yield* git(cwd, ["push", "-u", "upstream", "topic"]); + yield* git(cwd, ["push", "fork", "topic"]); + yield* git(cwd, ["config", "remote.pushDefault", "fork"]); + for (const name of pushUrls) { + yield* git(cwd, ["remote", "set-url", "--add", "--push", "fork", remotes[name]]); + } + + const pushed = yield* driver.pushCurrentBranch(cwd, null); + + assert.deepInclude(pushed, { status, upstreamBranch: "fork/topic", pushRemote: "fork" }); + const head = yield* git(cwd, ["rev-parse", "HEAD"]); + for (const name of pushUrls) { + assert.equal( + yield* git(remotes[name], ["--git-dir=.", "rev-parse", "refs/heads/topic"]), + head, + ); + } + assert.equal( + yield* git(cwd, ["rev-parse", "--abbrev-ref", "@{upstream}"]), + "upstream/topic", + ); + }), + ); + + it.effect("pushes an up-to-date branch whose tracked remote has a separate push URL", () => Effect.gen(function* () { const cwd = yield* makeTmpDir(); - const upstreamRemote = yield* makeTmpDir("git-upstream-remote-"); - const forkRemote = yield* makeTmpDir("git-fork-remote-"); + const fetchRemote = yield* makeTmpDir("git-fetch-remote-"); + const pushRemote = yield* makeTmpDir("git-push-remote-"); yield* initRepoWithCommit(cwd); const driver = yield* GitVcsDriver.GitVcsDriver; - yield* git(upstreamRemote, ["init", "--bare"]); - yield* git(forkRemote, ["init", "--bare"]); - yield* git(cwd, ["remote", "add", "upstream", upstreamRemote]); - yield* git(cwd, ["remote", "add", "fork", forkRemote]); + yield* git(fetchRemote, ["init", "--bare"]); + yield* git(pushRemote, ["init", "--bare"]); + yield* git(cwd, ["remote", "add", "origin", fetchRemote]); yield* git(cwd, ["checkout", "-b", "topic"]); - yield* git(cwd, ["push", "-u", "upstream", "topic"]); - yield* git(cwd, ["push", "fork", "topic"]); - yield* git(cwd, ["config", "remote.pushDefault", "fork"]); + yield* git(cwd, ["push", "-u", "origin", "topic"]); + yield* git(cwd, ["remote", "set-url", "--push", "origin", pushRemote]); const pushed = yield* driver.pushCurrentBranch(cwd, null); - assert.deepInclude(pushed, { - status: "skipped_up_to_date", - upstreamBranch: "fork/topic", - pushRemote: "fork", - }); + assert.deepInclude(pushed, { status: "pushed", upstreamBranch: "origin/topic" }); + assert.equal( + yield* git(pushRemote, ["--git-dir=.", "rev-parse", "refs/heads/topic"]), + yield* git(cwd, ["rev-parse", "HEAD"]), + ); }), ); diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index 3df984a107ae6..8c9f0ab649785 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -1,8 +1,11 @@ // Fork: Pass the account environment to commits/pushes. import * as Cache from "effect/Cache"; +import * as Cause from "effect/Cause"; import * as Clock from "effect/Clock"; import * as Data from "effect/Data"; +import * as Deferred from "effect/Deferred"; import * as Digest from "effect/crypto/Digest"; +import * as SecureRandom from "effect/crypto/SecureRandom"; import * as DateTime from "effect/DateTime"; import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; @@ -27,6 +30,7 @@ import { type GitCommandFailureReason, T3_PROJECT_FILE_NAME, type ReviewBranchCommitsInput, + type ReviewDiffChangeType, type ReviewDiffFileContentsInput, type ReviewDiffPreviewInput, type ReviewDiffFileStat, @@ -43,6 +47,7 @@ import { resolveProjectFileBackedSetting } from "@t3tools/shared/projectSettings import { gitCommandDuration, gitCommandsTotal, withMetrics } from "../observability/Metrics.ts"; import * as GitVcsDriver from "./GitVcsDriver.ts"; import { removeEmptyDirectory, type RemoveEmptyDirectoryError } from "./removeEmptyDirectory.ts"; +import { VcsAutomaticFetchInterval } from "./VcsAutomaticFetchInterval.ts"; import { explicitBareRepositoryEnv, gitConfigEnv, @@ -68,6 +73,10 @@ const branchBaseConfigKey = (branch: string) => `branch.${branch}.t3-merge-base` * CLI's key, which branches cut before then still carry instead. */ const legacyBranchBaseConfigKey = (branch: string) => `branch.${branch}.gh-merge-base`; +// Differently named upstreams are bases, not publish targets; pushing HEAD there would write features to shared branches. +const isAliasOfUpstreamHead = (branch: string, upstreamBranch: string, upstreamRef: string) => + branch === upstreamBranch || + (branch.endsWith(`/${upstreamBranch}`) && upstreamRef.endsWith(`/${branch}`)); const gitProcesses = Semaphore.makeUnsafe(8); const backgroundGitProcesses = Semaphore.makeUnsafe(2); // The fetch before `git worktree add` downloads everything the remote gained since @@ -76,6 +85,7 @@ const FETCH_REMOTE_TIMEOUT_MS = Duration.toMillis(Duration.minutes(5)); const WORKTREE_INDEX_REFRESH_TIMEOUT_MS = Duration.toMillis(Duration.minutes(5)); const WORKTREE_REMOVE_TIMEOUT_MS = Duration.toMillis(Duration.minutes(5)); const DEFAULT_MAX_OUTPUT_BYTES = 1_000_000; +const REF_SNAPSHOT_MAX_OUTPUT_BYTES = 16 * 1024 * 1024; const OUTPUT_TRUNCATED_MARKER = "\n\n[truncated]"; const PREPARED_COMMIT_PATCH_MAX_OUTPUT_BYTES = 49_000; const RANGE_COMMIT_SUMMARY_MAX_OUTPUT_BYTES = 19_000; @@ -92,6 +102,8 @@ const REVIEW_UNTRACKED_MAX_BYTES = 256 * 1024 * 1024; const UNTRACKED_TOTALS_RETRY_MIN_MS = 60_000; const UNTRACKED_TOTALS_RETRY_MAX_MS = 30 * 60_000; const REVIEW_DIFF_FILE_MAX_OUTPUT_BYTES = 1024 * 1024; +// Git's ident clean step (convert.c ident_to_git): `$Id:` up to the next `$` on the same line. +const GIT_IDENT_KEYWORD = /\$Id:[^$\n]*\$/g; const REVIEW_BRANCH_COMMIT_LIMIT = 100; const REVIEW_BRANCH_SUBJECT_LIMIT = 300; const REVIEW_BRANCH_COMMIT_MAX_OUTPUT_BYTES = 128 * 1024; @@ -223,18 +235,41 @@ function parseBranchAb(value: string): { ahead: number; behind: number } { }; } +function rawChangeType(status: string): ReviewDiffChangeType { + switch (status[0]) { + case "A": + return "new"; + case "D": + return "deleted"; + case "R": + return status === "R100" ? "rename-pure" : "rename-changed"; + default: + return "change"; + } +} + +// Parses `--numstat -z`, optionally preceded by `--raw` records that carry each file's change type. function parseReviewNumstat(stdout: string): ReviewDiffFileStat[] { const fields = stdout.split("\0"); + const changeTypes = new Map(); const files: ReviewDiffFileStat[] = []; for (let index = 0; index < fields.length; index++) { const field = fields[index]!; + if (field.startsWith(":")) { + const status = field.slice(field.lastIndexOf(" ") + 1); + if (/^[RC]/.test(status)) index++; + changeTypes.set(fields[++index]!, rawChangeType(status)); + continue; + } const match = /^(\d+|-)\t(\d+|-)\t([\s\S]*)$/.exec(field); if (!match) continue; const previousPath = match[3] === "" ? fields[++index]! : null; const path = previousPath !== null ? fields[++index]! : match[3]!; + const changeType = changeTypes.get(path); files.push({ path, previousPath, + ...(changeType === undefined ? {} : { changeType }), additions: match[1] === "-" ? 0 : Number(match[1]), deletions: match[2] === "-" ? 0 : Number(match[2]), }); @@ -335,6 +370,14 @@ function splitNullSeparatedPaths(input: string, truncated: boolean): string[] { return parts.filter((value) => value.length > 0); } +// Parses "git version 2.24.3 (Apple Git-128)" or "git version 2.45.1.windows.1". +const gitSupportsPathspecFromFile = (versionOutput: string): boolean => { + const match = /^git version (\d+)\.(\d+)/.exec(versionOutput); + if (match === null) return false; + const major = Number(match[1]); + return major > 2 || (major === 2 && Number(match[2]) >= 25); +}; + export function splitNullSeparatedGitStdoutPaths( result: Pick, ): string[] { @@ -590,6 +633,10 @@ export function isNonRepositoryGitStderr(stderr: string): boolean { const normalized = stderr.toLowerCase(); return NON_REPOSITORY_STDERR_PHRASES.some((phrase) => normalized.includes(phrase)); } +const UNTRACKED_FILES_SHOWN_CONFIG_VALUES = new Set(["normal", "all", "true", "yes", "on", "1"]); +function untrackedFilesHiddenByConfig(value: string | null): boolean { + return value !== null && !UNTRACKED_FILES_SHOWN_CONFIG_VALUES.has(value.toLowerCase()); +} function isUnbornHeadStderr(stderr: string): boolean { const normalized = stderr.toLowerCase(); return ( @@ -974,9 +1021,22 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* const { worktreesDir, worktreeCheckoutCacheDir } = yield* ServerConfig.ServerConfig; const isServerInternalPath = yield* makeIsServerInternalPath; const digest = yield* Digest.Digest; + const random = yield* SecureRandom.SecureRandom; const hostPlatform = yield* HostProcess.Platform; const serviceScope = yield* Effect.scope; - const statusFetches = new Map>(); + const automaticFetchInterval = yield* VcsAutomaticFetchInterval; + type StatusFetchState = { + phase: "queued" | "running" | "claimed"; + claimed: Deferred.Deferred; + finished: Deferred.Deferred; + }; + const statusFetches = new Map< + string, + { + state: StatusFetchState; + fiber: Fiber.Fiber; + } + >(); const statusFetchProcesses = yield* Semaphore.make(4); const executeRaw: GitVcsDriver.GitVcsDriver["Service"]["execute"] = Effect.fnUntraced( @@ -1403,11 +1463,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* const fiber = yield* Effect.uninterruptible( Effect.gen(function* () { const pending = statusFetches.get(key); - if (pending) return pending; + if (pending) return pending.fiber; + const claimed = yield* Deferred.make(); + const finished = yield* Deferred.make(); + const state: StatusFetchState = { phase: "queued", claimed, finished }; // Frequent poll deadlines and disconnected clients must not interrupt a // healthy writer. A separate safety limit prevents a hung transport from // occupying its worker slot indefinitely. - const started = yield* executeGit("GitVcsDriver.fetchRemoteForStatus", fetchCwd, args, { + const fetch = executeGit("GitVcsDriver.fetchRemoteForStatus", fetchCwd, args, { env: STATUS_UPSTREAM_REFRESH_ENV, fallbackErrorDetail: "Background Git fetch exited with a non-zero status.", timeoutMs: Duration.toMillis(STATUS_UPSTREAM_FETCH_TIMEOUT), @@ -1420,18 +1483,39 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* ), Effect.tapError(() => recordStatusRemoteRefreshFailure(key)), Effect.tapCause((cause) => Effect.logWarning("Background Git fetch failed", cause)), - statusFetchProcesses.withPermits(1), + ); + const started = yield* Effect.raceFirst( + Effect.uninterruptibleMask((restore) => + Effect.gen(function* () { + if (state.phase !== "queued") return false; + state.phase = "running"; + yield* restore(fetch); + return true; + }), + ).pipe(statusFetchProcesses.withPermits(1)), + Deferred.await(claimed).pipe(Effect.as(false)), + ).pipe( + Effect.flatMap((completed) => + completed + ? Effect.void + : Deferred.await(finished).pipe( + Effect.flatMap((succeeded) => + succeeded ? Effect.void : fetch.pipe(statusFetchProcesses.withPermits(1)), + ), + ), + ), Effect.onExit(() => Effect.sync(() => { - statusFetches.delete(key); + if (statusFetches.get(key)?.state === state) statusFetches.delete(key); }), ), Effect.forkIn(serviceScope, { startImmediately: false, uninterruptible: false }), ); - statusFetches.set(key, started); + statusFetches.set(key, { state, fiber: started }); return started; }), ); + yield* Effect.annotateCurrentSpan("statusFetch.joining", true); const result = yield* Fiber.join(fiber).pipe( Effect.timeoutOption(STATUS_UPSTREAM_REFRESH_TIMEOUT), ); @@ -1691,9 +1775,42 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* ), }); + const readRefSnapshot: GitVcsDriver.GitVcsDriver["Service"]["readRefSnapshot"] = Effect.fn( + "GitVcsDriver.readRefSnapshot", + )( + function* (cwd) { + const result = yield* executeGit( + "GitVcsDriver.readRefSnapshot", + cwd, + ["show-ref", "--head"], + { + allowNonZeroExit: true, + maxOutputBytes: REF_SNAPSHOT_MAX_OUTPUT_BYTES, + }, + ); + if (result.exitCode !== 0) return null; + const symbolicHead = yield* executeGit( + "GitVcsDriver.readRefSnapshot", + cwd, + ["symbolic-ref", "--quiet", "HEAD"], + { allowNonZeroExit: true }, + ); + // Exit 1 means detached HEAD; other failures require a full status read. + if (symbolicHead.exitCode !== 0 && symbolicHead.exitCode !== 1) return null; + // Deepening a shallow clone changes ancestry without moving refs. + const shallowPath = path.join(yield* resolveGitCommonDir(cwd), "shallow"); + const shallow = (yield* fileSystem.exists(shallowPath)) + ? yield* fileSystem.readFileString(shallowPath) + : ""; + return `${result.stdout}\0${symbolicHead.stdout}\0${shallow}`; + }, + Effect.orElseSucceed(() => null), + ); + const refreshStatusUpstreamIfStale = Effect.fn("refreshStatusUpstreamIfStale")(function* ( cwd: string, ) { + if (Duration.isZero(yield* automaticFetchInterval)) return; const headRef = yield* runGitStdout( "GitVcsDriver.refreshStatusUpstreamIfStale.headRef", cwd, @@ -1847,12 +1964,17 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* const resolvePushRemoteName = Effect.fn("resolvePushRemoteName")(function* ( cwd: string, refName: string, + fallbackRemoteName: string | null = null, ) { const configuredRemote = yield* resolveConfiguredPushRemoteName(cwd, refName); if (configuredRemote) { return configuredRemote; } + if (fallbackRemoteName) { + return fallbackRemoteName; + } + return yield* resolvePrimaryRemoteName(cwd).pipe(Effect.orElseSucceed(() => null)); }); @@ -1867,6 +1989,34 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* Effect.orElseSucceed(() => false), ); + // A fetch reads only the remote's first url, while a push writes every push URL (pushurl, else + // each url). A fetched ref is evidence of the push destination only when those are the same one. + const pushesOnlyToFetchUrl = Effect.fn("GitVcsDriver.pushesOnlyToFetchUrl")( + function* (cwd: string, remoteName: string) { + const [fetchUrl, pushUrls] = yield* Effect.all([ + runGitStdout("GitVcsDriver.pushesOnlyToFetchUrl.fetchUrl", cwd, [ + "remote", + "get-url", + "--", + remoteName, + ]), + runGitStdout("GitVcsDriver.pushesOnlyToFetchUrl.pushUrls", cwd, [ + "remote", + "get-url", + "--push", + "--all", + "--", + remoteName, + ]), + ]); + // Preserve whitespace in URLs and reject ambiguous multiline fetch URLs. + return ( + fetchUrl.endsWith("\n") && !fetchUrl.slice(0, -1).includes("\n") && pushUrls === fetchUrl + ); + }, + Effect.orElseSucceed(() => false), + ); + const ensureRemote: GitVcsDriver.GitVcsDriver["Service"]["ensureRemote"] = Effect.fn( "ensureRemote", )(function* (input) { @@ -2188,11 +2338,18 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* options?: GitVcsDriver.GitLocalStatusOptions, ) { const includeDivergence = options?.includeDivergence !== false; + // Hidden untracked files could not be committed; `all` stays as configured. + const untrackedFilesHidden = untrackedFilesHiddenByConfig( + yield* readConfigValue(cwd, "status.showUntrackedFiles").pipe( + Effect.orElseSucceed(() => null), + ), + ); const statusArgs = [ "status", "--porcelain=2", "--branch", "-z", + ...(untrackedFilesHidden ? ["--untracked-files=normal"] : []), ...(includeDivergence ? [] : ["--no-ahead-behind"]), ]; const indexResult = yield* executeGitWithStableDiagnostics( @@ -2249,41 +2406,25 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* isMissingGitCwdError(error) ? Effect.succeed(null) : Effect.fail(error), }), ); + // Porcelain diff rewrites indexes despite GIT_OPTIONAL_LOCKS=0; plumbing with -M preserves rename detection. + const readNumstat = (ref: string) => + executeGitWithStableDiagnostics( + "GitVcsDriver.statusDetails.numstat", + cwd, + ["diff-index", "-M", "--numstat", "-z", ref, "--"], + { allowNonZeroExit: true, maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES }, + ); // diff-index does not read status output, so both spawn together; a non-repository discards it. const [statusResult, numstatResult, defaultBranch, hasPrimaryRemote] = yield* Effect.all( [ readStatus, - // Porcelain diff rewrites indexes despite GIT_OPTIONAL_LOCKS=0; plumbing with -M preserves rename detection. - executeGitWithStableDiagnostics( - "GitVcsDriver.statusDetails.numstat", - cwd, - ["diff-index", "-M", "--numstat", "-z", "HEAD", "--"], - { allowNonZeroExit: true, maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES }, - ).pipe( + readNumstat("HEAD").pipe( + Effect.filterOrElse( + (result) => result.exitCode === 0 || !isUnbornHeadStderr(result.stderr), + () => Effect.flatMap(readEmptyTreeHash(cwd), readNumstat), + ), Effect.flatMap((result) => { if (result.exitCode === 0) return Effect.succeed(parseReviewNumstat(result.stdout)); - if (isUnbornHeadStderr(result.stderr)) { - return Effect.map( - Effect.all([ - runGitStdoutWithOptions( - "GitVcsDriver.statusDetails.numstat.unborn", - cwd, - ["diff-files", "--numstat", "-z"], - { maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES }, - ), - runGitStdoutWithOptions( - "GitVcsDriver.statusDetails.numstat.unborn.staged", - cwd, - ["diff", "--cached", "--numstat", "-z"], - { maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES }, - ), - ]), - ([unstagedStdout, stagedStdout]) => [ - ...parseReviewNumstat(stagedStdout), - ...parseReviewNumstat(unstagedStdout), - ], - ); - } return Effect.fail( new GitCommandError({ ...gitCommandContext({ @@ -2340,6 +2481,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* let refName: string | null = null; let upstreamRef: string | null = null; + // Git omits branch.ab when the upstream ref is gone, e.g. deleted after its PR merged, + // and also for an unborn HEAD, which can still track a fetched upstream. + let upstreamRefExists = false; + let isUnbornHead = false; let aheadCount = 0; let behindCount = 0; let aheadOfDefaultCount = 0; @@ -2350,9 +2495,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* const statusRecords = statusStdout.split("\0"); for (let index = 0; index < statusRecords.length; index++) { const line = statusRecords[index]!; + if (line === "# branch.oid (initial)") { + isUnbornHead = true; + continue; + } if (line.startsWith("# branch.head ")) { const value = line.slice("# branch.head ".length).trim(); - refName = value === "(detached)" ? null : value; + // `(unknown)` is printed while HEAD names no resolvable branch, e.g. mid-`git clone`. + refName = value === "(detached)" || value === "(unknown)" ? null : value; continue; } if (line.startsWith("# branch.upstream ")) { @@ -2360,11 +2510,13 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* upstreamRef = value.length > 0 ? value : null; continue; } - if (includeDivergence && line.startsWith("# branch.ab ")) { - const value = line.slice("# branch.ab ".length).trim(); - const parsed = parseBranchAb(value); - aheadCount = parsed.ahead; - behindCount = parsed.behind; + if (line.startsWith("# branch.ab ")) { + upstreamRefExists = true; + if (includeDivergence) { + const parsed = parseBranchAb(line.slice("# branch.ab ".length).trim()); + aheadCount = parsed.ahead; + behindCount = parsed.behind; + } continue; } if (line.length > 0 && !line.startsWith("#")) { @@ -2383,6 +2535,8 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* } } + if (!upstreamRefExists && !isUnbornHead) upstreamRef = null; + const isDefaultBranch = refName !== null && (refName === defaultBranch || @@ -2507,6 +2661,18 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* })), ); + // git add takes --pathspec-from-file from Git 2.25; older Git keeps the argument form. + let pathspecFromFileSupported: boolean | undefined; + const supportsPathspecFromFile = Effect.fn("GitVcsDriver.supportsPathspecFromFile")(function* ( + cwd: string, + ) { + if (pathspecFromFileSupported !== undefined) return pathspecFromFileSupported; + const version = yield* executeGit("GitVcsDriver.version", cwd, ["version"]).pipe(Effect.option); + if (Option.isNone(version)) return false; + pathspecFromFileSupported = gitSupportsPathspecFromFile(version.value.stdout); + return pathspecFromFileSupported; + }); + const stageCommitChanges = Effect.fnUntraced(function* ( cwd: string, filePaths?: readonly string[], @@ -2515,6 +2681,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* const config = env ? ["-c", "core.splitIndex=false", "-c", "splitIndex.sharedIndexExpire=never"] : []; + let preserveUnselectedConflicts = false; if (filePaths && filePaths.length > 0) { if (env) { const head = yield* executeGit( @@ -2535,14 +2702,40 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* { head: "MERGE_HEAD", operation: "merge" }, { head: "CHERRY_PICK_HEAD", operation: "cherry-pick" }, { head: "REVERT_HEAD", operation: "revert" }, + // A conflicted multi-commit `cherry-pick --no-commit` writes only the sequencer; reset + // would clear its unmerged entries, so `--continue` would walk past the conflict. + { head: "sequencer", operation: "cherry-pick or revert" }, ] as const; const headPaths = yield* runGitStdout("GitVcsDriver.commit.mergeHead", cwd, [ "rev-parse", ...inProgressHeads.flatMap(({ head }) => ["--git-path", head]), ]); const resolvedHeadPaths = headPaths.trim().split("\n"); - const inProgress = yield* Effect.findFirst(inProgressHeads, (_, index) => - fileSystem.exists(path.resolve(cwd, resolvedHeadPaths[index] ?? "")), + // Reftable keeps CHERRY_PICK_HEAD and REVERT_HEAD as refs with no file under .git. A + // same-named branch resolves to its full name; an ambiguity warning would blank stdout. + const operationRefs = new Set(["CHERRY_PICK_HEAD", "REVERT_HEAD"]); + const isOperationRef = (head: string) => + runGitStdout( + "GitVcsDriver.commit.operationRef", + cwd, + [ + "-c", + "core.warnAmbiguousRefs=false", + "rev-parse", + "--verify", + "--quiet", + "--symbolic-full-name", + head, + ], + true, + ).pipe(Effect.map((ref) => ref.trim() === head)); + const inProgress = yield* Effect.findFirst(inProgressHeads, ({ head }, index) => + fileSystem.exists(path.resolve(cwd, resolvedHeadPaths[index] ?? "")).pipe( + Effect.filterOrElse( + (exists) => exists || !operationRefs.has(head), + () => isOperationRef(head), + ), + ), ).pipe( Effect.mapError( (cause) => @@ -2565,16 +2758,33 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* detail: `Cannot commit selected files during a ${operation}. Commit all files to finish the ${operation}.`, }); } - yield* runGit("GitVcsDriver.commit.reset", cwd, ["reset", "--quiet"]); + const selectedFiles = new Set(filePaths); + const unmergedPaths = yield* runGitStdoutWithOptions( + "GitVcsDriver.commit.unmergedPaths", + cwd, + ["-c", "diff.relative=false", "diff", "--cached", "--name-only", "--diff-filter=U", "-z"], + { maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES }, + ); + // Reset drops every conflict stage, so keep the index while unselected files are conflicted. + preserveUnselectedConflicts = unmergedPaths + .split("\0") + .some((file) => file.length > 0 && !selectedFiles.has(file)); + if (!preserveUnselectedConflicts) { + yield* runGit("GitVcsDriver.commit.reset", cwd, ["reset", "--quiet"]); + } } + // Status paths are repository-relative, while cwd can be a subdirectory. + const pathspecs = filePaths.map((file) => `:(top,literal)${file}`); + const fromStdin = yield* supportsPathspecFromFile(cwd); yield* runGit( "GitVcsDriver.commit.addSelected", cwd, - [...config, "add", "-A", "--pathspec-from-file=-", "--pathspec-file-nul"], + fromStdin + ? [...config, "add", "-A", "--pathspec-from-file=-", "--pathspec-file-nul"] + : [...config, "add", "-A", "--", ...pathspecs], { ...(env ? { env } : {}), - // Status paths are repository-relative, while cwd can be a subdirectory. - stdin: filePaths.map((file) => `:(top,literal)${file}\0`).join(""), + ...(fromStdin ? { stdin: pathspecs.map((pathspec) => `${pathspec}\0`).join("") } : {}), }, ); } else { @@ -2609,7 +2819,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* ); const reports = check.stdout.split("\n"); const conflicted: string[] = []; - let topLevel: string | undefined; + let emptyTree: string | undefined; for (const file of unmergedPaths) { const markerLines = new Set( reports.flatMap((line) => { @@ -2620,11 +2830,26 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* }), ); if (markerLines.size === 0) continue; - topLevel ??= (yield* runGitStdout("GitVcsDriver.commit.topLevel", cwd, [ - "rev-parse", - "--show-toplevel", - ])).trim(); - const lines = (yield* fileSystem.readFileString(path.join(topLevel, file)).pipe( + emptyTree ??= yield* readEmptyTreeHash(cwd); + // --check numbers lines after working-tree-encoding and clean filters; a diff from the + // empty tree prints that view. Read it in full so large resolved files can still commit. + const fileDiff = yield* runGitStdoutWithOptions( + "GitVcsDriver.commit.conflictMarkers", + cwd, + [ + "-c", + "diff.relative=false", + "diff", + "--no-color", + "--no-ext-diff", + "--no-textconv", + "--text", + emptyTree, + "--", + `:(top,literal)${file}`, + ], + { ...(env ? { env } : {}), maxOutputBytes: Number.POSITIVE_INFINITY }, + ).pipe( Effect.mapError( (cause) => new GitCommandError({ @@ -2635,7 +2860,12 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* cause, }), ), - )).split("\n"); + ); + const diffLines = fileDiff.split("\n"); + const lines = diffLines + .slice(diffLines.findIndex((line) => line.startsWith("@@")) + 1) + .filter((line) => line.startsWith("+")) + .map((line) => line.slice(1)); // Git also reports a lone "=======", which is valid content such as a heading underline. const reported = (marker: string) => lines.some((line, index) => markerLines.has(index) && line.startsWith(marker)); @@ -2657,6 +2887,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* env ? { env } : {}, ); } + return preserveUnselectedConflicts && filePaths + ? filePaths.map((file) => `:(top,literal)${file}`) + : undefined; }); const prepareCommitContext: GitVcsDriver.GitVcsDriver["Service"]["prepareCommitContext"] = @@ -2801,14 +3034,26 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* body, options?: GitVcsDriver.GitCommitOptions, ) { - if (options?.stage !== undefined) { - yield* stageCommitChanges(cwd, options.stage.filePaths); - } + const partialCommitPaths = + options?.stage !== undefined + ? yield* stageCommitChanges(cwd, options.stage.filePaths) + : undefined; const args = ["commit", "-m", subject]; const trimmedBody = body.trim(); if (trimmedBody.length > 0) { args.push("-m", trimmedBody); } + const onlyFromStdin = partialCommitPaths ? yield* supportsPathspecFromFile(cwd) : false; + const onlyPathspecArgs = onlyFromStdin + ? ["--pathspec-from-file=-", "--pathspec-file-nul"] + : ["--", ...(partialCommitPaths ?? [])]; + const onlyPathspecStdin = + partialCommitPaths && onlyFromStdin + ? { stdin: partialCommitPaths.map((pathspec) => `${pathspec}\0`).join("") } + : {}; + if (partialCommitPaths) { + args.push("--only", ...onlyPathspecArgs); + } const progress = options?.progress?.onOutputLine === undefined ? options?.progress @@ -2822,9 +3067,19 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* yield* executeGit("GitVcsDriver.commit.commit", cwd, args, { maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES, ...(options?.env !== undefined ? { env: options.env } : {}), + ...onlyPathspecStdin, ...(options?.timeoutMs !== undefined ? { timeoutMs: options.timeoutMs } : {}), ...(progress ? { progress } : {}), }).pipe(Effect.asVoid); + if (partialCommitPaths) { + // --only runs hooks on a temporary index, so a hook's `git add` never reaches the real one. + yield* runGit( + "GitVcsDriver.commit.syncSelectedIndex", + cwd, + ["reset", "--quiet", "HEAD", ...onlyPathspecArgs], + onlyPathspecStdin, + ); + } const commitSha = yield* runGitStdout("GitVcsDriver.commit.revParseHead", cwd, [ "rev-parse", "HEAD", @@ -2865,10 +3120,40 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* hasUpstream: boolean, ) { if (!hasUpstream) { - const remoteName = yield* resolvePushRemoteName(cwd, branch); + // A deleted upstream keeps its config: republish to it when it was this branch's own, + // otherwise it was the base, so keep that before publishing. + let goneUpstream: { remoteName: string; branchName: string } | null = null; + let goneBaseUpstream: { remoteName: string; branchName: string; upstreamRef: string } | null = + null; + const mergeRef = yield* readConfigValue(cwd, `branch.${branch}.merge`); + const mergeBranch = mergeRef?.startsWith("refs/heads/") + ? mergeRef.slice("refs/heads/".length) + : null; + const configuredRemoteName = yield* readConfigValue(cwd, `branch.${branch}.remote`); + if (mergeBranch && configuredRemoteName && configuredRemoteName !== ".") { + if (isAliasOfUpstreamHead(branch, mergeBranch, `${configuredRemoteName}/${mergeBranch}`)) { + goneUpstream = { remoteName: configuredRemoteName, branchName: mergeBranch }; + } else { + goneBaseUpstream = { + remoteName: configuredRemoteName, + branchName: mergeBranch, + upstreamRef: `${configuredRemoteName}/${mergeBranch}`, + }; + } + } + const remoteName = yield* resolvePushRemoteName(cwd, branch, goneUpstream?.remoteName); if (!remoteName) return null; - const branchName = yield* resolvePublishBranchName(cwd, branch); - return { remoteName, branchName, publishes: true, upstream: null, pushRemote: null }; + // A gone local upstream still gets the full local name, as below. + const branchName = + goneUpstream?.branchName ?? + (configuredRemoteName === "." ? branch : yield* resolvePublishBranchName(cwd, branch)); + return { + remoteName, + branchName, + publishes: true, + upstream: goneBaseUpstream, + pushRemote: null, + }; } if ((yield* readConfigValue(cwd, `branch.${branch}.remote`)) === ".") { @@ -2892,11 +3177,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* const upstream = yield* resolveCurrentUpstream(cwd).pipe(Effect.orElseSucceed(() => null)); if (!upstream) return null; - // Differently named upstreams are bases, not publish targets; pushing HEAD there would write features to shared branches. - const isAliasOfUpstreamHead = - branch === upstream.branchName || - (branch.endsWith(`/${upstream.branchName}`) && upstream.upstreamRef.endsWith(`/${branch}`)); - if (isAliasOfUpstreamHead) { + if (isAliasOfUpstreamHead(branch, upstream.branchName, upstream.upstreamRef)) { // A fork workflow pushes to the configured push remote and keeps the upstream for pulls, as plain `git push` does. const configuredPushRemote = yield* resolveConfiguredPushRemoteName(cwd, branch).pipe( Effect.orElseSucceed(() => null), @@ -2980,30 +3261,36 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* const destination = yield* resolvePushDestination(cwd, branch, details.hasUpstream); if (hasNoLocalDelta && details.hasUpstream) { if (!destination?.pushRemote) { - return { - status: "skipped_up_to_date" as const, - branch, - ...(details.upstreamRef ? { upstreamBranch: details.upstreamRef } : {}), - }; - } - const { pushRemote, branchName } = destination; - const pushRemoteRef = `${pushRemote}/${branchName}`; - // The local ref can be stale when the branch was deleted or rewound elsewhere. - const pushRemoteHasHead = yield* fetchRemoteTrackingBranch({ - cwd, - remoteName: pushRemote, - remoteBranch: branchName, - }).pipe( - Effect.andThen(remoteRefContainsHead(cwd, pushRemoteRef)), - Effect.orElseSucceed(() => false), - ); - if (pushRemoteHasHead) { - return { - status: "skipped_up_to_date" as const, - branch, - upstreamBranch: pushRemoteRef, - pushRemote, - }; + // The tracking ref reflects the fetch URL, so it says nothing about a separate push URL. + if (!destination || (yield* pushesOnlyToFetchUrl(cwd, destination.remoteName))) { + return { + status: "skipped_up_to_date" as const, + branch, + ...(details.upstreamRef ? { upstreamBranch: details.upstreamRef } : {}), + }; + } + } else { + const { pushRemote, branchName } = destination; + const pushRemoteRef = `${pushRemote}/${branchName}`; + // The local ref can be stale when the branch was deleted or rewound elsewhere. + const pushRemoteHasHead = + (yield* pushesOnlyToFetchUrl(cwd, pushRemote)) && + (yield* fetchRemoteTrackingBranch({ + cwd, + remoteName: pushRemote, + remoteBranch: branchName, + }).pipe( + Effect.andThen(remoteRefContainsHead(cwd, pushRemoteRef)), + Effect.orElseSucceed(() => false), + )); + if (pushRemoteHasHead) { + return { + status: "skipped_up_to_date" as const, + branch, + upstreamBranch: pushRemoteRef, + pushRemote, + }; + } } } else if (hasNoLocalDelta) { const comparableBaseBranch = yield* resolveBaseBranchForNoUpstream(cwd, branch).pipe( @@ -3019,7 +3306,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* // The configured diff base can contain HEAD while the publish branch is still behind. if ( - yield* remoteRefContainsHead(cwd, `${destination.remoteName}/${destination.branchName}`) + (yield* remoteRefContainsHead( + cwd, + `${destination.remoteName}/${destination.branchName}`, + )) && + (yield* pushesOnlyToFetchUrl(cwd, destination.remoteName)) ) { return { status: "skipped_up_to_date" as const, @@ -3139,12 +3430,31 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* }); } const remote = yield* readConfigValue(cwd, `branch.${refName}.remote`); + // The upstream-only retry leaves other remote-tracking refs stale, so a claimed status + // fetch must still run after it. + let fetchedUpstreamOnly = false; if (remote) { const gitCommonDir = yield* resolveGitCommonDir(cwd); const pending = statusFetches.get(`${gitCommonDir}\0${remote}`); // The status wait can finish while its fetch still owns reference locks. // Let our existing writer finish before starting the pull's fetch. - if (pending) yield* Fiber.await(pending); + if (pending) { + const claimed = yield* Effect.uninterruptible( + Effect.gen(function* () { + if (pending.state.phase !== "queued") return false; + pending.state.phase = "claimed"; + yield* Effect.addFinalizer((exit) => + Deferred.succeed( + pending.state.finished, + Exit.isSuccess(exit) && !fetchedUpstreamOnly, + ), + ); + yield* Deferred.succeed(pending.state.claimed, undefined); + return true; + }), + ); + if (!claimed) yield* Fiber.await(pending.fiber); + } } const beforeSha = yield* runGitStdout( "GitVcsDriver.pullCurrentBranch.beforeSha", @@ -3198,6 +3508,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* currentSha === beforeSha ) { args = ["pull", "--ff-only", "--", remote, mergeRef]; + fetchedUpstreamOnly = true; result = yield* executeGitWithStableDiagnostics( "GitVcsDriver.pullCurrentBranch.pull", cwd, @@ -3226,11 +3537,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* const refreshed = yield* readStatusDetailsLocal(cwd); return { - status: beforeSha.length > 0 && beforeSha === afterSha ? "skipped_up_to_date" : "pulled", + status: + beforeSha.length > 0 && beforeSha === afterSha + ? ("skipped_up_to_date" as const) + : ("pulled" as const), refName, upstreamRef: refreshed.upstreamRef, }; - }); + }, Effect.scoped); const readRangeContext: GitVcsDriver.GitVcsDriver["Service"]["readRangeContext"] = Effect.fn( "readRangeContext", @@ -3336,7 +3650,21 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* ); const stagedDeletions = new Set(stagedDeletionsStdout.split("\0").filter(Boolean)); // Staged deletions never enter the index, so their worktree copies must not spend the budget. - const candidates = untrackedPaths.filter((relativePath) => !stagedDeletions.has(relativePath)); + // A nested repository (listed with a trailing slash) with no commits cannot be indexed: + // Git before 2.53 fails the add, and later versions accept it and then fail every diff. + const candidates = yield* Effect.filter( + untrackedPaths.filter((relativePath) => !stagedDeletions.has(relativePath)), + (relativePath) => + relativePath.endsWith("/") + ? executeGit( + "GitVcsDriver.readUnifiedWorkingTreeReviewDiff.nestedRepositoryHead", + path.resolve(cwd, relativePath), + ["rev-parse", "--verify", "--quiet", "HEAD"], + { allowNonZeroExit: true }, + ).pipe(Effect.map((result) => result.exitCode !== 1)) + : Effect.succeed(true), + { concurrency: 16 }, + ); const sizes = yield* Effect.forEach( candidates, (relativePath) => @@ -3562,8 +3890,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* detail: remembered.detail, }); } - // Empty status can mean untracked files are hidden; retain the full scan. - const untracked = yield* prepareUntrackedReviewIndexOrReason(cwd, pathspecs, undefined, false); + // Status always lists untracked files, so no roots means there are none to scan. + const untracked = + roots.length === 0 + ? { env: undefined, omitted: false } + : yield* prepareUntrackedReviewIndexOrReason(cwd, pathspecs, undefined, false); if ("unavailable" in untracked) { const count = remembered?.roots === rootsKey ? remembered.count + 1 : 1; untrackedTotalsFailures.delete(cwd); @@ -3818,7 +4149,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* const stat = yield* executeGit( "GitVcsDriver.getReviewDiffPreview.commitStat", cwd, - [...diffArgs, "--numstat", "-z", commit.parent, commit.sha, "--", ...pathArgs], + [...diffArgs, "--raw", "--numstat", "-z", commit.parent, commit.sha, "--", ...pathArgs], { maxOutputBytes: REVIEW_METADATA_MAX_OUTPUT_BYTES }, ); const files = parseReviewNumstat(stat.stdout); @@ -3868,7 +4199,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* ref: string, env?: NodeJS.ProcessEnv, ) { - const args = [...diffArgs, "--numstat", "-z"]; + const args = [...diffArgs, "--raw", "--numstat", "-z"]; const result = yield* executeGit( "GitVcsDriver.getReviewDiffPreview.stat", cwd, @@ -4089,7 +4420,43 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* if (bytes.includes(0)) { return yield* fileError("fs.readFile", `Cannot expand binary file '${input.newPath}'.`); } - return new TextDecoder("utf-8", { ignoreBOM: true }).decode(bytes); + const contents = new TextDecoder("utf-8", { ignoreBOM: true }).decode(bytes); + if (!contents.includes("$Id:")) return contents; + const collapsed = contents.replace(GIT_IDENT_KEYWORD, () => "$Id$"); + if (collapsed === contents) return contents; + // check-attr reports both boolean ident and literal ident=set as "set". + const attribute = yield* runGitStdout( + "GitVcsDriver.getReviewDiffFileContents.workingTree.ident", + repositoryRoot, + ["check-attr", "-z", "ident", "filter", "--", input.newPath], + ); + const attributes = attribute.split("\0"); + if (attributes[2] !== "set" || attributes[5] !== "unspecified") return contents; + + // Literal filter=unspecified is indistinguishable from an absent filter in check-attr. + const filters = yield* executeGit( + "GitVcsDriver.getReviewDiffFileContents.workingTree.filters", + repositoryRoot, + ["config", "--name-only", "--get-regexp", "^filter\\.unspecified\\."], + { allowNonZeroExit: true }, + ); + if (filters.exitCode !== 1 || filters.stdout.length > 0) return contents; + // Probe a nonoverlapping keyword so ident conversion runs once on the actual file. + const [canonicalHash, collapsedHash] = yield* Effect.all([ + runGitStdoutWithOptions( + "GitVcsDriver.getReviewDiffFileContents.workingTree.canonicalHash", + repositoryRoot, + ["hash-object", "--stdin", `--path=${input.newPath}`], + { stdin: "$Id: probe $" }, + ), + runGitStdoutWithOptions( + "GitVcsDriver.getReviewDiffFileContents.workingTree.collapsedHash", + repositoryRoot, + ["hash-object", "--stdin", `--path=${input.newPath}`], + { stdin: "$Id$" }, + ), + ]); + return canonicalHash === collapsedHash ? collapsed : contents; }); const getReviewDiffFileContents = Effect.fn("getReviewDiffFileContents")(function* ( @@ -4650,6 +5017,40 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* yield* refresh; }); + // Git writes the lock reason during registration, before checkout or user hooks can run. + const releaseWorktreeRegistration = Effect.fn("releaseWorktreeRegistration")(function* ( + cwd: string, + reason: string, + remove: boolean, + ) { + const commonDir = yield* readGitCommonDir(cwd); + if (commonDir === null) return; + const registrations = path.join(commonDir, "worktrees"); + const entries = yield* fileSystem.readDirectory(registrations).pipe( + Effect.catchIf( + (error) => error.reason._tag === "NotFound", + () => Effect.succeed([]), + ), + ); + for (const entry of entries) { + const adminDir = path.join(registrations, entry); + const locked = yield* fileSystem + .readFileString(path.join(adminDir, "locked")) + .pipe(Effect.orElseSucceed(() => "")); + if (locked.replace(/\r?\n$/, "") !== reason) continue; + // gitdir can be relative to the registration with worktree.useRelativePaths enabled. + const gitdir = yield* fileSystem.readFileString(path.join(adminDir, "gitdir")); + const registered = path.dirname(path.resolve(adminDir, gitdir.replace(/\r?\n$/, ""))); + yield* runGit("GitVcsDriver.createWorktree.unlock", cwd, [ + "worktree", + "unlock", + "--", + registered, + ]); + if (remove) yield* removeWorktree({ cwd, path: registered, force: true }); + } + }); + const createWorktree: GitVcsDriver.GitVcsDriver["Service"]["createWorktree"] = Effect.fn( "createWorktree", )(function* (input, options) { @@ -4694,6 +5095,20 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* } } const newRefName = input.newRefName; + // Older Git still creates worktrees normally, but cannot prove ownership for cancellation. + const addHelp = newRefName + ? yield* executeGit( + "GitVcsDriver.createWorktree.capabilities", + input.cwd, + ["worktree", "add", "-h"], + { allowNonZeroExit: true }, + ) + : null; + const lockReason = + addHelp && `${addHelp.stdout}\n${addHelp.stderr}`.includes("--reason") + ? `t3-create-${yield* random.uuidV4()}` + : undefined; + if (newRefName) { // Kept apart from the checkout so the shared-config lock only covers the tracking write. yield* createBranchAt( @@ -4727,6 +5142,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* `checkout.workers=${checkoutWorkers}`, "worktree", "add", + ...(lockReason ? ["--lock", "--reason", lockReason] : []), "--", worktreePath, checkoutRef, @@ -4754,30 +5170,66 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* : {}), }, ); + // Set inside the claim's uninterruptible region: a pending cancel still surfaces as it ends. + let claimed = false; + const onWorktreeClaimed = progress?.onWorktreeClaimed; yield* ( fastCheckout ? checkoutCache - .create({ cwd: input.cwd, worktreePath, refName: checkoutRef, checkoutWorkers }) + .create({ + cwd: input.cwd, + worktreePath, + refName: checkoutRef, + checkoutWorkers, + lockReason, + }) .pipe(Effect.flatMap((cloned) => (cloned ? Effect.void : stockCheckout))) : stockCheckout ).pipe( - Effect.onError(() => - newRefName - ? deleteLocalBranch({ cwd: input.cwd, refName: newRefName, force: true }).pipe( + Effect.andThen( + fastCheckout ? checkoutCache.schedule(worktreePath, cacheGeneration) : Effect.void, + ), + Effect.andThen( + onWorktreeClaimed + ? Effect.uninterruptible( + onWorktreeClaimed(worktreePath).pipe( + Effect.andThen( + Effect.sync(() => { + claimed = true; + }), + ), + ), + ) + : Effect.void, + ), + Effect.onError((cause) => + newRefName && !claimed + ? // A failed hook keeps its worktree, as Git does; only a cancel abandons it. + (Cause.hasInterrupts(cause) + ? lockReason + ? releaseWorktreeRegistration(input.cwd, lockReason, true) + : Effect.void + : Effect.void + ).pipe( + Effect.andThen( + deleteLocalBranch({ cwd: input.cwd, refName: newRefName, force: true }), + ), Effect.catch((cause) => - Effect.logWarning("could not remove the branch of a failed worktree", { cause }), + Effect.logWarning("could not clean up a failed worktree or its branch", { cause }), + ), + ) + : Effect.void, + ), + Effect.onExit(() => + lockReason + ? releaseWorktreeRegistration(input.cwd, lockReason, false).pipe( + Effect.catch((cause) => + Effect.logWarning("could not unlock a created worktree", { cause }), ), ) : Effect.void, ), ); - if (fastCheckout) { - yield* checkoutCache.schedule(worktreePath, cacheGeneration); - } - - if (progress?.onWorktreeClaimed) { - yield* progress.onWorktreeClaimed(worktreePath); - } // worktree add leaves submodules empty, potentially omitting project skills, tools or source. const hasSubmodules = yield* fileSystem @@ -4812,6 +5264,61 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* yield* progress.onSubmodulesStarted(); } const onSubmoduleLine = progress?.onSubmoduleLine; + const updateSubmodules = (paths: readonly string[]) => + runGit( + "GitVcsDriver.createWorktree.updateSubmodules", + worktreePath, + [ + "submodule", + "update", + "--init", + ...(submoduleMode.value === "recursive" ? ["--recursive"] : []), + // Git reports transfer progress to a pipe only when forced. + ...(onSubmoduleLine ? ["--progress"] : []), + // A clone left by a failed update already has HEAD at the gitlink, so Git would skip it. + ...(paths.length > 0 ? ["--force", "--", ...paths] : []), + ], + { + // Cloning scales with remote size; let it finish or be cancelled, like worktree add. + timeoutMs: null, + ...(onSubmoduleLine + ? { + env: { LANGUAGE: "C" }, + progress: { onStdoutLine: onSubmoduleLine, onStderrLine: onSubmoduleLine }, + // Progress for many submodules can pass the output cap; truncate instead of failing. + appendTruncationMarker: true, + keepLineCallbacksAfterTruncation: true, + } + : {}), + }, + ); + // One failed clone stops Git before it checks out any submodule, leaving the cloned ones + // as empty working trees that read as modified; check those out on their own. + const checkOutClonedSubmodules = Effect.gen(function* () { + // Records are "submodule..path\n". + const submodulePaths = splitNullSeparatedPaths( + yield* runGitStdout("GitVcsDriver.createWorktree.listSubmodules", worktreePath, [ + "config", + "-z", + "--file", + ".gitmodules", + "--get-regexp", + "^submodule\\..*\\.path$", + ]), + false, + ).flatMap((record) => { + const newline = record.indexOf("\n"); + return newline === -1 ? [] : [record.slice(newline + 1)]; + }); + const clonedPaths = yield* Effect.filter(submodulePaths, (submodulePath) => + fileSystem + .exists(path.join(worktreePath, submodulePath, ".git")) + .pipe(Effect.orElseSucceed(() => false)), + ); + if (clonedPaths.length > 0) { + yield* updateSubmodules(clonedPaths); + } + }); // Registering submodule URLs writes the shared config; the clones that follow do not. yield* runGit( "GitVcsDriver.createWorktree.initSubmodules", @@ -4819,39 +5326,23 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* ["submodule", "init"], { writesSharedConfig: true }, ).pipe( - Effect.andThen( - runGit( - "GitVcsDriver.createWorktree.updateSubmodules", - worktreePath, - [ - "submodule", - "update", - "--init", - ...(submoduleMode.value === "recursive" ? ["--recursive"] : []), - // Git reports transfer progress to a pipe only when forced. - ...(onSubmoduleLine ? ["--progress"] : []), - ], - { - // Cloning scales with remote size; let it finish or be cancelled, like worktree add. - timeoutMs: null, - ...(onSubmoduleLine - ? { - env: { LANGUAGE: "C" }, - progress: { onStdoutLine: onSubmoduleLine, onStderrLine: onSubmoduleLine }, - // Progress for many submodules can pass the output cap; truncate instead of failing. - appendTruncationMarker: true, - keepLineCallbacksAfterTruncation: true, - } - : {}), - }, - ), - ), + Effect.andThen(updateSubmodules([])), Effect.matchEffect({ onFailure: (cause) => Effect.logWarning("worktree submodule checkout failed; submodules may be incomplete", { worktreePath, cause, }).pipe( + Effect.andThen( + checkOutClonedSubmodules.pipe( + Effect.catch((retryCause) => + Effect.logWarning("could not check out the submodules that did clone", { + worktreePath, + cause: retryCause, + }), + ), + ), + ), Effect.andThen( progress?.onSubmodulesFinished ? progress.onSubmodulesFinished({ ok: false, detail: cause.message }) @@ -5144,8 +5635,12 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* remoteName: "origin", branchName: input.refName, }; - // `/HEAD` is the remote's default-branch pointer, not a branch it can serve. - return remoteNames.includes(remoteName) && branchName !== "HEAD" + // `/HEAD` is the remote's default-branch pointer, not a branch it can serve, and + // a revision expression such as `HEAD~1`, `@{-1}` or `:/text` is no branch name at all. + return remoteNames.includes(remoteName) && + branchName !== "HEAD" && + !branchName.startsWith("-") && + !/[\x00-\x20\x7f~^:?*[\\]|\.\.|@\{/.test(branchName) ? { remoteName, branchName } : null; }); @@ -5375,22 +5870,28 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* if (input.oldBranch === input.newBranch) { return { branch: input.newBranch }; } - const targetBranch = input.exactName - ? input.newBranch - : yield* resolveAvailableBranchName(input.cwd, input.newBranch); - - yield* executeGit( - "GitVcsDriver.renameBranch", + // Parallel launches often generate the same name; picking it under the lock that serializes + // the renames keeps a second launch from choosing a name the first is about to take. + return yield* withSharedConfigLock( input.cwd, - ["branch", "-m", "--", input.oldBranch, targetBranch], - { - timeoutMs: 10_000, - fallbackErrorDetail: "git branch rename failed", - writesSharedConfig: true, - }, - ); + Effect.gen(function* () { + const targetBranch = input.exactName + ? input.newBranch + : yield* resolveAvailableBranchName(input.cwd, input.newBranch); - return { branch: targetBranch }; + yield* executeGit( + "GitVcsDriver.renameBranch", + input.cwd, + ["branch", "-m", "--", input.oldBranch, targetBranch], + { + timeoutMs: 10_000, + fallbackErrorDetail: "git branch rename failed", + }, + ); + + return { branch: targetBranch }; + }), + ); }); const switchRef: GitVcsDriver.GitVcsDriver["Service"]["switchRef"] = Effect.fn("switchRef")( @@ -5632,6 +6133,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* statusDetails, statusDetailsLocal, statusDetailsRemote, + readRefSnapshot, prepareCommitContext, commit: Effect.fnUntraced(function* (cwd, subject, body, options) { return yield* withListRefsInvalidation(cwd, commit(cwd, subject, body, options)); diff --git a/apps/server/src/vcs/VcsAutomaticFetchInterval.ts b/apps/server/src/vcs/VcsAutomaticFetchInterval.ts new file mode 100644 index 0000000000000..0f2c900b3ba11 --- /dev/null +++ b/apps/server/src/vcs/VcsAutomaticFetchInterval.ts @@ -0,0 +1,26 @@ +import * as Context from "effect/Context"; +import * as Duration from "effect/Duration"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import { DEFAULT_AUTOMATIC_GIT_FETCH_INTERVAL } from "@t3tools/contracts"; +import { resolveServerBackgroundActivitySettings } from "@t3tools/shared/backgroundActivitySettings"; + +import * as ServerSettings from "../serverSettings.ts"; + +export class VcsAutomaticFetchInterval extends Context.Reference>( + "t3/vcs/VcsAutomaticFetchInterval", + { defaultValue: () => Effect.succeed(DEFAULT_AUTOMATIC_GIT_FETCH_INTERVAL) }, +) {} + +export const layer = Layer.effect( + VcsAutomaticFetchInterval, + Effect.gen(function* () { + const serverSettings = yield* ServerSettings.ServerSettingsService; + return serverSettings.getSettings.pipe( + Effect.map( + (settings) => resolveServerBackgroundActivitySettings(settings).automaticGitFetchInterval, + ), + Effect.orElseSucceed(() => DEFAULT_AUTOMATIC_GIT_FETCH_INTERVAL), + ); + }), +); diff --git a/apps/server/src/vcs/VcsProcess.ts b/apps/server/src/vcs/VcsProcess.ts index 4bfdcc90037ec..da2f988ce1ed8 100644 --- a/apps/server/src/vcs/VcsProcess.ts +++ b/apps/server/src/vcs/VcsProcess.ts @@ -33,6 +33,7 @@ export interface VcsProcessInput { readonly spawnCwd?: string; readonly stdin?: string; readonly onStdoutChunk?: (chunk: Uint8Array) => void; + readonly onSpawn?: () => void; readonly env?: NodeJS.ProcessEnv; readonly allowNonZeroExit?: boolean; readonly timeoutMs?: number; @@ -232,6 +233,7 @@ export const make = Effect.gen(function* () { ...(input.spawnCwd !== undefined ? { spawnCwd: input.spawnCwd } : {}), ...(input.stdin !== undefined ? { stdin: input.stdin } : {}), ...(input.onStdoutChunk !== undefined ? { onStdoutChunk: input.onStdoutChunk } : {}), + ...(input.onSpawn !== undefined ? { onSpawn: input.onSpawn } : {}), env: { ...(input.command === "git" ? unsetInheritedRepositoryEnv(process.env, hostPlatform) diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts index 96e29039abbd2..08fad4ae56aff 100644 --- a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts +++ b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts @@ -28,6 +28,7 @@ import type { import { GitCommandError, GitManagerError } from "@t3tools/contracts"; import { applyGitStatusStreamEvent } from "@t3tools/shared/git"; +import * as VcsAutomaticFetchInterval from "./VcsAutomaticFetchInterval.ts"; import * as VcsStatusBroadcaster from "./VcsStatusBroadcaster.ts"; import * as BackgroundPolicy from "../background/BackgroundPolicy.ts"; import * as GitWorkflowService from "../git/GitWorkflowService.ts"; @@ -77,6 +78,8 @@ function layerTestFor(state: { localInvalidationCalls: number; remoteInvalidationCalls: number; remoteStatusRefreshUpstreamValues?: Array; + localStatusBranchChanges?: Array; + refSnapshot?: string | null; remoteStatusError?: GitManagerError; backgroundWorkEnabled?: boolean; automaticFetchInterval?: Effect.Effect; @@ -101,15 +104,19 @@ function layerTestFor(state: { Layer.provide(layerBackgroundPolicy(() => state.backgroundWorkEnabled !== false)), Layer.provide( Layer.succeed( - VcsStatusBroadcaster.VcsAutomaticFetchInterval, + VcsAutomaticFetchInterval.VcsAutomaticFetchInterval, state.automaticFetchInterval ?? Effect.succeed(Duration.seconds(30)), ), ), Layer.provide( Layer.mock(GitWorkflowService.GitWorkflowService)({ - localStatus: () => + localStatus: (_input, options) => Effect.suspend(() => { state.localStatusCalls += 1; + const includeBranchChanges = options?.includeBranchChanges !== false; + state.localStatusBranchChanges?.push(includeBranchChanges); + const { branchChanges: _branchChanges, ...withoutBranchChanges } = + state.currentLocalStatus; if (state.failLocalStatus) { return (state.onLocalStatusFailure ?? Effect.void).pipe( Effect.andThen( @@ -124,7 +131,7 @@ function layerTestFor(state: { ); } return (state.beforeLocalStatus ?? Effect.void).pipe( - Effect.as(state.currentLocalStatus), + Effect.as(includeBranchChanges ? state.currentLocalStatus : withoutBranchChanges), ); }), repositoryRoot: () => Effect.sync(() => (state.currentLocalStatus.isRepo ? "/repo" : null)), @@ -134,6 +141,8 @@ function layerTestFor(state: { : (state.beforeCurrentBranch ?? Effect.void).pipe( Effect.andThen(Effect.sync(() => state.currentLocalStatus.refName)), ), + readRefSnapshot: () => + Effect.sync(() => (state.refSnapshot === undefined ? "refs" : state.refSnapshot)), upstreamDivergence: () => Effect.sync(() => state.currentRemoteStatus?.hasUpstream && state.currentLocalStatus.refName !== null @@ -747,6 +756,7 @@ describe("VcsStatusBroadcaster", () => { Layer.mock(GitWorkflowService.GitWorkflowService)({ localStatus: () => Effect.succeed(localStatus), currentBranch: () => Effect.succeed(localStatus.refName), + readRefSnapshot: () => Effect.succeed("refs"), remoteStatus: () => Effect.succeed(remoteStatus), invalidateLocalStatus: () => Effect.void, invalidateRemoteStatus: () => Effect.void, @@ -843,6 +853,7 @@ describe("VcsStatusBroadcaster", () => { Layer.mock(GitWorkflowService.GitWorkflowService)({ localStatus: () => Effect.succeed(baseLocalStatus), currentBranch: () => Effect.succeed(baseLocalStatus.refName), + readRefSnapshot: () => Effect.succeed("refs"), remoteStatus: () => Effect.gen(function* () { remoteReads += 1; @@ -889,6 +900,7 @@ describe("VcsStatusBroadcaster", () => { Layer.mock(GitWorkflowService.GitWorkflowService)({ localStatus: () => Effect.succeed(baseLocalStatus), currentBranch: () => Effect.succeed(baseLocalStatus.refName), + readRefSnapshot: () => Effect.succeed("refs"), remoteStatus: () => Effect.gen(function* () { remoteReads += 1; @@ -942,6 +954,7 @@ describe("VcsStatusBroadcaster", () => { return state.currentLocalStatus; }), currentBranch: () => Effect.succeed(state.currentLocalStatus.refName), + readRefSnapshot: () => Effect.succeed("refs"), remoteStatus: () => Effect.suspend(() => { state.remoteStatusCalls += 1; @@ -1068,6 +1081,49 @@ describe("VcsStatusBroadcaster", () => { }, ); + it.effect("publishes a checkout's branch before returning, ahead of the remote read", () => { + const state: Parameters[0] = { + currentLocalStatus: baseLocalStatus, + currentRemoteStatus: baseRemoteStatus, + localStatusCalls: 0, + remoteStatusCalls: 0, + localInvalidationCalls: 0, + remoteInvalidationCalls: 0, + }; + + return Effect.gen(function* () { + const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; + yield* broadcaster.getStatus({ cwd: "/repo" }); + const subscribed = yield* Deferred.make(); + const localPublished = yield* Deferred.make(); + const remotePublished = yield* Deferred.make(); + yield* broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }).pipe( + Stream.runForEach((event) => { + if (event._tag === "snapshot") return Deferred.succeed(subscribed, undefined); + if (event._tag === "localUpdated") return Deferred.succeed(localPublished, event.local); + return event.remote === null + ? Effect.void + : Deferred.succeed(remotePublished, event.remote); + }), + Effect.forkScoped, + ); + yield* Deferred.await(subscribed); + + const releaseRemote = yield* Deferred.make(); + state.beforeRemoteStatus = Deferred.await(releaseRemote); + state.currentLocalStatus = { ...baseLocalStatus, refName: "main" }; + state.currentRemoteStatus = remoteStatusWithPr; + yield* broadcaster.refreshAfterCheckout("/repo"); + + assert.equal((yield* Deferred.await(localPublished)).refName, "main"); + assert.isFalse(yield* Deferred.isDone(remotePublished)); + + yield* Deferred.succeed(releaseRemote, undefined); + assert.deepStrictEqual(yield* Deferred.await(remotePublished), remoteStatusWithPr); + assert.equal((yield* broadcaster.getStatus({ cwd: "/repo" })).pr?.number, 2978); + }).pipe(Effect.provide(layerTestFor(state)), Effect.scoped); + }); + it.effect("a remote poll refreshes Changes totals when divergence counts stay the same", () => { const state: Parameters[0] = { currentLocalStatus: { @@ -1107,6 +1163,116 @@ describe("VcsStatusBroadcaster", () => { }).pipe(Effect.provide(layerTestFor(state))); }); + it.effect("reads Changes totals only while a subscriber shows them", () => { + const branchChanges = { baseRef: "origin/main", insertions: 3, deletions: 1 }; + const state: Parameters[0] = { + currentLocalStatus: { ...baseLocalStatus, branchChanges }, + currentRemoteStatus: baseRemoteStatus, + localStatusCalls: 0, + remoteStatusCalls: 0, + localInvalidationCalls: 0, + remoteInvalidationCalls: 0, + localStatusBranchChanges: [], + }; + + return Effect.gen(function* () { + const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; + const paletteEvents = yield* Queue.unbounded(); + yield* broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }).pipe( + Stream.runForEach((event) => Queue.offer(paletteEvents, event)), + Effect.forkScoped, + ); + const paletteSnapshot = yield* Queue.take(paletteEvents); + assert.strictEqual(paletteSnapshot._tag, "snapshot"); + if (paletteSnapshot._tag === "snapshot") { + assert.isUndefined(paletteSnapshot.local.branchChanges); + } + yield* broadcaster.refreshLocalStatus("/repo"); + assert.deepStrictEqual(state.localStatusBranchChanges, [false, false]); + + const headerScope = yield* Scope.make(); + const headerEvents = yield* Queue.unbounded(); + yield* broadcaster.streamStatus({ cwd: "/repo" }).pipe( + Stream.runForEach((event) => Queue.offer(headerEvents, event)), + Effect.forkIn(headerScope), + ); + const headerSnapshot = yield* Queue.take(headerEvents); + assert.strictEqual(headerSnapshot._tag, "snapshot"); + if (headerSnapshot._tag === "snapshot") { + assert.deepStrictEqual(headerSnapshot.local.branchChanges, branchChanges); + } + const paletteUpdate = yield* Queue.take(paletteEvents); + assert.strictEqual(paletteUpdate._tag, "localUpdated"); + if (paletteUpdate._tag === "localUpdated") { + assert.deepStrictEqual(paletteUpdate.local.branchChanges, branchChanges); + } + // The header's first remote poll also rereads local status. + while ((yield* Queue.take(headerEvents))._tag !== "remoteUpdated") {} + const turnEnd = yield* broadcaster.refreshLocalStatus("/repo"); + assert.deepStrictEqual(turnEnd.branchChanges, branchChanges); + + yield* Scope.close(headerScope, Exit.void); + yield* broadcaster.refreshLocalStatus("/repo"); + assert.deepStrictEqual(state.localStatusBranchChanges, [ + false, + false, + true, + true, + true, + false, + ]); + }).pipe(Effect.provide(layerTestFor(state)), Effect.scoped); + }); + + it.effect("a light refresh that a header joins mid-read publishes Changes totals", () => { + const branchChanges = { baseRef: "origin/main", insertions: 3, deletions: 1 }; + const state: Parameters[0] = { + currentLocalStatus: { ...baseLocalStatus, branchChanges }, + currentRemoteStatus: baseRemoteStatus, + localStatusCalls: 0, + remoteStatusCalls: 0, + localInvalidationCalls: 0, + remoteInvalidationCalls: 0, + localStatusBranchChanges: [], + fileSystem: FileSystem.makeNoop({ realPath: (cwd) => Effect.succeed(cwd) }), + }; + + return Effect.gen(function* () { + const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; + const subscribeHeader = Effect.fn("subscribeHeader")(function* (scope: Scope.Scope) { + const events = yield* Queue.unbounded(); + yield* broadcaster.streamStatus({ cwd: "/repo" }).pipe( + Stream.runForEach((event) => Queue.offer(events, event)), + Effect.forkIn(scope, { startImmediately: true }), + ); + return events; + }); + const firstHeader = yield* Scope.make(); + const firstEvents = yield* subscribeHeader(firstHeader); + while ((yield* Queue.take(firstEvents))._tag !== "remoteUpdated") {} + yield* Scope.close(firstHeader, Exit.void); + + const readStarted = yield* Deferred.make(); + const finishRead = yield* Deferred.make(); + state.beforeLocalStatus = Deferred.succeed(readStarted, undefined).pipe( + Effect.andThen(Deferred.await(finishRead)), + ); + const turnEnd = yield* broadcaster.refreshLocalStatus("/repo").pipe(Effect.forkScoped); + yield* Deferred.await(readStarted); + // Starting immediately registers the header's demand before it waits behind the read. + const secondEvents = yield* subscribeHeader(yield* Scope.make()); + yield* Deferred.succeed(finishRead, undefined); + const snapshot = yield* Queue.take(secondEvents); + assert.strictEqual(snapshot._tag, "snapshot"); + if (snapshot._tag === "snapshot") { + assert.deepStrictEqual(snapshot.local.branchChanges, branchChanges); + } + + assert.deepStrictEqual((yield* Fiber.join(turnEnd)).branchChanges, branchChanges); + assert.deepStrictEqual(state.localStatusBranchChanges?.slice(-2), [false, true]); + }).pipe(Effect.provide(layerTestFor(state)), Effect.scoped); + }); + it.effect("an explicit refresh reads local totals after the fetch", () => { return Effect.gen(function* () { const fetchStarted = yield* Deferred.make(); @@ -1116,6 +1282,7 @@ describe("VcsStatusBroadcaster", () => { branchChanges: { baseRef: "origin/main", insertions: 745, deletions: 24 }, }; let cachedLocal: VcsStatusLocalResult | undefined; + let refSnapshot = "before"; const readLocalStatus = () => Effect.sync(() => (cachedLocal ??= local)); const layerTest = VcsStatusBroadcaster.layer.pipe( Layer.provideMerge(NodeServices.layer), @@ -1133,6 +1300,7 @@ describe("VcsStatusBroadcaster", () => { }), currentBranch: () => Effect.succeed(baseLocalStatus.refName), invalidateRemoteStatus: () => Effect.void, + readRefSnapshot: () => Effect.sync(() => refSnapshot), remoteStatus: () => Deferred.succeed(fetchStarted, undefined).pipe( Effect.andThen(Deferred.await(finishFetch)), @@ -1151,6 +1319,7 @@ describe("VcsStatusBroadcaster", () => { ...baseLocalStatus, branchChanges: { baseRef: "origin/main", insertions: 0, deletions: 0 }, }; + refSnapshot = "after"; yield* Deferred.succeed(finishFetch, undefined); const status = yield* Fiber.join(refresh); assert.deepStrictEqual(status.branchChanges, local.branchChanges); @@ -1158,6 +1327,59 @@ describe("VcsStatusBroadcaster", () => { }); }); + it.effect("an explicit refresh re-reads local totals only after refs moved", () => { + const totals = (insertions: number): VcsStatusLocalResult => ({ + ...baseLocalStatus, + branchChanges: { baseRef: "origin/main", insertions, deletions: 0 }, + }); + const state: Parameters[0] = { + currentLocalStatus: totals(745), + currentRemoteStatus: remoteStatusWithPr, + localStatusCalls: 0, + remoteStatusCalls: 0, + localInvalidationCalls: 0, + remoteInvalidationCalls: 0, + }; + return Effect.gen(function* () { + const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster; + const subscribed = yield* Deferred.make(); + const events = yield* broadcaster.streamStatus({ cwd: "/repo", includeRemote: false }).pipe( + Stream.tap((event) => + event._tag === "snapshot" ? Deferred.succeed(subscribed, undefined) : Effect.void, + ), + Stream.filter((event) => event._tag !== "snapshot"), + Stream.take(4), + Stream.runCollect, + Effect.forkScoped, + ); + yield* Deferred.await(subscribed); + + state.localStatusCalls = 0; + yield* broadcaster.refreshStatus("/repo"); + assert.equal(state.localStatusCalls, 1); + + state.localStatusCalls = 0; + state.currentRemoteStatus = baseRemoteStatus; + state.beforeRemoteStatus = Effect.sync(() => { + state.currentLocalStatus = totals(0); + state.refSnapshot = "moved"; + }); + const moved = yield* broadcaster.refreshStatus("/repo"); + assert.equal(state.localStatusCalls, 2); + assert.deepStrictEqual(moved.branchChanges, totals(0).branchChanges); + assert.deepStrictEqual( + [...(yield* Fiber.join(events))].map((event) => event._tag), + // The first refresh adds the totals this palette-style subscription skipped. + ["localUpdated", "remoteUpdated", "remoteUpdated", "localUpdated"], + ); + + state.localStatusCalls = 0; + state.refSnapshot = null; + yield* broadcaster.refreshStatus("/repo"); + assert.equal(state.localStatusCalls, 2); + }).pipe(Effect.provide(layerTestFor(state)), Effect.scoped); + }); + it.effect("publishes local status while an earlier lookup holds the remote lock", () => { const state: Parameters[0] = { currentLocalStatus: baseLocalStatus, @@ -1991,8 +2213,10 @@ it.effect("serializes branch reads with detailed refresh publication", () => { yield* Fiber.join(older); yield* Fiber.join(newer); state.branchRead = () => Effect.succeed("new"); - const result = yield* b.getStatus({ cwd: "/repo" }); - assert.equal(result.refName, "new"); + const result = yield* b + .streamStatus({ cwd: "/repo", includeRemote: false }) + .pipe(Stream.runHead, Effect.flatMap(Effect.fromOption)); + assert.equal(result._tag === "snapshot" && result.local.refName, "new"); assert.equal(state.localStatusCalls, 1); }).pipe(Effect.provide(layerTestFor(state))); }); diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.ts b/apps/server/src/vcs/VcsStatusBroadcaster.ts index 06cc4aff7dfa2..b691b9d2978a2 100644 --- a/apps/server/src/vcs/VcsStatusBroadcaster.ts +++ b/apps/server/src/vcs/VcsStatusBroadcaster.ts @@ -25,7 +25,6 @@ import type { VcsStatusSubscriptionInput, } from "@t3tools/contracts"; import { DEFAULT_AUTOMATIC_GIT_FETCH_INTERVAL } from "@t3tools/contracts"; -import { resolveServerBackgroundActivitySettings } from "@t3tools/shared/backgroundActivitySettings"; import { mergeGitStatusParts } from "@t3tools/shared/git"; import * as KeyedLock from "@t3tools/shared/KeyedLock"; import { resolveProjectSettings } from "@t3tools/shared/projectSettings"; @@ -34,6 +33,7 @@ import * as BackgroundPolicy from "../background/BackgroundPolicy.ts"; import * as GitWorkflowService from "../git/GitWorkflowService.ts"; import * as ProjectStore from "../orchestration-v2/ProjectStore.ts"; import * as ServerSettings from "../serverSettings.ts"; +import { VcsAutomaticFetchInterval } from "./VcsAutomaticFetchInterval.ts"; const VCS_STATUS_REFRESH_FAILURE_BASE_DELAY = Duration.seconds(30); const VCS_STATUS_REFRESH_FAILURE_MAX_DELAY = Duration.minutes(15); @@ -143,8 +143,12 @@ interface CachedRemoteStatus extends CachedValue { readonly branch: string | null; } +interface CachedLocalStatus extends CachedValue { + readonly includesBranchChanges: boolean; +} + interface CachedVcsStatus { - readonly local: CachedValue | null; + readonly local: CachedLocalStatus | null; readonly remote: CachedRemoteStatus | null; } @@ -160,24 +164,6 @@ interface ActiveRemotePoller { readonly demandCwds: Ref.Ref>; } -export class VcsAutomaticFetchInterval extends Context.Reference>( - "t3/vcs/VcsAutomaticFetchInterval", - { defaultValue: () => Effect.succeed(DEFAULT_AUTOMATIC_GIT_FETCH_INTERVAL) }, -) {} - -export const layerAutomaticFetchInterval = Layer.effect( - VcsAutomaticFetchInterval, - Effect.gen(function* () { - const serverSettings = yield* ServerSettings.ServerSettingsService; - return serverSettings.getSettings.pipe( - Effect.map( - (settings) => resolveServerBackgroundActivitySettings(settings).automaticGitFetchInterval, - ), - Effect.orElseSucceed(() => DEFAULT_AUTOMATIC_GIT_FETCH_INTERVAL), - ); - }), -); - export class VcsAutoPullPolicy extends Context.Reference<{ readonly isEnabled: (cwd: string) => Effect.Effect; }>("t3/vcs/VcsAutoPullPolicy", { @@ -236,6 +222,11 @@ export class VcsStatusBroadcaster extends Context.Service< readonly refreshLocalStatus: ( cwd: string, ) => Effect.Effect; + /** + * For a checkout someone is waiting on. Returns once the new branch is published, so the + * caller's reply does not outrun it, and finishes the slower remote read in the background. + */ + readonly refreshAfterCheckout: (cwd: string) => Effect.Effect; readonly refreshStatus: ( cwd: string, options?: { readonly localOnly?: boolean }, @@ -328,14 +319,51 @@ export const make = Effect.gen(function* () { return yield* Ref.get(cacheRef).pipe(Effect.map((cache) => cache.get(cwd) ?? null)); }); + // Only full subscribers and direct reads show Changes totals. Command palette rows subscribe + // without remote status and skip them, which halves the git processes a burst of rows spawns. + const branchChangesDemand = new Map(); + const wantsBranchChanges = (cwd: string) => branchChangesDemand.has(cwd); + const acquireBranchChangesDemand = (cwd: string) => + Effect.acquireRelease( + Effect.sync(() => { + branchChangesDemand.set(cwd, (branchChangesDemand.get(cwd) ?? 0) + 1); + }), + () => + Effect.sync(() => { + const count = (branchChangesDemand.get(cwd) ?? 1) - 1; + if (count > 0) branchChangesDemand.set(cwd, count); + else branchChangesDemand.delete(cwd); + }), + ); + + const readLocalStatus = Effect.fn("VcsStatusBroadcaster.readLocalStatus")(function* ( + cwd: string, + ) { + const includeBranchChanges = wantsBranchChanges(cwd); + const local = yield* workflow.localStatus({ cwd }, { includeBranchChanges }); + // A subscriber that shows totals arrived during the read and must not receive it without them. + if (!includeBranchChanges && wantsBranchChanges(cwd)) { + return { + local: yield* workflow.localStatus({ cwd }, { includeBranchChanges: true }), + includesBranchChanges: true, + }; + } + return { local, includesBranchChanges: includeBranchChanges }; + }); + const updateCachedLocalStatus = Effect.fn("VcsStatusBroadcaster.updateCachedLocalStatus")( function* ( cwd: string, - local: VcsStatusLocalResult, + read: { readonly local: VcsStatusLocalResult; readonly includesBranchChanges: boolean }, observation: number, options?: { publish?: boolean }, ) { - const nextLocal = { fingerprint: fingerprintStatusPart(local), value: local }; + const { local, includesBranchChanges } = read; + const nextLocal = { + fingerprint: fingerprintStatusPart(local), + value: local, + includesBranchChanges, + }; const [localChanged, remoteCleared] = yield* Ref.modify(cacheRef, (cache) => { const previous = cache.get(cwd) ?? { local: null, remote: null }; const remote = previous.remote?.branch === local.refName ? previous.remote : null; @@ -386,16 +414,17 @@ export const make = Effect.gen(function* () { options?: { publish?: boolean }, ) { const observation = ++branchObservation; - const local = yield* workflow.localStatus({ cwd }); - return yield* updateCachedLocalStatus(cwd, local, observation, options); + return yield* updateCachedLocalStatus(cwd, yield* readLocalStatus(cwd), observation, options); }); const getOrLoadLocalStatusUnlocked = Effect.fn("VcsStatusBroadcaster.getOrLoadLocalStatus")( function* (cwd: string) { const cached = yield* getCachedStatus(cwd); if (cached?.local) { - const branch = yield* workflow.currentBranch(cwd); - if (branch === cached.local.value.refName) return cached.local.value; + if (cached.local.includesBranchChanges || !wantsBranchChanges(cwd)) { + const branch = yield* workflow.currentBranch(cwd); + if (branch === cached.local.value.refName) return cached.local.value; + } yield* workflow.invalidateLocalStatus(cwd); return yield* loadLocalStatus(cwd, { publish: true }); } @@ -420,6 +449,7 @@ export const make = Effect.gen(function* () { "VcsStatusBroadcaster.getStatus", )(function* (input) { const cwd = yield* withFileSystem(normalizeCwd(input.cwd)); + yield* acquireBranchChangesDemand(cwd); yield* getOrLoadLocalStatus(cwd); const cached = yield* getCachedStatus(cwd); if (cached?.local && cached.remote) { @@ -436,14 +466,18 @@ export const make = Effect.gen(function* () { return mergeGitStatusParts(parts.local, parts.remote); }), ); - }); + }, Effect.scoped); // A refresh that started after a request saw every change that request could mean, so queued // callers share it instead of each running git status again. const localRefreshRequests = new Map(); const localRefreshResults = new Map< string, - { readonly startedAt: number; readonly local: VcsStatusLocalResult } + { + readonly startedAt: number; + readonly local: VcsStatusLocalResult; + readonly includesBranchChanges: boolean; + } >(); const refreshLocalStatusCore = Effect.fn("VcsStatusBroadcaster.refreshLocalStatusCore")( function* (cwd: string) { @@ -453,13 +487,19 @@ export const make = Effect.gen(function* () { cwd, Effect.gen(function* () { const shared = localRefreshResults.get(cwd); - if (shared !== undefined && shared.startedAt >= requestedAt) return shared.local; + if ( + shared !== undefined && + shared.startedAt >= requestedAt && + (shared.includesBranchChanges || !wantsBranchChanges(cwd)) + ) { + return shared.local; + } const startedAt = localRefreshRequests.get(cwd) ?? requestedAt; yield* workflow.invalidateLocalStatus(cwd); const observation = ++branchObservation; - const local = yield* workflow.localStatus({ cwd }); - localRefreshResults.set(cwd, { startedAt, local }); - return yield* updateCachedLocalStatus(cwd, local, observation, { publish: true }); + const read = yield* readLocalStatus(cwd); + localRefreshResults.set(cwd, { startedAt, ...read }); + return yield* updateCachedLocalStatus(cwd, read, observation, { publish: true }); }), ); }, @@ -603,33 +643,63 @@ export const make = Effect.gen(function* () { ); }); + const refreshRemoteAndLocalTotals = Effect.fn("VcsStatusBroadcaster.refreshRemoteAndLocalTotals")( + function* (cwd: string, policyCwd: string, refsBeforeLocalRead: string | null) { + return yield* withRemoteWriteLock( + cwd, + Effect.gen(function* () { + const refreshUpstream = !Duration.isZero(yield* automaticFetchInterval); + // Bump the PR lookup epoch after earlier remote writers have finished. + yield* workflow.invalidateStatus(cwd); + const remoteRead = yield* readRemoteStatus(cwd, { refreshUpstream }); + // Without a fetch, the cached behind count cannot justify an automatic pull. + const pulled = refreshUpstream + ? yield* maybeAutoPull(cwd, remoteRead, [policyCwd]) + : null; + if (pulled !== null) return mergeGitStatusParts(pulled.local, pulled.remote); + yield* updateCachedRemoteStatus(cwd, remoteRead, { publish: true }); + // A fetch, ours or not, can move the base used for local Changes totals. + if ( + refsBeforeLocalRead === null || + (yield* workflow.readRefSnapshot(cwd)) !== refsBeforeLocalRead + ) { + yield* refreshLocalStatusCore(cwd); + } + const parts = yield* getStatusParts(cwd); + return mergeGitStatusParts(parts.local, parts.remote); + }), + ); + }, + ); + const refreshStatus: VcsStatusBroadcaster["Service"]["refreshStatus"] = Effect.fn( "VcsStatusBroadcaster.refreshStatus", )(function* (rawCwd, options) { const cwd = yield* withFileSystem(normalizeCwd(rawCwd)); + yield* acquireBranchChangesDemand(cwd); + const refsBeforeLocalRead = options?.localOnly ? null : yield* workflow.readRefSnapshot(cwd); yield* refreshLocalStatusCore(cwd); if (options?.localOnly) { // Never cache a remote here: readers take `value: null` as "read and found no PR". const parts = yield* getStatusParts(cwd); return mergeGitStatusParts(parts.local, parts.remote); } - return yield* withRemoteWriteLock( - cwd, - Effect.gen(function* () { - const refreshUpstream = !Duration.isZero(yield* automaticFetchInterval); - // Bump the PR lookup epoch after earlier remote writers have finished. - yield* workflow.invalidateStatus(cwd); - const remoteRead = yield* readRemoteStatus(cwd, { refreshUpstream }); - // Fetch can move the base used for local Changes totals. - yield* refreshLocalStatusCore(cwd); - // Without a fetch, the cached behind count cannot justify an automatic pull. - const pulled = refreshUpstream ? yield* maybeAutoPull(cwd, remoteRead, [rawCwd]) : null; - if (pulled !== null) return mergeGitStatusParts(pulled.local, pulled.remote); - yield* updateCachedRemoteStatus(cwd, remoteRead, { publish: true }); - const parts = yield* getStatusParts(cwd); - return mergeGitStatusParts(parts.local, parts.remote); - }), + return yield* refreshRemoteAndLocalTotals(cwd, rawCwd, refsBeforeLocalRead); + }, Effect.scoped); + + const refreshAfterCheckout: VcsStatusBroadcaster["Service"]["refreshAfterCheckout"] = Effect.fn( + "VcsStatusBroadcaster.refreshAfterCheckout", + )(function* (rawCwd) { + const cwd = yield* withFileSystem(normalizeCwd(rawCwd)); + const localDone = yield* Deferred.make(); + // Owned by the broadcaster so a client disconnecting mid-switch still leaves status correct. + yield* refreshLocalStatusCore(cwd).pipe( + Effect.ensuring(Deferred.succeed(localDone, undefined)), + Effect.andThen(refreshRemoteAndLocalTotals(cwd, rawCwd, null)), + Effect.ignoreCause({ log: true }), + Effect.forkIn(broadcasterScope), ); + yield* Deferred.await(localDone); }); const refreshPullRequestStatus: VcsStatusBroadcaster["Service"]["refreshPullRequestStatus"] = @@ -1039,6 +1109,7 @@ export const make = Effect.gen(function* () { else detailedSubscribers.set(cwd, remaining); }), ); + if (input.includeRemote !== false) yield* acquireBranchChangesDemand(cwd); const subscription = yield* PubSub.subscribe(changesPubSub); // Watch before reading so a checkout in between still publishes. yield* Effect.acquireRelease(retainHeadWatcher(cwd), () => releaseHeadWatcher(cwd)); @@ -1122,6 +1193,7 @@ export const make = Effect.gen(function* () { ), getStatus, refreshLocalStatus, + refreshAfterCheckout, refreshStatus: shareQueuedRefreshStatus, refreshPullRequestStatus, streamStatus, diff --git a/apps/server/src/vcs/worktreeCheckoutCache.ts b/apps/server/src/vcs/worktreeCheckoutCache.ts index a72d6d859fc54..8a51189fe6d69 100644 --- a/apps/server/src/vcs/worktreeCheckoutCache.ts +++ b/apps/server/src/vcs/worktreeCheckoutCache.ts @@ -511,6 +511,7 @@ export const makeWorktreeCheckoutCache = Effect.fn("WorktreeCheckoutCache.make") readonly worktreePath: string; readonly refName: string; readonly checkoutWorkers: string; + readonly lockReason?: string | undefined; }) { if (!(yield* gitSupported)) return false; const worktreePath = path.resolve(input.cwd, input.worktreePath); @@ -613,13 +614,14 @@ export const makeWorktreeCheckoutCache = Effect.fn("WorktreeCheckoutCache.make") if (moved === null || (yield* fileSystem.exists(target.indexHook))) return null; return target; }); - const rollback = git("rollback", input.cwd, [ - "worktree", - "remove", - "--force", - "--", - worktreePath, - ]).pipe( + const rollback = ( + input.lockReason + ? git("unlockRollback", input.cwd, ["worktree", "unlock", "--", worktreePath]) + : Effect.void + ).pipe( + Effect.andThen( + git("rollback", input.cwd, ["worktree", "remove", "--force", "--", worktreePath]), + ), Effect.flatMap((removed) => removed === null ? Effect.logWarning("could not remove a partially created worktree", { worktreePath }) @@ -633,6 +635,7 @@ export const makeWorktreeCheckoutCache = Effect.fn("WorktreeCheckoutCache.make") "worktree", "add", "--no-checkout", + ...(input.lockReason ? ["--lock", "--reason", input.lockReason] : []), "--", worktreePath, input.refName, diff --git a/apps/server/src/vcs/worktreeRemoval.ts b/apps/server/src/vcs/worktreeRemoval.ts index f1140cdcc3a48..b48994873b282 100644 --- a/apps/server/src/vcs/worktreeRemoval.ts +++ b/apps/server/src/vcs/worktreeRemoval.ts @@ -9,6 +9,42 @@ import type * as GitVcsDriver from "./GitVcsDriver.ts"; const INDEX_LIST_MAX_BYTES = 64 * 1024 * 1024; +/** + * git-annex replaces a linked worktree's `.git` file with a symlink to its admin + * directory, so its symlinks into `.git/annex` resolve. Git then refuses to remove it. + */ +const annexedAdminDir = Effect.fn("WorktreeRemoval.annexedAdminDir")( + function* (worktreePath: string) { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const adminDir = path.resolve( + worktreePath, + yield* fileSystem.readLink(path.join(worktreePath, ".git")), + ); + const pointsBackTo = path.dirname( + path.resolve( + adminDir, + (yield* fileSystem.readFileString(path.join(adminDir, "gitdir"))).trim(), + ), + ); + return (yield* fileSystem.realPath(pointsBackTo)) === (yield* fileSystem.realPath(worktreePath)) + ? Option.some(adminDir) + : Option.none(); + }, + Effect.orElseSucceed(() => Option.none()), +); + +export const isLinkedWorktree = Effect.fn("WorktreeRemoval.isLinkedWorktree")(function* ( + worktreePath: string, +) { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + return ( + (yield* fileSystem.stat(path.join(worktreePath, ".git"))).type === "File" || + Option.isSome(yield* annexedAdminDir(worktreePath)) + ); +}); + /** * Git for Windows before 2.54 follows NTFS junctions; newer Git leaves them behind. * On Windows, mirror Git's removal checks, then delete with Node's `rm`, which @@ -33,6 +69,39 @@ export const removeLinkedWorktree = Effect.fn("WorktreeRemoval.removeLinkedWorkt // Git refuses to remove a worktree with untracked files unless forced, but // its check honors `status.showUntrackedFiles=no` and would delete them. const statusConfig = ["-c", "status.showUntrackedFiles=normal"]; + // Git's own refusals, worded the way it prints them. + const refuse = (message: string, cause?: unknown) => + new GitCommandError({ + operation: input.operation, + command: "git worktree remove", + cwd: input.cwd, + detail: `fatal: ${message}`, + ...(cause === undefined ? {} : { cause }), + }); + const annexed = yield* annexedAdminDir(worktreePath); + if (Option.isSome(annexed)) { + const adminDir = annexed.value; + const writeGitFile = fileSystem.writeFileString(gitFile, `gitdir: ${adminDir}\n`); + // A kept worktree's annexed files resolve only through the symlink; a gitdir + // file is the fallback when it cannot be recreated. + yield* Effect.addFinalizer(() => + fileSystem.exists(worktreePath).pipe( + Effect.flatMap((kept) => + kept + ? fileSystem.remove(gitFile, { force: true }).pipe( + Effect.andThen(fileSystem.symlink(adminDir, gitFile)), + Effect.catch(() => writeGitFile), + ) + : Effect.void, + ), + Effect.ignore, + ), + ); + yield* fileSystem.remove(gitFile).pipe( + Effect.andThen(writeGitFile), + Effect.mapError((cause) => refuse(`failed to replace '${gitFile}'`, cause)), + ); + } const linked = (yield* HostProcess.isWindows) && (yield* fileSystem.stat(gitFile).pipe( @@ -50,15 +119,6 @@ export const removeLinkedWorktree = Effect.fn("WorktreeRemoval.removeLinkedWorkt ]); } - // Git's own refusals, worded the way it prints them. - const refuse = (message: string, cause?: unknown) => - new GitCommandError({ - operation: input.operation, - command: "git worktree remove", - cwd: input.cwd, - detail: `fatal: ${message}`, - ...(cause === undefined ? {} : { cause }), - }); const gitOutput = (cwd: string, args: ReadonlyArray, env?: NodeJS.ProcessEnv) => input .execute({ @@ -153,4 +213,4 @@ export const removeLinkedWorktree = Effect.fn("WorktreeRemoval.removeLinkedWorkt ); // The folder is gone, so Git only drops the worktree's metadata. return yield* input.run(["worktree", "remove", "--force", "--force", "--", worktreePath]); -}); +}, Effect.scoped); diff --git a/apps/server/src/workspace/WorkspaceEntries.test.ts b/apps/server/src/workspace/WorkspaceEntries.test.ts index 1c50b94a7cbcc..92d8bc882c951 100644 --- a/apps/server/src/workspace/WorkspaceEntries.test.ts +++ b/apps/server/src/workspace/WorkspaceEntries.test.ts @@ -1014,6 +1014,68 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => { expect({ finders, rescans }).toEqual({ finders: 1, rescans: 1 }); }), ); + + it.effect("serves content search once a first scan that outlasted the wait finishes", () => + Effect.gen(function* () { + const cwd = yield* makeTempDir({ prefix: "t3code-workspace-content-slow-scan-" }); + yield* writeTextFile(cwd, "src/notes.ts", "const needle = 1;\n"); + // Real finders; only the initial wait and the progress it reads report a scan still running. + let scanning = true; + let finders = 0; + let settle = (): Promise => Promise.resolve(); + const createFinder = FileFinder.create.bind(FileFinder); + vi.spyOn(FileFinder, "create").mockImplementation((options) => { + const created = createFinder(options); + if (!created.ok) return created; + const finder = created.value; + finders += 1; + const waitForIndexReady = finder.waitForIndexReady.bind(finder); + settle = () => waitForIndexReady(15_000); + finder.waitForIndexReady = (timeoutMs) => + scanning ? Promise.resolve({ ok: true, value: false }) : waitForIndexReady(timeoutMs); + const getScanProgress = finder.getScanProgressAsync.bind(finder); + finder.getScanProgressAsync = async () => { + const progress = await getScanProgress(); + return scanning && progress.ok + ? { + ok: true, + value: { ...progress.value, isScanning: true, isWarmupComplete: false }, + } + : progress; + }; + return created; + }); + const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; + const searchNeedle = workspaceEntries + .searchContents({ + cwd, + query: "needle", + limit: 100, + caseSensitive: true, + wholeWord: false, + useRegex: false, + }) + .pipe( + Effect.map((result): unknown => result.matches.map((match) => match.path)), + Effect.catchTags({ + WorkspaceSearchIndexScanTimedOut: (error) => Effect.succeed(error._tag), + }), + ); + + const whileScanning = yield* Effect.all([searchNeedle, searchNeedle], { + concurrency: "unbounded", + }); + scanning = false; + yield* Effect.promise(() => settle()); + const afterScan = yield* searchNeedle; + + expect({ whileScanning, afterScan, finders }).toEqual({ + whileScanning: ["WorkspaceSearchIndexScanTimedOut", "WorkspaceSearchIndexScanTimedOut"], + afterScan: ["src/notes.ts"], + finders: 1, + }); + }), + ); }); describe("browse", () => { diff --git a/apps/server/src/workspace/WorkspaceEntries.ts b/apps/server/src/workspace/WorkspaceEntries.ts index 51b1720126536..ad1d2c2ae0ebe 100644 --- a/apps/server/src/workspace/WorkspaceEntries.ts +++ b/apps/server/src/workspace/WorkspaceEntries.ts @@ -179,7 +179,6 @@ export const make = Effect.gen(function* () { function* ( cause: | WorkspaceSearchIndex.WorkspaceSearchIndexCreateFailed - | WorkspaceSearchIndex.WorkspaceSearchIndexScanTimedOut | WorkspaceSearchIndex.WorkspaceSearchIndexRefreshFailed, ) { yield* Effect.logWarning("Failed to refresh workspace search index", { @@ -197,7 +196,6 @@ export const make = Effect.gen(function* () { Effect.provide(workspaceSearchIndexes.get(indexKey)), Effect.catchTags({ WorkspaceSearchIndexCreateFailed: recoverRefreshFailure, - WorkspaceSearchIndexScanTimedOut: recoverRefreshFailure, WorkspaceSearchIndexRefreshFailed: recoverRefreshFailure, }), ); diff --git a/apps/server/src/workspace/WorkspaceFileSystem.test.ts b/apps/server/src/workspace/WorkspaceFileSystem.test.ts index 34ddeaa872ad4..bc22117ba74ab 100644 --- a/apps/server/src/workspace/WorkspaceFileSystem.test.ts +++ b/apps/server/src/workspace/WorkspaceFileSystem.test.ts @@ -320,25 +320,27 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (i }), ); - it.effect("reads ~/ paths from the server home outside the workspace", () => - Effect.gen(function* () { - const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; - const cwd = yield* makeTempDir; - const home = yield* makeTempDir; - yield* writeTextFile(home, ".claude/CLAUDE.md", "# Notes\n"); - yield* writeTextFile(cwd, "~/.claude/CLAUDE.md", "Workspace decoy\n"); + it.effect.each(["~/.claude/CLAUDE.md", "~\\.claude/CLAUDE.md"])( + "reads %s from the server home outside the workspace", + (relativePath) => + Effect.gen(function* () { + const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem; + const cwd = yield* makeTempDir; + const home = yield* makeTempDir; + yield* writeTextFile(home, ".claude/CLAUDE.md", "# Notes\n"); + yield* writeTextFile(cwd, "~/.claude/CLAUDE.md", "Workspace decoy\n"); - const result = yield* workspaceFileSystem - .readFile({ cwd, relativePath: "~/.claude/CLAUDE.md" }) - .pipe(Effect.provideService(HostProcess.HomeDirectory, home)); + const result = yield* workspaceFileSystem + .readFile({ cwd, relativePath }) + .pipe(Effect.provideService(HostProcess.HomeDirectory, home)); - expect(result).toEqual({ - relativePath: "~/.claude/CLAUDE.md", - contents: "# Notes\n", - byteLength: 8, - truncated: false, - }); - }), + expect(result).toEqual({ + relativePath, + contents: "# Notes\n", + byteLength: 8, + truncated: false, + }); + }), ); it.effect("does not read server state files", () => diff --git a/apps/server/src/workspace/WorkspaceFileSystem.ts b/apps/server/src/workspace/WorkspaceFileSystem.ts index 7f3fb67b15000..494472e4ce68d 100644 --- a/apps/server/src/workspace/WorkspaceFileSystem.ts +++ b/apps/server/src/workspace/WorkspaceFileSystem.ts @@ -260,9 +260,10 @@ export const make = Effect.gen(function* () { input: ProjectReadFileInput, ) { const requestedPath = input.relativePath; - const hostPath = requestedPath.startsWith("~/") - ? expandHomePath(requestedPath, yield* HostProcess.HomeDirectory) - : yield* toHostPath(requestedPath); + const hostPath = + requestedPath.startsWith("~/") || requestedPath.startsWith("~\\") + ? expandHomePath(requestedPath, yield* HostProcess.HomeDirectory) + : yield* toHostPath(requestedPath); if ( isNetworkPath(hostPath) && !KnownWorkspaces.isWithinRoot(path, path.resolve(hostPath), path.resolve(input.cwd)) diff --git a/apps/server/src/workspace/WorkspaceSearchIndex.test.ts b/apps/server/src/workspace/WorkspaceSearchIndex.test.ts index 45102e4c1cfea..6c4e873f12b47 100644 --- a/apps/server/src/workspace/WorkspaceSearchIndex.test.ts +++ b/apps/server/src/workspace/WorkspaceSearchIndex.test.ts @@ -9,6 +9,7 @@ import { import * as NodeServices from "@effect/platform-node/NodeServices"; import { afterEach, expect, it } from "@effect/vitest"; import * as HostProcess from "@t3tools/shared/HostProcess"; +import { PROJECT_FAVICON_EXTENSIONS, isProjectFaviconPath } from "@t3tools/shared/projectFavicon"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; import * as Fiber from "effect/Fiber"; @@ -40,44 +41,98 @@ function fileItem(relativePath: string): FileItem { }; } -it.effect("filters image searches before applying the result limit", () => - Effect.scoped( - Effect.gen(function* () { - const items = [ - ...Array.from({ length: 200 }, (_, index) => fileItem(`src/file-${index}.ts`)), - fileItem("public/icon.svg"), - ]; - const fileSearch = vi.fn(async () => ({ - ok: true as const, - value: { - items, - scores: [], - totalMatched: items.length, - totalFiles: items.length, - }, - })); - const finder = { - destroy: vi.fn(), - waitForIndexReady: vi.fn(async () => ({ ok: true as const, value: true })), - getScanProgressAsync: async () => ({ - ok: true as const, - value: { isScanning: false }, - }), - watch: () => ({ ok: true as const, value: () => undefined }), - fileSearchAsync: fileSearch, - } as unknown as FileFinder; - vi.spyOn(FileFinder, "create").mockReturnValueOnce({ ok: true, value: finder }); - - const searchIndex = yield* WorkspaceSearchIndex.make("/workspace/project"); - const resultWithoutKind = yield* searchIndex.search("", 200, undefined, true); - const resultWithDirectoryKind = yield* searchIndex.search("", 200, "directory", true); - - expect(resultWithoutKind.entries).toEqual([{ kind: "file", path: "public/icon.svg" }]); - expect(resultWithDirectoryKind.entries).toEqual([{ kind: "file", path: "public/icon.svg" }]); - expect(fileSearch).toHaveBeenCalledTimes(2); - expect(fileSearch).toHaveBeenCalledWith("", { pageSize: 25_002 }); - }), - ).pipe(Effect.provide(testLayer)), +it.live("image searches match the image subset of a file search without fetching every file", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const vcsProcess = yield* VcsProcess.VcsProcess; + const writeFiles = Effect.fn(function* (root: string, relativePaths: ReadonlyArray) { + for (const relativePath of relativePaths) { + yield* fileSystem.makeDirectory(path.dirname(path.join(root, relativePath)), { + recursive: true, + }); + yield* fileSystem.writeFileString(path.join(root, relativePath), ""); + yield* fileSystem.utimes(path.join(root, relativePath), 0, 0); + } + }); + + const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-image-search-" }); + yield* vcsProcess.run({ operation: "test.gitInit", command: "git", args: ["init"], cwd }); + yield* fileSystem.writeFileString(path.join(cwd, ".git/info/exclude"), "hidden/\n"); + yield* writeFiles(cwd, [ + ...Array.from({ length: 220 }, (_, index) => `assets/source-${index}.ts`), + ...PROJECT_FAVICON_EXTENSIONS.flatMap((extension) => [ + `assets/logo${extension}`, + `other/logo${extension.toUpperCase()}`, + ]), + "assets/logo.ts", + "assets/logotype.png", + "assetss/logotype.png", + "assets/images/logo.PNG", + "assets/foo{bar}.png", + "assets/[id].png", + "hidden/logo.png", + ]); + + const fileSearch = vi.spyOn(FileFinder.prototype, "fileSearchAsync"); + const searchIndex = yield* WorkspaceSearchIndex.make(cwd); + yield* searchIndex.search("", 200, undefined, true); + const nativeResult = yield* Effect.promise( + (): ReturnType => fileSearch.mock.results[0]?.value, + ); + if (!nativeResult.ok) return yield* Effect.die(nativeResult.error); + expect(nativeResult.value.items.length).toBeLessThanOrEqual(201); + + // The image glob must not change fff's parse of fuzzy paths, globs, negations or locations. + for (const query of [ + "", + "logo", + "lgo", + "*.png", + "!*.png", + "assets/**", + "!assets/", + "assets*/", + "/assets", + "assets/", + "logo:12", + "/assets/logotype.png", + "assets/images/", + "logo *.svg !other", + "foo{bar}.png", + "[id].png", + "*.{png,svg}", + "hidden/", + ]) { + const ordinary = yield* searchIndex.search(query, 1000, "file"); + const expected = ordinary.entries.filter((entry) => isProjectFaviconPath(entry.path)); + for (const limit of [1, 200]) { + const images = yield* searchIndex.search(query, limit, "directory", true); + expect(images, `query=${query} limit=${limit}`).toEqual({ + entries: expected.slice(0, limit), + truncated: expected.length > limit, + }); + } + } + + // Past 15,000 matches fff drops its filename fallback, which the image glob would otherwise + // reorder: "x/logo/logo.png" must keep its place relative to "a/xxlogo.png". + const largeCwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-image-rank-" }); + yield* writeFiles(largeCwd, [ + ...Array.from({ length: 14_998 }, (_, index) => `b/logo-${index}.png`), + "x/logo/source.ts", + "a/xxlogo.png", + "x/logo/logo.png", + ]); + const largeIndex = yield* WorkspaceSearchIndex.make(largeCwd); + const pairPaths = ["a/xxlogo.png", "x/logo/logo.png"]; + const ordinary = yield* largeIndex.search("logo", 20_000, "file"); + const images = yield* largeIndex.search("logo", 20_000, "file", true); + expect(images.entries).toHaveLength(15_000); + expect(images.entries.filter((entry) => pairPaths.includes(entry.path))).toEqual( + ordinary.entries.filter((entry) => pairPaths.includes(entry.path)), + ); + }).pipe(Effect.scoped, Effect.provide(testLayer)), ); it.effect("fetches past a first page of Git-ignored results", () => diff --git a/apps/server/src/workspace/WorkspaceSearchIndex.ts b/apps/server/src/workspace/WorkspaceSearchIndex.ts index 22e307c182616..fd92420396e48 100644 --- a/apps/server/src/workspace/WorkspaceSearchIndex.ts +++ b/apps/server/src/workspace/WorkspaceSearchIndex.ts @@ -32,7 +32,7 @@ import type { ProjectSearchEntriesResult, } from "@t3tools/contracts"; import * as HostProcess from "@t3tools/shared/HostProcess"; -import { isProjectFaviconPath } from "@t3tools/shared/projectFavicon"; +import { PROJECT_FAVICON_EXTENSIONS, isProjectFaviconPath } from "@t3tools/shared/projectFavicon"; import { scoreSubsequenceMatch } from "@t3tools/shared/searchRanking"; import * as VcsProcess from "../vcs/VcsProcess.ts"; @@ -42,6 +42,14 @@ import { type IgnoreDecision, type IgnoreFile, parseIgnoreFile } from "./IgnoreP const requireForFff = NodeModule.createRequire(import.meta.url); const { FileFinder } = requireForFff("@ff-labs/fff-node") as typeof import("@ff-labs/fff-node"); +// A glob intersects existing extension constraints; fff ORs bare `*.ext` constraints together. +const IMAGE_SEARCH_CONSTRAINT = `**/*.{${PROJECT_FAVICON_EXTENSIONS.map((extension) => + extension.slice(1).replace(/[a-z]/g, (letter) => `[${letter}${letter.toUpperCase()}]`), +).join(",")}}`; + +// fff 0.11 disables filename fallback above this match count per base/overflow segment. +const FFF_FILENAME_FALLBACK_MAX_MATCHES = 15_000; + const WORKSPACE_INDEX_MAX_ENTRIES = 25_000; const WORKSPACE_INDEX_PAGE_SIZE = WORKSPACE_INDEX_MAX_ENTRIES + 2; const WORKSPACE_INDEX_SCAN_TIMEOUT = "15 seconds"; @@ -134,7 +142,10 @@ export class WorkspaceSearchIndex extends Context.Service< ) => Effect.Effect; readonly searchContents: ( input: Omit, - ) => Effect.Effect; + ) => Effect.Effect< + ProjectSearchContentsResult, + WorkspaceSearchIndexScanTimedOut | WorkspaceSearchIndexSearchFailed + >; readonly refresh: () => Effect.Effect; } >()("t3/workspace/WorkspaceSearchIndex") {} @@ -175,6 +186,14 @@ function toDirectoryEntry(item: DirItem, hostIsWindows: boolean): ProjectEntry | return normalizedPath ? { path: normalizedPath, kind: "directory" } : null; } +function isLoneFuzzyPath(query: string): boolean { + const trimmed = query.trim(); + return ( + !/\s/u.test(trimmed) && + (trimmed.startsWith("/") || (trimmed.endsWith("/") && !/^[*!\\]/u.test(trimmed))) + ); +} + type SearchPage = { readonly items: ReadonlyArray; readonly totalMatched: number }; function mapFileSearchResult( @@ -569,13 +588,12 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* ( cause, }), ).pipe( + // The scan keeps running on this finder; failing here would destroy it and cache the failure. Effect.catchTags({ - WorkspaceSearchIndexScanTimedOut: (error) => - variant === "paths" - ? Effect.sync(() => { - initialScanTimedOut = true; - }) - : Effect.fail(error), + WorkspaceSearchIndexScanTimedOut: () => + Effect.sync(() => { + initialScanTimedOut = true; + }), }), ); @@ -593,7 +611,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* ( const runSearch = Effect.fn("WorkspaceSearchIndex.runSearch")(function* ( query: string, pageSize: number, - operation: "directorySearch" | "fileSearch" | "grep" | "mixedSearch", + operation: "directorySearch" | "fileSearch" | "getScanProgress" | "grep" | "mixedSearch", execute: () => Promise>, ): Effect.fn.Return { // fff's searches wait on its picker lock, which its watcher can hold for minutes. @@ -841,13 +859,13 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* ( }); // Grows the first page until hidden entries can no longer crowd visible ones out of it. - const searchVisible = Effect.fn("WorkspaceSearchIndex.searchVisible")(function* ( + const searchVisible = Effect.fn("WorkspaceSearchIndex.searchVisible")(function* ( query: string, pageSize: number, operation: "directorySearch" | "fileSearch" | "mixedSearch", - execute: (pageSize: number) => Promise>>, + execute: (pageSize: number) => Promise & Metadata>>, pathOf: (item: I) => string, - ): Effect.fn.Return, WorkspaceSearchIndexSearchFailed> { + ): Effect.fn.Return & Metadata, WorkspaceSearchIndexSearchFailed> { let size = pageSize; while (true) { const fetchSize = size; @@ -862,6 +880,7 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* ( fetchSize >= WORKSPACE_INDEX_PAGE_SIZE ) { return { + ...result, items, totalMatched: result.totalMatched - (result.items.length - items.length), }; @@ -945,17 +964,40 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* ( ); // Index results that are filtered-out links get dropped in the merge, so fetch past them. const indexLimit = limit + linkPaths.size - symlinks.length; - const pageSize = imageOnly ? WORKSPACE_INDEX_PAGE_SIZE : Math.max(1, indexLimit + 1); + const pageSize = Math.max(1, indexLimit + 1); const merge = (indexed: ProjectSearchEntriesResult) => mergeSymlinkMatches(indexed, linkPaths, symlinks, query, limit); if (kind === "file" || imageOnly) { - const result = yield* searchVisible( - query, + const searchFiles = (fileQuery: string, size: number) => + searchVisible( + query, + size, + "fileSearch", + (fetchSize) => finder.fileSearchAsync(fileQuery, { pageSize: fetchSize }), + (item: FileItem) => item.relativePath, + ); + // Adding a constraint changes fff's lone-path parse; offset pages can skip tied matches. + if (imageOnly && isLoneFuzzyPath(query)) { + const result = yield* searchFiles(query, WORKSPACE_INDEX_PAGE_SIZE); + return merge(mapFileSearchResult(result, indexLimit, hostIsWindows, true)); + } + let result = yield* searchFiles( + imageOnly ? `${query} ${IMAGE_SEARCH_CONSTRAINT}` : query, pageSize, - "fileSearch", - (size) => finder.fileSearchAsync(query, { pageSize: size }), - (item) => item.relativePath, ); + if ( + imageOnly && + query.trim() && + result.totalFiles > FFF_FILENAME_FALLBACK_MAX_MATCHES && + result.totalMatched > 0 + ) { + const unfiltered = yield* runSearch(query, 1, "fileSearch", () => + finder.fileSearchAsync(query, { pageSize: 1 }), + ); + if (unfiltered.totalMatched > FFF_FILENAME_FALLBACK_MAX_MATCHES) { + result = yield* searchFiles(query, WORKSPACE_INDEX_PAGE_SIZE); + } + } return merge(mapFileSearchResult(result, indexLimit, hostIsWindows, imageOnly)); } if (kind === "directory") { @@ -996,6 +1038,20 @@ export const make = Effect.fn("WorkspaceSearchIndex.make")(function* ( const searchContents: WorkspaceSearchIndex["Service"]["searchContents"] = Effect.fn( "WorkspaceSearchIndex.searchContents", )(function* (input) { + if (initialScanTimedOut) { + const progress = yield* runSearch(input.query, input.limit, "getScanProgress", () => + finder.getScanProgressAsync(), + ); + // Files the first scan has not indexed yet would read as "No results found". + if (!progress.isWarmupComplete) { + return yield* new WorkspaceSearchIndexScanTimedOut({ + cwd, + timeout: WORKSPACE_INDEX_SCAN_TIMEOUT, + }); + } + initialScanTimedOut = false; + } + let { searchQuery, regexMode } = buildContentSearchQuery(input); const deadline = performance.now() + CONTENT_SEARCH_TIME_BUDGET_MS; // Grep cursors advance per file; whole-word filtering needs enough raw candidates before advancing. diff --git a/apps/server/src/ws.test.ts b/apps/server/src/ws.test.ts index 427757d6c1883..373803cf3f2f3 100644 --- a/apps/server/src/ws.test.ts +++ b/apps/server/src/ws.test.ts @@ -1,5 +1,9 @@ // Fork: Keep regression coverage for run each project as its own git account (#1). import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver"; +import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer"; +import { WebSocket } from "@effect/platform-node/NodeWS"; +// @effect-diagnostics-next-line nodeBuiltinImport:off -- Bind an isolated WebSocket negotiation fixture. +import * as NodeHttp from "node:http"; import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; import * as NodeServices from "@effect/platform-node/NodeServices"; import { assert, it } from "@effect/vitest"; @@ -9,9 +13,12 @@ import { AuthOrchestrationReadScope, AuthSessionId, AuthSourceControlWriteScope, + ORCHESTRATION_PROTOCOL_VERSION, + ORCHESTRATION_PROTOCOL_QUERY_PARAM, EnvironmentId, EventId, GitManagerError, + VcsProcessSpawnError, MessageId, ProjectId, ThreadId, @@ -34,6 +41,7 @@ import { type ServerConfigStreamEvent, type ServerProvider, } from "@t3tools/contracts"; +import { SOCKET_FRAME_PARTS_QUERY_PARAM } from "@t3tools/shared/socketFrameParts"; import * as Deferred from "effect/Deferred"; import type { ProviderInstance } from "@t3tools/provider-core/server/driver"; import * as DateTime from "effect/DateTime"; @@ -48,7 +56,12 @@ import * as PubSub from "effect/PubSub"; import * as Queue from "effect/Queue"; import * as Stream from "effect/Stream"; import * as TestClock from "effect/testing/TestClock"; -import { Headers } from "effect/http"; +import { Headers, HttpRouter, HttpServer } from "effect/http"; +import * as Context from "effect/Context"; +import * as FetchHttpClient from "effect/http/FetchHttpClient"; +import * as VcsProcess from "./vcs/VcsProcess.ts"; +import * as ProviderInstallationGate from "./provider/ProviderInstallationGate.ts"; +import * as Ws from "./ws.ts"; import { RpcGroup, RpcMessage, RpcServer } from "effect/rpc"; import * as RpcTest from "effect/rpc/RpcTest"; import * as SqlClient from "effect/sql/SqlClient"; @@ -894,6 +907,99 @@ const layerConfigSources = Layer.mergeAll( Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), ); +it.live.each([ + { subject: "desktop-bootstrap", extensions: "", framed: false }, + { subject: "desktop-bootstrap", extensions: "", framed: true }, + { subject: "one-time-token", extensions: "permessage-deflate", framed: false }, + { subject: "cloud-connect", extensions: "permessage-deflate", framed: false }, +])( + "negotiates compression for $subject sessions (framed=$framed)", + ({ subject, extensions, framed }) => + Effect.gen(function* () { + const services = Layer.mergeAll( + layerUnusedConfigRpcServices, + Layer.mock(ServerBrowser.ServerBrowser)({ + profileReporter: Effect.succeed(() => Effect.void), + }), + Layer.mock(EnvironmentAuth.EnvironmentAuth)({ + authenticateWebSocketUpgrade: () => + Effect.succeed({ + sessionId: AuthSessionId.make("compression-test"), + subject, + method: "bearer-access-token", + scopes: [], + }), + }), + Layer.mock(SessionStore.SessionStore)({ + cookieName: "compression-test", + legacyCookieName: undefined, + markConnected: () => Effect.void, + markDisconnected: () => Effect.void, + awaitInvalidation: () => Effect.never, + }), + Layer.mock(AnalyticsService.AnalyticsService)({ recordOwners: () => Effect.void }), + Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({ + namedProjectsRoot: process.cwd(), + }), + Layer.mock(ScratchThreadPromotion.ScratchThreadPromotion)({}), + Layer.mock(ProviderRegistry.ProviderRegistry)({}), + Layer.mock(UsageLimitSources.UsageLimitSources)({}), + Layer.mock(ExternalLauncher.ExternalLauncher)({}), + Layer.mock(RemoteOpenTargets.RemoteOpenTargets)({}), + Layer.mock(DirectEndpoints.DirectEndpoints)({}), + Layer.mock(ServerEnvironment.ServerEnvironment)({}), + Layer.mock(VcsProcess.VcsProcess)({ + run: (input) => + Effect.fail( + new VcsProcessSpawnError({ + operation: input.operation, + command: input.command, + cwd: input.cwd, + cause: "No VCS commands in the compression fixture", + }), + ), + }), + ProviderInstallationGate.layer, + FetchHttpClient.layer, + ); + const context = yield* Layer.build( + HttpRouter.serve(Ws.layer, { disableListenLog: true }).pipe( + Layer.provideMerge( + NodeHttpServer.layer(NodeHttp.createServer, { + host: "127.0.0.1", + port: 0, + websocket: { perMessageDeflate: true }, + }), + ), + Layer.provide(services), + Layer.provide(layerConfigSources), + ), + ); + const server = Context.get(context, HttpServer.HttpServer); + const origin = HttpServer.formatAddress(server.address).replace(/^http/, "ws"); + const socket = yield* Effect.acquireRelease( + Effect.sync( + () => + new WebSocket( + `${origin}/ws?${ORCHESTRATION_PROTOCOL_QUERY_PARAM}=${ORCHESTRATION_PROTOCOL_VERSION}${framed ? `&${SOCKET_FRAME_PARTS_QUERY_PARAM}=1` : ""}`, + { + origin: "t3code://app", + }, + ), + ), + (socket) => Effect.sync(() => socket.terminate()), + ); + yield* Effect.promise( + () => + new Promise((resolve, reject) => { + socket.once("open", resolve); + socket.once("error", reject); + }), + ); + assert.equal(socket.extensions, extensions); + }).pipe(Effect.scoped), +); + it.effect.each([ "pending", "cached", diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts index de41156c9d75a..435ce3df28b27 100644 --- a/apps/server/src/ws.ts +++ b/apps/server/src/ws.ts @@ -2,7 +2,9 @@ import { OrchestrationDispatchCommandError } from "@t3tools/contracts"; // @effect-diagnostics-next-line nodeBuiltinImport:off -- A synchronous digest inside the pure provider mapping. import * as NodeCrypto from "node:crypto"; +import * as HostProcess from "@t3tools/shared/HostProcess"; import * as Context from "effect/Context"; +import * as NodeHttpServerRequest from "@effect/platform-node/NodeHttpServerRequest"; import * as SecureRandom from "effect/crypto/SecureRandom"; import * as Orchestrator from "./orchestration-v2/Orchestrator.ts"; import * as PeerLinkRequests from "./peer/PeerLinkRequests.ts"; @@ -1840,6 +1842,7 @@ export const layerWsRpc = ( environment, auth, cwd: config.cwd, + hostname: yield* HostProcess.Hostname, keybindingsConfigPath: config.keybindingsConfigPath, keybindings: keybindingsConfig.keybindings, issues: keybindingsConfig.issues, @@ -2672,6 +2675,10 @@ export const layerWsRpc = ( Effect.annotateCurrentSpan({ "orchestration_v2.thread_id": input.threadId }).pipe( Effect.andThen(peerLinkRequests.answer(input)), ), + [WS_METHODS.serverGetSettings]: (_input) => + Effect.map(serverSettings.getSettings, (settings) => + ServerSettings.redactServerSettingsForClient(settings, settingsClient), + ), [WS_METHODS.serverUpdateSettings]: /* Request instrumentation already owns the span; keep this helper in that context. */ Effect.fnUntraced(function* ({ patch, providerInstanceMutation }) { @@ -3187,9 +3194,19 @@ export const layerWsRpc = ( ), ), [WS_METHODS.vcsCreateRef]: (input) => - gitWorkflow.createRef(input).pipe(Effect.tap(() => refreshGitStatus(input.cwd))), + gitWorkflow + .createRef(input) + .pipe( + Effect.tap(() => + input.switchRef + ? vcsStatusBroadcaster.refreshAfterCheckout(input.cwd) + : refreshGitStatus(input.cwd), + ), + ), [WS_METHODS.vcsSwitchRef]: (input) => - gitWorkflow.switchRef(input).pipe(Effect.tap(() => refreshGitStatus(input.cwd))), + gitWorkflow + .switchRef(input) + .pipe(Effect.tap(() => vcsStatusBroadcaster.refreshAfterCheckout(input.cwd))), [WS_METHODS.vcsInit]: (input) => vcsProvisioning.initRepository(input).pipe(Effect.tap(() => refreshGitStatus(input.cwd))), [WS_METHODS.reviewGetBranchCommits]: (input) => review.getBranchCommits(input), @@ -3591,6 +3608,12 @@ export const layer = Layer.unwrap( failEnvironmentInternal("internal_error", error), ), ); + // Desktop bootstrap sessions connect to their own backend, including same-machine WSL. + if (session.subject === "desktop-bootstrap") { + delete NodeHttpServerRequest.toIncomingMessage(request).headers[ + "sec-websocket-extensions" + ]; + } const clientAnalyticsProps = readClientAnalyticsProps(request); yield* analytics.recordOwners(readClientOwners(request), "client"); const reportProfiles = yield* serverBrowser.profileReporter; diff --git a/apps/web/src/appearanceFonts.ts b/apps/web/src/appearanceFonts.ts index 19f2d88d3c29d..50a664fea336e 100644 --- a/apps/web/src/appearanceFonts.ts +++ b/apps/web/src/appearanceFonts.ts @@ -276,6 +276,18 @@ function resolveGenericFamilyLabel(generic: string): string | null { return null; } +// Synthetic Apple labels are not necessarily available as named CSS families. +export function displayFontFamily(family: string): string { + const label = family + .trim() + .replace(/^(['"])(.*)\1$/, "$2") + .trim() + .toLowerCase(); + if (label === "sf pro") return appearanceFontStack(family, DEFAULT_SANS_FONT_STACK); + if (label === "sf mono") return DEFAULT_CODE_FONT_STACK; + return cssFontFamilies(family) ?? ""; +} + export function resolveDefaultFamilyLabel(stack: string): string | null { for (const raw of stack.split(",")) { const family = raw.trim().replace(/^(['"])(.*)\1$/, "$2"); diff --git a/apps/web/src/authBootstrap.test.ts b/apps/web/src/authBootstrap.test.ts index 65c6f8edd3424..f444c64cb16ad 100644 --- a/apps/web/src/authBootstrap.test.ts +++ b/apps/web/src/authBootstrap.test.ts @@ -8,6 +8,7 @@ import { import { createBrowserHistory } from "@tanstack/react-router"; import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; +import { HttpClientError, HttpClientRequest } from "effect/http"; import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; import { installEnvironmentHttpTest } from "../test/environmentHttpTest"; @@ -165,6 +166,54 @@ describe("resolveInitialServerAuthGateState", () => { expect(testApi.calls.browserSession).toEqual([{ credential: "desktop-bootstrap-token" }]); }); + it("retries the desktop bootstrap while the backend is still unreachable", async () => { + const nextSession = sequence( + unauthenticatedSession(DESKTOP_AUTH), + authenticatedSession(DESKTOP_AUTH), + ); + const testApi = await installAuthApi({ + session: nextSession, + browserSession: () => Effect.succeed(browserSession(["orchestration:read", "access:write"])), + }); + // What FetchHttpClient yields when nothing listens on the backend port. + const connectionRefused = new HttpClientError.HttpClientError({ + reason: new HttpClientError.TransportError({ + request: HttpClientRequest.get("http://localhost:3773/api/auth/session"), + cause: new TypeError("Failed to fetch"), + }), + }); + let unreachableAttempts = 1; + __setPrimaryHttpRunnerForTests((effect) => { + if (unreachableAttempts > 0) { + unreachableAttempts -= 1; + return Promise.reject(connectionRefused); + } + return testApi.run(effect); + }); + + const testWindow = installTestBrowser("http://localhost/"); + testWindow.desktopBridge = { + getLocalEnvironmentBootstraps: () => [ + { + id: "primary", + label: "Local", + httpBaseUrl: "http://localhost:3773", + wsBaseUrl: "ws://localhost:3773", + bootstrapToken: "desktop-bootstrap-token", + }, + ], + } as unknown as DesktopBridge; + + const { resolveInitialServerAuthGateState } = await import("./environments/primary"); + + vi.useFakeTimers({ toFake: ["setTimeout", "Date"] }); + await Promise.all([ + expect(resolveInitialServerAuthGateState()).resolves.toEqual({ status: "authenticated" }), + vi.advanceTimersByTimeAsync(1_000), + ]); + expect(testApi.calls.browserSession).toEqual([{ credential: "desktop-bootstrap-token" }]); + }); + it("takes a pairing token from the location hash and strips it immediately", async () => { const testWindow = installTestBrowser("http://localhost/#token=pairing-token"); const { takePairingTokenFromUrl } = await import("./environments/primary"); diff --git a/apps/web/src/browser/HostedBrowserWebview.tsx b/apps/web/src/browser/HostedBrowserWebview.tsx index 28edc42d406a8..023fb03c3aa17 100644 --- a/apps/web/src/browser/HostedBrowserWebview.tsx +++ b/apps/web/src/browser/HostedBrowserWebview.tsx @@ -20,6 +20,7 @@ import { useActiveBrowserRecordingTabIds } from "./browserRecording"; import { browserViewportSettingKey, resolveBrowserViewportLayout, + resolveBrowserViewportTransform, resolveFittedBrowserViewport, } from "./browserViewportLayout"; import { BrowserDeviceToolbar } from "./BrowserDeviceToolbar"; @@ -313,6 +314,23 @@ export function HostedBrowserWebview(props: { return () => withDesktopTab(runtimeTabId, () => bridge.setShown(runtimeTabId, false)); }, [clientSettingsHydrated, runtimeTabId, serverDriven, shown]); + // Captures copy the guest's raster, which a scaled guest only keeps at full detail on request. + const fullDetail = backgroundActivity || pictureInPicture || recordingActive; + // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- tells the desktop host once the guest is drawn at full detail, so its screenshot waits for that frame + useEffect(() => { + const bridge = window.desktopBridge?.preview; + if (!clientSettingsHydrated || !bridge || !serverDriven || !fullDetail) return; + let frame = requestAnimationFrame(() => { + frame = requestAnimationFrame(() => + withDesktopTab(runtimeTabId, () => bridge.setCaptureDrawn(runtimeTabId, true)), + ); + }); + return () => { + cancelAnimationFrame(frame); + withDesktopTab(runtimeTabId, () => bridge.setCaptureDrawn(runtimeTabId, false)); + }; + }, [clientSettingsHydrated, fullDetail, runtimeTabId, serverDriven]); + if (!clientSettingsHydrated || !config) return null; // This fixed wrapper sits outside the right panel shell, so it takes thumb presses itself. @@ -322,6 +340,7 @@ export function HostedBrowserWebview(props: { }; const renderingActive = active || backgroundActivity || pictureInPicture || recordingActive; + const viewportTransform = resolveBrowserViewportTransform(layout.viewportScale, fullDetail); const wrapperStyle = resolveHostedBrowserWebviewWrapperStyle({ active, renderingActive, @@ -354,47 +373,56 @@ export function HostedBrowserWebview(props: { onChange={commitViewportChange} /> ) : null} - + > + + {active && effectiveViewport._tag !== "fill" && !fittedSourceViewport ? ( <> { ).toEqual({ width: 460, height: 409 }); }); }); + +describe("resolveBrowserViewportTransform", () => { + it("keeps a full-detail guest at raster scale 1 while it draws at the fitted scale", () => { + expect(resolveBrowserViewportTransform(1, true)).toBeUndefined(); + expect(resolveBrowserViewportTransform(0.6, false)).toBe("scale(0.6)"); + const values = resolveBrowserViewportTransform(0.6, true) + ?.match(/^matrix3d\((.*)\)$/)?.[1] + ?.split(",") + .map(Number); + // Column-major: the x and y columns keep their 0.6 screen terms and reach length 1 through z. + expect(values?.slice(0, 2)).toEqual([0.6, 0]); + expect(values?.slice(4, 6)).toEqual([0, 0.6]); + expect(Math.hypot(...(values?.slice(0, 3) ?? []))).toBeCloseTo(1); + expect(Math.hypot(...(values?.slice(4, 7) ?? []))).toBeCloseTo(1); + }); +}); diff --git a/apps/web/src/browser/browserViewportLayout.ts b/apps/web/src/browser/browserViewportLayout.ts index 2fe4e035100ab..7fe6b170c671d 100644 --- a/apps/web/src/browser/browserViewportLayout.ts +++ b/apps/web/src/browser/browserViewportLayout.ts @@ -125,6 +125,22 @@ export function resolveBrowserViewportLayout( }; } +/** + * Chromium rasters a CSS-scaled guest at its on-screen scale, so captures of it lose detail. + * Its raster scale also counts x/y-to-z terms, which a `preserve-3d` parent keeps and the + * screen drops, so `fullDetail` draws like `scale()` but keeps full-resolution frames. + */ +export function resolveBrowserViewportTransform( + scale: number, + fullDetail: boolean, +): string | undefined { + if (scale >= 1) return undefined; + if (!fullDetail) return `scale(${scale})`; + // Positive depth keeps the guest in front of its parent for hit testing. + const depth = Math.sqrt(1 - scale * scale); + return `matrix3d(${scale}, 0, ${depth}, 0, 0, ${scale}, ${depth}, 0, 0, 0, 1, 0, 0, 0, 0, 1)`; +} + export function resolveBrowserDeviceViewportLayout( container: { readonly width: number; readonly height: number }, setting: Exclude, diff --git a/apps/web/src/cloud/t3ConnectState.ts b/apps/web/src/cloud/t3ConnectState.ts index c468c73d965ea..67fef2eaee1a6 100644 --- a/apps/web/src/cloud/t3ConnectState.ts +++ b/apps/web/src/cloud/t3ConnectState.ts @@ -3,6 +3,7 @@ import type { RelayManagedEndpoint, RelayManagedEndpointProviderKind, } from "@t3tools/contracts/relay"; +import { parseUrl } from "@t3tools/shared/url"; // Require managed discovery: stale tunnel config after a publish-only switch causes endpoint_provider_not_managed. export function isManagedTunnelActive( @@ -31,9 +32,9 @@ export function t3ConnectMcpUrl(input: { readonly relayEndpoint: RelayManagedEndpoint | undefined; }): string | null { const url = input.thisMachine - ? URL.parse(`https://${input.tunnelHostname ?? ""}`) + ? parseUrl(`https://${input.tunnelHostname ?? ""}`) : input.relayEndpoint?.providerKind === "cloudflare_tunnel" - ? URL.parse(input.relayEndpoint.httpBaseUrl) + ? parseUrl(input.relayEndpoint.httpBaseUrl) : null; return url?.protocol === "https:" && TUNNEL_HOSTNAME.test(url.hostname) ? `${url.origin}/mcp` diff --git a/apps/web/src/components/AppRouteModal.browser.test.tsx b/apps/web/src/components/AppRouteModal.browser.test.tsx index cb31eeca45ab0..004e07744e22e 100644 --- a/apps/web/src/components/AppRouteModal.browser.test.tsx +++ b/apps/web/src/components/AppRouteModal.browser.test.tsx @@ -24,4 +24,24 @@ describe("AppRouteModal", () => { expect(onCommit).toHaveBeenCalledWith("Keep titles short"); }); + + it("stays open when Esc belongs to a modal outside Base UI", async () => { + await using _screen = await render( + + undefined} /> + , + { router: "/settings/general" }, + ); + const settings = page.getByRole("dialog", { name: "Settings" }); + // Base UI takes initial focus a frame after opening; let it land before Clerk's modal takes focus. + await expect.element(settings).toHaveFocus(); + // Clerk renders its account modal in its own root and dismisses on a document Esc listener. + const foreignModal = document.body.appendChild(document.createElement("input")); + using _foreignModal = { [Symbol.dispose]: () => foreignModal.remove() }; + + foreignModal.focus(); + await userEvent.keyboard("{Escape}"); + + await expect.element(settings).toHaveAttribute("data-open"); + }); }); diff --git a/apps/web/src/components/AppRouteModal.tsx b/apps/web/src/components/AppRouteModal.tsx index bfce6b50d4cac..0c8a8a670d2f6 100644 --- a/apps/web/src/components/AppRouteModal.tsx +++ b/apps/web/src/components/AppRouteModal.tsx @@ -31,8 +31,18 @@ export function AppRouteModal({ label, children }: { label: string; children: Re open={open} // Portaled layers outside this tree (toasts, Clerk, confirm dialogs) are not backdrop presses. disablePointerDismissal - onOpenChange={(nextOpen) => { + onOpenChange={(nextOpen, eventDetails) => { if (nextOpen) return; + // Esc from a layer outside Base UI (Clerk's modals) is that layer's to handle. + const target = eventDetails.event.target; + if ( + eventDetails.reason === "escape-key" && + target !== document.body && + target instanceof Node && + !popupRef.current?.contains(target) + ) { + return eventDetails.cancel(); + } // Closing unmounts a focused field without a blur, so commit-on-blur rows would // drop what was typed. Blur first so they commit. const focused = document.activeElement; diff --git a/apps/web/src/components/AppSidebarLayout.browser.test.tsx b/apps/web/src/components/AppSidebarLayout.browser.test.tsx new file mode 100644 index 0000000000000..c97ea5e6412aa --- /dev/null +++ b/apps/web/src/components/AppSidebarLayout.browser.test.tsx @@ -0,0 +1,42 @@ +import { page, userEvent } from "vite-plus/test/browser"; +import { describe, expect, it, vi } from "vite-plus/test"; + +import { render } from "../../test/render"; + +vi.mock(import("../state/server"), async (original) => { + const actual = await original(); + const { Atom } = await import("effect/reactivity"); + const { compileResolvedKeybindingsConfig } = await import("@t3tools/shared/keybindings"); + const keybindings = compileResolvedKeybindingsConfig([ + { key: "mod+b", command: "sidebar.toggle", when: "!terminalFocus" }, + ]); + return { ...actual, primaryServerKeybindingsAtom: Atom.make(keybindings) }; +}); +// The thread list needs live environments; the shortcut lives in the shell around it. +vi.mock(import("./Sidebar"), () => ({ default: (() => null) as never })); + +import { AppSidebarLayout } from "./AppSidebarLayout"; + +describe("sidebar toggle shortcut", () => { + it("leaves the key to a focused terminal when its rule excludes terminalFocus", async () => { + await page.viewport(1280, 800); + using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("Linux x86_64"); + await using screen = await render( + +
    +