Skip to content

Starts a child for every pass of a loop - #302

Merged
johnnyt merged 4 commits into
mainfrom
sp-5pfj-loop-children
Nov 22, 2026
Merged

johnnyt merged 4 commits into
mainfrom
sp-5pfj-loop-children

Conversation

@johnnyt

@johnnyt johnnyt commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

What

A durable subchart inside a loop started only its first child. Every pass of the same <invoke> element asked for the one id Linkage.child_execution_id/3 derives, so the second pass found the first pass's finished child, took it for an at-least-once re-drive, adopted it, and the parent waited on it for good.

This PR gives every pass a child of its own and keeps the re-drive adoption, and records the design as an Amendment to ADR-0008 at proposed. Ruled by the operator, 2026-11-22: the fix lives in statifier_persistence, with the record in the same PR as the code, and no statifier_blocks change.

How

  • The pass token. Linkage.invocation/1 (new) writes "<macrostep>.<round>" from the invoke effect the interpreter emitted. Both counters are in the parent's persisted position, so a re-drive of the step stamps the same token and any later pass a different one.
  • The ids. The first pass keeps child_execution_id/3's id, so a fresh single child outside a loop is stored under the id it always was. A later pass's child takes Linkage.child_execution_id/4 (new): that id, then @, then the token.
  • Which id a pass takes. A single child is read for before it is created (existing/2, create_child/6, first_child/2, later_pass/5 in driver.ex). At the first id: nothing found creates there; a child of this parent and invocation that has not ended is adopted, as before; one that has ended belongs to an earlier pass, and the pass goes to its own id, where a child found is adopted (the token makes that exact). Rows written by 0.26.0 read by the same rule, so nothing is migrated.
  • Answer routing. A loop reuses the invoke id on every pass, and the parent's door checked liveness by invoke id alone. A single child's answer (from answer_parent/3 or the automatic answer) is now delivered only when the invoke id reads live and the child is the newest of the invocation's children (current_pass?/3, read inside the parent's step): the first pass's child is the oldest, and later passes are ordered by token. Any other answer is discarded: answer_parent/3 returns {:discarded, execution}, as it does for an invocation that is no longer live. A later pass's child that ended before its pass was re-driven is adopted by the re-drive, which delivers its recorded answer (adopt_later/2).
  • Where routing is exact, and the known limit. It is exact wherever a child's create commits with the parent's step: on Postgres under the default AdapterLock a lost drive leaves no child behind, so every stored child is one the parent started. On a store where the two commit apart, such as the in-memory adapter, a lost drive can leave a later pass's child the parent never started. That child ranks newest, so the waited-on pass's answer is discarded until it ends, and its answer is then taken for that pass. When no other step of the parent comes between the lost drive and that answer, the step the answer drives re-uses the lost macrostep and round, so the next pass adopts the same child and takes its answer again: one answer satisfies both passes, and the waited-on pass's own answer is never taken. If it had already ended, the parent waits until the host delivers that child's answer again. Ruled by the operator, 2026-11-22: this is a named known limit of this release. Exact routing on every store needs the parent's persisted position to record each live invocation's counters, which is a later change outside this package.
  • The exit's cancel. A loop's exit and re-entry land in one step, so on a re-drive decision 5's cancel for the invocation being left would also reach the later pass's child the interrupted drive had created. The {:cancel_invoke, _} arm now spares a later pass's child whose token is at or after the cancel effect's own counters (started_before?/3), through Executions.cascade_cancel_where/4, which is @doc false. cascade_cancel/3 is unchanged.
  • Postgres. Reading before creating also fixes a single child's re-drive on Postgres: the refused insert used to abort the parent's lock transaction, and the adoption read after it raised in_failed_sql_transaction.
  • Fan-out (start_child_at/6) is unchanged: same ids, same create-then-adopt order, same settlement.

What a host sees, all in changelog.d/sp-5pfj.md: two new functions (Added); more adapter calls in [:statifier_persistence, :adapter, :call] (Changed): a :fetch_execution per single child start, which answers :execution_not_found for a fresh child, and a :list_executions_by_metadata per single child answer while the invocation is live; and three fixes (Fixed): the routing, the loop, and the Postgres re-drive.

Provenance

Engineering choices made for this PR, each recorded in the Amendment:

  • The token is the macrostep and round on the invoke effect, not a stored count, so nothing new is stored. A count cannot tell a re-drive from the next pass, and storing a token in the linkage would need a new V09-style column and a migration for a host with linkage_columns: true.
  • The first pass keeps the old id instead of every child moving to a token id, because hosts derive that id today; the reference embedder's execution feed does.
  • One case is accepted and stated in the Amendment's case table: a first pass's child created by an interrupted drive that then ended before the re-drive is not adopted, and the re-drive starts a later pass's child. The cost is that the child's work runs twice. The first pass's id carries no token, so this child cannot be told apart from an earlier pass's ended child, and adopting it would leave the parent waiting for an answer already discarded.
  • An earlier version of this PR also excluded a later pass's child whose macrostep was after the parent's stored one. Review showed that macrostep numbers are reused after a lost drive, so the guard did not hold once another event was persisted. It is gone, and the case it aimed at is the named limit above.
  • If listing the invocation's children fails, the routing check falls back to the invoke id liveness read alone, which is the 0.26.0 rule.
  • The new shared test fixture is test/support/looping_subchart.ex. Its examples are library loan renewals.

Direction check of the record text

Every claim in the Amendment about code that predates this change was read on main at 46746e4, and each is cited by function and file. The code this change adds is cited by name only, with no SHA. The statifier facts (the effect's counters, generate_invoke_id/3, Position.to_binary/1, begin_macrostep/1) were read in the locked statifier 2.15.0. The reference embedder's id derivation was read in statifier_examples at 44e6f4a. git diff origin/main -- docs/adr/ shows zero removed lines.

Tests

  • driver_subchart_loop_test.exs (in-memory): two passes in order, then the parent goes on; a re-drive of the second pass adopts its child; a loop left mid-pass twice cancels the first pass's child and then a later pass's; a duplicate answer of an earlier pass changes nothing; a later pass's child that ended before the re-drive answers the pass that adopts it; the known limit, pinned twice: with another persisted step after the lost drive (the first pass's answer is discarded, and the stray's answer is later taken for that pass while a fresh child starts the next), and with none (the stray's one answer satisfies both passes, and the parent ends in renewed_all with both children completed).
  • driver_subchart_ecto_test.exs (Postgres, AdapterLock): two passes; a re-drive of the second pass; a single child's re-drive; the duplicate answer; the ended later-pass child. The two re-drive cases start from a stored state built by putting the parent's record back, because a lost drive cannot leave that state on this store.
  • ecto/loop_lost_drive_test.exs (Postgres, live): a drive of the second pass inside a transaction that rolls back leaves no child behind, and the first pass's answer delivered again is taken.
  • ecto/sqlite_migrations_test.exs (SQLite, linkage_columns: true): two passes; a re-drive of the second pass, driven by re-delivering the event the lost step processed.
  • execution_linkage_test.exs: the two new functions.

Each new test has a sabotage note above it. Every mutation in those notes was run and restored from a copy, and the restore was checked byte-equal.

Full mix quality was green locally on the committed tree, with the gate lock and a machine slot held from that run through the commit: 1,494 tests, 95.7% coverage, credo and dialyzer clean.

A durable subchart inside a loop started only its first child. Every
pass of the same <invoke> element asked for the one id
Linkage.child_execution_id/3 derives, so the second pass adopted the
first pass's finished child and the parent waited on it for good.

A single child is now read for before it is created. The first pass
keeps the old id. A child found there that has not ended is adopted,
as before; one that has ended belongs to an earlier pass, and this
pass creates its own under Linkage.child_execution_id/4, the old id
plus "@" and Linkage.invocation/1 of its invoke effect (the parent's
macrostep and round). Those counters are the same on a re-drive and
differ on any later pass, so a re-drive of a pass adopts its child.
The exit's cancel spares a later pass's child at or after its own
counters, so a re-drive of a loop's exit and entry does not cancel
the child it is about to adopt. Reading first also lets a re-drive on
Postgres adopt its child, which the refused insert used to abort.

Records the design as an ADR-0008 Amendment at proposed. A fan-out
keeps its ids. Tests prove two passes and a re-drive on the in-memory
adapter, on Postgres and on SQLite with the linkage columns on.

Refs: sp-5pfj
Every pass of a loop shares the invoke id, and the parent's door read
liveness by invoke id alone. So a duplicate answer of an earlier pass,
delivered after the next pass started, was taken as the next pass's
answer, and a later pass's child created by an interrupted drive could
answer the pass before it.

A single child's answer is now delivered only when that child is the
current pass's: the newest of the invocation's children the parent's
stored position could have started, read inside the parent's step. Any
other is discarded, as an answer to an invocation that is no longer
live is. A later pass's child that ended before its pass was re-driven
is adopted and answers that pass from its record.

The ADR-0008 Amendment states the routing decision and the cases
exactly, the first pass's crash window included, whose cost is that
the child's work runs twice.

Refs: sp-5pfj
The rule that a later pass's child counts only while its macrostep is
after the parent's stored one did not hold: macrostep numbers are
reused after a lost drive, so once another event was persisted a stray
child left by that drive counted, and the waited-on pass's genuine
answer was discarded.

A single child's answer now reaches the parent when the invoke id reads
live and the child is the newest of the invocation's children. That is
exact wherever a child's create commits with the parent's step, as on
Postgres under AdapterLock, where a lost drive leaves no child behind.
On a store where the two commit apart, a stray from a lost drive ranks
newest; ruled by the operator, 2026-11-22, that is a named known limit
of this release, and exact routing on every store needs the position
to record each live invocation's counters, a later change.

The ADR-0008 Amendment states the rule, where it is exact, the limit
and the cases. Tests pin the limit on the in-memory adapter and show
on Postgres that a lost drive leaves no child.

Refs: sp-5pfj
The named limit of newest-child routing left out one consequence. On
a store where a child's create and the parent's step commit apart,
when no other step of the parent comes between a lost drive and the
stray child's answer, that answer is taken for the waited-on pass, the
step it drives re-uses the lost macrostep and round, and the next pass
adopts the same stray and takes its answer again. One answer then
satisfies both passes.

The ADR-0008 Amendment, the changelog and two driver comments now say
so, and a test pins it on the in-memory adapter. No behaviour changes:
this is the known limit ruled by the operator, 2026-11-22.

Refs: sp-5pfj
@johnnyt
johnnyt merged commit 550e132 into main Nov 22, 2026
1 check passed
@johnnyt
johnnyt deleted the sp-5pfj-loop-children branch November 22, 2026 17:47
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