Skip to content

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

Python

session/load: seat the saved provider's plan login before restoring

justrachd7df0f64ad2a
diff --git a/scripts/test-acp-session-load.py b/scripts/test-acp-session-load.py
index 244b654e..c410861c 100644
--- a/scripts/test-acp-session-load.py
+++ b/scripts/test-acp-session-load.py
@@ -204,8 +204,47 @@ def claude_import(binary):
print("ACP Claude history import uses graff credentials and retains tool receipts: ok")
+def plan_login_restore(binary):
+ """#1556: a host switches models by respawning with --model and calling
+ session/load. A save on a plan-login provider outside that --model's scope
+ must still restore."""
+ if os.name == "nt":
+ return
+ record = {"email": "[email protected]", "issuer": "https://auth.openai.com", "subject": "user-1",
+ "client_id": "oaiapp_x", "ext_agent_host_id": "urn:uuid:1", "id_token": "a.b.c",
+ "access_token": "plan-token", "refresh_token": "ref-1", "token_type": "Bearer", "expires_in": 3600,
+ "expires_at": 4102444800, "earliest_refresh_at": 4102444000,
+ "scopes": ["chatgpt.tokens.use.direct", "email", "offline_access", "openid", "profile", "resource.invoke"],
+ "saved_at": "2026-09-30T00:00:00Z"}
+ with tempfile.TemporaryDirectory(prefix="graff-acp-plan-load-") as temporary:
+ cwd = Path(temporary).resolve()
+ home = cwd / "home"
+ credentials = home / ".graff" / "credentials"
+ credentials.mkdir(parents=True, mode=0o700)
+ os.chmod(home / ".graff", 0o700)
+ (credentials / "chatgpt-new.json").write_text(json.dumps(record))
+ os.chmod(credentials / "chatgpt-new.json", 0o600)
+ sessions = cwd / ".graff" / "sessions"
+ sessions.mkdir(parents=True)
+ (sessions / "plan-saved.session.json").write_text(json.dumps({
+ "provider": "chatgpt-new", "model": "gpt-6.1-sol", "workspace": str(cwd),
+ "messages": [{"role": "user", "content": "Plan-saved request."}, {"role": "assistant", "content": "ok"}]}))
+ model = ScriptedModel([{"text": "unused"}])
+ port = model.start(0)
+ client = Acp(binary, cwd, home, port) # --model vercel: the plan provider is out of scope
+ try:
+ client.request("initialize", {"protocolVersion": 1})
+ loaded = client.request("session/load", {"sessionId": "plan-saved", "cwd": str(cwd), "mcpServers": []})
+ assert "result" in loaded, loaded
+ finally:
+ client.close()
+ model.stop()
+ print("ACP session/load restores a plan-login save after a cross-provider --model respawn: ok")
+
+
def run(binary):
claude_import(binary)
+ plan_login_restore(binary)
with tempfile.TemporaryDirectory(prefix="graff-acp-load-") as temporary:
cwd = Path(temporary)
home = cwd / "home"
diff --git a/src/credential_failover.zig b/src/credential_failover.zig
index 559f73ac..e8e3438a 100644
--- a/src/credential_failover.zig
+++ b/src/credential_failover.zig
@@ -104,6 +104,21 @@ pub fn preferPlan(
source.* = .login;
}
+/// #1556: startup seats plans only for the providers its `--model` selection
+/// can observe. A restored session observes its saved provider too, so seat
+/// that one plan on demand, as an unscoped startup would have: before the
+/// stored-key fill, which only takes empty slots. No-op once it holds a login.
+pub fn preferPlanFor(io: std.Io, gpa: std.mem.Allocator, arena: std.mem.Allocator, home: []const u8, keys: *Keys, provider_id: []const u8) void {
+ if (home.len == 0) return;
+ const i = indexOf(provider_id) orelse return;
+ if (keys.sources[i] == .login) return;
+ preferPlan(io, gpa, arena, home, provider_mod.provider_specs[i], &keys.values[i], &keys.sources[i]);
+}
+
+test {
+ _ = @import("session_load_plan_tests.zig"); // #1556
+}
+
/// Test-only: the table is process-global, like the key store it mirrors.
pub fn resetForTest() void {
g_value = @splat(null);
diff --git a/src/session.zig b/src/session.zig
index a755b604..2c091d4f 100644
--- a/src/session.zig
+++ b/src/session.zig
@@ -493,6 +493,8 @@ pub fn loadSession(root: *Agent, keys: *Keys, arena: Allocator, name: []const u8
if (cacheKeyFromSession(obj)) |k| http_headers.restoreSessionId(k);
if (promptCacheKeyFromSession(obj)) |k| http_headers.restoreProjectRootId(k);
+ // #1556: an explicit cross-provider --model launch skipped this plan login.
+ if (!claude_import) @import("credential_failover.zig").preferPlanFor(root.io, root.gpa, root.arena, root.home, keys, pid);
root.ensureStoredKeys(keys);
if (!claude_import) {
if (std.mem.eql(u8, pid, "codex")) root.ensureModelCatalog(keys.*);
diff --git a/src/session_load_plan_tests.zig b/src/session_load_plan_tests.zig
new file mode 100644
index 00000000..3c030d96
--- /dev/null
+++ b/src/session_load_plan_tests.zig
@@ -0,0 +1,85 @@
+//! #1556: a host switches models by respawning with an explicit `--model` and
+//! reattaching the conversation with session/load. Startup seats plan logins
+//! only for the providers that `--model` can observe, so the saved session's
+//! own provider must have its plan seated when the restore needs it.
+
+const std = @import("std");
+const Agent = @import("agent.zig").Agent;
+const provider_mod = @import("provider.zig");
+const credential_store = @import("credential_store.zig");
+const credential_failover = @import("credential_failover.zig");
+
+const plan_record =
+ \\{"email": "[email protected]", "issuer": "https://auth.openai.com", "subject": "user-1",
+ \\ "client_id": "oaiapp_x", "ext_agent_host_id": "urn:uuid:1", "id_token": "a.b.c",
+ \\ "access_token": "plan-tok-1556", "refresh_token": "ref-1", "token_type": "Bearer", "expires_in": 3600,
+ \\ "expires_at": 4102444800, "earliest_refresh_at": 4102444000,
+ \\ "scopes": ["chatgpt.tokens.use.direct", "email", "offline_access", "openid", "profile", "resource.invoke"],
+ \\ "saved_at": "2026-09-30T00:00:00Z"}
+;
+
+test "#1556 session/load seats the saved provider's plan login a scoped launch skipped" {
+ 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();
+ credential_failover.resetForTest();
+ defer credential_failover.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.createDir(io, ".graff", credential_store.private_dir);
+ try tmp.dir.createDir(io, ".graff/credentials", credential_store.private_dir);
+ try credential_store.replaceFile(io, tmp.dir, ".graff/credentials/chatgpt-new.json", plan_record, credential_store.private_file);
+ try tmp.dir.createDirPath(io, ".graff/sessions");
+ try tmp.dir.writeFile(io, .{
+ .sub_path = ".graff/sessions/plan-1556.session.json",
+ .data = "{\"provider\":\"chatgpt-new\",\"model\":\"gpt-6.1-sol\",\"messages\":[{\"role\":\"user\",\"content\":\"remember 1556\"}]}",
+ });
+
+ // What `--model <another provider's model>` leaves behind: only that
+ // provider's credential is seated, and the saved provider's slot is empty.
+ 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;
+ }
+ try std.testing.expect(keys.get("chatgpt-new") == null);
+
+ 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,
+ // The stored-key fill is not under test (and would read the Keychain).
+ .stored_keys_loaded = true,
+ };
+ try session.loadSession(&root, &keys, arena, "plan-1556");
+ try std.testing.expectEqualStrings("chatgpt-new", root.provider.id);
+ try std.testing.expectEqualStrings("plan-tok-1556", root.provider.api_key);
+ try std.testing.expectEqual(provider_mod.Keys.CredentialSource.login, keys.sources[indexOf("chatgpt-new")]);
+}
+
+fn indexOf(id: []const u8) usize {
+ for (provider_mod.provider_specs, 0..) |spec, i| if (std.mem.eql(u8, spec.id, id)) return i;
+ unreachable;
+}