Skip to content

fix(bash): resolve caller-defined variable bases in sourced scripts - #5592

Closed
m3lonxd wants to merge 2 commits into
Graphify-Labs:v8from
m3lonxd:fix/bash-sourced-caller-var-5563
Closed

m3lonxd wants to merge 2 commits into
Graphify-Labs:v8from
m3lonxd:fix/bash-sourced-caller-var-5563

Conversation

@m3lonxd

@m3lonxd m3lonxd commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Closes #5563

What does this PR do?

Propagates statically tracked Bash directory variables (bash_var_bases) from loading scripts to sourced scripts (directly or transitively) so . "$ROOT/lib/beta.sh" and source "$(dirname "$VAR")/lib/x.sh" resolve against the caller's variable binding when the sourced script does not define VAR itself, restoring both imports_from and downstream calls edges.

Changes

  • graphify/extractors/bash.py:
    • Extended _bash_assignment_base to resolve leading tracked $VAR/sub assignments when VAR is already in var_bases.
    • Recorded top-level assignments (including export/declare/readonly declaration_command nodes) into portable in-root bash_var_bases, tracked explicit unmodeled overwrites in bash_assigned_unknown_vars (preserving self-default fallbacks like ${ROOT:-...}), and recorded deferred variable-based source entries into bash_var_sources (alongside optional fallback_target_path when a script-dir guess existed).
  • graphify/symbol_resolution.py:
    • Added bounded fixed-point propagation in resolve_bash_source_edges using _safe_bash_base_identity and _resolve_bash_var_source_target to resolve inherited $VAR/x and $(dirname "$VAR")/x sources (INFERRED, 0.85 confidence), feed resolved targets into sourced_files for downstream calls, and supersede wrong script-dir fallback decoys when a loader defines VAR.
  • graphify/cache.py:
    • Bumped _AST_CACHE_SCHEMA (58 -> 59) so stale cached bash payloads re-extract cleanly and anchor bash_var_bases portably via $graphify-root$.
  • tests/test_extract.py, tests/test_symbol_resolution.py:
    • Added regression test test_extract_bash_sourced_file_resolves_caller_var_5563 (proven failing on pre-fix v8) plus tests covering transitive loader propagation, export ROOT=..., Form 3 $(dirname "$VAR"), Form 4 $VAR/../lib, conflicting-loader fail-closed behavior, local overrides and dynamic overwrite guards, decoy superseding, standalone fallback preservation, out-of-tree/UNC traversal guards, cycles, malformed inputs, and relocated AST cache portability.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Verification & Invariants

  • Deterministic output: sorted traversal order for variable names, loader sets, and resolved targets.
  • Fail-closed behavior: conflicting VAR values across multiple loaders emit nothing; dynamic local overwrites block caller inheritance; out-of-tree, .. ceiling escapes, and UNC paths never probe or resolve.
  • Provenance preserved: loader-resolved variable imports are emitted as INFERRED (confidence_score=0.85), keeping extracted facts distinct from dynamic-variable inference.

How was this tested?

  • uv run --frozen pytest tests/test_extract.py -k '5563 or bash' -q (71 passed, 309 deselected)
  • uv run --frozen pytest tests/test_symbol_resolution.py tests/test_cache.py tests/test_node_id_canonical.py tests/test_affected_cli.py tests/test_extractors_registry.py -q (157 passed, 1 skipped Windows-only test)
  • uv run --frozen ruff check . (passed)
  • uv run --frozen pyright graphify/extractors/bash.py tests/test_symbol_resolution.py (0 errors)
  • uv run --frozen python -m tools.skillgen --check (134 artifacts OK)
  • uv run --frozen bandit -r graphify/extractors/bash.py graphify/symbol_resolution.py graphify/cache.py -ll -q (0 issues)

Graphify-specific checklist

  • Narrow, atomic change targeting only the issue scope
  • Regression test reproduces the pre-fix failure
  • _AST_CACHE_SCHEMA bumped cleanly (58 -> 59)
  • Co-Authored-By trailer included

Propagate statically known bash variable base directories (`bash_var_bases`)
from loading scripts to sourced libraries that reference `$VAR/x` or
`$(dirname "$VAR")/x` without reassigning `VAR`, resolving both `imports_from`
(INFERRED, 0.85 confidence) and downstream cross-file `calls` edges via a
cycle-safe fixed-point pass while staying fail-closed on conflicting loader
values or out-of-root paths. Bump AST cache schema to 59 so cached bash
extractions re-run portably across roots.

Fixes Graphify-Labs#5563

Co-Authored-By: Gemini <[email protected]>
@m3lonxd
m3lonxd requested a review from safishamsi as a code owner November 28, 2026 13:17
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @m3lonxd. 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.11 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 review View in Graphify

Nothing blocking · 12 possible issues to check · Merge confidence 5/5

12 possible issues to check · High impact

The change tracks Bash variable-directory assignments and snapshots their values at source sites.

12 possible issues we couldn't confirm

They don't change the score.

What this review covered
  • A changed file's diff was too long to show in full, so the review may not have seen every changed line in it.
  • 6 of 6 changed files inspected. This is scope, not a correctness guarantee.
Code graph · 4571 functions depend on this change
  • 5 functions became more coupled. Largest: extract() (1424 callers), extract_bash() (52 callers), resolve_bash_source_edges() (17 callers).
  • 63 of 652 test files are affected by this change.
  • Gate, test list and every flagged function: in the Graphify check.

Reviewed 7f61d7e · 13 earlier findings not re-checked

Comment thread graphify/extractors/bash.py Outdated
Comment on lines +746 to +747
for _stmt in root.children:
if _stmt.type == "variable_assignment":

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2  Later variable assignments can misresolve earlier sourced scripts

A loader that changes a path variable after sourcing a library can make the library's import point to the wrong file.

Capture tracked path-variable values when each source runs in bash.py.

Evidence · checked against the code

Expected: Resolve the library's source using the variable's value when the loader sourced the library.
Actually: Resolve it using the loader's later tracked assignment, which can select a different file and produce incorrect import and sourced-call edges.

  • Independent source review: Checked the source handling for tracked and untracked variables and the resolver's local-variable and blocked-variable checks. They do not preserve assignment state at each source location: a local tracked base is used directly, while a library without a local assignment receives the loader's precomputed base. The inspected tests cover tracked paths and resolver robustness, but not this before-source/after-source ordering case.
  • This concerns statically tracked top-level assignments and source paths; dynamic shell control flow can create additional ordering cases.
  • Some lookups asked for files or lines that do not exist; the review was told and carried on.
Prompt to fix with AI
In graphify/extractors/bash.py, _record_top_level_assignment (lines 746-747):
Problem: Later variable assignments can misresolve earlier sourced scripts
Expected: Resolve the library's source using the variable's value when the loader sourced the library.
Actually: Resolve it using the loader's later tracked assignment, which can select a different file and produce incorrect import and sourced-call edges.
Fix it at the cause, and add a test that fails without the fix.

Graphify · Checked against the code · View in Graphify

Track top-level bash variable assignments in source order during walk() so each top-level `source`/`.` statement resolves using and captures the `var_bases` and `assigned_unknown_vars` snapshot active at that exact line, while deferring function-body `source` resolution until top-level assignments finish and guarding tracked-variable `source` probes with `_safe_to_probe`.

Propagate per-source-site snapshots through `resolve_bash_source_edges()` so earlier sourced libraries receive the variable bases active when they were sourced rather than later reassignments in the same loader, and add regression, cache-portability, and UNC probe-guard tests.

Fixes P2 review finding on Graphify-Labs#5592 (for Graphify-Labs#5563).

Co-Authored-By: Gemini <[email protected]>
@safishamsi

Copy link
Copy Markdown
Member

Thanks @m3lonxd — the caller-defined-variable-base resolution (issue #5563) is a real gap and your implementation is well-tested in isolation (15 tests pass). We went a different way for v0.9.123, though: #5597 (tracked-dir invocation) and #5589 (cross-library sibling-sourced calls) landed as surgical fixes, and this PR is a ~1500-line rewrite that hard-conflicts with those plus #5568/#5570/#5578 and duplicates the schema bump. Could you bring the distinct cross-file caller-var feature back as a smaller PR rebased on top of #5589's resolver? Closing this form, but #5563 stays open — the feature is wanted, just in a smaller shape.

@safishamsi safishamsi closed this Nov 28, 2026
safishamsi pushed a commit that referenced this pull request Nov 30, 2026
…5563)

Surgical resubmission of #5592 layered directly on #5589 cross-library
sibling-sourced resolution and #5597 variable tracking.

- Tracks caller assignments in source order at program level
- Propagates caller-defined variable bases across sourcing chains
- Fails closed on UNC, out-of-root, cycles, and multi-caller conflicts
- Supersedes local fallback decoy edges

Fixes #5563

(cherry picked from commit cb00f8a)
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.

Bash extractor: a sourced file's . "$ROOT/lib/x.sh" gets no edge when ROOT is set by the script that loads it

2 participants