Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

perf(grid viewer): read a remote grid once per poll (#347)

Kelvin3163cbec106d
diff --git a/store/agents/autonomous-grid/README.md b/store/agents/autonomous-grid/README.md
index aa4d99ca4..afe9ebd6d 100644
--- a/store/agents/autonomous-grid/README.md
+++ b/store/agents/autonomous-grid/README.md
@@ -49,8 +49,9 @@ Remote telemetry does not by itself grant administrative access to each serving
exit codes over local, paired Harness, or SSH execution. Each operation updates a small workspace record.
Harness connections use Node 22's WebSocket and bounded, noninteractive commands; use the machine's
terminal for interactive sign-in. No remote command is automatically retried after a lost connection.
-- `viewer.mjs` polls `info`, `engines`, `models`, remote `stats`, and managed hosts' `device-info`.
- It serves a loopback viewer and SSE updates. Its only writes are the grid to look at and **Wake
+- `viewer.mjs` polls `info`, `engines`, `models`, and managed hosts' `device-info`. A remote grid is
+ read once per poll: `stats` carries the `engines` and `models` listings (an older `grid` is asked
+ for them separately). It serves a loopback viewer and SSE updates. Its only writes are the grid to look at and **Wake
now** on a resting grid, which asks the Harness daemon to start it (`lib/wake.mjs`); remote reads
carry `--no-wake`, so nothing else the viewer does starts a grid. No credential files, full CLI
output, or arbitrary workspace files are exposed to the browser.
diff --git a/store/agents/autonomous-grid/lib/telemetry.mjs b/store/agents/autonomous-grid/lib/telemetry.mjs
index 4068ae7da..21eafcae2 100644
--- a/store/agents/autonomous-grid/lib/telemetry.mjs
+++ b/store/agents/autonomous-grid/lib/telemetry.mjs
@@ -86,9 +86,12 @@ export function assemble(config, reads, previous = null, observedAt = now(), hol
const scope = JSON.stringify([config.mode, config.grid, config.controller]);
if (previous?.scope !== scope) previous = null;
const sources = Object.fromEntries(Object.entries(reads).map(([name, result]) => [name, { ok: result.ok, error: result.ok ? null : result.error, observedAt }]));
- // Asleep is an answer, not a failed read: a member learns it from engines' refusal, and reporting
- // that as a broken source would put an error finding in the agent's verdict for a grid at rest.
- if (hold === 'asleep' && sources.engines) sources.engines = { ok: true, error: null, observedAt };
+ // Asleep is an answer, not a failed read: a member learns it from the refusal of a grid read, and
+ // reporting that as a broken source would put an error finding in the agent's verdict for a grid at rest.
+ if (hold === 'asleep') for (const [name, result] of Object.entries(reads)) if (result.refusal === ASLEEP_CODE) sources[name] = { ok: true, error: null, observedAt };
+ // Too old to read without waking: the one refused read stands for the whole reading, under the
+ // `engines` source the notice goes on below — not beside it as a raw usage line.
+ if (hold === 'updating') for (const [name, result] of Object.entries(reads)) if (result.outdated) delete sources[name];
const info = reads.info?.ok && reads.info.value && typeof reads.info.value === 'object' ? reads.info.value : {};
const stats = reads.stats?.ok && reads.stats.value && typeof reads.stats.value === 'object' ? reads.stats.value : {};
const modelNames = new Map(objects(reads.models?.value).map(m => [text(m.model).toLowerCase(), text(m.model)]).filter(([a,b]) => a && b));
@@ -148,23 +151,33 @@ export function assemble(config, reads, previous = null, observedAt = now(), hol
}
/**
- * A remote grid's reads, in the order that lets a sleeping grid be SEEN without being started.
+ * A remote grid's reads, in the order that lets a sleeping grid be SEEN without being started — and
+ * read ONCE per poll.
*
* `info` first: it asks the control plane, never the grid, and for the grid's owner its status says
- * `asleep` outright — then nothing else is read. A member is shown no status, so `engines` goes next,
- * alone, with NO_WAKE: a sleeping grid refuses it with ASLEEP_CODE, and models and stats would only be
- * refused the same way. A `grid` too old for NO_WAKE refuses engines outright (exit 2); the others would
- * be refused too, and running them WITHOUT the flag is exactly the wake this exists to stop — so the
- * poll ends there and the last reading stays up. Only an engines answer earns models and stats.
+ * `asleep` outright — then nothing else is read. A member is shown no status, so `stats` goes next,
+ * alone, with NO_WAKE: a sleeping grid refuses it with ASLEEP_CODE. A `grid` too old for NO_WAKE refuses
+ * it outright (exit 2); the others would be refused too, and running them WITHOUT the flag is exactly the
+ * wake this exists to stop — so the poll ends there and the last reading stays up.
+ *
+ * `stats --json` carries `grid engines --json` and `grid models --json` under `listings`, out of the one
+ * overview read it made (autonomous-grid `cli/remote_stats.py`; ⚠️ a cross-repo contract, pinned by that
+ * repository's `tests/test_grid_reads_lockstep.py`). A `grid` older than the listings is read the old
+ * way: engines alone, and only an engines answer earns models.
*/
async function readRemote(read) {
const reads = { info: await read('info') };
if (reads.info.ok && reads.info.value?.status === ASLEEP_STATE) return { reads, hold: 'asleep' };
+ reads.stats = await read('stats', [NO_WAKE]);
+ if (reads.stats.refusal === ASLEEP_CODE) return { reads, hold: 'asleep' };
+ if (reads.stats.outdated) return { reads, hold: 'updating' };
+ const listings = reads.stats.ok ? reads.stats.value?.listings : null;
+ if (Array.isArray(listings?.engines) && Array.isArray(listings?.models)) {
+ return { reads: { ...reads, engines: { ok: true, value: listings.engines }, models: { ok: true, value: listings.models } }, hold: null };
+ }
reads.engines = await read('engines', [NO_WAKE]);
if (reads.engines.refusal === ASLEEP_CODE) return { reads, hold: 'asleep' };
- if (reads.engines.outdated) return { reads, hold: 'updating' };
- const [models, stats] = await Promise.all([read('models', [NO_WAKE]), read('stats', [NO_WAKE])]);
- return { reads: { ...reads, models, stats }, hold: null };
+ return { reads: { ...reads, models: await read('models', [NO_WAKE]) }, hold: null };
}
/** A LAN grid has no proxy and nothing to wake, so its reads are asked together as they always were —
diff --git a/store/agents/autonomous-grid/test/nowake.test.mjs b/store/agents/autonomous-grid/test/nowake.test.mjs
index cfbfa24fd..359926d10 100644
--- a/store/agents/autonomous-grid/test/nowake.test.mjs
+++ b/store/agents/autonomous-grid/test/nowake.test.mjs
@@ -48,9 +48,11 @@ test('a grid too old for --no-wake leaves the last reading, stale, and is never
assert.equal(first.nodes.length,earlier.nodes.length);assert.ok(first.nodes.every(n=>n.stale));
assert.match(first.notice,/^Test workstation is being updated — showing the reading from \d{2}:\d{2}$/);
assert.equal(first.readingFrom,'2026-09-24T08:05:00Z');
+ assert.equal(first.sources.stats,undefined,'the refused read is told once, as the notice — not beside it as a raw usage line');
+ assert.equal(first.sources.engines?.error,first.notice);
assert.ok(!first.events.some(e=>e.kind==='offline'));
const firstCalls=(await logged(log)).filter(c=>READS.includes(verbOf(c.argv)));
- assert.deepEqual(firstCalls.map(c=>verbOf(c.argv)),['engines'],'one refused read, then nothing — no retry and no models/stats');
+ assert.deepEqual(firstCalls.map(c=>verbOf(c.argv)),['stats'],'one refused read, then nothing — no retry and no engines/models');
process.env.HARNESS_GRID_BIN=current;
const second=await collect();
@@ -96,10 +98,10 @@ test('the first reading after a sleep does not call a node that has not rejoined
assert.ok(!woken.events.some(e=>e.kind==='offline'),'a cold wake is not every other engine leaving');
});
-test('a member grid is asleep when engines answers grid_asleep, and models and stats are skipped',async t=>{
+test('a member grid is asleep when its first grid read answers grid_asleep, and nothing else is asked',async t=>{
const dir=await temporary(t),log=join(dir,'calls.json');
const verbs={...awakeVerbs,info:{stdout:{grid:'test-grid',type:'domain-restricted',status:null,grid_url:'https://relay.example'}},
- engines:{stderr:`${asleepEnvelope}\nGrid test-grid is asleep: this grid is resting\n`,exit:1}};
+ stats:{stderr:`${asleepEnvelope}\nGrid test-grid is asleep: this grid is resting\n`,exit:1}};
const binary=await fakeGrid(dir,'grid-0.3.49',{verbs},log);
const workspace=join(dir,'ws');await atomicJson(join(workspace,'grid-fleet.json'),{...config,machines:[{...config.machines[0],gridBinary:binary}]});
const earlier=assemble(config,reads,null,'2026-09-24T08:05:00Z');
@@ -108,9 +110,9 @@ test('a member grid is asleep when engines answers grid_asleep, and models and s
assert.equal(snapshot.status,'asleep');assert.equal(snapshot.pollIntervalMs,30_000);
assert.ok(snapshot.nodes.length>0&&snapshot.nodes.every(n=>n.stale));
assert.ok(!snapshot.events.some(e=>e.kind==='offline'));
- assert.equal(snapshot.sources.engines?.ok,true,'asleep is an answer, not a failed read');
+ assert.equal(snapshot.sources.stats?.ok,true,'asleep is an answer, not a failed read');
const read=(await logged(log)).map(c=>c.argv).filter(a=>READS.includes(verbOf(a)));
- assert.deepEqual(read,[['--remote','engines','test-grid','--no-wake','--json']]);
+ assert.deepEqual(read,[['--remote','stats','test-grid','--no-wake','--json']]);
});
test('the viewer books its next read 30 s out while the grid is asleep, and at its own interval otherwise',async t=>{
diff --git a/store/agents/autonomous-grid/test/oneread.test.mjs b/store/agents/autonomous-grid/test/oneread.test.mjs
new file mode 100644
index 000000000..b99fc1ed7
--- /dev/null
+++ b/store/agents/autonomous-grid/test/oneread.test.mjs
@@ -0,0 +1,90 @@
+// One grid read per poll: `grid stats --json` carries `grid engines --json` and `grid models --json`
+// under `listings`, so a poll reads a remote grid once instead of three times — three overview reads of
+// one payload, and three `grid` processes, were what every open viewer cost. A `grid` older than the
+// listings is read the old way.
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import { mkdtemp, rm } from 'node:fs/promises';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { atomicJson, stateDir } from '../lib/fleet.mjs';
+import { assemble, createCollector } from '../lib/telemetry.mjs';
+import { config, reads, remoteNodes } from './fixtures.mjs';
+import { awakeVerbs, fakeGrid, logged, READS, verbOf } from './fakes.mjs';
+
+const temporary = async t => { const dir = await mkdtemp(join(tmpdir(), 'grid-oneread-')); t.after(() => rm(dir, { recursive:true, force:true })); return dir; };
+const asleepEnvelope = JSON.stringify({ error:{ code:'grid_asleep', message:'Grid test-grid is asleep: this grid is resting', status:503 } });
+const listed = { ...reads.stats.value, listings:{ engines:remoteNodes, models:reads.models.value } };
+
+/** A workspace whose `grid` answers `verbs` (over an awake grid), and the log of every argv it ran. */
+async function workspaceWith(t, verbs) {
+ const dir = await temporary(t), log = join(dir, 'calls.json');
+ const binary = await fakeGrid(dir, 'grid', { verbs:{ ...awakeVerbs, ...verbs } }, log);
+ const workspace = join(dir, 'ws');
+ await atomicJson(join(workspace, 'grid-fleet.json'), { ...config, machines:[{ ...config.machines[0], gridBinary:binary }] });
+ const gridReads = async () => (await logged(log)).map(c => c.argv).filter(argv => READS.includes(verbOf(argv)));
+ return { workspace, gridReads };
+}
+
+test('a grid that lists its engines and models with its stats is read once per poll', async t => {
+ const { workspace, gridReads } = await workspaceWith(t, { stats:{ stdout:listed } });
+
+ const snapshot = await createCollector(workspace)();
+
+ assert.deepEqual(await gridReads(), [['--remote', 'stats', 'test-grid', '--no-wake', '--json']]);
+ const threeReads = assemble(config, { ...reads, info:{ ok:true, value:awakeVerbs.info.stdout } }, null, snapshot.observedAt);
+ assert.equal(snapshot.status, 'live');
+ assert.deepEqual(snapshot.nodes, threeReads.nodes);
+ assert.deepEqual(snapshot.models, threeReads.models);
+ assert.deepEqual(snapshot.summary, threeReads.summary);
+});
+
+test('a grid older than the listings is read the old way, every read still without waking it', async t => {
+ const { workspace, gridReads } = await workspaceWith(t, {});
+
+ const snapshot = await createCollector(workspace)();
+
+ assert.equal(snapshot.status, 'live');
+ assert.equal(snapshot.summary.enginesOnline, 7);
+ const asked = await gridReads();
+ assert.deepEqual(asked.map(verbOf), ['stats', 'engines', 'models']);
+ for (const argv of asked) assert.ok(argv.includes('--no-wake'), argv.join(' '));
+});
+
+test('a member grid is asleep when its one read answers grid_asleep, and nothing else is asked', async t => {
+ const { workspace, gridReads } = await workspaceWith(t, {
+ info:{ stdout:{ grid:'test-grid', type:'domain-restricted', status:null, grid_url:'https://relay.example' } },
+ stats:{ stderr:`${asleepEnvelope}\nGrid test-grid is asleep: this grid is resting\n`, exit:1 },
+ });
+ await atomicJson(join(stateDir(workspace), 'snapshot.json'), assemble(config, reads, null, '2026-09-24T08:05:00Z'));
+
+ const snapshot = await createCollector(workspace)();
+
+ assert.equal(snapshot.status, 'asleep');
+ assert.ok(snapshot.nodes.length > 0 && snapshot.nodes.every(n => n.stale));
+ assert.equal(snapshot.sources.stats?.ok, true, 'asleep is an answer, not a failed read');
+ assert.deepEqual((await gridReads()).map(verbOf), ['stats']);
+});
+
+test('an older grid that falls asleep between two reads is shown asleep, and models is not asked', async t => {
+ const { workspace, gridReads } = await workspaceWith(t, {
+ engines:{ stderr:`${asleepEnvelope}\nGrid test-grid is asleep: this grid is resting\n`, exit:1 },
+ });
+
+ const snapshot = await createCollector(workspace)();
+
+ assert.equal(snapshot.status, 'asleep');
+ assert.equal(snapshot.sources.engines?.ok, true);
+ assert.deepEqual((await gridReads()).map(verbOf), ['stats', 'engines']);
+});
+
+test('a stats read that fails for another reason still draws the engines, as a partial reading', async t => {
+ const { workspace, gridReads } = await workspaceWith(t, { stats:{ stderr:'Could not reach grid test-grid: timed out\n', exit:1 } });
+
+ const snapshot = await createCollector(workspace)();
+
+ assert.equal(snapshot.status, 'partial');
+ assert.equal(snapshot.summary.enginesOnline, 7);
+ assert.equal(snapshot.sources.stats?.ok, false);
+ assert.deepEqual((await gridReads()).map(verbOf), ['stats', 'engines', 'models']);
+});