Skip to content

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

Keep aside cards in per-part files and lift their limits

Fei24112db6da8eeb40
diff --git a/docs/plans/2026-10-21-btw.md b/docs/plans/2026-10-21-btw.md
index ebb41122..29588ad6 100644
--- a/docs/plans/2026-10-21-btw.md
+++ b/docs/plans/2026-10-21-btw.md
@@ -38,8 +38,8 @@ Bot 之间也有反方向「拉」事实的需求(我需要对方掌握的事
- 回答用快照里的路由与思考强度,与产生这份上下文的请求一致。Bot 从未跑过模型、快照里没有路由时,拒绝并提示先正常对话一次。换了模型之后,btw 仍用快照里的路由;该路由的提供方已不可用时调用失败,卡片显示原因。
- 这次问答不写进该 Bot 的模型可见历史:主对话上下文不变,正在跑的回合不受影响、不改变路线。
- btw 卡的呈现分两处:回合还在跑时,卡片出现在输入框上方的专属泊位(`conversation.composer.dock`),带取消按钮;回合结束后,卡片移到快照边界所在回合的回合尾(`conversation.chat.turnTail`,DS Bot 的回合分隔已在用它)——同一时刻只有一处可见。btw 不是会话的转录条目,不能插在转录中间;旧分段的卡片并入 DS Bot 自己渲染旧分段的 EarlierParts 路径,仍挂在原分段的原回合处。
-- 卡片可折叠(问、答、时间),用户可见、模型看不到。问题与答案各限 2000 字,超出截断并标注「已截断」——这只约束卡片保留的内容,发给模型的问题原样发送;每个 Bot 保留最近 20 条(持久保存,重启后仍在),更早的丢弃。
-- 同一个 Bot 最多 2 个 btw 在飞(每次调用都是一次全上下文输入),超出的认领后拒绝并提示稍后再问。卡片上可以取消:中断这次调用。DSH 重启时,未完成的 btw 一律标为失败。
+- 卡片可折叠(问、答、时间),用户可见、模型看不到。问题与答案整段保留;卡片按分段存在自己的文件里,不写进团队状态文件;团队视图只带当前分段的卡片,更早分段的卡片随那一段的历史一起加载。不设条数、字数和并发上限。
+- 卡片上可以取消:中断这次调用。DSH 重启后,读到的未完成 btw 标为失败。
- 调用计入用量页,kind 为 `btw`;模型调用失败时卡片显示失败原因,没有其他副作用。达到输出上限时,本次问答标为失败,保留已生成的部分答案,并注明答案因输出上限而未完成。
## 不做什么
@@ -56,9 +56,10 @@ Bot 之间也有反方向「拉」事实的需求(我需要对方掌握的事
- 提示缓存是否命中取决于提供方;同构只能保留前缀、提高复用机会。未命中时,一次 btw 是一次全上下文调用的输入成本;答案的输出和可能的一次重试也照常计费。
- 带着工具声明时模型仍可能返回工具调用:不执行,重试一次,再犯即失败。
- 回合在跑时回合尾槽位尚不存在(DSH 在回合结束后才发布它),所以在跑的卡片先在输入框上方,回合结束后归位回合尾。
-- 在飞并发有上限(每个 Bot 2 个);总量不设上限,靠用量页和 btw 卡可见。
+- 总量不设上限,靠用量页和 btw 卡可见。
## 变更
- 2026-10-21:初稿;第一轮评审修订(快照的工具平衡边界、入口走输入触发机制、快照路由语义、卡片截断与取消规则);第二轮评审修订(请求与会话同构以复用缓存前缀、去掉思考强度降级、群聊/附件/空问题三种认领后拒绝、在飞并发上限每个 Bot 2 个、Bot 对 Bot 的 `ask_bot` 移出本设计另行提案);第三轮评审修订(运行中的回合没有回合尾槽位,在跑卡片改到输入框上方泊位、回合结束后归位;旧分段卡片并入 DS Bot 自己的旧分段渲染;缓存表述降级为「复用前缀、是否命中依提供方」,删去「近乎零成本」;「已落盘」改为「已提交到 Session」;认领后立即受理、后台答题)。待 Issue 与 PR。
- 2026-10-23:评审反馈(#413):去掉 btw 自定的输出上限(2000 token),改用路由自身的上限——思考与答案共享输出预算,自定的低上限会让思考模型的 btw 经常以截断失败收场。卡片每 Bot 20 条的上限维持不变。
+- 2026-10-23:去掉条数、字数和并发上限,卡片按分段存文件。
diff --git a/ds-bot/client.js b/ds-bot/client.js
index 6c3ac644..127fc67e 100644
--- a/ds-bot/client.js
+++ b/ds-bot/client.js
@@ -788,7 +788,6 @@ var ZH = {
"Answering…": "正在回答…",
Cancelled: "已取消",
"Answered from progress so far; nothing was run": "基于目前已有进展的回答,没有执行任何操作",
- Truncated: "已截断",
"Could not answer.": "没能答上。",
"Cancel this side question": "取消这次旁路提问",
Conversations: "对话",
@@ -1066,7 +1065,6 @@ var HOST_ZH = {
"btw is only available in a Bot's private chat.": "旁路提问只在 Bot 的私聊里可用。",
"Write a question after /btw.": "在 /btw 后面写上问题。",
"/btw does not take attachments.": "/btw 不能带附件。",
- "Two aside questions are already running for this Bot. Ask again when one finishes.": "这个 Bot 已经有两个旁路提问在进行,等其中一个结束再问。",
"This Bot has not called a model yet. Send a normal message first.": "这个 Bot 还没有调用过模型,请先正常发一条消息。",
"DS Bot restarted before this answer finished.": "DS Bot 在回答完成前重启了。",
"The model called a tool again after being told not to, so this aside was not answered.": "模型在被要求不要调用工具之后又调用了工具,所以这次没有回答。",
@@ -3307,7 +3305,7 @@ div:has(> nav [class*="_marks"]){display:none!important}
.bt-btw[data-open] .bt-btw-chevron{transform:rotate(45deg)}
.bt-btw-q{min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font-size:13px;line-height:18px}
.bt-btw[data-open] .bt-btw-q{white-space:normal}
-.bt-btw-time,.bt-btw-status,.bt-btw-mark{flex:none;color:var(--bt-ink-3);font-size:11px;line-height:14px;font-variant-numeric:tabular-nums}
+.bt-btw-time,.bt-btw-status{flex:none;color:var(--bt-ink-3);font-size:11px;line-height:14px;font-variant-numeric:tabular-nums}
.bt-btw-cancel{margin-left:auto}
.bt-btw-cancel:disabled{opacity:.55;cursor:default}
.bt-btw-note{margin:4px 0 0;color:var(--bt-ink-3);font-size:12px;line-height:16px}
@@ -3315,7 +3313,6 @@ div:has(> nav [class*="_marks"]){display:none!important}
.bt-btw-answer{font-size:14px;line-height:20px}
.bt-btw-answer p{margin:0}
.bt-btw-fail{margin:0 0 6px;color:var(--bt-danger);font-size:13px;line-height:18px}
-.bt-btw-mark{margin:6px 0 0}
@media (prefers-reduced-motion:reduce){.bt-btw-chevron{transition:none}}
`;
var PLUGIN_ID = "ds-bot";
@@ -5093,7 +5090,6 @@ function useFold(text, { limit = FOLD_AT, whole, live = false, markdown = true }
// src/client/btw-model.js
var BTW_SOURCE = "ds-bot-btw";
var BTW_TOKEN = "/btw ";
-var HOST_CUT = " [truncated]";
var GROUP_REFUSAL = "btw is only available in a Bot's private chat.";
var ATTACHMENT_REFUSAL = "/btw does not take attachments.";
var EMPTY_REFUSAL = "Write a question after /btw.";
@@ -5276,11 +5272,6 @@ function partIndexOf(bot, sessionId, cursor) {
if (Number.isInteger(cursor?.part)) return cursor.part;
return partChain(bot).indexOf(sessionId) - 1;
}
-function earlierPartCards(cards, bot, sessionId, partIndex) {
- const partId = partChain(bot)[partIndex];
- if (typeof partId !== "string" || partId === "" || partId === sessionId) return [];
- return (Array.isArray(cards) ? cards : []).filter((card) => card?.partId === partId);
-}
function rowTurn(item) {
return typeof item?.turn === "number" && Number.isInteger(item.turn) ? item.turn : void 0;
}
@@ -5395,19 +5386,6 @@ function withBtwRows(items, cards) {
for (const card of aside) out.push(btwRow(card));
return out;
}
-function presentText(text, truncated) {
- const raw = typeof text === "string" ? text : "";
- let body = raw;
- let cut = false;
- if (raw.endsWith(HOST_CUT)) {
- body = raw.slice(0, -HOST_CUT.length);
- cut = true;
- } else if (raw.endsWith("已截断")) {
- body = raw.slice(0, -"已截断".length).trimEnd();
- cut = true;
- }
- return { text: body, marked: truncated === true || cut };
-}
// src/client/btw.js
var import_react9 = require("react");
@@ -5428,9 +5406,8 @@ function BtwCardView({ card, compact: compact2 = false, actions }) {
if (card?.status && card.status !== "answering") setOpen(true);
}, [card?.status]);
if (!card) return null;
- const question = presentText(card.question, card.truncated === true || card.questionTruncated === true);
- const answer = presentText(card.answer, card.truncated === true || card.answerTruncated === true);
- const truncated = question.marked || answer.marked;
+ const question = typeof card.question === "string" ? card.question : "";
+ const answer = typeof card.answer === "string" ? card.answer : "";
const when3 = stampOf(card);
const answering = card.status === "answering";
const cancel = (event) => {
@@ -5462,7 +5439,7 @@ function BtwCardView({ card, compact: compact2 = false, actions }) {
onClick: () => setOpen((value) => !value)
},
(0, import_react9.createElement)("span", { className: "bt-btw-chevron", "aria-hidden": true }),
- (0, import_react9.createElement)("span", { className: "bt-btw-q" }, question.text)
+ (0, import_react9.createElement)("span", { className: "bt-btw-q" }, question)
),
when3 !== void 0 ? (0, import_react9.createElement)("time", { className: "bt-btw-time", dateTime: new Date(when3).toISOString() }, messageClock(when3)) : null,
answering ? (0, import_react9.createElement)("span", { className: "bt-shimmer" }, t("Answering…")) : null,
@@ -5481,8 +5458,7 @@ function BtwCardView({ card, compact: compact2 = false, actions }) {
"div",
{ className: "bt-btw-body" },
card.status === "failed" ? (0, import_react9.createElement)("p", { className: "bt-btw-fail" }, failureText(card)) : null,
- answer.text !== "" ? (0, import_react9.createElement)("div", { className: "bt-btw-answer" }, (0, import_react9.createElement)(import_dsh_client_ui_primitives.MarkdownText, { text: answer.text, labels: mdLabels2(), variant: "compact" })) : null,
- truncated ? (0, import_react9.createElement)("p", { className: "bt-btw-mark" }, t("Truncated")) : null
+ answer !== "" ? (0, import_react9.createElement)("div", { className: "bt-btw-answer" }, (0, import_react9.createElement)(import_dsh_client_ui_primitives.MarkdownText, { text: answer, labels: mdLabels2(), variant: "compact" })) : null
) : null
);
}
@@ -5974,8 +5950,9 @@ function EarlierParts({ sessionId, roster, actions }) {
setView((current) => {
const pages = current.pages.slice();
const at = pages.findIndex((entry) => entry.partIndex === partIndex);
- if (at >= 0) pages[at] = { partIndex, items: [...incoming, ...pages[at].items] };
- else pages.push({ partIndex, items: incoming });
+ const btw = Array.isArray(page.btw) ? page.btw : at >= 0 ? pages[at].btw : [];
+ if (at >= 0) pages[at] = { partIndex, items: [...incoming, ...pages[at].items], btw };
+ else pages.push({ partIndex, items: incoming, btw });
pages.sort((a, b) => a.partIndex - b.partIndex);
return { pages, cursor: page.cursor, loading: false, failed: false, startedAt: page.startedAt ?? current.startedAt };
});
@@ -6008,10 +5985,8 @@ function EarlierParts({ sessionId, roster, actions }) {
observer.observe(node);
return () => observer.disconnect();
}, [view.cursor, view.loading, view.failed]);
- const bot = roster.byId[sessionId];
- const cards = roster.btwCards?.[bot?.id];
const items = [];
- for (const page of view.pages) items.push(...withBtwRows(page.items, earlierPartCards(cards, bot, sessionId, page.partIndex)));
+ for (const page of view.pages) items.push(...withBtwRows(page.items, page.btw));
return (0, import_react10.createElement)(
"div",
{ className: "bt-earlier", role: "feed", "aria-label": t("Earlier messages"), "aria-busy": view.loading },
diff --git a/ds-bot/src/client/btw-model.js b/ds-bot/src/client/btw-model.js
index 65f25936..1d37d666 100644
--- a/ds-bot/src/client/btw-model.js
+++ b/ds-bot/src/client/btw-model.js
@@ -16,8 +16,6 @@ import { t } from './i18n.js'
export const BTW_SOURCE = 'ds-bot-btw'
export const BTW_TOKEN = '/btw '
-// The host appends this to a card's question or answer, and only when it cut the text.
-const HOST_CUT = ' [truncated]'
const GROUP_REFUSAL = "btw is only available in a Bot's private chat."
const ATTACHMENT_REFUSAL = '/btw does not take attachments.'
@@ -251,13 +249,6 @@ export function partIndexOf(bot, sessionId, cursor) {
return partChain(bot).indexOf(sessionId) - 1
}
-// Cards of one earlier part. The part on screen uses the dock and the turn tail.
-export function earlierPartCards(cards, bot, sessionId, partIndex) {
- const partId = partChain(bot)[partIndex]
- if (typeof partId !== 'string' || partId === '' || partId === sessionId) return []
- return (Array.isArray(cards) ? cards : []).filter(card => card?.partId === partId)
-}
-
function rowTurn(item) {
return typeof item?.turn === 'number' && Number.isInteger(item.turn) ? item.turn : undefined
}
@@ -403,17 +394,3 @@ export function withBtwRows(items, cards) {
for (const card of aside) out.push(btwRow(card))
return out
}
-
-export function presentText(text, truncated) {
- const raw = typeof text === 'string' ? text : ''
- let body = raw
- let cut = false
- if (raw.endsWith(HOST_CUT)) {
- body = raw.slice(0, -HOST_CUT.length)
- cut = true
- } else if (raw.endsWith('已截断')) {
- body = raw.slice(0, -'已截断'.length).trimEnd()
- cut = true
- }
- return { text: body, marked: truncated === true || cut }
-}
diff --git a/ds-bot/src/client/btw.js b/ds-bot/src/client/btw.js
index 519921d8..d551a349 100644
--- a/ds-bot/src/client/btw.js
+++ b/ds-bot/src/client/btw.js
@@ -6,7 +6,7 @@ import { hostText, t } from './i18n.js'
import { messageClock } from './text.js'
import {
BTW_SOURCE, BTW_TOKEN, btwQueryMatches, btwSurface, dockCards, matchBtwClaim, matchBtwEnter,
- matchBtwSpace, presentText, sessionCards, submitBtw, tailCards,
+ matchBtwSpace, sessionCards, submitBtw, tailCards,
} from './btw-model.js'
const mdLabels = () => ({ code: { copyLabel: t('Copy'), copiedLabel: t('Copied') }, footnotes: t('Footnotes') })
@@ -29,9 +29,8 @@ export function BtwCardView({ card, compact = false, actions }) {
if (card?.status && card.status !== 'answering') setOpen(true)
}, [card?.status])
if (!card) return null
- const question = presentText(card.question, card.truncated === true || card.questionTruncated === true)
- const answer = presentText(card.answer, card.truncated === true || card.answerTruncated === true)
- const truncated = question.marked || answer.marked
+ const question = typeof card.question === 'string' ? card.question : ''
+ const answer = typeof card.answer === 'string' ? card.answer : ''
const when = stampOf(card)
const answering = card.status === 'answering'
const cancel = (event) => {
@@ -54,7 +53,7 @@ export function BtwCardView({ card, compact = false, actions }) {
onClick: () => setOpen(value => !value),
},
h('span', { className: 'bt-btw-chevron', 'aria-hidden': true }),
- h('span', { className: 'bt-btw-q' }, question.text)),
+ h('span', { className: 'bt-btw-q' }, question)),
when !== undefined ? h('time', { className: 'bt-btw-time', dateTime: new Date(when).toISOString() }, messageClock(when)) : null,
answering ? h('span', { className: 'bt-shimmer' }, t('Answering…')) : null,
card.status === 'cancelled' ? h('span', { className: 'bt-btw-status' }, t('Cancelled')) : null,
@@ -65,8 +64,7 @@ export function BtwCardView({ card, compact = false, actions }) {
h('p', { className: 'bt-btw-note' }, t('Answered from progress so far; nothing was run')),
open ? h('div', { className: 'bt-btw-body' },
card.status === 'failed' ? h('p', { className: 'bt-btw-fail' }, failureText(card)) : null,
- answer.text !== '' ? h('div', { className: 'bt-btw-answer' }, h(MarkdownText, { text: answer.text, labels: mdLabels(), variant: 'compact' })) : null,
- truncated ? h('p', { className: 'bt-btw-mark' }, t('Truncated')) : null) : null)
+ answer !== '' ? h('div', { className: 'bt-btw-answer' }, h(MarkdownText, { text: answer, labels: mdLabels(), variant: 'compact' })) : null) : null)
}
function useTurnFacts(sessionId, actions) {
diff --git a/ds-bot/src/client/cells.js b/ds-bot/src/client/cells.js
index 1d9b4ec9..bbc21113 100644
--- a/ds-bot/src/client/cells.js
+++ b/ds-bot/src/client/cells.js
@@ -16,7 +16,7 @@ import { FOLD_AT, FOLD_HUGE, stripLongNote, useFold } from './fold.js'
import { noHover, useViewport, viewBottom } from './motion.js'
import { heldCardKey } from './sources.js'
import { BtwCardView } from './btw.js'
-import { earlierPartCards, partIndexOf, withBtwRows } from './btw-model.js'
+import { partIndexOf, withBtwRows } from './btw-model.js'
import { layoutOf } from './themes.js'
export { clockLabel }
@@ -391,8 +391,10 @@ function EarlierParts({ sessionId, roster, actions }) {
setView(current => {
const pages = current.pages.slice()
const at = pages.findIndex(entry => entry.partIndex === partIndex)
- if (at >= 0) pages[at] = { partIndex, items: [...incoming, ...pages[at].items] }
- else pages.push({ partIndex, items: incoming })
+ // The host sends a part's aside cards on every page of that part.
+ const btw = Array.isArray(page.btw) ? page.btw : (at >= 0 ? pages[at].btw : [])
+ if (at >= 0) pages[at] = { partIndex, items: [...incoming, ...pages[at].items], btw }
+ else pages.push({ partIndex, items: incoming, btw })
pages.sort((a, b) => a.partIndex - b.partIndex)
return { pages, cursor: page.cursor, loading: false, failed: false, startedAt: page.startedAt ?? current.startedAt }
})
@@ -423,10 +425,8 @@ function EarlierParts({ sessionId, roster, actions }) {
observer.observe(node)
return () => observer.disconnect()
}, [view.cursor, view.loading, view.failed])
- const bot = roster.byId[sessionId]
- const cards = roster.btwCards?.[bot?.id]
const items = []
- for (const page of view.pages) items.push(...withBtwRows(page.items, earlierPartCards(cards, bot, sessionId, page.partIndex)))
+ for (const page of view.pages) items.push(...withBtwRows(page.items, page.btw))
return h('div', { className: 'bt-earlier', role: 'feed', 'aria-label': t('Earlier messages'), 'aria-busy': view.loading },
h('div', { ref: top, className: 'bt-earlier-top' },
view.loading ? h(Shimmer, null, t('Loading earlier messages…'))
diff --git a/ds-bot/src/client/i18n.js b/ds-bot/src/client/i18n.js
index fd27a60a..933d66a0 100644
--- a/ds-bot/src/client/i18n.js
+++ b/ds-bot/src/client/i18n.js
@@ -739,7 +739,6 @@ const ZH = {
'Answering…': '正在回答…',
Cancelled: '已取消',
'Answered from progress so far; nothing was run': '基于目前已有进展的回答,没有执行任何操作',
- Truncated: '已截断',
'Could not answer.': '没能答上。',
'Cancel this side question': '取消这次旁路提问',
Conversations: '对话',
@@ -1020,7 +1019,6 @@ const HOST_ZH = {
"btw is only available in a Bot's private chat.": '旁路提问只在 Bot 的私聊里可用。',
'Write a question after /btw.': '在 /btw 后面写上问题。',
'/btw does not take attachments.': '/btw 不能带附件。',
- 'Two aside questions are already running for this Bot. Ask again when one finishes.': '这个 Bot 已经有两个旁路提问在进行,等其中一个结束再问。',
'This Bot has not called a model yet. Send a normal message first.': '这个 Bot 还没有调用过模型,请先正常发一条消息。',
'DS Bot restarted before this answer finished.': 'DS Bot 在回答完成前重启了。',
'The model called a tool again after being told not to, so this aside was not answered.': '模型在被要求不要调用工具之后又调用了工具,所以这次没有回答。',
diff --git a/ds-bot/src/client/styles.js b/ds-bot/src/client/styles.js
index 9452315a..e572cd95 100644
--- a/ds-bot/src/client/styles.js
+++ b/ds-bot/src/client/styles.js
@@ -1099,7 +1099,7 @@ div:has(> nav [class*="_marks"]){display:none!important}
.bt-btw[data-open] .bt-btw-chevron{transform:rotate(45deg)}
.bt-btw-q{min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font-size:13px;line-height:18px}
.bt-btw[data-open] .bt-btw-q{white-space:normal}
-.bt-btw-time,.bt-btw-status,.bt-btw-mark{flex:none;color:var(--bt-ink-3);font-size:11px;line-height:14px;font-variant-numeric:tabular-nums}
+.bt-btw-time,.bt-btw-status{flex:none;color:var(--bt-ink-3);font-size:11px;line-height:14px;font-variant-numeric:tabular-nums}
.bt-btw-cancel{margin-left:auto}
.bt-btw-cancel:disabled{opacity:.55;cursor:default}
.bt-btw-note{margin:4px 0 0;color:var(--bt-ink-3);font-size:12px;line-height:16px}
@@ -1107,7 +1107,6 @@ div:has(> nav [class*="_marks"]){display:none!important}
.bt-btw-answer{font-size:14px;line-height:20px}
.bt-btw-answer p{margin:0}
.bt-btw-fail{margin:0 0 6px;color:var(--bt-danger);font-size:13px;line-height:18px}
-.bt-btw-mark{margin:6px 0 0}
@media (prefers-reduced-motion:reduce){.bt-btw-chevron{transition:none}}
`
diff --git a/ds-bot/src/host/btw.js b/ds-bot/src/host/btw.js
index c030f1d1..98ec9214 100644
--- a/ds-bot/src/host/btw.js
+++ b/ds-bot/src/host/btw.js
@@ -1,9 +1,14 @@
// Aside questions (btw): one model call on a snapshot of a Bot's private chat.
// Nothing is written into the Session, and no tool call is ever dispatched.
+// Cards live in a file per Bot and part (<home>/btw/<botId>/<partId>.json), not
+// in state.json, so a save never rewrites them; only the host that owns the
+// team writes them.
import { randomUUID } from 'node:crypto'
+import { mkdir, readFile, rm, unlink, writeFile } from 'node:fs/promises'
+import { dirname, join } from 'node:path'
import { BlockAssembler } from '@deepseek-ai/dsh-llm'
-import { BTW_CARDS_MAX, BTW_CUT, BTW_IN_FLIGHT, BTW_LIMIT, BTW_TEXT_MAX } from './constants.js'
-import { headOf } from './text.js'
+import { BTW_LIMIT } from './constants.js'
+import { replaceFile } from './files.js'
// The mock model recognizes an aside by this first line.
export const BTW_MARKER = 'ds-bot/btw'
@@ -85,8 +90,6 @@ function anchorState(session, projections) {
return projections.stateOf(session, ANCHOR_KEY)
}
-const keptText = text => (text.length <= BTW_TEXT_MAX ? text : `${headOf(text, BTW_TEXT_MAX)}${BTW_CUT}`)
-
const callIds = (message) => {
if (message?.role !== 'assistant' || !Array.isArray(message.content)) return []
const ids = []
@@ -158,67 +161,6 @@ function snapshotOf(session, projections) {
const promptFor = (question, retry) => [BTW_PROMPT, ...(retry ? [BTW_RETRY] : []), question].join('\n\n')
-// Newest first. A card that is still answering is never dropped: the cap gives
-// way, but only for as long as that call is running.
-function retainCards(list) {
- if (list.length <= BTW_CARDS_MAX) return list
- let answering = 0
- for (const card of list) if (card?.status === 'answering') answering += 1
- if (answering === 0) return list.slice(0, BTW_CARDS_MAX)
- const finishedRoom = Math.max(BTW_CARDS_MAX, answering) - answering
- const kept = []
- let finished = 0
- for (const card of list) {
- if (card?.status === 'answering') {
- kept.push(card)
- continue
- }
- if (finished < finishedRoom) {
- kept.push(card)
- finished += 1
- }
- }
- return kept
-}
-
-// `recover` is only for the host that owns the team. A reader keeps a live
-// 'answering' card as it is. A bad value becomes an empty map so a later save
-// still loads.
-export function normalizeBtwCards(cards, options = {}) {
- const recover = options?.recover === true
- const now = typeof options?.now === 'number' ? options.now : Date.now()
- if (cards === undefined) return { cards: {}, changed: false }
- if (cards === null || typeof cards !== 'object' || Array.isArray(cards)) return { cards: {}, changed: true }
- let changed = false
- const next = {}
- for (const [botId, list] of Object.entries(cards)) {
- if (!Array.isArray(list)) {
- changed = true
- continue
- }
- const kept = []
- let touched = false
- for (const card of list) {
- if (card === null || typeof card !== 'object' || Array.isArray(card)) {
- touched = true
- continue
- }
- if (recover && card.status === 'answering') {
- touched = true
- kept.push({ ...card, status: 'failed', error: card.error ?? RESTART_ERROR, finishedAt: card.finishedAt ?? now })
- } else {
- kept.push(card)
- }
- }
- const capped = retainCards(kept)
- const stored = !touched && capped.length === list.length && capped.every((card, index) => card === list[index]) ? list : capped
- if (stored !== list) changed = true
- if (stored.length > 0) next[botId] = stored
- }
- if (Object.keys(next).length !== Object.keys(cards).length) changed = true
- return { cards: changed ? next : cards, changed }
-}
-
export function install(rt) {
const { ctx, state } = rt
registerAnchor(ctx)
@@ -230,7 +172,6 @@ export function install(rt) {
let disposing = false
const dropped = new Set()
- const inflight = botId => byBot.get(botId)?.size ?? 0
const track = (id, botId, controller) => {
flights.set(id, { botId, controller })
const ids = byBot.get(botId) ?? new Set()
@@ -245,8 +186,139 @@ export function install(rt) {
ids?.delete(id)
if (ids?.size === 0) byBot.delete(flight.botId)
}
- const listOf = botId => (Array.isArray(rt.own(state.btwCards, botId)) ? state.btwCards[botId] : [])
- const findCard = (botId, id) => listOf(botId).find(card => card?.id === id)
+
+ // Cards by Bot and part. A part's list is read from
+ // <home>/btw/<botId>/<partId>.json on first need and kept here; writes are
+ // whole-file replaces, queued so a finish and a cancel never interleave. A
+ // runtime without a home (a bare channel install) keeps cards in memory only.
+ const cardsDir = typeof rt.home === 'string' ? join(rt.home, 'btw') : undefined
+ const partFile = (botId, partId) => join(cardsDir, botId, `${partId}.json`)
+ const partKey = (botId, partId) => `${botId}/${partId}`
+ const writes = () => rt.readOnly?.() === undefined
+ /** @type {Map<string, Map<string, object[]>>} */
+ const cards = new Map()
+ const reading = new Map()
+ let writing = Promise.resolve()
+
+ const listOf = (botId, partId) => cards.get(botId)?.get(partId) ?? []
+ const findCard = (botId, id) => {
+ for (const list of cards.get(botId)?.values() ?? []) {
+ const found = list.find(card => card?.id === id)
+ if (found !== undefined) return found
+ }
+ return undefined
+ }
+
+ const writePart = (botId, partId) => {
+ const run = writing.then(async () => {
+ if (cardsDir === undefined || !writes() || dropped.has(botId) || rt.botOf(botId) === undefined) return
+ const path = partFile(botId, partId)
+ await mkdir(dirname(path), { recursive: true })
+ const temp = `${path}.${process.pid}.tmp`
+ try {
+ await writeFile(temp, JSON.stringify(cards.get(botId)?.get(partId) ?? [], null, 2), 'utf8')
+ await replaceFile(temp, path)
+ } catch (error) {
+ await unlink(temp).catch(() => {})
+ throw error
+ }
+ })
+ writing = run.catch(() => {})
+ return run
+ }
+
+ // A card event rewrites its part's file; the state save afterwards only bumps
+ // the revision, so a window polls fresh cards.
+ const persist = async (card) => {
+ await writePart(card.botId, card.partId).catch(error => rt.warn('ds-bot: btw cards of %s not saved: %s', card.botId, error))
+ await rt.save()
+ }
+
+ // A part's list, reading its file once. The owning host records the restart a
+ // previous process left behind: an 'answering' card that is not one of this
+ // process's flights failed with it, and the file is written again. A reading
+ // host keeps the cards as they were saved.
+ async function readPart(botId, partId) {
+ const byBot = cards.get(botId) ?? new Map()
+ cards.set(botId, byBot)
+ const held = byBot.get(partId)
+ if (held !== undefined) return held
+ const key = partKey(botId, partId)
+ const pending = reading.get(key)
+ if (pending !== undefined) return pending
+ const job = (async () => {
+ let list = []
+ if (cardsDir === undefined) {
+ byBot.set(partId, list)
+ return list
+ }
+ try {
+ const parsed = JSON.parse(await readFile(partFile(botId, partId), 'utf8'))
+ if (Array.isArray(parsed)) list = parsed.filter(card => card !== null && typeof card === 'object' && !Array.isArray(card))
+ } catch (error) {
+ if (error?.code !== 'ENOENT') rt.warn('ds-bot: unreadable btw cards %s: %s', partFile(botId, partId), error?.code ?? error)
+ }
+ if (writes()) {
+ const now = Date.now()
+ let touched = false
+ list = list.map((card) => {
+ if (card.status !== 'answering' || flights.has(card.id)) return card
+ touched = true
+ return { ...card, status: 'failed', error: card.error ?? RESTART_ERROR, finishedAt: card.finishedAt ?? now }
+ })
+ byBot.set(partId, list)
+ if (touched) await writePart(botId, partId).catch(error => rt.warn('ds-bot: btw cards of %s not saved: %s', botId, error))
+ } else {
+ byBot.set(partId, list)
+ }
+ return byBot.get(partId)
+ })()
+ reading.set(key, job)
+ try {
+ return await job
+ } finally {
+ reading.delete(key)
+ }
+ }
+
+ // Only each Bot's current part is read up front. An earlier part's file is
+ // read when that part's history is asked for (parts.js's earlier page).
+ const originalLoad = rt.load?.bind(rt)
+ if (typeof originalLoad === 'function') {
+ let ready
+ rt.load = () => (ready ??= (async () => {
+ await originalLoad()
+ for (const botId of Object.keys(state.bots)) await readPart(botId, rt.chatOf(botId))
+ })())
+ }
+
+ const originalRefresh = rt.refresh?.bind(rt)
+ if (typeof originalRefresh === 'function') {
+ rt.refresh = async () => {
+ const wasWriting = writes()
+ await originalRefresh()
+ // Taking over a team the other host closed makes this one the writer:
+ // re-read the loaded parts so a leftover 'answering' is recorded now.
+ if (wasWriting || !writes()) return
+ for (const [botId, byBot] of cards) {
+ const parts = Array.from(byBot.keys())
+ byBot.clear()
+ for (const partId of parts) await readPart(botId, partId)
+ }
+ }
+ }
+
+ // The team view carries each Bot's current part only; earlier parts come with
+ // their history pages.
+ const btwView = () => {
+ const view = {}
+ for (const botId of Object.keys(state.bots)) {
+ const list = listOf(botId, rt.chatOf(botId))
+ if (list.length > 0) view[botId] = list
+ }
+ return view
+ }
+
const abandoned = botId => disposing || dropped.has(botId) || rt.botOf(botId) === undefined
const stopError = (botId) => new Error(disposing && !dropped.has(botId) && rt.botOf(botId) !== undefined ? 'DS Bot is stopping.' : 'Unknown Bot')
@@ -318,31 +390,31 @@ export function install(rt) {
if (result.failed !== undefined) {
// Cancel already left this card, and the check above returned. A provider
// that stops on its own still fails the card, so it does not stay 'answering'.
- if (settle(card, { status: 'failed', error: result.failed, answer: '' })) await rt.save()
+ if (settle(card, { status: 'failed', error: result.failed, answer: '' })) await persist(card)
return
}
if (result.cutOff) {
- const answer = result.text ? keptText(result.text) : ''
- if (settle(card, { status: 'failed', error: BTW_LIMIT, answer, finish: 'max-tokens' })) await rt.save()
+ const answer = result.text ?? ''
+ if (settle(card, { status: 'failed', error: BTW_LIMIT, answer, finish: 'max-tokens' })) await persist(card)
return
}
if (result.toolCalls) {
if (attempt === 0) continue
- if (settle(card, { status: 'failed', error: TOOL_ERROR, answer: '' })) await rt.save()
+ if (settle(card, { status: 'failed', error: TOOL_ERROR, answer: '' })) await persist(card)
return
}
if (result.text === '') {
- if (settle(card, { status: 'failed', error: EMPTY_ERROR, answer: '' })) await rt.save()
+ if (settle(card, { status: 'failed', error: EMPTY_ERROR, answer: '' })) await persist(card)
return
}
- if (settle(card, { status: 'done', answer: keptText(result.text) })) await rt.save()
+ if (settle(card, { status: 'done', answer: result.text })) await persist(card)
return
}
} catch (error) {
if (signal.aborted || card.status !== 'answering' || !alive(card)) return
const message = error?.message ?? String(error)
rt.warn('ds-bot: btw for %s failed: %s', card.botId, message)
- if (settle(card, { status: 'failed', error: message, answer: '' })) await rt.save()
+ if (settle(card, { status: 'failed', error: message, answer: '' })) await persist(card)
}
}
@@ -363,7 +435,6 @@ export function install(rt) {
const question = typeof text === 'string' ? text : ''
if (question.trim() === '') throw Object.assign(new Error('Write a question after /btw.'), { code: 'bot/invalid' })
const { sessionId, botId } = locate(raw)
- if (inflight(botId) >= BTW_IN_FLIGHT) throw new Error('Two aside questions are already running for this Bot. Ask again when one finishes.')
const id = randomUUID()
const controller = new AbortController()
track(id, botId, controller)
@@ -381,14 +452,14 @@ export function install(rt) {
atSeq: snapshot.atSeq,
anchorSeq: snapshot.anchorSeq,
turn: snapshot.turn,
- question: keptText(question),
+ question,
answer: '',
status: 'answering',
createdAt: Date.now(),
}
- state.btwCards ??= {}
- state.btwCards[botId] = retainCards([card, ...listOf(botId)])
- await rt.save()
+ await readPart(botId, sessionId)
+ cards.get(botId).set(sessionId, [card, ...listOf(botId, sessionId)])
+ await persist(card)
// Unload keeps the saved card for the next owner. Deletion already removed it.
if (abandoned(botId) || findCard(botId, id) !== card) {
if (disposing && !dropped.has(botId) && findCard(botId, id) === card) return { id }
@@ -400,11 +471,10 @@ export function install(rt) {
started = true
return { id }
} catch (error) {
- const list = listOf(botId)
+ const list = listOf(botId, sessionId)
if (list.some(card => card.id === id)) {
- const rest = list.filter(card => card.id !== id)
- if (rest.length === 0) delete state.btwCards[botId]
- else state.btwCards[botId] = rest
+ cards.get(botId)?.set(sessionId, list.filter(card => card.id !== id))
+ await writePart(botId, sessionId).catch(() => {})
}
throw error
} finally {
@@ -420,7 +490,7 @@ export function install(rt) {
card.status = 'cancelled'
card.finishedAt = Date.now()
flights.get(id)?.controller.abort()
- await rt.save()
+ await persist(card)
return { answering: true }
}
@@ -429,16 +499,19 @@ export function install(rt) {
disposing = true
for (const flight of flights.values()) flight.controller.abort()
await drain()
+ await writing
}
// Clears the Bot's cards before the roster save, and waits out calls that
// would otherwise finish into the deleted team.
async function dropBot(botId) {
dropped.add(botId)
- if (state.btwCards != null) delete state.btwCards[botId]
+ cards.delete(botId)
+ for (const key of reading.keys()) if (key.startsWith(`${botId}/`)) reading.delete(key)
const ids = [...(byBot.get(botId) ?? [])]
for (const id of ids) flights.get(id)?.controller.abort()
await drain(ids)
+ if (cardsDir !== undefined && writes()) await rm(join(cardsDir, botId), { recursive: true, force: true }).catch(error => rt.warn('ds-bot: btw cards of %s not removed: %s', botId, error))
}
// Channel can be installed without the roster (the thinking route test). The
@@ -453,5 +526,5 @@ export function install(rt) {
const btwJoin = id => flights.get(id)?.done ?? Promise.resolve()
- Object.assign(rt, { askBtw, cancelBtw, btwJoin, btwDispose })
+ Object.assign(rt, { askBtw, cancelBtw, btwJoin, btwDispose, btwView, btwPartCards: readPart })
}
diff --git a/ds-bot/src/host/channel.js b/ds-bot/src/host/channel.js
index adc49ddc..ba5f4ae0 100644
--- a/ds-bot/src/host/channel.js
+++ b/ds-bot/src/host/channel.js
@@ -53,8 +53,9 @@ export function install(rt) {
exchangeTurns: state.exchangeTurns,
questions: state.questions,
views: state.views ?? {},
- // Aside answers, per Bot, newest first. Not part of the model-visible history.
- btwCards: state.btwCards ?? {},
+ // Aside answers of each Bot's current part, newest first. Not part of the
+ // model-visible history; earlier parts send their cards on their own pages.
+ btwCards: rt.btwView?.() ?? {},
// The team's DSH Agent (at most one): plain dsh Sessions, not Bots.
agents: Object.values(state.agents),
// Key names and who may use them; values never leave the host.
diff --git a/ds-bot/src/host/constants.js b/ds-bot/src/host/constants.js
index 236b786f..19ab2f17 100644
--- a/ds-bot/src/host/constants.js
+++ b/ds-bot/src/host/constants.js
@@ -83,13 +83,5 @@ export const OWN_CHAT_SEARCH = 20
export const HISTORY_PAGE = 200
export const HISTORY_PAGES_MAX = 50
export const HISTORY_TIMEOUT_MS = 20_000
-// An aside (btw) keeps this many cards per Bot, newest first.
-export const BTW_CARDS_MAX = 20
-// Question and answer text kept on a card. The model still receives the whole question.
-export const BTW_TEXT_MAX = 2000
-// Appended to a card's question or answer only, and only when that text was cut.
-export const BTW_CUT = ' [truncated]'
-// Aside calls in flight for one Bot. Each one reads the whole conversation.
-export const BTW_IN_FLIGHT = 2
// Shown when an aside stops at the route's own output limit. The partial answer stays.
export const BTW_LIMIT = 'Answer cut off at the output limit.'
diff --git a/ds-bot/src/host/parts.js b/ds-bot/src/host/parts.js
index d8eaadcf..fb151a07 100644
--- a/ds-bot/src/host/parts.js
+++ b/ds-bot/src/host/parts.js
@@ -1296,7 +1296,10 @@ export function install(rt) {
const at = parts.findIndex(part => part.sessionId === sessionId)
const index = Number.isInteger(cursor?.part) ? cursor.part : at - 1
if (at <= 0 || index < 0 || index >= at) return { items: [], cursor: null, startedAt: parts[at]?.startedAt ?? null }
- return { ...(await partPage(parts, index, cursor)), startedAt: parts[at].startedAt }
+ // Aside cards belong to the part that was current when they were asked, so
+ // they ride along with that part's history instead of the team view.
+ const btw = (await rt.btwPartCards?.(botId, parts[index].sessionId)) ?? []
+ return { ...(await partPage(parts, index, cursor)), btw, startedAt: parts[at].startedAt }
}
// A Bot's whole own chat, newest page first: the current part, then the earlier
diff --git a/ds-bot/src/host/store.js b/ds-bot/src/host/store.js
index 7e2b5ff2..9299901d 100644
--- a/ds-bot/src/host/store.js
+++ b/ds-bot/src/host/store.js
@@ -4,7 +4,6 @@
import { mkdirSync, statSync } from 'node:fs'
import { copyFile, mkdir, readFile, unlink, writeFile } from 'node:fs/promises'
import { join } from 'node:path'
-import { normalizeBtwCards } from './btw.js'
import { LEGACY_THEMES, MAX_EXCHANGES, ROOM_MODES, TEXT_KEPT } from './constants.js'
import { replaceFile } from './files.js'
import { homeLock } from './home-lock.js'
@@ -20,7 +19,7 @@ export function install(rt) {
const { ctx, home, statePath } = rt
/** @type {{version:1, workspaceId?:string, mainBotIds:string[], bots:Record<string,any>, rooms:Record<string,any>, agents:Record<string,any>, capabilities:{items:Record<string,any>}, exchanges:any[], exchangeTurns:Record<string,number[]>, revision:number}} */
- const state = { version: 1, mainBotIds: [], bots: {}, rooms: {}, agents: {}, capabilities: { items: {} }, exchanges: [], exchangeTurns: {}, works: {}, questions: {}, views: {}, btwCards: {}, prefs: {}, switching: {}, revision: 0 }
+ const state = { version: 1, mainBotIds: [], bots: {}, rooms: {}, agents: {}, capabilities: { items: {} }, exchanges: [], exchangeTurns: {}, works: {}, questions: {}, views: {}, prefs: {}, switching: {}, revision: 0 }
let loaded = null
let writing = Promise.resolve()
const { warn } = rt
@@ -162,15 +161,13 @@ export function install(rt) {
}
if (state.works === null || typeof state.works !== 'object' || Array.isArray(state.works)) state.works = {}
if (state.views === null || typeof state.views !== 'object' || Array.isArray(state.views)) state.views = {}
- // A live aside belongs to the host that owns the team. A reader keeps its
- // status; only the owner, and only while taking the team, records that a
- // previous process left it unfinished.
- const writer = holder === undefined && stuck === undefined
- const btw = normalizeBtwCards(state.btwCards, { recover: writer })
- if (btw.changed) {
- state.btwCards = btw.cards
+ // LEGACY: aside cards lived in this file only before the feature shipped;
+ // they are kept under btw/ per part now. A saved copy is dropped, not moved.
+ if ('btwCards' in state) {
+ delete state.btwCards
migrated = true
}
+ const writer = holder === undefined && stuck === undefined
// LEGACY: Bots no longer count the rounds they start in a group.
for (const room of Object.values(state.rooms)) {
if (!('botRounds' in room)) continue
diff --git a/ds-bot/test/btw.test.js b/ds-bot/test/btw.test.js
index 5a47b08f..ff875492 100644
--- a/ds-bot/test/btw.test.js
+++ b/ds-bot/test/btw.test.js
@@ -1,12 +1,10 @@
import assert from 'node:assert/strict'
-import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
+import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { hostname, tmpdir } from 'node:os'
import { join } from 'node:path'
import { test } from 'node:test'
import { createRuntime } from '../index.js'
-import { BTW_MARKER, normalizeBtwCards, trimToolBoundary } from '../src/host/btw.js'
-import { BTW_CARDS_MAX, BTW_CUT, BTW_TEXT_MAX } from '../src/host/constants.js'
-import { headOf } from '../src/host/text.js'
+import { BTW_MARKER, trimToolBoundary } from '../src/host/btw.js'
import { fakeContext } from './fake-ctx.js'
const user = text => ({ role: 'user', content: [{ type: 'text', text }] })
@@ -113,10 +111,16 @@ function deferred() {
// `gate` holds each model call open so a test can see the card while it is still
// answering. `scripts` are the chunk lists for successive calls; anything further
-// gets a short stop.
-async function setup({ state = team(), scripts = [], gate = false, messages, seq = 4, events, foreignLock = false } = {}) {
+// gets a short stop. `btw` seeds card files: { botId: { partId: [cards] } }.
+async function setup({ state = team(), scripts = [], gate = false, messages, seq = 4, events, foreignLock = false, btw = {} } = {}) {
const home = mkdtempSync(join(tmpdir(), 'dsb-btw-'))
writeFileSync(join(home, 'state.json'), JSON.stringify(state))
+ for (const [botId, parts] of Object.entries(btw)) {
+ for (const [partId, list] of Object.entries(parts)) {
+ mkdirSync(join(home, 'btw', botId), { recursive: true })
+ writeFileSync(join(home, 'btw', botId, `${partId}.json`), JSON.stringify(list))
+ }
+ }
if (foreignLock) writeFileSync(join(home, 'state.lock'), JSON.stringify({ pid: process.ppid, host: hostname(), token: 'other' }))
const header = {
config: { provider: 'deepseek', model: 'aside-model', reasoningEffort: 'high', maxTokens: 8000, temperature: 0.2, stop: ['END'] },
@@ -183,14 +187,15 @@ async function setup({ state = team(), scripts = [], gate = false, messages, seq
}
const stopRuntime = async () => {
for (const hold of gates) hold.resolve()
- for (const card of Object.values(rt.state.btwCards ?? {}).flat()) await rt.btwJoin(card.id)
+ for (const card of Object.values(rt.btwView?.() ?? {}).flat()) await rt.btwJoin(card.id)
await ctx.dispose()
rmSync(home, { recursive: true, force: true, maxRetries: 5 })
}
return { home, ctx, rt, session, sessions, header, toolHistory, requests, resolved, gates, call: callEndpoint, notices: () => notices, stop: stopRuntime }
}
-const cardsOf = rt => rt.state.btwCards?.b1 ?? []
+const cardsOf = rt => rt.btwView?.().b1 ?? []
+const cardsFile = (home, botId = 'b1', partId = 's-now') => JSON.parse(readFileSync(join(home, 'btw', botId, `${partId}.json`), 'utf8'))
const totals = (view) => {
const sums = {}
for (const [, owner, , kind, input, cacheRead, cacheWrite, output] of view.rows) {
@@ -316,7 +321,7 @@ test('a Bot id or an earlier private part asks the current part', async (t) => {
assert.equal(cardsOf(env.rt)[0].atSeq, null)
})
-test('group chats, an empty question, a missing route, and a third aside are refused', async (t) => {
+test('group chats, an empty question, and a missing route are refused, while further asides start', async (t) => {
const env = await setup({ gate: true })
t.after(() => env.stop())
const { session } = env
@@ -344,12 +349,13 @@ test('group chats, an empty question, a missing route, and a third aside are ref
assert.equal(cardsOf(env.rt).length, 0)
const first = await env.call('btw', { sessionId: 'b1', text: 'one' })
const second = await env.call('btw', { sessionId: 's-now', text: 'two' })
+ const third = await env.call('btw', { sessionId: 's-now', text: 'three' })
assert.equal(first.ok, true)
assert.equal(second.ok, true)
- await refuse({ sessionId: 's-now', text: 'three' }, 'Two aside questions are already running for this Bot. Ask again when one finishes.')
- assert.equal(env.requests.length, 2)
- assert.equal(session.derived, 3)
- assert.equal(cardsOf(env.rt).length, 2)
+ assert.equal(third.ok, true)
+ assert.equal(env.requests.length, 3)
+ assert.equal(session.derived, 4)
+ assert.equal(cardsOf(env.rt).length, 3)
assert.equal(session.writes, 0)
})
@@ -417,48 +423,48 @@ test('cancel aborts the call, and the freed slot can be used again', async (t) =
assert.equal(cardsOf(env.rt).find(item => item.id === next.value.id).status, 'answering')
})
-test('questions and answers are stored truncated, newest first, at most 20, and the view carries them', async (t) => {
- const question = `${'q'.repeat(BTW_TEXT_MAX)}END-OF-QUESTION`
- const answer = `${'a'.repeat(BTW_TEXT_MAX)}END-OF-ANSWER`
+test('questions and answers are kept whole, newest first, unbounded, in the part file', async (t) => {
+ const question = `${'q'.repeat(2000)}END-OF-QUESTION`
+ const answer = `${'a'.repeat(2000)}END-OF-ANSWER`
const env = await setup({ scripts: [stop(answer)] })
t.after(() => env.stop())
- const truncated = await env.call('btw', { sessionId: 's-now', text: question })
- await env.rt.btwJoin(truncated.value.id)
+ const long = await env.call('btw', { sessionId: 's-now', text: question })
+ await env.rt.btwJoin(long.value.id)
const card = cardsOf(env.rt)[0]
- assert.equal(card.id, truncated.value.id)
- assert.equal(card.question, `${headOf(question, BTW_TEXT_MAX)}${BTW_CUT}`)
- assert.equal(card.question.includes('END-OF-QUESTION'), false)
- assert.equal(card.answer, `${headOf(answer, BTW_TEXT_MAX)}${BTW_CUT}`)
- assert.equal(card.answer.includes('END-OF-ANSWER'), false)
+ assert.equal(card.id, long.value.id)
+ assert.equal(card.question, question)
+ assert.equal(card.answer, answer)
assert.equal(card.status, 'done')
assert.equal(promptOf(env.requests[0]).endsWith(question), true)
- for (let index = 0; index < BTW_CARDS_MAX; index += 1) {
+ for (let index = 0; index < 25; index += 1) {
const response = await env.call('btw', { sessionId: 's-now', text: `item ${String(index).padStart(2, '0')}` })
await env.rt.btwJoin(response.value.id)
}
const cards = cardsOf(env.rt)
- assert.equal(cards.length, BTW_CARDS_MAX)
- assert.equal(cards[0].question, `item ${String(BTW_CARDS_MAX - 1).padStart(2, '0')}`)
- assert.equal(cards.some(item => item.id === truncated.value.id), false)
+ assert.equal(cards.length, 26)
+ assert.equal(cards[0].question, 'item 24')
+ assert.equal(cards.at(-1).id, long.value.id)
+ const file = cardsFile(env.home)
+ assert.equal(file.length, 26)
+ assert.equal(file[0].id, cards[0].id)
const saved = JSON.parse(readFileSync(join(env.home, 'state.json'), 'utf8'))
- assert.equal(saved.btwCards.b1.length, BTW_CARDS_MAX)
- assert.equal(saved.btwCards.b1[0].id, cards[0].id)
+ assert.equal('btwCards' in saved, false)
const view = await env.call('state', {})
assert.equal(view.ok, true, view.error?.message)
+ assert.equal(view.value.btwCards.b1.length, 26)
assert.equal(view.value.btwCards.b1[0].id, cards[0].id)
- assert.equal(view.value.btwCards.b1.length, BTW_CARDS_MAX)
})
test('a restart marks an aside that was still answering as failed', async (t) => {
const env = await setup({
- state: team({
- btwCards: {
- b1: [
+ btw: {
+ b1: {
+ 's-now': [
{ id: 'live', botId: 'b1', partId: 's-now', atSeq: 2, question: 'Still going?', answer: '', status: 'answering', createdAt: 10 },
{ id: 'old', botId: 'b1', partId: 's-now', atSeq: 1, question: 'Earlier?', answer: 'Yes.', status: 'done', createdAt: 9, finishedAt: 11 },
],
},
- }),
+ },
})
t.after(() => env.stop())
const [live, old] = cardsOf(env.rt)
@@ -469,10 +475,13 @@ test('a restart marks an aside that was still answering as failed', async (t) =>
assert.equal(old.status, 'done')
assert.equal(old.answer, 'Yes.')
assert.equal(old.finishedAt, 11)
+ const file = cardsFile(env.home)
+ assert.equal(file[0].status, 'failed')
+ assert.equal(file[0].error, 'DS Bot restarted before this answer finished.')
+ assert.equal(file[1].status, 'done')
const saved = JSON.parse(readFileSync(join(env.home, 'state.json'), 'utf8'))
- assert.equal(saved.btwCards.b1[0].status, 'failed')
- assert.equal(saved.btwCards.b1[1].status, 'done')
- assert.equal(saved.revision, 4)
+ assert.equal('btwCards' in saved, false)
+ assert.equal(saved.revision, 3)
})
test('an empty answer and a provider abort fail the card', async (t) => {
@@ -495,43 +504,17 @@ test('an empty answer and a provider abort fail the card', async (t) => {
assert.equal(aborted.session.writes, 0)
})
-test('a reader keeps a live aside, and only the owner records a restart', () => {
- const answering = { id: 'live', botId: 'b1', status: 'answering', question: 'Still going?', answer: '', createdAt: 1 }
- const done = { id: 'old', botId: 'b1', status: 'done', question: 'Earlier?', answer: 'Yes.', createdAt: 2, finishedAt: 3 }
- const cards = { b1: [answering, done] }
- const reader = normalizeBtwCards(cards, { recover: false })
- assert.equal(reader.changed, false)
- assert.equal(reader.cards, cards)
- assert.equal(answering.status, 'answering')
- const owner = normalizeBtwCards(cards, { recover: true, now: 50 })
- assert.equal(owner.changed, true)
- assert.equal(owner.cards.b1[0].status, 'failed')
- assert.equal(owner.cards.b1[0].error, 'DS Bot restarted before this answer finished.')
- assert.equal(owner.cards.b1[0].finishedAt, 50)
- assert.equal(owner.cards.b1[1], done)
- assert.equal(answering.status, 'answering')
- const crowded = {
- b1: [
- ...Array.from({ length: BTW_CARDS_MAX }, (_, index) => ({ id: `d${index}`, status: 'done' })),
- { id: 'run', status: 'answering' },
- ],
- }
- const kept = normalizeBtwCards(crowded, { recover: false })
- assert.equal(kept.cards.b1.some(card => card.id === 'run' && card.status === 'answering'), true)
- assert.equal(kept.cards.b1.filter(card => card.status === 'done').length, BTW_CARDS_MAX - 1)
-})
-
test('a second host does not fail an aside the writer is still answering', async (t) => {
const env = await setup({
foreignLock: true,
- state: team({
- btwCards: {
- b1: [
+ btw: {
+ b1: {
+ 's-now': [
{ id: 'live', botId: 'b1', partId: 's-now', atSeq: 2, question: 'Still going?', answer: '', status: 'answering', createdAt: 10 },
{ id: 'old', botId: 'b1', partId: 's-now', atSeq: 1, question: 'Earlier?', answer: 'Yes.', status: 'done', createdAt: 9, finishedAt: 11 },
],
},
- }),
+ },
})
t.after(() => env.stop())
assert.equal(typeof env.rt.readOnly(), 'string')
@@ -541,9 +524,11 @@ test('a second host does not fail an aside the writer is still answering', async
assert.equal(live.finishedAt, undefined)
assert.equal(old.status, 'done')
assert.equal(old.answer, 'Yes.')
+ const file = cardsFile(env.home)
+ assert.equal(file[0].status, 'answering')
+ assert.equal(file[1].status, 'done')
const saved = JSON.parse(readFileSync(join(env.home, 'state.json'), 'utf8'))
- assert.equal(saved.btwCards.b1[0].status, 'answering')
- assert.equal(saved.btwCards.b1[1].status, 'done')
+ assert.equal('btwCards' in saved, false)
assert.equal(saved.revision, 3)
assert.equal(env.rt.state.revision, 3)
assert.equal(env.notices(), 0)
@@ -562,19 +547,19 @@ test('deleting a Bot aborts its aside and clears the card', async (t) => {
}))
const response = await env.call('btw', { sessionId: 'b2', text: 'hold this' })
assert.equal(response.ok, true, response.error?.message)
- const card = env.rt.state.btwCards.b2[0]
+ const card = env.rt.btwView().b2[0]
const signal = env.requests[0].signal
assert.equal(card.status, 'answering')
await env.rt.deleteBot('b2')
assert.equal(signal.aborted, true)
assert.equal(env.rt.botOf('b2'), undefined)
- assert.equal(env.rt.state.btwCards?.b2, undefined)
+ assert.equal(env.rt.btwView().b2, undefined)
await env.rt.btwJoin(card.id)
assert.notEqual(card.status, 'done')
- assert.equal(env.rt.state.btwCards?.b2, undefined)
+ assert.equal(existsSync(join(env.home, 'btw', 'b2')), false)
const saved = JSON.parse(readFileSync(join(env.home, 'state.json'), 'utf8'))
assert.equal(saved.bots.b2, undefined)
- assert.equal(saved.btwCards?.b2, undefined)
+ assert.equal('btwCards' in saved, false)
assert.equal(saved.bots.b1.id, 'b1')
})
@@ -591,19 +576,21 @@ test('unloading the plugin aborts an aside that is still answering', async (t) =
await env.rt.btwJoin(card.id)
assert.equal(card.status, 'answering')
assert.equal(env.rt.state.revision, revision)
+ const file = cardsFile(env.home)
+ assert.equal(file[0].id, card.id)
+ assert.equal(file[0].status, 'answering')
const saved = JSON.parse(readFileSync(join(env.home, 'state.json'), 'utf8'))
- assert.equal(saved.btwCards.b1[0].id, card.id)
- assert.equal(saved.btwCards.b1[0].status, 'answering')
+ assert.equal('btwCards' in saved, false)
assert.equal(saved.revision, revision)
})
-test('a running aside stays cancellable after newer cards fill the cap', async (t) => {
+test('a running aside stays cancellable after newer cards arrive', async (t) => {
const env = await setup({ gate: true })
t.after(() => env.stop())
const held = await env.call('btw', { sessionId: 's-now', text: 'hold this' })
assert.equal(held.ok, true, held.error?.message)
const heldId = held.value.id
- for (let index = 0; index < BTW_CARDS_MAX; index += 1) {
+ for (let index = 0; index < 25; index += 1) {
const response = await env.call('btw', { sessionId: 's-now', text: `short ${index}` })
assert.equal(response.ok, true, response.error?.message)
const gate = env.gates.at(-1)
@@ -612,7 +599,7 @@ test('a running aside stays cancellable after newer cards fill the cap', async (
await env.rt.btwJoin(response.value.id)
}
const cards = cardsOf(env.rt)
- assert.equal(cards.length, BTW_CARDS_MAX)
+ assert.equal(cards.length, 26)
assert.equal(cards.at(-1).id, heldId)
assert.equal(cards.at(-1).status, 'answering')
assert.equal(env.requests[0].signal.aborted, false)
@@ -699,3 +686,45 @@ test('a log-only tail anchors the aside to the turn, not the tail', async (t) =>
assert.equal(cardsOf(mid.rt)[0].anchorSeq, 1)
assert.equal(cardsOf(mid.rt)[0].turn, 3)
})
+
+test('the team view carries only the current part\'s cards', async (t) => {
+ const env = await setup({
+ btw: {
+ b1: {
+ 's-old': [{ id: 'gone', botId: 'b1', partId: 's-old', atSeq: 1, question: 'From the old part?', answer: 'Old.', status: 'done', createdAt: 5, finishedAt: 6 }],
+ },
+ },
+ })
+ t.after(() => env.stop())
+ const response = await env.call('btw', { sessionId: 's-now', text: 'in the new part' })
+ await env.rt.btwJoin(response.value.id)
+ const cards = cardsOf(env.rt)
+ assert.equal(cards.length, 1)
+ assert.equal(cards[0].partId, 's-now')
+ const view = await env.call('state', {})
+ assert.equal(view.ok, true, view.error?.message)
+ assert.equal(view.value.btwCards.b1.length, 1)
+ assert.equal(view.value.btwCards.b1[0].partId, 's-now')
+ assert.equal(view.value.btwCards.b1.some(card => card.id === 'gone'), false)
+})
+
+test('an earlier part page carries that part\'s cards, read from its file', async (t) => {
+ const old = [
+ { id: 'there', botId: 'b1', partId: 's-old', atSeq: 1, question: 'From the old part?', answer: 'Old.', status: 'done', createdAt: 5, finishedAt: 6 },
+ { id: 'left', botId: 'b1', partId: 's-old', atSeq: 2, question: 'Never done?', answer: '', status: 'answering', createdAt: 7 },
+ ]
+ const env = await setup({ btw: { b1: { 's-old': old } } })
+ t.after(() => env.stop())
+ env.sessions.set('s-old', makeSession('s-old', { messages: [user('Old work')], header: env.header, seq: 2, toolHistory: [] }))
+ const view = await env.call('state', {})
+ assert.equal(view.value.btwCards.b1, undefined)
+ const page = await env.call('earlier', { sessionId: 's-now' })
+ assert.equal(page.ok, true, page.error?.message)
+ assert.equal(page.value.btw.length, 2)
+ assert.equal(page.value.btw[0].id, 'there')
+ // The owning host's lazy read still records the restart this file kept.
+ assert.equal(page.value.btw[1].status, 'failed')
+ assert.equal(page.value.btw[1].error, 'DS Bot restarted before this answer finished.')
+ const file = cardsFile(env.home, 'b1', 's-old')
+ assert.equal(file[1].status, 'failed')
+})