Skip to content

The agent that grows with you

fix(desktop): bound the persisted group-chat log by characters, not just entries

teknium19048fbe27f06
diff --git a/apps/desktop/src/plugins/hermes-bots/group-chat.test.ts b/apps/desktop/src/plugins/hermes-bots/group-chat.test.ts
index 8d5ae2269cb..3d81a91b85f 100644
--- a/apps/desktop/src/plugins/hermes-bots/group-chat.test.ts
+++ b/apps/desktop/src/plugins/hermes-bots/group-chat.test.ts
@@ -80,6 +80,30 @@ describe('log window', () => {
expect(trimmed.watermarks.research).toBe(150 - 104)
expect(trimmed.watermarks.builder).toBe(0)
})
+
+ // The whole room map is persisted to localStorage on every write; a room
+ // of long bodies must stay a small fraction of the origin quota or the
+ // swallowed setItem failure silently loses every later room write.
+ it('persists a 400-long-body room under the character budget, watermarks intact', async () => {
+ const { chat, gateway } = await loadRoom()
+ const body = 'x'.repeat(64_000)
+
+ for (let i = 0; i < chat.GROUP_CHAT_LOG_RETAIN; i++) {
+ chat.appendGroupChatEntry('Core', { kind: 'user', name: 'You' }, `${i} ${body}`, 't1')
+ }
+
+ chat.updateGroupChat('Core', room => ({ ...room, watermarks: { builder: room.log.length } }), { sync: false })
+
+ const stored = (gateway.storage.get('group-chats') as Record<string, GroupChat>).Core
+ const chars = stored.log.reduce((sum, entry) => sum + entry.text.length, 0)
+
+ expect(chars).toBeLessThanOrEqual(chat.GROUP_CHAT_LOG_RETAIN_CHARS)
+ expect(JSON.stringify(stored).length).toBeLessThan(chat.GROUP_CHAT_LOG_RETAIN_CHARS * 1.25)
+ expect(Math.max(...stored.log.map(entry => entry.text.length))).toBeLessThanOrEqual(chat.GROUP_CHAT_HISTORY_LINE_CHARS)
+ expect(stored.log.at(-1)?.text.startsWith(`${chat.GROUP_CHAT_LOG_RETAIN - 1} `)).toBe(true)
+ expect(stored.log.at(-1)?.text.endsWith('… [truncated]')).toBe(true)
+ expect(stored.watermarks.builder).toBe(stored.log.length)
+ })
})
describe('room naming', () => {
diff --git a/apps/desktop/src/plugins/hermes-bots/group-chat.ts b/apps/desktop/src/plugins/hermes-bots/group-chat.ts
index 5b87510706a..12f2bf108e9 100644
--- a/apps/desktop/src/plugins/hermes-bots/group-chat.ts
+++ b/apps/desktop/src/plugins/hermes-bots/group-chat.ts
@@ -1277,12 +1277,13 @@ export const GROUP_CHAT_MAX_CONTINUATIONS = 2
// Per-turn room window (#114341 follow-up): a member sees every message since
// its last turn, up to BOTH ceilings — oldest dropped first, the cut named
// exactly. Room lines are short by construction (the rules ask for 1-3
-// sentences; user lines average ~100-300 chars), so ~200 entries and ~32 KB
-// (~8k tokens) bite at about the same place for ordinary traffic; the char
-// budget is what keeps a prompt bounded when the lines are long. One body
-// is cut to LINE_CHARS (mark: '… [truncated]') rather than evicting whole
-// messages, so a single giant paste costs a quarter of the window, not all
-// of it, while a multi-paragraph member result still lands intact.
+// sentences; user lines average ~100-300 chars), so ~200 entries and ~32k
+// characters (~8k tokens; budgets are String.length code units, not bytes)
+// bite at about the same place for ordinary traffic; the char budget is
+// what keeps a prompt bounded when the lines are long. One body is cut to
+// LINE_CHARS (mark: '… [truncated]') rather than evicting whole messages, so
+// a single giant paste costs a quarter of the window, not all of it, while a
+// multi-paragraph member result still lands intact.
export const GROUP_CHAT_HISTORY_LIMIT = 200
export const GROUP_CHAT_HISTORY_CHARS = 32_000
export const GROUP_CHAT_HISTORY_LINE_CHARS = 8_000
@@ -1290,6 +1291,16 @@ export const GROUP_CHAT_HISTORY_LINE_CHARS = 8_000
// a whole window still receives an exact omitted count, not a clamped
// watermark and a silently shortened room.
export const GROUP_CHAT_LOG_RETAIN = GROUP_CHAT_HISTORY_LIMIT * 2
+// Storage footprint of the retained log. updateGroupChat persists the WHOLE
+// room map to localStorage (~5M-char origin quota, a failed setItem is
+// swallowed and every later room write is lost with it). Measured on the
+// real persist path: 400 uncapped 8k bodies serialise to 3.25M chars, 400
+// 64k pastes to 25.6M. Stored bodies are therefore cut to the same
+// LINE_CHARS the turn prompt renders (nothing past it ever reaches a member)
+// and the log is head-trimmed to a character budget: 8x the turn window, so
+// ordinary traffic never hits it and a room of back-to-back pastes stays
+// well under a tenth of the quota.
+export const GROUP_CHAT_LOG_RETAIN_CHARS = GROUP_CHAT_HISTORY_CHARS * 8
export const GROUP_CHAT_MAX_MEMBERS = 6
/** Transcript form of a room speaker's identity. Friendly identity wins:
@@ -1379,20 +1390,43 @@ export function groupSpeakerLabel(name?: null | string, group?: null | string) {
}
/** Trim a room log + its watermarks to the retained window, keeping
- * watermark indices consistent with the trimmed array. */
+ * watermark indices consistent with the trimmed array. Runs on every
+ * updateGroupChat, i.e. right before the room map is persisted: entries are
+ * bounded by count AND by stored characters (each body cut to the prompt's
+ * LINE_CHARS, then the oldest dropped until the log fits the char budget). */
export function trimGroupChatLog(
log: GroupMessage[],
watermarks: Record<string, number>,
- limit = GROUP_CHAT_LOG_RETAIN
+ limit = GROUP_CHAT_LOG_RETAIN,
+ chars = GROUP_CHAT_LOG_RETAIN_CHARS
) {
- if (log.length <= limit) {
+ const capped = log.map(entry =>
+ entry.text.length > GROUP_CHAT_HISTORY_LINE_CHARS
+ ? { ...entry, text: compactGroupChatSyncText(entry.text, GROUP_CHAT_HISTORY_LINE_CHARS).text }
+ : entry
+ )
+
+ let total = 0
+ let keep = 0
+
+ for (let i = capped.length - 1; i >= 0 && keep < limit; i--) {
+ total += capped[i].text.length
+
+ if (keep && total > chars) {
+ break
+ }
+
+ keep++
+ }
+
+ if (keep >= log.length) {
return {
- log,
+ log: capped,
watermarks
}
}
- const drop = log.length - limit
+ const drop = log.length - keep
const trimmed: Record<string, number> = {}
for (const [name, index] of Object.entries(watermarks || {})) {
@@ -1400,7 +1434,7 @@ export function trimGroupChatLog(
}
return {
- log: log.slice(drop),
+ log: capped.slice(drop),
watermarks: trimmed
}
}
@@ -1555,7 +1589,9 @@ export function appendGroupChatEntry(
id: groupChatEntryId(),
at: Date.now(),
from,
- text: normalizeGroupChatText(text),
+ // Stored bodies share the prompt's per-line cap (see trimGroupChatLog);
+ // cutting here too keeps the duplicate-echo guard comparing like with like.
+ text: compactGroupChatSyncText(normalizeGroupChatText(text), GROUP_CHAT_HISTORY_LINE_CHARS).text,
thread: thread || 'legacy'
}
diff --git a/website/docs/user-guide/bot-mode.md b/website/docs/user-guide/bot-mode.md
index 0e52fe5fa42..e6dbf1772ec 100644
--- a/website/docs/user-guide/bot-mode.md
+++ b/website/docs/user-guide/bot-mode.md
@@ -119,7 +119,7 @@ Right-click a local Bot → **Manage groups** to add or remove it from any numbe
To edit an existing room as a unit, use **Manage members** — from the room header's people icon or the **Manage members…** button in **Group settings**. The checklist pre-selects the current members and lists every local and Connections Bot; add or remove several at once and **Save members** applies the new roster (still 2–6 Bots) while the room, its history and each member's room session stay in place. A removed Bot takes no further turns; an added Bot reads the recent room history on its first turn. **Cancel** changes nothing.
-Each member's turn carries every room message since its own last turn — up to about 200 messages or 32 KB of transcript, whichever fills first — with a single oversized paste cut to a marked excerpt rather than crowding out the messages around it; when the window still overflows, the prompt names exactly how many earlier messages were left out.
+Each member's turn carries every room message since its own last turn — up to about 200 messages or 32,000 characters of transcript, whichever fills first — with a single oversized message cut to a marked 8,000-character excerpt (the room log stores the same excerpt) rather than crowding out the messages around it; when the window still overflows, the prompt names exactly how many earlier messages were left out.
**Rooms follow your gateways, not one Desktop.** Each room's recent transcript, members, picture, and name are mirrored into the shared profile metadata of **every** gateway your Desktop is connected to, with per-gateway versioning so two Desktops writing at once merge instead of overwriting each other. Open Hermes Desktop on another machine against the same gateway (local network, Tailscale, anywhere) and the room appears with its history; gateway-only clients see it too. Rooms carry a durable internal identity, so renaming one changes just its display name everywhere, disbanding one removes it permanently on every client — even ones that were offline at the time — and recreating a same-name group starts a genuinely fresh room. If a gateway dies or is removed, nothing is lost: every connected Desktop keeps the full room locally and re-seeds any gateway it reconnects to. (The full orchestration log stays in each Desktop's local storage; the shared mirror is a bounded recent-history projection.)