fix(postgres): allocate synthetic issue numbers from a per-repo counter
InsertChatTask and EnqueueCallTask computed MAX(issue_number)+1, so concurrent inserts in one repository took the same number and the second failed on the unique key. Both now bump a chat_issue_numbers row, seeded from the existing maxima, whose row lock serializes allocation.
This commit is contained in:
1 parent
1a06081abe
commit
32cd65a58a
7 files changed
+78
-49
No files matched your search
@@ -57,7 +57,8 @@ SELECT status, count(*)::int AS count FROM tasks WHERE (sqlc.narg(scope_org)::te
|
||||
|
||||
-- name: InsertChatTask :one
|
||||
-- The synthetic issue number keeps chat-sourced tasks off the forge's real
|
||||
-- issue numbers; this allocator is the single source of truth for it.
|
||||
-- issue numbers; chat_issue_numbers is the single source of truth for it, and
|
||||
-- bumping its row serializes concurrent inserts in one repository.
|
||||
-- fallback_issue_number is only the seed for a repo's first chat task -- the
|
||||
-- passed value is not the issue number that lands.
|
||||
-- inputs is the chat task's own workflow inputs; a binding dispatch passes
|
||||
@@ -65,20 +66,20 @@ 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.
|
||||
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)
|
||||
SELECT
|
||||
sqlc.arg(owner), sqlc.arg(repo),
|
||||
COALESCE((
|
||||
SELECT MAX(existing.issue_number) FROM tasks existing
|
||||
WHERE existing.owner = sqlc.arg(owner)
|
||||
AND existing.repo = sqlc.arg(repo)
|
||||
AND existing.source = 'chat'
|
||||
), sqlc.arg(fallback_issue_number)) + 1,
|
||||
sqlc.arg(owner), sqlc.arg(repo), number.last_number,
|
||||
sqlc.arg(title), sqlc.arg(body), 'chat', sqlc.arg(workflow), 'chat', sqlc.arg(identity),
|
||||
owner_org.id,
|
||||
sqlc.arg(inputs),
|
||||
sqlc.arg(origin)
|
||||
FROM (SELECT COALESCE(
|
||||
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)
|
||||
ORDER BY m.created_at, m.org_id, m.workspace_id NULLS LAST LIMIT 1),
|
||||
|
||||
Reference in new issue
Block a user