@air-jam/devtools 0.9.3 → 1.0.0-beta.1
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 +60 -0
- package/dist/{chunk-VKMJO6VN.js → chunk-2O67AXCU.js} +150 -40
- package/dist/chunk-2O67AXCU.js.map +1 -0
- package/dist/chunk-AO3ZDXF6.js +142 -0
- package/dist/chunk-AO3ZDXF6.js.map +1 -0
- package/dist/chunk-ART5TEYT.js +472 -0
- package/dist/chunk-ART5TEYT.js.map +1 -0
- package/dist/chunk-ASC2CK2R.js +77 -0
- package/dist/chunk-ASC2CK2R.js.map +1 -0
- package/dist/chunk-E3UZZGAV.js +50 -0
- package/dist/chunk-E3UZZGAV.js.map +1 -0
- package/dist/chunk-EJTE7UKX.js +64 -0
- package/dist/chunk-EJTE7UKX.js.map +1 -0
- package/dist/chunk-ESFKNN75.js +110 -0
- package/dist/chunk-ESFKNN75.js.map +1 -0
- package/dist/chunk-EU7A6PXW.js +1339 -0
- package/dist/chunk-EU7A6PXW.js.map +1 -0
- package/dist/chunk-FN7E6EA7.js +549 -0
- package/dist/chunk-FN7E6EA7.js.map +1 -0
- package/dist/chunk-GDFAWHGA.js +190 -0
- package/dist/chunk-GDFAWHGA.js.map +1 -0
- package/dist/chunk-HZUGBH4Y.js +347 -0
- package/dist/chunk-HZUGBH4Y.js.map +1 -0
- package/dist/chunk-ITKAOEGX.js +93 -0
- package/dist/chunk-ITKAOEGX.js.map +1 -0
- package/dist/chunk-JBCUTWRI.js +897 -0
- package/dist/chunk-JBCUTWRI.js.map +1 -0
- package/dist/chunk-JSI5S4WP.js +1174 -0
- package/dist/chunk-JSI5S4WP.js.map +1 -0
- package/dist/chunk-L4D675BG.js +97 -0
- package/dist/chunk-L4D675BG.js.map +1 -0
- package/dist/chunk-M6CJ7GQC.js +133 -0
- package/dist/chunk-M6CJ7GQC.js.map +1 -0
- package/dist/{chunk-CKFT2ZJE.js → chunk-MEIMOSZC.js} +53 -105
- package/dist/chunk-MEIMOSZC.js.map +1 -0
- package/dist/{chunk-GUEZP773.js → chunk-O3XG3FNL.js} +204 -41
- package/dist/chunk-O3XG3FNL.js.map +1 -0
- package/dist/chunk-OM6SCJ7H.js +18 -0
- package/dist/chunk-OM6SCJ7H.js.map +1 -0
- package/dist/chunk-PKD4WVX5.js +63 -0
- package/dist/chunk-PKD4WVX5.js.map +1 -0
- package/dist/chunk-PMBLXGFM.js +3384 -0
- package/dist/chunk-PMBLXGFM.js.map +1 -0
- package/dist/chunk-PMNE5GIN.js +279 -0
- package/dist/chunk-PMNE5GIN.js.map +1 -0
- package/dist/chunk-QAWI64VR.js +148 -0
- package/dist/chunk-QAWI64VR.js.map +1 -0
- package/dist/chunk-QGADSPHZ.js +449 -0
- package/dist/chunk-QGADSPHZ.js.map +1 -0
- package/dist/chunk-SPYN66UV.js +338 -0
- package/dist/chunk-SPYN66UV.js.map +1 -0
- package/dist/context.d.ts +9 -3
- package/dist/context.js +2 -1
- package/dist/controller.d.ts +33 -7
- package/dist/controller.js +18 -8
- package/dist/dependencies.d.ts +44 -0
- package/dist/dependencies.js +10 -0
- package/dist/dev.d.ts +9 -3
- package/dist/dev.js +3 -4
- package/dist/game-builds.d.ts +200 -0
- package/dist/game-builds.js +16 -0
- package/dist/game-builds.js.map +1 -0
- package/dist/game-run-watch-CEd_SpAT.d.ts +84 -0
- package/dist/game-runs.d.ts +1452 -0
- package/dist/game-runs.js +76 -0
- package/dist/game-runs.js.map +1 -0
- package/dist/game-session-broker-CUvORfMd.d.ts +439 -0
- package/dist/game-session-broker.d.ts +4 -57
- package/dist/game-session-broker.js +33 -228
- package/dist/game-session-broker.js.map +1 -1
- package/dist/game-session.d.ts +3931 -10
- package/dist/game-session.js +186 -15
- package/dist/game-session.js.map +1 -1
- package/dist/game-settings.d.ts +16 -0
- package/dist/game-settings.js +237 -0
- package/dist/game-settings.js.map +1 -0
- package/dist/games.d.ts +11 -4
- package/dist/games.js +4 -2
- package/dist/harness/visual.d.ts +34 -151
- package/dist/harness/visual.js +6 -1
- package/dist/logs.d.ts +8 -2
- package/dist/logs.js +3 -4
- package/dist/logs.js.map +1 -1
- package/dist/managed-process-scope-BicYi_t9.d.ts +3 -0
- package/dist/mcp-config.js +2 -1
- package/dist/mcp-config.js.map +1 -1
- package/dist/platform-auth.d.ts +8 -111
- package/dist/platform-auth.js +9 -1
- package/dist/platform-game-analytics.d.ts +51 -0
- package/dist/platform-game-analytics.js +49 -0
- package/dist/platform-game-analytics.js.map +1 -0
- package/dist/platform-game-app-id.d.ts +46 -0
- package/dist/platform-game-app-id.js +93 -0
- package/dist/platform-game-app-id.js.map +1 -0
- package/dist/platform-game-media.d.ts +40 -2
- package/dist/platform-game-media.js +1 -1
- package/dist/platform-games.d.ts +24 -2
- package/dist/platform-games.js +16 -84
- package/dist/platform-games.js.map +1 -1
- package/dist/quality.d.ts +9 -3
- package/dist/quality.js +3 -4
- package/dist/quality.js.map +1 -1
- package/dist/release.d.ts +226 -16
- package/dist/release.js +18 -1331
- package/dist/release.js.map +1 -1
- package/dist/rooms-check.d.ts +90 -0
- package/dist/rooms-check.js +717 -0
- package/dist/rooms-check.js.map +1 -0
- package/dist/source-tree-contract.d.ts +16 -0
- package/dist/source-tree-contract.js +13 -0
- package/dist/source-tree-contract.js.map +1 -0
- package/dist/testing.d.ts +464 -0
- package/dist/testing.js +27 -0
- package/dist/testing.js.map +1 -0
- package/dist/tooling/describe-game.js +54 -0
- package/dist/tooling/describe-game.js.map +1 -0
- package/dist/tooling/game-session-broker.js +28 -0
- package/dist/tooling/game-session-broker.js.map +1 -0
- package/dist/tooling/hold-runtime-host.js +769 -67
- package/dist/tooling/hold-runtime-host.js.map +1 -1
- package/dist/tooling/list-visual-scenarios.js +18 -55
- package/dist/tooling/list-visual-scenarios.js.map +1 -1
- package/dist/tooling/process-scope-bootstrap.mjs +24 -0
- package/dist/tooling/process-scope-guard.d.ts +2 -0
- package/dist/tooling/process-scope-guard.js +388 -0
- package/dist/tooling/process-scope-guard.js.map +1 -0
- package/dist/tooling/process-scope-windows.ps1 +148 -0
- package/dist/tooling/rooms-check-device.d.ts +2 -0
- package/dist/tooling/rooms-check-device.js +498 -0
- package/dist/tooling/rooms-check-device.js.map +1 -0
- package/dist/tooling/run-headless-room.d.ts +2 -0
- package/dist/tooling/run-headless-room.js +287 -0
- package/dist/tooling/run-headless-room.js.map +1 -0
- package/dist/tooling/run-test-profile.d.ts +2 -0
- package/dist/tooling/run-test-profile.js +181 -0
- package/dist/tooling/run-test-profile.js.map +1 -0
- package/dist/tooling/run-visual-capture.js +70 -34
- package/dist/tooling/run-visual-capture.js.map +1 -1
- package/dist/tooling/seal-game-build.d.ts +2 -0
- package/dist/tooling/seal-game-build.js +120 -0
- package/dist/tooling/seal-game-build.js.map +1 -0
- package/dist/{types-C6NrHt88.d.ts → types-CdYrgrG_.d.ts} +337 -76
- package/dist/types-DyRD-ym1.d.ts +141 -0
- package/dist/visual.d.ts +9 -3
- package/dist/visual.js +24 -68
- package/dist/visual.js.map +1 -1
- package/package.json +51 -14
- package/src/bounded-response.ts +28 -0
- package/src/built-game-runtime.ts +152 -0
- package/src/context.ts +2 -13
- package/src/controller-protocol.ts +22 -0
- package/src/controller.ts +747 -597
- package/src/current-room-host.ts +166 -0
- package/src/decision-provider.ts +102 -0
- package/src/dependencies.ts +176 -0
- package/src/dependency-install-contract.ts +12 -0
- package/src/dev.ts +46 -50
- package/src/game-action-observation.ts +11 -1
- package/src/game-build-protocol.ts +72 -0
- package/src/game-build.ts +483 -0
- package/src/game-run-owner.ts +2358 -0
- package/src/game-run-protocol.ts +448 -0
- package/src/game-run-record.ts +92 -0
- package/src/game-run-replay.ts +95 -0
- package/src/game-run-watch.ts +155 -0
- package/src/game-session-broker.ts +689 -197
- package/src/game-session-operations.ts +61 -0
- package/src/game-session-protocol.ts +180 -0
- package/src/game-session-runtime.ts +861 -0
- package/src/game-session.ts +164 -417
- package/src/game-settings.ts +295 -0
- package/src/games.ts +88 -92
- package/src/harness/visual/browser.ts +100 -0
- package/src/harness/visual/controller-observation.ts +181 -0
- package/src/harness/visual/runner.ts +24 -21
- package/src/harness/visual/runtime-controller-observations.ts +151 -0
- package/src/harness/visual/scenario-contract.ts +1 -2
- package/src/harness/visual/scenario-helpers.ts +5 -1
- package/src/harness/visual/screencast.ts +155 -0
- package/src/harness/visual/session.ts +72 -41
- package/src/harness/visual/types.ts +8 -5
- package/src/headless-room-protocol.ts +112 -0
- package/src/headless-room.ts +222 -0
- package/src/helper-channel.ts +229 -0
- package/src/helper-scripts.ts +25 -5
- package/src/installed-dependencies.ts +74 -0
- package/src/managed-process-scope.ts +481 -0
- package/src/node-process-environment.ts +10 -0
- package/src/platform-auth.ts +194 -55
- package/src/platform-game-analytics.ts +50 -0
- package/src/platform-game-app-id.ts +99 -0
- package/src/platform-games.ts +13 -111
- package/src/policy-artifact-contract.ts +52 -0
- package/src/policy-artifacts.ts +134 -0
- package/src/process-scope-admission.ts +50 -0
- package/src/process-scope-protocol.ts +66 -0
- package/src/process-scope.ts +327 -0
- package/src/release.ts +28 -42
- package/src/rooms-check-fleet.ts +194 -0
- package/src/rooms-check-link.ts +138 -0
- package/src/rooms-check-protocol.ts +244 -0
- package/src/rooms-check.ts +797 -0
- package/src/runtime-owner-protocol.ts +106 -18
- package/src/source-tree-contract.ts +70 -0
- package/src/source-tree.ts +84 -0
- package/src/test-profile-process.ts +153 -0
- package/src/test-profile-protocol.ts +116 -0
- package/src/testing.ts +430 -0
- package/src/tooling/declaration-hook.ts +52 -0
- package/src/tooling/describe-game.ts +49 -0
- package/src/tooling/game-session-broker.ts +7 -0
- package/src/tooling/hold-runtime-host.ts +255 -38
- package/src/tooling/list-visual-scenarios.ts +19 -62
- package/src/tooling/load-game.ts +79 -0
- package/src/tooling/process-scope-bootstrap.mjs +24 -0
- package/src/tooling/process-scope-guard.ts +213 -0
- package/src/tooling/process-scope-windows.ps1 +148 -0
- package/src/tooling/rooms-check-device.ts +493 -0
- package/src/tooling/run-headless-room.ts +326 -0
- package/src/tooling/run-test-profile.ts +190 -0
- package/src/tooling/run-visual-capture.ts +37 -23
- package/src/tooling/runtime-owner-actions.ts +23 -0
- package/src/tooling/seal-game-build.ts +118 -0
- package/src/tooling/visual-pack.ts +10 -45
- package/src/types.ts +54 -77
- package/src/visual.ts +14 -22
- package/src/windows-process-scopes.ts +189 -0
- package/dist/agent.d.ts +0 -15
- package/dist/agent.js +0 -19
- package/dist/chunk-5YAARMW5.js +0 -36
- package/dist/chunk-5YAARMW5.js.map +0 -1
- package/dist/chunk-6WQODT37.js +0 -1019
- package/dist/chunk-6WQODT37.js.map +0 -1
- package/dist/chunk-CKFT2ZJE.js.map +0 -1
- package/dist/chunk-ER3Q2C4G.js +0 -358
- package/dist/chunk-ER3Q2C4G.js.map +0 -1
- package/dist/chunk-GUEZP773.js.map +0 -1
- package/dist/chunk-HIDNI56E.js +0 -33
- package/dist/chunk-HIDNI56E.js.map +0 -1
- package/dist/chunk-QKXS5D35.js +0 -43
- package/dist/chunk-QKXS5D35.js.map +0 -1
- package/dist/chunk-S4LSCEVE.js +0 -261
- package/dist/chunk-S4LSCEVE.js.map +0 -1
- package/dist/chunk-VKMJO6VN.js.map +0 -1
- package/dist/chunk-WFUR3MXY.js +0 -67
- package/dist/chunk-WFUR3MXY.js.map +0 -1
- package/dist/chunk-XEMR7KUE.js +0 -361
- package/dist/chunk-XEMR7KUE.js.map +0 -1
- package/dist/chunk-ZLPD7FY5.js +0 -191
- package/dist/chunk-ZLPD7FY5.js.map +0 -1
- package/dist/tooling/agent-contract.js +0 -193
- package/dist/tooling/agent-contract.js.map +0 -1
- package/dist/tooling/inspect-airjam-agent.js +0 -37
- package/dist/tooling/inspect-airjam-agent.js.map +0 -1
- package/src/agent.ts +0 -380
- package/src/tooling/agent-contract.ts +0 -232
- package/src/tooling/agent-declaration.ts +0 -36
- package/src/tooling/airjam-agent-inspection.ts +0 -45
- package/src/tooling/airjam-agent.ts +0 -118
- package/src/tooling/inspect-airjam-agent.ts +0 -20
- /package/dist/{agent.js.map → dependencies.js.map} +0 -0
- /package/dist/tooling/{agent-contract.d.ts → describe-game.d.ts} +0 -0
- /package/dist/tooling/{inspect-airjam-agent.d.ts → game-session-broker.d.ts} +0 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
export const policyArtifactPathSchema = z
|
|
4
|
+
.string()
|
|
5
|
+
.min(1)
|
|
6
|
+
.max(240)
|
|
7
|
+
.refine(
|
|
8
|
+
(value) =>
|
|
9
|
+
!value.includes("\\") &&
|
|
10
|
+
!value.startsWith("/") &&
|
|
11
|
+
value
|
|
12
|
+
.split("/")
|
|
13
|
+
.every((part) => part !== "" && part !== "." && part !== ".."),
|
|
14
|
+
"Artifacts require canonical relative paths.",
|
|
15
|
+
);
|
|
16
|
+
export const policyArtifactManifestSchema = z
|
|
17
|
+
.object({
|
|
18
|
+
version: z.literal(1),
|
|
19
|
+
provenance: z.record(z.string().min(1).max(80), z.json()),
|
|
20
|
+
files: z
|
|
21
|
+
.array(
|
|
22
|
+
z
|
|
23
|
+
.object({
|
|
24
|
+
path: policyArtifactPathSchema,
|
|
25
|
+
bytes: z
|
|
26
|
+
.number()
|
|
27
|
+
.int()
|
|
28
|
+
.positive()
|
|
29
|
+
.max(4 * 1024 ** 3),
|
|
30
|
+
sha256: z.string().regex(/^[a-f0-9]{64}$/),
|
|
31
|
+
})
|
|
32
|
+
.strict(),
|
|
33
|
+
)
|
|
34
|
+
.min(1)
|
|
35
|
+
.max(32),
|
|
36
|
+
})
|
|
37
|
+
.strict()
|
|
38
|
+
.refine(
|
|
39
|
+
(value) =>
|
|
40
|
+
new Set(value.files.map((file) => file.path)).size ===
|
|
41
|
+
value.files.length &&
|
|
42
|
+
value.files.reduce((bytes, file) => bytes + file.bytes, 0) <=
|
|
43
|
+
4 * 1024 ** 3,
|
|
44
|
+
"Artifact files must be unique and fit the 4 GiB bundle allowance.",
|
|
45
|
+
);
|
|
46
|
+
export const policyArtifactReceiptSchema = z
|
|
47
|
+
.object({
|
|
48
|
+
manifestSha256: z.string().regex(/^[a-f0-9]{64}$/),
|
|
49
|
+
manifest: policyArtifactManifestSchema,
|
|
50
|
+
verifyMs: z.number().finite().nonnegative(),
|
|
51
|
+
})
|
|
52
|
+
.strict();
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { constants, createReadStream } from "node:fs";
|
|
3
|
+
import { access, lstat, readFile, readdir, realpath } from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import type { z } from "zod";
|
|
6
|
+
import {
|
|
7
|
+
policyArtifactManifestSchema,
|
|
8
|
+
policyArtifactReceiptSchema,
|
|
9
|
+
} from "./policy-artifact-contract.js";
|
|
10
|
+
|
|
11
|
+
async function assertProtectedEntry(target: string, directory: boolean) {
|
|
12
|
+
const information = await lstat(target);
|
|
13
|
+
if (directory ? !information.isDirectory() : !information.isFile())
|
|
14
|
+
throw new Error(
|
|
15
|
+
"policy_artifact_invalid: Expected a regular artifact entry, without symlinks.",
|
|
16
|
+
);
|
|
17
|
+
if (
|
|
18
|
+
!process.getuid ||
|
|
19
|
+
process.getuid() === 0 ||
|
|
20
|
+
information.uid === process.getuid()
|
|
21
|
+
)
|
|
22
|
+
throw new Error(
|
|
23
|
+
"policy_artifact_writable: Artifacts require an independent filesystem owner and a non-root policy.",
|
|
24
|
+
);
|
|
25
|
+
try {
|
|
26
|
+
await access(target, constants.W_OK);
|
|
27
|
+
} catch (error) {
|
|
28
|
+
if (error instanceof Error && "code" in error && error.code === "EACCES")
|
|
29
|
+
return;
|
|
30
|
+
throw error;
|
|
31
|
+
}
|
|
32
|
+
throw new Error(
|
|
33
|
+
"policy_artifact_writable: Policy artifacts must be read-only to the workload.",
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** This validates public, independently provisioned files; it does not download or run models. */
|
|
38
|
+
export async function verifyPolicyArtifacts(options: {
|
|
39
|
+
projectDir: string;
|
|
40
|
+
manifestPath: string;
|
|
41
|
+
artifactRootDir?: string;
|
|
42
|
+
signal?: AbortSignal;
|
|
43
|
+
}): Promise<{
|
|
44
|
+
directory: string;
|
|
45
|
+
receipt: z.infer<typeof policyArtifactReceiptSchema>;
|
|
46
|
+
}> {
|
|
47
|
+
const started = performance.now();
|
|
48
|
+
options.signal?.throwIfAborted();
|
|
49
|
+
if (!options.artifactRootDir || !path.isAbsolute(options.artifactRootDir))
|
|
50
|
+
throw new Error(
|
|
51
|
+
"policy_artifacts_missing: Configure an absolute AIRJAM_TEST_ARTIFACTS_DIR for this policy.",
|
|
52
|
+
);
|
|
53
|
+
const project = await realpath(options.projectDir);
|
|
54
|
+
const manifestPath = await realpath(options.manifestPath);
|
|
55
|
+
const relative = path.relative(project, manifestPath);
|
|
56
|
+
if (
|
|
57
|
+
!relative ||
|
|
58
|
+
relative === ".." ||
|
|
59
|
+
relative.startsWith(`..${path.sep}`) ||
|
|
60
|
+
path.isAbsolute(relative)
|
|
61
|
+
)
|
|
62
|
+
throw new Error(
|
|
63
|
+
"policy_artifact_invalid: The manifest must be pinned inside the project source.",
|
|
64
|
+
);
|
|
65
|
+
if ((await lstat(manifestPath)).size > 64 * 1024)
|
|
66
|
+
throw new Error("policy_artifact_invalid: Manifest exceeds 64 KiB.");
|
|
67
|
+
const content = await readFile(manifestPath, { signal: options.signal });
|
|
68
|
+
const manifest = policyArtifactManifestSchema.parse(
|
|
69
|
+
JSON.parse(content.toString("utf8")),
|
|
70
|
+
);
|
|
71
|
+
const manifestSha256 = createHash("sha256").update(content).digest("hex");
|
|
72
|
+
const directory = path.join(options.artifactRootDir, manifestSha256);
|
|
73
|
+
const directories = new Set<string>();
|
|
74
|
+
let ancestor = directory;
|
|
75
|
+
while (true) {
|
|
76
|
+
directories.add(ancestor);
|
|
77
|
+
const parent = path.dirname(ancestor);
|
|
78
|
+
if (parent === ancestor) break;
|
|
79
|
+
ancestor = parent;
|
|
80
|
+
}
|
|
81
|
+
for (const file of manifest.files) {
|
|
82
|
+
let parent = path.dirname(path.join(directory, file.path));
|
|
83
|
+
while (parent !== directory) {
|
|
84
|
+
directories.add(parent);
|
|
85
|
+
parent = path.dirname(parent);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
for (const parent of directories) await assertProtectedEntry(parent, true);
|
|
89
|
+
const expected = new Set(manifest.files.map((file) => file.path));
|
|
90
|
+
const pending = [""];
|
|
91
|
+
for (const relative of pending) {
|
|
92
|
+
options.signal?.throwIfAborted();
|
|
93
|
+
for (const entry of await readdir(path.join(directory, relative), {
|
|
94
|
+
withFileTypes: true,
|
|
95
|
+
})) {
|
|
96
|
+
const name = relative ? `${relative}/${entry.name}` : entry.name;
|
|
97
|
+
if (
|
|
98
|
+
entry.isDirectory() &&
|
|
99
|
+
[...expected].some((file) => file.startsWith(name + "/"))
|
|
100
|
+
)
|
|
101
|
+
pending.push(name);
|
|
102
|
+
else if (!entry.isFile() || !expected.has(name))
|
|
103
|
+
throw new Error(
|
|
104
|
+
`policy_artifact_invalid: Unlisted artifact entry: ${name}.`,
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
for (const file of manifest.files) {
|
|
109
|
+
options.signal?.throwIfAborted();
|
|
110
|
+
const target = path.join(directory, file.path);
|
|
111
|
+
await assertProtectedEntry(target, false);
|
|
112
|
+
if ((await lstat(target)).size !== file.bytes)
|
|
113
|
+
throw new Error(
|
|
114
|
+
`policy_artifact_invalid: Size differs for ${file.path}.`,
|
|
115
|
+
);
|
|
116
|
+
const hash = createHash("sha256");
|
|
117
|
+
for await (const chunk of createReadStream(target, {
|
|
118
|
+
signal: options.signal,
|
|
119
|
+
}))
|
|
120
|
+
hash.update(chunk);
|
|
121
|
+
if (hash.digest("hex") !== file.sha256)
|
|
122
|
+
throw new Error(
|
|
123
|
+
`policy_artifact_invalid: Digest differs for ${file.path}.`,
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
return {
|
|
127
|
+
directory,
|
|
128
|
+
receipt: policyArtifactReceiptSchema.parse({
|
|
129
|
+
manifestSha256,
|
|
130
|
+
manifest,
|
|
131
|
+
verifyMs: performance.now() - started,
|
|
132
|
+
}),
|
|
133
|
+
};
|
|
134
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { processScopeAdmittedSchema } from "./process-scope-protocol.js";
|
|
3
|
+
|
|
4
|
+
export function processScopeToken() {
|
|
5
|
+
const token = process.argv
|
|
6
|
+
.find((value) => value.startsWith("--airjam-process-scope="))
|
|
7
|
+
?.split("=")[1];
|
|
8
|
+
if (!process.connected || !token)
|
|
9
|
+
throw new Error(
|
|
10
|
+
"process_scope_unavailable: A scoped helper requires its owner's admission channel.",
|
|
11
|
+
);
|
|
12
|
+
return token;
|
|
13
|
+
}
|
|
14
|
+
export async function admitProcessScopeMember(processId: number) {
|
|
15
|
+
const token = processScopeToken();
|
|
16
|
+
const id = randomUUID();
|
|
17
|
+
await new Promise<void>((resolve, reject) => {
|
|
18
|
+
const finish = (error?: Error) => {
|
|
19
|
+
clearTimeout(timer);
|
|
20
|
+
process.off("message", receive);
|
|
21
|
+
process.off("disconnect", disconnected);
|
|
22
|
+
if (error) reject(error);
|
|
23
|
+
else resolve();
|
|
24
|
+
};
|
|
25
|
+
const receive = (value: unknown) => {
|
|
26
|
+
const result = processScopeAdmittedSchema.safeParse(value);
|
|
27
|
+
if (result.success && result.data.id === id)
|
|
28
|
+
finish(result.data.error ? new Error(result.data.error) : undefined);
|
|
29
|
+
};
|
|
30
|
+
const disconnected = () =>
|
|
31
|
+
finish(
|
|
32
|
+
new Error("process_scope_unavailable: Admission owner disconnected."),
|
|
33
|
+
);
|
|
34
|
+
const timer = setTimeout(
|
|
35
|
+
() =>
|
|
36
|
+
finish(new Error("process_scope_unavailable: Admission timed out.")),
|
|
37
|
+
10000,
|
|
38
|
+
);
|
|
39
|
+
process.on("message", receive);
|
|
40
|
+
process.once("disconnect", disconnected);
|
|
41
|
+
process.send?.(
|
|
42
|
+
{ type: "airjam-process-scope/admit", id, token, processId },
|
|
43
|
+
undefined,
|
|
44
|
+
undefined,
|
|
45
|
+
(error: Error | null) => {
|
|
46
|
+
if (error) finish(error);
|
|
47
|
+
},
|
|
48
|
+
);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { processScopeReceiptSchema } from "./process-scope.js";
|
|
3
|
+
|
|
4
|
+
// Windows includes first-use PowerShell compilation, before admitting authored code.
|
|
5
|
+
export const processGuardStartupTimeoutMs =
|
|
6
|
+
process.platform === "win32" ? 30000 : 10000;
|
|
7
|
+
export const processGuardStartupDeadlineSchema = z.coerce.bigint().positive();
|
|
8
|
+
|
|
9
|
+
export const processGuardRequestSchema = z.discriminatedUnion("kind", [
|
|
10
|
+
z
|
|
11
|
+
.object({
|
|
12
|
+
kind: z.literal("open"),
|
|
13
|
+
id: z.uuid(),
|
|
14
|
+
token: z.uuid(),
|
|
15
|
+
rootDir: z.string().min(1),
|
|
16
|
+
})
|
|
17
|
+
.strict(),
|
|
18
|
+
z
|
|
19
|
+
.object({
|
|
20
|
+
kind: z.literal("register"),
|
|
21
|
+
id: z.uuid(),
|
|
22
|
+
token: z.uuid(),
|
|
23
|
+
processId: z.number().int().positive(),
|
|
24
|
+
root: z.boolean(),
|
|
25
|
+
})
|
|
26
|
+
.strict(),
|
|
27
|
+
z
|
|
28
|
+
.object({ kind: z.literal("close"), id: z.uuid(), token: z.uuid() })
|
|
29
|
+
.strict(),
|
|
30
|
+
]);
|
|
31
|
+
export type ProcessGuardRequest = z.infer<typeof processGuardRequestSchema>;
|
|
32
|
+
export const processGuardResponseSchema = z.discriminatedUnion("kind", [
|
|
33
|
+
z.object({ kind: z.literal("ready") }).strict(),
|
|
34
|
+
z.object({ kind: z.literal("startup-error"), message: z.string() }).strict(),
|
|
35
|
+
z
|
|
36
|
+
.object({
|
|
37
|
+
kind: z.literal("result"),
|
|
38
|
+
id: z.uuid(),
|
|
39
|
+
receipt: processScopeReceiptSchema.optional(),
|
|
40
|
+
})
|
|
41
|
+
.strict(),
|
|
42
|
+
z
|
|
43
|
+
.object({
|
|
44
|
+
kind: z.literal("error"),
|
|
45
|
+
id: z.uuid(),
|
|
46
|
+
message: z.string(),
|
|
47
|
+
scopeUnconfirmed: z.boolean(),
|
|
48
|
+
})
|
|
49
|
+
.strict(),
|
|
50
|
+
]);
|
|
51
|
+
export type ProcessGuardResponse = z.infer<typeof processGuardResponseSchema>;
|
|
52
|
+
export const processScopeAdmissionSchema = z
|
|
53
|
+
.object({
|
|
54
|
+
type: z.literal("airjam-process-scope/admit"),
|
|
55
|
+
id: z.uuid(),
|
|
56
|
+
token: z.uuid(),
|
|
57
|
+
processId: z.number().int().positive(),
|
|
58
|
+
})
|
|
59
|
+
.strict();
|
|
60
|
+
export const processScopeAdmittedSchema = z
|
|
61
|
+
.object({
|
|
62
|
+
type: z.literal("airjam-process-scope/admitted"),
|
|
63
|
+
id: z.uuid(),
|
|
64
|
+
error: z.string().optional(),
|
|
65
|
+
})
|
|
66
|
+
.strict();
|
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { readFileSync, readlinkSync } from "node:fs";
|
|
4
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
|
|
7
|
+
const processIdentitySchema = z
|
|
8
|
+
.object({
|
|
9
|
+
processId: z.number().int().positive(),
|
|
10
|
+
started: z.string().min(1),
|
|
11
|
+
})
|
|
12
|
+
.strict();
|
|
13
|
+
export const processScopeSchema = z
|
|
14
|
+
.object({
|
|
15
|
+
version: z.literal(1),
|
|
16
|
+
token: z.uuid(),
|
|
17
|
+
namespace: z.string().min(1),
|
|
18
|
+
groups: z
|
|
19
|
+
.array(
|
|
20
|
+
z
|
|
21
|
+
.object({
|
|
22
|
+
groupId: z.number().int().positive(),
|
|
23
|
+
anchor: processIdentitySchema,
|
|
24
|
+
members: z.array(processIdentitySchema).max(1024),
|
|
25
|
+
})
|
|
26
|
+
.strict(),
|
|
27
|
+
)
|
|
28
|
+
.max(64),
|
|
29
|
+
})
|
|
30
|
+
.strict();
|
|
31
|
+
export type ProcessScope = z.infer<typeof processScopeSchema>;
|
|
32
|
+
export const processScopeJournalSchema = z
|
|
33
|
+
.object({
|
|
34
|
+
guardian: processIdentitySchema,
|
|
35
|
+
scope: processScopeSchema,
|
|
36
|
+
})
|
|
37
|
+
.strict();
|
|
38
|
+
export const processScopeReceiptSchema = z
|
|
39
|
+
.object({
|
|
40
|
+
status: z.enum(["tree_clean", "tree_unconfirmed"]),
|
|
41
|
+
token: z.uuid(),
|
|
42
|
+
survivors: z.array(
|
|
43
|
+
z
|
|
44
|
+
.object({
|
|
45
|
+
processId: z.number().int().positive(),
|
|
46
|
+
groupId: z.number().int().positive(),
|
|
47
|
+
})
|
|
48
|
+
.strict(),
|
|
49
|
+
),
|
|
50
|
+
errors: z.array(z.string()),
|
|
51
|
+
})
|
|
52
|
+
.strict();
|
|
53
|
+
export type ProcessScopeReceipt = z.infer<typeof processScopeReceiptSchema>;
|
|
54
|
+
const processRowSchema = processIdentitySchema.extend({
|
|
55
|
+
parentProcessId: z.number().int().nonnegative(),
|
|
56
|
+
groupId: z.number().int().positive(),
|
|
57
|
+
state: z.string(),
|
|
58
|
+
command: z.string(),
|
|
59
|
+
});
|
|
60
|
+
type ProcessRow = z.infer<typeof processRowSchema>;
|
|
61
|
+
export const processScopeMarker = (token: string) =>
|
|
62
|
+
`--airjam-process-scope=${z.uuid().parse(token)}`;
|
|
63
|
+
export const createProcessScope = (): ProcessScope => ({
|
|
64
|
+
version: 1,
|
|
65
|
+
token: randomUUID(),
|
|
66
|
+
namespace: processScopeNamespace(),
|
|
67
|
+
groups: [],
|
|
68
|
+
});
|
|
69
|
+
let namespace: string | undefined;
|
|
70
|
+
export function processScopeNamespace() {
|
|
71
|
+
return (namespace ??=
|
|
72
|
+
process.platform === "linux"
|
|
73
|
+
? `linux:${readFileSync("/proc/sys/kernel/random/boot_id", "utf8").trim()}:${readlinkSync("/proc/self/ns/pid")}`
|
|
74
|
+
: `darwin:${execFileSync("sysctl", ["-n", "kern.boottime"], { encoding: "utf8", env: { ...process.env, LC_ALL: "C" } }).trim()}`);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function processTable(): ProcessRow[] {
|
|
78
|
+
if (process.platform === "win32")
|
|
79
|
+
throw new Error(
|
|
80
|
+
"Process scope verification requires a POSIX process table.",
|
|
81
|
+
);
|
|
82
|
+
const source = execFileSync(
|
|
83
|
+
"ps",
|
|
84
|
+
["-A", "-ww", "-o", "pid=,ppid=,pgid=,lstart=,stat=,command="],
|
|
85
|
+
{
|
|
86
|
+
encoding: "utf8",
|
|
87
|
+
maxBuffer: 16 * 1024 * 1024,
|
|
88
|
+
env: { ...process.env, LC_ALL: "C" },
|
|
89
|
+
},
|
|
90
|
+
);
|
|
91
|
+
return source.split("\n").flatMap((line) => {
|
|
92
|
+
const match =
|
|
93
|
+
/^\s*(\d+)\s+(\d+)\s+(\d+)\s+(\S+\s+\S+\s+\d+\s+\d+:\d+:\d+\s+\d+)\s+(\S+)\s+(.*)$/.exec(
|
|
94
|
+
line,
|
|
95
|
+
);
|
|
96
|
+
if (!match) {
|
|
97
|
+
if (line.trim())
|
|
98
|
+
throw new Error("Process scope received an invalid process table row.");
|
|
99
|
+
return [];
|
|
100
|
+
}
|
|
101
|
+
if (Number(match[3]) === 0) return [];
|
|
102
|
+
const processId = Number(match[1]);
|
|
103
|
+
let started = match[4];
|
|
104
|
+
if (process.platform === "linux") {
|
|
105
|
+
try {
|
|
106
|
+
const stat = readFileSync(`/proc/${processId}/stat`, "utf8");
|
|
107
|
+
started = `linux:${stat.slice(stat.lastIndexOf(")") + 2).split(" ")[19]}`;
|
|
108
|
+
} catch (error) {
|
|
109
|
+
if (
|
|
110
|
+
(error as NodeJS.ErrnoException).code === "ENOENT" ||
|
|
111
|
+
(error as NodeJS.ErrnoException).code === "ESRCH"
|
|
112
|
+
)
|
|
113
|
+
return [];
|
|
114
|
+
throw error;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return [
|
|
118
|
+
processRowSchema.parse({
|
|
119
|
+
processId,
|
|
120
|
+
parentProcessId: Number(match[2]),
|
|
121
|
+
groupId: Number(match[3]),
|
|
122
|
+
started,
|
|
123
|
+
state: match[5],
|
|
124
|
+
command: match[6],
|
|
125
|
+
}),
|
|
126
|
+
];
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
export function processIdentity(processId: number) {
|
|
130
|
+
const row = processTable().find((row) => row.processId === processId);
|
|
131
|
+
return row ? { processId: row.processId, started: row.started } : null;
|
|
132
|
+
}
|
|
133
|
+
function carriesToken(row: ProcessRow, token: string) {
|
|
134
|
+
return row.command.split(/\s+/).includes(processScopeMarker(token));
|
|
135
|
+
}
|
|
136
|
+
function inheritedScope(row: ProcessRow, token: string) {
|
|
137
|
+
try {
|
|
138
|
+
const marker = `AIRJAM_PROCESS_SCOPE=${token}`;
|
|
139
|
+
if (process.platform === "linux")
|
|
140
|
+
return readFileSync(`/proc/${row.processId}/environ`, "utf8")
|
|
141
|
+
.split("\0")
|
|
142
|
+
.includes(marker);
|
|
143
|
+
const command = execFileSync(
|
|
144
|
+
"ps",
|
|
145
|
+
["eww", "-p", String(row.processId), "-o", "command="],
|
|
146
|
+
{
|
|
147
|
+
encoding: "utf8",
|
|
148
|
+
maxBuffer: 1024 * 1024,
|
|
149
|
+
env: { ...process.env, LC_ALL: "C" },
|
|
150
|
+
},
|
|
151
|
+
);
|
|
152
|
+
return command.split(/\s+/).includes(marker);
|
|
153
|
+
} catch (error) {
|
|
154
|
+
const result = error as { status?: number; stdout?: string };
|
|
155
|
+
if (result.status === 1 && !result.stdout?.trim()) return false;
|
|
156
|
+
if (
|
|
157
|
+
["ENOENT", "ESRCH", "EACCES", "EPERM"].includes(
|
|
158
|
+
(error as NodeJS.ErrnoException).code ?? "",
|
|
159
|
+
)
|
|
160
|
+
)
|
|
161
|
+
return false;
|
|
162
|
+
throw error;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
function sameProcess(
|
|
166
|
+
left: z.infer<typeof processIdentitySchema>,
|
|
167
|
+
right: z.infer<typeof processIdentitySchema>,
|
|
168
|
+
) {
|
|
169
|
+
return left.processId === right.processId && left.started === right.started;
|
|
170
|
+
}
|
|
171
|
+
function rememberGroup(
|
|
172
|
+
scope: ProcessScope,
|
|
173
|
+
row: ProcessRow,
|
|
174
|
+
table: ProcessRow[],
|
|
175
|
+
) {
|
|
176
|
+
let group = scope.groups.find((group) => group.groupId === row.groupId);
|
|
177
|
+
if (!group) {
|
|
178
|
+
if (scope.groups.length >= 64)
|
|
179
|
+
throw new Error("A process scope exceeded its process-group allowance.");
|
|
180
|
+
group = {
|
|
181
|
+
groupId: row.groupId,
|
|
182
|
+
anchor: { processId: row.processId, started: row.started },
|
|
183
|
+
members: [],
|
|
184
|
+
};
|
|
185
|
+
scope.groups.push(group);
|
|
186
|
+
} else if (row.processId === row.groupId && !sameProcess(row, group.anchor)) {
|
|
187
|
+
group.anchor = { processId: row.processId, started: row.started };
|
|
188
|
+
group.members = [];
|
|
189
|
+
}
|
|
190
|
+
group.members = group.members.filter((previous) =>
|
|
191
|
+
table.some(
|
|
192
|
+
(member) =>
|
|
193
|
+
member.groupId === group.groupId && sameProcess(previous, member),
|
|
194
|
+
),
|
|
195
|
+
);
|
|
196
|
+
for (const member of table.filter(
|
|
197
|
+
(member) => member.groupId === group.groupId,
|
|
198
|
+
))
|
|
199
|
+
if (!group.members.some((previous) => sameProcess(previous, member))) {
|
|
200
|
+
if (group.members.length >= 1024)
|
|
201
|
+
throw new Error(
|
|
202
|
+
"A process scope exceeded its group membership allowance.",
|
|
203
|
+
);
|
|
204
|
+
group.members.push({
|
|
205
|
+
processId: member.processId,
|
|
206
|
+
started: member.started,
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
export function registerProcessScope(scope: ProcessScope, processId: number) {
|
|
211
|
+
processScopeSchema.parse(scope);
|
|
212
|
+
if (scope.namespace !== processScopeNamespace())
|
|
213
|
+
throw new Error(
|
|
214
|
+
"Process scope belongs to a different boot or PID namespace.",
|
|
215
|
+
);
|
|
216
|
+
const table = processTable();
|
|
217
|
+
const row = table.find((row) => row.processId === processId);
|
|
218
|
+
if (!row || !carriesToken(row, scope.token))
|
|
219
|
+
throw new Error(
|
|
220
|
+
"Process scope registration requires the live, marked process.",
|
|
221
|
+
);
|
|
222
|
+
if (row.groupId !== processId)
|
|
223
|
+
throw new Error("A registered process must lead its own process group.");
|
|
224
|
+
rememberGroup(scope, row, table);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** Verifies registered process generations and token-marked groups; never broad process-name killing. */
|
|
228
|
+
export async function closeProcessScope(
|
|
229
|
+
input: ProcessScope,
|
|
230
|
+
): Promise<ProcessScopeReceipt> {
|
|
231
|
+
const scope = processScopeSchema.parse(input);
|
|
232
|
+
if (scope.namespace !== processScopeNamespace())
|
|
233
|
+
return {
|
|
234
|
+
status: "tree_clean",
|
|
235
|
+
token: scope.token,
|
|
236
|
+
survivors: [],
|
|
237
|
+
errors: [],
|
|
238
|
+
};
|
|
239
|
+
const errors: string[] = [];
|
|
240
|
+
const retired = new Set<number>();
|
|
241
|
+
const signaled = new Map<number, NodeJS.Signals>();
|
|
242
|
+
const started = performance.now();
|
|
243
|
+
const deadline = started + 3000;
|
|
244
|
+
let survivors: ProcessRow[] = [];
|
|
245
|
+
const signal = (groupId: number, kind: NodeJS.Signals) => {
|
|
246
|
+
if (retired.has(groupId) || signaled.get(groupId) === kind) return;
|
|
247
|
+
try {
|
|
248
|
+
process.kill(-groupId, kind);
|
|
249
|
+
signaled.set(groupId, kind);
|
|
250
|
+
} catch (error) {
|
|
251
|
+
if ((error as NodeJS.ErrnoException).code === "ESRCH")
|
|
252
|
+
retired.add(groupId);
|
|
253
|
+
else errors.push(`Process group ${groupId}: ${String(error)}`);
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
try {
|
|
257
|
+
let empty = false;
|
|
258
|
+
while (true) {
|
|
259
|
+
const table = processTable();
|
|
260
|
+
for (const row of table.filter(
|
|
261
|
+
(row) =>
|
|
262
|
+
carriesToken(row, scope.token) ||
|
|
263
|
+
(process.platform === "linux" && inheritedScope(row, scope.token)),
|
|
264
|
+
)) {
|
|
265
|
+
if (retired.has(row.groupId)) {
|
|
266
|
+
errors.push(`Scope group ${row.groupId} reappeared after exit.`);
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
rememberGroup(scope, row, table);
|
|
270
|
+
}
|
|
271
|
+
survivors = [];
|
|
272
|
+
for (const group of scope.groups) {
|
|
273
|
+
if (retired.has(group.groupId)) continue;
|
|
274
|
+
const leader = table.find(
|
|
275
|
+
(row) =>
|
|
276
|
+
row.processId === group.groupId && row.groupId === group.groupId,
|
|
277
|
+
);
|
|
278
|
+
if (
|
|
279
|
+
group.anchor.processId === group.groupId &&
|
|
280
|
+
leader &&
|
|
281
|
+
!sameProcess(leader, group.anchor)
|
|
282
|
+
) {
|
|
283
|
+
retired.add(group.groupId);
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
const members = table.filter(
|
|
287
|
+
(row) => row.groupId === group.groupId && !row.state.startsWith("Z"),
|
|
288
|
+
);
|
|
289
|
+
if (!members.length) {
|
|
290
|
+
retired.add(group.groupId);
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
survivors.push(...members);
|
|
294
|
+
const verified = members.some(
|
|
295
|
+
(member) =>
|
|
296
|
+
carriesToken(member, scope.token) ||
|
|
297
|
+
group.members.some((previous) => sameProcess(previous, member)) ||
|
|
298
|
+
inheritedScope(member, scope.token),
|
|
299
|
+
);
|
|
300
|
+
if (!verified) continue;
|
|
301
|
+
rememberGroup(scope, members[0], table);
|
|
302
|
+
signal(
|
|
303
|
+
group.groupId,
|
|
304
|
+
performance.now() - started < 1000 ? "SIGTERM" : "SIGKILL",
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
if (!survivors.length && empty) break;
|
|
308
|
+
empty = !survivors.length;
|
|
309
|
+
if (performance.now() >= deadline) break;
|
|
310
|
+
await delay(25);
|
|
311
|
+
}
|
|
312
|
+
} catch (error) {
|
|
313
|
+
errors.push(String(error));
|
|
314
|
+
}
|
|
315
|
+
// Recovery must retain groups first discovered during this cleanup attempt.
|
|
316
|
+
input.groups = scope.groups;
|
|
317
|
+
return {
|
|
318
|
+
status:
|
|
319
|
+
!survivors.length && !errors.length ? "tree_clean" : "tree_unconfirmed",
|
|
320
|
+
token: scope.token,
|
|
321
|
+
survivors: survivors.map(({ processId, groupId }) => ({
|
|
322
|
+
processId,
|
|
323
|
+
groupId,
|
|
324
|
+
})),
|
|
325
|
+
errors,
|
|
326
|
+
};
|
|
327
|
+
}
|