The dsh-tools half of the Code Mode RFC (its fourth, final change): the registry gains its first config — mode: native | code | both — and OWNS how its tools reach the model. 'code' contributes exactly one wire tool, run_code, plus a lazy tools:sdk prompt section declaring every other tool as a generated TypeScript API (jsonSchemaToTs: total over the defineTool subset, unknown degradation, lexicographic byte-identical rendering); 'both' ships both representations; 'native' is byte-for-byte the old behavior. Non-native modes fail every assembly loudly without a typescript-language ctx.codeRuntime. run_code's dispatch bridge: JSON-normalizes each binding argument before dispatch (what dispatches is what the tool/code-dispatch event logs — the append can never fail on payload shape; BigInt/circulars reject that one call), serializes all program tool calls through a per-run queue (even Promise.all — no concurrency-safety metadata yet), routes every sub-call through tools/pre-execute → tools/post-execute (a deny rejects the program-side promise), drops sub-call additionalContext (no safe outlet mid-run; pinned), owns a run-scoped abort that follows the outer signal in and fires on settlement (in-flight sub-dispatch aborted, queued abandoned, queue drained before returning), and converts a failed run into CodeRunFailedError → a structured isError carrying kind + captured logs. tool/code-dispatch joins SessionEventMap by declaration merging (log-only; deriveMessages ignores it). The composed surface: the tools config forwards through agent-core and both app packages; examples/code-agent + demo:code run the worker runtime under mode code (keyless boot smoke + a with-key e2e proving the collapsed [run_code] header, the dispatch events, and the file the program wrote); two new snapshot scenarios (code-mode-turn, both-mode-turn) record the SDK section, collapsed header, dispatch events, and result card — each its own header-pinning class (the harness gains per-scenario config overlays and per-class pins). Catalogs, graphs, cookbook, hooks-bridge notes, and the RFC (moved to implemented/, restructured to decision-era headings) updated in the same change.
91 lines
3.7 KiB
TypeScript
91 lines
3.7 KiB
TypeScript
import { spawn, type ChildProcessWithoutNullStreams } from 'node:child_process'
|
|
import { mkdtemp, rm } from 'node:fs/promises'
|
|
import { tmpdir } from 'node:os'
|
|
import { join } from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
import { afterEach, describe, expect, it } from 'vitest'
|
|
|
|
/**
|
|
* Keyless Loader-path smoke for examples/code-agent: boot the REAL example
|
|
* through the `@deepseek-ai/dsh-stdio-agent` bin against its `cordis.yml`
|
|
* (the cordis Loader, `unwrapExports`, the full plugin tree incl. the
|
|
* worker-thread code runtime and the registry in `mode: code`), then close
|
|
* stdin with no prompt and assert the ready banner + a clean exit.
|
|
*
|
|
* No prompt is ever sent, so the model is NEVER called and no `run_code`
|
|
* turn happens — a dummy key lets `llm-deepseek`'s key-PRESENT check boot
|
|
* the tree. This is the export-shape guard (postmortem 0001) for the Code
|
|
* Mode composition; the with-key proof lives in `code-mode.e2e.ts`.
|
|
*/
|
|
|
|
const binScript = fileURLToPath(new URL('../../../packages/ui/stdio-agent/src/bin.ts', import.meta.url))
|
|
const configPath = fileURLToPath(new URL('../cordis.yml', import.meta.url))
|
|
const tsxLoader = fileURLToPath(import.meta.resolve('tsx'))
|
|
// Dev/test run UNBUILT: resolve `@deepseek-ai/dsh-*` through the root tsconfig
|
|
// `paths` map; tsx searches UP from cwd, and we spawn from a temp dir outside
|
|
// the repo, so point it at the repo tsconfig.
|
|
const repoTsconfig = fileURLToPath(new URL('../../../tsconfig.json', import.meta.url))
|
|
|
|
let child: ChildProcessWithoutNullStreams | undefined
|
|
let workdir: string | undefined
|
|
|
|
afterEach(async () => {
|
|
if (child !== undefined && child.exitCode === null) child.kill('SIGKILL')
|
|
child = undefined
|
|
if (workdir !== undefined) await rm(workdir, { recursive: true, force: true })
|
|
workdir = undefined
|
|
})
|
|
|
|
async function bootAndEof(): Promise<{ stdout: string; code: number }> {
|
|
workdir = await mkdtemp(join(tmpdir(), 'code-agent-smoke-'))
|
|
const cwd = workdir
|
|
return new Promise((resolve, reject) => {
|
|
const proc = spawn(
|
|
process.execPath,
|
|
// --expose-internals: cordis.yml loads the HMR plugin (mirrors demo:code).
|
|
['--expose-internals', '--import', tsxLoader, binScript, configPath],
|
|
{
|
|
cwd,
|
|
env: {
|
|
...process.env,
|
|
TSX_TSCONFIG_PATH: repoTsconfig,
|
|
// A dummy key so llm-deepseek's apply() (key-PRESENT check only) boots.
|
|
// No prompt is sent, so the adapter never streams — no network call.
|
|
DEEPSEEK_API_KEY: 'keyless-smoke-no-call',
|
|
},
|
|
stdio: ['pipe', 'pipe', 'pipe'],
|
|
},
|
|
)
|
|
child = proc
|
|
let stdout = ''
|
|
let stderr = ''
|
|
proc.stdout.setEncoding('utf8')
|
|
proc.stdout.on('data', (chunk: string) => { stdout += chunk })
|
|
proc.stderr.setEncoding('utf8')
|
|
proc.stderr.on('data', (chunk: string) => { stderr += chunk })
|
|
|
|
const timer = setTimeout(() => {
|
|
proc.kill('SIGKILL')
|
|
reject(new Error(`code-agent did not exit within 10s. stdout:\n${stdout}\nstderr:\n${stderr}`))
|
|
}, 10_000)
|
|
|
|
proc.on('exit', (code) => {
|
|
clearTimeout(timer)
|
|
if (code === 0) resolve({ stdout, code })
|
|
else reject(new Error(`code-agent exited ${code}. stderr:\n${stderr}`))
|
|
})
|
|
proc.on('error', (err) => { clearTimeout(timer); reject(err) })
|
|
|
|
// No prompt — just EOF, so the stdio UI exits without ever running a turn.
|
|
proc.stdin.end()
|
|
})
|
|
}
|
|
|
|
describe('code-agent keyless smoke (real cordis.yml via the Loader)', () => {
|
|
it('boots the Code Mode plugin tree, prints its banner, and exits cleanly on EOF', async () => {
|
|
const { stdout, code } = await bootAndEof()
|
|
expect(code).toBe(0)
|
|
expect(stdout).toContain('code-mode agent ready.')
|
|
}, 15_000)
|
|
})
|