Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

feat(bots): hydrate bot threads from the journal on open

Salman Qureshi062c5f00626e
diff --git a/src/channels/cli-bot-chat.test.ts b/src/channels/cli-bot-chat.test.ts
index 3484355d..ad44c8ca 100644
--- a/src/channels/cli-bot-chat.test.ts
+++ b/src/channels/cli-bot-chat.test.ts
@@ -55,6 +55,32 @@ describe('CLIChannel bot chat (transcript swap + target routing)', () => {
expect(channel.getTuiState().chatMessages.some(m => m.content.includes('publisher reply'))).toBe(true);
});
+ it('hydrates a cold thread from journal history (bot worked while away)', async () => {
+ const channel = new CLIChannel();
+ const history = [
+ { content: '✅ (cron, 3h ago, 12s) Weekly digest published.', timestamp: Date.now() - 3600_000 },
+ { content: '❌ (chat, 5m ago, 2s) Provider timeout.', timestamp: Date.now() - 300_000 },
+ ];
+ channel.enterBotChat('researcher', 'Research', history);
+ const msgs = channel.getTuiState().chatMessages;
+ expect(msgs.some(m => m.content.includes('bot chat'))).toBe(true);
+ expect(msgs.some(m => m.content.includes('📜') && m.content.includes('Weekly digest published.'))).toBe(true);
+ expect(msgs.some(m => m.content.includes('Provider timeout.'))).toBe(true);
+ });
+
+ it('live in-memory transcript wins over journal history (no duplication)', async () => {
+ const channel = new CLIChannel();
+ channel.enterBotChat('researcher', 'Research');
+ await channel.send('live bot reply', 'bot:researcher');
+ channel.exitBotChat();
+ // Re-open WITH history: the in-memory transcript (which holds the live
+ // message) must be used, not merged with the journal tail.
+ channel.enterBotChat('researcher', 'Research', [{ content: 'stale journal line', timestamp: 1 }]);
+ const msgs = channel.getTuiState().chatMessages;
+ expect(msgs.some(m => m.content.includes('live bot reply'))).toBe(true);
+ expect(msgs.some(m => m.content.includes('stale journal line'))).toBe(false);
+ });
+
it('persists a bot transcript across exit and re-entry', async () => {
const channel = new CLIChannel();
channel.enterBotChat('researcher', 'Research');
diff --git a/src/channels/cli.ts b/src/channels/cli.ts
index 88d7a287..f300f9e6 100644
--- a/src/channels/cli.ts
+++ b/src/channels/cli.ts
@@ -605,8 +605,9 @@ export class CLIChannel extends BaseChannel {
/** useSyncExternalStore contract: read the latest immutable state snapshot. */
// ---- Mercury Bot chat (per-bot transcripts) ------------------------------
- /** Open a bot's chat: swap the on-screen transcript to the bot's thread. */
- enterBotChat(botId: string, botName: string): void {
+ /** Open a bot's chat: swap the on-screen transcript to the bot's thread.
+ * `history` hydrates a thread with no in-memory transcript (journal tail). */
+ enterBotChat(botId: string, botName: string, history?: Array<{ content: string; timestamp: number }>): void {
if (this.activeBotId === botId) return;
if (this.activeBotId) {
this.botTranscripts.set(this.activeBotId, [...this.state.chatMessages]);
@@ -614,12 +615,24 @@ export class CLIChannel extends BaseChannel {
this.mainTranscript = [...this.state.chatMessages];
}
this.activeBotId = botId;
- const seed: ChatMessage[] = this.botTranscripts.get(botId) ?? [{
- id: `bot-open-${Date.now().toString(36)}`,
- role: 'system',
- content: `🤖 **${botName}** bot chat — everything you type here goes to the bot (runs outside the main conversation). \`/chat\` returns to the main transcript.`,
- timestamp: Date.now(),
- }];
+ // In-memory transcript wins (live session continuity); otherwise hydrate
+ // from the durable journal history the caller provides, so a thread
+ // opened after a restart (or after the bot worked unattended) shows what
+ // happened instead of an empty room. Bounded: caller passes ≤10 records.
+ const seed: ChatMessage[] = (this.botTranscripts.get(botId) ?? [
+ {
+ id: `bot-open-${Date.now().toString(36)}`,
+ role: 'system',
+ content: `🤖 **${botName}** bot chat — everything you type here goes to the bot (runs outside the main conversation). \`/chat\` returns to the main transcript.`,
+ timestamp: Date.now(),
+ },
+ ...(history ?? []).map((h, i) => ({
+ id: `bot-hist-${botId}-${i}-${h.timestamp.toString(36)}`,
+ role: 'system' as const,
+ content: `📜 ${h.content}`,
+ timestamp: h.timestamp,
+ })),
+ ]);
this.botTranscripts.set(botId, seed);
this.trimAndSetMessages(seed, { botChat: { botId, botName }, isThinking: false, liveActivity: null });
}
diff --git a/src/core/agent.ts b/src/core/agent.ts
index 40d09019..9f276aee 100644
--- a/src/core/agent.ts
+++ b/src/core/agent.ts
@@ -1482,7 +1482,13 @@ export class Agent {
return;
}
if (typeof (channel as any).enterBotChat === 'function') {
- (channel as any).enterBotChat(target, manifest.name);
+ // Hydrate a cold thread from the durable journal (last 10 runs,
+ // compact) — the bot may have worked unattended for hours.
+ const history = bm.getJournal(target, 10).map(r => ({
+ content: `${r.state === 'completed' ? '✅' : r.state === 'failed' ? '❌' : '⛔'} (${r.trigger}, ${formatRelative(r.startedAt)}${r.durationMs ? `, ${(r.durationMs / 1000).toFixed(0)}s` : ''}) ${r.summary?.slice(0, 300) ?? r.runId}`,
+ timestamp: r.startedAt,
+ })).reverse();
+ (channel as any).enterBotChat(target, manifest.name, history);
return;
}
await channel.send(`Opening a bot chat is only supported in the TUI — use \`/bots send ${target} <message>\` here.`, channelId);