feat(workflows): test-run unsaved drafts and browse workflow version history

A test run pins a validated draft, runs with forge and push writes
suppressed and recorded per step, and is excluded from workflow stats.
The History tab lists revisions that changed a workflow, diffs them as
YAML and on the canvas, restores into the editor, and ties runs to the
revision they used.
This commit is contained in:
sam committed 2026-10-13 12:27:40 +11:00
1 parent 2f05cbd34f
commit 2a066f6d4f
37 files changed
+677 -132

No files matched your search

@@ -186,6 +186,7 @@ SELECT workflow, stage,
COUNT(*) FILTER (WHERE (data::jsonb ->> 'error') IS NOT NULL)::int AS errors
FROM events
WHERE kind = 'stage_finish' AND ($1::text IS NULL OR org_id = $1)
AND task_id NOT IN (SELECT id FROM tasks WHERE source = 'test')
GROUP BY workflow, stage
ORDER BY workflow, stage
`
@@ -336,7 +337,7 @@ SELECT workflow,
AVG(iterations)::float8 AS avg_steps,
SUM(tokens_used)::bigint AS total_tokens
FROM tasks
WHERE workflow <> '' AND ($1::text IS NULL OR org_id = $1)
WHERE workflow <> '' AND source <> 'test' AND ($1::text IS NULL OR org_id = $1)
GROUP BY workflow
ORDER BY COUNT(*) DESC
`
@@ -391,24 +391,28 @@ func (q *Queries) EnqueueIssue(ctx context.Context, arg EnqueueIssueParams) (int
const insertChatTask = `-- name: InsertChatTask :one
WITH number AS (
INSERT INTO chat_issue_numbers (owner, repo, last_number)
VALUES ($1, $2, $10::bigint + 1)
VALUES ($1, $2, $12::bigint + 1)
ON CONFLICT (owner, repo) DO UPDATE SET last_number = chat_issue_numbers.last_number + 1
RETURNING last_number
)
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, workflow, source, identity, org_id, inputs, origin)
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, workflow, source, identity, org_id, inputs, origin, workflow_definition_yaml, workflow_definition_digest)
SELECT
$1, $2, number.last_number,
$3, $4, 'chat', $5, 'chat', $6,
owner_org.id,
$3, $4, 'chat', $5,
CASE WHEN $6::text = '' THEN 'chat' ELSE 'test' END,
$7,
$8
owner_org.id,
$8,
$9,
$6::text,
$10::text
FROM number, (SELECT COALESCE(
(SELECT a.org_id FROM org_agents a WHERE a.identity_id = $6),
(SELECT m.org_id FROM memberships m WHERE m.identity_id = $6
(SELECT a.org_id FROM org_agents a WHERE a.identity_id = $7),
(SELECT m.org_id FROM memberships m WHERE m.identity_id = $7
ORDER BY m.created_at, m.org_id, m.workspace_id NULLS LAST LIMIT 1),
'org-sys'
) AS id) AS owner_org
WHERE $9::text IS NULL OR owner_org.id = $9
WHERE $11::text IS NULL OR owner_org.id = $11
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, outcome_posted
`
@@ -418,9 +422,11 @@ type InsertChatTaskParams struct {
Title string
Body string
Workflow string
DraftYaml string
Identity string
Inputs string
Origin string
DraftDigest string
ScopeOrg pgtype.Text
FallbackIssueNumber int64
}
@@ -435,6 +441,8 @@ type InsertChatTaskParams struct {
// StampTaskBinding adds only the provenance.
// The task runs in its identity's org; a caller confined to another org
// inserts nothing, so it cannot start work under an identity it does not own.
// A non-empty draft_yaml makes the task a test run: it is pinned to that
// definition and its source is 'test'.
func (q *Queries) InsertChatTask(ctx context.Context, arg InsertChatTaskParams) (Task, error) {
row := q.db.QueryRow(ctx, insertChatTask,
arg.Owner,
@@ -442,9 +450,11 @@ func (q *Queries) InsertChatTask(ctx context.Context, arg InsertChatTaskParams)
arg.Title,
arg.Body,
arg.Workflow,
arg.DraftYaml,
arg.Identity,
arg.Inputs,
arg.Origin,
arg.DraftDigest,
arg.ScopeOrg,
arg.FallbackIssueNumber,
)
@@ -49,7 +49,7 @@ SELECT workflow,
AVG(iterations)::float8 AS avg_steps,
SUM(tokens_used)::bigint AS total_tokens
FROM tasks
WHERE workflow <> '' AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org))
WHERE workflow <> '' AND source <> 'test' AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org))
GROUP BY workflow
ORDER BY COUNT(*) DESC;
@@ -60,6 +60,7 @@ SELECT workflow, stage,
COUNT(*) FILTER (WHERE (data::jsonb ->> 'error') IS NOT NULL)::int AS errors
FROM events
WHERE kind = 'stage_finish' AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org))
AND task_id NOT IN (SELECT id FROM tasks WHERE source = 'test')
GROUP BY workflow, stage
ORDER BY workflow, stage;
@@ -67,19 +67,25 @@ SELECT status, count(*)::int AS count FROM tasks WHERE (sqlc.narg(scope_org)::te
-- StampTaskBinding adds only the provenance.
-- The task runs in its identity's org; a caller confined to another org
-- inserts nothing, so it cannot start work under an identity it does not own.
-- A non-empty draft_yaml makes the task a test run: it is pinned to that
-- definition and its source is 'test'.
WITH number AS (
INSERT INTO chat_issue_numbers (owner, repo, last_number)
VALUES (sqlc.arg(owner), sqlc.arg(repo), sqlc.arg(fallback_issue_number)::bigint + 1)
ON CONFLICT (owner, repo) DO UPDATE SET last_number = chat_issue_numbers.last_number + 1
RETURNING last_number
)
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, workflow, source, identity, org_id, inputs, origin)
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, workflow, source, identity, org_id, inputs, origin, workflow_definition_yaml, workflow_definition_digest)
SELECT
sqlc.arg(owner), sqlc.arg(repo), number.last_number,
sqlc.arg(title), sqlc.arg(body), 'chat', sqlc.arg(workflow), 'chat', sqlc.arg(identity),
sqlc.arg(title), sqlc.arg(body), 'chat', sqlc.arg(workflow),
CASE WHEN sqlc.arg(draft_yaml)::text = '' THEN 'chat' ELSE 'test' END,
sqlc.arg(identity),
owner_org.id,
sqlc.arg(inputs),
sqlc.arg(origin)
sqlc.arg(origin),
sqlc.arg(draft_yaml)::text,
sqlc.arg(draft_digest)::text
FROM number, (SELECT COALESCE(
(SELECT a.org_id FROM org_agents a WHERE a.identity_id = sqlc.arg(identity)),
(SELECT m.org_id FROM memberships m WHERE m.identity_id = sqlc.arg(identity)
+14 -4
View File
@@ -156,11 +156,11 @@ func (s *Store) EnqueueIssue(ctx context.Context, owner, repo string, number int
// EnqueueChatTask inserts a queued chat task with a store-allocated synthetic
// issue number. origin names the conversation that created it, if any.
func (s *Store) EnqueueChatTask(ctx context.Context, owner, repo, title, body, wf, identity, origin string, inputs map[string]any) (*workflow.Task, error) {
return insertChatTask(ctx, s.queries(), owner, repo, title, body, wf, identity, origin, inputs)
func (s *Store) EnqueueChatTask(ctx context.Context, owner, repo, title, body, wf, identity, origin, draft string, inputs map[string]any) (*workflow.Task, error) {
return insertChatTask(ctx, s.queries(), owner, repo, title, body, wf, identity, origin, draft, inputs)
}
func insertChatTask(ctx context.Context, q *postgresdb.Queries, owner, repo, title, body, wf, identity, origin string, inputs map[string]any) (*workflow.Task, error) {
func insertChatTask(ctx context.Context, q *postgresdb.Queries, owner, repo, title, body, wf, identity, origin, draft string, inputs map[string]any) (*workflow.Task, error) {
encoded, err := task.EncodeInputs(inputs)
if err != nil {
return nil, err
@@ -170,6 +170,8 @@ func insertChatTask(ctx context.Context, q *postgresdb.Queries, owner, repo, tit
Owner: owner, Repo: repo, Title: title, Body: body, Workflow: wf, Identity: identity,
Inputs: encoded,
Origin: origin,
DraftYaml: draft,
DraftDigest: draftDigest(draft),
FallbackIssueNumber: syntheticIssueNumberBase - 1,
})
if errors.Is(err, pgx.ErrNoRows) {
@@ -181,6 +183,14 @@ func insertChatTask(ctx context.Context, q *postgresdb.Queries, owner, repo, tit
return taskFromRow(t), nil
}
// draftDigest pins a test run's definition; no draft has no digest.
func draftDigest(draft string) string {
if draft == "" {
return ""
}
return workflow.DigestDefinition(draft)
}
// EnqueueBindingTask claims the dispatch ledger row for the capture's
// delivery, inserts the task, stamps its binding provenance and records the
// task on the ledger row in one transaction. A concurrent claim of the same
@@ -204,7 +214,7 @@ func (s *Store) EnqueueBindingTask(ctx context.Context, owner, repo, title, body
if n == 0 {
return nil, fmt.Errorf("store: claim binding dispatch: capture %q not found", captureID)
}
t, err := insertChatTask(ctx, q, owner, repo, title, body, wf, identity, "", inputs)
t, err := insertChatTask(ctx, q, owner, repo, title, body, wf, identity, "", "", inputs)
if err != nil {
return nil, err
}