Files
archie/internal/infrastructure/postgres/queries/call_tasks.sql
T
sam b7fd13923a fix(workflow): key a workflow.call on its declared step path
A call site had no durable identity, so any retry of it -- a lost
EnqueueCallTask reply, a failed call-step record, a requeued attempt that
re-runs the stage -- started a second child and its work happened twice.

The call step's declared path within the run is that identity: wrapStep
sets it (branch-qualified, restored on return), a workflow.call step must
declare an id so two call sites cannot share one, and the key crosses
EnqueueCallTaskRequest.call_key into tasks.call_key, which EnqueueCallTask
upserts on so the retry returns the child already started.
2026-10-08 22:31:07 +11:00

33 lines
1.7 KiB
SQL

-- name: EnqueueCallTask :one
-- The callee of a workflow.call step (docs/prds/workflow-calls.md)
--
-- The call key makes this the one write that can be retried: a call site that
-- already started its child gets that child back rather than a second one. The
-- upsert only fires for a keyed callee, and the WHERE below still decides
-- whether the caller may call at all -- a caller that is missing, not running
-- or past the depth limit inserts nothing and returns no row, conflict or not.
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, call_key)
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, sqlc.arg('call_key')
FROM caller
WHERE caller.status = 'running' AND caller.call_depth + 1 <= sqlc.arg('max_depth')::int
ON CONFLICT (call_parent_task_id, call_key) WHERE call_key <> '' DO UPDATE SET call_key = EXCLUDED.call_key
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;