Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/added-18494-release-feed-watcher.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- **Release-feed watcher: kick an agent when a tracked upstream dependency releases.** `governor.release_feeds.agents.<agent>.sources` lists GitHub releases or tags (`owner/repo`), PyPI packages, or a generic index URL. The governor polls them every `interval_s` (default `HIVE_RELEASE_FEED_INTERVAL`, else 15 minutes) with `If-None-Match` / `If-Modified-Since`, through the spoke's upstream-proxy rules, and keeps last-seen versions in `/data/release-feeds.json`. A new version queues a kick for the subscribed agent through the kick queue with a `${NEW_RELEASES}` block (package, old, new, URL) and records the reason on the governor kick-history entry; the first answer is a baseline and an unchanged feed kicks nothing. Kicks pass the same gates as the webhook CI-failure kick and are bounded by a per-agent cooldown (default `HIVE_RELEASE_FEED_COOLDOWN`, else 1 hour). Private indexes authenticate with `auth_var`, an existing per-agent or hive-level variable whose value is never logged. See `src/docs/release-feeds.md` ([#18494](https://github.com/hivecommons/hive/issues/18494)).
4 changes: 4 additions & 0 deletions src/cmd/hive/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/hivecommons/hive/pkg/priorart"
"github.com/hivecommons/hive/pkg/promptsrc"
"github.com/hivecommons/hive/pkg/questionclose"
"github.com/hivecommons/hive/pkg/releasefeed"
"github.com/hivecommons/hive/pkg/repoagents"
"github.com/hivecommons/hive/pkg/retro"
"github.com/hivecommons/hive/pkg/review/eventdispatch"
Expand Down Expand Up @@ -288,6 +289,9 @@ type boot struct {
// owned by its independent ticker goroutine.
matrixIngestSt *matrixIngestState
matrixIngestStarted bool
// releaseFeed is the release-feed watcher (#18494), started once by
// startReleaseFeedWatcher.
releaseFeed *releasefeed.Watcher
// gfi is good-first-issue curation state (good_first_issue.*).
gfi *gfiState
// firstTimer is first-time contributor welcome state
Expand Down
5 changes: 3 additions & 2 deletions src/cmd/hive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2502,7 +2502,7 @@ func (b *boot) bootStateWith(deps bootStateDeps) {
if len(b.saved.KickHistory) > 0 {
records := make([]governor.KickRecord, len(b.saved.KickHistory))
for i, ke := range b.saved.KickHistory {
records[i] = governor.KickRecord{Timestamp: ke.Timestamp, Agent: ke.Agent, Outcome: ke.Outcome, OutcomeReason: ke.OutcomeReason}
records[i] = governor.KickRecord{Timestamp: ke.Timestamp, Agent: ke.Agent, Outcome: ke.Outcome, OutcomeReason: ke.OutcomeReason, Reason: ke.Reason}
}
b.gov.SeedKickHistory(records)
b.logger.Info("kick history restored", "entries", len(records))
Expand Down Expand Up @@ -6354,6 +6354,7 @@ func (b *boot) runLoopWith(deps runLoopDeps) {
}
b.startReviewEvents()
b.startMatrixIngest()
b.startReleaseFeedWatcher()

// Webhook health re-check: in webhook mode the base tick (900s) is longer
// than the health window (2 × eval_interval_s), so without this a lapse
Expand Down Expand Up @@ -6830,7 +6831,7 @@ func persistStateWithPaths(agentMgr crux.Runtime, gov *governor.Governor, cfg *c
govKickHistory := gov.KickHistory()
kickEntries := make([]snapshot.GovKickEntry, len(govKickHistory))
for i, kr := range govKickHistory {
kickEntries[i] = snapshot.GovKickEntry{Timestamp: kr.Timestamp, Agent: kr.Agent, Outcome: kr.Outcome, OutcomeReason: kr.OutcomeReason}
kickEntries[i] = snapshot.GovKickEntry{Timestamp: kr.Timestamp, Agent: kr.Agent, Outcome: kr.Outcome, OutcomeReason: kr.OutcomeReason, Reason: kr.Reason}
}

state := &snapshot.PersistedState{
Expand Down
202 changes: 202 additions & 0 deletions src/cmd/hive/release_feed_watcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
package main

import (
"context"
"fmt"
"strings"
"time"

"github.com/hivecommons/hive/crux"
"github.com/hivecommons/hive/pkg/advisory"
"github.com/hivecommons/hive/pkg/config"
"github.com/hivecommons/hive/pkg/governor"
"github.com/hivecommons/hive/pkg/releasefeed"
"github.com/hivecommons/hive/pkg/resolve"
)

// releaseFeedStatePath is where the watcher keeps last-seen versions and
// validators across restarts. A var so tests can point it at a temp dir.
var releaseFeedStatePath = "/data/release-feeds.json"

// releaseFeedKickHeader opens a targeted release-feed kick when the agent's
// own template does not place ${NEW_RELEASES} itself.
const releaseFeedKickHeader = "Upstream dependencies you track published new versions. Act on them now (for example, open the dependency bump); detection was done by the hive's release-feed watcher, so do not poll the feeds yourself.\n\n"

// releaseFeedKicker gates and delivers a release-feed kick. Gating mirrors the
// webhook CI-failure kicker: agent known, enabled and not paused in config or
// at runtime, and governor eligibility (cadence pause, on-demand, budget). A
// refused kick returns releasefeed.ErrGated so the releases stay pending.
type releaseFeedKicker struct {
cfg func() *config.Config
gov func() *governor.Governor
isPaused func(agent string) bool
// render returns the agent's full kick prompt, whose ${NEW_RELEASES}
// the watcher's Block fills; nil or "" falls back to a targeted kick.
render func(agent string) string
// enqueue hands the message to the runtime's kick queue.
enqueue func(agent, message string) error
}

func (k *releaseFeedKicker) Kick(_ context.Context, agent string, releases []releasefeed.Release) error {
if reason := k.gate(agent); reason != "" {
return fmt.Errorf("%w: %s", releasefeed.ErrGated, reason)
}
if k.enqueue == nil {
return fmt.Errorf("%w: no kick sender", releasefeed.ErrGated)
}
block := releasefeed.FormatBlock(releases)
message := ""
if k.render != nil {
message = k.render(agent)
}
if !strings.Contains(message, releasefeed.BlockHeader) {
message = releaseFeedKickHeader + block
}
// Release names and URLs are upstream text an agent may quote into an
// issue or PR, so they go through the same mention sanitizer as every
// other hive-authored kick.
message = advisory.NeutralizeMentions(message)
if err := k.enqueue(agent, message); err != nil {
return err
}
if gov := k.governor(); gov != nil {
gov.RecordKickWithReason(agent, releasefeed.Reason(releases))
}
return nil
}

func (k *releaseFeedKicker) gate(agent string) string {
cfg := k.config()
if cfg == nil {
return "no config"
}
ac, ok := cfg.Agents[agent]
if !ok {
return "unknown agent"
}
if !ac.Enabled || ac.Paused || (k.isPaused != nil && k.isPaused(agent)) {
return "agent paused"
}
if gov := k.governor(); gov != nil && !gov.AgentEligibleForCELKick(agent) {
return "governor gated"
}
return ""
}

func (k *releaseFeedKicker) config() *config.Config {
if k.cfg != nil {
return k.cfg()
}
return nil
}

func (k *releaseFeedKicker) governor() *governor.Governor {
if k.gov != nil {
return k.gov()
}
return nil
}

// releaseFeedSubscriptions maps governor.release_feeds onto watcher sources.
func releaseFeedSubscriptions(cfg *config.Config) map[string][]releasefeed.Source {
out := map[string][]releasefeed.Source{}
if cfg == nil || cfg.Governor.ReleaseFeeds == nil {
return out
}
for agent, a := range cfg.Governor.ReleaseFeeds.Agents {
if len(a.Sources) == 0 {
continue
}
srcs := make([]releasefeed.Source, 0, len(a.Sources))
for _, s := range a.Sources {
srcs = append(srcs, releasefeed.Source{
Kind: s.Kind, Repo: s.Repo, Package: s.Package, URL: s.URL,
VersionField: s.VersionField, Name: s.Name, AuthVar: s.AuthVar,
})
}
out[agent] = srcs
}
return out
}

// releaseFeedToken resolves a source's auth variable through the agent's
// effective variables (per-agent over hive-level), the same registry that
// fills its kick prompt. An unresolved variable yields "". The value is
// returned to the fetcher only and never logged.
func releaseFeedToken(cfg *config.Config, agent, name string) string {
if cfg == nil || !config.ValidVariableName(name) {
return ""
}
raw := "${" + name + "}"
v := cfg.ResolveRegistryForAgent(agent, nil).Expand(context.Background(), raw, resolve.ScopeTemplate, nil)
if v == raw {
return ""
}
return strings.TrimSpace(v)
}

// releaseFeedConfig is the config the watcher goroutine reads: the immutable
// snapshot the governor loop publishes after every reload (b.sweepConfig),
// never b.cfg itself, which a reload overwrites in place under cfgReloadMu.
// Nil (not yet published) reads as disabled.
func (b *boot) releaseFeedConfig() *config.Config { return b.sweepConfig.Load() }

// newReleaseFeedWatcher builds the watcher from boot state. Config, governor,
// scheduler and runtime are resolved per call so a reload or late wiring is
// picked up without restarting it.
func (b *boot) newReleaseFeedWatcher() *releasefeed.Watcher {
return releasefeed.New(releasefeed.Options{
Enabled: func() bool { return b.releaseFeedConfig().ReleaseFeedsEnabled() },
Subscriptions: func() map[string][]releasefeed.Source { return releaseFeedSubscriptions(b.releaseFeedConfig()) },
Interval: func() time.Duration { return b.releaseFeedConfig().ReleaseFeedInterval() },
Cooldown: func(agent string) time.Duration { return b.releaseFeedConfig().ReleaseFeedCooldown(agent) },
Token: func(agent, name string) string { return releaseFeedToken(b.releaseFeedConfig(), agent, name) },
Store: releasefeed.NewFileStore(releaseFeedStatePath),
Kicker: b.newReleaseFeedKicker(),
Logger: b.logger,
})
}

// newReleaseFeedKicker wires the kicker to boot state.
func (b *boot) newReleaseFeedKicker() *releaseFeedKicker {
return &releaseFeedKicker{
cfg: b.releaseFeedConfig,
gov: func() *governor.Governor { return b.gov },
isPaused: func(agent string) bool { return b.agentMgr != nil && b.agentMgr.IsPaused(agent) },
render: func(agent string) string {
if b.sched == nil {
return ""
}
return b.sched.BuildAgentMessageFromLastActionable(agent)
},
enqueue: func(agent, message string) error {
if b.agentMgr == nil {
return errNoAgentRuntime
}
// The kick queue (#18285) delivers once the agent is idle, so a
// release never interrupts a running turn. A runtime without it
// (CRUX_MODE=sidecar) takes the plain kick.
if q, ok := b.agentMgr.(crux.KickQueuer); ok {
_, err := q.EnqueueKick(agent, "", message)
return err
}
return b.agentMgr.SendKick(agent, message)
},
}
}

// startReleaseFeedWatcher starts the watcher goroutine once and installs its
// ${NEW_RELEASES} source on the scheduler. The goroutine idles (one cheap
// config check per interval) while governor.release_feeds lists no source,
// so enabling it by a config reload needs no restart.
func (b *boot) startReleaseFeedWatcher() {
if b.releaseFeed != nil || b.ctx == nil || b.cfg == nil {
return
}
w := b.newReleaseFeedWatcher()
b.releaseFeed = w
if b.sched != nil {
b.sched.SetNewReleasesSource(w.Block)
}
go w.Run(b.ctx)
}
Loading
Loading