Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions packages/tool-hermes/src/session-adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const mocks = vi.hoisted(() => ({
sql: [] as string[],
sessions: [] as unknown[],
messages: [] as unknown[],
legacyMessageSchema: false,
}));

vi.mock('@teamai/tool-core/sessions/sqlite', async (importOriginal) => ({
Expand All @@ -28,6 +29,10 @@ const processes: Processes = {
const sql = args[2]!;
mocks.sql.push(sql);

if (mocks.legacyMessageSchema && sql.includes('display_kind, _compressed_summary')) {
return { code: 1, stdout: '', stderr: 'Error: in prepare, no such column: display_kind', signal: null };
}

return {
code: 0, stdout: JSON.stringify(sql.includes('FROM sessions') ? mocks.sessions : mocks.messages),
stderr: '', signal: null,
Expand All @@ -43,6 +48,7 @@ beforeEach(() => {
mocks.sql.length = 0;
mocks.sessions = [];
mocks.messages = [];
mocks.legacyMessageSchema = false;
});

afterEach(() => {
Expand Down Expand Up @@ -128,6 +134,36 @@ describe('HermesAdapter', () => {
]);
});

it('keeps only human-authored Hermes user messages', async () => {
const { adapter, cwd, id } = fixture();
mocks.messages = [
{ id: 20, role: 'user', content: 'Fix the parser', display_kind: null, _compressed_summary: 0,
tool_call_id: null, tool_calls: null, tool_name: null, timestamp: 200, finish_reason: null, reasoning: null, reasoning_content: null },
{ id: 21, role: 'user', content: 'Switched model', display_kind: 'model_switch', _compressed_summary: 0,
tool_call_id: null, tool_calls: null, tool_name: null, timestamp: 201, finish_reason: null, reasoning: null, reasoning_content: null },
{ id: 22, role: 'user', content: 'Earlier conversation summary', display_kind: null, _compressed_summary: 1,
tool_call_id: null, tool_calls: null, tool_name: null, timestamp: 202, finish_reason: null, reasoning: null, reasoning_content: null },
{ id: 23, role: 'assistant', content: 'Done', display_kind: null, _compressed_summary: 0,
tool_call_id: null, tool_calls: null, tool_name: null, timestamp: 203, finish_reason: 'stop', reasoning: null, reasoning_content: null },
];

const session = await adapter.readSession({ sessionId: id, projectPath: cwd });

expect(session.messages.map((message) => message.messageId)).toEqual(['20', '23']);
expect(mocks.sql.at(-1)).toContain('display_kind, _compressed_summary');
});

it('keeps sessions from a Hermes store that predates message classification columns', async () => {
const { adapter, cwd, id } = fixture();
mocks.legacyMessageSchema = true;

const session = await adapter.readSession({ sessionId: id, projectPath: cwd });

expect(session.messages.map((message) => message.messageId)).toEqual(['1', '2', '3', '4']);
expect(mocks.sql.at(-2)).toContain('display_kind, _compressed_summary');
expect(mocks.sql.at(-1)).toContain('NULL AS display_kind, 0 AS _compressed_summary');
});

it('matches a symlinked cwd and rejects unsafe non-Hermes ids', async () => {
const { adapter, cwd, id } = fixture();
const linked = path.join(path.dirname(cwd), 'linked-app');
Expand Down
22 changes: 18 additions & 4 deletions packages/tool-hermes/src/session-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const MessageRow = z.object({
id: z.number(), role: z.string(), content: z.string().nullable(), tool_call_id: z.string().nullable(),
tool_calls: z.string().nullable(), tool_name: z.string().nullable(), timestamp: z.number(),
finish_reason: z.string().nullable(), reasoning: z.string().nullable(), reasoning_content: z.string().nullable(),
display_kind: z.string().nullable().optional(), _compressed_summary: z.number().optional(),
});

const StoredToolCall = z.object({
Expand Down Expand Up @@ -97,6 +98,7 @@ function messagesOf(rows: MessageRow[]): Message[] {
const timestamp = iso(row.timestamp);

if (row.role === 'user') {
if (row.display_kind || row._compressed_summary === 1) continue;
const text = textOf(row.content);

if (text) messages.push({ role: 'user', content: [{ type: 'text', text }], timestamp, messageId: String(row.id) });
Expand Down Expand Up @@ -188,10 +190,22 @@ export class HermesAdapter extends AgentAdapter {
}

private async messages(sessionId: string): Promise<MessageRow[]> {
const raw = await this.query(
'SELECT id, role, content, tool_call_id, tool_calls, tool_name, timestamp, finish_reason, reasoning, reasoning_content ' +
`FROM messages WHERE session_id = '${sessionId}' AND (active = 1 OR compacted = 1) ORDER BY id;`,
);
const from = `FROM messages WHERE session_id = '${sessionId}' AND (active = 1 OR compacted = 1) ORDER BY id;`;
let raw: unknown[];

try {
raw = await this.query(
'SELECT id, role, content, tool_call_id, tool_calls, tool_name, timestamp, finish_reason, reasoning, reasoning_content, display_kind, _compressed_summary ' + from,
);
} catch (error) {
// Older Hermes stores predate both classification columns. Keep those
// sessions readable; their user rows were all ordinary prompts because
// the operational row kinds did not exist in that schema yet.
if (!(error instanceof Error) || !/no such column: (?:display_kind|_compressed_summary)/.test(error.message)) throw error;
raw = await this.query(
'SELECT id, role, content, tool_call_id, tool_calls, tool_name, timestamp, finish_reason, reasoning, reasoning_content, NULL AS display_kind, 0 AS _compressed_summary ' + from,
);
}

return raw.flatMap((value) => {
const row = MessageRow.safeParse(value);
Expand Down
Loading