@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,7 +1,588 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
/** Root-owned refactor scope and physical containment. No private child or Git grant. */
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { closeSync, constants, existsSync, fstatSync, fsyncSync, lstatSync, mkdirSync, openSync, readFileSync, readSync, realpathSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import { dirname, isAbsolute, join, resolve } from 'node:path';
|
|
5
|
+
import { mutationQaGit } from './mutation-qa-git.js';
|
|
6
|
+
const STORY_ID = /^P[0-9]{1,12}(?:\.[0-9]{1,12}){1,3}$/;
|
|
7
|
+
const SHA256 = /^[a-f0-9]{64}$/;
|
|
8
|
+
const MAX_FILE_BYTES = 8 * 1024 * 1024;
|
|
9
|
+
const MAX_PATHS = 256;
|
|
10
|
+
function digest(bytes) {
|
|
11
|
+
return createHash('sha256').update(bytes).digest('hex');
|
|
12
|
+
}
|
|
13
|
+
export function privateRefactorDirectory(root) {
|
|
14
|
+
const commonRaw = mutationQaGit(root, ['rev-parse', '--git-common-dir']).toString('utf8').trim();
|
|
15
|
+
const common = isAbsolute(commonRaw) ? resolve(commonRaw) : resolve(root, commonRaw);
|
|
16
|
+
if (realpathSync(common) !== common || lstatSync(common).isSymbolicLink()
|
|
17
|
+
|| lstatSync(common).uid !== process.getuid?.())
|
|
18
|
+
throw new Error('refactor-root-store-unverified');
|
|
19
|
+
const directory = join(common, 'neocortex-refactor');
|
|
20
|
+
mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
21
|
+
const stat = lstatSync(directory);
|
|
22
|
+
if (!stat.isDirectory() || stat.isSymbolicLink() || stat.uid !== process.getuid?.()
|
|
23
|
+
|| (stat.mode & 0o077) !== 0)
|
|
24
|
+
throw new Error('refactor-root-store-unverified');
|
|
25
|
+
return directory;
|
|
26
|
+
}
|
|
27
|
+
function pathIsSafe(path) {
|
|
28
|
+
if (!path || path.length > 512 || isAbsolute(path) || path.includes('\\')
|
|
29
|
+
|| /[\u0000-\u001f\u007f:*?\[\]]/.test(path) || path.normalize('NFC') !== path)
|
|
30
|
+
return false;
|
|
31
|
+
const parts = path.split('/');
|
|
32
|
+
return !parts.some((part) => !part || part === '.' || part === '..'
|
|
33
|
+
|| ['.git', '.neocortex', 'node_modules'].includes(part.toLowerCase()));
|
|
34
|
+
}
|
|
35
|
+
export function exactCommittedRefactorFile(root, path) {
|
|
36
|
+
if (!pathIsSafe(path))
|
|
37
|
+
throw new Error('refactor-root-write-set-invalid');
|
|
38
|
+
const physicalPath = join(root, path);
|
|
39
|
+
const stat = lstatSync(physicalPath);
|
|
40
|
+
if (realpathSync(physicalPath) !== physicalPath || !stat.isFile() || stat.isSymbolicLink()
|
|
41
|
+
|| stat.size === 0 || stat.size > MAX_FILE_BYTES)
|
|
42
|
+
throw new Error('refactor-root-write-set-invalid');
|
|
43
|
+
const bytes = readFileSync(physicalPath);
|
|
44
|
+
if (bytes.length === 0 || bytes.length > MAX_FILE_BYTES) {
|
|
45
|
+
throw new Error('refactor-root-write-set-invalid');
|
|
46
|
+
}
|
|
47
|
+
new TextDecoder('utf-8', { fatal: true }).decode(bytes);
|
|
48
|
+
if (bytes.includes(0))
|
|
49
|
+
throw new Error('refactor-root-write-set-invalid');
|
|
50
|
+
const committed = mutationQaGit(root, ['show', `HEAD:${path}`]);
|
|
51
|
+
if (!bytes.equals(committed))
|
|
52
|
+
throw new Error('refactor-root-source-dirty');
|
|
53
|
+
return Object.freeze({ sha256: digest(bytes), mode: stat.mode & 0o777 });
|
|
54
|
+
}
|
|
55
|
+
function ignoredByGit(root, path) {
|
|
56
|
+
try {
|
|
57
|
+
mutationQaGit(root, ['check-ignore', '--no-index', '-q', '--', path]);
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
if (error.status === 1)
|
|
62
|
+
return false;
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/** A new test/document path is explicit, absent, untracked and not ignored. */
|
|
67
|
+
function exactNewPath(root, path) {
|
|
68
|
+
if (!pathIsSafe(path))
|
|
69
|
+
throw new Error('refactor-root-write-set-invalid');
|
|
70
|
+
const physicalPath = join(root, path);
|
|
71
|
+
const parent = dirname(physicalPath);
|
|
72
|
+
const parentStat = lstatSync(parent);
|
|
73
|
+
if (!parentStat.isDirectory() || parentStat.isSymbolicLink() || realpathSync(parent) !== parent
|
|
74
|
+
|| mutationQaGit(root, ['ls-files', '--stage', '-z', '--', path]).length > 0
|
|
75
|
+
|| ignoredByGit(root, path))
|
|
76
|
+
throw new Error('refactor-root-write-set-invalid');
|
|
77
|
+
try {
|
|
78
|
+
lstatSync(physicalPath);
|
|
79
|
+
throw new Error('refactor-root-write-set-invalid');
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
if (error.code !== 'ENOENT')
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function indexDigest(root) {
|
|
87
|
+
return digest(mutationQaGit(root, ['ls-files', '--stage', '-z']));
|
|
88
|
+
}
|
|
89
|
+
function names(root, args) {
|
|
90
|
+
const bytes = mutationQaGit(root, args);
|
|
91
|
+
const decoded = new TextDecoder('utf-8', { fatal: true }).decode(bytes);
|
|
92
|
+
return decoded.split('\0').filter(Boolean);
|
|
93
|
+
}
|
|
94
|
+
function requireNormalIndex(root) {
|
|
95
|
+
const entries = names(root, ['ls-files', '-v', '-z']);
|
|
96
|
+
if (entries.some((entry) => !entry.startsWith('H '))) {
|
|
97
|
+
throw new Error('refactor-root-index-unverified');
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/** Preserve pre-existing visible and ignored Neocortex controls without copying them to a model. */
|
|
101
|
+
export function refactorSidecarDigest(root) {
|
|
102
|
+
const rows = [];
|
|
103
|
+
const seen = new Set();
|
|
104
|
+
let totalBytes = 0;
|
|
105
|
+
const entries = names(root, ['status', '--porcelain=v1', '-z', '--untracked-files=all']);
|
|
106
|
+
entries.push(...names(root, ['ls-files', '--others', '--ignored', '--exclude-standard', '-z', '--', '.neocortex/'])
|
|
107
|
+
.map(path => `!! ${path}`));
|
|
108
|
+
for (const entry of entries) {
|
|
109
|
+
if (entry.length < 4 || entry[2] !== ' ')
|
|
110
|
+
throw new Error('refactor-root-sidecar-unverified');
|
|
111
|
+
const path = entry.slice(3);
|
|
112
|
+
if (!path.startsWith('.neocortex/'))
|
|
113
|
+
continue;
|
|
114
|
+
if (seen.has(path) || path.includes('\\') || /[\u0000-\u001f\u007f]/.test(path)
|
|
115
|
+
|| path.split('/').some((part) => !part || part === '.' || part === '..')) {
|
|
116
|
+
throw new Error('refactor-root-sidecar-unverified');
|
|
117
|
+
}
|
|
118
|
+
seen.add(path);
|
|
119
|
+
const physicalPath = join(root, path);
|
|
120
|
+
const stat = lstatSync(physicalPath);
|
|
121
|
+
if (realpathSync(physicalPath) !== physicalPath || !stat.isFile() || stat.isSymbolicLink()
|
|
122
|
+
|| stat.nlink !== 1 || stat.size > 64 * 1024 * 1024)
|
|
123
|
+
throw new Error('refactor-root-sidecar-unverified');
|
|
124
|
+
totalBytes += stat.size;
|
|
125
|
+
if (totalBytes > 128 * 1024 * 1024)
|
|
126
|
+
throw new Error('refactor-root-sidecar-unverified');
|
|
127
|
+
const fd = openSync(physicalPath, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
128
|
+
try {
|
|
129
|
+
const buffer = Buffer.alloc(stat.size + 1);
|
|
130
|
+
let length = 0;
|
|
131
|
+
while (length < buffer.length) {
|
|
132
|
+
const count = readSync(fd, buffer, length, buffer.length - length, length);
|
|
133
|
+
if (!count)
|
|
134
|
+
break;
|
|
135
|
+
length += count;
|
|
136
|
+
}
|
|
137
|
+
const after = fstatSync(fd), current = lstatSync(physicalPath);
|
|
138
|
+
if (length !== stat.size || realpathSync(physicalPath) !== physicalPath
|
|
139
|
+
|| [after, current].some(value => value.dev !== stat.dev || value.ino !== stat.ino
|
|
140
|
+
|| value.size !== stat.size || value.mtimeMs !== stat.mtimeMs || value.ctimeMs !== stat.ctimeMs
|
|
141
|
+
|| value.mode !== stat.mode || value.nlink !== 1))
|
|
142
|
+
throw new Error('refactor-root-sidecar-unverified');
|
|
143
|
+
rows.push({ status: entry.slice(0, 2), path, sha256: digest(buffer.subarray(0, length)), mode: stat.mode & 0o777 });
|
|
144
|
+
}
|
|
145
|
+
finally {
|
|
146
|
+
closeSync(fd);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
rows.sort((a, b) => Buffer.from(a.path).compare(Buffer.from(b.path)));
|
|
150
|
+
return digest(JSON.stringify(rows));
|
|
151
|
+
}
|
|
152
|
+
function orderedPaths(paths) {
|
|
153
|
+
return [...paths].sort((a, b) => Buffer.from(a).compare(Buffer.from(b)));
|
|
154
|
+
}
|
|
155
|
+
function writeSetDigest(record) {
|
|
156
|
+
const files = record.files.map((file) => file.path);
|
|
157
|
+
return digest(JSON.stringify(record.schemaVersion === 1
|
|
158
|
+
? files : { files, newFiles: record.newFiles.map((file) => file.path) }));
|
|
159
|
+
}
|
|
160
|
+
function admissionPath(root, sha256) {
|
|
161
|
+
if (!SHA256.test(sha256))
|
|
162
|
+
throw new Error('refactor-root-admission-invalid');
|
|
163
|
+
return join(privateRefactorDirectory(root), `admission-${sha256}.json`);
|
|
164
|
+
}
|
|
165
|
+
export function readPrivateRefactorAdmission(root, sha256) {
|
|
166
|
+
const path = admissionPath(root, sha256);
|
|
167
|
+
const stat = lstatSync(path);
|
|
168
|
+
if (!stat.isFile() || stat.isSymbolicLink() || stat.uid !== process.getuid?.()
|
|
169
|
+
|| (stat.mode & 0o077) !== 0 || stat.size < 100 || stat.size > 512 * 1024) {
|
|
170
|
+
throw new Error('refactor-root-admission-invalid');
|
|
171
|
+
}
|
|
172
|
+
const raw = readFileSync(path);
|
|
173
|
+
if (raw.length > 512 * 1024)
|
|
174
|
+
throw new Error('refactor-root-admission-invalid');
|
|
175
|
+
let parsed;
|
|
176
|
+
try {
|
|
177
|
+
parsed = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(raw));
|
|
178
|
+
}
|
|
179
|
+
catch {
|
|
180
|
+
throw new Error('refactor-root-admission-invalid');
|
|
181
|
+
}
|
|
182
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
183
|
+
throw new Error('refactor-root-admission-invalid');
|
|
184
|
+
}
|
|
185
|
+
const record = parsed;
|
|
186
|
+
const v2 = record.schemaVersion === 2;
|
|
187
|
+
const keys = ['schemaVersion', 'kind', 'storyId', 'sourceHead', 'storySha256',
|
|
188
|
+
'planSha256', 'indexSha256', 'sidecarSha256', 'files', 'admissionSha256'];
|
|
189
|
+
if (v2)
|
|
190
|
+
keys.push('newFiles');
|
|
191
|
+
if (Object.keys(record).sort().join('\0') !== keys.sort().join('\0')
|
|
192
|
+
|| ![1, 2].includes(record.schemaVersion)
|
|
193
|
+
|| record.kind !== 'private-root-refactor-admission'
|
|
194
|
+
|| typeof record.storyId !== 'string' || !STORY_ID.test(record.storyId)
|
|
195
|
+
|| typeof record.sourceHead !== 'string' || !/^[a-f0-9]{40}(?:[a-f0-9]{24})?$/.test(record.sourceHead)
|
|
196
|
+
|| typeof record.storySha256 !== 'string' || !SHA256.test(record.storySha256)
|
|
197
|
+
|| typeof record.planSha256 !== 'string' || !SHA256.test(record.planSha256)
|
|
198
|
+
|| typeof record.indexSha256 !== 'string' || !SHA256.test(record.indexSha256)
|
|
199
|
+
|| typeof record.sidecarSha256 !== 'string' || !SHA256.test(record.sidecarSha256)
|
|
200
|
+
|| record.admissionSha256 !== sha256 || !Array.isArray(record.files)
|
|
201
|
+
|| record.files.length > MAX_PATHS
|
|
202
|
+
|| (v2 ? !Array.isArray(record.newFiles) || record.newFiles.length < 1
|
|
203
|
+
|| record.files.length + record.newFiles.length > MAX_PATHS
|
|
204
|
+
: record.files.length < 1)) {
|
|
205
|
+
throw new Error('refactor-root-admission-invalid');
|
|
206
|
+
}
|
|
207
|
+
if (digest(`root-refactor-story-scope-v1\0${record.sourceHead}\0${record.storyId}\0${record.storySha256}`)
|
|
208
|
+
!== record.planSha256)
|
|
209
|
+
throw new Error('refactor-root-admission-invalid');
|
|
210
|
+
const files = record.files;
|
|
211
|
+
if (files.some((file) => !file || typeof file !== 'object' || Array.isArray(file)
|
|
212
|
+
|| Object.keys(file).sort().join('\0') !== ['mode', 'path', 'sha256'].join('\0')
|
|
213
|
+
|| typeof file.path !== 'string'
|
|
214
|
+
|| !pathIsSafe(file.path)
|
|
215
|
+
|| typeof file.sha256 !== 'string'
|
|
216
|
+
|| !SHA256.test(file.sha256)
|
|
217
|
+
|| !Number.isSafeInteger(file.mode)
|
|
218
|
+
|| file.mode < 0
|
|
219
|
+
|| file.mode > 0o777)) {
|
|
220
|
+
throw new Error('refactor-root-admission-invalid');
|
|
221
|
+
}
|
|
222
|
+
const ordered = files.map((file) => file.path);
|
|
223
|
+
if (new Set(ordered).size !== ordered.length
|
|
224
|
+
|| ordered.join('\0') !== orderedPaths(ordered).join('\0')) {
|
|
225
|
+
throw new Error('refactor-root-admission-invalid');
|
|
226
|
+
}
|
|
227
|
+
if (v2) {
|
|
228
|
+
const newFiles = record.newFiles;
|
|
229
|
+
if (newFiles.some((file) => !file || typeof file !== 'object' || Array.isArray(file)
|
|
230
|
+
|| Object.keys(file).join('\0') !== 'path'
|
|
231
|
+
|| typeof file.path !== 'string'
|
|
232
|
+
|| !pathIsSafe(file.path))) {
|
|
233
|
+
throw new Error('refactor-root-admission-invalid');
|
|
234
|
+
}
|
|
235
|
+
const newPaths = newFiles.map((file) => file.path);
|
|
236
|
+
if (new Set([...ordered, ...newPaths]).size !== ordered.length + newPaths.length
|
|
237
|
+
|| newPaths.join('\0') !== orderedPaths(newPaths).join('\0')
|
|
238
|
+
|| [...ordered, ...newPaths].includes(`docs/stories/${record.storyId}.story.md`)) {
|
|
239
|
+
throw new Error('refactor-root-admission-invalid');
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
const { admissionSha256: _digest, ...body } = record;
|
|
243
|
+
if (digest(JSON.stringify(body)) !== sha256 || !raw.equals(Buffer.from(`${JSON.stringify(record)}\n`))) {
|
|
244
|
+
throw new Error('refactor-root-admission-invalid');
|
|
245
|
+
}
|
|
246
|
+
return record;
|
|
247
|
+
}
|
|
248
|
+
export function admitRootRefactor(projectRoot, storyId, planSha256, paths, authorizeRootEdit, newPaths = []) {
|
|
249
|
+
if (!authorizeRootEdit)
|
|
250
|
+
throw new Error('refactor-root-authorization-required');
|
|
251
|
+
if (!SHA256.test(planSha256) || paths.length + newPaths.length < 1
|
|
252
|
+
|| paths.length + newPaths.length > MAX_PATHS) {
|
|
253
|
+
throw new Error('refactor-root-write-set-invalid');
|
|
254
|
+
}
|
|
255
|
+
const root = resolve(projectRoot);
|
|
256
|
+
try {
|
|
257
|
+
const plan = planRootRefactor(root, storyId);
|
|
258
|
+
if (plan.planSha256 !== planSha256)
|
|
259
|
+
throw new Error('refactor-root-plan-stale');
|
|
260
|
+
requireNormalIndex(root);
|
|
261
|
+
const beforeIndex = indexDigest(root);
|
|
262
|
+
const beforeSidecar = refactorSidecarDigest(root);
|
|
263
|
+
const ordered = orderedPaths(paths);
|
|
264
|
+
const orderedNew = orderedPaths(newPaths);
|
|
265
|
+
if (new Set([...ordered, ...orderedNew]).size !== ordered.length + orderedNew.length
|
|
266
|
+
|| [...ordered, ...orderedNew].includes(`docs/stories/${storyId}.story.md`)) {
|
|
267
|
+
throw new Error('refactor-root-write-set-invalid');
|
|
268
|
+
}
|
|
269
|
+
const files = ordered.map((path) => Object.freeze({ path, ...exactCommittedRefactorFile(root, path) }));
|
|
270
|
+
const newFiles = orderedNew.map((path) => { exactNewPath(root, path); return Object.freeze({ path }); });
|
|
271
|
+
if (indexDigest(root) !== beforeIndex || refactorSidecarDigest(root) !== beforeSidecar
|
|
272
|
+
|| planRootRefactor(root, storyId).planSha256 !== planSha256) {
|
|
273
|
+
throw new Error('refactor-root-source-dirty');
|
|
274
|
+
}
|
|
275
|
+
for (const file of newFiles)
|
|
276
|
+
exactNewPath(root, file.path);
|
|
277
|
+
const base = { kind: 'private-root-refactor-admission',
|
|
278
|
+
storyId, sourceHead: plan.sourceHead, storySha256: plan.storySha256,
|
|
279
|
+
planSha256, indexSha256: beforeIndex, sidecarSha256: beforeSidecar, files };
|
|
280
|
+
const body = newFiles.length > 0
|
|
281
|
+
? { schemaVersion: 2, ...base, newFiles }
|
|
282
|
+
: { schemaVersion: 1, ...base };
|
|
283
|
+
const admissionSha256 = digest(JSON.stringify(body));
|
|
284
|
+
const record = { ...body, admissionSha256 };
|
|
285
|
+
const path = admissionPath(root, admissionSha256);
|
|
286
|
+
const bytes = Buffer.from(`${JSON.stringify(record)}\n`);
|
|
287
|
+
if (existsSync(path)) {
|
|
288
|
+
readPrivateRefactorAdmission(root, admissionSha256);
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
const handle = openSync(path, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL
|
|
292
|
+
| (constants.O_NOFOLLOW ?? 0), 0o600);
|
|
293
|
+
try {
|
|
294
|
+
writeFileSync(handle, bytes);
|
|
295
|
+
fsyncSync(handle);
|
|
296
|
+
}
|
|
297
|
+
finally {
|
|
298
|
+
closeSync(handle);
|
|
299
|
+
}
|
|
300
|
+
const directoryHandle = openSync(privateRefactorDirectory(root), constants.O_RDONLY | (constants.O_DIRECTORY ?? 0));
|
|
301
|
+
try {
|
|
302
|
+
fsyncSync(directoryHandle);
|
|
303
|
+
}
|
|
304
|
+
finally {
|
|
305
|
+
closeSync(directoryHandle);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
readPrivateRefactorAdmission(root, admissionSha256);
|
|
309
|
+
if (indexDigest(root) !== beforeIndex || refactorSidecarDigest(root) !== beforeSidecar
|
|
310
|
+
|| planRootRefactor(root, storyId).planSha256 !== planSha256) {
|
|
311
|
+
throw new Error('refactor-root-source-dirty');
|
|
312
|
+
}
|
|
313
|
+
for (const file of newFiles)
|
|
314
|
+
exactNewPath(root, file.path);
|
|
315
|
+
const receipt = { kind: 'root-refactor-write-set-admission',
|
|
316
|
+
storyId, sourceHead: plan.sourceHead, planSha256,
|
|
317
|
+
writeSetSha256: writeSetDigest(record), admissionSha256,
|
|
318
|
+
fileCount: files.length, rootEditPrepared: true, privateChildAuthorized: false,
|
|
319
|
+
gitCommitAuthorized: false, publicSafe: true };
|
|
320
|
+
return record.schemaVersion === 2
|
|
321
|
+
? Object.freeze({ schemaVersion: 2, ...receipt, newFileCount: newFiles.length })
|
|
322
|
+
: Object.freeze({ schemaVersion: 1, ...receipt });
|
|
323
|
+
}
|
|
324
|
+
catch (error) {
|
|
325
|
+
if (error instanceof Error && error.message.startsWith('refactor-root-'))
|
|
326
|
+
throw error;
|
|
327
|
+
throw new Error('refactor-root-write-set-invalid');
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
function physicalNewFile(root, path) {
|
|
331
|
+
if (!pathIsSafe(path) || ignoredByGit(root, path)
|
|
332
|
+
|| mutationQaGit(root, ['ls-files', '--stage', '-z', '--', path]).length > 0) {
|
|
333
|
+
throw new Error('refactor-root-delta-outside-admission');
|
|
334
|
+
}
|
|
335
|
+
const physicalPath = join(root, path);
|
|
336
|
+
const parent = dirname(physicalPath);
|
|
337
|
+
if (!lstatSync(parent).isDirectory() || realpathSync(parent) !== parent) {
|
|
338
|
+
throw new Error('refactor-root-delta-outside-admission');
|
|
339
|
+
}
|
|
340
|
+
const stat = lstatSync(physicalPath);
|
|
341
|
+
if (realpathSync(physicalPath) !== physicalPath || !stat.isFile() || stat.isSymbolicLink()
|
|
342
|
+
|| stat.size === 0 || stat.size > MAX_FILE_BYTES
|
|
343
|
+
|| (stat.mode & 0o113) !== 0)
|
|
344
|
+
throw new Error('refactor-root-delta-outside-admission');
|
|
345
|
+
const bytes = readFileSync(physicalPath);
|
|
346
|
+
if (bytes.length === 0 || bytes.length > MAX_FILE_BYTES || bytes.includes(0)) {
|
|
347
|
+
throw new Error('refactor-root-delta-outside-admission');
|
|
348
|
+
}
|
|
349
|
+
new TextDecoder('utf-8', { fatal: true }).decode(bytes);
|
|
350
|
+
return digest(bytes);
|
|
351
|
+
}
|
|
352
|
+
export function verifyRootRefactor(projectRoot, admissionSha256) {
|
|
353
|
+
return verifyRefactorDelta(projectRoot, admissionSha256, false).receipt;
|
|
354
|
+
}
|
|
355
|
+
/** Private characterization preparation only. Full review/commit still requires
|
|
356
|
+
* every admitted new file. Missing Markdown cannot authorize QA or Git effects.
|
|
357
|
+
*/
|
|
358
|
+
export function verifyRootRefactorForCharacterization(projectRoot, admissionSha256) {
|
|
359
|
+
return verifyRefactorDelta(projectRoot, admissionSha256, true);
|
|
360
|
+
}
|
|
361
|
+
function verifyRefactorDelta(projectRoot, admissionSha256, allowPendingDocumentation) {
|
|
362
|
+
const root = resolve(projectRoot);
|
|
363
|
+
try {
|
|
364
|
+
const record = readPrivateRefactorAdmission(root, admissionSha256);
|
|
365
|
+
const head = mutationQaGit(root, ['rev-parse', '--verify', 'HEAD']).toString('utf8').trim();
|
|
366
|
+
if (head !== record.sourceHead || indexDigest(root) !== record.indexSha256
|
|
367
|
+
|| refactorSidecarDigest(root) !== record.sidecarSha256) {
|
|
368
|
+
throw new Error('refactor-root-baseline-stale');
|
|
369
|
+
}
|
|
370
|
+
requireNormalIndex(root);
|
|
371
|
+
const storyPath = join(root, 'docs', 'stories', `${record.storyId}.story.md`);
|
|
372
|
+
const storyStat = lstatSync(storyPath);
|
|
373
|
+
if (!storyStat.isFile() || storyStat.isSymbolicLink() || realpathSync(storyPath) !== storyPath
|
|
374
|
+
|| digest(readFileSync(storyPath)) !== record.storySha256
|
|
375
|
+
|| digest(mutationQaGit(root, ['show', `HEAD:docs/stories/${record.storyId}.story.md`]))
|
|
376
|
+
!== record.storySha256) {
|
|
377
|
+
throw new Error('refactor-root-baseline-stale');
|
|
378
|
+
}
|
|
379
|
+
const changed = names(root, ['diff', '--name-only', '-z', '--no-ext-diff', '--no-textconv'])
|
|
380
|
+
.filter((path) => !path.startsWith('.neocortex/'));
|
|
381
|
+
const untracked = names(root, ['ls-files', '--others', '--exclude-standard', '-z'])
|
|
382
|
+
.filter((path) => !path.startsWith('.neocortex/'));
|
|
383
|
+
const admitted = new Set(record.files.map((file) => file.path));
|
|
384
|
+
const admittedNew = record.schemaVersion === 2 ? record.newFiles.map((file) => file.path) : [];
|
|
385
|
+
const pendingDocumentationPaths = [];
|
|
386
|
+
if (allowPendingDocumentation)
|
|
387
|
+
for (const path of admittedNew) {
|
|
388
|
+
if (!path.endsWith('.md'))
|
|
389
|
+
continue;
|
|
390
|
+
try {
|
|
391
|
+
lstatSync(join(root, path));
|
|
392
|
+
}
|
|
393
|
+
catch (error) {
|
|
394
|
+
if (error.code !== 'ENOENT')
|
|
395
|
+
throw error;
|
|
396
|
+
exactNewPath(root, path);
|
|
397
|
+
pendingDocumentationPaths.push(path);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
const materializedNew = admittedNew.filter(path => !pendingDocumentationPaths.includes(path));
|
|
401
|
+
const newSet = new Set(admittedNew);
|
|
402
|
+
if (changed.length + untracked.length === 0 || untracked.length !== materializedNew.length
|
|
403
|
+
|| changed.some((path) => !admitted.has(path))
|
|
404
|
+
|| untracked.some((path) => !newSet.has(path))) {
|
|
405
|
+
throw new Error('refactor-root-delta-outside-admission');
|
|
406
|
+
}
|
|
407
|
+
let changedFileCount = 0;
|
|
408
|
+
const currentDigests = [];
|
|
409
|
+
for (const file of record.files) {
|
|
410
|
+
const physicalPath = join(root, file.path);
|
|
411
|
+
if (digest(mutationQaGit(root, ['show', `HEAD:${file.path}`])) !== file.sha256) {
|
|
412
|
+
throw new Error('refactor-root-admission-invalid');
|
|
413
|
+
}
|
|
414
|
+
const stat = lstatSync(physicalPath);
|
|
415
|
+
if (realpathSync(physicalPath) !== physicalPath || !stat.isFile() || stat.isSymbolicLink()
|
|
416
|
+
|| stat.size === 0 || stat.size > MAX_FILE_BYTES || (stat.mode & 0o777) !== file.mode) {
|
|
417
|
+
throw new Error('refactor-root-delta-outside-admission');
|
|
418
|
+
}
|
|
419
|
+
const bytes = readFileSync(physicalPath);
|
|
420
|
+
if (bytes.length === 0 || bytes.length > MAX_FILE_BYTES) {
|
|
421
|
+
throw new Error('refactor-root-delta-outside-admission');
|
|
422
|
+
}
|
|
423
|
+
new TextDecoder('utf-8', { fatal: true }).decode(bytes);
|
|
424
|
+
if (bytes.includes(0))
|
|
425
|
+
throw new Error('refactor-root-delta-outside-admission');
|
|
426
|
+
const current = digest(bytes);
|
|
427
|
+
currentDigests.push(current);
|
|
428
|
+
if (current !== file.sha256)
|
|
429
|
+
changedFileCount++;
|
|
430
|
+
}
|
|
431
|
+
const newDigests = materializedNew.map((path) => physicalNewFile(root, path));
|
|
432
|
+
changedFileCount += newDigests.length;
|
|
433
|
+
const finalChanged = names(root, ['diff', '--name-only', '-z', '--no-ext-diff', '--no-textconv'])
|
|
434
|
+
.filter((path) => !path.startsWith('.neocortex/'));
|
|
435
|
+
const finalUntracked = names(root, ['ls-files', '--others', '--exclude-standard', '-z'])
|
|
436
|
+
.filter((path) => !path.startsWith('.neocortex/'));
|
|
437
|
+
requireNormalIndex(root);
|
|
438
|
+
if (changedFileCount === 0 || changedFileCount !== changed.length + untracked.length
|
|
439
|
+
|| finalChanged.join('\0') !== changed.join('\0')
|
|
440
|
+
|| finalUntracked.join('\0') !== untracked.join('\0')
|
|
441
|
+
|| record.files.some((file, index) => {
|
|
442
|
+
const path = join(root, file.path);
|
|
443
|
+
const stat = lstatSync(path);
|
|
444
|
+
if (realpathSync(path) !== path || !stat.isFile() || stat.isSymbolicLink()
|
|
445
|
+
|| stat.size > MAX_FILE_BYTES || (stat.mode & 0o777) !== file.mode)
|
|
446
|
+
return true;
|
|
447
|
+
const bytes = readFileSync(path);
|
|
448
|
+
return bytes.length > MAX_FILE_BYTES || digest(bytes) !== currentDigests[index];
|
|
449
|
+
})
|
|
450
|
+
|| materializedNew.some((path, index) => physicalNewFile(root, path) !== newDigests[index])
|
|
451
|
+
|| indexDigest(root) !== record.indexSha256
|
|
452
|
+
|| refactorSidecarDigest(root) !== record.sidecarSha256
|
|
453
|
+
|| mutationQaGit(root, ['rev-parse', '--verify', 'HEAD']).toString('utf8').trim() !== head) {
|
|
454
|
+
throw new Error('refactor-root-baseline-stale');
|
|
455
|
+
}
|
|
456
|
+
for (const path of pendingDocumentationPaths)
|
|
457
|
+
exactNewPath(root, path);
|
|
458
|
+
const receipt = { kind: 'root-refactor-delta-containment',
|
|
459
|
+
storyId: record.storyId, sourceHead: record.sourceHead,
|
|
460
|
+
planSha256: record.planSha256, admissionSha256,
|
|
461
|
+
writeSetSha256: writeSetDigest(record),
|
|
462
|
+
changedFileCount, rootEditContained: true, qaAccepted: false,
|
|
463
|
+
gitCommitAuthorized: false, publicSafe: true };
|
|
464
|
+
const fullReceipt = record.schemaVersion === 2
|
|
465
|
+
? Object.freeze({ schemaVersion: 2, ...receipt, newFileCount: materializedNew.length })
|
|
466
|
+
: Object.freeze({ schemaVersion: 1, ...receipt });
|
|
467
|
+
return Object.freeze({ receipt: fullReceipt,
|
|
468
|
+
pendingDocumentationPaths: Object.freeze(pendingDocumentationPaths) });
|
|
469
|
+
}
|
|
470
|
+
catch (error) {
|
|
471
|
+
if (error instanceof Error && error.message.startsWith('refactor-root-'))
|
|
472
|
+
throw error;
|
|
473
|
+
throw new Error('refactor-root-delta-unverified');
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
export function planRootRefactor(projectRoot, storyId) {
|
|
477
|
+
if (!STORY_ID.test(storyId))
|
|
478
|
+
throw new Error('refactor-root-story-invalid');
|
|
479
|
+
const root = resolve(projectRoot);
|
|
480
|
+
try {
|
|
481
|
+
if (realpathSync(root) !== root || !lstatSync(root).isDirectory()) {
|
|
482
|
+
throw new Error('refactor-root-source-unverified');
|
|
483
|
+
}
|
|
484
|
+
const top = mutationQaGit(root, ['rev-parse', '--show-toplevel']).toString('utf8').trim();
|
|
485
|
+
if (top !== root)
|
|
486
|
+
throw new Error('refactor-root-source-unverified');
|
|
487
|
+
const status = mutationQaGit(root, ['status', '--porcelain=v1', '-z', '--untracked-files=all'])
|
|
488
|
+
.toString('utf8').split('\0').filter(Boolean);
|
|
489
|
+
if (status.some((entry) => !/^.{2} \.neocortex\//.test(entry))) {
|
|
490
|
+
throw new Error('refactor-root-source-dirty');
|
|
491
|
+
}
|
|
492
|
+
const sourceHead = mutationQaGit(root, ['rev-parse', '--verify', 'HEAD']).toString('utf8').trim();
|
|
493
|
+
if (!/^[a-f0-9]{40}(?:[a-f0-9]{24})?$/.test(sourceHead)) {
|
|
494
|
+
throw new Error('refactor-root-source-unverified');
|
|
495
|
+
}
|
|
496
|
+
const relativePath = `docs/stories/${storyId}.story.md`;
|
|
497
|
+
const physicalPath = join(root, relativePath);
|
|
498
|
+
const stat = lstatSync(physicalPath);
|
|
499
|
+
if (realpathSync(physicalPath) !== physicalPath || !stat.isFile() || stat.isSymbolicLink()
|
|
500
|
+
|| stat.size === 0 || stat.size > 1024 * 1024) {
|
|
501
|
+
throw new Error('refactor-root-story-unverified');
|
|
502
|
+
}
|
|
503
|
+
const physical = readFileSync(physicalPath);
|
|
504
|
+
const committed = mutationQaGit(root, ['show', `HEAD:${relativePath}`]);
|
|
505
|
+
if (!physical.equals(committed))
|
|
506
|
+
throw new Error('refactor-root-story-unverified');
|
|
507
|
+
const storySha256 = digest(physical);
|
|
508
|
+
const planSha256 = digest(`root-refactor-story-scope-v1\0${sourceHead}\0${storyId}\0${storySha256}`);
|
|
509
|
+
return Object.freeze({ schemaVersion: 1, kind: 'root-refactor-story-scope', storyId,
|
|
510
|
+
sourceHead, storySha256, planSha256, candidateWriteSetRequired: true,
|
|
511
|
+
characterizationRequired: true, executionAuthorized: false,
|
|
512
|
+
gitAuthority: 'ROOT_ONLY', graphExecution: 'NOT_RUN', publicSafe: true });
|
|
513
|
+
}
|
|
514
|
+
catch (error) {
|
|
515
|
+
if (error instanceof Error && error.message.startsWith('refactor-root-'))
|
|
516
|
+
throw error;
|
|
517
|
+
throw new Error('refactor-root-source-unverified');
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
export function refactorRootCliHandler(args) {
|
|
521
|
+
let action = '';
|
|
522
|
+
let projectRoot = '';
|
|
523
|
+
let storyId = '';
|
|
524
|
+
let planSha256 = '';
|
|
525
|
+
let admissionSha256 = '';
|
|
526
|
+
let authorizeRootEdit = false;
|
|
527
|
+
const paths = [];
|
|
528
|
+
const newPaths = [];
|
|
529
|
+
let format = 'plain';
|
|
530
|
+
try {
|
|
531
|
+
const tokens = [...args];
|
|
532
|
+
action = tokens.shift() ?? '';
|
|
533
|
+
for (let index = 0; index < tokens.length; index++) {
|
|
534
|
+
const token = tokens[index];
|
|
535
|
+
const next = () => {
|
|
536
|
+
const value = tokens[++index];
|
|
537
|
+
if (!value || value.startsWith('--'))
|
|
538
|
+
throw new Error('refactor-root-arguments-invalid');
|
|
539
|
+
return value;
|
|
540
|
+
};
|
|
541
|
+
if (token === '--project-root')
|
|
542
|
+
projectRoot = next();
|
|
543
|
+
else if (token === '--story')
|
|
544
|
+
storyId = next();
|
|
545
|
+
else if (token === '--plan-sha')
|
|
546
|
+
planSha256 = next();
|
|
547
|
+
else if (token === '--admission-sha')
|
|
548
|
+
admissionSha256 = next();
|
|
549
|
+
else if (token === '--path')
|
|
550
|
+
paths.push(next());
|
|
551
|
+
else if (token === '--new-path')
|
|
552
|
+
newPaths.push(next());
|
|
553
|
+
else if (token === '--authorize-root-edit')
|
|
554
|
+
authorizeRootEdit = true;
|
|
555
|
+
else if (token === '--format')
|
|
556
|
+
format = next();
|
|
557
|
+
else
|
|
558
|
+
throw new Error('refactor-root-arguments-invalid');
|
|
559
|
+
}
|
|
560
|
+
if (!projectRoot || !['plain', 'json'].includes(format)) {
|
|
561
|
+
throw new Error('refactor-root-arguments-invalid');
|
|
562
|
+
}
|
|
563
|
+
if (action === 'plan' && storyId && !planSha256 && !admissionSha256
|
|
564
|
+
&& paths.length === 0 && newPaths.length === 0 && !authorizeRootEdit) {
|
|
565
|
+
const plan = planRootRefactor(projectRoot, storyId);
|
|
566
|
+
process.stdout.write(`${JSON.stringify({ ok: true, plan })}\n`);
|
|
567
|
+
}
|
|
568
|
+
else if (action === 'admit' && storyId && planSha256 && !admissionSha256
|
|
569
|
+
&& paths.length + newPaths.length > 0) {
|
|
570
|
+
const admission = admitRootRefactor(projectRoot, storyId, planSha256, paths, authorizeRootEdit, newPaths);
|
|
571
|
+
process.stdout.write(`${JSON.stringify({ ok: true, admission })}\n`);
|
|
572
|
+
}
|
|
573
|
+
else if (action === 'verify' && !storyId && !planSha256 && admissionSha256
|
|
574
|
+
&& paths.length === 0 && newPaths.length === 0 && !authorizeRootEdit) {
|
|
575
|
+
const containment = verifyRootRefactor(projectRoot, admissionSha256);
|
|
576
|
+
process.stdout.write(`${JSON.stringify({ ok: true, containment })}\n`);
|
|
577
|
+
}
|
|
578
|
+
else
|
|
579
|
+
throw new Error('refactor-root-arguments-invalid');
|
|
580
|
+
return 0;
|
|
581
|
+
}
|
|
582
|
+
catch (error) {
|
|
583
|
+
const reasonCode = error instanceof Error && /^refactor-root-[a-z-]+$/.test(error.message)
|
|
584
|
+
? error.message : 'refactor-root-source-unverified';
|
|
585
|
+
process.stdout.write(`${JSON.stringify({ ok: false, reasonCode })}\n`);
|
|
586
|
+
return 1;
|
|
587
|
+
}
|
|
588
|
+
}
|