invariant.spec.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. import { createUserMessage } from '@deepseek-ai/dsh-llm'
  2. import { describe, expect, it, vi } from 'vitest'
  3. import { Context } from '@deepseek-ai/cordis'
  4. import type { ContentBlock } from '@deepseek-ai/dsh-llm'
  5. import SessionStore, { Session, SessionId, SessionSeq, type SessionEvent } from '@deepseek-ai/dsh-session'
  6. import * as TimeInvariant from '@deepseek-ai/dsh-time-context/invariant'
  7. import InvariantRegistry from '@deepseek-ai/dsh-invariants'
  8. const SECOND = Date.parse('2026-07-14T00:00:00Z')
  9. async function setup(): Promise<Context> {
  10. const ctx = new Context()
  11. await ctx.plugin(SessionStore)
  12. await ctx.plugin(InvariantRegistry, { enabled: true })
  13. await ctx.plugin(TimeInvariant)
  14. return ctx
  15. }
  16. function event(
  17. text: string,
  18. time = SECOND + 456,
  19. content?: unknown[],
  20. plugin = 'time-context',
  21. ): SessionEvent<'user/message'> {
  22. return {
  23. type: 'user/message',
  24. surfaceOp: 'append',
  25. seq: SessionSeq(0),
  26. time,
  27. data: createUserMessage({
  28. content: (content ?? [{ type: 'text', text }]) as ContentBlock[],
  29. source: plugin === 'time-context'
  30. ? {
  31. kind: 'plugin',
  32. plugin,
  33. form: 'snapshot',
  34. sections: [{ name: plugin, text }],
  35. }
  36. : { kind: 'plugin', plugin },
  37. }),
  38. }
  39. }
  40. function reading(
  41. turn = '1',
  42. step = '1',
  43. baseline = 'model-visible message',
  44. timestamp = '2026-07-14T00:00:00+00:00[UTC]',
  45. browser = 'Browser time zone for this request: unavailable. Ask the user to clarify otherwise-unqualified dates and times.',
  46. ): string {
  47. return `Time sampled while preparing turn ${turn}, step ${step}: ${timestamp}\n`
  48. + `${browser}\n`
  49. + `Elapsed since the preceding ${baseline}: unavailable.`
  50. }
  51. function preparing(turn: number, step: number, clientTimeZone?: string): Session {
  52. const session = Session.create(SessionId(`time-invariant-${turn}-${step}`))
  53. for (let priorTurn = 1; priorTurn < turn; priorTurn += 1) {
  54. session.append('turn/start', { turn: priorTurn })
  55. session.append('turn/end', { turn: priorTurn, reason: { kind: 'completed' } })
  56. }
  57. session.append('turn/start', { turn })
  58. session.append('user/message', createUserMessage({
  59. content: [{ type: 'text', text: `turn ${turn}` }],
  60. source: clientTimeZone === undefined
  61. ? { kind: 'user' }
  62. : { kind: 'user', rpcId: `turn-${String(turn)}`, clientTimeZone } as never,
  63. }), { surfaceOp: 'append' })
  64. for (let priorStep = 1; priorStep < step; priorStep += 1) {
  65. session.append('step/start', { turn, step: priorStep })
  66. session.append('step/end', { turn, step: priorStep })
  67. }
  68. session.append('step/start', { turn, step })
  69. return session
  70. }
  71. function appendReading(session: Session, text: string): void {
  72. session.append('user/message', createUserMessage({
  73. content: [{ type: 'text', text }],
  74. source: {
  75. kind: 'plugin',
  76. plugin: 'time-context',
  77. form: 'snapshot',
  78. sections: [{ name: 'time-context', text }],
  79. },
  80. }), { surfaceOp: 'append' })
  81. }
  82. describe('time-context invariants', () => {
  83. it('accepts a reading whose turn, step, baseline, and timestamp agree', async () => {
  84. const ctx = await setup()
  85. const text = 'Time sampled while preparing turn 2, step 3: 2026-07-14T00:00:00+00:00[UTC]\n'
  86. + 'Browser time zone for this request: unavailable. Ask the user to clarify otherwise-unqualified dates and times.\n'
  87. + 'Elapsed since the preceding step context: 4m 2s.'
  88. expect(() => { ctx.emit('session/event', preparing(2, 3), event(text)) }).not.toThrow()
  89. })
  90. it('accepts a reading durably appended after a long process pause', async () => {
  91. const ctx = await setup()
  92. expect(() => {
  93. ctx.emit('session/event', preparing(1, 1), event(reading(), SECOND + 60_000))
  94. }).not.toThrow()
  95. })
  96. it('requires browser-zone policy and timestamp to match current-turn request provenance', async () => {
  97. const ctx = await setup()
  98. const policy = 'Browser time zone for this request: Asia/Shanghai. '
  99. + 'Interpret otherwise-unqualified dates and times in this zone.'
  100. expect(() => {
  101. ctx.emit('session/event', preparing(1, 1, 'Asia/Shanghai'), event(reading(
  102. '1',
  103. '1',
  104. 'model-visible message',
  105. '2026-07-14T08:00:00+08:00[Asia/Shanghai]',
  106. policy,
  107. ), SECOND + 456))
  108. }).not.toThrow()
  109. expect(() => {
  110. ctx.emit('session/event', preparing(1, 1, 'Asia/Shanghai'), event(reading()))
  111. }).toThrow(/browser-zone text/)
  112. expect(() => {
  113. ctx.emit('session/event', preparing(1, 1, 'Asia/Shanghai'), event(reading(
  114. '1',
  115. '1',
  116. 'model-visible message',
  117. '2026-07-14T00:00:00+00:00[UTC]',
  118. policy,
  119. )))
  120. }).toThrow(/rendered timestamp does not match the unique browser zone/)
  121. })
  122. it('reports browser-zone timestamp formatter failures as invariant violations', async () => {
  123. const ctx = await setup()
  124. const policy = 'Browser time zone for this request: Asia/Shanghai. '
  125. + 'Interpret otherwise-unqualified dates and times in this zone.'
  126. const formatToParts = vi.spyOn(Intl.DateTimeFormat.prototype, 'formatToParts')
  127. .mockImplementationOnce(() => { throw new RangeError('formatter unavailable') })
  128. try {
  129. expect(() => {
  130. ctx.emit('session/event', preparing(1, 1, 'Asia/Shanghai'), event(reading(
  131. '1',
  132. '1',
  133. 'model-visible message',
  134. '2026-07-14T08:00:00+08:00[Asia/Shanghai]',
  135. policy,
  136. )))
  137. }).toThrow(/browser zone cannot format its durable timestamp: RangeError: formatter unavailable/)
  138. } finally {
  139. formatToParts.mockRestore()
  140. }
  141. })
  142. it('rejects invalid browser provenance loaded across the durable boundary', async () => {
  143. const ctx = await setup()
  144. const timeZone = 'Not/A_Real_Zone'
  145. const policy = `Browser time zone for this request: ${timeZone}. `
  146. + 'Interpret otherwise-unqualified dates and times in this zone.'
  147. expect(() => {
  148. ctx.emit('session/event', preparing(1, 1, timeZone), event(reading(
  149. '1',
  150. '1',
  151. 'model-visible message',
  152. `2026-07-14T00:00:00+00:00[${timeZone}]`,
  153. policy,
  154. )))
  155. }).toThrow(/browser time zone is unsupported/)
  156. })
  157. it('rejects one corrupt zone even when another zone would classify the turn as mixed', async () => {
  158. const ctx = await setup()
  159. const session = preparing(1, 1, 'Asia/Shanghai')
  160. session.append('user/message', createUserMessage({
  161. content: [{ type: 'text', text: 'second browser prompt' }],
  162. source: {
  163. kind: 'user',
  164. rpcId: 'turn-1-invalid',
  165. clientTimeZone: 'Not/A_Real_Zone',
  166. } as never,
  167. }), { surfaceOp: 'append' })
  168. expect(() => {
  169. ctx.emit('session/event', session, event(reading(
  170. '1',
  171. '1',
  172. 'model-visible message',
  173. '2026-07-14T00:00:00+00:00[UTC]',
  174. 'Browser time zone for this request: mixed ["Asia/Shanghai","Not/A_Real_Zone"]. '
  175. + 'Ask the user to clarify otherwise-unqualified dates and times.',
  176. )))
  177. }).toThrow(/browser time zone is unsupported/)
  178. })
  179. it('validates each existing reading against its preceding durable prefix', async () => {
  180. const ctx = new Context()
  181. await ctx.plugin(SessionStore)
  182. const session = ctx.sessions.create(SessionId('time-invariant-late-valid'))
  183. session.append('turn/start', { turn: 1 })
  184. session.append('step/start', { turn: 1, step: 1 })
  185. session.append('user/message', createUserMessage({
  186. content: [{ type: 'text', text: 'prepare' }],
  187. source: { kind: 'user' },
  188. }), { surfaceOp: 'append' })
  189. appendReading(session, reading())
  190. await ctx.plugin(InvariantRegistry, { enabled: true })
  191. await expect(ctx.plugin(TimeInvariant)).resolves.toBeDefined()
  192. })
  193. it('rejects an invalid existing reading on late registration', async () => {
  194. const ctx = new Context()
  195. await ctx.plugin(SessionStore)
  196. const session = ctx.sessions.create(SessionId('time-invariant-late-invalid'))
  197. session.append('turn/start', { turn: 1 })
  198. session.append('step/start', { turn: 1, step: 1 })
  199. session.append('user/message', createUserMessage({
  200. content: [{ type: 'text', text: 'prepare' }],
  201. source: { kind: 'user' },
  202. }), { surfaceOp: 'append' })
  203. appendReading(session, reading('1', '2', 'step context'))
  204. await ctx.plugin(InvariantRegistry, { enabled: true })
  205. await expect(ctx.plugin(TimeInvariant).then(() => undefined)).rejects.toThrow(/expected turn 1\/step 1/)
  206. })
  207. it.each([
  208. [reading('1', '3', 'step context'), /expected turn 2\/step 3/],
  209. [reading('2', '2', 'step context'), /expected turn 2\/step 3/],
  210. ])('rejects a reading that disagrees with its session position', async (text, message) => {
  211. const ctx = await setup()
  212. expect(() => { ctx.emit('session/event', preparing(2, 3), event(text)) }).toThrow(message)
  213. })
  214. it('rejects a reading after cancellation closes the turn', async () => {
  215. const ctx = await setup()
  216. const session = preparing(1, 2)
  217. session.append('turn/end', { turn: 1, reason: { kind: 'aborted', reason: { kind: 'user' } } })
  218. expect(() => { ctx.emit('session/event', session, event(reading('1', '2', 'step context'))) })
  219. .toThrow(/inside an open turn/)
  220. })
  221. it('rejects a reading outside prompt assembly', async () => {
  222. const ctx = await setup()
  223. const ended = preparing(1, 1)
  224. ended.append('step/end', { turn: 1, step: 1 })
  225. expect(() => { ctx.emit('session/event', ended, event(reading())) }).toThrow(/follow step\/start/)
  226. const notEntered = Session.create(SessionId('time-invariant-turn-only'))
  227. notEntered.append('turn/start', { turn: 1 })
  228. expect(() => { ctx.emit('session/event', notEntered, event(reading())) }).toThrow(/follow step\/start/)
  229. expect(() => {
  230. ctx.emit('session/event', Session.create(SessionId('time-invariant-empty')), event(reading()))
  231. }).toThrow(/inside an open turn/)
  232. const requested = preparing(1, 1)
  233. requested.append('request/header', {
  234. header: { config: { provider: 'mock', model: 'model' } },
  235. reason: 'initial',
  236. })
  237. expect(() => { ctx.emit('session/event', requested, event(reading())) }).toThrow(/precede request\/header/)
  238. })
  239. it.each([
  240. ['not a reading', SECOND, undefined, /durable reading format/],
  241. [reading('0'), SECOND, undefined, /positive safe integers/],
  242. [reading('999999999999999999999'), SECOND, undefined, /positive safe integers/],
  243. [reading('1', '0', 'step context'), SECOND, undefined, /positive safe integers/],
  244. [reading('1', '999999999999999999999', 'step context'), SECOND, undefined, /positive safe integers/],
  245. [reading('1', '1', 'step context'), SECOND, undefined, /wrong elapsed-time baseline/],
  246. [reading('1', '2', 'model-visible message'), SECOND, undefined, /wrong elapsed-time baseline/],
  247. [reading('1', '1', 'model-visible message', '2026-99-99T00:00:00+00:00[UTC]'), SECOND, undefined, /must parse and not postdate/],
  248. [reading(), Number.NaN, undefined, /must parse and not postdate/],
  249. [reading(), SECOND - 1, undefined, /must parse and not postdate/],
  250. ['ignored', SECOND, [], /exactly one text block/],
  251. ['ignored', SECOND, [{ type: 'image', data: 'x', mimeType: 'image/png' }], /exactly one text block/],
  252. ['ignored', SECOND, [{ type: 'text', text: 'one' }, { type: 'text', text: 'two' }], /exactly one text block/],
  253. [reading(), SECOND, [{ type: 'text', text: reading(), extra: true }], /exactly one text block/],
  254. ] as const)('rejects an incoherent durable reading', async (text, time, content, message) => {
  255. const ctx = await setup()
  256. const preparationStep = text.includes('turn 1, step 2:') ? 2 : 1
  257. expect(() => {
  258. ctx.emit('session/event', preparing(1, preparationStep), event(
  259. text,
  260. time,
  261. content === undefined ? undefined : [...content],
  262. ))
  263. }).toThrow(message)
  264. })
  265. it('requires exact snapshot provenance without copied request authority', async () => {
  266. const ctx = await setup()
  267. const base = event(reading())
  268. for (const source of [
  269. { kind: 'plugin', plugin: 'time-context' },
  270. { ...base.data.source, authority: {} },
  271. {
  272. kind: 'plugin',
  273. plugin: 'time-context',
  274. form: 'snapshot',
  275. sections: [{ name: 'time-context', text: 'different' }],
  276. },
  277. {
  278. kind: 'plugin',
  279. plugin: 'time-context',
  280. form: 'snapshot',
  281. sections: { 0: { name: 'time-context', text: reading() }, length: 1 },
  282. },
  283. {
  284. kind: 'plugin',
  285. plugin: 'time-context',
  286. form: 'snapshot',
  287. sections: [{ name: 'time-context', text: reading(), extra: true }],
  288. },
  289. ]) {
  290. const malformed: SessionEvent<'user/message'> = {
  291. ...base,
  292. data: { ...base.data, source: source as never },
  293. }
  294. expect(() => { ctx.emit('session/event', preparing(1, 1), malformed) })
  295. .toThrow(/must carry only the exact snapshot text/)
  296. }
  297. })
  298. it('validates a seeded Session created after invariant registration', async () => {
  299. const ctx = await setup()
  300. const text = reading('1', '2', 'step context')
  301. expect(() => {
  302. ctx.sessions.create(SessionId('time-invariant-created-invalid'), {
  303. seed: [
  304. { type: 'turn/start', seq: SessionSeq(0), time: SECOND, data: { turn: 1 } },
  305. { type: 'step/start', seq: SessionSeq(1), time: SECOND, data: { turn: 1, step: 1 } },
  306. { ...event(text), seq: SessionSeq(2), surfaceOp: 'append' },
  307. ],
  308. })
  309. }).toThrow(/expected turn 1\/step 1/)
  310. expect(ctx.sessions.get(SessionId('time-invariant-created-invalid'))).toBeUndefined()
  311. })
  312. it('ignores context messages owned by another package', async () => {
  313. const ctx = await setup()
  314. const other = event('unrelated', SECOND + 456, undefined, 'other')
  315. expect(() => { ctx.emit('session/event', preparing(1, 1), other) }).not.toThrow()
  316. const user: SessionEvent<'user/message'> = {
  317. ...event('unrelated'),
  318. data: createUserMessage({
  319. content: [{ type: 'text', text: 'unrelated' }],
  320. source: { kind: 'user' },
  321. }),
  322. }
  323. expect(() => { ctx.emit('session/event', preparing(1, 1), user) }).not.toThrow()
  324. expect(() => {
  325. ctx.emit('session/event', preparing(1, 1), {
  326. type: 'turn/start', seq: SessionSeq(0), time: 0, data: { turn: 1 },
  327. })
  328. ctx.emit('tools/change')
  329. }).not.toThrow()
  330. })
  331. })