Whether to emit a soft read/search nudge for this tool call (#3435). The nudge is advisory context that used to be re-injected on EVERY qualifying Read/Glob/Grep/Bash call — thousands of times per session, far more tokens than the graph queries it asks for. Budgets are per agent: the se
(payload: dict)
| 1231 | |
| 1232 | |
| 1233 | def _nudge_allowed(payload: dict) -> bool: |
| 1234 | """Whether to emit a soft read/search nudge for this tool call (#3435). |
| 1235 | |
| 1236 | The nudge is advisory context that used to be re-injected on EVERY qualifying |
| 1237 | Read/Glob/Grep/Bash call — thousands of times per session, far more tokens |
| 1238 | than the graph queries it asks for. Budgets are per agent: the session id, |
| 1239 | plus the ``agent_id`` Claude Code sends for subagents (which share the |
| 1240 | parent's session id but start with an empty context). Per agent: |
| 1241 | |
| 1242 | * the first nudge always fires. The query stamp is project-wide and cannot |
| 1243 | say which agent queried, so it must not silence a fresh subagent or session; |
| 1244 | * after that, a fresh query stamp (query/explain/path within |
| 1245 | GRAPHIFY_HOOK_STRICT_TTL) means someone oriented recently, so no nudge; |
| 1246 | * at most GRAPHIFY_HOOK_NUDGE_CAP nudges (default 5; 0 or a non-number |
| 1247 | disables the cap), counted in a marker next to the strict-mode ones. |
| 1248 | |
| 1249 | Calls without a session id cannot be counted: only the stamp applies. |
| 1250 | Fail-open: on a marker error, only the stamp applies. Compaction keeps the |
| 1251 | session id but drops the earlier nudges from the model's context, so |
| 1252 | ``hook-guard compact`` (Claude Code SessionStart, source "compact") deletes |
| 1253 | the session's counters, see _reset_hook_session. |
| 1254 | """ |
| 1255 | from graphify.paths import out_path, write_text_atomic |
| 1256 | import hashlib |
| 1257 | stamp = _query_stamp_fresh() |
| 1258 | try: |
| 1259 | cap = int(os.environ.get("GRAPHIFY_HOOK_NUDGE_CAP", "5")) |
| 1260 | except ValueError: |
| 1261 | cap = 0 |
| 1262 | key = re.sub(r"[^A-Za-z0-9_-]", "_", str(payload.get("session_id") or ""))[:64] |
| 1263 | if not key: |
| 1264 | return not stamp |
| 1265 | agent = str(payload.get("agent_id") or "") |
| 1266 | if agent: |
| 1267 | # Hashed so long ids cannot truncate two agents into one key. |
| 1268 | key += "-" + hashlib.sha256(agent.encode("utf-8")).hexdigest()[:16] |
| 1269 | try: |
| 1270 | d = out_path("cache", "hook_sessions") |
| 1271 | d.mkdir(parents=True, exist_ok=True) |
| 1272 | marker = d / f"{key}.nudges" |
| 1273 | try: |
| 1274 | count = int(marker.read_text(encoding="utf-8").strip() or "0") |
| 1275 | except (OSError, ValueError): |
| 1276 | count = 0 |
| 1277 | if count and stamp: |
| 1278 | return False |
| 1279 | if 0 < cap <= count: |
| 1280 | return False |
| 1281 | write_text_atomic(marker, str(count + 1)) |
| 1282 | if count == 0: |
| 1283 | _gc_hook_session_markers(d) |
| 1284 | return True |
| 1285 | except Exception: |
| 1286 | return not stamp |
| 1287 | |
| 1288 | |
| 1289 | def _gc_hook_session_markers(d: "Path") -> None: |
no test coverage detected