repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by admin
extensions/loop/src/store.ts
Rawimport { mkdirSync } from "node:fs";
import { hostname } from "node:os";
import { dirname } from "node:path";
import type { DatabaseSync } from "node:sqlite";
import type { Gate, GateResult } from "./gates.ts";
export type Budgets = {
minutes: number | null;
turns: number | null;
attempts: number | null;
};
export type RunStatus = "active" | "paused" | "stopped" | "succeeded";
export type RunRecord = {
id: string;
goal: string;
gates: Gate[];
cwd: string;
budgets: Budgets;
status: RunStatus;
reason: string;
turns: number;
attempts: number;
createdAt: number;
updatedAt: number;
};
export type AttemptRecord = GateResult & {
runId: string;
attempt: number;
gateIndex: number;
gateName: string;
};
export type EventRecord = {
id: number;
runId: string;
kind: string;
payload: unknown;
at: number;
};
const SCHEMA = `
CREATE TABLE IF NOT EXISTS gate_processes (
run_id TEXT PRIMARY KEY,
pid INTEGER NOT NULL,
host TEXT NOT NULL,
process_group INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS run_owners (
run_id TEXT PRIMARY KEY,
token TEXT,
pid INTEGER,
host TEXT
);
CREATE TABLE IF NOT EXISTS session_runs (
session_id TEXT PRIMARY KEY,
run_id TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS runs (
id TEXT PRIMARY KEY,
goal TEXT NOT NULL,
gates TEXT NOT NULL,
cwd TEXT NOT NULL,
budgets TEXT NOT NULL,
status TEXT NOT NULL,
reason TEXT NOT NULL,
turns INTEGER NOT NULL,
attempts INTEGER NOT NULL,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS gate_attempts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
run_id TEXT NOT NULL REFERENCES runs(id),
attempt_no INTEGER NOT NULL,
gate_index INTEGER NOT NULL,
gate_name TEXT NOT NULL,
exit_code INTEGER,
error TEXT,
stdout TEXT NOT NULL,
stderr TEXT NOT NULL,
truncated INTEGER NOT NULL,
started_at INTEGER NOT NULL,
ended_at INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS gate_attempts_run ON gate_attempts(run_id, attempt_no);
CREATE TABLE IF NOT EXISTS events (
id INTEGER PRIMARY KEY AUTOINCREMENT,
run_id TEXT NOT NULL,
kind TEXT NOT NULL,
payload TEXT NOT NULL,
at INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS events_run ON events(run_id, id);
`;
type Row = Record<string, unknown>;
function toRun(row: Row): RunRecord {
return {
id: String(row["id"]),
goal: String(row["goal"]),
gates: JSON.parse(String(row["gates"])),
cwd: String(row["cwd"]),
budgets: JSON.parse(String(row["budgets"])),
status: String(row["status"]) as RunStatus,
reason: String(row["reason"]),
turns: Number(row["turns"]),
attempts: Number(row["attempts"]),
createdAt: Number(row["created_at"]),
updatedAt: Number(row["updated_at"]),
};
}
function toAttempt(row: Row): AttemptRecord {
return {
runId: String(row["run_id"]),
attempt: Number(row["attempt_no"]),
gateIndex: Number(row["gate_index"]),
gateName: String(row["gate_name"]),
exitCode: row["exit_code"] === null ? null : Number(row["exit_code"]),
error: row["error"] === null ? null : String(row["error"]),
stdout: String(row["stdout"]),
stderr: String(row["stderr"]),
truncated: Number(row["truncated"]) === 1,
startedAt: Number(row["started_at"]),
endedAt: Number(row["ended_at"]),
};
}
function statements(db: DatabaseSync) {
return {
saveRun: db.prepare(
`INSERT INTO runs (id, goal, gates, cwd, budgets, status, reason, turns, attempts, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(id) DO UPDATE SET status=excluded.status, reason=excluded.reason,
turns=excluded.turns, attempts=excluded.attempts, updated_at=excluded.updated_at`,
),
addAttempt: db.prepare(
`INSERT INTO gate_attempts (run_id, attempt_no, gate_index, gate_name, exit_code, error, stdout, stderr, truncated, started_at, ended_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
),
addEvent: db.prepare(
"INSERT INTO events (run_id, kind, payload, at) VALUES (?, ?, ?, ?)",
),
getRun: db.prepare("SELECT * FROM runs WHERE id = ?"),
listRuns: db.prepare(
"SELECT * FROM runs WHERE id LIKE ? ESCAPE '\\' ORDER BY created_at DESC, rowid DESC LIMIT ?",
),
attempts: db.prepare(
"SELECT * FROM (SELECT * FROM gate_attempts WHERE run_id = ? ORDER BY id DESC LIMIT ?) ORDER BY id",
),
events: db.prepare(
"SELECT * FROM (SELECT * FROM events WHERE run_id = ? ORDER BY id DESC LIMIT ?) ORDER BY id",
),
lastGateStart: db.prepare(
"SELECT payload FROM events WHERE run_id=? AND kind='gate_started' AND json_extract(payload, '$.attempt')=? ORDER BY id DESC LIMIT 1",
),
version: db.prepare(
"SELECT (SELECT ifnull(max(id), 0) FROM events) AS events, (SELECT ifnull(max(updated_at), 0) FROM runs) AS runs",
),
};
}
/** Project-local loop state; one synchronous connection per Pi session. */
export class LoopStore {
readonly path: string;
private db: DatabaseSync;
// Prepared once: runs and events are written on every turn and gate result.
private sql: ReturnType<typeof statements>;
private constructor(path: string, db: DatabaseSync) {
this.path = path;
this.db = db;
this.sql = statements(db);
}
static async open(path: string): Promise<LoopStore> {
// Deferred so session start never pays for loading SQLite.
const { DatabaseSync } = await import("node:sqlite");
mkdirSync(dirname(path), { recursive: true });
const db = new DatabaseSync(path);
try {
db.exec("PRAGMA busy_timeout=1000");
db.exec(SCHEMA);
return new LoopStore(path, db);
} catch (error) {
db.close();
throw error;
}
}
/** Serialize admission with writes; a live or unprovable owner is never displaced. */
claim(runId: string, token: string, sessionId?: string, fresh = false): void {
this.transaction(() => {
const row = this.db
.prepare("SELECT * FROM run_owners WHERE run_id=?")
.get(runId);
if (!row && !fresh)
throw new Error(
`Legacy run ${runId}: ownership unknown; stop all old Pi instances, then /loop recover ${runId}`,
);
if (row?.["token"] && row["token"] !== token) {
let dead = false;
if (row["host"] === hostname()) {
try {
process.kill(Number(row["pid"]), 0);
} catch (error) {
dead = (error as NodeJS.ErrnoException).code === "ESRCH";
}
}
if (!dead)
throw new Error(
`Run ${runId} owned by live or unverifiable process ${row["pid"]} on ${row["host"]}; inspect only`,
);
}
this.db
.prepare("INSERT OR REPLACE INTO run_owners VALUES (?, ?, ?, ?)")
.run(runId, token, process.pid, hostname());
if (sessionId)
this.db
.prepare("INSERT OR REPLACE INTO session_runs VALUES (?, ?)")
.run(sessionId, runId);
});
}
ownership(runId: string): string {
const row = this.db
.prepare("SELECT * FROM run_owners WHERE run_id=?")
.get(runId);
if (!row) return "legacy; ownership unknown, offline recovery required";
if (!row["token"]) return "released; explicit resume available";
if (row["host"] === hostname()) {
try {
process.kill(Number(row["pid"]), 0);
} catch (error) {
if ((error as NodeJS.ErrnoException).code === "ESRCH")
return `interrupted; owner process ${row["pid"]} exited`;
}
}
return `live or unverifiable owner process ${row["pid"]} on ${row["host"]}`;
}
trackGate(runId: string, pid: number): void {
this.db
.prepare("INSERT OR REPLACE INTO gate_processes VALUES (?, ?, ?, ?)")
.run(runId, pid, hostname(), process.platform === "win32" ? 0 : 1);
}
clearGate(runId: string): void {
this.db.prepare("DELETE FROM gate_processes WHERE run_id=?").run(runId);
}
/** A dead controller can leave a detached gate alive. Never overlap it. */
assertGateStopped(runId: string): void {
const row = this.db
.prepare("SELECT * FROM gate_processes WHERE run_id=?")
.get(runId);
if (!row) return;
let dead = false;
if (row["host"] === hostname()) {
try {
process.kill(Number(row["pid"]) * (row["process_group"] ? -1 : 1), 0);
} catch (error) {
dead = (error as NodeJS.ErrnoException).code === "ESRCH";
}
}
if (!dead)
throw new Error(
`Previous gate process ${row["pid"]} may still be running on ${row["host"]}; inspect and stop it before resume`,
);
}
/** Operator-confirmed offline migration only; old binaries cannot honor ownership. */
recoverLegacy(runId: string): void {
this.transaction(() => {
if (this.db.prepare("SELECT 1 FROM run_owners WHERE run_id=?").get(runId))
throw new Error("Run already has ownership metadata; use resume");
this.db.prepare("INSERT INTO run_owners(run_id) VALUES (?)").run(runId);
});
}
sessionRun(sessionId: string): RunRecord | undefined {
const row = this.db
.prepare("SELECT run_id FROM session_runs WHERE session_id=?")
.get(sessionId);
return row ? this.getRun(String(row["run_id"])) : undefined;
}
owned<T>(runId: string, token: string, write: () => T): T {
return this.transaction(() => {
const row = this.db
.prepare("SELECT token FROM run_owners WHERE run_id=?")
.get(runId);
if (row?.["token"] !== token)
throw new Error("Loop ownership lost; no write accepted");
return write();
});
}
release(runId: string, token: string): void {
this.db
.prepare(
"UPDATE run_owners SET token=NULL, pid=NULL, host=NULL WHERE run_id=? AND token=?",
)
.run(runId, token);
}
private transaction<T>(work: () => T): T {
this.db.exec("BEGIN IMMEDIATE");
try {
const result = work();
this.db.exec("COMMIT");
return result;
} catch (error) {
this.db.exec("ROLLBACK");
throw error;
}
}
saveRun(run: RunRecord): void {
this.sql.saveRun.run(
run.id,
run.goal,
JSON.stringify(run.gates),
run.cwd,
JSON.stringify(run.budgets),
run.status,
run.reason,
run.turns,
run.attempts,
run.createdAt,
run.updatedAt,
);
}
addAttempt(record: AttemptRecord): void {
this.sql.addAttempt.run(
record.runId,
record.attempt,
record.gateIndex,
record.gateName,
record.exitCode,
record.error,
record.stdout,
record.stderr,
record.truncated ? 1 : 0,
record.startedAt,
record.endedAt,
);
}
addEvent(runId: string, kind: string, payload: unknown, at: number): void {
this.sql.addEvent.run(runId, kind, JSON.stringify(payload ?? null), at);
}
getRun(id: string): RunRecord | undefined {
const row = this.sql.getRun.get(id);
return row ? toRun(row) : undefined;
}
/** Newest first; `prefix` matches abbreviated ids. */
listRuns(limit = 50, prefix = ""): RunRecord[] {
return this.sql.listRuns
.all(`${prefix.replaceAll(/[\\%_]/gu, "\\$&")}%`, limit)
.map(toRun);
}
attempts(runId: string, limit = 500): AttemptRecord[] {
return this.sql.attempts.all(runId, limit).map(toAttempt);
}
events(runId: string, limit = 200): EventRecord[] {
return this.sql.events.all(runId, limit).map((row) => ({
id: Number(row["id"]),
runId: String(row["run_id"]),
kind: String(row["kind"]),
payload: JSON.parse(String(row["payload"])),
at: Number(row["at"]),
}));
}
lastStartedGate(runId: string, attempt: number): number | undefined {
const row = this.sql.lastGateStart.get(runId, attempt);
if (!row) return undefined;
return (JSON.parse(String(row["payload"])) as { index: number }).index;
}
/** Poll revision for activity writes and durable control events. */
version(): string {
const row = this.sql.version.get();
return `${row?.["events"]}:${row?.["runs"]}`;
}
close(): void {
if (this.db.isOpen) this.db.close();
}
}