api-proxy-cold.spec.ts 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * Cold-session and degenerate-composition paths of the host ApiProxy:
  3. * sessions.list merging persisted-but-unattached summaries (mtime source,
  4. * createdAt fallbacks, lineage projection) and the resume error split when
  5. * the composition has no persistence gate and no agent factory.
  6. */
  7. import { mkdtempSync, writeFileSync, utimesSync } from 'node:fs'
  8. import { tmpdir } from 'node:os'
  9. import { join } from 'node:path'
  10. import { describe, expect, it } from 'vitest'
  11. import { Context } from 'cordis'
  12. import SessionStore from '@deepseek-ai/dsh-session'
  13. import AgentRegistry from '@deepseek-ai/dsh-agent'
  14. import UserInteractionService from '@deepseek-ai/dsh-user-interaction'
  15. import type { SessionHeader, SessionId } from '@deepseek-ai/dsh-session'
  16. import type { RpcRequest } from '@deepseek-ai/dsh-host-apiproxy/api/rpc'
  17. import { RpcId } from '@deepseek-ai/dsh-host-apiproxy/api/rpc'
  18. import { createApiProxy } from '@deepseek-ai/dsh-host-apiproxy'
  19. const sid = (id: string): SessionId => id as SessionId
  20. let nextRpc = 1
  21. function request<P>(payload: P): RpcRequest<P> {
  22. return { rpcId: RpcId(`cold-${String(nextRpc++)}`), payload }
  23. }
  24. function header(id: string, createdAt: number, extra: Partial<SessionHeader> = {}): SessionHeader {
  25. return { version: 0, id: sid(id), createdAt, cwd: '/proj', ...extra }
  26. }
  27. describe('sessions.list cold merge', () => {
  28. it('summarizes unattached sessions: log mtime, locate-less and vanished-log createdAt fallbacks, lineage', async () => {
  29. const ctx = new Context()
  30. await ctx.plugin(SessionStore)
  31. await ctx.plugin(UserInteractionService)
  32. const root = mkdtempSync(join(tmpdir(), 'dsh-cold-'))
  33. const logPath = join(root, 'a.log')
  34. writeFileSync(logPath, 'log-bytes')
  35. utimesSync(logPath, 5000, 5000) // mtime 5_000_000 ms — newer than every createdAt below
  36. const metas = [
  37. header('session-a', 1000),
  38. header('session-b', 2000, { parentSession: sid('session-parent') }),
  39. header('session-c', 1500),
  40. ]
  41. // Structural fake of the persistence face list() consumes: list + locate.
  42. // locate: a real per-session file (mtime wins), a backend without one
  43. // (SQLite shape → createdAt), and a path whose file vanished (stat ENOENT
  44. // → createdAt).
  45. ctx.provide('sessionPersistence', {
  46. list: () => Promise.resolve(metas),
  47. locate: (meta: SessionHeader) => {
  48. if (meta.id === sid('session-a')) return { kind: 'jsonl', path: logPath }
  49. if (meta.id === sid('session-c')) return { kind: 'jsonl', path: join(root, 'vanished.log') }
  50. return undefined
  51. },
  52. })
  53. const api = createApiProxy(ctx, { provider: 'p', model: 'm', cwd: '/tmp', workspaceRoot: '/tmp' })
  54. const response = await api.sessions.list(request({}))
  55. expect(response.result.ok).toBe(true)
  56. if (!response.result.ok) throw new Error('unreachable')
  57. const items = response.result.value.items
  58. expect(items.map(item => item.sessionId)).toEqual(['session-a', 'session-b', 'session-c'])
  59. const [a, b, c] = items
  60. expect(a?.updatedAt).toBeCloseTo(5_000_000, -3)
  61. expect(a?.running).toBe(false)
  62. expect(a?.cwd).toBe('/proj')
  63. expect(a?.parentSessionId).toBeUndefined()
  64. expect(b?.updatedAt).toBe(2000)
  65. expect(b?.parentSessionId).toBe('session-parent')
  66. expect(c?.updatedAt).toBe(1500)
  67. })
  68. })
  69. describe('degenerate composition (no persistence, no factory)', () => {
  70. it('list skips the cold merge and resume maps a non-not-found failure to internal', async () => {
  71. const ctx = new Context()
  72. await ctx.plugin(SessionStore)
  73. await ctx.plugin(AgentRegistry)
  74. await ctx.plugin(UserInteractionService)
  75. const api = createApiProxy(ctx, { provider: 'p', model: 'm', cwd: '/tmp', workspaceRoot: '/tmp' })
  76. const listed = await api.sessions.list(request({}))
  77. expect(listed.result.ok).toBe(true)
  78. if (listed.result.ok) expect(listed.result.value.items).toEqual([])
  79. // No persistence → the servable gate passes silently; the factory-less
  80. // registry then rejects resume, which is NOT a SessionNotFound.
  81. const response = await api.sessions.history(request({ sessionId: sid('session-ghost') }))
  82. expect(response.result.ok).toBe(false)
  83. if (!response.result.ok) {
  84. expect(response.result.error.code).toBe('internal')
  85. expect(response.result.error.message).toMatch(/resume failed for session "session-ghost"/)
  86. }
  87. })
  88. })