feat(ql3): execute copilot diagnosis tools

This commit is contained in:
whyour
2026-08-15 16:39:19 +08:00
parent 0163c94be7
commit 9c90c6ce82
25 changed files with 2952 additions and 27 deletions
@@ -1,4 +1,5 @@
const assert = require('node:assert/strict');
const { createHash } = require('node:crypto');
const { test } = require('node:test');
const {
@@ -33,6 +34,12 @@ const {
const {
storedJsonEquals,
} = require('../dist/copilot/failure-diagnosis/admission/postgresRepository.js');
const {
COPILOT_FAILURE_DIAGNOSIS_TOOL_UNLOCK_RECEIPT_SCHEMA,
createCopilotFailureDiagnosisToolUnlockCommand,
normalizeCopilotFailureDiagnosisToolUnlockCommand,
restoreCopilotFailureDiagnosisTrustedToolAuthority,
} = require('../dist/copilot/failure-diagnosis/toolExecution.js');
const DIGEST_A = 'a'.repeat(64);
const DIGEST_B = 'b'.repeat(64);
@@ -189,6 +196,40 @@ async function plan(options = {}) {
});
}
function successfulToolCompletion(current) {
const unsigned = Object.freeze({
schema: 'qinglong/tool-execution-completion@v1',
startId: 'cds:diagnosis-tool-start',
projectId: current.projectId,
runId: current.runId,
stepRunId: current.toolStepRunId,
startedStepRunVersion: 2,
completedStepRunVersion: 3,
barrierDigest: DIGEST_A,
adapterDigest: DIGEST_B,
resultArtifact: Object.freeze({
artifactId: 'cdra:diagnosis-result',
artifactDigest: DIGEST_C,
outputDigest: DIGEST_A,
executionResultDigest: DIGEST_B,
}),
stepRunMutationId: 'cdscm:diagnosis-tool',
stepRunMutationDigest: DIGEST_C,
completedStepRunDigest: DIGEST_A,
runEventId: 'cdsce:diagnosis-tool',
completedAtMs: 3_000,
});
return Object.freeze({
...unsigned,
completionDigest: createHash('sha256')
.update(
Buffer.from('qinglong/tool-execution-completion-digest@v1\0', 'utf8'),
)
.update(JSON.stringify(unsigned))
.digest('hex'),
});
}
test('creates one independent diagnosis Run with ready Tool and pending Model Steps', async () => {
const current = await plan();
assert.equal(current.schema, COPILOT_FAILURE_DIAGNOSIS_EXECUTION_PLAN_SCHEMA);
@@ -224,6 +265,10 @@ test('creates one independent diagnosis Run with ready Tool and pending Model St
},
{ kind: 'tool', status: 'ready', sequence: 2 },
);
assert.equal(
bundle.toolStepMutation.stepRun.definitionRef,
'tool:qinglong.run.log.excerpt@1.0.0',
);
assert.deepEqual(
{
kind: bundle.modelStepMutation.stepRun.kind,
@@ -257,6 +302,63 @@ test('creates one independent diagnosis Run with ready Tool and pending Model St
);
});
test('restores the exact admitted Tool and binds success to one Model unlock mutation', async () => {
const current = await plan();
const admission = createCopilotFailureDiagnosisAdmissionBundle(current);
const authority = restoreCopilotFailureDiagnosisTrustedToolAuthority(
current,
snapshot(),
);
assert.equal(authority.plan.planDigest, current.tool.planDigest);
assert.equal(authority.binding.bindingDigest, current.tool.bindingDigest);
const command = createCopilotFailureDiagnosisToolUnlockCommand({
plan: current,
completion: successfulToolCompletion(current),
modelStepRun: admission.modelStepMutation.stepRun,
run: {
id: current.runId,
projectId: current.projectId,
status: 'running',
version: 5,
eventSequence: 5,
},
});
assert.equal(
command.receipt.schema,
COPILOT_FAILURE_DIAGNOSIS_TOOL_UNLOCK_RECEIPT_SCHEMA,
);
assert.equal(command.modelStepRunMutation.previousStatus, 'pending');
assert.equal(command.modelStepRunMutation.stepRun.status, 'ready');
assert.equal(command.receipt.finalRunVersion, 6);
assert.deepEqual(
normalizeCopilotFailureDiagnosisToolUnlockCommand(
JSON.parse(JSON.stringify(command)),
),
command,
);
assert.throws(
() =>
normalizeCopilotFailureDiagnosisToolUnlockCommand({
...command,
receipt: { ...command.receipt, finalRunVersion: 7 },
}),
TypeError,
);
});
test('publishes Tool execution only through explicit AI subpaths', () => {
const root = require('../dist');
const execution = require('@qinglong/ai/failure-diagnosis-tool-execution');
const storage = require('@qinglong/ai/postgres-failure-diagnosis-tool-execution-storage');
assert.equal(root.executeCopilotFailureDiagnosisTool, undefined);
assert.equal(typeof execution.executeCopilotFailureDiagnosisTool, 'function');
assert.equal(
typeof storage.PostgresCopilotFailureDiagnosisToolUnlockRepository,
'function',
);
});
test('rejects a Tool input detached from the source failure fence', async () => {
await assert.rejects(
() => plan({ inputAttemptId: 'attempt-unrelated' }),
@@ -20,6 +20,7 @@ const {
LOCAL_MODEL_PRICE_CATALOG_AUTHORIZATION_MIGRATION_ID,
LOCAL_MODEL_PRICE_CATALOG_MIGRATION_ID,
POSTGRES_COPILOT_FAILURE_DIAGNOSIS_ADMISSION_MIGRATION_ID,
POSTGRES_COPILOT_FAILURE_DIAGNOSIS_TOOL_UNLOCK_MIGRATION_ID,
POSTGRES_MODEL_INVOCATION_MIGRATION_ID,
POSTGRES_MODEL_INVOCATION_MIGRATION_HISTORY_TABLE,
POSTGRES_MODEL_INVOCATION_MIGRATION_STREAM_ID,
@@ -426,6 +427,10 @@ test('PostgreSQL AI schema is an independent reviewed feature stream', async ()
POSTGRES_COPILOT_FAILURE_DIAGNOSIS_ADMISSION_MIGRATION_ID,
'pg-9018-ai-copilot-failure-diagnosis-admissions',
);
assert.equal(
POSTGRES_COPILOT_FAILURE_DIAGNOSIS_TOOL_UNLOCK_MIGRATION_ID,
'pg-9019-ai-copilot-failure-diagnosis-tool-unlocks',
);
assert.equal(
POSTGRES_MODEL_INVOCATION_MIGRATION_HISTORY_TABLE,
'ai_schema_migrations',
@@ -507,9 +512,13 @@ test('PostgreSQL AI schema is an independent reviewed feature stream', async ()
postgresModelInvocationMigrationDefinition.migrations[17].checksum,
'cd0837c68ecc6c2bce58d048308d0239397b6347b4483f02372f966c05ae7ad6',
);
assert.equal(
postgresModelInvocationMigrationDefinition.migrations[18].checksum,
'341733fde691e81efafabc9749197514d83af6c851e91ea55d85074d2dec6b4f',
);
assert.equal(
postgresModelInvocationMigrationDefinition.migrations.length,
18,
19,
);
const diagnosisAdmissionStatements = [];
@@ -537,6 +546,25 @@ test('PostgreSQL AI schema is an independent reviewed feature stream', async ()
assert.match(diagnosisAdmissionSql, /TO ql3_runtime/);
assert.doesNotMatch(diagnosisAdmissionSql, /GRANT[^;]*(?:UPDATE|DELETE)/);
const diagnosisToolUnlockStatements = [];
await postgresModelInvocationMigrationDefinition.migrations[18].up({
async query(statement) {
diagnosisToolUnlockStatements.push(statement);
return { rows: [] };
},
});
const diagnosisToolUnlockSql = diagnosisToolUnlockStatements.join('\n');
assert.match(
diagnosisToolUnlockSql,
/CREATE TABLE "ql3_ai"\."copilot_failure_diagnosis_tool_unlocks"/,
);
assert.match(
diagnosisToolUnlockSql,
/FOREIGN KEY \(start_id\)[\s\S]*tool_execution_completions/,
);
assert.match(diagnosisToolUnlockSql, /TO ql3_runtime/);
assert.doesNotMatch(diagnosisToolUnlockSql, /GRANT[^;]*(?:UPDATE|DELETE)/);
const retirementStatements = [];
await postgresModelInvocationMigrationDefinition.migrations[10].up({
async query(statement) {
@@ -175,7 +175,7 @@ test('readiness binds exact migration history and least-privilege primary author
assert.equal(report.ready, true);
assert.equal(
report.migrationIds.at(-1),
'pg-9018-ai-copilot-failure-diagnosis-admissions',
'pg-9019-ai-copilot-failure-diagnosis-tool-unlocks',
);
assert.match(
queries[1],
@@ -277,7 +277,7 @@ test('tester readiness freezes migration history and least privilege', async ()
assert.equal(ready.ready, true);
assert.equal(
ready.migrationIds.at(-1),
'pg-9018-ai-copilot-failure-diagnosis-admissions',
'pg-9019-ai-copilot-failure-diagnosis-tool-unlocks',
);
await assert.rejects(