refactor!: files only by handle: uploads in, downloads out (D-46)
check / check (push) Canceled after 0s
check / check (push) Canceled after 0s
The owner (2026-10-23, via the question tool): "Uploads only"; and before it: "What we would DEFINITELY want to avoid is passing a bunch of BLOBs through AI context windows. That's just a pure waste." No file passes through an agent's context, in either direction. In: the inline arguments are gone with the 256 KiB limit (INLINE_MAX, take_file's inline branch): content_base64 and its sha256 on books.attach_receipt and books.file_document, w9_content_base64 and w9_sha256 on books.set_w9, csv_text on the two CSV imports. A file comes only by books.begin_upload and `wanderbooks upload`, named by its upload_id; existing_attachment_id still names bytes stored already. The five filing tools deny unknown fields, and any inline argument to any tool is refused before it runs as an unknown argument pointing to books.begin_upload (access log: refused inline_file). Out: books.export (every format), books.asset_labels and books.get_receipt view bytes hand the file over (HandedFile: download command and expiry, D-29), as the CPA package and the W-9 do. Resource reads stay text. A token keeps up to 64 handed files (was 16). The access log's digest no longer exempts file bytes. Docs: handbook files-in-out and parties-and-tins, mcp-interface.md, rules.md, receipt-store.md, parties-and-attachments.md, plan/served.md (D-46; the inline decisions marked superseded), STATUS.md, testing.md (D-46: tests migrated to uploads and downloads, rewritten, removed, added). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01T3gXMVt7XJPi6ens1or9Dj
This commit is contained in:
38 files changed
+1702
-1172
No files matched your search
@@ -301,13 +301,19 @@ struct Agent {
|
||||
next: u64,
|
||||
/// Notifications received.
|
||||
notes: Vec<Value>,
|
||||
/// How it runs the commands it is given (`wanderbooks upload`), as
|
||||
/// [`Home::cmd`] runs them, and the folder its files are in: the
|
||||
/// program, the data root, the home.
|
||||
commands: Option<(PathBuf, PathBuf, PathBuf)>,
|
||||
}
|
||||
|
||||
impl Agent {
|
||||
fn start(home: &Home, book: &str) -> Self {
|
||||
let mut c = home.cmd("mcp");
|
||||
c.args(["--book", book]);
|
||||
Self::from(c)
|
||||
let mut a = Self::from(c);
|
||||
a.commands = Some((home.exe.clone(), home.root(), home.dir.path().to_path_buf()));
|
||||
a
|
||||
}
|
||||
|
||||
fn from(c: Command) -> Self {
|
||||
@@ -315,9 +321,50 @@ impl Agent {
|
||||
run: Running::start(c),
|
||||
next: 1,
|
||||
notes: Vec::new(),
|
||||
commands: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Send `bytes` as `name` as an agent does (D-46): books.begin_upload,
|
||||
/// the file written in the home, and the command it returns run on
|
||||
/// it. The upload_id.
|
||||
fn upload(&mut self, bytes: &[u8], name: &str) -> Result<String, String> {
|
||||
let (exe, root, home) = self.commands.clone().expect("an agent Agent::start made");
|
||||
let begun = self.call(
|
||||
"books.begin_upload",
|
||||
json!({"sha256": ledger_store::journal::sha256_hex(bytes), "size": bytes.len(),
|
||||
"mime_type": "text/plain", "file_name": name, "purpose": "receipt"}),
|
||||
)?;
|
||||
let command = begun["command"]
|
||||
.as_str()
|
||||
.ok_or_else(|| format!("no upload begun: {begun}"))?;
|
||||
let words: Vec<&str> = command
|
||||
.strip_suffix(" <the file's path>")
|
||||
.ok_or_else(|| command.to_string())?
|
||||
.split(' ')
|
||||
.collect();
|
||||
let path = home.join(format!("upload-{}", begun["upload_id"].as_str().unwrap()));
|
||||
std::fs::write(&path, bytes).unwrap();
|
||||
let out = Command::new(exe)
|
||||
.arg(words[1])
|
||||
.arg("--root")
|
||||
.arg(root)
|
||||
.args(["--test-knobs", KNOBS])
|
||||
.args(&words[2..])
|
||||
.arg(&path)
|
||||
.env_clear()
|
||||
.env("PATH", "/usr/bin:/bin")
|
||||
.env("HOME", &home)
|
||||
.stdin(Stdio::null())
|
||||
.output()
|
||||
.unwrap();
|
||||
let _ = std::fs::remove_file(&path);
|
||||
if !out.status.success() {
|
||||
return Err(String::from_utf8_lossy(&out.stderr).to_string());
|
||||
}
|
||||
Ok(begun["upload_id"].as_str().unwrap().to_string())
|
||||
}
|
||||
|
||||
fn send(&mut self, message: Value) {
|
||||
let stdin = self.run.child.stdin.as_mut().unwrap();
|
||||
writeln!(stdin, "{message}").unwrap();
|
||||
@@ -405,11 +452,12 @@ fn post_one(a: &mut Agent, n: u64) -> Result<u64, String> {
|
||||
json!({"entry_id": id, "account": account, "side": side, "amount": "1.00"}),
|
||||
)?;
|
||||
}
|
||||
// Bytes of each entry's own: the same bytes stored already would be
|
||||
// named for linking, not uploaded again.
|
||||
let upload = a.upload(format!("receipt {n}").as_bytes(), &format!("r{n}.txt"))?;
|
||||
a.call(
|
||||
"books.attach_receipt",
|
||||
json!({"entry_id": id, "file_name": format!("r{n}.txt"),
|
||||
"mime_type": "text/plain", "content_base64": "cmVjZWlwdA==",
|
||||
"sha256": "6f32860910ca0fb2a20c7fda143666b09dbf8db5238195c90a586fb542ff0cad"}),
|
||||
json!({"entry_id": id, "upload_id": upload}),
|
||||
)?;
|
||||
a.call(
|
||||
"books.post_entry",
|
||||
@@ -2257,15 +2305,15 @@ fn an_agent_sends_and_saves_files_with_the_commands_it_is_given() {
|
||||
)
|
||||
.unwrap();
|
||||
let att = filed["attachment_id"].as_u64().unwrap();
|
||||
// Too big to come back inline: the command saves a copy, beside any
|
||||
// file of its name, never over it.
|
||||
// Handed over (D-46): the command saves a copy, beside any file of
|
||||
// its name, never over it.
|
||||
let back = a
|
||||
.call(
|
||||
"books.get_receipt",
|
||||
json!({"attachment_id": att, "view": "bytes"}),
|
||||
)
|
||||
.unwrap();
|
||||
let download = back["download"].as_str().unwrap().to_string();
|
||||
let download = back["file"]["download"].as_str().unwrap().to_string();
|
||||
for n in 1..=2 {
|
||||
let out = run_given(
|
||||
&home,
|
||||
@@ -2348,7 +2396,7 @@ fn the_commands_given_run_as_the_agents_own_token() {
|
||||
json!({"attachment_id": filed["attachment_id"], "view": "bytes"}),
|
||||
)
|
||||
.unwrap();
|
||||
let download = back["download"].as_str().unwrap().to_string();
|
||||
let download = back["file"]["download"].as_str().unwrap().to_string();
|
||||
assert!(download.contains(" --token bookkeeper "), "{download}");
|
||||
let out = run_given(
|
||||
&home,
|
||||
|
||||
Reference in new issue
Block a user