Pattern Extraction

Lesson 8 · Learn Graphify · ~10 minutes

The graph doesn't just show you what exists — it reveals the patterns in how your code is organized. This lesson teaches you to extract those patterns and use them as constraints for AI-generated code.

Win

After this lesson, you can identify architectural patterns from graph structure and feed them to the AI as explicit constraints — producing code that matches your project's conventions on the first attempt.

What Are Patterns in a Graph?

Patterns are structural regularities. In a well-organized codebase, you'll see:

Finding Patterns in GRAPH_REPORT.md

The report's community descriptions hint at patterns. Look for:

# In GRAPH_REPORT.md, you might see:
#
# Community: "Update Operator Tests"
#   Key nodes: test_min_comparisons, test_max_comparisons, test_inc_basic, ...
#   Common imports: framework.assertions, framework.test_case, framework.parametrize
#   Structure: Each file inherits UpdateTestCase, defines TESTS list, has one test function

That last line is the pattern. Extract it explicitly.

Articulating Patterns as Constraints

Once you identify a pattern, express it as a constraint document the AI can reference:

# .graphify-patterns.md

## Update Operator Test Pattern
Every operator test file in the "Update Operator Tests" community:
1. Imports: framework.assertions, framework.test_case, framework.parametrize
2. Defines a dataclass extending UpdateTestCase (frozen=True)
3. Has a single TESTS: list[TestCase] at module level
4. Has a single parametrized test function using pytest_params()
5. Test IDs are prefixed with the operator name (e.g., min_, max_)
6. Module docstring describes what the file tests
7. File naming: test_{operator}_{aspect}.py

Feed this to the AI alongside the graph for highly consistent output.

Using the Graph to Detect Pattern Violations

Ask the AI to audit against the extracted pattern:

"Using the graph, find all test files in the 'Update Operator Tests' community
that DON'T import framework.parametrize. These are likely violating
our standard pattern."

Or check for structural drift:

"Compare the import edges of test_min_comparisons.py vs test_inc_basic.py.
Do they follow the same pattern? What's different?"

Architectural Boundaries

The graph makes boundaries visible. A well-architected project has clear layers with dependencies flowing in one direction:

# Expected dependency direction:
# test files → framework → utilities → driver
#
# Violations (circular or backwards dependencies):
# "framework.assertions imports test_helpers" ← boundary violation

Ask the AI to check for these:

"Using the graph edges, are there any imports that flow backwards —
from framework/ into tests/, or from utilities/ back into framework/?"
Key Insight

Patterns extracted from the graph are more reliable than patterns you think exist. The graph shows what the code actually does, not what the README says it should do. Trust the graph over documentation when they conflict.

Pattern Extraction Workflow

  1. Identify a community with 5+ nodes that look similar
  2. List common edges: What imports, inherits, and calls do they share?
  3. Articulate the pattern in a constraint document
  4. Validate: Do all members match? Note exceptions.
  5. Use as prompt context when generating new code in that community

Verify Your Understanding

What makes graph-extracted patterns more reliable than documented conventions?

How do you use an extracted pattern to improve AI-generated code?

Do This Now

  1. Open GRAPH_REPORT.md and pick the largest community
  2. Identify the structural pattern: shared imports, inheritance, naming
  3. Write it down as a constraint document (3-5 rules)
  4. Ask the AI to generate a new file following that pattern — does it match?