feat(ql3): page blocked cancellations

This commit is contained in:
whyour
2026-08-19 23:51:14 +08:00
parent 095683a4cb
commit cf21e984cb
31 changed files with 1466 additions and 42 deletions
@@ -0,0 +1,104 @@
'use strict';
const assert = require('node:assert/strict');
const { test } = require('node:test');
const {
createRunCancellationBlockedListCommand,
decodeRunCancellationBlockedCursor,
encodeRunCancellationBlockedCursor,
formatRunCancellationBlockedListCard,
projectRunCancellationBlockedList,
} = require('../dist/run-management/runCancellationBlockedList.js');
const UUIDS = [
'019fa000-0000-4000-8000-000000000001',
'019fa000-0000-4000-8000-000000000002',
'019fa000-0000-4000-8000-000000000003',
];
test('round-trips one bounded opaque blocked cursor', () => {
const cursor = {
snapshotAtMs: 1_700_000_000_000,
blockedAtMs: 1_699_999_999_000,
runId: 'run-16',
};
const token = encodeRunCancellationBlockedCursor(cursor);
assert.match(token, /^v1\.[A-Za-z0-9_-]+$/);
assert.deepEqual(decodeRunCancellationBlockedCursor(token), cursor);
for (const invalid of [
'v2.abc',
'v1.***',
`${token}=`,
'v1.e30',
]) {
assert.throws(() => decodeRunCancellationBlockedCursor(invalid));
}
});
test('builds one fixed blocked list command with no caller limit', () => {
let index = 0;
const cursor = encodeRunCancellationBlockedCursor({
snapshotAtMs: 1_700_000_000_000,
blockedAtMs: 1_699_999_999_000,
runId: 'run-16',
});
const command = createRunCancellationBlockedListCommand(
'project-1',
cursor,
() => UUIDS[index++],
);
assert.equal(command.operation, 'run.cancellation.blocked.list');
assert.equal(command.request.projectId, 'project-1');
assert.equal(Object.hasOwn(command.request, 'runId'), false);
assert.equal(Object.hasOwn(command.request.body, 'limit'), false);
assert.deepEqual(command.request.body.after, {
snapshotAtMs: 1_700_000_000_000,
blockedAtMs: 1_699_999_999_000,
runId: 'run-16',
});
});
test('projects a low-sensitive page and renders a deterministic card', () => {
const result = {
schemaVersion: 1,
requestId: 'request-blocked-1',
result: {
schemaVersion: 1,
operation: 'run.cancellation.blocked.list',
page: {
schema: 'qinglong/run-cancellation-dispatch-blocked-page@v1',
projectId: 'project-1',
snapshotAtMs: 1_700_000_000_000,
observedAtMs: 1_700_000_000_100,
items: Array.from({ length: 16 }, (_, index) => ({
runId: `run-${index + 1}`,
blockedAtMs: 1_699_999_999_000 + index,
})),
truncated: true,
nextCursor: {
snapshotAtMs: 1_700_000_000_000,
blockedAtMs: 1_699_999_999_015,
runId: 'run-16',
},
},
},
};
const observation = projectRunCancellationBlockedList(result);
assert.equal(observation.schema, 'qinglong/run-cancellation-blocked-list@v1');
assert.equal(observation.items.length, 16);
assert.match(observation.nextCursor, /^v1\./);
const card = formatRunCancellationBlockedListCard(observation);
assert.match(card, /Blocked Cancellations/);
assert.match(card, /run-1/);
assert.match(card, /NEXT_CURSOR\s+v1\./);
for (const forbidden of [
'attemptId',
'lastResult',
'leaseOwner',
'leaseToken',
'\u001b',
]) {
assert.equal(card.includes(forbidden), false);
}
});
@@ -187,6 +187,18 @@ function fixture(role = 'operator', options = {}) {
rowCount: 1,
};
}
if (
text.startsWith(
'SELECT run_id AS "runId", updated_at_ms AS "blockedAtMs"',
)
) {
return {
rows: [
{ runId: 'run-1', blockedAtMs: String(NOW - 1_500) },
],
rowCount: 1,
};
}
if (
text.startsWith('SELECT attempt_id AS "attemptId"') &&
text.includes('FROM "ql3"."run_cancellation_dispatches"') &&
@@ -425,6 +437,35 @@ test('allows a viewer to summarize Project cancellation availability atomically'
);
});
test('allows a viewer to page blocked Run identities under run.read', async () => {
const { calls, service } = fixture('viewer');
const listRequest = {
projectId: 'project-1',
requestId: 'request-blocked-1',
auditEventId: '019f9500-0000-4000-8000-000000000071',
failureAuditEventId: '019f9500-0000-4000-8000-000000000072',
principal: request().principal,
};
const result = await service.listBlockedCancellations(listRequest);
assert.deepEqual(result.items, [
{ runId: 'run-1', blockedAtMs: NOW - 1_500 },
]);
assert.equal(result.snapshotAtMs, NOW);
assert.equal(result.truncated, false);
const list = calls.find(({ sql }) =>
sql.startsWith(
'SELECT run_id AS "runId", updated_at_ms AS "blockedAtMs"',
),
);
assert.deepEqual(list.params, ['project-1', NOW, null, '', 17]);
const audit = calls.find(
({ sql, params }) =>
sql.startsWith('INSERT INTO "ql3"."security_audit_events"') &&
params[2] === 'run.cancellation.blocked.list',
);
assert.equal(audit.params[0], listRequest.auditEventId);
});
test('authorizes exact cancellation rearm and keeps the event identity server-side', async () => {
const { calls, service } = fixture();
const rearmRequest = {
@@ -147,6 +147,21 @@ const summaryCommand = normalizeClusterRunManagementCommand({
},
});
const blockedListCommand = normalizeClusterRunManagementCommand({
schemaVersion: 1,
operation: 'run.cancellation.blocked.list',
request: {
projectId: 'project-1',
requestId: 'request-blocked-1',
auditEventId: '019f9400-0000-4000-8000-000000000061',
failureAuditEventId: '019f9400-0000-4000-8000-000000000062',
body: {
schema: 'qinglong/run-cancellation-dispatch-blocked-list-request@v1',
after: null,
},
},
});
const rearmCommand = normalizeClusterRunManagementCommand({
schemaVersion: 1,
operation: 'run.cancellation.rearm',
@@ -412,6 +427,54 @@ test('validates the fixed low-sensitive Project cancellation summary', () => {
}
});
test('validates one snapshot-bound low-sensitive blocked page', () => {
const value = {
schemaVersion: 1,
operation: 'run.cancellation.blocked.list',
page: {
schema: 'qinglong/run-cancellation-dispatch-blocked-page@v1',
projectId: 'project-1',
snapshotAtMs: 1_000_000,
observedAtMs: 1_000_000,
items: [
{ runId: 'run-1', blockedAtMs: 999_100 },
{ runId: 'run-2', blockedAtMs: 999_200 },
],
truncated: false,
},
};
assert.deepEqual(
validateClusterRunManagementClientResult(value, blockedListCommand),
value,
);
for (const page of [
{ ...value.page, projectId: 'project-2' },
{ ...value.page, snapshotAtMs: 999_999 },
{
...value.page,
items: [value.page.items[1], value.page.items[0]],
},
{
...value.page,
items: [{ ...value.page.items[0], attemptId: 'attempt-1' }],
},
{
...value.page,
items: [{ ...value.page.items[0], lastResult: 'identity_mismatch' }],
},
{ ...value.page, truncated: true },
]) {
assert.throws(
() =>
validateClusterRunManagementClientResult(
{ ...value, page },
blockedListCommand,
),
ClusterPluginPackageManagementClientRequestError,
);
}
});
test('binds a rearm receipt to the exact dispatch version, result and delay fences', () => {
const value = {
schemaVersion: 1,
@@ -91,11 +91,27 @@ test('status mode calls only the summary operation and emits an alert exit', asy
authorized: request.socket.authorized,
command,
});
const bytes = Buffer.from(
JSON.stringify({
schemaVersion: 1,
requestId: command.request.requestId,
result: {
const managementResult =
command.operation === 'run.cancellation.blocked.list'
? {
schemaVersion: 1,
operation: 'run.cancellation.blocked.list',
page: {
schema:
'qinglong/run-cancellation-dispatch-blocked-page@v1',
projectId: 'project-1',
snapshotAtMs: 1_700_000_000_000,
observedAtMs: 1_700_000_000_000,
items: [
{
runId: 'run-1',
blockedAtMs: 1_699_999_999_000,
},
],
truncated: false,
},
}
: {
schemaVersion: 1,
operation: 'run.cancellation.summary',
summary: {
@@ -121,7 +137,12 @@ test('status mode calls only the summary operation and emits an alert exit', asy
},
oldestBlockedAtMs: 1_699_999_999_000,
},
},
};
const bytes = Buffer.from(
JSON.stringify({
schemaVersion: 1,
requestId: command.request.requestId,
result: managementResult,
}),
'utf8',
);
@@ -192,12 +213,37 @@ test('status mode calls only the summary operation and emits an alert exit', asy
schema: 'qinglong/run-cancellation-dispatch-summary-request@v1',
});
assert.equal(Object.hasOwn(requests[0].command.request, 'runId'), false);
const blocked = await runCli([
'blocked',
`--config=${configFile}`,
`--assertion=${assertionFile}`,
'--project=project-1',
'--format=json',
]);
assert.equal(blocked.status, 0, blocked.stderr);
assert.equal(blocked.stderr, '');
const blockedOutput = JSON.parse(blocked.stdout);
assert.equal(
blockedOutput.schema,
'qinglong/run-cancellation-blocked-list@v1',
);
assert.deepEqual(blockedOutput.items, [
{ runId: 'run-1', blockedAtMs: 1_699_999_999_000 },
]);
assert.equal(requests.length, 2);
assert.equal(requests[1].command.operation, 'run.cancellation.blocked.list');
assert.deepEqual(requests[1].command.request.body, {
schema: 'qinglong/run-cancellation-dispatch-blocked-list-request@v1',
after: null,
});
});
test('help documents status routing and invalid projects fail before I/O', async () => {
const help = await runCli(['--help']);
assert.equal(help.status, 0);
assert.match(help.stdout, /ql3-run-client status/);
assert.match(help.stdout, /ql3-run-client blocked/);
assert.match(help.stdout, /0=clear, 10=converging, 20=attention_required/);
const rejected = await runCli([
@@ -214,4 +260,14 @@ test('help documents status routing and invalid projects fail before I/O', async
event: 'usage_invalid',
code: 'QL3_RUN_MANAGEMENT_CLIENT_USAGE_INVALID',
});
const invalidCursor = await runCli([
'blocked',
'--config=/private/client.json',
'--assertion=/private/assertion.jwt',
'--project=project-1',
'--cursor=v1.***',
]);
assert.equal(invalidCursor.status, 64);
assert.equal(invalidCursor.stdout, '');
});
@@ -144,6 +144,16 @@ function summaryResult() {
};
}
function blockedListResult() {
return {
projectId: 'project-1',
snapshotAtMs: NOW,
observedAtMs: NOW,
items: [{ runId: 'run-1', blockedAtMs: NOW - 800 }],
truncated: false,
};
}
function rearmResult() {
return {
status: 'rearmed',
@@ -195,6 +205,24 @@ function summaryCommand(overrides = {}) {
};
}
function blockedListCommand(overrides = {}) {
return {
schemaVersion: 1,
operation: 'run.cancellation.blocked.list',
request: {
projectId: 'project-1',
requestId: 'request-blocked-1',
auditEventId: '019f9300-0000-4000-8000-000000000061',
failureAuditEventId: '019f9300-0000-4000-8000-000000000062',
body: {
schema: 'qinglong/run-cancellation-dispatch-blocked-list-request@v1',
after: null,
},
...overrides,
},
};
}
function rearmCommand(overrides = {}) {
return {
schemaVersion: 1,
@@ -232,6 +260,9 @@ test('routes one exact strong User retry and emits the shared response', async (
async summarizeCancellation() {
return summaryResult();
},
async listBlockedCancellations() {
return blockedListResult();
},
async inspectCancellation() {
return diagnosticResult();
},
@@ -271,6 +302,9 @@ test('routes one exact strong User stop and emits the shared response', async ()
async summarizeCancellation() {
return summaryResult();
},
async listBlockedCancellations() {
return blockedListResult();
},
async inspectCancellation() {
return diagnosticResult();
},
@@ -310,6 +344,9 @@ test('rejects weak or non-User identity before service authority', async () => {
async summarizeCancellation() {
return summaryResult();
},
async listBlockedCancellations() {
return blockedListResult();
},
async inspectCancellation() {
return diagnosticResult();
},
@@ -342,6 +379,7 @@ test('routes bounded cancellation inspection without lease capability data', asy
async retry() { return retryResult(); },
async stop() { return stopResult(); },
async summarizeCancellation() { return summaryResult(); },
async listBlockedCancellations() { return blockedListResult(); },
async inspectCancellation(request) {
calls.push(request);
return diagnosticResult();
@@ -376,6 +414,7 @@ test('routes one Project-scoped cancellation summary without Run identity', asyn
calls.push(request);
return summaryResult();
},
async listBlockedCancellations() { return blockedListResult(); },
async inspectCancellation() { return diagnosticResult(); },
async rearmCancellation() { return rearmResult(); },
},
@@ -398,6 +437,47 @@ test('routes one Project-scoped cancellation summary without Run identity', asyn
assert.equal(JSON.stringify(result).includes('leaseOwner'), false);
});
test('routes one fixed Project blocked page without dispatch internals', async () => {
const calls = [];
const transport = createClusterRunManagementTransport({
now: () => NOW,
service: {
async retry() { return retryResult(); },
async stop() { return stopResult(); },
async summarizeCancellation() { return summaryResult(); },
async listBlockedCancellations(request) {
calls.push(request);
return blockedListResult();
},
async inspectCancellation() { return diagnosticResult(); },
async rearmCancellation() { return rearmResult(); },
},
});
const result = await transport.execute(blockedListCommand(), {
authenticate: async () => principal(),
});
assert.equal(calls.length, 1);
assert.equal(calls[0].projectId, 'project-1');
assert.equal(Object.hasOwn(calls[0], 'after'), false);
assert.deepEqual(result, {
schemaVersion: 1,
operation: 'run.cancellation.blocked.list',
page: {
schema: 'qinglong/run-cancellation-dispatch-blocked-page@v1',
...blockedListResult(),
},
});
for (const forbidden of [
'attemptId',
'dispatchVersion',
'lastResult',
'leaseOwner',
'leaseToken',
]) {
assert.equal(JSON.stringify(result).includes(forbidden), false);
}
});
test('routes an exact blocked cancellation rearm receipt', async () => {
const calls = [];
const transport = createClusterRunManagementTransport({
@@ -406,6 +486,7 @@ test('routes an exact blocked cancellation rearm receipt', async () => {
async retry() { return retryResult(); },
async stop() { return stopResult(); },
async summarizeCancellation() { return summaryResult(); },
async listBlockedCancellations() { return blockedListResult(); },
async inspectCancellation() { return diagnosticResult(); },
async rearmCancellation(request) {
calls.push(request);