diff --git a/internal/domain/identity/identity.go b/internal/domain/identity/identity.go index 891728e3d..439bb8355 100644 --- a/internal/domain/identity/identity.go +++ b/internal/domain/identity/identity.go @@ -92,6 +92,10 @@ func StableID(legacyName string) IdentityID { // and the request path that resolves it agree without a lookup. func OperatorID() IdentityID { return StableID("operator") } +// AgentID is the bot identity the shipped workflows run as. The org upgrade +// seeds it on every install, so shipped YAML can name its fixed ID. +func AgentID() IdentityID { return StableID("agent") } + func New(id IdentityID, kind Kind, displayName string) (Identity, error) { value := Identity{ID: id, Kind: kind, DisplayName: strings.TrimSpace(displayName), Lifecycle: LifecycleActive, Version: 1} if err := value.Validate(); err != nil { diff --git a/internal/domain/workflow/definition.go b/internal/domain/workflow/definition.go index f6b832a6a..b533079a7 100644 --- a/internal/domain/workflow/definition.go +++ b/internal/domain/workflow/definition.go @@ -70,6 +70,8 @@ type SwitchStep struct { } // RootRef is the identity a workflow declares to run as the instance root. +// Nothing ships with it; stored copies that still name it keep running as +// the root until edited. const RootRef = "root" const switchDefault = "default" diff --git a/internal/domain/workflow/shipped/feasibility.yaml b/internal/domain/workflow/shipped/feasibility.yaml index 08f5ef65f..b6bf71457 100644 --- a/internal/domain/workflow/shipped/feasibility.yaml +++ b/internal/domain/workflow/shipped/feasibility.yaml @@ -1,5 +1,5 @@ id: feasibility -identity: root +identity: 6f129e29-0e26-53246-99bd-74cb4496203b steps: - type: repo.prepare - id: assess diff --git a/internal/domain/workflow/shipped/implement.yaml b/internal/domain/workflow/shipped/implement.yaml index 812395198..6d4b263fb 100644 --- a/internal/domain/workflow/shipped/implement.yaml +++ b/internal/domain/workflow/shipped/implement.yaml @@ -1,5 +1,5 @@ id: implement -identity: root +identity: 6f129e29-0e26-53246-99bd-74cb4496203b steps: - type: repo.prepare - type: gate.repository diff --git a/internal/domain/workflow/shipped/pr-review.yaml b/internal/domain/workflow/shipped/pr-review.yaml index f110acf38..9d26a18e1 100644 --- a/internal/domain/workflow/shipped/pr-review.yaml +++ b/internal/domain/workflow/shipped/pr-review.yaml @@ -1,5 +1,5 @@ id: pr-review -identity: root +identity: 6f129e29-0e26-53246-99bd-74cb4496203b inputs: pr_number: type: number diff --git a/internal/domain/workflow/shipped/remediate.yaml b/internal/domain/workflow/shipped/remediate.yaml index a6753cdc2..991f90d14 100644 --- a/internal/domain/workflow/shipped/remediate.yaml +++ b/internal/domain/workflow/shipped/remediate.yaml @@ -1,5 +1,5 @@ id: remediate -identity: root +identity: 6f129e29-0e26-53246-99bd-74cb4496203b steps: - type: review.start-round - type: repo.prepare diff --git a/internal/domain/workflow/shipped/tdd.yaml b/internal/domain/workflow/shipped/tdd.yaml index 230276b31..892da32f0 100644 --- a/internal/domain/workflow/shipped/tdd.yaml +++ b/internal/domain/workflow/shipped/tdd.yaml @@ -1,5 +1,5 @@ id: tdd -identity: root +identity: 6f129e29-0e26-53246-99bd-74cb4496203b steps: - type: repo.prepare - type: gate.repository diff --git a/internal/domain/workflow/shipped/triage.yaml b/internal/domain/workflow/shipped/triage.yaml index 8044bd050..a3074bc37 100644 --- a/internal/domain/workflow/shipped/triage.yaml +++ b/internal/domain/workflow/shipped/triage.yaml @@ -1,5 +1,5 @@ id: triage -identity: root +identity: 6f129e29-0e26-53246-99bd-74cb4496203b steps: - type: repo.prepare - id: classify diff --git a/internal/infrastructure/postgres/orgs.go b/internal/infrastructure/postgres/orgs.go index ad75b7f9c..87ff90699 100644 --- a/internal/infrastructure/postgres/orgs.go +++ b/internal/infrastructure/postgres/orgs.go @@ -305,6 +305,14 @@ func (s *Store) UpgradeDefaultOrg(ctx context.Context) error { {name: "operator", stamps: []func(context.Context, *postgresdb.Queries) (int64, error){ ensureOrgOperator, }}, + // Its own phase so installs that recorded the operator phase before + // the agent existed still get it. The final assignment below puts it + // in org-sys, and every agent's developer membership grants run. + {name: "agent", stamps: []func(context.Context, *postgresdb.Queries) (int64, error){ + func(ctx context.Context, q *postgresdb.Queries) (int64, error) { + return q.EnsureOrgAgentIdentity(ctx, string(identity.AgentID())) + }, + }}, } for _, p := range phases { if _, err := s.queries().GetOrgUpgradePhase(ctx, p.name); err == nil { diff --git a/internal/infrastructure/postgres/postgresdb/orgupgrade.sql.go b/internal/infrastructure/postgres/postgresdb/orgupgrade.sql.go index f000dbbac..4ccc1fe05 100644 --- a/internal/infrastructure/postgres/postgresdb/orgupgrade.sql.go +++ b/internal/infrastructure/postgres/postgresdb/orgupgrade.sql.go @@ -42,6 +42,21 @@ func (q *Queries) EnsureDefaultWorkspace(ctx context.Context) error { return err } +const ensureOrgAgentIdentity = `-- name: EnsureOrgAgentIdentity :execrows +INSERT INTO identities (id, kind, display_name, lifecycle, version, created_at, updated_at) +VALUES ($1, 'bot', 'Agent', 'active', 1, now(), now()) +ON CONFLICT (id) DO NOTHING +` + +// The bot identity the shipped workflows run as. +func (q *Queries) EnsureOrgAgentIdentity(ctx context.Context, id string) (int64, error) { + result, err := q.db.Exec(ctx, ensureOrgAgentIdentity, id) + if err != nil { + return 0, err + } + return result.RowsAffected(), nil +} + const ensureOrgOperatorIdentity = `-- name: EnsureOrgOperatorIdentity :execrows INSERT INTO identities (id, kind, display_name, lifecycle, version, created_at, updated_at) diff --git a/internal/infrastructure/postgres/queries/orgupgrade.sql b/internal/infrastructure/postgres/queries/orgupgrade.sql index e9049996b..124cfdc04 100644 --- a/internal/infrastructure/postgres/queries/orgupgrade.sql +++ b/internal/infrastructure/postgres/queries/orgupgrade.sql @@ -83,4 +83,10 @@ WHERE NOT EXISTS ( JOIN identities i ON i.id = m.identity_id WHERE m.org_id = $2 AND m.workspace_id IS NULL AND m.role = 'owner' AND i.kind = 'user' ) -ON CONFLICT DO NOTHING; \ No newline at end of file +ON CONFLICT DO NOTHING; + +-- name: EnsureOrgAgentIdentity :execrows +-- The bot identity the shipped workflows run as. +INSERT INTO identities (id, kind, display_name, lifecycle, version, created_at, updated_at) +VALUES ($1, 'bot', 'Agent', 'active', 1, now(), now()) +ON CONFLICT (id) DO NOTHING;