Skip to content

an open-source alternative to the dots, bots and muses of the world, run an office of claude code/codex like agents on your laptop, sandboxes or anywhere, uses your existing subscriptions

fix(worker-wake): activity means a turn — the zero-token session-start sample is not one

Gastón Péchieu2b67b2991a08
diff --git a/src/main/index.ts b/src/main/index.ts
index 5b9a0511..66dc140c 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -64,7 +64,7 @@ import { validateBaseUrl, buildAuthHeaders, resolveUpstreamUrl, secretRefFor, IN
import { RosterStore } from './roster';
import { buildWorkerLaunch } from './workerLaunch';
import { ControlRegistry } from './control';
-import { WorkerWakeWatchdog, WORKER_WAKE_REPORT_MS, type WorkerWakeFacts } from './workerWake';
+import { WorkerWakeWatchdog, WORKER_WAKE_REPORT_MS, activityEvidenceAt, type WorkerWakeFacts } from './workerWake';
import { inboxNudgeText } from '../shared/hiveNudge';
import { resolveGodName } from '../shared/godIdentity';
import { fetchHireManifest, readHireManifestFiles } from './hire';
@@ -5164,7 +5164,9 @@ function runWorkerWakeBeat(): void {
autoDeliveryPaused: snap.autoDeliveryPaused,
paused: snap.paused,
halted: snap.halted,
- lastActivityAt: telemetry.getAgentUsage(agentId)?.ts ?? 0,
+ // A turn the CLI demonstrably took: a tool span, or a usage sample WITH
+ // tokens. The zero-token sample stamped at session start is not one.
+ lastActivityAt: activityEvidenceAt({ usage: telemetry.getAgentUsage(agentId), spans: telemetry.getSpans(agentId) }),
oldestMailAt
});
}
diff --git a/src/main/workerWake.ts b/src/main/workerWake.ts
index d31bbd75..166ac3ee 100644
--- a/src/main/workerWake.ts
+++ b/src/main/workerWake.ts
@@ -126,6 +126,30 @@ export function isStalledWorker(f: WorkerWakeFacts, now = Date.now()): boolean {
return (f.lastActivityAt ?? 0) < mailAt;
}
+/** The subset of telemetry the activity rule reads. Structural so the beat can
+ * hand it the collector's own types and tests can hand it literals. */
+export interface ActivityEvidence {
+ /** The agent's latest usage sample (cumulative counters, ts = last update). */
+ usage?: { ts: number; input: number; output: number } | null;
+ /** Tool spans the agent has run, in arrival order. */
+ spans?: ReadonlyArray<{ ts: number }> | null;
+}
+
+/** When the CLI last demonstrably did a turn, or 0 when it never has.
+ *
+ * A usage sample only counts when it carries tokens: the collector stamps a
+ * sample at session start with every counter at zero, and a boot-time sample
+ * is exactly what a worker that never took its first turn has. A tool span is
+ * always a turn. Observed live 2026-09-07: a worker with 0 tokens, no tool and
+ * no transcript read as "last activity 63s ago" and was held as mid-turn. */
+export function activityEvidenceAt(ev: ActivityEvidence): number {
+ const u = ev.usage;
+ const worked = u && (Number(u.input) || 0) + (Number(u.output) || 0) > 0 ? Number(u.ts) || 0 : 0;
+ let span = 0;
+ for (const s of ev.spans ?? []) if (s && Number(s.ts) > span) span = Number(s.ts);
+ return Math.max(worked, span);
+}
+
export class WorkerWakeWatchdog {
/** ptyId → spawn timestamp (boot grace). */
private spawnedAt = new Map<string, number>();
diff --git a/test/worker-wake-stall.test.cjs b/test/worker-wake-stall.test.cjs
index fd0f2c8c..9e8e652c 100644
--- a/test/worker-wake-stall.test.cjs
+++ b/test/worker-wake-stall.test.cjs
@@ -17,6 +17,7 @@ const loadTs = require('./load-ts.cjs');
const {
WorkerWakeWatchdog,
isStalledWorker,
+ activityEvidenceAt,
WORKER_WAKE_IDLE_MS,
WORKER_WAKE_STALL_MS,
WORKER_WAKE_COOLDOWN_MS,
@@ -57,6 +58,26 @@ test('isStalledWorker: old mail with no activity since it landed', () => {
assert.equal(isStalledWorker(chatty({ oldestMailAt: mailAt, lastActivityAt: 0, inboxIds: [] }), NOW), false, 'no mail, nothing to stall on');
});
+test('activityEvidenceAt: only a turn counts — a zero-token sample at session start does not', () => {
+ assert.equal(activityEvidenceAt({}), 0);
+ assert.equal(activityEvidenceAt({ usage: null, spans: [] }), 0);
+ assert.equal(activityEvidenceAt({ usage: { ts: 5_000, input: 0, output: 0 } }), 0, 'the boot-time sample');
+ assert.equal(activityEvidenceAt({ usage: { ts: 5_000, input: 12, output: 0 } }), 5_000);
+ assert.equal(activityEvidenceAt({ usage: { ts: 5_000, input: 0, output: 3 } }), 5_000);
+ assert.equal(activityEvidenceAt({ usage: { ts: 5_000, input: 0, output: 0 }, spans: [{ ts: 7_000 }, { ts: 6_000 }] }), 7_000, 'a tool span is always a turn');
+ assert.equal(activityEvidenceAt({ usage: { ts: 9_000, input: 1, output: 1 }, spans: [{ ts: 7_000 }] }), 9_000);
+});
+
+test('a worker whose only "activity" is the zero-token boot sample is stalled (the worker-holly case)', () => {
+ const w = watchdog();
+ const mailAt = NOW - 2 * 60_000;
+ const bootSample = { ts: mailAt + 1_000, input: 0, output: 0 }; // stamped AFTER the mail
+ const f = chatty({ oldestMailAt: mailAt, lastActivityAt: activityEvidenceAt({ usage: bootSample, spans: [] }) });
+ assert.equal(f.lastActivityAt, 0);
+ assert.equal(w.explain(f, NOW), null);
+ assert.deepEqual(w.decide([f], NOW), ['stanley']);
+});
+
test('a stalled worker is nudged even though its terminal is chatty (the 17-minute case)', () => {
const w = watchdog();
const f = chatty({ oldestMailAt: NOW - 17 * 60_000, lastActivityAt: 0 });