A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Send the live state fields only when they changed
Fei24110fb2124de830
diff --git a/ds-bot/client.js b/ds-bot/client.jsindex cc54a653..6626dc52 100644--- a/ds-bot/client.js+++ b/ds-bot/client.js@@ -20325,6 +20325,7 @@ function apply(ctx) {const activity = createSource({});const work = createSource({ bots: {}, approvals: [] });let workKey = "";+ let liveKey;const live = createSource({});const cards = createSource({});const heldCards = createSource({});@@ -20359,20 +20360,25 @@ function apply(ctx) {const pull = async () => {const order = ++pulls;const known = roster.getSnapshot();- const value = await call("state", { since: known.revision, models: known.modelsKey ?? "", locale: language() });+ const value = await call("state", { since: known.revision, models: known.modelsKey ?? "", live: liveKey, locale: language() });if (order < applied) return;applied = order;- const nextActivity = JSON.stringify(value.activity ?? {});- if (nextActivity !== activityKey) {- activityKey = nextActivity;- activity.set(value.activity ?? {});+ if (value.liveKey !== void 0) liveKey = value.liveKey;+ if (value.activity !== void 0) {+ const nextActivity = JSON.stringify(value.activity);+ if (nextActivity !== activityKey) {+ activityKey = nextActivity;+ activity.set(value.activity);+ }}- const nextWork = JSON.stringify(value.work ?? null);- if (nextWork !== workKey) {- workKey = nextWork;- work.set(value.work ?? { bots: {}, approvals: [] });+ if (value.work !== void 0) {+ const nextWork = JSON.stringify(value.work);+ if (nextWork !== workKey) {+ workKey = nextWork;+ work.set(value.work);+ }}- noteMemoryNews(value.memoryNews ?? {});+ if (value.memoryNews !== void 0) noteMemoryNews(value.memoryNews);const current = roster.getSnapshot();const live2 = {};if (value.connectors !== void 0 && JSON.stringify(value.connectors) !== JSON.stringify(current.connectors ?? [])) live2.connectors = value.connectors;diff --git a/ds-bot/src/client/index.js b/ds-bot/src/client/index.jsindex 31f66057..29ff931d 100644--- a/ds-bot/src/client/index.js+++ b/ds-bot/src/client/index.js@@ -117,6 +117,9 @@ export function apply(ctx) {// Per Bot, the turn it runs and its tool; and the approvals waiting for the user.const work = createSource({ bots: {}, approvals: [] })let workKey = ''+ // The version of the live fields this window last got; while it still names the+ // host's answer, the poll leaves those fields out.+ let liveKey// Per Session, what its transcript has in flight (running tools, a streaming reply).const live = createSource({})// Per Session, the ask_user and request_secret calls its transcript draws; the@@ -155,14 +158,19 @@ export function apply(ctx) {const pull = async () => {const order = ++pullsconst known = roster.getSnapshot()- const value = await call('state', { since: known.revision, models: known.modelsKey ?? '', locale: language() })+ const value = await call('state', { since: known.revision, models: known.modelsKey ?? '', live: liveKey, locale: language() })if (order < applied) returnapplied = order- const nextActivity = JSON.stringify(value.activity ?? {})- if (nextActivity !== activityKey) { activityKey = nextActivity; activity.set(value.activity ?? {}) }- const nextWork = JSON.stringify(value.work ?? null)- if (nextWork !== workKey) { workKey = nextWork; work.set(value.work ?? { bots: {}, approvals: [] }) }- noteMemoryNews(value.memoryNews ?? {})+ if (value.liveKey !== undefined) liveKey = value.liveKey+ if (value.activity !== undefined) {+ const nextActivity = JSON.stringify(value.activity)+ if (nextActivity !== activityKey) { activityKey = nextActivity; activity.set(value.activity) }+ }+ if (value.work !== undefined) {+ const nextWork = JSON.stringify(value.work)+ if (nextWork !== workKey) { workKey = nextWork; work.set(value.work) }+ }+ if (value.memoryNews !== undefined) noteMemoryNews(value.memoryNews)const current = roster.getSnapshot()// Connector and capability states change without a new revision.const live = {}diff --git a/ds-bot/src/host/channel.js b/ds-bot/src/host/channel.jsindex 3fc5c7c6..d3e5f37f 100644--- a/ds-bot/src/host/channel.js+++ b/ds-bot/src/host/channel.js@@ -97,6 +97,16 @@ export function install(rt) {connectors: rt.connectorsView(),capabilityStatuses: rt.capabilityStatusesView(),})+ // The live fields' sources are spread across the host with no single change+ // counter, so the serialized answer itself is the detector: a poll whose `live`+ // still names it gets only the key back, not the fields again.+ let liveCache = null+ const liveAnswer = () => {+ const value = live()+ const json = JSON.stringify(value)+ if (liveCache?.json === json) return liveCache+ return liveCache = { key: (liveCache?.key ?? 0) + 1, value, json }+ }// 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', 'providers', 'provider-models', 'earlier', 'chat', 'run-stats', 'room-progress', 'exchange', 'exchange-text', 'file-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', 'thinking', 'avatar-library'])@@ -147,13 +157,18 @@ export function install(rt) {const readOnly = rt.readOnly()if (readOnly !== undefined && !READS.has(endpoint)) return fail(readOnly, 'bot/read-only')switch (endpoint) {- case 'state':+ case 'state': {if (typeof body.locale === 'string' && /^[a-z]{2,3}(-[\w-]+)?$/i.test(body.locale)) uiLocale = body.localeif (readOnly === undefined) await rt.bootstrap()await rt.catalog()await rt.loadSecrets()- if (body.since === state.revision && (body.models === undefined || body.models === modelsKey())) return ok({ revision: state.revision, unchanged: true, ...live() })- return ok({ ...view(), ...live() })+ const answer = liveAnswer()+ if (body.since === state.revision && (body.models === undefined || body.models === modelsKey())) {+ if (body.live === answer.key) return ok({ revision: state.revision, unchanged: true, liveKey: answer.key })+ return ok({ revision: state.revision, unchanged: true, liveKey: answer.key, ...answer.value })+ }+ return ok({ ...view(), liveKey: answer.key, ...answer.value })+ }case 'models':await rt.catalog({ fresh: true })return ok({ modelsKey: modelsKey() })diff --git a/ds-bot/test/roster.test.js b/ds-bot/test/roster.test.jsindex e21e2af2..d82fe5a1 100644--- a/ds-bot/test/roster.test.js+++ b/ds-bot/test/roster.test.js@@ -280,6 +280,40 @@ test('the state reports a changed model list without a new revision', async (t)assert.equal((await call('state', { since: first.revision, models: 'stale' })).value.unchanged, undefined)})+test('an unchanged state answer skips the live fields a window already holds', async (t) => {+ const { call, stop, ctx } = start(undefined, { models: ['deepseek-v4-flash'] })+ t.after(stop)+ const first = (await call('state', {})).value+ assert.equal(typeof first.liveKey, 'number')+ // The window echoes the live key it holds; while it still names the answer,+ // the live fields stay out.+ const quiet = (await call('state', { since: first.revision, models: first.modelsKey, live: first.liveKey })).value+ assert.equal(quiet.unchanged, true)+ assert.equal(quiet.liveKey, first.liveKey)+ assert.equal(quiet.activity, undefined)+ assert.equal(quiet.work, undefined)+ assert.equal(quiet.connectors, undefined)+ // A key that names an older answer still gets the fields with the current one.+ const stale = (await call('state', { since: first.revision, models: first.modelsKey, live: first.liveKey - 1 })).value+ assert.equal(stale.unchanged, true)+ assert.equal(stale.liveKey, first.liveKey)+ assert.equal(typeof stale.activity, 'object')+ assert.equal(typeof stale.work, 'object')+ // Live state that changes without a new revision moves the key.+ const chief = first.bots.find(bot => bot.id === first.mainBotId)+ for (const listener of ctx.listeners.get('session/event') ?? []) {+ listener({ id: chief.sessionId ?? chief.id }, { type: 'turn/start', data: { turn: 1 }, time: Date.now() })+ }+ const working = (await call('state', { since: first.revision, models: first.modelsKey, live: first.liveKey })).value+ assert.equal(working.unchanged, true)+ assert.notEqual(working.liveKey, first.liveKey)+ assert.equal(Object.keys(working.work?.bots ?? {}).length, 1)+ // And once it holds that answer, the next poll is small again.+ const again = (await call('state', { since: first.revision, models: first.modelsKey, live: working.liveKey })).value+ assert.equal(again.unchanged, true)+ assert.equal(again.activity, undefined)+})+test('a new team starts with a DeepSeek-blue whale as its Main Bot', async (t) => {const { call, stop } = start()t.after(stop)