pi-freeflow 1.9.5 → 1.9.6
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 +12 -0
- package/package.json +61 -61
- package/src/client.ts +23 -5
- package/src/commands.ts +22 -6
- package/src/index.ts +2 -1
- package/src/lease.ts +4 -2
- package/src/proxy.ts +76 -9
- package/src/relay-state.ts +19 -19
- package/src/update-checker.ts +5 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to pi-freeflow. Public, user-visible behavior only.
|
|
4
4
|
|
|
5
|
+
## 1.9.6 - 2026-09-03
|
|
6
|
+
|
|
7
|
+
### Fixes
|
|
8
|
+
- **Hiding the status widget now sticks.** `/freeflow hide` survives restarts, new sessions, and background update notices — previously the widget could reappear on the next session.
|
|
9
|
+
- **Calmer startup with many sessions.** Sessions starting at the same time now converge on a single background proxy instead of each spawning its own; a spawn that never becomes ready falls back to in-process mode instead of running untracked.
|
|
10
|
+
- **Survives daemon restarts mid-session.** If the background proxy is replaced while sessions run, connected sessions re-register automatically instead of silently losing their lease (which could retire the daemon mid-use).
|
|
11
|
+
- **Stale-version cleanup targets the right process.** Windows port matching is now exact, and Linux without `lsof`/`fuser` resolves the holder via `/proc` instead of giving up.
|
|
12
|
+
- **`/freeflow logs --follow` no longer runs forever.** The live tail stops when the session ends and caps burst output; the update/install subprocess now has a 2-minute timeout.
|
|
13
|
+
|
|
14
|
+
### Validation
|
|
15
|
+
- `npx tsc --noEmit` clean, `npm test` 299 (298 pass + 1 Linux-only skip) + smoke green on Windows; stress harness 7/7 on Windows and Linux `acerblue-local`. **`macOS not tested`** this cycle.
|
|
16
|
+
|
|
5
17
|
## 1.9.5 - 2026-09-03
|
|
6
18
|
|
|
7
19
|
### Changes
|
package/package.json
CHANGED
|
@@ -1,61 +1,61 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "pi-freeflow",
|
|
3
|
-
"type": "module",
|
|
4
|
-
"version": "1.9.
|
|
5
|
-
"description": "Thin provider for OMP/Pi — model list + dumb relay proxy + log; host pi-ai owns thinking/normalization",
|
|
6
|
-
"main": "extensions/index.ts",
|
|
7
|
-
"types": "src/index.ts",
|
|
8
|
-
"keywords": [
|
|
9
|
-
"pi-package",
|
|
10
|
-
"pi-extension",
|
|
11
|
-
"oh-my-pi",
|
|
12
|
-
"omp",
|
|
13
|
-
"free-models",
|
|
14
|
-
"opencode",
|
|
15
|
-
"kilocode",
|
|
16
|
-
"ai-models",
|
|
17
|
-
"relay"
|
|
18
|
-
],
|
|
19
|
-
"author": "trefeon",
|
|
20
|
-
"license": "MIT",
|
|
21
|
-
"repository": {
|
|
22
|
-
"type": "git",
|
|
23
|
-
"url": "git+https://github.com/trefeon/pi-freeflow.git"
|
|
24
|
-
},
|
|
25
|
-
"homepage": "https://github.com/trefeon/pi-freeflow#readme",
|
|
26
|
-
"engines": {
|
|
27
|
-
"node": ">=22.19.0"
|
|
28
|
-
},
|
|
29
|
-
"omp": {
|
|
30
|
-
"extensions": [
|
|
31
|
-
"./extensions"
|
|
32
|
-
]
|
|
33
|
-
},
|
|
34
|
-
"pi": {
|
|
35
|
-
"extensions": [
|
|
36
|
-
"./extensions"
|
|
37
|
-
]
|
|
38
|
-
},
|
|
39
|
-
"files": [
|
|
40
|
-
"extensions",
|
|
41
|
-
"src",
|
|
42
|
-
"README.md",
|
|
43
|
-
"CHANGELOG.md",
|
|
44
|
-
"LICENSE"
|
|
45
|
-
],
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "pi-freeflow",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.9.6",
|
|
5
|
+
"description": "Thin provider for OMP/Pi — model list + dumb relay proxy + log; host pi-ai owns thinking/normalization",
|
|
6
|
+
"main": "extensions/index.ts",
|
|
7
|
+
"types": "src/index.ts",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"pi-package",
|
|
10
|
+
"pi-extension",
|
|
11
|
+
"oh-my-pi",
|
|
12
|
+
"omp",
|
|
13
|
+
"free-models",
|
|
14
|
+
"opencode",
|
|
15
|
+
"kilocode",
|
|
16
|
+
"ai-models",
|
|
17
|
+
"relay"
|
|
18
|
+
],
|
|
19
|
+
"author": "trefeon",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/trefeon/pi-freeflow.git"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/trefeon/pi-freeflow#readme",
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=22.19.0"
|
|
28
|
+
},
|
|
29
|
+
"omp": {
|
|
30
|
+
"extensions": [
|
|
31
|
+
"./extensions"
|
|
32
|
+
]
|
|
33
|
+
},
|
|
34
|
+
"pi": {
|
|
35
|
+
"extensions": [
|
|
36
|
+
"./extensions"
|
|
37
|
+
]
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"extensions",
|
|
41
|
+
"src",
|
|
42
|
+
"README.md",
|
|
43
|
+
"CHANGELOG.md",
|
|
44
|
+
"LICENSE"
|
|
45
|
+
],
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@changesets/cli": "^2.27.0",
|
|
48
|
+
"@types/node": "^22.13.9",
|
|
49
|
+
"typescript": "^5.8.2",
|
|
50
|
+
"vitepress": "^1.6.4"
|
|
51
|
+
},
|
|
52
|
+
"dependencies": {},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"test": "node --experimental-strip-types --import ./test/setup.mjs --test --test-concurrency=1 test/**/*.test.ts",
|
|
55
|
+
"typecheck": "tsc --noEmit",
|
|
56
|
+
"smoke": "node --experimental-strip-types -e \"import('./extensions/index.ts').then(() => console.log('✓ Smoke test passed: extensions/index.ts loaded successfully')).catch(err => { console.error(err); process.exit(1); })\"",
|
|
57
|
+
"changeset": "changeset",
|
|
58
|
+
"docs:dev": "vitepress dev docs",
|
|
59
|
+
"docs:build": "vitepress build docs"
|
|
60
|
+
}
|
|
61
|
+
}
|
package/src/client.ts
CHANGED
|
@@ -63,7 +63,7 @@ function isSpawnEnabled(): boolean {
|
|
|
63
63
|
return process.env[DAEMON_SPAWN_ENV] !== "0";
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
type ControlResult = "ok" | "legacy" | "gone";
|
|
66
|
+
type ControlResult = "ok" | "unknown" | "legacy" | "gone";
|
|
67
67
|
|
|
68
68
|
async function controlCall(
|
|
69
69
|
port: number,
|
|
@@ -77,7 +77,12 @@ async function controlCall(
|
|
|
77
77
|
body: JSON.stringify(payload),
|
|
78
78
|
signal: AbortSignal.timeout(1500),
|
|
79
79
|
});
|
|
80
|
-
|
|
80
|
+
if (!res.ok) return "legacy";
|
|
81
|
+
try {
|
|
82
|
+
const data: unknown = await res.json();
|
|
83
|
+
if (data && typeof data === "object" && "ok" in data && data.ok === false) return "unknown";
|
|
84
|
+
} catch {}
|
|
85
|
+
return "ok";
|
|
81
86
|
} catch {
|
|
82
87
|
return "gone";
|
|
83
88
|
}
|
|
@@ -122,6 +127,9 @@ async function beatOnce(port: number): Promise<void> {
|
|
|
122
127
|
const result = await controlCall(port, "/_client/heartbeat", { id: CLIENT_ID });
|
|
123
128
|
if (result === "gone") {
|
|
124
129
|
void ensureDaemon();
|
|
130
|
+
} else if (result === "unknown") {
|
|
131
|
+
// Daemon restarted since attach: re-register our lease.
|
|
132
|
+
await attachTo(port);
|
|
125
133
|
}
|
|
126
134
|
}
|
|
127
135
|
|
|
@@ -259,13 +267,22 @@ export async function ensureDaemon(): Promise<number> {
|
|
|
259
267
|
}
|
|
260
268
|
|
|
261
269
|
if (isSpawnEnabled()) {
|
|
270
|
+
// Cross-process race guard: a sibling session may be spawning right
|
|
271
|
+
// now (singleflight is per-process). Jitter + re-probe converts most
|
|
272
|
+
// cold-start races into attach instead of N detached spawns.
|
|
273
|
+
await new Promise<void>((r) => setTimeout(r, 100 + Math.random() * 200));
|
|
274
|
+
if (await isProxyAlive(PORT)) {
|
|
275
|
+
await attachTo(PORT);
|
|
276
|
+
return PORT;
|
|
277
|
+
}
|
|
262
278
|
spawnDaemonProcess();
|
|
263
279
|
const ready = await waitForReady(PORT, DAEMON_READY_TIMEOUT_MS);
|
|
264
280
|
if (ready) {
|
|
265
281
|
const ver = await getDaemonVersion(PORT);
|
|
266
282
|
if (ver !== null && ver !== PKG_VERSION) {
|
|
267
|
-
|
|
268
|
-
|
|
283
|
+
if (await killStaleDaemon(PORT, ver, "proxy daemon")) {
|
|
284
|
+
lastSpawnAt = 0; // kill-confirmed retry bypasses the spawn throttle
|
|
285
|
+
spawnDaemonProcess();
|
|
269
286
|
const retryReady = await waitForReady(PORT, DAEMON_READY_TIMEOUT_MS);
|
|
270
287
|
if (retryReady) {
|
|
271
288
|
await attachTo(PORT);
|
|
@@ -282,7 +299,8 @@ export async function ensureDaemon(): Promise<number> {
|
|
|
282
299
|
} else {
|
|
283
300
|
logWarn("daemon spawn did not become ready — is the port blocked?");
|
|
284
301
|
}
|
|
285
|
-
|
|
302
|
+
// Fall through to the in-process fallback below — never hand the caller
|
|
303
|
+
// a port we did not attach to (no lease, no heartbeat).
|
|
286
304
|
}
|
|
287
305
|
|
|
288
306
|
try {
|
package/src/commands.ts
CHANGED
|
@@ -68,6 +68,7 @@ function spawnWithProgress(
|
|
|
68
68
|
cmd: string,
|
|
69
69
|
args: string[],
|
|
70
70
|
ctx: ExtensionContext,
|
|
71
|
+
timeoutMs = 120_000,
|
|
71
72
|
): Promise<number> {
|
|
72
73
|
const { promise, resolve } = Promise.withResolvers<number>();
|
|
73
74
|
try {
|
|
@@ -76,6 +77,16 @@ function spawnWithProgress(
|
|
|
76
77
|
stdio: "pipe",
|
|
77
78
|
windowsHide: true,
|
|
78
79
|
});
|
|
80
|
+
const timer = setTimeout(() => {
|
|
81
|
+
try { child.kill(); } catch {}
|
|
82
|
+
ctx.ui.notify(`spawn ${cmd} timed out after ${timeoutMs}ms — killed`, "warning");
|
|
83
|
+
resolve(1);
|
|
84
|
+
}, timeoutMs);
|
|
85
|
+
try { timer.unref?.(); } catch {}
|
|
86
|
+
const done = (code: number | null): void => {
|
|
87
|
+
clearTimeout(timer);
|
|
88
|
+
resolve(code ?? 0);
|
|
89
|
+
};
|
|
79
90
|
child.stdout?.on("data", (d: Buffer) => {
|
|
80
91
|
const s = String(d).trim();
|
|
81
92
|
if (s) ctx.ui.notify(s, "info");
|
|
@@ -85,10 +96,11 @@ function spawnWithProgress(
|
|
|
85
96
|
if (s) ctx.ui.notify(s, "info");
|
|
86
97
|
});
|
|
87
98
|
child.on("error", (err: Error) => {
|
|
99
|
+
clearTimeout(timer);
|
|
88
100
|
ctx.ui.notify(`spawn ${cmd} failed: ${err.message}`, "warning");
|
|
89
101
|
resolve(1);
|
|
90
102
|
});
|
|
91
|
-
child.on("close",
|
|
103
|
+
child.on("close", done);
|
|
92
104
|
} catch (e) {
|
|
93
105
|
ctx.ui.notify(`spawn ${cmd} failed: ${(e as Error).message}`, "warning");
|
|
94
106
|
resolve(1);
|
|
@@ -100,8 +112,15 @@ function spawnWithProgress(
|
|
|
100
112
|
* Follow-mode log poller: tracks the count of matched lines already printed
|
|
101
113
|
* and notifies only lines beyond it, so repeated ticks never re-notify the
|
|
102
114
|
* same tail. One poller at a time — a re-run clears the previous interval.
|
|
115
|
+
* Stop with stopLogsFollow (session shutdown / non-follow commands).
|
|
103
116
|
*/
|
|
104
117
|
let logsFollowTimer: NodeJS.Timeout | null = null;
|
|
118
|
+
export function stopLogsFollow(): void {
|
|
119
|
+
if (logsFollowTimer) {
|
|
120
|
+
clearInterval(logsFollowTimer);
|
|
121
|
+
logsFollowTimer = null;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
105
124
|
function startLogsFollow(
|
|
106
125
|
ctx: ExtensionContext,
|
|
107
126
|
filterLevel: LogLevel | null,
|
|
@@ -110,10 +129,7 @@ function startLogsFollow(
|
|
|
110
129
|
filterText: string | null,
|
|
111
130
|
baselineMatched: number,
|
|
112
131
|
): void {
|
|
113
|
-
|
|
114
|
-
clearInterval(logsFollowTimer);
|
|
115
|
-
logsFollowTimer = null;
|
|
116
|
-
}
|
|
132
|
+
stopLogsFollow();
|
|
117
133
|
let lastPrinted = baselineMatched;
|
|
118
134
|
logsFollowTimer = setInterval(() => {
|
|
119
135
|
try {
|
|
@@ -127,7 +143,7 @@ function startLogsFollow(
|
|
|
127
143
|
if (newCount <= 0) return;
|
|
128
144
|
const newLines = tail.lines.slice(
|
|
129
145
|
tail.lines.length - Math.min(newCount, tail.lines.length),
|
|
130
|
-
);
|
|
146
|
+
).slice(-50);
|
|
131
147
|
if (newLines.length > 0) {
|
|
132
148
|
ctx.ui.notify(newLines.join("\n"), "info");
|
|
133
149
|
}
|
package/src/index.ts
CHANGED
|
@@ -17,7 +17,7 @@ import {
|
|
|
17
17
|
setAliveCatalog,
|
|
18
18
|
} from "./catalog.ts";
|
|
19
19
|
import { ensureDaemon as ensureClientDaemon, getClientPort, stopHeartbeat } from "./client.ts";
|
|
20
|
-
import { createCommandSpec, updateStatusBar } from "./commands.ts";
|
|
20
|
+
import { createCommandSpec, stopLogsFollow, updateStatusBar } from "./commands.ts";
|
|
21
21
|
import { HOST, ONBOARDED_FLAG_FILE, PORT } from "./config.ts";
|
|
22
22
|
import { logInfo, logWarn } from "./logger.ts";
|
|
23
23
|
import { ALL_MODELS, KILO_MODEL_IDS, MODEL_MAP, resolveCanonicalModelId } from "./models.ts";
|
|
@@ -330,5 +330,6 @@ export default async function (pi: ExtensionAPI): Promise<void> {
|
|
|
330
330
|
});
|
|
331
331
|
pi.on?.("session_shutdown", () => {
|
|
332
332
|
stopHeartbeat();
|
|
333
|
+
stopLogsFollow();
|
|
333
334
|
});
|
|
334
335
|
}
|
package/src/lease.ts
CHANGED
|
@@ -32,11 +32,13 @@ export function registerClient(clientId: string): void {
|
|
|
32
32
|
leases.set(clientId, Date.now());
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
/** Renew an existing client lease
|
|
36
|
-
export function renewClient(clientId: string):
|
|
35
|
+
/** Renew an existing client lease. Returns false when the id is unknown (daemon restarted). */
|
|
36
|
+
export function renewClient(clientId: string): boolean {
|
|
37
37
|
if (leases.has(clientId)) {
|
|
38
38
|
leases.set(clientId, Date.now());
|
|
39
|
+
return true;
|
|
39
40
|
}
|
|
41
|
+
return false;
|
|
40
42
|
}
|
|
41
43
|
|
|
42
44
|
/** Remove a client lease (graceful detach on session end). */
|
package/src/proxy.ts
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import { randomUUID } from "node:crypto";
|
|
9
|
+
import fs from "node:fs";
|
|
9
10
|
import { execSync } from "node:child_process";
|
|
10
11
|
import * as http from "node:http";
|
|
11
12
|
import * as https from "node:https";
|
|
@@ -177,6 +178,63 @@ export async function getDaemonVersion(port: number): Promise<string | null> {
|
|
|
177
178
|
return h ? h.version : null;
|
|
178
179
|
}
|
|
179
180
|
|
|
181
|
+
/**
|
|
182
|
+
* Linux fallback when neither lsof nor fuser exists: resolve the LISTEN
|
|
183
|
+
* socket inode for `port` via /proc/net/tcp{,6}, then find the owning pid by
|
|
184
|
+
* scanning /proc fds. Dependency-free, same-user only, never self-kills.
|
|
185
|
+
*/
|
|
186
|
+
function killViaProcNet(port: number): boolean {
|
|
187
|
+
if (process.platform !== "linux") return false;
|
|
188
|
+
try {
|
|
189
|
+
const hexPort = port.toString(16).toUpperCase().padStart(4, "0");
|
|
190
|
+
const inodes = new Set<string>();
|
|
191
|
+
for (const table of ["/proc/net/tcp", "/proc/net/tcp6"]) {
|
|
192
|
+
let text = "";
|
|
193
|
+
try {
|
|
194
|
+
text = fs.readFileSync(table, "utf8");
|
|
195
|
+
} catch {
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
for (const line of text.split("\n").slice(1)) {
|
|
199
|
+
const cols = line.trim().split(/\s+/);
|
|
200
|
+
if (cols.length < 10) continue;
|
|
201
|
+
const addr = cols[1].split(":");
|
|
202
|
+
if (addr.length !== 2 || addr[1].toUpperCase() !== hexPort) continue;
|
|
203
|
+
if (cols[3] !== "0A") continue; // LISTEN only
|
|
204
|
+
inodes.add(cols[9]);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
if (inodes.size === 0) return false;
|
|
208
|
+
for (const pid of fs.readdirSync("/proc")) {
|
|
209
|
+
if (!/^\d+$/.test(pid) || Number(pid) === process.pid) continue;
|
|
210
|
+
let fds: string[];
|
|
211
|
+
try {
|
|
212
|
+
fds = fs.readdirSync(`/proc/${pid}/fd`);
|
|
213
|
+
} catch {
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
for (const fd of fds) {
|
|
217
|
+
let link = "";
|
|
218
|
+
try {
|
|
219
|
+
link = fs.readlinkSync(`/proc/${pid}/fd/${fd}`);
|
|
220
|
+
} catch {
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
const sock = link.match(/^socket:\[(\d+)\]$/);
|
|
224
|
+
if (sock && inodes.has(sock[1])) {
|
|
225
|
+
try {
|
|
226
|
+
process.kill(Number(pid), "SIGKILL");
|
|
227
|
+
return true;
|
|
228
|
+
} catch {
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
} catch {}
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
|
|
180
238
|
export async function killPortHolder(port: number): Promise<boolean> {
|
|
181
239
|
if (!Number.isInteger(port) || port < 1 || port > 65535) return false;
|
|
182
240
|
try {
|
|
@@ -189,8 +247,10 @@ export async function killPortHolder(port: number): Promise<boolean> {
|
|
|
189
247
|
}) as string;
|
|
190
248
|
for (const line of out.split("\n")) {
|
|
191
249
|
if (!line.includes("LISTENING")) continue;
|
|
192
|
-
if (!line.includes(`:${port}`)) continue;
|
|
193
250
|
const parts = line.trim().split(/\s+/);
|
|
251
|
+
// Match the port on the Local Address column only — a raw
|
|
252
|
+
// substring match can hit longer ports (:2818 vs :28180).
|
|
253
|
+
if (!parts[1] || !parts[1].endsWith(`:${port}`)) continue;
|
|
194
254
|
const pid = parts[parts.length - 1];
|
|
195
255
|
if (!pid || !/^\d+$/.test(pid)) continue;
|
|
196
256
|
// Windows netstat report: 127.0.0.1:38180 ... LISTENING/PID
|
|
@@ -206,13 +266,14 @@ export async function killPortHolder(port: number): Promise<boolean> {
|
|
|
206
266
|
encoding: "utf8",
|
|
207
267
|
timeout: 3000,
|
|
208
268
|
}) as string;
|
|
209
|
-
const
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
269
|
+
const pids = out.trim().split(/\s+/).filter((p) => /^\d+$/.test(p) && Number(p) !== process.pid);
|
|
270
|
+
// lsof matches both ends of a connection: our own recent probe socket
|
|
271
|
+
// (TIME_WAIT) can sort first, so skip self instead of taking [0].
|
|
272
|
+
if (pids.length === 0) return killViaProcNet(port);
|
|
273
|
+
execSync(`kill -9 ${pids[0]}`, { timeout: 3000, stdio: "ignore" });
|
|
213
274
|
return true;
|
|
214
275
|
} catch {
|
|
215
|
-
return
|
|
276
|
+
return killViaProcNet(port);
|
|
216
277
|
}
|
|
217
278
|
} catch {
|
|
218
279
|
return false;
|
|
@@ -271,8 +332,15 @@ export function handleControlRequest(
|
|
|
271
332
|
return;
|
|
272
333
|
}
|
|
273
334
|
if (reqPathname === "/_client/attach") registerClient(clientId);
|
|
274
|
-
else if (reqPathname === "/_client/heartbeat")
|
|
275
|
-
|
|
335
|
+
else if (reqPathname === "/_client/heartbeat") {
|
|
336
|
+
// Unknown id = daemon restarted since attach. Report ok:false so the
|
|
337
|
+
// client re-attaches instead of heartbeating lease-less.
|
|
338
|
+
if (!renewClient(clientId)) {
|
|
339
|
+
res.writeHead(200, { "content-type": "application/json" });
|
|
340
|
+
res.end(JSON.stringify({ ok: false }));
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
} else if (reqPathname === "/_client/detach") unregisterClient(clientId);
|
|
276
344
|
else {
|
|
277
345
|
res.writeHead(404, { "content-type": "application/json" });
|
|
278
346
|
res.end(JSON.stringify({ error: "not found" }));
|
|
@@ -345,7 +413,6 @@ export function startProxy(
|
|
|
345
413
|
if (handleHealthRequest(req, res, realPort, getActiveRequests())) return;
|
|
346
414
|
}
|
|
347
415
|
if (req.method === "GET" && (reqPathname === "/v1/models" || reqPathname === "/v1/models/")) {
|
|
348
|
-
touchActivity();
|
|
349
416
|
const alive = getAliveCatalog();
|
|
350
417
|
const body = JSON.stringify({
|
|
351
418
|
object: "list",
|
package/src/relay-state.ts
CHANGED
|
@@ -117,6 +117,7 @@ function parseRelayState(raw: string): RelayState | null {
|
|
|
117
117
|
: s?.enabled !== false && relays.length > 0,
|
|
118
118
|
url: typeof s?.url === "string" ? s.url.trim() : (relays[0]?.url || ""),
|
|
119
119
|
relays,
|
|
120
|
+
hideWidget: s?.hideWidget === true,
|
|
120
121
|
};
|
|
121
122
|
} catch {
|
|
122
123
|
return null;
|
|
@@ -182,9 +183,10 @@ export function loadRelayState(): RelayState {
|
|
|
182
183
|
if (healed) {
|
|
183
184
|
return healed;
|
|
184
185
|
}
|
|
185
|
-
return { mode: "auto", enabled: true, url: "", relays: [] };
|
|
186
|
+
return { mode: "auto", enabled: true, url: "", relays: [], hideWidget: false };
|
|
186
187
|
}
|
|
187
|
-
const
|
|
188
|
+
const raw = fs.readFileSync(RELAY_STATE_FILE, "utf8");
|
|
189
|
+
const main = parseRelayState(raw);
|
|
188
190
|
if (main) {
|
|
189
191
|
return main;
|
|
190
192
|
}
|
|
@@ -196,9 +198,15 @@ export function loadRelayState(): RelayState {
|
|
|
196
198
|
logWarn("relay state file unusable and no valid backup — starting fresh", {
|
|
197
199
|
path: RELAY_STATE_FILE,
|
|
198
200
|
});
|
|
199
|
-
|
|
201
|
+
// parse returns null on an empty pool by design (wipe-protection above);
|
|
202
|
+
// preserve an explicit hide so hide-with-zero-relays survives reload.
|
|
203
|
+
let hide = false;
|
|
204
|
+
try {
|
|
205
|
+
hide = (JSON.parse(raw) as RelayState)?.hideWidget === true;
|
|
206
|
+
} catch {}
|
|
207
|
+
return { mode: "auto", enabled: true, url: "", relays: [], hideWidget: hide };
|
|
200
208
|
} catch {
|
|
201
|
-
return { mode: "auto", enabled: true, url: "", relays: [] };
|
|
209
|
+
return { mode: "auto", enabled: true, url: "", relays: [], hideWidget: false };
|
|
202
210
|
}
|
|
203
211
|
}
|
|
204
212
|
|
|
@@ -537,9 +545,6 @@ export function setActiveRelayState(s: RelayState, persist = true): void {
|
|
|
537
545
|
// concurrent sessions cannot silently clobber each other's edits.
|
|
538
546
|
|
|
539
547
|
const RELAY_STATE_LOCK_FILE = `${RELAY_STATE_FILE}.lock`;
|
|
540
|
-
const LOCK_WAIT_MAX_MS = 2_000;
|
|
541
|
-
const LOCK_RETRY_START_MS = 25;
|
|
542
|
-
const LOCK_RETRY_MAX_MS = 200;
|
|
543
548
|
/** A lock older than this is presumed abandoned (crashed holder) and taken over. */
|
|
544
549
|
const LOCK_STALE_MS = 30_000;
|
|
545
550
|
|
|
@@ -571,24 +576,19 @@ function tryAcquireRelayLock(): boolean {
|
|
|
571
576
|
|
|
572
577
|
/**
|
|
573
578
|
* Apply a mutation to relay state with a compare-and-swap discipline.
|
|
574
|
-
*
|
|
579
|
+
* Makes a single lock acquisition attempt, loads the freshest on-disk state,
|
|
575
580
|
* runs the updater against it, persists atomically, and releases the lock.
|
|
576
|
-
*
|
|
577
|
-
* already prevents torn files; the lock only serializes write intent.
|
|
581
|
+
* When the lock is held the write proceeds unlocked — the atomic tmp+rename
|
|
582
|
+
* save already prevents torn files; the lock only serializes write intent.
|
|
583
|
+
* (Never spin here: this runs on the proxy request path and blocking the
|
|
584
|
+
* event loop stalls all in-flight streams.)
|
|
578
585
|
* Returns the state that was actually persisted (also becomes the in-memory
|
|
579
586
|
* active state).
|
|
580
587
|
*/
|
|
581
588
|
export function withRelayState(updater: (s: RelayState) => RelayState): RelayState {
|
|
582
|
-
const
|
|
583
|
-
let locked = tryAcquireRelayLock();
|
|
584
|
-
let delay = LOCK_RETRY_START_MS;
|
|
585
|
-
while (!locked && Date.now() < deadline) {
|
|
586
|
-
sleepSync(delay);
|
|
587
|
-
locked = tryAcquireRelayLock();
|
|
588
|
-
delay = Math.min(delay * 2, LOCK_RETRY_MAX_MS);
|
|
589
|
-
}
|
|
589
|
+
const locked = tryAcquireRelayLock();
|
|
590
590
|
if (!locked) {
|
|
591
|
-
logWarn("relay state lock held — proceeding without lock
|
|
591
|
+
logWarn("relay state lock held — proceeding without lock", {
|
|
592
592
|
lock: RELAY_STATE_LOCK_FILE,
|
|
593
593
|
});
|
|
594
594
|
}
|
package/src/update-checker.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { fileURLToPath } from "node:url";
|
|
|
4
4
|
import { UPDATE_CACHE_FILE, UPDATE_CHECK_TTL_MS } from "./config.ts";
|
|
5
5
|
import { logInfo } from "./logger.ts";
|
|
6
6
|
import { logWarn } from "./logger.ts";
|
|
7
|
-
import { getStatusUi } from "./relay-state.ts";
|
|
7
|
+
import { getActiveRelayState, getStatusUi } from "./relay-state.ts";
|
|
8
8
|
import type { ExtensionUIContext } from "./types.ts";
|
|
9
9
|
const REGISTRY_URL = "https://registry.npmjs.org/pi-freeflow/latest";
|
|
10
10
|
/** Strict semver shape: major.minor.patch with optional -prerelease or +build suffix. */
|
|
@@ -143,6 +143,10 @@ export function checkForUpdateInBackground(ui?: ExtensionUIContext | null): void
|
|
|
143
143
|
const local = getLocalVersion();
|
|
144
144
|
if (local && compareVersions(latest, local) > 0) {
|
|
145
145
|
logInfo(`pi-freeflow update available: ${local} -> ${latest} (run /freeflow update)`);
|
|
146
|
+
// Respect hide: log only, never re-show the widget.
|
|
147
|
+
try {
|
|
148
|
+
if (getActiveRelayState().hideWidget) return;
|
|
149
|
+
} catch {}
|
|
146
150
|
const targetUi = ui ?? null;
|
|
147
151
|
if (targetUi?.setStatus) {
|
|
148
152
|
try { targetUi.setStatus("freeflow", `update: ${local} → ${latest}`); } catch {}
|