← Back to Pull requests

feat(dotc1z): record decompressed db size on save (zstd FCS + Info log) #777

Closed
unknown closed this pull request without merging on Apr 22, 2026 18:56 UTC
View on GitHub
unknown unknown commented on Apr 21, 2026 18:47 UTC
Author

Summary

Two changes, one feature:

1. Producer side — saveC1z now writes the decompressed db size into the zstd frame header (Frame_Content_Size / FCS) and logs it at Info alongside the resulting compressed size. Upstream-spec-compatible — every existing zstd decoder ignores FCS and continues to stream exactly as today.
2. Consumer side — the dotc1z decoder peeks the frame header during init and exposes DeclaredDecodedSize() (uint64, bool). When the declared size already exceeds DecoderMaxDecodedSize, the first Read returns ErrMaxSizeExceeded with the exact declared size embedded in the message — no bytes decompressed, no decoder warmed up.

Why

The hit today isn't at sync time — it's during grant expansion. Expansion materializes one grant per (member, inherited access) tuple from the annotations the connector emits, and on some tenants that's enough to push the c1z past the 3 GiB decoder cap (observed: >200 GB decompressed). When downstream reopens the post-expansion c1z, it trips ErrMaxSizeExceeded with no signal about how much more data is in the stream. The only way to find the true size today is to download the c1z and re-decompress with a raised cap — operationally expensive, often not possible during an incident.

saveC1z runs once per C1File.Close(), so the final post-expansion save records the final post-expansion size. With FCS now in the header, any reader can learn that size directly — either via the 14-byte frame header (no dependency on this SDK) or via DeclaredDecodedSize() (this SDK). Fail-fast on cap means the next bad c1z reports its declared size in the error instead of a generic "cap exceeded".

What changed

Producer (pkg/dotc1z/file.go)


  • Stat the db file before encoding so we know its exact size.

  • Initialize the zstd encoder, then call ResetContentSize(outFile, dbSize) so the frame header carries FCS. ResetContentSize requires the written byte count to equal the advertised size at Close(), so the io.Copy is replaced with io.CopyN(..., dbSize) to make that invariant explicit.

  • After successful rename, log c1z: saved at Info with decompressed_bytes + compressed_bytes for operational visibility.
  • Consumer (pkg/dotc1z/decoder.go)


  • decoder grows declaredSize / hasDeclaredSize fields populated lazily during init.

  • Init logic extracted into ensureInit(), called from both Read and the new DeclaredDecodedSize().

  • After ReadHeader strips the 5-byte C1ZF magic, wrap the post-magic reader in bufio.Reader so we can Peek(zstd.HeaderMaxSize) the zstd frame header without consuming those bytes — the zstd decoder reads them back from bufio's buffer when it starts.

  • If FCS is advertised and declaredSize > maxDecodedSize, init stores an ErrMaxSizeExceeded-wrapped error with "declared decoded size N exceeds cap M". The pooled / fresh zstd decoder is never even constructed in this case.

  • Legacy c1zs (saved before this PR) don't advertise FCS — DeclaredDecodedSize returns (0, false), and Read behaves exactly as before.
  • Tests

    Producer:

  • TestSaveC1z_SetsFrameContentSize — decodes the frame header of a freshly-saved c1z, asserts header.HasFCS and header.FrameContentSize == dbSize.

  • All existing TestSaveC1z / TestLoadC1z subtests pass (including the empty-db edge case).
  • Consumer:

  • TestDecoder_DeclaredDecodedSize — DeclaredDecodedSize returns exact FCS before any Read; Read then streams the full payload; second call is idempotent.

  • TestDecoder_DeclaredDecodedSize_LegacyWithoutFCS — hand-built legacy c1z (C1ZF magic + zstd frame without ResetContentSize) returns (0, false) and still decodes cleanly.

  • TestDecoder_DeclaredSizeExceedsCap_FailsFast — cap set below declared size; first Read returns ErrMaxSizeExceeded with "declared decoded size" in the message; zero bytes produced.

  • TestC1ZDecoder_WindowSizeExceeded (16 MiB random payload) — moved here from TestC1ZDecoder, since FCS-aware SingleSegment frames make small-payload window-exceeded checks impossible. Preserves coverage of ErrWindowSizeExceeded.
  • Compat

  • No c1z format change — the 5-byte C1ZF\x00 magic and zstd stream layout are unchanged; FCS is an optional field inside the zstd frame header.

  • Older decoders (baton-sdk ≤ v0.8.25) ignore FCS — no behavioral change for any existing reader.

  • Callers that rely on errors.Is(err, ErrMaxSizeExceeded) keep working. The message now carries the declared size when known.
  • Follow-ups (separate PRs, not in scope)

  • Consumer-side (c1 repo): surface DeclaredDecodedSize() in the ErrMaxSizeExceeded log line at the fetch site so per-tenant c1z-growth is queryable; add an OTel metric (e.g. c1z.size.decompressed_bytes histogram) keyed on tenant.

  • Optional: expose an Inspect helper on dotc1z that returns FCS without constructing a decoder, so ops tooling can parse the 14-byte header without any zstd setup.
  • 🤖 Generated with Claude Code

    Diff: 628 lines (+471 / -157) across 7 files Reviewer: arreyder Review duration: 1449m
    All Vouch review confidence checks passed
    Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 628 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.386
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #777"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50.3% (MEDIUM) · Higher = larger blast radius
    50.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19.9% · Safe to merge
    19.9%