mirror of
https://github.com/deepseek-ai/deepseek-harness
synced 2026-08-15 21:04:50 +00:00
# Conflicts: # .agents/notes/implemented/architecture/2026-06-21-bounded-llm-request-recovery.i18n.yaml # .agents/notes/implemented/feature/2026-06-18-compaction-capability-seam.i18n.yaml # .agents/notes/implemented/feature/2026-06-18-compaction-capability-seam.md # .agents/notes/implemented/feature/2026-06-18-compaction-capability-seam.zh.md # .agents/notes/implemented/feature/2026-07-06-sandbox.i18n.yaml # .agents/notes/implemented/feature/2026-07-06-sandbox.md # .agents/notes/implemented/feature/2026-07-06-sandbox.zh.md # .agents/notes/implemented/feature/2026-07-27-tmux-location-context.i18n.yaml # .agents/notes/implemented/simplification/2026-06-20-public-agent-stop-surface.i18n.yaml # .agents/notes/implemented/simplification/2026-07-28-remove-synthetic-log-only-turns.i18n.yaml # .agents/notes/implemented/simplification/2026-07-30-private-agent-send.i18n.yaml # docs/architecture.i18n.yaml # docs/architecture.md # docs/architecture.zh.md # docs/config-catalog.md # docs/cordis-catalog/events.md # docs/cordis-catalog/services.md # docs/core-data-structures/compaction.i18n.yaml # docs/core-data-structures/core.i18n.yaml # docs/core-data-structures/core.md # docs/core-data-structures/core.zh.md # docs/core-data-structures/llm-streaming.i18n.yaml # docs/core-data-structures/llm-streaming.md # docs/core-data-structures/llm-streaming.zh.md # docs/core-data-structures/session.i18n.yaml # docs/event-producer-consumer.md # docs/module-graph.md # docs/persistence-catalog.md # examples/acp-agent/tests/goal-snapshots/goal-session/session.expected.jsonl # examples/acp-agent/tests/snapshots/advanced-toolchain/session.1.jsonl # examples/acp-agent/tests/snapshots/advanced-toolchain/session.2.jsonl # examples/acp-agent/tests/snapshots/advanced-toolchain/session.jsonl # examples/acp-agent/tests/snapshots/bash-spill/session.jsonl # examples/acp-agent/tests/snapshots/bash-tool-turn/session.jsonl # examples/acp-agent/tests/snapshots/both-mode-turn/session.jsonl # examples/acp-agent/tests/snapshots/cancel-tool-calls/session.jsonl # examples/acp-agent/tests/snapshots/cancel/session.jsonl # examples/acp-agent/tests/snapshots/code-mode-turn/session.jsonl # examples/acp-agent/tests/snapshots/code-mode-workspace-context/session.jsonl # examples/acp-agent/tests/snapshots/cordis-inspect-jsdoc/session.jsonl # examples/acp-agent/tests/snapshots/empty-response-retry/session.jsonl # examples/acp-agent/tests/snapshots/error-finish/session.jsonl # examples/acp-agent/tests/snapshots/escalation-approved/session.jsonl # examples/acp-agent/tests/snapshots/escalation-rejected/session.jsonl # examples/acp-agent/tests/snapshots/fs-edit/session.jsonl # examples/acp-agent/tests/snapshots/fs-escalation-approved/session.jsonl # examples/acp-agent/tests/snapshots/fs-glob-sampling/session.jsonl # examples/acp-agent/tests/snapshots/fs-policy-reject/session.jsonl # examples/acp-agent/tests/snapshots/fs-read-window/session.jsonl # examples/acp-agent/tests/snapshots/fs-read/session.jsonl # examples/acp-agent/tests/snapshots/fs-write-overwrite/session.jsonl # examples/acp-agent/tests/snapshots/fs-write/session.jsonl # examples/acp-agent/tests/snapshots/hook-cc-invalid-matcher/session.jsonl # examples/acp-agent/tests/snapshots/hook-cc-posttool-block/session.jsonl # examples/acp-agent/tests/snapshots/hook-cc-posttool-context/session.jsonl # examples/acp-agent/tests/snapshots/hook-cc-pretool-ask/session.jsonl # examples/acp-agent/tests/snapshots/hook-cc-pretool-deny/session.jsonl # examples/acp-agent/tests/snapshots/hook-cc-promptsubmit-context/session.jsonl # examples/acp-agent/tests/snapshots/hook-cc-stop-continue/session.jsonl # examples/acp-agent/tests/snapshots/hook-codex-invalid-matcher/session.jsonl # examples/acp-agent/tests/snapshots/hook-codex-posttool-block/session.jsonl # examples/acp-agent/tests/snapshots/hook-codex-posttool-context/session.jsonl # examples/acp-agent/tests/snapshots/hook-codex-pretool-block/session.jsonl # examples/acp-agent/tests/snapshots/hook-codex-promptsubmit-context/session.jsonl # examples/acp-agent/tests/snapshots/hook-codex-stop-continue/session.jsonl # examples/acp-agent/tests/snapshots/lsp-definition/session.jsonl # examples/acp-agent/tests/snapshots/multi-turn/session.jsonl # examples/acp-agent/tests/snapshots/packed-chunks/session.jsonl # examples/acp-agent/tests/snapshots/parallel-tool-calls/session.jsonl # examples/acp-agent/tests/snapshots/pty-tools/session.jsonl # examples/acp-agent/tests/snapshots/repeat-tool-guard/session.jsonl # examples/acp-agent/tests/snapshots/session-query-spill/session.jsonl # examples/acp-agent/tests/snapshots/session-sandbox-root/session.jsonl # examples/acp-agent/tests/snapshots/session-title-after-turn/session.jsonl # examples/acp-agent/tests/snapshots/skill-load/session.jsonl # examples/acp-agent/tests/snapshots/subagent-depth-two-rejection/session.1.jsonl # examples/acp-agent/tests/snapshots/subagent-depth-two-rejection/session.2.jsonl # examples/acp-agent/tests/snapshots/subagent-depth-two-rejection/session.jsonl # examples/acp-agent/tests/snapshots/subagent-fork/session.1.jsonl # examples/acp-agent/tests/snapshots/subagent-fork/session.jsonl # examples/acp-agent/tests/snapshots/subagent-mixed/session.1.jsonl # examples/acp-agent/tests/snapshots/subagent-mixed/session.2.jsonl # examples/acp-agent/tests/snapshots/subagent-mixed/session.jsonl # examples/acp-agent/tests/snapshots/subagent-multi/session.1.jsonl # examples/acp-agent/tests/snapshots/subagent-multi/session.2.jsonl # examples/acp-agent/tests/snapshots/subagent-multi/session.jsonl # examples/acp-agent/tests/snapshots/subagent-spawn/session.1.jsonl # examples/acp-agent/tests/snapshots/subagent-spawn/session.jsonl # examples/acp-agent/tests/snapshots/text-turn/session.jsonl # examples/acp-agent/tests/snapshots/todo-write/session.jsonl # examples/acp-agent/tests/snapshots/tool-call-turn/session.jsonl # examples/acp-agent/tests/snapshots/web-fetch/session.jsonl # examples/acp-agent/tests/snapshots/workflow-run/session.1.jsonl # examples/acp-agent/tests/snapshots/workflow-run/session.jsonl # examples/acp-agent/tests/snapshots/workspace-context/session.jsonl # examples/acp-agent/tests/snapshots/workspace-edit/session.jsonl # examples/headless-agent/tests/semantic-checkpoint-snapshots/tool-outcome-unknown/session.expected.jsonl # examples/headless-agent/tests/snapshots/advanced-toolchain/session.1.jsonl # examples/headless-agent/tests/snapshots/advanced-toolchain/session.2.jsonl # examples/headless-agent/tests/snapshots/advanced-toolchain/session.jsonl # examples/headless-agent/tests/snapshots/advanced-toolchain/stream-json.expected.jsonl # examples/headless-agent/tests/snapshots/goal-tools/stream-json.expected.jsonl # examples/headless-agent/tests/snapshots/missing-credential/stream-json.expected.jsonl # examples/headless-agent/tests/snapshots/provider-retry/stream-json.expected.jsonl # examples/headless-agent/tests/snapshots/pty-tools/session.jsonl # examples/headless-agent/tests/snapshots/pty-tools/stream-json.expected.jsonl # examples/headless-agent/tests/snapshots/ralph-loop/stream-json.expected.jsonl # examples/headless-agent/tests/subagent-inheritance-snapshots/parent-override/child.expected.jsonl # examples/headless-agent/tests/subagent-inheritance-snapshots/parent-override/parent.expected.jsonl # examples/jsonrpc-agent/tests/snapshots/bash-tool/notifications.expected.jsonl # examples/jsonrpc-agent/tests/snapshots/bash-tool/session.jsonl # examples/jsonrpc-agent/tests/snapshots/persistent-tools/notifications.expected.jsonl # examples/jsonrpc-agent/tests/snapshots/persistent-tools/session.jsonl # examples/jsonrpc-agent/tests/snapshots/subagent-spawn/notifications.expected.jsonl # examples/jsonrpc-agent/tests/snapshots/subagent-spawn/session.1.jsonl # examples/jsonrpc-agent/tests/snapshots/subagent-spawn/session.jsonl # examples/jsonrpc-agent/tests/snapshots/text-turn/notifications.expected.jsonl # examples/jsonrpc-agent/tests/snapshots/text-turn/session.jsonl # packages/client/runtime/README.i18n.yaml # packages/client/runtime/src/client/sessions/request-inspection.ts # packages/compact/compact-basic/README.i18n.yaml # packages/compact/compact-basic/README.md # packages/compact/compact-basic/README.zh.md # packages/compact/compact-basic/src/index.ts # packages/context/time-context/tests/time-context.spec.ts # packages/context/tmux-context/README.i18n.yaml # packages/context/tmux-context/tests/tmux-context.spec.ts # packages/context/workspace-context/tests/workspace-context.spec.ts # packages/cordis/tool-cordis/src/api-catalog.ts # packages/core/agent-loop/README.i18n.yaml # packages/core/agent-loop/README.md # packages/core/agent-loop/README.zh.md # packages/core/agent-loop/src/agent.ts # packages/core/agent/README.i18n.yaml # packages/core/agent/README.md # packages/core/agent/README.zh.md # packages/core/agent/src/types.ts # packages/core/session/README.i18n.yaml # packages/core/session/README.md # packages/core/session/README.zh.md # packages/fs/tool-str-replace-editor/tests/tools.spec.ts # packages/goal/command-goal/tests/command-goal.spec.ts # packages/goal/goal/tests/goal.spec.ts # packages/host/apiproxy/README.i18n.yaml # packages/host/apiproxy/README.md # packages/host/apiproxy/README.zh.md # packages/host/apiproxy/src/api/index.ts # packages/host/apiproxy/tests/api-proxy-workspace.spec.ts # packages/llm/llm/README.i18n.yaml # packages/llm/llm/README.md # packages/llm/llm/README.zh.md # packages/llm/llm/src/index.ts # packages/pty/pty-local/tests/index.spec.ts # packages/pty/pty-local/tests/local.spec.ts # packages/pty/pty/tests/service.spec.ts # packages/pty/tool-bash-persistent/tests/loader-composition.spec.ts # packages/pty/tool-bash-persistent/tests/tools.spec.ts # packages/pty/tool-pty/tests/loader-composition.spec.ts # packages/pty/tool-pty/tests/tools.spec.ts # packages/session-persistence/session-checkpoint-policy/tests/crash-recovery.e2e.ts # packages/skill/tool-skill/tests/tool-skill.spec.ts # packages/tasks/tasks-local/tests/tasks.spec.ts # packages/ui/tui/README.i18n.yaml # packages/ui/tui/tests/tui.spec.ts # packages/ui/user-approval/src/index.ts # packages/ui/user-approval/tests/approval.spec.ts
409 lines
18 KiB
TypeScript
409 lines
18 KiB
TypeScript
/**
|
|
* Keyless snapshot coverage for the TypeScript SDK path: each scenario spawns
|
|
* the REAL `dsh-jsonrpc-agent` runtime (per `DSH_EXAMPLE_MODE`) through the
|
|
* REAL `@deepseek-ai/dsh-sdk-client`, drives one turn over stdio JSON-RPC,
|
|
* and pins three surfaces — the SDK `RunResult`, the complete notification
|
|
* stream, and the persisted session logs. Replay serves recorded model
|
|
* responses via `llm-replay` (`cordis.snapshot.yml`); `DSH_SNAPSHOT=record`
|
|
* re-records against the live API; `DSH_SNAPSHOT=refresh` replays committed
|
|
* fixtures and rewrites expected outputs.
|
|
*/
|
|
|
|
import { mkdir, mkdtemp, readFile, readdir, rm, writeFile } from 'node:fs/promises'
|
|
import { tmpdir } from 'node:os'
|
|
import { basename, delimiter, join } from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
import { describe, expect, it } from 'vitest'
|
|
import {
|
|
normalizeSessionLog,
|
|
normalizeStdout,
|
|
refreshFixtureReplacements,
|
|
scrubRequestHeaders,
|
|
stabilizeRefreshLog,
|
|
tokenizeSessionFixtureCwd,
|
|
type HarvestedLog,
|
|
type NormalizeContext,
|
|
} from '@deepseek-ai/dsh-acp-snapshot'
|
|
import { resolveExampleLaunch } from '@deepseek-ai/dsh-loader-smoke'
|
|
import { DeepSeekHarness, type HarnessNotification, type RunResult } from '@deepseek-ai/dsh-sdk-client'
|
|
|
|
const testsDir = dirOf(import.meta.url)
|
|
const snapshotsDir = join(testsDir, 'snapshots')
|
|
const liveConfig = join(testsDir, '..', 'cordis.yml')
|
|
const replayConfig = join(testsDir, '..', 'cordis.snapshot.yml')
|
|
const persistentToolsLiveConfig = join(testsDir, '..', 'persistent-tools.cordis.yml')
|
|
const persistentToolsReplayConfig = join(testsDir, '..', 'persistent-tools.snapshot.cordis.yml')
|
|
const runtimeBin = fileURLToPath(new URL('../../../packages/examples/jsonrpc-demo/src/bin.ts', import.meta.url))
|
|
const repoTsconfig = fileURLToPath(new URL('../../../tsconfig.json', import.meta.url))
|
|
|
|
const mode = process.env.DSH_SNAPSHOT ?? 'replay'
|
|
const recording = mode === 'record'
|
|
const refreshing = mode === 'refresh'
|
|
|
|
function dirOf(url: string): string {
|
|
return fileURLToPath(new URL('.', url))
|
|
}
|
|
|
|
interface SdkScenario {
|
|
/** Scenario name; the snapshots/<name> fixture directory. */
|
|
name: string
|
|
/** The user prompt for the single SDK turn. */
|
|
prompt: string
|
|
/** Fixed SDK session id, so fixtures and replay binding stay stable. */
|
|
sessionId: string
|
|
/** How many child sessions the turn persists (subagent scenarios). */
|
|
children: number
|
|
/** Optional scenario-specific live and replay compositions. */
|
|
configs?: { live: string; replay: string }
|
|
/** Cwd-relative files whose final contents are part of the scenario contract. */
|
|
expectedFiles?: Readonly<Record<string, string>>
|
|
/** Assembled model-facing tool names and required argument keys. */
|
|
expectedTools?: Readonly<Record<string, readonly string[]>>
|
|
/** Stable policy-context clauses the real assembled request must include or omit. */
|
|
policyContext?: { includes: readonly string[]; excludes: readonly string[] }
|
|
}
|
|
|
|
const SCENARIOS: SdkScenario[] = [
|
|
{
|
|
name: 'text-turn',
|
|
prompt: 'Reply with exactly: SDK snapshot OK',
|
|
sessionId: 'sdk-snapshot-text',
|
|
children: 0,
|
|
},
|
|
{
|
|
name: 'bash-tool',
|
|
prompt: 'Run this exact command with your bash tool, then reply with its stdout only: echo dsh-sdk-proof-7391',
|
|
sessionId: 'sdk-snapshot-bash',
|
|
children: 0,
|
|
},
|
|
{
|
|
name: 'subagent-spawn',
|
|
prompt: "Use the subagent tool exactly once with description 'echo probe' and prompt: Reply with exactly: child answer 42. Then reply with the subagent's final answer verbatim.",
|
|
sessionId: 'sdk-snapshot-subagent',
|
|
children: 1,
|
|
},
|
|
{
|
|
name: 'persistent-tools',
|
|
prompt: 'Prove that bash state persists. Then create {{cwd}}/note.txt with a tab-indented line, view it, replace that literal tab-indented line, and make the persistent shell exit with code 9.',
|
|
sessionId: 'persistent-tools-snapshot',
|
|
children: 0,
|
|
configs: { live: persistentToolsLiveConfig, replay: persistentToolsReplayConfig },
|
|
expectedFiles: { 'note.txt': 'target:\n\tnew\n' },
|
|
expectedTools: { bash: ['command'], str_replace_editor: ['command', 'path'] },
|
|
policyContext: {
|
|
includes: ['Current DSH file policy: danger-full-access.', 'file modifications by available operations'],
|
|
excludes: ['write and edit tools', 'terminal sessions', 'one-shot bash commands'],
|
|
},
|
|
},
|
|
]
|
|
|
|
interface PersistedLog {
|
|
readonly path: string
|
|
readonly content: string
|
|
readonly header: Record<string, unknown>
|
|
}
|
|
|
|
interface MissingFile {
|
|
readonly missing: true
|
|
}
|
|
|
|
async function jsonlFiles(dir: string): Promise<string[]> {
|
|
const entries = await readdir(dir, { recursive: true })
|
|
return entries.filter(entry => entry.endsWith('.jsonl')).map(entry => join(dir, entry)).sort()
|
|
}
|
|
|
|
async function persistedLogs(sessionsRoot: string): Promise<PersistedLog[]> {
|
|
const files = await jsonlFiles(sessionsRoot)
|
|
return Promise.all(files.map(async (path) => {
|
|
const content = await readFile(path, 'utf8')
|
|
const header = JSON.parse(content.slice(0, content.indexOf('\n'))) as Record<string, unknown>
|
|
return { path, content, header }
|
|
}))
|
|
}
|
|
|
|
interface LoggedRequestHeader {
|
|
type?: string
|
|
data?: { header?: { system?: unknown; tools?: Array<{ name: string; parameters: { required?: string[] } }> } }
|
|
}
|
|
|
|
function assembledToolRequirements(log: PersistedLog): Record<string, string[]> {
|
|
const event = log.content.trimEnd().split('\n')
|
|
.map(line => JSON.parse(line) as LoggedRequestHeader)
|
|
.find(candidate => candidate.type === 'request/header')
|
|
const tools = event?.data?.header?.tools
|
|
if (tools === undefined) throw new Error('session log has no request/header tools')
|
|
return Object.fromEntries(tools.map(tool => [tool.name, tool.parameters.required ?? []]))
|
|
}
|
|
|
|
function assembledSystem(log: PersistedLog): string {
|
|
const event = log.content.trimEnd().split('\n')
|
|
.map(line => JSON.parse(line) as LoggedRequestHeader)
|
|
.find(candidate => candidate.type === 'request/header')
|
|
const system = event?.data?.header?.system
|
|
if (typeof system !== 'string') throw new Error('session log has no request/header system')
|
|
return system
|
|
}
|
|
|
|
function assembledPolicyContext(log: PersistedLog): string {
|
|
const contexts = log.content.trimEnd().split('\n').flatMap((line) => {
|
|
const event = JSON.parse(line) as {
|
|
type?: string
|
|
data?: { source?: { kind?: string; plugin?: string }; content?: Array<{ type?: string; text?: unknown }> }
|
|
}
|
|
if (event.type !== 'user/message'
|
|
|| event.data?.source?.kind !== 'plugin'
|
|
|| event.data.source.plugin !== '@deepseek-ai/dsh-system-prompt') return []
|
|
return event.data.content?.flatMap(block => block.type === 'text' && typeof block.text === 'string' ? [block.text] : []) ?? []
|
|
})
|
|
if (contexts.length !== 1) throw new Error(`session log has ${String(contexts.length)} runtime-context snapshots; expected one`)
|
|
return contexts[0] as string
|
|
}
|
|
|
|
function contextOf(logs: readonly { content: string; header: Record<string, unknown> }[], cwd: string): NormalizeContext {
|
|
return {
|
|
sessionIds: logs.flatMap(log => typeof log.header.id === 'string' ? [log.header.id] : []),
|
|
cwd,
|
|
}
|
|
}
|
|
|
|
function contextOfContents(contents: readonly string[]): NormalizeContext {
|
|
const headers = contents.map(content => JSON.parse(content.slice(0, content.indexOf('\n'))) as Record<string, unknown>)
|
|
return {
|
|
sessionIds: headers.flatMap(header => typeof header.id === 'string' ? [header.id] : []),
|
|
cwd: typeof headers[0]?.cwd === 'string' ? headers[0].cwd : '\0no-cwd\0',
|
|
}
|
|
}
|
|
|
|
async function hydrateReplayFixtures(scenario: SdkScenario, cwd: string): Promise<string[]> {
|
|
const root = join(cwd, '.replay-fixtures')
|
|
await mkdir(root, { recursive: true })
|
|
return Promise.all(fixtureFiles(scenario).map(async (source) => {
|
|
const destination = join(root, basename(source))
|
|
await writeFile(destination, (await readFile(source, 'utf8')).replaceAll('{{cwd}}', cwd))
|
|
return destination
|
|
}))
|
|
}
|
|
|
|
async function readExpectedFile(path: string): Promise<string | MissingFile> {
|
|
try {
|
|
return await readFile(path, 'utf8')
|
|
} catch (error: unknown) {
|
|
if (error instanceof Error && (error as NodeJS.ErrnoException).code === 'ENOENT') return { missing: true }
|
|
throw error
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Normalize the SDK-visible notification stream: embedded `session.event`
|
|
* envelopes get the session-log treatment (times zeroed, headers tokenized),
|
|
* then every record is scrubbed like a wire frame.
|
|
*/
|
|
function normalizeNotifications(notifications: readonly HarnessNotification[], ctx: NormalizeContext): string {
|
|
const events = notifications
|
|
.filter(n => n.method === 'session.event')
|
|
.map(n => n.params.event as Record<string, unknown>)
|
|
const normalizedEvents = events.length === 0
|
|
? []
|
|
: scrubRequestHeaders(normalizeSessionLog(
|
|
`${events.map(event => JSON.stringify(event)).join('\n')}\n`,
|
|
ctx,
|
|
)).trimEnd().split('\n').map(line => JSON.parse(line) as Record<string, unknown>)
|
|
let eventIndex = 0
|
|
const records = notifications.map((notification) => {
|
|
if (notification.method !== 'session.event') return { method: notification.method, params: notification.params }
|
|
const event = normalizedEvents[eventIndex++]
|
|
return { method: notification.method, params: { ...notification.params, event } }
|
|
})
|
|
return normalizeStdout(`${records.map(record => JSON.stringify(record)).join('\n')}\n`, ctx)
|
|
}
|
|
|
|
/** Normalize the owned-run projection. */
|
|
function normalizeResult(result: RunResult, ctx: NormalizeContext): string {
|
|
return normalizeStdout(`${JSON.stringify({
|
|
sessionId: result.sessionId,
|
|
finalResponse: result.finalResponse,
|
|
})}\n`, ctx)
|
|
}
|
|
|
|
/** One SDK turn against a fresh runtime subprocess in an isolated cwd. */
|
|
async function runScenario(scenario: SdkScenario): Promise<{
|
|
result: RunResult
|
|
notifications: HarnessNotification[]
|
|
logs: PersistedLog[]
|
|
observedFiles: Record<string, string | MissingFile>
|
|
cwd: string
|
|
}> {
|
|
const cwd = await mkdtemp(join(tmpdir(), `sdk-snapshot-${scenario.name}-`))
|
|
const sessionsRoot = join(cwd, '.sessions')
|
|
const replayFixtures = recording ? [] : await hydrateReplayFixtures(scenario, cwd)
|
|
const launch = resolveExampleLaunch({
|
|
srcBin: runtimeBin,
|
|
configArgs: [],
|
|
tsconfigPath: repoTsconfig,
|
|
})
|
|
const [parentFixture, ...childFixtures] = replayFixtures
|
|
const env: Record<string, string> = {
|
|
...Object.fromEntries(Object.entries(process.env).filter(([, value]) => value !== undefined)) as Record<string, string>,
|
|
...Object.fromEntries(Object.entries(launch.env).filter(([, value]) => value !== undefined)) as Record<string, string>,
|
|
DSH_CORDIS_CONFIG: recording
|
|
? scenario.configs?.live ?? liveConfig
|
|
: scenario.configs?.replay ?? replayConfig,
|
|
DSH_SESSION_ROOT: sessionsRoot,
|
|
DSH_CWD: cwd,
|
|
DSH_SNAPSHOT: mode,
|
|
NODE_OPTIONS: [process.env.NODE_OPTIONS, '--disable-warning=ExperimentalWarning'].filter(Boolean).join(' '),
|
|
...parentFixture === undefined ? {} : {
|
|
DSH_SNAPSHOT_FILE: parentFixture,
|
|
...childFixtures.length > 0 ? { DSH_SNAPSHOT_CHILD_FILES: childFixtures.join(delimiter) } : {},
|
|
},
|
|
}
|
|
|
|
const harness = new DeepSeekHarness({
|
|
launch: {
|
|
command: launch.command,
|
|
args: launch.args,
|
|
cwd,
|
|
env,
|
|
requestTimeoutMs: 110_000,
|
|
},
|
|
cwd,
|
|
provider: 'deepseek-official',
|
|
model: 'deepseek-v4-flash',
|
|
})
|
|
try {
|
|
const notifications: HarnessNotification[] = []
|
|
const result = await harness.run(scenario.prompt.replaceAll('{{cwd}}', cwd), {
|
|
sessionId: scenario.sessionId,
|
|
onNotification: (notification) => { notifications.push(notification) },
|
|
})
|
|
await harness.close()
|
|
const logs = await persistedLogs(sessionsRoot)
|
|
const observedFiles = Object.fromEntries(await Promise.all(
|
|
Object.keys(scenario.expectedFiles ?? {}).map(async (path): Promise<[string, string | MissingFile]> => [
|
|
path,
|
|
await readExpectedFile(join(cwd, path)),
|
|
]),
|
|
))
|
|
return { result, notifications, logs, observedFiles, cwd }
|
|
} finally {
|
|
await harness.close()
|
|
await rm(cwd, { recursive: true, force: true })
|
|
}
|
|
}
|
|
|
|
/** Order logs parent-first, children by creation time (fixture layout order). */
|
|
function orderLogs(logs: PersistedLog[], scenario: SdkScenario): PersistedLog[] {
|
|
const parents = logs.filter(log => typeof log.header.parentSession !== 'string')
|
|
const children = logs.filter(log => typeof log.header.parentSession === 'string')
|
|
.sort((left, right) => Number(left.header.createdAt) - Number(right.header.createdAt))
|
|
expect(parents).toHaveLength(1)
|
|
expect(children).toHaveLength(scenario.children)
|
|
return [...parents, ...children]
|
|
}
|
|
|
|
function fixtureFiles(scenario: SdkScenario): string[] {
|
|
const dir = join(snapshotsDir, scenario.name)
|
|
return [
|
|
join(dir, 'session.jsonl'),
|
|
...Array.from({ length: scenario.children }, (_, index) => join(dir, `session.${index + 1}.jsonl`)),
|
|
]
|
|
}
|
|
|
|
describe('TypeScript SDK snapshots over the jsonrpc runtime', () => {
|
|
for (const scenario of SCENARIOS) {
|
|
it(`replays ${scenario.name} through the SDK`, async () => {
|
|
const scenarioDir = join(snapshotsDir, scenario.name)
|
|
const notificationsExpectedPath = join(scenarioDir, 'notifications.expected.jsonl')
|
|
const resultExpectedPath = join(scenarioDir, 'result.expected.json')
|
|
|
|
const { result, notifications, logs, observedFiles, cwd } = await runScenario(scenario)
|
|
const ordered = orderLogs(logs, scenario)
|
|
const actualContext = contextOf(ordered, cwd)
|
|
|
|
if (recording) {
|
|
// Fixtures carry tokenized request headers; llm-replay reads only
|
|
// assistant output and tool traffic, so scrubbing keeps prompts and
|
|
// schemas out of the corpus without affecting replay.
|
|
await mkdir(scenarioDir, { recursive: true })
|
|
await Promise.all(ordered.map(async (log, index) => {
|
|
const file = fixtureFiles(scenario)[index]
|
|
if (file === undefined) throw new Error(`no fixture path for persisted log ${index}`)
|
|
await writeFile(file, scrubRequestHeaders(tokenizeSessionFixtureCwd(log.content)))
|
|
}))
|
|
}
|
|
|
|
const files = fixtureFiles(scenario)
|
|
let expectedContents = await Promise.all(files.map(file => readFile(file, 'utf8')))
|
|
|
|
if (refreshing) {
|
|
const harvested = ordered.map((log): HarvestedLog => ({
|
|
id: String(log.header.id),
|
|
createdAt: Number(log.header.createdAt),
|
|
...typeof log.header.parentSession === 'string' ? { parentSession: log.header.parentSession } : {},
|
|
content: log.content,
|
|
}))
|
|
const replacements = refreshFixtureReplacements(harvested, expectedContents)
|
|
expectedContents = await Promise.all(ordered.map(async (log, index) => {
|
|
const existing = expectedContents[index]
|
|
const file = files[index]
|
|
if (existing === undefined || file === undefined) throw new Error(`no fixture for persisted log ${index}`)
|
|
const stable = scrubRequestHeaders(tokenizeSessionFixtureCwd(
|
|
stabilizeRefreshLog(log.content, existing, replacements, actualContext),
|
|
))
|
|
await writeFile(file, stable)
|
|
return stable
|
|
}))
|
|
}
|
|
|
|
for (const [index, expected] of expectedContents.entries()) {
|
|
expect(scrubRequestHeaders(expected), `${scenario.name} session fixture ${index} carries request-header bulk`)
|
|
.toBe(expected)
|
|
}
|
|
|
|
// Persisted transcripts match the committed fixtures.
|
|
const expectedContext = contextOfContents(expectedContents)
|
|
for (const [index, log] of ordered.entries()) {
|
|
const expected = expectedContents[index]
|
|
if (expected === undefined) throw new Error(`no fixture for persisted log ${index}`)
|
|
expect(scrubRequestHeaders(normalizeSessionLog(log.content, actualContext)))
|
|
.toBe(scrubRequestHeaders(normalizeSessionLog(expected, expectedContext)))
|
|
}
|
|
|
|
// The SDK-visible wire stream and turn result match their expected outputs.
|
|
const normalizedNotifications = normalizeNotifications(notifications, actualContext)
|
|
const normalizedResult = normalizeResult(result, actualContext)
|
|
if (recording || refreshing) {
|
|
await writeFile(notificationsExpectedPath, normalizedNotifications)
|
|
await writeFile(resultExpectedPath, normalizedResult)
|
|
}
|
|
expect(normalizedNotifications).toBe(await readFile(notificationsExpectedPath, 'utf8'))
|
|
expect(normalizedResult).toBe(await readFile(resultExpectedPath, 'utf8'))
|
|
|
|
// Wire-shape invariants that must hold in every mode.
|
|
expect(notifications.at(-1)).toMatchObject({
|
|
method: 'session.status',
|
|
params: { status: 'idle' },
|
|
})
|
|
expect(observedFiles).toEqual(scenario.expectedFiles ?? {})
|
|
if (scenario.expectedTools !== undefined) {
|
|
const parent = ordered[0]
|
|
if (parent === undefined) throw new Error(`${scenario.name} has no parent session log`)
|
|
expect(assembledToolRequirements(parent)).toEqual(scenario.expectedTools)
|
|
}
|
|
if (scenario.policyContext !== undefined) {
|
|
const parent = ordered[0]
|
|
if (parent === undefined) throw new Error(`${scenario.name} has no parent session log`)
|
|
const context = assembledPolicyContext(parent)
|
|
for (const clause of scenario.policyContext.includes) expect(context).toContain(clause)
|
|
for (const clause of scenario.policyContext.excludes) expect(context).not.toContain(clause)
|
|
const system = assembledSystem(parent)
|
|
for (const clause of scenario.policyContext.includes) expect(system).not.toContain(clause)
|
|
}
|
|
if (scenario.children > 0) {
|
|
expect(notifications.some(n => n.method === 'subagent.started')).toBe(true)
|
|
expect(notifications.some(n => n.method === 'subagent.finished')).toBe(true)
|
|
}
|
|
})
|
|
}
|
|
})
|