Skip to content

refactor(grid3d): the 3D views are split into a pure core, ports and adapters, and can go fullscreen and be retitled - #87

Merged
dmccoystephenson merged 10 commits into
mainfrom
refactor/grid3d-ports
Nov 21, 2026
Merged

dmccoystephenson merged 10 commits into
mainfrom
refactor/grid3d-ports

Conversation

@dmccoystephenson

Copy link
Copy Markdown
Member

Summary

The grid3d views (Grid3DView on Strata's Python binding, Grid3DWebView over pygbag and the
page's grid relay) were restructured into a pure core plus ports and adapters, one interface or
implementation per file. Behaviour is unchanged, and every name importable today stays
importable. Two window calls were added so games can stop reaching into the engine. This PR is
stacked on the test-only PR that pins the views' seams; that one merges first.

This was approved as a port map beforehand. Per the owner's decisions, relay.py, the pygbag
template and its JS are untouched; the clock= callable stays as the clock seam; the desktop and
web event shapes stay as they are. It merges untagged and ships with 0.6.0.

Layout

grid3d/
  __init__.py                 public API, unchanged names (+ architecture note)
  scene.py board.py animate.py pick.py text.py ui.py touchkeys.py   unchanged (already pure)
  errors.py     Unavailable                        (model, from view.py)
  geometry.py   ViewGeometry, sun, near/far        (core, from view.py / web.py)
  state.py      ViewState: board, pieces, animator, HUD, scene()   (core; both views had a copy)
  wire.py       wire format 1.0, byte for byte     (core, from web.py)
  events.py     EVENT_*, Event, toPygameEvents, pygameKey          (core, from web.py)
  ports/        StrataLoader, SceneRenderer, EventSource, Surface, WindowChrome,
                GridRelay, PageObjects             (one Protocol per file)
  adapters/     BindingStrataLoader, StrataEngine, RelayDisplay, PlatformRelay,
                RecordingRelay, PygbagPageObjects  (one class per file)
  view.py       Grid3DView + desktop composition root (STRATA_LOADER); re-exports its 0.5.3 names
  web.py        Grid3DWebView, page helpers + browser composition root (PAGE); re-exports its 0.5.3 names
  relay.py, templates/pygbag.tmpl                  unchanged
Port Question it answers Adapters
StrataLoader Can Strata run here? Open an engine BindingStrataLoader
SceneRenderer Draw this Scene now StrataEngine, RelayDisplay
EventSource What input came since the last poll? StrataEngine, RelayDisplay
Surface Resize, snapshot, close StrataEngine, RelayDisplay
WindowChrome Fullscreen and the window title StrataEngine, RelayDisplay
GridRelay Talk to the page's grid relay PlatformRelay, RecordingRelay
PageObjects Which of the page's JS objects exist? PygbagPageObjects

New API

  • view.setFullscreen(enabled) and view.setTitle(title) return True when done and False
    when they can't: the loaded Strata lacks the call (0.8.0 has no strata_set_title), the view is
    closed, or, for fullscreen, the view is headless (Strata raises there). On Grid3DWebView both
    always return False. apex and ophidian can move to these later; view.strata, view.lib and
    view.engine keep working.
  • Grid3DView.using(display, ...) builds a view over any display with the ports, for tests and tools.
  • view.STRATA_LOADER and web.PAGE are the composition roots' defaults; tests can swap them.
  • PlatformRelay(relay=None, page=None) takes the page it looks in.
  • pollEvent() was not added: pollEvents() already drains the engine the way the games' loops do.

Visible differences (no behaviour change, but worth knowing)

  • Unavailable is defined in grid3d.errors, so tracebacks print
    preponderous.graphik.grid3d.errors.Unavailable. It is the same class object under every old
    name.
  • view.strata, view.lib, view.engine, webView.relay, webView.sent and webView.lastSent
    are now read-only properties: reading them is unchanged, assigning to them raises.
  • Monkeypatching view.STRATA_VERSION or view.loadStrata no longer changes what open() loads;
    they live in adapters/binding_strata_loader.py, and view.STRATA_LOADER is the seam now.
  • The private web._NoBoard / web._NO_BOARD stand-in moved into RelayDisplay.

Commits (one per step)

  1. the views' pure halves are moved into model and core modules
  2. the views' ports are declared, with fakes and contract tests
  3. the views' I/O is moved into adapters, one per file
  4. the views are written against their ports
  5. view.py and web.py are made the views' composition roots
  6. in-repo callers use the new homes; view and web keep their names
  7. feat: views can go fullscreen and be retitled without reaching into the engine
  8. test: CI keeps grid3d's layers pointing inward
  9. docs: how the views are built, and the new window calls
  10. ci: the release-archive install checks grid3d's subpackages and template

Test plan

  • Full suite (Python 3.12, strata-engine 0.8.0, Node present, so the template JS tests
    run): 534 → 703 passed, 0 skipped, 0 failed; same with GRAPHIK_REQUIRE_STRATA=1
  • CI render job (grid3d, llvmpipe, GRAPHIK_REQUIRE_STRATA=1): 353 → 522 passed;
    golden board image and frame-cost tests green
  • Python 3.10 without Strata: 700 passed, 3 skipped (the render tests, as in CI's main
    job); the grid3d sources parse with 3.9 grammar
  • Parity, old vs new: 7,068 cases, 0 differences, run through the skill's
    parity.py with one driver per side against each tree (each side imports its own tree):
    • 4,000 wire cases (every encoder, valid and refused values, NUL, lone-surrogate and non-ASCII
      labels, viewports, merged tiles, tints)
    • 1,500 view sessions on both views (scenes per tick, picks, insets, resize, drawScene, every
      engine call and every relay message byte)
    • 1,568 event-conversion cases
    • a deliberate one-constant change was caught, so the harness can see a difference
  • Golden bytes: golden/wire-1.0.hex is unchanged and its test passes
  • Frozen assertions: the only edits to existing tests change imports (web. → wire.,
    view.renderItems → adapters.strata_engine); after normalising those, the old and new
    assertion lines are identical. Test count only went up.
  • Architecture: the skill's check_layers.py reports 0 problems, and new
    test_grid3d_architecture.py enforces the same rule in CI (verified to catch an injected
    violation)
  • Release-archive install (built like test.yml): adapters/, ports/ and the template
    install, and the CI job now checks them
  • Frame time, 64×64 board headless on llvmpipe: old and new within noise (about 46–50 ms
    per frame each; the time is raster, not Python)
  • Games against this branch (each game's own suite, its default branch, its own deps):
    the same counts on 0.5.3 and on this branch:
    ophidian 804 passed, 2 skipped (both skips are the game's own, the same on both); apex 746
    passed; Tic-Tak-Toe 376 passed
  • Browser e2e: Tic-Tak-Toe (master) was built with pygbag and this branch's
    graphik; the adapters/ and ports/ files are in src.apk. Its Playwright e2e (phone and
    desktop; 3D, 2D, win/loss/tie, falling back to 2D) passed 151/151 on a second run. The first
    run had 2 timing misses: the HUD check on the desktop loss screen gave up, though the
    screenshot taken just after shows the loss screen drawn correctly. That run overlapped
    another browser job on the same machine. The existing 0.5.3 build passed 161/161 with the
    same harness (its game branch has 10 more checks).

drafted by Claude on behalf of Daniel Stephenson

🤖 Generated with Claude Code

dmccoystephenson and others added 10 commits November 20, 2026 19:15
…e modules

The parts of view.py and web.py that touch nothing outside Python were
moved, nearly verbatim, into their own modules:

- errors.py: Unavailable
- geometry.py: ViewGeometry, with the sun direction and near/far planes
  both views send to Strata
- wire.py: the grid relay's wire format 1.0 (encoders, decode, readHeader,
  WireError, limits), byte for byte as before
- events.py: the event types, Event, pygameKey and toPygameEvents
- state.py: ViewState, the board, pieces, animator and HUD that
  Grid3DView and Grid3DWebView each kept in their own copy; each view now
  calls its steps in the order it did before

view.py and web.py re-export every name they had, as the same objects, so
grid3d.view and grid3d.web import as before. No behaviour was changed.

drafted by Claude on behalf of Daniel Stephenson

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LYNeuoCqxFpUXrFhxnTg89
…act tests

Six ports were added under grid3d/ports/, one interface per file, each
with its contract in its doc comment:

- SceneRenderer: draw one complete Scene now
- EventSource: the input since the last poll
- Surface: resize, savePng, close
- StrataLoader: open a Strata engine here, or say why not
- GridRelay: the page's grid relay (its own six-call JS API)
- PageObjects: the page's JS objects by name

test_grid3d_ports.py runs each contract against a fake. The same
contracts run against the production adapters once they exist (their
cases skip until then). No behaviour was changed.

drafted by Claude on behalf of Daniel Stephenson

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LYNeuoCqxFpUXrFhxnTg89
The code that talks to Strata, the page and pygbag was moved, nearly
verbatim, into grid3d/adapters/:

- BindingStrataLoader: loadStrata, the pinned STRATA_VERSION, the SDL and
  Gallium defaults, the engine, its lighting and sun
- StrataEngine: drawing a Scene through the binding (renderItems,
  labelBytes), polling, resize, savePng, close
- RelayDisplay: drawing a Scene as relay messages (only what changed),
  sending, Event polling, the savePng refusal
- PlatformRelay, RecordingRelay: the two GridRelays
- PygbagPageObjects: window.<name> under pygbag

Grid3DView and Grid3DWebView now delegate to them and keep every
attribute games and tests read (view.strata, view.lib, view.engine,
view.relay, view.sent, view.lastSent), as properties. view.py and web.py
re-export every moved name. The port contracts now run against these
adapters as well as the fakes. No behaviour was changed.

drafted by Claude on behalf of Daniel Stephenson

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LYNeuoCqxFpUXrFhxnTg89
Grid3DView now draws, polls, resizes, saves and closes through whatever
display it is given: Grid3DView.using(display, ...) takes any
SceneRenderer, EventSource and Surface whose `strata` names the event
types, and the old constructor wraps the binding in a StrataEngine as
before. The page helpers (wants3D, set3D, buttonArea, touchScreen,
pageObject) and Grid3DWebView.open read the page through web.PAGE, a
PageObjects, and PlatformRelay takes the page it looks in.

test_grid3d_core.py drives both views over fakes of their ports, with no
stand-in strata module and no patched sys.platform. No behaviour was
changed.

drafted by Claude on behalf of Daniel Stephenson

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LYNeuoCqxFpUXrFhxnTg89
…roots

The production wiring was put in one place per view, as defaults:
view.STRATA_LOADER (a BindingStrataLoader) opens the desktop engine for
Grid3DView.open(), read at each call so a test or tool can swap it, and
web.PAGE plus PlatformRelay are what Grid3DWebView.open() and the page
helpers use. Both modules' docstrings now say how they are wired. A test
opens a Grid3DView through a fake loader. No behaviour was changed.

drafted by Claude on behalf of Daniel Stephenson

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LYNeuoCqxFpUXrFhxnTg89
…p their names

The wire-format and render tests now import the encoders from wire.py
and renderItems / labelBytes from adapters/strata_engine.py, where they
live; their assertions are unchanged. grid3d.view and grid3d.web are kept
as re-export shims, since code outside this repository imports from them
(ophidian, apex, Tic-Tak-Toe). test_grid3d_shims.py checks every name
either module had in 0.5.3 is still there, as the same object as its new
home, except the private _NoBoard stand-in, which moved into
RelayDisplay. view also keeps Animator and the picking functions it used
to import. No behaviour was changed.

drafted by Claude on behalf of Daniel Stephenson

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LYNeuoCqxFpUXrFhxnTg89
…g into the engine

Grid3DView.setFullscreen(enabled) and setTitle(title) were added, so
games can stop calling view.engine.set_fullscreen / set_title and
view.lib.has themselves (apex and ophidian do today; view.strata,
view.lib and view.engine keep working). They return True when done and
False when the loaded Strata lacks the call (0.8.0 has no
strata_set_title), the view is closed, or, for fullscreen, the view is
headless, where Strata would raise. Grid3DWebView has both and always
returns False: the browser owns its window and the page its title.

They sit on a new WindowChrome port, implemented by StrataEngine and
RelayDisplay and covered by the port contract tests. A pollEvent() was
not added: pollEvents() already drains the engine the way the games'
own loops do.

drafted by Claude on behalf of Daniel Stephenson

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LYNeuoCqxFpUXrFhxnTg89
test_grid3d_architecture.py walks every model, core and port module with
ast and fails when one imports an adapter or a composition root (view,
web, relay), resolving relative imports the way Python does, and fails
when a port or adapter file holds more than one class. A test feeds it a
violating file, so a walk that sees nothing can't pass unnoticed.

drafted by Claude on behalf of Daniel Stephenson

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LYNeuoCqxFpUXrFhxnTg89
…documented

docs/grid3d.md gained a "How the views are built" section (the layers,
the port and adapter table, the architecture and contract tests, and how
to add a backend: one adapter file plus a registry entry), rows for
setFullscreen, setTitle, view.strata / lib / engine, Grid3DView.using
and web.PAGE, and PlatformRelay's page argument. The package docstring
carries the same diagram, and test.yml's comment says where
STRATA_VERSION now lives.

drafted by Claude on behalf of Daniel Stephenson

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LYNeuoCqxFpUXrFhxnTg89
grid3d now has subpackages (ports/, adapters/). The archive-install job
imported only the top-level package, so a build that dropped them would
have passed. It now imports grid3d's adapters, ports and web from the
installed copy and checks the pygbag page template is there.

drafted by Claude on behalf of Daniel Stephenson

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LYNeuoCqxFpUXrFhxnTg89
@dmccoystephenson
dmccoystephenson merged commit 2db8884 into main Nov 21, 2026
7 checks passed
@dmccoystephenson
dmccoystephenson deleted the refactor/grid3d-ports branch November 21, 2026 02:17
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.

1 participant