Conform source attestation to estate v1 contract

#5
README.md CHANGED
@@ -472,7 +472,7 @@ One sovereign substrate, many organs — every decision carries a signed, checka
472
  - `GET /health` - typed read-only health contract; reachability is not readiness.
473
  - `GET /api/killinchu/v1/status` - runtime, doctrine, and contract discovery.
474
  - `GET /api/killinchu/v1/melt/summary` - measured in-process MELT summary with explicit external-trace limits.
475
- - `GET /.well-known/szl-source.json` - honest source attestation that keeps the observed GitHub revision, Hugging Face overlay base, and running Space revision distinct. It currently reports `PENDING_GITHUB_SYNC` and does not claim repository parity.
476
  - `GET /openapi.json`, `/api/killinchu/openapi.json`, or `/api/killinchu/v1/openapi.json` - the same generated API description, with bad legacy annotations omitted rather than crashing discovery.
477
 
478
  The MELT contract exposes aggregate metric family and sample counts, not private
 
472
  - `GET /health` - typed read-only health contract; reachability is not readiness.
473
  - `GET /api/killinchu/v1/status` - runtime, doctrine, and contract discovery.
474
  - `GET /api/killinchu/v1/melt/summary` - measured in-process MELT summary with explicit external-trace limits.
475
+ - `GET /.well-known/szl-source.json` - estate `szl.deployment-source/v1` contract. It dynamically measures the Hugging Face repository head while explicitly reporting `PENDING_GITHUB_SYNC`; repository-head evidence is not presented as proof of the exact serving process revision or GitHub parity.
476
  - `GET /openapi.json`, `/api/killinchu/openapi.json`, or `/api/killinchu/v1/openapi.json` - the same generated API description, with bad legacy annotations omitted rather than crashing discovery.
477
 
478
  The MELT contract exposes aggregate metric family and sample counts, not private
killinchu_public_contracts.py CHANGED
@@ -6,16 +6,24 @@ or authority to actuate a defense system.
6
  """
7
  from __future__ import annotations
8
 
 
9
  import os
 
 
 
10
  from datetime import datetime, timezone
11
  from typing import Any
12
 
13
 
14
  GITHUB_REPOSITORY = "https://github.com/szl-holdings/killinchu"
 
15
  GITHUB_OBSERVED_REVISION = "dbb7ef1b20b941b299235d13dbd8404eb96cc0a4"
16
  HF_SPACE_REPOSITORY = "https://huggingface.co/spaces/SZLHOLDINGS/killinchu"
17
- HF_OVERLAY_BASE_REVISION = "2dd8aaae39bf0ec22959b26cc3c24328f980447c"
 
18
  REVISION_OBSERVED_AT = "2026-07-11T16:16:12.897595+00:00"
 
 
19
 
20
 
21
  def _now() -> str:
@@ -42,57 +50,114 @@ def _source_git_build_variable() -> dict[str, Any]:
42
  }
43
 
44
 
45
- def source_attestation() -> dict[str, Any]:
46
- """Describe source/deployment revision evidence without claiming parity."""
47
- return {
48
- "schema": "szl.source-attestation/v1",
49
- "observed_at": _now(),
50
- "service": "killinchu",
51
- "attestation_state": "PENDING_GITHUB_SYNC",
52
- "alignment_state": "NOT_VERIFIED",
53
- "github_source_observation": {
54
- "repository": GITHUB_REPOSITORY,
55
- "branch": "main",
56
- "revision": GITHUB_OBSERVED_REVISION,
57
- "observed_at": REVISION_OBSERVED_AT,
58
- "evidence": (
59
- "https://api.github.com/repos/szl-holdings/killinchu/commits/"
60
- + GITHUB_OBSERVED_REVISION
61
- ),
62
- "live_branch_head_resolver": (
63
- "https://api.github.com/repos/szl-holdings/killinchu/commits/main"
64
- ),
65
- "semantics": "GitHub main revision observed when this overlay was prepared",
66
  },
67
- "huggingface_overlay": {
68
- "repository": HF_SPACE_REPOSITORY,
69
- "base_revision": HF_OVERLAY_BASE_REVISION,
70
- "base_revision_semantics": (
71
- "Hugging Face Space revision on which this source-attestation overlay was based"
72
- ),
73
- "repository_head_state": "RESOLVE_EXTERNALLY",
74
- "repository_head_resolver": (
75
- "https://huggingface.co/api/spaces/SZLHOLDINGS/killinchu"
76
- ),
77
- "repository_head_json_field": "sha",
78
- "running_process_revision_state": "NOT_EXPOSED_IN_PROCESS",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  },
80
- "build_metadata": {
81
- "source_git_revision_variable": _source_git_build_variable(),
 
82
  },
83
- "relationship": {
84
- "state": "PENDING_GITHUB_SYNC",
85
- "basis": (
86
- "The Hugging Face Space includes overlay commits not yet represented by a "
87
- "verified GitHub commit. Repository equality is not claimed."
88
- ),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  },
90
  "limits": [
91
- "The observed GitHub revision is point-in-time evidence, not a live branch-head claim.",
92
- "The overlay base revision is not the currently running Hugging Face revision.",
93
- "The Hub repository head may lead or lag the process during a rolling deployment, so it is not labeled as the running process revision.",
94
- "SZL_GIT_SHA is source build metadata and must not be interpreted as a Hugging Face deployment revision.",
95
- "This document is unsigned and does not establish SLSA provenance or binary reproducibility.",
96
  ],
97
  }
98
 
@@ -230,8 +295,22 @@ def register(app: Any, ns: str = "killinchu") -> dict[str, Any]:
230
  return JSONResponse(melt_summary(app, ns), headers={"Cache-Control": "no-store"})
231
 
232
  async def _source_attestation(_request):
 
 
 
 
 
 
 
 
 
 
 
 
 
233
  return JSONResponse(
234
- source_attestation(),
 
235
  headers={"Cache-Control": "no-store", "X-Content-Type-Options": "nosniff"},
236
  )
237
 
 
6
  """
7
  from __future__ import annotations
8
 
9
+ import json
10
  import os
11
+ import threading
12
+ import time
13
+ import urllib.request
14
  from datetime import datetime, timezone
15
  from typing import Any
16
 
17
 
18
  GITHUB_REPOSITORY = "https://github.com/szl-holdings/killinchu"
19
+ GITHUB_REPOSITORY_ID = "szl-holdings/killinchu"
20
  GITHUB_OBSERVED_REVISION = "dbb7ef1b20b941b299235d13dbd8404eb96cc0a4"
21
  HF_SPACE_REPOSITORY = "https://huggingface.co/spaces/SZLHOLDINGS/killinchu"
22
+ HF_SPACE_ID = "SZLHOLDINGS/killinchu"
23
+ HF_OVERLAY_BASE_REVISION = "fce3a1a581d212119473756d4907a06ff5dc9f2f"
24
  REVISION_OBSERVED_AT = "2026-07-11T16:16:12.897595+00:00"
25
+ _HF_HEAD_CACHE: dict[str, Any] = {"at": 0.0, "value": None}
26
+ _HF_HEAD_LOCK = threading.Lock()
27
 
28
 
29
  def _now() -> str:
 
50
  }
51
 
52
 
53
+ def _hf_repository_head(force: bool = False) -> dict[str, str] | None:
54
+ """Resolve the Hub repository head; never label it as the serving process SHA."""
55
+ now = time.monotonic()
56
+ with _HF_HEAD_LOCK:
57
+ cached = _HF_HEAD_CACHE.get("value")
58
+ if not force and now - float(_HF_HEAD_CACHE["at"]) < 60:
59
+ return dict(cached) if cached else None
60
+
61
+ request = urllib.request.Request(
62
+ "https://huggingface.co/api/spaces/SZLHOLDINGS/killinchu"
63
+ "?expand[]=sha&expand[]=lastModified",
64
+ headers={
65
+ "User-Agent": "szl-killinchu-source-attestation/2.0",
66
+ "Accept": "application/json",
 
 
 
 
 
 
 
67
  },
68
+ )
69
+ value: dict[str, str] | None = None
70
+ try:
71
+ with urllib.request.urlopen(request, timeout=4) as response:
72
+ payload = json.load(response)
73
+ revision = payload.get("sha")
74
+ last_modified = payload.get("lastModified")
75
+ if (
76
+ isinstance(revision, str)
77
+ and len(revision) == 40
78
+ and all(char in "0123456789abcdef" for char in revision)
79
+ and isinstance(last_modified, str)
80
+ and "T" in last_modified
81
+ ):
82
+ value = {
83
+ "revision": revision,
84
+ "last_modified": last_modified,
85
+ "observed_at": _now(),
86
+ }
87
+ except Exception:
88
+ value = None
89
+
90
+ with _HF_HEAD_LOCK:
91
+ _HF_HEAD_CACHE.update({"at": time.monotonic(), "value": value})
92
+ return dict(value) if value else None
93
+
94
+
95
+ def source_attestation(force: bool = False) -> dict[str, Any]:
96
+ """Emit the estate deployment-source shape without claiming process parity."""
97
+ head = _hf_repository_head(force=force)
98
+ if head is None:
99
+ raise RuntimeError("Hugging Face repository-head evidence is unavailable")
100
+ return {
101
+ "schema": "szl.deployment-source/v1",
102
+ "source": {
103
+ "repository": GITHUB_REPOSITORY_ID,
104
+ "commit": GITHUB_OBSERVED_REVISION,
105
+ "path": "",
106
  },
107
+ "deployment": {
108
+ "hf_space": HF_SPACE_ID,
109
+ "hf_revision": head["revision"],
110
  },
111
+ "built_at": head["last_modified"],
112
+ "alignment_state": "PENDING_GITHUB_SYNC",
113
+ "extensions": {
114
+ "schema": "szl.killinchu.deployment-source-extension/v1",
115
+ "deployment_revision_evidence": {
116
+ "state": "MEASURED",
117
+ "semantics": (
118
+ "Hugging Face repository head measured through the Hub API; it is not "
119
+ "proof of the exact revision serving this response during a rolling deploy."
120
+ ),
121
+ "resolver": (
122
+ "https://huggingface.co/api/spaces/SZLHOLDINGS/killinchu"
123
+ "?expand[]=sha&expand[]=lastModified"
124
+ ),
125
+ "observed_at": head["observed_at"],
126
+ "running_process_revision_state": "NOT_EXPOSED_IN_PROCESS",
127
+ },
128
+ "source_observation": {
129
+ "repository_url": GITHUB_REPOSITORY,
130
+ "branch": "main",
131
+ "observed_at": REVISION_OBSERVED_AT,
132
+ "immutable_evidence": (
133
+ "https://api.github.com/repos/szl-holdings/killinchu/commits/"
134
+ + GITHUB_OBSERVED_REVISION
135
+ ),
136
+ "live_branch_head_resolver": (
137
+ "https://api.github.com/repos/szl-holdings/killinchu/commits/main"
138
+ ),
139
+ },
140
+ "overlay": {
141
+ "repository": HF_SPACE_REPOSITORY,
142
+ "base_revision": HF_OVERLAY_BASE_REVISION,
143
+ "base_revision_semantics": (
144
+ "Hugging Face revision on which this corrective overlay was based"
145
+ ),
146
+ },
147
+ "build_metadata": {
148
+ "source_git_revision_variable": _source_git_build_variable(),
149
+ "built_at_semantics": (
150
+ "Provider-reported lastModified for deployment.hf_revision; not a "
151
+ "verified process start time or reproducible-build attestation."
152
+ ),
153
+ },
154
  },
155
  "limits": [
156
+ "PENDING_GITHUB_SYNC means GitHub/Hugging Face content parity is not claimed.",
157
+ "deployment.hf_revision is repository-head evidence, not proof of the exact serving process revision.",
158
+ "source.commit is a point-in-time GitHub source observation, not a live branch-head claim.",
159
+ "SZL_GIT_SHA remains unverified source build metadata, not the Hugging Face revision.",
160
+ "This unsigned document does not establish SLSA provenance or binary reproducibility.",
161
  ],
162
  }
163
 
 
295
  return JSONResponse(melt_summary(app, ns), headers={"Cache-Control": "no-store"})
296
 
297
  async def _source_attestation(_request):
298
+ try:
299
+ payload = source_attestation(
300
+ force=_request.query_params.get("refresh") == "1"
301
+ )
302
+ status_code = 200
303
+ except RuntimeError:
304
+ payload = {
305
+ "schema": "szl.source-attestation-unavailable/v1",
306
+ "state": "UNAVAILABLE",
307
+ "observed_at": _now(),
308
+ "detail": "Hugging Face repository-head evidence could not be measured.",
309
+ }
310
+ status_code = 503
311
  return JSONResponse(
312
+ payload,
313
+ status_code=status_code,
314
  headers={"Cache-Control": "no-store", "X-Content-Type-Options": "nosniff"},
315
  )
316
 
tests/test_public_contracts.py CHANGED
@@ -53,34 +53,64 @@ class PublicContractTests(unittest.TestCase):
53
 
54
  def test_source_attestation_keeps_source_and_deployment_distinct(self):
55
  client = TestClient(self.build_app())
56
- with patch.dict("os.environ", {"SZL_GIT_SHA": "source-build-sha"}):
 
 
 
 
 
 
 
57
  response = client.get("/.well-known/szl-source.json")
58
  self.assertEqual(response.status_code, 200)
59
  self.assertEqual(response.headers["cache-control"], "no-store")
60
  payload = response.json()
61
- self.assertEqual(payload["schema"], "szl.source-attestation/v1")
62
- self.assertEqual(payload["attestation_state"], "PENDING_GITHUB_SYNC")
63
- self.assertEqual(payload["alignment_state"], "NOT_VERIFIED")
64
- self.assertEqual(
65
- payload["github_source_observation"]["revision"],
66
- killinchu_public_contracts.GITHUB_OBSERVED_REVISION,
67
- )
68
  self.assertEqual(
69
- payload["huggingface_overlay"]["base_revision"],
70
- killinchu_public_contracts.HF_OVERLAY_BASE_REVISION,
 
 
 
 
71
  )
72
  self.assertEqual(
73
- payload["huggingface_overlay"]["repository_head_state"],
74
- "RESOLVE_EXTERNALLY",
75
  )
 
 
76
  self.assertEqual(
77
- payload["huggingface_overlay"]["running_process_revision_state"],
 
 
78
  "NOT_EXPOSED_IN_PROCESS",
79
  )
80
- build_variable = payload["build_metadata"]["source_git_revision_variable"]
81
  self.assertEqual(build_variable["value"], "source-build-sha")
82
  self.assertFalse(build_variable["is_huggingface_deployment_revision"])
83
- self.assertEqual(payload["relationship"]["state"], "PENDING_GITHUB_SYNC")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
  def test_all_openapi_discovery_paths_avoid_legacy_500(self):
86
  client = TestClient(self.build_app())
 
53
 
54
  def test_source_attestation_keeps_source_and_deployment_distinct(self):
55
  client = TestClient(self.build_app())
56
+ head = {
57
+ "revision": "a" * 40,
58
+ "last_modified": "2026-07-11T16:18:34.000Z",
59
+ "observed_at": "2026-07-11T16:20:00+00:00",
60
+ }
61
+ with patch.dict("os.environ", {"SZL_GIT_SHA": "source-build-sha"}), patch.object(
62
+ killinchu_public_contracts, "_hf_repository_head", return_value=head
63
+ ):
64
  response = client.get("/.well-known/szl-source.json")
65
  self.assertEqual(response.status_code, 200)
66
  self.assertEqual(response.headers["cache-control"], "no-store")
67
  payload = response.json()
68
+ self.assertEqual(payload["schema"], "szl.deployment-source/v1")
69
+ self.assertEqual(payload["alignment_state"], "PENDING_GITHUB_SYNC")
 
 
 
 
 
70
  self.assertEqual(
71
+ payload["source"],
72
+ {
73
+ "repository": "szl-holdings/killinchu",
74
+ "commit": killinchu_public_contracts.GITHUB_OBSERVED_REVISION,
75
+ "path": "",
76
+ },
77
  )
78
  self.assertEqual(
79
+ payload["deployment"],
80
+ {"hf_space": "SZLHOLDINGS/killinchu", "hf_revision": "a" * 40},
81
  )
82
+ self.assertEqual(payload["built_at"], "2026-07-11T16:18:34.000Z")
83
+ extension = payload["extensions"]
84
  self.assertEqual(
85
+ extension["deployment_revision_evidence"][
86
+ "running_process_revision_state"
87
+ ],
88
  "NOT_EXPOSED_IN_PROCESS",
89
  )
90
+ build_variable = extension["build_metadata"]["source_git_revision_variable"]
91
  self.assertEqual(build_variable["value"], "source-build-sha")
92
  self.assertFalse(build_variable["is_huggingface_deployment_revision"])
93
+
94
+ # Mirrors the identity checks in check_ecosystem_drift.compare.
95
+ identity_errors = []
96
+ if payload.get("schema") != "szl.deployment-source/v1":
97
+ identity_errors.append("schema")
98
+ if payload.get("source", {}).get("repository") != "szl-holdings/killinchu":
99
+ identity_errors.append("source.repository")
100
+ if payload.get("deployment", {}).get("hf_space") != "SZLHOLDINGS/killinchu":
101
+ identity_errors.append("deployment.hf_space")
102
+ if payload.get("deployment", {}).get("hf_revision") != "a" * 40:
103
+ identity_errors.append("deployment.hf_revision")
104
+ self.assertEqual(identity_errors, [])
105
+
106
+ def test_source_attestation_fails_unknown_instead_of_claiming_stale_head(self):
107
+ client = TestClient(self.build_app())
108
+ with patch.object(
109
+ killinchu_public_contracts, "_hf_repository_head", return_value=None
110
+ ):
111
+ response = client.get("/.well-known/szl-source.json?refresh=1")
112
+ self.assertEqual(response.status_code, 503)
113
+ self.assertEqual(response.json()["state"], "UNAVAILABLE")
114
 
115
  def test_all_openapi_discovery_paths_avoid_legacy_500(self):
116
  client = TestClient(self.build_app())