api-proxy-cold.spec.ts 4.1 KB

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