feat(store): workflow.call contract — callee enqueue, parent-scoped status read, task-scoped grant widening (archie-core-t2db.41)

This commit is contained in:
sam committed 2026-09-26 13:36:41 +10:00
1 parent df58c518dc
commit 484ea701fe
24 files changed
+1268 -254

No files matched your search

@@ -0,0 +1,20 @@
-- Workflow calls: the callee linkage on the task row
-- (docs/prds/workflow-calls.md).
--
-- tasks.call_parent_task_id names the task whose workflow.call step started
-- this one (0 on a root run) and call_depth counts workflow.call hops from
-- the root. Both default so every existing row and writer reads as a depth-0
-- root run; the depth limit itself is re-checked by the engine and the
-- enqueue, so no path can write past it.
-- +goose Up
ALTER TABLE tasks ADD COLUMN call_parent_task_id bigint NOT NULL DEFAULT 0;
ALTER TABLE tasks ADD COLUMN call_depth int NOT NULL DEFAULT 0;
CREATE INDEX idx_tasks_call_parent ON tasks (call_parent_task_id);
-- +goose Down
DROP INDEX idx_tasks_call_parent;
ALTER TABLE tasks DROP COLUMN call_depth;
ALTER TABLE tasks DROP COLUMN call_parent_task_id;
@@ -0,0 +1,118 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: call_tasks.sql
package postgresdb
import (
"context"
)
const callStatusDetail = `-- name: CallStatusDetail :one
SELECT detail FROM transitions WHERE task_id = $1 ORDER BY id DESC LIMIT 1
`
// The latest transition detail of one call's callee, the summary a wait:true
// caller receives (docs/prds/workflow-calls.md).
func (q *Queries) CallStatusDetail(ctx context.Context, taskID int64) (string, error) {
row := q.db.QueryRow(ctx, callStatusDetail, taskID)
var detail string
err := row.Scan(&detail)
return detail, err
}
const enqueueCallTask = `-- name: EnqueueCallTask :one
WITH caller AS (
SELECT t.id, t.owner, t.repo, t.issue_number, t.title, t.body, t.labels, t.status, t.workflow, t.stage, t.branch, t.plan, t.notes, t.pr_number, t.tokens_used, t.iterations, t.attempt, t.park_reason, t.watch_comment_id, t.park_class, t.remediation_rounds, t.retry_count, t.source, t.identity, t.binding_id, t.binding_version, t.review_payload, t.workflow_definition_version, t.workflow_definition_digest, t.workflow_definition_yaml, t.created_at, t.updated_at, t.review_cursor, t.inputs, t.org_id, t.workspace_id, t.call_parent_task_id, t.call_depth FROM tasks t WHERE t.id = $1 FOR UPDATE
)
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, workflow, source, identity, org_id, workspace_id, inputs, call_parent_task_id, call_depth)
SELECT
caller.owner,
caller.repo,
COALESCE((
SELECT MAX(existing.issue_number) FROM tasks existing
WHERE existing.owner = caller.owner
AND existing.repo = caller.repo
AND existing.source = 'chat'
), $2::bigint) + 1,
$3, $4, 'chat', $5, 'chat', caller.identity,
caller.org_id, caller.workspace_id, $6,
caller.id, caller.call_depth + 1
FROM caller
WHERE caller.status = 'running' AND caller.call_depth + 1 <= $7::int
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, stage, 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
`
type EnqueueCallTaskParams struct {
ID int64
FallbackIssueNumber int64
Title string
Body string
Workflow string
Inputs string
MaxDepth int32
}
// The callee of a workflow.call step (docs/prds/workflow-calls.md). The
// caller row is read FOR UPDATE, so two steps of the same run cannot race a
// depth re-check, and the callee inherits the caller's org, workspace,
// identity, owner and repo: a called workflow works the same run's context
// under its own definition and profile. The issue number is a fresh
// synthetic one for the inherited owner/repo, the same allocator
// InsertChatTask uses. The insert refuses a caller that is not running and
// a call that would pass the depth limit (the engine checks it too; the
// store is what owns the table, so it re-checks).
func (q *Queries) EnqueueCallTask(ctx context.Context, arg EnqueueCallTaskParams) (Task, error) {
row := q.db.QueryRow(ctx, enqueueCallTask,
arg.ID,
arg.FallbackIssueNumber,
arg.Title,
arg.Body,
arg.Workflow,
arg.Inputs,
arg.MaxDepth,
)
var i Task
err := row.Scan(
&i.ID,
&i.Owner,
&i.Repo,
&i.IssueNumber,
&i.Title,
&i.Body,
&i.Labels,
&i.Status,
&i.Workflow,
&i.Stage,
&i.Branch,
&i.Plan,
&i.Notes,
&i.PrNumber,
&i.TokensUsed,
&i.Iterations,
&i.Attempt,
&i.ParkReason,
&i.WatchCommentID,
&i.ParkClass,
&i.RemediationRounds,
&i.RetryCount,
&i.Source,
&i.Identity,
&i.BindingID,
&i.BindingVersion,
&i.ReviewPayload,
&i.WorkflowDefinitionVersion,
&i.WorkflowDefinitionDigest,
&i.WorkflowDefinitionYaml,
&i.CreatedAt,
&i.UpdatedAt,
&i.ReviewCursor,
&i.Inputs,
&i.OrgID,
&i.WorkspaceID,
&i.CallParentTaskID,
&i.CallDepth,
)
return i, err
}
@@ -323,6 +323,8 @@ type Task struct {
Inputs string
OrgID string
WorkspaceID string
CallParentTaskID int64
CallDepth int32
}
type ToolCall struct {
@@ -48,7 +48,7 @@ func (q *Queries) BeginRemediationTask(ctx context.Context, arg BeginRemediation
const claimByIssue = `-- name: ClaimByIssue :one
UPDATE tasks SET status = 'running', attempt = attempt + 1, updated_at = now()
WHERE owner = $1 AND repo = $2 AND issue_number = $3 AND status = 'queued'
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, stage, 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
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, stage, 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
`
type ClaimByIssueParams struct {
@@ -97,6 +97,8 @@ func (q *Queries) ClaimByIssue(ctx context.Context, arg ClaimByIssueParams) (Tas
&i.Inputs,
&i.OrgID,
&i.WorkspaceID,
&i.CallParentTaskID,
&i.CallDepth,
)
return i, err
}
@@ -111,7 +113,7 @@ WHERE id = (
FOR UPDATE SKIP LOCKED
LIMIT 1
)
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, stage, 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
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, stage, 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
`
// FOR UPDATE SKIP LOCKED replaces the SQLite single-writer assumption: two
@@ -157,6 +159,8 @@ func (q *Queries) ClaimNextTask(ctx context.Context) (Task, error) {
&i.Inputs,
&i.OrgID,
&i.WorkspaceID,
&i.CallParentTaskID,
&i.CallDepth,
)
return i, err
}
@@ -266,7 +270,7 @@ VALUES (
'default'
)
)
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, stage, 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
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, stage, 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
`
type InsertChatTaskParams struct {
@@ -331,6 +335,8 @@ func (q *Queries) InsertChatTask(ctx context.Context, arg InsertChatTaskParams)
&i.Inputs,
&i.OrgID,
&i.WorkspaceID,
&i.CallParentTaskID,
&i.CallDepth,
)
return i, err
}
@@ -619,7 +625,7 @@ func (q *Queries) StampTaskBinding(ctx context.Context, arg StampTaskBindingPara
}
const taskByID = `-- name: TaskByID :one
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, stage, 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 FROM tasks WHERE id = $1
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, stage, 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 FROM tasks WHERE id = $1
`
func (q *Queries) TaskByID(ctx context.Context, id int64) (Task, error) {
@@ -662,12 +668,14 @@ func (q *Queries) TaskByID(ctx context.Context, id int64) (Task, error) {
&i.Inputs,
&i.OrgID,
&i.WorkspaceID,
&i.CallParentTaskID,
&i.CallDepth,
)
return i, err
}
const taskByIssue = `-- name: TaskByIssue :one
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, stage, 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 FROM tasks WHERE owner = $1 AND repo = $2 AND issue_number = $3
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, stage, 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 FROM tasks WHERE owner = $1 AND repo = $2 AND issue_number = $3
`
type TaskByIssueParams struct {
@@ -716,12 +724,14 @@ func (q *Queries) TaskByIssue(ctx context.Context, arg TaskByIssueParams) (Task,
&i.Inputs,
&i.OrgID,
&i.WorkspaceID,
&i.CallParentTaskID,
&i.CallDepth,
)
return i, err
}
const taskByPR = `-- name: TaskByPR :one
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, stage, 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 FROM tasks WHERE owner = $1 AND repo = $2 AND pr_number = $3 AND status = $4
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, stage, 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 FROM tasks WHERE owner = $1 AND repo = $2 AND pr_number = $3 AND status = $4
`
type TaskByPRParams struct {
@@ -779,6 +789,8 @@ func (q *Queries) TaskByPR(ctx context.Context, arg TaskByPRParams) (Task, error
&i.Inputs,
&i.OrgID,
&i.WorkspaceID,
&i.CallParentTaskID,
&i.CallDepth,
)
return i, err
}
@@ -0,0 +1,34 @@
-- name: EnqueueCallTask :one
-- The callee of a workflow.call step (docs/prds/workflow-calls.md). The
-- caller row is read FOR UPDATE, so two steps of the same run cannot race a
-- depth re-check, and the callee inherits the caller's org, workspace,
-- identity, owner and repo: a called workflow works the same run's context
-- under its own definition and profile. The issue number is a fresh
-- synthetic one for the inherited owner/repo, the same allocator
-- InsertChatTask uses. The insert refuses a caller that is not running and
-- a call that would pass the depth limit (the engine checks it too; the
-- store is what owns the table, so it re-checks).
WITH caller AS (
SELECT t.* FROM tasks t WHERE t.id = $1 FOR UPDATE
)
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, workflow, source, identity, org_id, workspace_id, inputs, call_parent_task_id, call_depth)
SELECT
caller.owner,
caller.repo,
COALESCE((
SELECT MAX(existing.issue_number) FROM tasks existing
WHERE existing.owner = caller.owner
AND existing.repo = caller.repo
AND existing.source = 'chat'
), sqlc.arg('fallback_issue_number')::bigint) + 1,
sqlc.arg(title), sqlc.arg(body), 'chat', sqlc.arg(workflow), 'chat', caller.identity,
caller.org_id, caller.workspace_id, sqlc.arg(inputs),
caller.id, caller.call_depth + 1
FROM caller
WHERE caller.status = 'running' AND caller.call_depth + 1 <= sqlc.arg('max_depth')::int
RETURNING *;
-- name: CallStatusDetail :one
-- The latest transition detail of one call's callee, the summary a wait:true
-- caller receives (docs/prds/workflow-calls.md).
SELECT detail FROM transitions WHERE task_id = $1 ORDER BY id DESC LIMIT 1;
+83 -3
View File
@@ -12,7 +12,7 @@ import (
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/domain/workflow"
workflowtask "github.com/samcharles93/archie-core/internal/domain/workflow/task"
task "github.com/samcharles93/archie-core/internal/domain/workflow/task"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
"github.com/samcharles93/archie-core/internal/taskstate"
@@ -79,7 +79,7 @@ const syntheticIssueNumberBase = 1_000_000_000_000_000
func taskFromRow(t postgresdb.Task) *workflow.Task {
// The inputs column is only ever written by EncodeInputs, so a decode
// failure cannot arise from stored data this package produced.
inputs, _ := workflowtask.DecodeInputs(t.Inputs)
inputs, _ := task.DecodeInputs(t.Inputs)
return &workflow.Task{
ID: t.ID,
Owner: t.Owner,
@@ -114,6 +114,8 @@ func taskFromRow(t postgresdb.Task) *workflow.Task {
ReviewPayload: t.ReviewPayload,
ParkClass: t.ParkClass,
RemediationRounds: int(t.RemediationRounds),
CallParentTaskID: t.CallParentTaskID,
CallDepth: int(t.CallDepth),
CreatedAt: t.CreatedAt,
UpdatedAt: t.UpdatedAt,
}
@@ -126,6 +128,7 @@ var (
_ storecontract.ConfigSnapshotStore = (*Store)(nil)
_ storecontract.ChannelStatusStore = (*Store)(nil)
_ storecontract.ApplyStatusStore = (*Store)(nil)
_ task.Caller = (*Store)(nil)
_ workflow.Store = (*Store)(nil)
)
@@ -159,7 +162,7 @@ func (s *Store) EnqueueChatTask(ctx context.Context, owner, repo, title, body, w
// EnqueueBindingTask enqueues a binding-triggered task and stamps its binding
// provenance in a second statement (best-effort, as in the SQLite store).
func (s *Store) EnqueueBindingTask(ctx context.Context, owner, repo, title, body, wf, identity, bindingID string, bindingVersion int, inputs map[string]any) (*workflow.Task, error) {
encoded, err := workflowtask.EncodeInputs(inputs)
encoded, err := task.EncodeInputs(inputs)
if err != nil {
return nil, err
}
@@ -539,3 +542,80 @@ func (s *Store) TaskByID(ctx context.Context, taskID int64) (*workflow.Task, err
}
return taskFromRow(t), nil
}
// StartCall enqueues the callee of callerTaskID's workflow.call step
// (docs/prds/workflow-calls.md). The store owns the table, so it re-checks
// both runtime invariants the engine checks: the caller must be running and
// the call must not pass workflow.MaxCallDepth. The callee inherits the
// caller's org, workspace, identity, owner and repo, and takes a fresh
// synthetic issue number, the same allocator chat tasks use.
func (s *Store) StartCall(ctx context.Context, callerTaskID int64, wf string, inputs map[string]any) (*workflow.Task, error) {
encoded, err := task.EncodeInputs(inputs)
if err != nil {
return nil, err
}
callee, err := s.queries().EnqueueCallTask(ctx, postgresdb.EnqueueCallTaskParams{
ID: callerTaskID,
FallbackIssueNumber: syntheticIssueNumberBase - 1,
Title: fmt.Sprintf("workflow.call %s from task %d", wf, callerTaskID),
Body: fmt.Sprintf("Started by a workflow.call step from task %d (workflow %q); the call's inputs travel with the task.", callerTaskID, wf),
Workflow: wf,
Inputs: encoded,
MaxDepth: int32(workflow.MaxCallDepth),
})
if errors.Is(err, pgx.ErrNoRows) {
// The WITH reads the caller FOR UPDATE before inserting, so "no
// rows" means the caller is missing, not running, or past the depth
// limit. Name which, off a plain read.
return nil, s.callRefusal(ctx, callerTaskID)
}
if err != nil {
return nil, err
}
return taskFromRow(callee), nil
}
// callRefusal names why EnqueueCallTask inserted nothing. The three refusals
// are distinct failures an operator reads differently: a missing caller is a
// problem, a caller that is not running is a stale retry, and a depth
// refusal is the limit doing its job. The two behavioural ones carry the
// caller's detail in the log and the sentinel across the wire.
func (s *Store) callRefusal(ctx context.Context, callerTaskID int64) error {
caller, err := s.queries().TaskByID(ctx, callerTaskID)
if errors.Is(err, pgx.ErrNoRows) {
return fmt.Errorf("store: workflow.call caller task %d does not exist: %w", callerTaskID, storecontract.ErrCallNotYours)
}
if err != nil {
return err
}
if caller.Status != "running" {
return fmt.Errorf("store: workflow.call caller task %d is %s: %w", callerTaskID, caller.Status, storecontract.ErrCallCallerNotRunning)
}
return fmt.Errorf("store: workflow.call from task %d at depth %d: %w", callerTaskID, caller.CallDepth, storecontract.ErrCallDepthExceeded)
}
// CallStatus reads one call's callee for a waiting caller: its status and
// latest transition detail. The parent check is the row check the wire grant
// cannot do: a caller reads only the tasks it started.
func (s *Store) CallStatus(ctx context.Context, callerTaskID, callTaskID int64) (string, string, error) {
callee, err := s.queries().TaskByID(ctx, callTaskID)
if errors.Is(err, pgx.ErrNoRows) {
return "", "", storecontract.ErrCallNotYours
}
if err != nil {
return "", "", err
}
if callee.CallParentTaskID != callerTaskID {
return "", "", storecontract.ErrCallNotYours
}
// A callee that has not moved yet has no transition row; its queue
// status is the whole answer, and the empty detail says so.
detail, err := s.queries().CallStatusDetail(ctx, callTaskID)
if errors.Is(err, pgx.ErrNoRows) {
return callee.Status, "", nil
}
if err != nil {
return "", "", err
}
return callee.Status, detail, nil
}