Skip to content

A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.

Keep long messages out of state.json and turn down empty ones

Fei2411cc4fdb94f262
diff --git a/ds-bot/client.js b/ds-bot/client.js
index 9b5e894a..010c171a 100644
--- a/ds-bot/client.js
+++ b/ds-bot/client.js
@@ -437,6 +437,9 @@ var ZH = {
"Bot exchange transcript": "Bot 间对话记录",
"No messages yet": "还没有消息",
"Close Chat": "关闭",
+ "Show all · {count} characters": "显示全部 · {count} 字",
+ "Showing the first {count} characters": "只显示前 {count} 字",
+ "Only the start of this message is kept here": "这里只保留了这条消息的开头",
// Time and usage
"Today {time}": "今天 {time}",
"Yesterday {time}": "昨天 {time}",
@@ -2148,6 +2151,9 @@ button.bt-q-opt:active{background:var(--bt-active)}
.bt-exchange .bt-dialog{position:relative}
.bt-exchange .bt-dialog-log{max-width:none;margin:0;padding:39px max(16px,calc((100% - 960px)/2)) 76px;scrollbar-width:thin}
.bt-exchange .bt-tools{display:none}
+.bt-exchange .bt-whole{align-self:flex-start;margin-left:30px}
+.bt-exchange .bt-whole[data-note]{cursor:default}
+.bt-exchange .bt-whole[data-note]:hover{color:var(--bt-ink-3)}
.bt-exchange .bt-dialog-foot{position:absolute;left:0;right:0;bottom:0;height:76px;box-sizing:border-box;align-items:flex-end;padding:0 0 28px;background:var(--bt-main);z-index:3;animation:bt-foot-in .3s cubic-bezier(.22,1,.36,1) .3s both}
.bt-exchange .bt-dialog-foot::before{content:"";position:absolute;left:0;right:0;bottom:100%;height:40px;background:linear-gradient(to top,var(--bt-main),transparent);pointer-events:none}
.bt-exchange[data-leaving] .bt-dialog-foot{animation:bt-foot-out .22s cubic-bezier(.4,0,1,1) forwards}
@@ -8612,9 +8618,23 @@ ${listed}`, { axis: "y", ...QUICK_EDGES });
// src/client/exchange.js
var import_react21 = require("react");
var SHEET_EXIT_MS = 220;
+function WholeNote({ entry, read, onShow }) {
+ if (read === void 0 || read === "loading") {
+ return (0, import_react21.createElement)("button", { type: "button", className: "bt-more bt-whole", disabled: read === "loading", onClick: onShow }, t("Show all · {count} characters", { count: entry.length.toLocaleString() }));
+ }
+ if (read.whole) return null;
+ const note = typeof read.text === "string" && read.text.length > entry.text.length ? t("Showing the first {count} characters", { count: read.text.length.toLocaleString() }) : t("Only the start of this message is kept here");
+ return (0, import_react21.createElement)("span", { className: "bt-more bt-whole", "data-note": "" }, note);
+}
function ExchangeDialog({ pair, roster, actions }) {
const [entries2, setEntries] = (0, import_react21.useState)(null);
const [leaving, setLeaving] = (0, import_react21.useState)(false);
+ const [wholes, setWholes] = (0, import_react21.useState)({});
+ const showAll = async (id) => {
+ setWholes((current) => ({ ...current, [id]: "loading" }));
+ const read = await actions.exchangeText(id);
+ setWholes((current) => ({ ...current, [id]: read ?? { whole: false } }));
+ };
const close = () => {
if (leaving) return;
setLeaving(true);
@@ -8667,6 +8687,9 @@ function ExchangeDialog({ pair, roster, actions }) {
const bot = roster.byId[entry.from] ?? { id: entry.from, name: t("You"), color: "gray" };
const showDay = entry.time - lastDay > 30 * 60 * 1e3;
lastDay = entry.time;
+ const read = wholes[entry.id];
+ const cut = entry.length !== void 0;
+ const text = typeof read?.text === "string" ? read.text : cut ? `${entry.text}…` : entry.text;
return (0, import_react21.createElement)(
import_react21.Fragment,
{ key: entry.id },
@@ -8675,7 +8698,8 @@ function ExchangeDialog({ pair, roster, actions }) {
"div",
{ className: "bt-group-msg" },
(0, import_react21.createElement)("span", { className: "bt-group-name", style: { color: inkText(colorOf(bot)) } }, bot.name),
- (0, import_react21.createElement)(Bubbles, { texts: [entry.text], roster, selfId: bot.id, actions, lead: (0, import_react21.createElement)("span", { className: "bt-lead" }, (0, import_react21.createElement)(BotMark, { bot, size: 22 })) })
+ (0, import_react21.createElement)(Bubbles, { texts: [text], roster, selfId: bot.id, actions, lead: (0, import_react21.createElement)("span", { className: "bt-lead" }, (0, import_react21.createElement)(BotMark, { bot, size: 22 })) }),
+ cut ? (0, import_react21.createElement)(WholeNote, { entry, read, onShow: () => showAll(entry.id) }) : null
)
);
})
@@ -13930,6 +13954,7 @@ function apply(ctx) {
scheduleCreate: (task) => call("schedule-create", task),
scheduleDelete: (sessionId, id) => call("schedule-delete", { sessionId, id }),
exchange: (a, b) => call("exchange", { a, b }).catch(() => []),
+ exchangeText: (id) => call("exchange-text", { id }).catch(() => void 0),
roomProgress: (roomId) => call("room-progress", { roomId }).catch(() => []),
earlier: (sessionId, cursor) => call("earlier", { sessionId, cursor: cursor ?? null }),
// Hours come back in this browser's time zone, so days start at local midnight.
diff --git a/ds-bot/src/client/exchange.js b/ds-bot/src/client/exchange.js
index 37486487..2e1c7464 100644
--- a/ds-bot/src/client/exchange.js
+++ b/ds-bot/src/client/exchange.js
@@ -7,9 +7,29 @@ import { useEscape, usePaneLeft } from './overlay-hooks.js'
import { t } from './i18n.js'
const SHEET_EXIT_MS = 220
+
+// Under a long message: the button that reads it whole, or why only part of it shows.
+function WholeNote({ entry, read, onShow }) {
+ if (read === undefined || read === 'loading') {
+ return h('button', { type: 'button', className: 'bt-more bt-whole', disabled: read === 'loading', onClick: onShow }, t('Show all · {count} characters', { count: entry.length.toLocaleString() }))
+ }
+ if (read.whole) return null
+ const note = typeof read.text === 'string' && read.text.length > entry.text.length
+ ? t('Showing the first {count} characters', { count: read.text.length.toLocaleString() })
+ : t('Only the start of this message is kept here')
+ return h('span', { className: 'bt-more bt-whole', 'data-note': '' }, note)
+}
+
export function ExchangeDialog({ pair, roster, actions }) {
const [entries, setEntries] = useState(null)
const [leaving, setLeaving] = useState(false)
+ // A long message comes with its start only; its whole text is read on request.
+ const [wholes, setWholes] = useState({})
+ const showAll = async (id) => {
+ setWholes(current => ({ ...current, [id]: 'loading' }))
+ const read = await actions.exchangeText(id)
+ setWholes(current => ({ ...current, [id]: read ?? { whole: false } }))
+ }
// The sheet slides back down before it unmounts.
const close = () => {
if (leaving) return
@@ -48,11 +68,15 @@ export function ExchangeDialog({ pair, roster, actions }) {
const bot = roster.byId[entry.from] ?? { id: entry.from, name: t('You'), color: 'gray' }
const showDay = entry.time - lastDay > 30 * 60 * 1000
lastDay = entry.time
+ const read = wholes[entry.id]
+ const cut = entry.length !== undefined
+ const text = typeof read?.text === 'string' ? read.text : cut ? `${entry.text}…` : entry.text
return h(Fragment, { key: entry.id },
showDay ? h('time', { className: 'bt-time' }, dayLabel(entry.time)) : null,
h('div', { className: 'bt-group-msg' },
h('span', { className: 'bt-group-name', style: { color: inkText(colorOf(bot)) } }, bot.name),
- h(Bubbles, { texts: [entry.text], roster, selfId: bot.id, actions, lead: h('span', { className: 'bt-lead' }, h(BotMark, { bot: bot, size: 22 })) })))
+ h(Bubbles, { texts: [text], roster, selfId: bot.id, actions, lead: h('span', { className: 'bt-lead' }, h(BotMark, { bot: bot, size: 22 })) }),
+ cut ? h(WholeNote, { entry, read, onShow: () => showAll(entry.id) }) : null))
})),
h('div', { className: 'bt-dialog-foot' }, h('button', { type: 'button', className: 'bt-soft', onClick: close }, t('Close Chat')))))
}
diff --git a/ds-bot/src/client/i18n.js b/ds-bot/src/client/i18n.js
index 83b39980..fa072f34 100644
--- a/ds-bot/src/client/i18n.js
+++ b/ds-bot/src/client/i18n.js
@@ -379,6 +379,9 @@ const ZH = {
'Bot exchange transcript': 'Bot 间对话记录',
'No messages yet': '还没有消息',
'Close Chat': '关闭',
+ 'Show all · {count} characters': '显示全部 · {count} 字',
+ 'Showing the first {count} characters': '只显示前 {count} 字',
+ 'Only the start of this message is kept here': '这里只保留了这条消息的开头',
// Time and usage
'Today {time}': '今天 {time}',
diff --git a/ds-bot/src/client/index.js b/ds-bot/src/client/index.js
index 203345b2..4d7e6865 100644
--- a/ds-bot/src/client/index.js
+++ b/ds-bot/src/client/index.js
@@ -550,6 +550,7 @@ export function apply(ctx) {
scheduleCreate: task => call('schedule-create', task),
scheduleDelete: (sessionId, id) => call('schedule-delete', { sessionId, id }),
exchange: (a, b) => call('exchange', { a, b }).catch(() => []),
+ exchangeText: id => call('exchange-text', { id }).catch(() => undefined),
roomProgress: roomId => call('room-progress', { roomId }).catch(() => []),
earlier: (sessionId, cursor) => call('earlier', { sessionId, cursor: cursor ?? null }),
// Hours come back in this browser's time zone, so days start at local midnight.
diff --git a/ds-bot/src/client/styles.js b/ds-bot/src/client/styles.js
index c800f00d..7aabc515 100644
--- a/ds-bot/src/client/styles.js
+++ b/ds-bot/src/client/styles.js
@@ -327,6 +327,9 @@ button.bt-q-opt:active{background:var(--bt-active)}
.bt-exchange .bt-dialog{position:relative}
.bt-exchange .bt-dialog-log{max-width:none;margin:0;padding:39px max(16px,calc((100% - 960px)/2)) 76px;scrollbar-width:thin}
.bt-exchange .bt-tools{display:none}
+.bt-exchange .bt-whole{align-self:flex-start;margin-left:30px}
+.bt-exchange .bt-whole[data-note]{cursor:default}
+.bt-exchange .bt-whole[data-note]:hover{color:var(--bt-ink-3)}
.bt-exchange .bt-dialog-foot{position:absolute;left:0;right:0;bottom:0;height:76px;box-sizing:border-box;align-items:flex-end;padding:0 0 28px;background:var(--bt-main);z-index:3;animation:bt-foot-in .3s cubic-bezier(.22,1,.36,1) .3s both}
.bt-exchange .bt-dialog-foot::before{content:"";position:absolute;left:0;right:0;bottom:100%;height:40px;background:linear-gradient(to top,var(--bt-main),transparent);pointer-events:none}
.bt-exchange[data-leaving] .bt-dialog-foot{animation:bt-foot-out .22s cubic-bezier(.4,0,1,1) forwards}
diff --git a/ds-bot/src/host/channel.js b/ds-bot/src/host/channel.js
index d9a2dbb1..6deb2e16 100644
--- a/ds-bot/src/host/channel.js
+++ b/ds-bot/src/host/channel.js
@@ -80,7 +80,7 @@ export function install(rt) {
})
// What a host that only reads the team may still answer.
// Updates change the dsh profile, not the team, so a reading host may run them too.
- const READS = new Set(['state', 'models', 'efforts', 'earlier', 'room-progress', 'exchange', 'usage', 'schedules', 'memory-view', 'browser-wait', 'browser-result', 'diagnostics', 'diagnostics-log', 'diagnostics-clear', 'update-check', 'update-install', 'dsh-update-install', 'repo-stars', 'approval-answer'])
+ const READS = new Set(['state', 'models', 'efforts', 'earlier', 'room-progress', 'exchange', 'exchange-text', 'usage', 'schedules', 'memory-view', 'browser-wait', 'browser-result', 'diagnostics', 'diagnostics-log', 'diagnostics-clear', 'update-check', 'update-install', 'dsh-update-install', 'repo-stars', 'approval-answer'])
function setPrefs(body) {
if (body.theme !== undefined) {
@@ -131,6 +131,12 @@ export function install(rt) {
const pair = new Set([body.a, body.b])
return ok(state.exchanges.filter(entry => pair.has(entry.from) && pair.has(entry.to)))
}
+ case 'exchange-text': {
+ await rt.load()
+ const entry = state.exchanges.find(item => item.id === body.id)
+ if (entry === undefined) return fail('This message is no longer kept', 'bot/stale')
+ return ok(await rt.exchangeText(entry))
+ }
case 'earlier':
await rt.load()
return ok(await rt.earlierPage(String(body.sessionId ?? ''), body.cursor ?? undefined))
diff --git a/ds-bot/src/host/constants.js b/ds-bot/src/host/constants.js
index a694671a..85a2fc91 100644
--- a/ds-bot/src/host/constants.js
+++ b/ds-bot/src/host/constants.js
@@ -28,6 +28,14 @@ export const MAX_UNATTENDED_MESSAGES = 40
// stuck, and more messages only pile up behind it.
export const MAX_UNREAD_REQUESTS = 10
export const MAX_EXCHANGES = 600
+// state.json is written whole on every save, so a long message between Bots or a long
+// group line keeps only its start there. The rest stays where it already is (a message
+// in the Session it was delivered to) or in a file of its own (a group line).
+export const TEXT_KEPT = 2000
+// Of one group line, a member reads this much: in its prompt, and with read_group_chat.
+export const GROUP_LINE_READ = 20_000
+// The Bot exchange dialog shows at most this much of one message.
+export const TEXT_SHOW_MAX = 200_000
export const ROOM_REPLY_TIMEOUT_MS = 240_000
// Lines a member writes less than this apart while it works reach its group together.
export const ROOM_STEP_GAP_MS = 30_000
diff --git a/ds-bot/src/host/delivery.js b/ds-bot/src/host/delivery.js
index 5b20edcb..0567d16a 100644
--- a/ds-bot/src/host/delivery.js
+++ b/ds-bot/src/host/delivery.js
@@ -1,7 +1,8 @@
// Delivery: how one Bot, the user, or DS Bot itself puts a message into a Bot Session.
import { randomUUID } from 'node:crypto'
import { createUserMessage } from '@deepseek-ai/dsh-llm'
-import { SOURCE_KIND } from './constants.js'
+import { HISTORY_TIMEOUT_MS, SOURCE_KIND, TEXT_KEPT, TEXT_SHOW_MAX } from './constants.js'
+import { headOf, textOf } from './text.js'
export function install(rt) {
const { ctx, state } = rt
@@ -63,11 +64,14 @@ export function install(rt) {
...(askedBy ? { askedBy } : {}),
...(fromRoomId ? { senderRoomId: fromRoomId } : {}),
}
+ const targetSessionId = sessionId ?? rt.chatOf(toId)
if (EXCHANGE_ROLES.has(role) && !deferred && !resumed && !retried && !resent) {
- state.exchanges.push({ id: exchangeId, from: fromId, to: toId, text, role, time: Date.now(), ...(replyTo ? { replyTo } : {}), ...(audience === 'user' ? { audience } : {}) })
+ // The whole text is in the Session it goes to; exchangeText reads it back there.
+ const kept = headOf(text, TEXT_KEPT)
+ const whole = kept.length < text.length ? { length: text.length, sessionId: targetSessionId } : {}
+ state.exchanges.push({ id: exchangeId, from: fromId, to: toId, text: kept, ...whole, role, time: Date.now(), ...(replyTo ? { replyTo } : {}), ...(audience === 'user' ? { audience } : {}) })
void rt.save().catch(() => {})
}
- const targetSessionId = sessionId ?? rt.chatOf(toId)
await rt.ensureCapabilities(targetSessionId)
const agent = await resolveAgent(targetSessionId)
// A steered message cuts into a running turn at its next step; a follow-up
@@ -82,5 +86,23 @@ export function install(rt) {
return exchangeId
}
- Object.assign(rt, { resolveAgent, deliver })
+ // A message between Bots, whole, from the Session it was delivered to: the message
+ // there is its header line, then the text. `whole` is false when only the start could
+ // be read (the Session is gone, or the message is past what the dialog shows).
+ async function exchangeText(entry) {
+ if (entry.length === undefined) return { text: entry.text, whole: true }
+ try {
+ for await (const event of rt.eventsBackwards(entry.sessionId ?? rt.chatOf(entry.to), AbortSignal.timeout(HISTORY_TIMEOUT_MS))) {
+ if (event.type !== 'user/message' || event.data?.source?.exchangeId !== entry.id) continue
+ const delivered = textOf(event.data.content)
+ const text = delivered.slice(delivered.indexOf('\n') + 1)
+ return { text: headOf(text, TEXT_SHOW_MAX), whole: text.length <= TEXT_SHOW_MAX }
+ }
+ } catch (error) {
+ rt.warn('ds-bot: message %s not read back: %s', entry.id, error)
+ }
+ return { text: entry.text, whole: false }
+ }
+
+ Object.assign(rt, { resolveAgent, deliver, exchangeText })
}
diff --git a/ds-bot/src/host/groups.js b/ds-bot/src/host/groups.js
index 9f636fd5..04dd92f5 100644
--- a/ds-bot/src/host/groups.js
+++ b/ds-bot/src/host/groups.js
@@ -15,10 +15,13 @@
// turn keeps running in its group Session, and what it ends with is posted to the group,
// where the member that asked for it answers.
import { randomUUID } from 'node:crypto'
+import { mkdirSync, writeFileSync } from 'node:fs'
+import { readFile, rm } from 'node:fs/promises'
+import { join } from 'node:path'
import { createUserMessage } from '@deepseek-ai/dsh-llm'
import { defineTool } from '@deepseek-ai/dsh-tools'
-import { ROOM_LATE_ROUNDS, ROOM_REPLY_TIMEOUT_MS, ROOM_STEP_GAP_MS, ROOM_WORK_GRACE_MS, SOURCE_KIND, TEAM_TOOLS } from './constants.js'
-import { escapeRegExp, splitRef, textOf } from './text.js'
+import { GROUP_LINE_READ, ROOM_LATE_ROUNDS, ROOM_REPLY_TIMEOUT_MS, ROOM_STEP_GAP_MS, ROOM_WORK_GRACE_MS, SOURCE_KIND, TEAM_TOOLS, TEXT_KEPT } from './constants.js'
+import { escapeRegExp, headOf, splitRef, textOf } from './text.js'
import { mentionSpans } from '../shared/mentions.js'
import { GROUP_REF, output } from './tools.js'
import { failureLine } from './turns.js'
@@ -41,11 +44,6 @@ const ensureRoomLog = (room) => {
room.seen ??= {}
}
-const appendRoom = (room, line) => {
- room.seq += 1
- room.log = [...room.log, { ...line, seq: room.seq }].slice(-ROOM_LOG_MAX)
-}
-
// A member's own lines are already in its Session, so only the others' are news.
const unseenLines = (room, memberId) => {
const since = room.seen[memberId]
@@ -53,14 +51,15 @@ const unseenLines = (room, memberId) => {
return room.log.filter(line => line.seq > since && line.botId !== memberId).slice(-ROOM_DELTA_MAX)
}
-const lineText = (line) => {
+// `text` is what to show of a message line, by default what the log keeps of it.
+const lineText = (line, text = line.text) => {
if (line.kind === 'lookup') return `[${line.who} looked up its own chat${line.text ? ` for "${line.text}"` : ''}]`
if (line.kind === 'resumed') return `[${line.who}'s work here was cut off when DeepSeek Harness restarted; it is picking it back up and posts when done]`
if (line.kind === 'cut') return `[${line.who}'s work here was cut off by a restart again, so it stopped; ask it to go on if it still matters]`
if (line.kind === 'stopped') return `[The user stopped ${line.who}'s work here]`
- return `${line.who}: ${line.text}`
+ return `${line.who}: ${text}`
}
-const transcript = lines => lines.map(lineText).join('\n')
+const transcript = (lines, textOfLine = line => line.text) => lines.map(line => lineText(line, textOfLine(line))).join('\n')
// Models often echo the transcript format ("Name: …") despite the instruction.
export const stripOwnName = (name, text) => text.replace(new RegExp(`^\\s*\\**${escapeRegExp(name)}\\**\\s*[::]\\s*`), '').trim()
@@ -114,6 +113,65 @@ const ROOM_READ_MAX = 30
export function install(rt) {
const { ctx } = rt
+ // A long line keeps its start in state.json, which is written whole on every save,
+ // and what a member reads of it (GROUP_LINE_READ) in a file of its own: written once,
+ // before the state that names it, and removed with the line. All of it stays in the
+ // group's Session, where the group chat shows it.
+ const linesDir = join(rt.home, 'group-lines')
+ const lineFile = (roomId, seq) => join(linesDir, roomId, `${seq}.txt`)
+ const wholeLines = new Map()
+ const lineKey = (roomId, seq) => `${roomId}/${seq}`
+ const writes = () => rt.readOnly?.() === undefined
+
+ function keepGroupLine(roomId, line, text) {
+ const kept = headOf(text, TEXT_KEPT)
+ if (kept.length === text.length) return line
+ const read = headOf(text, GROUP_LINE_READ)
+ wholeLines.set(lineKey(roomId, line.seq), read)
+ if (writes()) {
+ try {
+ mkdirSync(join(linesDir, roomId), { recursive: true })
+ writeFileSync(lineFile(roomId, line.seq), read, 'utf8')
+ } catch (error) {
+ rt.warn('ds-bot: group line %s/%s not kept whole: %s', roomId, line.seq, error)
+ }
+ }
+ return { ...line, text: kept, length: text.length }
+ }
+
+ function dropGroupLine(roomId, line) {
+ if (line.length === undefined) return
+ wholeLines.delete(lineKey(roomId, line.seq))
+ if (writes()) void rm(lineFile(roomId, line.seq), { force: true }).catch(() => {})
+ }
+
+ function dropGroupLines(roomId) {
+ for (const key of wholeLines.keys()) if (key.startsWith(`${roomId}/`)) wholeLines.delete(key)
+ if (writes()) void rm(join(linesDir, roomId), { recursive: true, force: true }).catch(() => {})
+ }
+
+ // After a restart the whole lines are read back before a member reads them.
+ const loadGroupLines = room => Promise.all(room.log
+ .filter(line => line.length !== undefined && !wholeLines.has(lineKey(room.id, line.seq)))
+ .map(line => readFile(lineFile(room.id, line.seq), 'utf8').then(text => { wholeLines.set(lineKey(room.id, line.seq), text) }, () => {})))
+
+ // What a member reads of a line: all of it, up to GROUP_LINE_READ.
+ const memberText = room => (line) => {
+ const read = line.length === undefined ? line.text : wholeLines.get(lineKey(room.id, line.seq))
+ if (read === undefined) return `${line.text}… (cut; ${line.length} characters in all, the rest was not kept)`
+ const length = line.length ?? read.length
+ const shown = headOf(read, GROUP_LINE_READ)
+ return length > shown.length ? `${shown}… (cut; ${length} characters in all)` : shown
+ }
+
+ const appendRoom = (room, line) => {
+ room.seq += 1
+ const log = [...room.log, keepGroupLine(room.id, { ...line, seq: room.seq }, line.text ?? '')]
+ for (const gone of log.slice(0, -ROOM_LOG_MAX)) dropGroupLine(room.id, gone)
+ room.log = log.slice(-ROOM_LOG_MAX)
+ }
+ Object.assign(rt, { keepGroupLine, dropGroupLines })
+
// A member's group Session, created on its first turn there. A new one starts with
// a fresh look at the log and a brief of the member's own chat; the brief is the
// first message, so it stays fixed and the prefix stays cacheable.
@@ -161,9 +219,9 @@ export function install(rt) {
members,
notice,
leads ? adminNote(room) : '',
- earlier.length ? `Earlier in the group, before this round (context only):\n${transcript(earlier)}` : '',
+ earlier.length ? `Earlier in the group, before this round (context only):\n${transcript(earlier, memberText(room))}` : '',
current.length
- ? `This round so far${spokeThisRound ? ', since your last message' : ', starting with the new message'} (oldest first):\n${transcript(current)}`
+ ? `This round so far${spokeThisRound ? ', since your last message' : ', starting with the new message'} (oldest first):\n${transcript(current, memberText(room))}`
: 'Nothing new in this round since your last message.',
round.shown?.includes(member.id) ? '' : referenceNote(round.references),
working.length > 0 ? `Still at work on what they were asked; each posts here when done: ${working.map(item => `${item.bot.name} (${minutesSince(item.since)} min)`).join(', ')}.` : '',
@@ -354,6 +412,7 @@ export function install(rt) {
round.replies.push({ kind: 'failed', botId: member.id, name: member.name, color: member.color, code: 'SESSION', text: String(error?.message ?? error).slice(0, 300), time: Date.now() })
continue
}
+ await loadGroupLines(room)
const prompt = roomPrompt(room, member, round)
round.shown?.push(member.id)
const text = session.brief === undefined ? prompt : `${session.brief}\n\n${prompt}`
@@ -546,7 +605,10 @@ export function install(rt) {
ctx.tools.register(defineTool({
name: 'read_group_chat',
description: 'Read the latest messages of a group chat, including what you said there. You speak in groups from separate conversations, so your own chat does not show them. Main Bots may read any group; others the groups they are in.',
- parameters: { group: GROUP_REF },
+ parameters: {
+ group: GROUP_REF,
+ seq: { type: 'integer', description: 'Read one message whole: the seq that a cut message names' },
+ },
output,
async execute(args, exec) {
await rt.load()
@@ -555,8 +617,18 @@ export function install(rt) {
const { room, error } = rt.pickRoom(args.group, self)
if (error) return error
ensureRoomLog(room)
+ await loadGroupLines(room)
+ if (args.seq !== undefined) {
+ const line = room.log.find(entry => entry.seq === Number(args.seq))
+ return line === undefined ? `"${room.name}" no longer keeps message ${args.seq}; it keeps its latest ${ROOM_LOG_MAX}.` : lineText(line, memberText(room)(line))
+ }
+ // Thirty whole lines could fill the context, so a long one shows its start.
+ const short = (line) => {
+ const length = line.length ?? line.text.length
+ return length <= TEXT_KEPT ? line.text : `${headOf(line.text, TEXT_KEPT)}… (cut; ${length} characters in all; read_group_chat with seq ${line.seq} reads it whole)`
+ }
const lines = room.log.slice(-ROOM_READ_MAX)
- return lines.length === 0 ? `"${room.name}" has no messages yet.` : `Latest in "${room.name}" (oldest first):\n${transcript(lines)}`
+ return lines.length === 0 ? `"${room.name}" has no messages yet.` : `Latest in "${room.name}" (oldest first):\n${transcript(lines, short)}`
},
}))
diff --git a/ds-bot/src/host/roster.js b/ds-bot/src/host/roster.js
index 981bc34a..4fcad2c1 100644
--- a/ds-bot/src/host/roster.js
+++ b/ds-bot/src/host/roster.js
@@ -403,6 +403,7 @@ export function install(rt) {
for (const botId of Object.keys(room.sessions ?? {})) rt.dropGroupSession(room, botId)
delete state.rooms[id]
rt.roomInbox.delete(id)
+ rt.dropGroupLines(id)
const failed = await saveForDelete()
// Armed scheduled tasks refuse a plain archive and would keep delivering into
// the deleted group; the session-stop that stopActivity runs removes them.
diff --git a/ds-bot/src/host/store.js b/ds-bot/src/host/store.js
index 580ce7ab..a0bdc8ab 100644
--- a/ds-bot/src/host/store.js
+++ b/ds-bot/src/host/store.js
@@ -4,9 +4,9 @@
import { mkdirSync, statSync } from 'node:fs'
import { copyFile, mkdir, readFile, rename, unlink, writeFile } from 'node:fs/promises'
import { join } from 'node:path'
-import { LEGACY_THEMES, MAX_EXCHANGES, ROOM_MODES } from './constants.js'
+import { LEGACY_THEMES, MAX_EXCHANGES, ROOM_MODES, TEXT_KEPT } from './constants.js'
import { homeLock } from './home-lock.js'
-import { currentColor, normalize, unquote } from './text.js'
+import { currentColor, headOf, normalize, unquote } from './text.js'
export function install(rt) {
const { ctx, home, statePath } = rt
@@ -160,6 +160,24 @@ export function install(rt) {
room.notice ??= ''
migrated = true
}
+ // Teams from before #206 kept every long message whole in this file, written whole on
+ // every save. A message between Bots is whole in its recipient's Session; a group
+ // line's whole text moves to a file of its own, which only the writing host makes.
+ const writer = holder === undefined && stuck === undefined
+ for (const entry of state.exchanges) {
+ if (typeof entry.text !== 'string' || entry.text.length <= TEXT_KEPT || entry.length !== undefined) continue
+ entry.length = entry.text.length
+ entry.text = headOf(entry.text, TEXT_KEPT)
+ migrated = true
+ }
+ for (const room of writer ? Object.values(state.rooms) : []) {
+ if (!Array.isArray(room.log)) continue
+ const log = room.log.map(line => (typeof line.text === 'string' && line.length === undefined && typeof line.seq === 'number' ? rt.keepGroupLine(room.id, line, line.text) : line))
+ if (log.some((line, index) => line !== room.log[index])) {
+ room.log = log
+ migrated = true
+ }
+ }
// Unnamed groups from before names followed their members may still list old names.
const stale = Object.values(state.rooms).filter(room => !room.named && room.name !== rt.autoRoomName(room.members))
for (const room of stale) room.name = rt.autoRoomName(room.members)
@@ -242,7 +260,7 @@ export function install(rt) {
if (!writable()) return writing
state.revision += 1
state.exchanges = state.exchanges.slice(-MAX_EXCHANGES)
- const snapshot = JSON.stringify(state, null, 2)
+ const snapshot = JSON.stringify(state)
const run = writing.then(async () => {
const temp = `${statePath}.${process.pid}.tmp`
try {
diff --git a/ds-bot/src/host/text.js b/ds-bot/src/host/text.js
index db3dcb8c..547e4948 100644
--- a/ds-bot/src/host/text.js
+++ b/ds-bot/src/host/text.js
@@ -37,6 +37,8 @@ export const estimateTokens = (text) => {
return Math.ceil(tokens)
}
export const clip = (text, max, rest) => (text.length > max ? `${text.slice(0, max)}… (cut; ${rest})` : text)
+// The first `max` UTF-16 units of a text, never half of a character.
+export const headOf = (text, max) => (text.length <= max ? text : text.slice(0, /[\uD800-\uDBFF]/.test(text[max - 1]) ? max - 1 : max))
export const createClock = (timeZone) => {
const clockFormat = new Intl.DateTimeFormat('en-CA', {
diff --git a/ds-bot/src/host/tools.js b/ds-bot/src/host/tools.js
index 8a6bddf3..28cc925b 100644
--- a/ds-bot/src/host/tools.js
+++ b/ds-bot/src/host/tools.js
@@ -125,6 +125,7 @@ export function install(rt) {
const target = rt.findBot(args.to)
if (target === undefined) return `No Bot named ${args.to}. Team: ${Object.values(state.bots).map(bot => bot.name).join(', ')}`
if (target.id === sender.id) return 'You cannot message yourself.'
+ if (String(args.message ?? '').trim() === '') return 'Write a message to send.'
const hop = nextHop(exec)
if (hop > MAX_HOPS) return CHAIN_TOO_LONG
checkUnattended()
diff --git a/ds-bot/test/bot-messages.test.js b/ds-bot/test/bot-messages.test.js
index be8b4f5d..3646fcde 100644
--- a/ds-bot/test/bot-messages.test.js
+++ b/ds-bot/test/bot-messages.test.js
@@ -1,5 +1,5 @@
import assert from 'node:assert/strict'
-import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
+import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { setTimeout as delay } from 'node:timers/promises'
@@ -621,3 +621,52 @@ test('a request the model keeps turning down moves the Bot on, without its lates
await delay(30)
assert.equal(made, 2)
})
+
+// #206: a message between Bots used to go whole into state.json, which is written whole
+// on every save; an empty one started a turn on nothing.
+test('message_bot turns down an empty message', async (t) => {
+ const { agentOf, tool, exchanges, stop } = await start(team())
+ t.after(stop)
+ for (const message of ['', ' \n\t ']) assert.equal(await tool('message_bot', { to: 'Writer', message }, 'a'), 'Write a message to send.')
+ assert.deepEqual(agentOf('b').calls, [])
+ assert.deepEqual(await exchanges(), [])
+})
+
+test('a long message between Bots keeps its start in state.json and is read back whole from the Session', async (t) => {
+ const { ctx, agentOf, tool, call, exchanges, home, stop } = await start(team())
+ t.after(stop)
+ const long = `${'长'.repeat(30_000)}结尾`
+ await tool('message_bot', { to: 'Writer', message: long }, 'a')
+ const [, delivered] = agentOf('b').calls.find(([kind]) => kind === 'steer' || kind === 'followup')
+ assert.ok(delivered.content[0].text.endsWith(long), 'the recipient gets it whole')
+ const [entry] = await exchanges()
+ assert.equal(entry.text.length, 2000)
+ assert.equal(entry.length, long.length)
+ assert.equal(entry.sessionId, 'b')
+ const file = join(home, 'state.json')
+ for (let at = 0; at < 100 && !readFileSync(file, 'utf8').includes(entry.id); at += 1) await delay(20)
+ const saved = readFileSync(file, 'utf8')
+ assert.ok(saved.length < 10_000, `state.json is ${saved.length} characters`)
+ assert.ok(!saved.includes('\n'), 'state.json is written without indentation')
+
+ // The dialog reads the whole text back from the Session it was delivered to.
+ agentOf('b').session.seq = 2
+ ctx.sessionController.page = async ({ address }) => ({
+ records: agentOf(address.sessionId).calls.map(([, message], index) => ({ event: { type: 'user/message', seq: index + 1, data: { content: message.content, source: message.source } } })),
+ hasMore: false,
+ })
+ assert.deepEqual((await call('exchange-text', { id: entry.id })).value, { text: long, whole: true })
+ // A Session that cannot be read leaves the start, said to be only that.
+ ctx.sessionController.page = async () => { throw new Error('gone') }
+ assert.deepEqual((await call('exchange-text', { id: entry.id })).value, { text: entry.text, whole: false })
+})
+
+test('a team saved with long messages whole keeps only their start after it loads', async (t) => {
+ const seed = team()
+ seed.exchanges = [{ id: 'x1', from: 'a', to: 'b', text: 'y'.repeat(5000), role: 'request', time: 1 }]
+ const { exchanges, stop } = await start(seed)
+ t.after(stop)
+ const [entry] = await exchanges()
+ assert.equal(entry.text, 'y'.repeat(2000))
+ assert.equal(entry.length, 5000)
+})
diff --git a/ds-bot/test/rounds.test.js b/ds-bot/test/rounds.test.js
index 495826dc..8e536d96 100644
--- a/ds-bot/test/rounds.test.js
+++ b/ds-bot/test/rounds.test.js
@@ -1,6 +1,6 @@
import assert from 'node:assert/strict'
import { randomUUID } from 'node:crypto'
-import { mkdtempSync, readFileSync, rmSync } from 'node:fs'
+import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { test } from 'node:test'
@@ -11,8 +11,8 @@ import { fakeContext } from './fake-ctx.js'
// session/event listener, so a round runs with no model. Prompts land in the
// Session's inbox the way DSH holds them while a turn runs: queued for the next turn,
// or steered into the next step.
-function start() {
- const home = mkdtempSync(join(tmpdir(), 'ds-bot-rounds-'))
+// `home`: start on the data of an earlier run, as after a restart.
+function start({ home = mkdtempSync(join(tmpdir(), 'ds-bot-rounds-')) } = {}) {
const ctx = fakeContext()
const workspaces = new Map()
const sent = []
@@ -97,9 +97,9 @@ function start() {
await emit(sessionId, { type: 'assistant/message', data: { message: { content: [{ type: 'text', text }] } } })
await emit(sessionId, { type: 'turn/end', data: { reason: { kind: 'stop' } } })
}
- const stop = async () => {
+ const stop = async ({ keep = false } = {}) => {
await ctx.dispose()
- rmSync(home, { recursive: true, force: true, maxRetries: 5 })
+ if (!keep) rmSync(home, { recursive: true, force: true, maxRetries: 5 })
}
return { call, emit, userMessage, compose, relay, inboxOf, answer, stop, sent, ctx, home, rt }
}
@@ -347,3 +347,79 @@ test('a message sent between rounds still opens a full round', async (t) => {
const result = JSON.parse(await running)
assert.deepEqual(result.replies.map(reply => reply.name ?? 'User'), ['Chief', 'Writer'])
})
+
+// #206: state.json is written whole on every save, so a long group line keeps its start
+// there and its whole text in a file of its own. Members still read it whole.
+test('a long group line keeps its start in state.json, and members and read_group_chat read it whole', async (t) => {
+ const { call, emit, userMessage, relay, answer, stop, sent, ctx, home, rt } = start()
+ t.after(() => rmSync(home, { recursive: true, force: true, maxRetries: 5 }))
+ const room = await team(call, sent)
+ const long = `${'长'.repeat(9000)}结尾`
+ await emit(room.id, userMessage('Write it all out'))
+ const running = relay(room.id, 'relay')
+ const first = await turnFor(sent)
+ await answer(first.sessionId, first.message.source.exchangeId, long, 1)
+ const second = await turnFor(sent, first)
+ assert.ok(second.message.content[0].text.includes(long), 'the next member reads the line whole')
+ await answer(second.sessionId, second.message.source.exchangeId, 'Thanks.', 2)
+ assert.equal(JSON.parse(await running).replies[0].text, long)
+
+ await rt.save()
+ const saved = JSON.parse(readFileSync(join(home, 'state.json'), 'utf8')).rooms[room.id].log.find(line => line.length !== undefined)
+ assert.equal(saved.text.length, 2000)
+ assert.equal(saved.length, long.length)
+ assert.equal(readFileSync(join(home, 'group-lines', room.id, `${saved.seq}.txt`), 'utf8'), long)
+ const read = ctx.registeredTools.get('read_group_chat')
+ const exec = { agent: { id: first.sessionId } }
+ assert.match(await read.execute({ group: 'R1' }, exec), new RegExp(`cut; ${long.length} characters in all; read_group_chat with seq ${saved.seq} reads it whole`))
+ assert.ok((await read.execute({ group: 'R1', seq: saved.seq }, exec)).endsWith(long))
+ await stop({ keep: true })
+
+ // The log is full (60 lines) when DSH starts again, the long line second oldest.
+ const file = join(home, 'state.json')
+ const onDisk = JSON.parse(readFileSync(file, 'utf8'))
+ const kept = onDisk.rooms[room.id]
+ const fillers = Array.from({ length: 60 - kept.log.length }, (_, at) => ({ who: 'User', text: `line ${at}`, seq: kept.seq + at + 1 }))
+ kept.log = [...kept.log, ...fillers]
+ kept.seq += fillers.length
+ writeFileSync(file, JSON.stringify(onDisk))
+
+ // After a restart the whole line is read back from its file.
+ const again = start({ home })
+ t.after(() => again.stop({ keep: true }))
+ await again.call('state', {})
+ const whole = await again.ctx.registeredTools.get('read_group_chat').execute({ group: 'R1', seq: saved.seq }, exec)
+ assert.ok(whole.endsWith(long))
+
+ // The file goes with the line once the log moves past it, and with the group.
+ await again.emit(room.id, again.userMessage('Next'))
+ const next = again.relay(room.id, 'relay-2')
+ const one = await turnFor(again.sent)
+ await again.answer(one.sessionId, one.message.source.exchangeId, 'One.', 3)
+ const two = await turnFor(again.sent, one)
+ await again.answer(two.sessionId, two.message.source.exchangeId, 'Two.', 4)
+ await next
+ await waitFor(() => (existsSync(join(home, 'group-lines', room.id, `${saved.seq}.txt`)) ? undefined : true))
+ assert.equal(existsSync(join(home, 'group-lines', room.id, `${saved.seq}.txt`)), false)
+ await again.call('delete-room', { id: room.id })
+ await waitFor(() => (existsSync(join(home, 'group-lines', room.id)) ? undefined : true))
+ assert.equal(existsSync(join(home, 'group-lines', room.id)), false)
+})
+
+test('a group line longer than a member reads is cut in its prompt, with its length, and its file keeps only that', async (t) => {
+ const { call, emit, userMessage, relay, answer, stop, sent, home, rt } = start()
+ t.after(stop)
+ const room = await team(call, sent)
+ await emit(room.id, userMessage('Go'))
+ const running = relay(room.id, 'relay')
+ const first = await turnFor(sent)
+ await answer(first.sessionId, first.message.source.exchangeId, 'x'.repeat(25_000), 1)
+ const second = await turnFor(sent, first)
+ const prompt = second.message.content[0].text
+ assert.ok(prompt.includes(`${'x'.repeat(20_000)}… (cut; 25000 characters in all)`))
+ assert.ok(!prompt.includes('x'.repeat(20_001)))
+ await answer(second.sessionId, second.message.source.exchangeId, 'Ok.', 2)
+ await running
+ const line = rt.state.rooms[room.id].log.find(entry => entry.length === 25_000)
+ assert.equal(readFileSync(join(home, 'group-lines', room.id, `${line.seq}.txt`), 'utf8').length, 20_000)
+})