Merge pull request #93103 from kshitijk4poor/feat/bot-attention-badge

feat(desktop): needs-attention badge for background bot failures (#93091 item 3)
This commit is contained in:
kshitij
2026-08-24 01:08:56 +05:30
committed by GitHub
3 changed files with 285 additions and 1 deletions
+130 -1
View File
@@ -185,6 +185,98 @@ function trackInboundActivity(roster) {
}
}
// ── needs-attention badge (#93091 item 3) ───────────────────────────────────
// Attention-worthy failure classes — matches the #93091 item-1 reason-code
// enum (shipped separately). Until reason codes flow end-to-end,
// attentionReasonFromError ALSO classifies raw error text as a fallback so
// the badge works against current gateway error strings.
const BOT_ATTENTION_CLASSES = new Set([
'agent_blocked',
'provider_auth_or_access',
'provider_quota_limit',
'missing_config'
])
/** One-line user hint per attention class (roster badge tooltip). */
const BOT_ATTENTION_HINTS = {
provider_auth_or_access: 'Sign in again for this profile',
provider_quota_limit: 'Quota or balance exhausted',
missing_config: 'Provider not configured — run hermes model',
agent_blocked: 'Bot is blocked — see its last message'
}
/** Map an error (a #93091 reason code or raw error text) to an attention
* class, or null when the failure is transient (rate limit, server error,
* timeout) — transient classes must NEVER badge. Pure; tested directly. */
function attentionReasonFromError(errorTextOrReason) {
const raw = String(errorTextOrReason || '').trim()
if (!raw) {
return null
}
if (BOT_ATTENTION_CLASSES.has(raw)) {
return raw
}
const text = raw.toLowerCase()
// Transient failures first, so a retryable error never sticks a badge.
if (/rate.?limit|too many requests|\b429\b|\b5\d\d\b|server error|overloaded|timed?.?out|timeout|temporar/.test(text)) {
return null
}
if (/no llm provider|no access token|not configured|no api key|missing api key/.test(text)) {
return 'missing_config'
}
if (/\b401\b|\b403\b|unauthorized|forbidden|authentication|invalid.?api.?key|credentials? (are )?(invalid|expired)/.test(text)) {
return 'provider_auth_or_access'
}
if (/quota|out of funds|insufficient (credits?|funds|balance)|payment required|\b402\b|billing/.test(text)) {
return 'provider_quota_limit'
}
if (/\bblocked\b/.test(text)) {
return 'agent_blocked'
}
return null
}
/** Per-bot needs-attention state: roster key -> {reason, at, message}.
* Display-only presentation state (never persisted, never alters delivery).
* Latest failure wins; the bot's next good turn clears it. Hidden bots keep
* their entry — hiding is a roster-DISPLAY concern only. */
const $botAttention = atom({})
/** Record attention for a bot after a failed turn/delivery. Transient errors
* classify to null and set nothing. Latest failure wins. */
function noteBotAttention(key, errorTextOrReason) {
const reason = attentionReasonFromError(errorTextOrReason)
if (!key || !reason) {
return
}
$botAttention.set({
...$botAttention.get(),
[key]: { reason, at: Date.now(), message: String(errorTextOrReason || '').trim().slice(0, 200) }
})
}
/** A good turn clears the badge. */
function clearBotAttention(key) {
if (!key || !$botAttention.get()[key]) {
return
}
const next = { ...$botAttention.get() }
delete next[key]
$botAttention.set(next)
}
/** Last good cron list, same idea as the roster snapshot. */
const $lastJobs = atom([])
@@ -1435,13 +1527,19 @@ async function drainRelayOutboxes() {
continue
}
// Needs-attention hook (#93091 item 3): a delivered background DM is
// this bot's "good turn"; a classified delivery failure badges it.
const attentionKey = `${target.id}::${String(envelope?.target_profile || '')}`
try {
const res = await host.requestProfile(target.route, 'bot_relay.deliver', {
profile: String(envelope?.target_profile || ''),
message: String(envelope?.message || '')
})
clearBotAttention(attentionKey)
await postReply({ reply: String(res?.reply || '') })
} catch (error) {
noteBotAttention(attentionKey, error?.message || error)
await postReply({ error: String(error?.message || error || 'delivery failed') })
}
}
@@ -6933,8 +7031,17 @@ async function runGroupChatRounds(group, members, thread) {
try {
reply = await runGroupChatMemberTurn(group, member, prompt, thread, deltaImages)
} catch {
// Needs-attention hook (#93091 item 3): a turn that produced a real
// reply (or an explicit pass) is a good turn — clear the badge.
// A timed-out turn also returns null but never threw; leaving any
// prior badge in place there is the conservative choice.
if (reply !== null) {
clearBotAttention(groupMemberKey(member))
}
} catch (error) {
recordGroupActivity(group, { kind: 'failed', member: member.name, thread })
noteBotAttention(groupMemberKey(member), error?.message || error)
reply = null // a failed turn is a pass, never a room error
}
@@ -7447,6 +7554,18 @@ function BotRow({ bot, onDelete, onEdit, onGroup, showHandle }) {
// thin to rich; conditionally calling useValue here breaks React hook order.
const unreadByName = useValue($botUnread)
const unread = Boolean(unreadByName[botSelectionKey(bot)])
// Needs-attention badge (#93091 item 3): background failures record under
// the selection key (group turns) or the route key (relay deliveries) —
// check both. Local/unannotated rows carry no connectionId, so their relay
// failures live under `<activeConnectionId>::<name>` — resolve that shape
// too or active-gateway bots never badge. Hidden bots keep their entry;
// hiding is display-only.
const attentionByKey = useValue($botAttention)
const attention =
attentionByKey[botSelectionKey(bot)] ||
attentionByKey[botRosterKey(bot)] ||
attentionByKey[`${bot?.connectionId || activeConnectionId}::${bot?.name || 'default'}`] ||
null
// WHO sent the last message (bot-to-bot DM vs human) — the full stored
// history lives in the canonical chat, not inline.
// Preview identity must match click identity (#88200): when the backend
@@ -7552,6 +7671,16 @@ function BotRow({ bot, onDelete, onEdit, onGroup, showHandle }) {
}),
]
}),
attention
? jsx(Tip, {
label: BOT_ATTENTION_HINTS[attention.reason] || 'Needs attention',
children: jsx(Codicon, {
name: 'warning',
className: 'shrink-0 text-[0.6875rem] text-(--ui-warning,#f59e0b)',
'aria-label': 'needs attention'
})
})
: null,
unread
? jsx('span', {
className: 'size-2 shrink-0 rounded-full bg-(--ui-accent)',
@@ -0,0 +1,151 @@
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import test from 'node:test'
import vm from 'node:vm'
// Needs-attention badge (#93091 item 3): background bot failures whose class
// is attention-worthy (auth, quota, missing config, blocked) badge the roster
// tile; transient failures (rate limit, server error, timeout) never do; the
// bot's next good turn clears the badge. Display-only state — hooks live at
// the relay deliver reply and the group member turn boundary, and hidden bots
// keep their entry (hiding is roster-display-only).
const pluginSource = readFileSync(new URL('../plugin.js', import.meta.url), 'utf8')
/** Evaluate just the delimited needs-attention helper section in a bare vm
* context (same slice-and-eval approach as the other pure-helper tests). */
function loadHelpers() {
const start = pluginSource.indexOf('const BOT_ATTENTION_CLASSES')
const end = pluginSource.indexOf('/** Last good cron list')
assert.ok(start > -1 && end > start, 'needs-attention helper section exists')
const values = new Map()
const atom = initial => {
const slot = { get: () => values.get(slot), set: value => values.set(slot, value) }
values.set(slot, initial)
return slot
}
const context = { atom }
vm.createContext(context)
return vm.runInContext(
`${pluginSource.slice(start, end)}
;({ BOT_ATTENTION_CLASSES, BOT_ATTENTION_HINTS, attentionReasonFromError, $botAttention, noteBotAttention, clearBotAttention })`,
context
)
}
test('classifier: reason codes pass through; matches the #93091 item-1 enum', () => {
const { BOT_ATTENTION_CLASSES, attentionReasonFromError } = loadHelpers()
assert.deepEqual(
[...BOT_ATTENTION_CLASSES].sort(),
['agent_blocked', 'missing_config', 'provider_auth_or_access', 'provider_quota_limit']
)
for (const code of BOT_ATTENTION_CLASSES) {
assert.equal(attentionReasonFromError(code), code)
}
})
test('classifier: raw-text fallback maps current gateway error strings', () => {
const { attentionReasonFromError } = loadHelpers()
// The anthropic 401 shape observed on current main.
assert.equal(
attentionReasonFromError(
'Error code: 401 - {"type":"error","error":{"type":"authentication_error","message":"invalid x-api-key"}}'
),
'provider_auth_or_access'
)
assert.equal(
attentionReasonFromError('No LLM provider configured. Run hermes model to pick one.'),
'missing_config'
)
assert.equal(attentionReasonFromError('No access token found for profile'), 'missing_config')
assert.equal(attentionReasonFromError('Your account is out of funds'), 'provider_quota_limit')
assert.equal(attentionReasonFromError('quota exceeded for this billing period'), 'provider_quota_limit')
assert.equal(attentionReasonFromError('agent is blocked awaiting approval'), 'agent_blocked')
})
test('classifier: transient classes never badge', () => {
const { attentionReasonFromError } = loadHelpers()
for (const text of [
'Rate limit exceeded, retry shortly',
'Error code: 429 - too many requests',
'500 Internal Server Error',
'upstream 503 service unavailable',
'the model is overloaded, try again',
'request timed out after 180s',
'temporarily unavailable',
'',
null,
undefined
]) {
assert.equal(attentionReasonFromError(text), null, `expected null for ${JSON.stringify(text)}`)
}
})
test('lifecycle: set on classified failure, latest wins, cleared on success', () => {
const { $botAttention, noteBotAttention, clearBotAttention } = loadHelpers()
// Transient failure sets nothing.
noteBotAttention('radar', 'Rate limit exceeded')
assert.deepEqual(Object.keys($botAttention.get()), [])
// Classified failure badges the bot with reason + timestamp + snippet.
noteBotAttention('radar', 'Error code: 401 authentication_error')
const first = $botAttention.get().radar
assert.equal(first.reason, 'provider_auth_or_access')
assert.ok(first.at > 0)
assert.match(first.message, /401/)
// A later failure for the same bot merges — latest wins.
noteBotAttention('radar', 'No LLM provider configured')
assert.equal($botAttention.get().radar.reason, 'missing_config')
// Other bots are independent.
noteBotAttention('dixie', 'quota exceeded')
assert.equal($botAttention.get().dixie.reason, 'provider_quota_limit')
// The next good turn clears exactly that bot.
clearBotAttention('radar')
assert.equal($botAttention.get().radar, undefined)
assert.equal($botAttention.get().dixie.reason, 'provider_quota_limit')
// Clearing an unbadged bot is a no-op.
clearBotAttention('radar')
clearBotAttention('')
assert.equal($botAttention.get().dixie.reason, 'provider_quota_limit')
})
test('hooks: relay delivery and group member turns note/clear attention', () => {
// Relay drain: deliver success clears, deliver failure notes.
const drain = pluginSource.slice(
pluginSource.indexOf('async function drainRelayOutboxes'),
pluginSource.indexOf('function startBotRelay')
)
assert.match(drain, /clearBotAttention\(attentionKey\)/)
assert.match(drain, /noteBotAttention\(attentionKey, error\?\.message \|\| error\)/)
// Group member turn boundary: failure notes under the member key; a real
// reply clears it.
assert.match(pluginSource, /noteBotAttention\(groupMemberKey\(member\), error\?\.message \|\| error\)/)
assert.match(pluginSource, /clearBotAttention\(groupMemberKey\(member\)\)/)
})
test('render: roster row shows an amber warning glyph with a per-class hint', () => {
assert.match(pluginSource, /BOT_ATTENTION_HINTS\[attention\.reason\]/)
assert.match(pluginSource, /name: 'warning'/)
// Lookup covers all three key shapes so relay- and group-recorded state
// renders, including local/unannotated rows keyed by the ACTIVE connection.
assert.match(pluginSource, /attentionByKey\[botSelectionKey\(bot\)\] \|\|/)
assert.match(pluginSource, /attentionByKey\[botRosterKey\(bot\)\] \|\|/)
assert.match(pluginSource, /attentionByKey\[`\$\{bot\?\.connectionId \|\| activeConnectionId\}::\$\{bot\?\.name \|\| 'default'\}`\]/)
// Hint copy for every class.
assert.match(pluginSource, /Sign in again for this profile/)
assert.match(pluginSource, /Quota or balance exhausted/)
assert.match(pluginSource, /Provider not configured — run hermes model/)
assert.match(pluginSource, /Bot is blocked — see its last message/)
})
@@ -52,6 +52,8 @@ function renderBotRow(input = 'alpha') {
ROSTER_KEY: ['hermes-bots', 'roster'],
$botMeta: atom({}),
$botUnread: atom({}),
$botAttention: atom({}),
BOT_ATTENTION_HINTS: {},
$botChatFocused: atom(false),
$botsHomeFronted: atom(false),
$focusedBotOwner: atom({ connectionId: 'local', profile: 'default' }),
@@ -249,6 +251,8 @@ test('behavior: remote default never opens the same-name local chat', async () =
ROSTER_KEY: ['hermes-bots', 'roster'],
$botMeta: atom({ default: { chat: 'this-device-chat' } }),
$botUnread: atom({}),
$botAttention: atom({}),
BOT_ATTENTION_HINTS: {},
$botChatFocused: atom(false),
$botsHomeFronted: atom(false),
$focusedBotOwner: atom({ connectionId: 'mac-mini', profile: 'default' }),