personal memory agent
1from __future__ import annotations
2
3import subprocess
4from pathlib import Path
5
6from scripts.transparency_core import HEAD_LOG, PRODUCT, canonical_json_bytes
7from scripts.transparency_head_log import (
8 HeadLogRow,
9 append_head_row,
10 git_witness_status,
11 head_log_path,
12)
13
14
15def _git(repo: Path, *args: str) -> None:
16 subprocess.run(
17 ["git", *args],
18 cwd=repo,
19 check=True,
20 capture_output=True,
21 text=True,
22 )
23
24
25def _init_repo(repo: Path) -> None:
26 _git(repo, "init", "-q")
27 (repo / "README.md").write_text("fixture\n", encoding="utf-8")
28 _git(repo, "add", "README.md")
29 _git(
30 repo,
31 "-c",
32 "user.name=solstone-test",
33 "-c",
34 "user.email=solstone@example.invalid",
35 "commit",
36 "-qm",
37 "initial",
38 )
39
40
41def _row(seq: int, *, entry_sha256: str | None = None) -> HeadLogRow:
42 return HeadLogRow(
43 product=PRODUCT,
44 seq=seq,
45 version=f"0.0.{seq}",
46 entry_sha256=entry_sha256 or str(seq) * 64,
47 published_utc=f"2026-07-2{seq}T00:00:00Z",
48 )
49
50
51def test_append_head_row_preserves_prior_bytes(tmp_path: Path) -> None:
52 path = head_log_path(tmp_path)
53 prior = (
54 b'{"seq":1, "version":"0.0.1", "product":"solstone-journal", '
55 b'"published_utc":"2026-07-21T00:00:00Z", "entry_sha256":"'
56 + b"a" * 64
57 + b'"}\n'
58 )
59 path.write_bytes(prior)
60 new_row = _row(2, entry_sha256="b" * 64)
61
62 assert append_head_row(tmp_path, new_row) is True
63
64 expected_new = canonical_json_bytes(new_row.as_dict(), label=HEAD_LOG)
65 assert path.read_bytes() == prior + expected_new
66
67
68def test_git_witness_status_committed(tmp_path: Path) -> None:
69 _init_repo(tmp_path)
70 append_head_row(tmp_path, _row(1))
71 _git(tmp_path, "add", HEAD_LOG)
72 _git(
73 tmp_path,
74 "-c",
75 "user.name=solstone-test",
76 "-c",
77 "user.email=solstone@example.invalid",
78 "commit",
79 "-qm",
80 "commit head log",
81 )
82
83 status = git_witness_status(tmp_path)
84
85 assert status.state == "written-and-committed"
86
87
88def test_git_witness_status_tracked_but_modified(tmp_path: Path) -> None:
89 _init_repo(tmp_path)
90 append_head_row(tmp_path, _row(1))
91 _git(tmp_path, "add", HEAD_LOG)
92 _git(
93 tmp_path,
94 "-c",
95 "user.name=solstone-test",
96 "-c",
97 "user.email=solstone@example.invalid",
98 "commit",
99 "-qm",
100 "commit head log",
101 )
102 append_head_row(tmp_path, _row(2))
103
104 status = git_witness_status(tmp_path)
105
106 assert status.state == "written-uncommitted"
107
108
109def test_git_witness_status_untracked(tmp_path: Path) -> None:
110 _init_repo(tmp_path)
111 append_head_row(tmp_path, _row(1))
112
113 status = git_witness_status(tmp_path)
114
115 assert status.state == "written-untracked"