Skip to content

The agent that grows with you

fix(bots): protect new drafts from title sweep

fangliquanflq1b18442f3609
diff --git a/apps/desktop/src/plugins/hermes-bots/plugin.js b/apps/desktop/src/plugins/hermes-bots/plugin.js
index b0c1c33e02a..71a3e3f823d 100644
--- a/apps/desktop/src/plugins/hermes-bots/plugin.js
+++ b/apps/desktop/src/plugins/hermes-bots/plugin.js
@@ -2093,12 +2093,25 @@ function hideOwnedBotSessions() {
// user's real conversation inside a bot profile keeps whatever title the
// user gave it and is never touched.
const BOT_MODE_SWEEP_TITLES = new Set(['Bot Chat', 'Agent Inbox'])
+const BOT_MODE_SWEEP_MIN_AGE_SECONDS = 5 * 60
function isBotModeSweepTitle(title) {
const t = String(title || '').trim()
return BOT_MODE_SWEEP_TITLES.has(t) || t.startsWith('Group: ')
}
+function isBotModeSweepCandidate(row, nowSeconds = Date.now() / 1000) {
+ const startedAt = Number(row?.started_at)
+ return (
+ row &&
+ row.id &&
+ isBotModeSweepTitle(row.title) &&
+ Number.isFinite(startedAt) &&
+ startedAt > 0 &&
+ nowSeconds - startedAt >= BOT_MODE_SWEEP_MIN_AGE_SECONDS
+ )
+}
+
/** Ownership-based sweep: the id-based sweep above only covers sessions the
* plugin recorded ($botMeta canonical chats, $groupChats member sids), but
* Bot Mode sessions are ALSO minted outside the plugin — bot-to-bot CLI
@@ -2106,8 +2119,11 @@ function isBotModeSweepTitle(title) {
* profile) — and those ids the plugin never learns. So: enumerate each
* roster bot's OWN profile sessions (only bot profiles — a non-bot profile
* is never listed, so its sessions are never touched) and hide any VISIBLE
- * row whose title is Bot Mode plumbing. session.list without include_hidden
- * returns only visible rows, which keeps the sweep naturally idempotent.
+ * row whose title is Bot Mode plumbing and whose creation grace period has
+ * elapsed. The grace period protects a new desktop draft while its first-turn
+ * title is pending; missing or future timestamps fail closed. session.list
+ * without include_hidden returns only visible rows, which keeps the sweep
+ * naturally idempotent.
* Remote-source bots route to their own connection via requestForBot.
* Feature-detected + fire-and-forget: older gateways without per-profile
* session.list / session.set_hidden simply reject and the sweep no-ops. */
@@ -2142,7 +2158,7 @@ async function sweepBotProfileSessions() {
await Promise.all(
rows
- .filter(row => row && row.id && isBotModeSweepTitle(row.title))
+ .filter(row => isBotModeSweepCandidate(row))
.map(row =>
Promise.resolve(
requestForBot(bot, 'session.set_hidden', { session_id: row.id, hidden: true, profile: name })
diff --git a/apps/desktop/src/plugins/hermes-bots/tests/hide-bot-chats.test.mjs b/apps/desktop/src/plugins/hermes-bots/tests/hide-bot-chats.test.mjs
index e66069014e6..6424f7e27ee 100644
--- a/apps/desktop/src/plugins/hermes-bots/tests/hide-bot-chats.test.mjs
+++ b/apps/desktop/src/plugins/hermes-bots/tests/hide-bot-chats.test.mjs
@@ -221,13 +221,15 @@ test('sweepBotProfileSessions hides Bot-Mode-titled rows per roster bot, and onl
const calls = []
const rowsByProfile = {
alpha: [
- { id: 'a-1', title: 'Bot Chat' },
- { id: 'a-2', title: 'Agent Inbox' },
- { id: 'a-3', title: 'Group: Core' },
- { id: 'a-4', title: 'My real conversation' },
- { id: 'a-5', title: 'Bot Chat notes' } // not an exact title — kept
+ { id: 'a-1', title: 'Bot Chat', started_at: 1 },
+ { id: 'a-2', title: 'Agent Inbox', started_at: 1 },
+ { id: 'a-3', title: 'Group: Core', started_at: 1 },
+ { id: 'a-4', title: 'My real conversation', started_at: 1 },
+ { id: 'a-5', title: 'Bot Chat notes', started_at: 1 }, // not an exact title — kept
+ { id: 'a-6', title: 'Bot Chat', started_at: Date.now() / 1000 }, // live draft — kept
+ { id: 'a-7', title: 'Agent Inbox' } // missing age metadata — kept fail-closed
],
- remy: [{ id: 'r-1', title: 'Agent Inbox' }]
+ remy: [{ id: 'r-1', title: 'Agent Inbox', started_at: 1 }]
}
const context = {
host: { request: async () => ({}) },
@@ -256,7 +258,7 @@ test('sweepBotProfileSessions hides Bot-Mode-titled rows per roster bot, and onl
assert.deepEqual(
hidden.map(c => c.params.session_id).sort(),
['a-1', 'a-2', 'a-3', 'r-1'],
- 'exact plumbing titles only — user-titled rows in bot profiles stay visible'
+ 'exact plumbing titles only — user-titled and brand-new rows stay visible'
)
assert.ok(hidden.every(c => c.params.hidden === true))
// Remote-source bots route through requestForBot with their own bot row.