mirror of
https://github.com/whyour/qinglong.git
synced 2026-09-22 02:27:44 +08:00
feat(ql3): add bounded trusted run comparison
This commit is contained in:
+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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user