pi-freeflow 1.43.0 → 1.44.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 +22 -0
- package/README.md +2 -1
- package/package.json +1 -1
- package/src/client-recovery.ts +235 -0
- package/src/client-staging.ts +266 -0
- package/src/client.ts +114 -456
- package/src/cline-accounts.ts +20 -1220
- package/src/cline-pool.ts +641 -0
- package/src/cline-roll.ts +609 -0
- package/src/commands-cline.ts +283 -0
- package/src/commands-relay.ts +808 -0
- package/src/commands-startup.ts +261 -0
- package/src/commands.ts +121 -1200
- package/src/config-paths.ts +254 -0
- package/src/config.ts +16 -654
- package/src/fingerprint-env.ts +334 -0
- package/src/fingerprint-tools.ts +348 -0
- package/src/health.ts +9 -1
- package/src/lane-translation.ts +244 -0
- package/src/opencode-fingerprint.ts +69 -1325
- package/src/opencode-identity.ts +457 -0
- package/src/proxy-lanes.ts +1113 -0
- package/src/proxy-ports.ts +317 -0
- package/src/proxy.ts +134 -1333
- package/src/relay-codec.ts +365 -0
- package/src/relay-state.ts +1 -363
- package/src/relay.ts +7 -8
- package/src/sse-aggregator.ts +658 -0
- package/src/stream-cloak.ts +879 -0
- package/src/stream-pipe.ts +10 -869
- package/src/tool-translation.ts +4 -236
- package/src/update-core.ts +25 -0
- package/src/zen-sidecar.ts +96 -0
- package/src/zen-transport.ts +413 -0
package/src/client.ts
CHANGED
|
@@ -13,21 +13,14 @@ import { randomUUID } from "node:crypto";
|
|
|
13
13
|
import { spawn } from "node:child_process";
|
|
14
14
|
import fs from "node:fs";
|
|
15
15
|
import path from "node:path";
|
|
16
|
-
import { fileURLToPath } from "node:url";
|
|
17
16
|
import type * as http from "node:http";
|
|
18
17
|
import {
|
|
19
18
|
BASE_PORT_REPROBE_MS,
|
|
20
|
-
BREAKER_FAILURES,
|
|
21
|
-
BREAKER_HALT_MS,
|
|
22
|
-
BREAKER_WINDOW_MS,
|
|
23
|
-
BUSY_BYPASS_CONTINUOUS_MS,
|
|
24
|
-
BUSY_BYPASS_QUIET_MS,
|
|
25
19
|
DAEMON_CONTROL_TIMEOUT_MS,
|
|
26
20
|
DAEMON_CONTROL_TIMEOUT_MS_ENV,
|
|
27
21
|
DAEMON_HEARTBEAT_MS,
|
|
28
22
|
DAEMON_HEARTBEAT_MS_ENV,
|
|
29
23
|
DAEMON_READY_TIMEOUT_MS,
|
|
30
|
-
DAEMON_RUNTIME_ENV,
|
|
31
24
|
DAEMON_SPAWN_ENV,
|
|
32
25
|
DAEMON_WATCHDOG_MS,
|
|
33
26
|
DAEMON_WATCHDOG_MS_ENV,
|
|
@@ -37,10 +30,8 @@ import {
|
|
|
37
30
|
OPENCODE_VERSION_TTL_MS,
|
|
38
31
|
PKG_VERSION,
|
|
39
32
|
PORT,
|
|
40
|
-
RECOVERY_BACKOFF_BASE_MS,
|
|
41
|
-
RECOVERY_BACKOFF_CAP_MS,
|
|
42
33
|
refreshOpenCodeUserAgent,
|
|
43
|
-
|
|
34
|
+
ZEN_TRANSPORT_ENV,
|
|
44
35
|
} from "./config.ts";
|
|
45
36
|
import { logInfo, logWarn } from "./logger.ts";
|
|
46
37
|
import {
|
|
@@ -54,8 +45,71 @@ import {
|
|
|
54
45
|
} from "./proxy.ts";
|
|
55
46
|
import { compareVersions } from "./update-core.ts";
|
|
56
47
|
import { isDaemonIdentityBody } from "./health.ts";
|
|
57
|
-
import type { DaemonHealthSnapshot } from "./health.ts";
|
|
58
48
|
import { tokenAuthHeaders } from "./token.ts";
|
|
49
|
+
import {
|
|
50
|
+
daemonScriptPath,
|
|
51
|
+
findExecutableOnPath,
|
|
52
|
+
isJsRuntimeExecutable,
|
|
53
|
+
isUnderNodeModules,
|
|
54
|
+
materializeDaemonSources,
|
|
55
|
+
pruneStaleDaemonSrcDirs,
|
|
56
|
+
readPackageJsonObject,
|
|
57
|
+
resolveDaemonRuntime,
|
|
58
|
+
resolvePackageDir,
|
|
59
|
+
stageRuntimeDependencies,
|
|
60
|
+
type DaemonRuntime,
|
|
61
|
+
} from "./client-staging.ts";
|
|
62
|
+
import {
|
|
63
|
+
computeRecoveryBackoffMs,
|
|
64
|
+
getBusySince,
|
|
65
|
+
isBreakerHalted,
|
|
66
|
+
isOlderDaemonVersion,
|
|
67
|
+
isStuckBusy,
|
|
68
|
+
recordRecoveryFailure,
|
|
69
|
+
recordRecoverySuccess,
|
|
70
|
+
resetBusySince,
|
|
71
|
+
shouldRecoverOnHealth,
|
|
72
|
+
trackBusyEdge,
|
|
73
|
+
triggerRecovery,
|
|
74
|
+
watchdogCheck,
|
|
75
|
+
_getRecoveryStateForTest,
|
|
76
|
+
_resetRecoveryForTest,
|
|
77
|
+
type HealthForRecovery,
|
|
78
|
+
type RecoveryDeps,
|
|
79
|
+
} from "./client-recovery.ts";
|
|
80
|
+
|
|
81
|
+
export {
|
|
82
|
+
daemonScriptPath,
|
|
83
|
+
findExecutableOnPath,
|
|
84
|
+
isJsRuntimeExecutable,
|
|
85
|
+
isUnderNodeModules,
|
|
86
|
+
materializeDaemonSources,
|
|
87
|
+
pruneStaleDaemonSrcDirs,
|
|
88
|
+
readPackageJsonObject,
|
|
89
|
+
resolveDaemonRuntime,
|
|
90
|
+
resolvePackageDir,
|
|
91
|
+
stageRuntimeDependencies,
|
|
92
|
+
type DaemonRuntime,
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
export {
|
|
96
|
+
computeRecoveryBackoffMs,
|
|
97
|
+
getBusySince,
|
|
98
|
+
isBreakerHalted,
|
|
99
|
+
isOlderDaemonVersion,
|
|
100
|
+
isStuckBusy,
|
|
101
|
+
recordRecoveryFailure,
|
|
102
|
+
recordRecoverySuccess,
|
|
103
|
+
resetBusySince,
|
|
104
|
+
shouldRecoverOnHealth,
|
|
105
|
+
trackBusyEdge,
|
|
106
|
+
triggerRecovery,
|
|
107
|
+
watchdogCheck,
|
|
108
|
+
_getRecoveryStateForTest,
|
|
109
|
+
_resetRecoveryForTest,
|
|
110
|
+
type HealthForRecovery,
|
|
111
|
+
type RecoveryDeps,
|
|
112
|
+
};
|
|
59
113
|
|
|
60
114
|
const CLIENT_ID = randomUUID();
|
|
61
115
|
|
|
@@ -70,146 +124,11 @@ let ensuring = false;
|
|
|
70
124
|
*/
|
|
71
125
|
let ensureInFlight: Promise<number> | null = null;
|
|
72
126
|
let fallbackServer: http.Server | null = null;
|
|
73
|
-
/** First tick (Date.now()) the daemon has looked continuously busy; 0 = idle. */
|
|
74
|
-
let busySince = 0;
|
|
75
|
-
/** Consecutive recovery attempts (backoff exponent n). Reset on healthy watchdog tick. */
|
|
76
|
-
let backoffAttempt = 0;
|
|
77
|
-
/** Timestamps of recent recovery failures (breaker window). */
|
|
78
|
-
let recoveryFailures: number[] = [];
|
|
79
|
-
/** Watchdog respawns halted until this time (breaker open). */
|
|
80
|
-
let breakerOpenUntil = 0;
|
|
81
127
|
/** Last wall-clock attempt of a background UA refresh; storm-guard for heartbeat cadence. */
|
|
82
128
|
let lastUaRefreshAt = 0;
|
|
83
129
|
/** Test-injected fetch for the background UA refresh; null = global fetch. */
|
|
84
130
|
let uaRefreshFetch: typeof fetch | null = null;
|
|
85
131
|
|
|
86
|
-
interface DaemonRuntime {
|
|
87
|
-
execPath: string;
|
|
88
|
-
args: string[];
|
|
89
|
-
runtimeType: "bun" | "node";
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Detect whether an executable path is a JavaScript/TypeScript runtime (bun or node).
|
|
94
|
-
* Rejects host binaries like omp, pi, electron, code, etc.
|
|
95
|
-
*/
|
|
96
|
-
export function isJsRuntimeExecutable(execPath: string): "bun" | "node" | null {
|
|
97
|
-
if (!execPath || typeof execPath !== "string") return null;
|
|
98
|
-
const checkName = (target: string): "bun" | "node" | null => {
|
|
99
|
-
const base = path.basename(target.replace(/\\/g, "/")).toLowerCase().replace(/\.exe$/i, "");
|
|
100
|
-
if (base === "bun" || base === "bunx") return "bun";
|
|
101
|
-
if (base === "node" || base === "nodejs") return "node";
|
|
102
|
-
return null;
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
const direct = checkName(execPath);
|
|
106
|
-
if (direct) return direct;
|
|
107
|
-
|
|
108
|
-
try {
|
|
109
|
-
const real = fs.realpathSync(execPath);
|
|
110
|
-
const realMatch = checkName(real);
|
|
111
|
-
if (realMatch) return realMatch;
|
|
112
|
-
} catch { }
|
|
113
|
-
|
|
114
|
-
return null;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Search directories in PATH for an executable matching name.
|
|
119
|
-
* On Windows, tests extensions from PATHEXT (.exe, .cmd, .bat).
|
|
120
|
-
* On POSIX, checks file existence and executable bit (X_OK).
|
|
121
|
-
*/
|
|
122
|
-
export function findExecutableOnPath(
|
|
123
|
-
name: string,
|
|
124
|
-
envPath: string = process.env.PATH || process.env.Path || "",
|
|
125
|
-
): string | null {
|
|
126
|
-
if (!envPath || typeof envPath !== "string") return null;
|
|
127
|
-
const dirs = envPath.split(path.delimiter).filter(Boolean);
|
|
128
|
-
const hasExt = path.extname(name).length > 0;
|
|
129
|
-
const extensions = process.platform === "win32"
|
|
130
|
-
? (hasExt ? [""] : (process.env.PATHEXT ? process.env.PATHEXT.split(";").filter(Boolean) : [".exe", ".cmd", ".bat"]))
|
|
131
|
-
: [""];
|
|
132
|
-
|
|
133
|
-
for (const dir of dirs) {
|
|
134
|
-
for (const ext of extensions) {
|
|
135
|
-
const candidate = ext ? path.join(dir, `${name}${ext}`) : path.join(dir, name);
|
|
136
|
-
try {
|
|
137
|
-
const stat = fs.statSync(candidate);
|
|
138
|
-
if (stat.isFile()) {
|
|
139
|
-
if (process.platform !== "win32") {
|
|
140
|
-
fs.accessSync(candidate, fs.constants.X_OK);
|
|
141
|
-
}
|
|
142
|
-
return candidate;
|
|
143
|
-
}
|
|
144
|
-
} catch { }
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
return null;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Resolve the executable and arguments needed to run src/daemon.ts.
|
|
152
|
-
*
|
|
153
|
-
* Priority order:
|
|
154
|
-
* 1. Explicit override via opts.overrideExecPath or PI_FREEFLOW_DAEMON_RUNTIME env var.
|
|
155
|
-
* 2. Current process.execPath (if and only if it is actually a node or bun executable).
|
|
156
|
-
* 3. Search PATH for `bun` (preferred: native TypeScript support with zero flags).
|
|
157
|
-
* 4. Search PATH for `node` (Node.js >= 22.6 with --experimental-strip-types).
|
|
158
|
-
*
|
|
159
|
-
* Returns null if no valid runtime is found (e.g. running as a standalone `omp` binary
|
|
160
|
-
* without bun or node on PATH). The caller MUST fall back to an in-process proxy rather
|
|
161
|
-
* than spawning an arbitrary binary (which would cause recursive agent fork-bombs).
|
|
162
|
-
*/
|
|
163
|
-
export function resolveDaemonRuntime(opts?: {
|
|
164
|
-
overrideExecPath?: string;
|
|
165
|
-
scriptPath?: string;
|
|
166
|
-
envPath?: string;
|
|
167
|
-
currentExecPath?: string;
|
|
168
|
-
}): DaemonRuntime | null {
|
|
169
|
-
const script = opts?.scriptPath ?? daemonScriptPath();
|
|
170
|
-
const envExec = opts?.overrideExecPath ?? (typeof process.env[DAEMON_RUNTIME_ENV] === "string" ? process.env[DAEMON_RUNTIME_ENV]?.trim() : undefined);
|
|
171
|
-
|
|
172
|
-
// 1. Explicit override
|
|
173
|
-
if (envExec) {
|
|
174
|
-
const type = isJsRuntimeExecutable(envExec) ?? "node";
|
|
175
|
-
const args = type === "bun" ? [script] : ["--experimental-strip-types", script];
|
|
176
|
-
return { execPath: envExec, args, runtimeType: type };
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// 2. Check current process.execPath
|
|
180
|
-
const currentExec = opts?.currentExecPath ?? process.execPath;
|
|
181
|
-
const currentType = isJsRuntimeExecutable(currentExec);
|
|
182
|
-
if (currentType !== null) {
|
|
183
|
-
const args = currentType === "bun" ? [script] : ["--experimental-strip-types", script];
|
|
184
|
-
return { execPath: currentExec, args, runtimeType: currentType };
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// 3. Current executable is not a JS runtime (e.g. omp, pi, electron).
|
|
188
|
-
// Search PATH for bun first, then node.
|
|
189
|
-
const pathVal = opts?.envPath ?? process.env.PATH ?? process.env.Path ?? "";
|
|
190
|
-
|
|
191
|
-
const bunOnPath = findExecutableOnPath("bun", pathVal);
|
|
192
|
-
if (bunOnPath) {
|
|
193
|
-
return { execPath: bunOnPath, args: [script], runtimeType: "bun" };
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
const nodeOnPath = findExecutableOnPath("node", pathVal);
|
|
197
|
-
if (nodeOnPath) {
|
|
198
|
-
return {
|
|
199
|
-
execPath: nodeOnPath,
|
|
200
|
-
args: ["--experimental-strip-types", script],
|
|
201
|
-
runtimeType: "node",
|
|
202
|
-
};
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
return null;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
function daemonScriptPath(): string {
|
|
209
|
-
const p = path.join(path.dirname(fileURLToPath(import.meta.url)), "daemon.ts");
|
|
210
|
-
return process.platform === "win32" ? p.replace(/\\/g, "/") : p;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
132
|
function getHeartbeatMs(): number {
|
|
214
133
|
const raw = process.env[DAEMON_HEARTBEAT_MS_ENV];
|
|
215
134
|
if (raw) {
|
|
@@ -308,7 +227,7 @@ function startHeartbeat(port: number): void {
|
|
|
308
227
|
} catch { }
|
|
309
228
|
const wms = getWatchdogMs();
|
|
310
229
|
watchdogTimer = setInterval(() => {
|
|
311
|
-
void watchdogCheck(port);
|
|
230
|
+
void watchdogCheck(port, recoveryDeps);
|
|
312
231
|
}, wms);
|
|
313
232
|
try {
|
|
314
233
|
watchdogTimer.unref();
|
|
@@ -363,107 +282,6 @@ async function attachTo(port: number): Promise<void> {
|
|
|
363
282
|
maybeRefreshOpenCodeUserAgent();
|
|
364
283
|
}
|
|
365
284
|
|
|
366
|
-
/**
|
|
367
|
-
* Respawn backoff: min(2s * 2^n, 60s) + jitter. n is the consecutive-failure
|
|
368
|
-
* count; pass jitterMs = 0 in tests for a deterministic base.
|
|
369
|
-
*/
|
|
370
|
-
export function computeRecoveryBackoffMs(attempt: number, jitterMs = Math.random() * 500): number {
|
|
371
|
-
const n = Math.max(0, Math.floor(attempt));
|
|
372
|
-
const base = Math.min(RECOVERY_BACKOFF_BASE_MS * 2 ** n, RECOVERY_BACKOFF_CAP_MS);
|
|
373
|
-
return base + Math.max(0, jitterMs);
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
/**
|
|
377
|
-
* Stuck-busy check (pure, testable): a busy daemon may only be replaced after
|
|
378
|
-
* 5min of continuous busy AND zero forwarded stream bytes for 60s+. Anything
|
|
379
|
-
* else must be left untouched mid-request.
|
|
380
|
-
*/
|
|
381
|
-
export function isStuckBusy(busySinceMs: number, lastBytesAt: number, now = Date.now()): boolean {
|
|
382
|
-
if (!busySinceMs) return false;
|
|
383
|
-
if (now - busySinceMs < BUSY_BYPASS_CONTINUOUS_MS) return false;
|
|
384
|
-
if (lastBytesAt && now - lastBytesAt < BUSY_BYPASS_QUIET_MS) return false;
|
|
385
|
-
return true;
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
/** Track the busy/idle edge from a health snapshot; returns the updated busySince. */
|
|
389
|
-
export function trackBusyEdge(activeRequests: number, lastBytesAt: number, now = Date.now()): number {
|
|
390
|
-
if (activeRequests > 0) {
|
|
391
|
-
if (!busySince) busySince = now;
|
|
392
|
-
} else {
|
|
393
|
-
busySince = 0;
|
|
394
|
-
}
|
|
395
|
-
return busySince;
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
/** True while the breaker halt is in effect (respawns paused, fallback serves). */
|
|
399
|
-
export function isBreakerHalted(now = Date.now()): boolean {
|
|
400
|
-
return now < breakerOpenUntil;
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
/** Record a recovery failure; opens the breaker after 5 straight failures in 5min. */
|
|
404
|
-
export function recordRecoveryFailure(now = Date.now()): boolean {
|
|
405
|
-
recoveryFailures.push(now);
|
|
406
|
-
recoveryFailures = recoveryFailures.filter((t) => now - t <= BREAKER_WINDOW_MS);
|
|
407
|
-
backoffAttempt += 1;
|
|
408
|
-
if (recoveryFailures.length >= BREAKER_FAILURES) {
|
|
409
|
-
breakerOpenUntil = now + BREAKER_HALT_MS;
|
|
410
|
-
logWarn("recovery breaker open — 5 straight failures in 5min, halting respawn for 10min (in-process fallback meanwhile)");
|
|
411
|
-
return true;
|
|
412
|
-
}
|
|
413
|
-
return false;
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
/** Record a healthy watchdog tick: clears the straight-failure run and backoff. */
|
|
417
|
-
export function recordRecoverySuccess(): void {
|
|
418
|
-
recoveryFailures = [];
|
|
419
|
-
backoffAttempt = 0;
|
|
420
|
-
breakerOpenUntil = 0;
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
/** Test seam: snapshot of the recovery counters. */
|
|
424
|
-
export function _getRecoveryStateForTest(): {
|
|
425
|
-
backoffAttempt: number;
|
|
426
|
-
failures: number[];
|
|
427
|
-
breakerOpenUntil: number;
|
|
428
|
-
busySince: number;
|
|
429
|
-
} {
|
|
430
|
-
return { backoffAttempt, failures: [...recoveryFailures], breakerOpenUntil, busySince };
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
/** Test seam: reset recovery counters without touching timers. */
|
|
434
|
-
export function _resetRecoveryForTest(): void {
|
|
435
|
-
backoffAttempt = 0;
|
|
436
|
-
recoveryFailures = [];
|
|
437
|
-
breakerOpenUntil = 0;
|
|
438
|
-
busySince = 0;
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
/** Recovery-decision shape; canonical definition lives in health.ts. */
|
|
442
|
-
export type HealthForRecovery = DaemonHealthSnapshot;
|
|
443
|
-
|
|
444
|
-
/**
|
|
445
|
-
* Older-version guard: only a daemon strictly older than this build is worth
|
|
446
|
-
* recovering. shouldReplaceDaemon deliberately leaves a NEWER daemon running,
|
|
447
|
-
* so recovering from one could never succeed (watchdog → recover → refused →
|
|
448
|
-
* reattach → watchdog, until the breaker parks into the in-process fallback).
|
|
449
|
-
* Null/undefined version (pre-version daemon) is not older on its own.
|
|
450
|
-
*/
|
|
451
|
-
function isOlderDaemonVersion(version: string | null | undefined): boolean {
|
|
452
|
-
return version != null && compareVersions(version, PKG_VERSION) < 0;
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
/**
|
|
456
|
-
* Watchdog decision matrix (pure, testable). Recover when the daemon is gone,
|
|
457
|
-
* serves an older version, or reports a degraded failed-SSE window. Missing
|
|
458
|
-
* sseDegraded (pre-window daemon) never triggers on its own.
|
|
459
|
-
*/
|
|
460
|
-
export function shouldRecoverOnHealth(health: HealthForRecovery): boolean {
|
|
461
|
-
if (health === null) return true;
|
|
462
|
-
if (isOlderDaemonVersion(health.version)) return true;
|
|
463
|
-
if (health.sseDegraded === true) return true;
|
|
464
|
-
return false;
|
|
465
|
-
}
|
|
466
|
-
|
|
467
285
|
async function ensureInProcessFallback(): Promise<number> {
|
|
468
286
|
try {
|
|
469
287
|
const r = await startProxy();
|
|
@@ -476,85 +294,21 @@ async function ensureInProcessFallback(): Promise<number> {
|
|
|
476
294
|
}
|
|
477
295
|
}
|
|
478
296
|
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
return;
|
|
488
|
-
}
|
|
489
|
-
const opened = recordRecoveryFailure(now);
|
|
490
|
-
if (opened) {
|
|
491
|
-
void ensureInProcessFallback();
|
|
492
|
-
return;
|
|
493
|
-
}
|
|
494
|
-
const delay = computeRecoveryBackoffMs(backoffAttempt - 1);
|
|
495
|
-
logWarn(`watchdog recovery (${why}) — backing off ${Math.round(delay)}ms before respawn`);
|
|
496
|
-
stopHeartbeatInternal();
|
|
297
|
+
const recoveryDeps: RecoveryDeps = {
|
|
298
|
+
isEnsuring: () => ensuring,
|
|
299
|
+
setEnsuring: (val: boolean) => {
|
|
300
|
+
ensuring = val;
|
|
301
|
+
},
|
|
302
|
+
ensureInProcessFallback,
|
|
303
|
+
stopHeartbeatInternal,
|
|
304
|
+
resetHeartbeatPort: () => {
|
|
497
305
|
heartbeatPort = 0;
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
logInfo(`watchdog recovery deferred — daemon busy (${health.activeRequests} active, waiting for 5min-stuck + 60s-quiet)`);
|
|
505
|
-
await attachTo(port);
|
|
506
|
-
return;
|
|
507
|
-
}
|
|
508
|
-
}
|
|
509
|
-
const ver = health?.version ?? PKG_VERSION;
|
|
510
|
-
await killStaleDaemon(port, ver, "proxy daemon");
|
|
511
|
-
} catch (e) {
|
|
512
|
-
logWarn("watchdog pre-respawn probe failed", { error: String(e) });
|
|
513
|
-
}
|
|
514
|
-
// Release before delegating: ensureDaemon owns the flag from here (it
|
|
515
|
-
// early-returns while ensuring is true). The handoff is synchronous up
|
|
516
|
-
// to ensureDaemon's own ensuring=true, so no tick slips between.
|
|
517
|
-
ensuring = false;
|
|
518
|
-
await ensureDaemon();
|
|
519
|
-
} finally {
|
|
520
|
-
ensuring = false;
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
/**
|
|
525
|
-
* 5s watchdog tick: polls /_health for version + failed-SSE rate and drives
|
|
526
|
-
* stopHeartbeat → busy-ruled kill → ensureDaemon when the daemon is stale or
|
|
527
|
-
* degraded. Healthy ticks reset backoff/breaker and track the busy edge.
|
|
528
|
-
*/
|
|
529
|
-
async function watchdogCheck(port: number): Promise<void> {
|
|
530
|
-
if (ensuring) return;
|
|
531
|
-
if (isBreakerHalted()) return;
|
|
532
|
-
let health: Awaited<ReturnType<typeof getDaemonHealth>>;
|
|
533
|
-
try {
|
|
534
|
-
health = await getDaemonHealth(port);
|
|
535
|
-
} catch {
|
|
536
|
-
return;
|
|
537
|
-
}
|
|
538
|
-
if (health === null) {
|
|
539
|
-
await new Promise<void>((r) => setTimeout(r, 200));
|
|
540
|
-
try {
|
|
541
|
-
health = await getDaemonHealth(port);
|
|
542
|
-
} catch {
|
|
543
|
-
return;
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
if (health && (health.activeRequests ?? 0) > 0) {
|
|
547
|
-
trackBusyEdge(health.activeRequests ?? 0, health.lastBytesAt ?? 0);
|
|
548
|
-
} else {
|
|
549
|
-
trackBusyEdge(0, 0);
|
|
550
|
-
}
|
|
551
|
-
if (!shouldRecoverOnHealth(health)) {
|
|
552
|
-
recordRecoverySuccess();
|
|
553
|
-
return;
|
|
554
|
-
}
|
|
555
|
-
if (health && (health.activeRequests ?? 0) > 0 && !isStuckBusy(busySince, health.lastBytesAt ?? 0)) return;
|
|
556
|
-
void triggerRecovery(port, health === null ? "daemon gone" : isOlderDaemonVersion(health.version) ? `stale v${health.version}` : "failed-SSE degraded").catch((e) => logWarn("watchdog recovery failed", { error: String(e) }));
|
|
557
|
-
}
|
|
306
|
+
},
|
|
307
|
+
attachTo,
|
|
308
|
+
killStaleDaemon: (p, v, w) => killStaleDaemon(p, v, w),
|
|
309
|
+
ensureDaemon: () => ensureDaemon(),
|
|
310
|
+
getDaemonHealth: (p: number) => getDaemonHealth(p),
|
|
311
|
+
};
|
|
558
312
|
|
|
559
313
|
async function shouldReplaceDaemon(port: number, remoteVer: string): Promise<boolean> {
|
|
560
314
|
if (NO_KILL_ENV && process.env[NO_KILL_ENV] === "1") {
|
|
@@ -577,8 +331,8 @@ async function shouldReplaceDaemon(port: number, remoteVer: string): Promise<boo
|
|
|
577
331
|
return false;
|
|
578
332
|
}
|
|
579
333
|
if (health.activeRequests > 0) {
|
|
580
|
-
trackBusyEdge(health.activeRequests, health.lastBytesAt ?? 0);
|
|
581
|
-
if (!isStuckBusy(
|
|
334
|
+
const currentBusy = trackBusyEdge(health.activeRequests, health.lastBytesAt ?? 0);
|
|
335
|
+
if (!isStuckBusy(currentBusy, health.lastBytesAt ?? 0)) {
|
|
582
336
|
logInfo(
|
|
583
337
|
`Reusing existing pi-freeflow proxy daemon on http://${HOST}:${port} (${health.activeRequests} active request${health.activeRequests === 1 ? "" : "s"} — not interrupted)`,
|
|
584
338
|
);
|
|
@@ -587,7 +341,7 @@ async function shouldReplaceDaemon(port: number, remoteVer: string): Promise<boo
|
|
|
587
341
|
logWarn(`busy daemon on :${port} stuck (busy 5min+, no forwarded bytes 60s+) — bypassing busy guard`);
|
|
588
342
|
return true;
|
|
589
343
|
}
|
|
590
|
-
|
|
344
|
+
resetBusySince();
|
|
591
345
|
return true;
|
|
592
346
|
}
|
|
593
347
|
|
|
@@ -800,132 +554,6 @@ export async function ensureProxyReady(
|
|
|
800
554
|
return { ok: false, port, reason: proxyDownMessage(port) };
|
|
801
555
|
}
|
|
802
556
|
|
|
803
|
-
/** True when any path segment of `filePath` is `node_modules`. Node refuses to
|
|
804
|
-
* strip types for files under such a path (ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING),
|
|
805
|
-
* so a Node-spawned daemon must run from a staged copy elsewhere. */
|
|
806
|
-
function isUnderNodeModules(filePath: string): boolean {
|
|
807
|
-
return filePath.split(/[\\/]/).includes("node_modules");
|
|
808
|
-
}
|
|
809
|
-
|
|
810
|
-
function readPackageJsonObject(dir: string): Record<string, unknown> | null {
|
|
811
|
-
try {
|
|
812
|
-
const raw: unknown = JSON.parse(fs.readFileSync(path.join(dir, "package.json"), "utf8"));
|
|
813
|
-
return raw && typeof raw === "object" ? (raw as Record<string, unknown>) : null;
|
|
814
|
-
} catch {
|
|
815
|
-
return null;
|
|
816
|
-
}
|
|
817
|
-
}
|
|
818
|
-
|
|
819
|
-
/** Resolve a dependency's package directory the way Node would: walk up the
|
|
820
|
-
* node_modules chain from the importing package's root. */
|
|
821
|
-
function resolvePackageDir(name: string, fromDir: string): string | null {
|
|
822
|
-
let dir = fromDir;
|
|
823
|
-
for (; ;) {
|
|
824
|
-
const pkgJson = path.join(dir, "node_modules", name, "package.json");
|
|
825
|
-
if (fs.existsSync(pkgJson)) return path.dirname(pkgJson);
|
|
826
|
-
const parent = path.dirname(dir);
|
|
827
|
-
if (parent === dir) return null;
|
|
828
|
-
dir = parent;
|
|
829
|
-
}
|
|
830
|
-
}
|
|
831
|
-
|
|
832
|
-
/** Link (or copy, when linking is unavailable) each runtime dependency beside
|
|
833
|
-
* the staged sources so bare imports such as `undici` still resolve. */
|
|
834
|
-
function stageRuntimeDependencies(pkgRoot: string, pkgJson: Record<string, unknown>, stagedDir: string): void {
|
|
835
|
-
const deps = pkgJson.dependencies;
|
|
836
|
-
if (!deps || typeof deps !== "object") return;
|
|
837
|
-
const nodeModulesDir = path.join(stagedDir, "node_modules");
|
|
838
|
-
for (const name of Object.keys(deps as Record<string, unknown>)) {
|
|
839
|
-
const target = resolvePackageDir(name, pkgRoot);
|
|
840
|
-
if (target === null) {
|
|
841
|
-
logWarn("daemon dependency not found for staging", { dependency: name });
|
|
842
|
-
continue;
|
|
843
|
-
}
|
|
844
|
-
try {
|
|
845
|
-
fs.mkdirSync(nodeModulesDir, { recursive: true });
|
|
846
|
-
// Junction on Windows (no elevated privileges needed); dir symlink elsewhere.
|
|
847
|
-
fs.symlinkSync(target, path.join(nodeModulesDir, name), process.platform === "win32" ? "junction" : "dir");
|
|
848
|
-
} catch {
|
|
849
|
-
try {
|
|
850
|
-
fs.cpSync(target, path.join(nodeModulesDir, name), { recursive: true, dereference: true });
|
|
851
|
-
} catch (e) {
|
|
852
|
-
logWarn("daemon dependency staging failed", { dependency: name, error: String(e) });
|
|
853
|
-
}
|
|
854
|
-
}
|
|
855
|
-
}
|
|
856
|
-
}
|
|
857
|
-
|
|
858
|
-
/** Best-effort prune of staged daemon sources from earlier versions (1 day TTL). */
|
|
859
|
-
function pruneStaleDaemonSrcDirs(root: string, currentVersion: string): void {
|
|
860
|
-
try {
|
|
861
|
-
const now = Date.now();
|
|
862
|
-
for (const entry of fs.readdirSync(root)) {
|
|
863
|
-
if (entry === currentVersion) continue;
|
|
864
|
-
const full = path.join(root, entry);
|
|
865
|
-
try {
|
|
866
|
-
if (now - fs.statSync(full).mtimeMs > 24 * 60 * 60 * 1000) {
|
|
867
|
-
fs.rmSync(full, { recursive: true, force: true });
|
|
868
|
-
}
|
|
869
|
-
} catch { }
|
|
870
|
-
}
|
|
871
|
-
} catch { }
|
|
872
|
-
}
|
|
873
|
-
|
|
874
|
-
/**
|
|
875
|
-
* Materialize the daemon's sources outside node_modules so a Node runtime can
|
|
876
|
-
* execute `src/daemon.ts` (Node refuses type stripping for any file under a
|
|
877
|
-
* node_modules path, and this package ships TypeScript sources). Copies
|
|
878
|
-
* `src/` + `package.json` into a per-version cache directory, stages the
|
|
879
|
-
* runtime dependencies beside them, and returns the staged daemon entry.
|
|
880
|
-
* Returns the input path unchanged when the script is already outside
|
|
881
|
-
* node_modules (dev checkouts) or the package root is unreadable.
|
|
882
|
-
*/
|
|
883
|
-
export function materializeDaemonSources(scriptPath: string, cacheDir?: string): string {
|
|
884
|
-
if (!isUnderNodeModules(scriptPath)) return scriptPath;
|
|
885
|
-
const pkgRoot = path.dirname(path.dirname(scriptPath));
|
|
886
|
-
const pkgJson = readPackageJsonObject(pkgRoot);
|
|
887
|
-
const version = pkgJson !== null && typeof pkgJson.version === "string" ? pkgJson.version : "";
|
|
888
|
-
if (pkgJson === null || !version) return scriptPath;
|
|
889
|
-
|
|
890
|
-
const root = cacheDir ?? resolveDaemonSrcCacheDir();
|
|
891
|
-
const finalDir = path.join(root, version);
|
|
892
|
-
const finalEntry = path.join(finalDir, "src", "daemon.ts");
|
|
893
|
-
if (fs.existsSync(finalEntry) && fs.existsSync(path.join(finalDir, "package.json"))) {
|
|
894
|
-
pruneStaleDaemonSrcDirs(root, version);
|
|
895
|
-
return finalEntry;
|
|
896
|
-
}
|
|
897
|
-
|
|
898
|
-
const tmpDir = `${finalDir}.tmp-${process.pid}-${randomUUID().slice(0, 8)}`;
|
|
899
|
-
try {
|
|
900
|
-
fs.mkdirSync(path.join(tmpDir, "src"), { recursive: true });
|
|
901
|
-
fs.cpSync(path.join(pkgRoot, "src"), path.join(tmpDir, "src"), { recursive: true });
|
|
902
|
-
fs.copyFileSync(path.join(pkgRoot, "package.json"), path.join(tmpDir, "package.json"));
|
|
903
|
-
stageRuntimeDependencies(pkgRoot, pkgJson, tmpDir);
|
|
904
|
-
try {
|
|
905
|
-
fs.renameSync(tmpDir, finalDir);
|
|
906
|
-
logInfo(`staged proxy daemon sources outside node_modules at ${finalDir}`);
|
|
907
|
-
} catch (e) {
|
|
908
|
-
// A concurrent starter won the rename (or the dir appeared meanwhile):
|
|
909
|
-
// its copy is byte-identical for the same version, so reuse it.
|
|
910
|
-
try {
|
|
911
|
-
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
912
|
-
} catch { }
|
|
913
|
-
if (!fs.existsSync(finalEntry)) {
|
|
914
|
-
logWarn("daemon source staging failed", { error: String(e), dir: finalDir });
|
|
915
|
-
return scriptPath;
|
|
916
|
-
}
|
|
917
|
-
}
|
|
918
|
-
} catch (e) {
|
|
919
|
-
try {
|
|
920
|
-
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
921
|
-
} catch { }
|
|
922
|
-
logWarn("daemon source staging failed — spawning from the installed path", { error: String(e), dir: tmpDir });
|
|
923
|
-
return scriptPath;
|
|
924
|
-
}
|
|
925
|
-
pruneStaleDaemonSrcDirs(root, version);
|
|
926
|
-
return finalEntry;
|
|
927
|
-
}
|
|
928
|
-
|
|
929
557
|
interface DaemonChild {
|
|
930
558
|
pid?: number;
|
|
931
559
|
exitCode: number | null;
|
|
@@ -1029,19 +657,49 @@ export async function waitForReady(
|
|
|
1029
657
|
return false;
|
|
1030
658
|
}
|
|
1031
659
|
|
|
660
|
+
/**
|
|
661
|
+
* True when the running daemon's zenTransport snapshot disagrees with this
|
|
662
|
+
* client's PI_FREEFLOW_ZEN_TRANSPORT env setting. A daemon started with
|
|
663
|
+
* PI_FREEFLOW_ZEN_TRANSPORT=bun-sidecar reports mode "bun-sidecar" or
|
|
664
|
+
* reason "bun-absent" / "bun-probe-failed" (never "transport-flag-off"),
|
|
665
|
+
* so a missing/broken Bun binary does not trigger a respawn loop.
|
|
666
|
+
*/
|
|
667
|
+
export function isZenTransportEnvMismatch(
|
|
668
|
+
daemonZenTransport: { mode: string; bunVersion: string | null; reason: string } | undefined,
|
|
669
|
+
clientEnvValue = process.env[ZEN_TRANSPORT_ENV],
|
|
670
|
+
): boolean {
|
|
671
|
+
if (!daemonZenTransport) return false;
|
|
672
|
+
const clientWantsSidecar = clientEnvValue === "bun-sidecar";
|
|
673
|
+
const daemonHadSidecarFlag =
|
|
674
|
+
daemonZenTransport.mode === "bun-sidecar" ||
|
|
675
|
+
daemonZenTransport.reason === "bun-absent" ||
|
|
676
|
+
daemonZenTransport.reason === "bun-probe-failed";
|
|
677
|
+
return clientWantsSidecar !== daemonHadSidecarFlag;
|
|
678
|
+
}
|
|
679
|
+
|
|
1032
680
|
async function probeAndMaybeReplace(
|
|
1033
681
|
port: number,
|
|
1034
682
|
label: string,
|
|
1035
683
|
probeTimeoutMs = 150,
|
|
1036
684
|
): Promise<number | null> {
|
|
1037
685
|
if (!(await isProxyAlive(port, probeTimeoutMs))) return null;
|
|
1038
|
-
const
|
|
686
|
+
const health = await getDaemonHealth(port);
|
|
687
|
+
const ver = health ? health.version : null;
|
|
1039
688
|
if (ver !== null && ver !== PKG_VERSION) {
|
|
1040
689
|
logWarn(`detected proxy daemon version mismatch: running v${ver}, required v${PKG_VERSION} — auto-restarting`);
|
|
1041
690
|
await killStaleDaemon(port, ver, label);
|
|
1042
691
|
lastSpawnAt = 0;
|
|
1043
692
|
return null;
|
|
1044
693
|
}
|
|
694
|
+
if (health !== null && isZenTransportEnvMismatch(health.zenTransport)) {
|
|
695
|
+
logWarn(
|
|
696
|
+
`detected proxy daemon zenTransport mismatch: running ${health.zenTransport?.mode ?? "node"} (${health.zenTransport?.reason ?? "unknown"}), client wants ${process.env[ZEN_TRANSPORT_ENV] === "bun-sidecar" ? "bun-sidecar" : "node"} — auto-restarting`,
|
|
697
|
+
);
|
|
698
|
+
if (await killStaleDaemon(port, ver ?? PKG_VERSION, label)) {
|
|
699
|
+
lastSpawnAt = 0;
|
|
700
|
+
return null;
|
|
701
|
+
}
|
|
702
|
+
}
|
|
1045
703
|
return port;
|
|
1046
704
|
}
|
|
1047
705
|
|