
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="726a3ddf-a890-5a81-94dd-cf28105b94c0")}catch(e){}}();
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { createGitProcessEnv } from "../core/git-process-env.js";
import { getGitStdout } from "./git-utils.js";
const CHECKPOINT_AUTHOR_NAME = "kanban-checkpoint";
const CHECKPOINT_AUTHOR_EMAIL = "kanban-checkpoint@local";
function runGit(cwd, args, options = {}) {
    return getGitStdout(args, cwd, options);
}
async function tryRunGit(cwd, args, options = {}) {
    try {
        return await getGitStdout(args, cwd, options);
    }
    catch {
        return null;
    }
}
function encodeRefSegment(value) {
    return Buffer.from(value, "utf8").toString("base64url");
}
function buildCheckpointRef(taskId, turn) {
    return `refs/kanban/checkpoints/${encodeRefSegment(taskId)}/turn/${turn}`;
}
async function createWorkingTreeCheckpointCommit(repoRoot, turn, taskId) {
    const tempDir = await mkdtemp(join(tmpdir(), "kanban-checkpoint-"));
    const tempIndexPath = join(tempDir, "index");
    const gitEnv = {
        ...createGitProcessEnv(),
        GIT_INDEX_FILE: tempIndexPath,
        GIT_AUTHOR_NAME: CHECKPOINT_AUTHOR_NAME,
        GIT_AUTHOR_EMAIL: CHECKPOINT_AUTHOR_EMAIL,
        GIT_COMMITTER_NAME: CHECKPOINT_AUTHOR_NAME,
        GIT_COMMITTER_EMAIL: CHECKPOINT_AUTHOR_EMAIL,
    };
    try {
        const headCommit = await tryRunGit(repoRoot, ["rev-parse", "--verify", "HEAD"], {
            env: gitEnv,
        });
        if (headCommit) {
            await runGit(repoRoot, ["read-tree", headCommit], { env: gitEnv });
        }
        else {
            await runGit(repoRoot, ["read-tree", "--empty"], { env: gitEnv });
        }
        await runGit(repoRoot, ["add", "-A", "--", "."], { env: gitEnv });
        const treeOid = await runGit(repoRoot, ["write-tree"], { env: gitEnv });
        const commitMessage = `kanban checkpoint task:${taskId} turn:${turn}`;
        const commitArgs = ["commit-tree", treeOid, "-m", commitMessage];
        if (headCommit) {
            commitArgs.push("-p", headCommit);
        }
        return await runGit(repoRoot, commitArgs, {
            env: gitEnv,
        });
    }
    finally {
        await rm(tempDir, { recursive: true, force: true });
    }
}
export async function captureTaskTurnCheckpoint(input) {
    const repoRoot = await runGit(input.cwd, ["rev-parse", "--show-toplevel"]);
    const commit = await createWorkingTreeCheckpointCommit(repoRoot, input.turn, input.taskId);
    const ref = buildCheckpointRef(input.taskId, input.turn);
    await runGit(repoRoot, ["update-ref", ref, commit]);
    return {
        turn: input.turn,
        ref,
        commit,
        createdAt: Date.now(),
    };
}
export async function deleteTaskTurnCheckpointRef(input) {
    const repoRoot = await runGit(input.cwd, ["rev-parse", "--show-toplevel"]);
    await tryRunGit(repoRoot, ["update-ref", "-d", input.ref]);
}
//# sourceMappingURL=turn-checkpoints.js.map
//# debugId=726a3ddf-a890-5a81-94dd-cf28105b94c0
