Skip to content

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

Python

login: the printed link carries the code; Windows opens the browser

justrach811120341a87
diff --git a/src/oauth_codegraff.zig b/src/oauth_codegraff.zig
index d33cd922..09e80a3d 100644
--- a/src/oauth_codegraff.zig
+++ b/src/oauth_codegraff.zig
@@ -48,11 +48,15 @@ pub fn login(io: Io, gpa: Allocator, arena: Allocator, home: []const u8) !void {
};
const user_code = strFieldObj(start_resp, "user_code") orelse "";
const verification_uri = strFieldObj(start_resp, "verification_uri") orelse "https://codegraff.com/cli/auth";
- const complete_uri = strFieldObj(start_resp, "verification_uri_complete") orelse verification_uri;
+ const complete_uri = strFieldObj(start_resp, "verification_uri_complete") orelse
+ if (user_code.len > 0) try std.fmt.allocPrint(arena, "{s}?code={s}", .{ verification_uri, user_code }) else verification_uri;
const interval: i64 = @max(1, intFieldObj(start_resp, "interval", 2));
const expires: i64 = intFieldObj(start_resp, "expires_in", 600);
- try out.print("\nTo authorize, open:\n\n {s}\n\nand enter code: {s}{s}{s}\n\nwaiting for approval …\n", .{ verification_uri, style.bold, user_code, style.reset });
+ // Print the link that carries the code: a person who copies it (the
+ // browser did not open, or opened elsewhere) lands on the approval itself,
+ // not on a page with nowhere to type the code.
+ try out.print("\nTo authorize, open:\n\n {s}\n\nand check it shows the code {s}{s}{s}\n\nwaiting for approval …\n", .{ complete_uri, style.bold, user_code, style.reset });
try out.flush();
openBrowser(io, complete_uri);
diff --git a/src/oauth_helpers.zig b/src/oauth_helpers.zig
index 984a3125..728e07c8 100644
--- a/src/oauth_helpers.zig
+++ b/src/oauth_helpers.zig
@@ -541,12 +541,30 @@ test "#402: a refresh write preserves the fields the codex CLI owns" {
/// name for `open -a` on macOS, a command elsewhere).
pub fn openBrowser(io: Io, url: []const u8) void {
const chosen: ?[]const u8 = if (std.c.getenv("GRAFF_BROWSER")) |v| std.mem.span(v) else null;
- const argv: []const []const u8 = if (builtin.os.tag == .macos)
- (if (chosen) |app| &.{ "open", "-a", app, url } else &.{ "open", url })
- else if (chosen) |cmd|
- &.{ cmd, url }
- else
- &.{ "xdg-open", url };
- var child = std.process.spawn(io, .{ .argv = argv, .stdin = .ignore, .stdout = .ignore, .stderr = .ignore }) catch return;
+ var buf: [4][]const u8 = undefined;
+ var child = std.process.spawn(io, .{ .argv = browserArgv(builtin.os.tag, chosen, url, &buf), .stdin = .ignore, .stdout = .ignore, .stderr = .ignore }) catch return;
_ = child.wait(io) catch {};
}
+
+/// Windows has no `xdg-open`: the browser never opened there, so sign-ins
+/// waited on a page nobody saw. url.dll's handler takes the URL as one
+/// argument, with no shell to split it at `&`.
+fn browserArgv(os: std.Target.Os.Tag, chosen: ?[]const u8, url: []const u8, buf: *[4][]const u8) []const []const u8 {
+ const parts: []const []const u8 = switch (os) {
+ .macos => if (chosen) |app| &.{ "open", "-a", app, url } else &.{ "open", url },
+ .windows => if (chosen) |cmd| &.{ cmd, url } else &.{ "rundll32.exe", "url.dll,FileProtocolHandler", url },
+ else => if (chosen) |cmd| &.{ cmd, url } else &.{ "xdg-open", url },
+ };
+ @memcpy(buf[0..parts.len], parts);
+ return buf[0..parts.len];
+}
+
+test "browserArgv opens the default browser on every platform" {
+ var buf: [4][]const u8 = undefined;
+ const url = "https://codegraff.com/cli/auth?code=AB12-CD34&x=1";
+ try std.testing.expectEqualDeep(@as([]const []const u8, &.{ "rundll32.exe", "url.dll,FileProtocolHandler", url }), browserArgv(.windows, null, url, &buf));
+ try std.testing.expectEqualDeep(@as([]const []const u8, &.{ "open", url }), browserArgv(.macos, null, url, &buf));
+ try std.testing.expectEqualDeep(@as([]const []const u8, &.{ "xdg-open", url }), browserArgv(.linux, null, url, &buf));
+ try std.testing.expectEqualDeep(@as([]const []const u8, &.{ "open", "-a", "Safari", url }), browserArgv(.macos, "Safari", url, &buf));
+ try std.testing.expectEqualDeep(@as([]const []const u8, &.{ "firefox", url }), browserArgv(.windows, "firefox", url, &buf));
+}