import { mkdtemp, rm, writeFile } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { pathToFileURL } from 'node:url' import { afterEach, describe, expect, it } from 'vitest' import { Context } from '@deepseek-ai/cordis' import Loader from '@deepseek-ai/cordis-plugin-loader' import Include from '@deepseek-ai/cordis-plugin-include' import type { Agent } from '@deepseek-ai/dsh-agent' import CommandRuntime from '@deepseek-ai/dsh-commands' import { CompactionId, CompactionEngine, type CompactionAgentContext, type CompactionResult, type CompactionTrigger, type ManualCompactAgentContext, } from '@deepseek-ai/dsh-compaction' import * as commandCompact from '@deepseek-ai/dsh-command-compact' import { Session, SessionId } from '@deepseek-ai/dsh-session' const COMPACTION_ID = CompactionId('loader-command-compact-test') const RESULT: CompactionResult = { compactionId: COMPACTION_ID, startSeq: 1, summarySeq: 2, endSeq: 3, summary: [{ type: 'text', text: 'loader summary' }], shadowedRange: { start: 3, end: 8 }, shadowedSeqs: [3, 5, 8], shadowedTokenCount: 99, } class LoaderCompactionEngine extends CompactionEngine { override compactIfNeeded( _agent: CompactionAgentContext, _trigger: CompactionTrigger, _signal: AbortSignal, ): Promise { return Promise.resolve(null) } override compactRegion(): Promise { return Promise.resolve(RESULT) } override compactNow( agent: ManualCompactAgentContext, _signal: AbortSignal, sourceCommandId?: Parameters[2], ): Promise { const provenance = { compactionId: RESULT.compactionId, ...sourceCommandId === undefined ? {} : { sourceCommandId }, } agent.session.append('compaction/start', { ...provenance, turn: null }) agent.session.append('compaction/summary', { ...provenance, summary: RESULT.summary, shadowedRange: RESULT.shadowedRange, shadowedSeqs: RESULT.shadowedSeqs, shadowedTokenCount: RESULT.shadowedTokenCount, provider: 'loader-test', model: 'loader-test', }) agent.session.append('compaction/end', { ...provenance, turn: null }) return Promise.resolve({ ...RESULT, ...provenance }) } } let root: string | undefined let context: Context | undefined afterEach(async () => { await context?.fiber.dispose() context = undefined if (root !== undefined) await rm(root, { recursive: true, force: true }) root = undefined }) describe('command-compact real Loader composition', () => { it('discovers and executes /compact through the assembled command plane', async () => { root = await mkdtemp(join(tmpdir(), 'dsh-command-compact-loader-')) const configPath = join(root, 'cordis.yml') await writeFile(configPath, [ "- name: '@deepseek-ai/dsh-commands'", "- name: '@test/compact-backend'", "- name: '@deepseek-ai/dsh-command-compact'", '', ].join('\n')) context = new Context() context.baseUrl = pathToFileURL(root).href + '/' await context.plugin(Loader) context.loader.builtins.include = Include const modules = new Map([ ['@deepseek-ai/dsh-commands', CommandRuntime], ['@test/compact-backend', LoaderCompactionEngine], ['@deepseek-ai/dsh-command-compact', commandCompact], ]) context.loader.internal = { version: 'v2', async import(specifier: string) { if (!modules.has(specifier)) throw new Error(`unexpected Loader import: ${specifier}`) return modules.get(specifier) }, } as unknown as NonNullable await context.loader.create({ name: 'cordis:include', config: { path: pathToFileURL(configPath).href }, }) await context.loader.await() const session = Session.create(SessionId('loader-command-compact')) const agent = { session, status: 'idle', options: {}, reserveTurnAdmission: () => () => undefined, } as unknown as Agent expect(context.commands.list(agent)).toContainEqual({ name: 'compact', description: 'Compact older conversation history', }) const execution = await context.commands.execute(agent, '/compact', new AbortController().signal) if (execution === undefined) throw new Error('Loader composition did not resolve /compact') expect(execution.result).toEqual({ kind: 'success', text: 'Compacted 3 history items (~99 tokens).', sourceEventSeq: RESULT.summarySeq, }) expect(session.events.map(event => ({ type: event.type, data: event.data }))).toEqual([ { type: 'command/run', data: { commandId: execution.commandId, name: 'compact', args: '', source: { kind: 'user' }, }, }, { type: 'compaction/start', data: { compactionId: COMPACTION_ID, sourceCommandId: execution.commandId, turn: null, }, }, { type: 'compaction/summary', data: { compactionId: COMPACTION_ID, sourceCommandId: execution.commandId, summary: RESULT.summary, shadowedRange: RESULT.shadowedRange, shadowedSeqs: RESULT.shadowedSeqs, shadowedTokenCount: RESULT.shadowedTokenCount, provider: 'loader-test', model: 'loader-test', }, }, { type: 'compaction/end', data: { compactionId: COMPACTION_ID, sourceCommandId: execution.commandId, turn: null, }, }, { type: 'command/done', data: { commandId: execution.commandId, kind: 'success', text: 'Compacted 3 history items (~99 tokens).', sourceEventSeq: RESULT.summarySeq, }, }, ]) expect(session.surface.nodes).toEqual([]) expect(session.deriveMessages()).toEqual([]) }) })