@torrent-tv/proxy 2.36.2 → 2.37.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,13 @@
1
+ ## 2.37.1
2
+
3
+ - **Fix**: The cost of a seek no longer counts against the quality offer. `requiredSpeed` — the speed a step must sustain to survive a swarm — is built from the reader's interruptions, and the wait on the first piece after a JUMP is not one of them: those pieces have not been asked for yet and the encoder is restarting, so it measures the move, not the supply. Measured 2026-08-18: `proxy now offers 720p` landed 131 ms after a seek, collapsing a five-rung menu to one while the player was already hunting for a fragment, and another session churned `640p` → `640p 540p` → `640p 240p`. The wait is still reported, saying plainly that it belongs to the jump and is not counted, so a gap in the history cannot be mistaken for a swarm that never made the reader wait.
4
+
5
+ ## 2.37.0
6
+
7
+ - **Fix**: The segment the viewer seeks TO is no longer refused as stale. A seek bumps the wait epoch so requests made for the position being LEFT stop being held, and the epoch alone cannot tell those apart from the request for the position just arrived at — hls.js asks for it within milliseconds of the seek, and it raced the bump. Measured 2026-08-18: a seek to 1061.0 s, `segment-00101` answered 503 twice within 80 ms, the player never asked for it again, and instead re-fetched `a/0/segment-00103` and `a/0/segment-00104` **737 and 736 times over 149 seconds** — about half a gigabyte of the same two segments — while the picture stood at `t=1061.0s readyState=1` until the session ended. A held request is now released only when its segment lies behind where the viewer now is, or so far ahead that the running encode will not reach it; anything between is what the viewer is waiting for and is held.
8
+ - **New**: The log survives the container. `--log-file <path>` writes every line to a file as well as the console, appending across restarts and rotating at 32 MB with one previous turn kept. The console is the container's stdout, and the container is exactly what does not survive a crash: thirteen SIGSEGVs on 2026-08-18 each had the watchdog recreate it, taking every line before the crash away, and a deploy of ours destroyed the evidence for two field reports the same day. Opt-in and named by the caller, so nothing here assumes Home Assistant — the addon points it at `/data`.
9
+ - **New**: A refusal says what it refused. `[hold] <segment> refused: the viewer is at <position>s and this is not the segment there`, and a request kept across a seek says so too. The old line said only "superseded", which is why the freeze above took a day to explain.
10
+
1
11
  ## 2.36.2
2
12
 
3
13
  - **Fix**: A live session no longer answers 404 to the master playlist it has just published. The browser is handed `master.m3u8` when the session is created, and which rungs are worth OFFERING is recomputed every five seconds — so on 2026-08-18 a five-rung offer became a one-rung offer **192 ms** after creation (the session's own encoder started, charging the contention penalty of 2.35.0, and the first supply reading raised the bar of 2.36.0 from 1.00x to 1.06x), `buildMasterPlaylist` returned null for having fewer than two rungs, and the master answered 404. hls.js treats that as fatal and unrecoverable, so nothing played at all. The master now lists what CAN be spliced onto this session's cut grid — a fact about the source, settled once — while the live judgement stays where it belongs, in `offeredHeights` and in every progress report, which is what the viewer's menu already follows. The variant routes honour the published set too, so a quality switch can no longer meet a 404 on a rung the master named.
package/bin/cli.js CHANGED
@@ -27,7 +27,7 @@ import { collectHealthMetrics } from "../services/health-collector.js";
27
27
  import { createPortMapper } from "../services/port-mapper.js";
28
28
  import { classifyNat } from "../services/nat-classifier.js";
29
29
  import { DEFAULT_SEGMENT_FORMAT_ID, SEGMENT_FORMAT_IDS } from "../services/segment-formats/index.js";
30
- import { logger } from "../utils/logger.js";
30
+ import { logToFile, logger } from "../utils/logger.js";
31
31
 
32
32
  const require = createRequire(import.meta.url);
33
33
  const { version: PROXY_VERSION } = require("../package.json");
@@ -85,6 +85,10 @@ program
85
85
  "--state-dir <path>",
86
86
  "Where to keep what this host has measured about itself (default: beside the installed proxy)"
87
87
  )
88
+ .option(
89
+ "--log-file <path>",
90
+ "Also write the log to this file, so a crash or an update does not take it with them"
91
+ )
88
92
  .option(
89
93
  "--segment-format <format>",
90
94
  `HLS output container: ${SEGMENT_FORMAT_IDS.join(" | ")}`,
@@ -272,6 +276,7 @@ async function shutdown(signal) {
272
276
  }
273
277
 
274
278
  try {
279
+ logToFile(options.logFile);
275
280
  if (transcodeAudio) {
276
281
  assertFfmpegAvailability();
277
282
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@torrent-tv/proxy",
3
- "version": "2.36.2",
3
+ "version": "2.37.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": {
@@ -1,180 +1,201 @@
1
- import { logger } from "../../../utils/logger.js";
2
-
3
- /**
4
- * How long a request for a not-yet-produced file is held before answering with
5
- * a retryable 503.
6
- *
7
- * MEASUREMENT MODE (2026-08-02): deliberately far above any plausible player
8
- * deadline, so OUR limit never fires first. Whatever ends the wait is then the
9
- * player's own behaviour — which is exactly what we need to observe. The
10
- * `[hold]` log line at the call site records, per request, whether the segment
11
- * arrived, whether we gave up, or whether the CLIENT aborted, and after how
12
- * long.
13
- *
14
- * The previous value (2 s) was chosen to dodge a reported iOS AVPlayer ~3.5 s
15
- * response-header deadline. That deadline never appears in our own logs (no
16
- * -12889 across six hours of production logs), and every reference project
17
- * holds rather than refusing: Jellyfin and hls-vod-too hold unbounded,
18
- * hls-media-server holds 10 s. The early refusal is what made the player probe
19
- * scattered positions, which then steered the encoder off target. Choose the
20
- * final value from what this measurement shows, not from a number read
21
- * elsewhere.
22
- */
23
- const SEGMENT_WAIT_MS = 60_000;
24
-
25
- /**
26
- * Serve HLS playlist and segment files from an active transcode session.
27
- *
28
- * Briefly waits for the requested file to appear, then answers with a
29
- * retryable 503 rather than holding the connection, so clients — in
30
- * particular iOS's native HLS player — never hit their own response
31
- * deadline while a segment is still being produced.
32
- *
33
- * GET /transcode/:sessionId/:fileName
34
- *
35
- * @param {import("fastify").FastifyRequest} req
36
- * @param {import("fastify").FastifyReply} reply
37
- * @param {{ hlsSessionManager: import("../../../services/hls-session-manager.js").HlsSessionManager }} deps
38
- * @returns {Promise<void>}
39
- */
40
- export async function handleTranscodeSessionFileGet(req, reply, { hlsSessionManager }) {
41
- const sessionId = typeof req.params.sessionId === "string" ? req.params.sessionId : "";
42
- const fileName = typeof req.params.fileName === "string" ? req.params.fileName : "";
43
- return serveSessionFile(req, reply, { hlsSessionManager, sessionId, fileName });
44
- }
45
-
46
- /**
47
- * Serve one playlist or segment from a named session.
48
- *
49
- * Split from the route above because the variant route
50
- * (`/transcode/:sessionId/v/:height/:fileName`) serves the same files from
51
- * another session of the same family, and must hold, log and answer them
52
- * identically — a switch of quality must not go through a different code path
53
- * from the stream it switches away from.
54
- *
55
- * @param {import("fastify").FastifyRequest} req
56
- * @param {import("fastify").FastifyReply} reply
57
- * @param {{ hlsSessionManager: import("../../../services/hls-session-manager.js").HlsSessionManager, sessionId: string, fileName: string }} params
58
- * @returns {Promise<void>}
59
- */
60
- export async function serveSessionFile(req, reply, { hlsSessionManager, sessionId, fileName }) {
61
- // Hold the request only briefly, then answer "retry" instead of waiting for
62
- // the segment. iOS's native HLS player (AVPlayer) enforces a hard ~3.5 s
63
- // deadline on RESPONSE HEADERS and raises -12889 ("No response for media
64
- // file") when it passes — it then cancels in-flight requests, probes
65
- // neighbouring positions and can restart the stream from the beginning. That
66
- // is exactly the post-seek "player thrashing" seen in the field, because a
67
- // seek restarts ffmpeg and the first segment then takes far longer than 3.5 s
68
- // to appear. Holding the connection for 30 s (as this did) guaranteed the
69
- // timeout on every seek. A short hold keeps the fast path intact (a ready or
70
- // nearly-ready segment is still served on the first request) while a slow one
71
- // gets a prompt retryable answer, which resets the player's own deadline.
72
- // hls.js is unaffected: it consumes the 503 through its retry policy, whose
73
- // budget the client widens to match (see hls-player.js fragLoadPolicy).
74
- // Instrumented wait. `clientAborted` flips when the player drops the
75
- // connection while we are still holding it — the single most informative
76
- // signal about its real patience, and observable only from this side.
77
- const holdStartedAt = Date.now();
78
- let clientAborted = false;
79
- const onClientAbort = () => { clientAborted = true; };
80
- req.raw.on("close", onClientAbort);
81
-
82
- const result = await waitForSessionFile(hlsSessionManager, sessionId, fileName, SEGMENT_WAIT_MS);
83
-
84
- req.raw.off("close", onClientAbort);
85
- const heldMs = Date.now() - holdStartedAt;
86
- if (result.isPlaylist !== true) {
87
- const outcome = clientAborted
88
- ? "client-aborted"
89
- : result.kind === "ok" ? "served" : result.kind;
90
- logger.info(`[hold] ${fileName} ${outcome} after ${heldMs}ms`);
91
- }
92
-
93
- if (result.kind === "not-found") {
94
- return reply.code(404).send({ error: "Transcode session file was not found." });
95
- }
96
- if (result.kind === "superseded") {
97
- // The viewer moved while this was being held. Answer at once so the player
98
- // can ask for where it is now; `Retry-After: 0` because there is nothing to
99
- // wait for this segment is simply no longer the one being watched.
100
- reply.header("Retry-After", "0");
101
- return reply.code(503).send({ error: "Superseded by a seek." });
102
- }
103
- if (result.kind === "warming-up") {
104
- // The segment is still being produced (e.g. just after a seek-restart).
105
- // Return a retryable 503 — never 202, which hls.js cannot consume as a
106
- // media segment — so the player retries the fetch shortly.
107
- reply.header("Retry-After", "1");
108
- // `Retry-After` tells the player to re-request THIS segment after a short
109
- // pause. Without it a bare 503 reads as "nothing here", and the player goes
110
- // looking elsewhere: because our synthetic VOD playlist lists every segment
111
- // of the file, it believes they all exist and SCANS them (field log: one
112
- // user seek produced probes at #617, #717, #732…). That scan is what used
113
- // to steer the encoder off the real target. Whether iOS's native player
114
- // honours the hint is not guaranteed its behaviour is closed — but this
115
- // is the standard, correct way to say "wait, don't look elsewhere", and
116
- // hls.js already retries the same fragment regardless.
117
- reply.header("Retry-After", "1");
118
- return reply.code(503).send({ error: "Transcode segment is still being produced." });
119
- }
120
- if (result.kind === "failed") {
121
- return reply.code(500).send({ error: result.message });
122
- }
123
-
124
- if (result.isPlaylist) {
125
- reply.header("Cache-Control", "no-store");
126
- } else {
127
- reply.header("Cache-Control", "public, max-age=60");
128
- }
129
- reply.header("Content-Type", result.contentType);
130
- return reply.send(result.stream);
131
- }
132
-
133
- /**
134
- * Poll `hlsSessionManager.getFileStream()` until the file is available,
135
- * the session fails, or the timeout elapses.
136
- *
137
- * @param {import("../../../services/hls-session-manager.js").HlsSessionManager} hlsSessionManager
138
- * @param {string} sessionId
139
- * @param {string} fileName
140
- * @param {number} timeoutMs
141
- * @returns {Promise<Awaited<ReturnType<import("../../../services/hls-session-manager.js").HlsSessionManager["getFileStream"]>>>}
142
- */
143
- export async function waitForSessionFile(hlsSessionManager, sessionId, fileName, timeoutMs) {
144
- const startedAt = Date.now();
145
- // One sequence number for THIS request, reused by every poll below, so the
146
- // session can tell a newly-arrived request apart from an old one polling
147
- // again see HlsSessionManager#ensureEncodingFor for the encoder ping-pong
148
- // this prevents when one seek-bar scrub fires several segment requests.
149
- const requestSeq = hlsSessionManager.nextRequestSeq(sessionId);
150
- // The viewer's position when this request was made. A seek makes every held
151
- // request stale — it asks for a segment nobody is going to watch — and hls.js
152
- // keeps only ONE fragment load outstanding, so holding on blocks the request
153
- // the player actually needs now. Measured: 57 s of a 58 s backward seek was
154
- // this wait, and the segment the viewer wanted took 15 ms once it was asked
155
- // for.
156
- const seekEpoch = hlsSessionManager.seekEpoch(sessionId);
157
- while (Date.now() - startedAt < timeoutMs) {
158
- const result = await hlsSessionManager.getFileStream(sessionId, fileName, { requestSeq });
159
- if (result.kind !== "warming-up") {
160
- return result;
161
- }
162
- if (hlsSessionManager.seekEpoch(sessionId) !== seekEpoch) {
163
- return { kind: "superseded" };
164
- }
165
- await delay(300);
166
- }
167
- return { kind: "warming-up" };
168
- }
169
-
170
- /**
171
- * Resolve after a given number of milliseconds.
172
- *
173
- * @param {number} ms
174
- * @returns {Promise<void>}
175
- */
176
- function delay(ms) {
177
- return new Promise((resolve) => {
178
- setTimeout(resolve, ms);
179
- });
180
- }
1
+ import { logger } from "../../../utils/logger.js";
2
+
3
+ /**
4
+ * How long a request for a not-yet-produced file is held before answering with
5
+ * a retryable 503.
6
+ *
7
+ * MEASUREMENT MODE (2026-08-02): deliberately far above any plausible player
8
+ * deadline, so OUR limit never fires first. Whatever ends the wait is then the
9
+ * player's own behaviour — which is exactly what we need to observe. The
10
+ * `[hold]` log line at the call site records, per request, whether the segment
11
+ * arrived, whether we gave up, or whether the CLIENT aborted, and after how
12
+ * long.
13
+ *
14
+ * The previous value (2 s) was chosen to dodge a reported iOS AVPlayer ~3.5 s
15
+ * response-header deadline. That deadline never appears in our own logs (no
16
+ * -12889 across six hours of production logs), and every reference project
17
+ * holds rather than refusing: Jellyfin and hls-vod-too hold unbounded,
18
+ * hls-media-server holds 10 s. The early refusal is what made the player probe
19
+ * scattered positions, which then steered the encoder off target. Choose the
20
+ * final value from what this measurement shows, not from a number read
21
+ * elsewhere.
22
+ */
23
+ const SEGMENT_WAIT_MS = 60_000;
24
+
25
+ /**
26
+ * Serve HLS playlist and segment files from an active transcode session.
27
+ *
28
+ * Briefly waits for the requested file to appear, then answers with a
29
+ * retryable 503 rather than holding the connection, so clients — in
30
+ * particular iOS's native HLS player — never hit their own response
31
+ * deadline while a segment is still being produced.
32
+ *
33
+ * GET /transcode/:sessionId/:fileName
34
+ *
35
+ * @param {import("fastify").FastifyRequest} req
36
+ * @param {import("fastify").FastifyReply} reply
37
+ * @param {{ hlsSessionManager: import("../../../services/hls-session-manager.js").HlsSessionManager }} deps
38
+ * @returns {Promise<void>}
39
+ */
40
+ export async function handleTranscodeSessionFileGet(req, reply, { hlsSessionManager }) {
41
+ const sessionId = typeof req.params.sessionId === "string" ? req.params.sessionId : "";
42
+ const fileName = typeof req.params.fileName === "string" ? req.params.fileName : "";
43
+ return serveSessionFile(req, reply, { hlsSessionManager, sessionId, fileName });
44
+ }
45
+
46
+ /**
47
+ * Serve one playlist or segment from a named session.
48
+ *
49
+ * Split from the route above because the variant route
50
+ * (`/transcode/:sessionId/v/:height/:fileName`) serves the same files from
51
+ * another session of the same family, and must hold, log and answer them
52
+ * identically — a switch of quality must not go through a different code path
53
+ * from the stream it switches away from.
54
+ *
55
+ * @param {import("fastify").FastifyRequest} req
56
+ * @param {import("fastify").FastifyReply} reply
57
+ * @param {{ hlsSessionManager: import("../../../services/hls-session-manager.js").HlsSessionManager, sessionId: string, fileName: string }} params
58
+ * @returns {Promise<void>}
59
+ */
60
+ export async function serveSessionFile(req, reply, { hlsSessionManager, sessionId, fileName }) {
61
+ // Hold the request only briefly, then answer "retry" instead of waiting for
62
+ // the segment. iOS's native HLS player (AVPlayer) enforces a hard ~3.5 s
63
+ // deadline on RESPONSE HEADERS and raises -12889 ("No response for media
64
+ // file") when it passes — it then cancels in-flight requests, probes
65
+ // neighbouring positions and can restart the stream from the beginning. That
66
+ // is exactly the post-seek "player thrashing" seen in the field, because a
67
+ // seek restarts ffmpeg and the first segment then takes far longer than 3.5 s
68
+ // to appear. Holding the connection for 30 s (as this did) guaranteed the
69
+ // timeout on every seek. A short hold keeps the fast path intact (a ready or
70
+ // nearly-ready segment is still served on the first request) while a slow one
71
+ // gets a prompt retryable answer, which resets the player's own deadline.
72
+ // hls.js is unaffected: it consumes the 503 through its retry policy, whose
73
+ // budget the client widens to match (see hls-player.js fragLoadPolicy).
74
+ // Instrumented wait. `clientAborted` flips when the player drops the
75
+ // connection while we are still holding it — the single most informative
76
+ // signal about its real patience, and observable only from this side.
77
+ const holdStartedAt = Date.now();
78
+ let clientAborted = false;
79
+ const onClientAbort = () => { clientAborted = true; };
80
+ req.raw.on("close", onClientAbort);
81
+
82
+ const result = await waitForSessionFile(hlsSessionManager, sessionId, fileName, SEGMENT_WAIT_MS);
83
+
84
+ req.raw.off("close", onClientAbort);
85
+ const heldMs = Date.now() - holdStartedAt;
86
+ if (result.isPlaylist !== true) {
87
+ const outcome = clientAborted
88
+ ? "client-aborted"
89
+ : result.kind === "ok" ? "served" : result.kind;
90
+ logger.info(`[hold] ${fileName} ${outcome} after ${heldMs}ms`);
91
+ }
92
+
93
+ if (result.kind === "not-found") {
94
+ return reply.code(404).send({ error: "Transcode session file was not found." });
95
+ }
96
+ if (result.kind === "superseded") {
97
+ // The viewer moved while this was being held, and this segment is not the
98
+ // one they moved TO that case is kept and waited out, see
99
+ // `waitForSessionFile`. Answer at once so the player can ask for where it
100
+ // is now; `Retry-After: 0` because there is nothing to wait for.
101
+ //
102
+ // Named in the log, because a refusal that says only "superseded" is what
103
+ // made the 2026-08-18 freeze take a day to explain: the player was refused
104
+ // the segment at its own seek target and nothing recorded which segment or
105
+ // where the viewer was.
106
+ logger.info(
107
+ `[hold] ${fileName} refused: the viewer is at ` +
108
+ `${hlsSessionManager.viewerPositionOf(sessionId).toFixed(1)}s and this is not the segment there`
109
+ );
110
+ reply.header("Retry-After", "0");
111
+ return reply.code(503).send({ error: "Superseded by a seek." });
112
+ }
113
+ if (result.kind === "warming-up") {
114
+ // The segment is still being produced (e.g. just after a seek-restart).
115
+ // Return a retryable 503 never 202, which hls.js cannot consume as a
116
+ // media segment — so the player retries the fetch shortly.
117
+ reply.header("Retry-After", "1");
118
+ // `Retry-After` tells the player to re-request THIS segment after a short
119
+ // pause. Without it a bare 503 reads as "nothing here", and the player goes
120
+ // looking elsewhere: because our synthetic VOD playlist lists every segment
121
+ // of the file, it believes they all exist and SCANS them (field log: one
122
+ // user seek produced probes at #617, #717, #732…). That scan is what used
123
+ // to steer the encoder off the real target. Whether iOS's native player
124
+ // honours the hint is not guaranteed — its behaviour is closed — but this
125
+ // is the standard, correct way to say "wait, don't look elsewhere", and
126
+ // hls.js already retries the same fragment regardless.
127
+ reply.header("Retry-After", "1");
128
+ return reply.code(503).send({ error: "Transcode segment is still being produced." });
129
+ }
130
+ if (result.kind === "failed") {
131
+ return reply.code(500).send({ error: result.message });
132
+ }
133
+
134
+ if (result.isPlaylist) {
135
+ reply.header("Cache-Control", "no-store");
136
+ } else {
137
+ reply.header("Cache-Control", "public, max-age=60");
138
+ }
139
+ reply.header("Content-Type", result.contentType);
140
+ return reply.send(result.stream);
141
+ }
142
+
143
+ /**
144
+ * Poll `hlsSessionManager.getFileStream()` until the file is available,
145
+ * the session fails, or the timeout elapses.
146
+ *
147
+ * @param {import("../../../services/hls-session-manager.js").HlsSessionManager} hlsSessionManager
148
+ * @param {string} sessionId
149
+ * @param {string} fileName
150
+ * @param {number} timeoutMs
151
+ * @returns {Promise<Awaited<ReturnType<import("../../../services/hls-session-manager.js").HlsSessionManager["getFileStream"]>>>}
152
+ */
153
+ export async function waitForSessionFile(hlsSessionManager, sessionId, fileName, timeoutMs) {
154
+ const startedAt = Date.now();
155
+ // One sequence number for THIS request, reused by every poll below, so the
156
+ // session can tell a newly-arrived request apart from an old one polling
157
+ // again see HlsSessionManager#ensureEncodingFor for the encoder ping-pong
158
+ // this prevents when one seek-bar scrub fires several segment requests.
159
+ const requestSeq = hlsSessionManager.nextRequestSeq(sessionId);
160
+ // The viewer's position when this request was made. A seek makes every held
161
+ // request stale — it asks for a segment nobody is going to watch — and hls.js
162
+ // keeps only ONE fragment load outstanding, so holding on blocks the request
163
+ // the player actually needs now. Measured: 57 s of a 58 s backward seek was
164
+ // this wait, and the segment the viewer wanted took 15 ms once it was asked
165
+ // for.
166
+ let seekEpoch = hlsSessionManager.seekEpoch(sessionId);
167
+ while (Date.now() - startedAt < timeoutMs) {
168
+ const result = await hlsSessionManager.getFileStream(sessionId, fileName, { requestSeq });
169
+ if (result.kind !== "warming-up") {
170
+ return result;
171
+ }
172
+ if (hlsSessionManager.seekEpoch(sessionId) !== seekEpoch) {
173
+ // A seek moved the epoch. Whether THIS request is stale depends on which
174
+ // segment it asks for: the one the viewer has just landed on races the
175
+ // seek notification and would otherwise be refused at the exact moment it
176
+ // is needed — measured 2026-08-18, two 503s within 80 ms on the segment
177
+ // at the seek target, after which the player never asked for it again.
178
+ if (!hlsSessionManager.requestStillWanted(sessionId, fileName)) {
179
+ return { kind: "superseded" };
180
+ }
181
+ logger.info(
182
+ `[hold] ${fileName} kept across a seek: it is the segment the viewer now needs`
183
+ );
184
+ seekEpoch = hlsSessionManager.seekEpoch(sessionId);
185
+ }
186
+ await delay(300);
187
+ }
188
+ return { kind: "warming-up" };
189
+ }
190
+
191
+ /**
192
+ * Resolve after a given number of milliseconds.
193
+ *
194
+ * @param {number} ms
195
+ * @returns {Promise<void>}
196
+ */
197
+ function delay(ms) {
198
+ return new Promise((resolve) => {
199
+ setTimeout(resolve, ms);
200
+ });
201
+ }
@@ -7266,6 +7266,59 @@ export class HlsSessionManager {
7266
7266
  return session?.waitEpoch ?? 0;
7267
7267
  }
7268
7268
 
7269
+ /**
7270
+ * Where the viewer of this session is, in seconds.
7271
+ *
7272
+ * Exists so a refusal can name it. A log line that says only "superseded"
7273
+ * cannot be read afterwards: it does not say what was refused or against what
7274
+ * position, which is exactly what the 2026-08-18 investigation lacked.
7275
+ *
7276
+ * @param {string} sessionId
7277
+ * @returns {number} Zero when the session is gone or nothing has been reported.
7278
+ */
7279
+ viewerPositionOf(sessionId) {
7280
+ const session = isSafeSessionId(sessionId) ? this.sessionsById.get(sessionId) : null;
7281
+ const position = Number(session?.viewerPositionSeconds);
7282
+ return Number.isFinite(position) ? position : 0;
7283
+ }
7284
+
7285
+ /**
7286
+ * Whether a held request is for a segment the viewer STILL needs.
7287
+ *
7288
+ * The epoch alone says a seek happened; it cannot say whether this particular
7289
+ * request was made for the position left behind or for the one just arrived
7290
+ * at. That distinction is the whole of the failure measured 2026-08-18: the
7291
+ * viewer seeked to 1061.0 s, the request for `segment-00101` — the segment AT
7292
+ * that position — raced the seek notification, the epoch moved underneath it,
7293
+ * and it was answered 503 twice within 80 ms. The player then hunted at
7294
+ * sn=105-107, never came back to 101, and looped two audio segments 1473
7295
+ * times over 149 s while the picture stood still.
7296
+ *
7297
+ * A request is stale when its segment lies behind where the viewer now is, or
7298
+ * so far ahead that the running encode will not reach it. Anything between is
7299
+ * exactly what the viewer is waiting for, and holding it is the point.
7300
+ *
7301
+ * @param {string} sessionId
7302
+ * @param {string} fileName
7303
+ * @returns {boolean} True when the request should keep waiting.
7304
+ */
7305
+ requestStillWanted(sessionId, fileName) {
7306
+ const session = isSafeSessionId(sessionId) ? this.sessionsById.get(sessionId) : null;
7307
+ if (!session) {
7308
+ return false;
7309
+ }
7310
+ const index = session.segmentFormat?.segmentIndexFromName?.(fileName) ?? -1;
7311
+ if (!(index >= 0)) {
7312
+ return true; // a playlist or an init segment belongs to no position
7313
+ }
7314
+ const position = Number(session.viewerPositionSeconds);
7315
+ if (!Number.isFinite(position)) {
7316
+ return true; // nothing said where the viewer is; refusing would be a guess
7317
+ }
7318
+ const at = this.#segmentIndexForTime(session, position);
7319
+ return index >= at && index <= at + MAX_LOOKAHEAD_SEGMENTS;
7320
+ }
7321
+
7269
7322
  /**
7270
7323
  * Open a read stream for an HLS segment or playlist file from a session.
7271
7324
  *
@@ -484,6 +484,20 @@ export async function* readFragments({
484
484
  // process.
485
485
  const readerId = `read-${(readerSequence += 1)}`;
486
486
 
487
+ /**
488
+ * Set when the window JUMPS, cleared by the first wait after it.
489
+ *
490
+ * The wait that follows a jump is the cost of the jump: the pieces at the new
491
+ * position have not been asked for yet, and the encoder is restarting. It is
492
+ * not evidence about how well this swarm SUSTAINS a read, which is the only
493
+ * thing `requiredSpeed` is about — and letting it in is what collapsed the
494
+ * quality offer 131 ms after the seek measured on 2026-08-18, refusing every
495
+ * re-encoded rung on the strength of one jump.
496
+ *
497
+ * @type {boolean}
498
+ */
499
+ let waitBelongsToJump = false;
500
+
487
501
  const moveWindowTo = (pieceIndex) => {
488
502
  const next = readWindowFor({ pieceIndex, lastPiece, windowPieces });
489
503
  if (window && window.from === next.from && window.to === next.to) {
@@ -501,6 +515,7 @@ export async function* readFragments({
501
515
  // fills the store while the encoder runs ahead of the viewer.
502
516
  store.protectRange?.(readerId, next.from, next.to);
503
517
  if (isJump) {
518
+ waitBelongsToJump = true;
504
519
  // A jump — a seek, not the window sliding along — can land on pieces that
505
520
  // are already downloaded but have been spilled to disk. Bring the whole
506
521
  // window back at once instead of one disk round trip per piece as the
@@ -603,7 +618,18 @@ export async function* readFragments({
603
618
  // short, an immediate hit means it is longer than it needs to be. Applied
604
619
  // before the logging below so the line reports the window the next piece
605
620
  // will actually use.
606
- noteSupplyWait(`${torrent?.infoHash ?? "?"}/${file?.name ?? "?"}`, file?.name ?? "", waitedMs);
621
+ if (waitBelongsToJump) {
622
+ // Recorded nowhere: see `waitBelongsToJump`. Said out loud, because a
623
+ // gap in the supply history is otherwise indistinguishable from a swarm
624
+ // that never made the reader wait.
625
+ logger.info(
626
+ `piece-reader: ${waitedMs}ms on the first piece after a jump — the cost of moving, ` +
627
+ `not of this swarm's supply, so it is not counted against the quality offer`
628
+ );
629
+ waitBelongsToJump = false;
630
+ } else {
631
+ noteSupplyWait(`${torrent?.infoHash ?? "?"}/${file?.name ?? "?"}`, file?.name ?? "", waitedMs);
632
+ }
607
633
  const widened = nextWindowPieces({
608
634
  current: windowPieces,
609
635
  base: basePieces,
@@ -0,0 +1,106 @@
1
+ /**
2
+ * @file The segment the viewer just seeked TO must not be refused as stale.
3
+ *
4
+ * A seek bumps the wait epoch so that requests made for the position being left
5
+ * stop being held. The request for the position being ARRIVED at races that
6
+ * bump: hls.js asks for the new segment within milliseconds, and if the epoch
7
+ * moves underneath it, the epoch check alone cannot tell the two apart.
8
+ *
9
+ * Measured 2026-08-18: the viewer seeked to 1061.0 s, `segment-00101` — the
10
+ * segment at that very position — was answered 503 twice within 80 ms, the
11
+ * player never asked for it again, and instead looped `a/0/segment-00103` and
12
+ * `a/0/segment-00104` 737 and 736 times over 149 seconds while the picture
13
+ * stood at `t=1061.0s readyState=1`. The session ended without another frame.
14
+ */
15
+
16
+ import test from "node:test";
17
+ import assert from "node:assert/strict";
18
+ import { mkdtemp, rm } from "node:fs/promises";
19
+ import os from "node:os";
20
+ import path from "node:path";
21
+ import { HlsSessionManager } from "../services/hls-session-manager.js";
22
+ import { fmp4Format } from "../services/segment-formats/fmp4.js";
23
+
24
+ const SESSION_ID = "aaaaaaaa-1111-2222-3333-444444444444";
25
+ const SEGMENT_SECONDS = 10.4;
26
+ const SEEK_TO_SECONDS = 1061;
27
+ const SEGMENT_AT_SEEK = Math.floor(SEEK_TO_SECONDS / SEGMENT_SECONDS);
28
+
29
+ /**
30
+ * A session whose viewer has just landed at {@link SEEK_TO_SECONDS}.
31
+ *
32
+ * @returns {Promise<{ manager: HlsSessionManager, dirPath: string }>}
33
+ */
34
+ async function managerAfterSeek() {
35
+ const dirPath = await mkdtemp(path.join(os.tmpdir(), "seek-target-"));
36
+ const manager = new HlsSessionManager({
37
+ enabled: true,
38
+ ffmpegBin: "ffmpeg",
39
+ localBindHost: "127.0.0.1",
40
+ localPort: 9090
41
+ });
42
+ manager.sessionsById.set(SESSION_ID, {
43
+ id: SESSION_ID,
44
+ dirPath,
45
+ state: "ready",
46
+ fileName: "film.mkv",
47
+ startedAt: Date.now(),
48
+ lastAccessedAt: Date.now(),
49
+ consumers: new Set(),
50
+ segmentFormat: fmp4Format,
51
+ transcodeVideo: false,
52
+ useSyntheticPlaylist: true,
53
+ segmentBoundaries: Array.from({ length: 400 }, (_, index) => index * SEGMENT_SECONDS),
54
+ segmentCount: 399,
55
+ encodeStartIndex: SEGMENT_AT_SEEK,
56
+ waitEpoch: 1,
57
+ viewerPositionSeconds: SEEK_TO_SECONDS,
58
+ runState: "PRODUCING"
59
+ });
60
+ return { manager, dirPath };
61
+ }
62
+
63
+ test("the segment at the seek target is still wanted", async () => {
64
+ const { manager, dirPath } = await managerAfterSeek();
65
+
66
+ assert.equal(
67
+ manager.requestStillWanted(SESSION_ID, `segment-${String(SEGMENT_AT_SEEK).padStart(5, "0")}.mp4`),
68
+ true,
69
+ "this is the segment the viewer is waiting for; refusing it is what froze the field session"
70
+ );
71
+
72
+ await rm(dirPath, { recursive: true, force: true });
73
+ });
74
+
75
+ test("a segment behind the viewer is not wanted any more", async () => {
76
+ const { manager, dirPath } = await managerAfterSeek();
77
+
78
+ assert.equal(
79
+ manager.requestStillWanted(SESSION_ID, `segment-${String(SEGMENT_AT_SEEK - 3).padStart(5, "0")}.mp4`),
80
+ false,
81
+ "a request for the position the viewer left is exactly what the epoch exists to release"
82
+ );
83
+
84
+ await rm(dirPath, { recursive: true, force: true });
85
+ });
86
+
87
+ test("a segment far beyond what the run will reach is not wanted", async () => {
88
+ const { manager, dirPath } = await managerAfterSeek();
89
+
90
+ assert.equal(
91
+ manager.requestStillWanted(SESSION_ID, `segment-${String(SEGMENT_AT_SEEK + 200).padStart(5, "0")}.mp4`),
92
+ false,
93
+ "nothing will produce it before the viewer moves again"
94
+ );
95
+
96
+ await rm(dirPath, { recursive: true, force: true });
97
+ });
98
+
99
+ test("a playlist belongs to no position and is never stale", async () => {
100
+ const { manager, dirPath } = await managerAfterSeek();
101
+
102
+ assert.equal(manager.requestStillWanted(SESSION_ID, "index.m3u8"), true);
103
+ assert.equal(manager.requestStillWanted(SESSION_ID, "init.mp4"), true);
104
+
105
+ await rm(dirPath, { recursive: true, force: true });
106
+ });
@@ -51,6 +51,11 @@ test("a seek releases a held segment request instead of running out the hold", a
51
51
  const hlsSessionManager = {
52
52
  nextRequestSeq: () => 1,
53
53
  seekEpoch: () => epoch,
54
+ // The viewer seeked AWAY from this segment, so it is genuinely stale. A
55
+ // request for the segment they seeked TO is kept instead — see
56
+ // `test/seek-target-not-superseded.test.js`.
57
+ requestStillWanted: () => false,
58
+ viewerPositionOf: () => 0,
54
59
  async getFileStream() {
55
60
  polls += 1;
56
61
  // The viewer moves while this request is being held.
package/utils/logger.js CHANGED
@@ -1,13 +1,43 @@
1
1
  /**
2
- * @file Centralised console logger for the proxy process.
2
+ * @file Centralised logger for the proxy process — to the console always, and
3
+ * to a file when one is named.
3
4
  *
4
- * All messages are prefixed with `[proxy-client]` and coloured with chalk
5
- * for consistent, readable terminal output.
5
+ * All messages are prefixed with `[proxy-client]` and coloured with chalk for
6
+ * consistent, readable terminal output.
7
+ *
8
+ * **Why a file at all.** The console is the container's stdout, and the
9
+ * container does not survive what we most need to read about. On 2026-08-18 the
10
+ * proxy died thirteen times with SIGSEGV; each death had Home Assistant's
11
+ * watchdog RECREATE the container, and every line leading up to the crash went
12
+ * with it. The same day a deploy of ours destroyed the evidence for two field
13
+ * reports that were being investigated at the time. A log that disappears
14
+ * exactly when something goes wrong is not a log, and no amount of care in
15
+ * choosing what to print compensates for it.
16
+ *
17
+ * The file is opt-in and named by the caller (`--log-file`), so nothing here
18
+ * assumes Home Assistant or any other host: the addon points it at `/data`,
19
+ * which survives restarts and updates, and a bare npm or Docker run may point
20
+ * it anywhere or leave it off.
6
21
  */
7
22
 
23
+ import { createWriteStream, renameSync, statSync } from "node:fs";
8
24
  import chalk from "chalk";
9
25
 
10
26
  const PREFIX = "[proxy-client]";
27
+ /**
28
+ * When the file is rotated, and how many turns are kept.
29
+ *
30
+ * One turn holds a few hours of a busy session at the current rate; two turns
31
+ * therefore cover a night's worth of restarts, which is the span a morning
32
+ * report asks about. Bounded because the addon's `/data` is the owner's disk.
33
+ */
34
+ const MAX_FILE_BYTES = 32 * 1024 * 1024;
35
+
36
+ /** @type {import("node:fs").WriteStream | null} */
37
+ let fileStream = null;
38
+ /** @type {string} */
39
+ let filePath = "";
40
+ let writtenBytes = 0;
11
41
 
12
42
  /**
13
43
  * Return the current time as a compact ISO-8601 (UTC) string, e.g.
@@ -20,6 +50,61 @@ function ts() {
20
50
  return new Date().toISOString().slice(11, 23); // "HH:MM:SS.mmm" (UTC)
21
51
  }
22
52
 
53
+ /**
54
+ * Start writing every message to a file as well as the console.
55
+ *
56
+ * Appends: a restart must not erase what led up to it, which is the entire
57
+ * reason this exists. Failures are reported once and then ignored — a proxy
58
+ * that cannot write its log still has a viewer to serve.
59
+ *
60
+ * @param {string} pathToFile - Empty or absent leaves logging console-only.
61
+ * @returns {void}
62
+ */
63
+ export function logToFile(pathToFile) {
64
+ if (typeof pathToFile !== "string" || pathToFile.length === 0) {
65
+ return;
66
+ }
67
+ try {
68
+ filePath = pathToFile;
69
+ writtenBytes = statSync(pathToFile, { throwIfNoEntry: false })?.size ?? 0;
70
+ fileStream = createWriteStream(pathToFile, { flags: "a" });
71
+ fileStream.on("error", (error) => {
72
+ fileStream = null;
73
+ console.warn(chalk.yellow(`${PREFIX} [${ts()}] log file ${pathToFile} stopped: ${error?.message}`));
74
+ });
75
+ console.log(chalk.cyan(`${PREFIX} [${ts()}] logging to ${pathToFile} as well as the console`));
76
+ } catch (error) {
77
+ fileStream = null;
78
+ console.warn(chalk.yellow(`${PREFIX} [${ts()}] cannot log to ${pathToFile}: ${error?.message}`));
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Write one line to the file, rotating when it has grown past the cap.
84
+ *
85
+ * @param {string} line
86
+ * @returns {void}
87
+ */
88
+ function toFile(line) {
89
+ if (!fileStream) {
90
+ return;
91
+ }
92
+ const text = `${line}\n`;
93
+ writtenBytes += Buffer.byteLength(text);
94
+ if (writtenBytes > MAX_FILE_BYTES) {
95
+ try {
96
+ fileStream.end();
97
+ renameSync(filePath, `${filePath}.1`);
98
+ fileStream = createWriteStream(filePath, { flags: "a" });
99
+ writtenBytes = Buffer.byteLength(text);
100
+ } catch {
101
+ // Rotation failed; keep writing to whatever handle still works rather
102
+ // than losing the line that prompted it.
103
+ }
104
+ }
105
+ fileStream.write(text);
106
+ }
107
+
23
108
  /**
24
109
  * @typedef {Object} ProxyLogger
25
110
  * @property {(message: string) => void} info - Informational message (cyan).
@@ -34,8 +119,24 @@ function ts() {
34
119
  * @type {ProxyLogger}
35
120
  */
36
121
  export const logger = {
37
- info: (message) => console.log(chalk.cyan(`${PREFIX} [${ts()}] ${message}`)),
38
- success: (message) => console.log(chalk.green(`${PREFIX} [${ts()}] ${message}`)),
39
- warn: (message) => console.warn(chalk.yellow(`${PREFIX} [${ts()}] ${message}`)),
40
- error: (message) => console.error(chalk.red(`${PREFIX} [${ts()}] ${message}`)),
122
+ info: (message) => {
123
+ const line = `${PREFIX} [${ts()}] ${message}`;
124
+ console.log(chalk.cyan(line));
125
+ toFile(line);
126
+ },
127
+ success: (message) => {
128
+ const line = `${PREFIX} [${ts()}] ${message}`;
129
+ console.log(chalk.green(line));
130
+ toFile(line);
131
+ },
132
+ warn: (message) => {
133
+ const line = `${PREFIX} [${ts()}] ${message}`;
134
+ console.warn(chalk.yellow(line));
135
+ toFile(line);
136
+ },
137
+ error: (message) => {
138
+ const line = `${PREFIX} [${ts()}] ${message}`;
139
+ console.error(chalk.red(line));
140
+ toFile(line);
141
+ }
41
142
  };