2026-08-06 09f01778d5a034e2af50ae05da8908f7b6a871c8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env node
/**
 * Print durable recovery status for Impeccable live sessions.
 */
 
import { createLiveSessionStore } from './live/session-store.mjs';
import { readLiveServerInfo } from './lib/impeccable-paths.mjs';
import { manualApplyResumeHint, mountFailureAction, renderSummary } from './live-resume.mjs';
import { enterLiveRoot } from './live/roots.mjs';
 
function readServerInfo() {
  return readLiveServerInfo(process.cwd())?.info || null;
}
 
async function fetchServerStatus(info) {
  if (!info) return null;
  try {
    const res = await fetch(`http://localhost:${info.port}/status?token=${info.token}`);
    if (!res.ok) return null;
    return await res.json();
  } catch {
    return null;
  }
}
 
export async function statusCli() {
  const info = readServerInfo();
  const server = await fetchServerStatus(info);
  const store = createLiveSessionStore({ cwd: process.cwd() });
  const activeSessions = store.listActiveSessions();
  const manualApply = findPendingManualApply(server, activeSessions);
  const sessions = server?.activeSessions || activeSessions;
  const renderFailure = sessions.find((session) => session?.renderState === 'failed') || null;
  const payload = {
    liveServer: server ? {
      status: server.status,
      port: server.port,
      connectedClients: server.connectedClients,
      agentPolling: server.agentPolling,
      pendingEvents: server.pendingEvents,
    } : null,
    activeSessions: sessions,
    render: sessions.map((session) => ({ id: session?.id ?? null, ...renderSummary(session) })),
    recoveryHint: recoveryHint({ server, manualApply, renderFailure }),
  };
  console.log(JSON.stringify(payload, null, 2));
}
 
function recoveryHint({ server, manualApply, renderFailure }) {
  if (manualApply) return manualApplyResumeHint(manualApply);
  if (renderFailure) return mountFailureAction(renderFailure);
  if (server) {
    return 'Run live-poll.mjs to continue pending work, or live-complete.mjs --id <session> after manual cleanup.';
  }
  return 'Start live-server.mjs to requeue pending durable events, then run live-poll.mjs.';
}
 
function findPendingManualApply(server, activeSessions) {
  const fromServer = server?.pendingEvents?.find((event) => event?.type === 'manual_edit_apply');
  if (fromServer) return fromServer;
  const fromSession = activeSessions
    ?.map((session) => session.pendingEvent)
    .find((event) => event?.type === 'manual_edit_apply');
  return fromSession || null;
}
 
const _running = process.argv[1];
if (_running?.endsWith('live-status.mjs') || _running?.endsWith('live-status.mjs/')) {
  enterLiveRoot();
  statusCli();
}