Skip to content

fix(cobol): read the paragraphs of a procedure copybook - #5425

Closed
pavansystems wants to merge 3 commits into
Graphify-Labs:v8from
pavansystems:cobol-procedure-copybook
Closed

pavansystems wants to merge 3 commits into
Graphify-Labs:v8from
pavansystems:cobol-procedure-copybook

Conversation

@pavansystems

@pavansystems pavansystems commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

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 is taken for data from its first line to its last: it gets a file node and nothing else, and every paragraph and PERFORM in it is missing from the graph.

Before reading a file, this looks for the line where its procedure code begins: the first paragraph or section header, provided that

  • the file has neither a PROGRAM-ID nor a PROCEDURE DIVISION header, and
  • a statement follows that header (a line that begins with a statement verb, or an EXEC block).

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.

I found no existing issue for this.

Follow-up commit (fix(cobol): know a procedure copybook by any statement, not a short list), after the automated review. All three points were reproduced first:

  • 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.

Rebased onto v8 after #5423 landed, with one more follow-up commit (fix(cobol): do not read a quoted value as a statement in a copybook): reproduced the second automated review's point that a data copybook with a lone NAME. line and a later VALUE 'EXEC SQL COMMIT END-EXEC' was read as procedure code. Quoted text is now blanked before the check.

Type of change

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

Verification & Invariants

Invariant: a file with a PROGRAM-ID or a PROCEDURE DIVISION header, and a data copybook, are read exactly as before. The control test is a data copybook with a lone SPACES. line, which must still be read as data.

Persisted state: the AST cache holds per-file results from before this change, so a cached COBOL file keeps its old result until the file 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   # 25 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 the #5426 branch, which is this branch plus two commits: 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

Nothing blocking · 1 possible issue to check · Merge confidence 5/5

1 possible issue to check · Medium impact

Headerless COBOL copybooks are now treated as procedure code when a paragraph is followed by a recognized statement.

1 possible issue we couldn't confirm

They don't change the score.

Code graph · 44 functions depend on this change
  • 625 of 625 test files are affected by this change.
  • Gate, test list and every flagged function: in the Graphify check.

Reviewed 33a8af3 · 1 earlier finding not re-checked

Comment on lines 36 to +39
_PERFORM_MODIFIERS = frozenset({
"UNTIL", "VARYING", "TIMES", "WITH", "TEST", "FOREVER",
})
# A line that begins with a statement verb. Only procedure code has these; a

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  Paragraphs with unsupported statements stay classified as data

A headerless procedure copybook containing a SORT statement is not recognized as procedure code, so its paragraphs and PERFORM links are omitted.

Broaden _procedure_copybook_start to recognize valid statements such as SORT.

Evidence · checked against the code

Expected: The extractor identifies the copybook as procedure code and extracts its paragraphs and PERFORM links.
Actually: The helper returns None; the extractor stays in data mode and skips procedure parsing.

  • Independent source review: The data-copybook test confirms that a lone paragraph-shaped line should not trigger procedure mode, and the existing procedure-copybook test exercises recognized verbs such as IF and DISPLAY. Neither provides a guard for a valid SORT statement or covers the claimed omission.
Prompt to fix with AI
In graphify/extractors/cobol.py, _procedure_copybook_start (lines 36-39):
Problem: Paragraphs with unsupported statements stay classified as data
Expected: The extractor identifies the copybook as procedure code and extracts its paragraphs and `PERFORM` links.
Actually: The helper returns `None`; the extractor stays in data mode and skips procedure parsing.
Fix it at the cause, and add a test that fails without the fix.

Graphify · Checked against the code · View in Graphify

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.

P2  Some valid procedure copybooks are still treated as data

A headerless copybook whose paragraph contains only an unlisted statement, such as NEXT SENTENCE, is not recognized as procedure code, so its paragraph is lost.

Broaden _procedure_copybook_start to recognize valid statements such as NEXT SENTENCE.

Evidence · checked against the code

Expected: The extractor recognizes the file as procedure code and emits the paragraph.
Actually: The detector returns None; the extractor leaves procedure mode off and skips paragraph extraction.

  • Independent source review: The existing headerless-copybook test uses IF, which is recognized by _STATEMENT, and verifies paragraph extraction for that case; it does not cover or mitigate NEXT SENTENCE. The data-copybook test checks a separate input shape and does not prevent the stated omission.
Prompt to fix with AI
In graphify/extractors/cobol.py, _procedure_copybook_start (lines 39-42):
Problem: Some valid procedure copybooks are still treated as data
Expected: The extractor recognizes the file as procedure code and emits the paragraph.
Actually: The detector returns `None`; the extractor leaves procedure mode off and skips paragraph extraction.
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-procedure-copybook branch from 1706f5a to 33a8af3 Compare November 24, 2026 21:40
pavansystems and others added 3 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]>
@pavansystems
pavansystems force-pushed the cobol-procedure-copybook branch from 33a8af3 to 783b1bc 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
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