Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(ingestion): prefer the unique nearest COBOL mapset on ties (#4743)

ANO3457774b2c075f5f
diff --git a/packages/core/src/repowise/core/ingestion/framework_edges/mainframe.py b/packages/core/src/repowise/core/ingestion/framework_edges/mainframe.py
index 3eecd0cdb..0b9be3e5e 100644
--- a/packages/core/src/repowise/core/ingestion/framework_edges/mainframe.py
+++ b/packages/core/src/repowise/core/ingestion/framework_edges/mainframe.py
@@ -13,7 +13,8 @@ COBOL programs to BMS mapsets and to the programs their TRANSIDs run.
when every definition of T names the same one.
A job member is an entry point. Names fold case; two equally near candidates
-bind neither, and a utility (``IDCAMS``, ``SORT``) the repo does not hold links
+bind neither unless exactly one nearest mapset is under a ``cobol`` directory.
+A utility (``IDCAMS``, ``SORT``) the repo does not hold links
to nothing. A program returning to its own transaction gets no edge.
"""
@@ -123,7 +124,30 @@ def _add_mainframe_edges(
code = code_view(raw)
for use in mapset_uses(code):
for name in use.targets:
- target = nearest(mapsets.get(name, []), path)
+ candidates = mapsets.get(name, [])
+ target = nearest(candidates, path)
+ if target is None and candidates:
+ # Apply the language preference only within the nearest tie.
+ ref = PurePosixPath(path).parts
+ scores = {}
+ for candidate in candidates:
+ shared = 0
+ for left, right in zip(PurePosixPath(candidate).parts, ref, strict=False):
+ if left != right:
+ break
+ shared += 1
+ scores[candidate] = shared
+ closest = max(scores.values())
+ cobol = [
+ candidate
+ for candidate in candidates
+ if scores[candidate] == closest
+ and "cobol" in {
+ part.lower() for part in PurePosixPath(candidate).parts[:-1]
+ }
+ ]
+ if len(cobol) == 1:
+ target = cobol[0]
if target:
count += _file_edge(graph, path, target, set())
for use in transid_uses(code):
diff --git a/tests/unit/ingestion/test_mainframe_edges.py b/tests/unit/ingestion/test_mainframe_edges.py
index 8cd691792..5530a168b 100644
--- a/tests/unit/ingestion/test_mainframe_edges.py
+++ b/tests/unit/ingestion/test_mainframe_edges.py
@@ -171,6 +171,44 @@ def test_csd_definitions_in_jcl_in_stream_data() -> None:
assert [s.program for s in member.steps] == ["DFHCSDUP", "IEFBR14"]
[email protected]("operation", ["SEND", "RECEIVE"])
[email protected]("directory", ["cobol", "COBOL"])
+def test_cobol_mapset_wins_a_nearest_tie(operation: str, directory: str) -> None:
+ program = "sources/app/SBANK10P.cbl"
+ preferred = f"sources/bms/{directory}/COSGN00.bms"
+ graph = _edges({
+ program: ("cobol", _program(b"SBANK10P", (
+ f" EXEC CICS {operation} MAP('COSGN0A') "
+ "MAPSET('COSGN00') END-EXEC.\n"
+ ).encode())),
+ preferred: ("bms", MAPSET),
+ "sources/bms/pli/COSGN00.bms": ("bms", MAPSET),
+ })
+ assert set(graph.edges) == {(program, preferred)}
+
+
[email protected](("candidates", "expected"), [
+ (["sources/bms/cobol/a/COSGN00.bms", "sources/bms/cobol/b/COSGN00.bms"], None),
+ (["sources/bms/mycobol/COSGN00.bms", "sources/bms/pli/COSGN00.bms"], None),
+ (["sources/bms/pli/COSGN00.bms"], "sources/bms/pli/COSGN00.bms"),
+ (["sources/app/maps/pli/COSGN00.bms", "lib/cobol/COSGN00.bms"],
+ "sources/app/maps/pli/COSGN00.bms"),
+ (["sources/bms/a/COSGN00.bms", "sources/bms/b/COSGN00.bms",
+ "lib/cobol/COSGN00.bms"], None),
+ (["sources/bms/cobol/COSGN00.bms", "sources/bms/pli/COSGN00.bms",
+ "lib/cobol/COSGN00.bms"], "sources/bms/cobol/COSGN00.bms"),
+])
+def test_mapset_preference_preserves_nearness_and_ambiguity(
+ candidates: list[str], expected: str | None,
+) -> None:
+ program = "sources/app/SBANK10P.cbl"
+ graph = _edges({
+ program: ("cobol", PROGRAM),
+ **{path: ("bms", MAPSET) for path in candidates},
+ })
+ assert set(graph.edges) == ({(program, expected)} if expected else set())
+
+
def test_defined_programs_are_roots_linked_from_their_definition() -> None:
graph = _edges(
{