Files
simstudioai--sim/apps/sim/lib/copilot/request/lifecycle/resume-leg-context.test.ts
T
wehub-resource-sync d25d482dc2
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

83 lines
3.7 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { createStreamingContext } from '@/lib/copilot/request/context/request-context'
import { makeResumeLegContext, mergeResumeLegOutputs } from '@/lib/copilot/request/lifecycle/run'
// Guards the makeResumeLegContext / mergeResumeLegOutputs contract: the two MUST
// stay in lockstep (every per-leg-isolated scalar is reset on leg creation and
// folded back on merge), and the heavy accumulators stay shared by reference so
// all concurrent legs build one chat. This is the regression the inline comment
// warns about — without per-leg isolation the orchestrator's pre-fanout content
// gets multiplied by the leg count on merge.
describe('resume leg context isolate/merge contract', () => {
it('isolates the per-leg scalars while sharing the heavy accumulators by reference', () => {
const base = createStreamingContext({
accumulatedContent: 'PRE',
finalAssistantContent: 'PRE-FINAL',
usage: { prompt: 10, completion: 5 },
cost: { input: 1, output: 2, total: 3 },
errors: ['pre-existing'],
})
const leg = makeResumeLegContext(base)
// Per-leg scalars reset so a leg accumulates only its OWN output.
expect(leg.accumulatedContent).toBe('')
expect(leg.finalAssistantContent).toBe('')
expect(leg.usage).toBeUndefined()
expect(leg.cost).toBeUndefined()
expect(leg.errors).toEqual([])
expect(leg.streamComplete).toBe(false)
expect(leg.awaitingAsyncContinuation).toBeUndefined()
// A leg's own errors array is a fresh array (not the shared one) so a leg's
// retry rollback can't truncate a sibling's errors.
expect(leg.errors).not.toBe(base.errors)
// Heavy accumulators stay shared by reference (one merged chat).
expect(leg.contentBlocks).toBe(base.contentBlocks)
expect(leg.toolCalls).toBe(base.toolCalls)
expect(leg.pendingToolPromises).toBe(base.pendingToolPromises)
expect(leg.subAgentContent).toBe(base.subAgentContent)
})
it('folds a leg back exactly once (no double-count of the orchestrator content)', () => {
const base = createStreamingContext({ accumulatedContent: 'PRE', errors: ['pre'] })
const leg = makeResumeLegContext(base)
leg.accumulatedContent = 'JOIN'
leg.finalAssistantContent = 'JOIN-FINAL'
leg.usage = { prompt: 100, completion: 50 }
leg.cost = { input: 4, output: 5, total: 9 }
leg.errors.push('leg-err')
mergeResumeLegOutputs(base, leg)
// PRE seeded once + the leg's own output appended once — not PRE+PRE+JOIN.
expect(base.accumulatedContent).toBe('PREJOIN')
expect(base.finalAssistantContent).toBe('JOIN-FINAL')
expect(base.usage).toEqual({ prompt: 100, completion: 50 })
expect(base.cost).toEqual({ input: 4, output: 5, total: 9 })
expect(base.errors).toEqual(['pre', 'leg-err'])
})
it('does not multiply pre-fanout content across many legs (N children + one join leg)', () => {
const base = createStreamingContext({ accumulatedContent: 'PRE' })
// Seven child legs that stream subagent content (not main accumulatedContent)
// contribute nothing to the join scalars; only the join-carrying leg does.
for (let i = 0; i < 7; i++) {
const childLeg = makeResumeLegContext(base)
mergeResumeLegOutputs(base, childLeg)
}
const joinLeg = makeResumeLegContext(base)
joinLeg.accumulatedContent = 'SUMMARY'
joinLeg.usage = { prompt: 1, completion: 1 }
mergeResumeLegOutputs(base, joinLeg)
// Exactly the pre-fanout content + the one join leg's summary — the 7 child
// legs must not each re-append 'PRE'.
expect(base.accumulatedContent).toBe('PRESUMMARY')
expect(base.usage).toEqual({ prompt: 1, completion: 1 })
})
})