@commonlyai/cli 0.1.216 → 0.1.217

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commonlyai/cli",
3
- "version": "0.1.216",
3
+ "version": "0.1.217",
4
4
  "license": "Apache-2.0",
5
5
  "description": "The Commonly CLI — connect agents, manage pods, iterate fast",
6
6
  "type": "module",
@@ -38,6 +38,12 @@ import {
38
38
  } from '../lib/daemon-service.js';
39
39
  import { daemonLogsDir, daemonSeatLogPath, readLogTail } from '../lib/daemon-logs.js';
40
40
  import { loadDaemonState, saveDaemonState } from '../lib/daemon-state.js';
41
+ import {
42
+ CLEAR_SCREEN, renderDaemonWatch, shouldRedraw, WATCH_INTERVAL_MS,
43
+ } from '../lib/daemon-watch.js';
44
+ import {
45
+ createReachCache, loadPodReach, readSessionLastTurn,
46
+ } from '../lib/daemon-agent-states.js';
41
47
  import { installedCliVersion, startUpdateNotice, UPDATE_CHECK_OPT_OUT_ENV } from '../lib/update-check.js';
42
48
 
43
49
  // One supervised seat, as `commonly daemon status --verbose` names it. Two of these
@@ -58,6 +64,90 @@ export const formatSeatLine = (seat = {}) => {
58
64
  return ` ${seat.agentName}: ${seat.state} instance=${seat.instanceId || '-'} adapter=${seat.adapter || 'unknown'} model=${model}${effort} pid=${seat.pid || '-'} restarts=${restarts} lastTurn=${seat.lastTurnAt || 'never'}${error}`;
59
65
  };
60
66
 
67
+ // `daemon status --watch`: one frame, then a redraw every 2 s on a terminal. Off a
68
+ // terminal, or with NO_COLOR set, it prints one frame and returns. A failed
69
+ // fetch is shown in the frame, so the loop keeps running through a brief outage.
70
+ const watchDaemonStatus = async () => {
71
+ const redraw = shouldRedraw({ isTTY: process.stdout.isTTY });
72
+ // Each agent's pod comes from its token record. The verdict for a pod is read
73
+ // through the operator's login and cached for 10 s, so the 2 s frame does not
74
+ // re-read it. A failed read is thrown, so the next frame retries.
75
+ const reachFor = createReachCache({
76
+ load: (podId) => loadPodReach({
77
+ client: createClient({ instance: requireDaemonRecord().instanceUrl }),
78
+ podId,
79
+ }),
80
+ });
81
+ const frame = async () => {
82
+ try {
83
+ const record = requireDaemonRecord();
84
+ // A failed machine read still shows the local seats and logs, with the
85
+ // machine marked unreachable, so the operator can see what is running here.
86
+ let machine;
87
+ let machineNote = '';
88
+ try {
89
+ machine = await getDaemonMachineStatus({
90
+ client: createClient({ instance: record.instanceUrl, token: record.daemonToken }),
91
+ record,
92
+ });
93
+ } catch (error) {
94
+ machine = { name: record.machineName, status: 'unreachable', lastSeenAt: null };
95
+ machineNote = `Machine status unavailable (${error.message}): showing local seats only.`;
96
+ }
97
+ const seats = (loadDaemonState()?.seats || []).map((seat) => ({
98
+ ...seat,
99
+ // The supervisor stamps spawn time; the session store holds real turns.
100
+ lastTurnAt: readSessionLastTurn(seat.agentName),
101
+ }));
102
+ const logTails = {};
103
+ for (const seat of seats) {
104
+ const tail = readLogTail(daemonSeatLogPath(seat.agentName), 3);
105
+ logTails[seat.agentName] = tail ? tail.split('\n') : [];
106
+ }
107
+ let states = null;
108
+ let stateNote = '';
109
+ if (!getToken(record.instanceUrl)) {
110
+ stateNote = `Not logged in: showing local process state only. Run: commonly login --instance ${record.instanceUrl}`;
111
+ } else {
112
+ const podIds = [...new Set(seats.map((seat) => loadAgentToken(seat.agentName)?.podId).filter(Boolean))];
113
+ try {
114
+ states = await reachFor(podIds);
115
+ } catch (error) {
116
+ stateNote = `Agent states unavailable (${error.message}): showing local process state only`;
117
+ }
118
+ }
119
+ return renderDaemonWatch({
120
+ machine, seats, logTails, states, stateNote: [machineNote, stateNote].filter(Boolean).join(' '), now: Date.now(), width: process.stdout.columns || 80, color: redraw,
121
+ });
122
+ } catch (error) {
123
+ return daemonStatusErrorMessage(error);
124
+ }
125
+ };
126
+ if (!redraw) {
127
+ console.log(await frame());
128
+ return;
129
+ }
130
+ let busy = false;
131
+ const tick = async () => {
132
+ if (busy) return;
133
+ busy = true;
134
+ try {
135
+ process.stdout.write(`${CLEAR_SCREEN}${await frame()}\n`);
136
+ } finally {
137
+ busy = false;
138
+ }
139
+ };
140
+ await tick();
141
+ const timer = setInterval(tick, WATCH_INTERVAL_MS);
142
+ const stop = () => {
143
+ clearInterval(timer);
144
+ process.stdout.write('\n');
145
+ process.exit(0);
146
+ };
147
+ process.once('SIGINT', stop);
148
+ process.once('SIGTERM', stop);
149
+ };
150
+
61
151
  const requireDaemonRecord = () => {
62
152
  const record = loadDaemonRecord();
63
153
  if (!record) {
@@ -571,7 +661,12 @@ Examples:
571
661
  .command('status')
572
662
  .description('Show the server-derived liveness of this machine')
573
663
  .option('--verbose', 'Include locally supervised seat state and process details')
664
+ .option('--watch', 'Redraw the machine and agent view every 2 s until Ctrl-C')
574
665
  .action(async (opts) => {
666
+ if (opts.watch) {
667
+ await watchDaemonStatus();
668
+ return;
669
+ }
575
670
  try {
576
671
  const record = requireDaemonRecord();
577
672
  const machine = await getDaemonMachineStatus({
@@ -0,0 +1,95 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { homedir } from 'node:os';
3
+ import { join } from 'node:path';
4
+
5
+ // The server's verdict for each agent, as `daemon status --watch` shows it. The
6
+ // verdict is deriveAgentState's (backend/services/agentStateService.ts), the
7
+ // same one the Your Team row and the TASK-655 never-connected line use, so the
8
+ // watch and the product name an agent's state the same way. It is read through
9
+ // the operator's login, because the daemon's own credential cannot call the
10
+ // per-pod agent-states route. Each agent's pod comes from its token record.
11
+
12
+ // The reach states mapped to the words the watch prints. `reachable` is a
13
+ // native or push-webhook agent the server always treats as up.
14
+ const LABEL = {
15
+ listening: 'running',
16
+ reachable: 'running',
17
+ 'gone-dark': 'stalled',
18
+ 'never-connected': 'not connected',
19
+ };
20
+
21
+ // When one agent is installed in several pods, the most alive verdict wins.
22
+ const RANK = ['running', 'stalled', 'not connected', 'unknown'];
23
+
24
+ export const reachLabel = (reach) => LABEL[reach] || 'unknown';
25
+
26
+ export const seatKey = (agentName, instanceId) => (
27
+ `${String(agentName || '').toLowerCase()}:${String(instanceId || 'default').toLowerCase()}`
28
+ );
29
+
30
+ const moreAlive = (a, b) => (RANK.indexOf(a) <= RANK.indexOf(b) ? a : b);
31
+
32
+ // Reads one pod's agents and returns Map<seatKey, label>. `client` is a
33
+ // user-token client.
34
+ export const loadPodReach = async ({ client, podId }) => {
35
+ const body = await client.get(`/api/pods/${podId}/agent-states`);
36
+ const byAgent = new Map();
37
+ for (const agent of body?.agents || []) {
38
+ byAgent.set(seatKey(agent.agentName, agent.instanceId), reachLabel(agent.state));
39
+ }
40
+ return byAgent;
41
+ };
42
+
43
+ // The server allows 60 agent-states reads a minute per IP. Each pod is refreshed
44
+ // once per TTL, and the TTL stretches with the pod count, so N pods cost about 50
45
+ // reads a minute whatever N is: 1.2 s per pod at least, and never below `ttlMs`.
46
+ const PER_POD_SPACING_MS = 1_200;
47
+
48
+ // Caches each pod's verdict, so the 2 s watch frame does not re-read every pod.
49
+ // A failed refresh keeps the pod's last verdict and waits one TTL before trying
50
+ // again, so a 429 is neither discarded nor retried every frame. A pod with no
51
+ // earlier verdict throws, and the next call retries it. `load(podId)` returns
52
+ // Map<seatKey, label>. Returns `reachFor(podIds)`, which merges the pods' maps.
53
+ export const createReachCache = ({ load, ttlMs = 10_000, now = Date.now }) => {
54
+ const cached = new Map();
55
+ const reachFor = async (podIds) => {
56
+ const ttl = Math.max(ttlMs, podIds.length * PER_POD_SPACING_MS);
57
+ const merged = new Map();
58
+ for (const podId of podIds) {
59
+ const hit = cached.get(podId);
60
+ let agents;
61
+ if (hit && now() - hit.loadedAt < ttl) {
62
+ agents = hit.agents;
63
+ } else {
64
+ try {
65
+ agents = await load(podId);
66
+ } catch (error) {
67
+ if (!hit) throw error;
68
+ agents = hit.agents;
69
+ }
70
+ cached.set(podId, { agents, loadedAt: now() });
71
+ }
72
+ for (const [key, label] of agents) {
73
+ merged.set(key, merged.has(key) ? moreAlive(merged.get(key), label) : label);
74
+ }
75
+ }
76
+ return merged;
77
+ };
78
+ return reachFor;
79
+ };
80
+
81
+ // The agent's last supervised turn, from its session store, the record
82
+ // `agent list` reads. The most recent entry across pods, or null. The
83
+ // supervisor's own stamp is the spawn time, so the watch does not use it.
84
+ export const readSessionLastTurn = (name, { home = homedir() } = {}) => {
85
+ try {
86
+ const sessions = JSON.parse(readFileSync(join(home, '.commonly', 'sessions', `${name}.json`), 'utf8'));
87
+ let latest = null;
88
+ for (const entry of Object.values(sessions || {})) {
89
+ if (entry?.lastTurn && (!latest || entry.lastTurn > latest)) latest = entry.lastTurn;
90
+ }
91
+ return latest;
92
+ } catch {
93
+ return null;
94
+ }
95
+ };
@@ -0,0 +1,115 @@
1
+ // The live view behind `commonly daemon status --watch`: the machine, then one
2
+ // block per supervised agent with its state, last turn, last error and the tail
3
+ // of its log. Rendering is a pure function of its inputs so the layout can be
4
+ // tested at fixed widths; the redraw loop lives in the command.
5
+
6
+ import { seatKey } from './daemon-agent-states.js';
7
+
8
+ export const WATCH_INTERVAL_MS = 2000;
9
+
10
+ const ANSI = {
11
+ dim: '\x1b[2m',
12
+ bold: '\x1b[1m',
13
+ reset: '\x1b[0m',
14
+ };
15
+
16
+ // A redraw clears the screen and returns the cursor home. Plain ANSI, no
17
+ // dependency; the caller decides whether to use it at all.
18
+ export const CLEAR_SCREEN = '\x1b[H\x1b[2J';
19
+
20
+ // The view is for a person watching a terminal. Anything else gets one print.
21
+ export const shouldRedraw = ({ isTTY, env = process.env } = {}) => (
22
+ Boolean(isTTY) && !(env && env.NO_COLOR !== undefined && env.NO_COLOR !== '')
23
+ );
24
+
25
+ const relative = (iso, now) => {
26
+ if (!iso) return 'never';
27
+ const ms = now - new Date(iso).getTime();
28
+ if (!Number.isFinite(ms)) return 'unknown';
29
+ if (ms < 60_000) return `${Math.max(0, Math.round(ms / 1000))}s ago`;
30
+ if (ms < 3_600_000) return `${Math.round(ms / 60_000)}m ago`;
31
+ return `${Math.round(ms / 3_600_000)}h ago`;
32
+ };
33
+
34
+ // Terminal control sequences in a log or state field would reach the operator's
35
+ // terminal: a line could clear the watch, set the title, or write the clipboard.
36
+ // Strip OSC and CSI sequences, bare escapes, and C0/C1 controls before anything
37
+ // else. Tabs become spaces so the column count stays honest.
38
+ // eslint-disable-next-line no-control-regex -- matching control characters is the point
39
+ const OSC = /\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)?/g;
40
+ // eslint-disable-next-line no-control-regex -- matching control characters is the point
41
+ const CSI = /\x1b\[[0-?]*[ -/]*[@-~]/g;
42
+ // eslint-disable-next-line no-control-regex -- matching control characters is the point
43
+ const ESC = /\x1b[@-Z\\-_]/g;
44
+ // eslint-disable-next-line no-control-regex -- matching control characters is the point
45
+ const CONTROL = /[\x00-\x08\x0a-\x1f\x7f-\x9f]/g;
46
+ export const stripControls = (text) => String(text ?? '')
47
+ .replace(OSC, '').replace(CSI, '').replace(ESC, '').replace(CONTROL, '').replace(/\t/g, ' ');
48
+
49
+ // Credential shapes the CLI knows, plus the forms that carry a credential in a
50
+ // log without a known prefix: `Authorization: Basic …`, and a quoted or bare
51
+ // `token`/`password`/`secret`/`api_key` field. Generic key/value redaction can
52
+ // over-match, which is the cheaper error for a watch that prints logs.
53
+ const CREDENTIAL = [
54
+ [/cm_(?:daemon|agent|user)_[A-Za-z0-9_-]+/g, '[redacted]'],
55
+ [/\bBearer\s+\S+/gi, '[redacted]'],
56
+ [/\bBasic\s+[A-Za-z0-9+/=._-]+/g, '[redacted]'],
57
+ [/(\bauthorization\s*[:=]\s*)(?:(?:basic|bearer|digest|token)\s+)?\S+/gi, '$1[redacted]'],
58
+ [/(["']?\b(?:token|password|passwd|secret|api[_-]?key|access[_-]?token|refresh[_-]?token)\b["']?\s*[:=]\s*)(["']?)[^\s"',}&]+\2/gi, '$1$2[redacted]$2'],
59
+ ];
60
+ const redact = (text) => CREDENTIAL.reduce((value, [shape, replacement]) => value.replace(shape, replacement), text);
61
+
62
+ // Every field the watch prints goes through here: control sequences out,
63
+ // credentials redacted, then clipped to the width.
64
+ const clip = (text, width) => {
65
+ const value = redact(stripControls(text));
66
+ return value.length <= width ? value : `${value.slice(0, Math.max(0, width - 1))}…`;
67
+ };
68
+
69
+ // A log line can carry a credential or a whole prompt; the same boundary covers it.
70
+ export const safeLogLine = (line, width) => clip(line, width);
71
+
72
+ // The state is the server's verdict, read by daemon-agent-states. `states` is
73
+ // that Map. Without it (no login, or the read failed) the seat shows its local
74
+ // process state, labelled `local`, and never a server word. An agent the verdict
75
+ // does not list has no installation on its pod.
76
+ export const agentStateLabel = (seat = {}, states = null) => {
77
+ if (!states) return `local ${seat.state || 'unknown'}`;
78
+ return states.get(seatKey(seat.agentName, seat.instanceId)) || 'not registered';
79
+ };
80
+
81
+ const agentBlock = (seat, { now, width, color, logTail, states }) => {
82
+ const dim = (text) => (color ? `${ANSI.dim}${text}${ANSI.reset}` : text);
83
+ const bold = (text) => (color ? `${ANSI.bold}${text}${ANSI.reset}` : text);
84
+ const lines = [
85
+ `${bold(clip(seat.agentName || 'unknown', width))} ${agentStateLabel(seat, states)} last turn ${relative(seat.lastTurnAt, now)}`,
86
+ ];
87
+ if (seat.lastError) lines.push(clip(` error: ${seat.lastError}`, width));
88
+ for (const line of logTail || []) lines.push(dim(` ${safeLogLine(line, Math.max(10, width - 2))}`));
89
+ return lines;
90
+ };
91
+
92
+ // Inputs: `machine` { name, status, lastSeenAt }, `seats` (from the daemon state
93
+ // file), `logTails` { [agentName]: string[] }, `states` (Map from
94
+ // loadReachByAgent, or null), `stateNote` (why states is null), `now` (ms),
95
+ // `width` (columns).
96
+ export const renderDaemonWatch = ({
97
+ machine = {}, seats = [], logTails = {}, states = null, stateNote = '', now = Date.now(), width = 80, color = false,
98
+ } = {}) => {
99
+ const bold = (text) => (color ? `${ANSI.bold}${text}${ANSI.reset}` : text);
100
+ const out = [
101
+ bold(clip(`${machine.name || 'machine'}: ${machine.status || 'unknown'}`, width)),
102
+ clip(`last heartbeat ${relative(machine.lastSeenAt, now)}`, width),
103
+ ];
104
+ if (stateNote) out.push(clip(stateNote, width));
105
+ out.push('');
106
+ if (!seats.length) {
107
+ out.push('No supervised agents on this machine.');
108
+ return out.join('\n');
109
+ }
110
+ for (const seat of seats) {
111
+ out.push(...agentBlock(seat, { now, width, color, logTail: logTails[seat.agentName] || [], states }));
112
+ out.push('');
113
+ }
114
+ return out.join('\n').replace(/\n+$/, '');
115
+ };