feat(org): idempotency key org/identity/owner/repo/number in one place, task uniqueness to match (archie-core-e5fn)

This commit is contained in:
sam committed 2026-09-26 11:43:10 +10:00
1 parent 16c52c1a16
commit ee5339eb85
10 files changed
+85 -13

No files matched your search

@@ -249,7 +249,7 @@ func (q *Queries) EnqueueIssue(ctx context.Context, arg EnqueueIssueParams) (int
}
const insertChatTask = `-- name: InsertChatTask :one
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, workflow, source, identity)
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, workflow, source, identity, org_id)
VALUES (
$1, $2,
COALESCE((
@@ -258,7 +258,13 @@ VALUES (
AND existing.repo = $2
AND existing.source = 'chat'
), $3) + 1,
$4, $5, 'chat', $6, 'chat', $7
$4, $5, 'chat', $6, 'chat', $7,
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
ORDER BY m.created_at, m.org_id, m.workspace_id NULLS LAST LIMIT 1),
'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
`
@@ -42,7 +42,7 @@ SELECT status, count(*)::int AS count FROM tasks GROUP BY status;
-- issue numbers; this allocator is the single source of truth for it.
-- fallback_issue_number is only the seed for a repo's first chat task -- the
-- passed value is not the issue number that lands.
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, workflow, source, identity)
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, workflow, source, identity, org_id)
VALUES (
sqlc.arg(owner), sqlc.arg(repo),
COALESCE((
@@ -51,7 +51,13 @@ VALUES (
AND existing.repo = sqlc.arg(repo)
AND existing.source = 'chat'
), sqlc.arg(fallback_issue_number)) + 1,
sqlc.arg(title), sqlc.arg(body), 'chat', sqlc.arg(workflow), 'chat', sqlc.arg(identity)
sqlc.arg(title), sqlc.arg(body), 'chat', sqlc.arg(workflow), 'chat', sqlc.arg(identity),
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),
'default'
)
)
RETURNING *;
@@ -9,6 +9,7 @@ import (
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"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"
@@ -79,6 +80,7 @@ func taskFromRow(t postgresdb.Task) *workflow.Task {
ReviewCursor: t.ReviewCursor,
Source: t.Source,
Identity: t.Identity,
Org: org.OrgID(t.OrgID),
BindingID: t.BindingID,
BindingVersion: int(t.BindingVersion),
Inputs: inputs,