diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index a841f5f9f..aa071b145 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -1777,7 +1777,7 @@ func playbookInput(task *workflow.Task) playbook.DispatchInput { return playbook.DispatchInput{ Labels: labels, Kind: kind, - TaskID: workintake.TaskEnvelope{Owner: task.Owner, Repo: task.Repo, Number: task.IssueNumber}.IdempotencyKey(), + TaskID: workintake.TaskEnvelope{Org: task.Org, Identity: task.Identity, Owner: task.Owner, Repo: task.Repo, Number: task.IssueNumber}.IdempotencyKey(), Event: map[string]any{ "kind": kind, "labels": labels, diff --git a/internal/domain/eda/playbook/playbook_test.go b/internal/domain/eda/playbook/playbook_test.go index 7b5d43219..afd741e1d 100644 --- a/internal/domain/eda/playbook/playbook_test.go +++ b/internal/domain/eda/playbook/playbook_test.go @@ -367,7 +367,9 @@ actions: // TestDispatchInputCarriesTaskIdentity: the originating task's identity is // carried on the dispatch input end to end, so a caller can derive the // event_id half of the playbook_dispatches ledger key. The chosen identity is -// the TaskEnvelope.IdempotencyKey() string ("archie:owner/repo/number") -- +// the TaskEnvelope.IdempotencyKey() string +// ("archie:org/identity/owner/repo/number", empty org and identity resolving +// to the default org) -- // the value available at the discovery/dispatch point (pollNATS + webhook // receiver both compute kind/labels from a TaskEnvelope before any // workflow.Task row exists), NOT a workflow.Task.ID int64. diff --git a/internal/domain/workflow/task/task.go b/internal/domain/workflow/task/task.go index 863d637ee..d0df7a328 100644 --- a/internal/domain/workflow/task/task.go +++ b/internal/domain/workflow/task/task.go @@ -11,6 +11,7 @@ import ( "context" "time" + "github.com/samcharles93/archie-core/internal/domain/org" "github.com/samcharles93/archie-core/internal/events" "github.com/samcharles93/archie-core/internal/taskstate" ) @@ -86,6 +87,11 @@ type Task struct { // deployments). Used to scope /approve and /cancel authorization so // one identity cannot control another's chat-spawned tasks. Identity string `json:"identity"` + // Org is the org the task belongs to. The State Store derives it from + // the task's identity when the row is written + // (docs/prds/orgs-and-access.md, "Events and task identity"); a task + // of a single-operator install belongs to the default org. + Org org.OrgID `json:"org,omitempty"` // BindingID and BindingVersion are stamped when a task was created // from a playbook binding dispatch (t2db.4 Phase B). They record // provenance: which binding fired this task and at what version, so diff --git a/internal/domain/workintake/envelope.go b/internal/domain/workintake/envelope.go index c9289bf30..c51fa370c 100644 --- a/internal/domain/workintake/envelope.go +++ b/internal/domain/workintake/envelope.go @@ -16,6 +16,8 @@ import ( "fmt" "strconv" "strings" + + "github.com/samcharles93/archie-core/internal/domain/org" ) // dedupKeyPrefix namespaces idempotency keys so they cannot collide with @@ -47,6 +49,11 @@ type TaskEnvelope struct { // enqueues the task under the right owner. Identity string `json:"identity,omitempty"` + // Org is the org the identity serves, resolved by the producer before + // publishing (docs/prds/orgs-and-access.md, "Events and task identity"). + // Empty means the default org of a single-operator install. + Org org.OrgID `json:"org,omitempty"` + // Kind is the routing category, chosen by the publisher. Empty means // KindDefault, which is also what messages queued before this field // existed decode to. @@ -101,9 +108,24 @@ func (t TaskEnvelope) Ref() string { } // IdempotencyKey identifies this issue for delivery deduplication, so -// rediscovering it on a later poll does not enqueue the same work twice. +// rediscovering it on a later poll does not enqueue the same work twice. The +// key is org/identity/owner/repo/number (docs/prds/orgs-and-access.md, +// "Events and task identity"): the poller and the webhook receiver resolve +// the org and identity before publishing, so the same issue delivered both +// ways still gives one key, and the State Store's task uniqueness uses the +// same fields. This method is the one place that shape is produced. func (t TaskEnvelope) IdempotencyKey() string { - return dedupKeyPrefix + t.Owner + "/" + t.Repo + "/" + strconv.Itoa(t.Number) + return dedupKeyPrefix + string(t.org()) + "/" + t.Identity + "/" + + t.Owner + "/" + t.Repo + "/" + strconv.Itoa(t.Number) +} + +// org resolves the envelope's org, defaulting to the default org a record of +// a single-operator install belongs to. +func (t TaskEnvelope) org() org.OrgID { + if t.Org == "" { + return org.DefaultOrgID + } + return t.Org } // Subject returns the address this envelope routes to. diff --git a/internal/domain/workintake/envelope_test.go b/internal/domain/workintake/envelope_test.go index 6ad7ffdfa..7cee0603c 100644 --- a/internal/domain/workintake/envelope_test.go +++ b/internal/domain/workintake/envelope_test.go @@ -123,10 +123,21 @@ func TestTaskEnvelopeRef(t *testing.T) { } func TestTaskEnvelopeIdempotencyKey(t *testing.T) { + // An envelope with no org and no identity is the single-operator shape: + // both segments resolve to their default rather than disappearing, and + // the key is org/identity/owner/repo/number + // (docs/prds/orgs-and-access.md, "Events and task identity"). e := TaskEnvelope{Owner: "samcharles93", Repo: "archie-core", Number: 42} - if got, want := e.IdempotencyKey(), "archie:samcharles93/archie-core/42"; got != want { + if got, want := e.IdempotencyKey(), "archie:default//samcharles93/archie-core/42"; got != want { t.Fatalf("IdempotencyKey() = %q, want %q", got, want) } + // A resolved org and identity are carried verbatim; the same issue + // delivered by poll and by webhook with the same resolved identity + // still produces one key. + e.Org, e.Identity = "soc", "i-1" + if got, want := e.IdempotencyKey(), "archie:soc/i-1/samcharles93/archie-core/42"; got != want { + t.Fatalf("IdempotencyKey() with org = %q, want %q", got, want) + } } func TestTaskEnvelopeSubject(t *testing.T) { diff --git a/internal/domain/workintake/review_envelope.go b/internal/domain/workintake/review_envelope.go index 4dce1f52b..86d0a1c78 100644 --- a/internal/domain/workintake/review_envelope.go +++ b/internal/domain/workintake/review_envelope.go @@ -5,6 +5,8 @@ import ( "errors" "fmt" "strconv" + + "github.com/samcharles93/archie-core/internal/domain/org" ) // ErrInvalidReviewReaction reports a review reaction that cannot be routed. @@ -35,6 +37,11 @@ type ReviewCommentEnvelope struct { Body string `json:"body,omitempty"` Path string `json:"path,omitempty"` Line int `json:"line,omitempty"` + + // Org is the org the reaction's repo belongs to, resolved by the producer + // before publishing; empty means the default org of a single-operator + // install (docs/prds/orgs-and-access.md, "Events and task identity"). + Org org.OrgID `json:"org,omitempty"` } // Validate reports whether the envelope has the coordinates and external ID @@ -88,13 +95,18 @@ func (e ReviewCommentEnvelope) Ref() string { } // IdempotencyKey identifies one forge review record independently of delivery -// source. +// source. The key follows the task key's org/identity rule +// (docs/prds/orgs-and-access.md, "Events and task identity"), scoped to one +// review record. func (e ReviewCommentEnvelope) IdempotencyKey() string { id := e.ReviewID if e.Kind == ReviewReactionComment { id = e.CommentID } - return dedupKeyPrefix + "review/" + e.Owner + "/" + e.Repo + "/" + strconv.Itoa(e.PRNumber) + "/" + string(e.Kind) + "/" + strconv.FormatInt(id, 10) + if e.Org == "" { + e.Org = org.DefaultOrgID + } + return dedupKeyPrefix + "review/" + string(e.Org) + "/" + e.Owner + "/" + e.Repo + "/" + strconv.Itoa(e.PRNumber) + "/" + string(e.Kind) + "/" + strconv.FormatInt(id, 10) } // Subject returns the reaction address for this envelope. diff --git a/internal/domain/workintake/review_envelope_test.go b/internal/domain/workintake/review_envelope_test.go index cb0d51a24..e47a3b2db 100644 --- a/internal/domain/workintake/review_envelope_test.go +++ b/internal/domain/workintake/review_envelope_test.go @@ -41,12 +41,17 @@ func TestReviewCommentEnvelopeKindSeparatesIdempotencyKeys(t *testing.T) { if review.IdempotencyKey() == comment.IdempotencyKey() { t.Fatalf("review and comment idempotency keys both %q; independent ID sequences collided", review.IdempotencyKey()) } - if got, want := review.IdempotencyKey(), "archie:review/acme/widgets/42/review/17"; got != want { + if got, want := review.IdempotencyKey(), "archie:review/default/acme/widgets/42/review/17"; got != want { t.Errorf("review key = %q, want %q", got, want) } - if got, want := comment.IdempotencyKey(), "archie:review/acme/widgets/42/comment/17"; got != want { + if got, want := comment.IdempotencyKey(), "archie:review/default/acme/widgets/42/comment/17"; got != want { t.Errorf("comment key = %q, want %q", got, want) } + // A resolved org is carried into the key. + review.Org = "soc" + if got, want := review.IdempotencyKey(), "archie:review/soc/acme/widgets/42/review/17"; got != want { + t.Errorf("org-scoped review key = %q, want %q", got, want) + } } func TestReviewCommentEnvelopeValidation(t *testing.T) { diff --git a/internal/infrastructure/postgres/postgresdb/tasks.sql.go b/internal/infrastructure/postgres/postgresdb/tasks.sql.go index 4bca88356..57c284f8b 100644 --- a/internal/infrastructure/postgres/postgresdb/tasks.sql.go +++ b/internal/infrastructure/postgres/postgresdb/tasks.sql.go @@ -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 ` diff --git a/internal/infrastructure/postgres/queries/tasks.sql b/internal/infrastructure/postgres/queries/tasks.sql index 83f02d76d..1055b4214 100644 --- a/internal/infrastructure/postgres/queries/tasks.sql +++ b/internal/infrastructure/postgres/queries/tasks.sql @@ -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 *; diff --git a/internal/infrastructure/postgres/store.go b/internal/infrastructure/postgres/store.go index fbef1fda9..4e92f0786 100644 --- a/internal/infrastructure/postgres/store.go +++ b/internal/infrastructure/postgres/store.go @@ -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,