From ae744d1cc49532bd0d9a5058bb72fef7b4d715c8 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sat, 25 Jul 2026 23:54:06 +1000 Subject: [PATCH] feat: /spawn gateway command for chat-initiated tasks TaskCreator interface on gateway.Router allows chat users to create tasks via /spawn . Implemented as gateway-local command (no LLM needed). StoreTaskCreator wraps the daemon's store and uses the first configured repo as the task workspace with timestamp-based synthetic issue numbers to avoid collisions with Gitea-issued tasks. Wired into archied's gateway startup: when repos are configured, router.Tasks is populated so /spawn works. When no repos are configured, /spawn returns 'not configured'. Closes: archie-core-abg.13 --- cmd/archied/main.go | 3 ++ internal/gateway/gateway.go | 38 ++++++++++++++ internal/gateway/gateway_test.go | 88 ++++++++++++++++++++++++++++++++ internal/gateway/tasks.go | 48 +++++++++++++++++ internal/gateway/tasks_test.go | 60 ++++++++++++++++++++++ 5 files changed, 237 insertions(+) create mode 100644 internal/gateway/tasks.go create mode 100644 internal/gateway/tasks_test.go diff --git a/cmd/archied/main.go b/cmd/archied/main.go index 511ef70ba..88f13a301 100644 --- a/cmd/archied/main.go +++ b/cmd/archied/main.go @@ -184,6 +184,9 @@ func run() int { } tg := telegram.New(tgToken, "", "", nil, log) router := gateway.NewRouter(st, nil, "telegram") + if len(cfg.Repos) > 0 { + router.Tasks = gateway.NewStoreTaskCreator(st, cfg.Repos[0].Owner, cfg.Repos[0].Name) + } go func() { if err := tg.Start(ctx, router); err != nil && ctx.Err() == nil { log.Error("telegram gateway stopped", "err", err) diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 206c21730..b56b05a52 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -63,6 +63,13 @@ type ModelManager interface { SetActiveModel(ctx context.Context, ref string) error } +// TaskCreator creates a task from a chat command. The daemon supplies +// an implementation backed by the store. When nil on a Router, /spawn +// returns "not configured". +type TaskCreator interface { + CreateTask(ctx context.Context, title string) (int64, error) +} + // Router dispatches inbound messages. Gateway-local commands (like // /status) are handled directly. Everything else is routed to the LLM // responder. Gateway implementations call Route on every inbound @@ -70,6 +77,7 @@ type ModelManager interface { type Router struct { Store StatusReader Models ModelManager // nil = /model and /models not configured + Tasks TaskCreator // nil = /spawn not configured LLM LLMResponder // nil = LLM not wired yet gatewayName string } @@ -94,6 +102,10 @@ func (r *Router) Route(ctx context.Context, msg Message) (string, error) { return r.handleModels(ctx) case "/model": return r.handleModel(ctx, arg) + case "/spawn": + // Title is everything after "/spawn" — keep the multi-word text. + title := restAfter(text, cmd, r.gatewayName) + return r.handleSpawn(ctx, title) default: if r.LLM == nil { return "I'm running but LLM processing isn't wired yet. Try /status.", nil @@ -170,6 +182,32 @@ func (r *Router) handleModel(ctx context.Context, arg string) (string, error) { return fmt.Sprintf("Active model set to %s.", arg), nil } +// restAfter returns the text after the command token, stripping the +// optional @gateway mention. E.g. for "/spawn Fix the bug", returns +// "Fix the bug". +func restAfter(text, cmd, gatewayName string) string { + s := text + if gatewayName != "" { + s = strings.TrimPrefix(s, cmd+"@"+gatewayName) + } + s = strings.TrimPrefix(s, cmd) + return strings.TrimSpace(s) +} + +func (r *Router) handleSpawn(ctx context.Context, title string) (string, error) { + if r.Tasks == nil { + return "Task creation is not configured.", nil + } + if title == "" { + return "Usage: /spawn <title>", nil + } + id, err := r.Tasks.CreateTask(ctx, title) + if err != nil { + return fmt.Sprintf("Failed to create task: %v", err), nil + } + return fmt.Sprintf("Created task %d: %s", id, title), nil +} + func (r *Router) handleStatus(ctx context.Context) (string, error) { counts, err := r.Store.StatusCounts(ctx) if err != nil { diff --git a/internal/gateway/gateway_test.go b/internal/gateway/gateway_test.go index 06acdf936..7dc2355b2 100644 --- a/internal/gateway/gateway_test.go +++ b/internal/gateway/gateway_test.go @@ -273,3 +273,91 @@ func TestRouteModelNoManager(t *testing.T) { t.Errorf("reply = %q, want 'not configured' message", reply) } } + +// -- TaskCreator fakes for /spawn tests -- + +type fakeTaskCreator struct { + tasks []string + createFn func(ctx context.Context, title string) (int64, error) +} + +func (f *fakeTaskCreator) CreateTask(ctx context.Context, title string) (int64, error) { + f.tasks = append(f.tasks, title) + if f.createFn != nil { + return f.createFn(ctx, title) + } + return int64(len(f.tasks)), nil +} + +func TestRouteSpawnCreatesTask(t *testing.T) { + tc := &fakeTaskCreator{} + r := NewRouter(nil, nil, "test-gw") + r.Tasks = tc + reply, err := r.Route(context.Background(), Message{Text: "/spawn Fix the login bug"}) + if err != nil { + t.Fatalf("Route: %v", err) + } + if !strings.Contains(reply, "Created task") || !strings.Contains(reply, "1") { + t.Errorf("reply should confirm task creation: %q", reply) + } + if len(tc.tasks) != 1 || tc.tasks[0] != "Fix the login bug" { + t.Errorf("tasks = %v, want [Fix the login bug]", tc.tasks) + } +} + +func TestRouteSpawnAtMention(t *testing.T) { + tc := &fakeTaskCreator{} + r := NewRouter(nil, nil, "archie-bot") + r.Tasks = tc + reply, err := r.Route(context.Background(), Message{Text: "/spawn@archie-bot Deploy the release"}) + if err != nil { + t.Fatalf("Route: %v", err) + } + if !strings.Contains(reply, "Created task") { + t.Errorf("reply should confirm creation: %q", reply) + } +} + +func TestRouteSpawnNoTitle(t *testing.T) { + tc := &fakeTaskCreator{} + r := NewRouter(nil, nil, "test-gw") + r.Tasks = tc + reply, err := r.Route(context.Background(), Message{Text: "/spawn"}) + if err != nil { + t.Fatalf("Route: %v", err) + } + if !strings.Contains(reply, "Usage") { + t.Errorf("reply should show usage: %q", reply) + } + if len(tc.tasks) != 0 { + t.Errorf("no task should be created without a title") + } +} + +func TestRouteSpawnNoManager(t *testing.T) { + r := NewRouter(nil, nil, "test-gw") + reply, err := r.Route(context.Background(), Message{Text: "/spawn something"}) + if err != nil { + t.Fatalf("Route: %v", err) + } + if !strings.Contains(reply, "not configured") { + t.Errorf("reply = %q, want 'not configured'", reply) + } +} + +func TestRouteSpawnHandlesCreationError(t *testing.T) { + tc := &fakeTaskCreator{ + createFn: func(ctx context.Context, title string) (int64, error) { + return 0, fmt.Errorf("store unavailable") + }, + } + r := NewRouter(nil, nil, "test-gw") + r.Tasks = tc + reply, err := r.Route(context.Background(), Message{Text: "/spawn Break things"}) + if err != nil { + t.Fatalf("Route: %v", err) + } + if !strings.Contains(reply, "Failed") { + t.Errorf("reply should mention failure: %q", reply) + } +} diff --git a/internal/gateway/tasks.go b/internal/gateway/tasks.go new file mode 100644 index 000000000..e4e78c90f --- /dev/null +++ b/internal/gateway/tasks.go @@ -0,0 +1,48 @@ +package gateway + +import ( + "context" + "fmt" + "time" +) + +// StoreTaskCreator implements TaskCreator backed by a store interface. +// Chat-spawned tasks use a timestamp-based synthetic issue number to +// avoid colliding with Gitea-issued tasks. The first configured repo +// provides owner/name for the task row. +type StoreTaskCreator struct { + store storeWriter + owner string + repo string +} + +// storeWriter is the write surface StoreTaskCreator needs. +type storeWriter interface { + EnqueueIssue(ctx context.Context, owner, repo string, number int, title, body, labels string) (bool, error) +} + +// NewStoreTaskCreator returns a TaskCreator that enqueues chat-spawned +// tasks via the daemon's store. owner and repo identify the repo the +// task will work against (typically the identity's first repo). When +// empty, /spawn returns a configuration error. +func NewStoreTaskCreator(sw storeWriter, owner, repo string) *StoreTaskCreator { + return &StoreTaskCreator{store: sw, owner: owner, repo: repo} +} + +func (c *StoreTaskCreator) CreateTask(ctx context.Context, title string) (int64, error) { + if c.owner == "" || c.repo == "" { + return 0, fmt.Errorf("no repo configured for chat-spawned tasks") + } + // Synthetic issue number — prevents collisions with real Gitea issues + // (which are small sequential ints) while staying within the store's + // existing (owner, repo, number) uniqueness constraint. + number := int(time.Now().UnixNano()) + _, err := c.store.EnqueueIssue(ctx, c.owner, c.repo, number, title, "", "chat") + if err != nil { + return 0, err + } + // The store doesn't return the task ID from EnqueueIssue — ClaimByIssue + // gives us back the row. For the chat reply, the synthetic number is + // all the user needs to reference the task. + return int64(number), nil +} diff --git a/internal/gateway/tasks_test.go b/internal/gateway/tasks_test.go new file mode 100644 index 000000000..b0d35bbc7 --- /dev/null +++ b/internal/gateway/tasks_test.go @@ -0,0 +1,60 @@ +package gateway + +import ( + "context" + "strings" + "testing" +) + +type fakeStoreWriter struct { + owner, repo string + numbers []int + titles []string +} + +func (f *fakeStoreWriter) EnqueueIssue(ctx context.Context, owner, repo string, number int, title, body, labels string) (bool, error) { + f.owner = owner + f.repo = repo + f.numbers = append(f.numbers, number) + f.titles = append(f.titles, title) + return true, nil +} + +func TestStoreTaskCreatorCreatesTask(t *testing.T) { + sw := &fakeStoreWriter{} + tc := NewStoreTaskCreator(sw, "sam", "tau") + id, err := tc.CreateTask(context.Background(), "Fix the login bug") + if err != nil { + t.Fatalf("CreateTask: %v", err) + } + if id == 0 { + t.Error("task id should be non-zero") + } + if sw.owner != "sam" || sw.repo != "tau" { + t.Errorf("owner/repo = %s/%s, want sam/tau", sw.owner, sw.repo) + } + if sw.titles[0] != "Fix the login bug" { + t.Errorf("title = %q", sw.titles[0]) + } + if len(sw.numbers) != 1 { + t.Errorf("expected 1 enqueue, got %d", len(sw.numbers)) + } +} + +func TestStoreTaskCreatorRejectsEmptyRepo(t *testing.T) { + tc := NewStoreTaskCreator(&fakeStoreWriter{}, "", "") + _, err := tc.CreateTask(context.Background(), "something") + if err == nil || !strings.Contains(err.Error(), "no repo configured") { + t.Errorf("err = %v, want 'no repo configured'", err) + } +} + +func TestStoreTaskCreatorSyntheticNumbersDiffer(t *testing.T) { + sw := &fakeStoreWriter{} + tc := NewStoreTaskCreator(sw, "sam", "tau") + id1, _ := tc.CreateTask(context.Background(), "first") + id2, _ := tc.CreateTask(context.Background(), "second") + if id1 == id2 { + t.Errorf("synthetic numbers collided: %d", id1) + } +}