Read-Only Boot Coalescer V2 Implementation Plan

For agentic workers: REQUIRED SUB-SKILL: Use superpowers:executing-plans to implement this plan task-by-task.

Goal: Repair V1 so duplicate boot reads can fan out only when the leader result is current, complete, non-truncated, schema-valid, and no scheduler-visible Brain mutation would execute before the follower.

Architecture: Keep the V1 closed read-only command grammar and conservative pre-lease duplicate boundary. Add defense-in-depth result-envelope validation directly inside the follower UPDATE, and a correlated scheduler-order barrier that blocks any follower whose next execution would be preceded by a non-read-only Brain job. Preserve the existing resultSql-first integration path as the lease/expiry authority; add only bounded helper logic needed by the independent expiry test.

Tech Stack: TypeScript, Val Town std/sqlite, SQLite JSON1.

Spec: valblob:mosadek/project-brain-live-v1-durable/diagnostics/brain_bridge/readonly_coalesce/e72c_second_party_v2.json

Global Constraints

  • Candidate/nonauthority only; production project main and canonical brain.py remain untouched.
  • No direct Sprites.
  • No human_authority_approval.
  • Exact payload/timeout matching remains byte-conservative.
  • Only status, verify-chain, verify-claim-graph are coalescible.
  • Fanout is valid only after successful current resultSql lease/expiry validation.
  • No result fanout for null, malformed, open-schema, nonzero-returncode, or truncated envelopes.
  • Never move a follower read ahead of a scheduler-visible Brain mutation that would execute before it.
  • Preserve priority cap 1_000_000_000 and recover-stale recovery-first ordering semantics.

Task 1: Reproduce V1 falsifiers in the isolated sandbox

Files:

  • Existing: brain_bridge_readonly_coalesce_v1_candidate.ts
  • Existing: brain_bridge_readonly_coalesce_v1_test.ts
  • Existing: coalesce_e72c_adv_test.ts
  • Existing: coalesce_e72c_boundary_test.ts
  • Existing: coalesce_e72c_order_test.ts

Interfaces:

  • Consumes: reviewed V1 candidate and independent tests.

  • Produces: fresh RED evidence for expiry, truncated/null result, and mutation ordering defects.

  • Run original 14-assertion source suite and confirm PASS.

  • Run independent adversarial suite and confirm the reviewed failures.

  • Run boundary suite and confirm truncated/null failures.

  • Run ordering suite and confirm intervening mutation violation.

Task 2: Add V2 failing regressions before implementation

Files:

  • Create: brain_bridge_readonly_coalesce_v2_test.ts
  • Create: coalesce_v2_regression_test.ts

Interfaces:

  • Consumes: wished-for V2 exports matching V1 names.

  • Produces: RED tests for closed result envelopes, priority/recover-stale ordering, stale replay, and read-only non-barriers.

  • Copy the 14 logical source assertions to V2, using a complete valid result envelope.

  • Add failing tests for extra/missing result keys, nonzero returncode, both truncation flags, and null/malformed envelopes.

  • Add failing tests for same-priority mutation, higher-priority later mutation, recover-stale rank-0 mutation, same-timestamp ID ordering, and lower-priority mutation that should not block.

  • Add stale resultSql replay test proving a second result update cannot fan out.

  • Run V2 tests and verify RED because the V2 module is absent.

Task 3: Implement the minimal V2 repair

Files:

  • Create: brain_bridge_readonly_coalesce_v2_candidate.ts

Interfaces:

  • Produces: readonlyBrainCommand, coalesceFollowersSql, mayCoalesceSuccessfulLeader, and result-envelope validation helpers.

  • Port V1 read-only recognition unchanged.

  • Add exact closed result-envelope validator.

  • Make follower UPDATE validate ?1 result bytes before changing any follower.

  • Add correlated NOT EXISTS mutation barrier using recovery-first + capped-priority + created_at,id scheduler ordering.

  • Add optional lease-expiry rejection when lease_expires_at is present.

  • Run V2 source/regression tests until GREEN.

Task 4: Re-run independent suites against V2

Files:

  • Create V2-import copies of the three e72c suites; preserve scenarios/assertions.

  • Run adversarial suite; require all checks PASS.

  • Run boundary suite; require all checks PASS.

  • Run ordering suite; require PASS.

  • Re-run original V1 14/14 as baseline and V2 14/14 parity.

Task 5: Preserve and commit evidence

Files:

  • Preserve exact candidate and test bytes/hashes in project-scoped Val Town blobs.

  • Preserve one JSON receipt with commands/results/hashes/classification.

  • Compute SHA-256 for candidate/tests.

  • Store candidate/tests and receipt under evidence/readonly_coalesce_v2/a19c/.

  • Verify blob readback hashes.

  • Reconcile current Brain generation.

  • Commit task result through Brain with evidence URIs/hashes.

  • Claim next causally ready task.