@torrent-tv/proxy 2.86.0 → 2.87.1

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 (385) hide show
  1. package/CHANGELOG.md +2142 -1915
  2. package/CLAUDE.md +322 -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/browser-proxy-contract.md +94 -0
  7. package/docs/container-architecture.md +218 -213
  8. package/docs/download-architecture.md +7 -7
  9. package/docs/encode-architecture.md +287 -11
  10. package/docs/encode-run-state.md +2 -2
  11. package/docs/logs.md +97 -97
  12. package/docs/{disk-architecture.md → storage-architecture.md} +107 -1
  13. package/openspec/changes/adaptive-bitrate/design.md +2 -2
  14. package/openspec/changes/adaptive-bitrate/proposal.md +1 -1
  15. package/openspec/changes/adaptive-bitrate/tasks.md +1 -1
  16. package/openspec/changes/chunked-request-bodies/design.md +1 -1
  17. package/openspec/changes/chunked-request-bodies/proposal.md +1 -1
  18. package/openspec/changes/chunked-request-bodies/tasks.md +1 -1
  19. package/openspec/changes/cold-start/design.md +1 -1
  20. package/openspec/changes/disk-cap/proposal.md +1 -1
  21. package/openspec/changes/proxy-observability/proposal.md +2 -2
  22. package/openspec/changes/track-selection/proposal.md +1 -1
  23. package/openspec/changes/transcode-quality/proposal.md +1 -1
  24. package/package.json +2 -2
  25. package/research/delivery-wedge-stand-ladder-2026-09-13.md +329 -329
  26. package/research/double-spawn-2026-09-10.md +171 -171
  27. package/routes/api/playback-plan/post.js +2 -2
  28. package/routes/api/sources/files/get.js +1 -1
  29. package/routes/api/sources/stats/get.js +1 -1
  30. package/routes/api/sources/warm/post.js +10 -3
  31. package/routes/api/subtitles/get.js +230 -230
  32. package/routes/api/transcode-sessions/fragment-far/post.js +3 -3
  33. package/routes/api/transcode-sessions/net-report/post.js +129 -94
  34. package/routes/api/transcode-sessions/post.js +160 -139
  35. package/routes/api/transcode-sessions/progress/get.js +3 -3
  36. package/routes/api/transcode-sessions/release/post.js +3 -3
  37. package/routes/api/transcode-sessions/seek/post.js +12 -3
  38. package/routes/stream/get.js +2 -2
  39. package/routes/transcode/audio-file/get.js +15 -6
  40. package/routes/transcode/audio-warm/get.js +9 -6
  41. package/routes/transcode/session-file/get.js +52 -268
  42. package/routes/transcode/variant-file/get.js +46 -7
  43. package/routes/transcode/variant-warm/get.js +19 -6
  44. package/scripts/render-run-graph.js +1 -1
  45. package/server.js +720 -465
  46. package/services/encode/CoverageMap.js +428 -428
  47. package/services/encode/CushionReport.js +345 -0
  48. package/services/encode/EncodeAdmission.js +332 -0
  49. package/services/{orchestrators → encode}/EncodeOrchestrator.js +307 -29
  50. package/services/encode/EncodeRun.js +16 -9
  51. package/services/encode/EncodeRuns.js +1229 -0
  52. package/services/encode/Encoder.js +18 -2
  53. package/services/encode/EncoderSelection.js +38 -0
  54. package/services/encode/LocalObservations.js +359 -0
  55. package/services/encode/NvencEncoder.js +5 -0
  56. package/services/encode/OutputOpening.js +755 -0
  57. package/services/encode/OutputTimes.js +579 -0
  58. package/services/encode/Renditions.js +2162 -0
  59. package/services/encode/RunProgress.js +84 -0
  60. package/services/encode/SoftwareEncoder.js +12 -7
  61. package/services/encode/args.js +402 -186
  62. package/services/encode/calibration.js +361 -0
  63. package/services/encode/contention.js +128 -128
  64. package/services/encode/encode-run-state.js +16 -20
  65. package/services/encode/fingerprint.js +322 -0
  66. package/services/{hwaccel.js → encode/hwaccel.js} +82 -42
  67. package/services/{learned-median.js → encode/learned-median.js} +11 -0
  68. package/services/encode/output/EncodedOutput.js +57 -0
  69. package/services/encode/output/Output.js +131 -0
  70. package/services/{output/LiveOutputs.js → encode/output/OutputCatalog.js} +141 -40
  71. package/services/encode/output/OutputSpec.js +390 -0
  72. package/services/{output → encode/output}/Timeline.js +12 -37
  73. package/services/encode/output/index.js +20 -0
  74. package/services/encode/output/ladder.js +32 -0
  75. package/services/{output → encode/output}/playlists.js +3 -0
  76. package/services/encode/output-key-format.js +21 -0
  77. package/services/encode/quality/EncodeCost.js +1083 -0
  78. package/services/encode/quality/HostLoad.js +525 -0
  79. package/services/encode/quality/HostTimings.js +249 -0
  80. package/services/encode/quality/OutputQualityState.js +28 -0
  81. package/services/encode/quality/QualityController.js +935 -0
  82. package/services/encode/quality/QualityOffer.js +416 -0
  83. package/services/encode/quality/drain-threat.js +47 -0
  84. package/services/encode/quality/link-budget.js +254 -0
  85. package/services/encode/quality/output-format.js +266 -0
  86. package/services/encode/quality/serving-output.js +216 -0
  87. package/services/encode/quality/visible-rung.js +57 -0
  88. package/services/encode/run-command.js +23 -20
  89. package/services/encode/run-inputs.js +42 -0
  90. package/services/{segment-formats → encode/segment-formats}/fmp4.js +63 -0
  91. package/services/{segment-formats → encode/segment-formats}/index.js +3 -3
  92. package/services/encode/segment-formats/init-compat.js +324 -0
  93. package/services/{segment-formats → encode/segment-formats}/mp4-boxes.js +8 -3
  94. package/services/encode/throughput.js +97 -0
  95. package/services/{orchestrators → media}/ContainerOrchestrator.js +45 -7
  96. package/services/media/KeyframeTables.js +316 -0
  97. package/services/{source → media}/SourceFile.js +7 -55
  98. package/services/{torrent-worker/subtitle-cues.js → media/SubtitleCues.js} +103 -132
  99. package/services/media/SubtitleOrchestrator.js +167 -0
  100. package/services/{audio-inventory.js → media/audio-inventory.js} +7 -1
  101. package/services/{container → media/container}/AviContainer.js +266 -266
  102. package/services/{container → media/container}/Container.js +596 -601
  103. package/services/media/container/ContainerFactory.js +56 -0
  104. package/services/media/container/KeyframeTable.js +272 -0
  105. package/services/{container → media/container}/MatroskaContainer.js +1485 -1485
  106. package/services/{container → media/container}/Mp4Container.js +1244 -1244
  107. package/services/{container → media/container}/SubtitleFileContainer.js +322 -322
  108. package/services/media/keyframe-probe.js +157 -0
  109. package/services/media/media-info-probe.js +93 -0
  110. package/services/{playback-planner.js → media/playback-planner.js} +82 -39
  111. package/services/{tracks → media/tracks}/AudioTrack.js +131 -131
  112. package/services/{tracks → media/tracks}/language-detect.js +174 -174
  113. package/services/server/OutputLifecycle.js +418 -0
  114. package/services/server/SegmentServing.js +1146 -0
  115. package/services/server/ViewerRequests.js +522 -0
  116. package/services/server/controllers/PlaybackController.js +36 -0
  117. package/services/{controllers → server/controllers}/SubtitleController.js +17 -33
  118. package/services/server/http-responses.js +60 -0
  119. package/services/server/transcode-session-files.js +325 -0
  120. package/services/server/wire-outputs.js +983 -0
  121. package/services/storage/Diagnostics.js +202 -0
  122. package/services/storage/MachineBudget.js +235 -0
  123. package/services/{piece-store → storage}/allowance.js +107 -107
  124. package/services/{core-dumps.js → storage/core-dumps.js} +1 -1
  125. package/services/{files → storage/files}/CompletedFiles.js +116 -1
  126. package/services/{disk → storage}/free.js +33 -0
  127. package/services/storage/machine-memory.js +56 -0
  128. package/services/{memory-report.js → storage/memory-report.js} +575 -596
  129. package/services/{piece-store → storage/piece-store}/piece-disk-store.js +0 -7
  130. package/services/{piece-store → storage/piece-store}/shared-piece-store.js +28 -21
  131. package/services/{encode → storage/segment-store}/SegmentStore.js +395 -8
  132. package/services/storage/wire.js +175 -0
  133. package/services/{download → torrent/download}/SwarmSelection.js +2 -2
  134. package/services/{download → torrent/download}/registry.js +2 -2
  135. package/services/torrent/files.js +1 -1
  136. package/services/torrent/piece-store-of.js +29 -0
  137. package/services/{torrent-pool.js → torrent/torrent-pool.js} +26 -119
  138. package/services/{torrent-worker → torrent/worker}/background-fill.js +2 -2
  139. package/services/{torrent-worker → torrent/worker}/channel.js +271 -250
  140. package/services/{torrent-worker → torrent/worker}/client.js +77 -58
  141. package/services/torrent/worker/held-bytes.js +155 -0
  142. package/services/{torrent-worker → torrent/worker}/install-webrtc-shim.js +1 -1
  143. package/services/{torrent-worker → torrent/worker}/piece-reader.js +21 -30
  144. package/services/{torrent-worker → torrent/worker}/pool-adapter.js +101 -137
  145. package/services/{torrent-worker → torrent/worker}/protocol.js +26 -30
  146. package/services/torrent/worker/resume-warm.js +107 -0
  147. package/services/{torrent-worker → torrent/worker}/webrtc-shim.js +2 -2
  148. package/services/{torrent-worker → torrent/worker}/worker.js +68 -159
  149. package/services/{data-channel-handler.js → transport/data-channel-handler.js} +1356 -1356
  150. package/services/{delivery-probe.js → transport/delivery-probe.js} +842 -842
  151. package/services/{health-collector.js → transport/health-collector.js} +6 -35
  152. package/services/{packet-witness.js → transport/packet-witness.js} +808 -784
  153. package/services/{port-mapper.js → transport/port-mapper.js} +1 -1
  154. package/services/{usrsctp-state.js → transport/usrsctp-state.js} +1 -1
  155. package/services/{webrtc-manager.js → transport/webrtc-manager.js} +1 -1
  156. package/services/viewer/Assignments.js +340 -0
  157. package/services/{priority → viewer}/PriorityMap.js +390 -390
  158. package/services/{priority → viewer}/PriorityOrchestrator.js +325 -320
  159. package/services/viewer/Viewer.js +657 -483
  160. package/services/viewer/Viewers.js +368 -257
  161. package/services/viewer/active-output.js +37 -54
  162. package/services/viewer/audio-start.js +4 -3
  163. package/services/viewer/buffer-trend.js +81 -0
  164. package/services/viewer/choices.js +647 -0
  165. package/services/viewer/positions.js +126 -120
  166. package/services/viewer/report-intake.js +46 -45
  167. package/test/asset-paths.test.js +28 -0
  168. package/test/audio-inventory.test.js +3 -3
  169. package/test/audio-track-bitrate.test.js +40 -0
  170. package/test/auto-quality-step.test.js +707 -515
  171. package/test/available-share.test.js +1 -1
  172. package/test/background-fill.test.js +1 -1
  173. package/test/client-log-file.test.js +110 -110
  174. package/test/completed-files.test.js +1 -1
  175. package/test/concurrent-cost.test.js +13 -3
  176. package/test/container-media-info.test.js +3 -3
  177. package/test/core-dumps.test.js +1 -1
  178. package/test/coverage-follows-the-disk.test.js +46 -9
  179. package/test/cue-push-carries-what-the-walk-found.test.js +58 -0
  180. package/test/cut-times-timeline.test.js +2 -2
  181. package/test/cuts-follow-published-grid.test.js +9 -8
  182. package/test/data-channel-frame.test.js +1 -1
  183. package/test/dead-torrent-handle.test.js +1 -1
  184. package/test/declared-rates.test.js +2 -2
  185. package/test/declared-tracks.test.js +2 -2
  186. package/test/decode-cost-fit.test.js +1 -1
  187. package/test/decode-cost.test.js +34 -25
  188. package/test/delivery-probe.test.js +1 -1
  189. package/test/demand-register.test.js +4 -4
  190. package/test/dht-bootstrap.test.js +1 -1
  191. package/test/drain-threat.test.js +65 -0
  192. package/test/duplicate-add.test.js +1 -1
  193. package/test/encode-admission.test.js +146 -0
  194. package/test/encode-cost.test.js +199 -10
  195. package/test/encode-orchestrator.test.js +39 -1
  196. package/test/encode-plan-viewers.test.js +2 -2
  197. package/test/encode-run.test.js +2 -2
  198. package/test/encode-slope.test.js +1 -1
  199. package/test/encoded-output.test.js +39 -0
  200. package/test/encoder-readings.test.js +1 -1
  201. package/test/every-claimant-is-told-its-share.test.js +152 -0
  202. package/test/failed-encoder-still-serves-what-exists.test.js +354 -0
  203. package/test/failed-start-breaker.test.js +81 -30
  204. package/test/fastest-wires.test.js +1 -1
  205. package/test/ffmpeg-stream-counts.test.js +1 -1
  206. package/test/file-edges.test.js +3 -3
  207. package/test/fmp4-self-contained.test.js +1 -1
  208. package/test/fmp4-timestamps.test.js +1 -1
  209. package/test/fmp4-track-completeness.test.js +1 -1
  210. package/test/format-keyed-outputs.test.js +78 -0
  211. package/test/health-metrics.test.js +3 -2
  212. package/test/held-request-width.test.js +22 -23
  213. package/test/helpers/encode-run.js +136 -136
  214. package/test/helpers/held-file.js +29 -0
  215. package/test/helpers/manager.js +57 -0
  216. package/test/helpers/output-spec.js +63 -0
  217. package/test/host-load.test.js +1 -1
  218. package/test/index-accuracy-is-about-the-file.test.js +128 -0
  219. package/test/init-compat.test.js +219 -0
  220. package/test/input-lost-quiets-the-plan.test.js +1 -1
  221. package/test/input-unavailable.test.js +1 -1
  222. package/test/keeping-period.test.js +4 -4
  223. package/test/keyframe-index-accuracy.test.js +97 -45
  224. package/test/keyframe-table-budget.test.js +28 -33
  225. package/test/keyframes-belong-to-the-file.test.js +174 -57
  226. package/test/learned-median.test.js +1 -1
  227. package/test/letting-go-asks-who-is-watching.test.js +191 -0
  228. package/test/limit-row-by-area.test.js +60 -0
  229. package/test/link-budget.test.js +111 -0
  230. package/test/link-probe.test.js +56 -56
  231. package/test/local-encoding-data.test.js +273 -0
  232. package/test/look-ahead-coverage.test.js +1 -1
  233. package/test/manager-surface.test.js +294 -0
  234. package/test/matroska-blocks.test.js +0 -0
  235. package/test/matroska-cues-track.test.js +1 -1
  236. package/test/memory-budget.test.js +2 -2
  237. package/test/mp4-composition-times.test.js +0 -0
  238. package/test/mp4-subtitles.test.js +1 -1
  239. package/test/one-authority.test.js +45 -41
  240. package/test/one-budget.test.js +149 -0
  241. package/test/one-output-one-session.test.js +204 -0
  242. package/test/{disk-space.test.js → one-resource-divided.test.js} +30 -10
  243. package/test/one-serving-output-per-height.test.js +458 -0
  244. package/test/orchestrator-wired.test.js +46 -45
  245. package/test/output-catalog.test.js +39 -0
  246. package/test/output-shape.test.js +4 -51
  247. package/test/output-spec.test.js +251 -157
  248. package/test/output-speed.test.js +17 -15
  249. package/test/outputs-being-prepared.test.js +40 -0
  250. package/test/packet-witness-ring.test.js +2 -2
  251. package/test/packet-witness.test.js +1 -1
  252. package/test/piece-disk-store.test.js +2 -2
  253. package/test/piece-from-whole-file.test.js +1 -1
  254. package/test/piece-lru.test.js +1 -1
  255. package/test/piece-reader.test.js +2 -2
  256. package/test/piece-store-eviction.test.js +1 -1
  257. package/test/piece-store-never-refuses.test.js +1 -1
  258. package/test/piece-store-reservations.test.js +1 -1
  259. package/test/piece-store-slow-disk.test.js +1 -1
  260. package/test/piece-tail.test.js +1 -1
  261. package/test/plan-host-timings.test.js +1 -1
  262. package/test/plan-is-steady.test.js +225 -225
  263. package/test/priority-map-download.test.js +4 -4
  264. package/test/priority-map-emptied.test.js +204 -205
  265. package/test/priority-map-per-output.test.js +215 -209
  266. package/test/priority-map.test.js +1 -1
  267. package/test/probe-wedge-certainty.test.js +1 -1
  268. package/test/produced-copy-choice.test.js +15 -60
  269. package/test/quality-offer.test.js +162 -0
  270. package/test/quality-variants.test.js +1229 -1167
  271. package/test/rate-control-in-the-output.test.js +180 -0
  272. package/test/read-survives-withdrawal.test.js +2 -2
  273. package/test/read-window.test.js +10 -3
  274. package/test/resume-warm.test.js +1 -1
  275. package/test/run-inputs.test.js +80 -0
  276. package/test/run-position-follows-published-grid.test.js +13 -11
  277. package/test/run-progress.test.js +65 -0
  278. package/test/same-height-switch.test.js +664 -0
  279. package/test/seek-frees-the-viewers-requests.test.js +217 -0
  280. package/test/seek-landing.test.js +13 -13
  281. package/test/seek-target-not-superseded.test.js +28 -29
  282. package/test/segment-closed-on-both-branches.test.js +96 -0
  283. package/test/segment-cut-times.test.js +1 -1
  284. package/test/segment-hold-supersede.test.js +28 -35
  285. package/test/segment-judged-by-its-own-header.test.js +221 -0
  286. package/test/segment-serve-wiring.test.js +130 -39
  287. package/test/segment-store-eviction.test.js +22 -7
  288. package/test/segment-store.test.js +98 -14
  289. package/test/segment-true-start.test.js +1 -1
  290. package/test/segments-are-shared.test.js +17 -16
  291. package/test/send-chunk-bytes.test.js +1 -1
  292. package/test/serving-existing-output.test.js +102 -0
  293. package/test/serving-output.test.js +102 -0
  294. package/test/shared-piece-store.test.js +1 -1
  295. package/test/source-file.test.js +1 -1
  296. package/test/stale-request-after-seek.test.js +17 -21
  297. package/test/startup-readings.test.js +10 -5
  298. package/test/subtitle-cue-framing.test.js +5 -5
  299. package/test/subtitle-cue-source.test.js +36 -38
  300. package/test/subtitle-cue-walk.test.js +11 -10
  301. package/test/subtitle-defaults.test.js +1 -1
  302. package/test/subtitle-language.test.js +4 -4
  303. package/test/subtitle-track-numbering.test.js +5 -4
  304. package/test/supply-margin.test.js +1 -1
  305. package/test/swarm-follows-readers.test.js +3 -3
  306. package/test/swarm-reach.test.js +1 -1
  307. package/test/swarm-selection.test.js +3 -3
  308. package/test/tail-duplication.test.js +1 -1
  309. package/test/the-channel-has-both-ends.test.js +91 -0
  310. package/test/the-torrent-thread-serves-bytes.test.js +107 -0
  311. package/test/thread-pool.test.js +3 -3
  312. package/test/timeline.test.js +1 -12
  313. package/test/torrent-cost.test.js +1 -1
  314. package/test/tracks-begin-together.test.js +38 -24
  315. package/test/tunnel-renewal.test.js +1 -1
  316. package/test/two-viewers-one-picture.test.js +120 -87
  317. package/test/upload-hurry.test.js +3 -3
  318. package/test/usrsctp-state.test.js +1 -1
  319. package/test/video-facts.test.js +1 -1
  320. package/test/viewer-assignments.test.js +143 -0
  321. package/test/viewer-outputs.test.js +60 -52
  322. package/test/viewer-presence.test.js +22 -25
  323. package/test/viewer-requests-admission.test.js +89 -0
  324. package/test/viewer-states.test.js +189 -202
  325. package/test/viewer-subtitle-subscription.test.js +1 -1
  326. package/test/viewer.test.js +139 -136
  327. package/test/viewing-generations.test.js +390 -0
  328. package/test/visible-rung.test.js +37 -0
  329. package/test/wait-ledger.test.js +1 -1
  330. package/test/webrtc-shim.test.js +3 -3
  331. package/test/wedge-certainty.test.js +2 -2
  332. package/test/withdraw-piece-claim.test.js +2 -2
  333. package/test/worker-channel.test.js +1 -31
  334. package/utils/client-log-file.js +151 -151
  335. package/scripts/read-container-index.mjs +0 -108
  336. package/services/container/ContainerFactory.js +0 -122
  337. package/services/controllers/PlaybackController.js +0 -33
  338. package/services/disk/DiskSpace.js +0 -150
  339. package/services/disk/wire.js +0 -61
  340. package/services/hls-session-manager.js +0 -9608
  341. package/services/orchestrators/SubtitleOrchestrator.js +0 -160
  342. package/services/output/Output.js +0 -94
  343. package/services/output/OutputSpec.js +0 -202
  344. package/services/output/index.js +0 -1
  345. package/services/output/ladder.js +0 -26
  346. package/services/produced-index.js +0 -222
  347. package/services/quality/EncodeCost.js +0 -555
  348. package/services/torrent-worker/container-tracks.js +0 -414
  349. package/services/viewer/link-readings.js +0 -56
  350. package/test/produced-index.test.js +0 -142
  351. /package/{services → bin}/thread-pool.js +0 -0
  352. /package/services/{available-share.js → encode/available-share.js} +0 -0
  353. /package/services/{decode-cost-fit.js → encode/decode-cost-fit.js} +0 -0
  354. /package/services/{encoder-readings.js → encode/encoder-readings.js} +0 -0
  355. /package/services/{host-load.js → encode/host-load.js} +0 -0
  356. /package/services/{output → encode/output}/cut-grid.js +0 -0
  357. /package/services/{output → encode/output}/rates.js +0 -0
  358. /package/services/{segment-formats → encode/segment-formats}/mpegts.js +0 -0
  359. /package/services/{torrent-cost.js → encode/torrent-cost.js} +0 -0
  360. /package/services/{container → media/container}/ebml-reader.js +0 -0
  361. /package/services/{ffmpeg-banner.js → media/ffmpeg-banner.js} +0 -0
  362. /package/services/{tracks → media/tracks}/ContainerTrack.js +0 -0
  363. /package/services/{tracks → media/tracks}/ImageSubtitleTrack.js +0 -0
  364. /package/services/{tracks → media/tracks}/SubtitleTrack.js +0 -0
  365. /package/services/{tracks → media/tracks}/TextSubtitleTrack.js +0 -0
  366. /package/services/{tracks → media/tracks}/VideoTrack.js +0 -0
  367. /package/services/{tracks → media/tracks}/index.js +0 -0
  368. /package/services/{files → storage/files}/piece-from-whole-file.js +0 -0
  369. /package/services/{disk → storage}/keep.js +0 -0
  370. /package/services/{piece-store → storage/piece-store}/piece-lru.js +0 -0
  371. /package/services/{disk → storage}/returns.js +0 -0
  372. /package/services/{demand → torrent/demand}/DemandRegister.js +0 -0
  373. /package/services/{demand → torrent/demand}/Urgency.js +0 -0
  374. /package/services/{demand → torrent/demand}/Window.js +0 -0
  375. /package/services/{demand → torrent/demand}/index.js +0 -0
  376. /package/services/{demand → torrent/demand}/pieces.js +0 -0
  377. /package/services/{download → torrent/download}/withdraw-claim.js +0 -0
  378. /package/services/{supply-margin.js → torrent/supply-margin.js} +0 -0
  379. /package/services/{torrent-source-key.js → torrent/torrent-source-key.js} +0 -0
  380. /package/services/{torrent-worker → torrent/worker}/fastest-wires.js +0 -0
  381. /package/services/{torrent-worker → torrent/worker}/handle-state.js +0 -0
  382. /package/services/{nat-classifier.js → transport/nat-classifier.js} +0 -0
  383. /package/services/{registry-api.js → transport/registry-api.js} +0 -0
  384. /package/services/{tunnel-client.js → transport/tunnel-client.js} +0 -0
  385. /package/services/{priority → viewer}/WaitLedger.js +0 -0
@@ -14,7 +14,7 @@ import { EventEmitter } from "node:events";
14
14
  import { EncodeRun } from "../services/encode/EncodeRun.js";
15
15
  import { ENCODE_EXIT } from "../services/encode/encode-exit.js";
16
16
  import { SoftwareEncoder } from "../services/encode/SoftwareEncoder.js";
17
- import { EncodeOrchestrator } from "../services/orchestrators/EncodeOrchestrator.js";
17
+ import { EncodeOrchestrator } from "../services/encode/EncodeOrchestrator.js";
18
18
  import { penaltiesFrom } from "../services/encode/contention.js";
19
19
 
20
20
  // WHAT A SECOND ENCODER COSTS THE FIRST — measured, never a formula.
@@ -155,6 +155,44 @@ function orchestrator({ maxRuns = 2 } = {}) {
155
155
  return { made, lines, processes, buildRun };
156
156
  }
157
157
 
158
+ test("how many encoders are running is answered by the one thing that makes them", () => {
159
+ // ONE FACT, ONE OWNER. The session manager answered this twice by walking its
160
+ // own registry — once per SESSION and once per RUN — and the two agreed only
161
+ // while a session held at most one run. It has held several since the plan was
162
+ // allowed to place them, and what a second encoder costs the first was
163
+ // measured between ffmpeg PROCESSES, so the per-session count was the wrong
164
+ // one of the two exactly where they differed.
165
+ const { made, buildRun, processes } = orchestrator();
166
+ assert.equal(made.runningCount(), 0, "nothing is running before anything is made");
167
+
168
+ const first = buildRun({ from: 0, to: 99 });
169
+ const second = buildRun({ from: 100, to: 199 });
170
+ made.adopt(PICTURE, first);
171
+ made.adopt(PICTURE, second);
172
+ assert.equal(made.runningCount(), 2, "two processes on one output are two encoders, not one");
173
+
174
+ // And a run that has ended is forgotten as it ends, not at the next pass.
175
+ processes.get(first).emit("exit", 0, null);
176
+ assert.equal(made.runningCount(), 1);
177
+
178
+ // A SUSPENDED run is stopped where it stands and is using nothing, so a cost
179
+ // measured beside it is a cost measured alone.
180
+ //
181
+ // Stated as a run-shaped value rather than by suspending a real one: `pause`
182
+ // sends SIGSTOP by pid, which this platform does not have — so a check that
183
+ // went through it would pass or fail by the machine it ran on, and this
184
+ // project would rather have no check than one of those.
185
+ made.adopt(PICTURE, { id: "parked", from: 300, to: 399, head: 300, speedX: 1, isAlive: true, isSuspended: true, stop() {} });
186
+ assert.equal(made.runningCount(), 1, "a parked encoder is not one that is running");
187
+
188
+ // Nor is one on its way out. A run told to stop is still here — its exit
189
+ // arrives a turn or two later — and counting it makes whoever is deciding
190
+ // whether to start an encoder decide not to, on the strength of a process
191
+ // that is leaving.
192
+ made.adopt(PICTURE, { id: "leaving", from: 400, to: 499, head: 400, speedX: 1, isAlive: false, isSuspended: false, stop() {} });
193
+ assert.equal(made.runningCount(), 1);
194
+ });
195
+
158
196
  test("a viewer waiting gets an encoder at what they are waiting for", () => {
159
197
  const { made } = orchestrator();
160
198
  wants(made, [{ from: 100, to: 130 }]);
@@ -29,8 +29,8 @@ import assert from "node:assert/strict";
29
29
  import { EventEmitter } from "node:events";
30
30
  import { EncodeRun } from "../services/encode/EncodeRun.js";
31
31
  import { SoftwareEncoder } from "../services/encode/SoftwareEncoder.js";
32
- import { EncodeOrchestrator } from "../services/orchestrators/EncodeOrchestrator.js";
33
- import { mapForViewer, mergeMaps, runsOf } from "../services/priority/PriorityMap.js";
32
+ import { EncodeOrchestrator } from "../services/encode/EncodeOrchestrator.js";
33
+ import { mapForViewer, mergeMaps, runsOf } from "../services/viewer/PriorityMap.js";
34
34
  import { penaltiesFrom } from "../services/encode/contention.js";
35
35
 
36
36
  // WHAT A SECOND ENCODER COSTS THE FIRST — measured, never a formula.
@@ -41,7 +41,7 @@ function makeRun(span = {}) {
41
41
  const ends = [];
42
42
  const process_ = new FakeProcess();
43
43
  const run = new EncodeRun({
44
- address: "torrent:abc:fmt=fmp4:grid=kf@0:video-only:v=0/enc:854x480:auto",
44
+ address: "torrent:abc:fmt=fmp4:grid=kf@0:video-only:v=0/enc/libx264/854x480@24/veryfast/none/vbv=-",
45
45
  encoder: new SoftwareEncoder(),
46
46
  from: span.from ?? 10,
47
47
  to: span.to ?? 14,
@@ -135,7 +135,7 @@ test("an ending reports the stretch, how far it got and how long it lived", () =
135
135
  assert.equal(ended.to, 14);
136
136
  assert.equal(ended.reached, 10);
137
137
  assert.equal(ended.livedMs, 0, "the clock is injected, so this is exact");
138
- assert.equal(ended.address.includes("enc:854x480"), true);
138
+ assert.equal(ended.address.includes("854x480"), true);
139
139
  });
140
140
 
141
141
  test("a process that could not be started ends like any other failure", () => {
@@ -10,7 +10,7 @@
10
10
  import assert from "node:assert/strict";
11
11
  import test from "node:test";
12
12
 
13
- import { slopeOf } from "../services/hwaccel.js";
13
+ import { slopeOf } from "../services/encode/hwaccel.js";
14
14
 
15
15
  const at = (wallSec, outSec) => ({ wallSec, outSec });
16
16
 
@@ -0,0 +1,39 @@
1
+ /**
2
+ * @file An output holds what is born with it, and not what is produced.
3
+ *
4
+ * Decided with the user 2026-09-16: which pieces are made is the segment
5
+ * store's, and which encoders run is the encoding orchestrator's. A second
6
+ * holder of either is how the stale second owner stopped playback outright.
7
+ */
8
+
9
+ import test from "node:test";
10
+ import assert from "node:assert/strict";
11
+ import { EncodedOutput } from "../services/encode/output/EncodedOutput.js";
12
+ import { outputSpec } from "./helpers/output-spec.js";
13
+
14
+ function made() {
15
+ return new EncodedOutput({
16
+ id: "aaaaaaaabbbbcccc",
17
+ spec: outputSpec({ transcodeVideo: true, width: 1280, height: 720 }),
18
+ file: { key: "source-1:0" },
19
+ keyframes: null,
20
+ timeline: null,
21
+ segmentFormat: null,
22
+ output: null,
23
+ useSyntheticPlaylist: true,
24
+ playlistText: "#EXTM3U\n",
25
+ variantHeight: 720
26
+ });
27
+ }
28
+
29
+ test("its address is its format, read from the spec and kept nowhere else", () => {
30
+ const output = made();
31
+ assert.equal(output.outputKey, output.spec.toKey());
32
+ });
33
+
34
+ test("it keeps no statement of what has been produced or what is running", () => {
35
+ const output = made();
36
+ for (const field of ["runs", "runState", "progress", "produced", "ready", "state", "lastError", "failedStartCount"]) {
37
+ assert.equal(field in output, false, `${field} belongs to the store or the encoding orchestrator`);
38
+ }
39
+ });
@@ -12,7 +12,7 @@
12
12
  import assert from "node:assert/strict";
13
13
  import test from "node:test";
14
14
 
15
- import { speedFromReadings } from "../services/encoder-readings.js";
15
+ import { speedFromReadings } from "../services/encode/encoder-readings.js";
16
16
 
17
17
  const at = (seconds, processedSeconds) => ({ takenAt: seconds * 1000, processedSeconds });
18
18
 
@@ -0,0 +1,152 @@
1
+ /**
2
+ * @file Everything that holds bytes is told how many it may hold.
3
+ *
4
+ * Measured 2026-09-14, by reading every file outside the storage layer that
5
+ * touches the filesystem: three claimants had no share at all, and one held a
6
+ * ceiling over bytes it did not own.
7
+ *
8
+ * - whole files: NO BOUND OF ANY KIND. A 2.8 GB film kept whole on a host whose
9
+ * disk is often a 32 GB card;
10
+ * - diagnostics: bounded by a COUNT and never by a size. Two core dumps and
11
+ * five heap snapshots came to 3.2 GB on the addon host, and one wedge was
12
+ * captured thirteen times in six hours;
13
+ * - the torrent pool's "disk cap" of 10 GB: a sum over WebTorrent's downloaded
14
+ * bitfield, so a piece held purely in MEMORY told against a ceiling called
15
+ * disk, while the bytes it meant to bound belong to the spill and to the
16
+ * whole files, which have owners of their own. It owned nothing and is gone.
17
+ */
18
+
19
+ import test from "node:test";
20
+ import assert from "node:assert/strict";
21
+ import fs from "node:fs/promises";
22
+ import os from "node:os";
23
+ import path from "node:path";
24
+ import { CompletedFiles } from "../services/storage/files/CompletedFiles.js";
25
+ import { Diagnostics } from "../services/storage/Diagnostics.js";
26
+
27
+ const KILOBYTE = 1024;
28
+
29
+ /**
30
+ * @param {number} length
31
+ * @returns {() => AsyncGenerator<Buffer>}
32
+ */
33
+ function bytesOf(length) {
34
+ return async function* source() {
35
+ yield Buffer.alloc(length, 7);
36
+ };
37
+ }
38
+
39
+ /**
40
+ * @returns {Promise<{ files: CompletedFiles, root: string }>}
41
+ */
42
+ async function filesInATempRoot() {
43
+ const root = await fs.mkdtemp(path.join(os.tmpdir(), "whole-files-"));
44
+ return { files: new CompletedFiles({ root }), root };
45
+ }
46
+
47
+ test("a whole file is kept only while there is room for it", async () => {
48
+ const { files, root } = await filesInATempRoot();
49
+ try {
50
+ files.allow(4 * KILOBYTE);
51
+ const kept = await files.keep({
52
+ infoHash: "a".repeat(40), fileIndex: 0, length: 3 * KILOBYTE, name: "one.mkv", open: bytesOf(3 * KILOBYTE)
53
+ });
54
+ assert.ok(kept, "it fits, so it is kept");
55
+
56
+ const refused = await files.keep({
57
+ infoHash: "b".repeat(40), fileIndex: 0, length: 3 * KILOBYTE, name: "two.mkv", open: bytesOf(3 * KILOBYTE)
58
+ });
59
+ assert.equal(refused, null, "it does not fit, so it is not assembled at all");
60
+ assert.equal(files.size, 1, "and nothing already held was taken to make room during a write");
61
+ } finally {
62
+ await fs.rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 20 });
63
+ }
64
+ });
65
+
66
+ test("over its share, the whole file nobody has read for longest goes", async () => {
67
+ const { files, root } = await filesInATempRoot();
68
+ try {
69
+ files.allow(Number.MAX_SAFE_INTEGER);
70
+ for (const [index, hash] of [["0", "a"], ["1", "b"], ["2", "c"]]) {
71
+ await files.keep({
72
+ infoHash: hash.repeat(40), fileIndex: Number(index), length: KILOBYTE, name: `${index}.mkv`, open: bytesOf(KILOBYTE)
73
+ });
74
+ }
75
+ // The first two are asked for; the third is not, so it is the one nobody
76
+ // wants. Losing it is not losing data — the torrent can fetch it again, and
77
+ // until it does the read falls back to the pieces.
78
+ files.find("a".repeat(40), 0);
79
+ files.find("b".repeat(40), 1);
80
+
81
+ const after = await files.allow(2 * KILOBYTE);
82
+
83
+ assert.equal(after.removed, 1);
84
+ assert.ok(files.find("a".repeat(40), 0), "a file just read was taken");
85
+ assert.ok(files.find("b".repeat(40), 1), "a file just read was taken");
86
+ assert.equal(files.find("c".repeat(40), 2), null, "the longest-unread should have gone");
87
+ } finally {
88
+ await fs.rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 20 });
89
+ }
90
+ });
91
+
92
+ test("told nothing, whole files keep what they have — a share is not assumed", async () => {
93
+ const { files, root } = await filesInATempRoot();
94
+ try {
95
+ const kept = await files.keep({
96
+ infoHash: "a".repeat(40), fileIndex: 0, length: KILOBYTE, name: "one.mkv", open: bytesOf(KILOBYTE)
97
+ });
98
+ assert.ok(kept, "before the owner has divided anything there is nothing to refuse against");
99
+ } finally {
100
+ await fs.rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 20 });
101
+ }
102
+ });
103
+
104
+ test("over its share the evidence stops being collected, and nothing recorded is deleted", async () => {
105
+ const root = await fs.mkdtemp(path.join(os.tmpdir(), "diagnostics-"));
106
+ try {
107
+ await fs.writeFile(path.join(root, "core.MainThread.1.2"), Buffer.alloc(4 * KILOBYTE));
108
+ await fs.writeFile(path.join(root, "heap-x-1-2.heapsnapshot"), Buffer.alloc(2 * KILOBYTE));
109
+ // Not evidence: the proxy's own state lives in the same directory.
110
+ await fs.writeFile(path.join(root, "host-timings.json"), Buffer.alloc(KILOBYTE));
111
+
112
+ const said = [];
113
+ const diagnostics = new Diagnostics({
114
+ logger: { info: () => {}, warn: (line) => said.push(line) },
115
+ kinds: [
116
+ { name: "core dumps", directory: () => root, matches: (name) => name.startsWith("core.") },
117
+ { name: "heap snapshots", directory: () => root, matches: (name) => name.endsWith(".heapsnapshot") }
118
+ ]
119
+ });
120
+
121
+ assert.equal(await diagnostics.measure(), 6 * KILOBYTE, "the proxy's own state is not evidence");
122
+ assert.equal(
123
+ diagnostics.wanted(),
124
+ 10 * KILOBYTE,
125
+ "what it holds plus one more of the largest kind seen — measured, not a fraction"
126
+ );
127
+
128
+ diagnostics.allow(7 * KILOBYTE);
129
+ assert.equal(diagnostics.mayKeep({ what: "a heap snapshot", bytes: KILOBYTE }), true, "there is room for a small one");
130
+ assert.equal(diagnostics.mayKeep({ what: "a heap snapshot", bytes: 4 * KILOBYTE }), false, "there is not for a large one");
131
+
132
+ assert.equal(said.length, 1, "a refusal is a line, never a silence");
133
+ assert.match(said[0], /was NOT kept/);
134
+ const left = (await fs.readdir(root)).sort();
135
+ assert.deepEqual(
136
+ left,
137
+ ["core.MainThread.1.2", "heap-x-1-2.heapsnapshot", "host-timings.json"],
138
+ "nothing already recorded may be removed to make room for more"
139
+ );
140
+ } finally {
141
+ await fs.rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 20 });
142
+ }
143
+ });
144
+
145
+ test("before the owner has divided anything, the evidence is not refused", async () => {
146
+ const diagnostics = new Diagnostics({ kinds: [], logger: { info: () => {}, warn: () => {} } });
147
+ assert.equal(
148
+ diagnostics.mayKeep({ what: "a core dump", bytes: Number.MAX_SAFE_INTEGER }),
149
+ true,
150
+ "null is what a claimant that has not been told looks like, and it does not license growth by itself"
151
+ );
152
+ });
@@ -0,0 +1,354 @@
1
+ /**
2
+ * @file A failed encoder explains what is MISSING, and takes nothing away.
3
+ *
4
+ * Whether production failed used to be asked at the door, before the request
5
+ * had even been looked at, so an output whose encoding ended in an error
6
+ * answered 500 to every request — including requests for segments it had
7
+ * already finished and which were lying on disk under their served names. The
8
+ * viewer lost what was made as well as what was not, and a seek back into the
9
+ * finished stretch could not be served either.
10
+ *
11
+ * A piece under its served name is whole by construction, whoever wrote it and
12
+ * whatever has become of the encoder since. The failure belongs where a file is
13
+ * absent: there it is the truthful answer, and holding the request instead only
14
+ * spends the viewer's patience.
15
+ */
16
+
17
+ import test from "node:test";
18
+ import assert from "node:assert/strict";
19
+ import { rm, writeFile } from "node:fs/promises";
20
+ import path from "node:path";
21
+ import { wireOutputs } from "../services/server/wire-outputs.js";
22
+ import { SourceFile } from "../services/media/SourceFile.js";
23
+ import { Timeline } from "../services/encode/output/Timeline.js";
24
+ import { Output } from "../services/encode/output/Output.js";
25
+ import { KeyframeTable } from "../services/media/container/KeyframeTable.js";
26
+ import { ENCODE_RUN_STATE } from "../services/encode/encode-run-state.js";
27
+ import { fmp4Format } from "../services/encode/segment-formats/fmp4.js";
28
+ import { EncodeRun } from "../services/encode/EncodeRun.js";
29
+ import { fakeProcess, silentLogger } from "./helpers/encode-run.js";
30
+ import { waitForSessionFile } from "../routes/transcode/session-file/get.js";
31
+ import { outputSpec } from "./helpers/output-spec.js";
32
+
33
+ const SESSION_ID = "3333333344445555";
34
+ const OUTPUT_KEY = "failed-serves:fmt=fmp4:grid=uniform:video-only:v=0/copy";
35
+ // The other branch: a picture cut at the source's own keyframes, where the
36
+ // muxer is handed the cut times and writes each piece self-contained. It has no
37
+ // init FILE at all — the header is lifted out of the first piece — which is the
38
+ // case `#initFromFirstSegment` answers `null` for instead of throwing `ENOENT`.
39
+ const KEYFRAME_OUTPUT_KEY = "failed-serves-kf:fmt=fmp4:grid=kf@0:video-only:v=0/copy";
40
+
41
+ /**
42
+ * One output with segment #0 finished on disk and its encoder dead.
43
+ *
44
+ * @returns {Promise<{ manager: object, dirPath: string, outputKey: string }>}
45
+ */
46
+ async function outputWhoseEncoderDied({ failed = true, segments = [0], cutsAtGivenTimes = false } = {}) {
47
+ const manager = wireOutputs({
48
+ enabled: true,
49
+ ffmpegBin: "ffmpeg",
50
+ localBindHost: "127.0.0.1",
51
+ localPort: 9090
52
+ });
53
+ manager.encodeRuns.planEncodersNow = () => {};
54
+ manager.encodeRuns.planEncodersSoon = () => {};
55
+ const outputKey = cutsAtGivenTimes ? KEYFRAME_OUTPUT_KEY : OUTPUT_KEY;
56
+ manager.segmentStore.useFormat(outputKey, fmp4Format);
57
+ const dirPath = manager.segmentStore.directoryFor(outputKey);
58
+ // On the even grid — the branch that writes finished files outright, so their
59
+ // existence is the proof and the fixture needs no header. The other branch
60
+ // writes no init file at all.
61
+ for (const index of segments) {
62
+ await writeFile(path.join(dirPath, fmp4Format.segmentFileName(index)), Buffer.alloc(256, 0x5a));
63
+ }
64
+
65
+ const session = {
66
+ id: SESSION_ID,
67
+ spec: cutsAtGivenTimes
68
+ ? outputSpec({ transcodeVideo: false, cutGrid: "keyframe" })
69
+ : outputSpec({ transcodeVideo: true, cutGrid: "uniform" }),
70
+ outputKey,
71
+ dirPath,
72
+ timeline: new Timeline({
73
+ boundaries: [0, 12.5, 25, 37.5, 50, 62.5],
74
+ cutGrid: cutsAtGivenTimes ? "keyframe" : "uniform"
75
+ }),
76
+ state: "ready",
77
+ file: new SourceFile({ sourceKey: "source-1", fileIndex: 0, name: "video.mkv" }).learn({ durationSeconds: 62.5 }),
78
+ get inputFile() { return this.file; },
79
+ get audioFile() { return this.file; },
80
+ lastAloneSpeed: 2,
81
+ startedAt: Date.now(),
82
+ lastAccessedAt: Date.now(),
83
+ runs: new Set(),
84
+ lastError: "",
85
+ consumers: new Set(),
86
+ viewers: new Map(),
87
+ segmentCount: 5,
88
+ segmentFormat: fmp4Format,
89
+ useSyntheticPlaylist: true,
90
+ playlistText: "#EXTM3U\n",
91
+ output: new Output({ encodeWidth: 0, encodeHeight: 0, outputFps: 25, softwarePreset: null, applyTonemap: false }),
92
+ keyframes: new KeyframeTable().learn({ times: [0, 12.5, 25, 37.5, 50], format: "matroska" }),
93
+ audioOnly: false,
94
+ waitEpoch: 0
95
+ };
96
+ manager.outputs.set(SESSION_ID, session);
97
+ manager.viewers.of(session, "viewer-a").moveTo(0);
98
+ // The encoder ended for good, which is what every request below is asked
99
+ // against.
100
+ if (failed) {
101
+ manager.encodeOrchestrator.noteEnded({
102
+ address: outputKey,
103
+ run: { state: ENCODE_RUN_STATE.ENDED_FAILED },
104
+ lastError: "ffmpeg exited with code 255"
105
+ });
106
+ assert.equal(manager.encodeRuns.hasFailed(session), true, "the fixture must describe a failed output");
107
+ }
108
+ return { manager, dirPath, outputKey };
109
+ }
110
+
111
+ /**
112
+ * Wait until the store really holds a wait for this output.
113
+ *
114
+ * A check that ends a wait it has not yet registered proves nothing: it passes
115
+ * on the code that answers a request AFTER the event just as well as on the code
116
+ * that answers one already standing. The deadline is a backstop, never the
117
+ * measurement.
118
+ *
119
+ * @param {object} store
120
+ * @param {string} key
121
+ * @param {number} [howMany]
122
+ * @returns {Promise<void>}
123
+ */
124
+ async function untilWaitsAreRegistered(store, key, howMany = 1) {
125
+ const giveUpAt = Date.now() + 5_000;
126
+ while (store.waitingFor(key) < howMany) {
127
+ if (Date.now() > giveUpAt) {
128
+ throw new Error(`no wait registered on ${key} after 5s — the check would prove nothing`);
129
+ }
130
+ await new Promise((resolve) => setImmediate(resolve));
131
+ }
132
+ }
133
+
134
+ test("a segment that is on disk is served although the encoder failed", async (t) => {
135
+ const { manager, dirPath } = await outputWhoseEncoderDied();
136
+ t.after(async () => {
137
+ await manager.lifecycle.disposeAll();
138
+ await rm(dirPath, { recursive: true, force: true });
139
+ });
140
+
141
+ const answer = await manager.serving.getFileStream(SESSION_ID, "segment-00000.mp4", { consumerId: "viewer-a" });
142
+
143
+ assert.equal(answer.kind, "file", `what is made is made — the answer was ${answer.kind}`);
144
+ assert.equal(answer.contentType, fmp4Format.segmentContentType);
145
+ });
146
+
147
+ test("a segment that is not there says the encoder failed, instead of being held", async (t) => {
148
+ const { manager, dirPath } = await outputWhoseEncoderDied();
149
+ t.after(async () => {
150
+ await manager.lifecycle.disposeAll();
151
+ await rm(dirPath, { recursive: true, force: true });
152
+ });
153
+
154
+ const answer = await manager.serving.getFileStream(SESSION_ID, "segment-00003.mp4", { consumerId: "viewer-a" });
155
+
156
+ assert.equal(answer.kind, "failed", "nothing is going to write it, and the viewer is owed the reason");
157
+ assert.match(answer.message, /255/);
158
+ });
159
+
160
+ test("the playlist is a fact of the timeline and outlives the encoder", async (t) => {
161
+ const { manager, dirPath } = await outputWhoseEncoderDied();
162
+ t.after(async () => {
163
+ await manager.lifecycle.disposeAll();
164
+ await rm(dirPath, { recursive: true, force: true });
165
+ });
166
+
167
+ const answer = await manager.serving.getFileStream(SESSION_ID, "index.m3u8", { consumerId: "viewer-a" });
168
+
169
+ assert.equal(answer.kind, "file");
170
+ assert.equal(answer.isPlaylist, true);
171
+ });
172
+
173
+ test("a failure reaches a request that was ALREADY waiting", async (t) => {
174
+ // The case the other checks in this file do not reach: they set the failure
175
+ // and then ask. A wait that began BEFORE it is ended by the publication, by a
176
+ // wake, by the requester going or by its own deadline — and a failure is none
177
+ // of those, so it sat out the whole deadline for a piece nothing was going to
178
+ // make, and where the page states no deadline, until the viewer disconnected.
179
+ const { manager, dirPath } = await outputWhoseEncoderDied({ failed: false });
180
+ t.after(async () => {
181
+ await manager.lifecycle.disposeAll();
182
+ await rm(dirPath, { recursive: true, force: true });
183
+ });
184
+ const session = manager.outputs.get(SESSION_ID);
185
+ const child = fakeProcess();
186
+ const run = new EncodeRun({
187
+ address: OUTPUT_KEY,
188
+ encoder: { name: "libx264", kind: "software" },
189
+ from: 3,
190
+ to: 4,
191
+ buildArgs: () => [],
192
+ spawn: () => child,
193
+ logger: silentLogger,
194
+ lastSegmentIndex: () => 4,
195
+ because: "a check asked for it",
196
+ onEnded: (ended) => manager.encodeRuns.noteRunEnded(session, run, ended)
197
+ });
198
+ manager.encodeOrchestrator.adopt(OUTPUT_KEY, run);
199
+
200
+ // Held with a deadline long enough that running it out IS the failure.
201
+ const held = waitForSessionFile(manager.serving, SESSION_ID, "segment-00003.mp4", {
202
+ holdMs: 30_000,
203
+ consumerId: "viewer-a"
204
+ });
205
+ // The wait must EXIST before it can be said to have been woken.
206
+ await untilWaitsAreRegistered(manager.segmentStore, OUTPUT_KEY);
207
+
208
+ child.exit(255);
209
+
210
+ // Bounded, because a regression here would otherwise HANG for the whole
211
+ // thirty-second hold and take the rest of the file down with it.
212
+ const answer = await settledWithin(held, 5_000);
213
+
214
+ assert.notEqual(answer, STILL_WAITING, "it was left to sit out its whole hold");
215
+ assert.equal(answer.kind, "failed", "a wait nothing will satisfy must be told");
216
+ });
217
+
218
+ /** What a race answers when the promise did not settle in time. */
219
+ const STILL_WAITING = Symbol("still waiting");
220
+
221
+ /**
222
+ * The promise's answer, or {@link STILL_WAITING} if it has not settled yet.
223
+ *
224
+ * Used for BOTH directions, and the direction decides what the bound means. On
225
+ * "it must be told", the bound is a backstop that turns a regression into a
226
+ * failure in seconds instead of a test that hangs until the hold runs out — and
227
+ * a hanging test is worse than an absent one, because it takes the rest of the
228
+ * file down with it (measured here: three checks cancelled by one hang). On
229
+ * "it must go on waiting", the bound IS the grace, and it cannot fail red — it
230
+ * can only pass too easily.
231
+ *
232
+ * @param {Promise<unknown>} promise
233
+ * @param {number} withinMs
234
+ * @returns {Promise<unknown>}
235
+ */
236
+ async function settledWithin(promise, withinMs) {
237
+ return await Promise.race([
238
+ promise,
239
+ new Promise((resolve) => setTimeout(() => resolve(STILL_WAITING), withinMs))
240
+ ]);
241
+ }
242
+
243
+ test("a run that exits 0 having produced less than promised also reaches a waiting request", async (t) => {
244
+ // ffmpeg exits 0 both at the end of the file and when its input stops
245
+ // delivering, so a run that stopped short claims success. That ending is
246
+ // terminal all the same — and it was the one branch of the exit handler that
247
+ // returned before anybody waiting was told.
248
+ const { manager, dirPath } = await outputWhoseEncoderDied({ failed: false });
249
+ t.after(async () => {
250
+ await manager.lifecycle.disposeAll();
251
+ await rm(dirPath, { recursive: true, force: true });
252
+ });
253
+ const session = manager.outputs.get(SESSION_ID);
254
+ const child = fakeProcess();
255
+ const run = new EncodeRun({
256
+ address: OUTPUT_KEY,
257
+ encoder: { name: "libx264", kind: "software" },
258
+ from: 3,
259
+ to: 4,
260
+ buildArgs: () => [],
261
+ spawn: () => child,
262
+ logger: silentLogger,
263
+ lastSegmentIndex: () => 4,
264
+ because: "a check asked for it",
265
+ onEnded: (ended) => manager.encodeRuns.noteRunEnded(session, run, ended)
266
+ });
267
+ manager.encodeOrchestrator.adopt(OUTPUT_KEY, run);
268
+
269
+ const held = waitForSessionFile(manager.serving, SESSION_ID, "segment-00003.mp4", {
270
+ holdMs: 30_000,
271
+ consumerId: "viewer-a"
272
+ });
273
+ await untilWaitsAreRegistered(manager.segmentStore, OUTPUT_KEY);
274
+
275
+ // Zero, having made none of the two segments it was given.
276
+ child.exit(0);
277
+
278
+ const answer = await settledWithin(held, 5_000);
279
+
280
+ assert.equal(manager.encodeRuns.hasFailed(session), true, "stopping short is a failure, not a finished file");
281
+ assert.notEqual(answer, STILL_WAITING, "it was left to sit out its whole hold");
282
+ assert.equal(answer.kind, "failed", "a wait nothing will satisfy must be told");
283
+ });
284
+
285
+ test("one run of two ending in failure tells nobody — the other is still making it", async (t) => {
286
+ // The condition is the OUTPUT's state, not this run's ending. A run that
287
+ // failed while another is alive on the same output has not made the segment
288
+ // unobtainable, so a request for it goes on waiting rather than being handed
289
+ // an error by the run that lost.
290
+ const { manager, dirPath } = await outputWhoseEncoderDied({ failed: false });
291
+ t.after(async () => {
292
+ await manager.lifecycle.disposeAll();
293
+ await rm(dirPath, { recursive: true, force: true });
294
+ });
295
+ const session = manager.outputs.get(SESSION_ID);
296
+ const runOf = (from, to, child) => {
297
+ const run = new EncodeRun({
298
+ address: OUTPUT_KEY,
299
+ encoder: { name: "libx264", kind: "software" },
300
+ from,
301
+ to,
302
+ buildArgs: () => [],
303
+ spawn: () => child,
304
+ logger: silentLogger,
305
+ lastSegmentIndex: () => 4,
306
+ because: "a check asked for it",
307
+ onEnded: (ended) => manager.encodeRuns.noteRunEnded(session, run, ended)
308
+ });
309
+ manager.encodeOrchestrator.adopt(OUTPUT_KEY, run);
310
+ return run;
311
+ };
312
+ const dying = fakeProcess();
313
+ const working = fakeProcess();
314
+ runOf(3, 3, dying);
315
+ runOf(4, 4, working);
316
+
317
+ const held = waitForSessionFile(manager.serving, SESSION_ID, "segment-00003.mp4", {
318
+ holdMs: 30_000,
319
+ consumerId: "viewer-a"
320
+ });
321
+ await untilWaitsAreRegistered(manager.segmentStore, OUTPUT_KEY);
322
+
323
+ dying.exit(255);
324
+
325
+ assert.equal(
326
+ manager.encodeRuns.hasFailed(session),
327
+ false,
328
+ "the output has not failed while a run is alive on it"
329
+ );
330
+ assert.equal(await settledWithin(held, 300), STILL_WAITING, "the request must go on waiting");
331
+ });
332
+
333
+ test("an init that will never be made says so instead of warming up for ever", async (t) => {
334
+ // On the branch that lifts the header out of the first piece there is no file
335
+ // to be missing: `#initFromFirstSegment` answers null rather than throwing, so
336
+ // the catch that names a failure is never reached and the answer was always
337
+ // "still warming up".
338
+ //
339
+ // THE FIXTURE MUST BE ON THAT BRANCH, and it was not: with a re-encode on the
340
+ // even grid `cutsAtGivenTimes` is false, so the check read a missing init
341
+ // FILE and exercised the `ENOENT` path instead — the one that was never
342
+ // broken. A copy cut at the source's own keyframes is the configuration that
343
+ // writes no init file at all.
344
+ const { manager, dirPath } = await outputWhoseEncoderDied({ segments: [], cutsAtGivenTimes: true });
345
+ t.after(async () => {
346
+ await manager.lifecycle.disposeAll();
347
+ await rm(dirPath, { recursive: true, force: true });
348
+ });
349
+
350
+ const answer = await manager.serving.getFileStream(SESSION_ID, "init.mp4", { consumerId: "viewer-a" });
351
+
352
+ assert.equal(answer.kind, "failed", "nothing is going to produce the header it would come from");
353
+ assert.match(answer.message, /255/);
354
+ });