rtk-ai/rtk · error · anyhow::Error

negative record length

Error message

negative record length: {}

What it means

While walking the binlog stream, records of type RECORD_NAME_VALUE_LIST or RECORD_PROJECT_IMPORT_ARCHIVE are prefixed with a 7-bit-encoded signed length. A negative length means the stream is corrupt or being misparsed — a real record cannot have negative payload size. The parser bails instead of passing a negative value to skip(), which would panic or misbehave when cast to usize.

Solutions

  1. Regenerate the binlog with a current .NET SDK — the file is almost certainly corrupt or truncated.
  2. Confirm the input is actually a binlog (gzip magic + version header >= 18) and not an arbitrary gzip/text file.
  3. Retry the producing build; if it was interrupted (disk full, process killed), a partial stream explains the corruption.
  4. If it recurs on valid files, extend the parser's record-type handling so offsets stay aligned with the actual stream layout.
Defensive patterns

Strategy: try-catch

Validate before calling

// sanity check: file should be valid gzip and non-trivially large
let bytes = std::fs::read(path)?;
if bytes.len() < 32 || &bytes[..2] != b"\x1f\x8b" {
    bail!("not a plausible binlog (missing gzip header)");
}

Try / catch

match result {
    Err(e) if e.to_string().contains("negative record length") => {
        warn!("binlog corrupt; regenerating");
        std::fs::remove_file(&binlog_path).ok();
        rebuild_and_reparse()?;
    }
    other => other?,
}

Prevention

When it happens

Trigger: parse_events_from_binlog's record loop decodes read_7bit_i32() for an auxiliary record and gets len < 0, typically because the read offset has drifted (corrupt file, unsupported record variant parsed with the wrong layout) or the 7-bit encoding decodes a large value that wraps negative.

Common situations: Hand-edited or partially written binlog; a binlog produced by an MSBuild version emitting record types this parser misclassifies; feeding a non-binlog gzip file (or random bytes) into the parser.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of rtk-ai/rtk@0924356b4c (2026-09-19). Data as JSON: /api/errors/65faa332af893095. Report an issue: GitHub.

Appendix: source

Thrown at src/cmds/dotnet/binlog.rs:353

            .read_7bit_i32()
            .context("failed to read record kind")?;
        if kind == RECORD_END_OF_FILE {
            break;
        }

        match kind {
            RECORD_STRING => {
                let text = reader
                    .read_dotnet_string()
                    .context("failed to read string record")?;
                parsed.string_records.push(text);
            }
            RECORD_NAME_VALUE_LIST | RECORD_PROJECT_IMPORT_ARCHIVE => {
                let len = reader
                    .read_7bit_i32()
                    .context("failed to read record length")?;
                if len < 0 {
                    anyhow::bail!("negative record length: {}", len);
                }
                reader
                    .skip(len as usize)
                    .context("failed to skip auxiliary record payload")?;
            }
            _ => {
                let len = reader
                    .read_7bit_i32()
                    .context("failed to read event length")?;
                if len < 0 {
                    anyhow::bail!("negative event length: {}", len);
                }

                let payload = reader
                    .read_exact(len as usize)
                    .context("failed to read event payload")?;
                let mut event_reader = BinReader::new(payload);
                let _ =

View on GitHub (pinned to 0924356b4c)