@antprofuse/saddle-skill 0.2.0 → 0.3.0-alpha.2
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/SKILL.md +45 -61
- package/agents/openai.yaml +3 -3
- package/package.json +5 -15
- package/references/consumer-start.md +64 -0
- package/references/external-function-contracts.md +84 -0
- package/references/programming-model.md +86 -0
- package/references/validation.md +38 -0
- package/scripts/saddle-03-gate.js +96 -0
- package/assets/registry-consumer/Cargo.lock +0 -1814
- package/assets/registry-consumer/Cargo.toml +0 -20
- package/assets/registry-consumer/src/generated_artifact.rs +0 -858
- package/assets/registry-consumer/src/lib.rs +0 -12
- package/assets/registry-consumer/tests/provider_consumer.rs +0 -20
- package/package-files.txt +0 -25
- package/package-manifest.json +0 -112
- package/references/api-boundary.md +0 -71
- package/references/declaration-schema.md +0 -115
- package/references/identity-closure.json +0 -49
- package/references/machine-schema.md +0 -47
- package/references/migration.md +0 -24
- package/references/public-api.snapshot +0 -16
- package/scripts/parameterized-capacity-facts.py +0 -254
- package/scripts/sync-identity-closure.py +0 -124
- package/scripts/validate-contract.sh +0 -62
- package/scripts/validate-generated-preflight.sh +0 -120
- package/scripts/validate-generated-project.sh +0 -123
- package/scripts/validate-identity-closure.py +0 -138
- package/scripts/validate-machine-input.sh +0 -48
- package/scripts/validate-managed-objects-peak.py +0 -147
- package/scripts/validation-cache-key.py +0 -88
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
"""Fail closed unless every packaged Skill identity comes from one closure."""
|
|
3
|
-
|
|
4
|
-
from __future__ import annotations
|
|
5
|
-
|
|
6
|
-
import hashlib
|
|
7
|
-
import json
|
|
8
|
-
import re
|
|
9
|
-
import sys
|
|
10
|
-
from pathlib import Path
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
def digest(path: Path) -> str:
|
|
14
|
-
return hashlib.sha256(path.read_bytes()).hexdigest()
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
def exactly(pattern: str, text: str, label: str) -> str:
|
|
18
|
-
values = re.findall(pattern, text, flags=re.MULTILINE)
|
|
19
|
-
if len(values) != 1:
|
|
20
|
-
raise SystemExit(f"identity closure invalid: {label} count={len(values)}")
|
|
21
|
-
return values[0]
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
def main() -> None:
|
|
25
|
-
if len(sys.argv) != 2:
|
|
26
|
-
raise SystemExit("usage: validate-identity-closure.py <skill-directory>")
|
|
27
|
-
skill = Path(sys.argv[1]).resolve()
|
|
28
|
-
manifest = json.loads((skill / "package-manifest.json").read_text(encoding="utf-8"))
|
|
29
|
-
closure_path = skill / manifest["identity_closure"]["path"]
|
|
30
|
-
closure = json.loads(closure_path.read_text(encoding="utf-8"))
|
|
31
|
-
if closure.get("schema") != "saddle-skill-identity-closure/2":
|
|
32
|
-
raise SystemExit("identity closure invalid: schema")
|
|
33
|
-
if digest(closure_path) != manifest["identity_closure"]["sha256"]:
|
|
34
|
-
raise SystemExit("identity closure invalid: closure digest")
|
|
35
|
-
|
|
36
|
-
source = closure["artifact_source_commit"]
|
|
37
|
-
snapshot = skill / "references/public-api.snapshot"
|
|
38
|
-
boundary = skill / "references/api-boundary.md"
|
|
39
|
-
machine_doc = skill / "references/machine-schema.md"
|
|
40
|
-
validator = skill / "scripts/validate-machine-input.sh"
|
|
41
|
-
observed = {
|
|
42
|
-
"manifest": manifest["rust"]["source_commit"],
|
|
43
|
-
"snapshot": exactly(r"^source_commit=([0-9a-f]{40})$", snapshot.read_text(), "snapshot source"),
|
|
44
|
-
"boundary": exactly(r"Facade source `([0-9a-f]{40})`", boundary.read_text(), "boundary source"),
|
|
45
|
-
"validator": exactly(r"rust_facade_source=([0-9a-f]{40})", validator.read_text(), "validator source"),
|
|
46
|
-
}
|
|
47
|
-
if set(observed.values()) != {source}:
|
|
48
|
-
raise SystemExit(f"identity closure invalid: source drift {observed}")
|
|
49
|
-
registry_source = closure.get("registry_artifact_source_commit")
|
|
50
|
-
if not re.fullmatch(r"[0-9a-f]{40}", registry_source or "") or registry_source != source:
|
|
51
|
-
raise SystemExit("identity closure invalid: registry/source identity mismatch")
|
|
52
|
-
if manifest.get("release_approval") is not False or manifest.get("publish") is not False:
|
|
53
|
-
raise SystemExit("identity closure invalid: incompatible registry candidate must remain unpublished")
|
|
54
|
-
documented_schema = exactly(
|
|
55
|
-
r"machine:`(saddle-approved-build/[0-9]+)`", machine_doc.read_text(), "machine schema"
|
|
56
|
-
)
|
|
57
|
-
if {documented_schema, manifest["skill"]["machine_schema"], closure["machine_schema"]} != {"saddle-approved-build/2"}:
|
|
58
|
-
raise SystemExit("identity closure invalid: machine schema drift")
|
|
59
|
-
if digest(snapshot) != closure["public_api_snapshot_sha256"]:
|
|
60
|
-
raise SystemExit("identity closure invalid: snapshot digest")
|
|
61
|
-
if digest(machine_doc) != closure["machine_schema_document_sha256"]:
|
|
62
|
-
raise SystemExit("identity closure invalid: machine schema digest")
|
|
63
|
-
if manifest["rust"]["public_api_snapshot_sha256"] != closure["public_api_snapshot_sha256"]:
|
|
64
|
-
raise SystemExit("identity closure invalid: manifest snapshot digest")
|
|
65
|
-
expected_locks = {"registry_consumer": digest(skill / "assets/registry-consumer/Cargo.lock")}
|
|
66
|
-
if manifest["consumer_evidence"]["approved_locks"] != expected_locks or closure["approved_locks"] != expected_locks:
|
|
67
|
-
raise SystemExit("identity closure invalid: approved locks")
|
|
68
|
-
template_root = skill / "assets/registry-consumer"
|
|
69
|
-
template_inputs = {
|
|
70
|
-
"cargo_lock": template_root / "Cargo.lock",
|
|
71
|
-
"cargo_manifest": template_root / "Cargo.toml",
|
|
72
|
-
"library": template_root / "src/lib.rs",
|
|
73
|
-
"generated_owner": template_root / "src/generated_artifact.rs",
|
|
74
|
-
"external_consumer_test": template_root / "tests/provider_consumer.rs",
|
|
75
|
-
"output_validator": skill / "scripts/validate-generated-project.sh",
|
|
76
|
-
"preflight_validator": skill / "scripts/validate-generated-preflight.sh",
|
|
77
|
-
"cache_key_builder": skill / "scripts/validation-cache-key.py",
|
|
78
|
-
}
|
|
79
|
-
observed_template = {name: digest(path) for name, path in template_inputs.items()}
|
|
80
|
-
template_identity = hashlib.sha256(
|
|
81
|
-
json.dumps(observed_template, separators=(",", ":")).encode()
|
|
82
|
-
).hexdigest()
|
|
83
|
-
if closure.get("registry_consumer_template") != observed_template:
|
|
84
|
-
raise SystemExit("identity closure invalid: registry consumer template")
|
|
85
|
-
if manifest["consumer_evidence"].get("registry_consumer_template") != observed_template:
|
|
86
|
-
raise SystemExit("identity closure invalid: manifest registry consumer template")
|
|
87
|
-
if closure.get("template_identity_sha256") != template_identity or manifest["generated_static_facts"].get("template_identity_sha256") != template_identity:
|
|
88
|
-
raise SystemExit("identity closure invalid: template identity")
|
|
89
|
-
if manifest["generated_static_facts"].get("application_machine_fingerprint") != "NOT_GENERATED_SK_02":
|
|
90
|
-
raise SystemExit("identity closure invalid: SK-02 fabricated application fingerprint")
|
|
91
|
-
if manifest["consumer_evidence"].get("unified_application_sha256") != observed_template["library"] or closure.get("unified_application_sha256") != observed_template["library"]:
|
|
92
|
-
raise SystemExit("identity closure invalid: unified application")
|
|
93
|
-
if manifest["consumer_evidence"].get("unified_owner_sha256") != observed_template["generated_owner"] or closure.get("unified_owner_sha256") != observed_template["generated_owner"]:
|
|
94
|
-
raise SystemExit("identity closure invalid: unified owner")
|
|
95
|
-
if manifest.get("gate_identity") != closure.get("gate_identity"):
|
|
96
|
-
raise SystemExit("identity closure invalid: Gate identity")
|
|
97
|
-
gate = closure.get("gate_identity", {})
|
|
98
|
-
if not re.fullmatch(r"[0-9a-f]{40}", gate.get("d1_source_candidate", "")):
|
|
99
|
-
raise SystemExit("identity closure invalid: D1 source candidate")
|
|
100
|
-
for name, value in gate.items():
|
|
101
|
-
if name != "d1_source_candidate" and not re.fullmatch(r"[0-9a-f]{64}", value):
|
|
102
|
-
raise SystemExit(f"identity closure invalid: Gate digest {name}")
|
|
103
|
-
for field in (
|
|
104
|
-
"registry_artifact_source_commit",
|
|
105
|
-
"rust_candidate_manifest_sha256",
|
|
106
|
-
"registry_packages_sha256",
|
|
107
|
-
"framework_crate_sha256",
|
|
108
|
-
"fresh_registry_cargo_lock_sha256",
|
|
109
|
-
"registry_consumer_cargo_lock_sha256",
|
|
110
|
-
):
|
|
111
|
-
if manifest["identity_closure"][field] != closure[field]:
|
|
112
|
-
raise SystemExit(f"identity closure invalid: {field}")
|
|
113
|
-
expected_packages = {
|
|
114
|
-
"saddle-core", "saddle-macros", "saddle-admission", "saddle-observability", "saddle-runtime",
|
|
115
|
-
"saddle-db", "saddle-service", "saddle-framework",
|
|
116
|
-
}
|
|
117
|
-
if set(closure.get("registry_packages_sha256", {})) != expected_packages:
|
|
118
|
-
raise SystemExit("identity closure invalid: registry package set")
|
|
119
|
-
if any(not re.fullmatch(r"[0-9a-f]{64}", value) for value in closure["registry_packages_sha256"].values()):
|
|
120
|
-
raise SystemExit("identity closure invalid: registry package digest")
|
|
121
|
-
if manifest["npm"]["version"] != "0.2.0" or closure.get("skill_release") != "0.2.0":
|
|
122
|
-
raise SystemExit("identity closure invalid: Skill candidate version")
|
|
123
|
-
if manifest["rust"]["requirement"] != "=0.2.0" or closure.get("rust_release") != "0.2.0":
|
|
124
|
-
raise SystemExit("identity closure invalid: Rust RC version")
|
|
125
|
-
installation = manifest.get("installation", {})
|
|
126
|
-
if installation != {
|
|
127
|
-
"kind": "npm-tgz",
|
|
128
|
-
"command": "npm install --ignore-scripts --no-audit --no-fund <exact-tgz>",
|
|
129
|
-
"skill_root": "node_modules/@antprofuse/saddle-skill",
|
|
130
|
-
"entry": "SKILL.md",
|
|
131
|
-
"lifecycle_scripts": False,
|
|
132
|
-
}:
|
|
133
|
-
raise SystemExit("identity closure invalid: installation entry")
|
|
134
|
-
print("saddle-skill-identity-closure/2: valid")
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
if __name__ == "__main__":
|
|
138
|
-
main()
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bash
|
|
2
|
-
set -euo pipefail
|
|
3
|
-
|
|
4
|
-
if [[ $# -ne 1 || ! -f "$1" ]]; then
|
|
5
|
-
echo "usage: validate-machine-input.sh <machine-input>" >&2
|
|
6
|
-
exit 64
|
|
7
|
-
fi
|
|
8
|
-
input=$1
|
|
9
|
-
expected_keys=(
|
|
10
|
-
schema status release_approval declaration_schema skill_package rust_facade
|
|
11
|
-
rust_facade_source rust_compatibility rust_abi_frozen public_api public_api_snapshot
|
|
12
|
-
generated_static_schema generator_handoff build_internal runtime_profile
|
|
13
|
-
signed_production_bundle final_calibration baseline declaration_sha256
|
|
14
|
-
public_api_snapshot_sha256 skill_sha256 package_manifest_sha256 machine_schema_sha256
|
|
15
|
-
api_consumer_lock_sha256 unified_consumer_lock_sha256
|
|
16
|
-
query_db_compile_fail_lock_sha256 raw_signal_compile_fail_lock_sha256
|
|
17
|
-
generated_application_sha256 unified_owner_sha256 unified_machine_fingerprint
|
|
18
|
-
rustc_commit contract_validation unified_consumer_validation
|
|
19
|
-
public_api_snapshot_validation generated_static_validation migration_compile_fail
|
|
20
|
-
npm_pack_dry_run
|
|
21
|
-
)
|
|
22
|
-
actual_keys_file="$(mktemp)"
|
|
23
|
-
expected_keys_file="$(mktemp)"
|
|
24
|
-
trap 'rm -f "$actual_keys_file" "$expected_keys_file"' EXIT
|
|
25
|
-
awk -F= 'NF < 2 || $1 !~ /^[a-z][a-z0-9_]*$/ { exit 2 } { print $1 }' "$input" |
|
|
26
|
-
LC_ALL=C sort >"$actual_keys_file" || {
|
|
27
|
-
echo "invalid machine key/value syntax" >&2
|
|
28
|
-
exit 1
|
|
29
|
-
}
|
|
30
|
-
printf '%s\n' "${expected_keys[@]}" | LC_ALL=C sort >"$expected_keys_file"
|
|
31
|
-
if [[ "$(uniq -d "$actual_keys_file" | wc -l)" -ne 0 ]] ||
|
|
32
|
-
! diff -u "$expected_keys_file" "$actual_keys_file"; then
|
|
33
|
-
echo "unknown, duplicate, or missing machine field" >&2
|
|
34
|
-
exit 1
|
|
35
|
-
fi
|
|
36
|
-
grep -Fxq 'schema=saddle-approved-build/2' "$input"
|
|
37
|
-
grep -Fxq 'status=rc-blackbox-candidate-unpublished' "$input"
|
|
38
|
-
grep -Fxq 'release_approval=false' "$input"
|
|
39
|
-
grep -Fxq 'rust_facade_source=96cd4ec8da55b0770c5d3e8275c74e65241a4759' "$input"
|
|
40
|
-
grep -Fxq 'generated_static_schema=saddle-generated-static-facts/2' "$input"
|
|
41
|
-
grep -Fxq 'runtime_profile=opaque-verified-startup-plan-layout' "$input"
|
|
42
|
-
grep -Fxq 'signed_production_bundle=deployment-required' "$input"
|
|
43
|
-
grep -Fxq 'final_calibration=deployment-required' "$input"
|
|
44
|
-
if grep -Eq '(^|_)(capacity_profile|capacity_domain|task_storage|deadline_ms)=' "$input"; then
|
|
45
|
-
echo "generated machine input contains a deployment-owned field" >&2
|
|
46
|
-
exit 1
|
|
47
|
-
fi
|
|
48
|
-
echo "saddle-approved-build/2 machine input: rc-blackbox-candidate-unpublished"
|
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
"""Validate canonical Saddle generated managed-object peak machine facts."""
|
|
3
|
-
|
|
4
|
-
from __future__ import annotations
|
|
5
|
-
|
|
6
|
-
import re
|
|
7
|
-
import sys
|
|
8
|
-
from pathlib import Path
|
|
9
|
-
|
|
10
|
-
U64_MAX = (1 << 64) - 1
|
|
11
|
-
BASE_KEYS = {
|
|
12
|
-
"schema",
|
|
13
|
-
"target_pointer_width",
|
|
14
|
-
"runtime_profile_sha256",
|
|
15
|
-
"max_active_requests",
|
|
16
|
-
"max_waiters",
|
|
17
|
-
"waiter_managed_objects_peak",
|
|
18
|
-
"route_count",
|
|
19
|
-
"max_route_managed_objects_peak",
|
|
20
|
-
"managed_objects_peak",
|
|
21
|
-
"derivation",
|
|
22
|
-
}
|
|
23
|
-
ROUTE_FIELDS = {
|
|
24
|
-
"path",
|
|
25
|
-
"artifact_sha256",
|
|
26
|
-
"request_body_owners",
|
|
27
|
-
"response_reservation_owners",
|
|
28
|
-
"other_managed_owners",
|
|
29
|
-
"managed_objects_peak",
|
|
30
|
-
}
|
|
31
|
-
ROUTE_KEY = re.compile(r"route\.(\d+)\.([a-z][a-z0-9_]*)\Z")
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
def fail(message: str) -> None:
|
|
35
|
-
raise ValueError(message)
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
def checked(value: int, label: str) -> int:
|
|
39
|
-
if value < 0 or value > U64_MAX:
|
|
40
|
-
fail(f"{label} exceeds target usize")
|
|
41
|
-
return value
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
def number(values: dict[str, str], key: str) -> int:
|
|
45
|
-
text = values[key]
|
|
46
|
-
if not re.fullmatch(r"0|[1-9][0-9]*", text):
|
|
47
|
-
fail(f"{key} is not canonical unsigned decimal")
|
|
48
|
-
return checked(int(text), key)
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
def validate(path: Path) -> None:
|
|
52
|
-
values: dict[str, str] = {}
|
|
53
|
-
routes: dict[int, dict[str, str]] = {}
|
|
54
|
-
for line_number, raw in enumerate(path.read_text(encoding="utf-8").splitlines(), 1):
|
|
55
|
-
if not raw or "=" not in raw:
|
|
56
|
-
fail(f"line {line_number} is not canonical key=value")
|
|
57
|
-
key, value = raw.split("=", 1)
|
|
58
|
-
if key in values:
|
|
59
|
-
fail(f"duplicate field: {key}")
|
|
60
|
-
match = ROUTE_KEY.fullmatch(key)
|
|
61
|
-
if match:
|
|
62
|
-
index = int(match.group(1))
|
|
63
|
-
field = match.group(2)
|
|
64
|
-
if field not in ROUTE_FIELDS:
|
|
65
|
-
fail(f"unknown route field: {key}")
|
|
66
|
-
route = routes.setdefault(index, {})
|
|
67
|
-
if field in route:
|
|
68
|
-
fail(f"duplicate route field: {key}")
|
|
69
|
-
route[field] = value
|
|
70
|
-
elif key not in BASE_KEYS:
|
|
71
|
-
fail(f"unknown field: {key}")
|
|
72
|
-
values[key] = value
|
|
73
|
-
|
|
74
|
-
missing_base = BASE_KEYS - values.keys()
|
|
75
|
-
if missing_base:
|
|
76
|
-
fail(f"missing fields: {','.join(sorted(missing_base))}")
|
|
77
|
-
if values["schema"] != "saddle-managed-objects-peak/1":
|
|
78
|
-
fail("unsupported managed-object peak schema")
|
|
79
|
-
if values["target_pointer_width"] != "64":
|
|
80
|
-
fail("only the approved 64-bit target is supported")
|
|
81
|
-
if values["derivation"] != "checked(active*max_route+waiters*waiter)":
|
|
82
|
-
fail("unexpected derivation")
|
|
83
|
-
|
|
84
|
-
route_count = number(values, "route_count")
|
|
85
|
-
if route_count == 0 or set(routes) != set(range(route_count)):
|
|
86
|
-
fail("route set is missing, sparse, or contains an unexpected route")
|
|
87
|
-
|
|
88
|
-
route_peaks: list[int] = []
|
|
89
|
-
paths: set[str] = set()
|
|
90
|
-
for index in range(route_count):
|
|
91
|
-
route = routes[index]
|
|
92
|
-
missing = ROUTE_FIELDS - route.keys()
|
|
93
|
-
if missing:
|
|
94
|
-
fail(f"route.{index} missing fields: {','.join(sorted(missing))}")
|
|
95
|
-
if not route["path"].startswith("/") or route["path"] in paths:
|
|
96
|
-
fail(f"route.{index}.path is invalid or duplicated")
|
|
97
|
-
paths.add(route["path"])
|
|
98
|
-
if not re.fullmatch(r"[0-9a-f]{64}", route["artifact_sha256"]):
|
|
99
|
-
fail(f"route.{index}.artifact_sha256 is not SHA-256")
|
|
100
|
-
|
|
101
|
-
components = []
|
|
102
|
-
for field in (
|
|
103
|
-
"request_body_owners",
|
|
104
|
-
"response_reservation_owners",
|
|
105
|
-
"other_managed_owners",
|
|
106
|
-
):
|
|
107
|
-
components.append(
|
|
108
|
-
number(values, f"route.{index}.{field}")
|
|
109
|
-
)
|
|
110
|
-
if components != [1, 1, 0]:
|
|
111
|
-
fail(f"route.{index} is not the generated linear owner shape")
|
|
112
|
-
peak = number(values, f"route.{index}.managed_objects_peak")
|
|
113
|
-
if checked(sum(components), f"route.{index} component sum") != peak:
|
|
114
|
-
fail(f"route.{index} peak drifted from its owner components")
|
|
115
|
-
route_peaks.append(peak)
|
|
116
|
-
|
|
117
|
-
maximum = max(route_peaks)
|
|
118
|
-
if number(values, "max_route_managed_objects_peak") != maximum:
|
|
119
|
-
fail("max route peak drifted")
|
|
120
|
-
|
|
121
|
-
active = number(values, "max_active_requests")
|
|
122
|
-
waiters = number(values, "max_waiters")
|
|
123
|
-
waiter_peak = number(values, "waiter_managed_objects_peak")
|
|
124
|
-
if waiter_peak != 0:
|
|
125
|
-
fail("pre-admission waiters cannot own generated route objects")
|
|
126
|
-
active_total = checked(active * maximum, "active managed-object product")
|
|
127
|
-
waiter_total = checked(waiters * waiter_peak, "waiter managed-object product")
|
|
128
|
-
total = checked(active_total + waiter_total, "managed-object total")
|
|
129
|
-
if number(values, "managed_objects_peak") != total:
|
|
130
|
-
fail("managed_objects_peak drifted from checked derivation")
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
def main() -> int:
|
|
134
|
-
if len(sys.argv) != 2:
|
|
135
|
-
print("usage: validate-managed-objects-peak.py <machine-facts>", file=sys.stderr)
|
|
136
|
-
return 64
|
|
137
|
-
try:
|
|
138
|
-
validate(Path(sys.argv[1]))
|
|
139
|
-
except (OSError, ValueError) as error:
|
|
140
|
-
print(f"managed-object peak invalid: {error}", file=sys.stderr)
|
|
141
|
-
return 1
|
|
142
|
-
print("saddle-managed-objects-peak/1: valid")
|
|
143
|
-
return 0
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
if __name__ == "__main__":
|
|
147
|
-
raise SystemExit(main())
|
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
"""Build the deterministic P2 validation identity for an entry-local run."""
|
|
3
|
-
|
|
4
|
-
from __future__ import annotations
|
|
5
|
-
|
|
6
|
-
import argparse
|
|
7
|
-
import hashlib
|
|
8
|
-
import json
|
|
9
|
-
from pathlib import Path
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
def hash_parts(parts: list[tuple[str, bytes]]) -> str:
|
|
13
|
-
digest = hashlib.sha256()
|
|
14
|
-
for name, payload in parts:
|
|
15
|
-
encoded = name.encode("utf-8")
|
|
16
|
-
digest.update(len(encoded).to_bytes(8, "big"))
|
|
17
|
-
digest.update(encoded)
|
|
18
|
-
digest.update(len(payload).to_bytes(8, "big"))
|
|
19
|
-
digest.update(payload)
|
|
20
|
-
return digest.hexdigest()
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
def source_digest(project: Path) -> str:
|
|
24
|
-
paths = [project / "Cargo.toml"]
|
|
25
|
-
for directory in (project / "src", project / "tests"):
|
|
26
|
-
if directory.is_dir():
|
|
27
|
-
paths.extend(path for path in directory.rglob("*.rs") if path.is_file())
|
|
28
|
-
unique = sorted(set(paths), key=lambda item: item.relative_to(project).as_posix())
|
|
29
|
-
return hash_parts(
|
|
30
|
-
[(path.relative_to(project).as_posix(), path.read_bytes()) for path in unique]
|
|
31
|
-
)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
def parse_contract(value: str) -> tuple[str, Path]:
|
|
35
|
-
if "=" not in value:
|
|
36
|
-
raise argparse.ArgumentTypeError("build contract must be label=path")
|
|
37
|
-
label, raw_path = value.split("=", 1)
|
|
38
|
-
if not label or not raw_path:
|
|
39
|
-
raise argparse.ArgumentTypeError("build contract must be label=path")
|
|
40
|
-
path = Path(raw_path).resolve()
|
|
41
|
-
if not path.is_file():
|
|
42
|
-
raise argparse.ArgumentTypeError(f"build contract is not a file: {path}")
|
|
43
|
-
return label, path
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
def main() -> None:
|
|
47
|
-
parser = argparse.ArgumentParser()
|
|
48
|
-
subparsers = parser.add_subparsers(dest="command", required=True)
|
|
49
|
-
key_parser = subparsers.add_parser("cache-key")
|
|
50
|
-
key_parser.add_argument("--project", type=Path, required=True)
|
|
51
|
-
key_parser.add_argument("--rustc-version", required=True)
|
|
52
|
-
key_parser.add_argument("--toolchain-identity", required=True)
|
|
53
|
-
key_parser.add_argument("--target-triple", required=True)
|
|
54
|
-
key_parser.add_argument("--feature-set", required=True)
|
|
55
|
-
key_parser.add_argument("--build-contract", action="append", type=parse_contract, required=True)
|
|
56
|
-
args = parser.parse_args()
|
|
57
|
-
|
|
58
|
-
project = args.project.resolve()
|
|
59
|
-
lock = project / "Cargo.lock"
|
|
60
|
-
if not lock.is_file():
|
|
61
|
-
raise SystemExit(f"Cargo.lock is missing: {lock}")
|
|
62
|
-
contracts = sorted(args.build_contract, key=lambda item: item[0])
|
|
63
|
-
build_contract_digest = hash_parts([(label, path.read_bytes()) for label, path in contracts])
|
|
64
|
-
inputs = {
|
|
65
|
-
"RUSTC_VERSION": args.rustc_version,
|
|
66
|
-
"TOOLCHAIN_IDENTITY": args.toolchain_identity,
|
|
67
|
-
"TARGET_TRIPLE": args.target_triple,
|
|
68
|
-
"FEATURE_SET": args.feature_set,
|
|
69
|
-
"LOCK_DIGEST": hashlib.sha256(lock.read_bytes()).hexdigest(),
|
|
70
|
-
"SOURCE_DIGEST": source_digest(project),
|
|
71
|
-
"BUILD_CONTRACT_DIGEST": build_contract_digest,
|
|
72
|
-
}
|
|
73
|
-
canonical = [inputs[name] for name in (
|
|
74
|
-
"RUSTC_VERSION", "TOOLCHAIN_IDENTITY", "TARGET_TRIPLE", "FEATURE_SET",
|
|
75
|
-
"LOCK_DIGEST", "SOURCE_DIGEST", "BUILD_CONTRACT_DIGEST",
|
|
76
|
-
)]
|
|
77
|
-
output = {
|
|
78
|
-
"schema": "saddle-skill-validation-cache-key/1",
|
|
79
|
-
"inputs": inputs,
|
|
80
|
-
"cache_key": hashlib.sha256(
|
|
81
|
-
json.dumps(canonical, separators=(",", ":")).encode("utf-8")
|
|
82
|
-
).hexdigest(),
|
|
83
|
-
}
|
|
84
|
-
print(json.dumps(output, separators=(",", ":"), sort_keys=True))
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
if __name__ == "__main__":
|
|
88
|
-
main()
|