Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: doctor warns about Wakeshift forgets that died or wait while reporting back is off; test cron remove

garrytane01b4c4848e6
diff --git a/src/commands/doctor/checks/wakeshift.ts b/src/commands/doctor/checks/wakeshift.ts
index f110ffe83d..fb04e0fdf5 100644
--- a/src/commands/doctor/checks/wakeshift.ts
+++ b/src/commands/doctor/checks/wakeshift.ts
@@ -6,7 +6,9 @@
* terms links (every watch refuses), when watches died (5 failed attempts or a
* final Wakeshift refusal) or hit `id_reused` (a bug signal) in the last 7
* days, when `gbrain wakeshift emit` reported runs of crons nobody registered,
- * when a queued Wakeshift forget is older than a day, and when the published
+ * when a queued Wakeshift forget is older than a day or died in the last 30
+ * days, when forgets are queued while reporting back is off (nothing sends
+ * them then), and when the published
* manifest is older than this build. Shows the key's kid fingerprint only,
* never the key. Guide: docs/guides/wakeshift.md.
*/
@@ -34,7 +36,11 @@ async function runWakeshiftCheck(ctx: DoctorContext): Promise<Check[]> {
if (read.source === 'file_too_open') {
checks.push({ name: 'wakeshift', status: 'warn', message: `${wakeshiftKeyFile()} is readable by other users (mode ${read.mode}); gbrain ignores it until it is 0600. Fix: chmod 600 ${wakeshiftKeyFile()}`, details: { enabled, key_source: read.source, docs: DOCS } });
} else if (!enabled) {
- checks.push({ name: 'wakeshift', status: 'ok', severity: 'info', readiness_state: 'disabled_by_choice', message: 'Reporting back through Wakeshift is off (gbrain wakeshift enable turns it on).', details: { enabled, key_source: read.source, docs: DOCS } });
+ const [queued] = await connectedEngine(ctx).executeRaw<{ n: number }>("SELECT count(*)::int AS n FROM wake_forgets WHERE state = 'pending'");
+ const pending = Number(queued?.n ?? 0);
+ checks.push(pending > 0
+ ? { name: 'wakeshift', status: 'warn', message: `Reporting back through Wakeshift is off, but ${pending} Wakeshift forget(s) for removed members or revoked credentials are queued, and nothing sends them while it is off. Fix: gbrain wakeshift enable (with the key in place), then gbrain serve or the job worker sends them.`, details: { enabled, key_source: read.source, pending_forgets: pending, docs: DOCS } }
+ : { name: 'wakeshift', status: 'ok', severity: 'info', readiness_state: 'disabled_by_choice', message: 'Reporting back through Wakeshift is off (gbrain wakeshift enable turns it on).', details: { enabled, key_source: read.source, docs: DOCS } });
} else if (!kid) {
checks.push({ name: 'wakeshift', status: 'warn', message: `Wakeshift is enabled but has no usable key (${keyProblem}), so the watch tools are hidden. Fix: put the key in ${wakeshiftKeyFile()} (0600) or WAKESHIFT_API_KEY, then run gbrain wakeshift test.`, details: { enabled, key_source: read.source, docs: DOCS } });
} else {
@@ -49,21 +55,25 @@ async function runWakeshiftCheck(ctx: DoctorContext): Promise<Check[]> {
const engine = connectedEngine(ctx);
const members = await liveAccessMembers(engine);
const termsMissing = members > 0 && !operatorTerms(file);
- const [extra] = await engine.executeRaw<{ unregistered: string[] | null; stale_forgets: number }>(
+ const [extra] = await engine.executeRaw<{ unregistered: string[] | null; stale_forgets: number; dead_forgets: number }>(
`SELECT (SELECT array_agg(DISTINCT r.name ORDER BY r.name) FROM wake_cron_runs r
WHERE r.finished_at > now() - interval '7 days' AND NOT EXISTS (SELECT 1 FROM wake_crons c WHERE c.name = r.name)) AS unregistered,
- (SELECT count(*)::int FROM wake_forgets WHERE state = 'pending' AND created_at < now() - interval '1 day') AS stale_forgets`);
+ (SELECT count(*)::int FROM wake_forgets WHERE state = 'pending' AND created_at < now() - interval '1 day') AS stale_forgets,
+ (SELECT count(*)::int FROM wake_forgets WHERE state = 'dead' AND updated_at > now() - interval '30 days') AS dead_forgets`);
const unregistered = (extra?.unregistered ?? []).slice(0, 10);
const staleForgets = Number(extra?.stale_forgets ?? 0);
+ const deadForgets = Number(extra?.dead_forgets ?? 0);
const manifestStale = file.manifest_hash !== wakeshiftManifestHash();
const details = { enabled, key_source: read.source, kid, watches_7d: counts, live_members: members, terms_set: !termsMissing,
- unregistered_crons_7d: unregistered, stale_forgets: staleForgets, manifest_current: !manifestStale, docs: DOCS };
+ unregistered_crons_7d: unregistered, stale_forgets: staleForgets, dead_forgets_30d: deadForgets, manifest_current: !manifestStale, docs: DOCS };
const message = termsMissing
? `This brain has ${members} live access member(s) but no operator terms links, so every watch refuses and open watches end without a wake. Fix: gbrain wakeshift enable --terms-url <https url> --privacy-url <https url>`
: counts.id_reused! > 0
? `${counts.id_reused} watch(es) were refused with id_reused in the last 7 days: a gbrain bug, so please report it with this output.`
: counts.dead! > 0
? `${counts.dead} wake(s) died in the last 7 days (5 failed attempts or a final Wakeshift refusal); the agents were told the watch ended. Check the key and the app with gbrain wakeshift test.`
+ : deadForgets > 0
+ ? `${deadForgets} Wakeshift forget(s) failed for good in the last 30 days (a final refusal or 5 failed attempts), so those users were not deleted at Wakeshift. Check the key and the app with gbrain wakeshift test.`
: staleForgets > 0
? `${staleForgets} Wakeshift forget(s) for removed members or revoked credentials have waited over a day; gbrain serve or the job worker sends them. Check gbrain wakeshift test, and that one of them runs.`
: unregistered.length > 0
@@ -71,7 +81,7 @@ async function runWakeshiftCheck(ctx: DoctorContext): Promise<Check[]> {
: manifestStale
? 'The published Wakeshift event manifest is older than this build, so agents cannot watch the newer events. Fix: gbrain wakeshift enable'
: `Reporting back through Wakeshift is on (key kid ${kid}); ${counts.open} open watch(es), ${counts.delivered} delivered in the last 7 days.`;
- const warn = termsMissing || counts.id_reused! > 0 || counts.dead! > 0 || staleForgets > 0 || unregistered.length > 0 || manifestStale;
+ const warn = termsMissing || counts.id_reused! > 0 || counts.dead! > 0 || deadForgets > 0 || staleForgets > 0 || unregistered.length > 0 || manifestStale;
checks.push({ name: 'wakeshift', status: warn ? 'warn' : 'ok', message, details });
}
return checks;
diff --git a/test/wakeshift-pr2.serial.test.ts b/test/wakeshift-pr2.serial.test.ts
index 19f53bd178..1780e3296d 100644
--- a/test/wakeshift-pr2.serial.test.ts
+++ b/test/wakeshift-pr2.serial.test.ts
@@ -261,6 +261,17 @@ describe('gbrain.cron.finished', () => {
expect(fake.calls).toHaveLength(0);
});
+ test('cron remove ends the open watches on it and tells Wakeshift, and a later run wakes nobody', async () => {
+ await cli(['cron', 'add', 'nightly-digest']);
+ expect(body(await call('watch_cron', { name: 'nightly-digest' }))).toMatchObject({ watching: true });
+ expect((await cli(['cron', 'remove', 'nightly-digest'])).out).toContain('ended 1 open watch(es)');
+ expect(await engine.executeRaw('SELECT state, last_error FROM wake_subscriptions')).toEqual([{ state: 'ended', last_error: 'cron_removed' }]);
+ expect(paths('/event-subscriptions/end')).toHaveLength(1);
+ await cli(['emit', 'cron.finished', '--name', 'nightly-digest', '--status', 'ok']);
+ expect((await drain()).delivered).toBe(0);
+ expect(emits()).toHaveLength(0);
+ });
+
test('an unregistered emit is recorded with a stderr hint, and doctor names it', async () => {
const r = await cli(['emit', 'cron.finished', '--name', 'typo-cron', '--status', 'ok']);
expect(r.err).toContain('gbrain wakeshift cron add typo-cron');
@@ -417,6 +428,18 @@ describe('status, doctor and the gate', () => {
expect(checks[0]).toMatchObject({ status: 'warn', details: { stale_forgets: 1 } });
});
+ test('doctor warns about a forget that died, and about queued forgets while reporting back is off', async () => {
+ await engine.executeRaw("INSERT INTO wake_forgets (wake_user, state, last_error) VALUES ('0123456789abcdef0123456789abcdef', 'dead', 'bad_request')");
+ const dead = await doctorChecks();
+ expect(dead[0]).toMatchObject({ status: 'warn', details: { dead_forgets_30d: 1 } });
+ expect(dead[0]!.message).toContain('gbrain wakeshift test');
+ await engine.executeRaw("INSERT INTO wake_forgets (wake_user) VALUES ('fedcba9876543210fedcba9876543210')");
+ writeConfig({ enabled: false, url: fake.url });
+ const off = await doctorChecks();
+ expect(off[0]).toMatchObject({ status: 'warn', details: { enabled: false, pending_forgets: 1 } });
+ expect(off[0]!.message).toContain('gbrain wakeshift enable');
+ });
+
test('the manifest carries both new events, personal and deleted on delivery', () => {
const events = wakeshiftManifest().events as Array<Record<string, any>>;
for (const name of ['gbrain.write.settled', 'gbrain.cron.finished']) {