Skip to content

fix(watch): record a contended incremental request instead of dropping it (#4800) - #4802

Closed
Deepjyoti-Sarmah wants to merge 8 commits into
Graphify-Labs:v8from
Deepjyoti-Sarmah:fix/4800-incremental-lock-drop
Closed

Deepjyoti-Sarmah wants to merge 8 commits into
Graphify-Labs:v8from
Deepjyoti-Sarmah:fix/4800-incremental-lock-drop

Conversation

@Deepjyoti-Sarmah

Copy link
Copy Markdown
Contributor

What does this PR do?

Part of #4800, stacked on #4801. That PR gives graphify update a change set derived
from the manifest. A request built that way names no paths, and this PR is what
happens when such a request loses the per-repo lock.

_rebuild_code asks for the lock before it does anything. If another rebuild holds
it, each request kind records what it could not do so the holder can run it later:

if not block_on_lock:
    if incremental and changed_paths is None:
        pass                                # writes NOTHING
    elif changed_paths is None:
        _queue_pending_full(out)            # "re-extract everything"
    elif changed_paths:
        _queue_pending(out, changed_paths)  # "re-extract these files"

The derived kind has no path list to append and cannot use .pending_full: the drain
runs after the lock it just won, so a full marker would upgrade its own incremental
pass. So it recorded nothing, printed "nothing to queue", and returned False, which
is the same answer a caller gets when its change set was recorded and will run.

The review bot on #4801 flags this at graphify/watch.py:1979, rated high. This PR
closes that finding.

 _rebuild_code(..., incremental=True, block_on_lock=False)
   if incremental and changed_paths is None:
-    pass
+    _queue_pending_incremental(out)     # a third marker, written before the attempt
+    queued = True

   # inside the lock, whichever process holds it
+  queued_incremental = _drain_pending_incremental(out)
+  incremental_effective = incremental or queued_incremental
-  if queued_full:      full = True
-  elif changed_paths is None:  full = not incremental
+  if queued_full:      full = True     # a full request still wins
+  elif changed_paths is None:  full = not incremental_effective
   else:                full = False

An explicit changed_paths still wins, and .pending_full still wins over
everything. The late-arrival loop drains the third marker too and breaks only when
all three records are empty.

Three commits

a82abc60  test(watch): pin the lock-queue contract for the existing request kinds
acb92bf5  fix(watch): record a contended incremental request instead of dropping it
9ce7e721  docs(watch): describe the three queue request kinds

The first is pure coverage of the contract that already holds for the other two kinds
(.pending_full / .pending_changes semantics, and a caller that wins the lock not
upgrading itself). It passes on the base branch. The second adds the marker, the
wiring and the tests for it, in one commit, so a revert removes the behaviour and its
tests together. The third updates comments that described two kinds.

The contract the tests pin

tests/test_update_lock_contention.py, six tests, real lock and real queue files, no
monkeypatching. Contention is created by holding the lock in-process; flock is per
open file description, so a second open in the same process conflicts:

I1  a contended incremental request leaves a record a drain can act on
    ...and `.pending_incremental` is written on contention, drained once, then absent
I2  a caller that WINS the lock runs the pass it asked for, never a full one
    driven by a record it left itself
I3  the record drives the work through the winner's drain
I4  `.pending_full` / `.pending_changes` keep their meaning, and a queued full
    marker still upgrades a blocking incremental caller

I3 is the one worth reading. The call that ends up doing the work asks for the
default pass, which is a full one, so a single re-extracted file can only come from
the winner's own drain honouring the marker. Calling _rebuild_code(incremental=True)
again and calling that "honoured" would be the test re-asking, which proves nothing
about the queue.

No sleeps. Content changes carry an explicit os.utime bump, so nothing depends on
filesystem timestamp granularity.

Type of change

  • Bug fix
  • New feature
  • Tests or CI
  • Refactor
  • Security fix

Verification & Invariants

A request that loses the lock must either leave something the holder can act on, or
tell its caller it was not served. It must not return the same False that means
"queued and will run".

  • 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.

Merge Danger

Door: two-way. One new marker file under graphify-out/ and the drain that reads
it. Reverting the middle commit removes the behaviour and its tests together.

Blast Radius: narrow, and limited to builds that lose the lock:

  • A build that wins the lock behaves exactly as before, except that a marker left by
    another process is now honoured: the winner runs an incremental pass rather than a
    full one when no explicit paths and no full marker are present.
  • .pending_full still wins over everything, and an explicit changed_paths still
    wins over the derived set.
  • The marker is written before the lock attempt, as the other two records are. The
    write-before-lock order is deliberate and unchanged: reversing it broke
    tests/test_watch.py::test_rebuild_code_empty_change_set_does_not_claim_queued
    (Full-corpus rebuild requests are dropped on lock contention (log says "changes queued"), and a full rebuild never drains late incremental work #4274) in an earlier attempt at this area.
  • A stale marker left by a crash asks for one extra derived pass. That pass is a
    content diff, so it is cheap and it cannot miss work.

Limitations

  • This is design completion, not an incident fix. No non-blocking incremental
    caller exists today: graphify update blocks on the lock, and the watcher and the
    hooks pass explicit paths, so they use .pending_changes. The hole opens the day
    something calls _rebuild_code(incremental=True) without blocking, which is a
    reasonable thing to add later.
  • The winner's pass becomes incremental when a marker is queued, including when
    the winner's own request was a full one. That is the intended semantics (a queued
    incremental request has to be honoured), and it is what I3 pins. A full request
    still wins if .pending_full is also present.
  • An older graphify ignores the marker. It does not know the file, so it behaves
    as it does today: the request is dropped. Nothing breaks.
  • One test needed an adaptation for the 0.9.96 gate.
    test_i4_a_queued_full_marker_still_upgrades_a_blocking_incremental_caller queues a
    marker and expects a full pass, but the gate runs before the drain, so an untouched
    tree returns at the gate and never reaches the queue. It now carries one change
    first, the same way test_default_is_the_full_path in perf(update): re-extract only the files whose content changed (#4800) #4801 does. The assertion is
    unchanged.

How was this tested?

Red on the base branch, for the intended reason:

E  AssertionError: a contended incremental request was dropped: it left no pending
   record and its caller got an indistinguishable result (outcome=False, pending=[])
FAILED tests/test_update_lock_contention.py::test_i1_contended_incremental_request_is_not_silently_dropped

Green with the fix, including the mutation check. Removing the marker write from
commit 2 puts both I1 tests back to red, which is how I know they are not vacuous:

MUTATION, marker write removed -> 2 failed (I1 and the marker lifecycle)
restored                        -> 6 passed
tests/test_update_lock_contention.py       -> 6 passed
tests/test_update_incremental.py           -> 17 passed
.venv/bin/python -m pytest -q              -> 8895 passed, 17 skipped, 1 xfailed in 411.72s
.venv/bin/python -m ruff check graphify/ tests/test_update_lock_contention.py
                                           -> All checks passed

8895 is #4801's 8889 plus the six tests here. The suite ran with PYTHONPATH set to
the repo, so the tests that shell out via sys.executable -m graphify load this tree
rather than the installed package.

Diffstat against the base branch:

 graphify/watch.py                    |  66 +++++++++--
 tests/test_update_lock_contention.py | 219 +++++++++++++++++++++++++++++++++++
 2 files changed, 273 insertions(+), 12 deletions(-)

Not tested: Windows. The marker uses the same Path.touch() / unlink() pattern as
the two existing records.

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments. (N/A: no skill fragments touched.)
  • I confirmed that AST/structural extraction remains deterministic (no ambient state dependencies like ENV variables). (No extraction change.)
  • I reviewed changes for security implications (no unsafe interpolation into shell/Python).
  • I confirmed no API keys or local-only graph data are included.
  • (If applicable) I disclosed AI authorship in my commit messages. (No trailer in the commit messages.)

@github-actions github-actions Bot added the contributor Pull request from a returning community contributor label Oct 26, 2026
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @Deepjyoti-Sarmah. 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 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 4 advisory finding(s) below merit a look before merge.

Not checked: tests were not run; formal verification proved 0 of 4 changed function(s) (1 sampled, not proven, 3 not verified).

Formal verification. PR-changed functions: 1/4 verified (0 proven, 1 may-equivalent, 0 distinguished) · 3 not verified (1 vacuous, 2 unsupported).

Not verified on this run: dispatch\_command (vacuous: never exercised), detect\_incremental (unsupported), \_rebuild\_code (unsupported).


Graphify review — findings

Makes graphify update re-extract only files whose content differs from the manifest, so a warm tree costs what changed instead of the whole corpus; --force and a missing, unreadable or empty manifest still take the full path. Adds a third .pending_incremental marker alongside .pending_changes and .pending_full, so an incremental request that loses the lock is queued rather than dropped, and the lock holder's drain now has to pick between full, incremental and explicit-path passes. Extracts classify_manifest_deletions out of detect_incremental to split vanished manifest rows into deleted (gone from disk) and excluded (still on disk but out of scan), with resolution inputs counted as scanned.

Worth a look

  • Blocking full-rebuild request downgraded to incremental by a queued incremental marker — graphify/watch.py:2051 · Escalate · medium
    • NOT verified (no proof, no reproducing execution) — needs human review
  • Queued incremental marker is consumed and dropped when the lock holder has explicit changed_paths — graphify/watch.py:2045 · Escalate · medium
    • NOT verified (no proof, no reproducing execution) — needs human review
  • Root containment check accepts '..' paths outside the root — graphify/detect.py:3095 · Escalate · medium
    • NOT verified (no proof, no reproducing execution) — needs human review
  • absolute_identity is called before the falsy source_file guard, so None or empty paths are no longer short-circuited — graphify/watch.py:2516 · Escalate · medium
    • NOT verified (no proof, no reproducing execution) — needs human review

Review partial — this diff was larger than one review pass covers, so later files were not reviewed; some findings may be missing.

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 4039 functions depend on the 408 functions this change touches.

Health — this change adds coupling hotspots:

  • worse: _rebuild_code() — 228 callers, 70 callees
  • worse: load_manifest() — 17 callers, 5 callees
  • worse: classify_manifest_files() — 4 callers, 5 callees

Verification — 4039 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: 2543 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

121 of 467 test file(s) selected (26%) via static blast radius.

  • tests/test_affected_cli.py — impact
  • tests/test_agents_platform.py — impact
  • tests/test_astro_extraction.py — impact
  • tests/test_atomic_writes.py — impact
  • tests/test_backend_opt_in.py — impact
  • tests/test_binary_media_words.py — impact
  • tests/test_build.py — impact
  • tests/test_cache_stale_import_target.py — impact
  • tests/test_cargo_introspect.py — impact
  • tests/test_charmap_encoding.py — impact
  • tests/test_chunking.py — impact
  • tests/test_cjs_module_extension.py — impact
  • tests/test_claude_cli_backend.py — impact
  • tests/test_cli_export.py — impact
  • tests/test_cloud_cta.py — impact
  • tests/test_codebuddy.py — impact
  • tests/test_compact_graph_json.py — impact
  • tests/test_dedup_shrink_refuses_force_write.py — impact
  • tests/test_detect.py — impact
  • tests/test_devin.py — impact
  • tests/test_docx_to_markdown.py — impact
  • tests/test_dotnet.py — impact
  • tests/test_ejs_extractor.py — impact
  • tests/test_elixir_import_resolution.py — impact
  • tests/test_elixir_qualified_calls.py — impact
  • tests/test_elixir_unqualified_call_scope.py — impact
  • tests/test_evidence_binding.py — impact
  • tests/test_explain_cli.py — impact
  • tests/test_explain_same_file_tie.py — impact
  • tests/test_export_direction.py — impact
  • tests/test_ext_tagged_id_clash.py — impact
  • tests/test_external_stub_endpoints.py — impact
  • tests/test_extract.py — impact
  • tests/test_extract_cli.py — impact
  • tests/test_extract_cost_ledger.py — impact
  • tests/test_file_slice.py — impact
  • tests/test_file_truncation_warning.py — impact
  • tests/test_global_add_tag_inference.py — impact
  • tests/test_god_nodes_cli.py — impact
  • tests/test_hollow_chunks_arm_shrink_guard.py — impact
  • tests/test_hook_guard_token_match.py — impact
  • tests/test_hook_nudge_wording.py — impact
  • tests/test_hook_out_of_project_paths.py — impact
  • tests/test_hook_strict.py — impact
  • tests/test_ignore_file_encoding.py — impact
  • tests/test_image_vision.py — impact
  • tests/test_import_extension_resolution.py — impact
  • tests/test_incomplete_build_guard.py — impact
  • tests/test_incremental_mtime_collision.py — impact
  • tests/test_incremental_unscanned_placeholders.py — impact
  • … and 71 more

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.

Docs that may be stale (advisory)

…and 10 more.

Formal verification

Could not verify: Could not verify dispatch\_command.

The verifier did not have enough to check dispatch\_command, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: not verifiable: all 40 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly SystemExit — names the real obstacle, not a sampling gap)

Could not verify: Could not verify detect\_incremental.

The verifier did not have enough to check detect\_incremental, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: nondeterministic output (both versions disagreed with themselves on the witness) — not a behaviour change; a function whose output is not a function of its inputs (random / time / uuid / os.urandom, or state carried across calls) cannot be soundly distinguished by execution, so this tier abstains for the whole function

No difference found (not proven): No behavior difference found in \_queue\_pending\_full (not a proof).

The verifier ran both versions of \_queue\_pending\_full 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.

Could not verify: Could not verify \_rebuild\_code.

The verifier did not have enough to check \_rebuild\_code, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: nondeterministic output (both versions disagreed with themselves on the witness) — not a behaviour change; a function whose output is not a function of its inputs (random / time / uuid / os.urandom, or state carried across calls) cannot be soundly distinguished by execution, so this tier abstains for the whole function

· 3 more finding(s) on lines outside this diff (see the check run).

…g it

An incremental rebuild derives its change set from the manifest inside the lock,
so it names no paths and had nothing to append to .pending_changes, while
.pending_full would have meant "re-extract everything". Losing the lock therefore
dropped the request, and the False it returned was indistinguishable from the
"queued, it will run" answer a caller gets for the other two kinds.

This is latent rather than an incident: no non-blocking incremental caller exists
today, because the CLI blocks on the lock and the watcher and hooks pass explicit
paths. The fix completes the design.

The request is now recorded as a .pending_incremental marker, and any holder that
drains it runs an incremental pass, including a holder whose own request was a
full one. Reported by review on Graphify-Labs#4769.

Fixes the latent drop for Graphify-Labs#4769's incremental kind.
@Deepjyoti-Sarmah
Deepjyoti-Sarmah force-pushed the fix/4800-incremental-lock-drop branch from 9ce7e72 to 10f9321 Compare November 1, 2026 18:21
@safishamsi

Copy link
Copy Markdown
Member

Landed in 0.9.102 (published to PyPI), cherry-picked with your authorship preserved (the three unique commits on top of #4801). Thanks @Deepjyoti-Sarmah.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor Pull request from a returning community contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants