@ornexus/neocortex 4.60.67 → 4.60.68
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/release-source.json +1 -1
- package/dist/sbom.cdx.json +1194 -1194
- package/install.sh +1 -1
- package/package.json +2 -2
- package/packages/client/dist/adapters/adapter-registry.js +108 -1
- package/packages/client/dist/adapters/antigravity-adapter.js +77 -2
- package/packages/client/dist/adapters/claude-code-adapter.js +79 -3
- package/packages/client/dist/adapters/codex-adapter.js +80 -2
- package/packages/client/dist/adapters/cursor-adapter.js +115 -4
- package/packages/client/dist/adapters/gemini-adapter.js +71 -2
- package/packages/client/dist/adapters/grok-adapter.js +42 -2
- package/packages/client/dist/adapters/index.js +22 -1
- package/packages/client/dist/adapters/platform-detector.js +212 -1
- package/packages/client/dist/adapters/target-adapter.js +12 -0
- package/packages/client/dist/adapters/vscode-adapter.js +72 -2
- package/packages/client/dist/agent/refresh-stubs.js +417 -2
- package/packages/client/dist/agent/update-agent-yaml.js +102 -1
- package/packages/client/dist/agent/update-description.js +251 -1
- package/packages/client/dist/cache/crypto-utils.js +78 -1
- package/packages/client/dist/cache/encrypted-cache.js +108 -1
- package/packages/client/dist/cache/in-memory-asset-cache.js +73 -1
- package/packages/client/dist/cache/index.js +13 -1
- package/packages/client/dist/cache/protected-pi-boundary.js +44 -1
- package/packages/client/dist/checkpoint/checkpoint-client-reader.js +127 -2
- package/packages/client/dist/checkpoint/index.js +11 -1
- package/packages/client/dist/checkpoint/shared-checkpoint-types.js +32 -1
- package/packages/client/dist/cli.js +290 -3
- package/packages/client/dist/commands/activate.js +394 -8
- package/packages/client/dist/commands/architecture-answers.js +274 -7
- package/packages/client/dist/commands/architecture-memory-terminal.js +253 -4
- package/packages/client/dist/commands/architecture-non-ui-continuation.js +100 -1
- package/packages/client/dist/commands/architecture-root-binding.js +51 -1
- package/packages/client/dist/commands/cache-status.js +112 -2
- package/packages/client/dist/commands/config.js +88 -1
- package/packages/client/dist/commands/doctor.js +635 -2
- package/packages/client/dist/commands/invoke.js +7140 -142
- package/packages/client/dist/commands/legacy-epic-terminal.js +71 -1
- package/packages/client/dist/commands/legacy-story-branch.js +96 -1
- package/packages/client/dist/commands/legacy-story-pr.js +276 -1
- package/packages/client/dist/commands/mutation-qa-checkpoint.js +285 -5
- package/packages/client/dist/commands/mutation-qa-gate.js +54 -1
- package/packages/client/dist/commands/mutation-qa-git.js +41 -1
- package/packages/client/dist/commands/mutation-qa-policy.js +102 -1
- package/packages/client/dist/commands/mutation-qa-run.js +219 -3
- package/packages/client/dist/commands/mutation-qa-verdict.js +149 -1
- package/packages/client/dist/commands/mutation-qa.js +220 -1
- package/packages/client/dist/commands/native-agent.js +136 -2
- package/packages/client/dist/commands/planning-diagnosis.js +43 -1
- package/packages/client/dist/commands/planning-generation.js +53 -1
- package/packages/client/dist/commands/planning-memory.js +40 -1
- package/packages/client/dist/commands/planning-research.js +42 -1
- package/packages/client/dist/commands/private-session.js +75 -1
- package/packages/client/dist/commands/property-qa.js +139 -1
- package/packages/client/dist/commands/qa-control.js +16 -1
- package/packages/client/dist/commands/qa-scan-browser-launch.js +55 -1
- package/packages/client/dist/commands/qa-scan-browser-route.js +28 -1
- package/packages/client/dist/commands/qa-scan-capture.js +663 -5
- package/packages/client/dist/commands/qa-scan-discovery.js +376 -3
- package/packages/client/dist/commands/qa-scan-review.js +826 -5
- package/packages/client/dist/commands/qa-synthetic-backend-capture.js +266 -1
- package/packages/client/dist/commands/qa-synthetic-backend-review.js +297 -1
- package/packages/client/dist/commands/qa-synthetic-backend.js +340 -2
- package/packages/client/dist/commands/qa-synthetic-broker-port.js +85 -1
- package/packages/client/dist/commands/qa-synthetic-browser.js +39 -1
- package/packages/client/dist/commands/refactor-characterization.js +671 -21
- package/packages/client/dist/commands/refactor-commit.js +399 -13
- package/packages/client/dist/commands/refactor-control.js +142 -3
- package/packages/client/dist/commands/refactor-cpp-assertions.js +93 -3
- package/packages/client/dist/commands/refactor-review.js +283 -5
- package/packages/client/dist/commands/refactor-root.js +588 -7
- package/packages/client/dist/commands/refresh-memory.js +156 -3
- package/packages/client/dist/commands/shared-architecture-non-ui.js +24 -1
- package/packages/client/dist/commands/story-checkpoint.js +171 -1
- package/packages/client/dist/config/profile-directory.js +13 -1
- package/packages/client/dist/config/resolver-selection.js +278 -1
- package/packages/client/dist/config/secure-config.js +281 -12
- package/packages/client/dist/config.js +223 -2
- package/packages/client/dist/constants.js +25 -1
- package/packages/client/dist/context/context-collector.js +222 -2
- package/packages/client/dist/context/context-sanitizer.js +145 -1
- package/packages/client/dist/continuity/autonomous-governance.js +285 -1
- package/packages/client/dist/continuity/continuity-client-state-store.js +1587 -1
- package/packages/client/dist/continuity/invoke-hooks.js +129 -1
- package/packages/client/dist/continuity/migrations/001-initial-schema.js +12 -2
- package/packages/client/dist/continuity/migrations/002-processing-output-summary-indexes.js +12 -2
- package/packages/client/dist/continuity/migrations/003-headless-execution-attempts.js +12 -2
- package/packages/client/dist/continuity/migrations/004-outbox-delivery-state.js +12 -2
- package/packages/client/dist/continuity/migrations/005-bounded-headless-queue.js +12 -2
- package/packages/client/dist/continuity/migrations/006-native-agent-dispatches.js +10 -2
- package/packages/client/dist/continuity/native-agent-store.js +69 -1
- package/packages/client/dist/continuity/native-agent-types.js +90 -1
- package/packages/client/dist/continuity/outbox.js +117 -1
- package/packages/client/dist/continuity/runner-bootstrap-policy.js +172 -1
- package/packages/client/dist/continuity/sqlite-store.js +2536 -81
- package/packages/client/dist/effects/invoke-effect-gateways.js +62 -1
- package/packages/client/dist/errors/error-messages.js +228 -2
- package/packages/client/dist/errors/frontmatter-diagnostics.js +125 -1
- package/packages/client/dist/evidence/collector.js +347 -1
- package/packages/client/dist/evidence/index.js +1 -1
- package/packages/client/dist/evidence/types.js +2 -0
- package/packages/client/dist/fs/atomic-state-replace.js +152 -1
- package/packages/client/dist/generated/command-registry.generated.js +4076 -1
- package/packages/client/dist/generated/public-projections.generated.js +149 -1
- package/packages/client/dist/git/direct-yolo-c01-retry.js +138 -4
- package/packages/client/dist/git/direct-yolo-checkpoint.js +1234 -4
- package/packages/client/dist/git/git-lifecycle-step-ids.js +14 -1
- package/packages/client/dist/git/git-lifecycle-store.js +1938 -9
- package/packages/client/dist/git/legacy-story-branch-checkpoint.js +259 -9
- package/packages/client/dist/git/legacy-story-branch-setup.js +348 -4
- package/packages/client/dist/git/legacy-story-pr.js +3538 -70
- package/packages/client/dist/git/secure-filesystem-boundary.js +182 -5
- package/packages/client/dist/graph-retrieval/pre-command-hook.js +284 -1
- package/packages/client/dist/graph-retrieval/shared-graph-retrieval-contract.js +237 -1
- package/packages/client/dist/i18n/first-run.js +60 -2
- package/packages/client/dist/index.js +47 -1
- package/packages/client/dist/invocation/invoke-input.js +24 -1
- package/packages/client/dist/license/index.js +5 -1
- package/packages/client/dist/license/license-client.js +304 -1
- package/packages/client/dist/machine/fingerprint.js +163 -2
- package/packages/client/dist/machine/index.js +5 -1
- package/packages/client/dist/memory/project-memory-path-scope.js +62 -1
- package/packages/client/dist/memory/project-memory-writer.js +1007 -40
- package/packages/client/dist/memory/shared-project-memory-types.js +253 -2
- package/packages/client/dist/migrations/project-identity-capability.js +50 -1
- package/packages/client/dist/migrations/project-identity.js +493 -7
- package/packages/client/dist/migrations/state-json.js +344 -4
- package/packages/client/dist/policy/architecture-policy.js +184 -2
- package/packages/client/dist/policy/index.js +7 -1
- package/packages/client/dist/policy/shared-policy-types.js +38 -0
- package/packages/client/dist/policy/subagent-approval-policy.js +55 -1
- package/packages/client/dist/private-execution/clean-baseline.js +279 -2
- package/packages/client/dist/private-execution/codex-executor.js +574 -4
- package/packages/client/dist/private-execution/codex-host-runtime.js +137 -1
- package/packages/client/dist/private-execution/codex-lifecycle-guard.js +97 -1
- package/packages/client/dist/private-execution/codex-scoped-permissions.js +128 -1
- package/packages/client/dist/private-execution/delivery.js +410 -13
- package/packages/client/dist/private-execution/diagnosis-root-review.js +294 -2
- package/packages/client/dist/private-execution/frozen-handoff.js +19 -6
- package/packages/client/dist/private-execution/implementation-validation.js +106 -2
- package/packages/client/dist/private-execution/isolated-admission.js +1432 -23
- package/packages/client/dist/private-execution/isolated-session.js +725 -5
- package/packages/client/dist/private-execution/isolated-workspace.js +122 -1
- package/packages/client/dist/private-execution/pact-consumer-runtime.js +409 -3
- package/packages/client/dist/private-execution/pact-test-source.js +258 -1
- package/packages/client/dist/private-execution/pact-toolchain.js +290 -3
- package/packages/client/dist/private-execution/pact-write-set.js +172 -1
- package/packages/client/dist/private-execution/planning-diagnosis-checkpoint.js +406 -5
- package/packages/client/dist/private-execution/planning-generation-checkpoint.js +462 -5
- package/packages/client/dist/private-execution/planning-memory-checkpoint.js +139 -3
- package/packages/client/dist/private-execution/planning-research-checkpoint.js +325 -4
- package/packages/client/dist/private-execution/private-report-store.js +42 -1
- package/packages/client/dist/private-execution/property-test-candidate.js +103 -1
- package/packages/client/dist/private-execution/property-test-choices.js +147 -3
- package/packages/client/dist/private-execution/property-test-plan.js +271 -7
- package/packages/client/dist/private-execution/property-test-review.js +258 -4
- package/packages/client/dist/private-execution/property-test-runtime.js +372 -9
- package/packages/client/dist/private-execution/property-test-source.js +139 -1
- package/packages/client/dist/private-execution/root-checkpoint.js +80 -1
- package/packages/client/dist/private-execution/root-memory-git-evidence.js +135 -3
- package/packages/client/dist/private-execution/root-memory-recovery.js +93 -1
- package/packages/client/dist/private-execution/root-memory-request.js +29 -1
- package/packages/client/dist/private-execution/root-memory-scope.js +178 -1
- package/packages/client/dist/private-execution/root-memory-writer.js +102 -1
- package/packages/client/dist/private-execution/session-lease.js +63 -1
- package/packages/client/dist/private-execution/shared-private-admission-types.js +50 -1
- package/packages/client/dist/private-execution/shared-project-validation.js +66 -1
- package/packages/client/dist/private-execution/shared-root-memory-types.js +61 -1
- package/packages/client/dist/private-execution/ux-source-validation.js +372 -1
- package/packages/client/dist/private-execution/ux-write-set.js +191 -2
- package/packages/client/dist/resilience/circuit-breaker.js +170 -1
- package/packages/client/dist/resilience/degradation-manager.js +164 -1
- package/packages/client/dist/resilience/freshness-indicator.js +100 -1
- package/packages/client/dist/resilience/index.js +8 -1
- package/packages/client/dist/resilience/recovery-detector.js +74 -1
- package/packages/client/dist/resolvers/asset-resolver.js +13 -0
- package/packages/client/dist/resolvers/local-resolver.js +255 -8
- package/packages/client/dist/resolvers/remote-resolver.js +233 -1
- package/packages/client/dist/runner/binary.js +404 -1
- package/packages/client/dist/runner/cli.js +1571 -17
- package/packages/client/dist/runner/headless/adapters.js +49 -1
- package/packages/client/dist/runner/headless/claude-code-adapter.js +79 -1
- package/packages/client/dist/runner/headless/continuous-story-workspace.js +1628 -11
- package/packages/client/dist/runner/headless/epic-git-reconciliation-inventory.js +69 -1
- package/packages/client/dist/runner/headless/epic-merge-candidate-qa.js +110 -2
- package/packages/client/dist/runner/headless/epic-terminal-git-cli-adapter.js +1236 -8
- package/packages/client/dist/runner/headless/epic-terminal-git-runner.js +318 -2
- package/packages/client/dist/runner/headless/fake-adapter.js +62 -1
- package/packages/client/dist/runner/headless/managed-profile.js +132 -1
- package/packages/client/dist/runner/headless/opencode-adapter.js +145 -2
- package/packages/client/dist/runner/headless/prepared-story-bundle.js +43 -1
- package/packages/client/dist/runner/headless/resolver.js +231 -1
- package/packages/client/dist/runner/headless/run-yolo.js +885 -1
- package/packages/client/dist/runner/headless/run-yoloop.js +2574 -2
- package/packages/client/dist/runner/headless/types.js +574 -2
- package/packages/client/dist/runner/local-console.js +396 -13
- package/packages/client/dist/runner/portal-sync-worker.js +1487 -1
- package/packages/client/dist/runner/project-identity.js +106 -1
- package/packages/client/dist/runner/scheduler.js +995 -7
- package/packages/client/dist/runner-cli.js +18 -2
- package/packages/client/dist/state/invocation-shape.js +13 -1
- package/packages/client/dist/state/project-state-snapshot.js +1677 -1
- package/packages/client/dist/state/state-json-repair.js +785 -3
- package/packages/client/dist/state/structural-dag-projection.js +118 -1
- package/packages/client/dist/telemetry/index.js +5 -1
- package/packages/client/dist/telemetry/offline-queue.js +131 -1
- package/packages/client/dist/tier/index.js +5 -1
- package/packages/client/dist/tier/tier-aware-client.js +405 -1
- package/packages/client/dist/types/index.js +38 -1
- package/packages/client/dist/version.js +27 -1
- package/packages/client/dist/yolo/change-evidence-collector.js +126 -1
- package/packages/client/dist/yolo/invoke-envelope-capability.js +62 -1
- package/packages/client/dist/yolo/spec-envelope-collector.js +185 -5
- package/packages/client/dist/yoloop/discovery-hook.js +300 -2
- package/packages/client/dist/yoloop/index.js +8 -1
- package/packages/client/dist/yoloop/invoke-hooks.js +669 -12
- package/packages/client/dist/yoloop/shared-discover-epics.js +1007 -1
- package/packages/client/dist/yoloop/shared-yoloop-types.js +33 -1
- package/packages/client/dist/yoloop/target-normalization.js +180 -1
- package/packages/client/dist/yoloop/yoloop-client-state-store.js +1723 -1
- package/targets-stubs/antigravity/gemini.md +1 -1
- package/targets-stubs/antigravity/skill/SKILL.md +1 -1
- package/targets-stubs/claude-code/neocortex-root.agent.yaml +1 -1
- package/targets-stubs/claude-code/neocortex-root.md +2 -2
- package/targets-stubs/claude-code/neocortex.agent.yaml +1 -1
- package/targets-stubs/claude-code/neocortex.md +2 -2
- package/targets-stubs/codex/AGENTS.md +1 -1
- package/targets-stubs/cursor/agent.md +2 -2
- package/targets-stubs/gemini-cli/agent.md +2 -2
- package/targets-stubs/opencode/neocortex-root.md +1 -1
- package/targets-stubs/vscode/neocortex.agent.md +2 -2
|
@@ -1,21 +1,671 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
/** Root-owned, no-network observation for admitted Node JS/TS, Python or C++ tests. */
|
|
2
|
+
import { spawnSync } from 'node:child_process';
|
|
3
|
+
import { createHash, randomBytes } from 'node:crypto';
|
|
4
|
+
import { closeSync, constants, existsSync, fsyncSync, lstatSync, mkdirSync, mkdtempSync, openSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
|
|
5
|
+
import { tmpdir } from 'node:os';
|
|
6
|
+
import { basename, dirname, join, resolve } from 'node:path';
|
|
7
|
+
import { mutationQaGit } from './mutation-qa-git.js';
|
|
8
|
+
import { exactCommittedRefactorFile, privateRefactorDirectory, readPrivateRefactorAdmission, verifyRootRefactorForCharacterization } from './refactor-root.js';
|
|
9
|
+
const SHA256 = /^[a-f0-9]{64}$/;
|
|
10
|
+
const MAX_RUN_BYTES = 64 * 1024 * 1024;
|
|
11
|
+
const MAX_TESTS = 32;
|
|
12
|
+
const MAX_DEPENDENCIES = 128;
|
|
13
|
+
/** Node's native type-stripping switch first exists in 22.6.0. */
|
|
14
|
+
export function supportsNativeTypeStrippingVersion(version) {
|
|
15
|
+
const match = version.match(/^(\d+)\.(\d+)\.(\d+)$/);
|
|
16
|
+
if (!match)
|
|
17
|
+
return false;
|
|
18
|
+
const major = Number(match[1]);
|
|
19
|
+
const minor = Number(match[2]);
|
|
20
|
+
return major > 22 || (major === 22 && minor >= 6);
|
|
21
|
+
}
|
|
22
|
+
/** Distribution capability is separate from semver; some Node builds disable Amaro. */
|
|
23
|
+
export function supportsNativeTypeStrippingRuntime() {
|
|
24
|
+
return supportsNativeTypeStrippingVersion(process.versions.node)
|
|
25
|
+
&& process.config.variables.node_use_amaro === true;
|
|
26
|
+
}
|
|
27
|
+
function digest(bytes) {
|
|
28
|
+
return createHash('sha256').update(bytes).digest('hex');
|
|
29
|
+
}
|
|
30
|
+
function safePath(path) {
|
|
31
|
+
return path.length > 0 && path.length <= 512
|
|
32
|
+
&& /^[A-Za-z0-9_.-]+(?:\/[A-Za-z0-9_.-]+)*$/.test(path)
|
|
33
|
+
&& path.split('/').every((part) => part !== '.' && part !== '..'
|
|
34
|
+
&& !['.git', '.neocortex', 'node_modules'].includes(part.toLowerCase()));
|
|
35
|
+
}
|
|
36
|
+
function ordered(paths) {
|
|
37
|
+
return [...paths].sort((a, b) => Buffer.from(a).compare(Buffer.from(b)));
|
|
38
|
+
}
|
|
39
|
+
function requireExplicitJsModuleType(root, path, dependencies) {
|
|
40
|
+
if (!path.endsWith('.js') && !path.endsWith('.ts'))
|
|
41
|
+
return;
|
|
42
|
+
let directory = dirname(path);
|
|
43
|
+
while (true) {
|
|
44
|
+
const manifest = directory === '.' ? 'package.json' : `${directory}/package.json`;
|
|
45
|
+
if (existsSync(join(root, manifest))) {
|
|
46
|
+
if (!dependencies.includes(manifest))
|
|
47
|
+
throw new Error('refactor-root-tests-manifest-unbound');
|
|
48
|
+
let parsed;
|
|
49
|
+
try {
|
|
50
|
+
parsed = JSON.parse(physicalBytes(root, manifest).toString('utf8'));
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
throw new Error('refactor-root-tests-manifest-invalid');
|
|
54
|
+
}
|
|
55
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)
|
|
56
|
+
|| !['module', 'commonjs'].includes(parsed.type)
|
|
57
|
+
|| (path.endsWith('.ts') && parsed.type !== 'module')) {
|
|
58
|
+
throw new Error('refactor-root-tests-manifest-invalid');
|
|
59
|
+
}
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (directory === '.')
|
|
63
|
+
break;
|
|
64
|
+
directory = dirname(directory);
|
|
65
|
+
}
|
|
66
|
+
throw new Error('refactor-root-tests-manifest-missing');
|
|
67
|
+
}
|
|
68
|
+
function physicalBytes(root, path) {
|
|
69
|
+
if (!safePath(path))
|
|
70
|
+
throw new Error('refactor-root-tests-path-invalid');
|
|
71
|
+
const physicalPath = join(root, path);
|
|
72
|
+
const stat = lstatSync(physicalPath);
|
|
73
|
+
if (realpathSync(physicalPath) !== physicalPath || !stat.isFile() || stat.isSymbolicLink()
|
|
74
|
+
|| stat.size < 1 || stat.size > 8 * 1024 * 1024) {
|
|
75
|
+
throw new Error('refactor-root-tests-source-unverified');
|
|
76
|
+
}
|
|
77
|
+
const bytes = readFileSync(physicalPath);
|
|
78
|
+
if (bytes.length < 1 || bytes.length > 8 * 1024 * 1024 || bytes.includes(0)) {
|
|
79
|
+
throw new Error('refactor-root-tests-source-unverified');
|
|
80
|
+
}
|
|
81
|
+
new TextDecoder('utf-8', { fatal: true }).decode(bytes);
|
|
82
|
+
return bytes;
|
|
83
|
+
}
|
|
84
|
+
function baselinePath(root, sha256) {
|
|
85
|
+
if (!SHA256.test(sha256))
|
|
86
|
+
throw new Error('refactor-root-baseline-invalid');
|
|
87
|
+
return join(privateRefactorDirectory(root), `characterization-baseline-${sha256}.json`);
|
|
88
|
+
}
|
|
89
|
+
export function readRootRefactorBaseline(root, sha256) {
|
|
90
|
+
const stat = lstatSync(baselinePath(root, sha256));
|
|
91
|
+
if (!stat.isFile() || stat.isSymbolicLink() || stat.uid !== process.getuid?.()
|
|
92
|
+
|| (stat.mode & 0o077) !== 0 || stat.size < 100 || stat.size > 64 * 1024) {
|
|
93
|
+
throw new Error('refactor-root-baseline-invalid');
|
|
94
|
+
}
|
|
95
|
+
const raw = readFileSync(baselinePath(root, sha256));
|
|
96
|
+
if (raw.length > 64 * 1024)
|
|
97
|
+
throw new Error('refactor-root-baseline-invalid');
|
|
98
|
+
let parsed;
|
|
99
|
+
try {
|
|
100
|
+
parsed = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(raw));
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
throw new Error('refactor-root-baseline-invalid');
|
|
104
|
+
}
|
|
105
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
106
|
+
throw new Error('refactor-root-baseline-invalid');
|
|
107
|
+
}
|
|
108
|
+
const value = parsed;
|
|
109
|
+
const keys = ['schemaVersion', 'kind', 'admissionSha256', 'sourceHead', 'testFiles',
|
|
110
|
+
'dependencies', 'causalPath', 'candidateSha256', 'outputSha256',
|
|
111
|
+
'causalOutputSha256', 'baselineSha256'];
|
|
112
|
+
const filesValid = (files) => Array.isArray(files)
|
|
113
|
+
&& files.every((file) => file && typeof file === 'object' && !Array.isArray(file)
|
|
114
|
+
&& Object.keys(file).sort().join('\0') === 'path\0sha256'
|
|
115
|
+
&& typeof file.path === 'string'
|
|
116
|
+
&& typeof file.sha256 === 'string'
|
|
117
|
+
&& safePath(file.path) && SHA256.test(file.sha256));
|
|
118
|
+
const boundRunner = value.schemaVersion === 2;
|
|
119
|
+
if (Object.keys(value).sort().join('\0') !== (boundRunner
|
|
120
|
+
? [...keys, 'runner', 'toolchainSha256'] : keys).sort().join('\0')
|
|
121
|
+
|| (value.schemaVersion !== 1 && value.schemaVersion !== 2)
|
|
122
|
+
|| (boundRunner && (!['vitest', 'cpp'].includes(String(value.runner)) || !SHA256.test(String(value.toolchainSha256))))
|
|
123
|
+
|| value.kind !== 'root-refactor-characterization-baseline'
|
|
124
|
+
|| !SHA256.test(String(value.admissionSha256))
|
|
125
|
+
|| typeof value.sourceHead !== 'string' || !/^[a-f0-9]{40}(?:[a-f0-9]{24})?$/.test(value.sourceHead)
|
|
126
|
+
|| !filesValid(value.testFiles) || !filesValid(value.dependencies)
|
|
127
|
+
|| value.testFiles.length < 1 || value.testFiles.length > MAX_TESTS
|
|
128
|
+
|| value.dependencies.length > MAX_DEPENDENCIES
|
|
129
|
+
|| typeof value.causalPath !== 'string' || !safePath(value.causalPath)
|
|
130
|
+
|| !SHA256.test(String(value.candidateSha256)) || !SHA256.test(String(value.outputSha256))
|
|
131
|
+
|| !SHA256.test(String(value.causalOutputSha256))
|
|
132
|
+
|| value.baselineSha256 !== sha256)
|
|
133
|
+
throw new Error('refactor-root-baseline-invalid');
|
|
134
|
+
const tests = value.testFiles;
|
|
135
|
+
const dependencies = value.dependencies;
|
|
136
|
+
if (new Set([...tests, ...dependencies].map((file) => file.path)).size
|
|
137
|
+
!== tests.length + dependencies.length
|
|
138
|
+
|| tests.map((file) => file.path).join('\0') !== ordered(tests.map((file) => file.path)).join('\0')
|
|
139
|
+
|| dependencies.map((file) => file.path).join('\0')
|
|
140
|
+
!== ordered(dependencies.map((file) => file.path)).join('\0')) {
|
|
141
|
+
throw new Error('refactor-root-baseline-invalid');
|
|
142
|
+
}
|
|
143
|
+
const { baselineSha256: _digest, ...body } = value;
|
|
144
|
+
if (digest(JSON.stringify(body)) !== sha256
|
|
145
|
+
|| !raw.equals(Buffer.from(`${JSON.stringify(value)}\n`))) {
|
|
146
|
+
throw new Error('refactor-root-baseline-invalid');
|
|
147
|
+
}
|
|
148
|
+
return value;
|
|
149
|
+
}
|
|
150
|
+
function persistBaseline(root, body) {
|
|
151
|
+
const baselineSha256 = digest(JSON.stringify(body));
|
|
152
|
+
const path = baselinePath(root, baselineSha256);
|
|
153
|
+
const record = { ...body, baselineSha256 };
|
|
154
|
+
const bytes = Buffer.from(`${JSON.stringify(record)}\n`);
|
|
155
|
+
if (existsSync(path)) {
|
|
156
|
+
readRootRefactorBaseline(root, baselineSha256);
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
const handle = openSync(path, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL
|
|
160
|
+
| (constants.O_NOFOLLOW ?? 0), 0o600);
|
|
161
|
+
try {
|
|
162
|
+
writeFileSync(handle, bytes);
|
|
163
|
+
fsyncSync(handle);
|
|
164
|
+
}
|
|
165
|
+
finally {
|
|
166
|
+
closeSync(handle);
|
|
167
|
+
}
|
|
168
|
+
const directoryHandle = openSync(privateRefactorDirectory(root), constants.O_RDONLY | (constants.O_DIRECTORY ?? 0));
|
|
169
|
+
try {
|
|
170
|
+
fsyncSync(directoryHandle);
|
|
171
|
+
}
|
|
172
|
+
finally {
|
|
173
|
+
closeSync(directoryHandle);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
readRootRefactorBaseline(root, baselineSha256);
|
|
177
|
+
return baselineSha256;
|
|
178
|
+
}
|
|
179
|
+
function admittedPaths(record) {
|
|
180
|
+
return [...record.files.map((file) => file.path),
|
|
181
|
+
...(record.schemaVersion === 2 ? record.newFiles.map((file) => file.path) : [])];
|
|
182
|
+
}
|
|
183
|
+
function snapshot(root, record, dependencies, pendingDocumentationPaths = []) {
|
|
184
|
+
const files = new Map();
|
|
185
|
+
let totalBytes = 0;
|
|
186
|
+
for (const path of [...admittedPaths(record), ...dependencies]) {
|
|
187
|
+
if (pendingDocumentationPaths.includes(path)) {
|
|
188
|
+
try {
|
|
189
|
+
lstatSync(join(root, path));
|
|
190
|
+
}
|
|
191
|
+
catch (error) {
|
|
192
|
+
if (error.code === 'ENOENT')
|
|
193
|
+
continue;
|
|
194
|
+
throw error;
|
|
195
|
+
}
|
|
196
|
+
throw new Error('refactor-root-tests-source-stale');
|
|
197
|
+
}
|
|
198
|
+
const bytes = physicalBytes(root, path);
|
|
199
|
+
totalBytes += bytes.length;
|
|
200
|
+
if (totalBytes > MAX_RUN_BYTES)
|
|
201
|
+
throw new Error('refactor-root-tests-snapshot-too-large');
|
|
202
|
+
files.set(path, bytes);
|
|
203
|
+
}
|
|
204
|
+
const candidateSha256 = digest(JSON.stringify([
|
|
205
|
+
...[...files].map(([path, bytes]) => [path, digest(bytes)]),
|
|
206
|
+
...pendingDocumentationPaths.map(path => [path, null]),
|
|
207
|
+
]));
|
|
208
|
+
return { files, candidateSha256 };
|
|
209
|
+
}
|
|
210
|
+
function exactCppToolchain() {
|
|
211
|
+
try {
|
|
212
|
+
const executable = realpathSync('/usr/bin/g++');
|
|
213
|
+
const stat = lstatSync(executable);
|
|
214
|
+
if (!executable.startsWith('/usr/') || !/^[A-Za-z0-9_./+-]+$/.test(executable)
|
|
215
|
+
|| !stat.isFile() || stat.uid !== 0 || (stat.mode & 0o022) !== 0
|
|
216
|
+
|| stat.size < 1 || stat.size > 32 * 1024 * 1024)
|
|
217
|
+
throw new Error('invalid compiler');
|
|
218
|
+
const result = spawnSync(executable, ['-dumpfullversion', '-dumpversion'], {
|
|
219
|
+
env: { PATH: '/usr/bin:/bin' }, encoding: 'utf8', timeout: 10_000, maxBuffer: 4096,
|
|
220
|
+
});
|
|
221
|
+
const version = result.stdout?.trim();
|
|
222
|
+
if (result.status !== 0 || !/^(?:11|12|13|14|15)\.[0-9]+\.[0-9]+$/.test(version)) {
|
|
223
|
+
throw new Error('unsupported compiler');
|
|
224
|
+
}
|
|
225
|
+
return { executable, sha256: digest(JSON.stringify([executable, version,
|
|
226
|
+
digest(readFileSync(executable))])) };
|
|
227
|
+
}
|
|
228
|
+
catch {
|
|
229
|
+
throw new Error('refactor-root-tests-toolchain-unverified');
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
/** Bind an installed runner to the committed manifest and lock, never run npm or project scripts. */
|
|
233
|
+
function exactVitestToolchain(root, tests, dependencies) {
|
|
234
|
+
let packagePath = '';
|
|
235
|
+
for (const test of tests) {
|
|
236
|
+
let directory = dirname(test);
|
|
237
|
+
let nearest = '';
|
|
238
|
+
while (true) {
|
|
239
|
+
const candidate = directory === '.' ? 'package.json' : `${directory}/package.json`;
|
|
240
|
+
if (existsSync(join(root, candidate))) {
|
|
241
|
+
nearest = candidate;
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
if (directory === '.')
|
|
245
|
+
break;
|
|
246
|
+
directory = dirname(directory);
|
|
247
|
+
}
|
|
248
|
+
if (!nearest || packagePath && nearest !== packagePath) {
|
|
249
|
+
throw new Error('refactor-root-tests-toolchain-unverified');
|
|
250
|
+
}
|
|
251
|
+
packagePath = nearest;
|
|
252
|
+
}
|
|
253
|
+
if (!dependencies.includes(packagePath) || !dependencies.includes('package-lock.json')) {
|
|
254
|
+
throw new Error('refactor-root-tests-toolchain-unbound');
|
|
255
|
+
}
|
|
256
|
+
const manifestBytes = physicalBytes(root, packagePath);
|
|
257
|
+
const lockBytes = physicalBytes(root, 'package-lock.json');
|
|
258
|
+
let manifest;
|
|
259
|
+
let lock;
|
|
260
|
+
try {
|
|
261
|
+
manifest = JSON.parse(manifestBytes.toString('utf8'));
|
|
262
|
+
lock = JSON.parse(lockBytes.toString('utf8'));
|
|
263
|
+
}
|
|
264
|
+
catch {
|
|
265
|
+
throw new Error('refactor-root-tests-toolchain-unverified');
|
|
266
|
+
}
|
|
267
|
+
const declared = manifest.devDependencies?.vitest
|
|
268
|
+
?? manifest.dependencies?.vitest;
|
|
269
|
+
const packages = lock.packages;
|
|
270
|
+
const lockWorkspace = packagePath === 'package.json' ? '' : dirname(packagePath);
|
|
271
|
+
const lockedPackage = packages?.[lockWorkspace];
|
|
272
|
+
const lockedRequest = lockedPackage?.devDependencies?.vitest
|
|
273
|
+
?? lockedPackage?.dependencies?.vitest;
|
|
274
|
+
const lockedVersion = packages?.['node_modules/vitest']?.version;
|
|
275
|
+
const lockedViteVersion = packages?.['node_modules/vite']?.version;
|
|
276
|
+
if (typeof declared !== 'string' || declared.length > 80 || !/^[\^~]?[0-9]+\.[0-9]+\.[0-9]+$/.test(declared)
|
|
277
|
+
|| declared !== lockedRequest || typeof lockedVersion !== 'string'
|
|
278
|
+
|| !/^[0-9]+\.[0-9]+\.[0-9]+$/.test(lockedVersion)
|
|
279
|
+
|| !lockedVersion.startsWith('3.2.')
|
|
280
|
+
|| typeof lockedViteVersion !== 'string'
|
|
281
|
+
|| !/^[0-9]+\.[0-9]+\.[0-9]+$/.test(lockedViteVersion)
|
|
282
|
+
|| !supportsViteRunnerLoader(lockedViteVersion)) {
|
|
283
|
+
throw new Error('refactor-root-tests-toolchain-unverified');
|
|
284
|
+
}
|
|
285
|
+
const nodeModules = realpathSync(join(root, 'node_modules'));
|
|
286
|
+
const modulesStat = lstatSync(nodeModules);
|
|
287
|
+
if (basename(nodeModules) !== 'node_modules' || !modulesStat.isDirectory()
|
|
288
|
+
|| modulesStat.isSymbolicLink() || modulesStat.uid !== process.getuid?.()) {
|
|
289
|
+
throw new Error('refactor-root-tests-toolchain-unverified');
|
|
290
|
+
}
|
|
291
|
+
const installedPath = join(nodeModules, 'vitest', 'package.json');
|
|
292
|
+
const entryPath = join(nodeModules, 'vitest', 'vitest.mjs');
|
|
293
|
+
const vitePath = join(nodeModules, 'vite', 'package.json');
|
|
294
|
+
const installedStat = lstatSync(installedPath);
|
|
295
|
+
const entryStat = lstatSync(entryPath);
|
|
296
|
+
const viteStat = lstatSync(vitePath);
|
|
297
|
+
if (!installedStat.isFile() || installedStat.isSymbolicLink()
|
|
298
|
+
|| !entryStat.isFile() || entryStat.isSymbolicLink()
|
|
299
|
+
|| !viteStat.isFile() || viteStat.isSymbolicLink()
|
|
300
|
+
|| realpathSync(installedPath) !== installedPath || realpathSync(entryPath) !== entryPath
|
|
301
|
+
|| realpathSync(vitePath) !== vitePath
|
|
302
|
+
|| installedStat.size > 64 * 1024 || entryStat.size > 1024 * 1024
|
|
303
|
+
|| viteStat.size > 64 * 1024) {
|
|
304
|
+
throw new Error('refactor-root-tests-toolchain-unverified');
|
|
305
|
+
}
|
|
306
|
+
const installedBytes = readFileSync(installedPath);
|
|
307
|
+
const entryBytes = readFileSync(entryPath);
|
|
308
|
+
const viteBytes = readFileSync(vitePath);
|
|
309
|
+
let installed;
|
|
310
|
+
let vite;
|
|
311
|
+
try {
|
|
312
|
+
installed = JSON.parse(installedBytes.toString('utf8'));
|
|
313
|
+
vite = JSON.parse(viteBytes.toString('utf8'));
|
|
314
|
+
}
|
|
315
|
+
catch {
|
|
316
|
+
throw new Error('refactor-root-tests-toolchain-unverified');
|
|
317
|
+
}
|
|
318
|
+
if (installed.name !== 'vitest' || installed.version !== lockedVersion
|
|
319
|
+
|| vite.name !== 'vite' || vite.version !== lockedViteVersion) {
|
|
320
|
+
throw new Error('refactor-root-tests-toolchain-unverified');
|
|
321
|
+
}
|
|
322
|
+
return { nodeModules, sha256: digest(JSON.stringify([
|
|
323
|
+
digest(manifestBytes), digest(lockBytes), digest(installedBytes), digest(entryBytes),
|
|
324
|
+
digest(viteBytes),
|
|
325
|
+
])) };
|
|
326
|
+
}
|
|
327
|
+
function supportsViteRunnerLoader(version) {
|
|
328
|
+
const match = version.match(/^(\d+)\.(\d+)\.(\d+)$/);
|
|
329
|
+
return !!match && Number(match[1]) <= 7 && (Number(match[1]) > 6
|
|
330
|
+
|| Number(match[1]) === 6 && Number(match[2]) >= 1);
|
|
331
|
+
}
|
|
332
|
+
function sandboxCharacterizationTests(files, tests, runtime, canary, toolchain, cppToolchain, causalPath) {
|
|
333
|
+
if (runtime === 'python') {
|
|
334
|
+
const directory = dirname(tests[0]);
|
|
335
|
+
const discoverable = ordered([...files.keys()].filter((path) => (directory === '.' || path.startsWith(`${directory}/`))
|
|
336
|
+
&& /(?:^|\/)test_[A-Za-z0-9_]+\.py$/.test(path)));
|
|
337
|
+
if (JSON.stringify(discoverable) !== JSON.stringify(tests)) {
|
|
338
|
+
throw new Error('refactor-root-tests-path-invalid');
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
const executable = realpathSync(runtime === 'cpp' ? '/usr/bin/dash'
|
|
342
|
+
: runtime === 'python' ? '/usr/bin/python3' : process.execPath);
|
|
343
|
+
if (runtime === 'node-typescript' && !supportsNativeTypeStrippingRuntime()) {
|
|
344
|
+
throw new Error('refactor-root-tests-toolchain-unavailable');
|
|
345
|
+
}
|
|
346
|
+
if (process.platform !== 'linux' || !existsSync('/usr/bin/bwrap')
|
|
347
|
+
|| !lstatSync('/usr/bin/bwrap').isFile()
|
|
348
|
+
|| !(executable.startsWith('/usr/') || executable.startsWith('/home/linuxbrew/.linuxbrew/'))) {
|
|
349
|
+
throw new Error('refactor-root-tests-sandbox-unavailable');
|
|
350
|
+
}
|
|
351
|
+
const scratch = mkdtempSync(join(tmpdir(), 'p222-refactor-characterization-'));
|
|
352
|
+
try {
|
|
353
|
+
const work = join(scratch, 'work');
|
|
354
|
+
mkdirSync(work, { mode: 0o700 });
|
|
355
|
+
for (const [path, bytes] of files) {
|
|
356
|
+
const target = join(work, path);
|
|
357
|
+
mkdirSync(dirname(target), { recursive: true, mode: 0o700 });
|
|
358
|
+
writeFileSync(target, bytes, { mode: 0o600, flag: 'wx' });
|
|
359
|
+
}
|
|
360
|
+
if (runtime === 'vitest') {
|
|
361
|
+
if (!toolchain)
|
|
362
|
+
throw new Error('refactor-root-tests-toolchain-unavailable');
|
|
363
|
+
mkdirSync(join(work, 'node_modules'), { mode: 0o700 });
|
|
364
|
+
writeFileSync(join(work, 'p222-vitest.config.mjs'), `export default ${JSON.stringify({ test: { include: tests, environment: 'node',
|
|
365
|
+
silent: true, passWithNoTests: false, watch: false, maxWorkers: 1,
|
|
366
|
+
fileParallelism: false, pool: 'forks', coverage: { enabled: false } } })};\n`, { mode: 0o600, flag: 'wx' });
|
|
367
|
+
}
|
|
368
|
+
if (runtime === 'cpp') {
|
|
369
|
+
if (!cppToolchain || !causalPath || !files.has(causalPath)) {
|
|
370
|
+
throw new Error('refactor-root-tests-toolchain-unavailable');
|
|
371
|
+
}
|
|
372
|
+
const compiler = cppToolchain.executable;
|
|
373
|
+
const sources = ordered([...files.keys()].filter(path => path.endsWith('.cpp')
|
|
374
|
+
&& path !== causalPath && !tests.includes(path))).map(path => `'/work/${path}'`);
|
|
375
|
+
if (canary)
|
|
376
|
+
writeFileSync(join(work, 'p222-cpp-causal.cpp'), '#include <cstdio>\n#include <cstdlib>\n'
|
|
377
|
+
+ `extern "C" void __cyg_profile_func_enter(void*,void*) { std::fputs(${JSON.stringify(canary + '\n')},stderr); std::_Exit(86); }\n`
|
|
378
|
+
+ 'extern "C" void __cyg_profile_func_exit(void*,void*) {}\n', { mode: 0o600, flag: 'wx' });
|
|
379
|
+
const script = ['set -eu', 'ulimit -c 0',
|
|
380
|
+
`'${compiler}' -std=c++17 -O0 -UNDEBUG -I/work ${canary
|
|
381
|
+
? '-finstrument-functions -finstrument-functions-exclude-file-list=/usr/include ' : ''}`
|
|
382
|
+
+ `-c '/work/${causalPath}' -o /tmp/p222-cpp-causal.o`,
|
|
383
|
+
...tests.flatMap((test, index) => [
|
|
384
|
+
`'${compiler}' -std=c++17 -O0 -UNDEBUG -I/work '/work/${test}' ${sources.join(' ')} `
|
|
385
|
+
+ `/tmp/p222-cpp-causal.o ${canary ? '/work/p222-cpp-causal.cpp ' : ''}-o /tmp/p222-cpp-test-${index}`,
|
|
386
|
+
`/tmp/p222-cpp-test-${index}`,
|
|
387
|
+
`printf 'P222_CPP_TEST_PASSED_${index}\\n'`,
|
|
388
|
+
]),
|
|
389
|
+
].join('\n') + '\n';
|
|
390
|
+
writeFileSync(join(work, 'p222-cpp-runner.sh'), script, { mode: 0o600, flag: 'wx' });
|
|
391
|
+
}
|
|
392
|
+
const etc = join(scratch, 'etc');
|
|
393
|
+
mkdirSync(etc, { mode: 0o700 });
|
|
394
|
+
writeFileSync(join(etc, 'passwd'), 'nobody:x:65534:65534:nobody:/tmp:/bin/sh\n', { mode: 0o600 });
|
|
395
|
+
writeFileSync(join(etc, 'group'), 'nogroup:x:65534:\n', { mode: 0o600 });
|
|
396
|
+
writeFileSync(join(etc, 'hosts'), '127.0.0.1 localhost\n', { mode: 0o600 });
|
|
397
|
+
writeFileSync(join(etc, 'nsswitch.conf'), 'hosts: files\n', { mode: 0o600 });
|
|
398
|
+
const args = ['--die-with-parent', '--new-session', '--unshare-all', '--clearenv',
|
|
399
|
+
'--setenv', 'HOME', '/tmp', '--setenv', 'TMPDIR', '/tmp',
|
|
400
|
+
'--setenv', 'PATH', '/home/linuxbrew/.linuxbrew/bin:/usr/bin:/bin',
|
|
401
|
+
'--setenv', 'CI', 'true', '--ro-bind', '/usr', '/usr'];
|
|
402
|
+
if (runtime === 'python')
|
|
403
|
+
args.push('--setenv', 'PYTHONPATH', '/work');
|
|
404
|
+
for (const path of ['/lib', '/lib64', '/bin']) {
|
|
405
|
+
if (existsSync(path))
|
|
406
|
+
args.push('--ro-bind', path, path);
|
|
407
|
+
}
|
|
408
|
+
if (existsSync('/home/linuxbrew/.linuxbrew')) {
|
|
409
|
+
args.push('--ro-bind', '/home/linuxbrew/.linuxbrew', '/home/linuxbrew/.linuxbrew');
|
|
410
|
+
}
|
|
411
|
+
args.push('--dir', '/etc');
|
|
412
|
+
for (const file of ['passwd', 'group', 'hosts', 'nsswitch.conf']) {
|
|
413
|
+
args.push('--ro-bind', join(etc, file), `/etc/${file}`);
|
|
414
|
+
}
|
|
415
|
+
args.push('--dev', '/dev', '--proc', '/proc', '--tmpfs', '/tmp', '--ro-bind', work, '/work');
|
|
416
|
+
if (runtime === 'vitest')
|
|
417
|
+
args.push('--ro-bind', toolchain.nodeModules, '/work/node_modules');
|
|
418
|
+
args.push('--chdir', '/work', '--', executable);
|
|
419
|
+
if (runtime === 'cpp') {
|
|
420
|
+
args.push('/work/p222-cpp-runner.sh');
|
|
421
|
+
}
|
|
422
|
+
else if (runtime === 'python') {
|
|
423
|
+
// A literal discovery directory and fixed pattern run only the admitted
|
|
424
|
+
// test files copied into the read-only snapshot. No project runner,
|
|
425
|
+
// site-packages or shell-controlled command line enters this boundary.
|
|
426
|
+
args.push('-B', '-S', '-m', 'unittest', 'discover', '-s', dirname(tests[0]), '-p', 'test_*.py');
|
|
427
|
+
}
|
|
428
|
+
else if (runtime === 'vitest') {
|
|
429
|
+
args.push('/work/node_modules/vitest/vitest.mjs', 'run', '--root', '/work', '--config', '/work/p222-vitest.config.mjs', '--configLoader=runner', '--reporter=json', '--no-cache', '--no-file-parallelism', '--maxWorkers=1');
|
|
430
|
+
}
|
|
431
|
+
else {
|
|
432
|
+
if (runtime === 'node-typescript')
|
|
433
|
+
args.push('--experimental-strip-types');
|
|
434
|
+
args.push('--test', '--test-reporter=tap', ...tests);
|
|
435
|
+
}
|
|
436
|
+
const result = spawnSync('/usr/bin/bwrap', args, {
|
|
437
|
+
env: { PATH: '/usr/bin:/bin' }, encoding: 'buffer',
|
|
438
|
+
timeout: 180_000, maxBuffer: 4 * 1024 * 1024,
|
|
439
|
+
});
|
|
440
|
+
if (result.error || result.signal || result.status === null) {
|
|
441
|
+
throw new Error('refactor-root-tests-sandbox-unavailable');
|
|
442
|
+
}
|
|
443
|
+
const output = Buffer.concat([result.stdout ?? Buffer.alloc(0), result.stderr ?? Buffer.alloc(0)]);
|
|
444
|
+
const pythonTestsObserved = runtime !== 'python'
|
|
445
|
+
|| /^Ran [1-9][0-9]* tests? in /m.test(output.toString('utf8'));
|
|
446
|
+
let vitestTestsObserved = true;
|
|
447
|
+
if (runtime === 'vitest') {
|
|
448
|
+
try {
|
|
449
|
+
const report = JSON.parse((result.stdout ?? Buffer.alloc(0)).toString('utf8'));
|
|
450
|
+
vitestTestsObserved = Number.isSafeInteger(report.numPassedTests)
|
|
451
|
+
&& Number(report.numPassedTests) > 0
|
|
452
|
+
&& report.numTotalTests === report.numPassedTests
|
|
453
|
+
&& report.numFailedTests === 0;
|
|
454
|
+
}
|
|
455
|
+
catch {
|
|
456
|
+
vitestTestsObserved = false;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
const cppTestsObserved = runtime !== 'cpp' || tests.every((_, index) => output.toString('utf8').split('\n').includes(`P222_CPP_TEST_PASSED_${index}`));
|
|
460
|
+
return { passed: result.status === 0 && pythonTestsObserved && vitestTestsObserved && cppTestsObserved,
|
|
461
|
+
outputSha256: digest(output),
|
|
462
|
+
canaryObserved: canary !== undefined && (runtime === 'cpp'
|
|
463
|
+
? output.toString('utf8').split('\n').includes(canary) : output.includes(Buffer.from(canary))) };
|
|
464
|
+
}
|
|
465
|
+
finally {
|
|
466
|
+
rmSync(scratch, { recursive: true, force: true });
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
export function runRootRefactorCharacterization(projectRoot, admissionSha256, testPaths, dependencyPaths, phase, authorizeRun, causalPath, baselineSha256, runner = 'native') {
|
|
470
|
+
if (!authorizeRun)
|
|
471
|
+
throw new Error('refactor-root-run-authorization-required');
|
|
472
|
+
if (testPaths.length < 1 || testPaths.length > MAX_TESTS || dependencyPaths.length > MAX_DEPENDENCIES
|
|
473
|
+
|| !['native', 'vitest', 'cpp'].includes(runner)
|
|
474
|
+
|| (phase === 'baseline' && baselineSha256 !== undefined)
|
|
475
|
+
|| (phase === 'after' && (!baselineSha256 || !SHA256.test(baselineSha256)))) {
|
|
476
|
+
throw new Error('refactor-root-tests-arguments-invalid');
|
|
477
|
+
}
|
|
478
|
+
const tests = ordered(testPaths);
|
|
479
|
+
const dependencies = ordered(dependencyPaths);
|
|
480
|
+
const runtime = runner === 'cpp' ? 'cpp' : runner === 'vitest' ? 'vitest'
|
|
481
|
+
: tests.every((path) => /(?:^|\/)test_[A-Za-z0-9_]+\.py$/.test(path))
|
|
482
|
+
&& causalPath.endsWith('.py') ? 'python'
|
|
483
|
+
: tests.every((path) => path.endsWith('.test.ts')) && causalPath.endsWith('.ts')
|
|
484
|
+
? 'node-typescript' : 'node';
|
|
485
|
+
if (new Set([...tests, ...dependencies]).size !== tests.length + dependencies.length
|
|
486
|
+
|| tests.some((path) => !safePath(path) || (runtime === 'cpp' ? !path.endsWith('.test.cpp') : runtime === 'python'
|
|
487
|
+
? !/(?:^|\/)test_[A-Za-z0-9_]+\.py$/.test(path) || dirname(path) !== dirname(tests[0])
|
|
488
|
+
: runtime === 'vitest' ? !/\.test\.(?:ts|js|mjs)$/.test(path)
|
|
489
|
+
: runtime === 'node-typescript' ? !path.endsWith('.test.ts')
|
|
490
|
+
: !/\.test\.(?:mjs|cjs|js)$/.test(path)))
|
|
491
|
+
|| !safePath(causalPath) || (runtime === 'cpp'
|
|
492
|
+
? !causalPath.endsWith('.cpp') || causalPath.endsWith('.test.cpp') : runtime === 'python'
|
|
493
|
+
? !causalPath.endsWith('.py') : runtime === 'vitest'
|
|
494
|
+
? !/\.(?:ts|js|mjs)$/.test(causalPath) || /\.test\./.test(causalPath)
|
|
495
|
+
: runtime === 'node-typescript'
|
|
496
|
+
? !causalPath.endsWith('.ts') || causalPath.endsWith('.test.ts')
|
|
497
|
+
: !/\.(?:mjs|cjs|js)$/.test(causalPath))
|
|
498
|
+
|| dependencies.some((path) => !safePath(path) || runtime === 'python'
|
|
499
|
+
&& /(?:^|\/)test_[A-Za-z0-9_]+\.py$/.test(path))) {
|
|
500
|
+
throw new Error('refactor-root-tests-path-invalid');
|
|
501
|
+
}
|
|
502
|
+
const root = resolve(projectRoot);
|
|
503
|
+
try {
|
|
504
|
+
const record = readPrivateRefactorAdmission(root, admissionSha256);
|
|
505
|
+
const { pendingDocumentationPaths } = verifyRootRefactorForCharacterization(root, admissionSha256);
|
|
506
|
+
const admitted = new Set(admittedPaths(record));
|
|
507
|
+
if (tests.some((path) => !admitted.has(path))
|
|
508
|
+
|| !record.files.some((file) => file.path === causalPath)
|
|
509
|
+
|| tests.includes(causalPath)
|
|
510
|
+
|| dependencies.some((path) => admitted.has(path))) {
|
|
511
|
+
throw new Error('refactor-root-tests-path-invalid');
|
|
512
|
+
}
|
|
513
|
+
for (const path of [...tests, causalPath, ...dependencies]) {
|
|
514
|
+
requireExplicitJsModuleType(root, path, dependencies);
|
|
515
|
+
}
|
|
516
|
+
for (const path of dependencies)
|
|
517
|
+
exactCommittedRefactorFile(root, path);
|
|
518
|
+
const toolchain = runtime === 'vitest'
|
|
519
|
+
? exactVitestToolchain(root, tests, dependencies) : undefined;
|
|
520
|
+
const cppToolchain = runtime === 'cpp' ? exactCppToolchain() : undefined;
|
|
521
|
+
const toolchainSha256 = toolchain?.sha256 ?? cppToolchain?.sha256;
|
|
522
|
+
const changed = mutationQaGit(root, ['diff', '--name-only', '-z', '--no-ext-diff', '--no-textconv'])
|
|
523
|
+
.toString('utf8').split('\0').filter((path) => path && !path.startsWith('.neocortex/'));
|
|
524
|
+
const untracked = mutationQaGit(root, ['ls-files', '--others', '--exclude-standard', '-z'])
|
|
525
|
+
.toString('utf8').split('\0').filter((path) => path && !path.startsWith('.neocortex/'));
|
|
526
|
+
const changedOutsideTests = [...changed, ...untracked].filter((path) => !tests.includes(path));
|
|
527
|
+
if ((phase === 'baseline' && changedOutsideTests.length > 0)
|
|
528
|
+
|| (phase === 'after' && changedOutsideTests.length === 0)) {
|
|
529
|
+
throw new Error('refactor-root-tests-phase-invalid');
|
|
530
|
+
}
|
|
531
|
+
const testFiles = tests.map((path) => ({ path, sha256: digest(physicalBytes(root, path)) }));
|
|
532
|
+
const dependencyFiles = dependencies.map((path) => ({ path, sha256: digest(physicalBytes(root, path)) }));
|
|
533
|
+
const baseline = phase === 'after' ? readRootRefactorBaseline(root, baselineSha256) : null;
|
|
534
|
+
if (baseline && (baseline.admissionSha256 !== admissionSha256
|
|
535
|
+
|| baseline.sourceHead !== record.sourceHead
|
|
536
|
+
|| baseline.causalPath !== causalPath
|
|
537
|
+
|| (baseline.schemaVersion === 2 ? runner !== baseline.runner
|
|
538
|
+
|| baseline.toolchainSha256 !== toolchainSha256 : runner !== 'native')
|
|
539
|
+
|| JSON.stringify(baseline.testFiles) !== JSON.stringify(testFiles)
|
|
540
|
+
|| JSON.stringify(baseline.dependencies) !== JSON.stringify(dependencyFiles))) {
|
|
541
|
+
throw new Error('refactor-root-baseline-stale');
|
|
542
|
+
}
|
|
543
|
+
const before = snapshot(root, record, dependencies, pendingDocumentationPaths);
|
|
544
|
+
if (baseline && digest(before.files.get(causalPath))
|
|
545
|
+
=== record.files.find((file) => file.path === causalPath).sha256) {
|
|
546
|
+
throw new Error('refactor-root-tests-phase-invalid');
|
|
547
|
+
}
|
|
548
|
+
const execution = sandboxCharacterizationTests(before.files, tests, runtime, undefined, toolchain, cppToolchain, causalPath);
|
|
549
|
+
let causalOutputSha256 = null;
|
|
550
|
+
let causalFailureObserved = false;
|
|
551
|
+
if (execution.passed) {
|
|
552
|
+
const canary = `P222_REFACTOR_CAUSAL_${randomBytes(16).toString('hex')}`;
|
|
553
|
+
const causalFiles = new Map(before.files);
|
|
554
|
+
if (runtime !== 'cpp')
|
|
555
|
+
causalFiles.set(causalPath, Buffer.concat([
|
|
556
|
+
Buffer.from(runtime === 'python'
|
|
557
|
+
? `raise RuntimeError(${JSON.stringify(canary)})\n`
|
|
558
|
+
: `throw new Error(${JSON.stringify(canary)});\n`),
|
|
559
|
+
before.files.get(causalPath),
|
|
560
|
+
]));
|
|
561
|
+
const causal = sandboxCharacterizationTests(causalFiles, tests, runtime, canary, toolchain, cppToolchain, causalPath);
|
|
562
|
+
causalOutputSha256 = causal.outputSha256;
|
|
563
|
+
causalFailureObserved = !causal.passed && causal.canaryObserved;
|
|
564
|
+
}
|
|
565
|
+
const revalidated = verifyRootRefactorForCharacterization(root, admissionSha256);
|
|
566
|
+
if (JSON.stringify(revalidated.pendingDocumentationPaths) !== JSON.stringify(pendingDocumentationPaths)) {
|
|
567
|
+
throw new Error('refactor-root-tests-source-stale');
|
|
568
|
+
}
|
|
569
|
+
const after = snapshot(root, record, dependencies, pendingDocumentationPaths);
|
|
570
|
+
if (cppToolchain && exactCppToolchain().sha256 !== cppToolchain.sha256) {
|
|
571
|
+
throw new Error('refactor-root-tests-toolchain-stale');
|
|
572
|
+
}
|
|
573
|
+
if (before.candidateSha256 !== after.candidateSha256) {
|
|
574
|
+
throw new Error('refactor-root-tests-source-stale');
|
|
575
|
+
}
|
|
576
|
+
let savedBaseline = baselineSha256 ?? null;
|
|
577
|
+
if (phase === 'baseline' && execution.passed && causalFailureObserved && causalOutputSha256) {
|
|
578
|
+
savedBaseline = persistBaseline(root, {
|
|
579
|
+
...(runtime === 'vitest' || runtime === 'cpp'
|
|
580
|
+
? { schemaVersion: 2, runner: runtime,
|
|
581
|
+
toolchainSha256: toolchainSha256 }
|
|
582
|
+
: { schemaVersion: 1 }),
|
|
583
|
+
kind: 'root-refactor-characterization-baseline',
|
|
584
|
+
admissionSha256, sourceHead: record.sourceHead,
|
|
585
|
+
testFiles, dependencies: dependencyFiles, causalPath,
|
|
586
|
+
candidateSha256: before.candidateSha256,
|
|
587
|
+
outputSha256: execution.outputSha256, causalOutputSha256,
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
return Object.freeze({ schemaVersion: 1, kind: 'root-refactor-characterization-observation',
|
|
591
|
+
phase, admissionSha256, baselineSha256: savedBaseline,
|
|
592
|
+
candidateSha256: before.candidateSha256, outputSha256: execution.outputSha256,
|
|
593
|
+
causalOutputSha256,
|
|
594
|
+
testFileCount: tests.length, dependencyCount: dependencies.length,
|
|
595
|
+
testsPassedObserved: execution.passed, causalFailureObserved, qaAccepted: false,
|
|
596
|
+
gitCommitAuthorized: false, publicSafe: true });
|
|
597
|
+
}
|
|
598
|
+
catch (error) {
|
|
599
|
+
if (error instanceof Error && error.message.startsWith('refactor-root-'))
|
|
600
|
+
throw error;
|
|
601
|
+
throw new Error('refactor-root-tests-unverified');
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
export function refactorCharacterizationCliHandler(args) {
|
|
605
|
+
let projectRoot = '';
|
|
606
|
+
let admissionSha256 = '';
|
|
607
|
+
let baselineSha256;
|
|
608
|
+
let phase = '';
|
|
609
|
+
let authorizeRun = false;
|
|
610
|
+
let causalPath = '';
|
|
611
|
+
let runner = 'native';
|
|
612
|
+
let format = 'plain';
|
|
613
|
+
const tests = [];
|
|
614
|
+
const dependencies = [];
|
|
615
|
+
try {
|
|
616
|
+
if (args[0] !== 'characterize')
|
|
617
|
+
throw new Error('refactor-root-tests-arguments-invalid');
|
|
618
|
+
for (let index = 1; index < args.length; index++) {
|
|
619
|
+
const token = args[index];
|
|
620
|
+
const next = () => {
|
|
621
|
+
const value = args[++index];
|
|
622
|
+
if (!value || value.startsWith('--'))
|
|
623
|
+
throw new Error('refactor-root-tests-arguments-invalid');
|
|
624
|
+
return value;
|
|
625
|
+
};
|
|
626
|
+
if (token === '--project-root' && !projectRoot)
|
|
627
|
+
projectRoot = next();
|
|
628
|
+
else if (token === '--admission-sha' && !admissionSha256)
|
|
629
|
+
admissionSha256 = next();
|
|
630
|
+
else if (token === '--baseline-sha' && !baselineSha256)
|
|
631
|
+
baselineSha256 = next();
|
|
632
|
+
else if (token === '--phase' && !phase) {
|
|
633
|
+
const value = next();
|
|
634
|
+
if (value !== 'baseline' && value !== 'after')
|
|
635
|
+
throw new Error('refactor-root-tests-arguments-invalid');
|
|
636
|
+
phase = value;
|
|
637
|
+
}
|
|
638
|
+
else if (token === '--test-path')
|
|
639
|
+
tests.push(next());
|
|
640
|
+
else if (token === '--dependency')
|
|
641
|
+
dependencies.push(next());
|
|
642
|
+
else if (token === '--causal-path' && !causalPath)
|
|
643
|
+
causalPath = next();
|
|
644
|
+
else if (token === '--runner' && runner === 'native') {
|
|
645
|
+
const value = next();
|
|
646
|
+
if (value !== 'vitest' && value !== 'cpp')
|
|
647
|
+
throw new Error('refactor-root-tests-arguments-invalid');
|
|
648
|
+
runner = value;
|
|
649
|
+
}
|
|
650
|
+
else if (token === '--authorize-run' && !authorizeRun)
|
|
651
|
+
authorizeRun = true;
|
|
652
|
+
else if (token === '--format' && format === 'plain')
|
|
653
|
+
format = next();
|
|
654
|
+
else
|
|
655
|
+
throw new Error('refactor-root-tests-arguments-invalid');
|
|
656
|
+
}
|
|
657
|
+
if (!projectRoot || !admissionSha256 || !phase || !causalPath || !['plain', 'json'].includes(format)) {
|
|
658
|
+
throw new Error('refactor-root-tests-arguments-invalid');
|
|
659
|
+
}
|
|
660
|
+
const observation = runRootRefactorCharacterization(projectRoot, admissionSha256, tests, dependencies, phase, authorizeRun, causalPath, baselineSha256, runner);
|
|
661
|
+
const ok = observation.testsPassedObserved && observation.causalFailureObserved;
|
|
662
|
+
process.stdout.write(`${JSON.stringify({ ok, observation })}\n`);
|
|
663
|
+
return ok ? 0 : 1;
|
|
664
|
+
}
|
|
665
|
+
catch (error) {
|
|
666
|
+
const reasonCode = error instanceof Error && /^refactor-root-[a-z-]+$/.test(error.message)
|
|
667
|
+
? error.message : 'refactor-root-tests-unverified';
|
|
668
|
+
process.stdout.write(`${JSON.stringify({ ok: false, reasonCode })}\n`);
|
|
669
|
+
return 1;
|
|
670
|
+
}
|
|
671
|
+
}
|