From a5826156a3c6a5eb6d766669261cc07ea9054453 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 22 Nov 2026 11:36:08 +1100 Subject: [PATCH] feat(runs): filter runs by workflow and name each tool call in a trail entry (#2061) --- api/openapi.yaml | 11 +++++++++++ modules/runs/internal/store/store.go | 7 ++++--- modules/runs/trail.go | 27 ++++++++++++++++----------- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/api/openapi.yaml b/api/openapi.yaml index 5e3baa363..271667359 100644 --- a/api/openapi.yaml +++ b/api/openapi.yaml @@ -2906,6 +2906,8 @@ components: RunsToolCallView: additionalProperties: false properties: + id: + type: string input: type: string name: @@ -2953,6 +2955,8 @@ components: at: format: date-time type: string + call_id: + type: string error: type: string finish_reason: @@ -6145,6 +6149,13 @@ paths: maximum: 200 minimum: 1 type: integer + - description: Only this workflow's runs + explode: false + in: query + name: workflow + schema: + description: Only this workflow's runs + type: string responses: "200": content: diff --git a/modules/runs/internal/store/store.go b/modules/runs/internal/store/store.go index 3e229f614..a32ca1f59 100644 --- a/modules/runs/internal/store/store.go +++ b/modules/runs/internal/store/store.go @@ -521,12 +521,13 @@ type Recorded struct { RecordedAt time.Time `db:"recorded_at"` } -// Recent returns an organisation's latest runs, newest first. -func (s *Store) Recent(ctx context.Context, orgID string, limit int) ([]Summary, error) { +// Recent returns an organisation's latest runs, newest first, of one workflow when +// one is named. +func (s *Store) Recent(ctx context.Context, orgID, workflow string, limit int) ([]Summary, error) { var out []Summary if err := s.db.SelectContext(ctx, &out, ` SELECT id, workflow, workflow_version, state, attempts, error, detail, created_at, finished_at, dry FROM runs.runs - WHERE org_id = $1 AND NOT dry ORDER BY created_at DESC LIMIT $2`, orgID, limit); err != nil { + WHERE org_id = $1 AND NOT dry AND ($3 = '' OR workflow = $3) ORDER BY created_at DESC LIMIT $2`, orgID, limit, workflow); err != nil { return nil, fmt.Errorf("list runs: %w", err) } return out, nil diff --git a/modules/runs/trail.go b/modules/runs/trail.go index 850990f51..33c0bbfe4 100644 --- a/modules/runs/trail.go +++ b/modules/runs/trail.go @@ -121,16 +121,20 @@ type TrailEntry struct { // 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"` - 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"` + 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"` } @@ -221,7 +225,8 @@ func (a *runAPI) cancel(ctx context.Context, in *cancelRunInput) (*httpapi.Reply } type recentRunsInput struct { - Limit int `query:"limit" minimum:"1" maximum:"200" default:"50"` + Limit int `query:"limit" minimum:"1" maximum:"200" default:"50"` + Workflow string `query:"workflow" doc:"Only this workflow's runs"` } type runInput struct { @@ -238,7 +243,7 @@ type runAPI struct { } func (a *runAPI) recent(ctx context.Context, in *recentRunsInput) (*httpapi.Reply[[]RunView], error) { - runs, err := a.db.Recent(ctx, a.orgID, in.Limit) + runs, err := a.db.Recent(ctx, a.orgID, in.Workflow, in.Limit) if err != nil { return nil, huma.NewError(http.StatusInternalServerError, "couldn't list runs") } @@ -432,7 +437,7 @@ func entry(s store.Recorded) TrailEntry { } switch je.Kind { case agentloop.EntryTool: - e.Kind, e.Name = "tool", je.ToolName + 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", "" @@ -450,7 +455,7 @@ func entry(s store.Recorded) TrailEntry { } } for _, c := range je.Message.ToolCalls { - e.ToolCalls = append(e.ToolCalls, ToolCallView{Name: c.Name, Input: c.Arguments}) + e.ToolCalls = append(e.ToolCalls, ToolCallView{ID: c.ID, Name: c.Name, Input: c.Arguments}) } } e.Error = je.Error @@ -460,7 +465,7 @@ func entry(s store.Recorded) TrailEntry { // 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, limit) + runs, err := e.db.Recent(ctx, orgID, "", limit) if err != nil { return nil, err }