Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: OpenClaw skill plugin fails closed on unknown senders and stale suggestions
garrytanfad207d6154a
diff --git a/src/core/setup/openclaw-skill-plugin-code.ts b/src/core/setup/openclaw-skill-plugin-code.tsindex 483fba1484..3198f045e2 100644--- a/src/core/setup/openclaw-skill-plugin-code.ts+++ b/src/core/setup/openclaw-skill-plugin-code.ts@@ -256,6 +256,7 @@ function checkSkew(s) {if (refreshing || now < skewOpenUntil || now - skewCheckedAt < 3_600_000) return;skewCheckedAt = now;const env = { ...process.env, ...(GBRAIN_HOME_ENV ? { GBRAIN_HOME: GBRAIN_HOME_ENV } : {}) };+ delete env.GBRAIN_SKILL_LOADING;execFile(GBRAIN_BIN, ['--version'], { timeout: 10_000, env }, (err, stdout) => {const v = !err && /\d+\.\d+\.\d+(?:\.\d+)?/.exec(String(stdout));if (!v || v[0] === s.gbrain_version) return;@@ -298,11 +299,11 @@ async function resolveTurn(event, ctx, opts) {if (trigger === 'heartbeat' || trigger === 'cron') return finish({ outcome: 'skipped_provenance', provenance: trigger });if (Array.isArray(a.owner_sender_ids) && a.owner_sender_ids.length) {const sender = senderOf(event, ctx);- if (sender !== null && !a.owner_sender_ids.includes(sender)) return finish({ outcome: 'skipped_sender' });+ if (sender === null || !a.owner_sender_ids.includes(sender)) return finish({ outcome: 'skipped_sender' });}const turnId = (event && (event.currentUserMessageId || event.messageId)) || createHash('sha256').update(text).digest('hex').slice(0, 16);const memoKey = agentId + '\0' + sessionKey + '\0' + turnId;- if (memo.has(memoKey)) return finish({ outcome: 'retry' }, memo.get(memoKey));+ if (memo.has(memoKey)) return finish({ outcome: 'retry' }, arm === 'live' ? memo.get(memoKey) : '');const w = ensureWorker(s);if (!w) return finish({ outcome: 'error', code: 'skill_resolver_worker_unavailable' });const reset = resetNext.delete(sessionKey);diff --git a/src/core/setup/openclaw-skill-plugin.ts b/src/core/setup/openclaw-skill-plugin.tsindex 12629ee9dd..03b131c481 100644--- a/src/core/setup/openclaw-skill-plugin.ts+++ b/src/core/setup/openclaw-skill-plugin.ts@@ -23,6 +23,7 @@* admission. A failed or partial install is never reported as success: it* ends `skill_resolver_plugin_not_installed` with the commands still to run.*/+import { createHash } from 'node:crypto';import { chmodSync, copyFileSync, existsSync, mkdirSync, readdirSync, readFileSync, rmdirSync, rmSync, unlinkSync } from 'node:fs';import { dirname, join } from 'node:path';import { pathToFileURL } from 'node:url';@@ -92,10 +93,11 @@ function which(name: string): string | null {const shown = (argv: string[]) => argv.map((a) => (/^[\w./:=@-]+$/.test(a) ? a : `'${a.replace(/'/g, `'\\''`)}'`)).join(' ');const lastLine = (s: string) => s.trim().split('\n').at(-1) || 'no output';-/** `openclaw-<agent>`, folded to the catalog-name alphabet. */+/** `openclaw-<agent>`, folded to the catalog-name alphabet; an id the fold changes gets a short hash of itself, so two agents never share a catalog. */export function openclawCatalogName(agentId: string): string {- const slug = agentId.toLowerCase().replace(/[^a-z0-9_-]+/g, '-').replace(/^[-_]+/, '').slice(0, 54) || 'main';- return `openclaw-${slug}`;+ const slug = agentId.toLowerCase().replace(/[^a-z0-9_-]+/g, '-').replace(/^[-_]+/, '');+ if (slug && slug === agentId && slug.length <= 54) return `openclaw-${slug}`;+ return `openclaw-${slug.slice(0, 45) || 'agent'}-${createHash('sha256').update(agentId).digest('hex').slice(0, 8)}`;}interface RosterEntry { id: string; workspace?: string }diff --git a/test/openclaw-skill-plugin.test.ts b/test/openclaw-skill-plugin.test.tsindex 7e3ca4a8e7..97d44a2bf6 100644--- a/test/openclaw-skill-plugin.test.ts+++ b/test/openclaw-skill-plugin.test.ts@@ -19,6 +19,7 @@ import { createHash } from 'node:crypto';import { tmpdir } from 'node:os';import { join } from 'node:path';import { runCli } from './helpers/cli-spawn.ts';+import { openclawCatalogName } from '../src/core/setup/openclaw-skill-plugin.ts';setDefaultTimeout(120_000);const CLI = join(import.meta.dir, '..', 'src', 'cli.ts');@@ -84,7 +85,7 @@ const pluginDir = (b: Box) => join(b.state, 'gbrain', 'gbrain-skill-resolver');const readState = (b: Box) => JSON.parse(readFileSync(statePath(b), 'utf8'));/** Drive the generated plugin under Node with a fake OpenClaw api; each step is [kind, args]. */-function drive(b: Box, steps: unknown[], nodeOptions?: string): any {+function drive(b: Box, steps: unknown[], nodeOptions?: string, env: Record<string, string> = {}): any {const script = join(b.root, 'drive.mjs');writeFileSync(script, String.raw`import { pathToFileURL } from 'node:url';@@ -108,7 +109,7 @@ await sleep(300);console.log(JSON.stringify(out));process.exit(0);`);- const r = Bun.spawnSync([NODE!, script, pluginDir(b), statePath(b), JSON.stringify(steps)], { env: { PATH: b.env.PATH!, HOME: b.home, ...(nodeOptions ? { NODE_OPTIONS: nodeOptions } : {}) }, stdout: 'pipe', stderr: 'pipe', timeout: 60_000 });+ const r = Bun.spawnSync([NODE!, script, pluginDir(b), statePath(b), JSON.stringify(steps)], { env: { PATH: b.env.PATH!, HOME: b.home, ...(nodeOptions ? { NODE_OPTIONS: nodeOptions } : {}), ...env }, stdout: 'pipe', stderr: 'pipe', timeout: 60_000 });const text = r.stdout.toString().trim().split('\n').at(-1) ?? '';try { return JSON.parse(text); } catch { throw new Error('drive failed: ' + r.stderr.toString() + text); }}@@ -218,6 +219,15 @@ describe.skipIf(!NODE)('gbrain skills pin', () => {});});+test('catalog names: distinct agent ids never share a catalog, and plain ids keep their plain name', () => {+ const long = 'a'.repeat(60);+ const names = ['main', 'work', 'Work', 'a-b', 'a.b', long + 'x', long + 'y'].map(openclawCatalogName);+ expect(names[0]).toBe('openclaw-main');+ expect(names[1]).toBe('openclaw-work');+ expect(new Set(names).size).toBe(names.length);+ for (const n of names) expect(n).toMatch(/^openclaw-[a-z0-9][a-z0-9_-]{0,53}$/);+});+describe.skipIf(!NODE)('the gbrain-skill-resolver plugin under Node', () => {test('live: pins every turn, up to 3 suggestions, pinned never suggested, retry commits once; skips internal turns; shadow and off inject nothing', async () => {const b = box({ skills: 250 });@@ -277,6 +287,34 @@ describe.skipIf(!NODE)('the gbrain-skill-resolver plugin under Node', () => {expect(lines.at(-1)).toMatchObject({ arm: 'control', outcome: 'would_suggest' });});+ test('fails closed: a turn with no sender under an owner allowlist, and a remembered block once the arm is no longer live', async () => {+ const b = box();+ await setup(b, ['--skill-loading', 'resolve', '--owner-sender', 'owner-1']);+ const out = drive(b, [+ ['turn', { text: 'triage the email inbox', id: 'm1' }],+ ['turn', { text: 'sync the calendar', id: 'm2', sender: 'owner-1' }],+ ['state', { mode: 'shadow' }],+ ['turn', { text: 'sync the calendar', id: 'm2', sender: 'owner-1' }],+ ['state', { mode: 'live', live_fraction: 0 }],+ ['turn', { text: 'sync the calendar', id: 'm2', sender: 'owner-1' }],+ ]);+ expect(out[0]?.appendContext ?? '').not.toContain('<relevant_skills>');+ expect(out[1].appendContext).toContain('calendar-sync');+ expect(out[3]?.appendContext ?? '').not.toContain('<relevant_skills>');+ expect(out[5]?.appendContext ?? '').not.toContain('<relevant_skills>');+ expect(logLines(b)[0]).toMatchObject({ outcome: 'skipped_sender' });+ });++ test('the version-skew refresh never inherits GBRAIN_SKILL_LOADING, so a gateway env cannot turn the plugin off', async () => {+ const b = box();+ await setup(b, ['--skill-loading', 'resolve']);+ const rec = join(b.root, 'gbrain-calls.log');+ writeFileSync(b.bin, '#!/bin/sh\necho "skill_loading=[$GBRAIN_SKILL_LOADING] $*" >> \'' + rec + '\'\n[ "$1" = "--version" ] && echo "gbrain 99.0.0"\nexit 0\n');+ drive(b, [['turn', { text: 'triage the email inbox', id: 'm1' }], ['sleep', { ms: 1500 }]], undefined, { GBRAIN_SKILL_LOADING: 'spring' });+ const lines = readFileSync(rec, 'utf8').trim().split('\n');+ expect(lines).toContain('skill_loading=[] setup openclaw --skills-index-only');+ });+test('refuses a library writable by others, and survives a process-only NODE_OPTIONS flag', async () => {const b = box();await setup(b, ['--skill-loading', 'resolve']);