Skip to content

feat(cobol): resolve a PERFORM between a program and its copybooks - #5426

Closed
pavansystems wants to merge 5 commits into
Graphify-Labs:v8from
pavansystems:cobol-copybook-perform
Closed

pavansystems wants to merge 5 commits into
Graphify-Labs:v8from
pavansystems:cobol-copybook-perform

Conversation

@pavansystems

@pavansystems pavansystems commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

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 PERFORM against 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 PERFORM it cannot resolve in its own file in raw_calls, flagged cobol_perform. A new resolver, registered as cobol_copybook_performs next to the R one, 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.

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.

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)

  • A copybook whose only statement was a SORT, SEARCH, INSPECT, NEXT SENTENCE or STOP RUN stayed data. The check now knows the statement verbs of the language.
  • A name such as OPEN-FILES. counted as an OPEN statement, so a data copybook with a lone SPACES. line and such a name was read as procedure code. The verb must now be a whole word.
  • Any EXEC block counted as a statement, so the same data copybook with EXEC SQL INCLUDE SQLCA END-EXEC was read as procedure code. EXEC SQL INCLUDE and DECLARE no longer count.

Follow-up commit here (fix(cobol): start the copybook resolver for upper-case file extensions): reproduced that a corpus of PROG.CBL with BOOK.CPY never 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 PERFORM the resolver cannot bind gets no edge from the shared pass either. That is intended (see the paragraph on the shared pass above).

Rebased onto v8 after #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

  • Bug fix
  • New feature
  • Documentation
  • Tests or CI
  • Refactor
  • Security fix

Verification & Invariants

Invariant: a PERFORM is linked across files only inside one program's own files (the program plus the copybooks reached from it through imports_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. A PERFORM that 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.

  • Read the CONTRIBUTING.md guide.
  • Reproduced the issue and identified the invariant.
  • Made the smallest fix necessary.
  • Added a regression test (if bug fix) or isolated boundary test.
  • Kept the PR description synchronized with the final implementation.
  • Documented any limitations / unsupported cases explicitly.

How was this tested?

On this branch (macOS, Python from uv sync --all-extras --frozen):

uv run --frozen --all-extras pytest tests/test_cobol_extractor.py   # 31 passed
uv run --frozen ruff check .                                        # All checks passed
uv run --frozen pyright                                             # 771 errors, the same count as unmodified v8 (ba33a18)

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

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments. (Not applicable: no skill fragment changed.)
  • I confirmed that AST/structural extraction remains deterministic (no ambient state dependencies like ENV variables).
  • I reviewed changes for security implications (no unsafe interpolation into shell/Python).
  • I confirmed no API keys or local-only graph data are included.
  • (If applicable) I disclosed AI authorship in my commit messages.

The code, tests and this description were written with Claude Code; the commit carries a Co-Authored-By line.

🤖 Generated with Claude Code

@github-actions

Copy link
Copy Markdown

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.

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Graphify review View in Graphify

Review before merging · Merge confidence 4/5

1 bug · 4 possible issues to check · High impact

P2  Warm caches can hide the new COBOL links · cobol.py:438

4 possible issues we couldn't confirm

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

Comment thread graphify/extractors/cobol.py Outdated
Comment on lines +39 to +42
# 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"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug  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

Comment on lines 38 to +41
})
# A line that begins with a statement verb. Only procedure code has these; a
# data description begins with a level number.
_STATEMENT = re.compile(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug  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, and DISPLAY, which are recognized or handled separately; it does not cover SEARCH. The data-copybook test confirms the detector intentionally avoids treating a lone paragraph-like name as procedure code, but it does not prevent this missed SEARCH case.
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

@pavansystems
pavansystems force-pushed the cobol-copybook-perform branch from e8052f4 to a7be677 Compare November 23, 2026 19:04
Comment on lines +137 to +140
continue
if _PROGRAM.search(upper) or "PROCEDURE DIVISION" in upper:
return None
embedded = _EXEC.search(upper)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2  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 include EXEC SQL in a quoted value, so it does not guard against this case. The classifier has no string-masking check on the route from _EXEC.search to has_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

@pavansystems
pavansystems force-pushed the cobol-copybook-perform branch from a7be677 to 45bce24 Compare November 24, 2026 21:40
Comment on lines 435 to +438
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:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2  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

pavansystems and others added 5 commits November 25, 2026 18:06
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]>
@pavansystems
pavansystems force-pushed the cobol-copybook-perform branch from 45bce24 to 332b0d4 Compare November 25, 2026 17:08
safishamsi added a commit that referenced this pull request Nov 26, 2026
…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]>
@safishamsi

Copy link
Copy Markdown
Member

Shipped in v0.9.121, landed via authorship-preserving cherry-pick so your commits keep your name. Thanks @pavansystems!

@safishamsi safishamsi closed this Nov 26, 2026
scarsnow added a commit to scarsnow/graphify that referenced this pull request Nov 27, 2026
… 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants