统一:DSH 智能分析、Top100 与图表静默恢复
接入 DSH 专用运行时和对话界面,保留模型原始回答,支持会话分页、连续追问与显式报告。 补齐组合筛选、各维度 Top100、分组查询到排名图的转换,图表故障静默省略并基于保存数据自动恢复。 优化报告风格、图表顺序和表格换行,更新接入说明及构建输入。 验证:独立目录前后端构建通过;118 项 Java、102 项前端、102 项图表、14 项 DSH 应用和 13 项组合筛选检查通过。 仅提交相关源码、锁定依赖与测试;不包含凭据、会话、业务数据或本地产物。
This commit is contained in:
@@ -0,0 +1,339 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createAnalysisSessionClient } from '../src/agent/sessionClient.ts';
|
||||
|
||||
function deferred() {
|
||||
let resolve, reject;
|
||||
const promise = new Promise((yes, no) => { resolve = yes; reject = no; });
|
||||
return { promise, resolve, reject };
|
||||
}
|
||||
|
||||
async function until(condition, description) {
|
||||
for (let i = 0; i < 100; i++) {
|
||||
if (condition()) return;
|
||||
await new Promise(resolve => setImmediate(resolve));
|
||||
}
|
||||
assert.fail(description || 'The expected session state was not reached');
|
||||
}
|
||||
|
||||
const turn = (id, status = 'COMPLETED', extras = {}) => ({
|
||||
id, status, question: 'Question ' + id, createdAt: '2026-11-11T04:05:06.001234Z', ...extras,
|
||||
});
|
||||
const snapshot = (id, turns, before = '', activeTurnId = '') => ({
|
||||
id, title: id, turns, before, activeTurnId, context: { releaseId: 'release' },
|
||||
});
|
||||
|
||||
function host(pages, overrides = {}) {
|
||||
const connections = [];
|
||||
const calls = { pages: [], cancels: [], starts: [], creates: [] };
|
||||
const transport = {
|
||||
analysisSessions: async () => ({ items: Object.keys(pages).map(id => ({ id, title: id })) }),
|
||||
analysisSessionFollow: (id, { signal, onEvent }) => {
|
||||
const stream = deferred();
|
||||
const entry = { id, signal, emit: onEvent, close: stream.resolve, fail: stream.reject };
|
||||
connections.push(entry);
|
||||
signal.addEventListener('abort', () => stream.resolve(), { once: true });
|
||||
if (pages[id]) onEvent({ event: 'snapshot', data: pages[id] });
|
||||
return stream.promise;
|
||||
},
|
||||
analysisSessionPage: async (id, before) => {
|
||||
calls.pages.push([id, before]);
|
||||
return pages[id];
|
||||
},
|
||||
analysisTurn: async () => { throw new Error('Unexpected standalone turn request'); },
|
||||
analysisTurnEvents: async () => { throw new Error('Unexpected fallback stream'); },
|
||||
createAnalysisSession: async body => {
|
||||
calls.creates.push(body);
|
||||
return { session: { id: 'new', releaseId: 'release' } };
|
||||
},
|
||||
startAnalysisTurn: async (id, body) => {
|
||||
calls.starts.push([id, body]);
|
||||
return { turn: { id: 'new-turn', sessionId: id, status: 'QUEUED' } };
|
||||
},
|
||||
cancelAnalysisTurn: async id => {
|
||||
calls.cancels.push(id);
|
||||
return { turnId: id, status: 'CANCELLED', cancelRequested: true };
|
||||
},
|
||||
...overrides,
|
||||
};
|
||||
const client = createAnalysisSessionClient({ userId: 'actor', transport });
|
||||
return { client, connections, calls };
|
||||
}
|
||||
|
||||
test('the follow stream opens the tail in one snapshot and older pages cannot duplicate a focused turn', async t => {
|
||||
const pages = { A: snapshot('A', [turn('5'), turn('6')], '5') };
|
||||
let fullHistoryRequests = 0;
|
||||
const detail = { ...turn('2'), sessionId: 'A', answer: 'historical' };
|
||||
const { client } = host(pages, {
|
||||
analysisTurn: async id => { assert.equal(id, '2'); return detail; },
|
||||
analysisSessionPage: async (id, before) => {
|
||||
fullHistoryRequests++;
|
||||
assert.deepEqual([id, before], ['A', '5']);
|
||||
return snapshot('A', [turn('1'), turn('2', 'COMPLETED', { result: { answer: 'refreshed history' } }), turn('3'), turn('4')]);
|
||||
},
|
||||
});
|
||||
t.after(() => client.dispose());
|
||||
assert.equal(await client.openSession('A', '2'), true);
|
||||
const initial = client.getSnapshot();
|
||||
assert.equal(client.getSnapshot(), initial);
|
||||
assert.equal(fullHistoryRequests, 0);
|
||||
assert.equal(initial.historyLoading, false);
|
||||
assert.equal(initial.focusTurnId, '2');
|
||||
assert.deepEqual(initial.turns.map(row => row.id), ['2', '5', '6']);
|
||||
assert.equal(await client.loadOlder(), true);
|
||||
const expanded = client.getSnapshot();
|
||||
assert.equal(expanded.olderCursor, '');
|
||||
assert.deepEqual(expanded.turns.map(row => row.id), ['1', '2', '3', '4', '5', '6']);
|
||||
assert.equal(expanded.turns[1].answer, 'refreshed history');
|
||||
});
|
||||
|
||||
test('draft replacements remain ephemeral and large durable cursors reject old events', async t => {
|
||||
const pages = { A: snapshot('A', [turn('1', 'RUNNING', { eventCursor: '9007199254740994' })], '', '1') };
|
||||
const { client, connections } = host(pages);
|
||||
t.after(() => client.dispose());
|
||||
await client.openSession('A');
|
||||
const { emit } = connections[0];
|
||||
emit({ event: 'draft', data: { turnId: '1', attemptId: 'p', text: 'unverified draft' } });
|
||||
const draft = client.getSnapshot().turns[0];
|
||||
assert.equal(draft.dshDraft, 'unverified draft');
|
||||
assert.equal(draft.answer, undefined);
|
||||
emit({ id: '9007199254740993', event: 'progress', data: { turnId: '1', message: 'stale event' } });
|
||||
assert.equal(client.getSnapshot().turns[0], draft);
|
||||
emit({ id: '9007199254740995', event: 'tool_result', data: { turnId: '1', status: 'COMPLETED', steps: [{ name: 'metric' }] } });
|
||||
assert.equal(client.getSnapshot().busy, true);
|
||||
assert.equal(client.getSnapshot().turns[0].eventCursor, '9007199254740995');
|
||||
assert.equal(client.getSnapshot().turns[0].dshDraft, 'unverified draft');
|
||||
emit({ id: '9007199254740996', event: 'completed', data: { turnId: '1', result: { answer: 'checked answer' } } });
|
||||
emit({ event: 'draft', data: { turnId: '1', attemptId: 'p', text: 'late draft' } });
|
||||
const result = client.getSnapshot().turns[0];
|
||||
assert.equal(result.dshDraft, undefined);
|
||||
assert.equal(result.result.answer, 'checked answer');
|
||||
assert.equal(result.status, 'COMPLETED');
|
||||
assert.equal(client.getSnapshot().busy, false);
|
||||
});
|
||||
|
||||
test('switching and suspending abort reads without stopping a turn or accepting stale data', async t => {
|
||||
const older = deferred();
|
||||
const pages = { A: snapshot('A', [turn('4', 'RUNNING')], '4', '4'), B: snapshot('B', [turn('10')]) };
|
||||
const { client, connections, calls } = host(pages, { analysisSessionPage: async () => older.promise });
|
||||
t.after(() => client.dispose());
|
||||
await client.openSession('A');
|
||||
const loading = client.loadOlder();
|
||||
await client.openSession('B');
|
||||
const replacement = client.getSnapshot();
|
||||
assert.equal(connections[0].signal.aborted, true);
|
||||
older.resolve(snapshot('A', [turn('1'), turn('2'), turn('3')]));
|
||||
assert.equal(await loading, false);
|
||||
connections[0].emit({ event: 'draft', data: { turnId: '4', attemptId: 'a', text: 'unselected draft' } });
|
||||
assert.equal(client.getSnapshot(), replacement);
|
||||
client.suspend();
|
||||
assert.equal(connections[1].signal.aborted, true);
|
||||
assert.deepEqual(calls.cancels, []);
|
||||
client.resume();
|
||||
await until(() => connections.length === 3);
|
||||
assert.equal(connections[2].id, 'B');
|
||||
});
|
||||
|
||||
test('an uncertain submission reuses its receipt even after the server assigns the release', async t => {
|
||||
const bodies = [];
|
||||
const pages = { new: snapshot('new', []) };
|
||||
const { client, calls } = host(pages, {
|
||||
startAnalysisTurn: async (id, body) => {
|
||||
assert.equal(id, 'new');
|
||||
bodies.push(body);
|
||||
if (bodies.length === 1) throw new Error('Connection ended before receipt');
|
||||
pages.new = snapshot('new', [turn('new-turn', 'QUEUED')], '', 'new-turn');
|
||||
return { turn: { id: 'new-turn', sessionId: id, status: 'QUEUED' } };
|
||||
},
|
||||
});
|
||||
t.after(() => client.dispose());
|
||||
assert.equal(await client.submit('同一个问题'), false);
|
||||
assert.equal(client.getSnapshot().retryQuestion, '同一个问题');
|
||||
assert.equal(client.getSnapshot().releaseId, 'release');
|
||||
assert.equal(await client.submit('同一个问题', { releaseId: client.getSnapshot().releaseId }), true);
|
||||
assert.deepEqual(bodies[0], bodies[1]);
|
||||
assert.equal(calls.creates.length, 1);
|
||||
});
|
||||
|
||||
test('a pending stop follows the accepted receipt across navigation and does not cancel the new session', async t => {
|
||||
const receipt = deferred();
|
||||
let posting = false;
|
||||
const pages = { A: snapshot('A', []), B: snapshot('B', [turn('b', 'RUNNING')], '', 'b') };
|
||||
const { client, calls } = host(pages, {
|
||||
startAnalysisTurn: () => { posting = true; return receipt.promise; },
|
||||
});
|
||||
t.after(() => client.dispose());
|
||||
await client.openSession('A');
|
||||
const sending = client.submit('first');
|
||||
await until(() => posting);
|
||||
assert.equal(await client.cancel(), true);
|
||||
await client.openSession('B');
|
||||
receipt.resolve({ turn: { id: 'a', sessionId: 'A', status: 'RUNNING' } });
|
||||
assert.equal(await sending, true);
|
||||
assert.deepEqual(calls.cancels, ['a']);
|
||||
assert.equal(client.getSnapshot().activeTurnId, 'b');
|
||||
assert.equal(client.getSnapshot().sessionId, 'B');
|
||||
});
|
||||
|
||||
test('the queued prompt waits for the durable completion and is submitted once with the new baseline context', async t => {
|
||||
const pages = { A: snapshot('A', [turn('1', 'RUNNING', { eventCursor: '10' })], '', '1') };
|
||||
const { client, connections, calls } = host(pages);
|
||||
t.after(() => client.dispose());
|
||||
await client.openSession('A');
|
||||
assert.equal(await client.submit('next'), true);
|
||||
assert.equal(client.getSnapshot().queuedQuestion, 'next');
|
||||
assert.equal(calls.starts.length, 0);
|
||||
const first = connections[0];
|
||||
pages.A = { ...snapshot('A', [turn('1', 'COMPLETED')]), context: { releaseId: 'release', stage: 'black' } };
|
||||
first.emit({ id: '11', event: 'completed', data: { turnId: '1', result: { answer: 'checked' } } });
|
||||
first.close();
|
||||
await until(() => connections.length >= 2);
|
||||
connections[1].close();
|
||||
await until(() => calls.starts.length === 1);
|
||||
assert.equal(calls.starts[0][1].question, 'next');
|
||||
assert.equal(calls.starts[0][1].context.stage, 'black');
|
||||
assert.equal(client.getSnapshot().queuedQuestion, '');
|
||||
});
|
||||
|
||||
test('restarting a saved turn drops the previous stream before reusing the same turn ID', async t => {
|
||||
const receipt = deferred();
|
||||
const pages = { A: snapshot('A', [turn('1', 'FAILED')]) };
|
||||
const { client, connections } = host(pages, { retryAnalysisTurn: () => receipt.promise });
|
||||
t.after(() => client.dispose());
|
||||
await client.openSession('A');
|
||||
const pending = client.retry('1');
|
||||
assert.equal(connections[0].signal.aborted, true);
|
||||
connections[0].emit({ id: '100', event: 'completed', data: { turnId: '1', result: { answer: 'old attempt' } } });
|
||||
assert.equal(client.getSnapshot().turns[0].status, 'SUBMITTING');
|
||||
pages.A = snapshot('A', [turn('1', 'QUEUED', { eventCursor: '110' })], '', '1');
|
||||
receipt.resolve({ turn: { id: '1', sessionId: 'A', status: 'QUEUED' } });
|
||||
assert.equal(await pending, true);
|
||||
await until(() => connections.length === 2);
|
||||
assert.equal(client.getSnapshot().turns[0].answer, undefined);
|
||||
assert.equal(client.getSnapshot().turns[0].status, 'QUEUED');
|
||||
});
|
||||
|
||||
test('the full original model answer survives summaries, draft clearing, and replacement snapshots', async t => {
|
||||
const original = ' # 模型原文\n\n' + '完整分析内容。'.repeat(900) + '\n';
|
||||
const pages = { A: snapshot('A', [turn('1', 'RUNNING', { eventCursor: '1' })], '', '1') };
|
||||
const { client, connections } = host(pages);
|
||||
t.after(() => client.dispose());
|
||||
await client.openSession('A');
|
||||
const { emit } = connections[0];
|
||||
emit({ id: '2', event: 'progress', data: { turnId: '1', modelAnswer: original } });
|
||||
emit({ event: 'draft', data: { turnId: '1', attemptId: 'attempt', text: 'temporary draft' } });
|
||||
emit({ event: 'draft', data: { turnId: '1', attemptId: 'attempt', text: '' } });
|
||||
assert.equal(client.getSnapshot().turns[0].modelAnswer, original);
|
||||
assert.equal(client.getSnapshot().turns[0].dshDraft, undefined);
|
||||
|
||||
emit({ id: '3', event: 'completed', data: { turnId: '1', result: { answer: 'fixed summary' } } });
|
||||
assert.equal(client.getSnapshot().turns[0].modelAnswer, original);
|
||||
emit({ event: 'snapshot', data: snapshot('A', [turn('1', 'COMPLETED', { answer: 'another summary', eventCursor: '3' })]) });
|
||||
const persisted = client.getSnapshot().turns[0];
|
||||
assert.equal(persisted.modelAnswer, original);
|
||||
assert.equal(persisted.status, 'COMPLETED');
|
||||
});
|
||||
|
||||
test('cancelled and failed tasks retain the latest prose and accept a later full original', async t => {
|
||||
for (const status of ['CANCELLED', 'FAILED', 'PARTIAL']) {
|
||||
const pages = { A: snapshot('A', [turn('1', 'RUNNING', { eventCursor: '1' })], '', '1') };
|
||||
const { client, connections } = host(pages);
|
||||
t.after(() => client.dispose());
|
||||
await client.openSession('A');
|
||||
const { emit } = connections[0];
|
||||
emit({ event: 'draft', data: { turnId: '1', attemptId: 'attempt', text: '模型已输出的原文' } });
|
||||
emit({ id: '2', event: status.toLowerCase(), data: { turnId: '1', status, result: { answer: 'lifecycle message' } } });
|
||||
let row = client.getSnapshot().turns[0];
|
||||
assert.equal(row.modelAnswer, '模型已输出的原文');
|
||||
assert.equal(row.dshDraft, undefined);
|
||||
assert.equal(row.status, status);
|
||||
|
||||
emit({ id: '3', event: status.toLowerCase(), data: {
|
||||
turnId: '1', status, result: { modelAnswer: '模型已输出的完整原文\n包含末尾文本', answer: 'summary' },
|
||||
} });
|
||||
row = client.getSnapshot().turns[0];
|
||||
assert.equal(row.modelAnswer, '模型已输出的完整原文\n包含末尾文本');
|
||||
assert.equal(row.eventCursor, '3');
|
||||
assert.equal(row.status, status);
|
||||
}
|
||||
});
|
||||
|
||||
test('history and older-page refresh preserve explicit raw originals independently of summary answers', async t => {
|
||||
const pages = { A: snapshot('A', [
|
||||
turn('1', 'COMPLETED', { result: { modelAnswer: ' 已保存原文\n', answer: 'summary' } }),
|
||||
turn('2'),
|
||||
], '2') };
|
||||
const { client } = host(pages, {
|
||||
analysisSessionPage: async () => snapshot('A', [turn('1', 'COMPLETED', { result: { answer: 'updated summary', modelAnswer: '' } })]),
|
||||
});
|
||||
t.after(() => client.dispose());
|
||||
await client.openSession('A');
|
||||
assert.equal(client.getSnapshot().turns[0].modelAnswer, ' 已保存原文\n');
|
||||
await client.loadOlder();
|
||||
assert.equal(client.getSnapshot().turns[0].modelAnswer, ' 已保存原文\n');
|
||||
});
|
||||
|
||||
test('retry clears the previous attempt original before the new attempt starts', async t => {
|
||||
const receipt = deferred();
|
||||
const pages = { A: snapshot('A', [turn('1', 'FAILED', { modelAnswer: 'previous attempt', dshDraft: 'old intermediate' })]) };
|
||||
const { client } = host(pages, { retryAnalysisTurn: () => receipt.promise });
|
||||
t.after(() => client.dispose());
|
||||
await client.openSession('A');
|
||||
const pending = client.retry('1');
|
||||
assert.equal(client.getSnapshot().turns[0].modelAnswer, undefined);
|
||||
assert.equal(client.getSnapshot().turns[0].dshDraft, undefined);
|
||||
pages.A = snapshot('A', [turn('1', 'QUEUED')], '', '1');
|
||||
receipt.resolve({ turn: { id: '1', sessionId: 'A', status: 'QUEUED' } });
|
||||
await pending;
|
||||
assert.equal(client.getSnapshot().turns[0].modelAnswer, undefined);
|
||||
});
|
||||
|
||||
test('cancel refreshes the bounded final original tail after the terminal follow closes', async t => {
|
||||
const original = 'original before cancellation';
|
||||
const pages = { A: snapshot('A', [turn('1', 'RUNNING', { modelAnswer: original, attemptId: 'attempt' })], '', '1') };
|
||||
let reads = 0;
|
||||
const { client } = host(pages, {
|
||||
cancelAnalysisTurn: async () => {
|
||||
pages.A = snapshot('A', [turn('1', 'CANCELLED', { modelAnswer: original, attemptId: 'attempt' })]);
|
||||
return { status: 'CANCELLED' };
|
||||
},
|
||||
analysisTurn: async () => {
|
||||
reads++;
|
||||
return { ...turn('1', 'CANCELLED', { attemptId: 'attempt', modelAnswer: reads === 1 ? original : original + '\nfinal 250ms tail' }), sessionId: 'A' };
|
||||
},
|
||||
});
|
||||
t.after(() => client.dispose());
|
||||
await client.openSession('A');
|
||||
await client.cancel();
|
||||
await new Promise(resolve => setTimeout(resolve, 1100));
|
||||
assert.equal(reads, 2);
|
||||
assert.equal(client.getSnapshot().turns[0].modelAnswer, original + '\nfinal 250ms tail');
|
||||
});
|
||||
|
||||
test('a cancel-tail detail already in flight cannot overwrite a retried attempt', async t => {
|
||||
const detail = deferred();
|
||||
const pages = { A: snapshot('A', [turn('1', 'RUNNING', { attemptId: 'old' })], '', '1') };
|
||||
let reads = 0;
|
||||
const { client } = host(pages, {
|
||||
cancelAnalysisTurn: async () => {
|
||||
pages.A = snapshot('A', [turn('1', 'CANCELLED', { attemptId: 'old' })]);
|
||||
return { status: 'CANCELLED' };
|
||||
},
|
||||
analysisTurn: () => { reads++; return detail.promise; },
|
||||
retryAnalysisTurn: async () => {
|
||||
const next = turn('1', 'QUEUED', { attemptId: 'new' });
|
||||
pages.A = snapshot('A', [next], '', '1');
|
||||
return { turn: next };
|
||||
},
|
||||
});
|
||||
t.after(() => client.dispose());
|
||||
await client.openSession('A');
|
||||
await client.cancel();
|
||||
await new Promise(resolve => setTimeout(resolve, 400));
|
||||
assert.equal(reads, 1);
|
||||
await client.retry('1');
|
||||
detail.resolve({ ...turn('1', 'CANCELLED', { attemptId: 'old', modelAnswer: 'stale old tail' }), sessionId: 'A' });
|
||||
await new Promise(resolve => setImmediate(resolve));
|
||||
assert.equal(client.getSnapshot().turns[0].modelAnswer, undefined);
|
||||
assert.equal(client.getSnapshot().turns[0].status, 'QUEUED');
|
||||
});
|
||||
Reference in New Issue
Block a user