Skip to content

COBOL: record each PROCEDURE DIVISION's entry code as a calls-only unit (#4302) - #4323

Merged
squid-protocol merged 1 commit into
mainfrom
fix/4302-cobol-procedure-entry-unit
Oct 4, 2026
Merged

squid-protocol merged 1 commit into
mainfrom
fix/4302-cobol-procedure-entry-unit

Conversation

@squid-protocol

Copy link
Copy Markdown
Owner

Fixes #4302

What was broken

A COBOL program can run statements right after PROCEDURE DIVISION., before any paragraph or section header. Batch mains do this all the time: open the files, loop, close, GOBACK. Mode A only creates units at headers, so that code belonged to no unit. Its PERFORM/CALL/GO TO targets were recorded nowhere, and the paragraphs it drives had no caller. COBOL also never got a synthetic top-level slice the way Python gets __global_context__.

The fix

I chose the second shape the acceptance criteria offer: a synthetic calls-only slice in synthetic_unit_data, like Python's module-level bucket.

For each PROCEDURE DIVISION header, the entry code runs from the header's separator period (a USING … / RETURNING … clause may span lines) to the first unit header after it. If the program ends first, or the next program's header comes first (#4301's _COBOL_UNIT_END_RE), it stops there. The entry code becomes a calls-only __global_context__ unit:

  • it is marked is_synthetic_slice and calls_only, so it stays out of the function population and function_data;
  • its weights are zeroed, so file magnitude and impact don't change;
  • it carries calls_out_to and transfers_to, and the call resolver turns those into edges;
  • nested and sibling programs each get their own bucket, with a distinct start_line;
  • a bucket is kept only when it has at least one edge, since it exists to carry them. A DECLARATIVES opening or a DISPLAY … STOP RUN main produces no row.

Why not a real unit: a real unit would add a function to every such program. That changes the function population, the answer key's units field and the dead-code census. The calls-only slice records the edges, which is what the issue is about, without inventing a paragraph.

The code is in detector._cobol_procedure_entry_units, called right after _slice_by_labels for COBOL.

Evidence

  • REPRO1: there was no row before. Now __global_context__ L7-10 has calls_out_to = [ENTRY-TARGET, MAIN-PARA].
  • CBACT01C: the bucket covers L70-87 with [0000-ACCTFILE-OPEN, 1000-ACCTFILE-GET-NEXT, 9000-ACCTFILE-CLOSE]. All three paragraphs go from fan-in 0 to 1 in the golden output.
  • Corpus (language-crucible v1.7.0, all 628 COBOL/PL/I files): 44 buckets in 28 files, carrying 161 calls and 2 GO TO transfers.
    • Every file the issue names is covered: CBACT01C-04C, CBCUS01C, CBTRN02C, CBTRN03C, ZECS000, ZECS003, ZECSPLT, ECS001, MESSAGES, BALANCE, HISTORY, CASH00, EMPPAY, DEPTPAY, TESTPRG, FileCopy and tutorial.cob.
    • There are a few more real ones: CSUTLDTC, lsp-fixtures CALL/HELLO-WORLD/ADSORT/HOVER_PARA (a GO TO), WS88LEVEL and numeric-dump.
    • gnucobol_internals/syn_misc.at adds 17. It is an autotest file with many embedded test programs, and its entry regions run into the harness text (AT_SETUP([... CALL USING ...])).
  • No existing unit changed: span, name and edges are identical in all 628 files.
  • New tests in tests/extraction/languages/test_cobol_procedure_entry_4302.py: 6 tests, 5 of which fail on main. They cover:
    • the verbatim repro;
    • the synthetic/weightless/population checks;
    • the CBACT01C main line with an inline PERFORM UNTIL;
    • a multi-line USING header plus GO TO → transfers_to;
    • sibling plus nested programs, one bucket each, with the outer one stopping at the nested header;
    • no bucket when a paragraph comes first, for DECLARATIVES, or when there are no edges.

Golden-master diff (both legs regenerated: 830 diffs each, same paths)

  • Fan In on 23 mainframe files: the paragraphs the main lines PERFORM.
  • Import Edges on 2 files (lsp CALL.cbl, Z-SMS MESSAGES.cbl): new fcall: file edges from entry-code CALLs.
  • PageRank: 51 mainframe files (|Δ| ≤ 1.1e-5) and 70 others (|Δ| ≤ 3e-6). PageRank is computed over the whole graph, so new edges shift it everywhere.
  • Repository fingerprint / drift on 19 mainframe files follow the fan-in and PageRank changes.
  • Graph SQLite Row Counts/Functions goes from 49,030 to 49,057. The graph DB keeps a function node per (file, caller) for synthetic callers too, as it does for Python's bucket.
  • CASH00.cbl:
    • Variable Reassignments (weighted) goes from 93 to 91, Amplified Cascading Flux from 23 to 22, and Structural Magnitude from 122.78 to 120.78.
    • Cause: correlate_scoped uses unit ranges, so a state mutation in the entry code can no longer pair with a branch inside the first paragraph within 150 chars.
    • The detector's file-level equations for CASH00 are byte-identical before and after.
    • The cash-account directory magnitude, the global cobol impact (−2.0), and the layout X/Z of 3 cash-account files plus one neighbouring bms file follow from that.
  • pytest -m golden_crucible passes in both venvs.

Ground-truth ledger

ground_truth_ledger.py check is clean. The answer key scores function_data units, which this PR does not change, so there are no ledger changes.

Local checks

  • tests/extraction, tests/core_engine, tests/cobol_mainframe: 13,156 passed
  • mypy_audit --ci, ruff_audit --ci (0.16.0), dead_key_audit --ci: nothing new

Noted, not in scope

🤖 Generated with Claude Code

…it (#4302)

Statements between `PROCEDURE DIVISION.` and the first paragraph or
section header -- a batch program's main line -- belonged to no unit, so
the top of the program's call tree (PERFORM/CALL/GO TO) was recorded
nowhere and the paragraphs it drives had no caller.

Each PROCEDURE DIVISION's entry code (from the header's separator period
to the first unit header, or the program's end / next program's header)
now becomes a calls-only `__global_context__` bucket, Python's
module-level shape: stored in synthetic_unit_data, out of the function
population, no weight. Kept only when it carries an edge. Sibling and
nested programs each get their own.

Golden masters regenerated in both legs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FNWHcUSn5p7eMxFTsB5rFo
@github-actions

github-actions Bot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

🐦‍⬛ Muninn Security Scan

✅ No security issues found.

🐦‍⬛ Powered by Muninn · Skald Lab

@squid-protocol
squid-protocol marked this pull request as ready for review October 4, 2026 15:49
@squid-protocol
squid-protocol merged commit 0d64632 into main Oct 4, 2026
36 checks passed
@squid-protocol
squid-protocol deleted the fix/4302-cobol-procedure-entry-unit branch October 4, 2026 15:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant