From 970528ef81d40692cdeecd0c8c7d356eca1f4822 Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Thu, 19 Nov 2026 06:21:38 -0500 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20Customers:=20import=20hub-conse?= =?UTF-8?q?nted=20fields=20with=20hub=20provenance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Phase 3 of src/docs/design/customers-hub-consent.md (#19088). A third profile Source, hub, sits between auto and manual. An opt-in import posts salted hashes of confirmed customers' GitHub logins to the hub match endpoint (#19162). It applies only the real name, organization and country a person consented to share, and it never creates a person or overwrites a manual field. A cleared reply or the 72h staleness expiry hands a hub field back to automatic, and a forgotten login is never asked about. customers.hub_import.enabled (default off), HIVE_CUSTOMERS_HUB_IMPORT_ENABLED and a Settings, Features switch beside Customer avatars gate the import. The import also needs a hub registration. Hub-side provisioning of the match key is #19246. Closes #19163 Co-Authored-By: Claude Opus 5.5 Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Co-authored-by: clubanderson <407614+clubanderson@users.noreply.github.com> --- .../added-19163-customers-hub-import.md | 1 + src/cmd/hive/customers_hub_import.go | 152 ++++++ src/cmd/hive/customers_hub_import_test.go | 108 +++++ src/cmd/hive/customerswire.go | 8 +- src/cmd/hive/main.go | 6 + src/cmd/hive/sweep_workers.go | 7 + src/docs/env-vars.md | 2 + src/docs/operator-reference/README.md | 1 + src/pkg/config/customers.go | 43 ++ src/pkg/config/customers_test.go | 32 ++ .../config/testdata/agent_runtime_golden.json | 1 + src/pkg/customers/affiliation.go | 2 +- src/pkg/customers/hub_import.go | 440 ++++++++++++++++++ src/pkg/customers/hub_import_test.go | 407 ++++++++++++++++ src/pkg/customers/model.go | 21 +- src/pkg/customers/store.go | 13 +- src/pkg/dashboard/api_governor_features.go | 9 + src/pkg/dashboard/customers_ui_test.go | 75 +++ .../static-src/js/config-features.js | 5 + .../dashboard/static-src/js/config-state.js | 1 + src/pkg/dashboard/static-src/js/customers.js | 9 +- src/pkg/dashboard/static/index.html | 15 +- .../hub/customer_match_spoke_parity_test.go | 42 ++ src/pkg/hub/spoke/keys.go | 18 + 24 files changed, 1397 insertions(+), 21 deletions(-) create mode 100644 changelog.d/added-19163-customers-hub-import.md create mode 100644 src/cmd/hive/customers_hub_import.go create mode 100644 src/cmd/hive/customers_hub_import_test.go create mode 100644 src/pkg/customers/hub_import.go create mode 100644 src/pkg/customers/hub_import_test.go create mode 100644 src/pkg/hub/customer_match_spoke_parity_test.go diff --git a/changelog.d/added-19163-customers-hub-import.md b/changelog.d/added-19163-customers-hub-import.md new file mode 100644 index 0000000000..d8c7ab2022 --- /dev/null +++ b/changelog.d/added-19163-customers-hub-import.md @@ -0,0 +1 @@ +- Customers can import the real name, organization and country a confirmed customer entered on the hub and consented to share with this hive, shown "from the hub" and never over a value set by hand; a withdrawal or three days without confirmation clears it. Off by default: `customers.hub_import.enabled`, `HIVE_CUSTOMERS_HUB_IMPORT_ENABLED`, or Settings, Features ([#19163](https://github.com/hivecommons/hive/issues/19163)). diff --git a/src/cmd/hive/customers_hub_import.go b/src/cmd/hive/customers_hub_import.go new file mode 100644 index 0000000000..64494e2e77 --- /dev/null +++ b/src/cmd/hive/customers_hub_import.go @@ -0,0 +1,152 @@ +package main + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "io" + "log/slog" + "net/http" + "strings" + "time" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/customers" + spoke "github.com/hivecommons/hive/pkg/hub/spoke" +) + +// The Customers hub import (#19163, phase 3 of +// src/docs/design/customers-hub-consent.md). pkg/customers decides what is +// asked and what an answer may change; this file is the network call and the +// wiring. Off unless customers.hub_import.enabled AND a hub registration. + +const ( + // customersHubMatchPath is the hub's match route (pkg/hub + // customerMatchPath; cmd/hive does not import pkg/hub). + customersHubMatchPath = "/api/customers/match" + // customersHubImportInterval is how often the bounded pass runs. + customersHubImportInterval = customers.HubImportInterval + // customersHubImportTimeout bounds one pass, all its requests together. + customersHubImportTimeout = 60 * time.Second + // customersHubRequestTimeout bounds one request. + customersHubRequestTimeout = 20 * time.Second + // customersHubMaxErrBody caps what is read of a refusal. + customersHubMaxErrBody = 512 +) + +// customersHubMatcher posts one batch of match hashes to the hub with the +// per-hive heartbeat bearer. It sends no cookie, no identity header and no +// plaintext login. +type customersHubMatcher struct { + hubURL string + hiveID string + client *http.Client +} + +func (m customersHubMatcher) Match(ctx context.Context, hashes []string) (customers.HubMatchReply, error) { + bearer := spoke.SpokeHeartbeatKey() + if bearer == "" { + return customers.HubMatchReply{}, errors.New("no hub credential (HIVE_HEARTBEAT_KEY / HIVE_HUB_SECRET) configured") + } + body, err := json.Marshal(struct { + HiveID string `json:"hive_id"` + Hashes []string `json:"hashes"` + }{HiveID: m.hiveID, Hashes: hashes}) + if err != nil { + return customers.HubMatchReply{}, err + } + endpoint := strings.TrimRight(strings.TrimSpace(m.hubURL), "/") + customersHubMatchPath + req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(body)) + if err != nil { + return customers.HubMatchReply{}, err + } + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Authorization", "Bearer "+bearer) + client := m.client + if client == nil { + client = customersHubClient() + } + resp, err := client.Do(req) + if err != nil { + return customers.HubMatchReply{}, err + } + defer func() { _ = resp.Body.Close() }() + if resp.StatusCode != http.StatusOK { + _, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, customersHubMaxErrBody)) + return customers.HubMatchReply{}, fmt.Errorf("hub match: HTTP %d", resp.StatusCode) + } + return customers.DecodeHubMatchReply(resp.Body) +} + +// customersHubClient never follows a redirect, so the bearer only ever goes +// to the configured hub. +func customersHubClient() *http.Client { + return &http.Client{ + Timeout: customersHubRequestTimeout, + CheckRedirect: func(*http.Request, []*http.Request) error { + return http.ErrUseLastResponse + }, + } +} + +// customersHubLinked reports whether this hive is registered with a hub it +// can ask: hub reporting on, a hub URL and a hive id. +func customersHubLinked(cfg *config.Config) bool { + return cfg != nil && cfg.Hub.Enabled && strings.TrimSpace(cfg.Hub.URL) != "" && strings.TrimSpace(cfg.HiveID) != "" +} + +// newCustomersHubImport builds one import over the current config. Enabled +// is the spoke switch and the hub link together: either off, no request. +func newCustomersHubImport(store *customers.Store, cfg *config.Config, logger *slog.Logger) *customers.HubImport { + hiveID := "" + hubURL := "" + if cfg != nil { + hiveID = strings.TrimSpace(cfg.HiveID) + hubURL = cfg.Hub.URL + } + return &customers.HubImport{ + Store: store, + Matcher: customersHubMatcher{hubURL: hubURL, hiveID: hiveID}, + Enabled: func() bool { return cfg.CustomersHubImportEnabled() && customersHubLinked(cfg) }, + Key: func() string { return spoke.SpokeCustomerMatchKey(hiveID) }, + Audit: func(person, action string, fields []string) { + // One line per import or clear: person id and field names, + // never values, never a login or a hash. + if logger != nil { + logger.Info("audit: "+action, slog.String("person", person), slog.Any("fields", fields)) + } + }, + } +} + +// runCustomersHubImport is one import pass; only names one person for the +// accept-time pull, "" asks about every confirmed customer. Staleness expiry +// runs on every pass, switch on or off, and makes no request. +func (b *boot) runCustomersHubImport(ctx context.Context, cfg *config.Config, only string) { + if b.customers == nil || cfg == nil || !cfg.CustomersEnabled() { + return + } + ctx, cancel := context.WithTimeout(ctx, customersHubImportTimeout) + defer cancel() + res := newCustomersHubImport(b.customers.svc.Store(), cfg, b.logger).Run(ctx, only) + if b.logger != nil && (res.Requests > 0 || res.Expired > 0) { + b.logger.Info("[customers] hub import", "requests", res.Requests, "asked", res.Asked, + "imported", res.Imported, "cleared", res.Cleared, "expired", res.Expired, "skipped", res.Skipped) + } +} + +// hubImportOnAccept pulls the hub's answer for a person just accepted. It +// never delays or fails the accept. +func (c *customersDashboard) hubImportOnAccept(person string) { + if c.hubImport == nil || person == "" { + return + } + run := func() { c.hubImport(context.Background(), person) } + if c.spawn != nil { + c.spawn(run) + return + } + go run() +} diff --git a/src/cmd/hive/customers_hub_import_test.go b/src/cmd/hive/customers_hub_import_test.go new file mode 100644 index 0000000000..dd6ee8ccfe --- /dev/null +++ b/src/cmd/hive/customers_hub_import_test.go @@ -0,0 +1,108 @@ +package main + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "sync/atomic" + "testing" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/customers" + spoke "github.com/hivecommons/hive/pkg/hub/spoke" +) + +func TestCustomersHubMatcherPostsHashesWithBearer(t *testing.T) { + t.Setenv(spoke.EnvHeartbeatKey, "bearer-1") + var got struct { + HiveID string `json:"hive_id"` + Hashes []string `json:"hashes"` + } + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost || r.URL.Path != customersHubMatchPath { + t.Errorf("unexpected %s %s", r.Method, r.URL.Path) + } + if r.Header.Get("Authorization") != "Bearer bearer-1" { + t.Errorf("bearer = %q", r.Header.Get("Authorization")) + } + if r.Header.Get("Cookie") != "" { + t.Error("no cookie may be sent") + } + if err := json.NewDecoder(r.Body).Decode(&got); err != nil { + t.Errorf("decode: %v", err) + } + _, _ = w.Write([]byte(`{"matches":[{"match_hash":"h1","real_name":"Ada\u0007 L","notes":"never read","cleared":["organization","tags"]}]}`)) + })) + defer srv.Close() + + reply, err := customersHubMatcher{hubURL: srv.URL + "/", hiveID: "hive-a"}.Match(context.Background(), []string{"h1"}) + if err != nil { + t.Fatal(err) + } + if got.HiveID != "hive-a" || len(got.Hashes) != 1 || got.Hashes[0] != "h1" { + t.Fatalf("request body = %+v", got) + } + if len(reply.Matches) != 1 { + t.Fatalf("matches = %+v", reply.Matches) + } + m := reply.Matches[0] + if m.RealName != "Ada L" || len(m.Cleared) != 1 || m.Cleared[0] != customers.FieldOrganization { + t.Fatalf("reply not sanitised: %+v", m) + } +} + +func TestCustomersHubMatcherRefusal(t *testing.T) { + t.Setenv(spoke.EnvHeartbeatKey, "bearer-1") + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + http.NotFound(w, nil) + })) + defer srv.Close() + if _, err := (customersHubMatcher{hubURL: srv.URL, hiveID: "hive-a"}).Match(context.Background(), []string{"h1"}); err == nil { + t.Fatal("a 404 (hub sharing off) must be an error") + } +} + +func TestCustomersHubImportOffMakesNoRequest(t *testing.T) { + t.Setenv(config.EnvCustomersEnabled, "") + t.Setenv(config.EnvCustomersHubImportEnabled, "") + t.Setenv(spoke.EnvHeartbeatKey, "bearer-1") + t.Setenv(spoke.EnvCustomerMatchKey, "match-key") + var hits atomic.Int32 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + hits.Add(1) + _, _ = w.Write([]byte(`{"matches":[]}`)) + })) + defer srv.Close() + + store, err := customers.Open(t.TempDir(), nil) + if err != nil { + t.Fatal(err) + } + if _, err := store.Add("Ada", []customers.Identity{{Kind: customers.KindGitHub, Value: "ada"}}, "owner"); err != nil { + t.Fatal(err) + } + on := true + cases := map[string]*config.Config{ + "switch off": {HiveID: "hive-a", Hub: config.HubConfig{Enabled: true, URL: srv.URL}}, + "no hub": {HiveID: "hive-a", Customers: config.CustomersConfig{HubImport: config.CustomersHubImportConfig{Enabled: &on}}}, + } + for name, cfg := range cases { + res := newCustomersHubImport(store, cfg, nil).Run(context.Background(), "") + if res.Requests != 0 || res.Skipped != customers.HubSkipDisabled { + t.Fatalf("%s: %+v", name, res) + } + } + if hits.Load() != 0 { + t.Fatalf("hub was called %d times with the import off", hits.Load()) + } + + cfg := &config.Config{HiveID: "hive-a", Hub: config.HubConfig{Enabled: true, URL: srv.URL}, + Customers: config.CustomersConfig{HubImport: config.CustomersHubImportConfig{Enabled: &on}}} + if res := newCustomersHubImport(store, cfg, nil).Run(context.Background(), ""); res.Requests != 1 { + t.Fatalf("on: %+v", res) + } + if hits.Load() != 1 { + t.Fatalf("hub hits = %d, want 1", hits.Load()) + } +} diff --git a/src/cmd/hive/customerswire.go b/src/cmd/hive/customerswire.go index ffae6a6792..300f0865ad 100644 --- a/src/cmd/hive/customerswire.go +++ b/src/cmd/hive/customerswire.go @@ -34,7 +34,11 @@ type customersDashboard struct { // mentions is the accept-time GitHub search (step E, #18884); nil when // it is not wired. Only Accept and AcceptInto reach it. mentions *customers.MentionSearch - logger *slog.Logger + // hubImport is the accept-time hub pull (#19163); nil when not wired. + // It asks only about the person just accepted and is off unless + // customers.hub_import.enabled and a hub registration. + hubImport func(ctx context.Context, person string) + logger *slog.Logger // spawn runs the search; nil means a goroutine. Tests run it inline. spawn func(func()) } @@ -158,6 +162,7 @@ func (c *customersDashboard) Accept(v dashboard.CustomersViewer, suggestionID st res, err := customerResult(c.svc.Accept(customersViewer(v), suggestionID)) if err == nil { c.searchOnAccept(res.ID, ident, found) + c.hubImportOnAccept(res.ID) } return res, err } @@ -227,6 +232,7 @@ func (c *customersDashboard) AcceptInto(v dashboard.CustomersViewer, suggestionI res, err := customerResult(c.svc.AcceptInto(customersViewer(v), suggestionID, into)) if err == nil { c.searchOnAccept(res.ID, ident, found) + c.hubImportOnAccept(res.ID) } return res, err } diff --git a/src/cmd/hive/main.go b/src/cmd/hive/main.go index 8517a8fe99..d704c042da 100644 --- a/src/cmd/hive/main.go +++ b/src/cmd/hive/main.go @@ -3656,6 +3656,12 @@ func (b *boot) bootDashboardAPIWith(deps bootDashboardAPIDeps) { // Step E (#18884): the budgeted GitHub search on accept. Off unless // customers.mention_search.enabled; never reached by the sweep. svc.mentions = newCustomersMentionSearch(svc.svc, b.customersMentionConfig, b.currentGitHubClient) + // Phase 3 of customers-hub-consent (#19163): pull hub-consented + // fields for a person just accepted. Off unless + // customers.hub_import.enabled and a hub registration. + svc.hubImport = func(ctx context.Context, person string) { + b.runCustomersHubImport(ctx, b.customersMentionConfig(), person) + } b.dashSrv.SetCustomersService(svc) if b.sched != nil { installCustomersAgentContext(svc, b.sched.SetCustomerContextLookup) diff --git a/src/cmd/hive/sweep_workers.go b/src/cmd/hive/sweep_workers.go index 8370b8a8a8..e655d709d3 100644 --- a/src/cmd/hive/sweep_workers.go +++ b/src/cmd/hive/sweep_workers.go @@ -152,6 +152,13 @@ func (b *boot) sweepJobs() []sweepJob { {"customers_inference", fixed(customersInferenceInterval), sweepOptional, func(ctx context.Context, _ *github.Client, cfg *config.Config) { b.runCustomersInference(ctx, cfg) }}, + // Customers hub import (#19163): off unless + // customers.hub_import.enabled and a hub registration. Posts only + // salted hashes of confirmed customers' GitHub logins to the hub; + // makes no GitHub call. Staleness expiry runs every pass. + {"customers_hub_import", fixed(customersHubImportInterval), sweepOptional, func(ctx context.Context, _ *github.Client, cfg *config.Config) { + b.runCustomersHubImport(ctx, cfg, "") + }}, {"rfc_notify", fixed(rfcnotify.DefaultInterval + time.Second), sweepOptional, func(ctx context.Context, gh *github.Client, cfg *config.Config) { runRFCNotifySweep(ctx, newRFCNotifySource(gh, notify), len(cfg.Project.Repos), cfg, b.dashSrv, notify, b.logger) }}, diff --git a/src/docs/env-vars.md b/src/docs/env-vars.md index e8d92339f3..4a3c82952c 100644 --- a/src/docs/env-vars.md +++ b/src/docs/env-vars.md @@ -303,6 +303,7 @@ new value at the same time. | `HIVE_CUSTOMERS_ENABLED` | No | config `customers.enabled` (default on) | Turns the Customers section and its people store on or off; `false` opens no store, runs no sweep and hides the section, overriding config. Nothing the store holds leaves the hive. | | `HIVE_CUSTOMERS_INFERENCE_ENABLED` | No | config `customers.inference.enabled` (default off) | Turns goal and satisfaction suggestions on or off, overriding config. Needs Customers on. When on, a customer's own issues, pull requests and approved mail are sent to the meetings model; every suggestion waits for a person to confirm, edit or reject it. | | `HIVE_CUSTOMERS_AVATARS_ENABLED` | No | config `customers.avatars.enabled` (default on) | Turns GitHub pictures on the Customers list on or off, overriding config. On, the viewer's browser requests `https://github.com/.png` with no referrer for each person with a confirmed GitHub login; off, the list shows initials and makes no request to github.com. | +| `HIVE_CUSTOMERS_HUB_IMPORT_ENABLED` | No | config `customers.hub_import.enabled` (default off) | Turns the Customers hub import on or off, overriding config. Needs Customers on and a hub registration. When on, the hive posts salted hashes of its confirmed customers' GitHub logins to the hub hourly and on accept; the hub answers only with the real name, organization and country a person entered there and consented to share with this hive. See [customers-hub-consent](design/customers-hub-consent.md). | | `HIVE_CUSTOMERS_MENTION_SEARCH_ENABLED` | No | config `customers.mention_search.enabled` (default off) | Turns the accept-time GitHub mention search on or off, overriding config. Needs Customers on. When on, accepting a suggested GitHub login makes at most two GitHub search requests for that login, counted against the shared API budget. | | `HIVE_SESSIONS_ENABLED` | No | config `dashboard.sessions.enabled` (default on) | Turns the dashboard Inception (spek) sessions feature (interactive CLI terminals, `/api/sessions`) on or off; `false` hides it, overriding config. Limits: `HIVE_SESSIONS_MAX`, `HIVE_SESSIONS_MAX_PER_USER`, `HIVE_SESSIONS_IDLE_TTL_HOURS`. Workspace: `HIVE_SESSIONS_WORKSPACE_REPO` (the `org/repo` sessions open in, default `project.org/primary_repo`), `HIVE_SESSIONS_WORKSPACE_DIR` (root of the one shared clone per repo, `//`, and of each session's own git worktree of it, `/.sessions///` on branch `hive-session/`; sessions share commits and branches but not uncommitted edits or `.spektacular/` state; default `/data/session-sandbox/workspace`), `HIVE_SESSIONS_GID` (the sessions-only group that shares the clone, default 20000). Inbox seeds: `HIVE_SESSIONS_SEED_MAX_BYTES` (cap on one item's enveloped text in `.hive/inbox/`, default 524288); see [Sessions API](api-reference/sessions.md). | | `HIVE_SESSIONS_AUTOSTART` | No | config `dashboard.sessions.autostart` (default on) | Turns the session startup auto-answer on or off, overriding config: when a session starts the hive answers the vendor CLI's startup screens (first-run notice, trust for the session's folder, onboarding) so its first request runs unattended. It never answers a permission prompt or a login screen. `HIVE_SESSIONS_AUTOSTART_WINDOW_SEC` (default `90`) bounds how long it answers for after a start. See [Startup auto-answer](api-reference/sessions.md#startup-auto-answer). | @@ -700,6 +701,7 @@ only when neither is configured. |---|---:|---|---| | `HIVE_HEARTBEAT_KEY` | No | derived from `HIVE_HUB_SECRET` | Spoke heartbeat signing sub-key. For a self-service (`byo-`) hive this is the only key the spoke holds: the hub mints it at registration, shows it once, and stores only a hash. There is no derivation fallback for a `byo-` ID; rotate it from the dashboard if it is lost (see [hub-deployment.md](hub-deployment.md#self-service-registration)). | | `HIVE_SESSION_KEY` | No | derived from `HIVE_HUB_SECRET` | Spoke session-cookie signing sub-key. | +| `HIVE_CUSTOMER_MATCH_KEY` | No | derived from `HIVE_HUB_SECRET` and `HIVE_ID` | Per-hive key the Customers hub import salts GitHub login hashes with (label `hive-customer-match-v1`). Never the heartbeat bearer. Without it and without `HIVE_HUB_SECRET` the import asks nothing. | | `HIVE_INVITE_KEY` | No | derived from `HIVE_HUB_SECRET` | Per-hive contributor-invite signing key. Symmetric: the spoke both mints and verifies invite tokens with it. | | `HIVE_TERMINAL_KEY` | No | self-derived per-hive from `HIVE_HUB_SECRET` + `HIVE_ID` | Per-hive terminal-assertion signing key. It never falls back to a fleet-uniform key. | | `HIVE_SSO_PUBLIC_KEY` | No | none | Ed25519 **public** key a spoke verifies hub-minted SSO handoff tokens with. Holding only the public key, a spoke can verify but cannot mint. | diff --git a/src/docs/operator-reference/README.md b/src/docs/operator-reference/README.md index 1652661339..b1d9e18905 100644 --- a/src/docs/operator-reference/README.md +++ b/src/docs/operator-reference/README.md @@ -225,6 +225,7 @@ Top-level YAML keys accepted by `config.Config`: | `customers` | Customers: one profile per person built from what the hive already holds, kept in `/data/customers/people.json` (0600) with a rebuildable edge index in `/data/graph/customers.db`. Dashboard users and people added by hand are customers; anyone else is only a suggestion until accepted. Nothing it stores leaves the hive. Design: [customers](../design/customers.md). | Default ON (`enabled: true`); `enabled: false` (or `HIVE_CUSTOMERS_ENABLED=false`) opens no store, runs no sweep and hides the section. | | `customers.inference` | Goal and satisfaction suggestions (see `customers`): the hive reads a customer's own issues, pull requests and approved mail, asks the model configured for meetings for candidate goals and a satisfaction level with the evidence behind each, and files them as suggestions for a person to confirm, edit or reject. A confirmed or manual value is never overwritten. | Default OFF (`enabled: false`) because it sends that text to the model; `enabled: true` (or `HIVE_CUSTOMERS_INFERENCE_ENABLED=true`) turns it on. Needs `customers.enabled` and a meetings model (OpenAI-compatible or Anthropic); budget about 40,000 tokens a day. | | `customers.avatars` | GitHub pictures on the Customers list. The viewer's browser requests `https://github.com/.png` (no referrer) for each person with a confirmed GitHub login on screen; nobody known only by email is looked up anywhere, and the hive neither proxies nor stores pictures. See [customers](../design/customers.md). | Default ON (`enabled: true`); `enabled: false` (or `HIVE_CUSTOMERS_AVATARS_ENABLED=false`, or Settings, Features) shows initials and makes no request to github.com. | +| `customers.hub_import` | Import of the real name, organization and country a confirmed customer entered on the hub and consented to share with this hive. The hive posts salted hashes of confirmed customers' GitHub logins (never a suggestion, never plaintext) hourly and on accept. A hub value shows "from the hub", outranks the GitHub profile and never overwrites a value set by hand; a withdrawal or three days without confirmation clears it. See [customers-hub-consent](../design/customers-hub-consent.md). | Default OFF; `enabled: true` (or `HIVE_CUSTOMERS_HUB_IMPORT_ENABLED=true`, or Settings, Features) also needs a hub registration. | | `customers.mention_search` | Step E of Customers: when an owner accepts a suggested GitHub login, the hive makes one budgeted GitHub search (issues and pull requests that mention the login, and ones the login commented on, in the hive's own repositories) so the new profile starts with history beyond what the hive already holds. The results become `mentioned_in` and `commented_on` edges. Never run by the discovery sweep, and never for an email address. | Default OFF (`enabled: false`) because it sends the login to GitHub and spends shared API quota; `enabled: true` (or `HIVE_CUSTOMERS_MENTION_SEARCH_ENABLED=true`) turns it on. Needs `customers.enabled`. `max_requests_per_hour` (default 10, at most 60) bounds all accepts together; each accept spends at most 2 requests and keeps at most 60 results. It stands down while the API budget is below normal or the secondary-limit caution window is engaged. | | `governor.first_timer_welcome` | First-time contributor welcome: when a human whose GitHub `author_association` is `FIRST_TIME_CONTRIBUTOR`/`FIRST_TIMER` opens a PR, post one comment (per author per repo, marker ``) that thanks them and invites them to run their own hive — profile, leaderboard, trust-tier progression, offline review-fix/rebase handling. Bots, `author/clanker`/`author/hive` PRs, drafts and known relay contributors are skipped. | Default ON; `enabled: false` opts out; `template_path` overrides the text. Silent without a public dashboard URL. See [Welcoming first-time contributors](../good-first-issue.md#welcoming-first-time-contributors). | | `sla` | Delivery SLA targets (`sla.targets.start_to_pr_h`, `pr_to_merge_h`, `open_to_merge_h`, hours; defaults 4 / 24 / 72) the dashboard's Throughput section colours its Delivery SLA spans against. | Metrics are derived from the lifecycle timeline; no extra GitHub calls. Also editable from **Settings → Thresholds**. See [Delivery SLA](sweeps-and-operations.md#delivery-sla). | diff --git a/src/pkg/config/customers.go b/src/pkg/config/customers.go index 0c3066388c..ba7a227e08 100644 --- a/src/pkg/config/customers.go +++ b/src/pkg/config/customers.go @@ -26,6 +26,31 @@ type CustomersConfig struct { // ON. It is the one part of the section that makes the viewer's browser // contact another site: github.com, for people already on screen. Avatars CustomersAvatarsConfig `yaml:"avatars,omitempty" json:"avatars,omitempty"` + // HubImport gates the import of the real name, organization and country + // a person entered on the hub and consented to share with this hive + // (#19163). Default OFF. + HubImport CustomersHubImportConfig `yaml:"hub_import,omitempty" json:"hub_import,omitempty"` +} + +// EnvCustomersHubImportEnabled overrides customers.hub_import.enabled when it +// holds a recognised boolean. +const EnvCustomersHubImportEnabled = "HIVE_CUSTOMERS_HUB_IMPORT_ENABLED" + +// CustomersHubImportConfig (`customers.hub_import:`) lets this hive ask its +// hub for fields its confirmed customers entered there themselves. On, the +// hive posts salted hashes of its confirmed customers' GitHub logins to the +// hub on a bounded interval and when a customer is accepted; the hub answers +// only for people who consented to share with this hive. The switch is +// necessary and never sufficient: the person's consent on the hub decides +// what arrives. See src/docs/design/customers-hub-consent.md. +type CustomersHubImportConfig struct { + // Enabled turns the import on. nil = false. + Enabled *bool `yaml:"enabled,omitempty" json:"enabled,omitempty"` +} + +// IsEnabled reports whether the import is on (default off). +func (c CustomersHubImportConfig) IsEnabled() bool { + return c.Enabled != nil && *c.Enabled } // EnvCustomersAvatarsEnabled overrides customers.avatars.enabled when it @@ -114,6 +139,24 @@ func (c *Config) CustomersAvatarsEnabled() bool { return c.Customers.Avatars.IsEnabled() } +// CustomersHubImportEnabled reports whether the hub import is switched on: +// the HIVE_CUSTOMERS_HUB_IMPORT_ENABLED env wins over +// customers.hub_import.enabled when it holds a recognised boolean. It also +// needs the Customers section to be on. A hub registration is checked by the +// caller, which holds it. +func (c *Config) CustomersHubImportEnabled() bool { + if !c.CustomersEnabled() { + return false + } + if v, ok := parseBoolEnv(EnvCustomersHubImportEnabled); ok { + return v + } + if c == nil { + return false + } + return c.Customers.HubImport.IsEnabled() +} + // EnvCustomersMentionSearchEnabled overrides customers.mention_search.enabled // when it holds a recognised boolean. const EnvCustomersMentionSearchEnabled = "HIVE_CUSTOMERS_MENTION_SEARCH_ENABLED" diff --git a/src/pkg/config/customers_test.go b/src/pkg/config/customers_test.go index c548fb29f2..12be1034cb 100644 --- a/src/pkg/config/customers_test.go +++ b/src/pkg/config/customers_test.go @@ -167,3 +167,35 @@ func TestCustomersMentionSearchRequestsPerHour(t *testing.T) { }) } } + +func TestCustomersHubImportEnabled(t *testing.T) { + t.Setenv(EnvCustomersEnabled, "") + t.Setenv(EnvCustomersHubImportEnabled, "") + cfg := &Config{} + if cfg.CustomersHubImportEnabled() { + t.Fatal("hub import must default off") + } + off, on := false, true + cfg.Customers.HubImport.Enabled = &on + if !cfg.CustomersHubImportEnabled() { + t.Fatal("explicit true") + } + t.Setenv(EnvCustomersHubImportEnabled, "false") + if cfg.CustomersHubImportEnabled() { + t.Fatal("env false must win over config true") + } + cfg.Customers.HubImport.Enabled = &off + t.Setenv(EnvCustomersHubImportEnabled, "true") + if !cfg.CustomersHubImportEnabled() { + t.Fatal("env true must win over config false") + } + cfg.Customers.Enabled = &off + if cfg.CustomersHubImportEnabled() { + t.Fatal("hub import needs Customers on") + } + var none *Config + t.Setenv(EnvCustomersHubImportEnabled, "") + if none.CustomersHubImportEnabled() { + t.Fatal("a nil config has hub import off") + } +} diff --git a/src/pkg/config/testdata/agent_runtime_golden.json b/src/pkg/config/testdata/agent_runtime_golden.json index a52b7c543a..59494d3be9 100644 --- a/src/pkg/config/testdata/agent_runtime_golden.json +++ b/src/pkg/config/testdata/agent_runtime_golden.json @@ -791,6 +791,7 @@ "customers": { "inference": {}, "avatars": {}, + "hub_import": {}, "mention_search": {} }, "duplicate_sweep": {}, diff --git a/src/pkg/customers/affiliation.go b/src/pkg/customers/affiliation.go index 33e090f1b4..0a7212b6e9 100644 --- a/src/pkg/customers/affiliation.go +++ b/src/pkg/customers/affiliation.go @@ -125,7 +125,7 @@ func affiliationDismissed(pr Profile, key string) bool { // person has settled the field, nothing supports one, or the person refused it. func SuggestAffiliation(p Person) *AffiliationSuggestion { org := p.Profile.Organization - if org.Source == SourceManual { + if org.Source == SourceManual || org.Source == SourceHub { return nil } ok := func(v string) bool { diff --git a/src/pkg/customers/hub_import.go b/src/pkg/customers/hub_import.go new file mode 100644 index 0000000000..85b4a52cd8 --- /dev/null +++ b/src/pkg/customers/hub_import.go @@ -0,0 +1,440 @@ +package customers + +// Hub import (phase 3 of src/docs/design/customers-hub-consent.md, #19163). +// +// A person who entered their real name, organization or country on the hub, +// and consented to share them with this hive, can have them fill their +// profile here. The hive posts salted hashes of its confirmed customers' +// GitHub logins and the hub answers only for people who consented. The +// network call is the caller's HubMatcher; this file only decides what is +// asked and what an answer may change. +// +// Rules, each pinned by hub_import_test.go: +// - Off unless Enabled says on: off makes no request at all. +// - Only confirmed customers are asked about, never a suggestion, and only +// by a confirmed GitHub login. +// - Import never creates a person and touches nothing but the three fields. +// - manual is never overwritten; hub outranks auto. +// - A cleared entry, or a hub value not refreshed within HubImportMaxAge, +// removes only hub-provenance fields and hands them back to automatic. +// - A forgotten person is gone with a tombstone; import cannot rebuild them. +// - Reply values are untrusted: control and format characters stripped, +// length-capped, a country must be a known ISO code. + +import ( + "context" + "crypto/hmac" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "errors" + "io" + "sort" + "strings" + "time" + "unicode" + "unicode/utf8" +) + +const ( + // ProfileSourceHub is the Rule a hub-provenance field carries. + ProfileSourceHub = "hub_consent" + // HubImportInterval is how often the bounded import pass runs. + HubImportInterval = time.Hour + // HubImportMaxAge is how long a hub value is kept without being + // refreshed. Past it the value is cleared as if the hub had said so, so a + // hive that loses touch with the hub does not keep a withdrawn value. + HubImportMaxAge = 72 * time.Hour + // HubImportBatch is the most hashes one request carries; it matches the + // hub's maxCustomerMatchBatch. + HubImportBatch = 200 + // HubImportMaxRequests bounds the requests of one pass, below the hub's + // per-hive rate limit of six a minute. + HubImportMaxRequests = 4 + // HubImportMaxReply is the most reply bytes read; it matches the hub's + // response cap plus headroom. + HubImportMaxReply = 160 << 10 +) + +// Skip reasons of a hub import pass. +const ( + HubSkipDisabled = "disabled" + HubSkipNoKey = "no_match_key" + HubSkipNoPeople = "no_candidates" + HubSkipFailed = "request_failed" +) + +// Audit actions a hub import reports. +const ( + HubAuditImport = "customer_hub_import" + HubAuditClear = "customer_hub_clear" +) + +// HubFields are the only profile fields a hub import reads or writes. +var HubFields = []string{FieldRealName, FieldOrganization, FieldCountry} + +// HubMatchEntry is one answer entry, already reduced to what import reads. +type HubMatchEntry struct { + MatchHash string + RealName string + Organization string + Country string + Cleared []string +} + +// HubMatchReply is a decoded hub answer. +type HubMatchReply struct { + Matches []HubMatchEntry + Truncated bool +} + +// HubMatcher posts one batch of match hashes to the hub. +type HubMatcher interface { + Match(ctx context.Context, hashes []string) (HubMatchReply, error) +} + +// HubMatchHash is HMAC-SHA256(match key, lower-cased GitHub login) in hex, +// the hash the hub's match endpoint compares (pkg/hub customerMatchHash). +func HubMatchHash(key, login string) string { + login = strings.ToLower(strings.TrimSpace(login)) + if key == "" || login == "" { + return "" + } + mac := hmac.New(sha256.New, []byte(key)) + mac.Write([]byte(login)) + return hex.EncodeToString(mac.Sum(nil)) +} + +// DecodeHubMatchReply reads a hub answer. Only match_hash, the three field +// values and cleared are read; every other key is ignored, and each value is +// sanitised (SanitizeHubValue). Cleared names outside HubFields are dropped. +func DecodeHubMatchReply(r io.Reader) (HubMatchReply, error) { + var wire struct { + Matches []struct { + MatchHash string `json:"match_hash"` + RealName string `json:"real_name"` + Organization string `json:"organization"` + Country string `json:"country"` + Cleared []string `json:"cleared"` + } `json:"matches"` + Truncated bool `json:"truncated"` + } + if err := json.NewDecoder(io.LimitReader(r, HubImportMaxReply)).Decode(&wire); err != nil { + return HubMatchReply{}, err + } + out := HubMatchReply{Truncated: wire.Truncated} + for _, m := range wire.Matches { + e := HubMatchEntry{ + MatchHash: strings.TrimSpace(m.MatchHash), + RealName: SanitizeHubValue(m.RealName), + Organization: SanitizeHubValue(m.Organization), + Country: hubCountry(m.Country), + } + for _, c := range m.Cleared { + if contains(HubFields, c) && !contains(e.Cleared, c) { + e.Cleared = append(e.Cleared, c) + } + } + out.Matches = append(out.Matches, e) + } + return out, nil +} + +// SanitizeHubValue makes a hub value safe to store: control and format +// characters (including bidi overrides and zero-width marks) are removed, +// whitespace runs collapse to one space, and the result is cut on a rune +// boundary to MaxProfileFieldLen bytes. +func SanitizeHubValue(s string) string { + s = strings.Map(func(r rune) rune { + if r == utf8.RuneError || unicode.IsControl(r) || unicode.Is(unicode.Cf, r) { + if unicode.IsSpace(r) { + return ' ' + } + return -1 + } + return r + }, s) + s = strings.Join(strings.Fields(s), " ") + if len(s) <= MaxProfileFieldLen { + return s + } + cut := MaxProfileFieldLen + for cut > 0 && !utf8.RuneStart(s[cut]) { + cut-- + } + return strings.TrimSpace(s[:cut]) +} + +// hubCountry accepts only a known ISO 3166 alpha-2 code. +func hubCountry(s string) string { + s = strings.ToUpper(SanitizeHubValue(s)) + if len(s) != isoCodeLen || !ValidCountry(s) { + return "" + } + return s +} + +func (e HubMatchEntry) value(field string) string { + switch field { + case FieldRealName: + return e.RealName + case FieldOrganization: + return e.Organization + case FieldCountry: + return e.Country + } + return "" +} + +var errHubUnchanged = errors.New("customers: hub import changed nothing") + +// ApplyHubMatch applies one hub answer entry to an existing customer who +// still holds login as a confirmed GitHub identity. It never creates a +// person and touches only HubFields: a manual field is left alone, a cleared +// field that is hub provenance is emptied and handed back to automatic, and +// a value is written as hub provenance over an auto or empty field. A value +// the field already holds from the hub only refreshes its time. It returns +// the names it set and the names it cleared. +func (s *Store) ApplyHubMatch(id, login string, e HubMatchEntry) (set, cleared []string, err error) { + key := IdentityKey(KindGitHub, login) + _, err = s.changePerson(id, func(p *Person) error { + if p.Status != StatusCustomer || !p.HasIdentity(key) || s.tombs[s.tombstoneHash(key)] { + return ErrNotFound + } + now := s.now().UTC() + touched := false + for _, name := range HubFields { + f := p.Profile.Field(name) + if f.Source == SourceManual { + continue + } + if contains(e.Cleared, name) { + if f.Source == SourceHub { + *f = ProfileField{} + cleared = append(cleared, name) + touched = true + } + continue + } + v := e.value(name) + if name == FieldCountry { + v = hubCountry(v) + } else { + v = SanitizeHubValue(v) + } + if v == "" { + continue + } + if f.Source == SourceHub && f.Value == v { + f.At = now + touched = true + continue + } + *f = ProfileField{Value: v, Source: SourceHub, Rule: ProfileSourceHub, At: now} + set = append(set, name) + touched = true + } + if contains(cleared, FieldCountry) { + deriveAutoCountry(p, now) + } + if !touched { + return errHubUnchanged + } + return nil + }) + if errors.Is(err, errHubUnchanged) { + err = nil + } + return set, cleared, err +} + +// HubExpiry is one person's hub fields cleared for staleness. +type HubExpiry struct { + Person string + Fields []string +} + +// ExpireHubFields clears every hub-provenance field not refreshed within +// maxAge, the same way a cleared reply does, and reports what it cleared. +func (s *Store) ExpireHubFields(maxAge time.Duration) ([]HubExpiry, error) { + var out []HubExpiry + err := s.change(func() error { + now := s.now().UTC() + for id, p := range s.people { + var fields []string + for _, name := range HubFields { + f := p.Profile.Field(name) + if f.Source == SourceHub && now.Sub(f.At) > maxAge { + *f = ProfileField{} + fields = append(fields, name) + } + } + if len(fields) == 0 { + continue + } + if contains(fields, FieldCountry) { + deriveAutoCountry(&p, now) + } + p.UpdatedAt = now + s.people[id] = p + out = append(out, HubExpiry{Person: id, Fields: fields}) + } + if len(out) == 0 { + return errHubUnchanged + } + return nil + }) + if errors.Is(err, errHubUnchanged) { + err = nil + } + sort.Slice(out, func(i, j int) bool { return out[i].Person < out[j].Person }) + return out, err +} + +// HubImport runs the import. Every func is read at call time. +type HubImport struct { + Store *Store + Matcher HubMatcher + // Enabled is the spoke switch AND a hub registration. Off: no request. + Enabled func() bool + // Key is the per-hive match key; "" makes no request. + Key func() string + // Audit receives one call per import or clear: a person id, an action + // (HubAuditImport or HubAuditClear) and field names, never values. + Audit func(person, action string, fields []string) + // MaxAge overrides HubImportMaxAge (tests). + MaxAge time.Duration +} + +// HubImportResult counts what one pass did: counts only. +type HubImportResult struct { + Requests int `json:"requests"` + Asked int `json:"asked"` + Imported int `json:"imported"` + Cleared int `json:"cleared"` + Expired int `json:"expired"` + Skipped string `json:"skipped,omitempty"` +} + +type hubCandidate struct{ id, login string } + +// Run is one import pass. With only set, it asks about that one customer +// (the accept-time pull); otherwise about every confirmed customer with a +// GitHub login. Staleness expiry runs first and on every pass, switch on or +// off, because it makes no request and it is what removes a value the hive +// can no longer confirm. +func (h *HubImport) Run(ctx context.Context, only string) HubImportResult { + var res HubImportResult + if h == nil || h.Store == nil { + res.Skipped = HubSkipDisabled + return res + } + maxAge := h.MaxAge + if maxAge <= 0 { + maxAge = HubImportMaxAge + } + if exp, err := h.Store.ExpireHubFields(maxAge); err == nil { + for _, x := range exp { + res.Expired++ + h.audit(x.Person, HubAuditClear, x.Fields) + } + } + if h.Matcher == nil || h.Enabled == nil || !h.Enabled() { + res.Skipped = HubSkipDisabled + return res + } + key := "" + if h.Key != nil { + key = h.Key() + } + if key == "" { + res.Skipped = HubSkipNoKey + return res + } + byHash := h.candidates(key, only) + if len(byHash) == 0 { + res.Skipped = HubSkipNoPeople + return res + } + hashes := make([]string, 0, len(byHash)) + for hash := range byHash { + hashes = append(hashes, hash) + } + sort.Strings(hashes) + res.Asked = len(hashes) + for len(hashes) > 0 && res.Requests < HubImportMaxRequests { + batch := hashes + if len(batch) > HubImportBatch { + batch = batch[:HubImportBatch] + } + res.Requests++ + reply, err := h.Matcher.Match(ctx, batch) + if err != nil { + res.Skipped = HubSkipFailed + return res + } + last := -1 + pos := make(map[string]int, len(batch)) + for i, hash := range batch { + pos[hash] = i + } + for _, e := range reply.Matches { + i, asked := pos[e.MatchHash] + if !asked { + continue // never asked about: ignored + } + if i > last { + last = i + } + c := byHash[e.MatchHash] + set, cleared, err := h.Store.ApplyHubMatch(c.id, c.login, e) + if err != nil { + continue + } + if len(set) > 0 { + res.Imported++ + h.audit(c.id, HubAuditImport, set) + } + if len(cleared) > 0 { + res.Cleared++ + h.audit(c.id, HubAuditClear, cleared) + } + } + next := len(batch) + if reply.Truncated && last >= 0 && last+1 < len(batch) { + // The hub dropped entries after the last one it sent; ask + // again from there. + next = last + 1 + } + hashes = hashes[next:] + } + return res +} + +// candidates maps each confirmed customer's GitHub login hash to them. A +// suggestion, a claimed identity, a bot and a forgotten login are never +// hashed. +func (h *HubImport) candidates(key, only string) map[string]hubCandidate { + out := make(map[string]hubCandidate) + for _, p := range h.Store.People() { + if p.Status != StatusCustomer || only != "" && p.ID != only { + continue + } + for _, i := range p.Identities { + if i.Kind != KindGitHub || i.Claimed || IsBotLogin(i.Value) || h.Store.Forgotten(KindGitHub, i.Value) { + continue + } + login := NormalizeIdentity(KindGitHub, i.Value) + if hash := HubMatchHash(key, login); hash != "" { + out[hash] = hubCandidate{id: p.ID, login: login} + } + } + } + return out +} + +func (h *HubImport) audit(person, action string, fields []string) { + if h.Audit != nil && len(fields) > 0 { + h.Audit(person, action, append([]string(nil), fields...)) + } +} diff --git a/src/pkg/customers/hub_import_test.go b/src/pkg/customers/hub_import_test.go new file mode 100644 index 0000000000..836b1382b4 --- /dev/null +++ b/src/pkg/customers/hub_import_test.go @@ -0,0 +1,407 @@ +package customers + +import ( + "context" + "errors" + "strconv" + "strings" + "testing" + "time" +) + +const testHubKey = "0123456789abcdef0123456789abcdef" + +// fakeHub answers from a table keyed by login, so a test states what the hub +// says about a person without computing hashes by hand. +type fakeHub struct { + calls int + batches [][]string + byLogin map[string]HubMatchEntry + err error + // truncateAfter, when > 0, answers at most that many entries per call + // and sets Truncated when more matched. + truncateAfter int +} + +func (f *fakeHub) Match(_ context.Context, hashes []string) (HubMatchReply, error) { + f.calls++ + f.batches = append(f.batches, append([]string(nil), hashes...)) + if f.err != nil { + return HubMatchReply{}, f.err + } + want := map[string]HubMatchEntry{} + for login, e := range f.byLogin { + e.MatchHash = HubMatchHash(testHubKey, login) + want[e.MatchHash] = e + } + var out HubMatchReply + for _, h := range hashes { + if e, ok := want[h]; ok { + if f.truncateAfter > 0 && len(out.Matches) == f.truncateAfter { + out.Truncated = true + break + } + out.Matches = append(out.Matches, e) + } + } + return out, nil +} + +type hubAudit struct{ person, action, fields string } + +type hubFixture struct { + store *Store + hub *fakeHub + imp *HubImport + now *time.Time + on *bool + audit *[]hubAudit +} + +func newHubFixture(t *testing.T) *hubFixture { + t.Helper() + now := time.Date(2026, 11, 19, 10, 0, 0, 0, time.UTC) + on := true + st, err := Open(t.TempDir(), func() time.Time { return now }) + if err != nil { + t.Fatal(err) + } + var audit []hubAudit + hub := &fakeHub{byLogin: map[string]HubMatchEntry{}} + imp := &HubImport{ + Store: st, + Matcher: hub, + Enabled: func() bool { return on }, + Key: func() string { return testHubKey }, + Audit: func(person, action string, fields []string) { + audit = append(audit, hubAudit{person, action, strings.Join(fields, ",")}) + }, + } + return &hubFixture{store: st, hub: hub, imp: imp, now: &now, on: &on, audit: &audit} +} + +func (x *hubFixture) get(t *testing.T, id string) Person { + t.Helper() + p, err := x.store.Get(id) + if err != nil { + t.Fatal(err) + } + return p +} + +func TestHubImportOverwritesAutoButNeverManual(t *testing.T) { + x := newHubFixture(t) + ada := mustAdd(t, x.store, "ada", adaLogin) + if _, _, err := x.store.ApplyAutoProfile(ada.ID, map[string]string{ + FieldRealName: "ada from github", FieldOrganization: "github org", FieldLocation: "Berlin, Germany", + }); err != nil { + t.Fatal(err) + } + if _, err := x.store.SetProfileField(ada.ID, FieldOrganization, "Typed By Hand", "olivia"); err != nil { + t.Fatal(err) + } + x.hub.byLogin["ada"] = HubMatchEntry{RealName: "Ada Lovelace", Organization: "Hub Org", Country: "GB"} + + res := x.imp.Run(context.Background(), "") + if res.Requests != 1 || res.Imported != 1 || res.Skipped != "" { + t.Fatalf("result = %+v", res) + } + p := x.get(t, ada.ID) + if f := p.Profile.RealName; f.Value != "Ada Lovelace" || f.Source != SourceHub || f.Rule != ProfileSourceHub { + t.Fatalf("hub must overwrite auto: %+v", f) + } + if f := p.Profile.Organization; f.Value != "Typed By Hand" || f.Source != SourceManual { + t.Fatalf("manual must never be overwritten: %+v", f) + } + if f := p.Profile.Country; f.Value != "GB" || f.Source != SourceHub { + t.Fatalf("hub country must outrank the location-derived one: %+v", f) + } + if f := p.Profile.Location; f.Value != "Berlin, Germany" || f.Source != SourceAuto { + t.Fatalf("location is not a hub field and must be untouched: %+v", f) + } + + // The automatic source cannot take a hub field back. + if _, _, err := x.store.ApplyAutoProfile(ada.ID, map[string]string{FieldRealName: "other", FieldLocation: "Paris, France"}); err != nil { + t.Fatal(err) + } + p = x.get(t, ada.ID) + if p.Profile.RealName.Value != "Ada Lovelace" || p.Profile.Country.Value != "GB" { + t.Fatalf("auto overwrote hub: %+v", p.Profile) + } + // A hub organization is the person's own statement: no affiliation guess. + q := x.get(t, ada.ID) + q.Profile.Organization = ProfileField{Value: "Hub Org", Source: SourceHub} + if SuggestAffiliation(q) != nil { + t.Fatal("a hub organization must not be second-guessed") + } + // Audit: person id and field names only. + if len(*x.audit) != 1 || (*x.audit)[0] != (hubAudit{ada.ID, HubAuditImport, "real_name,country"}) { + t.Fatalf("audit = %+v", *x.audit) + } + for _, a := range *x.audit { + if strings.Contains(a.fields, "Ada") || strings.Contains(a.fields, "GB") { + t.Fatalf("audit carries a value: %+v", a) + } + } +} + +func TestHubImportEditMakesFieldManual(t *testing.T) { + x := newHubFixture(t) + ada := mustAdd(t, x.store, "ada", adaLogin) + x.hub.byLogin["ada"] = HubMatchEntry{RealName: "Ada Lovelace"} + x.imp.Run(context.Background(), "") + if _, err := x.store.SetProfileField(ada.ID, FieldRealName, "Countess Ada", "olivia"); err != nil { + t.Fatal(err) + } + x.hub.byLogin["ada"] = HubMatchEntry{RealName: "Ada L."} + x.imp.Run(context.Background(), "") + if f := x.get(t, ada.ID).Profile.RealName; f.Value != "Countess Ada" || f.Source != SourceManual { + t.Fatalf("an edited hub field must become manual and stay: %+v", f) + } + // A cleared reply does not remove a manual field either. + x.hub.byLogin["ada"] = HubMatchEntry{Cleared: []string{FieldRealName}} + x.imp.Run(context.Background(), "") + if f := x.get(t, ada.ID).Profile.RealName; f.Value != "Countess Ada" { + t.Fatalf("cleared removed a manual field: %+v", f) + } +} + +func TestHubImportNeverCreatesOrPromotes(t *testing.T) { + x := newHubFixture(t) + // A suggested person the hub would know about. + if _, err := x.store.Observe([]Observation{{Kind: KindGitHub, Value: "stranger", Predicate: PredAuthored, Artefact: IssueRef("o/r", 1)}}); err != nil { + t.Fatal(err) + } + // A claimed (unconfirmed) GitHub identity. + mallory := mustAdd(t, x.store, "mallory") + if _, err := x.store.changePerson(mallory.ID, func(p *Person) error { + p.Identities = append(p.Identities, Identity{Kind: KindGitHub, Value: "claimed", Claimed: true}) + return nil + }); err != nil { + t.Fatal(err) + } + x.hub.byLogin["stranger"] = HubMatchEntry{RealName: "Stranger"} + x.hub.byLogin["claimed"] = HubMatchEntry{RealName: "Claimed"} + x.hub.byLogin["nobody"] = HubMatchEntry{RealName: "Nobody"} + + res := x.imp.Run(context.Background(), "") + if x.hub.calls != 0 || res.Skipped != HubSkipNoPeople { + t.Fatalf("a suggestion or claim was asked about: calls=%d res=%+v", x.hub.calls, res) + } + if n := len(x.store.People()); n != 1 { + t.Fatalf("import created a person: %d people", n) + } + if len(x.store.Suggestions()) != 1 { + t.Fatal("import changed the suggestions") + } + + // An answer for a hash never asked about is ignored and creates no one. + ada := mustAdd(t, x.store, "ada", adaLogin) + before := x.get(t, ada.ID) + res = x.imp.Run(context.Background(), "") + if res.Imported != 0 || len(x.store.People()) != 2 { + t.Fatalf("result = %+v people=%d", res, len(x.store.People())) + } + for _, b := range x.hub.batches { + for _, h := range b { + if h == HubMatchHash(testHubKey, "stranger") || h == HubMatchHash(testHubKey, "claimed") { + t.Fatal("a suggested or claimed login was hashed") + } + } + } + after := x.get(t, ada.ID) + if len(after.Identities) != len(before.Identities) || len(after.Tags) != len(before.Tags) || after.Notes != before.Notes || after.Satisfaction.Level != before.Satisfaction.Level { + t.Fatal("import touched something other than the three fields") + } +} + +func TestHubImportSwitchOffMakesZeroRequests(t *testing.T) { + x := newHubFixture(t) + mustAdd(t, x.store, "ada", adaLogin) + x.hub.byLogin["ada"] = HubMatchEntry{RealName: "Ada Lovelace"} + *x.on = false + if res := x.imp.Run(context.Background(), ""); res.Skipped != HubSkipDisabled || res.Requests != 0 { + t.Fatalf("off result = %+v", res) + } + x.imp.Enabled = nil + x.imp.Run(context.Background(), "") + *x.on = true + x.imp.Enabled = func() bool { return *x.on } + x.imp.Key = func() string { return "" } + if res := x.imp.Run(context.Background(), ""); res.Skipped != HubSkipNoKey { + t.Fatalf("no key result = %+v", res) + } + if x.hub.calls != 0 { + t.Fatalf("switch off or no key made %d requests", x.hub.calls) + } +} + +func TestHubImportClearedRevertsToAutomatic(t *testing.T) { + x := newHubFixture(t) + ada := mustAdd(t, x.store, "ada", adaLogin) + if _, _, err := x.store.ApplyAutoProfile(ada.ID, map[string]string{FieldLocation: "Berlin, Germany"}); err != nil { + t.Fatal(err) + } + x.hub.byLogin["ada"] = HubMatchEntry{RealName: "Ada Lovelace", Organization: "Hub Org", Country: "GB"} + x.imp.Run(context.Background(), "") + x.hub.byLogin["ada"] = HubMatchEntry{Organization: "Hub Org", Cleared: []string{FieldRealName, FieldCountry, "notes", "email"}} + res := x.imp.Run(context.Background(), "") + if res.Cleared != 1 { + t.Fatalf("result = %+v", res) + } + p := x.get(t, ada.ID) + if p.Profile.RealName != (ProfileField{}) { + t.Fatalf("cleared real name not handed back: %+v", p.Profile.RealName) + } + if f := p.Profile.Country; f.Value != "DE" || f.Source != SourceAuto || f.Rule != CountryRuleLocation { + t.Fatalf("cleared country must return to the automatic one: %+v", f) + } + if f := p.Profile.Organization; f.Value != "Hub Org" || f.Source != SourceHub { + t.Fatalf("a field not named in cleared was removed: %+v", f) + } + last := (*x.audit)[len(*x.audit)-1] + if last != (hubAudit{ada.ID, HubAuditClear, "real_name,country"}) { + t.Fatalf("clear audit = %+v", last) + } +} + +func TestHubImportStalenessRevertsToAutomatic(t *testing.T) { + x := newHubFixture(t) + ada := mustAdd(t, x.store, "ada", adaLogin) + x.hub.byLogin["ada"] = HubMatchEntry{RealName: "Ada Lovelace", Organization: "Hub Org"} + x.imp.Run(context.Background(), "") + + // Refreshed within the max age: kept, its time moved on. + *x.now = x.now.Add(HubImportMaxAge - time.Hour) + x.hub.byLogin["ada"] = HubMatchEntry{RealName: "Ada Lovelace"} + x.imp.Run(context.Background(), "") + if f := x.get(t, ada.ID).Profile.RealName; f.Source != SourceHub || !f.At.Equal(*x.now) { + t.Fatalf("a re-sent value was not refreshed: %+v", f) + } + + // The hub becomes unreachable. Organization was last refreshed at the + // start and now ages out; real name was refreshed and stays. + x.hub.err = errors.New("unreachable") + *x.now = x.now.Add(2 * time.Hour) + res := x.imp.Run(context.Background(), "") + if res.Expired != 1 || res.Skipped != HubSkipFailed { + t.Fatalf("result = %+v", res) + } + p := x.get(t, ada.ID) + if p.Profile.Organization != (ProfileField{}) || p.Profile.RealName.Value != "Ada Lovelace" { + t.Fatalf("profile = %+v", p.Profile) + } + // Turning the switch off does not keep a hub value forever. + *x.on = false + *x.now = x.now.Add(HubImportMaxAge + time.Minute) + x.imp.Run(context.Background(), "") + if p := x.get(t, ada.ID); p.Profile.RealName != (ProfileField{}) { + t.Fatalf("a stale hub value outlived the switch: %+v", p.Profile.RealName) + } + last := (*x.audit)[len(*x.audit)-1] + if last != (hubAudit{ada.ID, HubAuditClear, "real_name"}) { + t.Fatalf("expiry audit = %+v", last) + } +} + +func TestHubImportForgottenPersonNotRebuilt(t *testing.T) { + x := newHubFixture(t) + ada := mustAdd(t, x.store, "ada", adaLogin) + keep := mustAdd(t, x.store, "bob", Identity{Kind: KindGitHub, Value: "bob"}) + if _, err := x.store.Forget(ada.ID); err != nil { + t.Fatal(err) + } + x.hub.byLogin["ada"] = HubMatchEntry{RealName: "Ada Lovelace"} + x.hub.byLogin["bob"] = HubMatchEntry{RealName: "Bob"} + x.imp.Run(context.Background(), "") + if _, ok := x.store.Lookup(KindGitHub, "ada"); ok || len(x.store.People()) != 1 { + t.Fatal("import rebuilt a forgotten person") + } + for _, h := range x.hub.batches[0] { + if h == HubMatchHash(testHubKey, "ada") { + t.Fatal("a forgotten login was sent to the hub") + } + } + if !x.store.Forgotten(KindGitHub, "ada") { + t.Fatal("the tombstone is gone") + } + // Applying an answer directly to the forgotten id fails too. + if _, _, err := x.store.ApplyHubMatch(ada.ID, "ada", HubMatchEntry{RealName: "x"}); !errors.Is(err, ErrNotFound) { + t.Fatalf("ApplyHubMatch on a forgotten person = %v", err) + } + if x.get(t, keep.ID).Profile.RealName.Value != "Bob" { + t.Fatal("the remaining customer was not filled") + } +} + +func TestDecodeHubMatchReplyReadsOnlyThreeFieldsSanitised(t *testing.T) { + long := strings.Repeat("é", MaxProfileFieldLen) + body := `{"matches":[{"match_hash":"h1","rev":"r","real_name":"Ada\u0007 ‮Love​lace\n","organization":"` + long + `",` + + `"country":"gb","notes":"secret","email":"a@example.com","slack_id":"U1","affiliation":"x","location":"Mars",` + + `"cleared":["notes","real_name","email","real_name"]},{"match_hash":"h2","country":"Atlantis"}],"truncated":true,"extra":1}` + reply, err := DecodeHubMatchReply(strings.NewReader(body)) + if err != nil { + t.Fatal(err) + } + if !reply.Truncated || len(reply.Matches) != 2 { + t.Fatalf("reply = %+v", reply) + } + e := reply.Matches[0] + if e.RealName != "Ada Lovelace" { + t.Fatalf("control/format characters not stripped: %q", e.RealName) + } + if len(e.Organization) > MaxProfileFieldLen || !strings.HasPrefix(e.Organization, "éé") || strings.ContainsRune(e.Organization, '�') { + t.Fatalf("value not capped on a rune boundary: %d bytes", len(e.Organization)) + } + if e.Country != "GB" || reply.Matches[1].Country != "" { + t.Fatalf("country = %q / %q", e.Country, reply.Matches[1].Country) + } + if strings.Join(e.Cleared, ",") != "real_name" { + t.Fatalf("cleared = %v", e.Cleared) + } + // Values stored through ApplyHubMatch are sanitised even when a caller + // skips the decoder. + x := newHubFixture(t) + ada := mustAdd(t, x.store, "ada", adaLogin) + if _, _, err := x.store.ApplyHubMatch(ada.ID, "ada", HubMatchEntry{RealName: "A\x1b[31mda", Country: "zz"}); err != nil { + t.Fatal(err) + } + p := x.get(t, ada.ID) + if p.Profile.RealName.Value != "A[31mda" || p.Profile.Country.Value != "" { + t.Fatalf("stored = %+v", p.Profile) + } +} + +func TestHubImportBatchesAndFollowsTruncation(t *testing.T) { + x := newHubFixture(t) + for i := 0; i < HubImportBatch+5; i++ { + login := "user" + strings.Repeat("x", i%3) + string(rune('a'+i%26)) + strconv.Itoa(i) + mustAdd(t, x.store, login, Identity{Kind: KindGitHub, Value: login}) + if i%50 == 0 { + x.hub.byLogin[login] = HubMatchEntry{RealName: "Name " + strconv.Itoa(i)} + } + } + x.hub.truncateAfter = 2 + res := x.imp.Run(context.Background(), "") + if res.Imported != len(x.hub.byLogin) { + t.Fatalf("result = %+v, want %d imported", res, len(x.hub.byLogin)) + } + if res.Requests > HubImportMaxRequests { + t.Fatalf("pass made %d requests", res.Requests) + } + for _, b := range x.hub.batches { + if len(b) > HubImportBatch { + t.Fatalf("batch of %d", len(b)) + } + } +} + +func TestHubImportOnlyAsksAboutOnePersonOnAccept(t *testing.T) { + x := newHubFixture(t) + ada := mustAdd(t, x.store, "ada", adaLogin) + mustAdd(t, x.store, "bob", Identity{Kind: KindGitHub, Value: "bob"}) + x.imp.Run(context.Background(), ada.ID) + if x.hub.calls != 1 || len(x.hub.batches[0]) != 1 || x.hub.batches[0][0] != HubMatchHash(testHubKey, "ada") { + t.Fatalf("accept-time pull asked about %v", x.hub.batches) + } +} diff --git a/src/pkg/customers/model.go b/src/pkg/customers/model.go index 88d8c48273..dc551179fd 100644 --- a/src/pkg/customers/model.go +++ b/src/pkg/customers/model.go @@ -2,7 +2,8 @@ // customer, the identities that belong to them, their tags, goals and // satisfaction, and the edges that tie them to artefacts the hive already // holds. Nothing in this package talks to the network, and nothing it stores -// leaves the hive. +// leaves the hive; the opt-in hub import (hub_import.go) only sends salted +// hashes of confirmed GitHub logins, through a caller-supplied HubMatcher. // // Design record: src/docs/design/customers.md (RFC #18742). package customers @@ -64,6 +65,10 @@ type Source string const ( SourceAuto Source = "auto" SourceManual Source = "manual" + // SourceHub is a profile field the person entered on the hub and + // consented to share with this hive (hub_import.go). It outranks auto + // and never overwrites manual. + SourceHub Source = "hub" ) // Bounds on what one person may hold. They keep people.json small and stop @@ -285,9 +290,11 @@ var ProfileFields = []string{FieldRealName, FieldOrganization, FieldLocation, Fi const ProfileSourceGitHub = "github_profile" // ProfileField is one optional fact about a person with its provenance, in -// the shape of a tag's: Source is auto (filled from a public GitHub profile) -// or manual (a person typed it, possibly to clear it). A manual value always -// wins; "auto" hands the field back to the automatic source. +// the shape of a tag's: Source is auto (filled from a public GitHub profile), +// hub (the person entered it on the hub and consented to share it with this +// hive) or manual (a person typed it here, possibly to clear it). A manual +// value always wins, hub outranks auto; "auto" hands the field back to the +// automatic source. type ProfileField struct { Value string `json:"value,omitempty"` Source Source `json:"source,omitempty"` @@ -296,9 +303,9 @@ type ProfileField struct { By string `json:"by,omitempty"` } -// Profile holds the optional descriptive fields of a person. All of it is -// either the person's own public GitHub profile text or typed by a person; -// nothing here comes from the hub. +// Profile holds the optional descriptive fields of a person: the person's +// own public GitHub profile text, what they consented to share from the hub +// (customers.hub_import, off by default), or what a person typed. type Profile struct { RealName ProfileField `json:"real_name,omitempty"` Organization ProfileField `json:"organization,omitempty"` diff --git a/src/pkg/customers/store.go b/src/pkg/customers/store.go index 94bc1cbc63..7920efe675 100644 --- a/src/pkg/customers/store.go +++ b/src/pkg/customers/store.go @@ -569,11 +569,12 @@ func (s *Store) ResetProfileField(id, field string) (Person, error) { // deriveAutoCountry keeps an automatic country in step with the location: it // fills or updates it from DeriveCountry, and drops one the location no longer -// supports. A country a person set or cleared is never touched. It reports +// supports. A country a person set or cleared, or one from the hub, is never +// touched. It reports // whether anything changed. func deriveAutoCountry(p *Person, now time.Time) bool { c := &p.Profile.Country - if c.Source == SourceManual { + if c.Source == SourceManual || c.Source == SourceHub { return false } code := DeriveCountry(p.Profile.Location.Value) @@ -613,16 +614,16 @@ func (s *Store) DismissAffiliation(id, value string) (Person, error) { } // ApplyAutoProfile fills profile fields from a public GitHub profile. It -// only writes a field a person has not set (manual) and only with a -// non-empty value, derives the country from the location, and reports -// whether anything changed. +// only writes a field a person has not set (manual) and the hub did not fill +// (hub), only with a non-empty value, derives the country from the location, +// and reports whether anything changed. func (s *Store) ApplyAutoProfile(id string, values map[string]string) (Person, bool, error) { changed := false p, err := s.changePerson(id, func(p *Person) error { for _, name := range ProfileFields { f := p.Profile.Field(name) v, err := cleanText(values[name], MaxProfileFieldLen, false) - if err != nil || v == "" || f.Source == SourceManual || f.Value == v { + if err != nil || v == "" || f.Source == SourceManual || f.Source == SourceHub || f.Value == v { continue } *f = ProfileField{Value: v, Source: SourceAuto, Rule: ProfileSourceGitHub, At: s.now()} diff --git a/src/pkg/dashboard/api_governor_features.go b/src/pkg/dashboard/api_governor_features.go index 3cb39df9ef..60eed1ce8f 100644 --- a/src/pkg/dashboard/api_governor_features.go +++ b/src/pkg/dashboard/api_governor_features.go @@ -145,6 +145,9 @@ func (s *Server) handleGovernorFeatures(w http.ResponseWriter, r *http.Request) // customers.avatars.enabled: GitHub profile pictures on the // Customers list. Default on. CustomersAvatarsEnabled *bool `json:"customersAvatarsEnabled"` + // #19163: customers.hub_import.enabled, the import of fields a + // person consented on the hub to share with this hive. Default off. + CustomersHubImportEnabled *bool `json:"customersHubImportEnabled"` // #18884: customers.mention_search.enabled, the budgeted GitHub // search made when a suggested person is accepted. Default off. CustomersSearchEnabled *bool `json:"customersSearchEnabled"` @@ -381,6 +384,10 @@ func (s *Server) handleGovernorFeatures(w http.ResponseWriter, r *http.Request) on := *body.CustomersAvatarsEnabled cfg.Customers.Avatars.Enabled = &on } + if body.CustomersHubImportEnabled != nil { + on := *body.CustomersHubImportEnabled + cfg.Customers.HubImport.Enabled = &on + } if body.CustomersSearchEnabled != nil { on := *body.CustomersSearchEnabled cfg.Customers.MentionSearch.Enabled = &on @@ -593,6 +600,8 @@ func featuresSectionResponse(cfg *config.Config) map[string]interface{} { "customersInferenceEffective": cfg.CustomersInferenceEnabled(), "customersAvatars": cfg.Customers.Avatars.IsEnabled(), "customersAvatarsEffective": cfg.CustomersAvatarsEnabled(), + "customersHubImport": cfg.Customers.HubImport.IsEnabled(), + "customersHubImportEffective": cfg.CustomersHubImportEnabled(), "customersSearch": cfg.Customers.MentionSearch.IsEnabled(), "customersSearchEffective": cfg.CustomersMentionSearchEnabled(), "publicationEnabled": cfg.Publication.Enabled, diff --git a/src/pkg/dashboard/customers_ui_test.go b/src/pkg/dashboard/customers_ui_test.go index d1a6145e3f..bb34841984 100644 --- a/src/pkg/dashboard/customers_ui_test.go +++ b/src/pkg/dashboard/customers_ui_test.go @@ -580,3 +580,78 @@ func TestCustomersProfileDetailsWiring(t *testing.T) { t.Error("the Details block is not part of the profile") } } + +// The Features tab renders the hub import switch (#19163) beside Customer +// avatars, wired to the key the API reads, with the env override line. +func TestFeaturesTab_CustomersHubImportToggleMarkup(t *testing.T) { + html := featuresMarkup(t) + for _, want := range []string{ + `data-section="features" data-key="customersHubImportEnabled"`, + `yamlHint('customers.hub_import.enabled')`, + `f.customersHubImportEffective !== f.customersHubImport`, + `HIVE_CUSTOMERS_HUB_IMPORT_ENABLED`, + } { + if !strings.Contains(html, want) { + t.Errorf("index.html missing %q", want) + } + } + avatars := strings.Index(html, `data-key="customersAvatarsEnabled"`) + hub := strings.Index(html, `data-key="customersHubImportEnabled"`) + search := strings.Index(html, `data-key="customersSearchEnabled"`) + if avatars < 0 || hub < avatars || search < hub { + t.Error("the hub import switch must sit right after Customer avatars") + } +} + +// customers.hub_import.enabled: default off, owner-only, persisted, reported +// with the effective value and overridden by env. +func TestGovernorFeatures_CustomersHubImportToggleRoundTrip(t *testing.T) { + t.Setenv(config.EnvCustomersEnabled, "") + t.Setenv(config.EnvCustomersHubImportEnabled, "") + s := covApiServer(t) + type payload struct { + Features struct { + On bool `json:"customersHubImport"` + Effective bool `json:"customersHubImportEffective"` + } `json:"features"` + } + read := func() payload { + var p payload + if err := json.Unmarshal(doOwnerGet(s, "/api/config/governor").Body.Bytes(), &p); err != nil { + t.Fatal(err) + } + return p + } + if got := read(); got.Features.On || got.Features.Effective { + t.Fatalf("default payload = %+v, want off", got.Features) + } + if rec := putFeatures(s, map[string]any{"customersHubImportEnabled": true}, func(r *http.Request) { + r.Header.Set("X-Hive-Role", "read-write") + }); rec.Code != http.StatusForbidden || s.deps.Config.Customers.HubImport.IsEnabled() { + t.Fatalf("non-owner PUT = %d", rec.Code) + } + if rec := doPut(s, "/api/config/governor/features", map[string]any{"customersHubImportEnabled": true}); rec.Code != http.StatusOK { + t.Fatalf("PUT on: %d - %s", rec.Code, rec.Body.String()) + } + if got := read(); !got.Features.On || !got.Features.Effective { + t.Fatalf("after enable = %+v", got.Features) + } + t.Setenv(config.EnvCustomersHubImportEnabled, "false") + if got := read(); !got.Features.On || got.Features.Effective { + t.Fatalf("env override = %+v", got.Features) + } +} + +// A hub-provenance field says "from the hub" on the profile and the +// organization pill (#19163). +func TestCustomersProfileShowsHubProvenance(t *testing.T) { + html := indexHTML(t) + for _, want := range []string{ + `cur.source === 'hub' ? 'from the hub'`, + `f.source === 'hub' ? 'From the hub`, + } { + if !strings.Contains(html, want) { + t.Errorf("Customers UI lacks %q", want) + } + } +} diff --git a/src/pkg/dashboard/static-src/js/config-features.js b/src/pkg/dashboard/static-src/js/config-features.js index a43ece5e45..aa063d821b 100644 --- a/src/pkg/dashboard/static-src/js/config-features.js +++ b/src/pkg/dashboard/static-src/js/config-features.js @@ -900,6 +900,11 @@ Customer avatars ${yamlHint('customers.avatars.enabled')} ${settingHelpMark('settings', 'features', 'Customer avatars', `ON by default. The Customers list shows a person's GitHub picture next to their name when they have a confirmed GitHub login. This is the one thing in Customers that leaves your browser: it asks github.com for https://github.com/.png, once per person on screen, and sends no referrer. Nothing goes to the hive's server or the hub, and nobody known only by email is looked up anywhere. Off, the list shows initials and makes no request to github.com. Applies on the next refresh. HIVE_CUSTOMERS_AVATARS_ENABLED overrides it. See src/docs/design/customers.md.`)} ${f.customersAvatarsEffective !== f.customersAvatars ? `

HIVE_CUSTOMERS_AVATARS_ENABLED overrides this setting: avatars are currently ${f.customersAvatarsEffective ? 'on' : 'off'}.

` : ''} +
+
+ Import details people shared on the hub ${yamlHint('customers.hub_import.enabled')} ${settingHelpMark('settings', 'features', 'Customer hub import', `OFF by default. When on, and this hive is registered with a hub, the hive asks the hub about its confirmed customers about once an hour and when a person is accepted. The request carries salted hashes of their GitHub logins and nothing else: no names, no emails, never a suggested person. The hub answers only for people who chose, on the hub, to share their real name, organization or country with this hive, and only values they typed themselves. Those fields show "from the hub". Editing one here makes it yours and the hub never changes it again; a value set by hand is never overwritten. When a person withdraws, or the hub has not confirmed a value for three days, it is removed and the field goes back to automatic. A forgotten person is never rebuilt. The import never creates a person. This switch is needed but never enough: each person's consent on the hub decides what arrives. Needs Customers on. HIVE_CUSTOMERS_HUB_IMPORT_ENABLED overrides it. See src/docs/design/customers-hub-consent.md.`)} +
+ ${f.customersHubImportEffective !== f.customersHubImport ? `

HIVE_CUSTOMERS_HUB_IMPORT_ENABLED (or turning Customers off) overrides this setting: the hub import is currently ${f.customersHubImportEffective ? 'on' : 'off'}.

` : ''}
Search GitHub when accepting a person ${yamlHint('customers.mention_search.enabled')} ${settingHelpMark('settings', 'features', 'Customer GitHub search on accept', `OFF by default. When on, accepting a suggested GitHub login makes one budgeted GitHub search (issues and pull requests that mention the login and ones it commented on, in this hive's own repositories) so the new profile starts with history beyond what the hive already holds. The results are stored as references only, never text. It is never run by the discovery sweep and never for an email address. It sends the login to GitHub, makes at most two search requests per accept, shares an hourly request budget across accepts (customers.mention_search.max_requests_per_hour in the config file, default 10), counts against the shared GitHub quota, and stands down while that quota is low or GitHub's secondary limit is engaged. Needs Customers on. HIVE_CUSTOMERS_MENTION_SEARCH_ENABLED overrides it. See src/docs/design/customers.md.`)} diff --git a/src/pkg/dashboard/static-src/js/config-state.js b/src/pkg/dashboard/static-src/js/config-state.js index 935e451031..6bc177cbd7 100644 --- a/src/pkg/dashboard/static-src/js/config-state.js +++ b/src/pkg/dashboard/static-src/js/config-state.js @@ -114,6 +114,7 @@ { tab: 'Features', section: 'Agents & tooling', label: 'Customers', key: 'customers.enabled', text: 'customers people profile person identities tags goals satisfaction suggested forget customer graph HIVE_CUSTOMERS_ENABLED customers.enabled' }, { tab: 'Features', section: 'Agents & tooling', label: 'Customer goal and satisfaction suggestions', key: 'customers.inference.enabled', text: 'customers inference infer suggest goals satisfaction model confirm HIVE_CUSTOMERS_INFERENCE_ENABLED customers.inference.enabled' }, { tab: 'Features', section: 'Agents & tooling', label: 'Customer avatars', key: 'customers.avatars.enabled', text: 'customers avatars avatar pictures photos github profile picture privacy HIVE_CUSTOMERS_AVATARS_ENABLED customers.avatars.enabled' }, + { tab: 'Features', section: 'Agents & tooling', label: 'Customer hub import', key: 'customers.hub_import.enabled', text: 'customers hub import consent shared real name organization country from the hub privacy HIVE_CUSTOMERS_HUB_IMPORT_ENABLED customers.hub_import.enabled' }, { tab: 'Features', section: 'Agents & tooling', label: 'Customer GitHub search on accept', key: 'customers.mention_search.enabled', text: 'customers github search accept mention mentions comments history quota budget suggested person HIVE_CUSTOMERS_MENTION_SEARCH_ENABLED customers.mention_search.enabled' }, { tab: 'Features', section: 'Agents & tooling', label: 'Re-entrant turns and PR follow-up windows', key: 'turn.reentrant.enabled', text: 'reentrant re-entrant turn envelope background fleet pr follow-up resume window retention turn.reentrant.enabled turn.reentrant.background_fleet_enabled turn.pr_follow_up.max_age turn.pr_follow_up.resume_id_max_age turn.pr_follow_up.retention' }, { tab: 'Features', section: 'Agents & tooling', label: 'Data directories and session retention', key: 'data.metrics_dir', text: 'data directories metrics logs agent configs claude copilot bob sessions retention prune janitor data.agents_dir data.bob_sessions_dir data.claude_sessions_dir data.copilot_sessions_dir data.logs_dir data.metrics_dir data.session_retention_days' }, diff --git a/src/pkg/dashboard/static-src/js/customers.js b/src/pkg/dashboard/static-src/js/customers.js index 17449301a5..e4120077b8 100644 --- a/src/pkg/dashboard/static-src/js/customers.js +++ b/src/pkg/dashboard/static-src/js/customers.js @@ -216,8 +216,9 @@ const v = customersProfileValue(p, 'organization'); if (!v) return ''; const f = p.profile.organization; - const why = f.source === 'manual' ? 'Set by ' + (f.by || 'a person') : 'From the public GitHub profile'; - return '' + escapeHtml(v) + ''; + const why = f.source === 'manual' ? 'Set by ' + (f.by || 'a person') : (f.source === 'hub' ? 'From the hub: the person shared it with this hive' : 'From the public GitHub profile'); + const src = f.source === 'manual' || f.source === 'hub' ? f.source : 'auto'; + return '' + escapeHtml(v) + ''; } // customersFlagEmoji: the flag for an ISO 3166 alpha-2 code, or '' when the @@ -540,7 +541,9 @@ const rows = CUSTOMERS_PROFILE_FIELDS.map(function (f) { const cur = (p.profile && p.profile[f.key]) || {}; const manual = cur.source === 'manual'; - const mark = cur.value ? (manual ? 'set by hand' : (cur.rule === 'location' ? 'from location' : 'from GitHub')) : (manual ? 'cleared by hand' : 'not known'); + // A hub value is one the person shared from the hub (#19163); + // editing it here makes it manual. + const mark = cur.value ? (manual ? 'set by hand' : (cur.source === 'hub' ? 'from the hub' : (cur.rule === 'location' ? 'from location' : 'from GitHub'))) : (manual ? 'cleared by hand' : 'not known'); return '' + (manual ? '' : ''); diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index 93c226d943..f69b920f56 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -54412,8 +54412,9 @@ const v = customersProfileValue(p, 'organization'); if (!v) return ''; const f = p.profile.organization; - const why = f.source === 'manual' ? 'Set by ' + (f.by || 'a person') : 'From the public GitHub profile'; - return '' + escapeHtml(v) + ''; + const why = f.source === 'manual' ? 'Set by ' + (f.by || 'a person') : (f.source === 'hub' ? 'From the hub: the person shared it with this hive' : 'From the public GitHub profile'); + const src = f.source === 'manual' || f.source === 'hub' ? f.source : 'auto'; + return '' + escapeHtml(v) + ''; } // customersFlagEmoji: the flag for an ISO 3166 alpha-2 code, or '' when the @@ -54736,7 +54737,9 @@ const rows = CUSTOMERS_PROFILE_FIELDS.map(function (f) { const cur = (p.profile && p.profile[f.key]) || {}; const manual = cur.source === 'manual'; - const mark = cur.value ? (manual ? 'set by hand' : (cur.rule === 'location' ? 'from location' : 'from GitHub')) : (manual ? 'cleared by hand' : 'not known'); + // A hub value is one the person shared from the hub (#19163); + // editing it here makes it manual. + const mark = cur.value ? (manual ? 'set by hand' : (cur.source === 'hub' ? 'from the hub' : (cur.rule === 'location' ? 'from location' : 'from GitHub'))) : (manual ? 'cleared by hand' : 'not known'); return '' + (manual ? '' : ''); @@ -64877,6 +64880,7 @@

Configure LiteLLM

{ tab: 'Features', section: 'Agents & tooling', label: 'Customers', key: 'customers.enabled', text: 'customers people profile person identities tags goals satisfaction suggested forget customer graph HIVE_CUSTOMERS_ENABLED customers.enabled' }, { tab: 'Features', section: 'Agents & tooling', label: 'Customer goal and satisfaction suggestions', key: 'customers.inference.enabled', text: 'customers inference infer suggest goals satisfaction model confirm HIVE_CUSTOMERS_INFERENCE_ENABLED customers.inference.enabled' }, { tab: 'Features', section: 'Agents & tooling', label: 'Customer avatars', key: 'customers.avatars.enabled', text: 'customers avatars avatar pictures photos github profile picture privacy HIVE_CUSTOMERS_AVATARS_ENABLED customers.avatars.enabled' }, + { tab: 'Features', section: 'Agents & tooling', label: 'Customer hub import', key: 'customers.hub_import.enabled', text: 'customers hub import consent shared real name organization country from the hub privacy HIVE_CUSTOMERS_HUB_IMPORT_ENABLED customers.hub_import.enabled' }, { tab: 'Features', section: 'Agents & tooling', label: 'Customer GitHub search on accept', key: 'customers.mention_search.enabled', text: 'customers github search accept mention mentions comments history quota budget suggested person HIVE_CUSTOMERS_MENTION_SEARCH_ENABLED customers.mention_search.enabled' }, { tab: 'Features', section: 'Agents & tooling', label: 'Re-entrant turns and PR follow-up windows', key: 'turn.reentrant.enabled', text: 'reentrant re-entrant turn envelope background fleet pr follow-up resume window retention turn.reentrant.enabled turn.reentrant.background_fleet_enabled turn.pr_follow_up.max_age turn.pr_follow_up.resume_id_max_age turn.pr_follow_up.retention' }, { tab: 'Features', section: 'Agents & tooling', label: 'Data directories and session retention', key: 'data.metrics_dir', text: 'data directories metrics logs agent configs claude copilot bob sessions retention prune janitor data.agents_dir data.bob_sessions_dir data.claude_sessions_dir data.copilot_sessions_dir data.logs_dir data.metrics_dir data.session_retention_days' }, @@ -79848,6 +79852,11 @@

${esc(s.title)}

Customer avatars ${yamlHint('customers.avatars.enabled')} ${settingHelpMark('settings', 'features', 'Customer avatars', `ON by default. The Customers list shows a person's GitHub picture next to their name when they have a confirmed GitHub login. This is the one thing in Customers that leaves your browser: it asks github.com for https://github.com/.png, once per person on screen, and sends no referrer. Nothing goes to the hive's server or the hub, and nobody known only by email is looked up anywhere. Off, the list shows initials and makes no request to github.com. Applies on the next refresh. HIVE_CUSTOMERS_AVATARS_ENABLED overrides it. See src/docs/design/customers.md.`)}
${f.customersAvatarsEffective !== f.customersAvatars ? `

HIVE_CUSTOMERS_AVATARS_ENABLED overrides this setting: avatars are currently ${f.customersAvatarsEffective ? 'on' : 'off'}.

` : ''} +
+
+ Import details people shared on the hub ${yamlHint('customers.hub_import.enabled')} ${settingHelpMark('settings', 'features', 'Customer hub import', `OFF by default. When on, and this hive is registered with a hub, the hive asks the hub about its confirmed customers about once an hour and when a person is accepted. The request carries salted hashes of their GitHub logins and nothing else: no names, no emails, never a suggested person. The hub answers only for people who chose, on the hub, to share their real name, organization or country with this hive, and only values they typed themselves. Those fields show "from the hub". Editing one here makes it yours and the hub never changes it again; a value set by hand is never overwritten. When a person withdraws, or the hub has not confirmed a value for three days, it is removed and the field goes back to automatic. A forgotten person is never rebuilt. The import never creates a person. This switch is needed but never enough: each person's consent on the hub decides what arrives. Needs Customers on. HIVE_CUSTOMERS_HUB_IMPORT_ENABLED overrides it. See src/docs/design/customers-hub-consent.md.`)} +
+ ${f.customersHubImportEffective !== f.customersHubImport ? `

HIVE_CUSTOMERS_HUB_IMPORT_ENABLED (or turning Customers off) overrides this setting: the hub import is currently ${f.customersHubImportEffective ? 'on' : 'off'}.

` : ''}
Search GitHub when accepting a person ${yamlHint('customers.mention_search.enabled')} ${settingHelpMark('settings', 'features', 'Customer GitHub search on accept', `OFF by default. When on, accepting a suggested GitHub login makes one budgeted GitHub search (issues and pull requests that mention the login and ones it commented on, in this hive's own repositories) so the new profile starts with history beyond what the hive already holds. The results are stored as references only, never text. It is never run by the discovery sweep and never for an email address. It sends the login to GitHub, makes at most two search requests per accept, shares an hourly request budget across accepts (customers.mention_search.max_requests_per_hour in the config file, default 10), counts against the shared GitHub quota, and stands down while that quota is low or GitHub's secondary limit is engaged. Needs Customers on. HIVE_CUSTOMERS_MENTION_SEARCH_ENABLED overrides it. See src/docs/design/customers.md.`)} diff --git a/src/pkg/hub/customer_match_spoke_parity_test.go b/src/pkg/hub/customer_match_spoke_parity_test.go new file mode 100644 index 0000000000..6d47c59a7a --- /dev/null +++ b/src/pkg/hub/customer_match_spoke_parity_test.go @@ -0,0 +1,42 @@ +package hub + +import ( + "testing" + + "github.com/hivecommons/hive/pkg/customers" + "github.com/hivecommons/hive/pkg/hub/spoke" +) + +// The spoke's hub import (#19163) must derive the same match key and hash +// the same way as the hub's match endpoint, or no consented person ever +// matches. +func TestCustomerMatchSpokeParity(t *testing.T) { + const master, hive = "test-master-secret", "hive-a" + t.Setenv(spoke.EnvCustomerMatchKey, "") + t.Setenv("HIVE_HUB_SECRET", master) + t.Setenv(spoke.EnvHiveID, hive) + key := spoke.SpokeCustomerMatchKey("") + if key == "" || key != customerMatchKey(master, hive) { + t.Fatalf("spoke-derived match key %q != hub key %q", key, customerMatchKey(master, hive)) + } + if key == derivePerHiveKey(master, infoHeartbeatKey, hive) { + t.Fatal("the match key must not be the heartbeat bearer") + } + for _, login := range []string{"ada", " Ada ", "OctoCat"} { + if got, want := customers.HubMatchHash(key, login), customerMatchHash(key, login); got != want || !validCustomerMatchHash(got) { + t.Fatalf("hash(%q): spoke %q, hub %q", login, got, want) + } + } + if got := spoke.SpokeCustomerMatchKey("hive-b"); got != customerMatchKey(master, "hive-b") { + t.Fatalf("an explicit hive id must win over HIVE_ID: %q", got) + } + t.Setenv(spoke.EnvCustomerMatchKey, "injected") + if spoke.SpokeCustomerMatchKey("") != "injected" { + t.Fatal("an injected key must win") + } + t.Setenv(spoke.EnvCustomerMatchKey, "") + t.Setenv("HIVE_HUB_SECRET", "") + if spoke.SpokeCustomerMatchKey("") != "" { + t.Fatal("no master and no injected key must give no key") + } +} diff --git a/src/pkg/hub/spoke/keys.go b/src/pkg/hub/spoke/keys.go index 546866f038..b4cdb3cce9 100644 --- a/src/pkg/hub/spoke/keys.go +++ b/src/pkg/hub/spoke/keys.go @@ -14,11 +14,13 @@ import ( const ( infoHeartbeatKey = "hive-heartbeat-v1" infoInviteKey = "hive-invite-v1" + infoCustomerMatchKey = "hive-customer-match-v1" infoSSOEd25519Seed = "hive-sso-ed25519-v1" EnvHeartbeatKey = "HIVE_HEARTBEAT_KEY" EnvSSOPublicKey = "HIVE_SSO_PUBLIC_KEY" EnvSSOPublicKeyPrevious = "HIVE_SSO_PUBLIC_KEY_PREV" EnvInviteKey = "HIVE_INVITE_KEY" + EnvCustomerMatchKey = "HIVE_CUSTOMER_MATCH_KEY" EnvHiveID = "HIVE_ID" ) @@ -90,6 +92,22 @@ func SpokeInviteKey() string { return derivePerHiveKey(strings.TrimSpace(os.Getenv("HIVE_HUB_SECRET")), infoInviteKey, strings.TrimSpace(os.Getenv(EnvHiveID))) } +// SpokeCustomerMatchKey is the per-hive key the Customers hub import salts +// login hashes with (#19163): HIVE_CUSTOMER_MATCH_KEY when injected, else +// derived from HIVE_HUB_SECRET and hiveID (HIVE_ID when hiveID is empty) +// under the hub's match label (pkg/hub infoCustomerMatchKey). It is never the +// heartbeat bearer, and "" when neither source is configured, which makes the +// import ask nothing. +func SpokeCustomerMatchKey(hiveID string) string { + if v := strings.TrimSpace(os.Getenv(EnvCustomerMatchKey)); v != "" { + return v + } + if hiveID = strings.TrimSpace(hiveID); hiveID == "" { + hiveID = strings.TrimSpace(os.Getenv(EnvHiveID)) + } + return derivePerHiveKey(strings.TrimSpace(os.Getenv("HIVE_HUB_SECRET")), infoCustomerMatchKey, hiveID) +} + func SpokeHeartbeatKey() string { if v := strings.TrimSpace(os.Getenv(EnvHeartbeatKey)); v != "" { return v From 8b7f5db9ea3895f2104243d2b90745b946142ca6 Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Thu, 19 Nov 2026 06:36:53 -0500 Subject: [PATCH 2/2] [scanner] fix: inventory the customers hub match sender in the outbound-site test Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- src/pkg/prosefilter/outbound_sites_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pkg/prosefilter/outbound_sites_test.go b/src/pkg/prosefilter/outbound_sites_test.go index 55843aff84..fc67aefde9 100644 --- a/src/pkg/prosefilter/outbound_sites_test.go +++ b/src/pkg/prosefilter/outbound_sites_test.go @@ -167,6 +167,7 @@ var outboundSites = map[string]outboundSite{ "mailin.(*HubClient).do": machine(reasonSpokeToHub), "main.(*delegateDeciderRunner).RunDeciderTurn": machine(reasonModelPrompt), "main.(*knowledgeAdviserLLM).complete": machine(reasonModelPrompt), + "main.customersHubMatcher.Match": machine(reasonSpokeToHub), "main.kbPost": machine(reasonInternalAPI), "managedagents.(*Adapter).doJSON": machine(reasonModelPrompt), "meetings.(*Client).CompanionAudio": machine(reasonTranscribe),