Skip to content

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

Add periodic clock context to Bot sessions

lulu feizhu471133e5cd36
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b1fc4426..6bb182af 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -15,6 +15,7 @@ All notable changes to DS Bot are listed here. The format follows [Keep a Change
### Fixed
+- Bots receive the current time on their first model step and refresh it after ten minutes, even when their DSH preset does not supply a clock.
- In a group chat, a conversation referenced in a message reached none of the members, and a reference whose title started with a member's name called that member. Now the members who answer the message get the referenced conversation.
- In a group chat, typing `@` and a Bot's name and pressing Enter inserted a conversation reference instead of the Bot's name.
- A long conversation's fresh part keeps the Bot's model and thinking level. Before, the new part fell back to the default model.
diff --git a/ds-bot/index.js b/ds-bot/index.js
index b88cb0a8..4379acdd 100644
--- a/ds-bot/index.js
+++ b/ds-bot/index.js
@@ -12,6 +12,7 @@ import * as delivery from './src/host/delivery.js'
import * as roster from './src/host/roster.js'
import * as agents from './src/host/agents.js'
import * as prompt from './src/host/prompt.js'
+import * as timeContext from './src/host/time-context.js'
import * as tools from './src/host/tools.js'
import * as chatIndex from './src/host/chat-index.js'
import * as groups from './src/host/groups.js'
@@ -36,7 +37,7 @@ export const inject = ['llm', 'tools', 'systemPrompt', 'sessionController', 'ses
// the functions other modules call; they call them through `rt` at run time, never during
// install. Listeners run and tools are listed in registration order, so this order holds:
// usage meters model requests after every listener that rewrites them.
-const MODULES = [diagnostics, store, models, permissions, turns, delivery, roster, agents, prompt, tools, chatIndex, groups, memory, parts, images, secrets, connectors, schedules, browser, usage, update, dshUpdate, channel]
+const MODULES = [diagnostics, store, models, permissions, turns, delivery, roster, agents, prompt, timeContext, tools, chatIndex, groups, memory, parts, images, secrets, connectors, schedules, browser, usage, update, dshUpdate, channel]
export function apply(ctx, config = {}) {
const home = config.home ?? join(process.env.DSH_HOME ?? join(homedir(), '.dsh'), 'bot')
diff --git a/ds-bot/src/host/time-context.js b/ds-bot/src/host/time-context.js
new file mode 100644
index 00000000..fc695e6d
--- /dev/null
+++ b/ds-bot/src/host/time-context.js
@@ -0,0 +1,43 @@
+import { createUserMessage } from '@deepseek-ai/dsh-llm'
+
+const SOURCE = 'ds-bot-time-context'
+const isClock = source => source?.kind === SOURCE || source?.kind === 'time-context'
+
+export function install(rt) {
+ const { ctx, config } = rt
+ const interval = config.timeRefreshIntervalMs ?? 600_000
+ if (!Number.isSafeInteger(interval) || interval < 0) throw new TypeError('timeRefreshIntervalMs must be a non-negative safe integer')
+ const formatter = new Intl.DateTimeFormat('en-CA', {
+ ...(config.timeZone ? { timeZone: config.timeZone } : {}),
+ year: 'numeric', month: '2-digit', day: '2-digit',
+ hour: '2-digit', minute: '2-digit', second: '2-digit', hourCycle: 'h23',
+ timeZoneName: 'longOffset',
+ })
+ const zone = formatter.resolvedOptions().timeZone
+
+ ctx.on('agent/pre-step', async ({ agent, signal }, next) => {
+ const decision = await next()
+ if (decision.kind === 'reject' || signal.aborted) return decision
+ await rt.load()
+ if (!rt.isBotSession(agent.id) || signal.aborted) return decision
+ // A preset may already supply DSH's clock for this step.
+ if (decision.messages.some(message => isClock(message.source))) return decision
+ const now = Date.now()
+ for await (const event of rt.eventsBackwards(agent.id, signal, Infinity)) {
+ if (event.type === 'user/message' && isClock(event.data.source)) {
+ if (now >= event.time && now - event.time < interval) return decision
+ break
+ }
+ if (event.time <= now - interval) break
+ }
+ if (signal.aborted) return decision
+ const text = `Current time: ${formatter.format(now)} (${zone}).`
+ return {
+ ...decision,
+ messages: [...decision.messages, createUserMessage({
+ content: [{ type: 'text', text }],
+ source: { kind: SOURCE, form: 'snapshot', sections: [{ name: SOURCE, text }] },
+ })],
+ }
+ }, { prepend: true })
+}
diff --git a/ds-bot/test/install.test.js b/ds-bot/test/install.test.js
index bcd2c120..cd37a79d 100644
--- a/ds-bot/test/install.test.js
+++ b/ds-bot/test/install.test.js
@@ -40,6 +40,7 @@ test('apply registers listeners, tools, and effects in a fixed order', (t) => {
'on agent/created',
'effect ds-bot: identity section',
'prompt bot-identity 640',
+ 'on agent/pre-step (prepend)',
'tool list_bots',
'tool message_bot',
'tool interrupt_bot',
diff --git a/ds-bot/test/time-context.test.js b/ds-bot/test/time-context.test.js
new file mode 100644
index 00000000..3d79601d
--- /dev/null
+++ b/ds-bot/test/time-context.test.js
@@ -0,0 +1,113 @@
+import assert from 'node:assert/strict'
+import { test } from 'node:test'
+import { install } from '../src/host/time-context.js'
+import { historyEntry } from '../src/host/parts.js'
+import { fakeContext } from './fake-ctx.js'
+
+const NOW = Date.UTC(2026, 9, 12, 4, 30)
+const reading = (time, kind = 'ds-bot-time-context') => ({ type: 'user/message', time, data: { source: { kind } } })
+
+function setup(t, { events = [], config = {}, owned = true } = {}) {
+ t.mock.method(Date, 'now', () => NOW)
+ const ctx = fakeContext()
+ let reads = 0
+ const rt = {
+ ctx, config: { timeZone: 'Asia/Shanghai', ...config },
+ load: async () => {},
+ isBotSession: () => owned,
+ async* eventsBackwards() {
+ reads += 1
+ yield* [...events].reverse()
+ },
+ }
+ install(rt)
+ const hook = ctx.listeners.get('agent/pre-step')[0]
+ const message = { role: 'user', content: [{ type: 'text', text: 'Hello' }], source: { kind: 'user' } }
+ const step = (decision = { kind: 'enter', messages: [message] }, payload = {}) => hook({
+ agent: { id: 'bot', session: { id: 'bot' } }, turn: 1, step: 1,
+ signal: new AbortController().signal, ...payload,
+ }, async () => decision)
+ return { step, message, reads: () => reads }
+}
+
+test('a first Bot step appends a dated, zoned snapshot without changing the user message', async (t) => {
+ const { step, message } = setup(t)
+ const result = await step()
+ assert.equal(result.messages[0], message)
+ const clock = result.messages[1]
+ assert.match(clock.content[0].text, /2026-10-12.*12:30:00.*GMT\+08:00.*Asia\/Shanghai/)
+ assert.deepEqual(clock.source, {
+ kind: 'ds-bot-time-context', form: 'snapshot',
+ sections: [{ name: 'ds-bot-time-context', text: clock.content[0].text }],
+ })
+ assert.equal(historyEntry({ type: 'user/message', data: clock }, false), undefined)
+})
+
+test('a resumed session suppresses readings before ten minutes and refreshes at the boundary', async (t) => {
+ const { step } = setup(t, { events: [reading(NOW - 599_999)] })
+ const decision = { kind: 'enter', messages: [] }
+ assert.equal(await step(decision), decision)
+ Date.now.mock.mockImplementation(() => NOW + 1)
+ assert.equal((await step(decision)).messages.length, 1)
+})
+
+test('a long running turn refreshes on a later step', async (t) => {
+ const { step } = setup(t, { events: [reading(NOW - 600_000)] })
+ assert.equal((await step(undefined, { step: 3 })).messages.length, 2)
+})
+
+test('native DSH readings, pending or persisted, do not get a second clock', async (t) => {
+ const { step, reads } = setup(t, { events: [reading(NOW - 1, 'time-context')] })
+ const decision = { kind: 'enter', messages: [{ source: { kind: 'time-context' } }] }
+ assert.equal(await step(decision), decision)
+ assert.equal(reads(), 0)
+ const empty = { kind: 'enter', messages: [] }
+ assert.equal(await step(empty), empty)
+})
+
+test('an expired native DSH clock is refreshed when the preset no longer supplies one', async (t) => {
+ const { step } = setup(t, { events: [reading(NOW - 600_000, 'time-context')] })
+ assert.equal((await step()).messages.length, 2)
+})
+
+test('a rejection or cancellation does not add a reading or read history', async (t) => {
+ const { step, reads } = setup(t)
+ const reject = { kind: 'reject' }
+ assert.equal(await step(reject), reject)
+ const abort = new AbortController()
+ abort.abort()
+ const decision = { kind: 'enter', messages: [] }
+ assert.equal(await step(decision, { signal: abort.signal }), decision)
+ assert.equal(reads(), 0)
+})
+
+test('ordinary DSH sessions are unchanged', async (t) => {
+ const { step, reads } = setup(t, { owned: false })
+ const decision = { kind: 'enter', messages: [] }
+ assert.equal(await step(decision), decision)
+ assert.equal(reads(), 0)
+})
+
+test('group member sessions and fresh parts get their own clock', async (t) => {
+ const { step } = setup(t)
+ for (const id of ['group-member', 'fresh-part']) {
+ assert.equal((await step(undefined, { agent: { id } })).messages.length, 2)
+ }
+})
+
+test('a backward wall-clock change refreshes the stale future reading', async (t) => {
+ const { step } = setup(t, { events: [reading(NOW + 1)] })
+ assert.equal((await step()).messages.length, 2)
+})
+
+test('a zero interval refreshes every eligible step', async (t) => {
+ const { step } = setup(t, { config: { timeRefreshIntervalMs: 0 }, events: [reading(NOW)] })
+ assert.equal((await step()).messages.length, 2)
+})
+
+test('invalid intervals and zones fail at plugin installation', () => {
+ for (const interval of [-1, 0.5, Infinity, NaN, '10']) {
+ assert.throws(() => install({ ctx: fakeContext(), config: { timeRefreshIntervalMs: interval } }), /timeRefreshIntervalMs/)
+ }
+ assert.throws(() => install({ ctx: fakeContext(), config: { timeZone: 'invalid-zone' } }), RangeError)
+})