invariant.spec.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 messages to equal the boundary derivation exactly (no unlogged prefix)', async () => {
  44. const { ctx, session, boundary } = await requestSetup()
  45. const extra = { role: 'user' as const, content: [{ type: 'text' as const, text: '<system-reminder>catalog</system-reminder>' }] }
  46. expect(() => { dispatch(ctx, loopRequest({ model: 'm', messages: Object.freeze([...boundary]), sessionId: session.id })) })
  47. .not.toThrow()
  48. expect(() => { dispatch(ctx, loopRequest({ model: 'm', messages: Object.freeze([extra, ...boundary]), sessionId: session.id })) })
  49. .toThrow(/diverges from the boundary derivation/)
  50. expect(() => { dispatch(ctx, loopRequest({ model: 'm', messages: Object.freeze([...boundary, extra]), sessionId: session.id })) })
  51. .toThrow(/diverges from the boundary derivation/)
  52. })
  53. it('rejects message and header divergence', async () => {
  54. const { ctx, session, boundary } = await requestSetup()
  55. const divergent = [...boundary, { role: 'user', content: [{ type: 'text', text: 'phantom' }] }]
  56. expect(() => { dispatch(ctx, loopRequest({ model: 'm', messages: Object.freeze(divergent), sessionId: session.id })) })
  57. .toThrow(/diverges from the boundary derivation/)
  58. expect(() => { dispatch(ctx, loopRequest({ model: 'other', messages: Object.freeze(boundary), sessionId: session.id })) })
  59. .toThrow(/diverges from the folded request header/)
  60. })
  61. it('rejects loop requests with no boundary or header', async () => {
  62. const ctx = await setup()
  63. const session = ctx.sessions.create(SessionId('req-bare'))
  64. session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  65. const bare = loopRequest({ model: 'm', messages: Object.freeze([]), sessionId: session.id })
  66. expect(() => { dispatch(ctx, bare) }).toThrow(/no step\/start/)
  67. session.append('step/start', { turn: 1, step: 1 })
  68. expect(() => { dispatch(ctx, bare) }).toThrow(/no request\/header event/)
  69. })
  70. it('rejects an unfrozen messages array but skips requests outside the loop contract', async () => {
  71. const { ctx, session, boundary } = await requestSetup()
  72. expect(() => { dispatch(ctx, loopRequest({ model: 'm', messages: [...boundary], sessionId: session.id })) })
  73. .toThrow(/frozen messages array/)
  74. expect(() => { dispatch(ctx, { model: 'summarizer', messages: [], sessionId: session.id }) }).not.toThrow()
  75. expect(() => { dispatch(ctx, Object.freeze({ model: 'm', messages: Object.freeze([]) })) }).not.toThrow()
  76. expect(() => { dispatch(ctx, Object.freeze({ model: 'm', messages: Object.freeze([]), sessionId: SessionId('ghost') })) })
  77. .not.toThrow()
  78. const directSession = ctx.sessions.create(SessionId('direct-one-shot'))
  79. expect(() => {
  80. dispatch(ctx, Object.freeze({ model: 'one-shot', messages: Object.freeze([]), sessionId: directSession.id }))
  81. }).not.toThrow()
  82. })
  83. it('rejects malformed requests carrying the loop marker', async () => {
  84. const { ctx, session } = await requestSetup()
  85. const messages: GenerateOptions['messages'] = []
  86. Object.freeze(messages)
  87. expect(() => {
  88. dispatch(ctx, markAgentLoopRequest({ provider: 'p', model: 'm', messages, sessionId: session.id }))
  89. }).toThrow(/request must be frozen/)
  90. expect(() => {
  91. dispatch(ctx, loopRequest({ model: 'm', messages: Object.freeze([]) }))
  92. }).toThrow(/carry a session id/)
  93. expect(() => {
  94. dispatch(ctx, loopRequest({
  95. model: 'm',
  96. messages: Object.freeze([]),
  97. sessionId: SessionId('missing-loop-session'),
  98. }))
  99. }).toThrow(/live session id/)
  100. })
  101. it('prepends ahead of a short-circuiting stream listener', async () => {
  102. const ctx = new Context()
  103. await ctx.plugin(SessionStore)
  104. ctx.on('llm/stream', () => (async function* () {})() as never)
  105. await ctx.plugin(InvariantService)
  106. await ctx.plugin(AgentLoopInvariant)
  107. const session = ctx.sessions.create(SessionId('prepend-check'))
  108. session.append('turn/start', { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } })
  109. session.append('user/message', { content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  110. session.append('step/start', { turn: 1, step: 1 })
  111. session.append('request/header', { header: { config: { provider: 'mock', model: 'm' } }, reason: 'initial' })
  112. const divergent = loopRequest({
  113. model: 'm',
  114. messages: Object.freeze([{ role: 'user', content: [{ type: 'text', text: 'phantom' }] }]),
  115. sessionId: session.id,
  116. })
  117. expect(() => { dispatch(ctx, divergent) }).toThrow(/diverges from the boundary derivation/)
  118. })
  119. })