graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
feat(workflow): add a shared "context" slot for task/stage prompts
Rach Pradhanec8f1348398e
diff --git a/src/schema.zig b/src/schema.zigindex e5cec034..ac18bd29 100644--- a/src/schema.zig+++ b/src/schema.zig@@ -188,9 +188,9 @@ const agent_output_spec = ToolSpec{const workflow_spec = ToolSpec{.name = "workflow",- .desc = "Run a workflow of parallel subagents (dynamic workflows as data). Two shapes. PHASES (fan-out + synthesis): phases run sequentially; tasks inside a phase run in parallel as isolated subagents with no shared context, so every prompt must be self-contained; from phase 2 on, {{prev}} in a task prompt is replaced with the labeled results of the previous phase (appended if omitted); a phase may carry `when` to run only if a substring appears in {{prev}} (conditional / early-exit). Returns the final phase results. Use for audits, multi-perspective review, parallel research. Tasks may carry their own system_prompt to run prompt variants side by side; when the fleet is on, a phase with 2+ variants is auto-scored by an LLM judge and each variant's fitness feeds back to the fleet (a tournament doubles as a DGM scoring round). PIPELINE (no barrier): pass {pipeline:{items,stages}} instead to map each item through the stages independently — item A can reach stage 3 while item B is still on stage 1; {{item}} is the item and {{prev}} is this item's previous-stage result. Use for per-item work like transform/verify each file.",+ .desc = "Run a workflow of parallel subagents (dynamic workflows as data). Two shapes. PHASES (fan-out + synthesis): phases run sequentially; tasks inside a phase run in parallel as isolated subagents with no shared context, so every prompt must be self-contained; from phase 2 on, {{prev}} in a task prompt is replaced with the labeled results of the previous phase (appended if omitted); a phase may carry `when` to run only if a substring appears in {{prev}} (conditional / early-exit). Returns the final phase results. Use for audits, multi-perspective review, parallel research. Tasks may carry their own system_prompt to run prompt variants side by side; when the fleet is on, a phase with 2+ variants is auto-scored by an LLM judge and each variant's fitness feeds back to the fleet (a tournament doubles as a DGM scoring round). PIPELINE (no barrier): pass {pipeline:{items,stages}} instead to map each item through the stages independently — item A can reach stage 3 while item B is still on stage 1; {{item}} is the item and {{prev}} is this item's previous-stage result. Use for per-item work like transform/verify each file. Optional top-level \"context\" (also settable on the pipeline object): one shared string prepended once, plus a blank line, to every task/stage prompt before {{prev}} substitution, so repo/task boilerplate is stated once instead of repeated in every task.",.schema =- \\{"type": "object", "properties": {"phases": {"type": "array", "description": "Fan-out + synthesis mode (barrier between phases).", "items": {"type": "object", "properties": {"title": {"type": "string", "description": "Short phase label for logs"}, "when": {"type": "string", "description": "Optional (phase 2+): run this phase only if this substring appears (case-insensitive) in the previous phase's results — gate a synthesis/exit phase on a sentinel"}, "tasks": {"type": "array", "items": {"type": "object", "properties": {"description": {"type": "string", "description": "Short label, 3-5 words"}, "prompt": {"type": "string", "description": "Complete, self-contained task; may contain {{prev}}"}, "agent": {"type": "string", "description": "Optional: a named agent type (reviewer, researcher, implementer, skeptic, or a .harness/agents/ name)"}, "system_prompt": {"type": "string", "description": "Optional: replace this task's system prompt (overrides agent)"}, "isolation": {"type": "string", "enum": ["shared_cwd", "worktree"], "description": "Optional: \"worktree\" gives this task its own scratch git worktree (auto-removed if it finishes with no changes, kept + reported if it has any) instead of the shared working tree. Default shared_cwd, unless the chosen agent persona sets its own default."}, "isolation_fallback": {"type": "boolean", "description": "Optional: if isolation:\"worktree\" fails to set up, run in the shared working tree instead of failing this task. Default false."}}, "required": ["description", "prompt"]}}}, "required": ["tasks"]}}, "pipeline": {"type": "object", "description": "No-barrier mode: map each item through the stages independently.", "properties": {"items": {"type": "array", "items": {"type": "string"}, "description": "Things to process, one independent chain each (e.g. file paths)"}, "stages": {"type": "array", "items": {"type": "object", "properties": {"description": {"type": "string", "description": "Short stage label"}, "prompt": {"type": "string", "description": "Self-contained; {{item}} = the item, {{prev}} = this item's previous-stage result"}, "agent": {"type": "string", "description": "Optional named agent type"}, "system_prompt": {"type": "string", "description": "Optional: replace this stage's system prompt"}, "isolation": {"type": "string", "enum": ["shared_cwd", "worktree"], "description": "Optional: \"worktree\" gives this stage its own scratch git worktree (auto-removed if it finishes with no changes, kept + reported if it has any) instead of the shared working tree. Default shared_cwd, unless the chosen agent persona sets its own default."}, "isolation_fallback": {"type": "boolean", "description": "Optional: if isolation:\"worktree\" fails to set up, run in the shared working tree instead of failing this stage. Default false."}}, "required": ["description", "prompt"]}}}, "required": ["items", "stages"]}}}+ \\{"type": "object", "properties": {"phases": {"type": "array", "description": "Fan-out + synthesis mode (barrier between phases).", "items": {"type": "object", "properties": {"title": {"type": "string", "description": "Short phase label for logs"}, "when": {"type": "string", "description": "Optional (phase 2+): run this phase only if this substring appears (case-insensitive) in the previous phase's results — gate a synthesis/exit phase on a sentinel"}, "tasks": {"type": "array", "items": {"type": "object", "properties": {"description": {"type": "string", "description": "Short label, 3-5 words"}, "prompt": {"type": "string", "description": "Complete, self-contained task; may contain {{prev}}"}, "agent": {"type": "string", "description": "Optional: a named agent type (reviewer, researcher, implementer, skeptic, or a .harness/agents/ name)"}, "system_prompt": {"type": "string", "description": "Optional: replace this task's system prompt (overrides agent)"}, "isolation": {"type": "string", "enum": ["shared_cwd", "worktree"], "description": "Optional: \"worktree\" gives this task its own scratch git worktree (auto-removed if it finishes with no changes, kept + reported if it has any) instead of the shared working tree. Default shared_cwd, unless the chosen agent persona sets its own default."}, "isolation_fallback": {"type": "boolean", "description": "Optional: if isolation:\"worktree\" fails to set up, run in the shared working tree instead of failing this task. Default false."}}, "required": ["description", "prompt"]}}}, "required": ["tasks"]}}, "context": {"type": "string", "description": "Optional: prepended once, plus a blank line, to every task prompt before {{prev}} substitution."}, "pipeline": {"type": "object", "description": "No-barrier mode: map each item through the stages independently.", "properties": {"items": {"type": "array", "items": {"type": "string"}, "description": "Things to process, one independent chain each (e.g. file paths)"}, "stages": {"type": "array", "items": {"type": "object", "properties": {"description": {"type": "string", "description": "Short stage label"}, "prompt": {"type": "string", "description": "Self-contained; {{item}} = the item, {{prev}} = this item's previous-stage result"}, "agent": {"type": "string", "description": "Optional named agent type"}, "system_prompt": {"type": "string", "description": "Optional: replace this stage's system prompt"}, "isolation": {"type": "string", "enum": ["shared_cwd", "worktree"], "description": "Optional: \"worktree\" gives this stage its own scratch git worktree (auto-removed if it finishes with no changes, kept + reported if it has any) instead of the shared working tree. Default shared_cwd, unless the chosen agent persona sets its own default."}, "isolation_fallback": {"type": "boolean", "description": "Optional: if isolation:\"worktree\" fails to set up, run in the shared working tree instead of failing this stage. Default false."}}, "required": ["description", "prompt"]}}, "context": {"type": "string", "description": "Optional: prepended once, plus a blank line, to every stage prompt before {{item}}/{{prev}} substitution."}}, "required": ["items", "stages"]}}},};diff --git a/src/workflow.zig b/src/workflow.zigindex 95c8a2a9..e493e898 100644--- a/src/workflow.zig+++ b/src/workflow.zig@@ -89,6 +89,16 @@ fn replacePlaceholder(arena: Allocator, hay: []const u8, needle: []const u8, witreturn buf;}+/// Prepend the workflow/pipeline-level shared "context" (if any) to a raw+/// task or stage prompt, BEFORE {{prev}}/{{item}} substitution runs on the+/// result: context, blank line, raw prompt (U5). Absent/empty context+/// returns `raw` unchanged (no allocation), so the composed prompt stays+/// byte-identical to today's behavior when no context is set.+fn withContext(arena: Allocator, context: []const u8, raw: []const u8) ![]const u8 {+ if (context.len == 0) return raw;+ return std.fmt.allocPrint(arena, "{s}\n\n{s}", .{ context, raw });+}+/// Resolve a pipeline stage prompt for one item: substitute {{item}}, and from/// stage 2 {{prev}} = this item's bounded previous-stage result. Either is/// appended when its placeholder is omitted (mirrors phases-mode {{prev}}).@@ -154,6 +164,9 @@ fn runPipeline(ctx: ToolCtx, pv: Value) !ToolOutput {const stage_vals = stages_val.array.items;if (items.len == 0 or items.len > max_pipeline_items) return .{ .text = try std.fmt.allocPrint(gpa, "pipeline needs 1-{d} items", .{max_pipeline_items}), .is_error = true };if (stage_vals.len == 0 or stage_vals.len > max_pipeline_stages) return .{ .text = try std.fmt.allocPrint(gpa, "pipeline needs 1-{d} stages", .{max_pipeline_stages}), .is_error = true };+ // U5 — shared context slot: one string, set once on the pipeline object,+ // prepended to every stage prompt below (before {{item}}/{{prev}}).+ const context_str: []const u8 = if (pv.object.get("context")) |cv| (if (cv == .string) cv.string else "") else "";var arena_state = std.heap.ArenaAllocator.init(gpa);defer arena_state.deinit();@@ -167,7 +180,7 @@ fn runPipeline(ctx: ToolCtx, pv: Value) !ToolOutput {sp.label = if (so.get("description")) |d| (if (d == .string) d.string else "stage") else "stage";const raw = if (so.get("prompt")) |p| (if (p == .string) p.string else "") else "";if (raw.len == 0) return .{ .text = try gpa.dupe(u8, "each pipeline stage needs a non-empty \"prompt\""), .is_error = true };- sp.prompt = raw;+ sp.prompt = try withContext(arena, context_str, raw);sp.override = fleet.resolveOverride(so);const an = fleet.resolveNiche(so);sp.niche = if (an.len > 0) an else sp.label;@@ -234,6 +247,10 @@ pub fn execWorkflow(ctx: ToolCtx, input: Value) !ToolOutput {.text = try std.fmt.allocPrint(gpa, "workflow needs 1-{d} phases", .{max_workflow_phases}),.is_error = true,};+ // U5 — shared context slot: one string, set once on the workflow object,+ // prepended to every task prompt below (before {{prev}} substitution) so+ // repo/task boilerplate is paid for once instead of per task.+ const context_str: []const u8 = if (obj.get("context")) |cv| (if (cv == .string) cv.string else "") else "";// All intermediate strings live in this arena; the final result is// duped into gpa for the caller.@@ -301,15 +318,16 @@ pub fn execWorkflow(ctx: ToolCtx, input: Value) !ToolOutput {const raw = if (task.get("prompt")) |p| (if (p == .string) p.string else "") else "";if (raw.len == 0) return .{ .text = try gpa.dupe(u8, "each task needs a non-empty \"prompt\""), .is_error = true };rawp.* = raw;+ const based = try withContext(arena, context_str, raw);if (phase_no == 1) {- prompt.* = raw;- } else if (std.mem.indexOf(u8, raw, "{{prev}}") != null) {- const size = std.mem.replacementSize(u8, raw, "{{prev}}", prev_results);+ prompt.* = based;+ } else if (std.mem.indexOf(u8, based, "{{prev}}") != null) {+ const size = std.mem.replacementSize(u8, based, "{{prev}}", prev_results);const buf = try arena.alloc(u8, size);- _ = std.mem.replace(u8, raw, "{{prev}}", prev_results, buf);+ _ = std.mem.replace(u8, based, "{{prev}}", prev_results, buf);prompt.* = buf;} else {- prompt.* = try std.fmt.allocPrint(arena, "{s}\n\nResults from the previous workflow phase:\n\n{s}", .{ raw, prev_results });+ prompt.* = try std.fmt.allocPrint(arena, "{s}\n\nResults from the previous workflow phase:\n\n{s}", .{ based, prev_results });}}@@ -398,6 +416,24 @@ test "cappedPrevBody bounds a wide phase output, keeps head + inspect tail (#4)"try std.testing.expect(std.mem.indexOf(u8, capped, big) == null);}+test "withContext: set prepends context + blank line; absent/empty is byte-identical (U5)" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();++ // Context set: context, blank line, raw task — nothing else.+ const with = try withContext(a, "Repo: zig 0.16, 600-line file cap.", "Implement the thing.");+ try std.testing.expectEqualStrings("Repo: zig 0.16, 600-line file cap.\n\nImplement the thing.", with);++ // Context absent → byte-identical to the raw task (no stray blank line).+ const absent = try withContext(a, "", "Implement the thing.");+ try std.testing.expectEqualStrings("Implement the thing.", absent);++ // Context present but empty string behaves the same as absent.+ const empty = try withContext(a, "", "Do the other thing.");+ try std.testing.expectEqualStrings("Do the other thing.", empty);+}+test "gateAllows: empty when always runs, else case-insensitive substring (#5)" {try std.testing.expect(gateAllows("anything", "")); // no gate → always runtry std.testing.expect(gateAllows("found 3 ISSUES here", "issues")); // case-insensitive hit