mirror of
https://github.com/deepseek-ai/deepseek-harness
synced 2026-08-15 21:04:50 +00:00
# Conflicts: # docs/architecture.md # docs/config-catalog.md # docs/cordis-catalog/events.md # docs/event-producer-consumer.md # docs/module-graph.md # examples/coding-agent/cordis.yml # examples/echo-agent/cordis.yml # knip.json # packages/core/agent-loop/README.md # packages/core/agent-loop/tests/config-session-id.spec.ts # packages/examples/README.md # packages/examples/stdio-demo/README.md # packages/examples/stdio-demo/tests/stdio-agent.spec.ts
233 lines
11 KiB
TypeScript
233 lines
11 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
import { Context } from 'cordis'
|
|
import { mkdtemp, rm } from 'node:fs/promises'
|
|
import { tmpdir } from 'node:os'
|
|
import { join } from 'node:path'
|
|
import LlmService from '@deepseek-ai/dsh-llm'
|
|
import SessionStore, { SessionId, type SessionEvent, type SessionHeader } from '@deepseek-ai/dsh-session'
|
|
import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
|
|
import ToolRegistry from '@deepseek-ai/dsh-tools'
|
|
import AgentRegistry, { AgentId } from '@deepseek-ai/dsh-agent'
|
|
import SessionPersistence from '@deepseek-ai/dsh-session-persistence'
|
|
import SessionPersistenceJsonl from '@deepseek-ai/dsh-session-persistence-jsonl'
|
|
import AgentLoop, { ReactLoopAgent } from '@deepseek-ai/dsh-agent-loop'
|
|
import { MockAdapter, textResponse } from './mock-adapter.ts'
|
|
|
|
const dirs: string[] = []
|
|
afterEach(async () => { for (const d of dirs.splice(0)) await rm(d, { recursive: true, force: true }) })
|
|
|
|
function waitForIdle(ctx: Context, agent: ReactLoopAgent): Promise<void> {
|
|
return new Promise((resolve) => {
|
|
const dispose = ctx.on('agent/status', (subject, status) => {
|
|
if (subject === agent && status === 'idle') { dispose(); resolve() }
|
|
})
|
|
})
|
|
}
|
|
|
|
describe('config-driven session id', () => {
|
|
it('identity-nests the deferred resume fiber under its labeled owner effect', async () => {
|
|
const ctx = new Context()
|
|
await ctx.plugin(LlmService)
|
|
await ctx.plugin(SessionStore)
|
|
await ctx.plugin(SystemPrompt)
|
|
await ctx.plugin(ToolRegistry)
|
|
await ctx.plugin(AgentRegistry)
|
|
const loopFiber = await ctx.plugin(AgentLoop, {
|
|
agents: [{ id: AgentId('main'), provider: 'mock', model: 'mock', resumeSessionId: SessionId('deferred') }],
|
|
})
|
|
|
|
const resumeEffect = loopFiber.getEffects().find(effect => effect.label === 'agentLoop.resume(main)')
|
|
expect(resumeEffect?.children.map(child => child.label)).toEqual(['ctx.plugin()'])
|
|
expect(loopFiber.getEffects().filter(effect => effect.label === 'ctx.plugin()')).toEqual([])
|
|
|
|
await loopFiber.dispose()
|
|
})
|
|
|
|
it('drops an in-flight declarative resume when its owner is disposed', async () => {
|
|
const loadStarted = Promise.withResolvers<undefined>()
|
|
const pendingLoad = Promise.withResolvers<{ meta: SessionHeader; events: SessionEvent[] }>()
|
|
class DeferredSessionPersistence extends SessionPersistence {
|
|
locate(_meta: SessionHeader): undefined { return undefined }
|
|
create(_meta: SessionHeader): Promise<void> { return Promise.resolve() }
|
|
append(_id: SessionId, _events: readonly SessionEvent[]): Promise<void> { return Promise.resolve() }
|
|
load(_id: SessionId): Promise<{ meta: SessionHeader; events: SessionEvent[] }> {
|
|
loadStarted.resolve(undefined)
|
|
return pendingLoad.promise
|
|
}
|
|
list(): Promise<SessionHeader[]> { return Promise.resolve([]) }
|
|
}
|
|
|
|
const ctx = new Context()
|
|
await ctx.plugin(LlmService)
|
|
await ctx.plugin(SessionStore)
|
|
await ctx.plugin(SystemPrompt)
|
|
await ctx.plugin(ToolRegistry)
|
|
await ctx.plugin(AgentRegistry)
|
|
const failures: Error[] = []
|
|
ctx.on('agent/start-failed', (_id, error) => { failures.push(error) })
|
|
const loopFiber = await ctx.plugin(AgentLoop, {
|
|
agents: [{ id: AgentId('main'), provider: 'mock', model: 'mock', resumeSessionId: SessionId('deferred') }],
|
|
})
|
|
await ctx.plugin(DeferredSessionPersistence)
|
|
await loadStarted.promise
|
|
|
|
await loopFiber.dispose()
|
|
await Promise.resolve()
|
|
expect(failures).toEqual([])
|
|
expect(ctx.agents.getStartFailure(AgentId('main'))).toBeUndefined()
|
|
await ctx.fiber.dispose()
|
|
})
|
|
|
|
it('normalizes a non-Error declarative resume rejection without duplicating an Error prefix', async () => {
|
|
class RejectingSessionPersistence extends SessionPersistence {
|
|
locate(_meta: SessionHeader): undefined { return undefined }
|
|
create(_meta: SessionHeader): Promise<void> { return Promise.resolve() }
|
|
append(_id: SessionId, _events: readonly SessionEvent[]): Promise<void> { return Promise.resolve() }
|
|
load(_id: SessionId): Promise<{ meta: SessionHeader; events: SessionEvent[] }> {
|
|
// Third-party backends can reject arbitrary values; this exercises normalization.
|
|
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
|
|
return Promise.reject('plain failure')
|
|
}
|
|
list(): Promise<SessionHeader[]> { return Promise.resolve([]) }
|
|
}
|
|
|
|
const ctx = new Context()
|
|
await ctx.plugin(LlmService)
|
|
await ctx.plugin(SessionStore)
|
|
await ctx.plugin(SystemPrompt)
|
|
await ctx.plugin(ToolRegistry)
|
|
await ctx.plugin(AgentRegistry)
|
|
const failures: Error[] = []
|
|
ctx.on('agent/start-failed', (_id, error) => { failures.push(error) })
|
|
const loopFiber = await ctx.plugin(AgentLoop, {
|
|
agents: [{ id: AgentId('main'), provider: 'mock', model: 'mock', resumeSessionId: SessionId('rejected') }],
|
|
})
|
|
await ctx.plugin(RejectingSessionPersistence)
|
|
|
|
await vi.waitFor(() => { expect(failures).toHaveLength(1) })
|
|
expect(failures[0]?.message).toBe('plain failure')
|
|
expect(failures[0]?.cause).toBe('plain failure')
|
|
await loopFiber.dispose()
|
|
await ctx.fiber.dispose()
|
|
})
|
|
|
|
it('config-driven create uses a fresh ${id}-session-<uuid> per run (restart-safe)', async () => {
|
|
const root = await mkdtemp(join(tmpdir(), 'dsh-cfg-session-'))
|
|
dirs.push(root)
|
|
const idPattern = /^cfg-session-[0-9a-f-]{36}$/
|
|
// Run 1: a config agent persists a turn under a generated session id.
|
|
const ctx1 = new Context()
|
|
await ctx1.plugin(LlmService)
|
|
await ctx1.plugin(SessionStore)
|
|
await ctx1.plugin(SystemPrompt)
|
|
await ctx1.plugin(ToolRegistry)
|
|
await ctx1.plugin(AgentRegistry)
|
|
await ctx1.plugin(AgentLoop, { agents: [{ id: AgentId('cfg'), provider: 'mock', model: 'mock' }] })
|
|
await ctx1.plugin(SessionPersistenceJsonl, { root })
|
|
ctx1.llm.registerAdapter(['mock'], new MockAdapter([textResponse('cfg')]))
|
|
const a1 = ctx1.agents.get(AgentId('cfg')) as ReactLoopAgent
|
|
expect(a1.session.id).toMatch(idPattern)
|
|
a1.send([{ type: 'text', text: 'q' }], { source: { kind: 'user' } })
|
|
await waitForIdle(ctx1, a1)
|
|
await ctx1.fiber.dispose()
|
|
|
|
// Run 2 over the SAME root: a fresh id means no on-disk collision (a fixed
|
|
// ${id}-session would crash here with "already has a persisted log").
|
|
const ctx2 = new Context()
|
|
await ctx2.plugin(LlmService)
|
|
await ctx2.plugin(SessionStore)
|
|
await ctx2.plugin(SystemPrompt)
|
|
await ctx2.plugin(ToolRegistry)
|
|
await ctx2.plugin(AgentRegistry)
|
|
await ctx2.plugin(AgentLoop, { agents: [{ id: AgentId('cfg'), provider: 'mock', model: 'mock' }] })
|
|
await ctx2.plugin(SessionPersistenceJsonl, { root })
|
|
ctx2.llm.registerAdapter(['mock'], new MockAdapter([textResponse('cfg2')]))
|
|
const a2 = ctx2.agents.get(AgentId('cfg')) as ReactLoopAgent
|
|
expect(a2.session.id).toMatch(idPattern)
|
|
expect(a2.session.id).not.toBe(a1.session.id)
|
|
a2.send([{ type: 'text', text: 'q2' }], { source: { kind: 'user' } })
|
|
await waitForIdle(ctx2, a2)
|
|
await ctx2.fiber.dispose()
|
|
})
|
|
|
|
it('config-driven resumeSessionId continues a persisted session (env-var resume)', async () => {
|
|
const root = await mkdtemp(join(tmpdir(), 'dsh-cfg-resume-'))
|
|
dirs.push(root)
|
|
|
|
// Run 1: a programmatically-created agent on a KNOWN session id persists a
|
|
// completed turn, so run 2 has a concrete id to resume.
|
|
const ctx1 = new Context()
|
|
await ctx1.plugin(LlmService)
|
|
await ctx1.plugin(SessionStore)
|
|
await ctx1.plugin(SystemPrompt)
|
|
await ctx1.plugin(ToolRegistry)
|
|
await ctx1.plugin(AgentRegistry)
|
|
await ctx1.plugin(AgentLoop, { agents: [] })
|
|
await ctx1.plugin(SessionPersistenceJsonl, { root })
|
|
ctx1.llm.registerAdapter(['mock'], new MockAdapter([textResponse('first')]))
|
|
const a1 = (await ctx1.agents.create({ agentId: AgentId('main'), sessionId: SessionId('sticky-1') })).agent as ReactLoopAgent
|
|
a1.send([{ type: 'text', text: 'remember me' }], { source: { kind: 'user' } })
|
|
await waitForIdle(ctx1, a1)
|
|
await ctx1.fiber.dispose()
|
|
|
|
// Resume waits for the injected persistence service, so poll until the
|
|
// config-created agent appears with its stored history.
|
|
const ctx2 = new Context()
|
|
await ctx2.plugin(LlmService)
|
|
await ctx2.plugin(SessionStore)
|
|
await ctx2.plugin(SystemPrompt)
|
|
await ctx2.plugin(ToolRegistry)
|
|
await ctx2.plugin(AgentRegistry)
|
|
await ctx2.plugin(AgentLoop, { agents: [{ id: AgentId('main'), provider: 'mock', model: 'mock', resumeSessionId: SessionId('sticky-1') }] })
|
|
await ctx2.plugin(SessionPersistenceJsonl, { root })
|
|
ctx2.llm.registerAdapter(['mock'], new MockAdapter([textResponse('second')]))
|
|
|
|
// The deferred resume runs on a microtask after the backend is available.
|
|
let resumed: ReactLoopAgent | undefined
|
|
for (let i = 0; i < 50 && !resumed; i++) {
|
|
await new Promise(r => setTimeout(r, 5))
|
|
resumed = ctx2.agents.get(AgentId('main')) as ReactLoopAgent | undefined
|
|
}
|
|
expect(resumed).toBeDefined()
|
|
// The live session id IS the resumed id (NOT a fresh ${id}-session-<uuid>),
|
|
// and the prior turn's user message is in the derived history.
|
|
expect(resumed!.session.id).toBe('sticky-1')
|
|
const derived = resumed!.session.deriveMessages()
|
|
expect(JSON.stringify(derived)).toContain('remember me')
|
|
await ctx2.fiber.dispose()
|
|
})
|
|
|
|
it('config-driven resume of a missing session is contained: logs a warning, no agent, no crash', async () => {
|
|
const root = await mkdtemp(join(tmpdir(), 'dsh-cfg-resume-miss-'))
|
|
dirs.push(root)
|
|
const ctx = new Context()
|
|
await ctx.plugin(LlmService)
|
|
await ctx.plugin(SessionStore)
|
|
await ctx.plugin(SystemPrompt)
|
|
await ctx.plugin(ToolRegistry)
|
|
await ctx.plugin(AgentRegistry)
|
|
const failures: Array<{ id: string; error: Error }> = []
|
|
ctx.on('agent/start-failed', (id, error) => { failures.push({ id, error }) })
|
|
const loopFiber = await ctx.plugin(AgentLoop, { agents: [{ id: AgentId('main'), provider: 'mock', model: 'mock', resumeSessionId: SessionId('does-not-exist') }] })
|
|
const warn = vi.spyOn((ctx.agentLoop as unknown as { ctx: { logger: { warn: (...a: unknown[]) => void } } }).ctx.logger, 'warn')
|
|
.mockImplementation(() => undefined)
|
|
await ctx.plugin(SessionPersistenceJsonl, { root })
|
|
ctx.llm.registerAdapter(['mock'], new MockAdapter([textResponse('x')]))
|
|
|
|
// The deferred resume fails (no such session on disk). It must be contained:
|
|
// a warning is logged, no 'main' agent is registered, and the app stays up.
|
|
await new Promise(r => setTimeout(r, 200))
|
|
expect(ctx.agents.get(AgentId('main'))).toBeUndefined()
|
|
expect(warn).toHaveBeenCalledWith(expect.stringContaining('config-driven resume of "does-not-exist" failed'))
|
|
expect(failures).toHaveLength(1)
|
|
expect(failures[0]?.id).toBe('main')
|
|
expect(failures[0]?.error.message).toBe('session "does-not-exist" not found')
|
|
expect(failures[0]?.error.cause).toBeInstanceOf(Error)
|
|
expect(ctx.agents.getStartFailure(AgentId('main'))).toBe(failures[0]?.error)
|
|
await loopFiber.dispose()
|
|
expect(ctx.agents.getStartFailure(AgentId('main'))).toBeUndefined()
|
|
warn.mockRestore()
|
|
await ctx.fiber.dispose()
|
|
})
|
|
})
|