@torrent-tv/proxy 2.85.1 → 2.87.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (381) hide show
  1. package/CHANGELOG.md +2138 -1909
  2. package/CLAUDE.md +318 -197
  3. package/assets/calibration/NOTICE.md +2 -2
  4. package/bin/cli.js +684 -616
  5. package/biome.json +409 -233
  6. package/docs/container-architecture.md +218 -213
  7. package/docs/download-architecture.md +7 -7
  8. package/docs/encode-architecture.md +287 -11
  9. package/docs/encode-run-state.md +2 -2
  10. package/docs/logs.md +97 -97
  11. package/docs/{disk-architecture.md → storage-architecture.md} +107 -1
  12. package/openspec/changes/adaptive-bitrate/design.md +2 -2
  13. package/openspec/changes/adaptive-bitrate/proposal.md +1 -1
  14. package/openspec/changes/adaptive-bitrate/tasks.md +1 -1
  15. package/openspec/changes/chunked-request-bodies/design.md +1 -1
  16. package/openspec/changes/chunked-request-bodies/proposal.md +1 -1
  17. package/openspec/changes/chunked-request-bodies/tasks.md +1 -1
  18. package/openspec/changes/cold-start/design.md +1 -1
  19. package/openspec/changes/disk-cap/proposal.md +1 -1
  20. package/openspec/changes/proxy-observability/proposal.md +2 -2
  21. package/openspec/changes/track-selection/proposal.md +1 -1
  22. package/openspec/changes/transcode-quality/proposal.md +1 -1
  23. package/package.json +2 -2
  24. package/research/delivery-wedge-stand-ladder-2026-09-13.md +329 -329
  25. package/research/double-spawn-2026-09-10.md +171 -171
  26. package/routes/api/playback-plan/post.js +2 -2
  27. package/routes/api/sources/files/get.js +1 -1
  28. package/routes/api/sources/stats/get.js +1 -1
  29. package/routes/api/sources/warm/post.js +10 -3
  30. package/routes/api/subtitles/get.js +230 -230
  31. package/routes/api/transcode-sessions/fragment-far/post.js +3 -3
  32. package/routes/api/transcode-sessions/net-report/post.js +129 -94
  33. package/routes/api/transcode-sessions/post.js +160 -139
  34. package/routes/api/transcode-sessions/progress/get.js +3 -3
  35. package/routes/api/transcode-sessions/release/post.js +3 -3
  36. package/routes/api/transcode-sessions/seek/post.js +12 -3
  37. package/routes/stream/get.js +2 -2
  38. package/routes/transcode/audio-file/get.js +15 -6
  39. package/routes/transcode/audio-warm/get.js +8 -5
  40. package/routes/transcode/session-file/get.js +429 -268
  41. package/routes/transcode/variant-file/get.js +47 -7
  42. package/routes/transcode/variant-warm/get.js +18 -6
  43. package/scripts/render-run-graph.js +1 -1
  44. package/server.js +720 -465
  45. package/services/encode/CoverageMap.js +428 -428
  46. package/services/encode/CushionReport.js +345 -0
  47. package/services/encode/EncodeAdmission.js +332 -0
  48. package/services/{orchestrators → encode}/EncodeOrchestrator.js +285 -17
  49. package/services/encode/EncodeRun.js +16 -9
  50. package/services/encode/EncodeRuns.js +1219 -0
  51. package/services/encode/Encoder.js +18 -2
  52. package/services/encode/EncoderSelection.js +38 -0
  53. package/services/encode/LocalObservations.js +359 -0
  54. package/services/encode/NvencEncoder.js +5 -0
  55. package/services/encode/OutputOpening.js +745 -0
  56. package/services/encode/OutputTimes.js +579 -0
  57. package/services/encode/Renditions.js +2162 -0
  58. package/services/encode/RunProgress.js +84 -0
  59. package/services/encode/SoftwareEncoder.js +12 -7
  60. package/services/encode/args.js +402 -186
  61. package/services/encode/calibration.js +361 -0
  62. package/services/encode/contention.js +128 -128
  63. package/services/encode/encode-run-state.js +16 -20
  64. package/services/encode/fingerprint.js +322 -0
  65. package/services/{hwaccel.js → encode/hwaccel.js} +82 -42
  66. package/services/{learned-median.js → encode/learned-median.js} +11 -0
  67. package/services/encode/output/EncodedOutput.js +57 -0
  68. package/services/encode/output/Output.js +131 -0
  69. package/services/{output/LiveOutputs.js → encode/output/OutputCatalog.js} +141 -40
  70. package/services/encode/output/OutputSpec.js +390 -0
  71. package/services/{output → encode/output}/Timeline.js +12 -37
  72. package/services/encode/output/index.js +20 -0
  73. package/services/encode/output/ladder.js +32 -0
  74. package/services/{output → encode/output}/playlists.js +3 -0
  75. package/services/encode/output-key-format.js +21 -0
  76. package/services/encode/quality/EncodeCost.js +1083 -0
  77. package/services/encode/quality/HostLoad.js +525 -0
  78. package/services/encode/quality/HostTimings.js +249 -0
  79. package/services/encode/quality/OutputQualityState.js +28 -0
  80. package/services/encode/quality/QualityController.js +931 -0
  81. package/services/encode/quality/QualityOffer.js +416 -0
  82. package/services/encode/quality/drain-threat.js +47 -0
  83. package/services/encode/quality/link-budget.js +254 -0
  84. package/services/encode/quality/output-format.js +266 -0
  85. package/services/encode/quality/serving-output.js +216 -0
  86. package/services/encode/quality/visible-rung.js +57 -0
  87. package/services/encode/run-command.js +23 -20
  88. package/services/encode/run-inputs.js +42 -0
  89. package/services/{segment-formats → encode/segment-formats}/fmp4.js +63 -0
  90. package/services/{segment-formats → encode/segment-formats}/index.js +3 -3
  91. package/services/encode/segment-formats/init-compat.js +324 -0
  92. package/services/{segment-formats → encode/segment-formats}/mp4-boxes.js +8 -3
  93. package/services/encode/throughput.js +97 -0
  94. package/services/{orchestrators → media}/ContainerOrchestrator.js +45 -7
  95. package/services/media/KeyframeTables.js +316 -0
  96. package/services/{source → media}/SourceFile.js +7 -55
  97. package/services/{torrent-worker/subtitle-cues.js → media/SubtitleCues.js} +103 -132
  98. package/services/media/SubtitleOrchestrator.js +167 -0
  99. package/services/{audio-inventory.js → media/audio-inventory.js} +7 -1
  100. package/services/{container → media/container}/AviContainer.js +266 -266
  101. package/services/{container → media/container}/Container.js +596 -601
  102. package/services/media/container/ContainerFactory.js +56 -0
  103. package/services/media/container/KeyframeTable.js +272 -0
  104. package/services/{container → media/container}/MatroskaContainer.js +1485 -1485
  105. package/services/{container → media/container}/Mp4Container.js +1244 -1244
  106. package/services/{container → media/container}/SubtitleFileContainer.js +322 -322
  107. package/services/media/keyframe-probe.js +157 -0
  108. package/services/media/media-info-probe.js +93 -0
  109. package/services/{playback-planner.js → media/playback-planner.js} +82 -39
  110. package/services/{tracks → media/tracks}/AudioTrack.js +131 -131
  111. package/services/{tracks → media/tracks}/language-detect.js +174 -174
  112. package/services/server/OutputLifecycle.js +418 -0
  113. package/services/server/SegmentServing.js +1146 -0
  114. package/services/server/ViewerRequests.js +522 -0
  115. package/services/server/controllers/PlaybackController.js +36 -0
  116. package/services/{controllers → server/controllers}/SubtitleController.js +17 -33
  117. package/services/server/wire-outputs.js +958 -0
  118. package/services/storage/Diagnostics.js +202 -0
  119. package/services/storage/MachineBudget.js +235 -0
  120. package/services/{piece-store → storage}/allowance.js +107 -107
  121. package/services/{core-dumps.js → storage/core-dumps.js} +1 -1
  122. package/services/{files → storage/files}/CompletedFiles.js +116 -1
  123. package/services/{disk → storage}/free.js +33 -0
  124. package/services/storage/machine-memory.js +56 -0
  125. package/services/{memory-report.js → storage/memory-report.js} +575 -596
  126. package/services/{piece-store → storage/piece-store}/piece-disk-store.js +0 -7
  127. package/services/{piece-store → storage/piece-store}/shared-piece-store.js +28 -21
  128. package/services/{encode → storage/segment-store}/SegmentStore.js +395 -8
  129. package/services/storage/wire.js +175 -0
  130. package/services/{download → torrent/download}/SwarmSelection.js +2 -2
  131. package/services/{download → torrent/download}/registry.js +2 -2
  132. package/services/torrent/files.js +1 -1
  133. package/services/torrent/piece-store-of.js +29 -0
  134. package/services/{torrent-pool.js → torrent/torrent-pool.js} +26 -119
  135. package/services/{torrent-worker → torrent/worker}/background-fill.js +2 -2
  136. package/services/{torrent-worker → torrent/worker}/channel.js +271 -250
  137. package/services/{torrent-worker → torrent/worker}/client.js +77 -58
  138. package/services/torrent/worker/held-bytes.js +155 -0
  139. package/services/{torrent-worker → torrent/worker}/install-webrtc-shim.js +1 -1
  140. package/services/{torrent-worker → torrent/worker}/piece-reader.js +21 -30
  141. package/services/{torrent-worker → torrent/worker}/pool-adapter.js +101 -137
  142. package/services/{torrent-worker → torrent/worker}/protocol.js +26 -30
  143. package/services/torrent/worker/resume-warm.js +107 -0
  144. package/services/{torrent-worker → torrent/worker}/webrtc-shim.js +2 -2
  145. package/services/{torrent-worker → torrent/worker}/worker.js +68 -159
  146. package/services/{data-channel-handler.js → transport/data-channel-handler.js} +1356 -1356
  147. package/services/{delivery-probe.js → transport/delivery-probe.js} +842 -842
  148. package/services/{health-collector.js → transport/health-collector.js} +6 -35
  149. package/services/{packet-witness.js → transport/packet-witness.js} +808 -784
  150. package/services/{port-mapper.js → transport/port-mapper.js} +1 -1
  151. package/services/{usrsctp-state.js → transport/usrsctp-state.js} +1 -1
  152. package/services/{webrtc-manager.js → transport/webrtc-manager.js} +1 -1
  153. package/services/viewer/Assignments.js +340 -0
  154. package/services/{priority → viewer}/PriorityMap.js +390 -390
  155. package/services/{priority → viewer}/PriorityOrchestrator.js +325 -320
  156. package/services/viewer/Viewer.js +657 -489
  157. package/services/viewer/Viewers.js +368 -257
  158. package/services/viewer/active-output.js +37 -54
  159. package/services/viewer/audio-start.js +46 -0
  160. package/services/viewer/buffer-trend.js +81 -0
  161. package/services/viewer/choices.js +647 -0
  162. package/services/viewer/positions.js +126 -120
  163. package/services/viewer/report-intake.js +46 -44
  164. package/test/asset-paths.test.js +28 -0
  165. package/test/audio-inventory.test.js +3 -3
  166. package/test/audio-track-bitrate.test.js +40 -0
  167. package/test/auto-quality-step.test.js +707 -518
  168. package/test/available-share.test.js +1 -1
  169. package/test/background-fill.test.js +1 -1
  170. package/test/client-log-file.test.js +110 -110
  171. package/test/completed-files.test.js +1 -1
  172. package/test/concurrent-cost.test.js +13 -3
  173. package/test/container-media-info.test.js +3 -3
  174. package/test/core-dumps.test.js +1 -1
  175. package/test/coverage-follows-the-disk.test.js +40 -8
  176. package/test/cue-push-carries-what-the-walk-found.test.js +58 -0
  177. package/test/cut-times-timeline.test.js +2 -2
  178. package/test/cuts-follow-published-grid.test.js +9 -8
  179. package/test/data-channel-frame.test.js +1 -1
  180. package/test/dead-torrent-handle.test.js +1 -1
  181. package/test/declared-rates.test.js +2 -2
  182. package/test/declared-tracks.test.js +2 -2
  183. package/test/decode-cost-fit.test.js +1 -1
  184. package/test/decode-cost.test.js +34 -25
  185. package/test/delivery-probe.test.js +1 -1
  186. package/test/demand-register.test.js +4 -4
  187. package/test/dht-bootstrap.test.js +1 -1
  188. package/test/drain-threat.test.js +65 -0
  189. package/test/duplicate-add.test.js +1 -1
  190. package/test/encode-admission.test.js +146 -0
  191. package/test/encode-cost.test.js +199 -10
  192. package/test/encode-orchestrator.test.js +39 -1
  193. package/test/encode-plan-viewers.test.js +2 -2
  194. package/test/encode-run.test.js +2 -2
  195. package/test/encode-slope.test.js +1 -1
  196. package/test/encoded-output.test.js +39 -0
  197. package/test/encoder-readings.test.js +1 -1
  198. package/test/every-claimant-is-told-its-share.test.js +152 -0
  199. package/test/failed-encoder-still-serves-what-exists.test.js +354 -0
  200. package/test/failed-start-breaker.test.js +81 -30
  201. package/test/fastest-wires.test.js +1 -1
  202. package/test/ffmpeg-stream-counts.test.js +1 -1
  203. package/test/file-edges.test.js +3 -3
  204. package/test/fmp4-self-contained.test.js +1 -1
  205. package/test/fmp4-timestamps.test.js +1 -1
  206. package/test/fmp4-track-completeness.test.js +1 -1
  207. package/test/format-keyed-outputs.test.js +78 -0
  208. package/test/health-metrics.test.js +3 -2
  209. package/test/held-request-width.test.js +22 -23
  210. package/test/helpers/encode-run.js +136 -136
  211. package/test/helpers/held-file.js +29 -0
  212. package/test/helpers/manager.js +57 -0
  213. package/test/helpers/output-spec.js +63 -0
  214. package/test/host-load.test.js +1 -1
  215. package/test/index-accuracy-is-about-the-file.test.js +128 -0
  216. package/test/init-compat.test.js +219 -0
  217. package/test/input-lost-quiets-the-plan.test.js +1 -1
  218. package/test/input-unavailable.test.js +1 -1
  219. package/test/keeping-period.test.js +4 -4
  220. package/test/keyframe-index-accuracy.test.js +97 -45
  221. package/test/keyframe-table-budget.test.js +28 -33
  222. package/test/keyframes-belong-to-the-file.test.js +174 -57
  223. package/test/learned-median.test.js +1 -1
  224. package/test/letting-go-asks-who-is-watching.test.js +191 -0
  225. package/test/limit-row-by-area.test.js +60 -0
  226. package/test/link-budget.test.js +111 -0
  227. package/test/link-probe.test.js +56 -56
  228. package/test/local-encoding-data.test.js +273 -0
  229. package/test/look-ahead-coverage.test.js +1 -1
  230. package/test/manager-surface.test.js +294 -0
  231. package/test/matroska-blocks.test.js +0 -0
  232. package/test/matroska-cues-track.test.js +1 -1
  233. package/test/memory-budget.test.js +2 -2
  234. package/test/mp4-composition-times.test.js +0 -0
  235. package/test/mp4-subtitles.test.js +1 -1
  236. package/test/one-authority.test.js +45 -41
  237. package/test/one-budget.test.js +149 -0
  238. package/test/one-output-one-session.test.js +204 -0
  239. package/test/{disk-space.test.js → one-resource-divided.test.js} +30 -10
  240. package/test/one-serving-output-per-height.test.js +458 -0
  241. package/test/orchestrator-wired.test.js +46 -45
  242. package/test/output-catalog.test.js +39 -0
  243. package/test/output-shape.test.js +4 -51
  244. package/test/output-spec.test.js +251 -157
  245. package/test/output-speed.test.js +17 -15
  246. package/test/outputs-being-prepared.test.js +40 -0
  247. package/test/packet-witness-ring.test.js +2 -2
  248. package/test/packet-witness.test.js +1 -1
  249. package/test/piece-disk-store.test.js +2 -2
  250. package/test/piece-from-whole-file.test.js +1 -1
  251. package/test/piece-lru.test.js +1 -1
  252. package/test/piece-reader.test.js +2 -2
  253. package/test/piece-store-eviction.test.js +1 -1
  254. package/test/piece-store-never-refuses.test.js +1 -1
  255. package/test/piece-store-reservations.test.js +1 -1
  256. package/test/piece-store-slow-disk.test.js +1 -1
  257. package/test/piece-tail.test.js +1 -1
  258. package/test/plan-host-timings.test.js +1 -1
  259. package/test/plan-is-steady.test.js +225 -225
  260. package/test/priority-map-download.test.js +4 -4
  261. package/test/priority-map-emptied.test.js +204 -205
  262. package/test/priority-map-per-output.test.js +215 -209
  263. package/test/priority-map.test.js +1 -1
  264. package/test/probe-wedge-certainty.test.js +1 -1
  265. package/test/produced-copy-choice.test.js +15 -60
  266. package/test/quality-offer.test.js +162 -0
  267. package/test/quality-variants.test.js +1229 -1167
  268. package/test/rate-control-in-the-output.test.js +180 -0
  269. package/test/read-survives-withdrawal.test.js +2 -2
  270. package/test/read-window.test.js +10 -3
  271. package/test/resume-warm.test.js +1 -1
  272. package/test/run-inputs.test.js +80 -0
  273. package/test/run-position-follows-published-grid.test.js +13 -11
  274. package/test/run-progress.test.js +65 -0
  275. package/test/same-height-switch.test.js +664 -0
  276. package/test/seek-frees-the-viewers-requests.test.js +217 -0
  277. package/test/seek-landing.test.js +13 -13
  278. package/test/seek-target-not-superseded.test.js +28 -29
  279. package/test/segment-closed-on-both-branches.test.js +96 -0
  280. package/test/segment-cut-times.test.js +1 -1
  281. package/test/segment-hold-supersede.test.js +28 -35
  282. package/test/segment-judged-by-its-own-header.test.js +221 -0
  283. package/test/segment-serve-wiring.test.js +130 -39
  284. package/test/segment-store-eviction.test.js +22 -7
  285. package/test/segment-store.test.js +98 -14
  286. package/test/segment-true-start.test.js +1 -1
  287. package/test/segments-are-shared.test.js +17 -16
  288. package/test/send-chunk-bytes.test.js +1 -1
  289. package/test/serving-existing-output.test.js +102 -0
  290. package/test/serving-output.test.js +102 -0
  291. package/test/shared-piece-store.test.js +1 -1
  292. package/test/source-file.test.js +1 -1
  293. package/test/stale-request-after-seek.test.js +17 -21
  294. package/test/startup-readings.test.js +10 -5
  295. package/test/subtitle-cue-framing.test.js +5 -5
  296. package/test/subtitle-cue-source.test.js +36 -38
  297. package/test/subtitle-cue-walk.test.js +11 -10
  298. package/test/subtitle-defaults.test.js +1 -1
  299. package/test/subtitle-language.test.js +4 -4
  300. package/test/subtitle-track-numbering.test.js +5 -4
  301. package/test/supply-margin.test.js +1 -1
  302. package/test/swarm-follows-readers.test.js +3 -3
  303. package/test/swarm-reach.test.js +1 -1
  304. package/test/swarm-selection.test.js +3 -3
  305. package/test/tail-duplication.test.js +1 -1
  306. package/test/the-channel-has-both-ends.test.js +91 -0
  307. package/test/the-torrent-thread-serves-bytes.test.js +107 -0
  308. package/test/thread-pool.test.js +3 -3
  309. package/test/timeline.test.js +1 -12
  310. package/test/torrent-cost.test.js +1 -1
  311. package/test/tracks-begin-together.test.js +38 -24
  312. package/test/tunnel-renewal.test.js +1 -1
  313. package/test/two-viewers-one-picture.test.js +120 -87
  314. package/test/upload-hurry.test.js +3 -3
  315. package/test/usrsctp-state.test.js +1 -1
  316. package/test/video-facts.test.js +1 -1
  317. package/test/viewer-assignments.test.js +143 -0
  318. package/test/viewer-outputs.test.js +60 -52
  319. package/test/viewer-presence.test.js +22 -25
  320. package/test/viewer-requests-admission.test.js +89 -0
  321. package/test/viewer-states.test.js +189 -172
  322. package/test/viewer-subtitle-subscription.test.js +1 -1
  323. package/test/viewer.test.js +139 -136
  324. package/test/viewing-generations.test.js +390 -0
  325. package/test/visible-rung.test.js +37 -0
  326. package/test/wait-ledger.test.js +1 -1
  327. package/test/webrtc-shim.test.js +3 -3
  328. package/test/wedge-certainty.test.js +2 -2
  329. package/test/withdraw-piece-claim.test.js +2 -2
  330. package/test/worker-channel.test.js +1 -31
  331. package/utils/client-log-file.js +151 -151
  332. package/scripts/read-container-index.mjs +0 -108
  333. package/services/container/ContainerFactory.js +0 -122
  334. package/services/controllers/PlaybackController.js +0 -33
  335. package/services/disk/DiskSpace.js +0 -150
  336. package/services/disk/wire.js +0 -61
  337. package/services/hls-session-manager.js +0 -9689
  338. package/services/orchestrators/SubtitleOrchestrator.js +0 -160
  339. package/services/output/Output.js +0 -94
  340. package/services/output/OutputSpec.js +0 -202
  341. package/services/output/index.js +0 -1
  342. package/services/output/ladder.js +0 -26
  343. package/services/produced-index.js +0 -222
  344. package/services/quality/EncodeCost.js +0 -555
  345. package/services/torrent-worker/container-tracks.js +0 -414
  346. package/test/produced-index.test.js +0 -142
  347. /package/{services → bin}/thread-pool.js +0 -0
  348. /package/services/{available-share.js → encode/available-share.js} +0 -0
  349. /package/services/{decode-cost-fit.js → encode/decode-cost-fit.js} +0 -0
  350. /package/services/{encoder-readings.js → encode/encoder-readings.js} +0 -0
  351. /package/services/{host-load.js → encode/host-load.js} +0 -0
  352. /package/services/{output → encode/output}/cut-grid.js +0 -0
  353. /package/services/{output → encode/output}/rates.js +0 -0
  354. /package/services/{segment-formats → encode/segment-formats}/mpegts.js +0 -0
  355. /package/services/{torrent-cost.js → encode/torrent-cost.js} +0 -0
  356. /package/services/{container → media/container}/ebml-reader.js +0 -0
  357. /package/services/{ffmpeg-banner.js → media/ffmpeg-banner.js} +0 -0
  358. /package/services/{tracks → media/tracks}/ContainerTrack.js +0 -0
  359. /package/services/{tracks → media/tracks}/ImageSubtitleTrack.js +0 -0
  360. /package/services/{tracks → media/tracks}/SubtitleTrack.js +0 -0
  361. /package/services/{tracks → media/tracks}/TextSubtitleTrack.js +0 -0
  362. /package/services/{tracks → media/tracks}/VideoTrack.js +0 -0
  363. /package/services/{tracks → media/tracks}/index.js +0 -0
  364. /package/services/{files → storage/files}/piece-from-whole-file.js +0 -0
  365. /package/services/{disk → storage}/keep.js +0 -0
  366. /package/services/{piece-store → storage/piece-store}/piece-lru.js +0 -0
  367. /package/services/{disk → storage}/returns.js +0 -0
  368. /package/services/{demand → torrent/demand}/DemandRegister.js +0 -0
  369. /package/services/{demand → torrent/demand}/Urgency.js +0 -0
  370. /package/services/{demand → torrent/demand}/Window.js +0 -0
  371. /package/services/{demand → torrent/demand}/index.js +0 -0
  372. /package/services/{demand → torrent/demand}/pieces.js +0 -0
  373. /package/services/{download → torrent/download}/withdraw-claim.js +0 -0
  374. /package/services/{supply-margin.js → torrent/supply-margin.js} +0 -0
  375. /package/services/{torrent-source-key.js → torrent/torrent-source-key.js} +0 -0
  376. /package/services/{torrent-worker → torrent/worker}/fastest-wires.js +0 -0
  377. /package/services/{torrent-worker → torrent/worker}/handle-state.js +0 -0
  378. /package/services/{nat-classifier.js → transport/nat-classifier.js} +0 -0
  379. /package/services/{registry-api.js → transport/registry-api.js} +0 -0
  380. /package/services/{tunnel-client.js → transport/tunnel-client.js} +0 -0
  381. /package/services/{priority → viewer}/WaitLedger.js +0 -0
@@ -0,0 +1,361 @@
1
+ /**
2
+ * @file Which encoding modes this machine may use, and what each costs at
3
+ * every size it may be asked for — measured at startup, before any viewer
4
+ * (roadmap item 97, step 14).
5
+ *
6
+ * A MODE is one encoder at one of the settings the proxy can actually select:
7
+ * every libx264 preset the offer chooses from, and the one setting a hardware
8
+ * encoder is driven at. A mode is usable only if it has been shown here to do
9
+ * two things on THIS machine:
10
+ *
11
+ * 1. produce a correct stream through the product's own arguments — the same
12
+ * `buildVideoArgs` a real output is encoded with, into fMP4 segments, each
13
+ * of which must then decode with no error. A device being present, or one
14
+ * of its settings passing, qualifies nothing else;
15
+ * 2. encode at a known speed at the size it is asked for.
16
+ *
17
+ * WHY SEVERAL SIZES. The speed used to be read at 640x360 alone and scaled to
18
+ * every other size by pixel count. Measured 2026-08-22, cost per pixel is not
19
+ * constant: 1280x720 came back at 0.445x the predicted throughput on
20
+ * `ultrafast` and 0.725x on `fast` (`research/preset-benchmark-size-scaling-
21
+ * 2026-08-22.md`). So each mode is read at a set of sizes that spans the
22
+ * ladder, and a size between two readings is priced by interpolating between
23
+ * them — never beyond the smallest or the largest one read. A frame outside
24
+ * what was read is a mode this machine has not shown it can do, and is not
25
+ * offered.
26
+ *
27
+ * HOW FAR AN INTERPOLATION MAY BE TRUSTED is measured too, not assumed: where
28
+ * three consecutive sizes were read, the middle one is predicted from the two
29
+ * outside it and compared with its own reading. The largest such error for the
30
+ * mode is its error bound, and every interpolated figure is lowered by it. A
31
+ * mode with fewer than three sizes read has no bound, and is then used only at
32
+ * the sizes it was read at.
33
+ *
34
+ * WHAT IS NOT READ, so the startup stays short. Sizes are read from the
35
+ * smallest up and, at each size, modes from the fastest down. A mode that
36
+ * cannot keep up at a size ends that size: every slower mode is slower still.
37
+ * A size at which even the fastest mode cannot keep up ends the walk: every
38
+ * larger size is larger still. What is left unread is unavailable, which is
39
+ * the answer it would have got anyway.
40
+ */
41
+
42
+ import { mkdtempSync, readdirSync, rmSync, statSync } from "node:fs";
43
+ import os from "node:os";
44
+ import path from "node:path";
45
+ import { softwareRateControlFor, TRANSCODE_FPS } from "./args.js";
46
+ import {
47
+ CALIBRATION_DIR,
48
+ CALIBRATION_REFERENCE_CLIP,
49
+ decodeToRawFrames,
50
+ measureEncodeSlope,
51
+ runFfmpeg,
52
+ verifySegmentsDecodeCleanly
53
+ } from "./hwaccel.js";
54
+ import { interpolationErrorOf } from "./throughput.js";
55
+
56
+ export { throughputAt, interpolationErrorOf } from "./throughput.js";
57
+
58
+ /**
59
+ * The sizes a mode is read at, smallest first.
60
+ *
61
+ * The smallest is below every frame the ladder produces — its lowest row is
62
+ * 426x240, and a 4:3 or narrower source gives less area than that at the same
63
+ * height. The largest is the 2160 row. The two between are the 720 and 1080
64
+ * rows, where most films are watched. 640x360 is the reference size every
65
+ * earlier reading was taken at, so the figures stay comparable with them.
66
+ */
67
+ export const CALIBRATION_FRAMES = Object.freeze([
68
+ Object.freeze({ width: 256, height: 144 }),
69
+ Object.freeze({ width: 640, height: 360 }),
70
+ Object.freeze({ width: 1280, height: 720 }),
71
+ Object.freeze({ width: 1920, height: 1080 }),
72
+ Object.freeze({ width: 3840, height: 2160 })
73
+ ]);
74
+
75
+ /** The size the correctness check encodes at, and every earlier reading was taken at. */
76
+ const CHECK_FRAME = Object.freeze({ width: 640, height: 360 });
77
+
78
+ /**
79
+ * @typedef {object} SizeReading
80
+ * @property {number} width
81
+ * @property {number} height
82
+ * @property {number} pixelsPerSec
83
+ *
84
+ * @typedef {object} CalibratedMode
85
+ * @property {string} preset - The setting, or the encoder's name where it has one setting.
86
+ * @property {number} pixelsPerSec - At 640x360, for everything that has always read that figure.
87
+ * @property {SizeReading[]} bySize - Smallest first.
88
+ * @property {number | null} interpolationError - The largest measured relative
89
+ * error of an interpolation between two readings, or null where it could not
90
+ * be measured.
91
+ * @property {{ averageKbps: number, peakKbps: number } | null} segmentKbps -
92
+ * What the correctness check's segments carried.
93
+ */
94
+
95
+ /**
96
+ * The settings a mode is known by, as text, for the log and for the key its
97
+ * later observations are filed under.
98
+ *
99
+ * @param {{ benchmarkArgs: (rung: string | null) => string[] }} encoder
100
+ * @param {string | null} rung
101
+ * @returns {string}
102
+ */
103
+ export function settingsOf(encoder, rung) {
104
+ return encoder.benchmarkArgs(rung).join(" ");
105
+ }
106
+
107
+ /**
108
+ * Encode the calibration clip through the product's own arguments for this
109
+ * mode, and check every segment decodes on its own.
110
+ *
111
+ * @param {{ ffmpegBin: string, encoder: object, rung: string | null, segmentDurationSec: number }} params
112
+ * @returns {Promise<{ ok: boolean, reason: string, segmentKbps: { averageKbps: number, peakKbps: number } | null }>}
113
+ */
114
+ export async function checkModeProducesCorrectSegments({ ffmpegBin, encoder, rung, segmentDurationSec }) {
115
+ let directory;
116
+ try {
117
+ directory = mkdtempSync(path.join(os.tmpdir(), "tt-modecheck-"));
118
+ } catch (error) {
119
+ return { ok: false, reason: `no writable temp directory (${error instanceof Error ? error.message : String(error)})`, segmentKbps: null };
120
+ }
121
+ // Three segments' worth, so at least two are cut and each is checked alone.
122
+ const seconds = segmentDurationSec * 3;
123
+ const rateControl = encoder.kind === "software"
124
+ ? softwareRateControlFor({ ...CHECK_FRAME, fps: TRANSCODE_FPS })
125
+ : null;
126
+ const args = [
127
+ "-hide_banner", "-loglevel", "error",
128
+ ...encoder.inputArgs,
129
+ "-stream_loop", "-1",
130
+ "-i", path.join(CALIBRATION_DIR, CALIBRATION_REFERENCE_CLIP),
131
+ "-t", String(seconds),
132
+ "-map", "0:v:0", "-an",
133
+ ...encoder.buildVideoArgs({
134
+ targetWidth: CHECK_FRAME.width,
135
+ targetHeight: CHECK_FRAME.height,
136
+ segmentDurationSec,
137
+ preset: rung ?? undefined,
138
+ fps: TRANSCODE_FPS,
139
+ tonemap: false,
140
+ forcedKeyframeTimes: null,
141
+ rateControl
142
+ }),
143
+ "-f", "hls",
144
+ "-hls_time", String(segmentDurationSec),
145
+ "-hls_list_size", "0",
146
+ "-hls_flags", "independent_segments",
147
+ "-hls_segment_type", "fmp4",
148
+ "-hls_fmp4_init_filename", "init.mp4",
149
+ "-hls_segment_filename", path.join(directory, "seg-%03d.m4s"),
150
+ path.join(directory, "index.m3u8")
151
+ ];
152
+ try {
153
+ const encoded = await runFfmpeg(ffmpegBin, args, 60_000);
154
+ if (encoded.code !== 0) {
155
+ const line = encoded.stderr.trim().split(/\r?\n/).pop() ?? "";
156
+ return { ok: false, reason: `the encode failed (exit ${encoded.code}${line ? `: ${line}` : ""})`, segmentKbps: null };
157
+ }
158
+ if (!(await verifySegmentsDecodeCleanly(ffmpegBin, directory))) {
159
+ return { ok: false, reason: "a segment it produced does not decode on its own", segmentKbps: null };
160
+ }
161
+ const sizes = readdirSync(directory)
162
+ .filter((name) => /^seg-\d+\.m4s$/.test(name))
163
+ .sort()
164
+ .map((name) => statSync(path.join(directory, name)).size);
165
+ // The last segment may be short; only whole ones say what a segment carries.
166
+ const whole = sizes.slice(0, Math.max(1, sizes.length - 1));
167
+ const kbps = whole.map((bytes) => (bytes * 8) / segmentDurationSec / 1000);
168
+ return {
169
+ ok: true,
170
+ reason: "",
171
+ segmentKbps: {
172
+ averageKbps: kbps.reduce((sum, value) => sum + value, 0) / kbps.length,
173
+ peakKbps: Math.max(...kbps)
174
+ }
175
+ };
176
+ } finally {
177
+ try {
178
+ rmSync(directory, { recursive: true, force: true, maxRetries: 5, retryDelay: 200 });
179
+ } catch {
180
+ // A temp directory the operating system will clear.
181
+ }
182
+ }
183
+ }
184
+
185
+ /**
186
+ * Qualify and measure every selectable mode of one encoder.
187
+ *
188
+ * Always resolves. A mode that fails either half is left out, with the reason
189
+ * logged; an encoder none of whose modes qualify comes back with no modes,
190
+ * and nothing on this host is then re-encoded with it.
191
+ *
192
+ * @param {object} params
193
+ * @param {string} params.ffmpegBin
194
+ * @param {object} params.encoder - An `Encoder`.
195
+ * @param {{ info: (m: string) => void, warn: (m: string) => void }} [params.logger]
196
+ * @param {number} [params.segmentDurationSec]
197
+ * @param {Array<{ width: number, height: number }>} [params.frames]
198
+ * @param {object} [params.measure] - Replaces the three readings, for a check
199
+ * that must not run ffmpeg: `check`, `rawFrames`, `speed`.
200
+ * @returns {Promise<{ modes: CalibratedMode[], refused: Array<{ preset: string, reason: string }> }>}
201
+ */
202
+ export async function calibrateEncoder({
203
+ ffmpegBin,
204
+ encoder,
205
+ logger,
206
+ segmentDurationSec = 4,
207
+ frames = CALIBRATION_FRAMES,
208
+ measure = null
209
+ }) {
210
+ const log = logger ?? { info: () => {}, warn: () => {} };
211
+ const check = measure?.check ?? checkModeProducesCorrectSegments;
212
+ const rawFrames = measure?.rawFrames ?? ((frame) => decodeToRawFrames(ffmpegBin, log, frame));
213
+ const speed = measure?.speed ?? ((rung, rawPath, frame) => measureEncodeSlope(ffmpegBin, encoder, rung, rawPath, frame));
214
+ const release = measure?.release ?? ((rawPath) => {
215
+ try {
216
+ rmSync(path.dirname(rawPath), { recursive: true, force: true, maxRetries: 5, retryDelay: 200 });
217
+ } catch {
218
+ // A temp directory the operating system will clear.
219
+ }
220
+ });
221
+ const rungs = Array.isArray(encoder.selectableRungs) && encoder.selectableRungs.length > 0
222
+ ? encoder.selectableRungs
223
+ : [null];
224
+ const startedAt = Date.now();
225
+
226
+ /** @type {Map<string | null, { ok: boolean, reason: string, segmentKbps: object | null }>} */
227
+ const checked = new Map();
228
+ for (const rung of rungs) {
229
+ const answer = await check({ ffmpegBin, encoder, rung, segmentDurationSec });
230
+ checked.set(rung, answer);
231
+ if (!answer.ok) {
232
+ log.warn(`calibration: ${encoder.name} "${rung ?? "as it comes"}" is not used on this host — ${answer.reason}`);
233
+ }
234
+ }
235
+ const qualified = rungs.filter((rung) => checked.get(rung)?.ok === true);
236
+
237
+ /** @type {Map<string | null, SizeReading[]>} */
238
+ const readings = new Map(qualified.map((rung) => [rung, []]));
239
+ const sortedFrames = [...frames].sort((left, right) => left.width * left.height - right.width * right.height);
240
+ for (const frame of sortedFrames) {
241
+ if (qualified.length === 0) {
242
+ break;
243
+ }
244
+ const rawPath = await rawFrames(frame);
245
+ if (rawPath === null) {
246
+ log.warn(`calibration: no frames at ${frame.width}x${frame.height}; ${encoder.name} is not read there or above`);
247
+ break;
248
+ }
249
+ let fastestKeptUp = false;
250
+ try {
251
+ // Fastest first: the ladder is slowest first, so walk it backwards.
252
+ for (let index = qualified.length - 1; index >= 0; index -= 1) {
253
+ const rung = qualified[index];
254
+ const realtimes = await speed(rung, rawPath, frame);
255
+ if (realtimes === null) {
256
+ log.warn(`calibration: ${encoder.name} "${rung ?? "as it comes"}" gave no reading at ${frame.width}x${frame.height}`);
257
+ break;
258
+ }
259
+ readings.get(rung).push({
260
+ width: frame.width,
261
+ height: frame.height,
262
+ pixelsPerSec: frame.width * frame.height * TRANSCODE_FPS * realtimes
263
+ });
264
+ if (index === qualified.length - 1) {
265
+ fastestKeptUp = realtimes >= 1;
266
+ }
267
+ if (realtimes < 1) {
268
+ // Every slower mode is slower still at this size.
269
+ break;
270
+ }
271
+ }
272
+ } finally {
273
+ release(rawPath);
274
+ }
275
+ if (!fastestKeptUp) {
276
+ // Every larger size is larger still.
277
+ break;
278
+ }
279
+ }
280
+
281
+ /** @type {CalibratedMode[]} */
282
+ const modes = [];
283
+ /** @type {Array<{ preset: string, reason: string }>} */
284
+ const refused = [];
285
+ for (const rung of rungs) {
286
+ const preset = rung ?? encoder.name;
287
+ if (checked.get(rung)?.ok !== true) {
288
+ refused.push({ preset, reason: checked.get(rung)?.reason ?? "not checked" });
289
+ continue;
290
+ }
291
+ const bySize = readings.get(rung) ?? [];
292
+ if (bySize.length === 0) {
293
+ refused.push({ preset, reason: "no size was read" });
294
+ continue;
295
+ }
296
+ const reference = bySize.find((reading) => reading.width === CHECK_FRAME.width && reading.height === CHECK_FRAME.height) ?? bySize[0];
297
+ const interpolationError = interpolationErrorOf(bySize);
298
+ modes.push({
299
+ preset,
300
+ pixelsPerSec: reference.pixelsPerSec,
301
+ bySize,
302
+ interpolationError,
303
+ segmentKbps: checked.get(rung).segmentKbps
304
+ });
305
+ log.info(
306
+ `calibration: ${encoder.name} "${preset}" ` +
307
+ bySize.map((reading) => `${reading.width}x${reading.height}=${(reading.pixelsPerSec / 1e6).toFixed(1)}Mpx/s`).join(" ") +
308
+ ` interpolation error ${interpolationError === null ? "not measured, used only at the sizes read" : `${(interpolationError * 100).toFixed(1)}%`}` +
309
+ (checked.get(rung).segmentKbps
310
+ ? ` segments ${Math.round(checked.get(rung).segmentKbps.averageKbps)}kbps average, ${Math.round(checked.get(rung).segmentKbps.peakKbps)}kbps peak at ${CHECK_FRAME.width}x${CHECK_FRAME.height}`
311
+ : "")
312
+ );
313
+ }
314
+ log.info(
315
+ `calibration: ${encoder.name} ${modes.length} of ${rungs.length} mode(s) usable ` +
316
+ `(${((Date.now() - startedAt) / 1000).toFixed(1)}s)` +
317
+ (refused.length > 0 ? `; not usable: ${refused.map((one) => `${one.preset} (${one.reason})`).join(", ")}` : "")
318
+ );
319
+ return { modes, refused };
320
+ }
321
+
322
+ /**
323
+ * What every encoder this proxy may use was measured to do, by kind: the one
324
+ * detection chose, and software, which is where a failing hardware encoder
325
+ * falls back to for the rest of the process.
326
+ */
327
+ export class HostCalibration {
328
+ /** @type {Map<string, CalibratedMode[]>} */
329
+ #byKind;
330
+
331
+ /**
332
+ * @param {object} params
333
+ * @param {Record<string, CalibratedMode[]>} params.byKind
334
+ * @param {import("./fingerprint.js").HostFingerprint | null} [params.fingerprint]
335
+ */
336
+ constructor({ byKind, fingerprint = null }) {
337
+ this.#byKind = new Map(Object.entries(byKind ?? {}).map(([kind, modes]) => [kind, Array.isArray(modes) ? modes : []]));
338
+ this.fingerprint = fingerprint;
339
+ }
340
+
341
+ /**
342
+ * The usable modes of one encoder kind, slowest first — the shape the offer
343
+ * and the preset choice have always read. Null where that kind was not
344
+ * calibrated at all.
345
+ *
346
+ * @param {string | null | undefined} kind
347
+ * @returns {CalibratedMode[] | null}
348
+ */
349
+ modesFor(kind) {
350
+ return kind && this.#byKind.has(kind) ? this.#byKind.get(kind) : null;
351
+ }
352
+
353
+ /**
354
+ * Every kind that was calibrated.
355
+ *
356
+ * @returns {string[]}
357
+ */
358
+ kinds() {
359
+ return [...this.#byKind.keys()];
360
+ }
361
+ }
@@ -1,128 +1,128 @@
1
- /**
2
- * @file What a second job costs on this machine — measured, because it is not
3
- * the sum of two prices.
4
- *
5
- * Measured on the addon host 2026-08-18, decoding the same clip:
6
- *
7
- * | what else was running | decode speed | cost |
8
- * |---|---|---|
9
- * | nothing (cold) | 2.10-2.25x | 0.46 s/s |
10
- * | nothing (hot, 68 °C) | 2.30-2.33x | 0.43 s/s |
11
- * | one encoder | 0.79-0.90x | 1.18 s/s |
12
- * | two encoders | 0.56-0.64x | 1.67 s/s |
13
- *
14
- * The same work costs **2.6× more** with one encoder beside it and 3.7× with
15
- * two. Heat is not the cause — the hot idle machine was the fastest of all.
16
- * Four cores share one narrow path to memory, and decoding and encoding both
17
- * saturate it.
18
- *
19
- * That contradicts the shape of the budget, not just its constants. Everything
20
- * in the quality offer adds seconds of work per second of content — this
21
- * encode, plus that decode, plus what is already committed — and these readings
22
- * say two jobs that each fit alone do not fit together. The availability
23
- * correction in `available-share.js` does not cover it either: that subtracts
24
- * the share of the machine taken by work nobody has been charged for, while
25
- * this is our own work colliding with itself.
26
- *
27
- * So the penalty is measured rather than derived, at startup, on the host it
28
- * describes — and beyond the range that was measured it does not extrapolate:
29
- * it holds the largest reading and says so, because nothing here knows what a
30
- * fourth job would do.
31
- */
32
-
33
- /**
34
- * Penalties measured on this host, keyed by how many OTHER jobs were running.
35
- *
36
- * @typedef {Map<number, number>} ContentionPenalties
37
- */
38
-
39
- /**
40
- * The multiplier for a job's cost when `othersRunning` other jobs share the
41
- * machine.
42
- *
43
- * @param {number} othersRunning
44
- * @param {ContentionPenalties | null} measured
45
- * @returns {{ penalty: number, measured: boolean, from: number }}
46
- * `from` is the reading it came from — equal to `othersRunning` when one was
47
- * measured for exactly that many, and the largest available otherwise.
48
- */
49
- export function contentionPenalty(othersRunning, measured) {
50
- const others = Number.isFinite(othersRunning) ? Math.max(0, Math.round(othersRunning)) : 0;
51
- if (others === 0) {
52
- // Alone on the machine is what every benchmark measures, so there is
53
- // nothing to correct.
54
- return { penalty: 1, measured: true, from: 0 };
55
- }
56
- if (!(measured instanceof Map) || measured.size === 0) {
57
- // Nothing measured: the honest multiplier is 1, and the caller's own log
58
- // says the prediction is uncorrected. Inventing a penalty would be the
59
- // same mistake as inventing a fill rate.
60
- return { penalty: 1, measured: false, from: 0 };
61
- }
62
- const exact = measured.get(others);
63
- if (Number.isFinite(exact) && exact > 0) {
64
- return { penalty: exact, measured: true, from: others };
65
- }
66
- // Beyond what was measured, hold the largest reading rather than continue the
67
- // curve. Two readings say nothing about the shape past them, and a budget
68
- // that extrapolates a memory bottleneck will be wrong in whichever direction
69
- // it guesses.
70
- let largestKey = 0;
71
- let largestValue = 1;
72
- for (const [key, value] of measured) {
73
- if (key <= others && key > largestKey && Number.isFinite(value) && value > 0) {
74
- largestKey = key;
75
- largestValue = value;
76
- }
77
- }
78
- if (largestKey === 0) {
79
- return { penalty: 1, measured: false, from: 0 };
80
- }
81
- return { penalty: largestValue, measured: true, from: largestKey };
82
- }
83
-
84
- /**
85
- * A cost, corrected for what else will be running.
86
- *
87
- * @param {number} costSecondsPerSecond - As the benchmarks priced it, alone.
88
- * @param {number} othersRunning
89
- * @param {ContentionPenalties | null} measured
90
- * @returns {number}
91
- */
92
- export function costWithContention(costSecondsPerSecond, othersRunning, measured) {
93
- if (!Number.isFinite(costSecondsPerSecond) || costSecondsPerSecond <= 0) {
94
- return costSecondsPerSecond;
95
- }
96
- return costSecondsPerSecond * contentionPenalty(othersRunning, measured).penalty;
97
- }
98
-
99
- /**
100
- * Turn measured decode speeds into penalties.
101
- *
102
- * @param {number} aloneSpeed - Decode speed with the machine to itself.
103
- * @param {Array<{ others: number, speed: number }>} beside - One reading per
104
- * number of other jobs running.
105
- * @returns {ContentionPenalties | null} Null when the alone reading is missing,
106
- * since every penalty is relative to it.
107
- */
108
- export function penaltiesFrom(aloneSpeed, beside) {
109
- if (!Number.isFinite(aloneSpeed) || aloneSpeed <= 0) {
110
- return null;
111
- }
112
- const penalties = new Map();
113
- for (const reading of Array.isArray(beside) ? beside : []) {
114
- const others = Number.isFinite(reading?.others) ? Math.round(reading.others) : null;
115
- const speed = Number(reading?.speed);
116
- if (others === null || others <= 0 || !Number.isFinite(speed) || speed <= 0) {
117
- continue;
118
- }
119
- // Cost is the reciprocal of speed, so the penalty is the reciprocal ratio.
120
- // A job that runs at half the speed costs twice as much.
121
- const penalty = aloneSpeed / speed;
122
- // Below 1 would mean the machine got FASTER for being busier. It happens in
123
- // the noise on a fast host, and it is not a discovery — it is a reading
124
- // that says there is no penalty to measure here.
125
- penalties.set(others, penalty > 1 ? penalty : 1);
126
- }
127
- return penalties.size > 0 ? penalties : null;
128
- }
1
+ /**
2
+ * @file What a second job costs on this machine — measured, because it is not
3
+ * the sum of two prices.
4
+ *
5
+ * Measured on the addon host 2026-08-18, decoding the same clip:
6
+ *
7
+ * | what else was running | decode speed | cost |
8
+ * |---|---|---|
9
+ * | nothing (cold) | 2.10-2.25x | 0.46 s/s |
10
+ * | nothing (hot, 68 °C) | 2.30-2.33x | 0.43 s/s |
11
+ * | one encoder | 0.79-0.90x | 1.18 s/s |
12
+ * | two encoders | 0.56-0.64x | 1.67 s/s |
13
+ *
14
+ * The same work costs **2.6× more** with one encoder beside it and 3.7× with
15
+ * two. Heat is not the cause — the hot idle machine was the fastest of all.
16
+ * Four cores share one narrow path to memory, and decoding and encoding both
17
+ * saturate it.
18
+ *
19
+ * That contradicts the shape of the budget, not just its constants. Everything
20
+ * in the quality offer adds seconds of work per second of content — this
21
+ * encode, plus that decode, plus what is already committed — and these readings
22
+ * say two jobs that each fit alone do not fit together. The availability
23
+ * correction in `encode/available-share.js` does not cover it either: that subtracts
24
+ * the share of the machine taken by work nobody has been charged for, while
25
+ * this is our own work colliding with itself.
26
+ *
27
+ * So the penalty is measured rather than derived, at startup, on the host it
28
+ * describes — and beyond the range that was measured it does not extrapolate:
29
+ * it holds the largest reading and says so, because nothing here knows what a
30
+ * fourth job would do.
31
+ */
32
+
33
+ /**
34
+ * Penalties measured on this host, keyed by how many OTHER jobs were running.
35
+ *
36
+ * @typedef {Map<number, number>} ContentionPenalties
37
+ */
38
+
39
+ /**
40
+ * The multiplier for a job's cost when `othersRunning` other jobs share the
41
+ * machine.
42
+ *
43
+ * @param {number} othersRunning
44
+ * @param {ContentionPenalties | null} measured
45
+ * @returns {{ penalty: number, measured: boolean, from: number }}
46
+ * `from` is the reading it came from — equal to `othersRunning` when one was
47
+ * measured for exactly that many, and the largest available otherwise.
48
+ */
49
+ export function contentionPenalty(othersRunning, measured) {
50
+ const others = Number.isFinite(othersRunning) ? Math.max(0, Math.round(othersRunning)) : 0;
51
+ if (others === 0) {
52
+ // Alone on the machine is what every benchmark measures, so there is
53
+ // nothing to correct.
54
+ return { penalty: 1, measured: true, from: 0 };
55
+ }
56
+ if (!(measured instanceof Map) || measured.size === 0) {
57
+ // Nothing measured: the honest multiplier is 1, and the caller's own log
58
+ // says the prediction is uncorrected. Inventing a penalty would be the
59
+ // same mistake as inventing a fill rate.
60
+ return { penalty: 1, measured: false, from: 0 };
61
+ }
62
+ const exact = measured.get(others);
63
+ if (Number.isFinite(exact) && exact > 0) {
64
+ return { penalty: exact, measured: true, from: others };
65
+ }
66
+ // Beyond what was measured, hold the largest reading rather than continue the
67
+ // curve. Two readings say nothing about the shape past them, and a budget
68
+ // that extrapolates a memory bottleneck will be wrong in whichever direction
69
+ // it guesses.
70
+ let largestKey = 0;
71
+ let largestValue = 1;
72
+ for (const [key, value] of measured) {
73
+ if (key <= others && key > largestKey && Number.isFinite(value) && value > 0) {
74
+ largestKey = key;
75
+ largestValue = value;
76
+ }
77
+ }
78
+ if (largestKey === 0) {
79
+ return { penalty: 1, measured: false, from: 0 };
80
+ }
81
+ return { penalty: largestValue, measured: true, from: largestKey };
82
+ }
83
+
84
+ /**
85
+ * A cost, corrected for what else will be running.
86
+ *
87
+ * @param {number} costSecondsPerSecond - As the benchmarks priced it, alone.
88
+ * @param {number} othersRunning
89
+ * @param {ContentionPenalties | null} measured
90
+ * @returns {number}
91
+ */
92
+ export function costWithContention(costSecondsPerSecond, othersRunning, measured) {
93
+ if (!Number.isFinite(costSecondsPerSecond) || costSecondsPerSecond <= 0) {
94
+ return costSecondsPerSecond;
95
+ }
96
+ return costSecondsPerSecond * contentionPenalty(othersRunning, measured).penalty;
97
+ }
98
+
99
+ /**
100
+ * Turn measured decode speeds into penalties.
101
+ *
102
+ * @param {number} aloneSpeed - Decode speed with the machine to itself.
103
+ * @param {Array<{ others: number, speed: number }>} beside - One reading per
104
+ * number of other jobs running.
105
+ * @returns {ContentionPenalties | null} Null when the alone reading is missing,
106
+ * since every penalty is relative to it.
107
+ */
108
+ export function penaltiesFrom(aloneSpeed, beside) {
109
+ if (!Number.isFinite(aloneSpeed) || aloneSpeed <= 0) {
110
+ return null;
111
+ }
112
+ const penalties = new Map();
113
+ for (const reading of Array.isArray(beside) ? beside : []) {
114
+ const others = Number.isFinite(reading?.others) ? Math.round(reading.others) : null;
115
+ const speed = Number(reading?.speed);
116
+ if (others === null || others <= 0 || !Number.isFinite(speed) || speed <= 0) {
117
+ continue;
118
+ }
119
+ // Cost is the reciprocal of speed, so the penalty is the reciprocal ratio.
120
+ // A job that runs at half the speed costs twice as much.
121
+ const penalty = aloneSpeed / speed;
122
+ // Below 1 would mean the machine got FASTER for being busier. It happens in
123
+ // the noise on a fast host, and it is not a discovery — it is a reading
124
+ // that says there is no penalty to measure here.
125
+ penalties.set(others, penalty > 1 ? penalty : 1);
126
+ }
127
+ return penalties.size > 0 ? penalties : null;
128
+ }