Skip to content

A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.

Answer the IM card the button names, not the chat's newest

AdamPlatin123d59ed18b67e8
diff --git a/ds-bot/src/host/im/index.js b/ds-bot/src/host/im/index.js
index fc5cd000..66d70316 100644
--- a/ds-bot/src/host/im/index.js
+++ b/ds-bot/src/host/im/index.js
@@ -232,17 +232,12 @@ export function install(rt) {
return undefined
}
- // The newest open card for this chat, and how to answer it.
- const openCard = (conn, chatKey, kind) => {
- const data = runtimes.get(conn.id)?.data
- if (data === undefined) return undefined
- for (const [cardKey, entries] of Object.entries(data.cards).reverse()) {
- if (!cardKey.startsWith(`${kind}:`)) continue
- const entry = entries.find(item => item.chatKey === chatKey && item.answer === undefined)
- if (entry === undefined) continue
- return { cardKey, entry }
- }
- return undefined
+ // One card this chat was sent and has not answered yet. A button names its own
+ // card, so it must never fall through to a newer card of the same kind.
+ const openCard = (conn, chatKey, cardKey) => {
+ const entries = runtimes.get(conn.id)?.data?.cards[cardKey]
+ const entry = entries?.find(item => item.chatKey === chatKey && item.answer === undefined)
+ return entry === undefined ? undefined : { cardKey, entry }
}
const questionById = (id) => {
for (const [sessionId, question] of Object.entries(state.questions ?? {})) {
@@ -250,8 +245,8 @@ export function install(rt) {
}
return undefined
}
- const answerQuestionCard = async (conn, chat, labels) => {
- const held = openCard(conn, chat.key, 'q')
+ const answerQuestionCard = async (conn, chat, cardKey, labels) => {
+ const held = openCard(conn, chat.key, cardKey)
if (held === undefined) return false
const found = questionById(held.cardKey.slice(2))
if (found === undefined) {
@@ -275,8 +270,8 @@ export function install(rt) {
}
return true
}
- const answerApprovalCard = async (conn, chat, choice) => {
- const held = openCard(conn, chat.key, 'a')
+ const answerApprovalCard = async (conn, chat, cardKey, choice) => {
+ const held = openCard(conn, chat.key, cardKey)
if (held === undefined) return false
try {
rt.answerApproval(held.cardKey.slice(2), choice)
@@ -349,12 +344,12 @@ export function install(rt) {
const options = found.question.options ?? []
const inRange = picked.length > 0 && (found.question.multiSelect === true || picked.length === 1)
&& picked.every(index => options[index - 1] !== undefined)
- if (inRange && await answerQuestionCard(conn, msg.chat, picked.map(index => options[index - 1].label))) return
+ if (inRange && await answerQuestionCard(conn, msg.chat, held.cardKey, picked.map(index => options[index - 1].label))) return
}
} else if (held !== undefined && held.cardKey.startsWith('a:')
&& picked.length === 1 && (picked[0] === 1 || picked[0] === 2)
&& rt.approvalsView().some(entry => `a:${entry.id}` === held.cardKey)) {
- if (await answerApprovalCard(conn, msg.chat, picked[0] === 1 ? 'allow' : 'reject')) return
+ if (await answerApprovalCard(conn, msg.chat, held.cardKey, picked[0] === 1 ? 'allow' : 'reject')) return
}
}
if (target === undefined) {
@@ -382,21 +377,24 @@ export function install(rt) {
if (!paired(conn, action.sender.id)) return
const data = String(action.data ?? '')
const chat = { key: action.chat.key, kind: action.chat.kind, title: action.chat.title }
- if (data.startsWith('q:')) {
- const [, id, index] = data.split(':')
- const found = questionById(id)
- const label = found?.question.options?.[Number(index)]?.label
+ // Button data is `<kind>:<id>:<choice>`; the id is everything between the
+ // first and the last colon, so an id with a colon in it still parses.
+ const cut = data.lastIndexOf(':')
+ const cardKey = data.slice(0, cut)
+ const choice = data.slice(cut + 1)
+ if (data.startsWith('q:') && cut > 2) {
+ const found = questionById(cardKey.slice(2))
+ const label = found?.question.options?.[Number(choice)]?.label
if (label === undefined) {
await sendCardReply(conn, chat, text('stale'))
return
}
- if (!(await answerQuestionCard(conn, chat, [label]))) await sendCardReply(conn, chat, text('stale'))
+ if (!(await answerQuestionCard(conn, chat, cardKey, [label]))) await sendCardReply(conn, chat, text('stale'))
return
}
- if (data.startsWith('a:')) {
- const [, , choice] = data.split(':')
+ if (data.startsWith('a:') && cut > 2) {
if (choice === 'allow' || choice === 'reject') {
- if (!(await answerApprovalCard(conn, chat, choice))) await sendCardReply(conn, chat, text('stale'))
+ if (!(await answerApprovalCard(conn, chat, cardKey, choice))) await sendCardReply(conn, chat, text('stale'))
}
}
}
diff --git a/ds-bot/test/im.test.js b/ds-bot/test/im.test.js
index cbd5af9c..30c8692a 100644
--- a/ds-bot/test/im.test.js
+++ b/ds-bot/test/im.test.js
@@ -386,6 +386,78 @@ test('own-chat approval races DSH: IM allow resolves and withdraws the card; lat
void seenSignal
})
+test('an approval button answers its own card, not the newest one in the chat', async (t) => {
+ const s = await start()
+ t.after(s.stop)
+ const conn = await add(s)
+ const one = s.rt.state.im.connections[conn.id]
+ one.paired.push({ id: 'u1', name: 'U1', at: 1 })
+ await s.rt.save()
+ await dm(s, 'u1', 'hi')
+ s.fake.sent.length = 0
+ const hang = () => new Promise(() => {})
+ const first = s.fireApprovals({ agent: { id: 'a' }, toolName: 'bash', reason: 'first', signal: new AbortController().signal }, hang)
+ await s.flush()
+ const firstId = s.fake.sent.at(-1).buttons[0].data.split(':')[1]
+ let secondOutcome
+ void s.fireApprovals({ agent: { id: 'a' }, toolName: 'bash', reason: 'second', signal: new AbortController().signal }, hang)
+ .then(([outcome]) => { secondOutcome = outcome })
+ await s.flush()
+ const secondId = s.fake.sent.at(-1).buttons[0].data.split(':')[1]
+ assert.notEqual(firstId, secondId)
+ await s.fake.handlers.onAction({ chat: { key: 'dm-u1', kind: 'direct', title: 'U1' }, sender: { id: 'u1', name: 'U1' }, data: `a:${firstId}:allow`, messageId: 1 })
+ assert.equal((await first)[0], 'allowed-once', 'the pressed card is the one answered')
+ await s.flush()
+ assert.equal(secondOutcome, undefined, 'the newer card stays open')
+ assert.ok(s.rt.approvalsView().some(entry => entry.id === secondId))
+})
+
+test('a button for a card this chat was never sent gets the stale text and answers nothing', async (t) => {
+ const s = await start()
+ t.after(s.stop)
+ const conn = await add(s)
+ const one = s.rt.state.im.connections[conn.id]
+ one.paired.push({ id: 'u1', name: 'U1', at: 1 }, { id: 'u2', name: 'U2', at: 1 })
+ await s.rt.save()
+ await dm(s, 'u1', 'hi')
+ s.fake.sent.length = 0
+ let outcome
+ void s.fireApprovals({ agent: { id: 'a' }, toolName: 'bash', reason: 'r', signal: new AbortController().signal }, () => new Promise(() => {}))
+ .then(([value]) => { outcome = value })
+ await s.flush()
+ const approvalId = s.fake.sent.at(-1).buttons[0].data.split(':')[1]
+ await s.fake.handlers.onAction({ chat: { key: 'dm-u2', kind: 'direct', title: 'U2' }, sender: { id: 'u2', name: 'U2' }, data: `a:${approvalId}:allow`, messageId: 1 })
+ await s.flush()
+ assert.equal(outcome, undefined, 'another chat cannot answer this card')
+ assert.match(s.fake.sent.at(-1).text, /already answered/)
+ await s.fake.handlers.onAction({ chat: { key: 'dm-u1', kind: 'direct', title: 'U1' }, sender: { id: 'u1', name: 'U1' }, data: 'a:no-such-card:allow', messageId: 1 })
+ await s.flush()
+ assert.equal(outcome, undefined, 'an unknown id answers nothing')
+})
+
+test('a question button answers its own question even when a newer one has the same option', async (t) => {
+ // Bot a asks from its current chat and from an earlier part: both cards reach dm-u1.
+ const seed = TEAM()
+ seed.bots.a.segments = [{ sessionId: 'a-old' }]
+ const s = await start({ seed })
+ t.after(s.stop)
+ const conn = await add(s)
+ const one = s.rt.state.im.connections[conn.id]
+ one.paired.push({ id: 'u1', name: 'U1', at: 1 })
+ await s.rt.save()
+ await dm(s, 'u1', 'hi')
+ s.rt.state.questions.a = { id: 'q1', question: 'First?', options: [{ label: 'yes' }, { label: 'no' }] }
+ s.rt.state.questions['a-old'] = { id: 'q2', question: 'Second?', options: [{ label: 'yes' }, { label: 'no' }] }
+ await s.rt.save()
+ await s.flush()
+ const cards = s.fake.sent
+ assert.ok(cards.some(entry => entry.buttons?.[0]?.data.startsWith('q:q1:')), 'the first card reached the chat')
+ assert.ok(cards.some(entry => entry.buttons?.[0]?.data.startsWith('q:q2:')), 'the newer card reached the same chat')
+ await s.fake.handlers.onAction({ chat: { key: 'dm-u1', kind: 'direct', title: 'U1' }, sender: { id: 'u1', name: 'U1' }, data: 'q:q1:0', messageId: 1 })
+ assert.ok(s.rt.state.questions.a === undefined, 'the pressed question is answered')
+ assert.ok(s.rt.state.questions['a-old'] !== undefined, 'the newer question stays open')
+})
+
test('DSH answering first settles the race; the original signal aborting cancels', async (t) => {
const s = await start()
t.after(s.stop)