package daemon import ( "context" "log/slog" "strings" "sync" "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/domain/identity" "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/domain/workintake" "github.com/samcharles93/archie-core/internal/forge" "github.com/samcharles93/archie-core/internal/taskstate" ) // activeIdentities answers whether an identity may still act; a poll skips // sources whose identity is suspended. type activeIdentities interface { identityActive(ctx context.Context, id identity.IdentityID) bool } // routedWorkflows answers whether an issue's labels route it to a workflow // its org disabled. type routedWorkflows interface { disabledRoutedWorkflow(ctx context.Context, labels, identityID string) (routed string, disabled bool, err error) } // IssuePoller discovers the forge issues that dispatch rules make work and // publishes them as tasks, and withdraws tasks whose issue drops out. type IssuePoller struct { cfg *config.Holder store storecontract.TaskStore log *slog.Logger sources func(context.Context) []IssueSource withdrawTask func(ctx context.Context, taskID int64, reason string) error publishTask func(context.Context, workintake.TaskEnvelope) error identities activeIdentities routing routedWorkflows // lastPolled remembers each repo's eligible issues from its previous // complete poll, under the dispatch rule that produced them, so only an // issue that was eligible and dropped out reads as withdrawn. lastPolledMu sync.Mutex lastPolled map[string]polledIssues } // NewIssuePoller builds the poller over the running configuration, the task // store and the task publisher. sources resolves each poll cycle's connections, // withdrawTask declines a task whose issue a complete poll no longer returns, // identities and routing answer the identity and routed-workflow checks. func NewIssuePoller( cfg *config.Holder, store storecontract.TaskStore, log *slog.Logger, sources func(context.Context) []IssueSource, withdrawTask func(ctx context.Context, taskID int64, reason string) error, publishTask func(context.Context, workintake.TaskEnvelope) error, identities activeIdentities, routing routedWorkflows, ) *IssuePoller { return &IssuePoller{ cfg: cfg, store: store, log: log, sources: sources, withdrawTask: withdrawTask, publishTask: publishTask, identities: identities, routing: routing, lastPolled: map[string]polledIssues{}, } } func (p *IssuePoller) issueSources(ctx context.Context) []IssueSource { return p.sources(ctx) } // pollIssuesWithConfig returns the repo's issues the dispatch rule makes // 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 (p *IssuePoller) pollIssuesWithConfig(ctx context.Context, fg forge.Forge, cfg config.Config, repo config.Repo) (issues []forge.Issue, complete bool) { labelled := func() ([]forge.Issue, bool) { // A label trigger needs a non-empty label or it would match every open issue. if cfg.Label == "" { p.log.ErrorContext(ctx, "label trigger configured with an empty label; refusing to poll (an empty label matches every open issue)", "repo", repo.FullName()) return nil, false } issues, err := fg.IssuesWithLabel(ctx, repo.Owner, repo.Name, cfg.Label) if err != nil { p.log.ErrorContext(ctx, "label poll failed", "repo", repo.FullName(), "err", err) return nil, false } return issues, true } assigned := func() ([]forge.Issue, bool) { if cfg.BotUser == "" { p.log.ErrorContext(ctx, "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 { p.log.ErrorContext(ctx, "assigned poll failed", "repo", repo.FullName(), "err", err) return nil, false } return issues, true } switch cfg.Dispatch.Trigger { case "label": return labelled() case "either": byAssignee, assignedOK := assigned() byLabel, labelledOK := labelled() seen := map[int]bool{} for _, is := range byAssignee { seen[is.Number] = true } issues = byAssignee for _, is := range byLabel { if !seen[is.Number] { issues = append(issues, is) } } return issues, assignedOK && labelledOK default: // "assignee" (default) return assigned() } } // 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 (p *IssuePoller) poll(ctx context.Context) error { var failed []string room := p.cfg.Get().IntakePerPass for _, source := range p.issueSources(ctx) { if !p.identities.identityActive(ctx, source.Identity) { continue } cfg := p.cfg.Get() cfg.BotUser = source.BotUser if source.Dispatch.Trigger != "" { cfg.Dispatch, cfg.Label = source.Dispatch, source.Label } tag := string(source.Identity) for _, repo := range source.Repos { issues, complete := p.pollIssuesWithConfig(ctx, source.Forge, cfg, repo) if complete { p.withdrawUnpolled(ctx, repo, tag, dispatchRule(cfg), issues) } else { failed = append(failed, repo.FullName()) } p.publishDiscovered(ctx, source.Forge, cfg, repo, issues, tag, &room) } } return passFailed("issue poll", failed) } // rememberPoll records a repo's latest eligible set and returns the previous // one, reporting whether it was taken under the same rule. func (p *IssuePoller) rememberPoll(key string, current polledIssues) (polledIssues, bool) { p.lastPolledMu.Lock() defer p.lastPolledMu.Unlock() previous, seen := p.lastPolled[key] p.lastPolled[key] = current return previous, seen && previous.rule == current.rule } // withdrawUnpolled declines the queued or running work of an identity's // issues in repo that a complete poll no longer returns: the issue was // closed, unlabelled or unassigned. func (p *IssuePoller) withdrawUnpolled(ctx context.Context, repo config.Repo, identity, rule string, polled []forge.Issue) { eligible := make(map[int]bool, len(polled)) for _, is := range polled { eligible[is.Number] = true } // Only an issue the same rule found eligible last time and no longer // finds was withdrawn. A changed label, trigger or bot user, or the first // poll after a restart, only records the new set: tasks picked up under // the old rule were not withdrawn by anyone. previous, sameRule := p.rememberPoll(identity+"\x00"+repo.FullName(), polledIssues{rule: rule, eligible: eligible}) if !sameRule { return } active, err := p.store.TasksPage(ctx, storecontract.TaskPage{ Statuses: []string{taskstate.Queued, taskstate.Running}, Limit: 500, }) if err != nil { p.log.WarnContext(ctx, "withdrawal check: active tasks unavailable", "repo", repo.FullName(), "err", err) return } for _, t := range active { if t.Owner != repo.Owner || t.Repo != repo.Name || t.Identity != identity || !t.IsForgeBacked() || t.PRNumber > 0 || eligible[t.IssueNumber] || !previous.eligible[t.IssueNumber] { continue } if err := p.withdrawTask(ctx, t.ID, "the issue is no longer assigned, labelled or open"); err != nil { p.log.WarnContext(ctx, "withdraw task", "task", t.ID, "err", err) } } } // publishDiscovered publishes a repo's discovered issues until the pass has // created its cap of tasks. room is what is left of the cap; the issues left // over are found again by the next poll, because an issue with a task is // skipped and one without is not. A cap of zero or less is no cap. func (p *IssuePoller) publishDiscovered(ctx context.Context, fg forge.Forge, cfg config.Config, repo config.Repo, issues []forge.Issue, identity string, room *int) { // A label or assignment is an operator's act, but the title and body that // reach the agent were written by the issue's author, who on a public // repository can be anyone. issues, err := forge.WriterIssues(ctx, p.log, fg, repo.Owner, repo.Name, issues) if err != nil { p.log.ErrorContext(ctx, "issues not published: author write access unknown", "repo", repo.FullName(), "err", err) return } capped := *room > 0 for _, is := range issues { if capped && *room == 0 { p.log.InfoContext(ctx, "intake cap reached; remaining issues wait for the next poll", "repo", repo.FullName()) return } if p.pollNATS(ctx, fg, cfg, repo, is, strings.Join(is.Labels, ","), identity) && capped { *room-- } } } // pollNATS publishes a discovered issue to NATS and reports whether it created // a task. func (p *IssuePoller) pollNATS(ctx context.Context, fg forge.Forge, cfg config.Config, repo config.Repo, is forge.Issue, labels, identity string) bool { // Read-only existence check prevents rediscovering an existing task. existing, err := p.store.TaskByIssue(ctx, repo.Owner, repo.Name, is.Number) if err != nil { p.log.ErrorContext(ctx, "task lookup failed", "repo", repo.FullName(), "issue", is.Number, "err", err) return false } if existing != nil { return false } // An issue whose label-routed workflow the identity's org disabled must // not queue a task at all. Skip without acknowledging, so a later poll // picks the issue up again if the workflow is re-enabled. if routed, disabled, err := p.routing.disabledRoutedWorkflow(ctx, labels, identity); err != nil { p.log.WarnContext(ctx, "routed workflow check failed; publishing anyway", "repo", repo.FullName(), "issue", is.Number, "err", err) } else if disabled { p.log.InfoContext(ctx, "issue skipped: its routed workflow is disabled for this org", "repo", repo.FullName(), "issue", is.Number, "workflow", routed) return false } // Encode here: the envelope and its subject belong to the work-intake // domain, not to the transport. parsed := workintake.SplitLabels(labels) task := workintake.TaskEnvelope{ Owner: repo.Owner, Repo: repo.Name, Number: is.Number, Title: is.Title, Body: is.Body, Labels: parsed, Identity: identity, } if err := p.publishTask(ctx, task); err != nil { p.log.ErrorContext(ctx, "task publish failed", "repo", repo.FullName(), "issue", is.Number, "err", err) return false } p.acknowledge(ctx, fg, cfg, repo, is) return true } // acknowledge posts the pickup reaction // using fg -- the forge client that owns repo (identity-scoped or root) -- // and the ack reaction from cfg, the dispatch config that owns this poll. func (p *IssuePoller) acknowledge(ctx context.Context, fg forge.Forge, cfg config.Config, repo config.Repo, is forge.Issue) { p.log.InfoContext(ctx, "issue queued", "repo", repo.FullName(), "issue", is.Number, "title", is.Title) if ack := cfg.Dispatch.AckReaction; ack != "" { if err := fg.React(ctx, repo.Owner, repo.Name, is.Number, ack); err != nil { p.log.WarnContext(ctx, "ack reaction failed", "issue", is.Number, "err", err) } } } // polledIssues is one repo's eligible issues under one dispatch rule. type polledIssues struct { rule string eligible map[int]bool } // dispatchRule identifies the rule a poll selects issues by. func dispatchRule(cfg config.Config) string { return cfg.Dispatch.Trigger + "\x00" + cfg.Label + "\x00" + cfg.BotUser }