Skip to content

The agent that grows with you

fix(desktop): bound the dispatchedTo scan to the current exchange

teknium11e4952ddba1b
diff --git a/apps/desktop/src/components/assistant-ui/thread/agent-delivery.tsx b/apps/desktop/src/components/assistant-ui/thread/agent-delivery.tsx
index 2d9d929d71b..638f8d3acc2 100644
--- a/apps/desktop/src/components/assistant-ui/thread/agent-delivery.tsx
+++ b/apps/desktop/src/components/assistant-ui/thread/agent-delivery.tsx
@@ -1,6 +1,7 @@
import { type ToolCallMessagePartProps } from '@assistant-ui/react'
import { type FC, useEffect, useState } from 'react'
+import { messageContentText } from '@/components/assistant-ui/thread/content'
import { AGENT_MESSAGE_RE, agentAvatarCache, resolveAgentAvatar } from '@/components/assistant-ui/thread/user-message'
// Sender-side inter-agent delivery: `hermes -p <agent> chat … -q "Message
@@ -26,12 +27,15 @@ function agentKey(value: unknown): string {
}
/**
- * Did THIS bot send a `message_agent` to `sender` in `earlier` (the thread up
- * to, not including, the inbound "Message from <sender>" row)? True means that
- * row is the teammate's answer to our dispatch — the round trip is complete and
- * the next assistant message addresses the human, not the teammate. Matching
- * the sender by handle OR display name (either may sign the inbound row) errs
- * towards "expanded": a missed fold shows content, a wrong fold hides it.
+ * Did THIS bot send a `message_agent` to `sender` in the CURRENT exchange of
+ * `earlier` (the thread up to, not including, the inbound "Message from
+ * <sender>" row)? True means that row is the teammate's answer to our dispatch
+ * — the round trip is complete and the next assistant message addresses the
+ * human, not the teammate. The scan stops at the nearest earlier human row or
+ * previous inbound row from the same sender: one dispatch exempts only the
+ * answer that follows it, never every later unsolicited delivery (#85884).
+ * Matching the sender by handle OR display name (either may sign the inbound
+ * row) errs towards "expanded": a missed fold shows content, a wrong fold hides it.
*/
export function dispatchedTo(earlier: readonly { content?: unknown; role?: string }[], sender: (string | undefined)[]): boolean {
const keys = new Set(sender.map(agentKey).filter(Boolean))
@@ -43,6 +47,16 @@ export function dispatchedTo(earlier: readonly { content?: unknown; role?: strin
for (let i = earlier.length - 1; i >= 0; i--) {
const row = earlier[i]
+ if (row.role === 'user') {
+ const inbound = AGENT_MESSAGE_RE.exec(messageContentText(row.content))
+
+ if (!inbound || [inbound[1], inbound[2], inbound[3]].some(part => keys.has(agentKey(part)))) {
+ return false
+ }
+
+ continue
+ }
+
if (row.role !== 'assistant' || !Array.isArray(row.content)) {
continue
}
diff --git a/apps/desktop/src/components/assistant-ui/thread/inter-agent-collapse.test.tsx b/apps/desktop/src/components/assistant-ui/thread/inter-agent-collapse.test.tsx
index 6034090e120..fb3e2005d99 100644
--- a/apps/desktop/src/components/assistant-ui/thread/inter-agent-collapse.test.tsx
+++ b/apps/desktop/src/components/assistant-ui/thread/inter-agent-collapse.test.tsx
@@ -134,6 +134,30 @@ describe('inter-agent collapse gate', () => {
expect(screen.getByText('show reply')).toBeTruthy()
})
+ it('collapses the reply to a LATER unsolicited delivery from a teammate dispatched to earlier', async () => {
+ // One dispatch exempts only the answer that follows it. Turns later, the
+ // same teammate messages in on its own — that exchange had no dispatch,
+ // so the deliberate fold (#85884) must still apply.
+ render(
+ <Harness
+ messages={[
+ user('u1', 'ask hermes for the list'),
+ dispatch('a0', '@Hermes'),
+ user('u2', DELIVERY),
+ assistant('a1', 'here is the list hermes sent', false),
+ user('u3', 'ok thanks'),
+ assistant('a2', 'anytime', false),
+ user('u4', 'Message from 🤖 Hermes (@hermes): unsolicited: build broke'),
+ assistant('a3', 'on it, checking the build', false)
+ ]}
+ />
+ )
+
+ expect(await screen.findByText(/Replied to/)).toBeTruthy()
+ expect(screen.getAllByText(/Replied to/)).toHaveLength(1)
+ expect(screen.getByText('show reply')).toBeTruthy()
+ })
+
it('collapses a settled reply to an inter-agent delivery', async () => {
render(<Harness messages={[user('u1', DELIVERY), assistant('a1', 'build is green', false)]} />)