graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Fix the Linux MCP service unit and `graff acp login`
justrach4bc1580bc989
diff --git a/scripts/install-mcp.py b/scripts/install-mcp.pyindex 1aa91f3f..e5539146 100644--- a/scripts/install-mcp.py+++ b/scripts/install-mcp.py@@ -199,6 +199,29 @@ def start_detached(command, directory, token, log_path):log.close()+def systemd_unit(command, directory, token):+ """The user unit. ExecStart arguments are quoted words; WorkingDirectory is+ one literal path that systemd does not unquote (a quoted value is "not+ absolute"), so it only escapes the % specifier character."""+ directory = str(directory)+ if '\n' in directory or not directory.startswith('/'):+ raise ValueError('Workspace path cannot be written to a systemd unit')+ quote = lambda value: json.dumps(str(value).replace('%', '%%').replace('$', '$$'))+ return (MARKER + '\n[Unit]\nDescription=Codegraff local MCP\n[Service]\n' ++ 'ExecStart=' + ' '.join(quote(part) for part in command) + '\n' ++ 'WorkingDirectory=' + directory.replace('%', '%%') + '\nEnvironment=GRAFF_MCP_TOKEN=' + token ++ '\nRestart=on-failure\nRestartSec=10\n[Install]\nWantedBy=default.target\n')+++def systemctl(*args):+ """systemctl --user, failing with systemd's own explanation."""+ result = run_managed(['systemctl', '--user', *args], check=False)+ if result.returncode != 0:+ detail = (result.stderr or b'').decode(errors='replace').strip() or f'exit {result.returncode}'+ raise ValueError(f'systemctl --user {" ".join(args)} failed: {detail}')+ return result++def service(home, executable, directory, port, token):state = home / '.graff/mcp'command = [str(executable), 'mcp', 'serve', '--http', '--port', str(port)]@@ -220,15 +243,11 @@ def service(home, executable, directory, port, token):target = home / '.config/systemd/user/codegraff-mcp.service'if target.exists() and MARKER not in target.read_text():raise ValueError('Existing service is not managed by Codegraff')- quote = lambda value: json.dumps(str(value).replace('%', '%%').replace('$', '$$'))- atomic(target, MARKER + '\n[Unit]\nDescription=Codegraff local MCP\n[Service]\n' +- 'ExecStart=' + ' '.join(quote(part) for part in command) + '\n' +- 'WorkingDirectory=' + quote(directory) + '\nEnvironment=GRAFF_MCP_TOKEN=' + token +- '\nRestart=on-failure\nRestartSec=10\n[Install]\nWantedBy=default.target\n')+ atomic(target, systemd_unit(command, directory, token))if linux_user_systemd():- run_managed(['systemctl', '--user', 'daemon-reload'])- run_managed(['systemctl', '--user', 'enable', '--now', 'codegraff-mcp.service'])- run_managed(['systemctl', '--user', 'restart', 'codegraff-mcp.service'])+ systemctl('daemon-reload')+ systemctl('enable', '--now', 'codegraff-mcp.service')+ systemctl('restart', 'codegraff-mcp.service')returnif sys.platform.startswith('linux'):start_detached(command, directory, token, home / '.graff/mcp/service.log')diff --git a/scripts/test-install-mcp.py b/scripts/test-install-mcp.pyindex 1631f1c6..43022707 100644--- a/scripts/test-install-mcp.py+++ b/scripts/test-install-mcp.py@@ -167,5 +167,34 @@ class InstallTests(unittest.TestCase):self.assertIn('Codegraff managed MCP service', unit.read_text())self.assertIn('mcp', unit.read_text())+ def test_systemd_unit_working_directory_is_a_literal_path(self):+ directory = Path('/home/user/my 100% $proj')+ unit = installer.systemd_unit(['/usr/bin/graff', 'mcp', 'serve'], directory, self.token)+ line = next(l for l in unit.splitlines() if l.startswith('WorkingDirectory='))+ # Unquoted (systemd takes the rest of the line as one path); only % escaped.+ self.assertEqual(line, 'WorkingDirectory=/home/user/my 100%% $proj')+ self.assertIn('ExecStart="/usr/bin/graff" "mcp" "serve"', unit)+ with self.assertRaises(ValueError):+ installer.systemd_unit(['graff'], 'relative/dir', self.token)++ @unittest.skipUnless(sys.platform.startswith('linux') and installer.shutil.which('systemd-analyze'),+ 'needs systemd-analyze')+ def test_systemd_accepts_the_unit(self):+ workspace = self.home / 'my 100% proj'+ workspace.mkdir()+ unit = self.home / 'codegraff-mcp.service'+ unit.write_text(installer.systemd_unit(['/bin/true'], workspace, self.token))+ result = subprocess.run(['systemd-analyze', '--user', 'verify', str(unit)],+ capture_output=True, text=True)+ self.assertNotIn('not absolute', result.stderr)+ self.assertNotIn('WorkingDirectory', result.stderr)++ def test_systemctl_failure_carries_its_stderr(self):+ failed = subprocess.CompletedProcess(['systemctl'], 1, b'', b'Failed to enable unit: Unit file is masked.')+ with patch.object(installer, 'run_managed', return_value=failed):+ with self.assertRaises(ValueError) as caught:+ installer.systemctl('enable', '--now', 'codegraff-mcp.service')+ self.assertIn('Unit file is masked', str(caught.exception))+if __name__ == '__main__':unittest.main()diff --git a/src/args.zig b/src/args.zigindex 2906db5f..1d02a056 100644--- a/src/args.zig+++ b/src/args.zig@@ -258,6 +258,7 @@ pub fn parse(init: std.process.Init) !Flags {try flags.positionals.append(arena, try arena.dupe(u8, arg));}}+ acpLoginIsLogin(&flags.positionals);if (flags.positionals.items.len > 0 and std.mem.eql(u8, flags.positionals.items[0], "login")) flags.login_flag = true;if (flags.login_flag and flags.positionals.items.len > 1) {const name = flags.positionals.items[1];@@ -320,6 +321,15 @@ pub fn parse(init: std.process.Init) !Flags {/// <name>` alike. Every ChatGPT name, codex included, is the plan's sign-in,/// chatgpt-new: the Codex client's login no longer works (ADR 0229). Null:/// not a login.+/// ACP Terminal Auth advertises `args: ["login"]`. The v1 schema appends them+/// to the agent command (`graff acp login`); the registry's docs say they+/// replace it (`graff login`). Both must sign in, never start the server.+pub fn acpLoginIsLogin(positionals: *std.ArrayList([]const u8)) void {+ const items = positionals.items;+ if (items.len > 1 and std.mem.eql(u8, items[0], "acp") and std.mem.eql(u8, items[1], "login"))+ _ = positionals.orderedRemove(0);+}+pub fn loginTarget(name: []const u8) ?[]const u8 {const aliases = [_][2][]const u8{.{ "codex", "chatgpt-new" }, .{ "chatgpt", "chatgpt-new" }, .{ "openai", "chatgpt-new" }, .{ "gpt", "chatgpt-new" },@@ -344,6 +354,24 @@ test "loginTarget: every ChatGPT name, codex included, is the plan's sign-in; untry std.testing.expect(loginTarget("") == null);}+test "acp login is login, with or without a target; plain acp stays acp" {+ const gpa = std.testing.allocator;+ for ([_]struct { in: []const []const u8, first: []const u8, len: usize }{+ .{ .in = &.{ "acp", "login" }, .first = "login", .len = 1 },+ .{ .in = &.{ "acp", "login", "chatgpt" }, .first = "login", .len = 2 },+ .{ .in = &.{"acp"}, .first = "acp", .len = 1 },+ .{ .in = &.{ "acp", "--yolo" }, .first = "acp", .len = 2 },+ .{ .in = &.{"login"}, .first = "login", .len = 1 },+ }) |case| {+ var list: std.ArrayList([]const u8) = .empty;+ defer list.deinit(gpa);+ try list.appendSlice(gpa, case.in);+ acpLoginIsLogin(&list);+ try std.testing.expectEqualStrings(case.first, list.items[0]);+ try std.testing.expectEqual(case.len, list.items.len);+ }+}+test "tui is a reserved subcommand, not a one-shot prompt" {var tui_pos = std.ArrayList([]const u8).empty;defer tui_pos.deinit(std.testing.allocator);