diff --git a/apps/desktop/scripts/electron-launcher.mjs b/apps/desktop/scripts/electron-launcher.mjs index 23610f3280cb4..19ee5d501b80d 100644 --- a/apps/desktop/scripts/electron-launcher.mjs +++ b/apps/desktop/scripts/electron-launcher.mjs @@ -288,6 +288,9 @@ function resolveMacBundleInfoPlistStrings(executableName) { NSScreenCaptureUsageDescription: "T3 Code captures the active window when you use the snapshot shortcut.", NSDocumentsFolderUsageDescription: "T3 Code reads project files you open in the desktop app.", + NSRemovableVolumesUsageDescription: + "T3 Code reads project files you open from external drives.", + NSNetworkVolumesUsageDescription: "T3 Code reads project files you open from network volumes.", }; } diff --git a/apps/server/src/git/GitWorkflowService.test.ts b/apps/server/src/git/GitWorkflowService.test.ts index 43b8a32d13e14..053f917039167 100644 --- a/apps/server/src/git/GitWorkflowService.test.ts +++ b/apps/server/src/git/GitWorkflowService.test.ts @@ -6,6 +6,7 @@ import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; import * as NodeServices from "@effect/platform-node/NodeServices"; import * as FileSystem from "effect/FileSystem"; +import { GitCommandError, VcsProcessOutputReadError } from "@t3tools/contracts"; import * as ServerConfig from "../config.ts"; import * as VcsProcess from "../vcs/VcsProcess.ts"; import * as GitManager from "./GitManager.ts"; @@ -79,4 +80,36 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("GitWorkflowServi ), ), ); + + it.effect("surfaces the signal that killed Git while detecting a repository", () => + Effect.gen(function* () { + const workflow = yield* GitWorkflowService.GitWorkflowService; + const error = yield* Effect.flip(workflow.listRefs({ cwd: "/workspace" })); + assert.instanceOf(error, GitCommandError); + assert.strictEqual((error as GitCommandError).signal, "SIGABRT"); + assert.include(error.message, "(terminated by SIGABRT)"); + }).pipe( + Effect.provide( + GitWorkflowService.layer.pipe( + Layer.provide( + Layer.mock(VcsDriverRegistry.VcsDriverRegistry)({ + detect: () => + Effect.fail( + new VcsProcessOutputReadError({ + operation: "GitVcsDriver.detectRepository", + command: "git", + cwd: "/workspace", + stream: "exitCode", + signal: "SIGABRT", + cause: new Error("Process interrupted due to receipt of signal: 'SIGABRT'"), + }), + ), + }), + ), + Layer.provide(Layer.mock(GitManager.GitManager)({})), + Layer.provide(Layer.mock(GitVcsDriver.GitVcsDriver)({})), + ), + ), + ), + ); }); diff --git a/apps/server/src/git/GitWorkflowService.ts b/apps/server/src/git/GitWorkflowService.ts index ca7c0b03c13d4..dd22edca16549 100644 --- a/apps/server/src/git/GitWorkflowService.ts +++ b/apps/server/src/git/GitWorkflowService.ts @@ -33,6 +33,7 @@ import { trimAsciiWhitespace } from "@t3tools/contracts"; import * as GitManager from "./GitManager.ts"; import * as GitVcsDriver from "../vcs/GitVcsDriver.ts"; import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts"; +import { signalField } from "../vcs/processSignal.ts"; import { withWorkspaceLease } from "../workspace/workspaceLease.ts"; export class GitWorkflowService extends Context.Service< @@ -250,6 +251,7 @@ export const make = Effect.gen(function* () { operation, cwd, detail: "Failed to detect a VCS repository for this Git workflow.", + ...signalField(cause), cause, }), ), @@ -272,6 +274,7 @@ export const make = Effect.gen(function* () { command: "vcs-route", cwd, detail: "Failed to detect a VCS repository for this Git command.", + ...signalField(cause), cause, }), ), @@ -300,6 +303,7 @@ export const make = Effect.gen(function* () { operation: "GitWorkflowService.repositoryRoot", cwd, detail: "Failed to detect a VCS repository for this Git workflow.", + ...signalField(cause), cause, }), ), diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index 658f6463857dd..dad6d2b0ab52d 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -46,6 +46,7 @@ import { parseT3ProjectFile } from "@t3tools/shared/t3ProjectFile"; import { resolveProjectFileBackedSetting } from "@t3tools/shared/projectSettings"; import { gitCommandDuration, gitCommandsTotal, withMetrics } from "../observability/Metrics.ts"; import * as GitVcsDriver from "./GitVcsDriver.ts"; +import { signalField } from "./processSignal.ts"; import { removeEmptyDirectory, type RemoveEmptyDirectoryError } from "./removeEmptyDirectory.ts"; import { VcsAutomaticFetchInterval } from "./VcsAutomaticFetchInterval.ts"; import { gitConfigEnv, serverGitConfigEnv, unsetInheritedRepositoryEnv } from "./VcsProcess.ts"; @@ -1157,6 +1158,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* new GitCommandError({ ...gitCommandContext(commandInput), detail: "Failed to read Git process exit code.", + ...signalField(cause), cause, }), ), diff --git a/apps/server/src/vcs/VcsProcess.test.ts b/apps/server/src/vcs/VcsProcess.test.ts index 18eaebe6e0959..c08e2b43884d4 100644 --- a/apps/server/src/vcs/VcsProcess.test.ts +++ b/apps/server/src/vcs/VcsProcess.test.ts @@ -16,7 +16,11 @@ import * as Schema from "effect/Schema"; import * as TestClock from "effect/testing/TestClock"; import { ChildProcessSpawner } from "effect/process"; -import { VcsProcessExitError, VcsProcessSpawnError } from "@t3tools/contracts"; +import { + VcsProcessExitError, + VcsProcessOutputReadError, + VcsProcessSpawnError, +} from "@t3tools/contracts"; import * as ProcessRunner from "../processRunner.ts"; import * as VcsProcess from "./VcsProcess.ts"; @@ -53,6 +57,44 @@ const captureProcessResult = ( ); describe("VcsProcess.run", () => { + it.effect("records the signal that killed the process as a structured field", () => + Effect.gen(function* () { + const error = yield* captureProcessResult( + Effect.fail( + new ProcessRunner.ProcessReadError({ + command: "git", + argumentCount: 2, + cwd: "/workspace", + stream: "exitCode", + cause: new Error("Process interrupted due to receipt of signal: 'SIGABRT'"), + }), + ), + ); + assert.instanceOf(error, VcsProcessOutputReadError); + assert.strictEqual(error.signal, "SIGABRT"); + assert.include(error.message, "terminated by SIGABRT"); + }), + ); + + it.effect("leaves the signal unset when reading the exit code fails for another reason", () => + Effect.gen(function* () { + const error = yield* captureProcessResult( + Effect.fail( + new ProcessRunner.ProcessReadError({ + command: "git", + argumentCount: 2, + cwd: "/workspace", + stream: "exitCode", + cause: new Error("boom"), + }), + ), + ); + assert.instanceOf(error, VcsProcessOutputReadError); + assert.strictEqual(error.signal, undefined); + assert.include(error.message, "failed to read exitCode"); + }), + ); + it.effect("explains a locked Git index and allows restore after the lock is released", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; diff --git a/apps/server/src/vcs/VcsProcess.ts b/apps/server/src/vcs/VcsProcess.ts index 28b46c8c5482a..d9cab3db1f15b 100644 --- a/apps/server/src/vcs/VcsProcess.ts +++ b/apps/server/src/vcs/VcsProcess.ts @@ -24,6 +24,7 @@ import type { SourceControlProcessInput } from "@t3tools/source-control-core/ser import { gitCommandDuration, gitCommandsTotal, withMetrics } from "../observability/Metrics.ts"; import * as ProcessRunner from "../processRunner.ts"; +import { findProcessSignal } from "./processSignal.ts"; export interface VcsProcessInput { readonly operation: string; @@ -296,12 +297,15 @@ export const make = Effect.gen(function* () { stdinBytes: error.stdinBytes, cause: error.cause, }), - ProcessReadError: (error) => - new VcsProcessOutputReadError({ + ProcessReadError: (error) => { + const signal = findProcessSignal(error.cause); + return new VcsProcessOutputReadError({ ...baseError, stream: error.stream, + ...(signal === undefined ? {} : { signal }), cause: error.cause, - }), + }); + }, }), ), ); diff --git a/apps/server/src/vcs/processSignal.test.ts b/apps/server/src/vcs/processSignal.test.ts new file mode 100644 index 0000000000000..9617beba454f6 --- /dev/null +++ b/apps/server/src/vcs/processSignal.test.ts @@ -0,0 +1,25 @@ +import { describe, expect, it } from "vite-plus/test"; + +import { findProcessSignal } from "./processSignal.ts"; + +const killed = new Error("Process interrupted due to receipt of signal: 'SIGABRT'"); + +describe("findProcessSignal", () => { + it("finds the signal at the end of a nested cause chain", () => { + const platform = new Error("ChildProcess.exitCode", { cause: killed }); + expect(findProcessSignal({ message: "wrapper", cause: platform })).toBe("SIGABRT"); + }); + + it("reads a signal a wrapper already recorded", () => { + expect(findProcessSignal({ signal: "SIGKILL", message: "wrapper" })).toBe("SIGKILL"); + }); + + it("ignores unrelated causes, malformed signals, and cycles", () => { + const cyclic: { message: string; cause?: unknown } = { message: "loop" }; + cyclic.cause = cyclic; + expect(findProcessSignal(cyclic)).toBeUndefined(); + expect(findProcessSignal({ signal: "not a signal" })).toBeUndefined(); + expect(findProcessSignal(new Error("exited with signal: 'sigabrt'"))).toBeUndefined(); + expect(findProcessSignal(undefined)).toBeUndefined(); + }); +}); diff --git a/apps/server/src/vcs/processSignal.ts b/apps/server/src/vcs/processSignal.ts new file mode 100644 index 0000000000000..af1aedacc2ff0 --- /dev/null +++ b/apps/server/src/vcs/processSignal.ts @@ -0,0 +1,29 @@ +const SIGNAL_PATTERN = /receipt of signal: '(SIG[A-Z0-9]+)'/; +const MAX_CAUSE_DEPTH = 16; + +/** + * Effect reports a child killed by a signal only as an error whose message names it. Walk the + * cause chain for that message, or for a wrapper that already recorded the signal. + */ +export const findProcessSignal = (cause: unknown): string | undefined => { + const seen = new Set(); + let current = cause; + for (let depth = 0; depth < MAX_CAUSE_DEPTH; depth += 1) { + if (typeof current !== "object" || current === null || seen.has(current)) return undefined; + seen.add(current); + const { signal, message, cause: next } = current as Record; + if (typeof signal === "string" && /^SIG[A-Z0-9]+$/.test(signal)) return signal; + if (typeof message === "string") { + const match = SIGNAL_PATTERN.exec(message); + if (match?.[1] !== undefined) return match[1]; + } + current = next; + } + return undefined; +}; + +/** Spread into an error constructor to carry a signal found on `cause`. */ +export const signalField = (cause: unknown): { readonly signal?: string } => { + const signal = findProcessSignal(cause); + return signal === undefined ? {} : { signal }; +}; diff --git a/packages/client-runtime/src/errors/safeLog.test.ts b/packages/client-runtime/src/errors/safeLog.test.ts index 1e4fc4a993c5d..cf0ea1942672c 100644 --- a/packages/client-runtime/src/errors/safeLog.test.ts +++ b/packages/client-runtime/src/errors/safeLog.test.ts @@ -46,4 +46,45 @@ describe("safeErrorLogAttributes", () => { expect(attributes).toEqual({ errorType: "object" }); }); + + it("keeps validated signal, exit code, reason, and operation fields", () => { + const error = Object.assign(new Error("message-secret-sentinel"), { + _tag: "GitCommandError", + signal: "SIGABRT", + exitCode: 134, + reason: "not_a_repository", + operation: "GitWorkflowService.listRefs", + }); + + expect(safeErrorLogAttributes(error)).toMatchObject({ + errorTag: "GitCommandError", + signal: "SIGABRT", + exitCode: 134, + reason: "not_a_repository", + operation: "GitWorkflowService.listRefs", + }); + expect( + safeErrorLogAttributes({ signal: "SIGKILL", exitCode: 1, operation: "Git.status" }), + ).toEqual({ errorType: "object", signal: "SIGKILL", exitCode: 1, operation: "Git.status" }); + }); + + it("drops diagnostic fields that do not match their fixed shape", () => { + const attributes = safeErrorLogAttributes( + Object.assign(new Error("x"), { + signal: "SIGABRT at /Users/me/secret", + exitCode: 1.5, + reason: "Not A Reason: token=secret", + operation: "has spaces /Users/me", + }), + ); + + expect(attributes).not.toHaveProperty("signal"); + expect(attributes).not.toHaveProperty("exitCode"); + expect(attributes).not.toHaveProperty("reason"); + expect(attributes).not.toHaveProperty("operation"); + expect(safeErrorLogAttributes({ signal: "sigabrt", exitCode: "1", reason: 5 })).toEqual({ + errorType: "object", + }); + expect(safeErrorLogAttributes({ exitCode: Number.NaN })).toEqual({ errorType: "object" }); + }); }); diff --git a/packages/client-runtime/src/errors/safeLog.ts b/packages/client-runtime/src/errors/safeLog.ts index 2f2015b30bfca..df95afa43cd1e 100644 --- a/packages/client-runtime/src/errors/safeLog.ts +++ b/packages/client-runtime/src/errors/safeLog.ts @@ -1,6 +1,9 @@ const SAFE_ERROR_LABEL = /^(?:Error|EvalError|RangeError|ReferenceError|SyntaxError|TypeError|URIError|AggregateError|DOMException|[A-Za-z][A-Za-z0-9]*(?:Error|Failure))$/; const SAFE_TRACE_ID = /^[A-Za-z0-9._:-]{1,128}$/; +const SAFE_SIGNAL = /^SIG[A-Z0-9]{1,16}$/; +const SAFE_REASON = /^[a-z][a-z0-9_]{0,63}$/; +const SAFE_OPERATION = /^[A-Za-z][A-Za-z0-9_.:-]{0,127}$/; const STACK_FRAME_LIMIT = 32; export interface SafeErrorLogAttributes { @@ -8,9 +11,47 @@ export interface SafeErrorLogAttributes { readonly errorName?: string; readonly errorTag?: string; readonly traceId?: string; + readonly signal?: string; + readonly exitCode?: number; + readonly reason?: string; + readonly operation?: string; readonly stack?: string; } +function readPatternField(error: unknown, key: string, pattern: RegExp): string | undefined { + try { + if (typeof error !== "object" || error === null) return undefined; + const value = (error as Record)[key]; + return typeof value === "string" && pattern.test(value) ? value : undefined; + } catch { + return undefined; + } +} + +function readExitCode(error: unknown): number | undefined { + try { + if (typeof error !== "object" || error === null) return undefined; + const value = (error as { readonly exitCode?: unknown }).exitCode; + return typeof value === "number" && Number.isInteger(value) ? value : undefined; + } catch { + return undefined; + } +} + +// Only fixed-shape top-level fields: no messages, details, or nested causes, which can carry paths and secrets. +function readDiagnosticFields(error: unknown) { + const signal = readPatternField(error, "signal", SAFE_SIGNAL); + const exitCode = readExitCode(error); + const reason = readPatternField(error, "reason", SAFE_REASON); + const operation = readPatternField(error, "operation", SAFE_OPERATION); + return { + ...(signal !== undefined ? { signal } : {}), + ...(exitCode !== undefined ? { exitCode } : {}), + ...(reason !== undefined ? { reason } : {}), + ...(operation !== undefined ? { operation } : {}), + }; +} + function readSafeLabel(value: unknown): string | undefined { return typeof value === "string" && SAFE_ERROR_LABEL.test(value) ? value : undefined; } @@ -90,6 +131,7 @@ export function safeErrorLogAttributes(error: unknown): SafeErrorLogAttributes { ...(errorName !== undefined ? { errorName } : {}), ...(errorTag !== undefined ? { errorTag } : {}), ...(traceId !== undefined ? { traceId } : {}), + ...readDiagnosticFields(error), ...(stack !== undefined ? { stack } : {}), }; } @@ -105,5 +147,6 @@ export function safeErrorLogAttributes(error: unknown): SafeErrorLogAttributes { : "primitive", ...(errorTag !== undefined ? { errorTag } : {}), ...(traceId !== undefined ? { traceId } : {}), + ...readDiagnosticFields(error), }; } diff --git a/packages/contracts/src/git.ts b/packages/contracts/src/git.ts index 2faf0ffb8c248..53bca078eb4e4 100644 --- a/packages/contracts/src/git.ts +++ b/packages/contracts/src/git.ts @@ -392,12 +392,15 @@ export class GitCommandError extends Schema.TaggedError()("GitC stderrLength: Schema.optional(Schema.Number), outputLength: Schema.optional(Schema.Number), reason: ForwardCompatibleOptional(GitCommandFailureReason), + // Set when the git process was killed by a signal, such as SIGABRT. + signal: Schema.optional(Schema.String), detail: Schema.String, cause: Schema.optional(Schema.Defect()), }) { override get message(): string { const reason = this.reason === undefined ? "" : ` (${this.reason})`; - return `Git command failed in ${this.operation} (${this.cwd}): ${this.detail}${reason}`; + const signal = this.signal === undefined ? "" : ` (terminated by ${this.signal})`; + return `Git command failed in ${this.operation} (${this.cwd}): ${this.detail}${reason}${signal}`; } } @@ -417,11 +420,13 @@ export class TextGenerationError extends Schema.TaggedError export class GitManagerError extends Schema.TaggedError()("GitManagerError", { operation: Schema.String, cwd: Schema.String, + signal: Schema.optional(Schema.String), detail: Schema.String, cause: Schema.optional(Schema.Defect()), }) { override get message(): string { - return `Git manager failed in ${this.operation}: ${this.detail}`; + const signal = this.signal === undefined ? "" : ` (terminated by ${this.signal})`; + return `Git manager failed in ${this.operation}: ${this.detail}${signal}`; } } diff --git a/packages/contracts/src/vcs.ts b/packages/contracts/src/vcs.ts index 4d388ad8befa6..0eb88951210b3 100644 --- a/packages/contracts/src/vcs.ts +++ b/packages/contracts/src/vcs.ts @@ -202,10 +202,15 @@ export class VcsProcessOutputReadError extends Schema.TaggedError