refactor(archied): discover issues through organisation connections

This commit is contained in:
sam committed 2026-10-17 13:38:46 +11:00
1 parent d6a2de6fd3
commit 96aaeec06b
20 files changed
+292 -621

No files matched your search

+3 -9
View File
@@ -54,20 +54,14 @@ token = { engine = "env", key = "ARCHIE_GITHUB_TOKEN" }
# let each identity poll.
# webhook_secret = { engine = "env", key = "ARCHIE_WEBHOOK_SECRET" }
# webhook_addr = "0.0.0.0:8645"
# intake, webhook_secret and webhook_addr exist only here. Set on a
# [identities.forge] entry they are REJECTED for the same reason: nothing
# reads them, so the identity would silently keep polling. Serving webhook
# intake per identity is a pending migration, not a setting.
# [[identities]] runs more than one bot identity from a single daemon, each
# with its own forge account and repository membership. Runtime settings
# are shared and edited through the control plane. Intake
# settings are rejected per identity, and [forge].intake = "webhook"/"both"
# is rejected alongside [[identities]]. Minimal shape:
# with its own repository membership. Forge access comes from organisation
# connections, edited through the control plane. [forge].intake =
# "webhook"/"both" is rejected alongside [[identities]]. Minimal shape:
# [[identities]]
# name = "personal" # required; namespaces logs/events/NATS subjects
# bot_user = "your-bot" # required
# forge = { type = "github", host = "https://github.com", token = { engine = "env", key = "ARCHIE_GITHUB_TOKEN_PERSONAL" } }
# org = "acme" # empty inherits the shared org (empty = default)
# granted_credentials = ["openai"] # credential@1 services this identity may resolve (see [containers.credentials])
+1 -2
View File
@@ -29,8 +29,7 @@ as a host binary. Pull it from GHCR: `ghcr.io/samcharles93/archie-agent`.
in this repository's `deployments/` directory on GitHub
(<https://github.com/samcharles93/archie-core/tree/main/deployments>) —
`standalone.toml` (no forge, the simplest start),
`single-forge-github.toml`, `multi-forge-github-gitea.toml` and
`docker-nats-stack.toml` cover the common cases. Every one needs
`single-forge-github.toml` and `docker-nats-stack.toml` cover the common cases. Every one needs
PostgreSQL 18 at `database_url`. Copy whichever fits to:
```bash
-3
View File
@@ -7,9 +7,6 @@ This directory contains configuration templates and guides for different deploym
- **[`single-forge-github.toml`](./single-forge-github.toml)**:
Standard single-forge deployment managing multiple GitHub repositories under a single bot account and GitHub token.
- **[`multi-forge-github-gitea.toml`](./multi-forge-github-gitea.toml)**:
Multi-identity deployment running GitHub and a self-hosted Gitea instance simultaneously with distinct bot accounts, tokens, and repository sets.
- **[`standalone.toml`](./standalone.toml)**: start here. Event-driven
automation with no forge or repositories, local Ollama models, loopback
services and no tokens.
-102
View File
@@ -1,102 +0,0 @@
# ==============================================================================
# Multi-Forge (GitHub + Gitea) Deployment Example
#
# Runs GitHub and a self-hosted Gitea instance simultaneously.
# Each [[identities]] entry defines its own forge, token, and repository list.
# Each identity runs in an isolated background goroutine and worktree directory.
# ==============================================================================
work_dir = "~/.local/share/archie/work"
state_dir = "~/.local/share/archie" # NATS store, task logs
# The State Store's PostgreSQL connection (PostgreSQL 18, required).
# `docker compose up -d postgres` provides this local default; point it at
# your own Postgres 18 server otherwise.
database_url = "postgres://archie:archie@127.0.0.1:5432/archie?sslmode=disable"
[services.gateway]
target = "127.0.0.1:8585"
# The State Store gRPC service owns the PostgreSQL task data; archied and
# archie-gateway dial it and never open those tables themselves.
# This deployment runs the agent in containers, so the target is the Docker
# bridge gateway (172.17.0.1) the container can reach. A non-loopback address
# needs [tls]; see config.example.toml.
[services.state]
target = "172.17.0.1:9090"
[models]
triage = "openai/gpt-6-luna"
planner = "openai/gpt-6-luna"
builder = "openai/gpt-6-luna"
[providers.openai]
class = "openai"
api_key_env = "OPENAI_API_KEY"
[nats]
mode = "embedded"
[containers]
# Pinned to a verified digest -- archie-agent has no published semver tag
# yet (CI only pushes :latest). See
# deployments/systemd-user-service.md#pinning-the-archie-agent-image.
image = "ghcr.io/samcharles93/archie-agent@sha256:870f1b1357979cfea23fccacb24166ebeb9ab321708ef2c2b218a763ed99b090"
pull_policy = "missing"
[budgets]
max_steps = 60
wall_clock = "30m"
# ------------------------------------------------------------------------------
# Identity 1: GitHub Bot
# ------------------------------------------------------------------------------
[[identities]]
name = "github-bot"
bot_user = "archie-github"
bot_email = "archie-github@users.noreply.github.com"
[identities.forge]
type = "github"
host = "https://github.com"
token = { engine = "env", key = "ARCHIE_GITHUB_TOKEN" }
[identities.dispatch]
trigger = "assignee"
ack_reaction = "eyes"
[[identities.repos]]
owner = "my-org"
name = "github-repo-one"
ecosystem = "go"
[[identities.repos]]
owner = "my-org"
name = "github-repo-two"
ecosystem = "node"
# ------------------------------------------------------------------------------
# Identity 2: Self-Hosted Gitea Bot
# ------------------------------------------------------------------------------
[[identities]]
name = "gitea-bot"
bot_user = "archie-gitea"
bot_email = "archie-gitea@example.com"
[identities.forge]
type = "gitea"
host = "https://gitea.example.com"
token = { engine = "env", key = "ARCHIE_GITEA_TOKEN" }
[identities.dispatch]
trigger = "label"
ack_reaction = "eyes"
[[identities.repos]]
owner = "internal-team"
name = "gitea-repo-alpha"
ecosystem = "go"
[[identities.repos]]
owner = "internal-team"
name = "gitea-repo-beta"
ecosystem = "python"
+5 -29
View File
@@ -182,10 +182,9 @@ type boot struct {
// messaging delivers outbound chat messages through the Messaging Service.
messaging *messagingrpc.Client
startGateways []func()
trees *worktree.Manager
identityRunners []*daemon.IdentityRunner
d *daemon.Daemon
startGateways []func()
trees *worktree.Manager
d *daemon.Daemon
// schedulingEngine is the cron/scheduling ticker engine (setupScheduling).
schedulingEngine *scheduling.Engine
@@ -523,30 +522,6 @@ func (b *boot) buildWorktreeManager() {
}
}
func (b *boot) buildTreesAndIdentities(ctx context.Context) error {
cfg, log := b.cfg, b.log
b.buildWorktreeManager()
for _, idCfg := range cfg.Identities {
idForge, idToken := resolveForge(ctx, idCfg.Forge, idCfg.Name, b.secrets, b.forgeExt, log.With("identity", idCfg.Name))
idTrees := &worktree.Manager{
WorkDir: filepath.Join(cfg.WorkDir, "identity-"+idCfg.Name),
Token: idToken,
BotUser: idCfg.BotUser,
BotEmail: idCfg.BotEmail,
BaseURL: idCfg.Forge.Host,
}
runner, err := daemon.NewIdentityRunner(ctx, idCfg, idForge, idTrees, log)
if err != nil {
log.Error("identity construction failed", "identity", idCfg.Name, "err", err)
return err
}
b.identityRunners = append(b.identityRunners, runner)
log.Info("identity configured", "identity", idCfg.Name, "bot_user", idCfg.BotUser, "repos", len(idCfg.Repos))
}
return nil
}
func (b *boot) registerNATSRPC() error {
log := b.log
if b.natsClient == nil {
@@ -618,7 +593,8 @@ func (b *boot) buildDaemon() {
KitLauncher: b.kitLauncher,
ModelEgress: b.modelEgress,
TaskModels: b.taskModels,
Identities: b.identityRunners,
IssueSources: b.issueSources,
OrgPrincipals: b.orgPrincipals,
RootIdentityID: identity.StableID(servicekit.IdentityNames(b.cfg)[0]),
TaskLogs: b.taskLogs,
AgentStatus: b.agentStatus,
+134
View File
@@ -0,0 +1,134 @@
package archied
import (
"context"
"encoding/json"
"github.com/samcharles93/archie-core/internal/app/controlplane"
"github.com/samcharles93/archie-core/internal/app/servicekit"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/daemon"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/org"
)
// orgAgents is every organisation with the principals that may act in it,
// system org first. The system org's own root identity leads its list.
type orgAgents struct {
owner org.OrgID
principals []access.Principal
}
// act returns ctx acting in the organisation. The system org is the
// daemon's own, so it needs no agent principal.
func (o orgAgents) act(ctx context.Context) context.Context {
if o.owner == org.DefaultOrgID {
return ctx
}
return org.WithOrg(access.WithPrincipal(ctx, o.principals[0]), o.owner)
}
func (b *boot) organisations(ctx context.Context) []orgAgents {
directory, _ := b.stateStore.(orgDirectory)
store := scheduleResourceStore{orgs: directory, log: b.log}
var out []orgAgents
for _, owner := range store.owners(ctx) {
entry := orgAgents{owner: owner}
if owner == org.DefaultOrgID && directory != nil {
root := identity.StableID(servicekit.IdentityNames(b.cfgHolder.Get())[0])
if principal, err := directory.PrincipalFor(ctx, root); err == nil {
entry.principals = append(entry.principals, principal)
}
}
if directory != nil {
agents, err := directory.ListOrgAgents(ctx, owner)
if err != nil {
b.log.Warn("organisation agents unavailable", "org", owner, "err", err)
}
for _, agent := range agents {
principal, err := directory.PrincipalFor(ctx, agent.IdentityID)
if err == nil && principal.Org == owner {
entry.principals = append(entry.principals, principal)
}
}
}
if owner != org.DefaultOrgID && len(entry.principals) == 0 {
b.log.Warn("organisation skipped: no agent to act as", "org", owner)
continue
}
out = append(out, entry)
}
return out
}
// orgPrincipals is the first acting principal of every organisation.
func (b *boot) orgPrincipals(ctx context.Context) []access.Principal {
var out []access.Principal
for _, o := range b.organisations(ctx) {
if len(o.principals) > 0 {
out = append(out, o.principals[0])
}
}
return out
}
// issueSources resolves every organisation's repository bindings to their
// connections, each read through the first agent its permits allow to clone.
// It runs every poll cycle, so a rotated credential applies to the next poll.
func (b *boot) issueSources(ctx context.Context) []daemon.IssueSource {
var out []daemon.IssueSource
for _, o := range b.organisations(ctx) {
log := b.log.With("org", o.owner)
octx := o.act(ctx)
var repos []config.Repo
if _, found, err := b.controlPlane.Query(octx, controlplane.RepositoryPoliciesKind, func(value []byte) error { return json.Unmarshal(value, &repos) }); err != nil || !found {
log.Warn("issue discovery: repositories unavailable", "err", err)
continue
}
connections, err := b.repositoryConnections(octx)
if err != nil {
log.Warn("issue discovery: connections unavailable", "err", err)
continue
}
byConnection := map[string][]config.Repo{}
for _, repo := range repos {
if repo.Connection == "" {
log.Warn("issue discovery: repository has no connection", "repo", repo.FullName())
continue
}
byConnection[repo.Connection] = append(byConnection[repo.Connection], repo)
}
for _, connection := range connections {
bound := byConnection[connection.ID]
if len(bound) == 0 {
continue
}
if source, ok := b.connectionSource(octx, o, connection, bound); ok {
out = append(out, source)
}
}
}
return out
}
func (b *boot) connectionSource(ctx context.Context, o orgAgents, connection config.ForgeConnection, repos []config.Repo) (daemon.IssueSource, bool) {
log := b.log.With("org", o.owner, "connection", connection.ID)
for _, principal := range o.principals {
if !connection.Permitted(string(principal.IdentityID), config.OperationClone) {
continue
}
resolved, err := b.openRepositoryConnection(ctx, principal, repos[0], connection, config.OperationClone)
if err != nil {
log.Warn("issue discovery: connection unavailable", "err", err)
return daemon.IssueSource{}, false
}
user := resolved.Trees.AuthUser
if user == "" && o.owner == org.DefaultOrgID {
user = b.cfgHolder.Get().BotUser
}
return daemon.IssueSource{Identity: principal.IdentityID, Forge: resolved.Forge, BotUser: user, Repos: repos}, true
}
log.Warn("issue discovery: no agent may clone through this connection")
return daemon.IssueSource{}, false
}
+1 -3
View File
@@ -195,9 +195,7 @@ func Run() int { //nolint:cyclop,funlen // the composition root's setup sequence
// The daemon publishes the configuration view, which carries the catalog's
// model limits.
b.startModelCatalogRefresh(ctx)
if err := b.buildTreesAndIdentities(ctx); err != nil {
return 1
}
b.buildWorktreeManager()
if err := b.registerNATSRPC(); err != nil {
return 1
+2 -2
View File
@@ -103,9 +103,9 @@ func reloadLoop(ctx context.Context, ch <-chan os.Signal, c *ReloadController, l
// defaults to requires-restart, which is the safe direction and forces
// whoever adds it to decide deliberately.
var reloadableFields = map[string]bool{
// Tickers reset on change (daemon.go Run / runIdentities).
// Tickers reset on change (daemon.go Run).
"PollInterval": true,
// Poll loops re-read per cycle (daemon.go poll / pollForIdentity).
// The poll loop re-reads it each cycle.
"Repos": true,
// Per-task snapshot: ForTask carries these into every dispatched
// TaskContext (config.go ForTask), built fresh from d.Cfg.Get() at
+3 -3
View File
@@ -168,14 +168,14 @@ func newChatTurnRunner(
func chatRepoEnv(cfg config.Config, identity string) []gateway.RepoEnv {
repos, forge := cfg.Repos, cfg.Forge
if len(cfg.Identities) > 0 {
repos, forge = nil, config.Forge{}
repos = nil
for _, id := range cfg.Identities {
if id.Name == identity {
repos, forge = id.Repos, id.Forge
repos = id.Repos
break
}
if len(repos) == 0 && len(id.Repos) > 0 {
repos, forge = id.Repos, id.Forge
repos = id.Repos
}
}
}
+3 -17
View File
@@ -8,7 +8,6 @@ import (
"strings"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/identity"
)
// ForgeConnectionsKind is the resource holding an organisation's external
@@ -32,12 +31,9 @@ func forgeConnectionsDefinition() Definition {
}
}
// seedForgeConnections migrates the file's forge configuration into
// connections: the root's [forge] becomes the root connection, and each
// identity that declared its own forge becomes that identity's connection.
// The permissions reproduce what the file already granted -- the root
// connection to root tasks, each identity's own forge to that identity -- so
// the migration neither widens nor narrows access. The file never recorded
// seedForgeConnections migrates the file's [forge] into the root connection.
// It grants no execution identity a permit, so the migration neither widens
// nor narrows access. The file never recorded
// which external account a credential belongs to, so Account is seeded empty
// and is filled by a verification.
func seedForgeConnections(cfg config.Config) any {
@@ -48,16 +44,6 @@ func seedForgeConnections(cfg config.Config) any {
// execution identity needs one, and the file granted it none.
out = append(out, c)
}
for _, id := range cfg.Identities {
c, ok := connectionFromForge("identity-"+id.Name, id.Name, id.Forge)
if !ok {
continue
}
// The file gave this identity its own credential and no limit on what
// it did with it; keeping that exact grant is what a migration owes.
c.Permits = map[string][]string{string(identity.StableID(id.Name)): config.Operations()}
out = append(out, c)
}
return out
}
@@ -3,11 +3,9 @@ package controlplane
import (
"encoding/json"
"errors"
"slices"
"testing"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/identity"
)
func TestForgeConnectionValidation(t *testing.T) {
@@ -86,16 +84,11 @@ func TestForgeConnectionValidation(t *testing.T) {
}
// TestConnectionMigrationKeepsTheFilesGrants is the migration's security
// claim: what the file granted, the connections grant, and nothing more. The
// root connection takes no permit entry, so no execution identity reaches it;
// each identity's own forge becomes a connection only that identity may use.
// claim: the root connection takes no permit entry, so no execution identity
// reaches it.
func TestConnectionMigrationKeepsTheFilesGrants(t *testing.T) {
cfg := config.Config{
Forge: config.Forge{Type: "github", Host: "github.com", Token: config.SecretRef{Engine: "env", Key: "ROOT_TOKEN"}},
Identities: []config.IdentityConfig{
{Name: "reviewer", Forge: config.Forge{Type: "github", Host: "github.com", Token: config.SecretRef{Engine: "env", Key: "REVIEWER_TOKEN"}}},
{Name: "silent"},
},
}
seeded, err := forgeConnectionsDefinition().seededValue(cfg)
if err != nil {
@@ -105,10 +98,10 @@ func TestConnectionMigrationKeepsTheFilesGrants(t *testing.T) {
if err := json.Unmarshal(seeded, &connections); err != nil {
t.Fatalf("decode seed: %v", err)
}
if len(connections) != 2 {
t.Fatalf("seeded %d connections, want the root's and the one identity that declared a forge: %+v", len(connections), connections)
if len(connections) != 1 {
t.Fatalf("seeded %d connections, want the root's alone: %+v", len(connections), connections)
}
root, reviewer := connections[0], connections[1]
root := connections[0]
if root.ID != rootConnectionID || len(root.Permits) != 0 {
t.Fatalf("root connection = %+v, want the root's forge and no permitted execution identity", root)
}
@@ -118,15 +111,4 @@ func TestConnectionMigrationKeepsTheFilesGrants(t *testing.T) {
if root.Token.Key != "ROOT_TOKEN" {
t.Fatalf("root connection credential = %+v, want the file's reference", root.Token)
}
if reviewer.ID != "identity-reviewer" {
t.Fatalf("second connection = %q, want the declaring identity's", reviewer.ID)
}
if permitted := reviewer.Permits[string(identity.StableID("reviewer"))]; !slices.Equal(permitted, config.Operations()) {
t.Fatalf("reviewer permits = %v, want every operation it already had (%v)", permitted, config.Operations())
}
for id := range reviewer.Permits {
if id != string(identity.StableID("reviewer")) {
t.Fatalf("reviewer connection permits %q as well; an identity with no forge of its own must not gain one", id)
}
}
}
+5 -6
View File
@@ -426,8 +426,8 @@ type Config struct {
// Tools holds MCP server and tool policy configuration (from config.tools.yaml).
Tools ToolsConfig `toml:"tools" yaml:"tools"`
// Identities declares multiple identities, each with its own forge, repos,
// models and poll loop. Empty uses the single-identity fields.
// Identities declares multiple identities, each with its own repos.
// Empty uses the single-identity fields.
Identities []IdentityConfig `toml:"identities" yaml:"identities"`
Repos []Repo `toml:"repos" yaml:"repos"`
ModelLimits map[string]ModelLimits `toml:"-" yaml:"-" json:"-"`
@@ -436,9 +436,9 @@ type Config struct {
Curators []CuratorDefinition `toml:"curators" yaml:"curators" json:"curators,omitempty"`
}
// IdentityConfig is a per-identity configuration subset. Each identity
// gets its own forge account and repository membership. Runtime settings
// come from the shared control plane.
// IdentityConfig is a per-identity configuration subset. Forge access comes
// from the identity's organisation connections; runtime settings come from
// the shared control plane.
type IdentityConfig struct {
// Name identifies this identity in logs, events, and NATS subject
// namespaces (archie.<name>.task.<type>). Required when Identities
@@ -457,7 +457,6 @@ type IdentityConfig struct {
// -- an explicit empty list turns grants off, needed
// because a plain nil already means "not set" here.
GrantedCredentials *[]string `toml:"granted_credentials" yaml:"granted_credentials"`
Forge Forge `toml:"forge" yaml:"forge"`
Repos []Repo `toml:"repos" yaml:"repos"`
}
+80 -269
View File
@@ -183,10 +183,12 @@ type Daemon struct {
// Storage is the pluggable storage backend for container mounts.
// A runnable task requires it; acquireTaskContainer parks on nil.
Storage storage.Backend
// Identities holds per-identity runner state for multi-identity mode.
// When non-empty, Run() starts one goroutine per identity instead of
// using the single-identity Forge/Trees/Cfg.Repos path.
Identities []*IdentityRunner
// IssueSources resolves, each poll cycle, every organisation connection
// to poll and the identity whose permit reaches it. Nil polls nothing.
IssueSources func(context.Context) []IssueSource
// OrgPrincipals is one acting principal per organisation, system org
// first. Nil dispatches the root identity's org alone.
OrgPrincipals func(context.Context) []access.Principal
// IdentityRepository is the durable lifecycle authority. Polling checks it
// every cycle so suspend/reactivate commands do not require a restart.
IdentityRepository identity.Repository
@@ -237,37 +239,29 @@ type Daemon struct {
lastPollAt sync.Map
}
// IdentityRunner bundles identity-specific state for a single agent
// identity running within a multi-identity daemon. Each identity gets its
// own forge client, worktree manager, repo list, and config -- but shares
// the store, NATS connection, container pool, and event bus with siblings.
type IdentityRunner struct {
ID identity.IdentityID
Name string
Forge forge.Forge
Trees *worktree.Manager
Repos []config.Repo
// Cfg holds the identity's forge account and repository membership.
Cfg config.IdentityConfig
Log *slog.Logger
// IssueSource is one forge connection's repositories as the identity
// permitted to read them sees them.
type IssueSource struct {
Identity identity.IdentityID
Forge forge.Forge
// BotUser is the connection's external account, the assignee polled for.
BotUser string
Repos []config.Repo
}
// NewIdentityRunner constructs an IdentityRunner from an identity config
// and a pre-built forge client. The caller owns forge and trees lifecycle;
// IdentityRunner just holds references.
func NewIdentityRunner(ctx context.Context, idCfg config.IdentityConfig, fg forge.Forge, trees *worktree.Manager, log *slog.Logger) (*IdentityRunner, error) {
if idCfg.Name == "" {
return nil, fmt.Errorf("identity name is required")
func (d *Daemon) issueSources(ctx context.Context) []IssueSource {
if d.IssueSources == nil {
return nil
}
return &IdentityRunner{
ID: identity.StableID(idCfg.Name),
Name: idCfg.Name,
Forge: fg,
Trees: trees,
Repos: idCfg.Repos,
Cfg: idCfg,
Log: log.With("identity", idCfg.Name),
}, nil
return d.IssueSources(ctx)
}
// sourceTag is the identity a source's tasks carry: empty for the root.
func (d *Daemon) sourceTag(source IssueSource) string {
if source.Identity == d.RootIdentityID {
return ""
}
return string(source.Identity)
}
// emit publishes an observability event; safe on a nil bus.
@@ -297,21 +291,18 @@ func (d *Daemon) Startup(ctx context.Context) error {
// an unreachable host would otherwise hold boot open indefinitely.
const sweepTimeout = 2 * time.Minute
// sweepAccess accepts pending invitations and verifies push access on the
// root forge and every identity's forge, each in its own goroutine.
// sweepAccess accepts pending invitations and verifies push access through
// every organisation connection, each in its own goroutine.
func (d *Daemon) sweepAccess(ctx context.Context) {
sweep := func(identity string, fg forge.Forge, repos []config.Repo) {
log := d.Log
if identity != "" {
log = log.With("identity", identity)
}
sweep := func(source IssueSource) {
log := d.Log.With("identity", source.Identity)
ctx, cancel := context.WithTimeout(ctx, sweepTimeout)
defer cancel()
if err := fg.AcceptInvitations(ctx); err != nil {
if err := source.Forge.AcceptInvitations(ctx); err != nil {
log.Warn("invitation sweep failed", "err", err)
}
for _, r := range repos {
if err := fg.VerifyPush(ctx, r.Owner, r.Name); err != nil {
for _, r := range source.Repos {
if err := source.Forge.VerifyPush(ctx, r.Owner, r.Name); err != nil {
// A sweep cut short by its own context is not a repo failure: name
// the budget when it expired, stay quiet on a shutdown.
switch {
@@ -328,28 +319,14 @@ func (d *Daemon) sweepAccess(ctx context.Context) {
}
var wg sync.WaitGroup
wg.Add(1 + len(d.Identities))
go func() {
defer wg.Done()
sweep("", d.Forge, d.Cfg.Get().Repos)
}()
for _, id := range d.Identities {
go func() {
defer wg.Done()
sweep(id.Name, id.Forge, id.Repos)
}()
for _, source := range d.issueSources(ctx) {
wg.Go(func() { sweep(source) })
}
wg.Wait()
}
// Run polls until the context ends. When Identities is non-empty, each
// identity gets its own goroutine with its own forge client, repo list,
// and poll interval -- failure isolation between identities (one identity's
// forge outage doesn't block another's poll cycle).
// Run polls until the context ends.
func (d *Daemon) Run(ctx context.Context) error {
if len(d.Identities) > 0 {
return d.runIdentities(ctx)
}
// A pass no longer waits for the tasks it submitted, so the run does, once
// the ctx is cancelled.
defer d.WaitForTasks()
@@ -373,112 +350,8 @@ func (d *Daemon) Run(ctx context.Context) error {
}
}
// runIdentities polls each identity in its own goroutine and runs
// maintenance and draining once in a shared loop, keeping the global
// concurrency cap and per-repo serialization.
func (d *Daemon) runIdentities(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
d.watchCallWaits(ctx)
var wg sync.WaitGroup
// One poll loop per identity. Polling is the only identity-scoped
// work; it never executes tasks.
for _, id := range d.Identities {
wg.Add(1)
go func(id *IdentityRunner) {
defer wg.Done()
interval := d.Cfg.Get().PollInterval.Std()
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
d.pollForIdentity(ctx, id)
select {
case <-ctx.Done():
return
case <-ticker.C:
// Pick up the current shared polling interval.
iv := d.Cfg.Get().PollInterval.Std()
if iv != interval {
interval = iv
ticker.Reset(iv)
}
}
}
}(id)
}
// One shared maintenance-and-drain loop.
wg.Go(func() {
interval := d.Cfg.Get().PollInterval.Std()
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
d.maintainAndDrain(ctx)
select {
case <-ctx.Done():
return
case <-ticker.C:
// PollInterval may have been reloaded; reset the ticker so a
// change takes effect on the next cycle.
if iv := d.Cfg.Get().PollInterval.Std(); iv != interval {
interval = iv
ticker.Reset(iv)
}
}
}
})
wg.Wait()
d.WaitForTasks()
return ctx.Err()
}
// pollForIdentity polls one identity's repos via its own forge client,
// enqueuing any discovered issues under that identity's name. It never
// drains or reconciles -- those are store-wide and run in the shared
// maintainAndDrain loop.
func (d *Daemon) pollForIdentity(ctx context.Context, id *IdentityRunner) {
d.markPoll(string(id.ID))
if !d.identityActive(ctx, id.ID) {
return
}
cfg := configForIdentity(d.Cfg.Get(), id.Cfg)
for _, repo := range identityRepositories(cfg.Repos, id.Repos) {
issues, complete := d.pollIssuesWithConfig(ctx, id.Forge, cfg, repo)
if complete {
d.withdrawUnpolled(ctx, repo, string(id.ID), dispatchRule(cfg), issues)
}
for _, is := range issues {
labels := strings.Join(is.Labels, ",")
if d.Tasks != nil {
d.pollNATS(ctx, id.Forge, cfg, repo, is, labels, string(id.ID))
}
}
}
}
// maintainAndDrain runs the store-wide passes of a multi-identity cycle:
// storage cleanup, PR reconciliation, then task dispatch. A single shared
// loop runs this so the global concurrency cap and per-repo serialization
// hold across identities.
func (d *Daemon) maintainAndDrain(ctx context.Context) {
d.cleanupExpiredStorage(ctx)
d.reconcilePRs(ctx)
d.scanPRReviews(ctx)
d.dispatchBindings(ctx)
d.drainReactions(ctx)
if d.Tasks != nil {
d.drainNATS(ctx)
}
}
// pollIssuesWithConfig returns the repo's issues the dispatch rule makes
// work, using the given forge client and dispatch config: single-identity
// mode passes d.Forge/d.Cfg, identity poll loops their own forge and
// configForIdentity, so each identity uses its own bot user with the shared
// dispatch settings. complete is false when any part of the poll failed or was skipped, so
// work, using the given forge client and dispatch config. complete is false when any part of the poll failed or was skipped, so
// the result must not be read as the whole eligible set.
func (d *Daemon) pollIssuesWithConfig(ctx context.Context, fg forge.Forge, cfg config.Config, repo config.Repo) (issues []forge.Issue, complete bool) {
// Webhook intake hears about issues as they change; polling as well would
@@ -502,6 +375,10 @@ func (d *Daemon) pollIssuesWithConfig(ctx context.Context, fg forge.Forge, cfg c
return issues, true
}
assigned := func() ([]forge.Issue, bool) {
if cfg.BotUser == "" {
d.Log.Error("assignee trigger needs the connection's account; refusing to poll", "repo", repo.FullName())
return nil, false
}
issues, err := fg.AssignedIssues(ctx, repo.Owner, repo.Name, cfg.BotUser)
if err != nil {
d.Log.Error("assigned poll failed", "repo", repo.FullName(), "err", err)
@@ -579,19 +456,19 @@ func (d *Daemon) dispatchBindings(ctx context.Context) {
d.dispatchOrgBindings(ctx)
return
}
identities := []identity.IdentityID{d.RootIdentityID}
for _, runner := range d.Identities {
if runner != nil {
identities = append(identities, runner.ID)
var principals []access.Principal
if d.OrgPrincipals != nil {
principals = d.OrgPrincipals(ctx)
} else {
principal, err := d.Principals.PrincipalFor(ctx, d.RootIdentityID)
if err != nil {
d.Log.Warn("binding dispatch: principal unavailable", "identity", d.RootIdentityID, "err", err)
return
}
principals = []access.Principal{principal}
}
seen := make(map[org.OrgID]bool)
for _, id := range identities {
principal, err := d.Principals.PrincipalFor(ctx, id)
if err != nil {
d.Log.Warn("binding dispatch: principal unavailable", "identity", id, "err", err)
continue
}
for _, principal := range principals {
if seen[principal.Org] {
continue
}
@@ -907,12 +784,9 @@ func (d *Daemon) drainReactions(ctx context.Context) {
}
// botUserForTask resolves the bot login that owns task, so the reaction
// consumer can exclude Archie's own comments. Identity-derived for
// multi-identity deployments, the global forge bot user otherwise.
// consumer can exclude Archie's own comments: the global forge bot user for
// root tasks.
func (d *Daemon) botUserForTask(task *workflowtask.Task) string {
if runner := d.identityFor(task); runner != nil {
return runner.Cfg.BotUser
}
if !d.rootTask(task) {
return ""
}
@@ -1244,20 +1118,27 @@ func (d *taskDispatcher) Wait() {
}
}
// poll discovers work through every organisation connection, resolved
// afresh each cycle so a rotated credential or a new connection applies to
// the next poll.
func (d *Daemon) poll(ctx context.Context) {
d.markPoll("")
if !d.identityActive(ctx, d.RootIdentityID) {
return
}
for _, repo := range d.Cfg.Get().Repos {
issues, complete := d.pollIssues(ctx, repo)
if complete {
d.withdrawUnpolled(ctx, repo, "", dispatchRule(d.Cfg.Get()), issues)
for _, source := range d.issueSources(ctx) {
if !d.identityActive(ctx, source.Identity) {
continue
}
for _, is := range issues {
labels := strings.Join(is.Labels, ",")
if d.Tasks != nil {
d.pollNATS(ctx, d.Forge, d.Cfg.Get(), repo, is, labels, "")
cfg := d.Cfg.Get()
cfg.BotUser = source.BotUser
tag := d.sourceTag(source)
for _, repo := range source.Repos {
issues, complete := d.pollIssuesWithConfig(ctx, source.Forge, cfg, repo)
if complete {
d.withdrawUnpolled(ctx, repo, tag, dispatchRule(cfg), issues)
}
for _, is := range issues {
if d.Tasks != nil {
d.pollNATS(ctx, source.Forge, cfg, repo, is, strings.Join(is.Labels, ","), tag)
}
}
}
}
@@ -1397,20 +1278,11 @@ func (d *Daemon) PublishReaction(ctx context.Context, reaction workintake.Review
}
// publisherIdentity is the identity a producer publishes as: the envelope's
// identity when it names one (an ID, or a name for a hand-built envelope),
// otherwise the root identity of a single-identity install.
// identity when it names one, otherwise the root identity.
func (d *Daemon) publisherIdentity(ref string) identity.IdentityID {
if ref == "" || identity.IdentityID(ref) == d.RootIdentityID || identity.StableID(ref) == d.RootIdentityID {
return d.RootIdentityID
}
for _, runner := range d.Identities {
if runner == nil {
continue
}
if runner.Name == ref || string(runner.ID) == ref {
return runner.ID
}
}
return identity.IdentityID(ref)
}
@@ -1517,13 +1389,6 @@ func (d *Daemon) processNATSTask(ctx context.Context, msg eventbus.Message, hold
}
}
// pollIssues discovers work for one repo according to the root dispatch
// config, using the root forge client. Identity poll loops use
// pollIssuesWithConfig with their own forge and config instead.
func (d *Daemon) pollIssues(ctx context.Context, repo config.Repo) ([]forge.Issue, bool) {
return d.pollIssuesWithConfig(ctx, d.Forge, d.Cfg.Get(), repo)
}
// closeResolvedIssue closes a finished task's forge issue, skipping chat
// tasks. Failures are logged.
func (d *Daemon) closeResolvedIssue(ctx context.Context, fg forge.Forge, task *workflow.Task, comment string) {
@@ -2331,9 +2196,6 @@ func (d *Daemon) runCredential(task *workflow.Task) (string, func(), error) {
}
func (d *Daemon) configFor(task *workflow.Task) config.Config {
if id := d.identityFor(task); id != nil {
return configForIdentity(d.Cfg.Get(), id.Cfg)
}
cfg := d.Cfg.Get()
if !d.rootTask(task) {
cfg.BotUser, cfg.BotEmail, cfg.Forge = "", "", config.Forge{}
@@ -2380,29 +2242,6 @@ func (d *Daemon) captureAttemptConfig(ctx context.Context, task *workflow.Task,
d.emit(event)
}
func configForIdentity(root config.Config, identity config.IdentityConfig) config.Config {
root.BotUser, root.BotEmail = identity.BotUser, identity.BotEmail
root.Org, root.GrantedCredentials = root.CredentialAccess(identity.Name)
root.Forge = identity.Forge
return root
}
// identityFor resolves the IdentityRunner that owns task, or nil for
// single-identity deployments and forge-sourced tasks recorded
// before multi-identity routing existed (task.Identity == ""). Callers
// resolve database identities separately; only the root may use root resources.
func (d *Daemon) identityFor(task *workflow.Task) *IdentityRunner {
if task == nil || task.Identity == "" {
return nil
}
for _, id := range d.Identities {
if id.Name == task.Identity || string(id.ID) == task.Identity {
return id
}
}
return nil
}
// identityMayAct reports whether a task's identity may act, and the park
// class if not. Empty is the root identity. An unknown or inactive identity
// never falls back to the root forge.
@@ -2421,10 +2260,7 @@ func (d *Daemon) identityMayAct(ctx context.Context, name string) (bool, string,
if name == "" || name == string(d.RootIdentityID) {
return true, "", ""
}
if d.identityFor(&workflow.Task{Identity: name}) == nil {
return false, "identity " + name + " no longer resolves", taskstate.ParkTransient
}
return true, "", ""
return false, "identity " + name + " no longer resolves", taskstate.ParkTransient
}
// parkIfIdentityUnresolvable parks task when its non-empty identity cannot be
@@ -2455,10 +2291,10 @@ func (d *Daemon) identityActive(ctx context.Context, id identity.IdentityID) boo
return value.Lifecycle == identity.LifecycleActive
}
// forgeFor returns the forge client that owns task: the identity's own
// client when task.Identity names a configured identity, else the root
// d.Forge for the root alone. This is the safety boundary that keeps one identity's forge
// token from being used against another identity's repos.
// forgeFor returns the forge client that owns task: its organisation
// connection, else the root d.Forge for the root alone. This is the safety
// boundary that keeps one identity's forge token from being used against
// another identity's repos.
func (d *Daemon) forgeFor(ctx context.Context, task *workflow.Task) forge.Forge {
if d.ResolveRepository != nil && task.HasRepository() {
resolved, err := d.Repository(ctx, task, config.OperationClone)
@@ -2467,9 +2303,6 @@ func (d *Daemon) forgeFor(ctx context.Context, task *workflow.Task) forge.Forge
}
return resolved.Forge
}
if id := d.identityFor(task); id != nil {
return id.Forge
}
if !d.rootTask(task) {
return forge.NewNoop(d.Log)
}
@@ -2482,9 +2315,6 @@ func (d *Daemon) treesFor(task *workflow.Task) *worktree.Manager {
// Cleanup needs only the org's path, not a still-valid external credential.
return &worktree.Manager{WorkDir: worktree.OrgWorkDir(d.Cfg.Get().WorkDir, task.OrgOrDefault())}
}
if id := d.identityFor(task); id != nil {
return id.Trees
}
if !d.rootTask(task) {
return &worktree.Manager{WorkDir: filepath.Join(d.Cfg.Get().WorkDir, "identity-"+string(identity.StableID(task.Identity)))}
}
@@ -2495,16 +2325,12 @@ func (d *Daemon) rootTask(task *workflow.Task) bool {
return task.Identity == "" || identity.IdentityID(task.Identity) == d.RootIdentityID
}
// repoFor resolves task's repo config from the owning identity's repo
// list when task.Identity is set, else the root Cfg.Repos.
// repoFor resolves a root task's repo config from Cfg.Repos.
func (d *Daemon) repoFor(t *workflow.Task) (config.Repo, bool) {
repos := d.Cfg.Get().Repos
if id := d.identityFor(t); id != nil {
repos = identityRepositories(repos, id.Repos)
} else if !d.rootTask(t) {
if !d.rootTask(t) {
return config.Repo{}, false
}
for _, r := range repos {
for _, r := range d.Cfg.Get().Repos {
if r.Owner == t.Owner && r.Name == t.Repo {
return r, true
}
@@ -2621,18 +2447,3 @@ func (d *Daemon) InFlight() int {
defer dispatcher.mu.Unlock()
return dispatcher.pending
}
// identityRepositories keeps account membership while taking live settings
// for repositories present in the shared control-plane configuration.
func identityRepositories(shared, owned []config.Repo) []config.Repo {
repos := append([]config.Repo(nil), owned...)
for i, repo := range repos {
for _, current := range shared {
if current.Owner == repo.Owner && current.Name == repo.Name {
repos[i] = current
break
}
}
}
return repos
}
+2 -4
View File
@@ -65,15 +65,13 @@ func (d *Daemon) adoptWorkflowIdentity(ctx context.Context, task *workflow.Task)
func (d *Daemon) declaredIdentity(ctx context.Context, task *workflow.Task, ref string) (identity.IdentityID, string, error) {
var id identity.IdentityID
var name string
if runner := d.identityFor(&workflow.Task{Identity: ref}); runner != nil {
id, name = runner.ID, runner.Name
} else if ref == workflow.RootRef || identity.IdentityID(ref) == d.RootIdentityID || identity.StableID(ref) == d.RootIdentityID {
if ref == workflow.RootRef || identity.IdentityID(ref) == d.RootIdentityID || identity.StableID(ref) == d.RootIdentityID {
id = d.RootIdentityID
} else {
id, name = identity.IdentityID(ref), ref
}
if d.IdentityRepository == nil {
if id == d.RootIdentityID || d.identityFor(&workflow.Task{Identity: ref}) != nil {
if id == d.RootIdentityID {
return id, name, nil
}
return "", "", pinFailure{taskstate.ParkTransient, fmt.Errorf("identity authority unavailable for %s", ref)}
+33 -33
View File
@@ -32,7 +32,31 @@ func (s *identityStore) ParkTask(context.Context, int64, string, string, tasksta
type principalOf struct{}
func (principalOf) PrincipalFor(_ context.Context, id identity.IdentityID) (access.Principal, error) {
return access.Principal{IdentityID: id}, nil
return access.Principal{IdentityID: id, Org: org.DefaultOrgID}, nil
}
// agentDirectory knows each agent by name and by ID, all assigned to the
// system org.
type agentDirectory struct{ identity.Repository }
var directoryAgents = map[identity.IdentityID]identity.IdentityID{
"root": "root", "deployer": "id-deployer", "id-deployer": "id-deployer", "bot": "id-bot", "id-bot": "id-bot",
}
func (agentDirectory) Get(_ context.Context, ref identity.IdentityID) (identity.Identity, error) {
id, ok := directoryAgents[ref]
if !ok {
return identity.Identity{}, identity.ErrNotFound
}
return identity.Identity{ID: id, Kind: identity.KindBot, Lifecycle: identity.LifecycleActive}, nil
}
func (agentDirectory) ListOrgAgents(_ context.Context, owner org.OrgID) ([]org.AgentAssignment, error) {
var out []org.AgentAssignment
for _, id := range directoryAgents {
out = append(out, org.AgentAssignment{IdentityID: id, OrgID: owner})
}
return out, nil
}
// runAllowed permits run only to the identities it lists.
@@ -79,15 +103,15 @@ func TestWorkflowIdentity(t *testing.T) {
wantDenied []identity.IdentityID
disabled bool
}{
{"allowed dispatch runs as the workflow's identity", deploy, "", runAllowed{"root": true, "id-deployer": true}, "deployer", false, nil, false},
{"allowed dispatch runs as the workflow's identity", deploy, "", runAllowed{"root": true, "id-deployer": true}, "id-deployer", false, nil, false},
{"refused invocation is parked", deploy, "", runAllowed{"id-deployer": true}, "", true, []identity.IdentityID{"root"}, false},
{"refused execution is parked", deploy, "", runAllowed{"root": true}, "", true, []identity.IdentityID{"id-deployer"}, false},
{"called workflow runs as its own identity, not its caller's", callee, "deployer", runAllowed{"id-deployer": true, "id-bot": true}, "bot", false, nil, false},
{"caller refused the called workflow is parked", callee, "deployer", runAllowed{"id-bot": true}, "", true, []identity.IdentityID{"id-deployer"}, false},
{"workflow naming the root runs as the root", "id: deploy\nidentity: root\n", "bot", runAllowed{"root": true, "id-bot": true}, "", false, nil, false},
{"no declared identity is refused", "id: deploy\n", "bot", runAllowed{"root": true, "id-bot": true}, "", true, nil, false},
{"called workflow runs as its own identity, not its caller's", callee, "id-deployer", runAllowed{"id-deployer": true, "id-bot": true}, "id-bot", false, nil, false},
{"caller refused the called workflow is parked", callee, "id-deployer", runAllowed{"id-bot": true}, "", true, []identity.IdentityID{"id-deployer"}, false},
{"workflow naming the root runs as the root", "id: deploy\nidentity: root\n", "id-bot", runAllowed{"root": true, "id-bot": true}, "", false, nil, false},
{"no declared identity is refused", "id: deploy\n", "id-bot", runAllowed{"root": true, "id-bot": true}, "", true, nil, false},
{"unconfigured identity is parked", "id: deploy\nidentity: ghost\n", "", runAllowed{"root": true}, "", true, nil, false},
{"disabled workflow is parked", deploy, "bot", runAllowed{"id-bot": true, "id-deployer": true}, "", true, nil, true},
{"disabled workflow is parked", deploy, "id-bot", runAllowed{"id-bot": true, "id-deployer": true}, "", true, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@@ -96,7 +120,7 @@ func TestWorkflowIdentity(t *testing.T) {
d := &Daemon{
Store: store, Log: slog.New(slog.DiscardHandler), RootIdentityID: "root",
Access: tt.allowed, Principals: principalOf{}, Denials: &denied,
Identities: []*IdentityRunner{{ID: "id-deployer", Name: "deployer"}, {ID: "id-bot", Name: "bot"}},
IdentityRepository: agentDirectory{}, AgentAssignments: agentDirectory{},
}
if tt.disabled {
d.WorkflowEnablement = enablementOf(workflowtask.WorkflowEnablement{}.SetEnabled("", "deploy", false))
@@ -116,30 +140,6 @@ func TestWorkflowIdentity(t *testing.T) {
}
}
func TestIdentityTasksUseLiveSettings(t *testing.T) {
root := config.Config{Models: map[string]string{"builder": "old/model"}, Providers: map[string]config.Provider{"old": {}}, Repos: []config.Repo{{Owner: "acme", Name: "app", Base: "main"}}}
id := config.IdentityConfig{Name: "bot", BotUser: "bot", BotEmail: "bot@example.test", Forge: config.Forge{Type: "github"}, Repos: []config.Repo{{Owner: "acme", Name: "app", Base: "stale"}}}
d := &Daemon{Cfg: config.NewHolder(root), Identities: []*IdentityRunner{{Name: "bot", Cfg: id, Repos: id.Repos}}}
task := &workflow.Task{Identity: "bot", Owner: "acme", Repo: "app"}
for _, model := range []string{"old/model", "new/model"} {
root.Models = map[string]string{"builder": model}
root.Budgets.MaxSteps++
root.Dispatch.Trigger = "label"
d.Cfg.Set(root)
got := d.configFor(task)
if !reflect.DeepEqual(got.Models, root.Models) || !reflect.DeepEqual(got.Providers, root.Providers) || got.Budgets != root.Budgets || got.Dispatch != root.Dispatch {
t.Fatalf("identity settings replaced the current runtime settings: %+v", got)
}
repo, ok := d.repoFor(task)
if !ok || repo.Base != "main" {
t.Fatalf("repository = %+v, found %v; want current base main", repo, ok)
}
if got.BotUser != id.BotUser || got.Forge != id.Forge {
t.Fatal("identity lost its forge account")
}
}
}
// signedOnly permits run only for a verified signature, as a policy
// conditioned on context.signature does.
type signedOnly struct{ runAllowed }
@@ -165,7 +165,7 @@ func TestWorkflowRunCarriesEventContext(t *testing.T) {
d := &Daemon{
Store: &identityStore{}, Log: slog.New(slog.DiscardHandler), RootIdentityID: "root",
Access: signedOnly{runAllowed{"root": true, "id-deployer": true}},
Principals: principalOf{}, Identities: []*IdentityRunner{{ID: "id-deployer", Name: "deployer"}},
Principals: principalOf{}, IdentityRepository: agentDirectory{}, AgentAssignments: agentDirectory{},
}
task := &workflow.Task{
ID: 1, Workflow: "deploy", BindingSignature: tt.signature,
+3 -1
View File
@@ -78,7 +78,9 @@ func TestDispatchOrgWorkflows(t *testing.T) {
{ID: "root", MappingID: "root", Matcher: binding.Matcher{Source: "root"}, OrgID: org.DefaultOrgID, Workflow: "deploy", Status: binding.StatusArmed, Owner: "owner", Repo: "repo"},
{ID: "acme", MappingID: "acme", Matcher: binding.Matcher{Source: "acme"}, OrgID: "acme", Workflow: "deploy", Status: binding.StatusArmed},
}}
d := &Daemon{Cfg: config.NewHolder(config.Config{}), Store: store, Log: slog.New(slog.DiscardHandler), Bindings: store, BindingDispatcher: store, BindingTaskCreator: store, Mappings: store, RootIdentityID: "root", Identities: []*IdentityRunner{{ID: "acme-bot", Name: "bot"}}, Principals: workflowPrincipals{"root": org.DefaultOrgID, "acme-bot": "acme"}, WorkflowDefinitions: orgDefinitions{
d := &Daemon{Cfg: config.NewHolder(config.Config{}), Store: store, Log: slog.New(slog.DiscardHandler), Bindings: store, BindingDispatcher: store, BindingTaskCreator: store, Mappings: store, RootIdentityID: "root", OrgPrincipals: func(context.Context) []access.Principal {
return []access.Principal{{IdentityID: "root", Org: org.DefaultOrgID}, {IdentityID: "acme-bot", Org: "acme"}}
}, Principals: workflowPrincipals{"root": org.DefaultOrgID, "acme-bot": "acme"}, WorkflowDefinitions: orgDefinitions{
org.DefaultOrgID: "id: deploy\nrepository: required\nsteps:\n - type: workflow.finish\n",
"acme": "id: deploy\nrepository: none\nsteps:\n - type: workflow.finish\n",
}}
@@ -270,7 +270,7 @@ func applyDispatchDefaults(cfg *config.Config) {
func applyIdentityDefaults(cfg *config.Config) {
if len(cfg.Identities) > 0 {
for i := range cfg.Identities {
cfg.Identities[i].BotEmail = defaultBotEmail(cfg.Identities[i].Forge.Type, cfg.Identities[i].BotUser, cfg.Identities[i].BotEmail)
cfg.Identities[i].BotEmail = defaultBotEmail(cfg.Forge.Type, cfg.Identities[i].BotUser, cfg.Identities[i].BotEmail)
}
if len(cfg.Identities) == 1 && cfg.BotUser == "" {
cfg.BotUser = cfg.Identities[0].BotUser
@@ -187,45 +187,9 @@ func validateForgeIntake(cfg *config.Config) error {
return fmt.Errorf("%w: forge.webhook_addr is required when forge.intake is %q", ErrInvalidInput, intake)
}
}
return validateIdentityForgeIntake(cfg.Identities)
}
// identityIntakes are the intake modes an identity may declare.
var identityIntakes = []string{config.ForgeIntakePoll}
// validateIdentityForgeIntake rejects intake settings on an identity; only
// the root forge's are read.
func validateIdentityForgeIntake(identities []config.IdentityConfig) error {
for i, id := range identities {
switch id.Forge.Intake {
case "", config.ForgeIntakePoll:
case config.ForgeIntakeWebhook, config.ForgeIntakeBoth:
return fmt.Errorf("%w: %s %q is not supported: per-identity webhook intake is not implemented, so this identity would silently keep polling (want %q)",
ErrInvalidInput, identitySettingPath(i, id, "forge.intake"), id.Forge.Intake, config.ForgeIntakePoll)
default:
return fmt.Errorf("%w: %s %q (want %s)", ErrInvalidInput, identitySettingPath(i, id, "forge.intake"), id.Forge.Intake, list(identityIntakes))
}
if id.Forge.WebhookSecret != (config.SecretRef{}) {
return fmt.Errorf("%w: %s is not supported: per-identity webhook intake is not implemented, so only the top-level forge.webhook_secret is read",
ErrInvalidInput, identitySettingPath(i, id, "forge.webhook_secret"))
}
if id.Forge.WebhookAddr != "" {
return fmt.Errorf("%w: %s is not supported: per-identity webhook intake is not implemented, so only the top-level forge.webhook_addr is read",
ErrInvalidInput, identitySettingPath(i, id, "forge.webhook_addr"))
}
}
return nil
}
// identitySettingPath renders a per-identity setting's location, with its
// name when set.
func identitySettingPath(i int, id config.IdentityConfig, field string) string {
if id.Name == "" {
return fmt.Sprintf("identities[%d].%s", i, field)
}
return fmt.Sprintf("identities[%d] (%q).%s", i, id.Name, field)
}
// validateProviders rejects base URLs carrying credentials or query state.
// A provider URL is used to build API requests, so userinfo or a stray query
// string would be silently attached to every call.
@@ -268,12 +232,6 @@ func validateIdentities(identities []config.IdentityConfig) error {
if len(id.Repos) == 0 {
return fmt.Errorf("%w: identities[%d] has no [[identities.repos]] entries -- at least one is required", ErrInvalidInput, i)
}
if !forgeTypePattern.MatchString(id.Forge.Type) {
return fmt.Errorf("%w: identities[%d].forge.type %q (want none or the name of an installed forge package)", ErrInvalidInput, i, id.Forge.Type)
}
if !ForgeDisabled(id.Forge.Type) && id.Forge.Token == (config.SecretRef{}) {
return fmt.Errorf("%w: identities[%d].forge.token is required (each identity needs its own secret reference; unlike the top-level [forge], there is no default)", ErrInvalidInput, i)
}
}
return nil
}
+5 -13
View File
@@ -138,12 +138,10 @@ type ForgeRepoView struct {
Name string `json:"name"`
}
// ForgeIdentityView is an identity's forge coordinates and repositories.
// ForgeIdentityView is an identity's repositories.
type ForgeIdentityView struct {
Name string `json:"name"`
ForgeType string `json:"forge_type"`
ForgeHost string `json:"forge_host"`
Repos []ForgeRepoView `json:"repos,omitempty"`
Name string `json:"name"`
Repos []ForgeRepoView `json:"repos,omitempty"`
}
// ReviewView mirrors config.Review, neither field of which is secret. It
@@ -319,8 +317,7 @@ func reposView(repos []config.Repo) []ForgeRepoView {
return out
}
// identityForgesView returns each identity's forge coordinates and
// repository names.
// identityForgesView returns each identity's repository names.
func identityForgesView(identities []config.IdentityConfig) []ForgeIdentityView {
out := make([]ForgeIdentityView, 0, len(identities))
for _, id := range identities {
@@ -328,12 +325,7 @@ func identityForgesView(identities []config.IdentityConfig) []ForgeIdentityView
for _, r := range id.Repos {
repos = append(repos, ForgeRepoView{Owner: r.Owner, Name: r.Name})
}
out = append(out, ForgeIdentityView{
Name: id.Name,
ForgeType: id.Forge.Type,
ForgeHost: id.Forge.Host,
Repos: repos,
})
out = append(out, ForgeIdentityView{Name: id.Name, Repos: repos})
}
return out
}
+6 -59
View File
@@ -137,70 +137,17 @@ type forgeCoordinates struct {
forgeType string
}
// resolveForge returns a function mapping a task to its forge: by identity
// name, then repository owner, else the default forge.
// resolveForge returns a function mapping a task to the instance forge. A
// multi-identity install withholds links: its tasks reach forges through
// organisation connections this projection does not carry.
func (s *Server) resolveForge(ctx context.Context) func(task.Task) forgeCoordinates {
unlinked := func(task.Task) forgeCoordinates { return forgeCoordinates{} }
view, ok, err := s.configSource()(ctx)
if err != nil || !ok {
if err != nil || !ok || view.MultiIdentity {
return unlinked
}
if len(view.Identities) == 0 {
// No per-identity forges published: links are withheld.
if view.MultiIdentity {
return unlinked
}
published := forgeCoordinates{host: view.Identity.ForgeHost, forgeType: view.Identity.ForgeType}
return func(task.Task) forgeCoordinates { return published }
}
return forgeLocator(view)
}
// ownedRepo identifies a repository within an identity's repo list.
type ownedRepo struct{ owner, name string }
// forgeLocator indexes a published projection's per-identity forges once per
// request and resolves each task against them: exact identity name, then repo
// ownership, with an ambiguous repo falling back to the default forge.
func forgeLocator(view ConfigView) func(task.Task) forgeCoordinates {
fallback := forgeCoordinates{host: view.Identity.ForgeHost, forgeType: view.Identity.ForgeType}
byName := make(map[string]forgeCoordinates, len(view.Identities))
byRepo := make(map[ownedRepo]forgeCoordinates)
// ambiguous records a repo two identities both claim, which ownership
// cannot decide -- it is answered by the default forge instead.
ambiguous := make(map[ownedRepo]bool)
for _, identity := range view.Identities {
coords := forgeCoordinates{host: identity.ForgeHost, forgeType: identity.ForgeType}
// First definition of a name wins, as the recovered resolver's
// forward scan did (and as daemon.identityFor still does), so the two
// agree even if a config ever names one identity twice.
if _, seen := byName[identity.Name]; !seen {
byName[identity.Name] = coords
}
for _, repo := range identity.Repos {
key := ownedRepo{owner: repo.Owner, name: repo.Name}
if _, seen := byRepo[key]; seen {
ambiguous[key] = true
continue
}
byRepo[key] = coords
}
}
return func(t task.Task) forgeCoordinates {
if t.Identity != "" {
if coords, ok := byName[t.Identity]; ok {
return coords
}
}
key := ownedRepo{owner: t.Owner, name: t.Repo}
if ambiguous[key] {
return fallback
}
if coords, ok := byRepo[key]; ok {
return coords
}
return fallback
}
published := forgeCoordinates{host: view.Identity.ForgeHost, forgeType: view.Identity.ForgeType}
return func(task.Task) forgeCoordinates { return published }
}
func taskURLs(task task.Task, forge forgeCoordinates) (repoURL, issueURL, prURL string) {