git-finalizer 2.0.0__py3-none-any.whl
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.
- git_finalizer/__init__.py +8 -0
- git_finalizer/delivery/__init__.py +3 -0
- git_finalizer/delivery/cli.py +166 -0
- git_finalizer/delivery/content_scan.py +524 -0
- git_finalizer/delivery/engine.py +2364 -0
- git_finalizer/delivery/integration_publish.py +547 -0
- git_finalizer/delivery/parser.py +552 -0
- git_finalizer/delivery/patch_check.py +165 -0
- git_finalizer/delivery/policy.py +311 -0
- git_finalizer/delivery/repo_bootstrap.py +588 -0
- git_finalizer/delivery/repo_state.py +612 -0
- git_finalizer/delivery/retirement_plan.py +740 -0
- git_finalizer/delivery/runtime.py +553 -0
- git_finalizer/delivery/scanners.py +329 -0
- git_finalizer/delivery/snapshot_verify.py +425 -0
- git_finalizer/delivery/source_review.py +374 -0
- git_finalizer/delivery/trailers.py +164 -0
- git_finalizer/delivery/validation.py +1095 -0
- git_finalizer/errors.py +105 -0
- git_finalizer/git_io.py +281 -0
- git_finalizer/json_io.py +55 -0
- git_finalizer/toolchain/__init__.py +3 -0
- git_finalizer/toolchain/tool_skill_sync.py +3044 -0
- git_finalizer/toolchain/tool_temp_dir.py +1057 -0
- git_finalizer/toolchain/workflow_health.py +504 -0
- git_finalizer-2.0.0.dist-info/METADATA +383 -0
- git_finalizer-2.0.0.dist-info/RECORD +30 -0
- git_finalizer-2.0.0.dist-info/WHEEL +4 -0
- git_finalizer-2.0.0.dist-info/entry_points.txt +5 -0
- git_finalizer-2.0.0.dist-info/licenses/LICENSE +202 -0
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
# GF-INTEGRATION-ADAPTER: snapshot-runner-evidence (single site: Snapshot Runner read verification)
|
|
2
|
+
"""Verify Snapshot Runner evidence before initial unborn-repository publication."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import hashlib
|
|
7
|
+
import os
|
|
8
|
+
from pathlib import Path, PurePosixPath
|
|
9
|
+
import re
|
|
10
|
+
import shutil
|
|
11
|
+
import stat
|
|
12
|
+
import subprocess
|
|
13
|
+
import sys
|
|
14
|
+
from typing import Any, NoReturn
|
|
15
|
+
|
|
16
|
+
from git_finalizer.errors import ErrorCode, FinalizerError
|
|
17
|
+
from git_finalizer.git_io import clean_git_env, find_git
|
|
18
|
+
from git_finalizer.json_io import loads_unique
|
|
19
|
+
|
|
20
|
+
EVIDENCE_SCHEMA_VERSION = 1
|
|
21
|
+
DATA_FIELDS = (
|
|
22
|
+
"status_short",
|
|
23
|
+
"staged_diff",
|
|
24
|
+
"unstaged_diff",
|
|
25
|
+
"file_context",
|
|
26
|
+
"baseline_kind",
|
|
27
|
+
"baseline_oid",
|
|
28
|
+
"initial_publication",
|
|
29
|
+
)
|
|
30
|
+
RUNNER_ERROR_CODE = re.compile(r"workflow_failed: ([A-Z][A-Z_]{0,63}):")
|
|
31
|
+
VERSION_TUPLE_RE = re.compile(r"(\d+)\.(\d+)\.(\d+)")
|
|
32
|
+
MAX_OUTPUT_BYTES = 16 * 1024 * 1024
|
|
33
|
+
MAX_SNAPSHOT_BYTES = 8 * 1024 * 1024
|
|
34
|
+
MAX_GENERATED_FILES = 500
|
|
35
|
+
MAX_GENERATED_BYTES = 8 * 1024 * 1024
|
|
36
|
+
MAX_SCAN_BYTES = 25 * 1024 * 1024
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def fail(message: str) -> NoReturn:
|
|
40
|
+
raise FinalizerError(
|
|
41
|
+
ErrorCode.SNAPSHOT_EVIDENCE_REJECTED,
|
|
42
|
+
f"snapshot evidence rejected: {message}",
|
|
43
|
+
final_phase="local_validation",
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _nonnegative(value: object) -> bool:
|
|
48
|
+
return isinstance(value, int) and not isinstance(value, bool) and 0 <= value <= 2**63 - 1
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _safe_path(value: object) -> bool:
|
|
52
|
+
if not isinstance(value, str) or not value:
|
|
53
|
+
return False
|
|
54
|
+
posix = PurePosixPath(value)
|
|
55
|
+
return (
|
|
56
|
+
value not in {".", "..", ".git"}
|
|
57
|
+
and not posix.is_absolute()
|
|
58
|
+
and not value.startswith(("/", "./"))
|
|
59
|
+
and not value.endswith("/")
|
|
60
|
+
and "\\" not in value
|
|
61
|
+
and "\0" not in value
|
|
62
|
+
and ".." not in posix.parts
|
|
63
|
+
and ".git" not in posix.parts
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _git_command(
|
|
68
|
+
repo: Path,
|
|
69
|
+
*arguments: str,
|
|
70
|
+
git_env: dict[str, str] | None = None,
|
|
71
|
+
) -> subprocess.CompletedProcess[bytes]:
|
|
72
|
+
git_bin = find_git(git_env.get("PATH") if git_env else None)
|
|
73
|
+
env = clean_git_env(git_env)
|
|
74
|
+
try:
|
|
75
|
+
return subprocess.run(
|
|
76
|
+
[git_bin, "-C", str(repo), *arguments],
|
|
77
|
+
capture_output=True,
|
|
78
|
+
timeout=30,
|
|
79
|
+
env=env,
|
|
80
|
+
close_fds=True,
|
|
81
|
+
check=False,
|
|
82
|
+
)
|
|
83
|
+
except (OSError, subprocess.SubprocessError) as exc:
|
|
84
|
+
fail(f"Git inspection failed: {exc}")
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def _parse_version_tuple(version_str: object) -> tuple[int, int, int] | None:
|
|
88
|
+
if not isinstance(version_str, str):
|
|
89
|
+
return None
|
|
90
|
+
match = VERSION_TUPLE_RE.search(version_str)
|
|
91
|
+
if match is None:
|
|
92
|
+
return None
|
|
93
|
+
return (int(match.group(1)), int(match.group(2)), int(match.group(3)))
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _run_runner(runner: str, arguments: list[str]) -> dict[str, Any]:
|
|
97
|
+
env = dict(os.environ)
|
|
98
|
+
env["LC_ALL"] = "C"
|
|
99
|
+
env["PYTHONDONTWRITEBYTECODE"] = "1"
|
|
100
|
+
try:
|
|
101
|
+
result = subprocess.run(
|
|
102
|
+
[runner, *arguments],
|
|
103
|
+
capture_output=True,
|
|
104
|
+
timeout=30,
|
|
105
|
+
env=env,
|
|
106
|
+
close_fds=True,
|
|
107
|
+
check=False,
|
|
108
|
+
)
|
|
109
|
+
except (OSError, subprocess.SubprocessError):
|
|
110
|
+
fail("Snapshot Runner invocation failed")
|
|
111
|
+
if result.returncode != 0:
|
|
112
|
+
stderr = result.stderr.decode("utf-8", errors="replace")
|
|
113
|
+
match = RUNNER_ERROR_CODE.search(stderr)
|
|
114
|
+
if match:
|
|
115
|
+
fail(f"Snapshot Runner refused the evidence read ({match.group(1)})")
|
|
116
|
+
fail("Snapshot Runner refused the evidence read")
|
|
117
|
+
if len(result.stdout) > MAX_OUTPUT_BYTES:
|
|
118
|
+
fail("Snapshot Runner evidence exceeded the read budget")
|
|
119
|
+
try:
|
|
120
|
+
document = loads_unique(result.stdout, allow_nonfinite=False)
|
|
121
|
+
except (UnicodeDecodeError, ValueError):
|
|
122
|
+
fail("Snapshot Runner returned invalid evidence JSON")
|
|
123
|
+
if not isinstance(document, dict):
|
|
124
|
+
fail("Snapshot Runner evidence root must be an object")
|
|
125
|
+
return document
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _validate_envelope(
|
|
129
|
+
document: dict[str, Any],
|
|
130
|
+
snapshot_id: str,
|
|
131
|
+
repository_name: str,
|
|
132
|
+
expected_selector: dict[str, Any],
|
|
133
|
+
) -> dict[str, Any]:
|
|
134
|
+
if (
|
|
135
|
+
document.get("evidence_schema_version") != EVIDENCE_SCHEMA_VERSION
|
|
136
|
+
or document.get("command") != "diff-audit"
|
|
137
|
+
or document.get("snapshot_id") != snapshot_id
|
|
138
|
+
or document.get("repository") != repository_name
|
|
139
|
+
or document.get("status") != "complete"
|
|
140
|
+
or document.get("truncated") is not False
|
|
141
|
+
or document.get("evidence_gap") is not False
|
|
142
|
+
or document.get("found") is not True
|
|
143
|
+
):
|
|
144
|
+
fail("snapshot metadata does not establish complete coverage")
|
|
145
|
+
if document.get("selector") != expected_selector:
|
|
146
|
+
fail("snapshot read selector does not match request")
|
|
147
|
+
evidence = document.get("evidence")
|
|
148
|
+
if not isinstance(evidence, dict):
|
|
149
|
+
fail("snapshot evidence payload is missing")
|
|
150
|
+
return evidence
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def _read_worktree(repo: Path, path: str) -> tuple[bytes, bool]:
|
|
154
|
+
target = repo / path
|
|
155
|
+
if target.is_symlink() or not target.is_file():
|
|
156
|
+
fail(f"candidate worktree target is missing or non-regular: {path}")
|
|
157
|
+
info = target.lstat()
|
|
158
|
+
if info.st_size > MAX_SCAN_BYTES:
|
|
159
|
+
fail(f"candidate file exceeds scan byte budget: {path}")
|
|
160
|
+
return target.read_bytes(), bool(stat.S_IMODE(info.st_mode) & 0o111)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def _read_git_version(
|
|
164
|
+
repo: Path,
|
|
165
|
+
stage: str,
|
|
166
|
+
path: str,
|
|
167
|
+
expected_bytes: int | None = None,
|
|
168
|
+
*,
|
|
169
|
+
git_env: dict[str, str] | None = None,
|
|
170
|
+
) -> tuple[bytes, bool]:
|
|
171
|
+
if stage == "index":
|
|
172
|
+
ls = _git_command(
|
|
173
|
+
repo, "--literal-pathspecs", "ls-files", "--stage", "-z", "--", path, git_env=git_env
|
|
174
|
+
)
|
|
175
|
+
else:
|
|
176
|
+
ls = _git_command(
|
|
177
|
+
repo, "--literal-pathspecs", "ls-tree", "-z", "HEAD", "--", path, git_env=git_env
|
|
178
|
+
)
|
|
179
|
+
if ls.returncode != 0:
|
|
180
|
+
fail(f"unable to inspect {stage} entry for {path}")
|
|
181
|
+
entries = [part for part in ls.stdout.split(b"\0") if part]
|
|
182
|
+
if len(entries) != 1:
|
|
183
|
+
fail(f"missing or ambiguous {stage} entry for {path}")
|
|
184
|
+
line = entries[0].decode("utf-8", errors="strict")
|
|
185
|
+
header, _, rec_path = line.partition("\t")
|
|
186
|
+
if rec_path != path:
|
|
187
|
+
fail(f"{stage} path mismatch for {path}")
|
|
188
|
+
tokens = header.split()
|
|
189
|
+
if stage == "index":
|
|
190
|
+
if len(tokens) != 3 or tokens[2] != "0":
|
|
191
|
+
fail(f"invalid index stage entry for {path}")
|
|
192
|
+
mode, oid = tokens[0], tokens[1]
|
|
193
|
+
else:
|
|
194
|
+
if len(tokens) != 3 or tokens[1] != "blob":
|
|
195
|
+
fail(f"invalid HEAD tree entry for {path}")
|
|
196
|
+
mode, oid = tokens[0], tokens[2]
|
|
197
|
+
if mode not in {"100644", "100755"}:
|
|
198
|
+
fail(f"unsupported Git file mode {mode} for {path}")
|
|
199
|
+
|
|
200
|
+
# Check object size via cat-file -s BEFORE materializing the blob in memory.
|
|
201
|
+
size_res = _git_command(repo, "cat-file", "-s", oid, git_env=git_env)
|
|
202
|
+
if size_res.returncode != 0:
|
|
203
|
+
fail(f"unable to read blob size for {path}")
|
|
204
|
+
try:
|
|
205
|
+
actual_size = int(size_res.stdout.decode("ascii").strip())
|
|
206
|
+
except ValueError:
|
|
207
|
+
fail(f"invalid blob size response for {path}")
|
|
208
|
+
if actual_size > MAX_SCAN_BYTES:
|
|
209
|
+
fail(f"{stage} blob exceeds scan byte budget: {path}")
|
|
210
|
+
if expected_bytes is not None and actual_size != expected_bytes:
|
|
211
|
+
fail(f"{stage} file byte count changed after snapshot: {path}")
|
|
212
|
+
|
|
213
|
+
blob = _git_command(repo, "cat-file", "blob", oid, git_env=git_env)
|
|
214
|
+
if blob.returncode != 0:
|
|
215
|
+
fail(f"unable to read {stage} blob for {path}")
|
|
216
|
+
return blob.stdout, mode == "100755"
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def _workspace_digest(records: list[dict[str, Any]]) -> str:
|
|
220
|
+
h = hashlib.sha256()
|
|
221
|
+
for record in sorted(records, key=lambda item: str(item["path"])):
|
|
222
|
+
h.update(str(record["path"]).encode("utf-8"))
|
|
223
|
+
h.update(b"\0")
|
|
224
|
+
h.update(str(record["bytes"]).encode("ascii"))
|
|
225
|
+
h.update(b"\0")
|
|
226
|
+
h.update(str(record["sha256"]).encode("ascii"))
|
|
227
|
+
h.update(b"\0")
|
|
228
|
+
h.update(b"1" if record["executable"] is True else b"0")
|
|
229
|
+
h.update(b"\n")
|
|
230
|
+
return h.hexdigest()
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def verify_snapshot_evidence(
|
|
234
|
+
snapshot_id: str,
|
|
235
|
+
repo: Path,
|
|
236
|
+
stage: str,
|
|
237
|
+
requested_paths: list[str],
|
|
238
|
+
*,
|
|
239
|
+
git_env: dict[str, str] | None = None,
|
|
240
|
+
) -> None:
|
|
241
|
+
"""Verify Snapshot Runner evidence matches all requested files at `stage` (`worktree|index|head`)."""
|
|
242
|
+
if stage not in {"worktree", "index", "head"}:
|
|
243
|
+
fail(f"invalid snapshot verification stage: {stage}")
|
|
244
|
+
if not requested_paths:
|
|
245
|
+
fail("snapshot verification requires explicit paths")
|
|
246
|
+
|
|
247
|
+
runner = shutil.which("snapshot-runner")
|
|
248
|
+
if not runner:
|
|
249
|
+
fail("Snapshot Runner is unavailable on PATH")
|
|
250
|
+
|
|
251
|
+
repo_resolved = repo.resolve()
|
|
252
|
+
repo_name = repo_resolved.name
|
|
253
|
+
|
|
254
|
+
index_doc = _run_runner(runner, ["read", snapshot_id, "--repo", str(repo_resolved)])
|
|
255
|
+
index_evidence = _validate_envelope(index_doc, snapshot_id, repo_name, {"kind": "index"})
|
|
256
|
+
fields_list = index_evidence.get("fields")
|
|
257
|
+
if not isinstance(fields_list, list):
|
|
258
|
+
fail("snapshot index fields missing")
|
|
259
|
+
runner_version = _parse_version_tuple(index_doc.get("runner_version"))
|
|
260
|
+
allowed_fields = (
|
|
261
|
+
set(DATA_FIELDS) | {"current_branch", "head"}
|
|
262
|
+
if runner_version is not None and runner_version >= (2, 6, 0)
|
|
263
|
+
else set(DATA_FIELDS)
|
|
264
|
+
)
|
|
265
|
+
available_fields = [item.get("field") for item in fields_list if isinstance(item, dict)]
|
|
266
|
+
if (
|
|
267
|
+
len(available_fields) != len(fields_list)
|
|
268
|
+
or len(set(available_fields)) != len(available_fields)
|
|
269
|
+
or not set(DATA_FIELDS).issubset(available_fields)
|
|
270
|
+
or not set(available_fields).issubset(allowed_fields)
|
|
271
|
+
):
|
|
272
|
+
fail("snapshot fields do not match required initial publication coverage")
|
|
273
|
+
data: dict[str, Any] = {}
|
|
274
|
+
# Use SR 2.6.0 batch `--fields` when runner >= 2.6.0, falling back to `--field` for SR 2.5.13/stubs.
|
|
275
|
+
if runner_version is not None and runner_version >= (2, 6, 0):
|
|
276
|
+
batch_arg = ",".join(DATA_FIELDS)
|
|
277
|
+
batch_doc = _run_runner(
|
|
278
|
+
runner,
|
|
279
|
+
["read", snapshot_id, "--repo", str(repo_resolved), "--fields", batch_arg],
|
|
280
|
+
)
|
|
281
|
+
batch_evidence = _validate_envelope(
|
|
282
|
+
batch_doc,
|
|
283
|
+
snapshot_id,
|
|
284
|
+
repo_name,
|
|
285
|
+
{"kind": "fields", "values": list(DATA_FIELDS)},
|
|
286
|
+
)
|
|
287
|
+
values = batch_evidence.get("fields")
|
|
288
|
+
if not isinstance(values, dict) or set(values) != set(DATA_FIELDS):
|
|
289
|
+
fail("batch fields payload mismatch")
|
|
290
|
+
data = dict(values)
|
|
291
|
+
else:
|
|
292
|
+
for field in DATA_FIELDS:
|
|
293
|
+
field_doc = _run_runner(
|
|
294
|
+
runner, ["read", snapshot_id, "--repo", str(repo_resolved), "--field", field]
|
|
295
|
+
)
|
|
296
|
+
field_evidence = _validate_envelope(
|
|
297
|
+
field_doc, snapshot_id, repo_name, {"kind": "field", "value": field}
|
|
298
|
+
)
|
|
299
|
+
if field_evidence.get("field") != field or "value" not in field_evidence:
|
|
300
|
+
fail(f"snapshot field selector mismatch: {field}")
|
|
301
|
+
data[field] = field_evidence["value"]
|
|
302
|
+
|
|
303
|
+
empty_tree_res = _git_command(repo, "hash-object", "-t", "tree", "/dev/null", git_env=git_env)
|
|
304
|
+
empty_tree = empty_tree_res.stdout.decode("ascii").strip()
|
|
305
|
+
if data.get("baseline_kind") != "empty_tree" or data.get("baseline_oid") != empty_tree:
|
|
306
|
+
fail("snapshot baseline is not the empty tree")
|
|
307
|
+
|
|
308
|
+
publication = data.get("initial_publication")
|
|
309
|
+
if not isinstance(publication, dict):
|
|
310
|
+
fail("initial_publication metadata is missing")
|
|
311
|
+
|
|
312
|
+
records = publication.get("files")
|
|
313
|
+
if not isinstance(records, list) or not records:
|
|
314
|
+
fail("initial_publication files are missing")
|
|
315
|
+
if publication.get("changed_file_count") != len(requested_paths):
|
|
316
|
+
fail("snapshot changed_file_count differs from explicit scope")
|
|
317
|
+
|
|
318
|
+
expected_set = set(requested_paths)
|
|
319
|
+
observed_paths: list[str] = []
|
|
320
|
+
content_records: dict[str, dict[str, Any]] = {}
|
|
321
|
+
generated_count = 0
|
|
322
|
+
generated_bytes = 0
|
|
323
|
+
total_snapshot_bytes = 0
|
|
324
|
+
|
|
325
|
+
for record in records:
|
|
326
|
+
if not isinstance(record, dict):
|
|
327
|
+
fail("initial_publication file entry is invalid")
|
|
328
|
+
path = record.get("path")
|
|
329
|
+
sha = record.get("sha256")
|
|
330
|
+
byte_count = record.get("bytes")
|
|
331
|
+
executable = record.get("executable")
|
|
332
|
+
coverage = record.get("coverage")
|
|
333
|
+
if (
|
|
334
|
+
not _safe_path(path)
|
|
335
|
+
or not isinstance(sha, str)
|
|
336
|
+
or not isinstance(executable, bool)
|
|
337
|
+
or not _nonnegative(byte_count)
|
|
338
|
+
or coverage not in {"content", "generated_manifest"}
|
|
339
|
+
):
|
|
340
|
+
fail("initial_publication file record is malformed")
|
|
341
|
+
assert isinstance(path, str) and isinstance(byte_count, int)
|
|
342
|
+
observed_paths.append(path)
|
|
343
|
+
total_snapshot_bytes += byte_count
|
|
344
|
+
|
|
345
|
+
if stage == "worktree":
|
|
346
|
+
actual_bytes, actual_exec = _read_worktree(repo, path)
|
|
347
|
+
else:
|
|
348
|
+
actual_bytes, actual_exec = _read_git_version(
|
|
349
|
+
repo, stage, path, expected_bytes=byte_count, git_env=git_env
|
|
350
|
+
)
|
|
351
|
+
|
|
352
|
+
if len(actual_bytes) != byte_count or hashlib.sha256(actual_bytes).hexdigest() != sha:
|
|
353
|
+
fail(f"file changed after snapshot was collected: {path}")
|
|
354
|
+
if actual_exec != executable:
|
|
355
|
+
fail(f"file executable mode changed after snapshot: {path}")
|
|
356
|
+
|
|
357
|
+
if coverage == "content":
|
|
358
|
+
content_records[path] = {"bytes": actual_bytes, "size": byte_count}
|
|
359
|
+
else:
|
|
360
|
+
generated_count += 1
|
|
361
|
+
generated_bytes += byte_count
|
|
362
|
+
|
|
363
|
+
if (
|
|
364
|
+
len(observed_paths) != len(set(observed_paths))
|
|
365
|
+
or set(observed_paths) != expected_set
|
|
366
|
+
or total_snapshot_bytes > MAX_SNAPSHOT_BYTES
|
|
367
|
+
or _workspace_digest(records) != publication.get("workspace_sha256")
|
|
368
|
+
):
|
|
369
|
+
fail("snapshot does not exactly cover the explicit publication paths")
|
|
370
|
+
|
|
371
|
+
# Verify file_context paths AND actual content against the workspace/index/head bytes.
|
|
372
|
+
contexts = data.get("file_context")
|
|
373
|
+
if not isinstance(contexts, list):
|
|
374
|
+
fail("snapshot content contexts are invalid")
|
|
375
|
+
context_paths: list[str] = []
|
|
376
|
+
for context in contexts:
|
|
377
|
+
if (
|
|
378
|
+
not isinstance(context, dict)
|
|
379
|
+
or set(context) not in ({"path", "content"}, {"path", "content", "source", "executable"})
|
|
380
|
+
or not isinstance(context.get("path"), str)
|
|
381
|
+
or not isinstance(context.get("content"), str)
|
|
382
|
+
):
|
|
383
|
+
fail("snapshot content context is invalid")
|
|
384
|
+
if "source" in context and (
|
|
385
|
+
context.get("source") not in {"untracked", "worktree"}
|
|
386
|
+
or not isinstance(context.get("executable"), bool)
|
|
387
|
+
):
|
|
388
|
+
fail("snapshot content context source is invalid")
|
|
389
|
+
ctx_path = str(context["path"])
|
|
390
|
+
ctx_content = str(context["content"])
|
|
391
|
+
if ctx_path in content_records:
|
|
392
|
+
if "[REDACTED_" in ctx_content:
|
|
393
|
+
fail(f"snapshot content context contains redacted secret markers: {ctx_path}")
|
|
394
|
+
try:
|
|
395
|
+
actual_text = content_records[ctx_path]["bytes"].decode("utf-8", errors="strict")
|
|
396
|
+
except UnicodeDecodeError:
|
|
397
|
+
fail(f"snapshot content file is not valid UTF-8: {ctx_path}")
|
|
398
|
+
if ctx_content != actual_text and "<<ABS_PATH:" not in ctx_content:
|
|
399
|
+
fail(f"snapshot content context does not match candidate file content: {ctx_path}")
|
|
400
|
+
context_paths.append(ctx_path)
|
|
401
|
+
|
|
402
|
+
if len(context_paths) != len(set(context_paths)) or set(context_paths) != set(content_records):
|
|
403
|
+
fail("snapshot content contexts do not match content coverage")
|
|
404
|
+
print(f"snapshot evidence verified: source={stage} covered={len(observed_paths)}/{len(requested_paths)}", flush=True)
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
def main(argv: list[str] | None = None) -> int:
|
|
408
|
+
args = list(sys.argv[1:] if argv is None else argv)
|
|
409
|
+
if len(args) < 4:
|
|
410
|
+
print(
|
|
411
|
+
"usage: snapshot_verify <snapshot_id> <repo> <worktree|index|head> <path>...",
|
|
412
|
+
file=sys.stderr,
|
|
413
|
+
)
|
|
414
|
+
return 2
|
|
415
|
+
snapshot_id, repo_raw, stage, *paths = args
|
|
416
|
+
try:
|
|
417
|
+
verify_snapshot_evidence(snapshot_id, Path(repo_raw), stage, paths)
|
|
418
|
+
return 0
|
|
419
|
+
except FinalizerError as exc:
|
|
420
|
+
print(f"ERROR [{exc.code}]: {exc.message}", file=sys.stderr)
|
|
421
|
+
return exc.exit_code
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
if __name__ == "__main__":
|
|
425
|
+
raise SystemExit(main())
|