@torrent-tv/proxy 2.80.8 → 2.80.9
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.
- package/CHANGELOG.md +1666 -1658
- package/bin/cli.js +606 -596
- package/package.json +1 -1
- package/services/encode/CoverageMap.js +428 -401
- package/services/encode/SegmentStore.js +718 -669
- package/services/hls-session-manager.js +7 -19
- package/services/memory-report.js +596 -592
- package/services/orchestrators/EncodeOrchestrator.js +726 -594
- package/test/coverage-follows-the-disk.test.js +187 -0
- package/test/coverage-map.test.js +195 -178
- package/test/encode-plan.test.js +539 -540
- package/test/run-intervals.test.js +100 -100
- package/test/segment-store.test.js +216 -187
package/test/encode-plan.test.js
CHANGED
|
@@ -1,540 +1,539 @@
|
|
|
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/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
|
-
const started = actions.filter((action) => action.type === "start").map((action) => action.from);
|
|
371
|
-
|
|
372
|
-
assert.deepEqual(started, [500], "the one machine goes where somebody is stopped");
|
|
373
|
-
});
|
|
374
|
-
|
|
375
|
-
test("two viewers far apart are both served, by however many encoders serve them soonest", () => {
|
|
376
|
-
// Two encoders, or one that goes to whichever of them is worse off: the answer
|
|
377
|
-
// is what a second process costs this machine, which is measured, and the
|
|
378
|
-
// score works it out. What must hold is that neither of them is simply left.
|
|
379
|
-
const coverage = new CoverageMap({ segmentCount: 1000 });
|
|
380
|
-
const actions = planEncoders({
|
|
381
|
-
coverage,
|
|
382
|
-
windows: [
|
|
383
|
-
{ from: 0, to: 30, withinSeconds: 0 },
|
|
384
|
-
{ from: 800, to: 830, withinSeconds: 0 }
|
|
385
|
-
],
|
|
386
|
-
runs: [run({ from: 900, to: 999, head: 900 })],
|
|
387
|
-
...HOST,
|
|
388
|
-
maxRuns: 3
|
|
389
|
-
});
|
|
390
|
-
const spans = actions
|
|
391
|
-
.filter((action) => action.type !== "stop")
|
|
392
|
-
.map((action) => [action.from, action.to < action.from ? Number.POSITIVE_INFINITY : action.to]);
|
|
393
|
-
assert.ok(spans.some(([from, to]) => from <= 0 && to >= 0), "the one at the beginning is served");
|
|
394
|
-
for (let index = 0; index < spans.length - 1; index += 1) {
|
|
395
|
-
const sorted = [...spans].sort((left, right) => left[0] - right[0]);
|
|
396
|
-
assert.ok(sorted[index][1] < sorted[index + 1][0], "and no two encoders share a number");
|
|
397
|
-
}
|
|
398
|
-
});
|
|
399
|
-
|
|
400
|
-
test("a machine that can hold one gives it to the viewer who is stopped soonest", () => {
|
|
401
|
-
// The bound is the host's own budget, the same arithmetic that decides the
|
|
402
|
-
// quality offer. Measured on the addon host 2026-09-03: at 1080p one encode
|
|
403
|
-
// saturates the machine and two land on realtime.
|
|
404
|
-
const coverage = new CoverageMap({ segmentCount: 1000 });
|
|
405
|
-
const actions = planEncoders({
|
|
406
|
-
coverage,
|
|
407
|
-
windows: [{ from: 800, to: 830 }, { from: 0, to: 30 }],
|
|
408
|
-
runs: [],
|
|
409
|
-
...HOST,
|
|
410
|
-
maxRuns: 1
|
|
411
|
-
});
|
|
412
|
-
const started = actions.filter((action) => action.type === "start").map((action) => action.from);
|
|
413
|
-
assert.deepEqual(started, [0]);
|
|
414
|
-
});
|
|
415
|
-
|
|
416
|
-
test("a viewer joining behind a running encoder gets their own, not a dragged one", () => {
|
|
417
|
-
// This is what the whole layer is for. The run in front is untouched; the
|
|
418
|
-
// viewer behind is not made to wait for it to be pulled back.
|
|
419
|
-
const coverage = new CoverageMap({ segmentCount: 1000 });
|
|
420
|
-
const runA = run({ from: 500, to: 600, head: 510 });
|
|
421
|
-
coverage.claim(runA, 500, 600);
|
|
422
|
-
// A head at #510 means #500..#509 are already made — that is what a head is.
|
|
423
|
-
// Left unsaid, the map believes a viewer is waiting on ten pieces nobody has,
|
|
424
|
-
// and moving the encoder back to make them is then the right answer.
|
|
425
|
-
for (let index = 500; index < 510; index += 1) {
|
|
426
|
-
coverage.markReady(index);
|
|
427
|
-
}
|
|
428
|
-
const actions = planEncoders({
|
|
429
|
-
coverage,
|
|
430
|
-
windows: [{ from: 500, to: 530 }, { from: 100, to: 130 }],
|
|
431
|
-
runs: [runA],
|
|
432
|
-
...HOST
|
|
433
|
-
});
|
|
434
|
-
assert.ok(actions.some((action) => action.type === "keep" && action.run === runA));
|
|
435
|
-
assert.equal(actions.some((action) => action.type === "move"), false);
|
|
436
|
-
const started = actions.filter((action) => action.type === "start");
|
|
437
|
-
assert.equal(started.length, 1);
|
|
438
|
-
assert.equal(started[0].from, 100);
|
|
439
|
-
});
|
|
440
|
-
|
|
441
|
-
test("the lowest thing a viewer is waiting for is reported, so a stalled plan is visible", () => {
|
|
442
|
-
const coverage = new CoverageMap({ segmentCount: 100 });
|
|
443
|
-
coverage.
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
//
|
|
452
|
-
//
|
|
453
|
-
// end
|
|
454
|
-
//
|
|
455
|
-
//
|
|
456
|
-
|
|
457
|
-
const
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
//
|
|
484
|
-
//
|
|
485
|
-
//
|
|
486
|
-
//
|
|
487
|
-
//
|
|
488
|
-
//
|
|
489
|
-
//
|
|
490
|
-
//
|
|
491
|
-
//
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
{ from:
|
|
501
|
-
|
|
502
|
-
],
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
.
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
const
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
.
|
|
531
|
-
.
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
spans[index].to
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
});
|
|
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/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
|
+
const started = actions.filter((action) => action.type === "start").map((action) => action.from);
|
|
371
|
+
|
|
372
|
+
assert.deepEqual(started, [500], "the one machine goes where somebody is stopped");
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
test("two viewers far apart are both served, by however many encoders serve them soonest", () => {
|
|
376
|
+
// Two encoders, or one that goes to whichever of them is worse off: the answer
|
|
377
|
+
// is what a second process costs this machine, which is measured, and the
|
|
378
|
+
// score works it out. What must hold is that neither of them is simply left.
|
|
379
|
+
const coverage = new CoverageMap({ segmentCount: 1000 });
|
|
380
|
+
const actions = planEncoders({
|
|
381
|
+
coverage,
|
|
382
|
+
windows: [
|
|
383
|
+
{ from: 0, to: 30, withinSeconds: 0 },
|
|
384
|
+
{ from: 800, to: 830, withinSeconds: 0 }
|
|
385
|
+
],
|
|
386
|
+
runs: [run({ from: 900, to: 999, head: 900 })],
|
|
387
|
+
...HOST,
|
|
388
|
+
maxRuns: 3
|
|
389
|
+
});
|
|
390
|
+
const spans = actions
|
|
391
|
+
.filter((action) => action.type !== "stop")
|
|
392
|
+
.map((action) => [action.from, action.to < action.from ? Number.POSITIVE_INFINITY : action.to]);
|
|
393
|
+
assert.ok(spans.some(([from, to]) => from <= 0 && to >= 0), "the one at the beginning is served");
|
|
394
|
+
for (let index = 0; index < spans.length - 1; index += 1) {
|
|
395
|
+
const sorted = [...spans].sort((left, right) => left[0] - right[0]);
|
|
396
|
+
assert.ok(sorted[index][1] < sorted[index + 1][0], "and no two encoders share a number");
|
|
397
|
+
}
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
test("a machine that can hold one gives it to the viewer who is stopped soonest", () => {
|
|
401
|
+
// The bound is the host's own budget, the same arithmetic that decides the
|
|
402
|
+
// quality offer. Measured on the addon host 2026-09-03: at 1080p one encode
|
|
403
|
+
// saturates the machine and two land on realtime.
|
|
404
|
+
const coverage = new CoverageMap({ segmentCount: 1000 });
|
|
405
|
+
const actions = planEncoders({
|
|
406
|
+
coverage,
|
|
407
|
+
windows: [{ from: 800, to: 830 }, { from: 0, to: 30 }],
|
|
408
|
+
runs: [],
|
|
409
|
+
...HOST,
|
|
410
|
+
maxRuns: 1
|
|
411
|
+
});
|
|
412
|
+
const started = actions.filter((action) => action.type === "start").map((action) => action.from);
|
|
413
|
+
assert.deepEqual(started, [0]);
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
test("a viewer joining behind a running encoder gets their own, not a dragged one", () => {
|
|
417
|
+
// This is what the whole layer is for. The run in front is untouched; the
|
|
418
|
+
// viewer behind is not made to wait for it to be pulled back.
|
|
419
|
+
const coverage = new CoverageMap({ segmentCount: 1000 });
|
|
420
|
+
const runA = run({ from: 500, to: 600, head: 510 });
|
|
421
|
+
coverage.claim(runA, 500, 600);
|
|
422
|
+
// A head at #510 means #500..#509 are already made — that is what a head is.
|
|
423
|
+
// Left unsaid, the map believes a viewer is waiting on ten pieces nobody has,
|
|
424
|
+
// and moving the encoder back to make them is then the right answer.
|
|
425
|
+
for (let index = 500; index < 510; index += 1) {
|
|
426
|
+
coverage.markReady(index);
|
|
427
|
+
}
|
|
428
|
+
const actions = planEncoders({
|
|
429
|
+
coverage,
|
|
430
|
+
windows: [{ from: 500, to: 530 }, { from: 100, to: 130 }],
|
|
431
|
+
runs: [runA],
|
|
432
|
+
...HOST
|
|
433
|
+
});
|
|
434
|
+
assert.ok(actions.some((action) => action.type === "keep" && action.run === runA));
|
|
435
|
+
assert.equal(actions.some((action) => action.type === "move"), false);
|
|
436
|
+
const started = actions.filter((action) => action.type === "start");
|
|
437
|
+
assert.equal(started.length, 1);
|
|
438
|
+
assert.equal(started[0].from, 100);
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
test("the lowest thing a viewer is waiting for is reported, so a stalled plan is visible", () => {
|
|
442
|
+
const coverage = new CoverageMap({ segmentCount: 100 });
|
|
443
|
+
coverage.setReady([40, 41]);
|
|
444
|
+
assert.equal(firstUnmetWant(coverage, [{ from: 40, to: 70 }]), 42);
|
|
445
|
+
coverage.setReady([40, 41, 42, 43, 44]);
|
|
446
|
+
assert.equal(firstUnmetWant(coverage, [{ from: 40, to: 44 }]), null);
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
test("a run with no end is making what the viewers ahead of it are waiting for", () => {
|
|
450
|
+
// Field, 2026-09-05: an encoder was started and killed every five seconds,
|
|
451
|
+
// each producing 0-2 segments, for as long as anybody watched. A run given no
|
|
452
|
+
// end carries `to = -1`, and two places read that as a number instead of as
|
|
453
|
+
// "no end": the overlap test called its work unwanted, and the claim it made
|
|
454
|
+
// in the coverage map was one segment long, so the plan saw the rest of the
|
|
455
|
+
// film as free and started another encoder there.
|
|
456
|
+
const coverage = new CoverageMap({ segmentCount: 570 });
|
|
457
|
+
const run = { from: 0, to: -1, head: 3, speedX: 8, isAlive: true };
|
|
458
|
+
coverage.claim(run, run.from, endOfRun(run));
|
|
459
|
+
|
|
460
|
+
const actions = planEncoders({
|
|
461
|
+
coverage,
|
|
462
|
+
live: [run],
|
|
463
|
+
wanted: [{ from: 0, to: 30 }],
|
|
464
|
+
maxRuns: 2,
|
|
465
|
+
segmentSeconds: 4,
|
|
466
|
+
...RUN_COSTS
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
assert.deepEqual(
|
|
470
|
+
actions.filter((action) => action.type === "stop"),
|
|
471
|
+
[],
|
|
472
|
+
"nothing is stopped: it is making exactly what is wanted"
|
|
473
|
+
);
|
|
474
|
+
assert.deepEqual(
|
|
475
|
+
actions.filter((action) => action.type === "start"),
|
|
476
|
+
[],
|
|
477
|
+
"and nothing new is started over ground it already holds"
|
|
478
|
+
);
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
test("spare budget IS spent on the rest of the film, once nobody is waiting", () => {
|
|
482
|
+
// The user's own correction: film nobody is waiting for still has value,
|
|
483
|
+
// because a viewer seeking back into a part that exists starts playing at
|
|
484
|
+
// once. So what the machine has spare goes to finishing the file.
|
|
485
|
+
//
|
|
486
|
+
// "Once nobody is waiting" is not a condition written anywhere — it falls out
|
|
487
|
+
// of the score. A second process costs the first the measured share of the
|
|
488
|
+
// machine, so while somebody is stopped on a piece, adding one delays that
|
|
489
|
+
// piece and the first term refuses it. Here the near film is already made,
|
|
490
|
+
// nothing is late in either arrangement, and the two that finish the rest
|
|
491
|
+
// sooner win.
|
|
492
|
+
const coverage = new CoverageMap({ segmentCount: 400 });
|
|
493
|
+
for (let index = 0; index <= 9; index += 1) {
|
|
494
|
+
coverage.markReady(index);
|
|
495
|
+
}
|
|
496
|
+
const actions = planEncoders({
|
|
497
|
+
coverage,
|
|
498
|
+
windows: [
|
|
499
|
+
{ from: 0, to: 9, priority: 32, withinSeconds: 0 },
|
|
500
|
+
{ from: 10, to: 399, priority: 20, withinSeconds: 40 }
|
|
501
|
+
],
|
|
502
|
+
runs: [],
|
|
503
|
+
...HOST,
|
|
504
|
+
maxRuns: 4
|
|
505
|
+
});
|
|
506
|
+
const started = actions.filter((action) => action.type === "start");
|
|
507
|
+
|
|
508
|
+
assert.ok(started.length > 1, "the machine does not stand idle while film is unmade");
|
|
509
|
+
const spans = started
|
|
510
|
+
.map((action) => [action.from, action.to])
|
|
511
|
+
.sort((left, right) => left[0] - right[0]);
|
|
512
|
+
for (let index = 0; index < spans.length - 1; index += 1) {
|
|
513
|
+
assert.ok(spans[index][1] < spans[index + 1][0], "and no two of them share a number");
|
|
514
|
+
}
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
test("two encoders never share a segment number", () => {
|
|
519
|
+
// The whole of what went wrong in the field: two encoders writing one name.
|
|
520
|
+
const coverage = new CoverageMap({ segmentCount: 400 });
|
|
521
|
+
const actions = planEncoders({
|
|
522
|
+
coverage,
|
|
523
|
+
windows: [{ from: 0, to: 399, priority: 32 }],
|
|
524
|
+
runs: [],
|
|
525
|
+
...HOST,
|
|
526
|
+
maxRuns: 4
|
|
527
|
+
});
|
|
528
|
+
const spans = actions
|
|
529
|
+
.filter((action) => action.type === "start")
|
|
530
|
+
.map((action) => ({ from: action.from, to: action.to }))
|
|
531
|
+
.sort((left, right) => left.from - right.from);
|
|
532
|
+
|
|
533
|
+
for (let index = 0; index < spans.length - 1; index += 1) {
|
|
534
|
+
assert.ok(
|
|
535
|
+
spans[index].to < spans[index + 1].from,
|
|
536
|
+
`#${spans[index].from}..#${spans[index].to} overlaps #${spans[index + 1].from}`
|
|
537
|
+
);
|
|
538
|
+
}
|
|
539
|
+
});
|