Skip to content

fix(llm): backfill edge source_file, add casefold recovery, pass allowed to on_chunk_done, and set ollama max_tokens (#3982) - #3985

Open
Adityakk9031 wants to merge 1 commit into
Graphify-Labs:v8from
Adityakk9031:fix/ollama-semantic-edge-backfill-and-token-cap
Open

Adityakk9031 wants to merge 1 commit into
Graphify-Labs:v8from
Adityakk9031:fix/ollama-semantic-edge-backfill-and-token-cap

Conversation

@Adityakk9031

Copy link
Copy Markdown
Contributor

Resolves #3982

Root Cause

  1. Missing Edge source_file: Local / weaker LLMs (such as small models via Ollama) frequently emit edges without a source_file field. During semantic cache grouping in save_semantic_cache, items without source_file are skipped.
  2. Casefold recovery: Model drift or case inconsistencies (e.g. WEIRD_NAMED_FILE.py vs weird_named_file.py) caused _recover_group_path in graphify/cache.py to fail basename matching and drop valid cache items.
  3. Single-file chunk attribution: In single-file chunks, any un-attributed items unambiguously belong to that single dispatched file.
  4. on_chunk_done callback signature: Callers running extract_corpus_parallel as a library did not receive the chunk allowlist (allowed), preventing proper downstream scoping / caching and risking unintended cache drops.
  5. Ollama max_tokens vs max_completion_tokens: Ollama's OpenAI-compatible endpoint ignores max_completion_tokens and only respects max_tokens (or options: {num_predict: ...}).

Changes

  • Edge source_file backfill & single-file chunk attribution (graphify/llm.py):
    • In single-file chunks, attribute missing source_file on nodes, edges, and hyperedges to the lone file.
    • In multi-file chunks, backfill missing edge source_file from endpoint node source files (source or target).
    • Emit a warning with count if edges remain un-attributed.
  • Casefold recovery (graphify/cache.py):
    • If exact basename matching fails in _recover_group_path, fall back to case-insensitive basename matching against allowed_paths.
  • on_chunk_done callback (graphify/llm.py):
    • Pass allowed=[unit_path(item) for item in chunk] to on_chunk_done with backwards-compatible fallback for 3-argument callbacks.
  • Ollama max_tokens (graphify/llm.py):
    • When backend == "ollama" in _call_openai_compat, supply kwargs["max_tokens"] = max_completion_tokens.
  • Tests:
    • Added unit tests for edge source_file backfill and on_chunk_done in tests/test_chunking.py.
    • Added unit tests for case-insensitive basename recovery in tests/test_semantic_cache_basename_recovery.py.
    • Added unit tests for Ollama max_tokens parameter in tests/test_llm_backends.py.

@github-actions

github-actions Bot commented Oct 2, 2026

Copy link
Copy Markdown

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

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

Not verified on this run: save\_semantic\_cache (unsupported), \_call\_openai\_compat (error), extract\_corpus\_parallel (unsupported).


Graphify review — findings

Fixes local/weak-model extraction gaps from #3982: - Source-file attribution. extract_corpus_parallel now fills in missing source_file values. In a single-file chunk, anything without one is attributed to that file; in multi-file chunks, edges inherit it from their source or target node, and any that still can't be placed trigger a RuntimeWarning with a count. - Chunk callback. on_chunk_done now also receives an allowed= list of the chunk's paths, falling back to the old 3-argument call when the callback rejects it. - Ollama and cache. Ollama requests now send max_tokens, since that endpoint ignores max_completion_tokens. save_semantic_cache falls back to a case-insensitive basename match when no exact match exists.

Worth a look

  • Callback TypeError fallback re-invokes on_chunk_done, masking errors and duplicating side effects — graphify/llm.py:2762 · 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 — 2548 functions depend on the 503 functions this change touches.

Health — this change adds coupling hotspots:

  • new: extract() — 716 callers, 48 callees
  • new: _rebuild_code() — 147 callers, 56 callees
  • new: deduplicate_entities() — 79 callers, 24 callees
  • new: detect() — 112 callers, 15 callees
  • new: build_merge() — 76 callers, 14 callees
  • new: save_semantic_cache() — 64 callers, 9 callees
  • new: load_cached() — 54 callers, 7 callees
  • new: extract_files_direct() — 17 callers, 20 callees
  • …and 44 more — each is listed as a finding

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

Test selection

Test selection

61 of 306 test file(s) selected (20%) via static blast radius.

  • tests/test_backend_env_isolation.py — impact
  • tests/test_backend_extras.py — impact
  • tests/test_build.py — impact
  • tests/test_build_merge_dedup_scope.py — impact
  • tests/test_build_merge_hyperedges_and_prune.py — impact
  • tests/test_build_merge_shrink_guard.py — impact
  • tests/test_cache.py — impact
  • tests/test_carried_hyperedge_remap.py — impact
  • tests/test_charmap_encoding.py — impact
  • tests/test_chunking.py — impact, changed-test
  • tests/test_claude_cli_backend.py — impact
  • tests/test_corrupt_graph_json.py — impact
  • tests/test_cross_extension_reexport_self_cycle.py — impact
  • tests/test_dedup.py — impact
  • tests/test_dedup_remaps_hyperedges.py — impact
  • tests/test_dedup_survivor_richness.py — impact
  • tests/test_definition_file_portability.py — impact
  • tests/test_detect.py — impact
  • tests/test_evidence_binding.py — impact
  • tests/test_extract.py — impact
  • tests/test_extract_cache_location.py — impact
  • tests/test_extract_cli.py — impact
  • tests/test_file_slice.py — impact
  • tests/test_global_graph.py — impact
  • tests/test_go_qualified_resolution.py — impact
  • tests/test_hyperedge_member_shapes.py — impact
  • tests/test_ignore_file_encoding.py — impact
  • tests/test_image_vision.py — impact
  • tests/test_incremental_mtime_collision.py — impact
  • tests/test_injection_sentinel_coverage.py — impact
  • tests/test_issue_3472_source_file_collision.py — impact
  • tests/test_label_retry.py — impact
  • tests/test_labeling.py — impact
  • tests/test_llm_backends.py — impact, changed-test
  • tests/test_llm_parser.py — impact
  • tests/test_llm_parser_reasoning.py — impact
  • tests/test_no_dedup_flag.py — impact
  • tests/test_non_string_node_ids.py — impact
  • tests/test_objc_field_table_remap.py — impact
  • tests/test_ollama.py — impact
  • tests/test_ollama_retry_cap.py — impact
  • tests/test_out_dir_evidence.py — impact
  • tests/test_oversized_document_slicing.py — impact
  • tests/test_partial_cache.py — impact
  • tests/test_pdf_slicing.py — impact
  • tests/test_pdf_token_estimate.py — impact
  • tests/test_pipeline.py — impact
  • tests/test_provider_registry.py — impact
  • tests/test_prs.py — impact
  • tests/test_prune_sweeps_orphans.py — impact
  • … and 11 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)

Formal verification

Could not verify: Could not verify save\_semantic\_cache.

The verifier did not have enough to check save\_semantic\_cache, 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: parameter `prompt` is annotated `'str | Path | None'` — outside the synthesizable primitive/collection set

Verification did not run: Verification did not run for \_call\_openai\_compat.

The verification could not execute (an environment/toolchain issue, not a statement about the code).

Guarantee: No guarantee, the check itself did not complete.

Note: Detail: harness produced no verdict (rc=124): timeout after 30s

Could not verify: Could not verify extract\_corpus\_parallel.

The verifier did not have enough to check extract\_corpus\_parallel, 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: parameter `on_chunk_done` is annotated `Callable | None` — outside the synthesizable primitive/collection set

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

@safishamsi

Copy link
Copy Markdown
Member

Thanks @Adityakk9031 — all four parts are genuinely still needed on current v8 (edge source_file backfill, casefold recovery, on_chunk_done(allowed=), ollama max_tokens), and they don't overlap the shipped #4340/#4168 ollama work. One blocking defect before it can land: the backfill's un-attributable-edge path calls warnings.warn(...), but warnings is not imported in llm.py. Since _run_one wraps everything in try/except Exception, any chunk with a truly sourceless edge raises NameError, gets swallowed, and the whole chunk is dropped — strictly worse than the single-edge drop it replaces. Add import warnings (or use the module's existing stderr-print pattern), add a test that drives missing_edges>0, and rebase onto the current _call_openai_compat (the num_ctx merge + ollama_seed insertions moved those hunks). Then it's a land.

@Adityakk9031
Adityakk9031 force-pushed the fix/ollama-semantic-edge-backfill-and-token-cap branch from 5f36393 to 82e973f Compare November 5, 2026 18:41
@Adityakk9031

Copy link
Copy Markdown
Contributor Author

Addressed the review feedback:

  • Added import warnings in graphify/llm.py so un-attributable edges emit a RuntimeWarning without raising NameError or dropping the chunk.
  • Added est_corpus_parallel_edge_unattributable_warning in ests/test_chunking.py verifying that un-attributable edges trigger the warning and keep the chunk.
  • Rebased cleanly onto upstream \�8\ and integrated with _openai_max_tokens_kwarg\ and current _call_openai_compat.
  • All tests pass.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

2 participants