invariant.spec.ts 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { freezeMessage, MessageId } from '@deepseek-ai/dsh-llm'
  2. import { describe, expect, it } from 'vitest'
  3. import { Context } from 'cordis'
  4. import { InboxItemId, type Agent, type InboxItem, type InboxPlacement } from '@deepseek-ai/dsh-agent'
  5. import * as AgentInvariant from '@deepseek-ai/dsh-agent/invariant'
  6. import { scopeTarget } from '@deepseek-ai/dsh-scope'
  7. import InvariantService from '@deepseek-ai/dsh-invariants'
  8. async function setup(): Promise<Context> {
  9. const ctx = new Context()
  10. await ctx.plugin(InvariantService)
  11. await ctx.plugin(AgentInvariant)
  12. return ctx
  13. }
  14. function mockAgent(id: string): Agent {
  15. return { id } as unknown as Agent
  16. }
  17. describe('agent status invariants', () => {
  18. it('accepts lifecycle transitions between idle and running', async () => {
  19. const ctx = await setup()
  20. const agent = mockAgent('a1')
  21. expect(() => {
  22. ctx.emit(scopeTarget(agent, agent), 'agent/status', agent, 'idle')
  23. ctx.emit(scopeTarget(agent, agent), 'agent/status', agent, 'running')
  24. ctx.emit(scopeTarget(agent, agent), 'agent/status', agent, 'idle')
  25. }).not.toThrow()
  26. })
  27. it('rejects a no-op transition', async () => {
  28. const ctx = await setup()
  29. const agent = mockAgent('a3')
  30. ctx.emit(scopeTarget(agent, agent), 'agent/status', agent, 'running')
  31. expect(() => { ctx.emit(scopeTarget(agent, agent), 'agent/status', agent, 'running') })
  32. .toThrow(/no-op transition/)
  33. })
  34. it('tracks agents independently', async () => {
  35. const ctx = await setup()
  36. const a = mockAgent('a5')
  37. const b = mockAgent('b5')
  38. ctx.emit(scopeTarget(a, a), 'agent/status', a, 'running')
  39. expect(() => { ctx.emit(scopeTarget(b, b), 'agent/status', b, 'running') }).not.toThrow()
  40. })
  41. })
  42. describe('agent inbox invariants', () => {
  43. let nextItem = 0
  44. const info = (placement: InboxPlacement = 'queued'): InboxItem => ({
  45. id: InboxItemId(`i-${nextItem++}`),
  46. message: freezeMessage({
  47. id: MessageId('m'),
  48. role: 'user' as const,
  49. content: [],
  50. source: { kind: 'user' as const },
  51. }),
  52. placement,
  53. })
  54. it('accepts a dequeue and a discard covered by prior enqueues', async () => {
  55. const ctx = await setup()
  56. const agent = mockAgent('i1')
  57. const at = scopeTarget(agent, agent)
  58. expect(() => {
  59. ctx.emit(at, 'agent/inbox/enqueue', agent, info())
  60. ctx.emit(at, 'agent/inbox/enqueue', agent, info('steering'))
  61. ctx.emit(at, 'agent/inbox/dequeue', agent, info())
  62. ctx.emit(at, 'agent/inbox/discard', agent, [info()])
  63. }).not.toThrow()
  64. })
  65. it('rejects a dequeue with no outstanding item', async () => {
  66. const ctx = await setup()
  67. const agent = mockAgent('i2')
  68. expect(() => { ctx.emit(scopeTarget(agent, agent), 'agent/inbox/dequeue', agent, info()) })
  69. .toThrow(/without a matching prior enqueue/)
  70. })
  71. it('rejects a discard larger than the outstanding count', async () => {
  72. const ctx = await setup()
  73. const agent = mockAgent('i3')
  74. const at = scopeTarget(agent, agent)
  75. ctx.emit(at, 'agent/inbox/enqueue', agent, info())
  76. expect(() => { ctx.emit(at, 'agent/inbox/discard', agent, [info(), info()]) })
  77. .toThrow(/dropped 2 items but only 1 were outstanding/)
  78. })
  79. it('accepts an empty discard against a fresh agent', async () => {
  80. const ctx = await setup()
  81. const agent = mockAgent('i4')
  82. expect(() => { ctx.emit(scopeTarget(agent, agent), 'agent/inbox/discard', agent, []) }).not.toThrow()
  83. })
  84. })