graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
//! Cross-session presence registry (#469): disk + lifecycle for worktree_lease.
//! Each root session writes pid + start-id + worktree identity + goal into
//! ~/.graff/live; liveness is probed via proc_identity, never the file.
//! announce/retire/goal are wired into session_run + goal_flow. The first
//! shared-tree mutation against a live peer checkpoints once. No locking.
const std = @import("std");
const builtin = @import("builtin");
const Io = std.Io;
const Allocator = std.mem.Allocator;
const proc_identity = @import("proc_identity.zig");
const worktree_lease = @import("worktree_lease.zig");
const util = @import("util.zig");
const main_mod = @import("main.zig"); // `unattended`: one-shots join channel rooms at the tail, not the backlog
const unixMs = util.unixMs;
const presence_mutate = @import("presence_mutate.zig");
const no_local_tools = @import("no_local_tools.zig");
const presence_record = @import("presence_record.zig");
const Owner = worktree_lease.Owner;
pub const formatRecord = presence_record.formatRecord;
pub const parseRecord = presence_record.parseRecord;
pub const isSharedTreeGit = presence_mutate.isSharedTreeGit;
pub const isSharedTreeShell = presence_mutate.isSharedTreeShell;
pub const mutationTarget = presence_mutate.mutationTarget;
/// #851: checkpoint the worktree the command mutates. An isolated `git -C`
/// / leading `cd` target that resolves to a different identity is not the
/// caller's shared tree. Unresolved or in-tree paths stay gated.
pub fn sharedTreeGateApplies(gpa: Allocator, io: Io, arena: Allocator, cmd: []const u8) bool {
const mine = g_identity;
if (mine.len == 0) return true;
const target = presence_mutate.mutationTarget(cmd) orelse return true;
const at = worktree_lease.identityAt(gpa, io, arena, target);
if (at.id.len == 0) return true;
return std.mem.eql(u8, mine, at.id);
}
/// Per-user registry: <home>/.graff/live. Deliberately NOT the per-project .graff/sessions of session_index.zig — presence is device-local and keyed by worktree identity so two checkouts of one repo stay distinct (#320).
pub const registry_subdir = ".graff/live";
const max_peers = 16;
const record_max = 4096;
/// A directory listing's worth of records with each pid's OS probe aligned —
/// the exact pair duplicateOwner/ownerVerdict consume.
pub const Peers = struct {
records: []const Owner,
probes: []const proc_identity.Probe,
};
/// Read every record in `dir`, probe each pid, reap the provably dead, and
/// return the survivors. Best-effort throughout: an unreadable registry means
/// "no peers", never an error propagated into a tool call.
pub fn listPeers(io: Io, arena: Allocator, dir: Io.Dir) Peers {
return listPeersBounded(io, arena, dir, max_peers);
}
pub fn listPeersBounded(io: Io, arena: Allocator, dir: Io.Dir, limit: usize) Peers {
var records: std.ArrayList(Owner) = .empty;
var probes: std.ArrayList(proc_identity.Probe) = .empty;
var d = dir;
var it = d.iterate();
while (it.next(io) catch null) |entry| {
if (entry.kind != .file) continue;
if (!std.mem.endsWith(u8, entry.name, ".json")) continue;
if (records.items.len >= @min(limit, 128)) break;
const text = d.readFileAlloc(io, entry.name, arena, .limited(record_max)) catch continue;
const rec = parseRecord(arena, text) orelse continue;
const live = proc_identity.probe(io, rec.pid);
if (live == .gone) {
// Provably dead (#413): reap on read so the registry self-cleans
// even when the owner crashed without retire().
@import("subagent_activity.zig").cleanup(io, d, rec.pid, rec.start_id);
d.deleteFile(io, entry.name) catch {};
continue;
}
records.append(arena, rec) catch break;
probes.append(arena, live) catch break;
}
return .{ .records = records.items, .probes = probes.items };
}
/// One stable key per (pid, start identity) — what a checkpoint acknowledgment
/// is remembered by. Start identity, not pid alone: a reused pid must read as
/// a NEW peer, not an already-acked one (#320's whole point).
pub fn ackKey(rec: Owner) u64 {
var buf: [32]u8 = undefined;
const text = std.fmt.bufPrint(&buf, "{d}:{d}", .{ rec.pid, rec.start_id }) catch unreachable;
return std.hash.Wyhash.hash(0, text);
}
/// #1424: another live process recorded under this very session (a reloaded
/// or relaunched session whose earlier process has not exited) is the session
/// itself, not a peer to coordinate with. Pid alone missed it.
pub fn ownSessionRecord(rec: Owner, my_session: []const u8) bool {
return my_session.len > 0 and std.mem.eql(u8, rec.session_id, my_session);
}
/// The first live foreign co-owner of `my_identity` that has not been
/// acknowledged yet, if any. Pure: the probe results and the ack set are the
/// caller's, so tests need no processes and no filesystem.
pub fn unackedPeer(peers: Peers, my_identity: []const u8, my_pid: i32, my_session: []const u8, acked: []const u64) ?Owner {
for (peers.records, 0..) |rec, i| {
if (i >= peers.probes.len) break;
if (ownSessionRecord(rec, my_session)) continue;
switch (worktree_lease.ownerVerdict(rec, my_identity, my_pid, peers.probes[i])) {
.live_foreign, .live_unverified => {
const key = ackKey(rec);
var seen = false;
for (acked) |k| {
if (k == key) {
seen = true;
break;
}
}
if (!seen) return rec;
},
else => {},
}
}
return null;
}
// --- wired layer: one root session per process, so module state is the
// registry handle. announce is the ONLY writer of this state; retire/noteGoal/
// gateCheck no-op when it never ran (tests, subagents, homeless runs). ---
var g_dir: ?[]const u8 = null; // gpa-owned registry dir path
var g_identity: []const u8 = ""; // gpa-owned own worktree identity
var g_own_name: ?[]const u8 = null; // gpa-owned own record's file name
var g_session: []const u8 = ""; // gpa-owned session id, for rewrites
var g_title: []const u8 = ""; // gpa-owned visible title (#700)
var g_session_base: []const u8 = ""; // gpa-owned saved-session slug (#700)
var g_goal: []const u8 = ""; // last-known goal (session-arena-owned is fine: both outlive the session)
var g_self: proc_identity.Record = .{}; // own pid + start-id, settled by announce
var g_chan: ?[]const u8 = null; // gpa-owned channel file name (hash of g_identity)
var g_inbox_off: u64 = 0; // bytes of the shared channel already delivered
var g_device_off: u64 = 0; // bytes of the device-wide room already delivered
var g_tail_seeked: bool = false; // one-shots fast-forward both rooms exactly once (empty room at join must not re-skip later arrivals)
var g_acked: [max_peers]u64 = undefined;
var g_acked_len: usize = 0;
var g_activity: []const u8 = "waiting";
var g_activity_ms: i64 = 0;
pub fn noteActivity(io: Io, arena: Allocator, working: bool) void {
if (g_own_name == null) return;
g_activity = if (working) "working" else "waiting";
g_activity_ms = unixMs(io);
writeOwn(io, arena);
}
fn writeOwn(io: Io, arena: Allocator) void {
const dir_path = g_dir orelse return;
const name = g_own_name orelse return;
var owner = worktree_lease.selfOwner(io, g_identity, g_session, unixMs(io));
owner.goal = g_goal;
owner.activity = g_activity;
owner.last_activity_ms = g_activity_ms;
owner.title = g_title;
owner.session_base = g_session_base;
const text = formatRecord(arena, owner) catch return;
var dir = Io.Dir.cwd().openDir(io, dir_path, .{}) catch return;
defer dir.close(io);
dir.writeFile(io, .{ .sub_path = name, .data = text }) catch {};
}
/// Register this root session and report any LIVE co-owner already present —
/// the #469 "see each other before, not mid-collision" moment. Returns the
/// owner for the caller to surface (null = alone, or registry unavailable).
/// Never fails a session: every failure mode degrades to silence, because
/// presence must never be the reason graff did not start.
pub fn announce(io: Io, gpa: Allocator, arena: Allocator, home: []const u8, session_id: []const u8, goal: []const u8) ?Owner {
if (builtin.is_test) return null; // tests get the parameterized store, never the real registry
// -p / --lean one-shots (evals) live in sibling sandboxes inside the
// harness repo. git rev-parse walks up to that repo, so parallel -j N
// sessions share one identity and checkpoint every edit (ADR 0024 SWE).
if (no_local_tools.lean) return null;
if (home.len == 0) return null;
const dir_path = std.fmt.allocPrint(gpa, "{s}/{s}", .{ home, registry_subdir }) catch return null;
var dir_owned = true; // deinit frees only what the globals point at, so every bail before `g_dir` is set owns dir_path — it used to leak (#549)
defer if (dir_owned) gpa.free(dir_path);
Io.Dir.cwd().createDirPath(io, dir_path) catch return null;
const identity = worktree_lease.currentIdentity(gpa, io, arena);
if (identity.id.len == 0) return null;
const self = proc_identity.selfRecord(io);
var name_buf: [64]u8 = undefined;
const name = std.fmt.bufPrint(&name_buf, "{d}-{x}.json", .{ self.pid, self.start_id }) catch return null;
g_dir = dir_path;
dir_owned = false;
g_identity = gpa.dupe(u8, identity.id) catch return null;
g_session = gpa.dupe(u8, session_id) catch "";
g_goal = gpa.dupe(u8, goal) catch "";
g_self = self;
var chan_buf: [chan_name_max]u8 = undefined;
g_chan = gpa.dupe(u8, chanName(&chan_buf, g_identity)) catch null;
g_own_name = gpa.dupe(u8, name) catch return null;
// Peer check BEFORE writing our own record: the callout describes the tree as it was when we arrived.
const duplicate = blk: {
var dir = Io.Dir.cwd().openDir(io, dir_path, .{ .iterate = true }) catch break :blk null;
defer dir.close(io);
const peers = listPeers(io, arena, dir);
break :blk worktree_lease.duplicateOwner(peers.records, peers.probes, g_identity, self.pid);
};
writeOwn(io, arena);
return duplicate;
}
pub fn rebind(io: Io, gpa: Allocator, arena: Allocator) void {
if (builtin.is_test or g_own_name == null) return;
const identity = worktree_lease.currentIdentity(gpa, io, arena);
if (identity.id.len == 0 or std.mem.eql(u8, identity.id, g_identity)) return;
if (g_identity.len > 0) gpa.free(g_identity);
g_identity = gpa.dupe(u8, identity.id) catch return;
if (g_chan) |c| gpa.free(c);
var chan_buf: [chan_name_max]u8 = undefined;
g_chan = gpa.dupe(u8, chanName(&chan_buf, g_identity)) catch null;
g_inbox_off = 0;
if (g_dir) |dir_path| if (g_chan) |chan| {
var dir = Io.Dir.cwd().openDir(io, dir_path, .{}) catch {
writeOwn(io, arena);
return;
};
defer dir.close(io);
g_inbox_off = presence_chan.roomSize(io, dir, chan);
};
writeOwn(io, arena);
}
/// retire is hygiene; deinit frees gpa-owned globals for the exit leak check.
pub fn deinit(gpa: Allocator) void {
if (g_dir) |p| gpa.free(p);
if (g_identity.len > 0) gpa.free(g_identity);
if (g_session.len > 0) gpa.free(g_session);
if (g_title.len > 0) gpa.free(g_title);
if (g_session_base.len > 0) gpa.free(g_session_base);
if (g_goal.len > 0) gpa.free(g_goal);
if (g_own_name) |n| gpa.free(n);
if (g_chan) |c| gpa.free(c);
g_dir = null;
g_identity = "";
g_session = "";
g_title = "";
g_session_base = "";
g_goal = "";
g_own_name = null;
g_chan = null;
g_self = .{};
g_inbox_off = 0;
g_device_off = 0;
g_tail_seeked = false;
g_acked_len = 0;
g_activity = "waiting";
g_activity_ms = 0;
}
pub fn retire(io: Io) void {
const dir_path = g_dir orelse return;
const name = g_own_name orelse return;
var dir = Io.Dir.cwd().openDir(io, dir_path, .{}) catch return;
defer dir.close(io);
@import("subagent_activity.zig").cleanup(io, dir, g_self.pid, g_self.start_id);
dir.deleteFile(io, name) catch {};
// The shared channel is NOT ours to delete: co-resident sessions may still be mid-drain, and its bytes are the room's history.
}
/// Refresh our record's goal — the coordination payload a peer's checkpoint
/// prints. Only the goal_flow setters call this; a cleared goal keeps the
/// last-known text rather than going blank mid-supersession. The goal is
/// re-duped onto the gpa (freeing the previous) so g_goal stays gpa-owned —
/// deinit frees it, and the caller's slice is usually session-arena memory.
pub fn noteGoal(io: Io, gpa: Allocator, arena: Allocator, goal: []const u8) void {
if (g_own_name == null) return; // never announced (tests, subagents): io may be undefined — touch nothing
const owned = gpa.dupe(u8, goal) catch return;
if (g_goal.len > 0) gpa.free(g_goal);
g_goal = owned;
writeOwn(io, arena);
}
/// Refresh the visible title and saved-session base (#700). Empty title is
/// honest (pre-title sessions). session_base is the durable file slug; when
/// set it also becomes the live session_id so a rename is addressable.
pub fn noteLabels(io: Io, gpa: Allocator, arena: Allocator, title: []const u8, session_base: []const u8) void {
if (g_own_name == null) return;
const owned_title = gpa.dupe(u8, title) catch return;
const owned_base = gpa.dupe(u8, session_base) catch {
gpa.free(owned_title);
return;
};
if (g_title.len > 0) gpa.free(g_title);
if (g_session_base.len > 0) gpa.free(g_session_base);
g_title = owned_title;
g_session_base = owned_base;
if (session_base.len > 0) {
const owned_session = gpa.dupe(u8, session_base) catch {
writeOwn(io, arena);
return;
};
if (g_session.len > 0) gpa.free(g_session);
g_session = owned_session;
}
writeOwn(io, arena);
}
pub fn noteLabelsFrom(io: Io, gpa: Allocator, arena: Allocator, title: ?[]const u8, session_base: []const u8) void {
noteLabels(io, gpa, arena, title orelse "", session_base);
}
/// The tool-gate half (#469): null = no unacknowledged live co-owner, else the
/// checkpoint text the model sees as its (un-executed) tool result. Seeing a
/// peer ACKs it, so the re-issued command runs and one process checkpoints at
/// most once per peer — awareness is the goal, not a tollbooth.
pub fn gateCheck(io: Io, arena: Allocator) ?[]const u8 {
return gateCheckIdentity(io, arena, g_identity);
}
pub fn hasCheckpointContext() bool {
return !no_local_tools.lean and g_dir != null and g_identity.len > 0;
}
pub fn gateCheckIdentity(io: Io, arena: Allocator, identity: []const u8) ?[]const u8 {
if (!hasCheckpointContext()) return null;
const dir_path = g_dir.?;
var dir = Io.Dir.cwd().openDir(io, dir_path, .{ .iterate = true }) catch return null;
defer dir.close(io);
const peers = listPeers(io, arena, dir);
const acked = @import("presence_ack.zig").ackLive(peers, identity, proc_identity.selfPid(), g_session, &g_acked, &g_acked_len);
const peer = acked.first orelse return null;
const warning = worktree_lease.duplicateOwnerWarning(arena, peer, unixMs(io) - peer.last_seen_ms);
const others = if (acked.fresh > 1) std.fmt.allocPrint(arena, " and {d} other live peer(s)", .{acked.fresh - 1}) catch "" else "";
return std.fmt.allocPrint(arena, "{s}shared-tree checkpoint: the action was NOT performed. Re-issue the identical call to proceed — this one checkpoint covers every peer live now; only a peer that starts later fires another — coordinate first via the peer_message tool (session \"{s}\"{s}), or keep your edits disjoint from theirs.", .{ warning, peer.session_id, others }) catch warning;
}
// The channel's wire format (Message, chanName, postMessage, readNewMessages)
// lives in presence_chan.zig — split out under the 600-line ceiling. This file
// keeps the wired layer: which dir/name/offset a LIVE session uses.
const presence_chan = @import("presence_chan.zig");
pub const Message = presence_chan.Message;
const chanName = presence_chan.chanName;
const isOwn = presence_chan.isOwn;
const chan_name_max = presence_chan.chan_name_max;
/// The live co-owners of MY worktree: every registry record whose verdict is
/// live_foreign/live_unverified. The sender's target list for peer_message
/// and /tell; already probed, with the provably dead reaped.
pub fn liveTreePeers(io: Io, arena: Allocator) []const Owner {
const dir_path = g_dir orelse return &.{};
if (g_identity.len == 0) return &.{};
var dir = Io.Dir.cwd().openDir(io, dir_path, .{ .iterate = true }) catch return &.{};
defer dir.close(io);
const peers = listPeers(io, arena, dir);
var live: std.ArrayList(Owner) = .empty;
for (peers.records, 0..) |rec, i| {
if (i >= peers.probes.len) break;
if (ownSessionRecord(rec, g_session)) continue;
switch (worktree_lease.ownerVerdict(rec, g_identity, g_self.pid, peers.probes[i])) {
.live_foreign, .live_unverified => live.append(arena, rec) catch break,
else => {},
}
}
return live.items;
}
/// Post to the shared worktree channel, stamped with OUR record. `to` names
/// the intended recipient (session substring or pid) and is metadata only —
/// every co-resident session hears the line. false when this session never
/// announced (tests, subagents) or the write failed.
pub fn postTo(io: Io, arena: Allocator, text: []const u8, to: []const u8) bool {
const dir_path = g_dir orelse return false;
const chan = g_chan orelse return false;
if (g_self.pid == 0) return false;
var dir = Io.Dir.cwd().openDir(io, dir_path, .{}) catch return false;
defer dir.close(io);
return presence_chan.postMessage(io, arena, dir, chan, .{
.from_pid = g_self.pid,
.from_start = g_self.start_id,
.from_session = g_session,
.from_goal = g_goal,
.to = to,
.ts_ms = unixMs(io),
.text = text,
});
}
/// Drain the shared channel: every complete message since our last drain,
// minus our own echo. Empty when unannounced or caught up. A session that
/// joins late hears the whole backlog once — but peer_channel.deliverInbound
/// tail-caps that first drain (backlog_tail_max) with an omitted-count marker.
/// EXCEPTION: a -p one-shot joins, works, and exits
/// inside a minute — the backlog is context it cannot use (measured ~4k
/// tokens of stale chatter injected on the first step of every benchmark
/// one-shot — and it made ephemeral workers ANSWER old messages, burning
/// turns on coordination theater). Unattended sessions start at the tail:
/// they hear what arrives while they run, not what predates them.
pub fn drainChannel(io: Io, arena: Allocator) []const Message {
const dir_path = g_dir orelse return &.{};
const chan = g_chan orelse return &.{};
if (g_self.pid == 0) return &.{};
var dir = Io.Dir.cwd().openDir(io, dir_path, .{}) catch return &.{};
defer dir.close(io);
if (main_mod.unattended and !g_tail_seeked) {
g_tail_seeked = true;
g_inbox_off = presence_chan.roomSize(io, dir, chan);
// The device room too: deliverInbound always drains the worktree
// channel first, so one fast-forward covers both rooms.
g_device_off = presence_chan.roomSize(io, dir, device_room);
}
const raw = presence_chan.readNewMessages(io, arena, dir, chan, &g_inbox_off);
var out: std.ArrayList(Message) = .empty;
for (raw) |m| {
if (!isOwn(m, g_self.pid, g_self.start_id)) out.append(arena, m) catch break;
}
return out.items;
}
pub fn registryPath() ?[]const u8 {
return g_dir;
}
pub fn ownSession() []const u8 {
return g_session;
}
pub fn ownIdentity() []const u8 {
return g_identity;
}
// --- the device-wide room (#469: "or just the same device"): one more log
// every announced session drains, regardless of worktree. Worktree rooms stay
// the default for coordination chatter; this one carries /tell all broadcasts
// and messages addressed to a session in another folder. ---
pub const device_room = "chan-all.jsonl";
/// Byte cursors into the worktree and device JSONL rooms. Persist these on
/// the session file so `/resume` continues instead of re-hearing the tail
/// (ADR 0014). Codex keeps a thread cursor; Claude persists the inbox.
pub const RoomCursor = struct { chan: u64, device: u64 };
pub fn roomCursor() RoomCursor {
return .{ .chan = g_inbox_off, .device = g_device_off };
}
pub fn adoptRoomCursor(c: RoomCursor) void {
g_inbox_off = c.chan;
g_device_off = c.device;
g_tail_seeked = true;
}
/// Join at the live tail — same as a `-p` one-shot. Used when a resumed
/// session has no saved cursor (legacy file) so we do not replay stale room.
pub fn seekRoomsToTail(io: Io, arena: Allocator) void {
const dir_path = g_dir orelse {
g_tail_seeked = true;
return;
};
var dir = Io.Dir.cwd().openDir(io, dir_path, .{}) catch {
g_tail_seeked = true;
return;
};
defer dir.close(io);
_ = arena;
if (g_chan) |chan| g_inbox_off = presence_chan.roomSize(io, dir, chan);
g_device_off = presence_chan.roomSize(io, dir, device_room);
g_tail_seeked = true;
}
pub fn resetRoomCursorForTest() void {
g_inbox_off = 0;
g_device_off = 0;
g_tail_seeked = false;
}
/// Tests only. `announce` is a no-op under `is_test`; this wires the same
/// process-local handle so `postTo` / `postToDevice` can write a tmp registry.
/// Caller owns the slices — do not `deinit` after this; call `unbindForTest`.
pub fn bindForTest(dir_path: []const u8, identity: []const u8, session: []const u8, self: proc_identity.Record) void {
g_dir = dir_path;
g_identity = identity;
g_session = session;
g_self = self;
}
pub fn unbindForTest() void {
g_acked_len = 0;
g_dir = null;
g_identity = "";
g_session = "";
g_self = .{};
resetRoomCursorForTest();
}
/// Every live session on this device, any worktree (self excluded) — the
/// /tell target list. Like liveTreePeers but identity-blind.
pub fn liveAllPeers(io: Io, arena: Allocator) []const Owner {
const dir_path = g_dir orelse return &.{};
var dir = Io.Dir.cwd().openDir(io, dir_path, .{ .iterate = true }) catch return &.{};
defer dir.close(io);
const peers = listPeers(io, arena, dir);
var live: std.ArrayList(Owner) = .empty;
for (peers.records, 0..) |rec, i| {
if (i >= peers.probes.len) break;
if (rec.pid == g_self.pid or ownSessionRecord(rec, g_session)) continue;
switch (peers.probes[i]) {
.gone => {},
else => live.append(arena, rec) catch break,
}
}
return live.items;
}
/// Post to the device-wide room. Delivery there is addressed-only: heard when
/// `to` names the session or the USER posted it (/tell sets from_user).
pub fn postToDevice(io: Io, arena: Allocator, text: []const u8, to: []const u8, from_user: bool) bool {
const dir_path = g_dir orelse return false;
if (g_self.pid == 0) return false;
var dir = Io.Dir.cwd().openDir(io, dir_path, .{}) catch return false;
defer dir.close(io);
return presence_chan.postMessage(io, arena, dir, device_room, .{
.from_pid = g_self.pid,
.from_start = g_self.start_id,
.from_session = g_session,
.from_goal = g_goal,
.to = to,
.ts_ms = unixMs(io),
.text = text,
.from_user = from_user,
});
}
/// Drain the device-wide room (own echo skipped), same offset discipline as
/// the worktree channel.
pub fn drainDevice(io: Io, arena: Allocator) []const Message {
const dir_path = g_dir orelse return &.{};
if (g_self.pid == 0) return &.{};
var dir = Io.Dir.cwd().openDir(io, dir_path, .{}) catch return &.{};
defer dir.close(io);
const raw = presence_chan.readNewMessages(io, arena, dir, device_room, &g_device_off);
var out: std.ArrayList(Message) = .empty;
for (raw) |m| {
if (!isOwn(m, g_self.pid, g_self.start_id)) out.append(arena, m) catch break;
}
return out.items;
}
test {
_ = @import("presence_tests.zig");
}
test "presence activity records turn boundaries but not metadata rewrites" {
const io = std.testing.io;
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_state.deinit();
const arena = arena_state.allocator();
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var path_buf: [std.fs.max_path_bytes]u8 = undefined;
const path_len = try tmp.dir.realPath(io, &path_buf);
bindForTest(path_buf[0..path_len], "/test/.git", "activity-test", proc_identity.selfRecord(io));
g_own_name = "activity.json";
defer {
g_own_name = null;
g_activity = "waiting";
g_activity_ms = 0;
unbindForTest();
}
// Startup/metadata refresh is not evidence of a turn.
writeOwn(io, arena);
const initial = parseRecord(arena, try tmp.dir.readFileAlloc(io, "activity.json", arena, .limited(record_max))).?;
try std.testing.expectEqual(@as(i64, 0), initial.last_activity_ms);
for ([_]bool{ true, false }) |working| {
noteActivity(io, arena, working);
const active = parseRecord(arena, try tmp.dir.readFileAlloc(io, "activity.json", arena, .limited(record_max))).?;
try std.testing.expect(active.last_activity_ms > 0);
try std.testing.expectEqualStrings(if (working) "working" else "waiting", active.activity);
// Make the distinction deterministic even on a same-millisecond rewrite.
g_activity_ms = 1234;
writeOwn(io, arena);
const refreshed = parseRecord(arena, try tmp.dir.readFileAlloc(io, "activity.json", arena, .limited(record_max))).?;
try std.testing.expectEqual(@as(i64, 1234), refreshed.last_activity_ms);
try std.testing.expect(refreshed.last_seen_ms > refreshed.last_activity_ms);
try std.testing.expectEqualStrings(active.activity, refreshed.activity);
}
}