afyx-engineering-kit 0.0.0-stage → 0.1.0-alpha.3
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/.afyx-source-revision +1 -0
- package/LICENSE +21 -0
- package/README.md +377 -2
- package/SECURITY.md +74 -0
- package/afyx-context/LICENSE +21 -0
- package/afyx-context/README.md +155 -0
- package/afyx-context/afyx-context.json +30 -0
- package/afyx-context/contracts/integration-v1.schema.json +34 -0
- package/afyx-context/package-lock.json +53 -0
- package/afyx-context/package.json +34 -0
- package/afyx-context/scripts/benchmark.mjs +39 -0
- package/afyx-context/scripts/build.mjs +15 -0
- package/afyx-context/scripts/comparative-benchmark.mjs +342 -0
- package/afyx-context/scripts/comparative-worker.mjs +74 -0
- package/afyx-context/scripts/graph-integration-e2e.mjs +188 -0
- package/afyx-context/scripts/integrated-dogfood.mjs +309 -0
- package/afyx-context/src/budget.js +166 -0
- package/afyx-context/src/cli.js +110 -0
- package/afyx-context/src/coordination.js +343 -0
- package/afyx-context/src/engine.js +198 -0
- package/afyx-context/src/errors.js +14 -0
- package/afyx-context/src/graph-integration-worker.js +70 -0
- package/afyx-context/src/graph-integration.js +373 -0
- package/afyx-context/src/graph-mcp-bridge.js +103 -0
- package/afyx-context/src/index.js +25 -0
- package/afyx-context/src/integrations.js +87 -0
- package/afyx-context/src/memory.js +255 -0
- package/afyx-context/src/storage.js +364 -0
- package/afyx-context/test-fixtures/coordination-worker.mjs +52 -0
- package/afyx-context/test-fixtures/graph-consumer-repo/addons/sale_extension/__init__.py +1 -0
- package/afyx-context/test-fixtures/graph-consumer-repo/addons/sale_extension/__manifest__.py +4 -0
- package/afyx-context/test-fixtures/graph-consumer-repo/addons/sale_extension/models/__init__.py +1 -0
- package/afyx-context/test-fixtures/graph-consumer-repo/addons/sale_extension/models/sale_order.py +5 -0
- package/afyx-context/test-fixtures/integrated-dogfood-repo/addons/integrated_sales/__init__.py +1 -0
- package/afyx-context/test-fixtures/integrated-dogfood-repo/addons/integrated_sales/__manifest__.py +10 -0
- package/afyx-context/test-fixtures/integrated-dogfood-repo/addons/integrated_sales/models/__init__.py +2 -0
- package/afyx-context/test-fixtures/integrated-dogfood-repo/addons/integrated_sales/models/sale_order.py +20 -0
- package/afyx-context/test-fixtures/integrated-dogfood-repo/addons/integrated_sales/models/sale_order_audit.py +11 -0
- package/afyx-context/test-fixtures/integrated-dogfood-repo/addons/integrated_sales/security/ir.model.access.csv +2 -0
- package/afyx-context/test-fixtures/integrated-dogfood-repo/addons/integrated_sales/views/sale_order_views.xml +14 -0
- package/afyx-forge/LICENSE +55 -0
- package/afyx-forge/README.md +126 -0
- package/afyx-forge/afyx-forge.json +28 -0
- package/afyx-forge/bin/afyx-forge +4 -0
- package/afyx-forge/bin/afyx-forge.cmd +3 -0
- package/afyx-forge/bin/afyx-forge.js +16 -0
- package/afyx-forge/contracts/forge-plan-v1.schema.json +172 -0
- package/afyx-forge/package.json +26 -0
- package/afyx-forge/src/classifier.js +190 -0
- package/afyx-forge/src/cli.js +111 -0
- package/afyx-forge/src/doctor.js +105 -0
- package/afyx-forge/src/guidance.js +120 -0
- package/afyx-forge/src/index.js +273 -0
- package/afyx-forge/src/planning.js +107 -0
- package/afyx-forge/src/policy.js +109 -0
- package/afyx-forge/src/workflow-refiner.js +151 -0
- package/afyx-graph/afyx-graph.json +20 -0
- package/afyx-graph/engine/LICENSE +21 -0
- package/afyx-graph/engine/README.md +104 -0
- package/afyx-graph/engine/release/RELEASE-MANIFEST.json +21 -0
- package/afyx-graph/engine/release/SHA256SUMS +2 -0
- package/afyx-graph/engine/release/afyx-graph-linux-x64.tar.gz +0 -0
- package/afyx-graph/engine/release/afyx-graph-win32-x64.zip +0 -0
- package/afyx-graph/engine/scripts/distribution-contract.mjs +212 -0
- package/afyx-graph/engine/scripts/distribution-product.json +33 -0
- package/bin/afyx-engineering-kit.mjs +61 -0
- package/install.ps1 +1053 -0
- package/install.sh +617 -0
- package/package.json +66 -4
- package/scripts/afyx-capability-orchestrator.mjs +752 -0
- package/scripts/afyx-claude-integration.mjs +547 -0
- package/scripts/afyx-context-artifact.mjs +105 -0
- package/scripts/afyx-credential-broker.mjs +75 -0
- package/scripts/afyx-doctor.ps1 +152 -0
- package/scripts/afyx-doctor.sh +146 -0
- package/scripts/afyx-forge-artifact.mjs +183 -0
- package/scripts/afyx-global-routing-policy.md +12 -0
- package/scripts/afyx-global-routing.mjs +168 -0
- package/scripts/afyx-graph-artifact.mjs +178 -0
- package/scripts/afyx-host-adapters.mjs +543 -0
- package/scripts/afyx-host-bridge-lifecycle.mjs +174 -0
- package/scripts/afyx-host-bridge.mjs +288 -0
- package/scripts/afyx-integration-contracts.mjs +825 -0
- package/scripts/afyx-mcp-integration.mjs +528 -0
- package/scripts/afyx-modular-integration.mjs +361 -0
- package/scripts/afyx-modular-runtime.mjs +451 -0
- package/scripts/afyx-native-skill-host.mjs +124 -0
- package/scripts/afyx-npm-installer.mjs +486 -0
- package/scripts/afyx-project.ps1 +205 -0
- package/scripts/afyx-project.sh +207 -0
- package/scripts/afyx-provider-backup.mjs +1206 -0
- package/scripts/afyx-provider-integration.mjs +724 -0
- package/scripts/afyx-provider-inventory.mjs +147 -0
- package/scripts/afyx-security-auditor.mjs +224 -0
- package/scripts/afyx-skill-engineering.mjs +354 -0
- package/scripts/afyx-tool-family-routing.mjs +64 -0
- package/scripts/afyx-universal-runtime.mjs +530 -0
- package/scripts/alpha_acceptance_preflight.py +240 -0
- package/scripts/benchmark-graph-memory.mjs +96 -0
- package/scripts/benchmark_capability_orchestrator.mjs +64 -0
- package/scripts/build-windows-installer.ps1 +259 -0
- package/scripts/build_forge_synthetic_image.py +328 -0
- package/scripts/check-reference-staleness.py +71 -0
- package/scripts/components.json +254 -0
- package/scripts/contracts/afyx-capability-request-v1.schema.json +189 -0
- package/scripts/contracts/afyx-capability-result-v1.schema.json +32 -0
- package/scripts/dogfood_engineering_comparison.mjs +303 -0
- package/scripts/engineering-assurance.json +28 -0
- package/scripts/install-afyx-context.ps1 +142 -0
- package/scripts/install-afyx-context.sh +126 -0
- package/scripts/install-afyx-forge.ps1 +265 -0
- package/scripts/install-afyx-forge.sh +273 -0
- package/scripts/install-afyx-graph.ps1 +571 -0
- package/scripts/install-afyx-graph.sh +438 -0
- package/scripts/install-codex-usage-tracker.ps1 +172 -0
- package/scripts/lib/AfyxComponents.psm1 +399 -0
- package/scripts/lib/AfyxSkillLifecycle.psm1 +289 -0
- package/scripts/lib/AfyxTerminal.psm1 +838 -0
- package/scripts/lib/afyx-components.sh +256 -0
- package/scripts/lib/afyx-skill-lifecycle.sh +271 -0
- package/scripts/link-global-dev.ps1 +193 -0
- package/scripts/link-global-dev.sh +130 -0
- package/scripts/phase8-paired-evidence.mjs +195 -0
- package/scripts/phase8-representative-workload.mjs +771 -0
- package/scripts/phase8-session-recorder.mjs +157 -0
- package/scripts/phase8_event_ledger.py +248 -0
- package/scripts/phase8_evidence_discovery.py +189 -0
- package/scripts/prepare-g6-package.mjs +349 -0
- package/scripts/resolve-git-release-artifacts.mjs +321 -0
- package/scripts/stage-npm-release.mjs +94 -0
- package/scripts/validate-afyx-graph.py +203 -0
- package/scripts/validate-components.py +194 -0
- package/scripts/validate-engineering-assurance.py +281 -0
- package/scripts/verify-graph-archive.mjs +64 -0
- package/skills/afyx-context/SKILL.md +26 -0
- package/skills/afyx-engineering-expert/README.md +42 -0
- package/skills/afyx-engineering-expert/SKILL.md +70 -0
- package/skills/afyx-engineering-expert/assets/result-template.md +15 -0
- package/skills/afyx-engineering-expert/references/ai-ml-engineer.md +17 -0
- package/skills/afyx-engineering-expert/references/business-analyst.md +17 -0
- package/skills/afyx-engineering-expert/references/clean-code-engineer.md +26 -0
- package/skills/afyx-engineering-expert/references/code-reviewer.md +24 -0
- package/skills/afyx-engineering-expert/references/cybersecurity-devsecops.md +17 -0
- package/skills/afyx-engineering-expert/references/data-analyst-bi.md +24 -0
- package/skills/afyx-engineering-expert/references/data-engineer.md +24 -0
- package/skills/afyx-engineering-expert/references/database-administrator.md +17 -0
- package/skills/afyx-engineering-expert/references/debugging-specialist.md +24 -0
- package/skills/afyx-engineering-expert/references/embedded-iot-engineer.md +24 -0
- package/skills/afyx-engineering-expert/references/erp-technical-specialist.md +17 -0
- package/skills/afyx-engineering-expert/references/hardware-diagnostics-auditor.md +24 -0
- package/skills/afyx-engineering-expert/references/hardware-electronics-engineer.md +24 -0
- package/skills/afyx-engineering-expert/references/integration-engineer.md +24 -0
- package/skills/afyx-engineering-expert/references/legacy-modernization-engineer.md +24 -0
- package/skills/afyx-engineering-expert/references/llmops-prompt-engineer.md +24 -0
- package/skills/afyx-engineering-expert/references/network-engineer.md +24 -0
- package/skills/afyx-engineering-expert/references/performance-benchmark-engineer.md +24 -0
- package/skills/afyx-engineering-expert/references/platform-engineer.md +24 -0
- package/skills/afyx-engineering-expert/references/product-manager.md +17 -0
- package/skills/afyx-engineering-expert/references/qa-engineer.md +17 -0
- package/skills/afyx-engineering-expert/references/site-reliability-engineer.md +17 -0
- package/skills/afyx-engineering-expert/references/software-engineer.md +17 -0
- package/skills/afyx-engineering-expert/references/solutions-architect.md +17 -0
- package/skills/afyx-engineering-expert/references/specialists-index.md +33 -0
- package/skills/afyx-engineering-expert/references/subject-matter-expert.md +17 -0
- package/skills/afyx-engineering-expert/references/system-analyst.md +17 -0
- package/skills/afyx-engineering-expert/references/technical-project-manager.md +24 -0
- package/skills/afyx-engineering-expert/references/technical-writer.md +24 -0
- package/skills/afyx-engineering-expert/references/ui-ux-designer.md +17 -0
- package/skills/afyx-engineering-expert/workflows/ai-data-erp.md +20 -0
- package/skills/afyx-engineering-expert/workflows/debugging.md +10 -0
- package/skills/afyx-engineering-expert/workflows/discovery-design.md +11 -0
- package/skills/afyx-engineering-expert/workflows/implementation.md +12 -0
- package/skills/afyx-engineering-expert/workflows/incident-reliability.md +12 -0
- package/skills/afyx-engineering-expert/workflows/review-audit.md +13 -0
- package/skills/afyx-forge/SKILL.md +43 -0
- package/skills/afyx-forge/references/adaptive-policy.md +19 -0
- package/skills/efficient-coding/SKILL.md +33 -0
- package/skills/efficient-coding/references/investigation.md +27 -0
- package/skills/efficient-coding/references/token-efficiency.md +25 -0
- package/skills/efficient-coding/references/tool-routing.md +13 -0
- package/skills/odoo-engineering/SKILL.md +33 -0
- package/skills/odoo-engineering/references/common.md +9 -0
- package/skills/odoo-engineering/references/odoo-10.md +32 -0
- package/skills/odoo-engineering/references/odoo-11.md +26 -0
- package/skills/odoo-engineering/references/odoo-12.md +31 -0
- package/skills/odoo-engineering/references/odoo-13.md +27 -0
- package/skills/odoo-engineering/references/odoo-14.md +31 -0
- package/skills/odoo-engineering/references/odoo-15.md +34 -0
- package/skills/odoo-engineering/references/odoo-16.md +37 -0
- package/skills/odoo-engineering/references/odoo-17.md +37 -0
- package/skills/odoo-engineering/references/odoo-18.md +48 -0
- package/skills/odoo-engineering/references/odoo-19.md +43 -0
- package/skills/odoo-engineering/references/odoo-20.md +39 -0
- package/skills/odoo-engineering/references/reference-schema.md +16 -0
- package/skills/odoo-engineering/references/version-detection.md +13 -0
- package/tools/codex-usage/CodexUsage.psm1 +496 -0
- package/tools/codex-usage/codex-usage-doctor.ps1 +150 -0
- package/tools/codex-usage/codex-usage-pricing.json +25 -0
- package/tools/codex-usage/codex-usage-stop.ps1 +167 -0
- package/tools/codex-usage/codex-usage-watch.ps1 +250 -0
- package/tools/codex-usage/vscode-user-task.json +31 -0
- package/tools/windows-setup/SetupBootstrapper.cs +258 -0
- package/uninstall.ps1 +189 -0
- package/uninstall.sh +167 -0
- package/update.ps1 +95 -0
- package/update.sh +67 -0
- package/verify.ps1 +411 -0
- package/verify.sh +265 -0
|
@@ -0,0 +1,724 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/** Provider-aware, consent-safe Afyx MCP lifecycle. */
|
|
3
|
+
import {
|
|
4
|
+
copyFileSync,
|
|
5
|
+
existsSync,
|
|
6
|
+
lstatSync,
|
|
7
|
+
mkdirSync,
|
|
8
|
+
readFileSync,
|
|
9
|
+
realpathSync,
|
|
10
|
+
renameSync,
|
|
11
|
+
rmSync,
|
|
12
|
+
writeFileSync,
|
|
13
|
+
} from 'node:fs';
|
|
14
|
+
import { basename, dirname, isAbsolute, relative, resolve } from 'node:path';
|
|
15
|
+
import { homedir } from 'node:os';
|
|
16
|
+
import { fileURLToPath } from 'node:url';
|
|
17
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
18
|
+
import { integrate as integrateCodex, probeMcp } from './afyx-mcp-integration.mjs';
|
|
19
|
+
import { inspectClaude, integrateClaude } from './afyx-claude-integration.mjs';
|
|
20
|
+
import {
|
|
21
|
+
createConfigurationBackup,
|
|
22
|
+
updateKnownState,
|
|
23
|
+
rollbackConfiguration,
|
|
24
|
+
} from './afyx-provider-backup.mjs';
|
|
25
|
+
import {
|
|
26
|
+
discoverProviderCapabilities,
|
|
27
|
+
negotiateProviderCapabilities,
|
|
28
|
+
} from './afyx-integration-contracts.mjs';
|
|
29
|
+
|
|
30
|
+
function serverName(options) {
|
|
31
|
+
return options.server || 'afyx_graph';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function launchArgs(options, provider) {
|
|
35
|
+
if (Array.isArray(options.args)) return options.args.map(String);
|
|
36
|
+
if (options.mode === 'workflow') {
|
|
37
|
+
return [
|
|
38
|
+
canonicalPath(options.entry), 'serve', '--mcp',
|
|
39
|
+
'--install-root', canonicalPath(options.installRoot),
|
|
40
|
+
'--project-root', canonicalPath(options.workspaceRoot),
|
|
41
|
+
'--provider', provider,
|
|
42
|
+
'--authorize', options.authorize || 'afyx-graph,afyx-context',
|
|
43
|
+
];
|
|
44
|
+
}
|
|
45
|
+
return [canonicalPath(options.entry), 'serve', '--mcp'];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function expectedTool(options) {
|
|
49
|
+
return options.tool || (options.mode === 'workflow' ? 'afyx_workflow' : 'afyx_graph_explore');
|
|
50
|
+
}
|
|
51
|
+
const COMPONENT_CONTRACT = JSON.parse(readFileSync(fileURLToPath(new URL('./components.json', import.meta.url)), 'utf8'));
|
|
52
|
+
const PROVIDER_DEFINITIONS = new Map(COMPONENT_CONTRACT.providers.map((definition) => [definition.provider, definition]));
|
|
53
|
+
|
|
54
|
+
export const PROVIDER_ADAPTER_CONTRACT = Object.freeze({
|
|
55
|
+
name: 'ProviderAdapter',
|
|
56
|
+
version: 1,
|
|
57
|
+
operations: Object.freeze(['inspect', 'plan', 'apply', 'verify', 'uninstall']),
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
function argument(name, fallback = '') {
|
|
61
|
+
const index = process.argv.indexOf(name);
|
|
62
|
+
return index >= 0 ? process.argv[index + 1] : fallback;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function same(left, right) {
|
|
66
|
+
return isDeepStrictEqual(left, right);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function claimedEntryEquivalent(actual, claimed) {
|
|
70
|
+
if (!actual || typeof actual !== 'object' || Array.isArray(actual)) return false;
|
|
71
|
+
if (actual.disabled !== undefined && typeof actual.disabled !== 'boolean') return false;
|
|
72
|
+
const { disabled: _disabled, ...nativeEntry } = actual;
|
|
73
|
+
return same(claimed, nativeEntry);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function normalizedPath(value) {
|
|
77
|
+
const absolute = canonicalPath(value);
|
|
78
|
+
return process.platform === 'win32' ? absolute.toLowerCase() : absolute;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function canonicalPath(value) {
|
|
82
|
+
const absolute = resolve(String(value || ''));
|
|
83
|
+
return existsSync(absolute) ? realpathSync.native(absolute) : absolute;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function entryEquivalent(actual, expected) {
|
|
87
|
+
return actual && typeof actual === 'object' && !Array.isArray(actual)
|
|
88
|
+
&& normalizedPath(actual.command) === normalizedPath(expected.command)
|
|
89
|
+
&& Array.isArray(actual.args) && actual.args.length === expected.args.length
|
|
90
|
+
&& actual.args.every((value, index) => normalizedPath(value) === normalizedPath(expected.args[index]));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function inside(base, candidate) {
|
|
94
|
+
const rel = relative(resolve(base), resolve(candidate));
|
|
95
|
+
return rel === '' || (!rel.startsWith('..') && !isAbsolute(rel));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function assertSafePath(base, candidate) {
|
|
99
|
+
if (!inside(base, candidate)) throw new Error(`PROVIDER_PATH_OUTSIDE_SCOPE: ${candidate}`);
|
|
100
|
+
const boundary = resolve(base);
|
|
101
|
+
let cursor = resolve(candidate);
|
|
102
|
+
for (;;) {
|
|
103
|
+
if (existsSync(cursor) && lstatSync(cursor).isSymbolicLink()) {
|
|
104
|
+
throw new Error(`PROVIDER_PATH_REPARSE_BLOCKED: ${cursor}`);
|
|
105
|
+
}
|
|
106
|
+
if (cursor === boundary) break;
|
|
107
|
+
const parent = dirname(cursor);
|
|
108
|
+
if (parent === cursor) break;
|
|
109
|
+
cursor = parent;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function parseObject(file, optional = true) {
|
|
114
|
+
if (!existsSync(file)) {
|
|
115
|
+
if (optional) return {};
|
|
116
|
+
throw new Error(`PROVIDER_FILE_MISSING: ${file}`);
|
|
117
|
+
}
|
|
118
|
+
let value;
|
|
119
|
+
try {
|
|
120
|
+
value = JSON.parse(readFileSync(file, 'utf8'));
|
|
121
|
+
} catch (error) {
|
|
122
|
+
throw new Error(`PROVIDER_INVALID_JSON: ${file}: ${error instanceof Error ? error.message : String(error)}`);
|
|
123
|
+
}
|
|
124
|
+
if (!value || Array.isArray(value) || typeof value !== 'object') {
|
|
125
|
+
throw new Error(`PROVIDER_INVALID_JSON_ROOT: ${file}`);
|
|
126
|
+
}
|
|
127
|
+
return value;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function atomicJson(file, value) {
|
|
131
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
132
|
+
const temporary = `${file}.afyx-tmp-${process.pid}-${Date.now()}`;
|
|
133
|
+
try {
|
|
134
|
+
writeFileSync(temporary, `${JSON.stringify(value, null, 2)}\n`, { encoding: 'utf8', flag: 'wx' });
|
|
135
|
+
renameSync(temporary, file);
|
|
136
|
+
} finally {
|
|
137
|
+
rmSync(temporary, { force: true });
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function snapshot(file) {
|
|
142
|
+
return existsSync(file) ? readFileSync(file) : null;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function restore(file, bytes) {
|
|
146
|
+
if (bytes === null) rmSync(file, { force: true });
|
|
147
|
+
else {
|
|
148
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
149
|
+
writeFileSync(file, bytes);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function restoreGuarded(file, before, lastKnown) {
|
|
154
|
+
if (!sameBytes(snapshot(file), lastKnown)) return false;
|
|
155
|
+
restore(file, before);
|
|
156
|
+
return sameBytes(snapshot(file), before);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function antigravityPaths(options) {
|
|
160
|
+
const scope = options.scope || 'global';
|
|
161
|
+
if (scope === 'workspace') {
|
|
162
|
+
const base = resolve(options.workspaceRoot);
|
|
163
|
+
return { base, config: resolve(base, '.agents', 'mcp_config.json') };
|
|
164
|
+
}
|
|
165
|
+
if (scope !== 'global') throw new Error(`PROVIDER_SCOPE_UNSUPPORTED: ${scope}`);
|
|
166
|
+
const base = resolve(options.geminiHome || resolve(homedir(), '.gemini'));
|
|
167
|
+
return { base, config: resolve(base, 'config', 'mcp_config.json') };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function expectedEntry(options, provider = 'antigravity') {
|
|
171
|
+
return { command: canonicalPath(options.node), args: launchArgs(options, provider) };
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function inspectAntigravity(options) {
|
|
175
|
+
const server = serverName(options);
|
|
176
|
+
const paths = antigravityPaths(options);
|
|
177
|
+
const ownership = `${paths.config}.afyx-ownership.json`;
|
|
178
|
+
assertSafePath(paths.base, paths.config);
|
|
179
|
+
assertSafePath(paths.base, ownership);
|
|
180
|
+
const document = parseObject(paths.config);
|
|
181
|
+
if (document.mcpServers !== undefined && (!document.mcpServers || Array.isArray(document.mcpServers) || typeof document.mcpServers !== 'object')) {
|
|
182
|
+
throw new Error(`PROVIDER_INVALID_MCP_SERVERS: ${paths.config}`);
|
|
183
|
+
}
|
|
184
|
+
const marker = parseObject(ownership);
|
|
185
|
+
if ((options.scope || 'global') === 'global') {
|
|
186
|
+
const legacy = resolve(options.geminiHome || resolve(homedir(), '.gemini'), 'antigravity', 'mcp_config.json');
|
|
187
|
+
assertSafePath(paths.base, legacy);
|
|
188
|
+
const legacyDocument = parseObject(legacy);
|
|
189
|
+
if (legacyDocument.mcpServers?.[server]) {
|
|
190
|
+
return { paths, ownership, document, marker, actual: null, expected: expectedEntry(options), claim: null, state: 'MCP_BLOCKED_LEGACY_DUPLICATE', owned: false, legacy, server };
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
const actual = document.mcpServers?.[server] ?? null;
|
|
194
|
+
const expected = expectedEntry(options);
|
|
195
|
+
const claim = marker.entries?.[server] ?? null;
|
|
196
|
+
const claimValid = claim?.provider === 'antigravity' && resolve(claim.configPath || '') === paths.config;
|
|
197
|
+
let state = 'MCP_NOT_CONFIGURED';
|
|
198
|
+
let owned = false;
|
|
199
|
+
if (actual) {
|
|
200
|
+
const claimed = claimValid && claimedEntryEquivalent(actual, claim.entry);
|
|
201
|
+
if (actual.disabled === true) {
|
|
202
|
+
state = claimed ? 'MCP_DISABLED_OWNED' : 'MCP_DISABLED_UNOWNED';
|
|
203
|
+
owned = claimed;
|
|
204
|
+
} else if (entryEquivalent(actual, expected)) {
|
|
205
|
+
state = claimed ? 'MCP_CONFIGURED' : 'MCP_CONFIGURED_UNOWNED';
|
|
206
|
+
owned = state === 'MCP_CONFIGURED';
|
|
207
|
+
} else if (claimed) {
|
|
208
|
+
state = 'MCP_STALE_OWNED';
|
|
209
|
+
owned = true;
|
|
210
|
+
} else {
|
|
211
|
+
state = 'MCP_BLOCKED';
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return { paths, ownership, document, marker, actual, expected, claim, state, owned, server };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function planForAntigravity(options, inspection) {
|
|
218
|
+
const action = options.uninstall
|
|
219
|
+
? (inspection.state === 'MCP_NOT_CONFIGURED' ? 'NONE' : inspection.owned ? 'REMOVE' : 'PRESERVE_FOREIGN')
|
|
220
|
+
: (inspection.state === 'MCP_NOT_CONFIGURED' ? 'CREATE' : inspection.state === 'MCP_STALE_OWNED' ? 'UPDATE' : 'NONE');
|
|
221
|
+
return {
|
|
222
|
+
provider: 'antigravity',
|
|
223
|
+
scope: options.scope,
|
|
224
|
+
configPath: inspection.paths.config,
|
|
225
|
+
ownershipPath: inspection.ownership,
|
|
226
|
+
state: inspection.state,
|
|
227
|
+
action,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export async function integrateAntigravity(options) {
|
|
232
|
+
let inspection;
|
|
233
|
+
try {
|
|
234
|
+
inspection = inspectAntigravity(options);
|
|
235
|
+
} catch (error) {
|
|
236
|
+
return { provider: 'antigravity', status: 'BLOCKED', changed: false, reason: 'INSPECTION_FAILED', detail: error instanceof Error ? error.message : String(error) };
|
|
237
|
+
}
|
|
238
|
+
const plan = planForAntigravity(options, inspection);
|
|
239
|
+
const result = { ...plan, status: 'CONFIGURED_RESTART_REQUIRED', changed: false, reason: '', detail: '' };
|
|
240
|
+
if (inspection.state === 'MCP_BLOCKED' || inspection.state === 'MCP_BLOCKED_LEGACY_DUPLICATE') {
|
|
241
|
+
const legacy = inspection.state === 'MCP_BLOCKED_LEGACY_DUPLICATE';
|
|
242
|
+
return { ...result, status: 'BLOCKED', reason: legacy ? 'LEGACY_DUPLICATE' : 'FOREIGN_ENTRY', detail: legacy ? `Legacy afyx_graph entry at ${inspection.legacy} was preserved; resolve it explicitly before unified registration.` : 'Existing unowned afyx_graph entry was preserved.' };
|
|
243
|
+
}
|
|
244
|
+
if (!options.uninstall && inspection.state.startsWith('MCP_DISABLED_')) {
|
|
245
|
+
return {
|
|
246
|
+
...result,
|
|
247
|
+
status: 'BLOCKED',
|
|
248
|
+
reason: 'EXPLICITLY_DISABLED',
|
|
249
|
+
detail: 'The native provider entry is explicitly disabled and was preserved unchanged.',
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
if (options.uninstall) {
|
|
253
|
+
if (inspection.state === 'MCP_NOT_CONFIGURED') return { ...result, status: 'UNAVAILABLE', detail: 'No provider entry is configured.' };
|
|
254
|
+
if (!inspection.owned) return { ...result, status: 'BLOCKED', reason: 'UNOWNED_ENTRY', detail: 'Matching but unowned entry was preserved.' };
|
|
255
|
+
if (!options.apply) return { ...result, status: 'CONFIGURED', detail: 'Removal is planned but not consented.' };
|
|
256
|
+
const backup = createConfigurationBackup({
|
|
257
|
+
provider: 'antigravity',
|
|
258
|
+
configPath: inspection.paths.config,
|
|
259
|
+
operation: 'antigravity-mcp-remove',
|
|
260
|
+
});
|
|
261
|
+
const markerBefore = snapshot(inspection.ownership);
|
|
262
|
+
let markerKnown = markerBefore;
|
|
263
|
+
try {
|
|
264
|
+
delete inspection.document.mcpServers[inspection.server];
|
|
265
|
+
if (Object.keys(inspection.document.mcpServers).length === 0) delete inspection.document.mcpServers;
|
|
266
|
+
if (inspection.marker.entries) {
|
|
267
|
+
delete inspection.marker.entries[inspection.server];
|
|
268
|
+
if (Object.keys(inspection.marker.entries).length === 0) delete inspection.marker.entries;
|
|
269
|
+
}
|
|
270
|
+
atomicJson(inspection.paths.config, inspection.document);
|
|
271
|
+
updateKnownState(backup);
|
|
272
|
+
if (inspection.marker.entries) atomicJson(inspection.ownership, inspection.marker);
|
|
273
|
+
else rmSync(inspection.ownership, { force: true });
|
|
274
|
+
markerKnown = snapshot(inspection.ownership);
|
|
275
|
+
const after = inspectAntigravity(options);
|
|
276
|
+
if (after.state !== 'MCP_NOT_CONFIGURED') throw new Error('Provider removal read-back failed.');
|
|
277
|
+
return { ...result, status: 'UNAVAILABLE', changed: true, detail: 'Owned provider entry removed; unrelated configuration preserved.' };
|
|
278
|
+
} catch (error) {
|
|
279
|
+
const rollback = rollbackConfiguration(backup, { reason: error instanceof Error ? error.message : String(error) });
|
|
280
|
+
const markerRestored = restoreGuarded(inspection.ownership, markerBefore, markerKnown);
|
|
281
|
+
const restored = rollback.restored && markerRestored;
|
|
282
|
+
return {
|
|
283
|
+
...result,
|
|
284
|
+
status: 'BLOCKED',
|
|
285
|
+
changed: !restored,
|
|
286
|
+
reason: rollback.conflict ? 'CONCURRENT_MODIFICATION_DETECTED' : (restored ? 'ROLLBACK_COMPLETED' : 'ROLLBACK_INCOMPLETE'),
|
|
287
|
+
detail: `${error instanceof Error ? error.message : String(error)}${restored ? ' (configuration restored safely)' : (rollback.conflict ? '; ' + rollback.reason : '; rollback was incomplete')}`,
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
if (!existsSync(options.node) || !existsSync(options.entry)) {
|
|
292
|
+
return { ...result, status: 'BLOCKED', reason: 'MCP_RUNTIME_UNAVAILABLE', detail: 'Absolute Node.js or MCP entry is unavailable.' };
|
|
293
|
+
}
|
|
294
|
+
if (!options.apply && ['MCP_NOT_CONFIGURED', 'MCP_STALE_OWNED'].includes(inspection.state)) {
|
|
295
|
+
return { ...result, status: 'DETECTED', detail: 'Configuration change is planned but not consented.' };
|
|
296
|
+
}
|
|
297
|
+
const probe = await probeMcp(resolve(options.node), resolve(options.entry), {
|
|
298
|
+
args: launchArgs(options, 'antigravity'), tool: expectedTool(options),
|
|
299
|
+
});
|
|
300
|
+
if (!probe.ok) return { ...result, status: 'BLOCKED', reason: 'MCP_HANDSHAKE_FAILED', detail: probe.detail };
|
|
301
|
+
if (options.apply && ['MCP_NOT_CONFIGURED', 'MCP_STALE_OWNED'].includes(inspection.state)) {
|
|
302
|
+
const backup = createConfigurationBackup({
|
|
303
|
+
provider: 'antigravity',
|
|
304
|
+
configPath: inspection.paths.config,
|
|
305
|
+
operation: 'antigravity-mcp-register',
|
|
306
|
+
});
|
|
307
|
+
const markerBefore = snapshot(inspection.ownership);
|
|
308
|
+
let markerKnown = markerBefore;
|
|
309
|
+
try {
|
|
310
|
+
inspection.document.mcpServers ||= {};
|
|
311
|
+
inspection.document.mcpServers[inspection.server] = inspection.expected;
|
|
312
|
+
inspection.marker.schemaVersion = 1;
|
|
313
|
+
inspection.marker.entries ||= {};
|
|
314
|
+
inspection.marker.entries[inspection.server] = {
|
|
315
|
+
provider: 'antigravity',
|
|
316
|
+
configPath: inspection.paths.config,
|
|
317
|
+
entry: inspection.expected,
|
|
318
|
+
};
|
|
319
|
+
atomicJson(inspection.paths.config, inspection.document);
|
|
320
|
+
updateKnownState(backup);
|
|
321
|
+
atomicJson(inspection.ownership, inspection.marker);
|
|
322
|
+
markerKnown = snapshot(inspection.ownership);
|
|
323
|
+
const after = inspectAntigravity(options);
|
|
324
|
+
if (after.state !== 'MCP_CONFIGURED' || !after.owned) throw new Error('Provider configuration read-back failed.');
|
|
325
|
+
result.changed = true;
|
|
326
|
+
} catch (error) {
|
|
327
|
+
const rollback = rollbackConfiguration(backup, { reason: error instanceof Error ? error.message : String(error) });
|
|
328
|
+
const markerRestored = restoreGuarded(inspection.ownership, markerBefore, markerKnown);
|
|
329
|
+
const restored = rollback.restored && markerRestored;
|
|
330
|
+
return {
|
|
331
|
+
...result,
|
|
332
|
+
status: 'BLOCKED',
|
|
333
|
+
changed: !restored,
|
|
334
|
+
reason: rollback.conflict ? 'CONCURRENT_MODIFICATION_DETECTED' : (restored ? 'ROLLBACK_COMPLETED' : 'ROLLBACK_INCOMPLETE'),
|
|
335
|
+
detail: `${error instanceof Error ? error.message : String(error)}${restored ? ' (configuration restored safely)' : (rollback.conflict ? '; ' + rollback.reason : '; rollback was incomplete')}`,
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
result.status = options.scope === 'global' ? 'CONFIGURED_RESTART_REQUIRED' : 'READY';
|
|
340
|
+
result.detail = `${probe.detail} Provider configuration read-back matched the absolute runtime entry.`;
|
|
341
|
+
return result;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
async function integrateCodexAdapter(options) {
|
|
345
|
+
if (options.scope !== 'global') {
|
|
346
|
+
return {
|
|
347
|
+
provider: 'codex', status: 'INCOMPATIBLE', changed: false,
|
|
348
|
+
reason: 'SCOPE_UNSUPPORTED',
|
|
349
|
+
detail: 'Codex direct MCP registration is user-scoped; use a session override or an installed plugin for isolated project acceptance.',
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
const codexConfigRoot = resolve(options.codexHome);
|
|
353
|
+
const codex = await integrateCodex({
|
|
354
|
+
node: resolve(options.node), entry: resolve(options.entry), codex: options.codex,
|
|
355
|
+
codexHome: codexConfigRoot, apply: Boolean(options.apply), uninstall: Boolean(options.uninstall),
|
|
356
|
+
providerTimeoutMs: options.providerTimeoutMs, server: serverName(options),
|
|
357
|
+
args: launchArgs(options, 'codex'), tool: expectedTool(options),
|
|
358
|
+
});
|
|
359
|
+
return {
|
|
360
|
+
provider: 'codex',
|
|
361
|
+
status: codex.mcp === 'MCP_REACHABLE' ? 'READY'
|
|
362
|
+
: codex.mcp === 'MCP_NOT_CONFIGURED' ? (options.uninstall ? 'UNAVAILABLE' : 'DETECTED')
|
|
363
|
+
: options.uninstall && codex.mcp === 'MCP_CONFIGURED' ? 'CONFIGURED'
|
|
364
|
+
: 'BLOCKED',
|
|
365
|
+
configPath: resolve(codexConfigRoot, 'config.toml'),
|
|
366
|
+
ownershipPath: resolve(codexConfigRoot, '.afyx-provider-ownership.json'),
|
|
367
|
+
...codex,
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
function defineProviderAdapter(provider, scopes, execute) {
|
|
372
|
+
const definition = PROVIDER_DEFINITIONS.get(provider);
|
|
373
|
+
const invoke = (operation, options) => {
|
|
374
|
+
const lifecycle = {
|
|
375
|
+
...options,
|
|
376
|
+
operation,
|
|
377
|
+
apply: operation === 'apply' || (operation === 'uninstall' && Boolean(options.apply)),
|
|
378
|
+
uninstall: operation === 'uninstall',
|
|
379
|
+
};
|
|
380
|
+
return execute(lifecycle);
|
|
381
|
+
};
|
|
382
|
+
const capabilities = Object.freeze({
|
|
383
|
+
scopes: Object.freeze([...scopes]),
|
|
384
|
+
operations: PROVIDER_ADAPTER_CONTRACT.operations,
|
|
385
|
+
configurationPolicy: 'merge-afyx-owned-entries-only',
|
|
386
|
+
});
|
|
387
|
+
const observeHost = (options = {}) => {
|
|
388
|
+
if (options.host !== undefined) return options.host;
|
|
389
|
+
if (provider === 'claude' && (options.extensionOnly || options.claudeExtension)) {
|
|
390
|
+
return {
|
|
391
|
+
detected: true,
|
|
392
|
+
command: 'extension',
|
|
393
|
+
version: options.hostVersion ?? null,
|
|
394
|
+
evidence: 'VSCODE_EXTENSION',
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
const candidate = provider === 'codex' ? options.codex
|
|
398
|
+
: provider === 'claude' ? options.claude : options.antigravity;
|
|
399
|
+
if (!candidate) return undefined;
|
|
400
|
+
return {
|
|
401
|
+
detected: existsSync(candidate),
|
|
402
|
+
command: basename(candidate),
|
|
403
|
+
version: options.hostVersion ?? null,
|
|
404
|
+
evidence: 'FILESYSTEM',
|
|
405
|
+
};
|
|
406
|
+
};
|
|
407
|
+
const discover = (options = {}) => discoverProviderCapabilities(definition, capabilities, { host: observeHost(options) });
|
|
408
|
+
return Object.freeze({
|
|
409
|
+
provider,
|
|
410
|
+
contract: PROVIDER_ADAPTER_CONTRACT,
|
|
411
|
+
capabilities,
|
|
412
|
+
discover,
|
|
413
|
+
negotiate: (requirements, options = {}) => negotiateProviderCapabilities(discover(options), requirements),
|
|
414
|
+
inspect: (options) => invoke('inspect', options),
|
|
415
|
+
plan: (options) => invoke('plan', options),
|
|
416
|
+
apply: (options) => invoke('apply', options),
|
|
417
|
+
verify: (options) => invoke('verify', options),
|
|
418
|
+
uninstall: (options) => invoke('uninstall', options),
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
export const providerAdapters = Object.freeze({
|
|
423
|
+
codex: defineProviderAdapter('codex', ['global'], integrateCodexAdapter),
|
|
424
|
+
antigravity: defineProviderAdapter('antigravity', ['global', 'workspace'], integrateAntigravity),
|
|
425
|
+
claude: defineProviderAdapter('claude', ['global', 'workspace'], integrateClaude),
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
export function getProviderAdapter(provider) {
|
|
429
|
+
return Object.hasOwn(providerAdapters, provider) ? providerAdapters[provider] : null;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
function unsupportedScopeResult(provider, scope) {
|
|
433
|
+
return {
|
|
434
|
+
provider, status: 'INCOMPATIBLE', changed: false, reason: 'SCOPE_UNSUPPORTED',
|
|
435
|
+
detail: provider === 'codex'
|
|
436
|
+
? 'Codex adapter currently supports its user-scoped native CLI configuration.'
|
|
437
|
+
: `Provider ${provider} does not support the requested ${scope} scope.`,
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
function prevalidateProviderSelection(selected, scope) {
|
|
442
|
+
return selected.map((provider) => {
|
|
443
|
+
const adapter = getProviderAdapter(provider);
|
|
444
|
+
if (!adapter) {
|
|
445
|
+
return {
|
|
446
|
+
provider,
|
|
447
|
+
adapter: null,
|
|
448
|
+
failure: { provider, status: 'BLOCKED', changed: false, reason: 'UNKNOWN_PROVIDER', detail: `Unknown provider: ${provider}` },
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
if (!adapter.capabilities.scopes.includes(scope)) {
|
|
452
|
+
return { provider, adapter, failure: unsupportedScopeResult(provider, scope) };
|
|
453
|
+
}
|
|
454
|
+
return { provider, adapter, failure: null };
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
function prevalidationBlockedResult(provider) {
|
|
459
|
+
return {
|
|
460
|
+
provider, status: 'BLOCKED', changed: false, reason: 'PREVALIDATION_FAILED',
|
|
461
|
+
detail: 'No provider mutation was executed because the selected provider set failed prevalidation.',
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
function sameBytes(left, right) {
|
|
466
|
+
if (left === null || right === null) return left === right;
|
|
467
|
+
return left.equals(right);
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
function providerRecoveryFiles(provider, options) {
|
|
471
|
+
if (provider === 'codex') {
|
|
472
|
+
const base = resolve(options.codexHome);
|
|
473
|
+
const files = [resolve(base, 'config.toml'), resolve(base, '.afyx-provider-ownership.json')];
|
|
474
|
+
for (const file of files) assertSafePath(base, file);
|
|
475
|
+
return { base, files };
|
|
476
|
+
}
|
|
477
|
+
if (provider === 'antigravity') {
|
|
478
|
+
const inspection = inspectAntigravity(options);
|
|
479
|
+
return { base: inspection.paths.base, files: [inspection.paths.config, inspection.ownership] };
|
|
480
|
+
}
|
|
481
|
+
if (provider === 'claude') {
|
|
482
|
+
const inspection = inspectClaude(options);
|
|
483
|
+
return { base: inspection.paths.base, files: [inspection.paths.config, inspection.paths.ownership] };
|
|
484
|
+
}
|
|
485
|
+
throw new Error(`UNKNOWN_PROVIDER: ${provider}`);
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
function captureProviderState(provider, options) {
|
|
489
|
+
const recovery = providerRecoveryFiles(provider, options);
|
|
490
|
+
return {
|
|
491
|
+
provider,
|
|
492
|
+
base: recovery.base,
|
|
493
|
+
files: recovery.files.map((file) => ({ file, before: snapshot(file), after: null })),
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
function sealProviderState(capture) {
|
|
498
|
+
let changed = false;
|
|
499
|
+
for (const item of capture.files) {
|
|
500
|
+
item.after = snapshot(item.file);
|
|
501
|
+
changed ||= !sameBytes(item.before, item.after);
|
|
502
|
+
}
|
|
503
|
+
return changed;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
function rollbackProviderState(capture) {
|
|
507
|
+
try {
|
|
508
|
+
for (const item of capture.files) {
|
|
509
|
+
assertSafePath(capture.base, item.file);
|
|
510
|
+
if (!sameBytes(snapshot(item.file), item.after)) {
|
|
511
|
+
return { status: 'INCOMPLETE', detail: 'Configuration changed after provider execution; exact-byte rollback was refused.' };
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
for (const item of [...capture.files].reverse()) restore(item.file, item.before);
|
|
515
|
+
if (capture.files.some((item) => !sameBytes(snapshot(item.file), item.before))) {
|
|
516
|
+
return { status: 'INCOMPLETE', detail: 'Configuration rollback read-back did not match the captured state.' };
|
|
517
|
+
}
|
|
518
|
+
return { status: 'RESTORED', detail: 'Owned configuration changes were restored to the exact captured bytes.' };
|
|
519
|
+
} catch (error) {
|
|
520
|
+
return { status: 'INCOMPLETE', detail: `Configuration rollback failed closed: ${error instanceof Error ? error.message : String(error)}` };
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
function validateRecoveryPathIsolation(captures) {
|
|
525
|
+
const owners = new Map();
|
|
526
|
+
for (const capture of captures) {
|
|
527
|
+
for (const item of capture.files) {
|
|
528
|
+
const key = normalizedPath(item.file);
|
|
529
|
+
const owner = owners.get(key);
|
|
530
|
+
if (owner && owner !== capture.provider) {
|
|
531
|
+
throw new Error(`RECOVERY_PATH_CONFLICT: ${owner} and ${capture.provider}`);
|
|
532
|
+
}
|
|
533
|
+
owners.set(key, capture.provider);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
function operationSucceeded(result, uninstall) {
|
|
539
|
+
const accepted = uninstall
|
|
540
|
+
? new Set(['UNAVAILABLE'])
|
|
541
|
+
: new Set(['READY', 'CONFIGURED_RESTART_REQUIRED']);
|
|
542
|
+
return accepted.has(result.status);
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
function aggregateStatus(results, uninstall = false) {
|
|
546
|
+
if (results.some((item) => item.status === 'BLOCKED')) return 'BLOCKED';
|
|
547
|
+
if (uninstall) {
|
|
548
|
+
return results.every((item) => item.status === 'UNAVAILABLE') ? 'READY' : 'PARTIAL';
|
|
549
|
+
}
|
|
550
|
+
return results.some((item) => item.status === 'CONFIGURED_RESTART_REQUIRED') ? 'CONFIGURED_RESTART_REQUIRED'
|
|
551
|
+
: results.every((item) => item.status === 'READY') ? 'READY' : 'PARTIAL';
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
function recoveryBlockedResult(provider, reason, detail) {
|
|
555
|
+
return { provider, status: 'BLOCKED', changed: false, reason, detail };
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
async function runTransactionalLifecycle(options, selection) {
|
|
559
|
+
const operation = options.uninstall ? 'uninstall' : 'apply';
|
|
560
|
+
const preflightOperation = options.uninstall ? 'uninstall' : 'plan';
|
|
561
|
+
const preflightOptions = { ...options, apply: false };
|
|
562
|
+
const preflights = [];
|
|
563
|
+
for (const item of selection) preflights.push(await item.adapter[preflightOperation](preflightOptions));
|
|
564
|
+
const failedPreflight = preflights.findIndex((result) => ['BLOCKED', 'INCOMPATIBLE'].includes(result.status));
|
|
565
|
+
if (failedPreflight >= 0) {
|
|
566
|
+
return {
|
|
567
|
+
providers: selection.map((item, index) => ['BLOCKED', 'INCOMPATIBLE'].includes(preflights[index].status)
|
|
568
|
+
? preflights[index]
|
|
569
|
+
: recoveryBlockedResult(item.provider, 'PREFLIGHT_FAILED', 'No provider mutation was executed because cross-provider preflight failed.')),
|
|
570
|
+
status: 'BLOCKED',
|
|
571
|
+
recovery: { status: 'NOT_STARTED', failedProvider: selection[failedPreflight].provider },
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
const captures = [];
|
|
576
|
+
try {
|
|
577
|
+
for (const item of selection) captures.push(captureProviderState(item.provider, options));
|
|
578
|
+
validateRecoveryPathIsolation(captures);
|
|
579
|
+
} catch (error) {
|
|
580
|
+
return {
|
|
581
|
+
providers: selection.map((item) => recoveryBlockedResult(
|
|
582
|
+
item.provider,
|
|
583
|
+
'RECOVERY_SNAPSHOT_FAILED',
|
|
584
|
+
`No provider mutation was executed: ${error instanceof Error ? error.message : String(error)}`,
|
|
585
|
+
)),
|
|
586
|
+
status: 'BLOCKED',
|
|
587
|
+
recovery: { status: 'NOT_STARTED' },
|
|
588
|
+
};
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
const results = [];
|
|
592
|
+
const changedIndexes = [];
|
|
593
|
+
for (let index = 0; index < selection.length; index += 1) {
|
|
594
|
+
const item = selection[index];
|
|
595
|
+
let result;
|
|
596
|
+
try {
|
|
597
|
+
result = await item.adapter[operation](options);
|
|
598
|
+
} catch (error) {
|
|
599
|
+
result = recoveryBlockedResult(item.provider, 'PROVIDER_EXECUTION_FAILED', error instanceof Error ? error.message : String(error));
|
|
600
|
+
}
|
|
601
|
+
const observedChanged = sealProviderState(captures[index]);
|
|
602
|
+
if (observedChanged !== Boolean(result.changed)) {
|
|
603
|
+
result = {
|
|
604
|
+
...result,
|
|
605
|
+
status: 'BLOCKED',
|
|
606
|
+
changed: observedChanged || Boolean(result.changed),
|
|
607
|
+
reason: 'CHANGE_REPORT_MISMATCH',
|
|
608
|
+
detail: 'Provider-reported mutation did not match its declared configuration recovery boundary.',
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
results.push(result);
|
|
612
|
+
if (selection.length === 1 && result.status === 'UNVERIFIED' && !observedChanged) continue;
|
|
613
|
+
if (operationSucceeded(result, options.uninstall)) {
|
|
614
|
+
if (observedChanged) changedIndexes.push(index);
|
|
615
|
+
continue;
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
const recoveryByIndex = new Map();
|
|
619
|
+
let recoveryStatus = observedChanged ? 'INCOMPLETE' : 'NOT_REQUIRED';
|
|
620
|
+
if (observedChanged) {
|
|
621
|
+
recoveryByIndex.set(index, {
|
|
622
|
+
status: 'INCOMPLETE',
|
|
623
|
+
detail: 'The failing provider changed its recovery boundary; automatic restoration was refused because ownership of the unexpected bytes is unproven.',
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
for (const changedIndex of [...changedIndexes].reverse()) {
|
|
627
|
+
const recovery = rollbackProviderState(captures[changedIndex]);
|
|
628
|
+
recoveryByIndex.set(changedIndex, recovery);
|
|
629
|
+
if (recovery.status === 'INCOMPLETE') recoveryStatus = 'INCOMPLETE';
|
|
630
|
+
else if (recoveryStatus === 'NOT_REQUIRED') recoveryStatus = 'RESTORED';
|
|
631
|
+
}
|
|
632
|
+
const failedProvider = item.provider;
|
|
633
|
+
const completed = results.map((providerResult, resultIndex) => {
|
|
634
|
+
const recovery = recoveryByIndex.get(resultIndex);
|
|
635
|
+
if (!recovery) return providerResult;
|
|
636
|
+
if (resultIndex === index) {
|
|
637
|
+
return { ...providerResult, changed: recovery.status !== 'RESTORED', recovery };
|
|
638
|
+
}
|
|
639
|
+
return {
|
|
640
|
+
...providerResult,
|
|
641
|
+
status: 'BLOCKED',
|
|
642
|
+
changed: recovery.status !== 'RESTORED',
|
|
643
|
+
reason: recovery.status === 'RESTORED' ? 'BATCH_ROLLED_BACK' : 'BATCH_ROLLBACK_INCOMPLETE',
|
|
644
|
+
detail: recovery.detail,
|
|
645
|
+
originalStatus: providerResult.status,
|
|
646
|
+
originalReason: providerResult.reason || '',
|
|
647
|
+
recovery,
|
|
648
|
+
};
|
|
649
|
+
});
|
|
650
|
+
for (let remaining = index + 1; remaining < selection.length; remaining += 1) {
|
|
651
|
+
completed.push({
|
|
652
|
+
...recoveryBlockedResult(selection[remaining].provider, 'BATCH_ABORTED', `Provider was not executed after ${failedProvider} failed.`),
|
|
653
|
+
recovery: { status: 'NOT_RUN' },
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
return {
|
|
657
|
+
providers: completed,
|
|
658
|
+
status: 'BLOCKED',
|
|
659
|
+
recovery: { status: recoveryStatus, failedProvider },
|
|
660
|
+
};
|
|
661
|
+
}
|
|
662
|
+
return { providers: results, status: aggregateStatus(results, Boolean(options.uninstall)), recovery: { status: 'NOT_REQUIRED' } };
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
export async function runProviderLifecycle(options) {
|
|
666
|
+
const selected = [...new Set(options.providers?.length ? options.providers : ['codex'])];
|
|
667
|
+
const selection = prevalidateProviderSelection(selected, options.scope);
|
|
668
|
+
if (options.apply && selection.some((item) => item.failure)) {
|
|
669
|
+
return {
|
|
670
|
+
providers: selection.map((item) => item.failure ?? prevalidationBlockedResult(item.provider)),
|
|
671
|
+
status: 'BLOCKED',
|
|
672
|
+
recovery: { status: 'NOT_STARTED' },
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
if (options.apply) return runTransactionalLifecycle(options, selection);
|
|
676
|
+
const results = [];
|
|
677
|
+
for (const item of selection) {
|
|
678
|
+
if (item.failure) {
|
|
679
|
+
results.push(item.failure);
|
|
680
|
+
continue;
|
|
681
|
+
}
|
|
682
|
+
const operation = options.uninstall ? 'uninstall' : options.verify ? 'verify' : options.apply ? 'apply' : 'plan';
|
|
683
|
+
results.push(await item.adapter[operation](options));
|
|
684
|
+
}
|
|
685
|
+
return {
|
|
686
|
+
providers: results,
|
|
687
|
+
status: aggregateStatus(results, Boolean(options.uninstall)),
|
|
688
|
+
};
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
if (process.argv[1] && fileURLToPath(import.meta.url) === resolve(process.argv[1])) {
|
|
692
|
+
const scope = argument('--scope', 'global');
|
|
693
|
+
const workspaceRoot = resolve(argument('--workspace-root', process.cwd()));
|
|
694
|
+
const options = {
|
|
695
|
+
providers: argument('--providers', 'codex').split(',').filter(Boolean),
|
|
696
|
+
scope,
|
|
697
|
+
workspaceRoot,
|
|
698
|
+
node: argument('--node'),
|
|
699
|
+
entry: argument('--entry'),
|
|
700
|
+
server: argument('--server', 'afyx_graph'),
|
|
701
|
+
mode: argument('--mode', 'graph'),
|
|
702
|
+
installRoot: resolve(argument('--install-root', resolve(homedir(), '.afyx'))),
|
|
703
|
+
authorize: argument('--authorize', 'afyx-graph,afyx-context'),
|
|
704
|
+
tool: argument('--tool'),
|
|
705
|
+
codex: argument('--codex'),
|
|
706
|
+
codexHome: argument('--codex-home', resolve(homedir(), '.codex')),
|
|
707
|
+
geminiHome: argument('--gemini-home', resolve(homedir(), '.gemini')),
|
|
708
|
+
claude: argument('--claude'),
|
|
709
|
+
claudeConfigDir: argument('--claude-config-dir') || undefined,
|
|
710
|
+
claudeExtension: process.argv.includes('--claude-extension'),
|
|
711
|
+
extensionOnly: process.argv.includes('--extension-only'),
|
|
712
|
+
providerTimeoutMs: Number(argument('--provider-timeout-ms', '15000')),
|
|
713
|
+
apply: process.argv.includes('--apply'),
|
|
714
|
+
verify: process.argv.includes('--verify'),
|
|
715
|
+
uninstall: process.argv.includes('--uninstall'),
|
|
716
|
+
};
|
|
717
|
+
runProviderLifecycle(options).then((result) => {
|
|
718
|
+
console.log(JSON.stringify(result));
|
|
719
|
+
if (result.status === 'BLOCKED') process.exitCode = 2;
|
|
720
|
+
}).catch((error) => {
|
|
721
|
+
console.error(`[provider-integration] ${error instanceof Error ? error.message : String(error)}`);
|
|
722
|
+
process.exitCode = 1;
|
|
723
|
+
});
|
|
724
|
+
}
|