mirror of
https://github.com/whyour/qinglong.git
synced 2026-09-20 16:07:11 +08:00
feat(ql3): compose cluster copilot diagnosis
This commit is contained in:
@@ -13,6 +13,18 @@ const {
|
||||
canonicalPluginPackagePromptOutputKeyringManifest,
|
||||
PLUGIN_PACKAGE_PROMPT_OUTPUT_KEYRING_MANIFEST_SCHEMA,
|
||||
} = require('@qinglong/ai/plugin-package-prompt-output-keyring-manifest');
|
||||
const {
|
||||
canonicalClusterToolInvocationKeyringManifest,
|
||||
CLUSTER_TOOL_INVOCATION_KEYRING_MANIFEST_SCHEMA,
|
||||
} = require('@qinglong/cluster-control/trusted-tool-invocation-keyring');
|
||||
const {
|
||||
canonicalClusterToolResultKeyringManifest,
|
||||
CLUSTER_TOOL_RESULT_KEYRING_MANIFEST_SCHEMA,
|
||||
} = require('@qinglong/cluster-control/trusted-tool-result-keyring');
|
||||
const {
|
||||
canonicalClusterCopilotFailureDiagnosisOutputKeyringManifest,
|
||||
CLUSTER_COPILOT_FAILURE_DIAGNOSIS_OUTPUT_KEYRING_MANIFEST_SCHEMA,
|
||||
} = require('@qinglong/cluster-control/failure-diagnosis-output-keyring');
|
||||
|
||||
function enabledEnvironment(overrides = {}) {
|
||||
return {
|
||||
@@ -74,6 +86,161 @@ test('AI config is fail-closed and bounded behind the explicit process flag', ()
|
||||
),
|
||||
/QL3_CLUSTER_AI_PROMPT_OUTPUT_KEYRING_ROOT is invalid/,
|
||||
);
|
||||
assert.throws(
|
||||
() =>
|
||||
loadProductionClusterAiConfig(
|
||||
enabledEnvironment({ QL3_CLUSTER_AI_COPILOT_ENABLED: 'true' }),
|
||||
),
|
||||
/QL3_CLUSTER_AI_COPILOT_CONFIG_FILE is invalid/,
|
||||
);
|
||||
assert.deepEqual(
|
||||
loadProductionClusterAiConfig(
|
||||
enabledEnvironment({
|
||||
QL3_CLUSTER_AI_COPILOT_ENABLED: 'true',
|
||||
QL3_CLUSTER_AI_COPILOT_CONFIG_FILE: '/run/ql3/copilot/config.json',
|
||||
QL3_CLUSTER_AI_COPILOT_INVOCATION_KEYRING_ROOT: '/run/ql3/invocation',
|
||||
QL3_CLUSTER_AI_COPILOT_RESULT_KEYRING_ROOT: '/run/ql3/result',
|
||||
QL3_CLUSTER_AI_COPILOT_OUTPUT_KEYRING_ROOT: '/run/ql3/output',
|
||||
}),
|
||||
).copilot,
|
||||
{
|
||||
configFile: '/run/ql3/copilot/config.json',
|
||||
invocationKeyringRootDirectory: '/run/ql3/invocation',
|
||||
resultKeyringRootDirectory: '/run/ql3/result',
|
||||
outputKeyringRootDirectory: '/run/ql3/output',
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
async function projectedFile(root, name, bytes) {
|
||||
await writeFile(join(root, name), bytes, { mode: 0o440 });
|
||||
await chmod(join(root, name), 0o440);
|
||||
}
|
||||
|
||||
test('Copilot composition is explicit, shares the Prompt gateway and exposes no route', async () => {
|
||||
const secretRoot = await mkdtemp(join(tmpdir(), 'ql3-cluster-ai-secret-'));
|
||||
const configRoot = await mkdtemp(join(tmpdir(), 'ql3-copilot-config-'));
|
||||
const invocationRoot = await mkdtemp(join(tmpdir(), 'ql3-copilot-invocation-'));
|
||||
const resultRoot = await mkdtemp(join(tmpdir(), 'ql3-copilot-result-'));
|
||||
const outputRoot = await mkdtemp(join(tmpdir(), 'ql3-copilot-output-'));
|
||||
const key = Buffer.alloc(32, 0x55).toString('base64url');
|
||||
const config = Buffer.from(`${JSON.stringify({
|
||||
schema: 'qinglong/cluster-copilot-failure-diagnosis-config@v1',
|
||||
provider: 'provider-primary',
|
||||
model: 'model-diagnosis',
|
||||
modelBoundary: 'external',
|
||||
responseLanguage: 'zh-CN',
|
||||
maxOutputTokens: 512,
|
||||
executionTimeoutMs: 60_000,
|
||||
egressPolicy: {
|
||||
schema: 'qinglong/copilot-model-egress-policy@v1',
|
||||
revision: 'cluster-copilot-v1',
|
||||
potentiallySensitiveDataBoundaries: ['external'],
|
||||
maxInputBytes: 64 * 1024,
|
||||
maxOutputTokens: 1024,
|
||||
},
|
||||
})}\n`);
|
||||
const invocation = canonicalClusterToolInvocationKeyringManifest({
|
||||
schema: CLUSTER_TOOL_INVOCATION_KEYRING_MANIFEST_SCHEMA,
|
||||
activeKeyId: 'invocation-key-1',
|
||||
keys: { 'invocation-key-1': key },
|
||||
});
|
||||
const result = canonicalClusterToolResultKeyringManifest({
|
||||
schema: CLUSTER_TOOL_RESULT_KEYRING_MANIFEST_SCHEMA,
|
||||
keys: { 'result-key-1': key },
|
||||
});
|
||||
const output = canonicalClusterCopilotFailureDiagnosisOutputKeyringManifest({
|
||||
schema: CLUSTER_COPILOT_FAILURE_DIAGNOSIS_OUTPUT_KEYRING_MANIFEST_SCHEMA,
|
||||
activeKeyId: 'output-key-1',
|
||||
keys: { 'output-key-1': key },
|
||||
});
|
||||
const gateway = {
|
||||
generate() {},
|
||||
supportsSuccessfulCompletionSink(sink) {
|
||||
return sink === registeredSink;
|
||||
},
|
||||
};
|
||||
const fakePool = { query() {}, connect() {} };
|
||||
const artifactStore = { put() {}, inspect() {}, readLogRange() {} };
|
||||
const copilot = Object.freeze({ execute() {} });
|
||||
let registeredSink;
|
||||
let created;
|
||||
let controlOptions;
|
||||
try {
|
||||
await Promise.all([
|
||||
projectedFile(configRoot, 'config.json', config),
|
||||
projectedFile(invocationRoot, 'keyring.json', invocation),
|
||||
projectedFile(resultRoot, 'keyring.json', result),
|
||||
projectedFile(outputRoot, 'keyring.json', output),
|
||||
]);
|
||||
const application = await startProductionClusterAiControlApplication({
|
||||
control: {
|
||||
config: controlConfig(),
|
||||
workerIngress: {
|
||||
config: { enabled: true },
|
||||
artifactStore,
|
||||
},
|
||||
},
|
||||
ai: {
|
||||
enabled: true,
|
||||
providerAuthorityFile: '/unused/providers.json',
|
||||
secretRootDirectory: secretRoot,
|
||||
copilot: {
|
||||
configFile: join(configRoot, 'config.json'),
|
||||
invocationKeyringRootDirectory: invocationRoot,
|
||||
resultKeyringRootDirectory: resultRoot,
|
||||
outputKeyringRootDirectory: outputRoot,
|
||||
},
|
||||
maxConcurrent: 1,
|
||||
recoveryLimit: 1,
|
||||
databaseMaxConnections: 1,
|
||||
},
|
||||
audit() {},
|
||||
async openAiDatabase() {
|
||||
return { pool: fakePool, async close() {} };
|
||||
},
|
||||
async bootstrapPrompt(options) {
|
||||
await options.openDatabase();
|
||||
registeredSink = options.createAdditionalSuccessfulCompletion({
|
||||
async recordWithAtomicSuccess() {},
|
||||
});
|
||||
return {
|
||||
status: 'active', profile: 'cluster', readiness: {}, capability: gateway,
|
||||
prompts: {}, promptCatalog: {}, promptExecutions: {},
|
||||
promptExecutionInspections: {}, async stop() { return 'stopped'; },
|
||||
};
|
||||
},
|
||||
async createCopilot(options) {
|
||||
created = options;
|
||||
return copilot;
|
||||
},
|
||||
async startControl(options) {
|
||||
controlOptions = options;
|
||||
return {
|
||||
status: 'active', address: { host: '127.0.0.1', port: 5800 },
|
||||
evidence: {}, recovery: { safe: true, remaining: 0, failed: 0 },
|
||||
unavailable: new Promise(() => {}), availabilityStatus() { return 'ready'; },
|
||||
async stop() { return 'stopped'; },
|
||||
};
|
||||
},
|
||||
});
|
||||
assert.equal(application.copilot, copilot);
|
||||
assert.equal(created.pool, fakePool);
|
||||
assert.equal(created.gateway, gateway);
|
||||
assert.equal(created.successfulCompletion, registeredSink);
|
||||
assert.equal(created.artifactStore, artifactStore);
|
||||
assert.equal('copilot' in controlOptions, false);
|
||||
assert.equal(await application.stop(), 'stopped');
|
||||
} finally {
|
||||
config.fill(0); invocation.fill(0); result.fill(0); output.fill(0);
|
||||
await Promise.all([
|
||||
rm(secretRoot, { recursive: true, force: true }),
|
||||
rm(configRoot, { recursive: true, force: true }),
|
||||
rm(invocationRoot, { recursive: true, force: true }),
|
||||
rm(resultRoot, { recursive: true, force: true }),
|
||||
rm(outputRoot, { recursive: true, force: true }),
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
test('explicit AI composition injects one reviewed Prompt capability and drains it after HTTP control', async () => {
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
const assert = require('node:assert/strict');
|
||||
const { chmod, mkdtemp, rm, writeFile } = require('node:fs/promises');
|
||||
const { tmpdir } = require('node:os');
|
||||
const { join } = require('node:path');
|
||||
const { test } = require('node:test');
|
||||
|
||||
const {
|
||||
CLUSTER_COPILOT_FAILURE_DIAGNOSIS_CONFIG_SCHEMA,
|
||||
ClusterCopilotFailureDiagnosisCompositionError,
|
||||
canonicalClusterCopilotFailureDiagnosisConfig,
|
||||
loadClusterCopilotFailureDiagnosisConfig,
|
||||
normalizeClusterCopilotFailureDiagnosisConfig,
|
||||
} = require('@qinglong/cluster-control/copilot-production');
|
||||
|
||||
function config(overrides = {}) {
|
||||
return {
|
||||
schema: CLUSTER_COPILOT_FAILURE_DIAGNOSIS_CONFIG_SCHEMA,
|
||||
provider: 'provider-primary',
|
||||
model: 'model-diagnosis',
|
||||
modelBoundary: 'external',
|
||||
responseLanguage: 'zh-CN',
|
||||
maxOutputTokens: 512,
|
||||
executionTimeoutMs: 60_000,
|
||||
egressPolicy: {
|
||||
schema: 'qinglong/copilot-model-egress-policy@v1',
|
||||
revision: 'cluster-copilot-v1',
|
||||
potentiallySensitiveDataBoundaries: ['external'],
|
||||
maxInputBytes: 64 * 1024,
|
||||
maxOutputTokens: 1024,
|
||||
},
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
test('normalizes one bounded deployment-owned Copilot model intent', () => {
|
||||
const normalized = normalizeClusterCopilotFailureDiagnosisConfig(config());
|
||||
assert.equal(normalized.provider, 'provider-primary');
|
||||
assert.equal(normalized.executionTimeoutMs, 60_000);
|
||||
assert.equal(Object.isFrozen(normalized), true);
|
||||
assert.equal(Object.isFrozen(normalized.egressPolicy), true);
|
||||
assert.throws(
|
||||
() => normalizeClusterCopilotFailureDiagnosisConfig(config({ extra: true })),
|
||||
ClusterCopilotFailureDiagnosisCompositionError,
|
||||
);
|
||||
assert.throws(
|
||||
() => normalizeClusterCopilotFailureDiagnosisConfig(config({
|
||||
modelBoundary: 'on_device',
|
||||
})),
|
||||
/egress model boundaries/,
|
||||
);
|
||||
assert.throws(
|
||||
() => normalizeClusterCopilotFailureDiagnosisConfig(config({
|
||||
executionTimeoutMs: 300_001,
|
||||
})),
|
||||
/execution timeout/,
|
||||
);
|
||||
});
|
||||
|
||||
test('loads only canonical read-only projected Copilot configuration', async () => {
|
||||
const root = await mkdtemp(join(tmpdir(), 'ql3-copilot-config-test-'));
|
||||
const file = join(root, 'config.json');
|
||||
const canonical = canonicalClusterCopilotFailureDiagnosisConfig(config());
|
||||
try {
|
||||
await writeFile(file, canonical, { mode: 0o440 });
|
||||
await chmod(file, 0o440);
|
||||
assert.deepEqual(
|
||||
await loadClusterCopilotFailureDiagnosisConfig(file),
|
||||
normalizeClusterCopilotFailureDiagnosisConfig(config()),
|
||||
);
|
||||
|
||||
await chmod(file, 0o640);
|
||||
await writeFile(file, Buffer.from(`${JSON.stringify(config(), null, 2)}\n`), {
|
||||
mode: 0o440,
|
||||
});
|
||||
await chmod(file, 0o440);
|
||||
await assert.rejects(
|
||||
loadClusterCopilotFailureDiagnosisConfig(file),
|
||||
/not canonical/,
|
||||
);
|
||||
|
||||
await chmod(file, 0o640);
|
||||
await writeFile(file, canonical, { mode: 0o640 });
|
||||
await chmod(file, 0o640);
|
||||
await assert.rejects(
|
||||
loadClusterCopilotFailureDiagnosisConfig(file),
|
||||
ClusterCopilotFailureDiagnosisCompositionError,
|
||||
);
|
||||
} finally {
|
||||
canonical.fill(0);
|
||||
await rm(root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user