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,289 @@
1
+ Set-StrictMode -Version Latest
2
+
3
+ $script:MarkerName = '.afyx-install.json'
4
+
5
+ function Get-AfyxBackupRoot {
6
+ if ($env:AFYX_BACKUP_ROOT) { return [IO.Path]::GetFullPath($env:AFYX_BACKUP_ROOT) }
7
+ $homePath = if ($env:USERPROFILE) { $env:USERPROFILE } else { $env:HOME }
8
+ return Join-Path (Join-Path $homePath '.afyx') 'backups'
9
+ }
10
+
11
+ function Assert-AfyxSkillTarget {
12
+ [CmdletBinding()]
13
+ param([Parameter(Mandatory)][string]$SkillsRoot, [Parameter(Mandatory)][string]$Target)
14
+ $rootFull = [IO.Path]::GetFullPath($SkillsRoot).TrimEnd([IO.Path]::DirectorySeparatorChar, [IO.Path]::AltDirectorySeparatorChar)
15
+ $targetFull = [IO.Path]::GetFullPath($Target)
16
+ $parentFull = [IO.Path]::GetFullPath((Split-Path -Parent $targetFull)).TrimEnd([IO.Path]::DirectorySeparatorChar, [IO.Path]::AltDirectorySeparatorChar)
17
+ $comparison = if ($env:OS -eq 'Windows_NT') { [StringComparison]::OrdinalIgnoreCase } else { [StringComparison]::Ordinal }
18
+ if (-not [string]::Equals($rootFull, $parentFull, $comparison) -or [string]::Equals($rootFull, $targetFull, $comparison)) {
19
+ throw "Skill target escapes configured root: $Target"
20
+ }
21
+ foreach ($path in @($rootFull, $targetFull)) {
22
+ if (Test-Path -LiteralPath $path) {
23
+ $item = Get-Item -LiteralPath $path -Force
24
+ if (($item.Attributes -band [IO.FileAttributes]::ReparsePoint) -ne 0) { throw "Refusing reparse-point skill path: $path" }
25
+ }
26
+ }
27
+ }
28
+
29
+ function Restore-AfyxInterruptedSkillTransaction {
30
+ [CmdletBinding()]
31
+ param([Parameter(Mandatory)][string]$Target)
32
+ $old = Join-Path (Split-Path -Parent $Target) ".$((Split-Path -Leaf $Target)).afyx-old"
33
+ if ((Test-Path -LiteralPath $old) -and -not (Test-Path -LiteralPath $Target)) {
34
+ Move-Item -LiteralPath $old -Destination $Target -ErrorAction Stop
35
+ Write-Warning "Recovered interrupted skill transaction: $Target"
36
+ } elseif ((Test-Path -LiteralPath $old) -and (Test-Path -LiteralPath $Target)) {
37
+ throw "Ambiguous interrupted skill transaction; preserve both $Target and $old for manual recovery."
38
+ }
39
+ }
40
+
41
+ function Get-AfyxSkillDigest {
42
+ [CmdletBinding()]
43
+ param([Parameter(Mandatory)][string]$Directory)
44
+ $root = [IO.Path]::GetFullPath($Directory).TrimEnd([IO.Path]::DirectorySeparatorChar, [IO.Path]::AltDirectorySeparatorChar)
45
+ $lines = @()
46
+ $files = @(Get-ChildItem -LiteralPath $root -File -Recurse -Force | Where-Object {
47
+ $_.Name -ne $script:MarkerName -and $_.FullName -notmatch '[\\/]\.git[\\/]'
48
+ } | Sort-Object FullName)
49
+ foreach ($file in $files) {
50
+ $relative = $file.FullName.Substring($root.Length).TrimStart('\', '/').Replace('\', '/')
51
+ $lines += "$(Get-FileHash -Algorithm SHA256 -LiteralPath $file.FullName | Select-Object -ExpandProperty Hash) ./$relative"
52
+ }
53
+ $bytes = [Text.Encoding]::UTF8.GetBytes(($lines -join "`n") + "`n")
54
+ $sha = [Security.Cryptography.SHA256]::Create()
55
+ try { return ([BitConverter]::ToString($sha.ComputeHash($bytes))).Replace('-', '').ToLowerInvariant() }
56
+ finally { $sha.Dispose() }
57
+ }
58
+
59
+ function Write-AfyxSkillMarker {
60
+ [CmdletBinding()]
61
+ param(
62
+ [Parameter(Mandatory)][string]$Target,
63
+ [Parameter(Mandatory)][string]$Component,
64
+ [Parameter(Mandatory)][ValidateSet('afyx', 'upstream')][string]$Owner,
65
+ [string]$Version = '', [string]$SourceRepository = '', [string]$SourceRevision = ''
66
+ )
67
+ $record = [ordered]@{
68
+ schema = 1
69
+ product = 'Afyx Codex Engineering Kit'
70
+ component = $Component
71
+ owner = $Owner
72
+ version = $Version
73
+ content_sha256 = if ($Owner -eq 'afyx') { Get-AfyxSkillDigest -Directory $Target } else { '' }
74
+ source_repository = $SourceRepository
75
+ source_revision = $SourceRevision
76
+ }
77
+ $marker = Join-Path $Target $script:MarkerName
78
+ [IO.File]::WriteAllText($marker, (($record | ConvertTo-Json -Depth 3) + "`n"), (New-Object Text.UTF8Encoding($false)))
79
+ if ($Owner -eq 'upstream' -and (Test-Path -LiteralPath (Join-Path $Target '.git'))) {
80
+ $info = Join-Path $Target '.git\info'
81
+ New-Item -ItemType Directory -Force -Path $info | Out-Null
82
+ $exclude = Join-Path $info 'exclude'
83
+ $entry = "/$script:MarkerName"
84
+ $existing = if (Test-Path -LiteralPath $exclude) { @(Get-Content -LiteralPath $exclude) } else { @() }
85
+ if ($entry -notin $existing) { Add-Content -LiteralPath $exclude -Value $entry -Encoding ascii }
86
+ }
87
+ }
88
+
89
+ function Get-AfyxSkillOwnership {
90
+ [CmdletBinding()]
91
+ param([Parameter(Mandatory)][string]$Target, [Parameter(Mandatory)][string]$Component, [Parameter(Mandatory)][string]$ExpectedOwner)
92
+ if (-not (Test-Path -LiteralPath $Target)) { return 'NOT_INSTALLED' }
93
+ try {
94
+ $targetItem = Get-Item -LiteralPath $Target -Force
95
+ if (-not $targetItem.PSIsContainer -or ($targetItem.Attributes -band [IO.FileAttributes]::ReparsePoint) -ne 0) { return 'UNSAFE' }
96
+ $markerPath = Join-Path $Target $script:MarkerName
97
+ if (-not (Test-Path -LiteralPath $markerPath -PathType Leaf)) { return 'FOREIGN_UNPROVEN' }
98
+ $marker = Get-Content -Raw -LiteralPath $markerPath -Encoding utf8 | ConvertFrom-Json -ErrorAction Stop
99
+ if ($marker.schema -ne 1 -or $marker.product -ne 'Afyx Codex Engineering Kit' -or $marker.component -ne $Component -or $marker.owner -ne $ExpectedOwner) { return 'FOREIGN_UNPROVEN' }
100
+ if ($ExpectedOwner -eq 'afyx') {
101
+ if ([string]$marker.content_sha256 -notmatch '^[0-9a-f]{64}$') { return 'FOREIGN_UNPROVEN' }
102
+ if ((Get-AfyxSkillDigest -Directory $Target) -eq $marker.content_sha256) { return 'AFYX_OWNED_UNMODIFIED' }
103
+ return 'AFYX_OWNED_MODIFIED'
104
+ }
105
+ if (-not (Test-Path -LiteralPath (Join-Path $Target '.git'))) { return 'FOREIGN_UNPROVEN' }
106
+ $head = (& git -C $Target rev-parse HEAD 2>$null) -join ''
107
+ $remote = (& git -C $Target remote get-url origin 2>$null) -join ''
108
+ $dirty = (& git -C $Target status --porcelain 2>$null) -join ''
109
+ if ($LASTEXITCODE -eq 0 -and $head -eq $marker.source_revision -and $remote -eq $marker.source_repository -and -not $dirty) { return 'EXTERNAL_UPSTREAM' }
110
+ return 'FOREIGN_UNPROVEN'
111
+ } catch { return 'FOREIGN_UNPROVEN' }
112
+ }
113
+
114
+ function Get-AfyxLegacySkillProvenance {
115
+ [CmdletBinding()]
116
+ param(
117
+ [Parameter(Mandatory)][string]$Target,
118
+ [Parameter(Mandatory)][string]$Component,
119
+ [Parameter(Mandatory)][ValidateSet('afyx', 'upstream')][string]$Owner,
120
+ [string]$LegacyVersion = '', [string]$LegacyContentSha256 = '',
121
+ [string]$SourceRepository = '', [string]$SourceRevision = ''
122
+ )
123
+ if (-not (Test-Path -LiteralPath $Target -PathType Container)) { return 'UNKNOWN' }
124
+ if (Test-Path -LiteralPath (Join-Path $Target $script:MarkerName)) { return 'UNKNOWN' }
125
+ try {
126
+ $item = Get-Item -LiteralPath $Target -Force
127
+ if (($item.Attributes -band [IO.FileAttributes]::ReparsePoint) -ne 0) { return 'UNKNOWN' }
128
+ if ($Owner -eq 'afyx') {
129
+ if ($LegacyContentSha256 -notmatch '^[0-9a-f]{64}$') { return 'UNKNOWN' }
130
+ if ((Get-AfyxSkillDigest -Directory $Target) -eq $LegacyContentSha256) { return 'MATCH' }
131
+ $manifest = Join-Path $Target 'SKILL.md'
132
+ if (-not (Test-Path -LiteralPath $manifest -PathType Leaf)) { return 'UNKNOWN' }
133
+ $content = Get-Content -Raw -LiteralPath $manifest -Encoding utf8
134
+ $nameMatch = [regex]::Match($content, '(?m)^name:\s*["'']?([^"''\r\n]+?)["'']?\s*$')
135
+ $versionMatch = [regex]::Match($content, '(?m)^\s*version:\s*["'']?([^"''\s]+)["'']?\s*$')
136
+ if (-not $nameMatch.Success) { return 'UNKNOWN' }
137
+ if ($nameMatch.Groups[1].Value.Trim() -ne $Component) { return 'FOREIGN' }
138
+ if ($versionMatch.Success -and $versionMatch.Groups[1].Value -eq $LegacyVersion) { return 'MODIFIED' }
139
+ return 'UNKNOWN'
140
+ }
141
+ if (-not (Test-Path -LiteralPath (Join-Path $Target '.git'))) { return 'UNKNOWN' }
142
+ $remote = ((& git -C $Target remote get-url origin 2>$null) -join '').Trim()
143
+ if ($LASTEXITCODE -ne 0) { return 'UNKNOWN' }
144
+ if ($remote -ne $SourceRepository) { return 'FOREIGN' }
145
+ $dirty = ((& git -C $Target status --porcelain 2>$null) -join '').Trim()
146
+ if ($LASTEXITCODE -ne 0) { return 'UNKNOWN' }
147
+ if ($dirty) { return 'MODIFIED' }
148
+ $head = ((& git -C $Target rev-parse HEAD 2>$null) -join '').Trim()
149
+ if ($LASTEXITCODE -ne 0 -or $head -ne $SourceRevision) { return 'UNKNOWN' }
150
+ return 'MATCH'
151
+ } catch { return 'UNKNOWN' }
152
+ }
153
+
154
+ function Adopt-AfyxLegacySkill {
155
+ [CmdletBinding()]
156
+ param(
157
+ [Parameter(Mandatory)][string]$SkillsRoot,
158
+ [Parameter(Mandatory)][string]$Target,
159
+ [Parameter(Mandatory)][string]$Component,
160
+ [Parameter(Mandatory)][ValidateSet('afyx', 'upstream')][string]$Owner,
161
+ [string]$Version = '', [string]$LegacyVersion = '', [string]$LegacyContentSha256 = '',
162
+ [string]$SourceRepository = '', [string]$SourceRevision = ''
163
+ )
164
+ Assert-AfyxSkillTarget -SkillsRoot $SkillsRoot -Target $Target
165
+ Restore-AfyxInterruptedSkillTransaction -Target $Target
166
+ $current = Get-AfyxSkillOwnership -Target $Target -Component $Component -ExpectedOwner $Owner
167
+ if ($current -in @('AFYX_OWNED_UNMODIFIED', 'EXTERNAL_UPSTREAM')) { return 'ALREADY_ADOPTED' }
168
+ if (Test-Path -LiteralPath (Join-Path $Target $script:MarkerName)) {
169
+ throw "Refusing to adopt $Component because ownership metadata already exists but is not valid."
170
+ }
171
+ $provenance = Get-AfyxLegacySkillProvenance -Target $Target -Component $Component -Owner $Owner `
172
+ -LegacyVersion $LegacyVersion -LegacyContentSha256 $LegacyContentSha256 `
173
+ -SourceRepository $SourceRepository -SourceRevision $SourceRevision
174
+ if ($provenance -ne 'MATCH') { throw "Refusing to adopt $Component because legacy provenance is $provenance." }
175
+ $backup = Backup-AfyxSkill -Component $Component -Target $Target
176
+ if ((Get-AfyxSkillDigest -Directory $backup) -ne (Get-AfyxSkillDigest -Directory $Target)) {
177
+ throw "$Component adoption backup verification failed."
178
+ }
179
+ if ($Owner -eq 'upstream' -and (Get-AfyxLegacySkillProvenance -Target $backup -Component $Component -Owner $Owner `
180
+ -LegacyVersion $LegacyVersion -LegacyContentSha256 $LegacyContentSha256 `
181
+ -SourceRepository $SourceRepository -SourceRevision $SourceRevision) -ne 'MATCH') {
182
+ throw "$Component adoption backup provenance verification failed."
183
+ }
184
+ $exclude = Join-Path $Target '.git\info\exclude'
185
+ $excludeEntry = "/$script:MarkerName"
186
+ $excludeAlreadyPresent = (Test-Path -LiteralPath $exclude) -and ($excludeEntry -in @(Get-Content -LiteralPath $exclude))
187
+ try {
188
+ Write-AfyxSkillMarker -Target $Target -Component $Component -Owner $Owner -Version $Version `
189
+ -SourceRepository $SourceRepository -SourceRevision $SourceRevision
190
+ $expected = if ($Owner -eq 'afyx') { 'AFYX_OWNED_UNMODIFIED' } else { 'EXTERNAL_UPSTREAM' }
191
+ if ((Get-AfyxSkillOwnership -Target $Target -Component $Component -ExpectedOwner $Owner) -ne $expected) {
192
+ throw "$Component post-adoption ownership verification failed."
193
+ }
194
+ } catch {
195
+ Remove-Item -LiteralPath (Join-Path $Target $script:MarkerName) -Force -ErrorAction SilentlyContinue
196
+ if ($Owner -eq 'upstream' -and -not $excludeAlreadyPresent -and (Test-Path -LiteralPath $exclude)) {
197
+ $remaining = @(Get-Content -LiteralPath $exclude | Where-Object { $_ -ne $excludeEntry })
198
+ [IO.File]::WriteAllLines($exclude, $remaining, (New-Object Text.UTF8Encoding($false)))
199
+ }
200
+ throw
201
+ }
202
+ return "ADOPTED backup=$backup"
203
+ }
204
+
205
+ function Backup-AfyxSkill {
206
+ [CmdletBinding()]
207
+ param([Parameter(Mandatory)][string]$Component, [Parameter(Mandatory)][string]$Target)
208
+ $componentRoot = Join-Path (Get-AfyxBackupRoot) $Component
209
+ New-Item -ItemType Directory -Force -Path $componentRoot | Out-Null
210
+ $limit = if ($env:AFYX_BACKUP_LIMIT -match '^\d+$') { [int]$env:AFYX_BACKUP_LIMIT } else { 20 }
211
+ if ($limit -le 0) { $limit = 20 }
212
+ $existing = @(Get-ChildItem -LiteralPath $componentRoot -Directory -Force)
213
+ if ($existing.Count -ge $limit) {
214
+ $eligible = @($existing | Where-Object {
215
+ $_.Name -match '^\d{8}-\d{6}-[\w-]+$' -and
216
+ -not ($_.Attributes -band [IO.FileAttributes]::ReparsePoint)
217
+ } | Sort-Object -Property CreationTime, LastWriteTime, Name)
218
+
219
+ $toPrune = ($existing.Count - $limit) + 1
220
+ $pruned = 0
221
+ foreach ($oldBackup in $eligible) {
222
+ if ($pruned -ge $toPrune) { break }
223
+ try {
224
+ Remove-Item -LiteralPath $oldBackup.FullName -Recurse -Force -ErrorAction Stop
225
+ $pruned++
226
+ Write-Verbose "Pruned historical backup: $($oldBackup.FullName)"
227
+ } catch {
228
+ Write-Warning "Could not prune historical backup $($oldBackup.FullName): $_"
229
+ }
230
+ }
231
+ }
232
+ $destination = Join-Path $componentRoot "$(Get-Date -Format 'yyyyMMdd-HHmmss')-$([guid]::NewGuid().ToString('N'))"
233
+ Copy-Item -LiteralPath $Target -Destination $destination -Recurse -Force -ErrorAction Stop
234
+ return $destination
235
+ }
236
+
237
+ function Install-AfyxSkillTransaction {
238
+ [CmdletBinding()]
239
+ param(
240
+ [Parameter(Mandatory)][string]$SkillsRoot, [Parameter(Mandatory)][string]$Component,
241
+ [Parameter(Mandatory)][string]$Name, [Parameter(Mandatory)][string]$Stage,
242
+ [Parameter(Mandatory)][string]$Target, [Parameter(Mandatory)][ValidateSet('afyx', 'upstream')][string]$Owner,
243
+ [string]$Version = '', [string]$SourceRepository = '', [string]$SourceRevision = '', [switch]$AuthorizeUnproven
244
+ )
245
+ Assert-AfyxSkillTarget -SkillsRoot $SkillsRoot -Target $Target
246
+ Restore-AfyxInterruptedSkillTransaction -Target $Target
247
+ if ((Get-Item -LiteralPath $Stage -Force).Attributes -band [IO.FileAttributes]::ReparsePoint) { throw "$Name staging directory is a reparse point." }
248
+ Write-AfyxSkillMarker -Target $Stage -Component $Component -Owner $Owner -Version $Version -SourceRepository $SourceRepository -SourceRevision $SourceRevision
249
+ $state = Get-AfyxSkillOwnership -Target $Target -Component $Component -ExpectedOwner $Owner
250
+ if ($state -eq 'AFYX_OWNED_MODIFIED' -and -not $AuthorizeUnproven) {
251
+ throw "Refusing to replace $Target ($state) without explicit repair authorization."
252
+ }
253
+ if ($state -notin @('NOT_INSTALLED', 'AFYX_OWNED_UNMODIFIED', 'AFYX_OWNED_MODIFIED', 'EXTERNAL_UPSTREAM')) {
254
+ throw "Refusing to take ownership of $Target ($state)."
255
+ }
256
+ if ($state -eq 'UNSAFE') { throw "Refusing unsafe skill target $Target." }
257
+ $old = Join-Path (Split-Path -Parent $Target) ".$((Split-Path -Leaf $Target)).afyx-old"
258
+ $backup = ''
259
+ $hadExisting = Test-Path -LiteralPath $Target
260
+ if ($hadExisting) { $backup = Backup-AfyxSkill -Component $Component -Target $Target; Move-Item -LiteralPath $Target -Destination $old -ErrorAction Stop }
261
+ try {
262
+ Move-Item -LiteralPath $Stage -Destination $Target -ErrorAction Stop
263
+ $installed = Get-AfyxSkillOwnership -Target $Target -Component $Component -ExpectedOwner $Owner
264
+ if ($installed -notin @('AFYX_OWNED_UNMODIFIED', 'EXTERNAL_UPSTREAM')) { throw "$Name post-install ownership verification failed." }
265
+ if ($hadExisting) { Remove-Item -LiteralPath $old -Recurse -Force -ErrorAction Stop }
266
+ if ($backup) { Write-Verbose "Backup: $backup" }
267
+ } catch {
268
+ if (Test-Path -LiteralPath $Target) { Remove-Item -LiteralPath $Target -Recurse -Force -ErrorAction SilentlyContinue }
269
+ if ($hadExisting -and (Test-Path -LiteralPath $old) -and -not (Test-Path -LiteralPath $Target)) { Move-Item -LiteralPath $old -Destination $Target -ErrorAction SilentlyContinue }
270
+ throw
271
+ }
272
+ }
273
+
274
+ function Remove-AfyxSkillSafely {
275
+ [CmdletBinding(SupportsShouldProcess)]
276
+ param([Parameter(Mandatory)][string]$SkillsRoot, [Parameter(Mandatory)][string]$Component, [Parameter(Mandatory)][string]$Target, [Parameter(Mandatory)][string]$ExpectedOwner)
277
+ Assert-AfyxSkillTarget -SkillsRoot $SkillsRoot -Target $Target
278
+ Restore-AfyxInterruptedSkillTransaction -Target $Target
279
+ $state = Get-AfyxSkillOwnership -Target $Target -Component $Component -ExpectedOwner $ExpectedOwner
280
+ if ($state -eq 'NOT_INSTALLED') { return }
281
+ if ($state -notin @('AFYX_OWNED_UNMODIFIED', 'EXTERNAL_UPSTREAM')) { throw "Refusing to remove $Target because installed ownership is $state." }
282
+ if ($PSCmdlet.ShouldProcess($Target, 'Back up and remove installed skill')) {
283
+ $backup = Backup-AfyxSkill -Component $Component -Target $Target
284
+ Remove-Item -LiteralPath $Target -Recurse -Force -ErrorAction Stop
285
+ Write-Host "Removed: $Target (backup: $backup)"
286
+ }
287
+ }
288
+
289
+ Export-ModuleMember -Function Assert-AfyxSkillTarget, Restore-AfyxInterruptedSkillTransaction, Get-AfyxSkillDigest, Write-AfyxSkillMarker, Get-AfyxSkillOwnership, Get-AfyxLegacySkillProvenance, Adopt-AfyxLegacySkill, Backup-AfyxSkill, Install-AfyxSkillTransaction, Remove-AfyxSkillSafely