|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// |
| 3 | +// Direct unit coverage for the subagent attribution ladder. The tracker is a |
| 4 | +// plain class, so these tests drive it without the stream-manager bridge. |
| 5 | +// Reaching rungs 1/2 THROUGH the bridge additionally requires the child's |
| 6 | +// `values` event (carrying a human first message) to arrive before any child |
| 7 | +// `messages` event — an ordering no bridge test encodes, which is why ladder |
| 8 | +// coverage lives here instead. |
| 9 | +import { describe, it, expect } from 'vitest'; |
| 10 | +import type { BaseMessage } from '@langchain/core/messages'; |
| 11 | +import { SubagentTracker, childStreamRefFromNamespace } from './subagent-tracker'; |
| 12 | + |
| 13 | +function taskCall(id: string, args: Record<string, unknown>) { |
| 14 | + return { id, name: 'task', args: { subagent_type: 'researcher', ...args } }; |
| 15 | +} |
| 16 | + |
| 17 | +function aiMsg(id: string, content: string): BaseMessage { |
| 18 | + return { id, type: 'ai', content } as unknown as BaseMessage; |
| 19 | +} |
| 20 | + |
| 21 | +describe('SubagentTracker attribution ladder', () => { |
| 22 | + it('rung 1: exact description match wins even with multiple candidates', () => { |
| 23 | + const t = new SubagentTracker(); |
| 24 | + t.registerFromToolCalls([ |
| 25 | + taskCall('call_a', { description: 'Summarize the meeting notes' }), |
| 26 | + taskCall('call_b', { description: 'Research quantum signals' }), |
| 27 | + ]); |
| 28 | + // Namespace id is an internal UUID — deliberately NOT a tool-call id, so |
| 29 | + // nothing but the ladder can resolve it. Two candidates outstanding, so |
| 30 | + // the positional rung would refuse; only the exact rung can attribute. |
| 31 | + const winner = t.matchSubgraphToSubagent('ns-uuid-1', 'Research quantum signals'); |
| 32 | + expect(winner).toBe('call_b'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('rung 2: substring match (either direction) wins when exact fails', () => { |
| 36 | + const t = new SubagentTracker(); |
| 37 | + t.registerFromToolCalls([ |
| 38 | + taskCall('call_a', { description: 'Summarize the meeting notes' }), |
| 39 | + taskCall('call_b', { description: 'Research quantum signals' }), |
| 40 | + ]); |
| 41 | + // The child's first human message elaborates on the stored description. |
| 42 | + const winner = t.matchSubgraphToSubagent( |
| 43 | + 'ns-uuid-2', |
| 44 | + 'Research quantum signals across the 2025 arxiv corpus', |
| 45 | + ); |
| 46 | + expect(winner).toBe('call_b'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('rung 2: an empty stored description is never a substring match', () => { |
| 50 | + const t = new SubagentTracker(); |
| 51 | + t.registerFromToolCalls([ |
| 52 | + taskCall('call_a', { description: '' }), |
| 53 | + taskCall('call_b', { description: 'Book a flight' }), |
| 54 | + ]); |
| 55 | + // 'anything' contains '' — without the guard at the substring rung, |
| 56 | + // call_a would claim every stream. It must not. |
| 57 | + const winner = t.matchSubgraphToSubagent('ns-uuid-3', 'anything unrelated'); |
| 58 | + expect(winner).toBeUndefined(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('rung 3: positional fallback attributes only when exactly one candidate is outstanding', () => { |
| 62 | + const t = new SubagentTracker(); |
| 63 | + t.registerFromToolCalls([taskCall('call_solo', { task_description: 'x' })]); |
| 64 | + expect(t.matchSubgraphToSubagent('ns-uuid-4', '')).toBe('call_solo'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('rung 3: refuses with two outstanding candidates and buffers instead', () => { |
| 68 | + const t = new SubagentTracker(); |
| 69 | + t.registerFromToolCalls([ |
| 70 | + taskCall('call_a', { task_description: 'x' }), |
| 71 | + taskCall('call_b', { task_description: 'y' }), |
| 72 | + ]); |
| 73 | + expect(t.matchSubgraphToSubagent('ns-uuid-5', '')).toBeUndefined(); |
| 74 | + |
| 75 | + // Unattributed messages are held, not dropped and not mis-assigned. |
| 76 | + t.addMessageToSubagent('ns-uuid-5', aiMsg('m1', 'early chunk')); |
| 77 | + // getSubagents() hides 'pending' entries — this loop is empty when |
| 78 | + // correct, and bites only when a mutant wrongly establishes the match |
| 79 | + // and promotes a candidate to 'running'. |
| 80 | + for (const subagent of t.getSubagents().values()) { |
| 81 | + expect(subagent.messages).toHaveLength(0); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + it('deferred retry: a pending match resolves when the tool call registers later', () => { |
| 86 | + const t = new SubagentTracker(); |
| 87 | + // Child stream arrives BEFORE the parent's tool call — nothing to match yet. |
| 88 | + expect(t.matchSubgraphToSubagent('ns-uuid-6', 'Find flights to Lisbon')).toBeUndefined(); |
| 89 | + t.addMessageToSubagent('ns-uuid-6', aiMsg('m1', 'checking fares')); |
| 90 | + |
| 91 | + // Parent tool call registers; registerFromToolCalls drains pendingMatches. |
| 92 | + t.registerFromToolCalls([taskCall('call_late', { description: 'Find flights to Lisbon' })]); |
| 93 | + |
| 94 | + const subagent = t.getSubagents().get('call_late'); |
| 95 | + expect(subagent?.status).toBe('running'); |
| 96 | + expect(subagent?.messages).toEqual([ |
| 97 | + expect.objectContaining({ id: 'm1', content: 'checking fares' }), |
| 98 | + ]); |
| 99 | + }); |
| 100 | + |
| 101 | + it('empty-description attribution never exact-matches an empty stored description', () => { |
| 102 | + const t = new SubagentTracker(); |
| 103 | + t.registerFromToolCalls([ |
| 104 | + taskCall('call_a', { description: '' }), |
| 105 | + taskCall('call_b', { description: 'Book a flight' }), |
| 106 | + ]); |
| 107 | + // ensureToolStreamAttribution runs the ladder with '' — with two |
| 108 | + // candidates outstanding it must refuse (positional rung), not let |
| 109 | + // '' === '' claim call_a at the exact rung. |
| 110 | + t.ensureToolStreamAttribution('ns-uuid-7'); |
| 111 | + t.addMessageToSubagent('ns-uuid-7', aiMsg('m1', 'child token')); |
| 112 | + // getSubagents() hides 'pending' entries — this loop is empty when |
| 113 | + // correct, and bites only when a mutant wrongly establishes the match |
| 114 | + // and promotes a candidate to 'running'. |
| 115 | + for (const subagent of t.getSubagents().values()) { |
| 116 | + expect(subagent.messages).toHaveLength(0); |
| 117 | + } |
| 118 | + }); |
| 119 | +}); |
| 120 | + |
| 121 | +describe('childStreamRefFromNamespace', () => { |
| 122 | + it('single tools: segment resolves to a tool child by tool-call id', () => { |
| 123 | + expect(childStreamRefFromNamespace(['tools:call-1'])).toEqual({ |
| 124 | + key: 'call-1', name: '', kind: 'tool', |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + it('a tool child followed by its own internal nodes stays a tool child', () => { |
| 129 | + // `model`/`agent` segments after the tools: segment are the child's own |
| 130 | + // graph internals, not a second delegation. |
| 131 | + expect(childStreamRefFromNamespace(['tools:call-1', 'agent:step-2'])).toEqual({ |
| 132 | + key: 'call-1', name: '', kind: 'tool', |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + it('plain subgraph namespace resolves to the first segment, named by node', () => { |
| 137 | + expect(childStreamRefFromNamespace(['research:uuid-1'])).toEqual({ |
| 138 | + key: 'research:uuid-1', name: 'research', kind: 'subgraph', |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + it('nested delegation registers as its own subgraph stream, never the outer tool child', () => { |
| 143 | + expect(childStreamRefFromNamespace(['tools:call-1', 'tools:call-2'])).toEqual({ |
| 144 | + key: 'tools:call-1|tools:call-2', name: 'tools', kind: 'subgraph', |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + it('nested delegation with intermediate segments still keys the full path', () => { |
| 149 | + expect(childStreamRefFromNamespace(['tools:call-1', 'agent:x', 'tools:call-2'])).toEqual({ |
| 150 | + key: 'tools:call-1|agent:x|tools:call-2', name: 'tools', kind: 'subgraph', |
| 151 | + }); |
| 152 | + }); |
| 153 | + |
| 154 | + it('trailing internal segments after the innermost tools: segment do not fragment the key', () => { |
| 155 | + expect(childStreamRefFromNamespace(['tools:call-1', 'tools:call-2', 'agent:x'])).toEqual({ |
| 156 | + key: 'tools:call-1|tools:call-2', name: 'tools', kind: 'subgraph', |
| 157 | + }); |
| 158 | + expect(childStreamRefFromNamespace(['tools:call-1', 'tools:call-2', 'model:y'])).toEqual({ |
| 159 | + key: 'tools:call-1|tools:call-2', name: 'tools', kind: 'subgraph', |
| 160 | + }); |
| 161 | + }); |
| 162 | +}); |
0 commit comments