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,752 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native selective capability orchestration for all Afyx-owned components.
|
|
3
|
+
*
|
|
4
|
+
* The orchestrator is deliberately host-neutral. Hosts provide observed component
|
|
5
|
+
* state and small in-process adapters; this module discovers, selects, and invokes
|
|
6
|
+
* only the capabilities justified by the task. No component is mandatory.
|
|
7
|
+
*
|
|
8
|
+
* Conforms to AfyxUniversalInterop/v1:
|
|
9
|
+
* - Standalone by design: every component functions with all optional peers absent
|
|
10
|
+
* - Universal bidirectional interoperability: any installed, healthy, compatible,
|
|
11
|
+
* and authorized component can participate in task-aware capability exchange
|
|
12
|
+
* - Default-enabled discovery: installed components are available for interoperability
|
|
13
|
+
* by default without requiring manual pair configuration, while honoring explicit OFF
|
|
14
|
+
* - Dynamic extensibility: synthetic and future components are discovered and routed
|
|
15
|
+
* without source-level hard-coded allowlist changes
|
|
16
|
+
* - Single domain authority: exactly one authoritative owner per domain
|
|
17
|
+
* - Strict gating: Forge Core remains assistive; autonomous execution is blocked until A8
|
|
18
|
+
* - Budget enforcement: bounded execution time, hops, tool calls, and context bytes
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { performance } from 'node:perf_hooks';
|
|
22
|
+
|
|
23
|
+
import {
|
|
24
|
+
PRODUCT_INVENTORY,
|
|
25
|
+
} from './afyx-modular-integration.mjs';
|
|
26
|
+
import {
|
|
27
|
+
ACCEPTED_PROVIDERS,
|
|
28
|
+
DOMAIN_AUTHORITY_MATRIX,
|
|
29
|
+
PROVIDER_CAPABILITY_SUPPORT,
|
|
30
|
+
resolveModularRuntimeActivation,
|
|
31
|
+
} from './afyx-modular-runtime.mjs';
|
|
32
|
+
|
|
33
|
+
export const AFYX_UNIVERSAL_INTEROP_CONTRACT = 'AfyxUniversalInterop/v1';
|
|
34
|
+
export const AFYX_CAPABILITY_REQUEST_CONTRACT = 'AfyxCapabilityRequest/v1';
|
|
35
|
+
export const AFYX_CAPABILITY_RESULT_CONTRACT = 'AfyxCapabilityResult/v1';
|
|
36
|
+
|
|
37
|
+
/** The six primary engineering components. */
|
|
38
|
+
export const PRIMARY_COMPONENT_IDS = Object.freeze([
|
|
39
|
+
'efficient-coding',
|
|
40
|
+
'odoo-engineering',
|
|
41
|
+
'afyx-engineering-expert',
|
|
42
|
+
'afyx-graph',
|
|
43
|
+
'afyx-context',
|
|
44
|
+
'afyx-forge',
|
|
45
|
+
]);
|
|
46
|
+
|
|
47
|
+
/** The canonical seven registered components in the Engineering Kit. */
|
|
48
|
+
export const SEVEN_REGISTERED_COMPONENT_IDS = Object.freeze([
|
|
49
|
+
'efficient-coding',
|
|
50
|
+
'odoo-engineering',
|
|
51
|
+
'afyx-engineering-expert',
|
|
52
|
+
'afyx-graph',
|
|
53
|
+
'afyx-context',
|
|
54
|
+
'afyx-forge',
|
|
55
|
+
'codex-usage-tracking',
|
|
56
|
+
]);
|
|
57
|
+
|
|
58
|
+
export const DEFAULT_BUDGET_LIMITS = Object.freeze({
|
|
59
|
+
maxToolCalls: 4,
|
|
60
|
+
maxContextBytes: 65_536,
|
|
61
|
+
maxExecutionMs: 5_000,
|
|
62
|
+
maxCapabilities: 4,
|
|
63
|
+
maxHops: 2,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const AUTONOMOUS_FORGE_INTENTS = new Set([
|
|
67
|
+
'autonomous-forge',
|
|
68
|
+
'forge-campaign',
|
|
69
|
+
'formal-forge-evaluation',
|
|
70
|
+
'production-forge-execution',
|
|
71
|
+
]);
|
|
72
|
+
|
|
73
|
+
/** In-memory dynamic component registry for universal extensibility. */
|
|
74
|
+
const DYNAMIC_COMPONENT_REGISTRY = new Map(Object.entries(PRODUCT_INVENTORY));
|
|
75
|
+
|
|
76
|
+
/** Register a dynamic or synthetic future Afyx-owned component. */
|
|
77
|
+
export function registerAfyxComponent(componentDef) {
|
|
78
|
+
if (!componentDef || typeof componentDef.id !== 'string') {
|
|
79
|
+
throw new Error('Component definition must include a valid id string');
|
|
80
|
+
}
|
|
81
|
+
const definition = Object.freeze({
|
|
82
|
+
id: componentDef.id,
|
|
83
|
+
canonicalName: componentDef.canonicalName ?? componentDef.id,
|
|
84
|
+
type: componentDef.type ?? 'runtime',
|
|
85
|
+
primaryCapability: componentDef.primaryCapability ?? `${componentDef.id}-capability`,
|
|
86
|
+
domainAuthority: componentDef.domainAuthority ?? `${componentDef.id} authoritative domain`,
|
|
87
|
+
ownershipClass: componentDef.ownershipClass ?? 'AFYX',
|
|
88
|
+
maturity: componentDef.maturity ?? 'SOURCE_READY',
|
|
89
|
+
productionReady: false,
|
|
90
|
+
activationMode: componentDef.activationMode ?? 'ON_DEMAND',
|
|
91
|
+
cooperatingComponents: Object.freeze(componentDef.cooperatingComponents ?? []),
|
|
92
|
+
providers: componentDef.providers ?? null,
|
|
93
|
+
});
|
|
94
|
+
DYNAMIC_COMPONENT_REGISTRY.set(componentDef.id, definition);
|
|
95
|
+
return definition;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Unregister a dynamically added component (primary 7 cannot be removed). */
|
|
99
|
+
export function unregisterAfyxComponent(componentId) {
|
|
100
|
+
if (!SEVEN_REGISTERED_COMPONENT_IDS.includes(componentId)) {
|
|
101
|
+
DYNAMIC_COMPONENT_REGISTRY.delete(componentId);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Retrieve definition of any registered component. */
|
|
106
|
+
export function getRegisteredComponent(componentId) {
|
|
107
|
+
return DYNAMIC_COMPONENT_REGISTRY.get(componentId);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Retrieve all registered component identifiers. */
|
|
111
|
+
export function getRegisteredComponentIds() {
|
|
112
|
+
return [...DYNAMIC_COMPONENT_REGISTRY.keys()];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function boundedInteger(value, fallback, minimum = 0) {
|
|
116
|
+
return Number.isSafeInteger(value) && value >= minimum ? value : fallback;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function normalizeBudgets(requested = {}) {
|
|
120
|
+
return Object.freeze({
|
|
121
|
+
maxToolCalls: boundedInteger(requested.maxToolCalls, DEFAULT_BUDGET_LIMITS.maxToolCalls),
|
|
122
|
+
maxContextBytes: boundedInteger(requested.maxContextBytes, DEFAULT_BUDGET_LIMITS.maxContextBytes),
|
|
123
|
+
maxExecutionMs: boundedInteger(requested.maxExecutionMs, DEFAULT_BUDGET_LIMITS.maxExecutionMs, 1),
|
|
124
|
+
maxCapabilities: boundedInteger(requested.maxCapabilities, DEFAULT_BUDGET_LIMITS.maxCapabilities),
|
|
125
|
+
maxHops: boundedInteger(requested.maxHops, DEFAULT_BUDGET_LIMITS.maxHops),
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function providerAllows(componentId, capability, provider, providerPermissions) {
|
|
130
|
+
if (providerPermissions?.[componentId] === false || providerPermissions?.[capability] === false) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
if (provider === null) return componentId !== 'afyx-credential-broker';
|
|
134
|
+
const providerMeta = PROVIDER_CAPABILITY_SUPPORT[provider];
|
|
135
|
+
if (!providerMeta) return false;
|
|
136
|
+
const declaredProviders = DYNAMIC_COMPONENT_REGISTRY.get(componentId)?.providers;
|
|
137
|
+
if (declaredProviders && !declaredProviders.includes(provider)) return false;
|
|
138
|
+
if (componentId === 'afyx-forge') {
|
|
139
|
+
return providerPermissions?.[componentId] === true;
|
|
140
|
+
}
|
|
141
|
+
if (providerMeta.supportedCapabilities.includes(capability)) {
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
// Dynamic synthetic component allowed on known provider if not explicitly forbidden
|
|
145
|
+
if (!SEVEN_REGISTERED_COMPONENT_IDS.includes(componentId) && providerPermissions?.[componentId] !== false) {
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Normalize caller-observed component state into a deterministic availability set.
|
|
153
|
+
*
|
|
154
|
+
* Implements default-enabled interoperability: installed components are enabled
|
|
155
|
+
* by default for interop discovery unless explicitly disabled by user/environment.
|
|
156
|
+
* Health, compatibility, positive source identity, and authorization remain mandatory.
|
|
157
|
+
*/
|
|
158
|
+
export function discoverAfyxCapabilities({
|
|
159
|
+
componentStates = {},
|
|
160
|
+
provider = null,
|
|
161
|
+
providerPermissions = {},
|
|
162
|
+
environment = process.env,
|
|
163
|
+
customComponents = [],
|
|
164
|
+
} = {}) {
|
|
165
|
+
const normalizedProvider = typeof provider === 'string' ? provider.toLowerCase() : null;
|
|
166
|
+
const providerKnown = normalizedProvider === null || ACCEPTED_PROVIDERS.includes(normalizedProvider);
|
|
167
|
+
const contextEnvironmentOff = String(environment?.AFYX_CONTEXT_OPT_IN ?? '').trim() === '0';
|
|
168
|
+
const capabilities = {};
|
|
169
|
+
|
|
170
|
+
if (Array.isArray(customComponents)) {
|
|
171
|
+
for (const comp of customComponents) {
|
|
172
|
+
if (comp?.id) registerAfyxComponent(comp);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Universal discovery over all registered and caller-declared components
|
|
177
|
+
const allComponentIds = new Set([
|
|
178
|
+
...DYNAMIC_COMPONENT_REGISTRY.keys(),
|
|
179
|
+
...Object.keys(componentStates),
|
|
180
|
+
]);
|
|
181
|
+
|
|
182
|
+
for (const componentId of allComponentIds) {
|
|
183
|
+
const observed = componentStates[componentId] ?? {};
|
|
184
|
+
const metadata = DYNAMIC_COMPONENT_REGISTRY.get(componentId) ?? {
|
|
185
|
+
id: componentId,
|
|
186
|
+
canonicalName: observed.canonicalName ?? componentId,
|
|
187
|
+
type: observed.type ?? 'runtime',
|
|
188
|
+
primaryCapability: observed.capability ?? `${componentId}-capability`,
|
|
189
|
+
domainAuthority: observed.domainAuthority ?? `${componentId} authoritative domain`,
|
|
190
|
+
ownershipClass: observed.ownershipClass ?? 'AFYX',
|
|
191
|
+
};
|
|
192
|
+
const capability = metadata.primaryCapability;
|
|
193
|
+
const explicitOff = observed.enabled === false ||
|
|
194
|
+
(componentId === 'afyx-context' && contextEnvironmentOff);
|
|
195
|
+
const providerAuthorized = providerKnown && providerAllows(
|
|
196
|
+
componentId,
|
|
197
|
+
capability,
|
|
198
|
+
normalizedProvider,
|
|
199
|
+
providerPermissions,
|
|
200
|
+
);
|
|
201
|
+
const installed = observed.installed === true;
|
|
202
|
+
// Default-enabled for interoperability if installed, unless explicitly set to false or context env off
|
|
203
|
+
const enabled = installed && observed.enabled !== false &&
|
|
204
|
+
!(componentId === 'afyx-context' && contextEnvironmentOff);
|
|
205
|
+
const healthy = observed.healthy === true;
|
|
206
|
+
const compatible = observed.compatible === true;
|
|
207
|
+
const authorized = observed.authorized === true && providerAuthorized;
|
|
208
|
+
const sourceIdentity = typeof observed.sourceIdentity === 'string' && observed.sourceIdentity.length > 0
|
|
209
|
+
? observed.sourceIdentity
|
|
210
|
+
: null;
|
|
211
|
+
const graphIndexUsable = componentId !== 'afyx-graph' ||
|
|
212
|
+
(observed.indexReady === true && observed.indexFresh === true);
|
|
213
|
+
const available = installed && enabled && healthy && compatible && authorized &&
|
|
214
|
+
sourceIdentity !== null && graphIndexUsable;
|
|
215
|
+
|
|
216
|
+
let reason = 'AVAILABLE';
|
|
217
|
+
if (!providerKnown) reason = 'UNKNOWN_PROVIDER';
|
|
218
|
+
else if (!installed) reason = 'NOT_INSTALLED';
|
|
219
|
+
else if (explicitOff) reason = 'EXPLICITLY_DISABLED';
|
|
220
|
+
else if (!enabled) reason = 'ENABLED_STATE_UNVERIFIED';
|
|
221
|
+
else if (!healthy) reason = 'UNHEALTHY_OR_UNVERIFIED';
|
|
222
|
+
else if (!compatible) reason = 'INCOMPATIBLE_OR_UNVERIFIED';
|
|
223
|
+
else if (!authorized) reason = 'UNAUTHORIZED_OR_UNVERIFIED';
|
|
224
|
+
else if (sourceIdentity === null) reason = 'SOURCE_IDENTITY_UNVERIFIED';
|
|
225
|
+
else if (!graphIndexUsable) reason = 'GRAPH_INDEX_NOT_READY_OR_FRESH';
|
|
226
|
+
|
|
227
|
+
capabilities[componentId] = Object.freeze({
|
|
228
|
+
componentId,
|
|
229
|
+
capability,
|
|
230
|
+
installed,
|
|
231
|
+
enabled,
|
|
232
|
+
healthy,
|
|
233
|
+
compatible,
|
|
234
|
+
authorized,
|
|
235
|
+
available,
|
|
236
|
+
reason,
|
|
237
|
+
sourceIdentity,
|
|
238
|
+
observationSource: observed.observationSource ?? 'CALLER_DECLARED',
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return Object.freeze({
|
|
243
|
+
contract: AFYX_UNIVERSAL_INTEROP_CONTRACT,
|
|
244
|
+
provider: normalizedProvider,
|
|
245
|
+
providerKnown,
|
|
246
|
+
capabilities: Object.freeze(capabilities),
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** Cheap deterministic task classification. */
|
|
251
|
+
export function classifyTaskIntent(task = {}) {
|
|
252
|
+
const intentText = `${task.intent ?? ''} ${task.operation ?? ''}`.toLowerCase();
|
|
253
|
+
const taskType = String(task.taskType ?? 'generic').toLowerCase();
|
|
254
|
+
const directSourceCandidate = task.exactKnownSource === true || task.knownFile === true ||
|
|
255
|
+
['known-file', 'string-replacement', 'exact-source'].includes(taskType) ||
|
|
256
|
+
/\b(exact source|known file|string replacement)\b/.test(intentText);
|
|
257
|
+
const odoo = task.project?.isOdoo === true || task.explicitOdooEvidence === true ||
|
|
258
|
+
taskType.startsWith('odoo-') || /\bodoo\b/.test(intentText);
|
|
259
|
+
const relational = task.needsRelationalEvidence === true ||
|
|
260
|
+
['impact-analysis', 'cross-asset', 'relationship-analysis'].includes(taskType) ||
|
|
261
|
+
/\b(dependency|relationship|blast radius|call graph|impact analysis)\b/.test(intentText);
|
|
262
|
+
const contextOptimization = task.needsContextOptimization === true ||
|
|
263
|
+
taskType === 'large-context';
|
|
264
|
+
const architecture = ['architecture', 'multi-disciplinary', 'system-design'].includes(taskType);
|
|
265
|
+
const forgeAdvice = task.forgeAdvice === true || taskType === 'forge-advice';
|
|
266
|
+
const forgeAutonomous = task.autonomousExecution === true || AUTONOMOUS_FORGE_INTENTS.has(taskType);
|
|
267
|
+
const telemetry = task.needsTelemetry === true || task.telemetry === true || task.trackUsage === true ||
|
|
268
|
+
taskType === 'usage-telemetry' || taskType === 'telemetry' ||
|
|
269
|
+
/\b(telemetry|usage tracking|usage report)\b/.test(intentText);
|
|
270
|
+
const customCapability = task.needsCustomCapability ?? task.customCapability ?? null;
|
|
271
|
+
const customComponentId = task.customComponentId ?? null;
|
|
272
|
+
const complexCapabilityNeeded = odoo || relational || contextOptimization ||
|
|
273
|
+
architecture || forgeAdvice || forgeAutonomous || telemetry ||
|
|
274
|
+
Boolean(customCapability) || Boolean(customComponentId);
|
|
275
|
+
const directSource = directSourceCandidate && !complexCapabilityNeeded;
|
|
276
|
+
|
|
277
|
+
return Object.freeze({
|
|
278
|
+
directSource,
|
|
279
|
+
odoo,
|
|
280
|
+
relational,
|
|
281
|
+
contextOptimization,
|
|
282
|
+
architecture,
|
|
283
|
+
forgeAdvice,
|
|
284
|
+
forgeAutonomous,
|
|
285
|
+
telemetry,
|
|
286
|
+
customCapability,
|
|
287
|
+
customComponentId,
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function dedupeKey(request) {
|
|
292
|
+
return [
|
|
293
|
+
request.taskId,
|
|
294
|
+
request.sourceRevision ?? '',
|
|
295
|
+
request.capability,
|
|
296
|
+
request.evidenceScope ?? '',
|
|
297
|
+
].join('|');
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function makeRequest({ task, componentId, discovery, budgets, reason, hopCount = 0 }) {
|
|
301
|
+
const discovered = discovery.capabilities[componentId];
|
|
302
|
+
return Object.freeze({
|
|
303
|
+
contract: AFYX_CAPABILITY_REQUEST_CONTRACT,
|
|
304
|
+
taskId: task.id ?? 'anonymous-task',
|
|
305
|
+
taskType: task.taskType ?? null,
|
|
306
|
+
intent: task.intent ?? task.taskType ?? 'generic',
|
|
307
|
+
capability: discovered.capability,
|
|
308
|
+
targetComponentId: componentId,
|
|
309
|
+
callerComponentId: 'afyx-native-orchestrator',
|
|
310
|
+
candidateProviders: Object.freeze(discovery.provider ? [discovery.provider] : []),
|
|
311
|
+
workspaceIdentity: task.workspaceIdentity ?? null,
|
|
312
|
+
sourceRevision: task.sourceRevision ?? null,
|
|
313
|
+
evidenceScope: task.evidenceScope ?? task.workspaceIdentity ?? 'task',
|
|
314
|
+
permissions: Object.freeze({ authorized: discovered.authorized }),
|
|
315
|
+
budgetLimits: budgets,
|
|
316
|
+
hopCount,
|
|
317
|
+
invocationReason: reason,
|
|
318
|
+
sourceIdentity: discovered.sourceIdentity,
|
|
319
|
+
payload: typeof task.payload === 'string' ? task.payload : (typeof task.text === 'string' ? task.text : (typeof task.input === 'string' ? task.input : null)),
|
|
320
|
+
classification: typeof task.classification === 'string' ? task.classification : null,
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function selectionCandidates(task, classification, modularRuntimePlan, discovery) {
|
|
325
|
+
if (classification.directSource) return [];
|
|
326
|
+
const selectedByPlanner = new Set(modularRuntimePlan.plannedComponents);
|
|
327
|
+
if (classification.odoo) selectedByPlanner.add('odoo-engineering');
|
|
328
|
+
if (classification.architecture) selectedByPlanner.add('afyx-engineering-expert');
|
|
329
|
+
if (classification.relational) selectedByPlanner.add('afyx-graph');
|
|
330
|
+
if (classification.contextOptimization) selectedByPlanner.add('afyx-context');
|
|
331
|
+
if (classification.forgeAdvice && !classification.forgeAutonomous) {
|
|
332
|
+
selectedByPlanner.add('afyx-forge');
|
|
333
|
+
}
|
|
334
|
+
if (classification.telemetry) {
|
|
335
|
+
selectedByPlanner.add('codex-usage-tracking');
|
|
336
|
+
}
|
|
337
|
+
if (classification.customComponentId) {
|
|
338
|
+
selectedByPlanner.add(classification.customComponentId);
|
|
339
|
+
}
|
|
340
|
+
if (classification.customCapability && discovery?.capabilities) {
|
|
341
|
+
for (const [id, comp] of Object.entries(discovery.capabilities)) {
|
|
342
|
+
if (comp.capability === classification.customCapability) {
|
|
343
|
+
selectedByPlanner.add(id);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const preferredOrder = [
|
|
349
|
+
...(classification.odoo ? ['odoo-engineering'] : []),
|
|
350
|
+
...(classification.architecture ? ['afyx-engineering-expert'] : []),
|
|
351
|
+
...(classification.relational ? ['afyx-graph'] : []),
|
|
352
|
+
'efficient-coding',
|
|
353
|
+
...(classification.contextOptimization ? ['afyx-context'] : []),
|
|
354
|
+
...(classification.forgeAdvice ? ['afyx-forge'] : []),
|
|
355
|
+
...(classification.telemetry ? ['codex-usage-tracking'] : []),
|
|
356
|
+
...(classification.customComponentId ? [classification.customComponentId] : []),
|
|
357
|
+
...(classification.customCapability ? Object.entries(discovery.capabilities)
|
|
358
|
+
.filter(([, component]) => component.capability === classification.customCapability)
|
|
359
|
+
.map(([id]) => id) : []),
|
|
360
|
+
...modularRuntimePlan.plannedComponents,
|
|
361
|
+
];
|
|
362
|
+
const reasons = {
|
|
363
|
+
'odoo-engineering': 'Odoo domain authority',
|
|
364
|
+
'afyx-engineering-expert': 'Cross-disciplinary authority',
|
|
365
|
+
'afyx-graph': 'Relational evidence is beneficial',
|
|
366
|
+
'efficient-coding': 'Focused implementation authority',
|
|
367
|
+
'afyx-context': 'Bounded context optimization requested',
|
|
368
|
+
'afyx-forge': 'Assistive advice requested; autonomous execution remains blocked',
|
|
369
|
+
'codex-usage-tracking': 'Passive usage telemetry observation',
|
|
370
|
+
};
|
|
371
|
+
return [...new Set(preferredOrder)]
|
|
372
|
+
.filter((componentId) => selectedByPlanner.has(componentId))
|
|
373
|
+
.map((componentId) => [componentId, reasons[componentId] ?? `Selected for ${componentId} capability`]);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/** Resolve an executable, deterministic route without invoking a component. */
|
|
377
|
+
export function resolveAfyxCapabilityRoute(options = {}) {
|
|
378
|
+
const task = options.task ?? {};
|
|
379
|
+
const budgets = normalizeBudgets(options.budgetLimits);
|
|
380
|
+
const discovery = discoverAfyxCapabilities(options);
|
|
381
|
+
const classification = classifyTaskIntent(task);
|
|
382
|
+
const modularRuntimePlan = resolveModularRuntimeActivation({
|
|
383
|
+
task,
|
|
384
|
+
provider: options.provider ?? null,
|
|
385
|
+
installedComponents: options.componentStates ?? {},
|
|
386
|
+
userPreferences: {
|
|
387
|
+
...(options.userPreferences ?? {}),
|
|
388
|
+
forceDirectSource: classification.directSource || options.userPreferences?.forceDirectSource === true,
|
|
389
|
+
disableContext: discovery.capabilities['afyx-context']?.enabled === false,
|
|
390
|
+
},
|
|
391
|
+
hostAuthorizations: options.hostAuthorizations ?? {},
|
|
392
|
+
});
|
|
393
|
+
const diagnostics = [];
|
|
394
|
+
const degradations = [];
|
|
395
|
+
const requests = [];
|
|
396
|
+
const seenCapabilities = new Set();
|
|
397
|
+
const owners = {};
|
|
398
|
+
|
|
399
|
+
if (!discovery.providerKnown) {
|
|
400
|
+
diagnostics.push({ code: 'UNKNOWN_PROVIDER', severity: 'ERROR' });
|
|
401
|
+
}
|
|
402
|
+
if (classification.forgeAutonomous) {
|
|
403
|
+
diagnostics.push({ code: 'FORGE_A8_REQUIRED', severity: 'ERROR' });
|
|
404
|
+
}
|
|
405
|
+
const inheritedBlockingDiagnostics = modularRuntimePlan.diagnostics.filter((item) =>
|
|
406
|
+
item.severity === 'ERROR' && !(
|
|
407
|
+
item.code === 'FORGE_GATED_PENDING_A8' &&
|
|
408
|
+
classification.forgeAdvice &&
|
|
409
|
+
!classification.forgeAutonomous
|
|
410
|
+
));
|
|
411
|
+
for (const item of inheritedBlockingDiagnostics) {
|
|
412
|
+
if (!diagnostics.some((diagnostic) => diagnostic.code === item.code)) {
|
|
413
|
+
diagnostics.push({ code: item.code, severity: item.severity });
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
for (const [componentId, reason] of selectionCandidates(task, classification, modularRuntimePlan, discovery)) {
|
|
418
|
+
if (seenCapabilities.has(componentId)) continue;
|
|
419
|
+
seenCapabilities.add(componentId);
|
|
420
|
+
const discovered = discovery.capabilities[componentId];
|
|
421
|
+
if (!discovered) continue;
|
|
422
|
+
|
|
423
|
+
const authority = DOMAIN_AUTHORITY_MATRIX[discovered.capability] ?? {
|
|
424
|
+
primaryOwner: componentId,
|
|
425
|
+
description: `${componentId} authoritative domain`,
|
|
426
|
+
};
|
|
427
|
+
if (authority?.primaryOwner !== componentId) {
|
|
428
|
+
diagnostics.push({ code: 'AUTHORITY_MISMATCH', componentId, severity: 'ERROR' });
|
|
429
|
+
continue;
|
|
430
|
+
}
|
|
431
|
+
owners[discovered.capability] = componentId;
|
|
432
|
+
if (!discovered.available) {
|
|
433
|
+
degradations.push({
|
|
434
|
+
componentId,
|
|
435
|
+
capability: discovered.capability,
|
|
436
|
+
reason: discovered.reason,
|
|
437
|
+
fallback: 'DIRECT_SOURCE',
|
|
438
|
+
});
|
|
439
|
+
continue;
|
|
440
|
+
}
|
|
441
|
+
if (requests.length >= budgets.maxCapabilities) {
|
|
442
|
+
degradations.push({
|
|
443
|
+
componentId,
|
|
444
|
+
capability: discovered.capability,
|
|
445
|
+
reason: 'CAPABILITY_BUDGET_EXHAUSTED',
|
|
446
|
+
fallback: 'DIRECT_SOURCE',
|
|
447
|
+
});
|
|
448
|
+
continue;
|
|
449
|
+
}
|
|
450
|
+
// Graph->Context Dependency-Aware Scheduling:
|
|
451
|
+
// When a task requests both relational Graph evidence and Context optimization,
|
|
452
|
+
// Context is a downstream dependent capability of Graph evidence.
|
|
453
|
+
// Do not schedule Context in initial requests; it will be scheduled dynamically
|
|
454
|
+
// only when the actual Graph result is eligible (>20KB) and available.
|
|
455
|
+
if (componentId === 'afyx-context' && classification.relational && classification.contextOptimization) {
|
|
456
|
+
continue;
|
|
457
|
+
}
|
|
458
|
+
requests.push(makeRequest({ task, componentId, discovery, budgets, reason }));
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
const blocked = !discovery.providerKnown || classification.forgeAutonomous || inheritedBlockingDiagnostics.length > 0;
|
|
462
|
+
return Object.freeze({
|
|
463
|
+
contract: AFYX_UNIVERSAL_INTEROP_CONTRACT,
|
|
464
|
+
taskId: task.id ?? 'anonymous-task',
|
|
465
|
+
status: blocked ? 'BLOCKED' : (degradations.length ? 'DEGRADED' : 'READY'),
|
|
466
|
+
executionStatus: 'NOT_RUN',
|
|
467
|
+
directSourceFastPath: classification.directSource,
|
|
468
|
+
classification,
|
|
469
|
+
discovery,
|
|
470
|
+
domainAuthorities: Object.freeze(owners),
|
|
471
|
+
requests: Object.freeze(requests),
|
|
472
|
+
degradations: Object.freeze(degradations),
|
|
473
|
+
diagnostics: Object.freeze(diagnostics),
|
|
474
|
+
planningEvidence: Object.freeze({
|
|
475
|
+
contract: modularRuntimePlan.contract,
|
|
476
|
+
status: modularRuntimePlan.status,
|
|
477
|
+
executionStatus: modularRuntimePlan.executionStatus,
|
|
478
|
+
}),
|
|
479
|
+
budgets,
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
function normalizedAdapterResult(request, raw, durationMs, validTargetIds) {
|
|
484
|
+
const succeeded = raw?.status === 'SUCCESS' || raw?.status === 'EXECUTED';
|
|
485
|
+
const output = raw?.output ?? null;
|
|
486
|
+
let measuredOutputBytes = 0;
|
|
487
|
+
try {
|
|
488
|
+
measuredOutputBytes = output === null
|
|
489
|
+
? 0
|
|
490
|
+
: Buffer.byteLength(typeof output === 'string' ? output : JSON.stringify(output));
|
|
491
|
+
} catch {
|
|
492
|
+
measuredOutputBytes = Number.MAX_SAFE_INTEGER;
|
|
493
|
+
}
|
|
494
|
+
const followUpRequests = Array.isArray(raw?.followUpRequests)
|
|
495
|
+
? raw.followUpRequests
|
|
496
|
+
.filter((item) => item && (validTargetIds ? validTargetIds.has(item.targetComponentId) : true))
|
|
497
|
+
.map((item) => Object.freeze({
|
|
498
|
+
targetComponentId: item.targetComponentId,
|
|
499
|
+
invocationReason: typeof item.invocationReason === 'string'
|
|
500
|
+
? item.invocationReason
|
|
501
|
+
: `Requested by ${request.targetComponentId}`,
|
|
502
|
+
}))
|
|
503
|
+
: [];
|
|
504
|
+
return Object.freeze({
|
|
505
|
+
contract: AFYX_CAPABILITY_RESULT_CONTRACT,
|
|
506
|
+
taskId: request.taskId,
|
|
507
|
+
capability: request.capability,
|
|
508
|
+
componentId: request.targetComponentId,
|
|
509
|
+
status: succeeded ? 'EXECUTED' : (raw?.status ?? 'FAILED'),
|
|
510
|
+
executionStatus: succeeded ? 'EXECUTED' : 'FAILED',
|
|
511
|
+
reason: succeeded ? undefined : (raw?.reason ?? 'ADAPTER_EXECUTION_FAILED'),
|
|
512
|
+
output,
|
|
513
|
+
evidence: Object.freeze(Array.isArray(raw?.evidence) ? raw.evidence : []),
|
|
514
|
+
provenance: Object.freeze({
|
|
515
|
+
workspaceIdentity: request.workspaceIdentity,
|
|
516
|
+
sourceRevision: request.sourceRevision,
|
|
517
|
+
componentSourceIdentity: request.sourceIdentity,
|
|
518
|
+
observationSource: 'ADAPTER_EXECUTION',
|
|
519
|
+
}),
|
|
520
|
+
durationMs,
|
|
521
|
+
contextBytes: Math.max(boundedInteger(raw?.contextBytes, 0), measuredOutputBytes),
|
|
522
|
+
followUpRequests: Object.freeze(followUpRequests),
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
function nonExecutedResult(request, status, reason) {
|
|
527
|
+
return Object.freeze({
|
|
528
|
+
contract: AFYX_CAPABILITY_RESULT_CONTRACT,
|
|
529
|
+
taskId: request.taskId,
|
|
530
|
+
capability: request.capability,
|
|
531
|
+
componentId: request.targetComponentId,
|
|
532
|
+
status,
|
|
533
|
+
executionStatus: 'NOT_RUN',
|
|
534
|
+
reason,
|
|
535
|
+
output: null,
|
|
536
|
+
evidence: Object.freeze([]),
|
|
537
|
+
provenance: Object.freeze({
|
|
538
|
+
workspaceIdentity: request.workspaceIdentity,
|
|
539
|
+
sourceRevision: request.sourceRevision,
|
|
540
|
+
componentSourceIdentity: request.sourceIdentity,
|
|
541
|
+
observationSource: 'ORCHESTRATOR_DECISION',
|
|
542
|
+
}),
|
|
543
|
+
durationMs: 0,
|
|
544
|
+
contextBytes: 0,
|
|
545
|
+
followUpRequests: Object.freeze([]),
|
|
546
|
+
});
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
function failedExecutionResult(request, reason, durationMs) {
|
|
550
|
+
return Object.freeze({
|
|
551
|
+
...nonExecutedResult(request, 'FAILED', reason),
|
|
552
|
+
executionStatus: 'FAILED',
|
|
553
|
+
durationMs,
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* Execute a route through caller-provided, in-process component adapters.
|
|
559
|
+
*
|
|
560
|
+
* Implements universal bidirectional interoperability: any available component
|
|
561
|
+
* can request an authorized follow-up capability from any other available component,
|
|
562
|
+
* bounded by budgets, deduplication, cycle prevention, and Forge A8 restrictions.
|
|
563
|
+
*/
|
|
564
|
+
export async function executeAfyxCapabilityRoute(options = {}) {
|
|
565
|
+
const route = resolveAfyxCapabilityRoute(options);
|
|
566
|
+
const adapters = options.adapters ?? {};
|
|
567
|
+
const startedAt = performance.now();
|
|
568
|
+
const queue = [...route.requests];
|
|
569
|
+
const results = [];
|
|
570
|
+
const seen = new Set();
|
|
571
|
+
const scheduledComponents = new Set(route.requests.map((request) => request.targetComponentId));
|
|
572
|
+
const validTargetIds = new Set(Object.keys(route.discovery.capabilities));
|
|
573
|
+
let contextBytes = 0;
|
|
574
|
+
let duplicateInvocationsAvoided = 0;
|
|
575
|
+
let toolCalls = 0;
|
|
576
|
+
let contextBudgetExceeded = false;
|
|
577
|
+
|
|
578
|
+
if (route.status === 'BLOCKED' || route.directSourceFastPath) {
|
|
579
|
+
return Object.freeze({
|
|
580
|
+
...route,
|
|
581
|
+
executionStatus: route.status === 'BLOCKED' ? 'BLOCKED' : 'DIRECT_SOURCE',
|
|
582
|
+
results: Object.freeze([]),
|
|
583
|
+
metrics: Object.freeze({
|
|
584
|
+
selectedCapabilities: route.requests.length,
|
|
585
|
+
executedCapabilities: 0,
|
|
586
|
+
toolCalls: 0,
|
|
587
|
+
contextBytes: 0,
|
|
588
|
+
duplicateInvocationsAvoided: 0,
|
|
589
|
+
elapsedMs: performance.now() - startedAt,
|
|
590
|
+
}),
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
while (queue.length > 0) {
|
|
595
|
+
const request = queue.shift();
|
|
596
|
+
const key = dedupeKey(request);
|
|
597
|
+
if (seen.has(key)) {
|
|
598
|
+
duplicateInvocationsAvoided += 1;
|
|
599
|
+
results.push(nonExecutedResult(request, 'SUPPRESSED', 'DUPLICATE_REQUEST'));
|
|
600
|
+
continue;
|
|
601
|
+
}
|
|
602
|
+
seen.add(key);
|
|
603
|
+
|
|
604
|
+
if (request.hopCount > route.budgets.maxHops) {
|
|
605
|
+
results.push(nonExecutedResult(request, 'BLOCKED', 'HOP_BUDGET_EXHAUSTED'));
|
|
606
|
+
continue;
|
|
607
|
+
}
|
|
608
|
+
if (toolCalls >= route.budgets.maxToolCalls) {
|
|
609
|
+
results.push(nonExecutedResult(request, 'BLOCKED', 'TOOL_CALL_BUDGET_EXHAUSTED'));
|
|
610
|
+
continue;
|
|
611
|
+
}
|
|
612
|
+
if (performance.now() - startedAt >= route.budgets.maxExecutionMs) {
|
|
613
|
+
results.push(nonExecutedResult(request, 'BLOCKED', 'EXECUTION_TIME_BUDGET_EXHAUSTED'));
|
|
614
|
+
continue;
|
|
615
|
+
}
|
|
616
|
+
const discovered = route.discovery.capabilities[request.targetComponentId];
|
|
617
|
+
if (!discovered?.available || discovered.capability !== request.capability) {
|
|
618
|
+
results.push(nonExecutedResult(request, 'BLOCKED', 'CAPABILITY_NOT_AVAILABLE'));
|
|
619
|
+
continue;
|
|
620
|
+
}
|
|
621
|
+
const adapter = adapters[request.targetComponentId];
|
|
622
|
+
if (typeof adapter !== 'function') {
|
|
623
|
+
results.push(nonExecutedResult(request, 'DEGRADED', 'ADAPTER_NOT_PROVIDED'));
|
|
624
|
+
continue;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
const invocationStartedAt = performance.now();
|
|
628
|
+
let result;
|
|
629
|
+
let timeout;
|
|
630
|
+
const controller = new AbortController();
|
|
631
|
+
try {
|
|
632
|
+
const remainingMs = Math.max(1, route.budgets.maxExecutionMs - (performance.now() - startedAt));
|
|
633
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
634
|
+
timeout = setTimeout(() => {
|
|
635
|
+
controller.abort();
|
|
636
|
+
reject(new Error('EXECUTION_TIME_BUDGET_EXHAUSTED'));
|
|
637
|
+
}, remainingMs);
|
|
638
|
+
});
|
|
639
|
+
toolCalls += 1;
|
|
640
|
+
result = normalizedAdapterResult(
|
|
641
|
+
request,
|
|
642
|
+
await Promise.race([adapter(request, { signal: controller.signal }), timeoutPromise]),
|
|
643
|
+
performance.now() - invocationStartedAt,
|
|
644
|
+
validTargetIds,
|
|
645
|
+
);
|
|
646
|
+
} catch (error) {
|
|
647
|
+
result = failedExecutionResult(
|
|
648
|
+
request,
|
|
649
|
+
error instanceof Error ? error.message : String(error),
|
|
650
|
+
performance.now() - invocationStartedAt,
|
|
651
|
+
);
|
|
652
|
+
} finally {
|
|
653
|
+
clearTimeout(timeout);
|
|
654
|
+
}
|
|
655
|
+
results.push(result);
|
|
656
|
+
contextBytes += result.contextBytes;
|
|
657
|
+
if (contextBytes > route.budgets.maxContextBytes) {
|
|
658
|
+
contextBudgetExceeded = true;
|
|
659
|
+
break;
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
// Process bidirectional follow-up capability requests and dynamic Graph->Context handoff
|
|
663
|
+
const isGraphResult = request.targetComponentId === 'afyx-graph';
|
|
664
|
+
const contextOptimizationRequested = route.classification.contextOptimization === true;
|
|
665
|
+
const isEligibleGraphPayload = result.status === 'EXECUTED' && typeof result.output === 'string' &&
|
|
666
|
+
(result.contextBytes > 20_000 || Buffer.byteLength(result.output, 'utf8') > 20_000);
|
|
667
|
+
const alreadyHasContextFollowUp = result.followUpRequests.some((f) => f?.targetComponentId === 'afyx-context');
|
|
668
|
+
|
|
669
|
+
const effectiveFollowUps = [...result.followUpRequests];
|
|
670
|
+
if (isGraphResult && contextOptimizationRequested && isEligibleGraphPayload && !alreadyHasContextFollowUp) {
|
|
671
|
+
effectiveFollowUps.push({
|
|
672
|
+
targetComponentId: 'afyx-context',
|
|
673
|
+
invocationReason: 'Compress large graph evidence payload',
|
|
674
|
+
});
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
for (const followUp of effectiveFollowUps) {
|
|
678
|
+
const target = followUp?.targetComponentId;
|
|
679
|
+
const followUpCapability = route.discovery.capabilities[target];
|
|
680
|
+
const forgeAllowed = target !== 'afyx-forge' ||
|
|
681
|
+
(route.classification.forgeAdvice && !route.classification.forgeAutonomous);
|
|
682
|
+
const isAvailable = followUpCapability?.available === true;
|
|
683
|
+
const notDirectSelfCycle = target !== request.targetComponentId;
|
|
684
|
+
|
|
685
|
+
if (!isAvailable || !forgeAllowed || !notDirectSelfCycle) {
|
|
686
|
+
if (followUpCapability) {
|
|
687
|
+
results.push(nonExecutedResult(
|
|
688
|
+
makeRequest({
|
|
689
|
+
task: options.task ?? {},
|
|
690
|
+
componentId: target,
|
|
691
|
+
discovery: route.discovery,
|
|
692
|
+
budgets: route.budgets,
|
|
693
|
+
reason: followUp.invocationReason ?? `Requested by ${request.targetComponentId}`,
|
|
694
|
+
hopCount: request.hopCount + 1,
|
|
695
|
+
}),
|
|
696
|
+
'BLOCKED',
|
|
697
|
+
!forgeAllowed ? 'FORGE_A8_REQUIRED' : (!notDirectSelfCycle ? 'DIRECT_SELF_CYCLE' : 'FOLLOW_UP_NOT_AUTHORIZED'),
|
|
698
|
+
));
|
|
699
|
+
}
|
|
700
|
+
continue;
|
|
701
|
+
}
|
|
702
|
+
if (!scheduledComponents.has(target) && scheduledComponents.size >= route.budgets.maxCapabilities) {
|
|
703
|
+
results.push(nonExecutedResult(
|
|
704
|
+
makeRequest({
|
|
705
|
+
task: options.task ?? {},
|
|
706
|
+
componentId: target,
|
|
707
|
+
discovery: route.discovery,
|
|
708
|
+
budgets: route.budgets,
|
|
709
|
+
reason: followUp.invocationReason ?? `Requested by ${request.targetComponentId}`,
|
|
710
|
+
hopCount: request.hopCount + 1,
|
|
711
|
+
}),
|
|
712
|
+
'BLOCKED',
|
|
713
|
+
'CAPABILITY_BUDGET_EXHAUSTED',
|
|
714
|
+
));
|
|
715
|
+
continue;
|
|
716
|
+
}
|
|
717
|
+
scheduledComponents.add(target);
|
|
718
|
+
queue.push(Object.freeze({
|
|
719
|
+
...makeRequest({
|
|
720
|
+
task: options.task ?? {},
|
|
721
|
+
componentId: target,
|
|
722
|
+
discovery: route.discovery,
|
|
723
|
+
budgets: route.budgets,
|
|
724
|
+
reason: followUp.invocationReason ?? `Requested by ${request.targetComponentId}`,
|
|
725
|
+
hopCount: request.hopCount + 1,
|
|
726
|
+
}),
|
|
727
|
+
callerComponentId: request.targetComponentId,
|
|
728
|
+
// Only actual executed evidence is passed to bounded downstream adapters.
|
|
729
|
+
sourceResult: result.status === 'EXECUTED' ? result : null,
|
|
730
|
+
}));
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
const executedCapabilities = results.filter((result) => result.executionStatus === 'EXECUTED').length;
|
|
735
|
+
const failed = results.some((result) => result.status === 'FAILED');
|
|
736
|
+
const degraded = contextBudgetExceeded || route.status === 'DEGRADED' || results.some((result) => ['DEGRADED', 'BLOCKED'].includes(result.status));
|
|
737
|
+
return Object.freeze({
|
|
738
|
+
...route,
|
|
739
|
+
status: failed ? 'FAILED' : (degraded ? 'DEGRADED' : 'READY'),
|
|
740
|
+
executionStatus: executedCapabilities > 0 ? 'EXECUTED' : 'NOT_RUN',
|
|
741
|
+
results: Object.freeze(results),
|
|
742
|
+
metrics: Object.freeze({
|
|
743
|
+
selectedCapabilities: route.requests.length,
|
|
744
|
+
executedCapabilities,
|
|
745
|
+
toolCalls,
|
|
746
|
+
contextBytes,
|
|
747
|
+
contextBudgetExceeded,
|
|
748
|
+
duplicateInvocationsAvoided,
|
|
749
|
+
elapsedMs: performance.now() - startedAt,
|
|
750
|
+
}),
|
|
751
|
+
});
|
|
752
|
+
}
|