fix(identity): use System as the one root identity and record who created every task (#1851)
deploy / plan (push) Successful in 25s
deploy / gate (push) Successful in 7m57s
deploy / agent-image (push) Successful in 21s
deploy / dist-zip (push) Skipped
deploy / archied-image (push) Successful in 2m12s

Drop the bot_user-derived root identity and the empty-identity sentinel. System
polls forge connections and owns instance-level work; a polled task stores the
polling identity, task_queued and work_request_submitted carry task id, actor
and principal, and an empty task identity is refused. Migration 0072 moves
empty tasks.identity, the identity-grants root key and the old root's
connection permits to System so a re-poll dedups.
This commit is contained in:
sam committed 2026-11-10 20:01:16 +11:00
1 parent 04798b990a
commit e3ffefdc7d
36 files changed
+283 -189

No files matched your search

@@ -0,0 +1,52 @@
-- System is the one root identity. Tasks that stored '' for the root, the
-- identity-grants "root" key and the connection permits of the identity the
-- config's bot_user used to seed all move to System, so the poller's new
-- identity matches the stored rows and a re-poll dedups.
-- The old root is the first identity imported from the config.
-- +goose Up
UPDATE tasks SET identity = '00000000-0000-5000-8000-000000000001' WHERE identity = '';
ALTER TABLE tasks ADD CONSTRAINT tasks_identity_set CHECK (identity <> '');
UPDATE resources r
SET value = convert_to(m.doc::text, 'UTF8')
FROM (
SELECT g.org_id, g.kind, (g.v - 'root') || jsonb_build_object('identities',
CASE WHEN jsonb_typeof(g.v -> 'identities') = 'object' THEN g.v -> 'identities' ELSE '{}'::jsonb END
|| jsonb_build_object('00000000-0000-5000-8000-000000000001',
CASE WHEN jsonb_typeof(g.v -> 'root') = 'array' THEN g.v -> 'root' ELSE '[]'::jsonb END)) AS doc
FROM (
SELECT org_id, kind, convert_from(value, 'UTF8')::jsonb AS v FROM resources WHERE kind = 'identity-grants'
) g
WHERE g.v ? 'root'
) m
WHERE r.org_id = m.org_id AND r.kind = m.kind;
UPDATE resources r
SET value = convert_to(m.doc::text, 'UTF8')
FROM (
SELECT f.org_id, f.kind, (
SELECT COALESCE(jsonb_agg(
CASE WHEN c -> 'permits' ? old.id THEN jsonb_set(c, '{permits}',
((c -> 'permits') - old.id) || jsonb_build_object('00000000-0000-5000-8000-000000000001',
(SELECT jsonb_agg(DISTINCT op) FROM jsonb_array_elements_text(
COALESCE(c -> 'permits' -> '00000000-0000-5000-8000-000000000001', '[]'::jsonb) || (c -> 'permits' -> old.id)) op)))
ELSE c END), '[]'::jsonb)
FROM jsonb_array_elements(f.v) c
) AS doc
FROM (
SELECT org_id, kind, convert_from(value, 'UTF8')::jsonb AS v FROM resources WHERE kind = 'forge-connections'
) f,
(
SELECT identity_id AS id FROM identity_events
WHERE request_id LIKE 'identity-import:%' AND identity_id <> '00000000-0000-5000-8000-000000000001'
ORDER BY id LIMIT 1
) old
WHERE jsonb_typeof(f.v) = 'array'
) m
WHERE r.org_id = m.org_id AND r.kind = m.kind;
-- +goose Down
ALTER TABLE tasks DROP CONSTRAINT tasks_identity_set;
+4 -1
View File
@@ -358,7 +358,7 @@ func (s *Store) EnsureAgentOrgMembership(ctx context.Context) error {
// PrincipalFor assembles the access principal for one identity: the org it
// serves and its memberships (internal/domain/access). An identity in no org
// is the default org with no role, which the shipped role policies never
// permit.
// permit. System owns the default org without a membership row.
func (s *Store) PrincipalFor(ctx context.Context, id identity.IdentityID) (access.Principal, error) {
resolved, err := s.OrgForIdentity(ctx, id)
if err != nil {
@@ -385,6 +385,9 @@ func (s *Store) PrincipalFor(ctx context.Context, id identity.IdentityID) (acces
if err != nil {
return access.Principal{}, err
}
if id == identity.SystemID {
memberships = access.SharedTokenOwner().Memberships
}
return access.Principal{IdentityID: id, Org: resolved, Memberships: memberships, Workspaces: workspaces}, nil
}
+10
View File
@@ -159,9 +159,16 @@ var (
_ workflow.Store = (*Store)(nil)
)
// errTaskIdentity refuses a task nobody created: every task records the
// identity that created it.
var errTaskIdentity = errors.New("task identity is required")
// EnqueueIssue inserts a new queued task for the issue; returns false if the
// issue is already tracked (the idempotency key is owner/repo/number).
func (s *Store) EnqueueIssue(ctx context.Context, owner, repo string, number int, title, body, labels, identity string) (bool, error) {
if identity == "" {
return false, errTaskIdentity
}
n, err := s.queries().EnqueueIssue(ctx, postgresdb.EnqueueIssueParams{
Owner: owner, Repo: repo, IssueNumber: int64(number),
Title: title, Body: body, Labels: labels, Identity: identity,
@@ -179,6 +186,9 @@ func (s *Store) EnqueueChatTask(ctx context.Context, owner, repo, title, body, w
}
func insertChatTask(ctx context.Context, q *postgresdb.Queries, owner, repo, title, body, wf, identity, origin, draft string, inputs map[string]any) (*workflow.Task, error) {
if identity == "" {
return nil, errTaskIdentity
}
encoded, err := task.EncodeInputs(inputs)
if err != nil {
return nil, err