afyx-engineering-kit 0.0.0-stage → 0.1.0-alpha.4

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.
Files changed (208) hide show
  1. package/.afyx-source-revision +1 -0
  2. package/LICENSE +21 -0
  3. package/README.md +377 -2
  4. package/SECURITY.md +74 -0
  5. package/afyx-context/LICENSE +21 -0
  6. package/afyx-context/README.md +155 -0
  7. package/afyx-context/afyx-context.json +30 -0
  8. package/afyx-context/contracts/integration-v1.schema.json +34 -0
  9. package/afyx-context/package-lock.json +53 -0
  10. package/afyx-context/package.json +34 -0
  11. package/afyx-context/scripts/benchmark.mjs +39 -0
  12. package/afyx-context/scripts/build.mjs +15 -0
  13. package/afyx-context/scripts/comparative-benchmark.mjs +342 -0
  14. package/afyx-context/scripts/comparative-worker.mjs +74 -0
  15. package/afyx-context/scripts/graph-integration-e2e.mjs +188 -0
  16. package/afyx-context/scripts/integrated-dogfood.mjs +309 -0
  17. package/afyx-context/src/budget.js +166 -0
  18. package/afyx-context/src/cli.js +110 -0
  19. package/afyx-context/src/coordination.js +343 -0
  20. package/afyx-context/src/engine.js +198 -0
  21. package/afyx-context/src/errors.js +14 -0
  22. package/afyx-context/src/graph-integration-worker.js +70 -0
  23. package/afyx-context/src/graph-integration.js +373 -0
  24. package/afyx-context/src/graph-mcp-bridge.js +103 -0
  25. package/afyx-context/src/index.js +25 -0
  26. package/afyx-context/src/integrations.js +87 -0
  27. package/afyx-context/src/memory.js +255 -0
  28. package/afyx-context/src/storage.js +364 -0
  29. package/afyx-context/test-fixtures/coordination-worker.mjs +52 -0
  30. package/afyx-context/test-fixtures/graph-consumer-repo/addons/sale_extension/__init__.py +1 -0
  31. package/afyx-context/test-fixtures/graph-consumer-repo/addons/sale_extension/__manifest__.py +4 -0
  32. package/afyx-context/test-fixtures/graph-consumer-repo/addons/sale_extension/models/__init__.py +1 -0
  33. package/afyx-context/test-fixtures/graph-consumer-repo/addons/sale_extension/models/sale_order.py +5 -0
  34. package/afyx-context/test-fixtures/integrated-dogfood-repo/addons/integrated_sales/__init__.py +1 -0
  35. package/afyx-context/test-fixtures/integrated-dogfood-repo/addons/integrated_sales/__manifest__.py +10 -0
  36. package/afyx-context/test-fixtures/integrated-dogfood-repo/addons/integrated_sales/models/__init__.py +2 -0
  37. package/afyx-context/test-fixtures/integrated-dogfood-repo/addons/integrated_sales/models/sale_order.py +20 -0
  38. package/afyx-context/test-fixtures/integrated-dogfood-repo/addons/integrated_sales/models/sale_order_audit.py +11 -0
  39. package/afyx-context/test-fixtures/integrated-dogfood-repo/addons/integrated_sales/security/ir.model.access.csv +2 -0
  40. package/afyx-context/test-fixtures/integrated-dogfood-repo/addons/integrated_sales/views/sale_order_views.xml +14 -0
  41. package/afyx-forge/LICENSE +55 -0
  42. package/afyx-forge/README.md +126 -0
  43. package/afyx-forge/afyx-forge.json +28 -0
  44. package/afyx-forge/bin/afyx-forge +4 -0
  45. package/afyx-forge/bin/afyx-forge.cmd +3 -0
  46. package/afyx-forge/bin/afyx-forge.js +16 -0
  47. package/afyx-forge/contracts/forge-plan-v1.schema.json +172 -0
  48. package/afyx-forge/package.json +26 -0
  49. package/afyx-forge/src/classifier.js +190 -0
  50. package/afyx-forge/src/cli.js +111 -0
  51. package/afyx-forge/src/doctor.js +105 -0
  52. package/afyx-forge/src/guidance.js +120 -0
  53. package/afyx-forge/src/index.js +273 -0
  54. package/afyx-forge/src/planning.js +107 -0
  55. package/afyx-forge/src/policy.js +109 -0
  56. package/afyx-forge/src/workflow-refiner.js +151 -0
  57. package/afyx-graph/afyx-graph.json +20 -0
  58. package/afyx-graph/engine/LICENSE +21 -0
  59. package/afyx-graph/engine/README.md +104 -0
  60. package/afyx-graph/engine/release/RELEASE-MANIFEST.json +21 -0
  61. package/afyx-graph/engine/release/SHA256SUMS +2 -0
  62. package/afyx-graph/engine/release/afyx-graph-linux-x64.tar.gz +0 -0
  63. package/afyx-graph/engine/release/afyx-graph-win32-x64.zip +0 -0
  64. package/afyx-graph/engine/scripts/distribution-contract.mjs +212 -0
  65. package/afyx-graph/engine/scripts/distribution-product.json +33 -0
  66. package/bin/afyx-engineering-kit.mjs +61 -0
  67. package/install.ps1 +1053 -0
  68. package/install.sh +617 -0
  69. package/package.json +66 -4
  70. package/scripts/afyx-capability-orchestrator.mjs +752 -0
  71. package/scripts/afyx-claude-integration.mjs +547 -0
  72. package/scripts/afyx-context-artifact.mjs +105 -0
  73. package/scripts/afyx-credential-broker.mjs +75 -0
  74. package/scripts/afyx-doctor.ps1 +152 -0
  75. package/scripts/afyx-doctor.sh +146 -0
  76. package/scripts/afyx-forge-artifact.mjs +183 -0
  77. package/scripts/afyx-global-routing-policy.md +12 -0
  78. package/scripts/afyx-global-routing.mjs +168 -0
  79. package/scripts/afyx-graph-artifact.mjs +178 -0
  80. package/scripts/afyx-host-adapters.mjs +543 -0
  81. package/scripts/afyx-host-bridge-lifecycle.mjs +174 -0
  82. package/scripts/afyx-host-bridge.mjs +288 -0
  83. package/scripts/afyx-integration-contracts.mjs +825 -0
  84. package/scripts/afyx-mcp-integration.mjs +528 -0
  85. package/scripts/afyx-modular-integration.mjs +361 -0
  86. package/scripts/afyx-modular-runtime.mjs +451 -0
  87. package/scripts/afyx-native-skill-host.mjs +124 -0
  88. package/scripts/afyx-npm-installer.mjs +486 -0
  89. package/scripts/afyx-project.ps1 +205 -0
  90. package/scripts/afyx-project.sh +207 -0
  91. package/scripts/afyx-provider-backup.mjs +1206 -0
  92. package/scripts/afyx-provider-integration.mjs +724 -0
  93. package/scripts/afyx-provider-inventory.mjs +147 -0
  94. package/scripts/afyx-security-auditor.mjs +224 -0
  95. package/scripts/afyx-skill-engineering.mjs +354 -0
  96. package/scripts/afyx-tool-family-routing.mjs +64 -0
  97. package/scripts/afyx-universal-runtime.mjs +530 -0
  98. package/scripts/alpha_acceptance_preflight.py +240 -0
  99. package/scripts/benchmark-graph-memory.mjs +96 -0
  100. package/scripts/benchmark_capability_orchestrator.mjs +64 -0
  101. package/scripts/build-windows-installer.ps1 +259 -0
  102. package/scripts/build_forge_synthetic_image.py +328 -0
  103. package/scripts/check-reference-staleness.py +71 -0
  104. package/scripts/components.json +254 -0
  105. package/scripts/contracts/afyx-capability-request-v1.schema.json +189 -0
  106. package/scripts/contracts/afyx-capability-result-v1.schema.json +32 -0
  107. package/scripts/dogfood_engineering_comparison.mjs +303 -0
  108. package/scripts/engineering-assurance.json +28 -0
  109. package/scripts/install-afyx-context.ps1 +142 -0
  110. package/scripts/install-afyx-context.sh +126 -0
  111. package/scripts/install-afyx-forge.ps1 +265 -0
  112. package/scripts/install-afyx-forge.sh +273 -0
  113. package/scripts/install-afyx-graph.ps1 +571 -0
  114. package/scripts/install-afyx-graph.sh +438 -0
  115. package/scripts/install-codex-usage-tracker.ps1 +172 -0
  116. package/scripts/lib/AfyxComponents.psm1 +399 -0
  117. package/scripts/lib/AfyxSkillLifecycle.psm1 +289 -0
  118. package/scripts/lib/AfyxTerminal.psm1 +838 -0
  119. package/scripts/lib/afyx-components.sh +256 -0
  120. package/scripts/lib/afyx-skill-lifecycle.sh +271 -0
  121. package/scripts/link-global-dev.ps1 +193 -0
  122. package/scripts/link-global-dev.sh +130 -0
  123. package/scripts/phase8-paired-evidence.mjs +195 -0
  124. package/scripts/phase8-representative-workload.mjs +771 -0
  125. package/scripts/phase8-session-recorder.mjs +157 -0
  126. package/scripts/phase8_event_ledger.py +248 -0
  127. package/scripts/phase8_evidence_discovery.py +189 -0
  128. package/scripts/prepare-g6-package.mjs +349 -0
  129. package/scripts/resolve-git-release-artifacts.mjs +321 -0
  130. package/scripts/stage-npm-release.mjs +94 -0
  131. package/scripts/validate-afyx-graph.py +203 -0
  132. package/scripts/validate-components.py +194 -0
  133. package/scripts/validate-engineering-assurance.py +281 -0
  134. package/scripts/verify-graph-archive.mjs +64 -0
  135. package/skills/afyx-context/SKILL.md +26 -0
  136. package/skills/afyx-engineering-expert/README.md +42 -0
  137. package/skills/afyx-engineering-expert/SKILL.md +70 -0
  138. package/skills/afyx-engineering-expert/assets/result-template.md +15 -0
  139. package/skills/afyx-engineering-expert/references/ai-ml-engineer.md +17 -0
  140. package/skills/afyx-engineering-expert/references/business-analyst.md +17 -0
  141. package/skills/afyx-engineering-expert/references/clean-code-engineer.md +26 -0
  142. package/skills/afyx-engineering-expert/references/code-reviewer.md +24 -0
  143. package/skills/afyx-engineering-expert/references/cybersecurity-devsecops.md +17 -0
  144. package/skills/afyx-engineering-expert/references/data-analyst-bi.md +24 -0
  145. package/skills/afyx-engineering-expert/references/data-engineer.md +24 -0
  146. package/skills/afyx-engineering-expert/references/database-administrator.md +17 -0
  147. package/skills/afyx-engineering-expert/references/debugging-specialist.md +24 -0
  148. package/skills/afyx-engineering-expert/references/embedded-iot-engineer.md +24 -0
  149. package/skills/afyx-engineering-expert/references/erp-technical-specialist.md +17 -0
  150. package/skills/afyx-engineering-expert/references/hardware-diagnostics-auditor.md +24 -0
  151. package/skills/afyx-engineering-expert/references/hardware-electronics-engineer.md +24 -0
  152. package/skills/afyx-engineering-expert/references/integration-engineer.md +24 -0
  153. package/skills/afyx-engineering-expert/references/legacy-modernization-engineer.md +24 -0
  154. package/skills/afyx-engineering-expert/references/llmops-prompt-engineer.md +24 -0
  155. package/skills/afyx-engineering-expert/references/network-engineer.md +24 -0
  156. package/skills/afyx-engineering-expert/references/performance-benchmark-engineer.md +24 -0
  157. package/skills/afyx-engineering-expert/references/platform-engineer.md +24 -0
  158. package/skills/afyx-engineering-expert/references/product-manager.md +17 -0
  159. package/skills/afyx-engineering-expert/references/qa-engineer.md +17 -0
  160. package/skills/afyx-engineering-expert/references/site-reliability-engineer.md +17 -0
  161. package/skills/afyx-engineering-expert/references/software-engineer.md +17 -0
  162. package/skills/afyx-engineering-expert/references/solutions-architect.md +17 -0
  163. package/skills/afyx-engineering-expert/references/specialists-index.md +33 -0
  164. package/skills/afyx-engineering-expert/references/subject-matter-expert.md +17 -0
  165. package/skills/afyx-engineering-expert/references/system-analyst.md +17 -0
  166. package/skills/afyx-engineering-expert/references/technical-project-manager.md +24 -0
  167. package/skills/afyx-engineering-expert/references/technical-writer.md +24 -0
  168. package/skills/afyx-engineering-expert/references/ui-ux-designer.md +17 -0
  169. package/skills/afyx-engineering-expert/workflows/ai-data-erp.md +20 -0
  170. package/skills/afyx-engineering-expert/workflows/debugging.md +10 -0
  171. package/skills/afyx-engineering-expert/workflows/discovery-design.md +11 -0
  172. package/skills/afyx-engineering-expert/workflows/implementation.md +12 -0
  173. package/skills/afyx-engineering-expert/workflows/incident-reliability.md +12 -0
  174. package/skills/afyx-engineering-expert/workflows/review-audit.md +13 -0
  175. package/skills/afyx-forge/SKILL.md +43 -0
  176. package/skills/afyx-forge/references/adaptive-policy.md +19 -0
  177. package/skills/efficient-coding/SKILL.md +33 -0
  178. package/skills/efficient-coding/references/investigation.md +27 -0
  179. package/skills/efficient-coding/references/token-efficiency.md +25 -0
  180. package/skills/efficient-coding/references/tool-routing.md +13 -0
  181. package/skills/odoo-engineering/SKILL.md +33 -0
  182. package/skills/odoo-engineering/references/common.md +9 -0
  183. package/skills/odoo-engineering/references/odoo-10.md +32 -0
  184. package/skills/odoo-engineering/references/odoo-11.md +26 -0
  185. package/skills/odoo-engineering/references/odoo-12.md +31 -0
  186. package/skills/odoo-engineering/references/odoo-13.md +27 -0
  187. package/skills/odoo-engineering/references/odoo-14.md +31 -0
  188. package/skills/odoo-engineering/references/odoo-15.md +34 -0
  189. package/skills/odoo-engineering/references/odoo-16.md +37 -0
  190. package/skills/odoo-engineering/references/odoo-17.md +37 -0
  191. package/skills/odoo-engineering/references/odoo-18.md +48 -0
  192. package/skills/odoo-engineering/references/odoo-19.md +43 -0
  193. package/skills/odoo-engineering/references/odoo-20.md +39 -0
  194. package/skills/odoo-engineering/references/reference-schema.md +16 -0
  195. package/skills/odoo-engineering/references/version-detection.md +13 -0
  196. package/tools/codex-usage/CodexUsage.psm1 +496 -0
  197. package/tools/codex-usage/codex-usage-doctor.ps1 +150 -0
  198. package/tools/codex-usage/codex-usage-pricing.json +25 -0
  199. package/tools/codex-usage/codex-usage-stop.ps1 +167 -0
  200. package/tools/codex-usage/codex-usage-watch.ps1 +250 -0
  201. package/tools/codex-usage/vscode-user-task.json +31 -0
  202. package/tools/windows-setup/SetupBootstrapper.cs +258 -0
  203. package/uninstall.ps1 +189 -0
  204. package/uninstall.sh +167 -0
  205. package/update.ps1 +95 -0
  206. package/update.sh +67 -0
  207. package/verify.ps1 +411 -0
  208. package/verify.sh +265 -0
@@ -0,0 +1,571 @@
1
+ [CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = 'Install')]
2
+ param(
3
+ [Parameter(ParameterSetName = 'Install')][switch]$Replace,
4
+ [Parameter(ParameterSetName = 'Install')][switch]$Update,
5
+ [Parameter(ParameterSetName = 'Validate')][switch]$ValidateOnly,
6
+ [Parameter(ParameterSetName = 'Uninstall')][switch]$Uninstall,
7
+ [string]$ArchivePath,
8
+ [string]$RuntimeRoot = (Join-Path $env:USERPROFILE '.afyx\graph'),
9
+ [switch]$SkipPathUpdate,
10
+ [switch]$Offline,
11
+ [switch]$NoBuildFallback,
12
+ [switch]$AllowDirtySource,
13
+ [switch]$ConfigureMcp,
14
+ [switch]$SkipMcp
15
+ )
16
+
17
+ Set-StrictMode -Version Latest
18
+ $ErrorActionPreference = 'Stop'
19
+
20
+ $kitRoot = Split-Path -Parent $PSScriptRoot
21
+ $metadataPath = Join-Path $kitRoot 'afyx-graph\afyx-graph.json'
22
+ $metadata = Get-Content -Raw -LiteralPath $metadataPath -Encoding utf8 | ConvertFrom-Json
23
+ $distributionProductPath = Join-Path $kitRoot 'afyx-graph\engine\scripts\distribution-product.json'
24
+ $releaseChannel = (Get-Content -Raw -LiteralPath $distributionProductPath -Encoding utf8 | ConvertFrom-Json).releaseChannel
25
+ $architecture = switch ([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString()) {
26
+ 'Arm64' { 'arm64' }
27
+ 'X64' { 'x64' }
28
+ default { throw "Unsupported Windows architecture: $_" }
29
+ }
30
+ $target = "win32-$architecture"
31
+ if ($target -ne 'win32-x64') { throw "Afyx Graph prebuilt runtime is unavailable for $target. Minimal and Recommended Kit presets remain available; consumer source compilation is disabled." }
32
+ $assetName = "afyx-graph-$target.zip"
33
+ $current = Join-Path $RuntimeRoot 'current'
34
+ $rootMetadata = Join-Path $RuntimeRoot 'metadata.json'
35
+ $launcher = Join-Path $current 'bin\afyx-graph.cmd'
36
+ $publicBin = Join-Path $RuntimeRoot 'bin'
37
+ $publicLauncher = Join-Path $publicBin 'afyx-graph.cmd'
38
+ $recoveryRoot = "$RuntimeRoot.afyx-old"
39
+ $engineRoot = Join-Path $kitRoot 'afyx-graph\engine'
40
+ $contractPath = Join-Path $engineRoot 'scripts\distribution-contract.mjs'
41
+ $artifactIdentityTool = Join-Path $kitRoot 'scripts\afyx-graph-artifact.mjs'
42
+ $releaseRoot = if ($env:AFYX_GRAPH_RELEASE_ROOT) { $env:AFYX_GRAPH_RELEASE_ROOT } else { Join-Path $engineRoot 'release' }
43
+ $diagnosticLog = Join-Path ([System.IO.Path]::GetTempPath()) "afyx-graph-installer-$([guid]::NewGuid().ToString('N')).log"
44
+
45
+ function Invoke-LoggedNative {
46
+ param([string]$FilePath, [string[]]$Arguments, [string]$FailureMessage)
47
+ $previousPreference = $ErrorActionPreference
48
+ $global:LASTEXITCODE = 0
49
+ try {
50
+ # Windows PowerShell 5.1 treats redirected native stderr as an error record.
51
+ # Let the process finish, then judge the exit code while retaining its diagnostics.
52
+ $ErrorActionPreference = 'Continue'
53
+ & $FilePath @Arguments *>> $diagnosticLog
54
+ $exitCode = $LASTEXITCODE
55
+ } catch {
56
+ throw "$FailureMessage $($_.Exception.Message) Log: $diagnosticLog"
57
+ } finally {
58
+ $ErrorActionPreference = $previousPreference
59
+ }
60
+ if ($exitCode -ne 0) { throw "$FailureMessage Exit code: $exitCode. Log: $diagnosticLog" }
61
+ }
62
+
63
+ function Get-CommandVersion([string]$Name, [string[]]$Arguments = @('--version')) {
64
+ $command = Get-Command $Name -ErrorAction SilentlyContinue
65
+ if (-not $command) { return $null }
66
+ try {
67
+ $output = (& $command.Source @Arguments 2>$null) -join ' '
68
+ if ($LASTEXITCODE -ne 0) { return $null }
69
+ return $output.Trim()
70
+ } catch { return $null }
71
+ }
72
+
73
+ function Get-NodeVersion {
74
+ $raw = Get-CommandVersion 'node' @('--version')
75
+ if (-not $raw) { return $null }
76
+ $match = [regex]::Match($raw, '(\d+)\.(\d+)\.(\d+)')
77
+ if (-not $match.Success) { return $null }
78
+ return [version]::new([int]$match.Groups[1].Value, [int]$match.Groups[2].Value, [int]$match.Groups[3].Value)
79
+ }
80
+
81
+ function Assert-HostRuntime {
82
+ $nodeVersion = Get-NodeVersion
83
+ if (-not $nodeVersion) {
84
+ throw 'REQUIRED: Node.js was not found on PATH. Afyx Graph requires Node.js 22.5.0 or newer. Install it from https://nodejs.org/ and retry.'
85
+ }
86
+ if ($nodeVersion -lt [version]'22.5.0') {
87
+ throw "REQUIRED: Node.js $nodeVersion is incompatible. Afyx Graph requires Node.js 22.5.0 or newer."
88
+ }
89
+ return $nodeVersion
90
+ }
91
+
92
+ function Get-SourceRevision {
93
+ if (-not (Test-Path -LiteralPath (Join-Path $kitRoot '.git'))) { return $null }
94
+ $git = Get-Command git -ErrorAction SilentlyContinue
95
+ if (-not $git) { return $null }
96
+ & $git.Source -C $kitRoot rev-parse --is-inside-work-tree *> $null
97
+ if ($LASTEXITCODE -ne 0) { return $null }
98
+ $commit = (& $git.Source -C $kitRoot rev-parse HEAD 2>$null).Trim()
99
+ $branch = ((& $git.Source -C $kitRoot branch --show-current 2>$null) -join '').Trim()
100
+ $dirty = [bool]((& $git.Source -C $kitRoot status --porcelain 2>$null) -join '')
101
+ $graphDirty = [bool]((& $git.Source -C $kitRoot status --porcelain -- afyx-graph 2>$null) -join '')
102
+ return [pscustomobject]@{ Commit = $commit; Branch = $branch; Dirty = $dirty; GraphDirty = $graphDirty }
103
+ }
104
+
105
+ function Get-RequestedRevision {
106
+ $source = Get-SourceRevision
107
+ if ($source) { return $source.Commit }
108
+ $marker = Join-Path $kitRoot '.afyx-source-revision'
109
+ if (Test-Path -LiteralPath $marker -PathType Leaf) {
110
+ $revision = (Get-Content -Raw -LiteralPath $marker -Encoding utf8).Trim()
111
+ if ($revision -notmatch '^[0-9a-f]{40}$') { throw 'Packaged source revision marker is invalid.' }
112
+ return $revision
113
+ }
114
+ return $null
115
+ }
116
+
117
+ function Build-LocalArchive {
118
+ if ($NoBuildFallback) { throw 'COMPONENT_REQUIRED: no matching verified release artifact is available and local build fallback was disabled.' }
119
+ $nodeVersion = Assert-HostRuntime
120
+ $npm = Get-Command npm -ErrorAction SilentlyContinue
121
+ if (-not $npm) {
122
+ throw 'BUILD_ONLY: npm was not found on PATH. It is required only because no verified release artifact was available.'
123
+ }
124
+ $source = Get-SourceRevision
125
+ if (-not $source) {
126
+ throw 'BUILD_ONLY: local fallback requires a validated Git checkout. Install Git or provide -ArchivePath with SHA256SUMS.'
127
+ }
128
+
129
+ $stagingRoot = $null
130
+ $buildEngineRoot = $engineRoot
131
+ $buildProvenance = 'LOCAL_CLEAN_BUILD'
132
+ try {
133
+
134
+ if ($source.Dirty -or $source.GraphDirty) {
135
+ if ($AllowDirtySource) {
136
+ Write-Warning "Building Afyx Graph with -AllowDirtySource; artifact provenance will be marked LOCAL_SOURCE_DIRTY."
137
+ $buildProvenance = 'LOCAL_SOURCE_DIRTY'
138
+ } else {
139
+ $stagingAttemptSuccess = $false
140
+ if ($source.Commit -and $source.Commit -match '^[0-9a-f]{7,64}$') {
141
+ $stagingRoot = Join-Path ([System.IO.Path]::GetTempPath()) "afyx-graph-clean-staging-$([guid]::NewGuid().ToString('N'))"
142
+ try {
143
+ New-Item -ItemType Directory -Path $stagingRoot -Force | Out-Null
144
+ $tar = Get-Command tar -ErrorAction SilentlyContinue
145
+ $git = Get-Command git -ErrorAction SilentlyContinue
146
+ if ($tar -and $git) {
147
+ $archiveTar = Join-Path $stagingRoot 'source.tar'
148
+ & $git.Source -C $kitRoot archive -o $archiveTar $source.Commit afyx-graph
149
+ if ($LASTEXITCODE -ne 0) { throw 'Git archive failed.' }
150
+ & $tar.Source -xf $archiveTar -C $stagingRoot
151
+ if ($LASTEXITCODE -ne 0) { throw 'Clean source extraction failed.' }
152
+ Remove-Item -LiteralPath $archiveTar -Force
153
+ $stagedEngine = Join-Path $stagingRoot 'afyx-graph\engine'
154
+ if (Test-Path -LiteralPath (Join-Path $stagedEngine 'package.json') -PathType Leaf) {
155
+ $buildEngineRoot = $stagedEngine
156
+ $stagingAttemptSuccess = $true
157
+ Write-Host "Local checkout has uncommitted changes outside clean revision $($source.Commit). Extracted verified clean commit tree for isolated build."
158
+ }
159
+ }
160
+ } catch {
161
+ $stagingAttemptSuccess = $false
162
+ }
163
+ }
164
+ if (-not $stagingAttemptSuccess) {
165
+ if ($stagingRoot -and (Test-Path -LiteralPath $stagingRoot)) { Remove-Item -Recurse -Force $stagingRoot -ErrorAction SilentlyContinue }
166
+ throw 'BUILD_ONLY: local source has uncommitted changes. Refusing to build an unverifiable Technical Alpha artifact; commit the changes, stash them, or explicitly use -AllowDirtySource for development builds.'
167
+ }
168
+ }
169
+ }
170
+
171
+ if (-not (Test-Path -LiteralPath (Join-Path $buildEngineRoot 'package-lock.json') -PathType Leaf)) {
172
+ throw 'BUILD_ONLY: afyx-graph/engine/package-lock.json is missing; local source is incomplete.'
173
+ }
174
+ $buildMetadataPath = if ($stagingRoot) { Join-Path $stagingRoot 'afyx-graph\afyx-graph.json' } else { $metadataPath }
175
+ $buildContractPath = Join-Path $buildEngineRoot 'scripts\distribution-contract.mjs'
176
+
177
+ Write-Host "Verified release unavailable; building Afyx Graph locally from $($source.Commit) on $($source.Branch)."
178
+ Push-Location $buildEngineRoot
179
+ try {
180
+ Invoke-LoggedNative -FilePath $npm.Source -Arguments @('ci') -FailureMessage 'BUILD_ONLY: npm ci failed; the existing installation was not changed.'
181
+ Invoke-LoggedNative -FilePath $npm.Source -Arguments @('run', 'build:clean') -FailureMessage 'BUILD_ONLY: Afyx Graph clean build failed; the existing installation was not changed.'
182
+ } finally {
183
+ Pop-Location
184
+ }
185
+
186
+ $buildRoot = Join-Path ([System.IO.Path]::GetTempPath()) "afyx-graph-build-$([guid]::NewGuid().ToString('N'))"
187
+ $bundleName = "afyx-graph-$target"
188
+ $bundleRoot = Join-Path $buildRoot $bundleName
189
+ try {
190
+ New-Item -ItemType Directory -Force -Path (Join-Path $bundleRoot 'lib'), (Join-Path $bundleRoot 'bin') | Out-Null
191
+ Copy-Item -LiteralPath (Join-Path $buildEngineRoot 'dist') -Destination (Join-Path $bundleRoot 'lib\dist') -Recurse
192
+ Copy-Item -LiteralPath (Join-Path $buildEngineRoot 'package.json') -Destination (Join-Path $bundleRoot 'lib\package.json')
193
+ Copy-Item -LiteralPath $buildMetadataPath -Destination (Join-Path $bundleRoot 'metadata.json')
194
+ $bundleMetadataPath = Join-Path $bundleRoot 'metadata.json'
195
+ $bundleMetadata = Get-Content -Raw -LiteralPath $bundleMetadataPath -Encoding utf8 | ConvertFrom-Json
196
+ $bundleMetadata | Add-Member -NotePropertyName release_channel -NotePropertyValue $releaseChannel
197
+ $bundleMetadata | ConvertTo-Json -Depth 8 | Set-Content -LiteralPath $bundleMetadataPath -Encoding utf8
198
+ $extractionVersion = [regex]::Match(
199
+ (Get-Content -Raw -LiteralPath (Join-Path $buildEngineRoot 'src\extraction\extraction-version.ts')),
200
+ 'EXTRACTION_VERSION\s*=\s*(\d+)'
201
+ ).Groups[1].Value
202
+ Invoke-LoggedNative -FilePath ((Get-Command node).Source) -Arguments @($artifactIdentityTool, 'stamp', '--metadata', $bundleMetadataPath, '--target', $target, '--revision', $source.Commit, '--extraction-version', $extractionVersion) -FailureMessage 'BUILD_ONLY: failed to stamp the local artifact identity.'
203
+ Copy-Item -LiteralPath (Join-Path $buildEngineRoot 'LICENSE') -Destination (Join-Path $bundleRoot 'LICENSE')
204
+ @'
205
+ @echo off
206
+ where node >nul 2>&1
207
+ if errorlevel 1 (
208
+ echo [Afyx Graph] Node.js was not found on PATH. Install Node.js 22.5.0 or newer: https://nodejs.org/ 1>&2
209
+ exit /b 1
210
+ )
211
+ node --disable-warning=ExperimentalWarning "%~dp0..\lib\dist\bin\afyx-graph.js" %*
212
+ '@ | Set-Content -LiteralPath (Join-Path $bundleRoot 'bin\afyx-graph.cmd') -Encoding ascii
213
+
214
+ Invoke-LoggedNative -FilePath ((Get-Command node).Source) -Arguments @($buildContractPath, 'verify-bundle', '--root', $bundleRoot, '--target', $target) -FailureMessage 'BUILD_ONLY: locally staged bundle failed the distribution contract.'
215
+ New-Item -ItemType Directory -Force -Path $releaseRoot | Out-Null
216
+ $archive = Join-Path $releaseRoot $assetName
217
+ if (Test-Path -LiteralPath $archive) { Remove-Item -LiteralPath $archive -Force }
218
+ Compress-Archive -LiteralPath $bundleRoot -DestinationPath $archive -CompressionLevel Optimal
219
+ $hash = (Get-FileHash -Algorithm SHA256 -LiteralPath $archive).Hash.ToLowerInvariant()
220
+ "$hash $assetName" | Set-Content -LiteralPath (Join-Path $releaseRoot 'SHA256SUMS') -Encoding ascii
221
+ Write-Host "Local artifact verified: $assetName (Node.js $nodeVersion, commit $($source.Commit)); build log: $diagnosticLog."
222
+ return [pscustomobject]@{ Path = $archive; Source = 'local-build'; Commit = $source.Commit; BuildProvenance = $buildProvenance }
223
+ } finally {
224
+ if (Test-Path -LiteralPath $buildRoot) { Remove-Item -LiteralPath $buildRoot -Recurse -Force }
225
+ }
226
+ } finally {
227
+ if ($stagingRoot -and (Test-Path -LiteralPath $stagingRoot)) { Remove-Item -LiteralPath $stagingRoot -Recurse -Force -ErrorAction SilentlyContinue }
228
+ }
229
+ }
230
+
231
+ function Update-UserPath([switch]$Remove) {
232
+ if ($SkipPathUpdate) { return }
233
+ $userPath = [Environment]::GetEnvironmentVariable('Path', 'User')
234
+ $entries = @($userPath -split ';' | Where-Object { $_ })
235
+ $present = $entries | Where-Object { $_.TrimEnd('\') -ieq $publicBin.TrimEnd('\') }
236
+ if ($Remove) {
237
+ if ($present -and $PSCmdlet.ShouldProcess('User PATH', "Remove $publicBin")) {
238
+ $updated = ($entries | Where-Object { $_.TrimEnd('\') -ine $publicBin.TrimEnd('\') }) -join ';'
239
+ [Environment]::SetEnvironmentVariable('Path', $updated, 'User')
240
+ }
241
+ } elseif (-not $present -and $PSCmdlet.ShouldProcess('User PATH', "Add $publicBin")) {
242
+ $updated = (@($entries) + $publicBin) -join ';'
243
+ [Environment]::SetEnvironmentVariable('Path', $updated, 'User')
244
+ }
245
+ }
246
+
247
+ function Get-State {
248
+ if (-not (Test-Path -LiteralPath $RuntimeRoot)) { return 'NOT INSTALLED' }
249
+ if (-not (Test-Path -LiteralPath $rootMetadata -PathType Leaf) -or
250
+ -not (Test-Path -LiteralPath $launcher -PathType Leaf)) { return 'INCOMPLETE' }
251
+ try {
252
+ $installed = Get-Content -Raw -LiteralPath $rootMetadata -Encoding utf8 | ConvertFrom-Json
253
+ if ($installed.product_name -ne 'Afyx Graph' -or -not $installed.product_version) { return 'INVALID' }
254
+ } catch { return 'INVALID' }
255
+ return 'HEALTHY'
256
+ }
257
+
258
+ function Test-AfyxOwnershipAt([string]$Path) {
259
+ $metadataAtPath = Join-Path $Path 'metadata.json'
260
+ if (-not (Test-Path -LiteralPath $metadataAtPath -PathType Leaf)) { return $false }
261
+ try {
262
+ return (Get-Content -Raw -LiteralPath $metadataAtPath -Encoding utf8 | ConvertFrom-Json).product_name -eq 'Afyx Graph'
263
+ } catch { return $false }
264
+ }
265
+
266
+ function Test-AfyxOwnership { return Test-AfyxOwnershipAt -Path $RuntimeRoot }
267
+
268
+ function Resolve-Archive {
269
+ if ($ArchivePath) {
270
+ $resolved = Resolve-Path -LiteralPath $ArchivePath -ErrorAction Stop
271
+ return [pscustomobject]@{ Path = $resolved.Path; Source = 'explicit-archive'; Commit = $null; RevisionStatus = 'UNKNOWN'; BuildProvenance = 'EXPLICIT_ARCHIVE' }
272
+ }
273
+ $requestedRevision = Get-RequestedRevision
274
+ $local = Join-Path $releaseRoot $assetName
275
+ if (Test-Path -LiteralPath $local -PathType Leaf) {
276
+ $identity = $null
277
+ try {
278
+ Test-ArchiveChecksum -Path $local
279
+ Test-ArchiveBundle -Path $local
280
+ $identity = Get-ArchiveIdentity -Path $local -RequestedRevision $requestedRevision
281
+ } catch {
282
+ Write-Warning "Ignoring invalid local release cache ($($_.Exception.Message)); rebuilding from validated source."
283
+ return Build-LocalArchive
284
+ }
285
+ if ($requestedRevision -and $identity.revisionStatus -ne 'MATCH') {
286
+ Write-Warning "Ignoring valid but stale local release cache (requested $requestedRevision; artifact $($identity.sourceRevision)); rebuilding from validated source."
287
+ return Build-LocalArchive
288
+ }
289
+ return [pscustomobject]@{ Path = $local; Source = 'local-release-cache'; Commit = $identity.sourceRevision; RevisionStatus = $identity.revisionStatus; BuildProvenance = 'RELEASE_CACHE' }
290
+ }
291
+ if (-not $Offline) {
292
+ $downloadRoot = Join-Path ([System.IO.Path]::GetTempPath()) "afyx-graph-download-$([guid]::NewGuid().ToString('N'))"
293
+ try {
294
+ New-Item -ItemType Directory -Path $downloadRoot | Out-Null
295
+ $download = Join-Path $downloadRoot $assetName
296
+ $tag = "afyx-graph-v$($metadata.product_version)"
297
+ $repoBase = 'https://github.com/a5zero7/afyx-engineering-kit'
298
+ $url = "$repoBase/releases/download/$tag/$assetName"
299
+ Invoke-WebRequest -UseBasicParsing -Uri $url -OutFile $download
300
+ Invoke-WebRequest -UseBasicParsing -Uri "$repoBase/releases/download/$tag/SHA256SUMS" -OutFile (Join-Path $downloadRoot 'SHA256SUMS')
301
+ Invoke-WebRequest -UseBasicParsing -Uri "$repoBase/releases/download/$tag/RELEASE-MANIFEST.json" -OutFile (Join-Path $downloadRoot 'RELEASE-MANIFEST.json')
302
+ Test-ArchiveChecksum -Path $download
303
+ Test-ArchiveBundle -Path $download
304
+ $identity = Get-ArchiveIdentity -Path $download -RequestedRevision $requestedRevision
305
+ if ($requestedRevision -and $identity.revisionStatus -ne 'MATCH') {
306
+ throw "Downloaded release revision is $($identity.sourceRevision), requested checkout is $requestedRevision."
307
+ }
308
+ return [pscustomobject]@{ Path = $download; Source = 'github-release'; Commit = $identity.sourceRevision; RevisionStatus = $identity.revisionStatus; BuildProvenance = 'GITHUB_RELEASE' }
309
+ } catch {
310
+ Write-Warning "Verified GitHub release unavailable ($($_.Exception.Message)); trying bounded local build fallback."
311
+ }
312
+ } else { Write-Host 'Offline mode: skipping GitHub release lookup.' }
313
+ return Build-LocalArchive
314
+ }
315
+
316
+ function Get-ArchiveIdentity([string]$Path, [string]$RequestedRevision) {
317
+ $probe = Join-Path ([System.IO.Path]::GetTempPath()) "afyx-graph-identity-$([guid]::NewGuid().ToString('N'))"
318
+ try {
319
+ Expand-Archive -LiteralPath $Path -DestinationPath $probe
320
+ $bundleMetadata = Join-Path $probe "afyx-graph-$target\metadata.json"
321
+ $arguments = @($artifactIdentityTool, 'inspect', '--metadata', $bundleMetadata)
322
+ if ($RequestedRevision) { $arguments += @('--requested-revision', $RequestedRevision) }
323
+ $json = (& (Get-Command node).Source @arguments 2>$null) -join ''
324
+ if ($LASTEXITCODE -ne 0) { throw 'Artifact identity could not be inspected.' }
325
+ return $json | ConvertFrom-Json
326
+ } finally {
327
+ if (Test-Path -LiteralPath $probe) { Remove-Item -LiteralPath $probe -Recurse -Force }
328
+ }
329
+ }
330
+
331
+ function Test-ArchiveChecksum([string]$Path) {
332
+ $sumsPath = Join-Path (Split-Path -Parent $Path) 'SHA256SUMS'
333
+ if (-not (Test-Path -LiteralPath $sumsPath -PathType Leaf)) { throw "SHA256SUMS is required beside $Path." }
334
+ $line = Get-Content -LiteralPath $sumsPath | Where-Object { $_ -match "(?i)^([0-9a-f]{64})\s+\*?$([regex]::Escape((Split-Path -Leaf $Path)))$" } | Select-Object -First 1
335
+ if (-not $line) { throw "SHA256SUMS has no entry for $(Split-Path -Leaf $Path)." }
336
+ $expected = ([regex]::Match($line, '^[0-9a-fA-F]{64}')).Value.ToLowerInvariant()
337
+ $actual = (Get-FileHash -Algorithm SHA256 -LiteralPath $Path).Hash.ToLowerInvariant()
338
+ if ($actual -ne $expected) { throw "Checksum mismatch for $(Split-Path -Leaf $Path)." }
339
+ }
340
+
341
+ function Test-ArchiveBundle([string]$Path) {
342
+ $probe = Join-Path ([System.IO.Path]::GetTempPath()) "afyx-graph-probe-$([guid]::NewGuid().ToString('N'))"
343
+ try {
344
+ Expand-Archive -LiteralPath $Path -DestinationPath $probe
345
+ Test-StagedBundle -Path (Join-Path $probe "afyx-graph-$target")
346
+ } finally {
347
+ if (Test-Path -LiteralPath $probe) { Remove-Item -LiteralPath $probe -Recurse -Force }
348
+ }
349
+ }
350
+
351
+ function Test-StagedBundle([string]$Path) {
352
+ $required = @('bin\afyx-graph.cmd', 'metadata.json', 'LICENSE')
353
+ foreach ($relative in $required) {
354
+ if (-not (Test-Path -LiteralPath (Join-Path $Path $relative) -PathType Leaf)) {
355
+ throw "Afyx Graph staged bundle is incomplete: $relative is missing."
356
+ }
357
+ }
358
+ $stagedMetadata = Get-Content -Raw -LiteralPath (Join-Path $Path 'metadata.json') -Encoding utf8 | ConvertFrom-Json
359
+ if ($stagedMetadata.product_name -ne 'Afyx Graph' -or $stagedMetadata.release_channel -ne $releaseChannel) {
360
+ throw 'Afyx Graph staged bundle identity or release channel is invalid.'
361
+ }
362
+ if ($stagedMetadata.product_version -ne $metadata.product_version) {
363
+ throw "Afyx Graph version mismatch: expected $($metadata.product_version), got $($stagedMetadata.product_version)."
364
+ }
365
+ if ($target -notin @($stagedMetadata.supported_platforms)) { throw "Afyx Graph bundle does not support $target." }
366
+ Invoke-LoggedNative -FilePath ((Get-Command node).Source) -Arguments @($contractPath, 'verify-bundle', '--root', $Path, '--target', $target) -FailureMessage 'Afyx Graph staged bundle failed the distribution contract.'
367
+ }
368
+
369
+ function Test-StagedRuntimeIdentity([string]$Path) {
370
+ $metadataPath = Join-Path $Path 'metadata.json'
371
+ $identity = (& (Get-Command node).Source $artifactIdentityTool inspect --metadata $metadataPath 2>$null) -join '' | ConvertFrom-Json
372
+ if ($identity.extractionVersion -eq $null) { return }
373
+ $compiled = Join-Path $Path 'lib\dist\extraction\extraction-version.js'
374
+ $actual = (& (Get-Command node).Source -e "process.stdout.write(String(require(process.argv[1]).EXTRACTION_VERSION))" $compiled 2>$null) -join ''
375
+ if ($LASTEXITCODE -ne 0 -or [int]$actual -ne [int]$identity.extractionVersion) {
376
+ throw "Installed artifact extraction identity mismatch: metadata $($identity.extractionVersion), compiled runtime $actual."
377
+ }
378
+ }
379
+
380
+ if (Test-Path -LiteralPath $recoveryRoot) {
381
+ if (Test-Path -LiteralPath $RuntimeRoot) {
382
+ throw "Interrupted Graph transaction is ambiguous: both $RuntimeRoot and $recoveryRoot exist. Refusing to modify either path."
383
+ }
384
+ if ($ValidateOnly -or $Uninstall -or $WhatIfPreference) {
385
+ throw "Interrupted Graph transaction found at $recoveryRoot. Retry the original install/update operation to restore it before validation or removal."
386
+ }
387
+ if (-not (Test-AfyxOwnershipAt -Path $recoveryRoot)) {
388
+ throw "Refusing to restore $recoveryRoot because Afyx Graph ownership cannot be verified."
389
+ }
390
+ Move-Item -LiteralPath $recoveryRoot -Destination $RuntimeRoot
391
+ Write-Host "Recovered interrupted Afyx Graph transaction at $RuntimeRoot."
392
+ }
393
+
394
+ $state = Get-State
395
+ if ($ValidateOnly) {
396
+ $installedMetadata = if (Test-Path -LiteralPath $rootMetadata) { Get-Content -Raw $rootMetadata | ConvertFrom-Json } else { $null }
397
+ $currentCheckoutRevision = Get-RequestedRevision
398
+ $revisionStatus = if (-not $installedMetadata -or -not $installedMetadata.source_revision -or -not $currentCheckoutRevision) {
399
+ 'UNKNOWN'
400
+ } elseif ([string]$installedMetadata.source_revision -eq $currentCheckoutRevision) {
401
+ 'MATCH'
402
+ } else {
403
+ 'REVISION_MISMATCH'
404
+ }
405
+ [pscustomobject]@{
406
+ Product = 'Afyx Graph'
407
+ State = $state
408
+ RuntimeRoot = $RuntimeRoot
409
+ Version = if ($installedMetadata) { $installedMetadata.product_version } else { $null }
410
+ SourceRevision = if ($installedMetadata -and $installedMetadata.source_revision) { $installedMetadata.source_revision } else { 'UNKNOWN' }
411
+ ExtractionVersion = if ($installedMetadata) { $installedMetadata.extraction_version } else { $null }
412
+ Provenance = if ($installedMetadata -and $installedMetadata.artifact_provenance) { $installedMetadata.artifact_provenance } else { 'UNKNOWN' }
413
+ ReleaseManifestStatus = if ($installedMetadata -and $installedMetadata.release_manifest_status) { $installedMetadata.release_manifest_status } else { 'MISSING' }
414
+ PublisherAuthenticity = if ($installedMetadata -and $installedMetadata.publisher_authenticity) { $installedMetadata.publisher_authenticity } else { 'UNVERIFIED' }
415
+ BuildProvenance = if ($installedMetadata -and $installedMetadata.build_provenance) { $installedMetadata.build_provenance } else { 'UNVERIFIED' }
416
+ CurrentCheckoutRevision = if ($currentCheckoutRevision) { $currentCheckoutRevision } else { 'UNKNOWN' }
417
+ RevisionStatus = $revisionStatus
418
+ UpdateAvailable = $revisionStatus -eq 'REVISION_MISMATCH'
419
+ } | Format-List
420
+ if ($revisionStatus -eq 'REVISION_MISMATCH') { Write-Host 'UPDATE AVAILABLE - REVISION MISMATCH' }
421
+ if ($state -in @('INCOMPLETE', 'INVALID')) { exit 1 }
422
+ exit 0
423
+ }
424
+
425
+ if ($Uninstall) {
426
+ if ($state -eq 'NOT INSTALLED') { Write-Host 'Afyx Graph: not installed'; exit 0 }
427
+ if (-not (Test-AfyxOwnership)) { throw "Refusing to remove $RuntimeRoot because Afyx Graph ownership cannot be verified." }
428
+ if ($PSCmdlet.ShouldProcess($RuntimeRoot, 'Remove Afyx-owned Graph runtime')) {
429
+ Remove-Item -LiteralPath $RuntimeRoot -Recurse -Force
430
+ Update-UserPath -Remove
431
+ }
432
+ Write-Host 'Afyx Graph: removed; project .afyx-graph indexes were not changed.'
433
+ exit 0
434
+ }
435
+
436
+ if ($state -ne 'NOT INSTALLED' -and -not ($Replace -or $Update)) {
437
+ throw "Afyx Graph already exists at $RuntimeRoot. Use -Replace after inspecting the installation."
438
+ }
439
+ if ($state -ne 'NOT INSTALLED' -and -not (Test-AfyxOwnership)) {
440
+ throw "Refusing to replace $RuntimeRoot because Afyx Graph ownership cannot be verified."
441
+ }
442
+ if ($WhatIfPreference) {
443
+ $nodeVersion = Get-NodeVersion
444
+ Write-Host "WhatIf: would resolve, verify, stage, and install $assetName to $RuntimeRoot"
445
+ Write-Host "Release channel: $releaseChannel; Node.js: $(if ($nodeVersion) { $nodeVersion } else { 'missing/incompatible' })"
446
+ exit 0
447
+ }
448
+
449
+ $nodeVersion = Assert-HostRuntime
450
+ $archiveResults = @(Resolve-Archive)
451
+ if ($archiveResults.Count -ne 1) { throw "Archive resolution returned $($archiveResults.Count) values instead of one artifact record. Log: $diagnosticLog" }
452
+ $resolvedArchive = $archiveResults[0]
453
+ if ($resolvedArchive -isnot [pscustomobject]) { throw 'Archive resolution did not return a structured artifact record.' }
454
+ foreach ($property in @('Path', 'Source', 'Commit', 'BuildProvenance')) {
455
+ if (-not $resolvedArchive.PSObject.Properties[$property]) { throw "Archive resolution omitted $property." }
456
+ }
457
+ if ([string]::IsNullOrWhiteSpace([string]$resolvedArchive.Path) -or -not (Test-Path -LiteralPath $resolvedArchive.Path -PathType Leaf)) {
458
+ throw 'Archive resolution returned a missing or invalid artifact path.'
459
+ }
460
+ if ($resolvedArchive.Source -notin @('explicit-archive', 'local-release-cache', 'github-release', 'local-build')) {
461
+ throw "Archive resolution returned an unsupported source: $($resolvedArchive.Source)."
462
+ }
463
+ if ($resolvedArchive.Source -ne 'local-build' -and -not $resolvedArchive.PSObject.Properties['RevisionStatus']) {
464
+ throw 'Archive resolution omitted revision status.'
465
+ }
466
+ $archive = $resolvedArchive.Path
467
+ Test-ArchiveChecksum -Path $archive
468
+ $parent = Split-Path -Parent $RuntimeRoot
469
+ New-Item -ItemType Directory -Force -Path $parent | Out-Null
470
+ $transaction = Join-Path $parent ".graph-stage-$([guid]::NewGuid().ToString('N'))"
471
+ $extract = Join-Path $transaction 'extract'
472
+ $prepared = Join-Path $transaction 'prepared'
473
+ $swapped = $false
474
+ try {
475
+ New-Item -ItemType Directory -Force -Path $extract, $prepared | Out-Null
476
+ Expand-Archive -LiteralPath $archive -DestinationPath $extract
477
+ $bundle = Get-ChildItem -LiteralPath $extract -Directory | Where-Object Name -eq "afyx-graph-$target" | Select-Object -First 1
478
+ if (-not $bundle) { throw "Archive does not contain afyx-graph-$target." }
479
+ Test-StagedBundle -Path $bundle.FullName
480
+ Test-StagedRuntimeIdentity -Path $bundle.FullName
481
+ $requestedRevision = Get-RequestedRevision
482
+ $releaseManifest = Join-Path (Split-Path -Parent $archive) 'RELEASE-MANIFEST.json'
483
+ $manifestStatus = 'MISSING'
484
+ $publisherAuthenticity = 'UNVERIFIED'
485
+ $buildProvenance = 'UNVERIFIED'
486
+ if (Test-Path -LiteralPath $releaseManifest -PathType Leaf) {
487
+ & (Get-Command node).Source $artifactIdentityTool verify-release-manifest --manifest $releaseManifest --archive $archive --metadata (Join-Path $bundle.FullName 'metadata.json') *> $null
488
+ if ($LASTEXITCODE -ne 0) { throw 'Release manifest verification failed.' }
489
+ $manifestStatus = 'VERIFIED'
490
+ $buildProvenance = 'COMMIT_BOUND_MANIFEST'
491
+ } elseif ($resolvedArchive.Source -eq 'github-release') {
492
+ throw 'GitHub release is missing RELEASE-MANIFEST.json.'
493
+ }
494
+ $gh = Get-Command gh -ErrorAction SilentlyContinue
495
+ if ($resolvedArchive.Source -eq 'github-release' -and $gh) {
496
+ & $gh.Source attestation verify $archive --repo a5zero7/afyx-engineering-kit *> $null
497
+ if ($LASTEXITCODE -ne 0) { throw 'GitHub publisher/build attestation verification failed.' }
498
+ $publisherAuthenticity = 'GITHUB_ATTESTED'
499
+ $buildProvenance = 'GITHUB_ATTESTED'
500
+ }
501
+ if ($resolvedArchive.Source -eq 'local-build' -and $resolvedArchive.PSObject.Properties['BuildProvenance'] -and $resolvedArchive.BuildProvenance) { $buildProvenance = $resolvedArchive.BuildProvenance }
502
+ & (Get-Command node).Source $artifactIdentityTool record-install --metadata (Join-Path $bundle.FullName 'metadata.json') --archive $archive --provenance $resolvedArchive.Source --requested-revision $(if ($requestedRevision) { $requestedRevision } else { 'UNKNOWN' }) --release-manifest-status $manifestStatus --publisher-authenticity $publisherAuthenticity --build-provenance $buildProvenance *> $null
503
+ if ($LASTEXITCODE -ne 0) { throw 'Failed to record installed artifact provenance.' }
504
+ Move-Item -LiteralPath $bundle.FullName -Destination (Join-Path $prepared 'current')
505
+ Copy-Item -LiteralPath (Join-Path $prepared 'current\metadata.json') -Destination (Join-Path $prepared 'metadata.json')
506
+ $stagedPublicBin = Join-Path $prepared 'bin'
507
+ New-Item -ItemType Directory -Force -Path $stagedPublicBin | Out-Null
508
+ @"
509
+ @echo off
510
+ @call "%~dp0..\current\bin\afyx-graph.cmd" %*
511
+ "@ | Set-Content -LiteralPath (Join-Path $stagedPublicBin 'afyx-graph.cmd') -Encoding ascii
512
+ if (Test-Path -LiteralPath $RuntimeRoot) { Move-Item -LiteralPath $RuntimeRoot -Destination $recoveryRoot }
513
+ try {
514
+ Move-Item -LiteralPath $prepared -Destination $RuntimeRoot
515
+ $swapped = $true
516
+ if ((Get-State) -ne 'HEALTHY') { throw 'Installed Afyx Graph failed post-swap validation.' }
517
+ $versionOutput = (& $publicLauncher --version 2>&1) -join ' '
518
+ if ($LASTEXITCODE -ne 0 -or $versionOutput -notmatch [regex]::Escape([string]$metadata.product_version)) {
519
+ throw "Installed Afyx Graph CLI verification failed: $versionOutput"
520
+ }
521
+ & $publicLauncher help *> $null
522
+ if ($LASTEXITCODE -ne 0) { throw 'Installed Afyx Graph help command failed.' }
523
+ } catch {
524
+ if (Test-Path -LiteralPath $RuntimeRoot) { Remove-Item -LiteralPath $RuntimeRoot -Recurse -Force }
525
+ if (Test-Path -LiteralPath $recoveryRoot) { Move-Item -LiteralPath $recoveryRoot -Destination $RuntimeRoot }
526
+ throw
527
+ }
528
+ if (Test-Path -LiteralPath $recoveryRoot) { Remove-Item -LiteralPath $recoveryRoot -Recurse -Force }
529
+ } finally {
530
+ if (Test-Path -LiteralPath $transaction) { Remove-Item -LiteralPath $transaction -Recurse -Force }
531
+ if (-not $swapped -and (Test-Path -LiteralPath $recoveryRoot) -and -not (Test-Path -LiteralPath $RuntimeRoot)) {
532
+ Move-Item -LiteralPath $recoveryRoot -Destination $RuntimeRoot
533
+ }
534
+ }
535
+
536
+ Update-UserPath
537
+
538
+ $mcpState = 'MCP_NOT_CONFIGURED'
539
+ if ($SkipMcp) {
540
+ Write-Host 'Afyx Graph MCP: skipped explicitly.'
541
+ } else {
542
+ $codexCommand = Get-Command codex.exe -ErrorAction SilentlyContinue
543
+ if (-not $codexCommand) { $codexCommand = Get-Command codex.cmd -ErrorAction SilentlyContinue }
544
+ $codexHome = if ($env:CODEX_HOME) { $env:CODEX_HOME } else { Join-Path $env:USERPROFILE '.codex' }
545
+ $installedEntry = Join-Path $RuntimeRoot 'current\lib\dist\bin\afyx-graph.js'
546
+ $arguments = @(
547
+ (Join-Path $kitRoot 'scripts\afyx-mcp-integration.mjs'),
548
+ '--node', (Get-Command node).Source,
549
+ '--entry', $installedEntry,
550
+ '--codex-home', $codexHome
551
+ )
552
+ if ($codexCommand) { $arguments += @('--codex', $codexCommand.Source) }
553
+ if ($ConfigureMcp) { $arguments += '--apply' }
554
+ $mcpJson = (& (Get-Command node).Source @arguments 2>$null) -join ''
555
+ try {
556
+ $mcpResult = $mcpJson | ConvertFrom-Json
557
+ $mcpState = $mcpResult.mcp
558
+ Write-Host "Afyx Graph MCP: $($mcpResult.mcp) - $($mcpResult.detail)"
559
+ } catch {
560
+ $mcpState = 'MCP_BLOCKED'
561
+ Write-Warning 'Afyx Graph MCP integration could not be evaluated; the healthy Graph runtime was preserved.'
562
+ }
563
+ if (-not $ConfigureMcp -and $mcpState -eq 'MCP_NOT_CONFIGURED') {
564
+ Write-Host 'Registration available: rerun the selected Graph lifecycle with -ConfigureMcp (or use the main installer selection).'
565
+ }
566
+ }
567
+
568
+ Write-Host "Afyx Graph $($metadata.product_version) [$releaseChannel]: installed at $RuntimeRoot"
569
+ Write-Host "Artifact source: $($resolvedArchive.Source); source revision: $(if ($resolvedArchive.Commit) { $resolvedArchive.Commit } else { 'release/explicit archive' })"
570
+ Write-Host "CLI: $publicLauncher"
571
+ if ($SkipMcp) { Write-Host 'MCP configuration was not changed.' }