live-resume.mjs

 1#!/usr/bin/env node
 2/**
 3 * Recover the next agent action from the durable live-session journal.
 4 */
 5
 6import { createLiveSessionStore } from './live-session-store.mjs';
 7
 8function parseArgs(argv) {
 9  const out = { id: null };
10  for (let i = 0; i < argv.length; i++) {
11    const arg = argv[i];
12    if (arg === '--id') out.id = argv[++i];
13    else if (arg.startsWith('--id=')) out.id = arg.slice('--id='.length);
14    else if (arg === '--help' || arg === '-h') out.help = true;
15  }
16  return out;
17}
18
19export async function resumeCli() {
20  const args = parseArgs(process.argv.slice(2));
21  if (args.help) {
22    console.log(`Usage: node live-resume.mjs [--id SESSION_ID]\n\nPrint the active durable session checkpoint and the next safe agent action.`);
23    return;
24  }
25
26  const store = createLiveSessionStore({ cwd: process.cwd(), sessionId: args.id || undefined });
27  const snapshot = args.id ? store.getSnapshot(args.id) : store.listActiveSessions()[0] || null;
28  if (!snapshot) {
29    console.log(JSON.stringify({ active: false, nextAction: 'No active durable live session found.' }, null, 2));
30    return;
31  }
32
33  const pending = snapshot.pendingEvent || null;
34  const nextAction = pending
35    ? `Run live-poll.mjs, handle ${pending.type} ${pending.id}, then acknowledge with live-poll.mjs --reply ${pending.id} done.`
36    : snapshot.phase === 'carbonize_required'
37      ? `Finish carbonize cleanup${snapshot.sourceFile ? ` in ${snapshot.sourceFile}` : ''}, then run live-complete.mjs --id ${snapshot.id}.`
38      : snapshot.phase === 'accept_requested'
39        ? `Run live-complete.mjs --id ${snapshot.id} after verifying the accepted variant is written.`
40        : `Inspect ${snapshot.id}; no pending agent event is currently queued.`;
41
42  console.log(JSON.stringify({ active: true, snapshot, pendingEvent: pending, nextAction }, null, 2));
43}
44
45const _running = process.argv[1];
46if (_running?.endsWith('live-resume.mjs') || _running?.endsWith('live-resume.mjs/')) {
47  resumeCli();
48}