@treestructuredplanning/gateway 0.3.0 → 0.3.4
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 +31 -6
- package/dist/cli.js +254 -161
- package/dist/gateway-schema.json +84 -0
- package/dist/{shared-KRIZBGVD.js → shared-IDNTQEWS.js} +1580 -598
- package/package.json +1 -1
|
@@ -1,7 +1,59 @@
|
|
|
1
1
|
import { createRequire } from "node:module"; const require = createRequire(import.meta.url);
|
|
2
2
|
|
|
3
|
+
// package.json
|
|
4
|
+
var package_default = {
|
|
5
|
+
name: "@treestructuredplanning/gateway",
|
|
6
|
+
version: "0.3.4",
|
|
7
|
+
description: "Outbound local harness gateway for Tree-Structured Planning",
|
|
8
|
+
type: "module",
|
|
9
|
+
engines: {
|
|
10
|
+
node: ">=22"
|
|
11
|
+
},
|
|
12
|
+
bin: {
|
|
13
|
+
"tsp-gateway": "dist/cli.js"
|
|
14
|
+
},
|
|
15
|
+
files: [
|
|
16
|
+
"dist/cli.js",
|
|
17
|
+
"dist/shared-*.js",
|
|
18
|
+
"dist/vendor.js",
|
|
19
|
+
"dist/gateway-schema.json",
|
|
20
|
+
"README.md"
|
|
21
|
+
],
|
|
22
|
+
scripts: {
|
|
23
|
+
build: "node scripts/build.mjs",
|
|
24
|
+
prepack: "pnpm build",
|
|
25
|
+
test: "pnpm build && node --test --test-concurrency=3 --test-timeout=120000 --test-force-exit test/*.test.mjs",
|
|
26
|
+
"measure:launch": "pnpm build && node scripts/measure-launch.mjs",
|
|
27
|
+
typecheck: "tsc --noEmit",
|
|
28
|
+
lint: "eslint src --report-unused-disable-directives --max-warnings 0",
|
|
29
|
+
check: "pnpm typecheck && pnpm lint && pnpm format:check && pnpm test",
|
|
30
|
+
"format:check": 'prettier --check "src/**/*.ts" "test/*.mjs" "scripts/*.mjs" README.md package.json tsconfig.json',
|
|
31
|
+
"test:coverage": 'pnpm build && node --test-concurrency=3 --test-timeout=120000 --test-force-exit --experimental-test-coverage --test-coverage-lines=80 --test --test-coverage-include="dist/*.js" --test-coverage-exclude="dist/vendor.js" test/*.test.mjs'
|
|
32
|
+
},
|
|
33
|
+
devDependencies: {
|
|
34
|
+
"@tsp/shared": "workspace:*",
|
|
35
|
+
"@types/node": "^24.10.9",
|
|
36
|
+
esbuild: "0.25.12",
|
|
37
|
+
typescript: "^5.7.2"
|
|
38
|
+
},
|
|
39
|
+
license: "MIT",
|
|
40
|
+
dependencies: {
|
|
41
|
+
"@napi-rs/keyring": "2.1.2",
|
|
42
|
+
"smol-toml": "1.6.0",
|
|
43
|
+
ws: "8.21.0"
|
|
44
|
+
},
|
|
45
|
+
publishConfig: {
|
|
46
|
+
access: "public"
|
|
47
|
+
},
|
|
48
|
+
repository: {
|
|
49
|
+
type: "git",
|
|
50
|
+
url: "git+https://github.com/WebifyServices/tsp.git",
|
|
51
|
+
directory: "packages/tsp-gateway"
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
3
55
|
// ../tsp-shared/src/generated/constants.ts
|
|
4
|
-
var GATEWAY_PACKAGE_VERSION = "0.3.
|
|
56
|
+
var GATEWAY_PACKAGE_VERSION = "0.3.4";
|
|
5
57
|
var GATEWAY_PROTOCOL_VERSION = 3;
|
|
6
58
|
var SESSION_ACTIVITY_COUNT_MAX = 1e6;
|
|
7
59
|
var AGENT_SESSION_STATE = {
|
|
@@ -14,12 +66,34 @@ var AGENT_SESSION_STATE = {
|
|
|
14
66
|
ENDED: "ended",
|
|
15
67
|
UNKNOWN: "unknown"
|
|
16
68
|
};
|
|
69
|
+
var GATEWAY_CLOSE_CODE = {
|
|
70
|
+
/** Observed transport loss only; RFC 6455 forbids sending this code. */
|
|
71
|
+
ABNORMAL_CLOSURE: 1006,
|
|
72
|
+
/** Terminal: re-pair the machine. */
|
|
73
|
+
MACHINE_REVOKED: 4501,
|
|
74
|
+
/** Terminal: re-pair with the owning account. */
|
|
75
|
+
MACHINE_NOT_OWNED: 4502,
|
|
76
|
+
/** Keep running; retry hourly until the gateway or hub is updated. */
|
|
77
|
+
PROTOCOL_OUTDATED: 4503,
|
|
78
|
+
/** Refresh once and reconnect; failed refresh is terminal and requires re-pair. */
|
|
79
|
+
TOKEN_EXPIRED_OR_INVALID: 4505,
|
|
80
|
+
/** Back off only if still the running daemon; otherwise exit. */
|
|
81
|
+
SUPERSEDED: 4506,
|
|
82
|
+
/** Reconnect with jittered backoff after server restart. */
|
|
83
|
+
SERVER_RESTARTING: 4507,
|
|
84
|
+
/** Reconnect with jittered backoff after heartbeat loss. */
|
|
85
|
+
HEARTBEAT_TIMEOUT: 4508
|
|
86
|
+
};
|
|
17
87
|
var GATEWAY_CLOSE_DISPOSITION = {
|
|
18
88
|
TERMINAL: "terminal",
|
|
19
89
|
TOKEN: "token",
|
|
20
90
|
OUTDATED: "outdated",
|
|
21
91
|
RECONNECTABLE: "reconnectable"
|
|
22
92
|
};
|
|
93
|
+
var GATEWAY_INSTALL = {
|
|
94
|
+
GLOBAL: "global",
|
|
95
|
+
NPX: "npx"
|
|
96
|
+
};
|
|
23
97
|
var HARNESS_KIND = {
|
|
24
98
|
CLAUDE_CODE: "claude_code",
|
|
25
99
|
CODEX: "codex",
|
|
@@ -53,6 +127,7 @@ var LAUNCH_REFUSAL_REASON = {
|
|
|
53
127
|
LAUNCH_SUPERSEDED: "launch_superseded",
|
|
54
128
|
LAUNCH_EXPIRED: "launch_expired",
|
|
55
129
|
LAUNCH_WITHDRAWN: "launch_withdrawn",
|
|
130
|
+
LAUNCH_DISMISSED: "launch_dismissed",
|
|
56
131
|
CONTROL_EXPIRED: "control_expired",
|
|
57
132
|
CONTROL_WITHDRAWN: "control_withdrawn"
|
|
58
133
|
};
|
|
@@ -72,6 +147,85 @@ var SESSION_ACTIVITY_KIND = {
|
|
|
72
147
|
/** Any other tool: the web, a skill, another MCP server. */
|
|
73
148
|
USING_TOOL: "using_tool"
|
|
74
149
|
};
|
|
150
|
+
var WS_CLOSE_CODE = {
|
|
151
|
+
/**
|
|
152
|
+
* A clean end. The frontend sends it when it unmounts or rotates a token, and a
|
|
153
|
+
* locally closed socket is not reconnected. A peer's Close frame ends only the
|
|
154
|
+
* transport, so clients reconnect with backoff. The server's own closes carry
|
|
155
|
+
* a more specific code.
|
|
156
|
+
*/
|
|
157
|
+
NORMAL_CLOSURE: 1e3,
|
|
158
|
+
/**
|
|
159
|
+
* The peer is leaving, as a browser does when it navigates away. Like
|
|
160
|
+
* `NORMAL_CLOSURE` it ends the transport; clients reconnect with backoff
|
|
161
|
+
* unless they have locally disposed the socket.
|
|
162
|
+
*/
|
|
163
|
+
GOING_AWAY: 1001,
|
|
164
|
+
/**
|
|
165
|
+
* The transport vanished without a Close frame. RFC 6455 forbids sending
|
|
166
|
+
* it; `FastAPIChannel.recv` raises it internally so pycrdt's receive loop
|
|
167
|
+
* ends the way Starlette reports a dropped peer.
|
|
168
|
+
*/
|
|
169
|
+
ABNORMAL_CLOSURE: 1006,
|
|
170
|
+
/** The per-IP connection rate limit rejected the socket. */
|
|
171
|
+
POLICY_VIOLATION: 1008,
|
|
172
|
+
/**
|
|
173
|
+
* A transient infrastructure failure (plan, membership or branch lookup)
|
|
174
|
+
* refused the socket. Clients reconnect with backoff instead of treating it
|
|
175
|
+
* as a permanent access refusal. Generation and plan events share a bounded
|
|
176
|
+
* retry budget; y-websocket owns the Yjs retry loop.
|
|
177
|
+
*/
|
|
178
|
+
INTERNAL_ERROR: 1011,
|
|
179
|
+
/**
|
|
180
|
+
* The ASGI server is restarting (uvicorn sends this during shutdown).
|
|
181
|
+
* Clients reconnect with backoff.
|
|
182
|
+
*/
|
|
183
|
+
SERVICE_RESTART: 1012,
|
|
184
|
+
/**
|
|
185
|
+
* The Yjs room's previous session left a rejected document the registry
|
|
186
|
+
* could not discard, so serving it would hand the client edits storage
|
|
187
|
+
* refuses. Transient: the next connect retries the discard. Nothing latches
|
|
188
|
+
* it, and the socket is accepted before it is closed, so y-websocket resets
|
|
189
|
+
* its backoff and retries about every 100 ms until the room is servable.
|
|
190
|
+
*/
|
|
191
|
+
TRY_AGAIN_LATER: 1013,
|
|
192
|
+
/**
|
|
193
|
+
* No usable credential: missing, expired, invalid, MCP-only or
|
|
194
|
+
* invalidated token. Recoverable with a fresh token: the generation hook
|
|
195
|
+
* refreshes it and retries, Yjs refreshes once until a successful sync, and
|
|
196
|
+
* plan events reopens when it rotates. An invalidated refresh credential
|
|
197
|
+
* cannot restore the session; sign-in is required.
|
|
198
|
+
*/
|
|
199
|
+
AUTH_FAILED: 4001,
|
|
200
|
+
/**
|
|
201
|
+
* The token is valid but its `email_verified` claim is false. A token
|
|
202
|
+
* minted before the account verified keeps the claim until it is refreshed.
|
|
203
|
+
* Generation and Yjs attempt a bounded refresh and stop if verification is
|
|
204
|
+
* still required; local edits are preserved. Plan events follows rotation.
|
|
205
|
+
*/
|
|
206
|
+
EMAIL_NOT_VERIFIED: 4002,
|
|
207
|
+
/**
|
|
208
|
+
* A live socket was closed because its workspace membership was removed or
|
|
209
|
+
* demoted below `COLLAB_ROLES` (`tsp_api.transports.yjs.revocation`, #1274). Distinct
|
|
210
|
+
* from `ACCESS_DENIED`, which refuses a connect (#1256), so the frontend
|
|
211
|
+
* branches on `CloseEvent.code` instead of parsing `CloseEvent.reason`.
|
|
212
|
+
*/
|
|
213
|
+
PERMISSION_REVOKED: 4003,
|
|
214
|
+
/** At connect: the plan is not the tenant's, or the membership is missing. Retrying cannot change the answer. */
|
|
215
|
+
ACCESS_DENIED: 4403,
|
|
216
|
+
/**
|
|
217
|
+
* At connect: the plan has no such branch. A branch lookup that itself
|
|
218
|
+
* fails closes `INTERNAL_ERROR` instead, so a storage blip never reads as a
|
|
219
|
+
* deleted branch.
|
|
220
|
+
*/
|
|
221
|
+
BRANCH_NOT_FOUND: 4404,
|
|
222
|
+
/**
|
|
223
|
+
* At connect: an authenticated workspace member lacks a collaboration
|
|
224
|
+
* role. Stop the socket without refreshing the workspace session or hiding
|
|
225
|
+
* the REST-readable plan. Retrying cannot change the membership role.
|
|
226
|
+
*/
|
|
227
|
+
INSUFFICIENT_ROLE: 4405
|
|
228
|
+
};
|
|
75
229
|
var GATEWAY_CLOSE_DISPOSITION_BY_CODE = {
|
|
76
230
|
"1006": "reconnectable",
|
|
77
231
|
"4501": "terminal",
|
|
@@ -92,9 +246,9 @@ function contains(folder, path) {
|
|
|
92
246
|
}
|
|
93
247
|
async function realLocation(path) {
|
|
94
248
|
let current = parse(path).root;
|
|
95
|
-
const
|
|
96
|
-
for (let index = 0; index <
|
|
97
|
-
const part =
|
|
249
|
+
const parts2 = path.slice(current.length).split(sep);
|
|
250
|
+
for (let index = 0; index < parts2.length; index++) {
|
|
251
|
+
const part = parts2[index];
|
|
98
252
|
if (part === "" || part === ".") continue;
|
|
99
253
|
if (part === "..") {
|
|
100
254
|
current = dirname(current);
|
|
@@ -104,7 +258,7 @@ async function realLocation(path) {
|
|
|
104
258
|
try {
|
|
105
259
|
current = await realpath(next);
|
|
106
260
|
} catch {
|
|
107
|
-
return resolve(next, ...
|
|
261
|
+
return resolve(next, ...parts2.slice(index + 1));
|
|
108
262
|
}
|
|
109
263
|
}
|
|
110
264
|
return current;
|
|
@@ -381,8 +535,12 @@ function decodeRegistration(value) {
|
|
|
381
535
|
throw new Error("Invalid machine hub path");
|
|
382
536
|
return registration;
|
|
383
537
|
}
|
|
384
|
-
function
|
|
385
|
-
|
|
538
|
+
function decodeRelease(value) {
|
|
539
|
+
if (!matches(requiredSchema("GatewayRelease"), value)) throw new Error("Invalid release");
|
|
540
|
+
return value;
|
|
541
|
+
}
|
|
542
|
+
function parseJson(text5) {
|
|
543
|
+
return JSON.parse(text5);
|
|
386
544
|
}
|
|
387
545
|
function decodeHubText(data) {
|
|
388
546
|
if (typeof data !== "string" || data.length > 131072) throw new Error("Invalid frame");
|
|
@@ -430,19 +588,19 @@ function opaque(value) {
|
|
|
430
588
|
}
|
|
431
589
|
var WAITING_FOR_MAX_LENGTH = 4e3;
|
|
432
590
|
var LAST_MESSAGE_MAX_LENGTH = 240;
|
|
433
|
-
function sanitizeWords(
|
|
591
|
+
function sanitizeWords(text5, maxLength) {
|
|
434
592
|
return [
|
|
435
|
-
...
|
|
593
|
+
...text5.replace(
|
|
436
594
|
/(?:\\\\[?.]\\|\\\?\?\\|\\\\|[A-Za-z]:[\\/]|~?\/|\.\.?\/)[^\s"'<>]+/g,
|
|
437
595
|
"[local path]"
|
|
438
596
|
).split("").map((char) => char.charCodeAt(0) < 32 || char.charCodeAt(0) === 127 ? " " : char).join("")
|
|
439
597
|
].slice(0, maxLength).join("");
|
|
440
598
|
}
|
|
441
|
-
function sanitizeWaiting(
|
|
442
|
-
return sanitizeWords(
|
|
599
|
+
function sanitizeWaiting(text5) {
|
|
600
|
+
return sanitizeWords(text5, WAITING_FOR_MAX_LENGTH);
|
|
443
601
|
}
|
|
444
|
-
function sanitizeLastMessage(
|
|
445
|
-
return sanitizeWords(
|
|
602
|
+
function sanitizeLastMessage(text5) {
|
|
603
|
+
return sanitizeWords(text5, LAST_MESSAGE_MAX_LENGTH);
|
|
446
604
|
}
|
|
447
605
|
var STARTUP_GRACE_MS = 1e4;
|
|
448
606
|
function backgroundSession(row) {
|
|
@@ -496,52 +654,6 @@ async function observedState(session, signedIn) {
|
|
|
496
654
|
}
|
|
497
655
|
}
|
|
498
656
|
|
|
499
|
-
// src/checkout-roots.ts
|
|
500
|
-
import { homedir } from "node:os";
|
|
501
|
-
import { isAbsolute as isAbsolute3, relative as relative2, resolve as resolve2, sep as sep2 } from "node:path";
|
|
502
|
-
var UNRESOLVED_WORKTREE_ROOT = "<clone>/.tsp/worktrees";
|
|
503
|
-
function inside(parent, child) {
|
|
504
|
-
const path = relative2(parent, child);
|
|
505
|
-
return path === "" || !path.startsWith(`..${sep2}`) && path !== ".." && !isAbsolute3(path);
|
|
506
|
-
}
|
|
507
|
-
function decodeWorktreeRoot(value, clone) {
|
|
508
|
-
if (value === null) return null;
|
|
509
|
-
if (typeof value !== "string" || !value.trim() || value.includes("\0"))
|
|
510
|
-
throw new Error("Invalid worktree root");
|
|
511
|
-
if (value.startsWith("~") && !value.startsWith("~/")) throw new Error("Invalid worktree root");
|
|
512
|
-
const expanded = value.startsWith("~/") ? resolve2(homedir(), value.slice(2)) : value;
|
|
513
|
-
if (clone && inside(resolve2(clone, expanded), resolve2(clone)))
|
|
514
|
-
throw new Error("Worktree root must not contain the clone");
|
|
515
|
-
if (isAbsolute3(expanded) && resolve2(expanded) === resolve2(expanded, ".."))
|
|
516
|
-
throw new Error("Worktree root must not contain the clone");
|
|
517
|
-
if (!clone && !isAbsolute3(expanded) && inside(resolve2("/clone", expanded), "/clone"))
|
|
518
|
-
throw new Error("Worktree root must not contain the clone");
|
|
519
|
-
return value;
|
|
520
|
-
}
|
|
521
|
-
function worktreeSetting(clone, config) {
|
|
522
|
-
const registered2 = Object.values(config.repositories).find((row) => row.folder === clone);
|
|
523
|
-
if (registered2?.worktreeRoot != null) return "repository";
|
|
524
|
-
return config.worktrees.root != null ? "machine" : null;
|
|
525
|
-
}
|
|
526
|
-
function worktreeRoot(clone, config, top = clone) {
|
|
527
|
-
const registered2 = Object.values(config.repositories).find((row) => row.folder === clone);
|
|
528
|
-
const configured = registered2?.worktreeRoot ?? config.worktrees.root;
|
|
529
|
-
decodeWorktreeRoot(configured, clone);
|
|
530
|
-
const folder = configured === null ? resolve2(top, ".tsp/worktrees") : resolve2(
|
|
531
|
-
clone,
|
|
532
|
-
configured.startsWith("~/") ? resolve2(homedir(), configured.slice(2)) : configured
|
|
533
|
-
);
|
|
534
|
-
return configured === null ? { folder, location: "in_clone", setting: null } : { folder, location: "custom", setting: worktreeSetting(clone, config) };
|
|
535
|
-
}
|
|
536
|
-
function worktreeFolder(root, attemptId) {
|
|
537
|
-
if (!/^att_[0-9a-f]{12}$/.test(attemptId)) throw new Error("Invalid attempt identity");
|
|
538
|
-
return resolve2(root.folder, attemptId);
|
|
539
|
-
}
|
|
540
|
-
function excludeLine(clone, root) {
|
|
541
|
-
if (!inside(clone, root.folder)) return null;
|
|
542
|
-
return root.location === "in_clone" ? "/.tsp/" : `/${relative2(clone, root.folder).split(sep2).map((segment) => segment.replace(/[\\*?[\]]/g, "\\$&")).join("/")}/`;
|
|
543
|
-
}
|
|
544
|
-
|
|
545
657
|
// src/process.ts
|
|
546
658
|
import { spawn } from "node:child_process";
|
|
547
659
|
function execute(file, args, env, cwd, { timeoutMs = 3e4 } = {}) {
|
|
@@ -590,7 +702,7 @@ function pidAlive(pid) {
|
|
|
590
702
|
import { constants } from "node:fs";
|
|
591
703
|
import { access, stat } from "node:fs/promises";
|
|
592
704
|
import { userInfo } from "node:os";
|
|
593
|
-
import { basename, isAbsolute as
|
|
705
|
+
import { basename, isAbsolute as isAbsolute3 } from "node:path";
|
|
594
706
|
var posix = {
|
|
595
707
|
flags: ["-l", "-i", "-c"],
|
|
596
708
|
command: 'cd /; cd -- "$5" || { "$1" -e "$6" "$3" "$5"; exit 125; }; "$1" -e "$2" "$3" "$4" || exit; cd /; cd -- "$5" || { "$1" -e "$6" "" "$5"; exit 125; }; shift 6; exec "$@"',
|
|
@@ -620,7 +732,7 @@ var strategies = /* @__PURE__ */ new Map([
|
|
|
620
732
|
["xonsh", null]
|
|
621
733
|
]);
|
|
622
734
|
async function executable(path) {
|
|
623
|
-
if (!
|
|
735
|
+
if (!isAbsolute3(path)) return false;
|
|
624
736
|
try {
|
|
625
737
|
await access(path, constants.X_OK);
|
|
626
738
|
return (await stat(path)).isFile();
|
|
@@ -701,8 +813,8 @@ var ShellOutput = class {
|
|
|
701
813
|
return output;
|
|
702
814
|
}
|
|
703
815
|
};
|
|
704
|
-
function decodeEnvironment(
|
|
705
|
-
const value = parseJson(
|
|
816
|
+
function decodeEnvironment(text5) {
|
|
817
|
+
const value = parseJson(text5);
|
|
706
818
|
if (!isObject(value)) throw new Error("Invalid shell environment capture");
|
|
707
819
|
const env = {};
|
|
708
820
|
for (const name of Object.keys(value)) {
|
|
@@ -1010,6 +1122,127 @@ function launchRepositoryKey(repository) {
|
|
|
1010
1122
|
return repositoryKey(repository);
|
|
1011
1123
|
}
|
|
1012
1124
|
|
|
1125
|
+
// src/trust-cache.ts
|
|
1126
|
+
import { stat as stat2 } from "node:fs/promises";
|
|
1127
|
+
var TRUST_CACHE_MAX_AGE_MS = 5 * 6e4;
|
|
1128
|
+
async function stampOf(path) {
|
|
1129
|
+
try {
|
|
1130
|
+
const info = await stat2(path, { bigint: true });
|
|
1131
|
+
return `${info.dev}:${info.ino}:${info.size}:${info.mtimeNs}:${info.ctimeNs}`;
|
|
1132
|
+
} catch (error) {
|
|
1133
|
+
const code = error.code;
|
|
1134
|
+
return code === "ENOENT" || code === "ENOTDIR" ? "absent" : `unreadable:${code ?? "?"}`;
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
var TrustCache = class {
|
|
1138
|
+
constructor(clock = Date.now, maxAgeMs = TRUST_CACHE_MAX_AGE_MS) {
|
|
1139
|
+
this.clock = clock;
|
|
1140
|
+
this.maxAgeMs = maxAgeMs;
|
|
1141
|
+
}
|
|
1142
|
+
entries = /* @__PURE__ */ new Map();
|
|
1143
|
+
/** Forgets every answer, for a change no file stamp or key shows, such as the launch configuration. */
|
|
1144
|
+
clear() {
|
|
1145
|
+
this.entries.clear();
|
|
1146
|
+
}
|
|
1147
|
+
/** One facts check's reads: each file is stamped once, and each key read at most once. */
|
|
1148
|
+
pass(read) {
|
|
1149
|
+
return new TrustCachePass(this.entries, this.clock, this.maxAgeMs, read);
|
|
1150
|
+
}
|
|
1151
|
+
};
|
|
1152
|
+
var TrustCachePass = class {
|
|
1153
|
+
constructor(entries, clock, maxAgeMs, read) {
|
|
1154
|
+
this.entries = entries;
|
|
1155
|
+
this.clock = clock;
|
|
1156
|
+
this.maxAgeMs = maxAgeMs;
|
|
1157
|
+
this.read = read;
|
|
1158
|
+
}
|
|
1159
|
+
stamps = /* @__PURE__ */ new Map();
|
|
1160
|
+
answers = /* @__PURE__ */ new Map();
|
|
1161
|
+
/**
|
|
1162
|
+
* The answer for `key`: the cached one while its files' stamps are unchanged and it is younger
|
|
1163
|
+
* than the maximum age, otherwise `full`, which names each file it reads through `watch`.
|
|
1164
|
+
*/
|
|
1165
|
+
get(key, full) {
|
|
1166
|
+
let answer = this.answers.get(key);
|
|
1167
|
+
if (!answer) {
|
|
1168
|
+
answer = this.answer(key, full);
|
|
1169
|
+
this.answers.set(key, answer);
|
|
1170
|
+
}
|
|
1171
|
+
return answer;
|
|
1172
|
+
}
|
|
1173
|
+
async answer(key, full) {
|
|
1174
|
+
const entry2 = this.entries.get(key);
|
|
1175
|
+
if (this.read === "cached" && entry2 && await this.current(entry2)) return entry2.value;
|
|
1176
|
+
const readAt = this.clock();
|
|
1177
|
+
const files = /* @__PURE__ */ new Map();
|
|
1178
|
+
const value = await full(async (path) => {
|
|
1179
|
+
files.set(path, await this.stamp(path));
|
|
1180
|
+
});
|
|
1181
|
+
this.entries.set(key, { files, readAt, value });
|
|
1182
|
+
return value;
|
|
1183
|
+
}
|
|
1184
|
+
async current(entry2) {
|
|
1185
|
+
if (this.clock() - entry2.readAt >= this.maxAgeMs) return false;
|
|
1186
|
+
for (const [path, stamp] of entry2.files)
|
|
1187
|
+
if (await this.stamp(path) !== stamp) return false;
|
|
1188
|
+
return true;
|
|
1189
|
+
}
|
|
1190
|
+
stamp(path) {
|
|
1191
|
+
let stamp = this.stamps.get(path);
|
|
1192
|
+
if (!stamp) {
|
|
1193
|
+
stamp = stampOf(path);
|
|
1194
|
+
this.stamps.set(path, stamp);
|
|
1195
|
+
}
|
|
1196
|
+
return stamp;
|
|
1197
|
+
}
|
|
1198
|
+
};
|
|
1199
|
+
|
|
1200
|
+
// src/checkout-roots.ts
|
|
1201
|
+
import { homedir } from "node:os";
|
|
1202
|
+
import { isAbsolute as isAbsolute4, relative as relative2, resolve as resolve2, sep as sep2 } from "node:path";
|
|
1203
|
+
var UNRESOLVED_WORKTREE_ROOT = "<clone>/.tsp/worktrees";
|
|
1204
|
+
function inside(parent, child) {
|
|
1205
|
+
const path = relative2(parent, child);
|
|
1206
|
+
return path === "" || !path.startsWith(`..${sep2}`) && path !== ".." && !isAbsolute4(path);
|
|
1207
|
+
}
|
|
1208
|
+
function decodeWorktreeRoot(value, clone) {
|
|
1209
|
+
if (value === null) return null;
|
|
1210
|
+
if (typeof value !== "string" || !value.trim() || value.includes("\0"))
|
|
1211
|
+
throw new Error("Invalid worktree root");
|
|
1212
|
+
if (value.startsWith("~") && !value.startsWith("~/")) throw new Error("Invalid worktree root");
|
|
1213
|
+
const expanded = value.startsWith("~/") ? resolve2(homedir(), value.slice(2)) : value;
|
|
1214
|
+
if (clone && inside(resolve2(clone, expanded), resolve2(clone)))
|
|
1215
|
+
throw new Error("Worktree root must not contain the clone");
|
|
1216
|
+
if (isAbsolute4(expanded) && resolve2(expanded) === resolve2(expanded, ".."))
|
|
1217
|
+
throw new Error("Worktree root must not contain the clone");
|
|
1218
|
+
if (!clone && !isAbsolute4(expanded) && inside(resolve2("/clone", expanded), "/clone"))
|
|
1219
|
+
throw new Error("Worktree root must not contain the clone");
|
|
1220
|
+
return value;
|
|
1221
|
+
}
|
|
1222
|
+
function worktreeSetting(clone, config) {
|
|
1223
|
+
const registered2 = Object.values(config.repositories).find((row) => row.folder === clone);
|
|
1224
|
+
if (registered2?.worktreeRoot != null) return "repository";
|
|
1225
|
+
return config.worktrees.root != null ? "machine" : null;
|
|
1226
|
+
}
|
|
1227
|
+
function worktreeRoot(clone, config, top = clone) {
|
|
1228
|
+
const registered2 = Object.values(config.repositories).find((row) => row.folder === clone);
|
|
1229
|
+
const configured = registered2?.worktreeRoot ?? config.worktrees.root;
|
|
1230
|
+
decodeWorktreeRoot(configured, clone);
|
|
1231
|
+
const folder = configured === null ? resolve2(top, ".tsp/worktrees") : resolve2(
|
|
1232
|
+
clone,
|
|
1233
|
+
configured.startsWith("~/") ? resolve2(homedir(), configured.slice(2)) : configured
|
|
1234
|
+
);
|
|
1235
|
+
return configured === null ? { folder, location: "in_clone", setting: null } : { folder, location: "custom", setting: worktreeSetting(clone, config) };
|
|
1236
|
+
}
|
|
1237
|
+
function worktreeFolder(root, attemptId) {
|
|
1238
|
+
if (!/^att_[0-9a-f]{12}$/.test(attemptId)) throw new Error("Invalid attempt identity");
|
|
1239
|
+
return resolve2(root.folder, attemptId);
|
|
1240
|
+
}
|
|
1241
|
+
function excludeLine(clone, root) {
|
|
1242
|
+
if (!inside(clone, root.folder)) return null;
|
|
1243
|
+
return root.location === "in_clone" ? "/.tsp/" : `/${relative2(clone, root.folder).split(sep2).map((segment) => segment.replace(/[\\*?[\]]/g, "\\$&")).join("/")}/`;
|
|
1244
|
+
}
|
|
1245
|
+
|
|
1013
1246
|
// src/acp-driver.ts
|
|
1014
1247
|
import { realpath as realpath2 } from "node:fs/promises";
|
|
1015
1248
|
import { homedir as homedir2 } from "node:os";
|
|
@@ -1073,11 +1306,11 @@ function decodeStoredSessions(result) {
|
|
|
1073
1306
|
function heldRequestText(params) {
|
|
1074
1307
|
if (!isObject(params)) return null;
|
|
1075
1308
|
const toolCall = field(params, "toolCall");
|
|
1076
|
-
for (const
|
|
1077
|
-
if (!
|
|
1309
|
+
for (const source of [isObject(toolCall) ? toolCall : null, params]) {
|
|
1310
|
+
if (!source) continue;
|
|
1078
1311
|
for (const key of ["title", "question", "name"]) {
|
|
1079
|
-
const
|
|
1080
|
-
if (typeof
|
|
1312
|
+
const text5 = field(source, key);
|
|
1313
|
+
if (typeof text5 === "string" && text5.trim()) return text5.trim();
|
|
1081
1314
|
}
|
|
1082
1315
|
}
|
|
1083
1316
|
return null;
|
|
@@ -1252,11 +1485,11 @@ var AcpConnection = class _AcpConnection {
|
|
|
1252
1485
|
const error = field(message, "error");
|
|
1253
1486
|
if (isObject(error)) {
|
|
1254
1487
|
const code = field(error, "code");
|
|
1255
|
-
const
|
|
1488
|
+
const text5 = field(error, "message");
|
|
1256
1489
|
request.reject(
|
|
1257
1490
|
new AcpRpcError(
|
|
1258
1491
|
typeof code === "number" ? code : -32603,
|
|
1259
|
-
typeof
|
|
1492
|
+
typeof text5 === "string" ? text5 : "ACP request failed"
|
|
1260
1493
|
)
|
|
1261
1494
|
);
|
|
1262
1495
|
} else request.resolve(field(message, "result"));
|
|
@@ -1353,8 +1586,8 @@ var BETWEEN_UPDATES = /* @__PURE__ */ new Set([
|
|
|
1353
1586
|
]);
|
|
1354
1587
|
function messageText(update) {
|
|
1355
1588
|
const content = field(update, "content");
|
|
1356
|
-
const
|
|
1357
|
-
return typeof
|
|
1589
|
+
const text5 = isObject(content) && field(content, "type") === "text" ? field(content, "text") : null;
|
|
1590
|
+
return typeof text5 === "string" ? text5 : "";
|
|
1358
1591
|
}
|
|
1359
1592
|
var AcpSession = class {
|
|
1360
1593
|
constructor(id, name, folder) {
|
|
@@ -1449,8 +1682,8 @@ var AcpSession = class {
|
|
|
1449
1682
|
}
|
|
1450
1683
|
/** The turn's last words, only for a finished turn, as the owner's canvas shows them. */
|
|
1451
1684
|
lastMessage() {
|
|
1452
|
-
const
|
|
1453
|
-
return this.state === "done" &&
|
|
1685
|
+
const text5 = sanitizeLastMessage(this.said.replace(/\s+/g, " ").trim());
|
|
1686
|
+
return this.state === "done" && text5 ? text5 : void 0;
|
|
1454
1687
|
}
|
|
1455
1688
|
async snapshot(activityRoot) {
|
|
1456
1689
|
return {
|
|
@@ -1468,6 +1701,56 @@ var AcpSession = class {
|
|
|
1468
1701
|
}
|
|
1469
1702
|
};
|
|
1470
1703
|
|
|
1704
|
+
// src/acp-trust.ts
|
|
1705
|
+
var AcpTrust = class {
|
|
1706
|
+
/**
|
|
1707
|
+
* `ask` is the agent's own trust read, which names each file it reads through `watch`.
|
|
1708
|
+
* `revision` counts launch-configuration changes: the agent finds its trust files through
|
|
1709
|
+
* that configuration, so an answer read under one is never reused under another.
|
|
1710
|
+
*/
|
|
1711
|
+
constructor(harness, ask, revision = () => 0, cache = new TrustCache()) {
|
|
1712
|
+
this.harness = harness;
|
|
1713
|
+
this.ask = ask;
|
|
1714
|
+
this.revision = revision;
|
|
1715
|
+
this.cache = cache;
|
|
1716
|
+
this.seen = revision();
|
|
1717
|
+
}
|
|
1718
|
+
seen;
|
|
1719
|
+
/** The folder's trust read now, uncached, as a launch needs it. */
|
|
1720
|
+
read(folder) {
|
|
1721
|
+
return this.ask(folder);
|
|
1722
|
+
}
|
|
1723
|
+
/**
|
|
1724
|
+
* One facts check's reads. A cached answer stands while every file the agent named is
|
|
1725
|
+
* unchanged, and for at most TRUST_CACHE_MAX_AGE_MS, since an agent may decide trust from
|
|
1726
|
+
* something no file stamp shows.
|
|
1727
|
+
*/
|
|
1728
|
+
reads(read) {
|
|
1729
|
+
const revision = this.revision();
|
|
1730
|
+
if (revision !== this.seen) {
|
|
1731
|
+
this.cache.clear();
|
|
1732
|
+
this.seen = revision;
|
|
1733
|
+
}
|
|
1734
|
+
const pass = this.cache.pass(read);
|
|
1735
|
+
return (folder) => pass.get(folder, (watch2) => this.ask(folder, watch2));
|
|
1736
|
+
}
|
|
1737
|
+
/** Each registered folder's trust row in slug order, with what `extra` reads in that folder first. */
|
|
1738
|
+
async rows(folders, read, extra) {
|
|
1739
|
+
const trust = this.reads(read);
|
|
1740
|
+
return Promise.all(
|
|
1741
|
+
Object.entries(folders).sort(([a], [b]) => a.localeCompare(b)).map(async ([slug, folder]) => {
|
|
1742
|
+
const found = await extra(folder);
|
|
1743
|
+
return {
|
|
1744
|
+
harness: this.harness,
|
|
1745
|
+
repository: repositoryFromKey(slug),
|
|
1746
|
+
trust: await trust(folder),
|
|
1747
|
+
...found
|
|
1748
|
+
};
|
|
1749
|
+
})
|
|
1750
|
+
);
|
|
1751
|
+
}
|
|
1752
|
+
};
|
|
1753
|
+
|
|
1471
1754
|
// src/acp-driver.ts
|
|
1472
1755
|
var ACP_TIMEOUTS = { startupMs: 1e4, requestMs: 3e4 };
|
|
1473
1756
|
var CANCEL_GRACE_MS = 2e3;
|
|
@@ -1483,16 +1766,21 @@ var AcpDriver = class {
|
|
|
1483
1766
|
this.executor = executor;
|
|
1484
1767
|
this.timeouts = timeouts;
|
|
1485
1768
|
this.groups = groups;
|
|
1769
|
+
this.trustRecord = new AcpTrust(
|
|
1770
|
+
agent.harness,
|
|
1771
|
+
agent.trust.bind(agent, executor),
|
|
1772
|
+
() => executor.configurationRevision
|
|
1773
|
+
);
|
|
1486
1774
|
this.displayName = agent.displayName;
|
|
1487
|
-
this.
|
|
1488
|
-
this.
|
|
1775
|
+
this.signInStep = agent.signInStep;
|
|
1776
|
+
this.installStep = agent.installStep;
|
|
1489
1777
|
this.unknownTrustAdmits = agent.unknownTrustAdmits;
|
|
1490
1778
|
this.repositorySetup = (folder) => agent.repositorySetup(folder);
|
|
1491
1779
|
this.hold = /* @__PURE__ */ new Set([ACP_PERMISSION_METHOD, ...agent.holdMethods]);
|
|
1492
1780
|
}
|
|
1493
1781
|
displayName;
|
|
1494
|
-
|
|
1495
|
-
|
|
1782
|
+
signInStep;
|
|
1783
|
+
installStep;
|
|
1496
1784
|
unknownTrustAdmits;
|
|
1497
1785
|
repositorySetup;
|
|
1498
1786
|
sessions = /* @__PURE__ */ new Map();
|
|
@@ -1500,6 +1788,7 @@ var AcpDriver = class {
|
|
|
1500
1788
|
hold;
|
|
1501
1789
|
loadSession = false;
|
|
1502
1790
|
probedVersion = null;
|
|
1791
|
+
trustRecord;
|
|
1503
1792
|
/**
|
|
1504
1793
|
* Derived from what the agent advertised at its last probe: Resume needs `session/load`. No
|
|
1505
1794
|
* terminal can join a live ACP session, so `attach_hint` is never advertised: the canvas
|
|
@@ -1555,7 +1844,7 @@ var AcpDriver = class {
|
|
|
1555
1844
|
}
|
|
1556
1845
|
}
|
|
1557
1846
|
async requireTrusted(folder) {
|
|
1558
|
-
if (await this.
|
|
1847
|
+
if (await this.trustRecord.read(folder) !== "trusted")
|
|
1559
1848
|
throw new HarnessRefusal("workspace_untrusted");
|
|
1560
1849
|
}
|
|
1561
1850
|
register(id, name, folder) {
|
|
@@ -1566,14 +1855,14 @@ var AcpDriver = class {
|
|
|
1566
1855
|
return session;
|
|
1567
1856
|
}
|
|
1568
1857
|
/** Sends one prompt without waiting for its turn, which ends through `settle`. */
|
|
1569
|
-
beginTurn(session, connection,
|
|
1858
|
+
beginTurn(session, connection, text5, check) {
|
|
1570
1859
|
check();
|
|
1571
1860
|
session.beginTurn(connection);
|
|
1572
1861
|
connection.onUpdate = (params) => session.observe(params);
|
|
1573
1862
|
connection.onHeldChange = () => session.holding(connection.held);
|
|
1574
1863
|
void connection.request(
|
|
1575
1864
|
"session/prompt",
|
|
1576
|
-
{ sessionId: session.id, prompt: [{ type: "text", text:
|
|
1865
|
+
{ sessionId: session.id, prompt: [{ type: "text", text: text5 }] },
|
|
1577
1866
|
{ timeoutMs: null }
|
|
1578
1867
|
).then((result) => decodeStopReason(result)).then(
|
|
1579
1868
|
(reason) => this.settle(session, connection, reason),
|
|
@@ -1597,7 +1886,7 @@ var AcpDriver = class {
|
|
|
1597
1886
|
);
|
|
1598
1887
|
await connection.close();
|
|
1599
1888
|
}
|
|
1600
|
-
async start(
|
|
1889
|
+
async start(command2, folder, requireLive, checkpoint) {
|
|
1601
1890
|
await this.requireTrusted(folder);
|
|
1602
1891
|
const connection = await this.connect(folder, requireLive);
|
|
1603
1892
|
try {
|
|
@@ -1609,21 +1898,21 @@ var AcpDriver = class {
|
|
|
1609
1898
|
)
|
|
1610
1899
|
);
|
|
1611
1900
|
await checkpoint?.(id);
|
|
1612
|
-
const session = this.register(id,
|
|
1613
|
-
this.beginTurn(session, connection,
|
|
1901
|
+
const session = this.register(id, command2.session_name, folder);
|
|
1902
|
+
this.beginTurn(session, connection, command2.seed, promptCheck(requireLive));
|
|
1614
1903
|
return id;
|
|
1615
1904
|
} catch (error) {
|
|
1616
1905
|
await connection.close();
|
|
1617
1906
|
throw error;
|
|
1618
1907
|
}
|
|
1619
1908
|
}
|
|
1620
|
-
async reconcileLaunch(ref,
|
|
1909
|
+
async reconcileLaunch(ref, command2, folder, requireLive, beforeSeed) {
|
|
1621
1910
|
const known = this.sessions.get(ref);
|
|
1622
1911
|
if (known?.seeded) return known.snapshot();
|
|
1623
1912
|
const connection = await this.connect(folder, requireLive);
|
|
1624
1913
|
try {
|
|
1625
1914
|
if (!connection.capabilities.loadSession) throw new HarnessRefusal("start_uncertain");
|
|
1626
|
-
const session = known ?? this.register(ref,
|
|
1915
|
+
const session = known ?? this.register(ref, command2.session_name, folder);
|
|
1627
1916
|
const seeded = await this.load(session, connection, folder, requireLive);
|
|
1628
1917
|
if (seeded !== false) {
|
|
1629
1918
|
if (seeded) session.endedWithGateway();
|
|
@@ -1634,7 +1923,7 @@ var AcpDriver = class {
|
|
|
1634
1923
|
await this.requireTrusted(folder);
|
|
1635
1924
|
if (!inside(await realpath2(await beforeSeed()), await realpath2(folder)))
|
|
1636
1925
|
throw new HarnessRefusal("start_uncertain");
|
|
1637
|
-
this.beginTurn(session, connection,
|
|
1926
|
+
this.beginTurn(session, connection, command2.seed, promptCheck(requireLive));
|
|
1638
1927
|
return session.snapshot();
|
|
1639
1928
|
} catch (error) {
|
|
1640
1929
|
await connection.close();
|
|
@@ -1726,7 +2015,7 @@ var AcpDriver = class {
|
|
|
1726
2015
|
if (ended === false && running3.connection === connection) running3.finish("stopped");
|
|
1727
2016
|
await connection.close();
|
|
1728
2017
|
}
|
|
1729
|
-
async resume(session,
|
|
2018
|
+
async resume(session, text5, folder, requireLive) {
|
|
1730
2019
|
if (session.state === "working" || session.state === "needs_input")
|
|
1731
2020
|
throw new HarnessRefusal("already_running");
|
|
1732
2021
|
const detected = await this.detect(folder);
|
|
@@ -1736,7 +2025,7 @@ var AcpDriver = class {
|
|
|
1736
2025
|
if (!this.loadSession) throw new HarnessRefusal("unsupported_command");
|
|
1737
2026
|
const record = this.register(session.id, session.name, folder);
|
|
1738
2027
|
if (record.connection) throw new HarnessRefusal("unsupported_command");
|
|
1739
|
-
const prompt =
|
|
2028
|
+
const prompt = text5 ?? (session.state === "stopped" ? RESUME_CONTINUATION : null);
|
|
1740
2029
|
const connection = await this.connect(folder, requireLive);
|
|
1741
2030
|
try {
|
|
1742
2031
|
if (await this.load(record, connection, folder, requireLive) === void 0)
|
|
@@ -1754,19 +2043,14 @@ var AcpDriver = class {
|
|
|
1754
2043
|
attachHint(id) {
|
|
1755
2044
|
return { harness: this.agent.harness, session_ref: id };
|
|
1756
2045
|
}
|
|
1757
|
-
|
|
1758
|
-
return
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
available: detection.available,
|
|
1766
|
-
signed_in: detection.signed_in ?? null
|
|
1767
|
-
};
|
|
1768
|
-
})
|
|
1769
|
-
);
|
|
2046
|
+
repositories(folders, read = "fresh") {
|
|
2047
|
+
return this.trustRecord.rows(folders, read, async (folder) => {
|
|
2048
|
+
const detection = await this.probe(folder);
|
|
2049
|
+
return { available: detection.available, signed_in: detection.signed_in ?? null };
|
|
2050
|
+
});
|
|
2051
|
+
}
|
|
2052
|
+
trust(folders, read) {
|
|
2053
|
+
return this.trustRecord.rows(folders, read, async () => ({}));
|
|
1770
2054
|
}
|
|
1771
2055
|
/** Ends agent process groups a gateway killed outright left behind, once at startup. */
|
|
1772
2056
|
async reap() {
|
|
@@ -1819,10 +2103,10 @@ function decodeVariables(env) {
|
|
|
1819
2103
|
if (!isObject(env)) throw new Error("launch.env must contain string variables");
|
|
1820
2104
|
const variables = {};
|
|
1821
2105
|
for (const name of Object.keys(env)) {
|
|
1822
|
-
const
|
|
1823
|
-
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name) || !text(
|
|
2106
|
+
const entry2 = field(env, name);
|
|
2107
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name) || !text(entry2))
|
|
1824
2108
|
throw new Error("launch.env must contain valid names and NUL-free string values");
|
|
1825
|
-
Object.defineProperty(variables, name, { value:
|
|
2109
|
+
Object.defineProperty(variables, name, { value: entry2, enumerable: true });
|
|
1826
2110
|
}
|
|
1827
2111
|
return variables;
|
|
1828
2112
|
}
|
|
@@ -1857,9 +2141,9 @@ async function privateWrite(path, value) {
|
|
|
1857
2141
|
await rename(temporary, path);
|
|
1858
2142
|
}
|
|
1859
2143
|
async function readConfig(path) {
|
|
1860
|
-
let
|
|
2144
|
+
let text5;
|
|
1861
2145
|
try {
|
|
1862
|
-
|
|
2146
|
+
text5 = await readFile(path, "utf8");
|
|
1863
2147
|
} catch (error) {
|
|
1864
2148
|
if (isObject(error) && field(error, "code") === "ENOENT")
|
|
1865
2149
|
return {
|
|
@@ -1872,7 +2156,7 @@ async function readConfig(path) {
|
|
|
1872
2156
|
};
|
|
1873
2157
|
throw error;
|
|
1874
2158
|
}
|
|
1875
|
-
const value = parseJson(
|
|
2159
|
+
const value = parseJson(text5);
|
|
1876
2160
|
if (!isObject(value) || Object.keys(value).some(
|
|
1877
2161
|
(key) => ![
|
|
1878
2162
|
"repositories",
|
|
@@ -1948,7 +2232,7 @@ async function registerFolder(folder, env = process.env) {
|
|
|
1948
2232
|
}
|
|
1949
2233
|
function repositoryFolders(config) {
|
|
1950
2234
|
return Object.fromEntries(
|
|
1951
|
-
Object.entries(config.repositories).map(([key,
|
|
2235
|
+
Object.entries(config.repositories).map(([key, entry2]) => [key, entry2.folder])
|
|
1952
2236
|
);
|
|
1953
2237
|
}
|
|
1954
2238
|
function decodeRepositories(value, repositories) {
|
|
@@ -1959,18 +2243,18 @@ function decodeRepositories(value, repositories) {
|
|
|
1959
2243
|
const map = {};
|
|
1960
2244
|
let upgraded = worktrees === void 0;
|
|
1961
2245
|
for (const slug of Object.keys(repositories)) {
|
|
1962
|
-
const
|
|
1963
|
-
const folder = typeof
|
|
1964
|
-
if (isObject(
|
|
2246
|
+
const entry2 = field(repositories, slug);
|
|
2247
|
+
const folder = typeof entry2 === "string" ? entry2 : isObject(entry2) ? field(entry2, "folder") : null;
|
|
2248
|
+
if (isObject(entry2) && Object.keys(entry2).some((key) => !["folder", "worktreeRoot"].includes(key)))
|
|
1965
2249
|
throw new Error("Invalid repository registry");
|
|
1966
|
-
upgraded ||= typeof
|
|
2250
|
+
upgraded ||= typeof entry2 === "string";
|
|
1967
2251
|
if (typeof folder !== "string" || !isAbsolute6(folder))
|
|
1968
2252
|
throw new Error("Invalid repository registry");
|
|
1969
2253
|
decodeWorktreeRoot(root, folder);
|
|
1970
2254
|
map[repositoryKey(storedRepository(slug))] = {
|
|
1971
2255
|
folder,
|
|
1972
2256
|
worktreeRoot: decodeWorktreeRoot(
|
|
1973
|
-
isObject(
|
|
2257
|
+
isObject(entry2) ? field(entry2, "worktreeRoot") : null,
|
|
1974
2258
|
folder
|
|
1975
2259
|
)
|
|
1976
2260
|
};
|
|
@@ -2059,7 +2343,7 @@ var AcpGroupLedger = class {
|
|
|
2059
2343
|
};
|
|
2060
2344
|
|
|
2061
2345
|
// src/claude-transcript.ts
|
|
2062
|
-
import { open as open2, readdir, stat as
|
|
2346
|
+
import { open as open2, readdir, stat as stat3 } from "node:fs/promises";
|
|
2063
2347
|
import { join as join4 } from "node:path";
|
|
2064
2348
|
var TRANSCRIPT_READ_BYTES = 1 << 20;
|
|
2065
2349
|
var LINE_LIMIT_BYTES = 8 << 20;
|
|
@@ -2116,7 +2400,7 @@ var ClaudeTranscripts = class {
|
|
|
2116
2400
|
if (!cursor.path) return null;
|
|
2117
2401
|
let size;
|
|
2118
2402
|
try {
|
|
2119
|
-
size = (await
|
|
2403
|
+
size = (await stat3(cursor.path)).size;
|
|
2120
2404
|
} catch {
|
|
2121
2405
|
this.cursors.set(conversationId, this.fresh(null));
|
|
2122
2406
|
return null;
|
|
@@ -2147,9 +2431,9 @@ var ClaudeTranscripts = class {
|
|
|
2147
2431
|
if (await exists(expected)) return expected;
|
|
2148
2432
|
if (this.now() - cursor.searchedAt < SEARCH_INTERVAL_MS) return null;
|
|
2149
2433
|
cursor.searchedAt = this.now();
|
|
2150
|
-
for (const
|
|
2151
|
-
if (!
|
|
2152
|
-
const candidate = join4(projects,
|
|
2434
|
+
for (const entry2 of await readdir(projects, { withFileTypes: true })) {
|
|
2435
|
+
if (!entry2.isDirectory()) continue;
|
|
2436
|
+
const candidate = join4(projects, entry2.name, file);
|
|
2153
2437
|
if (await exists(candidate)) return candidate;
|
|
2154
2438
|
}
|
|
2155
2439
|
return null;
|
|
@@ -2177,9 +2461,9 @@ var ClaudeTranscripts = class {
|
|
|
2177
2461
|
cursor.remainder = data.length > LINE_LIMIT_BYTES ? Buffer.alloc(0) : Buffer.from(data);
|
|
2178
2462
|
}
|
|
2179
2463
|
entry(conversationId, tally, line) {
|
|
2180
|
-
const
|
|
2181
|
-
if (!
|
|
2182
|
-
const { role, content, at } =
|
|
2464
|
+
const entry2 = parseEntry(line);
|
|
2465
|
+
if (!entry2) return;
|
|
2466
|
+
const { role, content, at } = entry2;
|
|
2183
2467
|
if (typeof content === "string") {
|
|
2184
2468
|
if (role === "user") {
|
|
2185
2469
|
tally.stepped(at);
|
|
@@ -2251,7 +2535,7 @@ function toolResult(tally, edits, block, at) {
|
|
|
2251
2535
|
}
|
|
2252
2536
|
async function exists(path) {
|
|
2253
2537
|
try {
|
|
2254
|
-
return (await
|
|
2538
|
+
return (await stat3(path)).isFile();
|
|
2255
2539
|
} catch {
|
|
2256
2540
|
return false;
|
|
2257
2541
|
}
|
|
@@ -2263,22 +2547,27 @@ import { homedir as homedir4 } from "node:os";
|
|
|
2263
2547
|
import { join as join5 } from "node:path";
|
|
2264
2548
|
var MAX_CONFIG_BYTES = 4 * 1024 * 1024;
|
|
2265
2549
|
var ClaudeTrust = class {
|
|
2266
|
-
constructor(environment = process.env, home = homedir4()) {
|
|
2550
|
+
constructor(environment = process.env, home = homedir4(), cache = new TrustCache()) {
|
|
2267
2551
|
this.environment = environment;
|
|
2268
2552
|
this.home = home;
|
|
2553
|
+
this.cache = cache;
|
|
2269
2554
|
}
|
|
2270
2555
|
observed = /* @__PURE__ */ new Map();
|
|
2271
2556
|
paths = /* @__PURE__ */ new Map();
|
|
2272
2557
|
mark(folder, trust) {
|
|
2273
2558
|
this.observed.set(folder, trust);
|
|
2274
2559
|
}
|
|
2275
|
-
async
|
|
2560
|
+
async path(folder) {
|
|
2561
|
+
const env = typeof this.environment === "function" ? await this.environment(folder) : this.environment;
|
|
2562
|
+
const path = join5(env["CLAUDE_CONFIG_DIR"] ?? env["HOME"] ?? this.home, ".claude.json");
|
|
2563
|
+
const previous = this.paths.get(folder);
|
|
2564
|
+
if (previous !== void 0 && path !== previous) this.observed.delete(folder);
|
|
2565
|
+
this.paths.set(folder, path);
|
|
2566
|
+
return path;
|
|
2567
|
+
}
|
|
2568
|
+
async projects(path, watch2) {
|
|
2276
2569
|
try {
|
|
2277
|
-
|
|
2278
|
-
const path = join5(env["CLAUDE_CONFIG_DIR"] ?? env["HOME"] ?? this.home, ".claude.json");
|
|
2279
|
-
const previous = this.paths.get(folder);
|
|
2280
|
-
if (previous !== void 0 && path !== previous) this.observed.delete(folder);
|
|
2281
|
-
this.paths.set(folder, path);
|
|
2570
|
+
await watch2(path);
|
|
2282
2571
|
const handle = await open3(path, "r");
|
|
2283
2572
|
try {
|
|
2284
2573
|
const info = await handle.stat();
|
|
@@ -2306,10 +2595,17 @@ var ClaudeTrust = class {
|
|
|
2306
2595
|
return null;
|
|
2307
2596
|
}
|
|
2308
2597
|
}
|
|
2309
|
-
async repositories(registry) {
|
|
2598
|
+
async repositories(registry, read = "fresh") {
|
|
2599
|
+
const pass = this.cache.pass(read);
|
|
2310
2600
|
return Promise.all(
|
|
2311
2601
|
Object.entries(registry).sort(([a], [b]) => a.localeCompare(b)).map(async ([slug, folder]) => {
|
|
2312
|
-
|
|
2602
|
+
let projects;
|
|
2603
|
+
try {
|
|
2604
|
+
const path = await this.path(folder);
|
|
2605
|
+
projects = await pass.get(path, (watch2) => this.projects(path, watch2));
|
|
2606
|
+
} catch {
|
|
2607
|
+
projects = null;
|
|
2608
|
+
}
|
|
2313
2609
|
const project = projects && Object.hasOwn(projects, folder) ? field(projects, folder) : null;
|
|
2314
2610
|
const accepted = isObject(project) ? field(project, "hasTrustDialogAccepted") : null;
|
|
2315
2611
|
if (typeof accepted === "boolean") this.observed.delete(folder);
|
|
@@ -2342,14 +2638,20 @@ var HarnessExecutor = class {
|
|
|
2342
2638
|
home;
|
|
2343
2639
|
captures = /* @__PURE__ */ new Map();
|
|
2344
2640
|
revision;
|
|
2641
|
+
invalidations = 0;
|
|
2345
2642
|
lastPath;
|
|
2346
2643
|
/** The most recent successful capture in any folder, for a folder with none of its own. */
|
|
2347
2644
|
latest;
|
|
2645
|
+
/** Counts launch-configuration changes, so nothing read under one configuration is reused under another. */
|
|
2646
|
+
get configurationRevision() {
|
|
2647
|
+
return this.invalidations;
|
|
2648
|
+
}
|
|
2348
2649
|
invalidate() {
|
|
2349
2650
|
this.captures.clear();
|
|
2350
2651
|
this.latest = void 0;
|
|
2351
2652
|
this.revision = void 0;
|
|
2352
2653
|
this.lastPath = void 0;
|
|
2654
|
+
this.invalidations += 1;
|
|
2353
2655
|
}
|
|
2354
2656
|
async configuration() {
|
|
2355
2657
|
const config = await this.options.config?.() ?? defaultLaunchConfig();
|
|
@@ -2581,7 +2883,7 @@ function slowShell(error) {
|
|
|
2581
2883
|
}
|
|
2582
2884
|
|
|
2583
2885
|
// src/claude-driver.ts
|
|
2584
|
-
import { stat as
|
|
2886
|
+
import { stat as stat4 } from "node:fs/promises";
|
|
2585
2887
|
import { setTimeout as delay2 } from "node:timers/promises";
|
|
2586
2888
|
|
|
2587
2889
|
// src/claude-config.ts
|
|
@@ -2598,9 +2900,9 @@ var ClaudeDriver = class {
|
|
|
2598
2900
|
this.executable = executable2;
|
|
2599
2901
|
this.executor = executor;
|
|
2600
2902
|
this.transcripts = transcripts;
|
|
2601
|
-
this.
|
|
2903
|
+
this.trustRecord = trust ?? new ClaudeTrust((folder) => this.executor.environment(false, folder));
|
|
2602
2904
|
}
|
|
2603
|
-
|
|
2905
|
+
trustRecord;
|
|
2604
2906
|
repositorySetup(folder) {
|
|
2605
2907
|
return {
|
|
2606
2908
|
instruction: "Run claude in the repository's folder once and accept its trust prompt.",
|
|
@@ -2608,8 +2910,11 @@ var ClaudeDriver = class {
|
|
|
2608
2910
|
};
|
|
2609
2911
|
}
|
|
2610
2912
|
displayName = "Claude Code";
|
|
2611
|
-
|
|
2612
|
-
|
|
2913
|
+
signInStep = { instruction: "Run claude and sign in.", command: "claude" };
|
|
2914
|
+
installStep = {
|
|
2915
|
+
instruction: "Install it, then run connect again.",
|
|
2916
|
+
link: "https://code.claude.com/docs/en/setup"
|
|
2917
|
+
};
|
|
2613
2918
|
unknownTrustAdmits = true;
|
|
2614
2919
|
capabilities = {
|
|
2615
2920
|
start: true,
|
|
@@ -2656,28 +2961,34 @@ var ClaudeDriver = class {
|
|
|
2656
2961
|
};
|
|
2657
2962
|
}
|
|
2658
2963
|
}
|
|
2659
|
-
async start(
|
|
2964
|
+
async start(command2, folder, requireLive) {
|
|
2660
2965
|
const seeding = promptCheck(requireLive);
|
|
2661
2966
|
seeding();
|
|
2662
2967
|
const result = await this.call(
|
|
2663
|
-
["--bg", "--name",
|
|
2968
|
+
["--bg", "--name", command2.session_name, "--", command2.seed],
|
|
2664
2969
|
folder,
|
|
2665
2970
|
"launch",
|
|
2666
2971
|
seeding
|
|
2667
2972
|
);
|
|
2973
|
+
const id = /backgrounded\s*·\s*([A-Za-z0-9_-]+)(?=\s*(?:·|$))/m.exec(result.stdout)?.[1];
|
|
2974
|
+
if (id) {
|
|
2975
|
+
const sessionId = opaque(id);
|
|
2976
|
+
this.trustRecord.mark(folder, "trusted");
|
|
2977
|
+
if (result.code !== 0)
|
|
2978
|
+
console.error(
|
|
2979
|
+
`Claude Code background launch exited with code ${result.code}; tracking returned session`
|
|
2980
|
+
);
|
|
2981
|
+
return sessionId;
|
|
2982
|
+
}
|
|
2668
2983
|
if (result.code !== 0) {
|
|
2669
2984
|
if (/Workspace not trusted/i.test(result.stdout + result.stderr)) {
|
|
2670
|
-
this.
|
|
2985
|
+
this.trustRecord.mark(folder, "untrusted");
|
|
2671
2986
|
throw new HarnessRefusal("workspace_untrusted");
|
|
2672
2987
|
}
|
|
2673
2988
|
if (await this.signedIn(folder) === false) throw new HarnessRefusal("not_signed_in");
|
|
2674
2989
|
throw new HarnessRefusal("harness_exited");
|
|
2675
2990
|
}
|
|
2676
|
-
|
|
2677
|
-
if (!id) throw new Error("Uncertain launch result; inspect local harness");
|
|
2678
|
-
const sessionId = opaque(id);
|
|
2679
|
-
this.trust.mark(folder, "trusted");
|
|
2680
|
-
return sessionId;
|
|
2991
|
+
throw new Error("Uncertain launch result; inspect local harness");
|
|
2681
2992
|
}
|
|
2682
2993
|
async list(folder, _requireLive, query) {
|
|
2683
2994
|
return (await this.readSessions(folder, query, false)).map((row) => row.session);
|
|
@@ -2687,7 +2998,7 @@ var ClaudeDriver = class {
|
|
|
2687
2998
|
const fallback = [];
|
|
2688
2999
|
for (const folder of new Set(folders)) {
|
|
2689
3000
|
try {
|
|
2690
|
-
if (!(await
|
|
3001
|
+
if (!(await stat4(folder)).isDirectory())
|
|
2691
3002
|
throw new Error("Session folder unavailable");
|
|
2692
3003
|
const env = await this.executor.environment(false, folder);
|
|
2693
3004
|
const configDir = claudeConfigDirectory(env);
|
|
@@ -2789,7 +3100,7 @@ var ClaudeDriver = class {
|
|
|
2789
3100
|
);
|
|
2790
3101
|
if (result.code !== 0) throw new HarnessRefusal("stop_failed");
|
|
2791
3102
|
}
|
|
2792
|
-
async resume(session,
|
|
3103
|
+
async resume(session, text5, folder, requireLive) {
|
|
2793
3104
|
if (session.state === "working" || session.state === "needs_input")
|
|
2794
3105
|
throw new HarnessRefusal("already_running");
|
|
2795
3106
|
if (session.pid !== null) {
|
|
@@ -2803,7 +3114,7 @@ var ClaudeDriver = class {
|
|
|
2803
3114
|
}
|
|
2804
3115
|
await this.executor.environment(true, folder);
|
|
2805
3116
|
if (await this.signedIn(folder) === false) throw new HarnessRefusal("not_signed_in");
|
|
2806
|
-
const prompt =
|
|
3117
|
+
const prompt = text5 ?? (session.state === "stopped" ? RESUME_CONTINUATION : null);
|
|
2807
3118
|
const resuming = promptCheck(requireLive);
|
|
2808
3119
|
resuming();
|
|
2809
3120
|
const result = await this.call(
|
|
@@ -2828,7 +3139,10 @@ var ClaudeDriver = class {
|
|
|
2828
3139
|
attachHint(id) {
|
|
2829
3140
|
return { harness: HARNESS_KIND.CLAUDE_CODE, session_ref: opaque(id) };
|
|
2830
3141
|
}
|
|
2831
|
-
|
|
3142
|
+
trust(folders, read) {
|
|
3143
|
+
return this.trustRecord.repositories(folders, read);
|
|
3144
|
+
}
|
|
3145
|
+
async repositories(folders, read = "fresh") {
|
|
2832
3146
|
const readiness = new Map(
|
|
2833
3147
|
await Promise.all(
|
|
2834
3148
|
[...new Set(Object.values(folders))].map(async (folder) => {
|
|
@@ -2847,7 +3161,7 @@ var ClaudeDriver = class {
|
|
|
2847
3161
|
})
|
|
2848
3162
|
)
|
|
2849
3163
|
);
|
|
2850
|
-
return (await this.
|
|
3164
|
+
return (await this.trustRecord.repositories(folders, read)).map((row) => {
|
|
2851
3165
|
const detection = readiness.get(folders[repositoryKey(row.repository)]);
|
|
2852
3166
|
return {
|
|
2853
3167
|
...row,
|
|
@@ -2962,8 +3276,8 @@ function requestWords(params) {
|
|
|
2962
3276
|
function lastAgentMessage(page2) {
|
|
2963
3277
|
const data = isObject(page2) ? field(page2, "data") : null;
|
|
2964
3278
|
if (!Array.isArray(data)) return null;
|
|
2965
|
-
for (const
|
|
2966
|
-
const item = isObject(
|
|
3279
|
+
for (const entry2 of data) {
|
|
3280
|
+
const item = isObject(entry2) ? field(entry2, "item") : null;
|
|
2967
3281
|
if (isObject(item) && field(item, "type") === "agentMessage") {
|
|
2968
3282
|
const said2 = text3(field(item, "text"));
|
|
2969
3283
|
if (said2) return said2;
|
|
@@ -3060,14 +3374,14 @@ var CodexActivity = class {
|
|
|
3060
3374
|
throw new Error("Unrecognised Codex item page");
|
|
3061
3375
|
let reachedSettled = false;
|
|
3062
3376
|
for (const raw of field(response, "data")) {
|
|
3063
|
-
const
|
|
3064
|
-
if (!
|
|
3065
|
-
if (thread.settled.has(
|
|
3377
|
+
const entry2 = entryOf(raw);
|
|
3378
|
+
if (!entry2) continue;
|
|
3379
|
+
if (thread.settled.has(entry2.id)) {
|
|
3066
3380
|
reachedSettled = true;
|
|
3067
3381
|
continue;
|
|
3068
3382
|
}
|
|
3069
|
-
unseen.delete(
|
|
3070
|
-
entries.push(
|
|
3383
|
+
unseen.delete(entry2.id);
|
|
3384
|
+
entries.push(entry2);
|
|
3071
3385
|
}
|
|
3072
3386
|
const next = field(response, "nextCursor");
|
|
3073
3387
|
cursor = typeof next === "string" && next ? next : null;
|
|
@@ -3078,7 +3392,7 @@ var CodexActivity = class {
|
|
|
3078
3392
|
thread.commands.delete(id);
|
|
3079
3393
|
thread.tally.finished(id);
|
|
3080
3394
|
}
|
|
3081
|
-
for (const
|
|
3395
|
+
for (const entry2 of entries.reverse()) this.apply(thread, entry2);
|
|
3082
3396
|
return thread.tally.snapshot(root);
|
|
3083
3397
|
}
|
|
3084
3398
|
apply(thread, { id, item, status, at, completed }) {
|
|
@@ -3122,13 +3436,40 @@ import { homedir as homedir7 } from "node:os";
|
|
|
3122
3436
|
import { basename as basename3, dirname as dirname3, join as join7, resolve as resolve5 } from "node:path";
|
|
3123
3437
|
import { parseToml } from "./vendor.js";
|
|
3124
3438
|
var MAX_CONFIG_BYTES2 = 4 * 1024 * 1024;
|
|
3439
|
+
var configPath = (env) => join7(env["CODEX_HOME"] ?? join7(env["HOME"] ?? homedir7(), ".codex"), "config.toml");
|
|
3440
|
+
var IGNORE = async () => {
|
|
3441
|
+
};
|
|
3125
3442
|
var CodexTrust = class {
|
|
3126
|
-
constructor(executor) {
|
|
3443
|
+
constructor(executor, cache = new TrustCache()) {
|
|
3127
3444
|
this.executor = executor;
|
|
3445
|
+
this.cache = cache;
|
|
3446
|
+
}
|
|
3447
|
+
/** The folder's trust read now, uncached, as a launch needs it. */
|
|
3448
|
+
read(folder) {
|
|
3449
|
+
return this.full(folder, IGNORE);
|
|
3128
3450
|
}
|
|
3129
|
-
|
|
3451
|
+
/**
|
|
3452
|
+
* One facts check's reads. A cached answer stands while `config.toml` is unchanged, which
|
|
3453
|
+
* skips the three git commands too: a folder that becomes a repository, or a worktree that
|
|
3454
|
+
* moves, under an unchanged config is seen at the next fresh read (a recheck, a reconnect)
|
|
3455
|
+
* or once the answer reaches TRUST_CACHE_MAX_AGE_MS.
|
|
3456
|
+
*/
|
|
3457
|
+
reads(read) {
|
|
3458
|
+
const pass = this.cache.pass(read);
|
|
3459
|
+
return async (folder) => {
|
|
3460
|
+
try {
|
|
3461
|
+
const path = configPath(await this.executor.environment(false, folder));
|
|
3462
|
+
return await pass.get(`${folder}\0${path}`, (watch2) => this.full(folder, watch2));
|
|
3463
|
+
} catch {
|
|
3464
|
+
return "unknown";
|
|
3465
|
+
}
|
|
3466
|
+
};
|
|
3467
|
+
}
|
|
3468
|
+
async full(folder, watch2) {
|
|
3130
3469
|
try {
|
|
3131
3470
|
const env = await this.executor.environment(false, folder);
|
|
3471
|
+
const path = configPath(env);
|
|
3472
|
+
await watch2(path);
|
|
3132
3473
|
const root = await this.executor.execute(
|
|
3133
3474
|
"git",
|
|
3134
3475
|
["rev-parse", "--show-toplevel"],
|
|
@@ -3148,17 +3489,13 @@ var CodexTrust = class {
|
|
|
3148
3489
|
folder
|
|
3149
3490
|
);
|
|
3150
3491
|
if (root.code !== 0 || prefix.code !== 0) return "unknown";
|
|
3151
|
-
const
|
|
3492
|
+
const canonical3 = await realpath4(root.stdout.trim());
|
|
3152
3493
|
const commonDir = common.code === 0 ? common.stdout.trim() : "";
|
|
3153
|
-
const main = basename3(commonDir) === ".git" ? await realpath4(dirname3(commonDir)) :
|
|
3494
|
+
const main = basename3(commonDir) === ".git" ? await realpath4(dirname3(commonDir)) : canonical3;
|
|
3154
3495
|
let given = resolve5(folder);
|
|
3155
3496
|
for (const part of prefix.stdout.trim().split("/").filter(Boolean)) {
|
|
3156
3497
|
if (part) given = dirname3(given);
|
|
3157
3498
|
}
|
|
3158
|
-
const path = join7(
|
|
3159
|
-
env["CODEX_HOME"] ?? join7(env["HOME"] ?? homedir7(), ".codex"),
|
|
3160
|
-
"config.toml"
|
|
3161
|
-
);
|
|
3162
3499
|
const file = await open4(path, "r");
|
|
3163
3500
|
let contents;
|
|
3164
3501
|
try {
|
|
@@ -3182,7 +3519,7 @@ var CodexTrust = class {
|
|
|
3182
3519
|
}
|
|
3183
3520
|
const projects = parseToml(contents)["projects"];
|
|
3184
3521
|
if (!isObject(projects)) return "unknown";
|
|
3185
|
-
for (const spelling of /* @__PURE__ */ new Set([
|
|
3522
|
+
for (const spelling of /* @__PURE__ */ new Set([canonical3, given, main])) {
|
|
3186
3523
|
const project = Object.hasOwn(projects, spelling) ? field(projects, spelling) : null;
|
|
3187
3524
|
const trust = isObject(project) ? field(project, "trust_level") : null;
|
|
3188
3525
|
if (trust === "trusted" || trust === "untrusted") return trust;
|
|
@@ -3461,12 +3798,12 @@ async function* threads(server, folder, query) {
|
|
|
3461
3798
|
sourceKinds: ["cli", "vscode", "appServer"]
|
|
3462
3799
|
})
|
|
3463
3800
|
) : loadedThreadPage(await server.request(method, { cursor, limit: 100 }));
|
|
3464
|
-
for (const
|
|
3465
|
-
const id = typeof
|
|
3801
|
+
for (const entry2 of page2.threads) {
|
|
3802
|
+
const id = typeof entry2 === "string" ? entry2 : entry2.id;
|
|
3466
3803
|
if (seen.has(id)) continue;
|
|
3467
3804
|
seen.add(id);
|
|
3468
|
-
if (typeof
|
|
3469
|
-
yield
|
|
3805
|
+
if (typeof entry2 !== "string") {
|
|
3806
|
+
yield entry2;
|
|
3470
3807
|
continue;
|
|
3471
3808
|
}
|
|
3472
3809
|
try {
|
|
@@ -3536,9 +3873,9 @@ async function listCodexSessions(server, folder, query, activity) {
|
|
|
3536
3873
|
// src/codex-driver.ts
|
|
3537
3874
|
var MINIMUM_CODEX_VERSION = "0.162.1";
|
|
3538
3875
|
function supportedCodexVersion(version) {
|
|
3539
|
-
const
|
|
3876
|
+
const parts2 = (text5) => (text5.split(/[-+]/)[0] ?? "").split(".").map(Number);
|
|
3540
3877
|
if (!version) return true;
|
|
3541
|
-
const found =
|
|
3878
|
+
const found = parts2(version), minimum = parts2(MINIMUM_CODEX_VERSION);
|
|
3542
3879
|
for (const [index, part] of minimum.entries()) {
|
|
3543
3880
|
const value = found[index] ?? 0;
|
|
3544
3881
|
if (value !== part) return value > part;
|
|
@@ -3557,18 +3894,18 @@ async function discardThread(server, threadId) {
|
|
|
3557
3894
|
} catch {
|
|
3558
3895
|
}
|
|
3559
3896
|
}
|
|
3560
|
-
async function seedThread(server, thread,
|
|
3561
|
-
if (thread.name !==
|
|
3897
|
+
async function seedThread(server, thread, command2, requireLive) {
|
|
3898
|
+
if (thread.name !== command2.session_name)
|
|
3562
3899
|
await server.request(
|
|
3563
3900
|
"thread/name/set",
|
|
3564
|
-
{ threadId: thread.id, name:
|
|
3901
|
+
{ threadId: thread.id, name: command2.session_name },
|
|
3565
3902
|
requireLive
|
|
3566
3903
|
);
|
|
3567
3904
|
await server.request(
|
|
3568
3905
|
"turn/start",
|
|
3569
3906
|
{
|
|
3570
3907
|
threadId: thread.id,
|
|
3571
|
-
input: [{ type: "text", text:
|
|
3908
|
+
input: [{ type: "text", text: command2.seed, text_elements: [] }]
|
|
3572
3909
|
},
|
|
3573
3910
|
promptCheck(requireLive)
|
|
3574
3911
|
);
|
|
@@ -3577,7 +3914,7 @@ var CodexDriver = class {
|
|
|
3577
3914
|
constructor(env, executable2 = "codex", executor = new HarnessExecutor(env), timeouts = {}) {
|
|
3578
3915
|
this.executable = executable2;
|
|
3579
3916
|
this.executor = executor;
|
|
3580
|
-
this.
|
|
3917
|
+
this.trustRecord = new CodexTrust(executor);
|
|
3581
3918
|
this.timeouts = { ...CODEX_TIMEOUTS, ...timeouts };
|
|
3582
3919
|
}
|
|
3583
3920
|
repositorySetup(folder) {
|
|
@@ -3587,8 +3924,11 @@ var CodexDriver = class {
|
|
|
3587
3924
|
};
|
|
3588
3925
|
}
|
|
3589
3926
|
displayName = "Codex";
|
|
3590
|
-
|
|
3591
|
-
|
|
3927
|
+
signInStep = { instruction: "Run codex login and sign in.", command: "codex login" };
|
|
3928
|
+
installStep = {
|
|
3929
|
+
instruction: `Install Codex ${MINIMUM_CODEX_VERSION} or newer, then run connect again.`,
|
|
3930
|
+
link: "https://developers.openai.com/codex/cli"
|
|
3931
|
+
};
|
|
3592
3932
|
/** Codex refuses a folder it has no trust record for, so unknown trust is a step to take. */
|
|
3593
3933
|
unknownTrustAdmits = false;
|
|
3594
3934
|
capabilities = {
|
|
@@ -3599,7 +3939,7 @@ var CodexDriver = class {
|
|
|
3599
3939
|
answer_input: false,
|
|
3600
3940
|
stop_starting: true
|
|
3601
3941
|
};
|
|
3602
|
-
|
|
3942
|
+
trustRecord;
|
|
3603
3943
|
activity = new CodexActivity();
|
|
3604
3944
|
timeouts;
|
|
3605
3945
|
async detect(folder) {
|
|
@@ -3630,15 +3970,15 @@ var CodexDriver = class {
|
|
|
3630
3970
|
"read",
|
|
3631
3971
|
folder
|
|
3632
3972
|
);
|
|
3633
|
-
const
|
|
3634
|
-
detection.signed_in = /not logged in/i.test(
|
|
3973
|
+
const text5 = login.stdout + login.stderr;
|
|
3974
|
+
detection.signed_in = /not logged in/i.test(text5) ? false : login.code === 0 && /logged in/i.test(text5) ? true : null;
|
|
3635
3975
|
}
|
|
3636
3976
|
} catch {
|
|
3637
3977
|
}
|
|
3638
3978
|
return detection;
|
|
3639
3979
|
}
|
|
3640
3980
|
async requireTrusted(folder) {
|
|
3641
|
-
if (await this.
|
|
3981
|
+
if (await this.trustRecord.read(folder) !== "trusted")
|
|
3642
3982
|
throw new HarnessRefusal("workspace_untrusted");
|
|
3643
3983
|
}
|
|
3644
3984
|
async open(folder, requireLive) {
|
|
@@ -3688,7 +4028,7 @@ var CodexDriver = class {
|
|
|
3688
4028
|
await server.close();
|
|
3689
4029
|
}
|
|
3690
4030
|
}
|
|
3691
|
-
async start(
|
|
4031
|
+
async start(command2, folder, requireLive, checkpoint) {
|
|
3692
4032
|
await this.requireTrusted(folder);
|
|
3693
4033
|
return this.connection(
|
|
3694
4034
|
folder,
|
|
@@ -3699,7 +4039,7 @@ var CodexDriver = class {
|
|
|
3699
4039
|
);
|
|
3700
4040
|
await checkpoint?.(thread.id);
|
|
3701
4041
|
try {
|
|
3702
|
-
await seedThread(server, thread,
|
|
4042
|
+
await seedThread(server, thread, command2, requireLive);
|
|
3703
4043
|
} catch (error) {
|
|
3704
4044
|
if (error instanceof HarnessRefusal) await discardThread(server, thread.id);
|
|
3705
4045
|
throw error;
|
|
@@ -3709,7 +4049,7 @@ var CodexDriver = class {
|
|
|
3709
4049
|
requireLive
|
|
3710
4050
|
);
|
|
3711
4051
|
}
|
|
3712
|
-
async reconcileLaunch(ref,
|
|
4052
|
+
async reconcileLaunch(ref, command2, folder, requireLive, beforeSeed) {
|
|
3713
4053
|
return this.connection(
|
|
3714
4054
|
folder,
|
|
3715
4055
|
async (server) => {
|
|
@@ -3742,7 +4082,7 @@ var CodexDriver = class {
|
|
|
3742
4082
|
requireLive.prompt?.();
|
|
3743
4083
|
if (!await inside2(await realpath6(await beforeSeed()), thread.cwd))
|
|
3744
4084
|
throw new HarnessRefusal("start_uncertain");
|
|
3745
|
-
await seedThread(server, thread,
|
|
4085
|
+
await seedThread(server, thread, command2, requireLive);
|
|
3746
4086
|
return { ...sessionFromThread(thread), state: "starting", waitingFor: null };
|
|
3747
4087
|
},
|
|
3748
4088
|
requireLive
|
|
@@ -3753,10 +4093,10 @@ var CodexDriver = class {
|
|
|
3753
4093
|
const turn = await this.connection(folder, (server) => lastTurn(server, session.id));
|
|
3754
4094
|
return turn ? [turn.id] : [];
|
|
3755
4095
|
},
|
|
3756
|
-
reconcile: async (session,
|
|
4096
|
+
reconcile: async (session, text5, observedRefs, folder, requireLive) => {
|
|
3757
4097
|
const turn = await this.connection(folder, (server) => lastTurn(server, session.id));
|
|
3758
4098
|
if (turn && !observedRefs.includes(turn.id)) return session.id;
|
|
3759
|
-
return this.resume(session,
|
|
4099
|
+
return this.resume(session, text5, folder, requireLive);
|
|
3760
4100
|
}
|
|
3761
4101
|
};
|
|
3762
4102
|
async list(folder, requireLive, query) {
|
|
@@ -3781,7 +4121,7 @@ var CodexDriver = class {
|
|
|
3781
4121
|
requireLive
|
|
3782
4122
|
);
|
|
3783
4123
|
}
|
|
3784
|
-
async resume(session,
|
|
4124
|
+
async resume(session, text5, folder, requireLive) {
|
|
3785
4125
|
if (session.state === "working" || session.state === "needs_input")
|
|
3786
4126
|
throw new HarnessRefusal("already_running");
|
|
3787
4127
|
const detected = await this.detect(folder);
|
|
@@ -3797,8 +4137,8 @@ var CodexDriver = class {
|
|
|
3797
4137
|
{ threadId: id, excludeTurns: true },
|
|
3798
4138
|
requireLive
|
|
3799
4139
|
);
|
|
3800
|
-
const turn =
|
|
3801
|
-
const prompt =
|
|
4140
|
+
const turn = text5 === null ? await lastTurn(server, id) : null;
|
|
4141
|
+
const prompt = text5 ?? (turn?.status === "interrupted" ? RESUME_CONTINUATION : null);
|
|
3802
4142
|
if (prompt !== null)
|
|
3803
4143
|
await server.request(
|
|
3804
4144
|
"turn/start",
|
|
@@ -3816,20 +4156,31 @@ var CodexDriver = class {
|
|
|
3816
4156
|
attachHint(id) {
|
|
3817
4157
|
return { harness: HARNESS_KIND.CODEX, session_ref: codexId(id) };
|
|
3818
4158
|
}
|
|
3819
|
-
async repositories(folders) {
|
|
4159
|
+
async repositories(folders, read = "fresh") {
|
|
4160
|
+
const trust = this.trustRecord.reads(read);
|
|
3820
4161
|
return Promise.all(
|
|
3821
4162
|
Object.entries(folders).sort(([a], [b]) => a.localeCompare(b)).map(async ([slug, folder]) => {
|
|
3822
4163
|
const detection = await this.probe(folder);
|
|
3823
4164
|
return {
|
|
3824
4165
|
harness: HARNESS_KIND.CODEX,
|
|
3825
4166
|
repository: repositoryFromKey(slug),
|
|
3826
|
-
trust: await
|
|
4167
|
+
trust: await trust(folder),
|
|
3827
4168
|
available: detection.available,
|
|
3828
4169
|
signed_in: detection.signed_in ?? null
|
|
3829
4170
|
};
|
|
3830
4171
|
})
|
|
3831
4172
|
);
|
|
3832
4173
|
}
|
|
4174
|
+
async trust(folders, read) {
|
|
4175
|
+
const trust = this.trustRecord.reads(read);
|
|
4176
|
+
return Promise.all(
|
|
4177
|
+
Object.entries(folders).sort(([a], [b]) => a.localeCompare(b)).map(async ([slug, folder]) => ({
|
|
4178
|
+
harness: HARNESS_KIND.CODEX,
|
|
4179
|
+
repository: repositoryFromKey(slug),
|
|
4180
|
+
trust: await trust(folder)
|
|
4181
|
+
}))
|
|
4182
|
+
);
|
|
4183
|
+
}
|
|
3833
4184
|
};
|
|
3834
4185
|
|
|
3835
4186
|
// src/cursor-agent.ts
|
|
@@ -3839,9 +4190,9 @@ import { basename as basename4, dirname as dirname4, join as join8 } from "node:
|
|
|
3839
4190
|
var MINIMUM_CURSOR_VERSION = "2026.10.19";
|
|
3840
4191
|
var MAX_RECORD_BYTES = 64 * 1024;
|
|
3841
4192
|
function supportedVersion(version) {
|
|
3842
|
-
const
|
|
4193
|
+
const parts2 = (text5) => (text5.split(/[-+]/)[0] ?? "").split(".").map(Number);
|
|
3843
4194
|
if (!version) return true;
|
|
3844
|
-
const found =
|
|
4195
|
+
const found = parts2(version), minimum = parts2(MINIMUM_CURSOR_VERSION);
|
|
3845
4196
|
for (const [index, part] of minimum.entries()) {
|
|
3846
4197
|
const value = found[index] ?? 0;
|
|
3847
4198
|
if (value !== part) return value > part;
|
|
@@ -3863,8 +4214,11 @@ var CursorAgent = class {
|
|
|
3863
4214
|
command: inFolder(folder, "agent")
|
|
3864
4215
|
};
|
|
3865
4216
|
}
|
|
3866
|
-
|
|
3867
|
-
|
|
4217
|
+
signInStep = { instruction: "Run agent login and sign in.", command: "agent login" };
|
|
4218
|
+
installStep = {
|
|
4219
|
+
instruction: `Install the Cursor CLI ${MINIMUM_CURSOR_VERSION} or newer, then run connect again.`,
|
|
4220
|
+
link: "https://cursor.com/docs/cli/installation"
|
|
4221
|
+
};
|
|
3868
4222
|
/** ACP has no trust gate of its own, so an undecided folder is a step to take, not a launch to admit. */
|
|
3869
4223
|
unknownTrustAdmits = false;
|
|
3870
4224
|
async probe(executor, folder) {
|
|
@@ -3874,22 +4228,28 @@ var CursorAgent = class {
|
|
|
3874
4228
|
probe.available = version.code === 0 && probe.version !== null && supportedVersion(probe.version);
|
|
3875
4229
|
if (!probe.available) return probe;
|
|
3876
4230
|
const status = await executor.execute(this.executable, ["status"], "read", folder);
|
|
3877
|
-
const
|
|
3878
|
-
probe.signedIn = /not logged in|not authenticated|login required/i.test(
|
|
4231
|
+
const text5 = status.stdout + status.stderr;
|
|
4232
|
+
probe.signedIn = /not logged in|not authenticated|login required/i.test(text5) ? false : status.code === 0 && /logged in/i.test(text5) ? true : null;
|
|
3879
4233
|
return probe;
|
|
3880
4234
|
}
|
|
3881
4235
|
/**
|
|
3882
4236
|
* The folder's own record, then, for a linked worktree, its main repository's: the
|
|
3883
4237
|
* gateway's attempt worktrees are new folders no one can have trusted ahead of time.
|
|
3884
4238
|
*/
|
|
3885
|
-
async trust(executor, folder) {
|
|
4239
|
+
async trust(executor, folder, watch2 = async () => {
|
|
4240
|
+
}) {
|
|
3886
4241
|
try {
|
|
3887
4242
|
const env = await executor.environment(false, folder);
|
|
3888
4243
|
const root = env["CURSOR_CONFIG_DIR"] ?? join8(env["HOME"] ?? homedir8(), ".cursor");
|
|
3889
4244
|
const trusted = async (spelling) => {
|
|
3890
|
-
const
|
|
3891
|
-
|
|
4245
|
+
const path = join8(
|
|
4246
|
+
root,
|
|
4247
|
+
"projects",
|
|
4248
|
+
cursorProjectKey(spelling),
|
|
4249
|
+
".workspace-trusted"
|
|
3892
4250
|
);
|
|
4251
|
+
await watch2(path);
|
|
4252
|
+
const record = await readRecord(path);
|
|
3893
4253
|
return isObject(record) && field(record, "workspacePath") === spelling;
|
|
3894
4254
|
};
|
|
3895
4255
|
for (const spelling of /* @__PURE__ */ new Set([await realpath7(folder), folder]))
|
|
@@ -3959,6 +4319,10 @@ var HarnessRegistry = class {
|
|
|
3959
4319
|
([harness, driver]) => ({ harness, ...driver.repositorySetup(folder) })
|
|
3960
4320
|
);
|
|
3961
4321
|
}
|
|
4322
|
+
/** Every harness this gateway drives. */
|
|
4323
|
+
kinds() {
|
|
4324
|
+
return Object.keys(this.drivers);
|
|
4325
|
+
}
|
|
3962
4326
|
async detect() {
|
|
3963
4327
|
return Promise.all(Object.values(this.drivers).map((driver) => driver.detect()));
|
|
3964
4328
|
}
|
|
@@ -3968,11 +4332,16 @@ var HarnessRegistry = class {
|
|
|
3968
4332
|
async dispose() {
|
|
3969
4333
|
await Promise.all(Object.values(this.drivers).map((driver) => driver.dispose?.()));
|
|
3970
4334
|
}
|
|
3971
|
-
|
|
4335
|
+
/** Each registered folder's readiness for `harnesses`, every harness unless named. */
|
|
4336
|
+
async repositories(folders, harnesses = this.kinds(), read = "fresh") {
|
|
3972
4337
|
return (await Promise.all(
|
|
3973
|
-
|
|
4338
|
+
harnesses.map((harness) => this.get(harness).repositories(folders, read))
|
|
3974
4339
|
)).flat();
|
|
3975
4340
|
}
|
|
4341
|
+
/** Each registered folder's trust for `harnesses`, with no harness command. */
|
|
4342
|
+
async trust(folders, harnesses, read) {
|
|
4343
|
+
return (await Promise.all(harnesses.map((harness) => this.get(harness).trust(folders, read)))).flat();
|
|
4344
|
+
}
|
|
3976
4345
|
};
|
|
3977
4346
|
|
|
3978
4347
|
// src/builtin-harnesses.ts
|
|
@@ -4139,11 +4508,11 @@ async function copyIncluded(git, folder, requireLive, prefix = "") {
|
|
|
4139
4508
|
const clone = await realpath9(git.clone);
|
|
4140
4509
|
const destinationRoot = await realpath9(folder);
|
|
4141
4510
|
for (const name of result.stdout.split("\0").filter(Boolean)) {
|
|
4142
|
-
const
|
|
4511
|
+
const source = resolve8(clone, name);
|
|
4143
4512
|
const destinationName = posix2.join(prefix, name);
|
|
4144
4513
|
const destination = resolve8(destinationRoot, destinationName);
|
|
4145
|
-
if (!inside(clone,
|
|
4146
|
-
if (!(await lstat(
|
|
4514
|
+
if (!inside(clone, source) || !inside(destinationRoot, destination)) continue;
|
|
4515
|
+
if (!(await lstat(source)).isFile() || !inside(clone, await realpath9(source))) continue;
|
|
4147
4516
|
if (await checkDestination(destinationRoot, destination)) continue;
|
|
4148
4517
|
const tracked = await git.run(
|
|
4149
4518
|
["--literal-pathspecs", "ls-files", "--error-unmatch", "--", destinationName],
|
|
@@ -4156,7 +4525,7 @@ async function copyIncluded(git, folder, requireLive, prefix = "") {
|
|
|
4156
4525
|
requireLive();
|
|
4157
4526
|
await mkdir2(dirname5(destination), { recursive: true });
|
|
4158
4527
|
requireLive();
|
|
4159
|
-
await copyFile(
|
|
4528
|
+
await copyFile(source, destination, constants3.COPYFILE_EXCL).catch((error) => {
|
|
4160
4529
|
if (!(error instanceof Error && "code" in error && error.code === "EEXIST"))
|
|
4161
4530
|
throw error;
|
|
4162
4531
|
});
|
|
@@ -4565,14 +4934,193 @@ async function createBranch(git, context, requireLive) {
|
|
|
4565
4934
|
return !local && !prior;
|
|
4566
4935
|
}
|
|
4567
4936
|
|
|
4937
|
+
// src/invocation.ts
|
|
4938
|
+
import { accessSync, constants as constants5, realpathSync, statSync } from "node:fs";
|
|
4939
|
+
import { posix as posix3, win32 } from "node:path";
|
|
4940
|
+
import { fileURLToPath } from "node:url";
|
|
4941
|
+
var PACKAGE = "@treestructuredplanning/gateway";
|
|
4942
|
+
var INSTALLED = "tsp-gateway";
|
|
4943
|
+
var NPX_LATEST = `npx -y ${PACKAGE}@latest`;
|
|
4944
|
+
function launchedByNpx(entry2) {
|
|
4945
|
+
return entry2.split(/[\\/]/).includes("_npx");
|
|
4946
|
+
}
|
|
4947
|
+
function gatewayInstall(entry2, env = process.env, platform = process.platform) {
|
|
4948
|
+
const command2 = commandPath(entry2, env, platform);
|
|
4949
|
+
return command2.onPath && command2.matches ? GATEWAY_INSTALL.GLOBAL : GATEWAY_INSTALL.NPX;
|
|
4950
|
+
}
|
|
4951
|
+
var RUNNER = {
|
|
4952
|
+
[GATEWAY_INSTALL.GLOBAL]: INSTALLED,
|
|
4953
|
+
[GATEWAY_INSTALL.NPX]: NPX_LATEST
|
|
4954
|
+
};
|
|
4955
|
+
function invocation(entry2, env = process.env, platform = process.platform) {
|
|
4956
|
+
return RUNNER[gatewayInstall(entry2, env, platform)];
|
|
4957
|
+
}
|
|
4958
|
+
var REFRESHES = /* @__PURE__ */ new Set(["connect", "start", "update"]);
|
|
4959
|
+
function gateway(args, entry2, version = package_default.version, env = process.env, platform = process.platform) {
|
|
4960
|
+
const install2 = entry2 === void 0 ? OWN_INSTALL : gatewayInstall(entry2, env, platform);
|
|
4961
|
+
const runner = install2 === GATEWAY_INSTALL.NPX && !REFRESHES.has(args.split(/\s/)[0] ?? "") ? `npx -y ${PACKAGE}@${version}` : RUNNER[install2];
|
|
4962
|
+
return `${runner} ${args}`;
|
|
4963
|
+
}
|
|
4964
|
+
var GLOBAL_TAIL = ["node_modules", ...PACKAGE.split("/"), "dist", "cli.js"];
|
|
4965
|
+
function globalCommandFolder(entry2, platform) {
|
|
4966
|
+
const parts2 = realPath(entry2).split(/[\\/]/);
|
|
4967
|
+
const head = parts2.length - GLOBAL_TAIL.length;
|
|
4968
|
+
if (head < 1 || GLOBAL_TAIL.some((part, index) => parts2[head + index] !== part)) return null;
|
|
4969
|
+
if (platform === "win32") return parts2.slice(0, head).join("\\");
|
|
4970
|
+
if (parts2[head - 1] !== "lib") return null;
|
|
4971
|
+
return [...parts2.slice(0, head - 1), "bin"].join("/");
|
|
4972
|
+
}
|
|
4973
|
+
function realPath(value) {
|
|
4974
|
+
try {
|
|
4975
|
+
return realpathSync(value);
|
|
4976
|
+
} catch {
|
|
4977
|
+
return value;
|
|
4978
|
+
}
|
|
4979
|
+
}
|
|
4980
|
+
function canonical(value, platform) {
|
|
4981
|
+
const path = platform === "win32" ? win32 : posix3;
|
|
4982
|
+
const real = realPath(path.resolve(value));
|
|
4983
|
+
return platform === "win32" ? real.toLowerCase() : real;
|
|
4984
|
+
}
|
|
4985
|
+
function commandPath(entry2, env, platform) {
|
|
4986
|
+
const folder = globalCommandFolder(entry2, platform);
|
|
4987
|
+
if (folder === null) return { folder, onPath: false, matches: false };
|
|
4988
|
+
const path = platform === "win32" ? win32 : posix3;
|
|
4989
|
+
const wanted = canonical(folder, platform);
|
|
4990
|
+
const extensions = platform === "win32" ? (env["PATHEXT"] ?? ".COM;.EXE;.BAT;.CMD").split(";") : [""];
|
|
4991
|
+
let onPath = false;
|
|
4992
|
+
let found = false;
|
|
4993
|
+
let matches2 = false;
|
|
4994
|
+
for (const candidate of (env["PATH"] ?? env["Path"])?.split(path.delimiter) ?? []) {
|
|
4995
|
+
const directory = (platform === "win32" ? candidate.replace(/^"(.*)"$/, "$1") : candidate) || ".";
|
|
4996
|
+
if (canonical(directory, platform) === wanted) onPath = true;
|
|
4997
|
+
if (found) continue;
|
|
4998
|
+
for (const extension of extensions) {
|
|
4999
|
+
const command2 = path.join(directory, `${INSTALLED}${extension}`);
|
|
5000
|
+
try {
|
|
5001
|
+
if (!statSync(command2).isFile()) continue;
|
|
5002
|
+
accessSync(command2, platform === "win32" ? constants5.F_OK : constants5.X_OK);
|
|
5003
|
+
found = true;
|
|
5004
|
+
matches2 = canonical(directory, platform) === wanted && (platform === "win32" || canonical(command2, platform) === canonical(entry2, platform));
|
|
5005
|
+
break;
|
|
5006
|
+
} catch {
|
|
5007
|
+
continue;
|
|
5008
|
+
}
|
|
5009
|
+
}
|
|
5010
|
+
}
|
|
5011
|
+
return { folder, onPath, matches: matches2 };
|
|
5012
|
+
}
|
|
5013
|
+
async function commandFolderOffPath(entry2, env, platform) {
|
|
5014
|
+
const command2 = commandPath(entry2, env, platform);
|
|
5015
|
+
return command2.onPath ? null : command2.folder;
|
|
5016
|
+
}
|
|
5017
|
+
var OWN_ENTRY = fileURLToPath(new URL("./cli.js", import.meta.url));
|
|
5018
|
+
var OWN_INSTALL = gatewayInstall(OWN_ENTRY);
|
|
5019
|
+
|
|
5020
|
+
// src/cli-output.ts
|
|
5021
|
+
import { styleText } from "node:util";
|
|
5022
|
+
var MARKS = {
|
|
5023
|
+
pass: { glyph: "\u2713", format: "green" },
|
|
5024
|
+
fail: { glyph: "\u2717", format: "red" },
|
|
5025
|
+
wait: { glyph: "\u25CB", format: "yellow" },
|
|
5026
|
+
next: { glyph: "\u2192", format: "cyan" }
|
|
5027
|
+
};
|
|
5028
|
+
var heading = (text5) => ({ kind: "heading", text: text5 });
|
|
5029
|
+
var marked = (mark, text5, depth = 0) => ({
|
|
5030
|
+
kind: "marked",
|
|
5031
|
+
mark,
|
|
5032
|
+
text: text5,
|
|
5033
|
+
depth
|
|
5034
|
+
});
|
|
5035
|
+
var text4 = (value, depth = 0) => ({
|
|
5036
|
+
kind: "text",
|
|
5037
|
+
text: value,
|
|
5038
|
+
depth,
|
|
5039
|
+
muted: false
|
|
5040
|
+
});
|
|
5041
|
+
var note = (value, depth = 0) => ({
|
|
5042
|
+
kind: "text",
|
|
5043
|
+
text: value,
|
|
5044
|
+
depth,
|
|
5045
|
+
muted: true
|
|
5046
|
+
});
|
|
5047
|
+
var command = (value, depth = 0) => ({
|
|
5048
|
+
kind: "command",
|
|
5049
|
+
text: value,
|
|
5050
|
+
depth
|
|
5051
|
+
});
|
|
5052
|
+
var entry = (name, summary, width, depth = 0) => ({
|
|
5053
|
+
kind: "entry",
|
|
5054
|
+
name,
|
|
5055
|
+
summary,
|
|
5056
|
+
width,
|
|
5057
|
+
depth
|
|
5058
|
+
});
|
|
5059
|
+
var blank = { kind: "blank" };
|
|
5060
|
+
function nextLines(action, depth = 0) {
|
|
5061
|
+
return [marked("next", `${action.instruction}:`, depth), command(action.command, depth + 2)];
|
|
5062
|
+
}
|
|
5063
|
+
function colorEnabled(stream, env) {
|
|
5064
|
+
if (env["NO_COLOR"]) return false;
|
|
5065
|
+
const force = env["FORCE_COLOR"];
|
|
5066
|
+
if (force !== void 0) return force !== "0" && force !== "false";
|
|
5067
|
+
return stream.isTTY === true && stream.hasColors() === true;
|
|
5068
|
+
}
|
|
5069
|
+
function renderLines(lines, color) {
|
|
5070
|
+
const style = (format, value) => color ? styleText(format, value, { validateStream: false }) : value;
|
|
5071
|
+
return lines.map((line) => {
|
|
5072
|
+
if (line.kind === "blank") return "";
|
|
5073
|
+
if (line.kind === "heading") return style("bold", line.text);
|
|
5074
|
+
const indent = " ".repeat(line.depth);
|
|
5075
|
+
if (line.kind === "command") return indent + style("cyan", line.text);
|
|
5076
|
+
if (line.kind === "entry")
|
|
5077
|
+
return `${indent}${style("cyan", line.name.padEnd(line.width))} ${line.summary}`;
|
|
5078
|
+
if (line.kind === "text")
|
|
5079
|
+
return indent + (line.muted ? style("dim", line.text) : line.text);
|
|
5080
|
+
const mark = MARKS[line.mark];
|
|
5081
|
+
return `${indent}${style(mark.format, mark.glyph)} ${line.text}`;
|
|
5082
|
+
});
|
|
5083
|
+
}
|
|
5084
|
+
function writeLines(lines, stream = process.stdout, env = process.env) {
|
|
5085
|
+
if (lines.length === 0) return;
|
|
5086
|
+
stream.write(renderLines(lines, colorEnabled(stream, env)).join("\n") + "\n");
|
|
5087
|
+
}
|
|
5088
|
+
var ActionableError = class extends Error {
|
|
5089
|
+
constructor(message, next) {
|
|
5090
|
+
super(message);
|
|
5091
|
+
this.next = next;
|
|
5092
|
+
this.name = "ActionableError";
|
|
5093
|
+
}
|
|
5094
|
+
};
|
|
5095
|
+
var UsageError = class extends Error {
|
|
5096
|
+
constructor(usage) {
|
|
5097
|
+
super(`Usage: ${gateway(usage)}`);
|
|
5098
|
+
this.usage = usage;
|
|
5099
|
+
this.name = "UsageError";
|
|
5100
|
+
}
|
|
5101
|
+
};
|
|
5102
|
+
function errorLines(error) {
|
|
5103
|
+
if (error instanceof UsageError)
|
|
5104
|
+
return [
|
|
5105
|
+
marked("fail", "That command takes different arguments. Usage:"),
|
|
5106
|
+
command(gateway(error.usage), 2),
|
|
5107
|
+
note(`${gateway("--help")} lists every command.`, 1)
|
|
5108
|
+
];
|
|
5109
|
+
const message = error instanceof Error ? error.message : "Gateway could not complete the command";
|
|
5110
|
+
return [
|
|
5111
|
+
marked("fail", message),
|
|
5112
|
+
...error instanceof ActionableError ? nextLines(error.next, 1) : []
|
|
5113
|
+
];
|
|
5114
|
+
}
|
|
5115
|
+
|
|
4568
5116
|
// src/checkout-config.ts
|
|
4569
5117
|
import { realpath as realpath12 } from "node:fs/promises";
|
|
4570
5118
|
import { resolve as resolve11 } from "node:path";
|
|
4571
5119
|
async function configureWorktrees(config, path) {
|
|
4572
5120
|
decodeWorktreeRoot(config.worktrees.root);
|
|
4573
|
-
for (const
|
|
4574
|
-
decodeWorktreeRoot(config.worktrees.root,
|
|
4575
|
-
decodeWorktreeRoot(
|
|
5121
|
+
for (const entry2 of Object.values(config.repositories)) {
|
|
5122
|
+
decodeWorktreeRoot(config.worktrees.root, entry2.folder);
|
|
5123
|
+
decodeWorktreeRoot(entry2.worktreeRoot, entry2.folder);
|
|
4576
5124
|
}
|
|
4577
5125
|
await saveConfig(path, config);
|
|
4578
5126
|
}
|
|
@@ -4580,46 +5128,59 @@ var SETTING_SOURCE = {
|
|
|
4580
5128
|
repository: "set for this repository with --repo",
|
|
4581
5129
|
machine: "set for this machine"
|
|
4582
5130
|
};
|
|
4583
|
-
function
|
|
4584
|
-
return
|
|
5131
|
+
function worktreeView(clone, root) {
|
|
5132
|
+
return {
|
|
5133
|
+
folder: root?.folder ?? UNRESOLVED_WORKTREE_ROOT,
|
|
5134
|
+
source: root?.setting ? SETTING_SOURCE[root.setting] : "the default inside the repository",
|
|
5135
|
+
outsideClone: root !== null && !inside(clone, root.folder)
|
|
5136
|
+
};
|
|
5137
|
+
}
|
|
5138
|
+
function worktreeLines(view, depth) {
|
|
5139
|
+
return [
|
|
5140
|
+
text4(`Worktrees: ${view.folder} (${view.source})`, depth),
|
|
5141
|
+
...view.outsideClone ? [
|
|
5142
|
+
note(
|
|
5143
|
+
"Codex: trust the registered clone; linked worktrees inherit that trust.",
|
|
5144
|
+
depth
|
|
5145
|
+
)
|
|
5146
|
+
] : []
|
|
5147
|
+
];
|
|
4585
5148
|
}
|
|
4586
|
-
async function printWorktreeRoots(config, executor) {
|
|
5149
|
+
async function printWorktreeRoots(config, executor, print = (lines) => writeLines(lines)) {
|
|
5150
|
+
const lines = [];
|
|
4587
5151
|
if (Object.keys(config.repositories).length === 0)
|
|
4588
|
-
|
|
4589
|
-
|
|
4590
|
-
|
|
4591
|
-
|
|
4592
|
-
const root = await previewWorktreeRoot(executor, entry.folder, config);
|
|
4593
|
-
console.log(
|
|
4594
|
-
`Worktrees for ${entry.folder}: ${root?.folder ?? UNRESOLVED_WORKTREE_ROOT} (${source(root)})`
|
|
5152
|
+
lines.push(
|
|
5153
|
+
text4(
|
|
5154
|
+
`Worktrees for future clones: ${config.worktrees.root ?? UNRESOLVED_WORKTREE_ROOT}`
|
|
5155
|
+
)
|
|
4595
5156
|
);
|
|
4596
|
-
|
|
4597
|
-
|
|
5157
|
+
for (const entry2 of Object.values(config.repositories)) {
|
|
5158
|
+
const root = await previewWorktreeRoot(executor, entry2.folder, config);
|
|
5159
|
+
lines.push(heading(entry2.folder), ...worktreeLines(worktreeView(entry2.folder, root), 1));
|
|
4598
5160
|
}
|
|
5161
|
+
print(lines);
|
|
4599
5162
|
}
|
|
4600
5163
|
function connectWorktreeArgs(args) {
|
|
4601
5164
|
if (args.length <= 2) return args[1] ? { folder: args[1] } : {};
|
|
4602
5165
|
if (args.length !== 4 || args[2] !== "--worktrees-root")
|
|
4603
|
-
throw new
|
|
5166
|
+
throw new UsageError("connect [folder] [--worktrees-root <path>]");
|
|
4604
5167
|
const root = decodeWorktreeRoot(args[3], resolve11(args[1]));
|
|
4605
5168
|
if (root === null) throw new Error("A worktree root is required");
|
|
4606
5169
|
return { folder: args[1], root };
|
|
4607
5170
|
}
|
|
4608
5171
|
async function setWorktreeRoot(config, path, args) {
|
|
4609
5172
|
if (args[1] !== "worktrees-root" || !(args.length === 3 && !args[2].startsWith("--repo") || args.length === 5 && args[2] === "--repo"))
|
|
4610
|
-
throw new
|
|
4611
|
-
"Usage: tsp-gateway config worktrees-root [--repo <folder>] <path|--reset>"
|
|
4612
|
-
);
|
|
5173
|
+
throw new UsageError("config worktrees-root [--repo <folder>] <path|--reset>");
|
|
4613
5174
|
const value = args.at(-1);
|
|
4614
5175
|
const root = value === "--reset" ? null : value;
|
|
4615
5176
|
if (args.length === 5) {
|
|
4616
5177
|
const folder = await realpath12(args[3]);
|
|
4617
|
-
const
|
|
4618
|
-
if (!
|
|
5178
|
+
const entry2 = Object.values(config.repositories).find((entry3) => entry3.folder === folder);
|
|
5179
|
+
if (!entry2)
|
|
4619
5180
|
throw new Error(
|
|
4620
|
-
|
|
5181
|
+
`Repository folder is not registered. Run ${gateway("add <folder>")} first.`
|
|
4621
5182
|
);
|
|
4622
|
-
|
|
5183
|
+
entry2.worktreeRoot = decodeWorktreeRoot(root, folder);
|
|
4623
5184
|
} else config.worktrees.root = decodeWorktreeRoot(root);
|
|
4624
5185
|
await configureWorktrees(config, path);
|
|
4625
5186
|
}
|
|
@@ -4635,13 +5196,13 @@ async function worktreesCommand({
|
|
|
4635
5196
|
args
|
|
4636
5197
|
}) {
|
|
4637
5198
|
if (args[0] === "config") await setWorktreeRoot(config, paths.config, args);
|
|
4638
|
-
else if (args.length !== 1) throw new
|
|
5199
|
+
else if (args.length !== 1) throw new UsageError("worktrees");
|
|
4639
5200
|
await printWorktreeRoots(config, executor);
|
|
4640
5201
|
}
|
|
4641
5202
|
|
|
4642
5203
|
// src/command-lifetime.ts
|
|
4643
|
-
function withdrawnReason(
|
|
4644
|
-
return
|
|
5204
|
+
function withdrawnReason(command2) {
|
|
5205
|
+
return command2.kind === "launch" ? "launch_withdrawn" : "control_withdrawn";
|
|
4645
5206
|
}
|
|
4646
5207
|
var lifetimeClock = {
|
|
4647
5208
|
monotonic: () => performance.now(),
|
|
@@ -4678,28 +5239,28 @@ var CommandLifetime = class {
|
|
|
4678
5239
|
* Aborts `signal` and fails every later check of this launch or resume, or only holds an
|
|
4679
5240
|
* uncertain one. Repeating it changes nothing.
|
|
4680
5241
|
*/
|
|
4681
|
-
withdraw(
|
|
4682
|
-
if (
|
|
5242
|
+
withdraw(command2) {
|
|
5243
|
+
if (command2.kind === "stop" || this.held || this.withdrawal.signal.aborted) return;
|
|
4683
5244
|
if (this.uncertain) {
|
|
4684
5245
|
this.held = true;
|
|
4685
5246
|
return;
|
|
4686
5247
|
}
|
|
4687
|
-
this.withdrawal.abort(new HarnessRefusal(withdrawnReason(
|
|
5248
|
+
this.withdrawal.abort(new HarnessRefusal(withdrawnReason(command2)));
|
|
4688
5249
|
}
|
|
4689
|
-
requireLive(
|
|
5250
|
+
requireLive(command2) {
|
|
4690
5251
|
if (this.withdrawal.signal.aborted) throw this.withdrawal.signal.reason;
|
|
4691
5252
|
if (this.remaining() <= 0)
|
|
4692
5253
|
throw new HarnessRefusal(
|
|
4693
|
-
|
|
5254
|
+
command2.kind === "launch" ? "launch_expired" : "control_expired"
|
|
4694
5255
|
);
|
|
4695
5256
|
}
|
|
4696
5257
|
/** The checks a command's harness effects run, carrying the withdrawal signal with them. */
|
|
4697
|
-
check(
|
|
4698
|
-
return Object.assign(() => this.requireLive(
|
|
5258
|
+
check(command2) {
|
|
5259
|
+
return Object.assign(() => this.requireLive(command2), {
|
|
4699
5260
|
signal: this.withdrawal.signal,
|
|
4700
5261
|
prompt: () => {
|
|
4701
|
-
this.requireLive(
|
|
4702
|
-
if (this.held) throw new HarnessRefusal(withdrawnReason(
|
|
5262
|
+
this.requireLive(command2);
|
|
5263
|
+
if (this.held) throw new HarnessRefusal(withdrawnReason(command2));
|
|
4703
5264
|
}
|
|
4704
5265
|
});
|
|
4705
5266
|
}
|
|
@@ -4729,27 +5290,27 @@ var CommandLifetimes = class {
|
|
|
4729
5290
|
constructor(journal, clock) {
|
|
4730
5291
|
this.journal = journal;
|
|
4731
5292
|
this.clock = clock;
|
|
4732
|
-
for (const
|
|
4733
|
-
const { command } =
|
|
4734
|
-
const lifetime = new CommandLifetime(
|
|
4735
|
-
this.lifetimes.set(
|
|
4736
|
-
if (
|
|
4737
|
-
if (
|
|
5293
|
+
for (const entry2 of journal.entries.values()) {
|
|
5294
|
+
const { command: command2 } = entry2;
|
|
5295
|
+
const lifetime = new CommandLifetime(command2.ttl_ms, clock, entry2.lifetime);
|
|
5296
|
+
this.lifetimes.set(command2.command_id, lifetime);
|
|
5297
|
+
if (entry2.receipt === null && command2.kind !== "stop") lifetime.uncertainStart();
|
|
5298
|
+
if (command2.kind === "stop") this.withdrawStarts(command2);
|
|
4738
5299
|
}
|
|
4739
5300
|
}
|
|
4740
5301
|
lifetimes = /* @__PURE__ */ new Map();
|
|
4741
5302
|
/** Commands received but not journaled yet, so a Stop can withdraw a start still queued. */
|
|
4742
5303
|
queued = /* @__PURE__ */ new Map();
|
|
4743
|
-
receive(
|
|
4744
|
-
let lifetime = this.lifetimes.get(
|
|
5304
|
+
receive(command2) {
|
|
5305
|
+
let lifetime = this.lifetimes.get(command2.command_id);
|
|
4745
5306
|
if (!lifetime) {
|
|
4746
|
-
lifetime = new CommandLifetime(
|
|
4747
|
-
this.lifetimes.set(
|
|
4748
|
-
if (!this.journal.entries.has(
|
|
4749
|
-
this.queued.set(
|
|
5307
|
+
lifetime = new CommandLifetime(command2.ttl_ms, this.clock);
|
|
5308
|
+
this.lifetimes.set(command2.command_id, lifetime);
|
|
5309
|
+
if (!this.journal.entries.has(command2.command_id))
|
|
5310
|
+
this.queued.set(command2.command_id, command2);
|
|
4750
5311
|
}
|
|
4751
|
-
if (
|
|
4752
|
-
this.withdrawStarts(
|
|
5312
|
+
if (command2.kind === "stop" && !this.journal.entries.get(command2.command_id)?.receipt)
|
|
5313
|
+
this.withdrawStarts(command2);
|
|
4753
5314
|
return lifetime;
|
|
4754
5315
|
}
|
|
4755
5316
|
/** The command left the queue; the journal holds it from here on. */
|
|
@@ -4757,10 +5318,10 @@ var CommandLifetimes = class {
|
|
|
4757
5318
|
this.queued.delete(commandId);
|
|
4758
5319
|
}
|
|
4759
5320
|
retire(actionId) {
|
|
4760
|
-
for (const { command } of this.journal.entries.values())
|
|
4761
|
-
if (
|
|
4762
|
-
for (const [id,
|
|
4763
|
-
if (
|
|
5321
|
+
for (const { command: command2 } of this.journal.entries.values())
|
|
5322
|
+
if (command2.action_id === actionId) this.lifetimes.delete(command2.command_id);
|
|
5323
|
+
for (const [id, command2] of this.queued)
|
|
5324
|
+
if (command2.action_id === actionId) {
|
|
4764
5325
|
this.queued.delete(id);
|
|
4765
5326
|
this.lifetimes.delete(id);
|
|
4766
5327
|
}
|
|
@@ -4768,10 +5329,10 @@ var CommandLifetimes = class {
|
|
|
4768
5329
|
/** Withdraws every unfinished launch or resume of the Stop's action received before it. */
|
|
4769
5330
|
withdrawStarts(stop) {
|
|
4770
5331
|
for (const [id, lifetime] of this.lifetimes) {
|
|
4771
|
-
const
|
|
4772
|
-
const
|
|
4773
|
-
if (
|
|
4774
|
-
lifetime.withdraw(
|
|
5332
|
+
const entry2 = this.journal.entries.get(id);
|
|
5333
|
+
const command2 = entry2?.command ?? this.queued.get(id);
|
|
5334
|
+
if (command2 && command2.kind !== "stop" && command2.action_id === stop.action_id && !entry2?.receipt)
|
|
5335
|
+
lifetime.withdraw(command2);
|
|
4775
5336
|
}
|
|
4776
5337
|
}
|
|
4777
5338
|
};
|
|
@@ -4840,17 +5401,17 @@ function readCommand(value, version) {
|
|
|
4840
5401
|
throw new Error("Invalid stored operation");
|
|
4841
5402
|
}
|
|
4842
5403
|
}
|
|
4843
|
-
function checkIdentity(row,
|
|
5404
|
+
function checkIdentity(row, command2) {
|
|
4844
5405
|
for (const key of ["action_id", "command_id"]) {
|
|
4845
5406
|
const value = field(row, key);
|
|
4846
|
-
if (value !== void 0 && value !==
|
|
5407
|
+
if (value !== void 0 && value !== command2[key])
|
|
4847
5408
|
throw new Error("Invalid stored identity");
|
|
4848
5409
|
}
|
|
4849
5410
|
}
|
|
4850
|
-
function readReceipt(value,
|
|
5411
|
+
function readReceipt(value, command2) {
|
|
4851
5412
|
if (value === null) return null;
|
|
4852
5413
|
const row = object(value);
|
|
4853
|
-
checkIdentity(row,
|
|
5414
|
+
checkIdentity(row, command2);
|
|
4854
5415
|
switch (field(row, "kind")) {
|
|
4855
5416
|
case "ack":
|
|
4856
5417
|
return { kind: "ack" };
|
|
@@ -4872,10 +5433,10 @@ function readAttachHint(value) {
|
|
|
4872
5433
|
throw new Error("Invalid attach session reference");
|
|
4873
5434
|
return { harness: member2(field(row, "harness"), Object.values(HARNESS_KIND)), session_ref };
|
|
4874
5435
|
}
|
|
4875
|
-
function readObservation(value,
|
|
5436
|
+
function readObservation(value, command2) {
|
|
4876
5437
|
if (value === null) return null;
|
|
4877
5438
|
const row = object(value);
|
|
4878
|
-
checkIdentity(row,
|
|
5439
|
+
checkIdentity(row, command2);
|
|
4879
5440
|
return {
|
|
4880
5441
|
checkout: decodeCheckoutReport(field(row, "checkout")),
|
|
4881
5442
|
session_ref: string(field(row, "session_ref")),
|
|
@@ -4890,11 +5451,11 @@ function readJournalEntry(value, folders = {}) {
|
|
|
4890
5451
|
const row = object(value), version = field(row, "format_version");
|
|
4891
5452
|
if (version !== void 0 && version !== 1 && version !== 2 && version !== 3)
|
|
4892
5453
|
throw new Error("Unsupported journal format");
|
|
4893
|
-
const
|
|
5454
|
+
const command2 = readCommand(field(row, "command"), version);
|
|
4894
5455
|
const lifetime = object(field(row, "lifetime"));
|
|
4895
|
-
const receipt = readReceipt(field(row, "receipt"),
|
|
5456
|
+
const receipt = readReceipt(field(row, "receipt"), command2);
|
|
4896
5457
|
if (receipt?.kind === "refused") validateRefusalDetail(receipt.reason, receipt.detail);
|
|
4897
|
-
const update = readObservation(field(row, "update"),
|
|
5458
|
+
const update = readObservation(field(row, "update"), command2);
|
|
4898
5459
|
const rawRefs = field(row, "observedRefs");
|
|
4899
5460
|
if (!Array.isArray(rawRefs)) throw new Error("Invalid stored observations");
|
|
4900
5461
|
const observedRefs = rawRefs.map((ref) => string(ref));
|
|
@@ -4903,16 +5464,16 @@ function readJournalEntry(value, folders = {}) {
|
|
|
4903
5464
|
throw new Error("Invalid stored attempts");
|
|
4904
5465
|
const envelope = {
|
|
4905
5466
|
protocol_version: GATEWAY_PROTOCOL_VERSION,
|
|
4906
|
-
action_id:
|
|
5467
|
+
action_id: command2.action_id
|
|
4907
5468
|
};
|
|
4908
5469
|
return {
|
|
4909
|
-
checkout: version === 3 ? readPrepared(field(row, "checkout")) : legacyPrepared(
|
|
5470
|
+
checkout: version === 3 ? readPrepared(field(row, "checkout")) : legacyPrepared(command2, folders),
|
|
4910
5471
|
lifetime: {
|
|
4911
5472
|
remainingMs: nonnegative(field(lifetime, "remainingMs")),
|
|
4912
5473
|
savedAtMs: nonnegative(field(lifetime, "savedAtMs"))
|
|
4913
5474
|
},
|
|
4914
|
-
command: { ...
|
|
4915
|
-
receipt: receipt && { ...receipt, ...envelope, command_id:
|
|
5475
|
+
command: { ...command2, protocol_version: GATEWAY_PROTOCOL_VERSION },
|
|
5476
|
+
receipt: receipt && { ...receipt, ...envelope, command_id: command2.command_id },
|
|
4916
5477
|
update: update && {
|
|
4917
5478
|
...update,
|
|
4918
5479
|
...envelope,
|
|
@@ -4923,15 +5484,15 @@ function readJournalEntry(value, folders = {}) {
|
|
|
4923
5484
|
attempts
|
|
4924
5485
|
};
|
|
4925
5486
|
}
|
|
4926
|
-
function storeJournalEntry(
|
|
4927
|
-
const { protocol_version: _version, ...
|
|
4928
|
-
const receipt =
|
|
4929
|
-
const update =
|
|
5487
|
+
function storeJournalEntry(entry2) {
|
|
5488
|
+
const { protocol_version: _version, ...command2 } = entry2.command;
|
|
5489
|
+
const receipt = entry2.receipt;
|
|
5490
|
+
const update = entry2.update;
|
|
4930
5491
|
return {
|
|
4931
5492
|
format_version: 3,
|
|
4932
|
-
checkout:
|
|
4933
|
-
lifetime:
|
|
4934
|
-
command,
|
|
5493
|
+
checkout: entry2.checkout,
|
|
5494
|
+
lifetime: entry2.lifetime,
|
|
5495
|
+
command: command2,
|
|
4935
5496
|
receipt: receipt === null ? null : receipt.kind === "refused" ? { kind: "refused", reason: receipt.reason, detail: receipt.detail ?? null } : { kind: "ack" },
|
|
4936
5497
|
update: update && {
|
|
4937
5498
|
checkout: update.checkout ?? null,
|
|
@@ -4942,9 +5503,9 @@ function storeJournalEntry(entry) {
|
|
|
4942
5503
|
attach_hint: update.attach_hint ?? null,
|
|
4943
5504
|
resumed_as: update.resumed_as ?? null
|
|
4944
5505
|
},
|
|
4945
|
-
conversationId:
|
|
4946
|
-
observedRefs:
|
|
4947
|
-
attempts:
|
|
5506
|
+
conversationId: entry2.conversationId,
|
|
5507
|
+
observedRefs: entry2.observedRefs,
|
|
5508
|
+
attempts: entry2.attempts
|
|
4948
5509
|
};
|
|
4949
5510
|
}
|
|
4950
5511
|
function readPrepared(value) {
|
|
@@ -4971,9 +5532,9 @@ function readPrepared(value) {
|
|
|
4971
5532
|
sharesCheckout: boolean("sharesCheckout")
|
|
4972
5533
|
};
|
|
4973
5534
|
}
|
|
4974
|
-
function legacyPrepared(
|
|
4975
|
-
if (
|
|
4976
|
-
const folder = folders[repositoryKey(
|
|
5535
|
+
function legacyPrepared(command2, folders) {
|
|
5536
|
+
if (command2.kind !== "launch") return null;
|
|
5537
|
+
const folder = folders[repositoryKey(command2.repository)];
|
|
4977
5538
|
if (!folder) return null;
|
|
4978
5539
|
return {
|
|
4979
5540
|
folder,
|
|
@@ -4996,19 +5557,19 @@ function absoluteFolder(value) {
|
|
|
4996
5557
|
}
|
|
4997
5558
|
|
|
4998
5559
|
// src/journal.ts
|
|
4999
|
-
function
|
|
5000
|
-
if (Array.isArray(value)) return `[${value.map((item) =>
|
|
5560
|
+
function canonical2(value) {
|
|
5561
|
+
if (Array.isArray(value)) return `[${value.map((item) => canonical2(item)).join(",")}]`;
|
|
5001
5562
|
if (isObject(value))
|
|
5002
|
-
return `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${
|
|
5563
|
+
return `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${canonical2(field(value, key))}`).join(",")}}`;
|
|
5003
5564
|
return JSON.stringify(value);
|
|
5004
5565
|
}
|
|
5005
|
-
function commandDigest(
|
|
5566
|
+
function commandDigest(command2) {
|
|
5006
5567
|
const identity2 = {
|
|
5007
|
-
...
|
|
5568
|
+
...command2,
|
|
5008
5569
|
ttl_ms: null,
|
|
5009
|
-
...
|
|
5570
|
+
...command2.kind === "resume" ? { text: command2.text ?? null } : {}
|
|
5010
5571
|
};
|
|
5011
|
-
return createHash("sha256").update(
|
|
5572
|
+
return createHash("sha256").update(canonical2(identity2)).digest("hex");
|
|
5012
5573
|
}
|
|
5013
5574
|
var Journal = class {
|
|
5014
5575
|
constructor(path) {
|
|
@@ -5033,9 +5594,9 @@ var Journal = class {
|
|
|
5033
5594
|
let value;
|
|
5034
5595
|
try {
|
|
5035
5596
|
value = parseJson(line);
|
|
5036
|
-
const
|
|
5597
|
+
const entry2 = readJournalEntry(value, folders);
|
|
5037
5598
|
upgrade ||= isObject(value) && field(value, "format_version") !== 3;
|
|
5038
|
-
this.entries.set(
|
|
5599
|
+
this.entries.set(entry2.command.command_id, entry2);
|
|
5039
5600
|
} catch {
|
|
5040
5601
|
this.quarantine(line, value);
|
|
5041
5602
|
console.error("TSP gateway skipped an unreadable journal entry.");
|
|
@@ -5044,40 +5605,40 @@ var Journal = class {
|
|
|
5044
5605
|
if (!upgrade) return;
|
|
5045
5606
|
await privateWrite(
|
|
5046
5607
|
this.path,
|
|
5047
|
-
[...this.entries.values()].map((
|
|
5608
|
+
[...this.entries.values()].map((entry2) => JSON.stringify(storeJournalEntry(entry2)) + "\n").join("") + this.unreadableRows.map((row) => row.line + "\n").join("")
|
|
5048
5609
|
);
|
|
5049
5610
|
}
|
|
5050
5611
|
quarantine(line, value) {
|
|
5051
|
-
const
|
|
5052
|
-
const id = isObject(
|
|
5053
|
-
const action = isObject(
|
|
5612
|
+
const command2 = isObject(value) ? field(value, "command") : null;
|
|
5613
|
+
const id = isObject(command2) ? field(command2, "command_id") : null;
|
|
5614
|
+
const action = isObject(command2) ? field(command2, "action_id") : null;
|
|
5054
5615
|
if (typeof id === "string") this.unreadableCommandIds.add(id);
|
|
5055
5616
|
this.unreadableRows.push({ line, actionId: typeof action === "string" ? action : null });
|
|
5056
5617
|
}
|
|
5057
5618
|
async retire(actionId) {
|
|
5058
5619
|
const retained = [...this.entries.values()].filter(
|
|
5059
|
-
(
|
|
5620
|
+
(entry2) => entry2.command.action_id !== actionId
|
|
5060
5621
|
);
|
|
5061
5622
|
const unreadable = this.unreadableRows.filter((row) => row.actionId !== actionId);
|
|
5062
5623
|
await privateWrite(
|
|
5063
5624
|
this.path,
|
|
5064
|
-
retained.map((
|
|
5625
|
+
retained.map((entry2) => JSON.stringify(storeJournalEntry(entry2)) + "\n").join("") + unreadable.map((row) => row.line + "\n").join("")
|
|
5065
5626
|
);
|
|
5066
5627
|
this.unreadableRows = unreadable;
|
|
5067
|
-
for (const [id,
|
|
5068
|
-
if (
|
|
5628
|
+
for (const [id, entry2] of this.entries) {
|
|
5629
|
+
if (entry2.command.action_id === actionId) this.entries.delete(id);
|
|
5069
5630
|
}
|
|
5070
5631
|
}
|
|
5071
|
-
async append(
|
|
5632
|
+
async append(entry2) {
|
|
5072
5633
|
const handle = await open6(this.path, "a", 384);
|
|
5073
5634
|
try {
|
|
5074
5635
|
await handle.chmod(384);
|
|
5075
|
-
await handle.writeFile(JSON.stringify(storeJournalEntry(
|
|
5636
|
+
await handle.writeFile(JSON.stringify(storeJournalEntry(entry2)) + "\n");
|
|
5076
5637
|
await handle.sync();
|
|
5077
5638
|
} finally {
|
|
5078
5639
|
await handle.close();
|
|
5079
5640
|
}
|
|
5080
|
-
this.entries.set(
|
|
5641
|
+
this.entries.set(entry2.command.command_id, entry2);
|
|
5081
5642
|
}
|
|
5082
5643
|
};
|
|
5083
5644
|
|
|
@@ -5117,12 +5678,12 @@ async function stopUntilSettled(driver, session, folder, requireLive, settleMs)
|
|
|
5117
5678
|
await delay3(STOP_POLL_MS);
|
|
5118
5679
|
}
|
|
5119
5680
|
}
|
|
5120
|
-
function stopUnstarted(journal,
|
|
5681
|
+
function stopUnstarted(journal, command2) {
|
|
5121
5682
|
const launches = [...journal.entries.values()].filter(
|
|
5122
|
-
(
|
|
5683
|
+
(entry2) => entry2.command.action_id === command2.action_id && entry2.command.kind === "launch"
|
|
5123
5684
|
);
|
|
5124
5685
|
if (launches.length === 0) throw new HarnessRefusal("session_not_found");
|
|
5125
|
-
if (launches.some((
|
|
5686
|
+
if (launches.some((entry2) => entry2.receipt === null))
|
|
5126
5687
|
throw new Error("The launch this Stop withdrew is still settling");
|
|
5127
5688
|
return { update: null, conversationId: null };
|
|
5128
5689
|
}
|
|
@@ -5135,12 +5696,12 @@ async function findSession(driver, ref, folder, requireLive) {
|
|
|
5135
5696
|
}
|
|
5136
5697
|
async function launchSession(journal, request) {
|
|
5137
5698
|
const { driver, intent, folder, retry, knownRef, requireLive } = request;
|
|
5138
|
-
const
|
|
5139
|
-
if (
|
|
5699
|
+
const command2 = intent.command;
|
|
5700
|
+
if (command2.kind !== "launch") throw new HarnessRefusal("unsupported_command");
|
|
5140
5701
|
if (driver.reconcileLaunch) {
|
|
5141
|
-
for (const
|
|
5142
|
-
if (
|
|
5143
|
-
intent.observedRefs = [...
|
|
5702
|
+
for (const entry2 of journal.entries.values()) {
|
|
5703
|
+
if (entry2.command.kind === "launch" && entry2.command.action_id === command2.action_id && entry2.observedRefs.length > intent.observedRefs.length)
|
|
5704
|
+
intent.observedRefs = [...entry2.observedRefs];
|
|
5144
5705
|
}
|
|
5145
5706
|
await journal.append(intent);
|
|
5146
5707
|
}
|
|
@@ -5153,7 +5714,7 @@ async function launchSession(journal, request) {
|
|
|
5153
5714
|
if (saved && !knownRef) {
|
|
5154
5715
|
session = await driver.reconcileLaunch(
|
|
5155
5716
|
saved,
|
|
5156
|
-
|
|
5717
|
+
command2,
|
|
5157
5718
|
folder,
|
|
5158
5719
|
requireLive,
|
|
5159
5720
|
request.beforeStart
|
|
@@ -5162,10 +5723,10 @@ async function launchSession(journal, request) {
|
|
|
5162
5723
|
const sessions = await driver.list(
|
|
5163
5724
|
folder,
|
|
5164
5725
|
requireLive,
|
|
5165
|
-
knownRef ? { refs: [knownRef] } : { name:
|
|
5726
|
+
knownRef ? { refs: [knownRef] } : { name: command2.session_name }
|
|
5166
5727
|
);
|
|
5167
5728
|
const matching = sessions.filter(
|
|
5168
|
-
(row) => knownRef ? row.id === knownRef : row.name ===
|
|
5729
|
+
(row) => knownRef ? row.id === knownRef : row.name === command2.session_name
|
|
5169
5730
|
);
|
|
5170
5731
|
if (matching.length > 1) throw new HarnessRefusal("start_uncertain");
|
|
5171
5732
|
session = matching[0];
|
|
@@ -5174,7 +5735,7 @@ async function launchSession(journal, request) {
|
|
|
5174
5735
|
await checkpoint(session.id);
|
|
5175
5736
|
session = await driver.reconcileLaunch(
|
|
5176
5737
|
session.id,
|
|
5177
|
-
|
|
5738
|
+
command2,
|
|
5178
5739
|
folder,
|
|
5179
5740
|
requireLive,
|
|
5180
5741
|
request.beforeStart
|
|
@@ -5186,18 +5747,18 @@ async function launchSession(journal, request) {
|
|
|
5186
5747
|
promptCheck(requireLive)();
|
|
5187
5748
|
const preparedFolder = await request.beforeStart();
|
|
5188
5749
|
promptCheck(requireLive)();
|
|
5189
|
-
const ref = await driver.start(
|
|
5750
|
+
const ref = await driver.start(command2, preparedFolder, requireLive, checkpoint);
|
|
5190
5751
|
return { ref };
|
|
5191
5752
|
}
|
|
5192
5753
|
async function resumeSession(journal, request) {
|
|
5193
5754
|
const { driver, intent, session, folder, retry, requireLive } = request;
|
|
5194
|
-
const
|
|
5195
|
-
if (
|
|
5755
|
+
const command2 = intent.command;
|
|
5756
|
+
if (command2.kind !== "resume") throw new HarnessRefusal("unsupported_command");
|
|
5196
5757
|
if (driver.resumeRecovery) {
|
|
5197
5758
|
if (retry && intent.conversationId !== null)
|
|
5198
5759
|
return driver.resumeRecovery.reconcile(
|
|
5199
5760
|
session,
|
|
5200
|
-
|
|
5761
|
+
command2.text ?? null,
|
|
5201
5762
|
intent.observedRefs,
|
|
5202
5763
|
folder,
|
|
5203
5764
|
requireLive
|
|
@@ -5206,7 +5767,7 @@ async function resumeSession(journal, request) {
|
|
|
5206
5767
|
intent.conversationId = session.conversationId;
|
|
5207
5768
|
await journal.append(intent);
|
|
5208
5769
|
promptCheck(requireLive)();
|
|
5209
|
-
return driver.resume(session,
|
|
5770
|
+
return driver.resume(session, command2.text ?? null, folder, requireLive);
|
|
5210
5771
|
}
|
|
5211
5772
|
const sessions = await driver.list(folder);
|
|
5212
5773
|
if (retry) {
|
|
@@ -5220,7 +5781,7 @@ async function resumeSession(journal, request) {
|
|
|
5220
5781
|
intent.observedRefs = sessions.filter((row) => row.conversationId === session.conversationId).map((row) => row.id);
|
|
5221
5782
|
await journal.append({ ...intent, conversationId: session.conversationId });
|
|
5222
5783
|
promptCheck(requireLive)();
|
|
5223
|
-
return await driver.resume(session,
|
|
5784
|
+
return await driver.resume(session, command2.text ?? null, folder, requireLive);
|
|
5224
5785
|
}
|
|
5225
5786
|
async function observeSessions(drivers, contexts, activity = false) {
|
|
5226
5787
|
const contextList = [...contexts];
|
|
@@ -5248,9 +5809,9 @@ async function observeSessions(drivers, contexts, activity = false) {
|
|
|
5248
5809
|
}))
|
|
5249
5810
|
);
|
|
5250
5811
|
for (const folder of listing.folders) {
|
|
5251
|
-
const
|
|
5812
|
+
const canonical3 = await realLocation(folder);
|
|
5252
5813
|
folders.set(folder, {
|
|
5253
|
-
sessions: canonicalSessions.filter((row) => row.folder ===
|
|
5814
|
+
sessions: canonicalSessions.filter((row) => row.folder === canonical3).map((row) => row.session),
|
|
5254
5815
|
listed: listing.listed
|
|
5255
5816
|
});
|
|
5256
5817
|
}
|
|
@@ -5290,19 +5851,19 @@ function unreadState(binding) {
|
|
|
5290
5851
|
}
|
|
5291
5852
|
|
|
5292
5853
|
// src/command-bindings.ts
|
|
5293
|
-
function rememberBinding(
|
|
5294
|
-
const { command, update } =
|
|
5295
|
-
if (!update || !
|
|
5854
|
+
function rememberBinding(entry2, previous) {
|
|
5855
|
+
const { command: command2, update } = entry2;
|
|
5856
|
+
if (!update || !entry2.checkout && !previous) return void 0;
|
|
5296
5857
|
return {
|
|
5297
|
-
harness:
|
|
5298
|
-
folder:
|
|
5299
|
-
checkout:
|
|
5858
|
+
harness: command2.kind === "launch" ? command2.harness : previous.harness,
|
|
5859
|
+
folder: entry2.checkout?.folder ?? previous.folder,
|
|
5860
|
+
checkout: entry2.checkout ? checkoutReport(entry2.checkout) : previous?.checkout,
|
|
5300
5861
|
state: update.state,
|
|
5301
5862
|
missingPolls: 0,
|
|
5302
5863
|
unreadablePolls: 0,
|
|
5303
|
-
repository:
|
|
5864
|
+
repository: command2.kind === "launch" ? repositoryKey(command2.repository) : previous.repository,
|
|
5304
5865
|
ref: update.resumed_as ?? update.session_ref,
|
|
5305
|
-
conversationId:
|
|
5866
|
+
conversationId: entry2.conversationId
|
|
5306
5867
|
};
|
|
5307
5868
|
}
|
|
5308
5869
|
|
|
@@ -5318,29 +5879,29 @@ async function prepareLaunchCheckout({
|
|
|
5318
5879
|
}) {
|
|
5319
5880
|
const entries = [...journal.entries.values()];
|
|
5320
5881
|
const launch = intent.command.kind === "resume" ? entries.find(
|
|
5321
|
-
(
|
|
5882
|
+
(entry2) => entry2.command.action_id === intent.command.action_id && entry2.command.kind === "launch"
|
|
5322
5883
|
) : intent;
|
|
5323
|
-
const
|
|
5324
|
-
if (
|
|
5884
|
+
const command2 = launch?.command;
|
|
5885
|
+
if (command2?.kind !== "launch") throw new HarnessRefusal("unsupported_command");
|
|
5325
5886
|
if (!intent.checkout) {
|
|
5326
|
-
const previous = entries.reverse().find((
|
|
5887
|
+
const previous = entries.reverse().find((entry2) => entry2.command.action_id === command2.action_id && entry2.checkout);
|
|
5327
5888
|
if (previous?.checkout) {
|
|
5328
5889
|
intent.checkout = previous.checkout;
|
|
5329
5890
|
await journal.append(intent);
|
|
5330
5891
|
}
|
|
5331
5892
|
}
|
|
5332
5893
|
if (!intent.checkout || revalidate) {
|
|
5333
|
-
const clone = intent.checkout?.clone ?? (await repositories())[launchRepositoryKey(
|
|
5894
|
+
const clone = intent.checkout?.clone ?? (await repositories())[launchRepositoryKey(command2.repository)];
|
|
5334
5895
|
if (!clone) throw new HarnessRefusal("repository_not_registered");
|
|
5335
5896
|
intent.checkout = await checkout.prepare({
|
|
5336
|
-
checkout:
|
|
5337
|
-
repository:
|
|
5897
|
+
checkout: command2.checkout,
|
|
5898
|
+
repository: command2.repository,
|
|
5338
5899
|
clone,
|
|
5339
5900
|
live: {
|
|
5340
5901
|
folders: () => [
|
|
5341
5902
|
...[...bindings.values()].filter((binding) => !isTerminalSessionState(binding.state)).map((binding) => binding.folder),
|
|
5342
5903
|
...[...journal.entries.values()].flatMap(
|
|
5343
|
-
(
|
|
5904
|
+
(entry2) => (entry2.command.kind === "launch" || entry2.command.kind === "resume") && entry2.command.command_id !== intent.command.command_id && entry2.receipt === null && entry2.checkout && (!entry2.update || !isTerminalSessionState(entry2.update.state)) ? [entry2.checkout.folder] : []
|
|
5344
5905
|
)
|
|
5345
5906
|
]
|
|
5346
5907
|
},
|
|
@@ -5353,18 +5914,18 @@ async function prepareLaunchCheckout({
|
|
|
5353
5914
|
}
|
|
5354
5915
|
|
|
5355
5916
|
// src/command-identity.ts
|
|
5356
|
-
function hasUncertainLaunch(journal,
|
|
5917
|
+
function hasUncertainLaunch(journal, command2) {
|
|
5357
5918
|
const earlier = [...journal.entries.values()].filter(
|
|
5358
|
-
(
|
|
5919
|
+
(entry2) => entry2.command.action_id === command2.action_id && entry2.command.command_id !== command2.command_id && entry2.command.kind === "launch"
|
|
5359
5920
|
);
|
|
5360
|
-
for (const
|
|
5361
|
-
if (
|
|
5921
|
+
for (const entry2 of earlier) {
|
|
5922
|
+
if (entry2.command.kind === "launch" && (entry2.command.seed !== command2.seed || entry2.command.harness !== command2.harness || repositoryKey(entry2.command.repository) !== repositoryKey(command2.repository) || JSON.stringify(entry2.command.checkout) !== JSON.stringify(command2.checkout)))
|
|
5362
5923
|
throw new Error("Action identity conflict");
|
|
5363
5924
|
}
|
|
5364
|
-
return earlier.some((
|
|
5925
|
+
return earlier.some((entry2) => entry2.receipt === null && entry2.checkout !== null);
|
|
5365
5926
|
}
|
|
5366
|
-
function startsAsRetry(journal,
|
|
5367
|
-
return
|
|
5927
|
+
function startsAsRetry(journal, command2, previous) {
|
|
5928
|
+
return command2.kind === "launch" ? Boolean(previous?.checkout) || hasUncertainLaunch(journal, command2) : Boolean(previous);
|
|
5368
5929
|
}
|
|
5369
5930
|
|
|
5370
5931
|
// src/command-observation.ts
|
|
@@ -5393,8 +5954,8 @@ var STARTUP_REASONS = {
|
|
|
5393
5954
|
exited: "shell_startup_failed",
|
|
5394
5955
|
folder_unavailable: "repository_folder_unavailable"
|
|
5395
5956
|
};
|
|
5396
|
-
function unreadableCommandRefusal(
|
|
5397
|
-
return new HarnessRefusal(
|
|
5957
|
+
function unreadableCommandRefusal(command2) {
|
|
5958
|
+
return new HarnessRefusal(command2.kind === "stop" ? "stop_failed" : "start_uncertain");
|
|
5398
5959
|
}
|
|
5399
5960
|
function provenRefusal(error, retry) {
|
|
5400
5961
|
if (error instanceof HarnessRefusal) return error;
|
|
@@ -5419,7 +5980,7 @@ var Commands = class {
|
|
|
5419
5980
|
})
|
|
5420
5981
|
);
|
|
5421
5982
|
this.lifetimes = new CommandLifetimes(journal, options.clock ?? lifetimeClock);
|
|
5422
|
-
for (const
|
|
5983
|
+
for (const entry2 of journal.entries.values()) this.remember(entry2);
|
|
5423
5984
|
}
|
|
5424
5985
|
stopSettleMs;
|
|
5425
5986
|
activity;
|
|
@@ -5428,12 +5989,12 @@ var Commands = class {
|
|
|
5428
5989
|
lifetimes;
|
|
5429
5990
|
resolutions = /* @__PURE__ */ new Map();
|
|
5430
5991
|
/** Called at socket receipt, before joining the serial work queue. A Stop withdraws its action's start at once. */
|
|
5431
|
-
receive(
|
|
5432
|
-
return this.lifetimes.receive(
|
|
5992
|
+
receive(command2) {
|
|
5993
|
+
return this.lifetimes.receive(command2);
|
|
5433
5994
|
}
|
|
5434
|
-
remember(
|
|
5435
|
-
const action =
|
|
5436
|
-
const binding = rememberBinding(
|
|
5995
|
+
remember(entry2) {
|
|
5996
|
+
const action = entry2.command.action_id;
|
|
5997
|
+
const binding = rememberBinding(entry2, this.bindings.get(action));
|
|
5437
5998
|
if (binding) this.bindings.set(action, binding);
|
|
5438
5999
|
}
|
|
5439
6000
|
update(harness, action, ref, state, resumedAs) {
|
|
@@ -5442,18 +6003,18 @@ var Commands = class {
|
|
|
5442
6003
|
checkout: this.bindings.get(action)?.checkout ?? null
|
|
5443
6004
|
};
|
|
5444
6005
|
}
|
|
5445
|
-
async process(
|
|
5446
|
-
const previous = this.journal.entries.get(
|
|
5447
|
-
if (previous && commandDigest(previous.command) !== commandDigest(
|
|
6006
|
+
async process(command2, accept) {
|
|
6007
|
+
const previous = this.journal.entries.get(command2.command_id);
|
|
6008
|
+
if (previous && commandDigest(previous.command) !== commandDigest(command2))
|
|
5448
6009
|
throw new Error("Command identity conflict");
|
|
5449
|
-
if (previous?.receipt && !this.resolutions.has(
|
|
6010
|
+
if (previous?.receipt && !this.resolutions.has(command2.command_id))
|
|
5450
6011
|
return await this.replay(previous);
|
|
5451
|
-
const lifetime = this.receive(
|
|
5452
|
-
const decision = this.resolutions.get(
|
|
6012
|
+
const lifetime = this.receive(command2);
|
|
6013
|
+
const decision = this.resolutions.get(command2.command_id) ?? (lifetime.remaining() > 0 ? await accept(command2, lifetime.remaining()) : true);
|
|
5453
6014
|
if (decision === false) return [];
|
|
5454
6015
|
const intent = {
|
|
5455
6016
|
...previous ?? {
|
|
5456
|
-
command,
|
|
6017
|
+
command: command2,
|
|
5457
6018
|
checkout: null,
|
|
5458
6019
|
receipt: null,
|
|
5459
6020
|
update: null,
|
|
@@ -5464,29 +6025,29 @@ var Commands = class {
|
|
|
5464
6025
|
lifetime: lifetime.checkpoint()
|
|
5465
6026
|
};
|
|
5466
6027
|
if (decision !== true) {
|
|
5467
|
-
this.resolutions.set(
|
|
6028
|
+
this.resolutions.set(command2.command_id, decision);
|
|
5468
6029
|
await this.journal.append({ ...intent, receipt: decision });
|
|
5469
|
-
this.resolutions.delete(
|
|
6030
|
+
this.resolutions.delete(command2.command_id);
|
|
5470
6031
|
return [];
|
|
5471
6032
|
}
|
|
5472
6033
|
await this.journal.append(intent);
|
|
5473
6034
|
let update = null, conversationId = intent.conversationId;
|
|
5474
6035
|
let receipt;
|
|
5475
|
-
this.lifetimes.dequeued(
|
|
6036
|
+
this.lifetimes.dequeued(command2.command_id);
|
|
5476
6037
|
try {
|
|
5477
|
-
const retry = startsAsRetry(this.journal,
|
|
6038
|
+
const retry = startsAsRetry(this.journal, command2, previous);
|
|
5478
6039
|
if (retry) lifetime.uncertainStart();
|
|
5479
|
-
else lifetime.requireLive(
|
|
5480
|
-
if (this.journal.unreadableCommandIds.has(
|
|
5481
|
-
throw unreadableCommandRefusal(
|
|
5482
|
-
const result =
|
|
6040
|
+
else lifetime.requireLive(command2);
|
|
6041
|
+
if (this.journal.unreadableCommandIds.has(command2.command_id))
|
|
6042
|
+
throw unreadableCommandRefusal(command2);
|
|
6043
|
+
const result = command2.kind === "launch" ? await this.launch(intent, retry, lifetime) : await this.control(intent, retry, lifetime);
|
|
5483
6044
|
update = result.update;
|
|
5484
6045
|
conversationId = result.conversationId;
|
|
5485
6046
|
receipt = {
|
|
5486
6047
|
kind: "ack",
|
|
5487
6048
|
protocol_version: GATEWAY_PROTOCOL_VERSION,
|
|
5488
|
-
command_id:
|
|
5489
|
-
action_id:
|
|
6049
|
+
command_id: command2.command_id,
|
|
6050
|
+
action_id: command2.action_id
|
|
5490
6051
|
};
|
|
5491
6052
|
} catch (error) {
|
|
5492
6053
|
const refusal = provenRefusal(error, Boolean(previous));
|
|
@@ -5494,15 +6055,15 @@ var Commands = class {
|
|
|
5494
6055
|
receipt = {
|
|
5495
6056
|
kind: "refused",
|
|
5496
6057
|
protocol_version: GATEWAY_PROTOCOL_VERSION,
|
|
5497
|
-
command_id:
|
|
5498
|
-
action_id:
|
|
6058
|
+
command_id: command2.command_id,
|
|
6059
|
+
action_id: command2.action_id,
|
|
5499
6060
|
reason: refusal.reason,
|
|
5500
6061
|
detail: refusalDetail(refusal, intent.checkout)
|
|
5501
6062
|
};
|
|
5502
6063
|
}
|
|
5503
6064
|
const outcome = {
|
|
5504
6065
|
checkout: intent.checkout,
|
|
5505
|
-
command,
|
|
6066
|
+
command: command2,
|
|
5506
6067
|
receipt,
|
|
5507
6068
|
update,
|
|
5508
6069
|
conversationId,
|
|
@@ -5523,10 +6084,10 @@ var Commands = class {
|
|
|
5523
6084
|
}
|
|
5524
6085
|
return [receipt, ...update ? [update] : []];
|
|
5525
6086
|
}
|
|
5526
|
-
async replay(
|
|
5527
|
-
if (!
|
|
5528
|
-
const binding = this.bindings.get(
|
|
5529
|
-
if (
|
|
6087
|
+
async replay(entry2) {
|
|
6088
|
+
if (!entry2.receipt) throw new Error("Cannot replay an unfinished command");
|
|
6089
|
+
const binding = this.bindings.get(entry2.command.action_id);
|
|
6090
|
+
if (entry2.receipt.kind === "refused" || !binding) return [entry2.receipt];
|
|
5530
6091
|
let rows;
|
|
5531
6092
|
try {
|
|
5532
6093
|
const folder = binding.folder;
|
|
@@ -5535,26 +6096,26 @@ var Commands = class {
|
|
|
5535
6096
|
rows = [];
|
|
5536
6097
|
}
|
|
5537
6098
|
const row = rows.find((session) => session.id === binding.ref);
|
|
5538
|
-
const prior =
|
|
6099
|
+
const prior = entry2.update;
|
|
5539
6100
|
const update = prior?.resumed_as === binding.ref ? this.update(
|
|
5540
6101
|
binding.harness,
|
|
5541
|
-
|
|
6102
|
+
entry2.command.action_id,
|
|
5542
6103
|
prior.session_ref,
|
|
5543
6104
|
row?.state ?? unreadState(binding),
|
|
5544
6105
|
binding.ref
|
|
5545
6106
|
) : this.update(
|
|
5546
6107
|
binding.harness,
|
|
5547
|
-
|
|
6108
|
+
entry2.command.action_id,
|
|
5548
6109
|
binding.ref,
|
|
5549
6110
|
row?.state ?? unreadState(binding)
|
|
5550
6111
|
);
|
|
5551
6112
|
update.waiting_for = row?.waitingFor ?? null;
|
|
5552
|
-
return this.emit(
|
|
6113
|
+
return this.emit(entry2.receipt, update);
|
|
5553
6114
|
}
|
|
5554
6115
|
async launch(intent, retry, lifetime) {
|
|
5555
|
-
const
|
|
5556
|
-
if (
|
|
5557
|
-
const requireLive = lifetime.check(
|
|
6116
|
+
const command2 = intent.command;
|
|
6117
|
+
if (command2.kind !== "launch") throw new HarnessRefusal("unsupported_command");
|
|
6118
|
+
const requireLive = lifetime.check(command2);
|
|
5558
6119
|
const preparation = {
|
|
5559
6120
|
journal: this.journal,
|
|
5560
6121
|
checkout: this.checkout,
|
|
@@ -5564,9 +6125,9 @@ var Commands = class {
|
|
|
5564
6125
|
requireLive
|
|
5565
6126
|
};
|
|
5566
6127
|
const folder = await prepareLaunchCheckout(preparation);
|
|
5567
|
-
const driver = await lifetime.unlessWithdrawn(this.drivers.ready(
|
|
5568
|
-
const known = this.bindings.get(
|
|
5569
|
-
if (known && (known.repository !== repositoryKey(
|
|
6128
|
+
const driver = await lifetime.unlessWithdrawn(this.drivers.ready(command2.harness, folder));
|
|
6129
|
+
const known = this.bindings.get(command2.action_id);
|
|
6130
|
+
if (known && (known.repository !== repositoryKey(command2.repository) || known.harness !== command2.harness))
|
|
5570
6131
|
throw new Error("Action identity conflict");
|
|
5571
6132
|
const { ref, session } = await launchSession(this.journal, {
|
|
5572
6133
|
driver,
|
|
@@ -5578,8 +6139,8 @@ var Commands = class {
|
|
|
5578
6139
|
requireLive
|
|
5579
6140
|
});
|
|
5580
6141
|
const update = this.update(
|
|
5581
|
-
|
|
5582
|
-
|
|
6142
|
+
command2.harness,
|
|
6143
|
+
command2.action_id,
|
|
5583
6144
|
ref,
|
|
5584
6145
|
session?.state ?? "starting"
|
|
5585
6146
|
);
|
|
@@ -5588,24 +6149,24 @@ var Commands = class {
|
|
|
5588
6149
|
return { update, conversationId: session?.conversationId ?? null };
|
|
5589
6150
|
}
|
|
5590
6151
|
async control(intent, retry, lifetime) {
|
|
5591
|
-
const
|
|
5592
|
-
if (
|
|
6152
|
+
const command2 = intent.command;
|
|
6153
|
+
if (command2.kind !== "stop" && command2.kind !== "resume")
|
|
5593
6154
|
throw new HarnessRefusal("unsupported_command");
|
|
5594
|
-
const requireLive = lifetime.check(
|
|
5595
|
-
const binding = this.bindings.get(
|
|
5596
|
-
if (
|
|
5597
|
-
return stopUnstarted(this.journal,
|
|
5598
|
-
if (!binding || (
|
|
6155
|
+
const requireLive = lifetime.check(command2);
|
|
6156
|
+
const binding = this.bindings.get(command2.action_id);
|
|
6157
|
+
if (command2.kind === "stop" && !command2.session_ref && !binding)
|
|
6158
|
+
return stopUnstarted(this.journal, command2);
|
|
6159
|
+
if (!binding || (command2.session_ref ?? binding.ref) !== binding.ref)
|
|
5599
6160
|
throw new HarnessRefusal("session_not_found");
|
|
5600
6161
|
const folder = binding.folder;
|
|
5601
6162
|
if (!folder) throw new HarnessRefusal("repository_not_registered");
|
|
5602
6163
|
const driver = this.drivers.get(binding.harness);
|
|
5603
6164
|
const session = await findSession(driver, binding.ref, folder, requireLive);
|
|
5604
|
-
if (
|
|
6165
|
+
if (command2.kind === "stop")
|
|
5605
6166
|
return {
|
|
5606
6167
|
update: this.update(
|
|
5607
6168
|
binding.harness,
|
|
5608
|
-
|
|
6169
|
+
command2.action_id,
|
|
5609
6170
|
session.id,
|
|
5610
6171
|
await stopUntilSettled(driver, session, folder, requireLive, this.stopSettleMs)
|
|
5611
6172
|
),
|
|
@@ -5629,7 +6190,7 @@ var Commands = class {
|
|
|
5629
6190
|
});
|
|
5630
6191
|
const update = this.update(
|
|
5631
6192
|
binding.harness,
|
|
5632
|
-
|
|
6193
|
+
command2.action_id,
|
|
5633
6194
|
session.id,
|
|
5634
6195
|
"starting",
|
|
5635
6196
|
ref === session.id ? void 0 : ref
|
|
@@ -5642,18 +6203,18 @@ var Commands = class {
|
|
|
5642
6203
|
}
|
|
5643
6204
|
/** A hub reconciliation also repairs receipts persisted by an older gateway. */
|
|
5644
6205
|
async reconcile(receipt) {
|
|
5645
|
-
const
|
|
5646
|
-
if (!
|
|
5647
|
-
if (
|
|
6206
|
+
const entry2 = this.journal.entries.get(receipt.command_id);
|
|
6207
|
+
if (!entry2) return;
|
|
6208
|
+
if (entry2.command.action_id !== receipt.action_id)
|
|
5648
6209
|
throw new Error("Receipt identity conflict");
|
|
5649
6210
|
this.resolutions.set(receipt.command_id, receipt);
|
|
5650
|
-
await this.journal.append({ ...
|
|
6211
|
+
await this.journal.append({ ...entry2, receipt });
|
|
5651
6212
|
this.resolutions.delete(receipt.command_id);
|
|
5652
6213
|
}
|
|
5653
6214
|
async retire(actionId) {
|
|
5654
|
-
for (const
|
|
5655
|
-
if (
|
|
5656
|
-
this.resolutions.delete(
|
|
6215
|
+
for (const entry2 of this.journal.entries.values())
|
|
6216
|
+
if (entry2.command.action_id === actionId)
|
|
6217
|
+
this.resolutions.delete(entry2.command.command_id);
|
|
5657
6218
|
this.lifetimes.retire(actionId);
|
|
5658
6219
|
await this.journal.retire(actionId);
|
|
5659
6220
|
this.bindings.delete(actionId);
|
|
@@ -5665,8 +6226,8 @@ var Commands = class {
|
|
|
5665
6226
|
/** Replay every binding's latest receipt before polling, including terminal sessions. */
|
|
5666
6227
|
durableReceipts() {
|
|
5667
6228
|
const latest = /* @__PURE__ */ new Map();
|
|
5668
|
-
for (const
|
|
5669
|
-
latest.set(
|
|
6229
|
+
for (const entry2 of this.journal.entries.values())
|
|
6230
|
+
latest.set(entry2.command.action_id, entry2);
|
|
5670
6231
|
const receipts = [];
|
|
5671
6232
|
for (const action of this.bindings.keys()) {
|
|
5672
6233
|
const receipt = latest.get(action)?.receipt;
|
|
@@ -5679,7 +6240,7 @@ var Commands = class {
|
|
|
5679
6240
|
}
|
|
5680
6241
|
/** Unfinished durable operations are retried locally, even without hub redelivery. */
|
|
5681
6242
|
get pendingIntents() {
|
|
5682
|
-
return [...this.journal.entries.values()].filter((
|
|
6243
|
+
return [...this.journal.entries.values()].filter((entry2) => entry2.receipt === null);
|
|
5683
6244
|
}
|
|
5684
6245
|
get hasLiveBindings() {
|
|
5685
6246
|
return [...this.bindings.values()].some(
|
|
@@ -5718,7 +6279,7 @@ var Commands = class {
|
|
|
5718
6279
|
};
|
|
5719
6280
|
|
|
5720
6281
|
// src/config-watch.ts
|
|
5721
|
-
import { statSync, watch } from "node:fs";
|
|
6282
|
+
import { statSync as statSync2, watch } from "node:fs";
|
|
5722
6283
|
import { basename as basename5, dirname as dirname8 } from "node:path";
|
|
5723
6284
|
var watchDirectory = (directory, listener) => watch(directory, (_, filename) => listener(filename));
|
|
5724
6285
|
function watchConfig(path, changed, options = {}) {
|
|
@@ -5754,8 +6315,61 @@ function watchConfig(path, changed, options = {}) {
|
|
|
5754
6315
|
};
|
|
5755
6316
|
}
|
|
5756
6317
|
function identity(path) {
|
|
5757
|
-
const
|
|
5758
|
-
return
|
|
6318
|
+
const stat6 = statSync2(path, { bigint: true, throwIfNoEntry: false });
|
|
6319
|
+
return stat6 === void 0 ? "absent" : `${stat6.ino}:${stat6.mtimeNs}:${stat6.size}`;
|
|
6320
|
+
}
|
|
6321
|
+
|
|
6322
|
+
// src/connection-state.ts
|
|
6323
|
+
var START = {
|
|
6324
|
+
instruction: "Start the gateway now and at login",
|
|
6325
|
+
command: gateway("start")
|
|
6326
|
+
};
|
|
6327
|
+
var CONNECT = {
|
|
6328
|
+
instruction: "Connect this computer again",
|
|
6329
|
+
command: gateway("connect")
|
|
6330
|
+
};
|
|
6331
|
+
var CONNECTION = {
|
|
6332
|
+
connected: { message: "Connected", text: "Connected to TSP", mark: "pass" },
|
|
6333
|
+
starting: { message: "Starting", text: "Starting", mark: "wait" },
|
|
6334
|
+
reconnecting: { message: "Reconnecting", text: "Reconnecting to TSP", mark: "wait" },
|
|
6335
|
+
waiting: {
|
|
6336
|
+
message: "Waiting for the network or TSP. Retrying\u2026",
|
|
6337
|
+
text: "Waiting for the network or TSP. Retrying\u2026",
|
|
6338
|
+
mark: "wait"
|
|
6339
|
+
},
|
|
6340
|
+
notRunning: { message: "Not running", text: "Not running", mark: "fail", next: START },
|
|
6341
|
+
stopped: { message: "Stopped", text: "Not running", mark: "fail", next: START },
|
|
6342
|
+
refreshFailed: {
|
|
6343
|
+
message: "Token refresh failed",
|
|
6344
|
+
text: "TSP no longer accepts this computer's credential",
|
|
6345
|
+
mark: "fail",
|
|
6346
|
+
next: CONNECT
|
|
6347
|
+
},
|
|
6348
|
+
tokenRejected: {
|
|
6349
|
+
message: "Token rejected after refresh",
|
|
6350
|
+
text: "TSP rejected this computer's credential",
|
|
6351
|
+
mark: "fail",
|
|
6352
|
+
next: CONNECT
|
|
6353
|
+
},
|
|
6354
|
+
accessEnded: {
|
|
6355
|
+
message: "Machine access ended",
|
|
6356
|
+
text: "This computer's access to TSP ended",
|
|
6357
|
+
mark: "fail",
|
|
6358
|
+
next: CONNECT
|
|
6359
|
+
},
|
|
6360
|
+
outdated: {
|
|
6361
|
+
message: "This gateway is outdated",
|
|
6362
|
+
text: "This gateway is outdated",
|
|
6363
|
+
mark: "fail",
|
|
6364
|
+
next: { instruction: "Update it", command: gateway("update") }
|
|
6365
|
+
}
|
|
6366
|
+
};
|
|
6367
|
+
function connectionState(message) {
|
|
6368
|
+
return Object.values(CONNECTION).find((state) => state.message === message) ?? {
|
|
6369
|
+
message,
|
|
6370
|
+
text: message.replace(/\.+$/, ""),
|
|
6371
|
+
mark: "fail"
|
|
6372
|
+
};
|
|
5759
6373
|
}
|
|
5760
6374
|
|
|
5761
6375
|
// src/launch-permits.ts
|
|
@@ -5766,7 +6380,7 @@ var LaunchPermits = class {
|
|
|
5766
6380
|
this.timeout = timeout;
|
|
5767
6381
|
}
|
|
5768
6382
|
pending = null;
|
|
5769
|
-
accept(
|
|
6383
|
+
accept(command2, remainingMs) {
|
|
5770
6384
|
return new Promise((resolve13) => {
|
|
5771
6385
|
if (!this.active() || remainingMs <= 0) {
|
|
5772
6386
|
resolve13(false);
|
|
@@ -5780,8 +6394,8 @@ var LaunchPermits = class {
|
|
|
5780
6394
|
Math.max(1, Math.min(2e4, remainingMs))
|
|
5781
6395
|
);
|
|
5782
6396
|
this.pending = {
|
|
5783
|
-
action:
|
|
5784
|
-
command:
|
|
6397
|
+
action: command2.action_id,
|
|
6398
|
+
command: command2.command_id,
|
|
5785
6399
|
resolve: (decision) => {
|
|
5786
6400
|
clearTimeout(timer);
|
|
5787
6401
|
this.pending = null;
|
|
@@ -5791,8 +6405,8 @@ var LaunchPermits = class {
|
|
|
5791
6405
|
this.send({
|
|
5792
6406
|
kind: "claim",
|
|
5793
6407
|
protocol_version: GATEWAY_PROTOCOL_VERSION,
|
|
5794
|
-
command_id:
|
|
5795
|
-
action_id:
|
|
6408
|
+
command_id: command2.command_id,
|
|
6409
|
+
action_id: command2.action_id
|
|
5796
6410
|
});
|
|
5797
6411
|
});
|
|
5798
6412
|
}
|
|
@@ -5807,10 +6421,201 @@ var LaunchPermits = class {
|
|
|
5807
6421
|
}
|
|
5808
6422
|
};
|
|
5809
6423
|
|
|
6424
|
+
// src/sign-in-schedule.ts
|
|
6425
|
+
var SIGN_IN_SCHEDULE = {
|
|
6426
|
+
closeWaitsMs: [5e3, 5e3, 1e4, 2e4, 4e4],
|
|
6427
|
+
easedWaitMs: 6e4,
|
|
6428
|
+
limitMs: 5 * 6e4
|
|
6429
|
+
};
|
|
6430
|
+
function nextSignInCheck(checks, schedule = SIGN_IN_SCHEDULE) {
|
|
6431
|
+
let elapsed = 0;
|
|
6432
|
+
for (let made = 0; made <= checks; made++) {
|
|
6433
|
+
const wait = schedule.closeWaitsMs[made] ?? schedule.easedWaitMs;
|
|
6434
|
+
if (elapsed + wait > schedule.limitMs) return null;
|
|
6435
|
+
if (made === checks) return wait;
|
|
6436
|
+
elapsed += wait;
|
|
6437
|
+
}
|
|
6438
|
+
return null;
|
|
6439
|
+
}
|
|
6440
|
+
function signInCheckAfter(checks, schedule = SIGN_IN_SCHEDULE) {
|
|
6441
|
+
return checks < schedule.closeWaitsMs.length && nextSignInCheck(checks, schedule) !== null ? "close" : "eased";
|
|
6442
|
+
}
|
|
6443
|
+
var SignInChecks = class {
|
|
6444
|
+
constructor(due, schedule = SIGN_IN_SCHEDULE) {
|
|
6445
|
+
this.due = due;
|
|
6446
|
+
this.schedule = schedule;
|
|
6447
|
+
}
|
|
6448
|
+
tracked = /* @__PURE__ */ new Map();
|
|
6449
|
+
/** Follows a full detection: checks each harness newly found signed out, and forgets the rest. */
|
|
6450
|
+
observe(capabilities) {
|
|
6451
|
+
const signedOut = new Set(
|
|
6452
|
+
capabilities.filter((row) => row.available && row.signed_in === false).map((row) => row.harness)
|
|
6453
|
+
);
|
|
6454
|
+
for (const harness of [...this.tracked.keys()])
|
|
6455
|
+
if (!signedOut.has(harness)) this.forget(harness);
|
|
6456
|
+
for (const harness of signedOut) if (!this.tracked.has(harness)) this.start(harness);
|
|
6457
|
+
}
|
|
6458
|
+
/** Checks closely again from the top: one tracked harness, or every one. */
|
|
6459
|
+
restart(harness) {
|
|
6460
|
+
for (const kind of harness === void 0 ? [...this.tracked.keys()] : [harness]) {
|
|
6461
|
+
if (!this.tracked.has(kind)) continue;
|
|
6462
|
+
this.forget(kind);
|
|
6463
|
+
this.start(kind);
|
|
6464
|
+
}
|
|
6465
|
+
}
|
|
6466
|
+
/** Records a check of `harness` and schedules the next, or stops. */
|
|
6467
|
+
checked(harness) {
|
|
6468
|
+
const entry2 = this.tracked.get(harness);
|
|
6469
|
+
if (!entry2) return;
|
|
6470
|
+
entry2.checks += 1;
|
|
6471
|
+
this.arm(harness, entry2);
|
|
6472
|
+
}
|
|
6473
|
+
tracks(harness) {
|
|
6474
|
+
return this.tracked.has(harness);
|
|
6475
|
+
}
|
|
6476
|
+
/** Checking this harness has stopped, so nothing reads its sign-in until something asks. */
|
|
6477
|
+
stopped(harness) {
|
|
6478
|
+
const entry2 = this.tracked.get(harness);
|
|
6479
|
+
return entry2 !== void 0 && nextSignInCheck(entry2.checks, this.schedule) === null;
|
|
6480
|
+
}
|
|
6481
|
+
/** How closely a tracked harness is being checked, for its advertisement. */
|
|
6482
|
+
check(harness) {
|
|
6483
|
+
const entry2 = this.tracked.get(harness);
|
|
6484
|
+
return entry2 && signInCheckAfter(entry2.checks, this.schedule);
|
|
6485
|
+
}
|
|
6486
|
+
dispose() {
|
|
6487
|
+
for (const harness of [...this.tracked.keys()]) this.forget(harness);
|
|
6488
|
+
}
|
|
6489
|
+
start(harness) {
|
|
6490
|
+
const entry2 = { checks: 0, timer: void 0 };
|
|
6491
|
+
this.tracked.set(harness, entry2);
|
|
6492
|
+
this.arm(harness, entry2);
|
|
6493
|
+
}
|
|
6494
|
+
arm(harness, entry2) {
|
|
6495
|
+
clearTimeout(entry2.timer);
|
|
6496
|
+
const wait = nextSignInCheck(entry2.checks, this.schedule);
|
|
6497
|
+
entry2.timer = wait === null ? void 0 : setTimeout(() => {
|
|
6498
|
+
entry2.timer = void 0;
|
|
6499
|
+
this.due(harness);
|
|
6500
|
+
}, wait);
|
|
6501
|
+
}
|
|
6502
|
+
forget(harness) {
|
|
6503
|
+
clearTimeout(this.tracked.get(harness)?.timer);
|
|
6504
|
+
this.tracked.delete(harness);
|
|
6505
|
+
}
|
|
6506
|
+
};
|
|
6507
|
+
|
|
6508
|
+
// src/machine-facts.ts
|
|
6509
|
+
function machineFacts(options, send, enqueue, state) {
|
|
6510
|
+
let lastAdvertisement;
|
|
6511
|
+
const harnesses = /* @__PURE__ */ new Map();
|
|
6512
|
+
let repositories = [];
|
|
6513
|
+
const publish = (kind) => {
|
|
6514
|
+
const advertisement = {
|
|
6515
|
+
protocol_version: GATEWAY_PROTOCOL_VERSION,
|
|
6516
|
+
gateway_version: options.version,
|
|
6517
|
+
harnesses: [...harnesses.values()].map((row) => {
|
|
6518
|
+
const check = checks.check(row.harness);
|
|
6519
|
+
return check ? { ...row, sign_in_check: check } : row;
|
|
6520
|
+
}),
|
|
6521
|
+
repositories,
|
|
6522
|
+
gateway_install: options.install
|
|
6523
|
+
};
|
|
6524
|
+
const encoded = JSON.stringify(advertisement);
|
|
6525
|
+
if (kind === "hello" || encoded !== lastAdvertisement) {
|
|
6526
|
+
send({ ...advertisement, protocol_version: GATEWAY_PROTOCOL_VERSION, kind });
|
|
6527
|
+
lastAdvertisement = encoded;
|
|
6528
|
+
}
|
|
6529
|
+
};
|
|
6530
|
+
const withTrust = async (quiet, read) => (await options.trust(quiet, read)).map((row) => {
|
|
6531
|
+
const known = repositories.find(
|
|
6532
|
+
(before) => before.harness === row.harness && repositoryKey(before.repository) === repositoryKey(row.repository)
|
|
6533
|
+
);
|
|
6534
|
+
const capability = harnesses.get(row.harness);
|
|
6535
|
+
return {
|
|
6536
|
+
...row,
|
|
6537
|
+
available: known?.available ?? capability?.available ?? false,
|
|
6538
|
+
signed_in: known ? known.signed_in : capability?.signed_in ?? null
|
|
6539
|
+
};
|
|
6540
|
+
});
|
|
6541
|
+
const advertise = async (kind, read = "cached") => {
|
|
6542
|
+
const kinds = options.drivers.kinds();
|
|
6543
|
+
const fresh = kinds.filter((harness) => !checks.stopped(harness));
|
|
6544
|
+
const stopped = kinds.filter((harness) => checks.stopped(harness));
|
|
6545
|
+
for (const row of await Promise.all(
|
|
6546
|
+
fresh.map((harness) => options.drivers.get(harness).detect())
|
|
6547
|
+
))
|
|
6548
|
+
harnesses.set(row.harness, row);
|
|
6549
|
+
const found = await options.repositories(fresh, read);
|
|
6550
|
+
const quiet = stopped.length > 0 ? await withTrust(stopped, read) : [];
|
|
6551
|
+
repositories = kinds.flatMap(
|
|
6552
|
+
(harness) => (fresh.includes(harness) ? found : quiet).filter((row) => row.harness === harness)
|
|
6553
|
+
);
|
|
6554
|
+
checks.observe([...harnesses.values()]);
|
|
6555
|
+
publish(kind);
|
|
6556
|
+
};
|
|
6557
|
+
const refreshFolders = async (harness) => {
|
|
6558
|
+
const rows = await options.repositories([harness], "fresh");
|
|
6559
|
+
repositories = options.drivers.kinds().flatMap(
|
|
6560
|
+
(kind) => (kind === harness ? rows : repositories).filter((row) => row.harness === kind)
|
|
6561
|
+
);
|
|
6562
|
+
};
|
|
6563
|
+
let refreshing = false;
|
|
6564
|
+
let refreshPending = false;
|
|
6565
|
+
const refresh = () => {
|
|
6566
|
+
if (!state.ready()) return;
|
|
6567
|
+
refreshPending = true;
|
|
6568
|
+
if (refreshing) return;
|
|
6569
|
+
refreshing = true;
|
|
6570
|
+
enqueue(async () => {
|
|
6571
|
+
try {
|
|
6572
|
+
refreshPending = false;
|
|
6573
|
+
await advertise("advertise");
|
|
6574
|
+
} finally {
|
|
6575
|
+
refreshing = false;
|
|
6576
|
+
if (refreshPending && state.live()) refresh();
|
|
6577
|
+
}
|
|
6578
|
+
});
|
|
6579
|
+
};
|
|
6580
|
+
const detectSignIn = async (harness, read) => {
|
|
6581
|
+
const detection = await options.drivers.get(harness).detect();
|
|
6582
|
+
harnesses.set(harness, detection);
|
|
6583
|
+
if (!detection.available || detection.signed_in !== false) {
|
|
6584
|
+
checks.observe([...harnesses.values()]);
|
|
6585
|
+
await advertise("advertise", read);
|
|
6586
|
+
return;
|
|
6587
|
+
}
|
|
6588
|
+
if (read === "fresh") await refreshFolders(harness);
|
|
6589
|
+
publish("advertise");
|
|
6590
|
+
};
|
|
6591
|
+
const checks = new SignInChecks((harness) => {
|
|
6592
|
+
enqueue(async () => {
|
|
6593
|
+
if (!checks.tracks(harness)) return;
|
|
6594
|
+
checks.checked(harness);
|
|
6595
|
+
await detectSignIn(harness, "cached");
|
|
6596
|
+
});
|
|
6597
|
+
}, options.signInSchedule);
|
|
6598
|
+
const recheck = (harness) => {
|
|
6599
|
+
enqueue(async () => {
|
|
6600
|
+
checks.restart(harness);
|
|
6601
|
+
if (checks.tracks(harness)) await detectSignIn(harness, "fresh");
|
|
6602
|
+
else await advertise("advertise", "fresh");
|
|
6603
|
+
});
|
|
6604
|
+
};
|
|
6605
|
+
return {
|
|
6606
|
+
advertise,
|
|
6607
|
+
refresh,
|
|
6608
|
+
recheck,
|
|
6609
|
+
restartChecks: () => checks.restart(),
|
|
6610
|
+
stop: () => checks.dispose()
|
|
6611
|
+
};
|
|
6612
|
+
}
|
|
6613
|
+
|
|
5810
6614
|
// src/hub-connection.ts
|
|
5811
6615
|
var STABLE_CONNECTION_MS = 3e4;
|
|
5812
6616
|
var HANDOVER_MS = 2e3;
|
|
5813
6617
|
var REFRESH_LEAD_MS = 15e3;
|
|
6618
|
+
var FACTS_CHECK_MS = 3e4;
|
|
5814
6619
|
function authenticatedSocket(pairing, token) {
|
|
5815
6620
|
const url = new URL(pairing.hub_path, pairing.origin);
|
|
5816
6621
|
url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
|
|
@@ -5858,59 +6663,25 @@ function transportSender(socket, isActive) {
|
|
|
5858
6663
|
};
|
|
5859
6664
|
}
|
|
5860
6665
|
function commandProcessor(commands, permits, retries, send) {
|
|
5861
|
-
return async (
|
|
5862
|
-
if (!retries.ready(
|
|
6666
|
+
return async (command2) => {
|
|
6667
|
+
if (!retries.ready(command2.command_id)) return;
|
|
5863
6668
|
let results;
|
|
5864
6669
|
try {
|
|
5865
6670
|
results = await commands.process(
|
|
5866
|
-
|
|
6671
|
+
command2,
|
|
5867
6672
|
(operation, remainingMs) => permits.accept(operation, remainingMs)
|
|
5868
6673
|
);
|
|
5869
6674
|
} catch {
|
|
5870
6675
|
console.error(
|
|
5871
|
-
`TSP gateway command pending: kind=${
|
|
6676
|
+
`TSP gateway command pending: kind=${command2.kind} command_id=${command2.command_id} action_id=${command2.action_id}`
|
|
5872
6677
|
);
|
|
5873
6678
|
return;
|
|
5874
6679
|
} finally {
|
|
5875
|
-
retries.defer(
|
|
6680
|
+
retries.defer(command2);
|
|
5876
6681
|
}
|
|
5877
6682
|
for (const result of results) send(result);
|
|
5878
6683
|
};
|
|
5879
6684
|
}
|
|
5880
|
-
function machineFacts(options, send, enqueue, state) {
|
|
5881
|
-
let lastAdvertisement;
|
|
5882
|
-
const advertise = async (kind) => {
|
|
5883
|
-
const advertisement = {
|
|
5884
|
-
protocol_version: GATEWAY_PROTOCOL_VERSION,
|
|
5885
|
-
gateway_version: options.version,
|
|
5886
|
-
harnesses: await options.drivers.detect(),
|
|
5887
|
-
repositories: await options.repositories()
|
|
5888
|
-
};
|
|
5889
|
-
const encoded = JSON.stringify(advertisement);
|
|
5890
|
-
if (kind === "hello" || encoded !== lastAdvertisement) {
|
|
5891
|
-
send({ ...advertisement, protocol_version: GATEWAY_PROTOCOL_VERSION, kind });
|
|
5892
|
-
lastAdvertisement = encoded;
|
|
5893
|
-
}
|
|
5894
|
-
};
|
|
5895
|
-
let refreshing = false;
|
|
5896
|
-
let refreshPending = false;
|
|
5897
|
-
const refresh = () => {
|
|
5898
|
-
if (!state.ready()) return;
|
|
5899
|
-
refreshPending = true;
|
|
5900
|
-
if (refreshing) return;
|
|
5901
|
-
refreshing = true;
|
|
5902
|
-
enqueue(async () => {
|
|
5903
|
-
try {
|
|
5904
|
-
refreshPending = false;
|
|
5905
|
-
await advertise("advertise");
|
|
5906
|
-
} finally {
|
|
5907
|
-
refreshing = false;
|
|
5908
|
-
if (refreshPending && state.live()) refresh();
|
|
5909
|
-
}
|
|
5910
|
-
});
|
|
5911
|
-
};
|
|
5912
|
-
return { advertise, refresh };
|
|
5913
|
-
}
|
|
5914
6685
|
function openConnection(options, setup) {
|
|
5915
6686
|
const socket = authenticatedSocket(options.pairing, setup.credential.token);
|
|
5916
6687
|
let isOpen = true;
|
|
@@ -5966,11 +6737,12 @@ async function serve(options, socket, setup, refreshDue) {
|
|
|
5966
6737
|
});
|
|
5967
6738
|
};
|
|
5968
6739
|
const processCommand = commandProcessor(options.commands, permits, retries, send);
|
|
5969
|
-
const
|
|
6740
|
+
const facts = machineFacts(options, send, enqueue, {
|
|
5970
6741
|
ready: () => ready,
|
|
5971
6742
|
live: () => active && !options.signal.aborted
|
|
5972
6743
|
});
|
|
5973
|
-
const
|
|
6744
|
+
const { advertise, refresh } = facts;
|
|
6745
|
+
const factsInterval = setInterval(refresh, options.advertiseMs ?? FACTS_CHECK_MS);
|
|
5974
6746
|
const unwatch = options.watchFacts?.(refresh);
|
|
5975
6747
|
const polling = sessionPolling(
|
|
5976
6748
|
options,
|
|
@@ -5996,7 +6768,7 @@ async function serve(options, socket, setup, refreshDue) {
|
|
|
5996
6768
|
if (options.signal.aborted) abort();
|
|
5997
6769
|
socket.addEventListener("open", () => {
|
|
5998
6770
|
enqueue(async () => {
|
|
5999
|
-
await advertise("hello");
|
|
6771
|
+
await advertise("hello", "fresh");
|
|
6000
6772
|
clearTimeout(timeout);
|
|
6001
6773
|
for (const receipt of options.commands.durableReceipts()) send(receipt);
|
|
6002
6774
|
await predecessor?.handOver();
|
|
@@ -6004,7 +6776,7 @@ async function serve(options, socket, setup, refreshDue) {
|
|
|
6004
6776
|
ready = true;
|
|
6005
6777
|
readyAt = now();
|
|
6006
6778
|
await polling.poll();
|
|
6007
|
-
await status(true,
|
|
6779
|
+
await status(true, CONNECTION.connected.message);
|
|
6008
6780
|
});
|
|
6009
6781
|
});
|
|
6010
6782
|
socket.addEventListener("message", (event) => {
|
|
@@ -6025,6 +6797,10 @@ async function serve(options, socket, setup, refreshDue) {
|
|
|
6025
6797
|
});
|
|
6026
6798
|
return;
|
|
6027
6799
|
}
|
|
6800
|
+
if (message.kind === "recheck_sign_in") {
|
|
6801
|
+
facts.recheck(message.harness);
|
|
6802
|
+
return;
|
|
6803
|
+
}
|
|
6028
6804
|
if (message.kind === "permit" || message.kind === "retire") {
|
|
6029
6805
|
permits.receive(message);
|
|
6030
6806
|
if (message.kind === "permit") {
|
|
@@ -6041,7 +6817,10 @@ async function serve(options, socket, setup, refreshDue) {
|
|
|
6041
6817
|
if (message.kind === "retire") {
|
|
6042
6818
|
retries.retire(message.action_id);
|
|
6043
6819
|
await options.commands.retire(message.action_id);
|
|
6044
|
-
} else
|
|
6820
|
+
} else {
|
|
6821
|
+
await processCommand(message);
|
|
6822
|
+
facts.restartChecks();
|
|
6823
|
+
}
|
|
6045
6824
|
await polling.poll();
|
|
6046
6825
|
await advertise("advertise");
|
|
6047
6826
|
});
|
|
@@ -6061,6 +6840,7 @@ async function serve(options, socket, setup, refreshDue) {
|
|
|
6061
6840
|
);
|
|
6062
6841
|
polling.stop();
|
|
6063
6842
|
clearInterval(factsInterval);
|
|
6843
|
+
facts.stop();
|
|
6064
6844
|
unwatch?.();
|
|
6065
6845
|
clearTimeout(expiry);
|
|
6066
6846
|
clearTimeout(timeout);
|
|
@@ -6091,9 +6871,9 @@ async function json(url, init) {
|
|
|
6091
6871
|
return await response.json();
|
|
6092
6872
|
}
|
|
6093
6873
|
function requiredText(value, key) {
|
|
6094
|
-
const
|
|
6095
|
-
if (typeof
|
|
6096
|
-
return
|
|
6874
|
+
const text5 = isObject(value) ? field(value, key) : null;
|
|
6875
|
+
if (typeof text5 !== "string" || !text5) throw new Error("Invalid OAuth response");
|
|
6876
|
+
return text5;
|
|
6097
6877
|
}
|
|
6098
6878
|
async function discover(origin) {
|
|
6099
6879
|
const value = await json(`${validateOrigin(origin)}/.well-known/oauth-authorization-server`);
|
|
@@ -6118,12 +6898,12 @@ async function tokenExchange(endpoint, form) {
|
|
|
6118
6898
|
throw new TokenAccessDenied(error.message);
|
|
6119
6899
|
throw error;
|
|
6120
6900
|
}
|
|
6121
|
-
const
|
|
6901
|
+
const access4 = requiredText(value, "access_token");
|
|
6122
6902
|
if (!isObject(value)) throw new Error("Invalid OAuth token");
|
|
6123
6903
|
const expiry = field(value, "expires_in"), refresh = field(value, "refresh_token");
|
|
6124
6904
|
if (typeof expiry !== "number" || !Number.isFinite(expiry) || expiry <= 0 || !(refresh === void 0 || typeof refresh === "string"))
|
|
6125
6905
|
throw new Error("Invalid OAuth token lifetime");
|
|
6126
|
-
return { access:
|
|
6906
|
+
return { access: access4, refresh: refresh ?? null, expires: Date.now() + expiry * 1e3 };
|
|
6127
6907
|
}
|
|
6128
6908
|
var OAuthTokens = class {
|
|
6129
6909
|
constructor(pairing, store) {
|
|
@@ -6135,7 +6915,7 @@ var OAuthTokens = class {
|
|
|
6135
6915
|
if (!force && this.current && this.current.expiresAt > Date.now() + 3e4)
|
|
6136
6916
|
return this.current;
|
|
6137
6917
|
const refresh = await this.store.get();
|
|
6138
|
-
if (!refresh) throw new TokenAccessDenied(
|
|
6918
|
+
if (!refresh) throw new TokenAccessDenied(`No gateway token. Run ${gateway("connect")}.`);
|
|
6139
6919
|
const endpoints = await discover(this.pairing.origin);
|
|
6140
6920
|
const tokens = await tokenExchange(
|
|
6141
6921
|
endpoints.token,
|
|
@@ -6200,10 +6980,12 @@ async function pair(origin, store, openBrowser, report = () => {
|
|
|
6200
6980
|
code_challenge: createHash2("sha256").update(verifier).digest("base64url"),
|
|
6201
6981
|
code_challenge_method: "S256"
|
|
6202
6982
|
}).toString();
|
|
6203
|
-
report(
|
|
6204
|
-
|
|
6983
|
+
report([
|
|
6984
|
+
marked("next", "Approve pairing in your browser. If it doesn't open, visit:"),
|
|
6985
|
+
command(url.href, 2)
|
|
6986
|
+
]);
|
|
6205
6987
|
await openBrowser(url.href);
|
|
6206
|
-
report("Waiting for approval (up to 3 minutes)\u2026");
|
|
6988
|
+
report([marked("wait", "Waiting for approval (up to 3 minutes)\u2026")]);
|
|
6207
6989
|
const tokens = await tokenExchange(
|
|
6208
6990
|
endpoints.token,
|
|
6209
6991
|
new URLSearchParams({
|
|
@@ -6254,9 +7036,9 @@ ${url.href}`);
|
|
|
6254
7036
|
}
|
|
6255
7037
|
async function unpair(pairing, store) {
|
|
6256
7038
|
const tokens = new OAuthTokens(pairing, store);
|
|
6257
|
-
let
|
|
7039
|
+
let access4;
|
|
6258
7040
|
try {
|
|
6259
|
-
|
|
7041
|
+
access4 = await tokens.access();
|
|
6260
7042
|
} catch (error) {
|
|
6261
7043
|
if (!(error instanceof TokenAccessDenied))
|
|
6262
7044
|
throw new Error("Could not access TSP; pairing retained for retry", { cause: error });
|
|
@@ -6267,7 +7049,7 @@ async function unpair(pairing, store) {
|
|
|
6267
7049
|
`${pairing.origin}/api/v1/gateway/machines/${pairing.machine_id}`,
|
|
6268
7050
|
{
|
|
6269
7051
|
method: "DELETE",
|
|
6270
|
-
headers: { Authorization: `Bearer ${
|
|
7052
|
+
headers: { Authorization: `Bearer ${access4.token}` },
|
|
6271
7053
|
redirect: "error",
|
|
6272
7054
|
signal: AbortSignal.timeout(2e4)
|
|
6273
7055
|
}
|
|
@@ -6304,8 +7086,8 @@ var IntentRetries = class {
|
|
|
6304
7086
|
this.commands = commands;
|
|
6305
7087
|
this.now = now;
|
|
6306
7088
|
this.random = random;
|
|
6307
|
-
for (const
|
|
6308
|
-
this.schedule(
|
|
7089
|
+
for (const entry2 of commands.pendingIntents)
|
|
7090
|
+
this.schedule(entry2.command.command_id, entry2.attempts);
|
|
6309
7091
|
}
|
|
6310
7092
|
retryAt = /* @__PURE__ */ new Map();
|
|
6311
7093
|
unjournaled = /* @__PURE__ */ new Map();
|
|
@@ -6322,17 +7104,17 @@ var IntentRetries = class {
|
|
|
6322
7104
|
ready(commandId) {
|
|
6323
7105
|
return this.now() >= (this.retryAt.get(commandId) ?? -Infinity);
|
|
6324
7106
|
}
|
|
6325
|
-
defer(
|
|
6326
|
-
const id =
|
|
6327
|
-
const
|
|
6328
|
-
if (
|
|
7107
|
+
defer(command2) {
|
|
7108
|
+
const id = command2.command_id;
|
|
7109
|
+
const entry2 = this.commands.journalEntry(id);
|
|
7110
|
+
if (entry2) {
|
|
6329
7111
|
this.unjournaled.delete(id);
|
|
6330
|
-
if (
|
|
7112
|
+
if (entry2.receipt === null) this.schedule(id, entry2.attempts);
|
|
6331
7113
|
else this.retryAt.delete(id);
|
|
6332
7114
|
return;
|
|
6333
7115
|
}
|
|
6334
7116
|
const attempts = (this.unjournaled.get(id)?.attempts ?? 0) + 1;
|
|
6335
|
-
this.unjournaled.set(id, { command, attempts });
|
|
7117
|
+
this.unjournaled.set(id, { command: command2, attempts });
|
|
6336
7118
|
this.schedule(id, attempts);
|
|
6337
7119
|
}
|
|
6338
7120
|
/** A retired action is withdrawn by the hub: its unjournaled commands must never run. */
|
|
@@ -6344,17 +7126,17 @@ var IntentRetries = class {
|
|
|
6344
7126
|
}
|
|
6345
7127
|
}
|
|
6346
7128
|
async retryPending(process2, isActive) {
|
|
6347
|
-
for (const
|
|
7129
|
+
for (const command2 of this.pending) {
|
|
6348
7130
|
if (!isActive()) break;
|
|
6349
|
-
await process2(
|
|
7131
|
+
await process2(command2);
|
|
6350
7132
|
}
|
|
6351
7133
|
}
|
|
6352
7134
|
get pending() {
|
|
6353
7135
|
const commands = [
|
|
6354
|
-
...this.commands.pendingIntents.map((
|
|
7136
|
+
...this.commands.pendingIntents.map((entry2) => entry2.command),
|
|
6355
7137
|
...[...this.unjournaled.values()].map((held) => held.command)
|
|
6356
7138
|
];
|
|
6357
|
-
const ids = new Set(commands.map((
|
|
7139
|
+
const ids = new Set(commands.map((command2) => command2.command_id));
|
|
6358
7140
|
for (const id of this.retryAt.keys()) if (!ids.has(id)) this.retryAt.delete(id);
|
|
6359
7141
|
return commands;
|
|
6360
7142
|
}
|
|
@@ -6362,6 +7144,12 @@ var IntentRetries = class {
|
|
|
6362
7144
|
|
|
6363
7145
|
// src/daemon.ts
|
|
6364
7146
|
var OUTDATED_RETRY_MS = 60 * 60 * 1e3;
|
|
7147
|
+
var RESTART_RETRY_MS = 1e3;
|
|
7148
|
+
var RESTART_RETRY_WINDOW_MS = 45e3;
|
|
7149
|
+
var RESTART_CLOSE_CODES = /* @__PURE__ */ new Set([
|
|
7150
|
+
WS_CLOSE_CODE.SERVICE_RESTART,
|
|
7151
|
+
GATEWAY_CLOSE_CODE.SERVER_RESTARTING
|
|
7152
|
+
]);
|
|
6365
7153
|
function reconnectDelay(attempt, random = Math.random) {
|
|
6366
7154
|
return Math.floor(Math.min(3e4, 500 * 2 ** Math.min(attempt, 6)) * (0.5 + random() * 0.5));
|
|
6367
7155
|
}
|
|
@@ -6381,24 +7169,30 @@ function reconnectState(options) {
|
|
|
6381
7169
|
if (!options.signal.aborted) throw new Error("Reconnect wait failed");
|
|
6382
7170
|
}
|
|
6383
7171
|
};
|
|
7172
|
+
const now = options.now ?? (() => performance.now());
|
|
6384
7173
|
const state = {
|
|
6385
7174
|
attempt: 0,
|
|
6386
7175
|
force: false,
|
|
6387
7176
|
rejectedToken: false,
|
|
6388
7177
|
closeCode: null,
|
|
6389
7178
|
closeDisposition: null,
|
|
6390
|
-
|
|
6391
|
-
|
|
6392
|
-
options.now ?? (() => performance.now()),
|
|
6393
|
-
options.random ?? Math.random
|
|
6394
|
-
),
|
|
7179
|
+
restartRetryUntil: Number.NEGATIVE_INFINITY,
|
|
7180
|
+
retries: new IntentRetries(options.commands, now, options.random ?? Math.random),
|
|
6395
7181
|
status: (connected, message) => options.status({
|
|
6396
7182
|
connected,
|
|
6397
7183
|
message,
|
|
6398
7184
|
close_code: state.closeCode,
|
|
6399
7185
|
close_disposition: state.closeDisposition
|
|
6400
7186
|
}),
|
|
6401
|
-
backoff: () =>
|
|
7187
|
+
backoff: () => {
|
|
7188
|
+
const delay5 = reconnectDelay(state.attempt++, options.random);
|
|
7189
|
+
return pause(
|
|
7190
|
+
now() < state.restartRetryUntil ? Math.min(delay5, RESTART_RETRY_MS) : delay5
|
|
7191
|
+
);
|
|
7192
|
+
},
|
|
7193
|
+
restarted: () => {
|
|
7194
|
+
state.restartRetryUntil = now() + RESTART_RETRY_WINDOW_MS;
|
|
7195
|
+
},
|
|
6402
7196
|
pause
|
|
6403
7197
|
};
|
|
6404
7198
|
return state;
|
|
@@ -6440,7 +7234,7 @@ async function reconnectLoop(options, handover) {
|
|
|
6440
7234
|
if (step === "halt") return;
|
|
6441
7235
|
if (step === "exit") break;
|
|
6442
7236
|
}
|
|
6443
|
-
await state.status(false,
|
|
7237
|
+
await state.status(false, CONNECTION.stopped.message);
|
|
6444
7238
|
}
|
|
6445
7239
|
async function accessToken(options, state, handover) {
|
|
6446
7240
|
try {
|
|
@@ -6449,11 +7243,10 @@ async function accessToken(options, state, handover) {
|
|
|
6449
7243
|
if (options.signal.aborted) return "exit";
|
|
6450
7244
|
if (error instanceof TokenAccessDenied) {
|
|
6451
7245
|
await handover.predecessor?.retire();
|
|
6452
|
-
await state.status(false,
|
|
7246
|
+
await state.status(false, CONNECTION.refreshFailed.message);
|
|
6453
7247
|
return "halt";
|
|
6454
7248
|
}
|
|
6455
|
-
if (!handover.predecessor)
|
|
6456
|
-
await state.status(false, "Waiting for the network or TSP. Retrying\u2026");
|
|
7249
|
+
if (!handover.predecessor) await state.status(false, CONNECTION.waiting.message);
|
|
6457
7250
|
await state.backoff();
|
|
6458
7251
|
return "retry";
|
|
6459
7252
|
}
|
|
@@ -6464,35 +7257,30 @@ async function afterClose(options, state, result, handover) {
|
|
|
6464
7257
|
const recovery = disposition(result.code);
|
|
6465
7258
|
state.closeCode = result.code;
|
|
6466
7259
|
state.closeDisposition = recovery;
|
|
7260
|
+
if (RESTART_CLOSE_CODES.has(result.code)) state.restarted();
|
|
6467
7261
|
const reconnect = !options.signal.aborted && recovery !== "terminal" && !(recovery === "token" && state.rejectedToken);
|
|
6468
7262
|
console.error(
|
|
6469
7263
|
`TSP gateway socket closed: code=${result.code} disposition=${recovery} reconnect=${reconnect}`
|
|
6470
7264
|
);
|
|
6471
7265
|
if (options.signal.aborted) return "exit";
|
|
6472
7266
|
if (recovery === "outdated") {
|
|
6473
|
-
await state.status(
|
|
6474
|
-
false,
|
|
6475
|
-
"This gateway is outdated. Run npx @treestructuredplanning/gateway@latest update."
|
|
6476
|
-
);
|
|
7267
|
+
await state.status(false, CONNECTION.outdated.message);
|
|
6477
7268
|
await state.pause(OUTDATED_RETRY_MS);
|
|
6478
7269
|
return "retry";
|
|
6479
7270
|
}
|
|
6480
7271
|
if (recovery === "terminal") {
|
|
6481
|
-
await state.status(false,
|
|
7272
|
+
await state.status(false, CONNECTION.accessEnded.message);
|
|
6482
7273
|
return "halt";
|
|
6483
7274
|
}
|
|
6484
7275
|
if (recovery === "token") {
|
|
6485
7276
|
if (state.rejectedToken) {
|
|
6486
|
-
await state.status(
|
|
6487
|
-
false,
|
|
6488
|
-
"Token rejected after refresh. Run tsp-gateway connect again."
|
|
6489
|
-
);
|
|
7277
|
+
await state.status(false, CONNECTION.tokenRejected.message);
|
|
6490
7278
|
return "halt";
|
|
6491
7279
|
}
|
|
6492
7280
|
state.rejectedToken = true;
|
|
6493
7281
|
state.force = true;
|
|
6494
7282
|
}
|
|
6495
|
-
if (!handover.predecessor?.open()) await state.status(false,
|
|
7283
|
+
if (!handover.predecessor?.open()) await state.status(false, CONNECTION.reconnecting.message);
|
|
6496
7284
|
await state.backoff();
|
|
6497
7285
|
return "retry";
|
|
6498
7286
|
}
|
|
@@ -6590,7 +7378,7 @@ async function daemonLock(state) {
|
|
|
6590
7378
|
}
|
|
6591
7379
|
|
|
6592
7380
|
// src/gateway-status.ts
|
|
6593
|
-
import { readFile as readFile6, stat as
|
|
7381
|
+
import { readFile as readFile6, stat as stat5 } from "node:fs/promises";
|
|
6594
7382
|
var GatewayStatus = class {
|
|
6595
7383
|
constructor(path, executor) {
|
|
6596
7384
|
this.path = path;
|
|
@@ -6620,9 +7408,9 @@ var GatewayStatus = class {
|
|
|
6620
7408
|
};
|
|
6621
7409
|
async function daemonConnection(statusPath, since = 0) {
|
|
6622
7410
|
try {
|
|
6623
|
-
const [
|
|
7411
|
+
const [text5, info] = await Promise.all([readFile6(statusPath, "utf8"), stat5(statusPath)]);
|
|
6624
7412
|
if (info.mtimeMs < since) return null;
|
|
6625
|
-
const status = parseJson(
|
|
7413
|
+
const status = parseJson(text5);
|
|
6626
7414
|
if (!isObject(status)) return null;
|
|
6627
7415
|
const pid = field(status, "pid"), message = field(status, "message");
|
|
6628
7416
|
return typeof pid === "number" && pidAlive(pid) && typeof message === "string" ? message : null;
|
|
@@ -6649,7 +7437,7 @@ function readLaunchObservation(value) {
|
|
|
6649
7437
|
// src/installer.ts
|
|
6650
7438
|
import { readFile as readFile7, rm as rm2 } from "node:fs/promises";
|
|
6651
7439
|
import { homedir as homedir9, userInfo as userInfo2 } from "node:os";
|
|
6652
|
-
import { dirname as dirname9, isAbsolute as isAbsolute10, join as join10, win32 } from "node:path";
|
|
7440
|
+
import { dirname as dirname9, isAbsolute as isAbsolute10, join as join10, win32 as win322 } from "node:path";
|
|
6653
7441
|
var label = "com.treestructuredplanning.tsp-gateway";
|
|
6654
7442
|
function xml(value) {
|
|
6655
7443
|
return value.replace(
|
|
@@ -6666,7 +7454,7 @@ function quoted(value) {
|
|
|
6666
7454
|
}
|
|
6667
7455
|
function installDefinition(platform, spec) {
|
|
6668
7456
|
if (![spec.node, spec.entry, spec.config, spec.state].every(
|
|
6669
|
-
platform === "win32" ? (value) =>
|
|
7457
|
+
platform === "win32" ? (value) => win322.isAbsolute(value) || isAbsolute10(value) : isAbsolute10
|
|
6670
7458
|
) || /[\r\n\0]/.test(Object.values(spec).join("")))
|
|
6671
7459
|
throw new Error("Installer requires absolute paths and single-line values");
|
|
6672
7460
|
const args = [
|
|
@@ -6710,7 +7498,7 @@ WantedBy=default.target
|
|
|
6710
7498
|
`;
|
|
6711
7499
|
if (platform === "win32")
|
|
6712
7500
|
return `<?xml version="1.0" encoding="UTF-16"?>
|
|
6713
|
-
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"><RegistrationInfo><Description>TSP gateway ${xml(spec.version)}</Description></RegistrationInfo><Triggers><LogonTrigger><Enabled>true</Enabled><UserId>${xml(spec.user ?? userInfo2().username)}</UserId></LogonTrigger></Triggers><Principals><Principal id="Author"><UserId>${xml(spec.user ?? userInfo2().username)}</UserId><LogonType>InteractiveToken</LogonType><RunLevel>LeastPrivilege</RunLevel></Principal></Principals><Settings><MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy><DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries><StopIfGoingOnBatteries>false</StopIfGoingOnBatteries><ExecutionTimeLimit>PT0S</ExecutionTimeLimit><RestartOnFailure><Interval>PT1M</Interval><Count>3</Count></RestartOnFailure></Settings><Actions Context="Author"><Exec><Command>${xml(spec.node)}</Command><Arguments>${xml(args.slice(1).map(quoted).join(" "))}</Arguments><WorkingDirectory>${xml(
|
|
7501
|
+
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"><RegistrationInfo><Description>TSP gateway ${xml(spec.version)}</Description></RegistrationInfo><Triggers><LogonTrigger><Enabled>true</Enabled><UserId>${xml(spec.user ?? userInfo2().username)}</UserId></LogonTrigger></Triggers><Principals><Principal id="Author"><UserId>${xml(spec.user ?? userInfo2().username)}</UserId><LogonType>InteractiveToken</LogonType><RunLevel>LeastPrivilege</RunLevel></Principal></Principals><Settings><MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy><DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries><StopIfGoingOnBatteries>false</StopIfGoingOnBatteries><ExecutionTimeLimit>PT0S</ExecutionTimeLimit><RestartOnFailure><Interval>PT1M</Interval><Count>3</Count></RestartOnFailure></Settings><Actions Context="Author"><Exec><Command>${xml(spec.node)}</Command><Arguments>${xml(args.slice(1).map(quoted).join(" "))}</Arguments><WorkingDirectory>${xml(win322.isAbsolute(spec.entry) ? win322.dirname(spec.entry) : dirname9(spec.entry))}</WorkingDirectory></Exec></Actions></Task>
|
|
6714
7502
|
`;
|
|
6715
7503
|
throw new Error("Login items support macOS, Linux and Windows");
|
|
6716
7504
|
}
|
|
@@ -6838,18 +7626,18 @@ function keychainOptions(platform = process.platform) {
|
|
|
6838
7626
|
return platform === "linux" ? { linux: { store: "secret-service" } } : {};
|
|
6839
7627
|
}
|
|
6840
7628
|
function keychain(account) {
|
|
6841
|
-
const
|
|
7629
|
+
const entry2 = new AsyncEntry("com.treestructuredplanning.gateway", account, keychainOptions());
|
|
6842
7630
|
return {
|
|
6843
|
-
get: () =>
|
|
6844
|
-
set: (token) =>
|
|
7631
|
+
get: () => entry2.getPassword(),
|
|
7632
|
+
set: (token) => entry2.setPassword(token),
|
|
6845
7633
|
delete: async () => {
|
|
6846
|
-
await
|
|
7634
|
+
await entry2.deletePassword();
|
|
6847
7635
|
}
|
|
6848
7636
|
};
|
|
6849
7637
|
}
|
|
6850
7638
|
|
|
6851
7639
|
// src/setup.ts
|
|
6852
|
-
import { readFile as readFile8 } from "node:fs/promises";
|
|
7640
|
+
import { access as access3, readFile as readFile8 } from "node:fs/promises";
|
|
6853
7641
|
import { dirname as dirname10, join as join11 } from "node:path";
|
|
6854
7642
|
async function pairMachine(host, config) {
|
|
6855
7643
|
const pairing = await pair(
|
|
@@ -6878,7 +7666,7 @@ async function addRepository(host, config, folder) {
|
|
|
6878
7666
|
return saveRepository(host, config, await registerFolder(folder, host.env));
|
|
6879
7667
|
}
|
|
6880
7668
|
async function installLoginItem(host, config, refresh = "always") {
|
|
6881
|
-
if (!config.pairing) throw
|
|
7669
|
+
if (!config.pairing) throw notConnected();
|
|
6882
7670
|
const path = await captureLoginPath(host.env, host.platform, config.launch);
|
|
6883
7671
|
const spec = {
|
|
6884
7672
|
node: host.node,
|
|
@@ -6895,32 +7683,81 @@ async function installLoginItem(host, config, refresh = "always") {
|
|
|
6895
7683
|
await install(spec, host.platform, host.home);
|
|
6896
7684
|
return "installed";
|
|
6897
7685
|
}
|
|
7686
|
+
function notConnected() {
|
|
7687
|
+
return new ActionableError("This computer isn't connected to TSP yet.", {
|
|
7688
|
+
instruction: "Connect it",
|
|
7689
|
+
command: gateway("connect")
|
|
7690
|
+
});
|
|
7691
|
+
}
|
|
6898
7692
|
var PACKAGE_INSTALL_TIMEOUT_MS = 6e5;
|
|
7693
|
+
async function packageVersion(folder) {
|
|
7694
|
+
const manifest = await readFile8(join11(folder, "package.json"), "utf8").then(parseJson).catch(() => null);
|
|
7695
|
+
const version = isObject(manifest) ? field(manifest, "version") : null;
|
|
7696
|
+
return typeof version === "string" && version ? version : null;
|
|
7697
|
+
}
|
|
7698
|
+
async function installedGateway(root) {
|
|
7699
|
+
const printed = root?.code === 0 ? root.stdout.trim() : "";
|
|
7700
|
+
if (!printed) return null;
|
|
7701
|
+
const folder = join11(printed, ...PACKAGE.split("/"));
|
|
7702
|
+
const version = await packageVersion(folder);
|
|
7703
|
+
if (!version) return null;
|
|
7704
|
+
const entry2 = join11(folder, "dist", "cli.js");
|
|
7705
|
+
if (!await access3(entry2).then(
|
|
7706
|
+
() => true,
|
|
7707
|
+
() => false
|
|
7708
|
+
))
|
|
7709
|
+
return null;
|
|
7710
|
+
return { version, entry: entry2 };
|
|
7711
|
+
}
|
|
7712
|
+
async function recoverLoginItem(host, config) {
|
|
7713
|
+
if (!await access3(host.entry).then(
|
|
7714
|
+
() => true,
|
|
7715
|
+
() => false
|
|
7716
|
+
)) {
|
|
7717
|
+
await removeLoginItem(host);
|
|
7718
|
+
return "The gateway's files are missing, so its start at login was turned off.";
|
|
7719
|
+
}
|
|
7720
|
+
const version = await packageVersion(dirname10(dirname10(host.entry)));
|
|
7721
|
+
if (!version) {
|
|
7722
|
+
await removeLoginItem(host);
|
|
7723
|
+
return "The gateway's version couldn't be read, so its start at login was turned off.";
|
|
7724
|
+
}
|
|
7725
|
+
await installLoginItem({ ...host, version }, config, "when-changed");
|
|
7726
|
+
return launchedByNpx(host.entry) ? `It still runs through npx, now at ${version}.` : version !== host.version ? `The gateway now runs the installed copy at ${version}.` : "The gateway keeps its pairing and folders.";
|
|
7727
|
+
}
|
|
6899
7728
|
async function updateGateway(host, config, run2 = execute) {
|
|
6900
|
-
if (!config.pairing) throw
|
|
6901
|
-
let latest = host;
|
|
7729
|
+
if (!config.pairing) throw notConnected();
|
|
6902
7730
|
const npm = host.platform === "win32" ? host.node : "npm";
|
|
6903
7731
|
const npmArgs = host.platform === "win32" ? [join11(dirname10(host.node), "node_modules", "npm", "bin", "npm-cli.js")] : [];
|
|
6904
|
-
|
|
6905
|
-
|
|
6906
|
-
|
|
6907
|
-
|
|
6908
|
-
|
|
6909
|
-
|
|
6910
|
-
|
|
7732
|
+
const install2 = ["install", "--global", `${PACKAGE}@latest`];
|
|
7733
|
+
host.report([marked("wait", "Installing the latest gateway with npm\u2026")]);
|
|
7734
|
+
const installed = await run2(npm, [...npmArgs, ...install2], host.env, void 0, {
|
|
7735
|
+
timeoutMs: PACKAGE_INSTALL_TIMEOUT_MS
|
|
7736
|
+
}).catch(() => null);
|
|
7737
|
+
if (installed?.code !== 0) {
|
|
7738
|
+
const byHand = {
|
|
7739
|
+
instruction: `Install it yourself, then run ${INSTALLED} update`,
|
|
7740
|
+
command: ["npm", ...install2].join(" ")
|
|
7741
|
+
};
|
|
7742
|
+
const recovery = await recoverLoginItem(host, config);
|
|
7743
|
+
throw new ActionableError(`npm couldn't install the latest gateway. ${recovery}`, byHand);
|
|
7744
|
+
}
|
|
7745
|
+
const located = await installedGateway(
|
|
7746
|
+
await run2(npm, [...npmArgs, "root", "--global"], host.env).catch(() => null)
|
|
7747
|
+
);
|
|
7748
|
+
if (!located) {
|
|
7749
|
+
const recovery = await recoverLoginItem(host, config);
|
|
7750
|
+
throw new ActionableError(
|
|
7751
|
+
`npm installed the latest gateway but couldn't find its files. ${recovery}`,
|
|
7752
|
+
{
|
|
7753
|
+
instruction: "Install it yourself, then run tsp-gateway start",
|
|
7754
|
+
command: ["npm", ...install2].join(" ")
|
|
7755
|
+
}
|
|
6911
7756
|
);
|
|
6912
|
-
|
|
6913
|
-
|
|
6914
|
-
|
|
6915
|
-
|
|
6916
|
-
const folder = join11(root.stdout.trim(), "@treestructuredplanning", "gateway");
|
|
6917
|
-
const manifest = parseJson(await readFile8(join11(folder, "package.json"), "utf8"));
|
|
6918
|
-
const version = isObject(manifest) ? field(manifest, "version") : null;
|
|
6919
|
-
if (typeof version !== "string" || !version)
|
|
6920
|
-
throw new Error("Updated gateway has no version.");
|
|
6921
|
-
latest = { ...host, entry: join11(folder, "dist", "cli.js"), version };
|
|
6922
|
-
}
|
|
6923
|
-
await installLoginItem(latest, config);
|
|
7757
|
+
}
|
|
7758
|
+
const { version, entry: entry2 } = located;
|
|
7759
|
+
await installLoginItem({ ...host, entry: entry2, version }, config);
|
|
7760
|
+
return { version, entry: entry2 };
|
|
6924
7761
|
}
|
|
6925
7762
|
async function removeLoginItem(host) {
|
|
6926
7763
|
return await uninstall(host.paths.state, host.platform, host.home) ? "removed" : "absent";
|
|
@@ -6939,10 +7776,9 @@ async function pairingUsable(host, pairing) {
|
|
|
6939
7776
|
return true;
|
|
6940
7777
|
} catch (error) {
|
|
6941
7778
|
if (error instanceof TokenAccessDenied) return false;
|
|
6942
|
-
throw new Error(
|
|
6943
|
-
|
|
6944
|
-
|
|
6945
|
-
);
|
|
7779
|
+
throw new Error("Could not reach TSP to check this computer's pairing. Try again.", {
|
|
7780
|
+
cause: error
|
|
7781
|
+
});
|
|
6946
7782
|
}
|
|
6947
7783
|
}
|
|
6948
7784
|
async function connectFolder(host, config, folder) {
|
|
@@ -6952,18 +7788,23 @@ async function connectFolder(host, config, folder) {
|
|
|
6952
7788
|
} catch (error) {
|
|
6953
7789
|
if (Object.keys(config.repositories).length > 0) return null;
|
|
6954
7790
|
throw new Error(
|
|
6955
|
-
|
|
7791
|
+
`Run ${gateway("connect")} inside a clone of a GitHub repository, or pass its folder.`,
|
|
6956
7792
|
{ cause: error }
|
|
6957
7793
|
);
|
|
6958
7794
|
}
|
|
6959
7795
|
}
|
|
6960
7796
|
async function connect(host, config, folder) {
|
|
6961
|
-
|
|
6962
|
-
|
|
6963
|
-
|
|
6964
|
-
|
|
6965
|
-
|
|
6966
|
-
|
|
7797
|
+
try {
|
|
7798
|
+
const target = await connectFolder(host, config, folder);
|
|
7799
|
+
const kept = config.pairing !== null && await pairingUsable(host, config.pairing);
|
|
7800
|
+
if (!kept) await pairMachine(host, config);
|
|
7801
|
+
const repository = target ? await saveRepository(host, config, target) : null;
|
|
7802
|
+
const loginItem = await installLoginItem(host, config, kept ? "when-changed" : "always");
|
|
7803
|
+
return { pairing: kept ? "kept" : "paired", repository, loginItem };
|
|
7804
|
+
} catch (error) {
|
|
7805
|
+
if (config.pairing) await recoverLoginItem(host, config);
|
|
7806
|
+
throw error;
|
|
7807
|
+
}
|
|
6967
7808
|
}
|
|
6968
7809
|
async function disconnect(host, config) {
|
|
6969
7810
|
const loginItem = await removeLoginItem(host);
|
|
@@ -6988,7 +7829,7 @@ async function folderSetups(drivers, config, repository, executor) {
|
|
|
6988
7829
|
if (found.length === 0) {
|
|
6989
7830
|
const required = here.map((row) => ({
|
|
6990
7831
|
harness: row.harness,
|
|
6991
|
-
|
|
7832
|
+
...drivers.get(row.harness).installStep
|
|
6992
7833
|
}));
|
|
6993
7834
|
setups.push({ slug, ready: [], required, optional: [] });
|
|
6994
7835
|
continue;
|
|
@@ -6998,8 +7839,7 @@ async function folderSetups(drivers, config, repository, executor) {
|
|
|
6998
7839
|
for (const row of found) {
|
|
6999
7840
|
const driver = drivers.get(row.harness);
|
|
7000
7841
|
const own = [];
|
|
7001
|
-
if (row.signed_in !== true)
|
|
7002
|
-
own.push({ harness: row.harness, instruction: driver.signInInstruction });
|
|
7842
|
+
if (row.signed_in !== true) own.push({ harness: row.harness, ...driver.signInStep });
|
|
7003
7843
|
const folder = folders[slug];
|
|
7004
7844
|
if (folder && trustIsStep(row.trust, driver))
|
|
7005
7845
|
own.push({ harness: row.harness, ...driver.repositorySetup(folder) });
|
|
@@ -7015,7 +7855,7 @@ async function folderSetups(drivers, config, repository, executor) {
|
|
|
7015
7855
|
const registered2 = config.repositories[setup.slug];
|
|
7016
7856
|
if (!registered2) return setup;
|
|
7017
7857
|
const root = await previewWorktreeRoot(executor, registered2.folder, config);
|
|
7018
|
-
return { ...setup,
|
|
7858
|
+
return { ...setup, worktree: worktreeView(registered2.folder, root) };
|
|
7019
7859
|
})
|
|
7020
7860
|
);
|
|
7021
7861
|
}
|
|
@@ -7031,69 +7871,180 @@ var REASONS = {
|
|
|
7031
7871
|
"configured direct": "chosen in your launch settings",
|
|
7032
7872
|
"Windows uses direct spawn": "as Windows does"
|
|
7033
7873
|
};
|
|
7874
|
+
var CONNECT_HERE = {
|
|
7875
|
+
instruction: "Run this inside a clone of a GitHub repository to register it",
|
|
7876
|
+
command: gateway("connect")
|
|
7877
|
+
};
|
|
7034
7878
|
function list(names) {
|
|
7035
7879
|
return names.length < 3 ? names.join(" and ") : `${names.slice(0, -1).join(", ")} and ${names[names.length - 1]}`;
|
|
7036
7880
|
}
|
|
7037
|
-
function lowerFirst(
|
|
7038
|
-
return
|
|
7881
|
+
function lowerFirst(value) {
|
|
7882
|
+
return value.charAt(0).toLowerCase() + value.slice(1);
|
|
7039
7883
|
}
|
|
7040
|
-
function
|
|
7041
|
-
return
|
|
7884
|
+
function stepTarget(step) {
|
|
7885
|
+
return step.command ?? step.link;
|
|
7042
7886
|
}
|
|
7043
|
-
function
|
|
7887
|
+
function stepSentence(step, lead) {
|
|
7888
|
+
const sentence = `${lead}, ${lowerFirst(step.instruction)}`;
|
|
7889
|
+
return stepTarget(step) ? sentence.replace(/\.+$/, ":") : sentence;
|
|
7890
|
+
}
|
|
7891
|
+
function stepLines(steps, kind, name, depth) {
|
|
7044
7892
|
return steps.flatMap((step) => {
|
|
7045
|
-
const
|
|
7046
|
-
|
|
7893
|
+
const sentence = kind === "required" ? marked("next", stepSentence(step, `To build with ${name(step.harness)}`), depth) : note(
|
|
7894
|
+
stepSentence(step, `Optional: to also build with ${name(step.harness)}`),
|
|
7895
|
+
depth
|
|
7896
|
+
);
|
|
7897
|
+
const target = stepTarget(step);
|
|
7898
|
+
return target ? [sentence, command(target, depth + 2)] : [sentence];
|
|
7047
7899
|
});
|
|
7048
7900
|
}
|
|
7049
|
-
function setupLines(setups, name) {
|
|
7901
|
+
function setupLines(setups, name, depth = 1) {
|
|
7050
7902
|
return setups.flatMap((setup) => {
|
|
7051
7903
|
const label2 = setup.slug || "This computer";
|
|
7052
7904
|
return [
|
|
7053
|
-
setup.ready.length > 0 ?
|
|
7054
|
-
|
|
7055
|
-
|
|
7056
|
-
|
|
7057
|
-
|
|
7058
|
-
|
|
7059
|
-
|
|
7060
|
-
)
|
|
7905
|
+
setup.ready.length > 0 ? marked(
|
|
7906
|
+
"pass",
|
|
7907
|
+
`${label2}: ready to build with ${list(setup.ready.map(name))}`,
|
|
7908
|
+
depth
|
|
7909
|
+
) : marked("fail", `${label2}: not ready to build yet`, depth),
|
|
7910
|
+
...setup.worktree ? worktreeLines(setup.worktree, depth + 1) : [],
|
|
7911
|
+
...stepLines(setup.required, "required", name, depth + 1),
|
|
7912
|
+
...stepLines(setup.optional, "optional", name, depth + 1)
|
|
7061
7913
|
];
|
|
7062
7914
|
});
|
|
7063
7915
|
}
|
|
7064
7916
|
function registeredLines(registered2, drivers) {
|
|
7065
7917
|
return [
|
|
7066
|
-
`Registered ${registered2.slug}
|
|
7918
|
+
marked("pass", `Registered ${registered2.slug}`),
|
|
7919
|
+
note("The running gateway picks up the change automatically.", 1),
|
|
7067
7920
|
...stepLines(
|
|
7068
7921
|
drivers.repositorySetups(registered2.folder),
|
|
7069
|
-
|
|
7070
|
-
drivers.displayName
|
|
7922
|
+
"required",
|
|
7923
|
+
drivers.displayName,
|
|
7924
|
+
1
|
|
7925
|
+
),
|
|
7926
|
+
...nextLines(
|
|
7927
|
+
{ instruction: "Start the gateway now and at login", command: gateway("start") },
|
|
7928
|
+
1
|
|
7929
|
+
)
|
|
7930
|
+
];
|
|
7931
|
+
}
|
|
7932
|
+
function gatewayLines(view) {
|
|
7933
|
+
const lines = [heading(`Gateway ${view.version}`)];
|
|
7934
|
+
if (view.machineId === null)
|
|
7935
|
+
return [
|
|
7936
|
+
...lines,
|
|
7937
|
+
marked("fail", "Not paired", 1),
|
|
7938
|
+
...nextLines({ instruction: "Pair this computer", command: gateway("connect") }, 2)
|
|
7939
|
+
];
|
|
7940
|
+
const current = connectionState(view.connection);
|
|
7941
|
+
const last = view.lastMessage !== null && view.lastMessage !== view.connection ? connectionState(view.lastMessage) : null;
|
|
7942
|
+
const next = current.mark === "fail" && last?.next ? last.next : current.next;
|
|
7943
|
+
return [
|
|
7944
|
+
...lines,
|
|
7945
|
+
marked("pass", `Paired as ${view.machineId}`, 1),
|
|
7946
|
+
marked(current.mark, current.text, 1),
|
|
7947
|
+
...last ? [note(`Last report: ${last.text}`, 2)] : [],
|
|
7948
|
+
...next ? nextLines(next, 2) : [],
|
|
7949
|
+
...!view.update ? [] : nextLines(
|
|
7950
|
+
{
|
|
7951
|
+
instruction: `Gateway ${view.update} is available. To update, run`,
|
|
7952
|
+
command: gateway("update")
|
|
7953
|
+
},
|
|
7954
|
+
1
|
|
7071
7955
|
)
|
|
7072
7956
|
];
|
|
7073
7957
|
}
|
|
7074
|
-
function
|
|
7075
|
-
|
|
7958
|
+
function verdictLines(view, setups) {
|
|
7959
|
+
const connection = connectionState(view.connection).mark;
|
|
7960
|
+
const registered2 = setups.filter((setup) => setup.slug !== "");
|
|
7961
|
+
const ready = registered2.filter((setup) => setup.ready.length > 0).map((setup) => setup.slug);
|
|
7962
|
+
if (view.machineId === null || connection === "fail" || registered2.length > 0 && !ready.length)
|
|
7963
|
+
return [marked("fail", "Not ready to build yet. Follow the \u2192 steps above.")];
|
|
7964
|
+
if (registered2.length === 0)
|
|
7965
|
+
return [
|
|
7966
|
+
marked("fail", "No repository folder is registered yet."),
|
|
7967
|
+
...nextLines(CONNECT_HERE, 1)
|
|
7968
|
+
];
|
|
7969
|
+
if (connection === "wait")
|
|
7970
|
+
return [
|
|
7971
|
+
marked(
|
|
7972
|
+
"wait",
|
|
7973
|
+
`Ready to build in ${list(ready)} once the gateway connects. Check with:`
|
|
7974
|
+
),
|
|
7975
|
+
command(gateway("status"), 2)
|
|
7976
|
+
];
|
|
7977
|
+
return [marked("pass", `Ready to build in ${list(ready)}.`)];
|
|
7978
|
+
}
|
|
7979
|
+
function folderSection(setups, name) {
|
|
7980
|
+
return [blank, heading("Folders"), ...setupLines(setups, name)];
|
|
7076
7981
|
}
|
|
7077
7982
|
function connectLines(view, setups, name) {
|
|
7078
7983
|
return [
|
|
7079
|
-
|
|
7080
|
-
...
|
|
7081
|
-
|
|
7984
|
+
...gatewayLines(view),
|
|
7985
|
+
...folderSection(setups, name),
|
|
7986
|
+
blank,
|
|
7987
|
+
...verdictLines(view, setups),
|
|
7988
|
+
note(
|
|
7989
|
+
`${gateway("stop")}, start and update manage the background gateway. Pairing and folders stay.`
|
|
7990
|
+
)
|
|
7991
|
+
];
|
|
7992
|
+
}
|
|
7993
|
+
async function commandPathLines(entry2, env, platform) {
|
|
7994
|
+
const folder = await commandFolderOffPath(entry2, env, platform);
|
|
7995
|
+
if (folder === null) return [];
|
|
7996
|
+
return [
|
|
7997
|
+
marked("wait", `${INSTALLED} isn't on your PATH yet`),
|
|
7998
|
+
note(`npm put it in ${folder}. Add that folder to your PATH to run it by name.`, 1)
|
|
7082
7999
|
];
|
|
7083
8000
|
}
|
|
7084
8001
|
function statusLines(view, setups, name) {
|
|
7085
8002
|
const reason = REASONS[view.launch.reason] ?? view.launch.reason;
|
|
7086
|
-
const how = view.launch.effective_mode === "login-shell" && view.launch.shell !== null ? `Sessions start through your login shell, ${view.launch.shell} (${reason}).` : `Sessions start without a login shell (${reason}).`;
|
|
7087
8003
|
return [
|
|
7088
|
-
|
|
7089
|
-
...
|
|
7090
|
-
|
|
7091
|
-
|
|
7092
|
-
|
|
8004
|
+
...gatewayLines(view),
|
|
8005
|
+
...folderSection(setups, name),
|
|
8006
|
+
blank,
|
|
8007
|
+
heading("Sessions"),
|
|
8008
|
+
text4(
|
|
8009
|
+
view.launch.effective_mode === "login-shell" && view.launch.shell !== null ? `Start through your login shell, ${view.launch.shell} (${reason})` : `Start without a login shell (${reason})`,
|
|
8010
|
+
1
|
|
8011
|
+
),
|
|
8012
|
+
...view.launch.wrapper === null ? [] : [text4(`Wrapper: ${view.launch.wrapper}`, 1)],
|
|
8013
|
+
text4(
|
|
8014
|
+
view.reporting === "off" ? "Activity reporting off: TSP sees only each session's state" : "Activity reporting on: TSP sees each session's kind of work and its counts of files edited and commands run",
|
|
8015
|
+
1
|
|
8016
|
+
),
|
|
8017
|
+
blank,
|
|
8018
|
+
...verdictLines(view, setups)
|
|
7093
8019
|
];
|
|
7094
8020
|
}
|
|
7095
8021
|
|
|
8022
|
+
// src/release-check.ts
|
|
8023
|
+
var RELEASE = /^(\d+)\.(\d+)\.(\d+)$/u;
|
|
8024
|
+
function parts(version) {
|
|
8025
|
+
const match = RELEASE.exec(version);
|
|
8026
|
+
return match ? match.slice(1).map(Number) : null;
|
|
8027
|
+
}
|
|
8028
|
+
async function shippedRelease(origin, fetcher = fetch, timeoutMs = 3e3) {
|
|
8029
|
+
try {
|
|
8030
|
+
const response = await fetcher(`${origin}/api/v1/gateway/release`, {
|
|
8031
|
+
redirect: "error",
|
|
8032
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
8033
|
+
});
|
|
8034
|
+
return response.ok ? decodeRelease(await response.json()).version : null;
|
|
8035
|
+
} catch {
|
|
8036
|
+
return null;
|
|
8037
|
+
}
|
|
8038
|
+
}
|
|
8039
|
+
function newerRelease(running3, shipped) {
|
|
8040
|
+
const have = parts(running3), offered = shipped === null ? null : parts(shipped);
|
|
8041
|
+
if (!have || !offered) return null;
|
|
8042
|
+
const at = have.findIndex((part, index) => part !== offered[index]);
|
|
8043
|
+
return at !== -1 && have[at] < offered[at] ? shipped : null;
|
|
8044
|
+
}
|
|
8045
|
+
|
|
7096
8046
|
export {
|
|
8047
|
+
package_default,
|
|
7097
8048
|
GATEWAY_CLOSE_DISPOSITION,
|
|
7098
8049
|
decodeReporting,
|
|
7099
8050
|
toWire,
|
|
@@ -7104,14 +8055,16 @@ export {
|
|
|
7104
8055
|
decodeHub,
|
|
7105
8056
|
parseJson,
|
|
7106
8057
|
sanitizeWaiting,
|
|
7107
|
-
worktreeRoot,
|
|
7108
|
-
worktreeFolder,
|
|
7109
|
-
excludeLine,
|
|
7110
8058
|
execute,
|
|
7111
8059
|
resolveLaunchPath,
|
|
7112
8060
|
HarnessRefusal,
|
|
7113
8061
|
repositoryKey,
|
|
7114
8062
|
parseRepositoryRemote,
|
|
8063
|
+
TRUST_CACHE_MAX_AGE_MS,
|
|
8064
|
+
TrustCache,
|
|
8065
|
+
worktreeRoot,
|
|
8066
|
+
worktreeFolder,
|
|
8067
|
+
excludeLine,
|
|
7115
8068
|
AcpDriver,
|
|
7116
8069
|
defaultLaunchConfig,
|
|
7117
8070
|
decodeLaunchConfig,
|
|
@@ -7140,6 +8093,25 @@ export {
|
|
|
7140
8093
|
CheckoutPreparer,
|
|
7141
8094
|
previewWorktreeRoot,
|
|
7142
8095
|
checkoutReport,
|
|
8096
|
+
PACKAGE,
|
|
8097
|
+
INSTALLED,
|
|
8098
|
+
gatewayInstall,
|
|
8099
|
+
invocation,
|
|
8100
|
+
gateway,
|
|
8101
|
+
heading,
|
|
8102
|
+
marked,
|
|
8103
|
+
text4 as text,
|
|
8104
|
+
note,
|
|
8105
|
+
command,
|
|
8106
|
+
entry,
|
|
8107
|
+
blank,
|
|
8108
|
+
nextLines,
|
|
8109
|
+
colorEnabled,
|
|
8110
|
+
renderLines,
|
|
8111
|
+
writeLines,
|
|
8112
|
+
ActionableError,
|
|
8113
|
+
UsageError,
|
|
8114
|
+
errorLines,
|
|
7143
8115
|
printWorktreeRoots,
|
|
7144
8116
|
connectWorktreeArgs,
|
|
7145
8117
|
recordConnectRoot,
|
|
@@ -7150,7 +8122,13 @@ export {
|
|
|
7150
8122
|
UNREADABLE_SESSION_POLLS,
|
|
7151
8123
|
Commands,
|
|
7152
8124
|
watchConfig,
|
|
8125
|
+
CONNECTION,
|
|
8126
|
+
connectionState,
|
|
7153
8127
|
LaunchPermits,
|
|
8128
|
+
SIGN_IN_SCHEDULE,
|
|
8129
|
+
nextSignInCheck,
|
|
8130
|
+
signInCheckAfter,
|
|
8131
|
+
SignInChecks,
|
|
7154
8132
|
HANDOVER_MS,
|
|
7155
8133
|
REFRESH_LEAD_MS,
|
|
7156
8134
|
OAuthTokens,
|
|
@@ -7174,6 +8152,7 @@ export {
|
|
|
7174
8152
|
keychain,
|
|
7175
8153
|
addRepository,
|
|
7176
8154
|
installLoginItem,
|
|
8155
|
+
notConnected,
|
|
7177
8156
|
updateGateway,
|
|
7178
8157
|
removeLoginItem,
|
|
7179
8158
|
connect,
|
|
@@ -7182,6 +8161,9 @@ export {
|
|
|
7182
8161
|
setupLines,
|
|
7183
8162
|
registeredLines,
|
|
7184
8163
|
connectLines,
|
|
7185
|
-
|
|
8164
|
+
commandPathLines,
|
|
8165
|
+
statusLines,
|
|
8166
|
+
shippedRelease,
|
|
8167
|
+
newerRelease
|
|
7186
8168
|
};
|
|
7187
|
-
//# sourceMappingURL=shared-
|
|
8169
|
+
//# sourceMappingURL=shared-IDNTQEWS.js.map
|