Repository navigation
fix(hook): bound soft nudges per agent (carries #3443) - #3893
andrebrait wants to merge 2 commits into
Conversation
|
Thanks for the pull request, @andrebrait. A maintainer will review it soon. Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions. A couple of things that speed up review: make sure the test suite passes on Python 3.10 and 3.13, and that the change keeps extraction deterministic. |
…Graphify-Labs#3443) Squashed from origin/fix/per-agent-hook-nudges (17a2822) onto upstream/v8.
There was a problem hiding this comment.
Graphify reviewed this change.
Worth a look — the grounded gate found no coupling regressions or blocking issues, but 2 advisory finding(s) below merit a look before merge.
Formal verification. 2 change(s) tested, no difference found (not proven).
Graphify review — findings
Bounds the soft Read/Glob/Grep/Bash nudge per agent via _nudge_allowed, so it no longer re-injects on every qualifying tool call: the first nudge always fires, later ones are skipped while a fresh graphify query/explain/path stamp shows the assistant is oriented, and it stops after GRAPHIFY_HOOK_NUDGE_CAP (default 5, 0/non-number disables) counted in a per-session marker. Keys budgets on session_id plus a hashed agent_id so Claude Code subagents each get their own count, falls open to stamp-only behaviour when there's no session id or a marker read/write fails, and reuses the shared _gc_hook_session_markers to expire markers past 24h.
Worth a look
- Non-atomic read-modify-write on nudge counter allows lost updates and cap bypass under concurrent hook invocations —
graphify/cli.py:748· Escalate · medium- agreed by 2 of 2 members but NOT verified (no proof, no reproducing execution) — consensus is not a verdict; needs human review
- First nudge is suppressed for hook payloads without session_id —
graphify/cli.py:732· Escalate · medium- agreed by 2 of 2 members but NOT verified (no proof, no reproducing execution) — consensus is not a verdict; needs human review
Analysis details — impact, health, verification
Impact & health
Graphify review
Impact — 349 functions depend on the 147 functions this change touches.
Health — this change adds coupling hotspots:
- new:
main()— 98 callers, 3 callees - new:
dispatch_command()— 2 callers, 125 callees - new:
_stale_graph_sources()— 7 callers, 6 callees - new:
_run_hook_guard()— 4 callers, 9 callees - new:
test_poisoned_manifest_is_healed()— 0 callers, 6 callees
Verification — 349 functions in the blast radius were not formally verified this run (proofs are advisory here).
Gate & verification
graphify gate
PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.
Advisory (not blocking):
- verification_scope: 287 function(s) in the blast radius were not formally verified this run
Test selection
Test selection
304 of 304 test file(s) selected (100%) via static blast radius.
Escalated to a full run for safety — the selection is not trustworthy on its own (see below). CI should run the whole suite.
tests/test_affected_cli.py— impact, full-run-safetytests/test_affected_member_seed.py— full-run-safetytests/test_agents_platform.py— impact, full-run-safetytests/test_analyze.py— full-run-safetytests/test_anthropic_custom_endpoint.py— full-run-safetytests/test_antigravity_install.py— full-run-safetytests/test_apm_fallback_version.py— full-run-safetytests/test_architecture_doc.py— full-run-safetytests/test_astro_extraction.py— full-run-safetytests/test_astro_import_ids.py— full-run-safetytests/test_atomic_canvas_export.py— full-run-safetytests/test_atomic_version_stamp.py— full-run-safetytests/test_atomic_writes.py— full-run-safetytests/test_backend_env_isolation.py— full-run-safetytests/test_backend_extras.py— full-run-safetytests/test_benchmark.py— full-run-safetytests/test_benchmark_raw_graph.py— full-run-safetytests/test_build.py— full-run-safetytests/test_build_merge_dedup_scope.py— full-run-safetytests/test_build_merge_hyperedges_and_prune.py— full-run-safetytests/test_build_merge_shrink_guard.py— full-run-safetytests/test_builtin_global_type_refs.py— full-run-safetytests/test_cache.py— full-run-safetytests/test_callflow_html.py— full-run-safetytests/test_cargo_introspect.py— full-run-safetytests/test_cargo_missing_manifest.py— full-run-safetytests/test_carried_hyperedge_remap.py— full-run-safetytests/test_case_sensitive_resolution.py— full-run-safetytests/test_charmap_encoding.py— full-run-safetytests/test_chunking.py— full-run-safetytests/test_cjs_module_extension.py— full-run-safetytests/test_claude_cli_backend.py— full-run-safetytests/test_claude_md.py— full-run-safetytests/test_cli_broken_pipe.py— full-run-safetytests/test_cli_export.py— full-run-safetytests/test_cli_help.py— full-run-safetytests/test_cluster.py— full-run-safetytests/test_cobol_extractor.py— full-run-safetytests/test_codebuddy.py— impact, full-run-safetytests/test_community_hub_labels.py— full-run-safetytests/test_community_labels_skill.py— full-run-safetytests/test_confidence.py— full-run-safetytests/test_corrupt_graph_json.py— full-run-safetytests/test_cpp_nested_and_cli.py— full-run-safetytests/test_cpp_objc_cross_file_calls.py— full-run-safetytests/test_cpp_preprocess.py— full-run-safetytests/test_cross_extension_reexport_self_cycle.py— full-run-safetytests/test_cross_language_call_resolution.py— full-run-safetytests/test_cross_repo_external_call_guards.py— full-run-safetytests/test_cross_repo_member_calls.py— full-run-safety- … and 254 more
non-code file(s) changed (
README.md) → running the full suite for safety (a code graph can't see config/fixture/data deps)
changed code file(s) with no mapped test (
README.md) — a coverage gap or a missing link — running the full suite rather than only the selected tests
Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.
Formal verification
No difference found (not proven): No behavior difference found in \_mark\_session\_denied (not a proof).
The verifier ran both versions of \_mark\_session\_denied on many inputs and saw identical behavior every time. Strong evidence the change is safe, but evidence, not a proof.
Guarantee: Empirical: differential testing (both versions run on many generated inputs). A divergence on an untested input remains possible, so this is 'no counterexample found', not 'proven equivalent'.
Note: An input the sampler did not try could still differ.
No difference found (not proven): No behavior difference found in \_run\_hook\_guard (not a proof).
The verifier ran both versions of \_run\_hook\_guard on many inputs and saw identical behavior every time. Strong evidence the change is safe, but evidence, not a proof.
Guarantee: Empirical: differential testing (both versions run on many generated inputs). A divergence on an untested input remains possible, so this is 'no counterexample found', not 'proven equivalent'.
Note: An input the sampler did not try could still differ.
· 5 more finding(s) on lines outside this diff (see the check run).
…Graphify-Labs#3443) Squashed from origin/fix/per-agent-hook-nudges (17a2822) onto upstream/v8.
The PreToolUse guard re-injected the MANDATORY read/search nudge on every qualifying Read/Glob/Grep/Bash call with no cap and no check whether the agent had just queried the graph. Only the opt-in strict deny was capped. One project measured 8,348 injections (~651k tokens) for 339 graphify calls, several times the cost of every query they were asking for. Skip the soft nudge while the query stamp is fresh (the agent is already oriented) and cap it at GRAPHIFY_HOOK_NUDGE_CAP nudges per session (default 5, 0 disables), counted in a per-session marker next to the strict-mode ones. Calls without a session id are uncounted, the guard still fails open, and the shared marker GC now runs on both paths. Fixes #3435
Subagents start with an empty context, but Claude Code gives them the parent's session_id; keying the nudge budget on session_id alone let a parent exhaust a subagent's budget. Key it on session_id plus a hash of agent_id when present. The query stamp is project-wide and cannot say which agent queried, so it no longer silences an agent's first nudge; it silences later ones. The stale-graph notice now draws on the same budget instead of firing on every read of an edited file.
…Graphify-Labs#3443) Squashed from origin/fix/per-agent-hook-nudges (158f035) onto upstream/v8.
17a2822 to
158f035
Compare
What does this PR do?
Carries #3443 by @L4XB onto the current
v8base, preserving its commit authorship and message, then adds one follow-up commit that closes three gaps found while running it downstream. Refs #3435, #3756, #2984.Relationship to #3443
v8. It is an integration candidate, not a claim of original authorship; maintainers can merge whichever shape is easiest, and this can be closed if the follow-up lands in fix(hook): bound the soft read/search nudge per session #3443 directly.The follow-up
session_idand are distinguished only byagent_id(hooks reference). Keying the counter onsession_idalone lets the parent exhaust the budget before a subagent, which starts with an empty context, sees a nudge. The marker is now keyed onsession_idplus a 16-character SHA-256 prefix ofagent_idwhen present, so long ids cannot truncate two agents into one key.last_query_stampis project-wide and cannot say which agent ranquery/explain/path. Each agent's first nudge now fires regardless of the stamp; the stamp silences the later ones. Calls without asession_idkeep the stamp-only behavior._READ_NUDGE_STALEfired on every read of a file edited after the last build. It now draws on the same per-agent budget._nudge_allowedtakes the hook payload instead of a session id so it can readagent_id. The strict-mode deny is unchanged.Type of change
Verification & Invariants
Invariants: each agent (session, or subagent within it) sees its first nudge; after that, nudges stop while the query stamp is fresh and after
GRAPHIFY_HOOK_NUDGE_CAPper agent; the strict deny still fires at most once per session; any marker error fails open.test_fresh_query_stamp_suppresses_denychanges accordingly: with a fresh stamp there is still no deny, the session's first nudge fires, and the second read is silent. New tests cover per-agent budgets (including 64-character session ids), a subagent's first nudge after the parent queried, the stamp without asession_id, and the stale notice sharing the budget. They were run red against #3443 alone before the follow-up.Known limitation (inherited from #3443): the counter is an unlocked read-modify-write, so concurrent hook processes for the same agent can exceed the cap slightly. They cannot suppress an agent's first nudge.
How was this tested?
Also deployed downstream and checked live under Oh My Pi: after
graphify query, the parent session's next grep was silent, and a freshly spawned subagent still received its first nudge, with separate markers undergraphify-out/cache/hook_sessions/.Graphify-specific checklist
uv run python -m tools.skillgen --bless) when changing their source fragments. (Not applicable.)