properties.spec.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * Property-based protocol-shape tests for the ACP update stream (RFC 001 → ADR 0013
  3. * precedent). Fuzz arbitrary harness `SessionEvent` sequences through the pure
  4. * `streamSessionEventUpdate` translator and assert legal update variants, call-before-result order
  5. * per tool id, and deterministic event-to-update translation. Keeping this pure makes live and
  6. * replay equivalence deterministic rather than a timing property.
  7. */
  8. import { describe, expect, it } from 'vitest'
  9. import fc from 'fast-check'
  10. import { CallId } from '@deepseek-ai/dsh-llm'
  11. import { SessionId, type SessionEvent } from '@deepseek-ai/dsh-session'
  12. import type { SessionNotification } from '@agentclientprotocol/sdk'
  13. import { streamSessionEventUpdate } from '../src/index.ts'
  14. const LEGAL_UPDATE_KINDS = new Set([
  15. 'agent_message_chunk',
  16. 'agent_thought_chunk',
  17. 'tool_call',
  18. 'tool_call_update',
  19. ])
  20. /**
  21. * Build a WELL-FORMED harness event sequence: a list of "actions" where a tool
  22. * result can only reference a call already opened earlier. This mirrors what
  23. * the loop actually appends (tool/call always precedes its tool/result), so the
  24. * ordering invariant is asserted over realistic logs, not arbitrary noise.
  25. */
  26. type Action =
  27. | { kind: 'text'; text: string }
  28. | { kind: 'reasoning'; text: string }
  29. | { kind: 'call'; id: string; name: string }
  30. | { kind: 'result'; idx: number; isError: boolean }
  31. | { kind: 'ignored' }
  32. function actionsArb(): fc.Arbitrary<Action[]> {
  33. const action: fc.Arbitrary<Action> = fc.oneof(
  34. fc.string().map((text): Action => ({ kind: 'text', text })),
  35. fc.string().map((text): Action => ({ kind: 'reasoning', text })),
  36. fc.record({ id: fc.string({ minLength: 1 }), name: fc.string() }).map(({ id, name }): Action => ({ kind: 'call', id, name })),
  37. fc.record({ idx: fc.nat(), isError: fc.boolean() }).map(({ idx, isError }): Action => ({ kind: 'result', idx, isError })),
  38. fc.constant<Action>({ kind: 'ignored' }),
  39. )
  40. return fc.array(action, { maxLength: 30 })
  41. }
  42. /** Lower well-formed actions into a harness event sequence. */
  43. function actionsToEvents(actions: Action[]): SessionEvent[] {
  44. const events: SessionEvent[] = []
  45. const openCalls: string[] = []
  46. for (const a of actions) {
  47. switch (a.kind) {
  48. case 'text':
  49. events.push({ type: 'assistant/chunk', seq: 0, time: 0, data: { turn: 1, step: 1, chunk: { type: 'text-delta', index: 0, text: a.text } } })
  50. break
  51. case 'reasoning':
  52. events.push({ type: 'assistant/chunk', seq: 0, time: 0, data: { turn: 1, step: 1, chunk: { type: 'reasoning-delta', index: 0, text: a.text } } })
  53. break
  54. case 'call':
  55. openCalls.push(a.id)
  56. events.push({ type: 'tool/call', seq: 0, time: 0, data: { turn: 1, step: 1, callId: CallId(a.id), name: a.name, arguments: '{}' } })
  57. break
  58. case 'result': {
  59. // Only emit a result for an already-opened call (well-formedness).
  60. if (openCalls.length === 0) break
  61. const id = openCalls[a.idx % openCalls.length]!
  62. events.push({ type: 'tool/result', seq: 0, time: 0, data: { turn: 1, step: 1, callId: CallId(id), content: [], isError: a.isError } })
  63. break
  64. }
  65. case 'ignored':
  66. events.push({ type: 'turn/end', seq: 0, time: 0, data: { turn: 1, reason: { kind: 'completed' } } })
  67. break
  68. }
  69. }
  70. return events
  71. }
  72. function runStream(events: SessionEvent[]): SessionNotification['update'][] {
  73. const out: SessionNotification['update'][] = []
  74. for (const event of events) streamSessionEventUpdate(SessionId('s1'), event, n => out.push(n.update))
  75. return out
  76. }
  77. describe('ACP update-stream invariants (property-based)', () => {
  78. it('every emitted update is a legal SessionUpdate variant', () => {
  79. fc.assert(fc.property(actionsArb(), (actions) => {
  80. for (const update of runStream(actionsToEvents(actions))) {
  81. expect(LEGAL_UPDATE_KINDS.has(update.sessionUpdate)).toBe(true)
  82. }
  83. }))
  84. })
  85. it('never emits a tool_call_update for an id before that id\'s tool_call', () => {
  86. fc.assert(fc.property(actionsArb(), (actions) => {
  87. const seenCall = new Set<string>()
  88. for (const update of runStream(actionsToEvents(actions))) {
  89. if (update.sessionUpdate === 'tool_call') {
  90. seenCall.add(update.toolCallId)
  91. } else if (update.sessionUpdate === 'tool_call_update') {
  92. expect(seenCall.has(update.toolCallId)).toBe(true)
  93. }
  94. }
  95. }))
  96. })
  97. it('is a pure function of the event (replay equals live)', () => {
  98. fc.assert(fc.property(actionsArb(), (actions) => {
  99. const events = actionsToEvents(actions)
  100. expect(runStream(events)).toEqual(runStream(events))
  101. }))
  102. })
  103. })