Files
dsh-context/tests/host/headers.spec.ts
T
3244ff29a9 feat: tool plugin source attribution in the Context browser (#25)
Tool-schema rows in the Context browser carry a best-effort source tag
for the registering plugin, recovered without a harness change:

- a harness-logged `plugin` field passes through verbatim (future-proof);
- MCP-proxied tools resolve `mcp:<server>` from their public names;
- shipped first-party tools resolve from a pinned package map;
- a live `tools.register()` hook (cordis `internal/get` reader slot,
  with a caller-package stack fallback for local-link installs) tags
  third-party, agent-scoped, and dynamic registrations, and peels a
  prior incarnation's wrapper on reload;
- tools already registered when the plugin mounts render a localized
  "unknown plugin" tag explaining the boot-order gap.

Priority: `mcp:<server>` > live record > pinned map > unknown. The
chip rides the shared tag frame tinted with the brand badge tokens.

Rebased onto v0.35.0 with maintainer review fixes; the attribution
priority and reload-safety rework and the chip theming are described
in the PR review comment.

Co-authored-by: Nwflower <1679659@qq.com>
Co-authored-by: bowenliang123 <liang.bowen.123@qq.com>
2026-08-28 09:50:10 +08:00

232 lines
9.6 KiB
TypeScript

// Unit tests for the contextHeaders projection unit (src/host/headers.ts) —
// the request-header CONTENT epochs behind the timeline's envelope figures.
// The unit is pure init/apply/view: each case drives real event envelopes
// through the real definition (no harness plumbing).
import assert from 'node:assert/strict'
import { describe, test } from 'vitest'
import { createContextHeadersDefinition } from '../../src/host/headers'
import type { HeadersState } from '../../src/host/headers'
import { header, foreign } from './helpers/events'
import type { TimelineEvent } from '../../src/host/fold'
type Def = ReturnType<typeof createContextHeadersDefinition>
/** The dual-contract fields ride the definition past its declared 0.1.0 return type. */
function compat(def: Def) {
return def as unknown as {
stateSchema: { parse: (value: unknown) => HeadersState }
wire: {
viewSchema: { safeParse: (value: unknown) => { success: boolean } }
view: Def['view']
}
}
}
/** A raw request/header envelope with full control over the header payload. */
function headerEvent(seq: number, rawHeader: unknown): TimelineEvent {
return { type: 'request/header', seq, time: seq * 1000, data: { header: rawHeader, reason: 'initial' } }
}
function fold(def: Def, events: TimelineEvent[]): HeadersState {
let state = def.init()
for (const ev of events) state = def.apply(state, ev as never)
return state
}
describe('createContextHeadersDefinition', () => {
test('init/apply/view fold header epochs', () => {
const def = createContextHeadersDefinition()
assert.equal(def.key, 'contextHeaders')
assert.equal(def.stateVersion, 1)
const init = def.init()
assert.deepEqual(init, { headers: [] })
const state = def.apply(init, header(1, {
system: 'You are an agent.',
tools: [{ name: 'bash', description: 'run a command' }],
}) as never)
assert.notEqual(state, init, 'a header event produces a new state')
assert.equal(state.headers.length, 1)
assert.equal(state.headers[0].seq, 1)
assert.equal(state.headers[0].system, 'You are an agent.')
assert.equal(state.headers[0].tools.length, 1)
assert.equal(state.headers[0].tools[0].name, 'bash')
const view = def.view(state)
assert.equal(view.headers.length, 1)
assert.equal(view.headers[0].tools[0].description, 'run a command')
})
test('a non-header event returns the same state reference', () => {
const def = createContextHeadersDefinition()
const state = fold(def, [header(1, { system: 'sys' })])
assert.equal(def.apply(state, foreign(2) as never), state)
})
test('null, undefined, and non-object headers return the same state reference', () => {
const def = createContextHeadersDefinition()
const state = fold(def, [header(1, { system: 'sys' })])
for (const [seq, raw] of [[2, null], [3, undefined], [4, 42]] as const) {
assert.equal(def.apply(state, headerEvent(seq, raw) as never), state, `header ${String(raw)} is not an epoch`)
}
})
test('a non-array tools field folds to an empty tool list', () => {
const def = createContextHeadersDefinition()
const view = def.view(fold(def, [headerEvent(1, { tools: 'nope' })]))
assert.deepEqual(view.headers[0].tools, [])
})
test('tool entries degrade bad names and omit bad descriptions', () => {
const def = createContextHeadersDefinition()
const view = def.view(fold(def, [headerEvent(1, {
tools: [
{ name: 42, description: 'kept' }, // non-string name → '?'
{ name: 'a', description: 7 }, // non-string description → omitted
{ name: 'b', description: '' }, // empty description → omitted
{ name: 'c', description: 'ok' },
],
})]))
const tools = view.headers[0].tools
assert.equal(tools[0].name, '?')
assert.equal(tools[0].description, 'kept')
assert.ok(!('description' in tools[1]))
assert.ok(!('description' in tools[2]))
assert.equal(tools[3].description, 'ok')
for (const tool of tools) {
assert.ok(Number.isInteger(tool.tokens) && tool.tokens >= 0, 'tool tokens priced')
assert.ok('schema' in tool, 'the raw schema rides the record')
}
})
test('system is omitted unless a non-empty string', () => {
const def = createContextHeadersDefinition()
const view = def.view(fold(def, [
headerEvent(1, { system: 42 }),
headerEvent(2, { system: '' }),
headerEvent(3, { system: 'sys' }),
]))
assert.ok(!('system' in view.headers[0]))
assert.ok(!('system' in view.headers[1]))
assert.equal(view.headers[2].system, 'sys')
})
test('the same epoch seq twice in a row returns the same state reference', () => {
const def = createContextHeadersDefinition()
const state = fold(def, [header(1, { system: 'a' })])
assert.equal(def.apply(state, header(1, { system: 'b' }) as never), state, 'duplicate epoch suppressed')
assert.equal(state.headers.length, 1)
})
test('retention caps at the 50 newest epochs', () => {
const def = createContextHeadersDefinition()
const events = Array.from({ length: 55 }, (_, i) => header(i + 1, { system: `s${i + 1}` }))
const state = fold(def, events)
assert.equal(state.headers.length, 50)
assert.equal(state.headers[0].seq, 6, 'the oldest five epochs dropped')
assert.equal(state.headers.at(-1)?.seq, 55)
})
test('view() copies records and tools off the state', () => {
const def = createContextHeadersDefinition()
const state = fold(def, [headerEvent(1, { system: 'sys', tools: [{ name: 'bash' }] })])
const view = def.view(state)
view.headers[0].tools[0].name = 'mutated'
view.headers[0].system = 'mutated'
assert.equal(state.headers[0].tools[0].name, 'bash', 'mutating the view must not alias state')
assert.equal(state.headers[0].system, 'sys')
})
test('schema, stateSchema, and the wire block validate a real folded view', () => {
const def = createContextHeadersDefinition()
const state = fold(def, [
header(1, {
system: 'You are an agent.',
tools: [{ name: 'bash', description: 'run a command', parameters: { type: 'object' } }],
}),
])
const view = def.view(state)
assert.equal(def.schema.safeParse(view).success, true, 'wire schema accepts the folded view')
const c = compat(def)
assert.deepEqual(c.stateSchema.parse(structuredClone(state)), state, 'state schema round-trips the fold state')
assert.equal(c.wire.view, def.view, 'the wire block shares the view function')
assert.equal(c.wire.viewSchema.safeParse(c.wire.view(state)).success, true)
})
test('a harness-provided plugin field rides the raw tool entry verbatim', () => {
const def = createContextHeadersDefinition()
const view = def.view(fold(def, [headerEvent(1, {
tools: [
{ name: 'mcp__github__get_issue', plugin: 'mcp:github' },
{ name: 'plain', plugin: '' }, // empty plugin behaves as absent
{ name: 'naked' },
],
})]))
const tools = view.headers[0].tools
assert.equal(tools[0].plugin, 'mcp:github')
assert.ok(!('plugin' in tools[1]))
assert.ok(!('plugin' in tools[2]))
})
test('the resolver fills an absent plugin at view time and never overrides a logged one', () => {
const resolve = (name: string): string | undefined =>
name === 'bash' ? '@deepseek-ai/dsh-tool-bash' : name === 'mcp__github__x' ? 'mcp:github' : undefined
const def = createContextHeadersDefinition(resolve)
const view = def.view(fold(def, [headerEvent(1, {
tools: [
{ name: 'bash' },
{ name: 'mcp__github__x' },
{ name: 'bash', plugin: 'logged-owner' }, // logged wins over the resolver
{ name: 'unknown' },
],
})]))
const tools = view.headers[0].tools
assert.equal(tools[0].plugin, '@deepseek-ai/dsh-tool-bash')
assert.equal(tools[1].plugin, 'mcp:github')
assert.equal(tools[2].plugin, 'logged-owner')
assert.ok(!('plugin' in tools[3]))
// The fill is a view-time projection: the folded state stays pure.
assert.ok(!('plugin' in fold(def, [headerEvent(9, { tools: [{ name: 'bash' }] })]).headers[0].tools[0]))
})
test('without a resolver no plugin is ever added', () => {
const def = createContextHeadersDefinition()
const view = def.view(fold(def, [headerEvent(1, { tools: [{ name: 'mcp__github__x' }] })]))
assert.ok(!('plugin' in view.headers[0].tools[0]))
})
test('plugin-bearing state and views pass both schemas', () => {
const def = createContextHeadersDefinition(name => (name === 'bash' ? '@deepseek-ai/dsh-tool-bash' : undefined))
const state = fold(def, [headerEvent(1, {
tools: [{ name: 'bash', plugin: 'mcp:github' }, { name: 'read' }],
})])
const view = def.view(state)
assert.equal(def.schema.safeParse(view).success, true)
const c = compat(def)
assert.deepEqual(c.stateSchema.parse(structuredClone(state)), state)
assert.equal(c.wire.viewSchema.safeParse(c.wire.view(state)).success, true)
})
})
describe('hostile tool entries', () => {
test('null and primitive tool entries degrade to unnamed JSON-priced tools', () => {
const def = createContextHeadersDefinition()
const view = def.view(fold(def, [headerEvent(1, {
tools: [null, 42, 'x', { name: 'bash' }],
})]))
const tools = view.headers[0].tools
assert.equal(tools.length, 4)
assert.equal(tools[0].name, '?')
assert.equal(tools[1].name, '?')
assert.equal(tools[2].name, '?')
assert.equal(tools[3].name, 'bash')
for (const tool of tools) {
assert.ok(Number.isInteger(tool.tokens) && tool.tokens >= 0, 'tool tokens priced')
assert.ok('schema' in tool, 'the raw schema rides the record')
}
})
})