Pulse `localhost:31337` URLs stall on bun — `fetch()` hits ~1–3.5s on ~20% of calls; use `127.0.0.1`
Summary
Hooks and tools reach the local Pulse server at http://localhost:31337/.... On bun, fetch() to the localhost hostname intermittently stalls: over 50 calls, 20% took 1–3.5s, the rest were <200ms. The same calls to http://127.0.0.1:31337/... were 50/50 under 5ms. Callers with short timeouts abort on the slow ones, so notify and voice events silently drop a fair chunk of the time.
Reproduce (bun 1.3.14, macOS)
Server on 127.0.0.1:31337 only:
for (const url of ["http://localhost:31337/notify", "http://127.0.0.1:31337/notify"]) {
let slow = 0, max = 0;
for (let i = 0; i < 50; i++) {
const s = Date.now();
try { await fetch(url, { method: "POST", body: "{}", signal: AbortSignal.timeout(6000) }); } catch {}
const d = Date.now() - s; max = Math.max(max, d); if (d > 1000) slow++;
}
console.log(url, "slow(>1s):", slow + "/50", "max:", max + "ms");
}
http://localhost:31337/notify slow(>1s): 10/50 max: 3577ms
http://127.0.0.1:31337/notify slow(>1s): 0/50 max: 5ms
Root cause
localhost resolves to both ::1 (IPv6, first) and 127.0.0.1; Pulse binds 127.0.0.1 only.
It's not an IPv6 blackhole and not disabled IPv6 — a raw TCP connect to a closed ::1 port refuses in ~13ms, and dns.lookup("localhost") returns in <5ms. Network and resolver are fine. The stall is in bun's fetch() hostname path: resolving/connecting the name intermittently takes 1–3.5s (rarely much longer), while a raw socket connect and a fetch() to the IP literal are both instant. The bimodal split (~80% <200ms, ~20% at 1–3.5s, nothing between) reads as a connection retry on ::1 before falling back to IPv4. The 127.0.0.1 literal skips name resolution and sidesteps it, whatever the exact internal cause.
Impact
- ~20% of
localhostcalls slow (1–3.5s) on the tested setup; anything over a caller's timeout aborts. A 2s-timeout notify caller drops the >2s slice outright. - Hits any user whose
localhostresolves IPv6-first (macOS default/etc/hosts) against an IPv4-only local server — the default Pulse setup on Mac. - No error surfaces: the call times out and is swallowed, so notifications just go missing.
- Exact percentage varies by machine, load, and bun version; measured on bun 1.3.14.
Scope
Find every site:
grep -rn "localhost:31337" --include="*.ts" hooks/ skills/ LIFEOS/
~20+ call sites, two kinds:
- bun
fetch()calls — the affected path (notify sender,/healthzand/api/workpollers, desktop notify channel). Fix these. curlin generated agent prompts —curldoes happy-eyeballs right and isn't affected. Cosmetic only.
Proposed fix (your call on the shape)
- (A) Literal — swap the loopback hostname for
127.0.0.1in thefetch()URLs. One concern, benign (a wrong step just leaves the existing timeout in place), no config change. Smallest surface. - (B) Shared constant — one exported
PULSE_URL(http://127.0.0.1:31337, env-overridable), repointed everywhere. Kills thehost:portliteral now duplicated across ~20 files. - (C) Config — surface the base URL through config (
PULSE.toml/PULSE.user.toml, or the user config). Most flexible; a call on where it lives.
Recommend (A) as the immediate fix, (B) as the durable follow-up. (C) only if you want it user-configurable.
Environment
bun 1.3.14, macOS, stock /etc/hosts (::1 localhost before 127.0.0.1), Pulse on 127.0.0.1:31337.
Fixed, and your root-cause analysis was exactly right: Pulse binds the IPv4 literal explicitly, so on stock macOS /etc/hosts every localhost call is guaranteed to try a dead ::1 first. There is now one PULSE_BASE constant (PULSE/endpoint.ts, PULSE_URL-overridable) and all 19 consumers use it. curl strings, tests, and docs left alone as you suggested.
Two sites weren't in your list and one of them mattered a lot. AlgorithmNudge matched a localhost:31337/notify literal to detect the voice capability, so it would have gone silently blind. And Daemon/SecurityFilter redacted localhost:PORT but had no pattern for the loopback IP — moving the codebase to 127.0.0.1 would have let the internal endpoint leak into the published daemon profile. Both fixed in the same pass. Good bug; it had a sharper edge than it looked.