Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
/** Durable authority for queued work. Payloads and caller-spread options are never authority. */
import { AsyncLocalStorage } from 'node:async_hooks';
import { createHash } from 'node:crypto';
import { realpathSync, statSync } from 'node:fs';
import { isAbsolute } from 'node:path';
import type { BrainEngine } from '../engine.ts';
import type { AuthInfo, OperationContext } from '../ops/contract.ts';
import { OperationError, opError } from '../ops/contract.ts';
import { catalogueError } from '../error-catalogue.ts';
import { STOP_PRODUCERS, legacyRecoveryHint, selectCommand } from './legacy-selection.ts';
import { normalizeSlugPrefix } from '../ops/context.ts';
import { hasScope } from '../scope.ts';
import { authSourcesFromGrant, grantFromTokenRow } from '../grants/model.ts';
import { isValidSourceId } from '../source-id.ts';
import { parseStoredActor } from '../access/actor-model.ts';
import { discoverGitRoot } from '../sync-git.ts';
import type { MinionJob } from './types.ts';
export const REMOTE_JOB_NAMES = ['sync', 'import', 'lint', 'lint-fix'] as const;
export type RemoteJobName = typeof REMOTE_JOB_NAMES[number];
export type SubmissionAuthority = { version: 1; kind: 'application' } | RemoteJobAuthority | RemoteAgentAuthority;
export interface RemoteAgentAuthority {
version: 1;
kind: 'remote_agent';
principal: { kind: 'oauth_client'; id: string };
/**
* GBRA-231: `actingFor` is the audience a shared agent asserted when it submitted the job. Every tool call the job
* makes reads as that audience, re-resolved against the client's live may_act_for, never as the client's own member.
*/
grant: { scopes: string[]; sourceId: string; sourceCreatedAt: string; allowedTools: string[]; allowedSlugPrefixes: string[]; actingFor?: string[];
/** GST-359 C1: the attested actor the job was submitted for; every tool call re-resolves it through actorCoverage. */
actor?: { system: string; external_id: string; member: string } };
payloadHash: string;
}
export interface RemoteJobAuthority {
version: 1;
kind: 'remote_generic';
principal: NonNullable<AuthInfo['principal']>;
grant: {
scopes: string[];
sourceId: string;
sourceCreatedAt: string;
canonicalRoot: string;
worktreeRoot: string | null;
jobName: RemoteJobName;
allowedOperations?: string[] | null;
};
payloadHash: string;
}
export const APPLICATION_AUTHORITY: SubmissionAuthority = Object.freeze({ version: 1, kind: 'application' });
const executionAuthority = new AsyncLocalStorage<{ authority: SubmissionAuthority; signal?: AbortSignal }>();
export function currentSubmissionAuthority(): SubmissionAuthority | undefined { return executionAuthority.getStore()?.authority; }
export function currentRemoteJobAuthority(): RemoteJobAuthority | undefined {
const a = executionAuthority.getStore()?.authority;
return a?.kind === 'remote_generic' ? a : undefined;
}
export function currentJobSignal(): AbortSignal | undefined { return executionAuthority.getStore()?.signal; }
export function withSubmissionAuthority<T>(authority: SubmissionAuthority, fn: () => T, signal?: AbortSignal): T {
return executionAuthority.run({ authority, signal }, fn);
}
function deny(message: string, suggestion: string): never {
throw opError('permission_denied', `Queued job authorization: ${message}`, suggestion);
}
function record(value: unknown): Record<string, unknown> | undefined {
return value !== null && typeof value === 'object' && !Array.isArray(value)
? value as Record<string, unknown> : undefined;
}
/** Stable digest also used by local legacy-review CAS; no credentials are hashed into payloads. */
export function authorityDigest(value: unknown): string {
const stable = (v: unknown): unknown => Array.isArray(v) ? v.map(stable)
: typeof v === 'bigint' ? v.toString()
: v instanceof Date ? v.toISOString()
: record(v) ? Object.fromEntries(Object.keys(v as object).sort().map(k => [k, stable((v as Record<string, unknown>)[k])]))
: v;
return createHash('sha256').update(JSON.stringify(stable(value))).digest('hex');
}
export function parseSubmissionAuthority(value: unknown): SubmissionAuthority | null {
if (typeof value === 'string') { try { value = JSON.parse(value); } catch { return null; } }
const a = record(value);
if (a?.version !== 1) return null;
if (a.kind === 'application') return APPLICATION_AUTHORITY;
const p = record(a.principal), g = record(a.grant);
if (a.kind === 'remote_agent') {
if (p?.kind !== 'oauth_client' || typeof p.id !== 'string' || !p.id || !g ||
!Array.isArray(g.scopes) || !g.scopes.every(v => typeof v === 'string') ||
typeof g.sourceId !== 'string' || !isValidSourceId(g.sourceId) || typeof g.sourceCreatedAt !== 'string' ||
!Array.isArray(g.allowedTools) || !g.allowedTools.length || !g.allowedTools.every(v => typeof v === 'string' && v.length) ||
!Array.isArray(g.allowedSlugPrefixes) || !g.allowedSlugPrefixes.every(v => typeof v === 'string') ||
(g.actingFor !== undefined && (!Array.isArray(g.actingFor) || !g.actingFor.length || g.actingFor.length > 50 || !g.actingFor.every(v => typeof v === 'string' && v.length))) ||
(g.actor !== undefined && (g.actingFor !== undefined || !parseStoredActor(g.actor))) ||
typeof a.payloadHash !== 'string' || !/^[a-f0-9]{64}$/.test(a.payloadHash)) return null;
return structuredClone(a) as unknown as RemoteAgentAuthority;
}
if (a.kind !== 'remote_generic' || !p || !g ||
!['oauth_client', 'legacy_token'].includes(String(p.kind)) || typeof p.id !== 'string' || !p.id ||
!Array.isArray(g.scopes) || !g.scopes.every(s => typeof s === 'string') ||
(g.allowedOperations !== undefined && g.allowedOperations !== null &&
(!Array.isArray(g.allowedOperations) || !g.allowedOperations.every(op => typeof op === 'string' && op.length))) ||
typeof g.sourceId !== 'string' || !isValidSourceId(g.sourceId) ||
typeof g.sourceCreatedAt !== 'string' || typeof g.canonicalRoot !== 'string' || !g.canonicalRoot ||
!(g.worktreeRoot === null || typeof g.worktreeRoot === 'string') ||
!REMOTE_JOB_NAMES.includes(g.jobName as RemoteJobName) || typeof a.payloadHash !== 'string' ||
!/^[a-f0-9]{64}$/.test(a.payloadHash)) return null;
return structuredClone(a) as unknown as RemoteJobAuthority;
}
export function assertSameAuthority(actual: unknown, expected: SubmissionAuthority): void {
const parsed = parseSubmissionAuthority(actual);
if (!parsed || authorityDigest(parsed) !== authorityDigest(expected)) deny('coalescing across submission authorities is forbidden',
'A job with this idempotency key was queued under another principal or grant. Submit with a new idempotency key, or leave that job to its submitter.');
}
async function sourceBoundary(engine: BrainEngine, sourceId: string, jobName: RemoteJobName) {
const [source] = await engine.executeRaw<Record<string, unknown>>(
'SELECT id, local_path, config, archived, created_at FROM sources WHERE id = $1', [sourceId]);
if (!source || source.archived !== false || typeof source.local_path !== 'string' || !source.local_path) {
deny('an active source with a registered filesystem root is required',
`Source ${sourceId} must be active and registered with a directory on the brain host. The brain host operator checks it with gbrain sources list --json.`);
}
let config = source.config;
if (typeof config === 'string') { try { config = JSON.parse(config); } catch { deny('malformed source config', `Source ${sourceId}'s stored config is not valid JSON on the brain host, so nothing was queued. The brain host operator inspects it with gbrain sources status ${sourceId} --json.`); } }
if (!record(config) || record(config)!.kind != null) deny('generic filesystem jobs require an ordinary filesystem source; use the dedicated connector endpoint',
`Source ${sourceId} is a connector source: the brain host syncs it with gbrain connectors (connector_sync), not through submit_job.`);
if (!isAbsolute(source.local_path)) deny('registered source root is a relative path',
`Source ${sourceId}'s registered root is a relative path, which names whatever directory the brain host runs from (#7630), so nothing was queued. The brain host operator sets it with gbrain sources set-path ${sourceId} <absolute-directory>.`);
let canonicalRoot: string;
try {
canonicalRoot = realpathSync(source.local_path);
if (!statSync(canonicalRoot).isDirectory()) deny('registered source root must be a directory',
`Source ${sourceId}'s registered root is not a directory on the brain host. The brain host operator restores the checkout or re-points it with gbrain sources set-path.`);
} catch { deny('registered source root is unavailable or is not a directory',
`Source ${sourceId}'s registered root is missing on the brain host. The brain host operator restores the checkout or re-points it with gbrain sources set-path.`); }
let worktreeRoot: string | null = null;
try { worktreeRoot = realpathSync(discoverGitRoot(canonicalRoot)); }
catch { if (jobName === 'sync') deny('remote sync requires an existing Git working tree; initialize or restore it locally',
`Source ${sourceId}'s root is not inside a Git working tree, so sync has nothing to diff. Submit import for it instead, or have the brain host operator restore the checkout.`); }
return { canonicalRoot, worktreeRoot, sourceCreatedAt: new Date(source.created_at as string).toISOString() };
}
export async function assertCurrentRemoteJobPrincipal(engine: BrainEngine, authority: RemoteJobAuthority): Promise<void> {
const { principal, grant } = authority;
let scopes: string[], sourceId: string | undefined;
if (principal.kind === 'oauth_client') {
// No fallback projections: an incomplete auth schema cannot authorize background work.
const [row] = await engine.executeRaw<Record<string, unknown>>(
'SELECT client_id, deleted_at, scope, source_id, bound_slug_prefixes, surface, allowed_operations FROM oauth_clients WHERE client_id = $1', [principal.id]);
if (!row || row.deleted_at != null) deny('OAuth client is missing or revoked', `OAuth client ${principal.id} was deleted or revoked, so this job does not run. Reconnect with a current client and submit a new job.`);
scopes = typeof row.scope === 'string' ? row.scope.split(/\s+/).filter(Boolean) : [];
sourceId = typeof row.source_id === 'string' ? row.source_id : undefined;
if (row.bound_slug_prefixes != null) deny('bulk filesystem jobs are unavailable to slug-bound clients',
`Client ${principal.id} is bound to slug prefixes, so it cannot run whole-source jobs. Use page writes, or ask the brain host operator for an unrestricted source grant.`);
if (row.surface != null && row.surface !== 'full') deny('current client surface does not permit generic jobs',
`Client ${principal.id} is on the ${String(row.surface)} surface; generic jobs need full. Ask the brain host operator to widen this client's surface, then submit a new job.`);
for (const operations of [grant.allowedOperations, row.allowed_operations]) {
if (operations != null && (!Array.isArray(operations) || !operations.every(op => typeof op === 'string') || !operations.includes('submit_job'))) {
deny('original and current operation grants must authorize submit_job',
`Client ${principal.id}'s operation allowlist does not include submit_job. Ask the brain host operator to add it, then submit a new job.`);
}
}
} else {
const [row] = await engine.executeRaw<Record<string, unknown>>(
'SELECT * FROM access_tokens WHERE id = $1', [principal.id]);
if (!row || row.revoked_at != null) deny('legacy token is missing or revoked', `Legacy token ${principal.id} was revoked, so this job does not run. Reconnect with a current credential and submit a new job.`);
const tokenGrant = grantFromTokenRow(row);
if (tokenGrant.permissionsMalformed) deny('malformed token permissions', `Legacy token ${principal.id}'s stored permissions cannot be read. Ask the brain host operator to reissue the credential, then submit a new job.`);
scopes = tokenGrant.scopes;
sourceId = authSourcesFromGrant(tokenGrant).sourceId;
}
if (!hasScope(grant.scopes, 'admin') || !hasScope(scopes, 'admin') || sourceId !== grant.sourceId) {
deny('the original grant and current principal must both authorize this source and admin operation',
`This job needs the admin scope on source ${grant.sourceId} both when submitted and now. Ask the brain host operator to grant it, then submit a new job.`);
}
}
export async function prepareRemoteJob(
ctx: OperationContext, name: string, input: unknown,
): Promise<{ data: Record<string, unknown>; authority: RemoteJobAuthority }> {
if (!REMOTE_JOB_NAMES.includes(name as RemoteJobName)) deny('unsupported remote job; only sync, import, lint and lint-fix are available through remote submit_job',
`Remote submit_job queues sync, import, lint and lint-fix only. Use the dedicated operation for ${name} work, or ask the user to run it with the gbrain CLI on the brain host.`);
const principal = ctx.auth?.principal;
const sourceId = ctx.auth?.sourceId;
if (!principal || !sourceId || !isValidSourceId(sourceId) || ctx.sourceId !== sourceId || !hasScope(ctx.auth?.scopes ?? [], 'admin')) {
deny('an authenticated persistent principal and scalar write source are required; use local CLI or a dedicated operation',
'Connect with a persistent OAuth client bound to one write source and holding the admin scope, or ask the user to run the job with the gbrain CLI on the brain host.');
}
if (ctx.auth?.boundSlugPrefixes !== undefined || ctx.auth?.fenceProjectionDegraded) deny('bulk filesystem jobs require an unrestricted source grant',
'This connection is slug-bound (or its grant projection is degraded), so it cannot queue whole-source jobs. Use page writes, or ask the brain host operator for an unrestricted source grant.');
const raw = input === undefined ? {} : record(input);
if (!raw) deny('job data must be an object', 'Send data as a JSON object (for sync, {"noPull": true}), or omit it.');
const allowed = name === 'sync' ? ['pull', 'noPull'] : [];
for (const key of Object.keys(raw)) if (!allowed.includes(key)) deny('unsupported remote job parameter', `Remove ${key}; remote ${name} accepts ${allowed.length ? allowed.join(' or ') : 'no data fields'}.`);
for (const key of allowed) if (raw[key] !== undefined && typeof raw[key] !== 'boolean') deny(`${key} must be a boolean`, `Send ${key} as true or false.`);
if (raw.pull !== undefined && raw.noPull !== undefined) deny('supply only one of pull and noPull', 'Send pull or noPull, not both.');
const boundary = await sourceBoundary(ctx.engine, sourceId, name as RemoteJobName);
const wholeWorktree = boundary.canonicalRoot === boundary.worktreeRoot;
const pull = raw.pull ?? (raw.noPull !== undefined ? !raw.noPull : wholeWorktree);
if (name === 'sync' && pull && !wholeWorktree) deny('pull is unavailable for a source nested inside another Git working tree',
`Source ${sourceId} sits inside a larger Git working tree, so it cannot pull. Submit sync with {"noPull": true}.`);
const data: Record<string, unknown> = name === 'sync'
? { repoPath: boundary.canonicalRoot, sourceId, noPull: !pull, noEmbed: true, noExtract: true, auto_embed_backfill: false }
: { dir: boundary.canonicalRoot, sourceId, ...(name === 'import' ? { noEmbed: true } : {}) };
const authority: RemoteJobAuthority = {
version: 1, kind: 'remote_generic', principal: { ...principal },
grant: { scopes: [...ctx.auth!.scopes], sourceId, ...boundary, jobName: name as RemoteJobName,
allowedOperations: ctx.auth?.allowedOperations == null ? null : [...ctx.auth.allowedOperations] },
payloadHash: authorityDigest(data),
};
await assertCurrentRemoteJobPrincipal(ctx.engine, authority);
return { data, authority };
}
const toolName = (name: string) => name.replace(/^(?:brain_|mcp__gbrain__)/, '');
async function assertCurrentAgent(engine: BrainEngine, a: RemoteAgentAuthority, data: Record<string, unknown>, jobId?: number): Promise<void> {
const [source] = await engine.executeRaw<Record<string, unknown>>('SELECT archived, created_at FROM sources WHERE id = $1', [a.grant.sourceId]);
if (!source || source.archived !== false || new Date(source.created_at as string).toISOString() !== a.grant.sourceCreatedAt) deny('agent source is missing, archived, or was replaced',
`Source ${a.grant.sourceId} was archived, removed or recreated after this agent job was accepted. Submit a new agent job against the current source.`);
if (data.__delegation_grant !== undefined) {
// Grant profiles reference the complete operation catalog. Filesystem
// writers need this module during catalog initialization; load the policy
// only when an actual delegated job is being checked.
const { effectiveDelegation, snapshotFromJob } = await import('./delegated-policy.ts');
const submitted = snapshotFromJob(data);
if (!submitted || submitted.clientId !== a.principal.id || submitted.sourceId !== a.grant.sourceId
|| authorityDigest(submitted.tools) !== authorityDigest(a.grant.allowedTools)
|| authorityDigest(submitted.slugPrefixes) !== authorityDigest(a.grant.allowedSlugPrefixes)
|| !hasScope(a.grant.scopes, 'agent')) deny('delegation snapshot differs from its accepted authority',
'The delegation snapshot in this job no longer matches its accepted grant, so it does not run. Submit a new agent job under the current grant.');
// Direct and delegated namespaces are independent. The current grant may
// narrow tools/paths, while the submitted snapshot remains the ceiling.
await effectiveDelegation(engine, submitted, jobId);
return;
}
// Preserve the older explicit authority shape for already accepted jobs.
const [row] = await engine.executeRaw<Record<string, unknown>>(
'SELECT deleted_at, scope, source_id, bound_tools, bound_source_id, bound_slug_prefixes FROM oauth_clients WHERE client_id = $1', [a.principal.id]);
if (!row || row.deleted_at != null) deny('agent owner is missing or revoked', `OAuth client ${a.principal.id}, which owns this agent job, was deleted or revoked. Reconnect with a current client and submit a new job.`);
const scopes = typeof row.scope === 'string' ? row.scope.split(/\s+/) : [];
if (!hasScope(a.grant.scopes, 'agent') || !hasScope(scopes, 'agent')) deny('original and current grants must include agent scope',
`Client ${a.principal.id} does not hold the agent scope now (or did not at submission). Ask the brain host operator to grant it, then submit a new job.`);
if (row.source_id !== a.grant.sourceId || (row.bound_source_id != null && row.bound_source_id !== a.grant.sourceId)) deny('agent source grant changed',
`Client ${a.principal.id} is now bound to another source than ${a.grant.sourceId}. Submit a new agent job under the current grant.`);
if (!Array.isArray(row.bound_tools) || !row.bound_tools.length ||
!row.bound_tools.every(t => typeof t === 'string' && t.length && !['file_list', 'file_url'].includes(toolName(t)))) deny('current agent tool binding is empty or unsupported',
`Client ${a.principal.id} has no usable bound tools (file_list and file_url cannot be agent tools). Ask the brain host operator to bind the agent's tools, then submit a new job.`);
const tools = row.bound_tools.map(t => toolName(t as string));
if (a.grant.allowedTools.some(t => !tools.includes(toolName(t)))) deny('accepted agent tools are outside the current binding',
`Client ${a.principal.id} no longer allows ${a.grant.allowedTools.filter(t => !tools.includes(toolName(t))).join(', ')}. Submit a new agent job with tools from its current binding.`);
const prefixes = row.bound_slug_prefixes;
if (prefixes != null) {
if (!Array.isArray(prefixes) || !prefixes.length || !prefixes.every(p => typeof p === 'string' && p.length) || !a.grant.allowedSlugPrefixes.length) deny('agent slug binding changed',
`Client ${a.principal.id}'s slug binding changed after this job was accepted. Submit a new agent job under the current binding.`);
for (const granted of a.grant.allowedSlugPrefixes) {
const requested = normalizeSlugPrefix(granted);
if (!prefixes.some(p => { const base = normalizeSlugPrefix(p as string); return base && (base.endsWith('/') ? requested.startsWith(base) : requested === base || requested.startsWith(`${base}/`)); })) deny('accepted agent slug grant is outside the current binding',
`Slug prefix ${granted} is outside client ${a.principal.id}'s current binding. Submit a new agent job with prefixes inside it.`);
}
}
}
export async function prepareRemoteAgent(ctx: OperationContext, data: Record<string, unknown>): Promise<RemoteAgentAuthority> {
const principal = ctx.auth?.principal;
if (principal?.kind !== 'oauth_client' || principal.id !== ctx.auth?.clientId || !hasScope(ctx.auth?.scopes ?? [], 'agent') ||
typeof data.source_id !== 'string' || data.source_id !== ctx.auth?.sourceId || !isValidSourceId(data.source_id)) deny('submit_agent requires a verified OAuth principal and scalar source grant',
'submit_agent needs an OAuth client holding the agent scope and bound to the one source named in source_id. Ask the brain host operator for such a client, or ask the user to run gbrain agent run on the brain host.');
const [source] = await ctx.engine.executeRaw<Record<string, unknown>>('SELECT archived, created_at FROM sources WHERE id = $1', [data.source_id]);
if (!source || source.archived !== false) deny('agent source must be active', `Source ${data.source_id} is archived or missing. Name an active source in source_id.`);
const { currentCallerAccess } = await import('../search/private-visibility.ts');
const { actor, audience: actingFor } = currentCallerAccess();
const authority: RemoteAgentAuthority = {
version: 1, kind: 'remote_agent', principal: { kind: 'oauth_client', id: principal.id },
grant: { scopes: [...ctx.auth!.scopes], sourceId: data.source_id, sourceCreatedAt: new Date(source.created_at as string).toISOString(),
allowedTools: [...data.allowed_tools as string[]], allowedSlugPrefixes: [...data.allowed_slug_prefixes as string[]],
...(actor ? { actor: { ...actor } } : actingFor?.length ? { actingFor: [...actingFor] } : {}) },
payloadHash: authorityDigest(data),
};
if (!parseSubmissionAuthority(authority)) deny('invalid agent grant', 'Send allowed_tools as a non-empty list of tool names and allowed_slug_prefixes as a list of strings.');
await assertCurrentAgent(ctx.engine, authority, data);
return authority;
}
/** Runs in BOTH inline and isolated workers, immediately before the handler. */
export async function authorizeJobExecution(engine: BrainEngine, job: Pick<MinionJob, 'name' | 'data' | 'submission_authority'> & Partial<Pick<MinionJob, 'id'>>): Promise<SubmissionAuthority> {
const a = parseSubmissionAuthority(job.submission_authority);
if (!a) deny('missing or unsupported submission authority; review locally with jobs authorize-legacy',
'This queued job has no readable submission authority, so it does not run until reviewed. On the brain host, gbrain jobs authorize-legacy lists it for the user to approve or cancel.');
if (a.kind === 'application') return a;
if (a.kind === 'remote_agent') {
if (job.name !== 'subagent' || authorityDigest(job.data) !== a.payloadHash) deny('agent payload differs from its accepted grant',
'This agent job\'s data changed after it was accepted, so it does not run. Submit a new agent job.');
await assertCurrentAgent(engine, a, job.data, job.id);
return a;
}
if (job.name !== a.grant.jobName || authorityDigest(job.data) !== a.payloadHash) deny('job data or name differs from its accepted grant',
`This ${job.name} job's data changed after it was accepted, so it does not run. Submit a new job.`);
await assertCurrentRemoteJobPrincipal(engine, a);
const boundary = await sourceBoundary(engine, a.grant.sourceId, a.grant.jobName);
for (const key of ['canonicalRoot', 'worktreeRoot', 'sourceCreatedAt'] as const) {
if (boundary[key] !== a.grant[key]) deny('source registration or filesystem root changed; submit a new job',
`Source ${a.grant.sourceId}'s ${key} changed after this job was accepted. Submit a new job against the current registration.`);
}
return a;
}
export async function assertRemoteJobControl(ctx: OperationContext, job: MinionJob, overrides?: unknown): Promise<void> {
if (ctx.remote === false) return;
const a = parseSubmissionAuthority(job.submission_authority);
if (!a || a.kind === 'application' || !ctx.auth?.principal ||
authorityDigest(a.principal) !== authorityDigest(ctx.auth.principal)) deny('only the submitting principal may restart a generic remote job',
'Only the principal that submitted this job can restart it. Submit a new job from this connection instead.');
if (overrides !== undefined && (!record(overrides) || Object.keys(overrides as object).length)) deny('replay overrides are unavailable remotely; submit a new job',
'Restart the job without overrides, or submit a new job with the changed data.');
if (!hasScope(ctx.auth.scopes, 'admin') || ctx.auth.sourceId !== a.grant.sourceId) deny('current request is outside the original grant',
`Restarting needs the admin scope on source ${a.grant.sourceId}, which this connection lacks. Restart it from a connection holding that grant.`);
await authorizeJobExecution(ctx.engine, job);
}
/** Statuses the claim gate covers: a row in one of them blocks every worker while its authority is unparsable. */
export const LIVE_JOB_STATUSES = ['waiting', 'active', 'delayed', 'waiting-children', 'paused'] as const;
/**
* The claim gate's population predicate, shared by `assertNoUnreviewedJobs`
* and the doctor `legacy_job_authority` check so their counts can never drift.
* Callers keep the rows `parseSubmissionAuthority` rejects.
*/
export const UNREVIEWED_LIVE_JOBS_WHERE = `status IN (${LIVE_JOB_STATUSES.map(s => `'${s}'`).join(',')})
AND submission_authority IS DISTINCT FROM '{"version":1,"kind":"application"}'::jsonb`;
/** Select-list column every coalesce read adds so SQL NULL stays distinguishable from JSONB null. */
export const LEGACY_AUTHORITY_COLUMN = 'submission_authority IS NULL AS legacy_authority_is_null';
/** `permission_denied` for a job row whose SQL NULL authority predates the v0.50 cutover. */
export function legacyJobAuthorityError(row: Record<string, unknown>, activeIds: readonly number[] = []): OperationError {
const id = String(row.id), name = String(row.name), status = String(row.status);
const what = `Queued job authorization: job ${id} (${name}, ${status}) has no submission authority because it predates the upgrade, so it cannot be reused until it is reviewed.`;
if (status === 'completed' || status === 'failed') {
return catalogueError('legacy_job_authority', what,
`Resubmit with a new idempotency key; a local operator can review the old row with ${selectCommand('authorize-legacy', { statuses: [status], names: [name] })}.`);
}
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.`);
}
return catalogueError('legacy_job_authority', what, legacyRecoveryHint(activeIds));
}
const RELEASABLE = new Set(['dead', 'cancelled']);
/**
* The one coalesce rule for `MinionQueue.add` (idempotency fast path, param
* coalescing, the pending and waiting caps, and the insert race). `row` is a
* raw `minion_jobs` row selected with `LEGACY_AUTHORITY_COLUMN`.
*
* Only SQL NULL authority (rows from before the v0.50 cutover) gets the
* legacy rule: a dead or cancelled key is released, a completed or failed row
* coalesces for application callers only, and a live row is refused for every
* caller because the claim gate keeps workers from ever running it. JSONB
* null, malformed and future-version authority keep the cross-authority
* denial, as does any authority that differs from the caller's.
*/
export function coalesceDecision(row: Record<string, unknown>, authority: SubmissionAuthority): 'coalesce' | 'release' {
const status = String(row.status);
if (row.legacy_authority_is_null === true) {
if (RELEASABLE.has(status)) return 'release';
if ((status === 'completed' || status === 'failed') && authority.kind === 'application') return 'coalesce';
throw legacyJobAuthorityError(row);
}
assertSameAuthority(row.submission_authority, authority);
return RELEASABLE.has(status) ? 'release' : 'coalesce';
}
/** Startup and claim gate: do not let sweeps silently destroy unresolved legacy dependency graphs. */
export async function assertNoUnreviewedJobs(engine: BrainEngine): Promise<void> {
const rows = await engine.executeRaw<{ id: number; submission_authority: unknown }>(
`SELECT id, submission_authority FROM minion_jobs
WHERE ${UNREVIEWED_LIVE_JOBS_WHERE}`);
const invalid = rows.filter(row => !parseSubmissionAuthority(row.submission_authority));
if (!invalid.length) return;
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()} Rows with unsupported non-NULL authority need matching application and database versions, or gbrain jobs cancel <id>; gbrain doctor lists them.`);
}