Skip to content

Commit 12474de

Browse files
authored
Merge pull request #133 from FSoft-AI4Code/fix/updater-doc-comments
updater: count doc-comment changes as component changes
2 parents 966f4cd + 6a9929d commit 12474de

3 files changed

Lines changed: 37 additions & 5 deletions

File tree

‎codewiki/src/be/updater/graph_diff.py‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,21 @@ def normalize_body(text: str | None) -> str:
3737
return _WS.sub(" ", (text or "").strip())
3838

3939

40+
def documented_source(node: Node) -> str:
41+
"""The component's code with its doc comment in front, when the code does not already hold it.
42+
43+
Languages like Rust keep doc comments (`///`) above the item, outside ``source_code``. Without
44+
them, a change that only documents behaviour would be invisible to the updater.
45+
"""
46+
code = node.source_code or ""
47+
doc = (node.docstring or "").strip()
48+
if doc and doc not in code:
49+
return f"{doc}\n{code}"
50+
return code
51+
52+
4053
def body_hash(node: Node) -> str:
41-
return hashlib.sha1(normalize_body(node.source_code).encode("utf-8")).hexdigest()
54+
return hashlib.sha1(normalize_body(documented_source(node)).encode("utf-8")).hexdigest()
4255

4356

4457
def signature(node: Node) -> dict[str, Any]:
@@ -78,8 +91,8 @@ def truncate_to_tokens(text: str, max_tokens: int) -> str:
7891

7992

8093
def unified_body_diff(old: Node | None, new: Node | None, label: str) -> str:
81-
old_lines = (old.source_code or "").splitlines(keepends=True) if old else []
82-
new_lines = (new.source_code or "").splitlines(keepends=True) if new else []
94+
old_lines = documented_source(old).splitlines(keepends=True) if old else []
95+
new_lines = documented_source(new).splitlines(keepends=True) if new else []
8396
diff = difflib.unified_diff(
8497
old_lines, new_lines, fromfile=f"a/{label}", tofile=f"b/{label}", n=2
8598
)

‎codewiki/src/be/updater/prompts.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from codewiki.src.be.dependency_analyzer.models.core import Node
1414
from codewiki.src.be.prompt_template import _fence_language, _format_module_tree_str
1515
from codewiki.src.be.updater.change_report import LeafReport
16-
from codewiki.src.be.updater.graph_diff import GraphDiff
16+
from codewiki.src.be.updater.graph_diff import GraphDiff, documented_source
1717

1818
VERDICT_VALUES = ("no-op", "patch", "rewrite")
1919

@@ -292,7 +292,7 @@ def render_leaf_components(
292292
node = graph.get(cid)
293293
if node is None or cid not in changed or not node.source_code:
294294
continue
295-
code = node.source_code
295+
code = documented_source(node)
296296
if budget <= 0:
297297
lines.append(f"\n(code of {cid} omitted for length; use read_code_components)")
298298
continue

‎tests/test_updater_graph_diff.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
CLASS_RENAMED,
1313
DIFF_TRUNCATED_MARKER,
1414
body_hash,
15+
documented_source,
1516
diff_graphs,
1617
)
1718
from codewiki.src.be.updater.options import UpdateOptions
@@ -83,3 +84,21 @@ def test_diff_truncation():
8384
new = {"a.py::f": node("a.py::f", "function", big_new)}
8485
d = diff_graphs(old, new, UpdateOptions(max_diff_tokens=50))
8586
assert DIFF_TRUNCATED_MARKER in d.records["a.py::f"].diff
87+
88+
89+
def test_doc_comment_only_change_is_a_body_change():
90+
# Rust keeps `///` doc comments above the item, outside source_code.
91+
code = "pub fn summarize(m: &Module) -> String {\n call(m)\n}\n"
92+
a = node("src/llm.rs::summarize", "function", code)
93+
b = node("src/llm.rs::summarize", "function", code)
94+
b.docstring = "/// Sends only names and imports, with temperature 0 and at most 120 tokens."
95+
d = diff_graphs({a.id: a}, {b.id: b})
96+
assert d.body == {a.id}
97+
assert "+/// Sends only names and imports, with temperature 0" in d.records[a.id].diff
98+
99+
100+
def test_docstring_inside_source_is_not_counted_twice():
101+
code = 'def g():\n """Return one."""\n return 1\n'
102+
a = node("f.py::g", "function", code)
103+
a.docstring = "Return one."
104+
assert documented_source(a) == code

0 commit comments

Comments
 (0)