Skip to content

spike(db): run spike 0002 with a worker-pg cell and an instance-level worker-do SQLite cell #37

Closed

Description

@aRustyDev

Context

Spike 0002 (.agents/plans/contrail/spikes/0002-database-engine.md) has not started. Round 27 narrowed it to Postgres only, with a timebox of about 3.5 days. D1 became a desk review, and so did DO-SQLite, which the spike frames as one object per workspace (spike file lines 5, 12-14; ADR-0040:113; Q-0040.1, QUESTIONS.md:73). The Postgres side is live: CNPG thrust-pg and thrust-pg-dev, reached through a Workers VPC service and Hyperdrive (ADR-0094; research/0010 §11-12; bead plane-byd.4), and spike 0001 Stage 2 ran on it (research/0004 §10). The contract suite is HTTP-only and target-agnostic, with four targets (scripts/contract.sh:5: native-memory | native-pg | worker-memory | worker-cf) and a bench pass (crates/thrust-contract-tests/src/bin/bench.rs). The Worker already uses Durable Object SQLite (crates/thrust-worker/src/durable.rs:34,57, workers-rs 0.8.7). ADR-0040 is proposed.

Problem

FR-04: since round 27 the Postgres path has picked up four costs.

  1. The Hyperdrive statement-flow constraint is documented behaviour, so every statement stays typed and uncached (crates/thrust-adapters-postgres/src/lib.rs:53-64; research/0004 §10.4 F1), and advisory locks are unsupported.
  2. TLS is not authenticated (cert_verification_mode = disabled, ADR-0094:111,120; Q-0094.9; plane-byd.9).
  3. Availability now includes woven-o11y (research/0010 §6 risks 1 and 7; Q-0094.4).
  4. The first database-backed request on a fresh isolate takes 595 to 615 ms (research/0004:387, F7), against p95 < 500 ms.

The round-11 rejection of DO-SQLite (ADR-0040:67,97) assumed one object per workspace. An instance-level object, which fits single-org (ADR-0003), was never weighed. FR-16: step 6 (the Rust data layer) is already decided by FR-07, and the Stage 2b items have no home (Q-0006.6, QUESTIONS.md:359). FR-22: the counter upsert is the first statement of the create transaction (crates/thrust-adapters-postgres/src/lib.rs:399-414), so its row lock is held for the whole transaction. Create p95 is 559 ms at c=8 against 296 ms at c=1 (research/0004:383).

Proposal

Rewrite spike 0002 as handoff item A3.1 specifies (ADR-0040 round-29 note):

  • Two cells, both driven by the existing contract suite and bench:
    • worker-pg: the slice on CNPG through Hyperdrive, i.e. today's worker-cf target on the real platform (wrangler.stage2.toml). The sequence number is allocated in its own short transaction, or last with UPDATE ... RETURNING (FR-22).
    • worker-do: one instance-level AppDo holding SQLite. The fetch Worker forwards to it, and the axum router and use-cases run inside it over a SQLite adapter. The native twin runs the same SQL on rusqlite. Known gap: sql.exec() rejects BEGIN and SAVEPOINT, and workers-rs 0.8.7 exposes Storage::transaction (an async 'static closure, src/durable.rs:635) but not transactionSync. The cell has to show how ADR-0045's explicit Tx maps onto that.
  • Keep steps 2, 3, 4, 7 and 9, and drop step 6 (FR-07). Add the Stage 2b items from Q-0006.6: Hyperdrive caching on versus off, using a separate config (research/0010:301); a bench from a distant region; a 24-hour cost reconciliation; one real Email send.
  • Exit criteria, per cell:
    • no duplicate sequence_id in 10k creates from at least 50 parallel writers (R-F2);
    • FTS plus trigram (pg_trgm, or the FTS5 trigram tokenizer on the DO);
    • list p95 < 300 ms from the origin's region;
    • a migration story;
    • a projected cost.
  • Decision rule: Postgres stays the default if the DO cell fails any criterion. If both cells pass, the owner decides (Q-0040.6, Track C of the handoff). Timebox: 3.5 + 3 days.

Acceptance criteria

  • spikes/0002-database-engine.md is rewritten with both cells, exit criteria per cell and the decision rule; the status row in spikes/README.md is updated.
  • scripts/contract.sh accepts worker-do and a native SQLite twin, and the main pass is green on each. The /_info vacuity guard (scripts/contract.sh:160-180) shows each run used the adapter set it claims.
  • The 50-writer, 10k-create test passes on both cells and fails against the mutant_dup_sequence build (scripts/mutants.sh:31), which is the control.
  • bench create p95 at c=1 and c=8 is recorded for worker-pg with the FR-22 allocation order, next to 296 and 559 ms (research/0004 §10.3).
  • A new research note gives FTS parity, list p95, the migration story and cost for each cell. ADR-0040 is decided, or stays proposed with the reason, and Q-0040.1 and Q-0006.6 are answered.
  • Every resource the spike created is listed and torn down, as in research/0004 §10.7.

References

  • Review: .agents/plans/contrail/reviews/2026-10-19-fable-architecture-review.md (FR-04, FR-16, FR-22)
  • Handoff: .agents/plans/contrail/reviews/2026-10-19-fable-handoff-PROMPT.md item A3.1
  • .agents/plans/contrail/spikes/0002-database-engine.md; ADR-0040 (adrs/data/base/0040-system-of-record-database.md:67,97,113), ADR-0041, ADR-0043, ADR-0044, ADR-0045, ADR-0094:111,120
  • Q-0040.1 (QUESTIONS.md:73), Q-0006.6 (QUESTIONS.md:359), Q-0094.4, Q-0094.9, Q-0040.6 (proposed in the handoff, Track C); SPEC R-F2 (SPEC.md:51)
  • research/0004 §10.3 and §10.4 (F1, F7); research/0010 §6 and §11
  • crates/thrust-worker/src/durable.rs:34,57; crates/thrust-adapters-postgres/src/lib.rs:399-414; crates/thrust-contract-tests/tests/contract.rs:196 (today's 20-writer test)
  • Beads: plane-byd.4

Blocked by: #35

Finding: FR-04, FR-16

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:infraprio:p0review:fable-2026-10-19type:spike

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions