fix(daemon): inherit live settings for identity tasks

This commit is contained in:
sam committed 2026-10-09 09:57:56 +11:00
1 parent 7e3b3dbd78
commit d0370ed9f7
8 files changed
+59 -72

No files matched your search

+2 -1
View File
@@ -63,7 +63,8 @@ token = { engine = "env", key = "ARCHIE_GITHUB_TOKEN" }
# 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/dispatch/models/providers/budgets/notify. Intake
# 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:
# [[identities]]
-3
View File
@@ -210,9 +210,6 @@ func applyExecutionBudgets(cfg *config.Config, settings workflow.ExecutionSettin
MaxSteps: settings.MaxModelToolSteps, WallClock: config.Duration(settings.MaxRuntime), GateMaxFailures: settings.MaxConsecutiveGateFailures,
TaskWallClock: config.Duration(settings.MaxTaskRuntime),
}
for i := range cfg.Identities {
cfg.Identities[i].Budgets = cfg.Budgets
}
}
// startLiveSettings starts everything a live control-plane update needs
+2 -13
View File
@@ -72,18 +72,7 @@ func (b *boot) startWatchdog(ctx context.Context) {
// beginning a fresh poll pass, and therefore how stale the progress marker may
// be before the loop counts as stalled.
//
// It is the root poll interval, or the slowest configured identity in
// multi-identity mode: each identity polls its own repos, identity poll
// intervals are frozen at boot, and the marker is stamped by whichever identity
// polled most recently -- so the worst case between stamps is the slowest
// identity, not the fastest. The root value is re-read per sample, so a
// reloaded poll_interval is honoured.
// The shared interval is re-read so live polling changes are honoured.
func (b *boot) loopCadence() time.Duration {
cadence := b.cfgHolder.Get().PollInterval.Std()
for _, id := range b.d.Identities {
if override := id.Cfg.PollInterval.Std(); override > cadence {
cadence = override
}
}
return cadence
return b.cfgHolder.Get().PollInterval.Std()
}
-5
View File
@@ -15,11 +15,6 @@ func ResolveProviderSecrets(cfg *config.Config, registry *secret.Registry, log *
if err := resolveProviderMap("root", cfg.Providers, registry, log); err != nil {
return err
}
for i := range cfg.Identities {
if err := resolveProviderMap(cfg.Identities[i].Name, cfg.Identities[i].Providers, registry, log); err != nil {
return fmt.Errorf("identity %q: %w", cfg.Identities[i].Name, err)
}
}
return nil
}
+5 -19
View File
@@ -396,8 +396,8 @@ type Config struct {
}
// IdentityConfig is a per-identity configuration subset. Each identity
// gets its own poll loop goroutine with isolated forge, repo, model,
// and dispatch configuration.
// gets its own forge account and repository membership. 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
@@ -413,22 +413,11 @@ type IdentityConfig struct {
Org string `toml:"org" yaml:"org"`
// GrantedCredentials overrides the shared GrantedCredentials for this
// identity. Nil (absent) inherits it; a present, empty list grants none
// -- the same explicit-empty-means-off shape DiffCapLines uses, needed
// -- 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"`
// DiffCapLines overrides the shared cap for this identity. Nil (absent)
// inherits it; an explicit 0 switches the cap off for this identity only.
DiffCapLines *int `toml:"diff_cap_lines" yaml:"diff_cap_lines"`
// PollInterval overrides the shared PollInterval. When 0, the shared
// interval is used.
PollInterval Duration `toml:"poll_interval" yaml:"poll_interval"`
Forge Forge `toml:"forge" yaml:"forge"`
Dispatch Dispatch `toml:"dispatch" yaml:"dispatch"`
Models map[string]string `toml:"models" yaml:"models"`
Providers map[string]Provider `toml:"providers" yaml:"providers"`
Budgets Budgets `toml:"budgets" yaml:"budgets"`
Notify Notify `toml:"notify" yaml:"notify"`
Repos []Repo `toml:"repos" yaml:"repos"`
Forge Forge `toml:"forge" yaml:"forge"`
Repos []Repo `toml:"repos" yaml:"repos"`
}
type ModelLimits struct {
@@ -587,10 +576,7 @@ func cloneIdentities(ids []IdentityConfig) []IdentityConfig {
}
out := make([]IdentityConfig, len(ids))
for i, id := range ids {
id.Models = cloneStringMap(id.Models)
id.Providers = maps.Clone(id.Providers)
id.Repos = cloneRepos(id.Repos)
id.DiffCapLines = cloneIntPtr(id.DiffCapLines)
out[i] = id
}
return out
+22 -28
View File
@@ -241,8 +241,7 @@ type IdentityRunner struct {
Forge forge.Forge
Trees *worktree.Manager
Repos []config.Repo
// Cfg is the identity-scoped config subset (forge, models, dispatch,
// budgets, etc.).
// Cfg holds the identity's forge account and repository membership.
Cfg config.IdentityConfig
Log *slog.Logger
}
@@ -385,9 +384,6 @@ func (d *Daemon) runIdentities(ctx context.Context) error {
go func(id *IdentityRunner) {
defer wg.Done()
interval := d.Cfg.Get().PollInterval.Std()
if id.Cfg.PollInterval > 0 {
interval = id.Cfg.PollInterval.Std()
}
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
@@ -396,12 +392,8 @@ func (d *Daemon) runIdentities(ctx context.Context) error {
case <-ctx.Done():
return
case <-ticker.C:
// The identity's own override is frozen (identity config is
// startup-built); the root fallback may have been reloaded.
// Pick up the current shared polling interval.
iv := d.Cfg.Get().PollInterval.Std()
if id.Cfg.PollInterval > 0 {
iv = id.Cfg.PollInterval.Std()
}
if iv != interval {
interval = iv
ticker.Reset(iv)
@@ -447,7 +439,7 @@ func (d *Daemon) pollForIdentity(ctx context.Context, id *IdentityRunner) {
}
d.markPoll()
cfg := configForIdentity(d.Cfg.Get(), id.Cfg)
for _, repo := range id.Repos {
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)
@@ -479,8 +471,8 @@ func (d *Daemon) maintainAndDrain(ctx context.Context) {
// 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 polls with its own bot user, label and
// trigger. complete is false when any part of the poll failed or was skipped, so
// 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
// 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
@@ -2365,22 +2357,9 @@ func (d *Daemon) captureAttemptConfig(ctx context.Context, task *workflow.Task,
}
func configForIdentity(root config.Config, identity config.IdentityConfig) config.Config {
root.BotUser = identity.BotUser
root.BotEmail = identity.BotEmail
// Only an identity that set the cap overrides the shared one. Assigning
// unconditionally made an identity that omits the key inherit a nil cap,
// which reads as "no cap" and silently disabled the safety rail.
if identity.DiffCapLines != nil {
root.DiffCapLines = identity.DiffCapLines
}
root.BotUser, root.BotEmail = identity.BotUser, identity.BotEmail
root.Org, root.GrantedCredentials = root.CredentialAccess(identity.Name)
root.Forge = identity.Forge
root.Dispatch = identity.Dispatch
root.Models = identity.Models
root.Providers = identity.Providers
root.Budgets = identity.Budgets
root.Notify = identity.Notify
root.Repos = identity.Repos
return root
}
@@ -2469,7 +2448,7 @@ func (d *Daemon) treesFor(task *workflow.Task) *worktree.Manager {
func (d *Daemon) repoFor(t *workflow.Task) (config.Repo, bool) {
repos := d.Cfg.Get().Repos
if id := d.identityFor(t); id != nil {
repos = id.Repos
repos = identityRepositories(repos, id.Repos)
}
for _, r := range repos {
if r.Owner == t.Owner && r.Name == t.Repo {
@@ -2555,3 +2534,18 @@ 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
}
+27
View File
@@ -3,8 +3,10 @@ package daemon
import (
"context"
"log/slog"
"reflect"
"testing"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
@@ -93,3 +95,28 @@ 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"
root.Notify.Webhook = "https://example.test/notify"
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 || got.Notify != root.Notify {
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")
}
}
}
@@ -82,9 +82,7 @@ func Apply(cfg *config.Config, snapshot Snapshot) []string {
}
cfg.ModelLimits = limits
cfg.Providers = mergeProviders(discovered, cfg.Providers)
for i := range cfg.Identities {
cfg.Identities[i].Providers = mergeProviders(discovered, cfg.Identities[i].Providers)
}
slices.Sort(models)
return models
}