Skip to content

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

Stop using a cached projection watermark as a usage cursor

chimney3b0e84a0e051
diff --git a/ds-bot/src/host/usage.js b/ds-bot/src/host/usage.js
index 1c021e3d..a6033e27 100644
--- a/ds-bot/src/host/usage.js
+++ b/ds-bot/src/host/usage.js
@@ -405,8 +405,13 @@ export function install(rt) {
for (const room of Object.values(state.rooms)) {
for (const segments of Object.values(room.segments ?? {})) for (const segment of segments) if (segment.endedAt < longAgo) ended.add(segment.sessionId)
}
+ // A listing row's projections are hints, not cursors. Only a 'sequenced'
+ // block shares the Session log's sequence space; a 'cached' block carries
+ // 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)
const targets = new Map()
- for (const item of listed) if (item.blank !== true) targets.set(item.sessionId, item.projections?.asOfSeq)
+ 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)]))
await eachLimited([...targets], SCAN_PARALLEL, async ([sessionId, asOfSeq]) => {
diff --git a/ds-bot/test/usage.test.js b/ds-bot/test/usage.test.js
index 142d2e28..da36a5a6 100644
--- a/ds-bot/test/usage.test.js
+++ b/ds-bot/test/usage.test.js
@@ -206,7 +206,7 @@ test('a subagent Session is read through its parent, even a parent that is gone;
listedExtra: [
{ 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: 'cached', asOfSeq: 1, values: {} } },
+ { sessionId: 's-broken', projections: { kind: 'sequenced', asOfSeq: 1, values: {} } },
],
logsExtra: {
's-orphan': [header('deepseek', 'v4-flash'), message(1, 0, usage(8, 1), HOUR)],
@@ -231,3 +231,51 @@ test('a subagent Session is read through its parent, even a parent that is gone;
assert.equal(pages.filter(sessionId => sessionId === 's-orphan').length, 1)
assert.deepEqual(unread(), ['s-broken'])
})
+
+test('a cached projection watermark beyond the log is not used as a page cursor', async (t) => {
+ const { call, ctx, stop, warnings } = start(undefined, {
+ listedExtra: [{ sessionId: 's-x', projections: { kind: 'cached', asOfSeq: 202, values: {} } }],
+ })
+ t.after(stop)
+ const page = ctx.sessionController.page
+ ctx.sessionController.page = async (request) => {
+ if (request.address.sessionId === 's-x' && request.throughSeq > 1) throw new Error(`session page through seq ${request.throughSeq} is past cursor 1`)
+ return page(request)
+ }
+ const view = (await call('usage', { timeZone: 'Asia/Shanghai' })).value
+ assert.equal(totals(view)['other/chat'], 44)
+ assert.deepEqual(warnings, [])
+})
+
+test('a cached projection watermark behind the log does not omit later usage', async (t) => {
+ const { call, stop, warnings } = start(undefined, {
+ listedExtra: [{ sessionId: 's-x', projections: { kind: 'cached', asOfSeq: 0, values: {} } }],
+ })
+ t.after(stop)
+ const view = (await call('usage', { timeZone: 'Asia/Shanghai' })).value
+ assert.equal(totals(view)['other/chat'], 44)
+ assert.deepEqual(warnings, [])
+})
+
+test('a sequenced projection watermark cuts the page without another projection read', async (t) => {
+ const { call, ctx, stop } = start(undefined, {
+ listedExtra: [{ sessionId: 's-x', projections: { kind: 'sequenced', asOfSeq: 1, values: {} } }],
+ })
+ t.after(stop)
+ const projections = ctx.sessionController.projections
+ ctx.sessionController.projections = async (request) => {
+ assert.notEqual(request.sessionId, 's-x')
+ return projections(request)
+ }
+ const view = (await call('usage', { timeZone: 'Asia/Shanghai' })).value
+ assert.equal(totals(view)['other/chat'], 44)
+})
+
+test('a projection hint without a sequence space is not used as a page cursor', async (t) => {
+ const { call, stop } = start(undefined, {
+ listedExtra: [{ sessionId: 's-x', projections: { asOfSeq: 0, values: {} } }],
+ })
+ t.after(stop)
+ const view = (await call('usage', { timeZone: 'Asia/Shanghai' })).value
+ assert.equal(totals(view)['other/chat'], 44)
+})