Repository navigation
Expand file tree
/
Copy pathincremental_memory.py
More file actions
313 lines (270 loc) · 9.24 KB
/
Copy pathincremental_memory.py
File metadata and controls
313 lines (270 loc) · 9.24 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
"""Incremental review memory for commit-aware AI review context."""
from __future__ import annotations
import json
import subprocess
from datetime import UTC, datetime
from pathlib import Path
from typing import Any
from core.discovery_api import safe_write_text
_MEMORY_FILE_NAME = "ai_review_memory.json"
_COMMIT_HEADER_PREFIX = "__C__"
def review_memory_path(repo_root: Path) -> Path:
"""Return persisted memory path under .structorium."""
return repo_root / ".structorium" / _MEMORY_FILE_NAME
def record_review_import_memory(
*,
repo_root: Path,
findings_payload: dict[str, Any],
assessment_mode: str,
max_entries: int = 240,
) -> None:
"""Persist one durable memory entry for imported review output."""
findings = findings_payload.get("findings", [])
if not isinstance(findings, list):
findings = []
touched_files = _extract_touched_files(findings)
touched_dimensions = _extract_dimensions(findings)
entry = {
"recorded_at": datetime.now(UTC).isoformat(timespec="seconds"),
"commit_sha": _git_head_sha(repo_root),
"assessment_mode": assessment_mode,
"finding_count": len(findings),
"dimensions": touched_dimensions[:24],
"files": touched_files[:40],
}
path = review_memory_path(repo_root)
data = _load_memory_file(path)
entries = data.get("entries", [])
if not isinstance(entries, list):
entries = []
entries.append(entry)
if len(entries) > max_entries:
entries = entries[-max_entries:]
data["entries"] = entries
path.parent.mkdir(parents=True, exist_ok=True)
safe_write_text(path, json.dumps(data, indent=2) + "\n")
def build_incremental_review_memory(
*,
repo_root: Path,
focus_files: list[str],
max_feedback_entries: int,
max_commits: int,
) -> dict[str, object]:
"""Build combined memory from persisted review feedback and git commit history."""
focus = {_normalize_path(path) for path in focus_files if isinstance(path, str) and path}
memory_data = _load_memory_file(review_memory_path(repo_root))
stored_entries = memory_data.get("entries", [])
if not isinstance(stored_entries, list):
stored_entries = []
feedback_entries: list[dict[str, object]] = []
for item in reversed(stored_entries):
if not isinstance(item, dict):
continue
files = item.get("files", [])
if focus and isinstance(files, list):
normalized_files = {
_normalize_path(path)
for path in files
if isinstance(path, str) and path
}
if normalized_files and not normalized_files.intersection(focus):
continue
feedback_entries.append(
{
"recorded_at": str(item.get("recorded_at", "")),
"commit_sha": str(item.get("commit_sha", "")),
"assessment_mode": str(item.get("assessment_mode", "")),
"finding_count": _safe_int(item.get("finding_count")),
"dimensions": _string_list(item.get("dimensions"), limit=8),
"files": _string_list(item.get("files"), limit=8),
}
)
if len(feedback_entries) >= max_feedback_entries:
break
commits = _recent_commits(repo_root=repo_root, focus=focus, max_commits=max_commits)
if commits is None:
return {
"status": "unavailable",
"reason": "git_history_unavailable",
"feedback_entries": feedback_entries,
}
return {
"status": "ready",
"focus_file_count": len(focus),
"feedback_entries": feedback_entries,
"recent_commits": commits,
}
def _load_memory_file(path: Path) -> dict[str, object]:
if not path.exists():
return {"entries": []}
try:
payload = json.loads(path.read_text())
except (OSError, json.JSONDecodeError):
return {"entries": []}
if not isinstance(payload, dict):
return {"entries": []}
return payload
def _git_head_sha(repo_root: Path) -> str:
output = _git_output(repo_root, ["rev-parse", "HEAD"])
if output is None:
return ""
return output.strip()
def _recent_commits(
*,
repo_root: Path,
focus: set[str],
max_commits: int,
) -> list[dict[str, object]] | None:
raw = _git_output(
repo_root,
[
"log",
"--date-order",
f"-n{max(1, max_commits)}",
f"--pretty=format:{_COMMIT_HEADER_PREFIX}%H|%ct|%s",
"--name-only",
],
)
if raw is None:
return None
commits: list[dict[str, object]] = []
current: dict[str, object] | None = None
for line in raw.splitlines():
text = line.strip()
if not text:
continue
if text.startswith(_COMMIT_HEADER_PREFIX):
if current is not None and _commit_matches_focus(current, focus):
commits.append(_compact_commit(current))
current = _parse_commit_header(text)
continue
if current is None:
continue
files = current.setdefault("files", [])
if isinstance(files, list):
files.append(_normalize_path(text))
if current is not None and _commit_matches_focus(current, focus):
commits.append(_compact_commit(current))
return commits[: min(len(commits), 16)]
def _parse_commit_header(raw: str) -> dict[str, object]:
body = raw[len(_COMMIT_HEADER_PREFIX):]
parts = body.split("|", 2)
sha = parts[0] if len(parts) >= 1 else ""
ts = _safe_int(parts[1]) if len(parts) >= 2 else 0
summary = parts[2] if len(parts) >= 3 else ""
return {"sha": sha, "timestamp": ts, "summary": summary, "files": []}
def _commit_matches_focus(commit: dict[str, object], focus: set[str]) -> bool:
if not focus:
return True
files = commit.get("files", [])
if not isinstance(files, list):
return False
normalized = {
_normalize_path(item)
for item in files
if isinstance(item, str) and item.strip()
}
return bool(normalized.intersection(focus))
def _compact_commit(commit: dict[str, object]) -> dict[str, object]:
ts = _safe_int(commit.get("timestamp"))
timestamp = (
datetime.fromtimestamp(ts, tz=UTC).isoformat(timespec="seconds")
if ts > 0
else ""
)
files = commit.get("files", [])
return {
"sha": str(commit.get("sha", ""))[:12],
"timestamp": timestamp,
"summary": str(commit.get("summary", "")),
"files": _string_list(files, limit=10),
}
def _extract_touched_files(findings: list[object]) -> list[str]:
seen: set[str] = set()
out: list[str] = []
for item in findings:
if not isinstance(item, dict):
continue
file_direct = item.get("file")
if isinstance(file_direct, str) and file_direct.strip():
normalized = _normalize_path(file_direct)
if normalized not in seen:
seen.add(normalized)
out.append(normalized)
related_files = item.get("related_files", [])
if not isinstance(related_files, list):
continue
for rel in related_files:
if not isinstance(rel, str) or not rel.strip():
continue
normalized = _normalize_path(rel)
if normalized in seen:
continue
seen.add(normalized)
out.append(normalized)
return out
def _extract_dimensions(findings: list[object]) -> list[str]:
seen: set[str] = set()
out: list[str] = []
for item in findings:
if not isinstance(item, dict):
continue
raw = item.get("dimension")
if not isinstance(raw, str):
continue
dim = raw.strip()
if not dim or dim in seen:
continue
seen.add(dim)
out.append(dim)
return out
def _normalize_path(raw: str) -> str:
return raw.strip().replace("\\", "/").lstrip("./")
def _string_list(value: object, *, limit: int) -> list[str]:
if not isinstance(value, list):
return []
out: list[str] = []
for item in value:
if not isinstance(item, str):
continue
text = item.strip()
if not text:
continue
out.append(text)
if len(out) >= limit:
break
return out
def _safe_int(value: object) -> int:
if isinstance(value, bool):
return 0
if isinstance(value, int):
return value
if isinstance(value, float):
return int(value)
if isinstance(value, str):
text = value.strip()
if text:
try:
return int(text)
except ValueError:
return 0
return 0
def _git_output(repo_root: Path, args: list[str]) -> str | None:
try:
result = subprocess.run(
["git", "-C", str(repo_root), *args],
check=False,
capture_output=True,
text=True,
timeout=20,
)
except (OSError, subprocess.SubprocessError):
return None
if result.returncode != 0:
return None
return result.stdout
__all__ = [
"build_incremental_review_memory",
"record_review_import_memory",
"review_memory_path",
]