Skip to content

Catalog Structure Contract

Status: Active
Created: 2026-01-03
Updated: 2026-01-03
Purpose: Define the codec package structure and AST extraction conventions


Overview

Single Source of Truth: All codec classes and functions live in runner/utils/codec/.

The catalog module uses AST extraction to read code from this package for inline mode. No separate templates directory β€” DRY principle.


Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    runner/utils/codec/                       β”‚
β”‚                  (Single Source of Truth)                    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  classes/          functions/                                β”‚
β”‚  β”œβ”€ list_node.py   β”œβ”€ list_node/                            β”‚
β”‚  β”œβ”€ tree_node.py   β”‚  β”œβ”€ struct.py    (Tier-1)              β”‚
β”‚  β”œβ”€ node.py        β”‚  └─ semantic.py  (Tier-1.5)            β”‚
β”‚  β”œβ”€ node_graph.py  β”œβ”€ tree_node/                            β”‚
β”‚  β”œβ”€ node_nary.py   β”œβ”€ node/                                 β”‚
β”‚  └─ doubly_...py   β”œβ”€ node_graph/                           β”‚
β”‚                    β”œβ”€ node_nary/                             β”‚
β”‚                    └─ doubly_list_node/                      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
           β”‚                           β”‚
           β–Ό                           β–Ό
    Import Mode                  Inline Mode
    ───────────                  ───────────
    from runner.utils.codec      catalog.get_with_deps()
    import list_to_tree          β†’ AST extraction
                                 β†’ Embed in solution

Helper Classes Reference

Class Description Example Problems
ListNode Singly linked list 2, 19, 21, 23, 24, 25, 61, 82, 83, 86, 92, 141, 142, 143, 160, 203, 206, 234, 328, 876
TreeNode Binary tree 94, 98, 100, 101, 102, 104, 105, 108, 110, 111, 112, 144, 145, 199, 226, 543, 572
Node Random pointer list 138 (Copy List with Random Pointer)
NodeGraph Graph with neighbors 133 (Clone Graph), 207, 210, 261, 323
NodeNary N-ary tree 429, 559, 589, 590
DoublyListNode Doubly linked list 430, 146 (LRU Cache internal)

Directory Structure

runner/utils/codec/
β”œβ”€β”€ __init__.py                      # Re-export all (IDE entry point)
β”œβ”€β”€ classes/                         # Data structure definitions
β”‚   β”œβ”€β”€ __init__.py                  # Re-export classes
β”‚   β”œβ”€β”€ list_node.py                 # class ListNode
β”‚   β”œβ”€β”€ tree_node.py                 # class TreeNode
β”‚   β”œβ”€β”€ node.py                      # class Node (random pointer)
β”‚   β”œβ”€β”€ node_graph.py                # class NodeGraph
β”‚   β”œβ”€β”€ node_nary.py                 # class NodeNary
β”‚   └── doubly_list_node.py          # class DoublyListNode
└── functions/                       # Conversion functions
    β”œβ”€β”€ __init__.py                  # Re-export all functions
    β”œβ”€β”€ list_node/                   # Depends on ListNode
    β”‚   β”œβ”€β”€ __init__.py
    β”‚   β”œβ”€β”€ struct.py                # Tier-1: list_to_linkedlist, etc.
    β”‚   └── semantic.py              # Tier-1.5: build_list_with_cycle, etc.
    β”œβ”€β”€ tree_node/                   # Depends on TreeNode
    β”‚   β”œβ”€β”€ __init__.py
    β”‚   └── struct.py                # Tier-1: list_to_tree, tree_to_list
    β”œβ”€β”€ node/                        # Depends on Node
    β”‚   β”œβ”€β”€ __init__.py
    β”‚   └── semantic.py              # Tier-1.5: build_random_pointer_list, etc.
    β”œβ”€β”€ node_graph/                  # Depends on NodeGraph
    β”‚   β”œβ”€β”€ __init__.py
    β”‚   └── struct.py                # Tier-1: adjacency_to_graph, etc.
    β”œβ”€β”€ node_nary/                   # Depends on NodeNary
    β”‚   β”œβ”€β”€ __init__.py
    β”‚   └── struct.py                # Tier-1: list_to_nary_tree, etc.
    └── doubly_list_node/            # Depends on DoublyListNode
        β”œβ”€β”€ __init__.py
        └── struct.py                # Tier-1: list_to_doubly_linked, etc.

Helper Functions Reference

ListNode Functions

Function Tier Description
list_to_linkedlist 1 [1,2,3] β†’ LinkedList
linkedlist_to_list 1 LinkedList β†’ [1,2,3]
build_list_with_cycle 1.5 Build list with cycle at pos
node_to_index 1.5 Find node's index in array
build_intersecting_lists 1.5 Build two intersecting lists

TreeNode Functions

Function Tier Description
list_to_tree 1 Level-order list β†’ BinaryTree
tree_to_list 1 BinaryTree β†’ level-order list

Node (Random Pointer) Functions

Function Tier Description
build_random_pointer_list 1.5 [[val, idx], ...] β†’ list with random pointers
encode_random_pointer_list 1.5 List with random pointers β†’ [[val, idx], ...]
verify_deep_copy 1.5 Verify deep copy has no shared nodes

NodeGraph Functions

Function Tier Description
adjacency_to_graph 1 Adjacency list β†’ Graph nodes
graph_to_adjacency 1 Graph nodes β†’ adjacency list

NodeNary Functions

Function Tier Description
list_to_nary_tree 1 Level-order with nulls β†’ N-ary tree
nary_tree_to_list 1 N-ary tree β†’ level-order with nulls

DoublyListNode Functions

Function Tier Description
list_to_doubly_linked 1 [1,2,3] β†’ DoublyLinkedList
doubly_linked_to_list 1 DoublyLinkedList β†’ [1,2,3]

Dependency Resolution

Dependencies are inferred from the directory structure:

Path Pattern Dependency
classes/*.py None
functions/list_node/*.py ListNode
functions/tree_node/*.py TreeNode
functions/node/*.py Node
functions/node_graph/*.py NodeGraph
functions/node_nary/*.py NodeNary
functions/doubly_list_node/*.py DoublyListNode

Tier Inference

Path Pattern Tier
classes/* base
functions/*/struct.py 1
functions/*/semantic.py 1.5

Usage

Import Mode (Runtime)

# In solution files
from runner.utils.codec import list_to_tree, TreeNode

root = list_to_tree([1, 2, 3])

IDE Support: Go to Definition works β€” jumps directly to tree_node.py.

Inline Mode (Codegen)

# In codegen
from codegen.core.catalog import get, get_with_deps

# Get single definition
code = get("ListNode")

# Get with dependencies resolved
code = get_with_deps("build_list_with_cycle")
# Returns: ListNode class + build_list_with_cycle function

Catalog API

from codegen.core.catalog import (
    get,            # Get single template code
    deps,           # Get dependency list
    get_with_deps,  # Get code with all dependencies
    list_all,       # List all available names
    list_classes,   # List all class names
    list_functions, # List all function names
    tier,           # Get tier for a name
)

# Examples
get("ListNode")  # Returns class definition
deps("list_to_tree")  # Returns ["TreeNode"]
get_with_deps("build_list_with_cycle")  # Returns ListNode + function
tier("list_to_tree")  # Returns "1"
tier("node_to_index")  # Returns "1.5"

Config Integration

config/problem-support.yaml only needs function names:

"0142":
  tier: "1.5"
  codec_mode: inline
  codec_hints:
    - build_list_with_cycle
    - node_to_index

No need to list ListNode β€” catalog resolves dependencies automatically via AST.


Adding New Codec

Add a new class

  1. Create runner/utils/codec/classes/new_class.py
  2. Add to classes/__init__.py
  3. Add to codec/__init__.py
  4. Done (catalog auto-discovers via AST)

Add a function

  1. Create directory runner/utils/codec/functions/new_class/
  2. Create struct.py (Tier-1) or semantic.py (Tier-1.5)
  3. Add to functions/new_class/__init__.py
  4. Add to functions/__init__.py
  5. Add to codec/__init__.py
  6. Update catalog/__init__.py dependency mapping
  7. Done

Key Benefits

Feature Benefit
Single Source No sync issues between runtime and inline
AST Extraction Precise code extraction without imports
Directory Structure Dependencies self-documenting
IDE Support Full Go to Definition works
DRY One copy of each function

Changelog

Date Change
2026-01-03 Initial contract
2026-01-03 Refactored: Single source with AST extraction