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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
|
"""
Git-derived diff/review-state helpers for the security-guidance plugin.
Extracted from security_reminder_hook.py for readability. Re-exported
there so callers keep resolving bare names through the hook module's
globals — tests that ``monkeypatch.setattr(hook, "<fn>", …)`` continue
to work without retargeting.
"""
import hashlib
import os
import subprocess
import sys
import unicodedata
import time
from _base import debug_log, make_state_dir, portable_output, _PV
import gitutil
from gitutil import (
GIT_CMD,
_git_dir, _git_toplevel, _git_status_porcelain,
_git_rev_parse_head, _is_ancestor, _git_name_only, _temp_index,
_secret_rename_origins, index_entry_count,
)
from session_state import load_state, with_locked_state
from extensibility import _same_dir
from gitutil import turn_commit_shas
from session_state import commit_log_files, get_commit_log_file
# =====================================================================
# TTL constants
# =====================================================================
# stop_hook_fire_count expires after this many seconds.
# The asyncRewake loop (vuln→exit(2)→fix→Stop again) is ~30-60s/cycle, so 120s
# comfortably contains MAX_STOP_HOOK_FIRINGS while letting the next user turn
# proceed unblocked. Replaces the UPS-reset that raced against background Stop.
STOP_LOOP_STATE_TTL_SEC = 120
# previous_findings expires independently. Dedup is content-based ((filePath,
# vulnerableCode) — see _record_fire), so a longer TTL suppresses exact-repeat
# re-flags across turns without masking regressions that change the code. v2's
# git-derived review set can re-surface the same uncommitted file across turns;
# 120s could let warnings pile up over a long session.
PREVIOUS_FINDINGS_TTL_SEC = int(os.environ.get("PREVIOUS_FINDINGS_TTL_SEC", "3600"))
# =====================================================================
# Git baseline + stop-state management
# =====================================================================
def save_baseline_sha(session_id, sha):
"""Save the git baseline SHA to state."""
def _save(state):
state["baseline_sha"] = sha
with_locked_state(session_id, _save)
def load_baseline_sha(session_id):
"""Load the git baseline SHA from state."""
def _load(state):
return state.get("baseline_sha")
return with_locked_state(session_id, _load)
def record_touched_path(session_id, file_path, custom_rule_stats=None):
"""Append a file path to the touched_paths list (deduped, capped at 200).
Stop is the consumer and clears under the same lock it reads with; UPS
no longer wipes. The cap is a defensive bound for sessions where Stop
never fires (disabled mid-session, abort) — git diff naturally filters
stale paths so over-retention is harmless, just wasteful.
"""
def _record(state):
# In the save this hook makes anyway: one more could drop another
# hook's change where there is no state lock (Windows).
_fold_custom_rule_stats(state, custom_rule_stats)
paths = state.setdefault("touched_paths", [])
if file_path not in paths:
paths.append(file_path)
if len(paths) > TOUCHED_PATHS_CAP:
del paths[:len(paths) - TOUCHED_PATHS_CAP]
with_locked_state(session_id, _record)
# Reported on the Stop row; see extensibility.edit_stats. The first three are
# kept as maximum or union over the turn's edits, the others are added up.
CUSTOM_RULE_KEYS = ("cr_rules", "cr_slow_ms", "cr_shapes", "cr_gain", "cr_loss")
def _fold_custom_rule_stats(state, stats):
if not stats:
return
kept = state.get("custom_rules")
if not isinstance(kept, dict):
kept = state["custom_rules"] = {}
for key in CUSTOM_RULE_KEYS:
new, old = stats.get(key, 0), kept.get(key, 0)
if not isinstance(old, int):
old = 0
if key == "cr_shapes":
value = old | new
elif key in ("cr_rules", "cr_slow_ms"):
value = max(old, new)
else:
value = old + new
if value:
kept[key] = value
def record_custom_rule_stats(session_id, stats):
"""Fold one edit's custom rule counts into the turn's. Telemetry only."""
with_locked_state(session_id, lambda state: _fold_custom_rule_stats(state, stats))
def consume_stop_state(session_id, clear=True):
"""Atomically snapshot all state the Stop hook needs and clear touched_paths.
The Stop hook is asyncRewake — it runs in the background after Claude's
turn ends. The user can submit a new prompt before this hook finishes its
initial state read. Telemetry showed a meaningful share of would-be reviews lost when
the next turn's UPS wiped touched_paths before Stop read it.
Single locked read-then-clear closes that window: PostToolUse appends
after this clear go into the next snapshot; UPS overwrites of baseline_sha
after this snapshot are invisible to this Stop fire.
"""
import time as _time
now = _time.time()
def _snap(state):
fire_ts = state.get("stop_hook_fire_count_ts", 0)
expired = (now - fire_ts) > STOP_LOOP_STATE_TTL_SEC
findings_ts = state.get("previous_findings_ts", fire_ts)
findings_expired = (now - findings_ts) > PREVIOUS_FINDINGS_TTL_SEC
snap = {
"touched_paths": list(state.get("touched_paths", [])),
"baseline_sha": state.get("baseline_sha"),
"head_at_capture": state.get("head_at_capture"),
# Where and when the prompt hook captured it; see baseline_origin.
"baseline_root": state.get("baseline_root"),
"prompt_at": state.get("prompt_at"),
"turn_started": state.get("turn_started"),
"baseline_kept": state.get("baseline_kept") or 0,
"untracked_at_baseline": (
dict(state["untracked_at_baseline"])
if isinstance(state.get("untracked_at_baseline"), dict) else {}
),
"fire_count": 0 if expired else state.get("stop_hook_fire_count", 0),
"fire_count_expired": expired and state.get("stop_hook_fire_count", 0) > 0,
"previous_findings": [] if findings_expired else list(state.get("previous_findings", [])),
"reviewed_diff_hash": state.get("reviewed_diff_hash"),
# State lost or overwritten since the last Stop (session_state).
"state_resets": state.get("state_resets", 0),
"state_overwrites": state.get("state_overwrites", 0),
"baseline_fail": state.get("baseline_fail") or 0,
"custom_rules": (
dict(state["custom_rules"])
if isinstance(state.get("custom_rules"), dict) else {}
),
}
# touched_paths is cleared per Stop, but one baseline can be
# reviewed by several Stops (rewakes). turn_touched keeps every path
# recorded since the prompt hook's baseline. Read by the scope
# metric and, through touched_since_prompt, by the no-edit-record note.
turn = list(dict.fromkeys(
[p for p in state.get("turn_touched", []) if isinstance(p, str)]
+ snap["touched_paths"]))[-TOUCHED_PATHS_CAP:]
snap["turn_touched"] = turn
if clear:
if snap["state_resets"] or snap["state_overwrites"]:
# turn_touched outlives this Stop, and so does its gap.
state["turn_touched_lost"] = True
state["turn_touched"] = turn
state["touched_paths"] = []
state.pop("reviewed_diff_hash", None)
state.pop("state_resets", None)
state.pop("state_overwrites", None)
state.pop("custom_rules", None)
return snap
return with_locked_state(session_id, _snap) or {
"touched_paths": [], "baseline_sha": None, "head_at_capture": None,
"untracked_at_baseline": {}, "baseline_root": None, "prompt_at": None,
"baseline_kept": 0, "turn_started": None,
"fire_count": 0, "fire_count_expired": False, "previous_findings": [],
"reviewed_diff_hash": None, "state_resets": 0, "state_overwrites": 0,
"baseline_fail": 0, "turn_touched": [], "custom_rules": {},
}
# record_touched_path and turn_touched keep the newest paths only.
TOUCHED_PATHS_CAP = 200
def touched_since_prompt(session_id):
"""(paths, complete): every path the session's edit tools recorded since
the prompt hook's baseline, read now and without consuming or writing
anything, so edits recorded while a Stop review runs are in it.
`complete` is False when the list is known to be missing paths: the
state was lost or overwritten since the prompt, it could not be read, or
the prompt hook's own save is not in it. Callers check the length
against TOUCHED_PATHS_CAP."""
try:
state = load_state(session_id)
except Exception:
return [], False
if not isinstance(state, dict):
return [], False
paths = list(dict.fromkeys(
[p for p in state.get("turn_touched", []) if isinstance(p, str)]
+ [p for p in state.get("touched_paths", []) if isinstance(p, str)]))
lost = (state.get("state_resets") or state.get("state_overwrites")
or state.get("turn_touched_lost"))
return paths, not lost and state.get("prompt_at") is not None
# Path names compare without case where the filesystem usually does.
_FOLD_CASE = os.name == "nt" or sys.platform == "darwin"
def _rel_in_repo(real_root, path):
"""`path` relative to the repo root (a real path), or None when it lies
outside the repo."""
real = os.path.realpath(path)
if sys.platform == "darwin":
# git and the file system can spell one name composed and decomposed.
real = unicodedata.normalize("NFC", real)
real_root = unicodedata.normalize("NFC", real_root)
if _FOLD_CASE:
# Fold before comparing: realpath keeps the case a path was typed in,
# so the repo folder itself can be spelled differently too.
real, real_root = real.casefold(), real_root.casefold()
try:
rel = os.path.relpath(real, real_root)
except ValueError: # another drive
return None
if rel == os.pardir or rel.startswith(os.pardir + os.sep):
return None
return rel
def touched_scope(repo_root, cwd, touched_paths):
"""Where the paths the session's edit tools wrote since the baseline lie:
(scope, repo-relative paths inside the repo). scope: 0 none recorded,
1 all inside the reviewed repo, 2 some outside, 3 all outside. Relative
paths are read against `cwd`. The list holds the newest 200 paths, so
this is approximate past that."""
root = os.path.realpath(repo_root)
inside, outside = set(), 0
for p in touched_paths:
rel = _rel_in_repo(root, os.path.join(cwd or root, p))
if rel is None:
outside += 1
else:
inside.add(rel)
if not touched_paths:
return 0, inside
if not outside:
return 1, inside
return (2 if inside else 3), inside
def review_scope_code(repo_root, scope, inside, files, base_fail):
"""One number, for telemetry only, saying how a Stop review relates to
what the session's edit tools wrote since the baseline:
base_fail * 100000 + scope * 10000 + min(untouched, 9999)
scope and inside: from touched_scope. untouched: how many of `files`
(repo-root-relative or absolute) no edit tool wrote. base_fail: 0 the
review diffed against a real turn-start snapshot, 1 and 2 see
last_baseline_fail, 3 there was no usable snapshot. Packed into one key
and sent even when 0, so a row without it was cut by the metrics key cap
or had no repo."""
root = os.path.realpath(repo_root)
untouched = sum(1 for f in files
if _rel_in_repo(root, os.path.join(root, f)) not in inside)
return int(base_fail or 0) * 100000 + scope * 10000 + min(untouched, 9999)
def findings_without_edit_record(session_id, at_stop_start, repo_root, cwd,
finding_paths, reviewed):
"""Which findings are in a reviewed file for which there is no record of
this session's edit tools writing it since the prompt: (those files as
the review named them, in order and without repeats; how many findings
they hold). The count is -1, with no files, when the record is known to
be incomplete: lost state, or more paths than the list keeps.
`at_stop_start`: turn_touched as the Stop began; a prompt that arrives
while the review runs resets the stored list. A finding path that is not
one of the `reviewed` files, or that cannot be resolved, is left out."""
since, complete = touched_since_prompt(session_id)
touched = list(dict.fromkeys(list(at_stop_start) + since))
if not complete or len(touched) >= TOUCHED_PATHS_CAP:
return [], -1
_, inside = touched_scope(repo_root, cwd, touched)
root = os.path.realpath(repo_root)
def rel(f):
try:
return _rel_in_repo(root, os.path.join(root, f))
except (ValueError, OSError): # NUL, or a name the OS cannot encode
return None
candidates = {}
for f in reviewed:
r = rel(f)
if r is not None and r not in inside:
candidates.setdefault(r, f)
names, count = [], 0
for f in finding_paths:
r = rel(f) if isinstance(f, str) and f else None
if r in candidates:
count += 1
if candidates[r] not in names:
names.append(candidates[r])
return names, count
def record_reviewed_diff(session_id, diff_hash):
def _save(state):
state["reviewed_diff_hash"] = diff_hash
with_locked_state(session_id, _save)
def restore_unreviewed_stop_state(session_id, paths, baseline_sha):
"""Put consumed touched_paths back so the next Stop reviews them.
consume_stop_state cleared touched_paths on disk; if Stop then exits
early for a transient reason (CCR API unreachable, Haiku HTTP error)
the next UPS would see an empty list, fall through the preservation
guard, and re-baseline past the unreviewed edits. Restoring keeps the
guard armed. Prepend+dedupe so any concurrent next-turn PostToolUse
appends survive.
"""
if not paths:
return
def _restore(state):
existing = state.get("touched_paths", [])
merged = list(dict.fromkeys(list(paths) + list(existing)))
if len(merged) > TOUCHED_PATHS_CAP:
merged = merged[:TOUCHED_PATHS_CAP]
state["touched_paths"] = merged
if baseline_sha and not state.get("baseline_sha"):
state["baseline_sha"] = baseline_sha
with_locked_state(session_id, _restore)
# state["baseline_kept"]: why a prompt left an earlier turn's baseline in
# place. Only telemetry reads it.
KEPT_NO_CAPTURE = 1 # it could not capture one (not a repo, or git failed)
KEPT_UNREVIEWED = 2 # the previous turn's edits were never reviewed
# baseline_origin() values that mean "captured in another checkout".
FOREIGN_ORIGINS = (1, 2, 3)
def baseline_origin(repo_root, baseline_sha, baseline_root):
"""Where the session's baseline was captured, relative to the checkout a
Stop review is about to read (`repo_root`):
0 that checkout (or the origin was not recorded)
1 another worktree of the same repository
2 another repository
3 another folder, which is gone or cannot be read as a checkout
4 there is no baseline
`baseline_root` is the repo root the prompt hook stored beside the SHA."""
if not baseline_sha:
return 4
if not baseline_root or not isinstance(baseline_root, str):
return 0
if _same_dir(baseline_root, repo_root):
return 0
if not os.path.isdir(baseline_root):
return 3
here, there = _git_dir(repo_root), _git_dir(baseline_root)
if not here or not there:
return 3
return 1 if _same_dir(here, there) else 2
def baseline_origin_metric(origin, kept):
"""The `bl_origin` number on Stop rows: baseline_origin, plus 10 or 20
(KEPT_*) when the baseline is an earlier turn's."""
if origin == 4 or kept not in (KEPT_NO_CAPTURE, KEPT_UNREVIEWED):
return origin
return origin + 10 * kept
def get_baseline_file_content(session_id, file_path, cwd):
"""Get the content of a file at the baseline SHA. Returns None if unavailable.
Decode the file content as UTF-8 with errors="replace" rather than using
text=True: source files in user repos can be latin-1 / cp1252 / shift-jis
/ etc., and on Windows text=True would decode via locale.getpreferredencoding()
in strict mode and raise UnicodeDecodeError in the subprocess reader
thread — leaving result.stdout=None and propagating AttributeError when
the caller tries to use it. Same class as the existing migrations at
security_reminder_hook.py:540 (reflog subjects) and :1115 (commit
diffs); this helper was missed in that pass. See
anthropics/claude-plugins-official#2056."""
baseline_sha = load_baseline_sha(session_id)
if not baseline_sha:
return None
try:
abs_path = os.path.abspath(file_path)
cwd_abs = os.path.abspath(cwd) if cwd else os.getcwd()
attempts = [(abs_path, cwd_abs, cwd)]
# The tool's path and the session directory can name the same place
# through different symlinks (/tmp and /var on macOS). The relative
# path between the two spellings then leaves the repo and git finds
# nothing, so the lookup is tried again with both resolved.
real_path, real_cwd = os.path.realpath(abs_path), os.path.realpath(cwd_abs)
if (real_path, real_cwd) != (abs_path, cwd_abs):
attempts.append((real_path, real_cwd, real_cwd))
for path, start, git_cwd in attempts:
try:
rel_path = os.path.relpath(path, start)
except ValueError:
continue
# `<rev>:<path>` is read from the repo root unless the path starts
# with `./`, and takes forward slashes only. Without both, the
# lookup fails for a session started below the repo root, and on
# Windows for any file that is not directly in the session
# directory.
rel_path = rel_path.replace(os.sep, "/")
result = subprocess.run(
[*GIT_CMD, "show", f"{baseline_sha}:./{rel_path}"],
cwd=git_cwd, capture_output=True, timeout=5
)
if result.returncode == 0:
return (result.stdout or b"").decode("utf-8", errors="replace")
return None
except (subprocess.TimeoutExpired, FileNotFoundError, OSError, ValueError):
return None
# How the last capture_git_baseline call got its SHA: 0 a real snapshot,
# 1 the snapshot failed and HEAD was returned, 2 no index copy and HEAD was
# returned. With 1 or 2 the next Stop review cannot tell the turn's changes
# from uncommitted work that was already there. Stored with baseline_sha.
# Telemetry only.
last_baseline_fail = 0
# Entries in the index the last capture copied, None if unknown. Telemetry only.
last_index_entries = None
# Who the snapshot commit is made by. Set on every commit-tree call, so the
# user's own name never appears on a commit they did not make and a repo with
# no identity configured still works. The commit object does land in
# `.git/objects`, but nothing refers to it, like the one `git stash create`
# used to make here: it is prunable from the moment it is written.
_SNAPSHOT_IDENT = {
"GIT_AUTHOR_NAME": "security-guidance", "GIT_AUTHOR_EMAIL": "security-guidance@localhost",
"GIT_COMMITTER_NAME": "security-guidance", "GIT_COMMITTER_EMAIL": "security-guidance@localhost",
}
# The whole snapshot, not one command: `stash create` was capped at 15 s and a
# per-command cap would let a slow filesystem hold the prompt for far longer.
_SNAPSHOT_BUDGET_S = 15
def _snapshot_commit(cwd, env, head_tree):
"""A commit of every tracked change, on the index copy `env` names.
Returns its SHA, "" when nothing tracked has changed (so the caller uses
HEAD, `head_tree` being HEAD's tree), or None when git refused or the
time budget ran out.
No `--missing-ok`: git counts a partial clone's promisor blobs as
present, so write-tree neither fetches nor fails on one, and the
existence check it would turn off is worth keeping.
"""
deadline = time.monotonic() + _SNAPSHOT_BUDGET_S
def run(args, extra=None):
left = deadline - time.monotonic()
if left <= 0:
raise subprocess.TimeoutExpired(args, _SNAPSHOT_BUDGET_S)
return subprocess.run(
[*GIT_CMD, *args], cwd=cwd, capture_output=True, timeout=left,
env={**env, **(extra or {})},
)
# No pathspec: `add -u` has staged the whole work tree, whatever directory
# of it `cwd` is, since git 2.0. `-- :/` would say the same but fails
# outright under a user's GIT_LITERAL_PATHSPECS.
add = run(["add", "-u"])
if add.returncode != 0:
debug_log(f"Snapshot: add -u failed: {_stderr_of(add)}")
return None
tree = run(["write-tree"])
sha = (tree.stdout or b"").decode("utf-8", errors="replace").strip()
if tree.returncode != 0 or not sha:
debug_log(f"Snapshot: write-tree failed: {_stderr_of(tree)}")
return None
if sha == head_tree:
return ""
if gitutil.last_dropped_gitlinks:
# A nested repository's entry is gone from the index copy, so the
# tree cannot equal HEAD's even on a clean work tree. Ask whether
# anything but those entries differs, rather than writing a commit
# object on every prompt in a repo with submodules.
same = run(["diff-tree", "--quiet", "--ignore-submodules=all", head_tree, sha])
if same.returncode == 0:
return ""
commit = run(["-c", "commit.gpgsign=false", "commit-tree", sha, "-p", "HEAD",
"-m", "security-guidance turn baseline"], _SNAPSHOT_IDENT)
out = (commit.stdout or b"").decode("utf-8", errors="replace").strip()
if commit.returncode != 0 or not out:
debug_log(f"Snapshot: commit-tree failed: {_stderr_of(commit)}")
return None
return out
def _stderr_of(result):
return (result.stderr or b"").decode("utf-8", errors="replace").strip()[:200]
def capture_git_baseline(cwd):
"""
Capture a git ref representing the current working tree state.
Builds a commit object for the current state (HEAD + uncommitted changes)
without touching the stash list or the working tree: `add -u` stages every
tracked change into a throwaway index copy, `write-tree` turns that into a
tree, and `commit-tree` hangs it off HEAD.
Falls back to HEAD if the working tree is clean.
Returns the SHA string, or None if not in a git repo or if the repo has no commits.
This was `git stash create`, which does the same thing in one command but
checks the work tree on a single thread, while `add -u` uses git's index
preload (one thread per 500 entries, up to 20). On a filesystem where each
file status is slow — a WSL repository on a Windows drive, a network mount
— that is the whole cost of the prompt: see #6854, where `stash create`
took 4 s on 1,424 files and never finished on 10,000.
The commands run on a throwaway copy of the index (GIT_INDEX_FILE), so
their `index.lock` lands next to the copy, not in `.git`: they cannot
collide with a concurrent git command, and a timeout kill cannot leave a
lock that blocks the user's next one. GIT_OPTIONAL_LOCKS does not cover
these. Copying the real index without a lock is safe: git replaces it by
atomic rename.
NOTE: the snapshot does NOT capture untracked files. UPS pairs this
SHA with a `_list_untracked()` snapshot stored as `untracked_at_baseline`,
and `compute_v2_review_set` subtracts that set so pre-existing untracked
files are not reviewed as Claude-authored. UPS takes no snapshot while the
Stop review, its only reader, is switched off.
"""
# stdout is a SHA so text=True is safe on stdout, but a non-ASCII
# filename in git's STDERR warning (e.g. a worktree
# with `Ávila_report.txt` triggers a quotePath/locale warning) would
# trip the stderr reader thread on Windows cp1252. Decode both streams
# leniently for symmetry with _list_untracked. See #2056.
global last_baseline_fail, last_index_entries
last_baseline_fail = 0
last_index_entries = None
try:
# HEAD's commit and its tree in one call: the commit is the answer
# for a clean tree, the tree is what the snapshot is compared with.
# A failure here means the repo has no commits — skip the review
# rather than create commits in the user's repo.
head_check = subprocess.run(
[*GIT_CMD, "rev-parse", "HEAD", "HEAD^{tree}"],
cwd=cwd, capture_output=True, timeout=5
)
head_out = (head_check.stdout or b"").decode("utf-8", errors="replace").split()
if head_check.returncode != 0 or len(head_out) != 2:
debug_log("No commits in repo, skipping baseline capture")
return None
head_sha, head_tree = head_out
sha = ""
# untracked_paths=[]: a plain copy of the index, no `add -N` pass.
with _temp_index(cwd, untracked_paths=[]) as env:
# env is None when no usable index copy could be made: fall
# back to HEAD.
if env is not None:
last_index_entries = index_entry_count(env["GIT_INDEX_FILE"])
sha = _snapshot_commit(cwd, env, head_tree)
if sha is None:
last_baseline_fail = 1
sha = ""
else:
last_baseline_fail = 2
# No snapshot: nothing tracked has changed, or it could not be taken.
return sha or head_sha
except (subprocess.TimeoutExpired, FileNotFoundError, OSError, ValueError) as e:
debug_log(f"Failed to capture git baseline: {e}")
return None
# ─── push-sweep reviewed-commit tracking ────────────────────────────────────
#
# Repo-local (not session-local) record of which commits the commit-review
# hook has already reviewed, so the push-sweep can advance its diff base past
# the contiguous reviewed prefix and skip entirely when everything pushed was
# already covered. Lives under `.git/` (same precedent as CC's
# `.git/claude-trailers`) so it survives across sessions and is per-clone.
#
# Format: one line per reviewed sha, append-only:
# <40-hex-sha>\t<unix-ts>\t<pv>\t<vulns_found>
#
# The trailing columns are observability only — load reads just the sha set.
# GC keeps the last _REVIEWED_SHAS_CAP entries; the file is small (~64 bytes
# per line) so even at the cap it's ~32KB.
# =====================================================================
# Reviewed-SHA log (commit/push dedup)
# =====================================================================
# ─── push-sweep reviewed-commit tracking ────────────────────────────────────
#
# Repo-local (not session-local) record of which commits the commit-review
# hook has already reviewed, so the push-sweep can advance its diff base past
# the contiguous reviewed prefix and skip entirely when everything pushed was
# already covered. Lives under `.git/` (same precedent as CC's
# `.git/claude-trailers`) so it survives across sessions and is per-clone.
#
# Format: one line per reviewed sha, append-only:
# <40-hex-sha>\t<unix-ts>\t<pv>\t<vulns_found>
#
# The trailing columns are observability only — load reads just the sha set.
# GC keeps the last _REVIEWED_SHAS_CAP entries; the file is small (~64 bytes
# per line) so even at the cap it's ~32KB.
_REVIEWED_SHAS_BASENAME = "sg-reviewed-shas"
_REVIEWED_SHAS_CAP = 500
def _is_full_sha(sha):
return len(sha) == 40 and all(c in "0123456789abcdef" for c in sha)
def _reviewed_shas_path(repo_root):
gd = _git_dir(repo_root)
return os.path.join(gd, _REVIEWED_SHAS_BASENAME) if gd else None
def _load_reviewed_shas(repo_root, _gd=None):
"""Set of full 40-hex shas previously reviewed in this clone."""
p = (os.path.join(_gd, _REVIEWED_SHAS_BASENAME) if _gd
else _reviewed_shas_path(repo_root))
if not p or not os.path.exists(p):
return set()
out = set()
try:
with open(p, "r") as f:
for line in f:
sha = line.split("\t", 1)[0].strip()
if _is_full_sha(sha):
out.add(sha)
except OSError:
pass
return out
def _append_reviewed_shas(repo_root, shas, vulns_found=0):
"""Record that `shas` were reviewed. Best-effort; never raises.
Uses fcntl.flock for the read-gc-write; appends are O_APPEND-atomic but
GC needs the lock so concurrent CC sessions in the same clone don't race
each other's truncation.
"""
p = _reviewed_shas_path(repo_root)
if not p or not shas:
return
import time as _time
ts = int(_time.time())
pv = _PV or 0
lines = [f"{s}\t{ts}\t{pv}\t{int(vulns_found)}\n" for s in shas]
try:
import fcntl
with open(p, "a+") as f:
fcntl.flock(f.fileno(), fcntl.LOCK_EX)
try:
f.seek(0)
existing = f.read().splitlines(keepends=True)
# Dedup by sha (first column) — keep newest, then cap.
seen = set()
merged = []
for ln in (existing + lines)[::-1]:
sha = ln.split("\t", 1)[0].strip()
if sha and sha not in seen:
seen.add(sha)
merged.append(ln if ln.endswith("\n") else ln + "\n")
merged = merged[:_REVIEWED_SHAS_CAP][::-1]
f.seek(0)
f.truncate()
f.writelines(merged)
finally:
fcntl.flock(f.fileno(), fcntl.LOCK_UN)
except (OSError, ImportError):
# fcntl unavailable (Windows) or write failed — degrade to plain
# append; cap enforcement happens on the next locked write.
try:
with open(p, "a") as f:
f.writelines(lines)
except OSError:
pass
# ─── commits of a turn, count only ──────────────────────────────────────────
#
# A commit review starts from a hook on the Bash tool. A commit made another
# way (the PowerShell tool, a script, the user's own terminal) starts none.
# The Stop row says how many commits a turn made and how many of them no
# commit hook saw. Nothing reads these to decide whether a review runs.
#
# Kept in a file of its own per session, append-only: saving the session state
# from one more background hook could drop another hook's change where there
# is no state lock (Windows). Lines: `seen <sha prefix>` from commit hooks,
# `counted <sha>` from the Stop hook. Every session's are read, so a commit
# is seen by any of them and counted once on this machine.
TURN_COMMITS_ERROR = -1
def _note_commits(session_id, kind, shas):
"""Never raises."""
if not shas or portable_output():
return
try:
path = get_commit_log_file(session_id)
make_state_dir(os.path.dirname(path))
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_APPEND
| getattr(os, "O_NOFOLLOW", 0), 0o600)
with os.fdopen(fd, "a") as f:
f.write("".join(f"{kind} {sha}\n" for sha in shas))
except Exception as e:
debug_log(f"{kind} commits not noted: {e}")
def note_seen_commits(session_id, shas):
"""A commit hook saw these commits."""
_note_commits(session_id, "seen", shas)
def note_counted_commits(session_id, shas):
"""These commits went out in a Stop row."""
_note_commits(session_id, "counted", shas)
def turn_commits_code(repo_root, prompt_at, until, head_before=None):
"""(code, shas) for the commits made in this checkout from the prompt to
`until` that no Stop row has counted yet:
made * 10000 + no_review * 100 + unseen (each capped at 99)
no_review: no finished or running review in this clone, for whatever
reason. unseen: of those, the ones no commit hook saw. The code is None
when nothing was committed or the start of the turn is not known,
TURN_COMMITS_ERROR when git could not say."""
if not isinstance(prompt_at, (int, float)) or not 0 < prompt_at < 1e11:
return None, []
shas = turn_commit_shas(repo_root, prompt_at, until, head_before)
if not shas:
return (TURN_COMMITS_ERROR if shas is None else None), []
noted = {"seen": set(), "counted": set()}
for path in commit_log_files(newer_than=prompt_at):
try:
with open(path, encoding="utf-8", errors="replace") as f:
for line in f:
kind, _, value = line.strip().partition(" ")
if value and kind in noted:
noted[kind].add(value)
except OSError:
return TURN_COMMITS_ERROR, []
new = [s for s in shas if s not in noted["counted"]]
if not new:
return None, []
gd = _git_dir(repo_root)
if not gd:
return TURN_COMMITS_ERROR, []
# The marker file, not its lock: taking the lock for a moment could make
# the review that is about to claim the commit leave it alone. Markers
# first: a review that ends in between then reads as finished.
missed = [s for s in new
if not os.path.exists(os.path.join(gd, _INFLIGHT_PREFIX + s))]
reviewed = _load_reviewed_shas(repo_root, _gd=gd)
missed = [s for s in missed if s not in reviewed]
seen = tuple(noted["seen"])
unseen = sum(1 for s in missed if not s.startswith(seen))
code = min(len(new), 99) * 10000 + min(len(missed), 99) * 100 + min(unseen, 99)
return code, new
# ─── repeated-diff count ────────────────────────────────────────────────────
#
# Count only. A change that comes back under a new commit (undo and re-commit,
# rebase before the first push) is unknown to the reviewed-shas record and is
# reviewed again. These helpers say how often that happens, by remembering a
# hash of each reviewed hunk next to the reviewed-shas file. Nothing here
# decides whether a review runs, and nothing here waits on another process
# for more than a moment.
_SEEN_DIFFS_BASENAME = "sg-reviewed-diffs"
_SEEN_DIFFS_TTL_S = 7 * 24 * 3600
# Past this size the record is cut back to its newest half.
_SEEN_DIFFS_TRIM_BYTES = 1024 * 1024
_SEEN_DIFFS_MAX_KEYS = 500
_SEEN_DIFFS_LOCK_TRIES = 10
# `diff_seen` values, weakest first. A review reports the weakest of its
# hunks: 3 means every hunk already had a finished review in this clone.
DIFF_SEEN_ERROR = -1 # the record could not be read or written
DIFF_SEEN_NO = 0 # some hunk was never reviewed
DIFF_SEEN_UNFINISHED = 1 # a review of it started and has no verdict (yet)
DIFF_SEEN_OTHER_TREE = 2 # finished, on different repo contents
DIFF_SEEN_SAME_TREE = 3 # finished, on the same tree
def _hunk_keys(diff_files):
"""One hash per hunk: the path plus the hunk's lines, context included.
The `@@` line is left out, so a hunk keeps its key when a rebase only
moves it; the same edit next to different code gets a different key.
"""
keys = {}
for path, content in diff_files:
hunks, cur = [], []
for ln in content.split("\n"):
if ln.startswith("@@"):
hunks.append(cur)
cur = []
else:
cur.append(ln)
hunks.append(cur)
for body in hunks:
# A file's last hunk ends with the diff's trailing blank line.
while body and not body[-1]:
body.pop()
if not body:
continue
h = hashlib.sha256(path.encode("utf-8", "replace"))
h.update(b"\0")
h.update("\n".join(body).encode("utf-8", "replace"))
keys[h.hexdigest()[:16]] = None
keys = list(keys)
if len(keys) > _SEEN_DIFFS_MAX_KEYS:
# Keep the record small: the hunks past the cap share one key, so
# they only match the same set of hunks again.
rest = hashlib.sha256(" ".join(keys[_SEEN_DIFFS_MAX_KEYS - 1:]).encode("ascii"))
keys = keys[:_SEEN_DIFFS_MAX_KEYS - 1] + [rest.hexdigest()[:16]]
return keys
def _seen_lock(fd):
"""True: locked. False: another process kept it. None: no flock here."""
for _ in range(_SEEN_DIFFS_LOCK_TRIES):
try:
if _flock_nb(fd, exclusive=True):
return True
except (ImportError, OSError):
return None
time.sleep(0.05)
return False
def _append_diff_row(f, locked, token, done):
"""Append one record for the review to `f`, the record opened "a+b"."""
_, keys, tree = token
row = f"{int(time.time())}\t{tree}\t{int(done)}\t{' '.join(keys)}\n".encode("ascii")
size = f.seek(0, os.SEEK_END)
if size:
f.seek(size - 1)
if f.read(1) != b"\n":
row = b"\n" + row # a torn last record must not swallow this one
# Cutting the record back rewrites it: not while another process holds
# the lock. Without flock at all, bounded beats exact.
if locked is not False and size + len(row) > _SEEN_DIFFS_TRIM_BYTES:
f.seek(0)
keep, room = [], _SEEN_DIFFS_TRIM_BYTES // 2
for ln in reversed(f.read().splitlines(keepends=True)):
room -= len(ln)
if room < 0:
break
keep.append(ln)
f.seek(0)
f.truncate()
row = b"".join(reversed(keep)) + row
f.write(row)
f.flush()
def _diff_review_started(repo_root, diff_files, tip="HEAD"):
"""Note that a review of `diff_files`, a diff ending at `tip`, is starting.
Returns (token, metrics). `metrics` is {"diff_seen": <DIFF_SEEN_* code>}:
whether this clone already reviewed every hunk of this diff. `token` goes
to `_diff_review_done` once the review has a verdict. Never raises.
"""
if portable_output() or not diff_files:
# No metrics leave the hook in portable mode, so nothing to count.
return None, {}
try:
gd = _git_dir(repo_root)
keys = _hunk_keys(diff_files)
if not gd or not keys:
raise ValueError("no git dir" if not gd else "no hunks")
token = (os.path.join(gd, _SEEN_DIFFS_BASENAME), keys,
"".join(_full_shas(repo_root, [tip + "^{tree}"])[:1]))
tree = token[2]
now = time.time()
best = dict.fromkeys(keys, DIFF_SEEN_NO)
# Read and note under one lock, so two reviews of one diff that start
# together cannot both miss each other.
with open(token[0], "a+b") as f:
locked = _seen_lock(f.fileno())
if locked is False:
raise OSError("record is locked")
f.seek(0)
for ln in f.read().decode("ascii", "replace").splitlines():
row = ln.split("\t")
if len(row) != 4 or not row[0].isdigit() or len(row[0]) > 12:
continue
if now - int(row[0]) > _SEEN_DIFFS_TTL_S:
continue
if row[2] != "1":
seen = DIFF_SEEN_UNFINISHED
elif tree and row[1] == tree:
seen = DIFF_SEEN_SAME_TREE
else:
seen = DIFF_SEEN_OTHER_TREE
for k in row[3].split(" "):
if best.get(k, seen) < seen:
best[k] = seen
_append_diff_row(f, locked, token, done=False)
return token, {"diff_seen": min(best.values())}
except Exception as e:
debug_log(f"Repeated-diff count: not counted ({type(e).__name__}: {e})")
return None, {"diff_seen": DIFF_SEEN_ERROR}
def _diff_review_done(token):
"""Note that the review `token` came from got a verdict. Never raises."""
if not token:
return
try:
with open(token[0], "a+b") as f:
_append_diff_row(f, _seen_lock(f.fileno()), token, done=True)
except Exception as e:
debug_log(f"Repeated-diff count: verdict not noted ({type(e).__name__}: {e})")
# ─── in-flight review markers ───────────────────────────────────────────────
#
# A commit or push review records its shas only once it has a verdict, which
# is minutes after it starts. A review that starts in between (the push right
# after a commit, the next commit's reflog scan) would send the same commits
# through a second full review. So a review first takes an exclusive flock on
# `sg-inflight-<sha>`, next to the reviewed-shas file, and holds it until its
# process ends; other reviews leave a locked commit to it. The kernel drops
# the lock however the holder dies, so a killed review blocks nobody. Native
# Windows Python has no flock; there the same lock is taken with LockFileEx.
# Where files cannot be locked (some network filesystems) there are no
# markers and nothing changes.
_INFLIGHT_PREFIX = "sg-inflight-"
# A marker file nobody holds (a killed review's) is removed once this old.
_INFLIGHT_GC_AGE_S = 3600
_inflight_held = {}
# Windows refuses to delete a file that is open, so a marker is closed first
# there. Elsewhere it is deleted while still locked, so nobody can lock a
# file that is about to go.
_UNLINK_BEFORE_CLOSE = os.name != "nt"
def _full_shas(repo_root, shas):
"""Resolve abbreviated commit refs to full 40-hex shas; drops unresolved."""
out = []
for s in shas:
try:
r = subprocess.run(
[*GIT_CMD, "rev-parse", "--verify", "-q", s],
cwd=repo_root, capture_output=True, timeout=5,
)
except (subprocess.TimeoutExpired, FileNotFoundError, OSError):
continue
if r.returncode == 0:
out.append(r.stdout.decode("utf-8", errors="replace").strip())
return out
_win_lock_file_ex = None
def _win_lock_nb(fd, exclusive):
"""`_flock_nb` for native Windows Python, with LockFileEx.
Shared and exclusive as with flock, never waits, and Windows drops the
lock when the handle is closed or the holder's process ends. Windows
locks stop other processes reading the locked bytes, so the byte locked
is one far past the end of the (empty) marker: copying a repository while
a review runs still reads the marker.
"""
global _win_lock_file_ex
import errno
try:
import ctypes
import msvcrt
if _win_lock_file_ex is None:
from ctypes import wintypes
class _Overlapped(ctypes.Structure):
_fields_ = [("Internal", ctypes.c_void_p), ("InternalHigh", ctypes.c_void_p),
("Offset", wintypes.DWORD), ("OffsetHigh", wintypes.DWORD),
("hEvent", wintypes.HANDLE)]
fn = ctypes.WinDLL("kernel32", use_last_error=True).LockFileEx
fn.argtypes = [wintypes.HANDLE, wintypes.DWORD, wintypes.DWORD, wintypes.DWORD,
wintypes.DWORD, ctypes.POINTER(_Overlapped)]
fn.restype = wintypes.BOOL
_win_lock_file_ex = (fn, _Overlapped)
except (ImportError, AttributeError, OSError) as e:
raise OSError(errno.ENOLCK, f"no file locking in this Python ({e})")
fn, _Overlapped = _win_lock_file_ex
fail_immediately, exclusive_lock = 0x1, 0x2
where = _Overlapped(Offset=0, OffsetHigh=0x7FFFFFFF)
if fn(msvcrt.get_osfhandle(fd), fail_immediately | (exclusive_lock if exclusive else 0),
0, 1, 0, ctypes.byref(where)):
return True
err = ctypes.get_last_error()
if err == 33: # ERROR_LOCK_VIOLATION: another handle holds it
return False
raise OSError(errno.ENOLCK, f"LockFileEx failed (WinError {err})")
def _flock_nb(fd, exclusive):
"""True when the lock was taken, False when another process holds it.
Raises OSError when the file cannot be locked at all."""
import errno
if os.name == "nt":
return _win_lock_nb(fd, exclusive)
try:
import fcntl
except ImportError as e:
raise OSError(errno.ENOLCK, f"no file locking in this Python ({e})")
try:
fcntl.flock(fd, (fcntl.LOCK_EX if exclusive else fcntl.LOCK_SH) | fcntl.LOCK_NB)
return True
except OSError as e:
if e.errno in (errno.EWOULDBLOCK, errno.EAGAIN):
return False
raise
def _again_if_refused(op, *args):
"""`op(*args)`, tried a few times where another process having the file
open for a moment refuses it (Windows). Raises the last OSError."""
import time as _time
tries = 1 if _UNLINK_BEFORE_CLOSE else 3
for attempt in range(tries):
try:
return op(*args)
except PermissionError:
if attempt == tries - 1:
raise
_time.sleep(0.01)
def _drop_marker(p, fd, unlink=True):
"""Close a marker this process has open and, with `unlink`, delete it, in
the order this platform needs. Never raises. A delete that stays refused
leaves an unlocked file, which is swept once old."""
steps = [(_again_if_refused, os.unlink, p)] if unlink else []
steps.insert(len(steps) if _UNLINK_BEFORE_CLOSE else 0, (os.close, fd))
for step, *args in steps:
try:
step(*args)
except OSError:
pass
def _release_inflight():
for p, fd in list(_inflight_held.items()):
_drop_marker(p, fd)
_inflight_held.clear()
def _sweep_inflight(gd):
"""Remove old marker files no review holds."""
import time as _time
cutoff = _time.time() - _INFLIGHT_GC_AGE_S
for name in os.listdir(gd):
p = os.path.join(gd, name)
if not name.startswith(_INFLIGHT_PREFIX) or p in _inflight_held:
continue
try:
if os.path.getmtime(p) > cutoff:
continue
fd = os.open(p, os.O_RDWR)
except OSError:
continue
try:
unheld = _flock_nb(fd, exclusive=True)
except OSError:
unheld = False
_drop_marker(p, fd, unlink=unheld)
def _mark_inflight(repo_root, shas):
"""Claim `shas` (full 40-hex) for the review this process is about to
run, until the process ends. Best-effort; never raises. A commit another
live review holds stays with that review."""
try:
gd = _git_dir(repo_root)
if not gd:
return
_sweep_inflight(gd)
for sha in shas:
p = os.path.join(gd, _INFLIGHT_PREFIX + sha)
if not _is_full_sha(sha) or p in _inflight_held:
continue
try:
fd = _again_if_refused(os.open, p, os.O_RDWR | os.O_CREAT, 0o600)
except OSError:
# One marker that cannot be opened does not cost the others
# their claim.
continue
try:
held = _flock_nb(fd, exclusive=True)
except OSError:
# Cannot lock here at all: leave no file that reads as held.
_drop_marker(p, fd)
return
if held and _UNLINK_BEFORE_CLOSE:
# The inode check: a holder unlinks its marker when it ends,
# and a lock on that unlinked file would claim nothing. Not
# where an open file cannot be unlinked in the first place.
try:
held = os.fstat(fd).st_ino == os.stat(p).st_ino
except OSError:
held = False
if not held:
os.close(fd)
continue
if not _inflight_held:
import atexit
atexit.register(_release_inflight)
_inflight_held[p] = fd
os.utime(p)
except Exception:
pass
def _load_inflight_shas(repo_root, shas, _gd=None):
"""Which of `shas` another live review in this clone holds right now."""
out = set()
gd = _gd or _git_dir(repo_root)
if not gd:
return out
for sha in shas:
p = os.path.join(gd, _INFLIGHT_PREFIX + sha)
if not _is_full_sha(sha) or p in _inflight_held:
continue
try:
fd = os.open(p, os.O_RDONLY)
except OSError:
continue
try:
# Shared, so two reviews asking at once do not look like a
# holder to each other.
if not _flock_nb(fd, exclusive=False):
out.add(sha)
except OSError:
pass
os.close(fd)
return out
def _reviewed_after_inflight(repo_root, push_range, max_wait_s, poll_s=1.0):
"""The reviewed-sha set for a push of `push_range` (oldest first), read
once no other review is still running on the commit the push review
would start from, or after `max_wait_s`. Returns (reviewed, waited_s)."""
import time as _time
gd = _git_dir(repo_root)
start = _time.monotonic()
waited = False
reviewed = _load_reviewed_shas(repo_root, _gd=gd)
while gd:
# Only the first unreviewed commit matters: if nobody is reviewing
# it, the push review covers everything from there on anyway.
first = next((s for s in push_range if s not in reviewed), None)
if first is None:
break
# In-flight before reviewed: a review that ends between the two
# reads then shows up as reviewed rather than as neither.
busy = _load_inflight_shas(repo_root, [first], _gd=gd)
reviewed = _load_reviewed_shas(repo_root, _gd=gd)
if first in reviewed:
continue
if not busy or _time.monotonic() - start >= max_wait_s:
break
waited = True
_time.sleep(poll_s)
return reviewed, (_time.monotonic() - start) if waited else 0.0
# =====================================================================
# v2 review-set computation (Stop hook)
# =====================================================================
UNTRACKED_BASELINE_CAP = 2000
# Set in a snapshot that hit the cap, to the time the listing started. NUL
# cannot occur in a path, so the key never collides with a file.
UNTRACKED_CAPPED_AT_KEY = "\0capped_at_ns"
# Coarse filesystem timestamps (1 s on ext3, 2 s on FAT) can stamp a file
# created just after the snapshot as slightly older than it.
_CAPPED_BASELINE_SLACK_NS = 2_000_000_000
def _list_untracked(cwd):
"""Repo-root-relative untracked (and not-ignored) path → mtime_ns, or {}
on error. Used at UPS to snapshot the pre-turn untracked set so the Stop
hook can exclude unchanged pre-existing untracked files from review.
mtime is captured so an in-place edit during the turn is still reviewed.
Uses ls-files (not status) for the UPS path: the index diff isn't needed,
and ls-files --others only walks the worktree against .gitignore.
Decodes stdout/stderr as UTF-8 with errors="replace" instead of using
text=True. With core.quotePath=false git emits raw UTF-8 bytes for
non-ASCII filenames; text=True decodes via locale.getpreferredencoding()
in strict mode — on Windows that's cp1252 with several undefined bytes
(0x81/0x8D/0x8F/0x90/0x9D), all of which appear in UTF-8 encodings of
common accented capitals (Á Í Ï Ð Ý) and most CJK/emoji codepoints.
A non-ASCII filename in the worktree crashed the subprocess reader
thread, left r.stdout=None, and propagated AttributeError out of the
helper — silently losing the baseline snapshot every UserPromptSubmit.
See anthropics/claude-plugins-official#2056. The sibling helpers in
gitutil.py already follow the lenient pattern; this function and
capture_git_baseline / _git_name_only / _git_status_porcelain were
the holdouts."""
try:
repo = _git_toplevel(cwd) or cwd
started_ns = time.time_ns()
# core.quotePath=false comes from GIT_CMD globally (see gitutil.py).
r = subprocess.run(
[*GIT_CMD, "ls-files", "--others", "--exclude-standard", "-z"],
cwd=repo, capture_output=True, timeout=15,
)
if r.returncode != 0:
stderr_str = (r.stderr or b"").decode("utf-8", errors="replace")
debug_log(f"_list_untracked rc={r.returncode}: {stderr_str[:200]}")
return {}
stdout = (r.stdout or b"").decode("utf-8", errors="replace")
out = {}
for p in stdout.split("\0"):
if not p:
continue
try:
out[p] = os.stat(os.path.join(repo, p)).st_mtime_ns
except OSError:
out[p] = 0
if len(out) >= UNTRACKED_BASELINE_CAP:
debug_log(f"_list_untracked: capped at {UNTRACKED_BASELINE_CAP}")
# The files past the cap are not recorded one by one. Record
# when the snapshot was taken so the Stop hook can still tell
# them from files created or edited during the turn.
out[UNTRACKED_CAPPED_AT_KEY] = started_ns
break
return out
except (subprocess.TimeoutExpired, FileNotFoundError, OSError, ValueError) as e:
# ValueError guards against any future strict-decode regression
# so the helper degrades to {} instead of crashing the hook.
debug_log(f"_list_untracked error: {e}")
return {}
def compute_v2_review_set(cwd, baseline_sha, head_at_capture, untracked_at_baseline=None,
since_ns=None):
"""v2 diff strategy: derive the review set from git state alone.
review_set = (files dirty vs current HEAD, plus files committed this turn
when HEAD advanced linearly) ∩ (files whose content differs from the
pre-turn baseline). The first term is immune to checkout/pull
ballooning; the second filters out the user's untouched pre-turn WIP.
Falls back to dirty_now alone when no baseline is available.
untracked_at_baseline: {repo-root-relative path: mtime_ns} captured at
UPS. The baseline snapshot doesn't include untracked files, so without this
snapshot a pre-existing untracked file looks "new since baseline" forever.
A file is excluded only if it was untracked at baseline AND its mtime is
unchanged — an in-place edit during the turn is still reviewed.
since_ns: for a checkout the prompt hook took no baseline or snapshot of
(the turn moved into it), the time the turn began. A dirty or untracked
file last written before then was already that way and is left out.
Known limitation: a Bash-only turn that's interrupted before Stop fires
leaves touched_paths empty, so the next UPS re-baselines past those edits.
v1 never reviews Bash-only turns at all, so v2 is no worse there.
Returns (absolute paths sorted, diff_base, repo_root, metrics).
diff_base is "HEAD" unless HEAD advanced linearly this turn (commits),
in which case it's head_at_capture so committed files produce a diff.
repo_root is the git toplevel — `git diff --name-only` outputs paths
relative to it (not to cwd), so the caller's get_git_diff must run
from there too or pathspecs won't match.
Also returns the untracked subset of review_set so get_git_diff can do
a targeted `add -N -- <files>` instead of a whole-tree scan.
"""
repo = _git_toplevel(cwd) or cwd
if not isinstance(untracked_at_baseline, dict):
untracked_at_baseline = {}
tracked_dirty, untracked = _git_status_porcelain(repo)
if tracked_dirty is None:
return [], "HEAD", repo, [], {"dirty_now_count": -1, "changed_since_count": -1, "review_set_count": 0}
capped_at_ns = untracked_at_baseline.get(UNTRACKED_CAPPED_AT_KEY)
def _unchanged_since_baseline(p):
base_mtime = untracked_at_baseline.get(p)
try:
st = os.stat(os.path.join(repo, p))
except OSError:
return False
if base_mtime is not None:
return st.st_mtime_ns == base_mtime
if not capped_at_ns:
return False
# The snapshot stopped at the cap, so this file may simply be one it
# never reached. Without this, every pre-existing file past the cap
# looks new on every turn and the same files are reviewed again and
# again. A file created or edited during the turn has a later mtime
# or ctime than the snapshot; anything older was already there.
return max(st.st_mtime_ns, st.st_ctime_ns) < capped_at_ns - _CAPPED_BASELINE_SLACK_NS
preexisting_unchanged = {p for p in untracked if _unchanged_since_baseline(p)}
if since_ns:
def _written_before_turn(p):
try:
st = os.stat(os.path.join(repo, p))
except OSError:
return False
return max(st.st_mtime_ns, st.st_ctime_ns) < since_ns - _CAPPED_BASELINE_SLACK_NS
preexisting_unchanged |= {p for p in untracked if _written_before_turn(p)}
tracked_dirty = {p for p in tracked_dirty if not _written_before_turn(p)}
new_untracked = untracked - preexisting_unchanged
dirty_now = tracked_dirty | new_untracked
diff_base = "HEAD"
current_head = _git_rev_parse_head(repo)
if (head_at_capture and current_head and head_at_capture != current_head
and _is_ancestor(repo, head_at_capture, current_head)):
dirty_now |= _git_name_only(repo, f"{head_at_capture}..HEAD") or set()
diff_base = head_at_capture
# changed_since: tracked files vs the turn baseline (no `add -N` — the
# baseline never contained untracked files anyway), then union with
# currently-untracked. The previous `include_untracked=True` arm cost a
# full `git add -N .` (slow in large repos) per call to surface
# untracked files in the diff output — but `git diff <baseline>` already
# lists them as "only in worktree" without that, and we have the explicit
# set from status regardless.
if baseline_sha:
changed_since = _git_name_only(repo, baseline_sha)
if changed_since is not None:
changed_since |= new_untracked
else:
changed_since = None
# changed_since is None on missing baseline OR on git error (e.g. the
# dangling baseline SHA was pruned). Either way, don't intersect with ∅ —
# that would silently zero the review set. Fall back to dirty_now.
review_set = (dirty_now & changed_since) if changed_since is not None else dirty_now
# Kept beside its new name so that the content diff pairs the two.
secret_origins = {
old for new, old in _secret_rename_origins.items() if new in review_set}
review_set = review_set | secret_origins
review_paths = [os.path.join(repo, p) for p in sorted(review_set)]
untracked_in_review = sorted(new_untracked & review_set)
metrics = {
"dirty_now_count": len(dirty_now),
"changed_since_count": len(changed_since) if changed_since is not None else -1,
"review_set_count": len(review_set),
}
# Only emit when nonzero to stay under the 10-key telemetry cap.
if preexisting_unchanged:
metrics["preexisting_untracked_excluded"] = len(preexisting_unchanged)
if secret_origins:
metrics["secret_renamed"] = len(secret_origins)
return review_paths, diff_base, repo, untracked_in_review, metrics
|