Skip to content

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

Keep current Sessions out of an archive made while the team runs

Fei2411d071f8c1fc45
diff --git a/ds-bot/src/host/parts.js b/ds-bot/src/host/parts.js
index 8d4ad299..1c2a71ba 100644
--- a/ds-bot/src/host/parts.js
+++ b/ds-bot/src/host/parts.js
@@ -1356,6 +1356,21 @@ export function install(rt) {
return () => clearTimeout(timer)
}, 'ds-bot: hand on input kept in earlier parts')
+ // An archive from DSH's own Session list while the team runs: DSH asks for the
+ // Session's activity (plain archive) or stops it (stopActivity) inside the archive,
+ // so an unarchive started here queues behind that archive and undoes it. The team's
+ // own archives drop the Session from its owner first.
+ const keepCurrent = ({ sessionId }) => {
+ if (readOnly() || !(rt.isBotSession(sessionId) || rt.roomOwners.has(sessionId))) return
+ void ctx.workspaceRegistry.unarchiveSession(sessionId)
+ .catch(error => rt.warn('ds-bot: current Session %s is archived in DSH and could not be taken out: %s', sessionId, error))
+ }
+ ctx.on('workspace/session-stop', keepCurrent)
+ ctx.on('workspace/session-activity', (request, next) => {
+ keepCurrent(request)
+ return next()
+ })
+
ctx.on('agent/status', ({ agent, status }) => {
if (status === 'idle' && rt.isBotSession(agent.id)) {
releaseRescued(agent)
@@ -1594,15 +1609,19 @@ export function install(rt) {
rt.chatOwners.set(partId, botId)
rt.pastOwners.delete(partId)
rt.pastOwners.set(current, { botId })
+ // Marked as a switch until its tasks sit on the opened part and it is archived, so
+ // the restart pass (handOnHeld) finishes a move that fails here.
+ state.switching[current] = { botId }
await rt.applyModel(bot, { force: true }).catch(error => rt.warn('ds-bot: model for %s not applied: %s', bot.name, error))
const room = undefined
await rt.applyPermission(bot, partId, room).catch(() => {})
await rt.save()
- // The replaced part is archived like any earlier part, once its tasks sit on the
- // opened one: the stop that archiving runs removes the tasks still on it.
+ // The stop that archiving runs removes the tasks still on the part, so they move first.
try {
await rt.moveSchedules?.(current, partId)
await ctx.workspaceRegistry.archiveSession(current, { stopActivity: true })
+ delete state.switching[current]
+ await rt.save()
} catch (error) {
rt.warn('ds-bot: earlier part %s not archived: %s', current, error)
}
diff --git a/ds-bot/test/archived-parts.test.js b/ds-bot/test/archived-parts.test.js
index 2a23d38b..490fd619 100644
--- a/ds-bot/test/archived-parts.test.js
+++ b/ds-bot/test/archived-parts.test.js
@@ -17,8 +17,9 @@ import { fakeContext } from './fake-ctx.js'
const SessionPersistence = typeof Jsonl.apply === 'function' ? Jsonl : Jsonl.default
// DSH's own Workspace registry, which holds the archive, on files in a temporary folder.
-// The listed Sessions count as live, so they can be archived.
-async function dshRegistry(t, root, live) {
+// The listed Sessions count as live, so they can be archived. The plugin's archive hooks
+// on `plugin` (a fake context) run inside an archive as DSH runs them.
+async function dshRegistry(t, root, live, plugin) {
const host = new Context()
t.after(() => host.fiber.dispose())
await host.plugin(Storage)
@@ -26,6 +27,14 @@ async function dshRegistry(t, root, live) {
await host.plugin(StorageDomain, { backend: 'json' })
await host.plugin(SessionPersistence, { root: join(root, 'sessions') })
await host.plugin({ name: 'live-sessions', apply: inner => inner.provide('sessions', { list: () => [], get: id => (live.includes(id) ? {} : undefined) }) })
+ await host.plugin({
+ name: 'plugin-hooks',
+ apply: (inner) => {
+ const hooks = name => plugin.listeners.get(name) ?? []
+ inner.on('workspace/session-stop', payload => Promise.all(hooks('workspace/session-stop').map(listener => listener(payload))))
+ inner.on('workspace/session-activity', (payload, next) => hooks('workspace/session-activity').reduceRight((rest, listener) => () => listener(payload, rest), next)())
+ },
+ })
await host.plugin(WorkspaceRegistry)
return new Promise((resolve, reject) => {
const timer = setTimeout(() => reject(new Error('the Workspace registry did not start')), 5000)
@@ -55,16 +64,16 @@ const TEAM = {
async function team(t, archived) {
const home = mkdtempSync(join(tmpdir(), 'dsb-archived-'))
writeFileSync(join(home, 'state.json'), JSON.stringify(TEAM))
- const registry = await dshRegistry(t, join(home, 'dsh'), ['m1', 'w1', 'w2', 'g1', 'gw', 'outside'])
- for (const id of archived) await registry.archiveSession(SessionId(id), { stopActivity: true })
const ctx = fakeContext({})
+ const registry = await dshRegistry(t, join(home, 'dsh'), ['m1', 'w1', 'w2', 'g1', 'gw', 'outside'], ctx)
+ for (const id of archived) await registry.archiveSession(SessionId(id), { stopActivity: true })
ctx.workspaceRegistry = registry
const register = ctx.connection.fetch.register
ctx.connection.fetch.register = (route) => { ctx.route = route; return register(route) }
- createRuntime(ctx, { home })
+ const rt = createRuntime(ctx, { home })
t.after(async () => { await ctx.dispose(); rmSync(home, { recursive: true, force: true, maxRetries: 5 }) })
const call = async (endpoint, payload = {}) => (await ctx.route.fetch(new Request('http://localhost/api/bot', { method: 'POST', body: JSON.stringify({ endpoint, payload }) }))).json()
- return { home, registry, call }
+ return { home, registry, call, rt }
}
const settled = async (check) => {
@@ -89,3 +98,25 @@ test('current team Sessions held archived in DSH are taken back out; earlier par
await settled(() => registry.archivedSessionIds.length === 2)
assert.deepEqual([...registry.archivedSessionIds].sort(), ['outside', 'w1'])
})
+
+test('a current Session archived in DSH while the team runs is taken back out', async (t) => {
+ const { registry, call } = await team(t, [])
+ await call('state')
+ await registry.archiveSession(SessionId('w2'), { stopActivity: true })
+ await registry.archiveSession(SessionId('gw'))
+ await registry.archiveSession(SessionId('outside'))
+ await settled(() => registry.archivedSessionIds.length === 1)
+ assert.deepEqual([...registry.archivedSessionIds], ['outside'])
+})
+
+test('a failed task move keeps the replaced part marked for the restart pass', async (t) => {
+ const { home, registry, call, rt } = await team(t, ['w1'])
+ await call('state')
+ rt.moveSchedules = async () => { throw new Error('schedule store unavailable') }
+ const result = await call('bot-open-part', { botId: 'w1', sessionId: 'w1' })
+ assert.equal(result.ok, true, JSON.stringify(result))
+ assert.deepEqual([...registry.archivedSessionIds], [])
+ const saved = JSON.parse(readFileSync(join(home, 'state.json'), 'utf8'))
+ assert.equal(saved.bots.w1.sessionId, 'w1')
+ assert.deepEqual(saved.switching, { w2: { botId: 'w1' } })
+})
diff --git a/ds-bot/test/install.test.js b/ds-bot/test/install.test.js
index 60e2a726..5466f721 100644
--- a/ds-bot/test/install.test.js
+++ b/ds-bot/test/install.test.js
@@ -79,6 +79,8 @@ test('apply registers listeners and effects in a fixed order', (t) => {
'on agent/request-error (prepend)',
'on agent/request-error (prepend)',
'effect ds-bot: hand on input kept in earlier parts',
+ 'on workspace/session-stop',
+ 'on workspace/session-activity',
'on agent/status',
'on llm/stream',
'on llm/stream',