mirror of
https://github.com/whyour/qinglong.git
synced 2026-09-20 16:07:11 +08:00
feat(ql3): add bounded trusted run comparison
This commit is contained in:
@@ -236,6 +236,12 @@
|
||||
"builtin-run-read-projection": [
|
||||
"dist/tool-execution/builtin-run-read/builtInRunReadProjection.d.ts"
|
||||
],
|
||||
"builtin-run-compare-tool": [
|
||||
"dist/tool-execution/builtin-run-compare/builtInRunCompareTool.d.ts"
|
||||
],
|
||||
"builtin-run-compare-projection": [
|
||||
"dist/tool-execution/builtin-run-compare/builtInRunCompareProjection.d.ts"
|
||||
],
|
||||
"run": [
|
||||
"dist/run/run.d.ts"
|
||||
],
|
||||
@@ -652,6 +658,16 @@
|
||||
"require": "./dist/tool-execution/builtin-run-read/builtInRunReadProjection.js",
|
||||
"default": "./dist/tool-execution/builtin-run-read/builtInRunReadProjection.js"
|
||||
},
|
||||
"./builtin-run-compare-tool": {
|
||||
"types": "./dist/tool-execution/builtin-run-compare/builtInRunCompareTool.d.ts",
|
||||
"require": "./dist/tool-execution/builtin-run-compare/builtInRunCompareTool.js",
|
||||
"default": "./dist/tool-execution/builtin-run-compare/builtInRunCompareTool.js"
|
||||
},
|
||||
"./builtin-run-compare-projection": {
|
||||
"types": "./dist/tool-execution/builtin-run-compare/builtInRunCompareProjection.d.ts",
|
||||
"require": "./dist/tool-execution/builtin-run-compare/builtInRunCompareProjection.js",
|
||||
"default": "./dist/tool-execution/builtin-run-compare/builtInRunCompareProjection.js"
|
||||
},
|
||||
"./secret-reference": {
|
||||
"types": "./dist/secret/secretReference.d.ts",
|
||||
"require": "./dist/secret/secretReference.js",
|
||||
|
||||
+305
@@ -0,0 +1,305 @@
|
||||
import { EXECUTION_ORIGINS, RUN_STATUSES } from '../../run/run';
|
||||
import type { RunRepositoryReader } from '../../run/runRepository';
|
||||
import {
|
||||
BoundedRunReadProjectionUnavailableError,
|
||||
executeBoundedRunReadProjection,
|
||||
type BoundedRunReadProjection,
|
||||
} from '../../run/projection/boundedRunReadProjection';
|
||||
import {
|
||||
normalizeToolDefinition,
|
||||
type ToolJsonValue,
|
||||
} from '../tool-registry/toolRegistry';
|
||||
|
||||
export const BUILTIN_RUN_COMPARE_TOOL = Object.freeze({
|
||||
name: 'qinglong.run.compare',
|
||||
version: '1.0.0',
|
||||
});
|
||||
export const BUILTIN_RUN_COMPARE_TIMEOUT_SECONDS = 5;
|
||||
|
||||
const MAX_INT = 2_147_483_647;
|
||||
const MIN_INT = -2_147_483_648;
|
||||
const CONTROL_PATTERN = /[\u0000-\u001f\u007f]/;
|
||||
const COMPARABLE_FIELDS = Object.freeze([
|
||||
'taskId',
|
||||
'taskRevision',
|
||||
'status',
|
||||
'priority',
|
||||
'executionOrigin',
|
||||
'executionOwner',
|
||||
] as const);
|
||||
|
||||
const RUN_PROJECTION_SCHEMA = Object.freeze({
|
||||
type: 'object' as const,
|
||||
properties: {
|
||||
found: { type: 'boolean' as const },
|
||||
id: { type: 'string' as const, minLength: 1, maxLength: 128 },
|
||||
taskId: { type: 'string' as const, minLength: 1, maxLength: 255 },
|
||||
taskRevision: { type: 'string' as const, minLength: 1, maxLength: 255 },
|
||||
status: {
|
||||
type: 'string' as const,
|
||||
maxLength: 32,
|
||||
enum: RUN_STATUSES,
|
||||
},
|
||||
version: { type: 'integer' as const, minimum: 0, maximum: MAX_INT },
|
||||
eventSequence: {
|
||||
type: 'integer' as const,
|
||||
minimum: 0,
|
||||
maximum: MAX_INT,
|
||||
},
|
||||
priority: {
|
||||
type: 'integer' as const,
|
||||
minimum: MIN_INT,
|
||||
maximum: MAX_INT,
|
||||
},
|
||||
executionOrigin: {
|
||||
type: 'string' as const,
|
||||
maxLength: 32,
|
||||
enum: EXECUTION_ORIGINS,
|
||||
},
|
||||
executionOwner: {
|
||||
type: 'string' as const,
|
||||
maxLength: 16,
|
||||
enum: ['legacy', 'runtime'] as const,
|
||||
},
|
||||
createdAtMs: {
|
||||
type: 'integer' as const,
|
||||
minimum: 0,
|
||||
maximum: Number.MAX_SAFE_INTEGER,
|
||||
},
|
||||
queuedAtMs: {
|
||||
type: 'integer' as const,
|
||||
minimum: 0,
|
||||
maximum: Number.MAX_SAFE_INTEGER,
|
||||
},
|
||||
startedAtMs: {
|
||||
type: 'integer' as const,
|
||||
minimum: 0,
|
||||
maximum: Number.MAX_SAFE_INTEGER,
|
||||
},
|
||||
finishedAtMs: {
|
||||
type: 'integer' as const,
|
||||
minimum: 0,
|
||||
maximum: Number.MAX_SAFE_INTEGER,
|
||||
},
|
||||
},
|
||||
required: ['found'] as const,
|
||||
additionalProperties: false,
|
||||
});
|
||||
|
||||
export const BUILTIN_RUN_COMPARE_TOOL_DEFINITION = normalizeToolDefinition({
|
||||
name: BUILTIN_RUN_COMPARE_TOOL.name,
|
||||
version: BUILTIN_RUN_COMPARE_TOOL.version,
|
||||
description:
|
||||
'Compare two low-sensitive Project-scoped Run projections using bounded point reads',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
baselineRunId: { type: 'string', minLength: 1, maxLength: 128 },
|
||||
candidateRunId: { type: 'string', minLength: 1, maxLength: 128 },
|
||||
},
|
||||
required: ['baselineRunId', 'candidateRunId'],
|
||||
additionalProperties: false,
|
||||
},
|
||||
outputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
baseline: RUN_PROJECTION_SCHEMA,
|
||||
candidate: RUN_PROJECTION_SCHEMA,
|
||||
comparable: { type: 'boolean' },
|
||||
sameTask: { type: 'boolean' },
|
||||
sameTaskRevision: { type: 'boolean' },
|
||||
changedFields: {
|
||||
type: 'array',
|
||||
items: {
|
||||
type: 'string',
|
||||
maxLength: 32,
|
||||
enum: COMPARABLE_FIELDS,
|
||||
},
|
||||
maxItems: COMPARABLE_FIELDS.length,
|
||||
},
|
||||
queueDelayDeltaMs: {
|
||||
type: 'integer',
|
||||
minimum: -Number.MAX_SAFE_INTEGER,
|
||||
maximum: Number.MAX_SAFE_INTEGER,
|
||||
},
|
||||
executionDurationDeltaMs: {
|
||||
type: 'integer',
|
||||
minimum: -Number.MAX_SAFE_INTEGER,
|
||||
maximum: Number.MAX_SAFE_INTEGER,
|
||||
},
|
||||
totalDurationDeltaMs: {
|
||||
type: 'integer',
|
||||
minimum: -Number.MAX_SAFE_INTEGER,
|
||||
maximum: Number.MAX_SAFE_INTEGER,
|
||||
},
|
||||
consistency: {
|
||||
type: 'string',
|
||||
maxLength: 32,
|
||||
enum: ['ordered_independent_point_reads'],
|
||||
},
|
||||
},
|
||||
required: [
|
||||
'baseline',
|
||||
'candidate',
|
||||
'comparable',
|
||||
'sameTask',
|
||||
'sameTaskRevision',
|
||||
'changedFields',
|
||||
'consistency',
|
||||
],
|
||||
additionalProperties: false,
|
||||
},
|
||||
effect: 'read',
|
||||
risk: 'low',
|
||||
requiredPermissions: ['run.read'],
|
||||
timeoutSeconds: BUILTIN_RUN_COMPARE_TIMEOUT_SECONDS,
|
||||
});
|
||||
|
||||
export class InvalidBuiltInRunCompareToolError extends TypeError {
|
||||
readonly code = 'BUILTIN_RUN_COMPARE_TOOL_INVALID';
|
||||
|
||||
constructor(message: string) {
|
||||
super(`Built-in Run compare Tool is invalid: ${message}`);
|
||||
this.name = 'InvalidBuiltInRunCompareToolError';
|
||||
}
|
||||
}
|
||||
|
||||
export class BuiltInRunCompareToolUnavailableError extends Error {
|
||||
readonly code = 'BUILTIN_RUN_COMPARE_TOOL_UNAVAILABLE';
|
||||
|
||||
constructor() {
|
||||
super('Built-in Run compare Tool is unavailable');
|
||||
this.name = 'BuiltInRunCompareToolUnavailableError';
|
||||
}
|
||||
}
|
||||
|
||||
function invalid(message: string): never {
|
||||
throw new InvalidBuiltInRunCompareToolError(message);
|
||||
}
|
||||
|
||||
function boundedText(value: unknown, maximum: number): value is string {
|
||||
return (
|
||||
typeof value === 'string' &&
|
||||
value.length > 0 &&
|
||||
value.length <= maximum &&
|
||||
!CONTROL_PATTERN.test(value)
|
||||
);
|
||||
}
|
||||
|
||||
function timestampDelta(
|
||||
baselineStart: unknown,
|
||||
baselineEnd: unknown,
|
||||
candidateStart: unknown,
|
||||
candidateEnd: unknown,
|
||||
): number | undefined {
|
||||
if (
|
||||
!Number.isSafeInteger(baselineStart) ||
|
||||
!Number.isSafeInteger(baselineEnd) ||
|
||||
!Number.isSafeInteger(candidateStart) ||
|
||||
!Number.isSafeInteger(candidateEnd)
|
||||
) {
|
||||
return undefined;
|
||||
}
|
||||
if (
|
||||
Number(baselineEnd) < Number(baselineStart) ||
|
||||
Number(candidateEnd) < Number(candidateStart)
|
||||
) {
|
||||
return undefined;
|
||||
}
|
||||
const baselineDuration = Number(baselineEnd) - Number(baselineStart);
|
||||
const candidateDuration = Number(candidateEnd) - Number(candidateStart);
|
||||
const delta = candidateDuration - baselineDuration;
|
||||
return Number.isSafeInteger(delta) ? delta : undefined;
|
||||
}
|
||||
|
||||
function compareProjections(
|
||||
baseline: BoundedRunReadProjection,
|
||||
candidate: BoundedRunReadProjection,
|
||||
): Readonly<Record<string, ToolJsonValue>> {
|
||||
const comparable = baseline.found === true && candidate.found === true;
|
||||
const changedFields = Object.freeze(
|
||||
comparable
|
||||
? COMPARABLE_FIELDS.filter((field) => baseline[field] !== candidate[field])
|
||||
: [],
|
||||
);
|
||||
const queueDelayDeltaMs = comparable
|
||||
? timestampDelta(
|
||||
baseline.createdAtMs,
|
||||
baseline.queuedAtMs,
|
||||
candidate.createdAtMs,
|
||||
candidate.queuedAtMs,
|
||||
)
|
||||
: undefined;
|
||||
const executionDurationDeltaMs = comparable
|
||||
? timestampDelta(
|
||||
baseline.startedAtMs,
|
||||
baseline.finishedAtMs,
|
||||
candidate.startedAtMs,
|
||||
candidate.finishedAtMs,
|
||||
)
|
||||
: undefined;
|
||||
const totalDurationDeltaMs = comparable
|
||||
? timestampDelta(
|
||||
baseline.createdAtMs,
|
||||
baseline.finishedAtMs,
|
||||
candidate.createdAtMs,
|
||||
candidate.finishedAtMs,
|
||||
)
|
||||
: undefined;
|
||||
return Object.freeze({
|
||||
baseline,
|
||||
candidate,
|
||||
comparable,
|
||||
sameTask: comparable && baseline.taskId === candidate.taskId,
|
||||
sameTaskRevision:
|
||||
comparable && baseline.taskRevision === candidate.taskRevision,
|
||||
changedFields,
|
||||
...(queueDelayDeltaMs === undefined ? {} : { queueDelayDeltaMs }),
|
||||
...(executionDurationDeltaMs === undefined
|
||||
? {}
|
||||
: { executionDurationDeltaMs }),
|
||||
...(totalDurationDeltaMs === undefined ? {} : { totalDurationDeltaMs }),
|
||||
consistency: 'ordered_independent_point_reads',
|
||||
});
|
||||
}
|
||||
|
||||
export async function executeBuiltInRunCompareTool(
|
||||
runs: Pick<RunRepositoryReader, 'findRunById'>,
|
||||
projectId: string,
|
||||
input: ToolJsonValue,
|
||||
): Promise<Readonly<Record<string, ToolJsonValue>>> {
|
||||
const inputRecord =
|
||||
input && typeof input === 'object' && !Array.isArray(input)
|
||||
? (input as Readonly<Record<string, ToolJsonValue>>)
|
||||
: null;
|
||||
if (
|
||||
!runs ||
|
||||
typeof runs.findRunById !== 'function' ||
|
||||
!boundedText(projectId, 128) ||
|
||||
!inputRecord ||
|
||||
Reflect.ownKeys(inputRecord).length !== 2 ||
|
||||
!boundedText(inputRecord.baselineRunId, 128) ||
|
||||
!boundedText(inputRecord.candidateRunId, 128) ||
|
||||
inputRecord.baselineRunId === inputRecord.candidateRunId
|
||||
) {
|
||||
return invalid('execution context or input is invalid');
|
||||
}
|
||||
try {
|
||||
const baseline = await executeBoundedRunReadProjection(
|
||||
runs,
|
||||
projectId,
|
||||
inputRecord.baselineRunId,
|
||||
);
|
||||
const candidate = await executeBoundedRunReadProjection(
|
||||
runs,
|
||||
projectId,
|
||||
inputRecord.candidateRunId,
|
||||
);
|
||||
return compareProjections(baseline, candidate);
|
||||
} catch (error) {
|
||||
if (!(error instanceof BoundedRunReadProjectionUnavailableError)) {
|
||||
return invalid('execution context or input is invalid');
|
||||
}
|
||||
throw new BuiltInRunCompareToolUnavailableError();
|
||||
}
|
||||
}
|
||||
+158
@@ -0,0 +1,158 @@
|
||||
import type { DeploymentProfile } from '../../cluster-control/clusterControlActivation';
|
||||
import type { RunRepositoryReader } from '../../run/runRepository';
|
||||
import {
|
||||
normalizeProjectToolDefinitionSnapshot,
|
||||
type ProjectToolDefinitionSnapshot,
|
||||
} from '../tool-registry/projectToolDefinitionSnapshot';
|
||||
import {
|
||||
ToolDefinitionRegistry,
|
||||
type ToolJsonValue,
|
||||
} from '../tool-registry/toolRegistry';
|
||||
import {
|
||||
createTrustedToolHandlerBinding,
|
||||
normalizeTrustedToolHandlerBinding,
|
||||
type TrustedToolHandlerBinding,
|
||||
} from '../trustedToolInvocation';
|
||||
import type {
|
||||
TrustedToolExecutionAdapter,
|
||||
TrustedToolExecutionAdapterContext,
|
||||
} from '../trustedToolExecution';
|
||||
import {
|
||||
BUILTIN_RUN_COMPARE_TOOL,
|
||||
BUILTIN_RUN_COMPARE_TOOL_DEFINITION,
|
||||
BUILTIN_RUN_COMPARE_TIMEOUT_SECONDS,
|
||||
InvalidBuiltInRunCompareToolError,
|
||||
executeBuiltInRunCompareTool,
|
||||
} from './builtInRunCompareProjection';
|
||||
|
||||
export {
|
||||
BUILTIN_RUN_COMPARE_TOOL,
|
||||
BUILTIN_RUN_COMPARE_TOOL_DEFINITION,
|
||||
BUILTIN_RUN_COMPARE_TIMEOUT_SECONDS,
|
||||
BuiltInRunCompareToolUnavailableError,
|
||||
InvalidBuiltInRunCompareToolError,
|
||||
executeBuiltInRunCompareTool,
|
||||
} from './builtInRunCompareProjection';
|
||||
|
||||
export const BUILTIN_RUN_COMPARE_ADAPTER = Object.freeze({
|
||||
id: 'builtin.qinglong.run-compare',
|
||||
version: '1.0.0',
|
||||
});
|
||||
export const BUILTIN_RUN_COMPARE_REDACTION_CONTRACT = Object.freeze({
|
||||
id: 'redaction.qinglong.run-compare',
|
||||
version: '1.0.0',
|
||||
});
|
||||
export const BUILTIN_RUN_COMPARE_AUDIT_CONTRACT = Object.freeze({
|
||||
id: 'audit.qinglong.tool-call',
|
||||
version: '1.0.0',
|
||||
});
|
||||
const CONTROL_PATTERN = /[\u0000-\u001f\u007f]/;
|
||||
|
||||
function invalid(message: string): never {
|
||||
throw new InvalidBuiltInRunCompareToolError(message);
|
||||
}
|
||||
|
||||
function sameValue(left: unknown, right: unknown): boolean {
|
||||
return JSON.stringify(left) === JSON.stringify(right);
|
||||
}
|
||||
|
||||
function boundedText(value: unknown, maximum: number): value is string {
|
||||
return (
|
||||
typeof value === 'string' &&
|
||||
value.length > 0 &&
|
||||
value.length <= maximum &&
|
||||
!CONTROL_PATTERN.test(value)
|
||||
);
|
||||
}
|
||||
|
||||
export function createBuiltInRunCompareToolHandlerBinding(
|
||||
snapshotValue: ProjectToolDefinitionSnapshot,
|
||||
profiles: readonly DeploymentProfile[],
|
||||
): Readonly<TrustedToolHandlerBinding> {
|
||||
const snapshot = normalizeProjectToolDefinitionSnapshot(snapshotValue);
|
||||
const definition = snapshot.definitions.find(
|
||||
(entry) =>
|
||||
entry.definition.name === BUILTIN_RUN_COMPARE_TOOL.name &&
|
||||
entry.definition.version === BUILTIN_RUN_COMPARE_TOOL.version,
|
||||
)?.definition;
|
||||
if (!definition || !sameValue(definition, BUILTIN_RUN_COMPARE_TOOL_DEFINITION)) {
|
||||
return invalid('reviewed Tool definition is absent or changed');
|
||||
}
|
||||
return createTrustedToolHandlerBinding(snapshot, {
|
||||
tool: BUILTIN_RUN_COMPARE_TOOL,
|
||||
adapter: BUILTIN_RUN_COMPARE_ADAPTER,
|
||||
executionClass: 'builtin_in_process',
|
||||
profiles,
|
||||
authorities: ['database.read'],
|
||||
timeoutSeconds: BUILTIN_RUN_COMPARE_TIMEOUT_SECONDS,
|
||||
redactionContract: BUILTIN_RUN_COMPARE_REDACTION_CONTRACT,
|
||||
auditContract: BUILTIN_RUN_COMPARE_AUDIT_CONTRACT,
|
||||
});
|
||||
}
|
||||
|
||||
export class BuiltInRunCompareToolAdapter
|
||||
implements TrustedToolExecutionAdapter
|
||||
{
|
||||
readonly binding!: Readonly<TrustedToolHandlerBinding>;
|
||||
readonly profile!: DeploymentProfile;
|
||||
readonly recoveryMode = 'retry_safe_read' as const;
|
||||
readonly #runs!: Pick<RunRepositoryReader, 'findRunById'>;
|
||||
|
||||
constructor(
|
||||
bindingValue: TrustedToolHandlerBinding,
|
||||
profile: DeploymentProfile,
|
||||
definitions: ToolDefinitionRegistry,
|
||||
runs: Pick<RunRepositoryReader, 'findRunById'>,
|
||||
) {
|
||||
const binding = normalizeTrustedToolHandlerBinding(bindingValue);
|
||||
if (!(definitions instanceof ToolDefinitionRegistry)) {
|
||||
return invalid('Tool Definition registry is invalid');
|
||||
}
|
||||
let definition;
|
||||
try {
|
||||
definition = definitions.resolve(
|
||||
BUILTIN_RUN_COMPARE_TOOL.name,
|
||||
BUILTIN_RUN_COMPARE_TOOL.version,
|
||||
);
|
||||
} catch {
|
||||
return invalid('reviewed Tool definition is unavailable');
|
||||
}
|
||||
if (
|
||||
!sameValue(binding.tool, BUILTIN_RUN_COMPARE_TOOL) ||
|
||||
!sameValue(binding.adapter, BUILTIN_RUN_COMPARE_ADAPTER) ||
|
||||
binding.executionClass !== 'builtin_in_process' ||
|
||||
!sameValue(binding.authorities, ['database.read']) ||
|
||||
binding.timeoutSeconds !== BUILTIN_RUN_COMPARE_TIMEOUT_SECONDS ||
|
||||
!sameValue(
|
||||
binding.redactionContract,
|
||||
BUILTIN_RUN_COMPARE_REDACTION_CONTRACT,
|
||||
) ||
|
||||
!sameValue(binding.auditContract, BUILTIN_RUN_COMPARE_AUDIT_CONTRACT) ||
|
||||
!binding.profiles.includes(profile) ||
|
||||
!sameValue(definition, BUILTIN_RUN_COMPARE_TOOL_DEFINITION)
|
||||
) {
|
||||
return invalid('binding does not match the reviewed adapter contract');
|
||||
}
|
||||
if (!runs || typeof runs.findRunById !== 'function') {
|
||||
return invalid('Run repository is invalid');
|
||||
}
|
||||
this.binding = binding;
|
||||
this.profile = profile;
|
||||
this.#runs = runs;
|
||||
Object.freeze(this);
|
||||
}
|
||||
|
||||
async execute(
|
||||
context: Readonly<TrustedToolExecutionAdapterContext>,
|
||||
input: ToolJsonValue,
|
||||
): Promise<unknown> {
|
||||
if (
|
||||
!context ||
|
||||
typeof context !== 'object' ||
|
||||
!boundedText(context.projectId, 128)
|
||||
) {
|
||||
return invalid('execution context or input is invalid');
|
||||
}
|
||||
return executeBuiltInRunCompareTool(this.#runs, context.projectId, input);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,280 @@
|
||||
const assert = require('node:assert/strict');
|
||||
const { test } = require('node:test');
|
||||
|
||||
const {
|
||||
BUILTIN_RUN_COMPARE_ADAPTER,
|
||||
BUILTIN_RUN_COMPARE_TOOL,
|
||||
BUILTIN_RUN_COMPARE_TOOL_DEFINITION,
|
||||
BuiltInRunCompareToolAdapter,
|
||||
BuiltInRunCompareToolUnavailableError,
|
||||
InvalidBuiltInRunCompareToolError,
|
||||
createBuiltInRunCompareToolHandlerBinding,
|
||||
executeBuiltInRunCompareTool,
|
||||
} = require('../dist/tool-execution/builtin-run-compare/builtInRunCompareTool');
|
||||
const {
|
||||
createPluginPackageResourceGenerationFromReferences,
|
||||
} = require('../dist/plugin-package/pluginPackageResourceGeneration');
|
||||
const {
|
||||
createProjectToolDefinitionSnapshot,
|
||||
projectToolDefinitionRegistry,
|
||||
} = require('../dist/tool-execution/tool-registry/projectToolDefinitionSnapshot');
|
||||
|
||||
const DIGEST_A = 'a'.repeat(64);
|
||||
const DIGEST_B = 'b'.repeat(64);
|
||||
const DIGEST_C = 'c'.repeat(64);
|
||||
|
||||
function run(id, overrides = {}) {
|
||||
return {
|
||||
id,
|
||||
projectId: 'project-compare',
|
||||
taskId: 'task-backup',
|
||||
taskRevision: 'task-backup@4',
|
||||
triggerType: 'schedule',
|
||||
executionOrigin: 'scheduled_system',
|
||||
executionOwner: 'runtime',
|
||||
status: 'succeeded',
|
||||
version: 4,
|
||||
eventSequence: 8,
|
||||
priority: 10,
|
||||
createdAtMs: 1_000,
|
||||
queuedAtMs: 1_020,
|
||||
startedAtMs: 1_050,
|
||||
finishedAtMs: 1_150,
|
||||
requestId: 'must-not-cross-tool-output',
|
||||
inputRef: 'artifact:must-not-cross-tool-output',
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function repository(records, calls = []) {
|
||||
return {
|
||||
async findRunById(runId) {
|
||||
calls.push(runId);
|
||||
return records.get(runId) ?? null;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function snapshot(definition = BUILTIN_RUN_COMPARE_TOOL_DEFINITION) {
|
||||
const generation = createPluginPackageResourceGenerationFromReferences({
|
||||
installationId: 'install-qinglong-compare',
|
||||
projectId: 'project-compare',
|
||||
packageName: 'qinglong',
|
||||
lockDigest: DIGEST_A,
|
||||
generation: 1,
|
||||
previousActiveLockDigest: null,
|
||||
contentDigest: DIGEST_B,
|
||||
resources: [],
|
||||
});
|
||||
return createProjectToolDefinitionSnapshot({
|
||||
projectId: 'project-compare',
|
||||
contributions: [
|
||||
{
|
||||
generation,
|
||||
revisionDigest: DIGEST_C,
|
||||
definitions: [definition],
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
test('compares two ordered Project-scoped Run point reads with bounded deltas', async () => {
|
||||
const calls = [];
|
||||
const baseline = run('run-success');
|
||||
const candidate = run('run-failure', {
|
||||
taskRevision: 'task-backup@5',
|
||||
status: 'failed',
|
||||
version: 6,
|
||||
eventSequence: 12,
|
||||
createdAtMs: 2_000,
|
||||
queuedAtMs: 2_040,
|
||||
startedAtMs: 2_100,
|
||||
finishedAtMs: 2_350,
|
||||
requestId: 'must-also-not-cross-tool-output',
|
||||
});
|
||||
const output = await executeBuiltInRunCompareTool(
|
||||
repository(
|
||||
new Map([
|
||||
[baseline.id, baseline],
|
||||
[candidate.id, candidate],
|
||||
]),
|
||||
calls,
|
||||
),
|
||||
'project-compare',
|
||||
{ baselineRunId: baseline.id, candidateRunId: candidate.id },
|
||||
);
|
||||
|
||||
assert.deepEqual(calls, ['run-success', 'run-failure']);
|
||||
assert.equal(output.comparable, true);
|
||||
assert.equal(output.sameTask, true);
|
||||
assert.equal(output.sameTaskRevision, false);
|
||||
assert.deepEqual(output.changedFields, ['taskRevision', 'status']);
|
||||
assert.equal(Object.isFrozen(output.changedFields), true);
|
||||
assert.equal(output.queueDelayDeltaMs, 20);
|
||||
assert.equal(output.executionDurationDeltaMs, 150);
|
||||
assert.equal(output.totalDurationDeltaMs, 200);
|
||||
assert.equal(output.consistency, 'ordered_independent_point_reads');
|
||||
assert.equal(output.baseline.requestId, undefined);
|
||||
assert.equal(output.candidate.inputRef, undefined);
|
||||
|
||||
const registry = projectToolDefinitionRegistry(snapshot());
|
||||
assert.deepEqual(
|
||||
registry.normalizeOutput(
|
||||
BUILTIN_RUN_COMPARE_TOOL.name,
|
||||
BUILTIN_RUN_COMPARE_TOOL.version,
|
||||
output,
|
||||
),
|
||||
output,
|
||||
);
|
||||
});
|
||||
|
||||
test('does not compare an absent or cross-Project Run and omits duration claims', async () => {
|
||||
const output = await executeBuiltInRunCompareTool(
|
||||
repository(
|
||||
new Map([
|
||||
['run-visible', run('run-visible')],
|
||||
[
|
||||
'run-foreign',
|
||||
run('run-foreign', { projectId: 'project-someone-else' }),
|
||||
],
|
||||
]),
|
||||
),
|
||||
'project-compare',
|
||||
{ baselineRunId: 'run-visible', candidateRunId: 'run-foreign' },
|
||||
);
|
||||
|
||||
assert.equal(output.baseline.found, true);
|
||||
assert.deepEqual(output.candidate, { found: false });
|
||||
assert.equal(output.comparable, false);
|
||||
assert.equal(output.sameTask, false);
|
||||
assert.equal(output.sameTaskRevision, false);
|
||||
assert.deepEqual(output.changedFields, []);
|
||||
assert.equal(output.queueDelayDeltaMs, undefined);
|
||||
assert.equal(output.executionDurationDeltaMs, undefined);
|
||||
assert.equal(output.totalDurationDeltaMs, undefined);
|
||||
|
||||
const invalidTimeline = await executeBuiltInRunCompareTool(
|
||||
repository(
|
||||
new Map([
|
||||
['run-baseline', run('run-baseline')],
|
||||
[
|
||||
'run-reversed',
|
||||
run('run-reversed', { startedAtMs: 3_000, finishedAtMs: 2_900 }),
|
||||
],
|
||||
]),
|
||||
),
|
||||
'project-compare',
|
||||
{ baselineRunId: 'run-baseline', candidateRunId: 'run-reversed' },
|
||||
);
|
||||
assert.equal(invalidTimeline.comparable, true);
|
||||
assert.equal(invalidTimeline.executionDurationDeltaMs, undefined);
|
||||
});
|
||||
|
||||
test('rejects aliases, unknown input, malformed records, and repository failure', async () => {
|
||||
const runs = repository(new Map());
|
||||
await assert.rejects(
|
||||
executeBuiltInRunCompareTool(runs, 'project-compare', {
|
||||
baselineRunId: 'same-run',
|
||||
candidateRunId: 'same-run',
|
||||
}),
|
||||
InvalidBuiltInRunCompareToolError,
|
||||
);
|
||||
await assert.rejects(
|
||||
executeBuiltInRunCompareTool(runs, 'project-compare', {
|
||||
baselineRunId: 'run-a',
|
||||
candidateRunId: 'run-b',
|
||||
injected: true,
|
||||
}),
|
||||
InvalidBuiltInRunCompareToolError,
|
||||
);
|
||||
await assert.rejects(
|
||||
executeBuiltInRunCompareTool(
|
||||
repository(
|
||||
new Map([
|
||||
['run-a', run('run-a')],
|
||||
['run-b', run('run-b', { version: -1 })],
|
||||
]),
|
||||
),
|
||||
'project-compare',
|
||||
{ baselineRunId: 'run-a', candidateRunId: 'run-b' },
|
||||
),
|
||||
BuiltInRunCompareToolUnavailableError,
|
||||
);
|
||||
await assert.rejects(
|
||||
executeBuiltInRunCompareTool(
|
||||
{
|
||||
async findRunById() {
|
||||
throw new Error('database DSN must not escape');
|
||||
},
|
||||
},
|
||||
'project-compare',
|
||||
{ baselineRunId: 'run-a', candidateRunId: 'run-b' },
|
||||
),
|
||||
BuiltInRunCompareToolUnavailableError,
|
||||
);
|
||||
});
|
||||
|
||||
test('binds the exact reviewed definition to a retry-safe database-read adapter', async () => {
|
||||
const currentSnapshot = snapshot();
|
||||
const binding = createBuiltInRunCompareToolHandlerBinding(currentSnapshot, [
|
||||
'edge',
|
||||
'standalone',
|
||||
'cluster-control',
|
||||
]);
|
||||
assert.deepEqual(binding.tool, BUILTIN_RUN_COMPARE_TOOL);
|
||||
assert.deepEqual(binding.adapter, BUILTIN_RUN_COMPARE_ADAPTER);
|
||||
assert.deepEqual(binding.authorities, ['database.read']);
|
||||
|
||||
const definitions = projectToolDefinitionRegistry(currentSnapshot);
|
||||
const adapter = new BuiltInRunCompareToolAdapter(
|
||||
binding,
|
||||
'edge',
|
||||
definitions,
|
||||
repository(
|
||||
new Map([
|
||||
['run-a', run('run-a')],
|
||||
['run-b', run('run-b', { status: 'failed' })],
|
||||
]),
|
||||
),
|
||||
);
|
||||
assert.equal(adapter.recoveryMode, 'retry_safe_read');
|
||||
const output = await adapter.execute(
|
||||
{ projectId: 'project-compare' },
|
||||
{ baselineRunId: 'run-a', candidateRunId: 'run-b' },
|
||||
);
|
||||
assert.deepEqual(output.changedFields, ['status']);
|
||||
|
||||
const changed = {
|
||||
...BUILTIN_RUN_COMPARE_TOOL_DEFINITION,
|
||||
description: 'unreviewed changed definition',
|
||||
};
|
||||
assert.throws(
|
||||
() =>
|
||||
createBuiltInRunCompareToolHandlerBinding(snapshot(changed), ['edge']),
|
||||
InvalidBuiltInRunCompareToolError,
|
||||
);
|
||||
assert.throws(
|
||||
() =>
|
||||
new BuiltInRunCompareToolAdapter(
|
||||
{ ...binding, authorities: ['database.read', 'network.client'] },
|
||||
'edge',
|
||||
definitions,
|
||||
repository(new Map()),
|
||||
),
|
||||
/handler authority is invalid/,
|
||||
);
|
||||
});
|
||||
|
||||
test('publishes only explicit compare subpaths and keeps runtime-core root unchanged', () => {
|
||||
const tool = require('@qinglong/runtime-core/builtin-run-compare-tool');
|
||||
const projection = require('@qinglong/runtime-core/builtin-run-compare-projection');
|
||||
const root = require('@qinglong/runtime-core');
|
||||
|
||||
assert.equal(tool.BUILTIN_RUN_COMPARE_TOOL.name, 'qinglong.run.compare');
|
||||
assert.equal(
|
||||
projection.BUILTIN_RUN_COMPARE_TOOL.name,
|
||||
'qinglong.run.compare',
|
||||
);
|
||||
assert.equal(root.BUILTIN_RUN_COMPARE_TOOL, undefined);
|
||||
assert.equal(root.executeBuiltInRunCompareTool, undefined);
|
||||
});
|
||||
Reference in New Issue
Block a user