Repository navigation
feat(cobol): resolve a PERFORM between a program and its copybooks - #5426
pavansystems wants to merge 5 commits into
Conversation
|
Thanks for the pull request, @pavansystems. 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. |
There was a problem hiding this comment.
Graphify review 
1 bug · 4 possible issues to check · High impact
Warm caches can hide the new COBOL links ·
cobol.py:438
4 possible issues we couldn't confirm
- COBOL copybook PERFORM resolution never runs for files with other or mixed-case extensions, so their PERFORMs produce no edges ·
graphify/extract.py:10242 - COBOL PERFORM calls that the copybook resolver does not bind are dropped with no edge at all ·
graphify/extract.py:13571 - The stranger-program check in the cross-program PERFORM test can crash on edges that lack a
source_filekey. ·tests/test_cobol_extractor.py:435 - +1 more in Graphify
They don't change the score.
Code graph · 5089 functions depend on this change
- 1 function became more coupled.
extract()(1369 callers). - 625 of 625 test files are affected by this change.
- Gate, test list and every flagged function: in the Graphify check.
Reviewed 45bce24 · 2 earlier findings not re-checked
| # A line that begins with a statement verb. Only procedure code has these; a | ||
| # data description begins with a level number. | ||
| _STATEMENT = re.compile( | ||
| r"^(?:ACCEPT|ADD|CALL|CLOSE|COMPUTE|CONTINUE|DISPLAY|DIVIDE|EVALUATE|EXIT" |
There was a problem hiding this comment.
SORT-only procedure copybooks are treated as data
A headerless copybook whose paragraph body starts with SORT is not recognized as procedure code, so its paragraphs are unavailable for PERFORM resolution.
Recognize SORT in _STATEMENT in graphify/extractors/cobol.py.
Evidence · checked against the code
- Independent source review: Reviewed the extractor’s procedure-mode transition, paragraph and PERFORM extraction, cross-file resolver registration, and existing headerless-copybook tests. Those tests cover other statement verbs, but none covers SORT; no route or guard compensates for a copybook whose only recognized body statement is SORT.
Prompt to fix with AI
In graphify/extractors/cobol.py, _procedure_copybook_start (lines 39-42):
Problem: SORT-only procedure copybooks are treated as data
Fix it at the cause, and add a test that fails without the fix.
Graphify · Checked against the code · View in Graphify
| }) | ||
| # A line that begins with a statement verb. Only procedure code has these; a | ||
| # data description begins with a level number. | ||
| _STATEMENT = re.compile( |
There was a problem hiding this comment.
Copybooks using
SEARCH alone lose their paragraphs
A headerless procedure copybook whose paragraph body uses SEARCH may be treated as data, so its paragraph is unavailable for PERFORM links.
Recognize SEARCH in _STATEMENT in graphify/extractors/cobol.py.
Evidence · checked against the code
- Independent source review: The existing headerless-copybook test covers
IF,PERFORM,EXEC CICS, andDISPLAY, which are recognized or handled separately; it does not coverSEARCH. The data-copybook test confirms the detector intentionally avoids treating a lone paragraph-like name as procedure code, but it does not prevent this missedSEARCHcase.
Prompt to fix with AI
In graphify/extractors/cobol.py, _procedure_copybook_start (lines 38-41):
Problem: Copybooks using `SEARCH` alone lose their paragraphs
Fix it at the cause, and add a test that fails without the fix.
Graphify · Checked against the code · View in Graphify
e8052f4 to
a7be677
Compare
| continue | ||
| if _PROGRAM.search(upper) or "PROCEDURE DIVISION" in upper: | ||
| return None | ||
| embedded = _EXEC.search(upper) |
There was a problem hiding this comment.
SQL-like text in a data value can hide later data items
A data copybook can be mistaken for procedure code when a quoted value contains EXEC SQL text, causing later data items to be omitted.
Mask quoted values in _procedure_copybook_start before matching _EXEC.
Evidence · checked against the code
Expected: The copybook remains classified as data, and subsequent data items are extracted.
Actually: The classifier treats the text inside the quoted value as an embedded SQL statement, returns the earlier continuation line as the procedure start, and the extraction loop skips later data items.
- Independent source review: The existing data-copybook test covers a
SPACES.continuation followed by an ordinary data item and confirms that the items are extracted. It does not includeEXEC SQLin a quoted value, so it does not guard against this case. The classifier has no string-masking check on the route from_EXEC.searchtohas_statement.
Prompt to fix with AI
In graphify/extractors/cobol.py, _procedure_copybook_start (lines 137-140):
Problem: SQL-like text in a data value can hide later data items
Expected: The copybook remains classified as data, and subsequent data items are extracted.
Actually: The classifier treats the text inside the quoted value as an embedded SQL statement, returns the earlier continuation line as the procedure start, and the extraction loop skips later data items.
Fix it at the cause, and add a test that fails without the fix.
Graphify · Checked against the code · View in Graphify
a7be677 to
45bce24
Compare
| add_edge(caller_id, target_id, "calls", line) | ||
| else: | ||
| # Not a paragraph of this file. It may be one of a copybook the | ||
| # program copies, or of the program that copies this copybook: |
There was a problem hiding this comment.
Warm caches can hide the new COBOL links
After upgrading without changing the cache namespace, previously cached COBOL files can keep the new PERFORM links and procedure-copybook paragraphs from appearing.
Bump the AST cache namespace for extract_cobol.
Evidence · checked against the code
Expected: COBOL files are re-extracted so the new resolver inputs and headerless procedure-copybook paragraphs are present.
Actually: Old per-file results are reused; missing PERFORM records and paragraphs are not regenerated, so corresponding cross-file links and symbols remain absent.
- Independent source review: Checked whether the project version or cache implementation changes invalidate old entries; pyproject.toml has no diff, the base and head package versions are both 0.9.119, and graphify/cache.py has no diff. The cache-hit path continues to pass cached per-file results to the resolver.
- Part of a file with a line too long to show was not read.
Prompt to fix with AI
In graphify/extractors/cobol.py, extract_cobol (lines 435-438):
Problem: Warm caches can hide the new COBOL links
Expected: COBOL files are re-extracted so the new resolver inputs and headerless procedure-copybook paragraphs are present.
Actually: Old per-file results are reused; missing PERFORM records and paragraphs are not regenerated, so corresponding cross-file links and symbols remain absent.
Fix it at the cause, and add a test that fails without the fix.
Graphify · Checked against the code · View in Graphify
A copybook of procedure code is pasted into the PROCEDURE DIVISION of the program that copies it, so it holds paragraphs and statements but has no `PROCEDURE DIVISION` header of its own. The extractor starts reading paragraphs only after that header, so such a file was taken for data from its first line to its last: it got a file node and nothing else, and every paragraph and PERFORM in it was missing from the graph. Before reading a file, look for the line where its procedure code begins: the first paragraph or section header, provided the file has neither a PROGRAM-ID nor a PROCEDURE DIVISION header and a statement (a line that begins with a statement verb, or an EXEC block) follows that header. From that line on the file is read as procedure code. The paragraphs hang under the file node, as the data items of a data copybook already do. Every other file is read exactly as before. A lone `NAME.` line is not enough to switch: a data copybook can have one where a VALUE clause runs on to the next line (`SPACES.`), and without a statement after it the file stays data. Not covered: a copybook that holds statements but no paragraph header (a fragment pasted into the middle of a paragraph), and statements that stand before the first header. Regression test: a `.cpy` with two paragraphs and a PERFORM between them, which fails without the change. A data copybook with a lone `SPACES.` line is the control. Co-Authored-By: Claude Fable 5.2 <[email protected]>
A headerless copybook is read as procedure code when a statement follows its first paragraph header. The check knew two dozen verbs, so a copybook whose only statement was a SORT, SEARCH, INSPECT, NEXT SENTENCE or STOP RUN stayed data and lost its paragraphs. The list now holds the statement verbs of the language. Two ways a data copybook with a lone `NAME.` line could be misread as procedure code, and lose its data items, are closed: - The verb had to be followed by a word boundary only, so a name such as `OPEN-FILES.` counted as an OPEN statement. It must now be a whole word. - Any EXEC block counted as a statement. `EXEC SQL INCLUDE` and `EXEC SQL DECLARE` are declarations that the DATA DIVISION has as well, and no longer count, also when the word stands on the line after `EXEC SQL`. Regression tests: a copybook for each of five verbs that were missing and for an EXEC SQL statement on one line and on three, and a data copybook with each of the three lookalikes. The verb and lookalike cases fail without the change. Co-Authored-By: Claude Fable 5.2 <[email protected]>
The check for a headerless procedure copybook looked for EXEC blocks in the whole line, quoted text included. A data copybook with a lone `NAME.` line and a later `VALUE 'EXEC SQL COMMIT END-EXEC'` was read as procedure code and lost its data items. Quoted text is now blanked before the check, as the extractor already does for PERFORM. Regression test: the quoted EXEC SQL value as a further case of the data-copybook lookalike test, which fails without the change, and a quoted `'MOVE A TO B'` as control. Co-Authored-By: Claude Fable 5.2 <[email protected]>
The compiler pastes every copybook into the program that copies it, so one COBOL program is spread over several files. A program can perform a paragraph of a copybook, a copybook a paragraph of the program, and one copybook a paragraph of another. The extractor resolves a PERFORM against the paragraphs of its own file only, so each of these was dropped: the shared routines of a codebase, which live in procedure copybooks, had no callers in the graph. The extractor now leaves a PERFORM it cannot resolve in its own file in `raw_calls`, flagged `cobol_perform`. A new resolver, registered as `cobol_copybook_performs`, binds them once every file is read: - A program's files are the program and every copybook reached from it through `imports_from` edges, copybooks copied by copybooks included. - A PERFORM is linked only to a paragraph of those files, and only when exactly one of them has a paragraph of that name. Two candidates leave it unlinked. - A copybook that several programs copy is part of each, so a PERFORM in it can get one edge per program. The edges are `calls` with `context: "copybook_perform"`, EXTRACTED, as for R's `sourced_call`: the COPY statement is explicit in the source. The shared cross-file call pass skips `cobol_perform` entries. A paragraph name is visible only inside its own program, and a corpus-wide name match would link `PERFORM INIT` to any program's INIT paragraph. Builds on the procedure-copybook change: without it a copybook has no paragraph nodes to link to. Not covered: COPY ... REPLACING that renames a paragraph, a copybook that is not found next to the copying file as `NAME.cpy` (the existing `imports_from` rule), and a THRU range or GO TO that crosses files. Tests: both directions between a program and a copybook (also on a cached second run), no match by name across programs, a copybook shared by two programs, a copybook copied by a copybook, and an ambiguous name. Co-Authored-By: Claude Fable 5.2 <[email protected]>
The resolver is started when the corpus has a file with one of its suffixes, compared as written. Only the lower-case COBOL suffixes were registered, so a corpus delivered as `PROG.CBL` with `BOOK.CPY` never ran it and got no cross-file PERFORM edge. The upper-case forms are registered as well, as for R. The test for an ambiguous name passed even when the resolver did nothing. It now also performs a name that only one of the two copybooks defines and expects that edge. Regression test: a `.CBL` program and a `.CPY` copybook, which fails without the change. Co-Authored-By: Claude Fable 5.2 <[email protected]>
45bce24 to
332b0d4
Compare
…a-57 Both change extraction output and ship in the same release as the 56->57 bump, so they share schema 57 with a combined rationale. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Shipped in v0.9.121, landed via authorship-preserving cherry-pick so your commits keep your name. Thanks @pavansystems! |
… 0.9.121 v8 0.9.121 also lands schema 57 (Rust Graphify-Labs#5398, COBOL Graphify-Labs#5424-Graphify-Labs#5426). Bump _AST_CACHE_SCHEMA to 58 with the C/C++ reachability lead and keep v8's schema-57 text verbatim as "prior (57)". Co-Authored-By: Claude Opus 5.5 <[email protected]>
What does this PR do?
Builds on #5425 (procedure copybooks): without it a copybook has no paragraph nodes to link to. This branch is stacked on that one, so until #5425 lands the diff here also shows its commits; the last two commits belong to this PR. I can rebase once #5425 is in.
The compiler pastes every copybook into the program that copies it, so one COBOL program is spread over several files. A program can perform a paragraph of a copybook, a copybook a paragraph of the program, and one copybook a paragraph of another. The extractor resolves a
PERFORMagainst the paragraphs of its own file only, so each of these is dropped: the shared routines of a codebase, which live in procedure copybooks, have no callers in the graph.The extractor now leaves a
PERFORMit cannot resolve in its own file inraw_calls, flaggedcobol_perform. A new resolver, registered ascobol_copybook_performsnext to the R one, binds them once every file is read:imports_fromedges, copybooks copied by copybooks included.PERFORMis linked only to a paragraph of those files, and only when exactly one of them has a paragraph of that name. Two candidates leave it unlinked.PERFORMin it can get one edge per program.The edges are
callswithcontext: "copybook_perform",EXTRACTED, as for R'ssourced_call: theCOPYstatement is explicit in the source.The shared cross-file call pass skips
cobol_performentries. A paragraph name is visible only inside its own program, and a corpus-wide name match would linkPERFORM INITto any program'sINITparagraph.Not covered:
COPY ... REPLACINGthat renames a paragraph, a copybook that is not found next to the copying file asNAME.cpy(the existingimports_fromrule), and aTHRUrange orGO TOthat crosses files.Tests: both directions between a program and a copybook (also on a cached second run), no match by name across programs, a copybook shared by two programs, a copybook copied by a copybook, and an ambiguous name.
I found no existing issue for this.
Follow-up in #5425 (
fix(cobol): know a procedure copybook by any statement, not a short list), after the automated review. All three points were reproduced first: (this branch was rebased onto it)SORT,SEARCH,INSPECT,NEXT SENTENCEorSTOP RUNstayed data. The check now knows the statement verbs of the language.OPEN-FILES.counted as anOPENstatement, so a data copybook with a loneSPACES.line and such a name was read as procedure code. The verb must now be a whole word.EXECblock counted as a statement, so the same data copybook withEXEC SQL INCLUDE SQLCA END-EXECwas read as procedure code.EXEC SQL INCLUDEandDECLAREno longer count.Follow-up commit here (
fix(cobol): start the copybook resolver for upper-case file extensions): reproduced that a corpus ofPROG.CBLwithBOOK.CPYnever started the resolver, since only lower-case suffixes were registered. The upper-case forms are registered too, as for R. The ambiguous-name test now also expects an edge for a name only one copybook defines, so it no longer passes when the resolver does nothing.Not changed: a
PERFORMthe resolver cannot bind gets no edge from the shared pass either. That is intended (see the paragraph on the shared pass above).Rebased onto
v8after #5423 landed, and onto the updated #5425, which has one more follow-up (a quoted value such as'EXEC SQL COMMIT END-EXEC'is no longer read as a statement). The diff here shows the three commits of #5425 followed by the two of this PR.Type of change
Verification & Invariants
Invariant: a
PERFORMis linked across files only inside one program's own files (the program plus the copybooks reached from it throughimports_from), and only when exactly one of those files has a paragraph of that name. An ambiguous name, or a name that only matches in another program, stays unlinked. APERFORMthat resolves inside its own file is handled exactly as before.Persisted state: the extractor now writes unresolved PERFORMs to
raw_calls, which is cached per file, so the resolver also works on a cached second run (covered by a test). A COBOL file cached before this change has no such entries until it changes or the cache is cleared. I did not bump_AST_CACHE_SCHEMA, following the earlier COBOL fixes; say so if you want it bumped.How was this tested?
On this branch (macOS, Python from
uv sync --all-extras --frozen):The new tests were also run against unmodified
v8, where they fail.After the rebase, the full suite (
uv run --frozen --all-extras pytest tests) was run on this branch: 11917 passed, 165 skipped, 1 xfailed, none failed.The combined branch was also run on a private COBOL codebase and the result compared with links I had been adding outside Graphify; it produced no link that the comparison did not also have.
Graphify-specific checklist
uv run python -m tools.skillgen --bless) when changing their source fragments. (Not applicable: no skill fragment changed.)The code, tests and this description were written with Claude Code; the commit carries a
Co-Authored-Byline.🤖 Generated with Claude Code