@kodycodes/cli 3.22.7 → 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/README.md +8 -7
- package/dist/add-mcp-install.d.ts +1 -1
- package/dist/api-token-store.d.ts +0 -2
- package/dist/api-token.d.ts +6 -21
- package/dist/{auth-W5RWT6IP.js → auth-EEGVQLUH.js} +10 -9
- package/dist/auth-bootstrap.d.ts +4 -19
- package/dist/bin.js +15 -16
- package/dist/bin.js.map +1 -1
- package/dist/capability-proxy.d.ts +6 -36
- package/dist/{chunk-XV6D6HSG.js → chunk-42R5NM3P.js} +1 -2
- package/dist/chunk-42R5NM3P.js.map +6 -0
- package/dist/{chunk-ZUROJOJ2.js → chunk-4AKERLPX.js} +3 -5
- package/dist/chunk-4AKERLPX.js.map +6 -0
- package/dist/{chunk-FPYLVYHC.js → chunk-4Q5BFP2W.js} +64 -249
- package/dist/chunk-4Q5BFP2W.js.map +6 -0
- package/dist/{chunk-FHLTB67D.js → chunk-5W34SXJW.js} +8 -119
- package/dist/chunk-5W34SXJW.js.map +6 -0
- package/dist/{chunk-K2G6GYKA.js → chunk-62N7DN7L.js} +1 -1
- package/dist/{chunk-K2G6GYKA.js.map → chunk-62N7DN7L.js.map} +1 -1
- package/dist/{chunk-DBNNDB7I.js → chunk-7CKXJKIA.js} +9 -9
- package/dist/{chunk-DBNNDB7I.js.map → chunk-7CKXJKIA.js.map} +1 -1
- package/dist/{chunk-OKM2QMOV.js → chunk-7JU4GNCS.js} +16 -116
- 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-2FU3OO7F.js → chunk-BWZ3ZIQO.js} +5 -19
- package/dist/chunk-BWZ3ZIQO.js.map +6 -0
- package/dist/{chunk-4LAKIU3Y.js → chunk-C7GRF5V5.js} +2 -2
- package/dist/{chunk-SLNAWWQK.js → chunk-E4R3Y6MM.js} +1 -1
- package/dist/{chunk-SLNAWWQK.js.map → chunk-E4R3Y6MM.js.map} +1 -1
- package/dist/{chunk-G7GRULRW.js → chunk-EMEUJKXR.js} +2 -2
- package/dist/{chunk-5E2HSGBZ.js → chunk-FY4SRIOG.js} +4 -5
- package/dist/chunk-FY4SRIOG.js.map +6 -0
- package/dist/{chunk-F3XGCTRQ.js → chunk-IIGF3ZWU.js} +137 -82
- package/dist/chunk-IIGF3ZWU.js.map +6 -0
- package/dist/{chunk-OXBMXZ6S.js → chunk-JSNRL2K6.js} +3 -167
- package/dist/{chunk-OXBMXZ6S.js.map → chunk-JSNRL2K6.js.map} +3 -3
- package/dist/{chunk-RD6ZJU2N.js → chunk-KZQLRBP2.js} +5 -5
- package/dist/{chunk-76OYRT4A.js → chunk-LQNGII7U.js} +1 -1
- package/dist/{chunk-76OYRT4A.js.map → chunk-LQNGII7U.js.map} +1 -1
- package/dist/{chunk-2XPH7L2P.js → chunk-MIT3TUWB.js} +1 -1
- package/dist/{chunk-2XPH7L2P.js.map → chunk-MIT3TUWB.js.map} +1 -1
- package/dist/{chunk-VP2O3HLT.js → chunk-NKH2L5RA.js} +17 -1
- package/dist/chunk-NKH2L5RA.js.map +6 -0
- package/dist/{chunk-M7PRSUCC.js → chunk-NKQRWYSY.js} +4 -2
- package/dist/chunk-NKQRWYSY.js.map +6 -0
- package/dist/{chunk-D6XME4RY.js → chunk-RE7LNPRO.js} +2 -5
- package/dist/{chunk-D6XME4RY.js.map → chunk-RE7LNPRO.js.map} +1 -1
- package/dist/chunk-SLHFID52.js +107 -0
- package/dist/chunk-SLHFID52.js.map +6 -0
- package/dist/{chunk-YQRMKXCC.js → chunk-V4SN6Y7O.js} +4 -6
- package/dist/chunk-V4SN6Y7O.js.map +6 -0
- package/dist/{chunk-DEI4SMFK.js → chunk-XXCAGPVV.js} +7 -9
- 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-ZXNYKFSN.js → chunk-YLDI65PR.js} +153 -153
- package/dist/chunk-YLDI65PR.js.map +6 -0
- package/dist/{chunk-NPQ2XAK5.js → chunk-YYCR2HQJ.js} +3 -3
- package/dist/chunk-YYCR2HQJ.js.map +6 -0
- package/dist/cli.d.ts +10 -1
- package/dist/cli.js +17 -16
- package/dist/{commands-MZMI3MEY.js → commands-ZJNROANP.js} +1158 -73
- package/dist/commands-ZJNROANP.js.map +6 -0
- package/dist/credential-call-map.d.ts +0 -6
- package/dist/credential-dir.d.ts +0 -2
- package/dist/credentialed-fetch-scan.d.ts +2 -2
- package/dist/defaults.d.ts +5 -5
- package/dist/detect-running-hosts.d.ts +1 -1
- package/dist/dev/dev-assets.d.ts +1 -3
- package/dist/dev/dev-bundler.d.ts +0 -5
- package/dist/dev/dev-events.d.ts +2 -2
- 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 +3 -5
- package/dist/dev/live-reload.d.ts +4 -3
- package/dist/dev/workerd-process.d.ts +0 -1
- package/dist/{dev-command-XIQY56LW.js → dev-command-TDLNJ7ZA.js} +22 -23
- package/dist/dev-command-TDLNJ7ZA.js.map +6 -0
- package/dist/dev-command.d.ts +1 -10
- package/dist/discover-login-identity.d.ts +1 -1
- package/dist/execution-context/auto-update.d.ts +1 -66
- package/dist/execution-context/client.d.ts +2 -3
- package/dist/execution-context/commands.d.ts +0 -11
- package/dist/execution-context/daemon.d.ts +2 -7
- package/dist/execution-context/fs-access.d.ts +5 -4
- package/dist/execution-context/host-commands.d.ts +3 -47
- package/dist/execution-context/host-fs-bridge.d.ts +0 -2
- package/dist/execution-context/keystore.d.ts +0 -1
- package/dist/execution-context/package-graph-cache.d.ts +1 -1
- package/dist/execution-context/run-executor.d.ts +6 -6
- package/dist/execution-context/runner-lock.d.ts +0 -8
- package/dist/execution-context/service-commands.d.ts +17 -8
- package/dist/execution-context/service.d.ts +3 -13
- package/dist/execution-context/windows-command.d.ts +0 -2
- package/dist/fetch-body-secret-resolution.d.ts +0 -1
- package/dist/{git-credential-BT363OJQ.js → git-credential-QEMS2OZF.js} +11 -10
- package/dist/git-credential-QEMS2OZF.js.map +6 -0
- package/dist/help.d.ts +0 -6
- package/dist/host-catalog.d.ts +2 -5
- package/dist/host-config.d.ts +2 -3
- package/dist/{install-NTS5BOCR.js → install-MPB5WEG4.js} +3 -5
- package/dist/{install-NTS5BOCR.js.map → install-MPB5WEG4.js.map} +1 -1
- package/dist/install-hosts.d.ts +2 -1
- package/dist/install.d.ts +1 -6
- package/dist/invoke-passthrough.d.ts +0 -1
- package/dist/local-egress.d.ts +2 -1
- package/dist/{chunk-SLBG76VX.js → local-execute-3I5YDMM5.js} +30 -16
- 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-WFDNSNOJ.js → local-package-graph-PMZQD7WF.js} +9 -18
- package/dist/local-package-graph.d.ts +0 -34
- package/dist/local-package-run-SJIAEVMD.js +34 -0
- package/dist/local-package-run.d.ts +2 -2
- package/dist/local-runtime-bridge.d.ts +2 -5
- package/dist/local-runtime-source.d.ts +0 -2
- package/dist/login-callback-page.d.ts +3 -2
- package/dist/mock/install-mock-fetch-shim.d.ts +1 -1
- package/dist/mock/load-package-mocks.d.ts +0 -1
- package/dist/mock/mock-fetch-gateway.d.ts +1 -4
- package/dist/mock/mock-server.d.ts +0 -1
- package/dist/mock/mock-storage.d.ts +2 -2
- package/dist/mock/mock-supported.d.ts +2 -1
- package/dist/{mock-dep-resolve-G7TA4CJB.js → mock-dep-resolve-4WSWOQHT.js} +7 -14
- package/dist/mock-dep-resolve-4WSWOQHT.js.map +6 -0
- package/dist/{mock-fetch-gateway-QNUFNYAH.js → mock-fetch-gateway-5K7STXZQ.js} +5 -10
- package/dist/{mock-local-deps-K2NARQ7F.js → mock-local-deps-7GS6RPYL.js} +12 -14
- package/dist/mock-local-deps.d.ts +0 -4
- package/dist/{mock-server-2FMSQMBT.js → mock-server-WOSXMC2L.js} +6 -9
- package/dist/node-engine.d.ts +0 -2
- package/dist/oauth-browser.d.ts +0 -3
- package/dist/oauth-provider.d.ts +1 -1
- package/dist/{open-api-client-DJL2PE7P.js → open-api-client-A26G263D.js} +9 -16
- package/dist/open-api-client.d.ts +1 -7
- package/dist/{org-SGFIXD42.js → org-NWSSC7L7.js} +3 -10
- package/dist/org-NWSSC7L7.js.map +6 -0
- package/dist/org.d.ts +0 -2
- package/dist/package-app-dev.d.ts +1 -1
- package/dist/package-collaborators.d.ts +2 -4
- package/dist/package-local-runtime-source.d.ts +2 -2
- package/dist/{package-manifest-KCLPIMOZ.js → package-manifest-OFJ5PRA2.js} +3 -4
- package/dist/package-manifest.d.ts +7 -6
- package/dist/process-info.d.ts +0 -2
- package/dist/publish-exit.d.ts +0 -11
- package/dist/{remote-execute-KUSUI6UT.js → remote-execute-PDSZBGQ7.js} +10 -9
- package/dist/remote-execute-PDSZBGQ7.js.map +6 -0
- package/dist/remote-execute.d.ts +2 -2
- package/dist/{run-executor-4TY5KNQQ.js → run-executor-6HMXYWQ5.js} +3 -21
- package/dist/run-executor-6HMXYWQ5.js.map +6 -0
- package/dist/{setup-git-KBH7GA6E.js → setup-git-7PQUU6JB.js} +10 -39
- package/dist/setup-git.d.ts +6 -60
- package/dist/{skill-IY263IXV.js → skill-QM3N6H55.js} +3 -3
- package/dist/skill-refresh.js +2 -2
- package/dist/{store-ZPBJTYRI.js → store-4G2J5DRZ.js} +3 -9
- package/dist/store.d.ts +2 -6
- package/dist/{structured-error-QYNRZZKC.js → structured-error-D2G3PBEZ.js} +2 -5
- package/dist/structured-error.d.ts +3 -3
- package/dist/{chunk-MHN2FS4K.js → target-LQ247AM3.js} +4 -4
- package/dist/target-LQ247AM3.js.map +6 -0
- package/dist/{whoami-SGE7VKGY.js → whoami-JGNVG2UX.js} +7 -7
- package/dist/whoami-JGNVG2UX.js.map +6 -0
- package/dist/whoami.d.ts +0 -11
- package/dist/workerd-binary.d.ts +2 -3
- package/package.json +1 -1
- package/dist/chunk-2FU3OO7F.js.map +0 -6
- package/dist/chunk-5E2HSGBZ.js.map +0 -6
- package/dist/chunk-7PRJ36I5.js +0 -23
- package/dist/chunk-7PRJ36I5.js.map +0 -6
- package/dist/chunk-CWQVWIAQ.js +0 -994
- package/dist/chunk-CWQVWIAQ.js.map +0 -6
- package/dist/chunk-DEI4SMFK.js.map +0 -6
- package/dist/chunk-F3XGCTRQ.js.map +0 -6
- package/dist/chunk-FHLTB67D.js.map +0 -6
- package/dist/chunk-FPYLVYHC.js.map +0 -6
- package/dist/chunk-M7PRSUCC.js.map +0 -6
- package/dist/chunk-MHN2FS4K.js.map +0 -6
- package/dist/chunk-NPQ2XAK5.js.map +0 -6
- package/dist/chunk-OKM2QMOV.js.map +0 -6
- package/dist/chunk-SLBG76VX.js.map +0 -6
- package/dist/chunk-VP2O3HLT.js.map +0 -6
- package/dist/chunk-XV6D6HSG.js.map +0 -6
- package/dist/chunk-YQRMKXCC.js.map +0 -6
- package/dist/chunk-ZUROJOJ2.js.map +0 -6
- package/dist/chunk-ZXNYKFSN.js.map +0 -6
- package/dist/commands-MZMI3MEY.js.map +0 -6
- package/dist/dev-command-XIQY56LW.js.map +0 -6
- package/dist/git-credential-BT363OJQ.js.map +0 -6
- package/dist/local-execute-BI5253MQ.js +0 -39
- package/dist/local-package-run-RRELUZEM.js +0 -33
- package/dist/mock-dep-resolve-G7TA4CJB.js.map +0 -6
- package/dist/org-SGFIXD42.js.map +0 -6
- package/dist/remote-execute-KUSUI6UT.js.map +0 -6
- package/dist/run-executor-4TY5KNQQ.js.map +0 -6
- package/dist/structured-error-QYNRZZKC.js.map +0 -6
- package/dist/target-UQBEV64U.js +0 -15
- package/dist/target-UQBEV64U.js.map +0 -6
- package/dist/whoami-SGE7VKGY.js.map +0 -6
- /package/dist/{auth-W5RWT6IP.js.map → auth-EEGVQLUH.js.map} +0 -0
- /package/dist/{chunk-4LAKIU3Y.js.map → chunk-C7GRF5V5.js.map} +0 -0
- /package/dist/{chunk-G7GRULRW.js.map → chunk-EMEUJKXR.js.map} +0 -0
- /package/dist/{chunk-RD6ZJU2N.js.map → chunk-KZQLRBP2.js.map} +0 -0
- /package/dist/{local-execute-BI5253MQ.js.map → local-package-graph-PMZQD7WF.js.map} +0 -0
- /package/dist/{local-package-graph-WFDNSNOJ.js.map → local-package-run-SJIAEVMD.js.map} +0 -0
- /package/dist/{local-package-run-RRELUZEM.js.map → mock-fetch-gateway-5K7STXZQ.js.map} +0 -0
- /package/dist/{mock-fetch-gateway-QNUFNYAH.js.map → mock-local-deps-7GS6RPYL.js.map} +0 -0
- /package/dist/{mock-local-deps-K2NARQ7F.js.map → mock-server-WOSXMC2L.js.map} +0 -0
- /package/dist/{mock-server-2FMSQMBT.js.map → open-api-client-A26G263D.js.map} +0 -0
- /package/dist/{open-api-client-DJL2PE7P.js.map → package-manifest-OFJ5PRA2.js.map} +0 -0
- /package/dist/{package-manifest-KCLPIMOZ.js.map → setup-git-7PQUU6JB.js.map} +0 -0
- /package/dist/{setup-git-KBH7GA6E.js.map → skill-QM3N6H55.js.map} +0 -0
- /package/dist/{skill-IY263IXV.js.map → store-4G2J5DRZ.js.map} +0 -0
- /package/dist/{store-ZPBJTYRI.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-CWQVWIAQ.js";
|
|
25
2
|
import {
|
|
26
3
|
allowLocalPath,
|
|
27
|
-
createLocalWorkerTracker,
|
|
28
|
-
createPackageGraphCache,
|
|
29
4
|
effectiveFs,
|
|
30
5
|
emptyEffectiveFs,
|
|
31
6
|
fsFeatures,
|
|
@@ -33,38 +8,370 @@ 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
|
|
52
|
-
} from "./chunk-
|
|
27
|
+
} from "./chunk-RE7LNPRO.js";
|
|
53
28
|
import {
|
|
54
29
|
keyringService
|
|
55
|
-
} from "./chunk-
|
|
30
|
+
} from "./chunk-NKH2L5RA.js";
|
|
56
31
|
import "./chunk-UW6AWLXX.js";
|
|
57
32
|
|
|
58
33
|
// src/execution-context/commands.ts
|
|
59
|
-
import {
|
|
60
|
-
appendFileSync,
|
|
61
|
-
readFileSync as readFileSync6,
|
|
62
|
-
rmSync as rmSync4,
|
|
63
|
-
unwatchFile,
|
|
64
|
-
watchFile
|
|
65
|
-
} from "node:fs";
|
|
34
|
+
import { appendFileSync, rmSync as rmSync5, unwatchFile, watchFile } from "node:fs";
|
|
66
35
|
import { cpus, platform, totalmem } from "node:os";
|
|
67
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
|
+
|
|
68
375
|
// src/execution-context/client.ts
|
|
69
376
|
var executionContextsPath = "/v1/execution-contexts";
|
|
70
377
|
var reloginHint = "Run `kody login` again, then retry `kody context register`.";
|
|
@@ -1812,11 +2119,680 @@ async function otherLocalDaemons(input) {
|
|
|
1812
2119
|
);
|
|
1813
2120
|
}
|
|
1814
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
|
+
|
|
1815
2791
|
// src/execution-context/keystore.ts
|
|
1816
2792
|
import {
|
|
1817
2793
|
createCipheriv,
|
|
1818
2794
|
createDecipheriv,
|
|
1819
|
-
randomBytes as
|
|
2795
|
+
randomBytes as randomBytes4,
|
|
1820
2796
|
scryptSync
|
|
1821
2797
|
} from "node:crypto";
|
|
1822
2798
|
import {
|
|
@@ -1878,7 +2854,7 @@ function resolveWrappingKey(input) {
|
|
|
1878
2854
|
"The key for this execution context is missing from the OS keyring. Re-register the machine with `kody context register`."
|
|
1879
2855
|
);
|
|
1880
2856
|
}
|
|
1881
|
-
const fresh =
|
|
2857
|
+
const fresh = randomBytes4(32);
|
|
1882
2858
|
try {
|
|
1883
2859
|
entry.setPassword(fresh.toString("base64url"));
|
|
1884
2860
|
} catch {
|
|
@@ -1904,7 +2880,7 @@ function fileWrappingKey(input) {
|
|
|
1904
2880
|
`The file key for this execution context is missing (${path}). Re-register the machine with \`kody connect\`.`
|
|
1905
2881
|
);
|
|
1906
2882
|
}
|
|
1907
|
-
const fresh =
|
|
2883
|
+
const fresh = randomBytes4(32);
|
|
1908
2884
|
mkdirSync3(dirname3(path), { recursive: true, mode: 448 });
|
|
1909
2885
|
writeFileSync3(path, `${fresh.toString("base64url")}
|
|
1910
2886
|
`, { mode: 384 });
|
|
@@ -1948,9 +2924,9 @@ function noWrappingKeyError() {
|
|
|
1948
2924
|
);
|
|
1949
2925
|
}
|
|
1950
2926
|
function encryptPrivateKey(privateJwk, wrapping) {
|
|
1951
|
-
const salt = wrapping.kind === "passphrase" ?
|
|
2927
|
+
const salt = wrapping.kind === "passphrase" ? randomBytes4(16) : null;
|
|
1952
2928
|
const key = wrapping.key(salt);
|
|
1953
|
-
const iv =
|
|
2929
|
+
const iv = randomBytes4(12);
|
|
1954
2930
|
const cipher = createCipheriv("aes-256-gcm", key, iv);
|
|
1955
2931
|
const data = Buffer.concat([
|
|
1956
2932
|
cipher.update(JSON.stringify(privateJwk), "utf8"),
|
|
@@ -2028,7 +3004,7 @@ function loadContext(input) {
|
|
|
2028
3004
|
}
|
|
2029
3005
|
|
|
2030
3006
|
// src/execution-context/service-commands.ts
|
|
2031
|
-
import { spawn as
|
|
3007
|
+
import { spawn as spawn3, spawnSync } from "node:child_process";
|
|
2032
3008
|
import { existsSync as existsSync4, readFileSync as readFileSync5, readdirSync as readdirSync2 } from "node:fs";
|
|
2033
3009
|
import { createInterface } from "node:readline";
|
|
2034
3010
|
import { setTimeout as sleep } from "node:timers/promises";
|
|
@@ -2043,7 +3019,7 @@ import {
|
|
|
2043
3019
|
rmSync as rmSync3,
|
|
2044
3020
|
writeFileSync as writeFileSync4
|
|
2045
3021
|
} from "node:fs";
|
|
2046
|
-
import { homedir as
|
|
3022
|
+
import { homedir as homedir3, tmpdir, userInfo } from "node:os";
|
|
2047
3023
|
import { dirname as dirname4, join as join3, sep as sep2 } from "node:path";
|
|
2048
3024
|
var serviceEnvVar = "KODY_DAEMON_SERVICE";
|
|
2049
3025
|
var stayStoppedExitCode = 0;
|
|
@@ -2067,7 +3043,7 @@ function scheduledTaskName(ecid) {
|
|
|
2067
3043
|
return `\\Kody\\${serviceName(ecid)}`;
|
|
2068
3044
|
}
|
|
2069
3045
|
function servicePaths(input) {
|
|
2070
|
-
const home = input.home ??
|
|
3046
|
+
const home = input.home ?? homedir3();
|
|
2071
3047
|
const env = input.env ?? process.env;
|
|
2072
3048
|
const dir = contextDir(input.apiUrl, input.ecid);
|
|
2073
3049
|
return {
|
|
@@ -3266,7 +4242,7 @@ ${runner ? ` Runner: ${describeHolder(runner)}
|
|
|
3266
4242
|
if (!Number.isInteger(lines) || lines < 1)
|
|
3267
4243
|
throw new Error("--lines must be a positive whole number.");
|
|
3268
4244
|
if (input.follow) {
|
|
3269
|
-
const child =
|
|
4245
|
+
const child = spawn3("tail", ["-n", String(lines), "-F", paths.logPath], {
|
|
3270
4246
|
stdio: "inherit"
|
|
3271
4247
|
});
|
|
3272
4248
|
return await new Promise((resolve) => {
|
|
@@ -3449,6 +4425,123 @@ async function runUpdate(env) {
|
|
|
3449
4425
|
return restarted === services.length ? 0 : 1;
|
|
3450
4426
|
}
|
|
3451
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
|
+
|
|
3452
4545
|
// src/execution-context/commands.ts
|
|
3453
4546
|
var defaultMaxConcurrentRuns = 2;
|
|
3454
4547
|
function maxConcurrentRuns(value = process.env.KODY_CONTEXT_MAX_CONCURRENT) {
|
|
@@ -3594,9 +4687,19 @@ Store the machine key wrapping key in a file instead (only readable by your user
|
|
|
3594
4687
|
capabilities: localCapabilities(),
|
|
3595
4688
|
client: { name: "kody-cli", version: input.clientVersion }
|
|
3596
4689
|
});
|
|
3597
|
-
let wrapping;
|
|
3598
4690
|
try {
|
|
3599
|
-
|
|
4691
|
+
saveContext({
|
|
4692
|
+
context: {
|
|
4693
|
+
version: 1,
|
|
4694
|
+
ecid: registered.ecid,
|
|
4695
|
+
name: registered.name,
|
|
4696
|
+
apiUrl: input.apiUrl,
|
|
4697
|
+
orgSlug: registered.owner.orgSlug,
|
|
4698
|
+
publicKeyJwk
|
|
4699
|
+
},
|
|
4700
|
+
privateJwk: privateKeyJwk,
|
|
4701
|
+
wrapping: await wrappingFor(registered.ecid)
|
|
4702
|
+
});
|
|
3600
4703
|
} catch (error) {
|
|
3601
4704
|
const path = `/v1/execution-contexts/${encodeURIComponent(registered.ecid)}`;
|
|
3602
4705
|
const token = await input.token();
|
|
@@ -3606,18 +4709,6 @@ Store the machine key wrapping key in a file instead (only readable by your user
|
|
|
3606
4709
|
});
|
|
3607
4710
|
throw error;
|
|
3608
4711
|
}
|
|
3609
|
-
saveContext({
|
|
3610
|
-
context: {
|
|
3611
|
-
version: 1,
|
|
3612
|
-
ecid: registered.ecid,
|
|
3613
|
-
name: registered.name,
|
|
3614
|
-
apiUrl: input.apiUrl,
|
|
3615
|
-
orgSlug: registered.owner.orgSlug,
|
|
3616
|
-
publicKeyJwk
|
|
3617
|
-
},
|
|
3618
|
-
privateJwk: privateKeyJwk,
|
|
3619
|
-
wrapping
|
|
3620
|
-
});
|
|
3621
4712
|
const steps = (registered.nextSteps ?? []).map(
|
|
3622
4713
|
(step) => ` ${step.text ?? ""}${step.cli ? `
|
|
3623
4714
|
${step.cli}` : ""}`
|
|
@@ -3846,7 +4937,7 @@ Run \`kody daemon\` in a terminal, or fix that and run: kody daemon install --co
|
|
|
3846
4937
|
}
|
|
3847
4938
|
const { context, privateKey } = started;
|
|
3848
4939
|
let { serverKeys } = started;
|
|
3849
|
-
const { runContextRun } = await import("./run-executor-
|
|
4940
|
+
const { runContextRun } = await import("./run-executor-6HMXYWQ5.js");
|
|
3850
4941
|
const hostCommands = new HostCommands({
|
|
3851
4942
|
localEnabled: await readLocalCommandsEnabled().catch(() => false)
|
|
3852
4943
|
});
|
|
@@ -4032,7 +5123,7 @@ Run \`kody daemon\` in a terminal, or fix that and run: kody daemon install --co
|
|
|
4032
5123
|
if (hardStop) clearTimeout(hardStop);
|
|
4033
5124
|
process.off("SIGINT", onSignal);
|
|
4034
5125
|
process.off("SIGTERM", onSignal);
|
|
4035
|
-
|
|
5126
|
+
rmSync5(statePath, { force: true });
|
|
4036
5127
|
releaseLock();
|
|
4037
5128
|
}
|
|
4038
5129
|
if (stop === "replaced") {
|
|
@@ -4111,9 +5202,6 @@ function describeChain(chain, contexts = /* @__PURE__ */ new Map()) {
|
|
|
4111
5202
|
if (chain.strict) lines.push("Strict: Kody cloud is not in this chain.");
|
|
4112
5203
|
return lines.join("\n") || "Empty chain.";
|
|
4113
5204
|
}
|
|
4114
|
-
function readCodeFile(path) {
|
|
4115
|
-
return readFileSync6(path, "utf8");
|
|
4116
|
-
}
|
|
4117
5205
|
var policyUsage = "Usage: kody context policy [show] | kody context policy allow <path> [--mode ro|rw] | kody context policy remove <path>";
|
|
4118
5206
|
var commandsUsage = "Usage: kody context commands [status | on | off] [--context <name>] [--json]";
|
|
4119
5207
|
async function runCommandsOptInCommand(input, [action = "status", ...extra]) {
|
|
@@ -4252,10 +5340,7 @@ function appendOutputTo(path) {
|
|
|
4252
5340
|
}
|
|
4253
5341
|
}
|
|
4254
5342
|
export {
|
|
4255
|
-
defaultMaxConcurrentRuns,
|
|
4256
|
-
localCapabilities,
|
|
4257
5343
|
maxConcurrentRuns,
|
|
4258
|
-
readCodeFile,
|
|
4259
5344
|
runExecutionContextCommand
|
|
4260
5345
|
};
|
|
4261
|
-
//# sourceMappingURL=commands-
|
|
5346
|
+
//# sourceMappingURL=commands-ZJNROANP.js.map
|