@torrent-tv/proxy 2.89.17 → 2.89.19

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 (300) hide show
  1. package/README.md +14 -3
  2. package/package.json +10 -2
  3. package/server.js +0 -11
  4. package/utils/client-log-file.js +147 -42
  5. package/.claude/commands/opsx/apply.md +0 -155
  6. package/.claude/commands/opsx/archive.md +0 -160
  7. package/.claude/commands/opsx/explore.md +0 -174
  8. package/.claude/commands/opsx/propose.md +0 -109
  9. package/.claude/commands/opsx/sync.md +0 -143
  10. package/.claude/skills/openspec-apply-change/SKILL.md +0 -159
  11. package/.claude/skills/openspec-archive-change/SKILL.md +0 -117
  12. package/.claude/skills/openspec-explore/SKILL.md +0 -289
  13. package/.claude/skills/openspec-propose/SKILL.md +0 -113
  14. package/.claude/skills/openspec-sync-specs/SKILL.md +0 -147
  15. package/.dockerignore +0 -5
  16. package/AGENTS.md +0 -363
  17. package/CHANGELOG.md +0 -2377
  18. package/Dockerfile +0 -30
  19. package/biome.json +0 -412
  20. package/corpus.tmp.json +0 -1
  21. package/docs/browser-proxy-contract.md +0 -155
  22. package/docs/container-architecture.md +0 -273
  23. package/docs/download-architecture.md +0 -261
  24. package/docs/encode-architecture.md +0 -765
  25. package/docs/encode-run-state.md +0 -96
  26. package/docs/logs.md +0 -151
  27. package/docs/playback-start-forecast.md +0 -113
  28. package/docs/socks5-egress-research.md +0 -114
  29. package/docs/storage-architecture.md +0 -273
  30. package/knip.json +0 -14
  31. package/main2.cpuprofile +0 -1
  32. package/openspec/changes/adaptive-bitrate/.openspec.yaml +0 -0
  33. package/openspec/changes/adaptive-bitrate/design.md +0 -92
  34. package/openspec/changes/adaptive-bitrate/proposal.md +0 -56
  35. package/openspec/changes/adaptive-bitrate/specs/adaptive-bitrate/spec.md +0 -51
  36. package/openspec/changes/adaptive-bitrate/tasks.md +0 -40
  37. package/openspec/changes/binary-distribution/.openspec.yaml +0 -2
  38. package/openspec/changes/binary-distribution/proposal.md +0 -53
  39. package/openspec/changes/chunked-request-bodies/design.md +0 -81
  40. package/openspec/changes/chunked-request-bodies/proposal.md +0 -53
  41. package/openspec/changes/chunked-request-bodies/specs/chunked-request-bodies/spec.md +0 -30
  42. package/openspec/changes/chunked-request-bodies/tasks.md +0 -40
  43. package/openspec/changes/cold-start/design.md +0 -98
  44. package/openspec/changes/cold-start/proposal.md +0 -62
  45. package/openspec/changes/cold-start/specs/cold-start/spec.md +0 -47
  46. package/openspec/changes/cold-start/tasks.md +0 -48
  47. package/openspec/changes/disk-cap/.openspec.yaml +0 -2
  48. package/openspec/changes/disk-cap/proposal.md +0 -37
  49. package/openspec/changes/disk-cap/specs/disk-cap/spec.md +0 -25
  50. package/openspec/changes/disk-cap/tasks.md +0 -19
  51. package/openspec/changes/proxy-observability/.openspec.yaml +0 -2
  52. package/openspec/changes/proxy-observability/design.md +0 -38
  53. package/openspec/changes/proxy-observability/proposal.md +0 -49
  54. package/openspec/changes/proxy-observability/specs/observability/spec.md +0 -33
  55. package/openspec/changes/proxy-observability/tasks.md +0 -21
  56. package/openspec/changes/seek-debounce/.openspec.yaml +0 -0
  57. package/openspec/changes/seek-debounce/design.md +0 -118
  58. package/openspec/changes/seek-debounce/proposal.md +0 -59
  59. package/openspec/changes/seek-debounce/specs/seek-debounce/spec.md +0 -41
  60. package/openspec/changes/seek-debounce/tasks.md +0 -33
  61. package/openspec/changes/subtitle-language/.openspec.yaml +0 -2
  62. package/openspec/changes/subtitle-language/proposal.md +0 -50
  63. package/openspec/changes/subtitle-language/specs/subtitle-language/spec.md +0 -34
  64. package/openspec/changes/subtitle-language/tasks.md +0 -26
  65. package/openspec/changes/track-selection/.openspec.yaml +0 -2
  66. package/openspec/changes/track-selection/design.md +0 -47
  67. package/openspec/changes/track-selection/proposal.md +0 -45
  68. package/openspec/changes/track-selection/specs/track-selection/spec.md +0 -41
  69. package/openspec/changes/track-selection/tasks.md +0 -20
  70. package/openspec/changes/transcode-quality/.openspec.yaml +0 -2
  71. package/openspec/changes/transcode-quality/proposal.md +0 -51
  72. package/openspec/changes/transcode-quality/specs/transcode-quality/spec.md +0 -139
  73. package/openspec/changes/transcode-quality/tasks.md +0 -63
  74. package/openspec/config.yaml +0 -38
  75. package/public/.well-known/appspecific/com.chrome.devtools.json +0 -1
  76. package/research/delivery-wedge-stand-ladder-2026-09-13.md +0 -329
  77. package/research/double-spawn-2026-09-10.md +0 -171
  78. package/research/handover-2026-09-13.md +0 -209
  79. package/research/handover-reader-claims-removal-2026-09-12.md +0 -166
  80. package/research/piece-withdrawn-but-still-claimed-2026-09-12.md +0 -175
  81. package/research/preset-benchmark-size-scaling-2026-08-22.md +0 -55
  82. package/research/priority-map-is-the-truth-2026-09-12.md +0 -225
  83. package/research/valgrind-replica-2026-08-27/logs.tar.gz +0 -0
  84. package/scripts/build-audio-samples.mjs +0 -31
  85. package/scripts/render-run-graph.js +0 -173
  86. package/test/asset-paths.test.js +0 -28
  87. package/test/audio-buffer-continuity.test.js +0 -77
  88. package/test/audio-calibration.test.js +0 -23
  89. package/test/audio-inventory.test.js +0 -212
  90. package/test/audio-track-bitrate.test.js +0 -40
  91. package/test/audio-warm-recorded.test.js +0 -63
  92. package/test/auto-quality-step.test.js +0 -762
  93. package/test/available-share.test.js +0 -76
  94. package/test/background-fill.test.js +0 -190
  95. package/test/client-log-file.test.js +0 -126
  96. package/test/client-logs-route.test.js +0 -34
  97. package/test/coalesce.test.js +0 -39
  98. package/test/completed-files.test.js +0 -115
  99. package/test/concurrent-cost.test.js +0 -148
  100. package/test/container-media-info.test.js +0 -228
  101. package/test/contention.test.js +0 -76
  102. package/test/core-dumps.test.js +0 -57
  103. package/test/coverage-follows-the-disk.test.js +0 -233
  104. package/test/coverage-map.test.js +0 -195
  105. package/test/cue-push-carries-what-the-walk-found.test.js +0 -58
  106. package/test/cut-times-timeline.test.js +0 -65
  107. package/test/cuts-follow-published-grid.test.js +0 -109
  108. package/test/data-channel-frame.test.js +0 -63
  109. package/test/data-channel-viewer-lifecycle.test.js +0 -193
  110. package/test/dead-torrent-handle.test.js +0 -52
  111. package/test/declared-rates.test.js +0 -116
  112. package/test/declared-tracks.test.js +0 -43
  113. package/test/decode-cost-fit.test.js +0 -202
  114. package/test/decode-cost.test.js +0 -475
  115. package/test/delivery-probe-field.test.js +0 -173
  116. package/test/delivery-probe.test.js +0 -382
  117. package/test/demand-register.test.js +0 -206
  118. package/test/dht-bootstrap.test.js +0 -72
  119. package/test/drain-threat.test.js +0 -65
  120. package/test/duplicate-add.test.js +0 -48
  121. package/test/encode-admission.test.js +0 -170
  122. package/test/encode-cost.test.js +0 -275
  123. package/test/encode-exit.test.js +0 -111
  124. package/test/encode-orchestrator.test.js +0 -426
  125. package/test/encode-plan-viewers.test.js +0 -725
  126. package/test/encode-plan.test.js +0 -545
  127. package/test/encode-run-state.test.js +0 -314
  128. package/test/encode-run.test.js +0 -248
  129. package/test/encode-slope.test.js +0 -46
  130. package/test/encoded-output.test.js +0 -39
  131. package/test/encoder-kinds.test.js +0 -122
  132. package/test/encoder-readings.test.js +0 -68
  133. package/test/episode-naming.test.js +0 -131
  134. package/test/every-claimant-is-told-its-share.test.js +0 -152
  135. package/test/failed-encoder-still-serves-what-exists.test.js +0 -354
  136. package/test/failed-start-breaker.test.js +0 -202
  137. package/test/fastest-wires.test.js +0 -143
  138. package/test/ffmpeg-stream-counts.test.js +0 -101
  139. package/test/file-edges.test.js +0 -191
  140. package/test/files-in-use.test.js +0 -34
  141. package/test/fixtures/audio-frame-boundary.json +0 -1
  142. package/test/fixtures/refused-audio-20261002.json +0 -10
  143. package/test/fmp4-presentation-clock.test.js +0 -148
  144. package/test/fmp4-self-contained.test.js +0 -80
  145. package/test/fmp4-timestamps.test.js +0 -140
  146. package/test/fmp4-track-completeness.test.js +0 -41
  147. package/test/format-keyed-outputs.test.js +0 -78
  148. package/test/health-metrics.test.js +0 -36
  149. package/test/held-read-ownership.test.js +0 -77
  150. package/test/held-request-width.test.js +0 -157
  151. package/test/helpers/encode-run.js +0 -248
  152. package/test/helpers/held-file.js +0 -77
  153. package/test/helpers/manager.js +0 -62
  154. package/test/helpers/matroska-file.js +0 -296
  155. package/test/helpers/output-spec.js +0 -63
  156. package/test/host-load.test.js +0 -68
  157. package/test/index-accuracy-is-about-the-file.test.js +0 -128
  158. package/test/init-compat.test.js +0 -219
  159. package/test/input-lost-quiets-the-plan.test.js +0 -261
  160. package/test/input-unavailable.test.js +0 -48
  161. package/test/installation-id.test.js +0 -39
  162. package/test/keeping-period.test.js +0 -83
  163. package/test/keyframe-index-accuracy.test.js +0 -188
  164. package/test/keyframe-table-budget.test.js +0 -123
  165. package/test/keyframes-belong-to-the-file.test.js +0 -249
  166. package/test/learned-median.test.js +0 -37
  167. package/test/letting-go-asks-who-is-watching.test.js +0 -191
  168. package/test/limit-row-by-area.test.js +0 -60
  169. package/test/link-budget.test.js +0 -111
  170. package/test/link-probe.test.js +0 -56
  171. package/test/local-encoding-data.test.js +0 -273
  172. package/test/logger-repeats.test.js +0 -134
  173. package/test/look-ahead-coverage.test.js +0 -56
  174. package/test/manager-surface.test.js +0 -291
  175. package/test/matroska-blocks.test.js +0 -0
  176. package/test/matroska-cluster-discovery.test.js +0 -323
  177. package/test/matroska-cues-track.test.js +0 -192
  178. package/test/media-time.test.js +0 -62
  179. package/test/memory-budget.test.js +0 -390
  180. package/test/move-cost.test.js +0 -216
  181. package/test/mp4-composition-times.test.js +0 -0
  182. package/test/mp4-subtitles.test.js +0 -173
  183. package/test/one-authority.test.js +0 -285
  184. package/test/one-budget.test.js +0 -149
  185. package/test/one-output-one-session.test.js +0 -273
  186. package/test/one-resource-divided.test.js +0 -185
  187. package/test/one-serving-output-per-height.test.js +0 -458
  188. package/test/orchestrator-wired.test.js +0 -207
  189. package/test/output-catalog.test.js +0 -39
  190. package/test/output-shape.test.js +0 -21
  191. package/test/output-spec.test.js +0 -251
  192. package/test/output-speed.test.js +0 -92
  193. package/test/outputs-being-prepared.test.js +0 -40
  194. package/test/packet-witness-ring.test.js +0 -236
  195. package/test/packet-witness.test.js +0 -148
  196. package/test/piece-completeness.test.js +0 -78
  197. package/test/piece-disk-store.test.js +0 -401
  198. package/test/piece-from-whole-file.test.js +0 -129
  199. package/test/piece-lru.test.js +0 -319
  200. package/test/piece-reader.test.js +0 -162
  201. package/test/piece-selection-offset.test.js +0 -85
  202. package/test/piece-store-eviction.test.js +0 -595
  203. package/test/piece-store-never-refuses.test.js +0 -153
  204. package/test/piece-store-reservations.test.js +0 -403
  205. package/test/piece-store-slow-disk.test.js +0 -159
  206. package/test/piece-tail.test.js +0 -97
  207. package/test/plan-host-timings.test.js +0 -68
  208. package/test/plan-is-steady.test.js +0 -225
  209. package/test/playback-readiness.test.js +0 -518
  210. package/test/playback-start-regressions.test.js +0 -101
  211. package/test/priority-map-download.test.js +0 -174
  212. package/test/priority-map-emptied.test.js +0 -204
  213. package/test/priority-map-per-output.test.js +0 -215
  214. package/test/priority-map.test.js +0 -163
  215. package/test/probe-wedge-certainty.test.js +0 -60
  216. package/test/produced-copy-choice.test.js +0 -213
  217. package/test/published-timeline.test.js +0 -83
  218. package/test/quality-offer.test.js +0 -160
  219. package/test/quality-variants.test.js +0 -1274
  220. package/test/rate-control-in-the-output.test.js +0 -181
  221. package/test/read-survives-withdrawal.test.js +0 -164
  222. package/test/read-window.test.js +0 -365
  223. package/test/reads-not-yet-arrived.test.js +0 -218
  224. package/test/resume-warm.test.js +0 -39
  225. package/test/run-graph-drift.test.js +0 -37
  226. package/test/run-inputs.test.js +0 -80
  227. package/test/run-intervals.test.js +0 -100
  228. package/test/run-position-follows-published-grid.test.js +0 -89
  229. package/test/run-progress.test.js +0 -64
  230. package/test/same-height-switch.test.js +0 -663
  231. package/test/seek-frees-the-viewers-requests.test.js +0 -217
  232. package/test/seek-landing.test.js +0 -109
  233. package/test/seek-target-not-superseded.test.js +0 -195
  234. package/test/segment-closed-on-both-branches.test.js +0 -114
  235. package/test/segment-cut-times.test.js +0 -63
  236. package/test/segment-demand.test.js +0 -80
  237. package/test/segment-hold-supersede.test.js +0 -91
  238. package/test/segment-judged-by-its-own-header.test.js +0 -222
  239. package/test/segment-serve-wiring.test.js +0 -481
  240. package/test/segment-store-eviction.test.js +0 -247
  241. package/test/segment-store.test.js +0 -352
  242. package/test/segment-true-start.test.js +0 -86
  243. package/test/segments-are-shared.test.js +0 -176
  244. package/test/send-chunk-bytes.test.js +0 -86
  245. package/test/serving-existing-output.test.js +0 -111
  246. package/test/serving-output.test.js +0 -102
  247. package/test/shared-piece-store.test.js +0 -310
  248. package/test/sidecar-naming.test.js +0 -142
  249. package/test/soundtrack-rate-bound.test.js +0 -289
  250. package/test/source-file.test.js +0 -133
  251. package/test/source-files-route.test.js +0 -113
  252. package/test/source-registry.test.js +0 -18
  253. package/test/split-a-stretch.test.js +0 -139
  254. package/test/stale-request-after-seek.test.js +0 -158
  255. package/test/startup-readings.test.js +0 -124
  256. package/test/stream-route.test.js +0 -237
  257. package/test/subtitle-cue-framing.test.js +0 -200
  258. package/test/subtitle-cue-source.test.js +0 -102
  259. package/test/subtitle-cue-walk.test.js +0 -385
  260. package/test/subtitle-cursor.test.js +0 -63
  261. package/test/subtitle-defaults.test.js +0 -118
  262. package/test/subtitle-language.test.js +0 -252
  263. package/test/subtitle-metadata.test.js +0 -15
  264. package/test/subtitle-plan-waits.test.js +0 -125
  265. package/test/subtitle-track-numbering.test.js +0 -391
  266. package/test/supply-margin.test.js +0 -190
  267. package/test/swarm-follows-readers.test.js +0 -174
  268. package/test/swarm-reach.test.js +0 -130
  269. package/test/swarm-selection.test.js +0 -220
  270. package/test/tail-duplication.test.js +0 -167
  271. package/test/the-channel-has-both-ends.test.js +0 -91
  272. package/test/the-torrent-thread-serves-bytes.test.js +0 -107
  273. package/test/thread-pool.test.js +0 -53
  274. package/test/timeline.test.js +0 -84
  275. package/test/torrent-contents.test.js +0 -235
  276. package/test/torrent-cost.test.js +0 -71
  277. package/test/torrent-files.test.js +0 -221
  278. package/test/torrent-naming.test.js +0 -255
  279. package/test/tracks-begin-together.test.js +0 -209
  280. package/test/tunnel-renewal.test.js +0 -167
  281. package/test/two-viewers-one-picture.test.js +0 -508
  282. package/test/upload-hurry.test.js +0 -186
  283. package/test/usrsctp-state.test.js +0 -131
  284. package/test/video-facts.test.js +0 -102
  285. package/test/viewer-assignments.test.js +0 -143
  286. package/test/viewer-outputs.test.js +0 -285
  287. package/test/viewer-presence.test.js +0 -116
  288. package/test/viewer-requests-admission.test.js +0 -92
  289. package/test/viewer-starts-on-map.test.js +0 -27
  290. package/test/viewer-states.test.js +0 -192
  291. package/test/viewer-subtitle-subscription.test.js +0 -96
  292. package/test/viewer.test.js +0 -139
  293. package/test/viewing-generations.test.js +0 -390
  294. package/test/visible-rung.test.js +0 -37
  295. package/test/wait-ledger.test.js +0 -100
  296. package/test/webrtc-shim.test.js +0 -149
  297. package/test/wedge-certainty.test.js +0 -131
  298. package/test/whole-file-held-read.test.js +0 -30
  299. package/test/withdraw-piece-claim.test.js +0 -212
  300. package/test/worker-channel.test.js +0 -158
@@ -1,545 +0,0 @@
1
- /**
2
- * @file How many encoders there should be, and where.
3
- *
4
- * Every case here is one the code used to get wrong for a reason recorded in
5
- * `research/encoder-layer-2026-09-04.md`: a run placed at a viewer's position
6
- * rather than at the first thing missing, a run with no end, and nothing that
7
- * stops a run which has caught up with material already made.
8
- */
9
-
10
- import test from "node:test";
11
- import assert from "node:assert/strict";
12
- import { CoverageMap } from "../services/encode/CoverageMap.js";
13
- import { endOfRun } from "../services/encode/EncodeRun.js";
14
- import { firstUnmetWant, planEncoders } from "../services/encode/EncodePlan.js";
15
- import { contentionPenalty, penaltiesFrom } from "../services/encode/contention.js";
16
-
17
- // WHAT A SECOND ENCODER COSTS THE FIRST — measured, never a formula.
18
- //
19
- // Addon host, 2026-09-03: 854x480 through libx264 `ultrafast` ran at 7.12x with
20
- // the machine to itself, and at 4.20x and 4.16x when two ran at once. The
21
- // penalty is read off that reading by the SAME two functions production uses,
22
- // so nothing here invents a shape: beyond what was measured the reading is held
23
- // rather than extrapolated.
24
- const MEASURED_PENALTIES = penaltiesFrom(7.12, [{ others: 1, speed: 4.18 }]);
25
- const penaltyFor = (others) => contentionPenalty(others, MEASURED_PENALTIES).penalty;
26
-
27
- // What a start and a stop cost, measured on the same host: a spawn with its
28
- // input open is 0.12 s there.
29
- const RUN_COSTS = { killCostSec: 0, firstByteWaitSec: 0.12 };
30
-
31
-
32
- /** A host that can afford two encoders, four-second segments, a cheap restart. */
33
- // A host that has measured itself: the start and the death from its own runs,
34
- // and what the swarm charges to fetch a second of film again. All four terms
35
- // have to be present for the drive-or-move comparison to mean anything, and a
36
- // host missing any of them keeps its encoders instead — which is its own check
37
- // below rather than the shape every other check is written against.
38
- const HOST = {
39
- // Measured on the addon host: a second encoder beside the first costs about
40
- // half its speed. Without it an extra process is free and the score always
41
- // wants more of them.
42
- contentionPenaltyFor: penaltyFor,
43
- // What the startup benchmark says this host encodes at, in realtimes. It is
44
- // measured before any viewer exists, so the plan always has a speed.
45
- speedX: 2,
46
- maxRuns: 2,
47
- segmentSeconds: 4,
48
- killCostSec: 0.5,
49
- firstByteWaitSec: 1,
50
- refetchSecPerFilmSecond: 0.25
51
- };
52
-
53
- /**
54
- * @param {Partial<import("../services/encode/EncodePlan.js").LiveRun>} run
55
- * @returns {import("../services/encode/EncodePlan.js").LiveRun}
56
- */
57
- function run(run_) {
58
- // A run has no name: the plan hands back the run itself, so a test compares
59
- // the thing rather than a token standing for it.
60
- return { from: 0, to: 100, head: 0, speedX: 2, ...run_ };
61
- }
62
-
63
- test("a viewer waiting on nothing made starts one encoder, at what they are waiting for", () => {
64
- // It starts where the viewer is stopped and runs to the end of the film,
65
- // because nothing else is in the way. The window says where to start and
66
- // whether to start; it does not say where to stop — a window travels forward
67
- // as the viewer plays, and a run bounded by one has to be replaced every few
68
- // seconds.
69
- const coverage = new CoverageMap({ segmentCount: 100 });
70
- const actions = planEncoders({
71
- coverage,
72
- windows: [{ from: 40, to: 70 }],
73
- runs: [],
74
- ...HOST
75
- });
76
- assert.deepEqual(
77
- actions.map((action) => ({ type: action.type, from: action.from, to: action.to })),
78
- [{ type: "start", from: 40, to: 99 }]
79
- );
80
- });
81
-
82
- test("two viewers a couple of numbers apart are never given the same stretch twice", () => {
83
- // Found by the orchestrator's own checks. Under the old design the second
84
- // viewer was given an encoder of their own while the run already there had
85
- // nowhere left to go — two processes side by side for one stretch of film.
86
- //
87
- // The model answers it by arithmetic instead of by a rule: the number the
88
- // second viewer stands on is due now and the encoder behind them needs six
89
- // seconds, so it IS late; a new one delivers it in two, so placing one is
90
- // worth it; and the one behind is then bounded to the two numbers it can still
91
- // make and retires. One encoder, and the second viewer waits four seconds
92
- // less. What must never happen — two of them running the same stretch — does
93
- // not.
94
- const coverage = new CoverageMap({ segmentCount: 1000 });
95
- const runA = run({ from: 100, to: 999, head: 100 });
96
- coverage.claim(runA, 100, 999);
97
- const actions = planEncoders({
98
- coverage,
99
- windows: [{ from: 100, to: 130 }, { from: 102, to: 132 }],
100
- runs: [runA],
101
- ...HOST
102
- });
103
- const spans = actions
104
- .filter((action) => action.type !== "stop")
105
- .map((action) => [action.from, action.to])
106
- .sort((left, right) => left[0] - right[0]);
107
- for (let index = 0; index < spans.length - 1; index += 1) {
108
- assert.ok(spans[index][1] < spans[index + 1][0],
109
- `#${spans[index][0]}..#${spans[index][1]} overlaps #${spans[index + 1][0]}`);
110
- }
111
- assert.ok(spans.some(([from, to]) => from <= 102 && (to < from || to >= 102)),
112
- "somebody is making what the second viewer needs");
113
- // How MANY is the score's answer and not a rule: with these two standing two
114
- // numbers apart, a second process makes the one in front wait 1.4 s longer and
115
- // the one behind 2.5 s less, so the pair is better off. What is fixed is that
116
- // they never share a stretch, which the loop above asserts.
117
- });
118
-
119
- test("a viewer whose whole window is already made starts nothing", () => {
120
- // The case that used to restart an encoder to make a second copy of material
121
- // sitting on the disk.
122
- const coverage = new CoverageMap({ segmentCount: 100 });
123
- for (let index = 40; index <= 70; index += 1) {
124
- coverage.markReady(index);
125
- }
126
- const actions = planEncoders({ coverage, windows: [{ from: 40, to: 70 }], runs: [], ...HOST });
127
- assert.deepEqual(actions, []);
128
- });
129
-
130
- test("a run is given an end at the edge of what is free", () => {
131
- // Handed the rest of the film it would drive straight into another run's
132
- // ground; handed the free stretch it stops where the covered material starts.
133
- const coverage = new CoverageMap({ segmentCount: 100 });
134
- const runB = run({ from: 50, to: 80 });
135
- coverage.claim(runB, 50, 80);
136
- const actions = planEncoders({ coverage, windows: [{ from: 40, to: 90 }], runs: [], ...HOST });
137
- assert.equal(actions.length, 1);
138
- assert.deepEqual({ from: actions[0].from, to: actions[0].to }, { from: 40, to: 49 });
139
- });
140
-
141
- test("a run that has caught up with made material is not killed for it", () => {
142
- // It has arrived at film somebody else made. Killing it was the old answer and
143
- // it is never the right one: the work is either taken past the made stretch or
144
- // left to drive through, and which of those depends on the score, but the
145
- // encoder goes on existing either way.
146
- const coverage = new CoverageMap({ segmentCount: 100 });
147
- const runA = run({ head: 10, from: 0, to: 100 });
148
- coverage.claim(runA, 0, 100);
149
- for (let index = 10; index <= 30; index += 1) {
150
- coverage.markReady(index);
151
- }
152
- const actions = planEncoders({
153
- coverage,
154
- windows: [{ from: 0, to: 90 }],
155
- runs: [runA],
156
- ...HOST
157
- });
158
- assert.equal(actions.some((action) => action.type === "stop"), false, "not killed");
159
- assert.ok(
160
- actions.some((action) => (action.type === "keep" || action.type === "move") && action.run === runA),
161
- "it is still one of the encoders on this output"
162
- );
163
- // And nothing is arranged so that two of them make the same piece.
164
- const spans = actions
165
- .filter((action) => action.type !== "stop")
166
- .map((action) => [action.from, action.to < action.from ? Number.POSITIVE_INFINITY : action.to])
167
- .sort((left, right) => left[0] - right[0]);
168
- for (let index = 0; index < spans.length - 1; index += 1) {
169
- assert.ok(spans[index][1] < spans[index + 1][0], "the stretches do not overlap");
170
- }
171
- });
172
-
173
- test("a covered stretch shorter than a restart is driven through instead", () => {
174
- // The comparison is of two measured quantities: the covered stretch divided
175
- // by this run's own speed, against what a restart costs. One segment at 50x
176
- // is 0.08 s of encoding against 0.12 s to move.
177
- const coverage = new CoverageMap({ segmentCount: 100 });
178
- const runA = run({ head: 10, speedX: 50 });
179
- coverage.claim(runA, 0, 100);
180
- coverage.markReady(10);
181
- const actions = planEncoders({
182
- coverage,
183
- // From where the run stands, so the only question asked is the covered piece
184
- // under it. Beginning at #0 would also be asking who makes #0..#9.
185
- windows: [{ from: 10, to: 90 }],
186
- runs: [runA],
187
- ...HOST
188
- });
189
- assert.equal(actions.some((action) => action.type === "move"), false);
190
- assert.ok(actions.some((action) => action.type === "keep" && action.run === runA));
191
- });
192
-
193
- test("a run standing on film nobody has made is never taken away", () => {
194
- // The field failure this guards: 684 starts in 482 seconds on 2026-09-05,
195
- // because a run was moved whenever anything ahead of it had been made.
196
- //
197
- // Moving is priced rather than forbidden — a run standing ON made film may
198
- // well be worth restarting one number along, and the two checks above measure
199
- // both sides of that. What can never be worth it is moving a run that has
200
- // nothing made under it or ahead of it: there is no work to skip, so the move
201
- // buys nothing and costs a start.
202
- const coverage = new CoverageMap({ segmentCount: 100 });
203
- const runA = run({ head: 10, speedX: 2 });
204
- coverage.claim(runA, 0, 100);
205
- const actions = planEncoders({
206
- coverage,
207
- windows: [{ from: 10, to: 90 }],
208
- runs: [runA],
209
- ...HOST
210
- });
211
- assert.equal(actions.some((action) => action.type === "move"), false);
212
- assert.ok(actions.some((action) => action.type === "keep" && action.run === runA));
213
- });
214
-
215
- test("a long stretch of made film is skipped, and the swarm's price is in the reckoning", () => {
216
- // Driving through costs this encoder's time AND the swarm the same bytes a
217
- // second time. Twenty made pieces of 4 s at 1x is 80 s of encoding plus 20 s
218
- // of fetching, against a move priced at 0.12 + 0.5 + 3 seconds — so it skips.
219
- //
220
- // There is no separate comparison to read here: both are seconds, both are in
221
- // the one score, and the arrangement with the smaller total is the one taken.
222
- const coverage = new CoverageMap({ segmentCount: 200 });
223
- const runA = run({ head: 10, speedX: 1 });
224
- coverage.claim(runA, 0, 200);
225
- for (let at = 10; at < 30; at += 1) {
226
- coverage.markReady(at);
227
- }
228
- const actions = planEncoders({
229
- coverage,
230
- // The viewer is PAST the made stretch, so the only question is this encoder:
231
- // drive through twenty pieces that exist, or skip them. With a viewer at #0
232
- // as well, something has to cross that stretch whatever happens, and then
233
- // moving buys nothing — which the score says too, and is why the window
234
- // starts where the viewer actually is.
235
- windows: [{ from: 30, to: 190 }],
236
- runs: [runA],
237
- ...HOST,
238
- // One encoder, so the question is only about THIS one: drive through the
239
- // twenty pieces that exist, or skip them. With room for a second, the
240
- // machine simply buys one and the question never arises.
241
- maxRuns: 1,
242
- killCostSec: 0.5,
243
- firstByteWaitSec: 3,
244
- refetchSecPerFilmSecond: 0.25
245
- });
246
- const move = actions.find((action) => action.type === "move");
247
- assert.ok(move, "it is taken past the made film rather than left to make it again");
248
- assert.equal(move.from, 30, "to the first thing nobody has");
249
- });
250
-
251
- test("a short covered stretch is driven through rather than paid a restart for", () => {
252
- // One covered segment at 1x is 4 s of encoding plus 1 s of refetch, against a
253
- // move priced at 0.12 + 0.5 + 30 seconds on a host where the first bytes are
254
- // slow to come. The comparison, not a rule, decides it.
255
- const coverage = new CoverageMap({ segmentCount: 200 });
256
- const runA = run({ head: 10, speedX: 1 });
257
- coverage.claim(runA, 0, 200);
258
- coverage.markReady(10);
259
- const actions = planEncoders({
260
- coverage,
261
- // From where the run stands, so the only question is the covered piece under
262
- // it. A window starting at #0 would also be asking who makes #0..#9.
263
- windows: [{ from: 10, to: 190 }],
264
- runs: [runA],
265
- ...HOST,
266
- killCostSec: 0.5,
267
- firstByteWaitSec: 30,
268
- refetchSecPerFilmSecond: 0.25
269
- });
270
- assert.equal(actions.some((action) => action.type === "move"), false);
271
- assert.ok(actions.some((action) => action.type === "keep" && action.run === runA));
272
- });
273
-
274
- test("a run with nothing left ahead of it does not go on making nothing", () => {
275
- // Everything from #10 to the end exists. The encoder standing at #10 has
276
- // nothing to do there; whether it is stopped or taken back to the film before
277
- // #0 that nobody has made is the score's answer, and both are right answers.
278
- // What must not happen is that it stays where it is, producing nothing.
279
- const coverage = new CoverageMap({ segmentCount: 100 });
280
- const runA = run({ head: 10 });
281
- coverage.claim(runA, 0, 100);
282
- for (let index = 10; index < 100; index += 1) {
283
- coverage.markReady(index);
284
- }
285
- const actions = planEncoders({
286
- coverage,
287
- windows: [{ from: 0, to: 90 }],
288
- runs: [runA],
289
- ...HOST
290
- });
291
- const kept = actions.find((action) => action.type === "keep" && action.run === runA);
292
- assert.equal(kept, undefined, "it is not left standing on film that already exists");
293
- const madeAgain = actions.filter((action) => action.type !== "stop");
294
- for (const action of madeAgain) {
295
- assert.ok(action.from < 10, "and whatever is made is film nobody has");
296
- }
297
- });
298
-
299
- test("every encoder stops when nobody is watching the output", () => {
300
- // A look-ahead cannot answer this: it asks how far AHEAD of a viewer a run
301
- // is, and there is no viewer.
302
- const coverage = new CoverageMap({ segmentCount: 100 });
303
- const runA = run();
304
- const runB = run();
305
- const actions = planEncoders({
306
- coverage,
307
- windows: [],
308
- runs: [runA, runB],
309
- ...HOST
310
- });
311
- assert.deepEqual(
312
- actions.map((action) => [action.type, action.run]),
313
- [["stop", runA], ["stop", runB]]
314
- );
315
- });
316
-
317
- test("a run standing outside every window keeps working: the file is encoded whole", () => {
318
- // The rule, stated by the user 2026-09-05: while a file is being encoded it
319
- // is encoded whole, and a viewer decides the ORDER, not whether a run may
320
- // live. Stopping a run for standing outside a window is what produced the
321
- // field oscillation of that day — placed by one rule, killed by another,
322
- // 350-700ms per cycle, nothing ever produced.
323
- const coverage = new CoverageMap({ segmentCount: 1000 });
324
- const runA = run({ from: 500, to: 600, head: 520 });
325
- coverage.claim(runA, 500, 600);
326
- const actions = planEncoders({
327
- coverage,
328
- windows: [{ from: 0, to: 40 }],
329
- runs: [runA],
330
- ...HOST
331
- });
332
- assert.ok(
333
- !actions.some((action) => action.type === "stop" && action.run === runA),
334
- "it is making film that will be wanted, and nothing else is making it"
335
- );
336
- });
337
-
338
- test("the same plan run twice on an unchanged state gives the same answer", () => {
339
- // What the oscillation actually was: two passes over one state disagreeing
340
- // with each other. Nothing about the state changes between them here.
341
- const coverage = new CoverageMap({ segmentCount: 1000 });
342
- const runA = run({ from: 500, to: 600, head: 520 });
343
- coverage.claim(runA, 500, 600);
344
- const input = { coverage, windows: [{ from: 0, to: 40 }], runs: [runA], ...HOST };
345
-
346
- const first = planEncoders(input).map((action) => action.type);
347
- const second = planEncoders(input).map((action) => action.type);
348
-
349
- assert.deepEqual(first, second);
350
- assert.ok(!first.includes("stop"), "and neither pass kills what the other would start");
351
- });
352
-
353
- test("the one machine goes to whoever is due soonest, not to the smallest number", () => {
354
- // Urgency is a TIME in this model, so the test states one. The far zone is
355
- // lower in number and needed sooner; the order must come from the time.
356
- const coverage = new CoverageMap({ segmentCount: 1000 });
357
- const actions = planEncoders({
358
- coverage,
359
- windows: [
360
- { from: 0, to: 100, priority: 1, withinSeconds: 600 },
361
- { from: 500, to: 530, priority: 3, withinSeconds: 0 }
362
- ],
363
- // A run whose speed has been measured: how long an encoder takes to reach a
364
- // number is the whole comparison, and with nothing measured the model has no
365
- // ground to prefer one place over another and places once.
366
- runs: [run({ from: 900, to: 999, head: 900 })],
367
- ...HOST,
368
- maxRuns: 2
369
- });
370
- // WHERE the machine ends up, not how it got there. A run standing at #900
371
- // reaches nothing anybody wants, so it is taken to #500 rather than killed and
372
- // replaced — one process instead of a death and a cold start, which is
373
- // strictly better and is what the plan now answers.
374
- const placed = actions
375
- .filter((action) => action.type === "start" || action.type === "move")
376
- .map((action) => action.from);
377
-
378
- assert.deepEqual(placed, [500], "the one machine goes where somebody is stopped");
379
- });
380
-
381
- test("two viewers far apart are both served, by however many encoders serve them soonest", () => {
382
- // Two encoders, or one that goes to whichever of them is worse off: the answer
383
- // is what a second process costs this machine, which is measured, and the
384
- // score works it out. What must hold is that neither of them is simply left.
385
- const coverage = new CoverageMap({ segmentCount: 1000 });
386
- const actions = planEncoders({
387
- coverage,
388
- windows: [
389
- { from: 0, to: 30, withinSeconds: 0 },
390
- { from: 800, to: 830, withinSeconds: 0 }
391
- ],
392
- runs: [run({ from: 900, to: 999, head: 900 })],
393
- ...HOST,
394
- maxRuns: 3
395
- });
396
- const spans = actions
397
- .filter((action) => action.type !== "stop")
398
- .map((action) => [action.from, action.to < action.from ? Number.POSITIVE_INFINITY : action.to]);
399
- assert.ok(spans.some(([from, to]) => from <= 0 && to >= 0), "the one at the beginning is served");
400
- for (let index = 0; index < spans.length - 1; index += 1) {
401
- const sorted = [...spans].sort((left, right) => left[0] - right[0]);
402
- assert.ok(sorted[index][1] < sorted[index + 1][0], "and no two encoders share a number");
403
- }
404
- });
405
-
406
- test("a machine that can hold one gives it to the viewer who is stopped soonest", () => {
407
- // The bound is the host's own budget, the same arithmetic that decides the
408
- // quality offer. Measured on the addon host 2026-09-03: at 1080p one encode
409
- // saturates the machine and two land on realtime.
410
- const coverage = new CoverageMap({ segmentCount: 1000 });
411
- const actions = planEncoders({
412
- coverage,
413
- windows: [{ from: 800, to: 830 }, { from: 0, to: 30 }],
414
- runs: [],
415
- ...HOST,
416
- maxRuns: 1
417
- });
418
- const started = actions.filter((action) => action.type === "start").map((action) => action.from);
419
- assert.deepEqual(started, [0]);
420
- });
421
-
422
- test("a viewer joining behind a running encoder gets their own, not a dragged one", () => {
423
- // This is what the whole layer is for. The run in front is untouched; the
424
- // viewer behind is not made to wait for it to be pulled back.
425
- const coverage = new CoverageMap({ segmentCount: 1000 });
426
- const runA = run({ from: 500, to: 600, head: 510 });
427
- coverage.claim(runA, 500, 600);
428
- // A head at #510 means #500..#509 are already made — that is what a head is.
429
- // Left unsaid, the map believes a viewer is waiting on ten pieces nobody has,
430
- // and moving the encoder back to make them is then the right answer.
431
- for (let index = 500; index < 510; index += 1) {
432
- coverage.markReady(index);
433
- }
434
- const actions = planEncoders({
435
- coverage,
436
- windows: [{ from: 500, to: 530 }, { from: 100, to: 130 }],
437
- runs: [runA],
438
- ...HOST
439
- });
440
- assert.ok(actions.some((action) => action.type === "keep" && action.run === runA));
441
- assert.equal(actions.some((action) => action.type === "move"), false);
442
- const started = actions.filter((action) => action.type === "start");
443
- assert.equal(started.length, 1);
444
- assert.equal(started[0].from, 100);
445
- });
446
-
447
- test("the lowest thing a viewer is waiting for is reported, so a stalled plan is visible", () => {
448
- const coverage = new CoverageMap({ segmentCount: 100 });
449
- coverage.setReady([40, 41]);
450
- assert.equal(firstUnmetWant(coverage, [{ from: 40, to: 70 }]), 42);
451
- coverage.setReady([40, 41, 42, 43, 44]);
452
- assert.equal(firstUnmetWant(coverage, [{ from: 40, to: 44 }]), null);
453
- });
454
-
455
- test("a run with no end is making what the viewers ahead of it are waiting for", () => {
456
- // Field, 2026-09-05: an encoder was started and killed every five seconds,
457
- // each producing 0-2 segments, for as long as anybody watched. A run given no
458
- // end carries `to = -1`, and two places read that as a number instead of as
459
- // "no end": the overlap test called its work unwanted, and the claim it made
460
- // in the coverage map was one segment long, so the plan saw the rest of the
461
- // film as free and started another encoder there.
462
- const coverage = new CoverageMap({ segmentCount: 570 });
463
- const run = { from: 0, to: -1, head: 3, speedX: 8, isAlive: true };
464
- coverage.claim(run, run.from, endOfRun(run));
465
-
466
- const actions = planEncoders({
467
- coverage,
468
- live: [run],
469
- wanted: [{ from: 0, to: 30 }],
470
- maxRuns: 2,
471
- segmentSeconds: 4,
472
- ...RUN_COSTS
473
- });
474
-
475
- assert.deepEqual(
476
- actions.filter((action) => action.type === "stop"),
477
- [],
478
- "nothing is stopped: it is making exactly what is wanted"
479
- );
480
- assert.deepEqual(
481
- actions.filter((action) => action.type === "start"),
482
- [],
483
- "and nothing new is started over ground it already holds"
484
- );
485
- });
486
-
487
- test("spare budget IS spent on the rest of the film, once nobody is waiting", () => {
488
- // The user's own correction: film nobody is waiting for still has value,
489
- // because a viewer seeking back into a part that exists starts playing at
490
- // once. So what the machine has spare goes to finishing the file.
491
- //
492
- // "Once nobody is waiting" is not a condition written anywhere — it falls out
493
- // of the score. A second process costs the first the measured share of the
494
- // machine, so while somebody is stopped on a piece, adding one delays that
495
- // piece and the first term refuses it. Here the near film is already made,
496
- // nothing is late in either arrangement, and the two that finish the rest
497
- // sooner win.
498
- const coverage = new CoverageMap({ segmentCount: 400 });
499
- for (let index = 0; index <= 9; index += 1) {
500
- coverage.markReady(index);
501
- }
502
- const actions = planEncoders({
503
- coverage,
504
- windows: [
505
- { from: 0, to: 9, priority: 32, withinSeconds: 0 },
506
- { from: 10, to: 399, priority: 20, withinSeconds: 40 }
507
- ],
508
- runs: [],
509
- ...HOST,
510
- maxRuns: 4
511
- });
512
- const started = actions.filter((action) => action.type === "start");
513
-
514
- assert.ok(started.length > 1, "the machine does not stand idle while film is unmade");
515
- const spans = started
516
- .map((action) => [action.from, action.to])
517
- .sort((left, right) => left[0] - right[0]);
518
- for (let index = 0; index < spans.length - 1; index += 1) {
519
- assert.ok(spans[index][1] < spans[index + 1][0], "and no two of them share a number");
520
- }
521
- });
522
-
523
-
524
- test("two encoders never share a segment number", () => {
525
- // The whole of what went wrong in the field: two encoders writing one name.
526
- const coverage = new CoverageMap({ segmentCount: 400 });
527
- const actions = planEncoders({
528
- coverage,
529
- windows: [{ from: 0, to: 399, priority: 32 }],
530
- runs: [],
531
- ...HOST,
532
- maxRuns: 4
533
- });
534
- const spans = actions
535
- .filter((action) => action.type === "start")
536
- .map((action) => ({ from: action.from, to: action.to }))
537
- .sort((left, right) => left.from - right.from);
538
-
539
- for (let index = 0; index < spans.length - 1; index += 1) {
540
- assert.ok(
541
- spans[index].to < spans[index + 1].from,
542
- `#${spans[index].from}..#${spans[index].to} overlaps #${spans[index + 1].from}`
543
- );
544
- }
545
- });