feat(ql3): add strong local run stop

This commit is contained in:
whyour
2026-08-12 10:36:38 +08:00
parent dd370b2842
commit 86eb0f1eb3
16 changed files with 1430 additions and 133 deletions
+6 -1
View File
@@ -65,6 +65,11 @@
"require": "./dist/run-management/runRetryCommand.js",
"default": "./dist/run-management/runRetryCommand.js"
},
"./run-stop-command": {
"types": "./dist/run-management/runStopCommand.d.ts",
"require": "./dist/run-management/runStopCommand.js",
"default": "./dist/run-management/runStopCommand.js"
},
"./plugin-package-workflow-command": {
"types": "./dist/plugin-package/pluginPackageWorkflowCommand.d.ts",
"require": "./dist/plugin-package/pluginPackageWorkflowCommand.js",
@@ -130,7 +135,7 @@
"ql3-ai-feature": "dist/ai-management/aiFeatureCli.js",
"ql3-secret": "dist/security-management/secretCli.js",
"ql3-task": "dist/automation-management/taskDefinitionCli.js",
"ql3-run": "dist/run-management/runRetryCli.js",
"ql3-run": "dist/run-management/runManagementCli.js",
"ql3-workflow": "dist/plugin-package/pluginPackageWorkflowCli.js",
"ql3-trigger": "dist/automation-management/triggerCli.js",
"ql3-policy": "dist/security-management/projectPolicyCli.js",
@@ -87,8 +87,8 @@ export const QINGLONG3_PRODUCT_COMMANDS: readonly QingLong3ProductCommandDefinit
Object.freeze({
name: 'run',
binary: 'ql3-run',
target: 'run-management/runRetryCli.js',
description: 'retry terminal Runs under strong local authentication',
target: 'run-management/runManagementCli.js',
description: 'retry or stop Runs under strong local authentication',
}),
Object.freeze({
name: 'trigger',
@@ -1,25 +1,26 @@
#!/usr/bin/env node
import { runLocalRunRetryCommandFile } from './runRetryCommand';
const USAGE =
'Usage: ql3-run retry --command-file /absolute/private-command.json';
const USAGE = [
'Usage: ql3-run retry --command-file /absolute/private-command.json',
' ql3-run stop --command-file /absolute/private-command.json',
].join('\n');
async function main(argv: readonly string[]): Promise<void> {
if (argv.length === 1 && (argv[0] === '--help' || argv[0] === '-h')) {
process.stdout.write(`${USAGE}\n`);
return;
}
const operation = argv[0];
const commandFilePath = argv[2];
if (
argv.length !== 3 ||
argv[0] !== 'retry' ||
(operation !== 'retry' && operation !== 'stop') ||
argv[1] !== '--command-file' ||
commandFilePath === undefined
) {
process.stderr.write(
`${JSON.stringify({
code: 'LOCAL_RUN_RETRY_CLI_USAGE_INVALID',
code: 'LOCAL_RUN_MANAGEMENT_CLI_USAGE_INVALID',
message: USAGE,
})}\n`,
);
@@ -27,7 +28,16 @@ async function main(argv: readonly string[]): Promise<void> {
return;
}
try {
const result = await runLocalRunRetryCommandFile(commandFilePath);
const result =
operation === 'retry'
? await import('./runRetryCommand.js').then(
({ runLocalRunRetryCommandFile }) =>
runLocalRunRetryCommandFile(commandFilePath),
)
: await import('./runStopCommand.js').then(
({ runLocalRunStopCommandFile }) =>
runLocalRunStopCommandFile(commandFilePath),
);
process.stdout.write(`${JSON.stringify(result)}\n`);
} catch (error) {
const candidate = error as {
@@ -41,12 +51,12 @@ async function main(argv: readonly string[]): Promise<void> {
code:
typeof candidate.code === 'string'
? candidate.code
: 'LOCAL_RUN_RETRY_CLI_FAILED',
: 'LOCAL_RUN_MANAGEMENT_CLI_FAILED',
name: typeof candidate.name === 'string' ? candidate.name : 'Error',
message:
typeof candidate.message === 'string'
? candidate.message
: 'Local Run retry command failed',
: 'Local Run management command failed',
...(Number.isSafeInteger(candidate.retryAfterMs)
? { retryAfterMs: candidate.retryAfterMs }
: {}),
@@ -0,0 +1,482 @@
import { randomUUID } from 'node:crypto';
import path from 'node:path';
import {
PrivateLocalCommandFileError,
readPrivateLocalCommandFile,
} from '@qinglong/local-command-file';
import {
AuthenticatedLocalCommandAuthenticationError,
establishAuthenticatedLocalCommand,
type AuthenticatedLocalCommand,
} from '@qinglong/local-owner-console/authenticated-command';
import {
LocalSqliteAuthenticatedManagementFenceError,
type LocalSqliteAuthenticatedUserCredentialFence,
} from '@qinglong/local-sqlite/authenticated-management';
import {
openLocalSqliteRunManagementDatabase,
type LocalSqliteRunManagementDatabase,
} from '@qinglong/local-sqlite/run-management';
import { ProjectPolicyEngine } from '@qinglong/runtime-core/project-policy';
import {
RUN_CANCELLATION_SCHEMA,
RunCancellationFenceRejectedError,
RunCancellationNotFoundError,
RunCancellationUnavailableError,
parseRunCancellationRequestBody,
type RunCancellationResult,
} from '@qinglong/runtime-core/run-cancellation';
import {
normalizeSecurityAuditRecord,
type SecurityAuditRecord,
} from '@qinglong/runtime-core/security-audit';
const MAX_PATH_BYTES = 4_096;
const UUID_V4_PATTERN =
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
const IDENTIFIER_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/;
export interface LocalRunStopCommandOptions {
readonly deploymentRoot: string;
readonly databasePath: string;
readonly profile: 'edge' | 'standalone';
readonly ownerPepperKeyringDirectory: string;
readonly credentialFilePath: string;
readonly busyTimeoutMs?: number;
}
export interface LocalRunStopCommand {
readonly schemaVersion: 1;
readonly operation: 'run.stop';
readonly options: LocalRunStopCommandOptions;
readonly request: Readonly<{
projectId: string;
runId: string;
mutationId: string;
requestId: string;
auditEventId: string;
failureAuditEventId: string;
occurredAtMs: number;
}>;
}
export interface LocalRunStopCommandResult {
readonly schemaVersion: 1;
readonly operation: 'run.stop';
readonly stop: Readonly<RunCancellationResult>;
}
export interface LocalRunStopCommandRunner {
run(commandFilePath: string): Promise<Readonly<LocalRunStopCommandResult>>;
}
export interface LocalRunStopCommandRunnerDependencies {
readonly openDatabase: typeof openLocalSqliteRunManagementDatabase;
readonly authenticate: typeof establishAuthenticatedLocalCommand;
readonly now: () => number;
readonly randomUuid: () => string;
}
export class LocalRunStopCommandConfigurationError extends TypeError {
readonly code = 'LOCAL_RUN_STOP_COMMAND_CONFIGURATION_INVALID';
constructor(message: string, options?: ErrorOptions) {
super(
`Local Run stop command configuration is invalid: ${message}`,
options,
);
this.name = 'LocalRunStopCommandConfigurationError';
}
}
export class LocalRunStopCommandAuthorizationError extends Error {
readonly code = 'LOCAL_RUN_STOP_COMMAND_AUTHORIZATION_REJECTED';
constructor() {
super('Local Run stop command authorization was rejected');
this.name = 'LocalRunStopCommandAuthorizationError';
}
}
export class LocalRunStopCommandUnavailableError extends Error {
readonly code = 'LOCAL_RUN_STOP_COMMAND_UNAVAILABLE';
constructor(options?: ErrorOptions) {
super('Local Run stop command is unavailable', options);
this.name = 'LocalRunStopCommandUnavailableError';
}
}
function exactRecord(
value: unknown,
required: readonly string[],
optional: readonly string[],
label: string,
): asserts value is Record<string, unknown> {
if (!value || typeof value !== 'object' || Array.isArray(value)) {
throw new LocalRunStopCommandConfigurationError(
`${label} must be an object`,
);
}
const keys = Object.keys(value).sort();
const allowed = new Set([...required, ...optional]);
if (
required.some((key) => !keys.includes(key)) ||
keys.some((key) => !allowed.has(key))
) {
throw new LocalRunStopCommandConfigurationError(
`${label} shape is invalid`,
);
}
}
function boundedPath(value: unknown, label: string): string {
if (
typeof value !== 'string' ||
value.length < 1 ||
Buffer.byteLength(value, 'utf8') > MAX_PATH_BYTES ||
value.includes('\0') ||
!path.isAbsolute(value) ||
path.normalize(value) !== value ||
path.parse(value).root === value
) {
throw new LocalRunStopCommandConfigurationError(
`${label} must be a normalized bounded absolute non-root path`,
);
}
return value;
}
function descendant(root: string, value: string, label: string): void {
const relative = path.relative(root, value);
if (
relative.length === 0 ||
relative === '..' ||
relative.startsWith(`..${path.sep}`) ||
path.isAbsolute(relative)
) {
throw new LocalRunStopCommandConfigurationError(
`${label} must be a descendant of deploymentRoot`,
);
}
}
function identifier(value: unknown, label: string): string {
if (typeof value !== 'string' || !IDENTIFIER_PATTERN.test(value)) {
throw new LocalRunStopCommandConfigurationError(`${label} is invalid`);
}
return value;
}
function uuid(value: unknown, label: string): string {
if (typeof value !== 'string' || !UUID_V4_PATTERN.test(value)) {
throw new LocalRunStopCommandConfigurationError(`${label} is invalid`);
}
return value;
}
function options(value: unknown): Readonly<LocalRunStopCommandOptions> {
exactRecord(
value,
[
'deploymentRoot',
'databasePath',
'profile',
'ownerPepperKeyringDirectory',
'credentialFilePath',
],
['busyTimeoutMs'],
'options',
);
const deploymentRoot = boundedPath(value.deploymentRoot, 'deploymentRoot');
const databasePath = boundedPath(value.databasePath, 'databasePath');
const ownerPepperKeyringDirectory = boundedPath(
value.ownerPepperKeyringDirectory,
'ownerPepperKeyringDirectory',
);
const credentialFilePath = boundedPath(
value.credentialFilePath,
'credentialFilePath',
);
for (const [target, label] of [
[databasePath, 'databasePath'],
[ownerPepperKeyringDirectory, 'ownerPepperKeyringDirectory'],
[credentialFilePath, 'credentialFilePath'],
] as const) {
descendant(deploymentRoot, target, label);
}
if (
new Set([databasePath, ownerPepperKeyringDirectory, credentialFilePath])
.size !== 3 ||
(value.profile !== 'edge' && value.profile !== 'standalone') ||
(value.busyTimeoutMs !== undefined &&
(!Number.isSafeInteger(value.busyTimeoutMs) ||
(value.busyTimeoutMs as number) < 1 ||
(value.busyTimeoutMs as number) > 60_000))
) {
throw new LocalRunStopCommandConfigurationError('options are invalid');
}
return Object.freeze({
deploymentRoot,
databasePath,
profile: value.profile,
ownerPepperKeyringDirectory,
credentialFilePath,
...(value.busyTimeoutMs === undefined
? {}
: { busyTimeoutMs: value.busyTimeoutMs as number }),
});
}
function normalizeCommand(value: unknown): Readonly<LocalRunStopCommand> {
exactRecord(
value,
['schemaVersion', 'operation', 'options', 'request'],
[],
'command',
);
if (value.schemaVersion !== 1 || value.operation !== 'run.stop') {
throw new LocalRunStopCommandConfigurationError(
'schemaVersion or operation is invalid',
);
}
exactRecord(
value.request,
[
'projectId',
'runId',
'mutationId',
'requestId',
'auditEventId',
'failureAuditEventId',
'occurredAtMs',
],
[],
'request',
);
const mutationId = uuid(value.request.mutationId, 'mutationId');
try {
parseRunCancellationRequestBody({
schema: RUN_CANCELLATION_SCHEMA,
mutationId,
});
} catch (error) {
throw new LocalRunStopCommandConfigurationError('stop request is invalid', {
cause: error,
});
}
const auditEventId = uuid(value.request.auditEventId, 'auditEventId');
const failureAuditEventId = uuid(
value.request.failureAuditEventId,
'failureAuditEventId',
);
if (
auditEventId === failureAuditEventId ||
!Number.isSafeInteger(value.request.occurredAtMs) ||
(value.request.occurredAtMs as number) < 0
) {
throw new LocalRunStopCommandConfigurationError('request is invalid');
}
return Object.freeze({
schemaVersion: 1,
operation: 'run.stop',
options: options(value.options),
request: Object.freeze({
projectId: identifier(value.request.projectId, 'projectId'),
runId: identifier(value.request.runId, 'runId'),
mutationId,
requestId: identifier(value.request.requestId, 'requestId'),
auditEventId,
failureAuditEventId,
occurredAtMs: value.request.occurredAtMs as number,
}),
});
}
function readCommandFile(
commandFilePath: string,
): Readonly<LocalRunStopCommand> {
try {
return normalizeCommand(readPrivateLocalCommandFile(commandFilePath));
} catch (error) {
if (error instanceof LocalRunStopCommandConfigurationError) throw error;
if (error instanceof PrivateLocalCommandFileError) {
throw new LocalRunStopCommandConfigurationError(
'private command file cannot be read',
{ cause: error },
);
}
throw new LocalRunStopCommandConfigurationError('command file is invalid', {
cause: error,
});
}
}
function dependencies(
value: LocalRunStopCommandRunnerDependencies,
): LocalRunStopCommandRunnerDependencies {
if (
!value ||
typeof value !== 'object' ||
Array.isArray(value) ||
Object.keys(value).sort().join('\0') !==
['authenticate', 'now', 'openDatabase', 'randomUuid'].sort().join('\0') ||
typeof value.openDatabase !== 'function' ||
typeof value.authenticate !== 'function' ||
typeof value.now !== 'function' ||
typeof value.randomUuid !== 'function'
) {
throw new LocalRunStopCommandConfigurationError(
'runner dependencies are invalid',
);
}
return Object.freeze({ ...value });
}
function failureReason(error: unknown): string {
if (error instanceof RunCancellationNotFoundError) return 'run_not_found';
if (
error instanceof LocalRunStopCommandAuthorizationError ||
error instanceof RunCancellationFenceRejectedError ||
error instanceof LocalSqliteAuthenticatedManagementFenceError
) {
return 'run_stop_fence_rejected';
}
return 'run_stop_unavailable';
}
function failureAudit(
command: Readonly<LocalRunStopCommand>,
authenticated: Readonly<AuthenticatedLocalCommand> | undefined,
error: unknown,
): Readonly<SecurityAuditRecord> {
const unauthenticated = authenticated === undefined;
return normalizeSecurityAuditRecord({
eventId: command.request.failureAuditEventId,
requestId: command.request.requestId,
operationId: 'run.stop',
projectId: command.request.projectId,
subject: authenticated?.principal.subject ?? null,
authenticationId: authenticated?.principal.authenticationId ?? null,
outcome: unauthenticated ? 'authentication_rejected' : 'denied',
reasons: [
unauthenticated ? 'local_console_required' : failureReason(error),
],
fence: null,
occurredAtMs: command.request.occurredAtMs,
});
}
export function createLocalRunStopCommandRunner(
candidateDependencies: LocalRunStopCommandRunnerDependencies = {
openDatabase: openLocalSqliteRunManagementDatabase,
authenticate: establishAuthenticatedLocalCommand,
now: Date.now,
randomUuid: randomUUID,
},
): Readonly<LocalRunStopCommandRunner> {
const adapters = dependencies(candidateDependencies);
return Object.freeze({
async run(commandFilePath: string) {
const command = readCommandFile(commandFilePath);
const nowMs = adapters.now();
if (
!Number.isSafeInteger(nowMs) ||
nowMs < command.request.occurredAtMs ||
nowMs - command.request.occurredAtMs > 5 * 60_000
) {
throw new LocalRunStopCommandConfigurationError(
'request time is outside the accepted window',
);
}
const database: LocalSqliteRunManagementDatabase =
await adapters.openDatabase({
databasePath: command.options.databasePath,
profile: command.options.profile,
...(command.options.busyTimeoutMs === undefined
? {}
: { busyTimeoutMs: command.options.busyTimeoutMs }),
});
let authenticated: Readonly<AuthenticatedLocalCommand> | undefined;
try {
try {
authenticated = await adapters.authenticate(database, {
deploymentRoot: command.options.deploymentRoot,
databasePath: command.options.databasePath,
ownerPepperKeyringDirectory:
command.options.ownerPepperKeyringDirectory,
credentialFilePath: command.options.credentialFilePath,
authenticationNamespace: 'local_run_stop',
});
database.activateUserCredentialFence(
authenticated.databaseFence as Readonly<LocalSqliteAuthenticatedUserCredentialFence>,
);
const decision = await new ProjectPolicyEngine(
database.projectPolicy,
).authorize(
authenticated.principal,
command.request.projectId,
'run.stop',
);
if (
decision.effect !== 'allow' ||
!decision.fence ||
decision.fence.bindingVersion === null
) {
throw new LocalRunStopCommandAuthorizationError();
}
await authenticated.confirm();
const stop =
await database.runCancellation.requestUserCancellationAudited({
projectId: command.request.projectId,
runId: command.request.runId,
mutationId: command.request.mutationId,
eventId: adapters.randomUuid(),
requestId: command.request.requestId,
auditEventId: command.request.auditEventId,
principal: authenticated.principal,
policyFence: decision.fence,
});
return Object.freeze({
schemaVersion: 1 as const,
operation: 'run.stop' as const,
stop,
});
} catch (error) {
try {
await database.securityAudit.record(
failureAudit(command, authenticated, error),
);
} catch (auditError) {
throw new LocalRunStopCommandUnavailableError({
cause: auditError,
});
}
throw error;
}
} finally {
await database.close();
}
},
});
}
export function runLocalRunStopCommandFile(
commandFilePath: string,
): Promise<Readonly<LocalRunStopCommandResult>> {
return createLocalRunStopCommandRunner().run(commandFilePath);
}
export function isLocalRunStopCommandError(error: unknown): boolean {
return (
error instanceof LocalRunStopCommandConfigurationError ||
error instanceof LocalRunStopCommandAuthorizationError ||
error instanceof LocalRunStopCommandUnavailableError ||
error instanceof AuthenticatedLocalCommandAuthenticationError ||
error instanceof LocalSqliteAuthenticatedManagementFenceError ||
error instanceof RunCancellationNotFoundError ||
error instanceof RunCancellationFenceRejectedError ||
error instanceof RunCancellationUnavailableError
);
}
@@ -60,7 +60,10 @@ test('help and version are bounded installation-derived product facts', () => {
const help = qingLong3ProductHelp();
assert.match(help, /^Usage: ql3 <command> \[arguments\]/);
assert.match(help, /\n task\s+manage Task definitions\n/);
assert.match(help, /\n run\s+retry terminal Runs/);
assert.match(
help,
/\n run\s+retry or stop Runs under strong local authentication/,
);
assert.match(help, /Root service mutation remains isolated/);
assert.equal(help.includes('ql3-service-bridge '), false);
assert.equal(loadQingLong3ProductVersion(moduleDirectory), manifest.version);
@@ -158,7 +161,10 @@ test('product binary exposes help/version and delegates without a shell', () =>
assert.equal(delegatedRunHelp.status, 0);
assert.equal(
delegatedRunHelp.stdout.trim(),
'Usage: ql3-run retry --command-file /absolute/private-command.json',
[
'Usage: ql3-run retry --command-file /absolute/private-command.json',
' ql3-run stop --command-file /absolute/private-command.json',
].join('\n'),
);
assert.equal(delegatedRunHelp.stderr, '');
@@ -195,7 +195,7 @@ test('binary exposes only the private command-file retry interface', () => {
'..',
'dist',
'run-management',
'runRetryCli.js',
'runManagementCli.js',
);
const help = spawnSync(process.execPath, [cli, '--help'], {
encoding: 'utf8',
@@ -203,7 +203,10 @@ test('binary exposes only the private command-file retry interface', () => {
assert.equal(help.status, 0);
assert.equal(
help.stdout.trim(),
'Usage: ql3-run retry --command-file /absolute/private-command.json',
[
'Usage: ql3-run retry --command-file /absolute/private-command.json',
' ql3-run stop --command-file /absolute/private-command.json',
].join('\n'),
);
const invalid = spawnSync(process.execPath, [cli, 'retry'], {
encoding: 'utf8',
@@ -211,6 +214,6 @@ test('binary exposes only the private command-file retry interface', () => {
assert.equal(invalid.status, 64);
assert.equal(
JSON.parse(invalid.stderr).code,
'LOCAL_RUN_RETRY_CLI_USAGE_INVALID',
'LOCAL_RUN_MANAGEMENT_CLI_USAGE_INVALID',
);
});
@@ -0,0 +1,245 @@
const assert = require('node:assert/strict');
const { spawnSync } = require('node:child_process');
const { DatabaseSync } = require('node:sqlite');
const path = require('node:path');
const { test } = require('node:test');
const {
RunCancellationNotFoundError,
} = require('@qinglong/runtime-core/run-cancellation');
const {
openLocalSqliteRuntimeDatabase,
} = require('@qinglong/local-sqlite/runtime');
const {
runLocalRunStopCommandFile,
} = require('../dist/run-management/runStopCommand.js');
const {
auditRows,
localManagementFixture,
writeCommand,
} = require('./localManagementFixture.cjs');
function uuid(value) {
return `019f9300-0000-4000-8000-${value.toString(16).padStart(12, '0')}`;
}
async function createRunningSource(value, suffix = 1) {
const runtime = await openLocalSqliteRuntimeDatabase({
databasePath: value.databasePath,
profile: 'edge',
});
try {
const task = (
await runtime.taskDefinitions.appendTaskDefinitionRevision({
projectId: 'default',
taskId: `stop-product-task-${suffix}`,
expectedRevision: null,
mutationId: uuid(10 + suffix),
name: 'Stop product task',
kind: 'command',
spec: {
schema: 'qinglong/command@v1',
config: {
command: {
kind: 'argv',
file: '/bin/echo',
args: ['stop-product'],
},
},
},
labels: {},
enabled: true,
occurredAtMs: value.now,
})
).definition;
return await (
await runtime.taskStartRepository()
).startTask({
projectId: 'default',
taskId: task.taskId,
mutationId: uuid(20 + suffix),
expectedRevision: task.revision,
expectedContentDigest: task.contentDigest,
runId: uuid(30 + suffix),
attemptId: uuid(40 + suffix),
createdEventId: uuid(50 + suffix),
queuedEventId: uuid(60 + suffix),
subject: { type: 'user', id: 'automation-user' },
policyFence: { projectVersion: 1, bindingVersion: 1 },
});
} finally {
await runtime.close();
}
}
function request(value, runId, overrides = {}) {
return {
projectId: 'default',
runId,
mutationId: uuid(100),
requestId: 'local-run-stop-product-1',
auditEventId: uuid(101),
failureAuditEventId: uuid(102),
occurredAtMs: value.now,
...overrides,
};
}
test('strongly stops one Local Run and replays through the unified CLI', async (t) => {
const value = await localManagementFixture(t);
const source = await createRunningSource(value);
const commandFile = writeCommand(
value,
'run.stop',
request(value, source.runId),
'run-stop',
);
const accepted = await runLocalRunStopCommandFile(commandFile);
assert.equal(accepted.schemaVersion, 1);
assert.equal(accepted.operation, 'run.stop');
assert.equal(accepted.stop.status, 'accepted');
assert.equal(accepted.stop.runId, source.runId);
assert.equal(accepted.stop.cancelReason, 'user');
const cli = path.join(
__dirname,
'..',
'dist',
'run-management',
'runManagementCli.js',
);
const replay = spawnSync(
process.execPath,
[cli, 'stop', '--command-file', commandFile],
{ encoding: 'utf8' },
);
assert.equal(replay.status, 0, replay.stderr);
assert.equal(JSON.parse(replay.stdout).stop.status, 'already_requested');
assert.deepEqual(
auditRows(value.databasePath)
.filter(({ operationId }) => operationId === 'run.stop')
.map((row) => ({ ...row })),
[
{
eventId: uuid(101),
operationId: 'run.stop',
outcome: 'allowed',
reasonsJson: '["role_grant","strong_authentication"]',
},
],
);
const database = new DatabaseSync(value.databasePath, { readOnly: true });
try {
assert.deepEqual(
{
...database
.prepare(
`SELECT "cancel_reason" AS "cancelReason",
(SELECT count(*) FROM "RunEvents"
WHERE "run_id" = ? AND "type" = 'run.cancel_requested')
AS "eventCount"
FROM "Runs" WHERE "id" = ?`,
)
.get(source.runId, source.runId),
},
{ cancelReason: 'user', eventCount: 1 },
);
} finally {
database.close();
}
});
test('audits a missing stop target without creating Run state', async (t) => {
const value = await localManagementFixture(t);
const commandFile = writeCommand(
value,
'run.stop',
request(value, 'missing-run', {
mutationId: uuid(110),
requestId: 'local-run-stop-missing',
auditEventId: uuid(111),
failureAuditEventId: uuid(112),
}),
'run-stop-missing',
);
await assert.rejects(
runLocalRunStopCommandFile(commandFile),
RunCancellationNotFoundError,
);
assert.deepEqual(
auditRows(value.databasePath)
.filter(({ operationId }) => operationId === 'run.stop')
.map((row) => ({ ...row })),
[
{
eventId: uuid(112),
operationId: 'run.stop',
outcome: 'denied',
reasonsJson: '["run_not_found"]',
},
],
);
});
test('denies a Viewer and preserves the running Run', async (t) => {
const value = await localManagementFixture(t);
const source = await createRunningSource(value, 2);
const policyDatabase = new DatabaseSync(value.databasePath);
try {
policyDatabase
.prepare(
`INSERT INTO "QingLong3ProjectRoleBindings" (
"project_id", "subject_type", "subject_id", "version", "state",
"role", "mutation_id", "changed_by_type", "changed_by_id",
"created_at_ms"
) VALUES ('default', 'user', 'automation-user', 2, 'active', 'viewer',
'run-stop-viewer-binding', 'user', 'automation-user', ?)`,
)
.run(value.now + 1);
} finally {
policyDatabase.close();
}
const commandFile = writeCommand(
value,
'run.stop',
request(value, source.runId, {
mutationId: uuid(120),
requestId: 'local-run-stop-viewer',
auditEventId: uuid(121),
failureAuditEventId: uuid(122),
}),
'run-stop-viewer',
);
await assert.rejects(
runLocalRunStopCommandFile(commandFile),
(error) => error?.code === 'LOCAL_RUN_STOP_COMMAND_AUTHORIZATION_REJECTED',
);
assert.deepEqual(
auditRows(value.databasePath)
.filter(({ operationId }) => operationId === 'run.stop')
.map((row) => ({ ...row })),
[
{
eventId: uuid(122),
operationId: 'run.stop',
outcome: 'denied',
reasonsJson: '["run_stop_fence_rejected"]',
},
],
);
const database = new DatabaseSync(value.databasePath, { readOnly: true });
try {
assert.equal(
database
.prepare(
`SELECT "cancel_requested_at_ms" AS value FROM "Runs" WHERE "id" = ?`,
)
.get(source.runId).value,
null,
);
} finally {
database.close();
}
});