diff --git a/apps/server/src/checkpointing/Layers/CheckpointStore.ts b/apps/server/src/checkpointing/Layers/CheckpointStore.ts index 0a1d7abb9d..184ec96323 100644 --- a/apps/server/src/checkpointing/Layers/CheckpointStore.ts +++ b/apps/server/src/checkpointing/Layers/CheckpointStore.ts @@ -86,139 +86,140 @@ const makeCheckpointStore = Effect.gen(function* () { Effect.catch(() => Effect.succeed(false)), ); - const captureCheckpoint: CheckpointStoreShape["captureCheckpoint"] = (input) => - Effect.gen(function* () { - const operation = "CheckpointStore.captureCheckpoint"; - - yield* Effect.acquireUseRelease( - fs.makeTempDirectory({ prefix: "t3-fs-checkpoint-" }), - (tempDir) => - Effect.gen(function* () { - 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: "t3code@users.noreply.github.com", - GIT_COMMITTER_NAME: "T3 Code", - GIT_COMMITTER_EMAIL: "t3code@users.noreply.github.com", - }; - - const headExists = yield* hasHeadCommit(input.cwd); - if (headExists) { - yield* git.execute({ - operation, - cwd: input.cwd, - args: ["read-tree", "HEAD"], - env: commitEnv, - }); - } - - yield* git.execute({ - operation, - cwd: input.cwd, - args: ["add", "-A", "--", "."], - env: commitEnv, - }); - - const writeTreeResult = yield* git.execute({ - operation, - cwd: input.cwd, - args: ["write-tree"], - env: commitEnv, - }); - const treeOid = writeTreeResult.stdout.trim(); - if (treeOid.length === 0) { - return yield* new GitCommandError({ - operation, - command: "git write-tree", - cwd: input.cwd, - detail: "git write-tree returned an empty tree oid.", - }); - } - - const message = `t3 checkpoint ref=${input.checkpointRef}`; - const commitTreeResult = yield* git.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 GitCommandError({ - operation, - command: "git commit-tree", - cwd: input.cwd, - detail: "git commit-tree returned an empty commit oid.", - }); - } - - yield* git.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 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: "t3code@users.noreply.github.com", + GIT_COMMITTER_NAME: "T3 Code", + GIT_COMMITTER_EMAIL: "t3code@users.noreply.github.com", + }; + + const headExists = yield* hasHeadCommit(input.cwd); + if (headExists) { + yield* git.execute({ + operation, + cwd: input.cwd, + args: ["read-tree", "HEAD"], + env: commitEnv, + }); + } + + yield* git.execute({ + operation, + cwd: input.cwd, + args: ["add", "-A", "--", "."], + env: commitEnv, + }); + + const writeTreeResult = yield* git.execute({ + operation, + cwd: input.cwd, + args: ["write-tree"], + env: commitEnv, + }); + const treeOid = writeTreeResult.stdout.trim(); + if (treeOid.length === 0) { + return yield* new GitCommandError({ + operation, + command: "git write-tree", + cwd: input.cwd, + detail: "git write-tree returned an empty tree oid.", + }); + } + + const message = `t3 checkpoint ref=${input.checkpointRef}`; + const commitTreeResult = yield* git.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 GitCommandError({ + operation, + command: "git commit-tree", + cwd: input.cwd, + detail: "git commit-tree returned an empty commit oid.", + }); + } + + yield* git.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 hasCheckpointRef: CheckpointStoreShape["hasCheckpointRef"] = (input) => resolveCheckpointCommit(input.cwd, input.checkpointRef).pipe( Effect.map((commit) => commit !== null), ); - const restoreCheckpoint: CheckpointStoreShape["restoreCheckpoint"] = (input) => - Effect.gen(function* () { - const operation = "CheckpointStore.restoreCheckpoint"; + const restoreCheckpoint: CheckpointStoreShape["restoreCheckpoint"] = Effect.fn( + "restoreCheckpoint", + )(function* (input) { + const operation = "CheckpointStore.restoreCheckpoint"; - let commitOid = yield* resolveCheckpointCommit(input.cwd, input.checkpointRef); + let commitOid = yield* resolveCheckpointCommit(input.cwd, input.checkpointRef); - if (!commitOid && input.fallbackToHead === true) { - commitOid = yield* resolveHeadCommit(input.cwd); - } + if (!commitOid && input.fallbackToHead === true) { + commitOid = yield* resolveHeadCommit(input.cwd); + } - if (!commitOid) { - return false; - } + if (!commitOid) { + return false; + } + yield* git.execute({ + operation, + cwd: input.cwd, + args: ["restore", "--source", commitOid, "--worktree", "--staged", "--", "."], + }); + yield* git.execute({ + operation, + cwd: input.cwd, + args: ["clean", "-fd", "--", "."], + }); + + const headExists = yield* hasHeadCommit(input.cwd); + if (headExists) { yield* git.execute({ operation, cwd: input.cwd, - args: ["restore", "--source", commitOid, "--worktree", "--staged", "--", "."], - }); - yield* git.execute({ - operation, - cwd: input.cwd, - args: ["clean", "-fd", "--", "."], + args: ["reset", "--quiet", "--", "."], }); + } - const headExists = yield* hasHeadCommit(input.cwd); - if (headExists) { - yield* git.execute({ - operation, - cwd: input.cwd, - args: ["reset", "--quiet", "--", "."], - }); - } - - return true; - }); + return true; + }); - const diffCheckpoints: CheckpointStoreShape["diffCheckpoints"] = (input) => - Effect.gen(function* () { + const diffCheckpoints: CheckpointStoreShape["diffCheckpoints"] = Effect.fn("diffCheckpoints")( + function* (input) { const operation = "CheckpointStore.diffCheckpoints"; let fromCommitOid = yield* resolveCheckpointCommit(input.cwd, input.fromCheckpointRef); @@ -247,24 +248,26 @@ const makeCheckpointStore = Effect.gen(function* () { }); return result.stdout; - }); - - const deleteCheckpointRefs: CheckpointStoreShape["deleteCheckpointRefs"] = (input) => - Effect.gen(function* () { - const operation = "CheckpointStore.deleteCheckpointRefs"; - - yield* Effect.forEach( - input.checkpointRefs, - (checkpointRef) => - git.execute({ - operation, - cwd: input.cwd, - args: ["update-ref", "-d", checkpointRef], - allowNonZeroExit: true, - }), - { discard: true }, - ); - }); + }, + ); + + const deleteCheckpointRefs: CheckpointStoreShape["deleteCheckpointRefs"] = Effect.fn( + "deleteCheckpointRefs", + )(function* (input) { + const operation = "CheckpointStore.deleteCheckpointRefs"; + + yield* Effect.forEach( + input.checkpointRefs, + (checkpointRef) => + git.execute({ + operation, + cwd: input.cwd, + args: ["update-ref", "-d", checkpointRef], + allowNonZeroExit: true, + }), + { discard: true }, + ); + }); return { isGitRepository,