feat: per-worktree dynamic registry augmentation
When a worktree already exists on disk (retry, waiting_human -> implement handoff), the daemon now scans it for .agents/skills/ that declare metadata.archie.workflow and augments the startup registry with any newly discovered workflows. Worktree skills take precedence over the startup registry for that task only. Changes: - skillbuild.AugmentRegistry(worktree, base) — scans a worktree directory for skills, builds workflows from plugins, returns a new registry with worktree entries merged over base. Base is never mutated. Mirrors BuildRegistry but starts from caller's registry instead of builtins. - daemon.process() — computes worktree path, calls AugmentRegistry if the directory exists, uses augmented registry for routing. Degrades gracefully to startup registry on error. Tests (10 new): - Adds worktree workflows to base registry - Worktree skills override same-named base entries - Base registry is not mutated (copy semantics) - Nonexistent worktree returns base unchanged - Worktree with no .agents/skills/ returns base unchanged - Worktree skill with no plugins does not override base - Adversarial: concurrent augmentation is safe - Adversarial: broken SKILL.md is skipped - Adversarial: bare worktree (no .agents/) does not error - Adversarial: nil base is treated as empty Why: skills are per-project (.agents/skills/ in each repo). The startup registry only knows about a shared skills directory. Per- worktree augmentation means a repo can define its own workflows without restarting the daemon.
This commit is contained in:
1 parent
9bd61e61ac
commit
a9a3f7b3f9
3 files changed
+424
-2
No files matched your search
@@ -27,6 +27,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/plugin"
|
||||
"github.com/samcharles93/archie-core/internal/store"
|
||||
"github.com/samcharles93/archie-core/internal/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/workflow/skillbuild"
|
||||
"github.com/samcharles93/archie-core/internal/worktree"
|
||||
)
|
||||
|
||||
@@ -484,13 +485,26 @@ func (d *Daemon) process(ctx context.Context, task *store.Task) {
|
||||
_ = d.Store.Transition(ctx, task.ID, store.StatusRunning, store.StatusParked, "repo no longer in config")
|
||||
return
|
||||
}
|
||||
wf := workflow.Route(task, d.Workflows)
|
||||
// Per-worktree registry augmentation: if the worktree already
|
||||
// exists (e.g. retry, waiting_human → implement handoff), scan
|
||||
// it for .agents/skills/ that declare workflows. Worktree skills
|
||||
// override the startup registry for this task only.
|
||||
workDir := d.Trees.Dir(task.Owner, task.Repo, task.IssueNumber)
|
||||
registry := d.Workflows
|
||||
if _, err := os.Stat(workDir); err == nil {
|
||||
aug, err := skillbuild.AugmentRegistry(workDir, d.Workflows)
|
||||
if err != nil {
|
||||
d.Log.Warn("worktree registry augmentation failed — using startup registry", "dir", workDir, "err", err)
|
||||
} else {
|
||||
registry = aug
|
||||
}
|
||||
}
|
||||
wf := workflow.Route(task, registry)
|
||||
d.Log.Info("processing task", "repo", repo.FullName(), "issue", task.IssueNumber, "workflow", wf.Name, "attempt", task.Attempt)
|
||||
d.Forge.SetStateLabel(ctx, task.Owner, task.Repo, task.IssueNumber, d.Cfg.Dispatch.StateLabel("working"), d.Cfg.Dispatch.LabelValues())
|
||||
|
||||
// Acquire a container for the task when Docker sandboxing is enabled.
|
||||
if d.ContainerPool != nil {
|
||||
workDir := d.Trees.Dir(task.Owner, task.Repo, task.IssueNumber)
|
||||
// Write task.json — the container's boot-time brief.
|
||||
_ = container.WriteTaskJSON(workDir, container.TaskPayload{
|
||||
ID: task.ID, Owner: task.Owner, Repo: task.Repo,
|
||||
|
||||
@@ -71,6 +71,48 @@ type SkillWorkflow struct {
|
||||
Dir string // skill directory name
|
||||
}
|
||||
|
||||
// AugmentRegistry scans a worktree directory for skills that declare
|
||||
// workflows, builds each from its stage plugins, and returns a new
|
||||
// registry with worktree workflows merged over the base. The base
|
||||
// registry is not mutated.
|
||||
//
|
||||
// This is the per-task complement to BuildRegistry: BuildRegistry
|
||||
// runs once at startup against a shared skills directory (or the
|
||||
// workdir root); AugmentRegistry runs at process time against a
|
||||
// specific cloned worktree so that per-repo skills are discovered
|
||||
// without restarting the daemon.
|
||||
func AugmentRegistry(worktree string, base workflow.Registry) (workflow.Registry, error) {
|
||||
catalog, err := skill.Catalog(worktree)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("skill catalog: %w", err)
|
||||
}
|
||||
|
||||
// Copy base — never mutate the caller's registry.
|
||||
reg := make(workflow.Registry, len(base)+len(catalog))
|
||||
for k, v := range base {
|
||||
reg[k] = v
|
||||
}
|
||||
|
||||
for _, entry := range catalog {
|
||||
if entry.Workflow == "" {
|
||||
continue
|
||||
}
|
||||
sw := SkillWorkflow{Workflow: entry.Workflow, Dir: entry.Dir}
|
||||
wf, err := BuildWorkflow(worktree, sw)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("build workflow %s from skill %s: %w", entry.Workflow, entry.Dir, err)
|
||||
}
|
||||
// Only override the base entry when the skill actually has stages.
|
||||
// An empty workflow means the skill declared intent but has no
|
||||
// plugins yet — keep the base (or built-in) definition.
|
||||
if len(wf.Stages) > 0 {
|
||||
reg[entry.Workflow] = wf
|
||||
}
|
||||
}
|
||||
|
||||
return reg, nil
|
||||
}
|
||||
|
||||
// BuildWorkflow loads stage plugins from a skill's plugins/ directory
|
||||
// and returns a workflow.Workflow. Stages are Yaegi-interpreted .go files
|
||||
// sorted by filename. Each file must export:
|
||||
|
||||
@@ -342,6 +342,372 @@ func TestBuildRegistryEmptySkillsDirReturnsBuiltins(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// ── per-worktree registry augmentation ───────────────────────────────
|
||||
|
||||
func TestAugmentRegistryAddsWorktreeWorkflows(t *testing.T) {
|
||||
// When a worktree contains .agents/skills/ with workflow-declaring
|
||||
// skills, AugmentRegistry must add those workflows to the base
|
||||
// registry. Workflows already in the base are not duplicated.
|
||||
|
||||
worktree := t.TempDir()
|
||||
skillDir := filepath.Join(worktree, ".agents", "skills", "archie-wf-custom")
|
||||
pluginsDir := filepath.Join(skillDir, "plugins")
|
||||
if err := os.MkdirAll(pluginsDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(skillDir, "SKILL.md"), []byte(`---
|
||||
name: archie-wf-custom
|
||||
description: Custom workflow from worktree
|
||||
version: 1.0.0
|
||||
metadata:
|
||||
archie:
|
||||
workflow: custom
|
||||
---
|
||||
Custom worktree workflow.
|
||||
`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(pluginsDir, "01-do.go"), []byte(`package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/samcharles93/archie-core/internal/workflow"
|
||||
)
|
||||
|
||||
func Stage() (string, func(context.Context, *workflow.TaskContext) error) {
|
||||
return "do", func(ctx context.Context, tc *workflow.TaskContext) error {
|
||||
tc.BuildSummary = "worktree-custom-ran"
|
||||
return nil
|
||||
}
|
||||
}
|
||||
`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
base := workflow.Registry{"default": {Name: "default", Stages: nil}}
|
||||
aug, err := AugmentRegistry(worktree, base)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Base entries preserved.
|
||||
if _, ok := aug["default"]; !ok {
|
||||
t.Error("'default' missing from augmented registry")
|
||||
}
|
||||
|
||||
// Worktree workflow added.
|
||||
wf, ok := aug["custom"]
|
||||
if !ok {
|
||||
t.Fatal("'custom' workflow not found in augmented registry — worktree skill was not discovered")
|
||||
}
|
||||
if wf.Name != "custom" {
|
||||
t.Errorf("Workflow.Name = %q, want custom", wf.Name)
|
||||
}
|
||||
if len(wf.Stages) != 1 {
|
||||
t.Fatalf("got %d stages, want 1", len(wf.Stages))
|
||||
}
|
||||
if wf.Stages[0].Name != "do" {
|
||||
t.Errorf("stage[0].Name = %q, want do", wf.Stages[0].Name)
|
||||
}
|
||||
|
||||
// Run the worktree workflow.
|
||||
tc := &workflow.TaskContext{BuildSummary: ""}
|
||||
if err := wf.Stages[0].Run(context.Background(), tc); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if tc.BuildSummary != "worktree-custom-ran" {
|
||||
t.Errorf("BuildSummary = %q, want 'worktree-custom-ran'", tc.BuildSummary)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAugmentRegistryWorktreeOverridesBase(t *testing.T) {
|
||||
// A worktree skill declaring the same workflow name as a base entry
|
||||
// must take precedence. Worktree skills ARE the repo's workflow
|
||||
// definition.
|
||||
|
||||
worktree := t.TempDir()
|
||||
skillDir := filepath.Join(worktree, ".agents", "skills", "archie-wf-implement")
|
||||
pluginsDir := filepath.Join(skillDir, "plugins")
|
||||
if err := os.MkdirAll(pluginsDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(skillDir, "SKILL.md"), []byte(`---
|
||||
name: archie-wf-implement
|
||||
description: Repo-specific implement workflow
|
||||
version: 1.0.0
|
||||
metadata:
|
||||
archie:
|
||||
workflow: implement
|
||||
---
|
||||
Repo-custom implement.
|
||||
`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(pluginsDir, "01-repo-plan.go"), []byte(`package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/samcharles93/archie-core/internal/workflow"
|
||||
)
|
||||
|
||||
func Stage() (string, func(context.Context, *workflow.TaskContext) error) {
|
||||
return "repo-plan", func(ctx context.Context, tc *workflow.TaskContext) error {
|
||||
tc.BuildSummary = "repo-implement-override"
|
||||
return nil
|
||||
}
|
||||
}
|
||||
`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Base has "implement" as a built-in.
|
||||
base := workflow.Registry{"implement": {Name: "implement", Stages: []workflow.Stage{
|
||||
{Name: "builtin-plan", Run: func(ctx context.Context, tc *workflow.TaskContext) error {
|
||||
tc.BuildSummary = "builtin-implement"
|
||||
return nil
|
||||
}},
|
||||
}}}
|
||||
aug, err := AugmentRegistry(worktree, base)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
wf, ok := aug["implement"]
|
||||
if !ok {
|
||||
t.Fatal("'implement' missing from augmented registry")
|
||||
}
|
||||
if len(wf.Stages) != 1 {
|
||||
t.Fatalf("got %d stages, want 1 from worktree override", len(wf.Stages))
|
||||
}
|
||||
if wf.Stages[0].Name != "repo-plan" {
|
||||
t.Errorf("stage[0].Name = %q, want 'repo-plan' from worktree override", wf.Stages[0].Name)
|
||||
}
|
||||
|
||||
// Run it — must use worktree version.
|
||||
tc := &workflow.TaskContext{BuildSummary: ""}
|
||||
if err := wf.Stages[0].Run(context.Background(), tc); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if tc.BuildSummary != "repo-implement-override" {
|
||||
t.Errorf("BuildSummary = %q, want 'repo-implement-override' (worktree override)", tc.BuildSummary)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAugmentRegistryDoesNotModifyBase(t *testing.T) {
|
||||
// AugmentRegistry returns a NEW registry — the caller's base must
|
||||
// not be mutated.
|
||||
base := workflow.Registry{"keep": {Name: "keep"}}
|
||||
worktree := t.TempDir()
|
||||
|
||||
aug, err := AugmentRegistry(worktree, base)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Augmented has the entry.
|
||||
if _, ok := aug["keep"]; !ok {
|
||||
t.Fatal("'keep' missing from augmented registry")
|
||||
}
|
||||
|
||||
// Mutating augmented must not affect base.
|
||||
aug["keep"] = workflow.Workflow{Name: "modified"}
|
||||
if base["keep"].Name != "keep" {
|
||||
t.Error("AugmentRegistry mutated the base registry — must return a copy")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAugmentRegistryNonexistentWorktreeReturnsBase(t *testing.T) {
|
||||
base := workflow.Registry{"default": {Name: "default"}}
|
||||
aug, err := AugmentRegistry("/nonexistent/worktree/path", base)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(aug) != len(base) {
|
||||
t.Errorf("got %d entries, want %d (nonexistent worktree adds nothing)", len(aug), len(base))
|
||||
}
|
||||
}
|
||||
|
||||
func TestAugmentRegistryWorktreeWithNoSkillsReturnsBase(t *testing.T) {
|
||||
// A worktree without .agents/skills/ must not error — returns
|
||||
// a copy of the base unchanged.
|
||||
worktree := t.TempDir()
|
||||
base := workflow.Registry{"default": {Name: "default"}}
|
||||
aug, err := AugmentRegistry(worktree, base)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(aug) != len(base) {
|
||||
t.Errorf("got %d entries, want %d (no skills in worktree)", len(aug), len(base))
|
||||
}
|
||||
}
|
||||
|
||||
func TestAugmentRegistryWorktreeSkillWithNoStagesDoesNotOverride(t *testing.T) {
|
||||
// A skill that declares a workflow but has no plugins must not
|
||||
// override the base entry — it would produce an empty workflow.
|
||||
worktree := t.TempDir()
|
||||
skillDir := filepath.Join(worktree, ".agents", "skills", "archie-wf-empty")
|
||||
if err := os.MkdirAll(skillDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(skillDir, "SKILL.md"), []byte(`---
|
||||
name: archie-wf-empty
|
||||
description: Workflow with no stages
|
||||
version: 1.0.0
|
||||
metadata:
|
||||
archie:
|
||||
workflow: implement
|
||||
---
|
||||
No plugins here.
|
||||
`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
base := workflow.Registry{"implement": {Name: "implement", Stages: []workflow.Stage{
|
||||
{Name: "builtin", Run: func(ctx context.Context, tc *workflow.TaskContext) error { return nil }},
|
||||
}}}
|
||||
aug, err := AugmentRegistry(worktree, base)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
wf := aug["implement"]
|
||||
if len(wf.Stages) == 0 {
|
||||
t.Error("worktree skill with no stages overrode base — must keep base when skill is empty")
|
||||
}
|
||||
if wf.Stages[0].Name != "builtin" {
|
||||
t.Error("base stage was replaced by empty worktree skill")
|
||||
}
|
||||
}
|
||||
|
||||
// ── AugmentRegistry adversarial tests ────────────────────────────────
|
||||
|
||||
func TestAugmentRegistryConcurrentAccess(t *testing.T) {
|
||||
// AugmentRegistry must be safe to call concurrently from different
|
||||
// goroutines (e.g. multiple tasks processed in parallel).
|
||||
worktree := t.TempDir()
|
||||
skillDir := filepath.Join(worktree, ".agents", "skills", "archie-wf-concurrent")
|
||||
pluginsDir := filepath.Join(skillDir, "plugins")
|
||||
if err := os.MkdirAll(pluginsDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(skillDir, "SKILL.md"), []byte(`---
|
||||
name: archie-wf-concurrent
|
||||
description: Concurrent test
|
||||
version: 1.0.0
|
||||
metadata:
|
||||
archie:
|
||||
workflow: concurrent
|
||||
---
|
||||
Concurrent.
|
||||
`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(pluginsDir, "01-step.go"), []byte(`package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/samcharles93/archie-core/internal/workflow"
|
||||
)
|
||||
|
||||
func Stage() (string, func(context.Context, *workflow.TaskContext) error) {
|
||||
return "step", func(ctx context.Context, tc *workflow.TaskContext) error {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
base := workflow.Registry{"default": {Name: "default"}}
|
||||
|
||||
// Run 10 concurrent augmentations — none should panic or corrupt state.
|
||||
errs := make(chan error, 10)
|
||||
for i := 0; i < 10; i++ {
|
||||
go func() {
|
||||
aug, err := AugmentRegistry(worktree, base)
|
||||
if err != nil {
|
||||
errs <- err
|
||||
return
|
||||
}
|
||||
if _, ok := aug["concurrent"]; !ok {
|
||||
errs <- nil // would use sentinel but keeping simple
|
||||
return
|
||||
}
|
||||
errs <- nil
|
||||
}()
|
||||
}
|
||||
for i := 0; i < 10; i++ {
|
||||
if err := <-errs; err != nil {
|
||||
t.Error("concurrent AugmentRegistry failed:", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Base must still be unmodified.
|
||||
if _, ok := base["concurrent"]; ok {
|
||||
t.Error("base registry was mutated by concurrent augmentation")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAugmentRegistryBrokenSkillMDIsSkipped(t *testing.T) {
|
||||
// A SKILL.md with invalid YAML must not crash augmentation — the
|
||||
// skill is simply skipped (skill.Catalog skips parse errors).
|
||||
worktree := t.TempDir()
|
||||
skillDir := filepath.Join(worktree, ".agents", "skills", "archie-wf-broken")
|
||||
if err := os.MkdirAll(skillDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(skillDir, "SKILL.md"), []byte(`---
|
||||
this is not valid: [[[ YAML {{{
|
||||
---
|
||||
Body.
|
||||
`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
base := workflow.Registry{"default": {Name: "default"}}
|
||||
aug, err := AugmentRegistry(worktree, base)
|
||||
if err != nil {
|
||||
t.Fatal("AugmentRegistry errored on broken SKILL.md:", err)
|
||||
}
|
||||
if len(aug) != len(base) {
|
||||
t.Errorf("got %d entries, want %d (broken SKILL.md adds nothing)", len(aug), len(base))
|
||||
}
|
||||
}
|
||||
|
||||
func TestAugmentRegistryWorktreeWithNoSkillsDir(t *testing.T) {
|
||||
// A worktree that exists but has no .agents/ directory at all
|
||||
// must not error.
|
||||
worktree := t.TempDir()
|
||||
// Don't create .agents/skills/ — just a bare worktree.
|
||||
if err := os.WriteFile(filepath.Join(worktree, "README.md"), []byte(`# repo`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
base := workflow.Registry{"default": {Name: "default"}}
|
||||
aug, err := AugmentRegistry(worktree, base)
|
||||
if err != nil {
|
||||
t.Fatal("AugmentRegistry errored on worktree without .agents/: ", err)
|
||||
}
|
||||
if len(aug) != len(base) {
|
||||
t.Errorf("got %d entries, want %d", len(aug), len(base))
|
||||
}
|
||||
}
|
||||
|
||||
func TestAugmentRegistryNilBaseReturnsEmpty(t *testing.T) {
|
||||
// A nil base registry must be treated as empty — not panic.
|
||||
worktree := t.TempDir()
|
||||
aug, err := AugmentRegistry(worktree, nil)
|
||||
if err != nil {
|
||||
t.Fatal("AugmentRegistry errored with nil base:", err)
|
||||
}
|
||||
if aug == nil {
|
||||
t.Error("AugmentRegistry returned nil for nil base — want empty registry")
|
||||
}
|
||||
if len(aug) != 0 {
|
||||
t.Errorf("got %d entries with nil base and no skills, want 0", len(aug))
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildRegistryNoStagesReturnsEmptyWorkflow(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
skillDir := filepath.Join(dir, ".agents", "skills", "archie-wf-empty")
|
||||
|
||||
Reference in new issue
Block a user