feat(graph): PLC programs, function blocks and functions in the code graph (t82) #936

Merged
sharang merged 1 commits from feat/t82-plc-graph into main 2026-11-07 10:05:16 +00:00
Owner

The second PR of t82: PLC projects get a code graph. Until now PLC targets skipped the graph phase entirely.

Stacked on #935 (C and C#): the base is feat/t82-c-csharp-graph, so only this PR's commit shows. Merge #935 first, then retarget this PR to main before #935's branch is deleted (deleting a base branch closes the PR).

What becomes a node (Structured Text .st/.scl/.exp/.il and PLCopen XML exports):

  • a PROGRAM is a module and an entry point, since the PLC's tasks run it;
  • a FUNCTION_BLOCK is a class, since each instance keeps its own state;
  • a FUNCTION is a function.

Edges:

  • A variable whose type is a function block is a type reference to it.
  • A call through an instance (Pump1(Start := x)) is a call to the instance's type. The instance name is matched case-insensitively, as IEC 61131-3 reads it; names of other POUs are matched as written.
  • A call by name (Clamp(level)) is a call.
  • Standard blocks (TON, CTU) have no node, so their edges stay unresolved and are dropped.
  • Method calls on an instance (pump1.Reset()) aren't parsed as calls today, so they have no edge.

How it fits together:

  • The PLC scan reads each source tree once. The findings and the graph both come from that read, and the graph covers every PLC source tree of the target.
  • GraphEngine::assemble takes nodes and edges parsed outside the graph crate. build_graph is now "parse the directory, then assemble".
  • FileGraph is public, with node_at / edge_at taking lines instead of tree-sitter nodes.
  • One build function takes either a tree or a PLC project's POUs. The scan uses it, and so does the explorer's manual rebuild (POST /graph/{id}/build), which would otherwise replace a PLC target's graph with file nodes only.
  • planned_phases lists GraphBuilding for PLC targets, and its test is updated.

Lines:

  • A POU now records the line of its END_* keyword, so a finding inside it maps to it.
  • A PLCopen POU spans its <pou> element in the XML. Its statements keep the lines of the Structured Text reconstructed from the XML, so finding lines and fingerprints don't change.
  • For PLCopen findings the impact therefore falls back to the file. Matching those findings to their XML lines is a separate change, because it changes fingerprints.

Watch the first nightly after this lands: the PLC fixtures get a code graph for the first time, so the graph counts on PLC targets appear and some triage drift is possible.

Tests:

  • POU kinds, spans and entry points.
  • Instance, type-reference and call edges.
  • PLCopen spans.
  • The engine linking instances and dropping standard blocks.
  • Every POU of examples/plc-demo becomes a node.
  • Agent lib tests: 1,002 pass. Graph tests: 100 pass. Clippy with the CI flags is clean.

Next in t82: the graph card on the target page, and the /chat recommendation.

🤖 Generated with Claude Code

The second PR of t82: PLC projects get a code graph. Until now PLC targets skipped the graph phase entirely. **Stacked on #935** (C and C#): the base is `feat/t82-c-csharp-graph`, so only this PR's commit shows. Merge #935 first, then retarget this PR to `main` before #935's branch is deleted (deleting a base branch closes the PR). **What becomes a node** (Structured Text `.st/.scl/.exp/.il` and PLCopen XML exports): - a `PROGRAM` is a module and an entry point, since the PLC's tasks run it; - a `FUNCTION_BLOCK` is a class, since each instance keeps its own state; - a `FUNCTION` is a function. **Edges:** - A variable whose type is a function block is a type reference to it. - A call through an instance (`Pump1(Start := x)`) is a call to the instance's type. The instance name is matched case-insensitively, as IEC 61131-3 reads it; names of other POUs are matched as written. - A call by name (`Clamp(level)`) is a call. - Standard blocks (`TON`, `CTU`) have no node, so their edges stay unresolved and are dropped. - Method calls on an instance (`pump1.Reset()`) aren't parsed as calls today, so they have no edge. **How it fits together:** - The PLC scan reads each source tree once. The findings and the graph both come from that read, and the graph covers every PLC source tree of the target. - `GraphEngine::assemble` takes nodes and edges parsed outside the graph crate. `build_graph` is now "parse the directory, then assemble". - `FileGraph` is public, with `node_at` / `edge_at` taking lines instead of tree-sitter nodes. - One build function takes either a tree or a PLC project's POUs. The scan uses it, and so does the explorer's manual rebuild (`POST /graph/{id}/build`), which would otherwise replace a PLC target's graph with file nodes only. - `planned_phases` lists `GraphBuilding` for PLC targets, and its test is updated. **Lines:** - A POU now records the line of its `END_*` keyword, so a finding inside it maps to it. - A PLCopen POU spans its `<pou>` element in the XML. Its statements keep the lines of the Structured Text reconstructed from the XML, so **finding lines and fingerprints don't change**. - For PLCopen findings the impact therefore falls back to the file. Matching those findings to their XML lines is a separate change, because it changes fingerprints. **Watch the first nightly after this lands:** the PLC fixtures get a code graph for the first time, so the graph counts on PLC targets appear and some triage drift is possible. **Tests:** - POU kinds, spans and entry points. - Instance, type-reference and call edges. - PLCopen spans. - The engine linking instances and dropping standard blocks. - Every POU of `examples/plc-demo` becomes a node. - Agent lib tests: 1,002 pass. Graph tests: 100 pass. Clippy with the CI flags is clean. Next in t82: the graph card on the target page, and the /chat recommendation. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
sharang changed target branch from feat/t82-c-csharp-graph to main 2026-11-07 09:44:11 +00:00
sharang added 1 commit 2026-11-07 09:44:37 +00:00
feat(graph): PLC programs, function blocks and functions in the code graph (t82)
CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Deploy Agent (pull_request) Skipped
CI / Deploy Dashboard (pull_request) Skipped
CI / Deploy Docs (pull_request) Skipped
CI / Deploy MCP (pull_request) Skipped
CI / Agent integration (pull_request) Successful in 4m2s
CI / Check (pull_request) Successful in 20m29s
dc2c9845fd
A PLC target now gets a code graph. Before this, PLC targets skipped the
graph phase entirely.

Nodes:
- A PROGRAM is a module and an entry point.
- A FUNCTION_BLOCK is a class.
- A FUNCTION is a function.

Edges:
- A variable whose type is a function block is a type reference to it.
- A call through an instance is a call to its type.
- Calls by name are calls.
- Standard blocks such as TON have no node, so their edges are dropped.

How it fits together:
- The PLC module reads each tree once, and both the findings and the
  graph come from that read.
- The engine gets a public assemble step for nodes parsed outside the
  graph crate, and FileGraph is public with line-based adders.
- One build function takes a tree or a PLC project's POUs. The scan
  uses it, and so does the explorer's manual rebuild, which would
  otherwise replace a PLC target's graph with file nodes only.

POU spans:
- A POU records the line of its END_ keyword.
- A PLCopen POU spans its <pou> element in the XML. Its statements keep
  their synthetic lines, so finding lines and fingerprints don't change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sharang force-pushed feat/t82-plc-graph from 062b34105a to dc2c9845fd 2026-11-07 09:44:37 +00:00 Compare
sharang merged commit 37888af374 into main 2026-11-07 10:05:16 +00:00
Sign in to join this conversation.