Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

Optimize VCS diff loading to be up to 98% faster (#2586)

legs5165b8c333c0
diff --git a/apps/server/integration/OrchestrationEngineHarness.integration.ts b/apps/server/integration/OrchestrationEngineHarness.integration.ts
index c9d669551..837c32fc4 100644
--- a/apps/server/integration/OrchestrationEngineHarness.integration.ts
+++ b/apps/server/integration/OrchestrationEngineHarness.integration.ts
@@ -73,7 +73,6 @@ import {
import { deriveServerPaths, ServerConfig } from "../src/config.ts";
import { WorkspaceEntriesLive } from "../src/workspace/Layers/WorkspaceEntries.ts";
import { WorkspacePathsLive } from "../src/workspace/Layers/WorkspacePaths.ts";
-import * as GitVcsDriver from "../src/vcs/GitVcsDriver.ts";
import * as VcsDriverRegistry from "../src/vcs/VcsDriverRegistry.ts";
import { VcsStatusBroadcaster } from "../src/vcs/VcsStatusBroadcaster.ts";
import { GitWorkflowService } from "../src/git/GitWorkflowService.ts";
@@ -311,8 +310,11 @@ export const makeOrchestrationIntegrationHarness = (
Layer.provideMerge(serverSettingsLayer),
);
const gitWorkflowLayer = Layer.mock(GitWorkflowService)({
- renameBranch: (input: Parameters<GitVcsDriver.GitVcsDriverShape["renameBranch"]>[0]) =>
- Effect.succeed({ branch: input.newBranch }),
+ renameBranch: (input: {
+ readonly cwd: string;
+ readonly oldBranch: string;
+ readonly newBranch: string;
+ }) => Effect.succeed({ branch: input.newBranch }),
});
const textGenerationLayer = Layer.succeed(TextGeneration, {
generateBranchName: () => Effect.succeed({ branch: "update" }),
diff --git a/apps/server/src/checkpointing/Layers/CheckpointDiffQuery.test.ts b/apps/server/src/checkpointing/Layers/CheckpointDiffQuery.test.ts
index 5d4b072c9..ecbbbeae9 100644
--- a/apps/server/src/checkpointing/Layers/CheckpointDiffQuery.test.ts
+++ b/apps/server/src/checkpointing/Layers/CheckpointDiffQuery.test.ts
@@ -41,11 +41,110 @@ function makeThreadCheckpointContext(input: {
}
describe("CheckpointDiffQueryLive", () => {
+ it("uses the narrow full-thread context lookup for all-turns diffs", async () => {
+ const projectId = ProjectId.make("project-full-thread");
+ const threadId = ThreadId.make("thread-full-thread");
+ const toCheckpointRef = checkpointRefForThreadTurn(threadId, 4);
+ let getThreadCheckpointContextCalls = 0;
+ let getFullThreadDiffContextCalls = 0;
+ const diffCheckpointsCalls: Array<{
+ readonly fromCheckpointRef: CheckpointRef;
+ readonly toCheckpointRef: CheckpointRef;
+ readonly cwd: string;
+ readonly ignoreWhitespace: boolean;
+ }> = [];
+
+ const checkpointStore: CheckpointStoreShape = {
+ isGitRepository: () => Effect.succeed(true),
+ captureCheckpoint: () => Effect.void,
+ hasCheckpointRef: () => Effect.succeed(true),
+ restoreCheckpoint: () => Effect.succeed(true),
+ diffCheckpoints: ({ fromCheckpointRef, toCheckpointRef, cwd, ignoreWhitespace }) =>
+ Effect.sync(() => {
+ diffCheckpointsCalls.push({
+ fromCheckpointRef,
+ toCheckpointRef,
+ cwd,
+ ignoreWhitespace,
+ });
+ return "full thread diff patch";
+ }),
+ deleteCheckpointRefs: () => Effect.void,
+ };
+
+ const layer = CheckpointDiffQueryLive.pipe(
+ Layer.provideMerge(Layer.succeed(CheckpointStore, checkpointStore)),
+ Layer.provideMerge(
+ Layer.succeed(ProjectionSnapshotQuery, {
+ getCommandReadModel: () =>
+ Effect.die("CheckpointDiffQuery should not request the command read model"),
+ getSnapshot: () =>
+ Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
+ getShellSnapshot: () =>
+ Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
+ getArchivedShellSnapshot: () =>
+ Effect.die("CheckpointDiffQuery should not request archived shell snapshots"),
+ getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
+ getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
+ getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
+ getProjectShellById: () => Effect.succeed(Option.none()),
+ getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
+ getThreadCheckpointContext: () =>
+ Effect.sync(() => {
+ getThreadCheckpointContextCalls += 1;
+ return Option.none();
+ }),
+ getFullThreadDiffContext: () =>
+ Effect.sync(() => {
+ getFullThreadDiffContextCalls += 1;
+ return Option.some({
+ threadId,
+ projectId,
+ workspaceRoot: "/tmp/workspace",
+ worktreePath: "/tmp/worktree",
+ latestCheckpointTurnCount: 4,
+ toCheckpointRef,
+ });
+ }),
+ getThreadShellById: () => Effect.succeed(Option.none()),
+ getThreadDetailById: () => Effect.succeed(Option.none()),
+ }),
+ ),
+ );
+
+ const result = await Effect.runPromise(
+ Effect.gen(function* () {
+ const query = yield* CheckpointDiffQuery;
+ return yield* query.getFullThreadDiff({
+ threadId,
+ toTurnCount: 4,
+ ignoreWhitespace: true,
+ });
+ }).pipe(Effect.provide(layer)),
+ );
+
+ expect(getThreadCheckpointContextCalls).toBe(0);
+ expect(getFullThreadDiffContextCalls).toBe(1);
+ expect(diffCheckpointsCalls).toEqual([
+ {
+ cwd: "/tmp/worktree",
+ fromCheckpointRef: checkpointRefForThreadTurn(threadId, 0),
+ toCheckpointRef,
+ ignoreWhitespace: true,
+ },
+ ]);
+ expect(result).toEqual({
+ threadId,
+ fromTurnCount: 0,
+ toTurnCount: 4,
+ diff: "full thread diff patch",
+ });
+ });
+
it("computes diffs using canonical turn-0 checkpoint refs", async () => {
const projectId = ProjectId.make("project-1");
const threadId = ThreadId.make("thread-1");
const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
- const hasCheckpointRefCalls: Array<CheckpointRef> = [];
const diffCheckpointsCalls: Array<{
readonly fromCheckpointRef: CheckpointRef;
readonly toCheckpointRef: CheckpointRef;
@@ -65,11 +164,7 @@ describe("CheckpointDiffQueryLive", () => {
const checkpointStore: CheckpointStoreShape = {
isGitRepository: () => Effect.succeed(true),
captureCheckpoint: () => Effect.void,
- hasCheckpointRef: ({ checkpointRef }) =>
- Effect.sync(() => {
- hasCheckpointRefCalls.push(checkpointRef);
- return true;
- }),
+ hasCheckpointRef: () => Effect.succeed(true),
restoreCheckpoint: () => Effect.succeed(true),
diffCheckpoints: ({ fromCheckpointRef, toCheckpointRef, cwd, ignoreWhitespace }) =>
Effect.sync(() => {
@@ -102,6 +197,7 @@ describe("CheckpointDiffQueryLive", () => {
getProjectShellById: () => Effect.succeed(Option.none()),
getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
+ getFullThreadDiffContext: () => Effect.die("unused"),
getThreadShellById: () => Effect.succeed(Option.none()),
getThreadDetailById: () => Effect.succeed(Option.none()),
}),
@@ -121,7 +217,6 @@ describe("CheckpointDiffQueryLive", () => {
);
const expectedFromRef = checkpointRefForThreadTurn(threadId, 0);
- expect(hasCheckpointRefCalls).toEqual([expectedFromRef, toCheckpointRef]);
expect(diffCheckpointsCalls).toEqual([
{
cwd: "/tmp/workspace",
@@ -184,6 +279,7 @@ describe("CheckpointDiffQueryLive", () => {
getProjectShellById: () => Effect.succeed(Option.none()),
getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
+ getFullThreadDiffContext: () => Effect.die("unused"),
getThreadShellById: () => Effect.succeed(Option.none()),
getThreadDetailById: () => Effect.succeed(Option.none()),
}),
@@ -204,6 +300,74 @@ describe("CheckpointDiffQueryLive", () => {
expect(diffCheckpointsCalls).toEqual([{ ignoreWhitespace: true }]);
});
+ it("does not preflight checkpoint refs before diffing", async () => {
+ const projectId = ProjectId.make("project-no-preflight");
+ const threadId = ThreadId.make("thread-no-preflight");
+ const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
+ let hasCheckpointRefCallCount = 0;
+
+ const threadCheckpointContext = makeThreadCheckpointContext({
+ projectId,
+ threadId,
+ workspaceRoot: "/tmp/workspace",
+ worktreePath: null,
+ checkpointTurnCount: 1,
+ checkpointRef: toCheckpointRef,
+ });
+
+ const checkpointStore: CheckpointStoreShape = {
+ isGitRepository: () => Effect.succeed(true),
+ captureCheckpoint: () => Effect.void,
+ hasCheckpointRef: () =>
+ Effect.sync(() => {
+ hasCheckpointRefCallCount += 1;
+ return true;
+ }),
+ restoreCheckpoint: () => Effect.succeed(true),
+ diffCheckpoints: () => Effect.succeed("diff patch"),
+ deleteCheckpointRefs: () => Effect.void,
+ };
+
+ const layer = CheckpointDiffQueryLive.pipe(
+ Layer.provideMerge(Layer.succeed(CheckpointStore, checkpointStore)),
+ Layer.provideMerge(
+ Layer.succeed(ProjectionSnapshotQuery, {
+ getCommandReadModel: () =>
+ Effect.die("CheckpointDiffQuery should not request the command read model"),
+ getSnapshot: () =>
+ Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
+ getShellSnapshot: () =>
+ Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
+ getArchivedShellSnapshot: () =>
+ Effect.die("CheckpointDiffQuery should not request archived shell snapshots"),
+ getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
+ getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
+ getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
+ getProjectShellById: () => Effect.succeed(Option.none()),
+ getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
+ getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
+ getFullThreadDiffContext: () => Effect.die("unused"),
+ getThreadShellById: () => Effect.succeed(Option.none()),
+ getThreadDetailById: () => Effect.succeed(Option.none()),
+ }),
+ ),
+ );
+
+ await Effect.runPromise(
+ Effect.gen(function* () {
+ const query = yield* CheckpointDiffQuery;
+ return yield* query.getTurnDiff({
+ threadId,
+ fromTurnCount: 0,
+ toTurnCount: 1,
+ ignoreWhitespace: true,
+ });
+ }).pipe(Effect.provide(layer)),
+ );
+
+ expect(hasCheckpointRefCallCount).toBe(0);
+ });
+
it("fails when the thread is missing from the snapshot", async () => {
const threadId = ThreadId.make("thread-missing");
@@ -234,6 +398,7 @@ describe("CheckpointDiffQueryLive", () => {
getProjectShellById: () => Effect.succeed(Option.none()),
getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
getThreadCheckpointContext: () => Effect.succeed(Option.none()),
+ getFullThreadDiffContext: () => Effect.succeed(Option.none()),
getThreadShellById: () => Effect.succeed(Option.none()),
getThreadDetailById: () => Effect.succeed(Option.none()),
}),
diff --git a/apps/server/src/checkpointing/Layers/CheckpointDiffQuery.ts b/apps/server/src/checkpointing/Layers/CheckpointDiffQuery.ts
index a280c0878..b07c06ac9 100644
--- a/apps/server/src/checkpointing/Layers/CheckpointDiffQuery.ts
+++ b/apps/server/src/checkpointing/Layers/CheckpointDiffQuery.ts
@@ -1,6 +1,7 @@
import {
+ type CheckpointRef,
OrchestrationGetTurnDiffResult,
- type OrchestrationGetFullThreadDiffInput,
+ type ThreadId,
type OrchestrationGetFullThreadDiffResult,
type OrchestrationGetTurnDiffResult as OrchestrationGetTurnDiffResultType,
} from "@t3tools/contracts";
@@ -20,6 +21,22 @@ import {
const isTurnDiffResult = Schema.is(OrchestrationGetTurnDiffResult);
+function buildTurnDiffResult(
+ input: {
+ readonly threadId: ThreadId;
+ readonly fromTurnCount: number;
+ readonly toTurnCount: number;
+ },
+ diff: string,
+): OrchestrationGetTurnDiffResultType {
+ return {
+ threadId: input.threadId,
+ fromTurnCount: input.fromTurnCount,
+ toTurnCount: input.toTurnCount,
+ diff,
+ };
+}
+
const make = Effect.gen(function* () {
const projectionSnapshotQuery = yield* ProjectionSnapshotQuery;
const checkpointStore = yield* CheckpointStore;
@@ -28,6 +45,12 @@ const make = Effect.gen(function* () {
function* (input) {
const operation = "CheckpointDiffQuery.getTurnDiff";
const ignoreWhitespace = input.ignoreWhitespace ?? true;
+ yield* Effect.annotateCurrentSpan({
+ "checkpoint.thread_id": input.threadId,
+ "checkpoint.from_turn_count": input.fromTurnCount,
+ "checkpoint.to_turn_count": input.toTurnCount,
+ "checkpoint.ignore_whitespace": ignoreWhitespace,
+ });
if (input.fromTurnCount === input.toTurnCount) {
const emptyDiff: OrchestrationGetTurnDiffResultType = {
@@ -45,9 +68,9 @@ const make = Effect.gen(function* () {
return emptyDiff;
}
- const threadContext = yield* projectionSnapshotQuery.getThreadCheckpointContext(
- input.threadId,
- );
+ const threadContext = yield* projectionSnapshotQuery
+ .getThreadCheckpointContext(input.threadId)
+ .pipe(Effect.withSpan("checkpoint.turnDiff.lookupContext"));
if (Option.isNone(threadContext)) {
return yield* new CheckpointInvariantError({
operation,
@@ -100,70 +123,121 @@ const make = Effect.gen(function* () {
});
}
- const [fromExists, toExists] = yield* Effect.all(
- [
- checkpointStore.hasCheckpointRef({
- cwd: workspaceCwd,
- checkpointRef: fromCheckpointRef,
- }),
- checkpointStore.hasCheckpointRef({
- cwd: workspaceCwd,
- checkpointRef: toCheckpointRef,
- }),
- ],
- { concurrency: "unbounded" },
- );
-
- if (!fromExists) {
- return yield* new CheckpointUnavailableError({
- threadId: input.threadId,
- turnCount: input.fromTurnCount,
- detail: `Filesystem checkpoint is unavailable for turn ${input.fromTurnCount}.`,
+ const diff = yield* checkpointStore
+ .diffCheckpoints({
+ cwd: workspaceCwd,
+ fromCheckpointRef,
+ toCheckpointRef,
+ fallbackFromToHead: false,
+ ignoreWhitespace,
+ })
+ .pipe(Effect.withSpan("checkpoint.turnDiff.diffCheckpoints"));
+
+ const turnDiff = buildTurnDiffResult(input, diff);
+ if (!isTurnDiffResult(turnDiff)) {
+ return yield* new CheckpointInvariantError({
+ operation,
+ detail: "Computed turn diff result does not satisfy contract schema.",
});
}
- if (!toExists) {
- return yield* new CheckpointUnavailableError({
+ return turnDiff;
+ },
+ );
+
+ const getFullThreadDiff: CheckpointDiffQueryShape["getFullThreadDiff"] = Effect.fn(
+ "CheckpointDiffQuery.getFullThreadDiff",
+ )(function* (input) {
+ const operation = "CheckpointDiffQuery.getFullThreadDiff";
+ const ignoreWhitespace = input.ignoreWhitespace ?? true;
+ yield* Effect.annotateCurrentSpan({
+ "checkpoint.thread_id": input.threadId,
+ "checkpoint.from_turn_count": 0,
+ "checkpoint.to_turn_count": input.toTurnCount,
+ "checkpoint.ignore_whitespace": ignoreWhitespace,
+ "checkpoint.diff_kind": "full-thread",
+ });
+
+ if (input.toTurnCount === 0) {
+ const emptyDiff = buildTurnDiffResult(
+ {
threadId: input.threadId,
- turnCount: input.toTurnCount,
- detail: `Filesystem checkpoint is unavailable for turn ${input.toTurnCount}.`,
+ fromTurnCount: 0,
+ toTurnCount: 0,
+ },
+ "",
+ );
+ if (!isTurnDiffResult(emptyDiff)) {
+ return yield* new CheckpointInvariantError({
+ operation,
+ detail: "Computed full thread diff result does not satisfy contract schema.",
});
}
+ return emptyDiff satisfies OrchestrationGetFullThreadDiffResult;
+ }
+
+ const threadContext = yield* projectionSnapshotQuery
+ .getFullThreadDiffContext(input.threadId, input.toTurnCount)
+ .pipe(Effect.withSpan("checkpoint.fullThread.lookupContext"));
- const diff = yield* checkpointStore.diffCheckpoints({
+ if (Option.isNone(threadContext)) {
+ return yield* new CheckpointInvariantError({
+ operation,
+ detail: `Thread '${input.threadId}' not found.`,
+ });
+ }
+
+ if (input.toTurnCount > threadContext.value.latestCheckpointTurnCount) {
+ return yield* new CheckpointUnavailableError({
+ threadId: input.threadId,
+ turnCount: input.toTurnCount,
+ detail: `Turn diff range exceeds current turn count: requested ${input.toTurnCount}, current ${threadContext.value.latestCheckpointTurnCount}.`,
+ });
+ }
+
+ const workspaceCwd = threadContext.value.worktreePath ?? threadContext.value.workspaceRoot;
+ if (!workspaceCwd) {
+ return yield* new CheckpointInvariantError({
+ operation,
+ detail: `Workspace path missing for thread '${input.threadId}' when computing full thread diff.`,
+ });
+ }
+
+ if (!threadContext.value.toCheckpointRef) {
+ return yield* new CheckpointUnavailableError({
+ threadId: input.threadId,
+ turnCount: input.toTurnCount,
+ detail: `Checkpoint ref is unavailable for turn ${input.toTurnCount}.`,
+ });
+ }
+
+ const diff = yield* checkpointStore
+ .diffCheckpoints({
cwd: workspaceCwd,
- fromCheckpointRef,
- toCheckpointRef,
+ fromCheckpointRef: checkpointRefForThreadTurn(input.threadId, 0),
+ toCheckpointRef: threadContext.value.toCheckpointRef as CheckpointRef,
fallbackFromToHead: false,
ignoreWhitespace,
- });
+ })
+ .pipe(Effect.withSpan("checkpoint.fullThread.diffCheckpoints"));
- const turnDiff: OrchestrationGetTurnDiffResultType = {
+ const turnDiff = buildTurnDiffResult(
+ {
threadId: input.threadId,
- fromTurnCount: input.fromTurnCount,
+ fromTurnCount: 0,
toTurnCount: input.toTurnCount,
- diff,
- };
- if (!isTurnDiffResult(turnDiff)) {
- return yield* new CheckpointInvariantError({
- operation,
- detail: "Computed turn diff result does not satisfy contract schema.",
- });
- }
-
- return turnDiff;
- },
- );
+ },
+ diff,
+ );
+ if (!isTurnDiffResult(turnDiff)) {
+ return yield* new CheckpointInvariantError({
+ operation,
+ detail: "Computed full thread diff result does not satisfy contract schema.",
+ });
+ }
- const getFullThreadDiff: CheckpointDiffQueryShape["getFullThreadDiff"] = (
- input: OrchestrationGetFullThreadDiffInput,
- ) =>
- getTurnDiff({
- threadId: input.threadId,
- fromTurnCount: 0,
- toTurnCount: input.toTurnCount,
- ignoreWhitespace: input.ignoreWhitespace ?? true,
- }).pipe(Effect.map((result): OrchestrationGetFullThreadDiffResult => result));
+ return turnDiff satisfies OrchestrationGetFullThreadDiffResult;
+ });
return {
getTurnDiff,
diff --git a/apps/server/src/checkpointing/Layers/CheckpointStore.ts b/apps/server/src/checkpointing/Layers/CheckpointStore.ts
index 1dccd5e0c..9bd72e78b 100644
--- a/apps/server/src/checkpointing/Layers/CheckpointStore.ts
+++ b/apps/server/src/checkpointing/Layers/CheckpointStore.ts
@@ -1,304 +1,79 @@
/**
* CheckpointStoreLive - Filesystem checkpoint store adapter layer.
*
- * Implements hidden Git-ref checkpoint capture/restore directly with
- * Effect-native child process execution (`effect/unstable/process`).
- *
- * This layer owns filesystem/Git interactions only; it does not persist
- * checkpoint metadata and does not coordinate provider rollback semantics.
+ * Resolves the active VCS driver once per checkpoint operation and delegates
+ * checkpoint-specific behavior to the driver's optional checkpoint capability.
*
* @module CheckpointStoreLive
*/
-import { randomUUID } from "node:crypto";
-
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
-import * as FileSystem from "effect/FileSystem";
-import * as Path from "effect/Path";
-import { CheckpointInvariantError } from "../Errors.ts";
-import { VcsProcessExitError } from "@t3tools/contracts";
-import { VcsDriverRegistry } from "../../vcs/VcsDriverRegistry.ts";
import { CheckpointStore, type CheckpointStoreShape } from "../Services/CheckpointStore.ts";
-import { CheckpointRef } from "@t3tools/contracts";
-
-const CHECKPOINT_DIFF_MAX_OUTPUT_BYTES = 10_000_000;
+import { VcsUnsupportedOperationError } from "@t3tools/contracts";
+import { VcsDriverRegistry } from "../../vcs/VcsDriverRegistry.ts";
+import type { VcsCheckpointOps } from "../../vcs/VcsDriver.ts";
const makeCheckpointStore = Effect.gen(function* () {
- const fs = yield* FileSystem.FileSystem;
- const path = yield* Path.Path;
const vcsRegistry = yield* VcsDriverRegistry;
- const vcs = {
- execute: (input: {
- readonly operation: string;
- readonly cwd: string;
- readonly args: ReadonlyArray<string>;
- readonly stdin?: string;
- readonly env?: NodeJS.ProcessEnv;
- readonly allowNonZeroExit?: boolean;
- readonly timeoutMs?: number;
- readonly maxOutputBytes?: number;
- readonly truncateOutputAtMaxBytes?: boolean;
- }) =>
- vcsRegistry
- .resolve({ cwd: input.cwd, requestedKind: "git" })
- .pipe(Effect.flatMap((handle) => handle.driver.execute(input))),
- };
-
- const resolveHeadCommit = (cwd: string) =>
- vcs
- .execute({
- operation: "CheckpointStore.resolveHeadCommit",
- cwd,
- args: ["rev-parse", "--verify", "--quiet", "HEAD^{commit}"],
- allowNonZeroExit: true,
- })
- .pipe(
- Effect.map((result) => {
- if (result.exitCode !== 0) {
- return null;
- }
- const commit = result.stdout.trim();
- return commit.length > 0 ? commit : null;
- }),
- );
- const hasHeadCommit = (cwd: string) =>
- vcs
- .execute({
- operation: "CheckpointStore.hasHeadCommit",
- cwd,
- args: ["rev-parse", "--verify", "HEAD"],
- allowNonZeroExit: true,
- })
- .pipe(Effect.map((result) => result.exitCode === 0));
-
- const resolveCheckpointCommit = (cwd: string, checkpointRef: CheckpointRef) =>
- vcs
- .execute({
- operation: "CheckpointStore.resolveCheckpointCommit",
- cwd,
- args: ["rev-parse", "--verify", "--quiet", `${checkpointRef}^{commit}`],
- allowNonZeroExit: true,
- })
- .pipe(
- Effect.map((result) => {
- if (result.exitCode !== 0) {
- return null;
- }
- const commit = result.stdout.trim();
- return commit.length > 0 ? commit : null;
- }),
- );
+ const resolveCheckpoints = Effect.fn("CheckpointStore.resolveCheckpoints")(function* (
+ operation: string,
+ cwd: string,
+ ) {
+ const handle = yield* vcsRegistry.resolve({ cwd });
+ if (!handle.driver.checkpoints) {
+ return yield* new VcsUnsupportedOperationError({
+ operation,
+ kind: handle.kind,
+ detail: `${handle.kind} driver does not implement checkpoint operations.`,
+ });
+ }
+ return handle.driver.checkpoints satisfies VcsCheckpointOps;
+ });
const isGitRepository: CheckpointStoreShape["isGitRepository"] = (cwd) =>
- vcs
- .execute({
- operation: "CheckpointStore.isGitRepository",
- cwd,
- args: ["rev-parse", "--is-inside-work-tree"],
- allowNonZeroExit: true,
- })
- .pipe(
- Effect.map((result) => result.exitCode === 0 && result.stdout.trim() === "true"),
- Effect.catch(() => Effect.succeed(false)),
- );
+ vcsRegistry.resolve({ cwd, requestedKind: "git" }).pipe(
+ Effect.map(() => true),
+ Effect.catch(() => Effect.succeed(false)),
+ );
const captureCheckpoint: CheckpointStoreShape["captureCheckpoint"] = Effect.fn(
"captureCheckpoint",
)(function* (input) {
- const operation = "CheckpointStore.captureCheckpoint";
-
- yield* Effect.acquireUseRelease(
- fs.makeTempDirectory({ prefix: "t3-fs-checkpoint-" }),
- Effect.fn("captureCheckpoint.withTempDirectory")(function* (tempDir) {
- const tempIndexPath = path.join(tempDir, `index-${randomUUID()}`);
- const commitEnv: NodeJS.ProcessEnv = {
- ...process.env,
- GIT_INDEX_FILE: tempIndexPath,
- GIT_AUTHOR_NAME: "T3 Code",
- GIT_AUTHOR_EMAIL: "[email protected]",
- GIT_COMMITTER_NAME: "T3 Code",
- GIT_COMMITTER_EMAIL: "[email protected]",
- };
-
- const headExists = yield* hasHeadCommit(input.cwd);
- if (headExists) {
- yield* vcs.execute({
- operation,
- cwd: input.cwd,
- args: ["read-tree", "HEAD"],
- env: commitEnv,
- });
- }
-
- yield* vcs.execute({
- operation,
- cwd: input.cwd,
- args: ["add", "-A", "--", "."],
- env: commitEnv,
- });
-
- const writeTreeResult = yield* vcs.execute({
- operation,
- cwd: input.cwd,
- args: ["write-tree"],
- env: commitEnv,
- });
- const treeOid = writeTreeResult.stdout.trim();
- if (treeOid.length === 0) {
- return yield* new VcsProcessExitError({
- operation,
- command: "git write-tree",
- cwd: input.cwd,
- exitCode: 0,
- detail: "git write-tree returned an empty tree oid.",
- });
- }
-
- const message = `t3 checkpoint ref=${input.checkpointRef}`;
- const commitTreeResult = yield* vcs.execute({
- operation,
- cwd: input.cwd,
- args: ["commit-tree", treeOid, "-m", message],
- env: commitEnv,
- });
- const commitOid = commitTreeResult.stdout.trim();
- if (commitOid.length === 0) {
- return yield* new VcsProcessExitError({
- operation,
- command: "git commit-tree",
- cwd: input.cwd,
- exitCode: 0,
- detail: "git commit-tree returned an empty commit oid.",
- });
- }
-
- yield* vcs.execute({
- operation,
- cwd: input.cwd,
- args: ["update-ref", input.checkpointRef, commitOid],
- });
- }),
- (tempDir) => fs.remove(tempDir, { recursive: true }),
- ).pipe(
- Effect.catchTags({
- PlatformError: (error) =>
- Effect.fail(
- new CheckpointInvariantError({
- operation: "CheckpointStore.captureCheckpoint",
- detail: "Failed to capture checkpoint.",
- cause: error,
- }),
- ),
- }),
- );
+ const checkpoints = yield* resolveCheckpoints("CheckpointStore.captureCheckpoint", input.cwd);
+ return yield* checkpoints.captureCheckpoint(input);
});
- const hasCheckpointRef: CheckpointStoreShape["hasCheckpointRef"] = (input) =>
- resolveCheckpointCommit(input.cwd, input.checkpointRef).pipe(
- Effect.map((commit) => commit !== null),
- );
+ const hasCheckpointRef: CheckpointStoreShape["hasCheckpointRef"] = Effect.fn("hasCheckpointRef")(
+ function* (input) {
+ const checkpoints = yield* resolveCheckpoints("CheckpointStore.hasCheckpointRef", input.cwd);
+ return yield* checkpoints.hasCheckpointRef(input);
+ },
+ );
const restoreCheckpoint: CheckpointStoreShape["restoreCheckpoint"] = Effect.fn(
"restoreCheckpoint",
)(function* (input) {
- const operation = "CheckpointStore.restoreCheckpoint";
-
- let commitOid = yield* resolveCheckpointCommit(input.cwd, input.checkpointRef);
-
- if (!commitOid && input.fallbackToHead === true) {
- commitOid = yield* resolveHeadCommit(input.cwd);
- }
-
- if (!commitOid) {
- return false;
- }
-
- yield* vcs.execute({
- operation,
- cwd: input.cwd,
- args: ["restore", "--source", commitOid, "--worktree", "--staged", "--", "."],
- });
- yield* vcs.execute({
- operation,
- cwd: input.cwd,
- args: ["clean", "-fd", "--", "."],
- });
-
- const headExists = yield* hasHeadCommit(input.cwd);
- if (headExists) {
- yield* vcs.execute({
- operation,
- cwd: input.cwd,
- args: ["reset", "--quiet", "--", "."],
- });
- }
-
- return true;
+ const checkpoints = yield* resolveCheckpoints("CheckpointStore.restoreCheckpoint", input.cwd);
+ return yield* checkpoints.restoreCheckpoint(input);
});
const diffCheckpoints: CheckpointStoreShape["diffCheckpoints"] = Effect.fn("diffCheckpoints")(
function* (input) {
- const operation = "CheckpointStore.diffCheckpoints";
-
- let fromCommitOid = yield* resolveCheckpointCommit(input.cwd, input.fromCheckpointRef);
- const toCommitOid = yield* resolveCheckpointCommit(input.cwd, input.toCheckpointRef);
-
- if (!fromCommitOid && input.fallbackFromToHead === true) {
- const headCommit = yield* resolveHeadCommit(input.cwd);
- if (headCommit) {
- fromCommitOid = headCommit;
- }
- }
-
- if (!fromCommitOid || !toCommitOid) {
- return yield* new VcsProcessExitError({
- operation,
- command: "git diff",
- cwd: input.cwd,
- exitCode: 1,
- detail: "Checkpoint ref is unavailable for diff operation.",
- });
- }
-
- const diffArgs = [
- "diff",
- "--patch",
- "--minimal",
- "--no-color",
- ...(input.ignoreWhitespace ? ["--ignore-all-space"] : []),
- fromCommitOid,
- toCommitOid,
- ];
-
- const result = yield* vcs.execute({
- operation,
- cwd: input.cwd,
- args: diffArgs,
- maxOutputBytes: CHECKPOINT_DIFF_MAX_OUTPUT_BYTES,
- });
-
- return result.stdout;
+ const checkpoints = yield* resolveCheckpoints("CheckpointStore.diffCheckpoints", input.cwd);
+ return yield* checkpoints.diffCheckpoints(input);
},
);
const deleteCheckpointRefs: CheckpointStoreShape["deleteCheckpointRefs"] = Effect.fn(
"deleteCheckpointRefs",
)(function* (input) {
- const operation = "CheckpointStore.deleteCheckpointRefs";
-
- yield* Effect.forEach(
- input.checkpointRefs,
- (checkpointRef) =>
- vcs.execute({
- operation,
- cwd: input.cwd,
- args: ["update-ref", "-d", checkpointRef],
- allowNonZeroExit: true,
- }),
- { discard: true },
+ const checkpoints = yield* resolveCheckpoints(
+ "CheckpointStore.deleteCheckpointRefs",
+ input.cwd,
);
+ return yield* checkpoints.deleteCheckpointRefs(input);
});
return {
diff --git a/apps/server/src/environment/Layers/ServerEnvironment.ts b/apps/server/src/environment/Layers/ServerEnvironment.ts
index 87b2f3416..6972af9b3 100644
--- a/apps/server/src/environment/Layers/ServerEnvironment.ts
+++ b/apps/server/src/environment/Layers/ServerEnvironment.ts
@@ -6,6 +6,7 @@ import * as Path from "effect/Path";
import * as Random from "effect/Random";
import { ServerConfig } from "../../config.ts";
+import { layer as ProcessRunnerLive } from "../../processRunner.ts";
import { ServerEnvironment, type ServerEnvironmentShape } from "../Services/ServerEnvironment.ts";
import packageJson from "../../../package.json" with { type: "json" };
import { resolveServerEnvironmentLabel } from "./ServerEnvironmentLabel.ts";
@@ -93,4 +94,6 @@ export const makeServerEnvironment = Effect.fn("makeServerEnvironment")(function
} satisfies ServerEnvironmentShape;
});
-export const ServerEnvironmentLive = Layer.effect(ServerEnvironment, makeServerEnvironment());
+export const ServerEnvironmentLive = Layer.effect(ServerEnvironment, makeServerEnvironment()).pipe(
+ Layer.provide(ProcessRunnerLive),
+);
diff --git a/apps/server/src/environment/Layers/ServerEnvironmentLabel.test.ts b/apps/server/src/environment/Layers/ServerEnvironmentLabel.test.ts
index 26637b05f..1237b7022 100644
--- a/apps/server/src/environment/Layers/ServerEnvironmentLabel.test.ts
+++ b/apps/server/src/environment/Layers/ServerEnvironmentLabel.test.ts
@@ -1,20 +1,36 @@
import { afterEach, describe, expect, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
+import * as Layer from "effect/Layer";
import { vi } from "vitest";
-vi.mock("../../processRunner.ts", () => ({
- runProcess: vi.fn(),
-}));
-
-import { runProcess } from "../../processRunner.ts";
+import { ProcessRunner, ProcessSpawnError, type ProcessRunnerShape } from "../../processRunner.ts";
import { resolveServerEnvironmentLabel } from "./ServerEnvironmentLabel.ts";
+import { ChildProcessSpawner } from "effect/unstable/process";
+
+const runMock = vi.fn<ProcessRunnerShape["run"]>();
-const mockedRunProcess = vi.mocked(runProcess);
+const ProcessRunnerTest = Layer.succeed(
+ ProcessRunner,
+ ProcessRunner.of({
+ run: (input) => runMock(input),
+ }),
+);
const NoopFileSystemLayer = FileSystem.layerNoop({});
+const TestLayer = Layer.merge(NoopFileSystemLayer, ProcessRunnerTest);
+const LinuxMachineInfoLayer = Layer.merge(
+ ProcessRunnerTest,
+ FileSystem.layerNoop({
+ exists: (path) => Effect.succeed(path === "/etc/machine-info"),
+ readFileString: (path) =>
+ path === "/etc/machine-info"
+ ? Effect.succeed('PRETTY_HOSTNAME="Build Agent 01"\nICON_NAME="computer-vm"\n')
+ : Effect.succeed(""),
+ }),
+);
afterEach(() => {
- mockedRunProcess.mockReset();
+ runMock.mockReset();
});
describe("resolveServerEnvironmentLabel", () => {
@@ -24,7 +40,7 @@ describe("resolveServerEnvironmentLabel", () => {
cwdBaseName: "t3code",
platform: "win32",
hostname: "macbook-pro",
- }).pipe(Effect.provide(NoopFileSystemLayer));
+ }).pipe(Effect.provide(TestLayer));
expect(result).toBe("macbook-pro");
}),
@@ -32,25 +48,30 @@ describe("resolveServerEnvironmentLabel", () => {
it.effect("prefers the macOS ComputerName", () =>
Effect.gen(function* () {
- mockedRunProcess.mockResolvedValueOnce({
- stdout: " Julius's MacBook Pro \n",
- stderr: "",
- code: 0,
- signal: null,
- timedOut: false,
- });
+ runMock.mockReturnValueOnce(
+ Effect.succeed({
+ stdout: " Julius's MacBook Pro \n",
+ stderr: "",
+ code: ChildProcessSpawner.ExitCode(0),
+ timedOut: false,
+ stdoutTruncated: false,
+ stderrTruncated: false,
+ }),
+ );
const result = yield* resolveServerEnvironmentLabel({
cwdBaseName: "t3code",
platform: "darwin",
hostname: "macbook-pro",
- }).pipe(Effect.provide(NoopFileSystemLayer));
+ }).pipe(Effect.provide(TestLayer));
expect(result).toBe("Julius's MacBook Pro");
- expect(mockedRunProcess).toHaveBeenCalledWith(
- "scutil",
- ["--get", "ComputerName"],
- expect.objectContaining({ allowNonZeroExit: true }),
+ expect(runMock).toHaveBeenCalledWith(
+ expect.objectContaining({
+ command: "scutil",
+ args: ["--get", "ComputerName"],
+ timeoutBehavior: "timedOutResult",
+ }),
);
}),
);
@@ -61,44 +82,39 @@ describe("resolveServerEnvironmentLabel", () => {
cwdBaseName: "t3code",
platform: "linux",
hostname: "buildbox",
- }).pipe(
- Effect.provide(
- FileSystem.layerNoop({
- exists: (path) => Effect.succeed(path === "/etc/machine-info"),
- readFileString: (path) =>
- path === "/etc/machine-info"
- ? Effect.succeed('PRETTY_HOSTNAME="Build Agent 01"\nICON_NAME="computer-vm"\n')
- : Effect.succeed(""),
- }),
- ),
- );
+ }).pipe(Effect.provide(LinuxMachineInfoLayer));
expect(result).toBe("Build Agent 01");
- expect(mockedRunProcess).not.toHaveBeenCalled();
+ expect(runMock).not.toHaveBeenCalled();
}),
);
it.effect("falls back to hostnamectl pretty hostname on Linux", () =>
Effect.gen(function* () {
- mockedRunProcess.mockResolvedValueOnce({
- stdout: "CI Runner\n",
- stderr: "",
- code: 0,
- signal: null,
- timedOut: false,
- });
+ runMock.mockReturnValueOnce(
+ Effect.succeed({
+ stdout: "CI Runner\n",
+ stderr: "",
+ code: ChildProcessSpawner.ExitCode(0),
+ timedOut: false,
+ stdoutTruncated: false,
+ stderrTruncated: false,
+ }),
+ );
const result = yield* resolveServerEnvironmentLabel({
cwdBaseName: "t3code",
platform: "linux",
hostname: "runner-01",
- }).pipe(Effect.provide(NoopFileSystemLayer));
+ }).pipe(Effect.provide(TestLayer));
expect(result).toBe("CI Runner");
- expect(mockedRunProcess).toHaveBeenCalledWith(
- "hostnamectl",
- ["--pretty"],
- expect.objectContaining({ allowNonZeroExit: true }),
+ expect(runMock).toHaveBeenCalledWith(
+ expect.objectContaining({
+ command: "hostnamectl",
+ args: ["--pretty"],
+ timeoutBehavior: "timedOutResult",
+ }),
);
}),
);
@@ -109,7 +125,7 @@ describe("resolveServerEnvironmentLabel", () => {
cwdBaseName: "t3code",
platform: "win32",
hostname: "JULIUS-LAPTOP",
- }).pipe(Effect.provide(NoopFileSystemLayer));
+ }).pipe(Effect.provide(TestLayer));
expect(result).toBe("JULIUS-LAPTOP");
}),
@@ -117,13 +133,21 @@ describe("resolveServerEnvironmentLabel", () => {
it.effect("falls back to the hostname when the friendly-label command is missing", () =>
Effect.gen(function* () {
- mockedRunProcess.mockRejectedValueOnce(new Error("spawn scutil ENOENT"));
+ runMock.mockReturnValueOnce(
+ Effect.fail(
+ new ProcessSpawnError({
+ command: "scutil",
+ args: ["--get", "ComputerName"],
+ cause: new Error("spawn scutil ENOENT"),
+ }),
+ ),
+ );
const result = yield* resolveServerEnvironmentLabel({
cwdBaseName: "t3code",
platform: "darwin",
hostname: "macbook-pro",
- }).pipe(Effect.provide(NoopFileSystemLayer));
+ }).pipe(Effect.provide(TestLayer));
expect(result).toBe("macbook-pro");
}),
@@ -131,19 +155,22 @@ describe("resolveServerEnvironmentLabel", () => {
it.effect("falls back to the cwd basename when the hostname is blank", () =>
Effect.gen(function* () {
- mockedRunProcess.mockResolvedValueOnce({
- stdout: " ",
- stderr: "",
- code: 0,
- signal: null,
- timedOut: false,
- });
+ runMock.mockReturnValueOnce(
+ Effect.succeed({
+ stdout: " ",
+ stderr: "",
+ code: ChildProcessSpawner.ExitCode(0),
+ timedOut: false,
+ stdoutTruncated: false,
+ stderrTruncated: false,
+ }),
+ );
const result = yield* resolveServerEnvironmentLabel({
cwdBaseName: "t3code",
platform: "linux",
hostname: " ",
- }).pipe(Effect.provide(NoopFileSystemLayer));
+ }).pipe(Effect.provide(TestLayer));
expect(result).toBe("t3code");
}),
diff --git a/apps/server/src/environment/Layers/ServerEnvironmentLabel.ts b/apps/server/src/environment/Layers/ServerEnvironmentLabel.ts
index 16fa92952..7dc6bc34a 100644
--- a/apps/server/src/environment/Layers/ServerEnvironmentLabel.ts
+++ b/apps/server/src/environment/Layers/ServerEnvironmentLabel.ts
@@ -2,8 +2,9 @@ import * as OS from "node:os";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
+import * as Option from "effect/Option";
-import { runProcess } from "../../processRunner.ts";
+import { ProcessRunner } from "../../processRunner.ts";
interface ResolveServerEnvironmentLabelInput {
readonly cwdBaseName: string;
@@ -52,19 +53,21 @@ const runFriendlyLabelCommand = Effect.fn("runFriendlyLabelCommand")(function* (
command: string,
args: readonly string[],
) {
- const result = yield* Effect.tryPromise({
- try: () =>
- runProcess(command, args, {
- allowNonZeroExit: true,
- }),
- catch: () => null,
- }).pipe(Effect.orElseSucceed(() => null));
-
- if (!result || result.code !== 0) {
+ const processRunner = yield* ProcessRunner;
+ const result = yield* processRunner
+ .run({
+ command,
+ args,
+ timeoutBehavior: "timedOutResult",
+ shell: process.platform === "win32",
+ })
+ .pipe(Effect.option);
+
+ if (Option.isNone(result) || result.value.code !== 0) {
return null;
}
- return normalizeLabel(result.stdout);
+ return normalizeLabel(result.value.stdout);
});
const resolveFriendlyHostLabel = Effect.fn("resolveFriendlyHostLabel")(function* (
diff --git a/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts b/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts
index 75fd236f9..7909d5cd6 100644
--- a/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts
+++ b/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts
@@ -9,6 +9,7 @@ import {
type OrchestrationEvent,
ProviderInstanceId,
} from "@t3tools/contracts";
+import * as NodeServices from "@effect/platform-node/NodeServices";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as ManagedRuntime from "effect/ManagedRuntime";
@@ -37,7 +38,6 @@ import {
} from "../Services/ProjectionPipeline.ts";
import { ProjectionSnapshotQuery } from "../Services/ProjectionSnapshotQuery.ts";
import { ServerConfig } from "../../config.ts";
-import * as NodeServices from "@effect/platform-node/NodeServices";
const asProjectId = (value: string): ProjectId => ProjectId.make(value);
const asMessageId = (value: string): MessageId => MessageId.make(value);
@@ -197,6 +197,7 @@ describe("OrchestrationEngine", () => {
getProjectShellById: () => Effect.succeed(Option.none()),
getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
getThreadCheckpointContext: () => Effect.succeed(Option.none()),
+ getFullThreadDiffContext: () => Effect.succeed(Option.none()),
getThreadShellById: () => Effect.succeed(Option.none()),
getThreadDetailById: () => Effect.succeed(Option.none()),
}),
@@ -785,6 +786,7 @@ describe("OrchestrationEngine", () => {
Layer.provide(OrchestrationCommandReceiptRepositoryLive),
Layer.provide(RepositoryIdentityResolverLive),
Layer.provide(SqlitePersistenceMemory),
+ Layer.provide(NodeServices.layer),
),
);
const engine = await runtime.runPromise(Effect.service(OrchestrationEngineService));
@@ -927,6 +929,7 @@ describe("OrchestrationEngine", () => {
Layer.provide(OrchestrationCommandReceiptRepositoryLive),
Layer.provide(RepositoryIdentityResolverLive),
Layer.provide(SqlitePersistenceMemory),
+ Layer.provide(NodeServices.layer),
),
);
const engine = await runtime.runPromise(Effect.service(OrchestrationEngineService));
diff --git a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts
index 01d7c2957..7db2a23e5 100644
--- a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts
+++ b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts
@@ -8,6 +8,7 @@ import {
ProviderInstanceId,
} from "@t3tools/contracts";
import { assert, it } from "@effect/vitest";
+import * as NodeServices from "@effect/platform-node/NodeServices";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as SqlClient from "effect/unstable/sql/SqlClient";
@@ -29,6 +30,7 @@ const projectionSnapshotLayer = it.layer(
OrchestrationProjectionSnapshotQueryLive.pipe(
Layer.provideMerge(RepositoryIdentityResolverLive),
Layer.provideMerge(SqlitePersistenceMemory),
+ Layer.provideMerge(NodeServices.layer),
),
);
diff --git a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts
index f326a524f..9b3c0fa7a 100644
--- a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts
+++ b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts
@@ -1,5 +1,6 @@
import {
ChatAttachment,
+ CheckpointRef,
IsoDateTime,
MessageId,
NonNegativeInt,
@@ -49,6 +50,7 @@ import { RepositoryIdentityResolver } from "../../project/Services/RepositoryIde
import { ORCHESTRATION_PROJECTOR_NAMES } from "./ProjectionPipeline.ts";
import {
ProjectionSnapshotQuery,
+ type ProjectionFullThreadDiffContext,
type ProjectionSnapshotCounts,
type ProjectionThreadCheckpointContext,
type ProjectionSnapshotQueryShape,
@@ -122,6 +124,18 @@ const ProjectionThreadCheckpointContextThreadRowSchema = Schema.Struct({
workspaceRoot: Schema.String,
worktreePath: Schema.NullOr(Schema.String),
});
+const FullThreadDiffContextLookupInput = Schema.Struct({
+ threadId: ThreadId,
+ checkpointTurnCount: NonNegativeInt,
+});
+const ProjectionFullThreadDiffContextRowSchema = Schema.Struct({
+ threadId: ThreadId,
+ projectId: ProjectId,
+ workspaceRoot: Schema.String,
+ worktreePath: Schema.NullOr(Schema.String),
+ latestCheckpointTurnCount: Schema.NullOr(NonNegativeInt),
+ toCheckpointRef: Schema.NullOr(CheckpointRef),
+});
const REQUIRED_SNAPSHOT_PROJECTORS = [
ORCHESTRATION_PROJECTOR_NAMES.projects,
@@ -882,6 +896,38 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
`,
});
+ const getFullThreadDiffContextRow = SqlSchema.findOneOption({
+ Request: FullThreadDiffContextLookupInput,
+ Result: ProjectionFullThreadDiffContextRowSchema,
+ execute: ({ threadId, checkpointTurnCount }) =>
+ sql`
+ SELECT
+ threads.thread_id AS "threadId",
+ threads.project_id AS "projectId",
+ projects.workspace_root AS "workspaceRoot",
+ threads.worktree_path AS "worktreePath",
+ (
+ SELECT MAX(turns.checkpoint_turn_count)
+ FROM projection_turns AS turns
+ WHERE turns.thread_id = threads.thread_id
+ AND turns.checkpoint_turn_count IS NOT NULL
+ ) AS "latestCheckpointTurnCount",
+ (
+ SELECT turns.checkpoint_ref
+ FROM projection_turns AS turns
+ WHERE turns.thread_id = threads.thread_id
+ AND turns.checkpoint_turn_count = ${checkpointTurnCount}
+ LIMIT 1
+ ) AS "toCheckpointRef"
+ FROM projection_threads AS threads
+ INNER JOIN projection_projects AS projects
+ ON projects.project_id = threads.project_id
+ WHERE threads.thread_id = ${threadId}
+ AND threads.deleted_at IS NULL
+ LIMIT 1
+ `,
+ });
+
const getSnapshot: ProjectionSnapshotQueryShape["getSnapshot"] = () =>
sql
.withTransaction(
@@ -1759,6 +1805,35 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
});
});
+ const getFullThreadDiffContext: NonNullable<
+ ProjectionSnapshotQueryShape["getFullThreadDiffContext"]
+ > = (threadId, toTurnCount) =>
+ Effect.gen(function* () {
+ const row = yield* getFullThreadDiffContextRow({
+ threadId,
+ checkpointTurnCount: toTurnCount,
+ }).pipe(
+ Effect.mapError(
+ toPersistenceSqlOrDecodeError(
+ "ProjectionSnapshotQuery.getFullThreadDiffContext:query",
+ "ProjectionSnapshotQuery.getFullThreadDiffContext:decodeRow",
+ ),
+ ),
+ );
+ if (Option.isNone(row)) {
+ return Option.none<ProjectionFullThreadDiffContext>();
+ }
+
+ return Option.some({
+ threadId: row.value.threadId,
+ projectId: row.value.projectId,
+ workspaceRoot: row.value.workspaceRoot,
+ worktreePath: row.value.worktreePath,
+ latestCheckpointTurnCount: row.value.latestCheckpointTurnCount ?? 0,
+ toCheckpointRef: row.value.toCheckpointRef,
+ });
+ });
+
const getThreadShellById: ProjectionSnapshotQueryShape["getThreadShellById"] = (threadId) =>
Effect.gen(function* () {
const [threadRow, latestTurnRow, sessionRow] = yield* Effect.all([
@@ -1963,6 +2038,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
getProjectShellById,
getFirstActiveThreadIdByProjectId,
getThreadCheckpointContext,
+ getFullThreadDiffContext,
getThreadShellById,
getThreadDetailById,
} satisfies ProjectionSnapshotQueryShape;
diff --git a/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts b/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts
index 53dfe33bf..7d85f0240 100644
--- a/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts
+++ b/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts
@@ -7,6 +7,7 @@
* @module ProjectionSnapshotQuery
*/
import type {
+ CheckpointRef,
OrchestrationCheckpointSummary,
OrchestrationProject,
OrchestrationProjectShell,
@@ -40,6 +41,15 @@ export interface ProjectionThreadCheckpointContext {
readonly checkpoints: ReadonlyArray<OrchestrationCheckpointSummary>;
}
+export interface ProjectionFullThreadDiffContext {
+ readonly threadId: ThreadId;
+ readonly projectId: ProjectId;
+ readonly workspaceRoot: string;
+ readonly worktreePath: string | null;
+ readonly latestCheckpointTurnCount: number;
+ readonly toCheckpointRef: CheckpointRef | null;
+}
+
/**
* ProjectionSnapshotQueryShape - Service API for read-model snapshots.
*/
@@ -125,6 +135,15 @@ export interface ProjectionSnapshotQueryShape {
threadId: ThreadId,
) => Effect.Effect<Option.Option<ProjectionThreadCheckpointContext>, ProjectionRepositoryError>;
+ /**
+ * Read only the narrow context needed to compute a full-thread diff from
+ * checkpoint 0 to a specific turn count.
+ */
+ readonly getFullThreadDiffContext: (
+ threadId: ThreadId,
+ toTurnCount: number,
+ ) => Effect.Effect<Option.Option<ProjectionFullThreadDiffContext>, ProjectionRepositoryError>;
+
/**
* Read a single active thread shell row by id.
*/
diff --git a/apps/server/src/processRunner.test.ts b/apps/server/src/processRunner.test.ts
index af4ff528e..fae9ad574 100644
--- a/apps/server/src/processRunner.test.ts
+++ b/apps/server/src/processRunner.test.ts
@@ -1,25 +1,282 @@
-import { describe, expect, it } from "vitest";
+import { describe, expect, it } from "@effect/vitest";
+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 Sink from "effect/Sink";
+import * as Stream from "effect/Stream";
+import { TestClock } from "effect/testing";
+import { ChildProcessSpawner } from "effect/unstable/process";
-import { isWindowsCommandNotFound, runProcess } from "./processRunner.ts";
+import {
+ isWindowsCommandNotFound,
+ ProcessOutputLimitError,
+ ProcessRunner,
+ ProcessTimeoutError,
+ layer as ProcessRunnerLive,
+ type ProcessRunInput,
+} from "./processRunner.ts";
-describe("runProcess", () => {
- it("fails when output exceeds max buffer in default mode", async () => {
- await expect(
- runProcess("node", ["-e", "process.stdout.write('x'.repeat(2048))"], { maxBufferBytes: 128 }),
- ).rejects.toThrow("exceeded stdout buffer limit");
+type ChildProcessCommand = {
+ readonly command: string;
+ readonly args: ReadonlyArray<string>;
+};
+
+// Accesses private properties of ChildProcessCommand for testing purposes
+function asChildProcessCommand(command: unknown): ChildProcessCommand {
+ return command as ChildProcessCommand;
+}
+
+function makeHandle(input: {
+ readonly stdout?: string | Stream.Stream<Uint8Array>;
+ readonly stderr?: string | Stream.Stream<Uint8Array>;
+ readonly code?: number;
+ readonly stdin?: ChildProcessSpawner.ChildProcessHandle["stdin"];
+ readonly exitCode?: Effect.Effect<ChildProcessSpawner.ExitCode>;
+}) {
+ return ChildProcessSpawner.makeHandle({
+ pid: ChildProcessSpawner.ProcessId(1),
+ exitCode: input.exitCode ?? Effect.succeed(ChildProcessSpawner.ExitCode(input.code ?? 0)),
+ isRunning: Effect.succeed(false),
+ kill: () => Effect.void,
+ unref: Effect.succeed(Effect.void),
+ stdin: input.stdin ?? Sink.drain,
+ stdout:
+ typeof input.stdout === "string"
+ ? Stream.encodeText(Stream.make(input.stdout))
+ : (input.stdout ?? Stream.empty),
+ stderr:
+ typeof input.stderr === "string"
+ ? Stream.encodeText(Stream.make(input.stderr))
+ : (input.stderr ?? Stream.empty),
+ all: Stream.empty,
+ getInputFd: () => Sink.drain,
+ getOutputFd: () => Stream.empty,
});
+}
+
+function makeSpawner(
+ f: (command: ChildProcessCommand) => Effect.Effect<ChildProcessSpawner.ChildProcessHandle>,
+) {
+ return ChildProcessSpawner.make((command) => f(asChildProcessCommand(command)));
+}
+
+const runWith =
+ (spawner: ChildProcessSpawner.ChildProcessSpawner["Service"]) => (input: ProcessRunInput) =>
+ Effect.service(ProcessRunner).pipe(
+ Effect.flatMap((runner) =>
+ runner.run({
+ ...input,
+ shell: input.shell ?? false,
+ }),
+ ),
+ Effect.provide(
+ ProcessRunnerLive.pipe(
+ Layer.provide(Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner)),
+ ),
+ ),
+ );
+
+describe("runProcess", () => {
+ it.effect("collects stdout through an injected ChildProcessSpawner", () =>
+ Effect.gen(function* () {
+ const spawner = makeSpawner((command) =>
+ Effect.sync(() => {
+ expect(command.command).toBe("fake");
+ expect(command.args).toEqual(["stdout-bytes", "32"]);
+ return makeHandle({ stdout: "x".repeat(32) });
+ }),
+ );
+
+ const result = yield* runWith(spawner)({
+ command: "fake",
+ args: ["stdout-bytes", "32"],
+ });
- it("truncates output when outputMode is truncate", async () => {
- const result = await runProcess("node", ["-e", "process.stdout.write('x'.repeat(2048))"], {
- maxBufferBytes: 128,
- outputMode: "truncate",
- });
+ expect(result.code).toBe(0);
+ expect(result.stdout).toBe("x".repeat(32));
+ expect(result.timedOut).toBe(false);
+ }),
+ );
- expect(result.code).toBe(0);
- expect(result.stdout.length).toBeLessThanOrEqual(128);
- expect(result.stdoutTruncated).toBe(true);
- expect(result.stderrTruncated).toBe(false);
+ it.effect("runs through the ProcessRunner service", () => {
+ const spawner = makeSpawner((command) =>
+ Effect.sync(() => {
+ expect(command.command).toBe("fake");
+ expect(command.args).toEqual(["--service"]);
+ return makeHandle({ stdout: "service ok" });
+ }),
+ );
+ const layer = ProcessRunnerLive.pipe(
+ Layer.provide(Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner)),
+ );
+
+ return Effect.gen(function* () {
+ const runner = yield* ProcessRunner;
+ const result = yield* runner.run({
+ command: "fake",
+ args: ["--service"],
+ });
+
+ expect(result.stdout).toBe("service ok");
+ }).pipe(Effect.provide(layer));
});
+
+ it.effect("fails when output exceeds max buffer in default mode", () =>
+ Effect.gen(function* () {
+ const spawner = makeSpawner(() => Effect.succeed(makeHandle({ stdout: "x".repeat(2048) })));
+
+ const error = yield* runWith(spawner)({
+ command: "fake",
+ args: ["stdout-bytes", "2048"],
+ maxOutputBytes: 128,
+ }).pipe(Effect.flip);
+
+ expect(error).toBeInstanceOf(ProcessOutputLimitError);
+ }),
+ );
+
+ it.effect("fails fast on output limit before timeout for long-running output", () =>
+ Effect.gen(function* () {
+ const textChunk = "x".repeat(64);
+ const spawner = makeSpawner(() =>
+ Effect.succeed(
+ makeHandle({
+ stdout: Stream.fromIterable(Array.from({ length: 10 }, () => textChunk)).pipe(
+ Stream.encodeText,
+ ),
+ exitCode: Effect.never,
+ }),
+ ),
+ );
+
+ const error = yield* runWith(spawner)({
+ command: "fake",
+ args: ["spam-stdout"],
+ maxOutputBytes: 128,
+ timeout: "2 seconds",
+ }).pipe(Effect.flip);
+
+ expect(error).toBeInstanceOf(ProcessOutputLimitError);
+ }),
+ );
+
+ it.effect("truncates output when outputMode is truncate", () =>
+ Effect.gen(function* () {
+ const spawner = makeSpawner(() => Effect.succeed(makeHandle({ stdout: "x".repeat(2048) })));
+
+ const result = yield* runWith(spawner)({
+ command: "fake",
+ args: ["stdout-bytes", "2048"],
+ maxOutputBytes: 128,
+ outputMode: "truncate",
+ });
+
+ expect(result.code).toBe(0);
+ expect(result.stdout.length).toBeLessThanOrEqual(128);
+ expect(result.stdoutTruncated).toBe(true);
+ expect(result.stderrTruncated).toBe(false);
+ }),
+ );
+
+ it.effect("writes stdin before waiting for exit", () =>
+ Effect.gen(function* () {
+ const stdinWritten = yield* Deferred.make<void>();
+ const decoder = new TextDecoder();
+ const spawner = makeSpawner(() =>
+ Effect.succeed(
+ makeHandle({
+ stdout: "stdin payload",
+ stdin: Sink.forEach((chunk: Uint8Array) => {
+ const text = decoder.decode(chunk, { stream: true });
+ return text.includes("stdin payload")
+ ? Deferred.succeed(stdinWritten, undefined)
+ : Effect.void;
+ }),
+ exitCode: Deferred.await(stdinWritten).pipe(Effect.as(ChildProcessSpawner.ExitCode(0))),
+ }),
+ ),
+ );
+
+ const result = yield* runWith(spawner)({
+ command: "fake",
+ args: ["stdin-echo"],
+ stdin: "stdin payload",
+ });
+
+ expect(result.stdout).toBe("stdin payload");
+ expect(result.code).toBe(0);
+ }),
+ );
+
+ it.effect("returns output for non-zero exit codes", () =>
+ Effect.gen(function* () {
+ const spawner = makeSpawner(() => Effect.succeed(makeHandle({ stderr: "boom", code: 2 })));
+
+ const result = yield* runWith(spawner)({
+ command: "fake",
+ args: ["stderr-exit", "boom", "2"],
+ });
+
+ expect(result.code).toBe(2);
+ expect(result.stderr).toBe("boom");
+ }),
+ );
+
+ it.effect("fails on timeout", () =>
+ Effect.gen(function* () {
+ const spawner = makeSpawner(() =>
+ Effect.succeed(
+ makeHandle({
+ exitCode: Effect.never,
+ }),
+ ),
+ );
+ const errorFiber = yield* runWith(spawner)({
+ command: "fake",
+ args: ["sleep"],
+ timeout: "50 millis",
+ }).pipe(Effect.flip, Effect.forkScoped);
+
+ yield* Effect.yieldNow;
+ yield* TestClock.adjust(Duration.millis(50));
+ const error = yield* Fiber.join(errorFiber);
+
+ expect(error).toBeInstanceOf(ProcessTimeoutError);
+ }),
+ );
+
+ it.effect("returns a synthetic timed out result when timeoutBehavior is timedOutResult", () =>
+ Effect.gen(function* () {
+ const spawner = makeSpawner(() =>
+ Effect.succeed(
+ makeHandle({
+ exitCode: Effect.never,
+ }),
+ ),
+ );
+ const resultFiber = yield* runWith(spawner)({
+ command: "fake",
+ args: ["sleep"],
+ timeout: "50 millis",
+ timeoutBehavior: "timedOutResult",
+ }).pipe(Effect.forkScoped);
+
+ yield* Effect.yieldNow;
+ yield* TestClock.adjust(Duration.millis(50));
+ const result = yield* Fiber.join(resultFiber);
+
+ expect(result).toMatchObject({
+ stdout: "",
+ stderr: "",
+ code: null,
+ timedOut: true,
+ stdoutTruncated: false,
+ stderrTruncated: false,
+ });
+ }),
+ );
});
describe("isWindowsCommandNotFound", () => {
diff --git a/apps/server/src/processRunner.ts b/apps/server/src/processRunner.ts
index aeeec53e0..55988a853 100644
--- a/apps/server/src/processRunner.ts
+++ b/apps/server/src/processRunner.ts
@@ -1,43 +1,101 @@
-// @effect-diagnostics nodeBuiltinImport:off
-import { type ChildProcess as ChildProcessHandle, spawn, spawnSync } from "node:child_process";
-import * as NodeTimers from "node:timers";
-export interface ProcessRunOptions {
- cwd?: string | undefined;
- timeoutMs?: number | undefined;
- env?: NodeJS.ProcessEnv | undefined;
- stdin?: string | undefined;
- allowNonZeroExit?: boolean | undefined;
- maxBufferBytes?: number | undefined;
- outputMode?: "error" | "truncate" | undefined;
-}
+import * as Data from "effect/Data";
+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 * as Option from "effect/Option";
+import * as PlatformError from "effect/PlatformError";
+import * as Scope from "effect/Scope";
+import * as Stream from "effect/Stream";
+import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
+import {
+ collectUint8StreamText,
+ type CollectedUint8StreamText,
+} from "./stream/collectUint8StreamText.ts";
-export interface ProcessRunResult {
- stdout: string;
- stderr: string;
- code: number | null;
- signal: NodeJS.Signals | null;
- timedOut: boolean;
- stdoutTruncated?: boolean | undefined;
- stderrTruncated?: boolean | undefined;
+export interface ProcessRunInput {
+ readonly command: string;
+ readonly args: ReadonlyArray<string>;
+ readonly cwd?: string | undefined;
+ readonly spawnCwd?: string | undefined;
+ readonly timeout?: Duration.Input | undefined;
+ readonly env?: NodeJS.ProcessEnv | undefined;
+ readonly stdin?: string | undefined;
+ readonly maxOutputBytes?: number | undefined;
+ readonly outputMode?: "error" | "truncate" | undefined;
+ readonly truncatedMarker?: string | undefined;
+ readonly shell?: boolean | string | undefined;
+ /**
+ * On timeout, return a synthetic timedOut result.
+ * Partial stdout/stderr are not preserved.
+ */
+ readonly timeoutBehavior?: "error" | "timedOutResult" | undefined;
}
-function commandLabel(command: string, args: readonly string[]): string {
- return [command, ...args].join(" ");
+export interface ProcessRunOutput {
+ readonly stdout: string;
+ readonly stderr: string;
+ readonly code: ChildProcessSpawner.ExitCode | null;
+ readonly timedOut: boolean;
+ readonly stdoutTruncated: boolean;
+ readonly stderrTruncated: boolean;
}
-function normalizeSpawnError(command: string, args: readonly string[], error: unknown): Error {
- if (!(error instanceof Error)) {
- return new Error(`Failed to run ${commandLabel(command, args)}.`);
- }
+export class ProcessSpawnError extends Data.TaggedError("ProcessSpawnError")<{
+ readonly command: string;
+ readonly args: ReadonlyArray<string>;
+ readonly cwd?: string | undefined;
+ readonly cause: unknown;
+}> {}
- const maybeCode = (error as NodeJS.ErrnoException).code;
- if (maybeCode === "ENOENT") {
- return new Error(`Command not found: ${command}`);
- }
+export class ProcessStdinError extends Data.TaggedError("ProcessStdinError")<{
+ readonly command: string;
+ readonly args: ReadonlyArray<string>;
+ readonly cwd?: string | undefined;
+ readonly cause: unknown;
+}> {}
+
+export class ProcessOutputLimitError extends Data.TaggedError("ProcessOutputLimitError")<{
+ readonly command: string;
+ readonly args: ReadonlyArray<string>;
+ readonly cwd?: string | undefined;
+ readonly stream: "stdout" | "stderr";
+ readonly maxBytes: number;
+}> {}
+
+export class ProcessReadError extends Data.TaggedError("ProcessReadError")<{
+ readonly command: string;
+ readonly args: ReadonlyArray<string>;
+ readonly cwd?: string | undefined;
+ readonly stream: "stdout" | "stderr" | "exitCode";
+ readonly cause: unknown;
+}> {}
+
+export class ProcessTimeoutError extends Data.TaggedError("ProcessTimeoutError")<{
+ readonly command: string;
+ readonly args: ReadonlyArray<string>;
+ readonly cwd?: string | undefined;
+ readonly timeoutMs: number;
+}> {}
+
+export type ProcessRunError =
+ | ProcessSpawnError
+ | ProcessStdinError
+ | ProcessOutputLimitError
+ | ProcessReadError
+ | ProcessTimeoutError;
- return new Error(`Failed to run ${commandLabel(command, args)}: ${error.message}`);
+export interface ProcessRunnerShape {
+ readonly run: (input: ProcessRunInput) => Effect.Effect<ProcessRunOutput, ProcessRunError>;
}
+export class ProcessRunner extends Context.Service<ProcessRunner, ProcessRunnerShape>()(
+ "t3/process/ProcessRunner",
+) {}
+
+const DEFAULT_TIMEOUT = "60 seconds";
+const DEFAULT_MAX_OUTPUT_BYTES = 8 * 1024 * 1024;
+
const WINDOWS_COMMAND_NOT_FOUND_PATTERNS = [
/is not recognized as an internal or external command/i,
/n.o . reconhecido como um comando interno/i,
@@ -57,230 +115,222 @@ export function isWindowsCommandNotFound(code: number | null, stderr: string): b
return hasWindowsCommandNotFoundMessage(stderr);
}
-function normalizeExitError(
- command: string,
- args: readonly string[],
- result: ProcessRunResult,
-): Error {
- if (isWindowsCommandNotFound(result.code, result.stderr)) {
- return new Error(`Command not found: ${command}`);
- }
-
- const reason = result.timedOut
- ? "timed out"
- : `failed (code=${result.code ?? "null"}, signal=${result.signal ?? "null"})`;
- const stderr = result.stderr.trim();
- const detail = stderr.length > 0 ? ` ${stderr}` : "";
- return new Error(`${commandLabel(command, args)} ${reason}.${detail}`);
-}
-
-function normalizeStdinError(command: string, args: readonly string[], error: unknown): Error {
- if (!(error instanceof Error)) {
- return new Error(`Failed to write stdin for ${commandLabel(command, args)}.`);
- }
- return new Error(`Failed to write stdin for ${commandLabel(command, args)}: ${error.message}`);
-}
-
-function normalizeBufferError(
- command: string,
- args: readonly string[],
- stream: "stdout" | "stderr",
- maxBufferBytes: number,
-): Error {
- return new Error(
- `${commandLabel(command, args)} exceeded ${stream} buffer limit (${maxBufferBytes} bytes).`,
+const collectText = Effect.fn("processRunner.collectText")(function* (input: {
+ readonly command: string;
+ readonly args: ReadonlyArray<string>;
+ readonly cwd?: string | undefined;
+ readonly streamName: "stdout" | "stderr";
+ readonly stream: Stream.Stream<Uint8Array, PlatformError.PlatformError>;
+ readonly maxOutputBytes: number;
+ readonly outputMode: "error" | "truncate";
+ readonly truncatedMarker: string;
+}) {
+ const stream = input.stream.pipe(
+ Stream.mapError(
+ (cause) =>
+ new ProcessReadError({
+ command: input.command,
+ args: input.args,
+ cwd: input.cwd,
+ stream: input.streamName,
+ cause,
+ }),
+ ),
);
-}
-const DEFAULT_MAX_BUFFER_BYTES = 8 * 1024 * 1024;
-
-/**
- * On Windows with `shell: true`, `child.kill()` only terminates the `cmd.exe`
- * wrapper, leaving the actual command running. Use `taskkill /T` to kill the
- * entire process tree instead.
- */
-function killChild(child: ChildProcessHandle, signal: NodeJS.Signals = "SIGTERM"): void {
- if (process.platform === "win32" && child.pid !== undefined) {
- try {
- spawnSync("taskkill", ["/pid", String(child.pid), "/T", "/F"], { stdio: "ignore" });
- return;
- } catch {
- // fallback to direct kill
- }
- }
- child.kill(signal);
-}
-
-function appendChunkWithinLimit(
- target: string,
- currentBytes: number,
- chunk: Buffer,
- maxBytes: number,
-): {
- next: string;
- nextBytes: number;
- truncated: boolean;
-} {
- const remaining = maxBytes - currentBytes;
- if (remaining <= 0) {
- return { next: target, nextBytes: currentBytes, truncated: true };
- }
- if (chunk.length <= remaining) {
- return {
- next: `${target}${chunk.toString()}`,
- nextBytes: currentBytes + chunk.length,
- truncated: false,
- };
+ if (input.outputMode === "truncate") {
+ return yield* collectUint8StreamText({
+ stream,
+ maxBytes: input.maxOutputBytes,
+ truncatedMarker: input.truncatedMarker,
+ });
}
- return {
- next: `${target}${chunk.subarray(0, remaining).toString()}`,
- nextBytes: currentBytes + remaining,
- truncated: true,
- };
-}
-
-export async function runProcess(
- command: string,
- args: readonly string[],
- options: ProcessRunOptions = {},
-): Promise<ProcessRunResult> {
- const timeoutMs = options.timeoutMs ?? 60_000;
- const maxBufferBytes = options.maxBufferBytes ?? DEFAULT_MAX_BUFFER_BYTES;
- const outputMode = options.outputMode ?? "error";
- return new Promise<ProcessRunResult>((resolve, reject) => {
- const child = spawn(command, args, {
- cwd: options.cwd,
- env: options.env,
- stdio: "pipe",
- shell: process.platform === "win32",
- });
+ return yield* stream.pipe(
+ Stream.runFoldEffect<
+ {
+ readonly chunks: Uint8Array<ArrayBufferLike>[];
+ readonly bytes: number;
+ },
+ Uint8Array<ArrayBufferLike>,
+ ProcessOutputLimitError | ProcessReadError,
+ never
+ >(
+ () => ({ chunks: [], bytes: 0 }),
+ (state, chunk) => {
+ const remainingBytes = input.maxOutputBytes - state.bytes;
+ if (remainingBytes <= 0 || chunk.byteLength > remainingBytes) {
+ return Effect.fail(
+ new ProcessOutputLimitError({
+ command: input.command,
+ args: input.args,
+ cwd: input.cwd,
+ stream: input.streamName,
+ maxBytes: input.maxOutputBytes,
+ }),
+ );
+ }
- let stdout = "";
- let stderr = "";
- let stdoutBytes = 0;
- let stderrBytes = 0;
- let stdoutTruncated = false;
- let stderrTruncated = false;
- let timedOut = false;
- let settled = false;
- let forceKillTimer: ReturnType<typeof NodeTimers.setTimeout> | null = null;
+ state.chunks.push(chunk);
+ return Effect.succeed({
+ chunks: state.chunks,
+ bytes: state.bytes + chunk.byteLength,
+ });
+ },
+ ),
+ Effect.map(
+ (state): CollectedUint8StreamText => ({
+ text: Buffer.concat(state.chunks, state.bytes).toString("utf8"),
+ bytes: state.bytes,
+ truncated: false,
+ }),
+ ),
+ );
+});
- // @effect-diagnostics-next-line globalTimers:off - Promise/child_process boundary; moving this runner to Effect timers is a separate refactor.
- const timeoutTimer = NodeTimers.setTimeout(() => {
- timedOut = true;
- killChild(child, "SIGTERM");
- // @effect-diagnostics-next-line globalTimers:off - Promise/child_process boundary; see timeout timer above.
- forceKillTimer = NodeTimers.setTimeout(() => {
- killChild(child, "SIGKILL");
- }, 1_000);
- }, timeoutMs);
+function finalizeRunProcess<R>(
+ effect: Effect.Effect<ProcessRunOutput, ProcessRunError, R | Scope.Scope>,
+ input: ProcessRunInput,
+): Effect.Effect<ProcessRunOutput, ProcessRunError, Exclude<R, Scope.Scope>> {
+ const timeout = Duration.fromInputUnsafe(input.timeout ?? DEFAULT_TIMEOUT);
+ const timeoutBehavior = input.timeoutBehavior ?? "error";
- const finalize = (callback: () => void): void => {
- if (settled) return;
- settled = true;
- NodeTimers.clearTimeout(timeoutTimer);
- if (forceKillTimer) {
- NodeTimers.clearTimeout(forceKillTimer);
+ return effect.pipe(
+ Effect.scoped,
+ Effect.timeoutOption(timeout),
+ Effect.flatMap((result) => {
+ if (Option.isSome(result)) {
+ return Effect.succeed(result.value);
+ }
+ if (timeoutBehavior === "timedOutResult") {
+ return Effect.succeed({
+ stdout: "",
+ stderr: "",
+ code: null,
+ timedOut: true,
+ stdoutTruncated: false,
+ stderrTruncated: false,
+ } satisfies ProcessRunOutput);
}
- callback();
- };
+ return Effect.fail(
+ new ProcessTimeoutError({
+ command: input.command,
+ args: input.args,
+ cwd: input.cwd,
+ timeoutMs: Duration.toMillis(timeout),
+ }),
+ );
+ }),
+ );
+}
- const fail = (error: Error): void => {
- killChild(child, "SIGTERM");
- finalize(() => {
- reject(error);
- });
- };
+const runProcessCore = Effect.fn("processRunner.runProcessCore")(function* (
+ spawner: ChildProcessSpawner.ChildProcessSpawner["Service"],
+ input: ProcessRunInput,
+): Effect.fn.Return<ProcessRunOutput, ProcessRunError, Scope.Scope> {
+ const maxOutputBytes = input.maxOutputBytes ?? DEFAULT_MAX_OUTPUT_BYTES;
+ const outputMode = input.outputMode ?? "error";
+ const truncatedMarker = input.truncatedMarker ?? "";
- const appendOutput = (stream: "stdout" | "stderr", chunk: Buffer | string): Error | null => {
- const chunkBuffer = typeof chunk === "string" ? Buffer.from(chunk) : chunk;
- const text = chunkBuffer.toString();
- const byteLength = chunkBuffer.length;
- if (stream === "stdout") {
- if (outputMode === "truncate") {
- const appended = appendChunkWithinLimit(stdout, stdoutBytes, chunkBuffer, maxBufferBytes);
- stdout = appended.next;
- stdoutBytes = appended.nextBytes;
- stdoutTruncated = stdoutTruncated || appended.truncated;
- return null;
- }
- stdout += text;
- stdoutBytes += byteLength;
- if (stdoutBytes > maxBufferBytes) {
- return normalizeBufferError(command, args, "stdout", maxBufferBytes);
- }
- } else {
- if (outputMode === "truncate") {
- const appended = appendChunkWithinLimit(stderr, stderrBytes, chunkBuffer, maxBufferBytes);
- stderr = appended.next;
- stderrBytes = appended.nextBytes;
- stderrTruncated = stderrTruncated || appended.truncated;
- return null;
- }
- stderr += text;
- stderrBytes += byteLength;
- if (stderrBytes > maxBufferBytes) {
- return normalizeBufferError(command, args, "stderr", maxBufferBytes);
- }
- }
- return null;
- };
+ const child = yield* spawner
+ .spawn(
+ ChildProcess.make(input.command, [...input.args], {
+ ...((input.spawnCwd ?? input.cwd) ? { cwd: input.spawnCwd ?? input.cwd } : {}),
+ ...(input.env !== undefined
+ ? {
+ env: input.env,
+ extendEnv: true,
+ }
+ : {}),
+ ...(input.shell !== undefined ? { shell: input.shell } : {}),
+ }),
+ )
+ .pipe(
+ Effect.mapError(
+ (cause) =>
+ new ProcessSpawnError({
+ command: input.command,
+ args: input.args,
+ cwd: input.cwd,
+ cause,
+ }),
+ ),
+ );
- child.stdout.on("data", (chunk: Buffer | string) => {
- const error = appendOutput("stdout", chunk);
- if (error) {
- fail(error);
- }
- });
+ const writeStdin =
+ input.stdin === undefined
+ ? Effect.void
+ : Stream.run(Stream.encodeText(Stream.make(input.stdin)), child.stdin).pipe(
+ Effect.mapError(
+ (cause) =>
+ new ProcessStdinError({
+ command: input.command,
+ args: input.args,
+ cwd: input.cwd,
+ cause,
+ }),
+ ),
+ );
- child.stderr.on("data", (chunk: Buffer | string) => {
- const error = appendOutput("stderr", chunk);
- if (error) {
- fail(error);
- }
- });
+ const [stdout, stderr] = yield* Effect.all(
+ [
+ collectText({
+ command: input.command,
+ args: input.args,
+ cwd: input.cwd,
+ streamName: "stdout",
+ stream: child.stdout,
+ maxOutputBytes,
+ outputMode,
+ truncatedMarker,
+ }),
+ collectText({
+ command: input.command,
+ args: input.args,
+ cwd: input.cwd,
+ streamName: "stderr",
+ stream: child.stderr,
+ maxOutputBytes,
+ outputMode,
+ truncatedMarker,
+ }),
+ writeStdin,
+ ],
+ { concurrency: "unbounded" },
+ );
- child.once("error", (error) => {
- finalize(() => {
- reject(normalizeSpawnError(command, args, error));
- });
- });
+ const exitCode = yield* child.exitCode.pipe(
+ Effect.mapError(
+ (cause) =>
+ new ProcessReadError({
+ command: input.command,
+ args: input.args,
+ cwd: input.cwd,
+ stream: "exitCode",
+ cause,
+ }),
+ ),
+ );
- child.once("close", (code, signal) => {
- const result: ProcessRunResult = {
- stdout,
- stderr,
- code,
- signal,
- timedOut,
- stdoutTruncated,
- stderrTruncated,
- };
+ return {
+ stdout: stdout.text,
+ stderr: stderr.text,
+ code: exitCode,
+ timedOut: false,
+ stdoutTruncated: stdout.truncated,
+ stderrTruncated: stderr.truncated,
+ } satisfies ProcessRunOutput;
+});
- finalize(() => {
- if (!options.allowNonZeroExit && (timedOut || (code !== null && code !== 0))) {
- reject(normalizeExitError(command, args, result));
- return;
- }
- resolve(result);
- });
- });
+export const make = Effect.fn("makeProcessRunner")(function* () {
+ const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
- child.stdin.once("error", (error) => {
- fail(normalizeStdinError(command, args, error));
- });
+ const run: ProcessRunnerShape["run"] = (input) =>
+ finalizeRunProcess(runProcessCore(spawner, input), input);
- if (options.stdin !== undefined) {
- child.stdin.write(options.stdin, (error) => {
- if (error) {
- fail(normalizeStdinError(command, args, error));
- return;
- }
- child.stdin.end();
- });
- return;
- }
- child.stdin.end();
+ return ProcessRunner.of({
+ run,
});
-}
+});
+
+export const layer = Layer.effect(ProcessRunner, make());
diff --git a/apps/server/src/project/Layers/ProjectSetupScriptRunner.test.ts b/apps/server/src/project/Layers/ProjectSetupScriptRunner.test.ts
index 1a13229fd..d0fafd56a 100644
--- a/apps/server/src/project/Layers/ProjectSetupScriptRunner.test.ts
+++ b/apps/server/src/project/Layers/ProjectSetupScriptRunner.test.ts
@@ -36,6 +36,7 @@ const makeProjectionSnapshotQueryLayer = (project: OrchestrationProject) =>
Effect.succeed(projectId === project.id ? Option.some(project) : Option.none()),
getFirstActiveThreadIdByProjectId: () => Effect.die("unused"),
getThreadCheckpointContext: () => Effect.die("unused"),
+ getFullThreadDiffContext: () => Effect.die("unused"),
getThreadShellById: () => Effect.die("unused"),
getThreadDetailById: () => Effect.die("unused"),
});
diff --git a/apps/server/src/project/Layers/RepositoryIdentityResolver.test.ts b/apps/server/src/project/Layers/RepositoryIdentityResolver.test.ts
index df88c1033..a47181c86 100644
--- a/apps/server/src/project/Layers/RepositoryIdentityResolver.test.ts
+++ b/apps/server/src/project/Layers/RepositoryIdentityResolver.test.ts
@@ -1,15 +1,13 @@
-// @effect-diagnostics nodeBuiltinImport:off
-import { realpathSync } from "node:fs";
-
import * as NodeServices from "@effect/platform-node/NodeServices";
import { expect, it } from "@effect/vitest";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
-import * as TestClock from "effect/testing/TestClock";
+import * as Path from "effect/Path";
+import { TestClock } from "effect/testing";
-import { runProcess } from "../../processRunner.ts";
+import * as ProcessRunner from "../../processRunner.ts";
import { RepositoryIdentityResolver } from "../Services/RepositoryIdentityResolver.ts";
import {
makeRepositoryIdentityResolver,
@@ -17,11 +15,17 @@ import {
} from "./RepositoryIdentityResolver.ts";
const normalizePathSeparators = (value: string) => value.replaceAll("\\", "/");
-const normalizeResolvedPath = (value: string) =>
- normalizePathSeparators(realpathSync.native(value));
+const normalizeResolvedPath = (value: string) => normalizePathSeparators(value);
const git = (cwd: string, args: ReadonlyArray<string>) =>
- Effect.promise(() => runProcess("git", ["-C", cwd, ...args]));
+ Effect.gen(function* () {
+ const processRunner = yield* ProcessRunner.ProcessRunner;
+ return yield* processRunner.run({
+ command: "git",
+ args: ["-C", cwd, ...args],
+ shell: process.platform === "win32",
+ });
+ }).pipe(Effect.provide(ProcessRunner.layer));
const makeRepositoryIdentityResolverTestLayer = (options: {
readonly positiveCacheTtl?: Duration.Input;
@@ -33,7 +37,7 @@ const makeRepositoryIdentityResolverTestLayer = (options: {
cacheCapacity: 16,
...options,
}),
- );
+ ).pipe(Layer.provide(ProcessRunner.layer));
it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => {
it.effect("normalizes equivalent GitHub remotes into a stable repository identity", () =>
@@ -48,10 +52,13 @@ it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => {
const resolver = yield* RepositoryIdentityResolver;
const identity = yield* resolver.resolve(cwd);
+ const resolvedIdentityRoot =
+ identity?.rootPath === undefined ? "" : yield* fileSystem.realPath(identity.rootPath);
+ const resolvedCwd = yield* fileSystem.realPath(cwd);
expect(identity).not.toBeNull();
expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
- expect(normalizeResolvedPath(identity?.rootPath ?? "")).toBe(normalizeResolvedPath(cwd));
+ expect(normalizeResolvedPath(resolvedIdentityRoot)).toBe(normalizeResolvedPath(resolvedCwd));
expect(identity?.displayName).toBe("t3tools/t3code");
expect(identity?.provider).toBe("github");
expect(identity?.owner).toBe("t3tools");
@@ -62,10 +69,11 @@ it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => {
it.effect("returns the git top-level root path when resolving from a nested workspace", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
const repoRoot = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-repository-identity-nested-root-test-",
});
- const nestedWorkspace = `${repoRoot}/packages/web`;
+ const nestedWorkspace = path.join(repoRoot, "packages", "web");
yield* fileSystem.makeDirectory(nestedWorkspace, { recursive: true });
yield* git(repoRoot, ["init"]);
@@ -73,10 +81,15 @@ it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => {
const resolver = yield* RepositoryIdentityResolver;
const identity = yield* resolver.resolve(nestedWorkspace);
+ const resolvedIdentityRoot =
+ identity?.rootPath === undefined ? "" : yield* fileSystem.realPath(identity.rootPath);
+ const resolvedRepoRoot = yield* fileSystem.realPath(repoRoot);
expect(identity).not.toBeNull();
expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code");
- expect(normalizeResolvedPath(identity?.rootPath ?? "")).toBe(normalizeResolvedPath(repoRoot));
+ expect(normalizeResolvedPath(resolvedIdentityRoot)).toBe(
+ normalizeResolvedPath(resolvedRepoRoot),
+ );
}).pipe(Effect.provide(RepositoryIdentityResolverLive)),
);
diff --git a/apps/server/src/project/Layers/RepositoryIdentityResolver.ts b/apps/server/src/project/Layers/RepositoryIdentityResolver.ts
index 8e3239a2a..926c1d0c2 100644
--- a/apps/server/src/project/Layers/RepositoryIdentityResolver.ts
+++ b/apps/server/src/project/Layers/RepositoryIdentityResolver.ts
@@ -9,7 +9,7 @@ import {
normalizeGitRemoteUrl,
} from "@t3tools/shared/git";
-import { runProcess } from "../../processRunner.ts";
+import * as ProcessRunner from "../../processRunner.ts";
import {
RepositoryIdentityResolver,
type RepositoryIdentityResolverShape,
@@ -83,50 +83,63 @@ interface RepositoryIdentityResolverOptions {
readonly negativeCacheTtl?: Duration.Input;
}
-async function resolveRepositoryIdentityCacheKey(cwd: string): Promise<string> {
+const resolveRepositoryIdentityCacheKey = Effect.fn("resolveRepositoryIdentityCacheKey")(function* (
+ cwd: string,
+) {
+ const processRunner = yield* ProcessRunner.ProcessRunner;
let cacheKey = cwd;
- try {
- const topLevelResult = await runProcess("git", ["-C", cwd, "rev-parse", "--show-toplevel"], {
- allowNonZeroExit: true,
- });
- if (topLevelResult.code !== 0) {
- return cacheKey;
- }
-
- const candidate = topLevelResult.stdout.trim();
- if (candidate.length > 0) {
- cacheKey = candidate;
- }
- } catch {
+ const topLevelResult = yield* processRunner
+ .run({
+ command: "git",
+ args: ["-C", cwd, "rev-parse", "--show-toplevel"],
+ timeoutBehavior: "timedOutResult",
+ shell: process.platform === "win32",
+ })
+ .pipe(Effect.option);
+ if (topLevelResult._tag === "None" || topLevelResult.value.code !== 0) {
return cacheKey;
}
- return cacheKey;
-}
+ const candidate = topLevelResult.value.stdout.trim();
+ if (candidate.length > 0) {
+ cacheKey = candidate;
+ }
-async function resolveRepositoryIdentityFromCacheKey(
- cacheKey: string,
-): Promise<RepositoryIdentity | null> {
- try {
- const remoteResult = await runProcess("git", ["-C", cacheKey, "remote", "-v"], {
- allowNonZeroExit: true,
- });
- if (remoteResult.code !== 0) {
+ return cacheKey;
+});
+
+const resolveRepositoryIdentityFromCacheKey = Effect.fn("resolveRepositoryIdentityFromCacheKey")(
+ function* (
+ cacheKey: string,
+ ): Effect.fn.Return<RepositoryIdentity | null, never, ProcessRunner.ProcessRunner> {
+ const processRunner = yield* ProcessRunner.ProcessRunner;
+ const remoteResult = yield* processRunner
+ .run({
+ command: "git",
+ args: ["-C", cacheKey, "remote", "-v"],
+ timeoutBehavior: "timedOutResult",
+ shell: process.platform === "win32",
+ })
+ .pipe(Effect.option);
+ if (remoteResult._tag === "None" || remoteResult.value.code !== 0) {
return null;
}
- const remote = pickPrimaryRemote(parseRemoteFetchUrls(remoteResult.stdout));
+ const remote = pickPrimaryRemote(parseRemoteFetchUrls(remoteResult.value.stdout));
return remote ? buildRepositoryIdentity({ ...remote, rootPath: cacheKey }) : null;
- } catch {
- return null;
- }
-}
+ },
+);
export const makeRepositoryIdentityResolver = Effect.fn("makeRepositoryIdentityResolver")(
function* (options: RepositoryIdentityResolverOptions = {}) {
+ const processRunner = yield* ProcessRunner.ProcessRunner;
+
const repositoryIdentityCache = yield* Cache.makeWith<string, RepositoryIdentity | null>(
- (cacheKey) => Effect.promise(() => resolveRepositoryIdentityFromCacheKey(cacheKey)),
+ (cacheKey) =>
+ resolveRepositoryIdentityFromCacheKey(cacheKey).pipe(
+ Effect.provideService(ProcessRunner.ProcessRunner, processRunner),
+ ),
{
capacity: options.cacheCapacity ?? DEFAULT_REPOSITORY_IDENTITY_CACHE_CAPACITY,
timeToLive: Exit.match({
@@ -142,7 +155,9 @@ export const makeRepositoryIdentityResolver = Effect.fn("makeRepositoryIdentityR
const resolve: RepositoryIdentityResolverShape["resolve"] = Effect.fn(
"RepositoryIdentityResolver.resolve",
)(function* (cwd) {
- const cacheKey = yield* Effect.promise(() => resolveRepositoryIdentityCacheKey(cwd));
+ const cacheKey = yield* resolveRepositoryIdentityCacheKey(cwd).pipe(
+ Effect.provideService(ProcessRunner.ProcessRunner, processRunner),
+ );
return yield* Cache.get(repositoryIdentityCache, cacheKey);
});
@@ -155,4 +170,4 @@ export const makeRepositoryIdentityResolver = Effect.fn("makeRepositoryIdentityR
export const RepositoryIdentityResolverLive = Layer.effect(
RepositoryIdentityResolver,
makeRepositoryIdentityResolver(),
-);
+).pipe(Layer.provide(ProcessRunner.layer));
diff --git a/apps/server/src/provider/Layers/ProviderSessionReaper.test.ts b/apps/server/src/provider/Layers/ProviderSessionReaper.test.ts
index add225e2e..6bd90e283 100644
--- a/apps/server/src/provider/Layers/ProviderSessionReaper.test.ts
+++ b/apps/server/src/provider/Layers/ProviderSessionReaper.test.ts
@@ -202,6 +202,7 @@ describe("ProviderSessionReaper", () => {
getProjectShellById: () => Effect.die("unused"),
getFirstActiveThreadIdByProjectId: () => Effect.die("unused"),
getThreadCheckpointContext: () => Effect.die("unused"),
+ getFullThreadDiffContext: () => Effect.die("unused"),
getThreadShellById: (threadId) =>
Effect.succeed(
input.readModel.threads.find((thread) => thread.id === threadId)
diff --git a/apps/server/src/serverRuntimeStartup.test.ts b/apps/server/src/serverRuntimeStartup.test.ts
index 1da9a6307..33b9abcb9 100644
--- a/apps/server/src/serverRuntimeStartup.test.ts
+++ b/apps/server/src/serverRuntimeStartup.test.ts
@@ -98,6 +98,7 @@ it.effect("launchStartupHeartbeat does not block the caller while counts are loa
getProjectShellById: () => Effect.succeed(Option.none()),
getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
getThreadCheckpointContext: () => Effect.succeed(Option.none()),
+ getFullThreadDiffContext: () => Effect.succeed(Option.none()),
getThreadShellById: () => Effect.succeed(Option.none()),
getThreadDetailById: () => Effect.succeed(Option.none()),
}),
@@ -159,6 +160,7 @@ it.effect("resolveAutoBootstrapWelcomeTargets returns existing project and threa
getProjectShellById: () => Effect.die("unused"),
getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.some(bootstrapThreadId)),
getThreadCheckpointContext: () => Effect.succeed(Option.none()),
+ getFullThreadDiffContext: () => Effect.succeed(Option.none()),
getThreadShellById: () => Effect.die("unused"),
getThreadDetailById: () => Effect.die("unused"),
}),
@@ -200,6 +202,7 @@ it.effect("resolveAutoBootstrapWelcomeTargets creates a project and thread when
getProjectShellById: () => Effect.die("unused"),
getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
getThreadCheckpointContext: () => Effect.succeed(Option.none()),
+ getFullThreadDiffContext: () => Effect.succeed(Option.none()),
getThreadShellById: () => Effect.die("unused"),
getThreadDetailById: () => Effect.die("unused"),
}),
diff --git a/apps/server/src/sourceControl/SourceControlDiscovery.ts b/apps/server/src/sourceControl/SourceControlDiscovery.ts
index 493983964..6b00cf779 100644
--- a/apps/server/src/sourceControl/SourceControlDiscovery.ts
+++ b/apps/server/src/sourceControl/SourceControlDiscovery.ts
@@ -100,7 +100,7 @@ export const layer = Layer.effect(
cwd: config.cwd,
timeoutMs: 5_000,
maxOutputBytes: 8_000,
- truncateOutputAtMaxBytes: true,
+ appendTruncationMarker: true,
})
.pipe(
Effect.map(
diff --git a/apps/server/src/sourceControl/SourceControlProviderDiscovery.ts b/apps/server/src/sourceControl/SourceControlProviderDiscovery.ts
index 425c4f617..23bbada04 100644
--- a/apps/server/src/sourceControl/SourceControlProviderDiscovery.ts
+++ b/apps/server/src/sourceControl/SourceControlProviderDiscovery.ts
@@ -139,7 +139,7 @@ function probeCli(input: {
cwd: input.cwd,
timeoutMs: 5_000,
maxOutputBytes: 8_000,
- truncateOutputAtMaxBytes: true,
+ appendTruncationMarker: true,
})
.pipe(
Effect.map(
@@ -216,7 +216,7 @@ export function probeSourceControlProvider(input: {
allowNonZeroExit: true,
timeoutMs: 5_000,
maxOutputBytes: 8_000,
- truncateOutputAtMaxBytes: true,
+ appendTruncationMarker: true,
})
.pipe(
Effect.map(
diff --git a/apps/server/src/stream/collectUint8StreamText.ts b/apps/server/src/stream/collectUint8StreamText.ts
index 3fa6dc224..7ac553047 100644
--- a/apps/server/src/stream/collectUint8StreamText.ts
+++ b/apps/server/src/stream/collectUint8StreamText.ts
@@ -8,7 +8,7 @@ export interface CollectedUint8StreamText {
}
interface CollectState {
- readonly text: string;
+ chunks: Uint8Array[];
readonly bytes: number;
readonly truncated: boolean;
}
@@ -18,18 +18,21 @@ export const collectUint8StreamText = <E>(input: {
readonly maxBytes?: number | undefined;
readonly truncatedMarker?: string | null | undefined;
}): Effect.Effect<CollectedUint8StreamText, E> => {
- const decoder = new TextDecoder();
const maxBytes = input.maxBytes ?? Number.POSITIVE_INFINITY;
const truncatedMarker = input.truncatedMarker ?? "";
return input.stream.pipe(
Stream.runFold(
(): CollectState => ({
- text: "",
+ chunks: [],
bytes: 0,
truncated: false,
}),
(state, chunk): CollectState => {
+ /*
+ * keep draining after truncation so the child process can exit normally.
+ * its a know issue that on windows killing after the output cap can force an expensive taskkill operation and hurt performance
+ */
if (state.truncated) {
return state;
}
@@ -38,30 +41,30 @@ export const collectUint8StreamText = <E>(input: {
if (remainingBytes <= 0) {
return {
...state,
- text: `${state.text}${truncatedMarker}`,
truncated: true,
};
}
const nextChunk =
chunk.byteLength > remainingBytes ? chunk.slice(0, remainingBytes) : chunk;
- const text = `${state.text}${decoder.decode(nextChunk, { stream: true })}`;
+ state.chunks.push(nextChunk);
const bytes = state.bytes + nextChunk.byteLength;
const truncated = chunk.byteLength > remainingBytes;
return {
- text: truncated ? `${text}${truncatedMarker}` : text,
+ chunks: state.chunks,
bytes,
truncated,
};
},
),
- Effect.map(
- (state): CollectedUint8StreamText => ({
- text: state.truncated ? state.text : `${state.text}${decoder.decode()}`,
+ Effect.map((state): CollectedUint8StreamText => {
+ const text = Buffer.concat(state.chunks, state.bytes).toString("utf8");
+ return {
+ text: state.truncated && truncatedMarker.length > 0 ? `${text}${truncatedMarker}` : text,
bytes: state.bytes,
truncated: state.truncated,
- }),
- ),
+ };
+ }),
);
};
diff --git a/apps/server/src/terminal/Layers/Manager.test.ts b/apps/server/src/terminal/Layers/Manager.test.ts
index 0cca0f64e..b81d65965 100644
--- a/apps/server/src/terminal/Layers/Manager.test.ts
+++ b/apps/server/src/terminal/Layers/Manager.test.ts
@@ -6,21 +6,24 @@ import {
type TerminalOpenInput,
type TerminalRestartInput,
} from "@t3tools/contracts";
-import * as Cause from "effect/Cause";
+import * as Data from "effect/Data";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Encoding from "effect/Encoding";
import * as Exit from "effect/Exit";
import * as Fiber from "effect/Fiber";
import * as FileSystem from "effect/FileSystem";
-import * as Path from "effect/Path";
+import * as Layer from "effect/Layer";
+import * as Option from "effect/Option";
import * as PlatformError from "effect/PlatformError";
+import * as Path from "effect/Path";
import * as Ref from "effect/Ref";
import * as Schedule from "effect/Schedule";
import * as Scope from "effect/Scope";
-import * as TestClock from "effect/testing/TestClock";
+import { TestClock } from "effect/testing";
import { expect } from "vitest";
+import * as ProcessRunner from "../../processRunner.ts";
import type { TerminalManagerShape } from "../Services/Manager.ts";
import {
type PtyAdapterShape,
@@ -31,6 +34,10 @@ import {
} from "../Services/PTY.ts";
import { makeTerminalManagerWithOptions } from "./Manager.ts";
+class WaitForConditionError extends Data.TaggedError("WaitForConditionError")<{
+ readonly message: string;
+}> {}
+
class FakePtyProcess implements PtyProcess {
readonly writes: string[] = [];
readonly resizeCalls: Array<{ cols: number; rows: number }> = [];
@@ -127,11 +134,21 @@ class FakePtyAdapter implements PtyAdapterShape {
const waitFor = <E, R>(
predicate: Effect.Effect<boolean, E, R>,
timeout: Duration.Input = 800,
-): Effect.Effect<void, E | Cause.TimeoutError, R> =>
+): Effect.Effect<void, WaitForConditionError | E, R> =>
predicate.pipe(
- Effect.filter((done) => done),
+ Effect.filterOrFail(
+ (done) => done,
+ () => new WaitForConditionError({ message: "Condition not met" }),
+ ),
Effect.retry(Schedule.spaced("15 millis")),
- Effect.timeout(timeout),
+ Effect.timeoutOption(timeout),
+ Effect.flatMap((result) =>
+ Option.match(result, {
+ onNone: () =>
+ Effect.fail(new WaitForConditionError({ message: "Timed out waiting for condition" })),
+ onSome: () => Effect.void,
+ }),
+ ),
);
function openInput(overrides: Partial<TerminalOpenInput> = {}): TerminalOpenInput {
@@ -156,33 +173,27 @@ function restartInput(overrides: Partial<TerminalRestartInput> = {}): TerminalRe
};
}
-function historyLogName(threadId: string): string {
- return `terminal_${Encoding.encodeBase64Url(threadId)}.log`;
-}
-
-function multiTerminalHistoryLogName(threadId: string, terminalId: string): string {
- const threadPart = `terminal_${Encoding.encodeBase64Url(threadId)}`;
- if (terminalId === DEFAULT_TERMINAL_ID) {
- return `${threadPart}.log`;
- }
- return `${threadPart}_${Encoding.encodeBase64Url(terminalId)}.log`;
-}
-
-function historyLogPath(logsDir: string, threadId = "thread-1"): string {
- return joinPath(logsDir, historyLogName(threadId));
-}
+const historyLogPath = (logsDir: string, threadId = "thread-1") =>
+ Effect.service(Path.Path).pipe(
+ Effect.map(({ join }) => join(logsDir, `terminal_${Encoding.encodeBase64Url(threadId)}.log`)),
+ );
-function multiTerminalHistoryLogPath(
+const multiTerminalHistoryLogPath = (
logsDir: string,
threadId = "thread-1",
- terminalId = "default",
-): string {
- return joinPath(logsDir, multiTerminalHistoryLogName(threadId, terminalId));
-}
-
-function joinPath(...segments: ReadonlyArray<string>): string {
- return segments.join("/");
-}
+ terminalId = DEFAULT_TERMINAL_ID,
+) =>
+ Effect.service(Path.Path).pipe(
+ Effect.map(({ join }) => {
+ const threadPart = `terminal_${Encoding.encodeBase64Url(threadId)}`;
+ return join(
+ logsDir,
+ terminalId === DEFAULT_TERMINAL_ID
+ ? `${threadPart}.log`
+ : `${threadPart}_${Encoding.encodeBase64Url(terminalId)}.log`,
+ );
+ }),
+ );
interface CreateManagerOptions {
shellResolver?: () => string;
@@ -209,13 +220,13 @@ const createManager = (
): Effect.Effect<
ManagerFixture,
PlatformError.PlatformError,
- FileSystem.FileSystem | Path.Path | Scope.Scope
+ FileSystem.FileSystem | Path.Path | Scope.Scope | ProcessRunner.ProcessRunner
> =>
Effect.flatMap(Effect.service(FileSystem.FileSystem), (fs) =>
Effect.gen(function* () {
- const pathService = yield* Path.Path;
+ const { join } = yield* Path.Path;
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-terminal-" });
- const logsDir = pathService.join(baseDir, "userdata", "logs", "terminals");
+ const logsDir = join(baseDir, "userdata", "logs", "terminals");
const ptyAdapter = options.ptyAdapter ?? new FakePtyAdapter();
const manager = yield* makeTerminalManagerWithOptions({
@@ -246,6 +257,7 @@ const createManager = (
return {
baseDir,
logsDir,
+ join,
ptyAdapter,
manager,
getEvents: Ref.get(eventsRef),
@@ -253,7 +265,12 @@ const createManager = (
}),
);
-it.layer(NodeServices.layer, { excludeTestServices: true })("TerminalManager", (it) => {
+it.layer(
+ Layer.merge(NodeServices.layer, ProcessRunner.layer.pipe(Layer.provide(NodeServices.layer))),
+ { excludeTestServices: true },
+)("TerminalManager", (it) => {
+ const itEffectSkipOnWindows = process.platform === "win32" ? it.effect.skip : it.effect;
+
it.effect("spawns lazily and reuses running terminal per thread", () =>
Effect.gen(function* () {
const { manager, ptyAdapter } = yield* createManager();
@@ -290,13 +307,13 @@ it.layer(NodeServices.layer, { excludeTestServices: true })("TerminalManager", (
fs.writeFileString(filePath, contents),
);
- it.effect("preserves non-notFound cwd stat failures", () =>
+ itEffectSkipOnWindows("preserves non-notFound cwd stat failures", () =>
Effect.gen(function* () {
- if (process.platform === "win32") return;
+ const path = yield* Path.Path;
const { manager, baseDir } = yield* createManager();
- const blockedRoot = joinPath(baseDir, "blocked-root");
- const blockedCwd = joinPath(blockedRoot, "cwd");
+ const blockedRoot = path.join(baseDir, "blocked-root");
+ const blockedCwd = path.join(blockedRoot, "cwd");
yield* makeDirectory(blockedCwd);
yield* chmod(blockedRoot, 0o000);
@@ -390,15 +407,27 @@ it.layer(NodeServices.layer, { excludeTestServices: true })("TerminalManager", (
it.effect("clears transcript and emits cleared event", () =>
Effect.gen(function* () {
const { manager, ptyAdapter, logsDir, getEvents } = yield* createManager();
+ const path = yield* Path.Path;
yield* manager.open(openInput());
const process = ptyAdapter.processes[0];
expect(process).toBeDefined();
if (!process) return;
process.emitData("hello\n");
- yield* waitFor(pathExists(historyLogPath(logsDir)));
+ yield* waitFor(
+ historyLogPath(logsDir).pipe(
+ Effect.provideService(Path.Path, path),
+ Effect.flatMap(pathExists),
+ ),
+ );
yield* manager.clear({ threadId: "thread-1", terminalId: DEFAULT_TERMINAL_ID });
- yield* waitFor(Effect.map(readFileString(historyLogPath(logsDir)), (text) => text === ""));
+ yield* waitFor(
+ historyLogPath(logsDir).pipe(
+ Effect.provideService(Path.Path, path),
+ Effect.flatMap(readFileString),
+ Effect.map((text) => text === ""),
+ ),
+ );
const events = yield* getEvents;
expect(events.some((event) => event.type === "cleared")).toBe(true);
@@ -421,21 +450,34 @@ it.layer(NodeServices.layer, { excludeTestServices: true })("TerminalManager", (
expect(firstProcess).toBeDefined();
if (!firstProcess) return;
firstProcess.emitData("before restart\n");
- yield* waitFor(pathExists(historyLogPath(logsDir)));
+ const path = yield* Path.Path;
+ yield* waitFor(
+ historyLogPath(logsDir).pipe(
+ Effect.provideService(Path.Path, path),
+ Effect.flatMap(pathExists),
+ ),
+ );
const snapshot = yield* manager.restart(restartInput());
assert.equal(snapshot.history, "");
assert.equal(snapshot.status, "running");
expect(ptyAdapter.spawnInputs).toHaveLength(2);
- yield* waitFor(Effect.map(readFileString(historyLogPath(logsDir)), (text) => text === ""));
+ yield* waitFor(
+ historyLogPath(logsDir).pipe(
+ Effect.provideService(Path.Path, path),
+ Effect.flatMap(readFileString),
+ Effect.map((text) => text === ""),
+ ),
+ );
}),
);
it.effect("propagates explicit worktree metadata through snapshots and lifecycle events", () =>
Effect.gen(function* () {
const { manager, getEvents, baseDir } = yield* createManager();
- const firstWorktreePath = joinPath(baseDir, "worktrees", "feature-a");
- const secondWorktreePath = joinPath(baseDir, "worktrees", "feature-b");
+ const path = yield* Path.Path;
+ const firstWorktreePath = path.join(baseDir, "worktrees", "feature-a");
+ const secondWorktreePath = path.join(baseDir, "worktrees", "feature-b");
yield* makeDirectory(firstWorktreePath);
yield* makeDirectory(secondWorktreePath);
const startedSnapshot = yield* manager.open(
@@ -471,7 +513,8 @@ it.layer(NodeServices.layer, { excludeTestServices: true })("TerminalManager", (
it.effect("preserves worktree metadata when reopening an exited session", () =>
Effect.gen(function* () {
const { manager, ptyAdapter, getEvents, baseDir } = yield* createManager();
- const worktreePath = joinPath(baseDir, "worktrees", "feature-a");
+ const path = yield* Path.Path;
+ const worktreePath = path.join(baseDir, "worktrees", "feature-a");
yield* makeDirectory(worktreePath);
yield* manager.open(
@@ -513,12 +556,18 @@ it.layer(NodeServices.layer, { excludeTestServices: true })("TerminalManager", (
it.effect("emits exited event and reopens with clean transcript after exit", () =>
Effect.gen(function* () {
const { manager, ptyAdapter, logsDir, getEvents } = yield* createManager();
+ const path = yield* Path.Path;
yield* manager.open(openInput());
const process = ptyAdapter.processes[0];
expect(process).toBeDefined();
if (!process) return;
process.emitData("old data\n");
- yield* waitFor(pathExists(historyLogPath(logsDir)));
+ yield* waitFor(
+ historyLogPath(logsDir).pipe(
+ Effect.provideService(Path.Path, path),
+ Effect.flatMap(pathExists),
+ ),
+ );
process.emitExit({ exitCode: 0, signal: 0 });
yield* waitFor(
@@ -528,7 +577,12 @@ it.layer(NodeServices.layer, { excludeTestServices: true })("TerminalManager", (
assert.equal(reopened.history, "");
expect(ptyAdapter.spawnInputs).toHaveLength(2);
- expect(yield* readFileString(historyLogPath(logsDir))).toBe("");
+ expect(
+ yield* historyLogPath(logsDir).pipe(
+ Effect.provideService(Path.Path, path),
+ Effect.flatMap(readFileString),
+ ),
+ ).toBe("");
}),
);
@@ -715,10 +769,21 @@ it.layer(NodeServices.layer, { excludeTestServices: true })("TerminalManager", (
expect(process).toBeDefined();
if (!process) return;
process.emitData("bye\n");
- yield* waitFor(pathExists(historyLogPath(logsDir)));
+ const path = yield* Path.Path;
+ yield* waitFor(
+ historyLogPath(logsDir).pipe(
+ Effect.provideService(Path.Path, path),
+ Effect.flatMap(pathExists),
+ ),
+ );
yield* manager.close({ threadId: "thread-1", deleteHistory: true });
- expect(yield* pathExists(historyLogPath(logsDir))).toBe(false);
+ expect(
+ yield* historyLogPath(logsDir).pipe(
+ Effect.provideService(Path.Path, path),
+ Effect.flatMap(pathExists),
+ ),
+ ).toBe(false);
}),
);
@@ -735,19 +800,36 @@ it.layer(NodeServices.layer, { excludeTestServices: true })("TerminalManager", (
defaultProcess.emitData("default\n");
sidecarProcess.emitData("sidecar\n");
- yield* waitFor(pathExists(multiTerminalHistoryLogPath(logsDir, "thread-1", "default")));
- yield* waitFor(pathExists(multiTerminalHistoryLogPath(logsDir, "thread-1", "sidecar")));
+ const path = yield* Path.Path;
+ yield* waitFor(
+ multiTerminalHistoryLogPath(logsDir, "thread-1", "default").pipe(
+ Effect.provideService(Path.Path, path),
+ Effect.flatMap(pathExists),
+ ),
+ );
+ yield* waitFor(
+ multiTerminalHistoryLogPath(logsDir, "thread-1", "sidecar").pipe(
+ Effect.provideService(Path.Path, path),
+ Effect.flatMap(pathExists),
+ ),
+ );
yield* manager.close({ threadId: "thread-1", deleteHistory: true });
assert.equal(defaultProcess.killed, true);
assert.equal(sidecarProcess.killed, true);
- expect(yield* pathExists(multiTerminalHistoryLogPath(logsDir, "thread-1", "default"))).toBe(
- false,
- );
- expect(yield* pathExists(multiTerminalHistoryLogPath(logsDir, "thread-1", "sidecar"))).toBe(
- false,
- );
+ expect(
+ yield* multiTerminalHistoryLogPath(logsDir, "thread-1", "default").pipe(
+ Effect.provideService(Path.Path, path),
+ Effect.flatMap(pathExists),
+ ),
+ ).toBe(false);
+ expect(
+ yield* multiTerminalHistoryLogPath(logsDir, "thread-1", "sidecar").pipe(
+ Effect.provideService(Path.Path, path),
+ Effect.flatMap(pathExists),
+ ),
+ ).toBe(false);
}),
);
@@ -786,7 +868,13 @@ it.layer(NodeServices.layer, { excludeTestServices: true })("TerminalManager", (
first.emitData("first-history\n");
second.emitData("second-history\n");
- yield* waitFor(pathExists(historyLogPath(logsDir, "thread-1")));
+ const path = yield* Path.Path;
+ yield* waitFor(
+ historyLogPath(logsDir, "thread-1").pipe(
+ Effect.provideService(Path.Path, path),
+ Effect.flatMap(pathExists),
+ ),
+ );
first.emitExit({ exitCode: 0, signal: 0 });
yield* Effect.sleep(Duration.millis(5));
second.emitExit({ exitCode: 0, signal: 0 });
@@ -809,8 +897,9 @@ it.layer(NodeServices.layer, { excludeTestServices: true })("TerminalManager", (
it.effect("migrates legacy transcript filenames to terminal-scoped history path on open", () =>
Effect.gen(function* () {
const { manager, logsDir } = yield* createManager();
- const legacyPath = joinPath(logsDir, "thread-1.log");
- const nextPath = historyLogPath(logsDir);
+ const path = yield* Path.Path;
+ const legacyPath = path.join(logsDir, "thread-1.log");
+ const nextPath = yield* historyLogPath(logsDir);
yield* writeFileString(legacyPath, "legacy-line\n");
const snapshot = yield* manager.open(openInput());
diff --git a/apps/server/src/terminal/Layers/Manager.ts b/apps/server/src/terminal/Layers/Manager.ts
index 39875202d..9f20ebc83 100644
--- a/apps/server/src/terminal/Layers/Manager.ts
+++ b/apps/server/src/terminal/Layers/Manager.ts
@@ -5,13 +5,13 @@ import {
type TerminalSessionStatus,
} from "@t3tools/contracts";
import { makeKeyedCoalescingWorker } from "@t3tools/shared/KeyedCoalescingWorker";
+import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
import * as Encoding from "effect/Encoding";
import * as Equal from "effect/Equal";
import * as Exit from "effect/Exit";
import * as Fiber from "effect/Fiber";
import * as FileSystem from "effect/FileSystem";
-import * as DateTime from "effect/DateTime";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
@@ -26,7 +26,7 @@ import {
terminalRestartsTotal,
terminalSessionsTotal,
} from "../../observability/Metrics.ts";
-import { runProcess } from "../../processRunner.ts";
+import * as ProcessRunner from "../../processRunner.ts";
import {
TerminalCwdError,
TerminalHistoryError,
@@ -51,6 +51,7 @@ const DEFAULT_MAX_RETAINED_INACTIVE_SESSIONS = 128;
const DEFAULT_OPEN_COLS = 120;
const DEFAULT_OPEN_ROWS = 30;
const TERMINAL_ENV_BLOCKLIST = new Set(["PORT", "ELECTRON_RENDERER_PORT", "ELECTRON_RUN_AS_NODE"]);
+const nowIso = Effect.map(DateTime.now, DateTime.formatIso);
class TerminalSubprocessCheckError extends Schema.TaggedErrorClass<TerminalSubprocessCheckError>()(
"TerminalSubprocessCheckError",
@@ -72,7 +73,9 @@ class TerminalProcessSignalError extends Schema.TaggedErrorClass<TerminalProcess
) {}
interface TerminalSubprocessChecker {
- (terminalPid: number): Effect.Effect<boolean, TerminalSubprocessCheckError>;
+ (
+ terminalPid: number,
+ ): Effect.Effect<boolean, TerminalSubprocessCheckError, ProcessRunner.ProcessRunner>;
}
interface ShellCandidate {
@@ -211,17 +214,23 @@ function normalizeShellCommand(
return firstToken.replace(/^['"]|['"]$/g, "");
}
-function basename(command: string, platform: NodeJS.Platform): string {
- const normalized = platform === "win32" ? command.replaceAll("/", "\\") : command;
- const separator = platform === "win32" ? "\\" : "/";
- return normalized.slice(normalized.lastIndexOf(separator) + 1);
+function basenameForPlatform(command: string, platform: NodeJS.Platform): string {
+ const normalized =
+ platform === "win32" ? command.replaceAll("/", "\\") : command.replaceAll("\\", "/");
+ const parts = normalized
+ .split(platform === "win32" ? /\\+/ : /\/+/)
+ .filter((part) => part.length > 0);
+ return parts.at(-1) ?? normalized;
}
-function joinWindowsPath(...segments: ReadonlyArray<string>): string {
- return segments
- .filter((segment) => segment.length > 0)
- .join("\\")
- .replace(/\\+/g, "\\");
+function joinWindowsPath(...parts: ReadonlyArray<string>): string {
+ return parts
+ .map((part, index) => {
+ if (index === 0) return part.replace(/[\\/]+$/g, "");
+ return part.replace(/^[\\/]+|[\\/]+$/g, "");
+ })
+ .filter((part) => part.length > 0)
+ .join("\\");
}
function shellCandidateFromCommand(
@@ -229,7 +238,7 @@ function shellCandidateFromCommand(
platform: NodeJS.Platform = process.platform,
): ShellCandidate | null {
if (!command || command.length === 0) return null;
- const shellName = basename(command, platform).toLowerCase();
+ const shellName = basenameForPlatform(command, platform).toLowerCase();
if (platform === "win32" && (shellName === "pwsh.exe" || shellName === "powershell.exe")) {
return { shell: command, args: ["-NoLogo"] };
}
@@ -357,66 +366,82 @@ function isRetryableShellSpawnError(error: PtySpawnError): boolean {
function checkWindowsSubprocessActivity(
terminalPid: number,
-): Effect.Effect<boolean, TerminalSubprocessCheckError> {
+): Effect.Effect<boolean, TerminalSubprocessCheckError, ProcessRunner.ProcessRunner> {
const command = [
`$children = Get-CimInstance Win32_Process -Filter "ParentProcessId = ${terminalPid}" -ErrorAction SilentlyContinue`,
"if ($children) { exit 0 }",
"exit 1",
].join("; ");
- return Effect.tryPromise({
- try: () =>
- runProcess("powershell.exe", ["-NoProfile", "-NonInteractive", "-Command", command], {
- timeoutMs: 1_500,
- allowNonZeroExit: true,
- maxBufferBytes: 32_768,
- outputMode: "truncate",
- }),
- catch: (cause) =>
- new TerminalSubprocessCheckError({
- message: "Failed to check Windows terminal subprocess activity.",
- cause,
- terminalPid,
- command: "powershell",
- }),
- }).pipe(Effect.map((result) => result.code === 0));
+ return Effect.gen(function* () {
+ const processRunner = yield* ProcessRunner.ProcessRunner;
+ return yield* processRunner.run({
+ command: "powershell.exe",
+ args: ["-NoProfile", "-NonInteractive", "-Command", command],
+ timeout: "1500 millis",
+ maxOutputBytes: 32_768,
+ outputMode: "truncate",
+ shell: process.platform === "win32",
+ timeoutBehavior: "timedOutResult",
+ });
+ }).pipe(
+ Effect.map((result) => result.code === 0),
+ Effect.mapError(
+ (cause) =>
+ new TerminalSubprocessCheckError({
+ message: "Failed to check Windows terminal subprocess activity.",
+ cause,
+ terminalPid,
+ command: "powershell",
+ }),
+ ),
+ );
}
const checkPosixSubprocessActivity = Effect.fn("terminal.checkPosixSubprocessActivity")(function* (
terminalPid: number,
-): Effect.fn.Return<boolean, TerminalSubprocessCheckError> {
- const runPgrep = Effect.tryPromise({
- try: () =>
- runProcess("pgrep", ["-P", String(terminalPid)], {
- timeoutMs: 1_000,
- allowNonZeroExit: true,
- maxBufferBytes: 32_768,
- outputMode: "truncate",
- }),
- catch: (cause) =>
- new TerminalSubprocessCheckError({
- message: "Failed to inspect terminal subprocesses with pgrep.",
- cause,
- terminalPid,
- command: "pgrep",
- }),
- });
+): Effect.fn.Return<boolean, TerminalSubprocessCheckError, ProcessRunner.ProcessRunner> {
+ const processRunner = yield* ProcessRunner.ProcessRunner;
+ const runPgrep = processRunner
+ .run({
+ command: "pgrep",
+ args: ["-P", String(terminalPid)],
+ timeout: "1 second",
+ maxOutputBytes: 32_768,
+ outputMode: "truncate",
+ timeoutBehavior: "timedOutResult",
+ })
+ .pipe(
+ Effect.mapError(
+ (cause) =>
+ new TerminalSubprocessCheckError({
+ message: "Failed to inspect terminal subprocesses with pgrep.",
+ cause,
+ terminalPid,
+ command: "pgrep",
+ }),
+ ),
+ );
- const runPs = Effect.tryPromise({
- try: () =>
- runProcess("ps", ["-eo", "pid=,ppid="], {
- timeoutMs: 1_000,
- allowNonZeroExit: true,
- maxBufferBytes: 262_144,
- outputMode: "truncate",
- }),
- catch: (cause) =>
- new TerminalSubprocessCheckError({
- message: "Failed to inspect terminal subprocesses with ps.",
- cause,
- terminalPid,
- command: "ps",
- }),
- });
+ const runPs = processRunner
+ .run({
+ command: "ps",
+ args: ["-eo", "pid=,ppid="],
+ timeout: "1 second",
+ maxOutputBytes: 262_144,
+ outputMode: "truncate",
+ timeoutBehavior: "timedOutResult",
+ })
+ .pipe(
+ Effect.mapError(
+ (cause) =>
+ new TerminalSubprocessCheckError({
+ message: "Failed to inspect terminal subprocesses with ps.",
+ cause,
+ terminalPid,
+ command: "ps",
+ }),
+ ),
+ );
const pgrepResult = yield* Effect.exit(runPgrep);
if (pgrepResult._tag === "Success") {
@@ -447,7 +472,7 @@ const checkPosixSubprocessActivity = Effect.fn("terminal.checkPosixSubprocessAct
const defaultSubprocessChecker = Effect.fn("terminal.defaultSubprocessChecker")(function* (
terminalPid: number,
-): Effect.fn.Return<boolean, TerminalSubprocessCheckError> {
+): Effect.fn.Return<boolean, TerminalSubprocessCheckError, ProcessRunner.ProcessRunner> {
if (!Number.isInteger(terminalPid) || terminalPid <= 0) {
return false;
}
@@ -725,14 +750,19 @@ export const makeTerminalManagerWithOptions = Effect.fn("makeTerminalManagerWith
const path = yield* Path.Path;
const context = yield* Effect.context<never>();
const runFork = Effect.runForkWith(context);
- const nowIso = Effect.map(DateTime.now, DateTime.formatIso);
const logsDir = options.logsDir;
const historyLineLimit = options.historyLineLimit ?? DEFAULT_HISTORY_LINE_LIMIT;
const platform = options.platform ?? process.platform;
const baseEnv = options.env ?? process.env;
const shellResolver = options.shellResolver ?? (() => defaultShellResolver(platform, baseEnv));
- const subprocessChecker = options.subprocessChecker ?? defaultSubprocessChecker;
+ const processRunner = yield* ProcessRunner.ProcessRunner;
+ const subprocessChecker =
+ options.subprocessChecker ??
+ ((terminalPid) =>
+ defaultSubprocessChecker(terminalPid).pipe(
+ Effect.provideService(ProcessRunner.ProcessRunner, processRunner),
+ ));
const subprocessPollIntervalMs =
options.subprocessPollIntervalMs ?? DEFAULT_SUBPROCESS_POLL_INTERVAL_MS;
const processKillGraceMs = options.processKillGraceMs ?? DEFAULT_PROCESS_KILL_GRACE_MS;
@@ -1419,22 +1449,23 @@ export const makeTerminalManagerWithOptions = Effect.fn("makeTerminalManagerWith
runFork(drainProcessEvents(session, processPid));
});
- const startedAt = yield* nowIso;
+ const runningAt = yield* nowIso;
yield* modifyManagerState((state) => {
session.process = ptyProcess;
session.pid = processPid;
session.status = "running";
- session.updatedAt = startedAt;
+ session.updatedAt = runningAt;
session.unsubscribeData = unsubscribeData;
session.unsubscribeExit = unsubscribeExit;
return [undefined, state] as const;
});
+ const createdAt = yield* nowIso;
yield* publishEvent({
type: eventType,
threadId: session.threadId,
terminalId: session.terminalId,
- createdAt: startedAt,
+ createdAt,
snapshot: snapshot(session),
});
}),
@@ -1451,8 +1482,8 @@ export const makeTerminalManagerWithOptions = Effect.fn("makeTerminalManagerWith
if (ptyProcess) {
yield* startKillEscalation(ptyProcess, session.threadId, session.terminalId);
}
+ const erroredAt = yield* nowIso;
- const failedAt = yield* nowIso;
yield* modifyManagerState((state) => {
session.status = "error";
session.pid = null;
@@ -1463,18 +1494,19 @@ export const makeTerminalManagerWithOptions = Effect.fn("makeTerminalManagerWith
session.pendingProcessEvents = [];
session.pendingProcessEventIndex = 0;
session.processEventDrainRunning = false;
- session.updatedAt = failedAt;
+ session.updatedAt = erroredAt;
return [undefined, state] as const;
});
yield* evictInactiveSessionsIfNeeded();
const message = error.message;
+ const createdAt = yield* nowIso;
yield* publishEvent({
type: "error",
threadId: session.threadId,
terminalId: session.terminalId,
- createdAt: failedAt,
+ createdAt,
message,
});
yield* Effect.logError("failed to start terminal", {
@@ -1547,6 +1579,7 @@ export const makeTerminalManagerWithOptions = Effect.fn("makeTerminalManagerWith
}
const updatedAt = yield* nowIso;
+ const createdAt = yield* nowIso;
const event = yield* modifyManagerState((state) => {
const liveSession: Option.Option<TerminalSessionState> = Option.fromNullishOr(
state.sessions.get(toSessionKey(session.threadId, session.terminalId)),
@@ -1568,7 +1601,7 @@ export const makeTerminalManagerWithOptions = Effect.fn("makeTerminalManagerWith
type: "activity" as const,
threadId: liveSession.value.threadId,
terminalId: liveSession.value.terminalId,
- createdAt: updatedAt,
+ createdAt,
hasRunningSubprocess: hasRunningSubprocess.value,
}),
state,
@@ -1797,19 +1830,20 @@ export const makeTerminalManagerWithOptions = Effect.fn("makeTerminalManagerWith
Effect.gen(function* () {
const terminalId = input.terminalId ?? DEFAULT_TERMINAL_ID;
const session = yield* requireSession(input.threadId, terminalId);
+ const updatedAt = yield* nowIso;
session.history = "";
session.pendingHistoryControlSequence = "";
session.pendingProcessEvents = [];
session.pendingProcessEventIndex = 0;
session.processEventDrainRunning = false;
- const clearedAt = yield* nowIso;
- session.updatedAt = clearedAt;
+ session.updatedAt = updatedAt;
yield* persistHistory(input.threadId, terminalId, session.history);
+ const createdAt = yield* nowIso;
yield* publishEvent({
type: "cleared",
threadId: input.threadId,
terminalId,
- createdAt: clearedAt,
+ createdAt,
});
}),
);
@@ -1933,4 +1967,6 @@ export const makeTerminalManagerWithOptions = Effect.fn("makeTerminalManagerWith
},
);
-export const TerminalManagerLive = Layer.effect(TerminalManager, makeTerminalManager());
+export const TerminalManagerLive = Layer.effect(TerminalManager, makeTerminalManager()).pipe(
+ Layer.provide(ProcessRunner.layer),
+);
diff --git a/apps/server/src/vcs/GitVcsDriver.test.ts b/apps/server/src/vcs/GitVcsDriver.test.ts
index 8ec819fc8..70bb8655e 100644
--- a/apps/server/src/vcs/GitVcsDriver.test.ts
+++ b/apps/server/src/vcs/GitVcsDriver.test.ts
@@ -67,6 +67,7 @@ runVcsDriverContractSuite<GitVcsDriver.GitVcsDriver, GitContractError>({
it.effect("GitVcsDriver forwards execute env to the VCS process", () => {
let observedEnv: NodeJS.ProcessEnv | undefined;
+ let observedAppendTruncationMarker: boolean | undefined;
return Effect.gen(function* () {
const driver = yield* GitVcsDriver.makeVcsDriverShape();
@@ -78,26 +79,32 @@ it.effect("GitVcsDriver forwards execute env to the VCS process", () => {
env: {
GIT_INDEX_FILE: "/tmp/t3-index",
},
+ appendTruncationMarker: true,
});
assert.deepStrictEqual(observedEnv, {
GIT_INDEX_FILE: "/tmp/t3-index",
});
+ assert.strictEqual(observedAppendTruncationMarker, true);
}).pipe(
Effect.provide(
- Layer.mock(VcsProcess.VcsProcess)({
- run: (input) =>
- Effect.sync(() => {
- observedEnv = input.env;
- return {
- exitCode: ChildProcessSpawner.ExitCode(0),
- stdout: "",
- stderr: "",
- stdoutTruncated: false,
- stderrTruncated: false,
- };
- }),
- }),
+ Layer.mergeAll(
+ NodeServices.layer,
+ Layer.mock(VcsProcess.VcsProcess)({
+ run: (input) =>
+ Effect.sync(() => {
+ observedEnv = input.env;
+ observedAppendTruncationMarker = input.appendTruncationMarker;
+ return {
+ exitCode: ChildProcessSpawner.ExitCode(0),
+ stdout: "",
+ stderr: "",
+ stdoutTruncated: false,
+ stderrTruncated: false,
+ };
+ }),
+ }),
+ ),
),
);
});
diff --git a/apps/server/src/vcs/GitVcsDriver.ts b/apps/server/src/vcs/GitVcsDriver.ts
index 2bd2d5c00..c07aeaa09 100644
--- a/apps/server/src/vcs/GitVcsDriver.ts
+++ b/apps/server/src/vcs/GitVcsDriver.ts
@@ -1,8 +1,12 @@
+import { randomUUID } from "node:crypto";
+
import * as Context from "effect/Context";
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
+import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
+import * as Path from "effect/Path";
import { ChildProcessSpawner } from "effect/unstable/process";
import {
@@ -35,7 +39,7 @@ export interface ExecuteGitInput {
readonly allowNonZeroExit?: boolean;
readonly timeoutMs?: number;
readonly maxOutputBytes?: number;
- readonly truncateOutputAtMaxBytes?: boolean;
+ readonly appendTruncationMarker?: boolean;
readonly progress?: ExecuteGitProgress;
}
@@ -218,6 +222,7 @@ export class GitVcsDriver extends Context.Service<GitVcsDriver, GitVcsDriverShap
const WORKSPACE_FILES_MAX_OUTPUT_BYTES = 16 * 1024 * 1024;
const GIT_CHECK_IGNORE_MAX_STDIN_BYTES = 256 * 1024;
+const CHECKPOINT_DIFF_MAX_OUTPUT_BYTES = 10_000_000;
const WORKSPACE_GIT_HARDENED_CONFIG_ARGS = [
"-c",
"core.fsmonitor=false",
@@ -318,14 +323,15 @@ const gitCommand = (
readonly allowNonZeroExit?: boolean;
readonly timeoutMs?: number;
readonly maxOutputBytes?: number;
- readonly truncateOutputAtMaxBytes?: boolean;
+ readonly appendTruncationMarker?: boolean;
},
) =>
process.run({
operation,
command: "git",
- args,
+ args: ["-C", cwd, ...args],
cwd,
+ spawnCwd: globalThis.process.cwd(),
...(options?.stdin !== undefined ? { stdin: options.stdin } : {}),
...(options?.env !== undefined ? { env: options.env } : {}),
...(options?.allowNonZeroExit !== undefined
@@ -333,13 +339,15 @@ const gitCommand = (
: {}),
...(options?.timeoutMs !== undefined ? { timeoutMs: options.timeoutMs } : {}),
...(options?.maxOutputBytes !== undefined ? { maxOutputBytes: options.maxOutputBytes } : {}),
- ...(options?.truncateOutputAtMaxBytes !== undefined
- ? { truncateOutputAtMaxBytes: options.truncateOutputAtMaxBytes }
+ ...(options?.appendTruncationMarker !== undefined
+ ? { appendTruncationMarker: options.appendTruncationMarker }
: {}),
});
export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* () {
- const process = yield* VcsProcess.VcsProcess;
+ const fileSystem = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const vcsProcess = yield* VcsProcess.VcsProcess;
const capabilities = {
kind: "git" as const,
supportsWorktrees: true,
@@ -351,7 +359,7 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
const isInsideWorkTree: VcsDriver.VcsDriverShape["isInsideWorkTree"] = (cwd) =>
gitCommand(
- process,
+ vcsProcess,
"GitVcsDriver.isInsideWorkTree",
cwd,
["rev-parse", "--is-inside-work-tree"],
@@ -363,14 +371,14 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
).pipe(Effect.map((result) => result.exitCode === 0 && result.stdout.trim() === "true"));
const execute: VcsDriver.VcsDriverShape["execute"] = (input) =>
- gitCommand(process, input.operation, input.cwd, input.args, {
+ gitCommand(vcsProcess, input.operation, input.cwd, input.args, {
...(input.stdin !== undefined ? { stdin: input.stdin } : {}),
...(input.env !== undefined ? { env: input.env } : {}),
...(input.allowNonZeroExit !== undefined ? { allowNonZeroExit: input.allowNonZeroExit } : {}),
...(input.timeoutMs !== undefined ? { timeoutMs: input.timeoutMs } : {}),
...(input.maxOutputBytes !== undefined ? { maxOutputBytes: input.maxOutputBytes } : {}),
- ...(input.truncateOutputAtMaxBytes !== undefined
- ? { truncateOutputAtMaxBytes: input.truncateOutputAtMaxBytes }
+ ...(input.appendTruncationMarker !== undefined
+ ? { appendTruncationMarker: input.appendTruncationMarker }
: {}),
});
@@ -381,12 +389,12 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
return null;
}
- const root = yield* gitCommand(process, "GitVcsDriver.detectRepository.root", cwd, [
+ const root = yield* gitCommand(vcsProcess, "GitVcsDriver.detectRepository.root", cwd, [
"rev-parse",
"--show-toplevel",
]);
const gitCommonDir = yield* gitCommand(
- process,
+ vcsProcess,
"GitVcsDriver.detectRepository.commonDir",
cwd,
["rev-parse", "--git-common-dir"],
@@ -402,7 +410,7 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
const listWorkspaceFiles: VcsDriver.VcsDriverShape["listWorkspaceFiles"] = (cwd) =>
gitCommand(
- process,
+ vcsProcess,
"GitVcsDriver.listWorkspaceFiles",
cwd,
[
@@ -417,7 +425,7 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
allowNonZeroExit: true,
timeoutMs: 20_000,
maxOutputBytes: WORKSPACE_FILES_MAX_OUTPUT_BYTES,
- truncateOutputAtMaxBytes: true,
+ appendTruncationMarker: true,
},
).pipe(
Effect.flatMap((result) =>
@@ -444,11 +452,17 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
const listRemotes: VcsDriver.VcsDriverShape["listRemotes"] = Effect.fn("listRemotes")(
function* (cwd) {
- const result = yield* gitCommand(process, "GitVcsDriver.listRemotes", cwd, ["remote", "-v"], {
- allowNonZeroExit: true,
- timeoutMs: 5_000,
- maxOutputBytes: 64 * 1024,
- });
+ const result = yield* gitCommand(
+ vcsProcess,
+ "GitVcsDriver.listRemotes",
+ cwd,
+ ["remote", "-v"],
+ {
+ allowNonZeroExit: true,
+ timeoutMs: 5_000,
+ maxOutputBytes: 64 * 1024,
+ },
+ );
if (result.exitCode !== 0) {
return yield* new VcsProcessExitError({
@@ -494,7 +508,7 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
for (const chunk of chunks) {
const result = yield* gitCommand(
- process,
+ vcsProcess,
"GitVcsDriver.filterIgnoredPaths",
cwd,
[...WORKSPACE_GIT_HARDENED_CONFIG_ARGS, "check-ignore", "--no-index", "-z", "--stdin"],
@@ -503,7 +517,7 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
allowNonZeroExit: true,
timeoutMs: 20_000,
maxOutputBytes: WORKSPACE_FILES_MAX_OUTPUT_BYTES,
- truncateOutputAtMaxBytes: true,
+ appendTruncationMarker: true,
},
);
@@ -530,14 +544,266 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
});
const initRepository: VcsDriver.VcsDriverShape["initRepository"] = (input) =>
- gitCommand(process, "GitVcsDriver.initRepository", input.cwd, ["init"], {
+ gitCommand(vcsProcess, "GitVcsDriver.initRepository", input.cwd, ["init"], {
timeoutMs: 10_000,
maxOutputBytes: 64 * 1024,
}).pipe(Effect.asVoid);
+ const resolveHeadCommit = (cwd: string) =>
+ execute({
+ operation: "GitVcsDriver.checkpoints.resolveHeadCommit",
+ cwd,
+ args: ["rev-parse", "--verify", "--quiet", "HEAD^{commit}"],
+ allowNonZeroExit: true,
+ }).pipe(
+ Effect.map((result) => {
+ if (result.exitCode !== 0) {
+ return null;
+ }
+ const commit = result.stdout.trim();
+ return commit.length > 0 ? commit : null;
+ }),
+ );
+
+ const hasHeadCommit = (cwd: string) =>
+ execute({
+ operation: "GitVcsDriver.checkpoints.hasHeadCommit",
+ cwd,
+ args: ["rev-parse", "--verify", "HEAD"],
+ allowNonZeroExit: true,
+ }).pipe(Effect.map((result) => result.exitCode === 0));
+
+ const resolveCheckpointCommit = (cwd: string, checkpointRef: string) =>
+ execute({
+ operation: "GitVcsDriver.checkpoints.resolveCheckpointCommit",
+ cwd,
+ args: ["rev-parse", "--verify", "--quiet", `${checkpointRef}^{commit}`],
+ allowNonZeroExit: true,
+ }).pipe(
+ Effect.map((result) => {
+ if (result.exitCode !== 0) {
+ return null;
+ }
+ const commit = result.stdout.trim();
+ return commit.length > 0 ? commit : null;
+ }),
+ );
+
+ const resolveGitCommonDir = (cwd: string) =>
+ Effect.gen(function* () {
+ const result = yield* execute({
+ operation: "GitVcsDriver.checkpoints.resolveGitCommonDir",
+ cwd,
+ args: ["rev-parse", "--git-common-dir"],
+ });
+ const gitCommonDir = result.stdout.trim();
+ return path.isAbsolute(gitCommonDir) ? gitCommonDir : path.resolve(cwd, gitCommonDir);
+ });
+
+ const checkpoints: VcsDriver.VcsCheckpointOps = {
+ captureCheckpoint: Effect.fn("GitVcsDriver.checkpoints.captureCheckpoint")(function* (input) {
+ const operation = "GitVcsDriver.checkpoints.captureCheckpoint";
+ const gitCommonDir = yield* resolveGitCommonDir(input.cwd);
+ const tempIndexPath = path.join(gitCommonDir, `t3-checkpoint-index-${randomUUID()}`);
+ const commitEnv: NodeJS.ProcessEnv = {
+ ...process.env,
+ GIT_INDEX_FILE: tempIndexPath,
+ GIT_AUTHOR_NAME: "T3 Code",
+ GIT_AUTHOR_EMAIL: "[email protected]",
+ GIT_COMMITTER_NAME: "T3 Code",
+ GIT_COMMITTER_EMAIL: "[email protected]",
+ };
+
+ const cleanupTempIndex = fileSystem
+ .remove(tempIndexPath, { force: true })
+ .pipe(Effect.ignore);
+
+ yield* Effect.gen(function* () {
+ const headExists = yield* hasHeadCommit(input.cwd);
+ if (headExists) {
+ yield* execute({
+ operation,
+ cwd: input.cwd,
+ args: ["read-tree", "HEAD"],
+ env: commitEnv,
+ });
+ }
+
+ yield* execute({
+ operation,
+ cwd: input.cwd,
+ args: ["add", "-A", "--", "."],
+ env: commitEnv,
+ });
+
+ const writeTreeResult = yield* execute({
+ operation,
+ cwd: input.cwd,
+ args: ["write-tree"],
+ env: commitEnv,
+ });
+ const treeOid = writeTreeResult.stdout.trim();
+ if (treeOid.length === 0) {
+ return yield* new VcsProcessExitError({
+ operation,
+ command: "git write-tree",
+ cwd: input.cwd,
+ exitCode: 0,
+ detail: "git write-tree returned an empty tree oid.",
+ });
+ }
+
+ const message = `t3 checkpoint ref=${input.checkpointRef}`;
+ const commitTreeResult = yield* execute({
+ operation,
+ cwd: input.cwd,
+ args: ["commit-tree", treeOid, "-m", message],
+ env: commitEnv,
+ });
+ const commitOid = commitTreeResult.stdout.trim();
+ if (commitOid.length === 0) {
+ return yield* new VcsProcessExitError({
+ operation,
+ command: "git commit-tree",
+ cwd: input.cwd,
+ exitCode: 0,
+ detail: "git commit-tree returned an empty commit oid.",
+ });
+ }
+
+ yield* execute({
+ operation,
+ cwd: input.cwd,
+ args: ["update-ref", input.checkpointRef, commitOid],
+ });
+ }).pipe(Effect.ensuring(cleanupTempIndex));
+ }),
+
+ hasCheckpointRef: (input) =>
+ resolveCheckpointCommit(input.cwd, input.checkpointRef).pipe(
+ Effect.map((commit) => commit !== null),
+ ),
+
+ restoreCheckpoint: Effect.fn("GitVcsDriver.checkpoints.restoreCheckpoint")(function* (input) {
+ const operation = "GitVcsDriver.checkpoints.restoreCheckpoint";
+
+ let commitOid = yield* resolveCheckpointCommit(input.cwd, input.checkpointRef);
+
+ if (!commitOid && input.fallbackToHead === true) {
+ commitOid = yield* resolveHeadCommit(input.cwd);
+ }
+
+ if (!commitOid) {
+ return false;
+ }
+
+ yield* execute({
+ operation,
+ cwd: input.cwd,
+ args: ["restore", "--source", commitOid, "--worktree", "--staged", "--", "."],
+ });
+ yield* execute({
+ operation,
+ cwd: input.cwd,
+ args: ["clean", "-fd", "--", "."],
+ });
+
+ const headExists = yield* hasHeadCommit(input.cwd);
+ if (headExists) {
+ yield* execute({
+ operation,
+ cwd: input.cwd,
+ args: ["reset", "--quiet", "--", "."],
+ });
+ }
+
+ return true;
+ }),
+
+ diffCheckpoints: Effect.fn("GitVcsDriver.checkpoints.diffCheckpoints")(function* (input) {
+ const operation = "GitVcsDriver.checkpoints.diffCheckpoints";
+ yield* Effect.annotateCurrentSpan({
+ "checkpoint.cwd": input.cwd,
+ "checkpoint.from_ref": input.fromCheckpointRef,
+ "checkpoint.to_ref": input.toCheckpointRef,
+ "checkpoint.ignore_whitespace": input.ignoreWhitespace,
+ "checkpoint.fallback_from_to_head": input.fallbackFromToHead,
+ });
+
+ let fromRevision: string = input.fromCheckpointRef;
+ if (input.fallbackFromToHead === true) {
+ const resolvedFromCommit = yield* resolveCheckpointCommit(
+ input.cwd,
+ input.fromCheckpointRef,
+ );
+ if (resolvedFromCommit) {
+ fromRevision = resolvedFromCommit;
+ } else {
+ const headCommit = yield* resolveHeadCommit(input.cwd);
+ if (!headCommit) {
+ return yield* new VcsProcessExitError({
+ operation,
+ command: "git diff",
+ cwd: input.cwd,
+ exitCode: 1,
+ detail: "Checkpoint ref is unavailable for diff operation.",
+ });
+ }
+ fromRevision = headCommit;
+ }
+ }
+
+ const result = yield* execute({
+ operation,
+ cwd: input.cwd,
+ args: [
+ "diff",
+ "--patch",
+ "--no-color",
+ "--no-ext-diff",
+ "--no-textconv",
+ ...(input.ignoreWhitespace ? ["--ignore-all-space"] : []),
+ `${fromRevision}^{commit}`,
+ `${input.toCheckpointRef}^{commit}`,
+ ],
+ allowNonZeroExit: true,
+ maxOutputBytes: CHECKPOINT_DIFF_MAX_OUTPUT_BYTES,
+ });
+
+ if (result.exitCode !== 0) {
+ return yield* new VcsProcessExitError({
+ operation,
+ command: "git diff",
+ cwd: input.cwd,
+ exitCode: result.exitCode,
+ detail: result.stderr.trim() || "Checkpoint ref is unavailable for diff operation.",
+ });
+ }
+
+ return result.stdout;
+ }),
+
+ deleteCheckpointRefs: Effect.fn("GitVcsDriver.checkpoints.deleteCheckpointRefs")(
+ function* (input) {
+ yield* Effect.forEach(
+ input.checkpointRefs,
+ (checkpointRef) =>
+ execute({
+ operation: "GitVcsDriver.checkpoints.deleteCheckpointRefs",
+ cwd: input.cwd,
+ args: ["update-ref", "-d", checkpointRef],
+ allowNonZeroExit: true,
+ }),
+ { discard: true },
+ );
+ },
+ ),
+ };
+
return VcsDriver.VcsDriver.of({
capabilities,
execute,
+ checkpoints,
detectRepository,
isInsideWorkTree,
listWorkspaceFiles,
diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts
index 3a05dea63..19f12862d 100644
--- a/apps/server/src/vcs/GitVcsDriverCore.ts
+++ b/apps/server/src/vcs/GitVcsDriverCore.ts
@@ -77,7 +77,7 @@ interface ExecuteGitOptions {
fallbackErrorMessage?: string | undefined;
env?: NodeJS.ProcessEnv | undefined;
maxOutputBytes?: number | undefined;
- truncateOutputAtMaxBytes?: boolean | undefined;
+ appendTruncationMarker?: boolean | undefined;
progress?: GitVcsDriver.ExecuteGitProgress | undefined;
}
@@ -535,7 +535,7 @@ const collectOutput = Effect.fnUntraced(function* <E>(
input: Pick<GitVcsDriver.ExecuteGitInput, "operation" | "cwd" | "args">,
stream: Stream.Stream<Uint8Array, E>,
maxOutputBytes: number,
- truncateOutputAtMaxBytes: boolean,
+ appendTruncationMarker: boolean,
onLine: ((line: string) => Effect.Effect<void, never>) | undefined,
): Effect.fn.Return<{ readonly text: string; readonly truncated: boolean }, GitCommandError> {
const decoder = new TextDecoder();
@@ -565,11 +565,11 @@ const collectOutput = Effect.fnUntraced(function* <E>(
});
const processChunk = Effect.fnUntraced(function* (chunk: Uint8Array) {
- if (truncateOutputAtMaxBytes && truncated) {
+ if (appendTruncationMarker && truncated) {
return;
}
const nextBytes = bytes + chunk.byteLength;
- if (!truncateOutputAtMaxBytes && nextBytes > maxOutputBytes) {
+ if (!appendTruncationMarker && nextBytes > maxOutputBytes) {
return yield* new GitCommandError({
operation: input.operation,
command: quoteGitCommand(input.args),
@@ -579,11 +579,11 @@ const collectOutput = Effect.fnUntraced(function* <E>(
}
const chunkToDecode =
- truncateOutputAtMaxBytes && nextBytes > maxOutputBytes
+ appendTruncationMarker && nextBytes > maxOutputBytes
? chunk.subarray(0, Math.max(0, maxOutputBytes - bytes))
: chunk;
bytes += chunkToDecode.byteLength;
- truncated = truncateOutputAtMaxBytes && nextBytes > maxOutputBytes;
+ truncated = appendTruncationMarker && nextBytes > maxOutputBytes;
const decoded = decoder.decode(chunkToDecode, { stream: !truncated });
text += decoded;
@@ -619,7 +619,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
} as const;
const timeoutMs = input.timeoutMs ?? DEFAULT_TIMEOUT_MS;
const maxOutputBytes = input.maxOutputBytes ?? DEFAULT_MAX_OUTPUT_BYTES;
- const truncateOutputAtMaxBytes = input.truncateOutputAtMaxBytes ?? false;
+ const appendTruncationMarker = input.appendTruncationMarker ?? false;
const runGitCommand = Effect.fn("runGitCommand")(function* () {
const trace2Monitor = yield* createTrace2Monitor(commandInput, input.progress).pipe(
@@ -646,14 +646,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
commandInput,
child.stdout,
maxOutputBytes,
- truncateOutputAtMaxBytes,
+ appendTruncationMarker,
input.progress?.onStdoutLine,
),
collectOutput(
commandInput,
child.stderr,
maxOutputBytes,
- truncateOutputAtMaxBytes,
+ appendTruncationMarker,
input.progress?.onStderrLine,
),
child.exitCode.pipe(
@@ -746,8 +746,8 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
allowNonZeroExit: true,
...(options.timeoutMs !== undefined ? { timeoutMs: options.timeoutMs } : {}),
...(options.maxOutputBytes !== undefined ? { maxOutputBytes: options.maxOutputBytes } : {}),
- ...(options.truncateOutputAtMaxBytes !== undefined
- ? { truncateOutputAtMaxBytes: options.truncateOutputAtMaxBytes }
+ ...(options.appendTruncationMarker !== undefined
+ ? { appendTruncationMarker: options.appendTruncationMarker }
: {}),
...(options.progress ? { progress: options.progress } : {}),
}).pipe(
@@ -1379,7 +1379,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
["diff", "--cached", "--patch", "--minimal"],
{
maxOutputBytes: PREPARED_COMMIT_PATCH_MAX_OUTPUT_BYTES,
- truncateOutputAtMaxBytes: true,
+ appendTruncationMarker: true,
},
);
@@ -1598,7 +1598,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
["log", "--oneline", range],
{
maxOutputBytes: RANGE_COMMIT_SUMMARY_MAX_OUTPUT_BYTES,
- truncateOutputAtMaxBytes: true,
+ appendTruncationMarker: true,
},
),
runGitStdoutWithOptions(
@@ -1607,7 +1607,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
["diff", "--stat", range],
{
maxOutputBytes: RANGE_DIFF_SUMMARY_MAX_OUTPUT_BYTES,
- truncateOutputAtMaxBytes: true,
+ appendTruncationMarker: true,
},
),
runGitStdoutWithOptions(
@@ -1616,7 +1616,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
["diff", "--patch", "--minimal", range],
{
maxOutputBytes: RANGE_DIFF_PATCH_MAX_OUTPUT_BYTES,
- truncateOutputAtMaxBytes: true,
+ appendTruncationMarker: true,
},
),
],
diff --git a/apps/server/src/vcs/VcsDriver.ts b/apps/server/src/vcs/VcsDriver.ts
index 3bc524ca0..9e6a6a402 100644
--- a/apps/server/src/vcs/VcsDriver.ts
+++ b/apps/server/src/vcs/VcsDriver.ts
@@ -9,13 +9,53 @@ import type {
VcsListWorkspaceFilesResult,
VcsRepositoryIdentity,
} from "@t3tools/contracts";
+import { CheckpointRef } from "@t3tools/contracts";
import * as VcsProcess from "./VcsProcess.ts";
+export interface VcsCaptureCheckpointInput {
+ readonly cwd: string;
+ readonly checkpointRef: CheckpointRef;
+}
+
+export interface VcsRestoreCheckpointInput {
+ readonly cwd: string;
+ readonly checkpointRef: CheckpointRef;
+ readonly fallbackToHead?: boolean;
+}
+
+export interface VcsDiffCheckpointsInput {
+ readonly cwd: string;
+ readonly fromCheckpointRef: CheckpointRef;
+ readonly toCheckpointRef: CheckpointRef;
+ readonly fallbackFromToHead?: boolean;
+ readonly ignoreWhitespace: boolean;
+}
+
+export interface VcsDeleteCheckpointRefsInput {
+ readonly cwd: string;
+ readonly checkpointRefs: ReadonlyArray<CheckpointRef>;
+}
+
+export interface VcsCheckpointOps {
+ readonly captureCheckpoint: (input: VcsCaptureCheckpointInput) => Effect.Effect<void, VcsError>;
+ readonly hasCheckpointRef: (
+ input: Omit<VcsRestoreCheckpointInput, "fallbackToHead">,
+ ) => Effect.Effect<boolean, VcsError>;
+ readonly restoreCheckpoint: (
+ input: VcsRestoreCheckpointInput,
+ ) => Effect.Effect<boolean, VcsError>;
+ readonly diffCheckpoints: (input: VcsDiffCheckpointsInput) => Effect.Effect<string, VcsError>;
+ readonly deleteCheckpointRefs: (
+ input: VcsDeleteCheckpointRefsInput,
+ ) => Effect.Effect<void, VcsError>;
+}
+
export interface VcsDriverShape {
readonly capabilities: VcsDriverCapabilities;
readonly execute: (
input: Omit<VcsProcess.VcsProcessInput, "command">,
) => Effect.Effect<VcsProcess.VcsProcessOutput, VcsError>;
+ readonly checkpoints?: VcsCheckpointOps;
readonly detectRepository: (cwd: string) => Effect.Effect<VcsRepositoryIdentity | null, VcsError>;
readonly isInsideWorkTree: (cwd: string) => Effect.Effect<boolean, VcsError>;
readonly listWorkspaceFiles: (
diff --git a/apps/server/src/vcs/VcsDriverRegistry.test.ts b/apps/server/src/vcs/VcsDriverRegistry.test.ts
index de5b95ffe..03c09c16b 100644
--- a/apps/server/src/vcs/VcsDriverRegistry.test.ts
+++ b/apps/server/src/vcs/VcsDriverRegistry.test.ts
@@ -1,4 +1,5 @@
import { assert, it, describe } from "@effect/vitest";
+import * as NodeServices from "@effect/platform-node/NodeServices";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import { ChildProcessSpawner } from "effect/unstable/process";
@@ -15,9 +16,13 @@ const processOutput = (stdout: string): VcsProcess.VcsProcessOutput => ({
stderrTruncated: false,
});
+const normalizeGitArgs = (args: ReadonlyArray<string>): ReadonlyArray<string> =>
+ args[0] === "-C" && args.length >= 2 ? args.slice(2) : args;
+
describe("VcsDriverRegistry", () => {
it.effect("routes directly by VCS driver kind for non-repository workflows", () => {
const layer = Layer.effect(VcsDriverRegistry.VcsDriverRegistry, VcsDriverRegistry.make()).pipe(
+ Layer.provide(NodeServices.layer),
Layer.provide(
Layer.mock(VcsProjectConfig.VcsProjectConfig)({
resolveKind: (input) => Effect.succeed(input.requestedKind ?? "auto"),
@@ -41,6 +46,7 @@ describe("VcsDriverRegistry", () => {
it.effect("caches repository detection for repeated resolves in the same cwd and kind", () => {
const calls: VcsProcess.VcsProcessInput[] = [];
const layer = Layer.effect(VcsDriverRegistry.VcsDriverRegistry, VcsDriverRegistry.make()).pipe(
+ Layer.provide(NodeServices.layer),
Layer.provide(
Layer.mock(VcsProjectConfig.VcsProjectConfig)({
resolveKind: (input) => Effect.succeed(input.requestedKind ?? "auto"),
@@ -51,7 +57,9 @@ describe("VcsDriverRegistry", () => {
run: (input) =>
Effect.sync(() => {
calls.push(input);
- const command = input.args.join(" ");
+ const normalizedArgs =
+ input.args[0] === "-C" && input.args.length >= 2 ? input.args.slice(2) : input.args;
+ const command = normalizedArgs.join(" ");
if (command === "rev-parse --is-inside-work-tree") {
return processOutput("true\n");
}
@@ -75,7 +83,7 @@ describe("VcsDriverRegistry", () => {
assert.equal(first.repository.rootPath, "/repo");
assert.equal(second.repository.rootPath, "/repo");
assert.deepStrictEqual(
- calls.map((call) => call.args.join(" ")),
+ calls.map((call) => normalizeGitArgs(call.args).join(" ")),
[
"rev-parse --is-inside-work-tree",
"rev-parse --show-toplevel",
diff --git a/apps/server/src/vcs/VcsProcess.test.ts b/apps/server/src/vcs/VcsProcess.test.ts
new file mode 100644
index 000000000..b58d64e43
--- /dev/null
+++ b/apps/server/src/vcs/VcsProcess.test.ts
@@ -0,0 +1,138 @@
+import * as NodeServices from "@effect/platform-node/NodeServices";
+import { describe, expect, it } from "@effect/vitest";
+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 { TestClock } from "effect/testing";
+
+import { VcsProcessExitError, VcsProcessTimeoutError } from "@t3tools/contracts";
+import * as VcsProcess from "./VcsProcess.ts";
+
+const run = (input: VcsProcess.VcsProcessInput) =>
+ Effect.gen(function* () {
+ const process = yield* VcsProcess.VcsProcess;
+ return yield* process.run(input);
+ });
+
+const liveLayer = VcsProcess.layer.pipe(Layer.provide(NodeServices.layer));
+
+const provideLive = <A, E, R>(effect: Effect.Effect<A, E, R | VcsProcess.VcsProcess>) =>
+ effect.pipe(Effect.provide(liveLayer));
+
+describe("VcsProcess.run", () => {
+ it.effect("collects stdout", () =>
+ Effect.gen(function* () {
+ const result = yield* run({
+ operation: "test.stdout",
+ command: "node",
+ args: ["-e", "process.stdout.write('hello')"],
+ cwd: process.cwd(),
+ });
+
+ expect(result.stdout).toBe("hello");
+ expect(result.stderr).toBe("");
+ expect(result.stdoutTruncated).toBe(false);
+ expect(result.stderrTruncated).toBe(false);
+ }).pipe(provideLive),
+ );
+
+ it.effect("writes stdin before waiting for exit", () =>
+ Effect.gen(function* () {
+ const result = yield* run({
+ operation: "test.stdin",
+ command: "node",
+ args: [
+ "-e",
+ [
+ "process.stdin.setEncoding('utf8');",
+ "let data='';",
+ "process.stdin.on('data', chunk => { data += chunk; });",
+ "process.stdin.on('end', () => { process.stdout.write(data); });",
+ ].join(""),
+ ],
+ cwd: process.cwd(),
+ stdin: "stdin payload",
+ });
+
+ expect(result.stdout).toBe("stdin payload");
+ }).pipe(provideLive),
+ );
+
+ it.effect("fails with VcsProcessExitError for non-zero exits by default", () =>
+ Effect.gen(function* () {
+ const error = yield* run({
+ operation: "test.exit",
+ command: "node",
+ args: ["-e", "process.stderr.write('boom'); process.exit(2)"],
+ cwd: process.cwd(),
+ }).pipe(Effect.flip);
+
+ expect(error).toBeInstanceOf(VcsProcessExitError);
+ }).pipe(provideLive),
+ );
+
+ it.effect("returns output when non-zero exits are allowed", () =>
+ Effect.gen(function* () {
+ const result = yield* run({
+ operation: "test.allowed-exit",
+ command: "node",
+ args: ["-e", "process.stderr.write('boom'); process.exit(2)"],
+ cwd: process.cwd(),
+ allowNonZeroExit: true,
+ });
+
+ expect(result.exitCode).toBe(2);
+ expect(result.stderr).toBe("boom");
+ }).pipe(provideLive),
+ );
+
+ it.effect("truncates output and appends the marker when requested", () =>
+ Effect.gen(function* () {
+ const result = yield* run({
+ operation: "test.truncate-marker",
+ command: "node",
+ args: ["-e", "process.stdout.write('x'.repeat(2048))"],
+ cwd: process.cwd(),
+ maxOutputBytes: 128,
+ appendTruncationMarker: true,
+ });
+
+ expect(result.stdoutTruncated).toBe(true);
+ expect(result.stdout).toContain("[truncated]");
+ expect(result.stderrTruncated).toBe(false);
+ }).pipe(provideLive),
+ );
+
+ it.effect("truncates without the marker when truncation markers are disabled", () =>
+ Effect.gen(function* () {
+ const result = yield* run({
+ operation: "test.truncate-silent",
+ command: "node",
+ args: ["-e", "process.stdout.write('x'.repeat(2048))"],
+ cwd: process.cwd(),
+ maxOutputBytes: 128,
+ });
+
+ expect(result.stdoutTruncated).toBe(true);
+ expect(result.stdout).not.toContain("[truncated]");
+ }).pipe(provideLive),
+ );
+
+ it.effect("fails with VcsProcessTimeoutError on timeout", () =>
+ Effect.gen(function* () {
+ const errorFiber = yield* run({
+ operation: "test.timeout",
+ command: "node",
+ args: ["-e", "setTimeout(() => {}, 5000)"],
+ cwd: process.cwd(),
+ timeoutMs: 50,
+ }).pipe(Effect.flip, Effect.forkScoped);
+ yield* Effect.yieldNow;
+ yield* TestClock.adjust(Duration.millis(50));
+ const error = yield* Fiber.join(errorFiber);
+
+ expect(error).toBeInstanceOf(VcsProcessTimeoutError);
+ }).pipe(provideLive),
+ );
+});
diff --git a/apps/server/src/vcs/VcsProcess.ts b/apps/server/src/vcs/VcsProcess.ts
index 7e80b9619..a4caf7d32 100644
--- a/apps/server/src/vcs/VcsProcess.ts
+++ b/apps/server/src/vcs/VcsProcess.ts
@@ -1,12 +1,7 @@
-import * as Duration from "effect/Duration";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
-import * as Option from "effect/Option";
-import * as PlatformError from "effect/PlatformError";
-import * as Sink from "effect/Sink";
-import * as Stream from "effect/Stream";
-import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
+import { ChildProcessSpawner } from "effect/unstable/process";
import {
VcsOutputDecodeError,
@@ -15,19 +10,21 @@ import {
VcsProcessSpawnError,
VcsProcessTimeoutError,
} from "@t3tools/contracts";
-import { collectUint8StreamText } from "../stream/collectUint8StreamText.ts";
+import { ProcessRunner, layer as ProcessRunnerLive } from "../processRunner.ts";
+import * as Match from "effect/Match";
export interface VcsProcessInput {
readonly operation: string;
readonly command: string;
readonly args: ReadonlyArray<string>;
readonly cwd: string;
+ readonly spawnCwd?: string;
readonly stdin?: string;
readonly env?: NodeJS.ProcessEnv;
readonly allowNonZeroExit?: boolean;
readonly timeoutMs?: number;
readonly maxOutputBytes?: number;
- readonly truncateOutputAtMaxBytes?: boolean;
+ readonly appendTruncationMarker?: boolean;
}
export interface VcsProcessOutput {
@@ -38,25 +35,7 @@ export interface VcsProcessOutput {
readonly stderrTruncated: boolean;
}
-export interface VcsProcessCollectedText {
- readonly text: string;
- readonly truncated: boolean;
-}
-
-export interface VcsProcessHandle {
- readonly pid: ChildProcessSpawner.ProcessId;
- readonly stdin: Sink.Sink<void, Uint8Array, never, VcsError>;
- readonly stdout: Stream.Stream<Uint8Array, VcsError>;
- readonly stderr: Stream.Stream<Uint8Array, VcsError>;
- readonly exitCode: Effect.Effect<ChildProcessSpawner.ExitCode, VcsError>;
- readonly writeStdin: (input: string) => Effect.Effect<void, VcsError>;
-}
-
export interface VcsProcessShape {
- readonly withProcess: <A, E, R>(
- input: VcsProcessInput,
- use: (handle: VcsProcessHandle) => Effect.Effect<A, E, R>,
- ) => Effect.Effect<A, E | VcsError, R>;
readonly run: (input: VcsProcessInput) => Effect.Effect<VcsProcessOutput, VcsError>;
}
@@ -72,164 +51,72 @@ function commandLabel(command: string, args: ReadonlyArray<string>): string {
return [command, ...args].join(" ");
}
-function outputDecodeError(
- input: VcsProcessInput,
- detail: string,
- cause: unknown,
-): VcsOutputDecodeError {
- return new VcsOutputDecodeError({
- operation: input.operation,
- command: commandLabel(input.command, input.args),
- cwd: input.cwd,
- detail,
- cause,
- });
-}
-
-export const collectText = Effect.fn("VcsProcess.collectText")(function* (input: {
- readonly operation: string;
- readonly command: string;
- readonly cwd: string;
- readonly stream: Stream.Stream<Uint8Array, VcsError>;
- readonly maxOutputBytes?: number;
- readonly truncateOutputAtMaxBytes?: boolean;
-}) {
- const maxOutputBytes = input.maxOutputBytes ?? DEFAULT_MAX_OUTPUT_BYTES;
- return yield* collectUint8StreamText({
- stream: input.stream,
- maxBytes: maxOutputBytes,
- truncatedMarker: input.truncateOutputAtMaxBytes ? OUTPUT_TRUNCATED_MARKER : null,
- });
-});
-
export const make = Effect.fn("makeVcsProcess")(function* () {
- const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
+ const processRunner = yield* ProcessRunner;
- const spawn = Effect.fn("VcsProcess.spawn")(function* (input: VcsProcessInput) {
+ const run = Effect.fn("VcsProcess.run")(function* (input: VcsProcessInput) {
const label = commandLabel(input.command, input.args);
- const child = yield* spawner
- .spawn(
- ChildProcess.make(input.command, [...input.args], {
- cwd: input.cwd,
- env: {
- ...process.env,
- ...input.env,
- },
- }),
- )
+ const baseError = {
+ operation: input.operation,
+ command: label,
+ cwd: input.cwd,
+ };
+
+ const result = yield* processRunner
+ .run({
+ command: input.command,
+ args: input.args,
+ cwd: input.cwd,
+ ...(input.spawnCwd !== undefined ? { spawnCwd: input.spawnCwd } : {}),
+ ...(input.stdin !== undefined ? { stdin: input.stdin } : {}),
+ ...(input.env !== undefined ? { env: input.env } : {}),
+ timeout: input.timeoutMs ?? DEFAULT_TIMEOUT_MS,
+ maxOutputBytes: input.maxOutputBytes ?? DEFAULT_MAX_OUTPUT_BYTES,
+ outputMode: "truncate",
+ truncatedMarker: input.appendTruncationMarker ? OUTPUT_TRUNCATED_MARKER : "",
+ timeoutBehavior: "error",
+ })
.pipe(
Effect.mapError(
- (cause) =>
- new VcsProcessSpawnError({
- operation: input.operation,
- command: label,
- cwd: input.cwd,
- cause,
- }),
+ Match.valueTags({
+ ProcessSpawnError: (error) =>
+ VcsProcessSpawnError.fromProcessSpawnError(baseError, error),
+ ProcessOutputLimitError: (error) =>
+ VcsOutputDecodeError.fromProcessOutputLimitError(baseError, error),
+ ProcessTimeoutError: (error) =>
+ VcsProcessTimeoutError.fromProcessTimeoutError(baseError, error),
+ ProcessStdinError: (error) =>
+ VcsOutputDecodeError.fromProcessStdinError(baseError, error),
+ ProcessReadError: (error) =>
+ VcsOutputDecodeError.fromProcessReadError(baseError, error),
+ }),
),
);
- yield* Effect.addFinalizer(() => child.kill().pipe(Effect.ignore));
- const mapStreamError = (streamName: "stdout" | "stderr") =>
- Stream.mapError((cause: PlatformError.PlatformError) =>
- outputDecodeError(input, `failed to read process ${streamName}`, cause),
- );
- const mapEffectError = (detail: string) =>
- Effect.mapError((cause: PlatformError.PlatformError) =>
- outputDecodeError(input, detail, cause),
- );
- const writeStdin = (stdin: string) =>
- Stream.run(Stream.encodeText(Stream.make(stdin)), child.stdin).pipe(
- mapEffectError("failed to write process stdin"),
- );
-
- return {
- pid: child.pid,
- stdin: child.stdin.pipe(
- Sink.mapError((cause) => outputDecodeError(input, "failed to write process stdin", cause)),
- ),
- stdout: child.stdout.pipe(mapStreamError("stdout")),
- stderr: child.stderr.pipe(mapStreamError("stderr")),
- exitCode: child.exitCode.pipe(mapEffectError("failed to read process exit code")),
- writeStdin,
- } satisfies VcsProcessHandle;
- });
-
- const withProcess: VcsProcessShape["withProcess"] = (input, use) =>
- Effect.scoped(spawn(input).pipe(Effect.flatMap(use)));
+ if (result.code === null) {
+ return yield* VcsOutputDecodeError.missingExitCode(baseError);
+ }
- const run = Effect.fn("VcsProcess.run")(function* (input: VcsProcessInput) {
- const timeoutMs = input.timeoutMs ?? DEFAULT_TIMEOUT_MS;
- const maxOutputBytes = input.maxOutputBytes ?? DEFAULT_MAX_OUTPUT_BYTES;
- const label = commandLabel(input.command, input.args);
-
- const runProcess = Effect.gen(function* () {
- const [stdout, stderr, exitCode] = yield* withProcess(input, (child) =>
- Effect.all(
- [
- collectText({
- operation: input.operation,
- command: label,
- cwd: input.cwd,
- stream: child.stdout,
- maxOutputBytes,
- truncateOutputAtMaxBytes: input.truncateOutputAtMaxBytes ?? false,
- }),
- collectText({
- operation: input.operation,
- command: label,
- cwd: input.cwd,
- stream: child.stderr,
- maxOutputBytes,
- truncateOutputAtMaxBytes: input.truncateOutputAtMaxBytes ?? false,
- }),
- child.exitCode,
- input.stdin === undefined ? Effect.void : child.writeStdin(input.stdin),
- ],
- { concurrency: "unbounded" },
- ),
- ).pipe(Effect.map(([stdout, stderr, exitCode]) => [stdout, stderr, exitCode] as const));
+ if (!input.allowNonZeroExit && result.code !== 0) {
+ return yield* new VcsProcessExitError({
+ operation: input.operation,
+ command: label,
+ cwd: input.cwd,
+ exitCode: result.code,
+ detail: result.stderr.trim() || `${label} exited with code ${result.code}.`,
+ });
+ }
- if (!input.allowNonZeroExit && exitCode !== 0) {
- return yield* new VcsProcessExitError({
- operation: input.operation,
- command: label,
- cwd: input.cwd,
- exitCode,
- detail: stderr.text.trim() || `${label} exited with code ${exitCode}.`,
- });
- }
-
- return {
- exitCode,
- stdout: stdout.text,
- stderr: stderr.text,
- stdoutTruncated: stdout.truncated,
- stderrTruncated: stderr.truncated,
- } satisfies VcsProcessOutput;
- });
-
- return yield* runProcess.pipe(
- Effect.scoped,
- Effect.timeoutOption(Duration.millis(timeoutMs)),
- Effect.flatMap((result) =>
- Option.match(result, {
- onSome: Effect.succeed,
- onNone: () =>
- Effect.fail(
- new VcsProcessTimeoutError({
- operation: input.operation,
- command: label,
- cwd: input.cwd,
- timeoutMs,
- }),
- ),
- }),
- ),
- );
+ return {
+ exitCode: result.code,
+ stdout: result.stdout,
+ stderr: result.stderr,
+ stdoutTruncated: result.stdoutTruncated,
+ stderrTruncated: result.stderrTruncated,
+ } satisfies VcsProcessOutput;
});
- return VcsProcess.of({ withProcess, run });
+ return VcsProcess.of({ run });
});
-export const layer = Layer.effect(VcsProcess, make());
+export const layer = Layer.effect(VcsProcess, make()).pipe(Layer.provide(ProcessRunnerLive));
diff --git a/apps/server/src/vcs/testing/VcsDriverContractHarness.ts b/apps/server/src/vcs/testing/VcsDriverContractHarness.ts
index e18cea349..fd4742835 100644
--- a/apps/server/src/vcs/testing/VcsDriverContractHarness.ts
+++ b/apps/server/src/vcs/testing/VcsDriverContractHarness.ts
@@ -11,6 +11,10 @@ import * as Option from "effect/Option";
import type { VcsDriverKind } from "@t3tools/contracts";
import * as VcsDriver from "../VcsDriver.ts";
+function normalizePathForComparison(value: string): string {
+ return value.replaceAll("\\", "/");
+}
+
export interface VcsDriverFixture<R, E> {
readonly createRepo: (cwd: string) => Effect.Effect<void, E, R>;
readonly writeFile: (
@@ -67,7 +71,11 @@ export function runVcsDriverContractSuite<R, E>(input: VcsDriverContractSuiteInp
yield* input.fixture.writeFile(cwd, "src/index.ts", "export const value = 1;\n");
const identity = yield* driver.detectRepository(cwd);
assert.equal(identity?.kind, input.kind);
- assert.isTrue(identity?.rootPath.endsWith(cwd));
+ assert.isTrue(
+ normalizePathForComparison(identity?.rootPath ?? "").endsWith(
+ normalizePathForComparison(cwd),
+ ),
+ );
assert.equal(identity?.freshness.source, "live-local");
assert.isTrue(DateTime.isDateTime(identity?.freshness.observedAt));
assert.isTrue(Option.isNone(identity?.freshness.expiresAt ?? Option.none()));
diff --git a/packages/contracts/src/vcs.ts b/packages/contracts/src/vcs.ts
index 25f6c04c6..f52f825ec 100644
--- a/packages/contracts/src/vcs.ts
+++ b/packages/contracts/src/vcs.ts
@@ -58,6 +58,34 @@ export const VcsListRemotesResult = Schema.Struct({
});
export type VcsListRemotesResult = typeof VcsListRemotesResult.Type;
+export interface VcsProcessErrorContext {
+ readonly operation: string;
+ readonly command: string;
+ readonly cwd: string;
+}
+
+export interface VcsProcessSpawnFailure {
+ readonly cause: unknown;
+}
+
+export interface VcsProcessStdinFailure {
+ readonly cause: unknown;
+}
+
+export interface VcsProcessReadFailure {
+ readonly stream: "stdout" | "stderr" | "exitCode";
+ readonly cause: unknown;
+}
+
+export interface VcsProcessOutputLimitFailure {
+ readonly stream: "stdout" | "stderr";
+ readonly maxBytes: number;
+}
+
+export interface VcsProcessTimeoutFailure {
+ readonly timeoutMs: number;
+}
+
export class VcsProcessSpawnError extends Schema.TaggedErrorClass<VcsProcessSpawnError>()(
"VcsProcessSpawnError",
{
@@ -70,6 +98,13 @@ export class VcsProcessSpawnError extends Schema.TaggedErrorClass<VcsProcessSpaw
override get message(): string {
return `VCS process failed to spawn in ${this.operation}: ${this.command} (${this.cwd})`;
}
+
+ static fromProcessSpawnError(context: VcsProcessErrorContext, error: VcsProcessSpawnFailure) {
+ return new VcsProcessSpawnError({
+ ...context,
+ cause: error.cause,
+ });
+ }
}
export class VcsProcessExitError extends Schema.TaggedErrorClass<VcsProcessExitError>()(
@@ -99,6 +134,13 @@ export class VcsProcessTimeoutError extends Schema.TaggedErrorClass<VcsProcessTi
override get message(): string {
return `VCS process timed out in ${this.operation}: ${this.command} (${this.cwd}) after ${this.timeoutMs}ms`;
}
+
+ static fromProcessTimeoutError(context: VcsProcessErrorContext, error: VcsProcessTimeoutFailure) {
+ return new VcsProcessTimeoutError({
+ ...context,
+ timeoutMs: error.timeoutMs,
+ });
+ }
}
export class VcsOutputDecodeError extends Schema.TaggedErrorClass<VcsOutputDecodeError>()(
@@ -114,6 +156,42 @@ export class VcsOutputDecodeError extends Schema.TaggedErrorClass<VcsOutputDecod
override get message(): string {
return `VCS output decode failed in ${this.operation}: ${this.command} (${this.cwd}) - ${this.detail}`;
}
+
+ static fromProcessStdinError(context: VcsProcessErrorContext, error: VcsProcessStdinFailure) {
+ return new VcsOutputDecodeError({
+ ...context,
+ detail: "failed to write process stdin",
+ cause: error.cause,
+ });
+ }
+
+ static fromProcessReadError(context: VcsProcessErrorContext, error: VcsProcessReadFailure) {
+ return new VcsOutputDecodeError({
+ ...context,
+ detail:
+ error.stream === "exitCode"
+ ? "failed to read process exit code"
+ : `failed to read process ${error.stream}`,
+ cause: error.cause,
+ });
+ }
+
+ static fromProcessOutputLimitError(
+ context: VcsProcessErrorContext,
+ error: VcsProcessOutputLimitFailure,
+ ) {
+ return new VcsOutputDecodeError({
+ ...context,
+ detail: `process ${error.stream} exceeded ${error.maxBytes} bytes`,
+ });
+ }
+
+ static missingExitCode(context: VcsProcessErrorContext) {
+ return new VcsOutputDecodeError({
+ ...context,
+ detail: "process completed without an exit code",
+ });
+ }
}
export class VcsRepositoryDetectionError extends Schema.TaggedErrorClass<VcsRepositoryDetectionError>()(