@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
91 lines • 3.66 kB
JavaScript
/**
* Persistent skip-list for one-time directory-scan migrations.
*
* Missing, unreadable, or malformed state is fail-open: every scan runs again.
* Bump MIGRATIONS_STATE_SCHEMA_VERSION when a listed migration's semantics change.
*/
import { mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { getAgentDir } from "./config.js";
export const MIGRATIONS_STATE_SCHEMA_VERSION = 1;
export const MIGRATIONS_STATE_FILENAME = "migrations-state.json";
export const SCAN_MIGRATIONS = [
"migrateLegacySenpiDirs",
"migrateSessionsFromAgentRoot",
"restoreDrainedPiDirs",
];
const SCAN_MIGRATION_NAMES = new Set(SCAN_MIGRATIONS);
function isScanMigrationName(value) {
return SCAN_MIGRATION_NAMES.has(value);
}
function isRecord(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function isStringArray(value) {
return Array.isArray(value) && value.every((entry) => typeof entry === "string");
}
function readState(agentDir) {
let parsed;
try {
parsed = JSON.parse(readFileSync(join(agentDir, MIGRATIONS_STATE_FILENAME), "utf-8"));
}
catch {
return undefined;
}
if (!isRecord(parsed) || parsed.schemaVersion !== MIGRATIONS_STATE_SCHEMA_VERSION)
return undefined;
return parsed;
}
function completedOf(state) {
const completed = state?.completed;
if (!isStringArray(completed))
return [];
return completed.filter((name) => isScanMigrationName(name));
}
export function readCompletedScanMigrations(agentDir = getAgentDir()) {
return new Set(completedOf(readState(agentDir)));
}
export function writeCompletedScanMigrations(completed, agentDir = getAgentDir()) {
writeState(agentDir, { ...readState(agentDir), completed: [...completed] });
}
function isMtimeEntry(entry) {
return typeof entry[1] === "number" && Number.isFinite(entry[1]);
}
export function readLegacyPiAgentDirRecord(agentDir = getAgentDir()) {
const raw = readState(agentDir)?.legacyPiAgentDir;
if (!isRecord(raw))
return { noticedMtimes: {} };
const noticedMtimes = isRecord(raw.noticedMtimes)
? Object.fromEntries(Object.entries(raw.noticedMtimes).filter(isMtimeEntry))
: {};
const copiedAt = raw.copiedAt;
return typeof copiedAt === "number" && Number.isFinite(copiedAt) ? { copiedAt, noticedMtimes } : { noticedMtimes };
}
export function writeLegacyPiAgentDirRecord(record, agentDir = getAgentDir()) {
const state = readState(agentDir);
writeState(agentDir, { ...state, completed: completedOf(state), legacyPiAgentDir: record });
}
/** A fresh copy restarts the notice history: every later pi edit is new to this agent dir. */
export function recordLegacyPiAgentDirCopy(agentDir = getAgentDir(), copiedAt = Date.now()) {
writeLegacyPiAgentDirRecord({ copiedAt, noticedMtimes: {} }, agentDir);
}
/** Keeps every field it does not own, so the scan list and the pi-copy record never erase each other. */
function writeState(agentDir, fields) {
mkdirSync(agentDir, { recursive: true });
const target = join(agentDir, MIGRATIONS_STATE_FILENAME);
const temporary = `${target}.${process.pid}.tmp`;
const payload = { schemaVersion: MIGRATIONS_STATE_SCHEMA_VERSION, ...fields };
try {
writeFileSync(temporary, `${JSON.stringify(payload, null, 2)}\n`);
renameSync(temporary, target);
}
catch {
try {
rmSync(temporary, { force: true });
}
catch {
// Next boot fail-opens and rescans.
}
}
}
//# sourceMappingURL=migrations-state.js.map