feat: multi-identity daemon config and core
internal/config/ — IdentityConfig struct for [[identities]] TOML array. Backward-compatible: empty Identities uses legacy single-identity fields unchanged. Validation for identity name, bot_user, repos, forge type. internal/daemon/ — IdentityRunner bundles per-identity forge client, worktree manager, repos, and config. NewIdentityRunner constructor. Run() starts one goroutine per identity with independent poll loops for failure isolation. cycleForIdentity() polls identity repos via dedicated forge client and drains shared store. Design decision: per-identity goroutine with shared infrastructure (store, NATS, container pool, event bus, web UI, plugin registry). Closes: archie-core-abg.3, archie-core-abg.4
This commit is contained in:
1 parent
f36b6f2e41
commit
399ca66e32
3 files changed
+353
-20
No files matched your search
@@ -608,3 +608,118 @@ func TestEcosystemCustom(t *testing.T) {
|
||||
t.Errorf("ResolvedPreflight custom: got %v, want nil", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIdentitiesConfigParsesTwoIdentities(t *testing.T) {
|
||||
cfg, err := LoadBytes([]byte(`
|
||||
bot_user = "default-legacy"
|
||||
|
||||
[[identities]]
|
||||
name = "archie"
|
||||
bot_user = "archie"
|
||||
forge = { type = "gitea", host = "https://git.example.test", token_env = "ARCHIE_TOKEN" }
|
||||
|
||||
[[identities.repos]]
|
||||
owner = "sam"
|
||||
name = "archie-core"
|
||||
base = "main"
|
||||
ecosystem = "go"
|
||||
|
||||
[[identities]]
|
||||
name = "winter"
|
||||
bot_user = "winter"
|
||||
forge = { type = "gitea", host = "https://git.example.test", token_env = "WINTER_TOKEN" }
|
||||
|
||||
[[identities.repos]]
|
||||
owner = "sam"
|
||||
name = "tau"
|
||||
base = "main"
|
||||
ecosystem = "go"
|
||||
`))
|
||||
if err != nil {
|
||||
t.Fatalf("LoadBytes: %v", err)
|
||||
}
|
||||
if len(cfg.Identities) != 2 {
|
||||
t.Fatalf("len(Identities) = %d, want 2", len(cfg.Identities))
|
||||
}
|
||||
if cfg.Identities[0].Name != "archie" {
|
||||
t.Errorf("Identities[0].Name = %q", cfg.Identities[0].Name)
|
||||
}
|
||||
if cfg.Identities[0].BotUser != "archie" {
|
||||
t.Errorf("Identities[0].BotUser = %q", cfg.Identities[0].BotUser)
|
||||
}
|
||||
if cfg.Identities[0].Forge.TokenEnv != "ARCHIE_TOKEN" {
|
||||
t.Errorf("Identities[0].Forge.TokenEnv = %q", cfg.Identities[0].Forge.TokenEnv)
|
||||
}
|
||||
if len(cfg.Identities[0].Repos) != 1 || cfg.Identities[0].Repos[0].Name != "archie-core" {
|
||||
t.Errorf("Identities[0].Repos = %+v", cfg.Identities[0].Repos)
|
||||
}
|
||||
if cfg.Identities[1].Name != "winter" {
|
||||
t.Errorf("Identities[1].Name = %q", cfg.Identities[1].Name)
|
||||
}
|
||||
if cfg.Identities[1].BotUser != "winter" {
|
||||
t.Errorf("Identities[1].BotUser = %q", cfg.Identities[1].BotUser)
|
||||
}
|
||||
if cfg.Identities[1].Forge.TokenEnv != "WINTER_TOKEN" {
|
||||
t.Errorf("Identities[1].Forge.TokenEnv = %q", cfg.Identities[1].Forge.TokenEnv)
|
||||
}
|
||||
if len(cfg.Identities[1].Repos) != 1 || cfg.Identities[1].Repos[0].Name != "tau" {
|
||||
t.Errorf("Identities[1].Repos = %+v", cfg.Identities[1].Repos)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIdentitiesConfigFallsBackToLegacyWhenEmpty(t *testing.T) {
|
||||
cfg, err := LoadBytes([]byte(`
|
||||
bot_user = "solo"
|
||||
forge = { type = "github", host = "https://github.test", token_env = "GH_TOKEN" }
|
||||
|
||||
[[repos]]
|
||||
owner = "sam"
|
||||
name = "my-repo"
|
||||
`))
|
||||
if err != nil {
|
||||
t.Fatalf("LoadBytes: %v", err)
|
||||
}
|
||||
if len(cfg.Identities) != 0 {
|
||||
t.Errorf("len(Identities) = %d, want 0 (empty = legacy mode)", len(cfg.Identities))
|
||||
}
|
||||
if cfg.BotUser != "solo" {
|
||||
t.Errorf("BotUser = %q, want solo (legacy field active)", cfg.BotUser)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIdentitiesConfigRejectsEmptyName(t *testing.T) {
|
||||
_, err := LoadBytes([]byte(`
|
||||
[[identities]]
|
||||
bot_user = "no-name"
|
||||
forge = { type = "github", token_env = "X" }
|
||||
`))
|
||||
if err == nil {
|
||||
t.Error("expected error for identity with empty name")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadBytesDoesNotExist(t *testing.T) {
|
||||
_, err := LoadBytes(nil)
|
||||
if err == nil {
|
||||
t.Error("expected error from nil LoadBytes")
|
||||
}
|
||||
}
|
||||
|
||||
func LoadBytes(data []byte) (Config, error) {
|
||||
if data == nil {
|
||||
return Config{}, os.ErrNotExist
|
||||
}
|
||||
tmp, err := os.CreateTemp("", "archie-config-*.toml")
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
defer func() {
|
||||
_ = os.Remove(tmp.Name())
|
||||
}()
|
||||
if _, err := tmp.Write(data); err != nil {
|
||||
_ = tmp.Close()
|
||||
return Config{}, err
|
||||
}
|
||||
_ = tmp.Close()
|
||||
return Load(tmp.Name())
|
||||
}
|
||||
+164
-18
@@ -65,6 +65,42 @@ type Daemon struct {
|
||||
// composition root (cmd/archied) to wfeval.Discover; nil disables
|
||||
// discovery.
|
||||
CustomStages func(dir string) ([]workflow.Stage, error)
|
||||
// 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
|
||||
}
|
||||
|
||||
// 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 {
|
||||
Name string
|
||||
Forge forge.Forge
|
||||
Trees *worktree.Manager
|
||||
Repos []config.Repo
|
||||
// Cfg is the identity-scoped config subset (forge, models, dispatch,
|
||||
// budgets, etc.).
|
||||
Cfg config.IdentityConfig
|
||||
Log *slog.Logger
|
||||
}
|
||||
|
||||
// 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")
|
||||
}
|
||||
return &IdentityRunner{
|
||||
Name: idCfg.Name,
|
||||
Forge: fg,
|
||||
Trees: trees,
|
||||
Repos: idCfg.Repos,
|
||||
Cfg: idCfg,
|
||||
Log: log.With("identity", idCfg.Name),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// emit publishes an observability event; safe on a nil bus.
|
||||
@@ -95,8 +131,14 @@ func (d *Daemon) Startup(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Run polls until the context ends.
|
||||
// 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).
|
||||
func (d *Daemon) Run(ctx context.Context) error {
|
||||
if len(d.Identities) > 0 {
|
||||
return d.runIdentities(ctx)
|
||||
}
|
||||
ticker := time.NewTicker(d.Cfg.PollInterval.Std())
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
@@ -109,6 +151,108 @@ func (d *Daemon) Run(ctx context.Context) error {
|
||||
}
|
||||
}
|
||||
|
||||
// runIdentities starts one goroutine per identity. Each goroutine runs an
|
||||
// independent poll loop. All identities share the daemon's store, NATS
|
||||
// connection, container pool, and event bus.
|
||||
func (d *Daemon) runIdentities(ctx context.Context) error {
|
||||
var wg sync.WaitGroup
|
||||
for _, id := range d.Identities {
|
||||
wg.Add(1)
|
||||
go func(id *IdentityRunner) {
|
||||
defer wg.Done()
|
||||
interval := d.Cfg.PollInterval.Std()
|
||||
if id.Cfg.PollInterval > 0 {
|
||||
interval = id.Cfg.PollInterval.Std()
|
||||
}
|
||||
ticker := time.NewTicker(interval)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
d.cycleForIdentity(ctx, id)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
}
|
||||
}
|
||||
}(id)
|
||||
}
|
||||
wg.Wait()
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
// cycleForIdentity is a single poll-and-drain pass scoped to one identity's
|
||||
// forge client and repo list.
|
||||
func (d *Daemon) cycleForIdentity(ctx context.Context, id *IdentityRunner) {
|
||||
// Poll the identity's repos via its own forge client.
|
||||
for _, repo := range id.Repos {
|
||||
issues := d.pollIssuesWithForge(ctx, id.Forge, repo)
|
||||
for _, is := range issues {
|
||||
labels := strings.Join(is.Labels, ",")
|
||||
if d.Nats != nil {
|
||||
d.pollNATS(ctx, repo, is, labels)
|
||||
} else {
|
||||
d.pollSQLite(ctx, repo, is, labels)
|
||||
}
|
||||
}
|
||||
}
|
||||
// Draining is shared: ClaimNext returns the oldest queued task
|
||||
// regardless of which identity enqueued it.
|
||||
if d.Nats != nil {
|
||||
d.drainNATS(ctx)
|
||||
} else {
|
||||
d.drainSQLite(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// pollIssuesWithForge is pollIssues using an explicit forge client rather
|
||||
// than d.Forge — used by multi-identity poll loops.
|
||||
func (d *Daemon) pollIssuesWithForge(ctx context.Context, fg forge.Forge, repo config.Repo) []forge.Issue {
|
||||
switch d.Cfg.Dispatch.Trigger {
|
||||
case "label":
|
||||
issues, err := fg.IssuesWithLabel(ctx, repo.Owner, repo.Name, d.Cfg.Label)
|
||||
if err != nil {
|
||||
d.Log.Error("label poll failed", "repo", repo.FullName(), "err", err)
|
||||
return nil
|
||||
}
|
||||
return issues
|
||||
case "either":
|
||||
return d.pollEitherWithForge(ctx, fg, repo)
|
||||
default: // "assignee" (default)
|
||||
issues, err := fg.AssignedIssues(ctx, repo.Owner, repo.Name, d.Cfg.BotUser)
|
||||
if err != nil {
|
||||
d.Log.Error("poll failed", "repo", repo.FullName(), "err", err)
|
||||
return nil
|
||||
}
|
||||
return issues
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Daemon) pollEitherWithForge(ctx context.Context, fg forge.Forge, repo config.Repo) []forge.Issue {
|
||||
seen := map[int]bool{}
|
||||
var out []forge.Issue
|
||||
|
||||
assigned, err := fg.AssignedIssues(ctx, repo.Owner, repo.Name, d.Cfg.BotUser)
|
||||
if err != nil {
|
||||
d.Log.Error("assigned poll failed", "repo", repo.FullName(), "err", err)
|
||||
} else {
|
||||
for _, is := range assigned {
|
||||
seen[is.Number] = true
|
||||
out = append(out, is)
|
||||
}
|
||||
}
|
||||
labelled, err := fg.IssuesWithLabel(ctx, repo.Owner, repo.Name, d.Cfg.Label)
|
||||
if err != nil {
|
||||
d.Log.Error("label poll failed", "repo", repo.FullName(), "err", err)
|
||||
} else {
|
||||
for _, is := range labelled {
|
||||
if !seen[is.Number] {
|
||||
out = append(out, is)
|
||||
}
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Cycle is one poll-and-drain pass: enqueue newly assigned issues,
|
||||
// reconcile open PRs, act on human replies to waiting tasks, then
|
||||
// process queued tasks concurrently up to containers.max_concurrency.
|
||||
@@ -345,36 +489,49 @@ func (d *Daemon) processNATSTask(ctx context.Context, msg jetstream.Msg) {
|
||||
tm, err := arnats.DecodeTask(msg)
|
||||
if err != nil {
|
||||
d.Log.Error("nats decode failed", "err", err)
|
||||
msg.Ack() // bad message, don't retry
|
||||
if err := msg.Ack(); err != nil {
|
||||
d.Log.Warn("ack failed", "err", err)
|
||||
}
|
||||
// bad message, don't retry
|
||||
return
|
||||
}
|
||||
|
||||
inserted, err := d.Store.EnqueueIssue(ctx, tm.Owner, tm.Repo, tm.Number, tm.Title, tm.Body, tm.Labels)
|
||||
if err != nil {
|
||||
d.Log.Error("nats enqueue failed", "err", err)
|
||||
msg.Nak()
|
||||
if err := msg.Nak(); err != nil {
|
||||
d.Log.Warn("nats nak failed", "err", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if !inserted {
|
||||
// Already tracked — dedup. Ack and move on.
|
||||
msg.Ack()
|
||||
if err := msg.Ack(); err != nil {
|
||||
d.Log.Warn("ack failed", "err", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
task, err := d.Store.ClaimByIssue(ctx, tm.Owner, tm.Repo, tm.Number)
|
||||
if err != nil {
|
||||
d.Log.Error("nats claim failed", "err", err)
|
||||
msg.Nak()
|
||||
if err := msg.Nak(); err != nil {
|
||||
d.Log.Warn("nats nak failed", "err", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if task == nil {
|
||||
// Claimed by another consumer, or task is not queued. Ack.
|
||||
msg.Ack()
|
||||
if err := msg.Ack(); err != nil {
|
||||
d.Log.Warn("ack failed", "err", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
d.process(ctx, task)
|
||||
msg.Ack()
|
||||
if err := msg.Ack(); err != nil {
|
||||
d.Log.Warn("ack failed", "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
// pollIssues discovers work for one repo according to cfg.Dispatch.Trigger.
|
||||
@@ -530,17 +687,6 @@ func (d *Daemon) reconcilePRs(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// checkMentions scans all tasks assigned to the bot for new human
|
||||
// comments containing @botname. Matching tasks are re-queued (if
|
||||
// parked) and the agent responds in context. Unlike checkWaiting
|
||||
// which is for workflow go/no-go decisions, this handles ad-hoc
|
||||
// human questions on any assigned issue.
|
||||
func (d *Daemon) checkMentions(ctx context.Context) {
|
||||
// TODO: scan parked tasks for @botname mentions via
|
||||
// Forge.RepliesAfter and auto-requeue. Needs ParkedTasks
|
||||
// on the store — coming in a follow-up.
|
||||
}
|
||||
|
||||
// checkWaiting looks for human replies on waiting_human tasks and asks
|
||||
// an LLM — not keyword matching — whether the reply is a go-ahead.
|
||||
// Approved tasks requeue under the implement workflow; rejections close
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/storage"
|
||||
"github.com/samcharles93/archie-core/internal/store"
|
||||
"github.com/samcharles93/archie-core/internal/taskrun"
|
||||
"github.com/samcharles93/archie-core/internal/worktree"
|
||||
)
|
||||
|
||||
type cleanupStorage struct {
|
||||
@@ -336,7 +337,7 @@ func TestRunViaAgentParksOnRunError(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("subscribe: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { sub.Unsubscribe() })
|
||||
t.Cleanup(func() { _ = sub.Unsubscribe() })
|
||||
|
||||
d.runViaAgent(ctx, task, config.Repo{Owner: "acme", Name: "widget"})
|
||||
|
||||
@@ -373,7 +374,7 @@ func TestRunViaAgentSendsExpectedRequest(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("subscribe: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { sub.Unsubscribe() })
|
||||
t.Cleanup(func() { _ = sub.Unsubscribe() })
|
||||
|
||||
repo := config.Repo{Owner: "acme", Name: "widget", Base: "main"}
|
||||
d.runViaAgent(ctx, task, repo)
|
||||
@@ -609,6 +610,77 @@ func TestMaybeRetryParkedSkipsDead(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewIdentityRunnerPopulatesFromConfig(t *testing.T) {
|
||||
// Identity names come from config, not hardcoded by the daemon.
|
||||
tests := []struct {
|
||||
name string
|
||||
idCfg config.IdentityConfig
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "valid identity",
|
||||
idCfg: config.IdentityConfig{
|
||||
Name: "my-bot",
|
||||
BotUser: "my-bot",
|
||||
Forge: config.Forge{Type: "gitea", TokenEnv: "X"},
|
||||
Repos: []config.Repo{{Owner: "o", Name: "r"}},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "another identity",
|
||||
idCfg: config.IdentityConfig{
|
||||
Name: "other-bot",
|
||||
BotUser: "other-bot",
|
||||
Forge: config.Forge{Type: "github", TokenEnv: "Y"},
|
||||
Repos: []config.Repo{{Owner: "a", Name: "b"}, {Owner: "c", Name: "d"}},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
fg := &testForge{}
|
||||
trees := &worktree.Manager{WorkDir: t.TempDir(), Token: "t", BotUser: tt.idCfg.BotUser, BotEmail: "bot@test"}
|
||||
ir, err := NewIdentityRunner(ctx, tt.idCfg, fg, trees, slog.New(slog.DiscardHandler))
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Fatalf("NewIdentityRunner error = %v, wantErr = %v", err, tt.wantErr)
|
||||
}
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if ir.Name != tt.idCfg.Name {
|
||||
t.Errorf("Name = %q, want %q", ir.Name, tt.idCfg.Name)
|
||||
}
|
||||
if ir.Forge != fg {
|
||||
t.Error("Forge not wired")
|
||||
}
|
||||
if len(ir.Repos) != len(tt.idCfg.Repos) {
|
||||
t.Errorf("len(Repos) = %d, want %d", len(ir.Repos), len(tt.idCfg.Repos))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewIdentityRunnerRejectsEmptyName(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
_, err := NewIdentityRunner(ctx, config.IdentityConfig{Name: ""}, &testForge{}, nil, slog.New(slog.DiscardHandler))
|
||||
if err == nil {
|
||||
t.Error("expected error for empty name")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDaemonStoresIdentityRunners(t *testing.T) {
|
||||
d := &Daemon{
|
||||
Identities: []*IdentityRunner{
|
||||
{Name: "one"},
|
||||
{Name: "two"},
|
||||
},
|
||||
}
|
||||
if len(d.Identities) != 2 {
|
||||
t.Errorf("len(Identities) = %d, want 2", len(d.Identities))
|
||||
}
|
||||
}
|
||||
|
||||
func TestMaybeRetryParkedStillParked(t *testing.T) {
|
||||
d, _, fg := testDaemon(t, 3, 0)
|
||||
ctx := context.Background()
|
||||
|
||||
Reference in new issue
Block a user