Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
feat(errors): fix wave 5 refusal catalogue with anchor doc test (DX-O1, DX-O2)
garrytan6c3c3a062adf
diff --git a/docs/guides/repair.md b/docs/guides/repair.mdindex 62160df746..b22d268f67 100644--- a/docs/guides/repair.md+++ b/docs/guides/repair.md@@ -491,6 +491,46 @@ covers the whole retry budget each grant authorizes. A resumed run replays thegrant it already made instead of granting another cycle. A signature-mismatchedvector is never reconciled; it is re-embedded.+## Wave 5 refusal codes++Each heading below is the `docs` anchor a refusal carries. The full recovery+steps land with the fix that raises each code.++### Explicit-only repair kinds++`stale-atoms` and `extractor-facts` run only when named:+`gbrain repair <kind>` previews, and `gbrain repair <kind> --apply --expect <hash>`+applies exactly the previewed set. `gbrain repair --all`, the remediation plan+and `gbrain doctor --remediate --include-repairs` list them with their preview+command (`explicit_kind_required`) and never run them. A build without the+kind refuses with `unavailable`.++### Preview changed++`preview_changed`: the hash passed with `--expect` names no saved preview, or a+preview older than 7 days. Nothing was applied. Re-run the printed preview+command and apply with the new hash.++### Legacy job authority++`permission_denied` for a job row with SQL NULL submission authority left by an+upgrade across v0.50.++### Legacy jobs active++`legacy_jobs_active`: a legacy-job preview found active jobs. Stop producers+and workers and cancel the listed jobs first.++### Legacy job selection invalid++`legacy_job_selection_invalid`: `--select` named an unknown key or value. The+message lists the valid ones.++### Projection owner resident++`projection_owner_resident`: a projection drain was refused because a resident+owner holds the PGLite datastore.+## Related- [Write refusal reasons](write-refusals.md) — what a refused write means and the recovery commanddiff --git a/docs/guides/write-refusals.md b/docs/guides/write-refusals.mdindex 1b10b09aa2..a57ccab454 100644--- a/docs/guides/write-refusals.md+++ b/docs/guides/write-refusals.md@@ -60,6 +60,8 @@ of making a duplicate.| <a id="migrations_running"></a>`migrations_running` | `migrations_running` (exit 75) | `gbrain apply-migrations` (or the post-upgrade step of `gbrain upgrade`) found another runner holding the brain's orchestration lock and stopped before touching the migration ledger, so migrations never run twice in parallel. The message names the holder's host and pid. A holder that died is taken over automatically on the next run. `gbrain upgrade` reports this as `Migrations: running`, not as a failed upgrade. | Wait for the other run to finish; `gbrain doctor` shows migration progress. Then `gbrain apply-migrations --yes` confirms everything is applied. || <a id="writer_deactivate"></a>`writer_deactivate` blockers | `writer_not_quiesced`, `writer_admin_locked`, `writer_admin_state_changed`, `writer_lock_unavailable` | `gbrain sources writer deactivate` found pending work (a queued, running or recovering write, a topology change or effect that is not settled, a held connector item, or a live connector or maintenance lease), the writer admin lock, a changed admin state, or a local process holding a worktree lock. Nothing changed. The suggestion names each blocker and its exit. | Run `gbrain sources writer deactivate --dry-run` for the full list, run each named exit (`gbrain cancel-write-request <request_id>`, `gbrain sync --source <id> --no-pull --retry-failed`, `gbrain repair embedding-effects --source <id>`, `gbrain sources writer retry-effects <source> --request-id <id> --dry-run`, `gbrain sources writer unlock`, `gbrain sources retry-held <id>`), then deactivate again with a fresh `--expected-state` from `gbrain sources writer status --json`. See the [deactivate runbook](../architecture/topologies.md#deactivate-runbook). || unknown option (repair) | `invalid_params` | `gbrain repair` refuses any option it does not list, so a mistyped flag or `--max-usd` never runs a repair silently without it. | Fix the option (`gbrain repair --help`). To cap paid repair work, run `gbrain doctor --remediate --yes --include-repairs --max-usd <n>`. |+| <a id="colon_slug_windows_write_through"></a>`colon_slug_windows_write_through` | `colon_slug_windows_write_through` | A slug containing `:` cannot be written through to a file on Windows. | Write it on a non-Windows host, or choose a slug without `:`. |+| <a id="embedding_auth_failed"></a>`embedding_auth_failed` | `embedding_auth_failed` | The embedding provider rejected the configured key (HTTP 401 or 403). | Check which key is in effect and replace it. |### Other error codesdiff --git a/src/core/error-catalogue.ts b/src/core/error-catalogue.tsnew file mode 100644index 0000000000..9e81a0b87d--- /dev/null+++ b/src/core/error-catalogue.ts@@ -0,0 +1,57 @@+/**+ * Error catalogue for coded refusals (fix wave 5, DX-O2 / ENG-O12).+ *+ * One entry per refusal: the stable wire `code` and the docs anchor that+ * explains it. Every entry's anchor must exist in its guide; the doc test+ * (`test/error-catalogue.test.ts`) resolves each `docs` against the Markdown+ * headings and `<a id>` tags, so a renamed heading fails CI instead of+ * leaving a dead link in an error.+ *+ * Wire mapping: an `OperationError` carries the hint as `suggestion` and the+ * anchor as `docs` (its `toJSON()` serializes both for CLI `--json` and MCP);+ * a CLI-only refusal may use a `StructuredError`, which names them `hint` and+ * `docs_url`. `/ingest` renders an `OperationError` as+ * `{ error, message, hint, docs_url }`.+ *+ * Entries are keyed by name, not code, so one code can carry two anchors:+ * `legacy_job_authority` keeps the existing `permission_denied` code and adds+ * its own anchor. Add a refusal by adding one entry and its doc heading.+ */+import { OperationError } from './ops/contract.ts';+import { StructuredAgentError, buildError } from './errors.ts';++export interface CatalogueEntry {+ /** Stable machine-readable code on the wire. */+ code: string;+ /** Repository-relative docs pointer, `docs/guides/<guide>.md#<anchor>`. */+ docs: string;+}++export const ERROR_CATALOGUE = {+ legacy_jobs_active: { code: 'legacy_jobs_active', docs: 'docs/guides/repair.md#legacy-jobs-active' },+ legacy_job_selection_invalid: { code: 'legacy_job_selection_invalid', docs: 'docs/guides/repair.md#legacy-job-selection-invalid' },+ legacy_job_authority: { code: 'permission_denied', docs: 'docs/guides/repair.md#legacy-job-authority' },+ preview_changed: { code: 'preview_changed', docs: 'docs/guides/repair.md#preview-changed' },+ projection_owner_resident: { code: 'projection_owner_resident', docs: 'docs/guides/repair.md#projection-owner-resident' },+ explicit_kind_required: { code: 'explicit_kind_required', docs: 'docs/guides/repair.md#explicit-only-repair-kinds' },+ repair_kind_unavailable: { code: 'unavailable', docs: 'docs/guides/repair.md#explicit-only-repair-kinds' },+ colon_slug_windows_write_through: { code: 'colon_slug_windows_write_through', docs: 'docs/guides/write-refusals.md#colon_slug_windows_write_through' },+ embedding_auth_failed: { code: 'embedding_auth_failed', docs: 'docs/guides/write-refusals.md#embedding_auth_failed' },+} as const satisfies Record<string, CatalogueEntry>;++export type CatalogueName = keyof typeof ERROR_CATALOGUE;++/**+ * An `OperationError` for a catalogue entry. `message` is one sentence; `hint`+ * is the exact command to run next, filled with real values.+ */+export function catalogueError(name: CatalogueName, message: string, hint: string): OperationError {+ const entry = ERROR_CATALOGUE[name];+ return new OperationError(entry.code, message, hint, entry.docs);+}++/** The same refusal as a `StructuredError` envelope, for CLI-only surfaces. */+export function catalogueStructuredError(name: CatalogueName, errorClass: string, message: string, hint: string): StructuredAgentError {+ const entry = ERROR_CATALOGUE[name];+ return new StructuredAgentError(buildError({ class: errorClass, code: entry.code, message, hint, docs_url: entry.docs }));+}diff --git a/test/error-catalogue.test.ts b/test/error-catalogue.test.tsnew file mode 100644index 0000000000..a5ee5d977d--- /dev/null+++ b/test/error-catalogue.test.ts@@ -0,0 +1,69 @@+/**+ * Fix wave 5 error catalogue (DX-O1 / DX-O2 / ENG-O12): every catalogue+ * entry's docs anchor exists in its guide, and a catalogue refusal carries its+ * code, one-sentence message, hint and docs pointer on both wire shapes+ * (`OperationError.toJSON()` for CLI `--json` and MCP; the StructuredError+ * envelope for CLI-only surfaces).+ */+import { describe, expect, test } from 'bun:test';+import { readFileSync } from 'node:fs';+import { join } from 'node:path';+import { ERROR_CATALOGUE, catalogueError, catalogueStructuredError, type CatalogueName } from '../src/core/error-catalogue.ts';+import { serializeError } from '../src/core/errors.ts';++const ROOT = join(import.meta.dir, '..');++/** GitHub's heading anchor: lowercase, punctuation dropped, spaces to hyphens. */+function headingAnchor(heading: string): string {+ return heading.trim().toLowerCase().replace(/[^\p{L}\p{N}\s_-]/gu, '').replace(/\s/g, '-');+}++function anchorsOf(path: string): Set<string> {+ const text = readFileSync(join(ROOT, path), 'utf8').replace(/^```[\s\S]*?^```/gm, '');+ const anchors = new Set<string>();+ for (const [, heading] of text.matchAll(/^#{1,6}\s+(.+)$/gm)) anchors.add(headingAnchor(heading!));+ for (const [, id] of text.matchAll(/<a id="([^"]+)"><\/a>/g)) anchors.add(id!);+ return anchors;+}++describe('error catalogue docs anchors', () => {+ test('every entry points at docs/guides/repair.md or write-refusals.md and its anchor exists', () => {+ const names = Object.keys(ERROR_CATALOGUE) as CatalogueName[];+ expect(names.length).toBeGreaterThan(0);+ for (const name of names) {+ const [path, anchor] = ERROR_CATALOGUE[name].docs.split('#');+ expect(['docs/guides/repair.md', 'docs/guides/write-refusals.md']).toContain(path!);+ expect(anchor, `${name} has no anchor`).toBeTruthy();+ expect(anchorsOf(path!).has(anchor!), `${name}: ${path}#${anchor} does not exist`).toBe(true);+ }+ });++ test('the anchor resolver rejects a heading that does not exist', () => {+ expect(anchorsOf('docs/guides/repair.md').has('preview-changed')).toBe(true);+ expect(anchorsOf('docs/guides/repair.md').has('no-such-wave-5-heading')).toBe(false);+ });++ test('the DX-O2 codes are catalogued with their stable codes, and the legacy refusal keeps permission_denied', () => {+ for (const code of ['legacy_jobs_active', 'legacy_job_selection_invalid', 'preview_changed', 'projection_owner_resident',+ 'colon_slug_windows_write_through', 'embedding_auth_failed'] as const) {+ expect(ERROR_CATALOGUE[code].code).toBe(code);+ }+ expect(ERROR_CATALOGUE.legacy_job_authority).toEqual({ code: 'permission_denied', docs: 'docs/guides/repair.md#legacy-job-authority' });+ expect(ERROR_CATALOGUE.colon_slug_windows_write_through.docs).toStartWith('docs/guides/write-refusals.md#');+ });+});++describe('catalogue refusals on the wire', () => {+ test('OperationError.toJSON carries code, message, hint as suggestion and the anchor as docs', () => {+ const error = catalogueError('projection_owner_resident', 'A resident owner holds this PGLite brain.', 'Stop it first: gbrain serve --stop');+ expect(error.toJSON()).toMatchObject({ error: 'projection_owner_resident', message: 'A resident owner holds this PGLite brain.',+ suggestion: 'Stop it first: gbrain serve --stop', docs: 'docs/guides/repair.md#projection-owner-resident' });+ expect(JSON.parse(JSON.stringify(catalogueError('legacy_job_authority', 'm.', 'h')))).toMatchObject({ error: 'permission_denied', docs: 'docs/guides/repair.md#legacy-job-authority' });+ });++ test('the StructuredError envelope names them hint and docs_url', () => {+ const error = catalogueStructuredError('legacy_job_selection_invalid', 'SelectionInvalid', 'Unknown --select key "state".', 'gbrain jobs authorize-legacy --select "status=waiting"');+ expect(serializeError(error)).toEqual({ class: 'SelectionInvalid', code: 'legacy_job_selection_invalid', message: 'Unknown --select key "state".',+ hint: 'gbrain jobs authorize-legacy --select "status=waiting"', docs_url: 'docs/guides/repair.md#legacy-job-selection-invalid' });+ });+});