"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ACTIVE_TASK_FILENAME = void 0;
exports.activeTaskPath = activeTaskPath;
exports.writeActiveTask = writeActiveTask;
exports.readActiveTaskFile = readActiveTaskFile;
exports.clearActiveTask = clearActiveTask;
exports.readActiveTask = readActiveTask;
const fs = __importStar(require("node:fs"));
const path = __importStar(require("node:path"));
const lockfile_1 = require("./lockfile");
exports.ACTIVE_TASK_FILENAME = '.aidev.active';
function activeTaskPath(cwd) {
    return path.join(cwd, exports.ACTIVE_TASK_FILENAME);
}
function writeActiveTask(cwd, taskId) {
    fs.writeFileSync(activeTaskPath(cwd), taskId.trim(), 'utf8');
}
function readActiveTaskFile(cwd) {
    const p = activeTaskPath(cwd);
    if (!fs.existsSync(p))
        return null;
    const raw = fs.readFileSync(p, 'utf8').trim();
    return raw.length > 0 ? raw : null;
}
function clearActiveTask(cwd) {
    try {
        fs.unlinkSync(activeTaskPath(cwd));
    }
    catch {
        // already removed or never existed
    }
}
/**
 * Task id currently being implemented by a live `aidev run` in `cwd`, if any.
 * Returns null when no lock is held, the holder is dead, or no task file exists.
 */
function readActiveTask(cwd) {
    const pid = (0, lockfile_1.readLock)(cwd);
    if (pid === null || !(0, lockfile_1.isProcessAlive)(pid)) {
        clearActiveTask(cwd);
        return null;
    }
    return readActiveTaskFile(cwd);
}
//# sourceMappingURL=activeTask.js.map