@torrent-tv/proxy 2.80.8 → 2.80.10

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 (34) hide show
  1. package/CHANGELOG.md +1675 -1658
  2. package/bin/cli.js +606 -596
  3. package/package.json +51 -50
  4. package/services/data-channel-handler.js +2 -4
  5. package/services/encode/CoverageMap.js +428 -401
  6. package/services/encode/EncodePlan.js +0 -3
  7. package/services/encode/EncodeRun.js +14 -0
  8. package/services/encode/SegmentStore.js +718 -669
  9. package/services/hls-session-manager.js +44 -162
  10. package/services/hwaccel.js +2 -2
  11. package/services/memory-report.js +596 -592
  12. package/services/orchestrators/EncodeOrchestrator.js +143 -11
  13. package/services/quality/EncodeCost.js +555 -555
  14. package/test/auto-quality-step.test.js +514 -507
  15. package/test/contention.test.js +1 -1
  16. package/test/coverage-follows-the-disk.test.js +187 -0
  17. package/test/coverage-map.test.js +195 -178
  18. package/test/encode-orchestrator.test.js +1 -2
  19. package/test/encode-plan-viewers.test.js +18 -19
  20. package/test/encode-plan.test.js +3 -4
  21. package/test/held-request-width.test.js +155 -154
  22. package/test/helpers/encode-run.js +136 -128
  23. package/test/orchestrator-wired.test.js +16 -11
  24. package/test/produced-copy-choice.test.js +358 -327
  25. package/test/run-intervals.test.js +100 -100
  26. package/test/segment-serve-wiring.test.js +76 -20
  27. package/test/segment-store.test.js +216 -187
  28. package/test/stale-request-after-seek.test.js +51 -77
  29. package/test/tracks-begin-together.test.js +176 -153
  30. package/test/viewer-outputs.test.js +278 -273
  31. package/test/behind-head-repair.test.js +0 -261
  32. /package/services/{contention.js → encode/contention.js} +0 -0
  33. /package/{test/decode-cost.test.js → test-measured/decode-cost.measured.js} +0 -0
  34. /package/{test/decode-measurement.test.js → test-measured/decode-measurement.measured.js} +0 -0
@@ -1,401 +1,428 @@
1
- /**
2
- * @file What has been made of one output, what is being made right now, and by
3
- * whom.
4
- *
5
- * One map per set of output parameters — never per session, never per viewer.
6
- * Every segment number is in exactly one of three states:
7
- *
8
- * 1. **ready** — a file that is closed and can be served to anybody;
9
- * 2. **being made** — claimed by a named live run, which has been given that
10
- * stretch and is working forward through it;
11
- * 3. **free** — nobody has made it and nobody is making it.
12
- *
13
- * Two questions are asked of it constantly and both have to be cheap, because
14
- * one of them is on the path that answers a viewer:
15
- *
16
- * 1. is segment N ready — a set lookup;
17
- * 2. where is the first gap at or after N — a walk over numbers, never over the
18
- * disk. The walk it replaces listed every run directory of a session on the
19
- * thread carrying the data channel, 1350 files for a 90-minute film, on
20
- * every segment request.
21
- *
22
- * **Claims are intervals, not heads.** A run says which stretch it was given,
23
- * so two runs on one output cannot be sent to the same numbers: the gap finder
24
- * skips what another run will reach. A run that only announced its current
25
- * position would leave the question "will anybody make #400" unanswerable
26
- * without guessing at its speed.
27
- *
28
- * **Nothing here touches a disk, a process or a clock**, so every decision it
29
- * makes can be exercised with numbers alone.
30
- */
31
-
32
- /** @typedef {"ready" | "making" | "free"} SegmentState */
33
-
34
- export class CoverageMap {
35
- /** Numbers whose file is closed and servable. @type {Set<number>} */
36
- #ready = new Set();
37
-
38
- /** Run id → the stretch it was given, both ends inclusive. @type {Map<string, {from: number, to: number}>} */
39
- #claims = new Map();
40
-
41
- /** How many segments this output has in total. @type {number} */
42
- #segmentCount;
43
-
44
- /**
45
- * @param {object} [params]
46
- * @param {number} [params.segmentCount=0] - The length of the output, in
47
- * segments. Zero means it is not known yet, and then a gap search has to be
48
- * given its own bound by the caller.
49
- */
50
- constructor({ segmentCount = 0 } = {}) {
51
- this.#segmentCount = Number.isInteger(segmentCount) && segmentCount > 0 ? segmentCount : 0;
52
- }
53
-
54
- /**
55
- * The length of the output, once the playlist is known.
56
- *
57
- * @param {number} count
58
- */
59
- setSegmentCount(count) {
60
- if (Number.isInteger(count) && count > 0) {
61
- this.#segmentCount = count;
62
- }
63
- }
64
-
65
- /** @returns {number} */
66
- get segmentCount() {
67
- return this.#segmentCount;
68
- }
69
-
70
- /**
71
- * Record that a segment is closed and can be served.
72
- *
73
- * Idempotent, and deliberately independent of who made it: a segment made by
74
- * a run that has since died is as good as one made by a run still going, and
75
- * a segment left by a previous life of this process is as good as either.
76
- *
77
- * @param {number} index
78
- */
79
- markReady(index) {
80
- if (Number.isInteger(index) && index >= 0) {
81
- this.#ready.add(index);
82
- }
83
- }
84
-
85
- /**
86
- * @param {Iterable<number>} indexes
87
- */
88
- markReadyAll(indexes) {
89
- for (const index of indexes) {
90
- this.markReady(index);
91
- }
92
- }
93
-
94
- /**
95
- * Forget a segment: its file has gone, or was never closed.
96
- *
97
- * @param {number} index
98
- */
99
- markGone(index) {
100
- this.#ready.delete(index);
101
- }
102
-
103
- /**
104
- * A run has been given a stretch to fill.
105
- *
106
- * Replaces whatever that run claimed before, because a run has one stretch at
107
- * a time: moved forward past ready material, it states the new one.
108
- *
109
- * @param {object} run - The run itself. A run has no name and needs none:
110
- * what identifies it here is that it IS itself, and what identifies it in a
111
- * log line is the stretch it was given, which no other live run of this
112
- * output can hold.
113
- * @param {number} from - First segment number, inclusive.
114
- * @param {number} to - Last segment number, inclusive. May be
115
- * `Number.POSITIVE_INFINITY` for a run with no end yet, which is what every
116
- * run was before ends existed.
117
- */
118
- claim(run, from, to) {
119
- if (!run || !Number.isInteger(from) || from < 0) {
120
- return;
121
- }
122
- const end = Number.isFinite(to) ? Math.max(from, Math.trunc(to)) : Number.POSITIVE_INFINITY;
123
- this.#claims.set(run, { from, to: end });
124
- }
125
-
126
- /**
127
- * A run has ended. Whatever it did not finish goes back to free.
128
- *
129
- * Nothing is un-marked: what it DID finish stays ready, because a closed file
130
- * is closed whoever made it and whatever became of them afterwards.
131
- *
132
- * @param {object} run
133
- */
134
- release(run) {
135
- this.#claims.delete(run);
136
- }
137
-
138
- /**
139
- * How many numbers between two are already made.
140
- *
141
- * What an encoder driving from one to the other would produce a SECOND time,
142
- * and therefore what the swarm would be asked to fetch a second time. It is a
143
- * term of when that encoder arrives, not a separate question about whether to
144
- * move it: an encoder that has to re-make three hundred pieces on its way is
145
- * simply slower to get there, and the model compares arrivals.
146
- *
147
- * @param {number} from - Inclusive.
148
- * @param {number} to - Inclusive.
149
- * @returns {number}
150
- */
151
- madeBetween(from, to) {
152
- const first = Number.isInteger(from) && from > 0 ? from : 0;
153
- const last = Number.isInteger(to) ? to : -1;
154
- let count = 0;
155
- for (let index = first; index <= last; index += 1) {
156
- if (this.isReady(index)) {
157
- count += 1;
158
- }
159
- }
160
- return count;
161
- }
162
-
163
- /**
164
- * @param {number} index
165
- * @returns {boolean}
166
- */
167
- isReady(index) {
168
- return this.#ready.has(index);
169
- }
170
-
171
- /**
172
- * The run that was given this number, if any.
173
- *
174
- * @param {number} index
175
- * @returns {object | null}
176
- */
177
- makerOf(index) {
178
- for (const [run, span] of this.#claims) {
179
- if (index >= span.from && index <= span.to) {
180
- return run;
181
- }
182
- }
183
- return null;
184
- }
185
-
186
- /**
187
- * @param {number} index
188
- * @returns {SegmentState}
189
- */
190
- stateOf(index) {
191
- if (this.#ready.has(index)) {
192
- return "ready";
193
- }
194
- return this.makerOf(index) === null ? "free" : "making";
195
- }
196
-
197
- /**
198
- * The first number at or after `index` that nobody has made and nobody is
199
- * making where a new run belongs.
200
- *
201
- * @param {number} index
202
- * @param {number} [bound] - Search no further than this number, inclusive.
203
- * Defaults to the last segment of the output; required while the length is
204
- * unknown.
205
- * @param {object} [exceptRun] - The run asking. Its own claim does not make
206
- * a number taken as far as it is concerned: a run looking for where to move
207
- * would otherwise be blocked by the very stretch it is trying to leave, and
208
- * a run that had claimed the rest of the film could never move at all.
209
- * @returns {number | null} Null when there is no gap in range, which is what
210
- * "everything ahead is already covered" looks like.
211
- */
212
- firstGapFrom(index, bound = undefined, exceptRun = null) {
213
- const start = Number.isInteger(index) && index > 0 ? index : 0;
214
- const last = Number.isInteger(bound) ? bound : this.#segmentCount - 1;
215
- if (!Number.isInteger(last) || last < start) {
216
- return null;
217
- }
218
- for (let at = start; at <= last; at += 1) {
219
- if (this.#ready.has(at)) {
220
- continue;
221
- }
222
- const maker = this.makerOf(at);
223
- if (maker === null || CoverageMap.#isExcepted(maker, exceptRun)) {
224
- return at;
225
- }
226
- }
227
- return null;
228
- }
229
-
230
- /**
231
- * How many numbers from `index` onwards are already covered — ready, or
232
- * claimed by a run other than `exceptRun`.
233
- *
234
- * This is what prices a decision: a run that has arrived at covered material
235
- * either drives through it, paying its own encode time for every one of these
236
- * numbers, or is moved to the gap beyond them, paying one restart. Both terms
237
- * are measured elsewhere; this is the length.
238
- *
239
- * @param {number} index
240
- * @param {object} [exceptRun] - The run asking. Its own claim does not
241
- * count as somebody else's coverage.
242
- * @returns {number}
243
- */
244
- coveredRunFrom(index, exceptRun = null) {
245
- const start = Number.isInteger(index) && index > 0 ? index : 0;
246
- const last = this.#segmentCount > 0 ? this.#segmentCount - 1 : Number.MAX_SAFE_INTEGER;
247
- let at = start;
248
- while (at <= last) {
249
- if (this.#ready.has(at)) {
250
- at += 1;
251
- continue;
252
- }
253
- const maker = this.makerOf(at);
254
- if (maker !== null && !CoverageMap.#isExcepted(maker, exceptRun)) {
255
- at += 1;
256
- continue;
257
- }
258
- break;
259
- }
260
- return at - start;
261
- }
262
-
263
- /**
264
- * How many numbers from `index` onward have NOT been made, claims ignored.
265
- *
266
- * The stretch an encoder placed here could work through before it would be
267
- * re-making something that exists. Claims are deliberately left out: a claim
268
- * says another encoder MEANS to make it, and where two encoders are being
269
- * placed in one pass the claims of the moment are about to be re-cut — so
270
- * asking about them here gives an answer that was true a step ago. Where one
271
- * encoder's road ends because another begins is decided once, over all the
272
- * placements together, after they are known.
273
- *
274
- * @param {number} index
275
- * @returns {number} Zero when `index` is already made; the rest of the track
276
- * when nothing ahead is; `Infinity` when the length is not yet known.
277
- */
278
- unmadeRunFrom(index) {
279
- const start = Number.isInteger(index) && index > 0 ? index : 0;
280
- if (this.isReady(start)) {
281
- return 0;
282
- }
283
- if (this.#segmentCount <= 0) {
284
- return Number.POSITIVE_INFINITY;
285
- }
286
- let end = start;
287
- while (end < this.#segmentCount && !this.isReady(end)) {
288
- end += 1;
289
- }
290
- return end - start;
291
- }
292
-
293
- /**
294
- * How many numbers from `index` onwards are free nobody has made them and
295
- * nobody is making them.
296
- *
297
- * This is what gives a run its END. A run handed the whole rest of the film
298
- * would drive straight through the next stretch somebody else is making; a
299
- * run handed exactly the free stretch stops where the covered material
300
- * begins, which is also where it would have been moved to anyway.
301
- *
302
- * @param {number} index
303
- * @param {object} [exceptRun] - The run asking, whose own claim does not
304
- * make a number unfree for it.
305
- * @returns {number} Zero when `index` itself is not free.
306
- */
307
- freeRunFrom(index, exceptRun = null) {
308
- const start = Number.isInteger(index) && index > 0 ? index : 0;
309
- if (this.#segmentCount <= 0) {
310
- // The length is not known, so how far the free stretch reaches is not
311
- // known either, and the honest answer is "as far as there is film" — the
312
- // caller turns that into a run with no end.
313
- //
314
- // Never walked one number at a time to find that out. It used to be, up to
315
- // MAX_SAFE_INTEGER, with a scan of every claim at each step: on the addon
316
- // host, 2026-09-05, the main thread spun at 100% from the look-ahead timer
317
- // and the proxy stopped answering anything at all, its own log included.
318
- // The length was missing because the field naming it had moved and three
319
- // readers were left on the old name — but a walk whose end depends on a
320
- // field being present must not be able to do this even then.
321
- const covered = this.#firstCoveredFrom(start, exceptRun);
322
- return covered === null ? Number.POSITIVE_INFINITY : covered - start;
323
- }
324
- const last = this.#segmentCount - 1;
325
- let at = start;
326
- while (at <= last) {
327
- if (this.#ready.has(at)) {
328
- break;
329
- }
330
- const maker = this.makerOf(at);
331
- if (maker !== null && !CoverageMap.#isExcepted(maker, exceptRun)) {
332
- break;
333
- }
334
- at += 1;
335
- }
336
- return at - start;
337
- }
338
-
339
- /**
340
- * The first number at or after `index` that somebody has made or is making,
341
- * or null when nobody has touched anything from there on.
342
- *
343
- * Asked of what the map HOLDS rather than by walking the numbers, so it can be
344
- * answered without a length: the map knows every ready number and every claim,
345
- * and both are finite however long the film is.
346
- *
347
- * @param {number} index
348
- * @param {object | null} exceptRun
349
- * @returns {number | null}
350
- */
351
- /**
352
- * Is this claim one of the runs the caller is setting aside?
353
- *
354
- * A single run or a set of them: a pass that re-cuts several roads at once has
355
- * to ask about all of them together, and asking once per run gave an answer
356
- * true of no moment.
357
- *
358
- * @param {object} maker
359
- * @param {object | Set<object> | null} except
360
- * @returns {boolean}
361
- */
362
- static #isExcepted(maker, except) {
363
- if (except === null || except === undefined) {
364
- return false;
365
- }
366
- return except instanceof Set ? except.has(maker) : maker === except;
367
- }
368
-
369
- #firstCoveredFrom(index, exceptRun) {
370
- let lowest = null;
371
- for (const ready of this.#ready) {
372
- if (ready >= index && (lowest === null || ready < lowest)) {
373
- lowest = ready;
374
- }
375
- }
376
- for (const [run, span] of this.#claims) {
377
- if (CoverageMap.#isExcepted(run, exceptRun)) {
378
- continue;
379
- }
380
- // A claim that has already begun covers `index` itself.
381
- const covers = span.from <= index && index <= span.to ? index : span.from;
382
- if (covers >= index && (lowest === null || covers < lowest)) {
383
- lowest = covers;
384
- }
385
- }
386
- return lowest;
387
- }
388
-
389
- /**
390
- * What this map holds, for a log line.
391
- *
392
- * @returns {{ ready: number, claims: number, segmentCount: number }}
393
- */
394
- stats() {
395
- return {
396
- ready: this.#ready.size,
397
- claims: this.#claims.size,
398
- segmentCount: this.#segmentCount
399
- };
400
- }
401
- }
1
+ /**
2
+ * @file What has been made of one output, what is being made right now, and by
3
+ * whom.
4
+ *
5
+ * One map per set of output parameters — never per session, never per viewer.
6
+ * Every segment number is in exactly one of three states:
7
+ *
8
+ * 1. **ready** — a file that is closed and can be served to anybody;
9
+ * 2. **being made** — claimed by a named live run, which has been given that
10
+ * stretch and is working forward through it;
11
+ * 3. **free** — nobody has made it and nobody is making it.
12
+ *
13
+ * Two questions are asked of it constantly and both have to be cheap, because
14
+ * one of them is on the path that answers a viewer:
15
+ *
16
+ * 1. is segment N ready — a set lookup;
17
+ * 2. where is the first gap at or after N — a walk over numbers, never over the
18
+ * disk. The walk it replaces listed every run directory of a session on the
19
+ * thread carrying the data channel, 1350 files for a 90-minute film, on
20
+ * every segment request.
21
+ *
22
+ * **READINESS IS A PROJECTION, NEVER A MEMORY.** What is ready is a fact of the
23
+ * disk, and the disk has one owner the segment store. This map does not
24
+ * remember what it was once told: {@link CoverageMap#setReady} REPLACES the
25
+ * whole picture, so every number it calls ready was placed there by that one
26
+ * authority, whole, immediately before the answer was used.
27
+ *
28
+ * It used to accumulate. `markReadyAll` added and nothing ever took away the
29
+ * word for taking away existed and was called from no line of the product — so
30
+ * a number stayed ready for the life of the process after its file had been
31
+ * discarded, dropped for room, or overwritten by a run restarting on it. Field
32
+ * 2026-09-07: this map said 482 of 482 segments were made while the directory
33
+ * held nothing a header could be lifted out of; the plan therefore scored every
34
+ * arrangement as equally perfect, took the one encoder away as unnecessary — its
35
+ * own words, "the film is no worse off without it" — and placed none for the
36
+ * rest of the session. Two sessions in a row ended with the viewer looking at an
37
+ * error card, and the second one never received a single byte.
38
+ *
39
+ * The remedy is not a way to un-mark. A second owner that is kept in step can
40
+ * fall out of step again; a projection cannot.
41
+ *
42
+ * **Claims are intervals, not heads.** A run says which stretch it was given,
43
+ * so two runs on one output cannot be sent to the same numbers: the gap finder
44
+ * skips what another run will reach. A run that only announced its current
45
+ * position would leave the question "will anybody make #400" unanswerable
46
+ * without guessing at its speed.
47
+ *
48
+ * **Nothing here touches a disk, a process or a clock**, so every decision it
49
+ * makes can be exercised with numbers alone.
50
+ */
51
+
52
+ /** @typedef {"ready" | "making" | "free"} SegmentState */
53
+
54
+ export class CoverageMap {
55
+ /** Numbers whose file is closed and servable. @type {Set<number>} */
56
+ #ready = new Set();
57
+
58
+ /** Run id → the stretch it was given, both ends inclusive. @type {Map<string, {from: number, to: number}>} */
59
+ #claims = new Map();
60
+
61
+ /** How many segments this output has in total. @type {number} */
62
+ #segmentCount;
63
+
64
+ /**
65
+ * @param {object} [params]
66
+ * @param {number} [params.segmentCount=0] - The length of the output, in
67
+ * segments. Zero means it is not known yet, and then a gap search has to be
68
+ * given its own bound by the caller.
69
+ */
70
+ constructor({ segmentCount = 0 } = {}) {
71
+ this.#segmentCount = Number.isInteger(segmentCount) && segmentCount > 0 ? segmentCount : 0;
72
+ }
73
+
74
+ /**
75
+ * The length of the output, once the playlist is known.
76
+ *
77
+ * @param {number} count
78
+ */
79
+ setSegmentCount(count) {
80
+ if (Number.isInteger(count) && count > 0) {
81
+ this.#segmentCount = count;
82
+ }
83
+ }
84
+
85
+ /** @returns {number} */
86
+ get segmentCount() {
87
+ return this.#segmentCount;
88
+ }
89
+
90
+ /**
91
+ * Record that a segment is closed and can be served.
92
+ *
93
+ * Idempotent, and deliberately independent of who made it: a segment made by
94
+ * a run that has since died is as good as one made by a run still going, and
95
+ * a segment left by a previous life of this process is as good as either.
96
+ *
97
+ * @param {number} index
98
+ */
99
+ markReady(index) {
100
+ if (Number.isInteger(index) && index >= 0) {
101
+ this.#ready.add(index);
102
+ }
103
+ }
104
+
105
+ /**
106
+ * State the WHOLE picture of what is ready, replacing whatever was here.
107
+ *
108
+ * The only way readiness enters this map in the product, and the reason it
109
+ * cannot drift: a number absent from `indexes` is not ready, whatever this map
110
+ * was told a moment ago. Its file may have been discarded with the run that
111
+ * had it open, dropped to make room, or reopened by a run restarting on it —
112
+ * none of which this map can see, and none of which it now has to.
113
+ *
114
+ * There is deliberately no way to take one number back. A retraction is a
115
+ * second owner keeping a copy in step, and a copy kept in step is what this
116
+ * replaced.
117
+ *
118
+ * @param {Iterable<number>} indexes
119
+ */
120
+ setReady(indexes) {
121
+ const stated = new Set();
122
+ for (const index of indexes) {
123
+ if (Number.isInteger(index) && index >= 0) {
124
+ stated.add(index);
125
+ }
126
+ }
127
+ this.#ready = stated;
128
+ }
129
+
130
+ /**
131
+ * A run has been given a stretch to fill.
132
+ *
133
+ * Replaces whatever that run claimed before, because a run has one stretch at
134
+ * a time: moved forward past ready material, it states the new one.
135
+ *
136
+ * @param {object} run - The run itself. A run has no name and needs none:
137
+ * what identifies it here is that it IS itself, and what identifies it in a
138
+ * log line is the stretch it was given, which no other live run of this
139
+ * output can hold.
140
+ * @param {number} from - First segment number, inclusive.
141
+ * @param {number} to - Last segment number, inclusive. May be
142
+ * `Number.POSITIVE_INFINITY` for a run with no end yet, which is what every
143
+ * run was before ends existed.
144
+ */
145
+ claim(run, from, to) {
146
+ if (!run || !Number.isInteger(from) || from < 0) {
147
+ return;
148
+ }
149
+ const end = Number.isFinite(to) ? Math.max(from, Math.trunc(to)) : Number.POSITIVE_INFINITY;
150
+ this.#claims.set(run, { from, to: end });
151
+ }
152
+
153
+ /**
154
+ * A run has ended. Whatever it did not finish goes back to free.
155
+ *
156
+ * Nothing is un-marked: what it DID finish stays ready, because a closed file
157
+ * is closed whoever made it and whatever became of them afterwards.
158
+ *
159
+ * @param {object} run
160
+ */
161
+ release(run) {
162
+ this.#claims.delete(run);
163
+ }
164
+
165
+ /**
166
+ * How many numbers between two are already made.
167
+ *
168
+ * What an encoder driving from one to the other would produce a SECOND time,
169
+ * and therefore what the swarm would be asked to fetch a second time. It is a
170
+ * term of when that encoder arrives, not a separate question about whether to
171
+ * move it: an encoder that has to re-make three hundred pieces on its way is
172
+ * simply slower to get there, and the model compares arrivals.
173
+ *
174
+ * @param {number} from - Inclusive.
175
+ * @param {number} to - Inclusive.
176
+ * @returns {number}
177
+ */
178
+ madeBetween(from, to) {
179
+ const first = Number.isInteger(from) && from > 0 ? from : 0;
180
+ const last = Number.isInteger(to) ? to : -1;
181
+ let count = 0;
182
+ for (let index = first; index <= last; index += 1) {
183
+ if (this.isReady(index)) {
184
+ count += 1;
185
+ }
186
+ }
187
+ return count;
188
+ }
189
+
190
+ /**
191
+ * @param {number} index
192
+ * @returns {boolean}
193
+ */
194
+ isReady(index) {
195
+ return this.#ready.has(index);
196
+ }
197
+
198
+ /**
199
+ * The run that was given this number, if any.
200
+ *
201
+ * @param {number} index
202
+ * @returns {object | null}
203
+ */
204
+ makerOf(index) {
205
+ for (const [run, span] of this.#claims) {
206
+ if (index >= span.from && index <= span.to) {
207
+ return run;
208
+ }
209
+ }
210
+ return null;
211
+ }
212
+
213
+ /**
214
+ * @param {number} index
215
+ * @returns {SegmentState}
216
+ */
217
+ stateOf(index) {
218
+ if (this.#ready.has(index)) {
219
+ return "ready";
220
+ }
221
+ return this.makerOf(index) === null ? "free" : "making";
222
+ }
223
+
224
+ /**
225
+ * The first number at or after `index` that nobody has made and nobody is
226
+ * making — where a new run belongs.
227
+ *
228
+ * @param {number} index
229
+ * @param {number} [bound] - Search no further than this number, inclusive.
230
+ * Defaults to the last segment of the output; required while the length is
231
+ * unknown.
232
+ * @param {object} [exceptRun] - The run asking. Its own claim does not make
233
+ * a number taken as far as it is concerned: a run looking for where to move
234
+ * would otherwise be blocked by the very stretch it is trying to leave, and
235
+ * a run that had claimed the rest of the film could never move at all.
236
+ * @returns {number | null} Null when there is no gap in range, which is what
237
+ * "everything ahead is already covered" looks like.
238
+ */
239
+ firstGapFrom(index, bound = undefined, exceptRun = null) {
240
+ const start = Number.isInteger(index) && index > 0 ? index : 0;
241
+ const last = Number.isInteger(bound) ? bound : this.#segmentCount - 1;
242
+ if (!Number.isInteger(last) || last < start) {
243
+ return null;
244
+ }
245
+ for (let at = start; at <= last; at += 1) {
246
+ if (this.#ready.has(at)) {
247
+ continue;
248
+ }
249
+ const maker = this.makerOf(at);
250
+ if (maker === null || CoverageMap.#isExcepted(maker, exceptRun)) {
251
+ return at;
252
+ }
253
+ }
254
+ return null;
255
+ }
256
+
257
+ /**
258
+ * How many numbers from `index` onwards are already covered — ready, or
259
+ * claimed by a run other than `exceptRun`.
260
+ *
261
+ * This is what prices a decision: a run that has arrived at covered material
262
+ * either drives through it, paying its own encode time for every one of these
263
+ * numbers, or is moved to the gap beyond them, paying one restart. Both terms
264
+ * are measured elsewhere; this is the length.
265
+ *
266
+ * @param {number} index
267
+ * @param {object} [exceptRun] - The run asking. Its own claim does not
268
+ * count as somebody else's coverage.
269
+ * @returns {number}
270
+ */
271
+ coveredRunFrom(index, exceptRun = null) {
272
+ const start = Number.isInteger(index) && index > 0 ? index : 0;
273
+ const last = this.#segmentCount > 0 ? this.#segmentCount - 1 : Number.MAX_SAFE_INTEGER;
274
+ let at = start;
275
+ while (at <= last) {
276
+ if (this.#ready.has(at)) {
277
+ at += 1;
278
+ continue;
279
+ }
280
+ const maker = this.makerOf(at);
281
+ if (maker !== null && !CoverageMap.#isExcepted(maker, exceptRun)) {
282
+ at += 1;
283
+ continue;
284
+ }
285
+ break;
286
+ }
287
+ return at - start;
288
+ }
289
+
290
+ /**
291
+ * How many numbers from `index` onward have NOT been made, claims ignored.
292
+ *
293
+ * The stretch an encoder placed here could work through before it would be
294
+ * re-making something that exists. Claims are deliberately left out: a claim
295
+ * says another encoder MEANS to make it, and where two encoders are being
296
+ * placed in one pass the claims of the moment are about to be re-cut — so
297
+ * asking about them here gives an answer that was true a step ago. Where one
298
+ * encoder's road ends because another begins is decided once, over all the
299
+ * placements together, after they are known.
300
+ *
301
+ * @param {number} index
302
+ * @returns {number} Zero when `index` is already made; the rest of the track
303
+ * when nothing ahead is; `Infinity` when the length is not yet known.
304
+ */
305
+ unmadeRunFrom(index) {
306
+ const start = Number.isInteger(index) && index > 0 ? index : 0;
307
+ if (this.isReady(start)) {
308
+ return 0;
309
+ }
310
+ if (this.#segmentCount <= 0) {
311
+ return Number.POSITIVE_INFINITY;
312
+ }
313
+ let end = start;
314
+ while (end < this.#segmentCount && !this.isReady(end)) {
315
+ end += 1;
316
+ }
317
+ return end - start;
318
+ }
319
+
320
+ /**
321
+ * How many numbers from `index` onwards are free — nobody has made them and
322
+ * nobody is making them.
323
+ *
324
+ * This is what gives a run its END. A run handed the whole rest of the film
325
+ * would drive straight through the next stretch somebody else is making; a
326
+ * run handed exactly the free stretch stops where the covered material
327
+ * begins, which is also where it would have been moved to anyway.
328
+ *
329
+ * @param {number} index
330
+ * @param {object} [exceptRun] - The run asking, whose own claim does not
331
+ * make a number unfree for it.
332
+ * @returns {number} Zero when `index` itself is not free.
333
+ */
334
+ freeRunFrom(index, exceptRun = null) {
335
+ const start = Number.isInteger(index) && index > 0 ? index : 0;
336
+ if (this.#segmentCount <= 0) {
337
+ // The length is not known, so how far the free stretch reaches is not
338
+ // known either, and the honest answer is "as far as there is film" — the
339
+ // caller turns that into a run with no end.
340
+ //
341
+ // Never walked one number at a time to find that out. It used to be, up to
342
+ // MAX_SAFE_INTEGER, with a scan of every claim at each step: on the addon
343
+ // host, 2026-09-05, the main thread spun at 100% from the look-ahead timer
344
+ // and the proxy stopped answering anything at all, its own log included.
345
+ // The length was missing because the field naming it had moved and three
346
+ // readers were left on the old name — but a walk whose end depends on a
347
+ // field being present must not be able to do this even then.
348
+ const covered = this.#firstCoveredFrom(start, exceptRun);
349
+ return covered === null ? Number.POSITIVE_INFINITY : covered - start;
350
+ }
351
+ const last = this.#segmentCount - 1;
352
+ let at = start;
353
+ while (at <= last) {
354
+ if (this.#ready.has(at)) {
355
+ break;
356
+ }
357
+ const maker = this.makerOf(at);
358
+ if (maker !== null && !CoverageMap.#isExcepted(maker, exceptRun)) {
359
+ break;
360
+ }
361
+ at += 1;
362
+ }
363
+ return at - start;
364
+ }
365
+
366
+ /**
367
+ * The first number at or after `index` that somebody has made or is making,
368
+ * or null when nobody has touched anything from there on.
369
+ *
370
+ * Asked of what the map HOLDS rather than by walking the numbers, so it can be
371
+ * answered without a length: the map knows every ready number and every claim,
372
+ * and both are finite however long the film is.
373
+ *
374
+ * @param {number} index
375
+ * @param {object | null} exceptRun
376
+ * @returns {number | null}
377
+ */
378
+ /**
379
+ * Is this claim one of the runs the caller is setting aside?
380
+ *
381
+ * A single run or a set of them: a pass that re-cuts several roads at once has
382
+ * to ask about all of them together, and asking once per run gave an answer
383
+ * true of no moment.
384
+ *
385
+ * @param {object} maker
386
+ * @param {object | Set<object> | null} except
387
+ * @returns {boolean}
388
+ */
389
+ static #isExcepted(maker, except) {
390
+ if (except === null || except === undefined) {
391
+ return false;
392
+ }
393
+ return except instanceof Set ? except.has(maker) : maker === except;
394
+ }
395
+
396
+ #firstCoveredFrom(index, exceptRun) {
397
+ let lowest = null;
398
+ for (const ready of this.#ready) {
399
+ if (ready >= index && (lowest === null || ready < lowest)) {
400
+ lowest = ready;
401
+ }
402
+ }
403
+ for (const [run, span] of this.#claims) {
404
+ if (CoverageMap.#isExcepted(run, exceptRun)) {
405
+ continue;
406
+ }
407
+ // A claim that has already begun covers `index` itself.
408
+ const covers = span.from <= index && index <= span.to ? index : span.from;
409
+ if (covers >= index && (lowest === null || covers < lowest)) {
410
+ lowest = covers;
411
+ }
412
+ }
413
+ return lowest;
414
+ }
415
+
416
+ /**
417
+ * What this map holds, for a log line.
418
+ *
419
+ * @returns {{ ready: number, claims: number, segmentCount: number }}
420
+ */
421
+ stats() {
422
+ return {
423
+ ready: this.#ready.size,
424
+ claims: this.#claims.size,
425
+ segmentCount: this.#segmentCount
426
+ };
427
+ }
428
+ }