Skip to content

The agent that grows with you

fix(desktop): skipping a model-only merge keeps later rows' ids

Tekniumcd9c67297ef3
diff --git a/apps/desktop/src/lib/chat-messages/hydration-model-only.test.ts b/apps/desktop/src/lib/chat-messages/hydration-model-only.test.ts
new file mode 100644
index 00000000000..997f9578bc6
--- /dev/null
+++ b/apps/desktop/src/lib/chat-messages/hydration-model-only.test.ts
@@ -0,0 +1,21 @@
+import { describe, expect, it } from 'vitest'
+
+import { toChatMessages } from './hydration'
+
+// A micro-compaction merge (`display_metadata.model_only`) is skipped, but the ids of the rows after it
+// keep their stored position: filtering first shifted every later message and tool-part id by one.
+describe('toChatMessages with a model-only merge row', () => {
+ it('skips the merge and keeps later ids keyed by stored position', () => {
+ const messages = [
+ { role: 'user', content: 'Q1\n\nQ2', timestamp: 100, display_metadata: { model_only: true } },
+ { role: 'user', content: 'Q3', timestamp: 200 },
+ { role: 'assistant', content: 'A3', timestamp: 201 }
+ ]
+
+ const result = toChatMessages(messages as never)
+
+ expect(result.map(m => m.role)).toEqual(['user', 'assistant'])
+ expect(result[0].id).toBe('200-1-user')
+ expect(result[1].id).toBe('201-2-assistant')
+ })
+})
diff --git a/apps/desktop/src/lib/chat-messages/hydration.ts b/apps/desktop/src/lib/chat-messages/hydration.ts
index 8e155fb1e14..3a538f91381 100644
--- a/apps/desktop/src/lib/chat-messages/hydration.ts
+++ b/apps/desktop/src/lib/chat-messages/hydration.ts
@@ -423,7 +423,12 @@ export function toChatMessages(messages: SessionMessage[]): ChatMessage[] {
clearPendingTools()
}
- messages.filter(isPainted).forEach((message, index) => {
+ messages.forEach((message, index) => {
+ // Skipped in place, not filtered first: `index` keys tool-part and message ids by the stored row position.
+ if (!isPainted(message)) {
+ return
+ }
+
if (message.role === 'assistant') {
nearestAssistant = message
} else if (message.role === 'user' || message.role === 'system') {
diff --git a/hermes_cli/gateway_chat.py b/hermes_cli/gateway_chat.py
index 378ef79e843..4e780c2fb8e 100644
--- a/hermes_cli/gateway_chat.py
+++ b/hermes_cli/gateway_chat.py
@@ -235,9 +235,11 @@ async def run_gateway_chat(args, emitter=None):
view.unattended = isinstance(oneshot_prompt, str)
view.resume_footer = oneshot and not quiet
if (getattr(args, "resume", None) or title or latest) and not quiet:
+ from agent.context_compressor import MODEL_ONLY_DISPLAY_METADATA_KEY
for row in snapshot.get("messages", []):
# A micro-compaction merge (model_only) is not printed: its original prompts are.
- merged = isinstance(row.get("display_metadata"), dict) and row["display_metadata"].get("model_only")
+ metadata = row.get("display_metadata")
+ merged = isinstance(metadata, dict) and metadata.get(MODEL_ONLY_DISPLAY_METADATA_KEY)
if row.get("role") in {"user", "assistant"} and isinstance(row.get("content"), str) and not merged:
print(f"{row['role']}: {row['content']}")
return await view.run(query, oneshot=oneshot)