import { homedir } from "node:os"; import { isAbsolute, join, resolve } from "node:path"; import type { SettingDeclaration } from "./pi-ext-settings.ts"; export const DEFAULT_STATE_PATH = ".pi/loop/state.db"; /** Accepts absolute, `~/`, or project-relative paths; relative paths resolve against the session cwd. */ export function parseStatePath(raw: unknown): string { if ( typeof raw !== "string" || !raw.trim() || raw.length > 4096 || /[\x00-\x1f]/u.test(raw) ) throw new Error("expected a state database file path"); return raw.startsWith("~/") || raw.startsWith("~\\") ? join(homedir(), raw.slice(2)) : raw; } export function statePathFor(cwd: string, value: string): string { return isAbsolute(value) ? value : resolve(cwd, value); } export const STATE_PATH_SETTING: SettingDeclaration = { key: "loop.statePath", label: "State database", description: "SQLite file holding runs, gate attempts, and events; relative paths resolve against the project directory.", env: "PI_LOOP_STATE_PATH", default: DEFAULT_STATE_PATH, parse: parseStatePath, };