Skip to content

Stream installation exports instead of assembling the archive in memory #47

Closed

Description

@TusanHomichi

Problem or outcome

record_export::export_at (#46) reads every finalized version of the scope with fetch_all and then assembles the ZIP archive in a Vec<u8> before the HTTP handler returns it as one body. For the installation scope this holds the corpus in memory while the export is produced. #46 already releases each version's bytes as they are written (rows are consumed by build_archive), so the peak is roughly one copy of the exported bytes plus one row rather than two copies, but a very large finalized history still costs memory in proportion, and the process can be pressured before the download completes.

The outcome is an installation export whose memory use is bounded by a few units, not by the history: rows are read incrementally and the archive is written to the response as it is produced, with the same documented bytes.

Scope

In scope:

  • Stream the query (a cursor over evaluation_version in archive order) into a ZIP writer that writes to the response body as it goes, for every scope or for the installation scope alone.
  • Keep the format byte-identical for a given scope and export instant (docs/formats/record-export.md): stored entries, fixed entry order and metadata, canonical manifests. Note that a non-seekable writer emits data descriptors, which the format document must either permit explicitly or the implementation must avoid (for example by computing sizes and CRCs first, since stored entries have known lengths).
  • Decide how a failure part-way through a streamed body is surfaced to the operator (a truncated ZIP does not verify, which is the right failure, but the interface should say what happened).
  • Update ADR 0014's cost note once exports stream.

Out of scope:

Acceptance criteria

  • An installation export with many finalized versions completes with memory bounded by a few units, measured and stated in the PR.
  • The streamed archive is byte-identical to the buffered one for the same scope and instant, proven by a test that produces both.
  • export verify accepts the streamed archive, and the format document says exactly what changed in the container, if anything.
  • A mid-stream failure is visible to the operator and never presented as a complete export.

Expected proof

  • cargo test --workspace with a test producing a large invented corpus and comparing streamed and buffered bytes.
  • The usual gates: cargo fmt --check, cargo clippy --workspace --all-targets -- -D warnings, web/ check and build if the interface changes.

Related roadmap, decisions, or issues

Activity

  1. TusanHomichi commented on Oct 7, 2026

    @TusanHomichi
    MemberAuthor

    Review round 3 on PR #69 — plan (2026-10-07)

    Scope and source. Finish the open review round on #69 so the maintainer can take a merge decision. Authorized by the owner's 2026-10-07 request ("finish PR #69 review round"), under AGENTS.md / CONTRIBUTING.md / docs/workflow.md at 03cc8c7. In scope: origin/main merged into codex/47-stream-installation-exports (done, c631b6b, clean), the six open review threads on #69, the Rust files the PR already touches, ADR 0014 and docs/formats/record-export.md, their tests, the full gate sequence, a push, and thread replies. Out of scope: merge, deployment, new issues' work, the unrelated untracked scratch files in the main checkout. No owner effort budget was set; the no-default-cap policy (#73) applies.

    Triage of the open threads, against the code at 4755731:

    Thread Finding State
    record_export.rs (outdated) audit connection acquired while holding the read connection fixed at 4755731: audit is its own committed write_tx, then one reader; 1- and 2-connection pool tests
    exports_http.rs:291 failure marker lost when the queue is full fixed at 4755731: Completion flag; closure without success fails the body; transport test outlasts both windows
    record_export.rs:249 read snapshot lives as long as the download → WAL pinned real — fix A
    exports_http.rs:222 abandoned request still audits and scans metadata real — fix B
    exports_http.rs:456 queue bounded by items, not bytes real — fix C
    hold review at c1eae2f (own account) the two items above it repaired at 4755731; the hold itself never cleared — closed in this round

    Fixes.

    • A. Finalized versions are immutable and undeletable by trigger (evaluation_version_no_update / _no_delete, migration 0010), so the stored fingerprints — not one long transaction — are what tie the manifest to the payloads. The metadata pass becomes one short read transaction committed before the response starts; each unit is then a point read released before its bytes go to the client, cross-checked against the manifest's content_hash/chain_hash/record_schema (a mismatch or missing row ends the transfer incomplete). No connection is held while blocked on the client. This changes the "one snapshot for manifest and payload" wording of the earlier hold review; ADR 0014 says so.
    • B. export_to takes a cancellation probe checked before the audit write, before and during the metadata pass, and before the response starts; the handler's probe is the body channel's is_closed(), true the moment the handler returns or is dropped. Cancellation surfaces as a client departure, not a production failure.
    • C. ChunkSink::write hands on at most 64 KiB per item; the queue holds ≤ 8 × 64 KiB.

    Acceptance. Each fix has a test that fails against the code it replaces (A: wal_checkpoint(TRUNCATE) reports busy=0 while an export is blocked mid-stream; B: no record_exported row and no bytes when cancelled before start, row stands and zero bytes when cancelled after the audit; C: write_all(1 MiB) into an unread queue stalls after exactly 512 KiB). Byte identity and every existing stream test still pass. Both Rust modules stay under 1,000 lines or the ownership boundary is named first. Full gates from CONTRIBUTING.md on the frozen candidate, including npm run e2e (the PR touches web/src/lib/api.ts). Hosted pr-gate observed after the push. Threads 1–2 answered and resolved; 3–5 answered with the fix and its negative control.

    Routes. Primary integration, selection and review: this session with its plan-review advisor. Rust implementation from the fixed spec: the implementer role, owning every .rs file. ADR 0014 / format doc: the docs role, owning those two files only. Verification, falsification receipts, gates, push, and GitHub writes: the primary. Roles are recorded, not model identifiers; the host does not expose the serving model and this project's guidance names none.

    Next action. Implement A–C with their tests in the #47 worktree, then falsify each against the stashed src/ and run the gates.


    Generated by Claude Code

  2. TusanHomichi commented on Oct 7, 2026

    @TusanHomichi
    MemberAuthor

    Round 3 receipt — PR #69 at 732c566 (via 151fcd6, d4e60f7, 88adb09, 9bb0cc2, 096530e, 2870c93)

    Pushed 151fcd6c78bbdd798fc7748a90a3f11d5fe3caa8 (tree 4c8d596a) to codex/47-stream-installation-exports; the branch carries origin/main 03cc8c7 via merge c631b6b. Round summary, thread replies, and the design-change note are on the PR. All five review threads are answered and resolved.

    Acceptance against the plan above:

    • A/B/C each have a test that fails against the behaviour it replaces — receipts: A busy = 1, log = 385, checkpointed = 384 with the payload pass inside a transaction (gated on the first write beyond three payloads' worth of bytes; d4e60f7 replaced a write-count gate whose comment misplaced it); B audit count 1 vs 0 and 348,478 bytes vs 0 with the probe forced false; C write_all(1 MiB) succeeds with the whole-entry write. A first A control that pinned the reader after the gate point was invalid and is preserved as such.
    • Byte identity and every prior stream test pass; record_export.rs 911 lines, exports_http.rs 790.
    • Full local gates on the frozen candidate (2026-10-07 18:55–19:06 UTC, worktree /data/dev/src/consolebook-worktrees/47-stream-installation-exports): web check 289 files 0/0 and build on the unchanged web/ tree; cargo fmt --check clean; clippy -D warnings clean; cargo test --workspace 27 binaries, 197 passed, 0 failed, 1 ignored (206 s); cargo build -p consolebook-server; npm run e2e 56 passed (7.5 min, system Google Chrome). Gate logs (six candidates), falsification receipts, posted texts and their SHA-256s are archived at /data/dev/home/.codex/tmp/consolebook-pr69-round3-20261007/round3-evidence-v2.tar.gz (SHA-256 8dbf695330c383990f6211a902f62f326859eb00a84274053e7a27a4652320f6; the earlier round3-evidence.tar.gz is kept), reopened and read back before this edit; exercised binaries dcf0d9ae37daa527… (151fcd6/d4e60f7), b60aaa21f24d4319… (88adb09), b65b771e9fdfca88… (096530e).
    • Preserved failures: candidate d02a8e0 (lease per point read) failed the memory proof at 23,484 KiB peak for the 4,000-unit corpus, four runs; the same code measured 15.0–17.7 MiB later, so the peak varies with how many pooled connections the reads land on (one SQLite page cache each). 151fcd6 held one connection for the pass (steady 14,808 KiB) and was correctly flagged by Codex as a lease starvation vector; 88adb09 returns to a lease per read, bounds the proof against the pool's page-cache ceiling read at run time (10,000 KiB), and adds a starvation test that fails against the held lease (acquire timeout). ADR 0014 names the term.
    • Hosted pr-gate: the 151fcd6 run was cancelled by the next push and the d4e60f7 run superseded; on 88adb09 it is green — run 37674688625, observed 2026-10-07 ~19:35 UTC. Codex's re-review of 88adb09 raised one further P1 (concurrent export producers unbounded); 9bb0cc2 + 096530e bound admission at four per server (AppState-owned semaphore; a first process-wide static cut failed the full suite by sharing slots across parallel test servers — preserved as gates4/gates5) with a typed 503 export_busy refusal and a live-socket test that is served 200 without the bound. Full local gates on 096530e: fmt, clippy, cargo test --workspace 27 binaries / 199 passed / 0 failed / 1 ignored (157 s), build, npm run e2e 56 passed (5.4 min, system Chrome); hosted pr-gate on 096530e (run 37678442926): web/format/clippy/Test green, then a 25-minute hang in Install browser (apt fetch) to the 30-minute job timeout before any browser test ran — runner infrastructure, not this diff; 2870c93 (test-only: the memory proof now skips, not fails, where no process peak is reported — Codex P2) is the one re-run and is green — run 37682401715 (observed 2026-10-07 ~20:45 UTC). 732c566 corrects ADR 0014's abandonment wording (Codex P2, docs only; identical binary); all nine review threads are resolved; pr-gate on 732c566 is green — run 37684726494 (observed 2026-10-07 ~20:58 UTC).

    Routes: as planned — this session plus its plan-review advisor for selection, review and integration; one implementer-role subagent for the Rust change; one docs-role subagent for the two docs. The memory-proof repair, the lease, starvation and admission fixes, the falsification runs, the gates and the GitHub writes were the primary's. Roles, not model identifiers, are recorded.

    Next action: none for this round — hosted gates are green on the head, every thread is resolved, and the merge decision is the maintainer's.


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions