Implementation:1ay1 Agentty Capped File Read
| 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
- Repository: 1ay1_Agentty
- File: include/agentty/util/capped_read.hpp
- Lines: 1-162
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