akm-cli
Version:
akm (Agent Knowledge Manager) — a portable, local-first capability library for AI agents. Discover, load, share, and improve reusable skills, scripts, workflows, and knowledge across any shell-capable coding agent, including Claude Code, OpenCode, and Cur
48 lines (47 loc) • 2.33 kB
JavaScript
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
/**
* Whether the process that owns an `active` task attempt is still there, read from the owner the attempt recorded at
* reservation. The one rule: `reconcile-abandoned.ts` writes what it says, and the history readers show it.
*
* - running: the owner process is alive (same host, same boot, same start time).
* - interrupted: the owner is provably gone: a different boot, a dead pid, or a reused pid.
* - indeterminate: it cannot be told (another host, no liveness evidence).
*
* Elapsed time never decides. An attempt reserved before owners were recorded is interrupted only when it started
* before the current boot.
*/
import os from "node:os";
import { isProcessAlive } from "../../core/common.js";
import { processIdentity } from "../../core/file-lock.js";
import { decodeTaskHistoryMetadata, } from "../../storage/repositories/task-history-repository.js";
function classifyOwner(owner, here) {
if (owner.host !== here.host)
return "indeterminate";
if (owner.bootId !== undefined && here.self !== undefined && owner.bootId !== here.self.bootId)
return "interrupted";
if (owner.startTime !== undefined && here.self !== undefined) {
const current = processIdentity(owner.pid);
if (current !== undefined)
return current.startTime === owner.startTime ? "running" : "interrupted";
}
return isProcessAlive(owner.pid) ? "indeterminate" : "interrupted";
}
export function classifyAttempt(row, here) {
let owner;
try {
owner = decodeTaskHistoryMetadata(row.metadata_json).owner;
}
catch {
// Metadata that cannot be read names no owner, so the start-time rule below decides: an attempt that started
// before this boot has an owner that is gone, whatever its metadata says.
owner = undefined;
}
if (owner)
return classifyOwner(owner, here);
return new Date(row.started_at).getTime() < here.bootedAtMs ? "interrupted" : "indeterminate";
}
export function hereNow() {
return { host: os.hostname(), self: processIdentity(process.pid), bootedAtMs: Date.now() - os.uptime() * 1000 };
}