Skip to content

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

Preserve review-time turns and place cards at turn tails

Fei2411067824d45e55
diff --git a/ds-bot/client.js b/ds-bot/client.js
index 3883548c..157dbd40 100644
--- a/ds-bot/client.js
+++ b/ds-bot/client.js
@@ -18958,6 +18958,21 @@ function serveBrowserReads({ browsers, post, clientId = globalThis.crypto.random
};
}
+// src/client/learning-card-model.js
+function proposalTurn(proposal, turns) {
+ if (!Number.isFinite(proposal?.at) || typeof turns?.values !== "function") return void 0;
+ let latest;
+ let before;
+ for (const location2 of turns.values()) {
+ const end = location2?.end?.time;
+ if (location2?.status !== "closed" || !Number.isFinite(end)) continue;
+ const later = (target) => target === void 0 || end > target.end.time || end === target.end.time && location2.turn > target.turn;
+ if (later(latest)) latest = location2;
+ if (end <= proposal.at && later(before)) before = location2;
+ }
+ return (before ?? latest)?.turn;
+}
+
// src/client/learning-card.js
var import_react46 = require("react");
function ProposalCard({ proposal, initialError = "", onFailure, onResolved, actions }) {
@@ -18981,7 +18996,7 @@ function ProposalCard({ proposal, initialError = "", onFailure, onResolved, acti
};
return (0, import_react46.createElement)(
"section",
- { className: "bt-q bt-secret bt-learning-card", role: "group", "aria-labelledby": titleId, "aria-busy": busy },
+ { className: "bt-q bt-learning-card", role: "group", "aria-labelledby": titleId, "aria-busy": busy },
(0, import_react46.createElement)(
"div",
{ className: "bt-q-head" },
@@ -18989,12 +19004,12 @@ function ProposalCard({ proposal, initialError = "", onFailure, onResolved, acti
"div",
{ className: "bt-q-text" },
(0, import_react46.createElement)("p", { className: "bt-q-title", id: titleId }, t(proposal.action === "update" ? "Skill update" : "New skill")),
- (0, import_react46.createElement)("div", { className: "bt-q-detail" }, (0, import_react46.createElement)("code", { className: "bt-secret-name" }, proposal.name))
+ (0, import_react46.createElement)("div", { className: "bt-q-detail" }, (0, import_react46.createElement)("code", null, proposal.name))
)
),
(0, import_react46.createElement)(
"div",
- { className: "bt-secret-body" },
+ { className: "bt-learning-body" },
(0, import_react46.createElement)("p", { className: "bt-learning-description" }, proposal.description),
proposal.why ? (0, import_react46.createElement)("p", { className: "bt-learning-why" }, proposal.why) : null,
(0, import_react46.createElement)(
@@ -19004,7 +19019,7 @@ function ProposalCard({ proposal, initialError = "", onFailure, onResolved, acti
(0, import_react46.createElement)("pre", null, proposal.content)
)
),
- error ? (0, import_react46.createElement)("div", { className: "bt-secret-error", role: "alert" }, error) : null,
+ error ? (0, import_react46.createElement)("div", { className: "bt-learning-error", role: "alert" }, error) : null,
(0, import_react46.createElement)(
"div",
{ className: "bt-q-foot" },
@@ -19013,8 +19028,9 @@ function ProposalCard({ proposal, initialError = "", onFailure, onResolved, acti
)
);
}
-function LearningDock({ sessionId, useRoster, actions }) {
+function LearningTurnTail({ turn, sessionId, useRoster, actions }) {
const roster = useRoster((value) => value);
+ const facts = useTurnFacts(sessionId, actions);
const [failed, setFailed] = (0, import_react46.useState)({});
const bot = roster.byId?.[sessionId];
if (!bot || currentPart(bot) !== sessionId) return null;
@@ -19023,7 +19039,8 @@ function LearningDock({ sessionId, useRoster, actions }) {
for (const entry of Object.values(failed)) {
if (entry.proposal.botId === bot.id && !proposals.some((proposal) => proposal.id === entry.proposal.id)) visible.push(entry.proposal);
}
- if (visible.length === 0) return null;
+ const cards = visible.filter((proposal) => proposalTurn(proposal, facts?.turns) === turn?.turn);
+ if (cards.length === 0) return null;
const onFailure = (proposal, error) => setFailed((current) => ({ ...current, [proposal.id]: { proposal, error } }));
const onResolved = (id) => setFailed((current) => {
if (!(id in current)) return current;
@@ -19031,25 +19048,27 @@ function LearningDock({ sessionId, useRoster, actions }) {
delete next[id];
return next;
});
- return (0, import_react46.createElement)(
+ return (0, import_react46.createElement)("div", { className: "bt-learning-stack" }, cards.map((proposal) => (0, import_react46.createElement)(
"div",
- { className: "bt-q-dock bt-secret-dock bt-learning-dock" },
- visible.map((proposal) => (0, import_react46.createElement)(ProposalCard, {
- key: proposal.id,
+ { className: "bt-q-inline", key: proposal.id },
+ (0, import_react46.createElement)(ProposalCard, {
proposal,
initialError: failed[proposal.id]?.error,
onFailure,
onResolved,
actions
- }))
- );
+ })
+ )));
}
var LEARNING_CSS = `
+.bt-learning-stack{display:flex;flex-direction:column;align-items:flex-start;gap:4px}
+.bt-learning-body{display:flex;flex-direction:column;gap:8px}
.bt-learning-description{margin:0;font-size:13px;line-height:18px;color:var(--bt-ink)}
.bt-learning-why{margin:0;font-size:12px;line-height:16px;color:var(--bt-ink-2)}
.bt-learning-preview{font-size:12px;line-height:16px;color:var(--bt-ink-2)}
.bt-learning-preview summary{width:fit-content;cursor:pointer}
.bt-learning-preview pre{max-height:220px;overflow:auto;margin:6px 0 0;padding:8px;border:1px solid var(--bt-line);border-radius:8px;background:var(--bt-card);color:var(--bt-ink);font-family:ui-monospace,monospace;font-size:12px;line-height:16px;white-space:pre-wrap;overflow-wrap:anywhere}
+.bt-learning-error{font-size:12px;line-height:16px;color:var(--bt-danger)}
`;
// src/client/right-panel.js
@@ -20063,6 +20082,7 @@ function apply(ctx) {
// A side question is not a transcript row. After its snapshot turn closes it
// sits on that turn's tail, ahead of the time separator.
() => shadow("conversation.chat.turnTail", { id: "bot-btw", order: 800, priority: BOT_PRIORITY, inject: face }, BtwTurnTail),
+ () => shadow("conversation.chat.turnTail", { id: "bot-learning", order: 850, priority: BOT_PRIORITY, inject: face }, LearningTurnTail),
// A Bot condenses its conversation without telling anyone; the chat keeps showing
// every message.
() => shadow("conversation.chat.node", { key: "question-reply", priority: BOT_PRIORITY, locale: "chat" }, AnsweredReply),
@@ -20090,7 +20110,6 @@ function apply(ctx) {
() => shadow("conversation.input.dock", { id: "bot-question", order: -100, inject: face }, QuestionDock),
() => shadow("conversation.input.dock", { id: "bot-view", order: -95, inject: face }, WidgetDock),
() => shadow("conversation.input.dock", { id: "bot-secret", order: -90, inject: face }, SecretDock),
- () => shadow("conversation.input.dock", { id: "bot-learning", order: -80, inject: face }, LearningDock),
() => shadow("conversation.input.dock", { id: "bot-handoff", order: -85, inject: face }, HandoffDock)
];
ctx.effect(() => installCompiledStyles(RETURN_CSS, "return"), "ds-bot: return styles");
diff --git a/ds-bot/src/client/btw.js b/ds-bot/src/client/btw.js
index c8f119d2..c2b80850 100644
--- a/ds-bot/src/client/btw.js
+++ b/ds-bot/src/client/btw.js
@@ -70,7 +70,7 @@ export function BtwCardView({ card, compact = false, sessionId, actions, onDelet
answer !== '' ? h('div', { className: 'bt-btw-answer' }, h(MarkdownText, { text: answer, labels: mdLabels(), variant: 'compact' })) : null) : null)
}
-function useTurnFacts(sessionId, actions) {
+export function useTurnFacts(sessionId, actions) {
const [facts, setFacts] = useState(() => actions.turnFacts(sessionId))
useEffect(() => {
let live = true
diff --git a/ds-bot/src/client/index.js b/ds-bot/src/client/index.js
index 0563fb87..c05c883b 100644
--- a/ds-bot/src/client/index.js
+++ b/ds-bot/src/client/index.js
@@ -24,7 +24,7 @@ import { Overlays } from './overlays.js'
import { createBotBrowsers, serveBrowserReads } from './bot-browsers.js'
import { BROWSER_CSS } from './browser-pane.js'
import { SECRET_CSS, SecretDock } from './secret-card.js'
-import { LearningDock, LEARNING_CSS } from './learning-card.js'
+import { LearningTurnTail, LEARNING_CSS } from './learning-card.js'
import { MODEL_CSS, ModelChoiceDock, DeepSeekSignIn } from './model-menu.js'
import { BOT_SETTINGS_CSS } from './bot-settings.js'
import { AVATAR_LIBRARY_CSS } from './avatar-library.js'
@@ -833,6 +833,7 @@ export function apply(ctx) {
// A side question is not a transcript row. After its snapshot turn closes it
// sits on that turn's tail, ahead of the time separator.
() => shadow('conversation.chat.turnTail', { id: 'bot-btw', order: 800, priority: BOT_PRIORITY, inject: face }, BtwTurnTail),
+ () => shadow('conversation.chat.turnTail', { id: 'bot-learning', order: 850, priority: BOT_PRIORITY, inject: face }, LearningTurnTail),
// A Bot condenses its conversation without telling anyone; the chat keeps showing
// every message.
() => shadow('conversation.chat.node', { key: 'question-reply', priority: BOT_PRIORITY, locale: 'chat' }, AnsweredReply),
@@ -860,7 +861,6 @@ export function apply(ctx) {
() => shadow('conversation.input.dock', { id: 'bot-question', order: -100, inject: face }, QuestionDock),
() => shadow('conversation.input.dock', { id: 'bot-view', order: -95, inject: face }, WidgetDock),
() => shadow('conversation.input.dock', { id: 'bot-secret', order: -90, inject: face }, SecretDock),
- () => shadow('conversation.input.dock', { id: 'bot-learning', order: -80, inject: face }, LearningDock),
() => shadow('conversation.input.dock', { id: 'bot-handoff', order: -85, inject: face }, HandoffDock),
]
// The return button belongs to neither group: it renders only in Agent mode.
diff --git a/ds-bot/src/client/learning-card-model.js b/ds-bot/src/client/learning-card-model.js
new file mode 100644
index 00000000..bec6f7e0
--- /dev/null
+++ b/ds-bot/src/client/learning-card-model.js
@@ -0,0 +1,13 @@
+export function proposalTurn(proposal, turns) {
+ if (!Number.isFinite(proposal?.at) || typeof turns?.values !== 'function') return undefined
+ let latest
+ let before
+ for (const location of turns.values()) {
+ const end = location?.end?.time
+ if (location?.status !== 'closed' || !Number.isFinite(end)) continue
+ const later = target => target === undefined || end > target.end.time || (end === target.end.time && location.turn > target.turn)
+ if (later(latest)) latest = location
+ if (end <= proposal.at && later(before)) before = location
+ }
+ return (before ?? latest)?.turn
+}
diff --git a/ds-bot/src/client/learning-card.js b/ds-bot/src/client/learning-card.js
index 14d9098f..2da27922 100644
--- a/ds-bot/src/client/learning-card.js
+++ b/ds-bot/src/client/learning-card.js
@@ -1,4 +1,6 @@
import { createElement as h, useId, useRef, useState } from 'react'
+import { useTurnFacts } from './btw.js'
+import { proposalTurn } from './learning-card-model.js'
import { currentPart } from './text.js'
import { t } from './i18n.js'
@@ -23,25 +25,26 @@ function ProposalCard({ proposal, initialError = '', onFailure, onResolved, acti
setBusy(false)
})
}
- return h('section', { className: 'bt-q bt-secret bt-learning-card', role: 'group', 'aria-labelledby': titleId, 'aria-busy': busy },
+ return h('section', { className: 'bt-q bt-learning-card', role: 'group', 'aria-labelledby': titleId, 'aria-busy': busy },
h('div', { className: 'bt-q-head' },
h('div', { className: 'bt-q-text' },
h('p', { className: 'bt-q-title', id: titleId }, t(proposal.action === 'update' ? 'Skill update' : 'New skill')),
- h('div', { className: 'bt-q-detail' }, h('code', { className: 'bt-secret-name' }, proposal.name)))),
- h('div', { className: 'bt-secret-body' },
+ h('div', { className: 'bt-q-detail' }, h('code', null, proposal.name)))),
+ h('div', { className: 'bt-learning-body' },
h('p', { className: 'bt-learning-description' }, proposal.description),
proposal.why ? h('p', { className: 'bt-learning-why' }, proposal.why) : null,
h('details', { className: 'bt-learning-preview' },
h('summary', null, t('Preview')),
h('pre', null, proposal.content))),
- error ? h('div', { className: 'bt-secret-error', role: 'alert' }, error) : null,
+ error ? h('div', { className: 'bt-learning-error', role: 'alert' }, error) : null,
h('div', { className: 'bt-q-foot' },
h('button', { type: 'button', className: 'bt-q-btn', disabled: busy, onClick: () => answer(false) }, t('Decline')),
h('button', { type: 'button', className: 'bt-q-submit', disabled: busy, onClick: () => answer(true) }, t('Approve'))))
}
-export function LearningDock({ sessionId, useRoster, actions }) {
+export function LearningTurnTail({ turn, sessionId, useRoster, actions }) {
const roster = useRoster(value => value)
+ const facts = useTurnFacts(sessionId, actions)
const [failed, setFailed] = useState({})
const bot = roster.byId?.[sessionId]
if (!bot || currentPart(bot) !== sessionId) return null
@@ -50,7 +53,8 @@ export function LearningDock({ sessionId, useRoster, actions }) {
for (const entry of Object.values(failed)) {
if (entry.proposal.botId === bot.id && !proposals.some(proposal => proposal.id === entry.proposal.id)) visible.push(entry.proposal)
}
- if (visible.length === 0) return null
+ const cards = visible.filter(proposal => proposalTurn(proposal, facts?.turns) === turn?.turn)
+ if (cards.length === 0) return null
const onFailure = (proposal, error) => setFailed(current => ({ ...current, [proposal.id]: { proposal, error } }))
const onResolved = id => setFailed(current => {
if (!(id in current)) return current
@@ -58,21 +62,23 @@ export function LearningDock({ sessionId, useRoster, actions }) {
delete next[id]
return next
})
- return h('div', { className: 'bt-q-dock bt-secret-dock bt-learning-dock' },
- visible.map(proposal => h(ProposalCard, {
- key: proposal.id,
+ return h('div', { className: 'bt-learning-stack' }, cards.map(proposal => h('div', { className: 'bt-q-inline', key: proposal.id },
+ h(ProposalCard, {
proposal,
initialError: failed[proposal.id]?.error,
onFailure,
onResolved,
actions,
- })))
+ }))))
}
export const LEARNING_CSS = `
+.bt-learning-stack{display:flex;flex-direction:column;align-items:flex-start;gap:4px}
+.bt-learning-body{display:flex;flex-direction:column;gap:8px}
.bt-learning-description{margin:0;font-size:13px;line-height:18px;color:var(--bt-ink)}
.bt-learning-why{margin:0;font-size:12px;line-height:16px;color:var(--bt-ink-2)}
.bt-learning-preview{font-size:12px;line-height:16px;color:var(--bt-ink-2)}
.bt-learning-preview summary{width:fit-content;cursor:pointer}
.bt-learning-preview pre{max-height:220px;overflow:auto;margin:6px 0 0;padding:8px;border:1px solid var(--bt-line);border-radius:8px;background:var(--bt-card);color:var(--bt-ink);font-family:ui-monospace,monospace;font-size:12px;line-height:16px;white-space:pre-wrap;overflow-wrap:anywhere}
+.bt-learning-error{font-size:12px;line-height:16px;color:var(--bt-danger)}
`
diff --git a/ds-bot/src/host/learning.js b/ds-bot/src/host/learning.js
index f1662d28..a13b1f7d 100644
--- a/ds-bot/src/host/learning.js
+++ b/ds-bot/src/host/learning.js
@@ -175,6 +175,7 @@ export function install(rt) {
// A turn in a Bot's own chat that ran long marks the chat for the next pass.
const started = new Map()
+ const noted = new Map()
let lastActivity = Date.now()
ctx.on('agent/status', ({ agent, status }) => {
const id = agent?.id
@@ -190,6 +191,7 @@ export function install(rt) {
if (since === undefined || botId === undefined || Date.now() - since < settings.minTurnMs) return
const pending = data().pending
pending[botId] = [...new Set([...(pending[botId] ?? []), id])]
+ noted.set(id, (noted.get(id) ?? 0) + 1)
void persist()
})
@@ -280,13 +282,15 @@ export function install(rt) {
const store = data()
store.lastRun = Date.now()
let made = 0
- for (const [botId, sessions] of Object.entries(store.pending)) {
+ for (const [botId, pendingSessions] of Object.entries(store.pending)) {
const bot = rt.botOf(botId)
if (bot === undefined) {
delete store.pending[botId]
continue
}
if (waitingFor(botId).length > 0) continue
+ const sessions = [...pendingSessions]
+ const notedAtStart = new Map(sessions.map(sessionId => [sessionId, noted.get(sessionId)]))
try {
const read = []
for (const sessionId of sessions) read.push({ sessionId, ...await linesSince(sessionId) })
@@ -297,7 +301,10 @@ export function install(rt) {
made += 1
}
for (const entry of read) if (entry.through !== undefined) store.cursors[entry.sessionId] = entry.through
- delete store.pending[botId]
+ const remaining = (store.pending[botId] ?? []).filter(sessionId =>
+ !notedAtStart.has(sessionId) || noted.get(sessionId) !== notedAtStart.get(sessionId))
+ if (remaining.length === 0) delete store.pending[botId]
+ else store.pending[botId] = remaining
} catch (error) {
rt.warn('ds-bot: the skill review of %s failed: %s', bot.name, error?.message ?? error)
continue
diff --git a/ds-bot/test/learning-card.test.js b/ds-bot/test/learning-card.test.js
new file mode 100644
index 00000000..b7887470
--- /dev/null
+++ b/ds-bot/test/learning-card.test.js
@@ -0,0 +1,18 @@
+import assert from 'node:assert/strict'
+import { test } from 'node:test'
+import { proposalTurn } from '../src/client/learning-card-model.js'
+
+const turns = new Map([
+ [1, { turn: 1, end: { time: 100 }, status: 'closed' }],
+ [2, { turn: 2, end: { time: 200 }, status: 'closed' }],
+ [3, { turn: 3, status: 'open' }],
+])
+
+test('a proposal belongs to the latest closed turn ending before its timestamp', () => {
+ assert.equal(proposalTurn({ at: 150 }, turns), 1)
+ assert.equal(proposalTurn({ at: 200 }, turns), 2)
+})
+
+test('a proposal without an earlier closed turn falls back to the latest closed turn', () => {
+ assert.equal(proposalTurn({ at: 50 }, turns), 2)
+})
diff --git a/ds-bot/test/learning.test.js b/ds-bot/test/learning.test.js
index 27a9347e..79cb2fa0 100644
--- a/ds-bot/test/learning.test.js
+++ b/ds-bot/test/learning.test.js
@@ -196,6 +196,62 @@ test('a none reply makes no proposal and advances the chat cursor', async t => {
assert.deepEqual((await runtime.call('state')).value.learning ?? {}, {})
})
+test('long turns ending during review remain pending for the next pass', async t => {
+ const history = { b2: [...ownChat], b3: [...ownChat] }
+ const runtime = await startRuntime({
+ history,
+ replies: [noneReply, noneReply],
+ config: { learningTimes: { tickMs: 100_000 } },
+ })
+ t.after(() => runtime.stop())
+ const load = runtime.rt.load
+ runtime.rt.load = async (...args) => {
+ const result = await load(...args)
+ runtime.rt.chatOwners.set('b3', 'b2')
+ return result
+ }
+ runtime.rt.chatOwners.set('b3', 'b2')
+ const b3Statuses = []
+ runtime.ctx.on('agent/status', ({ agent, status }) => {
+ if (agent?.id === 'b3') b3Statuses.push(status)
+ })
+ assert.equal(runtime.rt.chatOwners.get('b3'), 'b2')
+ assert.equal(runtime.rt.isBotSession('b3'), true)
+ await finishTurn(runtime.ctx)
+ await until(() => runtime.learningState().pending?.b2?.includes('b2'))
+
+ const botShot = runtime.rt.botShot
+ let endedDuringReview = false
+ runtime.rt.botShot = async (...args) => {
+ if (!endedDuringReview) {
+ endedDuringReview = true
+ addNextTurn(history)
+ history.b3.push(
+ { event: { seq: 12, type: 'user/message', data: { source: { kind: 'user' }, content: [{ type: 'text', text: 'Please keep this new work too.' }] } } },
+ { event: { seq: 13, type: 'assistant/message', data: { message: { content: [{ type: 'text', text: 'The second chat has new work.' }] } } } },
+ )
+ await finishTurn(runtime.ctx, { id: 'b2' })
+ await finishTurn(runtime.ctx, { id: 'b3' })
+ assert.deepEqual(b3Statuses, ['running', 'idle'])
+ await until(() => runtime.learningState().pending?.b2?.includes('b3'))
+ }
+ return botShot(...args)
+ }
+
+ await runtime.rt.learningRun()
+
+ assert.deepEqual(runtime.learningState().pending.b2, ['b2', 'b3'])
+ assert.equal(runtime.learningState().cursors.b2, 11)
+
+ await runtime.rt.learningRun()
+
+ assert.equal(runtime.requests.length, 2)
+ const request = runtime.requests[1].messages[0].content[0].text
+ assert.match(request, /Please check the release process too\./)
+ assert.match(request, /Please keep this new work too\./)
+ assert.equal(runtime.learningState().pending.b2, undefined)
+})
+
test('approval ownership survives a normal skill edit and remains learned on review', async t => {
const history = { b2: [...ownChat] }
const updateReply = JSON.stringify({