forked from ChelseaKR/habitable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_golden_packet.py
More file actions
139 lines (116 loc) · 4.72 KB
/
Copy pathmake_golden_packet.py
File metadata and controls
139 lines (116 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright 2026 Chelsea Kelly-Reif
"""Regenerate the current-version golden packet fixture.
The golden corpus (``tests/golden/packet-vN/``) pins backward compatibility: every
packet version habitable has ever emitted must keep verifying forever
(``tests/test_golden.py``). Run this after an intentional, reviewed change to the
export format to (re)build the *current* version's fixture:
uv run python scripts/make_golden_packet.py
It builds a fresh, self-contained packet with the real pipeline (a deterministic
clock and a fixed-time local RFC 3161 issuer) and copies the verifiable subset —
``bundle.json``, its signature, and the shared media — into
``tests/golden/packet-v<PACKET_VERSION>/``. Older fixtures (e.g. ``packet-v1``) are
the back-compat contract and are never touched by this script.
"""
from __future__ import annotations
import shutil
import tempfile
from collections.abc import Callable
from pathlib import Path
import piexif
from PIL import Image
from habitable.artifact import add_relationship, capture_artifact
from habitable.capture import capture
from habitable.packet import PACKET_VERSION, build_packet
from habitable.tsa import LocalRfc3161TSA
from habitable.vault import Vault
_FIXED_EPOCH = 1_767_312_000 # 2026-01-02T00:00:00Z — reproducible timestamps
_GENERATED_AT = "2026-01-02T00:10:00Z"
_ROOT = Path(__file__).resolve().parent.parent
_GOLDEN = _ROOT / "tests" / "golden" / f"packet-v{PACKET_VERSION}"
def _counter_ms(start_ms: int) -> Callable[[], int]:
state = {"t": start_ms}
def tick() -> int:
state["t"] += 1
return state["t"]
return tick
def _synthetic_photo(path: Path) -> None:
"""A never-real JPEG carrying an EXIF capture time and GPS (stripped on export)."""
exif = {
"0th": {},
"Exif": {piexif.ExifIFD.DateTimeOriginal: b"2026:01:02 09:15:00"},
"GPS": {
piexif.GPSIFD.GPSLatitudeRef: b"N",
piexif.GPSIFD.GPSLatitude: ((38, 1), (33, 1), (0, 1)),
piexif.GPSIFD.GPSLongitudeRef: b"W",
piexif.GPSIFD.GPSLongitude: ((121, 1), (44, 1), (0, 1)),
},
"1st": {},
"thumbnail": None,
}
Image.new("RGB", (48, 48), (70, 70, 60)).save(path, "jpeg", exif=piexif.dump(exif))
def main() -> int:
work = Path(tempfile.mkdtemp(prefix="golden-packet-"))
vault = Vault.create(
work / "vault",
"golden-passphrase",
case_id="golden-4B",
unit="4B",
time_source=_counter_ms(_FIXED_EPOCH * 1000),
)
issue = vault.document.add_issue(
category="mold", room="bathroom", title="Black mold", severity="high"
)
vault.add_timeline_event(
issue,
event_type="condition_observed",
text="mold on ceiling",
occurred_at="2026-01-02",
source="firsthand",
)
photo = work / "p.jpg"
_synthetic_photo(photo)
tsa = LocalRfc3161TSA("golden-tsa", time_source=lambda: _FIXED_EPOCH)
capture(vault, photo, issue_id=issue, tsa=tsa)
# Exercise the version-specific surfaces, not just the shape every version
# shares (issue #160). A fixture that carries no artifact, relationship,
# profile, or handoff view leaves `_verify_v4_workflows` -- roughly 250
# lines of hostile-input parsing in the standalone verifier -- with nothing
# to bite on, which is how packet v4 shipped pinned by nothing.
vault.document.set_use_case_profile("repair_delivery")
request = work / "repair-request.txt"
request.write_text(
"Synthetic repair request for the golden fixture. Never real tenant data.\n",
encoding="utf-8",
)
artifact = capture_artifact(
vault,
request,
issue_id=issue,
artifact_type="repair_request",
title="Repair request",
source_assertion="tenant copy",
occurred_at="2026-01-02",
tsa=tsa,
)
add_relationship(
vault,
issue_id=issue,
relationship_type="documents_condition",
source_id=artifact.artifact_id,
target_id=issue,
)
out = work / "packet"
build_packet(vault, out, generated_at=_GENERATED_AT, make_pdf=False)
# Commit only the verifiable, self-contained subset (mirrors packet-v1's shape).
if _GOLDEN.exists():
shutil.rmtree(_GOLDEN)
(_GOLDEN / "media").mkdir(parents=True)
shutil.copy(out / "bundle.json", _GOLDEN / "bundle.json")
shutil.copy(out / "bundle.sig.json", _GOLDEN / "bundle.sig.json")
for media_file in sorted((out / "media").iterdir()):
shutil.copy(media_file, _GOLDEN / "media" / media_file.name)
print(f"wrote {_GOLDEN.relative_to(_ROOT)} (packet_version={PACKET_VERSION})")
return 0
if __name__ == "__main__":
raise SystemExit(main())