Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ The `roundtable` command (the package bin; from a clone, `bin/roundtable`) is th
| Command | Purpose |
| --- | --- |
| `roundtable paths [--json]` | Prints the resolved project paths; the machine contract for adapters |
| `roundtable status [--repo <path>]` | Prints the heartbeat summary; exits 1 when there is no valid heartbeat |
| `roundtable doctor` | Prints the doctor report; creates no files |
| `roundtable run` | Runs the Roundtable loop in the foreground, with external state only |
| `roundtable migrate --repo <path>` | Moves an older in-repo `.roundtable/state/` to the external layout |
Expand Down
33 changes: 33 additions & 0 deletions runtime/lib/cli.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { spawn } from 'node:child_process';
import { readFile } from 'node:fs/promises';
import { homedir } from 'node:os';
import { join, relative, resolve } from 'node:path';
import { parseArgs } from 'node:util';
Expand All @@ -7,13 +8,15 @@ import { doctorReport } from './doctor.mjs';
import { initLoopHome } from './init.mjs';
import { migrateProject } from './migrate.mjs';
import { resolveProjectPaths } from './project-paths.mjs';
import { parseHeartbeat } from '../supervisor/heartbeat.mjs';
import { installSkill, uninstallSkill } from './skill-install.mjs';

// The rtd entry the `rtd` service unit runs (bin/rtd: `node runtime/rtd.mjs --work-root <root> --github --loop ...`).
const RTD = fileURLToPath(new URL('../rtd.mjs', import.meta.url));
const USAGE = `usage: roundtable <command> [--repo <path>]
init [--repo <path>] create the loop home and limits file, then check gh, pi, codex and free disk
paths [--json] print the resolved project paths
status [--repo <path>] print the heartbeat summary of the running loop
doctor [--json] print the doctor report (writes nothing)
run [--repo <path>] [-- <rtd args>] run the rtd loop in the foreground
migrate [--repo <path>] move a legacy in-repo layout to the external layout
Expand Down Expand Up @@ -65,6 +68,36 @@ export async function main(argv, deps = {}) {
stdout.write(v.json ? `${JSON.stringify({ lines, failed })}\n` : lines.map((line) => `${line}\n`).join(''));
return failed ? 1 : 0;
},
async status() {
const { heartbeatFile } = await paths();
let text;
try { text = await readFile(heartbeatFile, 'utf8'); }
catch (err) { if (err.code !== 'ENOENT') throw err; }
const hb = parseHeartbeat(text);
const where = heartbeatFile.replaceAll(home, '~');
if (!hb.ok) {
stderr.write(`roundtable status: ${hb.reason === 'missing' ? `no heartbeat at ${where}` : `invalid heartbeat at ${where}: ${hb.reason}${hb.field ? ` (${hb.field})` : ''}`}\n`);
return 1;
}
const { value: h } = hb;
const times = [['writtenAt', h.writtenAt], ['daemon.lastCycleAt', h.daemon.lastCycleAt],
['frontChief.lastTurnAt', h.frontChief.lastTurnAt], ['lastMaterialProgress.at', h.lastMaterialProgress?.at]];
const outOfRange = times.find(([, ms]) => ms !== undefined && Number.isNaN(new Date(ms).getTime()));
if (outOfRange) {
stderr.write(`roundtable status: invalid heartbeat at ${where}: invalid-time (${outOfRange[0]})\n`);
return 1;
}
const progress = h.lastMaterialProgress ? `${h.lastMaterialProgress.kind} at ${new Date(h.lastMaterialProgress.at).toISOString()}` : 'none';
stdout.write([
`writtenAt: ${new Date(h.writtenAt).toISOString()}`,
`daemon state: ${h.daemon.state}`,
`daemon lastCycleAt: ${new Date(h.daemon.lastCycleAt).toISOString()}`,
`frontChief state: ${h.frontChief.state}`,
`frontChief lastTurnAt: ${new Date(h.frontChief.lastTurnAt).toISOString()}`,
`lastMaterialProgress: ${progress}`,
].map((line) => `${line}\n`).join(''));
return 0;
},
async run() {
const resolved = await paths();
const external = resolved.layout === 'external';
Expand Down
59 changes: 59 additions & 0 deletions test/roundtable-cli.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,62 @@ test('run launches the rtd entry and exits with its code', async (t) => {
assert.equal(r.status, 2);
assert.match(r.stderr, /usage: rtd\.mjs --work-root/);
});

async function writeHeartbeatAt(s, text) {
const paths = JSON.parse(roundtable(s.env, ['paths', '--json', '--repo', s.repo]).stdout);
await mkdir(join(paths.heartbeatFile, '..'), { recursive: true });
await writeFile(paths.heartbeatFile, text);
}

test('status prints the heartbeat summary and exits 0 for a valid heartbeat', async (t) => {
const s = await sandbox(t);
const hb = {
schemaVersion: 1,
writtenAt: 1800000000000,
daemon: { pid: 1, lastCycleAt: 1800000000000, state: 'running' },
frontChief: { session: 'x', lastTurnAt: 1800000000000, state: 'idle' },
lastMaterialProgress: { kind: 'commit', ref: 'abc', at: 1800000000000 },
};
await writeHeartbeatAt(s, JSON.stringify(hb));
const r = roundtable(s.env, ['status', '--repo', s.repo]);
assert.equal(r.status, 0, r.stderr);
assert.match(r.stdout, /^writtenAt: 2027-01-15T08:00:00\.000Z$/m);
assert.match(r.stdout, /^daemon state: running$/m);
assert.match(r.stdout, /^frontChief state: idle$/m);
assert.match(r.stdout, /^lastMaterialProgress: commit at 2027-01-15T08:00:00\.000Z$/m);
});

test('status with no heartbeat is a one-line stderr message and exit 1', async (t) => {
const s = await sandbox(t);
const r = roundtable(s.env, ['status', '--repo', s.repo]);
assert.equal(r.status, 1);
assert.equal(r.stdout, '');
assert.match(r.stderr, /^roundtable status: no heartbeat at \S+\n$/);
assert.doesNotMatch(r.stderr, /\n\s+at /);
});

test('status with an invalid heartbeat names the reason and exit 1', async (t) => {
const s = await sandbox(t);
await writeHeartbeatAt(s, JSON.stringify({ schemaVersion: 1, writtenAt: 'soon' }));
const r = roundtable(s.env, ['status', '--repo', s.repo]);
assert.equal(r.status, 1);
assert.match(r.stderr, /^roundtable status: invalid heartbeat at \S+: invalid-schema \(writtenAt\)\n$/);
assert.doesNotMatch(r.stderr, /\n\s+at /);
});

test('status with a heartbeat time outside the Date range names the field and exit 1', async (t) => {
const s = await sandbox(t);
const hb = {
schemaVersion: 1,
writtenAt: 1e20,
daemon: { pid: 1, lastCycleAt: 1800000000000, state: 'running' },
frontChief: { session: 'x', lastTurnAt: 1800000000000, state: 'idle' },
lastMaterialProgress: null,
};
await writeHeartbeatAt(s, JSON.stringify(hb));
const r = roundtable(s.env, ['status', '--repo', s.repo]);
assert.equal(r.status, 1);
assert.equal(r.stdout, '');
assert.match(r.stderr, /^roundtable status: invalid heartbeat at \S+: invalid-time \(writtenAt\)\n$/);
assert.doesNotMatch(r.stderr, /\n\s+at /);
});
Loading