From 06121ce4ddfa87d3e0f2602e324e4e654dfdabad Mon Sep 17 00:00:00 2001 From: Danathar Date: Wed, 18 Nov 2026 23:53:21 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20review=5Ffeedback.rules.*=20setting?= =?UTF-8?q?s=20in=20hive.yaml=20plus=20owner-only=20Settings=20card?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add review_feedback.rules.{enabled,trusted_associations,extra_phrases, disabled_signals} to hive.yaml and an owner-only "Rules from review feedback" card in Settings, Features, Review that edits them in place. The sweep resolves the policy from hive.yaml on every pass and hands it to the open rule store, so an edit applies without a restart. The keys are additive: HIVE_REVIEW_FEEDBACK_RULES and HIVE_REVIEW_FEEDBACK_RULE_TRUSTED_ASSOCIATIONS keep working and win when set, and the card and API report that. Extra phrases are quoted, never patterns; disabled signals name the built-in classifier signals. Tests for the config block, the policy and classifier, the live store, the owner-only API and the card; docs and a changelog fragment. Closes #19138 Hive-Run: hivecommons/hive#19138 Hive-Plan: #19135 review-rule capture fixes, settings step Hive-Spec: review-feedback-rules#settings Co-Authored-By: Claude Sonnet 5.5 Signed-off-by: Danathar --- ...ed-19138-review-feedback-rules-settings.md | 1 + .../hive/review_feedback_rule_policy_test.go | 51 +++++ src/cmd/hive/review_feedback_wire.go | 34 +++- src/docs/api-reference/configuration.md | 2 + src/docs/operator-reference/README.md | 4 + src/docs/review-feedback-rules.md | 26 ++- src/docs/settings.md | 1 + src/pkg/config/config.go | 13 +- src/pkg/config/review_feedback.go | 160 +++++++++++++++ src/pkg/config/review_feedback_test.go | 102 ++++++++++ src/pkg/dashboard/api.go | 2 + .../api_config_review_feedback_rules.go | 191 ++++++++++++++++++ .../api_config_review_feedback_rules_test.go | 155 ++++++++++++++ .../review_feedback_rules_editor.test.js | 160 +++++++++++++++ .../static-src/js/config-features.js | 8 + .../js/config-review-feedback-rules.js | 159 +++++++++++++++ src/pkg/dashboard/static-src/manifest.txt | 1 + src/pkg/dashboard/static/index.html | 167 +++++++++++++++ src/pkg/reviewrules/classify.go | 24 ++- src/pkg/reviewrules/policy.go | 113 +++++++++++ src/pkg/reviewrules/policy_test.go | 116 +++++++++++ src/pkg/reviewrules/store.go | 25 ++- 22 files changed, 1496 insertions(+), 19 deletions(-) create mode 100644 changelog.d/added-19138-review-feedback-rules-settings.md create mode 100644 src/cmd/hive/review_feedback_rule_policy_test.go create mode 100644 src/pkg/config/review_feedback.go create mode 100644 src/pkg/config/review_feedback_test.go create mode 100644 src/pkg/dashboard/api_config_review_feedback_rules.go create mode 100644 src/pkg/dashboard/api_config_review_feedback_rules_test.go create mode 100644 src/pkg/dashboard/review_feedback_rules_editor.test.js create mode 100644 src/pkg/dashboard/static-src/js/config-review-feedback-rules.js create mode 100644 src/pkg/reviewrules/policy.go create mode 100644 src/pkg/reviewrules/policy_test.go diff --git a/changelog.d/added-19138-review-feedback-rules-settings.md b/changelog.d/added-19138-review-feedback-rules-settings.md new file mode 100644 index 0000000000..b4b2729091 --- /dev/null +++ b/changelog.d/added-19138-review-feedback-rules-settings.md @@ -0,0 +1 @@ +- The review-feedback rule capture now has `review_feedback.rules.*` keys in `hive.yaml` ([#19138](https://github.com/hivecommons/hive/issues/19138), part of [#19135](https://github.com/hivecommons/hive/issues/19135)): `enabled`, `trusted_associations`, `extra_phrases` (your own phrases that mark a reviewer's comment as a rule) and `disabled_signals` (built-in phrases to stop counting), plus an owner-only **Rules from review feedback** card in Settings, Features, Review that edits them in place. A change applies on the next sweep pass without a restart. The keys are additive: the `HIVE_REVIEW_FEEDBACK_RULES` and `HIVE_REVIEW_FEEDBACK_RULE_TRUSTED_ASSOCIATIONS` environment variables keep working and win when set, and the card says so. See [Reviewer-stated rules](src/docs/review-feedback-rules.md#settings). diff --git a/src/cmd/hive/review_feedback_rule_policy_test.go b/src/cmd/hive/review_feedback_rule_policy_test.go new file mode 100644 index 0000000000..c6e23551f0 --- /dev/null +++ b/src/cmd/hive/review_feedback_rule_policy_test.go @@ -0,0 +1,51 @@ +package main + +import ( + "testing" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/reviewrules" +) + +// hivecommons/hive#19138: the sweep reads review_feedback.rules from hive.yaml +// on every pass, so an owner's edit applies without a restart. +func TestReviewRulePolicyFollowsConfigLive(t *testing.T) { + t.Setenv(reviewrules.DisableEnvVar, "") + t.Setenv(reviewrules.TrustedAssociationsEnvVar, "") + cfg := &config.Config{} + if p := reviewRulePolicy(cfg); p.Off || !p.Trusted["COLLABORATOR"] || len(p.ExtraPhrases) != 0 { + t.Fatalf("defaults = %+v", p) + } + off := false + if err := cfg.SetReviewFeedbackRules(config.ReviewFeedbackRulesConfig{ + Enabled: &off, TrustedAssociations: []string{"OWNER"}, ExtraPhrases: []string{"house rule"}, DisabledSignals: []string{"never"}, + }); err != nil { + t.Fatal(err) + } + p := reviewRulePolicy(cfg) + if !p.Off || len(p.Trusted) != 1 || !p.Trusted["OWNER"] || p.ExtraPhrases[0] != "house rule" || !p.DisabledSignals["never"] { + t.Fatalf("after edit = %+v", p) + } + // The environment variables keep working and win. + t.Setenv(reviewrules.DisableEnvVar, "1") + if p := reviewRulePolicy(cfg); p.Off { + t.Fatal("HIVE_REVIEW_FEEDBACK_RULES=1 must keep capture on") + } +} + +// A store that is already open takes the new policy on the next pass. +func TestReviewRuleStoreTakesPolicyWithoutReopen(t *testing.T) { + h := newRuleHarness(t, reviewrules.Config{}) + t.Setenv(reviewrules.DisableEnvVar, "") + t.Setenv(reviewrules.TrustedAssociationsEnvVar, "") + cfg := &config.Config{} + if err := cfg.SetReviewFeedbackRules(config.ReviewFeedbackRulesConfig{ExtraPhrases: []string{"house rule"}}); err != nil { + t.Fatal(err) + } + h.rules.SetPolicy(reviewRulePolicy(cfg)) + h.feedback(ruleItem("inline:7", "OWNER", "House rule: every analysis cites the scorecard.")) + res := h.sweep(t).run(t.Context(), parseReviewFeedbackKey) + if res.RulesCaptured != 1 { + t.Fatalf("captured = %d, want 1 (%+v)", res.RulesCaptured, res) + } +} diff --git a/src/cmd/hive/review_feedback_wire.go b/src/cmd/hive/review_feedback_wire.go index 769ce48340..0dad4d8468 100644 --- a/src/cmd/hive/review_feedback_wire.go +++ b/src/cmd/hive/review_feedback_wire.go @@ -386,17 +386,28 @@ func sweepReviewFeedback(ctx context.Context, cfg *config.Config, client *github } reviewFeedbackStore = st } - if reviewRuleStore == nil && reviewrules.Enabled() { - rs, err := reviewrules.Open(reviewrules.Dir(), reviewrules.VaultDir(), reviewrules.ConfigFromEnv()) + // The rule preferences are read from hive.yaml on every pass, so an owner's + // edit in Settings takes effect on the next pass without a restart + // (hivecommons/hive#19138). The environment variables still win. + rulePolicy := reviewRulePolicy(cfg) + if reviewRuleStore == nil && !rulePolicy.Off { + rcfg := reviewrules.ConfigFromEnv() + rcfg.Trusted = rulePolicy.Trusted + rs, err := reviewrules.Open(reviewrules.Dir(), reviewrules.VaultDir(), rcfg) if err != nil { logger.Warn("review feedback: rule index unreadable or not writable; rules kept in memory only", "dir", reviewrules.Dir(), "error", err) } reviewRuleStore = rs } + var ruleStore *reviewrules.Store if reviewRuleStore != nil { + reviewRuleStore.SetPolicy(rulePolicy) for _, line := range reviewRuleStore.Reconcile(now) { logger.Info("review feedback: rule changed by an operator", "change", line) } + if !rulePolicy.Off { + ruleStore = reviewRuleStore + } } available := agentAvailability(cfg, agentMgr) sweep := reviewFeedbackSweep{ @@ -410,7 +421,7 @@ func sweepReviewFeedback(ctx context.Context, cfg *config.Config, client *github Cursor: reviewFeedbackCursor, Now: now, Logger: logger, - Rules: reviewRuleStore, + Rules: ruleStore, Lane: cfg.BaseAgentName, Managed: cfg.IsManagedRepo, Route: func(owner string) reviewFeedbackRoute { @@ -437,3 +448,20 @@ func sweepReviewFeedback(ctx context.Context, cfg *config.Config, client *github "rules_captured", res.RulesCaptured, "rules_queued", res.RulesQueued, "rules_merged", res.RulesMerged) } } + +// reviewRulePolicy is the effective rule-capture policy: review_feedback.rules +// in hive.yaml merged with the environment variables, which still win. +func reviewRulePolicy(cfg *config.Config) reviewrules.Policy { + return reviewrules.Resolve(reviewRuleSettings(cfg)).Policy +} + +// reviewRuleSettings converts the yaml block to the package's plain settings. +func reviewRuleSettings(cfg *config.Config) reviewrules.Settings { + r := cfg.ReviewFeedbackRules() + return reviewrules.Settings{ + Enabled: r.Enabled, + TrustedAssociations: r.TrustedAssociations, + ExtraPhrases: r.ExtraPhrases, + DisabledSignals: r.DisabledSignals, + } +} diff --git a/src/docs/api-reference/configuration.md b/src/docs/api-reference/configuration.md index 1052e983e1..4eaf8e8996 100644 --- a/src/docs/api-reference/configuration.md +++ b/src/docs/api-reference/configuration.md @@ -106,6 +106,8 @@ Part of the [Dashboard REST API reference](../api-reference.md); see that page f | `PUT` | `/api/config/reference-repos/write-identity` | Owner only | Set `project.remote_write_identity` ([#18516](https://github.com/hivecommons/hive/issues/18516)): body `{"write_identity": "bot"}`, one of `bot`, `bot_only`, `owner`. 400 on an unknown value, or when a hand-written write entry would have no credential under the new value (the message boot exits 22 with); nothing is stored then. 500 when the file could not be written. On success the reference repos are re-resolved before the response, which is the same body as `GET /api/config/reference-repos`; audited as `config_remote_write_identity` with `previous` and `write_identity` | `pkg/dashboard/api.go:563` | | `GET` | `/api/config/repos/parent-autoclose` | Owner only | `repos..parent_autoclose` for every monitored repo: the effective value (`true`, `epics_too`, `trackers_too` or `false`), whether the repo carries its own, the default and the four options the Settings editor offers ([#19070](https://github.com/hivecommons/hive/issues/19070)) | `pkg/dashboard/api.go:560` | | `PUT` | `/api/config/repos/parent-autoclose` | Owner only | Set one monitored repo's parent auto-close policy: `{"repo": "owner/name", "value": "true" \| "epics_too" \| "trackers_too" \| "false"}`. `true` is the default and clears the override. 400 for a missing repo or an invalid value, 404 for a repo that is not monitored, 500 when the file could not be written (previous value restored). Applies without a restart; audited as `config_repo_parent_autoclose`. Parents a person reopened and tracking issues are never auto-closed under any value | `pkg/dashboard/api.go:561` | +| `GET` | `/api/config/review-feedback/rules` | Owner only | `review_feedback.rules.*` ([#19138](https://github.com/hivecommons/hive/issues/19138)): the stored `enabled`, `trusted_associations`, `extra_phrases` and `disabled_signals`, the effective `effective_enabled` and `effective_trusted_associations` after the environment variables, `enabled_from_env` / `trusted_from_env` (an environment variable decides the value), and the vocabulary the Settings card offers (`associations`, `default_trusted_associations`, `signals`) | `pkg/dashboard/api_config_review_feedback_rules.go:102` | +| `PUT` | `/api/config/review-feedback/rules` | Owner only | Update `review_feedback.rules`. Every field is optional and an absent one is left alone: `{"enabled": bool, "trusted_associations": ["OWNER", ...], "extra_phrases": ["..."], "disabled_signals": ["never"]}`; an empty list restores the default or the built-in behaviour. 400 with nothing changed for an unknown association or signal, an extra phrase shorter than 4 or longer than 100 characters, or more than 50 phrases; 500 when the file could not be written (previous values restored). Applies on the next review-feedback sweep pass without a restart; audited as `config_review_feedback_rules`. Responds with the GET body | `pkg/dashboard/api_config_review_feedback_rules.go:103` | | `GET` | `/api/config/prompt-sources` | Owner only | Repo-sourced prompt and definition gate ([#18490](https://github.com/hivecommons/hive/issues/18490)): `repos` (one row per managed repo, then any owner approval whose repo is no longer managed, each `{repo, managed, owner_approved, seed_allowed, allowed, approved_by, approved_at}`), `seed_enabled`, `seed_allowlist`, `trust_notice`, `seed_only_note` | `pkg/dashboard/api.go:572` | | `PUT` | `/api/config/prompt-sources/owner-repos` | Owner only | `{"repo": "org/name", "allowed": true, "acknowledge_trust": true}` approves one of the hive's managed repos as a `prompt_source` / `definition_source` repo; `"allowed": false` revokes. 400 with nothing changed for a repo outside `project.repos` (still needs the seed edit), a malformed slug, or an approval without `acknowledge_trust: true`. Persisted to `variables.security.owner_github_prompt_repos` in the running config; audited as `config_prompt_source_repo`. Responds with the GET body | `pkg/dashboard/api.go:573` | | `GET` | `/api/config/review` | Dashboard auth/session | Review Config Get | `pkg/dashboard/api.go:218` | diff --git a/src/docs/operator-reference/README.md b/src/docs/operator-reference/README.md index af28a9d2d9..47ac1280f9 100644 --- a/src/docs/operator-reference/README.md +++ b/src/docs/operator-reference/README.md @@ -302,6 +302,10 @@ Go callers keep reading `cfg.Agents`, `cfg.Governor.Gateways` and so on through | `governor.kick_limits.max_issues` / `max_prs` | `100` / `50` | Caps on every issue list and every PR list (actionable, stale drafts, merge-eligible, CI-failing) rendered into a kick prompt; a cut list ends with an explicit "… and N more" line. An absent key or a negative value means the default; an explicit `0` opts out of the cap entirely (`KickListUnlimited` — every item is listed), and anything above `500` is pinned to `500`. Editable without touching this file under **Settings → Repos → Kick prompt list caps**. Truncation drops the *newest, lowest-priority* items: issues are listed oldest first, and PRs in review-priority order (fixes, then refactors/docs, then tests, oldest first within each class), so lowering a cap never hides the most urgent work. Raise `max_prs` only if your agents actually work more than 50 PRs per turn; the tail of a long list costs tokens and delivery time on every kick without changing behaviour (#7368). | | `project.agents_md_max_bytes` | `32768` (or env `HIVE_AGENTS_MD_MAX_BYTES`); clamped to `2048`..`262144`. `repos..agents_md_max_bytes` overrides it per repo. | Caps the repo `AGENTS.md` block prepended to a kick. Whole sections (split at `#`/`##` headings; each resolved skill is its own unit) are kept or dropped, never clipped mid-rule; when any are dropped the kick carries a one-line `⚠️ AGENTS.md exceeded N bytes; … — read AGENTS.md …` warning naming them and the scheduler logs it. See [agents-md.md](../agents-md.md#size-cap-whole-sections-visible-warning). | | `repos..parent_autoclose` | `true` (default), `epics_too`, `trackers_too` or `false` | Decomposed-parent auto-close policy for the repo: `true` closes a parent when every child is closed except `epic`/`hold`-labelled issues; `epics_too` also closes those; `trackers_too` does that and also closes tracker/umbrella issues with no sub-issues once everything they reference is finished; `false` never closes a parent. A parent a person reopened after a hive auto-close is never re-closed under any value. Editable in Settings, Repos tab, Parent auto-close (owner only; applies without a restart). See [stage-labels.md](../labels-and-control-signals/stage-labels.md#decomposed-parents--closed-by-their-children-never-stalled). | +| `review_feedback.rules.enabled` | `true` (default) or `false` | Whether the review-feedback sweep records rules reviewers state on the hive's PRs into its knowledge. `false` stops capture; rules already recorded keep loading until retracted. `HIVE_REVIEW_FEEDBACK_RULES`, when set, decides instead. Editable in Settings, Features tab, Review (owner only; applies on the next sweep pass, without a restart). See [Reviewer-stated rules](../review-feedback-rules.md#settings). | +| `review_feedback.rules.trusted_associations` | list of GitHub author associations (`OWNER`, `MEMBER`, `COLLABORATOR`, `CONTRIBUTOR`, ...); empty = `OWNER, MEMBER, COLLABORATOR` | Associations whose rules are captured at once; every other reviewer's rule is queued for a person to approve. `HIVE_REVIEW_FEEDBACK_RULE_TRUSTED_ASSOCIATIONS`, when set, decides instead. | +| `review_feedback.rules.extra_phrases` | list of phrases, 4 to 100 characters each, at most 50 | Extra phrases that mark a comment as a rule, matched case-insensitively as whole words (never as patterns). Quoted lines, code blocks and questions are ignored, as for the built-in phrases. | +| `review_feedback.rules.disabled_signals` | list of built-in signal names | Built-in signals that no longer mark a comment as a rule: `source-of-truth`, `authoritative`, `always`, `never`, `going-forward`, `capture-request`, `assessment-criteria`, `knowledge-base`. | | `project.style` | unset (built-in text applies) | Hive-wide **house style**: a short how-to-change rule list prepended to every kick on every backend, directly under the header. A managed repo's root `HIVE_STYLE.md` (read from `project.checkouts_dir`) overrides it; precedence repo > hive > built-in. Inline text wins over `project.style_file`. See [house-style.md](../house-style.md). | | `project.style_file` | unset | Path on the hive host holding the hive-wide house style; used when `project.style` is empty. | | `project.style_max_bytes` | `4096`; values under `512` are raised to `512`. | Caps each house-style candidate. An oversize candidate is skipped whole (not clipped) and the next source applies; the skip is audited as `house_style_skip`. The repo file is also run through ioscan at the issue-body policy and refused whole on a block. | diff --git a/src/docs/review-feedback-rules.md b/src/docs/review-feedback-rules.md index adfeb1b6e0..2f63649bb5 100644 --- a/src/docs/review-feedback-rules.md +++ b/src/docs/review-feedback-rules.md @@ -15,7 +15,7 @@ leaving it to the agent's discretion ([#19046](https://github.com/hivecommons/hi and `knowledge-base`. Comment text is data: it is matched and quoted, never executed, fetched or followed. 2. **Extract.** The rule text is the sentences that carry a signal, without clauses that only ask for the capture, bounded to 600 characters. 3. **Gate on trust.** A rule changes what every later run is told, so only reviewers whose GitHub association is in - `HIVE_REVIEW_FEEDBACK_RULE_TRUSTED_ASSOCIATIONS` (default `OWNER,MEMBER,COLLABORATOR`) are captured at once. Any other reviewer's rule is + `review_feedback.rules.trusted_associations` (default `OWNER,MEMBER,COLLABORATOR`; see [Settings](#settings)) are captured at once. Any other reviewer's rule is queued as a draft knowledge entry that a person approves. A trusted reviewer restating a queued rule activates it. 4. **Persist.** The rule gets an id (`R-1`, `R-2`, ...) and is written to the `review-rules` local knowledge channel as an entry tagged `pin`, with provenance: the PR, the comment id and URL (the entry's `source`), the reviewer, their association, the time, and whether the hive or @@ -77,10 +77,30 @@ neither kicks nor the dashboard wait for a restart. After a change, the primer r ## Settings +The preferences are `review_feedback.rules.*` keys in `hive.yaml`, and the owner edits them in Settings, **Features** tab, **Review** section, **Rules from review feedback**. A change applies on the sweep's next pass (every five minutes) without a restart. The card is owner-only; every other role sees that it is. + +```yaml +review_feedback: + rules: + enabled: true # false stops capture; the sweep keeps running + trusted_associations: [OWNER, MEMBER, COLLABORATOR] + extra_phrases: ["house rule"] # phrases that also mark a comment as a rule + disabled_signals: [knowledge-base] +``` + +| Key | Default | Meaning | +|---|---|---| +| `review_feedback.rules.enabled` | on | `false` stops rule capture. Rules already recorded keep loading into kicks until retracted. | +| `review_feedback.rules.trusted_associations` | `OWNER,MEMBER,COLLABORATOR` | Associations whose rules are captured at once; others are queued. An empty list is the default. | +| `review_feedback.rules.extra_phrases` | none | Extra phrases that count as a signal named `extra-phrase`. Matched case-insensitively as whole words, never as patterns; 4 to 100 characters, at most 50. | +| `review_feedback.rules.disabled_signals` | none | Built-in signals (`source-of-truth`, `authoritative`, `always`, `never`, `going-forward`, `capture-request`, `assessment-criteria`, `knowledge-base`) that no longer count. | + +The environment variables keep working and win over the yaml keys when they are set; the Settings card says so when one is, and the key is still saved. Extra phrases and disabled signals have no environment variable. + | Variable | Default | Meaning | |---|---|---| -| `HIVE_REVIEW_FEEDBACK_RULES` | on | Set to `0`, `false`, `off` or `no` to stop rule capture. The sweep keeps running. | -| `HIVE_REVIEW_FEEDBACK_RULE_TRUSTED_ASSOCIATIONS` | `OWNER,MEMBER,COLLABORATOR` | Associations whose rules are captured at once; others are queued. | +| `HIVE_REVIEW_FEEDBACK_RULES` | on | Set to `0`, `false`, `off` or `no` to stop rule capture, or to any other value to keep it on whatever `review_feedback.rules.enabled` says. Unlike the yaml key, it also keeps the review-rules channel out of the kick primer. | +| `HIVE_REVIEW_FEEDBACK_RULE_TRUSTED_ASSOCIATIONS` | `OWNER,MEMBER,COLLABORATOR` | Overrides `review_feedback.rules.trusted_associations`. | | `HIVE_REVIEW_FEEDBACK_RULES_MAX` | `200` | Bound on live (active or queued) rules. | | `HIVE_REVIEW_FEEDBACK_RULE_MERGE_THRESHOLD` | `0.8` | Text similarity at which same-scope rules merge. | | `HIVE_REVIEW_FEEDBACK_RULES_VAULT` | `/data/vaults/review-rules` | Knowledge channel the rules are written to. | diff --git a/src/docs/settings.md b/src/docs/settings.md index 22f529e0fb..5e3c67189b 100644 --- a/src/docs/settings.md +++ b/src/docs/settings.md @@ -81,6 +81,7 @@ Optional capabilities, each off or inert until you turn it on. Where a feature h - Spek stage runner: [Spektacular](spektacular.md). - Auto-merge: [App self-merge sweep](operator-reference.md#app-self-merge-sweep-auto_merge), [Trusted bot authors](hive-merge.md#trusted-bot-authors), and [Trusted-author auto-merge](hive-merge.md#trusted-author-auto-merge). - Review gate: [Review swarm](review-swarm.md#configuration), [Blocking line and backlog](review-swarm.md#blocking-line-reviewseverity-reviewbacklog), and [external review bot threads](review-bot-threads.md). +- Rules from review feedback: [Reviewer-stated rules](review-feedback-rules.md#settings). Owner only; the card edits `review_feedback.rules.*` and applies on the next sweep pass. - Trajectory review: [Trajectory-Review Lane](trajectory-review.md#configuration). - Linear agent: [Linear agent integration](linear-agent.md). - Upstream watch: [Upstream watch](upstream-watch.md#configuration). diff --git a/src/pkg/config/config.go b/src/pkg/config/config.go index fc4979e45e..6b7285b513 100644 --- a/src/pkg/config/config.go +++ b/src/pkg/config/config.go @@ -185,11 +185,14 @@ type Config struct { Runs RunsConfig `yaml:"runs,omitempty" json:"runs,omitempty"` // Meetings turns on hive as a live meeting participant through Quorum // (hivecommons/hive#18284). Zero value: off, no Quorum process. - Meetings MeetingsConfig `yaml:"meetings,omitempty" json:"meetings,omitempty"` - Swarm SwarmConfig `yaml:"swarm,omitempty" json:"swarm,omitempty"` - Autonomy AutonomyConfig `yaml:"autonomy,omitempty" json:"autonomy,omitempty"` - Review ReviewConfig `yaml:"review,omitempty" json:"review,omitempty"` - AutoMerge AutoMergeConfig `yaml:"auto_merge,omitempty" json:"auto_merge,omitempty"` + Meetings MeetingsConfig `yaml:"meetings,omitempty" json:"meetings,omitempty"` + Swarm SwarmConfig `yaml:"swarm,omitempty" json:"swarm,omitempty"` + Autonomy AutonomyConfig `yaml:"autonomy,omitempty" json:"autonomy,omitempty"` + Review ReviewConfig `yaml:"review,omitempty" json:"review,omitempty"` + // ReviewFeedback holds the preferences for capturing rules reviewers state + // on the hive'"'"'s PRs (hivecommons/hive#19138). Zero value: the defaults. + ReviewFeedback ReviewFeedbackConfig `yaml:"review_feedback,omitempty" json:"review_feedback,omitempty"` + AutoMerge AutoMergeConfig `yaml:"auto_merge,omitempty" json:"auto_merge,omitempty"` // Decisions tunes how decisions on needs-human items are recorded; its // `delegate` block gates the activity monitor's "Let hive decide" // action (default on). diff --git a/src/pkg/config/review_feedback.go b/src/pkg/config/review_feedback.go new file mode 100644 index 0000000000..157390ccfb --- /dev/null +++ b/src/pkg/config/review_feedback.go @@ -0,0 +1,160 @@ +package config + +import ( + "fmt" + "sort" + "strings" + "sync" +) + +// ReviewFeedbackConfig is the `review_feedback:` block of hive.yaml +// (hivecommons/hive#19138). Its `rules` block holds the preferences for +// capturing reviewer-stated rules into the hive's knowledge +// (src/docs/review-feedback-rules.md). Every key is additive: leave the block +// out and the capture behaves exactly as it did, and the +// HIVE_REVIEW_FEEDBACK_RULES and HIVE_REVIEW_FEEDBACK_RULE_TRUSTED_ASSOCIATIONS +// environment variables keep working. +type ReviewFeedbackConfig struct { + Rules ReviewFeedbackRulesConfig `yaml:"rules,omitempty" json:"rules,omitempty"` +} + +// ReviewFeedbackRulesConfig is `review_feedback.rules`. +type ReviewFeedbackRulesConfig struct { + // Enabled turns reviewer-stated rule capture on or off. Nil keeps the + // default (on). The review-feedback sweep itself is not affected. + Enabled *bool `yaml:"enabled,omitempty" json:"enabled,omitempty"` + // TrustedAssociations are the GitHub author associations whose rules are + // captured at once; every other reviewer's rule is queued for a person. + // Empty keeps OWNER, MEMBER and COLLABORATOR. + TrustedAssociations []string `yaml:"trusted_associations,omitempty" json:"trusted_associations,omitempty"` + // ExtraPhrases are additional phrases that mark a comment as a rule. + // Matching is case-insensitive and on word boundaries. + ExtraPhrases []string `yaml:"extra_phrases,omitempty" json:"extra_phrases,omitempty"` + // DisabledSignals names built-in signals that no longer mark a comment as + // a rule (for example "knowledge-base"). + DisabledSignals []string `yaml:"disabled_signals,omitempty" json:"disabled_signals,omitempty"` +} + +// ReviewFeedbackAssociations are the GitHub author associations a rule's +// reviewer can have. +var ReviewFeedbackAssociations = []string{ + "OWNER", "MEMBER", "COLLABORATOR", "CONTRIBUTOR", "FIRST_TIME_CONTRIBUTOR", "FIRST_TIMER", "MANNEQUIN", "NONE", +} + +// ReviewFeedbackSignals are the built-in signal names disabled_signals may +// name. It mirrors the Signal* constants in pkg/reviewrules, which a test keeps +// in step (pkg/config cannot import it). +var ReviewFeedbackSignals = []string{ + "source-of-truth", "authoritative", "always", "never", "going-forward", "capture-request", "assessment-criteria", "knowledge-base", +} + +const ( + // MaxReviewFeedbackExtraPhrases bounds extra_phrases. + MaxReviewFeedbackExtraPhrases = 50 + // MaxReviewFeedbackPhraseRunes bounds one extra phrase. + MaxReviewFeedbackPhraseRunes = 100 + // MinReviewFeedbackPhraseRunes keeps a phrase specific enough not to match + // ordinary prose. + MinReviewFeedbackPhraseRunes = 4 +) + +var reviewFeedbackMu sync.RWMutex + +// ReviewFeedbackRules returns a copy of review_feedback.rules, safe to read +// while the dashboard edits it. +func (c *Config) ReviewFeedbackRules() ReviewFeedbackRulesConfig { + if c == nil { + return ReviewFeedbackRulesConfig{} + } + reviewFeedbackMu.RLock() + defer reviewFeedbackMu.RUnlock() + r := c.ReviewFeedback.Rules + if r.Enabled != nil { + v := *r.Enabled + r.Enabled = &v + } + r.TrustedAssociations = append([]string(nil), r.TrustedAssociations...) + r.ExtraPhrases = append([]string(nil), r.ExtraPhrases...) + r.DisabledSignals = append([]string(nil), r.DisabledSignals...) + return r +} + +// SetReviewFeedbackRules replaces review_feedback.rules with the normalised, +// validated r. Nothing changes on an error. +func (c *Config) SetReviewFeedbackRules(r ReviewFeedbackRulesConfig) error { + if c == nil { + return fmt.Errorf("config unavailable") + } + norm, err := NormalizeReviewFeedbackRules(r) + if err != nil { + return err + } + reviewFeedbackMu.Lock() + defer reviewFeedbackMu.Unlock() + c.ReviewFeedback.Rules = norm + return nil +} + +// NormalizeReviewFeedbackRules validates r and returns it in canonical form: +// associations and signals upper/lower-cased, sorted and de-duplicated, phrases +// trimmed with inner whitespace collapsed and de-duplicated case-insensitively. +func NormalizeReviewFeedbackRules(r ReviewFeedbackRulesConfig) (ReviewFeedbackRulesConfig, error) { + out := ReviewFeedbackRulesConfig{} + if r.Enabled != nil { + v := *r.Enabled + out.Enabled = &v + } + assocOK := map[string]bool{} + for _, a := range ReviewFeedbackAssociations { + assocOK[a] = true + } + seen := map[string]bool{} + for _, a := range r.TrustedAssociations { + a = strings.ToUpper(strings.TrimSpace(a)) + if a == "" || seen[a] { + continue + } + if !assocOK[a] { + return ReviewFeedbackRulesConfig{}, fmt.Errorf("review_feedback.rules.trusted_associations: %q is not a GitHub author association (one of %s)", a, strings.Join(ReviewFeedbackAssociations, ", ")) + } + seen[a] = true + out.TrustedAssociations = append(out.TrustedAssociations, a) + } + sort.Strings(out.TrustedAssociations) + + sigOK := map[string]bool{} + for _, s := range ReviewFeedbackSignals { + sigOK[s] = true + } + seen = map[string]bool{} + for _, s := range r.DisabledSignals { + s = strings.ToLower(strings.TrimSpace(s)) + if s == "" || seen[s] { + continue + } + if !sigOK[s] { + return ReviewFeedbackRulesConfig{}, fmt.Errorf("review_feedback.rules.disabled_signals: %q is not a built-in signal (one of %s)", s, strings.Join(ReviewFeedbackSignals, ", ")) + } + seen[s] = true + out.DisabledSignals = append(out.DisabledSignals, s) + } + sort.Strings(out.DisabledSignals) + + seen = map[string]bool{} + for _, p := range r.ExtraPhrases { + p = strings.Join(strings.Fields(p), " ") + if p == "" || seen[strings.ToLower(p)] { + continue + } + n := len([]rune(p)) + if n < MinReviewFeedbackPhraseRunes || n > MaxReviewFeedbackPhraseRunes { + return ReviewFeedbackRulesConfig{}, fmt.Errorf("review_feedback.rules.extra_phrases: %q must be %d to %d characters", p, MinReviewFeedbackPhraseRunes, MaxReviewFeedbackPhraseRunes) + } + seen[strings.ToLower(p)] = true + out.ExtraPhrases = append(out.ExtraPhrases, p) + if len(out.ExtraPhrases) > MaxReviewFeedbackExtraPhrases { + return ReviewFeedbackRulesConfig{}, fmt.Errorf("review_feedback.rules.extra_phrases: at most %d phrases", MaxReviewFeedbackExtraPhrases) + } + } + return out, nil +} diff --git a/src/pkg/config/review_feedback_test.go b/src/pkg/config/review_feedback_test.go new file mode 100644 index 0000000000..f2ddaae335 --- /dev/null +++ b/src/pkg/config/review_feedback_test.go @@ -0,0 +1,102 @@ +package config + +import ( + "strings" + "testing" + + "gopkg.in/yaml.v3" +) + +// hivecommons/hive#19138: review_feedback.rules.* in hive.yaml. + +func TestNormalizeReviewFeedbackRules(t *testing.T) { + off := false + got, err := NormalizeReviewFeedbackRules(ReviewFeedbackRulesConfig{ + Enabled: &off, + TrustedAssociations: []string{" member ", "OWNER", "owner", ""}, + ExtraPhrases: []string{" house rule ", "House Rule", "as a rule"}, + DisabledSignals: []string{"Knowledge-Base", "never", "never"}, + }) + if err != nil { + t.Fatal(err) + } + if got.Enabled == nil || *got.Enabled { + t.Fatalf("enabled = %v", got.Enabled) + } + if want := "MEMBER,OWNER"; strings.Join(got.TrustedAssociations, ",") != want { + t.Errorf("trusted = %v, want %s", got.TrustedAssociations, want) + } + if want := "house rule,as a rule"; strings.Join(got.ExtraPhrases, ",") != want { + t.Errorf("phrases = %v, want %s", got.ExtraPhrases, want) + } + if want := "knowledge-base,never"; strings.Join(got.DisabledSignals, ",") != want { + t.Errorf("signals = %v, want %s", got.DisabledSignals, want) + } +} + +func TestNormalizeReviewFeedbackRulesRejects(t *testing.T) { + cases := map[string]ReviewFeedbackRulesConfig{ + "unknown association": {TrustedAssociations: []string{"STRANGER"}}, + "unknown signal": {DisabledSignals: []string{"vibes"}}, + "phrase too short": {ExtraPhrases: []string{"abc"}}, + "phrase too long": {ExtraPhrases: []string{strings.Repeat("x", MaxReviewFeedbackPhraseRunes+1)}}, + } + many := ReviewFeedbackRulesConfig{} + for i := 0; i <= MaxReviewFeedbackExtraPhrases; i++ { + many.ExtraPhrases = append(many.ExtraPhrases, "phrase number "+strings.Repeat("a", i+1)) + } + cases["too many phrases"] = many + for name, in := range cases { + if _, err := NormalizeReviewFeedbackRules(in); err == nil { + t.Errorf("%s: want an error", name) + } + } +} + +func TestSetReviewFeedbackRulesKeepsOldValueOnError(t *testing.T) { + c := &Config{} + if err := c.SetReviewFeedbackRules(ReviewFeedbackRulesConfig{ExtraPhrases: []string{"house rule"}}); err != nil { + t.Fatal(err) + } + if err := c.SetReviewFeedbackRules(ReviewFeedbackRulesConfig{TrustedAssociations: []string{"nope"}}); err == nil { + t.Fatal("want an error") + } + if got := c.ReviewFeedbackRules().ExtraPhrases; len(got) != 1 || got[0] != "house rule" { + t.Fatalf("a refused set changed the config: %v", got) + } + // The getter returns a copy. + r := c.ReviewFeedbackRules() + r.ExtraPhrases[0] = "mutated" + if got := c.ReviewFeedbackRules().ExtraPhrases[0]; got != "house rule" { + t.Fatalf("getter leaked its slice: %q", got) + } +} + +func TestReviewFeedbackYAMLRoundTrip(t *testing.T) { + src := `review_feedback: + rules: + enabled: false + trusted_associations: [OWNER] + extra_phrases: ["house rule"] + disabled_signals: [knowledge-base] +` + var c Config + if err := yaml.Unmarshal([]byte(src), &c); err != nil { + t.Fatal(err) + } + r := c.ReviewFeedbackRules() + if r.Enabled == nil || *r.Enabled || r.TrustedAssociations[0] != "OWNER" || r.ExtraPhrases[0] != "house rule" || r.DisabledSignals[0] != "knowledge-base" { + t.Fatalf("parsed = %+v", r) + } + out, err := yaml.Marshal(c.ReviewFeedback) + if err != nil || !strings.Contains(string(out), "house rule") { + t.Fatalf("marshal: %v %s", err, out) + } + // An absent block stays absent. + empty, _ := yaml.Marshal(struct { + R ReviewFeedbackConfig `yaml:"review_feedback,omitempty"` + }{}) + if strings.Contains(string(empty), "enabled") { + t.Fatalf("zero block wrote keys: %s", empty) + } +} diff --git a/src/pkg/dashboard/api.go b/src/pkg/dashboard/api.go index e1b26dc63d..7ea3ed6d1b 100644 --- a/src/pkg/dashboard/api.go +++ b/src/pkg/dashboard/api.go @@ -577,6 +577,8 @@ func (s *Server) RegisterAPI(deps *Dependencies) { s.registerVersionContainsRoute() // Per-kick outcome record (#18916, api_kick_outcomes.go). s.registerKickOutcomeRoutes() + // review_feedback.rules.* editor (#19138, api_config_review_feedback_rules.go). + s.registerReviewFeedbackRulesRoutes() } var ( diff --git a/src/pkg/dashboard/api_config_review_feedback_rules.go b/src/pkg/dashboard/api_config_review_feedback_rules.go new file mode 100644 index 0000000000..ce5ac05cc8 --- /dev/null +++ b/src/pkg/dashboard/api_config_review_feedback_rules.go @@ -0,0 +1,191 @@ +package dashboard + +import ( + "net/http" + "strconv" + "strings" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/reviewrules" +) + +// The review_feedback.rules.* settings (hivecommons/hive#19138): the +// preferences for capturing rules reviewers state on the hive's PRs into its +// knowledge (src/docs/review-feedback-rules.md), edited on the Settings +// Features tab, Review section. +// +// Both endpoints are OWNER-ONLY: the trusted associations decide whose words +// are pinned into every later kick without a person's approval. A PUT is +// validated before anything changes, applies without a restart (the sweep +// reads hive.yaml on each pass), is audited, and is rolled back when the config +// file cannot be written. The HIVE_REVIEW_FEEDBACK_RULES and +// HIVE_REVIEW_FEEDBACK_RULE_TRUSTED_ASSOCIATIONS variables still win when set; +// the response says so, and the card shows it. + +// reviewFeedbackSignal describes one built-in signal the card can switch off. +type reviewFeedbackSignal struct { + Name string `json:"name"` + Description string `json:"description"` +} + +// reviewFeedbackSignals is the card's vocabulary, in classifier table order. +var reviewFeedbackSignals = []reviewFeedbackSignal{ + {reviewrules.SignalSourceOfTruth, `"source of truth"`}, + {reviewrules.SignalAuthoritative, `"authoritative"`}, + {reviewrules.SignalAlways, `"should always", "must always"`}, + {reviewrules.SignalNever, `"should never", "must never"`}, + {reviewrules.SignalGoingForward, `"going forward", "from now on", "in future", "next time", "for future assessments"`}, + {reviewrules.SignalCaptureRequest, `"capture this", "document this", "record it"`}, + {reviewrules.SignalCriteria, `"assessment criteria", "assessment knowledge"`}, + {reviewrules.SignalKnowledgeBase, `"knowledge base"`}, +} + +// reviewFeedbackRulesResponse is the GET/PUT response body. +type reviewFeedbackRulesResponse struct { + // Enabled, TrustedAssociations, ExtraPhrases and DisabledSignals are the + // values hive.yaml holds (the editable ones). Enabled is true when unset. + Enabled bool `json:"enabled"` + TrustedAssociations []string `json:"trusted_associations"` + ExtraPhrases []string `json:"extra_phrases"` + DisabledSignals []string `json:"disabled_signals"` + // EffectiveEnabled and EffectiveTrustedAssociations are what the sweep + // uses after the environment variables are applied. + EffectiveEnabled bool `json:"effective_enabled"` + EffectiveTrustedAssociations []string `json:"effective_trusted_associations"` + // EnabledFromEnv and TrustedFromEnv say an environment variable decides the + // value, so editing the key has no effect until the variable is removed. + EnabledFromEnv bool `json:"enabled_from_env"` + TrustedFromEnv bool `json:"trusted_from_env"` + // Defaults and vocabulary for the editor. + DefaultTrustedAssociations []string `json:"default_trusted_associations"` + Associations []string `json:"associations"` + Signals []reviewFeedbackSignal `json:"signals"` +} + +func reviewFeedbackRulesResponseFor(cfg *config.Config) reviewFeedbackRulesResponse { + r := cfg.ReviewFeedbackRules() + res := reviewrules.Resolve(reviewrules.Settings{ + Enabled: r.Enabled, + TrustedAssociations: r.TrustedAssociations, + ExtraPhrases: r.ExtraPhrases, + DisabledSignals: r.DisabledSignals, + }) + effTrusted := make([]string, 0, len(res.Policy.Trusted)) + for _, a := range config.ReviewFeedbackAssociations { + if res.Policy.Trusted[a] { + effTrusted = append(effTrusted, a) + } + } + nonNil := func(s []string) []string { + if s == nil { + return []string{} + } + return s + } + return reviewFeedbackRulesResponse{ + Enabled: r.Enabled == nil || *r.Enabled, + TrustedAssociations: nonNil(r.TrustedAssociations), + ExtraPhrases: nonNil(r.ExtraPhrases), + DisabledSignals: nonNil(r.DisabledSignals), + EffectiveEnabled: !res.Policy.Off, + EffectiveTrustedAssociations: effTrusted, + EnabledFromEnv: res.EnabledFromEnv, + TrustedFromEnv: res.TrustedFromEnv, + DefaultTrustedAssociations: strings.Split(reviewrules.DefaultTrustedAssociations, ","), + Associations: config.ReviewFeedbackAssociations, + Signals: reviewFeedbackSignals, + } +} + +// registerReviewFeedbackRulesRoutes mounts the editor's two owner-only routes. +func (s *Server) registerReviewFeedbackRulesRoutes() { + s.mux.HandleFunc("GET /api/config/review-feedback/rules", s.handleReviewFeedbackRulesGet) + s.mux.HandleFunc("PUT /api/config/review-feedback/rules", s.handleReviewFeedbackRulesPut) +} + +// handleReviewFeedbackRulesGet returns the review_feedback.rules settings. +func (s *Server) handleReviewFeedbackRulesGet(w http.ResponseWriter, r *http.Request) { + if !requireOwnerRole(w, r) { + return + } + if s.deps == nil || s.deps.Config == nil { + jsonError(w, "config unavailable", http.StatusServiceUnavailable) + return + } + jsonResponse(w, reviewFeedbackRulesResponseFor(s.deps.Config)) +} + +// handleReviewFeedbackRulesPut updates review_feedback.rules. Every field is +// optional; an absent one is left as it is. Sending an empty list clears it, +// which restores the default (trusted associations) or the built-in behaviour +// (phrases, signals). +func (s *Server) handleReviewFeedbackRulesPut(w http.ResponseWriter, r *http.Request) { + if !requireOwnerRole(w, r) { + return + } + if s.deps == nil || s.deps.Config == nil { + jsonError(w, "config unavailable", http.StatusServiceUnavailable) + return + } + var body struct { + Enabled *bool `json:"enabled"` + TrustedAssociations *[]string `json:"trusted_associations"` + ExtraPhrases *[]string `json:"extra_phrases"` + DisabledSignals *[]string `json:"disabled_signals"` + } + if err := decodeBody(r, &body); err != nil { + jsonError(w, "invalid body", http.StatusBadRequest) + return + } + cfg := s.deps.Config + + // --- validate before mutating anything --- + previous := cfg.ReviewFeedbackRules() + next := previous + if body.Enabled != nil { + if *body.Enabled { + next.Enabled = nil // on is the default; keep the file free of it + } else { + v := false + next.Enabled = &v + } + } + if body.TrustedAssociations != nil { + next.TrustedAssociations = *body.TrustedAssociations + } + if body.ExtraPhrases != nil { + next.ExtraPhrases = *body.ExtraPhrases + } + if body.DisabledSignals != nil { + next.DisabledSignals = *body.DisabledSignals + } + + // --- apply --- + if err := cfg.SetReviewFeedbackRules(next); err != nil { + jsonError(w, err.Error(), http.StatusBadRequest) + return + } + if err := s.saveConfig(); err != nil { + // Memory must not diverge from the file: put the old values back. + _ = cfg.SetReviewFeedbackRules(previous) + if s.logger != nil { + s.logger.Error("failed to persist config after review_feedback.rules update", "error", err) + } + jsonError(w, "failed to save config; review_feedback.rules unchanged", http.StatusInternalServerError) + return + } + cur := cfg.ReviewFeedbackRules() + s.auditFromRequest(r, "config_review_feedback_rules", auditDetail( + "section", "review_feedback", + "enabled", strconv.FormatBool(cur.Enabled == nil || *cur.Enabled), + "trusted_associations", strings.Join(cur.TrustedAssociations, ","), + "extra_phrases", strconv.Itoa(len(cur.ExtraPhrases)), + "disabled_signals", strings.Join(cur.DisabledSignals, ","), + ), "") + if s.logger != nil { + s.logger.Info("review_feedback.rules updated via runtime settings", + "enabled", cur.Enabled == nil || *cur.Enabled, "trusted_associations", cur.TrustedAssociations, + "extra_phrases", len(cur.ExtraPhrases), "disabled_signals", cur.DisabledSignals) + } + jsonResponse(w, reviewFeedbackRulesResponseFor(cfg)) +} diff --git a/src/pkg/dashboard/api_config_review_feedback_rules_test.go b/src/pkg/dashboard/api_config_review_feedback_rules_test.go new file mode 100644 index 0000000000..94587e6b41 --- /dev/null +++ b/src/pkg/dashboard/api_config_review_feedback_rules_test.go @@ -0,0 +1,155 @@ +package dashboard + +import ( + "encoding/json" + "net/http" + "path/filepath" + "strings" + "testing" + + "github.com/hivecommons/hive/pkg/reviewrules" +) + +// hivecommons/hive#19138: the owner-only review_feedback.rules.* editor. + +const reviewFeedbackRulesPath = "/api/config/review-feedback/rules" + +func decodeReviewFeedbackRules(t *testing.T, body []byte) reviewFeedbackRulesResponse { + t.Helper() + var got reviewFeedbackRulesResponse + if err := json.Unmarshal(body, &got); err != nil { + t.Fatalf("decode: %v (%s)", err, body) + } + return got +} + +func clearRuleEnv(t *testing.T) { + t.Helper() + t.Setenv(reviewrules.DisableEnvVar, "") + t.Setenv(reviewrules.TrustedAssociationsEnvVar, "") +} + +func TestReviewFeedbackRules_OwnerOnly(t *testing.T) { + clearRuleEnv(t) + s := covApiServer(t) + if rec := doGetNoRole(s, reviewFeedbackRulesPath); rec.Code != http.StatusForbidden { + t.Fatalf("non-owner GET: want 403, got %d", rec.Code) + } + if rec := doPutNoRole(s, reviewFeedbackRulesPath, `{"enabled":false}`); rec.Code != http.StatusForbidden { + t.Fatalf("non-owner PUT: want 403, got %d", rec.Code) + } + if r := s.deps.Config.ReviewFeedbackRules(); r.Enabled != nil { + t.Fatalf("a refused PUT changed the config: %+v", r) + } +} + +func TestReviewFeedbackRules_GetDefaults(t *testing.T) { + clearRuleEnv(t) + s := covApiServer(t) + rec := doOwnerGet(s, reviewFeedbackRulesPath) + if rec.Code != http.StatusOK { + t.Fatalf("GET: %d %s", rec.Code, rec.Body.String()) + } + got := decodeReviewFeedbackRules(t, rec.Body.Bytes()) + if !got.Enabled || !got.EffectiveEnabled || got.EnabledFromEnv || got.TrustedFromEnv { + t.Fatalf("defaults = %+v", got) + } + if strings.Join(got.EffectiveTrustedAssociations, ",") != "OWNER,MEMBER,COLLABORATOR" { + t.Fatalf("effective trusted = %v", got.EffectiveTrustedAssociations) + } + if len(got.Signals) != len(reviewrules.SignalNames()) || got.ExtraPhrases == nil || got.DisabledSignals == nil || got.TrustedAssociations == nil { + t.Fatalf("vocabulary or empty lists missing: %+v", got) + } + for i, n := range reviewrules.SignalNames() { + if got.Signals[i].Name != n { + t.Errorf("signal %d = %q, want %q", i, got.Signals[i].Name, n) + } + } +} + +func TestReviewFeedbackRules_PutAppliesLiveAndAudits(t *testing.T) { + clearRuleEnv(t) + s := covApiServer(t) + cfg := s.deps.Config + cfg.SourcePath = filepath.Join(t.TempDir(), "hive.yaml") + rec := doPut(s, reviewFeedbackRulesPath, map[string]any{ + "enabled": false, + "trusted_associations": []string{"owner"}, + "extra_phrases": []string{"house rule"}, + "disabled_signals": []string{"knowledge-base"}, + }) + if rec.Code != http.StatusOK { + t.Fatalf("PUT: %d %s", rec.Code, rec.Body.String()) + } + live := cfg.ReviewFeedbackRules() + if live.Enabled == nil || *live.Enabled || live.TrustedAssociations[0] != "OWNER" || live.ExtraPhrases[0] != "house rule" || live.DisabledSignals[0] != "knowledge-base" { + t.Fatalf("live config = %+v", live) + } + got := decodeReviewFeedbackRules(t, rec.Body.Bytes()) + if got.EffectiveEnabled || strings.Join(got.EffectiveTrustedAssociations, ",") != "OWNER" { + t.Fatalf("response = %+v", got) + } + found := false + if s.audit != nil { + for _, e := range s.audit.Recent(50) { + found = found || e.Action == "config_review_feedback_rules" + } + if !found { + t.Error("missing config_review_feedback_rules audit") + } + } + + // Absent fields are left alone; turning capture back on clears the key. + rec = doPut(s, reviewFeedbackRulesPath, map[string]any{"enabled": true}) + if rec.Code != http.StatusOK { + t.Fatalf("PUT enabled: %d %s", rec.Code, rec.Body.String()) + } + live = cfg.ReviewFeedbackRules() + if live.Enabled != nil || live.ExtraPhrases[0] != "house rule" { + t.Fatalf("partial PUT changed other fields or left enabled set: %+v", live) + } + // An empty list restores the built-in behaviour. + if rec = doPut(s, reviewFeedbackRulesPath, map[string]any{"trusted_associations": []string{}, "extra_phrases": []string{}}); rec.Code != http.StatusOK { + t.Fatalf("PUT clear: %d %s", rec.Code, rec.Body.String()) + } + if live = cfg.ReviewFeedbackRules(); len(live.TrustedAssociations) != 0 || len(live.ExtraPhrases) != 0 { + t.Fatalf("clear left %+v", live) + } +} + +func TestReviewFeedbackRules_PutValidation(t *testing.T) { + clearRuleEnv(t) + s := covApiServer(t) + for name, body := range map[string]map[string]any{ + "unknown association": {"trusted_associations": []string{"STRANGER"}}, + "unknown signal": {"disabled_signals": []string{"vibes"}}, + "short phrase": {"extra_phrases": []string{"abc"}}, + "valid next to bad": {"extra_phrases": []string{"house rule"}, "disabled_signals": []string{"vibes"}}, + } { + if rec := doPut(s, reviewFeedbackRulesPath, body); rec.Code != http.StatusBadRequest { + t.Errorf("%s: code %d, want 400 (%s)", name, rec.Code, rec.Body.String()) + } + } + if r := s.deps.Config.ReviewFeedbackRules(); len(r.ExtraPhrases) != 0 || len(r.DisabledSignals) != 0 { + t.Fatalf("a refused PUT stored %+v", r) + } +} + +func TestReviewFeedbackRules_EnvStillWinsAndIsReported(t *testing.T) { + t.Setenv(reviewrules.DisableEnvVar, "off") + t.Setenv(reviewrules.TrustedAssociationsEnvVar, "MEMBER") + s := covApiServer(t) + got := decodeReviewFeedbackRules(t, doOwnerGet(s, reviewFeedbackRulesPath).Body.Bytes()) + if got.EffectiveEnabled || !got.EnabledFromEnv || !got.TrustedFromEnv || strings.Join(got.EffectiveTrustedAssociations, ",") != "MEMBER" { + t.Fatalf("env not reported: %+v", got) + } + // The yaml value is still editable and reported as stored. + rec := doPut(s, reviewFeedbackRulesPath, map[string]any{"trusted_associations": []string{"OWNER"}}) + if rec.Code != http.StatusOK { + t.Fatalf("PUT: %d %s", rec.Code, rec.Body.String()) + } + got = decodeReviewFeedbackRules(t, rec.Body.Bytes()) + if strings.Join(got.TrustedAssociations, ",") != "OWNER" || strings.Join(got.EffectiveTrustedAssociations, ",") != "MEMBER" { + t.Fatalf("stored vs effective = %+v", got) + } +} diff --git a/src/pkg/dashboard/review_feedback_rules_editor.test.js b/src/pkg/dashboard/review_feedback_rules_editor.test.js new file mode 100644 index 0000000000..695183bfe7 --- /dev/null +++ b/src/pkg/dashboard/review_feedback_rules_editor.test.js @@ -0,0 +1,160 @@ +// Settings -> Features -> Review -> Rules from review feedback +// (hivecommons/hive#19138): renderReviewFeedbackRulesPanel paints the enabled +// switch, the trusted associations, the extra phrases and the built-in signals, +// and Save PUTs them to /api/config/review-feedback/rules. An environment +// override is called out, a server refusal is shown verbatim and the edits are +// kept. +// Run: node --test src/pkg/dashboard/*.test.js +const test = require('node:test'); +const assert = require('node:assert'); +const fs = require('node:fs'); +const path = require('node:path'); + +const html = fs.readFileSync(path.join(__dirname, 'static', 'index.html'), 'utf8'); +const ASYNC_PREFIX = 'async '; +const fnSrc = name => { + let start = html.indexOf('function ' + name + '('); + assert.ok(start >= 0, name + ' not found'); + if (html.slice(start - ASYNC_PREFIX.length, start) === ASYNC_PREFIX) start -= ASYNC_PREFIX.length; + let depth = 0; + for (let i = html.indexOf('{', start); i < html.length; i++) { + if (html[i] === '{') depth++; + else if (html[i] === '}' && --depth === 0) return html.slice(start, i + 1); + } + throw new Error('unbalanced ' + name); +}; + +// Minimal fake DOM: elements with children, className, textContent (which +// clears children, as in the browser), attributes and listeners. +const makeDoc = () => { + const doc = {}; + const makeEl = tag => { + const el = { tag, children: [], listeners: {}, className: '', title: '', _text: '' }; + Object.defineProperty(el, 'textContent', { + get() { return el._text + el.children.map(c => c.textContent).join(''); }, + set(v) { el._text = String(v); el.children = []; }, + }); + el.appendChild = c => { el.children.push(c); return c; }; + el.setAttribute = () => {}; + el.addEventListener = (type, fn) => { (el.listeners[type] = el.listeners[type] || []).push(fn); }; + el.fire = (type, ev) => (el.listeners[type] || []).map(fn => fn(ev || {})); + el.find = pred => { + const out = []; + const walk = n => { if (pred(n)) out.push(n); (n.children || []).forEach(walk); }; + walk(el); + return out; + }; + return el; + }; + doc.createElement = makeEl; + doc.createTextNode = text => ({ tag: '#text', children: [], className: '', textContent: String(text) }); + doc.getElementById = () => null; + return doc; +}; + + + +const DATA = { + enabled: true, + trusted_associations: [], + extra_phrases: ['house rule'], + disabled_signals: ['knowledge-base'], + effective_enabled: true, + effective_trusted_associations: ['OWNER', 'MEMBER', 'COLLABORATOR'], + enabled_from_env: false, + trusted_from_env: false, + default_trusted_associations: ['OWNER', 'MEMBER', 'COLLABORATOR'], + associations: ['OWNER', 'MEMBER', 'COLLABORATOR', 'CONTRIBUTOR'], + signals: [{ name: 'never', description: '"should never"' }, { name: 'knowledge-base', description: '"knowledge base"' }], +}; + +const setup = ({ data = DATA, put }) => { + const doc = makeDoc(); + const toasts = []; + const calls = []; + const fetchStub = async (url, opts) => { + calls.push({ url, opts, body: JSON.parse(opts.body) }); + return put(calls[calls.length - 1]); + }; + const render = new Function('document', 'fetch', 'showToast', '_inceptionAuthHeaders', 'saveErrorMessage', + "const REVIEW_FEEDBACK_RULES_URL = '/api/config/review-feedback/rules';\n" + + fnSrc('rfrNode') + '\n' + fnSrc('reviewFeedbackRulesPhrases') + '\n' + fnSrc('renderReviewFeedbackRulesPanel') + '\nreturn renderReviewFeedbackRulesPanel;')( + doc, fetchStub, (m, k) => toasts.push([m, k]), () => ({}), async res => res.errorMessage); + const host = doc.createElement('div'); + render(host, data); + return { host, toasts, calls }; +}; + +const boxes = host => host.find(n => n.tag === 'input'); +const boxFor = (host, label) => host.find(n => n.tag === 'label').find(l => l.textContent.startsWith(label)).find(n => n.tag === 'input')[0]; +const flip = (box, checked) => { box.checked = checked; box.fire('change'); }; +const saveButton = host => host.find(n => n.tag === 'button' && n.textContent === 'Save review-feedback rules')[0]; +const textarea = host => host.find(n => n.tag === 'textarea')[0]; +const ok = json => ({ ok: true, json: async () => json }); + +test('shows the enabled switch, the default trusted set, the phrases and the signals', () => { + const { host } = setup({ put: () => { throw new Error('no save'); } }); + assert.strictEqual(boxFor(host, 'Capture rules').checked, true); + assert.deepStrictEqual(['OWNER', 'MEMBER', 'COLLABORATOR', 'CONTRIBUTOR'].map(a => boxFor(host, a).checked), [true, true, true, false]); + assert.strictEqual(textarea(host).value, 'house rule'); + assert.strictEqual(boxFor(host, 'never').checked, true); + assert.strictEqual(boxFor(host, 'knowledge-base').checked, false); + assert.ok(host.textContent.includes('review_feedback.rules.enabled')); + assert.strictEqual(saveButton(host).disabled, true); +}); + +test('environment overrides are called out', () => { + const { host } = setup({ + data: Object.assign({}, DATA, { enabled_from_env: true, effective_enabled: false, trusted_from_env: true, effective_trusted_associations: ['MEMBER'] }), + put: () => { throw new Error('no save'); }, + }); + assert.match(host.textContent, /HIVE_REVIEW_FEEDBACK_RULES environment variable is set.*currently off/); + assert.match(host.textContent, /HIVE_REVIEW_FEEDBACK_RULE_TRUSTED_ASSOCIATIONS environment variable is set \(currently MEMBER\)/); +}); + +test('Save PUTs the edited values; the default trusted set is sent as an empty list', async () => { + const { host, calls, toasts } = setup({ put: () => ok(Object.assign({}, DATA, { enabled: false })) }); + flip(boxFor(host, 'Capture rules'), false); + flip(boxFor(host, 'never'), false); + textarea(host).value = ' house rule \n\nas a rule\n'; + textarea(host).fire('input'); + assert.strictEqual(saveButton(host).disabled, false); + await saveButton(host).fire('click')[0]; + assert.strictEqual(calls.length, 1); + assert.strictEqual(calls[0].url, '/api/config/review-feedback/rules'); + assert.strictEqual(calls[0].opts.method, 'PUT'); + assert.deepStrictEqual(calls[0].body, { + enabled: false, + trusted_associations: [], + extra_phrases: ['house rule', 'as a rule'], + disabled_signals: ['knowledge-base', 'never'].sort(), + }); + assert.deepStrictEqual(toasts, [['Review-feedback rules saved', 'success']]); +}); + +test('a changed trusted set is sent as chosen', async () => { + const { host, calls } = setup({ put: () => ok(DATA) }); + flip(boxFor(host, 'COLLABORATOR'), false); + flip(boxFor(host, 'CONTRIBUTOR'), true); + await saveButton(host).fire('click')[0]; + assert.deepStrictEqual(calls[0].body.trusted_associations.sort(), ['CONTRIBUTOR', 'MEMBER', 'OWNER']); +}); + +test('unticking every association asks instead of silently restoring the default', async () => { + const { host, calls } = setup({ put: () => { throw new Error('no save'); } }); + ['OWNER', 'MEMBER', 'COLLABORATOR'].forEach(a => flip(boxFor(host, a), false)); + await saveButton(host).fire('click'); + assert.strictEqual(calls.length, 0); + assert.match(host.textContent, /Tick at least one association/); +}); + +test('a refusal is shown as-is and the edits are kept', async () => { + const { host, toasts } = setup({ put: () => ({ ok: false, status: 400, errorMessage: 'extra_phrases: "abc" must be 4 to 100 characters' }) }); + textarea(host).value = 'abc'; + textarea(host).fire('input'); + await saveButton(host).fire('click')[0]; + assert.deepStrictEqual(toasts, []); + assert.match(host.textContent, /must be 4 to 100 characters/); + assert.strictEqual(textarea(host).value, 'abc'); + assert.strictEqual(saveButton(host).disabled, false); +}); diff --git a/src/pkg/dashboard/static-src/js/config-features.js b/src/pkg/dashboard/static-src/js/config-features.js index 7586a67611..414ac9582b 100644 --- a/src/pkg/dashboard/static-src/js/config-features.js +++ b/src/pkg/dashboard/static-src/js/config-features.js @@ -139,6 +139,9 @@ // into FEATURES_SECTIONS; keep new controls inside the section they belong // to rather than appending to the end of the template. function renderGovFeatures(f, am, rv) { + /* Review feedback rules (#19138): painted asynchronously from their own + owner-only endpoint, like the Repos tab's parent auto-close. */ + loadReviewFeedbackRules(); f = f || {}; am = am || {}; rv = rv || {}; @@ -660,6 +663,11 @@
Look this hash up in the "Contributing hives" table at hivecommons.dev/benchmarks to verify your records arrived${f.benchmarksLastPushAt ? ` · last pushed ${esc(f.benchmarksLastPushAt)} (${esc(String(f.benchmarksLastPushCount || 0))} records)` : ' · nothing pushed yet'}.
+
+
Rules from review feedback ${yamlHint('review_feedback.rules')} ${settingHelpMark('review-feedback-rules', 'settings', 'Rules from review feedback', `When a person reviewing a hive PR states a rule ("X is the source of truth", "going forward ..."), the hive records it in its knowledge for later work. Choose whether it does, whose rules count at once (every other reviewer's rule waits for approval), extra phrases that mark a comment as a rule, and built-in signals to ignore. Owner only. Applies on the next sweep pass, without a restart.`)}
+
Loading…
+
+
Review evidence signing
Read-only here: signing keys are provisioned on disk and set in hive.yaml.
diff --git a/src/pkg/dashboard/static-src/js/config-review-feedback-rules.js b/src/pkg/dashboard/static-src/js/config-review-feedback-rules.js new file mode 100644 index 0000000000..1cfe5df009 --- /dev/null +++ b/src/pkg/dashboard/static-src/js/config-review-feedback-rules.js @@ -0,0 +1,159 @@ + /* Review feedback rules editor (#19138): review_feedback.rules.*, loaded + from the owner-only /api/config/review-feedback/rules endpoint and saved + back to it. Rendered with DOM nodes and textContent only, so a phrase or + a server string is never parsed as markup. The server validates the + values and applies them without a restart; its error message is shown + as-is and nothing is saved on an error. */ + const REVIEW_FEEDBACK_RULES_HOST_ID = 'cfg-review-feedback-rules-host'; + const REVIEW_FEEDBACK_RULES_URL = '/api/config/review-feedback/rules'; + + function rfrNode(tag, className, text) { + const node = document.createElement(tag); + if (className) node.className = className; + if (text !== undefined) node.textContent = text; + return node; + } + + async function loadReviewFeedbackRules() { + let host = null; + try { + const r = await fetch(REVIEW_FEEDBACK_RULES_URL); + host = document.getElementById(REVIEW_FEEDBACK_RULES_HOST_ID); + if (!host) return; + if (r.status === 403) { + host.textContent = ''; + host.appendChild(rfrNode('div', 'fs-sm text-muted', 'Only the hive owner can view and edit review-feedback rules.')); + return; + } + if (!r.ok) throw new Error('HTTP ' + r.status); + renderReviewFeedbackRulesPanel(host, await r.json()); + } catch (e) { + host = host || document.getElementById(REVIEW_FEEDBACK_RULES_HOST_ID); + if (!host) return; + host.textContent = ''; + host.appendChild(rfrNode('div', 'fs-sm text-danger', 'Failed to load review-feedback rules: ' + e.message)); + } + } + + /* reviewFeedbackRulesPhrases turns the textarea into the phrase list: one + phrase per line, blank lines dropped. */ + function reviewFeedbackRulesPhrases(text) { + return String(text || '').split('\n').map(s => s.trim()).filter(Boolean); + } + + function renderReviewFeedbackRulesPanel(host, data) { + if (!host) return; + data = data || {}; + const associations = Array.isArray(data.associations) ? data.associations : []; + const signals = Array.isArray(data.signals) ? data.signals : []; + const defaults = Array.isArray(data.default_trusted_associations) ? data.default_trusted_associations : []; + /* An empty stored trusted list means "the default", so the boxes show the + default set ticked; saving that set back sends an empty list. */ + const storedTrusted = Array.isArray(data.trusted_associations) ? data.trusted_associations : []; + const state = { + enabled: data.enabled !== false, + trusted: new Set(storedTrusted.length ? storedTrusted : defaults), + phrases: (Array.isArray(data.extra_phrases) ? data.extra_phrases : []).join('\n'), + disabled: new Set(Array.isArray(data.disabled_signals) ? data.disabled_signals : []), + }; + const saved = JSON.stringify([state.enabled, [...state.trusted].sort(), state.phrases, [...state.disabled].sort()]); + const snapshot = () => JSON.stringify([state.enabled, [...state.trusted].sort(), state.phrases, [...state.disabled].sort()]); + host.textContent = ''; + host.appendChild(rfrNode('p', 'config-help', + 'When someone reviewing a hive PR states a rule for later work, such as "X is the source of truth" or "going forward, ...", the hive records it in its knowledge and tells the reviewer on the PR. ' + + 'A rule from a reviewer whose GitHub association is not ticked below waits for a person to approve it. ' + + 'Saving applies the change on the next sweep pass, without a restart.')); + if (data.enabled_from_env) { + host.appendChild(rfrNode('p', 'config-help text-danger', + 'The HIVE_REVIEW_FEEDBACK_RULES environment variable is set, so it decides whether capture runs (currently ' + (data.effective_enabled ? 'on' : 'off') + '); the switch below is saved but has no effect until the variable is removed.')); + } + if (data.trusted_from_env) { + host.appendChild(rfrNode('p', 'config-help text-danger', + 'The HIVE_REVIEW_FEEDBACK_RULE_TRUSTED_ASSOCIATIONS environment variable is set (currently ' + (data.effective_trusted_associations || []).join(', ') + '), so it decides whose rules count at once; the boxes below are saved but have no effect until the variable is removed.')); + } + + const statusEl = rfrNode('div', 'fs-sm'); + const setStatus = (msg, cls) => { statusEl.textContent = msg; statusEl.className = 'fs-sm ' + (cls || 'text-muted'); }; + const saveBtn = rfrNode('button', 'hv-btn btn-primary btn-sm', 'Save review-feedback rules'); + const syncSave = () => { saveBtn.disabled = snapshot() === saved; }; + + const checkRow = (label, hint, checked, onChange) => { + const row = rfrNode('label', 'config-toggle'); + const box = document.createElement('input'); + box.type = 'checkbox'; + box.checked = checked; + box.addEventListener('change', () => { onChange(box.checked); setStatus(''); syncSave(); }); + row.appendChild(box); + const text = rfrNode('span', 'config-toggle-label', label); + if (hint) text.appendChild(rfrNode('span', 'text-muted fs-sm', ' ' + hint)); + row.appendChild(text); + return row; + }; + + host.appendChild(checkRow('Capture rules reviewers state', '', state.enabled, v => { state.enabled = v; })); + + host.appendChild(rfrNode('div', 'config-subgroup', 'Rules count at once from')); + const trustedList = rfrNode('div', 'config-list'); + associations.forEach(a => { + trustedList.appendChild(checkRow(a, '', state.trusted.has(a), v => { if (v) state.trusted.add(a); else state.trusted.delete(a); })); + }); + host.appendChild(trustedList); + + host.appendChild(rfrNode('div', 'config-subgroup', 'Extra phrases that mark a comment as a rule (one per line)')); + const area = document.createElement('textarea'); + area.rows = 3; + area.value = state.phrases; + area.placeholder = 'e.g. house rule'; + area.spellcheck = false; + area.setAttribute('aria-label', 'Extra phrases that mark a comment as a rule'); + area.addEventListener('input', () => { state.phrases = area.value; setStatus(''); syncSave(); }); + host.appendChild(area); + host.appendChild(rfrNode('div', 'config-help', 'Matched case-insensitively as whole words, never as patterns. Quoted lines, code blocks and questions are ignored.')); + + host.appendChild(rfrNode('div', 'config-subgroup', 'Built-in signals (untick to stop one from counting)')); + const signalList = rfrNode('div', 'config-list'); + signals.forEach(sig => { + signalList.appendChild(checkRow(sig.name, sig.description, !state.disabled.has(sig.name), v => { if (v) state.disabled.delete(sig.name); else state.disabled.add(sig.name); })); + }); + host.appendChild(signalList); + + const hints = rfrNode('div', 'config-help'); + const hint = rfrNode('span', 'settings-yaml-hint', 'YAML '); + hint.appendChild(rfrNode('code', '', 'review_feedback.rules.enabled | trusted_associations | extra_phrases | disabled_signals')); + hints.appendChild(hint); + host.appendChild(hints); + + const actions = rfrNode('div', 'config-list-add'); + actions.appendChild(saveBtn); + actions.appendChild(statusEl); + host.appendChild(actions); + syncSave(); + + saveBtn.addEventListener('click', async () => { + const trusted = [...state.trusted].filter(a => associations.includes(a)); + /* An empty list means "the default" to the server, so an owner who + unticked everything would silently get it back. Ask instead. */ + if (!trusted.length) { setStatus('Tick at least one association, or keep the default set.', 'text-danger'); return; } + saveBtn.disabled = true; + setStatus('Saving…'); + const sameAsDefault = trusted.length === defaults.length && defaults.every(a => state.trusted.has(a)); + try { + const res = await fetch(REVIEW_FEEDBACK_RULES_URL, { + method: 'PUT', + headers: _inceptionAuthHeaders(), + body: JSON.stringify({ + enabled: state.enabled, + trusted_associations: sameAsDefault ? [] : trusted, + extra_phrases: reviewFeedbackRulesPhrases(state.phrases), + disabled_signals: [...state.disabled], + }), + }); + if (!res.ok) throw new Error(await saveErrorMessage(res)); + renderReviewFeedbackRulesPanel(host, await res.json()); + showToast('Review-feedback rules saved', 'success'); + } catch (e) { + setStatus(e.message, 'text-danger'); + syncSave(); + } + }); + } diff --git a/src/pkg/dashboard/static-src/manifest.txt b/src/pkg/dashboard/static-src/manifest.txt index 1eb9156157..dac6cfc2e2 100644 --- a/src/pkg/dashboard/static-src/manifest.txt +++ b/src/pkg/dashboard/static-src/manifest.txt @@ -176,6 +176,7 @@ js/config-hub.js js/config-agent.js js/config-governor.js js/config-parent-autoclose.js +js/config-review-feedback-rules.js js/config-workflows.js js/config-workflows-handoff.js js/config-workflows-library.js diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index 2d835bca94..8d50060a97 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -73677,6 +73677,165 @@

Agent Watchdog

syncSave(); }); } + /* Review feedback rules editor (#19138): review_feedback.rules.*, loaded + from the owner-only /api/config/review-feedback/rules endpoint and saved + back to it. Rendered with DOM nodes and textContent only, so a phrase or + a server string is never parsed as markup. The server validates the + values and applies them without a restart; its error message is shown + as-is and nothing is saved on an error. */ + const REVIEW_FEEDBACK_RULES_HOST_ID = 'cfg-review-feedback-rules-host'; + const REVIEW_FEEDBACK_RULES_URL = '/api/config/review-feedback/rules'; + + function rfrNode(tag, className, text) { + const node = document.createElement(tag); + if (className) node.className = className; + if (text !== undefined) node.textContent = text; + return node; + } + + async function loadReviewFeedbackRules() { + let host = null; + try { + const r = await fetch(REVIEW_FEEDBACK_RULES_URL); + host = document.getElementById(REVIEW_FEEDBACK_RULES_HOST_ID); + if (!host) return; + if (r.status === 403) { + host.textContent = ''; + host.appendChild(rfrNode('div', 'fs-sm text-muted', 'Only the hive owner can view and edit review-feedback rules.')); + return; + } + if (!r.ok) throw new Error('HTTP ' + r.status); + renderReviewFeedbackRulesPanel(host, await r.json()); + } catch (e) { + host = host || document.getElementById(REVIEW_FEEDBACK_RULES_HOST_ID); + if (!host) return; + host.textContent = ''; + host.appendChild(rfrNode('div', 'fs-sm text-danger', 'Failed to load review-feedback rules: ' + e.message)); + } + } + + /* reviewFeedbackRulesPhrases turns the textarea into the phrase list: one + phrase per line, blank lines dropped. */ + function reviewFeedbackRulesPhrases(text) { + return String(text || '').split('\n').map(s => s.trim()).filter(Boolean); + } + + function renderReviewFeedbackRulesPanel(host, data) { + if (!host) return; + data = data || {}; + const associations = Array.isArray(data.associations) ? data.associations : []; + const signals = Array.isArray(data.signals) ? data.signals : []; + const defaults = Array.isArray(data.default_trusted_associations) ? data.default_trusted_associations : []; + /* An empty stored trusted list means "the default", so the boxes show the + default set ticked; saving that set back sends an empty list. */ + const storedTrusted = Array.isArray(data.trusted_associations) ? data.trusted_associations : []; + const state = { + enabled: data.enabled !== false, + trusted: new Set(storedTrusted.length ? storedTrusted : defaults), + phrases: (Array.isArray(data.extra_phrases) ? data.extra_phrases : []).join('\n'), + disabled: new Set(Array.isArray(data.disabled_signals) ? data.disabled_signals : []), + }; + const saved = JSON.stringify([state.enabled, [...state.trusted].sort(), state.phrases, [...state.disabled].sort()]); + const snapshot = () => JSON.stringify([state.enabled, [...state.trusted].sort(), state.phrases, [...state.disabled].sort()]); + host.textContent = ''; + host.appendChild(rfrNode('p', 'config-help', + 'When someone reviewing a hive PR states a rule for later work, such as "X is the source of truth" or "going forward, ...", the hive records it in its knowledge and tells the reviewer on the PR. ' + + 'A rule from a reviewer whose GitHub association is not ticked below waits for a person to approve it. ' + + 'Saving applies the change on the next sweep pass, without a restart.')); + if (data.enabled_from_env) { + host.appendChild(rfrNode('p', 'config-help text-danger', + 'The HIVE_REVIEW_FEEDBACK_RULES environment variable is set, so it decides whether capture runs (currently ' + (data.effective_enabled ? 'on' : 'off') + '); the switch below is saved but has no effect until the variable is removed.')); + } + if (data.trusted_from_env) { + host.appendChild(rfrNode('p', 'config-help text-danger', + 'The HIVE_REVIEW_FEEDBACK_RULE_TRUSTED_ASSOCIATIONS environment variable is set (currently ' + (data.effective_trusted_associations || []).join(', ') + '), so it decides whose rules count at once; the boxes below are saved but have no effect until the variable is removed.')); + } + + const statusEl = rfrNode('div', 'fs-sm'); + const setStatus = (msg, cls) => { statusEl.textContent = msg; statusEl.className = 'fs-sm ' + (cls || 'text-muted'); }; + const saveBtn = rfrNode('button', 'hv-btn btn-primary btn-sm', 'Save review-feedback rules'); + const syncSave = () => { saveBtn.disabled = snapshot() === saved; }; + + const checkRow = (label, hint, checked, onChange) => { + const row = rfrNode('label', 'config-toggle'); + const box = document.createElement('input'); + box.type = 'checkbox'; + box.checked = checked; + box.addEventListener('change', () => { onChange(box.checked); setStatus(''); syncSave(); }); + row.appendChild(box); + const text = rfrNode('span', 'config-toggle-label', label); + if (hint) text.appendChild(rfrNode('span', 'text-muted fs-sm', ' ' + hint)); + row.appendChild(text); + return row; + }; + + host.appendChild(checkRow('Capture rules reviewers state', '', state.enabled, v => { state.enabled = v; })); + + host.appendChild(rfrNode('div', 'config-subgroup', 'Rules count at once from')); + const trustedList = rfrNode('div', 'config-list'); + associations.forEach(a => { + trustedList.appendChild(checkRow(a, '', state.trusted.has(a), v => { if (v) state.trusted.add(a); else state.trusted.delete(a); })); + }); + host.appendChild(trustedList); + + host.appendChild(rfrNode('div', 'config-subgroup', 'Extra phrases that mark a comment as a rule (one per line)')); + const area = document.createElement('textarea'); + area.rows = 3; + area.value = state.phrases; + area.placeholder = 'e.g. house rule'; + area.spellcheck = false; + area.setAttribute('aria-label', 'Extra phrases that mark a comment as a rule'); + area.addEventListener('input', () => { state.phrases = area.value; setStatus(''); syncSave(); }); + host.appendChild(area); + host.appendChild(rfrNode('div', 'config-help', 'Matched case-insensitively as whole words, never as patterns. Quoted lines, code blocks and questions are ignored.')); + + host.appendChild(rfrNode('div', 'config-subgroup', 'Built-in signals (untick to stop one from counting)')); + const signalList = rfrNode('div', 'config-list'); + signals.forEach(sig => { + signalList.appendChild(checkRow(sig.name, sig.description, !state.disabled.has(sig.name), v => { if (v) state.disabled.delete(sig.name); else state.disabled.add(sig.name); })); + }); + host.appendChild(signalList); + + const hints = rfrNode('div', 'config-help'); + const hint = rfrNode('span', 'settings-yaml-hint', 'YAML '); + hint.appendChild(rfrNode('code', '', 'review_feedback.rules.enabled | trusted_associations | extra_phrases | disabled_signals')); + hints.appendChild(hint); + host.appendChild(hints); + + const actions = rfrNode('div', 'config-list-add'); + actions.appendChild(saveBtn); + actions.appendChild(statusEl); + host.appendChild(actions); + syncSave(); + + saveBtn.addEventListener('click', async () => { + const trusted = [...state.trusted].filter(a => associations.includes(a)); + /* An empty list means "the default" to the server, so an owner who + unticked everything would silently get it back. Ask instead. */ + if (!trusted.length) { setStatus('Tick at least one association, or keep the default set.', 'text-danger'); return; } + saveBtn.disabled = true; + setStatus('Saving…'); + const sameAsDefault = trusted.length === defaults.length && defaults.every(a => state.trusted.has(a)); + try { + const res = await fetch(REVIEW_FEEDBACK_RULES_URL, { + method: 'PUT', + headers: _inceptionAuthHeaders(), + body: JSON.stringify({ + enabled: state.enabled, + trusted_associations: sameAsDefault ? [] : trusted, + extra_phrases: reviewFeedbackRulesPhrases(state.phrases), + disabled_signals: [...state.disabled], + }), + }); + if (!res.ok) throw new Error(await saveErrorMessage(res)); + renderReviewFeedbackRulesPanel(host, await res.json()); + showToast('Review-feedback rules saved', 'success'); + } catch (e) { + setStatus(e.message, 'text-danger'); + syncSave(); + } + }); + } // Workflow-model bindings (hivecommons/hive#17620, RFC phase 2). // // Each repo row on Settings → Repos carries a "Workflow" select: hive-default @@ -78759,6 +78918,9 @@

${esc(s.title)}

// into FEATURES_SECTIONS; keep new controls inside the section they belong // to rather than appending to the end of the template. function renderGovFeatures(f, am, rv) { + /* Review feedback rules (#19138): painted asynchronously from their own + owner-only endpoint, like the Repos tab's parent auto-close. */ + loadReviewFeedbackRules(); f = f || {}; am = am || {}; rv = rv || {}; @@ -79280,6 +79442,11 @@

${esc(s.title)}

Look this hash up in the "Contributing hives" table at hivecommons.dev/benchmarks to verify your records arrived${f.benchmarksLastPushAt ? ` · last pushed ${esc(f.benchmarksLastPushAt)} (${esc(String(f.benchmarksLastPushCount || 0))} records)` : ' · nothing pushed yet'}.
+
+
Rules from review feedback ${yamlHint('review_feedback.rules')} ${settingHelpMark('review-feedback-rules', 'settings', 'Rules from review feedback', `When a person reviewing a hive PR states a rule ("X is the source of truth", "going forward ..."), the hive records it in its knowledge for later work. Choose whether it does, whose rules count at once (every other reviewer's rule waits for approval), extra phrases that mark a comment as a rule, and built-in signals to ignore. Owner only. Applies on the next sweep pass, without a restart.`)}
+
Loading…
+
+
Review evidence signing
Read-only here: signing keys are provisioned on disk and set in hive.yaml.
diff --git a/src/pkg/reviewrules/classify.go b/src/pkg/reviewrules/classify.go index 9bf229994b..94e01fbe55 100644 --- a/src/pkg/reviewrules/classify.go +++ b/src/pkg/reviewrules/classify.go @@ -52,6 +52,9 @@ const ( SignalCaptureRequest = "capture-request" SignalCriteria = "assessment-criteria" SignalKnowledgeBase = "knowledge-base" + // SignalExtraPhrase marks a match on an operator-supplied phrase + // (review_feedback.rules.extra_phrases). + SignalExtraPhrase = "extra-phrase" ) type signal struct { @@ -135,14 +138,25 @@ type Classification struct { // Classify says whether body states a rule for future work. It is a pure // function of the text: every signal is an explicit phrase from ruleSignals. -func Classify(body string) Classification { +func Classify(body string) Classification { return ClassifyWith(body, Policy{}) } + +// ClassifyWith is Classify under an operator's policy: built-in signals the +// policy disables do not count, and its extra phrases count as one more signal +// named SignalExtraPhrase. +func ClassifyWith(body string, p Policy) Classification { body = stripNoise(body) var hit []string for _, s := range ruleSignals { + if p.DisabledSignals[s.name] { + continue + } if s.re.MatchString(body) { hit = append(hit, s.name) } } + if re := p.extraRegexp(); re != nil && re.MatchString(body) { + hit = append(hit, SignalExtraPhrase) + } if len(hit) == 0 { return Classification{Kind: KindCorrection} } @@ -156,14 +170,18 @@ func ForeignHomeHint(body string) bool { return foreignHomeHint.MatchString(body // ExtractRuleText returns the rule a comment states: the sentences that carry a // signal, without the clauses that only ask for the capture, normalised and // bounded. A comment with no signal yields its whole body, bounded. -func ExtractRuleText(body string) string { +func ExtractRuleText(body string) string { return ExtractRuleTextWith(body, Policy{}) } + +// ExtractRuleTextWith is ExtractRuleText under an operator's policy, so a +// sentence carrying only an extra phrase is kept as the rule. +func ExtractRuleTextWith(body string, p Policy) string { clean := stripNoise(body) var kept []string for _, sent := range sentenceSplit.Split(clean, -1) { if strings.TrimSpace(sent) == "" { continue } - if Classify(sent).Kind == KindRule { + if ClassifyWith(sent, p).Kind == KindRule { kept = append(kept, sent) } } diff --git a/src/pkg/reviewrules/policy.go b/src/pkg/reviewrules/policy.go new file mode 100644 index 0000000000..d57dde753d --- /dev/null +++ b/src/pkg/reviewrules/policy.go @@ -0,0 +1,113 @@ +package reviewrules + +import ( + "os" + "regexp" + "strings" +) + +// Policy is what an operator tunes about rule capture without a code change +// (hivecommons/hive#19138): whether it runs, whose rules are trusted, extra +// phrases that mark a comment as a rule, and built-in signals to ignore. The +// zero value is the built-in behaviour with capture on. +type Policy struct { + // Off turns capture off. + Off bool + // Trusted are the upper-case author associations that may state a rule + // on their own authority. Nil keeps DefaultTrustedAssociations. + Trusted map[string]bool + // ExtraPhrases are matched case-insensitively on word boundaries. + ExtraPhrases []string + // DisabledSignals are built-in signal names that no longer count. + DisabledSignals map[string]bool + + extraRe *regexp.Regexp +} + +// extraRegexp is the compiled alternation of ExtraPhrases, nil when there are +// none. Resolve compiles it once; a Policy built by hand compiles on use. +// Phrases are quoted, so one can never act as a pattern. +func (p Policy) extraRegexp() *regexp.Regexp { + if p.extraRe != nil { + return p.extraRe + } + var parts []string + for _, ph := range p.ExtraPhrases { + ph = strings.Join(strings.Fields(ph), " ") + if ph == "" { + continue + } + parts = append(parts, strings.ReplaceAll(regexp.QuoteMeta(ph), " ", `\s+`)) + } + if len(parts) == 0 { + return nil + } + return regexp.MustCompile(`(?i)(?:^|[^\pL\pN_])(?:` + strings.Join(parts, "|") + `)(?:[^\pL\pN_]|$)`) +} + +// Settings is the operator's preference set as hive.yaml gives it +// (review_feedback.rules.*). It is a plain struct so this package does not +// import pkg/config. +type Settings struct { + Enabled *bool + TrustedAssociations []string + ExtraPhrases []string + DisabledSignals []string +} + +// Resolved is the effective policy and where each part came from. +type Resolved struct { + Policy Policy + // EnabledFromEnv and TrustedFromEnv say the environment variable decided + // the value, so the yaml key has no effect while it is set. + EnabledFromEnv bool + TrustedFromEnv bool +} + +// Resolve merges hive.yaml with the environment. The environment variables +// keep working and win when they are set: HIVE_REVIEW_FEEDBACK_RULES set to a +// false-like value stops capture, and a non-empty +// HIVE_REVIEW_FEEDBACK_RULE_TRUSTED_ASSOCIATIONS is the trusted list. Without +// them, the yaml keys apply, then the defaults. Extra phrases and disabled +// signals have no environment variable. +func Resolve(set Settings) Resolved { + var out Resolved + out.Policy.Off = set.Enabled != nil && !*set.Enabled + if v := strings.TrimSpace(os.Getenv(DisableEnvVar)); v != "" { + switch strings.ToLower(v) { + case "0", "false", "off", "no": + out.Policy.Off = true + out.EnabledFromEnv = true + default: + out.Policy.Off = false + out.EnabledFromEnv = true + } + } + switch list := strings.TrimSpace(os.Getenv(TrustedAssociationsEnvVar)); { + case list != "": + out.Policy.Trusted = ParseAssociations(list) + out.TrustedFromEnv = true + case len(set.TrustedAssociations) > 0: + out.Policy.Trusted = ParseAssociations(strings.Join(set.TrustedAssociations, ",")) + default: + out.Policy.Trusted = ParseAssociations(DefaultTrustedAssociations) + } + out.Policy.ExtraPhrases = append([]string(nil), set.ExtraPhrases...) + if len(set.DisabledSignals) > 0 { + out.Policy.DisabledSignals = map[string]bool{} + for _, s := range set.DisabledSignals { + out.Policy.DisabledSignals[strings.ToLower(strings.TrimSpace(s))] = true + } + } + out.Policy.extraRe = out.Policy.extraRegexp() + return out +} + +// SignalNames lists the built-in signal names in table order. +func SignalNames() []string { + names := make([]string, 0, len(ruleSignals)) + for _, s := range ruleSignals { + names = append(names, s.name) + } + return names +} diff --git a/src/pkg/reviewrules/policy_test.go b/src/pkg/reviewrules/policy_test.go new file mode 100644 index 0000000000..1c53d09955 --- /dev/null +++ b/src/pkg/reviewrules/policy_test.go @@ -0,0 +1,116 @@ +package reviewrules + +import ( + "strings" + "testing" + + "github.com/hivecommons/hive/pkg/config" +) + +// hivecommons/hive#19138: operator-tunable capture policy. + +func TestSignalVocabularyMatchesConfig(t *testing.T) { + if got, want := strings.Join(config.ReviewFeedbackSignals, ","), strings.Join(SignalNames(), ","); got != want { + t.Fatalf("config.ReviewFeedbackSignals = %s, classifier signals = %s", got, want) + } +} + +func TestResolve(t *testing.T) { + off := false + t.Run("defaults", func(t *testing.T) { + t.Setenv(DisableEnvVar, "") + t.Setenv(TrustedAssociationsEnvVar, "") + r := Resolve(Settings{}) + if r.Policy.Off || r.EnabledFromEnv || r.TrustedFromEnv { + t.Fatalf("resolved = %+v", r) + } + if !r.Policy.Trusted["OWNER"] || !r.Policy.Trusted["MEMBER"] || !r.Policy.Trusted["COLLABORATOR"] || r.Policy.Trusted["CONTRIBUTOR"] { + t.Fatalf("trusted = %v", r.Policy.Trusted) + } + }) + t.Run("yaml applies without the env", func(t *testing.T) { + t.Setenv(DisableEnvVar, "") + t.Setenv(TrustedAssociationsEnvVar, "") + r := Resolve(Settings{Enabled: &off, TrustedAssociations: []string{"OWNER"}, DisabledSignals: []string{"never"}}) + if !r.Policy.Off || len(r.Policy.Trusted) != 1 || !r.Policy.Trusted["OWNER"] || !r.Policy.DisabledSignals["never"] { + t.Fatalf("resolved = %+v", r) + } + }) + t.Run("env still wins", func(t *testing.T) { + t.Setenv(DisableEnvVar, "off") + t.Setenv(TrustedAssociationsEnvVar, "MEMBER, contributor") + on := true + r := Resolve(Settings{Enabled: &on, TrustedAssociations: []string{"OWNER"}}) + if !r.Policy.Off || !r.EnabledFromEnv || !r.TrustedFromEnv || !r.Policy.Trusted["CONTRIBUTOR"] || r.Policy.Trusted["OWNER"] { + t.Fatalf("resolved = %+v", r) + } + }) + t.Run("env on overrides yaml off", func(t *testing.T) { + t.Setenv(DisableEnvVar, "1") + t.Setenv(TrustedAssociationsEnvVar, "") + if r := Resolve(Settings{Enabled: &off}); r.Policy.Off || !r.EnabledFromEnv { + t.Fatalf("resolved = %+v", r) + } + }) +} + +func TestClassifyWithPolicy(t *testing.T) { + extra := Resolve(Settings{ExtraPhrases: []string{"house rule", "ground truth"}}).Policy + for _, tc := range []struct { + name string + body string + p Policy + kind Kind + sig string + }{ + {"extra phrase", "This is a House Rule for us.", extra, KindRule, SignalExtraPhrase}, + {"extra phrase at the edges", "ground truth", extra, KindRule, SignalExtraPhrase}, + {"extra phrase needs word boundaries", "the groundtruthful answer", extra, KindCorrection, ""}, + {"extra phrase in a quote is ignored", "> house rule\nthe score is too high", extra, KindCorrection, ""}, + {"no extra phrases, no match", "This is a house rule.", Policy{}, KindCorrection, ""}, + {"phrase is not a pattern", "anything", Resolve(Settings{ExtraPhrases: []string{".*.*"}}).Policy, KindCorrection, ""}, + {"disabled signal", "Add that to the knowledge base.", Policy{DisabledSignals: map[string]bool{SignalKnowledgeBase: true}}, KindCorrection, ""}, + {"other signals survive", "Going forward, cite it. Add that to the knowledge base.", Policy{DisabledSignals: map[string]bool{SignalKnowledgeBase: true}}, KindRule, SignalGoingForward}, + } { + t.Run(tc.name, func(t *testing.T) { + got := ClassifyWith(tc.body, tc.p) + if got.Kind != tc.kind { + t.Fatalf("kind = %q (%v), want %q", got.Kind, got.Signals, tc.kind) + } + if tc.sig != "" && (len(got.Signals) != 1 || got.Signals[0] != tc.sig) { + t.Fatalf("signals = %v, want [%s]", got.Signals, tc.sig) + } + }) + } +} + +func TestExtractRuleTextWithExtraPhrase(t *testing.T) { + p := Policy{ExtraPhrases: []string{"house rule"}} + got := ExtractRuleTextWith("Nice work overall. House rule: every analysis cites the scorecard. Thanks!", p) + if !strings.Contains(got, "every analysis cites the scorecard") || strings.Contains(got, "Thanks") { + t.Fatalf("text = %q", got) + } +} + +// An owner's edit applies to a running store: no reopen. +func TestStoreSetPolicyAppliesLive(t *testing.T) { + s, _, _ := openStore(t, Config{}) + const phrase = "House rule: every analysis cites the scorecard." + if r := s.CaptureComment(cmt("inline:1", "OWNER", phrase), t0); r.Outcome != OutcomeNotRule { + t.Fatalf("before = %q", r.Outcome) + } + s.SetPolicy(Policy{Trusted: ParseAssociations("OWNER"), ExtraPhrases: []string{"house rule"}}) + r := s.CaptureComment(cmt("inline:2", "OWNER", phrase), t0) + if r.Outcome != OutcomeCaptured || len(r.Signals) != 1 || r.Signals[0] != SignalExtraPhrase { + t.Fatalf("after = %q %v", r.Outcome, r.Signals) + } + // Narrowing the trusted set queues a member's rule that was trusted before. + if r := s.CaptureComment(cmt("inline:3", "MEMBER", "Going forward, cite the commit for every claim."), t0); r.Outcome != OutcomeQueued { + t.Fatalf("member after narrowing = %q", r.Outcome) + } + // A disabled built-in signal stops matching at once. + s.SetPolicy(Policy{Trusted: ParseAssociations("OWNER"), DisabledSignals: map[string]bool{SignalGoingForward: true}}) + if r := s.CaptureComment(cmt("inline:4", "OWNER", "Going forward, cite the commit for every claim, always."), t0); r.Outcome != OutcomeNotRule { + t.Fatalf("disabled signal = %q", r.Outcome) + } +} diff --git a/src/pkg/reviewrules/store.go b/src/pkg/reviewrules/store.go index 717201a6f6..fa689ccacb 100644 --- a/src/pkg/reviewrules/store.go +++ b/src/pkg/reviewrules/store.go @@ -255,7 +255,9 @@ type Store struct { dir string vault string cfg Config - st index + // policy is the operator's live phrase and signal preferences. + policy Policy + st index // Reindex, when set, is called after the vault changed so the primer's // view is current without waiting for its periodic refresh. Reindex func() @@ -343,6 +345,19 @@ type Result struct { Err error } +// SetPolicy applies an operator's policy to a running store: the trusted +// associations, the extra phrases and the disabled signals take effect on the +// next comment, without a restart (hivecommons/hive#19138). The caps in the +// store's Config are unchanged. +func (s *Store) SetPolicy(p Policy) { + s.mu.Lock() + defer s.mu.Unlock() + s.policy = p + if p.Trusted != nil { + s.cfg.Trusted = p.Trusted + } +} + func (s *Store) trusted(assoc string) bool { return s.cfg.Trusted[strings.ToUpper(strings.TrimSpace(assoc))] } @@ -449,16 +464,16 @@ func (s *Store) Remember(c Comment, now time.Time) error { // It is idempotent per comment: a comment id that was captured, queued or // merged before returns OutcomeDuplicate and changes nothing. func (s *Store) CaptureComment(c Comment, now time.Time) Result { - cl := Classify(c.Body) + s.mu.Lock() + defer s.mu.Unlock() + cl := ClassifyWith(c.Body, s.policy) if cl.Kind != KindRule { return Result{Outcome: OutcomeNotRule} } - s.mu.Lock() - defer s.mu.Unlock() if id, ok := s.st.Comments[c.key()]; ok { return Result{Outcome: OutcomeDuplicate, Rule: s.copyOf(s.resolve(s.byID(id))), Signals: cl.Signals} } - text := ExtractRuleText(c.Body) + text := ExtractRuleTextWith(c.Body, s.policy) if text == "" { return Result{Outcome: OutcomeRefused, Signals: cl.Signals, Err: errors.New("the comment has no text to record")} }