sqlite-integration.spec.ts 8.2 KB

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