feat(ql3): expose run comparison over local mcp

This commit is contained in:
whyour
2026-08-14 16:23:22 +08:00
parent 3b0aa97303
commit a55613afaa
8 changed files with 285 additions and 11 deletions
@@ -45,6 +45,11 @@ import {
BUILTIN_RUN_STEP_LIST_TOOL_DEFINITION,
executeBuiltInRunStepListTool,
} from '../tool-projection/runStepList';
import {
BUILTIN_RUN_COMPARE_TOOL,
BUILTIN_RUN_COMPARE_TOOL_DEFINITION,
executeBuiltInRunCompareTool,
} from '@qinglong/runtime-core/builtin-run-compare-projection';
import {
BUILTIN_RUN_READ_TOOL,
BUILTIN_RUN_READ_TOOL_DEFINITION,
@@ -177,6 +182,18 @@ const LOCAL_MCP_READ_TOOLS: readonly LocalMcpReadToolDescriptor[] =
input: ToolJsonValue,
) => executeBuiltInRunReadTool(authority.runs, projectId, input),
}),
Object.freeze({
tool: BUILTIN_RUN_COMPARE_TOOL,
definition: BUILTIN_RUN_COMPARE_TOOL_DEFINITION,
title: 'Compare QingLong Runs',
auditReason: 'tool_qinglong_run_compare',
unavailableCode: 'run_compare_unavailable',
execute: (
authority: LocalMcpReadAuthority,
projectId: string,
input: ToolJsonValue,
) => executeBuiltInRunCompareTool(authority.runs, projectId, input),
}),
Object.freeze({
tool: BUILTIN_RUN_EVENT_LIST_TOOL,
definition: BUILTIN_RUN_EVENT_LIST_TOOL_DEFINITION,
@@ -171,6 +171,16 @@ function fixture(options = {}) {
let approvalListReads = 0;
let approvalDetailReads = 0;
let confirmations = 0;
const candidateRun = Object.freeze({
...run(),
id: 'run-2',
taskRevision: 'revision-2',
priority: 1,
createdAtMs: 20,
queuedAtMs: 21,
startedAtMs: 24,
finishedAtMs: 30,
});
const server = createQingLongLocalMcpServer({
projectId: 'default',
now: () => NOW,
@@ -209,13 +219,14 @@ function fixture(options = {}) {
async listRunsByProject(query) {
events.push('read-list');
listReads += 1;
const values = [{ ...run(), id: 'run-2', createdAtMs: 20 }, run()];
const values = [candidateRun, run()];
return values.slice(0, query.limit);
},
async findRunById(runId) {
events.push('read');
reads += 1;
return runId === 'run-1' ? run(options.runProjectId) : null;
if (runId === 'run-1') return run(options.runProjectId);
return runId === 'run-2' ? candidateRun : null;
},
async listEvents(runId, query) {
events.push('read-events');
@@ -381,6 +392,7 @@ test('advertises bounded read-only Run Tools and executes auth -> Policy -> Audi
[
'qinglong.run.list',
'qinglong.run.get',
'qinglong.run.compare',
'qinglong.run.events.list',
'qinglong.run.steps.list',
'qinglong.task.get',
@@ -448,6 +460,88 @@ test('advertises bounded read-only Run Tools and executes auth -> Policy -> Audi
});
});
test('compares two Project Runs 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.run.compare',
arguments: {
baselineRunId: 'run-1',
candidateRunId: 'run-2',
},
});
assert.equal(response.result.isError, undefined);
assert.deepEqual(response.result.structuredContent, {
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-2',
taskId: 'task-1',
taskRevision: 'revision-2',
status: 'succeeded',
version: 3,
eventSequence: 4,
priority: 1,
executionOrigin: 'manual',
executionOwner: 'runtime',
createdAtMs: 20,
queuedAtMs: 21,
startedAtMs: 24,
finishedAtMs: 30,
},
comparable: true,
sameTask: true,
sameTaskRevision: false,
changedFields: ['taskRevision', 'priority'],
queueDelayDeltaMs: 0,
executionDurationDeltaMs: 5,
totalDurationDeltaMs: 7,
consistency: 'ordered_independent_point_reads',
});
assert.deepEqual(value.permissions, [
'tool.call:qinglong.run.compare',
'run.read',
]);
assert.deepEqual(value.events, [
'authenticate',
'policy:tool.call:qinglong.run.compare',
'policy:run.read',
'audit:allowed',
'confirm',
'read',
'read',
]);
assert.deepEqual(value.audits[0].reasons, [
'tool_invocation_allowed',
'tool_qinglong_run_compare',
]);
assert.deepEqual(value.counters(), {
reads: 2,
listReads: 0,
eventReads: 0,
taskListReads: 0,
triggerListReads: 0,
approvalListReads: 0,
approvalDetailReads: 0,
confirmations: 1,
});
});
test('discovers recent Project Runs through the same fenced admission', async (t) => {
const value = fixture();
const connected = await client(value.server, t);
@@ -461,17 +555,17 @@ test('discovers recent Project Runs through the same fenced admission', async (t
{
id: 'run-2',
taskId: 'task-1',
taskRevision: 'revision-1',
taskRevision: 'revision-2',
status: 'succeeded',
version: 3,
eventSequence: 4,
priority: 0,
priority: 1,
executionOrigin: 'manual',
executionOwner: 'runtime',
createdAtMs: 20,
queuedAtMs: 11,
startedAtMs: 12,
finishedAtMs: 13,
queuedAtMs: 21,
startedAtMs: 24,
finishedAtMs: 30,
},
],
hasMore: true,
@@ -74,6 +74,22 @@ async function fixture(t) {
priority: 0,
createdAtMs: NOW - 2_000,
});
await transaction.insertRun({
id: 'run-mcp-e2e-candidate',
projectId: 'default',
taskId: 'task-mcp',
taskRevision: 'revision-2',
taskName: 'MCP test candidate',
triggerType: 'manual',
executionOrigin: 'manual',
executionOwner: 'runtime',
triggeredBy: 'user:mcp-owner',
status: 'created',
version: 0,
eventSequence: 0,
priority: 1,
createdAtMs: NOW - 3_000,
});
await transaction.appendEvent({
id: 'mcp-e2e-event-1',
runId: 'run-mcp-e2e',
@@ -456,6 +472,7 @@ test('serves the authenticated Run Tool over the real stdio protocol and persist
[
'qinglong.run.list',
'qinglong.run.get',
'qinglong.run.compare',
'qinglong.run.events.list',
'qinglong.run.steps.list',
'qinglong.task.get',
@@ -602,7 +619,11 @@ test('serves the authenticated Run Tool over the real stdio protocol and persist
createdAtMs: NOW - 2_000,
},
],
hasMore: false,
hasMore: true,
next: {
createdAtMs: NOW - 2_000,
runId: 'run-mcp-e2e',
},
});
const called = await request('tools/call', {
name: 'qinglong.run.get',
@@ -622,6 +643,47 @@ test('serves the authenticated Run Tool over the real stdio protocol and persist
executionOwner: 'runtime',
createdAtMs: NOW - 2_000,
});
const compared = await request('tools/call', {
name: 'qinglong.run.compare',
arguments: {
baselineRunId: 'run-mcp-e2e',
candidateRunId: 'run-mcp-e2e-candidate',
},
});
assert.equal(compared.result.isError, undefined, JSON.stringify(compared));
assert.deepEqual(compared.result.structuredContent, {
baseline: {
found: true,
id: 'run-mcp-e2e',
taskId: 'task-mcp',
taskRevision: 'revision-1',
status: 'created',
version: 0,
eventSequence: 2,
priority: 0,
executionOrigin: 'manual',
executionOwner: 'runtime',
createdAtMs: NOW - 2_000,
},
candidate: {
found: true,
id: 'run-mcp-e2e-candidate',
taskId: 'task-mcp',
taskRevision: 'revision-2',
status: 'created',
version: 0,
eventSequence: 0,
priority: 1,
executionOrigin: 'manual',
executionOwner: 'runtime',
createdAtMs: NOW - 3_000,
},
comparable: true,
sameTask: true,
sameTaskRevision: false,
changedFields: ['taskRevision', 'priority'],
consistency: 'ordered_independent_point_reads',
});
const events = await request('tools/call', {
name: 'qinglong.run.events.list',
arguments: { runId: 'run-mcp-e2e', limit: 1 },
@@ -698,6 +760,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 {