Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

Load and list session saves larger than 8 MiB

justrach39a67a2da57d
diff --git a/src/acp_session_load.zig b/src/acp_session_load.zig
index 00006602..aae9c416 100644
--- a/src/acp_session_load.zig
+++ b/src/acp_session_load.zig
@@ -81,8 +81,8 @@ pub fn load(ctx: *anyopaque, arena: Allocator, w: *Io.Writer, req: proto.Request
};
const canonical_active = workspace_switch.currentAbs(live.root.io, arena);
if (stat.kind != .file) return reject(w, req, "Unknown session ID in the selected workspace");
- const bytes = Io.Dir.cwd().readFileAlloc(live.root.io, path, arena, .limited(8 * 1024 * 1024)) catch
- return reject(w, req, "Saved session could not be loaded");
+ const bytes = Io.Dir.cwd().readFileAlloc(live.root.io, path, arena, .limited(session_index.max_session_bytes)) catch |err|
+ return reject(w, req, loadErrorText(arena, if (err == error.StreamTooLong) error.SessionTooLarge else err, ""));
const parsed = std.json.parseFromSliceLeaky(std.json.Value, arena, bytes, .{ .allocate = .alloc_always }) catch
return reject(w, req, "Saved session could not be loaded");
if (parsed != .object) return reject(w, req, "Saved session could not be loaded");
@@ -121,6 +121,7 @@ pub fn load(ctx: *anyopaque, arena: Allocator, w: *Io.Writer, req: proto.Request
/// of all, which the host would otherwise present as lost context.
fn loadErrorText(arena: Allocator, err: anyerror, provider_id: []const u8) []const u8 {
if (err == error.FileNotFound) return "Unknown session ID in the selected workspace";
+ if (err == error.SessionTooLarge) return "Saved session is larger than the session read limit and could not be loaded";
if (err == error.MissingKey and provider_id.len > 0)
return std.fmt.allocPrint(arena, "Saved session is intact, but its provider \"{s}\" has no credential in this process; run `graff login {s}` or `graff key set {s} <key>`, then load it again", .{ provider_id, provider_id, provider_id }) catch "Saved session is intact, but its provider has no credential in this process";
return std.fmt.allocPrint(arena, "Saved session could not be loaded ({s})", .{@errorName(err)}) catch "Saved session could not be loaded";
@@ -136,6 +137,7 @@ test "loadErrorText names a missing credential instead of blaming the save (#155
try std.testing.expect(std.mem.indexOf(u8, missing, "could not be loaded") == null);
try std.testing.expectEqualStrings("Unknown session ID in the selected workspace", loadErrorText(a, error.FileNotFound, "x"));
try std.testing.expectEqualStrings("Saved session could not be loaded (BadSession)", loadErrorText(a, error.BadSession, "x"));
+ try std.testing.expect(std.mem.indexOf(u8, loadErrorText(a, error.SessionTooLarge, "x"), "read limit") != null);
}
fn replayFromStart(params: std.json.Value) bool {
diff --git a/src/session.zig b/src/session.zig
index 2c091d4f..f89523f6 100644
--- a/src/session.zig
+++ b/src/session.zig
@@ -467,7 +467,8 @@ pub fn restoreContextMeter(root: *Agent, saved_context_tokens: u64, saved_local_
pub fn loadSession(root: *Agent, keys: *Keys, arena: Allocator, name: []const u8) !void {
flushSaves(); // #273: a session switch never leaves the outgoing one queued
const found = @import("session_discovery.zig").locate(root, arena, name) orelse return error.FileNotFound;
- const data = Io.Dir.cwd().readFileAlloc(root.io, found.path, arena, .limited(8 * 1024 * 1024)) catch return error.FileNotFound;
+ const data = Io.Dir.cwd().readFileAlloc(root.io, found.path, arena, .limited(session_index.max_session_bytes)) catch |err|
+ return if (err == error.StreamTooLong) error.SessionTooLarge else error.FileNotFound;
const parsed = try std.json.parseFromSliceLeaky(Value, arena, data, .{ .allocate = .alloc_always });
if (parsed != .object) return error.BadSession;
const obj = parsed.object;
diff --git a/src/session_discovery.zig b/src/session_discovery.zig
index 21c2fcdd..fcd2cfe9 100644
--- a/src/session_discovery.zig
+++ b/src/session_discovery.zig
@@ -70,7 +70,7 @@ pub fn locateIn(
if (session_index.sameWorkspace(workspace, cwd)) continue;
const path = absSession(arena, workspace, name) catch continue;
if (!exists(io, path)) continue;
- const data = Io.Dir.cwd().readFileAlloc(io, path, arena, .limited(8 * 1024 * 1024)) catch continue;
+ const data = Io.Dir.cwd().readFileAlloc(io, path, arena, .limited(session_index.max_session_bytes)) catch continue;
const meta = session_index.sessionMetaFromBytes(arena, data);
const ms = meta.updated_ms;
if (best == null or ms > best_ms) {
diff --git a/src/session_index.zig b/src/session_index.zig
index deed4733..a4fcfc40 100644
--- a/src/session_index.zig
+++ b/src/session_index.zig
@@ -19,6 +19,12 @@ pub const session_ext = ".session.json";
/// Title-named session files live here (resume reads this).
pub const sessions_dir = ".graff/sessions";
+/// Read limit shared by every session-file reader. saveSession has no limit,
+/// and a provider-native compaction item is opaque and grows with the context
+/// it summarizes, so one item alone can be many MiB. A save that loads must
+/// also list, so nothing reads a session with a smaller cap (#1654).
+pub var max_session_bytes: usize = 256 * 1024 * 1024;
+
/// INVARIANT: every `.graff/` path this harness builds is FORWARD-SLASHED on
/// every platform, including Windows. Not an accident of `allocPrint` — a
/// choice, and callers may rely on it:
@@ -79,7 +85,7 @@ pub fn sessionMetaFromBytes(arena: Allocator, data: []const u8) SessionMeta {
pub fn sessionMeta(root: *Agent, arena: Allocator, base: []const u8) SessionMeta {
const path = sessionPath(arena, base) catch return .{};
- const data = Io.Dir.cwd().readFileAlloc(root.io, path, arena, .limited(8 * 1024 * 1024)) catch return .{};
+ const data = Io.Dir.cwd().readFileAlloc(root.io, path, arena, .limited(max_session_bytes)) catch return .{};
return sessionMetaFromBytes(arena, data);
}
@@ -115,14 +121,14 @@ pub fn sameWorkspace(a: []const u8, b: []const u8) bool {
return std.mem.eql(u8, std.mem.trimEnd(u8, a, "/"), std.mem.trimEnd(u8, b, "/"));
}
-fn listFromDir(io: Io, arena: Allocator, dir: Io.Dir, workspace: []const u8, local: bool) std.ArrayList(SessionEntry) {
+pub fn listFromDir(io: Io, arena: Allocator, dir: Io.Dir, workspace: []const u8, local: bool) std.ArrayList(SessionEntry) {
var entries: std.ArrayList(SessionEntry) = .empty;
var it = dir.iterate();
while (it.next(io) catch null) |entry| {
if (entry.kind != .file) continue;
if (!std.mem.endsWith(u8, entry.name, session_ext)) continue;
const base = arena.dupe(u8, entry.name[0 .. entry.name.len - session_ext.len]) catch continue;
- const data = dir.readFileAlloc(io, entry.name, arena, .limited(8 * 1024 * 1024)) catch continue;
+ const data = dir.readFileAlloc(io, entry.name, arena, .limited(max_session_bytes)) catch continue;
const meta = sessionMetaFromBytes(arena, data);
entries.append(arena, .{
.base = base,
@@ -235,6 +241,10 @@ pub fn savedSessionsHint(n: usize, buf: []u8) []const u8 {
}) catch "";
}
+test {
+ _ = @import("session_size_tests.zig"); // #1654
+}
+
test "sessionMetaFromBytes reads title + updated_ms from the header only" {
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_state.deinit();
diff --git a/src/session_size_tests.zig b/src/session_size_tests.zig
new file mode 100644
index 00000000..63b22db9
--- /dev/null
+++ b/src/session_size_tests.zig
@@ -0,0 +1,76 @@
+//! #1654: saveSession writes no size limit, but every reader used to cap a
+//! session file at 8 MiB. One opaque provider compaction item can exceed that
+//! alone, so a long session saved fine and then could not be listed or loaded.
+
+const std = @import("std");
+const Agent = @import("agent.zig").Agent;
+const provider_mod = @import("provider.zig");
+const session_index = @import("session_index.zig");
+
+/// A save past the old 8 MiB cap: one large opaque message before the tail.
+fn writeLargeSave(gpa: std.mem.Allocator, dir: std.Io.Dir, sub_path: []const u8) !void {
+ const blob = try gpa.alloc(u8, 9 * 1024 * 1024);
+ defer gpa.free(blob);
+ @memset(blob, 'a');
+ const data = try std.fmt.allocPrint(gpa, "{{\"provider\":\"anthropic\",\"model\":\"sonnet\",\"title\":\"Long chat\",\"updated_ms\":7,\"messages\":[{{\"role\":\"user\",\"content\":\"{s}\"}},{{\"role\":\"user\",\"content\":\"remember 1654\"}}]}}", .{blob});
+ defer gpa.free(data);
+ try dir.writeFile(std.testing.io, .{ .sub_path = sub_path, .data = data });
+}
+
+test "#1654 a session save larger than 8 MiB still lists and loads" {
+ const session = @import("session.zig");
+ const writer = @import("session_writer.zig");
+ const transcript = @import("session_transcript.zig");
+ const gpa = std.testing.allocator;
+ const io = std.testing.io;
+ writer.resetForTest();
+ defer writer.resetForTest();
+ transcript.resetForTest();
+ defer transcript.resetForTest();
+
+ var arena_state = std.heap.ArenaAllocator.init(gpa);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var home_buf: [std.fs.max_path_bytes]u8 = undefined;
+ const home = home_buf[0..try tmp.dir.realPath(io, &home_buf)];
+ try tmp.dir.createDirPath(io, ".graff/sessions");
+ try writeLargeSave(gpa, tmp.dir, ".graff/sessions/large-1654.session.json");
+
+ var sessions = try tmp.dir.openDir(io, ".graff/sessions", .{ .iterate = true });
+ defer sessions.close(io);
+ const listed = session_index.listFromDir(io, arena, sessions, home, false);
+ try std.testing.expectEqual(@as(usize, 1), listed.items.len);
+ try std.testing.expectEqualStrings("Long chat", listed.items[0].title.?);
+
+ var keys: provider_mod.Keys = .{ .values = @splat(null) };
+ for (provider_mod.provider_specs, &keys.values, &keys.sources) |spec, *value, *source| {
+ if (!std.mem.eql(u8, spec.id, "anthropic")) continue;
+ value.* = "launch-key";
+ source.* = .environment;
+ }
+ var client: std.http.Client = .{ .allocator = gpa, .io = io };
+ defer client.deinit();
+ var root: Agent = .{
+ .gpa = gpa,
+ .arena = arena,
+ .io = io,
+ .client = &client,
+ .provider = try keys.providerById("anthropic", "sonnet"),
+ .messages = .init(arena),
+ .sub = false,
+ .label = "root",
+ .out = null,
+ .home = home,
+ .stored_keys_loaded = true,
+ };
+ try session.loadSession(&root, &keys, arena, "large-1654");
+ try std.testing.expectEqual(@as(usize, 2), root.messages.items.len);
+
+ // Past the shared limit the save is reported as too large, not as missing.
+ const saved_limit = session_index.max_session_bytes;
+ defer session_index.max_session_bytes = saved_limit;
+ session_index.max_session_bytes = 1024 * 1024;
+ try std.testing.expectError(error.SessionTooLarge, session.loadSession(&root, &keys, arena, "large-1654"));
+}