Skip to content

Commit e33058a

Browse files
Merge pull request #2 from BlockScience/feat/pipeline-decomposition
feat(compiler): decompose pipeline into reusable stages
2 parents 4aaf57f + 47b5451 commit e33058a

43 files changed

Lines changed: 960 additions & 106 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CLAUDE.md

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# CLAUDE.md
22

3-
This file provides guidance to Claude Code when working in this monorepo.
3+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
44

55
## Project
66

7-
`gds-core` — monorepo for the Generalized Dynamical Systems ecosystem. Contains four packages managed as a uv workspace.
7+
`gds-core` — monorepo for the Generalized Dynamical Systems ecosystem. Typed compositional specifications for complex systems, grounded in [GDS theory](https://doi.org/10.57938/e8d456ea-d975-4111-ac41-052ce73cb0cc). Four packages managed as a uv workspace.
88

99
## Packages
1010

@@ -27,6 +27,9 @@ uv run --package gds-viz pytest packages/gds-viz/tests -v
2727
uv run --package gds-games pytest packages/gds-games/tests -v
2828
uv run --package gds-examples pytest packages/gds-examples -v
2929

30+
# Run a single test
31+
uv run --package gds-framework pytest packages/gds-framework/tests/test_blocks.py::TestStackComposition::test_rshift_operator -v
32+
3033
# Run all tests
3134
uv run --package gds-framework pytest packages/gds-framework/tests packages/gds-viz/tests packages/gds-games/tests packages/gds-examples -v
3235

@@ -58,10 +61,66 @@ gds-games ← game theory DSL (depends on gds-framework)
5861
gds-examples ← tutorials (depends on gds-framework + gds-viz)
5962
```
6063

64+
### gds-framework: Two-Layer Design
65+
66+
**Layer 1 — Composition Algebra** (`blocks/`, `compiler/`, `ir/`, `verification/generic_checks.py`):
67+
Domain-neutral engine. Blocks with bidirectional typed interfaces, composed via four operators (`>>`, `|`, `.feedback()`, `.loop()`). A 3-stage compiler flattens composition trees into flat IR (blocks + wirings + hierarchy). Six generic verification checks (G-001..G-006) validate structural properties on the IR.
68+
69+
**Layer 2 — Specification Framework** (`spec.py`, `canonical.py`, `state.py`, `spaces.py`, `types/`):
70+
Where GDS theory lives. `GDSSpec` is the central registry for types, spaces, entities, blocks, wirings, and parameters. `project_canonical()` derives the formal `h = f ∘ g` decomposition. Seven semantic checks (SC-001..SC-007) validate domain properties on the spec.
71+
72+
These layers are loosely coupled — you can use the composition algebra without `GDSSpec`, and `GDSSpec` does not depend on the compiler.
73+
74+
### Two Type Systems
75+
76+
These coexist at different levels:
77+
78+
1. **Token-based** (`types/tokens.py`) — structural set matching at composition/wiring time. Port names auto-tokenize (split on spaces, lowercase → frozenset). The `>>` operator and auto-wiring use token overlap for matching. `"Heater Command"` auto-wires to `"Command Signal"` because they share the token `"command"`.
79+
80+
2. **TypeDef-based** (`types/typedef.py`) — runtime validation at the data level. TypeDef wraps a Python type + optional constraint predicate. Used by Spaces and Entities to validate actual data values. Never called during compilation.
81+
82+
### Compilation Pipeline
83+
84+
```
85+
Block tree → flatten() → list[AtomicBlock] → block_compiler() → list[BlockIR]
86+
→ _walk_wirings() → list[WiringIR] (explicit + auto-wired)
87+
→ _extract_hierarchy() → HierarchyNodeIR tree
88+
= SystemIR(blocks, wirings, hierarchy)
89+
```
90+
91+
### How gds-games Extends gds-framework
92+
93+
`gds-games` subclasses `AtomicBlock` as `OpenGame`, adding a `Signature(Interface)` that maps game theory's `(X, Y, R, S)` to GDS's `(forward_in, forward_out, backward_in, backward_out)`. It adds 6 atomic game types, a `Pattern` container (analogous to `GDSSpec`), its own compiler (`compile_to_ir()``PatternIR`), and 13 OGS-specific verification checks.
94+
95+
The critical interop bridge is `PatternIR.to_system_ir()` — projects OGS IR to GDS `SystemIR`, enabling reuse of all 6 generic GDS verification checks without duplication. OGS also adds `CorecursiveLoop` (`.corecursive()`), which maps to GDS `TemporalLoop` at the IR level.
96+
97+
### Block Hierarchy (Sealed)
98+
99+
Only 5 concrete Block types exist — domain packages subclass `AtomicBlock` only, never the operators:
100+
- `AtomicBlock` — leaf node
101+
- `StackComposition` (`>>`) — sequential, validates token overlap
102+
- `ParallelComposition` (`|`) — independent, no validation
103+
- `FeedbackLoop` (`.feedback()`) — backward within timestep, CONTRAVARIANT
104+
- `TemporalLoop` (`.loop()`) — forward across timesteps, COVARIANT only
105+
106+
Block roles (`BoundaryAction`, `Policy`, `Mechanism`, `ControlAction`) subclass `AtomicBlock` and enforce constraints at construction via `@model_validator`.
107+
108+
### Verification: Two Registries
109+
110+
Both use the pluggable pattern: `Callable[[T], list[Finding]]`.
111+
112+
- **Generic checks (G-001..G-006)** operate on `SystemIR` — structural topology only
113+
- **Semantic checks (SC-001..SC-007)** operate on `GDSSpec` — domain properties (completeness, determinism, reachability, type safety, parameter references, canonical wellformedness)
114+
61115
### Key Conventions
62116

63-
- All data models are Pydantic v2 BaseModel
117+
- All data models are Pydantic v2 `BaseModel` — frozen for value objects, mutable for registries
118+
- `@model_validator(mode="after")` returning `Self` for construction-time invariant enforcement
64119
- Absolute imports only (never relative)
65-
- Ruff config inherited from root (line-length 88)
120+
- `TYPE_CHECKING` guard in `blocks/base.py` to break circular dependency with `blocks/composition.py`
121+
- Ruff config at root: line-length 88, rules `E, W, F, I, UP, B, SIM, TCH, RUF`
122+
- Tags (`Tagged` mixin) are inert — stripped at compile time, never affect verification
123+
- Parameters (Θ) are structural metadata — GDS never assigns values or binds them
124+
- `GDSSpec.collect()` type-dispatches TypeDef/Space/Entity/Block/ParameterDef; SpecWiring stays explicit via `register_wiring()`
66125
- Each package published independently to PyPI via tag-based workflow (`gds-framework/v0.3.1`)
67126
- Per-package CLAUDE.md files contain package-specific architecture details

0 commit comments

Comments
 (0)