fix(daemon): sweep every identity's forge and repos at startup

daemon.Startup ran the invitation sweep and push verification only against the
root forge and Cfg().Repos. In multi-identity mode Run branches to
runIdentities, where each identity polls its own forge and its own repo list --
so per-identity invitations were never accepted and per-identity repos were
never push-verified. A task from those repos failed later at dispatch instead of
warning at boot.

Startup now branches on len(d.Identities) and sweeps each identity through
sweepIdentity, which reuses id.Forge and id.Repos -- the same IdentityRunner
fields runIdentities and pollForIdentity already iterate, so there is no second
identity-to-forge mapping. One identity's forge outage cannot abort a sibling's
sweep: failures are logged as warnings, matching Run's stated failure-isolation
intent.

Red-then-green: TestStartupSweepsEveryIdentityForgeAndRepos failed with
"identity one AcceptInvitations = 0, want 1" (and both identities' repos empty)
before the change.
This commit is contained in:
sam committed 2026-09-19 01:51:03 +10:00
1 parent 5e15bfd07a
commit 8ffa6ffb51
2 files changed
+110

No files matched your search

+28
View File
@@ -259,6 +259,19 @@ func (d *Daemon) Startup(ctx context.Context) error {
if n > 0 {
d.Log.Info("re-queued tasks left running by a previous daemon", "count", n)
}
// Multi-identity mode sweeps each identity's own forge and repo list:
// Run takes runIdentities there and never polls the root forge/repos, so
// verifying the root repo list would warn about repos no task can target.
// Failures are isolated per identity (logged as warnings, never aborting
// a sibling's sweep), matching Run's failure-isolation intent.
if len(d.Identities) > 0 {
for _, id := range d.Identities {
d.sweepIdentity(ctx, id)
}
return nil
}
if err := d.Forge.AcceptInvitations(ctx); err != nil {
d.Log.Warn("invitation sweep failed", "err", err)
}
@@ -270,6 +283,21 @@ func (d *Daemon) Startup(ctx context.Context) error {
return nil
}
// sweepIdentity runs the boot-time invitation sweep and push verification
// for one identity's own forge and repo list. Failures are logged as
// warnings and never propagate, so one identity's forge outage cannot stop
// another identity's boot sweep.
func (d *Daemon) sweepIdentity(ctx context.Context, id *IdentityRunner) {
if err := id.Forge.AcceptInvitations(ctx); err != nil {
d.Log.Warn("invitation sweep failed", "identity", id.Name, "err", err)
}
for _, r := range id.Repos {
if err := id.Forge.VerifyPush(ctx, r.Owner, r.Name); err != nil {
d.Log.Warn("repo not pushable -- tasks from it will fail", "identity", id.Name, "repo", r.FullName(), "err", err)
}
}
}
// 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
+82
View File
@@ -1336,6 +1336,88 @@ func TestRepoForPrefersOwningIdentityRepoList(t *testing.T) {
}
}
// sweepForge records invitation sweeps and push verifications so a test can
// assert Startup sweeps each identity's own forge and repos. It embeds
// testForge for the rest of the forge.Forge surface; those methods are
// unreachable from Startup.
type sweepForge struct {
testForge
invitations int
verifiedRepos []string
}
func (f *sweepForge) AcceptInvitations(context.Context) error {
f.invitations++
return nil
}
func (f *sweepForge) VerifyPush(_ context.Context, owner, repo string) error {
f.verifiedRepos = append(f.verifiedRepos, owner+"/"+repo)
return nil
}
// TestStartupSweepsEveryIdentityForgeAndRepos pins the boot-time invitation
// sweep and push verification in multi-identity mode. Startup used to sweep
// only d.Forge and d.Cfg.Get().Repos, so per-identity invites were never
// accepted and per-identity repos never push-verified: a task from one of
// those repos would fail at dispatch instead of warning at boot. In
// multi-identity mode Run takes runIdentities and never polls the root repo
// list, so Startup must sweep each identity's own forge/repos and leave the
// root forge/repos alone (single-identity mode keeps the original root sweep).
func TestStartupSweepsEveryIdentityForgeAndRepos(t *testing.T) {
rootFg := &sweepForge{}
oneFg := &sweepForge{}
twoFg := &sweepForge{}
d := &Daemon{
Cfg: config.NewHolder(config.Config{
Repos: []config.Repo{{Owner: "root", Name: "repo"}},
}),
Store: store.OpenTest(t),
Forge: rootFg,
Log: slog.New(slog.DiscardHandler),
Identities: []*IdentityRunner{
{
Name: "one",
Forge: oneFg,
Repos: []config.Repo{{Owner: "one", Name: "a"}},
},
{
Name: "two",
Forge: twoFg,
Repos: []config.Repo{{Owner: "two", Name: "b"}, {Owner: "two", Name: "c"}},
},
},
}
if err := d.Startup(context.Background()); err != nil {
t.Fatalf("Startup: %v", err)
}
if oneFg.invitations != 1 {
t.Errorf("identity one AcceptInvitations = %d, want 1", oneFg.invitations)
}
if twoFg.invitations != 1 {
t.Errorf("identity two AcceptInvitations = %d, want 1", twoFg.invitations)
}
if got := oneFg.verifiedRepos; !slices.Equal(got, []string{"one/a"}) {
t.Errorf("identity one verified repos = %v, want [one/a]", got)
}
if got := twoFg.verifiedRepos; !slices.Equal(got, []string{"two/b", "two/c"}) {
t.Errorf("identity two verified repos = %v, want [two/b two/c]", got)
}
// The root forge/repo list must not be swept in multi-identity mode:
// Run never polls it there, so verifying it would warn about repos no
// task can target.
if rootFg.invitations != 0 {
t.Errorf("root AcceptInvitations = %d, want 0 (root forge is not polled in multi-identity mode)", rootFg.invitations)
}
if len(rootFg.verifiedRepos) != 0 {
t.Errorf("root verified repos = %v, want none (root repo list is not polled in multi-identity mode)", rootFg.verifiedRepos)
}
}
// mustCoreConn returns the raw NATS connection for tests that drive core-NATS
// subscriptions directly, failing the test if the client is not connected.
func mustCoreConn(t *testing.T, c *arnats.Client) *natsio.Conn {