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,256 @@
|
|
|
1
|
+
# shellcheck shell=bash
|
|
2
|
+
# Canonical component model for the Afyx Codex Engineering Kit (Bash 3.2+).
|
|
3
|
+
# The data lives once in scripts/components.json; installer, updater,
|
|
4
|
+
# uninstaller, verifier and doctor all ask this library for component state.
|
|
5
|
+
# Read-only: nothing here writes, installs, or changes configuration.
|
|
6
|
+
#
|
|
7
|
+
# Callers set (or accept the defaults for): skills_root, graph_root, codex_home.
|
|
8
|
+
# afyx_component_evaluate <id> fills AFYX_STATE / AFYX_DETAIL / AFYX_VERSION /
|
|
9
|
+
# AFYX_PATH in the caller's shell (no subshell, so no lost globals).
|
|
10
|
+
|
|
11
|
+
AFYX_COMPONENTS_JSON="${AFYX_COMPONENTS_JSON:-$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)/components.json}"
|
|
12
|
+
skills_root="${skills_root:-${CODEX_SKILLS_ROOT:-$HOME/.agents/skills}}"
|
|
13
|
+
graph_root="${graph_root:-${AFYX_GRAPH_RUNTIME_ROOT:-$HOME/.afyx/graph}}"
|
|
14
|
+
context_root="${context_root:-${AFYX_CONTEXT_RUNTIME_ROOT:-$HOME/.afyx/context}}"
|
|
15
|
+
forge_root="${forge_root:-${AFYX_FORGE_RUNTIME_ROOT:-$HOME/.afyx/forge}}"
|
|
16
|
+
codex_home="${codex_home:-${CODEX_HOME:-$HOME/.codex}}"
|
|
17
|
+
|
|
18
|
+
# One "id" per line, in contract order. The contract is one "key": "value" per line.
|
|
19
|
+
afyx_component_ids() {
|
|
20
|
+
sed -n 's/^[[:space:]]*"id"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$AFYX_COMPONENTS_JSON"
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
# afyx_component_field <id> <key> -> value ("" when absent)
|
|
24
|
+
afyx_component_field() {
|
|
25
|
+
awk -v id="$1" -v key="$2" '
|
|
26
|
+
/^[[:space:]]*\{[[:space:]]*$/ { inobj = 1; hit = 0; val = ""; next }
|
|
27
|
+
inobj && /^[[:space:]]*\}[,]?[[:space:]]*$/ { if (hit) { print val; exit } inobj = 0; next }
|
|
28
|
+
inobj {
|
|
29
|
+
if ($0 ~ "^[[:space:]]*\"id\"[[:space:]]*:[[:space:]]*\"" id "\"") hit = 1
|
|
30
|
+
if (match($0, "^[[:space:]]*\"" key "\"[[:space:]]*:[[:space:]]*\"[^\"]*\"")) {
|
|
31
|
+
s = substr($0, RSTART, RLENGTH); sub(/^[^:]*:[[:space:]]*"/, "", s); sub(/"$/, "", s); val = s
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
' "$AFYX_COMPONENTS_JSON"
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
afyx_provider_ids() {
|
|
38
|
+
sed -n 's/^[[:space:]]*"provider"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$AFYX_COMPONENTS_JSON"
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
afyx_provider_field() {
|
|
42
|
+
awk -v provider="$1" -v key="$2" '
|
|
43
|
+
/^[[:space:]]*\{[[:space:]]*$/ { inobj = 1; hit = 0; val = ""; next }
|
|
44
|
+
inobj && /^[[:space:]]*\}[,]?[[:space:]]*$/ { if (hit) { print val; exit } inobj = 0; next }
|
|
45
|
+
inobj {
|
|
46
|
+
if ($0 ~ "^[[:space:]]*\"provider\"[[:space:]]*:[[:space:]]*\"" provider "\"") hit = 1
|
|
47
|
+
if (match($0, "^[[:space:]]*\"" key "\"[[:space:]]*:[[:space:]]*\"[^\"]*\"")) {
|
|
48
|
+
s = substr($0, RSTART, RLENGTH); sub(/^[^:]*:[[:space:]]*"/, "", s); sub(/"$/, "", s); val = s
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
' "$AFYX_COMPONENTS_JSON"
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
afyx_preset_valid() {
|
|
55
|
+
case "$1" in minimal|recommended|full|custom) return 0 ;; *) return 1 ;; esac
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
afyx_component_desired() {
|
|
59
|
+
local id="$1" preset="$2" custom="${3:-}" providers="${4:-codex}" memberships restriction provider
|
|
60
|
+
afyx_preset_valid "$preset" || return 2
|
|
61
|
+
if [[ "$preset" == custom ]]; then
|
|
62
|
+
case ";$custom;" in *";$id;"*) ;; *) return 1 ;; esac
|
|
63
|
+
else
|
|
64
|
+
memberships="$(afyx_component_field "$id" presets)"
|
|
65
|
+
case ";$memberships;" in *";$preset;"*) ;; *) return 1 ;; esac
|
|
66
|
+
fi
|
|
67
|
+
restriction="$(afyx_component_field "$id" providers)"
|
|
68
|
+
[[ -z "$restriction" ]] && return 0
|
|
69
|
+
local old_ifs="$IFS"; IFS=','
|
|
70
|
+
for provider in $providers; do case ";$restriction;" in *";$provider;"*) IFS="$old_ifs"; return 0 ;; esac; done
|
|
71
|
+
IFS="$old_ifs"; return 1
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
# afyx_control_classify <id> <actual-state> <preset> [custom-id;list] [provider-id,list]
|
|
75
|
+
# Sets a read-only desired-state comparison for callers; it never mutates disk.
|
|
76
|
+
afyx_control_classify() {
|
|
77
|
+
local id="$1" actual="$2" preset="$3" custom="${4:-}" providers="${5:-codex}" declared platform type runtime_verified=true marker field value
|
|
78
|
+
declared="$(afyx_component_field "$id" ownership_class)"
|
|
79
|
+
platform="$(afyx_component_field "$id" platforms)"
|
|
80
|
+
type="$(afyx_component_field "$id" type)"
|
|
81
|
+
AFYX_DESIRED=false; afyx_component_desired "$id" "$preset" "$custom" "$providers" && AFYX_DESIRED=true
|
|
82
|
+
AFYX_ACTUAL_OWNERSHIP="$declared"
|
|
83
|
+
[[ "$actual" == UNKNOWN ]] && AFYX_ACTUAL_OWNERSHIP=USER
|
|
84
|
+
[[ "$actual" == 'NOT INSTALLED' ]] && AFYX_ACTUAL_OWNERSHIP=NONE
|
|
85
|
+
if [[ "$type" == runtime && "$actual" != 'NOT INSTALLED' ]]; then
|
|
86
|
+
runtime_verified=false
|
|
87
|
+
marker="$(afyx_component_root "$id")/$(afyx_component_field "$id" marker_file)"
|
|
88
|
+
field="$(afyx_component_field "$id" marker_field)"; value="$(afyx_component_field "$id" marker_value)"
|
|
89
|
+
if [[ -f "$marker" ]] && grep -q "\"$field\"[[:space:]]*:[[:space:]]*\"$value\"" "$marker"; then runtime_verified=true; fi
|
|
90
|
+
[[ "$runtime_verified" == true ]] || AFYX_ACTUAL_OWNERSHIP=USER
|
|
91
|
+
fi
|
|
92
|
+
AFYX_REPAIR=NONE
|
|
93
|
+
if [[ "$platform" == windows && "$(afyx_platform)" != windows ]]; then
|
|
94
|
+
AFYX_DRIFT=NOT_APPLICABLE; return 0
|
|
95
|
+
fi
|
|
96
|
+
if [[ "$AFYX_DESIRED" != true ]]; then
|
|
97
|
+
[[ "$actual" == 'NOT INSTALLED' ]] && AFYX_DRIFT=ABSENT || AFYX_DRIFT=EXTRA
|
|
98
|
+
return 0
|
|
99
|
+
fi
|
|
100
|
+
if [[ "$runtime_verified" != true ]]; then AFYX_DRIFT=UNVERIFIED; AFYX_REPAIR=BLOCKED; return 0; fi
|
|
101
|
+
case "$actual" in
|
|
102
|
+
HEALTHY) AFYX_DRIFT=IN_SYNC ;;
|
|
103
|
+
'NOT INSTALLED') AFYX_DRIFT=MISSING; AFYX_REPAIR=INSTALL ;;
|
|
104
|
+
DRIFTED) AFYX_DRIFT=MODIFIED; [[ "$declared" == AFYX ]] && AFYX_REPAIR=REPAIR || AFYX_REPAIR=BLOCKED ;;
|
|
105
|
+
INCOMPLETE|INVALID) AFYX_DRIFT=DEGRADED; [[ "$declared" == AFYX ]] && AFYX_REPAIR=REPAIR || AFYX_REPAIR=BLOCKED ;;
|
|
106
|
+
*) AFYX_DRIFT=UNVERIFIED; AFYX_REPAIR=BLOCKED ;;
|
|
107
|
+
esac
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
afyx_control_plan_identity() {
|
|
111
|
+
local preset="$1" custom="${2:-}" providers="${3:-codex}" id desired= separator=
|
|
112
|
+
afyx_preset_valid "$preset" || return 2
|
|
113
|
+
for id in $(afyx_component_ids); do
|
|
114
|
+
[[ "$(afyx_component_field "$id" type)" == detected-cli ]] && continue
|
|
115
|
+
if afyx_component_desired "$id" "$preset" "$custom" "$providers"; then desired="${desired}${separator}${id}"; separator=,; fi
|
|
116
|
+
done
|
|
117
|
+
printf 'v1|%s|%s|%s' "$preset" "$providers" "$desired"
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
afyx_platform() {
|
|
121
|
+
if [[ -n "${AFYX_PLATFORM:-}" ]]; then printf '%s' "$AFYX_PLATFORM"; return; fi
|
|
122
|
+
case "$(uname -s)" in MINGW*|MSYS*|CYGWIN*) printf windows ;; *) printf unix ;; esac
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
afyx_component_root() {
|
|
126
|
+
local root relative
|
|
127
|
+
case "$(afyx_component_field "$1" install_root)" in
|
|
128
|
+
skills_root) root="$skills_root" ;;
|
|
129
|
+
afyx_graph_root) root="$graph_root" ;;
|
|
130
|
+
afyx_context_root) root="$context_root" ;;
|
|
131
|
+
afyx_forge_root) root="$forge_root" ;;
|
|
132
|
+
codex_tools) root="$codex_home/tools" ;;
|
|
133
|
+
*) root="" ;;
|
|
134
|
+
esac
|
|
135
|
+
[[ -n "$root" ]] || return 0
|
|
136
|
+
relative="$(afyx_component_field "$1" install_path)"
|
|
137
|
+
if [[ -n "$relative" ]]; then printf '%s/%s' "$root" "$relative"; else printf '%s' "$root"; fi
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
# afyx_skill_manifest_check <dir> <require_version:true|false> -> 0 ok; AFYX_DETAIL says why not
|
|
141
|
+
afyx_skill_manifest_check() {
|
|
142
|
+
local manifest="$1/SKILL.md"
|
|
143
|
+
[[ -f "$manifest" ]] || { AFYX_DETAIL='SKILL.md missing'; return 1; }
|
|
144
|
+
[[ -s "$manifest" ]] || { AFYX_DETAIL='SKILL.md is empty'; return 1; }
|
|
145
|
+
[[ "$(head -n 1 "$manifest")" == '---' ]] || { AFYX_DETAIL='frontmatter delimiters missing or malformed'; return 1; }
|
|
146
|
+
grep -Eq '^name:[[:space:]]*[a-z0-9-]+[[:space:]]*$' "$manifest" || { AFYX_DETAIL='valid name missing'; return 1; }
|
|
147
|
+
grep -Eq '^description:[[:space:]]*[^[:space:]].*$' "$manifest" || { AFYX_DETAIL='description missing'; return 1; }
|
|
148
|
+
if [[ "$2" == true ]]; then
|
|
149
|
+
grep -Eq '^metadata:[[:space:]]*$' "$manifest" || { AFYX_DETAIL='metadata.version missing'; return 1; }
|
|
150
|
+
grep -Eq '^[[:space:]]+version:[[:space:]]*[^[:space:]].*$' "$manifest" || { AFYX_DETAIL='metadata.version missing'; return 1; }
|
|
151
|
+
fi
|
|
152
|
+
return 0
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
afyx_skill_version() {
|
|
156
|
+
sed -n 's/^[[:space:]]*version:[[:space:]]*"\{0,1\}\([^"[:space:]]*\).*/\1/p' "$1/SKILL.md" 2>/dev/null | head -n 1
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
afyx_component_evaluate() {
|
|
160
|
+
local id="$1" type platforms path list item present total platform marker_file field value
|
|
161
|
+
AFYX_STATE=UNKNOWN; AFYX_DETAIL=''; AFYX_VERSION=''; AFYX_PATH=''
|
|
162
|
+
type="$(afyx_component_field "$id" type)"
|
|
163
|
+
platforms="$(afyx_component_field "$id" platforms)"
|
|
164
|
+
platform="$(afyx_platform)"
|
|
165
|
+
|
|
166
|
+
if [[ "$type" == detected-cli ]]; then
|
|
167
|
+
if command -v "$(afyx_component_field "$id" detect_command)" >/dev/null 2>&1; then
|
|
168
|
+
AFYX_STATE=HEALTHY; AFYX_DETAIL='externally managed; detected'
|
|
169
|
+
else AFYX_STATE='NOT INSTALLED'; AFYX_DETAIL='externally managed; not detected'; fi
|
|
170
|
+
return 0
|
|
171
|
+
fi
|
|
172
|
+
if [[ "$platforms" == windows && "$platform" != windows ]]; then
|
|
173
|
+
AFYX_STATE='NOT INSTALLED'; AFYX_DETAIL='Windows-only runtime'; return 0
|
|
174
|
+
fi
|
|
175
|
+
|
|
176
|
+
path="$(afyx_component_root "$id")"; AFYX_PATH="$path"
|
|
177
|
+
[[ -n "$path" ]] || { AFYX_DETAIL='component root is not defined'; return 0; }
|
|
178
|
+
|
|
179
|
+
list="$(afyx_component_field "$id" required_files)"
|
|
180
|
+
local extra="$(afyx_component_field "$id" "required_files_$platform")"
|
|
181
|
+
[[ -n "$extra" ]] && list="${list:+$list;}$extra"
|
|
182
|
+
|
|
183
|
+
case "$type" in
|
|
184
|
+
skill)
|
|
185
|
+
if [[ ! -e "$path" ]]; then AFYX_STATE='NOT INSTALLED'; AFYX_DETAIL='not installed'; return 0; fi
|
|
186
|
+
if ! afyx_skill_manifest_check "$path" "$(afyx_component_field "$id" version_required)"; then AFYX_STATE=INVALID; return 0; fi
|
|
187
|
+
local IFS=';'
|
|
188
|
+
for item in $list; do
|
|
189
|
+
[[ -n "$item" ]] || continue
|
|
190
|
+
if [[ ! -f "$path/$item" ]]; then AFYX_STATE=INCOMPLETE; AFYX_DETAIL="reference missing: $item"; return 0; fi
|
|
191
|
+
if [[ "$item" != SKILL.md && ! -s "$path/$item" ]]; then AFYX_STATE=INCOMPLETE; AFYX_DETAIL="reference empty: $item"; return 0; fi
|
|
192
|
+
done
|
|
193
|
+
unset IFS
|
|
194
|
+
AFYX_STATE=HEALTHY; AFYX_DETAIL='frontmatter and required references valid'; AFYX_VERSION="$(afyx_skill_version "$path")"
|
|
195
|
+
;;
|
|
196
|
+
runtime)
|
|
197
|
+
if [[ ! -e "$path" ]]; then AFYX_STATE='NOT INSTALLED'; AFYX_DETAIL='not installed'; return 0; fi
|
|
198
|
+
if [[ -L "$path" ]]; then AFYX_STATE=UNKNOWN; AFYX_DETAIL='runtime root is a symbolic link'; return 0; fi
|
|
199
|
+
if [[ ! -r "$path" ]]; then AFYX_STATE=UNKNOWN; AFYX_DETAIL='access denied while inspecting component'; return 0; fi
|
|
200
|
+
local IFS=';' unix_list
|
|
201
|
+
unix_list=";$(afyx_component_field "$id" required_files_unix);"
|
|
202
|
+
for item in $list; do
|
|
203
|
+
[[ -n "$item" ]] || continue
|
|
204
|
+
if [[ ! -f "$path/$item" ]]; then AFYX_STATE=INCOMPLETE; AFYX_DETAIL="missing: $item"; return 0; fi
|
|
205
|
+
case "$unix_list" in *";$item;"*) if [[ "$platform" == unix && ! -x "$path/$item" ]]; then AFYX_STATE=INCOMPLETE; AFYX_DETAIL="not executable: $item"; return 0; fi ;; esac
|
|
206
|
+
done
|
|
207
|
+
unset IFS
|
|
208
|
+
marker_file="$path/$(afyx_component_field "$id" marker_file)"
|
|
209
|
+
field="$(afyx_component_field "$id" marker_field)"; value="$(afyx_component_field "$id" marker_value)"
|
|
210
|
+
if ! grep -q "\"$field\"[[:space:]]*:[[:space:]]*\"$value\"" "$marker_file"; then AFYX_STATE=INVALID; AFYX_DETAIL='ownership marker mismatch'; return 0; fi
|
|
211
|
+
field="$(afyx_component_field "$id" version_source)"; field="${field#*#}"
|
|
212
|
+
AFYX_VERSION="$(sed -n "s/.*\"$field\"[[:space:]]*:[[:space:]]*\"\\([^\"]*\\)\".*/\\1/p" "$marker_file" | head -n 1)"
|
|
213
|
+
if [[ "$(afyx_component_field "$id" version_required)" == true && -z "$AFYX_VERSION" ]]; then AFYX_STATE=INVALID; AFYX_DETAIL="$field missing from metadata"; return 0; fi
|
|
214
|
+
local integrity_manifest expected_digest actual_digest line expected_hash relative actual_hash
|
|
215
|
+
integrity_manifest="$(afyx_component_field "$id" integrity_manifest_file)"
|
|
216
|
+
if [[ -n "$integrity_manifest" ]]; then
|
|
217
|
+
expected_digest="$(sed -n 's/.*"artifact_digest"[[:space:]]*:[[:space:]]*"\([0-9a-f]*\)".*/\1/p' "$marker_file")"
|
|
218
|
+
if command -v sha256sum >/dev/null 2>&1; then actual_digest="$(sha256sum "$path/$integrity_manifest" | awk '{print $1}')"
|
|
219
|
+
else actual_digest="$(shasum -a 256 "$path/$integrity_manifest" | awk '{print $1}')"; fi
|
|
220
|
+
if [[ ${#expected_digest} -ne 64 || "$actual_digest" != "$expected_digest" ]]; then AFYX_STATE=DRIFTED; AFYX_DETAIL='Afyx ownership verified but runtime integrity differs'; return 0; fi
|
|
221
|
+
while IFS= read -r line; do
|
|
222
|
+
expected_hash="${line%% *}"; relative="${line#* }"
|
|
223
|
+
if [[ ! "$expected_hash" =~ ^[0-9a-f]{64}$ || "$relative" != current/* || "$relative" == *../* || ! -f "$path/$relative" || -L "$path/$relative" ]]; then AFYX_STATE=DRIFTED; AFYX_DETAIL='Afyx ownership verified but runtime integrity differs'; return 0; fi
|
|
224
|
+
if command -v sha256sum >/dev/null 2>&1; then actual_hash="$(sha256sum "$path/$relative" | awk '{print $1}')"
|
|
225
|
+
else actual_hash="$(shasum -a 256 "$path/$relative" | awk '{print $1}')"; fi
|
|
226
|
+
if [[ "$actual_hash" != "$expected_hash" ]]; then AFYX_STATE=DRIFTED; AFYX_DETAIL='Afyx ownership verified but runtime integrity differs'; return 0; fi
|
|
227
|
+
done < "$path/$integrity_manifest"
|
|
228
|
+
local identity_file identity_field metadata_value identity_value
|
|
229
|
+
identity_file="$(afyx_component_field "$id" identity_file)"
|
|
230
|
+
if [[ -n "$identity_file" ]]; then
|
|
231
|
+
[[ -f "$path/$identity_file" ]] || { AFYX_STATE=DRIFTED; AFYX_DETAIL='Afyx ownership verified but runtime identity is missing'; return 0; }
|
|
232
|
+
for identity_field in product_name product_version source_revision ownership_class build_provenance release_channel integration_default; do
|
|
233
|
+
metadata_value="$(sed -n 's/.*"'"$identity_field"'"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$marker_file" | head -n 1)"
|
|
234
|
+
identity_value="$(sed -n 's/.*"'"$identity_field"'"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$path/$identity_file" | head -n 1)"
|
|
235
|
+
if [[ "$metadata_value" != "$identity_value" ]]; then AFYX_STATE=DRIFTED; AFYX_DETAIL='Afyx ownership verified but runtime identity differs'; return 0; fi
|
|
236
|
+
done
|
|
237
|
+
fi
|
|
238
|
+
fi
|
|
239
|
+
AFYX_STATE=HEALTHY; AFYX_DETAIL='Afyx-owned runtime valid'
|
|
240
|
+
;;
|
|
241
|
+
toolset)
|
|
242
|
+
present=0; total=0
|
|
243
|
+
local IFS=';'
|
|
244
|
+
for item in $list; do
|
|
245
|
+
[[ -n "$item" ]] || continue
|
|
246
|
+
total=$((total + 1)); [[ -f "$path/$item" ]] && present=$((present + 1))
|
|
247
|
+
done
|
|
248
|
+
unset IFS
|
|
249
|
+
if ((present == 0)); then AFYX_STATE='NOT INSTALLED'; AFYX_DETAIL='not installed'
|
|
250
|
+
elif ((present == total)); then AFYX_STATE=HEALTHY; AFYX_DETAIL='runtime files installed'
|
|
251
|
+
else AFYX_STATE=INCOMPLETE; AFYX_DETAIL="$present of $total runtime files present"; fi
|
|
252
|
+
;;
|
|
253
|
+
*) AFYX_DETAIL="unsupported component type: $type" ;;
|
|
254
|
+
esac
|
|
255
|
+
return 0
|
|
256
|
+
}
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
# shellcheck shell=bash
|
|
2
|
+
# Transactional skill lifecycle helpers for Bash 3.2+.
|
|
3
|
+
|
|
4
|
+
AFYX_SKILL_MARKER='.afyx-install.json'
|
|
5
|
+
afyx_backup_root="${AFYX_BACKUP_ROOT:-$HOME/.afyx/backups}"
|
|
6
|
+
afyx_backup_limit="${AFYX_BACKUP_LIMIT:-20}"
|
|
7
|
+
|
|
8
|
+
afyx_sha256() {
|
|
9
|
+
if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | awk '{print $1}'
|
|
10
|
+
elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" | awk '{print $1}'
|
|
11
|
+
else printf 'A SHA-256 utility is required.\n' >&2; return 1
|
|
12
|
+
fi
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
afyx_skill_digest() {
|
|
16
|
+
local directory="$1" listing relative digest
|
|
17
|
+
listing="$(mktemp "${TMPDIR:-/tmp}/afyx-skill-digest.XXXXXX")" || return 1
|
|
18
|
+
if ! (
|
|
19
|
+
cd "$directory" || exit 1
|
|
20
|
+
find . -type f ! -path './.git/*' ! -name "$AFYX_SKILL_MARKER" -print |
|
|
21
|
+
LC_ALL=C sort |
|
|
22
|
+
while IFS= read -r relative; do
|
|
23
|
+
digest="$(afyx_sha256 "$relative")" || exit 1
|
|
24
|
+
printf '%s %s\n' "$digest" "$relative"
|
|
25
|
+
done
|
|
26
|
+
) >"$listing"; then
|
|
27
|
+
rm -f -- "$listing"
|
|
28
|
+
return 1
|
|
29
|
+
fi
|
|
30
|
+
afyx_sha256 "$listing"
|
|
31
|
+
local status=$?
|
|
32
|
+
rm -f -- "$listing"
|
|
33
|
+
return "$status"
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
afyx_assert_skill_target() {
|
|
37
|
+
local root="$1" target="$2" root_real parent_real
|
|
38
|
+
[[ -n "$root" && -n "$target" && "$target" != "$root" ]] || { printf 'Unsafe empty or root skill target.\n' >&2; return 1; }
|
|
39
|
+
[[ ! -L "$root" && ! -L "$target" ]] || { printf 'Refusing symlinked skill path: %s\n' "$target" >&2; return 1; }
|
|
40
|
+
mkdir -p "$root" || return 1
|
|
41
|
+
root_real="$(cd "$root" && pwd -P)" || return 1
|
|
42
|
+
parent_real="$(cd "$(dirname "$target")" && pwd -P)" || return 1
|
|
43
|
+
[[ "$parent_real" == "$root_real" ]] || { printf 'Skill target escapes configured root: %s\n' "$target" >&2; return 1; }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
afyx_recover_skill_transaction() {
|
|
47
|
+
local target="$1" old
|
|
48
|
+
old="$(dirname "$target")/.$(basename "$target").afyx-old"
|
|
49
|
+
if [[ -e "$old" && ! -e "$target" ]]; then
|
|
50
|
+
mv "$old" "$target" || return 1
|
|
51
|
+
printf 'Recovered interrupted skill transaction: %s\n' "$target" >&2
|
|
52
|
+
elif [[ -e "$old" && -e "$target" ]]; then
|
|
53
|
+
printf 'Ambiguous interrupted skill transaction; preserve both %s and %s for manual recovery.\n' "$target" "$old" >&2
|
|
54
|
+
return 1
|
|
55
|
+
fi
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
afyx_marker_value() {
|
|
59
|
+
local marker="$1" field="$2"
|
|
60
|
+
sed -n "s/.*\"$field\"[[:space:]]*:[[:space:]]*\"\([^\"]*\)\".*/\1/p" "$marker" | head -n 1
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
# Sets AFYX_OWNERSHIP_STATE to NOT_INSTALLED, AFYX_OWNED_UNMODIFIED,
|
|
64
|
+
# AFYX_OWNED_MODIFIED, EXTERNAL_UPSTREAM, FOREIGN_UNPROVEN, or UNSAFE.
|
|
65
|
+
afyx_classify_skill() {
|
|
66
|
+
local target="$1" component="$2" expected_owner="$3" marker owner marked_component recorded actual repository revision head remote dirty
|
|
67
|
+
AFYX_OWNERSHIP_STATE=FOREIGN_UNPROVEN
|
|
68
|
+
[[ -e "$target" || -L "$target" ]] || { AFYX_OWNERSHIP_STATE=NOT_INSTALLED; return 0; }
|
|
69
|
+
[[ ! -L "$target" && -d "$target" ]] || { AFYX_OWNERSHIP_STATE=UNSAFE; return 0; }
|
|
70
|
+
marker="$target/$AFYX_SKILL_MARKER"
|
|
71
|
+
[[ -f "$marker" && ! -L "$marker" ]] || return 0
|
|
72
|
+
owner="$(afyx_marker_value "$marker" owner)"
|
|
73
|
+
marked_component="$(afyx_marker_value "$marker" component)"
|
|
74
|
+
[[ "$owner" == "$expected_owner" && "$marked_component" == "$component" ]] || return 0
|
|
75
|
+
if [[ "$owner" == afyx ]]; then
|
|
76
|
+
recorded="$(afyx_marker_value "$marker" content_sha256)"
|
|
77
|
+
[[ ${#recorded} -eq 64 && "$recorded" != *[!0-9a-fA-F]* ]] || return 0
|
|
78
|
+
actual="$(afyx_skill_digest "$target")" || { AFYX_OWNERSHIP_STATE=UNSAFE; return 0; }
|
|
79
|
+
if [[ "$actual" == "$recorded" ]]; then AFYX_OWNERSHIP_STATE=AFYX_OWNED_UNMODIFIED
|
|
80
|
+
else AFYX_OWNERSHIP_STATE=AFYX_OWNED_MODIFIED; fi
|
|
81
|
+
return 0
|
|
82
|
+
fi
|
|
83
|
+
repository="$(afyx_marker_value "$marker" source_repository)"
|
|
84
|
+
revision="$(afyx_marker_value "$marker" source_revision)"
|
|
85
|
+
[[ -d "$target/.git" && ${#revision} -eq 40 ]] || return 0
|
|
86
|
+
head="$(git -C "$target" rev-parse HEAD 2>/dev/null)" || return 0
|
|
87
|
+
remote="$(git -C "$target" remote get-url origin 2>/dev/null)" || return 0
|
|
88
|
+
dirty="$(git -C "$target" status --porcelain 2>/dev/null)" || return 0
|
|
89
|
+
if [[ "$head" == "$revision" && "$remote" == "$repository" && -z "$dirty" ]]; then
|
|
90
|
+
AFYX_OWNERSHIP_STATE=EXTERNAL_UPSTREAM
|
|
91
|
+
else
|
|
92
|
+
AFYX_OWNERSHIP_STATE=FOREIGN_UNPROVEN
|
|
93
|
+
fi
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
# Sets AFYX_LEGACY_PROVENANCE to MATCH, MODIFIED, FOREIGN, or UNKNOWN.
|
|
97
|
+
afyx_legacy_skill_provenance() {
|
|
98
|
+
local target="$1" component="$2" owner="$3" legacy_digest="${4:-}" legacy_version="${5:-}" repository="${6:-}" revision="${7:-}"
|
|
99
|
+
local actual manifest name version remote dirty head
|
|
100
|
+
AFYX_LEGACY_PROVENANCE=UNKNOWN
|
|
101
|
+
[[ -d "$target" && ! -L "$target" && ! -e "$target/$AFYX_SKILL_MARKER" ]] || return 0
|
|
102
|
+
if [[ "$owner" == afyx ]]; then
|
|
103
|
+
[[ ${#legacy_digest} -eq 64 && "$legacy_digest" != *[!0-9a-fA-F]* ]] || return 0
|
|
104
|
+
actual="$(afyx_skill_digest "$target")" || return 0
|
|
105
|
+
if [[ "$actual" == "$legacy_digest" ]]; then AFYX_LEGACY_PROVENANCE=MATCH; return 0; fi
|
|
106
|
+
manifest="$target/SKILL.md"
|
|
107
|
+
[[ -f "$manifest" && ! -L "$manifest" ]] || return 0
|
|
108
|
+
name="$(sed -n 's/^name:[[:space:]]*\([^[:space:]]*\).*/\1/p' "$manifest" | head -n 1)"
|
|
109
|
+
version="$(sed -n 's/^[[:space:]]*version:[[:space:]]*"\{0,1\}\([^"[:space:]]*\).*/\1/p' "$manifest" | head -n 1)"
|
|
110
|
+
if [[ -z "$name" ]]; then return 0
|
|
111
|
+
elif [[ "$name" != "$component" ]]; then AFYX_LEGACY_PROVENANCE=FOREIGN
|
|
112
|
+
elif [[ -n "$version" && "$version" == "$legacy_version" ]]; then AFYX_LEGACY_PROVENANCE=MODIFIED
|
|
113
|
+
fi
|
|
114
|
+
return 0
|
|
115
|
+
fi
|
|
116
|
+
[[ -d "$target/.git" ]] || return 0
|
|
117
|
+
remote="$(git -C "$target" remote get-url origin 2>/dev/null)" || return 0
|
|
118
|
+
[[ "$remote" == "$repository" ]] || { AFYX_LEGACY_PROVENANCE=FOREIGN; return 0; }
|
|
119
|
+
dirty="$(git -C "$target" status --porcelain 2>/dev/null)" || return 0
|
|
120
|
+
[[ -z "$dirty" ]] || { AFYX_LEGACY_PROVENANCE=MODIFIED; return 0; }
|
|
121
|
+
head="$(git -C "$target" rev-parse HEAD 2>/dev/null)" || return 0
|
|
122
|
+
[[ "$head" == "$revision" ]] && AFYX_LEGACY_PROVENANCE=MATCH
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
afyx_write_skill_marker() {
|
|
126
|
+
local target="$1" component="$2" owner="$3" version="$4" repository="${5:-}" revision="${6:-}" digest
|
|
127
|
+
digest=''
|
|
128
|
+
if [[ "$owner" == afyx ]]; then digest="$(afyx_skill_digest "$target")" || return 1; fi
|
|
129
|
+
cat >"$target/$AFYX_SKILL_MARKER" <<EOF
|
|
130
|
+
{
|
|
131
|
+
"schema": 1,
|
|
132
|
+
"product": "Afyx Codex Engineering Kit",
|
|
133
|
+
"component": "$component",
|
|
134
|
+
"owner": "$owner",
|
|
135
|
+
"version": "$version",
|
|
136
|
+
"content_sha256": "$digest",
|
|
137
|
+
"source_repository": "$repository",
|
|
138
|
+
"source_revision": "$revision"
|
|
139
|
+
}
|
|
140
|
+
EOF
|
|
141
|
+
if [[ "$owner" == upstream && -d "$target/.git" ]]; then
|
|
142
|
+
mkdir -p "$target/.git/info" || return 1
|
|
143
|
+
grep -qxF "/$AFYX_SKILL_MARKER" "$target/.git/info/exclude" 2>/dev/null || printf '/%s\n' "$AFYX_SKILL_MARKER" >>"$target/.git/info/exclude"
|
|
144
|
+
fi
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
afyx_backup_skill() {
|
|
148
|
+
local component="$1" target="$2" destination count limit to_prune pruned
|
|
149
|
+
mkdir -p "$afyx_backup_root/$component" || return 1
|
|
150
|
+
limit="$afyx_backup_limit"
|
|
151
|
+
if [[ ! "$limit" =~ ^[0-9]+$ ]] || [[ "$limit" -le 0 ]]; then
|
|
152
|
+
limit=20
|
|
153
|
+
fi
|
|
154
|
+
|
|
155
|
+
count="$(find "$afyx_backup_root/$component" -mindepth 1 -maxdepth 1 -type d 2>/dev/null | wc -l | tr -d ' ')"
|
|
156
|
+
if [[ "$count" -ge "$limit" ]]; then
|
|
157
|
+
to_prune=$(( count - limit + 1 ))
|
|
158
|
+
pruned=0
|
|
159
|
+
while IFS= read -r dir_path; do
|
|
160
|
+
[[ -z "$dir_path" ]] && continue
|
|
161
|
+
[[ "$pruned" -ge "$to_prune" ]] && break
|
|
162
|
+
local base_name
|
|
163
|
+
base_name="$(basename "$dir_path")"
|
|
164
|
+
if [[ ! -L "$dir_path" ]] && [[ "$base_name" =~ ^[0-9]{8}-[0-9]{6}- ]]; then
|
|
165
|
+
rm -rf -- "$dir_path" 2>/dev/null && pruned=$(( pruned + 1 ))
|
|
166
|
+
fi
|
|
167
|
+
done < <(find "$afyx_backup_root/$component" -mindepth 1 -maxdepth 1 -type d 2>/dev/null | sort)
|
|
168
|
+
fi
|
|
169
|
+
local suffix
|
|
170
|
+
suffix="${RANDOM:-$$}-$$-$(date +%N 2>/dev/null || echo 0)"
|
|
171
|
+
destination="$afyx_backup_root/$component/$(date +%Y%m%d-%H%M%S)-$suffix"
|
|
172
|
+
cp -a "$target" "$destination" || return 1
|
|
173
|
+
printf '%s' "$destination"
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
afyx_adopt_legacy_skill() {
|
|
177
|
+
local component="$1" target="$2" owner="$3" version="${4:-}" legacy_digest="${5:-}" legacy_version="${6:-}" repository="${7:-}" revision="${8:-}"
|
|
178
|
+
local state backup before after exclude exclude_entry exclude_had=false exclude_tmp
|
|
179
|
+
afyx_assert_skill_target "$skills_root" "$target" || return 1
|
|
180
|
+
afyx_recover_skill_transaction "$target" || return 1
|
|
181
|
+
afyx_classify_skill "$target" "$component" "$owner"; state="$AFYX_OWNERSHIP_STATE"
|
|
182
|
+
if [[ "$state" == AFYX_OWNED_UNMODIFIED || "$state" == EXTERNAL_UPSTREAM ]]; then printf 'ALREADY_ADOPTED'; return 0; fi
|
|
183
|
+
[[ ! -e "$target/$AFYX_SKILL_MARKER" ]] || {
|
|
184
|
+
printf 'Refusing to adopt %s because ownership metadata already exists but is not valid.\n' "$component" >&2; return 1;
|
|
185
|
+
}
|
|
186
|
+
afyx_legacy_skill_provenance "$target" "$component" "$owner" "$legacy_digest" "$legacy_version" "$repository" "$revision"
|
|
187
|
+
[[ "$AFYX_LEGACY_PROVENANCE" == MATCH ]] || {
|
|
188
|
+
printf 'Refusing to adopt %s because legacy provenance is %s.\n' "$component" "$AFYX_LEGACY_PROVENANCE" >&2; return 1;
|
|
189
|
+
}
|
|
190
|
+
backup="$(afyx_backup_skill "$component" "$target")" || return 1
|
|
191
|
+
before="$(afyx_skill_digest "$target")" || return 1
|
|
192
|
+
after="$(afyx_skill_digest "$backup")" || return 1
|
|
193
|
+
[[ "$before" == "$after" ]] || { printf '%s adoption backup verification failed.\n' "$component" >&2; return 1; }
|
|
194
|
+
if [[ "$owner" == upstream ]]; then
|
|
195
|
+
afyx_legacy_skill_provenance "$backup" "$component" "$owner" "$legacy_digest" "$legacy_version" "$repository" "$revision"
|
|
196
|
+
[[ "$AFYX_LEGACY_PROVENANCE" == MATCH ]] || {
|
|
197
|
+
printf '%s adoption backup provenance verification failed.\n' "$component" >&2; return 1;
|
|
198
|
+
}
|
|
199
|
+
fi
|
|
200
|
+
exclude="$target/.git/info/exclude"; exclude_entry="/$AFYX_SKILL_MARKER"
|
|
201
|
+
[[ -f "$exclude" ]] && grep -qxF "$exclude_entry" "$exclude" && exclude_had=true
|
|
202
|
+
if ! afyx_write_skill_marker "$target" "$component" "$owner" "$version" "$repository" "$revision"; then
|
|
203
|
+
rm -f -- "$target/$AFYX_SKILL_MARKER"; return 1
|
|
204
|
+
fi
|
|
205
|
+
afyx_classify_skill "$target" "$component" "$owner"; state="$AFYX_OWNERSHIP_STATE"
|
|
206
|
+
if [[ "$state" != AFYX_OWNED_UNMODIFIED && "$state" != EXTERNAL_UPSTREAM ]]; then
|
|
207
|
+
rm -f -- "$target/$AFYX_SKILL_MARKER"
|
|
208
|
+
if [[ "$owner" == upstream && "$exclude_had" == false && -f "$exclude" ]]; then
|
|
209
|
+
exclude_tmp="$exclude.afyx-tmp-$$"
|
|
210
|
+
grep -vxF "$exclude_entry" "$exclude" >"$exclude_tmp" || true
|
|
211
|
+
mv "$exclude_tmp" "$exclude"
|
|
212
|
+
fi
|
|
213
|
+
printf '%s post-adoption ownership verification failed.\n' "$component" >&2
|
|
214
|
+
return 1
|
|
215
|
+
fi
|
|
216
|
+
printf 'ADOPTED backup=%s' "$backup"
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
afyx_promote_skill() {
|
|
220
|
+
local component="$1" label="$2" stage="$3" target="$4" owner="$5" version="$6" repository="${7:-}" revision="${8:-}" authorize="${9:-false}"
|
|
221
|
+
local parent old='' backup='' expected state
|
|
222
|
+
expected="$owner"
|
|
223
|
+
afyx_assert_skill_target "$skills_root" "$target" || return 1
|
|
224
|
+
afyx_recover_skill_transaction "$target" || return 1
|
|
225
|
+
[[ -d "$stage" && ! -L "$stage" ]] || { printf '%s staging directory is unsafe.\n' "$label" >&2; return 1; }
|
|
226
|
+
afyx_skill_manifest_check "$stage" "$(afyx_component_field "$component" version_required)" || { printf '%s staged manifest is invalid: %s.\n' "$label" "$AFYX_DETAIL" >&2; return 1; }
|
|
227
|
+
afyx_write_skill_marker "$stage" "$component" "$owner" "$version" "$repository" "$revision" || return 1
|
|
228
|
+
afyx_classify_skill "$target" "$component" "$expected"; state="$AFYX_OWNERSHIP_STATE"
|
|
229
|
+
if [[ "$state" != NOT_INSTALLED ]]; then
|
|
230
|
+
case "$state" in
|
|
231
|
+
AFYX_OWNED_UNMODIFIED|EXTERNAL_UPSTREAM) ;;
|
|
232
|
+
AFYX_OWNED_MODIFIED)
|
|
233
|
+
[[ "$authorize" == true ]] || { printf 'Refusing to replace %s (%s) without explicit repair authorization.\n' "$target" "$state" >&2; return 1; } ;;
|
|
234
|
+
FOREIGN_UNPROVEN)
|
|
235
|
+
printf 'Refusing to take ownership of %s (%s).\n' "$target" "$state" >&2; return 1 ;;
|
|
236
|
+
*) printf 'Refusing unsafe skill target %s (%s).\n' "$target" "$state" >&2; return 1 ;;
|
|
237
|
+
esac
|
|
238
|
+
backup="$(afyx_backup_skill "$component" "$target")" || return 1
|
|
239
|
+
parent="$(dirname "$target")"; old="$parent/.$(basename "$target").afyx-old"
|
|
240
|
+
[[ ! -e "$old" ]] || { printf 'Transaction recovery path already exists: %s\n' "$old" >&2; return 1; }
|
|
241
|
+
mv "$target" "$old" || return 1
|
|
242
|
+
fi
|
|
243
|
+
if ! mv "$stage" "$target"; then
|
|
244
|
+
[[ -n "$old" && -e "$old" && ! -e "$target" ]] && mv "$old" "$target"
|
|
245
|
+
return 1
|
|
246
|
+
fi
|
|
247
|
+
afyx_classify_skill "$target" "$component" "$expected"
|
|
248
|
+
if [[ "$AFYX_OWNERSHIP_STATE" != AFYX_OWNED_UNMODIFIED && "$AFYX_OWNERSHIP_STATE" != EXTERNAL_UPSTREAM ]]; then
|
|
249
|
+
rm -rf -- "$target"
|
|
250
|
+
[[ -n "$old" && -e "$old" ]] && mv "$old" "$target"
|
|
251
|
+
printf '%s post-install ownership verification failed.\n' "$label" >&2
|
|
252
|
+
return 1
|
|
253
|
+
fi
|
|
254
|
+
if [[ -n "$old" && -e "$old" ]]; then rm -rf -- "$old"; fi
|
|
255
|
+
if [[ -n "$backup" ]]; then printf 'Backup: %s\n' "$backup" >&2; fi
|
|
256
|
+
return 0
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
afyx_remove_skill() {
|
|
260
|
+
local component="$1" target="$2" expected_owner="$3" state backup
|
|
261
|
+
afyx_assert_skill_target "$skills_root" "$target" || return 1
|
|
262
|
+
afyx_recover_skill_transaction "$target" || return 1
|
|
263
|
+
afyx_classify_skill "$target" "$component" "$expected_owner"; state="$AFYX_OWNERSHIP_STATE"
|
|
264
|
+
[[ "$state" == NOT_INSTALLED ]] && return 0
|
|
265
|
+
[[ "$state" == AFYX_OWNED_UNMODIFIED || "$state" == EXTERNAL_UPSTREAM ]] || {
|
|
266
|
+
printf 'Refusing to remove %s because installed ownership is %s.\n' "$target" "$state" >&2; return 1;
|
|
267
|
+
}
|
|
268
|
+
backup="$(afyx_backup_skill "$component" "$target")" || return 1
|
|
269
|
+
rm -rf -- "$target" || return 1
|
|
270
|
+
printf 'Removed: %s (backup: %s)\n' "$target" "$backup"
|
|
271
|
+
}
|