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 }, } }