rea
morluto
Reverse engineer anything with agents, from app behavior down to native binaries.
PROJECT TOPICS
INSTALL REFERENCE
dsh plugin --profile web add github:SundaeYAGO/dsh-stream-timeout-fix
该命令指向仓库当前默认分支;尚无绑定当前 commit 的完整验证结果。
PROJECT README
English · 简体中文
A tiny DeepSeek Harness (DSH) host plugin that stops Node's built-in fetch
from killing long, silently-stalled LLM streams — the ones that die with
TypeError: terminated (cause: BodyTimeoutError, UND_ERR_BODY_TIMEOUT)
after roughly 304–305 seconds of mid-stream silence, and get reported as a
TRANSPORT failure even though the provider never hung up.
Who this is for. Third-party / self-hosted OpenAI-compatible endpoints whose responses can take a long time and occasionally stall mid-stream: university or company private deployments, LiteLLM-style gateways, self-hosted vLLM, and the like. Against a well-behaved large cloud API you probably do not need it.
Maintenance. Written by the author for personal use, and published in case it helps someone else. It may not be actively maintained, issues and PRs may go unanswered, and there is no support commitment. The plugin is ~120 lines — read it before you install it. See Validation status below: the fix is confirmed by a real 607 s silence that survived end-to-end.
Distribution. There is no npm package — this is a small single-purpose patch and does not warrant one. Install straight from this repository; see Install.
With a streaming LLM endpoint, some requests fail like this:
TypeError: terminated, classified as a transport/network error.The provider's own logs show the opposite of a provider-side failure: it records a
client disconnect (LiteLLM / nginx log 499) and the upstream still finishes
generating and still bills the tokens — you pay for output you never received.
Node's built-in fetch runs on undici, which
ships two client-side clocks that are invisible until they fire:
| Clock | Default | Applies to | Error when it fires |
|---|---|---|---|
headersTimeout |
300_000 ms |
waiting for the first response byte | TypeError: fetch failed, cause HeadersTimeoutError (UND_ERR_HEADERS_TIMEOUT) |
bodyTimeout |
300_000 ms |
idle gap between body bytes (re-armed by every received byte) | TypeError: terminated, cause BodyTimeoutError (UND_ERR_BODY_TIMEOUT) |
Because bodyTimeout is re-armed per byte, ordinary slow-but-flowing streams are
never affected (streams running 20+ minutes are fine). It only bites when the
gap exceeds 300 s — exactly what a stalled upstream produces.
Two things make this hard to attribute correctly:
bodyTimeout reports itself as terminated, i.e. literally "the other side
disconnected". Nothing points at the client's own clock./terminated/ and maps it to TRANSPORT
(see dsh-llm-pi-ai), which reads as a network/provider problem.So the observable facts (client disconnect in the provider log, 499, successful
upstream generation, a TRANSPORT error on our side) all look like "the platform
drops idle connections at ~300 s" — while the actual killer is our own default.
DSH's LLM path never passes a custom fetch
(dsh-llm-pi-ai → pi-ai → OpenAI SDK v6 → globalThis.fetch → global undici
dispatcher), so one process-wide swap covers every provider at once:
setGlobalDispatcher(new Agent({ bodyTimeout: 1_800_000, headersTimeout: 600_000 }))
bodyTimeout = 30 min, headersTimeout = 10 min by default — finite on purpose,
so a genuinely dead peer is still reclaimed.streamIdleTimeoutMs) and provider retry
policies remain the real guards; this plugin only removes the disagreement
between two idle clocks.Symbol.for('undici.globalDispatcher.1') slot, so an Agent created by undici
8.x is honored by Node's built-in fetch (undici 7.x).The failure this plugin targets is gone, and a real >300 s silence has now survived end-to-end. In order of evidential strength:
21:56:49 → 22:06:57), was not retried even once, and completed
normally (tool-calls, 754.0 s total, 18,156 output tokens). Both undici's
bodyTimeout and pi-ai's streamIdleTimeoutMs had to be raised for that to
happen: with either left at its 300 s default the request would have died at
~300–304 s. This is the acceptance test, passed.TypeError: terminated
(UND_ERR_BODY_TIMEOUT); with this plugin's agent the same silence completes
(npm test, scripts/reproduce-300s.mjs).message: "terminated", always after content had
started flowing, never in the headers phase.stream finish events, not log text), the
targeted class — TRANSPORT whose cause chain is TypeError: terminated →
BodyTimeoutError (UND_ERR_BODY_TIMEOUT) — did not recur once. What remains is
AUTH / SERVER 503 / INVALID_REQUEST 404 / EMPTY_RESPONSE / TIMEOUT, and the
TIMEOUTs are the other clocks, not undici.pi-ai stream idle timeout after 300000ms — the route's streamIdleTimeoutMs was
unset (default 300000). Raise it: see “Raise streamIdleTimeoutMs as well”.TIMEOUT: "Request timed out." —
the openai SDK's timeoutMs default (600 s), in the headers phase. Raise it too:
see “The four clocks” (clock 4).You can check whether a stall was actually survived — rather than a long request that streamed continuously — with one command:
node scripts/audit-session-gaps.mjs ~/.dsh/sessions --min-gap-s=250
# -> lists attempts by largest inter-chunk gap, and how many reached >= 300 s
If a request still dies during a long silence, compare the gap length and the message:
304–305 s points at an undici default clock somewhere in the stack; exactly 300 s
with pi-ai stream idle timeout points at DSH's own watchdog; Request timed out.
points at the SDK's timeoutMs (headers phase); anything else points at a
server/gateway idle limit — which this plugin does not address.
Requires Node ≥ 22.19 — that is the undici dependency's engine requirement
(engines.node >= 22.19.0). Any DSH build able to mount this plugin already bundles
undici 8.x itself, so this matches the real deployment range; on Node 20 the import
fails instead of silently misbehaving.
git clone https://github.com/SundaeYAGO/dsh-stream-timeout-fix
cd dsh-stream-timeout-fix
npm install # fetches the `undici` dependency the plugin imports
Then mount the checkout as a local bundle in your profile:
~/.dsh/profiles/<profile>/package.json, add
"dsh-stream-timeout-fix": "link:<absolute path to the checkout>" to dependencies;"dsh-stream-timeout-fix" to dsh.profile.bundles;pnpm install, which creates the
node_modules link) and restart DSH — profiles that support live bundle application
mount it without a restart.The package's own cordis.patch.yml mounts the plugin row, so no manual profile-patch
edit is needed. If your DSH CLI accepts git specs (the same form other DSH plugins use),
dsh plugin --profile <profile> add github:SundaeYAGO/dsh-stream-timeout-fix should
perform those three steps for you.
include:stream-timeout-fix).[stream-timeout-fix] global dispatcher replaced: bodyTimeout=1800000ms, headersTimeout=600000ms ...globalThis.__streamTimeoutFix holds the installed Agent,
the effective timeouts, and the runtime versions.| Setting | Default | Where |
|---|---|---|
bodyTimeoutMs |
1800000 |
patch row config, or env DSH_STREAM_TIMEOUT_BODY_MS |
headersTimeoutMs |
600000 |
patch row config, or env DSH_STREAM_TIMEOUT_HEADERS_MS |
Set either to 0 to disable that clock entirely (not recommended globally — you
lose the last-resort reclamation of dead sockets).
@deepseek-ai/dsh-llm-retry), configured per
provider in your profile patch — see the pairing section below if you want both.streamIdleTimeoutMs is per-route
configuration (dsh-llm-pi-ai), not something a dispatcher swap can touch — see
“Raise streamIdleTimeoutMs as well”.streamIdleTimeoutMs as well (needed for >300 s silences)This plugin only removes undici's 300 s clocks. DSH's own per-route watchdog still
cuts a silent stream at streamIdleTimeoutMs (default 300000), reporting
pi-ai stream idle timeout after 300000ms with code: TIMEOUT.
That is not theory: on 2026-10-18 two attempts died at exactly 300.02 s and
300.04 s with this plugin active (undici ceiling already 30 min), because
streamIdleTimeoutMs was unset on the route. Without raising it, the plugin improves
the diagnosis (honest TIMEOUT instead of TRANSPORT) but does not extend how
long a silence can last.
providers:
your-provider:
streamIdleTimeoutMs: 1800000 # 30 min — must exceed the longest stall you want to survive
Note that pi-ai counts silence from request dispatch, including the wait for the first token: a huge prompt with a slow prefill consumes the same budget as a mid-stream stall. (Same profile patch as the retry policy below — different key.)
This plugin owns the first two. The other two live in dsh-llm-pi-ai and are also
worth configuring — one of them is what actually cut a request on 2026-10-28:
| # | Clock | Owner | Where it bites | Default |
|---|---|---|---|---|
| 1 | headersTimeout |
undici (this plugin) | no response headers yet | 300 s → 600 s here |
| 2 | bodyTimeout |
undici (this plugin) | silence between chunks | 300 s → 1800 s here |
| 3 | streamIdleTimeoutMs |
dsh-llm-pi-ai |
silence, first byte included | 300 000 ms |
| 4 | timeoutMs |
dsh-llm-pi-ai → openai SDK |
headers phase (Request timed out., code TIMEOUT) |
SDK default 600 000 ms |
Clock 4 is the sneaky one: it is the OpenAI SDK's request timeout and it surfaces as
Request timed out. / TIMEOUT — easy to mistake for a platform failure. Set both on
the route:
providers:
your-provider:
streamIdleTimeoutMs: 1800000 # clock 3 — idle gaps, first byte included
timeoutMs: 1800000 # clock 4 — headers phase (SDK request timeout)
The plugin makes no network requests of its own and adds no cost by itself. But it changes the outcome of a stall, and that has a price:
Automatic retries belong to your profile patch
(~/.dsh/profiles/<profile>/cordis.patch.yml), not to this plugin. Example for a
provider route:
providers:
your-provider:
retryPolicy:
mode: always # no retry cap; ignores retryableCodes
backoff: { initialDelayMs: 5000, maxDelayMs: 300000, jitterRatio: 0.25 }
Choose delays that respect your provider's rate limit (institutional deployments often have a small per-key RPM), and keep the cost note above in mind: retries are billed.
fetch in the DSH host process (all
providers, other plugins) loses the 300 s default ceiling and inherits the
configured one. That is the point, but it is a real blast radius.#7721, #7671, #7598, #7468, #7502) blame a userland
undici 8.x dispatcher in the process-global slot for breaking content-encoding and
response headers on Node's built-in fetch (bundled undici 7.x) — the same slot
this plugin swaps. scripts/check-content-encoding.mjs (part of npm test)
verifies gzip decoding before and after the swap; it passes on Node 24.21 with
undici 8.11.2. If you ever see raw gzip/brotli or missing headers after
installing, disable the plugin and report your node -v plus
process.versions.undici pair.: ping) — a server-side measure. This plugin is the client-side fix.HeadersTimeoutError —
an undici exception class, which revealed the client-side clocks.message: "terminated", always mid-stream, always after content started.t = 2.045 s → error at t = 306.062 s
(TypeError: terminated, cause BodyTimeoutError, code UND_ERR_BODY_TIMEOUT),
while a client built with Agent({ bodyTimeout: 0 }) survived 338 s of
silence and completed the stream. setGlobalDispatcher from a differently
versioned undici copy also fixed the built-in fetch — the injection path this
plugin uses.scripts/reproduce-300s.mjs ships that reproduction:
node scripts/reproduce-300s.mjs # default fetch: reproduces the ~304 s kill
node scripts/reproduce-300s.mjs --plugin # after this plugin: survives past 300 s
npm install
npm test
npm test runs a fast, deterministic pair:
Agent with a tiny bodyTimeout proves that a silent stream
dies with UND_ERR_BODY_TIMEOUT (the reported failure shape);apply(), the same silence completes, and the global dispatcher
is verified to be this plugin's agent;Vibe-coded by the author, with GLM 5.3 and DeepSeek V4.1 Flash driving the
investigation and the implementation. Every claim in this README is backed by scripts
in this repo (npm test, scripts/reproduce-300s.mjs) — run them yourself rather than
trusting the prose. See
PROVENANCE.md
for the full note.
See CHANGELOG.md.
MIT. undici is a normal dependency (not vendored), so its own license stays with it.
CLASSIFICATION EVIDENCE
系统优先读取 GitHub Topics,再与站内分类词典和词根规则比对。当前命中: litellm、vllm。