@gate-forge/cli 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +511 -51
- package/dist/adapter-audit.d.ts +154 -0
- package/dist/adapter-audit.d.ts.map +1 -0
- package/dist/adapter-audit.js +531 -0
- package/dist/adapter-audit.js.map +1 -0
- package/dist/adapter-scaffold.d.ts +66 -0
- package/dist/adapter-scaffold.d.ts.map +1 -0
- package/dist/adapter-scaffold.js +353 -0
- package/dist/adapter-scaffold.js.map +1 -0
- package/dist/adopted-baseline.d.ts +10 -0
- package/dist/adopted-baseline.d.ts.map +1 -1
- package/dist/adopted-baseline.js +18 -1
- package/dist/adopted-baseline.js.map +1 -1
- package/dist/args.d.ts +15 -0
- package/dist/args.d.ts.map +1 -1
- package/dist/args.js +59 -2
- package/dist/args.js.map +1 -1
- package/dist/behavior-setup.d.ts +239 -0
- package/dist/behavior-setup.d.ts.map +1 -0
- package/dist/behavior-setup.js +738 -0
- package/dist/behavior-setup.js.map +1 -0
- package/dist/broker.d.ts.map +1 -1
- package/dist/broker.js +28 -2
- package/dist/broker.js.map +1 -1
- package/dist/cache-exclusions.d.ts +45 -0
- package/dist/cache-exclusions.d.ts.map +1 -0
- package/dist/cache-exclusions.js +205 -0
- package/dist/cache-exclusions.js.map +1 -0
- package/dist/candidate-tree.d.ts +86 -14
- package/dist/candidate-tree.d.ts.map +1 -1
- package/dist/candidate-tree.js +236 -62
- package/dist/candidate-tree.js.map +1 -1
- package/dist/ci-witnessed-template.d.ts +43 -0
- package/dist/ci-witnessed-template.d.ts.map +1 -0
- package/dist/ci-witnessed-template.js +283 -0
- package/dist/ci-witnessed-template.js.map +1 -0
- package/dist/cli.d.ts +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +31 -4
- package/dist/cli.js.map +1 -1
- package/dist/commands/adapters.d.ts +15 -0
- package/dist/commands/adapters.d.ts.map +1 -0
- package/dist/commands/adapters.js +284 -0
- package/dist/commands/adapters.js.map +1 -0
- package/dist/commands/adopt.d.ts.map +1 -1
- package/dist/commands/adopt.js +9 -1
- package/dist/commands/adopt.js.map +1 -1
- package/dist/commands/baseline.d.ts.map +1 -1
- package/dist/commands/baseline.js +42 -1
- package/dist/commands/baseline.js.map +1 -1
- package/dist/commands/blocking.d.ts +81 -2
- package/dist/commands/blocking.d.ts.map +1 -1
- package/dist/commands/blocking.js +336 -53
- package/dist/commands/blocking.js.map +1 -1
- package/dist/commands/check.d.ts +36 -0
- package/dist/commands/check.d.ts.map +1 -1
- package/dist/commands/check.js +1008 -62
- package/dist/commands/check.js.map +1 -1
- package/dist/commands/classify.d.ts +2 -1
- package/dist/commands/classify.d.ts.map +1 -1
- package/dist/commands/classify.js +143 -5
- package/dist/commands/classify.js.map +1 -1
- package/dist/commands/enforce.d.ts +4 -4
- package/dist/commands/enforce.d.ts.map +1 -1
- package/dist/commands/enforce.js +41 -9
- package/dist/commands/enforce.js.map +1 -1
- package/dist/commands/enforcement.d.ts +13 -1
- package/dist/commands/enforcement.d.ts.map +1 -1
- package/dist/commands/enforcement.js +800 -30
- package/dist/commands/enforcement.js.map +1 -1
- package/dist/commands/history.d.ts +12 -0
- package/dist/commands/history.d.ts.map +1 -0
- package/dist/commands/history.js +45 -0
- package/dist/commands/history.js.map +1 -0
- package/dist/commands/init-presets.d.ts +148 -0
- package/dist/commands/init-presets.d.ts.map +1 -0
- package/dist/commands/init-presets.js +194 -0
- package/dist/commands/init-presets.js.map +1 -0
- package/dist/commands/init.d.ts +7 -12
- package/dist/commands/init.d.ts.map +1 -1
- package/dist/commands/init.js +610 -69
- package/dist/commands/init.js.map +1 -1
- package/dist/commands/keys.d.ts.map +1 -1
- package/dist/commands/keys.js +29 -13
- package/dist/commands/keys.js.map +1 -1
- package/dist/commands/next.d.ts.map +1 -1
- package/dist/commands/next.js +744 -15
- package/dist/commands/next.js.map +1 -1
- package/dist/commands/pre-commit.d.ts +25 -0
- package/dist/commands/pre-commit.d.ts.map +1 -1
- package/dist/commands/pre-commit.js +91 -6
- package/dist/commands/pre-commit.js.map +1 -1
- package/dist/commands/quarantine.d.ts +16 -0
- package/dist/commands/quarantine.d.ts.map +1 -0
- package/dist/commands/quarantine.js +213 -0
- package/dist/commands/quarantine.js.map +1 -0
- package/dist/commands/run.d.ts +39 -0
- package/dist/commands/run.d.ts.map +1 -0
- package/dist/commands/run.js +196 -0
- package/dist/commands/run.js.map +1 -0
- package/dist/commands/test-gates.d.ts +284 -2
- package/dist/commands/test-gates.d.ts.map +1 -1
- package/dist/commands/test-gates.js +3619 -186
- package/dist/commands/test-gates.js.map +1 -1
- package/dist/commands/tests.d.ts +1 -1
- package/dist/commands/tests.d.ts.map +1 -1
- package/dist/commands/tests.js +225 -22
- package/dist/commands/tests.js.map +1 -1
- package/dist/endpoint-compiler.d.ts.map +1 -1
- package/dist/endpoint-compiler.js +29 -8
- package/dist/endpoint-compiler.js.map +1 -1
- package/dist/engine-browser.d.ts +183 -0
- package/dist/engine-browser.d.ts.map +1 -0
- package/dist/engine-browser.js +381 -0
- package/dist/engine-browser.js.map +1 -0
- package/dist/engine-identity.d.ts +100 -0
- package/dist/engine-identity.d.ts.map +1 -0
- package/dist/engine-identity.js +182 -0
- package/dist/engine-identity.js.map +1 -0
- package/dist/evaluate.d.ts +89 -0
- package/dist/evaluate.d.ts.map +1 -1
- package/dist/evaluate.js +137 -16
- package/dist/evaluate.js.map +1 -1
- package/dist/execution.d.ts +253 -9
- package/dist/execution.d.ts.map +1 -1
- package/dist/execution.js +309 -12
- package/dist/execution.js.map +1 -1
- package/dist/git-hooks.d.ts +17 -0
- package/dist/git-hooks.d.ts.map +1 -1
- package/dist/git-hooks.js +119 -0
- package/dist/git-hooks.js.map +1 -1
- package/dist/git-objects.d.ts +60 -0
- package/dist/git-objects.d.ts.map +1 -0
- package/dist/git-objects.js +152 -0
- package/dist/git-objects.js.map +1 -0
- package/dist/history.d.ts +52 -0
- package/dist/history.d.ts.map +1 -0
- package/dist/history.js +116 -0
- package/dist/history.js.map +1 -0
- package/dist/host-load.d.ts +49 -0
- package/dist/host-load.d.ts.map +1 -0
- package/dist/host-load.js +78 -0
- package/dist/host-load.js.map +1 -0
- package/dist/input-snapshot.d.ts +63 -9
- package/dist/input-snapshot.d.ts.map +1 -1
- package/dist/input-snapshot.js +195 -17
- package/dist/input-snapshot.js.map +1 -1
- package/dist/io.d.ts +36 -0
- package/dist/io.d.ts.map +1 -1
- package/dist/io.js +21 -0
- package/dist/io.js.map +1 -1
- package/dist/mapping.d.ts +84 -1
- package/dist/mapping.d.ts.map +1 -1
- package/dist/mapping.js +276 -5
- package/dist/mapping.js.map +1 -1
- package/dist/native-freeze.d.ts +217 -0
- package/dist/native-freeze.d.ts.map +1 -0
- package/dist/native-freeze.js +332 -0
- package/dist/native-freeze.js.map +1 -0
- package/dist/pipeline.d.ts +52 -1
- package/dist/pipeline.d.ts.map +1 -1
- package/dist/pipeline.js +141 -3
- package/dist/pipeline.js.map +1 -1
- package/dist/playwright-browsers.d.ts +210 -0
- package/dist/playwright-browsers.d.ts.map +1 -0
- package/dist/playwright-browsers.js +364 -0
- package/dist/playwright-browsers.js.map +1 -0
- package/dist/plugins.d.ts +15 -5
- package/dist/plugins.d.ts.map +1 -1
- package/dist/plugins.js +59 -9
- package/dist/plugins.js.map +1 -1
- package/dist/progress.d.ts +148 -0
- package/dist/progress.d.ts.map +1 -0
- package/dist/progress.js +263 -0
- package/dist/progress.js.map +1 -0
- package/dist/providers.d.ts +26 -1
- package/dist/providers.d.ts.map +1 -1
- package/dist/providers.js +39 -0
- package/dist/providers.js.map +1 -1
- package/dist/receipts.d.ts +1 -0
- package/dist/receipts.d.ts.map +1 -1
- package/dist/receipts.js +35 -7
- package/dist/receipts.js.map +1 -1
- package/dist/repo-scan.d.ts +8 -1
- package/dist/repo-scan.d.ts.map +1 -1
- package/dist/repo-scan.js +221 -27
- package/dist/repo-scan.js.map +1 -1
- package/dist/reseal-chain.d.ts +184 -0
- package/dist/reseal-chain.d.ts.map +1 -0
- package/dist/reseal-chain.js +735 -0
- package/dist/reseal-chain.js.map +1 -0
- package/dist/reseal-evidence.d.ts +241 -0
- package/dist/reseal-evidence.d.ts.map +1 -0
- package/dist/reseal-evidence.js +319 -0
- package/dist/reseal-evidence.js.map +1 -0
- package/dist/reseal.d.ts +181 -0
- package/dist/reseal.d.ts.map +1 -0
- package/dist/reseal.js +1620 -0
- package/dist/reseal.js.map +1 -0
- package/dist/response-field-guidance.d.ts +82 -0
- package/dist/response-field-guidance.d.ts.map +1 -0
- package/dist/response-field-guidance.js +184 -0
- package/dist/response-field-guidance.js.map +1 -0
- package/dist/run-cache.d.ts +260 -0
- package/dist/run-cache.d.ts.map +1 -0
- package/dist/run-cache.js +771 -0
- package/dist/run-cache.js.map +1 -0
- package/dist/run-preflight.d.ts +77 -0
- package/dist/run-preflight.d.ts.map +1 -0
- package/dist/run-preflight.js +774 -0
- package/dist/run-preflight.js.map +1 -0
- package/dist/run-recipe.d.ts +107 -0
- package/dist/run-recipe.d.ts.map +1 -0
- package/dist/run-recipe.js +295 -0
- package/dist/run-recipe.js.map +1 -0
- package/dist/run-reliability.d.ts +126 -0
- package/dist/run-reliability.d.ts.map +1 -0
- package/dist/run-reliability.js +221 -0
- package/dist/run-reliability.js.map +1 -0
- package/dist/runtime.d.ts +15 -0
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +11 -1
- package/dist/runtime.js.map +1 -1
- package/dist/secret-guard.d.ts +36 -0
- package/dist/secret-guard.d.ts.map +1 -0
- package/dist/secret-guard.js +63 -0
- package/dist/secret-guard.js.map +1 -0
- package/dist/singleton-guidance.d.ts +73 -0
- package/dist/singleton-guidance.d.ts.map +1 -0
- package/dist/singleton-guidance.js +118 -0
- package/dist/singleton-guidance.js.map +1 -0
- package/dist/staged-candidate.d.ts +17 -0
- package/dist/staged-candidate.d.ts.map +1 -1
- package/dist/staged-candidate.js +50 -0
- package/dist/staged-candidate.js.map +1 -1
- package/dist/state-artifacts.d.ts +70 -0
- package/dist/state-artifacts.d.ts.map +1 -0
- package/dist/state-artifacts.js +215 -0
- package/dist/state-artifacts.js.map +1 -0
- package/dist/state.d.ts +170 -2
- package/dist/state.d.ts.map +1 -1
- package/dist/state.js +248 -8
- package/dist/state.js.map +1 -1
- package/dist/verifier-keys.d.ts +33 -2
- package/dist/verifier-keys.d.ts.map +1 -1
- package/dist/verifier-keys.js +95 -9
- package/dist/verifier-keys.js.map +1 -1
- package/guides/CONNECT-YOUR-PROJECT.md +367 -0
- package/guides/QUICKSTART.md +404 -0
- package/guides/RUNNER-NEUTRAL-EVIDENCE.md +199 -0
- package/guides/TEST-ENVIRONMENT.md +1226 -0
- package/guides/UPGRADE-0.6-to-0.7.md +65 -0
- package/guides/UPGRADE-0.7-to-0.8.md +114 -0
- package/package.json +12 -10
|
@@ -0,0 +1,771 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Content-addressed run cache: reuse
|
|
3
|
+
* unchanged detector and pytest-collection work at commit time.
|
|
4
|
+
*
|
|
5
|
+
* Invariants (the plan's hard rules):
|
|
6
|
+
* - The cache is a SPEED-UP, never proof. Receipts, attestations, and
|
|
7
|
+
* `--require-e2e` keep their exact checks; a cached contribution is
|
|
8
|
+
* ordinary detector input, re-validated by the same schemas.
|
|
9
|
+
* - Every key covers every input that can change the result: plugin
|
|
10
|
+
* id/version/config, the plugin module's bytes (repo-relative module or
|
|
11
|
+
* the whole installed package), the scanned input bytes, and the
|
|
12
|
+
* interpreter identity (absolute path, version, installed-package
|
|
13
|
+
* digest). Pytest keys add the collector argv and suite config.
|
|
14
|
+
* - Any doubt → full scan. An unreadable input, an unresolvable or
|
|
15
|
+
* non-python interpreter, a corrupt/foreign cache entry, a version or
|
|
16
|
+
* format mismatch — all degrade to running the work again.
|
|
17
|
+
* - The cache lives under the EXCLUDED run-state directory, never enters
|
|
18
|
+
* the input digest, and is never used as evidence.
|
|
19
|
+
* `GATEFORGE_NO_CACHE=1` (or `--no-cache`, or a recognized CI provider)
|
|
20
|
+
* forces a full scan: no reads, no writes.
|
|
21
|
+
*/
|
|
22
|
+
import { spawnSync, execFileSync } from 'node:child_process';
|
|
23
|
+
import { createHash } from 'node:crypto';
|
|
24
|
+
import { readdirSync, mkdirSync, lstatSync, readFileSync, renameSync, statSync, writeFileSync, } from 'node:fs';
|
|
25
|
+
import { createRequire } from 'node:module';
|
|
26
|
+
import { basename, dirname, join, resolve, sep } from 'node:path';
|
|
27
|
+
import { DetectorOutputSchema, canonicalJson, } from '@gate-forge/core';
|
|
28
|
+
import { VERSION } from './commands/common.js';
|
|
29
|
+
/** Cache entry format version; a mismatch is a miss (never a parse of foreign bytes). */
|
|
30
|
+
export const PLUGIN_CACHE_FORMAT_VERSION = 1;
|
|
31
|
+
/** Domain tag keeping plugin-cache keys separate from every other digest. */
|
|
32
|
+
export const PLUGIN_CACHE_DOMAIN = 'gateforge.plugin-cache.v1';
|
|
33
|
+
/** Directory under the run-state dir holding plugin cache entries. */
|
|
34
|
+
export const PLUGIN_CACHE_DIR = 'cache/plugin';
|
|
35
|
+
/** Directory under the run-state dir holding pytest collection entries. */
|
|
36
|
+
export const PYTEST_CACHE_DIR = 'cache/pytest';
|
|
37
|
+
/** Beyond this many files a package tree is treated as uncacheable (fail closed). */
|
|
38
|
+
const PACKAGE_DIGEST_FILE_BUDGET = 5000;
|
|
39
|
+
/** Pytest config documents hashed into the collection cache key. */
|
|
40
|
+
const PYTEST_CONFIG_BASENAMES = {
|
|
41
|
+
'pytest.ini': true,
|
|
42
|
+
'pyproject.toml': true,
|
|
43
|
+
'setup.cfg': true,
|
|
44
|
+
'tox.ini': true,
|
|
45
|
+
};
|
|
46
|
+
/** Memoized interpreter identities per resolved interpreter path (one probe per run). */
|
|
47
|
+
const INTERPRETER_IDENTITY_CACHE = new Map();
|
|
48
|
+
/**
|
|
49
|
+
* Resolves the cache control for one gate run.
|
|
50
|
+
*
|
|
51
|
+
* Args:
|
|
52
|
+
* env: process environment (kill switches and CI detection).
|
|
53
|
+
* stateDir: absolute run-state directory for cache storage.
|
|
54
|
+
* noCacheFlag: `--no-cache` presence.
|
|
55
|
+
*
|
|
56
|
+
* Returns:
|
|
57
|
+
* CacheControl: disabled when `--no-cache`, `GATEFORGE_NO_CACHE` is set,
|
|
58
|
+
* or a recognized CI-provider marker is present.
|
|
59
|
+
*/
|
|
60
|
+
export function resolveCacheControl(env, stateDir, noCacheFlag) {
|
|
61
|
+
const ciDetected = [
|
|
62
|
+
'GITHUB_ACTIONS', 'GITLAB_CI', 'BUILDKITE', 'CIRCLECI', 'JENKINS_URL', 'TF_BUILD',
|
|
63
|
+
].some((name) => Boolean(env[name]));
|
|
64
|
+
const noCacheEnv = env['GATEFORGE_NO_CACHE'] === '1' || env['GATEFORGE_NO_CACHE'] === 'true';
|
|
65
|
+
return { stateDir: resolve(stateDir), disabled: noCacheFlag || noCacheEnv || ciDetected };
|
|
66
|
+
}
|
|
67
|
+
/** sha256 of a string or buffer as lowercase hex. */
|
|
68
|
+
function sha256Hex(data) {
|
|
69
|
+
return createHash('sha256').update(data).digest('hex');
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Resolves a command name to an absolute executable path the way a child
|
|
73
|
+
* spawn would (PATH scan), or returns the input when already absolute.
|
|
74
|
+
*
|
|
75
|
+
* Args:
|
|
76
|
+
* command: the argv[0] of a configured plugin command.
|
|
77
|
+
* env: process environment providing PATH.
|
|
78
|
+
*
|
|
79
|
+
* Returns:
|
|
80
|
+
* string | null: absolute executable path, or null when not resolvable.
|
|
81
|
+
*/
|
|
82
|
+
function resolveExecutable(command, env) {
|
|
83
|
+
if (command.includes('/'))
|
|
84
|
+
return resolve(command);
|
|
85
|
+
const pathEntries = (env['PATH'] ?? '').split(':').filter((entry) => entry.length > 0);
|
|
86
|
+
for (const dir of pathEntries) {
|
|
87
|
+
const candidate = join(dir, command);
|
|
88
|
+
try {
|
|
89
|
+
if (statSync(candidate).isFile())
|
|
90
|
+
return candidate;
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
// Not in this PATH entry; keep scanning.
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Digests a directory's direct entries (name, mtimeMs, size — sorted) so
|
|
100
|
+
* installed-package changes (version bumps, reinstalls, in-place edits)
|
|
101
|
+
* invalidate interpreter identity cheaply. A missing directory digests as
|
|
102
|
+
* empty — deterministic, not an error.
|
|
103
|
+
*
|
|
104
|
+
* Args:
|
|
105
|
+
* directory: absolute directory to summarize.
|
|
106
|
+
*
|
|
107
|
+
* Returns:
|
|
108
|
+
* string: lowercase hex digest of the sorted entry summary.
|
|
109
|
+
*/
|
|
110
|
+
function digestDirectoryEntries(directory) {
|
|
111
|
+
let entries;
|
|
112
|
+
try {
|
|
113
|
+
entries = readdirSync(directory);
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
return sha256Hex('');
|
|
117
|
+
}
|
|
118
|
+
const summary = [];
|
|
119
|
+
for (const name of entries.sort()) {
|
|
120
|
+
try {
|
|
121
|
+
const stat = statSync(join(directory, name));
|
|
122
|
+
summary.push(`${name}:${stat.mtimeMs.toString(36)}:${stat.size.toString(36)}`);
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
summary.push(`${name}:gone`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return sha256Hex(summary.join('\n'));
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Probes one python-style interpreter for cache identity: absolute path,
|
|
132
|
+
* `sys.version`, and a site-packages (purelib) digest. Any failure returns
|
|
133
|
+
* null — the caller must then run the work uncached (fail closed).
|
|
134
|
+
*
|
|
135
|
+
* Args:
|
|
136
|
+
* interpreter: the configured interpreter command (e.g. `python3`).
|
|
137
|
+
* env: process environment for PATH resolution and the probe child.
|
|
138
|
+
*
|
|
139
|
+
* Returns:
|
|
140
|
+
* InterpreterIdentity | null: identity, or null when the interpreter
|
|
141
|
+
* cannot be probed as python.
|
|
142
|
+
*/
|
|
143
|
+
export function interpreterIdentity(interpreter, env) {
|
|
144
|
+
const resolved = resolveExecutable(interpreter, env);
|
|
145
|
+
if (resolved === null)
|
|
146
|
+
return null;
|
|
147
|
+
const target = pythonTargetOf(resolved, env);
|
|
148
|
+
if (target === null)
|
|
149
|
+
return null;
|
|
150
|
+
let base = INTERPRETER_IDENTITY_CACHE.get(target.python);
|
|
151
|
+
if (base === undefined) {
|
|
152
|
+
base = probeInterpreter(target.python, env);
|
|
153
|
+
INTERPRETER_IDENTITY_CACHE.set(target.python, base);
|
|
154
|
+
}
|
|
155
|
+
if (base === null)
|
|
156
|
+
return null;
|
|
157
|
+
return target.entryScriptDigest === undefined ? base : { ...base, entryScriptDigest: target.entryScriptDigest };
|
|
158
|
+
}
|
|
159
|
+
/** Matches python interpreter basenames: python, python3, python3.12. */
|
|
160
|
+
const PYTHON_BASENAME = /^python(\d+(\.\d+)*)?$/;
|
|
161
|
+
/**
|
|
162
|
+
* Finds the python interpreter behind a command WITHOUT executing it.
|
|
163
|
+
* A python binary (or python-named shim) is probed directly. Any other
|
|
164
|
+
* program is only accepted when it is a python entry script: its shebang
|
|
165
|
+
* (or pip's `/bin/sh` + `'''exec' "<python>"` long-path form) names a
|
|
166
|
+
* python interpreter; the script's own bytes then join the key. Anything
|
|
167
|
+
* else is uncacheable, because running an unknown program with `-c` can
|
|
168
|
+
* do real work (a `pytest` entry script treats `-c` as a config file and
|
|
169
|
+
* collects the whole suite).
|
|
170
|
+
*
|
|
171
|
+
* Args:
|
|
172
|
+
* resolved: absolute path of argv[0].
|
|
173
|
+
* env: process environment for PATH resolution of `env`-style shebangs.
|
|
174
|
+
*
|
|
175
|
+
* Returns:
|
|
176
|
+
* { python, entryScriptDigest? } | null: interpreter to probe, or null.
|
|
177
|
+
*/
|
|
178
|
+
function pythonTargetOf(resolved, env) {
|
|
179
|
+
if (PYTHON_BASENAME.test(basename(resolved)))
|
|
180
|
+
return { python: resolved };
|
|
181
|
+
let bytes;
|
|
182
|
+
try {
|
|
183
|
+
bytes = readFileSync(resolved);
|
|
184
|
+
}
|
|
185
|
+
catch {
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
if (bytes.length > 1024 * 1024 || bytes[0] !== 0x23 || bytes[1] !== 0x21)
|
|
189
|
+
return null;
|
|
190
|
+
const lines = bytes.subarray(0, 4096).toString('utf8').split('\n');
|
|
191
|
+
const shebang = (lines[0] ?? '').slice(2).trim().split(/\s+/);
|
|
192
|
+
let program = shebang[0] ?? '';
|
|
193
|
+
if (basename(program) === 'env') {
|
|
194
|
+
program = shebang.slice(1).find((token) => !token.startsWith('-')) ?? '';
|
|
195
|
+
}
|
|
196
|
+
if (!PYTHON_BASENAME.test(basename(program))) {
|
|
197
|
+
const execLine = /^'''exec' "([^"]+)" "\$0" "\$@"/.exec(lines[1] ?? '');
|
|
198
|
+
if (!['sh', 'bash'].includes(basename(program)) || execLine === null)
|
|
199
|
+
return null;
|
|
200
|
+
program = execLine[1] ?? '';
|
|
201
|
+
if (!PYTHON_BASENAME.test(basename(program)))
|
|
202
|
+
return null;
|
|
203
|
+
}
|
|
204
|
+
const python = resolveExecutable(program, env);
|
|
205
|
+
if (python === null)
|
|
206
|
+
return null;
|
|
207
|
+
return { python, entryScriptDigest: sha256Hex(bytes) };
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Builds the Node interpreter identity without spawning (the probe above
|
|
211
|
+
* is python-specific): the running executable and version are exact, and
|
|
212
|
+
* the repo's `node_modules` top-level entries stand in for the
|
|
213
|
+
* installed-package digest (an install, upgrade, or removal changes an
|
|
214
|
+
* entry). A missing `node_modules` digests deterministically as empty.
|
|
215
|
+
*
|
|
216
|
+
* Args:
|
|
217
|
+
* cwd: repo root whose `node_modules` is summarized.
|
|
218
|
+
*
|
|
219
|
+
* Returns:
|
|
220
|
+
* InterpreterIdentity: identity of the running Node installation.
|
|
221
|
+
*/
|
|
222
|
+
function nodeIdentityFor(cwd) {
|
|
223
|
+
return {
|
|
224
|
+
path: process.execPath,
|
|
225
|
+
version: process.version,
|
|
226
|
+
packagesDigest: digestDirectoryEntries(join(resolve(cwd), 'node_modules')),
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Runs the actual interpreter probe (spawn + purelib digest). Separated
|
|
231
|
+
* from {@link interpreterIdentity} so the memoization boundary stays clear.
|
|
232
|
+
*
|
|
233
|
+
* Args:
|
|
234
|
+
* resolved: absolute interpreter path.
|
|
235
|
+
* env: process environment for the probe child.
|
|
236
|
+
*
|
|
237
|
+
* Returns:
|
|
238
|
+
* InterpreterIdentity | null: identity, or null on any probe failure.
|
|
239
|
+
*/
|
|
240
|
+
function probeInterpreter(resolved, env) {
|
|
241
|
+
const probe = "import json, sys, sysconfig; print(json.dumps({'executable': sys.executable, 'version': sys.version, 'purelib': sysconfig.get_paths().get('purelib', '')}))";
|
|
242
|
+
const result = spawnSync(resolved, ['-c', probe], {
|
|
243
|
+
env: { ...env, PYTHONDONTWRITEBYTECODE: '1' },
|
|
244
|
+
encoding: 'utf8',
|
|
245
|
+
timeout: 30_000,
|
|
246
|
+
});
|
|
247
|
+
if (result.error !== undefined || result.status !== 0)
|
|
248
|
+
return null;
|
|
249
|
+
let parsed;
|
|
250
|
+
try {
|
|
251
|
+
parsed = JSON.parse(result.stdout);
|
|
252
|
+
}
|
|
253
|
+
catch {
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
if (typeof parsed.executable !== 'string' ||
|
|
257
|
+
typeof parsed.version !== 'string' ||
|
|
258
|
+
typeof parsed.purelib !== 'string') {
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
return {
|
|
262
|
+
path: parsed.executable,
|
|
263
|
+
version: parsed.version,
|
|
264
|
+
packagesDigest: digestDirectoryEntries(parsed.purelib),
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Digests one file's bytes with its repo-relative identity.
|
|
269
|
+
*
|
|
270
|
+
* Args:
|
|
271
|
+
* absolutePath: file to read.
|
|
272
|
+
* repoRelativePath: posix-relative identity mixed into the digest.
|
|
273
|
+
*
|
|
274
|
+
* Returns:
|
|
275
|
+
* string | null: hex digest, or null when the file is unreadable.
|
|
276
|
+
*/
|
|
277
|
+
function fileDigest(absolutePath, repoRelativePath) {
|
|
278
|
+
try {
|
|
279
|
+
return sha256Hex(`${repoRelativePath}\0${readFileSync(absolutePath)}`);
|
|
280
|
+
}
|
|
281
|
+
catch {
|
|
282
|
+
return null;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Resolves a package specifier to its installed package directory via the
|
|
287
|
+
* repo's require context (no module execution).
|
|
288
|
+
*
|
|
289
|
+
* Args:
|
|
290
|
+
* specifier: bare package specifier (e.g. `@gate-forge/pack-fastapi`).
|
|
291
|
+
* cwd: repo root owning node_modules resolution.
|
|
292
|
+
*
|
|
293
|
+
* Returns:
|
|
294
|
+
* string | null: absolute package directory, or null when unresolvable.
|
|
295
|
+
*/
|
|
296
|
+
function packageDirOf(specifier, cwd) {
|
|
297
|
+
try {
|
|
298
|
+
const require = createRequire(join(cwd, 'package.json'));
|
|
299
|
+
const manifestPath = require.resolve(`${specifier}/package.json`);
|
|
300
|
+
return dirname(manifestPath);
|
|
301
|
+
}
|
|
302
|
+
catch {
|
|
303
|
+
return null;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Digests a whole file tree recursively (relative path + bytes, sorted).
|
|
308
|
+
* Skips nested `node_modules` directories; exceeds the file budget → null
|
|
309
|
+
* (the caller treats the plugin as uncacheable instead of truncating).
|
|
310
|
+
*
|
|
311
|
+
* Args:
|
|
312
|
+
* root: absolute directory to digest.
|
|
313
|
+
*
|
|
314
|
+
* Returns:
|
|
315
|
+
* string | null: hex digest, or null when unreadable or over budget.
|
|
316
|
+
*/
|
|
317
|
+
function digestTree(root) {
|
|
318
|
+
const parts = [];
|
|
319
|
+
let count = 0;
|
|
320
|
+
const walk = (directory, relative) => {
|
|
321
|
+
let entries;
|
|
322
|
+
try {
|
|
323
|
+
entries = readdirSync(directory, { withFileTypes: true });
|
|
324
|
+
}
|
|
325
|
+
catch {
|
|
326
|
+
return false;
|
|
327
|
+
}
|
|
328
|
+
for (const entry of entries.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0))) {
|
|
329
|
+
if (entry.name === 'node_modules')
|
|
330
|
+
continue;
|
|
331
|
+
const childRelative = relative === '' ? entry.name : `${relative}/${entry.name}`;
|
|
332
|
+
const childAbsolute = join(directory, entry.name);
|
|
333
|
+
if (entry.isSymbolicLink())
|
|
334
|
+
continue;
|
|
335
|
+
if (entry.isDirectory()) {
|
|
336
|
+
if (!walk(childAbsolute, childRelative))
|
|
337
|
+
return false;
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
if (!entry.isFile())
|
|
341
|
+
continue;
|
|
342
|
+
count += 1;
|
|
343
|
+
if (count > PACKAGE_DIGEST_FILE_BUDGET)
|
|
344
|
+
return false;
|
|
345
|
+
const digest = fileDigest(childAbsolute, childRelative);
|
|
346
|
+
if (digest === null)
|
|
347
|
+
return false;
|
|
348
|
+
parts.push(digest);
|
|
349
|
+
}
|
|
350
|
+
return true;
|
|
351
|
+
};
|
|
352
|
+
return walk(root, '') ? sha256Hex(parts.join('\n')) : null;
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Digests the plugin module's bytes: a repo-relative module file's bytes,
|
|
356
|
+
* or (for package specifiers) the whole installed package tree — the
|
|
357
|
+
* detector's python sources live inside the package, so a pack update
|
|
358
|
+
* must invalidate cached results.
|
|
359
|
+
*
|
|
360
|
+
* Args:
|
|
361
|
+
* moduleSpecifier: configured in-process module (`./x.mjs` or package).
|
|
362
|
+
* cwd: repo root for relative resolution and package lookup.
|
|
363
|
+
*
|
|
364
|
+
* Returns:
|
|
365
|
+
* string | null: hex digest, or null when unresolvable/unreadable.
|
|
366
|
+
*/
|
|
367
|
+
function moduleBytesDigestOf(moduleSpecifier, cwd) {
|
|
368
|
+
if (moduleSpecifier.startsWith('./') || moduleSpecifier.startsWith('../')) {
|
|
369
|
+
return fileDigest(resolve(cwd, moduleSpecifier), moduleSpecifier);
|
|
370
|
+
}
|
|
371
|
+
const packageDir = packageDirOf(moduleSpecifier, cwd);
|
|
372
|
+
if (packageDir === null)
|
|
373
|
+
return null;
|
|
374
|
+
return digestTree(packageDir);
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Digests the plugin input bytes: every expanded scan path with its
|
|
378
|
+
* content, sorted by path (order-insensitive like the detectors' view).
|
|
379
|
+
*
|
|
380
|
+
* Args:
|
|
381
|
+
* cwd: repo root the relative paths resolve against.
|
|
382
|
+
* paths: repo-relative scanned paths handed to every plugin.
|
|
383
|
+
*
|
|
384
|
+
* Returns:
|
|
385
|
+
* string | null: hex digest, or null when any input is unreadable.
|
|
386
|
+
*/
|
|
387
|
+
export function digestPathListInputs(cwd, paths) {
|
|
388
|
+
const sorted = [...paths].sort();
|
|
389
|
+
const parts = [];
|
|
390
|
+
for (const relativePath of sorted) {
|
|
391
|
+
const digest = fileDigest(resolve(cwd, relativePath), relativePath);
|
|
392
|
+
if (digest === null)
|
|
393
|
+
return null;
|
|
394
|
+
parts.push(digest);
|
|
395
|
+
}
|
|
396
|
+
return sha256Hex(parts.join('\n'));
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Resolves and digests subprocess plugin source using its configured Python.
|
|
400
|
+
* Script invocations and `-m` imports are supported; unknown forms are
|
|
401
|
+
* uncacheable rather than reusing results without source identity.
|
|
402
|
+
*
|
|
403
|
+
* Args:
|
|
404
|
+
* command: configured subprocess argv.
|
|
405
|
+
* cwd: repository root used by the plugin process.
|
|
406
|
+
* env: inherited environment used for import resolution.
|
|
407
|
+
*
|
|
408
|
+
* Returns:
|
|
409
|
+
* string | null: source digest, or null when source resolution fails.
|
|
410
|
+
*/
|
|
411
|
+
function subprocessModuleBytesDigestOf(command, cwd, env) {
|
|
412
|
+
const python = command[0];
|
|
413
|
+
const mode = command[1];
|
|
414
|
+
if (python === undefined || mode === undefined)
|
|
415
|
+
return null;
|
|
416
|
+
if (mode === '-m') {
|
|
417
|
+
const moduleName = command[2];
|
|
418
|
+
const resolved = resolveExecutable(python, env);
|
|
419
|
+
if (moduleName === undefined || resolved === null)
|
|
420
|
+
return null;
|
|
421
|
+
const probe = [
|
|
422
|
+
'import importlib.util, json',
|
|
423
|
+
`spec = importlib.util.find_spec(${JSON.stringify(moduleName)})`,
|
|
424
|
+
"print(json.dumps({'origin': None if spec is None else spec.origin, 'locations': [] if spec is None or spec.submodule_search_locations is None else list(spec.submodule_search_locations)}))",
|
|
425
|
+
].join('; ');
|
|
426
|
+
const result = spawnSync(resolved, ['-c', probe], {
|
|
427
|
+
cwd,
|
|
428
|
+
env: { ...env, PYTHONDONTWRITEBYTECODE: '1' },
|
|
429
|
+
encoding: 'utf8',
|
|
430
|
+
timeout: 30_000,
|
|
431
|
+
});
|
|
432
|
+
if (result.error !== undefined || result.status !== 0)
|
|
433
|
+
return null;
|
|
434
|
+
try {
|
|
435
|
+
const found = JSON.parse(result.stdout);
|
|
436
|
+
if (Array.isArray(found.locations) && found.locations.length > 0) {
|
|
437
|
+
const digests = found.locations.map((location) => typeof location === 'string' ? digestTree(location) : null);
|
|
438
|
+
return digests.every((digest) => digest !== null)
|
|
439
|
+
? sha256Hex(digests.join('\n'))
|
|
440
|
+
: null;
|
|
441
|
+
}
|
|
442
|
+
if (typeof found.origin === 'string' && found.origin !== 'built-in' && found.origin !== 'frozen') {
|
|
443
|
+
const stat = statSync(found.origin);
|
|
444
|
+
return stat.isDirectory()
|
|
445
|
+
? digestTree(found.origin)
|
|
446
|
+
: fileDigest(found.origin, `python-module:${moduleName}`);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
catch {
|
|
450
|
+
return null;
|
|
451
|
+
}
|
|
452
|
+
return null;
|
|
453
|
+
}
|
|
454
|
+
if (mode.startsWith('-'))
|
|
455
|
+
return null;
|
|
456
|
+
return fileDigest(resolve(cwd, mode), `python-script:${mode}`);
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* Builds the full cache identity for one plugin run, or null when ANY
|
|
460
|
+
* component cannot be established (then the plugin must run fresh).
|
|
461
|
+
*
|
|
462
|
+
* Args:
|
|
463
|
+
* plugin: the configured plugin (id/version/transport/command/module).
|
|
464
|
+
* inputsDigest: precomputed digest of the expanded scan path bytes.
|
|
465
|
+
* cwd: repo root (module resolution).
|
|
466
|
+
* env: process environment for interpreter resolution.
|
|
467
|
+
*
|
|
468
|
+
* Returns:
|
|
469
|
+
* PluginCacheIdentity | null: identity, or null when uncacheable.
|
|
470
|
+
*/
|
|
471
|
+
export function pluginCacheIdentity(plugin, inputsDigest, cwd, env, engineVersion = VERSION) {
|
|
472
|
+
let interpreter;
|
|
473
|
+
if (plugin.transport === 'subprocess') {
|
|
474
|
+
const command = plugin.command ?? [];
|
|
475
|
+
interpreter = command.length > 0 ? interpreterIdentity(command[0] ?? '', env) : null;
|
|
476
|
+
}
|
|
477
|
+
else {
|
|
478
|
+
// In-process plugins run under Node, but the bundled detectors spawn
|
|
479
|
+
// python3 internally, so the machine's python3 identity rides the key
|
|
480
|
+
// too. A truly absent python is STABLE key material ('absent') — a
|
|
481
|
+
// python appearing later changes the key; a python that exists but
|
|
482
|
+
// cannot be probed is doubt → uncacheable (fail closed).
|
|
483
|
+
const pythonResolved = resolveExecutable('python3', env);
|
|
484
|
+
const python = pythonResolved === null ? 'absent' : interpreterIdentity(pythonResolved, env);
|
|
485
|
+
interpreter = python === null ? null : { node: nodeIdentityFor(cwd), python };
|
|
486
|
+
}
|
|
487
|
+
if (interpreter === null)
|
|
488
|
+
return null;
|
|
489
|
+
const moduleBytesDigest = plugin.transport === 'in-process' && plugin.module !== undefined
|
|
490
|
+
? moduleBytesDigestOf(plugin.module, cwd)
|
|
491
|
+
: plugin.transport === 'subprocess'
|
|
492
|
+
? subprocessModuleBytesDigestOf(plugin.command ?? [], cwd, env)
|
|
493
|
+
: null;
|
|
494
|
+
if (moduleBytesDigest === null)
|
|
495
|
+
return null;
|
|
496
|
+
return {
|
|
497
|
+
plugin: {
|
|
498
|
+
id: plugin.id,
|
|
499
|
+
version: plugin.version,
|
|
500
|
+
transport: plugin.transport,
|
|
501
|
+
...(plugin.command !== undefined ? { command: plugin.command } : {}),
|
|
502
|
+
...(plugin.module !== undefined ? { module: plugin.module } : {}),
|
|
503
|
+
},
|
|
504
|
+
engineVersion,
|
|
505
|
+
pythonEnvironment: {
|
|
506
|
+
PYTHONPATH: env['PYTHONPATH'],
|
|
507
|
+
PYTHONHOME: env['PYTHONHOME'],
|
|
508
|
+
VIRTUAL_ENV: env['VIRTUAL_ENV'],
|
|
509
|
+
},
|
|
510
|
+
moduleBytesDigest,
|
|
511
|
+
inputsDigest,
|
|
512
|
+
interpreter,
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* Looks up one plugin's cached discovery result. Null means "run fresh"
|
|
517
|
+
* — either the identity is uncacheable or any validation step doubted.
|
|
518
|
+
*
|
|
519
|
+
* Args:
|
|
520
|
+
* stateDir: absolute run-state directory.
|
|
521
|
+
* plugin: the configured plugin.
|
|
522
|
+
* inputsDigest: digest of the expanded scan path bytes.
|
|
523
|
+
* cwd: repo root.
|
|
524
|
+
* env: process environment for interpreter resolution.
|
|
525
|
+
*
|
|
526
|
+
* Returns:
|
|
527
|
+
* DetectorOutput | null: the cached validated output, or null.
|
|
528
|
+
*/
|
|
529
|
+
export function lookupPluginCache(stateDir, plugin, inputsDigest, cwd, env) {
|
|
530
|
+
const identity = pluginCacheIdentity(plugin, inputsDigest, cwd, env);
|
|
531
|
+
if (identity === null)
|
|
532
|
+
return null;
|
|
533
|
+
return readPluginCache(stateDir, pluginCacheKey(identity));
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* Stores one fresh plugin result under its content key. Uncacheable
|
|
537
|
+
* identities skip silently — the result was already computed, and
|
|
538
|
+
* skipping storage only costs a future miss.
|
|
539
|
+
*
|
|
540
|
+
* Args:
|
|
541
|
+
* stateDir: absolute run-state directory.
|
|
542
|
+
* plugin: the configured plugin.
|
|
543
|
+
* inputsDigest: digest of the expanded scan path bytes.
|
|
544
|
+
* cwd: repo root.
|
|
545
|
+
* env: process environment for interpreter resolution.
|
|
546
|
+
* result: the validated discovery output.
|
|
547
|
+
*
|
|
548
|
+
* Returns:
|
|
549
|
+
* void.
|
|
550
|
+
*/
|
|
551
|
+
export function storePluginResult(stateDir, plugin, inputsDigest, cwd, env, result) {
|
|
552
|
+
const identity = pluginCacheIdentity(plugin, inputsDigest, cwd, env);
|
|
553
|
+
if (identity === null)
|
|
554
|
+
return;
|
|
555
|
+
writePluginCache(stateDir, pluginCacheKey(identity), result);
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* Computes the cache key for one plugin identity (domain-separated,
|
|
559
|
+
* canonical JSON so key order never matters).
|
|
560
|
+
*
|
|
561
|
+
* Args:
|
|
562
|
+
* identity: the plugin cache identity.
|
|
563
|
+
*
|
|
564
|
+
* Returns:
|
|
565
|
+
* string: 64-char lowercase hex cache key.
|
|
566
|
+
*/
|
|
567
|
+
export function pluginCacheKey(identity) {
|
|
568
|
+
// canonicalJson takes JsonValue; the identity is a closed internal shape.
|
|
569
|
+
const payload = { domain: PLUGIN_CACHE_DOMAIN, identity };
|
|
570
|
+
return sha256Hex(canonicalJson(payload));
|
|
571
|
+
}
|
|
572
|
+
/** Absolute cache entry path for one key. */
|
|
573
|
+
function pluginCachePath(stateDir, key) {
|
|
574
|
+
return join(resolve(stateDir), ...PLUGIN_CACHE_DIR.split('/'), `${key}.json`);
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* Reads one plugin cache entry, validating format, key, and the pinned
|
|
578
|
+
* discovery shape. ANY doubt (missing file, corrupt JSON, foreign key,
|
|
579
|
+
* schema drift) is a miss — never a guess.
|
|
580
|
+
*
|
|
581
|
+
* Args:
|
|
582
|
+
* stateDir: absolute run-state directory.
|
|
583
|
+
* key: the computed cache key.
|
|
584
|
+
*
|
|
585
|
+
* Returns:
|
|
586
|
+
* DetectorOutput | null: the cached validated output, or null on any doubt.
|
|
587
|
+
*/
|
|
588
|
+
export function readPluginCache(stateDir, key) {
|
|
589
|
+
let raw;
|
|
590
|
+
try {
|
|
591
|
+
raw = readFileSync(pluginCachePath(stateDir, key), 'utf8');
|
|
592
|
+
}
|
|
593
|
+
catch {
|
|
594
|
+
return null;
|
|
595
|
+
}
|
|
596
|
+
let entry;
|
|
597
|
+
try {
|
|
598
|
+
entry = JSON.parse(raw);
|
|
599
|
+
}
|
|
600
|
+
catch {
|
|
601
|
+
return null;
|
|
602
|
+
}
|
|
603
|
+
if (entry.formatVersion !== PLUGIN_CACHE_FORMAT_VERSION ||
|
|
604
|
+
entry.key !== key ||
|
|
605
|
+
typeof entry.result !== 'object' ||
|
|
606
|
+
entry.result === null ||
|
|
607
|
+
DetectorOutputSchema.safeParse(entry.result).success === false) {
|
|
608
|
+
return null;
|
|
609
|
+
}
|
|
610
|
+
return entry.result;
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* Persists one validated plugin result under its key (atomic tmp+rename,
|
|
614
|
+
* so a concurrent reader never sees a partial document).
|
|
615
|
+
*
|
|
616
|
+
* Args:
|
|
617
|
+
* stateDir: absolute run-state directory (cache lives inside, excluded).
|
|
618
|
+
* key: the computed cache key.
|
|
619
|
+
* result: the validated discovery output to store.
|
|
620
|
+
*
|
|
621
|
+
* Returns:
|
|
622
|
+
* void.
|
|
623
|
+
*/
|
|
624
|
+
export function writePluginCache(stateDir, key, result) {
|
|
625
|
+
const entry = {
|
|
626
|
+
formatVersion: PLUGIN_CACHE_FORMAT_VERSION,
|
|
627
|
+
key,
|
|
628
|
+
result,
|
|
629
|
+
};
|
|
630
|
+
const target = pluginCachePath(stateDir, key);
|
|
631
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
632
|
+
const tmp = `${target}.tmp-${process.pid}`;
|
|
633
|
+
writeFileSync(tmp, canonicalJson(entry), 'utf8');
|
|
634
|
+
renameSync(tmp, target);
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* Computes the pytest collection cache key for one suite (Phase 3 domain).
|
|
638
|
+
*
|
|
639
|
+
* Args:
|
|
640
|
+
* suite: configured suite and the exact collector argv.
|
|
641
|
+
* pythonFilesDigest: digest over all Python and pytest configuration bytes.
|
|
642
|
+
* interpreter: collector interpreter identity (null = uncacheable).
|
|
643
|
+
* environmentDigest: digest over the collector's effective environment.
|
|
644
|
+
*
|
|
645
|
+
* Returns:
|
|
646
|
+
* string: 64-character lowercase hexadecimal cache key.
|
|
647
|
+
*/
|
|
648
|
+
export function pytestCacheKey(suite, pythonFilesDigest, interpreter, environmentDigest, engineVersion = VERSION) {
|
|
649
|
+
const payload = {
|
|
650
|
+
domain: 'gateforge.pytest-cache.v1',
|
|
651
|
+
engineVersion,
|
|
652
|
+
suite,
|
|
653
|
+
pythonFilesDigest,
|
|
654
|
+
interpreter,
|
|
655
|
+
environmentDigest,
|
|
656
|
+
};
|
|
657
|
+
return sha256Hex(canonicalJson(payload));
|
|
658
|
+
}
|
|
659
|
+
/** Absolute pytest cache entry path for one key. */
|
|
660
|
+
export function pytestCachePath(stateDir, key) {
|
|
661
|
+
return join(resolve(stateDir), ...PYTEST_CACHE_DIR.split('/'), `${key}.json`);
|
|
662
|
+
}
|
|
663
|
+
/**
|
|
664
|
+
* Reads a persisted pytest collection document (nodes + summary pieces).
|
|
665
|
+
* Validates the domain envelope; any doubt is a miss.
|
|
666
|
+
*
|
|
667
|
+
* Args:
|
|
668
|
+
* stateDir: absolute run-state directory.
|
|
669
|
+
* key: the computed pytest cache key.
|
|
670
|
+
*
|
|
671
|
+
* Returns:
|
|
672
|
+
* unknown | null: the stored document, or null on any doubt.
|
|
673
|
+
*/
|
|
674
|
+
export function readPytestCache(stateDir, key) {
|
|
675
|
+
let raw;
|
|
676
|
+
try {
|
|
677
|
+
raw = readFileSync(pytestCachePath(stateDir, key), 'utf8');
|
|
678
|
+
}
|
|
679
|
+
catch {
|
|
680
|
+
return null;
|
|
681
|
+
}
|
|
682
|
+
let entry;
|
|
683
|
+
try {
|
|
684
|
+
entry = JSON.parse(raw);
|
|
685
|
+
}
|
|
686
|
+
catch {
|
|
687
|
+
return null;
|
|
688
|
+
}
|
|
689
|
+
if (entry.domain !== 'gateforge.pytest-cache.v1' ||
|
|
690
|
+
entry.formatVersion !== PLUGIN_CACHE_FORMAT_VERSION ||
|
|
691
|
+
entry.key !== key) {
|
|
692
|
+
return null;
|
|
693
|
+
}
|
|
694
|
+
return entry.result;
|
|
695
|
+
}
|
|
696
|
+
/**
|
|
697
|
+
* Persists one pytest collection document under its key (atomic rename).
|
|
698
|
+
*
|
|
699
|
+
* Args:
|
|
700
|
+
* stateDir: absolute run-state directory.
|
|
701
|
+
* key: the computed pytest cache key.
|
|
702
|
+
* result: the collection document to store.
|
|
703
|
+
*
|
|
704
|
+
* Returns:
|
|
705
|
+
* void.
|
|
706
|
+
*/
|
|
707
|
+
export function writePytestCache(stateDir, key, result) {
|
|
708
|
+
const target = pytestCachePath(stateDir, key);
|
|
709
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
710
|
+
const tmp = `${target}.tmp-${process.pid}`;
|
|
711
|
+
writeFileSync(tmp, canonicalJson({
|
|
712
|
+
domain: 'gateforge.pytest-cache.v1',
|
|
713
|
+
formatVersion: PLUGIN_CACHE_FORMAT_VERSION,
|
|
714
|
+
key,
|
|
715
|
+
result: result,
|
|
716
|
+
}), 'utf8');
|
|
717
|
+
renameSync(tmp, target);
|
|
718
|
+
}
|
|
719
|
+
/**
|
|
720
|
+
* Digests Python and pytest-config files from Git's tracked and nonignored
|
|
721
|
+
* untracked inventory, excluding run-state directories.
|
|
722
|
+
*
|
|
723
|
+
* Args:
|
|
724
|
+
* cwd: absolute repository root.
|
|
725
|
+
* stateDirs: absolute run-state directories to exclude.
|
|
726
|
+
*
|
|
727
|
+
* Returns:
|
|
728
|
+
* string | null: digest, or null when inventory or an input is unreadable.
|
|
729
|
+
*/
|
|
730
|
+
export function digestPytestInputs(cwd, ...stateDirs) {
|
|
731
|
+
const root = resolve(cwd);
|
|
732
|
+
const stateRoots = stateDirs.map((directory) => resolve(directory));
|
|
733
|
+
let inventory;
|
|
734
|
+
try {
|
|
735
|
+
const bytes = execFileSync('git', ['ls-files', '-z', '--cached', '--others', '--exclude-standard'], {
|
|
736
|
+
cwd: root,
|
|
737
|
+
encoding: 'buffer',
|
|
738
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
739
|
+
});
|
|
740
|
+
inventory = bytes.toString('utf8').split('\0').filter(Boolean);
|
|
741
|
+
}
|
|
742
|
+
catch {
|
|
743
|
+
return null;
|
|
744
|
+
}
|
|
745
|
+
const parts = [];
|
|
746
|
+
for (const name of inventory) {
|
|
747
|
+
const absolute = resolve(root, name);
|
|
748
|
+
if (!absolute.startsWith(`${root}${sep}`))
|
|
749
|
+
return null;
|
|
750
|
+
if (stateRoots.some((stateRoot) => absolute === stateRoot || absolute.startsWith(`${stateRoot}${sep}`)))
|
|
751
|
+
continue;
|
|
752
|
+
const basename = name.slice(name.lastIndexOf('/') + 1);
|
|
753
|
+
if (!basename.endsWith('.py') && PYTEST_CONFIG_BASENAMES[basename] !== true)
|
|
754
|
+
continue;
|
|
755
|
+
let stat;
|
|
756
|
+
try {
|
|
757
|
+
stat = lstatSync(absolute);
|
|
758
|
+
}
|
|
759
|
+
catch {
|
|
760
|
+
return null;
|
|
761
|
+
}
|
|
762
|
+
if (stat.isSymbolicLink())
|
|
763
|
+
return null;
|
|
764
|
+
const digest = fileDigest(absolute, name);
|
|
765
|
+
if (digest === null)
|
|
766
|
+
return null;
|
|
767
|
+
parts.push(digest);
|
|
768
|
+
}
|
|
769
|
+
return sha256Hex(parts.sort().join('\n'));
|
|
770
|
+
}
|
|
771
|
+
//# sourceMappingURL=run-cache.js.map
|