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