feat(daemon): run each workflow as its declared identity

Every attempt checks that the identity that started the task and the
identity the workflow declares may both run it, recording a denial for
either. A workflow that declares no identity is refused rather than
running as its starter or the root. A workflow.call callee is started by
its caller's declared identity instead of inheriting the caller's row.
The workflow settings panel marks the identity as required and offers
the agent identities.
This commit is contained in:
sam committed 2026-10-11 14:49:38 +00:00
1 parent d66dca44e0
commit f765e23812
11 files changed
+162 -79

No files matched your search

@@ -27,7 +27,7 @@ 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
SELECT caller.owner, caller.repo, $9::bigint + 1 FROM caller
ON CONFLICT (owner, repo) DO UPDATE SET last_number = chat_issue_numbers.last_number + 1
RETURNING last_number
)
@@ -36,11 +36,11 @@ SELECT
caller.owner,
caller.repo,
number.last_number,
$2, $3, 'chat', $4, 'chat', caller.identity,
caller.org_id, caller.workspace_id, $5,
caller.id, caller.call_depth + 1, $6
$2, $3, 'chat', $4, 'chat', $5,
caller.org_id, caller.workspace_id, $6,
caller.id, caller.call_depth + 1, $7
FROM caller, number
WHERE caller.status = 'running' AND caller.call_depth + 1 <= $7::int
WHERE caller.status = 'running' AND caller.call_depth + 1 <= $8::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
`
@@ -50,6 +50,7 @@ type EnqueueCallTaskParams struct {
Title string
Body string
Workflow string
Identity string
Inputs string
CallKey string
MaxDepth int32
@@ -65,13 +66,17 @@ type EnqueueCallTaskParams struct {
// 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. The issue number comes from chat_issue_numbers, as for
// InsertChatTask.
// InsertChatTask. The callee is started by the identity the caller's
// workflow declares, read from its pinned definition: a callee never inherits the
// identity that started its caller, and runs as the one its own workflow
// declares.
func (q *Queries) EnqueueCallTask(ctx context.Context, arg EnqueueCallTaskParams) (Task, error) {
row := q.db.QueryRow(ctx, enqueueCallTask,
arg.ID,
arg.Title,
arg.Body,
arg.Workflow,
arg.Identity,
arg.Inputs,
arg.CallKey,
arg.MaxDepth,
@@ -8,7 +8,10 @@
-- 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. The issue number comes from chat_issue_numbers, as for
-- InsertChatTask.
-- InsertChatTask. The callee is started by the identity the caller's
-- workflow declares, read from its pinned definition: a callee never inherits the
-- identity that started its caller, and runs as the one its own workflow
-- declares.
WITH caller AS (
SELECT t.* FROM tasks t WHERE t.id = $1 FOR UPDATE
), number AS (
@@ -22,7 +25,7 @@ SELECT
caller.owner,
caller.repo,
number.last_number,
sqlc.arg(title), sqlc.arg(body), 'chat', sqlc.arg(workflow), 'chat', caller.identity,
sqlc.arg(title), sqlc.arg(body), 'chat', sqlc.arg(workflow), 'chat', sqlc.arg(identity),
caller.org_id, caller.workspace_id, sqlc.arg(inputs),
caller.id, caller.call_depth + 1, sqlc.arg('call_key')
FROM caller, number
+31 -4
View File
@@ -818,17 +818,23 @@ func (s *Store) TaskByID(ctx context.Context, taskID int64) (*workflow.Task, err
}
// StartCall enqueues a workflow.call callee inheriting the caller's org,
// workspace, identity and repo, and returns the callee already started under
// callerTaskID and callKey when there is one. A child that ended dead or
// declined freed its key, so such a call starts a fresh child. The caller must
// be running and within workflow.MaxCallDepth.
// workspace and repo, started by the identity the caller's workflow declares,
// and returns the callee already started under callerTaskID and callKey when
// there is one. A child that ended dead or declined freed its key, so such a
// call starts a fresh child. The caller must be running and within
// workflow.MaxCallDepth.
func (s *Store) StartCall(ctx context.Context, callerTaskID int64, callKey, wf string, inputs map[string]any) (*workflow.Task, error) {
encoded, err := task.EncodeInputs(inputs)
if err != nil {
return nil, err
}
starter, err := s.callerIdentity(ctx, callerTaskID)
if err != nil {
return nil, err
}
callee, err := s.queries().EnqueueCallTask(ctx, postgresdb.EnqueueCallTaskParams{
ID: callerTaskID,
Identity: starter,
FallbackIssueNumber: syntheticIssueNumberBase - 1,
Title: fmt.Sprintf("workflow.call %s from task %d", wf, callerTaskID),
Body: fmt.Sprintf("Started by a workflow.call step from task %d (workflow %q); the call's inputs travel with the task.", callerTaskID, wf),
@@ -849,6 +855,27 @@ func (s *Store) StartCall(ctx context.Context, callerTaskID int64, callKey, wf s
return taskFromRow(callee), nil
}
// callerIdentity is the identity the caller's pinned workflow declares, the
// one its callees are started by. The pin does not change while the caller's
// call step runs.
func (s *Store) callerIdentity(ctx context.Context, callerTaskID int64) (string, error) {
caller, err := s.queries().TaskByID(ctx, postgresdb.TaskByIDParams{ID: callerTaskID, ScopeOrg: scopeOrg(ctx)})
if errors.Is(err, pgx.ErrNoRows) {
return "", fmt.Errorf("store: workflow.call caller task %d does not exist: %w", callerTaskID, storecontract.ErrCallNotYours)
}
if err != nil {
return "", err
}
iface, err := task.ParseWorkflowInterface(caller.WorkflowDefinitionYaml)
if err != nil {
return "", fmt.Errorf("store: workflow.call caller task %d: %w", callerTaskID, err)
}
if iface.Identity == "" {
return "", fmt.Errorf("store: workflow.call caller task %d runs a workflow that declares no identity", callerTaskID)
}
return iface.Identity, nil
}
// callRefusal explains why EnqueueCallTask inserted nothing.
func (s *Store) callRefusal(ctx context.Context, callerTaskID int64) error {
caller, err := s.queries().TaskByID(ctx, postgresdb.TaskByIDParams{ID: callerTaskID, ScopeOrg: scopeOrg(ctx)})