@kodycodes/cli 3.22.8 → 3.22.9
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/dist/add-mcp-install.d.ts +1 -1
- package/dist/api-token.d.ts +1 -1
- package/dist/{auth-4N35PHF7.js → auth-EEGVQLUH.js} +7 -6
- package/dist/auth-bootstrap.d.ts +1 -1
- package/dist/bin.js +10 -10
- package/dist/capability-proxy.d.ts +1 -1
- package/dist/{chunk-DH5RPHG6.js → chunk-42R5NM3P.js} +1 -1
- package/dist/{chunk-CA7AAWGW.js → chunk-4AKERLPX.js} +3 -3
- package/dist/{chunk-OKIL5IUB.js → chunk-4Q5BFP2W.js} +21 -222
- package/dist/chunk-4Q5BFP2W.js.map +6 -0
- package/dist/{chunk-2Y4VVWIL.js → chunk-5W34SXJW.js} +6 -117
- package/dist/chunk-5W34SXJW.js.map +6 -0
- package/dist/{chunk-WAZTX2ED.js → chunk-7CKXJKIA.js} +7 -7
- package/dist/{chunk-ZU5QBMXR.js → chunk-7JU4GNCS.js} +13 -108
- package/dist/chunk-7JU4GNCS.js.map +6 -0
- package/dist/chunk-BKUM4PLI.js +220 -0
- package/dist/chunk-BKUM4PLI.js.map +6 -0
- package/dist/{chunk-PQPGW43A.js → chunk-BWZ3ZIQO.js} +4 -4
- package/dist/{chunk-DHCDUGD2.js → chunk-EMEUJKXR.js} +2 -2
- package/dist/{chunk-UQIY7RY4.js → chunk-FY4SRIOG.js} +3 -3
- package/dist/{chunk-XAXNNOEC.js → chunk-IIGF3ZWU.js} +62 -58
- package/dist/chunk-IIGF3ZWU.js.map +6 -0
- package/dist/{chunk-T3V6WFHI.js → chunk-JSNRL2K6.js} +2 -166
- package/dist/{chunk-T3V6WFHI.js.map → chunk-JSNRL2K6.js.map} +3 -3
- package/dist/{chunk-LTSHQAUL.js → chunk-KZQLRBP2.js} +4 -4
- package/dist/{chunk-IOG54R4P.js → chunk-NKQRWYSY.js} +4 -2
- package/dist/chunk-NKQRWYSY.js.map +6 -0
- package/dist/chunk-SLHFID52.js +107 -0
- package/dist/chunk-SLHFID52.js.map +6 -0
- package/dist/{chunk-DJ6XKQEC.js → chunk-V4SN6Y7O.js} +2 -2
- package/dist/{chunk-F34Q7D5C.js → chunk-XXCAGPVV.js} +5 -3
- package/dist/chunk-XXCAGPVV.js.map +6 -0
- package/dist/chunk-YFNQZPEP.js +124 -0
- package/dist/chunk-YFNQZPEP.js.map +6 -0
- package/dist/{chunk-SX3MQHCJ.js → chunk-YLDI65PR.js} +151 -151
- package/dist/chunk-YLDI65PR.js.map +6 -0
- package/dist/{chunk-QL776E77.js → chunk-YYCR2HQJ.js} +3 -3
- package/dist/cli.js +10 -10
- package/dist/{commands-QL2SLSIL.js → commands-ZJNROANP.js} +1144 -45
- package/dist/commands-ZJNROANP.js.map +6 -0
- package/dist/detect-running-hosts.d.ts +1 -1
- package/dist/dev/dev-assets.d.ts +1 -1
- package/dist/dev/dev-port.d.ts +1 -1
- package/dist/dev/dev-proxy-origin.d.ts +1 -1
- package/dist/dev/dev-registry.d.ts +1 -1
- package/dist/dev/error-page.d.ts +1 -1
- package/dist/dev/live-reload.d.ts +2 -2
- package/dist/{dev-command-P73A4I3C.js → dev-command-TDLNJ7ZA.js} +18 -16
- package/dist/dev-command-TDLNJ7ZA.js.map +6 -0
- package/dist/dev-command.d.ts +1 -1
- package/dist/discover-login-identity.d.ts +1 -1
- package/dist/execution-context/daemon.d.ts +1 -1
- package/dist/execution-context/package-graph-cache.d.ts +1 -1
- package/dist/execution-context/run-executor.d.ts +6 -6
- package/dist/{git-credential-L5AR3JRT.js → git-credential-QEMS2OZF.js} +8 -7
- package/dist/git-credential-QEMS2OZF.js.map +6 -0
- package/dist/{install-24DK5RBH.js → install-MPB5WEG4.js} +2 -2
- package/dist/install.d.ts +1 -1
- package/dist/{chunk-VA5KCKCC.js → local-execute-3I5YDMM5.js} +27 -13
- package/dist/local-execute-3I5YDMM5.js.map +6 -0
- package/dist/local-execute-auth.d.ts +2 -2
- package/dist/local-execute.d.ts +4 -4
- package/dist/local-gateway-fetch-forward.d.ts +2 -2
- package/dist/{local-package-graph-3TRAIUBT.js → local-package-graph-PMZQD7WF.js} +7 -4
- package/dist/{local-package-run-N3O2SD3L.js → local-package-run-SJIAEVMD.js} +14 -13
- package/dist/local-package-run.d.ts +2 -2
- package/dist/mock/install-mock-fetch-shim.d.ts +1 -1
- package/dist/mock/mock-fetch-gateway.d.ts +1 -1
- package/dist/{mock-dep-resolve-IVZOW4QE.js → mock-dep-resolve-4WSWOQHT.js} +5 -12
- package/dist/mock-dep-resolve-4WSWOQHT.js.map +6 -0
- package/dist/{mock-fetch-gateway-VAGQFWVN.js → mock-fetch-gateway-5K7STXZQ.js} +3 -4
- package/dist/{mock-local-deps-AUAWEK46.js → mock-local-deps-7GS6RPYL.js} +10 -8
- package/dist/{mock-server-7P53AZUZ.js → mock-server-WOSXMC2L.js} +4 -5
- package/dist/oauth-provider.d.ts +1 -1
- package/dist/{open-api-client-QVDDK27F.js → open-api-client-A26G263D.js} +6 -5
- package/dist/open-api-client.d.ts +1 -1
- package/dist/package-app-dev.d.ts +1 -1
- package/dist/{package-manifest-AEBPXNCJ.js → package-manifest-OFJ5PRA2.js} +3 -4
- package/dist/{remote-execute-PBSB32YY.js → remote-execute-PDSZBGQ7.js} +6 -5
- package/dist/remote-execute-PDSZBGQ7.js.map +6 -0
- package/dist/remote-execute.d.ts +2 -2
- package/dist/{run-executor-TSI7J56V.js → run-executor-6HMXYWQ5.js} +3 -21
- package/dist/run-executor-6HMXYWQ5.js.map +6 -0
- package/dist/{setup-git-S57U4NT5.js → setup-git-7PQUU6JB.js} +7 -6
- package/dist/setup-git.d.ts +1 -1
- package/dist/{structured-error-KS24XUSZ.js → structured-error-D2G3PBEZ.js} +2 -3
- package/dist/structured-error.d.ts +1 -1
- package/dist/{chunk-MHN2FS4K.js → target-LQ247AM3.js} +4 -4
- package/dist/target-LQ247AM3.js.map +6 -0
- package/dist/{whoami-RUMTWBZL.js → whoami-JGNVG2UX.js} +5 -4
- package/dist/whoami-JGNVG2UX.js.map +6 -0
- package/package.json +1 -1
- package/dist/chunk-2Y4VVWIL.js.map +0 -6
- package/dist/chunk-F34Q7D5C.js.map +0 -6
- package/dist/chunk-IOG54R4P.js.map +0 -6
- package/dist/chunk-MHN2FS4K.js.map +0 -6
- package/dist/chunk-NZ7JT42A.js +0 -994
- package/dist/chunk-NZ7JT42A.js.map +0 -6
- package/dist/chunk-OKIL5IUB.js.map +0 -6
- package/dist/chunk-SX3MQHCJ.js.map +0 -6
- package/dist/chunk-VA5KCKCC.js.map +0 -6
- package/dist/chunk-XAXNNOEC.js.map +0 -6
- package/dist/chunk-ZU5QBMXR.js.map +0 -6
- package/dist/commands-QL2SLSIL.js.map +0 -6
- package/dist/dev-command-P73A4I3C.js.map +0 -6
- package/dist/git-credential-L5AR3JRT.js.map +0 -6
- package/dist/local-execute-WDKUY2JK.js +0 -39
- package/dist/mock-dep-resolve-IVZOW4QE.js.map +0 -6
- package/dist/remote-execute-PBSB32YY.js.map +0 -6
- package/dist/run-executor-TSI7J56V.js.map +0 -6
- package/dist/structured-error-KS24XUSZ.js.map +0 -6
- package/dist/target-UQBEV64U.js +0 -15
- package/dist/target-UQBEV64U.js.map +0 -6
- package/dist/whoami-RUMTWBZL.js.map +0 -6
- /package/dist/{auth-4N35PHF7.js.map → auth-EEGVQLUH.js.map} +0 -0
- /package/dist/{chunk-DH5RPHG6.js.map → chunk-42R5NM3P.js.map} +0 -0
- /package/dist/{chunk-CA7AAWGW.js.map → chunk-4AKERLPX.js.map} +0 -0
- /package/dist/{chunk-WAZTX2ED.js.map → chunk-7CKXJKIA.js.map} +0 -0
- /package/dist/{chunk-PQPGW43A.js.map → chunk-BWZ3ZIQO.js.map} +0 -0
- /package/dist/{chunk-DHCDUGD2.js.map → chunk-EMEUJKXR.js.map} +0 -0
- /package/dist/{chunk-UQIY7RY4.js.map → chunk-FY4SRIOG.js.map} +0 -0
- /package/dist/{chunk-LTSHQAUL.js.map → chunk-KZQLRBP2.js.map} +0 -0
- /package/dist/{chunk-DJ6XKQEC.js.map → chunk-V4SN6Y7O.js.map} +0 -0
- /package/dist/{chunk-QL776E77.js.map → chunk-YYCR2HQJ.js.map} +0 -0
- /package/dist/{install-24DK5RBH.js.map → install-MPB5WEG4.js.map} +0 -0
- /package/dist/{local-execute-WDKUY2JK.js.map → local-package-graph-PMZQD7WF.js.map} +0 -0
- /package/dist/{local-package-graph-3TRAIUBT.js.map → local-package-run-SJIAEVMD.js.map} +0 -0
- /package/dist/{local-package-run-N3O2SD3L.js.map → mock-fetch-gateway-5K7STXZQ.js.map} +0 -0
- /package/dist/{mock-fetch-gateway-VAGQFWVN.js.map → mock-local-deps-7GS6RPYL.js.map} +0 -0
- /package/dist/{mock-local-deps-AUAWEK46.js.map → mock-server-WOSXMC2L.js.map} +0 -0
- /package/dist/{mock-server-7P53AZUZ.js.map → open-api-client-A26G263D.js.map} +0 -0
- /package/dist/{open-api-client-QVDDK27F.js.map → package-manifest-OFJ5PRA2.js.map} +0 -0
- /package/dist/{package-manifest-AEBPXNCJ.js.map → setup-git-7PQUU6JB.js.map} +0 -0
- /package/dist/{setup-git-S57U4NT5.js.map → structured-error-D2G3PBEZ.js.map} +0 -0
|
@@ -1,31 +1,6 @@
|
|
|
1
1
|
import { createRequire as __createRequire } from 'node:module'; const require = __createRequire(import.meta.url);
|
|
2
|
-
import {
|
|
3
|
-
HostCommands,
|
|
4
|
-
classifyFrameText,
|
|
5
|
-
decodeEnvelopePayload,
|
|
6
|
-
ecpContextKeyId,
|
|
7
|
-
ecpDefaultGrant,
|
|
8
|
-
ecpMaxConcurrentRunsCap,
|
|
9
|
-
ecpPingFrame,
|
|
10
|
-
ecpProtocolVersion,
|
|
11
|
-
ecpRootPublicKeys,
|
|
12
|
-
ecpRunHeader,
|
|
13
|
-
ecpSignatureHeader,
|
|
14
|
-
ecpTestRootPublicKey,
|
|
15
|
-
ed25519JwkThumbprint,
|
|
16
|
-
formatEcpSignatureHeader,
|
|
17
|
-
importEd25519PrivateKey,
|
|
18
|
-
importEd25519PublicKey,
|
|
19
|
-
publishedKeyJwk,
|
|
20
|
-
signEcpHttpRequest,
|
|
21
|
-
signFrame,
|
|
22
|
-
verifyFrame,
|
|
23
|
-
verifyServerKeySet
|
|
24
|
-
} from "./chunk-NZ7JT42A.js";
|
|
25
2
|
import {
|
|
26
3
|
allowLocalPath,
|
|
27
|
-
createLocalWorkerTracker,
|
|
28
|
-
createPackageGraphCache,
|
|
29
4
|
effectiveFs,
|
|
30
5
|
emptyEffectiveFs,
|
|
31
6
|
fsFeatures,
|
|
@@ -33,19 +8,19 @@ import {
|
|
|
33
8
|
localPolicyPath,
|
|
34
9
|
readLocalCommandsEnabled,
|
|
35
10
|
readLocalPolicy,
|
|
36
|
-
reapLeftoverWorkers,
|
|
37
11
|
removeLocalPath,
|
|
38
12
|
setLocalAutoUpdateEnabled,
|
|
39
13
|
setLocalCommandsEnabled,
|
|
40
14
|
windowsCommandLine
|
|
41
|
-
} from "./chunk-
|
|
42
|
-
import
|
|
43
|
-
|
|
44
|
-
|
|
15
|
+
} from "./chunk-JSNRL2K6.js";
|
|
16
|
+
import {
|
|
17
|
+
killWindowsTree,
|
|
18
|
+
windowsFileLaunch,
|
|
19
|
+
windowsShellLaunch
|
|
20
|
+
} from "./chunk-LQNGII7U.js";
|
|
45
21
|
import {
|
|
46
22
|
apiTokenFileStorePath
|
|
47
|
-
} from "./chunk-
|
|
48
|
-
import "./chunk-NHRYNRXG.js";
|
|
23
|
+
} from "./chunk-YFNQZPEP.js";
|
|
49
24
|
import {
|
|
50
25
|
cliConfigDir,
|
|
51
26
|
secretServiceKeyringOptions
|
|
@@ -56,9 +31,347 @@ import {
|
|
|
56
31
|
import "./chunk-UW6AWLXX.js";
|
|
57
32
|
|
|
58
33
|
// src/execution-context/commands.ts
|
|
59
|
-
import { appendFileSync, rmSync as
|
|
34
|
+
import { appendFileSync, rmSync as rmSync5, unwatchFile, watchFile } from "node:fs";
|
|
60
35
|
import { cpus, platform, totalmem } from "node:os";
|
|
61
36
|
|
|
37
|
+
// ../shared/src/base64.ts
|
|
38
|
+
function bytesToBase64(bytes) {
|
|
39
|
+
let binary = "";
|
|
40
|
+
for (const byte of bytes) {
|
|
41
|
+
binary += String.fromCharCode(byte);
|
|
42
|
+
}
|
|
43
|
+
return btoa(binary);
|
|
44
|
+
}
|
|
45
|
+
function base64ToBytes(value) {
|
|
46
|
+
const binary = atob(value);
|
|
47
|
+
return Uint8Array.from(binary, (char) => char.charCodeAt(0));
|
|
48
|
+
}
|
|
49
|
+
function bytesToBase64Url(bytes) {
|
|
50
|
+
return bytesToBase64(bytes).replaceAll("+", "-").replaceAll("/", "_").replaceAll("=", "");
|
|
51
|
+
}
|
|
52
|
+
function base64UrlToBytes(value) {
|
|
53
|
+
const normalized = value.replaceAll("-", "+").replaceAll("_", "/");
|
|
54
|
+
const padded = normalized.padEnd(
|
|
55
|
+
normalized.length + (4 - normalized.length % 4) % 4,
|
|
56
|
+
"="
|
|
57
|
+
);
|
|
58
|
+
return base64ToBytes(padded);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// ../shared/src/execution-context-protocol/envelope.ts
|
|
62
|
+
var ecpFrameSignaturePrefix = "kody-ecp/1\n";
|
|
63
|
+
var ecpKeySetSignaturePrefix = "kody-ecp-keys/1\n";
|
|
64
|
+
var ecpHttpSignaturePrefix = "kody-ecp-http/1\n";
|
|
65
|
+
var ecpContextKeyId = "ctx";
|
|
66
|
+
var ecpPingFrame = '{"t":"ping"}';
|
|
67
|
+
var encoder = new TextEncoder();
|
|
68
|
+
var decoder = new TextDecoder();
|
|
69
|
+
function isRecord(value) {
|
|
70
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
71
|
+
}
|
|
72
|
+
var base64UrlPattern = /^[A-Za-z0-9_-]+$/;
|
|
73
|
+
function normalizeEd25519PublicJwk(value) {
|
|
74
|
+
if (!isRecord(value)) return null;
|
|
75
|
+
if (value.kty !== "OKP" || value.crv !== "Ed25519") return null;
|
|
76
|
+
if (typeof value.x !== "string" || !base64UrlPattern.test(value.x)) {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
if (base64UrlToBytes(value.x).byteLength !== 32) return null;
|
|
80
|
+
return { kty: "OKP", crv: "Ed25519", x: value.x };
|
|
81
|
+
}
|
|
82
|
+
async function ed25519JwkThumbprint(jwk) {
|
|
83
|
+
const canonical = `{"crv":"Ed25519","kty":"OKP","x":"${jwk.x}"}`;
|
|
84
|
+
const digest = await crypto.subtle.digest(
|
|
85
|
+
"SHA-256",
|
|
86
|
+
encoder.encode(canonical)
|
|
87
|
+
);
|
|
88
|
+
return bytesToBase64Url(new Uint8Array(digest));
|
|
89
|
+
}
|
|
90
|
+
async function importEd25519PublicKey(jwk) {
|
|
91
|
+
return await crypto.subtle.importKey(
|
|
92
|
+
"jwk",
|
|
93
|
+
{ kty: jwk.kty, crv: jwk.crv, x: jwk.x },
|
|
94
|
+
{ name: "Ed25519" },
|
|
95
|
+
false,
|
|
96
|
+
["verify"]
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
async function importEd25519PrivateKey(jwk) {
|
|
100
|
+
return await crypto.subtle.importKey(
|
|
101
|
+
"jwk",
|
|
102
|
+
{ kty: jwk.kty, crv: jwk.crv, x: jwk.x, d: jwk.d },
|
|
103
|
+
{ name: "Ed25519" },
|
|
104
|
+
false,
|
|
105
|
+
["sign"]
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
async function signDetached(input) {
|
|
109
|
+
const signature = await crypto.subtle.sign(
|
|
110
|
+
{ name: "Ed25519" },
|
|
111
|
+
input.privateKey,
|
|
112
|
+
encoder.encode(input.prefix + input.message)
|
|
113
|
+
);
|
|
114
|
+
return bytesToBase64Url(new Uint8Array(signature));
|
|
115
|
+
}
|
|
116
|
+
async function verifyDetached(input) {
|
|
117
|
+
if (!base64UrlPattern.test(input.signature)) return false;
|
|
118
|
+
const signature = base64UrlToBytes(input.signature);
|
|
119
|
+
if (signature.byteLength !== 64) return false;
|
|
120
|
+
try {
|
|
121
|
+
return await crypto.subtle.verify(
|
|
122
|
+
{ name: "Ed25519" },
|
|
123
|
+
input.publicKey,
|
|
124
|
+
signature,
|
|
125
|
+
encoder.encode(input.prefix + input.message)
|
|
126
|
+
);
|
|
127
|
+
} catch {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function encodeEnvelopePayload(payload) {
|
|
132
|
+
return bytesToBase64Url(encoder.encode(JSON.stringify(payload)));
|
|
133
|
+
}
|
|
134
|
+
function decodeEnvelopePayload(p) {
|
|
135
|
+
if (!base64UrlPattern.test(p)) return null;
|
|
136
|
+
try {
|
|
137
|
+
return JSON.parse(decoder.decode(base64UrlToBytes(p)));
|
|
138
|
+
} catch {
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
function parseEnvelope(value) {
|
|
143
|
+
if (!isRecord(value)) return null;
|
|
144
|
+
const { p, s, k } = value;
|
|
145
|
+
if (typeof p !== "string" || typeof s !== "string" || typeof k !== "string") {
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
if (!p || !s || !k) return null;
|
|
149
|
+
return { p, s, k };
|
|
150
|
+
}
|
|
151
|
+
async function signEnvelope(input) {
|
|
152
|
+
const p = encodeEnvelopePayload(input.payload);
|
|
153
|
+
const s = await signDetached({
|
|
154
|
+
prefix: input.prefix,
|
|
155
|
+
message: p,
|
|
156
|
+
privateKey: input.privateKey
|
|
157
|
+
});
|
|
158
|
+
return { p, s, k: input.kid };
|
|
159
|
+
}
|
|
160
|
+
async function verifyEnvelope(input) {
|
|
161
|
+
const ok = await verifyDetached({
|
|
162
|
+
prefix: input.prefix,
|
|
163
|
+
message: input.envelope.p,
|
|
164
|
+
signature: input.envelope.s,
|
|
165
|
+
publicKey: input.publicKey
|
|
166
|
+
});
|
|
167
|
+
if (!ok) return null;
|
|
168
|
+
return decodeEnvelopePayload(input.envelope.p);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// ../shared/src/execution-context-protocol/replay.ts
|
|
172
|
+
var ecpFrameClockSkewMs = 6e4;
|
|
173
|
+
function checkNextSeq(lastSeen, received) {
|
|
174
|
+
return Number.isSafeInteger(received) && received === lastSeen + 1 ? "ok" : "replay";
|
|
175
|
+
}
|
|
176
|
+
function checkFrameClock(ts, now) {
|
|
177
|
+
return Number.isFinite(ts) && Math.abs(now - ts) <= ecpFrameClockSkewMs ? "ok" : "clock_skew";
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// ../shared/src/execution-context-protocol/frames.ts
|
|
181
|
+
var ecpProtocolVersion = 1;
|
|
182
|
+
var ecpFrameFormatVersion = 1;
|
|
183
|
+
var ecpMaxFrameBytes = 6 * 1024 * 1024;
|
|
184
|
+
var ecpMaxConcurrentRunsCap = 8;
|
|
185
|
+
var ecpDefaultGrant = {
|
|
186
|
+
fs: [],
|
|
187
|
+
egress: { mode: "public" },
|
|
188
|
+
runtime: { mode: "all" },
|
|
189
|
+
limits: { timeoutMs: 9e4, memoryMb: 512, maxConcurrent: 2 },
|
|
190
|
+
payload: {
|
|
191
|
+
maxRequestBytes: 5 * 1024 * 1024,
|
|
192
|
+
maxResultBytes: 100 * 1024,
|
|
193
|
+
resultContentTypes: ["application/json", "text/plain"]
|
|
194
|
+
},
|
|
195
|
+
commands: { enabled: false }
|
|
196
|
+
};
|
|
197
|
+
var ecpGrantMaximums = {
|
|
198
|
+
timeoutMs: 15 * 6e4,
|
|
199
|
+
memoryMb: 8192,
|
|
200
|
+
maxConcurrent: ecpMaxConcurrentRunsCap,
|
|
201
|
+
maxRequestBytes: 5 * 1024 * 1024,
|
|
202
|
+
maxResultBytes: 1024 * 1024
|
|
203
|
+
};
|
|
204
|
+
function isRecord2(value) {
|
|
205
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
206
|
+
}
|
|
207
|
+
function parseFramePayload(value) {
|
|
208
|
+
if (!isRecord2(value)) return null;
|
|
209
|
+
const { v, t, ec, sid, seq, ts, re, body } = value;
|
|
210
|
+
if (typeof v !== "number" || typeof t !== "string" || !t) return null;
|
|
211
|
+
if (typeof ec !== "string" || typeof sid !== "string") return null;
|
|
212
|
+
if (typeof seq !== "number" || !Number.isSafeInteger(seq)) return null;
|
|
213
|
+
if (typeof ts !== "number" || !Number.isFinite(ts)) return null;
|
|
214
|
+
if (re !== void 0 && (typeof re !== "number" || !Number.isSafeInteger(re)))
|
|
215
|
+
return null;
|
|
216
|
+
if (!isRecord2(body)) return null;
|
|
217
|
+
return {
|
|
218
|
+
v,
|
|
219
|
+
t,
|
|
220
|
+
ec,
|
|
221
|
+
sid,
|
|
222
|
+
seq,
|
|
223
|
+
ts,
|
|
224
|
+
...re === void 0 ? {} : { re },
|
|
225
|
+
body
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
function classifyFrameText(text) {
|
|
229
|
+
if (text.length > ecpMaxFrameBytes) {
|
|
230
|
+
return { kind: "invalid", code: "frame_too_large" };
|
|
231
|
+
}
|
|
232
|
+
if (text === ecpPingFrame) return { kind: "ping" };
|
|
233
|
+
let parsed;
|
|
234
|
+
try {
|
|
235
|
+
parsed = JSON.parse(text);
|
|
236
|
+
} catch {
|
|
237
|
+
return { kind: "invalid", code: "bad_signature" };
|
|
238
|
+
}
|
|
239
|
+
const envelope = parseEnvelope(parsed);
|
|
240
|
+
if (!envelope) return { kind: "invalid", code: "bad_signature" };
|
|
241
|
+
return { kind: "frame", envelope };
|
|
242
|
+
}
|
|
243
|
+
async function signFrame(input) {
|
|
244
|
+
const envelope = await signEnvelope({
|
|
245
|
+
prefix: ecpFrameSignaturePrefix,
|
|
246
|
+
payload: input.payload,
|
|
247
|
+
privateKey: input.privateKey,
|
|
248
|
+
kid: input.kid
|
|
249
|
+
});
|
|
250
|
+
return JSON.stringify(envelope);
|
|
251
|
+
}
|
|
252
|
+
async function verifyFrame(input) {
|
|
253
|
+
if (input.expect.kid !== void 0 && input.envelope.k !== input.expect.kid) {
|
|
254
|
+
return { ok: false, code: "bad_signature" };
|
|
255
|
+
}
|
|
256
|
+
const decoded = await verifyEnvelope({
|
|
257
|
+
prefix: ecpFrameSignaturePrefix,
|
|
258
|
+
envelope: input.envelope,
|
|
259
|
+
publicKey: input.publicKey
|
|
260
|
+
});
|
|
261
|
+
const frame = parseFramePayload(decoded);
|
|
262
|
+
if (!frame || frame.v !== ecpFrameFormatVersion) {
|
|
263
|
+
return { ok: false, code: "bad_signature" };
|
|
264
|
+
}
|
|
265
|
+
if (frame.ec !== input.expect.ec || frame.sid !== input.expect.sid) {
|
|
266
|
+
return { ok: false, code: "bad_signature" };
|
|
267
|
+
}
|
|
268
|
+
if (checkNextSeq(input.expect.lastSeq, frame.seq) !== "ok") {
|
|
269
|
+
return { ok: false, code: "replay" };
|
|
270
|
+
}
|
|
271
|
+
if (checkFrameClock(frame.ts, input.now) !== "ok") {
|
|
272
|
+
return { ok: false, code: "clock_skew" };
|
|
273
|
+
}
|
|
274
|
+
return { ok: true, frame };
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// ../shared/src/execution-context-protocol/http-signature.ts
|
|
278
|
+
var ecpSignatureHeader = "Kody-ECP-Signature";
|
|
279
|
+
var ecpRunHeader = "Kody-ECP-Run";
|
|
280
|
+
var encoder2 = new TextEncoder();
|
|
281
|
+
async function sha256Base64Url(body) {
|
|
282
|
+
const bytes = typeof body === "string" ? encoder2.encode(body) : new Uint8Array(body);
|
|
283
|
+
const digest = await crypto.subtle.digest("SHA-256", bytes);
|
|
284
|
+
return bytesToBase64Url(new Uint8Array(digest));
|
|
285
|
+
}
|
|
286
|
+
async function ecpHttpSignatureInput(input) {
|
|
287
|
+
return [
|
|
288
|
+
input.method.toUpperCase(),
|
|
289
|
+
input.pathAndQuery,
|
|
290
|
+
String(input.ts),
|
|
291
|
+
input.nonce,
|
|
292
|
+
await sha256Base64Url(input.body)
|
|
293
|
+
].join("\n");
|
|
294
|
+
}
|
|
295
|
+
function randomEcpNonce() {
|
|
296
|
+
const bytes = new Uint8Array(16);
|
|
297
|
+
crypto.getRandomValues(bytes);
|
|
298
|
+
return bytesToBase64Url(bytes);
|
|
299
|
+
}
|
|
300
|
+
function formatEcpSignatureHeader(signature) {
|
|
301
|
+
return `ec=${signature.ec}, ts=${signature.ts}, n=${signature.n}, s=${signature.s}`;
|
|
302
|
+
}
|
|
303
|
+
async function signEcpHttpRequest(input) {
|
|
304
|
+
const ts = input.now ?? Date.now();
|
|
305
|
+
const n = input.nonce ?? randomEcpNonce();
|
|
306
|
+
const message = await ecpHttpSignatureInput({ ...input, ts, nonce: n });
|
|
307
|
+
const s = await signDetached({
|
|
308
|
+
prefix: ecpHttpSignaturePrefix,
|
|
309
|
+
message,
|
|
310
|
+
privateKey: input.privateKey
|
|
311
|
+
});
|
|
312
|
+
return { ec: input.ec, ts, n, s };
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// ../shared/src/execution-context-protocol/server-keys.ts
|
|
316
|
+
var ecpRootPublicKeys = [
|
|
317
|
+
{ kid: "root-1", x: "snJD_ruDIz9ejTdYyRuDT38rlmScNQyXSYpeUCxOBRQ" },
|
|
318
|
+
{ kid: "root-2", x: "8TMMLGTn67-wjqM4tU7hUcKtvEgENM2cE4DFjLdBz-w" }
|
|
319
|
+
];
|
|
320
|
+
var ecpTestRootPublicKey = {
|
|
321
|
+
kid: "root-test-1",
|
|
322
|
+
x: "6qRBa8n9QOpgMVvKw2Ck3hUGuaVAi0D-bNGjum0u5rk"
|
|
323
|
+
};
|
|
324
|
+
function isRecord3(value) {
|
|
325
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
326
|
+
}
|
|
327
|
+
function parsePublishedKey(value) {
|
|
328
|
+
if (!isRecord3(value)) return null;
|
|
329
|
+
const { kid, x, notAfter } = value;
|
|
330
|
+
if (typeof kid !== "string" || !kid) return null;
|
|
331
|
+
if (typeof notAfter !== "number" || !Number.isFinite(notAfter)) return null;
|
|
332
|
+
const jwk = normalizeEd25519PublicJwk({ kty: "OKP", crv: "Ed25519", x });
|
|
333
|
+
if (!jwk) return null;
|
|
334
|
+
return { kid, x: jwk.x, notAfter };
|
|
335
|
+
}
|
|
336
|
+
function parseKeySetPayload(value) {
|
|
337
|
+
if (!isRecord3(value) || value.v !== 1) return null;
|
|
338
|
+
if (!Array.isArray(value.keys) || typeof value.issuedAt !== "number") {
|
|
339
|
+
return null;
|
|
340
|
+
}
|
|
341
|
+
const keys = [];
|
|
342
|
+
for (const entry of value.keys) {
|
|
343
|
+
const key = parsePublishedKey(entry);
|
|
344
|
+
if (!key) return null;
|
|
345
|
+
keys.push(key);
|
|
346
|
+
}
|
|
347
|
+
return { v: 1, keys, issuedAt: value.issuedAt };
|
|
348
|
+
}
|
|
349
|
+
async function verifyServerKeySet(input) {
|
|
350
|
+
const envelope = parseEnvelope(input.document);
|
|
351
|
+
if (!envelope) return null;
|
|
352
|
+
const root = (input.roots ?? ecpRootPublicKeys).find(
|
|
353
|
+
(candidate) => candidate.kid === envelope.k
|
|
354
|
+
);
|
|
355
|
+
if (!root) return null;
|
|
356
|
+
const rootJwk = normalizeEd25519PublicJwk({
|
|
357
|
+
kty: "OKP",
|
|
358
|
+
crv: "Ed25519",
|
|
359
|
+
x: root.x
|
|
360
|
+
});
|
|
361
|
+
if (!rootJwk) return null;
|
|
362
|
+
const ok = await verifyDetached({
|
|
363
|
+
prefix: ecpKeySetSignaturePrefix,
|
|
364
|
+
message: envelope.p,
|
|
365
|
+
signature: envelope.s,
|
|
366
|
+
publicKey: await importEd25519PublicKey(rootJwk)
|
|
367
|
+
});
|
|
368
|
+
if (!ok) return null;
|
|
369
|
+
return parseKeySetPayload(decodeEnvelopePayload(envelope.p));
|
|
370
|
+
}
|
|
371
|
+
function publishedKeyJwk(key) {
|
|
372
|
+
return { kty: "OKP", crv: "Ed25519", x: key.x };
|
|
373
|
+
}
|
|
374
|
+
|
|
62
375
|
// src/execution-context/client.ts
|
|
63
376
|
var executionContextsPath = "/v1/execution-contexts";
|
|
64
377
|
var reloginHint = "Run `kody login` again, then retry `kody context register`.";
|
|
@@ -1806,11 +2119,680 @@ async function otherLocalDaemons(input) {
|
|
|
1806
2119
|
);
|
|
1807
2120
|
}
|
|
1808
2121
|
|
|
2122
|
+
// src/execution-context/host-commands.ts
|
|
2123
|
+
import { spawn as spawn2 } from "node:child_process";
|
|
2124
|
+
import { randomBytes as randomBytes3 } from "node:crypto";
|
|
2125
|
+
import { stat } from "node:fs/promises";
|
|
2126
|
+
import { homedir as homedir2 } from "node:os";
|
|
2127
|
+
import { isAbsolute as isAbsolute2 } from "node:path";
|
|
2128
|
+
var HostCommandError = class extends Error {
|
|
2129
|
+
constructor(code, message) {
|
|
2130
|
+
super(`${message} (${code})`);
|
|
2131
|
+
this.code = code;
|
|
2132
|
+
this.name = "HostCommandError";
|
|
2133
|
+
}
|
|
2134
|
+
code;
|
|
2135
|
+
};
|
|
2136
|
+
var hostCommandLimits = {
|
|
2137
|
+
foregroundDefaultTimeoutMs: 6e4,
|
|
2138
|
+
foregroundMaxTimeoutMs: 15 * 6e4,
|
|
2139
|
+
backgroundDefaultTimeoutMs: 2 * 60 * 6e4,
|
|
2140
|
+
backgroundMaxTimeoutMs: 24 * 60 * 6e4,
|
|
2141
|
+
defaultOutputBytes: 256 * 1024,
|
|
2142
|
+
maxOutputBytes: 4 * 1024 * 1024,
|
|
2143
|
+
backgroundBufferBytes: 4 * 1024 * 1024,
|
|
2144
|
+
maxOutputReadBytes: 1024 * 1024,
|
|
2145
|
+
maxBackgroundJobs: 16,
|
|
2146
|
+
keepFinishedJobsMs: 60 * 6e4,
|
|
2147
|
+
maxCommandChars: 64 * 1024,
|
|
2148
|
+
maxStdinBytes: 5 * 1024 * 1024
|
|
2149
|
+
};
|
|
2150
|
+
var OutputBuffer = class {
|
|
2151
|
+
constructor(capacity) {
|
|
2152
|
+
this.capacity = capacity;
|
|
2153
|
+
}
|
|
2154
|
+
capacity;
|
|
2155
|
+
#chunks = [];
|
|
2156
|
+
#size = 0;
|
|
2157
|
+
/** Bytes ever written. */
|
|
2158
|
+
total = 0;
|
|
2159
|
+
get dropped() {
|
|
2160
|
+
return this.total - this.#size;
|
|
2161
|
+
}
|
|
2162
|
+
write(chunk) {
|
|
2163
|
+
this.total += chunk.length;
|
|
2164
|
+
this.#chunks.push(chunk);
|
|
2165
|
+
this.#size += chunk.length;
|
|
2166
|
+
while (this.#size > this.capacity && this.#chunks.length > 0) {
|
|
2167
|
+
const head = this.#chunks[0];
|
|
2168
|
+
const over = this.#size - this.capacity;
|
|
2169
|
+
if (head.length <= over) {
|
|
2170
|
+
this.#chunks.shift();
|
|
2171
|
+
this.#size -= head.length;
|
|
2172
|
+
} else {
|
|
2173
|
+
this.#chunks[0] = head.subarray(over);
|
|
2174
|
+
this.#size -= over;
|
|
2175
|
+
}
|
|
2176
|
+
}
|
|
2177
|
+
}
|
|
2178
|
+
/** Bytes from absolute `offset` (clamped to what is still kept). */
|
|
2179
|
+
read(offset, maxBytes) {
|
|
2180
|
+
const all = Buffer.concat(this.#chunks);
|
|
2181
|
+
let start = Math.max(offset, this.dropped);
|
|
2182
|
+
while (start - this.dropped < all.length && start > offset && isUtf8Continuation(all[start - this.dropped]))
|
|
2183
|
+
start++;
|
|
2184
|
+
let end = Math.min(start - this.dropped + maxBytes, all.length);
|
|
2185
|
+
if (end < all.length && isUtf8Continuation(all[end])) {
|
|
2186
|
+
let back = end;
|
|
2187
|
+
while (back > start - this.dropped && isUtf8Continuation(all[back]))
|
|
2188
|
+
back--;
|
|
2189
|
+
if (back > start - this.dropped) end = back;
|
|
2190
|
+
else {
|
|
2191
|
+
while (end < all.length && isUtf8Continuation(all[end])) end++;
|
|
2192
|
+
}
|
|
2193
|
+
}
|
|
2194
|
+
const slice = all.subarray(start - this.dropped, end);
|
|
2195
|
+
return {
|
|
2196
|
+
text: slice.toString("utf8"),
|
|
2197
|
+
from: start,
|
|
2198
|
+
next: start + slice.length,
|
|
2199
|
+
skipped: start - offset
|
|
2200
|
+
};
|
|
2201
|
+
}
|
|
2202
|
+
text() {
|
|
2203
|
+
return Buffer.concat(this.#chunks).toString("utf8");
|
|
2204
|
+
}
|
|
2205
|
+
};
|
|
2206
|
+
function newCommandId() {
|
|
2207
|
+
return `cmd_${randomBytes3(9).toString("base64url")}`;
|
|
2208
|
+
}
|
|
2209
|
+
function clampInt(value, fallback, max, min = 1) {
|
|
2210
|
+
if (value === void 0 || value === null) return fallback;
|
|
2211
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
2212
|
+
throw new HostCommandError(
|
|
2213
|
+
"invalid_request",
|
|
2214
|
+
`Expected a number, got ${typeof value}.`
|
|
2215
|
+
);
|
|
2216
|
+
}
|
|
2217
|
+
return Math.min(Math.max(Math.floor(value), min), max);
|
|
2218
|
+
}
|
|
2219
|
+
function isUtf8Continuation(byte) {
|
|
2220
|
+
return (byte & 192) === 128;
|
|
2221
|
+
}
|
|
2222
|
+
function shellFor(env) {
|
|
2223
|
+
const shell = env.SHELL;
|
|
2224
|
+
return shell && isAbsolute2(shell) ? shell : "/bin/sh";
|
|
2225
|
+
}
|
|
2226
|
+
function killTree(child, signal) {
|
|
2227
|
+
if (child.pid === void 0) return;
|
|
2228
|
+
if (process.platform === "win32") {
|
|
2229
|
+
void killWindowsTree(child.pid, {
|
|
2230
|
+
leaderExited: child.exitCode !== null || child.signalCode !== null
|
|
2231
|
+
});
|
|
2232
|
+
return;
|
|
2233
|
+
}
|
|
2234
|
+
try {
|
|
2235
|
+
process.kill(-child.pid, signal);
|
|
2236
|
+
} catch {
|
|
2237
|
+
try {
|
|
2238
|
+
child.kill(signal);
|
|
2239
|
+
} catch {
|
|
2240
|
+
}
|
|
2241
|
+
}
|
|
2242
|
+
}
|
|
2243
|
+
function stoppedOutcome(outcome, stoppedByKody) {
|
|
2244
|
+
if (!stoppedByKody || process.platform !== "win32") return outcome;
|
|
2245
|
+
return { ...outcome, code: null, signal: outcome.signal ?? "SIGKILL" };
|
|
2246
|
+
}
|
|
2247
|
+
var HostCommands = class {
|
|
2248
|
+
#jobs = /* @__PURE__ */ new Map();
|
|
2249
|
+
#onFinished = null;
|
|
2250
|
+
/** Finishes that could not be sent yet (no live connection); sent on the next reporter. */
|
|
2251
|
+
#unreported = [];
|
|
2252
|
+
#foreground = /* @__PURE__ */ new Map();
|
|
2253
|
+
#pendingStarts = 0;
|
|
2254
|
+
#localEnabled;
|
|
2255
|
+
/** The server grant's commands flag (off from website/API/CLI). */
|
|
2256
|
+
#grantEnabled = false;
|
|
2257
|
+
#platform;
|
|
2258
|
+
constructor(options = {}) {
|
|
2259
|
+
this.#localEnabled = options.localEnabled === true;
|
|
2260
|
+
this.#platform = options.platform ?? process.platform;
|
|
2261
|
+
}
|
|
2262
|
+
/** Both layers allow commands. */
|
|
2263
|
+
get enabled() {
|
|
2264
|
+
return this.#localEnabled && this.#grantEnabled;
|
|
2265
|
+
}
|
|
2266
|
+
setLocalEnabled(enabled) {
|
|
2267
|
+
this.#localEnabled = enabled;
|
|
2268
|
+
if (!enabled) this.killAll("commands_off");
|
|
2269
|
+
}
|
|
2270
|
+
setGrantEnabled(enabled) {
|
|
2271
|
+
this.#grantEnabled = enabled;
|
|
2272
|
+
if (!enabled) this.killAll("commands_off");
|
|
2273
|
+
}
|
|
2274
|
+
/** Clears the reporter if it is still `report` (a newer connection may own it). */
|
|
2275
|
+
clearReporter(report) {
|
|
2276
|
+
if (this.#onFinished === report) this.#onFinished = null;
|
|
2277
|
+
}
|
|
2278
|
+
setReporter(report) {
|
|
2279
|
+
this.#onFinished = report;
|
|
2280
|
+
if (!report) return;
|
|
2281
|
+
const queued = this.#unreported.splice(0);
|
|
2282
|
+
for (const finished of queued) this.#report(finished);
|
|
2283
|
+
}
|
|
2284
|
+
/** Throws the denial a run sees when it may not run commands here. */
|
|
2285
|
+
assertAvailable() {
|
|
2286
|
+
if (!this.#localEnabled) {
|
|
2287
|
+
throw new HostCommandError(
|
|
2288
|
+
"execution_context_commands_off",
|
|
2289
|
+
"Host commands are off on this machine: its owner has not opted in. Turn them on on the machine with `kody context commands on` or the Kody desktop app (Machine > Allow commands)."
|
|
2290
|
+
);
|
|
2291
|
+
}
|
|
2292
|
+
if (!this.#grantEnabled) {
|
|
2293
|
+
throw new HostCommandError(
|
|
2294
|
+
"execution_context_commands_off",
|
|
2295
|
+
"Host commands were turned off for this machine (from the website, API or CLI). Its owner turns them back on on the machine with `kody context commands on` or the Kody desktop app."
|
|
2296
|
+
);
|
|
2297
|
+
}
|
|
2298
|
+
}
|
|
2299
|
+
/** Dispatches one `kody.command.<name>(args)` call from a run. */
|
|
2300
|
+
async call(name, args, run) {
|
|
2301
|
+
this.assertAvailable();
|
|
2302
|
+
if (name === "status" || name === "output" || name === "kill" || name === "list") {
|
|
2303
|
+
await run.authorize({ lifecycle: name });
|
|
2304
|
+
this.assertAvailable();
|
|
2305
|
+
}
|
|
2306
|
+
switch (name) {
|
|
2307
|
+
case "run":
|
|
2308
|
+
return await this.run(args, run);
|
|
2309
|
+
case "start":
|
|
2310
|
+
return await this.start(args, run);
|
|
2311
|
+
case "status":
|
|
2312
|
+
return this.status(args, run);
|
|
2313
|
+
case "output":
|
|
2314
|
+
return this.output(args, run);
|
|
2315
|
+
case "kill":
|
|
2316
|
+
return this.kill(args, run);
|
|
2317
|
+
case "list":
|
|
2318
|
+
return this.list(run);
|
|
2319
|
+
default:
|
|
2320
|
+
throw new HostCommandError(
|
|
2321
|
+
"invalid_request",
|
|
2322
|
+
`Unknown kody.command.${name}. Use run, start, status, output, kill or list.`
|
|
2323
|
+
);
|
|
2324
|
+
}
|
|
2325
|
+
}
|
|
2326
|
+
async #prepare(input, background) {
|
|
2327
|
+
const raw = typeof input === "string" ? { command: input } : input;
|
|
2328
|
+
const spec = raw ?? {};
|
|
2329
|
+
if (typeof spec !== "object" || Array.isArray(spec)) {
|
|
2330
|
+
throw new HostCommandError(
|
|
2331
|
+
"invalid_request",
|
|
2332
|
+
"Pass { command } or { file, args }."
|
|
2333
|
+
);
|
|
2334
|
+
}
|
|
2335
|
+
const hasCommand = typeof spec.command === "string" && spec.command.length > 0;
|
|
2336
|
+
const hasFile = typeof spec.file === "string" && spec.file.length > 0;
|
|
2337
|
+
if (hasCommand === hasFile) {
|
|
2338
|
+
throw new HostCommandError(
|
|
2339
|
+
"invalid_request",
|
|
2340
|
+
"Pass exactly one of `command` (a shell command line) or `file` (an executable, with optional `args`)."
|
|
2341
|
+
);
|
|
2342
|
+
}
|
|
2343
|
+
const args = spec.args ?? [];
|
|
2344
|
+
if (!Array.isArray(args) || !args.every((arg) => typeof arg === "string")) {
|
|
2345
|
+
throw new HostCommandError(
|
|
2346
|
+
"invalid_request",
|
|
2347
|
+
"`args` must be an array of strings."
|
|
2348
|
+
);
|
|
2349
|
+
}
|
|
2350
|
+
if (hasCommand && spec.args !== void 0) {
|
|
2351
|
+
throw new HostCommandError(
|
|
2352
|
+
"invalid_request",
|
|
2353
|
+
"`args` goes with `file`, not `command`."
|
|
2354
|
+
);
|
|
2355
|
+
}
|
|
2356
|
+
const display = hasCommand ? spec.command : [spec.file, ...args].join(" ");
|
|
2357
|
+
if (display.length > hostCommandLimits.maxCommandChars) {
|
|
2358
|
+
throw new HostCommandError("invalid_request", "The command is too long.");
|
|
2359
|
+
}
|
|
2360
|
+
const cwd = spec.cwd ?? homedir2();
|
|
2361
|
+
if (typeof cwd !== "string" || !isAbsolute2(cwd)) {
|
|
2362
|
+
throw new HostCommandError(
|
|
2363
|
+
"invalid_request",
|
|
2364
|
+
"`cwd` must be an absolute path on the machine."
|
|
2365
|
+
);
|
|
2366
|
+
}
|
|
2367
|
+
const cwdStat = await stat(cwd).catch(() => null);
|
|
2368
|
+
if (!cwdStat?.isDirectory()) {
|
|
2369
|
+
throw new HostCommandError(
|
|
2370
|
+
"invalid_request",
|
|
2371
|
+
`cwd ${cwd} is not a folder on this machine.`
|
|
2372
|
+
);
|
|
2373
|
+
}
|
|
2374
|
+
const extraEnv = spec.env ?? {};
|
|
2375
|
+
if (typeof extraEnv !== "object" || Array.isArray(extraEnv) || !Object.entries(extraEnv).every(
|
|
2376
|
+
([key, value]) => /^[A-Za-z_][A-Za-z0-9_]*$/.test(key) && typeof value === "string"
|
|
2377
|
+
)) {
|
|
2378
|
+
throw new HostCommandError(
|
|
2379
|
+
"invalid_request",
|
|
2380
|
+
"`env` must map variable names to string values."
|
|
2381
|
+
);
|
|
2382
|
+
}
|
|
2383
|
+
if (spec.stdin !== void 0 && typeof spec.stdin !== "string") {
|
|
2384
|
+
throw new HostCommandError("invalid_request", "`stdin` must be a string.");
|
|
2385
|
+
}
|
|
2386
|
+
if ((spec.stdin?.length ?? 0) > hostCommandLimits.maxStdinBytes) {
|
|
2387
|
+
throw new HostCommandError(
|
|
2388
|
+
"invalid_request",
|
|
2389
|
+
"`stdin` is larger than 5 MB."
|
|
2390
|
+
);
|
|
2391
|
+
}
|
|
2392
|
+
const timeoutMs = background ? clampInt(
|
|
2393
|
+
spec.timeoutMs,
|
|
2394
|
+
hostCommandLimits.backgroundDefaultTimeoutMs,
|
|
2395
|
+
hostCommandLimits.backgroundMaxTimeoutMs,
|
|
2396
|
+
1e3
|
|
2397
|
+
) : clampInt(
|
|
2398
|
+
spec.timeoutMs,
|
|
2399
|
+
hostCommandLimits.foregroundDefaultTimeoutMs,
|
|
2400
|
+
hostCommandLimits.foregroundMaxTimeoutMs,
|
|
2401
|
+
100
|
|
2402
|
+
);
|
|
2403
|
+
const maxOutputBytes = clampInt(
|
|
2404
|
+
spec.maxOutputBytes,
|
|
2405
|
+
hostCommandLimits.defaultOutputBytes,
|
|
2406
|
+
hostCommandLimits.maxOutputBytes,
|
|
2407
|
+
1024
|
|
2408
|
+
);
|
|
2409
|
+
if (spec.shell !== void 0 && (!hasCommand || spec.shell !== "powershell" && spec.shell !== "cmd")) {
|
|
2410
|
+
throw new HostCommandError(
|
|
2411
|
+
"invalid_request",
|
|
2412
|
+
"`shell` goes with `command` and is 'powershell' or 'cmd' (Windows only)."
|
|
2413
|
+
);
|
|
2414
|
+
}
|
|
2415
|
+
const env = { ...process.env, ...extraEnv };
|
|
2416
|
+
const launch = this.#platform !== "win32" ? {
|
|
2417
|
+
file: hasCommand ? shellFor(env) : spec.file,
|
|
2418
|
+
argv: hasCommand ? ["-c", spec.command] : args,
|
|
2419
|
+
verbatim: false
|
|
2420
|
+
} : hasCommand ? windowsShellLaunch(
|
|
2421
|
+
spec.command,
|
|
2422
|
+
spec.shell ?? "powershell",
|
|
2423
|
+
env
|
|
2424
|
+
) : windowsFileLaunch(spec.file, args, env);
|
|
2425
|
+
return {
|
|
2426
|
+
display,
|
|
2427
|
+
cwd,
|
|
2428
|
+
env,
|
|
2429
|
+
envKeys: Object.keys(extraEnv).sort(),
|
|
2430
|
+
stdin: spec.stdin,
|
|
2431
|
+
timeoutMs,
|
|
2432
|
+
maxOutputBytes,
|
|
2433
|
+
file: launch.file,
|
|
2434
|
+
argv: launch.argv,
|
|
2435
|
+
verbatim: launch.verbatim
|
|
2436
|
+
};
|
|
2437
|
+
}
|
|
2438
|
+
#spawn(prepared) {
|
|
2439
|
+
const child = spawn2(prepared.file, prepared.argv, {
|
|
2440
|
+
cwd: prepared.cwd,
|
|
2441
|
+
env: prepared.env,
|
|
2442
|
+
// POSIX: own a process group so killTree reaches grandchildren.
|
|
2443
|
+
// Windows: taskkill /T walks the tree; detached would open a console.
|
|
2444
|
+
detached: this.#platform !== "win32",
|
|
2445
|
+
windowsHide: true,
|
|
2446
|
+
windowsVerbatimArguments: prepared.verbatim,
|
|
2447
|
+
stdio: [prepared.stdin === void 0 ? "ignore" : "pipe", "pipe", "pipe"]
|
|
2448
|
+
});
|
|
2449
|
+
if (prepared.stdin !== void 0 && child.stdin) {
|
|
2450
|
+
child.stdin.on("error", () => {
|
|
2451
|
+
});
|
|
2452
|
+
child.stdin.end(prepared.stdin);
|
|
2453
|
+
}
|
|
2454
|
+
return child;
|
|
2455
|
+
}
|
|
2456
|
+
#report(finished) {
|
|
2457
|
+
const requeue = () => {
|
|
2458
|
+
if (this.#unreported.length < 256) this.#unreported.push(finished);
|
|
2459
|
+
};
|
|
2460
|
+
const report = this.#onFinished;
|
|
2461
|
+
if (!report) return requeue();
|
|
2462
|
+
try {
|
|
2463
|
+
void Promise.resolve(report(finished)).catch(requeue);
|
|
2464
|
+
} catch {
|
|
2465
|
+
requeue();
|
|
2466
|
+
}
|
|
2467
|
+
}
|
|
2468
|
+
async run(raw, run) {
|
|
2469
|
+
const prepared = await this.#prepare(raw, false);
|
|
2470
|
+
const commandId = newCommandId();
|
|
2471
|
+
await run.authorize({
|
|
2472
|
+
commandId,
|
|
2473
|
+
command: prepared.display,
|
|
2474
|
+
cwd: prepared.cwd,
|
|
2475
|
+
envKeys: prepared.envKeys,
|
|
2476
|
+
background: false
|
|
2477
|
+
});
|
|
2478
|
+
this.assertAvailable();
|
|
2479
|
+
const startedAt = Date.now();
|
|
2480
|
+
const stdout = new OutputBuffer(prepared.maxOutputBytes);
|
|
2481
|
+
const stderr = new OutputBuffer(prepared.maxOutputBytes);
|
|
2482
|
+
const ended = { reason: "exited" };
|
|
2483
|
+
const child = this.#spawn(prepared);
|
|
2484
|
+
this.#foreground.set(child, ended);
|
|
2485
|
+
child.stdout?.on("data", (chunk) => stdout.write(chunk));
|
|
2486
|
+
child.stderr?.on("data", (chunk) => stderr.write(chunk));
|
|
2487
|
+
const timer = setTimeout(() => {
|
|
2488
|
+
ended.reason = "timed_out";
|
|
2489
|
+
killTree(child, "SIGKILL");
|
|
2490
|
+
}, prepared.timeoutMs);
|
|
2491
|
+
const onAbort = () => {
|
|
2492
|
+
ended.reason = "cancelled";
|
|
2493
|
+
killTree(child, "SIGKILL");
|
|
2494
|
+
};
|
|
2495
|
+
run.signal.addEventListener("abort", onAbort, { once: true });
|
|
2496
|
+
if (run.signal.aborted) onAbort();
|
|
2497
|
+
const exited = await new Promise((resolve) => {
|
|
2498
|
+
child.once(
|
|
2499
|
+
"error",
|
|
2500
|
+
(error) => resolve({ code: null, signal: null, error: error.message })
|
|
2501
|
+
);
|
|
2502
|
+
child.once("close", (code, signal) => resolve({ code, signal }));
|
|
2503
|
+
});
|
|
2504
|
+
const outcome = stoppedOutcome(exited, ended.reason !== "exited");
|
|
2505
|
+
clearTimeout(timer);
|
|
2506
|
+
this.#foreground.delete(child);
|
|
2507
|
+
run.signal.removeEventListener("abort", onAbort);
|
|
2508
|
+
if (outcome.error) ended.reason = "spawn_failed";
|
|
2509
|
+
const durationMs = Date.now() - startedAt;
|
|
2510
|
+
this.#report({
|
|
2511
|
+
commandId,
|
|
2512
|
+
runId: run.runId,
|
|
2513
|
+
command: prepared.display,
|
|
2514
|
+
cwd: prepared.cwd,
|
|
2515
|
+
exitCode: outcome.code,
|
|
2516
|
+
signal: outcome.signal,
|
|
2517
|
+
durationMs,
|
|
2518
|
+
background: false,
|
|
2519
|
+
reason: ended.reason
|
|
2520
|
+
});
|
|
2521
|
+
if (outcome.error) {
|
|
2522
|
+
throw new HostCommandError(
|
|
2523
|
+
"invalid_request",
|
|
2524
|
+
`Could not start the command: ${outcome.error}`
|
|
2525
|
+
);
|
|
2526
|
+
}
|
|
2527
|
+
return {
|
|
2528
|
+
commandId,
|
|
2529
|
+
exitCode: outcome.code,
|
|
2530
|
+
signal: outcome.signal,
|
|
2531
|
+
timedOut: ended.reason === "timed_out",
|
|
2532
|
+
stdout: stdout.text(),
|
|
2533
|
+
stderr: stderr.text(),
|
|
2534
|
+
stdoutTruncated: stdout.dropped > 0,
|
|
2535
|
+
stderrTruncated: stderr.dropped > 0,
|
|
2536
|
+
durationMs
|
|
2537
|
+
};
|
|
2538
|
+
}
|
|
2539
|
+
async start(raw, run) {
|
|
2540
|
+
this.#sweep();
|
|
2541
|
+
const running = [...this.#jobs.values()].filter(
|
|
2542
|
+
(job2) => job2.state === "running"
|
|
2543
|
+
);
|
|
2544
|
+
if (running.length + this.#pendingStarts >= hostCommandLimits.maxBackgroundJobs) {
|
|
2545
|
+
throw new HostCommandError(
|
|
2546
|
+
"invalid_request",
|
|
2547
|
+
`This machine already runs ${running.length + this.#pendingStarts} background commands. Kill one (kody.command.kill) first.`
|
|
2548
|
+
);
|
|
2549
|
+
}
|
|
2550
|
+
this.#pendingStarts++;
|
|
2551
|
+
let prepared;
|
|
2552
|
+
let commandId;
|
|
2553
|
+
let child;
|
|
2554
|
+
try {
|
|
2555
|
+
prepared = await this.#prepare(raw, true);
|
|
2556
|
+
commandId = newCommandId();
|
|
2557
|
+
await run.authorize({
|
|
2558
|
+
commandId,
|
|
2559
|
+
command: prepared.display,
|
|
2560
|
+
cwd: prepared.cwd,
|
|
2561
|
+
envKeys: prepared.envKeys,
|
|
2562
|
+
background: true
|
|
2563
|
+
});
|
|
2564
|
+
this.assertAvailable();
|
|
2565
|
+
child = this.#spawn(prepared);
|
|
2566
|
+
} finally {
|
|
2567
|
+
this.#pendingStarts--;
|
|
2568
|
+
}
|
|
2569
|
+
const job = {
|
|
2570
|
+
id: commandId,
|
|
2571
|
+
runId: run.runId,
|
|
2572
|
+
ownerUserId: run.requesterUserId,
|
|
2573
|
+
command: prepared.display,
|
|
2574
|
+
cwd: prepared.cwd,
|
|
2575
|
+
child,
|
|
2576
|
+
pid: child.pid,
|
|
2577
|
+
startedAt: Date.now(),
|
|
2578
|
+
endedAt: null,
|
|
2579
|
+
exitCode: null,
|
|
2580
|
+
signal: null,
|
|
2581
|
+
state: "running",
|
|
2582
|
+
stdout: new OutputBuffer(hostCommandLimits.backgroundBufferBytes),
|
|
2583
|
+
stderr: new OutputBuffer(hostCommandLimits.backgroundBufferBytes),
|
|
2584
|
+
done: Promise.resolve(),
|
|
2585
|
+
timer: null,
|
|
2586
|
+
error: null
|
|
2587
|
+
};
|
|
2588
|
+
child.stdout?.on("data", (chunk) => job.stdout.write(chunk));
|
|
2589
|
+
child.stderr?.on("data", (chunk) => job.stderr.write(chunk));
|
|
2590
|
+
job.timer = setTimeout(() => {
|
|
2591
|
+
if (job.state !== "running") return;
|
|
2592
|
+
job.state = "timed_out";
|
|
2593
|
+
killTree(child, "SIGKILL");
|
|
2594
|
+
}, prepared.timeoutMs);
|
|
2595
|
+
job.timer.unref?.();
|
|
2596
|
+
job.done = new Promise((resolve) => {
|
|
2597
|
+
const finish = (code, signal, error) => {
|
|
2598
|
+
if (job.endedAt !== null) return resolve();
|
|
2599
|
+
if (job.timer) clearTimeout(job.timer);
|
|
2600
|
+
({ code, signal } = stoppedOutcome(
|
|
2601
|
+
{ code, signal },
|
|
2602
|
+
job.state === "killed" || job.state === "timed_out"
|
|
2603
|
+
));
|
|
2604
|
+
job.endedAt = Date.now();
|
|
2605
|
+
job.exitCode = code;
|
|
2606
|
+
job.signal = signal;
|
|
2607
|
+
if (error) {
|
|
2608
|
+
job.state = "failed";
|
|
2609
|
+
job.error = error;
|
|
2610
|
+
} else if (job.state === "running") {
|
|
2611
|
+
job.state = "exited";
|
|
2612
|
+
}
|
|
2613
|
+
this.#report({
|
|
2614
|
+
commandId,
|
|
2615
|
+
runId: job.runId,
|
|
2616
|
+
command: job.command,
|
|
2617
|
+
cwd: job.cwd,
|
|
2618
|
+
exitCode: code,
|
|
2619
|
+
signal,
|
|
2620
|
+
durationMs: job.endedAt - job.startedAt,
|
|
2621
|
+
background: true,
|
|
2622
|
+
reason: job.state === "failed" ? "spawn_failed" : job.state === "killed" ? job.error === "commands_off" ? "commands_off" : "killed" : job.state === "timed_out" ? "timed_out" : "exited"
|
|
2623
|
+
});
|
|
2624
|
+
resolve();
|
|
2625
|
+
};
|
|
2626
|
+
child.once("error", (error) => finish(null, null, error.message));
|
|
2627
|
+
child.once("close", (code, signal) => finish(code, signal));
|
|
2628
|
+
});
|
|
2629
|
+
this.#jobs.set(commandId, job);
|
|
2630
|
+
return {
|
|
2631
|
+
commandId,
|
|
2632
|
+
pid: job.pid ?? null,
|
|
2633
|
+
startedAt: new Date(job.startedAt).toISOString(),
|
|
2634
|
+
timeoutMs: prepared.timeoutMs
|
|
2635
|
+
};
|
|
2636
|
+
}
|
|
2637
|
+
#job(raw, run) {
|
|
2638
|
+
const id = typeof raw === "string" ? raw : raw?.commandId;
|
|
2639
|
+
const job = typeof id === "string" ? this.#jobs.get(id) : void 0;
|
|
2640
|
+
if (!job || job.ownerUserId !== run.requesterUserId) {
|
|
2641
|
+
throw new HostCommandError(
|
|
2642
|
+
"not_found",
|
|
2643
|
+
`No background command ${String(id)} on this machine for you. kody.command.list() shows yours.`
|
|
2644
|
+
);
|
|
2645
|
+
}
|
|
2646
|
+
return job;
|
|
2647
|
+
}
|
|
2648
|
+
#describe(job) {
|
|
2649
|
+
return {
|
|
2650
|
+
commandId: job.id,
|
|
2651
|
+
command: job.command,
|
|
2652
|
+
cwd: job.cwd,
|
|
2653
|
+
state: job.state,
|
|
2654
|
+
pid: job.pid ?? null,
|
|
2655
|
+
exitCode: job.exitCode,
|
|
2656
|
+
signal: job.signal,
|
|
2657
|
+
startedAt: new Date(job.startedAt).toISOString(),
|
|
2658
|
+
endedAt: job.endedAt === null ? null : new Date(job.endedAt).toISOString(),
|
|
2659
|
+
durationMs: (job.endedAt ?? Date.now()) - job.startedAt,
|
|
2660
|
+
stdoutBytes: job.stdout.total,
|
|
2661
|
+
stderrBytes: job.stderr.total,
|
|
2662
|
+
...job.error && job.state === "failed" ? { error: job.error } : {}
|
|
2663
|
+
};
|
|
2664
|
+
}
|
|
2665
|
+
status(raw, run) {
|
|
2666
|
+
return this.#describe(this.#job(raw, run));
|
|
2667
|
+
}
|
|
2668
|
+
output(raw, run) {
|
|
2669
|
+
const job = this.#job(raw, run);
|
|
2670
|
+
const input = typeof raw === "object" && raw ? raw : {};
|
|
2671
|
+
const maxBytes = clampInt(
|
|
2672
|
+
input.maxBytes,
|
|
2673
|
+
hostCommandLimits.defaultOutputBytes,
|
|
2674
|
+
hostCommandLimits.maxOutputReadBytes
|
|
2675
|
+
);
|
|
2676
|
+
const stdout = job.stdout.read(
|
|
2677
|
+
clampInt(input.stdoutOffset, 0, Number.MAX_SAFE_INTEGER, 0),
|
|
2678
|
+
maxBytes
|
|
2679
|
+
);
|
|
2680
|
+
const stderr = job.stderr.read(
|
|
2681
|
+
clampInt(input.stderrOffset, 0, Number.MAX_SAFE_INTEGER, 0),
|
|
2682
|
+
maxBytes
|
|
2683
|
+
);
|
|
2684
|
+
return {
|
|
2685
|
+
commandId: job.id,
|
|
2686
|
+
state: job.state,
|
|
2687
|
+
exitCode: job.exitCode,
|
|
2688
|
+
stdout: stdout.text,
|
|
2689
|
+
stderr: stderr.text,
|
|
2690
|
+
stdoutOffset: stdout.next,
|
|
2691
|
+
stderrOffset: stderr.next,
|
|
2692
|
+
stdoutSkipped: stdout.skipped,
|
|
2693
|
+
stderrSkipped: stderr.skipped,
|
|
2694
|
+
done: job.state !== "running" && stdout.next >= job.stdout.total && stderr.next >= job.stderr.total
|
|
2695
|
+
};
|
|
2696
|
+
}
|
|
2697
|
+
kill(raw, run) {
|
|
2698
|
+
const job = this.#job(raw, run);
|
|
2699
|
+
const requested = raw?.signal;
|
|
2700
|
+
const signal = requested === "SIGINT" || requested === "SIGKILL" || requested === "SIGTERM" ? requested : "SIGTERM";
|
|
2701
|
+
if (job.state === "running") {
|
|
2702
|
+
job.state = "killed";
|
|
2703
|
+
killTree(job.child, signal);
|
|
2704
|
+
if (signal !== "SIGKILL") {
|
|
2705
|
+
const escalate = setTimeout(() => killTree(job.child, "SIGKILL"), 5e3);
|
|
2706
|
+
escalate.unref?.();
|
|
2707
|
+
}
|
|
2708
|
+
}
|
|
2709
|
+
return this.#describe(job);
|
|
2710
|
+
}
|
|
2711
|
+
list(run) {
|
|
2712
|
+
this.#sweep();
|
|
2713
|
+
return {
|
|
2714
|
+
commands: [...this.#jobs.values()].filter((job) => job.ownerUserId === run.requesterUserId).map((job) => this.#describe(job))
|
|
2715
|
+
};
|
|
2716
|
+
}
|
|
2717
|
+
/** Commands were turned off (or the daemon stops): end every background job. */
|
|
2718
|
+
killAll(reason) {
|
|
2719
|
+
for (const [child, ended] of this.#foreground) {
|
|
2720
|
+
ended.reason = reason === "commands_off" ? "commands_off" : "cancelled";
|
|
2721
|
+
killTree(child, "SIGKILL");
|
|
2722
|
+
}
|
|
2723
|
+
for (const job of this.#jobs.values()) {
|
|
2724
|
+
if (job.state !== "running") continue;
|
|
2725
|
+
job.state = "killed";
|
|
2726
|
+
job.error = reason === "commands_off" ? "commands_off" : null;
|
|
2727
|
+
killTree(job.child, "SIGKILL");
|
|
2728
|
+
}
|
|
2729
|
+
}
|
|
2730
|
+
/** Background jobs still running (auto-update waits for them). */
|
|
2731
|
+
runningBackground() {
|
|
2732
|
+
let count = 0;
|
|
2733
|
+
for (const job of this.#jobs.values()) if (job.state === "running") count++;
|
|
2734
|
+
return count;
|
|
2735
|
+
}
|
|
2736
|
+
async idle() {
|
|
2737
|
+
await Promise.all([...this.#jobs.values()].map((job) => job.done));
|
|
2738
|
+
}
|
|
2739
|
+
#sweep() {
|
|
2740
|
+
const cutoff = Date.now() - hostCommandLimits.keepFinishedJobsMs;
|
|
2741
|
+
for (const [id, job] of this.#jobs) {
|
|
2742
|
+
if (job.endedAt !== null && job.endedAt < cutoff) this.#jobs.delete(id);
|
|
2743
|
+
}
|
|
2744
|
+
}
|
|
2745
|
+
};
|
|
2746
|
+
|
|
2747
|
+
// src/execution-context/package-graph-cache.ts
|
|
2748
|
+
import { createHash as createHash2 } from "node:crypto";
|
|
2749
|
+
function createPackageGraphCache(options = {}) {
|
|
2750
|
+
const maxEntries = options.maxEntries ?? 32;
|
|
2751
|
+
const ttlMs = options.ttlMs ?? 6e4;
|
|
2752
|
+
const now = options.now ?? Date.now;
|
|
2753
|
+
const entries = /* @__PURE__ */ new Map();
|
|
2754
|
+
return {
|
|
2755
|
+
get size() {
|
|
2756
|
+
return entries.size;
|
|
2757
|
+
},
|
|
2758
|
+
slot(scope) {
|
|
2759
|
+
const key = createHash2("sha256").update(
|
|
2760
|
+
JSON.stringify([
|
|
2761
|
+
scope.apiUrl,
|
|
2762
|
+
scope.userId,
|
|
2763
|
+
scope.orgId,
|
|
2764
|
+
scope.conversationId ?? null,
|
|
2765
|
+
scope.code
|
|
2766
|
+
])
|
|
2767
|
+
).digest("hex");
|
|
2768
|
+
return {
|
|
2769
|
+
get: () => {
|
|
2770
|
+
const entry = entries.get(key);
|
|
2771
|
+
if (!entry) return void 0;
|
|
2772
|
+
entries.delete(key);
|
|
2773
|
+
if (entry.expiresAt <= now()) return void 0;
|
|
2774
|
+
entries.set(key, entry);
|
|
2775
|
+
return entry.graph;
|
|
2776
|
+
},
|
|
2777
|
+
set: (graph) => {
|
|
2778
|
+
entries.delete(key);
|
|
2779
|
+
entries.set(key, { graph, expiresAt: now() + ttlMs });
|
|
2780
|
+
while (entries.size > maxEntries) {
|
|
2781
|
+
const oldest = entries.keys().next().value;
|
|
2782
|
+
if (oldest === void 0) break;
|
|
2783
|
+
entries.delete(oldest);
|
|
2784
|
+
}
|
|
2785
|
+
}
|
|
2786
|
+
};
|
|
2787
|
+
}
|
|
2788
|
+
};
|
|
2789
|
+
}
|
|
2790
|
+
|
|
1809
2791
|
// src/execution-context/keystore.ts
|
|
1810
2792
|
import {
|
|
1811
2793
|
createCipheriv,
|
|
1812
2794
|
createDecipheriv,
|
|
1813
|
-
randomBytes as
|
|
2795
|
+
randomBytes as randomBytes4,
|
|
1814
2796
|
scryptSync
|
|
1815
2797
|
} from "node:crypto";
|
|
1816
2798
|
import {
|
|
@@ -1872,7 +2854,7 @@ function resolveWrappingKey(input) {
|
|
|
1872
2854
|
"The key for this execution context is missing from the OS keyring. Re-register the machine with `kody context register`."
|
|
1873
2855
|
);
|
|
1874
2856
|
}
|
|
1875
|
-
const fresh =
|
|
2857
|
+
const fresh = randomBytes4(32);
|
|
1876
2858
|
try {
|
|
1877
2859
|
entry.setPassword(fresh.toString("base64url"));
|
|
1878
2860
|
} catch {
|
|
@@ -1898,7 +2880,7 @@ function fileWrappingKey(input) {
|
|
|
1898
2880
|
`The file key for this execution context is missing (${path}). Re-register the machine with \`kody connect\`.`
|
|
1899
2881
|
);
|
|
1900
2882
|
}
|
|
1901
|
-
const fresh =
|
|
2883
|
+
const fresh = randomBytes4(32);
|
|
1902
2884
|
mkdirSync3(dirname3(path), { recursive: true, mode: 448 });
|
|
1903
2885
|
writeFileSync3(path, `${fresh.toString("base64url")}
|
|
1904
2886
|
`, { mode: 384 });
|
|
@@ -1942,9 +2924,9 @@ function noWrappingKeyError() {
|
|
|
1942
2924
|
);
|
|
1943
2925
|
}
|
|
1944
2926
|
function encryptPrivateKey(privateJwk, wrapping) {
|
|
1945
|
-
const salt = wrapping.kind === "passphrase" ?
|
|
2927
|
+
const salt = wrapping.kind === "passphrase" ? randomBytes4(16) : null;
|
|
1946
2928
|
const key = wrapping.key(salt);
|
|
1947
|
-
const iv =
|
|
2929
|
+
const iv = randomBytes4(12);
|
|
1948
2930
|
const cipher = createCipheriv("aes-256-gcm", key, iv);
|
|
1949
2931
|
const data = Buffer.concat([
|
|
1950
2932
|
cipher.update(JSON.stringify(privateJwk), "utf8"),
|
|
@@ -2022,7 +3004,7 @@ function loadContext(input) {
|
|
|
2022
3004
|
}
|
|
2023
3005
|
|
|
2024
3006
|
// src/execution-context/service-commands.ts
|
|
2025
|
-
import { spawn as
|
|
3007
|
+
import { spawn as spawn3, spawnSync } from "node:child_process";
|
|
2026
3008
|
import { existsSync as existsSync4, readFileSync as readFileSync5, readdirSync as readdirSync2 } from "node:fs";
|
|
2027
3009
|
import { createInterface } from "node:readline";
|
|
2028
3010
|
import { setTimeout as sleep } from "node:timers/promises";
|
|
@@ -2037,7 +3019,7 @@ import {
|
|
|
2037
3019
|
rmSync as rmSync3,
|
|
2038
3020
|
writeFileSync as writeFileSync4
|
|
2039
3021
|
} from "node:fs";
|
|
2040
|
-
import { homedir as
|
|
3022
|
+
import { homedir as homedir3, tmpdir, userInfo } from "node:os";
|
|
2041
3023
|
import { dirname as dirname4, join as join3, sep as sep2 } from "node:path";
|
|
2042
3024
|
var serviceEnvVar = "KODY_DAEMON_SERVICE";
|
|
2043
3025
|
var stayStoppedExitCode = 0;
|
|
@@ -2061,7 +3043,7 @@ function scheduledTaskName(ecid) {
|
|
|
2061
3043
|
return `\\Kody\\${serviceName(ecid)}`;
|
|
2062
3044
|
}
|
|
2063
3045
|
function servicePaths(input) {
|
|
2064
|
-
const home = input.home ??
|
|
3046
|
+
const home = input.home ?? homedir3();
|
|
2065
3047
|
const env = input.env ?? process.env;
|
|
2066
3048
|
const dir = contextDir(input.apiUrl, input.ecid);
|
|
2067
3049
|
return {
|
|
@@ -3260,7 +4242,7 @@ ${runner ? ` Runner: ${describeHolder(runner)}
|
|
|
3260
4242
|
if (!Number.isInteger(lines) || lines < 1)
|
|
3261
4243
|
throw new Error("--lines must be a positive whole number.");
|
|
3262
4244
|
if (input.follow) {
|
|
3263
|
-
const child =
|
|
4245
|
+
const child = spawn3("tail", ["-n", String(lines), "-F", paths.logPath], {
|
|
3264
4246
|
stdio: "inherit"
|
|
3265
4247
|
});
|
|
3266
4248
|
return await new Promise((resolve) => {
|
|
@@ -3443,6 +4425,123 @@ async function runUpdate(env) {
|
|
|
3443
4425
|
return restarted === services.length ? 0 : 1;
|
|
3444
4426
|
}
|
|
3445
4427
|
|
|
4428
|
+
// src/execution-context/worker-tracker.ts
|
|
4429
|
+
import { execFileSync as execFileSync3 } from "node:child_process";
|
|
4430
|
+
import {
|
|
4431
|
+
mkdirSync as mkdirSync5,
|
|
4432
|
+
readFileSync as readFileSync6,
|
|
4433
|
+
readdirSync as readdirSync3,
|
|
4434
|
+
rmSync as rmSync4,
|
|
4435
|
+
writeFileSync as writeFileSync5
|
|
4436
|
+
} from "node:fs";
|
|
4437
|
+
import { dirname as dirname5, join as join4 } from "node:path";
|
|
4438
|
+
var defaultOps = {
|
|
4439
|
+
isAlive(pid) {
|
|
4440
|
+
try {
|
|
4441
|
+
process.kill(pid, 0);
|
|
4442
|
+
return true;
|
|
4443
|
+
} catch (error) {
|
|
4444
|
+
return error.code === "EPERM";
|
|
4445
|
+
}
|
|
4446
|
+
},
|
|
4447
|
+
commandLine(pid) {
|
|
4448
|
+
try {
|
|
4449
|
+
if (process.platform === "linux") {
|
|
4450
|
+
return readFileSync6(`/proc/${pid}/cmdline`, "utf8").replaceAll(
|
|
4451
|
+
"\0",
|
|
4452
|
+
" "
|
|
4453
|
+
);
|
|
4454
|
+
}
|
|
4455
|
+
if (process.platform === "win32") return windowsCommandLine(pid);
|
|
4456
|
+
return execFileSync3("ps", ["-p", String(pid), "-o", "command="], {
|
|
4457
|
+
encoding: "utf8",
|
|
4458
|
+
stdio: ["ignore", "pipe", "ignore"]
|
|
4459
|
+
});
|
|
4460
|
+
} catch {
|
|
4461
|
+
return null;
|
|
4462
|
+
}
|
|
4463
|
+
},
|
|
4464
|
+
kill(pid) {
|
|
4465
|
+
try {
|
|
4466
|
+
process.kill(pid, "SIGKILL");
|
|
4467
|
+
} catch {
|
|
4468
|
+
}
|
|
4469
|
+
}
|
|
4470
|
+
};
|
|
4471
|
+
function readRecord(path) {
|
|
4472
|
+
try {
|
|
4473
|
+
const value = JSON.parse(readFileSync6(path, "utf8"));
|
|
4474
|
+
return Number.isInteger(value.pid) && value.pid > 0 && typeof value.configPath === "string" && value.configPath.endsWith("config.capnp") && Number.isInteger(value.ownerPid) ? value : null;
|
|
4475
|
+
} catch {
|
|
4476
|
+
return null;
|
|
4477
|
+
}
|
|
4478
|
+
}
|
|
4479
|
+
function reapLeftoverWorkers(dir, input = {}) {
|
|
4480
|
+
const ops = input.ops ?? defaultOps;
|
|
4481
|
+
const selfPid = input.selfPid ?? process.pid;
|
|
4482
|
+
let entries;
|
|
4483
|
+
try {
|
|
4484
|
+
entries = readdirSync3(dir).filter((name) => name.endsWith(".json"));
|
|
4485
|
+
} catch {
|
|
4486
|
+
return [];
|
|
4487
|
+
}
|
|
4488
|
+
const killed = [];
|
|
4489
|
+
for (const name of entries) {
|
|
4490
|
+
const path = join4(dir, name);
|
|
4491
|
+
const record = readRecord(path);
|
|
4492
|
+
if (!record) {
|
|
4493
|
+
rmSync4(path, { force: true });
|
|
4494
|
+
continue;
|
|
4495
|
+
}
|
|
4496
|
+
if (record.ownerPid !== selfPid && ops.isAlive(record.ownerPid)) continue;
|
|
4497
|
+
if (ops.isAlive(record.pid) && ops.commandLine(record.pid)?.includes(record.configPath)) {
|
|
4498
|
+
ops.kill(record.pid);
|
|
4499
|
+
killed.push(record.pid);
|
|
4500
|
+
}
|
|
4501
|
+
rmSync4(dirname5(record.configPath), { recursive: true, force: true });
|
|
4502
|
+
rmSync4(path, { force: true });
|
|
4503
|
+
}
|
|
4504
|
+
return killed;
|
|
4505
|
+
}
|
|
4506
|
+
function createLocalWorkerTracker(dir, input = {}) {
|
|
4507
|
+
const ops = input.ops ?? defaultOps;
|
|
4508
|
+
const live = /* @__PURE__ */ new Set();
|
|
4509
|
+
const recordPath = (pid) => join4(dir, `${pid}.json`);
|
|
4510
|
+
const killAll = () => {
|
|
4511
|
+
for (const pid of live) {
|
|
4512
|
+
ops.kill(pid);
|
|
4513
|
+
rmSync4(recordPath(pid), { force: true });
|
|
4514
|
+
}
|
|
4515
|
+
live.clear();
|
|
4516
|
+
};
|
|
4517
|
+
process.on("exit", killAll);
|
|
4518
|
+
return {
|
|
4519
|
+
started({ pid, configPath }) {
|
|
4520
|
+
live.add(pid);
|
|
4521
|
+
try {
|
|
4522
|
+
mkdirSync5(dir, { recursive: true, mode: 448 });
|
|
4523
|
+
const record = {
|
|
4524
|
+
pid,
|
|
4525
|
+
configPath,
|
|
4526
|
+
ownerPid: process.pid
|
|
4527
|
+
};
|
|
4528
|
+
writeFileSync5(recordPath(pid), JSON.stringify(record), {
|
|
4529
|
+
mode: 384
|
|
4530
|
+
});
|
|
4531
|
+
} catch {
|
|
4532
|
+
}
|
|
4533
|
+
},
|
|
4534
|
+
exited(pid) {
|
|
4535
|
+
live.delete(pid);
|
|
4536
|
+
rmSync4(recordPath(pid), { force: true });
|
|
4537
|
+
},
|
|
4538
|
+
killAll,
|
|
4539
|
+
dispose() {
|
|
4540
|
+
process.off("exit", killAll);
|
|
4541
|
+
}
|
|
4542
|
+
};
|
|
4543
|
+
}
|
|
4544
|
+
|
|
3446
4545
|
// src/execution-context/commands.ts
|
|
3447
4546
|
var defaultMaxConcurrentRuns = 2;
|
|
3448
4547
|
function maxConcurrentRuns(value = process.env.KODY_CONTEXT_MAX_CONCURRENT) {
|
|
@@ -3838,7 +4937,7 @@ Run \`kody daemon\` in a terminal, or fix that and run: kody daemon install --co
|
|
|
3838
4937
|
}
|
|
3839
4938
|
const { context, privateKey } = started;
|
|
3840
4939
|
let { serverKeys } = started;
|
|
3841
|
-
const { runContextRun } = await import("./run-executor-
|
|
4940
|
+
const { runContextRun } = await import("./run-executor-6HMXYWQ5.js");
|
|
3842
4941
|
const hostCommands = new HostCommands({
|
|
3843
4942
|
localEnabled: await readLocalCommandsEnabled().catch(() => false)
|
|
3844
4943
|
});
|
|
@@ -4024,7 +5123,7 @@ Run \`kody daemon\` in a terminal, or fix that and run: kody daemon install --co
|
|
|
4024
5123
|
if (hardStop) clearTimeout(hardStop);
|
|
4025
5124
|
process.off("SIGINT", onSignal);
|
|
4026
5125
|
process.off("SIGTERM", onSignal);
|
|
4027
|
-
|
|
5126
|
+
rmSync5(statePath, { force: true });
|
|
4028
5127
|
releaseLock();
|
|
4029
5128
|
}
|
|
4030
5129
|
if (stop === "replaced") {
|
|
@@ -4244,4 +5343,4 @@ export {
|
|
|
4244
5343
|
maxConcurrentRuns,
|
|
4245
5344
|
runExecutionContextCommand
|
|
4246
5345
|
};
|
|
4247
|
-
//# sourceMappingURL=commands-
|
|
5346
|
+
//# sourceMappingURL=commands-ZJNROANP.js.map
|