mirror of
https://github.com/deepseek-ai/deepseek-harness
synced 2026-08-15 21:04:50 +00:00
Review follow-up (#196): a listed name with no registered tool was silently ignored; misconfiguration must block work instead. The check lives in the assembly — the earliest moment the registered tool set exists (tool plugins register after the service constructs) and the only universal one (cordis has no "all plugins loaded" event; registrations change at any time). assemble() is now async so the throw surfaces as a rejection rather than a synchronous escape from a Promise-returning method. Blast radius, pinned by a loop-level test: the rejection reaches the turn's outer catch — the turn closes balanced with an `error` reason, agent/error mirrors it, no step opens, no request/header is logged, no request reaches the adapter, and the agent returns to idle; every turn fails identically until the config is fixed. A boot-time validation pass was considered and rejected (recorded in the RFC). The general principle — misconfiguration fails loud, never a silent skip — is added to AGENTS.md.
118 lines
5.7 KiB
TypeScript
118 lines
5.7 KiB
TypeScript
/**
|
|
* Loop-level tool-order determinism: the request/header event — and therefore
|
|
* the frozen request the adapter receives — carries the assembly's canonical
|
|
* tool order (system-prompt's `toolOrder` config, or lexicographic name
|
|
* order), regardless of the order tool plugins happened to register in.
|
|
* Registration order is a plugin-load artifact (concurrent dynamic imports
|
|
* race), so nothing downstream of the registry may depend on it.
|
|
*/
|
|
|
|
import { describe, expect, it } from 'vitest'
|
|
import { Context } from 'cordis'
|
|
import LlmService from '@deepseek-ai/dsh-llm'
|
|
import SessionStore, { foldRequestHeader } from '@deepseek-ai/dsh-session'
|
|
import SystemPrompt, { TOOL_ORDER_REST } from '@deepseek-ai/dsh-system-prompt'
|
|
import type { Config as SystemPromptConfig } from '@deepseek-ai/dsh-system-prompt'
|
|
import ToolRegistry, { defineTool } from '@deepseek-ai/dsh-tools'
|
|
import AgentRegistry, { AgentId } from '@deepseek-ai/dsh-agent'
|
|
import AgentLoop, { ReactLoopAgent } from '@deepseek-ai/dsh-agent-loop'
|
|
import { MockAdapter, textResponse } from './mock-adapter.ts'
|
|
|
|
async function harness(adapter: MockAdapter, toolOrder?: SystemPromptConfig['toolOrder']) {
|
|
const ctx = new Context()
|
|
await ctx.plugin(LlmService)
|
|
await ctx.plugin(SessionStore)
|
|
await ctx.plugin(SystemPrompt, { persona: 'stable base', ...toolOrder !== undefined ? { toolOrder } : {} })
|
|
await ctx.plugin(ToolRegistry)
|
|
await ctx.plugin(AgentRegistry)
|
|
await ctx.plugin(AgentLoop, { agents: [] })
|
|
ctx.llm.registerAdapter(['mock'], adapter)
|
|
return ctx
|
|
}
|
|
|
|
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()
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
function registerNamed(ctx: Context, name: string) {
|
|
ctx.tools.register(defineTool({
|
|
name,
|
|
description: `the ${name} tool`,
|
|
parameters: {},
|
|
async execute() {
|
|
return [{ type: 'text', text: name }]
|
|
},
|
|
}))
|
|
}
|
|
|
|
/** Run one text-only turn and return the harness context + agent. */
|
|
async function runTurn(registrationOrder: string[], toolOrder?: SystemPromptConfig['toolOrder']) {
|
|
const adapter = new MockAdapter([textResponse('done')])
|
|
const ctx = await harness(adapter, toolOrder)
|
|
for (const name of registrationOrder) registerNamed(ctx, name)
|
|
const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' })
|
|
agent.send([{ type: 'text', text: 'go' }])
|
|
await waitForIdle(ctx, agent)
|
|
return { ctx, agent, adapter }
|
|
}
|
|
|
|
describe('loop-level canonical tool order', () => {
|
|
it('logs the request/header with tools in canonical order, not registration order', async () => {
|
|
const { agent, adapter } = await runTurn(['zulu', 'alpha', 'mike'])
|
|
const header = foldRequestHeader(agent.session.events)
|
|
expect(header?.tools?.map(tool => tool.name)).toEqual(['alpha', 'mike', 'zulu'])
|
|
// The dispatched request is built FROM the logged header (whose tools the
|
|
// assembly already canonicalized) and reaches the adapter deep-frozen —
|
|
// the marker the reconstruction invariant keys on.
|
|
expect(adapter.requests[0]?.tools?.map(tool => tool.name)).toEqual(['alpha', 'mike', 'zulu'])
|
|
expect(Object.isFrozen(adapter.requests[0])).toBe(true)
|
|
expect(adapter.requests[0]?.sessionId).toBe(agent.session.id)
|
|
})
|
|
|
|
it('produces the same header order for any registration order', async () => {
|
|
const first = await runTurn(['alpha', 'mike', 'zulu'])
|
|
const second = await runTurn(['zulu', 'mike', 'alpha'])
|
|
const names = (run: typeof first) => foldRequestHeader(run.agent.session.events)?.tools?.map(tool => tool.name)
|
|
expect(names(first)).toEqual(['alpha', 'mike', 'zulu'])
|
|
expect(names(second)).toEqual(names(first))
|
|
})
|
|
|
|
it('honors a configured toolOrder in the logged header and the dispatched request', async () => {
|
|
const { agent, adapter } = await runTurn(['alpha', 'zulu', 'mike'], ['zulu', TOOL_ORDER_REST])
|
|
const header = foldRequestHeader(agent.session.events)
|
|
expect(header?.tools?.map(tool => tool.name)).toEqual(['zulu', 'alpha', 'mike'])
|
|
expect(adapter.requests[0]?.tools?.map(tool => tool.name)).toEqual(['zulu', 'alpha', 'mike'])
|
|
expect(Object.isFrozen(adapter.requests[0])).toBe(true)
|
|
})
|
|
|
|
it('fails the turn — no model request — when toolOrder names an unregistered tool', async () => {
|
|
// The assemble rejection escapes to runTurn's outer catch: the open turn
|
|
// closes with an `error` reason (agent/error mirrors it), no step opens,
|
|
// no request/header is logged, the adapter never sees a request, and the
|
|
// agent returns to idle — a misconfigured deployment fails every turn
|
|
// deterministically instead of silently reordering nothing.
|
|
const adapter = new MockAdapter([textResponse('never sent')])
|
|
const ctx = await harness(adapter, ['ghost', TOOL_ORDER_REST])
|
|
registerNamed(ctx, 'alpha')
|
|
const errors: Error[] = []
|
|
ctx.on('agent/error', (_agent, _turn, _step, error) => void errors.push(error))
|
|
const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' })
|
|
agent.send([{ type: 'text', text: 'go' }])
|
|
await waitForIdle(ctx, agent)
|
|
expect(adapter.requests).toHaveLength(0)
|
|
expect(errors.map(e => e.message)).toEqual(['toolOrder lists unregistered tool "ghost"; registered tools: alpha'])
|
|
expect(foldRequestHeader(agent.session.events)).toBeUndefined()
|
|
const end = agent.session.events.find(e => e.type === 'turn/end')
|
|
expect(end?.type === 'turn/end' && end.data.reason).toMatchObject({ kind: 'error', step: 1 })
|
|
// The turn is balanced (turn/start → turn/end) with no step events inside.
|
|
expect(agent.session.events.some(e => e.type === 'step/start')).toBe(false)
|
|
})
|
|
})
|