/**
 * Pure handlers for `opensquid checkpoints` (CLI.6 + DURABLE.4 scaffold) —
 * split out of checkpoints.ts for the file-size budget.
 *
 * Commander-agnostic. Direct-import callers (tests, future programmatic
 * tooling) use these. The commander wiring in `./checkpoints.ts` calls
 * these through `./checkpoints_actions.ts`.
 *
 * Imports from: ../../runtime/durable/index.js.
 * Imported by: ./checkpoints.ts (re-exports public surface),
 *   ./checkpoints_actions.ts (verb handlers).
 */
/** Inspect interrupted runs (manifest-orphan checkpoints are dropped). */
export async function list(opts) {
    const now = (opts.nowMs ?? Date.now)();
    const windowMs = opts.windowMs ?? Number.POSITIVE_INFINITY;
    const summaries = await opts.store.scanInterrupted(windowMs, now);
    const out = [];
    for (const s of summaries) {
        const m = await opts.store.getRunManifest(s.runId);
        if (!m)
            continue;
        out.push({
            runId: s.runId,
            packId: m.packId,
            skill: m.skill,
            ruleId: m.ruleId,
            lastCompletedStep: s.lastCompletedStep,
            lastCompletedAtMs: s.lastCompletedAtMs,
            ageMs: now - s.lastCompletedAtMs,
        });
    }
    return out;
}
/** Inspect one run: manifest + every checkpoint row + terminal state. */
export async function show(store, runId) {
    const [manifest, checkpoints, hasTerminalMarker] = await Promise.all([
        store.getRunManifest(runId),
        store.fetchRun(runId),
        store.hasTerminalMarker(runId),
    ]);
    return { manifest, checkpoints, hasTerminalMarker };
}
/** Explicit resume — bypasses DURABLE.4's resume window
 *  (`Resumer.resume` itself never consults the window; only
 *  `scanInterrupted` / `resumeOnStartup` do). */
export async function resume(resumer, store, runId) {
    const manifest = await store.getRunManifest(runId);
    if (!manifest) {
        return { resumed: false, manifestMissing: true };
    }
    const lastCompleted = await store.lastCompletedStep(runId);
    const result = await resumer.resume({
        runId: manifest.runId,
        packId: manifest.packId,
        packVersion: manifest.packVersion,
        skill: manifest.skill,
        ruleId: manifest.ruleId,
        eventKind: manifest.eventKind,
        eventPayload: manifest.eventPayload,
        lastCompletedStep: lastCompleted?.stepIdx ?? -1,
        lastCompletedAtMs: lastCompleted?.completedAtMs ?? manifest.startedAtMs,
    });
    return result.reason
        ? { resumed: result.resumed, reason: result.reason }
        : { resumed: result.resumed };
}
/** Prune old checkpoint rows. Pass-through to `pruneOlderThan`. */
export async function clean(opts) {
    const removed = await opts.store.pruneOlderThan(opts.olderThanMs, (opts.nowMs ?? Date.now)());
    return { removed };
}
//# sourceMappingURL=checkpoints_core.js.map