invariant.spec.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { describe, expect, it } from 'vitest'
  2. import { Context } from 'cordis'
  3. import SessionStore, { SessionId } from '@deepseek-ai/dsh-session'
  4. import InvariantService from '@deepseek-ai/dsh-invariants'
  5. import * as AgentLoopInvariant from '@deepseek-ai/dsh-agent-loop/invariant'
  6. import { markAgentLoopRequest, type GenerateOptions } from '@deepseek-ai/dsh-llm'
  7. async function setup(): Promise<Context> {
  8. const ctx = new Context()
  9. await ctx.plugin(SessionStore)
  10. await ctx.plugin(InvariantService)
  11. await ctx.plugin(AgentLoopInvariant)
  12. return ctx
  13. }
  14. function dispatch(ctx: Context, options: unknown): void {
  15. void ctx.waterfall('llm/stream', options as never, () => (async function* () {})() as never)
  16. }
  17. function loopRequest<T extends object>(options: T): Readonly<T> {
  18. markAgentLoopRequest(options as GenerateOptions)
  19. return Object.freeze(options)
  20. }
  21. async function requestSetup() {
  22. const ctx = await setup()
  23. const session = ctx.sessions.create(SessionId('req-check'))
  24. session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  25. session.append('user/message', { content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  26. const boundary = session.deriveMessages()
  27. session.append('step/start', { turn: 1, step: 1 })
  28. session.append('request/header', { header: { config: { provider: 'mock', model: 'm' } }, reason: 'initial' })
  29. return { ctx, session, boundary }
  30. }
  31. describe('request-reconstruction invariant', () => {
  32. it('accepts a frozen request equal to the boundary derivation and folded header', async () => {
  33. const { ctx, session, boundary } = await requestSetup()
  34. const options = loopRequest({ model: 'm', messages: Object.freeze(boundary), sessionId: session.id })
  35. expect(() => { dispatch(ctx, options) }).not.toThrow()
  36. })
  37. it('uses the step boundary rather than content appended afterward', async () => {
  38. const { ctx, session, boundary } = await requestSetup()
  39. session.append('user/message', { content: [{ type: 'text', text: '[late]' }], source: { kind: 'plugin', plugin: 'x' } }, { surfaceOp: 'append' })
  40. const options = loopRequest({ model: 'm', messages: Object.freeze(boundary), sessionId: session.id })
  41. expect(() => { dispatch(ctx, options) }).not.toThrow()
  42. })
  43. it('requires the folded session prefix ahead of derived history', async () => {
  44. const { ctx, session, boundary } = await requestSetup()
  45. const prefix = { role: 'user' as const, content: [{ type: 'text' as const, text: '<system-reminder>catalog</system-reminder>' }] }
  46. session.append('request/header', { header: { config: { provider: 'mock', model: 'm' }, messagePrefix: [prefix] }, reason: 'change' })
  47. expect(() => { dispatch(ctx, loopRequest({ model: 'm', messages: Object.freeze([prefix, ...boundary]), sessionId: session.id })) })
  48. .not.toThrow()
  49. expect(() => { dispatch(ctx, loopRequest({ model: 'm', messages: Object.freeze([...boundary]), sessionId: session.id })) })
  50. .toThrow(/diverges from the boundary derivation/)
  51. expect(() => { dispatch(ctx, loopRequest({ model: 'm', messages: Object.freeze([...boundary, prefix]), sessionId: session.id })) })
  52. .toThrow(/diverges from the boundary derivation/)
  53. })
  54. it('rejects message and header divergence', async () => {
  55. const { ctx, session, boundary } = await requestSetup()
  56. const divergent = [...boundary, { role: 'user', content: [{ type: 'text', text: 'phantom' }] }]
  57. expect(() => { dispatch(ctx, loopRequest({ model: 'm', messages: Object.freeze(divergent), sessionId: session.id })) })
  58. .toThrow(/diverges from the boundary derivation/)
  59. expect(() => { dispatch(ctx, loopRequest({ model: 'other', messages: Object.freeze(boundary), sessionId: session.id })) })
  60. .toThrow(/diverges from the folded request header/)
  61. })
  62. it('rejects loop requests with no boundary or header', async () => {
  63. const ctx = await setup()
  64. const session = ctx.sessions.create(SessionId('req-bare'))
  65. session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  66. const bare = loopRequest({ model: 'm', messages: Object.freeze([]), sessionId: session.id })
  67. expect(() => { dispatch(ctx, bare) }).toThrow(/no step\/start/)
  68. session.append('step/start', { turn: 1, step: 1 })
  69. expect(() => { dispatch(ctx, bare) }).toThrow(/no request\/header event/)
  70. })
  71. it('rejects an unfrozen messages array but skips requests outside the loop contract', async () => {
  72. const { ctx, session, boundary } = await requestSetup()
  73. expect(() => { dispatch(ctx, loopRequest({ model: 'm', messages: [...boundary], sessionId: session.id })) })
  74. .toThrow(/frozen messages array/)
  75. expect(() => { dispatch(ctx, { model: 'summarizer', messages: [], sessionId: session.id }) }).not.toThrow()
  76. expect(() => { dispatch(ctx, Object.freeze({ model: 'm', messages: Object.freeze([]) })) }).not.toThrow()
  77. expect(() => { dispatch(ctx, Object.freeze({ model: 'm', messages: Object.freeze([]), sessionId: SessionId('ghost') })) })
  78. .not.toThrow()
  79. const directSession = ctx.sessions.create(SessionId('direct-one-shot'))
  80. expect(() => {
  81. dispatch(ctx, Object.freeze({ model: 'one-shot', messages: Object.freeze([]), sessionId: directSession.id }))
  82. }).not.toThrow()
  83. })
  84. it('rejects malformed requests carrying the loop marker', async () => {
  85. const { ctx, session } = await requestSetup()
  86. const messages: GenerateOptions['messages'] = []
  87. Object.freeze(messages)
  88. expect(() => {
  89. dispatch(ctx, markAgentLoopRequest({ provider: 'p', model: 'm', messages, sessionId: session.id }))
  90. }).toThrow(/request must be frozen/)
  91. expect(() => {
  92. dispatch(ctx, loopRequest({ model: 'm', messages: Object.freeze([]) }))
  93. }).toThrow(/carry a session id/)
  94. expect(() => {
  95. dispatch(ctx, loopRequest({
  96. model: 'm',
  97. messages: Object.freeze([]),
  98. sessionId: SessionId('missing-loop-session'),
  99. }))
  100. }).toThrow(/live session id/)
  101. })
  102. it('prepends ahead of a short-circuiting stream listener', async () => {
  103. const ctx = new Context()
  104. await ctx.plugin(SessionStore)
  105. ctx.on('llm/stream', () => (async function* () {})() as never)
  106. await ctx.plugin(InvariantService)
  107. await ctx.plugin(AgentLoopInvariant)
  108. const session = ctx.sessions.create(SessionId('prepend-check'))
  109. session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  110. session.append('user/message', { content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  111. session.append('step/start', { turn: 1, step: 1 })
  112. session.append('request/header', { header: { config: { provider: 'mock', model: 'm' } }, reason: 'initial' })
  113. const divergent = loopRequest({
  114. model: 'm',
  115. messages: Object.freeze([{ role: 'user', content: [{ type: 'text', text: 'phantom' }] }]),
  116. sessionId: session.id,
  117. })
  118. expect(() => { dispatch(ctx, divergent) }).toThrow(/diverges from the boundary derivation/)
  119. })
  120. })