@torrent-tv/proxy 2.35.0 → 2.36.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## 2.36.1
2
+
3
+ - **Fix**: The cut list of a copied picture is built from the picture's own keyframes, and no longer from every entry in the container's table. A Matroska CuePoint belongs to the track named inside it, and RFC 9559 leaves the muxer free to index whichever tracks it likes — both field files index their subtitles as well. Measured over the swarm on 2026-08-18, reading only the head and the table: `Minions.and.Monsters.1080p.mkv` has **2778 video entries, one every 2.002 s, and 4669 more across four subtitle tracks**; `Moana.2.2024.720p.BluRay … MegaPeer.mkv` has **1055 video entries and 5007 across five**. Read without the track, the extra times entered the cut list as though they were keyframes; ffmpeg can only cut a copied picture at a real keyframe at or after the time it is given, so each such cut landed at the next one instead — which is exactly the disagreement the field measured, and why it was always positive: 2.002 s on the first file (its own keyframe spacing), a median of 6.3 s and a worst case of 21 s on the second. The reader now takes the first video track's number from Tracks — already inside the head it fetches, with one short extra read only for a file that keeps Tracks elsewhere — and keeps the entries of that track. Nothing else about the two-read approach changes, and a session costs nothing more. With the fix the same two files read 2778 and 1055 times, all of them keyframes. When the filter leaves NOTHING — a table whose entries name a track number Tracks never declares — the unfiltered table is used rather than no table: that case is this reader failing to recognise the file, and answering with nothing would put an even grid on a copied picture, which is the failure it exists to prevent.
4
+ - **Chore**: `scripts/read-container-index.mjs` reads the index of any `.torrent` over the swarm — two short ranged reads, in memory, no file written — so a claim about what a container says can be checked against a real film in seconds. Written after the measurement above was made by hand three times.
5
+
6
+ ## 2.36.0
7
+
8
+ - **New**: The torrent is charged for the megabytes it is measured to be moving, and the price it is charged at no longer contains work that is not the torrent's. Two faults, both visible in one field log from the addon host (2026-08-18): the same session reported **145.4 ms of CPU per MB over 8.7 MB and 23.1 ms per MB over 54 MB**, a sixfold disagreement that followed the size of the interval rather than anything about the torrent — because a process with nothing to do still runs its timers, its tunnel and its session sweeps, and that draw does not shrink when fewer megabytes move. A minimum-megabytes threshold stood against exactly this and did not hold, because a chosen number was standing in for a measured one. The draw is now measured directly, in the intervals where nothing encodes and not one byte moves, and subtracted before the rest is called the torrent's (`services/torrent-cost.js`). What the threshold was reaching for is arrived at from the readings instead: the draw's own readings disagree by a measured amount, that disagreement is worth `scatter × elapsed` seconds over an interval, and a remainder smaller than it measures the wobble in the subtraction rather than the torrent — so a small interval fails on the same arithmetic that lets a large one through, with no size chosen anywhere. The second fault: the price was then charged against the file's own byte rate — what the viewer consumes — so a fully downloaded file moving nothing still paid, and a file being fetched ahead of the viewer, which is how every session starts, paid too little. It is charged against the rate the torrent is measured to be moving, sampled every five seconds per watched torrent and divided among the files of it being read, so two episodes of one pack do not each pay for the whole download.
9
+ - **New**: No separate penalty for downloading, because the measurement says none is needed. The readings of 2026-08-18 have decoding at 10.1-11.2x with nothing running and 6.45-6.71x while the torrent pulled 1.8 MB/s — an extra 0.050-0.066 seconds of work per second of content, which is **28-37 ms per megabyte moved**, against the 20-35 ms/MB the host measures for itself. The download's effect on other work is the processor time it consumes, and that is already priced; unlike a second encoder, which costs 2.71× and is not explained by any sum. So this closes roadmap item 6 with a subtraction rather than another multiplier.
10
+ - **New**: A quality step is judged against the speed this file's own supply demands, not against a chosen margin. `1.5` (and `1.8` where decoding had no price) stood for "faster than realtime by enough", and what "enough" means is measured per file and per swarm by the reader that waits for the pieces: `1 + worst wait / median interval`, which the proxy has been printing since 2.30.0 without using. On the field torrent of 2026-08-17 that is 1.67, and on the 720p rip of the same evening 4.04-8.14 — a torrent on which no re-encoded step could have kept up, and which a fixed 1.5 admitted. Where the swarm has not been measured yet — fewer than two interruptions — the bar is realtime, which is the one thing that can be said without measuring it, and the offer is restated as soon as the reader has something to say. The refusal line names the figure it refused against and where it came from. One chosen number survives, and only where nothing can be measured: a host whose calibration produced no decode term at all is judged on an encode-only prediction that was several times optimistic in the field, and its bar stays at the 1.8 it has had since before decoding was priced, because lowering it to realtime would make the least-measured hosts the most permissive.
11
+ - **Chore**: Two learning thresholds removed, both of which were chosen numbers standing in for measurements. A reading of an encoder's speed is no longer withheld for the first twenty seconds of a run: each sample now carries the serial of the run it was taken from and a pair whose serials differ is discarded, which is what the wait stood in for — a restart spends up to a second and a half making its directory and burying its predecessor, and a sample taken in that window carries the old run's position, so paired with the new one it reads a twenty-minute seek as twenty minutes produced in five seconds. The wait cost every reading a short run could have given, which is how a rung spent three minutes below realtime teaching nothing (2026-08-15); the serial costs none. And a new median is adopted when it has moved further than the readings behind it disagree with each other, instead of by more than five per cent (`services/learned-median.js`). What remains chosen is the length of the history a median is taken over, and it is now written down as such rather than given a measured-sounding reason.
12
+
1
13
  ## 2.35.0
2
14
 
3
15
  - **New**: A second job's cost is measured on the host instead of being added as though jobs were independent. Measured on the addon host 2026-08-18, decoding the same clip: **2.10-2.25x alone, 0.79-0.90x with one encoder beside it, 0.56-0.64x with two** — the same work costs 2.6× more for having company, and 3.7× for having two. Heat is not the cause: the hot idle machine (68 °C, a lower reported clock) was the fastest reading of all, which settles what roadmap item 6 was opened for. Four cores sharing one path to memory is the cause, and it contradicts the SHAPE of the budget rather than its constants — everything in the quality offer adds seconds of work per second of content, and these readings say two jobs that each fit alone do not fit together. So the penalty is now measured at startup the way everything else is (the cheapest clip decoded alone, then again while an encoder of it runs), and the offer multiplies a step's cost by it when anything else is encoding. Beyond the readings it holds the largest rather than extrapolating: two points say nothing about a fourth job, and a budget that guesses at a memory bottleneck will be wrong in whichever direction it guesses. With nothing measured, nothing is corrected. This is separate from the availability share of 2.33.0, which removes work nobody has been charged for; this is our own work colliding with itself.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@torrent-tv/proxy",
3
- "version": "2.35.0",
3
+ "version": "2.36.1",
4
4
  "description": "Torrent proxy client that exposes webseed-like HTTP stream endpoint.",
5
5
  "license": "GPL-3.0-or-later",
6
6
  "publishConfig": {
@@ -0,0 +1,108 @@
1
+ /**
2
+ * @file What this proxy would cut a file at, read from a real torrent.
3
+ *
4
+ * The keyframe readers take two short ranged reads, so the index of a 7 GB film
5
+ * can be examined over the swarm in seconds without downloading it. Run against
6
+ * a `.torrent` file to see what the container's own table says, per track, and
7
+ * what the reader hands to the cut list.
8
+ *
9
+ * node scripts/read-container-index.mjs path/to/film.torrent [more.torrent …]
10
+ *
11
+ * Developer tool: nothing here runs in a session, and no viewer is involved.
12
+ */
13
+
14
+ import fs from "node:fs";
15
+ import path from "node:path";
16
+ import MemoryChunkStore from "memory-chunk-store";
17
+ import WebTorrent from "webtorrent";
18
+ import { readMatroskaKeyframeTimes } from "../services/container-index/matroska.js";
19
+ import { readMp4KeyframeTimes } from "../services/container-index/mp4.js";
20
+ import { readAviKeyframeTimes } from "../services/container-index/avi.js";
21
+
22
+ const torrents = process.argv.slice(2);
23
+ if (torrents.length === 0) {
24
+ console.error("usage: node scripts/read-container-index.mjs <file.torrent> [...]");
25
+ process.exit(1);
26
+ }
27
+
28
+ /**
29
+ * Collect one byte range of a torrent file.
30
+ *
31
+ * @param {import("webtorrent").TorrentFile} file
32
+ * @param {number} start
33
+ * @param {number} end - Inclusive.
34
+ * @returns {Promise<Buffer>}
35
+ */
36
+ function readRangeOf(file, start, end) {
37
+ return new Promise((resolve, reject) => {
38
+ const chunks = [];
39
+ const stream = file.createReadStream({ start, end });
40
+ stream.on("data", (chunk) => chunks.push(chunk));
41
+ stream.on("end", () => resolve(Buffer.concat(chunks)));
42
+ stream.on("error", reject);
43
+ });
44
+ }
45
+
46
+ /**
47
+ * Say what a set of times looks like: how many, where it starts, how far apart.
48
+ *
49
+ * @param {string} label
50
+ * @param {number[] | null} times
51
+ * @returns {void}
52
+ */
53
+ function describe(label, times) {
54
+ if (!times || times.length === 0) {
55
+ console.log(`${label}: no index`);
56
+ return;
57
+ }
58
+ const gaps = times.slice(1).map((time, index) => time - times[index]).sort((left, right) => left - right);
59
+ const median = gaps.length > 0 ? gaps[Math.floor(gaps.length / 2)] : 0;
60
+ console.log(
61
+ `${label}: ${times.length} times, first ${times.slice(0, 4).map((time) => time.toFixed(3)).join(" ")}, ` +
62
+ `median gap ${median.toFixed(3)}s, last ${times[times.length - 1].toFixed(3)}`
63
+ );
64
+ }
65
+
66
+ for (const torrentPath of torrents) {
67
+ // uTP is off: binding its socket needs a permission this does not have on
68
+ // every developer machine, and TCP peers are enough to read an index.
69
+ const client = new WebTorrent({ utp: false });
70
+ console.log(`\n=== ${path.basename(torrentPath)} ===`);
71
+ await new Promise((resolve) => {
72
+ // In memory, deliberately. The file store creates the whole file on disk at
73
+ // its full length before a byte of it is wanted — reading the index of four
74
+ // films that way filled 34 GB and then failed with the disk full. Only two
75
+ // short ranges are ever read here, so they can simply be held.
76
+ client.add(fs.readFileSync(torrentPath), { store: MemoryChunkStore }, async (torrent) => {
77
+ try {
78
+ // Nothing is wanted up front; the ranged reads below select what they need.
79
+ torrent.deselect(0, torrent.pieces.length - 1, 0);
80
+ const file = torrent.files
81
+ .filter((candidate) => /\.(mkv|mp4|avi)$/i.test(candidate.name))
82
+ .sort((left, right) => right.length - left.length)[0];
83
+ if (!file) {
84
+ console.log("no video file in this torrent");
85
+ return resolve();
86
+ }
87
+ console.log(`file: ${file.name} (${(file.length / 1e9).toFixed(2)} GB)`);
88
+ const readRange = async (start, end) => {
89
+ const last = Math.min(end, file.length - 1);
90
+ return start > last ? null : readRangeOf(file, start, last);
91
+ };
92
+ if (/\.mkv$/i.test(file.name)) {
93
+ describe("matroska", await readMatroskaKeyframeTimes(readRange, file.length));
94
+ } else if (/\.mp4$/i.test(file.name)) {
95
+ describe("mp4", await readMp4KeyframeTimes(readRange, file.length));
96
+ } else {
97
+ describe("avi", await readAviKeyframeTimes(readRange, file.length));
98
+ }
99
+ } catch (error) {
100
+ console.log(`failed: ${error?.message ?? error}`);
101
+ } finally {
102
+ resolve();
103
+ }
104
+ });
105
+ });
106
+ await new Promise((done) => client.destroy(done));
107
+ }
108
+ process.exit(0);
@@ -28,6 +28,14 @@ const ID_TIMESTAMP_SCALE = 0x2ad7b1;
28
28
  const ID_CUES = 0x1c53bb6b;
29
29
  const ID_CUE_POINT = 0xbb;
30
30
  const ID_CUE_TIME = 0xb3;
31
+ const ID_CUE_TRACK_POSITIONS = 0xb7;
32
+ const ID_CUE_TRACK = 0xf7;
33
+ const ID_TRACKS = 0x1654ae6b;
34
+ const ID_TRACK_ENTRY = 0xae;
35
+ const ID_TRACK_NUMBER = 0xd7;
36
+ const ID_TRACK_TYPE = 0x83;
37
+ // TrackType 1 is video; 2 is audio, 17 subtitles, and the rest are rarer still.
38
+ const TRACK_TYPE_VIDEO = 1;
31
39
 
32
40
  // How much of the file start to read. Must cover the EBML header, the SeekHead
33
41
  // and Info; 64 KB is generous for every real muxer (the file measured needed
@@ -36,6 +44,10 @@ const HEAD_BYTES = 64 * 1024;
36
44
  // Cap on the Cues read. A two-hour film indexes to tens of KB; anything beyond
37
45
  // this is not a normal index and not worth pulling over a torrent.
38
46
  const MAX_CUES_BYTES = 8 * 1024 * 1024;
47
+ // Cap on a Tracks read, for the rare file whose Tracks element sits outside the
48
+ // head window. Track entries are small, so a file with dozens of them still
49
+ // fits well inside this.
50
+ const MAX_TRACKS_BYTES = 1024 * 1024;
39
51
  // Matroska's default timestamp scale (nanoseconds per tick) when Info omits it.
40
52
  const DEFAULT_TIMESTAMP_SCALE = 1_000_000;
41
53
 
@@ -121,13 +133,64 @@ function readTimestampScale(head, segmentDataOffset) {
121
133
  }
122
134
 
123
135
  /**
124
- * Cue times (seconds, ascending) from a Cues payload.
136
+ * The number of the first video track, from a Tracks payload.
137
+ *
138
+ * The FIRST one, because that is the track ffmpeg is told to copy (`0:v:0`).
139
+ *
140
+ * @param {Buffer} buffer
141
+ * @param {{ dataOffset: number, size: number }} tracks
142
+ * @returns {number | null}
143
+ */
144
+ function readVideoTrackNumber(buffer, tracks) {
145
+ const tracksEnd = Math.min(buffer.length, tracks.dataOffset + tracks.size);
146
+ for (const entry of iterateElements(buffer, tracks.dataOffset, tracksEnd)) {
147
+ if (entry.id !== ID_TRACK_ENTRY) {
148
+ continue;
149
+ }
150
+ const entryEnd = Math.min(tracksEnd, entry.dataOffset + entry.size);
151
+ let number = null;
152
+ let type = null;
153
+ for (const field of iterateElements(buffer, entry.dataOffset, entryEnd)) {
154
+ if (field.id === ID_TRACK_NUMBER) {
155
+ number = readUint(buffer, field.dataOffset, field.size);
156
+ } else if (field.id === ID_TRACK_TYPE) {
157
+ type = readUint(buffer, field.dataOffset, field.size);
158
+ }
159
+ }
160
+ if (number !== null && type === TRACK_TYPE_VIDEO) {
161
+ return number;
162
+ }
163
+ }
164
+ return null;
165
+ }
166
+
167
+ /**
168
+ * Cue times (seconds, ascending) of ONE track, from a Cues payload.
169
+ *
170
+ * The track is the whole point, and leaving it out is what this reader got
171
+ * wrong until 2026-08-18. A CuePoint belongs to the track named inside its
172
+ * CueTrackPositions, and a muxer indexes whatever tracks it likes: RFC 9559
173
+ * says each keyframe of a video track SHOULD be referenced, and that the Cues
174
+ * Element "can be used to index every single timestamp of every Block or they
175
+ * can be indexed selectively". Both field files index their SUBTITLE tracks as
176
+ * well — `Minions.and.Monsters.1080p.mkv` has 2778 video entries every 2.002 s
177
+ * plus 4669 across four subtitle tracks; `Moana.2 … MegaPeer.mkv` has 1055
178
+ * video entries plus 5007 across five.
179
+ *
180
+ * Read without the track, those extra times enter the cut list as though they
181
+ * were keyframes. ffmpeg can only cut a COPIED picture at a real keyframe at or
182
+ * after the time it is given, so every cut asked for at one of them lands late
183
+ * — which is exactly what the field measured: on the first file every deviation
184
+ * was 2.002 s, that file's own keyframe spacing, and on the second the median
185
+ * was 6.3 s with a worst case of 21 s. Never once negative.
125
186
  *
126
187
  * @param {Buffer} cues
127
188
  * @param {number} timestampScale - Nanoseconds per tick.
189
+ * @param {number | null} trackNumber - Null keeps every entry, which is right
190
+ * only for a file that indexes one track.
128
191
  * @returns {number[]}
129
192
  */
130
- function readCueTimes(cues, timestampScale) {
193
+ function readCueTimes(cues, timestampScale, trackNumber) {
131
194
  const times = [];
132
195
  const secondsPerTick = timestampScale / 1e9;
133
196
  for (const point of iterateElements(cues)) {
@@ -135,11 +198,26 @@ function readCueTimes(cues, timestampScale) {
135
198
  continue;
136
199
  }
137
200
  const pointEnd = Math.min(cues.length, point.dataOffset + point.size);
201
+ let time = null;
202
+ let belongsToTrack = trackNumber === null;
138
203
  for (const field of iterateElements(cues, point.dataOffset, pointEnd)) {
139
204
  if (field.id === ID_CUE_TIME) {
140
- times.push(readUint(cues, field.dataOffset, field.size) * secondsPerTick);
141
- break;
205
+ time = readUint(cues, field.dataOffset, field.size) * secondsPerTick;
206
+ continue;
207
+ }
208
+ if (field.id !== ID_CUE_TRACK_POSITIONS || belongsToTrack) {
209
+ continue;
142
210
  }
211
+ const positionsEnd = Math.min(pointEnd, field.dataOffset + field.size);
212
+ for (const inner of iterateElements(cues, field.dataOffset, positionsEnd)) {
213
+ if (inner.id === ID_CUE_TRACK && readUint(cues, inner.dataOffset, inner.size) === trackNumber) {
214
+ belongsToTrack = true;
215
+ break;
216
+ }
217
+ }
218
+ }
219
+ if (time !== null && belongsToTrack) {
220
+ times.push(time);
143
221
  }
144
222
  }
145
223
  times.sort((left, right) => left - right);
@@ -195,6 +273,64 @@ export async function readMatroskaKeyframeTimes(readRange, fileSize) {
195
273
  const payloadEnd = Math.min(cuesChunk.length, cuesElement.dataOffset + cuesElement.size);
196
274
  const payload = cuesChunk.subarray(cuesElement.dataOffset, payloadEnd);
197
275
 
198
- const times = readCueTimes(payload, readTimestampScale(head, seekHead.segmentDataOffset));
199
- return times.length > 0 ? times : null;
276
+ // Whose entries to keep. Tracks sits near the head and is normally inside the
277
+ // bytes already fetched; when it is not, SeekHead says where it is and one
278
+ // more short read gets it. Nothing is fetched twice and nothing is scanned.
279
+ const videoTrack = await readVideoTrack(readRange, head, seekHead, fileSize);
280
+ const timestampScale = readTimestampScale(head, seekHead.segmentDataOffset);
281
+ const times = readCueTimes(payload, timestampScale, videoTrack);
282
+ if (times.length > 0) {
283
+ return times;
284
+ }
285
+ if (videoTrack === null) {
286
+ return null;
287
+ }
288
+ // The filter left nothing, and that is not an answer about the file: a table
289
+ // exists, and this reader simply failed to recognise which of its entries
290
+ // belong to the picture — a track numbered one way in Tracks and another in
291
+ // the cue points, or an entry with no CueTrack at all. Returning null here
292
+ // would put an EVEN grid on a copied picture, which is the failure this
293
+ // module exists to prevent, so the unfiltered table is used instead: less
294
+ // exact than the picture's own keyframes, better than a grid that has nothing
295
+ // to do with the file.
296
+ const unfiltered = readCueTimes(payload, timestampScale, null);
297
+ return unfiltered.length > 0 ? unfiltered : null;
298
+ }
299
+
300
+ /**
301
+ * The video track's number — from the head when it is there, and from one extra
302
+ * short read when it is not.
303
+ *
304
+ * @param {(start: number, end: number) => Promise<Buffer | null>} readRange
305
+ * @param {Buffer} head
306
+ * @param {{ segmentDataOffset: number, entries: Map<number, number> }} seekHead
307
+ * @param {number} fileSize
308
+ * @returns {Promise<number | null>} Null when Tracks cannot be read at all, and
309
+ * then every cue entry is kept — right for a file that indexes only its
310
+ * picture, wrong for one that does not, and nothing here can tell them apart.
311
+ * Refusing the index instead would put an even grid on a copied picture,
312
+ * which is the failure this reader exists to prevent.
313
+ */
314
+ async function readVideoTrack(readRange, head, seekHead, fileSize) {
315
+ const inHead = findElement(head, ID_TRACKS, [], seekHead.segmentDataOffset);
316
+ if (inHead && inHead.dataOffset + inHead.size <= head.length) {
317
+ return readVideoTrackNumber(head, inHead);
318
+ }
319
+ const relative = seekHead.entries.get(ID_TRACKS);
320
+ if (relative === undefined) {
321
+ return null;
322
+ }
323
+ const offset = seekHead.segmentDataOffset + relative;
324
+ if (!Number.isFinite(offset) || offset <= 0 || offset >= fileSize) {
325
+ return null;
326
+ }
327
+ const chunk = await readRange(offset, Math.min(fileSize - 1, offset + MAX_TRACKS_BYTES));
328
+ if (!chunk || chunk.length === 0) {
329
+ return null;
330
+ }
331
+ const element = [...iterateElements(chunk, 0, chunk.length)][0];
332
+ if (!element || element.id !== ID_TRACKS) {
333
+ return null;
334
+ }
335
+ return readVideoTrackNumber(chunk, { dataOffset: element.dataOffset, size: element.size });
200
336
  }
@@ -1,128 +1,128 @@
1
- /**
2
- * @file What a second job costs on this machine — measured, because it is not
3
- * the sum of two prices.
4
- *
5
- * Measured on the addon host 2026-08-18, decoding the same clip:
6
- *
7
- * | what else was running | decode speed | cost |
8
- * |---|---|---|
9
- * | nothing (cold) | 2.10-2.25x | 0.46 s/s |
10
- * | nothing (hot, 68 °C) | 2.30-2.33x | 0.43 s/s |
11
- * | one encoder | 0.79-0.90x | 1.18 s/s |
12
- * | two encoders | 0.56-0.64x | 1.67 s/s |
13
- *
14
- * The same work costs **2.6× more** with one encoder beside it and 3.7× with
15
- * two. Heat is not the cause — the hot idle machine was the fastest of all.
16
- * Four cores share one narrow path to memory, and decoding and encoding both
17
- * saturate it.
18
- *
19
- * That contradicts the shape of the budget, not just its constants. Everything
20
- * in the quality offer adds seconds of work per second of content — this
21
- * encode, plus that decode, plus what is already committed — and these readings
22
- * say two jobs that each fit alone do not fit together. The availability
23
- * correction in `available-share.js` does not cover it either: that subtracts
24
- * the share of the machine taken by work nobody has been charged for, while
25
- * this is our own work colliding with itself.
26
- *
27
- * So the penalty is measured rather than derived, at startup, on the host it
28
- * describes — and beyond the range that was measured it does not extrapolate:
29
- * it holds the largest reading and says so, because nothing here knows what a
30
- * fourth job would do.
31
- */
32
-
33
- /**
34
- * Penalties measured on this host, keyed by how many OTHER jobs were running.
35
- *
36
- * @typedef {Map<number, number>} ContentionPenalties
37
- */
38
-
39
- /**
40
- * The multiplier for a job's cost when `othersRunning` other jobs share the
41
- * machine.
42
- *
43
- * @param {number} othersRunning
44
- * @param {ContentionPenalties | null} measured
45
- * @returns {{ penalty: number, measured: boolean, from: number }}
46
- * `from` is the reading it came from — equal to `othersRunning` when one was
47
- * measured for exactly that many, and the largest available otherwise.
48
- */
49
- export function contentionPenalty(othersRunning, measured) {
50
- const others = Number.isFinite(othersRunning) ? Math.max(0, Math.round(othersRunning)) : 0;
51
- if (others === 0) {
52
- // Alone on the machine is what every benchmark measures, so there is
53
- // nothing to correct.
54
- return { penalty: 1, measured: true, from: 0 };
55
- }
56
- if (!(measured instanceof Map) || measured.size === 0) {
57
- // Nothing measured: the honest multiplier is 1, and the caller's own log
58
- // says the prediction is uncorrected. Inventing a penalty would be the
59
- // same mistake as inventing a fill rate.
60
- return { penalty: 1, measured: false, from: 0 };
61
- }
62
- const exact = measured.get(others);
63
- if (Number.isFinite(exact) && exact > 0) {
64
- return { penalty: exact, measured: true, from: others };
65
- }
66
- // Beyond what was measured, hold the largest reading rather than continue the
67
- // curve. Two readings say nothing about the shape past them, and a budget
68
- // that extrapolates a memory bottleneck will be wrong in whichever direction
69
- // it guesses.
70
- let largestKey = 0;
71
- let largestValue = 1;
72
- for (const [key, value] of measured) {
73
- if (key <= others && key > largestKey && Number.isFinite(value) && value > 0) {
74
- largestKey = key;
75
- largestValue = value;
76
- }
77
- }
78
- if (largestKey === 0) {
79
- return { penalty: 1, measured: false, from: 0 };
80
- }
81
- return { penalty: largestValue, measured: true, from: largestKey };
82
- }
83
-
84
- /**
85
- * A cost, corrected for what else will be running.
86
- *
87
- * @param {number} costSecondsPerSecond - As the benchmarks priced it, alone.
88
- * @param {number} othersRunning
89
- * @param {ContentionPenalties | null} measured
90
- * @returns {number}
91
- */
92
- export function costWithContention(costSecondsPerSecond, othersRunning, measured) {
93
- if (!Number.isFinite(costSecondsPerSecond) || costSecondsPerSecond <= 0) {
94
- return costSecondsPerSecond;
95
- }
96
- return costSecondsPerSecond * contentionPenalty(othersRunning, measured).penalty;
97
- }
98
-
99
- /**
100
- * Turn measured decode speeds into penalties.
101
- *
102
- * @param {number} aloneSpeed - Decode speed with the machine to itself.
103
- * @param {Array<{ others: number, speed: number }>} beside - One reading per
104
- * number of other jobs running.
105
- * @returns {ContentionPenalties | null} Null when the alone reading is missing,
106
- * since every penalty is relative to it.
107
- */
108
- export function penaltiesFrom(aloneSpeed, beside) {
109
- if (!Number.isFinite(aloneSpeed) || aloneSpeed <= 0) {
110
- return null;
111
- }
112
- const penalties = new Map();
113
- for (const reading of Array.isArray(beside) ? beside : []) {
114
- const others = Number.isFinite(reading?.others) ? Math.round(reading.others) : null;
115
- const speed = Number(reading?.speed);
116
- if (others === null || others <= 0 || !Number.isFinite(speed) || speed <= 0) {
117
- continue;
118
- }
119
- // Cost is the reciprocal of speed, so the penalty is the reciprocal ratio.
120
- // A job that runs at half the speed costs twice as much.
121
- const penalty = aloneSpeed / speed;
122
- // Below 1 would mean the machine got FASTER for being busier. It happens in
123
- // the noise on a fast host, and it is not a discovery — it is a reading
124
- // that says there is no penalty to measure here.
125
- penalties.set(others, penalty > 1 ? penalty : 1);
126
- }
127
- return penalties.size > 0 ? penalties : null;
128
- }
1
+ /**
2
+ * @file What a second job costs on this machine — measured, because it is not
3
+ * the sum of two prices.
4
+ *
5
+ * Measured on the addon host 2026-08-18, decoding the same clip:
6
+ *
7
+ * | what else was running | decode speed | cost |
8
+ * |---|---|---|
9
+ * | nothing (cold) | 2.10-2.25x | 0.46 s/s |
10
+ * | nothing (hot, 68 °C) | 2.30-2.33x | 0.43 s/s |
11
+ * | one encoder | 0.79-0.90x | 1.18 s/s |
12
+ * | two encoders | 0.56-0.64x | 1.67 s/s |
13
+ *
14
+ * The same work costs **2.6× more** with one encoder beside it and 3.7× with
15
+ * two. Heat is not the cause — the hot idle machine was the fastest of all.
16
+ * Four cores share one narrow path to memory, and decoding and encoding both
17
+ * saturate it.
18
+ *
19
+ * That contradicts the shape of the budget, not just its constants. Everything
20
+ * in the quality offer adds seconds of work per second of content — this
21
+ * encode, plus that decode, plus what is already committed — and these readings
22
+ * say two jobs that each fit alone do not fit together. The availability
23
+ * correction in `available-share.js` does not cover it either: that subtracts
24
+ * the share of the machine taken by work nobody has been charged for, while
25
+ * this is our own work colliding with itself.
26
+ *
27
+ * So the penalty is measured rather than derived, at startup, on the host it
28
+ * describes — and beyond the range that was measured it does not extrapolate:
29
+ * it holds the largest reading and says so, because nothing here knows what a
30
+ * fourth job would do.
31
+ */
32
+
33
+ /**
34
+ * Penalties measured on this host, keyed by how many OTHER jobs were running.
35
+ *
36
+ * @typedef {Map<number, number>} ContentionPenalties
37
+ */
38
+
39
+ /**
40
+ * The multiplier for a job's cost when `othersRunning` other jobs share the
41
+ * machine.
42
+ *
43
+ * @param {number} othersRunning
44
+ * @param {ContentionPenalties | null} measured
45
+ * @returns {{ penalty: number, measured: boolean, from: number }}
46
+ * `from` is the reading it came from — equal to `othersRunning` when one was
47
+ * measured for exactly that many, and the largest available otherwise.
48
+ */
49
+ export function contentionPenalty(othersRunning, measured) {
50
+ const others = Number.isFinite(othersRunning) ? Math.max(0, Math.round(othersRunning)) : 0;
51
+ if (others === 0) {
52
+ // Alone on the machine is what every benchmark measures, so there is
53
+ // nothing to correct.
54
+ return { penalty: 1, measured: true, from: 0 };
55
+ }
56
+ if (!(measured instanceof Map) || measured.size === 0) {
57
+ // Nothing measured: the honest multiplier is 1, and the caller's own log
58
+ // says the prediction is uncorrected. Inventing a penalty would be the
59
+ // same mistake as inventing a fill rate.
60
+ return { penalty: 1, measured: false, from: 0 };
61
+ }
62
+ const exact = measured.get(others);
63
+ if (Number.isFinite(exact) && exact > 0) {
64
+ return { penalty: exact, measured: true, from: others };
65
+ }
66
+ // Beyond what was measured, hold the largest reading rather than continue the
67
+ // curve. Two readings say nothing about the shape past them, and a budget
68
+ // that extrapolates a memory bottleneck will be wrong in whichever direction
69
+ // it guesses.
70
+ let largestKey = 0;
71
+ let largestValue = 1;
72
+ for (const [key, value] of measured) {
73
+ if (key <= others && key > largestKey && Number.isFinite(value) && value > 0) {
74
+ largestKey = key;
75
+ largestValue = value;
76
+ }
77
+ }
78
+ if (largestKey === 0) {
79
+ return { penalty: 1, measured: false, from: 0 };
80
+ }
81
+ return { penalty: largestValue, measured: true, from: largestKey };
82
+ }
83
+
84
+ /**
85
+ * A cost, corrected for what else will be running.
86
+ *
87
+ * @param {number} costSecondsPerSecond - As the benchmarks priced it, alone.
88
+ * @param {number} othersRunning
89
+ * @param {ContentionPenalties | null} measured
90
+ * @returns {number}
91
+ */
92
+ export function costWithContention(costSecondsPerSecond, othersRunning, measured) {
93
+ if (!Number.isFinite(costSecondsPerSecond) || costSecondsPerSecond <= 0) {
94
+ return costSecondsPerSecond;
95
+ }
96
+ return costSecondsPerSecond * contentionPenalty(othersRunning, measured).penalty;
97
+ }
98
+
99
+ /**
100
+ * Turn measured decode speeds into penalties.
101
+ *
102
+ * @param {number} aloneSpeed - Decode speed with the machine to itself.
103
+ * @param {Array<{ others: number, speed: number }>} beside - One reading per
104
+ * number of other jobs running.
105
+ * @returns {ContentionPenalties | null} Null when the alone reading is missing,
106
+ * since every penalty is relative to it.
107
+ */
108
+ export function penaltiesFrom(aloneSpeed, beside) {
109
+ if (!Number.isFinite(aloneSpeed) || aloneSpeed <= 0) {
110
+ return null;
111
+ }
112
+ const penalties = new Map();
113
+ for (const reading of Array.isArray(beside) ? beside : []) {
114
+ const others = Number.isFinite(reading?.others) ? Math.round(reading.others) : null;
115
+ const speed = Number(reading?.speed);
116
+ if (others === null || others <= 0 || !Number.isFinite(speed) || speed <= 0) {
117
+ continue;
118
+ }
119
+ // Cost is the reciprocal of speed, so the penalty is the reciprocal ratio.
120
+ // A job that runs at half the speed costs twice as much.
121
+ const penalty = aloneSpeed / speed;
122
+ // Below 1 would mean the machine got FASTER for being busier. It happens in
123
+ // the noise on a fast host, and it is not a discovery — it is a reading
124
+ // that says there is no penalty to measure here.
125
+ penalties.set(others, penalty > 1 ? penalty : 1);
126
+ }
127
+ return penalties.size > 0 ? penalties : null;
128
+ }