Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:1ay1 Agentty Capped File Read

From Leeroopedia
Knowledge Sources
Domains IO, Type_System
Last Updated 2026-10-28 12:00 GMT

Overview

Size-limited file read returning a typed variant of four outcomes (Content, Absent, Unreadable, TooBig), so ignoring "too big" does not compile.

Description

The agentty::util::capped_read module reads a user file with a size limit in a shape where the four outcomes are four TYPES. Four files independently wrote the same helper returning an empty string for absent, unreadable, AND too big — callers wrote `if (raw.empty()) continue;` which silently skipped oversized files with no warning. Skills vanished, hooks were inert, AGENTS.md was cut with no marker.

A variant makes the outcomes alternatives: there is no `.text` to read until you have said which case you are in, and `visit` over a lambda set that omits one alternative is a compile error. `TooBig` is NOT an error — the file is fine, agentty's limit is the constraint — and each caller answers it differently (keep a stub, exit 1, truncate with a marker). `std::expected` would funnel all three into one `if (!r) return;`, which is exactly the collapse that caused the bug.

Usage

Import this when you need to: read a user file with a size cap, handling each outcome explicitly (Content, Absent, Unreadable, TooBig).

Code Reference

Source Location

Signature

namespace agentty::util {

struct Content { std::string text; };      // bytes, within the cap
struct Absent {};                           // no such file (or a directory)
struct Unreadable { std::string why; };      // present, but the read failed
struct TooBig { std::size_t size, cap; };    // present, readable, over the limit

using CappedRead = std::variant<Content, Absent, Unreadable, TooBig>;

[[nodiscard]] CappedRead read_capped(const std::filesystem::path& p,
                                      std::size_t cap_bytes);
}

Import

#include "agentty/util/capped_read.hpp"

I/O Contract

Inputs

Name Type Required Description
p std::filesystem::path Yes File to read
cap_bytes std::size_t Yes Maximum bytes; over → TooBig

Outputs

Name Type Description
CappedRead std::variant Absent | Unreadable | TooBig — must handle all four

Usage Examples

Read with All Outcomes Handled

#include "agentty/util/capped_read.hpp"

auto result = agentty::util::read_capped("SKILL.md", 64 * 1024);
std::visit(overloaded{
    [](agentty::util::Content& c) {
        // Use c.text — the file's bytes, within the cap
    },
    [](agentty::util::Absent) {
        // No such file — skip silently
    },
    [](agentty::util::Unreadable& u) {
        // Read failed — log u.why
    },
    [](agentty::util::TooBig& t) {
        // Over the limit — MUST handle, can't silently skip
        // t.size, t.cap
    }
}, result);
// Forgetting TooBig is a COMPILE ERROR — the variant visit is non-exhaustive

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment