A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Seal cold Sessions in the usage scan and summarize unread ones
Fei24117bc183f2d19e
diff --git a/ds-bot/src/host/usage.js b/ds-bot/src/host/usage.jsindex d19c0f8c..fd07b9f1 100644--- a/ds-bot/src/host/usage.js+++ b/ds-bot/src/host/usage.js@@ -333,7 +333,9 @@ export function install(rt) {async function scanSession(ledger, address, asOfSeq, sealable, signal) {const sessionId = address.kind === 'session' ? address.sessionId : address.childSessionIdconst mark = ledger.mark(sessionId)- if (mark?.sealed) return+ // A sealed Session is skipped only while it still cannot grow: one sealed while+ // cold can resume, so a warm listing clears the seal by reading it again.+ if (mark?.sealed && sealable) returnlet through = asOfSeqif (through === undefined) {const projections = await ctx.sessionController.projections({ sessionId }, signal)@@ -410,22 +412,41 @@ export function install(rt) {// the persisted cache's own watermark, so it must not cut a page. The// Session's exact cursor is read below when no hint is usable.const listedCursor = item => (item.projections?.kind === 'sequenced' ? item.projections.asOfSeq : undefined)+ // A Session with no Agent attached is cold: its log cannot grow right now, so+ // once it is read to its tip it is as finished as an ended part and seals.+ const cold = new Set(listed.filter(item => item.agentAvailable !== true && item.running !== true).map(item => item.sessionId))const targets = new Map()for (const item of listed) if (item.blank !== true) targets.set(item.sessionId, listedCursor(item))for (const sessionId of owners.keys()) if (!targets.has(sessionId)) targets.set(sessionId, undefined)const addresses = new Map(listed.map(item => [item.sessionId, addressOf(item)]))+ let reached = 0+ const failed = []await eachLimited([...targets], SCAN_PARALLEL, async ([sessionId, asOfSeq]) => {+ if (signal.aborted) return+ reached += 1const address = addresses.has(sessionId) ? addresses.get(sessionId) : { kind: 'session', sessionId }if (address === undefined) returntry {- await scanSession(ledger, address, asOfSeq, ended.has(sessionId), signal)+ await scanSession(ledger, address, asOfSeq, ended.has(sessionId) || cold.has(sessionId), signal)unreadable.delete(sessionId)} catch (error) {- if (unreadable.has(sessionId)) return- unreadable.add(sessionId)- rt.warn('ds-bot: usage could not read %s: %s', sessionId, error)+ // The scan timeout cancels the reads still open; those Sessions are not+ // broken, so they are counted below but not remembered as unreadable.+ if (!signal.aborted) {+ if (unreadable.has(sessionId)) return+ unreadable.add(sessionId)+ }+ failed.push([sessionId, error])}})+ // One line for the whole scan, not one per Session it could not read.+ const missed = failed.length + (signal.aborted ? targets.size - reached : 0)+ if (missed > 0) {+ const first = failed[0]+ rt.warn('ds-bot: usage could not read %d Session%s%s%s', missed, missed === 1 ? '' : 's',+ signal.aborted ? ' before the scan ran out of time' : '',+ first !== undefined ? ` (first: ${first[0]}: ${first[1]})` : '')+ }})().finally(() => {scannedAt = Date.now()scanning = undefineddiff --git a/ds-bot/test/usage.test.js b/ds-bot/test/usage.test.jsindex da36a5a6..d811fd97 100644--- a/ds-bot/test/usage.test.js+++ b/ds-bot/test/usage.test.js@@ -207,6 +207,7 @@ test('a subagent Session is read through its parent, even a parent that is gone;{ sessionId: 's-orphan', parentSessionId: 's-gone', origin: 'subagent', projections: { kind: 'cached', asOfSeq: 1, values: {} } },{ sessionId: 's-lost', origin: 'subagent', projections: { kind: 'cached', asOfSeq: 1, values: {} } },{ sessionId: 's-broken', projections: { kind: 'sequenced', asOfSeq: 1, values: {} } },+ { sessionId: 's-broken2', projections: { kind: 'sequenced', asOfSeq: 1, values: {} } },],logsExtra: {'s-orphan': [header('deepseek', 'v4-flash'), message(1, 0, usage(8, 1), HOUR)],@@ -220,16 +221,55 @@ test('a subagent Session is read through its parent, even a parent that is gone;assert.equal(totals(view)['other/chat'], 44 + 9)// No parent on record: it cannot be addressed, so it is not asked for.assert.ok(!pages.includes('s-lost'))+ // A scan reports one summarized line, not one per Session it could not read.const unread = () => warnings.map(([template, ...args]) => template === '%s' ? args[0] : template).filter(line => line.includes('usage could not read'))- .map(line => line.match(/usage could not read (\S+):/)?.[1])- assert.deepEqual(unread(), ['s-broken'])+ assert.equal(unread().length, 1)+ assert.match(unread()[0], /usage could not read 2 Sessions \(first: s-broken/)t.mock.timers.tick(60_000)await call('usage', { timeZone: 'Asia/Shanghai' })assert.equal(pages.filter(sessionId => sessionId === 's-orphan').length, 1)- assert.deepEqual(unread(), ['s-broken'])+ assert.equal(unread().length, 1)+})++// #654: a Session with no Agent attached cannot grow, so once read to its tip it+// seals and later scans skip it without reading it again — until it comes back.+test('a cold Session seals once read and is read again only once it warms', async (t) => {+ t.mock.timers.enable({ apis: ['Date'], now: 10 * HOUR })+ const { call, ctx, stop } = start(undefined, {+ listedExtra: [{ sessionId: 's-x', projections: { kind: 'cached', asOfSeq: 1, values: {} } }],+ })+ t.after(stop)+ let reads = 0+ const projections = ctx.sessionController.projections+ ctx.sessionController.projections = async (request) => {+ if (request.sessionId === 's-x') reads += 1+ return projections(request)+ }+ const relist = agentAvailable => {+ ctx.sessionController.list = async () => ({+ items: [{ sessionId: 's-x', agentAvailable, running: false, projections: { kind: 'cached', asOfSeq: 1, values: {} } }],+ })+ }+ // First scan: cold but never read, so read once and sealed.+ await call('usage', { timeZone: 'Asia/Shanghai' })+ assert.equal(reads, 1)+ // Still cold: the sealed mark skips it without any read.+ t.mock.timers.tick(60_000)+ await call('usage', { timeZone: 'Asia/Shanghai' })+ assert.equal(reads, 1)+ // Listed warm again, the seal no longer skips it.+ relist(true)+ t.mock.timers.tick(60_000)+ await call('usage', { timeZone: 'Asia/Shanghai' })+ assert.equal(reads, 2)+ // Cold once more, it is sealed at the same tip and skipped again.+ relist(false)+ t.mock.timers.tick(60_000)+ await call('usage', { timeZone: 'Asia/Shanghai' })+ assert.equal(reads, 2)})test('a cached projection watermark beyond the log is not used as a page cursor', async (t) => {