time-context.spec.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
  2. import { Context } from 'cordis'
  3. import Loader from '@cordisjs/plugin-loader'
  4. import { CallId, LlmAdapter } from '@deepseek-ai/dsh-llm'
  5. import type { GenerateOptions, StreamChunk } from '@deepseek-ai/dsh-llm'
  6. import { Session, SessionId, type SessionEvent } from '@deepseek-ai/dsh-session'
  7. import AgentRegistry, { agentEvents, AgentMessageId, type Agent } from '@deepseek-ai/dsh-agent'
  8. import { defineContentToolFixture } from '@deepseek-ai/dsh-tools'
  9. import AgentLoop from '@deepseek-ai/dsh-agent-loop'
  10. import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit'
  11. import * as timeContext from '@deepseek-ai/dsh-time-context'
  12. import type { Config } from '@deepseek-ai/dsh-time-context'
  13. const BASE = Date.parse('2026-07-14T00:00:00.000Z')
  14. const ORIGINAL_TIME_ZONE = process.env['TZ']
  15. const SIGNAL = new AbortController().signal
  16. beforeEach(() => {
  17. process.env['TZ'] = 'UTC'
  18. vi.useFakeTimers()
  19. vi.setSystemTime(BASE)
  20. })
  21. afterEach(() => {
  22. vi.restoreAllMocks()
  23. vi.useRealTimers()
  24. if (ORIGINAL_TIME_ZONE === undefined) delete process.env['TZ']
  25. else process.env['TZ'] = ORIGINAL_TIME_ZONE
  26. })
  27. async function mount(config: Config = {}) {
  28. const ctx = new Context()
  29. await ctx.plugin(AgentRegistry)
  30. const fiber = await ctx.plugin(timeContext, config)
  31. return { ctx, fiber }
  32. }
  33. function sessionAgent(session: Session, id = 'agent'): Agent {
  34. return {
  35. id: SessionId(id),
  36. options: {},
  37. session,
  38. status: 'running',
  39. ctx: new Context(),
  40. followup: () => AgentMessageId('stub'),
  41. queue: () => AgentMessageId('stub'),
  42. steer: () => AgentMessageId('stub'),
  43. inject(content, options) {
  44. session.append('user/message', {
  45. content,
  46. source: options?.source ?? { kind: 'user' },
  47. }, { surfaceOp: 'append' })
  48. return AgentMessageId('stub')
  49. },
  50. send: () => AgentMessageId('stub'),
  51. cancel() {},
  52. whenIdle: () => Promise.resolve(),
  53. }
  54. }
  55. function openMessageTurn(session: Session, turn: number): void {
  56. session.append('turn/start', { turn, trigger: { kind: 'message', source: { kind: 'user' } } })
  57. session.append('user/message', {
  58. content: [{ type: 'text', text: `turn ${turn}` }],
  59. source: { kind: 'user' },
  60. }, { surfaceOp: 'append' })
  61. }
  62. function contextTexts(session: Session): string[] {
  63. const texts: string[] = []
  64. for (const event of session.events) {
  65. if (event.type === 'user/message'
  66. && event.data.source.kind === 'plugin'
  67. && event.data.source.plugin === 'time-context') {
  68. texts.push(event.data.content.find(block => block.type === 'text')?.text ?? '')
  69. }
  70. }
  71. return texts
  72. }
  73. async function fire(
  74. ctx: Context,
  75. agent: Agent,
  76. turn: number,
  77. step: number,
  78. signal: AbortSignal = SIGNAL,
  79. ): Promise<void> {
  80. await agentEvents(ctx, agent).serial('agent/pre-step', turn, step, signal)
  81. }
  82. function textResponse(text: string): StreamChunk[] {
  83. return [
  84. { type: 'block-start', index: 0, blockType: 'text' },
  85. { type: 'block-end', index: 0, block: { type: 'text', text } },
  86. { type: 'finish', reason: { kind: 'stop' } },
  87. ]
  88. }
  89. function toolCallResponse(): StreamChunk[] {
  90. return [
  91. { type: 'block-start', index: 0, blockType: 'tool-call' },
  92. {
  93. type: 'block-end',
  94. index: 0,
  95. block: { type: 'tool-call', id: CallId('tick-1'), name: 'tick', arguments: '{}' },
  96. },
  97. { type: 'finish', reason: { kind: 'tool-calls' } },
  98. ]
  99. }
  100. class ScriptedAdapter extends LlmAdapter {
  101. readonly requests: GenerateOptions[] = []
  102. constructor(private readonly script: StreamChunk[][]) {
  103. super()
  104. }
  105. override async * stream(options: GenerateOptions): AsyncIterable<StreamChunk> {
  106. this.requests.push(options)
  107. const chunks = this.script.shift()
  108. if (chunks === undefined) throw new Error('ScriptedAdapter: script exhausted')
  109. for (const chunk of chunks) yield chunk
  110. }
  111. }
  112. async function loopHarness(adapter: ScriptedAdapter, config: Config = {}): Promise<Context> {
  113. const ctx = new Context()
  114. await mountAgentLoopTestDependencies(ctx)
  115. await ctx.plugin(AgentLoop, { agents: [] })
  116. await ctx.plugin(timeContext, config)
  117. ctx.llm.registerAdapter(['mock'], adapter)
  118. return ctx
  119. }
  120. function requestText(request: GenerateOptions): string {
  121. return request.messages
  122. .flatMap(message => message.content)
  123. .filter(block => block.type === 'text')
  124. .map(block => block.text)
  125. .join('\n')
  126. }
  127. describe('durable step context', () => {
  128. it('records turn, step, zoned time, and the preceding model-visible message baseline', async () => {
  129. const { ctx } = await mount({ timeZone: 'Asia/Shanghai' })
  130. const session = new Session(SessionId('first'))
  131. openMessageTurn(session, 1)
  132. vi.setSystemTime(BASE + 90_061_000)
  133. await fire(ctx, sessionAgent(session), 1, 1)
  134. expect(contextTexts(session)).toEqual([
  135. 'Time sampled while preparing turn 1, step 1: 2026-07-15T09:01:01+08:00[Asia/Shanghai]\n'
  136. + 'Elapsed since the preceding model-visible message: 1d 1h 1m 1s.',
  137. ])
  138. const event = session.events.at(-1)
  139. expect(event?.type).toBe('user/message')
  140. if (event?.type !== 'user/message') throw new Error('missing time context')
  141. expect(event.data.source).toEqual({ kind: 'plugin', plugin: 'time-context' })
  142. expect(event.surfaceOp).toBe('append')
  143. })
  144. it('reports an unavailable first-step baseline when no model-visible message precedes it', async () => {
  145. const { ctx } = await mount()
  146. const session = new Session(SessionId('unavailable'))
  147. session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  148. await fire(ctx, sessionAgent(session), 1, 1)
  149. expect(contextTexts(session)[0]).toContain(
  150. 'Elapsed since the preceding model-visible message: unavailable.',
  151. )
  152. })
  153. it.each([
  154. ['omitted interval', {}],
  155. ['zero interval', { refreshIntervalMs: 0 }],
  156. ] as const)('uses the preceding durable step-context timestamp after step one with %s', async (_label, config) => {
  157. const { ctx } = await mount(config)
  158. const session = new Session(SessionId('later-step'))
  159. const agent = sessionAgent(session)
  160. openMessageTurn(session, 3)
  161. await fire(ctx, agent, 3, 1)
  162. vi.setSystemTime(BASE + 61_000)
  163. await fire(ctx, agent, 3, 2)
  164. expect(contextTexts(session)[1]).toBe(
  165. 'Time sampled while preparing turn 3, step 2: 2026-07-14T00:01:01+00:00[UTC]\n'
  166. + 'Elapsed since the preceding step context: 1m 1s.',
  167. )
  168. })
  169. it('reports an unavailable later-step baseline at the matching turn boundary', async () => {
  170. const { ctx } = await mount()
  171. const session = new Session(SessionId('later-step-boundary'))
  172. openMessageTurn(session, 4)
  173. await fire(ctx, sessionAgent(session), 4, 2)
  174. expect(contextTexts(session)[0]).toContain(
  175. 'Elapsed since the preceding step context: unavailable.',
  176. )
  177. })
  178. it('reports an unavailable later-step baseline when event lookup is exhausted', async () => {
  179. const { ctx } = await mount()
  180. const session = new Session(SessionId('later-step-exhausted'))
  181. await fire(ctx, sessionAgent(session), 1, 2)
  182. expect(contextTexts(session)[0]).toContain(
  183. 'Elapsed since the preceding step context: unavailable.',
  184. )
  185. })
  186. it('injects after backward wall-clock movement and clamps elapsed time to zero', async () => {
  187. const { ctx } = await mount({ refreshIntervalMs: 60_000 })
  188. const session = new Session(SessionId('backward'))
  189. const agent = sessionAgent(session)
  190. openMessageTurn(session, 1)
  191. await fire(ctx, agent, 1, 1)
  192. vi.setSystemTime(BASE - 5_000)
  193. await fire(ctx, agent, 1, 2)
  194. expect(contextTexts(session)).toHaveLength(2)
  195. expect(contextTexts(session)[1]).toContain('Elapsed since the preceding step context: 0s.')
  196. })
  197. it('uses a shadowed durable injection after resume and injects at the exact threshold', async () => {
  198. const { ctx } = await mount({ refreshIntervalMs: 1_000 })
  199. const original = new Session(SessionId('seed-source'))
  200. openMessageTurn(original, 1)
  201. await fire(ctx, sessionAgent(original), 1, 1)
  202. const user = original.events.find(event => event.type === 'user/message' && event.data.source.kind === 'user')
  203. const reading = original.events.find(event => event.type === 'user/message' && event.data.source.kind === 'plugin')
  204. if (user === undefined || reading === undefined) throw new Error('missing source surface events')
  205. original.append('user/message', {
  206. content: [{ type: 'text', text: 'compacted history' }],
  207. source: { kind: 'plugin', plugin: 'compact-basic' },
  208. }, {
  209. surfaceOp: { op: 'replace', start: user.seq, end: reading.seq },
  210. sourceEventSeqs: [user.seq, reading.seq],
  211. })
  212. original.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  213. expect(JSON.stringify(original.deriveMessages())).not.toContain('Time sampled while preparing')
  214. const resumed = new Session(SessionId('resumed'), [...original.events])
  215. const resumedAgent = sessionAgent(resumed)
  216. vi.setSystemTime(BASE + 999)
  217. openMessageTurn(resumed, 2)
  218. const beforeSkip = resumed.events.length
  219. await fire(ctx, resumedAgent, 2, 1)
  220. expect(resumed.events).toHaveLength(beforeSkip)
  221. expect(contextTexts(resumed)).toHaveLength(1)
  222. vi.setSystemTime(BASE + 1_000)
  223. await fire(ctx, resumedAgent, 2, 2)
  224. expect(contextTexts(resumed)).toHaveLength(2)
  225. expect(contextTexts(resumed)[1]).toContain(
  226. 'Elapsed since the preceding step context: unavailable.',
  227. )
  228. })
  229. it('applies a positive interval across turns without sharing state between sessions', async () => {
  230. const { ctx } = await mount({ refreshIntervalMs: 1_000 })
  231. const first = new Session(SessionId('interval-first'))
  232. const firstAgent = sessionAgent(first, 'first-agent')
  233. openMessageTurn(first, 1)
  234. await fire(ctx, firstAgent, 1, 1)
  235. first.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  236. vi.setSystemTime(BASE + 500)
  237. openMessageTurn(first, 2)
  238. const beforeSkip = first.events.length
  239. await fire(ctx, firstAgent, 2, 1)
  240. const independent = new Session(SessionId('interval-independent'))
  241. openMessageTurn(independent, 1)
  242. await fire(ctx, sessionAgent(independent, 'independent-agent'), 1, 1)
  243. expect(first.events).toHaveLength(beforeSkip)
  244. expect(contextTexts(first)).toHaveLength(1)
  245. expect(contextTexts(independent)).toHaveLength(1)
  246. })
  247. it('runs before ordinary pre-step listeners and skips an already-aborted step', async () => {
  248. const { ctx } = await mount()
  249. const session = new Session(SessionId('ordering'))
  250. const agent = sessionAgent(session)
  251. openMessageTurn(session, 1)
  252. let ordinarySawContext = false
  253. ctx.on('agent/pre-step', (subject) => {
  254. ordinarySawContext = subject.session.events.some(event => event.type === 'user/message')
  255. })
  256. await fire(ctx, agent, 1, 1)
  257. const abort = new AbortController()
  258. abort.abort()
  259. await fire(ctx, agent, 1, 2, abort.signal)
  260. expect(ordinarySawContext).toBe(true)
  261. expect(contextTexts(session)).toHaveLength(1)
  262. })
  263. })
  264. describe('configuration and lifecycle', () => {
  265. it('defaults to the process system zone and retains the zone resolved at plugin load', async () => {
  266. process.env['TZ'] = 'Asia/Shanghai'
  267. const { ctx } = await mount()
  268. process.env['TZ'] = 'America/New_York'
  269. const session = new Session(SessionId('system-zone'))
  270. openMessageTurn(session, 1)
  271. await fire(ctx, sessionAgent(session), 1, 1)
  272. expect(contextTexts(session)[0]).toContain('2026-07-14T08:00:00+08:00[Asia/Shanghai]')
  273. })
  274. it('fails loud for an invalid explicit zone or an unavailable process zone', async () => {
  275. const invalid = new Context()
  276. await invalid.plugin(AgentRegistry)
  277. await expect(invalid.plugin(timeContext, { timeZone: 'Not/A_Real_Zone' })).rejects.toThrow(
  278. /invalid IANA timeZone/,
  279. )
  280. vi.spyOn(Intl, 'DateTimeFormat').mockImplementationOnce(() => {
  281. throw new RangeError('system zone unavailable')
  282. })
  283. const unresolved = new Context()
  284. await unresolved.plugin(AgentRegistry)
  285. await expect(unresolved.plugin(timeContext, {})).rejects.toThrow(/failed to resolve the system time zone/)
  286. })
  287. it('rejects invalid refresh intervals at plugin load with one diagnostic', async () => {
  288. const invalid = [-1, 0.5, Number.MAX_SAFE_INTEGER + 1, Number.POSITIVE_INFINITY, Number.NaN]
  289. for (const refreshIntervalMs of invalid) {
  290. await expect(mount({ refreshIntervalMs })).rejects.toThrow(
  291. 'time-context: refreshIntervalMs must be a non-negative safe integer',
  292. )
  293. }
  294. })
  295. it('removes its listener when the plugin fiber disposes', async () => {
  296. const { ctx, fiber } = await mount()
  297. const session = new Session(SessionId('dispose'))
  298. const agent = sessionAgent(session)
  299. openMessageTurn(session, 1)
  300. await fire(ctx, agent, 1, 1)
  301. await fiber.dispose()
  302. await fire(ctx, agent, 1, 2)
  303. expect(contextTexts(session)).toHaveLength(1)
  304. })
  305. })
  306. describe('real agent-loop request history', () => {
  307. it.each([
  308. ['throws', 'error'],
  309. ['cancels', 'aborted'],
  310. ] as const)('retains the preparation reading when a later pre-step listener %s', async (mode, reasonKind) => {
  311. const adapter = new ScriptedAdapter([textResponse('unused')])
  312. const ctx = await loopHarness(adapter)
  313. let laterSawReading = false
  314. ctx.on('agent/pre-step', (subject) => {
  315. laterSawReading = contextTexts(subject.session).length === 1
  316. if (mode === 'throws') throw new Error('later pre-step failure')
  317. subject.cancel({ kind: 'user' })
  318. })
  319. const agent = ctx.agentLoop.create(SessionId(`late-${mode}`), { provider: 'mock', model: 'mock' })
  320. agent.followup([{ type: 'text', text: 'start' }])
  321. await agent.whenIdle()
  322. expect(laterSawReading).toBe(true)
  323. expect(contextTexts(agent.session)).toHaveLength(1)
  324. expect(adapter.requests).toHaveLength(0)
  325. expect(agent.session.events.some(event => event.type === 'step/start')).toBe(false)
  326. const turnEnd = agent.session.events.findLast(event => event.type === 'turn/end')
  327. expect(turnEnd?.type === 'turn/end' && turnEnd.data.reason.kind).toBe(reasonKind)
  328. await ctx.fiber.dispose()
  329. })
  330. it('persists one ordered context per request, accumulates readings, and leaves system headers unchanged', async () => {
  331. const adapter = new ScriptedAdapter([toolCallResponse(), textResponse('done')])
  332. const ctx = await loopHarness(adapter)
  333. ctx.tools.register(defineContentToolFixture({
  334. name: 'tick',
  335. description: 'advance fake time',
  336. parameters: {},
  337. async execute() {
  338. vi.setSystemTime(BASE + 61_000)
  339. return [{ type: 'text' as const, text: 'advanced' }]
  340. },
  341. }))
  342. const agent = ctx.agentLoop.create(SessionId('loop'), { provider: 'mock', model: 'mock' })
  343. agent.followup([{ type: 'text', text: 'start' }])
  344. await agent.whenIdle()
  345. expect(adapter.requests).toHaveLength(2)
  346. const contexts = agent.session.events.filter(
  347. (event): event is SessionEvent<'user/message'> => event.type === 'user/message' && event.data.source.kind === 'plugin')
  348. const starts = agent.session.events.filter(event => event.type === 'step/start')
  349. expect(contexts).toHaveLength(adapter.requests.length)
  350. expect(starts).toHaveLength(adapter.requests.length)
  351. for (let index = 0; index < contexts.length; index += 1) {
  352. expect(contexts[index]!.seq).toBeLessThan(starts[index]!.seq)
  353. }
  354. expect(contexts.every(event => event.data.source.kind === 'plugin'
  355. && event.data.source.plugin === 'time-context'
  356. && event.surfaceOp === 'append')).toBe(true)
  357. const firstRequestText = requestText(adapter.requests[0]!)
  358. const secondRequestText = requestText(adapter.requests[1]!)
  359. expect(firstRequestText).toContain('Time sampled while preparing turn 1, step 1:')
  360. expect(firstRequestText).toContain('Elapsed since the preceding model-visible message: 0s.')
  361. expect(firstRequestText).not.toContain('Time sampled while preparing turn 1, step 2:')
  362. expect(secondRequestText).toContain('Time sampled while preparing turn 1, step 1:')
  363. expect(secondRequestText).toContain('Time sampled while preparing turn 1, step 2:')
  364. expect(secondRequestText).toContain('Elapsed since the preceding step context: 1m 1s.')
  365. for (const request of adapter.requests) expect(request.system).not.toContain('Time sampled while preparing')
  366. const headers = agent.session.events.filter(event => event.type === 'request/header')
  367. expect(JSON.stringify(headers)).not.toContain('Time sampled while preparing')
  368. await ctx.fiber.dispose()
  369. })
  370. })
  371. describe('real Loader export path', () => {
  372. it('keeps namespace metadata and boots the agent listener through unwrapExports', async () => {
  373. expect('default' in timeContext).toBe(false)
  374. const loader = Object.create(Loader.prototype) as Loader
  375. const unwrapped = loader.unwrapExports(timeContext) as Record<string, unknown>
  376. expect(unwrapped).toBe(timeContext)
  377. expect(unwrapped.name).toBe('time-context')
  378. expect(unwrapped.inject).toEqual(['agents'])
  379. expect(unwrapped.Config).toBeDefined()
  380. expect(typeof unwrapped.apply).toBe('function')
  381. const ctx = new Context()
  382. await ctx.plugin(AgentRegistry)
  383. const plugin = loader.unwrapExports(timeContext) as Parameters<Context['plugin']>[0]
  384. await ctx.plugin(plugin)
  385. const session = new Session(SessionId('loader'))
  386. openMessageTurn(session, 1)
  387. await fire(ctx, sessionAgent(session), 1, 1)
  388. expect(contextTexts(session)[0]).toContain('Time sampled while preparing turn 1, step 1:')
  389. })
  390. })