agent-continuation.bench.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /** Baseline budgets for long-history requests, tool continuation, and fork-child discovery. */
  2. import { cp, mkdir, mkdtemp, rm } from 'node:fs/promises'
  3. import { availableParallelism, cpus, tmpdir } from 'node:os'
  4. import { join } from 'node:path'
  5. import { afterAll, beforeAll, describe, expect, it } from 'vitest'
  6. import { runBuiltBenchmarkWorker } from '../support/built-worker.ts'
  7. import { ciTimeBudget, PERFORMANCE_BUDGET_HEADROOM } from '../support/calibration.ts'
  8. import type { ContinuationReport } from './agent-continuation.worker.ts'
  9. import type { CatalogReport } from './child-catalog.worker.ts'
  10. import type { ProfileReport } from './profile-continuation.worker.ts'
  11. import { WORKLOAD } from './workload.ts'
  12. const ATTEMPTS = 5
  13. const WORKER_TIMEOUT_MS = 60_000
  14. /** M4 Pro / Node 24.19 baseline expectations, before shared CI scaling and variance headroom. */
  15. const EXPECTED_MS = { 'profile-continuation': 1_700 } as const
  16. /** Standard two-CPU hosted CI baseline request-history median is 582.304 ms. */
  17. const EXPECTED_BASELINE_REQUEST_CI_MS = 600
  18. const BASELINE_REQUEST_BUDGET_MS = Math.ceil(EXPECTED_BASELINE_REQUEST_CI_MS * PERFORMANCE_BUDGET_HEADROOM)
  19. /** Standard two-CPU hosted CI tool-continuation median is 898.252 ms. */
  20. const EXPECTED_TOOL_CONTINUATION_CI_MS = 900
  21. const TOOL_CONTINUATION_BUDGET_MS = Math.ceil(EXPECTED_TOOL_CONTINUATION_CI_MS * PERFORMANCE_BUDGET_HEADROOM)
  22. /** Standard two-CPU hosted CI catalog median is 858.364 ms; 900 ms is the rounded expectation. */
  23. const EXPECTED_CATALOG_CI_MS = 900
  24. const CATALOG_BUDGET_MS = Math.ceil(EXPECTED_CATALOG_CI_MS * PERFORMANCE_BUDGET_HEADROOM)
  25. /** Reviewed hosted limit: floor(238 × 1.25); calibration records the original reference. */
  26. const REQUEST_HISTORY_BUDGET_MS = 297
  27. const EXPECTED_RETAINED_HEAP_MB = 23
  28. const WORKERS = join(import.meta.dirname, '..', '.dsh-build', 'agent-continuation')
  29. type Scenario = 'request-history' | 'catalog' | 'tool-continuation' | keyof typeof EXPECTED_MS
  30. type Report = ContinuationReport | CatalogReport | ProfileReport
  31. function workerName(scenario: Scenario): string {
  32. if (scenario === 'profile-continuation') return 'profile-continuation.worker.js'
  33. return scenario === 'catalog' ? 'child-catalog.worker.js' : 'agent-continuation.worker.js'
  34. }
  35. async function run<Output>(root: string, scenario: Scenario, mode: string): Promise<Output> {
  36. const outcome = await runBuiltBenchmarkWorker<Output>({
  37. worker: join(WORKERS, workerName(scenario)), args: [root, mode],
  38. timeoutMs: WORKER_TIMEOUT_MS, exposeGc: true,
  39. })
  40. if (outcome.timedOut || outcome.signal !== null || outcome.exitCode !== 0 || outcome.report === undefined) {
  41. throw new Error('backend worker failed: ' + JSON.stringify(outcome))
  42. }
  43. return outcome.report
  44. }
  45. function median(values: readonly number[]): number {
  46. return [...values].sort((a, b) => a - b)[Math.floor(values.length / 2)] as number
  47. }
  48. function expectTotalWithinBudget(value: number, budget: number): void {
  49. expect(value).toBeLessThanOrEqual(budget)
  50. }
  51. describe('standard hosted catalog calibration', () => {
  52. it('accepts the recorded two-CPU samples that exceed the historical budget', () => {
  53. const recordedMedian = median([797.373945, 883.157358, 858.363927, 790.568538, 904.5785669999999])
  54. expect(recordedMedian).toBe(858.363927)
  55. expect(() => expectTotalWithinBudget(recordedMedian, 800)).toThrow()
  56. expectTotalWithinBudget(recordedMedian, CATALOG_BUDGET_MS)
  57. expect(CATALOG_BUDGET_MS).toBe(1_125)
  58. })
  59. it('rejects a synthetic material catalog regression', () => {
  60. const regressionMedian = median([1_380, 1_400, 1_420, 1_410, 1_390])
  61. expect(regressionMedian).toBe(1_400)
  62. expect(() => expectTotalWithinBudget(regressionMedian, CATALOG_BUDGET_MS)).toThrow()
  63. })
  64. })
  65. describe('standard hosted tool-continuation calibration', () => {
  66. it('accepts recorded two-CPU samples but rejects a material regression', () => {
  67. const recordedMedian = median([917.006744, 892.091482, 887.838867, 905.6594390000001, 898.2517579999999])
  68. expect(recordedMedian).toBe(898.2517579999999)
  69. expect(() => expectTotalWithinBudget(recordedMedian, 850)).toThrow()
  70. expectTotalWithinBudget(recordedMedian, TOOL_CONTINUATION_BUDGET_MS)
  71. expect(TOOL_CONTINUATION_BUDGET_MS).toBe(1_125)
  72. expect(() => expectTotalWithinBudget(1_400, TOOL_CONTINUATION_BUDGET_MS)).toThrow()
  73. })
  74. })
  75. describe('standard hosted baseline request-history calibration', () => {
  76. it('accepts recorded two-CPU samples but rejects a material regression', () => {
  77. const recordedMedian = median([618.598065, 618.606407, 582.0351149999999, 582.303506, 581.8318300000001])
  78. expect(recordedMedian).toBe(582.303506)
  79. expect(() => expectTotalWithinBudget(recordedMedian, 550)).toThrow()
  80. expectTotalWithinBudget(recordedMedian, BASELINE_REQUEST_BUDGET_MS)
  81. expect(BASELINE_REQUEST_BUDGET_MS).toBe(750)
  82. expect(() => expectTotalWithinBudget(900, BASELINE_REQUEST_BUDGET_MS)).toThrow()
  83. })
  84. })
  85. function assertRequestHistoryBudget(value: number): void {
  86. expect(value).toBeLessThanOrEqual(REQUEST_HISTORY_BUDGET_MS)
  87. }
  88. describe('standard hosted request-history calibration', () => {
  89. it('accepts the recorded two-CPU samples above the historical budget', () => {
  90. const recorded = [183.355397, 184.468253, 185.042397, 182.160790, 182.924728]
  91. const recordedMedian = median(recorded)
  92. expect(recordedMedian).toBe(183.355397)
  93. expect(recordedMedian).toBeGreaterThan(ciTimeBudget(70))
  94. assertRequestHistoryBudget(recordedMedian)
  95. assertRequestHistoryBudget(Math.max(...recorded))
  96. expect(REQUEST_HISTORY_BUDGET_MS).toBe(297)
  97. })
  98. it('rejects a synthetic material request-history regression', () => {
  99. const regressionMedian = median([308, 310, 312, 311, 309])
  100. expect(() => assertRequestHistoryBudget(regressionMedian)).toThrow()
  101. })
  102. it('accepts the observed slower hosted runners', () => {
  103. const recordedMedians = [
  104. [246.87661500000002, 246.88104699999997, 272.3702179999999, 265.796833, 272.50750700000003],
  105. [279.6894890000001, 297.79284899999993, 263.17839100000003, 252.66029200000003, 251.26736099999994],
  106. ].map(median)
  107. expect(recordedMedians).toEqual([265.796833, 263.17839100000003])
  108. for (const recordedMedian of recordedMedians) {
  109. expect(() => expectTotalWithinBudget(recordedMedian, 238)).toThrow()
  110. assertRequestHistoryBudget(recordedMedian)
  111. }
  112. })
  113. })
  114. describe('continuing tool-heavy Sessions with large histories', () => {
  115. let scratch: string | undefined
  116. const sources = new Map<Scenario, string>()
  117. beforeAll(async () => {
  118. scratch = await mkdtemp(join(tmpdir(), 'dsh-agent-continuation-bench-'))
  119. for (const scenario of ['request-history', 'catalog'] as const) {
  120. const root = join(scratch, 'source-' + scenario)
  121. await run(root, scenario, 'seed')
  122. sources.set(scenario, root)
  123. }
  124. sources.set('tool-continuation', sources.get('request-history') as string)
  125. })
  126. afterAll(async () => {
  127. if (scratch !== undefined) await rm(scratch, { recursive: true, force: true })
  128. })
  129. for (const scenario of ['request-history', 'tool-continuation', 'catalog', 'profile-continuation'] as const) {
  130. it(scenario, async () => {
  131. const samples: Report[] = []
  132. for (let attempt = 0; attempt < ATTEMPTS; attempt++) {
  133. const root = join(scratch as string, scenario + '-' + String(attempt))
  134. if (scenario === 'profile-continuation') await mkdir(root)
  135. else await cp(sources.get(scenario) as string, root, { recursive: true })
  136. try { samples.push(await run<Report>(root, scenario, scenario)) }
  137. finally { await rm(root, { recursive: true, force: true }) }
  138. }
  139. const totalMs = samples.map(sample => sample.totalMs)
  140. const budgetMs = scenario === 'request-history' ? REQUEST_HISTORY_BUDGET_MS
  141. : scenario === 'catalog' ? CATALOG_BUDGET_MS
  142. : scenario === 'tool-continuation' ? TOOL_CONTINUATION_BUDGET_MS : ciTimeBudget(EXPECTED_MS[scenario])
  143. const retainedHeapBudgetMb = EXPECTED_RETAINED_HEAP_MB * PERFORMANCE_BUDGET_HEADROOM
  144. console.log(JSON.stringify({
  145. benchmark: 'agent-continuation/' + scenario, workload: WORKLOAD,
  146. runtime: {
  147. cpuModels: [...new Set(cpus().map(cpu => cpu.model))],
  148. availableParallelism: availableParallelism(),
  149. platform: process.platform, arch: process.arch,
  150. node: process.version, v8: process.versions.v8,
  151. },
  152. samples, totalMs: { min: Math.min(...totalMs), median: median(totalMs), max: Math.max(...totalMs) },
  153. budgetMs, ...(scenario === 'tool-continuation' ? { retainedHeapBudgetMb } : {}),
  154. }))
  155. if (scenario === 'request-history') assertRequestHistoryBudget(median(totalMs))
  156. else expectTotalWithinBudget(median(totalMs), budgetMs)
  157. if (scenario === 'tool-continuation') {
  158. expect(median((samples as ContinuationReport[]).map(sample => sample.retainedHeapMb)))
  159. .toBeLessThanOrEqual(retainedHeapBudgetMb)
  160. }
  161. })
  162. }
  163. })