Skip to content

fix(sync-service): promote adopted dependency consumers to transaction writes - #4780

Open
alco wants to merge 3 commits into
fix/start-dependency-consumer-on-demandfrom
fix/promote-inner-consumer-write-unit
Open

fix(sync-service): promote adopted dependency consumers to transaction writes#4780
alco wants to merge 3 commits into
fix/start-dependency-consumer-on-demandfrom
fix/promote-inner-consumer-write-unit

Conversation

@alco

@alco alco commented Aug 27, 2026

Copy link
Copy Markdown
Member

Background

Shape consumers without their own subquery dependencies start with write_unit: :txn_fragment. Consumers known to be inner shapes start with write_unit: :txn, because dependency materializers and outer-shape evaluation require transaction-atomic updates.

The startup hint is not sufficient when an already-running consumer is later adopted as a dependency. This can happen in two pre-existing sequences:

  • a standalone shape is requested directly and later reused as a parent's subquery dependency;
  • after restart, transaction routing lazily recovers an orphaned dependency as a standalone shape before a new parent reuses it.

In both cases the dependency materializer can subscribe to a consumer that remains in fragment-write mode.

Fix

Treat materializer subscription as the authoritative signal that a consumer is an inner shape and promote it to whole-transaction writes.

If no transaction is in progress, promotion is immediate. If the subscription arrives during a fragmented transaction, that transaction finishes in fragment mode and promotion occurs at its commit boundary. This avoids mixing already-written fragments with a buffered tail of the same transaction.

The existing is_subquery_shape? startup option remains a fast path for consumers known to be dependencies when they start.

Tests

  • verifies an already-running standalone consumer is reused by a parent and promoted from :txn_fragment to :txn;
  • verifies subscription during an in-flight fragmented transaction defers promotion until commit;
  • covers the immediate and deferred state transitions directly;
  • full relevant files: 110 passed, 1 excluded across ShapeCache, consumer state, and consumer tests.

Stack

This PR is stacked on #4779 and should be reviewed against fix/start-dependency-consumer-on-demand until that PR merges.

@codecov

codecov Bot commented Aug 27, 2026

Copy link
Copy Markdown

Codecov Report

βœ… All modified and coverable lines are covered by tests.
βœ… Project coverage is 60.01%. Comparing base (e75eb2a) to head (91fbe92).
βœ… All tests successful. No failed tests found.

Additional details and impacted files
@@                           Coverage Diff                            @@
##           fix/start-dependency-consumer-on-demand    #4780   +/-   ##
========================================================================
  Coverage                                    60.01%   60.01%           
========================================================================
  Files                                          397      397           
  Lines                                        43772    43772           
  Branches                                     12590    12588    -2     
========================================================================
+ Hits                                         26268    26271    +3     
+ Misses                                       17423    17420    -3     
  Partials                                        81       81           
Flag Coverage Ξ”
packages/agents 72.64% <ΓΈ> (ΓΈ)
packages/agents-mcp 77.70% <ΓΈ> (ΓΈ)
packages/agents-mobile 80.67% <ΓΈ> (ΓΈ)
packages/agents-runtime 83.72% <ΓΈ> (ΓΈ)
packages/agents-server 75.45% <ΓΈ> (+0.04%) ⬆️
packages/agents-server-ui 8.32% <ΓΈ> (ΓΈ)
packages/electric-ax 51.06% <ΓΈ> (ΓΈ)
packages/experimental 87.73% <ΓΈ> (ΓΈ)
packages/react-hooks 86.48% <ΓΈ> (ΓΈ)
packages/start 82.83% <ΓΈ> (ΓΈ)
packages/typescript-client 91.95% <ΓΈ> (ΓΈ)
packages/y-electric 56.05% <ΓΈ> (ΓΈ)
typescript 60.01% <ΓΈ> (+<0.01%) ⬆️
unit-tests 60.01% <ΓΈ> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

β˜” View full report in Codecov by Harness.
πŸ“’ Have feedback on the report? Share it here.

πŸš€ New features to boost your workflow:
  • πŸ“¦ JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

alco and others added 2 commits August 27, 2026 12:24
`Consumer.subscribe_materializer/3` used the default 5-second `GenServer.call`
timeout, unlike every other call on the materializer startup path
(`await_snapshot_start/3`, `Materializer.subscribe/1`, `wait_until_ready/1`,
`new_changes/3`), which already wait with `:infinity` and rely on process
monitors for liveness. Bring it in line so the consumer is free to reply
whenever it is actually safe to subscribe.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016uMB7eVy1iXSfY8gqcn5A8
Deferring only the write-unit promotion was not enough. A materializer's
initial history read is bounded by storage's `last_seen_txn_offset`, which
advances only on commit, while a fragment-streaming consumer's
`latest_offset` advances with every fragment it writes. Replying to a
mid-transaction subscription with that offset let the materializer skip
past fragments it could not yet read, silently dropping the head of the
transaction from its index: a two-fragment transaction inserting rows 1
and 2 left the materializer with link values `[2]`.

Park the subscription instead: when `subscribe_materializer` arrives while
a fragmented transaction is pending, stash `{pid, from}` in
`deferred_materializer_subscription` and complete it (monitor, promote,
reply) from `finish_pending_txn/1` at the commit boundary. The consumer is
not marked as subscribed during the wait, so it never calls into the
blocked materializer; the materializer then reads a fully committed
history and receives every later change as a whole transaction.

`State.mark_materializer_subscribed/1` now assumes that invariant and
promotes directly; the separate `maybe_promote_write_unit/1` hook is gone.
A consumer already buffering whole transactions can still be subscribed
mid-transaction, since it has no partial writes on disk.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016uMB7eVy1iXSfY8gqcn5A8
@netlify

netlify Bot commented Aug 27, 2026

Copy link
Copy Markdown

βœ… Deploy Preview for electric-next ready!

Name Link
πŸ”¨ Latest commit 91fbe92
πŸ” Latest deploy log https://app.netlify.com/projects/electric-next/deploys/6a9012c45ad41b000809cb1d
😎 Deploy Preview https://deploy-preview-4780--electric-next.netlify.app
πŸ“± Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

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