sqlite-integration.spec.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import { afterEach, describe, expect, it } from 'vitest'
  2. import { Context } from '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 SessionPersistenceJsonl from '@deepseek-ai/dsh-session-persistence-jsonl'
  14. import SessionQuerySqlite from '@deepseek-ai/dsh-session-query-sqlite'
  15. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  16. import ToolRegistry 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', 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(ToolRegistry)
  38. await ctx.plugin(SessionPersistenceJsonl, { root, compression: 'none' })
  39. await ctx.plugin(SessionQuerySqlite, { 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(ToolRegistry)
  102. await ctx.plugin(SessionPersistenceJsonl, { root, compression: 'none' })
  103. await ctx.plugin(SessionQuerySqlite, { 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. const caller = ctx.sessions.create(SessionId('fractional-caller'), {
  136. meta: { createdAt: base + 1_000, cwd: '/work' },
  137. })
  138. let call = 0
  139. const execute = (args: unknown) => ctx.tools.execute({
  140. name: 'session_event_search',
  141. arguments: args,
  142. callId: CallId(`fractional-integration-${++call}`),
  143. signal: new AbortController().signal,
  144. agent: fakeAgent(caller),
  145. })
  146. const lowerBound = await execute({
  147. session_id: persisted,
  148. query: 'fractional integration needle',
  149. time_from: '2026-07-24T00:00:00.12300001Z',
  150. })
  151. expect(lowerBound.isError).toBe(false)
  152. const lowerText = lowerBound.content.map(block => block.type === 'text' ? block.text : '').join('\n')
  153. expect(lowerText).toContain('seq 1')
  154. expect(lowerText).not.toContain('seq 0')
  155. const upperBound = await execute({
  156. session_id: persisted,
  157. query: 'fractional integration needle',
  158. time_to: '2026-07-24T08:00:00.1239999+08:00',
  159. })
  160. expect(upperBound.isError).toBe(false)
  161. const upperText = upperBound.content.map(block => block.type === 'text' ? block.text : '').join('\n')
  162. expect(upperText).toContain('seq 0')
  163. expect(upperText).not.toContain('seq 1')
  164. const emptySameMillisecond = await execute({
  165. session_id: persisted,
  166. query: 'fractional integration needle',
  167. time_from: '2026-07-24T00:00:00.12300001Z',
  168. time_to: '2026-07-24T08:00:00.1239999+08:00',
  169. })
  170. expect(emptySameMillisecond.isError).toBe(false)
  171. expect(emptySameMillisecond.content.map(block => block.type === 'text' ? block.text : '').join('\n'))
  172. .toContain('No prior event matches found.')
  173. })
  174. })