Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(jobs): --dry-run never applies a --select; recovery previews every live status (Codex review)

garrytan1c59b891db59
diff --git a/src/commands/doctor/checks/legacy-job-authority.ts b/src/commands/doctor/checks/legacy-job-authority.ts
index b56b5489a7..bc64f85abc 100644
--- a/src/commands/doctor/checks/legacy-job-authority.ts
+++ b/src/commands/doctor/checks/legacy-job-authority.ts
@@ -10,7 +10,7 @@
*/
import type { BrainEngine } from '../../../core/engine.ts';
import { ERROR_CATALOGUE } from '../../../core/error-catalogue.ts';
-import { legacyRecoveryHint } from '../../../core/minions/legacy-selection.ts';
+import { LIVE_LEGACY_PREVIEW, legacyRecoveryHint } from '../../../core/minions/legacy-selection.ts';
import { UNREVIEWED_LIVE_JOBS_SQL, parseSubmissionAuthority } from '../../../core/minions/submission-authority.ts';
import type { Check } from '../../doctor.ts';
import { connectedEngine, type DoctorContext, type DoctorEntry } from '../context.ts';
@@ -70,7 +70,7 @@ export async function legacyJobAuthorityCheck(engine: BrainEngine): Promise<Chec
return {
name: 'legacy_job_authority', status: 'fail', details,
message: `${live} queued job(s) predate submission authority and block every worker (${authorizable} authorizable with SQL NULL authority, ${state.unsupported} unsupported). `
- + `${legacyRecoveryHint(Object.keys(state.authorizable), [], state.active_ids)}${unsupported} See ${ERROR_CATALOGUE.legacy_job_authority.docs}.`,
+ + `${legacyRecoveryHint(state.active_ids)}${unsupported} See ${ERROR_CATALOGUE.legacy_job_authority.docs}.`,
};
}
@@ -85,9 +85,7 @@ export async function legacyJobAuthorityBannerNote(engine: BrainEngine): Promise
const state = await readLegacyJobAuthority(engine);
const authorizable = sum(state.authorizable);
if (!authorizable && !state.unsupported) return null;
- const statuses = Object.keys(state.authorizable).filter(status => status !== 'active');
- const preview = `gbrain jobs authorize-legacy --select "status=${(statuses.length ? statuses : ['waiting']).join('|')}"`;
return `legacy_job_authority: ${authorizable + state.unsupported} queued job(s) from before v0.50 block every worker. `
- + `Stop producers (gbrain serve, gbrain autopilot) and workers, cancel active jobs, then preview with: ${preview}`
+ + `Stop producers (gbrain serve, gbrain autopilot) and workers, cancel active jobs, then preview with: ${LIVE_LEGACY_PREVIEW}`
+ `${state.unsupported ? `; ${state.unsupported} unsupported row(s) need matching versions or gbrain jobs cancel <id>` : ''}. Recipe: ${ERROR_CATALOGUE.legacy_job_authority.docs}`;
}
diff --git a/src/commands/jobs/authorize-legacy.ts b/src/commands/jobs/authorize-legacy.ts
index 9219b53821..66ce55b4f5 100644
--- a/src/commands/jobs/authorize-legacy.ts
+++ b/src/commands/jobs/authorize-legacy.ts
@@ -22,7 +22,8 @@ export async function runJobsAuthorizeLegacy({ args, engine }: JobsCommandContex
const selection = parseLegacyJobSelection(select, 'authorize-legacy');
const expected = parseFlag(args, '--expect');
const yes = args.includes('--yes');
- if (expected !== undefined || yes) {
+ // --dry-run always previews, even next to --expect/--yes, matching the --ids path.
+ if (!args.includes('--dry-run') && (expected !== undefined || yes)) {
const result = await applyLegacySelection(engine, selection, expected, yes);
if (json) { console.log(JSON.stringify(result, null, 2)); return; }
console.log(`Authorized ${result.authorized} legacy job(s) matching ${result.selection}. Restart producers and workers now.`);
diff --git a/src/commands/jobs/cancel.ts b/src/commands/jobs/cancel.ts
index 250857325a..cef37e7949 100644
--- a/src/commands/jobs/cancel.ts
+++ b/src/commands/jobs/cancel.ts
@@ -11,7 +11,8 @@ async function runLegacyCancel({ args, engine }: JobsCommandContext): Promise<vo
const selection = parseLegacyJobSelection(parseFlag(args, '--select'), 'cancel');
const expected = parseFlag(args, '--expect');
const yes = args.includes('--yes');
- if (expected !== undefined || yes) {
+ // --dry-run always previews, even next to --expect/--yes, matching the --ids path.
+ if (!args.includes('--dry-run') && (expected !== undefined || yes)) {
const result = await applyLegacyCancel(engine, selection, expected, yes);
if (json) { console.log(JSON.stringify(result, null, 2)); return; }
console.log(`Cancelled ${result.cancelled_ids.length} legacy job(s) matching ${result.selection}.`);
diff --git a/src/core/minions/legacy-selection.ts b/src/core/minions/legacy-selection.ts
index 81d2c06dcc..023c3b64b0 100644
--- a/src/core/minions/legacy-selection.ts
+++ b/src/core/minions/legacy-selection.ts
@@ -78,16 +78,22 @@ export function parseLegacyJobSelection(raw: string | undefined, command: Legacy
/** Shared stop instruction for every legacy-job refusal (ENG-O4: active jobs are cancelled, never finished). */
export const STOP_PRODUCERS = 'Stop producers (gbrain serve, gbrain autopilot) and workers';
+/** Every live status a legacy review can authorize: one preview then covers what a required cancellation leaves behind. */
+export const REVIEWABLE_LIVE_STATUSES = ['waiting', 'delayed', 'waiting-children', 'paused'] as const;
+
+/** The filled preview that reviews every live SQL NULL row the claim gate blocks on. */
+export const LIVE_LEGACY_PREVIEW = selectCommand('authorize-legacy', { statuses: [...REVIEWABLE_LIVE_STATUSES], names: [] });
+
/**
* The recovery order every legacy refusal names (DX-O3(b)): stop producers
- * and workers, cancel active jobs, preview with the filled `--select`, apply
- * with the printed hash, restart.
+ * and workers, cancel active jobs, preview every reviewable live status (a
+ * cancellation can move a parent from waiting-children to waiting, so a
+ * filter built from today's statuses could miss it), apply with the printed
+ * hash, restart.
*/
-export function legacyRecoveryHint(statuses: Iterable<string>, names: string[] = [], activeIds: readonly number[] = []): string {
- const reviewable = [...new Set(statuses)].filter(status => SELECT_STATUSES['authorize-legacy'].includes(status));
- const preview = selectCommand('authorize-legacy', { statuses: reviewable.length ? reviewable : ['waiting'], names });
+export function legacyRecoveryHint(activeIds: readonly number[] = []): string {
const cancels = activeIds.length ? activeIds.slice(0, 10).map(id => `gbrain jobs cancel ${id}`).join('; ') : 'gbrain jobs cancel <id>';
- return `${STOP_PRODUCERS}, cancel active jobs (${cancels}), preview with ${preview}, apply with the printed --expect <hash> --yes, then restart them.`;
+ return `${STOP_PRODUCERS}, cancel active jobs (${cancels}), preview with ${LIVE_LEGACY_PREVIEW}, apply with the printed --expect <hash> --yes, then restart them.`;
}
/** `legacy_jobs_active`: legacy rows are reviewed only with nothing active. Lists up to 10 ids to cancel. */
diff --git a/src/core/minions/submission-authority.ts b/src/core/minions/submission-authority.ts
index cc19b889ec..57cf1bed99 100644
--- a/src/core/minions/submission-authority.ts
+++ b/src/core/minions/submission-authority.ts
@@ -297,8 +297,7 @@ export function legacyJobAuthorityError(row: Record<string, unknown>, activeIds:
if (status === 'active') {
return catalogueError('legacy_job_authority', what, `${STOP_PRODUCERS}, then cancel it: gbrain jobs cancel ${id}; run gbrain doctor to review the rest.`);
}
- // Name every reviewable live status: one preview and apply then clears the whole SQL NULL population the claim gate blocks on.
- return catalogueError('legacy_job_authority', what, legacyRecoveryHint(LIVE_JOB_STATUSES, [], activeIds));
+ return catalogueError('legacy_job_authority', what, legacyRecoveryHint(activeIds));
}
const RELEASABLE = new Set(['dead', 'cancelled']);
@@ -334,6 +333,6 @@ export async function assertNoUnreviewedJobs(engine: BrainEngine): Promise<void>
const unsupported = invalid.filter(row => row.legacy_authority_is_null !== true).length;
throw catalogueError('legacy_job_authority',
`Queued job authorization: ${invalid.length} legacy jobs have missing or unsupported authority, so workers cannot start until they are reviewed.`,
- `${legacyRecoveryHint(invalid.filter(row => row.legacy_authority_is_null === true).map(row => row.status))}${unsupported
+ `${legacyRecoveryHint()}${unsupported
? ` ${unsupported} job(s) carry unsupported non-NULL authority: run matching application and database versions, or cancel them with gbrain jobs cancel <id> (gbrain doctor lists them).` : ''}`);
}
diff --git a/test/doctor-legacy-job-authority.test.ts b/test/doctor-legacy-job-authority.test.ts
index 0827c6546b..a2a290d2a4 100644
--- a/test/doctor-legacy-job-authority.test.ts
+++ b/test/doctor-legacy-job-authority.test.ts
@@ -78,7 +78,7 @@ describe('legacy_job_authority doctor check', () => {
expect(check.message).toContain('6 queued job(s) predate submission authority and block every worker (4 authorizable with SQL NULL authority, 2 unsupported)');
expect(check.message).toContain('Stop producers (gbrain serve, gbrain autopilot) and workers');
expect(check.message).toContain(`cancel active jobs (gbrain jobs cancel ${active})`);
- expect(check.message).toContain('preview with gbrain jobs authorize-legacy --select "status=waiting|paused"');
+ expect(check.message).toContain('preview with gbrain jobs authorize-legacy --select "status=waiting|delayed|waiting-children|paused"');
expect(check.message).toContain('apply with the printed --expect <hash> --yes, then restart them');
expect(check.message).toContain(`gbrain jobs cancel ${jsonbNull}; gbrain jobs cancel ${future}`);
expect(check.message).toContain('docs/guides/repair.md#legacy-job-authority');
@@ -92,7 +92,7 @@ describe('legacy_job_authority doctor check', () => {
const lines = await postUpgradeRecoveryBanner(engine, 'host (pglite, id test-brain)');
const text = lines.join('\n');
expect(text).toContain('[AGENT] legacy_job_authority: 2 queued job(s) from before v0.50 block every worker.');
- expect(text).toContain('preview with: gbrain jobs authorize-legacy --select "status=waiting"');
+ expect(text).toContain('preview with: gbrain jobs authorize-legacy --select "status=waiting|delayed|waiting-children|paused"');
expect(text).toContain('1 unsupported row(s) need matching versions or gbrain jobs cancel <id>');
expect(text).not.toContain('--yes');
expect(text).not.toContain('--apply');
diff --git a/test/jobs-authorize-legacy-select.test.ts b/test/jobs-authorize-legacy-select.test.ts
index 2115d6bdc5..240cc1a397 100644
--- a/test/jobs-authorize-legacy-select.test.ts
+++ b/test/jobs-authorize-legacy-select.test.ts
@@ -16,6 +16,7 @@ import { OperationError } from '../src/core/ops/contract.ts';
import { operations } from '../src/core/operations.ts';
import { runJobs } from '../src/commands/jobs.ts';
import { _resetCliExitVerdictForTests, currentExitCode } from '../src/core/cli-force-exit.ts';
+import { argv } from './helpers/legacy-journey.ts';
let engine: PGLiteEngine;
let queue: MinionQueue;
@@ -197,6 +198,26 @@ describe('CLI transport (ENG-O12) and CLI-only pin (ENG-O13)', () => {
expect(applied.out).toContain('Authorized 2 legacy job(s) matching status=waiting|paused.');
});
+ test('--dry-run next to --expect/--yes previews and authorizes nothing (Codex review)', async () => {
+ const a = await legacy('synthesize');
+ const json = JSON.parse((await cli(['authorize-legacy', '--select', 'status=waiting', '--json'])).out);
+ const dry = await cli(['authorize-legacy', '--select', 'status=waiting', '--expect', json.snapshot_digest, '--yes', '--dry-run']);
+ expect(dry.out).toContain('Nothing was changed.');
+ expect((await authorities())[a]).toBeNull();
+ });
+
+ test('the printed recovery still selects a parent that a required cancel moved to waiting (Codex review)', async () => {
+ const parent = await legacy('fanout', 'waiting');
+ const child = await queue.add('busy', {}, { parent_job_id: parent });
+ await engine.executeRaw(`UPDATE minion_jobs SET status = 'active', lock_token = 't', lock_until = now() + interval '1 hour', claim_generation = claim_generation + 1 WHERE id = $1`, [child.id]);
+ const gate = await refusal(() => assertNoUnreviewedJobs(engine));
+ const preview = /preview with (gbrain jobs authorize-legacy --select "[^"]+")/.exec(gate.suggestion!)![1]!;
+ await queue.cancelJob(child.id);
+ expect((await queue.getJob(parent))?.status).toBe('waiting');
+ const printed = await cli(argv(preview).slice(2));
+ expect(printed.out).toContain('(SQL NULL authority, authorizable): 1');
+ });
+
test('a refusal prints the toJSON envelope with --json and exits 1', async () => {
const result = await cli(['authorize-legacy', '--select', 'state=waiting', '--json']);
expect(result.exit).toBe(1);
diff --git a/test/jobs-cancel-select.test.ts b/test/jobs-cancel-select.test.ts
index c14c1fbb75..ec60ef70ad 100644
--- a/test/jobs-cancel-select.test.ts
+++ b/test/jobs-cancel-select.test.ts
@@ -138,6 +138,10 @@ describe('jobs cancel --select', () => {
const preview = JSON.parse(out.join('\n'));
expect(preview.rows.map((r: { id: number }) => r.id)).toEqual([a]);
out.length = 0;
+ await runJobs(engine, ['cancel', '--select', 'status=waiting', '--expect', preview.preview_hash, '--yes', '--dry-run']);
+ expect(out.join('\n')).toContain('Nothing was changed.');
+ expect((await statuses())[a]).toBe('waiting');
+ out.length = 0;
await runJobs(engine, ['cancel', '--select', 'status=waiting', '--expect', preview.preview_hash, '--yes']);
expect(out.join('\n')).toBe('Cancelled 1 legacy job(s) matching status=waiting.');
out.length = 0;