|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { createStreamingContext } from '@/lib/copilot/request/context/request-context' |
| 3 | +import { makeResumeLegContext, mergeResumeLegOutputs } from '@/lib/copilot/request/lifecycle/run' |
| 4 | + |
| 5 | +// Guards the makeResumeLegContext / mergeResumeLegOutputs contract: the two MUST |
| 6 | +// stay in lockstep (every per-leg-isolated scalar is reset on leg creation and |
| 7 | +// folded back on merge), and the heavy accumulators stay shared by reference so |
| 8 | +// all concurrent legs build one chat. This is the regression the inline comment |
| 9 | +// warns about — without per-leg isolation the orchestrator's pre-fanout content |
| 10 | +// gets multiplied by the leg count on merge. |
| 11 | +describe('resume leg context isolate/merge contract', () => { |
| 12 | + it('isolates the per-leg scalars while sharing the heavy accumulators by reference', () => { |
| 13 | + const base = createStreamingContext({ |
| 14 | + accumulatedContent: 'PRE', |
| 15 | + finalAssistantContent: 'PRE-FINAL', |
| 16 | + usage: { prompt: 10, completion: 5 }, |
| 17 | + cost: { input: 1, output: 2, total: 3 }, |
| 18 | + errors: ['pre-existing'], |
| 19 | + }) |
| 20 | + |
| 21 | + const leg = makeResumeLegContext(base) |
| 22 | + |
| 23 | + // Per-leg scalars reset so a leg accumulates only its OWN output. |
| 24 | + expect(leg.accumulatedContent).toBe('') |
| 25 | + expect(leg.finalAssistantContent).toBe('') |
| 26 | + expect(leg.usage).toBeUndefined() |
| 27 | + expect(leg.cost).toBeUndefined() |
| 28 | + expect(leg.errors).toEqual([]) |
| 29 | + expect(leg.streamComplete).toBe(false) |
| 30 | + expect(leg.awaitingAsyncContinuation).toBeUndefined() |
| 31 | + |
| 32 | + // A leg's own errors array is a fresh array (not the shared one) so a leg's |
| 33 | + // retry rollback can't truncate a sibling's errors. |
| 34 | + expect(leg.errors).not.toBe(base.errors) |
| 35 | + |
| 36 | + // Heavy accumulators stay shared by reference (one merged chat). |
| 37 | + expect(leg.contentBlocks).toBe(base.contentBlocks) |
| 38 | + expect(leg.toolCalls).toBe(base.toolCalls) |
| 39 | + expect(leg.pendingToolPromises).toBe(base.pendingToolPromises) |
| 40 | + expect(leg.subAgentContent).toBe(base.subAgentContent) |
| 41 | + }) |
| 42 | + |
| 43 | + it('folds a leg back exactly once (no double-count of the orchestrator content)', () => { |
| 44 | + const base = createStreamingContext({ accumulatedContent: 'PRE', errors: ['pre'] }) |
| 45 | + |
| 46 | + const leg = makeResumeLegContext(base) |
| 47 | + leg.accumulatedContent = 'JOIN' |
| 48 | + leg.finalAssistantContent = 'JOIN-FINAL' |
| 49 | + leg.usage = { prompt: 100, completion: 50 } |
| 50 | + leg.cost = { input: 4, output: 5, total: 9 } |
| 51 | + leg.errors.push('leg-err') |
| 52 | + |
| 53 | + mergeResumeLegOutputs(base, leg) |
| 54 | + |
| 55 | + // PRE seeded once + the leg's own output appended once — not PRE+PRE+JOIN. |
| 56 | + expect(base.accumulatedContent).toBe('PREJOIN') |
| 57 | + expect(base.finalAssistantContent).toBe('JOIN-FINAL') |
| 58 | + expect(base.usage).toEqual({ prompt: 100, completion: 50 }) |
| 59 | + expect(base.cost).toEqual({ input: 4, output: 5, total: 9 }) |
| 60 | + expect(base.errors).toEqual(['pre', 'leg-err']) |
| 61 | + }) |
| 62 | + |
| 63 | + it('does not multiply pre-fanout content across many legs (N children + one join leg)', () => { |
| 64 | + const base = createStreamingContext({ accumulatedContent: 'PRE' }) |
| 65 | + |
| 66 | + // Seven child legs that stream subagent content (not main accumulatedContent) |
| 67 | + // contribute nothing to the join scalars; only the join-carrying leg does. |
| 68 | + for (let i = 0; i < 7; i++) { |
| 69 | + const childLeg = makeResumeLegContext(base) |
| 70 | + mergeResumeLegOutputs(base, childLeg) |
| 71 | + } |
| 72 | + const joinLeg = makeResumeLegContext(base) |
| 73 | + joinLeg.accumulatedContent = 'SUMMARY' |
| 74 | + joinLeg.usage = { prompt: 1, completion: 1 } |
| 75 | + mergeResumeLegOutputs(base, joinLeg) |
| 76 | + |
| 77 | + // Exactly the pre-fanout content + the one join leg's summary — the 7 child |
| 78 | + // legs must not each re-append 'PRE'. |
| 79 | + expect(base.accumulatedContent).toBe('PRESUMMARY') |
| 80 | + expect(base.usage).toEqual({ prompt: 1, completion: 1 }) |
| 81 | + }) |
| 82 | +}) |
0 commit comments