Skip to content

feat(build): wire the planner into the request lifecycle - #3

Merged
srmcguirt merged 2 commits into
mainfrom
feat/plan-wiring
Aug 1, 2026
Merged

srmcguirt merged 2 commits into
mainfrom
feat/plan-wiring

Conversation

@srmcguirt

Copy link
Copy Markdown
Member

The engine (magna-core), the batched steps (magna-dataplan), and the register→execute→read bridge (PlanContext) were all complete and tested — and nothing called them. There was no async_graphql Extension anywhere that turned an incoming operation into a plan. This PR is that missing piece, in the Grafast shape: plan the whole operation, run one DAG, resolve fields from its results.

Commit 1 — fix(dataplan): tweak-aware step fingerprints

PgSelectStep's config hash excluded tweaks, but tweaks carry WHERE/ORDER/LIMIT and the optimizer merges steps on (type_id, deps, config_hash) — so two same-table steps with different filter arguments shared a fingerprint and would be collapsed, serving one field's rows to the other. Latent while nothing planned; live data-bleed the moment planning gained a caller (i.e., commit 2).

  • apply_keyed(key, tweak) — caller declares the argument values; key hashes into the fingerprint in registration order. Same args ⇒ steps still dedupe; different args ⇒ never collide.
  • apply(tweak) (unkeyed) — opaque closure, unknowable effect ⇒ the step's own id folds in, fingerprint unique, never dedupes. Safe by default.
  • 4 regression tests pin the property.

Commit 2 — feat(build): the wiring

Piece Role
PlanRegistry plan fns keyed by (type, field); empty ⇒ extension not attached ⇒ unplanned schemas pay nothing
PlanScope what a plan fn works against — allocates step ids, registers into the operation's one shared plan (what makes cross-field dedup structural)
PlanResults request-scoped handle; resolvers read output by response key (alias or name)
PlanExtension per-request: prepare_request inserts the handle → parse_query captures the operation → execute walks root query selections, resolves args against variables, calls plan fns, executes the DAG, publishes — then resolution runs
ExtensionContext::plan_field the embedder registration surface, available in every phase

Plus a latent bug the wiring exposed: dedup removed the duplicate step and rewrote graph edges but discarded the removed→canonical remap — get_result(deduped_id) returned None, breaking result lookup for exactly the fields dedup helped. OptimizedPlan now carries remap; PlanContext::get_result forwards through it.

Scope (first pass, documented in the module)

Root query fields only; fragment-reached fields are not planned and error loudly rather than silently falling back. Mutations/subscriptions unplanned. Nested selection planning (steps depending on parent output) is the next stage.

Proof — tests/plan_wiring.rs, no database

  • a field served entirely through the plan path
  • { a: answer(n:7) b: answer(n:7) } → one step execution, both fields resolve (remap fix, live)
  • { a: answer(n:1) b: answer(n:2) } → two executions — different args never share (the data-bleed guard)
  • variables resolve into plan arguments
  • unplanned operations execute zero steps

Workspace: 405 passed, 0 failed. (fmt/clippy CI lanes remain red from pre-existing main drift — untouched here, same as #2.)

First consumer: fw-api's document roots (fw-graph step library over fw-store), landing next in the data repo.

🤖 Generated with Claude Code

srmcguirt and others added 2 commits August 1, 2026 11:11
…dness

PgSelectStep's config hash covered schema/table/selected_cols/fk_col and
deliberately excluded tweaks ("tweaks are runtime, not plan-time"). But
tweaks are registered at plan time and carry WHERE/ORDER/LIMIT, and the
optimizer's deduplicate() merges steps on (type_id, deps, config_hash) —
so two steps over the same table with DIFFERENT filter arguments shared a
fingerprint, and the optimizer would collapse them and serve one field's
rows to the other field. Latent today (nothing constructs a plan yet);
live data-bleed the moment the plan path gains a caller.

The fix makes tweak identity part of the fingerprint, two ways:

  apply_keyed(key, tweak)  the caller declares the argument values the
                           tweak was built from; the key hashes into the
                           fingerprint in registration order. Same table +
                           same declared args => steps still dedupe (the
                           whole point of the optimizer); different args
                           => never collide.

  apply(tweak)             unkeyed: the closure is opaque, its effect on
                           the SQL is unknowable, so the step's own id is
                           folded in, making the fingerprint unique. An
                           unkeyed step never deduplicates — safe by
                           default rather than fast by assumption.

Four regression tests pin the property: same-args dedupe, different-args
never collide, unkeyed poisons dedup, untweaked baseline still dedupes.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
… + PlanExtension

The engine (magna-core), the batched steps (magna-dataplan), and the
register→execute→read bridge (PlanContext) were all complete and tested —
and nothing called them. The auto-generated resolvers bypass the planner
via QueryExecutor, and there was no async-graphql Extension anywhere that
would turn an incoming operation into a plan. This commit is that missing
piece, in the Grafast shape: plan the whole operation, run one DAG,
resolve fields from its results.

WHAT'S NEW

  plan_extension.rs
    PlanRegistry     plan fns keyed by (type, field), populated at
                     schema-build time; empty registry ⇒ extension not
                     attached ⇒ unplanned schemas pay nothing.
    PlanScope        what a plan fn works against: allocates step ids,
                     registers steps into the operation's ONE shared plan
                     (which is what makes cross-field dedup structural).
    PlanResults      request-scoped handle; resolvers read their output by
                     response key (alias if present, else field name).
    PlanExtension    per-request lifecycle: prepare_request inserts the
                     results handle → parse_query captures the operation →
                     execute() walks root query selections, resolves field
                     arguments against variables, calls plan fns, executes
                     the plan, publishes outputs — then resolution runs.

  ExtensionContext::plan_field(type, field, fn)
    The embedder registration surface, available in every phase. The field
    itself is still registered via query_field with a resolver that reads
    PlanResults — planning supplies the data, not the schema entry.

  OptimizedPlan.remap + PlanContext follows it
    Dedup removed the duplicate step and rewrote graph edges, but DISCARDED
    the removed→canonical remap — so get_result(deduped_id) returned None,
    breaking result lookup for exactly the fields dedup helped. The remap
    now survives into PlanContext and get_result forwards through it.

SCOPE (first pass, documented in the module)
  Root query fields only; fragment-reached fields are not planned and must
  error loudly rather than silently fall back. Mutations/subscriptions
  unplanned. Nested selection planning (steps depending on parent output)
  is the next stage, not this one.

PROOF (tests/plan_wiring.rs — no database)
  · a field served entirely through the plan path
  · { a: answer(n:7) b: answer(n:7) } → ONE step execution, BOTH fields
    resolve (the remap fix, live)
  · { a: answer(n:1) b: answer(n:2) } → two executions — different args
    never share (the data-bleed guard from the fingerprint commit)
  · variables resolve into plan arguments
  · unplanned operations execute zero steps

Workspace: 405 passed, 0 failed.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Aug 1, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 399d94b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@srmcguirt
srmcguirt merged commit 0a7b6e0 into main Aug 1, 2026
2 of 4 checks passed
@srmcguirt
srmcguirt deleted the feat/plan-wiring branch August 1, 2026 15:22
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