Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(sources): attach refuses an invalid source id with actionable invalid_params (#6827)

time-attacke30288613847
diff --git a/src/commands/sources.ts b/src/commands/sources.ts
index 6e752b2fd0..25e28cb1c7 100644
--- a/src/commands/sources.ts
+++ b/src/commands/sources.ts
@@ -74,16 +74,18 @@ import { deleteSourceRow } from '../core/source-delete.ts';
import { USAGE_EXIT_CODE } from '../core/exit-codes.ts';
import { isSyncStrategy } from '../core/sync.ts';
import { pullStrategyOf } from '../core/git-remote.ts';
+import { isValidSourceId } from '../core/source-id.ts';
+import { opError } from '../core/ops/contract.ts';
// ── Validation ──────────────────────────────────────────────
-// Shared with source-resolver.ts — canonical shape.
-const SOURCE_ID_RE = /^[a-z0-9](?:[a-z0-9-]{0,30}[a-z0-9])?$/;
-
function validateSourceId(id: string): void {
- if (!SOURCE_ID_RE.test(id)) {
- throw new Error(
+ if (!isValidSourceId(id)) {
+ throw opError(
+ 'invalid_params',
`Invalid source id "${id}". Must be 1-32 lowercase alnum chars with optional interior hyphens (e.g. "wiki", "yc-media").`,
+ 'Use 1-32 lowercase letters, digits and interior hyphens, for example: gbrain sources attach wiki. gbrain sources list shows the existing ids.',
+ { fix: { argv: ['gbrain', 'sources', 'list', '--json'], consent: [], actor: 'agent', requires_exclusive: false, why: 'Lists the existing source ids to attach to.' } },
);
}
}
diff --git a/src/core/behavior-change-notice.ts b/src/core/behavior-change-notice.ts
index b49dcce091..ef9cf4530d 100644
--- a/src/core/behavior-change-notice.ts
+++ b/src/core/behavior-change-notice.ts
@@ -910,6 +910,7 @@ export const BEHAVIOR_CHANGES: ReadonlyArray<{ since: string; text: ChangeText;
{ since: '0.60.307.0', text: '`gbrain advisor` (and the `advisor` MCP operation) reports a database read that fails inside the `usage-shape` or `stalled-jobs` check as `collector_failed:<check>` and exits 1, instead of returning no findings for that check and possibly reporting a healthy brain. A brain with no pages, and an older brain without `minion_jobs`, `sources` or `sources.last_sync_at`, still report nothing for those checks.' },
{ since: '0.60.307.0', text: '`gbrain search tune --apply` now writes before it reports: `--json` lists in `applied` and `revert_commands` only the changes that landed, adds per-item `results` (`applied`, `unsupported`, `failed`, `outcome_unknown`, `not_attempted`), `not_applied` and `apply_error`, and the text output marks a failed or unrecognized change with ✗ instead of ✓. The batch stops at the first write that does not land and the command exits non-zero; changes that already landed stay in place with their revert commands.' },
{ since: '0.60.307.0', text: 'A worker registry directory that exists but cannot be listed (permission denied, or a file where the directory should be) now reads as unknown worker liveness instead of no workers. Doctor `queue_health` reports `worker_alive: null` with a liveness-unknown message and no start-a-worker fix, `legacy_job_authority` reports `live_workers: null`, `gbrain jobs supervisor status --json` reports `workers: null`, a submitted job\'s `queue_state` omits `worker_alive` and warns that liveness is unknown, `gbrain jobs stats` reports a wedge instead of `no_worker`, and the `migrate-embeddings` plan warns about an unknown worker count. Each names the errno and path in `worker_registry_errno` and `worker_registry_path`. A missing registry directory still means no workers.' },
+ { since: '0.60.307.0', text: '`gbrain sources attach` with an invalid source id now refuses with `invalid_params` (a caller error) and a fix that lists the existing ids (`gbrain sources list --json`), instead of an `internal_error`. Nothing is written in either case.' },
];
/** The newest disclosed change's release: the notice id moves only when a release adds rows. */
diff --git a/test/sources-attach-contract.test.ts b/test/sources-attach-contract.test.ts
new file mode 100644
index 0000000000..25c48f73d4
--- /dev/null
+++ b/test/sources-attach-contract.test.ts
@@ -0,0 +1,28 @@
+import { expect, test } from 'bun:test';
+import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { runSources } from '../src/commands/sources.ts';
+import { caught, envelopeFor } from './helpers/agent-envelope.ts';
+
+test('invalid attach source has typed actionable caller error before engine or filesystem work', async () => {
+ const previous = process.cwd();
+ const root = mkdtempSync(join(tmpdir(), 'source-attach-contract-'));
+ const marker = join(root, '.gbrain-source');
+ writeFileSync(marker, 'existing-source\n');
+ let reads = 0;
+ const engine = new Proxy({}, { get() { reads++; throw Error('engine touched'); } });
+ try {
+ process.chdir(root);
+ const err = await caught(() => runSources(engine as never, ['attach', 'INVALID_SOURCE']));
+ const envelope = envelopeFor(err, 'stdio');
+ expect(envelope.code).toBe('invalid_params');
+ expect(envelope.suggestion).toContain('gbrain sources attach');
+ expect(envelope.fix?.argv).toEqual(['gbrain', 'sources', 'list', '--json']);
+ expect(reads).toBe(0);
+ expect(readFileSync(marker, 'utf8')).toBe('existing-source\n');
+ } finally {
+ process.chdir(previous);
+ rmSync(root, { recursive: true, force: true });
+ }
+});