Files
sam a101b64703 fix(runs): a cancelled run's trail records who stopped it (#2100)
Cancelling a run moved it and the runs it called to cancelled and wrote nothing
to either trail, so a stopped run could not say when it stopped or who asked.

The stop now returns the runs it stopped and records one cancel entry on each,
in the same transaction, naming the principal that asked. The entry is a step
named cancel with the caller's identity, so the run detail shows it with the
rest of the trail.
2026-11-27 19:24:19 +11:00

544 lines
19 KiB
Go

package runs
import (
"context"
"encoding/json/jsontext"
json "encoding/json/v2"
"errors"
"net/http"
"strconv"
"strings"
"time"
"github.com/danielgtaylor/huma/v2"
"github.com/google/uuid"
"github.com/samcharles93/ai-sdk/agentloop"
"github.com/samcharles93/ai-sdk/chat"
"github.com/samcharles93/archie-core/internal/platform/httpapi"
"github.com/samcharles93/archie-core/modules/approvals"
"github.com/samcharles93/archie-core/modules/governance"
"github.com/samcharles93/archie-core/modules/judgement"
"github.com/samcharles93/archie-core/modules/runs/internal/store"
)
// Origin is the event and trigger that started a run.
type Origin struct {
EventID uuid.UUID `json:"event_id"`
// SourceID is the nil UUID for a schedule tick. Source is the source's
// label.
SourceID uuid.UUID `json:"source_id"`
Source string `json:"source"`
DeclaredEventType string `json:"declared_event_type"`
Fields jsontext.Value `json:"fields"`
ReceivedAt time.Time `json:"received_at"`
// TriggerID is the trigger that started the run, and Trigger its current
// name. TriggerID is the nil UUID for a run a trigger proposal started,
// whose trigger wasn't saved, and Trigger is then the proposed name.
TriggerID uuid.UUID `json:"trigger_id"`
Trigger string `json:"trigger"`
Filter jsontext.Value `json:"filter"`
}
// Origins finds the event and trigger that started a run. A run started any
// other way has none.
type Origins interface {
Origin(ctx context.Context, runID uuid.UUID) (Origin, bool, error)
}
// RunView is a run as the API shows it.
type RunView struct {
ID uuid.UUID `json:"id"`
WorkflowID uuid.UUID `json:"workflow_id"`
// Workflow is the workflow's current name.
Workflow string `json:"workflow"`
Version int `json:"workflow_version"`
State string `json:"state"`
Attempts int `json:"attempts"`
Error string `json:"error,omitzero"`
// Detail explains how the run ended: a finish step's detail, or why a
// step parked the run.
Detail string `json:"detail,omitzero"`
CreatedAt time.Time `json:"created_at"`
FinishedAt *time.Time `json:"finished_at,omitzero"`
Result jsontext.Value `json:"result,omitzero"`
// Dry marks a test run of a draft, which changes nothing.
Dry bool `json:"dry,omitzero"`
}
// Trail is a run's decision trail: what started it and every step it took,
// in order.
type Trail struct {
Run RunView `json:"run"`
Origin *Origin `json:"origin,omitzero"`
// Definition is the workflow document the run executes: the version it
// started under, or for a test run the draft it tested. It is empty when
// the workflow has since been deleted.
Definition string `json:"definition,omitzero"`
// Stages is how far each top-level step of the definition got.
Stages []Stage `json:"stages"`
// Actions are the run's concrete actions in the order they were judged,
// each with its verdicts, the approval it was put to and what came of it.
Actions []ActionTrail `json:"actions"`
Entries []TrailEntry `json:"entries"`
}
// ActionTrail is one action from judgement to effect.
type ActionTrail struct {
Decision judgement.Decision `json:"decision"`
// Rejudged is the judgement made after an approval, just before the action
// ran.
Rejudged *judgement.Decision `json:"rejudged,omitzero"`
// Approval is the request put to a person, with the summary they were shown
// and who answered. An action that needed none has none.
Approval *approvals.View `json:"approval,omitzero"`
// Stop is why an action of a sequence stopped the rest of it: its target,
// effect or judgement no longer matched what the person approved.
Stop string `json:"stop,omitzero"`
// Effect is what the action did. An action that never ran has none.
Effect *Effect `json:"effect,omitzero"`
}
// The outcomes an action's effect can have.
const (
EffectRan = "ran"
EffectFailed = "failed"
EffectUnknown = "unknown"
)
// Effect is what an action did: its output, its failure, or that it was
// started and can't be confirmed.
type Effect struct {
State string `json:"state"`
At time.Time `json:"at"`
Output string `json:"output,omitzero"`
Error string `json:"error,omitzero"`
}
// TrailEntry is one recorded step. Kind is "model" or "compaction" for an
// agent's model call, "tool" for its tool call, and "step" for any other
// workflow step.
type TrailEntry struct {
Seq int `json:"seq"`
// Step is the top-level step of the definition that made the entry.
Step string `json:"step,omitzero"`
At time.Time `json:"at"`
Kind string `json:"kind"`
Name string `json:"name,omitzero"`
Text string `json:"text,omitzero"`
Reasoning string `json:"reasoning,omitzero"`
ToolCalls []ToolCallView `json:"tool_calls,omitzero"`
Usage *chat.Usage `json:"usage,omitzero"`
// Request is the model request exactly as it was sent: the messages, the
// tools offered and the sampling settings.
Request jsontext.Value `json:"request,omitzero"`
// FinishReason is the wire finish reason of a model call.
FinishReason string `json:"finish_reason,omitzero"`
// CallID is the model's id for the tool call a tool entry answers.
CallID string `json:"call_id,omitzero"`
Input string `json:"input,omitzero"`
Output string `json:"output,omitzero"`
Value jsontext.Value `json:"value,omitzero"`
Error string `json:"error,omitzero"`
Unknown bool `json:"unknown,omitzero"`
}
// ToolCallView is a tool call a model asked for.
type ToolCallView struct {
// ID pairs the call with the tool entry that answers it.
ID string `json:"id,omitzero"`
Name string `json:"name"`
Input string `json:"input"`
}
// Decisions finds the judgements made in a run.
type Decisions interface {
ForRun(ctx context.Context, runID uuid.UUID) ([]judgement.Decision, error)
}
// Approvals finds the approval requests made in a run.
type Approvals interface {
ForRun(ctx context.Context, runID uuid.UUID) ([]approvals.Request, error)
}
// Register adds the run API for orgID: recent runs, each run's trail, and
// the workflows runs execute.
func (e *Engine) Register(api huma.API, orgID string, origins Origins, decisions Decisions, asked Approvals) {
a := &runAPI{engine: e, db: e.db, orgID: orgID, origins: origins, decisions: decisions, approvals: asked}
tags := []string{"Runs"}
httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionRead, governance.KindRun), huma.Operation{
OperationID: "list-runs", Method: http.MethodGet, Path: "/api/runs",
Summary: "List recent runs", Tags: tags,
}, a.recent)
httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionRead, governance.KindRun), huma.Operation{
OperationID: "get-run", Method: http.MethodGet, Path: "/api/runs/{id}",
Summary: "Get a run's decision trail", Tags: tags,
}, a.trail)
httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionRun, governance.KindRun), huma.Operation{
OperationID: "retry-run", Method: http.MethodPost, Path: "/api/runs/{id}/retry",
Summary: "Restart a parked or ended run from a step, keeping the results of the steps before it", Tags: tags,
}, a.retry)
httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionUpdate, governance.KindRun), huma.Operation{
OperationID: "cancel-run", Method: http.MethodPost, Path: "/api/runs/{id}/cancel",
Summary: "Cancel a run and the runs it started with workflow.call", Tags: tags,
}, a.cancel)
e.registerWorkflows(api, orgID)
}
type retryRunInput struct {
ID string `path:"id" format:"uuid"`
Body struct {
From string `json:"from,omitempty" doc:"The step to retry from, by id or as the trail names it; by default the step that parked the run"`
}
}
func (a *runAPI) retry(ctx context.Context, in *retryRunInput) (*httpapi.Reply[RunView], error) {
id, err := uuid.Parse(in.ID)
if err != nil {
return nil, huma.NewError(http.StatusBadRequest, "a run id is a UUID")
}
switch err := a.engine.Retry(ctx, a.orgID, id, in.Body.From); {
case errors.Is(err, store.ErrNoRun):
return nil, huma.NewError(http.StatusNotFound, "no such run")
case errors.Is(err, ErrNotRetryable):
return nil, huma.NewError(http.StatusConflict, "only a run that has ended or parked retries, and only from a step it reached")
case err != nil:
return nil, huma.NewError(http.StatusInternalServerError, "couldn't retry the run")
}
d, ok, err := a.db.Detail(ctx, a.orgID, id)
if err != nil || !ok {
return nil, huma.NewError(http.StatusInternalServerError, "couldn't read the run")
}
return httpapi.OK(view(d.Summary, d.Result)), nil
}
type cancelRunInput struct {
ID string `path:"id" format:"uuid"`
}
func (a *runAPI) cancel(ctx context.Context, in *cancelRunInput) (*httpapi.Reply[RunView], error) {
id, err := uuid.Parse(in.ID)
if err != nil {
return nil, huma.NewError(http.StatusBadRequest, "a run id is a UUID")
}
switch err := a.engine.Cancel(ctx, a.orgID, id); {
case errors.Is(err, store.ErrNoRun):
return nil, huma.NewError(http.StatusNotFound, "no such run")
case errors.Is(err, ErrEnded):
return nil, huma.NewError(http.StatusConflict, "the run has already ended, so there is nothing to cancel")
case err != nil:
return nil, huma.NewError(http.StatusInternalServerError, "couldn't cancel the run")
}
d, ok, err := a.db.Detail(ctx, a.orgID, id)
if err != nil || !ok {
return nil, huma.NewError(http.StatusInternalServerError, "couldn't read the run")
}
return httpapi.OK(view(d.Summary, d.Result)), nil
}
type recentRunsInput struct {
Limit int `query:"limit" minimum:"1" maximum:"200" default:"50"`
WorkflowID string `query:"workflow_id" format:"uuid" doc:"Only this workflow's runs"`
SysQuery string `query:"sys_query" doc:"Conditions every row must meet, joined by ^: field=value, field!=value or fieldLIKEtext; applied to the rows the limit returns"`
}
// runFilterFields are what a run list's filter may compare. The filter applies
// to the runs the limit returns.
var runFilterFields = httpapi.Fields[RunView]{
"id": func(v RunView) string { return v.ID.String() },
"workflow": func(v RunView) string { return v.Workflow },
"state": func(v RunView) string { return v.State },
"error": func(v RunView) string { return v.Error },
}
type runInput struct {
ID string `path:"id" format:"uuid"`
}
type runAPI struct {
engine *Engine
db *store.Store
orgID string
origins Origins
decisions Decisions
approvals Approvals
}
func (a *runAPI) recent(ctx context.Context, in *recentRunsInput) (*httpapi.Reply[[]RunView], error) {
var workflow uuid.NullUUID
if in.WorkflowID != "" {
id, err := uuid.Parse(in.WorkflowID)
if err != nil {
return nil, huma.NewError(http.StatusBadRequest, "a workflow id is a UUID")
}
workflow = uuid.NullUUID{UUID: id, Valid: true}
}
runs, err := a.db.Recent(ctx, a.orgID, workflow, in.Limit)
if err != nil {
return nil, huma.NewError(http.StatusInternalServerError, "couldn't list runs")
}
views := make([]RunView, 0, len(runs))
for _, s := range runs {
views = append(views, view(s, nil))
}
filtered, err := httpapi.Filter(in.SysQuery, runFilterFields, views)
if err != nil {
return nil, huma.Error400BadRequest(err.Error())
}
return httpapi.OK(filtered), nil
}
func (a *runAPI) trail(ctx context.Context, in *runInput) (*httpapi.Reply[Trail], error) {
id, err := uuid.Parse(in.ID)
if err != nil {
return nil, huma.NewError(http.StatusBadRequest, "a run id is a UUID")
}
d, ok, err := a.db.Detail(ctx, a.orgID, id)
switch {
case err != nil:
return nil, huma.NewError(http.StatusInternalServerError, "couldn't read the run")
case !ok:
return nil, huma.NewError(http.StatusNotFound, "no such run")
}
steps, err := a.db.RecordedSteps(ctx, id)
if err != nil {
return nil, huma.NewError(http.StatusInternalServerError, "couldn't read the run's steps")
}
t := Trail{Run: view(d.Summary, d.Result), Entries: make([]TrailEntry, 0, len(steps)), Stages: []Stage{}}
if body, ok := a.definition(ctx, d); ok {
t.Definition = body
if doc, err := ParseDocument(body); err == nil {
t.Stages = stagesOf(doc, steps, t.Run)
}
}
regs := regions(steps)
if o, ok, err := a.origins.Origin(ctx, id); err != nil {
return nil, huma.NewError(http.StatusInternalServerError, "couldn't read what started the run")
} else if ok {
t.Origin = &o
}
for _, s := range steps {
// Markers record step positions and results for the engine. The
// stages show what they record.
if s.Seq >= markerBase {
continue
}
e := entry(s)
e.Step = stageOf(regs, s.Seq)
t.Entries = append(t.Entries, e)
}
decisions, err := a.decisions.ForRun(ctx, id)
if err != nil {
return nil, huma.NewError(http.StatusInternalServerError, "couldn't read the run's decisions")
}
asked, err := a.approvals.ForRun(ctx, id)
if err != nil {
return nil, huma.NewError(http.StatusInternalServerError, "couldn't read the run's approvals")
}
t.Actions = actions(decisions, asked, steps)
return httpapi.OK(t), nil
}
// definition is the document a run executes. A workflow deleted since the run
// has none to show.
func (a *runAPI) definition(ctx context.Context, d store.Detail) (string, bool) {
if d.Dry {
o, err := readOverlay(d.Overlay)
return o.documentOr(), err == nil
}
w, err := a.db.Workflow(ctx, a.orgID, d.WorkflowID, d.Version)
return w.Body, err == nil
}
// actions joins each decision with its approval and its effect. An approved
// action's effect is its run step, and the judgement made just before it is
// folded into the action rather than listed as one of its own. An action that
// ran unattended did so in the agent's tool call, whose journal position is the
// decision's key.
func actions(decisions []judgement.Decision, asked []approvals.Request, steps []store.Recorded) []ActionTrail {
byName := make(map[string]store.Recorded, len(steps))
tools := make(map[string]store.Recorded, len(steps))
rejudgements := make(map[string]string, len(steps))
for _, s := range steps {
byName[s.Name] = s
if key, ok := strings.CutPrefix(s.Name, "judge-again:"); ok {
rejudgements[stepKey(s)] = key
}
}
for _, s := range steps {
if strings.HasPrefix(s.Name, "journal:agent:") {
if key, ok := toolKey(s); ok {
tools[key] = s
}
}
}
rejudged := make(map[string]judgement.Decision, len(rejudgements))
out := make([]ActionTrail, 0, len(decisions))
for _, d := range decisions {
if of, ok := rejudgements[d.Key]; ok {
rejudged[of] = d
continue
}
out = append(out, ActionTrail{Decision: d})
}
for i := range out {
d := out[i].Decision
view, covers := approvedBy(asked, d)
out[i].Approval = view
if r, ok := rejudged[d.Key]; ok {
out[i].Rejudged = &r
out[i].Stop = stoppedBy(d, r, covers)
}
switch run, ran := byName["run:"+d.Key]; {
case ran:
out[i].Effect = effectOf(run)
case d.Level.Runs():
if t, ok := tools[d.Key]; ok {
out[i].Effect = effectOf(t)
}
}
}
return out
}
// approvedBy returns the request that covered d, and how many actions it
// covers, or nil when no person was asked.
func approvedBy(asked []approvals.Request, d judgement.Decision) (*approvals.View, int) {
for _, req := range asked {
if req.Covers(d) {
v := req.View()
return &v, len(v.Steps)
}
}
return nil, 0
}
// stoppedBy is why a step judged again stopped the sequence that covers it, or
// empty when it didn't: a request for a single action stops nothing.
func stoppedBy(d, fresh judgement.Decision, covers int) string {
if covers < 2 {
return ""
}
if next, why := afterApproval(d, fresh); next == reaskPerson || next == refuseAction {
return why
}
return ""
}
// stepKey is the idempotency key the engine gave the step at s.
func stepKey(s store.Recorded) string { return s.RunID.String() + ":" + strconv.Itoa(s.Seq) }
// toolKey is the idempotency key the agent loop gave the tool call a journal
// step records.
func toolKey(s store.Recorded) (string, bool) {
agent, ok := strings.CutPrefix(s.Name, "journal:agent:")
if !ok {
return "", false
}
var je agentloop.Entry
if err := json.Unmarshal(s.Output, &je); err != nil || je.Kind != agentloop.EntryTool {
return "", false
}
return s.RunID.String() + ":" + agent + ":" + strconv.FormatInt(je.Position, 10), true
}
// effectOf reads what an action's step recorded. A tool call's journal entry
// and an approved action's run step record it differently.
func effectOf(s store.Recorded) *Effect {
e := TrailEntry{}
if strings.HasPrefix(s.Name, "journal:agent:") {
e = entry(s)
} else {
e.Error, e.Unknown = s.Error.String, s.Outcome == store.OutcomeUnknown
var out string
if err := json.Unmarshal(s.Output, &out); err == nil {
e.Output = out
}
}
ef := &Effect{State: EffectRan, At: s.RecordedAt, Output: e.Output, Error: e.Error}
switch {
case e.Unknown:
ef.State = EffectUnknown
case e.Error != "":
ef.State = EffectFailed
}
return ef
}
func view(s store.Summary, result jsontext.Value) RunView {
v := RunView{
ID: s.ID, WorkflowID: s.WorkflowID, Workflow: s.Workflow, Version: s.Version, State: s.State, Attempts: s.Attempts,
Error: s.Error.String, Detail: s.Detail.String, CreatedAt: s.CreatedAt, Result: result, Dry: s.Dry,
}
if s.FinishedAt.Valid {
v.FinishedAt = &s.FinishedAt.Time
}
return v
}
// entry turns a recorded step into a trail entry. An agent's journal steps
// are unpacked into the model and tool calls they record.
func entry(s store.Recorded) TrailEntry {
e := TrailEntry{Seq: s.Seq, At: s.RecordedAt, Kind: "step", Name: s.Name, Error: s.Error.String}
if s.Outcome == store.OutcomeUnknown {
e.Unknown = true
}
if !strings.HasPrefix(s.Name, "journal:agent:") {
e.Value = s.Output
return e
}
var je agentloop.Entry
if err := json.Unmarshal(s.Output, &je); err != nil {
e.Value = s.Output
return e
}
switch je.Kind {
case agentloop.EntryTool:
e.Kind, e.Name, e.CallID = "tool", je.ToolName, je.ToolCallID
e.Input, e.Output, e.Error, e.Unknown = je.Input, je.Output, je.Error, je.Unknown
case agentloop.EntryModel, agentloop.EntryCompact:
e.Kind, e.Name = "model", ""
if je.Kind == agentloop.EntryCompact {
e.Kind = "compaction"
}
e.Request, e.FinishReason = je.Request, je.FinishReason
usage := je.Usage
e.Usage = &usage
if je.Message != nil {
e.Text = je.Message.Content
for _, p := range je.Message.Parts {
if rp, ok := p.(chat.ReasoningPart); ok {
e.Reasoning += rp.Text
}
}
for _, c := range je.Message.ToolCalls {
e.ToolCalls = append(e.ToolCalls, ToolCallView{ID: c.ID, Name: c.Name, Input: c.Arguments})
}
}
e.Error = je.Error
}
return e
}
// Recent returns orgID's latest runs, newest first.
func (e *Engine) Recent(ctx context.Context, orgID string, limit int) ([]RunView, error) {
runs, err := e.db.Recent(ctx, orgID, uuid.NullUUID{}, limit)
if err != nil {
return nil, err
}
out := make([]RunView, 0, len(runs))
for _, s := range runs {
out = append(out, view(s, nil))
}
return out, nil
}
// Run returns one run with its result; ok is false when there is no such run.
func (e *Engine) Run(ctx context.Context, orgID string, id uuid.UUID) (RunView, bool, error) {
d, ok, err := e.db.Detail(ctx, orgID, id)
if err != nil || !ok {
return RunView{}, ok, err
}
return view(d.Summary, d.Result), true, nil
}