Files
deepseek-harness/examples/desktop/test/subagent-lineage.test.js
ZiyaZhang e8f5c0b51b feat(desktop): DSH Electron desktop shell — harness internals visualized
Minimal Electron shell over the DSH JSON-RPC runtime — a first-look at
what a ChatGPT.app-style host on top of the DeepSeek Harness looks
like, with the harness's normally-invisible internals (trace timeline,
context surface, subagent tree, compaction, plugin registry, rubrics)
brought forward as first-class UI surfaces so plugin authors and
researchers can see what the agent is actually doing.

Runs against three keyless-to-live profiles (stdio-echo works on
master out of the box; daemon-echo / daemon-vibe-echo activate once
the daemon-demo lands; stdio-deepseek and daemon-vibe hit the real
DeepSeek API when you supply a key). HARNESS_DEV auto-resolves to the
in-repo runtime when this shell ships under examples/desktop/, so a
fresh clone launches without config; env DSH_DEV_ROOT overrides for
custom layouts, and a sibling deepseek-harness-dev/ checkout is the
original dev workflow.

Cold-clone gate (P0 fixes for first-time-clone usability):
- HARNESS_DEV: 3-candidate resolver (env → walk-up in-repo marker →
  sibling), unit-tested via mock fs so ordering is locked without
  needing either real layout on disk.
- config yml leaves rewritten at assemble time so the sibling-clone
  paths (../../deepseek-harness-dev/examples/echo-agent/…) become
  the in-repo paths (../../echo-agent/…) in the released tree —
  source yml stays usable for local dev, released tree ships a
  working shape.
- pnpm-workspace.yaml allowBuilds.electron = true (was placeholder).
- missing-key card in stdio-deepseek offers a one-click switch to
  stdio-echo (the keyless profile that works on master) rather than
  daemon-echo (blocked on the not-yet-shipped daemon-demo).
- assemble-oss-release.sh rewrites the source-side breadcrumb name
  'dsh-desktop-demo' → 'dsh-desktop' for the released package.json.

FOUC guard on the onboarding gate (41fc5df carried) keeps the
first-launch splash from flashing before the runtime probe finishes.

Test suite (1634 tests in source, 3990 in the runtime repo) covers
resolver ordering, renderer classifiers, trace timeline shape,
compaction diff rendering, rubric parity, and the missing-key
onboarding paths.
2026-07-18 12:59:34 -07:00

131 lines
5.1 KiB
JavaScript

// Ticket #15 A (2026-07-17) — subagent-lineage pure module tests.
//
// Locks the routing contract: registerStarted → resolveChild returns the
// record, pushChildEvent appends to childEvents, markFinished flips
// running=false and merges patch fields, forget removes the record, and
// the spawn anchor heuristic reads meta.lastSpawnCallId.
'use strict'
const test = require('node:test')
const assert = require('node:assert/strict')
const {
createSubagentLineage,
isSpawnAgentToolCall,
spawnAnchorFor,
SPAWN_TOOL_NAMES,
} = require('../src/renderer/subagent-lineage.js')
test('registerStarted stores lineage and resolveChild returns it', () => {
const store = createSubagentLineage()
const rec = store.registerStarted({
parentSessionId: 'root-1',
childSessionId: 'child-1',
parentCallId: 'call_spawn_1',
})
assert.equal(rec.parentSessionId, 'root-1')
assert.equal(rec.childSessionId, 'child-1')
assert.equal(rec.parentCallId, 'call_spawn_1')
assert.equal(rec.running, true)
assert.equal(rec.status, 'running')
assert.deepEqual(rec.childEvents, [])
const found = store.resolveChild('child-1')
assert.strictEqual(found, rec)
})
test('registerStarted is idempotent — repeat call returns the same record', () => {
const store = createSubagentLineage()
const first = store.registerStarted({
parentSessionId: 'root-1', childSessionId: 'child-1', parentCallId: null,
})
const second = store.registerStarted({
parentSessionId: 'root-1', childSessionId: 'child-1', parentCallId: 'call_late',
})
assert.strictEqual(second, first)
// Late-arriving parentCallId is adopted so a heuristic race doesn't
// permanently strand the anchor.
assert.equal(first.parentCallId, 'call_late')
})
test('registerStarted returns null when childSessionId missing', () => {
const store = createSubagentLineage()
assert.equal(store.registerStarted({ parentSessionId: 'root-1' }), null)
})
test('resolveChild returns null for unknown ids and no lookup for null', () => {
const store = createSubagentLineage()
store.registerStarted({ parentSessionId: 'p', childSessionId: 'c' })
assert.equal(store.resolveChild('nope'), null)
assert.equal(store.resolveChild(null), null)
assert.equal(store.resolveChild(undefined), null)
})
test('pushChildEvent appends to the lineage record and preserves order', () => {
const store = createSubagentLineage()
store.registerStarted({ parentSessionId: 'root', childSessionId: 'c1' })
store.pushChildEvent('c1', { type: 'assistant/chunk', seq: 1 })
store.pushChildEvent('c1', { type: 'tool/call', seq: 2 })
store.pushChildEvent('c1', { type: 'tool/result', seq: 3 })
const rec = store.resolveChild('c1')
assert.equal(rec.childEvents.length, 3)
assert.deepEqual(rec.childEvents.map((e) => e.seq), [1, 2, 3])
})
test('pushChildEvent no-ops for unknown ids', () => {
const store = createSubagentLineage()
assert.equal(store.pushChildEvent('missing', { type: 'x' }), null)
})
test('attachCard stashes DOM handles for later render access', () => {
const store = createSubagentLineage()
store.registerStarted({ parentSessionId: 'root', childSessionId: 'c1' })
const cardEl = { id: 'card' }
const bodyEl = { id: 'body' }
const rec = store.attachCard('c1', { cardEl, bodyEl })
assert.strictEqual(rec.cardEl, cardEl)
assert.strictEqual(rec.bodyEl, bodyEl)
})
test('markFinished flips running=false and merges status/lastAssistantMessage', () => {
const store = createSubagentLineage()
store.registerStarted({ parentSessionId: 'root', childSessionId: 'c1' })
const last = [{ type: 'text', text: '{"count":1}' }]
const rec = store.markFinished('c1', { status: 'ok', lastAssistantMessage: last })
assert.equal(rec.running, false)
assert.equal(rec.status, 'ok')
assert.strictEqual(rec.lastAssistantMessage, last)
})
test('forget removes the lineage record', () => {
const store = createSubagentLineage()
store.registerStarted({ parentSessionId: 'root', childSessionId: 'c1' })
assert.equal(store.size(), 1)
store.forget('c1')
assert.equal(store.size(), 0)
assert.equal(store.resolveChild('c1'), null)
})
test('isSpawnAgentToolCall matches the canonical spawn tool names', () => {
for (const name of SPAWN_TOOL_NAMES) {
assert.equal(isSpawnAgentToolCall({ type: 'tool/call', data: { name } }), true)
}
assert.equal(isSpawnAgentToolCall({ type: 'tool/call', data: { name: 'grep' } }), false)
assert.equal(isSpawnAgentToolCall({ type: 'tool/result', data: { name: 'spawn_agent' } }), false)
assert.equal(isSpawnAgentToolCall(null), false)
})
test('spawnAnchorFor reads meta.lastSpawnCallId', () => {
assert.equal(spawnAnchorFor({ lastSpawnCallId: 'call_spawn_9' }), 'call_spawn_9')
assert.equal(spawnAnchorFor({}), null)
assert.equal(spawnAnchorFor(null), null)
})
test('two distinct child sessions do not collide', () => {
const store = createSubagentLineage()
store.registerStarted({ parentSessionId: 'root', childSessionId: 'c1', parentCallId: 'a' })
store.registerStarted({ parentSessionId: 'root', childSessionId: 'c2', parentCallId: 'b' })
assert.equal(store.resolveChild('c1').parentCallId, 'a')
assert.equal(store.resolveChild('c2').parentCallId, 'b')
assert.equal(store.size(), 2)
})