mirror of
https://github.com/whyour/qinglong.git
synced 2026-09-20 16:07:11 +08:00
feat(ql3): compare bounded task run outcomes
This commit is contained in:
@@ -75,6 +75,11 @@
|
||||
"require": "./dist/run-management/runManualRetryRepository.js",
|
||||
"default": "./dist/run-management/runManualRetryRepository.js"
|
||||
},
|
||||
"./task-run-outcome-window": {
|
||||
"types": "./dist/run/outcome-comparison/taskRunOutcomeWindowReader.d.ts",
|
||||
"require": "./dist/run/outcome-comparison/taskRunOutcomeWindowReader.js",
|
||||
"default": "./dist/run/outcome-comparison/taskRunOutcomeWindowReader.js"
|
||||
},
|
||||
"./run-manager": {
|
||||
"types": "./dist/entrypoints/runManager.d.ts",
|
||||
"require": "./dist/entrypoints/runManager.js",
|
||||
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
import { RUN_STATUSES, type RunStatus } from '@qinglong/runtime-core/run';
|
||||
import {
|
||||
RunRepositoryConstraintError,
|
||||
RunRepositoryOperationError,
|
||||
} from '@qinglong/runtime-core/run-repository';
|
||||
import {
|
||||
normalizeTaskRunOutcomeWindowQuery,
|
||||
normalizeTaskRunOutcomeWindowRecord,
|
||||
type TaskRunOutcomeWindowQuery,
|
||||
type TaskRunOutcomeWindowReader,
|
||||
type TaskRunOutcomeWindowRecord,
|
||||
} from '@qinglong/runtime-core/task-run-outcome-window';
|
||||
import type { PostgresQueryable } from '@qinglong/runtime-core';
|
||||
|
||||
type QueryRow = Readonly<Record<string, unknown>>;
|
||||
|
||||
function requiredString(row: QueryRow, property: string): string {
|
||||
const value = row[property];
|
||||
if (typeof value !== 'string' || value.length === 0) {
|
||||
throw new RunRepositoryConstraintError(
|
||||
`PostgreSQL Task Run outcome row has an invalid ${property}`,
|
||||
);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function requiredInteger(row: QueryRow, property: string): number {
|
||||
const value = row[property];
|
||||
if (typeof value === 'number' && Number.isSafeInteger(value)) return value;
|
||||
if (typeof value === 'string' && /^(0|[1-9]\d*)$/.test(value)) {
|
||||
const parsed = Number(value);
|
||||
if (Number.isSafeInteger(parsed)) return parsed;
|
||||
}
|
||||
throw new RunRepositoryConstraintError(
|
||||
`PostgreSQL Task Run outcome row has an invalid ${property}`,
|
||||
);
|
||||
}
|
||||
|
||||
function record(row: QueryRow): Readonly<TaskRunOutcomeWindowRecord> {
|
||||
const status = requiredString(row, 'status');
|
||||
if (!RUN_STATUSES.includes(status as RunStatus)) {
|
||||
throw new RunRepositoryConstraintError(
|
||||
'PostgreSQL Task Run outcome row has an invalid status',
|
||||
);
|
||||
}
|
||||
return normalizeTaskRunOutcomeWindowRecord({
|
||||
id: requiredString(row, 'id'),
|
||||
projectId: requiredString(row, 'projectId'),
|
||||
taskId: requiredString(row, 'taskId'),
|
||||
status: status as RunStatus,
|
||||
createdAtMs: requiredInteger(row, 'createdAtMs'),
|
||||
});
|
||||
}
|
||||
|
||||
export class PostgresTaskRunOutcomeWindowReader
|
||||
implements TaskRunOutcomeWindowReader
|
||||
{
|
||||
constructor(private readonly queryable: PostgresQueryable) {}
|
||||
|
||||
async listRecentRunsByTask(
|
||||
value: Readonly<TaskRunOutcomeWindowQuery>,
|
||||
): Promise<readonly Readonly<TaskRunOutcomeWindowRecord>[]> {
|
||||
const query = normalizeTaskRunOutcomeWindowQuery(value);
|
||||
try {
|
||||
const result = await this.queryable.query<QueryRow>(
|
||||
`SELECT
|
||||
"id" AS "id",
|
||||
"project_id" AS "projectId",
|
||||
"task_id" AS "taskId",
|
||||
"status" AS "status",
|
||||
"created_at_ms" AS "createdAtMs"
|
||||
FROM "ql3"."runs"
|
||||
WHERE "project_id" = $1 AND "task_id" = $2
|
||||
ORDER BY "created_at_ms" DESC, "id" DESC
|
||||
LIMIT $3`,
|
||||
[query.projectId, query.taskId, query.limit],
|
||||
);
|
||||
return Object.freeze(result.rows.map(record));
|
||||
} catch (error) {
|
||||
if (error instanceof RunRepositoryConstraintError) throw error;
|
||||
throw new RunRepositoryOperationError(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
const assert = require('node:assert/strict');
|
||||
const { test } = require('node:test');
|
||||
|
||||
const {
|
||||
RunRepositoryConstraintError,
|
||||
RunRepositoryOperationError,
|
||||
} = require('@qinglong/runtime-core/run-repository');
|
||||
const {
|
||||
PostgresTaskRunOutcomeWindowReader,
|
||||
} = require('@qinglong/cluster-postgres/task-run-outcome-window');
|
||||
|
||||
function harness(rows) {
|
||||
const queries = [];
|
||||
return {
|
||||
queries,
|
||||
queryable: {
|
||||
async query(text, values) {
|
||||
queries.push({ text, values });
|
||||
return { rows, rowCount: rows.length };
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
test('reads one bounded PostgreSQL Project Task window with bigint normalization', async () => {
|
||||
assert.equal(
|
||||
require('@qinglong/cluster-postgres').PostgresTaskRunOutcomeWindowReader,
|
||||
undefined,
|
||||
);
|
||||
const value = harness([
|
||||
{
|
||||
id: 'run-failed',
|
||||
projectId: 'project-a',
|
||||
taskId: 'task-a',
|
||||
status: 'failed',
|
||||
createdAtMs: '2000',
|
||||
},
|
||||
{
|
||||
id: 'run-succeeded',
|
||||
projectId: 'project-a',
|
||||
taskId: 'task-a',
|
||||
status: 'succeeded',
|
||||
createdAtMs: '1000',
|
||||
},
|
||||
]);
|
||||
const rows = await new PostgresTaskRunOutcomeWindowReader(
|
||||
value.queryable,
|
||||
).listRecentRunsByTask({
|
||||
projectId: 'project-a',
|
||||
taskId: 'task-a',
|
||||
limit: 65,
|
||||
});
|
||||
|
||||
assert.deepEqual(rows, [
|
||||
{
|
||||
id: 'run-failed',
|
||||
projectId: 'project-a',
|
||||
taskId: 'task-a',
|
||||
status: 'failed',
|
||||
createdAtMs: 2000,
|
||||
},
|
||||
{
|
||||
id: 'run-succeeded',
|
||||
projectId: 'project-a',
|
||||
taskId: 'task-a',
|
||||
status: 'succeeded',
|
||||
createdAtMs: 1000,
|
||||
},
|
||||
]);
|
||||
assert.deepEqual(value.queries[0].values, ['project-a', 'task-a', 65]);
|
||||
assert.match(
|
||||
value.queries[0].text,
|
||||
/WHERE "project_id" = \$1 AND "task_id" = \$2[\s\S]*ORDER BY "created_at_ms" DESC, "id" DESC[\s\S]*LIMIT \$3/u,
|
||||
);
|
||||
});
|
||||
|
||||
test('rejects invalid windows and maps driver failures without leaking details', async () => {
|
||||
const invalid = harness([
|
||||
{
|
||||
id: 'run-invalid',
|
||||
projectId: 'project-a',
|
||||
taskId: 'task-a',
|
||||
status: 'invented',
|
||||
createdAtMs: '1000',
|
||||
},
|
||||
]);
|
||||
await assert.rejects(
|
||||
new PostgresTaskRunOutcomeWindowReader(
|
||||
invalid.queryable,
|
||||
).listRecentRunsByTask({
|
||||
projectId: 'project-a',
|
||||
taskId: 'task-a',
|
||||
limit: 65,
|
||||
}),
|
||||
RunRepositoryConstraintError,
|
||||
);
|
||||
|
||||
await assert.rejects(
|
||||
new PostgresTaskRunOutcomeWindowReader({
|
||||
async query() {
|
||||
throw new Error('postgresql://private-host/secret');
|
||||
},
|
||||
}).listRecentRunsByTask({
|
||||
projectId: 'project-a',
|
||||
taskId: 'task-a',
|
||||
limit: 65,
|
||||
}),
|
||||
(error) => {
|
||||
assert.ok(error instanceof RunRepositoryOperationError);
|
||||
assert.equal(error.message, 'Run repository operation failed');
|
||||
return true;
|
||||
},
|
||||
);
|
||||
});
|
||||
@@ -50,6 +50,11 @@ import {
|
||||
BUILTIN_RUN_COMPARE_TOOL_DEFINITION,
|
||||
executeBuiltInRunCompareTool,
|
||||
} from '@qinglong/runtime-core/builtin-run-compare-projection';
|
||||
import {
|
||||
BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL,
|
||||
BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL_DEFINITION,
|
||||
executeBuiltInTaskRunOutcomeCompareTool,
|
||||
} from '@qinglong/runtime-core/builtin-task-run-outcome-compare-projection';
|
||||
import {
|
||||
BUILTIN_RUN_READ_TOOL,
|
||||
BUILTIN_RUN_READ_TOOL_DEFINITION,
|
||||
@@ -58,6 +63,7 @@ import {
|
||||
import type { RunRepositoryReader } from '@qinglong/runtime-core/run-repository';
|
||||
import type { StepRunRepository } from '@qinglong/runtime-core/step-run';
|
||||
import type { ProjectRunListReader } from '@qinglong/runtime-core/project-run-list';
|
||||
import type { TaskRunOutcomeWindowReader } from '@qinglong/runtime-core/task-run-outcome-window';
|
||||
import type { SecurityPrincipal } from '@qinglong/runtime-core/security';
|
||||
import type { TaskDefinitionSource } from '@qinglong/runtime-core/task-definition';
|
||||
import type { TriggerSource } from '@qinglong/runtime-core/trigger';
|
||||
@@ -112,7 +118,8 @@ type LocalMcpRunReader = Pick<
|
||||
RunRepositoryReader,
|
||||
'findRunById' | 'listEvents'
|
||||
> &
|
||||
ProjectRunListReader;
|
||||
ProjectRunListReader &
|
||||
TaskRunOutcomeWindowReader;
|
||||
|
||||
type LocalMcpTaskReader = Pick<
|
||||
TaskDefinitionSource,
|
||||
@@ -194,6 +201,24 @@ const LOCAL_MCP_READ_TOOLS: readonly LocalMcpReadToolDescriptor[] =
|
||||
input: ToolJsonValue,
|
||||
) => executeBuiltInRunCompareTool(authority.runs, projectId, input),
|
||||
}),
|
||||
Object.freeze({
|
||||
tool: BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL,
|
||||
definition: BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL_DEFINITION,
|
||||
title: 'Compare Latest QingLong Task Run Outcomes',
|
||||
auditReason: 'tool_qinglong_task_runs_compare',
|
||||
unavailableCode: 'task_run_outcome_compare_unavailable',
|
||||
execute: (
|
||||
authority: LocalMcpReadAuthority,
|
||||
projectId: string,
|
||||
input: ToolJsonValue,
|
||||
) =>
|
||||
executeBuiltInTaskRunOutcomeCompareTool(
|
||||
authority.runs,
|
||||
authority.runs,
|
||||
projectId,
|
||||
input,
|
||||
),
|
||||
}),
|
||||
Object.freeze({
|
||||
tool: BUILTIN_RUN_EVENT_LIST_TOOL,
|
||||
definition: BUILTIN_RUN_EVENT_LIST_TOOL_DEFINITION,
|
||||
|
||||
@@ -165,6 +165,7 @@ function fixture(options = {}) {
|
||||
const audits = [];
|
||||
let reads = 0;
|
||||
let listReads = 0;
|
||||
let outcomeWindowReads = 0;
|
||||
let eventReads = 0;
|
||||
let taskListReads = 0;
|
||||
let triggerListReads = 0;
|
||||
@@ -181,6 +182,15 @@ function fixture(options = {}) {
|
||||
startedAtMs: 24,
|
||||
finishedAtMs: 30,
|
||||
});
|
||||
const failedRun = Object.freeze({
|
||||
...candidateRun,
|
||||
id: 'run-failed',
|
||||
status: 'failed',
|
||||
createdAtMs: 30,
|
||||
queuedAtMs: 31,
|
||||
startedAtMs: 34,
|
||||
finishedAtMs: 40,
|
||||
});
|
||||
const server = createQingLongLocalMcpServer({
|
||||
projectId: 'default',
|
||||
now: () => NOW,
|
||||
@@ -222,11 +232,37 @@ function fixture(options = {}) {
|
||||
const values = [candidateRun, run()];
|
||||
return values.slice(0, query.limit);
|
||||
},
|
||||
async listRecentRunsByTask(query) {
|
||||
events.push('read-outcome-window');
|
||||
outcomeWindowReads += 1;
|
||||
assert.deepEqual(query, {
|
||||
projectId: 'default',
|
||||
taskId: 'task-1',
|
||||
limit: 65,
|
||||
});
|
||||
return [
|
||||
{
|
||||
id: failedRun.id,
|
||||
projectId: failedRun.projectId,
|
||||
taskId: failedRun.taskId,
|
||||
status: failedRun.status,
|
||||
createdAtMs: failedRun.createdAtMs,
|
||||
},
|
||||
{
|
||||
id: 'run-1',
|
||||
projectId: 'default',
|
||||
taskId: 'task-1',
|
||||
status: 'succeeded',
|
||||
createdAtMs: 10,
|
||||
},
|
||||
];
|
||||
},
|
||||
async findRunById(runId) {
|
||||
events.push('read');
|
||||
reads += 1;
|
||||
if (runId === 'run-1') return run(options.runProjectId);
|
||||
return runId === 'run-2' ? candidateRun : null;
|
||||
if (runId === 'run-2') return candidateRun;
|
||||
return runId === 'run-failed' ? failedRun : null;
|
||||
},
|
||||
async listEvents(runId, query) {
|
||||
events.push('read-events');
|
||||
@@ -328,6 +364,7 @@ function fixture(options = {}) {
|
||||
counters: () => ({
|
||||
reads,
|
||||
listReads,
|
||||
outcomeWindowReads,
|
||||
eventReads,
|
||||
taskListReads,
|
||||
triggerListReads,
|
||||
@@ -393,6 +430,7 @@ test('advertises bounded read-only Run Tools and executes auth -> Policy -> Audi
|
||||
'qinglong.run.list',
|
||||
'qinglong.run.get',
|
||||
'qinglong.run.compare',
|
||||
'qinglong.task.runs.compare',
|
||||
'qinglong.run.events.list',
|
||||
'qinglong.run.steps.list',
|
||||
'qinglong.task.get',
|
||||
@@ -451,6 +489,7 @@ test('advertises bounded read-only Run Tools and executes auth -> Policy -> Audi
|
||||
assert.deepEqual(value.counters(), {
|
||||
reads: 1,
|
||||
listReads: 0,
|
||||
outcomeWindowReads: 0,
|
||||
eventReads: 0,
|
||||
taskListReads: 0,
|
||||
triggerListReads: 0,
|
||||
@@ -533,6 +572,98 @@ test('compares two Project Runs through the same fenced admission', async (t) =>
|
||||
assert.deepEqual(value.counters(), {
|
||||
reads: 2,
|
||||
listReads: 0,
|
||||
outcomeWindowReads: 0,
|
||||
eventReads: 0,
|
||||
taskListReads: 0,
|
||||
triggerListReads: 0,
|
||||
approvalListReads: 0,
|
||||
approvalDetailReads: 0,
|
||||
confirmations: 1,
|
||||
});
|
||||
});
|
||||
|
||||
test('selects and compares latest Task outcomes through the same fenced admission', async (t) => {
|
||||
const value = fixture();
|
||||
const connected = await client(value.server, t);
|
||||
const response = await connected.request('tools/call', {
|
||||
name: 'qinglong.task.runs.compare',
|
||||
arguments: { taskId: 'task-1' },
|
||||
});
|
||||
assert.equal(response.result.isError, undefined);
|
||||
assert.deepEqual(response.result.structuredContent, {
|
||||
taskId: 'task-1',
|
||||
baselineOutcome: 'succeeded',
|
||||
candidateOutcome: 'failed',
|
||||
baseline: {
|
||||
found: true,
|
||||
id: 'run-1',
|
||||
taskId: 'task-1',
|
||||
taskRevision: 'revision-1',
|
||||
status: 'succeeded',
|
||||
version: 3,
|
||||
eventSequence: 4,
|
||||
priority: 0,
|
||||
executionOrigin: 'manual',
|
||||
executionOwner: 'runtime',
|
||||
createdAtMs: 10,
|
||||
queuedAtMs: 11,
|
||||
startedAtMs: 12,
|
||||
finishedAtMs: 13,
|
||||
},
|
||||
candidate: {
|
||||
found: true,
|
||||
id: 'run-failed',
|
||||
taskId: 'task-1',
|
||||
taskRevision: 'revision-2',
|
||||
status: 'failed',
|
||||
version: 3,
|
||||
eventSequence: 4,
|
||||
priority: 1,
|
||||
executionOrigin: 'manual',
|
||||
executionOwner: 'runtime',
|
||||
createdAtMs: 30,
|
||||
queuedAtMs: 31,
|
||||
startedAtMs: 34,
|
||||
finishedAtMs: 40,
|
||||
},
|
||||
comparable: true,
|
||||
sameTask: true,
|
||||
sameTaskRevision: false,
|
||||
changedFields: ['taskRevision', 'status', 'priority'],
|
||||
queueDelayDeltaMs: 0,
|
||||
executionDurationDeltaMs: 5,
|
||||
totalDurationDeltaMs: 7,
|
||||
consistency: 'bounded_task_window_then_ordered_point_reads',
|
||||
selection: {
|
||||
windowLimit: 64,
|
||||
searchedRunCount: 2,
|
||||
hasOlderRuns: false,
|
||||
complete: true,
|
||||
order: 'created_at_desc_id_desc',
|
||||
},
|
||||
});
|
||||
assert.deepEqual(value.permissions, [
|
||||
'tool.call:qinglong.task.runs.compare',
|
||||
'run.read',
|
||||
]);
|
||||
assert.deepEqual(value.events, [
|
||||
'authenticate',
|
||||
'policy:tool.call:qinglong.task.runs.compare',
|
||||
'policy:run.read',
|
||||
'audit:allowed',
|
||||
'confirm',
|
||||
'read-outcome-window',
|
||||
'read',
|
||||
'read',
|
||||
]);
|
||||
assert.deepEqual(value.audits[0].reasons, [
|
||||
'tool_invocation_allowed',
|
||||
'tool_qinglong_task_runs_compare',
|
||||
]);
|
||||
assert.deepEqual(value.counters(), {
|
||||
reads: 2,
|
||||
listReads: 0,
|
||||
outcomeWindowReads: 1,
|
||||
eventReads: 0,
|
||||
taskListReads: 0,
|
||||
triggerListReads: 0,
|
||||
@@ -590,6 +721,7 @@ test('discovers recent Project Runs through the same fenced admission', async (t
|
||||
assert.deepEqual(value.counters(), {
|
||||
reads: 0,
|
||||
listReads: 1,
|
||||
outcomeWindowReads: 0,
|
||||
eventReads: 0,
|
||||
taskListReads: 0,
|
||||
triggerListReads: 0,
|
||||
@@ -642,6 +774,7 @@ test('lists a payload-free Run event page through the same fenced admission', as
|
||||
assert.deepEqual(value.counters(), {
|
||||
reads: 1,
|
||||
listReads: 0,
|
||||
outcomeWindowReads: 0,
|
||||
eventReads: 1,
|
||||
taskListReads: 0,
|
||||
triggerListReads: 0,
|
||||
@@ -694,6 +827,7 @@ test('discovers low-sensitive Tasks through task.read admission', async (t) => {
|
||||
assert.deepEqual(value.counters(), {
|
||||
reads: 0,
|
||||
listReads: 0,
|
||||
outcomeWindowReads: 0,
|
||||
eventReads: 0,
|
||||
taskListReads: 1,
|
||||
triggerListReads: 0,
|
||||
@@ -743,6 +877,7 @@ test('reads one current Task fence through task.read admission', async (t) => {
|
||||
assert.deepEqual(value.counters(), {
|
||||
reads: 0,
|
||||
listReads: 0,
|
||||
outcomeWindowReads: 0,
|
||||
eventReads: 0,
|
||||
taskListReads: 1,
|
||||
triggerListReads: 0,
|
||||
@@ -803,6 +938,7 @@ test('discovers low-sensitive Triggers through trigger.read admission', async (t
|
||||
assert.deepEqual(value.counters(), {
|
||||
reads: 0,
|
||||
listReads: 0,
|
||||
outcomeWindowReads: 0,
|
||||
eventReads: 0,
|
||||
taskListReads: 0,
|
||||
triggerListReads: 1,
|
||||
@@ -859,6 +995,7 @@ test('discovers low-sensitive Approvals through approval.read admission', async
|
||||
assert.deepEqual(value.counters(), {
|
||||
reads: 0,
|
||||
listReads: 0,
|
||||
outcomeWindowReads: 0,
|
||||
eventReads: 0,
|
||||
taskListReads: 0,
|
||||
triggerListReads: 0,
|
||||
@@ -930,6 +1067,7 @@ test('reads one redacted Approval preview through approval.read and artifact.rea
|
||||
assert.deepEqual(value.counters(), {
|
||||
reads: 0,
|
||||
listReads: 0,
|
||||
outcomeWindowReads: 0,
|
||||
eventReads: 0,
|
||||
taskListReads: 0,
|
||||
triggerListReads: 0,
|
||||
|
||||
@@ -90,6 +90,44 @@ async function fixture(t) {
|
||||
priority: 1,
|
||||
createdAtMs: NOW - 3_000,
|
||||
});
|
||||
await transaction.insertRun({
|
||||
id: 'run-mcp-e2e-success',
|
||||
projectId: 'default',
|
||||
taskId: 'task-mcp',
|
||||
taskRevision: 'revision-3',
|
||||
taskName: 'MCP successful baseline',
|
||||
triggerType: 'manual',
|
||||
executionOrigin: 'manual',
|
||||
executionOwner: 'runtime',
|
||||
triggeredBy: 'user:mcp-owner',
|
||||
status: 'succeeded',
|
||||
version: 0,
|
||||
eventSequence: 0,
|
||||
priority: 0,
|
||||
createdAtMs: NOW - 4_000,
|
||||
queuedAtMs: NOW - 3_900,
|
||||
startedAtMs: NOW - 3_800,
|
||||
finishedAtMs: NOW - 3_600,
|
||||
});
|
||||
await transaction.insertRun({
|
||||
id: 'run-mcp-e2e-failure',
|
||||
projectId: 'default',
|
||||
taskId: 'task-mcp',
|
||||
taskRevision: 'revision-4',
|
||||
taskName: 'MCP failed candidate',
|
||||
triggerType: 'manual',
|
||||
executionOrigin: 'manual',
|
||||
executionOwner: 'runtime',
|
||||
triggeredBy: 'user:mcp-owner',
|
||||
status: 'failed',
|
||||
version: 0,
|
||||
eventSequence: 0,
|
||||
priority: 2,
|
||||
createdAtMs: NOW - 5_000,
|
||||
queuedAtMs: NOW - 4_850,
|
||||
startedAtMs: NOW - 4_700,
|
||||
finishedAtMs: NOW - 4_300,
|
||||
});
|
||||
await transaction.appendEvent({
|
||||
id: 'mcp-e2e-event-1',
|
||||
runId: 'run-mcp-e2e',
|
||||
@@ -473,6 +511,7 @@ test('serves the authenticated Run Tool over the real stdio protocol and persist
|
||||
'qinglong.run.list',
|
||||
'qinglong.run.get',
|
||||
'qinglong.run.compare',
|
||||
'qinglong.task.runs.compare',
|
||||
'qinglong.run.events.list',
|
||||
'qinglong.run.steps.list',
|
||||
'qinglong.task.get',
|
||||
@@ -684,6 +723,67 @@ test('serves the authenticated Run Tool over the real stdio protocol and persist
|
||||
changedFields: ['taskRevision', 'priority'],
|
||||
consistency: 'ordered_independent_point_reads',
|
||||
});
|
||||
const comparedOutcomes = await request('tools/call', {
|
||||
name: 'qinglong.task.runs.compare',
|
||||
arguments: { taskId: 'task-mcp' },
|
||||
});
|
||||
assert.equal(
|
||||
comparedOutcomes.result.isError,
|
||||
undefined,
|
||||
JSON.stringify(comparedOutcomes),
|
||||
);
|
||||
assert.deepEqual(comparedOutcomes.result.structuredContent, {
|
||||
taskId: 'task-mcp',
|
||||
baselineOutcome: 'succeeded',
|
||||
candidateOutcome: 'failed',
|
||||
baseline: {
|
||||
found: true,
|
||||
id: 'run-mcp-e2e-success',
|
||||
taskId: 'task-mcp',
|
||||
taskRevision: 'revision-3',
|
||||
status: 'succeeded',
|
||||
version: 0,
|
||||
eventSequence: 0,
|
||||
priority: 0,
|
||||
executionOrigin: 'manual',
|
||||
executionOwner: 'runtime',
|
||||
createdAtMs: NOW - 4_000,
|
||||
queuedAtMs: NOW - 3_900,
|
||||
startedAtMs: NOW - 3_800,
|
||||
finishedAtMs: NOW - 3_600,
|
||||
},
|
||||
candidate: {
|
||||
found: true,
|
||||
id: 'run-mcp-e2e-failure',
|
||||
taskId: 'task-mcp',
|
||||
taskRevision: 'revision-4',
|
||||
status: 'failed',
|
||||
version: 0,
|
||||
eventSequence: 0,
|
||||
priority: 2,
|
||||
executionOrigin: 'manual',
|
||||
executionOwner: 'runtime',
|
||||
createdAtMs: NOW - 5_000,
|
||||
queuedAtMs: NOW - 4_850,
|
||||
startedAtMs: NOW - 4_700,
|
||||
finishedAtMs: NOW - 4_300,
|
||||
},
|
||||
comparable: true,
|
||||
sameTask: true,
|
||||
sameTaskRevision: false,
|
||||
changedFields: ['taskRevision', 'status', 'priority'],
|
||||
queueDelayDeltaMs: 50,
|
||||
executionDurationDeltaMs: 200,
|
||||
totalDurationDeltaMs: 300,
|
||||
consistency: 'bounded_task_window_then_ordered_point_reads',
|
||||
selection: {
|
||||
windowLimit: 64,
|
||||
searchedRunCount: 4,
|
||||
hasOlderRuns: false,
|
||||
complete: true,
|
||||
order: 'created_at_desc_id_desc',
|
||||
},
|
||||
});
|
||||
const events = await request('tools/call', {
|
||||
name: 'qinglong.run.events.list',
|
||||
arguments: { runId: 'run-mcp-e2e', limit: 1 },
|
||||
@@ -765,6 +865,11 @@ test('serves the authenticated Run Tool over the real stdio protocol and persist
|
||||
outcome: 'allowed',
|
||||
subjectId: 'mcp-user',
|
||||
},
|
||||
{
|
||||
operationId: 'mcp.tool.call',
|
||||
outcome: 'allowed',
|
||||
subjectId: 'mcp-user',
|
||||
},
|
||||
],
|
||||
);
|
||||
} finally {
|
||||
|
||||
@@ -205,6 +205,11 @@
|
||||
"require": "./dist/tool-execution/localMcpReadDatabase.js",
|
||||
"default": "./dist/tool-execution/localMcpReadDatabase.js"
|
||||
},
|
||||
"./task-run-outcome-window": {
|
||||
"types": "./dist/run/outcome-comparison/taskRunOutcomeWindowReader.d.ts",
|
||||
"require": "./dist/run/outcome-comparison/taskRunOutcomeWindowReader.js",
|
||||
"default": "./dist/run/outcome-comparison/taskRunOutcomeWindowReader.js"
|
||||
},
|
||||
"./approved-action": {
|
||||
"types": "./dist/approved-action/approvalRequestRepository.d.ts",
|
||||
"require": "./dist/approved-action/approvalRequestRepository.js",
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
import { RUN_STATUSES } from '@qinglong/runtime-core/run';
|
||||
import {
|
||||
normalizeTaskRunOutcomeWindowQuery,
|
||||
type TaskRunOutcomeWindowQuery,
|
||||
type TaskRunOutcomeWindowReader,
|
||||
type TaskRunOutcomeWindowRecord,
|
||||
} from '@qinglong/runtime-core/task-run-outcome-window';
|
||||
import type { DatabaseSync } from 'node:sqlite';
|
||||
|
||||
import {
|
||||
queryRows,
|
||||
requiredEnum,
|
||||
requiredInteger,
|
||||
requiredString,
|
||||
} from '../runPersistence';
|
||||
|
||||
export class LocalSqliteTaskRunOutcomeWindowReader
|
||||
implements TaskRunOutcomeWindowReader
|
||||
{
|
||||
constructor(private readonly client: DatabaseSync) {}
|
||||
|
||||
async listRecentRunsByTask(
|
||||
value: Readonly<TaskRunOutcomeWindowQuery>,
|
||||
): Promise<readonly Readonly<TaskRunOutcomeWindowRecord>[]> {
|
||||
const query = normalizeTaskRunOutcomeWindowQuery(value);
|
||||
return queryRows(
|
||||
this.client,
|
||||
`SELECT
|
||||
"id" AS "id",
|
||||
"project_id" AS "projectId",
|
||||
"task_id" AS "taskId",
|
||||
"status" AS "status",
|
||||
"created_at_ms" AS "createdAtMs"
|
||||
FROM "Runs"
|
||||
WHERE "project_id" = ? AND "task_id" = ?
|
||||
ORDER BY "created_at_ms" DESC, "id" DESC
|
||||
LIMIT ?`,
|
||||
[query.projectId, query.taskId, query.limit],
|
||||
).map((row) =>
|
||||
Object.freeze({
|
||||
id: requiredString(row, 'id'),
|
||||
projectId: requiredString(row, 'projectId'),
|
||||
taskId: requiredString(row, 'taskId'),
|
||||
status: requiredEnum(row, 'status', RUN_STATUSES),
|
||||
createdAtMs: requiredInteger(row, 'createdAtMs'),
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,10 @@ import type {
|
||||
ProjectRunListQuery,
|
||||
ProjectRunListReader,
|
||||
} from '@qinglong/runtime-core/project-run-list';
|
||||
import type {
|
||||
TaskRunOutcomeWindowQuery,
|
||||
TaskRunOutcomeWindowReader,
|
||||
} from '@qinglong/runtime-core/task-run-outcome-window';
|
||||
import {
|
||||
RunRepositoryBusyError,
|
||||
RunRepositoryOperationError,
|
||||
@@ -26,6 +30,7 @@ import {
|
||||
type LocalSqliteReadinessEvidence,
|
||||
} from '../readiness/readiness';
|
||||
import { LocalSqliteRunReader } from '../run/runReader';
|
||||
import { LocalSqliteTaskRunOutcomeWindowReader } from '../run/outcome-comparison/taskRunOutcomeWindowReader';
|
||||
import { LocalSqliteStepRunRepository } from '../run/stepRunRepository';
|
||||
import {
|
||||
assertLocalSqliteOptions,
|
||||
@@ -44,7 +49,8 @@ export interface LocalSqliteMcpReadDatabase {
|
||||
readonly profile: LocalSqliteProfile;
|
||||
readonly readiness: LocalSqliteReadinessEvidence;
|
||||
readonly runs: Pick<RunRepositoryReader, 'findRunById' | 'listEvents'> &
|
||||
ProjectRunListReader;
|
||||
ProjectRunListReader &
|
||||
TaskRunOutcomeWindowReader;
|
||||
readonly stepRuns: Pick<StepRunRepository, 'listByRun'>;
|
||||
readonly taskDefinitions: Pick<
|
||||
TaskDefinitionSource,
|
||||
@@ -76,13 +82,17 @@ export async function openLocalSqliteMcpReadDatabase(
|
||||
const readiness = await auditLocalSqliteReadiness(client);
|
||||
const authority = new LocalSqliteOperationAuthority(client);
|
||||
const reader = new LocalSqliteRunReader(client);
|
||||
const outcomeWindowReader = new LocalSqliteTaskRunOutcomeWindowReader(
|
||||
client,
|
||||
);
|
||||
const stepRunRepository = new LocalSqliteStepRunRepository(authority);
|
||||
const taskRepository = new LocalSqliteTaskDefinitionRepository(authority);
|
||||
const triggerRepository = new LocalSqliteTriggerRepository(authority);
|
||||
const approvalSource = new LocalSqliteApprovalRequestSource(authority);
|
||||
const security = new LocalSqliteSecurityAuthorityStore(authority);
|
||||
const runs: Pick<RunRepositoryReader, 'findRunById' | 'listEvents'> &
|
||||
ProjectRunListReader = Object.freeze({
|
||||
ProjectRunListReader &
|
||||
TaskRunOutcomeWindowReader = Object.freeze({
|
||||
listRunsByProject(query: Readonly<ProjectRunListQuery>) {
|
||||
return authority.enqueue(
|
||||
() => reader.listRunsByProject(query),
|
||||
@@ -94,6 +104,17 @@ export async function openLocalSqliteMcpReadDatabase(
|
||||
),
|
||||
);
|
||||
},
|
||||
listRecentRunsByTask(query: Readonly<TaskRunOutcomeWindowQuery>) {
|
||||
return authority.enqueue(
|
||||
() => outcomeWindowReader.listRecentRunsByTask(query),
|
||||
(reason) =>
|
||||
reason === 'busy'
|
||||
? new RunRepositoryBusyError()
|
||||
: new RunRepositoryOperationError(
|
||||
new Error('Local SQLite MCP read database is closed'),
|
||||
),
|
||||
);
|
||||
},
|
||||
findRunById(runId: string) {
|
||||
return authority.enqueue(
|
||||
() => reader.findRunById(runId),
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const os = require('node:os');
|
||||
const path = require('node:path');
|
||||
const { DatabaseSync } = require('node:sqlite');
|
||||
const { test } = require('node:test');
|
||||
|
||||
const {
|
||||
migrateLocalSqlitePath,
|
||||
openLocalSqliteRuntimeDatabase,
|
||||
} = require('../dist');
|
||||
const {
|
||||
openLocalSqliteMcpReadDatabase,
|
||||
} = require('@qinglong/local-sqlite/mcp-read-database');
|
||||
const {
|
||||
LocalSqliteTaskRunOutcomeWindowReader,
|
||||
} = require('@qinglong/local-sqlite/task-run-outcome-window');
|
||||
|
||||
function run(id, projectId, taskId, status, createdAtMs) {
|
||||
return {
|
||||
id,
|
||||
projectId,
|
||||
taskId,
|
||||
taskRevision: 'revision-1',
|
||||
taskName: id,
|
||||
triggerType: 'manual',
|
||||
executionOrigin: 'manual',
|
||||
executionOwner: 'runtime',
|
||||
triggeredBy: 'test',
|
||||
status,
|
||||
version: 0,
|
||||
eventSequence: 0,
|
||||
priority: 0,
|
||||
createdAtMs,
|
||||
};
|
||||
}
|
||||
|
||||
test('reads one fixed Project Task outcome window through the existing indexed SQLite authority', async (t) => {
|
||||
assert.equal(typeof LocalSqliteTaskRunOutcomeWindowReader, 'function');
|
||||
assert.equal(
|
||||
require('@qinglong/local-sqlite').LocalSqliteTaskRunOutcomeWindowReader,
|
||||
undefined,
|
||||
);
|
||||
const directory = fs.mkdtempSync(
|
||||
path.join(os.tmpdir(), 'ql3-outcome-window-'),
|
||||
);
|
||||
t.after(() => fs.rmSync(directory, { recursive: true, force: true }));
|
||||
const options = {
|
||||
databasePath: path.join(directory, 'qinglong3.sqlite'),
|
||||
profile: 'edge',
|
||||
};
|
||||
await migrateLocalSqlitePath(options);
|
||||
const runtime = await openLocalSqliteRuntimeDatabase(options);
|
||||
await runtime.runRepository.transaction(async (transaction) => {
|
||||
for (const value of [
|
||||
run('run-old-success', 'default', 'task-a', 'succeeded', 10),
|
||||
run('run-failure', 'default', 'task-a', 'failed', 20),
|
||||
run('run-running', 'default', 'task-a', 'running', 30),
|
||||
run('run-other-task', 'default', 'task-b', 'failed', 40),
|
||||
run('run-other-project', 'other', 'task-a', 'failed', 50),
|
||||
]) {
|
||||
await transaction.insertRun(value);
|
||||
}
|
||||
});
|
||||
await runtime.close();
|
||||
|
||||
const database = await openLocalSqliteMcpReadDatabase(options);
|
||||
t.after(() => database.close());
|
||||
const rows = await database.runs.listRecentRunsByTask({
|
||||
projectId: 'default',
|
||||
taskId: 'task-a',
|
||||
limit: 3,
|
||||
});
|
||||
assert.deepEqual(rows, [
|
||||
{
|
||||
id: 'run-running',
|
||||
projectId: 'default',
|
||||
taskId: 'task-a',
|
||||
status: 'running',
|
||||
createdAtMs: 30,
|
||||
},
|
||||
{
|
||||
id: 'run-failure',
|
||||
projectId: 'default',
|
||||
taskId: 'task-a',
|
||||
status: 'failed',
|
||||
createdAtMs: 20,
|
||||
},
|
||||
{
|
||||
id: 'run-old-success',
|
||||
projectId: 'default',
|
||||
taskId: 'task-a',
|
||||
status: 'succeeded',
|
||||
createdAtMs: 10,
|
||||
},
|
||||
]);
|
||||
await assert.rejects(
|
||||
database.runs.listRecentRunsByTask({
|
||||
projectId: 'default',
|
||||
taskId: 'task-a',
|
||||
limit: 66,
|
||||
}),
|
||||
TypeError,
|
||||
);
|
||||
|
||||
await database.close();
|
||||
const client = new DatabaseSync(options.databasePath, { readOnly: true });
|
||||
t.after(() => {
|
||||
if (client.isOpen) client.close();
|
||||
});
|
||||
const plan = client
|
||||
.prepare(
|
||||
`EXPLAIN QUERY PLAN
|
||||
SELECT "id", "project_id", "task_id", "status", "created_at_ms"
|
||||
FROM "Runs"
|
||||
WHERE "project_id" = ? AND "task_id" = ?
|
||||
ORDER BY "created_at_ms" DESC, "id" DESC
|
||||
LIMIT ?`,
|
||||
)
|
||||
.all('default', 'task-a', 65);
|
||||
assert.match(
|
||||
plan.map((entry) => entry.detail).join('\n'),
|
||||
/ql3_local_runs_task_created_idx/u,
|
||||
);
|
||||
});
|
||||
@@ -242,6 +242,12 @@
|
||||
"builtin-run-compare-projection": [
|
||||
"dist/tool-execution/builtin-run-compare/builtInRunCompareProjection.d.ts"
|
||||
],
|
||||
"builtin-task-run-outcome-compare-tool": [
|
||||
"dist/tool-execution/builtin-run-compare/builtInTaskRunOutcomeCompareTool.d.ts"
|
||||
],
|
||||
"builtin-task-run-outcome-compare-projection": [
|
||||
"dist/tool-execution/builtin-run-compare/builtInTaskRunOutcomeCompareProjection.d.ts"
|
||||
],
|
||||
"run": [
|
||||
"dist/run/run.d.ts"
|
||||
],
|
||||
@@ -254,6 +260,9 @@
|
||||
"project-run-list": [
|
||||
"dist/run/projectRunList.d.ts"
|
||||
],
|
||||
"task-run-outcome-window": [
|
||||
"dist/run/outcome-comparison/taskRunOutcomeWindow.d.ts"
|
||||
],
|
||||
"bounded-run-read-projection": [
|
||||
"dist/run/projection/boundedRunReadProjection.d.ts"
|
||||
],
|
||||
@@ -318,6 +327,11 @@
|
||||
"require": "./dist/run/projectRunList.js",
|
||||
"default": "./dist/run/projectRunList.js"
|
||||
},
|
||||
"./task-run-outcome-window": {
|
||||
"types": "./dist/run/outcome-comparison/taskRunOutcomeWindow.d.ts",
|
||||
"require": "./dist/run/outcome-comparison/taskRunOutcomeWindow.js",
|
||||
"default": "./dist/run/outcome-comparison/taskRunOutcomeWindow.js"
|
||||
},
|
||||
"./bounded-run-read-projection": {
|
||||
"types": "./dist/run/projection/boundedRunReadProjection.d.ts",
|
||||
"require": "./dist/run/projection/boundedRunReadProjection.js",
|
||||
@@ -668,6 +682,16 @@
|
||||
"require": "./dist/tool-execution/builtin-run-compare/builtInRunCompareProjection.js",
|
||||
"default": "./dist/tool-execution/builtin-run-compare/builtInRunCompareProjection.js"
|
||||
},
|
||||
"./builtin-task-run-outcome-compare-tool": {
|
||||
"types": "./dist/tool-execution/builtin-run-compare/builtInTaskRunOutcomeCompareTool.d.ts",
|
||||
"require": "./dist/tool-execution/builtin-run-compare/builtInTaskRunOutcomeCompareTool.js",
|
||||
"default": "./dist/tool-execution/builtin-run-compare/builtInTaskRunOutcomeCompareTool.js"
|
||||
},
|
||||
"./builtin-task-run-outcome-compare-projection": {
|
||||
"types": "./dist/tool-execution/builtin-run-compare/builtInTaskRunOutcomeCompareProjection.d.ts",
|
||||
"require": "./dist/tool-execution/builtin-run-compare/builtInTaskRunOutcomeCompareProjection.js",
|
||||
"default": "./dist/tool-execution/builtin-run-compare/builtInTaskRunOutcomeCompareProjection.js"
|
||||
},
|
||||
"./secret-reference": {
|
||||
"types": "./dist/secret/secretReference.d.ts",
|
||||
"require": "./dist/secret/secretReference.js",
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
import { RUN_STATUSES, type RunStatus } from '../run';
|
||||
|
||||
export const MAX_TASK_RUN_OUTCOME_WINDOW_STORAGE_LIMIT = 65;
|
||||
|
||||
const CONTROL_PATTERN = /[\u0000-\u001f\u007f]/;
|
||||
|
||||
export interface TaskRunOutcomeWindowQuery {
|
||||
readonly projectId: string;
|
||||
readonly taskId: string;
|
||||
readonly limit: number;
|
||||
}
|
||||
|
||||
export interface TaskRunOutcomeWindowRecord {
|
||||
readonly id: string;
|
||||
readonly projectId: string;
|
||||
readonly taskId: string;
|
||||
readonly status: RunStatus;
|
||||
readonly createdAtMs: number;
|
||||
}
|
||||
|
||||
export interface TaskRunOutcomeWindowReader {
|
||||
listRecentRunsByTask(
|
||||
query: Readonly<TaskRunOutcomeWindowQuery>,
|
||||
): Promise<readonly Readonly<TaskRunOutcomeWindowRecord>[]>;
|
||||
}
|
||||
|
||||
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 normalizeTaskRunOutcomeWindowQuery(
|
||||
value: Readonly<TaskRunOutcomeWindowQuery>,
|
||||
): Readonly<TaskRunOutcomeWindowQuery> {
|
||||
if (
|
||||
!value ||
|
||||
typeof value !== 'object' ||
|
||||
Array.isArray(value) ||
|
||||
Reflect.ownKeys(value).length !== 3 ||
|
||||
!Object.hasOwn(value, 'projectId') ||
|
||||
!Object.hasOwn(value, 'taskId') ||
|
||||
!Object.hasOwn(value, 'limit') ||
|
||||
!boundedText(value.projectId, 128) ||
|
||||
!boundedText(value.taskId, 255) ||
|
||||
!Number.isSafeInteger(value.limit) ||
|
||||
value.limit < 1 ||
|
||||
value.limit > MAX_TASK_RUN_OUTCOME_WINDOW_STORAGE_LIMIT
|
||||
) {
|
||||
throw new TypeError('Task Run outcome window query is invalid');
|
||||
}
|
||||
return Object.freeze({
|
||||
projectId: value.projectId,
|
||||
taskId: value.taskId,
|
||||
limit: value.limit,
|
||||
});
|
||||
}
|
||||
|
||||
export function normalizeTaskRunOutcomeWindowRecord(
|
||||
value: Readonly<TaskRunOutcomeWindowRecord>,
|
||||
): Readonly<TaskRunOutcomeWindowRecord> {
|
||||
if (
|
||||
!value ||
|
||||
typeof value !== 'object' ||
|
||||
Array.isArray(value) ||
|
||||
Reflect.ownKeys(value).length !== 5 ||
|
||||
!Object.hasOwn(value, 'id') ||
|
||||
!Object.hasOwn(value, 'projectId') ||
|
||||
!Object.hasOwn(value, 'taskId') ||
|
||||
!Object.hasOwn(value, 'status') ||
|
||||
!Object.hasOwn(value, 'createdAtMs') ||
|
||||
!boundedText(value.id, 128) ||
|
||||
!boundedText(value.projectId, 128) ||
|
||||
!boundedText(value.taskId, 255) ||
|
||||
!RUN_STATUSES.includes(value.status) ||
|
||||
!Number.isSafeInteger(value.createdAtMs) ||
|
||||
value.createdAtMs < 0
|
||||
) {
|
||||
throw new TypeError('Task Run outcome window record is invalid');
|
||||
}
|
||||
return Object.freeze({
|
||||
id: value.id,
|
||||
projectId: value.projectId,
|
||||
taskId: value.taskId,
|
||||
status: value.status,
|
||||
createdAtMs: value.createdAtMs,
|
||||
});
|
||||
}
|
||||
+21
-10
@@ -19,7 +19,7 @@ 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([
|
||||
export const BUILTIN_RUN_COMPARABLE_FIELDS = Object.freeze([
|
||||
'taskId',
|
||||
'taskRevision',
|
||||
'status',
|
||||
@@ -28,7 +28,7 @@ const COMPARABLE_FIELDS = Object.freeze([
|
||||
'executionOwner',
|
||||
] as const);
|
||||
|
||||
const RUN_PROJECTION_SCHEMA = Object.freeze({
|
||||
export const BUILTIN_RUN_PROJECTION_SCHEMA = Object.freeze({
|
||||
type: 'object' as const,
|
||||
properties: {
|
||||
found: { type: 'boolean' as const },
|
||||
@@ -103,8 +103,8 @@ export const BUILTIN_RUN_COMPARE_TOOL_DEFINITION = normalizeToolDefinition({
|
||||
outputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
baseline: RUN_PROJECTION_SCHEMA,
|
||||
candidate: RUN_PROJECTION_SCHEMA,
|
||||
baseline: BUILTIN_RUN_PROJECTION_SCHEMA,
|
||||
candidate: BUILTIN_RUN_PROJECTION_SCHEMA,
|
||||
comparable: { type: 'boolean' },
|
||||
sameTask: { type: 'boolean' },
|
||||
sameTaskRevision: { type: 'boolean' },
|
||||
@@ -113,9 +113,9 @@ export const BUILTIN_RUN_COMPARE_TOOL_DEFINITION = normalizeToolDefinition({
|
||||
items: {
|
||||
type: 'string',
|
||||
maxLength: 32,
|
||||
enum: COMPARABLE_FIELDS,
|
||||
enum: BUILTIN_RUN_COMPARABLE_FIELDS,
|
||||
},
|
||||
maxItems: COMPARABLE_FIELDS.length,
|
||||
maxItems: BUILTIN_RUN_COMPARABLE_FIELDS.length,
|
||||
},
|
||||
queueDelayDeltaMs: {
|
||||
type: 'integer',
|
||||
@@ -212,14 +212,21 @@ function timestampDelta(
|
||||
return Number.isSafeInteger(delta) ? delta : undefined;
|
||||
}
|
||||
|
||||
function compareProjections(
|
||||
export type BuiltInRunComparisonConsistency =
|
||||
| 'ordered_independent_point_reads'
|
||||
| 'bounded_task_window_then_ordered_point_reads';
|
||||
|
||||
export function compareBoundedRunProjections(
|
||||
baseline: BoundedRunReadProjection,
|
||||
candidate: BoundedRunReadProjection,
|
||||
consistency: BuiltInRunComparisonConsistency,
|
||||
): 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])
|
||||
? BUILTIN_RUN_COMPARABLE_FIELDS.filter(
|
||||
(field) => baseline[field] !== candidate[field],
|
||||
)
|
||||
: [],
|
||||
);
|
||||
const queueDelayDeltaMs = comparable
|
||||
@@ -259,7 +266,7 @@ function compareProjections(
|
||||
? {}
|
||||
: { executionDurationDeltaMs }),
|
||||
...(totalDurationDeltaMs === undefined ? {} : { totalDurationDeltaMs }),
|
||||
consistency: 'ordered_independent_point_reads',
|
||||
consistency,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -295,7 +302,11 @@ export async function executeBuiltInRunCompareTool(
|
||||
projectId,
|
||||
inputRecord.candidateRunId,
|
||||
);
|
||||
return compareProjections(baseline, candidate);
|
||||
return compareBoundedRunProjections(
|
||||
baseline,
|
||||
candidate,
|
||||
'ordered_independent_point_reads',
|
||||
);
|
||||
} catch (error) {
|
||||
if (!(error instanceof BoundedRunReadProjectionUnavailableError)) {
|
||||
return invalid('execution context or input is invalid');
|
||||
|
||||
+360
@@ -0,0 +1,360 @@
|
||||
import type { RunRepositoryReader } from '../../run/runRepository';
|
||||
import {
|
||||
BoundedRunReadProjectionUnavailableError,
|
||||
executeBoundedRunReadProjection,
|
||||
type BoundedRunReadProjection,
|
||||
} from '../../run/projection/boundedRunReadProjection';
|
||||
import {
|
||||
MAX_TASK_RUN_OUTCOME_WINDOW_STORAGE_LIMIT,
|
||||
normalizeTaskRunOutcomeWindowQuery,
|
||||
normalizeTaskRunOutcomeWindowRecord,
|
||||
type TaskRunOutcomeWindowReader,
|
||||
type TaskRunOutcomeWindowRecord,
|
||||
} from '../../run/outcome-comparison/taskRunOutcomeWindow';
|
||||
import {
|
||||
normalizeToolDefinition,
|
||||
type ToolJsonValue,
|
||||
} from '../tool-registry/toolRegistry';
|
||||
import {
|
||||
BUILTIN_RUN_COMPARABLE_FIELDS,
|
||||
BUILTIN_RUN_PROJECTION_SCHEMA,
|
||||
compareBoundedRunProjections,
|
||||
} from './builtInRunCompareProjection';
|
||||
|
||||
export const BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL = Object.freeze({
|
||||
name: 'qinglong.task.runs.compare',
|
||||
version: '1.0.0',
|
||||
});
|
||||
export const BUILTIN_TASK_RUN_OUTCOME_COMPARE_TIMEOUT_SECONDS = 5;
|
||||
export const TASK_RUN_OUTCOME_SEARCH_LIMIT = 64;
|
||||
|
||||
const MAX_INT = 2_147_483_647;
|
||||
const MIN_INT = -2_147_483_648;
|
||||
const CONTROL_PATTERN = /[\u0000-\u001f\u007f]/;
|
||||
|
||||
export const BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL_DEFINITION =
|
||||
normalizeToolDefinition({
|
||||
name: BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL.name,
|
||||
version: BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL.version,
|
||||
description:
|
||||
'Compare the latest succeeded and failed Runs found in one fixed bounded Task history window',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
taskId: { type: 'string', minLength: 1, maxLength: 255 },
|
||||
},
|
||||
required: ['taskId'],
|
||||
additionalProperties: false,
|
||||
},
|
||||
outputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
taskId: { type: 'string', minLength: 1, maxLength: 255 },
|
||||
baselineOutcome: {
|
||||
type: 'string',
|
||||
maxLength: 16,
|
||||
enum: ['succeeded'] as const,
|
||||
},
|
||||
candidateOutcome: {
|
||||
type: 'string',
|
||||
maxLength: 16,
|
||||
enum: ['failed'] as const,
|
||||
},
|
||||
baseline: BUILTIN_RUN_PROJECTION_SCHEMA,
|
||||
candidate: BUILTIN_RUN_PROJECTION_SCHEMA,
|
||||
comparable: { type: 'boolean' },
|
||||
sameTask: { type: 'boolean' },
|
||||
sameTaskRevision: { type: 'boolean' },
|
||||
changedFields: {
|
||||
type: 'array',
|
||||
items: {
|
||||
type: 'string',
|
||||
maxLength: 32,
|
||||
enum: BUILTIN_RUN_COMPARABLE_FIELDS,
|
||||
},
|
||||
maxItems: BUILTIN_RUN_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,
|
||||
},
|
||||
selection: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
windowLimit: {
|
||||
type: 'integer',
|
||||
minimum: TASK_RUN_OUTCOME_SEARCH_LIMIT,
|
||||
maximum: TASK_RUN_OUTCOME_SEARCH_LIMIT,
|
||||
},
|
||||
searchedRunCount: {
|
||||
type: 'integer',
|
||||
minimum: 0,
|
||||
maximum: TASK_RUN_OUTCOME_SEARCH_LIMIT,
|
||||
},
|
||||
hasOlderRuns: { type: 'boolean' },
|
||||
complete: { type: 'boolean' },
|
||||
order: {
|
||||
type: 'string',
|
||||
maxLength: 32,
|
||||
enum: ['created_at_desc_id_desc'] as const,
|
||||
},
|
||||
},
|
||||
required: [
|
||||
'windowLimit',
|
||||
'searchedRunCount',
|
||||
'hasOlderRuns',
|
||||
'complete',
|
||||
'order',
|
||||
],
|
||||
additionalProperties: false,
|
||||
},
|
||||
consistency: {
|
||||
type: 'string',
|
||||
maxLength: 64,
|
||||
enum: ['bounded_task_window_then_ordered_point_reads'] as const,
|
||||
},
|
||||
},
|
||||
required: [
|
||||
'taskId',
|
||||
'baselineOutcome',
|
||||
'candidateOutcome',
|
||||
'baseline',
|
||||
'candidate',
|
||||
'comparable',
|
||||
'sameTask',
|
||||
'sameTaskRevision',
|
||||
'changedFields',
|
||||
'selection',
|
||||
'consistency',
|
||||
],
|
||||
additionalProperties: false,
|
||||
},
|
||||
effect: 'read',
|
||||
risk: 'low',
|
||||
requiredPermissions: ['run.read'],
|
||||
timeoutSeconds: BUILTIN_TASK_RUN_OUTCOME_COMPARE_TIMEOUT_SECONDS,
|
||||
});
|
||||
|
||||
export class InvalidBuiltInTaskRunOutcomeCompareToolError extends TypeError {
|
||||
readonly code = 'BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL_INVALID';
|
||||
|
||||
constructor(message: string) {
|
||||
super(`Built-in Task Run outcome compare Tool is invalid: ${message}`);
|
||||
this.name = 'InvalidBuiltInTaskRunOutcomeCompareToolError';
|
||||
}
|
||||
}
|
||||
|
||||
export class BuiltInTaskRunOutcomeCompareToolUnavailableError extends Error {
|
||||
readonly code = 'BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL_UNAVAILABLE';
|
||||
|
||||
constructor() {
|
||||
super('Built-in Task Run outcome compare Tool is unavailable');
|
||||
this.name = 'BuiltInTaskRunOutcomeCompareToolUnavailableError';
|
||||
}
|
||||
}
|
||||
|
||||
function invalid(message: string): never {
|
||||
throw new InvalidBuiltInTaskRunOutcomeCompareToolError(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 isStrictlyOlder(
|
||||
value: Readonly<TaskRunOutcomeWindowRecord>,
|
||||
previous: Readonly<TaskRunOutcomeWindowRecord>,
|
||||
): boolean {
|
||||
return (
|
||||
value.createdAtMs < previous.createdAtMs ||
|
||||
(value.createdAtMs === previous.createdAtMs && value.id < previous.id)
|
||||
);
|
||||
}
|
||||
|
||||
async function selectOutcomeWindow(
|
||||
windows: TaskRunOutcomeWindowReader,
|
||||
projectId: string,
|
||||
taskId: string,
|
||||
): Promise<
|
||||
Readonly<{
|
||||
latestSucceededRunId?: string;
|
||||
latestFailedRunId?: string;
|
||||
searchedRunCount: number;
|
||||
hasOlderRuns: boolean;
|
||||
complete: boolean;
|
||||
}>
|
||||
> {
|
||||
let rows: readonly Readonly<TaskRunOutcomeWindowRecord>[];
|
||||
try {
|
||||
rows = await windows.listRecentRunsByTask(
|
||||
normalizeTaskRunOutcomeWindowQuery({
|
||||
projectId,
|
||||
taskId,
|
||||
limit: MAX_TASK_RUN_OUTCOME_WINDOW_STORAGE_LIMIT,
|
||||
}),
|
||||
);
|
||||
} catch {
|
||||
throw new BuiltInTaskRunOutcomeCompareToolUnavailableError();
|
||||
}
|
||||
if (
|
||||
!Array.isArray(rows) ||
|
||||
rows.length > MAX_TASK_RUN_OUTCOME_WINDOW_STORAGE_LIMIT
|
||||
) {
|
||||
throw new BuiltInTaskRunOutcomeCompareToolUnavailableError();
|
||||
}
|
||||
|
||||
let previous: Readonly<TaskRunOutcomeWindowRecord> | undefined;
|
||||
let latestSucceededRunId: string | undefined;
|
||||
let latestFailedRunId: string | undefined;
|
||||
for (const row of rows) {
|
||||
let normalized: Readonly<TaskRunOutcomeWindowRecord>;
|
||||
try {
|
||||
normalized = normalizeTaskRunOutcomeWindowRecord(row);
|
||||
} catch {
|
||||
throw new BuiltInTaskRunOutcomeCompareToolUnavailableError();
|
||||
}
|
||||
if (
|
||||
normalized.projectId !== projectId ||
|
||||
normalized.taskId !== taskId ||
|
||||
(previous !== undefined && !isStrictlyOlder(normalized, previous))
|
||||
) {
|
||||
throw new BuiltInTaskRunOutcomeCompareToolUnavailableError();
|
||||
}
|
||||
previous = normalized;
|
||||
}
|
||||
|
||||
const window = rows.slice(0, TASK_RUN_OUTCOME_SEARCH_LIMIT);
|
||||
for (const row of window) {
|
||||
if (row.status === 'succeeded' && latestSucceededRunId === undefined) {
|
||||
latestSucceededRunId = row.id;
|
||||
}
|
||||
if (row.status === 'failed' && latestFailedRunId === undefined) {
|
||||
latestFailedRunId = row.id;
|
||||
}
|
||||
if (latestSucceededRunId !== undefined && latestFailedRunId !== undefined) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
const hasOlderRuns = rows.length > TASK_RUN_OUTCOME_SEARCH_LIMIT;
|
||||
const complete =
|
||||
!hasOlderRuns ||
|
||||
(latestSucceededRunId !== undefined && latestFailedRunId !== undefined);
|
||||
return Object.freeze({
|
||||
...(latestSucceededRunId === undefined ? {} : { latestSucceededRunId }),
|
||||
...(latestFailedRunId === undefined ? {} : { latestFailedRunId }),
|
||||
searchedRunCount: window.length,
|
||||
hasOlderRuns,
|
||||
complete,
|
||||
});
|
||||
}
|
||||
|
||||
async function selectedProjection(
|
||||
runs: Pick<RunRepositoryReader, 'findRunById'>,
|
||||
projectId: string,
|
||||
taskId: string,
|
||||
expectedStatus: 'succeeded' | 'failed',
|
||||
runId: string | undefined,
|
||||
): Promise<Readonly<BoundedRunReadProjection>> {
|
||||
if (runId === undefined) return Object.freeze({ found: false });
|
||||
const projection = await executeBoundedRunReadProjection(
|
||||
runs,
|
||||
projectId,
|
||||
runId,
|
||||
);
|
||||
if (
|
||||
projection.found !== true ||
|
||||
projection.taskId !== taskId ||
|
||||
projection.status !== expectedStatus
|
||||
) {
|
||||
throw new BuiltInTaskRunOutcomeCompareToolUnavailableError();
|
||||
}
|
||||
return projection;
|
||||
}
|
||||
|
||||
export async function executeBuiltInTaskRunOutcomeCompareTool(
|
||||
windows: TaskRunOutcomeWindowReader,
|
||||
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 (
|
||||
!windows ||
|
||||
typeof windows.listRecentRunsByTask !== 'function' ||
|
||||
!runs ||
|
||||
typeof runs.findRunById !== 'function' ||
|
||||
!boundedText(projectId, 128) ||
|
||||
!inputRecord ||
|
||||
Reflect.ownKeys(inputRecord).length !== 1 ||
|
||||
!boundedText(inputRecord.taskId, 255)
|
||||
) {
|
||||
return invalid('execution context or input is invalid');
|
||||
}
|
||||
|
||||
try {
|
||||
const selected = await selectOutcomeWindow(
|
||||
windows,
|
||||
projectId,
|
||||
inputRecord.taskId,
|
||||
);
|
||||
const baseline = await selectedProjection(
|
||||
runs,
|
||||
projectId,
|
||||
inputRecord.taskId,
|
||||
'succeeded',
|
||||
selected.latestSucceededRunId,
|
||||
);
|
||||
const candidate = await selectedProjection(
|
||||
runs,
|
||||
projectId,
|
||||
inputRecord.taskId,
|
||||
'failed',
|
||||
selected.latestFailedRunId,
|
||||
);
|
||||
const comparison = compareBoundedRunProjections(
|
||||
baseline,
|
||||
candidate,
|
||||
'bounded_task_window_then_ordered_point_reads',
|
||||
);
|
||||
return Object.freeze({
|
||||
taskId: inputRecord.taskId,
|
||||
baselineOutcome: 'succeeded',
|
||||
candidateOutcome: 'failed',
|
||||
...comparison,
|
||||
selection: Object.freeze({
|
||||
windowLimit: TASK_RUN_OUTCOME_SEARCH_LIMIT,
|
||||
searchedRunCount: selected.searchedRunCount,
|
||||
hasOlderRuns: selected.hasOlderRuns,
|
||||
complete: selected.complete,
|
||||
order: 'created_at_desc_id_desc',
|
||||
}),
|
||||
});
|
||||
} catch (error) {
|
||||
if (
|
||||
error instanceof BuiltInTaskRunOutcomeCompareToolUnavailableError ||
|
||||
error instanceof BoundedRunReadProjectionUnavailableError
|
||||
) {
|
||||
throw new BuiltInTaskRunOutcomeCompareToolUnavailableError();
|
||||
}
|
||||
return invalid('execution context or input is invalid');
|
||||
}
|
||||
}
|
||||
+181
@@ -0,0 +1,181 @@
|
||||
import type { DeploymentProfile } from '../../cluster-control/clusterControlActivation';
|
||||
import type { RunRepositoryReader } from '../../run/runRepository';
|
||||
import type { TaskRunOutcomeWindowReader } from '../../run/outcome-comparison/taskRunOutcomeWindow';
|
||||
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_TASK_RUN_OUTCOME_COMPARE_TIMEOUT_SECONDS,
|
||||
BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL,
|
||||
BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL_DEFINITION,
|
||||
InvalidBuiltInTaskRunOutcomeCompareToolError,
|
||||
executeBuiltInTaskRunOutcomeCompareTool,
|
||||
} from './builtInTaskRunOutcomeCompareProjection';
|
||||
|
||||
export {
|
||||
BUILTIN_TASK_RUN_OUTCOME_COMPARE_TIMEOUT_SECONDS,
|
||||
BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL,
|
||||
BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL_DEFINITION,
|
||||
TASK_RUN_OUTCOME_SEARCH_LIMIT,
|
||||
BuiltInTaskRunOutcomeCompareToolUnavailableError,
|
||||
InvalidBuiltInTaskRunOutcomeCompareToolError,
|
||||
executeBuiltInTaskRunOutcomeCompareTool,
|
||||
} from './builtInTaskRunOutcomeCompareProjection';
|
||||
|
||||
export const BUILTIN_TASK_RUN_OUTCOME_COMPARE_ADAPTER = Object.freeze({
|
||||
id: 'builtin.qinglong.task-runs-compare',
|
||||
version: '1.0.0',
|
||||
});
|
||||
export const BUILTIN_TASK_RUN_OUTCOME_COMPARE_REDACTION_CONTRACT =
|
||||
Object.freeze({
|
||||
id: 'redaction.qinglong.task-runs-compare',
|
||||
version: '1.0.0',
|
||||
});
|
||||
export const BUILTIN_TASK_RUN_OUTCOME_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 InvalidBuiltInTaskRunOutcomeCompareToolError(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 createBuiltInTaskRunOutcomeCompareToolHandlerBinding(
|
||||
snapshotValue: ProjectToolDefinitionSnapshot,
|
||||
profiles: readonly DeploymentProfile[],
|
||||
): Readonly<TrustedToolHandlerBinding> {
|
||||
const snapshot = normalizeProjectToolDefinitionSnapshot(snapshotValue);
|
||||
const definition = snapshot.definitions.find(
|
||||
(entry) =>
|
||||
entry.definition.name === BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL.name &&
|
||||
entry.definition.version ===
|
||||
BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL.version,
|
||||
)?.definition;
|
||||
if (
|
||||
!definition ||
|
||||
!sameValue(definition, BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL_DEFINITION)
|
||||
) {
|
||||
return invalid('reviewed Tool definition is absent or changed');
|
||||
}
|
||||
return createTrustedToolHandlerBinding(snapshot, {
|
||||
tool: BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL,
|
||||
adapter: BUILTIN_TASK_RUN_OUTCOME_COMPARE_ADAPTER,
|
||||
executionClass: 'builtin_in_process',
|
||||
profiles,
|
||||
authorities: ['database.read'],
|
||||
timeoutSeconds: BUILTIN_TASK_RUN_OUTCOME_COMPARE_TIMEOUT_SECONDS,
|
||||
redactionContract: BUILTIN_TASK_RUN_OUTCOME_COMPARE_REDACTION_CONTRACT,
|
||||
auditContract: BUILTIN_TASK_RUN_OUTCOME_COMPARE_AUDIT_CONTRACT,
|
||||
});
|
||||
}
|
||||
|
||||
export class BuiltInTaskRunOutcomeCompareToolAdapter
|
||||
implements TrustedToolExecutionAdapter
|
||||
{
|
||||
readonly binding!: Readonly<TrustedToolHandlerBinding>;
|
||||
readonly profile!: DeploymentProfile;
|
||||
readonly recoveryMode = 'retry_safe_read' as const;
|
||||
readonly #windows!: TaskRunOutcomeWindowReader;
|
||||
readonly #runs!: Pick<RunRepositoryReader, 'findRunById'>;
|
||||
|
||||
constructor(
|
||||
bindingValue: TrustedToolHandlerBinding,
|
||||
profile: DeploymentProfile,
|
||||
definitions: ToolDefinitionRegistry,
|
||||
windows: TaskRunOutcomeWindowReader,
|
||||
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_TASK_RUN_OUTCOME_COMPARE_TOOL.name,
|
||||
BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL.version,
|
||||
);
|
||||
} catch {
|
||||
return invalid('reviewed Tool definition is unavailable');
|
||||
}
|
||||
if (
|
||||
!sameValue(binding.tool, BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL) ||
|
||||
!sameValue(binding.adapter, BUILTIN_TASK_RUN_OUTCOME_COMPARE_ADAPTER) ||
|
||||
binding.executionClass !== 'builtin_in_process' ||
|
||||
!sameValue(binding.authorities, ['database.read']) ||
|
||||
binding.timeoutSeconds !==
|
||||
BUILTIN_TASK_RUN_OUTCOME_COMPARE_TIMEOUT_SECONDS ||
|
||||
!sameValue(
|
||||
binding.redactionContract,
|
||||
BUILTIN_TASK_RUN_OUTCOME_COMPARE_REDACTION_CONTRACT,
|
||||
) ||
|
||||
!sameValue(
|
||||
binding.auditContract,
|
||||
BUILTIN_TASK_RUN_OUTCOME_COMPARE_AUDIT_CONTRACT,
|
||||
) ||
|
||||
!binding.profiles.includes(profile) ||
|
||||
!sameValue(definition, BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL_DEFINITION)
|
||||
) {
|
||||
return invalid('binding does not match the reviewed adapter contract');
|
||||
}
|
||||
if (!windows || typeof windows.listRecentRunsByTask !== 'function') {
|
||||
return invalid('Task Run outcome window reader is invalid');
|
||||
}
|
||||
if (!runs || typeof runs.findRunById !== 'function') {
|
||||
return invalid('Run repository is invalid');
|
||||
}
|
||||
this.binding = binding;
|
||||
this.profile = profile;
|
||||
this.#windows = windows;
|
||||
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 executeBuiltInTaskRunOutcomeCompareTool(
|
||||
this.#windows,
|
||||
this.#runs,
|
||||
context.projectId,
|
||||
input,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,328 @@
|
||||
const assert = require('node:assert/strict');
|
||||
const { test } = require('node:test');
|
||||
|
||||
const {
|
||||
BUILTIN_TASK_RUN_OUTCOME_COMPARE_ADAPTER,
|
||||
BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL,
|
||||
BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL_DEFINITION,
|
||||
BuiltInTaskRunOutcomeCompareToolAdapter,
|
||||
BuiltInTaskRunOutcomeCompareToolUnavailableError,
|
||||
InvalidBuiltInTaskRunOutcomeCompareToolError,
|
||||
TASK_RUN_OUTCOME_SEARCH_LIMIT,
|
||||
createBuiltInTaskRunOutcomeCompareToolHandlerBinding,
|
||||
executeBuiltInTaskRunOutcomeCompareTool,
|
||||
} = require('../dist/tool-execution/builtin-run-compare/builtInTaskRunOutcomeCompareTool');
|
||||
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-outcomes',
|
||||
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',
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function windowRecord(value) {
|
||||
return {
|
||||
id: value.id,
|
||||
projectId: value.projectId,
|
||||
taskId: value.taskId,
|
||||
status: value.status,
|
||||
createdAtMs: value.createdAtMs,
|
||||
};
|
||||
}
|
||||
|
||||
function fixture(records, window, calls = []) {
|
||||
return {
|
||||
windows: {
|
||||
async listRecentRunsByTask(query) {
|
||||
calls.push({ type: 'window', query });
|
||||
return window;
|
||||
},
|
||||
},
|
||||
runs: {
|
||||
async findRunById(runId) {
|
||||
calls.push({ type: 'point', runId });
|
||||
return records.get(runId) ?? null;
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function snapshot(
|
||||
definition = BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL_DEFINITION,
|
||||
) {
|
||||
const generation = createPluginPackageResourceGenerationFromReferences({
|
||||
installationId: 'install-qinglong-outcome-compare',
|
||||
projectId: 'project-outcomes',
|
||||
packageName: 'qinglong',
|
||||
lockDigest: DIGEST_A,
|
||||
generation: 1,
|
||||
previousActiveLockDigest: null,
|
||||
contentDigest: DIGEST_B,
|
||||
resources: [],
|
||||
});
|
||||
return createProjectToolDefinitionSnapshot({
|
||||
projectId: 'project-outcomes',
|
||||
contributions: [
|
||||
{
|
||||
generation,
|
||||
revisionDigest: DIGEST_C,
|
||||
definitions: [definition],
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
test('selects and compares the latest succeeded and failed Runs in one fixed Task window', async () => {
|
||||
const calls = [];
|
||||
const succeeded = run('run-success');
|
||||
const failed = 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,
|
||||
});
|
||||
const ignored = run('run-running', {
|
||||
status: 'running',
|
||||
createdAtMs: 3_000,
|
||||
});
|
||||
const value = fixture(
|
||||
new Map([
|
||||
[succeeded.id, succeeded],
|
||||
[failed.id, failed],
|
||||
]),
|
||||
[windowRecord(ignored), windowRecord(failed), windowRecord(succeeded)],
|
||||
calls,
|
||||
);
|
||||
const output = await executeBuiltInTaskRunOutcomeCompareTool(
|
||||
value.windows,
|
||||
value.runs,
|
||||
'project-outcomes',
|
||||
{ taskId: 'task-backup' },
|
||||
);
|
||||
|
||||
assert.deepEqual(calls, [
|
||||
{
|
||||
type: 'window',
|
||||
query: {
|
||||
projectId: 'project-outcomes',
|
||||
taskId: 'task-backup',
|
||||
limit: 65,
|
||||
},
|
||||
},
|
||||
{ type: 'point', runId: 'run-success' },
|
||||
{ type: 'point', runId: 'run-failure' },
|
||||
]);
|
||||
assert.equal(output.taskId, 'task-backup');
|
||||
assert.equal(output.baselineOutcome, 'succeeded');
|
||||
assert.equal(output.candidateOutcome, 'failed');
|
||||
assert.equal(output.baseline.id, 'run-success');
|
||||
assert.equal(output.candidate.id, 'run-failure');
|
||||
assert.deepEqual(output.changedFields, ['taskRevision', 'status']);
|
||||
assert.equal(output.queueDelayDeltaMs, 20);
|
||||
assert.equal(output.executionDurationDeltaMs, 150);
|
||||
assert.equal(output.totalDurationDeltaMs, 200);
|
||||
assert.deepEqual(output.selection, {
|
||||
windowLimit: 64,
|
||||
searchedRunCount: 3,
|
||||
hasOlderRuns: false,
|
||||
complete: true,
|
||||
order: 'created_at_desc_id_desc',
|
||||
});
|
||||
assert.equal(
|
||||
output.consistency,
|
||||
'bounded_task_window_then_ordered_point_reads',
|
||||
);
|
||||
assert.equal(output.baseline.requestId, undefined);
|
||||
|
||||
const registry = projectToolDefinitionRegistry(snapshot());
|
||||
assert.deepEqual(
|
||||
registry.normalizeOutput(
|
||||
BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL.name,
|
||||
BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL.version,
|
||||
output,
|
||||
),
|
||||
output,
|
||||
);
|
||||
});
|
||||
|
||||
test('reports an incomplete fixed window without exposing pagination', async () => {
|
||||
const calls = [];
|
||||
const rows = Array.from(
|
||||
{ length: TASK_RUN_OUTCOME_SEARCH_LIMIT + 1 },
|
||||
(_, index) =>
|
||||
windowRecord(
|
||||
run(`run-${String(100 - index).padStart(3, '0')}`, {
|
||||
status: 'running',
|
||||
createdAtMs: 10_000 - index,
|
||||
}),
|
||||
),
|
||||
);
|
||||
const value = fixture(new Map(), rows, calls);
|
||||
const output = await executeBuiltInTaskRunOutcomeCompareTool(
|
||||
value.windows,
|
||||
value.runs,
|
||||
'project-outcomes',
|
||||
{ taskId: 'task-backup' },
|
||||
);
|
||||
|
||||
assert.deepEqual(output.baseline, { found: false });
|
||||
assert.deepEqual(output.candidate, { found: false });
|
||||
assert.equal(output.comparable, false);
|
||||
assert.deepEqual(output.selection, {
|
||||
windowLimit: 64,
|
||||
searchedRunCount: 64,
|
||||
hasOlderRuns: true,
|
||||
complete: false,
|
||||
order: 'created_at_desc_id_desc',
|
||||
});
|
||||
assert.equal(calls.length, 1);
|
||||
assert.equal(
|
||||
BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL_DEFINITION.inputSchema.properties
|
||||
.after,
|
||||
undefined,
|
||||
);
|
||||
assert.equal(
|
||||
BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL_DEFINITION.inputSchema.properties
|
||||
.limit,
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
|
||||
test('fails closed on foreign, unordered, corrupt, disappearing, and unavailable records', async () => {
|
||||
const valid = windowRecord(run('run-valid'));
|
||||
for (const window of [
|
||||
[{ ...valid, projectId: 'other-project' }],
|
||||
[valid, { ...valid, id: 'run-newer', createdAtMs: 2_000 }],
|
||||
[{ ...valid, status: 'invented' }],
|
||||
]) {
|
||||
const value = fixture(new Map(), window);
|
||||
await assert.rejects(
|
||||
executeBuiltInTaskRunOutcomeCompareTool(
|
||||
value.windows,
|
||||
value.runs,
|
||||
'project-outcomes',
|
||||
{ taskId: 'task-backup' },
|
||||
),
|
||||
BuiltInTaskRunOutcomeCompareToolUnavailableError,
|
||||
);
|
||||
}
|
||||
|
||||
const missing = fixture(new Map(), [valid]);
|
||||
await assert.rejects(
|
||||
executeBuiltInTaskRunOutcomeCompareTool(
|
||||
missing.windows,
|
||||
missing.runs,
|
||||
'project-outcomes',
|
||||
{ taskId: 'task-backup' },
|
||||
),
|
||||
BuiltInTaskRunOutcomeCompareToolUnavailableError,
|
||||
);
|
||||
|
||||
await assert.rejects(
|
||||
executeBuiltInTaskRunOutcomeCompareTool(
|
||||
{
|
||||
async listRecentRunsByTask() {
|
||||
throw new Error('private DSN must not escape');
|
||||
},
|
||||
},
|
||||
fixture(new Map(), []).runs,
|
||||
'project-outcomes',
|
||||
{ taskId: 'task-backup' },
|
||||
),
|
||||
BuiltInTaskRunOutcomeCompareToolUnavailableError,
|
||||
);
|
||||
});
|
||||
|
||||
test('rejects aliases and binds the reviewed retry-safe database-read adapter', async () => {
|
||||
const empty = fixture(new Map(), []);
|
||||
await assert.rejects(
|
||||
executeBuiltInTaskRunOutcomeCompareTool(
|
||||
empty.windows,
|
||||
empty.runs,
|
||||
'project-outcomes',
|
||||
{ taskId: 'task-backup', after: 'cursor' },
|
||||
),
|
||||
InvalidBuiltInTaskRunOutcomeCompareToolError,
|
||||
);
|
||||
|
||||
const currentSnapshot = snapshot();
|
||||
const binding = createBuiltInTaskRunOutcomeCompareToolHandlerBinding(
|
||||
currentSnapshot,
|
||||
['edge', 'standalone', 'cluster-control'],
|
||||
);
|
||||
assert.deepEqual(binding.tool, BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL);
|
||||
assert.deepEqual(binding.adapter, BUILTIN_TASK_RUN_OUTCOME_COMPARE_ADAPTER);
|
||||
assert.deepEqual(binding.authorities, ['database.read']);
|
||||
|
||||
const adapter = new BuiltInTaskRunOutcomeCompareToolAdapter(
|
||||
binding,
|
||||
'edge',
|
||||
projectToolDefinitionRegistry(currentSnapshot),
|
||||
empty.windows,
|
||||
empty.runs,
|
||||
);
|
||||
assert.equal(adapter.recoveryMode, 'retry_safe_read');
|
||||
const output = await adapter.execute(
|
||||
{ projectId: 'project-outcomes' },
|
||||
{ taskId: 'task-backup' },
|
||||
);
|
||||
assert.equal(output.selection.complete, true);
|
||||
|
||||
assert.throws(
|
||||
() =>
|
||||
new BuiltInTaskRunOutcomeCompareToolAdapter(
|
||||
{ ...binding, authorities: ['database.read', 'network.client'] },
|
||||
'edge',
|
||||
projectToolDefinitionRegistry(currentSnapshot),
|
||||
empty.windows,
|
||||
empty.runs,
|
||||
),
|
||||
/handler authority is invalid/,
|
||||
);
|
||||
});
|
||||
|
||||
test('publishes only explicit outcome comparison subpaths and keeps the root unchanged', () => {
|
||||
const tool = require('@qinglong/runtime-core/builtin-task-run-outcome-compare-tool');
|
||||
const projection = require('@qinglong/runtime-core/builtin-task-run-outcome-compare-projection');
|
||||
const window = require('@qinglong/runtime-core/task-run-outcome-window');
|
||||
const root = require('@qinglong/runtime-core');
|
||||
|
||||
assert.equal(
|
||||
tool.BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL.name,
|
||||
'qinglong.task.runs.compare',
|
||||
);
|
||||
assert.equal(projection.TASK_RUN_OUTCOME_SEARCH_LIMIT, 64);
|
||||
assert.equal(window.MAX_TASK_RUN_OUTCOME_WINDOW_STORAGE_LIMIT, 65);
|
||||
assert.equal(root.BUILTIN_TASK_RUN_OUTCOME_COMPARE_TOOL, undefined);
|
||||
assert.equal(root.executeBuiltInTaskRunOutcomeCompareTool, undefined);
|
||||
});
|
||||
Reference in New Issue
Block a user