mirror of
https://github.com/whyour/qinglong.git
synced 2026-09-20 16:07:11 +08:00
feat(ql3): operationalize cancellation rearm
This commit is contained in:
@@ -1,4 +1,16 @@
|
||||
export { PostgresRunManualRetryRepository } from '../run-management/runManualRetryRepository';
|
||||
export {
|
||||
InvalidRunCancellationDispatchManagementError,
|
||||
PostgresRunCancellationDispatchManagementRepository,
|
||||
RunCancellationDispatchManagementConflictError,
|
||||
RunCancellationDispatchManagementNotFoundError,
|
||||
RunCancellationDispatchManagementUnavailableError,
|
||||
type BlockingCancellationDispatchResult,
|
||||
type PostgresRunCancellationDispatchInspectCommand,
|
||||
type PostgresRunCancellationDispatchRearmCommand,
|
||||
type RunCancellationDispatchDiagnostic,
|
||||
type RunCancellationDispatchRearmReceipt,
|
||||
} from '../run-management/runCancellationDispatchManagementRepository';
|
||||
export {
|
||||
PostgresClusterRunCancellationRepository,
|
||||
type PostgresRunManagementCancellationCommand,
|
||||
|
||||
@@ -338,5 +338,10 @@ export const postgresqlMainMigrationManifest: MigrationStreamManifest =
|
||||
checksum:
|
||||
'b6d7ac81b5f75530df05f8ef05878fa30aa0f4418363973ded89d14ffce151b2',
|
||||
}),
|
||||
Object.freeze({
|
||||
id: 'pg-0067-cancellation-dispatch-management',
|
||||
checksum:
|
||||
'e78e24a06dc4c4dbdd859685f28b4bc837a8cfb279eb3512e0a57dc6d27eaaaa',
|
||||
}),
|
||||
]),
|
||||
});
|
||||
|
||||
@@ -69,6 +69,7 @@ import { pg0063PluginPackageSecretBindingTransitionReceiptsMigration } from './p
|
||||
import { pg0064PluginPackageSecretBindingTransitionApprovalPlansMigration } from './pg-0064-plugin-package-secret-binding-transition-approval-plans';
|
||||
import { pg0065ApprovedActionManualRecoveryMigration } from '../approved-action/pg-0065-approved-action-manual-recovery';
|
||||
import { pg0066CancellationDispatchMigration } from '../run/migrations/pg-0066-cancellation-dispatch';
|
||||
import { pg0067CancellationDispatchManagementMigration } from '../run-management/pg-0067-cancellation-dispatch-management';
|
||||
|
||||
export const postgresqlMainMigrationStream: MigrationStreamDefinition<PostgresMigrationContext> =
|
||||
Object.freeze({
|
||||
@@ -143,5 +144,6 @@ export const postgresqlMainMigrationStream: MigrationStreamDefinition<PostgresMi
|
||||
pg0064PluginPackageSecretBindingTransitionApprovalPlansMigration,
|
||||
pg0065ApprovedActionManualRecoveryMigration,
|
||||
pg0066CancellationDispatchMigration,
|
||||
pg0067CancellationDispatchManagementMigration,
|
||||
]),
|
||||
});
|
||||
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
import { CAPABILITIES_V65 } from '../run/migrations/pg-0066-cancellation-dispatch';
|
||||
import { definePostgresSqlMigration } from '../migrations/sqlMigration';
|
||||
|
||||
export const CAPABILITIES_V66 = CAPABILITIES_V65.replace(
|
||||
'"run_cancellation_dispatch":1,',
|
||||
'"run_cancellation_dispatch":1,"run_cancellation_dispatch_management":1,',
|
||||
);
|
||||
|
||||
export const pg0067CancellationDispatchManagementMigration =
|
||||
definePostgresSqlMigration({
|
||||
id: 'pg-0067-cancellation-dispatch-management',
|
||||
statements: [
|
||||
`ALTER TABLE "ql3"."run_cancellation_dispatches" DROP CONSTRAINT ql3_run_cancellation_dispatch_result_state_check`,
|
||||
`
|
||||
ALTER TABLE "ql3"."run_cancellation_dispatches"
|
||||
ADD CONSTRAINT ql3_run_cancellation_dispatch_result_state_check CHECK (
|
||||
(status = 'pending' AND last_result IS NULL) OR
|
||||
(status IN ('leased', 'retry_wait') AND last_result IN (
|
||||
'identity_mismatch', 'pid_mismatch', 'unsupported', 'invalid',
|
||||
'controller_missing', 'handle_missing', 'dispatch_error'
|
||||
)) OR
|
||||
(status = 'leased' AND last_result IS NULL) OR
|
||||
(status = 'dispatched' AND last_result IN (
|
||||
'termination_requested', 'already_exited'
|
||||
)) OR
|
||||
(status = 'blocked' AND last_result IN (
|
||||
'identity_mismatch', 'pid_mismatch', 'unsupported', 'invalid'
|
||||
))
|
||||
)
|
||||
`.trim(),
|
||||
`GRANT SELECT ON "ql3"."run_cancellation_dispatches" TO ql3_run_manager`,
|
||||
`GRANT UPDATE (status, version, next_attempt_at_ms, updated_at_ms) ON "ql3"."run_cancellation_dispatches" TO ql3_run_manager`,
|
||||
`DO $ql3$ BEGIN UPDATE "ql3"."schema_capabilities" SET contract_version = 66, migration_id = 'pg-0067-cancellation-dispatch-management', capabilities = '${CAPABILITIES_V66}'::jsonb, updated_at_ms = floor(extract(epoch FROM transaction_timestamp()) * 1000)::bigint WHERE contract_name = 'control-core' AND contract_version = 65 AND migration_id = 'pg-0066-cancellation-dispatch' AND capabilities = '${CAPABILITIES_V65}'::jsonb; IF NOT FOUND THEN RAISE EXCEPTION 'control-core capability is not at version 65' USING ERRCODE = 'check_violation'; END IF; END $ql3$`,
|
||||
],
|
||||
});
|
||||
+892
@@ -0,0 +1,892 @@
|
||||
import type { PostgresClient, PostgresPool } from '@qinglong/runtime-core';
|
||||
import {
|
||||
RUN_STATUSES,
|
||||
type RunStatus,
|
||||
type SecurityPolicyFence,
|
||||
type SecurityPrincipal,
|
||||
} from '@qinglong/runtime-core';
|
||||
import {
|
||||
CANCELLATION_DISPATCH_BLOCKING_RESULTS,
|
||||
CANCELLATION_DISPATCH_RESULTS,
|
||||
CANCELLATION_DISPATCH_STATUSES,
|
||||
MAX_CANCELLATION_DISPATCH_RETRY_DELAY_MS,
|
||||
type CancellationDispatchResult,
|
||||
type CancellationDispatchStatus,
|
||||
} from '@qinglong/runtime-core/cancellation-dispatch';
|
||||
import { normalizeSecurityPrincipal } from '@qinglong/runtime-core/security';
|
||||
|
||||
type Row = Record<string, unknown>;
|
||||
|
||||
export type BlockingCancellationDispatchResult =
|
||||
(typeof CANCELLATION_DISPATCH_BLOCKING_RESULTS)[number];
|
||||
|
||||
export type RunCancellationDispatchDiagnostic = Readonly<{
|
||||
projectId: string;
|
||||
runId: string;
|
||||
runStatus: RunStatus;
|
||||
runVersion: number;
|
||||
eventSequence: number;
|
||||
cancelRequestedAtMs?: number;
|
||||
cancelReason?: 'user' | 'policy' | 'shutdown' | 'reconcile' | 'timeout';
|
||||
operatorAction: 'none' | 'wait' | 'rearm';
|
||||
dispatch: Readonly<{
|
||||
attemptId: string;
|
||||
status: CancellationDispatchStatus;
|
||||
version: number;
|
||||
dispatchCount: number;
|
||||
nextAttemptAtMs?: number;
|
||||
leaseExpiresAtMs?: number;
|
||||
lastResult?: CancellationDispatchResult;
|
||||
lastDispatchedAtMs?: number;
|
||||
createdAtMs: number;
|
||||
updatedAtMs: number;
|
||||
}> | null;
|
||||
}>;
|
||||
|
||||
export type RunCancellationDispatchRearmReceipt = Readonly<{
|
||||
status: 'rearmed';
|
||||
projectId: string;
|
||||
runId: string;
|
||||
attemptId: string;
|
||||
previousDispatchVersion: number;
|
||||
dispatchVersion: number;
|
||||
previousResult: BlockingCancellationDispatchResult;
|
||||
retryDelayMs: number;
|
||||
nextAttemptAtMs: number;
|
||||
runVersion: number;
|
||||
eventSequence: number;
|
||||
}>;
|
||||
|
||||
interface ManagementAuthority {
|
||||
readonly projectId: string;
|
||||
readonly runId: string;
|
||||
readonly requestId: string;
|
||||
readonly auditEventId: string;
|
||||
readonly principal: Readonly<SecurityPrincipal>;
|
||||
readonly policyFence: Readonly<SecurityPolicyFence>;
|
||||
}
|
||||
|
||||
export interface PostgresRunCancellationDispatchInspectCommand
|
||||
extends ManagementAuthority {}
|
||||
|
||||
export interface PostgresRunCancellationDispatchRearmCommand
|
||||
extends ManagementAuthority {
|
||||
readonly mutationId: string;
|
||||
readonly eventId: string;
|
||||
readonly expectedDispatchVersion: number;
|
||||
readonly expectedLastResult: BlockingCancellationDispatchResult;
|
||||
readonly retryDelayMs: number;
|
||||
}
|
||||
|
||||
export class InvalidRunCancellationDispatchManagementError extends TypeError {
|
||||
readonly code = 'RUN_CANCELLATION_DISPATCH_MANAGEMENT_INVALID';
|
||||
constructor() {
|
||||
super('Run cancellation dispatch management input is invalid');
|
||||
this.name = 'InvalidRunCancellationDispatchManagementError';
|
||||
}
|
||||
}
|
||||
|
||||
export class RunCancellationDispatchManagementNotFoundError extends Error {
|
||||
readonly code = 'RUN_CANCELLATION_DISPATCH_MANAGEMENT_NOT_FOUND';
|
||||
constructor() {
|
||||
super('Run cancellation dispatch management target is unavailable');
|
||||
this.name = 'RunCancellationDispatchManagementNotFoundError';
|
||||
}
|
||||
}
|
||||
|
||||
export class RunCancellationDispatchManagementConflictError extends Error {
|
||||
readonly code = 'RUN_CANCELLATION_DISPATCH_MANAGEMENT_CONFLICT';
|
||||
constructor(
|
||||
readonly reason:
|
||||
| 'authorization_changed'
|
||||
| 'run_terminal'
|
||||
| 'cancellation_missing'
|
||||
| 'dispatch_missing'
|
||||
| 'dispatch_not_blocked'
|
||||
| 'dispatch_version_changed'
|
||||
| 'dispatch_result_changed'
|
||||
| 'attempt_not_active'
|
||||
| 'mutation_conflict',
|
||||
) {
|
||||
super(`Run cancellation dispatch management conflict: ${reason}`);
|
||||
this.name = 'RunCancellationDispatchManagementConflictError';
|
||||
}
|
||||
}
|
||||
|
||||
export class RunCancellationDispatchManagementUnavailableError extends Error {
|
||||
readonly code = 'RUN_CANCELLATION_DISPATCH_MANAGEMENT_UNAVAILABLE';
|
||||
constructor(options?: ErrorOptions) {
|
||||
super('Run cancellation dispatch management is unavailable', options);
|
||||
this.name = 'RunCancellationDispatchManagementUnavailableError';
|
||||
}
|
||||
}
|
||||
|
||||
const IDENTIFIER_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/u;
|
||||
const UUID_PATTERN =
|
||||
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/u;
|
||||
const STRONG_ASSURANCES = new Set(['multi_factor', 'hardware']);
|
||||
const ACTIVE_RUN_STATUSES = new Set<RunStatus>([
|
||||
'created',
|
||||
'queued',
|
||||
'dispatching',
|
||||
'running',
|
||||
'waiting_approval',
|
||||
'retry_wait',
|
||||
'lost',
|
||||
]);
|
||||
const ACTIVE_ATTEMPT_STATUSES = new Set(['claimed', 'starting', 'running']);
|
||||
const CANCEL_REASONS = new Set([
|
||||
'user',
|
||||
'policy',
|
||||
'shutdown',
|
||||
'reconcile',
|
||||
'timeout',
|
||||
]);
|
||||
const MAX_AUTHENTICATION_AGE_MS = 5 * 60_000;
|
||||
const MIN_MANUAL_RETRY_DELAY_MS = 1_000;
|
||||
const REARM_SCHEMA = 'qinglong/run-cancellation-dispatch-rearm@v1';
|
||||
|
||||
function invalid(): never {
|
||||
throw new InvalidRunCancellationDispatchManagementError();
|
||||
}
|
||||
|
||||
function exact(value: unknown, keys: readonly string[]): Record<string, unknown> {
|
||||
if (!value || typeof value !== 'object' || Array.isArray(value)) invalid();
|
||||
const actual = Object.keys(value as object).sort();
|
||||
const expected = [...keys].sort();
|
||||
if (
|
||||
actual.length !== expected.length ||
|
||||
actual.some((key, index) => key !== expected[index])
|
||||
) {
|
||||
invalid();
|
||||
}
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
|
||||
function text(row: Row, key: string): string {
|
||||
const value = row[key];
|
||||
if (typeof value !== 'string' || value.length < 1) {
|
||||
throw new TypeError(`PostgreSQL cancellation management ${key} is invalid`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function integer(row: Row, key: string): number {
|
||||
const raw = row[key];
|
||||
const value =
|
||||
typeof raw === 'string' && /^(0|[1-9]\d*)$/u.test(raw)
|
||||
? Number(raw)
|
||||
: raw;
|
||||
if (typeof value !== 'number' || !Number.isSafeInteger(value) || value < 0) {
|
||||
throw new TypeError(`PostgreSQL cancellation management ${key} is invalid`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function optionalInteger(row: Row, key: string): number | undefined {
|
||||
return row[key] === null || row[key] === undefined
|
||||
? undefined
|
||||
: integer(row, key);
|
||||
}
|
||||
|
||||
function optionalText(row: Row, key: string): string | undefined {
|
||||
return row[key] === null || row[key] === undefined
|
||||
? undefined
|
||||
: text(row, key);
|
||||
}
|
||||
|
||||
function identifier(value: unknown): string {
|
||||
if (typeof value !== 'string' || !IDENTIFIER_PATTERN.test(value)) invalid();
|
||||
return value;
|
||||
}
|
||||
|
||||
function uuid(value: unknown): string {
|
||||
if (typeof value !== 'string' || !UUID_PATTERN.test(value)) invalid();
|
||||
return value;
|
||||
}
|
||||
|
||||
function boundedInteger(
|
||||
value: unknown,
|
||||
minimum: number,
|
||||
maximum = Number.MAX_SAFE_INTEGER,
|
||||
): number {
|
||||
if (
|
||||
typeof value !== 'number' ||
|
||||
!Number.isSafeInteger(value) ||
|
||||
value < minimum ||
|
||||
value > maximum
|
||||
) {
|
||||
invalid();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function storedInteger(
|
||||
value: unknown,
|
||||
name: string,
|
||||
minimum = 0,
|
||||
maximum = Number.MAX_SAFE_INTEGER,
|
||||
): number {
|
||||
if (
|
||||
typeof value !== 'number' ||
|
||||
!Number.isSafeInteger(value) ||
|
||||
value < minimum ||
|
||||
value > maximum
|
||||
) {
|
||||
throw new TypeError(`PostgreSQL cancellation management ${name} is invalid`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function normalizeAuthority(
|
||||
value: unknown,
|
||||
extraKeys: readonly string[],
|
||||
): Readonly<ManagementAuthority> & Record<string, unknown> {
|
||||
const input = exact(value, [
|
||||
'projectId',
|
||||
'runId',
|
||||
'requestId',
|
||||
'auditEventId',
|
||||
'principal',
|
||||
'policyFence',
|
||||
...extraKeys,
|
||||
]);
|
||||
const principal = exact(input.principal, [
|
||||
'subject',
|
||||
'authenticationId',
|
||||
'authenticatedAtMs',
|
||||
'expiresAtMs',
|
||||
'assurance',
|
||||
]) as unknown as SecurityPrincipal;
|
||||
const fence = exact(input.policyFence, [
|
||||
'projectVersion',
|
||||
'bindingVersion',
|
||||
]);
|
||||
return Object.freeze({
|
||||
...input,
|
||||
projectId: identifier(input.projectId),
|
||||
runId: identifier(input.runId),
|
||||
requestId: identifier(input.requestId),
|
||||
auditEventId: uuid(input.auditEventId),
|
||||
principal,
|
||||
policyFence: Object.freeze({
|
||||
projectVersion: boundedInteger(fence.projectVersion, 1, 2_147_483_647),
|
||||
bindingVersion: boundedInteger(fence.bindingVersion, 1, 2_147_483_647),
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
function normalizeInspectCommand(
|
||||
value: Readonly<PostgresRunCancellationDispatchInspectCommand>,
|
||||
): Readonly<PostgresRunCancellationDispatchInspectCommand> {
|
||||
return normalizeAuthority(value, []);
|
||||
}
|
||||
|
||||
function normalizeRearmCommand(
|
||||
value: Readonly<PostgresRunCancellationDispatchRearmCommand>,
|
||||
): Readonly<PostgresRunCancellationDispatchRearmCommand> {
|
||||
const input = normalizeAuthority(value, [
|
||||
'mutationId',
|
||||
'eventId',
|
||||
'expectedDispatchVersion',
|
||||
'expectedLastResult',
|
||||
'retryDelayMs',
|
||||
]);
|
||||
if (
|
||||
!CANCELLATION_DISPATCH_BLOCKING_RESULTS.includes(
|
||||
input.expectedLastResult as BlockingCancellationDispatchResult,
|
||||
)
|
||||
) {
|
||||
invalid();
|
||||
}
|
||||
const eventId = uuid(input.eventId);
|
||||
if (eventId === input.auditEventId) invalid();
|
||||
return Object.freeze({
|
||||
projectId: input.projectId,
|
||||
runId: input.runId,
|
||||
requestId: input.requestId,
|
||||
auditEventId: input.auditEventId,
|
||||
principal: input.principal,
|
||||
policyFence: input.policyFence,
|
||||
mutationId: uuid(input.mutationId),
|
||||
eventId,
|
||||
expectedDispatchVersion: boundedInteger(
|
||||
input.expectedDispatchVersion,
|
||||
1,
|
||||
2_147_483_646,
|
||||
),
|
||||
expectedLastResult:
|
||||
input.expectedLastResult as BlockingCancellationDispatchResult,
|
||||
retryDelayMs: boundedInteger(
|
||||
input.retryDelayMs,
|
||||
MIN_MANUAL_RETRY_DELAY_MS,
|
||||
MAX_CANCELLATION_DISPATCH_RETRY_DELAY_MS,
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
async function begin(client: PostgresClient): Promise<void> {
|
||||
await client.query('BEGIN ISOLATION LEVEL SERIALIZABLE');
|
||||
await client.query(`SELECT set_config('statement_timeout', $1, true)`, [
|
||||
'5000ms',
|
||||
]);
|
||||
await client.query(`SELECT set_config('lock_timeout', $1, true)`, ['1000ms']);
|
||||
await client.query(
|
||||
`SELECT set_config('idle_in_transaction_session_timeout', $1, true)`,
|
||||
['10000ms'],
|
||||
);
|
||||
}
|
||||
|
||||
async function rollback(client: PostgresClient): Promise<void> {
|
||||
try {
|
||||
await client.query('ROLLBACK');
|
||||
} catch {
|
||||
// Preserve the transaction failure.
|
||||
}
|
||||
}
|
||||
|
||||
async function databaseNow(client: PostgresClient): Promise<number> {
|
||||
const result = await client.query<Row>(`
|
||||
SELECT floor(extract(epoch FROM transaction_timestamp()) * 1000)::bigint
|
||||
AS "nowMs"
|
||||
`);
|
||||
if (result.rows.length !== 1) {
|
||||
throw new TypeError('PostgreSQL cancellation management clock is invalid');
|
||||
}
|
||||
return integer(result.rows[0]!, 'nowMs');
|
||||
}
|
||||
|
||||
function strongPrincipal(
|
||||
value: Readonly<SecurityPrincipal>,
|
||||
observedAtMs: number,
|
||||
): Readonly<SecurityPrincipal> {
|
||||
let principal: Readonly<SecurityPrincipal>;
|
||||
try {
|
||||
principal = normalizeSecurityPrincipal(value, observedAtMs);
|
||||
} catch {
|
||||
throw new RunCancellationDispatchManagementConflictError(
|
||||
'authorization_changed',
|
||||
);
|
||||
}
|
||||
if (
|
||||
principal.subject.type !== 'user' ||
|
||||
!STRONG_ASSURANCES.has(principal.assurance) ||
|
||||
principal.authenticatedAtMs > observedAtMs ||
|
||||
observedAtMs - principal.authenticatedAtMs > MAX_AUTHENTICATION_AGE_MS
|
||||
) {
|
||||
throw new RunCancellationDispatchManagementConflictError(
|
||||
'authorization_changed',
|
||||
);
|
||||
}
|
||||
return principal;
|
||||
}
|
||||
|
||||
async function confirmAuthorization(
|
||||
client: PostgresClient,
|
||||
command: Readonly<ManagementAuthority>,
|
||||
): Promise<void> {
|
||||
const result = await client.query<Row>(
|
||||
`SELECT "ql3"."lock_run_management_policy_fence"(
|
||||
$1::varchar, $2::varchar, $3::varchar, $4::integer, $5::integer
|
||||
) AS "matches"`,
|
||||
[
|
||||
command.projectId,
|
||||
command.principal.subject.type,
|
||||
command.principal.subject.id,
|
||||
command.policyFence.projectVersion,
|
||||
command.policyFence.bindingVersion,
|
||||
],
|
||||
);
|
||||
if (result.rows.length !== 1 || result.rows[0]?.matches !== true) {
|
||||
throw new RunCancellationDispatchManagementConflictError(
|
||||
'authorization_changed',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function recordAllowedAudit(
|
||||
client: PostgresClient,
|
||||
command: Readonly<ManagementAuthority>,
|
||||
operationId: 'run.cancellation.inspect' | 'run.cancellation.rearm',
|
||||
observedAtMs: number,
|
||||
): Promise<void> {
|
||||
const inserted = await client.query<Row>(
|
||||
`INSERT INTO "ql3"."security_audit_events" (
|
||||
event_id, request_id, operation_id, project_id,
|
||||
subject_type, subject_id, authentication_id, outcome, reasons,
|
||||
project_version, binding_version, occurred_at_ms
|
||||
) VALUES ($1, $2, $3, $4, 'user', $5, $6, 'allowed', $7::jsonb,
|
||||
$8, $9, $10)
|
||||
ON CONFLICT (event_id) DO NOTHING RETURNING event_id AS "eventId"`,
|
||||
[
|
||||
command.auditEventId,
|
||||
command.requestId,
|
||||
operationId,
|
||||
command.projectId,
|
||||
command.principal.subject.id,
|
||||
command.principal.authenticationId,
|
||||
JSON.stringify(['role_grant', 'strong_authentication']),
|
||||
command.policyFence.projectVersion,
|
||||
command.policyFence.bindingVersion,
|
||||
observedAtMs,
|
||||
],
|
||||
);
|
||||
if (inserted.rows.length === 1) return;
|
||||
const replay = await client.query<Row>(
|
||||
`SELECT request_id AS "requestId", operation_id AS "operationId",
|
||||
project_id AS "projectId", subject_type AS "subjectType",
|
||||
subject_id AS "subjectId", authentication_id AS "authenticationId",
|
||||
outcome, reasons, project_version AS "projectVersion",
|
||||
binding_version AS "bindingVersion"
|
||||
FROM "ql3"."security_audit_events" WHERE event_id = $1`,
|
||||
[command.auditEventId],
|
||||
);
|
||||
const row = replay.rows[0];
|
||||
if (
|
||||
replay.rows.length !== 1 ||
|
||||
!row ||
|
||||
row.requestId !== command.requestId ||
|
||||
row.operationId !== operationId ||
|
||||
row.projectId !== command.projectId ||
|
||||
row.subjectType !== 'user' ||
|
||||
row.subjectId !== command.principal.subject.id ||
|
||||
row.authenticationId !== command.principal.authenticationId ||
|
||||
row.outcome !== 'allowed' ||
|
||||
!Array.isArray(row.reasons) ||
|
||||
row.reasons.join('\0') !== 'role_grant\0strong_authentication' ||
|
||||
integer(row, 'projectVersion') !== command.policyFence.projectVersion ||
|
||||
integer(row, 'bindingVersion') !== command.policyFence.bindingVersion
|
||||
) {
|
||||
throw new RunCancellationDispatchManagementConflictError(
|
||||
'mutation_conflict',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function runStatus(row: Row): RunStatus {
|
||||
const value = text(row, 'runStatus') as RunStatus;
|
||||
if (!RUN_STATUSES.includes(value)) {
|
||||
throw new TypeError('PostgreSQL cancellation management Run status is invalid');
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function dispatchProjection(row: Row): NonNullable<RunCancellationDispatchDiagnostic['dispatch']> {
|
||||
const status = text(row, 'dispatchStatus') as CancellationDispatchStatus;
|
||||
const lastResult = optionalText(row, 'lastResult') as
|
||||
| CancellationDispatchResult
|
||||
| undefined;
|
||||
if (
|
||||
!CANCELLATION_DISPATCH_STATUSES.includes(status) ||
|
||||
(lastResult !== undefined &&
|
||||
!CANCELLATION_DISPATCH_RESULTS.includes(lastResult))
|
||||
) {
|
||||
throw new TypeError('PostgreSQL cancellation management dispatch is invalid');
|
||||
}
|
||||
const nextAttemptAtMs = optionalInteger(row, 'nextAttemptAtMs');
|
||||
const leaseExpiresAtMs = optionalInteger(row, 'leaseExpiresAtMs');
|
||||
const lastDispatchedAtMs = optionalInteger(row, 'lastDispatchedAtMs');
|
||||
return Object.freeze({
|
||||
attemptId: text(row, 'attemptId'),
|
||||
status,
|
||||
version: integer(row, 'dispatchVersion'),
|
||||
dispatchCount: integer(row, 'dispatchCount'),
|
||||
...(nextAttemptAtMs === undefined ? {} : { nextAttemptAtMs }),
|
||||
...(leaseExpiresAtMs === undefined ? {} : { leaseExpiresAtMs }),
|
||||
...(lastResult === undefined ? {} : { lastResult }),
|
||||
...(lastDispatchedAtMs === undefined ? {} : { lastDispatchedAtMs }),
|
||||
createdAtMs: integer(row, 'dispatchCreatedAtMs'),
|
||||
updatedAtMs: integer(row, 'dispatchUpdatedAtMs'),
|
||||
});
|
||||
}
|
||||
|
||||
function diagnostic(
|
||||
command: Readonly<ManagementAuthority>,
|
||||
run: Row,
|
||||
dispatchRow?: Row,
|
||||
): Readonly<RunCancellationDispatchDiagnostic> {
|
||||
const cancelRequestedAtMs = optionalInteger(run, 'cancelRequestedAtMs');
|
||||
const cancelReason = optionalText(run, 'cancelReason');
|
||||
if (
|
||||
(cancelRequestedAtMs === undefined) !== (cancelReason === undefined) ||
|
||||
(cancelReason !== undefined && !CANCEL_REASONS.has(cancelReason))
|
||||
) {
|
||||
throw new TypeError('PostgreSQL cancellation management intent is invalid');
|
||||
}
|
||||
const dispatch = dispatchRow ? dispatchProjection(dispatchRow) : null;
|
||||
return Object.freeze({
|
||||
projectId: command.projectId,
|
||||
runId: command.runId,
|
||||
runStatus: runStatus(run),
|
||||
runVersion: integer(run, 'runVersion'),
|
||||
eventSequence: integer(run, 'eventSequence'),
|
||||
...(cancelRequestedAtMs === undefined
|
||||
? {}
|
||||
: {
|
||||
cancelRequestedAtMs,
|
||||
cancelReason: cancelReason as NonNullable<
|
||||
RunCancellationDispatchDiagnostic['cancelReason']
|
||||
>,
|
||||
}),
|
||||
operatorAction:
|
||||
dispatch?.status === 'blocked'
|
||||
? 'rearm'
|
||||
: dispatch && dispatch.status !== 'dispatched'
|
||||
? 'wait'
|
||||
: cancelRequestedAtMs !== undefined && !dispatch
|
||||
? 'wait'
|
||||
: 'none',
|
||||
dispatch,
|
||||
});
|
||||
}
|
||||
|
||||
function rearmReceiptFromEvent(
|
||||
command: Readonly<PostgresRunCancellationDispatchRearmCommand>,
|
||||
event: Row,
|
||||
): Readonly<RunCancellationDispatchRearmReceipt> {
|
||||
const payload = exact(event.payload, [
|
||||
'schema',
|
||||
'mutation_id',
|
||||
'previous_dispatch_version',
|
||||
'dispatch_version',
|
||||
'previous_result',
|
||||
'retry_delay_ms',
|
||||
'next_attempt_at_ms',
|
||||
'run_version',
|
||||
]);
|
||||
if (
|
||||
text(event, 'eventId') !== command.eventId ||
|
||||
text(event, 'eventType') !== 'run.cancel_dispatch_rearmed' ||
|
||||
text(event, 'actorType') !== 'user' ||
|
||||
text(event, 'actorId') !== command.principal.subject.id ||
|
||||
payload.schema !== REARM_SCHEMA ||
|
||||
payload.mutation_id !== command.mutationId ||
|
||||
payload.previous_dispatch_version !== command.expectedDispatchVersion ||
|
||||
payload.previous_result !== command.expectedLastResult ||
|
||||
payload.retry_delay_ms !== command.retryDelayMs
|
||||
) {
|
||||
throw new RunCancellationDispatchManagementConflictError(
|
||||
'mutation_conflict',
|
||||
);
|
||||
}
|
||||
return Object.freeze({
|
||||
status: 'rearmed',
|
||||
projectId: command.projectId,
|
||||
runId: command.runId,
|
||||
attemptId: text(event, 'attemptId'),
|
||||
previousDispatchVersion: storedInteger(
|
||||
payload.previous_dispatch_version,
|
||||
'previousDispatchVersion',
|
||||
1,
|
||||
),
|
||||
dispatchVersion: storedInteger(
|
||||
payload.dispatch_version,
|
||||
'dispatchVersion',
|
||||
2,
|
||||
),
|
||||
previousResult:
|
||||
payload.previous_result as BlockingCancellationDispatchResult,
|
||||
retryDelayMs: storedInteger(
|
||||
payload.retry_delay_ms,
|
||||
'retryDelayMs',
|
||||
MIN_MANUAL_RETRY_DELAY_MS,
|
||||
MAX_CANCELLATION_DISPATCH_RETRY_DELAY_MS,
|
||||
),
|
||||
nextAttemptAtMs: storedInteger(
|
||||
payload.next_attempt_at_ms,
|
||||
'nextAttemptAtMs',
|
||||
),
|
||||
runVersion: storedInteger(payload.run_version, 'runVersion', 1),
|
||||
eventSequence: integer(event, 'eventSequence'),
|
||||
});
|
||||
}
|
||||
|
||||
export class PostgresRunCancellationDispatchManagementRepository {
|
||||
constructor(private readonly pool: PostgresPool) {
|
||||
if (!pool || typeof pool.connect !== 'function') {
|
||||
throw new InvalidRunCancellationDispatchManagementError();
|
||||
}
|
||||
}
|
||||
|
||||
inspect(
|
||||
value: Readonly<PostgresRunCancellationDispatchInspectCommand>,
|
||||
): Promise<Readonly<RunCancellationDispatchDiagnostic>> {
|
||||
const command = normalizeInspectCommand(value);
|
||||
return this.transaction(async (client) => {
|
||||
const observedAtMs = await databaseNow(client);
|
||||
const authorized = Object.freeze({
|
||||
...command,
|
||||
principal: strongPrincipal(command.principal, observedAtMs),
|
||||
});
|
||||
await confirmAuthorization(client, authorized);
|
||||
const run = await client.query<Row>(
|
||||
`SELECT project_id AS "projectId", status AS "runStatus",
|
||||
version AS "runVersion", event_sequence AS "eventSequence",
|
||||
cancel_requested_at_ms AS "cancelRequestedAtMs",
|
||||
cancel_reason AS "cancelReason"
|
||||
FROM "ql3"."runs" WHERE id = $1`,
|
||||
[command.runId],
|
||||
);
|
||||
if (
|
||||
run.rows.length !== 1 ||
|
||||
run.rows[0]?.projectId !== command.projectId
|
||||
) {
|
||||
throw new RunCancellationDispatchManagementNotFoundError();
|
||||
}
|
||||
const dispatch = await client.query<Row>(
|
||||
`SELECT attempt_id AS "attemptId", status AS "dispatchStatus",
|
||||
version AS "dispatchVersion", dispatch_count AS "dispatchCount",
|
||||
next_attempt_at_ms AS "nextAttemptAtMs",
|
||||
lease_expires_at_ms AS "leaseExpiresAtMs",
|
||||
last_result AS "lastResult",
|
||||
last_dispatched_at_ms AS "lastDispatchedAtMs",
|
||||
created_at_ms AS "dispatchCreatedAtMs",
|
||||
updated_at_ms AS "dispatchUpdatedAtMs"
|
||||
FROM "ql3"."run_cancellation_dispatches" WHERE run_id = $1`,
|
||||
[command.runId],
|
||||
);
|
||||
if (dispatch.rows.length > 1) {
|
||||
throw new TypeError('PostgreSQL cancellation management dispatch duplicated');
|
||||
}
|
||||
await recordAllowedAudit(
|
||||
client,
|
||||
authorized,
|
||||
'run.cancellation.inspect',
|
||||
observedAtMs,
|
||||
);
|
||||
return diagnostic(authorized, run.rows[0]!, dispatch.rows[0]);
|
||||
});
|
||||
}
|
||||
|
||||
rearm(
|
||||
value: Readonly<PostgresRunCancellationDispatchRearmCommand>,
|
||||
): Promise<Readonly<RunCancellationDispatchRearmReceipt>> {
|
||||
const command = normalizeRearmCommand(value);
|
||||
return this.transaction(async (client) => {
|
||||
const observedAtMs = await databaseNow(client);
|
||||
const authorized = Object.freeze({
|
||||
...command,
|
||||
principal: strongPrincipal(command.principal, observedAtMs),
|
||||
});
|
||||
await confirmAuthorization(client, authorized);
|
||||
const run = await client.query<Row>(
|
||||
`SELECT project_id AS "projectId", status AS "runStatus",
|
||||
version AS "runVersion", event_sequence AS "eventSequence",
|
||||
cancel_requested_at_ms AS "cancelRequestedAtMs"
|
||||
FROM "ql3"."runs" WHERE id = $1 FOR UPDATE`,
|
||||
[command.runId],
|
||||
);
|
||||
if (
|
||||
run.rows.length !== 1 ||
|
||||
run.rows[0]?.projectId !== command.projectId
|
||||
) {
|
||||
throw new RunCancellationDispatchManagementNotFoundError();
|
||||
}
|
||||
const dedupeKey = `cancel-dispatch-rearm:${command.mutationId}`;
|
||||
const replay = await client.query<Row>(
|
||||
`SELECT id AS "eventId", sequence AS "eventSequence",
|
||||
type AS "eventType", actor_type AS "actorType",
|
||||
actor_id AS "actorId", attempt_id AS "attemptId", payload
|
||||
FROM "ql3"."run_events"
|
||||
WHERE run_id = $1 AND dedupe_key = $2`,
|
||||
[command.runId, dedupeKey],
|
||||
);
|
||||
if (replay.rows.length === 1) {
|
||||
const receipt = rearmReceiptFromEvent(authorized, replay.rows[0]!);
|
||||
await recordAllowedAudit(
|
||||
client,
|
||||
authorized,
|
||||
'run.cancellation.rearm',
|
||||
observedAtMs,
|
||||
);
|
||||
return receipt;
|
||||
}
|
||||
if (replay.rows.length !== 0) {
|
||||
throw new RunCancellationDispatchManagementConflictError(
|
||||
'mutation_conflict',
|
||||
);
|
||||
}
|
||||
if (!ACTIVE_RUN_STATUSES.has(runStatus(run.rows[0]!))) {
|
||||
throw new RunCancellationDispatchManagementConflictError('run_terminal');
|
||||
}
|
||||
if (optionalInteger(run.rows[0]!, 'cancelRequestedAtMs') === undefined) {
|
||||
throw new RunCancellationDispatchManagementConflictError(
|
||||
'cancellation_missing',
|
||||
);
|
||||
}
|
||||
const candidate = await client.query<Row>(
|
||||
`SELECT attempt_id AS "attemptId"
|
||||
FROM "ql3"."run_cancellation_dispatches" WHERE run_id = $1`,
|
||||
[command.runId],
|
||||
);
|
||||
if (candidate.rows.length !== 1) {
|
||||
throw new RunCancellationDispatchManagementConflictError(
|
||||
'dispatch_missing',
|
||||
);
|
||||
}
|
||||
const attemptId = text(candidate.rows[0]!, 'attemptId');
|
||||
const attempt = await client.query<Row>(
|
||||
`SELECT status AS "attemptStatus" FROM "ql3"."run_attempts"
|
||||
WHERE run_id = $1 AND id = $2`,
|
||||
[command.runId, attemptId],
|
||||
);
|
||||
if (
|
||||
attempt.rows.length !== 1 ||
|
||||
!ACTIVE_ATTEMPT_STATUSES.has(text(attempt.rows[0]!, 'attemptStatus'))
|
||||
) {
|
||||
throw new RunCancellationDispatchManagementConflictError(
|
||||
'attempt_not_active',
|
||||
);
|
||||
}
|
||||
const dispatch = await client.query<Row>(
|
||||
`SELECT attempt_id AS "attemptId", status AS "dispatchStatus",
|
||||
version AS "dispatchVersion", last_result AS "lastResult"
|
||||
FROM "ql3"."run_cancellation_dispatches"
|
||||
WHERE run_id = $1 FOR UPDATE`,
|
||||
[command.runId],
|
||||
);
|
||||
if (dispatch.rows.length !== 1) {
|
||||
throw new RunCancellationDispatchManagementConflictError(
|
||||
'dispatch_missing',
|
||||
);
|
||||
}
|
||||
const current = dispatch.rows[0]!;
|
||||
if (
|
||||
text(current, 'attemptId') !== attemptId ||
|
||||
text(current, 'dispatchStatus') !== 'blocked'
|
||||
) {
|
||||
throw new RunCancellationDispatchManagementConflictError(
|
||||
'dispatch_not_blocked',
|
||||
);
|
||||
}
|
||||
if (integer(current, 'dispatchVersion') !== command.expectedDispatchVersion) {
|
||||
throw new RunCancellationDispatchManagementConflictError(
|
||||
'dispatch_version_changed',
|
||||
);
|
||||
}
|
||||
if (text(current, 'lastResult') !== command.expectedLastResult) {
|
||||
throw new RunCancellationDispatchManagementConflictError(
|
||||
'dispatch_result_changed',
|
||||
);
|
||||
}
|
||||
const nextAttemptAtMs = observedAtMs + command.retryDelayMs;
|
||||
const runVersion = integer(run.rows[0]!, 'runVersion');
|
||||
const eventSequence = integer(run.rows[0]!, 'eventSequence');
|
||||
if (
|
||||
!Number.isSafeInteger(nextAttemptAtMs) ||
|
||||
runVersion >= 2_147_483_647 ||
|
||||
eventSequence >= 2_147_483_647
|
||||
) {
|
||||
throw new TypeError('PostgreSQL cancellation management counter overflowed');
|
||||
}
|
||||
const dispatchVersion = command.expectedDispatchVersion + 1;
|
||||
const nextRunVersion = runVersion + 1;
|
||||
const nextEventSequence = eventSequence + 1;
|
||||
const runUpdated = await client.query(
|
||||
`UPDATE "ql3"."runs"
|
||||
SET version = $2, event_sequence = $3
|
||||
WHERE id = $1 AND version = $4`,
|
||||
[command.runId, nextRunVersion, nextEventSequence, runVersion],
|
||||
);
|
||||
if (runUpdated.rowCount !== 1) {
|
||||
throw new RunCancellationDispatchManagementConflictError(
|
||||
'dispatch_version_changed',
|
||||
);
|
||||
}
|
||||
const dispatchUpdated = await client.query(
|
||||
`UPDATE "ql3"."run_cancellation_dispatches"
|
||||
SET status = 'retry_wait', version = $2,
|
||||
next_attempt_at_ms = $3, updated_at_ms = $4
|
||||
WHERE run_id = $1 AND attempt_id = $5 AND status = 'blocked'
|
||||
AND version = $6 AND last_result = $7`,
|
||||
[
|
||||
command.runId,
|
||||
dispatchVersion,
|
||||
nextAttemptAtMs,
|
||||
observedAtMs,
|
||||
attemptId,
|
||||
command.expectedDispatchVersion,
|
||||
command.expectedLastResult,
|
||||
],
|
||||
);
|
||||
if (dispatchUpdated.rowCount !== 1) {
|
||||
throw new RunCancellationDispatchManagementConflictError(
|
||||
'dispatch_version_changed',
|
||||
);
|
||||
}
|
||||
const payload = Object.freeze({
|
||||
schema: REARM_SCHEMA,
|
||||
mutation_id: command.mutationId,
|
||||
previous_dispatch_version: command.expectedDispatchVersion,
|
||||
dispatch_version: dispatchVersion,
|
||||
previous_result: command.expectedLastResult,
|
||||
retry_delay_ms: command.retryDelayMs,
|
||||
next_attempt_at_ms: nextAttemptAtMs,
|
||||
run_version: nextRunVersion,
|
||||
});
|
||||
await client.query(
|
||||
`INSERT INTO "ql3"."run_events" (
|
||||
id, run_id, sequence, type, dedupe_key, actor_type, actor_id,
|
||||
attempt_id, step_run_id, payload, created_at_ms
|
||||
) VALUES ($1, $2, $3, 'run.cancel_dispatch_rearmed', $4,
|
||||
'user', $5, $6, NULL, $7::jsonb, $8)`,
|
||||
[
|
||||
command.eventId,
|
||||
command.runId,
|
||||
nextEventSequence,
|
||||
dedupeKey,
|
||||
authorized.principal.subject.id,
|
||||
attemptId,
|
||||
JSON.stringify(payload),
|
||||
observedAtMs,
|
||||
],
|
||||
);
|
||||
await recordAllowedAudit(
|
||||
client,
|
||||
authorized,
|
||||
'run.cancellation.rearm',
|
||||
observedAtMs,
|
||||
);
|
||||
return Object.freeze({
|
||||
status: 'rearmed',
|
||||
projectId: command.projectId,
|
||||
runId: command.runId,
|
||||
attemptId,
|
||||
previousDispatchVersion: command.expectedDispatchVersion,
|
||||
dispatchVersion,
|
||||
previousResult: command.expectedLastResult,
|
||||
retryDelayMs: command.retryDelayMs,
|
||||
nextAttemptAtMs,
|
||||
runVersion: nextRunVersion,
|
||||
eventSequence: nextEventSequence,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private async transaction<T>(
|
||||
operation: (client: PostgresClient) => Promise<T>,
|
||||
): Promise<T> {
|
||||
let client: PostgresClient | undefined;
|
||||
try {
|
||||
client = await this.pool.connect();
|
||||
await begin(client);
|
||||
const result = await operation(client);
|
||||
await client.query('COMMIT');
|
||||
return result;
|
||||
} catch (error) {
|
||||
if (client) await rollback(client);
|
||||
if (
|
||||
error instanceof InvalidRunCancellationDispatchManagementError ||
|
||||
error instanceof RunCancellationDispatchManagementNotFoundError ||
|
||||
error instanceof RunCancellationDispatchManagementConflictError
|
||||
) {
|
||||
throw error;
|
||||
}
|
||||
throw new RunCancellationDispatchManagementUnavailableError({
|
||||
cause: error,
|
||||
});
|
||||
} finally {
|
||||
client?.release();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,13 +21,14 @@ export interface PostgresSchemaContractTrigger {
|
||||
export interface PostgresSchemaContract {
|
||||
readonly schema: 'ql3';
|
||||
readonly contractName: 'control-core';
|
||||
readonly contractVersion: 65;
|
||||
readonly migrationId: 'pg-0066-cancellation-dispatch';
|
||||
readonly contractVersion: 66;
|
||||
readonly migrationId: 'pg-0067-cancellation-dispatch-management';
|
||||
readonly minimumServerMajor: 16;
|
||||
readonly maximumServerMajor: 18;
|
||||
readonly capabilities: Readonly<{
|
||||
run_core: 1;
|
||||
run_cancellation_dispatch: 1;
|
||||
run_cancellation_dispatch_management: 1;
|
||||
run_attempt_log_retention: 1;
|
||||
run_management_boundary: 1;
|
||||
run_management_stop: 1;
|
||||
@@ -119,8 +120,8 @@ export const postgresqlControlSchemaContract: PostgresSchemaContract =
|
||||
Object.freeze({
|
||||
schema: 'ql3',
|
||||
contractName: 'control-core',
|
||||
contractVersion: 65,
|
||||
migrationId: 'pg-0066-cancellation-dispatch',
|
||||
contractVersion: 66,
|
||||
migrationId: 'pg-0067-cancellation-dispatch-management',
|
||||
minimumServerMajor: 16,
|
||||
maximumServerMajor: 18,
|
||||
capabilities: Object.freeze({
|
||||
@@ -169,6 +170,7 @@ export const postgresqlControlSchemaContract: PostgresSchemaContract =
|
||||
project_tool_definition_snapshot: 1,
|
||||
run_core: 1,
|
||||
run_cancellation_dispatch: 1,
|
||||
run_cancellation_dispatch_management: 1,
|
||||
run_attempt_log_retention: 1,
|
||||
run_management_boundary: 1,
|
||||
run_management_stop: 1,
|
||||
|
||||
@@ -1485,6 +1485,8 @@ const REQUIRED_RUN_MANAGER_PRIVILEGES: RequiredPrivileges = Object.freeze(
|
||||
name === 'run_events' ||
|
||||
name === 'security_audit_events'
|
||||
? { ...NO_TABLE_PRIVILEGES, select: true, insert: true }
|
||||
: name === 'run_cancellation_dispatches'
|
||||
? { ...NO_TABLE_PRIVILEGES, select: true }
|
||||
: name === 'plugin_package_identity_keyset_ledger'
|
||||
? {
|
||||
...NO_TABLE_PRIVILEGES,
|
||||
@@ -2237,6 +2239,14 @@ async function assertRunManagerColumnPrivileges(
|
||||
'missing-runs-contract',
|
||||
]);
|
||||
}
|
||||
const dispatch = contract.tables.find(
|
||||
({ name }) => name === 'run_cancellation_dispatches',
|
||||
);
|
||||
if (!dispatch) {
|
||||
throw new PostgresSchemaReadinessError('run_manager_role_invalid', [
|
||||
'missing-run-cancellation-dispatch-contract',
|
||||
]);
|
||||
}
|
||||
const result = await queryable.query<ColumnPrivilegeRow>(
|
||||
`
|
||||
SELECT
|
||||
@@ -2269,6 +2279,41 @@ ORDER BY requested.column_name
|
||||
if (actual.size !== run.columns.length) {
|
||||
findings.push('column-privilege-row-count:runs');
|
||||
}
|
||||
const dispatchResult = await queryable.query<ColumnPrivilegeRow>(
|
||||
`
|
||||
SELECT
|
||||
requested.column_name AS "columnName",
|
||||
has_column_privilege(
|
||||
current_user,
|
||||
format('%I.%I', $1::text, 'run_cancellation_dispatches'),
|
||||
requested.column_name,
|
||||
'UPDATE'
|
||||
) AS "updateAllowed"
|
||||
FROM unnest($2::text[]) AS requested(column_name)
|
||||
ORDER BY requested.column_name
|
||||
`.trim(),
|
||||
[contract.schema, dispatch.columns],
|
||||
);
|
||||
const allowedDispatch = new Set([
|
||||
'status',
|
||||
'version',
|
||||
'next_attempt_at_ms',
|
||||
'updated_at_ms',
|
||||
]);
|
||||
const actualDispatch = new Map(
|
||||
dispatchResult.rows.map((row) => [row.columnName, row]),
|
||||
);
|
||||
for (const columnName of dispatch.columns) {
|
||||
const row = actualDispatch.get(columnName);
|
||||
if (!row || row.updateAllowed !== allowedDispatch.has(columnName)) {
|
||||
findings.push(
|
||||
`column-update-privilege:run_cancellation_dispatches.${columnName}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
if (actualDispatch.size !== dispatch.columns.length) {
|
||||
findings.push('column-privilege-row-count:run_cancellation_dispatches');
|
||||
}
|
||||
if (findings.length > 0) {
|
||||
throw new PostgresSchemaReadinessError(
|
||||
'run_manager_role_invalid',
|
||||
|
||||
@@ -117,6 +117,7 @@ test('defines the immutable PostgreSQL capability and Run core stream', async ()
|
||||
'pg-0064-plugin-package-secret-binding-transition-approval-plans',
|
||||
'pg-0065-approved-action-manual-recovery',
|
||||
'pg-0066-cancellation-dispatch',
|
||||
'pg-0067-cancellation-dispatch-management',
|
||||
],
|
||||
);
|
||||
for (const migration of postgresqlMainMigrationStream.migrations) {
|
||||
@@ -585,6 +586,11 @@ test('freezes every published PostgreSQL migration checksum', () => {
|
||||
checksum:
|
||||
'b6d7ac81b5f75530df05f8ef05878fa30aa0f4418363973ded89d14ffce151b2',
|
||||
},
|
||||
{
|
||||
id: 'pg-0067-cancellation-dispatch-management',
|
||||
checksum:
|
||||
'e78e24a06dc4c4dbdd859685f28b4bc837a8cfb279eb3512e0a57dc6d27eaaaa',
|
||||
},
|
||||
];
|
||||
assert.deepEqual(
|
||||
postgresqlMainMigrationStream.migrations.map(({ id, checksum }) => ({
|
||||
@@ -2329,3 +2335,41 @@ test('advances capability v65 with database-timed fenced cancellation dispatch',
|
||||
/migration_id = 'pg-0065-approved-action-manual-recovery'/,
|
||||
);
|
||||
});
|
||||
|
||||
test('advances capability v66 with least-privilege cancellation diagnostics and rearm', async () => {
|
||||
const migration = migrationById(
|
||||
'pg-0067-cancellation-dispatch-management',
|
||||
);
|
||||
const statements = [];
|
||||
await migration.up({
|
||||
async query(statement) {
|
||||
statements.push(statement);
|
||||
return { rows: [] };
|
||||
},
|
||||
});
|
||||
const sql = statements.join('\n');
|
||||
assert.match(
|
||||
sql,
|
||||
/DROP CONSTRAINT ql3_run_cancellation_dispatch_result_state_check/,
|
||||
);
|
||||
assert.match(
|
||||
sql,
|
||||
/status IN \('leased', 'retry_wait'\)[\s\S]+identity_mismatch[\s\S]+dispatch_error/,
|
||||
);
|
||||
assert.match(
|
||||
sql,
|
||||
/GRANT SELECT ON "ql3"\."run_cancellation_dispatches" TO ql3_run_manager/,
|
||||
);
|
||||
assert.match(
|
||||
sql,
|
||||
/GRANT UPDATE \(status, version, next_attempt_at_ms, updated_at_ms\) ON "ql3"\."run_cancellation_dispatches" TO ql3_run_manager/,
|
||||
);
|
||||
assert.doesNotMatch(
|
||||
sql,
|
||||
/GRANT (?:INSERT|DELETE|TRUNCATE)[^;]+run_cancellation_dispatches[^;]+ql3_run_manager/,
|
||||
);
|
||||
assert.match(sql, /contract_version = 66/);
|
||||
assert.match(sql, /"run_cancellation_dispatch_management":1/);
|
||||
assert.match(sql, /contract_version = 65/);
|
||||
assert.match(sql, /migration_id = 'pg-0066-cancellation-dispatch'/);
|
||||
});
|
||||
|
||||
@@ -527,6 +527,7 @@ function runManagerPrivileges() {
|
||||
'runs',
|
||||
'run_attempts',
|
||||
'run_events',
|
||||
'run_cancellation_dispatches',
|
||||
'security_audit_events',
|
||||
'plugin_package_identity_keyset_ledger',
|
||||
]);
|
||||
@@ -781,19 +782,36 @@ function queryable(overrides = {}) {
|
||||
};
|
||||
}
|
||||
if (text.includes('has_column_privilege')) {
|
||||
assert.match(text, /format\('%I\.%I', \$1::text, 'runs'\)/);
|
||||
const dispatchManagement = text.includes(
|
||||
"format('%I.%I', $1::text, 'run_cancellation_dispatches')",
|
||||
);
|
||||
const tableName = dispatchManagement
|
||||
? 'run_cancellation_dispatches'
|
||||
: 'runs';
|
||||
assert.match(
|
||||
text,
|
||||
new RegExp(
|
||||
`format\\('%I\\.%I', \\$1::text, '${tableName}'\\)`,
|
||||
),
|
||||
);
|
||||
const columns = contract.tables.find(
|
||||
({ name }) => name === 'runs',
|
||||
({ name }) => name === tableName,
|
||||
).columns;
|
||||
const allowed = new Set([
|
||||
'cancel_requested_at_ms',
|
||||
'cancel_reason',
|
||||
'version',
|
||||
'event_sequence',
|
||||
]);
|
||||
const allowed = new Set(
|
||||
dispatchManagement
|
||||
? ['status', 'version', 'next_attempt_at_ms', 'updated_at_ms']
|
||||
: [
|
||||
'cancel_requested_at_ms',
|
||||
'cancel_reason',
|
||||
'version',
|
||||
'event_sequence',
|
||||
],
|
||||
);
|
||||
return {
|
||||
rows:
|
||||
overrides.runManagerColumnPrivileges ??
|
||||
(dispatchManagement
|
||||
? overrides.runManagerDispatchColumnPrivileges
|
||||
: overrides.runManagerColumnPrivileges) ??
|
||||
columns.map((columnName) => ({
|
||||
columnName,
|
||||
updateAllowed: allowed.has(columnName),
|
||||
@@ -817,7 +835,7 @@ test('accepts the exact PostgreSQL control schema and least-privilege runtime ro
|
||||
serverMajor: 16,
|
||||
currentUser: 'ql3_runtime',
|
||||
contractName: 'control-core',
|
||||
contractVersion: 65,
|
||||
contractVersion: 66,
|
||||
migrationIds: [
|
||||
'pg-0001-schema-capability',
|
||||
'pg-0002-run-core',
|
||||
@@ -885,6 +903,7 @@ test('accepts the exact PostgreSQL control schema and least-privilege runtime ro
|
||||
'pg-0064-plugin-package-secret-binding-transition-approval-plans',
|
||||
'pg-0065-approved-action-manual-recovery',
|
||||
'pg-0066-cancellation-dispatch',
|
||||
'pg-0067-cancellation-dispatch-management',
|
||||
],
|
||||
});
|
||||
});
|
||||
@@ -915,10 +934,10 @@ test('accepts the exact schema and isolated least-privilege admin role', async (
|
||||
}),
|
||||
);
|
||||
assert.equal(report.currentUser, 'ql3_admin');
|
||||
assert.equal(report.contractVersion, 65);
|
||||
assert.equal(report.contractVersion, 66);
|
||||
assert.equal(
|
||||
report.migrationIds.at(-1),
|
||||
'pg-0066-cancellation-dispatch',
|
||||
'pg-0067-cancellation-dispatch-management',
|
||||
);
|
||||
});
|
||||
|
||||
@@ -931,10 +950,10 @@ test('accepts the isolated least-privilege automation manager role', async () =>
|
||||
}),
|
||||
);
|
||||
assert.equal(report.currentUser, 'ql3_automation_manager');
|
||||
assert.equal(report.contractVersion, 65);
|
||||
assert.equal(report.contractVersion, 66);
|
||||
assert.equal(
|
||||
report.migrationIds.at(-1),
|
||||
'pg-0066-cancellation-dispatch',
|
||||
'pg-0067-cancellation-dispatch-management',
|
||||
);
|
||||
|
||||
const widened = automationManagerPrivileges();
|
||||
@@ -963,10 +982,10 @@ test('accepts the isolated least-privilege human Approval manager role', async (
|
||||
}),
|
||||
);
|
||||
assert.equal(report.currentUser, 'ql3_approval_manager');
|
||||
assert.equal(report.contractVersion, 65);
|
||||
assert.equal(report.contractVersion, 66);
|
||||
assert.equal(
|
||||
report.migrationIds.at(-1),
|
||||
'pg-0066-cancellation-dispatch',
|
||||
'pg-0067-cancellation-dispatch-management',
|
||||
);
|
||||
|
||||
const widened = approvalManagerPrivileges();
|
||||
@@ -997,10 +1016,10 @@ test('accepts the isolated least-privilege Run manager role', async () => {
|
||||
}),
|
||||
);
|
||||
assert.equal(report.currentUser, 'ql3_run_manager');
|
||||
assert.equal(report.contractVersion, 65);
|
||||
assert.equal(report.contractVersion, 66);
|
||||
assert.equal(
|
||||
report.migrationIds.at(-1),
|
||||
'pg-0066-cancellation-dispatch',
|
||||
'pg-0067-cancellation-dispatch-management',
|
||||
);
|
||||
|
||||
const widened = runManagerPrivileges();
|
||||
@@ -1045,6 +1064,35 @@ test('accepts the isolated least-privilege Run manager role', async () => {
|
||||
error.code === 'run_manager_role_invalid' &&
|
||||
error.facts.includes('column-update-privilege:runs.status'),
|
||||
);
|
||||
|
||||
const widenedDispatchColumns = postgresqlControlSchemaContract.tables
|
||||
.find(({ name }) => name === 'run_cancellation_dispatches')
|
||||
.columns.map((columnName) => ({
|
||||
columnName,
|
||||
updateAllowed: [
|
||||
'status',
|
||||
'version',
|
||||
'next_attempt_at_ms',
|
||||
'updated_at_ms',
|
||||
'lease_token_digest',
|
||||
].includes(columnName),
|
||||
}));
|
||||
await assert.rejects(
|
||||
assertPostgresRunManagerSchemaReady(
|
||||
queryable({
|
||||
currentUser: 'ql3_run_manager',
|
||||
privileges: runManagerPrivileges(),
|
||||
functionMode: 'run-manager',
|
||||
runManagerDispatchColumnPrivileges: widenedDispatchColumns,
|
||||
}),
|
||||
),
|
||||
(error) =>
|
||||
error instanceof PostgresSchemaReadinessError &&
|
||||
error.code === 'run_manager_role_invalid' &&
|
||||
error.facts.includes(
|
||||
'column-update-privilege:run_cancellation_dispatches.lease_token_digest',
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
test('accepts isolated Package manager and executor roles', async () => {
|
||||
@@ -1132,10 +1180,10 @@ test('accepts the exact schema and isolated Worker ingress role', async () => {
|
||||
}),
|
||||
);
|
||||
assert.equal(report.currentUser, 'ql3_worker_ingress');
|
||||
assert.equal(report.contractVersion, 65);
|
||||
assert.equal(report.contractVersion, 66);
|
||||
assert.equal(
|
||||
report.migrationIds.at(-1),
|
||||
'pg-0066-cancellation-dispatch',
|
||||
'pg-0067-cancellation-dispatch-management',
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
+291
@@ -0,0 +1,291 @@
|
||||
'use strict';
|
||||
|
||||
const assert = require('node:assert/strict');
|
||||
const { test } = require('node:test');
|
||||
|
||||
const {
|
||||
InvalidRunCancellationDispatchManagementError,
|
||||
PostgresRunCancellationDispatchManagementRepository,
|
||||
RunCancellationDispatchManagementConflictError,
|
||||
} = require('@qinglong/cluster-postgres/run-manager');
|
||||
|
||||
const NOW = 1_000_000;
|
||||
|
||||
function command(overrides = {}) {
|
||||
return {
|
||||
projectId: 'project-1',
|
||||
runId: 'run-1',
|
||||
requestId: 'request-1',
|
||||
auditEventId: '019f9600-0000-4000-8000-000000000001',
|
||||
principal: {
|
||||
subject: { type: 'user', id: 'operator-1' },
|
||||
authenticationId: 'oidc:run-management-1',
|
||||
authenticatedAtMs: NOW - 1_000,
|
||||
expiresAtMs: NOW + 60_000,
|
||||
assurance: 'multi_factor',
|
||||
},
|
||||
policyFence: { projectVersion: 2, bindingVersion: 3 },
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function rearmCommand(overrides = {}) {
|
||||
return command({
|
||||
requestId: 'request-rearm-1',
|
||||
auditEventId: '019f9600-0000-4000-8000-000000000011',
|
||||
mutationId: '019f9600-0000-4000-8000-000000000012',
|
||||
eventId: '019f9600-0000-4000-8000-000000000013',
|
||||
expectedDispatchVersion: 3,
|
||||
expectedLastResult: 'identity_mismatch',
|
||||
retryDelayMs: 5_000,
|
||||
...overrides,
|
||||
});
|
||||
}
|
||||
|
||||
function runRow() {
|
||||
return {
|
||||
projectId: 'project-1',
|
||||
runStatus: 'running',
|
||||
runVersion: 6,
|
||||
eventSequence: 8,
|
||||
cancelRequestedAtMs: NOW - 2_000,
|
||||
cancelReason: 'user',
|
||||
};
|
||||
}
|
||||
|
||||
function dispatchRow(overrides = {}) {
|
||||
return {
|
||||
attemptId: 'attempt-1',
|
||||
dispatchStatus: 'blocked',
|
||||
dispatchVersion: 3,
|
||||
dispatchCount: 1,
|
||||
nextAttemptAtMs: null,
|
||||
leaseExpiresAtMs: null,
|
||||
lastResult: 'identity_mismatch',
|
||||
lastDispatchedAtMs: NOW - 1_500,
|
||||
dispatchCreatedAtMs: NOW - 1_900,
|
||||
dispatchUpdatedAtMs: NOW - 1_500,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function fixture(options = {}) {
|
||||
const calls = [];
|
||||
const client = {
|
||||
async query(sql, params = []) {
|
||||
const text = sql.replace(/\s+/g, ' ').trim();
|
||||
calls.push({ sql: text, params });
|
||||
if (
|
||||
text === 'BEGIN ISOLATION LEVEL SERIALIZABLE' ||
|
||||
text === 'COMMIT' ||
|
||||
text === 'ROLLBACK' ||
|
||||
text.startsWith('SELECT set_config')
|
||||
) {
|
||||
return { rows: [], rowCount: 0 };
|
||||
}
|
||||
if (text.includes('transaction_timestamp()')) {
|
||||
return { rows: [{ nowMs: NOW }], rowCount: 1 };
|
||||
}
|
||||
if (text.includes('lock_run_management_policy_fence')) {
|
||||
return {
|
||||
rows: [{ matches: options.authorized !== false }],
|
||||
rowCount: 1,
|
||||
};
|
||||
}
|
||||
if (text.includes('FROM "ql3"."runs" WHERE id = $1 FOR UPDATE')) {
|
||||
return { rows: [runRow()], rowCount: 1 };
|
||||
}
|
||||
if (text.includes('FROM "ql3"."runs" WHERE id = $1')) {
|
||||
return { rows: [runRow()], rowCount: 1 };
|
||||
}
|
||||
if (
|
||||
text.includes('FROM "ql3"."run_events"') &&
|
||||
text.includes('dedupe_key = $2')
|
||||
) {
|
||||
return { rows: options.replay ? [options.replay] : [], rowCount: 0 };
|
||||
}
|
||||
if (
|
||||
text.startsWith('SELECT attempt_id AS "attemptId"') &&
|
||||
!text.includes('dispatchStatus') &&
|
||||
!text.includes('FOR UPDATE')
|
||||
) {
|
||||
return { rows: [{ attemptId: 'attempt-1' }], rowCount: 1 };
|
||||
}
|
||||
if (text.includes('FROM "ql3"."run_attempts"')) {
|
||||
return {
|
||||
rows: [{ attemptStatus: options.attemptStatus ?? 'running' }],
|
||||
rowCount: 1,
|
||||
};
|
||||
}
|
||||
if (
|
||||
text.includes('FROM "ql3"."run_cancellation_dispatches"') &&
|
||||
text.includes('FOR UPDATE')
|
||||
) {
|
||||
return {
|
||||
rows: [
|
||||
dispatchRow({
|
||||
lastResult: options.lastResult ?? 'identity_mismatch',
|
||||
}),
|
||||
],
|
||||
rowCount: 1,
|
||||
};
|
||||
}
|
||||
if (text.includes('FROM "ql3"."run_cancellation_dispatches"')) {
|
||||
return { rows: [dispatchRow()], rowCount: 1 };
|
||||
}
|
||||
if (text.startsWith('UPDATE "ql3"."runs"')) {
|
||||
return { rows: [], rowCount: 1 };
|
||||
}
|
||||
if (text.startsWith('UPDATE "ql3"."run_cancellation_dispatches"')) {
|
||||
return { rows: [], rowCount: 1 };
|
||||
}
|
||||
if (text.startsWith('INSERT INTO "ql3"."run_events"')) {
|
||||
return { rows: [], rowCount: 1 };
|
||||
}
|
||||
if (text.startsWith('INSERT INTO "ql3"."security_audit_events"')) {
|
||||
return { rows: [{ eventId: params[0] }], rowCount: 1 };
|
||||
}
|
||||
throw new Error(`unexpected query: ${text}`);
|
||||
},
|
||||
release() {
|
||||
calls.push({ sql: 'RELEASE', params: [] });
|
||||
},
|
||||
};
|
||||
const pool = { async connect() { return client; } };
|
||||
return {
|
||||
calls,
|
||||
repository: new PostgresRunCancellationDispatchManagementRepository(pool),
|
||||
};
|
||||
}
|
||||
|
||||
test('inspects one low-sensitive blocked dispatch under run.read authority', async () => {
|
||||
const { calls, repository } = fixture();
|
||||
const result = await repository.inspect(command());
|
||||
assert.equal(result.operatorAction, 'rearm');
|
||||
assert.equal(result.dispatch.status, 'blocked');
|
||||
assert.equal(result.dispatch.lastResult, 'identity_mismatch');
|
||||
const dispatchRead = calls.find(({ sql }) =>
|
||||
sql.includes('FROM "ql3"."run_cancellation_dispatches"'),
|
||||
);
|
||||
assert.equal(dispatchRead.sql.includes('lease_owner'), false);
|
||||
assert.equal(dispatchRead.sql.includes('lease_token_digest'), false);
|
||||
assert.equal(
|
||||
calls.some(
|
||||
({ sql }) =>
|
||||
sql.startsWith('INSERT INTO "ql3"."security_audit_events"') &&
|
||||
sql.includes('$3'),
|
||||
),
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
test('rearms an exact blocked dispatch with one event and allowed audit', async () => {
|
||||
const { calls, repository } = fixture();
|
||||
const result = await repository.rearm(rearmCommand());
|
||||
assert.deepEqual(result, {
|
||||
status: 'rearmed',
|
||||
projectId: 'project-1',
|
||||
runId: 'run-1',
|
||||
attemptId: 'attempt-1',
|
||||
previousDispatchVersion: 3,
|
||||
dispatchVersion: 4,
|
||||
previousResult: 'identity_mismatch',
|
||||
retryDelayMs: 5_000,
|
||||
nextAttemptAtMs: NOW + 5_000,
|
||||
runVersion: 7,
|
||||
eventSequence: 9,
|
||||
});
|
||||
const update = calls.find(({ sql }) =>
|
||||
sql.startsWith('UPDATE "ql3"."run_cancellation_dispatches"'),
|
||||
);
|
||||
assert.deepEqual(update.params, [
|
||||
'run-1',
|
||||
4,
|
||||
NOW + 5_000,
|
||||
NOW,
|
||||
'attempt-1',
|
||||
3,
|
||||
'identity_mismatch',
|
||||
]);
|
||||
const attemptRead = calls.find(({ sql }) =>
|
||||
sql.includes('FROM "ql3"."run_attempts"'),
|
||||
);
|
||||
assert.equal(attemptRead.sql.includes('FOR KEY SHARE'), false);
|
||||
const event = calls.find(({ sql }) =>
|
||||
sql.startsWith('INSERT INTO "ql3"."run_events"'),
|
||||
);
|
||||
assert.equal(event.params[0], rearmCommand().eventId);
|
||||
assert.equal(JSON.parse(event.params[6]).previous_result, 'identity_mismatch');
|
||||
assert.ok(
|
||||
calls.findIndex(({ sql }) =>
|
||||
sql.startsWith('UPDATE "ql3"."run_cancellation_dispatches"'),
|
||||
) < calls.findIndex(({ sql }) => sql === 'COMMIT'),
|
||||
);
|
||||
});
|
||||
|
||||
test('exact mutation replay returns the immutable receipt without another update', async () => {
|
||||
const replay = {
|
||||
eventId: rearmCommand().eventId,
|
||||
eventSequence: 9,
|
||||
eventType: 'run.cancel_dispatch_rearmed',
|
||||
actorType: 'user',
|
||||
actorId: 'operator-1',
|
||||
attemptId: 'attempt-1',
|
||||
payload: {
|
||||
schema: 'qinglong/run-cancellation-dispatch-rearm@v1',
|
||||
mutation_id: rearmCommand().mutationId,
|
||||
previous_dispatch_version: 3,
|
||||
dispatch_version: 4,
|
||||
previous_result: 'identity_mismatch',
|
||||
retry_delay_ms: 5_000,
|
||||
next_attempt_at_ms: NOW + 5_000,
|
||||
run_version: 7,
|
||||
},
|
||||
};
|
||||
const { calls, repository } = fixture({ replay });
|
||||
assert.equal((await repository.rearm(rearmCommand())).dispatchVersion, 4);
|
||||
assert.equal(calls.some(({ sql }) => sql.startsWith('UPDATE')), false);
|
||||
});
|
||||
|
||||
test('stale result and authorization changes fail closed before mutation', async () => {
|
||||
const stale = fixture({ lastResult: 'pid_mismatch' });
|
||||
await assert.rejects(
|
||||
stale.repository.rearm(rearmCommand()),
|
||||
(error) =>
|
||||
error instanceof RunCancellationDispatchManagementConflictError &&
|
||||
error.reason === 'dispatch_result_changed',
|
||||
);
|
||||
assert.equal(
|
||||
stale.calls.some(({ sql }) => sql.startsWith('UPDATE')),
|
||||
false,
|
||||
);
|
||||
|
||||
const unauthorized = fixture({ authorized: false });
|
||||
await assert.rejects(
|
||||
unauthorized.repository.inspect(command()),
|
||||
(error) =>
|
||||
error instanceof RunCancellationDispatchManagementConflictError &&
|
||||
error.reason === 'authorization_changed',
|
||||
);
|
||||
assert.equal(
|
||||
unauthorized.calls.some(({ sql }) =>
|
||||
sql.includes('FROM "ql3"."runs"'),
|
||||
),
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
test('rejects malformed management authority before opening PostgreSQL', async () => {
|
||||
let opened = false;
|
||||
const repository = new PostgresRunCancellationDispatchManagementRepository({
|
||||
async connect() {
|
||||
opened = true;
|
||||
throw new Error('must not open');
|
||||
},
|
||||
});
|
||||
assert.throws(
|
||||
() => repository.rearm(rearmCommand({ retryDelayMs: 0 })),
|
||||
InvalidRunCancellationDispatchManagementError,
|
||||
);
|
||||
assert.equal(opened, false);
|
||||
});
|
||||
Reference in New Issue
Block a user