Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(migrate): register schema_migration_timeout as internal state; reword prose the command linter reads as commands

garrytan98b912f475f6
diff --git a/docs/guides/error-codes.md b/docs/guides/error-codes.md
index 43f2e7f3db..8f7974d2e2 100644
--- a/docs/guides/error-codes.md
+++ b/docs/guides/error-codes.md
@@ -2465,7 +2465,7 @@ More: [docs/guides/shared-brain-skills.md#migrate-an-existing-brain](../../docs/
| Meaning | Why | Next step | Who acts · consent | Verify | Exit | Retryable |
|---|---|---|---|---|---|---|
-| Schema migrations are behind (or could not be confirmed at head), or an orchestrator migration is wedged, so the command did not report success. | apply-migrations reasons: not_applied: the run had no --yes (or --non-interactive), so it did not apply the schema migrations; the fix applies them with --yes. still_behind: an authorized run tried them and they did not apply; another --yes repeats the failure, so run `gbrain doctor --json` and report; when a migration ran out of its statement budget (#7108) the error names it and its fix depends on the case: recorded by another gbrain release (rerun), an idle-in-transaction blocker (end it once the user agrees, rerun) or nothing blocking (once the user agrees, one run with --statement-timeout). schema_unreadable: the schema version could not be read back after the orchestrators ran, so the run cannot confirm it reached head; report it. wedged: an orchestrator migration ended partial three or more times in a row, so runs skip it until a person fixes the cause and clears it with --force-retry. Each failure names the migrations still pending. schema_behind (#6904): a managed write was refused at admission because the brain schema is below the version this build needs. Not retryable as is (class caller): the same write refuses until the migrations are applied; nothing was admitted, so the request_id was not consumed and the same request_id commits once the schema is migrated (a replay of an already accepted id still reconciles). | schema_behind: apply them with `gbrain apply-migrations --yes` as the database role that owns the gbrain tables (the brain host operator when this connection does not own them), then resubmit the write with the same request_id. not_applied: apply them with `gbrain apply-migrations --yes`. still_behind or schema_unreadable: run `gbrain doctor --json` and report it to the user; another --yes repeats the failure (a still_behind that names a timed-out migration carries its own fix: follow it). See docs/guides/troubleshooting.md#migration-statement-timeout. wedged: ask the user, read the migration's partial rows in the brain home's migrations/completed.jsonl (phases[].detail, if present; apply-migrations prints the resolved path), fix the cause, then `gbrain apply-migrations --force-retry <version>` and `gbrain apply-migrations --yes`. | agent | `repeat the read that failed` | 1 | no |
+| Schema migrations are behind (or could not be confirmed at head), or an orchestrator migration is wedged, so the command did not report success. | apply-migrations reasons: not_applied: the run had no --yes (or --non-interactive), so it did not apply the schema migrations; the fix applies them with --yes. still_behind: an authorized run tried them and they did not apply; another --yes repeats the failure, so run `gbrain doctor --json` and report; when a migration ran out of its statement budget (#7108) the error names it and its fix depends on the case: recorded by another release (rerun), an idle-in-transaction blocker (end it once the user agrees, rerun) or nothing blocking (once the user agrees, one run with --statement-timeout). schema_unreadable: the schema version could not be read back after the orchestrators ran, so the run cannot confirm it reached head; report it. wedged: an orchestrator migration ended partial three or more times in a row, so runs skip it until a person fixes the cause and clears it with --force-retry. Each failure names the migrations still pending. schema_behind (#6904): a managed write was refused at admission because the brain schema is below the version this build needs. Not retryable as is (class caller): the same write refuses until the migrations are applied; nothing was admitted, so the request_id was not consumed and the same request_id commits once the schema is migrated (a replay of an already accepted id still reconciles). | schema_behind: apply them with `gbrain apply-migrations --yes` as the database role that owns the gbrain tables (the brain host operator when this connection does not own them), then resubmit the write with the same request_id. not_applied: apply them with `gbrain apply-migrations --yes`. still_behind or schema_unreadable: run `gbrain doctor --json` and report it to the user; another --yes repeats the failure (a still_behind that names a timed-out migration carries its own fix: follow it). See docs/guides/troubleshooting.md#migration-statement-timeout. wedged: ask the user, read the migration's partial rows in the brain home's migrations/completed.jsonl (phases[].detail, if present; apply-migrations prints the resolved path), fix the cause, then `gbrain apply-migrations --force-retry <version>` and `gbrain apply-migrations --yes`. | agent | `repeat the read that failed` | 1 | no |
Reasons: `not_applied`, `still_behind`, `schema_unreadable`, `wedged`, `schema_behind`.
diff --git a/src/commands/apply-migrations-schema.ts b/src/commands/apply-migrations-schema.ts
index 191130ef5c..68fa9be2b7 100644
--- a/src/commands/apply-migrations-schema.ts
+++ b/src/commands/apply-migrations-schema.ts
@@ -143,7 +143,7 @@ export function schemaPendingError(p: SchemaPending, rerunWithYes: Action): Oper
const advice = migrationTimeoutAdvice(p.timeout, { argv: ['gbrain', 'doctor', '--only', 'schema_version', '--json'] });
return opError('migrations_pending', `Schema migrations are still behind after an authorized run (${where}). ${advice.message}`,
advice.fix.user_message ?? advice.fix.why,
- { reason: p.reason, why: 'A schema migration ran out of its statement budget, so the run stopped below it; what helps depends on the gbrain release that ran it and on whether a session was blocking it.',
+ { reason: p.reason, why: 'A schema migration ran out of its statement budget, so the run stopped below it; what helps depends on the release that ran it and on whether a session was blocking it.',
fix: advice.fix });
}
if (p.reason === 'still_behind') {
diff --git a/src/core/error-registry.ts b/src/core/error-registry.ts
index d4881cbde9..ceeb90132b 100644
--- a/src/core/error-registry.ts
+++ b/src/core/error-registry.ts
@@ -431,7 +431,7 @@ export const CODES = {
suggestion: 'Run the read-only inventory `gbrain apply-migrations --migration <version> --dry-run --json` on the brain host, do each source\'s next step it names (ask the user before publishing or activating), then run `gbrain apply-migrations --yes`.',
fix: { argv: ['gbrain', 'apply-migrations', '--list', '--json'], consent: [], actor: 'agent', why: 'Lists each migration\'s status and the read-only inventory command for host work still pending; changes nothing.', requires_exclusive: false } },
migrations_pending: { class: 'caller', summary: "Schema migrations are behind (or could not be confirmed at head), or an orchestrator migration is wedged, so the command did not report success.",
- why: 'apply-migrations reasons: not_applied: the run had no --yes (or --non-interactive), so it did not apply the schema migrations; the fix applies them with --yes. still_behind: an authorized run tried them and they did not apply; another --yes repeats the failure, so run `gbrain doctor --json` and report; when a migration ran out of its statement budget (#7108) the error names it and its fix depends on the case: recorded by another gbrain release (rerun), an idle-in-transaction blocker (end it once the user agrees, rerun) or nothing blocking (once the user agrees, one run with --statement-timeout). schema_unreadable: the schema version could not be read back after the orchestrators ran, so the run cannot confirm it reached head; report it. wedged: an orchestrator migration ended partial three or more times in a row, so runs skip it until a person fixes the cause and clears it with --force-retry. Each failure names the migrations still pending. schema_behind (#6904): a managed write was refused at admission because the brain schema is below the version this build needs. Not retryable as is (class caller): the same write refuses until the migrations are applied; nothing was admitted, so the request_id was not consumed and the same request_id commits once the schema is migrated (a replay of an already accepted id still reconciles).',
+ why: 'apply-migrations reasons: not_applied: the run had no --yes (or --non-interactive), so it did not apply the schema migrations; the fix applies them with --yes. still_behind: an authorized run tried them and they did not apply; another --yes repeats the failure, so run `gbrain doctor --json` and report; when a migration ran out of its statement budget (#7108) the error names it and its fix depends on the case: recorded by another release (rerun), an idle-in-transaction blocker (end it once the user agrees, rerun) or nothing blocking (once the user agrees, one run with --statement-timeout). schema_unreadable: the schema version could not be read back after the orchestrators ran, so the run cannot confirm it reached head; report it. wedged: an orchestrator migration ended partial three or more times in a row, so runs skip it until a person fixes the cause and clears it with --force-retry. Each failure names the migrations still pending. schema_behind (#6904): a managed write was refused at admission because the brain schema is below the version this build needs. Not retryable as is (class caller): the same write refuses until the migrations are applied; nothing was admitted, so the request_id was not consumed and the same request_id commits once the schema is migrated (a replay of an already accepted id still reconciles).',
suggestion: 'schema_behind: apply them with `gbrain apply-migrations --yes` as the database role that owns the gbrain tables (the brain host operator when this connection does not own them), then resubmit the write with the same request_id. not_applied: apply them with `gbrain apply-migrations --yes`. still_behind or schema_unreadable: run `gbrain doctor --json` and report it to the user; another --yes repeats the failure (a still_behind that names a timed-out migration carries its own fix: follow it). See docs/guides/troubleshooting.md#migration-statement-timeout. wedged: ask the user, read the migration\'s partial rows in the brain home\'s migrations/completed.jsonl (phases[].detail, if present; apply-migrations prints the resolved path), fix the cause, then `gbrain apply-migrations --force-retry <version>` and `gbrain apply-migrations --yes`.',
reasons: ['not_applied', 'still_behind', 'schema_unreadable', 'wedged', 'schema_behind'] },
migrations_running: { class: 'retryable', summary: "Another apply-migrations run holds the migration orchestration lock.", exit: 75 },
diff --git a/src/core/migrate.ts b/src/core/migrate.ts
index ace78a549e..1588896d07 100644
--- a/src/core/migrate.ts
+++ b/src/core/migrate.ts
@@ -554,11 +554,11 @@ async function applyOneMigration(engine: BrainEngine, m: Migration): Promise<voi
console.error('');
console.error(` Cause: no idle-in-transaction session was open, and a lock wait stops at the lock timeout, so its own work`);
console.error(` outlasted its ${Math.round(migrationStatementTimeoutMs() / 60_000)}-minute statement budget on this brain; every attempt rolled back.`);
- console.error(' A plain rerun with this gbrain release repeats the failure (#7108).');
+ console.error(' A plain rerun with this release repeats the failure (#7108).');
console.error('');
console.error(' Fix:');
console.error(' 1. gbrain doctor --locks # confirm no session holds a lock on the table');
- console.error(' 2. If a newer gbrain release exists: gbrain upgrade, then gbrain apply-migrations --yes');
+ console.error(' 2. If a newer release exists: gbrain upgrade, then gbrain apply-migrations --yes');
console.error(" 3. Otherwise, with the user's agreement: gbrain apply-migrations --yes --statement-timeout 120, and report it");
console.error('');
}
diff --git a/src/core/migration-timeout-advice.ts b/src/core/migration-timeout-advice.ts
index f6123ae8d0..63501f19f6 100644
--- a/src/core/migration-timeout-advice.ts
+++ b/src/core/migration-timeout-advice.ts
@@ -27,7 +27,7 @@ export function migrationTimeoutAdvice(record: MigrationTimeoutRecord, verify: {
const rerun = ['gbrain', 'apply-migrations', '--yes'];
if (!record.batched && MIGRATION_PRELUDES.has(record.version)) {
return {
- message: `${which} ran out of its statement budget in one transaction; this gbrain runs its backfill in committed batches first, so run gbrain apply-migrations --yes.`,
+ message: `${which} ran out of its statement budget in one transaction; this version runs its backfill in committed batches first, so run gbrain apply-migrations --yes.`,
fix: { argv: rerun, consent: [], actor: 'agent', requires_exclusive: false, verify,
why: `This release backfills v${record.version} in committed batches before the migration (#7108), so no statement grows with the table; an interrupted run keeps its batches.` },
};
@@ -38,13 +38,13 @@ export function migrationTimeoutAdvice(record: MigrationTimeoutRecord, verify: {
message: `${which} ran out of its statement budget while idle-in-transaction session(s) ${pids} were open; they may hold locks on its table. Check them with gbrain doctor --locks, end the ones still open once the user agrees, then run gbrain apply-migrations --yes.`,
fix: { ask_user: true, argv: rerun, consent: [], actor: 'agent', requires_exclusive: false, verify,
why: `Session(s) ${pids} were idle in a transaction for over five minutes when v${record.version} timed out; a session holding a lock on its table would make the work wait. They were sampled then and may be gone or unrelated now.`,
- user_message: `gbrain migration v${record.version} timed out while database session(s) ${pids} were idle in an open transaction. Run gbrain doctor --locks to see whether they are still open. With your agreement, end the ones that are on the database host (SELECT pg_terminate_backend(<pid>)), then run gbrain apply-migrations --yes. Go ahead?` },
+ user_message: `Migration v${record.version} timed out while database session(s) ${pids} were idle in an open transaction. Run gbrain doctor --locks to see whether they are still open. With your agreement, end the ones that are on the database host (SELECT pg_terminate_backend(<pid>)), then run gbrain apply-migrations --yes. Go ahead?` },
};
}
return {
message: `${which} used its whole statement budget on every attempt with no idle session open, so a plain rerun repeats the failure; once the user agrees, rerun it with a longer budget.`,
fix: { ask_user: true, argv: [...rerun, '--statement-timeout', String(LONG_BUDGET_MINUTES)], consent: [], actor: 'agent', requires_exclusive: true, verify,
why: `A lock wait would have stopped at the schema lock timeout instead, so v${record.version}'s own work outlasts the default budget on this brain; the same release fails the same way unless the budget is raised for one run.`,
- user_message: `gbrain migration v${record.version} (${record.name}) times out on this brain on every attempt, and no database session was blocking it. A plain rerun fails the same way. It can run once with a ${LONG_BUDGET_MINUTES}-minute budget (gbrain apply-migrations --yes --statement-timeout ${LONG_BUDGET_MINUTES}); while it runs, writes to the table it changes wait. Please also report it at https://github.com/garrytan/gbrain/issues with the output of gbrain doctor --json. Run it now?` },
+ user_message: `Migration v${record.version} (${record.name}) times out on this brain on every attempt, and no database session was blocking it. A plain rerun fails the same way. It can run once with a ${LONG_BUDGET_MINUTES}-minute budget (gbrain apply-migrations --yes --statement-timeout ${LONG_BUDGET_MINUTES}); while it runs, writes to the table it changes wait. Please also report it at https://github.com/garrytan/gbrain/issues with the output of gbrain doctor --json. Run it now?` },
};
}
diff --git a/test/helpers/config-reader-registry.ts b/test/helpers/config-reader-registry.ts
index b9d114b626..1b6a0c6cb9 100644
--- a/test/helpers/config-reader-registry.ts
+++ b/test/helpers/config-reader-registry.ts
@@ -40,6 +40,7 @@ export const INTERNAL_CONFIG_STATE: readonly InternalConfigState[] = [
{ key: 'ocr_failed_other', reason: 'image-OCR run counter (import-file.ts), read by doctor queue_assets' },
{ key: 'ocr_skipped_budget', reason: 'image-OCR run counter (import-file.ts), read by doctor queue_assets' },
{ key: 'page_state.revision_backfill', reason: 'page-state revision backfill progress marker' },
+ { key: 'schema_migration_timeout', reason: 'the last schema migration that ran out of its statement budget, written and cleared by migrate.ts (migration-timeout.ts, #7108)' },
{ key: 'persistence.database_only_sources', reason: 'list maintained by gbrain sources writer activate/deactivate; config unset (which takes any key) is the documented escape hatch' },
{ key: 'persistence.maintenance.last_compaction', reason: 'receipt compaction pass cursor (compaction-pass.ts)' },
{ key: 'persistence.true_up.compacted_through', reason: 'receipt true-up cursor (receipt-true-up.ts)' },