Skip to content

Your team of AI agents, on your Mac, Windows or Linux. Agents that talk in channels, write code, review each other and use your tools, on the Claude Code, Codex, OpenCode or Pi you already have. Anything that touches the real world waits for your yes.

Let the hand-off screen see mentions at the end of a long reply (#171)

Alan Jones Riose21e204e7f92
diff --git a/src/core/router.ts b/src/core/router.ts
index 15d43ab..be31ddb 100644
--- a/src/core/router.ts
+++ b/src/core/router.ts
@@ -4,7 +4,8 @@
// can read Linear. It also screens an agent's reply that mentions others, so a
// "thanks, @bia" doesn't wake her up.
import { L } from '../shared/lang'
-import type { Agent, Connector, Message } from '../shared/types'
+import { MENTION } from '../shared/mention-text'
+import { CODE_SPANS, type Agent, type Connector, type Message } from '../shared/types'
import type { Engine, TurnShape, Usage } from './engines/types'
import type { ThreadDigest } from './memory'
@@ -110,10 +111,62 @@ export const handoffSystem = (): string =>
].join('\n')
)
+// A long reply goes to the hand-off screen as its opening plus the lines around each mention
+// of a candidate, so an ask written at the end of a long plan ("@rafa, I need two decisions")
+// is what gets judged. What's left out shows as "[…]". A short reply goes in whole.
+const REPLY_MAX = 3000
+const OPENING = 1000
+const BEFORE = 600
+const AFTER = 1200
+// A cut moves to the nearest line break when one is this close, so lines arrive whole.
+const SNAP = 200
+
+const lineStart = (t: string, i: number): number => {
+ const n = t.lastIndexOf('\n', i)
+ return n >= 0 && i - n <= SNAP ? n + 1 : i
+}
+const lineEnd = (t: string, i: number): number => {
+ const n = t.indexOf('\n', i)
+ return n >= 0 && n - i <= SNAP ? n : i
+}
+
+export function replyExcerpt(reply: string, handles: string[]): string {
+ const text = reply.trim()
+ if (text.length <= REPLY_MAX) return text
+ const wanted = new Set(handles.map((h) => h.toLowerCase()))
+ // Same rule as `mentioned`: a handle inside code doesn't count. Blanking keeps the offsets.
+ const plain = text.replace(CODE_SPANS, (s) => ' '.repeat(s.length))
+ // The first and the last mention of each candidate: the ask is usually one of them, and a reply
+ // that names someone on every line still goes in bounded.
+ const at = new Map<string, number[]>()
+ for (const m of plain.matchAll(MENTION)) {
+ const h = m[2]!.toLowerCase()
+ if (!wanted.has(h)) continue
+ const i = m.index + m[1]!.length
+ const seen = at.get(h)
+ if (!seen) at.set(h, [i])
+ else seen[1] = i
+ }
+ const spans: [number, number][] = [[0, lineEnd(text, OPENING)]]
+ for (const i of [...at.values()].flat()) spans.push([lineStart(text, Math.max(0, i - BEFORE)), lineEnd(text, Math.min(text.length, i + AFTER))])
+ spans.sort((a, b) => a[0] - b[0])
+ const merged: [number, number][] = []
+ for (const [from, to] of spans) {
+ const last = merged[merged.length - 1]
+ if (last && from <= last[1] + 1) last[1] = Math.max(last[1], to)
+ else merged.push([from, to])
+ }
+ const parts = merged.map(([from, to]) => text.slice(from, to).trim())
+ const end = merged[merged.length - 1]![1]
+ return parts.join('\n[…]\n') + (end < text.length ? '\n[…]' : '')
+}
+
// The reply sits between <reply> tags so it can't be confused with the roster or the history.
export function handoffPrompt(opts: { reply: string; author: Agent; candidates: Agent[]; messages: Message[]; members: Agent[]; me: string; connectors?: Connector[]; busy?: Agent[] }): string {
const { reply, author, candidates, messages, members, me, connectors = [], busy = [] } = opts
const name = speaker(members, me)
+ const excerpt = replyExcerpt(reply, candidates.map((a) => a.handle))
+ const cut = excerpt !== reply.trim()
return [
L('Agentes mencionados:', 'Mentioned agents:'),
...candidates.map((a) => `- ${a.handle}: ${describeAgent(a, connectors)}`),
@@ -129,9 +182,14 @@ export function handoffPrompt(opts: { reply: string; author: Agent; candidates:
L('Conversa recente:', 'Recent conversation:'),
...messages.filter(said).slice(-ROUTER_HISTORY).map((m) => `[${name(m)}] ${m.text.trim().slice(0, 600)}`),
'',
- L(`Resposta de ${author.name} que você avalia:`, `${author.name}'s reply you are judging:`),
+ cut
+ ? L(
+ `Resposta de ${author.name} que você avalia (longa: vão o começo e os trechos com as menções; […] marca o que ficou de fora):`,
+ `${author.name}'s reply you are judging (long: its opening and the parts with the mentions; […] marks what was left out):`
+ )
+ : L(`Resposta de ${author.name} que você avalia:`, `${author.name}'s reply you are judging:`),
'<reply>',
- reply.trim().slice(0, 3000),
+ excerpt,
'</reply>',
'',
REPLY_JSON()
diff --git a/test/router.test.ts b/test/router.test.ts
index e803054..45809e7 100644
--- a/test/router.test.ts
+++ b/test/router.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
-import { handoffPrompt, handoffSystem, parseRoute, routerPrompt } from '../src/core/router'
+import { handoffPrompt, handoffSystem, parseRoute, replyExcerpt, routerPrompt } from '../src/core/router'
import type { Agent, Connector, Message } from '../src/shared/types'
const agent = (id: string, handle: string, role: string, instructions = ''): Agent => ({
@@ -106,6 +106,45 @@ describe('handoffPrompt', () => {
})
})
+describe('replyExcerpt', () => {
+ // A long plan, written line by line, with the ask at the very end.
+ const plan = Array.from({ length: 80 }, (_, i) => `Passo ${i + 1}: ajustar o módulo ${i + 1} e conferir o resultado.`).join('\n')
+ const ask = '@nina, preciso de duas decisões:\n1. aprova a ordem?\n2. aprova começar pelo levantamento?'
+
+ it('keeps a short reply whole', () => {
+ expect(replyExcerpt(' Valeu, @nina! ', ['nina'])).toBe('Valeu, @nina!')
+ })
+
+ it('keeps the opening and the lines around a mention at the end of a long reply', () => {
+ const out = replyExcerpt(`${plan}\n\n${ask}`, ['nina'])
+ expect(out.startsWith('Passo 1:')).toBe(true)
+ expect(out).toContain(ask)
+ expect(out).toContain('\n[…]\n')
+ expect(out).not.toContain('Passo 30:')
+ expect(out.length).toBeLessThan(4000)
+ })
+
+ it('marks a cut at the end, and ignores handles inside code or not asked about', () => {
+ const out = replyExcerpt(`${plan}\n\`@nina\` @lia`, ['nina'])
+ expect(out.endsWith('\n[…]')).toBe(true)
+ expect(out).not.toContain('@lia')
+ })
+
+ it('stays bounded when a long reply names someone on every line', () => {
+ const busy = Array.from({ length: 400 }, (_, i) => `Item ${i + 1}: @nina confere o módulo ${i + 1}.`).join('\n')
+ const out = replyExcerpt(busy, ['nina'])
+ expect(out).toContain('Item 400:')
+ expect(out).not.toContain('Item 200:')
+ expect(out.length).toBeLessThan(1000 + 2 * (600 + 1200) + 500)
+ })
+
+ it('goes into the hand-off prompt, which says it was cut', () => {
+ const p = handoffPrompt({ reply: `${plan}\n\n${ask}`, author: lia, candidates: [nina], messages: [], members: team, me: 'Edu' })
+ expect(p).toContain(ask)
+ expect(p).toContain('[…] marca o que ficou de fora')
+ })
+})
+
describe('parseRoute with hand-off candidates', () => {
it('accepts an empty list and drops handles outside the candidates', () => {
expect(parseRoute('{"handles":[],"reason":"agradecimento"}', [nina])).toEqual({ agents: [], reason: 'agradecimento' })