sinelaw/fresh · error

missing count in count_lf response

Error message

missing count in count_lf response

What it means

count_line_feeds_in_range sent a count_lf request to the remote agent and the response JSON did not contain a usable unsigned integer under the "count" key. The adapter requires the exact protocol field, so a malformed or missing count is surfaced as InvalidData rather than silently returning 0.

Solutions

  1. Check the remote agent version matches the editor's protocol; upgrade or downgrade so count_lf returns {"count": <u64>}.
  2. Log the raw count_lf response to see what the agent actually returned (error vs. malformed result).
  3. Handle the InvalidData error by falling back to computing the line count from read_range data.

Example fix

// before
let lf = remote_fs.count_line_feeds_in_range(&path, start, end)?;
// after
let lf = remote_fs.count_line_feeds_in_range(&path, start, end)
    .unwrap_or_else(|e| {
        eprintln!("count_lf failed ({e}); computing locally");
        count_lf_from_bytes(&remote_fs.read_range(&path, start, end).unwrap())
    });
Defensive patterns

Strategy: type-guard

Validate before calling

let resp: serde_json::Value = send_count_lf(...)?;
if resp.get("count").and_then(|v| v.as_u64()).is_none() {
    return Err("agent count_lf response malformed".into());
}

Type guard

fn valid_count_response(v: &serde_json::Value) -> bool { v.get("count").and_then(|c| c.as_u64()).is_some() }

Try / catch

let lf = remote_fs.count_line_feeds_in_range(&path, start, end)
    .unwrap_or_else(|e| { eprintln!("count_lf failed: {e}"); fallback_count_lf(&path, start, end) });

Prevention

When it happens

Trigger: A count_lf response from the remote agent lacking the "count" field, containing null, a non-integer value, or an error payload where a result was expected.

Common situations: Remote agent/protocol version mismatch where the field was renamed or the method unimplemented, an error JSON returned in place of the result, or a custom/proxy agent responding with a different schema.

Related errors


AI-assisted analysis of sinelaw/fresh@67894ca546 (2026-09-13). Data as JSON: /api/errors/4a778833fa6c40c3. Report an issue: GitHub.

Appendix: source

Thrown at crates/fresh-editor/src/services/remote/filesystem.rs:305

            ));
        }

        Ok(content)
    }

    fn count_line_feeds_in_range(&self, path: &Path, offset: u64, len: usize) -> io::Result<usize> {
        let path_str = path.to_string_lossy();
        let result = self
            .channel
            .request_blocking("count_lf", count_lf_params(&path_str, offset, len))
            .map_err(Self::to_io_error)?;

        result
            .get("count")
            .and_then(|v| v.as_u64())
            .map(|c| c as usize)
            .ok_or_else(|| {
                io::Error::new(
                    io::ErrorKind::InvalidData,
                    "missing count in count_lf response",
                )
            })
    }

    fn write_file(&self, path: &Path, data: &[u8]) -> io::Result<()> {
        let path_str = path.to_string_lossy();
        self.channel
            .request_blocking("write", write_params(&path_str, data))
            .map_err(Self::to_io_error)?;
        Ok(())
    }

    fn create_file(&self, path: &Path) -> io::Result<Box<dyn FileWriter>> {
        // Create an empty file first
        self.write_file(path, &[])?;
        Ok(Box::new(RemoteFileWriter::new(

View on GitHub (pinned to 67894ca546)