The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test(desktop): focus group-to-local-bot handoff on close-callback RED
funky-xamarin7c255dbbbfc3
diff --git a/apps/desktop/src/plugins/hermes-bots/tests/group-to-local-bot-handoff.test.mjs b/apps/desktop/src/plugins/hermes-bots/tests/group-to-local-bot-handoff.test.mjsindex 4eba0e57ead..86e3957d083 100644--- a/apps/desktop/src/plugins/hermes-bots/tests/group-to-local-bot-handoff.test.mjs+++ b/apps/desktop/src/plugins/hermes-bots/tests/group-to-local-bot-handoff.test.mjs@@ -3,348 +3,147 @@ import { readFileSync } from 'node:fs'import test from 'node:test'import vm from 'node:vm'-// #89834 — opening a local bot while a group owns the main workspace must-// retire that group's registered main tab (and clear the selection atom)-// BEFORE any async source prep / canonical open. Remote bots stay put.-//-// Behavioral seam (same style as profile-prewarm): render real BotRow /-// ActiveNow open handlers and assert close-before-open ordering. Source-// regex is not the load-bearing proof.--const source = readFileSync(new URL('../plugin.js', import.meta.url), 'utf8')--function sourceBetween(start, end) {- const from = source.indexOf(start)- const to = source.indexOf(end, from)-- assert.notEqual(from, -1, `missing ${start}`)- assert.notEqual(to, -1, `missing ${end}`)-- return source.slice(from, to)+// #89834 — local open invokes registered group main-tab closer before canonical open.+const pluginSource = readFileSync(new URL('../plugin.js', import.meta.url), 'utf8')+const atom = i => {+ let v = i+ return { get: () => v, set: n => { v = typeof n === 'function' ? n(v) : n }, listen: () => () => {} }}--// ACTIVE_WINDOW_S through activeBots (includes botActivitySession + workerActiveAt).-function activeBotsSource() {- return sourceBetween('const ACTIVE_WINDOW_S', '// ── bot row ─')-}--function handoffHelpersSource() {- return (- sourceBetween('function closeGroupChatMainTab(group) {', '/** Local-bot open handoff:') +- sourceBetween('function dismissGroupChatForLocalBotOpen() {', '/** Main-window wrapper:')- )-}--/** Extract `bot => { ... }` assigned to ActiveNowStrip's onOpen in BotsPane. */-function activeNowOnOpenSource() {- const strip = source.indexOf('jsx(ActiveNowStrip, {')- assert.ok(strip >= 0, 'ActiveNowStrip mount must remain in BotsPane')-- const key = source.indexOf('onOpen: bot => {', strip)- assert.ok(key > strip, 'ActiveNowStrip onOpen handler must remain inline')-- const start = key + 'onOpen: '.length- assert.equal(source.slice(start, start + 'bot => {'.length), 'bot => {')-- let i = start + 'bot => {'.length- let depth = 1-- while (i < source.length && depth > 0) {- const ch = source[i]- if (ch === '{') depth += 1- else if (ch === '}') depth -= 1- i += 1+const jsx = (t, p = {}) => ({ type: t, props: p })+const find = (tree, pred) => {+ if (tree == null || typeof tree !== 'object') return null+ if (Array.isArray(tree)) {+ for (const c of tree) { const f = find(c, pred); if (f) return f }+ return null}-- assert.equal(depth, 0, 'ActiveNowStrip onOpen braces must balance')- return source.slice(start, i)-}--function atom(initial) {- let value = initial- return {- get: () => value,- set: next => {- value = typeof next === 'function' ? next(value) : next- }- }-}--function node(type, props = {}) {- return { type, props }+ return pred(tree) ? tree : find(tree.props?.children, pred)}-function findButton(tree, predicate = () => true) {- const visit = value => {- if (value == null || typeof value !== 'object') return null- if (Array.isArray(value)) {- for (const child of value) {- const found = visit(child)- if (found) return found- }- return null- }- if (value.type === 'button' && predicate(value)) return value- return visit(value.props?.children)- }- return visit(tree)-}--function loadCallers() {- const timeline = []- const notifications = []- const groupChatMainTabs = new Map()- const $groupChatWorkspace = atom(null)- const $selectedBot = atom('default')- const $botUnread = atom({})- const $botMeta = atom({})- const $focusedBotProfile = atom('default')- const $lastRoster = atom([])-- const prepareSource = sourceBetween('async function prepareBotSource(', 'function displayName(')- const botRowSource = sourceBetween('function BotRow(', '// ── model picker')- const activeNowSource = sourceBetween(- 'function ActiveNowStrip({ roster, activeProfile, gatewayState, metaByName, onOpen }) {',- '/** Assign a bot to a group-chat membership'- )-- const context = {- BotFace: 'BotFace',- ContextMenu: 'ContextMenu',- ContextMenuContent: 'ContextMenuContent',- ContextMenuItem: 'ContextMenuItem',- ContextMenuSeparator: 'ContextMenuSeparator',- ContextMenuTrigger: 'ContextMenuTrigger',- ROSTER_KEY: ['hermes-bots', 'roster'],- groupChatMainTabs,- $groupChatWorkspace,- $selectedBot,- $botUnread,- $botMeta,- $focusedBotProfile,- $lastRoster,- botAppearance: () => ({ shape: 'round', color: '#000', image: null }),- botGroups: () => [],- botHandle: value => value,- botOpenGeneration: 0,- botRosterKey: bot => bot.name,- botRosterMeta: (bot, metaByName) => metaByName?.[bot.name] ?? null,- cn: (...values) => values.filter(Boolean).join(' '),- createCanonicalChat: async () => null,- displayName: (bot, _meta) => bot?.title || bot?.name || 'bot',- duplicateBot: async () => 'copy',- haptic: () => undefined,- isBackfilledFacePng: () => false,- previewKind: () => ({ fromBot: false, sender: null }),- generatedSessionTitle: () => null,- openBotCanonicalChat: async (...args) => {- timeline.push({ type: 'openBotCanonicalChat', args })- return 'stored-chat'- },- ACTIVE_WINDOW_S: 90,- A2A_PREFIX_RE: /^$/,- useEffect: () => undefined,- useState: initial => [typeof initial === 'function' ? initial() : initial, () => undefined],- host: {- state: { gateway: atom('open'), profile: atom('default') },- ensureAgent: async () => undefined,- activeConnectionId: () => 'local',- warmAgent: () => undefined,- warmProfile: () => undefined,- request: async () => ({ profiles: [], sessions: [] }),- notify: payload => notifications.push(payload),- notifyError: () => undefined,- newChat: () => timeline.push({ type: 'newChat' }),- navigate: () => timeline.push({ type: 'navigate' })+function load() {+ const timeline = [], notifications = []+ const host = {+ state: {+ profile: { get: () => 'other', listen: () => {} },+ gateway: { get: () => 'open', listen: () => {} },+ connectionId: { get: () => 'local', listen: () => {} }},- jsx: node,- jsxs: node,- onEdit: () => undefined,- queryClient: { invalidateQueries: () => undefined },- relativeTime: () => 'now',- saveBotMeta: () => undefined,- showsHandle: () => false,- stripPreviewMarkdown: text => String(text || ''),- useValue: store => store.get(),- allMeta: {},- timeline,- notifications+ request: async m => m === 'session.create'+ ? { stored_session_id: 'stored-chat', session_id: 'rt-chat' }+ : { profiles: [], sessions: [] },+ notify: p => notifications.push(p),+ notifyError: () => {}, openSession: async () => {}, ensureAgent: async () => {},+ activeConnectionId: () => 'local', warmAgent: () => {}, warmProfile: () => {},+ newChat: () => timeline.push({ type: 'newChat' }),+ navigate: () => timeline.push({ type: 'navigate' })}-- // Bind Map + atoms on the sandbox so helper source closes over the live bindings.- context.groupChatMainTabs = groupChatMainTabs- context.$groupChatWorkspace = $groupChatWorkspace-- const code = [- activeBotsSource(),- handoffHelpersSource(),- prepareSource,- botRowSource,- activeNowSource,- 'const openActiveNow = ' + activeNowOnOpenSource() + ';',- 'globalThis.__callers = { BotRow, ActiveNowStrip, openActiveNow, dismissGroupChatForLocalBotOpen, closeGroupChatMainTab };'- ].join('\n')-- vm.runInNewContext(code, context)-- const registerGroupMainTab = group => {+ const ui = 'Button Checkbox Codicon ConfirmDialog ContextMenu ContextMenuContent ContextMenuItem ContextMenuSeparator ContextMenuTrigger CopyButton Dialog DialogContent DialogDescription DialogFooter DialogHeader DialogTitle DropdownMenu DropdownMenuContent DropdownMenuItem DropdownMenuTrigger EmptyState GlyphSpinner Input ScrollArea SearchField Select SelectContent SelectItem SelectTrigger SelectValue Switch Textarea Tip'.split(' ')+ const ctx = {+ atom, jsx, jsxs: jsx, cn: (...a) => a.filter(Boolean).join(' '), haptic: () => {},+ useEffect: () => {}, useRef: i => ({ current: typeof i === 'function' ? i() : i }),+ useState: i => [typeof i === 'function' ? i() : i, () => {}],+ useValue: s => (s && typeof s.get === 'function' ? s.get() : s),+ useQuery: () => ({ data: [], isLoading: false, isFetching: false, refetch: () => {} }),+ ...Object.fromEntries(ui.map(n => [n, n])),+ PALETTE_AREA: 'palette', COMPOSER_AREAS: { middleware: 'middleware' },+ profileColor: () => '#000', queryClient: { invalidateQueries: () => {} }, relativeTime: () => 'now',+ document: { getElementById: () => null, createElement: () => ({}), head: { appendChild: () => {} } },+ setTimeout, clearTimeout, console, Date, Math, JSON, Promise, Map, Set, URL, Error,+ Array, Object, String, Boolean, Number, RegExp, host+ }+ const code = pluginSource+ .replace(/^import\s+\*\s+as\s+sdk\s+from '@hermes\/plugin-sdk'\r?\n/m, '')+ .replace(/^import\s+\{[\s\S]*?\}\s+from '@hermes\/plugin-sdk'\r?\n/m, '')+ .replace(/^const \{ McpTab, ToolsetConfigPanel \} = sdk\r?\n/m, '')+ .replace(/^import .* from 'react'\r?\n/m, '')+ .replace(/^import .* from 'react\/jsx-runtime'\r?\n/m, '')+ .replace('export default {', 'globalThis.plugin = {')+ .concat(`\nglobalThis.__h={BotRow,ActiveNowStrip,BotsPane,openGroupChat,groupChatMainTabs,$groupChatWorkspace};`)+ vm.runInNewContext(code, ctx, { filename: 'plugin.js' })+ // Live binding: handlers resolve this mutable global at call time.+ ctx.openBotCanonicalChat = async (...a) => { timeline.push({ type: 'openBotCanonicalChat', args: a }); return 'stored-chat' }+ const api = ctx.__h+ const registerGroup = group => {let closed = 0- $groupChatWorkspace.set(group)- groupChatMainTabs.set(group, () => {- closed += 1- timeline.push({ type: 'mainTabClose', group })- })- return {- closed: () => closed,- isRegistered: () => groupChatMainTabs.has(group)- }+ ctx.host.openWorkspace = () => () => { closed += 1; timeline.push({ type: 'mainTabClose', group }) }+ api.openGroupChat(group)+ assert.equal(api.$groupChatWorkspace.get(), group)+ assert.ok(api.groupChatMainTabs.has(group))+ return { closed: () => closed, registered: () => api.groupChatMainTabs.has(group) }}-- return {- ...context.__callers,- $groupChatWorkspace,- $selectedBot,- groupChatMainTabs,- timeline,- notifications,- registerGroupMainTab,- renderBotRow(bot) {- const tree = context.__callers.BotRow({ bot, onEdit: () => undefined })- const row = tree.type === 'button' ? tree : tree.props.children[0].props.children- return row- },- renderActiveNowChip(bot, { gatewayState = 'open', activeProfile = 'other' } = {}) {- // Force the bot into the strip via a fresh last_session inside the window.- const rosterBot = {- ...bot,- last_session: bot.last_session || {- id: 'live',- last_active: Math.floor(Date.now() / 1000) - 5- }- }- const tree = context.__callers.ActiveNowStrip({- roster: [rosterBot],- activeProfile,- gatewayState,- metaByName: {},- onOpen: context.__callers.openActiveNow- })- const chip = findButton(tree, button => String(button.props?.title || '').includes(bot.name) ||- String(button.props?.title || '').includes(bot.title || ''))- assert.ok(chip, 'Active Now chip must render for the seeded bot')- return chip- }+ const botRow = bot => {+ const tree = api.BotRow({ bot, onEdit: () => {} })+ const row = tree.type === 'button' ? tree : tree.props.children[0].props.children+ assert.equal(typeof row.props.onClick, 'function')+ return row+ }+ // Real Active Now onOpen from BotsPane (not a hand-copied fragment).+ const strip = find(api.BotsPane(), n => n?.props && typeof n.props.onOpen === 'function' && 'roster' in n.props)+ assert.ok(strip, 'BotsPane mounts ActiveNowStrip with onOpen')+ const onOpen = strip.props.onOpen+ const activeChip = bot => {+ const b = { ...bot, last_session: bot.last_session || { id: 'live', last_active: Math.floor(Date.now() / 1000) - 5 } }+ const tree = api.ActiveNowStrip({ roster: [b], activeProfile: 'other', gatewayState: 'open', metaByName: {}, onOpen })+ const chip = find(tree, n => n.type === 'button' && /Open /.test(String(n.props?.title || ''))+ && (String(n.props.title).includes(bot.name) || String(n.props.title).includes(bot.title || '')))+ assert.ok(chip, 'Active Now chip renders')+ return chip}+ return { api, timeline, notifications, registerGroup, botRow, activeChip }}-test('BotRow local open closes the group main tab before canonical open', async () => {- const runtime = loadCallers()- const tab = runtime.registerGroupMainTab('Core')- const row = runtime.renderBotRow({ name: 'alpha', title: 'Alpha' })-- await row.props.onClick()+const drain = async () => { await new Promise(r => setImmediate(r)); await new Promise(r => setImmediate(r)) }+const assertCloseBeforeOpen = tl => {+ const c = tl.findIndex(e => e.type === 'mainTabClose'), o = tl.findIndex(e => e.type === 'openBotCanonicalChat')+ assert.ok(c >= 0, 'registered main-tab closer must run')+ assert.ok(o >= 0, 'local open must reach canonical chat')+ assert.ok(c < o, 'close must precede canonical open')+}+const remote = { name: 'research', title: 'Research', remoteSource: true, sourceScoped: true, connectionId: 'work', connectionLabel: 'Work' }+test('BotRow local open closes group main tab before canonical open', async () => {+ const r = load(), tab = r.registerGroup('Core')+ await r.botRow({ name: 'alpha', title: 'Alpha' }).props.onClick()assert.equal(tab.closed(), 1, 'registered main-tab closer must run')- assert.equal(runtime.$groupChatWorkspace.get(), null, 'group selection atom must clear')- assert.equal(tab.isRegistered(), false, 'closer entry must be retired')- assert.ok(- runtime.timeline.some(entry => entry.type === 'openBotCanonicalChat'),- 'local open must proceed to canonical chat'- )-- const closeAt = runtime.timeline.findIndex(entry => entry.type === 'mainTabClose')- const openAt = runtime.timeline.findIndex(entry => entry.type === 'openBotCanonicalChat')- assert.ok(closeAt >= 0 && openAt >= 0 && closeAt < openAt, 'close must precede canonical open')+ assert.equal(r.api.$groupChatWorkspace.get(), null)+ assert.equal(tab.registered(), false)+ assertCloseBeforeOpen(r.timeline)})-test('BotRow remote open leaves the group main tab open', async () => {- const runtime = loadCallers()- const tab = runtime.registerGroupMainTab('Core')- const row = runtime.renderBotRow({- name: 'research',- title: 'Research',- remoteSource: true,- sourceScoped: true,- connectionId: 'work',- connectionLabel: 'Work'- })-- await row.props.onClick()-+test('BotRow remote open does not dismiss group main tab', async () => {+ const r = load(), tab = r.registerGroup('Core')+ await r.botRow(remote).props.onClick()assert.equal(tab.closed(), 0, 'remote open must not dismiss the group tab')- assert.equal(runtime.$groupChatWorkspace.get(), 'Core')- assert.equal(tab.isRegistered(), true)- assert.equal(- runtime.timeline.some(entry => entry.type === 'openBotCanonicalChat'),- false,- 'remote open must not hop into a canonical chat'- )- assert.ok(runtime.notifications.some(n => /Stay in this chat/.test(n.message || '')))+ assert.equal(r.api.$groupChatWorkspace.get(), 'Core')+ assert.equal(tab.registered(), true)+ assert.equal(r.timeline.some(e => e.type === 'openBotCanonicalChat'), false)+ assert.ok(r.notifications.some(n => /Stay in this chat/.test(n.message || '')))})-test('Active Now local open closes the group main tab before canonical open', async () => {- const runtime = loadCallers()- const tab = runtime.registerGroupMainTab('Ops')- const chip = runtime.renderActiveNowChip({ name: 'alpha', title: 'Alpha' })-- await chip.props.onClick()- // Active Now fires canonical open inside an async IIFE — drain microtasks.- await new Promise(resolve => setImmediate(resolve))- await new Promise(resolve => setImmediate(resolve))-+test('Active Now local open closes group main tab before canonical open', async () => {+ const r = load(), tab = r.registerGroup('Ops')+ await r.activeChip({ name: 'alpha', title: 'Alpha' }).props.onClick()+ await drain()assert.equal(tab.closed(), 1, 'registered main-tab closer must run')- assert.equal(runtime.$groupChatWorkspace.get(), null)- assert.equal(tab.isRegistered(), false)- assert.ok(runtime.timeline.some(entry => entry.type === 'openBotCanonicalChat'))-- const closeAt = runtime.timeline.findIndex(entry => entry.type === 'mainTabClose')- const openAt = runtime.timeline.findIndex(entry => entry.type === 'openBotCanonicalChat')- assert.ok(closeAt >= 0 && openAt >= 0 && closeAt < openAt, 'close must precede canonical open')-})--test('Active Now remote open leaves the group main tab open', async () => {- const runtime = loadCallers()- const tab = runtime.registerGroupMainTab('Ops')- const chip = runtime.renderActiveNowChip({- name: 'research',- title: 'Research',- remoteSource: true,- sourceScoped: true,- connectionId: 'work',- connectionLabel: 'Work'- })-- await chip.props.onClick()- await new Promise(resolve => setImmediate(resolve))-- assert.equal(tab.closed(), 0)- assert.equal(runtime.$groupChatWorkspace.get(), 'Ops')- assert.equal(tab.isRegistered(), true)- assert.equal(- runtime.timeline.some(entry => entry.type === 'openBotCanonicalChat'),- false- )- assert.ok(runtime.notifications.some(n => /Stay in this chat/.test(n.message || '')))+ assert.equal(r.api.$groupChatWorkspace.get(), null)+ assert.equal(tab.registered(), false)+ assertCloseBeforeOpen(r.timeline)})-test('local BotRow open with no main-tab registration still clears in-panel selection', async () => {- const runtime = loadCallers()- runtime.$groupChatWorkspace.set('Legacy')- assert.equal(runtime.groupChatMainTabs.has('Legacy'), false)-- const row = runtime.renderBotRow({ name: 'alpha', title: 'Alpha' })- await row.props.onClick()-- assert.equal(runtime.$groupChatWorkspace.get(), null)- assert.ok(runtime.timeline.some(entry => entry.type === 'openBotCanonicalChat'))+test('Active Now remote open does not dismiss group main tab', async () => {+ const r = load(), tab = r.registerGroup('Ops')+ await r.activeChip(remote).props.onClick(); await drain()+ assert.equal(tab.closed(), 0, 'remote open must not dismiss the group tab')+ assert.equal(r.api.$groupChatWorkspace.get(), 'Ops')+ assert.equal(tab.registered(), true)})-test('local BotRow open with no group selected is safe and still opens chat', async () => {- const runtime = loadCallers()- assert.equal(runtime.$groupChatWorkspace.get(), null)-- const row = runtime.renderBotRow({ name: 'alpha', title: 'Alpha' })- await assert.doesNotReject(async () => row.props.onClick())- assert.equal(runtime.$groupChatWorkspace.get(), null)- assert.ok(runtime.timeline.some(entry => entry.type === 'openBotCanonicalChat'))+test('no group / old-host: local open is safe and still opens chat', async () => {+ const r = load()+ assert.equal(r.api.$groupChatWorkspace.get(), null)+ await assert.doesNotReject(() => r.botRow({ name: 'alpha', title: 'Alpha' }).props.onClick())+ assert.ok(r.timeline.some(e => e.type === 'openBotCanonicalChat'))+ r.api.$groupChatWorkspace.set('Legacy')+ assert.equal(r.api.groupChatMainTabs.has('Legacy'), false)+ await r.botRow({ name: 'beta', title: 'Beta' }).props.onClick()+ assert.equal(r.api.$groupChatWorkspace.get(), null)})