summaryrefslogtreecommitdiff
path: root/plugins/security-guidance/hooks/diffstate.py
diff options
context:
space:
mode:
authorMohamed Hegazy <mhegazy@anthropic.com>2026-10-14 16:31:17 +0000
committerClaude <noreply@anthropic.com>2026-10-14 16:31:17 +0000
commitacc1870f4c4e2be8b3a27763cc6ad8a716e57e37 (patch)
tree86045d3f423066a43436666961d920bb2b8c9022 /plugins/security-guidance/hooks/diffstate.py
parent641d925db5a662e72f8650cb1b7670a59dab1abc (diff)
security-guidance: keep the session state when a save is cut short
The state file was rewritten in place, so a hook killed during a save left a truncated file that the next hook read as an empty state: the turn baseline, the touched paths and the shown warnings were gone. Write a temporary file and rename it over the state file. Windows keeps writing in place for now: hooks have no state lock there, and a rename can be refused or make a concurrent read fail. A state file that cannot be decoded no longer stops the hook. The Stop metrics gain two keys, sent only when nonzero: state_resets (the state file was unreadable and the state started over) and state_overwrites (without a lock, a save replaced one made by another hook since the read). Refs #1944
Diffstat (limited to 'plugins/security-guidance/hooks/diffstate.py')
-rw-r--r--plugins/security-guidance/hooks/diffstate.py7
1 files changed, 6 insertions, 1 deletions
diff --git a/plugins/security-guidance/hooks/diffstate.py b/plugins/security-guidance/hooks/diffstate.py
index be839212..31f3105e 100644
--- a/plugins/security-guidance/hooks/diffstate.py
+++ b/plugins/security-guidance/hooks/diffstate.py
@@ -103,17 +103,22 @@ def consume_stop_state(session_id, clear=True):
"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),
}
if clear:
state["touched_paths"] = []
state.pop("reviewed_diff_hash", None)
+ state.pop("state_resets", None)
+ state.pop("state_overwrites", None)
return snap
return with_locked_state(session_id, _snap) or {
"touched_paths": [], "baseline_sha": None, "head_at_capture": None,
"untracked_at_baseline": {},
"fire_count": 0, "fire_count_expired": False, "previous_findings": [],
- "reviewed_diff_hash": None,
+ "reviewed_diff_hash": None, "state_resets": 0, "state_overwrites": 0,
}