Add the read-only Fanyu diagnostic client (cluster_overview, task_diagnosis, request_trace, egress) with private file-based key loading, mutual CA verification, a 40 req/min token bucket, short TTL caches, and strict separation between unavailable telemetry and healthy state. Feed the last ten conversation turns into streaming answers, verify every reported fact against the server-side evidence ledger before persisting a diagnosis, withhold model thinking frames from the client stream, classify stream error codes, and cover secret isolation and diagnosis downgrades with dedicated unit tests.
138 lines
6.3 KiB
TypeScript
138 lines
6.3 KiB
TypeScript
import { mkdtempSync, readFileSync, writeFileSync } from 'node:fs';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
import type { Actor } from '@wishforge/contracts';
|
|
import { ControlPlane } from '../apps/api/src/domain/control-plane.js';
|
|
import { Connections } from '../apps/api/src/domain/connections.js';
|
|
import { Copilot } from '../apps/api/src/domain/copilot.js';
|
|
import { gatewayDiagnostics, type Transport } from '../apps/api/src/domain/copilot-diagnostics.js';
|
|
import type { StreamEvent } from '../apps/api/src/domain/copilot-stream.js';
|
|
import { MemoryStore } from './helpers/memory-store.js';
|
|
|
|
/**
|
|
* A whole streamed answer with production diagnostics on, and a gateway key that is fake but real
|
|
* enough: it is read from a kit file the way the server reads it, and the gateway puts it into an
|
|
* error. It must not reach the model, the browser, the transcript, the audit record or the log.
|
|
*/
|
|
const FAKE_KEY = 'fake-key-0123456789abcdef0123456789abcdef';
|
|
const owner: Actor = { tenantId: 'demo', userId: 'owner', role: 'owner', kind: 'human' };
|
|
const kit = mkdtempSync(join(tmpdir(), 'kit-'));
|
|
writeFileSync(join(kit, 'agent.key'), FAKE_KEY);
|
|
writeFileSync(join(kit, 'ca.crt'), 'ca');
|
|
|
|
const sse = (frames: unknown[]) =>
|
|
new Response(frames.map(frame => `data: ${JSON.stringify(frame)}\n\n`).join(''), {
|
|
status: 200,
|
|
headers: { 'content-type': 'text/event-stream' },
|
|
});
|
|
const toolCall = (id: string, name: string, input: unknown) =>
|
|
sse([
|
|
{ type: 'content_block_start', index: 0, content_block: { type: 'tool_use', id, name } },
|
|
{ type: 'content_block_delta', index: 0, delta: { type: 'input_json_delta', partial_json: JSON.stringify(input) } },
|
|
{ type: 'message_delta', delta: { stop_reason: 'tool_use' }, usage: { input_tokens: 1, output_tokens: 1 } },
|
|
]);
|
|
|
|
afterEach(() => vi.unstubAllGlobals());
|
|
|
|
describe('Copilot diagnostics key', () => {
|
|
it('stays out of every stream event, stored record, model request and log line', async () => {
|
|
const store = new MemoryStore();
|
|
store.seed('demo');
|
|
const base = {
|
|
NODE_ENV: 'test',
|
|
WF_DEPLOYMENT_MODE: 'development',
|
|
WF_PROVISIONER_BACKEND: 'local',
|
|
WF_TENANT_ID: 'demo',
|
|
WF_CLAUDE_BIN: '/usr/local/bin/claude',
|
|
WF_VAULT_KEY: 'a'.repeat(64),
|
|
WF_ANTHROPIC_BASE_URL: 'https://model.invalid',
|
|
WF_DIAGNOSTIC_KIT_DIR: kit,
|
|
};
|
|
const control = new ControlPlane(store, base);
|
|
const project = await control.createProject(owner, {
|
|
name: 'droboticflow',
|
|
repositoryUrl: 'https://example.org/d.git',
|
|
});
|
|
await new Connections(store, base).create(owner, {
|
|
provider: 'claude-code',
|
|
label: 'test',
|
|
apiKey: 'tenant-fixture-key',
|
|
});
|
|
const env = { ...base, WF_COPILOT_DIAGNOSTICS_PROJECTS: project.id };
|
|
const wiki = { snapshot: async () => ({ project, version: 'a'.repeat(40), files: [] }) };
|
|
const service = new Copilot(control, env, async () => ({}), wiki, undefined, {
|
|
open: async () => 'tenant-fixture-key',
|
|
});
|
|
|
|
// The gateway as the server sees it: the key comes from the kit file, and one failure carries it.
|
|
const transport: Transport = async path => {
|
|
const key = readFileSync(join(kit, 'agent.key'), 'utf8').trim();
|
|
if (path.startsWith('logs/')) throw new Error(`socket hang up (Authorization: Bearer ${key})`);
|
|
if (path.startsWith('tasks/'))
|
|
return {
|
|
status: 200,
|
|
body: { available: true, observedAt: '2026-11-10T17:40:00Z', data: { status: 'submitted', attempt: 1 } },
|
|
};
|
|
return { status: 503, body: { error: 'unavailable', detail: `token ${key} rejected upstream` } };
|
|
};
|
|
Object.assign(service, {
|
|
diagnosticsClient: gatewayDiagnostics(transport, { now: () => 0, sleep: async () => undefined }),
|
|
});
|
|
|
|
const requests: string[] = [];
|
|
let round = 0;
|
|
vi.stubGlobal('fetch', async (_url: string, init: RequestInit) => {
|
|
requests.push(String(init.body));
|
|
round += 1;
|
|
if (round === 1) return toolCall('t1', 'task_diagnosis', { taskId: 'annotation-task-abc' });
|
|
if (round === 2) return toolCall('t2', 'request_trace', { requestId: 'f'.repeat(32) });
|
|
if (round === 3) return toolCall('t3', 'cluster_overview', {});
|
|
return toolCall('t4', 'submit_answer', {
|
|
kind: 'bug',
|
|
answer: '已确认任务处于送检状态;请求记录暂时拿不到。',
|
|
goal: '送检后标注内容不丢失',
|
|
acceptanceCriteria: ['打回后再送检,标注仍在'],
|
|
diagnosis: {
|
|
verdict: 'problem',
|
|
facts: [{ text: '任务状态为 submitted', evidenceId: 'ev-1' }],
|
|
hypotheses: [{ text: '打回时切片被重置', confidence: 'medium' }],
|
|
missing: [],
|
|
},
|
|
});
|
|
});
|
|
const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined);
|
|
const error = vi.spyOn(console, 'error').mockImplementation(() => undefined);
|
|
const log = vi.spyOn(console, 'log').mockImplementation(() => undefined);
|
|
|
|
const events: StreamEvent[] = [];
|
|
const session = await service.createSession(owner, project.id);
|
|
await service.stream(
|
|
owner,
|
|
project.id,
|
|
session.id,
|
|
{ content: '打回再送检后标注全丢了', clientId: 'client-leak-abcdef' },
|
|
e => events.push(e),
|
|
);
|
|
const logged = [warn, error, log].flatMap(spy => spy.mock.calls.flat().map(String)).join('\n');
|
|
warn.mockRestore();
|
|
error.mockRestore();
|
|
log.mockRestore();
|
|
|
|
// The answer really went through all three lookups, and the failures stayed failures.
|
|
expect(
|
|
events.filter(e => e.type === 'tool_use' && e.phase === 'end').map(e => (e as { outcome?: string }).outcome),
|
|
).toEqual(['ok', 'missing', 'missing']);
|
|
const diagnosisCard = events.find(e => e.type === 'card' && e.cardType === 'diagnosis');
|
|
expect(diagnosisCard).toBeDefined();
|
|
expect(events.at(-1)?.type).toBe('done');
|
|
|
|
const stored = JSON.stringify([...store.state.get('demo')!.entities.values()]);
|
|
// The transcript and the audit record were written, so there was something to leak into.
|
|
expect(stored).toContain('"verdict":"problem"');
|
|
expect(stored).toContain('toolCalls');
|
|
for (const exit of [JSON.stringify(events), stored, requests.join('\n'), logged])
|
|
expect(exit).not.toContain(FAKE_KEY);
|
|
});
|
|
});
|