Skip to content

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

Python

feat(ultracode): fixed catalog of five orchestration shapes (#293)

Rach Pradhan3585446f7081
diff --git a/src/pickers.zig b/src/pickers.zig
index 2eff670c..aedffc8c 100644
--- a/src/pickers.zig
+++ b/src/pickers.zig
@@ -25,6 +25,7 @@ const Keys = provider_mod.Keys;
const command_catalog = @import("command_catalog.zig");
const picker_auth = @import("picker_auth.zig");
const util = @import("util.zig");
+const shapes = @import("shapes.zig");
/// Case-insensitive subsequence match (fzf-style): every char of `needle`
/// appears in `hay` in order, gaps allowed — so "gpt5.5" matches "gpt-5.5".
@@ -402,28 +403,32 @@ pub const UltracodeMessage = struct {
explicit: bool,
};
-const ultracode_explicit_note =
+const ultracode_explicit_head =
\\[harness note: the user invoked the "ultracode" codeword, opting
\\this turn into multi-agent orchestration. Fulfill the request with
- \\the workflow tool: decompose it into sequential phases of parallel
- \\subagents — fan out for coverage first, then a synthesis phase.
+ \\the workflow tool.
\\Tell code-exploration subagents to go through the repo with the
\\codedb tool (search / symbol / callers / outline / context) before
\\reaching for bash grep — it is indexed and structural.
\\Use the workflow even if you could do the work solo; skip it only
- \\if the message needs a purely conversational reply.]
+ \\if the message needs a purely conversational reply.
;
-const ultracode_persistent_note =
+const ultracode_persistent_head =
\\[harness note: ultracode mode is enabled for this session. Use the
- \\workflow tool for coding tasks: decompose the work into sequential
- \\phases with parallel subagents for exploration/review where helpful,
- \\then synthesize and implement. Tell code-exploration subagents to go
+ \\workflow tool for coding tasks. Tell code-exploration subagents to go
\\through the repo with the codedb tool (search / symbol / callers /
\\outline / context) before reaching for bash grep — it is indexed and
- \\structural.]
+ \\structural.
;
+// Both steering notes carry the shape catalog (#293), so an ultracode turn
+// instantiates one of the five known shapes under canonical slot names instead
+// of inventing a fresh structure each run — scores from differently-shaped runs
+// are not comparable, which is what kept the fleet from accruing real fitness.
+const ultracode_explicit_note = ultracode_explicit_head ++ "\n\n" ++ shapes.shape_catalog_note ++ "]";
+const ultracode_persistent_note = ultracode_persistent_head ++ "\n\n" ++ shapes.shape_catalog_note ++ "]";
+
/// `raw` is what the user actually typed this turn; `msg` is the assembled
/// turn message (goal/eval/loop/plan notes may already be appended). The
/// explicit-codeword scan runs ONLY on `raw`: harness-assembled notes replay
@@ -578,4 +583,14 @@ test "applyUltracodeSteering handles explicit and persistent modes" {
try std.testing.expect(explicit.explicit);
try std.testing.expect(std.mem.indexOf(u8, explicit.text, "user invoked the \"ultracode\" codeword") != null);
try std.testing.expect(std.mem.indexOf(u8, explicit.text, "ultracode mode is enabled") == null);
+
+ // #293: BOTH steering paths must carry the shape catalog — a turn steered
+ // into orchestration without it is exactly the free-form case the catalog
+ // exists to remove. Check a slot word and the closing bracket, so a broken
+ // concatenation (dropped catalog, or a note left unterminated) fails here.
+ for ([_][]const u8{ explicit.text, persistent.text }) |steered| {
+ try std.testing.expect(std.mem.indexOf(u8, steered, "Pick ONE shape") != null);
+ try std.testing.expect(std.mem.indexOf(u8, steered, "synthesize") != null);
+ try std.testing.expect(std.mem.endsWith(u8, steered, "]"));
+ }
}
diff --git a/src/shapes.zig b/src/shapes.zig
new file mode 100644
index 00000000..755b4c51
--- /dev/null
+++ b/src/shapes.zig
@@ -0,0 +1,152 @@
+//! Ultracode orchestration shapes (#293): the fixed catalog of default
+//! multi-agent workflow shapes, the canonical slot vocabulary those shapes
+//! name their phases with, and the steering text appended to an ultracode
+//! turn.
+//!
+//! Lives outside pickers.zig (which owns the steering call site) for two
+//! reasons: pickers.zig is at the 600-line cap, and the slot vocabulary is
+//! not a picker concern — it is the third component of the MAP-Elites cell
+//! key (#290). Keeping this a std-only leaf module, prompts.zig-style, lets
+//! both the steering path and the fleet scoring path import it with no cycle.
+//!
+//! Why a fixed catalog at all: left to itself the model authors a different
+//! workflow shape every run, so two scores for the same persona are not
+//! comparable — the structure varied underneath them. A closed catalog with
+//! named slots makes "reviewer prompt, mid tier, verify slot" a cell that
+//! accrues real fitness across runs.
+
+const std = @import("std");
+
+/// Canonical phase/stage labels an ultracode workflow names its slots with.
+///
+/// This is the closed vocabulary behind the third component of the composite
+/// niche key (#290). A prompt variant's fitness is only comparable to another
+/// variant scored in the SAME slot, so the set has to be stable — free-text
+/// phase titles would mint a fresh cell per run and nothing would ever
+/// accumulate enough samples to promote.
+pub const canonical_slots = [_][]const u8{
+ // A — review/audit
+ "find",
+ "verify",
+ "synthesize",
+ // B — research/understand
+ "sweep",
+ // C — design/solve
+ "variants",
+ "build",
+ // D — migration/mechanical
+ "transform",
+ // E — feature
+ "scope",
+ "implement",
+ "review",
+};
+
+/// The first alphanumeric token of `label`, lowercased into `buf`.
+/// Returns an empty slice when the label has no leading word.
+fn firstWord(buf: []u8, label: []const u8) []const u8 {
+ var start: usize = 0;
+ while (start < label.len and !std.ascii.isAlphanumeric(label[start])) start += 1;
+ var n: usize = 0;
+ var i = start;
+ while (i < label.len and std.ascii.isAlphanumeric(label[i]) and n < buf.len) : (i += 1) {
+ buf[n] = std.ascii.toLower(label[i]);
+ n += 1;
+ }
+ return buf[0..n];
+}
+
+/// Normalize a free-text phase title / pipeline stage label to a canonical
+/// slot, or "" when it matches none.
+///
+/// Matching is on the label's FIRST WORD only, case-insensitively. That is
+/// deliberately narrow: a substring rule would make "review the findings"
+/// ambiguous between `review` and `find`, and which one won would depend on
+/// array order rather than on what the phase actually does. First-word
+/// matching keeps natural titles working ("find security bugs" -> `find`,
+/// "synthesize the results" -> `synthesize`) with no ordering hazard.
+///
+/// An unrecognized label maps to "" (uncelled) rather than minting a new cell
+/// — an off-vocabulary phase still runs, it just does not accrue fitness.
+pub fn canonicalSlot(label: []const u8) []const u8 {
+ var buf: [32]u8 = undefined;
+ const word = firstWord(&buf, label);
+ if (word.len == 0) return "";
+ for (canonical_slots) |slot| {
+ if (std.mem.eql(u8, slot, word)) return slot;
+ }
+ return "";
+}
+
+/// The shape catalog appended to every ultracode turn. Kept tight on purpose:
+/// this rides along on each turn of an opt-in mode, so it should buy structure
+/// per token rather than restate what the workflow tool's schema already says.
+pub const shape_catalog_note =
+ \\Pick ONE shape below and instantiate it with the workflow tool. Title each
+ \\phase with its slot word exactly — the fleet keys a variant's fitness on
+ \\that slot, and an off-vocabulary title makes the round unscoreable.
+ \\
+ \\A review/audit — there is a diff, or the ask is review/audit/find bugs:
+ \\ find one reviewer per dimension (correctness, security, perf, tests), parallel
+ \\ verify one skeptic per finding, told to REFUTE it; gate with when:"FINDING"
+ \\ synthesize one task; findings ranked most-severe first
+ \\B research/understand — the ask is a question about how something works:
+ \\ sweep researchers each searching a DIFFERENT way (by symbol, by callers,
+ \\ by git history, by tests); tell them not to duplicate each other
+ \\ synthesize one map
+ \\C design/solve — open-ended "how should we…":
+ \\ variants N implementers, same task, a DIFFERENT system_prompt each
+ \\ (MVP-first / risk-first / perf-first); 2+ variants are auto-judged
+ \\ build synthesize from the winner, grafting the runners-up's best ideas
+ \\D migration/mechanical — the same edit across many files:
+ \\ use pipeline, NOT phases: {"pipeline":{"items":[…],"stages":[transform, verify]}}
+ \\ each item flows independently, so no item waits on any other
+ \\E feature — build something new:
+ \\ scope → implement → review
+ \\
+ \\Scale to the ask: "find bugs" is 3 finders + 1 verify; "thoroughly audit" is
+ \\6 finders + 3-vote adversarial verify + synthesize. Use phases only when a
+ \\phase genuinely needs ALL of the previous one; per-item work belongs in
+ \\pipeline. Give file-editing tasks isolation:"worktree" so they cannot collide.
+;
+
+test "canonicalSlot: exact, first-word, and miss" {
+ // Bare slot words map to themselves.
+ for (canonical_slots) |slot| try std.testing.expectEqualStrings(slot, canonicalSlot(slot));
+ // Natural titles match on their first word.
+ try std.testing.expectEqualStrings("find", canonicalSlot("find security bugs"));
+ try std.testing.expectEqualStrings("synthesize", canonicalSlot("Synthesize the results"));
+ try std.testing.expectEqualStrings("verify", canonicalSlot("VERIFY each finding"));
+ // Leading punctuation/whitespace is skipped.
+ try std.testing.expectEqualStrings("sweep", canonicalSlot(" - sweep the repo"));
+ // The ordering hazard a substring rule would have: this is `review`, not `find`.
+ try std.testing.expectEqualStrings("review", canonicalSlot("review the findings"));
+ // Off-vocabulary titles are uncelled rather than minting a new cell.
+ try std.testing.expectEqualStrings("", canonicalSlot("code review"));
+ try std.testing.expectEqualStrings("", canonicalSlot("ponder"));
+ try std.testing.expectEqualStrings("", canonicalSlot(""));
+ try std.testing.expectEqualStrings("", canonicalSlot("---"));
+}
+
+test "canonicalSlot: a word longer than the scratch buffer cannot match or overflow" {
+ const long = "a" ** 200;
+ try std.testing.expectEqualStrings("", canonicalSlot(long));
+ // A canonical slot followed by a very long tail still matches on word one.
+ try std.testing.expectEqualStrings("find", canonicalSlot("find " ++ long));
+}
+
+test "shape catalog names every canonical slot it depends on" {
+ // Guards the seam with #290: if a slot is added to the vocabulary but the
+ // catalog never tells the model to use it, that cell can never be filled.
+ for (canonical_slots) |slot| {
+ try std.testing.expect(std.mem.indexOf(u8, shape_catalog_note, slot) != null);
+ }
+}
+
+test "shape catalog covers all five shapes and stays within a sane token budget" {
+ for ([_][]const u8{ "A review/audit", "B research/understand", "C design/solve", "D migration/mechanical", "E feature" }) |shape| {
+ try std.testing.expect(std.mem.indexOf(u8, shape_catalog_note, shape) != null);
+ }
+ // It rides on every ultracode turn; keep it from silently ballooning.
+ try std.testing.expect(shape_catalog_note.len < 2048);
+}