Fix/workflow eda audit (#1627)

* fix(webui): accept the merge task action

The action whitelist omitted merge, so the dashboard's Merge button on a
pr_open task was refused with 400 unknown action.

Closes: archie-core-slpoz

* fix(workflow): give each parallel branch its own outputs

Branches shared the task's Outputs map, so concurrent declared-output
writes raced (or were lost when the map started nil). Each branch now
writes a copy; join merges new outputs in branch-name order and refuses
one written by two branches as a second write.

Closes: archie-core-3a7w2

* fix(postgres): dispatch captures oldest first and keep pending ones

ListUndispatchedCaptures ordered newest first and listed captures no
binding can match (failed HMAC), so sustained intake starved older work
and bad signatures held the batch. It is now oldest first and lists only
dispatchable captures. Count retention no longer evicts a capture that
is still pending for an armed binding. The daemon lists only sources
with an enabled workflow, so captures waiting on a disabled one do not
hold the oldest-first batch.

Closes: archie-core-2et4b, archie-core-o0s0z

* fix(ui): refuse renaming a workflow onto an existing id

Saving upserts by id, so editing a workflow's id to another workflow's
id removed the open one and overwrote the other. The editor now refuses
that save and says why.

Closes: archie-core-wpoz8

* fix(workflow): reject unknown step settings at save

Step factories decoded settings with yaml.Node.Decode, which drops keys
it does not know, and settings-less steps ignored settings entirely, so a
misspelt key (readOnly for read_only) saved and silently did nothing.
Every factory now decodes strictly and settings-less steps refuse
settings; ParseDefinition runs the factories, so the save refuses them.

Closes: archie-core-8b5nk

* fix(postgres): keep a stored capture when its prune fails

InsertCapture committed the capture, then returned the prune's error, so
intake answered 500 and a sender without a delivery header retried into
a duplicate capture and a duplicate dispatch. A failed prune is now
logged and the stored capture's id returned.

Closes: archie-core-3miuh

* fix(crondelivery): fail and advance a job no runner can deliver

Router.Run returned nil for a kind with no runner, so the engine reported
a successful run while the schedule never advanced and the job fired on
every tick. The run now fails with the refusal, which the engine reports,
and the schedule advances (a one-shot is removed). The router's own
refusal event is gone with it.

Closes: archie-core-s6ahh

* fix(taskactions): stop the runs of cancelled callees

CancelExecution cancelled a caller's waited-on callees in the store, but
only the caller's run was stopped, so callee containers ran to the end.
CancelExecution now returns the callees it cancelled (nested included)
in place of the step ids nobody read, and every cancelling action stops
their runs too.

Closes: archie-core-9labz

* fix(workflow): treat a zero json.Number as false in when

Task inputs and step results decode numbers as json.Number, which truthy
did not unpack, so when: inputs.count held for a count of 0.

Closes: archie-core-uv270

* fix(webui): read a workflow's runs with a workflow-filtered page

The workflows page and the canvas run picker filtered the newest 100
tasks of every workflow client-side, so on a busy instance a workflow's
older runs were invisible. GET /api/tasks takes a workflow filter, carried
through the State Store's Tasks request to the query, and both views ask
for the selected workflow's runs.

Closes: archie-core-tpiw1

* fix(ui): load the restored workflow into the editor

Restore Defaults saved the shipped definition but left the open draft in
the editor, still marked unsaved, so a later save re-applied it. The
editor now reloads the restored definition.

Closes: archie-core-85e7w
This commit is contained in:
sam authored and GitHub committed 2026-10-12 03:20:00 +11:00
1 parent 678f8ec815
commit af1a5f0b89
36 files changed
+592 -148

No files matched your search

+1 -1
View File
@@ -76,7 +76,7 @@ func (b *boot) setupScheduling() error {
}
runners[scheduling.KindWorkflow] = workflowRunner
}
router, err := crondelivery.NewRouter(store, runners, cfg.Events)
router, err := crondelivery.NewRouter(store, runners)
if err != nil {
return fmt.Errorf("scheduling: build router: %w", err)
}
+1 -1
View File
@@ -25,5 +25,5 @@ type eventCaptureStore interface { //nolint:interfacebloat // composite of the s
// openEDAStore serves the event-capture store from the State Store's Postgres
// pool, sealing binding secrets with cipher.
func (b *server) openEDAStore(cipher bindingcipher.BindingCipher) {
b.eda = postgres.NewEDA(b.pg, cipher)
b.eda = postgres.NewEDA(b.pg, cipher, b.log)
}
+25 -13
View File
@@ -4631,10 +4631,12 @@ func (x *CancelExecutionRequest) GetTo() string {
type CancelExecutionResponse struct {
state protoimpl.MessageState `protogen:"open.v1"`
// cancelled_step_ids are the StepExecutions the transaction cancelled.
CancelledStepIds []int64 `protobuf:"varint,1,rep,packed,name=cancelled_step_ids,json=cancelledStepIds,proto3" json:"cancelled_step_ids,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
// cancelled_callee_ids are the executions the caller was waiting on that
// were cancelled with it, nested callees included; the caller stops their
// runs as well as its own.
CancelledCalleeIds []int64 `protobuf:"varint,2,rep,packed,name=cancelled_callee_ids,json=cancelledCalleeIds,proto3" json:"cancelled_callee_ids,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *CancelExecutionResponse) Reset() {
@@ -4667,9 +4669,9 @@ func (*CancelExecutionResponse) Descriptor() ([]byte, []int) {
return file_state_v1_state_proto_rawDescGZIP(), []int{70}
}
func (x *CancelExecutionResponse) GetCancelledStepIds() []int64 {
func (x *CancelExecutionResponse) GetCancelledCalleeIds() []int64 {
if x != nil {
return x.CancelledStepIds
return x.CancelledCalleeIds
}
return nil
}
@@ -6664,8 +6666,10 @@ type TasksRequest struct {
Statuses []string `protobuf:"bytes,2,rep,name=statuses,proto3" json:"statuses,omitempty"`
AfterUpdatedAt *timestamppb.Timestamp `protobuf:"bytes,3,opt,name=after_updated_at,json=afterUpdatedAt,proto3" json:"after_updated_at,omitempty"`
AfterId int64 `protobuf:"varint,4,opt,name=after_id,json=afterId,proto3" json:"after_id,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
// workflow, when set, keeps only that workflow's tasks.
Workflow string `protobuf:"bytes,5,opt,name=workflow,proto3" json:"workflow,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *TasksRequest) Reset() {
@@ -6726,6 +6730,13 @@ func (x *TasksRequest) GetAfterId() int64 {
return 0
}
func (x *TasksRequest) GetWorkflow() string {
if x != nil {
return x.Workflow
}
return ""
}
type TasksResponse struct {
state protoimpl.MessageState `protogen:"open.v1"`
Tasks []*Task `protobuf:"bytes,1,rep,name=tasks,proto3" json:"tasks,omitempty"`
@@ -16837,9 +16848,9 @@ const file_state_v1_state_proto_rawDesc = "" +
"\x16CancelExecutionRequest\x12\x17\n" +
"\atask_id\x18\x01 \x01(\x03R\x06taskId\x12\x16\n" +
"\x06reason\x18\x02 \x01(\tR\x06reason\x12\x0e\n" +
"\x02to\x18\x03 \x01(\tR\x02to\"G\n" +
"\x17CancelExecutionResponse\x12,\n" +
"\x12cancelled_step_ids\x18\x01 \x03(\x03R\x10cancelledStepIds\"\xb0\x03\n" +
"\x02to\x18\x03 \x01(\tR\x02to\"e\n" +
"\x17CancelExecutionResponse\x120\n" +
"\x14cancelled_callee_ids\x18\x02 \x03(\x03R\x12cancelledCalleeIdsJ\x04\b\x01\x10\x02R\x12cancelled_step_ids\"\xb0\x03\n" +
"\rStepExecution\x12\x0e\n" +
"\x02id\x18\x01 \x01(\x03R\x02id\x12!\n" +
"\fexecution_id\x18\x02 \x01(\x03R\vexecutionId\x12\x18\n" +
@@ -16964,12 +16975,13 @@ const file_state_v1_state_proto_rawDesc = "" +
"\x05tasks\x18\x01 \x03(\v2\x0e.state.v1.TaskR\x05tasks\"\x1b\n" +
"\x19ClearTerminalTasksRequest\"2\n" +
"\x1aClearTerminalTasksResponse\x12\x14\n" +
"\x05count\x18\x01 \x01(\x03R\x05count\"\xa1\x01\n" +
"\x05count\x18\x01 \x01(\x03R\x05count\"\xbd\x01\n" +
"\fTasksRequest\x12\x14\n" +
"\x05limit\x18\x01 \x01(\x03R\x05limit\x12\x1a\n" +
"\bstatuses\x18\x02 \x03(\tR\bstatuses\x12D\n" +
"\x10after_updated_at\x18\x03 \x01(\v2\x1a.google.protobuf.TimestampR\x0eafterUpdatedAt\x12\x19\n" +
"\bafter_id\x18\x04 \x01(\x03R\aafterId\"5\n" +
"\bafter_id\x18\x04 \x01(\x03R\aafterId\x12\x1a\n" +
"\bworkflow\x18\x05 \x01(\tR\bworkflow\"5\n" +
"\rTasksResponse\x12$\n" +
"\x05tasks\x18\x01 \x03(\v2\x0e.state.v1.TaskR\x05tasks\"\x15\n" +
"\x13StatusCountsRequest\"\x95\x01\n" +
+9 -8
View File
@@ -598,16 +598,22 @@ func (d *Daemon) dispatchBindings(ctx context.Context) {
}
func (d *Daemon) dispatchOrgBindings(ctx context.Context) {
// Gather armed sources first so the captures query filters down to
// only relevant rows.
// Gather the sources of armed bindings whose workflow is enabled first, so
// the oldest-first captures query is not held by captures that wait on a
// disabled workflow.
bindings, err := d.Bindings.ListBindings(ctx)
if err != nil {
d.Log.Warn("list bindings failed", "error", err)
return
}
enablement, err := d.workflowEnablement(ctx)
if err != nil {
d.Log.Warn("binding dispatch: workflow enablement unavailable", "error", err)
return
}
sources := make([]string, 0, len(bindings))
for _, b := range bindings {
if b.OrgID == org.OrgFromContext(ctx) && b.Status == binding.StatusArmed && b.Matcher.Source != "" {
if b.OrgID == org.OrgFromContext(ctx) && b.Status == binding.StatusArmed && b.Matcher.Source != "" && enablement.Enabled(b.OrgID, b.Workflow) {
sources = append(sources, b.Matcher.Source)
}
}
@@ -625,11 +631,6 @@ func (d *Daemon) dispatchOrgBindings(ctx context.Context) {
d.Log.Warn("binding dispatch: workflow definitions unavailable", "error", err)
return
}
enablement, err := d.workflowEnablement(ctx)
if err != nil {
d.Log.Warn("binding dispatch: workflow enablement unavailable", "error", err)
return
}
for _, c := range captures {
d.dispatchCapture(ctx, c, workflows, enablement)
}
@@ -60,7 +60,9 @@ type StepRecorder interface {
}
// ExecutionCanceller cancels an execution and its current attempt's open
// steps in one transaction.
// steps in one transaction. It also cancels the callees the execution was
// waiting on, and returns their ids (nested callees included) so the caller
// can stop their runs too.
type ExecutionCanceller interface {
CancelExecution(ctx context.Context, taskID int64, reason, to string) ([]int64, error)
}
@@ -281,6 +283,8 @@ type DispatchLedger interface {
// after the task it names, in (updated_at, id) descending order.
type TaskPage struct {
Statuses []string
// Workflow, when set, keeps only that workflow's tasks.
Workflow string
After TaskCursor
Limit int
}
+24 -11
View File
@@ -189,7 +189,7 @@ type Store interface {
ArchiveTask(context.Context, int64, string, events.Event) (int64, error)
InsertEvent(context.Context, events.Event) (int64, error)
// CancelExecution cancels the execution and its open steps in one
// transaction.
// transaction, and returns the waited-on callees it cancelled with it.
CancelExecution(context.Context, int64, string, string) ([]int64, error)
}
@@ -256,10 +256,12 @@ func (s Service) Withdraw(ctx context.Context, id int64, reason string) error {
event := s.attributed(task, Actor{})
event.Kind, event.Detail = events.KindTaskWithdrawn, "issue withdrawn: "+reason
event.Data = map[string]any{"reason": reason}
if _, err := s.Store.CancelExecution(ctx, task.ID, event.Detail, declined); err != nil {
callees, err := s.Store.CancelExecution(ctx, task.ID, event.Detail, declined)
if err != nil {
return err
}
s.deliver(task.ID)
s.deliver(callees...)
s.emit(ctx, event)
return nil
}
@@ -295,10 +297,12 @@ func (s Service) apply(ctx context.Context, task *Task, actor Actor, action task
// A rejection ends the run's work wherever it sits, so it rides the
// one cancel path like every other operator stop.
o.event.Kind, o.event.Detail, o.verb = rejectedKind(actor), actor.describe("rejected"), "declined"
if _, err := s.Store.CancelExecution(ctx, task.ID, actor.describe("rejected"), declined); err != nil {
callees, err := s.Store.CancelExecution(ctx, task.ID, actor.describe("rejected"), declined)
if err != nil {
return o, err
}
s.deliver(task.ID)
s.deliver(callees...)
return o, nil
case taskstate.ActionCancel, taskstate.ActionAbandon:
return s.applyCancelOrAbandon(ctx, task, action, actor, o)
@@ -382,11 +386,14 @@ func selectedFindingKeys(gate workflowtask.ReviewGate, requested []string) ([]st
return selection, nil
}
// deliver cancels the in-memory context of a run the store has already recorded
// as cancelled.
func (s Service) deliver(taskID int64) {
if s.CancelTask != nil {
s.CancelTask(taskID)
// deliver cancels the in-memory contexts of runs the store has already
// recorded as cancelled.
func (s Service) deliver(taskIDs ...int64) {
if s.CancelTask == nil {
return
}
for _, id := range taskIDs {
s.CancelTask(id)
}
}
@@ -500,12 +507,14 @@ func (s Service) applyStop(ctx context.Context, task *Task, actor Actor, o outco
// context cancel that found nothing executing says so; the parked
// execution is recorded either way, and the task's own unwind cannot win
// the race any more -- its next step write is already stale.
if _, err := s.Store.CancelExecution(ctx, task.ID, o.event.Detail, parked); err != nil {
callees, err := s.Store.CancelExecution(ctx, task.ID, o.event.Detail, parked)
if err != nil {
return o, err
}
if !s.CancelTask(task.ID) {
o.event.Detail = "no active execution was found; recoverable work was parked, " + actor.describe("stopped")
}
s.deliver(callees...)
return o, nil
}
@@ -514,16 +523,20 @@ func (s Service) applyCancelOrAbandon(ctx context.Context, task *Task, action ta
// Abandoning ends archie's run, not the issue: no verb, so the issue
// stays open for a human.
o.event.Kind, o.event.Detail = events.KindTaskAbandoned, actor.describe("abandoned")
if _, err := s.Store.CancelExecution(ctx, task.ID, o.event.Detail, declined); err != nil {
callees, err := s.Store.CancelExecution(ctx, task.ID, o.event.Detail, declined)
if err != nil {
return o, err
}
s.deliver(callees...)
return o, nil
}
o.event.Kind, o.event.Detail, o.verb = events.KindTaskCancelled, actor.describe("cancelled"), "cancelled"
if _, err := s.Store.CancelExecution(ctx, task.ID, o.event.Detail, declined); err != nil {
callees, err := s.Store.CancelExecution(ctx, task.ID, o.event.Detail, declined)
if err != nil {
return o, err
}
s.deliver(task.ID)
s.deliver(callees...)
return o, nil
}
@@ -0,0 +1,62 @@
package taskactions
import (
"context"
"slices"
"testing"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/taskstate"
)
// cancelStore is a task store whose cancel also cancels one waited-on callee.
type cancelStore struct {
Store
task Task
callees []int64
}
func (f *cancelStore) TaskByID(context.Context, int64) (*Task, error) {
task := f.task
return &task, nil
}
func (f *cancelStore) CancelExecution(context.Context, int64, string, string) ([]int64, error) {
return f.callees, nil
}
func (f *cancelStore) InsertEvent(context.Context, events.Event) (int64, error) { return 1, nil }
// Every operator action that cancels a run must also stop the runs of the
// callees the store cancelled with it, or they run on to completion.
func TestCancellingActionsStopCallees(t *testing.T) {
tests := []struct {
status string
action taskstate.Action
}{
{status: taskstate.Queued, action: taskstate.ActionCancel},
{status: taskstate.Running, action: taskstate.ActionStop},
{status: taskstate.Running, action: taskstate.ActionReject},
{status: taskstate.Parked, action: taskstate.ActionAbandon},
}
for _, tt := range tests {
t.Run(string(tt.action), func(t *testing.T) {
var stopped []int64
s := Service{
Store: &cancelStore{task: Task{ID: 1, Status: tt.status}, callees: []int64{7, 9}},
CancelTask: func(id int64) bool {
stopped = append(stopped, id)
return true
},
}
if err := s.Apply(t.Context(), nil, Actor{}, 1, tt.action, ActionPayload{}); err != nil {
t.Fatal(err)
}
for _, callee := range []int64{7, 9} {
if !slices.Contains(stopped, callee) {
t.Fatalf("stopped %v, want callee %d among them", stopped, callee)
}
}
})
}
}
+1 -1
View File
@@ -55,7 +55,7 @@ func newAgentRunStage(settings yaml.Node) (Stage, error) {
if settings.Kind == 0 {
return Stage{}, fmt.Errorf("%s: settings.mission is required", AgentRunStepName)
}
if err := settings.Decode(&s); err != nil {
if err := decodeSettingsStrict(settings, &s); err != nil {
return Stage{}, fmt.Errorf("%s: %w", AgentRunStepName, err)
}
if err := s.check(); err != nil {
+1 -1
View File
@@ -55,7 +55,7 @@ func newCommandRunStage(settings yaml.Node) (Stage, error) {
return Stage{}, fmt.Errorf("%s: settings.run is required", CommandRunStepName)
}
var s commandRunSettings
if err := settings.Decode(&s); err != nil {
if err := decodeSettingsStrict(settings, &s); err != nil {
return Stage{}, fmt.Errorf("%s: %w", CommandRunStepName, err)
}
switch s.Level {
@@ -0,0 +1,38 @@
package workflow
import (
"testing"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
)
// TestConditionReadsDecodedInputs pins when: against inputs as a task carries
// them, decoded with numbers kept exact.
func TestConditionReadsDecodedInputs(t *testing.T) {
tests := []struct {
when, inputs string
want bool
}{
{when: "inputs.count", inputs: `{"count": 0}`, want: false},
{when: "inputs.count", inputs: `{"count": 0.0}`, want: false},
{when: "inputs.count", inputs: `{"count": 3}`, want: true},
{when: "!inputs.count", inputs: `{"count": 0}`, want: true},
{when: "inputs.flag", inputs: `{"flag": false}`, want: false},
{when: "inputs.missing", inputs: `{}`, want: false},
}
for _, tt := range tests {
t.Run(tt.when+" "+tt.inputs, func(t *testing.T) {
inputs, err := task.DecodeInputs(tt.inputs)
if err != nil {
t.Fatal(err)
}
c, err := parseCondition(tt.when)
if err != nil {
t.Fatal(err)
}
if got := c.holds(&TaskContext{Task: &task.Task{Inputs: inputs}}); got != tt.want {
t.Fatalf("holds = %v, want %v", got, tt.want)
}
})
}
}
+10 -1
View File
@@ -2,6 +2,7 @@ package workflow
import (
"context"
"encoding/json"
"fmt"
"maps"
"slices"
@@ -79,7 +80,10 @@ func newApproveStage(settings yaml.Node) (Stage, error) {
}}, nil
}
func newCloseIssueStage(yaml.Node) (Stage, error) {
func newCloseIssueStage(settings yaml.Node) (Stage, error) {
if err := noSettings(CloseIssueStepName, settings); err != nil {
return Stage{}, err
}
return Stage{Name: CloseIssueStepName, Run: func(ctx context.Context, tc *TaskContext) error {
if !tc.Task.IsForgeBacked() {
return nil
@@ -167,6 +171,11 @@ func truthy(value any) bool {
return v != 0
case int:
return v != 0
case json.Number:
// Inputs and results decode numbers exactly; one too large for a
// float is not zero.
f, err := v.Float64()
return err != nil || f != 0
}
return true
}
+3 -5
View File
@@ -29,11 +29,9 @@ func FinishStepType() StepType {
}
func newFinishStage(settings yaml.Node) (Stage, error) {
var s finishSettings
if settings.Kind != 0 {
if err := settings.Decode(&s); err != nil {
return Stage{}, fmt.Errorf("%s: %w", FinishStepName, err)
}
s, err := decodeSettings[finishSettings](FinishStepName, settings)
if err != nil {
return Stage{}, err
}
if s.Status == "" {
s.Status = "completed"
+13 -2
View File
@@ -29,7 +29,10 @@ func RepoStepTypes() []StepType {
{Name: RepoCommitStepName, Factory: newRepoCommitStage, Settings: repoCommitSettings{}},
{Name: RepoOpenPRStepName, Factory: newRepoOpenPRStage, Settings: repoOpenPRSettings{}},
{Name: GateRepositoryName, Factory: newGateRepositoryStage, Settings: gateRepositorySettings{}},
{Name: GateDiffSizeStepName, Factory: func(yaml.Node) (Stage, error) {
{Name: GateDiffSizeStepName, Factory: func(settings yaml.Node) (Stage, error) {
if err := noSettings(GateDiffSizeStepName, settings); err != nil {
return Stage{}, err
}
stage := StageDiffCap()
stage.Name = GateDiffSizeStepName
return stage, nil
@@ -43,12 +46,20 @@ func decodeSettings[T any](step string, settings yaml.Node) (T, error) {
if settings.Kind == 0 {
return s, nil
}
if err := settings.Decode(&s); err != nil {
if err := decodeSettingsStrict(settings, &s); err != nil {
return s, fmt.Errorf("%s: %w", step, err)
}
return s, nil
}
// noSettings refuses settings on a step type that reads none.
func noSettings(step string, settings yaml.Node) error {
if _, err := decodeSettings[struct{}](step, settings); err != nil {
return fmt.Errorf("%s takes no settings", step)
}
return nil
}
type repoPrepareSettings struct {
// Branch is "fresh" (the default, a new branch from the base) or "task",
// the branch an earlier run of this task pushed.
+8 -2
View File
@@ -29,7 +29,10 @@ func ReviewStepTypes() []StepType {
// intake, anatomy, lenses, review, precision gate, verification, coverage,
// synthesis, operator approval, merge gate and output. Its phases share
// private state, so they run as one step.
func newReviewPRStage(yaml.Node) (Stage, error) {
func newReviewPRStage(settings yaml.Node) (Stage, error) {
if err := noSettings(ReviewPRStepName, settings); err != nil {
return Stage{}, err
}
phases := prReviewStandaloneStages()
return Stage{Name: ReviewPRStepName, Run: func(ctx context.Context, tc *TaskContext) error {
for _, phase := range phases {
@@ -47,7 +50,10 @@ func newReviewPRStage(yaml.Node) (Stage, error) {
// newReviewRoundStage opens one remediation round: it refuses a review
// payload that does not decode, parks with a PR comment once the repository's
// round cap is reached, and otherwise counts the round.
func newReviewRoundStage(yaml.Node) (Stage, error) {
func newReviewRoundStage(settings yaml.Node) (Stage, error) {
if err := noSettings(ReviewRoundStepName, settings); err != nil {
return Stage{}, err
}
check, roundCap := StageCheckReviewPayload(), StageRemediationRoundCap()
return Stage{Name: ReviewRoundStepName, Run: func(ctx context.Context, tc *TaskContext) error {
if err := check.Run(ctx, tc); err != nil {
+26 -6
View File
@@ -337,6 +337,7 @@ func compileParallel(step StepRecord, registry StepRegistry) (Stage, error) {
return wrapStep(step, func(ctx context.Context, tc *TaskContext) error {
clones := make([]*TaskContext, len(branches))
errs := make([]error, len(branches))
before := maps.Clone(tc.Task.Outputs)
var wg sync.WaitGroup
for i := range branches {
clones[i] = tc.branch(names[i])
@@ -344,7 +345,7 @@ func compileParallel(step StepRecord, registry StepRegistry) (Stage, error) {
}
wg.Wait()
for i, clone := range clones {
tc.join(clone)
errs[i] = errors.Join(errs[i], tc.join(clone, before))
if errs[i] != nil {
errs[i] = fmt.Errorf("branch %q: %w", names[i], errs[i])
}
@@ -423,13 +424,14 @@ func branchStepName(branch, name string) string {
}
// branch is a copy of the run's context for one parallel branch. The task is
// copied too, with its usage counters zeroed, so join can add what the branch
// spent without the branches racing on one task.
// copied too, with its usage counters zeroed and its own outputs, so join can
// add what the branch spent and wrote without the branches racing on one task.
func (tc *TaskContext) branch(name string) *TaskContext {
clone := *tc
clone.workflowBranch = name
taskCopy := *tc.Task
taskCopy.TokensUsed, taskCopy.Iterations = 0, 0
taskCopy.Outputs = maps.Clone(tc.Task.Outputs)
clone.Task = &taskCopy
clone.RunUsage = agentrun.Usage{}
clone.Outcome = Outcome{}
@@ -438,9 +440,26 @@ func (tc *TaskContext) branch(name string) *TaskContext {
return &clone
}
// join folds a finished branch back into the run: its step results, what it
// spent, and its outcome when the run has none yet.
func (tc *TaskContext) join(branch *TaskContext) {
// join folds a finished branch back into the run: its step results, the
// outputs it wrote beyond those it started with, what it spent, and its
// outcome when the run has none yet. An output an earlier-joined branch also
// wrote is refused, as a second write is; branches join in name order, so the
// refusal does not depend on which finished first.
func (tc *TaskContext) join(branch *TaskContext, before map[string]any) error {
var err error
for _, name := range slices.Sorted(maps.Keys(branch.Task.Outputs)) {
if _, inherited := before[name]; inherited {
continue
}
if _, written := tc.Task.Outputs[name]; written {
err = errors.Join(err, fmt.Errorf("output %q has already been written; each output is written once per attempt", name))
continue
}
if tc.Task.Outputs == nil {
tc.Task.Outputs = map[string]any{}
}
tc.Task.Outputs[name] = branch.Task.Outputs[name]
}
for id, result := range branch.stepResults {
if _, ok := tc.stepResults[id]; !ok {
if tc.stepResults == nil {
@@ -455,4 +474,5 @@ func (tc *TaskContext) join(branch *TaskContext) {
if tc.Outcome.Status == "" {
tc.Outcome = branch.Outcome
}
return err
}
@@ -5,6 +5,9 @@ import (
"errors"
"fmt"
"log/slog"
"maps"
"strings"
"sync"
"testing"
"gopkg.in/yaml.v3"
@@ -151,3 +154,93 @@ steps:
})
}
}
// lockedStepStore serialises the step writes parallel branches make.
type lockedStepStore struct {
mu sync.Mutex
fakeStepStore
}
func (f *lockedStepStore) StartStep(ctx context.Context, s task.StepStart) (int64, events.Event, error) {
f.mu.Lock()
defer f.mu.Unlock()
return f.fakeStepStore.StartStep(ctx, s)
}
func (f *lockedStepStore) FinishStep(ctx context.Context, s task.StepFinish) (events.Event, error) {
f.mu.Lock()
defer f.mu.Unlock()
return f.fakeStepStore.FinishStep(ctx, s)
}
// TestParallelBranchesWriteOutputs pins how parallel branches write declared
// outputs: each branch writes its own, and two branches writing one output is
// refused as a second write would be, whatever order the branches finish in.
func TestParallelBranchesWriteOutputs(t *testing.T) {
src := `id: fan
repository: none
outputs:
a: {type: string}
b: {type: string}
c: {type: string}
z: {type: string}
steps:
- parallel:
one: [{type: agent.run, settings: {read_only: true, names: [a, c]}}]
two: [{type: agent.run, settings: {read_only: true, names: [%s]}}]
`
tests := []struct {
name, two, wantErr string
before, want map[string]any
}{
{name: "distinct outputs", two: "b", want: map[string]any{"a": "one", "b": "two", "c": "one"}},
{
name: "distinct outputs beside an earlier one", two: "b",
before: map[string]any{"z": "before"},
want: map[string]any{"a": "one", "b": "two", "c": "one", "z": "before"},
},
{name: "one output from two branches", two: "b, c", wantErr: `output "c" has already been written`},
{name: "a branch rewriting an earlier output", two: "z", before: map[string]any{"z": "before"}, wantErr: `output "z" has already been written`},
}
// A fake read-only agent.run, the step type a branch may run.
registry := StepRegistry{AgentRunStepName: func(node yaml.Node) (Stage, error) {
var s struct{ Names []string }
if err := node.Decode(&s); err != nil {
return Stage{}, err
}
return Stage{Run: func(_ context.Context, tc *TaskContext) error {
for _, name := range s.Names {
for range 100 {
tc.workflowInterface()
}
if err := tc.WriteOutput(name, tc.workflowBranch); err != nil {
return err
}
}
return nil
}}, nil
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
wf, err := ParseAndCompile(fmt.Sprintf(src, tt.two), registry)
if err != nil {
t.Fatal(err)
}
tc := &TaskContext{Task: &task.Task{ID: 1, Attempt: 1, Outputs: maps.Clone(tt.before)}, Store: &lockedStepStore{}, Log: slog.Default()}
tc.runInterface, tc.runInterfaceSet = wf.Interface, true
err = wf.Stages[0].Run(context.Background(), tc)
if tt.wantErr != "" {
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("err = %v, want %q", err, tt.wantErr)
}
return
}
if err != nil {
t.Fatal(err)
}
if !maps.Equal(tc.Task.Outputs, tt.want) {
t.Fatalf("outputs = %v, want %v", tc.Task.Outputs, tt.want)
}
})
}
}
@@ -0,0 +1,47 @@
package workflow
import (
"fmt"
"strings"
"testing"
)
// TestParseDefinitionRejectsUnknownStepSettings pins the step contract at
// save: a settings key the step type does not read is refused, so a misspelt
// key cannot be dropped silently.
func TestParseDefinitionRejectsUnknownStepSettings(t *testing.T) {
types := []StepType{AgentRunStepType(), CommandRunStepType(), DiffRulesStepType(), FinishStepType(), WorkflowCallStepType()}
types = append(types, ControlStepTypes()...)
types = append(types, RepoStepTypes()...)
types = append(types, ReviewStepTypes()...)
registry := StepRegistry{}
for _, st := range types {
registry[st.Name] = st.Factory
}
tests := []struct {
name, step, wantErr string
}{
{name: "agent.run", step: `{type: agent.run, settings: {mission: m, readOnly: true}}`, wantErr: "readOnly"},
{name: "command.run", step: `{type: command.run, settings: {run: [{name: build, argv: [make]}], ifEmpty: pass}}`, wantErr: "ifEmpty"},
{name: "workflow.finish", step: `{type: workflow.finish, settings: {status: completed, reason: r}}`, wantErr: "reason"},
{name: "workflow.call", step: `{id: c, type: workflow.call, settings: {workflow: other, wiat: true}}`, wantErr: "wiat"},
{name: "repo.prepare", step: `{type: repo.prepare, settings: {brnach: task}}`, wantErr: "brnach"},
{name: "a step that takes no settings", step: `{type: forge.close-issue, settings: {comment: done}}`, wantErr: "takes no settings"},
{name: "known settings", step: `{type: agent.run, settings: {mission: m, read_only: true}}`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
src := fmt.Sprintf("id: w\nrepository: required\nsteps:\n - %s\n", tt.step)
_, err := ParseDefinition(src, registry)
if tt.wantErr == "" {
if err != nil {
t.Fatalf("err = %v, want none", err)
}
return
}
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("err = %v, want one naming %q", err, tt.wantErr)
}
})
}
}
+1 -1
View File
@@ -79,7 +79,7 @@ func newWorkflowCallStage(settings yaml.Node) (Stage, error) {
if settings.Kind == 0 {
return Stage{}, fmt.Errorf("%s: settings.workflow is required", WorkflowCallStepName)
}
if err := settings.Decode(&s); err != nil {
if err := decodeSettingsStrict(settings, &s); err != nil {
return Stage{}, fmt.Errorf("%s: %w", WorkflowCallStepName, err)
}
if strings.TrimSpace(s.Workflow) == "" {
+12 -37
View File
@@ -8,32 +8,26 @@ import (
"time"
"github.com/samcharles93/archie-core/internal/domain/scheduling"
"github.com/samcharles93/archie-core/internal/events"
)
// phaseDispatch is the phase reported for a dispatch refusal; it matches the
// scheduling engine's value.
const phaseDispatch = "dispatch"
// Router runs a due job with the runner for its kind and records successful
// runs. An unknown kind is reported as a dispatch KindJobError and Run
// returns nil.
// Router runs a due job with the runner for its kind and advances its
// schedule. A job whose kind has no runner fails its run and still advances,
// so it is reported once per firing rather than on every tick.
type Router struct {
specs RouterStore
runners map[string]scheduling.Runner
sink scheduling.Sink
}
// NewRouter builds a router over specs and runners. A nil sink disables
// events. An empty kind is KindChat.
func NewRouter(specs RouterStore, runners map[string]scheduling.Runner, sink scheduling.Sink) (*Router, error) {
// NewRouter builds a router over specs and runners. An empty kind is
// KindChat.
func NewRouter(specs RouterStore, runners map[string]scheduling.Runner) (*Router, error) {
if specs == nil {
return nil, errors.New("crondelivery: spec lookup must not be nil")
}
if len(runners) == 0 {
return nil, errors.New("crondelivery: at least one runner is required")
}
return &Router{specs: specs, runners: runners, sink: sink}, nil
return &Router{specs: specs, runners: runners}, nil
}
// Run implements scheduling.Runner: hydrate, resolve the kind, dispatch.
@@ -52,8 +46,10 @@ func (r *Router) Run(ctx context.Context, job scheduling.Job) error {
}
runner, ok := r.runners[kind]
if !ok {
r.refuse(job, kind)
return nil
// The kind is in the message: the operator's next step is to add a
// runner for it.
refused := fmt.Errorf("crondelivery: no runner registered for job kind %q", strings.TrimSpace(kind))
return errors.Join(refused, r.recordRun(ctx, job))
}
if err := runner.Run(ctx, job); err != nil {
return err
@@ -61,7 +57,7 @@ func (r *Router) Run(ctx context.Context, job scheduling.Job) error {
return r.recordRun(ctx, job)
}
// recordRun advances the job's schedule after a successful run.
// recordRun advances the job's schedule.
// ErrScheduleUnsupported (a one-shot) is ignored.
func (r *Router) recordRun(ctx context.Context, job scheduling.Job) error {
if err := r.specs.MarkRun(ctx, job.ID, time.Now()); err != nil && !errors.Is(err, scheduling.ErrScheduleUnsupported) {
@@ -69,24 +65,3 @@ func (r *Router) recordRun(ctx context.Context, job scheduling.Job) error {
}
return nil
}
// refuse reports a job whose kind has no runner. The data map matches the
// engine's own error shape — {"job", "pool", "phase", "err"} — so one consumer
// renders a dispatch refusal and a failed run without a special case, and the
// detail is the job's human-readable label, exactly as the engine sets it.
func (r *Router) refuse(job scheduling.Job, kind string) {
if r.sink == nil {
return
}
r.sink.Emit(events.KindJobError, job.Detail, map[string]any{
"job": job.ID,
"pool": string(job.Pool),
"phase": phaseDispatch,
// The kind is in the message, not a bare "unknown job": the
// operator's next step is to add a runner mapping for it.
"err": fmt.Sprintf("crondelivery: no runner registered for job kind %q",
strings.TrimSpace(kind)),
})
}
var _ scheduling.Runner = (*Router)(nil)
@@ -0,0 +1,55 @@
package crondelivery
import (
"context"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/domain/scheduling"
)
type fakeRouterStore struct {
spec scheduling.JobSpec
marked int
}
func (f *fakeRouterStore) Get(context.Context, string) (scheduling.JobSpec, bool, error) {
return f.spec, true, nil
}
func (f *fakeRouterStore) MarkRun(context.Context, string, time.Time) error {
f.marked++
return nil
}
type okRunner struct{}
func (okRunner) Run(context.Context, scheduling.Job) error { return nil }
// A job no runner can deliver must fail its run, so the engine does not
// report it as run, and still advance, so it does not fire on every tick.
func TestRouterAdvancesEveryJob(t *testing.T) {
tests := []struct {
kind string
wantErr bool
}{
{kind: scheduling.KindChat},
{kind: scheduling.KindWorkflow, wantErr: true},
}
for _, tt := range tests {
t.Run(tt.kind, func(t *testing.T) {
store := &fakeRouterStore{spec: scheduling.JobSpec{ID: "j", Kind: tt.kind}}
router, err := NewRouter(store, map[string]scheduling.Runner{scheduling.KindChat: okRunner{}})
if err != nil {
t.Fatal(err)
}
err = router.Run(t.Context(), scheduling.Job{ID: "j"})
if (err != nil) != tt.wantErr {
t.Fatalf("err = %v, want error %v", err, tt.wantErr)
}
if store.marked != 1 {
t.Fatalf("schedule advanced %d times, want 1", store.marked)
}
})
}
}
+11 -7
View File
@@ -9,6 +9,7 @@ import (
"encoding/json"
"errors"
"fmt"
"log/slog"
"time"
"github.com/google/uuid"
@@ -42,13 +43,14 @@ type EDA struct {
q *postgresdb.Queries
cipher bindingcipher.BindingCipher
notify func(events.Event)
log *slog.Logger
}
// NewEDA builds an EDA store over pool. A nil cipher keeps source secrets in
// plaintext; the caller resolves the keyring through
// bindingcipher.NewBindingCipher. The pool is owned by the caller, not this store.
func NewEDA(pool *pgxpool.Pool, cipher bindingcipher.BindingCipher) *EDA {
return &EDA{pool: pool, q: postgresdb.New(pool), cipher: cipher}
func NewEDA(pool *pgxpool.Pool, cipher bindingcipher.BindingCipher, log *slog.Logger) *EDA {
return &EDA{pool: pool, q: postgresdb.New(pool), cipher: cipher, log: log}
}
// SetNotify wires the change-event callback. nil is a silent no-op, which is
@@ -75,7 +77,8 @@ func newRecordID() string { return uuid.NewString() }
// InsertCapture stores one inbound event verbatim, tagged with the event type
// it is identified as on arrival (empty when unidentified). retention and maxEvents are
// applied as a prune after the insert.
// applied as a prune after the insert; a failed prune is logged, not returned,
// because the capture is already stored and a retry would store it again.
func (s *EDA) InsertCapture(ctx context.Context, c storecontract.CapturedEvent, retention time.Duration, maxEvents int) (string, error) {
received := c.ReceivedAt
if received.IsZero() {
@@ -108,7 +111,7 @@ func (s *EDA) InsertCapture(ctx context.Context, c storecontract.CapturedEvent,
return "", fmt.Errorf("edastore: insert capture: %w", err)
}
if err := s.pruneCaptures(ctx, id, retention, maxEvents); err != nil {
return "", err
s.log.Warn("capture stored but not pruned", "capture", id, "err", err)
}
return id, nil
}
@@ -162,9 +165,10 @@ func (s *EDA) ListCaptures(ctx context.Context, limit int) ([]storecontract.Capt
return out, nil
}
// ListUndispatchedCaptures returns recent identified captures for the given
// sources that some armed binding for their event type has not dispatched yet.
// An unidentified capture is never returned, so it never dispatches.
// ListUndispatchedCaptures returns the oldest dispatchable, identified
// captures for the given sources that some armed binding for their event type
// has not dispatched yet. An unidentified capture is never returned, so it
// never dispatches.
func (s *EDA) ListUndispatchedCaptures(ctx context.Context, sources []string, limit int) ([]storecontract.CapturedEvent, error) {
if len(sources) == 0 || limit <= 0 {
return nil, nil
@@ -162,7 +162,15 @@ WHERE c.org_id = (SELECT m.org_id FROM captures AS m WHERE m.id = $1)
WHERE k.org_id = (SELECT m.org_id FROM captures AS m WHERE m.id = $1)
ORDER BY k.received_at DESC
LIMIT $2
)
)
AND NOT (
c.event_type <> '' AND (c.authenticated OR c.unsigned)
AND EXISTS (
SELECT 1 FROM bindings b JOIN mappings m ON m.id = b.mapping
WHERE b.org_id = c.org_id AND b.source = c.source AND b.status = 'armed' AND m.event_type = c.event_type
AND NOT EXISTS (SELECT 1 FROM binding_dispatches d WHERE d.binding = b.id AND d.delivery = c.delivery)
)
)
`
type DeleteCapturesBeyondCountParams struct {
@@ -171,7 +179,8 @@ type DeleteCapturesBeyondCountParams struct {
}
// Count retention stays within the just-inserted capture's org: one org's
// volume must never evict another org's recent captures.
// volume must never evict another org's recent captures. It never evicts a
// capture ListUndispatchedCaptures would still offer: that is accepted work.
func (q *Queries) DeleteCapturesBeyondCount(ctx context.Context, arg DeleteCapturesBeyondCountParams) error {
_, err := q.db.Exec(ctx, deleteCapturesBeyondCount, arg.CaptureID, arg.MaxEvents)
return err
@@ -994,12 +1003,13 @@ SELECT c.id, c.source, c.remote_addr, c.content_type, c.headers, c.body, c.authe
FROM captures c
WHERE c.org_id = $1 AND c.source = ANY($2::text[])
AND c.event_type <> ''
AND (c.authenticated OR c.unsigned)
AND EXISTS (
SELECT 1 FROM bindings b JOIN mappings m ON m.id = b.mapping
WHERE b.org_id = c.org_id AND b.source = c.source AND b.status = 'armed' AND m.event_type = c.event_type
AND NOT EXISTS (SELECT 1 FROM binding_dispatches d WHERE d.binding = b.id AND d.delivery = c.delivery)
)
ORDER BY c.received_at DESC
ORDER BY c.received_at, c.id
LIMIT $3
`
@@ -1009,6 +1019,9 @@ type ListUndispatchedCapturesParams struct {
EntryLimit int32
}
// Oldest first, so sustained intake cannot starve accepted work. Only
// dispatchable captures (signed, or from an approved unsigned source) are
// listed: no binding ever matches the rest, so they would hold the batch.
func (q *Queries) ListUndispatchedCaptures(ctx context.Context, arg ListUndispatchedCapturesParams) ([]Capture, error) {
rows, err := q.db.Query(ctx, listUndispatchedCaptures, arg.OrgID, arg.Sources, arg.EntryLimit)
if err != nil {
@@ -585,15 +585,17 @@ SELECT id, owner, repo, issue_number, title, status, workflow,
FROM tasks
WHERE ($1::text IS NULL OR org_id = $1)
AND (cardinality($2::text[]) = 0 OR status = ANY($2::text[]))
AND ($3::timestamptz IS NULL
OR (updated_at, id) < ($3::timestamptz, $4::bigint))
AND ($3::text = '' OR workflow = $3::text)
AND ($4::timestamptz IS NULL
OR (updated_at, id) < ($4::timestamptz, $5::bigint))
ORDER BY updated_at DESC, id DESC
LIMIT $5
LIMIT $6
`
type ListTaskSummariesParams struct {
ScopeOrg pgtype.Text
Statuses []string
Workflow string
BeforeUpdated pgtype.Timestamptz
BeforeID int64
PageLimit int32
@@ -638,6 +640,7 @@ func (q *Queries) ListTaskSummaries(ctx context.Context, arg ListTaskSummariesPa
rows, err := q.db.Query(ctx, listTaskSummaries,
arg.ScopeOrg,
arg.Statuses,
arg.Workflow,
arg.BeforeUpdated,
arg.BeforeID,
arg.PageLimit,
@@ -14,16 +14,20 @@ ORDER BY received_at DESC
LIMIT $1;
-- name: ListUndispatchedCaptures :many
-- Oldest first, so sustained intake cannot starve accepted work. Only
-- dispatchable captures (signed, or from an approved unsigned source) are
-- listed: no binding ever matches the rest, so they would hold the batch.
SELECT c.id, c.source, c.remote_addr, c.content_type, c.headers, c.body, c.authenticated, c.received_at, c.unsigned, c.event_type, c.org_id, c.workspace_id, c.delivery
FROM captures c
WHERE c.org_id = @org_id AND c.source = ANY(@sources::text[])
AND c.event_type <> ''
AND (c.authenticated OR c.unsigned)
AND EXISTS (
SELECT 1 FROM bindings b JOIN mappings m ON m.id = b.mapping
WHERE b.org_id = c.org_id AND b.source = c.source AND b.status = 'armed' AND m.event_type = c.event_type
AND NOT EXISTS (SELECT 1 FROM binding_dispatches d WHERE d.binding = b.id AND d.delivery = c.delivery)
)
ORDER BY c.received_at DESC
ORDER BY c.received_at, c.id
LIMIT @entry_limit;
-- name: DeleteCapturesOlderThan :exec
@@ -31,7 +35,8 @@ DELETE FROM captures WHERE received_at < $1;
-- name: DeleteCapturesBeyondCount :exec
-- Count retention stays within the just-inserted capture's org: one org's
-- volume must never evict another org's recent captures.
-- volume must never evict another org's recent captures. It never evicts a
-- capture ListUndispatchedCaptures would still offer: that is accepted work.
DELETE FROM captures AS c
WHERE c.org_id = (SELECT m.org_id FROM captures AS m WHERE m.id = sqlc.arg(capture_id))
AND c.id NOT IN (
@@ -39,7 +44,15 @@ WHERE c.org_id = (SELECT m.org_id FROM captures AS m WHERE m.id = sqlc.arg(captu
WHERE k.org_id = (SELECT m.org_id FROM captures AS m WHERE m.id = sqlc.arg(capture_id))
ORDER BY k.received_at DESC
LIMIT sqlc.arg(max_events)
);
)
AND NOT (
c.event_type <> '' AND (c.authenticated OR c.unsigned)
AND EXISTS (
SELECT 1 FROM bindings b JOIN mappings m ON m.id = b.mapping
WHERE b.org_id = c.org_id AND b.source = c.source AND b.status = 'armed' AND m.event_type = c.event_type
AND NOT EXISTS (SELECT 1 FROM binding_dispatches d WHERE d.binding = b.id AND d.delivery = c.delivery)
)
);
-- name: InsertMapping :exec
INSERT INTO mappings (id, name, source_hint, event_type, fields, org_id)
@@ -47,6 +47,7 @@ SELECT id, owner, repo, issue_number, title, status, workflow,
FROM tasks
WHERE (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org))
AND (cardinality(@statuses::text[]) = 0 OR status = ANY(@statuses::text[]))
AND (@workflow::text = '' OR workflow = @workflow::text)
AND (sqlc.narg(before_updated)::timestamptz IS NULL
OR (updated_at, id) < (sqlc.narg(before_updated)::timestamptz, @before_id::bigint))
ORDER BY updated_at DESC, id DESC
+16 -15
View File
@@ -282,7 +282,9 @@ func stepParentID(parentID int64) pgtype.Int8 {
}
// CancelExecution cancels the current attempt's open steps and moves the
// execution to `to`, in one transaction with events and an audit row.
// execution to `to`, in one transaction with events and an audit row, then
// cancels the callees it was waiting on. It returns the cancelled callees'
// ids, nested callees included.
func (s *Store) CancelExecution(ctx context.Context, taskID int64, reason, to string) ([]int64, error) {
tx, err := s.pool.Begin(ctx)
if err != nil {
@@ -344,25 +346,24 @@ func (s *Store) CancelExecution(ctx context.Context, taskID int64, reason, to st
if err := tx.Commit(ctx); err != nil {
return nil, err
}
ids := make([]int64, 0, len(steps))
for _, step := range steps {
ids = append(ids, step.ID)
}
if err := s.cancelWaitedCallees(ctx, waiting, reason, to); err != nil {
return ids, err
}
return ids, nil
return s.cancelWaitedCallees(ctx, waiting, reason, to)
}
// cancelWaitedCallees cancels each callee the caller was waiting on.
// Failures are reported; the caller's cancel stands.
func (s *Store) cancelWaitedCallees(ctx context.Context, waiting []postgresdb.WaitingCallStepsRow, reason, to string) error {
// cancelWaitedCallees cancels each callee the caller was waiting on and
// returns the ids of those it cancelled, nested callees included. Failures are
// reported; the caller's cancel stands.
func (s *Store) cancelWaitedCallees(ctx context.Context, waiting []postgresdb.WaitingCallStepsRow, reason, to string) ([]int64, error) {
var cancelled []int64
var failures []error
for _, call := range waiting {
if _, err := s.CancelExecution(ctx, call.CalledExecutionID, reason, to); err != nil {
// A callee is returned even when its cancel failed: stopping a run
// that already ended does nothing.
nested, err := s.CancelExecution(ctx, call.CalledExecutionID, reason, to)
if err != nil {
failures = append(failures, fmt.Errorf("cancel callee %d: %w", call.CalledExecutionID, err))
}
cancelled = append(cancelled, call.CalledExecutionID)
cancelled = append(cancelled, nested...)
}
return errors.Join(failures...)
return cancelled, errors.Join(failures...)
}
@@ -737,6 +737,7 @@ func (s *Store) TasksPage(ctx context.Context, page storecontract.TaskPage) ([]w
params := postgresdb.ListTaskSummariesParams{
ScopeOrg: scopeOrg(ctx),
Statuses: append([]string{}, page.Statuses...),
Workflow: page.Workflow,
PageLimit: int32(min(max(page.Limit, 1), 500)), // clamped
}
if page.After.ID != 0 {
+5 -4
View File
@@ -189,14 +189,15 @@ func (c *Client) StartStep(ctx context.Context, s task.StepStart) (int64, events
// CancelExecution is the one cancel path:
// the State Store records the cancellation -- every non-terminal step of the
// current attempt plus the execution's own move -- and returns the steps it
// cancelled; the caller cancels the run's in-memory context afterwards.
// current attempt plus the execution's own move, and its waited-on callees --
// and returns the callees it cancelled; the caller cancels the runs'
// in-memory contexts afterwards.
func (c *Client) CancelExecution(ctx context.Context, taskID int64, reason, to string) ([]int64, error) {
r, err := c.client.CancelExecution(ctx, &pb.CancelExecutionRequest{TaskId: taskID, Reason: reason, To: to})
if err != nil {
return nil, unmapError(err)
}
return r.CancelledStepIds, nil
return r.CancelledCalleeIds, nil
}
func (c *Client) FinishStep(ctx context.Context, s task.StepFinish) (events.Event, error) {
@@ -348,7 +349,7 @@ func (c *Client) Tasks(ctx context.Context, limit int) ([]task.Task, error) {
// TasksPage returns one page of tasks, most recently updated first.
func (c *Client) TasksPage(ctx context.Context, page storecontract.TaskPage) ([]task.Task, error) {
r, err := c.client.Tasks(ctx, &pb.TasksRequest{
Limit: int64(page.Limit), Statuses: page.Statuses,
Limit: int64(page.Limit), Statuses: page.Statuses, Workflow: page.Workflow,
AfterUpdatedAt: timestamp(page.After.UpdatedAt), AfterId: page.After.ID,
})
if err != nil {
+5 -4
View File
@@ -332,8 +332,9 @@ func (s *server) CallerModels(ctx context.Context, _ *pb.CallerModelsRequest) (*
}
// CancelExecution is the one cancel path:
// the store records the cancellation and returns the steps it cancelled; the
// caller delivers it by cancelling the run's in-memory context afterwards.
// the store records the cancellation and returns the callees it cancelled
// with it; the caller delivers it by cancelling the runs' in-memory contexts
// afterwards.
func (s *server) CancelExecution(ctx context.Context, r *pb.CancelExecutionRequest) (*pb.CancelExecutionResponse, error) {
if s.deps.Canceller == nil {
return nil, status.Error(codes.Unavailable, "execution canceller unavailable")
@@ -342,7 +343,7 @@ func (s *server) CancelExecution(ctx context.Context, r *pb.CancelExecutionReque
if err != nil {
return nil, s.logErr("CancelExecution", err)
}
return &pb.CancelExecutionResponse{CancelledStepIds: ids}, nil
return &pb.CancelExecutionResponse{CancelledCalleeIds: ids}, nil
}
func (s *server) FinishStep(ctx context.Context, r *pb.FinishStepRequest) (*pb.FinishStepResponse, error) {
@@ -517,7 +518,7 @@ func (s *server) ClearTerminalTasks(ctx context.Context, _ *pb.ClearTerminalTask
func (s *server) Tasks(ctx context.Context, r *pb.TasksRequest) (*pb.TasksResponse, error) {
tasks, err := s.deps.Tasks.TasksPage(ctx, storecontract.TaskPage{
Statuses: r.Statuses, Limit: int(r.Limit),
Statuses: r.Statuses, Workflow: r.Workflow, Limit: int(r.Limit),
After: storecontract.TaskCursor{UpdatedAt: timeValue(r.AfterUpdatedAt), ID: r.AfterId},
})
if err != nil {
+2 -2
View File
@@ -95,7 +95,7 @@ const defaultTaskPage = 100
// taskPageFrom reads the task list's query parameters.
func taskPageFrom(q url.Values) (storecontract.TaskPage, error) {
page := storecontract.TaskPage{Limit: defaultTaskPage}
page := storecontract.TaskPage{Limit: defaultTaskPage, Workflow: q.Get("workflow")}
switch status := q.Get("status"); status {
case "":
case "needs_you":
@@ -320,7 +320,7 @@ func taskMutation(action taskstate.Action) bool {
switch action {
case taskstate.ActionCancel, taskstate.ActionStop, taskstate.ActionApprove,
taskstate.ActionReject, taskstate.ActionRereview, taskstate.ActionRetry,
taskstate.ActionAbandon, taskstate.ActionArchive:
taskstate.ActionAbandon, taskstate.ActionArchive, taskstate.ActionMerge:
return true
default:
return false
+34
View File
@@ -0,0 +1,34 @@
package webui
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// The dashboard renders every action the task's status offers, so the handler
// must accept each one the action service applies.
func TestDecodeTaskActionAcceptsServiceActions(t *testing.T) {
tests := []struct {
action string
want int
}{
{action: "merge", want: http.StatusOK},
{action: "archive", want: http.StatusOK},
{action: "open_pr", want: http.StatusBadRequest},
{action: "bogus", want: http.StatusBadRequest},
}
for _, tc := range tests {
t.Run(tc.action, func(t *testing.T) {
r := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/api/tasks/1/actions", strings.NewReader(`{"action":"`+tc.action+`"}`))
w := httptest.NewRecorder()
if _, _, ok := decodeTaskAction(w, r); ok {
w.WriteHeader(http.StatusOK)
}
if w.Code != tc.want {
t.Fatalf("status = %d, want %d (%s)", w.Code, tc.want, strings.TrimSpace(w.Body.String()))
}
})
}
}
+8 -2
View File
@@ -752,8 +752,12 @@ message CancelExecutionRequest {
string to = 3;
}
message CancelExecutionResponse {
// cancelled_step_ids are the StepExecutions the transaction cancelled.
repeated int64 cancelled_step_ids = 1;
reserved 1;
reserved "cancelled_step_ids";
// cancelled_callee_ids are the executions the caller was waiting on that
// were cancelled with it, nested callees included; the caller stops their
// runs as well as its own.
repeated int64 cancelled_callee_ids = 2;
}
// StepExecution mirrors internal/domain/workflow/task.StepExecution.
@@ -925,6 +929,8 @@ message TasksRequest {
repeated string statuses = 2;
google.protobuf.Timestamp after_updated_at = 3;
int64 after_id = 4;
// workflow, when set, keeps only that workflow's tasks.
string workflow = 5;
}
message TasksResponse { repeated Task tasks = 1; }
+1 -1
View File
@@ -147,4 +147,4 @@ Edge: ${e}
Source: ${t}
Target: ${n}`,EDGE_ORPHANED:e=>`Edge was orphaned (suddenly missing source or target) and has been removed
Edge: ${e}`,EDGE_NOT_FOUND:e=>`Edge not found
Edge: ${e}`,USEVUEFLOW_OPTIONS:()=>`The options parameter is deprecated and will be removed in the next major version. Please use the id parameter instead`},_$=class extends Error{constructor(e,...t){super(g$[e]?.call(g$,...t)),this.name=`VueFlowError`,this.code=e,this.args=t}};function v$(e){return`clientX`in e}function y$(e){return`sourceEvent`in e}function b$(e,t){let n=v$(e),r,i;return n?(r=e.clientX,i=e.clientY):`touches`in e&&e.touches.length>0?(r=e.touches[0].clientX,i=e.touches[0].clientY):`changedTouches`in e&&e.changedTouches.length>0?(r=e.changedTouches[0].clientX,i=e.changedTouches[0].clientY):(r=0,i=0),{x:r-(t?.left??0),y:i-(t?.top??0)}}var x$=()=>typeof navigator<`u`&&(navigator==null?void 0:navigator.userAgent)?.indexOf(`Mac`)>=0;function S$(e){return{width:e.dimensions?.width??e.width??0,height:e.dimensions?.height??e.height??0}}function C$(e,t=[1,1]){return{x:t[0]*Math.round(e.x/t[0]),y:t[1]*Math.round(e.y/t[1])}}var w$=()=>!0;function T$(e){e?.classList.remove(`valid`,`connecting`,`vue-flow__handle-valid`,`vue-flow__handle-connecting`)}function E$(e,t,n){let r=[],i={x:e.x-n,y:e.y-n,width:n*2,height:n*2};for(let e of t.values())pQ(i,fQ(e))>0&&r.push(e);return r}var D$=250;function O$(e,t,n,r){let i=[],a=1/0,o=E$(e,n,t+D$);for(let n of o){let o=[...n.handleBounds?.source??[],...n.handleBounds?.target??[]];for(let s of o){if(r.nodeId===s.nodeId&&r.type===s.type&&r.id===s.id)continue;let{x:o,y:c}=d$(n,s,s.position,!0),l=Math.sqrt((o-e.x)**2+(c-e.y)**2);l>t||(l<a?(i=[{...s,x:o,y:c}],a=l):l===a&&i.push({...s,x:o,y:c}))}}if(!i.length)return null;if(i.length>1){let e=r.type===`source`?`target`:`source`;return i.find(t=>t.type===e)??i[0]}return i[0]}function k$(e,{handle:t,connectionMode:n,fromNodeId:r,fromHandleId:i,fromType:a,doc:o,lib:s,flowId:c,isValidConnection:l=w$},u,d,f,p){let m=a===`target`,h=t?o.querySelector(`.${s}-flow__handle[data-id="${c}-${t?.nodeId}-${t?.id}-${t?.type}"]`):null,{x:g,y:_}=b$(e),v=o.elementFromPoint(g,_),y=v?.classList.contains(`${s}-flow__handle`)?v:h,b={handleDomNode:y,isValid:!1,connection:null,toHandle:null};if(y){let e=A$(void 0,y),t=y.getAttribute(`data-nodeid`),a=y.getAttribute(`data-handleid`),o=y.classList.contains(`connectable`),s=y.classList.contains(`connectableend`);if(!t||!e)return b;let c={source:m?t:r,sourceHandle:m?a:i,target:m?r:t,targetHandle:m?i:a};b.connection=c,b.isValid=o&&s&&(n===YZ.Strict?m&&e===`source`||!m&&e===`target`:t!==r||a!==i)&&l(c,{nodes:d,edges:u,sourceNode:f(c.source),targetNode:f(c.target)}),b.toHandle=N$(t,e,a,p,n,!0)}return b}function A$(e,t){return e||(t?.classList.contains(`target`)?`target`:t?.classList.contains(`source`)?`source`:null)}function j$(e,t){let n=null;return t?n=`valid`:e&&!t&&(n=`invalid`),n}function M$(e,t){let n=null;return t?n=!0:e&&!t&&(n=!1),n}function N$(e,t,n,r,i,a=!1){let o=r.get(e);if(!o)return null;let s=i===YZ.Strict?o.handleBounds?.[t]:[...o.handleBounds?.source??[],...o.handleBounds?.target??[]],c=(n?s?.find(e=>e.id===n):s?.[0])??null;return c&&a?{...c,...d$(o,c,c.position,!0)}:c}var P$={[KZ.Left]:KZ.Right,[KZ.Right]:KZ.Left,[KZ.Top]:KZ.Bottom,[KZ.Bottom]:KZ.Top},F$=[`production`,`prod`];function I$(e,...t){L$()&&console.warn(`[Vue Flow]: ${e}`,...t)}function L$(){return!F$.includes(`production`)}function R$(e,t,n,r,i){let a=t.querySelectorAll(`.vue-flow__handle.${e}`);return a?.length?Array.from(a).map(t=>{let a=t.getBoundingClientRect();return{id:t.getAttribute(`data-handleid`),type:e,nodeId:i,position:t.getAttribute(`data-handlepos`),x:(a.left-n.left)/r,y:(a.top-n.top)/r,...mQ(t)}}):null}function z$(e,t,n,r,i,a=!1,o){i.value=!1,e.selected?(a||e.selected&&t)&&(r([e]),ft(()=>{o.blur()})):n([e])}function B$(e){return z(e)!==void 0}function V$(e,t,n,r){if(!e||!e.source||!e.target)return n(new _$(h$.EDGE_INVALID,e?.id??`[ID UNKNOWN]`)),!1;let i;return i=vQ(e)?e:{...e,id:OQ(e)},i=wQ(i,void 0,r),!kQ(i,t)&&i}function H$(e,t,n,r,i){if(!t.source||!t.target)return i(new _$(h$.EDGE_INVALID,e.id)),!1;if(!n)return i(new _$(h$.EDGE_NOT_FOUND,e.id)),!1;let{id:a,...o}=e;return{...o,id:r?OQ(t):a,source:t.source,target:t.target,sourceHandle:t.sourceHandle,targetHandle:t.targetHandle}}function U$(e,t,n){let r={},i=[];for(let a=0;a<e.length;++a){let o=e[a];if(!yQ(o)){n(new _$(h$.NODE_INVALID,o?.id));continue}let s=CQ(o,t(o.id),o.parentNode);o.parentNode&&(r[o.parentNode]=!0),i[a]=s}for(let e of i){let a=t(e.parentNode)||i.find(t=>t.id===e.parentNode);e.parentNode&&!a&&n(new _$(h$.NODE_MISSING_PARENT,e.id,e.parentNode)),(e.parentNode||r[e.id])&&(r[e.id]&&(e.isParent=!0),a&&(a.isParent=!0))}return i}function W$(e,t,n,r,i,a){let o=i,s=r.get(o)||new Map;r.set(o,s.set(n,t)),o=`${i}-${e}`;let c=r.get(o)||new Map;if(r.set(o,c.set(n,t)),a){o=`${i}-${e}-${a}`;let s=r.get(o)||new Map;r.set(o,s.set(n,t))}}function G$(e,t,n){e.clear();for(let t of n){let{source:n,target:r,sourceHandle:i=null,targetHandle:a=null}=t,o={edgeId:t.id,source:n,target:r,sourceHandle:i,targetHandle:a},s=`${n}-${i}--${r}-${a}`;W$(`source`,o,`${r}-${a}--Line truncated
Edge: ${e}`,USEVUEFLOW_OPTIONS:()=>`The options parameter is deprecated and will be removed in the next major version. Please use the id parameter instead`},_$=class extends Error{constructor(e,...t){super(g$[e]?.call(g$,...t)),this.name=`VueFlowError`,this.code=e,this.args=t}};function v$(e){return`clientX`in e}function y$(e){return`sourceEvent`in e}function b$(e,t){let n=v$(e),r,i;return n?(r=e.clientX,i=e.clientY):`touches`in e&&e.touches.length>0?(r=e.touches[0].clientX,i=e.touches[0].clientY):`changedTouches`in e&&e.changedTouches.length>0?(r=e.changedTouches[0].clientX,i=e.changedTouches[0].clientY):(r=0,i=0),{x:r-(t?.left??0),y:i-(t?.top??0)}}var x$=()=>typeof navigator<`u`&&(navigator==null?void 0:navigator.userAgent)?.indexOf(`Mac`)>=0;function S$(e){return{width:e.dimensions?.width??e.width??0,height:e.dimensions?.height??e.height??0}}function C$(e,t=[1,1]){return{x:t[0]*Math.round(e.x/t[0]),y:t[1]*Math.round(e.y/t[1])}}var w$=()=>!0;function T$(e){e?.classList.remove(`valid`,`connecting`,`vue-flow__handle-valid`,`vue-flow__handle-connecting`)}function E$(e,t,n){let r=[],i={x:e.x-n,y:e.y-n,width:n*2,height:n*2};for(let e of t.values())pQ(i,fQ(e))>0&&r.push(e);return r}var D$=250;function O$(e,t,n,r){let i=[],a=1/0,o=E$(e,n,t+D$);for(let n of o){let o=[...n.handleBounds?.source??[],...n.handleBounds?.target??[]];for(let s of o){if(r.nodeId===s.nodeId&&r.type===s.type&&r.id===s.id)continue;let{x:o,y:c}=d$(n,s,s.position,!0),l=Math.sqrt((o-e.x)**2+(c-e.y)**2);l>t||(l<a?(i=[{...s,x:o,y:c}],a=l):l===a&&i.push({...s,x:o,y:c}))}}if(!i.length)return null;if(i.length>1){let e=r.type===`source`?`target`:`source`;return i.find(t=>t.type===e)??i[0]}return i[0]}function k$(e,{handle:t,connectionMode:n,fromNodeId:r,fromHandleId:i,fromType:a,doc:o,lib:s,flowId:c,isValidConnection:l=w$},u,d,f,p){let m=a===`target`,h=t?o.querySelector(`.${s}-flow__handle[data-id="${c}-${t?.nodeId}-${t?.id}-${t?.type}"]`):null,{x:g,y:_}=b$(e),v=o.elementFromPoint(g,_),y=v?.classList.contains(`${s}-flow__handle`)?v:h,b={handleDomNode:y,isValid:!1,connection:null,toHandle:null};if(y){let e=A$(void 0,y),t=y.getAttribute(`data-nodeid`),a=y.getAttribute(`data-handleid`),o=y.classList.contains(`connectable`),s=y.classList.contains(`connectableend`);if(!t||!e)return b;let c={source:m?t:r,sourceHandle:m?a:i,target:m?r:t,targetHandle:m?i:a};b.connection=c,b.isValid=o&&s&&(n===YZ.Strict?m&&e===`source`||!m&&e===`target`:t!==r||a!==i)&&l(c,{nodes:d,edges:u,sourceNode:f(c.source),targetNode:f(c.target)}),b.toHandle=N$(t,e,a,p,n,!0)}return b}function A$(e,t){return e||(t?.classList.contains(`target`)?`target`:t?.classList.contains(`source`)?`source`:null)}function j$(e,t){let n=null;return t?n=`valid`:e&&!t&&(n=`invalid`),n}function M$(e,t){let n=null;return t?n=!0:e&&!t&&(n=!1),n}function N$(e,t,n,r,i,a=!1){let o=r.get(e);if(!o)return null;let s=i===YZ.Strict?o.handleBounds?.[t]:[...o.handleBounds?.source??[],...o.handleBounds?.target??[]],c=(n?s?.find(e=>e.id===n):s?.[0])??null;return c&&a?{...c,...d$(o,c,c.position,!0)}:c}var P$={[KZ.Left]:KZ.Right,[KZ.Right]:KZ.Left,[KZ.Top]:KZ.Bottom,[KZ.Bottom]:KZ.Top},F$=[`production`,`prod`];function I$(e,...t){L$()&&console.warn(`[Vue Flow]: ${e}`,...t)}function L$(){return!F$.includes(`production`)}function R$(e,t,n,r,i){let a=t.querySelectorAll(`.vue-flow__handle.${e}`);return a?.length?Array.from(a).map(t=>{let a=t.getBoundingClientRect();return{id:t.getAttribute(`data-handleid`),type:e,nodeId:i,position:t.getAttribute(`data-handlepos`),x:(a.left-n.left)/r,y:(a.top-n.top)/r,...mQ(t)}}):null}function z$(e,t,n,r,i,a=!1,o){i.value=!1,e.selected?(a||e.selected&&t)&&(r([e]),ft(()=>{o.blur()})):n([e])}function B$(e){return z(e)!==void 0}function V$(e,t,n,r){if(!e||!e.source||!e.target)return n(new _$(h$.EDGE_INVALID,e?.id??`[ID UNKNOWN]`)),!1;let i;return i=vQ(e)?e:{...e,id:OQ(e)},i=wQ(i,void 0,r),!kQ(i,t)&&i}function H$(e,t,n,r,i){if(!t.source||!t.target)return i(new _$(h$.EDGE_INVALID,e.id)),!1;if(!n)return i(new _$(h$.EDGE_NOT_FOUND,e.id)),!1;let{id:a,...o}=e;return{...o,id:r?OQ(t):a,source:t.source,target:t.target,sourceHandle:t.sourceHandle,targetHandle:t.targetHandle}}function U$(e,t,n){let r={},i=[];for(let a=0;a<e.length;++a){let o=e[a];if(!yQ(o)){n(new _$(h$.NODE_INVALID,o?.id));continue}let s=CQ(o,t(o.id),o.parentNode);o.parentNode&&(r[o.parentNode]=!0),i[a]=s}for(let e of i){let a=t(e.parentNode)||i.find(t=>t.id===e.parentNode);e.parentNode&&!a&&n(new _$(h$.NODE_MISSING_PARENT,e.id,e.parentNode)),(e.parentNode||r[e.id])&&(r[e.id]&&(e.isParent=!0),a&&(a.isParent=!0))}return i}function W$(e,t,n,r,i,a){let o=i,s=r.get(o)||new Map;r.set(o,s.set(n,t)),o=`${i}-${e}`;let c=r.get(o)||new Map;if(r.set(o,c.set(n,t)),a){o=`${i}-${e}-${a}`;let s=r.get(o)||new Map;r.set(o,s.set(n,t))}}function G$(e,t,n){e.clear();for(let t of n){let{source:n,target:r,sourceHandle:i=null,targetHandle:a=null}=t,o={edgeId:t.id,source:n,target:r,sourceHandle:i,targetHandle:a},s=`${n}-${i}--${r}-${a}`;W$(`source`,o,`${r}-${a}--Line truncated
+16 -6
View File
@@ -151,6 +151,12 @@ async function saveDraft(): Promise<void> {
const nextID = parsed.value.ok ? parsed.value.id : "";
if (!nextID) return;
const previous = selected.value;
// Saving replaces the entry with that id, so a rename onto another
// workflow's id would silently overwrite it.
if (previous !== nextID && collection.value.definitions.some((entry) => entry.id === nextID)) {
localError.value = `A workflow with id "${nextID}" already exists; choose another id.`;
return;
}
let next = collection.value;
if (previous && previous !== nextID)
next = removeWorkflowDefinition(next, previous);
@@ -173,12 +179,16 @@ async function remove(): Promise<void> {
async function restoreOne(): Promise<void> {
if (!shippedEntry.value) return;
await save(
upsertWorkflowDefinition(
collection.value,
cloneControlPlaneValue(shippedEntry.value),
),
);
// The restored definition replaces whatever draft was open.
if (
await save(
upsertWorkflowDefinition(
collection.value,
cloneControlPlaneValue(shippedEntry.value),
),
)
)
load(selected.value);
}
// What is typed, read against the served step vocabulary (workflow-yaml.ts).
+11 -2
View File
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { computed, onMounted, ref, watchEffect } from "vue";
import { computed, onMounted, ref, watch, watchEffect } from "vue";
import { Ellipsis, Plus, RotateCcw } from "@lucide/vue";
import {
@@ -63,6 +63,7 @@ async function load() {
data.value = workflows ?? {};
runs.value = tasks ?? [];
error.value = null;
void loadWorkflowRuns();
} catch (err) {
error.value = String((err as Error).message || err);
}
@@ -124,7 +125,15 @@ async function setEnabled(value: boolean) {
}
}
const stages = computed(() => (data.value.stages ?? []).filter((s) => s.workflow === selected.value));
const workflowRuns = computed(() => runs.value.filter((t) => t.workflow === selected.value));
// The selected workflow's runs come from a workflow-filtered page: the global
// list above holds only the newest tasks of every workflow.
const workflowRuns = ref<RunTask[]>([]);
async function loadWorkflowRuns() {
const requested = selected.value;
const page = requested ? await api.taskPage<RunTask>({ workflow: requested }).catch(() => null) : null;
if (selected.value === requested) workflowRuns.value = page?.tasks ?? [];
}
watch(selected, () => void loadWorkflowRuns(), { immediate: true });
function create() {
let n = 1;
+8 -5
View File
@@ -27,11 +27,14 @@ export function useWorkflowRuns(workflow: Ref<string>) {
let picked = false;
async function loadRuns(): Promise<void> {
const tasks = (await api.tasks<WorkflowRun[]>().catch(() => [])) ?? [];
runs.value = tasks
.filter((task) => task.workflow === workflow.value)
.sort((a, b) => Number(b.id) - Number(a.id))
.slice(0, RECENT_RUNS);
// The server filters by workflow: filtering a global page here would miss
// every run older than that page.
const requested = workflow.value;
const page = requested
? await api.taskPage<WorkflowRun>({ workflow: requested, limit: RECENT_RUNS }).catch(() => null)
: null;
if (workflow.value !== requested) return;
runs.value = (page?.tasks ?? []).sort((a, b) => Number(b.id) - Number(a.id));
if (!picked || (watched.value && !runs.value.some((run) => String(run.id) === watched.value)))
watched.value = runs.value[0] ? String(runs.value[0].id) : "";
}