import assert from 'node:assert/strict' import { existsSync, readFileSync, readdirSync } from 'node:fs' import { createRequire } from 'node:module' import { dirname, join } from 'node:path' import { test } from 'node:test' import { fileURLToPath } from 'node:url' import yaml from 'js-yaml' import { facetContextText, facetRuleText } from '../lib/scope.js' import { harnessScopes } from '../scripts/harness-scope.mjs' const PRESETS = join(dirname(fileURLToPath(import.meta.url)), '..', 'presets') /** * The prompt variables the harness registers for a section. * * `dsh-agent-loop` puts each of these into the prompt registry, and an assembly * substitutes only what is registered. An unknown reference is not rendered as * text — interpolation throws, and the throw takes the entire turn with it, so * the user sees "This turn failed" and nothing else. A harness release that * stopped registering `cwd` is exactly how this bundle's preset stopped * composing sessions: the persona suffix it copied from the deployment said * "Your working directory is …" with that variable in it. */ const REGISTERED = ['provider', 'model'] /** Every `{{name}}` reference in one text, as the name alone. */ function referencesIn(text) { return [...text.matchAll(/\{\{([^{}]*)\}\}/g)].map((match) => match[1]) } test('no preset references a prompt variable the harness does not register', () => { const files = readdirSync(PRESETS).filter((name) => name.endsWith('.yml')) assert.ok(files.length > 0, 'the bundle ships at least one preset') const found = [] for (const file of files) { for (const name of referencesIn(readFileSync(join(PRESETS, file), 'utf8'))) found.push([file, name]) } // The model reference is the one that has to keep working, so the guard cannot // be satisfied by a preset that references nothing at all. assert.ok( found.some(([, name]) => name === 'model'), 'a preset still names the model', ) for (const [file, name] of found) { assert.ok( REGISTERED.includes(name), `${file} references {{${name}}}; the harness registers only ${REGISTERED.join(', ')}, and an unregistered reference fails the whole turn`, ) } }) test('the text this bundle contributes to the prompt holds no variable references', () => { // The rule and the binding are rebuilt from real values on every request, so a // placeholder in either would be a reference the harness never resolves — the // same failure, this time from the bundle's own hand. const rule = facetRuleText() const binding = facetContextText({ cwd: '/w', root: '/w/src', extras: ['/w/shared'], mode: 'ask' }) assert.ok(rule.length > 0 && binding.length > 0, 'both halves contribute text') assert.deepEqual(referencesIn(rule), []) assert.deepEqual(referencesIn(binding), []) }) /* * Drift against the shipped `standard` composition. * * A preset declaration replaces the whole plugin list rather than merging into * it, so both files under `presets/` are frozen copies of the harness's * `standard` patch. That copy is what breaks when the harness changes: on dsh * 0.2.2-rc.1 the harness folded `list_agents` into * `@deepseek-ai/dsh-tool-subagent-control` and dropped its `./list-agents` * subpath, and the copied row could never start — which breaks the *whole* * preset, because one row that fails to mount fails the declaration. These * tests turn "remember to re-copy it" into a build failure. * * They need the installed harness, so they skip on a checkout without one. */ const PACKAGE_ROOT = join(dirname(fileURLToPath(import.meta.url)), '..') const FACET_PRESET = join(PRESETS, 'facet.patch.yml') const COMPACTION_PRESET = join(PRESETS, 'standard-compaction.patch.yml') const HARNESS_SCOPES = harnessScopes() const STANDARD_PRESET = HARNESS_SCOPES .map((scope) => join(scope, '@deepseek-ai', 'dsh-web-app', 'presets', 'standard.patch.yml')) .find((file) => existsSync(file)) const SKIP_DRIFT = HARNESS_SCOPES.length === 0 ? 'no dsh installation found; set DSH_HARNESS_NODE_MODULES to compare against the shipped standard preset' : STANDARD_PRESET === undefined ? 'the shipped standard preset was not found in any harness scope' : false /** The `!!js` tag carries a source expression; both compared sides keep it as text. */ const PRESET_SCHEMA = yaml.DEFAULT_SCHEMA.extend([ new yaml.Type('tag:yaml.org,2002:js', { kind: 'scalar', construct: (value) => value }), ]) /** The plugin rows of a single-preset patch file, as written. */ function presetPlugins(file) { const [patch] = yaml.load(readFileSync(file, 'utf8'), { schema: PRESET_SCHEMA }) return patch.insert[0].config.plugins } /** Drop absent keys, so a missing field and an undefined field compare the same. */ function normalizedRows(rows) { return rows.map((row) => ({ id: row.id, name: row.name, ...(row.disabled === undefined ? {} : { disabled: row.disabled }), ...(row.group === true ? { group: true } : {}), ...(row.isolate === undefined ? {} : { isolate: row.isolate }), ...(row.config === undefined ? {} : { config: Array.isArray(row.config) ? normalizedRows(row.config) : row.config }), })) } /** One row by id, at any depth. */ function rowById(rows, id) { for (const row of rows) { if (row.id === id) return row if (Array.isArray(row.config)) { const found = rowById(row.config, id) if (found !== undefined) return found } } return undefined } /** A copy with the bundle's compaction budget removed, so everything else can be compared. */ function withoutCompactionBudget(rows) { const copy = structuredClone(rows) const strip = (list) => { for (const row of list) { if (row.id === 'compaction-basic') delete row.config if (Array.isArray(row.config)) strip(row.config) } } strip(copy) return copy } /** The only settings either file adds to a `standard` row. */ const COMPACTION_BUDGET = { thresholdRatio: 0.35, retainTokens: 80000, maxTokens: 16384 } test('the facet preset is the shipped standard composition plus its documented changes', { skip: SKIP_DRIFT }, () => { const standard = normalizedRows(presetPlugins(STANDARD_PRESET)) const facet = normalizedRows(presetPlugins(FACET_PRESET)) const guard = facet.filter((row) => row.id === 'facet-guard') assert.deepEqual(guard, [{ id: 'facet-guard', name: 'dsh-facet/guard' }], 'the facet preset mounts facet-guard exactly once') const withoutGuard = facet.filter((row) => row.id !== 'facet-guard') assert.deepEqual( withoutCompactionBudget(withoutGuard), withoutCompactionBudget(standard), 'presets/facet.patch.yml has drifted from the shipped standard composition; re-copy it from the harness and re-apply only the guard and the compaction budget', ) assert.deepEqual( rowById(withoutGuard, 'compaction-basic')?.config, COMPACTION_BUDGET, 'the facet preset keeps its compaction budget', ) }) test('the standard-compaction overlay is the shipped standard composition plus the compaction budget', { skip: SKIP_DRIFT }, () => { const standard = normalizedRows(presetPlugins(STANDARD_PRESET)) const overlay = normalizedRows(presetPlugins(COMPACTION_PRESET)) assert.deepEqual( rowById(overlay, 'compaction-basic')?.config, COMPACTION_BUDGET, 'the optional overlay configures the compaction budget', ) assert.deepEqual( withoutCompactionBudget(overlay), withoutCompactionBudget(standard), 'presets/standard-compaction.patch.yml has drifted from the shipped standard composition; re-copy it from the harness and re-apply only the compaction budget', ) }) /** Every row's id and module specifier, groups included. */ function namedRows(rows) { return rows.flatMap((row) => [ { id: row.id, name: row.name }, ...(Array.isArray(row.config) ? namedRows(row.config) : []), ]) } test('every plugin a shipped preset names resolves against the installed harness', { skip: SKIP_DRIFT }, () => { // Anchoring inside a scope makes Node look in that scope's own node_modules: // its parent directory carries the scope as a `node_modules` entry, which is // exactly the lookup the profile uses. Several scopes are tried because a // legacy `profiles/node_modules` mirror is only a snapshot of the installation. const harnessResolvers = HARNESS_SCOPES.map((scope) => createRequire(join(scope, 'noop.mjs'))) const fromPlugin = createRequire(join(PACKAGE_ROOT, 'package.json')) const unresolved = [] for (const file of [FACET_PRESET, COMPACTION_PRESET]) { for (const { id, name } of namedRows(presetPlugins(file))) { // `cordis:group` is a loader builtin rather than an installed package. if (name === 'cordis:group') continue const resolvers = name === 'dsh-facet/guard' ? [fromPlugin] : harnessResolvers let failure const resolved = resolvers.some((resolver) => { try { resolver.resolve(name) return true } catch (error) { failure = error return false } }) if (!resolved) unresolved.push(`${file.split('/').pop()}: ${id} (${name}): ${failure?.code ?? failure?.message}`) } } assert.deepEqual( unresolved, [], 'a preset names a plugin or subpath the harness no longer provides; re-copy the preset from the shipped standard', ) })