fix(bots): bound continuation rounds + index-order mention tracking (#94755 review)

- GROUP_CHAT_MAX_CONTINUATIONS=2 caps continuation rounds independently of
  the message cap, so pathological @mention chains can't consume the room's
  whole budget on handoffs.
- unaddressedGroupMentions now orders by log INDEX instead of entry id:
  ids are UUIDs (groupChatEntryId), not monotonic — string comparison could
  both re-drive answered members and miss stranded ones.
- New unaddressed-mentions.test.mjs exercises the REAL function via the
  vm-slice pattern (replacing concept-only helpers) and pins the ordering
  fix with a UUID-vs-log-order case.
This commit is contained in:
beplee
2026-08-26 05:19:40 +07:00
committed by Teknium
parent 24a5b6ecb7
commit 411f9c2f44
2 changed files with 85 additions and 8 deletions
+20 -8
View File
@@ -6698,7 +6698,10 @@ function knownGroups(metaByName) {
// room messages that are NEW since it last saw the room.
const GROUP_CHAT_MAX_ROUNDS = 3
// #94478 review: continuation rounds are bounded independently of the message cap so a pathological mention chain can't consume the room's whole budget on handoffs.
const GROUP_CHAT_MAX_MESSAGES = 10
const GROUP_CHAT_MAX_CONTINUATIONS = 2
const GROUP_CHAT_HISTORY_LIMIT = 24
const GROUP_CHAT_MAX_MEMBERS = 6
@@ -7989,7 +7992,10 @@ function unaddressedGroupMentions(group, members, thread) {
const room = $groupChats.get()[group] || { log: [] }
const log = (room.log || []).filter(e => groupThreadOf(e) === thread)
// key → id of the entry that most recently cited this member.
// key → log INDEX of the entry that most recently cited this member.
// Entry ids are UUIDs (groupChatEntryId), NOT monotonic — index order is
// the only guaranteed ordering, and it is what "answered after the citing
// entry" actually means. (#94478 review)
const citedAt = new Map()
for (const entry of log) {
@@ -8010,7 +8016,7 @@ function unaddressedGroupMentions(group, members, thread) {
// Never count a bot citing itself as a pending handoff.
if (citingMemberKey && citingMemberKey !== key) {
citedAt.set(key, entry.id)
citedAt.set(key, log.indexOf(entry))
}
}
}
@@ -8031,15 +8037,15 @@ function unaddressedGroupMentions(group, members, thread) {
})()
if (speakerKey) {
lastPostAt.set(speakerKey, entry.id)
lastPostAt.set(speakerKey, log.indexOf(entry))
}
}
return [...citedAt.keys()].filter(key => {
const citedId = citedAt.get(key)
const answeredAt = lastPostAt.get(key)
const citedIdx = citedAt.get(key)
const answeredIdx = lastPostAt.get(key)
return answeredAt === undefined || answeredAt <= citedId
return answeredIdx === undefined || answeredIdx <= citedIdx
})
}
@@ -8052,6 +8058,7 @@ async function runGroupChatRounds(group, members, thread) {
const startEpoch = ($groupChats.get()[group] || {}).epoch || 0
const isCurrent = () => (($groupChats.get()[group] || {}).epoch || 0) === startEpoch
let posted = 0
let continuations = 0
try {
for (let round = 0; round < GROUP_CHAT_MAX_ROUNDS; round++) {
@@ -8239,7 +8246,12 @@ async function runGroupChatRounds(group, members, thread) {
// settled.
const pendingKeys = unaddressedGroupMentions(group, members, thread)
if (pendingKeys.length) {
// #94478 review: bound continuation rounds independently of the
// message cap so a pathological mention chain can't consume the
// room's entire budget on back-and-forth handoffs.
continuations += 1
if (pendingKeys.length && continuations <= GROUP_CHAT_MAX_CONTINUATIONS) {
const citedMembers = members.filter(member => pendingKeys.includes(groupMemberKey(member)))
if (citedMembers.length && posted < GROUP_CHAT_MAX_MESSAGES) {
@@ -8249,7 +8261,7 @@ async function runGroupChatRounds(group, members, thread) {
)
for (const member of continuationResponders) {
if (!isCurrent() || posted >= GROUP_CHAT_MAX_MESSAGES) {
if (!isCurrent() || posted >= GROUP_CHAT_MAX_MESSAGES || continuations > GROUP_CHAT_MAX_CONTINUATIONS) {
break
}
@@ -0,0 +1,65 @@
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import test from 'node:test'
import vm from 'node:vm'
// #94478 review: exercise the REAL unaddressedGroupMentions from plugin.js
// via the same vm-slice pattern as active-now-strip.test.mjs, plus pin the
// log-index ordering fix (entry ids are UUIDs, NOT monotonic).
const source = readFileSync(new URL('../plugin.js', import.meta.url), 'utf8')
function loadUnaddressed(roomLog) {
const start = source.indexOf('function unaddressedGroupMentions')
const end = source.indexOf('/** Drive one bounded round-robin turn for ONE THREAD.')
assert.ok(start >= 0 && end > start, 'unaddressedGroupMentions block must remain extractable')
const members = [{ name: 'alpha' }, { name: 'beta' }, { name: 'gamma' }]
const sandbox = {
$groupChats: {
// The production code indexes rooms by name; expose the room under
// both shapes so either access style resolves.
get: k => ({ log: roomLog, [k ?? 'g']: { log: roomLog } }),
},
groupThreadOf: e => e.thread,
parseGroupChatMentions: (text, ms) => ({
mentioned: [...new Set(ms.filter(m => text.includes(`@${m.name}`)).map(m => m.name))],
everyone: false,
}),
groupMemberKey: m => m.name,
members,
out: null,
}
vm.createContext(sandbox)
vm.runInContext(`${source.slice(start, end)}\nout = unaddressedGroupMentions('g', members, 't1')`, sandbox)
return sandbox.out
}
function entry(id, from, text, thread = 't1') {
return { id, at: 0, from: { kind: 'member', name: from }, text, thread }
}
test('unaddressedGroupMentions flags a cited member with no later post', () => {
const pending = loadUnaddressed([
entry('zzzz-0002', 'alpha', 'hello'),
entry('aaaa-0003', 'beta', 'ping @gamma — take this'),
])
assert.equal(JSON.stringify(pending), JSON.stringify(['gamma']))
})
test('a reply AFTER the citation in log order counts as answered even when its id sorts lower', () => {
const pending = loadUnaddressed([
entry('aaaa-0003', 'beta', 'ping @gamma — take this'),
entry('9999-0004', 'gamma', 'on it'),
])
assert.equal(JSON.stringify(pending), JSON.stringify([]))
})
test('self-citations and user entries never create pending handoffs', () => {
const pending = loadUnaddressed([
entry('bbbb-0001', 'alpha', 'I will do @alpha things myself'),
{ id: 'cccc-0002', at: 0, from: { kind: 'user', name: 'You' }, text: '@beta @gamma look here', thread: 't1' },
])
assert.equal(JSON.stringify(pending), JSON.stringify([]))
})