fix(gateway): name a plan wait's workflow from its run, not the next one
workflow.approve pre-writes the task's workflow column with the workflow that runs once approved, so task_list named a workflow that had not started while the task's own log named the one that produced the plan. Chat now names the compiled workflow through the pinned definition for a plan wait, matching the log.
This commit is contained in:
5 files changed
+113
-20
No files matched your search
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/logging"
|
||||
"github.com/samcharles93/archie-core/internal/taskstate"
|
||||
)
|
||||
|
||||
type chatTaskControllerAdapter struct {
|
||||
@@ -77,6 +78,9 @@ func (a chatTaskControllerAdapter) CancelChatTask(ctx context.Context, taskID in
|
||||
// happens here, as it does for the writer and controller adapters below.
|
||||
type chatTaskListerAdapter struct {
|
||||
tasks func(context.Context, int) ([]workflow.Task, error)
|
||||
// task reads one task in full. The list projection omits the definition
|
||||
// pin, which names the workflow a plan wait's run was compiled from.
|
||||
task func(context.Context, int64) (*workflow.Task, error)
|
||||
}
|
||||
|
||||
func (a chatTaskListerAdapter) ListChatTasks(ctx context.Context, identity string, limit int) ([]gateway.ChatTaskSummary, error) {
|
||||
@@ -104,7 +108,7 @@ func (a chatTaskListerAdapter) ListChatTasks(ctx context.Context, identity strin
|
||||
Repo: task.Owner + "/" + task.Repo,
|
||||
Title: task.Title,
|
||||
Status: task.Status,
|
||||
Workflow: task.Workflow,
|
||||
Workflow: a.chatWorkflow(ctx, task),
|
||||
PRNumber: task.PRNumber,
|
||||
Attempt: task.Attempt,
|
||||
ParkReason: task.ParkReason,
|
||||
@@ -114,6 +118,26 @@ func (a chatTaskListerAdapter) ListChatTasks(ctx context.Context, identity strin
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// chatWorkflow names the workflow the task's current run was compiled from, so
|
||||
// what chat shows agrees with the run's own log. A plan approval pre-writes the
|
||||
// workflow column with the workflow that runs once approved -- one that has not
|
||||
// started -- so the pinned definition answers there instead. A review wait and
|
||||
// every other status name the workflow the task is on or will run next.
|
||||
func (a chatTaskListerAdapter) chatWorkflow(ctx context.Context, task workflow.Task) string {
|
||||
if a.task == nil || taskstate.ApprovalKind(task.Status, task.ReviewGate) != taskstate.ApprovalPlan {
|
||||
return task.Workflow
|
||||
}
|
||||
full, err := a.task(ctx, task.ID)
|
||||
if err != nil || full == nil {
|
||||
return task.Workflow
|
||||
}
|
||||
compiled, ok, err := workflow.PinnedDefinitionID(full)
|
||||
if err != nil || !ok {
|
||||
return task.Workflow
|
||||
}
|
||||
return compiled
|
||||
}
|
||||
|
||||
// chatTaskLogReaderAdapter gives the gateway a read view of a task's
|
||||
// persisted log history without importing internal/logging or the task store
|
||||
// into the gateway package.
|
||||
|
||||
@@ -18,6 +18,72 @@ func (emptyTaskLogs) TaskLog(context.Context, int64, int, logging.Query) (loggin
|
||||
return logging.TaskLogPage{}, nil
|
||||
}
|
||||
|
||||
// TestChatTaskWorkflow pins which workflow chat names for a task. A plan
|
||||
// approval pre-writes the task's workflow column to the workflow that runs
|
||||
// once approved, so the column names a workflow that has not started; the
|
||||
// pinned definition names the one the run was compiled from, which is what
|
||||
// the run's own log names.
|
||||
func TestChatTaskWorkflow(t *testing.T) {
|
||||
const feasibility = "id: feasibility\nsteps:\n - type: workflow.approve\n settings: {plan: p, then: implement}\n"
|
||||
pinned := func(t workflow.Task) workflow.Task {
|
||||
t.WorkflowDefinitionYAML = feasibility
|
||||
t.WorkflowDefinitionDigest = workflow.DigestDefinition(feasibility)
|
||||
return t
|
||||
}
|
||||
reviewWait := `{"findings":[{"key":"f","finding":{}}],"workflow":"pr-review"}`
|
||||
tests := []struct {
|
||||
name string
|
||||
task workflow.Task
|
||||
pin bool
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "a plan wait names the workflow that produced the plan",
|
||||
task: workflow.Task{ID: 22, Status: workflow.StatusWaitingApproval, Workflow: "implement"},
|
||||
pin: true,
|
||||
want: "feasibility",
|
||||
},
|
||||
{
|
||||
name: "a review wait names the workflow the gate resumes",
|
||||
task: workflow.Task{ID: 30, Status: workflow.StatusWaitingApproval, Workflow: "pr-review", ReviewGate: reviewWait},
|
||||
pin: true,
|
||||
want: "pr-review",
|
||||
},
|
||||
{
|
||||
name: "a queued task names the workflow it will run next",
|
||||
task: workflow.Task{ID: 31, Status: workflow.StatusQueued, Workflow: "implement"},
|
||||
pin: true,
|
||||
want: "implement",
|
||||
},
|
||||
{
|
||||
name: "a task with no pin names its workflow column",
|
||||
task: workflow.Task{ID: 32, Status: workflow.StatusWaitingApproval, Workflow: "implement"},
|
||||
want: "implement",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// The list projection is narrow and carries no pin; the full read
|
||||
// is where the pin lives.
|
||||
full := tt.task
|
||||
if tt.pin {
|
||||
full = pinned(tt.task)
|
||||
}
|
||||
lister := chatTaskListerAdapter{
|
||||
tasks: func(context.Context, int) ([]workflow.Task, error) { return []workflow.Task{tt.task}, nil },
|
||||
task: func(context.Context, int64) (*workflow.Task, error) { return &full, nil },
|
||||
}
|
||||
got, err := lister.ListChatTasks(t.Context(), string(identity.OperatorID()), 10)
|
||||
if err != nil || len(got) != 1 {
|
||||
t.Fatalf("list = %d tasks, %v; want 1", len(got), err)
|
||||
}
|
||||
if got[0].Workflow != tt.want {
|
||||
t.Errorf("workflow = %q, want %q", got[0].Workflow, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestChatTaskScope pins the chat grant boundary: the operator profile sees
|
||||
// and approves every task, and any other identity only its own.
|
||||
func TestChatTaskScope(t *testing.T) {
|
||||
|
||||
@@ -192,7 +192,7 @@ func (b *server) setupGatewayChat(ctx context.Context, actor gateway.ChatTaskAct
|
||||
router.Models = b.chatModels
|
||||
router.Updates = b.updateService
|
||||
router.InitSessions(b.chatSessionStore)
|
||||
configureTaskCommands(router, b.chatTasks, b.chatController, chatTaskListerAdapter{tasks: b.stateStore.Tasks}, b.defaultChatIdentity)
|
||||
configureTaskCommands(router, b.chatTasks, b.chatController, chatTaskListerAdapter{tasks: b.stateStore.Tasks, task: b.stateStore.TaskByID}, b.defaultChatIdentity)
|
||||
router.Health = b.statusHealth
|
||||
// ToolLimits reads the live holder, not cfg: the runtime-resource
|
||||
// watch republishes a tool-settings change through it, so a policy edit
|
||||
@@ -202,7 +202,7 @@ func (b *server) setupGatewayChat(ctx context.Context, actor gateway.ChatTaskAct
|
||||
ToolLimits: func() modelloop.ToolLimits { return toolLimits(b.cfgHolder.Get()) },
|
||||
LLM: b.chatLLM, ChatModels: b.chatModels, ToolReg: b.toolReg,
|
||||
Soul: b.soul, ChatTasks: b.chatTasks,
|
||||
ChatTaskLister: chatTaskListerAdapter{tasks: b.stateStore.Tasks},
|
||||
ChatTaskLister: chatTaskListerAdapter{tasks: b.stateStore.Tasks, task: b.stateStore.TaskByID},
|
||||
ChatTaskLogs: chatTaskLogReaderAdapter{tasks: b.stateStore.TaskByID, taskLogs: b.stateStore},
|
||||
ChatTaskActor: actor,
|
||||
DefaultChatIdentity: b.defaultChatIdentity,
|
||||
|
||||
@@ -2127,7 +2127,7 @@ func (d *Daemon) pinWorkflowDefinition(ctx context.Context, task *workflow.Task)
|
||||
// pinDefinition keeps a valid pin and resolves a missing or mismatched one
|
||||
// again from the active definitions.
|
||||
func (d *Daemon) pinDefinition(ctx context.Context, task *workflow.Task) error {
|
||||
pinned, ok, err := pinnedDefinitionID(task)
|
||||
pinned, ok, err := workflow.PinnedDefinitionID(task)
|
||||
if err != nil {
|
||||
return pinFailure{taskstate.ParkNeedsDecision, err}
|
||||
}
|
||||
@@ -2185,22 +2185,6 @@ func parkBlocker(err error) taskstate.Blocker {
|
||||
return ""
|
||||
}
|
||||
|
||||
// pinnedDefinitionID returns the workflow id a task's pin declares, checking
|
||||
// its digest. ok is false when there is no pin.
|
||||
func pinnedDefinitionID(task *workflow.Task) (string, bool, error) {
|
||||
if task.WorkflowDefinitionYAML == "" {
|
||||
return "", false, nil
|
||||
}
|
||||
if workflow.DigestDefinition(task.WorkflowDefinitionYAML) != task.WorkflowDefinitionDigest {
|
||||
return "", false, fmt.Errorf("stored workflow definition digest mismatch")
|
||||
}
|
||||
id, err := workflow.DefinitionID(task.WorkflowDefinitionYAML)
|
||||
if err != nil {
|
||||
return "", false, fmt.Errorf("stored workflow definition: %w", err)
|
||||
}
|
||||
return id, true, nil
|
||||
}
|
||||
|
||||
// runActionPlaybooks runs the action playbooks matching a task before it is
|
||||
// pinned. Failures never fail the task; tasks already naming a workflow are
|
||||
// skipped.
|
||||
|
||||
@@ -169,6 +169,25 @@ func DefinitionID(src string) (string, error) {
|
||||
return declared.ID, nil
|
||||
}
|
||||
|
||||
// PinnedDefinitionID returns the workflow id a task's definition pin declares,
|
||||
// checking the pin against its digest. ok is false when the task carries no
|
||||
// pin. This is the workflow the task's current run was compiled from, which is
|
||||
// what the run's own log names; the task's workflow column names the workflow
|
||||
// it runs next.
|
||||
func PinnedDefinitionID(t *Task) (string, bool, error) {
|
||||
if t.WorkflowDefinitionYAML == "" {
|
||||
return "", false, nil
|
||||
}
|
||||
if DigestDefinition(t.WorkflowDefinitionYAML) != t.WorkflowDefinitionDigest {
|
||||
return "", false, fmt.Errorf("stored workflow definition digest mismatch")
|
||||
}
|
||||
id, err := DefinitionID(t.WorkflowDefinitionYAML)
|
||||
if err != nil {
|
||||
return "", false, fmt.Errorf("stored workflow definition: %w", err)
|
||||
}
|
||||
return id, true, nil
|
||||
}
|
||||
|
||||
// WorkflowDefinitionEntry and WorkflowDefinitionCollection are defined in
|
||||
// package task so dashboards can decode the stored projection without linking
|
||||
// the engine. Validation and compilation stay here.
|
||||
|
||||
Reference in new issue
Block a user