Files

41 lines
2.3 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 whose child can still do the work -- a
-- child that ended dead or closed_wont_do freed its key -- 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. The issue number comes from chat_issue_numbers, as for
-- InsertChatTask. The callee is started by the identity the caller's
-- workflow declares, read from its pinned definition: a callee never inherits the
-- identity that started its caller, and runs as the one its own workflow
-- declares. A callee of a test run is a test run too, so it cannot write to a
-- forge either.
WITH caller AS (
SELECT t.* FROM tasks t WHERE t.id = $1 FOR UPDATE
), number AS (
INSERT INTO chat_issue_numbers (owner, repo, last_number)
SELECT caller.owner, caller.repo, sqlc.arg('fallback_issue_number')::bigint + 1 FROM caller
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, workspace_id, inputs, call_parent_task_id, call_depth, call_key)
SELECT
caller.owner,
caller.repo,
number.last_number,
sqlc.arg(title), sqlc.arg(body), 'chat', sqlc.arg(workflow),
CASE WHEN caller.source = 'test' THEN 'test' ELSE 'chat' END, sqlc.arg(identity),
caller.org_id, caller.workspace_id, sqlc.arg(inputs),
caller.id, caller.call_depth + 1, sqlc.arg('call_key')
FROM caller, number
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 <> '' AND status NOT IN ('dead', 'closed_wont_do') 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;