command-goal.spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import { describe, expect, it, vi } from 'vitest'
  2. import { Context } from 'cordis'
  3. import Loader from '@cordisjs/plugin-loader'
  4. import AgentRegistry, {} from '@deepseek-ai/dsh-agent'
  5. import type { Agent, AgentStatus } from '@deepseek-ai/dsh-agent'
  6. import CommandService from '@deepseek-ai/dsh-commands'
  7. import GoalService from '@deepseek-ai/dsh-goal'
  8. import type { GoalRef } from '@deepseek-ai/dsh-goal'
  9. import SessionStore, { Session, SessionId, type UserMessage } from '@deepseek-ai/dsh-session'
  10. import * as commandGoal from '@deepseek-ai/dsh-command-goal'
  11. interface Harness {
  12. readonly ctx: Context
  13. readonly agent: Agent
  14. readonly session: Session
  15. readonly plugin: Awaited<ReturnType<Context['plugin']>>
  16. }
  17. /** Append one idle injection using the public Agent contract (idle inject wraps in a one-shot injection turn, per turn enclosure). */
  18. function appendInjection(session: Session, input: UserMessage): void {
  19. const lastStart = session.events.findLast(event => event.type === 'turn/start')
  20. const turn = (lastStart?.data.turn ?? 0) + 1
  21. session.append('turn/start', { turn, trigger: { kind: 'injection', source: input.source } })
  22. session.append('user/message', input, { surfaceOp: 'append' })
  23. session.append('turn/end', { turn, reason: { kind: 'completed' } })
  24. }
  25. /** Build a live idle agent accepted by the exact-identity goal service. */
  26. function stubAgent(ctx: Context, id: string): { agent: Agent; session: Session } {
  27. // Store-created: the command executor durably logs lifecycle events on it.
  28. const session = ctx.sessions.create(SessionId(id))
  29. let status: AgentStatus = 'idle'
  30. const agent: Agent = {
  31. id: session.id,
  32. options: {},
  33. session,
  34. ctx: new Context(),
  35. get status() { return status },
  36. get acceptsNextStep() { return status === 'running' },
  37. send: () => {},
  38. followup: () => {},
  39. steer: () => {},
  40. inject(input) { appendInjection(session, input) },
  41. cancel() { status = 'idle' },
  42. whenIdle() { return Promise.resolve() },
  43. }
  44. return { agent, session }
  45. }
  46. /** Mount the real command registry, goal domain, and producer. */
  47. async function harness(): Promise<Harness> {
  48. const ctx = new Context()
  49. await ctx.plugin(SessionStore)
  50. await ctx.plugin(CommandService)
  51. await ctx.plugin(AgentRegistry)
  52. await ctx.plugin(GoalService)
  53. const plugin = await ctx.plugin(commandGoal)
  54. const { agent, session } = stubAgent(ctx, `command-goal-${Math.random()}`)
  55. ctx.agents.register(agent)
  56. return { ctx, agent, session, plugin }
  57. }
  58. /** The log with executor-owned command lifecycle bookkeeping stripped (goal assertions target domain events). */
  59. function domainEvents(session: Session): readonly Session['events'][number][] {
  60. const lifecycle = new Set<number>()
  61. for (const event of session.events) {
  62. if (event.type !== 'command/run' && event.type !== 'command/done') continue
  63. lifecycle.add(event.seq)
  64. // The zero-step wrap around a lifecycle event is bookkeeping too.
  65. const before = session.events[event.seq - 1]
  66. const after = session.events[event.seq + 1]
  67. if (before?.type === 'turn/start') lifecycle.add(before.seq)
  68. if (after?.type === 'turn/end') lifecycle.add(after.seq)
  69. }
  70. return session.events.filter(event => !lifecycle.has(event.seq))
  71. }
  72. /** Execute `/goal` through the same registry boundary as a UI adapter. */
  73. async function run(test: Harness, suffix = ''): Promise<NonNullable<Awaited<ReturnType<CommandService['execute']>>>['result']> {
  74. const execution = await test.ctx.commands.execute(
  75. test.agent,
  76. `/goal${suffix}`,
  77. new AbortController().signal,
  78. )
  79. if (execution === undefined) throw new Error('goal command was not registered')
  80. return execution.result
  81. }
  82. /** Current exact compare-and-set ref. */
  83. function ref(goal: NonNullable<ReturnType<GoalService['get']>>): GoalRef {
  84. return { id: goal.id, revision: goal.revision }
  85. }
  86. describe('@deepseek-ai/dsh-command-goal registration', () => {
  87. it('registers one global command with Loader-safe exports and disposes it', async () => {
  88. const test = await harness()
  89. expect(commandGoal.name).toBe('command-goal')
  90. expect(commandGoal.inject).toEqual(['commands', 'goals'])
  91. expect('default' in commandGoal).toBe(false)
  92. const loader = Object.create(Loader.prototype) as Loader
  93. expect(loader.unwrapExports(commandGoal)).toBe(commandGoal)
  94. expect(test.ctx.commands.list(test.agent)).toContainEqual({
  95. name: 'goal',
  96. description: 'set or view the goal for a long-running task',
  97. input: { hint: '[<objective>|clear|edit <objective>|pause|resume]' },
  98. })
  99. expect(test.ctx.commands.find(test.agent, 'goal')).toBeDefined()
  100. await test.plugin.dispose()
  101. expect(test.ctx.commands.find(test.agent, 'goal')).toBeUndefined()
  102. })
  103. })
  104. describe('/goal human command', () => {
  105. it('shows an empty status without mutating the session', async () => {
  106. const test = await harness()
  107. await expect(run(test)).resolves.toEqual({
  108. kind: 'success',
  109. text: 'No goal is currently set.\nUsage: /goal [<objective>|clear|edit <objective>|pause|resume]',
  110. })
  111. expect(domainEvents(test.session)).toEqual([])
  112. })
  113. it('creates a trimmed objective and refuses silent replacement of unfinished work', async () => {
  114. const test = await harness()
  115. const created = await run(test, '\n finish the release ')
  116. expect(created.kind).toBe('success')
  117. expect(created.text).toContain('Goal created\nStatus: active')
  118. expect(created.text).toContain('Objective: finish the release')
  119. expect(created.text).toContain('Rounds: 0/256')
  120. expect(created.text).toContain('Activation: armed')
  121. expect(test.ctx.goals.get(test.agent)?.objective).toBe('finish the release')
  122. expect(domainEvents(test.session).map(event => event.type)).toEqual(['turn/start', 'user/message', 'turn/end'])
  123. const count = domainEvents(test.session).length
  124. await expect(run(test, ' replacement')).resolves.toEqual({
  125. kind: 'error',
  126. text: 'A goal is already active. Use /goal edit <objective> to change it or /goal clear before replacing it.',
  127. })
  128. expect(domainEvents(test.session)).toHaveLength(count)
  129. })
  130. it('treats only exact control words as controls', async () => {
  131. const test = await harness()
  132. await run(test, ' pause everything only after verification')
  133. expect(test.ctx.goals.get(test.agent)?.objective).toBe('pause everything only after verification')
  134. })
  135. it('edits inline, requires an objective, and starts a new goal when the old one is complete', async () => {
  136. const empty = await harness()
  137. const invalidEdit = await run(empty, ' edit')
  138. expect(invalidEdit.kind).toBe('error')
  139. expect(invalidEdit.text).toContain('requires a replacement objective')
  140. const missingEdit = await run(empty, ' edit replacement')
  141. expect(missingEdit.kind).toBe('error')
  142. expect(missingEdit.text).toContain('/goal edit requires one')
  143. const test = await harness()
  144. await run(test, ' first')
  145. const first = test.ctx.goals.get(test.agent)!
  146. const updated = await run(test, ' EDIT\n second ')
  147. expect(updated.kind).toBe('success')
  148. expect(updated.text).toContain('Goal updated')
  149. expect(test.ctx.goals.get(test.agent)).toMatchObject({ id: first.id, objective: 'second', revision: 2 })
  150. const current = test.ctx.goals.get(test.agent)!
  151. test.ctx.goals.complete(test.agent, ref(current))
  152. const replacement = await run(test, ' edit third')
  153. expect(replacement.kind).toBe('success')
  154. expect(replacement.text).toContain('Goal created')
  155. expect(test.ctx.goals.get(test.agent)).toMatchObject({ objective: 'third', revision: 1 })
  156. expect(test.ctx.goals.get(test.agent)?.id).not.toBe(first.id)
  157. })
  158. it('returns direct missing-state results for pause, resume, and clear', async () => {
  159. const test = await harness()
  160. const missingPause = await run(test, ' pause')
  161. expect(missingPause.kind).toBe('error')
  162. expect(missingPause.text).toContain('/goal pause requires one')
  163. const missingResume = await run(test, ' resume')
  164. expect(missingResume.kind).toBe('error')
  165. expect(missingResume.text).toContain('/goal resume requires one')
  166. await expect(run(test, ' clear')).resolves.toEqual({ kind: 'success', text: 'No goal to clear.' })
  167. })
  168. it('pauses, resumes, clears, and converts expected domain rejections to command errors', async () => {
  169. const test = await harness()
  170. await run(test, ' work')
  171. const redundantResume = await run(test, ' RESUME')
  172. expect(redundantResume).toEqual({
  173. kind: 'error',
  174. text: 'The goal command is not valid for the current state. Run /goal to view available commands.',
  175. })
  176. const paused = await run(test, ' PAUSE')
  177. expect(paused.kind).toBe('success')
  178. expect(paused.text).toContain('Goal paused')
  179. expect(test.ctx.goals.get(test.agent)).toMatchObject({ phase: 'paused', activation: 'disarmed' })
  180. const resumed = await run(test, ' resume')
  181. expect(resumed.kind).toBe('success')
  182. expect(resumed.text).toContain('Goal resumed')
  183. expect(test.ctx.goals.get(test.agent)).toMatchObject({ phase: 'active', activation: 'armed' })
  184. await expect(run(test, ' clear')).resolves.toEqual({ kind: 'success', text: 'Goal cleared.' })
  185. expect(test.ctx.goals.get(test.agent)).toBeUndefined()
  186. })
  187. it('shows every durable phase and distinguishes disarmed active state', async () => {
  188. const test = await harness()
  189. test.ctx.goals.create(test.agent, { objective: 'state matrix', maxGoalRounds: 1 })
  190. test.ctx.goals.disarm(test.agent)
  191. expect((await run(test)).text)
  192. .toContain('Status: active\nObjective: state matrix\nRounds: 0/1\nActivation: disarmed')
  193. expect((await run(test)).text).toContain('/goal resume')
  194. let goal = test.ctx.goals.get(test.agent)!
  195. goal = test.ctx.goals.resume(test.agent, ref(goal))
  196. goal = test.ctx.goals.pause(test.agent, ref(goal))
  197. expect((await run(test)).text).toContain('Status: paused')
  198. goal = test.ctx.goals.resume(test.agent, ref(goal))
  199. goal = test.ctx.goals.block(test.agent, ref(goal), {
  200. code: 'upstream-unavailable',
  201. message: 'Provider unavailable',
  202. })
  203. const blocked = await run(test)
  204. expect(blocked.text).toContain('Status: blocked')
  205. expect(blocked.text).toContain('Blocker: upstream-unavailable: Provider unavailable')
  206. goal = test.ctx.goals.resume(test.agent, ref(goal))
  207. test.ctx.goals.complete(test.agent, ref(goal))
  208. const complete = await run(test)
  209. expect(complete.text).toContain('Status: complete')
  210. expect(complete.text).toContain('Commands: /goal <objective>, /goal clear')
  211. })
  212. it('does not turn unexpected implementation failures into expected command results', async () => {
  213. const test = await harness()
  214. vi.spyOn(test.ctx.goals, 'get').mockImplementationOnce(() => { throw new Error('unexpected failure') })
  215. await expect(run(test)).rejects.toThrow('unexpected failure')
  216. })
  217. })