@f5-sales-demo/xcsh 19.61.4 → 19.61.5
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/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@f5-sales-demo/xcsh",
|
|
4
|
-
"version": "19.61.
|
|
4
|
+
"version": "19.61.5",
|
|
5
5
|
"description": "Coding agent CLI with read, bash, edit, write tools and session management",
|
|
6
6
|
"homepage": "https://github.com/f5-sales-demo/xcsh",
|
|
7
7
|
"author": "Can Boluk",
|
|
@@ -56,13 +56,13 @@
|
|
|
56
56
|
"dependencies": {
|
|
57
57
|
"@agentclientprotocol/sdk": "0.16.1",
|
|
58
58
|
"@mozilla/readability": "^0.6",
|
|
59
|
-
"@f5-sales-demo/xcsh-stats": "19.61.
|
|
60
|
-
"@f5-sales-demo/pi-agent-core": "19.61.
|
|
61
|
-
"@f5-sales-demo/pi-ai": "19.61.
|
|
62
|
-
"@f5-sales-demo/pi-natives": "19.61.
|
|
63
|
-
"@f5-sales-demo/pi-resource-management": "19.61.
|
|
64
|
-
"@f5-sales-demo/pi-tui": "19.61.
|
|
65
|
-
"@f5-sales-demo/pi-utils": "19.61.
|
|
59
|
+
"@f5-sales-demo/xcsh-stats": "19.61.5",
|
|
60
|
+
"@f5-sales-demo/pi-agent-core": "19.61.5",
|
|
61
|
+
"@f5-sales-demo/pi-ai": "19.61.5",
|
|
62
|
+
"@f5-sales-demo/pi-natives": "19.61.5",
|
|
63
|
+
"@f5-sales-demo/pi-resource-management": "19.61.5",
|
|
64
|
+
"@f5-sales-demo/pi-tui": "19.61.5",
|
|
65
|
+
"@f5-sales-demo/pi-utils": "19.61.5",
|
|
66
66
|
"@sinclair/typebox": "^0.34",
|
|
67
67
|
"@xterm/headless": "^6.0",
|
|
68
68
|
"ajv": "^8.20",
|
|
@@ -31,12 +31,19 @@ export class ChatHandler {
|
|
|
31
31
|
#session: AgentSession;
|
|
32
32
|
#activeChats = new Map<string, ActiveChat>();
|
|
33
33
|
#activeHistoryHint: string | undefined;
|
|
34
|
+
#onTurnStart: (() => void) | undefined;
|
|
34
35
|
|
|
35
36
|
constructor(server: BridgeServer, session: AgentSession) {
|
|
36
37
|
this.#server = server;
|
|
37
38
|
this.#session = session;
|
|
38
39
|
}
|
|
39
40
|
|
|
41
|
+
/** Register a callback fired when a chat turn is accepted (used by the worker's
|
|
42
|
+
* manager keepalive to refresh lastSeen at turn start). */
|
|
43
|
+
onTurnStart(cb: () => void): void {
|
|
44
|
+
this.#onTurnStart = cb;
|
|
45
|
+
}
|
|
46
|
+
|
|
40
47
|
attach(): void {
|
|
41
48
|
this.#server.onMessage(msg => {
|
|
42
49
|
if (isChatRequest(msg)) this.#handleChatRequest(msg as unknown as ChatRequest);
|
|
@@ -75,6 +82,7 @@ export class ChatHandler {
|
|
|
75
82
|
spanEmitted: false,
|
|
76
83
|
};
|
|
77
84
|
this.#activeChats.set(id, chat);
|
|
85
|
+
this.#onTurnStart?.();
|
|
78
86
|
|
|
79
87
|
const unsubscribe = this.#session.subscribe((event: AgentSessionEvent) => {
|
|
80
88
|
this.#handleSessionEvent(chat, event);
|
|
@@ -17,7 +17,9 @@ const SHUTDOWN_REASONS = new Set<string>(["superseded", "updated", "manual"]);
|
|
|
17
17
|
export type ControlMsg =
|
|
18
18
|
| { type: "provision"; sessionId: string; tenant: string }
|
|
19
19
|
| { type: "release"; sessionId: string }
|
|
20
|
-
|
|
20
|
+
// An sid-less status is the legacy no-op sink; an sid-carrying status is a
|
|
21
|
+
// keepalive from an actively-chatting worker (see keepaliveFrame/touchLastSeen).
|
|
22
|
+
| { type: "status"; sessionId?: string }
|
|
21
23
|
| { type: "hello" }
|
|
22
24
|
| { type: "shutdown"; reason: ShutdownReason };
|
|
23
25
|
|
|
@@ -42,7 +44,8 @@ function isNonEmpty(v: unknown): v is string {
|
|
|
42
44
|
export function parseControlMsg(raw: unknown): ControlMsg | null {
|
|
43
45
|
if (!raw || typeof raw !== "object") return null;
|
|
44
46
|
const m = raw as Record<string, unknown>;
|
|
45
|
-
if (m.type === "status")
|
|
47
|
+
if (m.type === "status")
|
|
48
|
+
return isNonEmpty(m.sessionId) ? { type: "status", sessionId: m.sessionId } : { type: "status" };
|
|
46
49
|
if (m.type === "provision" && isNonEmpty(m.sessionId) && isTenant(m.tenant))
|
|
47
50
|
return { type: "provision", sessionId: m.sessionId, tenant: m.tenant };
|
|
48
51
|
if (m.type === "release" && isNonEmpty(m.sessionId)) return { type: "release", sessionId: m.sessionId };
|
|
@@ -115,6 +118,27 @@ export function staleKeys(reg: Registry, now: number, idleMs: number): string[]
|
|
|
115
118
|
return out;
|
|
116
119
|
}
|
|
117
120
|
|
|
121
|
+
/** Refresh a worker's `lastSeen` from a keepalive/status frame; true iff a known
|
|
122
|
+
* session was touched. The manager never sees chat traffic (it flows worker↔
|
|
123
|
+
* bridge↔extension), so an actively-used session would otherwise be idle-reaped
|
|
124
|
+
* mid-conversation. An actively-chatting worker emits `keepaliveFrame` to drive
|
|
125
|
+
* this, keeping its session out of `staleKeys`. */
|
|
126
|
+
export function touchLastSeen(reg: Registry, sessionId: string | undefined, now: number): boolean {
|
|
127
|
+
if (!sessionId) return false;
|
|
128
|
+
const w = reg.get(sessionId);
|
|
129
|
+
if (!w) return false;
|
|
130
|
+
w.lastSeen = now;
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** The NDJSON keepalive an actively-chatting worker writes to the manager control
|
|
135
|
+
* socket to refresh its `lastSeen` (consumed by `touchLastSeen`). Null for the
|
|
136
|
+
* unbound `spare` sentinel or an empty id — a spare has no session to keep alive. */
|
|
137
|
+
export function keepaliveFrame(sessionId: string): string | null {
|
|
138
|
+
if (!sessionId || sessionId === "spare") return null;
|
|
139
|
+
return `${JSON.stringify({ type: "status", sessionId })}\n`;
|
|
140
|
+
}
|
|
141
|
+
|
|
118
142
|
/** How many new spares to spawn now to reach `target`, without exceeding the port
|
|
119
143
|
* budget: spares + active workers must fit the discovery range. Never negative. */
|
|
120
144
|
export function sparesToSpawn(
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worker→manager keepalive client.
|
|
3
|
+
*
|
|
4
|
+
* The manager idle-reaps a worker whose `lastSeen` is older than `IDLE_MS`, and
|
|
5
|
+
* `lastSeen` is refreshed ONLY when the manager receives a control frame. Chat
|
|
6
|
+
* traffic flows worker↔bridge↔extension and never reaches the manager, so an
|
|
7
|
+
* actively-used-then-briefly-idle session used to be reaped mid-conversation
|
|
8
|
+
* (the "Turn aborted." after a break). This client emits a `status{sessionId}`
|
|
9
|
+
* keepalive over the manager's UNIX control socket while a turn is in flight and
|
|
10
|
+
* at each turn start, so the manager's sweep leaves an in-use session alive.
|
|
11
|
+
*
|
|
12
|
+
* The socket lifecycle (connect/reconnect) is injected as a `Connector` so the
|
|
13
|
+
* scheduling/emit logic is unit-testable without a real socket; the worker wires
|
|
14
|
+
* a `Bun.connect` transport. Best-effort throughout: if the manager socket is
|
|
15
|
+
* down (e.g. mid-supersede) emits are dropped and the next emit reconnects — so
|
|
16
|
+
* after a manager handoff the keepalive re-targets the successor manager, which
|
|
17
|
+
* has already re-adopted this worker.
|
|
18
|
+
*/
|
|
19
|
+
import { keepaliveFrame } from "./manager-core";
|
|
20
|
+
|
|
21
|
+
/** A live connection to the manager control socket. */
|
|
22
|
+
export interface KeepaliveTransport {
|
|
23
|
+
write(data: string): void;
|
|
24
|
+
close(): void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Opens a transport; `onClose` MUST be invoked when the connection drops so the
|
|
28
|
+
* keepalive knows to reconnect on its next emit. Returns null if it cannot open. */
|
|
29
|
+
export type Connector = (onClose: () => void) => Promise<KeepaliveTransport | null>;
|
|
30
|
+
|
|
31
|
+
export interface ManagerKeepaliveDeps {
|
|
32
|
+
connect: Connector;
|
|
33
|
+
/** Current worker session id (may transition from "spare" to a real id). */
|
|
34
|
+
sessionId: () => string;
|
|
35
|
+
/** True while a chat turn is in flight. */
|
|
36
|
+
busy: () => boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export class ManagerKeepalive {
|
|
40
|
+
#deps: ManagerKeepaliveDeps;
|
|
41
|
+
#transport: KeepaliveTransport | null = null;
|
|
42
|
+
#connecting = false;
|
|
43
|
+
#pending = false; // a keepalive is due once a connection is (re)established
|
|
44
|
+
#stopped = false;
|
|
45
|
+
|
|
46
|
+
constructor(deps: ManagerKeepaliveDeps) {
|
|
47
|
+
this.#deps = deps;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Periodic tick (driven by the worker's interval): emit iff a turn is busy. */
|
|
51
|
+
tick(): void {
|
|
52
|
+
if (this.#deps.busy()) this.#emit();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** One-shot emit at the start of each turn — refreshes lastSeen immediately so
|
|
56
|
+
* a session with long think-time between turns is never reaped between them. */
|
|
57
|
+
turnStart(): void {
|
|
58
|
+
this.#emit();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Stop emitting and close any open transport. */
|
|
62
|
+
stop(): void {
|
|
63
|
+
this.#stopped = true;
|
|
64
|
+
this.#pending = false;
|
|
65
|
+
this.#transport?.close();
|
|
66
|
+
this.#transport = null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
#emit(): void {
|
|
70
|
+
if (this.#stopped) return;
|
|
71
|
+
const frame = keepaliveFrame(this.#deps.sessionId());
|
|
72
|
+
if (!frame) return; // spare / unbound → nothing to keep alive (also skips connecting)
|
|
73
|
+
if (this.#transport) {
|
|
74
|
+
try {
|
|
75
|
+
this.#transport.write(frame);
|
|
76
|
+
} catch {
|
|
77
|
+
this.#transport = null; // dead socket → drop and reconnect on next emit
|
|
78
|
+
this.#pending = true;
|
|
79
|
+
void this.#ensureConnected();
|
|
80
|
+
}
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
this.#pending = true;
|
|
84
|
+
void this.#ensureConnected();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async #ensureConnected(): Promise<void> {
|
|
88
|
+
if (this.#stopped || this.#transport || this.#connecting) return;
|
|
89
|
+
this.#connecting = true;
|
|
90
|
+
let t: KeepaliveTransport | null = null;
|
|
91
|
+
try {
|
|
92
|
+
t = await this.#deps.connect(() => {
|
|
93
|
+
this.#transport = null; // socket dropped → next emit reconnects
|
|
94
|
+
});
|
|
95
|
+
} catch {
|
|
96
|
+
t = null;
|
|
97
|
+
}
|
|
98
|
+
this.#connecting = false;
|
|
99
|
+
if (!t) return; // manager unreachable — best-effort, a later emit retries
|
|
100
|
+
if (this.#stopped) {
|
|
101
|
+
t.close();
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
this.#transport = t;
|
|
105
|
+
if (this.#pending) {
|
|
106
|
+
this.#pending = false;
|
|
107
|
+
const frame = keepaliveFrame(this.#deps.sessionId());
|
|
108
|
+
if (frame) {
|
|
109
|
+
try {
|
|
110
|
+
this.#transport.write(frame);
|
|
111
|
+
} catch {
|
|
112
|
+
this.#transport = null;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
package/src/commands/manager.ts
CHANGED
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
* on sessionId, so two tabs of the SAME tenant get two workers.
|
|
12
12
|
* {"type":"release","sessionId":"tab-7"} → kill + forget that session's worker
|
|
13
13
|
* {"type":"status"} → accepted; no-op sink
|
|
14
|
+
* {"type":"status","sessionId":"tab-7"} → keepalive: refresh that worker's
|
|
15
|
+
* lastSeen so an actively-chatting session is not idle-reaped
|
|
14
16
|
*
|
|
15
17
|
* All registry/port/idempotency/idle policy is the pure `manager-core`; this file
|
|
16
18
|
* is the thin I/O shell (socket, spawn, kill, timer) around it. A background sweep
|
|
@@ -25,7 +27,15 @@ import { Command } from "@f5-sales-demo/pi-utils/cli";
|
|
|
25
27
|
import { VERSION } from "@f5-sales-demo/pi-utils/dirs";
|
|
26
28
|
import { portCandidates } from "../browser/extension-bridge";
|
|
27
29
|
import { removeManagerState, writeManagerState } from "../services/manager-state";
|
|
28
|
-
import {
|
|
30
|
+
import {
|
|
31
|
+
needsProvision,
|
|
32
|
+
parseControlMsg,
|
|
33
|
+
pickPort,
|
|
34
|
+
type Registry,
|
|
35
|
+
sparesToSpawn,
|
|
36
|
+
staleKeys,
|
|
37
|
+
touchLastSeen,
|
|
38
|
+
} from "./manager-core";
|
|
29
39
|
|
|
30
40
|
/** Reap a worker idle longer than this (ms). */
|
|
31
41
|
const IDLE_MS = 20 * 60_000;
|
|
@@ -434,8 +444,13 @@ export default class Manager extends Command {
|
|
|
434
444
|
const managerProvisionMs = Date.now() - provisionReceivedAt;
|
|
435
445
|
if (!adoptSpare(msg, managerProvisionMs)) spawnWorker(msg, managerProvisionMs); // adopt a warm spare, else cold-spawn (fallback)
|
|
436
446
|
}
|
|
437
|
-
|
|
438
|
-
|
|
447
|
+
touchLastSeen(reg, msg.sessionId, Date.now()); // touch on every provision (keep-alive)
|
|
448
|
+
} else if (msg.type === "status") {
|
|
449
|
+
// Keepalive from an actively-chatting worker (#idle-reap): refresh
|
|
450
|
+
// lastSeen so the idle sweep never reaps a session that is in use.
|
|
451
|
+
// Chat traffic never reaches the manager, so this is the only signal
|
|
452
|
+
// that an otherwise-quiet-to-the-manager worker is still working.
|
|
453
|
+
touchLastSeen(reg, msg.sessionId, Date.now());
|
|
439
454
|
} else if (msg.type === "release") {
|
|
440
455
|
reap(msg.sessionId);
|
|
441
456
|
} else if (msg.type === "shutdown") {
|
|
@@ -452,7 +467,6 @@ export default class Manager extends Command {
|
|
|
452
467
|
/* client hung up mid-handshake — ignore */
|
|
453
468
|
}
|
|
454
469
|
}
|
|
455
|
-
// "status" is validated but currently a no-op sink.
|
|
456
470
|
};
|
|
457
471
|
|
|
458
472
|
// Per-connection NDJSON buffer: a control frame can be split across two TCP
|
package/src/commands/worker.ts
CHANGED
|
@@ -13,6 +13,8 @@
|
|
|
13
13
|
* `XCSH_SESSION_TENANT` so it advertises its assigned tenant even before a context
|
|
14
14
|
* is bound. This lets the extension panel lock onto the right tenant immediately.
|
|
15
15
|
*/
|
|
16
|
+
import { homedir } from "node:os";
|
|
17
|
+
import { join } from "node:path";
|
|
16
18
|
import { getProjectDir, getXCSHConfigDir, logger } from "@f5-sales-demo/pi-utils";
|
|
17
19
|
import { Command } from "@f5-sales-demo/pi-utils/cli";
|
|
18
20
|
import { ChatHandler } from "../browser/chat-handler";
|
|
@@ -25,6 +27,7 @@ import { createAgentSession } from "../sdk";
|
|
|
25
27
|
import { activateTenantContext } from "../services/session-context-binding";
|
|
26
28
|
import { ContextService } from "../services/xcsh-context";
|
|
27
29
|
import { deriveTenantEnv } from "../services/xcsh-env";
|
|
30
|
+
import { type KeepaliveTransport, ManagerKeepalive } from "./manager-keepalive";
|
|
28
31
|
|
|
29
32
|
/** Mutable worker identity. Seeded from env at spawn (backward compat); replaced by a
|
|
30
33
|
* late IPC bind on a pre-warmed spare. `null` fields fall back to env, then the "spare"
|
|
@@ -77,6 +80,16 @@ export function sessionInfoForWorker(): {
|
|
|
77
80
|
/** Hard ceiling for draining an in-flight chat turn on SIGTERM before teardown (#1874). */
|
|
78
81
|
const WORKER_DRAIN_TIMEOUT_MS = 10_000;
|
|
79
82
|
|
|
83
|
+
/** How often the worker pings the manager while a turn is in flight, refreshing its
|
|
84
|
+
* lastSeen so an actively-chatting session is never idle-reaped. Comfortably under
|
|
85
|
+
* the manager's IDLE_MS (20 min); a turn also pings once at its start. */
|
|
86
|
+
const KEEPALIVE_MS = 60_000;
|
|
87
|
+
|
|
88
|
+
/** The manager control socket (same derivation as native-host / chrome-cli). */
|
|
89
|
+
function managerSockPath(): string {
|
|
90
|
+
return process.env.XCSH_MANAGER_SOCK ?? join(homedir(), ".xcsh", "manager.sock");
|
|
91
|
+
}
|
|
92
|
+
|
|
80
93
|
/** Browser-automation tool set — identical scoping to `main.ts`'s extension path.
|
|
81
94
|
* With scoped tools the ONLY way to create a resource is the form-driven workflow
|
|
82
95
|
* runner, which is exactly what the human watching the browser wants. */
|
|
@@ -274,6 +287,38 @@ export default class Worker extends Command {
|
|
|
274
287
|
const chatHandler = new ChatHandler(bridge, session);
|
|
275
288
|
chatHandler.attach();
|
|
276
289
|
|
|
290
|
+
// Manager keepalive: while a turn is in flight (and at each turn start), ping
|
|
291
|
+
// the manager control socket so it refreshes this worker's lastSeen and its
|
|
292
|
+
// idle sweep does not reap an actively-used session mid-conversation. Chat
|
|
293
|
+
// traffic never reaches the manager, so this is the only liveness signal.
|
|
294
|
+
// Best-effort + self-reconnecting: a dropped socket (e.g. manager supersede)
|
|
295
|
+
// is re-opened on the next emit, re-targeting the successor manager.
|
|
296
|
+
const keepalive = new ManagerKeepalive({
|
|
297
|
+
sessionId: () => sessionInfoForWorker().sessionId ?? "spare",
|
|
298
|
+
busy: () => chatHandler.busy,
|
|
299
|
+
connect: async onClose => {
|
|
300
|
+
try {
|
|
301
|
+
const sock = await Bun.connect({
|
|
302
|
+
unix: managerSockPath(),
|
|
303
|
+
socket: { data() {}, close: () => onClose(), error: () => onClose() },
|
|
304
|
+
});
|
|
305
|
+
const transport: KeepaliveTransport = {
|
|
306
|
+
write: data => {
|
|
307
|
+
sock.write(data);
|
|
308
|
+
},
|
|
309
|
+
close: () => {
|
|
310
|
+
sock.end();
|
|
311
|
+
},
|
|
312
|
+
};
|
|
313
|
+
return transport;
|
|
314
|
+
} catch {
|
|
315
|
+
return null; // manager not reachable — a later emit retries
|
|
316
|
+
}
|
|
317
|
+
},
|
|
318
|
+
});
|
|
319
|
+
chatHandler.onTurnStart(() => keepalive.turnStart());
|
|
320
|
+
const keepaliveTimer = setInterval(() => keepalive.tick(), KEEPALIVE_MS);
|
|
321
|
+
|
|
277
322
|
// session-ready. Emit the per-tab boot breakdown when requested (parity with
|
|
278
323
|
// main.ts:1002-1009). PI_TIMING=x prints then exits — used by bench/extension-session.ts
|
|
279
324
|
// to measure total worker cold-start; otherwise this is a no-op.
|
|
@@ -287,6 +332,8 @@ export default class Worker extends Command {
|
|
|
287
332
|
|
|
288
333
|
let shuttingDown = false;
|
|
289
334
|
const teardown = () => {
|
|
335
|
+
clearInterval(keepaliveTimer);
|
|
336
|
+
keepalive.stop();
|
|
290
337
|
chatHandler.dispose();
|
|
291
338
|
void bridge.close().finally(() => process.exit(0));
|
|
292
339
|
};
|
|
@@ -17,17 +17,17 @@ export interface BuildInfo {
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
export const BUILD_INFO: BuildInfo = {
|
|
20
|
-
"version": "19.61.
|
|
21
|
-
"commit": "
|
|
22
|
-
"shortCommit": "
|
|
20
|
+
"version": "19.61.5",
|
|
21
|
+
"commit": "e9f45bc1c87e84c1d99060a016ae1e20907e2210",
|
|
22
|
+
"shortCommit": "e9f45bc",
|
|
23
23
|
"branch": "main",
|
|
24
|
-
"tag": "v19.61.
|
|
25
|
-
"commitDate": "2026-07-
|
|
26
|
-
"buildDate": "2026-07-
|
|
24
|
+
"tag": "v19.61.5",
|
|
25
|
+
"commitDate": "2026-07-08T01:47:55Z",
|
|
26
|
+
"buildDate": "2026-07-08T02:09:52.692Z",
|
|
27
27
|
"dirty": true,
|
|
28
28
|
"prNumber": "",
|
|
29
29
|
"repoUrl": "https://github.com/f5-sales-demo/xcsh",
|
|
30
30
|
"repoSlug": "f5-sales-demo/xcsh",
|
|
31
|
-
"commitUrl": "https://github.com/f5-sales-demo/xcsh/commit/
|
|
32
|
-
"releaseUrl": "https://github.com/f5-sales-demo/xcsh/releases/tag/v19.61.
|
|
31
|
+
"commitUrl": "https://github.com/f5-sales-demo/xcsh/commit/e9f45bc1c87e84c1d99060a016ae1e20907e2210",
|
|
32
|
+
"releaseUrl": "https://github.com/f5-sales-demo/xcsh/releases/tag/v19.61.5"
|
|
33
33
|
};
|