diff options
| author | Mohamed Hegazy <mhegazy@anthropic.com> | 2026-11-07 02:01:32 +0000 |
|---|---|---|
| committer | Claude <noreply@anthropic.com> | 2026-11-07 02:01:32 +0000 |
| commit | a8fa1532035ddf875c8594dd4407cbe38ffbe99a (patch) | |
| tree | 2694d4dd1767f07c9938fc687b1a0c4593eaa6d4 /plugins/security-guidance/hooks | |
| parent | aa5665a817bc2a48d48084c4192a083995aa024d (diff) | |
security-guidance: keep an incomplete edit record incomplete for the whole turnmhegazy/sg-stop-finding-names-unwritten
A record lost before one Stop was treated as whole by the next, and a prompt
that was never stored as having no edits, so the note could name the
session's own file. File names in the note are quoted as JSON.
Co-authored-by: mhegazy <mhegazy@anthropic.com>
Diffstat (limited to 'plugins/security-guidance/hooks')
| -rw-r--r-- | plugins/security-guidance/hooks/diffstate.py | 21 | ||||
| -rwxr-xr-x | plugins/security-guidance/hooks/security_reminder_hook.py | 10 |
2 files changed, 19 insertions, 12 deletions
diff --git a/plugins/security-guidance/hooks/diffstate.py b/plugins/security-guidance/hooks/diffstate.py index 612932ed..9ca56774 100644 --- a/plugins/security-guidance/hooks/diffstate.py +++ b/plugins/security-guidance/hooks/diffstate.py @@ -61,10 +61,6 @@ def load_baseline_sha(session_id): return with_locked_state(session_id, _load) -# record_touched_path and turn_touched keep the newest paths only. -TOUCHED_PATHS_CAP = 200 - - def record_touched_path(session_id, file_path): """Append a file path to the touched_paths list (deduped, capped at 200). @@ -132,6 +128,9 @@ def consume_stop_state(session_id, clear=True): + 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) @@ -149,13 +148,18 @@ def consume_stop_state(session_id, clear=True): } +# 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 file was lost or overwritten since the last Stop, or it could not - be read. Callers check the length against TOUCHED_PATHS_CAP.""" + 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: @@ -165,8 +169,9 @@ def touched_since_prompt(session_id): 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") - return paths, not lost + 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. diff --git a/plugins/security-guidance/hooks/security_reminder_hook.py b/plugins/security-guidance/hooks/security_reminder_hook.py index 824c22d5..44208fb3 100755 --- a/plugins/security-guidance/hooks/security_reminder_hook.py +++ b/plugins/security-guidance/hooks/security_reminder_hook.py @@ -240,10 +240,11 @@ NO_EDIT_RECORD_NOTE_MAX_FILES = 10 def _no_edit_record_note(names): - """The note for `names` (from findings_without_edit_record), or "".""" + """The note for `names` (from findings_without_edit_record), or "". + Quoted as JSON: a file name can hold a line break.""" if not names: return "" - shown = ", ".join(f"`{n}`" for n in names[:NO_EDIT_RECORD_NOTE_MAX_FILES]) + shown = ", ".join(json.dumps(n) for n in names[:NO_EDIT_RECORD_NOTE_MAX_FILES]) if len(names) > NO_EDIT_RECORD_NOTE_MAX_FILES: shown += (f" and {len(names) - NO_EDIT_RECORD_NOTE_MAX_FILES} more of " "the files above") @@ -883,6 +884,7 @@ def handle_user_prompt_submit(input_data): state["baseline_sha"] = sha state["head_at_capture"] = head state.pop("turn_touched", None) + state.pop("turn_touched_lost", None) _store_baseline_fail(state, baseline_fail) # A Stop review that ends up in another checkout does not use # this baseline. @@ -2982,8 +2984,8 @@ def handle_stop_hook(input_data): [fp for fp, _ in diff_files]) no_record_note = _no_edit_record_note(names) # Findings in such files; -1 when the record was incomplete - # and no note was added. Sent even when 0, as the last key - # of the row, so a row without it was cut by the key cap. + # and no note was added. Sent even when 0 and after the review + # keys, so a row without it was cut by the key cap. _TAIL_METRICS["finding_no_edit_record"] = n_no_record except Exception as e: debug_log(f"no-edit-record note skipped: {e}") |
