sqlite-integration.spec.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import { afterEach, describe, expect, it } from 'vitest'
  2. import { Context } from '@deepseek-ai/cordis'
  3. import { mkdtemp, rm } from 'node:fs/promises'
  4. import { tmpdir } from 'node:os'
  5. import { join } from 'node:path'
  6. import type { Agent } from '@deepseek-ai/dsh-agent'
  7. import { createUserMessage, ToolCallId } from '@deepseek-ai/dsh-llm'
  8. import SessionStore, {
  9. SESSION_FORMAT_VERSION,
  10. SessionId,
  11. SessionSeq,
  12. type Session,
  13. } from '@deepseek-ai/dsh-session'
  14. import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection'
  15. import { turnBoundaryProjectionDefinition } from '@deepseek-ai/dsh-agent-loop'
  16. import JsonlSessionPersistence from '@deepseek-ai/dsh-session-persistence-jsonl'
  17. import SqliteSessionQueryEngine from '@deepseek-ai/dsh-session-query-sqlite'
  18. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  19. import ToolRuntime from '@deepseek-ai/dsh-tools'
  20. import * as ToolSessionQuery from '@deepseek-ai/dsh-tool-session-query'
  21. const temporaryDirectories: string[] = []
  22. const contexts: Context[] = []
  23. afterEach(async () => {
  24. for (const ctx of contexts.splice(0)) await ctx.fiber.dispose()
  25. for (const directory of temporaryDirectories.splice(0)) {
  26. await rm(directory, { recursive: true, force: true })
  27. }
  28. })
  29. function fakeAgent(session: Session): Agent {
  30. return { id: session.id, session } as unknown as Agent
  31. }
  32. function registerTurnBoundary(ctx: Context): void {
  33. ctx.sessionProjections.register(turnBoundaryProjectionDefinition)
  34. }
  35. describe('tool-session-query with the real SQLite provider', () => {
  36. it('searches live prior-step history and a persisted same-workspace log', { timeout: 20_000 }, async () => {
  37. const root = await mkdtemp(join(tmpdir(), 'dsh-tool-session-query-'))
  38. temporaryDirectories.push(root)
  39. const ctx = new Context()
  40. contexts.push(ctx)
  41. await ctx.plugin(SessionStore)
  42. await ctx.plugin(SessionProjectionRegistry)
  43. registerTurnBoundary(ctx)
  44. await ctx.plugin(SystemPrompt)
  45. await ctx.plugin(ToolRuntime)
  46. await ctx.plugin(JsonlSessionPersistence, { root, compression: 'none' })
  47. await ctx.plugin(SqliteSessionQueryEngine, { path: join(root, 'session-query.db') })
  48. await ctx.plugin(ToolSessionQuery)
  49. const persisted = SessionId('persisted')
  50. await ctx.sessionPersistence.create({
  51. version: SESSION_FORMAT_VERSION,
  52. id: persisted,
  53. createdAt: 1,
  54. cwd: '/work',
  55. isSeeded: false,
  56. })
  57. await ctx.sessionPersistence.append(persisted, [{
  58. type: 'user/message',
  59. seq: SessionSeq(0),
  60. time: 2,
  61. data: createUserMessage({
  62. content: [{ type: 'text', text: 'persisted integration needle' }],
  63. source: { kind: 'user' },
  64. }),
  65. surfaceOp: 'append',
  66. }])
  67. const caller = ctx.sessions.create(SessionId('caller'), {
  68. meta: { createdAt: 10, cwd: '/work' },
  69. })
  70. caller.append('turn/start', { turn: 1 })
  71. caller.append(
  72. 'user/message',
  73. createUserMessage({
  74. content: [{ type: 'text', text: 'live integration needle' }], source: { kind: 'user' },
  75. }),
  76. { surfaceOp: 'append' },
  77. )
  78. caller.append('step/start', { turn: 1, step: 1 })
  79. let call = 0
  80. const execute = (name: string, args: unknown) => ctx.tools.execute({
  81. name,
  82. arguments: args,
  83. callId: ToolCallId(`integration-${++call}`),
  84. signal: new AbortController().signal,
  85. agent: fakeAgent(caller),
  86. })
  87. const sessions = await execute('session_search', { query: 'persisted integration needle' })
  88. expect(sessions.isError).toBe(false)
  89. expect(sessions.content.map(block => block.type === 'text' ? block.text : '').join('\n'))
  90. .toContain('Session persisted')
  91. const persistedEvents = await execute('session_event_search', {
  92. session_id: persisted,
  93. query: 'persisted integration needle',
  94. })
  95. expect(persistedEvents.isError).toBe(false)
  96. expect(persistedEvents.content.map(block => block.type === 'text' ? block.text : '').join('\n'))
  97. .toContain('seq 0')
  98. const liveEvents = await execute('session_event_search', { query: 'live integration needle' })
  99. expect(liveEvents.isError).toBe(false)
  100. expect(liveEvents.content.map(block => block.type === 'text' ? block.text : '').join('\n'))
  101. .toContain('seq 1')
  102. })
  103. it('passes finite fractional epoch-millisecond bounds through SQLite comparisons', async () => {
  104. const root = await mkdtemp(join(tmpdir(), 'dsh-tool-session-query-fractional-'))
  105. temporaryDirectories.push(root)
  106. const ctx = new Context()
  107. contexts.push(ctx)
  108. await ctx.plugin(SessionStore)
  109. await ctx.plugin(SessionProjectionRegistry)
  110. registerTurnBoundary(ctx)
  111. await ctx.plugin(SystemPrompt)
  112. await ctx.plugin(ToolRuntime)
  113. await ctx.plugin(JsonlSessionPersistence, { root, compression: 'none' })
  114. await ctx.plugin(SqliteSessionQueryEngine, { path: join(root, 'session-query.db') })
  115. await ctx.plugin(ToolSessionQuery)
  116. const base = Date.parse('2026-07-24T00:00:00.000Z')
  117. const persisted = SessionId('fractional-persisted')
  118. await ctx.sessionPersistence.create({
  119. version: SESSION_FORMAT_VERSION,
  120. id: persisted,
  121. createdAt: base,
  122. cwd: '/work',
  123. isSeeded: false,
  124. })
  125. await ctx.sessionPersistence.append(persisted, [
  126. {
  127. type: 'user/message',
  128. seq: SessionSeq(0),
  129. time: base + 123,
  130. data: createUserMessage({
  131. content: [{ type: 'text', text: 'fractional integration needle' }],
  132. source: { kind: 'user' },
  133. }),
  134. surfaceOp: 'append',
  135. },
  136. {
  137. type: 'user/message',
  138. seq: SessionSeq(1),
  139. time: base + 124,
  140. data: createUserMessage({
  141. content: [{ type: 'text', text: 'fractional integration needle' }],
  142. source: { kind: 'user' },
  143. }),
  144. surfaceOp: 'append',
  145. },
  146. {
  147. type: 'user/message',
  148. seq: SessionSeq(2),
  149. time: -124,
  150. data: createUserMessage({
  151. content: [{ type: 'text', text: 'pre-epoch fractional needle' }],
  152. source: { kind: 'user' },
  153. }),
  154. surfaceOp: 'append',
  155. },
  156. {
  157. type: 'user/message',
  158. seq: SessionSeq(3),
  159. time: -123,
  160. data: createUserMessage({
  161. content: [{ type: 'text', text: 'pre-epoch fractional needle' }],
  162. source: { kind: 'user' },
  163. }),
  164. surfaceOp: 'append',
  165. },
  166. ])
  167. const caller = ctx.sessions.create(SessionId('fractional-caller'), {
  168. meta: { createdAt: base + 1_000, cwd: '/work' },
  169. })
  170. let call = 0
  171. const execute = (args: unknown) => ctx.tools.execute({
  172. name: 'session_event_search',
  173. arguments: args,
  174. callId: ToolCallId(`fractional-integration-${++call}`),
  175. signal: new AbortController().signal,
  176. agent: fakeAgent(caller),
  177. })
  178. const lowerBound = await execute({
  179. session_id: persisted,
  180. query: 'fractional integration needle',
  181. time_from: '2026-07-24T00:00:00.12300001Z',
  182. })
  183. expect(lowerBound.isError).toBe(false)
  184. const lowerText = lowerBound.content.map(block => block.type === 'text' ? block.text : '').join('\n')
  185. expect(lowerText).toContain('seq 1')
  186. expect(lowerText).not.toContain('seq 0')
  187. const upperBound = await execute({
  188. session_id: persisted,
  189. query: 'fractional integration needle',
  190. time_to: '2026-07-24T08:00:00.1239999+08:00',
  191. })
  192. expect(upperBound.isError).toBe(false)
  193. const upperText = upperBound.content.map(block => block.type === 'text' ? block.text : '').join('\n')
  194. expect(upperText).toContain('seq 0')
  195. expect(upperText).not.toContain('seq 1')
  196. const emptySameMillisecond = await execute({
  197. session_id: persisted,
  198. query: 'fractional integration needle',
  199. time_from: '2026-07-24T00:00:00.12300001Z',
  200. time_to: '2026-07-24T08:00:00.1239999+08:00',
  201. })
  202. expect(emptySameMillisecond.isError).toBe(false)
  203. expect(emptySameMillisecond.content.map(block => block.type === 'text' ? block.text : '').join('\n'))
  204. .toContain('No prior event matches found.')
  205. const preEpochLower = await execute({
  206. session_id: persisted,
  207. query: 'pre-epoch fractional needle',
  208. time_from: '1969-12-31T23:59:59.87600001Z',
  209. })
  210. expect(preEpochLower.isError).toBe(false)
  211. const preEpochLowerText = preEpochLower.content
  212. .map(block => block.type === 'text' ? block.text : '').join('\n')
  213. expect(preEpochLowerText).toContain('seq 3')
  214. expect(preEpochLowerText).not.toContain('seq 2')
  215. const preEpochUpper = await execute({
  216. session_id: persisted,
  217. query: 'pre-epoch fractional needle',
  218. time_to: '1969-12-31T19:59:59.8769999-04:00',
  219. })
  220. expect(preEpochUpper.isError).toBe(false)
  221. const preEpochUpperText = preEpochUpper.content
  222. .map(block => block.type === 'text' ? block.text : '').join('\n')
  223. expect(preEpochUpperText).toContain('seq 2')
  224. expect(preEpochUpperText).not.toContain('seq 3')
  225. })
  226. })