Skip to content

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

Python

Merge #697 into release/v0.0.282: clone-on-write session branches

Cursor Agent642924a062dd
diff --cc TUI/root.zig
index d60f629d,ca45e023..7919550e
--- a/TUI/root.zig
+++ b/TUI/root.zig
@@@ -38,14 -42,11 +42,18 @@@ pub const StateFn = engine.StateFn
pub const PeerFn = engine.PeerFn;
pub const RunOpts = run_mod.RunOpts;
pub const run = run_mod.run;
+ pub fn setCurrentModel(name: []const u8, provider: []const u8) void {
+ engine.g_model_name = name;
+ engine.g_model_provider = provider;
+ }
pub const restore = @import("restore.zig");
+pub const enable_seq = run_mod.enable_seq;
+
+/// Claim the alt-screen as soon as the command is `tui`/`repl`, so leftover
+/// boot (keys, MCP, AGENTS.md) happens inside the pager. No-op off a TTY.
+pub fn claim() bool {
+ return restore.claimScreen(run_mod.enable_seq);
+}
/// Restore the terminal BEFORE std prints a panic, or the alt-screen exit in
/// the restore sequence erases the message and the stack trace (#535).
pub const panic = restore.Panic;
diff --cc TUI/run.zig
index 2fa161c2,5a24f295..afa919ec
--- a/TUI/run.zig
+++ b/TUI/run.zig
@@@ -65,12 -67,22 +67,24 @@@ pub fn run
var m: Model = undefined;
m.setup(gpa);
defer m.deinit();
+ defer if (opts.state_fn) |f| f(opts.turn_ctx, .{
+ .session_name = m.session_name orelse "",
+ .goal = m.goal orelse "",
+ .strict = m.strict,
+ .ultracode = m.ultracode,
+ });
+ for (opts.initial_history) |item| m.push(if (item.role == .user) .user else .assistant, item.text) catch {};
+ m.turns = m.userTurnCount();
+ if (opts.session_name.len > 0) m.session_name = gpa.dupe(u8, opts.session_name) catch null;
+ if (opts.initial_goal.len > 0) m.goal = gpa.dupe(u8, opts.initial_goal) catch null;
+ m.strict = opts.initial_strict;
+ m.ultracode = opts.initial_ultracode;
if (opts.yolo) m.mode = .always_approve;
- var raw = tty.enterRaw() orelse return error.NotATty;
- restore_mod.arm(raw, enable_seq);
+ const claimed = restore_mod.takeClaim();
+ var raw = claimed orelse (tty.enterRaw() orelse return error.NotATty);
+ if (claimed != null) _ = tty.enterRaw(); // boot may have put the line discipline back
+ if (claimed == null) restore_mod.arm(raw, enable_seq);
defer restore_mod.disarm();
defer tty.restore(raw);
diff --cc docs/adr/0049-resume-branches-have-independent-durable-identity.md
index 00000000,c4be74f5..e3c1fcc2
mode 000000,100644..100644
--- a/docs/adr/0049-resume-branches-have-independent-durable-identity.md
+++ b/docs/adr/0049-resume-branches-have-independent-durable-identity.md
@@@ -1,0 -1,43 +1,43 @@@
-# 0042. Resume branches have independent durable identity
++# 0049. Resume branches have independent durable identity
+
+ Status: accepted 2026-08-30
+
+ ## Context
+
+ Issue #689 reproduced silent loss when two processes resumed one session name.
+ The advisory writer lock serialized individual whole-file replacements, but it
+ could not turn two descendants into independent tips. It also exposed that the
+ fullscreen ACP-backed TUI rendered an empty conversation after startup resume
+ and bypassed the root final-save path.
+
+ Provider-native tool/reasoning blocks and compaction boundaries make merging two
+ message arrays unsafe. A process-lifetime source lock would avoid corruption by
+ rejecting the second user, but would not provide the requested branch behavior.
+
+ ## Decision
+
+ `--resume SOURCE --branch DESTINATION` and
+ `/resume SOURCE --branch DESTINATION` clone SOURCE once into a new durable
+ identity. DESTINATION must be new and distinct from SOURCE; creation uses an
+ exclusive filesystem claim so two processes cannot both win a previously
+ unused name. Future turns, transcripts, checkpoints, compaction, and shutdown
+ saves target only DESTINATION; the session header records `parent: SOURCE` and
+ the branch receives a fresh persisted cache/session UUID.
+
+ The fullscreen TUI, TTY `graff repl`, scripted `graff repl`, and ACP startup all
+ consume the same restored provider-native history. The fullscreen frontend also
+ projects that history into visible rows and syncs its engine-owned conversation
+ back to the root before final save.
+
+ A branch copies ADR 0014's peer cursor and unread inbox snapshot exactly once as
+ part of the session snapshot. Parent and child then persist their cursors
+ independently; neither replays the room and neither shares mutable inbox state.
+ Git worktree isolation remains a separate choice.
+
+ ## Consequences
+
+ Concurrent continuations need distinct destination names, which keeps conflicts
+ explicit and makes reopening deterministic. Existing `/resume SOURCE` remains a
+ same-tip continuation for compatibility and is not safe as a branching command.
+ There is no automatic merge; future merge/cherry-pick work must understand
+ provider-native history rather than appending JSON arrays.
diff --cc docs/adr/README.md
index fd8b7e81,783f6a71..2ea750be
--- a/docs/adr/README.md
+++ b/docs/adr/README.md
@@@ -52,13 -52,7 +52,14 @@@ record only when you need the evidence
| [0039](0039-local-tools-are-project-scripts.md) | Agent-authored local tools are project scripts under `.graff/tools/`; skills stay instructions. Runtime catalog extras, not `schema.effectiveRootSpecs`. |
| [0040](0040-codedb-stays-when-licensed.md) | Ordinary reads use native `codedb` / `read_file`; codedb-pro is extra search, not the default reader. |
| [0041](0041-tui-is-an-acp-client.md) | The fullscreen TUI is an in-process ACP client: session/prompt in, session/update thought/tool/text out. No child `graff acp`. |
-| [0042](0042-resume-branches-have-independent-durable-identity.md) | `--resume SOURCE --branch DEST` clones provider history and peer cursor state once; every later save belongs only to DEST. |
+| [0042](0042-tui-claims-screen-before-session.md) | `graff tui` / TTY `graff repl` claim the alt-screen before keys/MCP/prompt; leftover boot happens inside the pager. |
+| [0043](0043-pi-swe-same-seat.md) | Pi SWE A/B uses `pi-xai` on the SuperGrok seat; do not steal Pi's catalog or heap from the json-stream pass. |
+| [0044](0044-oneshot-skips-learn-auto.md) | `-p` and `--json` skip learn auto-init; the Pi SWE wall gap was a 38s `graff-pinned` copy, not their catalog. |
+| [0045](0045-glm-flash-swe-codegraff.md) | Codegraff `glm-5.3-flash` SWE: Pi 5/6 in 758s, graff 3/6 with three 300s timeouts; do not steal Pi's heap. |
+| [0046](0046-flash-omits-default-effort.md) | Flash / Gemini send `reasoning_effort=low` (omit still thinks); lean `-p` shortens tool prose; `-p` streams. |
+| [0047](0047-codegraff-swe-not-glm-only.md) | Codegraff SWE A/B is not GLM-only: Gemini graff 5/6 in 103s; DeepSeek flash still one-shots; do not steal Pi's catalog. |
+| [0048](0048-model-http-client-recovery-uses-generations.md) | Model HTTP calls lease a recoverable client generation; request-construction TLS failure rotates safely without deinitializing in-flight users. |
++| [0049](0049-resume-branches-have-independent-durable-identity.md) | `--resume SOURCE --branch DEST` clones provider history and peer cursor state once; every later save belongs only to DEST. |
## When to write one
diff --cc src/main.zig
index c929a04c,4162923f..a7cbf573
--- a/src/main.zig
+++ b/src/main.zig
@@@ -475,9 -482,8 +475,8 @@@ pub fn main(init: std.process.Init) !vo
// Closing the learning loop: this session counts toward the next trial.
defer session_run.startBackgroundLearning(gpa, arena, startup_timing.shutdown_trace.at(io, "background-learning"), init.environ_map, &invocation_budget, !flags.no_telemetry_flag);
- // `graff` is the default session. TTY `graff repl` / `graff tui` open the Grok-style pager.
- // `graff acp` (acp.zig) is the same idea over Zed's stdio Agent Client Protocol. Both self-contained — each exits after.
- if (try session_run.runReplCommand(gpa, io, init.environ_map, &root, @import("bench_priors.zig").noteKeys(&keys), client, in, out, arena, flags) or try @import("acp.zig").runAcpCommand(gpa, io, init.environ_map, &root, @import("bench_priors.zig").noteKeys(&keys), client, in, out, arena, flags) or try @import("tui_launch.zig").maybeRun(gpa, io, init.environ_map, &root, @import("bench_priors.zig").noteKeys(&keys), client, arena, flags, json_mode, g_cwd_display)) return;
+ // Pager frontends sync their engine conversation back before root finalization; ACP remains self-contained.
- if (try session_run.runFrontendCommands(gpa, io, init.environ_map, &root, @import("bench_priors.zig").noteKeys(&keys), &client, in, out, arena, flags, json_mode, g_cwd_display, startup_timing.shutdown_trace.at(io, "final-save"))) return;
++ if (try session_run.runFrontendCommands(gpa, io, init.environ_map, &root, @import("bench_priors.zig").noteKeys(&keys), client, in, out, arena, flags, json_mode, g_cwd_display, startup_timing.shutdown_trace.at(io, "final-save"))) return;
// One-shot print mode: run the single prompt to completion, print the final text to stdout, exit.
if (flags.oneshot_prompt) |prompt_text| {
try session_run.runOneshotPrompt(gpa, io, arena, &root, @import("bench_priors.zig").noteKeys(&keys), &tracer, out, prompt_text); // one-shot exits before loop_ctx below — capture keys for sub-first routing here too
diff --cc src/session_run.zig
index 45597fb4,4f68e028..11d41c87
--- a/src/session_run.zig
+++ b/src/session_run.zig
@@@ -109,15 -116,26 +115,29 @@@ pub fn runReplCommand(gpa: Allocator, i
models_buf.appendSlice(mi.name) catch {};
}
try repl.runScripted(gpa, io, environ_map, in, out, &repl_ctx, repl_glue.replTurnCb, repl_glue.replModelCb, repl_glue.replCancelCb, root.provider.model, models_buf.items);
+ // Same stderr footer as `-p`, so evals can score the scripted REPL the
+ // same way. TTY `graff repl` is the TUI and keeps the restore tail clean.
+ pricing.printUsageFooter(io);
+ root.messages = try convo.cloneInto(root.arena);
return true;
}
+ pub fn runFrontendCommands(gpa: Allocator, io: Io, environ_map: anytype, root: *agent_mod.Agent, keys: *provider_mod.Keys, client: *std.http.Client, in: *Io.Reader, out: *Io.Writer, arena: Allocator, flags: args.Flags, json_mode: bool, cwd: []const u8, final_io: Io) !bool {
+ if (try runReplCommand(gpa, io, environ_map, root, keys, client, in, out, arena, flags)) {
+ try finalizeSession(gpa, final_io, arena, out, root, json_mode);
+ return true;
+ }
+ if (try @import("acp.zig").runAcpCommand(gpa, io, environ_map, root, keys, client, in, out, arena, flags)) return true;
+ if (try tui_launch.maybeRun(gpa, io, environ_map, root, keys, client, arena, flags, json_mode, cwd)) {
+ try finalizeSession(gpa, final_io, arena, out, root, json_mode);
+ return true;
+ }
+ return false;
+ }
+
/// One-shot print mode (`-p`/bare positional prompt): run the single prompt
/// to completion, print the final text to stdout, exit. Tool progress goes
-/// to stderr (say() with no out writer), streaming stays quiet, and the gate
+/// to stderr (say() with no out writer), paint stays quiet, and the gate
/// denies anything not pre-approved instead of prompting (there's no one to
/// ask). Moved out of main() verbatim (600-line goal); `root`/`tracer` are
/// already stable main()-owned storage by the time this runs.
diff --cc src/test_hooks.zig
index e5122cea,7fb12292..ff1fe6aa
--- a/src/test_hooks.zig
+++ b/src/test_hooks.zig
@@@ -343,6 -343,4 +345,7 @@@ test
_ = @import("channel_worker.zig");
_ = @import("session_wake.zig");
_ = @import("tui_acp.zig");
+ _ = @import("readline_paste.zig"); // #674: semantic paste spans (not reached via readline.zig's runtime import)
+ _ = @import("agent_request_scratch.zig"); // #693: split out of agent_request.zig (600-line ceiling)
++ _ = @import("commands_resume.zig"); // #697: --resume SOURCE --branch DEST parser
}