Skip to content

fix(hook): bound soft nudges per agent (carries #3443) - #3893

Closed
andrebrait wants to merge 2 commits into
Graphify-Labs:v8from
andrebrait:fix/per-agent-hook-nudges
Closed

andrebrait wants to merge 2 commits into
Graphify-Labs:v8from
andrebrait:fix/per-agent-hook-nudges

Conversation

@andrebrait

Copy link
Copy Markdown
Contributor

What does this PR do?

Carries #3443 by @L4XB onto the current v8 base, 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

The follow-up

  1. Subagents get their own budget. Claude Code subagent tool calls carry the parent's session_id and are distinguished only by agent_id (hooks reference). Keying the counter on session_id alone lets the parent exhaust the budget before a subagent, which starts with an empty context, sees a nudge. The marker is now keyed on session_id plus a 16-character SHA-256 prefix of agent_id when present, so long ids cannot truncate two agents into one key.
  2. The query stamp no longer silences agents that never queried. last_query_stamp is project-wide and cannot say which agent ran query/explain/path. Each agent's first nudge now fires regardless of the stamp; the stamp silences the later ones. Calls without a session_id keep the stamp-only behavior.
  3. The stale-graph notice is bounded. _READ_NUDGE_STALE fired on every read of a file edited after the last build. It now draws on the same per-agent budget.

_nudge_allowed takes the hook payload instead of a session id so it can read agent_id. The strict-mode deny is unchanged.

Type of change

  • Bug fix

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_CAP per agent; the strict deny still fires at most once per session; any marker error fails open.

test_fresh_query_stamp_suppresses_deny changes 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 a session_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.

  • Read the CONTRIBUTING.md guide.
  • Reproduced the issue and identified the invariant.
  • Made the smallest fix necessary.
  • Added a regression test (if bug fix) or isolated boundary test.
  • Kept the PR description synchronized with the final implementation.
  • Documented any limitations / unsupported cases explicitly.

How was this tested?

uv run pytest tests/test_hook_guard.py tests/test_hook_strict.py tests/test_hook_guard_token_match.py tests/test_hook_out_of_project_paths.py tests/test_install_strings.py
  165 passed, 4 skipped
uv run pytest -q
  24 failed, 5961 passed, 100 skipped (all 24 also fail on unmodified v8 here: optional
  Erlang/R/Solidity/VB.NET extractors and Ollama backend not installed locally)
uv run ruff check .
  All checks passed!

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 under graphify-out/cache/hook_sessions/.

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments. (Not applicable.)
  • I confirmed that AST/structural extraction remains deterministic (no ambient state dependencies like ENV variables).
  • I reviewed changes for security implications (no unsafe interpolation into shell/Python).
  • I confirmed no API keys or local-only graph data are included.

@github-actions

Copy link
Copy Markdown

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.

andrebrait added a commit to pfBlockerNG/graphify that referenced this pull request Sep 28, 2026
…Graphify-Labs#3443)

Squashed from origin/fix/per-agent-hook-nudges (17a2822) onto upstream/v8.

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-safety
  • tests/test_affected_member_seed.py — full-run-safety
  • tests/test_agents_platform.py — impact, full-run-safety
  • tests/test_analyze.py — full-run-safety
  • tests/test_anthropic_custom_endpoint.py — full-run-safety
  • tests/test_antigravity_install.py — full-run-safety
  • tests/test_apm_fallback_version.py — full-run-safety
  • tests/test_architecture_doc.py — full-run-safety
  • tests/test_astro_extraction.py — full-run-safety
  • tests/test_astro_import_ids.py — full-run-safety
  • tests/test_atomic_canvas_export.py — full-run-safety
  • tests/test_atomic_version_stamp.py — full-run-safety
  • tests/test_atomic_writes.py — full-run-safety
  • tests/test_backend_env_isolation.py — full-run-safety
  • tests/test_backend_extras.py — full-run-safety
  • tests/test_benchmark.py — full-run-safety
  • tests/test_benchmark_raw_graph.py — full-run-safety
  • tests/test_build.py — full-run-safety
  • tests/test_build_merge_dedup_scope.py — full-run-safety
  • tests/test_build_merge_hyperedges_and_prune.py — full-run-safety
  • tests/test_build_merge_shrink_guard.py — full-run-safety
  • tests/test_builtin_global_type_refs.py — full-run-safety
  • tests/test_cache.py — full-run-safety
  • tests/test_callflow_html.py — full-run-safety
  • tests/test_cargo_introspect.py — full-run-safety
  • tests/test_cargo_missing_manifest.py — full-run-safety
  • tests/test_carried_hyperedge_remap.py — full-run-safety
  • tests/test_case_sensitive_resolution.py — full-run-safety
  • tests/test_charmap_encoding.py — full-run-safety
  • tests/test_chunking.py — full-run-safety
  • tests/test_cjs_module_extension.py — full-run-safety
  • tests/test_claude_cli_backend.py — full-run-safety
  • tests/test_claude_md.py — full-run-safety
  • tests/test_cli_broken_pipe.py — full-run-safety
  • tests/test_cli_export.py — full-run-safety
  • tests/test_cli_help.py — full-run-safety
  • tests/test_cluster.py — full-run-safety
  • tests/test_cobol_extractor.py — full-run-safety
  • tests/test_codebuddy.py — impact, full-run-safety
  • tests/test_community_hub_labels.py — full-run-safety
  • tests/test_community_labels_skill.py — full-run-safety
  • tests/test_confidence.py — full-run-safety
  • tests/test_corrupt_graph_json.py — full-run-safety
  • tests/test_cpp_nested_and_cli.py — full-run-safety
  • tests/test_cpp_objc_cross_file_calls.py — full-run-safety
  • tests/test_cpp_preprocess.py — full-run-safety
  • tests/test_cross_extension_reexport_self_cycle.py — full-run-safety
  • tests/test_cross_language_call_resolution.py — full-run-safety
  • tests/test_cross_repo_external_call_guards.py — full-run-safety
  • tests/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).

andrebrait added a commit to pfBlockerNG/graphify that referenced this pull request Sep 28, 2026
…Graphify-Labs#3443)

Squashed from origin/fix/per-agent-hook-nudges (17a2822) onto upstream/v8.
L4XB and others added 2 commits September 29, 2026 12:01
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.
andrebrait added a commit to pfBlockerNG/graphify that referenced this pull request Sep 29, 2026
…Graphify-Labs#3443)

Squashed from origin/fix/per-agent-hook-nudges (158f035) onto upstream/v8.
@andrebrait
andrebrait force-pushed the fix/per-agent-hook-nudges branch from 17a2822 to 158f035 Compare September 29, 2026 12:29
@andrebrait andrebrait closed this Oct 5, 2026
@andrebrait
andrebrait deleted the fix/per-agent-hook-nudges branch October 5, 2026 12:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants