Skip to content

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

Python

perf(plugins): one readdir per folder and named skill loads from memory

Cursor Agenta88be193c20e
diff --git a/docs/adr/0007-plugins-are-read-in-place.md b/docs/adr/0007-plugins-are-read-in-place.md
index 1d16e566..89b2acc9 100644
--- a/docs/adr/0007-plugins-are-read-in-place.md
+++ b/docs/adr/0007-plugins-are-read-in-place.md
@@ -47,4 +47,8 @@ config.
- A plugin installed after first-run adopt is visible next session.
- Project plugin MCP is not more trusted than project `.mcp.json`; it is less.
+- Discover classifies with one readdir per visited directory (not a stat per
+ marker) and stores skill/agent dirs on the plugin so later merges do not
+ inspect again. A named `skill` load hits the session catalog; a miss or a
+ list still rescans.
- Revisit if a user needs to disable one plugin without `GRAFF_NO_PLUGINS`.
diff --git a/src/fleet.zig b/src/fleet.zig
index 2fc149a6..cb5e19ec 100644
--- a/src/fleet.zig
+++ b/src/fleet.zig
@@ -128,12 +128,12 @@ pub fn loadAgentTypes(io: Io, arena: Allocator, home: ?[]const u8) []const Agent
}
const plugs = plugins.discover(io, arena, home, Io.Dir.cwd());
for (plugs) |p| {
- if (!p.personal or !p.agents) continue;
- loadAgentDir(io, arena, &list, std.fmt.allocPrint(arena, "{s}/agents", .{p.path}) catch continue);
+ if (!p.personal) continue;
+ for (p.agent_dirs) |d| loadAgentDir(io, arena, &list, d);
}
for (plugs) |p| {
- if (p.personal or !p.agents) continue;
- loadAgentDir(io, arena, &list, std.fmt.allocPrint(arena, "{s}/agents", .{p.path}) catch continue);
+ if (p.personal) continue;
+ for (p.agent_dirs) |d| loadAgentDir(io, arena, &list, d);
}
loadAgentDir(io, arena, &list, agents_dir);
// A verified learning ref is the highest-precedence project policy. The
diff --git a/src/plugin_layout.zig b/src/plugin_layout.zig
index b0243dd9..423a592d 100644
--- a/src/plugin_layout.zig
+++ b/src/plugin_layout.zig
@@ -30,6 +30,7 @@ pub const RootSkill = struct {
};
pub const Layout = struct {
+ name: []const u8 = "",
skill_dirs: []const []const u8 = &.{},
agent_dirs: []const []const u8 = &.{},
root_skill: ?RootSkill = null,
@@ -45,12 +46,6 @@ fn join(arena: Allocator, a: []const u8, b: []const u8) []const u8 {
return std.fmt.allocPrint(arena, "{s}/{s}", .{ a, b }) catch "";
}
-fn existsJoin(io: Io, a: []const u8, b: []const u8) bool {
- var buf: [std.fs.max_path_bytes]u8 = undefined;
- const p = std.fmt.bufPrint(&buf, "{s}/{s}", .{ a, b }) catch return false;
- return exists(io, p);
-}
-
/// Refuse `..` so a manifest cannot walk out of the plugin tree.
fn resolveRel(arena: Allocator, plugin_path: []const u8, rel: []const u8) ?[]const u8 {
var r = std.mem.trim(u8, rel, " \t");
@@ -163,29 +158,68 @@ fn ingestMcpField(io: Io, arena: Allocator, servers: *std.json.ObjectMap, found:
}
}
-/// Claude defaults plus any extra dirs the manifest names.
+fn readOneManifest(io: Io, arena: Allocator, plugin_path: []const u8, rel: []const u8) ?Value {
+ const text = Io.Dir.cwd().readFileAlloc(io, join(arena, plugin_path, rel), arena, .limited(64 * 1024)) catch return null;
+ const v = std.json.parseFromSliceLeaky(Value, arena, text, .{ .allocate = .alloc_always }) catch return null;
+ return if (v == .object) v else null;
+}
+
+/// Claude defaults plus any extra dirs the manifest names. One readdir of the
+/// plugin root, then at most one manifest file — not a stat per marker.
pub fn inspect(io: Io, arena: Allocator, plugin_path: []const u8) Layout {
var skills: std.ArrayList([]const u8) = .empty;
var agents: std.ArrayList([]const u8) = .empty;
- appendDir(arena, &skills, io, join(arena, plugin_path, "skills"));
- const commands = join(arena, plugin_path, "commands");
- const has_commands = exists(io, commands);
- if (has_commands) appendDir(arena, &skills, io, commands);
- appendDir(arena, &agents, io, join(arena, plugin_path, "agents"));
+ var has_commands = false;
+ var has_mcp = false;
+ var saw_root_skill = false;
+ var saw_claude = false;
+ var saw_cursor = false;
+ var saw_grok = false;
+ var saw_plugin_json = false;
+ var dir = Io.Dir.cwd().openDir(io, plugin_path, .{ .iterate = true }) catch return .{
+ .name = std.fs.path.basename(plugin_path),
+ };
+ defer dir.close(io);
+ var it = dir.iterate();
+ while (it.next(io) catch null) |entry| {
+ if (std.mem.eql(u8, entry.name, "skills")) {
+ skills.append(arena, join(arena, plugin_path, "skills")) catch {};
+ } else if (std.mem.eql(u8, entry.name, "commands")) {
+ has_commands = true;
+ skills.append(arena, join(arena, plugin_path, "commands")) catch {};
+ } else if (std.mem.eql(u8, entry.name, "agents")) {
+ agents.append(arena, join(arena, plugin_path, "agents")) catch {};
+ } else if (std.mem.eql(u8, entry.name, "SKILL.md")) {
+ saw_root_skill = true;
+ } else if (std.mem.eql(u8, entry.name, "mcp.json") or std.mem.eql(u8, entry.name, ".mcp.json")) {
+ has_mcp = true;
+ } else if (std.mem.eql(u8, entry.name, ".claude-plugin")) {
+ saw_claude = true;
+ } else if (std.mem.eql(u8, entry.name, ".cursor-plugin")) {
+ saw_cursor = true;
+ } else if (std.mem.eql(u8, entry.name, ".grok-plugin")) {
+ saw_grok = true;
+ } else if (std.mem.eql(u8, entry.name, "plugin.json")) {
+ saw_plugin_json = true;
+ }
+ }
- const manifest = readManifest(io, arena, plugin_path);
+ const manifest_rel: ?[]const u8 = if (saw_claude) ".claude-plugin/plugin.json" else if (saw_cursor) ".cursor-plugin/plugin.json" else if (saw_grok) ".grok-plugin/plugin.json" else if (saw_plugin_json) "plugin.json" else null;
+ const manifest = if (manifest_rel) |rel| readOneManifest(io, arena, plugin_path, rel) else null;
if (manifest) |v| {
collectPaths(arena, &skills, io, plugin_path, v.object.get("skills"));
collectPaths(arena, &skills, io, plugin_path, v.object.get("commands"));
collectPaths(arena, &agents, io, plugin_path, v.object.get("agents"));
+ if (v.object.get("mcpServers") != null) has_mcp = true;
}
+ const name = if (manifest) |v| blk: {
+ if (v.object.get("name")) |n| if (n == .string and n.string.len > 0) break :blk n.string;
+ break :blk std.fs.path.basename(plugin_path);
+ } else std.fs.path.basename(plugin_path);
+
var root_skill: ?RootSkill = null;
- if (skills.items.len == 0 and existsJoin(io, plugin_path, "SKILL.md")) {
- const name = if (manifest) |v| blk: {
- if (v.object.get("name")) |n| if (n == .string and n.string.len > 0) break :blk n.string;
- break :blk std.fs.path.basename(plugin_path);
- } else std.fs.path.basename(plugin_path);
+ if (skills.items.len == 0 and saw_root_skill) {
root_skill = .{
.path = join(arena, plugin_path, "SKILL.md"),
.name = name,
@@ -193,10 +227,8 @@ pub fn inspect(io: Io, arena: Allocator, plugin_path: []const u8) Layout {
};
}
- const has_mcp = existsJoin(io, plugin_path, ".mcp.json") or existsJoin(io, plugin_path, "mcp.json") or
- (if (manifest) |v| v.object.get("mcpServers") != null else false);
-
return .{
+ .name = name,
.skill_dirs = skills.items,
.agent_dirs = agents.items,
.root_skill = root_skill,
@@ -238,6 +270,7 @@ test "inspect: commands/ and root SKILL.md follow Claude defaults" {
try Io.Dir.cwd().writeFile(io, .{ .sub_path = join(arena, plug, "commands/hello.md"), .data = "---\nname: hello\ndescription: say hi\n---\nHi.\n" });
const lay = inspect(io, arena, plug);
+ try testing.expectEqualStrings("greet", lay.name);
try testing.expect(lay.has_commands);
try testing.expectEqual(@as(usize, 1), lay.skill_dirs.len);
try testing.expect(std.mem.endsWith(u8, lay.skill_dirs[0], "commands"));
@@ -248,6 +281,7 @@ test "inspect: commands/ and root SKILL.md follow Claude defaults" {
try Io.Dir.cwd().writeFile(io, .{ .sub_path = join(arena, lone, ".claude-plugin/plugin.json"), .data = "{\"name\":\"solo\"}" });
try Io.Dir.cwd().writeFile(io, .{ .sub_path = join(arena, lone, "SKILL.md"), .data = "---\nname: solo\ndescription: one playbook\n---\nDo the thing.\n" });
const lone_lay = inspect(io, arena, lone);
+ try testing.expectEqualStrings("solo", lone_lay.name);
try testing.expect(lone_lay.root_skill != null);
try testing.expectEqualStrings("solo", lone_lay.root_skill.?.name);
try testing.expectEqual(@as(usize, 0), lone_lay.skill_dirs.len);
diff --git a/src/plugins.zig b/src/plugins.zig
index eb875d35..b00cdb16 100644
--- a/src/plugins.zig
+++ b/src/plugins.zig
@@ -5,7 +5,8 @@
//! other harness already installed them. graff's first-run `adopt` copies once
//! and then ignores anything added later. This module is the live scan.
//!
-//! Skills stay on-demand (`skill` tool / `/skills`). MCP still needs `/mcp
+//! Skills stay on-demand (`skill` tool / `/skills`), including Claude
+//! `commands/*.md` and a plugin-root `SKILL.md`. MCP still needs `/mcp
//! trust` or `--yolo`. Plugin hooks are not run. GRAFF_NO_PLUGINS=1 disables.
const std = @import("std");
@@ -29,6 +30,10 @@ pub const Plugin = struct {
agents: bool = false,
mcp: bool = false,
commands: bool = false,
+ /// Filled by the walk so later skill/agent/MCP merges do not inspect again.
+ skill_dirs: []const []const u8 = &.{},
+ agent_dirs: []const []const u8 = &.{},
+ root_skill: ?layout.RootSkill = null,
};
fn off() bool {
@@ -43,10 +48,6 @@ pub fn applyEnv(environ_map: anytype) void {
disabled = environ_map.get("GRAFF_NO_PLUGINS") != null;
}
-fn exists(io: Io, path: []const u8) bool {
- return (Io.Dir.cwd().statFile(io, path, .{}) catch null) != null;
-}
-
fn join(arena: Allocator, a: []const u8, b: []const u8) []const u8 {
return std.fmt.allocPrint(arena, "{s}/{s}", .{ a, b }) catch "";
}
@@ -57,66 +58,57 @@ fn skipped(name: []const u8) bool {
std.mem.eql(u8, name, "node_modules");
}
-fn existsJoin(io: Io, a: []const u8, b: []const u8) bool {
- var buf: [std.fs.max_path_bytes]u8 = undefined;
- const p = std.fmt.bufPrint(&buf, "{s}/{s}", .{ a, b }) catch return false;
- return exists(io, p);
-}
-
-fn isPlugin(io: Io, path: []const u8) bool {
- return existsJoin(io, path, ".cursor-plugin/plugin.json") or
- existsJoin(io, path, ".claude-plugin/plugin.json") or
- existsJoin(io, path, ".grok-plugin/plugin.json") or
- existsJoin(io, path, "plugin.json") or
- existsJoin(io, path, ".mcp.json") or
- existsJoin(io, path, "mcp.json") or
- existsJoin(io, path, "skills") or
- existsJoin(io, path, "agents") or
- existsJoin(io, path, "commands") or
- existsJoin(io, path, "SKILL.md");
-}
-
-fn hasMcp(io: Io, path: []const u8) bool {
- return existsJoin(io, path, ".mcp.json") or existsJoin(io, path, "mcp.json");
-}
-
-/// Manifest `name` when present — Cursor cache folders are content hashes.
-fn pluginName(io: Io, arena: Allocator, path: []const u8) []const u8 {
- const files = [_][]const u8{ ".cursor-plugin/plugin.json", ".claude-plugin/plugin.json", ".grok-plugin/plugin.json", "plugin.json" };
- for (files) |rel| {
- const text = Io.Dir.cwd().readFileAlloc(io, join(arena, path, rel), arena, .limited(64 * 1024)) catch continue;
- const v = std.json.parseFromSliceLeaky(Value, arena, text, .{ .allocate = .alloc_always }) catch continue;
- if (v == .object) if (v.object.get("name")) |n| if (n == .string and n.string.len > 0) return n.string;
- }
- return arena.dupe(u8, std.fs.path.basename(path)) catch std.fs.path.basename(path);
+/// True when this directory entry is enough to treat the parent as a plugin,
+/// so the walk does not `stat` ten marker paths per visited folder.
+fn pluginMarker(name: []const u8) bool {
+ return std.mem.eql(u8, name, ".cursor-plugin") or
+ std.mem.eql(u8, name, ".claude-plugin") or
+ std.mem.eql(u8, name, ".grok-plugin") or
+ std.mem.eql(u8, name, "plugin.json") or
+ std.mem.eql(u8, name, ".mcp.json") or
+ std.mem.eql(u8, name, "mcp.json") or
+ std.mem.eql(u8, name, "skills") or
+ std.mem.eql(u8, name, "agents") or
+ std.mem.eql(u8, name, "commands") or
+ std.mem.eql(u8, name, "SKILL.md");
}
fn walk(io: Io, arena: Allocator, list: *std.ArrayList(Plugin), path: []const u8, origin: []const u8, personal: bool, depth: u8, visits: *usize) void {
if (list.items.len >= plugin_cap or visits.* >= visit_cap) return;
var dir = Io.Dir.cwd().openDir(io, path, .{ .iterate = true }) catch return;
defer dir.close(io);
- if (isPlugin(io, path)) {
+ var children: std.ArrayList([]const u8) = .empty;
+ var plugin_here = false;
+ var it = dir.iterate();
+ while (it.next(io) catch null) |entry| {
+ if (pluginMarker(entry.name)) plugin_here = true;
+ if (entry.kind != .directory and entry.kind != .sym_link) continue;
+ if (entry.name.len > 0 and entry.name[0] == '.') continue;
+ if (skipped(entry.name)) continue;
+ const child = join(arena, path, entry.name);
+ if (child.len == 0) continue;
+ children.append(arena, child) catch {};
+ }
+ if (plugin_here) {
const lay = layout.inspect(io, arena, path);
list.append(arena, .{
- .name = pluginName(io, arena, path),
+ .name = if (lay.name.len > 0) lay.name else std.fs.path.basename(path),
.path = path,
.origin = origin,
.personal = personal,
.skills = lay.skill_dirs.len > 0 or lay.root_skill != null,
.agents = lay.agent_dirs.len > 0,
- .mcp = lay.has_mcp or hasMcp(io, path),
+ .mcp = lay.has_mcp,
.commands = lay.has_commands,
+ .skill_dirs = lay.skill_dirs,
+ .agent_dirs = lay.agent_dirs,
+ .root_skill = lay.root_skill,
}) catch {};
return; // do not treat a plugin's skills/ as a nested plugin
}
if (depth == 0) return;
visits.* += 1;
- var it = dir.iterate();
- while (it.next(io) catch null) |entry| {
- if (entry.kind != .directory and entry.kind != .sym_link) continue;
- if (skipped(entry.name)) continue;
- const child = join(arena, path, entry.name);
- if (child.len == 0) continue;
+ for (children.items) |child| {
walk(io, arena, list, child, origin, personal, depth - 1, visits);
if (list.items.len >= plugin_cap) return;
}
@@ -169,7 +161,7 @@ pub fn skillDirs(io: Io, arena: Allocator, home: ?[]const u8, project: Io.Dir, p
var out: std.ArrayList([]const u8) = .empty;
for (discover(io, arena, home, project)) |p| {
if (p.personal != personal) continue;
- for (layout.inspect(io, arena, p.path).skill_dirs) |d| out.append(arena, d) catch {};
+ out.appendSlice(arena, p.skill_dirs) catch {};
}
return out.items;
}
@@ -178,7 +170,7 @@ pub fn rootSkills(io: Io, arena: Allocator, home: ?[]const u8, project: Io.Dir,
var out: std.ArrayList(layout.RootSkill) = .empty;
for (discover(io, arena, home, project)) |p| {
if (p.personal != personal) continue;
- if (layout.inspect(io, arena, p.path).root_skill) |rs| out.append(arena, rs) catch {};
+ if (p.root_skill) |rs| out.append(arena, rs) catch {};
}
return out.items;
}
@@ -187,7 +179,7 @@ pub fn agentDirs(io: Io, arena: Allocator, home: ?[]const u8, project: Io.Dir, p
var out: std.ArrayList([]const u8) = .empty;
for (discover(io, arena, home, project)) |p| {
if (p.personal != personal) continue;
- for (layout.inspect(io, arena, p.path).agent_dirs) |d| out.append(arena, d) catch {};
+ out.appendSlice(arena, p.agent_dirs) catch {};
}
return out.items;
}
@@ -274,10 +266,9 @@ pub fn slashCommand(io: Io, arena: Allocator, home: []const u8, line: []const u8
const want = std.mem.trim(u8, rest["load ".len..], " \t");
for (list) |p| {
if (!std.ascii.eqlIgnoreCase(p.name, want)) continue;
- const lay = layout.inspect(io, arena, p.path);
try out.print("{s} [{s}/{s}] {s}\n", .{ p.name, p.origin, if (p.personal) "user" else "project", p.path });
- if (lay.has_commands) try out.writeAll(" commands/*.md load as skills (Claude default)\n");
- if (lay.root_skill) |rs| try out.print(" root skill: {s}\n", .{rs.name});
+ if (p.commands) try out.writeAll(" commands/*.md load as skills (Claude default)\n");
+ if (p.root_skill) |rs| try out.print(" root skill: {s}\n", .{rs.name});
if (p.skills) try out.writeAll(" skills: on-demand via the skill tool / /skills\n");
if (p.agents) try out.writeAll(" agents: in the fleet\n");
if (p.mcp) try out.writeAll(" mcp: in the merge (needs /mcp trust or --yolo)\n");
@@ -344,8 +335,11 @@ test "discover: Claude commands/ alone is a plugin and a skill dir" {
const list = discover(io, arena, home, tmp.dir);
try testing.expectEqual(@as(usize, 1), list.len);
+ try testing.expectEqualStrings("greet", list[0].name);
try testing.expect(list[0].commands);
try testing.expect(list[0].skills);
+ try testing.expectEqual(@as(usize, 1), list[0].skill_dirs.len);
+ try testing.expect(std.mem.endsWith(u8, list[0].skill_dirs[0], "commands"));
const dirs = skillDirs(io, arena, home, tmp.dir, true);
try testing.expectEqual(@as(usize, 1), dirs.len);
try testing.expect(std.mem.endsWith(u8, dirs[0], "commands"));
@@ -530,3 +524,32 @@ test "discover: Cursor cache uses .cursor-plugin + mcp.json; name from manifest"
try testing.expect(found);
try testing.expectEqualStrings("https://gmailmcp.googleapis.com/mcp/v1", servers.get("gmail").?.object.get("url").?.string);
}
+
+test "discover: empty cache dirs are not plugins; layout rides on Plugin" {
+ var tmp = testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+ const io = testing.io;
+ const base = try tmpBase(io, &tmp, arena);
+ const home = join(arena, base, "home");
+ try Io.Dir.cwd().createDirPath(io, join(arena, home, ".cursor/plugins/cache/org/id/not-a-plugin"));
+ try Io.Dir.cwd().createDirPath(io, join(arena, home, ".claude/plugins/cache/official/empty"));
+ try testing.expectEqual(@as(usize, 0), discover(io, arena, home, tmp.dir).len);
+
+ const plug = join(arena, home, ".codegraff/plugins/solo");
+ try Io.Dir.cwd().createDirPath(io, plug);
+ try Io.Dir.cwd().writeFile(io, .{
+ .sub_path = join(arena, plug, "SKILL.md"),
+ .data = "---\nname: solo\ndescription: one playbook\n---\nDo the thing.\n",
+ });
+ const list = discover(io, arena, home, tmp.dir);
+ try testing.expectEqual(@as(usize, 1), list.len);
+ try testing.expectEqualStrings("solo", list[0].name);
+ try testing.expect(list[0].root_skill != null);
+ try testing.expectEqualStrings("solo", list[0].root_skill.?.name);
+ const roots = rootSkills(io, arena, home, tmp.dir, true);
+ try testing.expectEqual(@as(usize, 1), roots.len);
+ try testing.expectEqualStrings("solo", roots[0].name);
+}
diff --git a/src/skill_docs.zig b/src/skill_docs.zig
index 7bed82e7..44afc6aa 100644
--- a/src/skill_docs.zig
+++ b/src/skill_docs.zig
@@ -158,7 +158,8 @@ fn scan(io: Io, arena: Allocator, home: ?[]const u8) []const Skill {
/// bundled < personal dirs < user plugins < .claude/.grok/.codex/skills <
/// project plugins < .harness/skills. Each later tier shadows the previous by
-/// name. Plugin trees are read in place (ADR 0007), not copied.
+/// name. Plugin trees are read in place (ADR 0007), not copied. One `discover`
+/// fills both plugin tiers so startup does not walk Cursor/Claude trees four times.
fn collect(io: Io, arena: Allocator, home: ?[]const u8) std.ArrayList(Skill) {
var list: std.ArrayList(Skill) = .empty;
list.appendSlice(arena, &builtins) catch {};
@@ -166,14 +167,21 @@ fn collect(io: Io, arena: Allocator, home: ?[]const u8) std.ArrayList(Skill) {
const path = std.fmt.allocPrint(arena, "{s}/{s}", .{ h, dir }) catch continue;
loadDir(io, arena, &list, path, .personal);
};
- for (plugins.skillDirs(io, arena, home, Io.Dir.cwd(), true)) |d| loadDir(io, arena, &list, d, .plugin);
- for (plugins.rootSkills(io, arena, home, Io.Dir.cwd(), true)) |rs| mergeFile(io, arena, &list, rs.dir, rs.path, rs.name, .plugin);
+ const plugs = plugins.discover(io, arena, home, Io.Dir.cwd());
+ for (plugs) |p| {
+ if (!p.personal) continue;
+ for (p.skill_dirs) |d| loadDir(io, arena, &list, d, .plugin);
+ if (p.root_skill) |rs| mergeFile(io, arena, &list, rs.dir, rs.path, rs.name, .plugin);
+ }
loadDir(io, arena, &list, compat_dir, .project);
loadDir(io, arena, &list, ".grok/skills", .project);
loadDir(io, arena, &list, ".codex/skills", .project);
loadDir(io, arena, &list, ".cursor/skills", .project);
- for (plugins.skillDirs(io, arena, home, Io.Dir.cwd(), false)) |d| loadDir(io, arena, &list, d, .plugin);
- for (plugins.rootSkills(io, arena, home, Io.Dir.cwd(), false)) |rs| mergeFile(io, arena, &list, rs.dir, rs.path, rs.name, .plugin);
+ for (plugs) |p| {
+ if (p.personal) continue;
+ for (p.skill_dirs) |d| loadDir(io, arena, &list, d, .plugin);
+ if (p.root_skill) |rs| mergeFile(io, arena, &list, rs.dir, rs.path, rs.name, .plugin);
+ }
loadDir(io, arena, &list, project_dir, .project);
return list;
}
@@ -266,14 +274,20 @@ pub fn promptCatalog(arena: Allocator, list: []const Skill) []const u8 {
return aw.writer.buffered();
}
-/// The `skill` tool. Rescans the skill directories every call, so a skill
-/// written earlier in this same session loads without a restart. An unknown
-/// name comes back as an error listing what does exist.
+/// The `skill` tool. A named load already in the session catalog (`g_skills`)
+/// returns that body without walking plugin trees again. A miss, or an empty
+/// name (list), rescans so a SKILL.md written this session still loads.
+/// An unknown name comes back as an error listing what does exist.
pub fn execSkill(gpa: Allocator, io: Io, input: Value) !ToolOutput {
+ const requested = std.mem.trim(u8, tools.strField(input, "name") orelse "", " \t\r\n");
+ if (requested.len > 0) {
+ for (g_skills) |sk| {
+ if (std.ascii.eqlIgnoreCase(sk.name, requested)) return .{ .text = try render(gpa, sk) };
+ }
+ }
var arena_state = std.heap.ArenaAllocator.init(gpa);
defer arena_state.deinit();
const list = scan(io, arena_state.allocator(), g_home);
- const requested = std.mem.trim(u8, tools.strField(input, "name") orelse "", " \t\r\n");
if (requested.len == 0) return .{ .text = try listing(gpa, list, null) };
for (list) |sk| {
if (std.ascii.eqlIgnoreCase(sk.name, requested)) return .{ .text = try render(gpa, sk) };
@@ -461,3 +475,24 @@ test "listing: an unknown name is an error result that names what exists" {
defer gpa.free(empty);
try std.testing.expect(std.mem.indexOf(u8, empty, ".harness/skills/<name>/SKILL.md") != null);
}
+
+test "execSkill: a named load hits the session catalog without a rescan" {
+ const prev = g_skills;
+ defer g_skills = prev;
+ const one = [_]Skill{.{
+ .name = "memhit",
+ .desc = "d",
+ .body = "hello-from-mem",
+ .source = .plugin,
+ }};
+ g_skills = &one;
+
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ var obj: std.json.ObjectMap = .empty;
+ try obj.put(arena_state.allocator(), "name", .{ .string = "memhit" });
+ const out = try execSkill(std.testing.allocator, std.testing.io, .{ .object = obj });
+ defer std.testing.allocator.free(out.text);
+ try std.testing.expect(!out.is_error);
+ try std.testing.expect(std.mem.indexOf(u8, out.text, "hello-from-mem") != null);
+}