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
@@ -0,0 +1,19 @@
|
||||
-- chat_issue_numbers holds the last synthetic issue number allocated in each
|
||||
-- repository. Chat and workflow.call tasks take theirs by bumping this row,
|
||||
-- whose row lock serializes concurrent inserts in one repository, which a
|
||||
-- MAX(issue_number) read under READ COMMITTED cannot.
|
||||
|
||||
-- +goose Up
|
||||
|
||||
CREATE TABLE chat_issue_numbers (
|
||||
owner text NOT NULL,
|
||||
repo text NOT NULL,
|
||||
last_number bigint NOT NULL,
|
||||
PRIMARY KEY (owner, repo)
|
||||
);
|
||||
INSERT INTO chat_issue_numbers (owner, repo, last_number)
|
||||
SELECT owner, repo, MAX(issue_number) FROM tasks WHERE source = 'chat' GROUP BY owner, repo;
|
||||
|
||||
-- +goose Down
|
||||
|
||||
DROP TABLE chat_issue_numbers;
|
||||
@@ -25,35 +25,35 @@ func (q *Queries) CallStatusDetail(ctx context.Context, taskID int64) (string, e
|
||||
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.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, t.outputs, t.review_gate, t.rereview_rounds, t.retry_mode, t.resume_from, t.resume_results, t.pending_reviews, t.origin, t.call_key, t.outcome_posted 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, $8::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,
|
||||
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, $7
|
||||
FROM caller
|
||||
WHERE caller.status = 'running' AND caller.call_depth + 1 <= $8::int
|
||||
number.last_number,
|
||||
$2, $3, 'chat', $4, 'chat', caller.identity,
|
||||
caller.org_id, caller.workspace_id, $5,
|
||||
caller.id, caller.call_depth + 1, $6
|
||||
FROM caller, number
|
||||
WHERE caller.status = 'running' AND caller.call_depth + 1 <= $7::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 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
|
||||
`
|
||||
|
||||
type EnqueueCallTaskParams struct {
|
||||
ID int64
|
||||
FallbackIssueNumber int64
|
||||
Title string
|
||||
Body string
|
||||
Workflow string
|
||||
Inputs string
|
||||
CallKey string
|
||||
MaxDepth int32
|
||||
FallbackIssueNumber int64
|
||||
}
|
||||
|
||||
// The callee of a workflow.call step (docs/prds/workflow-calls.md)
|
||||
@@ -64,17 +64,18 @@ type EnqueueCallTaskParams struct {
|
||||
// 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.
|
||||
// conflict or not. The issue number comes from chat_issue_numbers, as for
|
||||
// InsertChatTask.
|
||||
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.CallKey,
|
||||
arg.MaxDepth,
|
||||
arg.FallbackIssueNumber,
|
||||
)
|
||||
var i Task
|
||||
err := row.Scan(
|
||||
|
||||
@@ -114,6 +114,12 @@ type ChannelStatus struct {
|
||||
ObservedAt time.Time
|
||||
}
|
||||
|
||||
type ChatIssueNumber struct {
|
||||
Owner string
|
||||
Repo string
|
||||
LastNumber int64
|
||||
}
|
||||
|
||||
type ConfigSnapshot struct {
|
||||
ID int64
|
||||
Schema string
|
||||
|
||||
@@ -389,33 +389,32 @@ 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)
|
||||
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
|
||||
$1, $2,
|
||||
COALESCE((
|
||||
SELECT MAX(existing.issue_number) FROM tasks existing
|
||||
WHERE existing.owner = $1
|
||||
AND existing.repo = $2
|
||||
AND existing.source = 'chat'
|
||||
), $3) + 1,
|
||||
$4, $5, 'chat', $6, 'chat', $7,
|
||||
$1, $2, number.last_number,
|
||||
$3, $4, 'chat', $5, 'chat', $6,
|
||||
owner_org.id,
|
||||
$8,
|
||||
$9
|
||||
FROM (SELECT COALESCE(
|
||||
(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
|
||||
$7,
|
||||
$8
|
||||
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
|
||||
ORDER BY m.created_at, m.org_id, m.workspace_id NULLS LAST LIMIT 1),
|
||||
'org-sys'
|
||||
) AS id) AS owner_org
|
||||
WHERE $10::text IS NULL OR owner_org.id = $10
|
||||
WHERE $9::text IS NULL OR owner_org.id = $9
|
||||
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
|
||||
`
|
||||
|
||||
type InsertChatTaskParams struct {
|
||||
Owner string
|
||||
Repo string
|
||||
FallbackIssueNumber int64
|
||||
Title string
|
||||
Body string
|
||||
Workflow string
|
||||
@@ -423,10 +422,12 @@ type InsertChatTaskParams struct {
|
||||
Inputs string
|
||||
Origin string
|
||||
ScopeOrg pgtype.Text
|
||||
FallbackIssueNumber int64
|
||||
}
|
||||
|
||||
// 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
|
||||
@@ -438,7 +439,6 @@ func (q *Queries) InsertChatTask(ctx context.Context, arg InsertChatTaskParams)
|
||||
row := q.db.QueryRow(ctx, insertChatTask,
|
||||
arg.Owner,
|
||||
arg.Repo,
|
||||
arg.FallbackIssueNumber,
|
||||
arg.Title,
|
||||
arg.Body,
|
||||
arg.Workflow,
|
||||
@@ -446,6 +446,7 @@ func (q *Queries) InsertChatTask(ctx context.Context, arg InsertChatTaskParams)
|
||||
arg.Inputs,
|
||||
arg.Origin,
|
||||
arg.ScopeOrg,
|
||||
arg.FallbackIssueNumber,
|
||||
)
|
||||
var i Task
|
||||
err := row.Scan(
|
||||
|
||||
@@ -7,24 +7,25 @@
|
||||
-- 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.
|
||||
-- conflict or not. The issue number comes from chat_issue_numbers, as for
|
||||
-- InsertChatTask.
|
||||
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,
|
||||
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,
|
||||
number.last_number,
|
||||
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
|
||||
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 *;
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -62,7 +62,7 @@ func guardTransition(ctx context.Context, q *postgresdb.Queries, taskID int64, f
|
||||
func (s *Store) Close() error { return nil }
|
||||
|
||||
// syntheticIssueNumberBase is the seed for a repo's first chat task. The
|
||||
// COALESCE allocator adds one to the passed fallback, so the fallback here is
|
||||
// allocator adds one to the passed fallback, so the fallback here is
|
||||
// base-1 and the first chat task lands on exactly base: the reserved
|
||||
// JSON-safe floor that keeps synthetic issue numbers clear of real ones.
|
||||
const syntheticIssueNumberBase = 1_000_000_000_000_000
|
||||
|
||||
Reference in new issue
Block a user