diff --git a/gemini-extension.json b/gemini-extension.json index 61a14197f..9cfece080 100644 --- a/gemini-extension.json +++ b/gemini-extension.json @@ -1,6 +1,6 @@ { "name": "axiomcode", - "version": "0.2.3", + "version": "0.2.4", "description": "Ask your repository how its code connects: who calls this, what breaks if I change it, which tests an edit reaches, how A reaches B. Answers come from a resolved call graph and are verified against it; nothing is guessed.", "contextFileName": "plugins/axiomcode/AGENTS.md", "mcpServers": { diff --git a/graph/python/engine/projections/bindings.dl b/graph/python/engine/projections/bindings.dl index baa19e72c..1d64485bb 100644 --- a/graph/python/engine/projections/bindings.dl +++ b/graph/python/engine/projections/bindings.dl @@ -50,9 +50,20 @@ binding_is_namespace("lib", h) :- lib_py_binding(_, _, _, _, _, _, _, _, _, _, _ // bindingCount is the number of DISTINCT binding sites for this name in this scope. // == 1 is the precondition for treating a name as single-valued (the alias case // `_Aliased = Base`); > 1 means the name is rebound and a single-value inference is -// unsound. Consumed by resolution/name-resolution.dl. -binding_rebinding("client", n, first, last, h) :- py_binding(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, n, first, last, _, _, _, _, _, _, _, _, _, _, h). -binding_rebinding("lib", n, first, last, h) :- lib_py_binding(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, n, first, last, _, _, _, _, _, _, _, _, _, _, h). +// unsound. +// This is the RAW per-row count. It cannot see a write made from a NESTED scope through +// `nonlocal`: that write is a separate NONLOCAL row with its own count, so the enclosing +// cell's row still says 1. resolution/name-resolution.dl derives binding_rebinding from +// this and adds those writes; every single-write guard reads binding_rebinding. Only the +// LEGB walk reads the raw count, because it is what binding_rebinding is derived from. +binding_rebinding_raw("client", n, first, last, h) :- py_binding(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, n, first, last, _, _, _, _, _, _, _, _, _, _, h). +binding_rebinding_raw("lib", n, first, last, h) :- lib_py_binding(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, n, first, last, _, _, _, _, _, _, _, _, _, _, h). + +// ── binding_is_assigned(Prov, BindingHash) ─────────────────────────────────── +// Symbol.is_assigned(): the name is a store target in this scope. Separates a +// `nonlocal fn` that rebinds the enclosing cell from one that only reads it. +binding_is_assigned("client", h) :- py_binding(_, _, _, _, _, _, _, _, _, _, "true", _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, h). +binding_is_assigned("lib", h) :- lib_py_binding(_, _, _, _, _, _, _, _, _, _, "true", _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, h). // ── binding_annotation(Prov, DeclaredTypeName, DeclaredBaseType, PotentialQName, BindingHash) ── // "" on 68.2% of parameters, which is exactly why declared-type receiver typing — diff --git a/graph/python/engine/resolution/name-resolution.dl b/graph/python/engine/resolution/name-resolution.dl index b9345eb12..7efac4e75 100644 --- a/graph/python/engine/resolution/name-resolution.dl +++ b/graph/python/engine/resolution/name-resolution.dl @@ -438,7 +438,7 @@ binding_lookup(p, b, b2) :- binding_owner(p, mod, _, b), module_init(p, _, modScope, mod), binding_decl(p, n, modScope, _, _, b2), b2 != b, - binding_rebinding(p, cnt, _, _, b2), cnt != "0". + binding_rebinding_raw(p, cnt, _, _, b2), cnt != "0". // (c) FREE / NONLOCAL — nearest enclosing scope that binds the name. Candidate set // first, then the max-depth filter in the next stratum. binding_free_candidate(p, b, b2, d) :- @@ -446,7 +446,7 @@ binding_free_candidate(p, b, b2, d) :- binding_decl(p, n, s, _, _, b), scope_ancestor(p, s, anc), binding_decl(p, n, anc, _, _, b2), - binding_rebinding(p, cnt, _, _, b2), cnt != "0", + binding_rebinding_raw(p, cnt, _, _, b2), cnt != "0", scope_depth(p, dstr, _, anc), d = to_number(dstr). binding_free_nearest_depth(p, b, dmax) :- @@ -456,6 +456,29 @@ binding_lookup(p, b, b2) :- binding_free_nearest_depth(p, b, dmax), binding_free_candidate(p, b, b2, dmax). +// ── binding_nonlocal_write / binding_rebinding: writes from a nested scope ── +// symtable counts a name's binding sites per scope, so a cell rebound from a nested +// function through `nonlocal` has two rows, each with its own count: the cell's own +// (1, `fn = first`) and the nested NONLOCAL row (`fn = second`). Read alone, the cell's +// row says the name is written once, and every single-write guard then commits to the +// first write. #381: `fn = first; def swap(): nonlocal fn; fn = second; swap(); fn()` +// came out known_edge -> first, a function that cannot run there, and `second` was the +// target of no edge. +// A NONLOCAL row that is ASSIGNED is a write to the cell its LEGB walk lands on. One that +// only reads (`nonlocal fn` and no store) is not, and leaves the count alone. +binding_nonlocal_write(p, cell, nb) :- + binding_decl(p, _, _, "NONLOCAL", _, nb), + binding_is_assigned(p, nb), + binding_lookup(p, nb, cell), cell != nb. +binding_rebinding(p, n, first, last, b) :- + binding_rebinding_raw(p, n, first, last, b), + !binding_nonlocal_write(p, b, _). +binding_rebinding(p, total, first, last, b) :- + binding_rebinding_raw(p, n, first, last, b), + binding_nonlocal_write(p, b, _), + k = count : { binding_nonlocal_write(p, b, _) }, + total = to_string(to_number(n) + k). + // ── binding_lookup_unresolved(Prov, ReferenceBinding, Name, BindingKind) ───── // A reference-side binding whose LEGB walk found no declaration. THE HONEST FRONTIER: // a builtin (`super`, `len`, `object`), a name from `from x import *`, or a genuine diff --git a/graph/python/souffle/decls_all.dl b/graph/python/souffle/decls_all.dl index db011f111..b216cc982 100644 --- a/graph/python/souffle/decls_all.dl +++ b/graph/python/souffle/decls_all.dl @@ -6,7 +6,10 @@ .decl binding_decl(c0:symbol,c1:symbol,c2:symbol,c3:symbol,c4:symbol,c5:symbol) .decl binding_predicates(c0:symbol,c1:symbol,c2:symbol,c3:symbol,c4:symbol,c5:symbol,c6:symbol) .decl binding_is_namespace(c0:symbol,c1:symbol) +.decl binding_rebinding_raw(c0:symbol,c1:symbol,c2:symbol,c3:symbol,c4:symbol) .decl binding_rebinding(c0:symbol,c1:symbol,c2:symbol,c3:symbol,c4:symbol) +.decl binding_is_assigned(c0:symbol,c1:symbol) +.decl binding_nonlocal_write(c0:symbol,c1:symbol,c2:symbol) .decl binding_annotation(c0:symbol,c1:symbol,c2:symbol,c3:symbol,c4:symbol) .decl binding_declared_ref(c0:symbol,c1:symbol,c2:symbol) .decl binding_declared_nominal(c0:symbol,c1:symbol,c2:symbol) diff --git a/graph/test/python/cases/44-nonlocal-rebind/src/main.py b/graph/test/python/cases/44-nonlocal-rebind/src/main.py new file mode 100644 index 000000000..69cd26958 --- /dev/null +++ b/graph/test/python/cases/44-nonlocal-rebind/src/main.py @@ -0,0 +1,56 @@ +# A closure cell rebound from a nested scope through `nonlocal` is written twice, so the +# call through it must not commit to the first write alone (#381). + + +def first() -> str: + return "first" + + +def second() -> str: + return "second" + + +def nonlocal_rebind() -> str: + fn = first + + def swap() -> None: + nonlocal fn + fn = second + + swap() + return fn() + + +def nonlocal_read_only() -> str: + # A nested scope that only READS the cell through `nonlocal` is not a write; the call + # still has exactly one value and stays a committed edge. + fn = first + + def peek() -> str: + nonlocal fn + return fn.__name__ + + peek() + return fn() + + +def unrelated_nested_name() -> str: + # A nested scope that assigns its OWN local of the same name does not touch the cell. + fn = first + + def shadow() -> None: + fn = second + fn() + + shadow() + return fn() + + +def main() -> None: + nonlocal_rebind() + nonlocal_read_only() + unrelated_nested_name() + + +if __name__ == "__main__": + main() diff --git a/graph/test/python/expected/44-nonlocal-rebind.edges b/graph/test/python/expected/44-nonlocal-rebind.edges new file mode 100644 index 000000000..f817f75fa --- /dev/null +++ b/graph/test/python/expected/44-nonlocal-rebind.edges @@ -0,0 +1,16 @@ +ambiguous_unknown SIMPLE_CALL main.nonlocal_rebind -> - +callback_registered METHOD_REFERENCE main.nonlocal_read_only -> main.first +callback_registered METHOD_REFERENCE main.nonlocal_rebind -> main.first +callback_registered METHOD_REFERENCE main.nonlocal_rebind..swap -> main.second +callback_registered METHOD_REFERENCE main.unrelated_nested_name -> main.first +callback_registered METHOD_REFERENCE main.unrelated_nested_name..shadow -> main.second +known_edge SIMPLE_CALL main. -> main.main +known_edge SIMPLE_CALL main.main -> main.nonlocal_read_only +known_edge SIMPLE_CALL main.main -> main.nonlocal_rebind +known_edge SIMPLE_CALL main.main -> main.unrelated_nested_name +known_edge SIMPLE_CALL main.nonlocal_read_only -> main.first +known_edge SIMPLE_CALL main.nonlocal_read_only -> main.nonlocal_read_only..peek +known_edge SIMPLE_CALL main.nonlocal_rebind -> main.nonlocal_rebind..swap +known_edge SIMPLE_CALL main.unrelated_nested_name -> main.first +known_edge SIMPLE_CALL main.unrelated_nested_name -> main.unrelated_nested_name..shadow +known_edge SIMPLE_CALL main.unrelated_nested_name..shadow -> main.second diff --git a/graph/test/python/expected/44-nonlocal-rebind.entries b/graph/test/python/expected/44-nonlocal-rebind.entries new file mode 100644 index 000000000..276e39fa5 --- /dev/null +++ b/graph/test/python/expected/44-nonlocal-rebind.entries @@ -0,0 +1 @@ +── entry_point (0) ── diff --git a/graph/test/python/expected/44-nonlocal-rebind.framework b/graph/test/python/expected/44-nonlocal-rebind.framework new file mode 100644 index 000000000..b9cbca067 --- /dev/null +++ b/graph/test/python/expected/44-nonlocal-rebind.framework @@ -0,0 +1,5 @@ +── framework_edge (0) ── +── framework_unjoined (0) ── +── remote_edge (0) ── +── remote_unserved (0) ── +── remote_unsent (0) ── diff --git a/graph/test/python/expected/44-nonlocal-rebind.tiers b/graph/test/python/expected/44-nonlocal-rebind.tiers new file mode 100644 index 000000000..8356c96cc --- /dev/null +++ b/graph/test/python/expected/44-nonlocal-rebind.tiers @@ -0,0 +1,24 @@ +distinct call sites emitted: 16 + +--- by tier: edge ROWS, and the distinct SITES they cover --- + 1 rows 1 sites ambiguous_unknown + 5 rows 5 sites callback_registered + 10 rows 10 sites known_edge + +--- edge rows by call kind --- + 5 METHOD_REFERENCE + 11 SIMPLE_CALL + +--- unresolved reasons --- + 1 no_rule + +--- the engine's own conservation ledger --- + 11 _total_sites + 1 ambiguous_unknown + 10 known_edge + +--- reconciling rows against the conserved site count --- + edge rows 16 + minus extra rows from multi-target sites 0 + = tier/site pairs 16 + engine's conserved site total 11 diff --git a/package.json b/package.json index c1fd43e95..144dabe85 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@axiomcode/code-graph", - "version": "0.2.3", + "version": "0.2.4", "description": "AxiomCode Graph: a resolved call graph of your codebase grounded in formal methods, so you and your coding agents can see who calls what, what a change breaks, and which tests it reaches.", "repository": { "type": "git", @@ -31,11 +31,11 @@ ], "license": "FSL-1.1-Apache-2.0", "optionalDependencies": { - "@axiomcode/engine-darwin-arm64": "0.2.3", - "@axiomcode/engine-darwin-x64": "0.2.3", - "@axiomcode/engine-linux-x64": "0.2.3", - "@axiomcode/engine-linux-arm64": "0.2.3", - "@axiomcode/engine-win32-x64": "0.2.3" + "@axiomcode/engine-darwin-arm64": "0.2.4", + "@axiomcode/engine-darwin-x64": "0.2.4", + "@axiomcode/engine-linux-x64": "0.2.4", + "@axiomcode/engine-linux-arm64": "0.2.4", + "@axiomcode/engine-win32-x64": "0.2.4" }, "bin": { "axiomcode": "bin/axiomcode.js" diff --git a/plugins/axiomcode/.claude-plugin/plugin.json b/plugins/axiomcode/.claude-plugin/plugin.json index a45810eee..55c135b5e 100644 --- a/plugins/axiomcode/.claude-plugin/plugin.json +++ b/plugins/axiomcode/.claude-plugin/plugin.json @@ -1,7 +1,7 @@ { "name": "axiomcode", "description": "Ask your repository how its code connects: who calls this, what breaks if I change it, which tests an edit reaches, how A reaches B. Answers come from a resolved call graph and are verified against it; nothing is guessed.", - "version": "0.2.3", + "version": "0.2.4", "author": { "name": "AxiomCode" } diff --git a/plugins/axiomcode/.codex-plugin/plugin.json b/plugins/axiomcode/.codex-plugin/plugin.json index 29c2ba521..c5a0c074c 100644 --- a/plugins/axiomcode/.codex-plugin/plugin.json +++ b/plugins/axiomcode/.codex-plugin/plugin.json @@ -1,6 +1,6 @@ { "name": "axiomcode", - "version": "0.2.3", + "version": "0.2.4", "description": "Ask your repository how its code connects: who calls this, what breaks if I change it, which tests an edit reaches, how A reaches B. Answers come from a resolved call graph and are verified against it; nothing is guessed.", "author": { "name": "AxiomCode", "url": "https://github.com/AxiomCodeAI/axiomcodegraph" }, "homepage": "https://github.com/AxiomCodeAI/axiomcodegraph", diff --git a/plugins/axiomcode/.cursor-plugin/plugin.json b/plugins/axiomcode/.cursor-plugin/plugin.json index fadd636d0..a797bda89 100644 --- a/plugins/axiomcode/.cursor-plugin/plugin.json +++ b/plugins/axiomcode/.cursor-plugin/plugin.json @@ -1,6 +1,6 @@ { "name": "axiomcode", - "version": "0.2.3", + "version": "0.2.4", "description": "Ask your repository how its code connects: who calls this, what breaks if I change it, which tests an edit reaches, how A reaches B. Answers come from a resolved call graph and are verified against it; nothing is guessed. Java, TypeScript, Python, JavaScript.", "author": { "name": "AxiomCode" }, "homepage": "https://github.com/AxiomCodeAI/axiomcodegraph",