@dev-anywhere/proxy 0.4.84 → 0.5.0
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/README.md +12 -3
- package/dist/{chunk-2JSCH6DW.js → chunk-5OJB4FGI.js} +11 -4
- package/dist/chunk-5OJB4FGI.js.map +1 -0
- package/dist/{chunk-RYUEUDBR.js → chunk-5WZ6B735.js} +9 -6
- package/dist/chunk-5WZ6B735.js.map +1 -0
- package/dist/{chunk-LWDOXOWY.js → chunk-CN7UT6KW.js} +3 -80
- package/dist/chunk-CN7UT6KW.js.map +1 -0
- package/dist/{chunk-B3GHP25U.js → chunk-F53HQ6GP.js} +14 -2
- package/dist/chunk-F53HQ6GP.js.map +1 -0
- package/dist/chunk-NOUZTHUN.js +86 -0
- package/dist/chunk-NOUZTHUN.js.map +1 -0
- package/dist/{chunk-6ESPTMAZ.js → chunk-OEXRT54F.js} +6 -6
- package/dist/{chunk-ENXGI3FO.js → chunk-RSJKBUBY.js} +387 -332
- package/dist/chunk-RSJKBUBY.js.map +1 -0
- package/dist/index.js +21 -6
- package/dist/index.js.map +1 -1
- package/dist/quick-tunnel-MO3U67K3.js +318 -0
- package/dist/quick-tunnel-MO3U67K3.js.map +1 -0
- package/dist/{relay-token-QD7ULGNS.js → relay-token-X7NWRMKV.js} +4 -3
- package/dist/{relay-token-QD7ULGNS.js.map → relay-token-X7NWRMKV.js.map} +1 -1
- package/dist/serve.js +428 -212
- package/dist/serve.js.map +1 -1
- package/dist/session-worker.js +2 -27
- package/dist/session-worker.js.map +1 -1
- package/dist/{terminal-EUZKQXTH.js → terminal-4EXLE5C7.js} +27 -19
- package/dist/terminal-4EXLE5C7.js.map +1 -0
- package/dist/terminal-worker.js +55 -12
- package/dist/terminal-worker.js.map +1 -1
- package/package.json +4 -4
- package/dist/chunk-2JSCH6DW.js.map +0 -1
- package/dist/chunk-B3GHP25U.js.map +0 -1
- package/dist/chunk-ENXGI3FO.js.map +0 -1
- package/dist/chunk-LWDOXOWY.js.map +0 -1
- package/dist/chunk-RYUEUDBR.js.map +0 -1
- package/dist/terminal-EUZKQXTH.js.map +0 -1
- /package/dist/{chunk-6ESPTMAZ.js.map → chunk-OEXRT54F.js.map} +0 -0
package/dist/serve.js
CHANGED
|
@@ -15,8 +15,9 @@ import {
|
|
|
15
15
|
} from "./chunk-NXELIU46.js";
|
|
16
16
|
import {
|
|
17
17
|
decidePtySemanticTransition,
|
|
18
|
-
shouldReleaseTextApprovalOnInput
|
|
19
|
-
|
|
18
|
+
shouldReleaseTextApprovalOnInput,
|
|
19
|
+
shouldStartPtyTurnOnInput
|
|
20
|
+
} from "./chunk-5WZ6B735.js";
|
|
20
21
|
import {
|
|
21
22
|
CLAUDE_PROVIDER,
|
|
22
23
|
CODEX_PROVIDER,
|
|
@@ -26,17 +27,15 @@ import {
|
|
|
26
27
|
appendPtySemanticTextTail,
|
|
27
28
|
extractOscSequences,
|
|
28
29
|
extractOscSignals,
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
} from "./chunk-
|
|
32
|
-
import {
|
|
33
|
-
spawnScript
|
|
34
|
-
} from "./chunk-IHUQNJF6.js";
|
|
30
|
+
extractOscWorkingDirectory,
|
|
31
|
+
extractTextSignals
|
|
32
|
+
} from "./chunk-F53HQ6GP.js";
|
|
35
33
|
import {
|
|
36
34
|
ControlErrorCode,
|
|
37
35
|
MessageEnvelopeSchema,
|
|
38
36
|
RELAY_JSON_MESSAGE_MAX_BYTES,
|
|
39
37
|
RelayControlSchema,
|
|
38
|
+
SESSION_CREATE_SERVER_DEADLINE_MS,
|
|
40
39
|
SessionState,
|
|
41
40
|
buildMessage,
|
|
42
41
|
createFSM,
|
|
@@ -51,19 +50,18 @@ import {
|
|
|
51
50
|
serializeControl,
|
|
52
51
|
serializeIpc,
|
|
53
52
|
serializeWorkerMsg
|
|
54
|
-
} from "./chunk-
|
|
53
|
+
} from "./chunk-RSJKBUBY.js";
|
|
55
54
|
import {
|
|
56
55
|
buildProviderEnv,
|
|
57
56
|
loadConfig,
|
|
58
57
|
saveAgentCliPath
|
|
59
|
-
} from "./chunk-
|
|
58
|
+
} from "./chunk-5OJB4FGI.js";
|
|
60
59
|
import {
|
|
61
60
|
atomicWriteFileSync
|
|
62
61
|
} from "./chunk-NQDJ6QAM.js";
|
|
63
62
|
import {
|
|
64
|
-
flushLogger,
|
|
65
63
|
serviceLogger
|
|
66
|
-
} from "./chunk-
|
|
64
|
+
} from "./chunk-NOUZTHUN.js";
|
|
67
65
|
import {
|
|
68
66
|
DATA_DIR,
|
|
69
67
|
DEFAULT_PROXY_PROFILE,
|
|
@@ -79,6 +77,12 @@ import {
|
|
|
79
77
|
sessionPaths,
|
|
80
78
|
tildify
|
|
81
79
|
} from "./chunk-7RSBFN2G.js";
|
|
80
|
+
import {
|
|
81
|
+
spawnScript
|
|
82
|
+
} from "./chunk-IHUQNJF6.js";
|
|
83
|
+
import {
|
|
84
|
+
flushLogger
|
|
85
|
+
} from "./chunk-CN7UT6KW.js";
|
|
82
86
|
|
|
83
87
|
// src/serve.ts
|
|
84
88
|
import { createServer as createServer2 } from "net";
|
|
@@ -86,6 +90,7 @@ import { unlinkSync as unlinkSync2, writeFileSync as writeFileSync2, chmodSync,
|
|
|
86
90
|
|
|
87
91
|
// src/serve/session-manager.ts
|
|
88
92
|
import { readFileSync as readFileSync2, existsSync as existsSync2 } from "fs";
|
|
93
|
+
import { isAbsolute, normalize } from "path";
|
|
89
94
|
import { nanoid } from "nanoid";
|
|
90
95
|
|
|
91
96
|
// src/serve/session-history-metadata.ts
|
|
@@ -395,6 +400,21 @@ var SessionManager = class {
|
|
|
395
400
|
session.pid = pid;
|
|
396
401
|
this.save();
|
|
397
402
|
}
|
|
403
|
+
updateTerminalCwd(id, cwd) {
|
|
404
|
+
const session = this.sessions.get(id);
|
|
405
|
+
if (!session || session.mode !== "pty" || session.kind !== "terminal") return false;
|
|
406
|
+
const nextCwd = normalize(cwd);
|
|
407
|
+
if (!isAbsolute(nextCwd) || nextCwd === session.cwd) return false;
|
|
408
|
+
const previousCwd = session.cwd;
|
|
409
|
+
session.cwd = nextCwd;
|
|
410
|
+
session.updatedAt = Date.now();
|
|
411
|
+
this.save();
|
|
412
|
+
serviceLogger.info(
|
|
413
|
+
{ sessionId: id, previousCwd, cwd: nextCwd },
|
|
414
|
+
"Terminal working directory changed"
|
|
415
|
+
);
|
|
416
|
+
return true;
|
|
417
|
+
}
|
|
398
418
|
renameSession(id, name) {
|
|
399
419
|
const session = this.sessions.get(id);
|
|
400
420
|
if (!session) {
|
|
@@ -909,12 +929,12 @@ var RelayConnection = class extends EventEmitter {
|
|
|
909
929
|
|
|
910
930
|
// src/serve/handlers/control-messages.ts
|
|
911
931
|
import { readdir as readdir2, mkdir } from "fs/promises";
|
|
912
|
-
import { join as join4, isAbsolute as
|
|
932
|
+
import { join as join4, isAbsolute as isAbsolute3, normalize as normalize2 } from "path";
|
|
913
933
|
|
|
914
934
|
// src/serve/session-history.ts
|
|
915
935
|
import { readdir, stat, access, open } from "fs/promises";
|
|
916
936
|
import { createReadStream } from "fs";
|
|
917
|
-
import { isAbsolute, join as join2, resolve, sep } from "path";
|
|
937
|
+
import { isAbsolute as isAbsolute2, join as join2, resolve, sep } from "path";
|
|
918
938
|
import { homedir as homedir2, tmpdir } from "os";
|
|
919
939
|
import { createInterface } from "readline";
|
|
920
940
|
var claudeProjectsDir = () => join2(homedir2(), ".claude", "projects");
|
|
@@ -962,7 +982,7 @@ async function scanSessionHistory(options = {}) {
|
|
|
962
982
|
}
|
|
963
983
|
function normalizeAbsolutePath(path) {
|
|
964
984
|
const trimmed = path.trim();
|
|
965
|
-
if (!trimmed || !
|
|
985
|
+
if (!trimmed || !isAbsolute2(trimmed)) return null;
|
|
966
986
|
let resolved = resolve(trimmed);
|
|
967
987
|
while (resolved.length > 1 && resolved.endsWith(sep)) {
|
|
968
988
|
resolved = resolved.slice(0, -1);
|
|
@@ -1609,8 +1629,8 @@ function classifyPathError(err) {
|
|
|
1609
1629
|
// src/serve/handlers/control-messages.ts
|
|
1610
1630
|
var COMMAND_REFRESH_MS = 6 * 60 * 60 * 1e3;
|
|
1611
1631
|
function isPathSafe(path) {
|
|
1612
|
-
if (!
|
|
1613
|
-
const normalized =
|
|
1632
|
+
if (!isAbsolute3(path)) return false;
|
|
1633
|
+
const normalized = normalize2(path);
|
|
1614
1634
|
if (normalized.includes("..")) return false;
|
|
1615
1635
|
return true;
|
|
1616
1636
|
}
|
|
@@ -2292,16 +2312,37 @@ var WorkerRegistry = class {
|
|
|
2292
2312
|
});
|
|
2293
2313
|
const workerPid = child.pid;
|
|
2294
2314
|
this.children.set(sessionId, child);
|
|
2315
|
+
child.once("exit", (code, signal) => {
|
|
2316
|
+
const activeSession = this.deps.sessionManager.getSession(sessionId);
|
|
2317
|
+
this.delete(sessionId);
|
|
2318
|
+
if (!activeSession) return;
|
|
2319
|
+
const result = this.deps.sessionManager.terminateSession(sessionId);
|
|
2320
|
+
serviceLogger.warn(
|
|
2321
|
+
{ sessionId, workerPid, code, signal, removed: result.success },
|
|
2322
|
+
"JSON worker process exited without worker_exit IPC"
|
|
2323
|
+
);
|
|
2324
|
+
});
|
|
2295
2325
|
serviceLogger.info(
|
|
2296
2326
|
{ sessionId, workerPid, cwd: options?.cwd, resume: options?.resumeSessionId },
|
|
2297
2327
|
"Worker process spawned"
|
|
2298
2328
|
);
|
|
2299
2329
|
return workerPid;
|
|
2300
2330
|
}
|
|
2301
|
-
connect(sessionId, sockPath) {
|
|
2331
|
+
connect(sessionId, sockPath, timeoutMs) {
|
|
2302
2332
|
return new Promise((resolve3) => {
|
|
2303
2333
|
const sock = connect(sockPath);
|
|
2304
|
-
|
|
2334
|
+
let settled = false;
|
|
2335
|
+
let timeout;
|
|
2336
|
+
const finish = (result) => {
|
|
2337
|
+
if (settled) return;
|
|
2338
|
+
settled = true;
|
|
2339
|
+
if (timeout) clearTimeout(timeout);
|
|
2340
|
+
resolve3(result);
|
|
2341
|
+
};
|
|
2342
|
+
const onConnectError = () => finish(null);
|
|
2343
|
+
sock.once("error", onConnectError);
|
|
2344
|
+
sock.once("connect", () => {
|
|
2345
|
+
sock.off("error", onConnectError);
|
|
2305
2346
|
this.sockets.set(sessionId, sock);
|
|
2306
2347
|
createWorkerReader(
|
|
2307
2348
|
sock,
|
|
@@ -2315,9 +2356,18 @@ var WorkerRegistry = class {
|
|
|
2315
2356
|
);
|
|
2316
2357
|
sock.on("close", () => this.onDisconnect(sessionId));
|
|
2317
2358
|
sock.on("error", () => this.onDisconnect(sessionId));
|
|
2318
|
-
|
|
2359
|
+
finish(sock);
|
|
2319
2360
|
});
|
|
2320
|
-
|
|
2361
|
+
if (timeoutMs !== void 0) {
|
|
2362
|
+
timeout = setTimeout(
|
|
2363
|
+
() => {
|
|
2364
|
+
sock.destroy();
|
|
2365
|
+
finish(null);
|
|
2366
|
+
},
|
|
2367
|
+
Math.max(1, timeoutMs)
|
|
2368
|
+
);
|
|
2369
|
+
timeout.unref?.();
|
|
2370
|
+
}
|
|
2321
2371
|
});
|
|
2322
2372
|
}
|
|
2323
2373
|
// 枚举 DATA_DIR 下所有 session 目录,尝试连接存活的 worker.sock;失败则清理 stale socket。
|
|
@@ -2352,6 +2402,9 @@ var WorkerRegistry = class {
|
|
|
2352
2402
|
has(sessionId) {
|
|
2353
2403
|
return this.sockets.has(sessionId);
|
|
2354
2404
|
}
|
|
2405
|
+
hasProcess(sessionId) {
|
|
2406
|
+
return this.children.has(sessionId);
|
|
2407
|
+
}
|
|
2355
2408
|
delete(sessionId) {
|
|
2356
2409
|
this.children.delete(sessionId);
|
|
2357
2410
|
this.sockets.delete(sessionId);
|
|
@@ -2684,9 +2737,6 @@ function terminateSessionByOwnership(deps, sessionId) {
|
|
|
2684
2737
|
}
|
|
2685
2738
|
deps.terminalSockets.delete(sessionId);
|
|
2686
2739
|
const result = deps.sessionManager.terminateSession(sessionId);
|
|
2687
|
-
deps.controlHandlers.cleanup(sessionId);
|
|
2688
|
-
deps.agentStatusRegistry.delete(sessionId);
|
|
2689
|
-
deps.broadcastSessionList();
|
|
2690
2740
|
serviceLogger.info(
|
|
2691
2741
|
{ sessionId, success: result.success },
|
|
2692
2742
|
"Terminal worker session terminated"
|
|
@@ -2702,9 +2752,6 @@ function terminateSessionByOwnership(deps, sessionId) {
|
|
|
2702
2752
|
const result = deps.sessionManager.terminateSession(sessionId, {
|
|
2703
2753
|
preserveProviderHooks: true
|
|
2704
2754
|
});
|
|
2705
|
-
deps.controlHandlers.cleanup(sessionId);
|
|
2706
|
-
deps.agentStatusRegistry.delete(sessionId);
|
|
2707
|
-
deps.broadcastSessionList();
|
|
2708
2755
|
serviceLogger.info(
|
|
2709
2756
|
{ sessionId, success: result.success },
|
|
2710
2757
|
"Local terminal session detached from remote view"
|
|
@@ -2717,13 +2764,17 @@ function terminateSessionByOwnership(deps, sessionId) {
|
|
|
2717
2764
|
return { success, action: "terminate_hosted_pty" };
|
|
2718
2765
|
}
|
|
2719
2766
|
if (session?.mode === "json") {
|
|
2720
|
-
deps.workerRegistry.send(sessionId, { type: "worker_stop" });
|
|
2721
|
-
|
|
2767
|
+
const stopDelivered = deps.workerRegistry.send(sessionId, { type: "worker_stop" });
|
|
2768
|
+
if (stopDelivered) {
|
|
2769
|
+
deps.workerRegistry.delete(sessionId);
|
|
2770
|
+
} else {
|
|
2771
|
+
deps.workerRegistry.terminateProcess(sessionId);
|
|
2772
|
+
}
|
|
2722
2773
|
const result = deps.sessionManager.terminateSession(sessionId);
|
|
2723
|
-
|
|
2724
|
-
|
|
2725
|
-
|
|
2726
|
-
|
|
2774
|
+
serviceLogger.info(
|
|
2775
|
+
{ sessionId, success: result.success, stopDelivered },
|
|
2776
|
+
"JSON worker session terminated"
|
|
2777
|
+
);
|
|
2727
2778
|
return { success: result.success, action: "terminate_json_worker" };
|
|
2728
2779
|
}
|
|
2729
2780
|
const hostedTerminated = deps.hostedPtyRegistry.terminate(sessionId);
|
|
@@ -3204,7 +3255,7 @@ var RelayResourceHandlers = class {
|
|
|
3204
3255
|
// src/serve/relay-session-create-handler.ts
|
|
3205
3256
|
import { rmSync, statSync as statSync2 } from "fs";
|
|
3206
3257
|
import { homedir as homedir5 } from "os";
|
|
3207
|
-
import { isAbsolute as
|
|
3258
|
+
import { isAbsolute as isAbsolute4 } from "path";
|
|
3208
3259
|
import { nanoid as nanoid3 } from "nanoid";
|
|
3209
3260
|
|
|
3210
3261
|
// src/serve/hosted-pty-registry.ts
|
|
@@ -3352,7 +3403,7 @@ var HostedPtyRegistry = class {
|
|
|
3352
3403
|
const hosted = this.sessions.get(sessionId);
|
|
3353
3404
|
if (!hosted) return false;
|
|
3354
3405
|
if (hosted.kind === "agent") {
|
|
3355
|
-
this.
|
|
3406
|
+
this.updateSemanticStateOnInput(sessionId, hosted, data);
|
|
3356
3407
|
}
|
|
3357
3408
|
hosted.child.write(data);
|
|
3358
3409
|
serviceLogger.debug(
|
|
@@ -3396,6 +3447,9 @@ var HostedPtyRegistry = class {
|
|
|
3396
3447
|
terminate(sessionId) {
|
|
3397
3448
|
return this.close(sessionId, { kill: true, notify: true });
|
|
3398
3449
|
}
|
|
3450
|
+
abortStartup(sessionId) {
|
|
3451
|
+
return this.close(sessionId, { kill: true, notify: false });
|
|
3452
|
+
}
|
|
3399
3453
|
destroyAll() {
|
|
3400
3454
|
for (const sessionId of Array.from(this.sessions.keys())) {
|
|
3401
3455
|
this.close(sessionId, { kill: true, notify: false });
|
|
@@ -3411,14 +3465,16 @@ var HostedPtyRegistry = class {
|
|
|
3411
3465
|
this.deps.touchSessionActivity(sessionId);
|
|
3412
3466
|
this.sendBinary(sessionId, Buffer.from(data, "utf-8"), hosted.outputSeq);
|
|
3413
3467
|
const oscSequences = extractOscSequences(data);
|
|
3468
|
+
const cwd = extractOscWorkingDirectory(data);
|
|
3414
3469
|
const session = this.deps.sessionManager.getSession(sessionId);
|
|
3415
3470
|
const oscSignal = extractOscSignals(data, session?.provider);
|
|
3416
3471
|
if (oscSignal?.title) {
|
|
3417
3472
|
this.sendTerminalTitle(sessionId, oscSignal.title);
|
|
3418
3473
|
}
|
|
3474
|
+
if (hosted.kind === "terminal" && cwd) {
|
|
3475
|
+
this.deps.updateTerminalCwd(sessionId, cwd);
|
|
3476
|
+
}
|
|
3419
3477
|
if (hosted.kind === "terminal") return;
|
|
3420
|
-
this.deps.applyPtyStateToSession(sessionId, "working");
|
|
3421
|
-
const hasTextActivity = normalizePtySemanticText(data).length > 0;
|
|
3422
3478
|
hosted.semanticTextTail = appendPtySemanticTextTail(hosted.semanticTextTail, data);
|
|
3423
3479
|
const textSignal = oscSignal ? null : extractTextSignals(hosted.semanticTextTail, session?.provider);
|
|
3424
3480
|
const signal = oscSignal ?? textSignal;
|
|
@@ -3443,8 +3499,7 @@ var HostedPtyRegistry = class {
|
|
|
3443
3499
|
currentState: hosted.currentState,
|
|
3444
3500
|
signal: signal ?? null,
|
|
3445
3501
|
sessionStateIsWaitingApproval: session?.state === SessionState.WAITING_APPROVAL,
|
|
3446
|
-
allowTitleOnlyApprovalRelease: !hosted.textApprovalWaitActive
|
|
3447
|
-
hasTextActivity
|
|
3502
|
+
allowTitleOnlyApprovalRelease: !hosted.textApprovalWaitActive
|
|
3448
3503
|
});
|
|
3449
3504
|
hosted.currentState = decision.nextState;
|
|
3450
3505
|
if (decision.nextState !== "approval_wait") {
|
|
@@ -3454,10 +3509,15 @@ var HostedPtyRegistry = class {
|
|
|
3454
3509
|
this.sendPtyState(sessionId, decision.nextState, decision.meta, hosted);
|
|
3455
3510
|
this.deps.applyPtyStateToSession(sessionId, decision.nextState);
|
|
3456
3511
|
}
|
|
3457
|
-
|
|
3458
|
-
if (
|
|
3459
|
-
|
|
3460
|
-
|
|
3512
|
+
updateSemanticStateOnInput(sessionId, hosted, data) {
|
|
3513
|
+
if (hosted.textApprovalWaitActive && hosted.currentState === "approval_wait" && shouldReleaseTextApprovalOnInput(data)) {
|
|
3514
|
+
hosted.textApprovalWaitActive = false;
|
|
3515
|
+
hosted.currentState = "working";
|
|
3516
|
+
this.sendPtyState(sessionId, "working", void 0, hosted);
|
|
3517
|
+
this.deps.applyPtyStateToSession(sessionId, "working");
|
|
3518
|
+
return;
|
|
3519
|
+
}
|
|
3520
|
+
if (!shouldStartPtyTurnOnInput(hosted.currentState, data)) return;
|
|
3461
3521
|
hosted.currentState = "working";
|
|
3462
3522
|
this.sendPtyState(sessionId, "working", void 0, hosted);
|
|
3463
3523
|
this.deps.applyPtyStateToSession(sessionId, "working");
|
|
@@ -3523,21 +3583,102 @@ var HostedPtyRegistry = class {
|
|
|
3523
3583
|
if (options.notify) {
|
|
3524
3584
|
this.sendPtyState(sessionId, "turn_complete", void 0, hosted);
|
|
3525
3585
|
this.deps.sessionManager.terminateSession(sessionId);
|
|
3526
|
-
this.deps.onSessionClosed(sessionId);
|
|
3527
3586
|
}
|
|
3528
3587
|
this.sessions.delete(sessionId);
|
|
3529
3588
|
return true;
|
|
3530
3589
|
}
|
|
3531
3590
|
};
|
|
3532
3591
|
|
|
3592
|
+
// src/serve/json-worker-startup.ts
|
|
3593
|
+
var CONNECT_RETRY_BASE_MS = 100;
|
|
3594
|
+
var CONNECT_RETRY_MAX_MS = 1e3;
|
|
3595
|
+
var JsonWorkerStartupTimeoutError = class extends Error {
|
|
3596
|
+
constructor() {
|
|
3597
|
+
super("JSON worker startup deadline reached");
|
|
3598
|
+
this.name = "JsonWorkerStartupTimeoutError";
|
|
3599
|
+
}
|
|
3600
|
+
};
|
|
3601
|
+
function throwIfAborted(signal) {
|
|
3602
|
+
if (signal.aborted) {
|
|
3603
|
+
throw signal.reason instanceof Error ? signal.reason : new Error("JSON worker startup aborted");
|
|
3604
|
+
}
|
|
3605
|
+
}
|
|
3606
|
+
function wait(delayMs, signal) {
|
|
3607
|
+
return new Promise((resolve3, reject) => {
|
|
3608
|
+
throwIfAborted(signal);
|
|
3609
|
+
const onAbort = () => {
|
|
3610
|
+
clearTimeout(timer);
|
|
3611
|
+
reject(signal.reason instanceof Error ? signal.reason : new Error("Startup aborted"));
|
|
3612
|
+
};
|
|
3613
|
+
const timer = setTimeout(() => {
|
|
3614
|
+
signal.removeEventListener("abort", onAbort);
|
|
3615
|
+
resolve3();
|
|
3616
|
+
}, delayMs);
|
|
3617
|
+
timer.unref?.();
|
|
3618
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
3619
|
+
});
|
|
3620
|
+
}
|
|
3621
|
+
async function waitForJsonWorkerStartup(options) {
|
|
3622
|
+
let attempt = 0;
|
|
3623
|
+
while (true) {
|
|
3624
|
+
throwIfAborted(options.signal);
|
|
3625
|
+
const remainingBeforeDelay = options.deadlineAt - Date.now();
|
|
3626
|
+
if (remainingBeforeDelay <= 0) throw new JsonWorkerStartupTimeoutError();
|
|
3627
|
+
const delayMs = Math.min(
|
|
3628
|
+
CONNECT_RETRY_BASE_MS * Math.max(1, attempt),
|
|
3629
|
+
CONNECT_RETRY_MAX_MS,
|
|
3630
|
+
remainingBeforeDelay
|
|
3631
|
+
);
|
|
3632
|
+
await wait(delayMs, options.signal);
|
|
3633
|
+
throwIfAborted(options.signal);
|
|
3634
|
+
const connectBudgetMs = options.deadlineAt - Date.now();
|
|
3635
|
+
if (connectBudgetMs <= 0) throw new JsonWorkerStartupTimeoutError();
|
|
3636
|
+
attempt += 1;
|
|
3637
|
+
const socket = await options.workerRegistry.connect(
|
|
3638
|
+
options.sessionId,
|
|
3639
|
+
options.socketPath,
|
|
3640
|
+
connectBudgetMs
|
|
3641
|
+
);
|
|
3642
|
+
if (options.signal.aborted) {
|
|
3643
|
+
socket?.destroy();
|
|
3644
|
+
options.workerRegistry.delete(options.sessionId);
|
|
3645
|
+
throwIfAborted(options.signal);
|
|
3646
|
+
}
|
|
3647
|
+
if (!socket) {
|
|
3648
|
+
if (!options.workerRegistry.hasProcess(options.sessionId)) {
|
|
3649
|
+
throw new Error("JSON worker exited before opening its control socket");
|
|
3650
|
+
}
|
|
3651
|
+
continue;
|
|
3652
|
+
}
|
|
3653
|
+
if (!options.workerRegistry.hasProcess(options.sessionId)) {
|
|
3654
|
+
socket.destroy();
|
|
3655
|
+
throw new Error("JSON worker exited before reporting ready");
|
|
3656
|
+
}
|
|
3657
|
+
const readyBudgetMs = options.deadlineAt - Date.now();
|
|
3658
|
+
if (readyBudgetMs <= 0) throw new JsonWorkerStartupTimeoutError();
|
|
3659
|
+
try {
|
|
3660
|
+
await options.workerRegistry.waitForReady(options.sessionId, readyBudgetMs);
|
|
3661
|
+
} catch (err) {
|
|
3662
|
+
if (Date.now() >= options.deadlineAt) throw new JsonWorkerStartupTimeoutError();
|
|
3663
|
+
throw err;
|
|
3664
|
+
}
|
|
3665
|
+
throwIfAborted(options.signal);
|
|
3666
|
+
if (!options.workerRegistry.has(options.sessionId)) {
|
|
3667
|
+
throw new Error("JSON worker disconnected after reporting ready");
|
|
3668
|
+
}
|
|
3669
|
+
return;
|
|
3670
|
+
}
|
|
3671
|
+
}
|
|
3672
|
+
|
|
3533
3673
|
// src/serve/relay-session-create-handler.ts
|
|
3534
|
-
var
|
|
3674
|
+
var WORKER_START_FAILED_MESSAGE = "Agent \u8FDB\u7A0B\u542F\u52A8\u5931\u8D25\uFF0C\u8BF7\u68C0\u67E5 Agent CLI \u914D\u7F6E\u540E\u91CD\u8BD5";
|
|
3675
|
+
var WORKER_START_TIMEOUT_MESSAGE = "Agent \u542F\u52A8\u8D85\u65F6\uFF0C\u8BF7\u68C0\u67E5 Agent CLI \u914D\u7F6E\u4E0E\u5F00\u53D1\u673A\u8D1F\u8F7D\u540E\u91CD\u8BD5";
|
|
3535
3676
|
function validateSessionCwd(cwd) {
|
|
3536
3677
|
if (typeof cwd !== "string" || !cwd.trim()) {
|
|
3537
3678
|
return { message: "\u8BF7\u8F93\u5165\u5DE5\u4F5C\u76EE\u5F55", code: ControlErrorCode.INVALID_PATH };
|
|
3538
3679
|
}
|
|
3539
3680
|
const trimmed = cwd.trim();
|
|
3540
|
-
if (!
|
|
3681
|
+
if (!isAbsolute4(trimmed)) {
|
|
3541
3682
|
return { message: "\u5DE5\u4F5C\u76EE\u5F55\u5FC5\u987B\u662F\u7EDD\u5BF9\u8DEF\u5F84", code: ControlErrorCode.INVALID_PATH };
|
|
3542
3683
|
}
|
|
3543
3684
|
try {
|
|
@@ -3569,16 +3710,15 @@ var RelaySessionCreateHandler = class {
|
|
|
3569
3710
|
this.deps = deps;
|
|
3570
3711
|
}
|
|
3571
3712
|
deps;
|
|
3572
|
-
//
|
|
3573
|
-
//
|
|
3574
|
-
|
|
3575
|
-
pendingTimers = /* @__PURE__ */ new Map();
|
|
3713
|
+
// spawn/connect/ready 共享一个绝对截止时间。pending 在 publish 前不进入
|
|
3714
|
+
// SessionManager,所以失败或 shutdown 都可以作为一个未发布事务统一回收。
|
|
3715
|
+
pendingJsonCreates = /* @__PURE__ */ new Map();
|
|
3576
3716
|
destroy() {
|
|
3577
|
-
for (const [pendingId,
|
|
3578
|
-
|
|
3717
|
+
for (const [pendingId, pending] of this.pendingJsonCreates) {
|
|
3718
|
+
this.pendingJsonCreates.delete(pendingId);
|
|
3719
|
+
pending.controller.abort(new Error("Relay session create handler destroyed"));
|
|
3579
3720
|
this.cleanupPendingJsonSession(pendingId);
|
|
3580
3721
|
}
|
|
3581
|
-
this.pendingTimers.clear();
|
|
3582
3722
|
}
|
|
3583
3723
|
onSessionCreate(msg) {
|
|
3584
3724
|
if (msg.kind === "terminal") {
|
|
@@ -3625,97 +3765,123 @@ var RelaySessionCreateHandler = class {
|
|
|
3625
3765
|
const name = requestedName ?? tildify(sessionCwd);
|
|
3626
3766
|
const nameLocked = requestedName !== void 0;
|
|
3627
3767
|
const pendingId = nanoid3();
|
|
3628
|
-
const
|
|
3629
|
-
|
|
3630
|
-
|
|
3631
|
-
|
|
3632
|
-
|
|
3633
|
-
|
|
3634
|
-
|
|
3635
|
-
|
|
3636
|
-
|
|
3637
|
-
|
|
3638
|
-
|
|
3639
|
-
const maxRetries = 20;
|
|
3640
|
-
const scheduleAttempt = (delayMs) => {
|
|
3641
|
-
const timer = setTimeout(tryConnect, delayMs);
|
|
3642
|
-
this.pendingTimers.set(pendingId, timer);
|
|
3643
|
-
};
|
|
3644
|
-
const tryConnect = () => {
|
|
3645
|
-
this.pendingTimers.delete(pendingId);
|
|
3646
|
-
attempt++;
|
|
3647
|
-
this.deps.workerRegistry.connect(pendingId, paths.workerSock).then((sock) => {
|
|
3648
|
-
if (sock) {
|
|
3649
|
-
this.deps.workerRegistry.waitForReady(pendingId, JSON_WORKER_READY_TIMEOUT_MS).then(() => {
|
|
3650
|
-
const session = this.deps.sessionManager.createSession(
|
|
3651
|
-
"json",
|
|
3652
|
-
sessionCwd,
|
|
3653
|
-
workerPid,
|
|
3654
|
-
name,
|
|
3655
|
-
pendingId,
|
|
3656
|
-
provider,
|
|
3657
|
-
void 0,
|
|
3658
|
-
nameLocked
|
|
3659
|
-
);
|
|
3660
|
-
if (resumeSessionId) {
|
|
3661
|
-
this.deps.sessionManager.setHistorySessionId(session.id, resumeSessionId);
|
|
3662
|
-
}
|
|
3663
|
-
this.deps.relaySend(
|
|
3664
|
-
serializeControl({
|
|
3665
|
-
type: "session_create_response",
|
|
3666
|
-
requestId,
|
|
3667
|
-
sessionId: session.id,
|
|
3668
|
-
name: session.name,
|
|
3669
|
-
nameLocked: session.nameLocked,
|
|
3670
|
-
mode: "json",
|
|
3671
|
-
provider
|
|
3672
|
-
})
|
|
3673
|
-
);
|
|
3674
|
-
if (resumeSessionId) {
|
|
3675
|
-
this.pushHistoryMessages(session.id, resumeSessionId);
|
|
3676
|
-
}
|
|
3677
|
-
serviceLogger.info(
|
|
3678
|
-
{ sessionId: session.id, cwd: sessionCwd },
|
|
3679
|
-
"JSON session created via relay"
|
|
3680
|
-
);
|
|
3681
|
-
this.deps.controlHandlers.pushCommandList(session.id, sessionCwd);
|
|
3682
|
-
this.deps.broadcastSessionSync(session);
|
|
3683
|
-
this.deps.broadcastSessionList();
|
|
3684
|
-
}).catch((err) => {
|
|
3685
|
-
const error = err instanceof Error ? err.message : String(err);
|
|
3686
|
-
this.cleanupPendingJsonSession(pendingId);
|
|
3687
|
-
this.deps.relaySend(
|
|
3688
|
-
serializeControl({
|
|
3689
|
-
type: "session_create_response",
|
|
3690
|
-
requestId,
|
|
3691
|
-
sessionId: pendingId,
|
|
3692
|
-
errorCode: ControlErrorCode.WORKER_START_FAILED,
|
|
3693
|
-
error
|
|
3694
|
-
})
|
|
3695
|
-
);
|
|
3696
|
-
serviceLogger.error(
|
|
3697
|
-
{ sessionId: pendingId, error },
|
|
3698
|
-
"Worker failed to report ready via relay"
|
|
3699
|
-
);
|
|
3700
|
-
});
|
|
3701
|
-
} else if (attempt < maxRetries) {
|
|
3702
|
-
scheduleAttempt(Math.min(100 * attempt, 2e3));
|
|
3703
|
-
} else {
|
|
3704
|
-
this.cleanupPendingJsonSession(pendingId);
|
|
3705
|
-
this.deps.relaySend(
|
|
3706
|
-
serializeControl({
|
|
3707
|
-
type: "session_create_response",
|
|
3708
|
-
requestId,
|
|
3709
|
-
sessionId: pendingId,
|
|
3710
|
-
errorCode: ControlErrorCode.WORKER_START_FAILED,
|
|
3711
|
-
error: "Worker failed to start"
|
|
3712
|
-
})
|
|
3713
|
-
);
|
|
3714
|
-
serviceLogger.error({ sessionId: pendingId }, "Worker connection timeout via relay");
|
|
3715
|
-
}
|
|
3768
|
+
const deadlineAt = Date.now() + SESSION_CREATE_SERVER_DEADLINE_MS;
|
|
3769
|
+
let workerPid;
|
|
3770
|
+
try {
|
|
3771
|
+
const hook = this.deps.createHookContext(pendingId, provider);
|
|
3772
|
+
workerPid = this.deps.workerRegistry.spawn(pendingId, {
|
|
3773
|
+
cwd: sessionCwd,
|
|
3774
|
+
resumeSessionId,
|
|
3775
|
+
permissionMode,
|
|
3776
|
+
provider,
|
|
3777
|
+
streamDelta,
|
|
3778
|
+
hook
|
|
3716
3779
|
});
|
|
3717
|
-
}
|
|
3718
|
-
|
|
3780
|
+
} catch (err) {
|
|
3781
|
+
this.reportJsonStartupFailure(requestId, pendingId, WORKER_START_FAILED_MESSAGE, err);
|
|
3782
|
+
return;
|
|
3783
|
+
}
|
|
3784
|
+
const controller = new AbortController();
|
|
3785
|
+
this.pendingJsonCreates.set(pendingId, { controller });
|
|
3786
|
+
void waitForJsonWorkerStartup({
|
|
3787
|
+
workerRegistry: this.deps.workerRegistry,
|
|
3788
|
+
sessionId: pendingId,
|
|
3789
|
+
socketPath: sessionPaths(pendingId).workerSock,
|
|
3790
|
+
deadlineAt,
|
|
3791
|
+
signal: controller.signal
|
|
3792
|
+
}).then(
|
|
3793
|
+
() => this.publishJsonSession({
|
|
3794
|
+
requestId,
|
|
3795
|
+
pendingId,
|
|
3796
|
+
workerPid,
|
|
3797
|
+
sessionCwd,
|
|
3798
|
+
name,
|
|
3799
|
+
nameLocked,
|
|
3800
|
+
provider,
|
|
3801
|
+
resumeSessionId
|
|
3802
|
+
})
|
|
3803
|
+
).catch((err) => {
|
|
3804
|
+
if (!this.takePendingJsonCreate(pendingId)) return;
|
|
3805
|
+
this.reportJsonStartupFailure(
|
|
3806
|
+
requestId,
|
|
3807
|
+
pendingId,
|
|
3808
|
+
err instanceof JsonWorkerStartupTimeoutError ? WORKER_START_TIMEOUT_MESSAGE : WORKER_START_FAILED_MESSAGE,
|
|
3809
|
+
err
|
|
3810
|
+
);
|
|
3811
|
+
});
|
|
3812
|
+
}
|
|
3813
|
+
takePendingJsonCreate(sessionId) {
|
|
3814
|
+
if (!this.pendingJsonCreates.has(sessionId)) return false;
|
|
3815
|
+
this.pendingJsonCreates.delete(sessionId);
|
|
3816
|
+
return true;
|
|
3817
|
+
}
|
|
3818
|
+
publishJsonSession(options) {
|
|
3819
|
+
if (!this.takePendingJsonCreate(options.pendingId)) return;
|
|
3820
|
+
let session;
|
|
3821
|
+
try {
|
|
3822
|
+
session = this.deps.sessionManager.createSession(
|
|
3823
|
+
"json",
|
|
3824
|
+
options.sessionCwd,
|
|
3825
|
+
options.workerPid,
|
|
3826
|
+
options.name,
|
|
3827
|
+
options.pendingId,
|
|
3828
|
+
options.provider,
|
|
3829
|
+
void 0,
|
|
3830
|
+
options.nameLocked
|
|
3831
|
+
);
|
|
3832
|
+
} catch (err) {
|
|
3833
|
+
this.reportJsonStartupFailure(
|
|
3834
|
+
options.requestId,
|
|
3835
|
+
options.pendingId,
|
|
3836
|
+
WORKER_START_FAILED_MESSAGE,
|
|
3837
|
+
err
|
|
3838
|
+
);
|
|
3839
|
+
return;
|
|
3840
|
+
}
|
|
3841
|
+
if (options.resumeSessionId) {
|
|
3842
|
+
this.deps.sessionManager.setHistorySessionId(session.id, options.resumeSessionId);
|
|
3843
|
+
}
|
|
3844
|
+
this.deps.relaySend(
|
|
3845
|
+
serializeControl({
|
|
3846
|
+
type: "session_create_response",
|
|
3847
|
+
requestId: options.requestId,
|
|
3848
|
+
sessionId: session.id,
|
|
3849
|
+
name: session.name,
|
|
3850
|
+
nameLocked: session.nameLocked,
|
|
3851
|
+
mode: "json",
|
|
3852
|
+
provider: options.provider
|
|
3853
|
+
})
|
|
3854
|
+
);
|
|
3855
|
+
if (options.resumeSessionId) {
|
|
3856
|
+
this.pushHistoryMessages(session.id, options.resumeSessionId);
|
|
3857
|
+
}
|
|
3858
|
+
serviceLogger.info(
|
|
3859
|
+
{ sessionId: session.id, cwd: options.sessionCwd },
|
|
3860
|
+
"JSON session created via relay"
|
|
3861
|
+
);
|
|
3862
|
+
this.deps.controlHandlers.pushCommandList(session.id, options.sessionCwd);
|
|
3863
|
+
this.deps.broadcastSessionSync(session);
|
|
3864
|
+
this.deps.broadcastSessionList();
|
|
3865
|
+
}
|
|
3866
|
+
reportJsonStartupFailure(requestId, sessionId, message, reason) {
|
|
3867
|
+
this.cleanupPendingJsonSession(sessionId);
|
|
3868
|
+
this.deps.relaySend(
|
|
3869
|
+
serializeControl({
|
|
3870
|
+
type: "session_create_response",
|
|
3871
|
+
requestId,
|
|
3872
|
+
sessionId,
|
|
3873
|
+
errorCode: ControlErrorCode.WORKER_START_FAILED,
|
|
3874
|
+
error: message
|
|
3875
|
+
})
|
|
3876
|
+
);
|
|
3877
|
+
serviceLogger.error(
|
|
3878
|
+
{
|
|
3879
|
+
sessionId,
|
|
3880
|
+
error: reason instanceof Error ? reason.message : String(reason),
|
|
3881
|
+
timedOut: message === WORKER_START_TIMEOUT_MESSAGE
|
|
3882
|
+
},
|
|
3883
|
+
"JSON worker startup failed via relay"
|
|
3884
|
+
);
|
|
3719
3885
|
}
|
|
3720
3886
|
cleanupPendingJsonSession(sessionId) {
|
|
3721
3887
|
const killed = this.deps.workerRegistry.terminateProcess(sessionId);
|
|
@@ -3746,8 +3912,9 @@ var RelaySessionCreateHandler = class {
|
|
|
3746
3912
|
const requestedName = normalizeSessionName(msg.name);
|
|
3747
3913
|
const name = requestedName ?? tildify(cwd);
|
|
3748
3914
|
const nameLocked = requestedName !== void 0;
|
|
3749
|
-
|
|
3915
|
+
let session;
|
|
3750
3916
|
try {
|
|
3917
|
+
const hook = this.deps.createHookContext(pendingId, provider);
|
|
3751
3918
|
const pid = this.deps.hostedPtyRegistry.start({
|
|
3752
3919
|
sessionId: pendingId,
|
|
3753
3920
|
provider,
|
|
@@ -3756,7 +3923,7 @@ var RelaySessionCreateHandler = class {
|
|
|
3756
3923
|
permissionMode,
|
|
3757
3924
|
hook
|
|
3758
3925
|
});
|
|
3759
|
-
|
|
3926
|
+
session = this.deps.sessionManager.createSession(
|
|
3760
3927
|
"pty",
|
|
3761
3928
|
cwd,
|
|
3762
3929
|
pid,
|
|
@@ -3766,31 +3933,8 @@ var RelaySessionCreateHandler = class {
|
|
|
3766
3933
|
"proxy-hosted",
|
|
3767
3934
|
nameLocked
|
|
3768
3935
|
);
|
|
3769
|
-
if (resumeSessionId) {
|
|
3770
|
-
if (provider === "claude") {
|
|
3771
|
-
this.deps.sessionManager.setClaudeSessionId(session.id, resumeSessionId);
|
|
3772
|
-
} else {
|
|
3773
|
-
this.deps.sessionManager.setHistorySessionId(session.id, resumeSessionId);
|
|
3774
|
-
}
|
|
3775
|
-
}
|
|
3776
|
-
this.deps.relaySend(
|
|
3777
|
-
serializeControl({
|
|
3778
|
-
type: "session_create_response",
|
|
3779
|
-
requestId: msg.requestId,
|
|
3780
|
-
sessionId: session.id,
|
|
3781
|
-
name: session.name,
|
|
3782
|
-
nameLocked: session.nameLocked,
|
|
3783
|
-
mode: "pty",
|
|
3784
|
-
provider,
|
|
3785
|
-
ptyOwner: "proxy-hosted"
|
|
3786
|
-
})
|
|
3787
|
-
);
|
|
3788
|
-
this.deps.controlHandlers.pushCommandList(session.id, cwd);
|
|
3789
|
-
this.deps.controlHandlers.pushFileTree(session.id, cwd);
|
|
3790
|
-
this.deps.broadcastSessionSync(session);
|
|
3791
|
-
this.deps.broadcastSessionList();
|
|
3792
|
-
serviceLogger.info({ sessionId: session.id, provider, cwd }, "Hosted PTY session created");
|
|
3793
3936
|
} catch (err) {
|
|
3937
|
+
this.cleanupPendingHostedPtySession(pendingId);
|
|
3794
3938
|
const error = err instanceof Error ? err.message : String(err);
|
|
3795
3939
|
this.deps.relaySend(
|
|
3796
3940
|
serializeControl({
|
|
@@ -3812,7 +3956,43 @@ var RelaySessionCreateHandler = class {
|
|
|
3812
3956
|
},
|
|
3813
3957
|
"Hosted PTY session create failed"
|
|
3814
3958
|
);
|
|
3959
|
+
return;
|
|
3960
|
+
}
|
|
3961
|
+
if (resumeSessionId) {
|
|
3962
|
+
if (provider === "claude") {
|
|
3963
|
+
this.deps.sessionManager.setClaudeSessionId(session.id, resumeSessionId);
|
|
3964
|
+
} else {
|
|
3965
|
+
this.deps.sessionManager.setHistorySessionId(session.id, resumeSessionId);
|
|
3966
|
+
}
|
|
3815
3967
|
}
|
|
3968
|
+
this.deps.relaySend(
|
|
3969
|
+
serializeControl({
|
|
3970
|
+
type: "session_create_response",
|
|
3971
|
+
requestId: msg.requestId,
|
|
3972
|
+
sessionId: session.id,
|
|
3973
|
+
name: session.name,
|
|
3974
|
+
nameLocked: session.nameLocked,
|
|
3975
|
+
mode: "pty",
|
|
3976
|
+
provider,
|
|
3977
|
+
ptyOwner: "proxy-hosted"
|
|
3978
|
+
})
|
|
3979
|
+
);
|
|
3980
|
+
this.deps.controlHandlers.pushCommandList(session.id, cwd);
|
|
3981
|
+
this.deps.controlHandlers.pushFileTree(session.id, cwd);
|
|
3982
|
+
this.deps.broadcastSessionSync(session);
|
|
3983
|
+
this.deps.broadcastSessionList();
|
|
3984
|
+
serviceLogger.info({ sessionId: session.id, provider, cwd }, "Hosted PTY session created");
|
|
3985
|
+
}
|
|
3986
|
+
cleanupPendingHostedPtySession(sessionId) {
|
|
3987
|
+
const killed = this.deps.hostedPtyRegistry.abortStartup(sessionId);
|
|
3988
|
+
rmSync(sessionPaths(sessionId).dir, { recursive: true, force: true });
|
|
3989
|
+
this.deps.cleanupHookContext(sessionId);
|
|
3990
|
+
this.deps.permissionBroker.cleanupSession(sessionId, "Hosted PTY failed to start");
|
|
3991
|
+
this.deps.agentStatusRegistry.delete(sessionId);
|
|
3992
|
+
serviceLogger.warn(
|
|
3993
|
+
{ sessionId, killed },
|
|
3994
|
+
"Cleaned up pending hosted PTY session after startup failure"
|
|
3995
|
+
);
|
|
3816
3996
|
}
|
|
3817
3997
|
createShellTerminalSession(msg) {
|
|
3818
3998
|
const pendingId = nanoid3();
|
|
@@ -4466,6 +4646,7 @@ var RelayRouter = class {
|
|
|
4466
4646
|
ts.write(serializeIpc({ type: "pty_subscribe", sessionId: sid, requestId }));
|
|
4467
4647
|
serviceLogger.info({ sessionId: sid, requestId }, "Subscribe forwarded to terminal");
|
|
4468
4648
|
} else {
|
|
4649
|
+
this.deps.terminalSubscriptionBacklog.add(sid, requestId);
|
|
4469
4650
|
serviceLogger.warn({ sessionId: sid }, "Subscribe failed: terminal socket not available");
|
|
4470
4651
|
}
|
|
4471
4652
|
}
|
|
@@ -4753,9 +4934,12 @@ var AgentStatusRegistry = class {
|
|
|
4753
4934
|
};
|
|
4754
4935
|
|
|
4755
4936
|
// src/serve/terminal-worker-spawner.ts
|
|
4937
|
+
function buildTerminalWorkerArgs(options, profileName = PROFILE_NAME) {
|
|
4938
|
+
return ["--profile", profileName, options.sessionId, options.cwd, options.name];
|
|
4939
|
+
}
|
|
4756
4940
|
var TerminalWorkerSpawner = class {
|
|
4757
4941
|
start(options) {
|
|
4758
|
-
const child = spawnScript("terminal-worker",
|
|
4942
|
+
const child = spawnScript("terminal-worker", buildTerminalWorkerArgs(options), {
|
|
4759
4943
|
env: { ...process.env },
|
|
4760
4944
|
logger: serviceLogger
|
|
4761
4945
|
});
|
|
@@ -4887,6 +5071,11 @@ function changeSessionState(sessionManager, relay, sessionId, next) {
|
|
|
4887
5071
|
if (changed) pushSessionStatus(relay, sessionManager, sessionId);
|
|
4888
5072
|
return changed;
|
|
4889
5073
|
}
|
|
5074
|
+
function changeTerminalCwd(sessionManager, relay, sessionId, cwd) {
|
|
5075
|
+
const changed = sessionManager.updateTerminalCwd(sessionId, cwd);
|
|
5076
|
+
if (changed) broadcastSessionList(relay, sessionManager);
|
|
5077
|
+
return changed;
|
|
5078
|
+
}
|
|
4890
5079
|
function touchSessionActivity(sessionManager, relay, sessionId, now = Date.now()) {
|
|
4891
5080
|
const touched = sessionManager.touchSession(sessionId, now, ACTIVITY_STATUS_PUSH_INTERVAL_MS);
|
|
4892
5081
|
if (touched) pushSessionStatus(relay, sessionManager, sessionId);
|
|
@@ -4897,6 +5086,7 @@ function touchSessionActivity(sessionManager, relay, sessionId, now = Date.now()
|
|
|
4897
5086
|
function createEventBridge(deps) {
|
|
4898
5087
|
const changeState = (sessionId, next) => changeSessionState(deps.sessionManager, deps.relayConnection, sessionId, next);
|
|
4899
5088
|
const touchActivity = (sessionId) => touchSessionActivity(deps.sessionManager, deps.relayConnection, sessionId);
|
|
5089
|
+
const updateTerminalCwd = (sessionId, cwd) => changeTerminalCwd(deps.sessionManager, deps.relayConnection, sessionId, cwd);
|
|
4900
5090
|
const emitAgentStatus = (sessionId, phase) => {
|
|
4901
5091
|
const session = deps.sessionManager.getSession(sessionId);
|
|
4902
5092
|
if (!session) return;
|
|
@@ -4940,6 +5130,7 @@ function createEventBridge(deps) {
|
|
|
4940
5130
|
return {
|
|
4941
5131
|
changeSessionState: changeState,
|
|
4942
5132
|
touchSessionActivity: touchActivity,
|
|
5133
|
+
updateTerminalCwd,
|
|
4943
5134
|
emitAgentStatus,
|
|
4944
5135
|
cleanupSessionResources
|
|
4945
5136
|
};
|
|
@@ -5018,15 +5209,14 @@ function handleTerminalConnection(socket, deps) {
|
|
|
5018
5209
|
sessionManager,
|
|
5019
5210
|
workerRegistry,
|
|
5020
5211
|
terminalSockets,
|
|
5212
|
+
terminalSubscriptionBacklog,
|
|
5021
5213
|
hostedPtyRegistry,
|
|
5022
5214
|
relayConnection,
|
|
5023
|
-
controlHandlers,
|
|
5024
|
-
agentStatusRegistry,
|
|
5025
5215
|
permissionBroker,
|
|
5026
5216
|
createHookContext,
|
|
5027
5217
|
emitAgentStatus,
|
|
5218
|
+
updateTerminalCwd,
|
|
5028
5219
|
resolveInterruptedApprovals: resolveInterruptedApprovals2,
|
|
5029
|
-
cleanupSessionResources,
|
|
5030
5220
|
config
|
|
5031
5221
|
} = deps;
|
|
5032
5222
|
const bridgeDeps = {
|
|
@@ -5113,6 +5303,10 @@ function handleTerminalConnection(socket, deps) {
|
|
|
5113
5303
|
);
|
|
5114
5304
|
break;
|
|
5115
5305
|
}
|
|
5306
|
+
case "pty_cwd_change": {
|
|
5307
|
+
updateTerminalCwd(msg.sessionId, msg.cwd);
|
|
5308
|
+
break;
|
|
5309
|
+
}
|
|
5116
5310
|
case "pty_semantic_event": {
|
|
5117
5311
|
if (!sessionManager.getSession(msg.sessionId)) break;
|
|
5118
5312
|
const logPayload = {
|
|
@@ -5159,11 +5353,8 @@ function handleTerminalConnection(socket, deps) {
|
|
|
5159
5353
|
{
|
|
5160
5354
|
sessionManager,
|
|
5161
5355
|
workerRegistry,
|
|
5162
|
-
controlHandlers,
|
|
5163
5356
|
terminalSockets,
|
|
5164
|
-
hostedPtyRegistry
|
|
5165
|
-
agentStatusRegistry,
|
|
5166
|
-
broadcastSessionList: () => broadcastSessionList(relayConnection, sessionManager)
|
|
5357
|
+
hostedPtyRegistry
|
|
5167
5358
|
},
|
|
5168
5359
|
msg.sessionId
|
|
5169
5360
|
);
|
|
@@ -5201,10 +5392,28 @@ function handleTerminalConnection(socket, deps) {
|
|
|
5201
5392
|
broadcastSessionSync(relayConnection, session);
|
|
5202
5393
|
}
|
|
5203
5394
|
broadcastSessionList(relayConnection, sessionManager);
|
|
5395
|
+
const pendingSubscribes = terminalSubscriptionBacklog.take(msg.sessionId);
|
|
5396
|
+
for (const pending of pendingSubscribes) {
|
|
5397
|
+
if (!socket.writable) break;
|
|
5398
|
+
socket.write(
|
|
5399
|
+
serializeIpc({
|
|
5400
|
+
type: "pty_subscribe",
|
|
5401
|
+
sessionId: msg.sessionId,
|
|
5402
|
+
requestId: pending.requestId
|
|
5403
|
+
})
|
|
5404
|
+
);
|
|
5405
|
+
}
|
|
5406
|
+
if (pendingSubscribes.length > 0) {
|
|
5407
|
+
serviceLogger.info(
|
|
5408
|
+
{ sessionId: msg.sessionId, count: pendingSubscribes.length },
|
|
5409
|
+
"Pending PTY subscribes forwarded to terminal"
|
|
5410
|
+
);
|
|
5411
|
+
}
|
|
5204
5412
|
serviceLogger.info({ sessionId: msg.sessionId }, "PTY session registered");
|
|
5205
5413
|
break;
|
|
5206
5414
|
}
|
|
5207
5415
|
case "pty_deregister": {
|
|
5416
|
+
terminalSubscriptionBacklog.delete(msg.sessionId);
|
|
5208
5417
|
relayConnection.sendRaw(
|
|
5209
5418
|
serializeControl({
|
|
5210
5419
|
type: "pty_state",
|
|
@@ -5214,7 +5423,6 @@ function handleTerminalConnection(socket, deps) {
|
|
|
5214
5423
|
);
|
|
5215
5424
|
sessionManager.terminateSession(msg.sessionId);
|
|
5216
5425
|
terminalSockets.delete(msg.sessionId);
|
|
5217
|
-
cleanupSessionResources(msg.sessionId);
|
|
5218
5426
|
serviceLogger.info({ sessionId: msg.sessionId }, "PTY session deregistered");
|
|
5219
5427
|
break;
|
|
5220
5428
|
}
|
|
@@ -5287,6 +5495,7 @@ function handleTerminalConnection(socket, deps) {
|
|
|
5287
5495
|
for (const [sessionId, terminalSocket] of terminalSockets) {
|
|
5288
5496
|
if (terminalSocket === socket) {
|
|
5289
5497
|
terminalSockets.delete(sessionId);
|
|
5498
|
+
terminalSubscriptionBacklog.delete(sessionId);
|
|
5290
5499
|
const session = sessionManager.getSession(sessionId);
|
|
5291
5500
|
if (!session) {
|
|
5292
5501
|
serviceLogger.info({ sessionId }, "Terminal socket closed, session already cleaned");
|
|
@@ -5307,7 +5516,6 @@ function handleTerminalConnection(socket, deps) {
|
|
|
5307
5516
|
})
|
|
5308
5517
|
);
|
|
5309
5518
|
sessionManager.terminateSession(sessionId);
|
|
5310
|
-
cleanupSessionResources(sessionId);
|
|
5311
5519
|
serviceLogger.info(
|
|
5312
5520
|
{ sessionId },
|
|
5313
5521
|
"PTY session cleaned up on socket close (crash fallback)"
|
|
@@ -5511,7 +5719,7 @@ var HookServer = class {
|
|
|
5511
5719
|
{ sessionId: binding.sessionId, provider: binding.provider, event: parsed.event },
|
|
5512
5720
|
"Provider hook ignored for inactive session"
|
|
5513
5721
|
);
|
|
5514
|
-
this.
|
|
5722
|
+
this.writeNeutralProviderResponse(res);
|
|
5515
5723
|
return;
|
|
5516
5724
|
}
|
|
5517
5725
|
const payload = asRecord(parsed.payload);
|
|
@@ -5528,7 +5736,7 @@ var HookServer = class {
|
|
|
5528
5736
|
return;
|
|
5529
5737
|
}
|
|
5530
5738
|
this.options.onEvent?.(event);
|
|
5531
|
-
this.
|
|
5739
|
+
this.writeNeutralProviderResponse(res);
|
|
5532
5740
|
}
|
|
5533
5741
|
async handlePermissionRequest(event, res) {
|
|
5534
5742
|
const requestId = event.requestId ?? (typeof event.payload.tool_use_id === "string" ? event.payload.tool_use_id : void 0) ?? this.nextFallbackRequestId(event.sessionId);
|
|
@@ -5561,28 +5769,10 @@ var HookServer = class {
|
|
|
5561
5769
|
}
|
|
5562
5770
|
};
|
|
5563
5771
|
}
|
|
5564
|
-
|
|
5565
|
-
if (eventName === "PreToolUse") {
|
|
5566
|
-
if (provider === "codex") {
|
|
5567
|
-
return null;
|
|
5568
|
-
}
|
|
5569
|
-
return {
|
|
5570
|
-
hookSpecificOutput: {
|
|
5571
|
-
hookEventName: "PreToolUse",
|
|
5572
|
-
permissionDecision: "ask"
|
|
5573
|
-
}
|
|
5574
|
-
};
|
|
5575
|
-
}
|
|
5576
|
-
return null;
|
|
5577
|
-
}
|
|
5578
|
-
writeProviderResponse(res, payload) {
|
|
5772
|
+
writeNeutralProviderResponse(res) {
|
|
5579
5773
|
if (res.headersSent) return;
|
|
5580
|
-
|
|
5581
|
-
|
|
5582
|
-
res.end();
|
|
5583
|
-
return;
|
|
5584
|
-
}
|
|
5585
|
-
this.writeJson(res, 200, payload);
|
|
5774
|
+
res.writeHead(200);
|
|
5775
|
+
res.end();
|
|
5586
5776
|
}
|
|
5587
5777
|
readBody(req) {
|
|
5588
5778
|
return new Promise((resolve3, reject) => {
|
|
@@ -5840,7 +6030,7 @@ import { basename } from "path";
|
|
|
5840
6030
|
// src/serve/remote-file-path.ts
|
|
5841
6031
|
import { realpathSync } from "fs";
|
|
5842
6032
|
import { homedir as homedir6 } from "os";
|
|
5843
|
-
import { extname as extname2, isAbsolute as
|
|
6033
|
+
import { extname as extname2, isAbsolute as isAbsolute5, resolve as resolve2 } from "path";
|
|
5844
6034
|
var EXT_MIME_MAP = {
|
|
5845
6035
|
".png": "image/png",
|
|
5846
6036
|
".jpg": "image/jpeg",
|
|
@@ -5875,7 +6065,7 @@ function guessMimeType(filePath) {
|
|
|
5875
6065
|
}
|
|
5876
6066
|
function resolveRemoteFilePath(rawPath, cwd) {
|
|
5877
6067
|
const expandedPath = rawPath === "~" || rawPath.startsWith("~/") ? resolve2(homedir6(), rawPath.slice(2)) : rawPath;
|
|
5878
|
-
const candidate =
|
|
6068
|
+
const candidate = isAbsolute5(expandedPath) ? resolve2(expandedPath) : resolve2(cwd, expandedPath);
|
|
5879
6069
|
return realpathSync(candidate);
|
|
5880
6070
|
}
|
|
5881
6071
|
|
|
@@ -6088,6 +6278,29 @@ var RemoteFileStreamManager = class {
|
|
|
6088
6278
|
}
|
|
6089
6279
|
};
|
|
6090
6280
|
|
|
6281
|
+
// src/serve/terminal-subscription-backlog.ts
|
|
6282
|
+
var MAX_PENDING_SUBSCRIBES_PER_SESSION = 8;
|
|
6283
|
+
var TerminalSubscriptionBacklog = class {
|
|
6284
|
+
pending = /* @__PURE__ */ new Map();
|
|
6285
|
+
add(sessionId, requestId) {
|
|
6286
|
+
const entries = this.pending.get(sessionId) ?? [];
|
|
6287
|
+
if (requestId && entries.some((entry) => entry.requestId === requestId)) return;
|
|
6288
|
+
entries.push({ requestId });
|
|
6289
|
+
if (entries.length > MAX_PENDING_SUBSCRIBES_PER_SESSION) {
|
|
6290
|
+
entries.splice(0, entries.length - MAX_PENDING_SUBSCRIBES_PER_SESSION);
|
|
6291
|
+
}
|
|
6292
|
+
this.pending.set(sessionId, entries);
|
|
6293
|
+
}
|
|
6294
|
+
take(sessionId) {
|
|
6295
|
+
const entries = this.pending.get(sessionId) ?? [];
|
|
6296
|
+
this.pending.delete(sessionId);
|
|
6297
|
+
return entries;
|
|
6298
|
+
}
|
|
6299
|
+
delete(sessionId) {
|
|
6300
|
+
this.pending.delete(sessionId);
|
|
6301
|
+
}
|
|
6302
|
+
};
|
|
6303
|
+
|
|
6091
6304
|
// src/serve.ts
|
|
6092
6305
|
function resolveInterruptedApprovals(permissionBroker, hookEventRouter, relay, sessionId) {
|
|
6093
6306
|
const approvals = permissionBroker.listSession(sessionId);
|
|
@@ -6150,6 +6363,8 @@ async function startService(options) {
|
|
|
6150
6363
|
const agentStatusRegistry = new AgentStatusRegistry();
|
|
6151
6364
|
let unregisterHookSession = () => {
|
|
6152
6365
|
};
|
|
6366
|
+
let cleanupRemovedSessionRuntime = () => {
|
|
6367
|
+
};
|
|
6153
6368
|
const sessionManager = new SessionManager({
|
|
6154
6369
|
persistPath: SESSIONS_PATH,
|
|
6155
6370
|
historyMetadataPath: HISTORY_METADATA_PATH,
|
|
@@ -6157,13 +6372,12 @@ async function startService(options) {
|
|
|
6157
6372
|
if (!context?.preserveProviderHooks) {
|
|
6158
6373
|
unregisterHookSession(id);
|
|
6159
6374
|
}
|
|
6160
|
-
permissionBroker.cleanupSession(id, "session removed");
|
|
6161
|
-
agentStatusRegistry.delete(id);
|
|
6162
6375
|
const paths = sessionPaths(id);
|
|
6163
6376
|
try {
|
|
6164
6377
|
rmSync2(paths.dir, { recursive: true, force: true });
|
|
6165
6378
|
} catch {
|
|
6166
6379
|
}
|
|
6380
|
+
cleanupRemovedSessionRuntime(id);
|
|
6167
6381
|
}
|
|
6168
6382
|
});
|
|
6169
6383
|
sessionManager.startReaper();
|
|
@@ -6221,6 +6435,7 @@ async function startService(options) {
|
|
|
6221
6435
|
controlHandlers,
|
|
6222
6436
|
permissionBroker
|
|
6223
6437
|
});
|
|
6438
|
+
cleanupRemovedSessionRuntime = eventBridge.cleanupSessionResources;
|
|
6224
6439
|
const jsonObserver = new JsonObserver({
|
|
6225
6440
|
changeSessionState: eventBridge.changeSessionState,
|
|
6226
6441
|
emitAgentStatus: eventBridge.emitAgentStatus
|
|
@@ -6259,10 +6474,11 @@ async function startService(options) {
|
|
|
6259
6474
|
relayConnection,
|
|
6260
6475
|
getProviderEnv,
|
|
6261
6476
|
touchSessionActivity: eventBridge.touchSessionActivity,
|
|
6262
|
-
|
|
6263
|
-
|
|
6477
|
+
updateTerminalCwd: eventBridge.updateTerminalCwd,
|
|
6478
|
+
applyPtyStateToSession: (sessionId, ptyState) => applyPtyStateToSession(ptyBridgeDeps, sessionId, ptyState)
|
|
6264
6479
|
});
|
|
6265
6480
|
const terminalWorkerSpawner = new TerminalWorkerSpawner();
|
|
6481
|
+
const terminalSubscriptionBacklog = new TerminalSubscriptionBacklog();
|
|
6266
6482
|
const remoteFileStreamManager = new RemoteFileStreamManager({
|
|
6267
6483
|
relayConnection,
|
|
6268
6484
|
sessionManager
|
|
@@ -6304,7 +6520,8 @@ async function startService(options) {
|
|
|
6304
6520
|
getAgentCliSuggestions,
|
|
6305
6521
|
setAgentCliPath,
|
|
6306
6522
|
remoteFileStreamManager,
|
|
6307
|
-
remoteFileUploadManager
|
|
6523
|
+
remoteFileUploadManager,
|
|
6524
|
+
terminalSubscriptionBacklog
|
|
6308
6525
|
});
|
|
6309
6526
|
relayConnection.on("message", (msg) => relayRouter.handle(msg));
|
|
6310
6527
|
relayConnection.on("binary", (data) => {
|
|
@@ -6339,15 +6556,14 @@ async function startService(options) {
|
|
|
6339
6556
|
sessionManager,
|
|
6340
6557
|
workerRegistry,
|
|
6341
6558
|
terminalSockets,
|
|
6559
|
+
terminalSubscriptionBacklog,
|
|
6342
6560
|
hostedPtyRegistry,
|
|
6343
6561
|
relayConnection,
|
|
6344
|
-
controlHandlers,
|
|
6345
|
-
agentStatusRegistry,
|
|
6346
6562
|
permissionBroker,
|
|
6347
6563
|
hookEventRouter: hookRuntime.hookEventRouter,
|
|
6348
6564
|
createHookContext: hookRuntime.createHookContext,
|
|
6349
6565
|
emitAgentStatus: eventBridge.emitAgentStatus,
|
|
6350
|
-
|
|
6566
|
+
updateTerminalCwd: eventBridge.updateTerminalCwd,
|
|
6351
6567
|
config: statusConfig,
|
|
6352
6568
|
resolveInterruptedApprovals: (sessionId) => resolveInterruptedApprovals(
|
|
6353
6569
|
permissionBroker,
|