refactor: add canonical transcript assembly seam

This commit is contained in:
tommy0103
2026-07-21 00:58:34 +08:00
parent 3ee44de4e5
commit f79f1b3e3b
39 changed files with 1741 additions and 670 deletions
+70 -16
View File
@@ -1,21 +1,33 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { assembleSessionMessages } from '../app/src/shared/session-detail-assembly.mjs';
import { assembleSessionDetail } from '../app/src/shared/session-detail-assembly.mjs';
test('session assembly preserves thinking and attaches tool result and subagent evidence', () => {
const messages = [
{ uuid: 'thinking-1', type: 'assistant', content_type: 'thinking', text: 'reasoning' },
{ uuid: 'answer-1', type: 'assistant', content_type: 'text', text: 'answer' },
{ uuid: 'tool-1', type: 'assistant', content_type: 'tool_use', text: '' },
{ uuid: 'result-1', type: 'user', content_type: 'tool_result', text: '' },
{
uuid: 'thinking-1', timestamp: '2026-06-10T10:00:00Z',
type: 'assistant', content_type: 'thinking', text: 'reasoning',
},
{
uuid: 'answer-1', timestamp: '2026-06-10T10:00:01Z',
type: 'assistant', content_type: 'text', text: 'answer',
},
{
uuid: 'tool-1', timestamp: '2026-06-10T10:00:02Z',
type: 'assistant', content_type: 'tool_use', text: '',
},
{
uuid: 'result-1', timestamp: '2026-06-10T10:00:03Z',
type: 'user', content_type: 'tool_result', text: '',
},
];
const assembled = assembleSessionMessages({
const assembled = assembleSessionDetail({
messages,
toolCalls: [{ id: 'call-1', message_uuid: 'tool-1', name: 'Agent', input_json: '{"description":"inspect"}' }],
toolResults: [{ tool_use_id: 'call-1', message_uuid: 'result-1', content: 'done', is_error: 0 }],
subagents: [{ agent_id: 'agent-1', parent_tool_use_id: 'call-1', agent_type: 'reviewer', description: 'inspect' }],
workflows: [],
});
}).messages;
assert.equal(assembled.length, 1);
assert.equal(assembled[0].uuid, 'answer-1');
@@ -25,34 +37,76 @@ test('session assembly preserves thinking and attaches tool result and subagent
});
test('session assembly keeps Skill evidence standalone and embeds matching workflow agents', () => {
const assembled = assembleSessionMessages({
const assembled = assembleSessionDetail({
messages: [
{ uuid: 'skill-1', type: 'assistant', content_type: 'tool_use', text: '' },
{ uuid: 'skill-md', type: 'user', content_type: 'text', is_meta: 1, text: 'Base directory for this skill\n# Skill' },
{ uuid: 'skill-md', type: 'user', content_type: 'skill_instructions', is_meta: 1, text: '# Skill instructions' },
{ uuid: 'workflow-1', type: 'assistant', content_type: 'tool_use', text: '' },
],
toolCalls: [
{ id: 'call-skill', message_uuid: 'skill-1', name: 'Skill', input_json: '{"skill":"obelisk"}' },
{ id: 'call-workflow', message_uuid: 'workflow-1', name: 'Workflow', input_json: '{}' },
{ id: 'call-skill', message_uuid: 'skill-1', name: 'Skill', presentation: 'skill', input_json: '{"skill":"obelisk"}' },
{ id: 'call-workflow', message_uuid: 'workflow-1', name: 'Workflow', presentation: 'default', input_json: '{}' },
],
toolResults: [{ tool_use_id: 'call-workflow', content: 'run-1 complete', is_error: 0 }],
toolResults: [{ tool_use_id: 'call-workflow', content: 'complete', is_error: 0 }],
subagents: [],
workflows: [{
run_id: 'run-1',
parent_tool_use_id: 'call-workflow',
workflow_name: 'review',
status: 'complete',
agents: [{ agent_id: 'agent-1', phase: 'review', label: 'Reviewer', state: 'complete' }],
}],
});
}).messages;
assert.equal(assembled[0]._skillMd, 'Base directory for this skill\n# Skill');
assert.equal(assembled[0]._skillMd, '# Skill instructions');
assert.equal(assembled[1].tool_calls[0].workflow.run_id, 'run-1');
assert.deepEqual(assembled[1].tool_calls[0].workflow.agents, [{
agent_id: 'agent-1',
phase: 'review',
label: 'Reviewer',
state: 'complete',
tokens: undefined,
duration_ms: undefined,
tokens: null,
duration_ms: null,
}]);
});
test('session assembly trusts canonical classification instead of parsing provider text', () => {
const detail = assembleSessionDetail({
messages: [{
uuid: 'provider-owned-classification',
type: 'user',
content_type: 'text',
is_meta: 0,
text: '<system-reminder>text alone does not define presentation semantics</system-reminder>',
}],
});
assert.equal(detail.messages[0].is_meta, 0);
});
test('canonical ordering is stable across provider and SQLite iteration order', () => {
const detail = assembleSessionDetail([
{
kind: 'message', uuid: 'b', session_id: 'session', type: 'user', parent_uuid: null,
timestamp: '2026-06-10T10:00:00Z', role: 'user', text: 'second', content_type: 'text',
is_meta: 0, visibility: 'visible', model: null, is_sidechain: 0, agent_id: null,
input_tokens: null, output_tokens: null, cwd: null, skill: null, source: 'test',
},
{
kind: 'message', uuid: 'a', session_id: 'session', type: 'user', parent_uuid: null,
timestamp: '2026-06-10T10:00:00Z', role: 'user', text: 'first', content_type: 'text',
is_meta: 0, visibility: 'visible', model: null, is_sidechain: 0, agent_id: null,
input_tokens: null, output_tokens: null, cwd: null, skill: null, source: 'test',
},
]);
assert.deepEqual(detail.messages.map(message => message.uuid), ['a', 'b']);
});
test('direct session assembly rejects an incomplete provider delta', () => {
assert.throws(() => assembleSessionDetail([{
kind: 'session', id: 'session', title: null, project: null,
started_at: null, ended_at: null, git_branch: null, version: null,
message_count: 1, countMode: 'delta', jsonl_path: '/session.jsonl', source: 'test',
}]), /fresh full parse/);
});