From abcb72a76aee686d6baf587cc9afee62f6877742 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 3 Nov 2026 02:35:06 +1100 Subject: [PATCH] feat(curator): dry-run self-intake pass with run inspection tools (#1770) Curators could only reach the workflow/integration tools; there was no way to survey task runs. Adds run_list/run_events to the shared registry (guarded as read on task kind), attaches the instance's acting principal to curator model calls so guarded tools authorize, and records a pass's final text as a pass.summary action so the report is inspectable. Ships a dry-run self-intake definition in dev.toml/config.example.toml declaring only those two tools, so it cannot file issues yet. --- config.example.toml | 15 ++ deployments/dev.toml | 11 ++ internal/app/archiegateway/curators.go | 40 ++++- internal/app/archiegateway/tools.go | 3 + internal/domain/curator/definition_engine.go | 14 +- .../domain/curator/definition_engine_test.go | 28 ++++ internal/gateway/run_tools.go | 137 ++++++++++++++++++ 7 files changed, 246 insertions(+), 2 deletions(-) create mode 100644 internal/gateway/run_tools.go diff --git a/config.example.toml b/config.example.toml index 3b15e178e..5b2d11082 100644 --- a/config.example.toml +++ b/config.example.toml @@ -94,3 +94,18 @@ target = "127.0.0.1:9090" # releases_key = "base64 public key" # catalogue = "registry.example/owner/archipelago/catalogue" # catalogue_key = "base64 public key" + +# Long-running agent loops that maintain memory, skills and — in dry run — +# file follow-up issues from recent runs. Definitions are data: the server +# validates them and the gateway applies them live. +# [[curators]] +# name = "self-intake" +# interval = "1h" +# tools = ["run_list", "run_events"] +# instructions = """ +# You are archie's self-intake curator, in dry run. Each pass: list recent +# task runs with run_list, inspect anything parked or failed with run_events, +# then report candidate follow-up issues — concrete, each tied to the run +# that produced it. Do not create issues; this pass reports only (no +# issue-creating tool is declared, so it cannot). +# """ diff --git a/deployments/dev.toml b/deployments/dev.toml index baaf84d2f..b505b4bcf 100644 --- a/deployments/dev.toml +++ b/deployments/dev.toml @@ -45,3 +45,14 @@ listen = "127.0.0.1:8585" [services.daemon] target = "127.0.0.1:8587" listen = "127.0.0.1:8587" + +# Dry-run self-intake curator (#1770): surveys recent runs and reports +# candidate follow-up issues as pass summaries; declares no issue-creating +# tool, so it cannot file. Needs a chat model configured to run at all. +[[curators]] +name = "self-intake" +interval = "1h" +tools = ["run_list", "run_events"] +instructions = """ +You are archie's self-intake curator, in dry run. Each pass: list recent task runs with run_list, inspect anything parked or failed with run_events, then report candidate follow-up issues — concrete, each tied to the run that produced it. Do not create issues; this pass reports only (no issue-creating tool is declared, so it cannot). End your reply with the report. +""" diff --git a/internal/app/archiegateway/curators.go b/internal/app/archiegateway/curators.go index 0b0352030..9b776ceb2 100644 --- a/internal/app/archiegateway/curators.go +++ b/internal/app/archiegateway/curators.go @@ -12,7 +12,9 @@ import ( "github.com/samcharles93/archie-core/internal/agentexec/modelloop" "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/domain/access" "github.com/samcharles93/archie-core/internal/domain/curator" + "github.com/samcharles93/archie-core/internal/domain/org" "github.com/samcharles93/archie-core/internal/domain/usage" "github.com/samcharles93/archie-core/internal/events" "github.com/samcharles93/archie-core/internal/infrastructure/controlplanestore" @@ -40,7 +42,7 @@ func (b *server) setupCurators(ctx context.Context) bool { MemoryEngines: b.memEngines, Skills: skillcurator.NewStore(skillsRoot), Conversations: sessioncurator.NewAdapter(b.chatSessionStore, b.cfg.BotUser), - LLM: curatorLLMRunner{llm: b.chatLLM, outcomes: b.providerOutcomes, usage: b.usage}, + LLM: actingPrincipalRunner{inner: curatorLLMRunner{llm: b.chatLLM, outcomes: b.providerOutcomes, usage: b.usage}, resolve: b.actingPrincipal}, Model: model, }) @@ -95,6 +97,42 @@ func (s curatorEventSink) Emit(kind, detail string, data map[string]any) { s.b.Publish(context.Background(), events.Event{Kind: kind, Detail: detail, Data: data}) } +// actingPrincipal resolves the identity the instance acts as for automated +// work (the default chat identity, falling back to the bot user), so curator +// passes authorize guarded tools exactly as that identity would. +func (b *server) actingPrincipal(ctx context.Context) (access.Principal, error) { + name := b.defaultChatIdentity + if name == "" { + name = b.cfg.BotUser + } + if name == "" { + return access.Principal{}, fmt.Errorf("no acting identity configured (chat identity or bot user)") + } + idn, err := b.stateStore.ResolveLegacyName(ctx, name) + if err != nil { + return access.Principal{}, fmt.Errorf("resolve acting identity %q: %w", name, err) + } + return b.stateStore.PrincipalFor(ctx, idn.ID) +} + +// actingPrincipalRunner attaches the acting principal to every curator model +// call. Tool handlers run in the call's context, so guarded tools see the +// curator as the instance's identity, never as an anonymous or caller-supplied +// one; a pass that cannot resolve the principal fails closed. +type actingPrincipalRunner struct { + inner curator.LLMRunner + resolve func(context.Context) (access.Principal, error) +} + +func (r actingPrincipalRunner) Chat(ctx context.Context, req curator.ChatRequest) (curator.ChatResult, error) { + p, err := r.resolve(ctx) + if err != nil { + return curator.ChatResult{}, fmt.Errorf("curator pass: %w", err) + } + ctx = org.WithScope(org.WithOrg(access.WithPrincipal(ctx, p), p.Org), p.Org) + return r.inner.Chat(ctx, req) +} + // curatorLLMRunner adapts the shared ai-sdk runtime to the curator // family's narrow LLMRunner contract: one model reference, plain // messages, and the declared tool set -- no streaming. Tools are built diff --git a/internal/app/archiegateway/tools.go b/internal/app/archiegateway/tools.go index 1b2a26d7c..791857dca 100644 --- a/internal/app/archiegateway/tools.go +++ b/internal/app/archiegateway/tools.go @@ -109,6 +109,9 @@ func (b *server) registerWorkflowTools(ctx context.Context) { authz := infraaccess.StartLive(ctx, b.stateStore.ListPolicies, b.stateStore.PoliciesVersion, b.log) workflows := controlplanerpc.NewWorkflows(b.stateStore.ControlPlane()) entries := append(gateway.WorkflowTools(workflows, authz), gateway.IntegrationTools(integrationSource{Workflows: workflows, store: b.stateStore}, authz)...) + // Run-inspection tools back long-running curators (self-intake first); + // the State Store client satisfies the source directly. + entries = append(entries, gateway.RunTools(b.stateStore, authz)...) for _, entry := range entries { if err := b.toolReg.Register(entry); err != nil { b.log.WarnContext(ctx, "workflow tool registration failed", "tool", entry.Name, "err", err) diff --git a/internal/domain/curator/definition_engine.go b/internal/domain/curator/definition_engine.go index 9eedcc5e2..cceb54ba7 100644 --- a/internal/domain/curator/definition_engine.go +++ b/internal/domain/curator/definition_engine.go @@ -13,6 +13,10 @@ import ( // registry's activity surface and events. const ActionToolCalled = "tool.called" +// ActionSummary is the Action.Type recorded for the pass's final text: the +// report a definition pass produces, kept inspectable in the activity trail. +const ActionSummary = "pass.summary" + // defaultDefinitionMaxSteps bounds the model/tool loop of one definition pass. const defaultDefinitionMaxSteps = 10 @@ -84,7 +88,7 @@ func (e *DefinitionEngine) Pass(ctx context.Context, in PassInput) (PassResult, return PassResult{}, err } - actions := make([]Action, 0, len(res.ToolCalls)) + actions := make([]Action, 0, len(res.ToolCalls)+1) for _, call := range res.ToolCalls { actions = append(actions, Action{ At: e.host.Clock.Now(), @@ -93,6 +97,14 @@ func (e *DefinitionEngine) Pass(ctx context.Context, in PassInput) (PassResult, Reason: in.Reason, }) } + if summary := strings.TrimSpace(res.Text); summary != "" { + actions = append(actions, Action{ + At: e.host.Clock.Now(), + Type: ActionSummary, + Detail: summary, + Reason: in.Reason, + }) + } return PassResult{Actions: actions}, nil } diff --git a/internal/domain/curator/definition_engine_test.go b/internal/domain/curator/definition_engine_test.go index 6037c0ed2..59bbbc93b 100644 --- a/internal/domain/curator/definition_engine_test.go +++ b/internal/domain/curator/definition_engine_test.go @@ -139,3 +139,31 @@ func TestInstructionOnlyRegistrarViewFiltered(t *testing.T) { t.Error("registrar view has no clock; time is always bound") } } + +// TestDefinitionPassRecordsSummary pins that a pass's final text is recorded +// as a pass.summary action, so a definition's report stays inspectable in the +// activity trail instead of being dropped when the model returns. +func TestDefinitionPassRecordsSummary(t *testing.T) { + llm := &fakeLLM{} + registry := NewRegistry(fullHost(llm)) + engine := NewDefinitionEngine(instructionOnlyDefinition()) + if err := registry.Register(engine); err != nil { + t.Fatal(err) + } + res, err := engine.Pass(t.Context(), PassInput{Reason: "test"}) + if err != nil { + t.Fatalf("pass: %v", err) + } + var summary *Action + for i, a := range res.Actions { + if a.Type == ActionSummary { + summary = &res.Actions[i] + } + } + if summary == nil { + t.Fatalf("no %s action recorded; actions: %+v", ActionSummary, res.Actions) + } + if summary.Detail != "done" { + t.Errorf("summary detail = %q, want the model's final text %q", summary.Detail, "done") + } +} diff --git a/internal/gateway/run_tools.go b/internal/gateway/run_tools.go new file mode 100644 index 000000000..f059f351b --- /dev/null +++ b/internal/gateway/run_tools.go @@ -0,0 +1,137 @@ +package gateway + +import ( + "context" + "fmt" + "time" + + "github.com/samcharles93/archie-core/internal/domain/access" + "github.com/samcharles93/archie-core/internal/domain/workflow/task" + "github.com/samcharles93/archie-core/internal/events" + "github.com/samcharles93/archie-core/internal/tools" +) + +// RunSource is the state-store surface the run-inspection tools use. The +// State Store client satisfies it directly. +type RunSource interface { + // Tasks returns the most recently updated tasks. + Tasks(ctx context.Context, limit int) ([]task.Task, error) + // TaskEvents returns one task's events, oldest first. + TaskEvents(ctx context.Context, taskID int64) ([]events.Event, error) +} + +// RunSummary is one task as run_list shows it. +type RunSummary struct { + ID int64 `json:"id"` + Repo string `json:"repo"` + Title string `json:"title"` + Status string `json:"status"` + Workflow string `json:"workflow,omitempty"` + Issue int `json:"issue,omitempty"` + PRNumber int `json:"pr_number,omitempty"` + ParkReason string `json:"park_reason,omitempty"` + UpdatedAt time.Time `json:"updated_at"` +} + +// RunEvent is one task event as run_events shows it. +type RunEvent struct { + At time.Time `json:"at"` + Kind string `json:"kind"` + Stage string `json:"stage,omitempty"` + Detail string `json:"detail,omitempty"` +} + +// RunTools returns the read-only run-inspection tools for long-running +// curators: what ran recently, and each run's event trail. Reads are checked +// as reading tasks, the same check the dashboard's task list gets. Nil +// source or authorizer registers none. +func RunTools(source RunSource, authz access.Authorizer) []tools.ToolEntry { + if source == nil || authz == nil { + return nil + } + read := func(entry tools.ToolEntry) tools.ToolEntry { + return guarded(authz, access.ActionRead, access.KindTask, nil, entry) + } + return []tools.ToolEntry{ + read(runListTool(source)), + read(runEventsTool(source)), + } +} + +func runListTool(source RunSource) tools.ToolEntry { + return tools.ToolEntry{ + Name: "run_list", + Toolset: "runs", + Description: "List the most recently updated task runs across the instance, most recent first. " + + "Use it to survey what ran, what parked and why.", + Classification: tools.ClassIdempotent, + Schema: tools.JSONSchema{ + "type": "object", + "properties": map[string]any{ + "limit": map[string]any{ + "type": "integer", + "description": fmt.Sprintf( + "Maximum runs to return. Defaults to %d, capped at %d.", + defaultTaskListLimit, maxTaskListLimit, + ), + }, + }, + }, + Handler: func(ctx context.Context, input map[string]any) (any, error) { + rows, err := source.Tasks(ctx, taskListLimit(input)) + if err != nil { + return nil, fmt.Errorf("run_list: %w", err) + } + out := make([]RunSummary, 0, len(rows)) + for _, t := range rows { + out = append(out, RunSummary{ + ID: t.ID, + Repo: t.Owner + "/" + t.Repo, + Title: t.Title, + Status: t.Status, + Workflow: t.Workflow, + Issue: t.IssueNumber, + PRNumber: t.PRNumber, + ParkReason: t.ParkReason, + UpdatedAt: t.UpdatedAt, + }) + } + return out, nil + }, + } +} + +func runEventsTool(source RunSource) tools.ToolEntry { + return tools.ToolEntry{ + Name: "run_events", + Toolset: "runs", + Description: "List one task run's event trail, oldest first: stage transitions, deliveries, " + + "failures and park reasons. Use it to see why a run ended the way it did.", + Classification: tools.ClassIdempotent, + Schema: tools.JSONSchema{ + "type": "object", + "properties": map[string]any{ + "task_id": map[string]any{ + "type": "integer", + "description": "The task run's ID, as run_list reports it.", + }, + }, + "required": []string{"task_id"}, + }, + Handler: func(ctx context.Context, input map[string]any) (any, error) { + id, ok := asInt64(input["task_id"]) + if !ok || id <= 0 { + return nil, fmt.Errorf("run_events: task_id must be a positive integer") + } + evs, err := source.TaskEvents(ctx, id) + if err != nil { + return nil, fmt.Errorf("run_events: %w", err) + } + out := make([]RunEvent, 0, len(evs)) + for _, e := range evs { + out = append(out, RunEvent{At: e.At, Kind: e.Kind, Stage: e.Stage, Detail: e.Detail}) + } + return out, nil + }, + } +}