diff --git a/.github/scripts/native-queue-policy.cjs b/.github/scripts/native-queue-policy.cjs index a54bcab393..9bd9b8df13 100644 --- a/.github/scripts/native-queue-policy.cjs +++ b/.github/scripts/native-queue-policy.cjs @@ -99,10 +99,25 @@ function membersForGroup(entries, group) { return members; } +// Name the condition that tripped: every one of these is a per-PR state the +// author or an operator must change, never a shared CI incident. An opaque +// "moved, draft, closed, or held" verdict across several held or draft PRs +// reads like a broken workflow and gets filed as one (#18128). +function ineligibleReason(pr, head, base) { + if (pr.state !== 'open') return `is ${pr.state}, not open`; + if (pr.draft) return 'is a draft'; + if (pr.head?.sha !== head) return `head moved to ${pr.head?.sha} (event head ${head})`; + if (pr.base?.ref !== base) return `base moved to ${pr.base?.ref} (event base ${base})`; + const held = (pr.labels ?? []).filter(l => BLOCKED_LABEL.test(l.name)).map(l => l.name); + if (held.length) return `is held by label ${held.join(', ')}`; + return null; +} + function assertEligible(pr, head, base) { - if (pr.state !== 'open' || pr.draft || pr.head?.sha !== head || - pr.base?.ref !== base || pr.labels?.some(l => BLOCKED_LABEL.test(l.name))) { - throw new Error(`#${pr.number}: head/base moved, draft, closed, or held`); + const reason = ineligibleReason(pr, head, base); + if (reason) { + throw new Error(`#${pr.number} ${reason}; not eligible for admission ` + + '(PR state, not a CI failure: re-evaluated on the next synchronize, ready_for_review or hold-label change)'); } } diff --git a/.github/scripts/native-queue-policy.test.cjs b/.github/scripts/native-queue-policy.test.cjs index 742d90ffa7..8e7d626d63 100644 --- a/.github/scripts/native-queue-policy.test.cjs +++ b/.github/scripts/native-queue-policy.test.cjs @@ -306,6 +306,19 @@ test('draft, closed, held and moved-head/base PRs cannot pass', () => { ]) assert.throws(() => assertEligible({ ...eligible(1), ...change }, sha(11), 'v5')); }); +test('ineligibility names the tripped condition, not a generic verdict (#18128)', () => { + const expect = (change, pattern) => + assert.throws(() => assertEligible({ ...eligible(1), ...change }, sha(11), 'v5'), pattern); + expect({ draft: true }, /#1 is a draft/); + expect({ state: 'closed' }, /#1 is closed, not open/); + expect({ head: { sha: sha(99) } }, new RegExp(`#1 head moved to ${sha(99)} \\(event head ${sha(11)}\\)`)); + expect({ base: { ref: 'v6' } }, /#1 base moved to v6 \(event base v5\)/); + expect({ labels: [{ name: 'size/L' }, { name: 'needs-human' }, { name: 'hold' }] }, + /#1 is held by label needs-human, hold/); + expect({ labels: [{ name: 'do-not-merge/work-in-progress' }] }, /PR state, not a CI failure/); + assert.doesNotThrow(() => assertEligible({ ...eligible(1), labels: [{ name: 'stage/070-fixing' }] }, sha(11), 'v5')); +}); + test('group evaluation checks each member head, all evidence, and live synthetic ref', async () => { const f = fixture(); assert.match(await evaluate(f), /all 2 member heads/); @@ -381,7 +394,7 @@ test('a 5xx from the API is retried as pending evidence, never read as a verdict test('open PR with moved head still fails closed on pull_request_target', async () => { const f = fixture('pull_request_target'); f.state.prs[0].head.sha = sha(99); - await assert.rejects(evaluate(f), /head\/base moved/); + await assert.rejects(evaluate(f), /#1 head moved to/); }); test('untrusted events and workflow provenance are rejected', async () => { diff --git a/CHANGELOG.md b/CHANGELOG.md index a6cea8e4e3..bdb9163ae0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,6 +66,260 @@ Hive did not historically maintain a complete changelog. This file starts a prag - The signed-commit reconciler's intentional refusal to rewrite a foreign or merge commit no longer files repeated unclassified-error fleet reports; its warning and PR remediation remain visible ([#17924](https://github.com/hivecommons/hive/issues/17924)). - Dashboard logs one summary warning per five minutes for dropped SSE events instead of one warning per event, so a stalled browser tab no longer triggers repeated-error fleet reports (#17995). +## 2026-11-14 (v5.256.1) + +### Fixed + +- Review verdicts discarded for lacking a head SHA and a dispatch are now logged at INFO instead of WARN, so they no longer trigger repeated-error self-reports. + +## 2026-11-14 (v5.256.0) + +### Added + +- Dashboard: Settings β†’ Repos β†’ Reference repos shows entries derived from an accepted request issue as read-only rows with a lock, a `from owner/repo#N` link to the issue, a credential-state chip (`write-tier`, `read-only`, `unresolved`) and the reason; `PUT /api/config/reference-repos` answers 409 to any attempt to edit, remove or replace a derived entry, naming the issue to change instead, while hand-written entries stay editable (#18161, part of #18139). + +### Security + +- The egress proxy now reloads `uid-map.json` when a UID or agent name is not in its copy, so an agent added at runtime is identified from its own socket and protected by the proxy-UID name-claim check ([#18133](https://github.com/hivecommons/hive/issues/18133)) without restarting the hive ([#18184](https://github.com/hivecommons/hive/issues/18184)). + +## 2026-11-14 (v5.255.0) + +### Added + +- The repos page's reference repo cards show an entry derived from an accepted request issue read-only: a lock, a `from owner/repo#N` link to the request issue and its credential chip (`πŸ”‘ App installation`, `read-only` with the reason, or `unresolved`), with no edit or remove control; `/api/status` `referenceRepos` entries now also carry `derived_from_url`, the request issue's link on the hive's GitHub host (#18139, #18161). +- Dashboard: Settings β†’ Repos has a Reference repos editor for `project.reference_repos`, so a hosted hive's owner can add, change and remove study-only repos without the config file; a save is refused with the loader's or boot's own message (including a write entry with no usable credential, or a `write_token_file` not already set in the file: that path stays operator-only) and otherwise applies to the GitHub proxy without a restart; derived entries are never written to the file (#18136). + +### Fixed + +- The self-host card on `/get-started`, `hub-deployment.md`, `register-self-hosted-hive.md` and the `HIVE_MIRROR_HUB_HIVE_ID` row in `env-vars.md` now show the self-service hive ID as `byo---`, the double-hyphen form that [#18166](https://github.com/hivecommons/hive/issues/18166) shipped, instead of the earlier single-hyphen form ([#18147](https://github.com/hivecommons/hive/issues/18147)). + +## 2026-11-14 (v5.254.0) + +### Added + +- A hive now derives write-tier `project.reference_repos` entries from an accepted request issue instead of needing the operator to hand-write them ([#18139](https://github.com/hivecommons/hive/issues/18139)). When an authorized repo's bound workflow model declares `intake.referenceRepoFields` (first: `cncf-techdocs-assessment@curated-2026.11.2`; hives bound to 11.1 must rebind), every repository the request form names in scope becomes an `issues+prs` reference repo once a person with write access applies `triage/accepted`; a bot applying the label grants nothing. The credential is the App installation on the requesting org, with a read-only fallback and one marked comment on the request issue when there is none, so a derived entry never fails boot. Entries are rebuilt at boot, on every config reload and every `HIVE_REFERENCE_REPOS_DERIVE_INTERVAL` (default `10m`), vanish when the issue closes or loses the label, are never written to any saved config, yield to a hand-written entry on the same repo, carry `(from owner/repo#N)` in the kick, and appear read-only in Settings with their source and credential state. Docs: `agent-configuration.md`, `operator-reference/README.md`, `design/workflow-models.md`, `workflows/techdocs-assessment.md` (#18157, #18158, #18159, #18160, #18161, #18162). +- Spoke: optional **report-only heartbeat mirror** to a second hub ([#18060](https://github.com/hivecommons/hive/issues/18060), [#18148](https://github.com/hivecommons/hive/issues/18148)). With `HIVE_MIRROR_HUB_URL` and `HIVE_MIRROR_HUB_HEARTBEAT_KEY` set (the key comes from registering the hive on that hub, for example through its **Register your own hive** form), every heartbeat the primary hub accepts is also POSTed, in its own goroutine with its own short timeout (`HIVE_MIRROR_HUB_TIMEOUT`, default 5s), to the mirror hub as a reduced copy: the dashboard token hash, the App and webhook key fingerprints, authentication error text, the signed-in user list and the activity digest are left out, and `HIVE_MIRROR_HUB_HIVE_ID` can override the reported `hive_id`. The mirror's response is never applied: upgrade, restart, branch switch, breaker, GitHub App config, funded gateway, project claim, access list, visibility and cluster id from the mirror are counted and ignored, and logged at most once per hour as `mirror_hub_response_ignored` (field names only). A mirror failure never affects the primary heartbeat, liveness or the hive. The primary key is never sent to the mirror; without a mirror key nothing is sent. `GET /api/health/deep` shows the mirror under `hub_heartbeat.mirror` (`url_host`, `last_ok`, `last_attempt`, `last_error`, `ignored_fields`, `last_ignored_fields`), host only, never the key. +- `project.reference_repos` entries are now derived from accepted request issues at boot, on every config reload and every `HIVE_REFERENCE_REPOS_DERIVE_INTERVAL` (default `10m`), in memory only: for each authorized repo whose bound workflow model declares `intake.referenceRepoFields`, the hive lists open issues carrying `triage/accepted`, reads who applied the label from the timeline and their repository permission, runs the request-form parser and installs the result on the proxy at the `issues+prs` tier (hand-written entries win on the same repo, authorized repos are never derived, duplicates keep the first). Closing the request issue or removing the label drops the entry, its proxy policy and its kick line on the next pass; a saved overlay or runtime config never contains a derived entry; the kick's REFERENCE REPOS block appends `(from owner/repo#N)` and, for a read-only derived entry, the reason (#18139, #18159). + +### Changed + +- The hub's `/get-started#self-host` card now describes self-service registration (sign in, Register your own hive, copy the one-time key, paste the Compose, Podman or Kubernetes snippet) instead of telling self-hosters to open an issue and obtain `HIVE_HUB_SECRET` from the operator. `hub-deployment.md` gains a Self-service registration section for operators, `env-vars.md` notes that `byo-` hives hold only `HIVE_HEARTBEAT_KEY`, and a new `register-self-hosted-hive.md` walks owners through the flow ([#18147](https://github.com/hivecommons/hive/issues/18147), part of [#18060](https://github.com/hivecommons/hive/issues/18060)). + +## 2026-11-14 (v5.253.0) + +### Added + +- Hub dashboard: **Register your own hive** is now a form when the hub runs with `HIVE_HUB_SELFSERVICE_REGISTRATION` ([#18060](https://github.com/hivecommons/hive/issues/18060), [#18145](https://github.com/hivecommons/hive/issues/18145)). Both CTAs (header and the "No hives yet" empty state) ask `GET /api/hub/selfservice/status` on load; with the flag off they keep linking to the self-host guide, with it on they open a modal that takes a name (shown with the fixed `byo--` prefix), org, one `owner/name` repository per line and an optional https dashboard URL, and posts to `/api/hub/selfservice/hives`. On success the heartbeat key is shown once with a copy button and a "copy before closing" warning, beside the hub URL and Docker Compose, Podman and Kubernetes snippets in tabs; the key lives only in the open dialog and is wiped from the DOM on close. Validation, not-an-admin (naming the refused repo), duplicate, per-user cap and GitHub-unreachable answers appear inline. A new **Self-hosted hives** section lists the signed-in user's own registrations with an online badge and last accepted heartbeat, **Rotate key** (confirm, then the new key once with the overlap note) and **Remove** (confirm, then revoke). +- A `project.reference_repos` entry derived from an accepted request issue resolves its credential from the GitHub App installation on the requesting org instead of refusing to boot: with an installation it keeps `write: [issues, prs]`, without one it is served read-only in memory and the request issue gets exactly one marked `hive:reference-repos-read-only` comment saying the hive can read but not open issues or PRs there (re-posted only when the read-only set changes, never on a plain reload). `write_token_file` is never consulted for a derived entry, a hand-written write-mode entry without a credential still exits 22, and `/api/status` `referenceRepos` entries now carry `derived_from`, `credential_state` (`write-tier`, `read-only`, `unresolved`) and `credential_reason` (#18139, #18158). +- The hub marketplace seeds a second curated TechDocs model, `cncf-techdocs-assessment@curated-2026.11.2`, identical to `curated-2026.11.1` except for a `spec.intake` block naming the assessment-request form headings ("Website or documentation repository", "Other repositories or sites in scope", "Out of scope") that yield derived write-tier reference repos once a maintainer accepts the request; `curated-2026.11.1` stays in the catalogue unchanged, so a hive still bound to it derives nothing until it rebinds (#18139, #18160). + +### Security + +- **Self-service hive registration hardening** ([#18166](https://github.com/hivecommons/hive/issues/18166), part of [#18060](https://github.com/hivecommons/hive/issues/18060)): `HIVE_HUB_SELFSERVICE_MAX_PER_USER` is now enforced again under the self-service store lock, so concurrent `POST /api/hub/selfservice/hives` requests from one user can no longer all pass the pre-ownership count and exceed the cap (the late refusal answers `429` and logs `selfservice_hive_refused` with `reason=per_user_cap`). New self-service IDs use `byo---` instead of `byo--`, so logins that contain hyphens can no longer collide or squat each other's IDs (`a-b` + `c` and `a` + `b-c` both used to become `byo-a-b-c`); a login containing `--` or starting or ending with `-` is refused with `reason=invalid_login`, and a name may not start with `-`. Hives registered before this change keep their existing IDs and keys. Repo references `.` and `..` (for example `owner/..`) are now refused everywhere repo refs are validated, so they can no longer be resolved as path segments in the GitHub ownership-check URL. + +## 2026-11-14 (v5.252.0) + +### Added + +- Hub: self-service (`byo-`) hives now expire. A background sweep (every `HIVE_HUB_SELFSERVICE_SWEEP_INTERVAL`, default 10m) revokes the key and removes the registry entry of any registration that went longer than `HIVE_HUB_SELFSERVICE_EXPIRY` (default 24h) without an accepted heartbeat, writing `audit: selfservice_hive_expired`. The hub also assigns every `byo-` registry entry the hive type `self-hosted` from its ID prefix, ignoring any `hive_type` the heartbeat claims, so the dashboard and `/api/hub/*` listings can badge them (#18146, part of #18060). + +## 2026-11-14 (v5.251.0) + +### Added + +- Workflow models gain `spec.intake.referenceRepoFields` / `outOfScopeFields`, `config.ReferenceRepo` gains a never-serialized `DerivedFrom` provenance, and the new `pkg/refderive` parser derives write-tier reference repos from an accepted assessment request issue, refusing with a typed reason when the request is closed, not `triage/accepted`, or accepted by a bot or a user without write access (#18139, #18157). +- Hub: signed-in users can register a hive they run themselves ([#18060](https://github.com/hivecommons/hive/issues/18060), [#18144](https://github.com/hivecommons/hive/issues/18144)). Behind `HIVE_HUB_SELFSERVICE_REGISTRATION` (default off), `POST /api/hub/selfservice/hives` builds the ID as `byo--`, proves `permissions.admin` on every listed repo with the caller's own GitHub token (refusing the whole registration by repo name otherwise), enforces `HIVE_HUB_SELFSERVICE_MAX_PER_USER` (default 5), and returns the heartbeat key once with Docker Compose, Podman and Kubernetes snippets; the owner can list, rotate and revoke their entries, a hub admin can revoke any, and every decision writes an `audit:` line without key material. The dashboard form follows in a later PR. + +## 2026-11-14 (v5.250.0) + +### Added + +- Hub: self-service (`byo-`) hives now have their own heartbeat credential ([#18060](https://github.com/hivecommons/hive/issues/18060), [#18142](https://github.com/hivecommons/hive/issues/18142)). The hub mints a random key per registered hive, stores only its SHA-256 hash in `/data/hub-selfservice.json`, and verifies a `byo-` heartbeat against that hash and nothing else: a master-derived key never authenticates a `byo-` ID, and a self-service key authenticates no other hive. Keys rotate with a 10-minute overlap so a redeploy does not show the hive offline, and each accepted heartbeat stamps the record for the coming expiry sweep. The registration API and UI follow in later PRs; nothing changes for hosted or operator-provisioned hives. + +### Fixed + +- The permissions watcher no longer attempts, or logs per path, a chown it can never perform. After the entrypoint's privilege drop the hive process runs as `dev` (uid 1001) without `CAP_CHOWN`, so an entry another uid owns, such as a sandbox kick workspace under `/data/agents/sandbox/-/.claude/skills/...` written by the Job pod's uid, always fails with `operation not permitted`; a surge of kicks minted ~4096 such paths between two ticks and the per-path deduper warned once for each of them (4087 `chown failed` WARN lines in ten minutes) before saturating. The watcher now decides ownership is unrepairable before the syscall, leaves the entry's mode alone as not ours, and reports all such entries as one summary line per tick: WARN with the count, a sample path and a hint when the condition appears or grows, DEBUG while it holds unchanged, nothing once it clears. Entries the process owns are still chowned and chmodded exactly as before (#18120). +- The stage engine now treats `architecture discussion` as a discussion label beside `proposal` and `kind/design`, and reads an `[RFC]` / `RFC:` title opener (after any leading emoji) as a discussion signal when no discussion label is present, so an unclaimed RFC derives `stage/018-open-for-discussion` (reason `open for discussion: architecture discussion` or `open for discussion: rfc title`) instead of showing as idle agent work at 010. A live claim or covering PR still stages 020 / 030 / 040 as before (#18141). +- The model-ranked knowledge advisor no longer burns its deliver arm on 429s from the model endpoint: a 429 is retried once after the upstream's `Retry-After` (a jittered 2 to 4 s backoff when absent) inside the existing `timeout_s`, a decision that still ends throttled is recorded as the new `rate-limited` outcome (distinct from `error`, excluded from the retro arm comparison) and is not charged against the per-agent-hour or per-day budgets, at most one advisor call is in flight across the whole hive on top of the per-agent guard, and the first 429 in each five-minute window logs at WARN with the `Retry-After`, `x-ratelimit-*` headers and response body while the rest log at DEBUG (#18151). + +## 2026-11-14 (v5.249.0) + +### Added + +- `config.ProjectConfig` gains a `ReferenceRepoTier(slug)` accessor (and `ReferenceRepo.Tier()`) returning a reference repo's write tier, so the proxy, kick renderer and dashboard share one source of truth (#18018). +- Dashboard Sessions backend (off by default, `dashboard.sessions.enabled` / `HIVE_SESSIONS_ENABLED`): named, shared, interactive CLI terminals behind `/api/sessions` (create, rename, close, resume, scrollback, input and upload), with per-session HOME and a credential-free environment, no permission-bypass flags, per-spoke and per-user limits, an idle sweeper and `session_*` audit entries; `/api/terminal/handoff` accepts `{"session":""}`. +- Dashboard: a new Sessions section (when sessions are enabled) with tabbed, named, grouped, shared CLI terminals, a send/attach input bar and a read-only scrollback view for the read role; it replaces the Inception wizard UI and keeps Campaigns and Jam on a tab. + +### Fixed + +- `native-queue-policy` now names the condition that made a PR ineligible for admission (`is a draft`, `is held by label needs-human`, `head moved to `, `base moved to v6`, `is closed`) instead of the single opaque `head/base moved, draft, closed, or held` line, and says it is PR state rather than a CI failure. Several held or draft PRs failing with the same generic text looked like a broken shared workflow and were filed as a shared-CI incident (#18128); the check's fail-closed verdict was correct on every one of them. +- **A per-agent overlay for a derived replica no longer crash-loops the hive** (#18137). Dashboard saves on a replica such as `reviewer-2` used to write `reviewer-2.yaml` into `data.agents_dir` as if it were a base agent, and the next restart failed with `replicas:2 would create reviewer-2, but an agent with that name already exists` before the dashboard could bind. Replica edits are now written to the base agent's overlay, and a stray `-N.yaml` already on disk is skipped at boot with an ERROR log naming the file, so the hive starts and the replica is re-derived from its base. + +## 2026-11-13 (v5.248.1) + +### Fixed + +- The inference translator no longer lets a caller at the shared proxy UID (`dev`, 1001 on v5 pods, where the hive process and every agent without a per-agent UID run) claim the `sk-hive-` key of an agent that has its own UID: such an agent always connects from that UID, so the claim is refused with `reason=proxy_uid_claims_isolated_agent` and the victim's route and upstream credential are never used. Names with no per-agent UID are still accepted from the proxy UID, root stays trusted, and `HIVE_PROXY_ADVISORY_OK` still accepts self-asserted identity only where the socket proves nothing, not when it proves the claim false (#18115). + +## 2026-11-13 (v5.248.0) + +### Added + +- **`review.perspectives_source` is documented.** `src/docs/review-swarm.md` now describes linking the review perspective set to a `kind: ReviewPerspectives` file in a GitHub repo (names and focus text only; `combined_perspectives`, `plan_match` and `max_perspectives_per_pr` stay operator-owned), the source-wins/inline-fallback rule, the last-known-good fallback order with a warning on a malformed document, the shared seed-only `github_prompt_allowlist` trust gate, and the read-only Review Gate display. `hive.yaml.example` gains a commented `perspectives_source:` block ([#17975](https://github.com/hivecommons/hive/issues/17975), [#17990](https://github.com/hivecommons/hive/issues/17990)). +- A voice connector (Plaud, WisprFlow) added, edited or removed from the Knowledge Integrations dashboard now shows up in, or leaves, the Inbox on the same save instead of after a restart, and the Inbox's direct Plaud read path is attached at boot too. When the saved connector list could not be applied to the running syncer, the save answers with `runtime_error` and the dashboard toasts `saved, but the running syncer did not reload: ...` instead of a plain success ([#18091](https://github.com/hivecommons/hive/issues/18091)). + +## 2026-11-13 (v5.247.2) + +### Fixed + +- Session prune now warns once per path when a session directory cannot be removed due to permission denied, instead of repeating the warning every tick. + +## 2026-11-13 (v5.247.1) + +### Fixed + +- Dashboard: an agent whose provider refused it for quota (for example Copilot's "You have exceeded your monthly quota") is now drawn red and labelled `out of quota` on the top-bar tile, the collapsed Agents tile and its agent card, instead of green "authenticated"; `/api/status` agent rows carry `quotaExhausted: true` from the existing per-agent quota flag. Login problems still show first, and disabled, paused and stopped agents keep their own look ([#18111](https://github.com/hivecommons/hive/issues/18111)). + +## 2026-11-13 (v5.247.0) + +### Added + +- Knowledge connectors added, edited, enabled, disabled or removed from the Knowledge Integrations dashboard now take effect without restarting the hive: a hive booted with no connectors builds and starts the syncer on the first save (Check, Sync and Connect stop answering `no knowledge connectors are configured`), and a running syncer is reconfigured in place so unchanged connectors keep syncing. The `knowledge.publish` mirror still needs a restart when its connector changes ([#18091](https://github.com/hivecommons/hive/issues/18091)). + +## 2026-11-13 (v5.246.1) + +### Fixed + +- Stop refusing inference requests from the hive's own UID (`dev`, 1001 on v5 pods) when private plain-HTTP forwarding is off, so agents without a per-agent UID reach their inference route again; the proxy UID is still refused when `HIVE_PROXY_ALLOW_PRIVATE_FORWARD=true` (#18061). +- The contributor-lane automerge sweep now treats a PR with more changed files than GitHub's files API returns (3000) as a standing `files-evidence` skip instead of re-warning every sweep. + +## 2026-11-13 (v5.246.0) + +### Added + +- Dashboard: an owner-only fuel gauge on the Overview now shows how much of the hive's Claude plan usage is left in each limit (weekly, plus a 5-hour or model-specific limit when the plan has one) and when each resets, turning amber at 25% left and red at 10%. It is on by default whenever the hive uses Claude, can be hidden from the Overview settings, and can also be shown as a header chip on every page with `dashboard.subscription_gauge_header`. It displays the usage readings hive already makes, so it adds no calls to Claude and changes nothing about measurement or provider rotation; `/api/providers/headroom` readings now also carry `plan_type` when a provider reports one. + +### Fixed + +- Close-on-merge no longer re-warns every tick for merged PRs it cannot read (404/403); it logs once per recheck window. +- **Agents out of provider quota are no longer kicked every cycle** ([#18112](https://github.com/hivecommons/hive/issues/18112)). The provider spending-limit gate only saw agents behind the hive's inference gateway, so an agent whose own provider refused it β€” GitHub Copilot's "You have exceeded your monthly quota" β€” kept being kicked and sent review work all night, each turn ending at once, until its review dispatches expired and pull requests were parked for a human. The governor now holds kicks per agent while that agent's `QuotaExhausted` flag is set, lets one probe kick through per `governor.provider_budget.probe_interval_s` (default 30 minutes) to notice when the quota resets, and resumes normal kicking as soon as the flag clears. Other agents are never held, and the "provider quota exhausted" alert now says the kicks are held and how often they are retried. + +## 2026-11-13 (v5.245.0) + +### Added + +- The hive image now ships the `spektacular` CLI (pinned 0.31.0, checksum-verified, amd64 and arm64), so the Inbox β†’ RFC destination runs `spektacular voice --publish` in-pod without an agent fallback (#17888). + +### Fixed + +- Completed Claude credential re-bridges (a token refresh replacing an agent's shared-login symlink, repaired automatically) no longer file `unclassified_error_repeated` fleet self-reports; failed repairs still do. +- Dashboard top bar: when the centre column is narrow, the agent run-order strip's summary pill is now a real button that opens the Agents section (click, Enter or Space), and it shows how many scheduled agents are in the run order ("9 agents", "1 agent") instead of a count capped at 3; the misleading "β–Ύ" caret is gone ([#18110](https://github.com/hivecommons/hive/issues/18110)). + +## 2026-11-13 (v5.244.1) + +### Fixed + +- Contributor-lane auto-promotion now warns once per refused author and once per refused repository when GitHub's search cannot see a repository, instead of once per author Γ— repository pair, so one invisible repository or one unsearchable author no longer floods WARN and self-reports as a repeated unclassified error (#18059). +- Knowledge Integrations: when the connector syncer is not running (`runtime: false`, hive booted with no `knowledge.connectors`), connector cards now show a `not loaded` badge with a restart hint and docs link instead of `pending`, and Connect / Check / Sync are disabled with the same hint rather than failing with 404 toasts; `pending` stays reserved for a loaded connector awaiting sign-in ([#18093](https://github.com/hivecommons/hive/issues/18093)). +- The ioscan classifier now retries a single transient model-endpoint failure (HTTP 429/5xx or a dropped connection) before applying its fail mode, so one gateway blip no longer passes untrusted text on unclassified or withholds it ([#18105](https://github.com/hivecommons/hive/issues/18105)). + +## 2026-11-13 (v5.244.0) + +### Added + +- Hub header nav and My Hives now link to the fleet benchmarks page (hivecommons.dev/benchmarks), and the spoke dashboard FAQ points at it for model comparisons (#18034). + +### Fixed + +- Knowledge Integrations: the per-connector **Check** button now reports its result β€” a success/info toast with the sign-in state (and the card refreshed), or an error toast carrying the server's `error` string verbatim (for example `no knowledge connectors are configured`) instead of a "checking" toast followed by silence ([#18092](https://github.com/hivecommons/hive/issues/18092)). + +## 2026-11-13 (v5.243.0) + +### Added + +- `/api/status` `referenceRepos[]` entries now carry an additive `reference_tier` field (`read-only`, `issues`, `prs` or `issues+prs`) so API consumers can tell a `project.reference_repos` entry from a managed repo; the entries stay out of `repos[]` and carry no ACMM, work-item or pause fields, and the dashboard's `πŸ“š reference Β· ` chip reads it ([#18021](https://github.com/hivecommons/hive/issues/18021)). + +### Changed + +- Dashboard: "Create webhook for me" is idempotent β€” it returns the existing hook for this hive's payload URL instead of creating a duplicate, and answers 412 naming the missing `organization_hooks`/`repository_hooks` write permission (#18028) +- **`review.perspectives_source` is now resolved at startup and on every config reload, and the review relay perspective set is reinstalled each time**, so a verdict naming a perspective the link removed is refused and one it added is accepted without a restart. A dashboard edit of the review config no longer drops the linked set, and a malformed or unreachable document keeps the last-known-good set with a warning. ([#17988](https://github.com/hivecommons/hive/issues/17988), part of [#17975](https://github.com/hivecommons/hive/issues/17975)) + +### Fixed + +- dashboard: a Projects-card Release that lands while the rows are re-sorting after a Hold is no longer sent, so holding items down a list no longer releases a neighbouring item's hold (#18054) +- Documents imported from the dashboard (PDF, HTML, DOCX, text) are now saved to `knowledge.documents` in `hive.yaml` like git sources, and boot re-imports any configured document whose on-disk record is missing, so an upgrade no longer drops a PDF knowledge source while keeping GitHub ones. + +### Security + +- The hub now withholds hub-held credentials and management actions (GitHub App config and keys, pending gateway, project config, cluster ID, access list, upgrade, restart and breaker instructions) from the heartbeat response of any hive whose ID is in the reserved `byo-` self-service namespace, through one guard driven by a classification of every response field ([#18060](https://github.com/hivecommons/hive/issues/18060)). + +## 2026-11-13 (v5.242.0) + +### Added + +- Activity monitor: a workflow-model chevron that is not the row's active stage now wears a glyph for what the stage means (inbox, bolt, hand, pencil, pull-request, flask, eye, people, thumbs-up, merge, check, …) picked by the column's name, dimmed to 60%, instead of its clipped label; the active chevron leads its word with the same glyph. Workflow columns take an optional `icon` (an allowlisted icon id or a single emoji, validated server- and client-side; never markup), and the workflow editor's column inspector previews the default and lets the operator replace it (#18033). + +### Changed + +- images: bump gh 2.103.3 -> 2.104.0 (#18063) +- kicks: register REFERENCE REPOS as layout section 10a and golden its wording (#18074) + +### Fixed + +- Benchmarks export now adds `model.canonical` and `model.effort` next to the reported id and derives `model.provider` from the model family, so `claude-sonnet-5-5` (Copilot CLI), `claude-sonnet-5.5` (Claude Code) and `openai-codex/gpt-6.1-sol` no longer appear as separate models on hivecommons.dev/benchmarks. +- Repeated trust-tier agent role claim rejections over contribute-ws are now logged once per user and role at INFO instead of flooding WARN, so they no longer surface as unclassified self-reported errors (#18071). + +## 2026-11-13 (v5.241.1) + +### Changed + +- **Settings β†’ Forge App β†’ GitHub webhooks names the App permission to grant** (#18029): when the GitHub App installation cannot manage hooks, the card now says which permission to grant (Organization β†’ Webhooks `organization_hooks`, or Repository β†’ Webhooks `repository_hooks`, Read and write) so **Create webhook for me** appears, instead of silently omitting the option. Node-executed tests pin the card's copy for relay, none and env-secret modes, and a parity test checks every endpoint the card calls is routed and owner-only. +- docs: project.reference_repos in agent-configuration and hive.yaml.example (#18073) + +## 2026-11-13 (v5.241.0) + +### Added + +- **Review Gate shows linked review perspectives read-only.** When `review.perspectives_source` links the perspective set to a repo file, Settings β†’ Features β†’ Review Gate β†’ Perspectives now shows the linked set (not the inline fallback), a "Linked to owner/repo/path@ref" line pointing at the file on GitHub, and the resolver's status and any fetch or malformed-document warning. The checkboxes and focus text are read-only and "Add perspective" is hidden, and `PUT /api/config/review` answers 409 for `perspectives` / `perspective_prompts` edits that the next reload would silently replace; `combined_perspectives`, `plan_match` and `max_perspectives_per_pr` stay editable. Unlinked hives see no change ([#17989](https://github.com/hivecommons/hive/issues/17989)). + +## 2026-11-13 (v5.240.0) + +### Added + +- **`project.reference_repos`: study-only repos outside the project** ([#18015](https://github.com/hivecommons/hive/issues/18015)). A hive whose deliverable is *about* another project (a docs assessment of `hivecommons/*` landing in `cncf/techdocs`) can list `owner/name` repos it may clone unauthenticated and GET, with the proxy stripping the hive's token and refusing every write by default; the kick tells agents such items are in scope and that findings go in the deliverable in the authorized repo, so the scanner no longer flags them out-of-scope/critical. A rare per-entry `write: [issues, prs]` opt-in allows only opening issues, opening PRs/pushing topic branches and commenting on the hive's own items, using the App's installation on the target org or `write_token_file`; boot refuses write-mode without either (exit 22). Dashboard shows a minimal `reference Β· ` card. +- Owner-facing GitHub webhook setup: βš™ Settings β†’ Forge App gains a "GitHub webhooks" section showing the payload URL, events and live receiver status, with **Generate secret** (stored in `/data/github-webhook-secret.key`, shown once) and **Create webhook for me** when the App installation has hooks write permission; a webhooks tile on the Governor card deep-links to it; new owner guide in `docs/webhooks.md` (#18017). + +## 2026-11-12 (v5.239.0) + +### Added + +- **Resolver for `review.perspectives_source`** that fetches a `kind: ReviewPerspectives` document behind the same seed-only allowlist as `definition_source`, and keeps the last-known-good set with a visible warning when a later fetch fails or a document no longer validates, instead of silently falling back to the built-in perspectives. The review relay, dispatch, head aggregation and `${REVIEW_PERSPECTIVES}` now read the resolved set via `ReviewConfig.EffectivePerspectives()`; with no `perspectives_source` configured, behaviour is unchanged. ([#17987](https://github.com/hivecommons/hive/issues/17987), part of [#17975](https://github.com/hivecommons/hive/issues/17975)) + +### Fixed + +- Activity monitor: the column header now follows the workflow model the rows wear β€” a repository bound to a custom model gets that model's column names over its chevrons (ungrouped pages are partitioned by model with a header per run), instead of hive-default's "PR in progress / Merge queue" names (#18002). +- Kicks are no longer refused wholesale as "prompt injection" because of the ioscan exfiltration canary: the preamble is now a labelled `=== HIVE HARNESS HEADER` block that states it is the operator's own configuration and not a task (Claude Sonnet 5 via the Copilot CLI refused ~65% of one hosted supervisor's kicks on the old `SECURITY CANARY:` marker), and a kick that is still refused with a canary in front is re-delivered once without it instead of burning the cadence slot (#18012). +- Stop contributor automerge passes cleanly when a client hand-over or shutdown cancels an in-flight sweep, without filing a spurious failure report. +- Spoke self-upgrade now installs the hosted template's startupProbe (10-min budget) on a hive Deployment that has none, not only raises an undersized one β€” pre-#9692 spokes on pull-only clusters were liveness-killed 90s into a forge-blocked boot and crash-looped (grey dot) for the length of the GHE outage; the fleet status-dot hovers now carry a colour legend and the grey dot says "no heartbeat in 5 min". + +## 2026-11-12 (v5.238.1) + +### Fixed + +- Spoke: a placeholder claimed onto a different forge than its pool default (GHE slot β†’ github.com repo) now moves GitHub App installation discovery to the delivered App and API URL even while installation_id is still 0, instead of polling the boot-time App/forge forever and reporting "App 5686 has no installation" until a pod restart. +- Hub fleet view: a placeholder assigned with an explicit forge now shows that forge (e.g. github.com) in the Location pill while the claim is pending, instead of the cluster default the spoke still heartbeats from its pre-claim config (github.ibm.com on vllm-d). +- Dashboard logs one summary warning per five minutes for dropped SSE events instead of one warning per event, so a stalled browser tab no longer triggers repeated-error fleet reports (#17995). + +## 2026-11-12 (v5.238.0) + +### Added + +- **`review.perspectives_source` config key and `kind: ReviewPerspectives` document format** for live-linking a hive's review perspective names and focus text to a file in a GitHub repo, so maintainers can change the set through ordinary PRs instead of hive config edits. This lands the config surface, a runtime-only resolved slot that is never written back to the overlay, and the validating document parser (`pkg/perspsrc`); fetching and wiring follow in later changes, so behaviour is unchanged until then and unchanged whenever the key is absent. Once resolved, the linked set wins over inline `review.perspectives` / `review.perspective_prompts`, which stay the fallback. ([#17986](https://github.com/hivecommons/hive/issues/17986), part of [#17975](https://github.com/hivecommons/hive/issues/17975)) + +### Fixed + +- The signed-commit reconciler's intentional refusal to rewrite a foreign or merge commit no longer files repeated unclassified-error fleet reports; its warning and PR remediation remain visible ([#17924](https://github.com/hivecommons/hive/issues/17924)). + ## 2026-11-12 (v6.27.3) ### Fixed diff --git a/bin/ttyd-tmux.sh b/bin/ttyd-tmux.sh index 36ce5a950f..70e219a48e 100755 --- a/bin/ttyd-tmux.sh +++ b/bin/ttyd-tmux.sh @@ -30,6 +30,24 @@ else SESSION="supervisor" SESSION_FROM_ARG=0 fi +# User sessions (dashboard Sessions feature): hive-sess- runs on its OWN +# tmux server whose socket is named after the session. Reject any other +# hive-sess-* shape so ?arg= cannot reach a glob or a path. +USER_SESSION_RE='^hive-sess-[a-f0-9]{8,32}$' +case "$SESSION" in + hive-sess-*) + if ! [[ "$SESSION" =~ $USER_SESSION_RE ]]; then + echo "error: invalid user session name" >&2 + exit 1 + fi + ;; +esac +case "$SESSION" in + hive-sess-*) + # Per-session tmux server: every tmux call below targets its socket. + tmux() { command tmux -L "$SESSION" "$@"; } + ;; +esac TTYD_WAIT_SEC="${HIVE_TTYD_WAIT_SEC:-900}" TTYD_POLL_SEC="${HIVE_TTYD_POLL_SEC:-2}" TTYD_HISTORY_LIMIT="${HIVE_TTYD_HISTORY_LIMIT:-50000}" diff --git a/changelog.d/changed-18193.md b/changelog.d/changed-18193.md new file mode 100644 index 0000000000..b535a1d877 --- /dev/null +++ b/changelog.d/changed-18193.md @@ -0,0 +1 @@ +- chore: top up v6 from v5 (2026-11-14) (#18193) diff --git a/dashboard/openapi.json b/dashboard/openapi.json index 34e8d69719..cd5c4e8712 100644 --- a/dashboard/openapi.json +++ b/dashboard/openapi.json @@ -84,6 +84,10 @@ "name": "Swarm", "description": "Repo swarm mode status, lifecycle, and history" }, + { + "name": "Sessions", + "description": "Interactive named CLI terminals" + }, { "name": "Workflows", "description": "Workflow models (RFC hivecommons/hive#17620): the spoke-local library of ingested/authored workflow definitions, per-repository bindings, and compilation. Writes are owner-gated and audited (workflow_save, workflow_delete, workflow_bind, workflow_unbind). Every route answers 503 when the hive has no workflow store." @@ -1832,6 +1836,10 @@ "backendAuthLastError": { "type": "string", "description": "The offending pane line that produced backendAuthStatus." + }, + "quotaExhausted": { + "type": "boolean", + "description": "True when the agent's pane shows a provider quota refusal (e.g. Copilot \"You have exceeded your monthly quota\"), from crux.Process.QuotaExhausted; covers every backend (#18111). Omitted when false." } } } @@ -3788,6 +3796,104 @@ } } }, + "/api/config/reference-repos": { + "get": { + "tags": [ + "System" + ], + "summary": "Reference repos", + "description": "Returns project.reference_repos and the write vocabulary the editor offers. Owner only.", + "responses": { + "200": { + "description": "Current reference repos", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "reference_repos": { + "type": "array", + "items": { + "type": "object" + } + }, + "write_kinds": { + "type": "array", + "items": { + "type": "string" + } + } + } + } + } + } + } + } + }, + "put": { + "tags": [ + "System" + ], + "summary": "Replace reference repos", + "description": "Replaces project.reference_repos after the same validation boot applies. Send an empty array to clear. Owner only.", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "required": [ + "reference_repos" + ], + "properties": { + "reference_repos": { + "type": "array", + "items": { + "type": "object" + } + } + } + } + } + } + }, + "responses": { + "200": { + "description": "Current reference repos", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "reference_repos": { + "type": "array", + "items": { + "type": "object" + } + }, + "write_kinds": { + "type": "array", + "items": { + "type": "string" + } + } + } + } + } + } + }, + "400": { + "description": "Invalid list; nothing was stored" + }, + "409": { + "description": "The list edits, removes or replaces an entry derived from a request issue (derived_from); the message names the issue to change instead. Nothing was stored" + }, + "500": { + "description": "The config file could not be written; the previous list is restored on the proxy and nothing changed" + } + } + } + }, "/api/config/stat-sources": { "get": { "tags": [ @@ -26341,6 +26447,284 @@ } } }, + "/api/config/github/webhooks": { + "get": { + "tags": [ + "System" + ], + "summary": "Describe GitHub webhook setup for this hive (owner)", + "description": "Owner-only. Everything the Settings β†’ Forge App β†’ GitHub webhooks section shows: the payload URL GitHub must call, the events to subscribe, whether a secret is configured and where it comes from (never the secret itself), whether this hive receives hub-signed relays (hosted), the live receiver health and whether the App installation's hooks permission allows creating the webhook from here. See docs/webhooks.md#setting-up-webhooks-owner-guide.", + "responses": { + "200": { + "description": "Webhook setup", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "payload_url": { + "type": "string" + }, + "payload_url_source": { + "type": "string", + "enum": [ + "config", + "hub", + "request", + "none" + ] + }, + "path": { + "type": "string" + }, + "content_type": { + "type": "string" + }, + "events": { + "type": "array", + "items": { + "type": "string" + } + }, + "optional_events": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "verification": { + "type": "string" + }, + "hosted": { + "type": "boolean" + }, + "secret_configured": { + "type": "boolean" + }, + "secret_source": { + "type": "string", + "enum": [ + "env", + "file", + "" + ] + }, + "secret_path": { + "type": "string" + }, + "can_generate_secret": { + "type": "boolean" + }, + "health": { + "type": "object", + "properties": { + "healthy": { + "type": "boolean" + }, + "last_event_at": { + "type": "string" + }, + "events_1h": { + "type": "integer" + }, + "invalidations_1h": { + "type": "integer" + }, + "targeted_refreshes_1h": { + "type": "integer" + }, + "tracker_invalidations_1h": { + "type": "integer" + }, + "last_ci_event_at": { + "type": "string" + }, + "ci_events_1h": { + "type": "integer" + }, + "ci_events_applied_1h": { + "type": "integer" + }, + "ci_refetches_1h": { + "type": "integer" + }, + "verification": { + "type": "string" + } + } + }, + "org": { + "type": "string" + }, + "repos": { + "type": "array", + "items": { + "type": "string" + } + }, + "app_org_hooks": { + "type": "boolean" + }, + "app_repo_hooks": { + "type": "boolean" + }, + "app_probe_error": { + "type": "string" + }, + "create_available": { + "type": "boolean" + }, + "docs_url": { + "type": "string" + } + } + } + } + } + }, + "403": { + "description": "Not an owner" + }, + "503": { + "description": "Config not loaded" + } + } + } + }, + "/api/config/github/webhooks/secret": { + "post": { + "tags": [ + "System" + ], + "summary": "Generate this hive's GitHub webhook secret (owner)", + "description": "Owner-only. Writes a new random secret to the hive's secret file (mode 0600) and returns it exactly once; later GETs only report that a file secret is configured. Refused with 409 while GITHUB_WEBHOOK_SECRET is set in the environment, which always wins. Audited.", + "responses": { + "200": { + "description": "Secret, shown once", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "secret": { + "type": "string" + }, + "secret_path": { + "type": "string" + }, + "verification": { + "type": "string" + }, + "note": { + "type": "string" + } + } + } + } + } + }, + "403": { + "description": "Not an owner" + }, + "409": { + "description": "Secret is supplied by the environment" + }, + "500": { + "description": "Could not write the secret file" + } + } + } + }, + "/api/config/github/webhooks/create": { + "post": { + "tags": [ + "System" + ], + "summary": "Create the org or repo webhook through the GitHub App (owner)", + "description": "Owner-only. Uses the App installation's organization_hooks or repository_hooks write permission to create a webhook on GitHub pointing at this hive's payload URL with the configured secret and event list. Requires a configured secret, a public dashboard URL and the matching permission; the repository must be one of project.repos. Idempotent: when a hook with the same payload URL already exists on the target it is returned with created=false instead of creating a duplicate. Creation is audited.", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "scope": { + "type": "string", + "enum": [ + "organization", + "repository" + ] + }, + "repo": { + "type": "string", + "description": "Repository name (bare or org/name); required for scope=repository." + } + } + } + } + } + }, + "responses": { + "200": { + "description": "Webhook created, or the existing hook for this payload URL", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "hook_id": { + "type": "integer", + "description": "Same as id" + }, + "created": { + "type": "boolean", + "description": "false when an existing hook for this payload URL was returned" + }, + "events": { + "type": "array", + "items": { + "type": "string" + } + }, + "payload_url": { + "type": "string" + }, + "scope": { + "type": "string" + }, + "target": { + "type": "string" + }, + "url": { + "type": "string" + } + } + } + } + } + }, + "400": { + "description": "Invalid scope or repository" + }, + "403": { + "description": "Not an owner" + }, + "409": { + "description": "No secret / no public URL / no project.org / no App auth" + }, + "412": { + "description": "The App installation lacks organization_hooks or repository_hooks write for the scope" + }, + "502": { + "description": "GitHub API error" + } + } + } + }, "/api/config/github": { "put": { "tags": [ @@ -42075,6 +42459,307 @@ } } } + }, + "/api/sessions": { + "get": { + "tags": [ + "Sessions" + ], + "summary": "List sessions, vendors and limits", + "description": "Any role. While the feature is off returns enabled=false with empty lists.", + "responses": { + "200": { + "description": "OK" + } + } + }, + "post": { + "tags": [ + "Sessions" + ], + "summary": "Create a session (owner or read-write)", + "description": "Starts a named CLI session. Vendor must be a known, allowed, installed backend; never a shell, never a bypass-permissions launch.", + "responses": { + "201": { + "description": "OK" + }, + "400": { + "description": "Invalid name, group or vendor" + }, + "403": { + "description": "Role or vendor not allowed" + }, + "404": { + "description": "Sessions disabled" + }, + "409": { + "description": "Vendor not installed" + }, + "429": { + "description": "Session limit reached" + } + }, + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "group": { + "type": "string" + }, + "vendor": { + "type": "string" + } + } + } + } + } + } + } + }, + "/api/sessions/{id}": { + "patch": { + "tags": [ + "Sessions" + ], + "summary": "Rename or regroup a session (creator or owner)", + "description": "Changes name and/or group.", + "responses": { + "200": { + "description": "OK" + }, + "403": { + "description": "Not the creator or an owner" + }, + "404": { + "description": "Unknown session" + } + }, + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "group": { + "type": "string" + } + } + } + } + } + }, + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ] + }, + "delete": { + "tags": [ + "Sessions" + ], + "summary": "Close a session (creator or owner)", + "description": "Kills tmux and keeps the metadata marked ended; on an ended session removes its metadata.", + "responses": { + "200": { + "description": "OK" + }, + "403": { + "description": "Not the creator or an owner" + }, + "404": { + "description": "Unknown session" + } + }, + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ] + } + }, + "/api/sessions/{id}/log": { + "get": { + "tags": [ + "Sessions" + ], + "summary": "Redacted scrollback snapshot (any role)", + "description": "lines is capped at 5000.", + "responses": { + "200": { + "description": "OK" + }, + "404": { + "description": "Unknown session" + } + }, + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ] + } + }, + "/api/sessions/{id}/input": { + "post": { + "tags": [ + "Sessions" + ], + "summary": "Type text into a session (owner or read-write)", + "description": "Literal paste, never key names. Audited with user and byte count, not the text.", + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Invalid text" + }, + "403": { + "description": "Role not allowed" + }, + "409": { + "description": "Session not running" + }, + "413": { + "description": "Input too large" + } + }, + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "text": { + "type": "string" + }, + "submit": { + "type": "boolean" + } + } + } + } + } + }, + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ] + } + }, + "/api/sessions/{id}/upload": { + "post": { + "tags": [ + "Sessions" + ], + "summary": "Upload a file into a session directory (owner or read-write)", + "description": "Multipart field file. Returns the absolute path; the file is never executed.", + "responses": { + "200": { + "description": "OK" + }, + "403": { + "description": "Role not allowed" + }, + "413": { + "description": "Upload too large" + }, + "429": { + "description": "Per-session file cap reached" + } + }, + "requestBody": { + "required": true, + "content": { + "multipart/form-data": { + "schema": { + "type": "object", + "properties": { + "file": { + "type": "string", + "format": "binary" + } + } + } + } + } + }, + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ] + } + }, + "/api/sessions/{id}/resume": { + "post": { + "tags": [ + "Sessions" + ], + "summary": "Resume an ended session (owner or read-write)", + "description": "Only for vendors with a resume handle.", + "responses": { + "200": { + "description": "OK" + }, + "403": { + "description": "Role not allowed" + }, + "409": { + "description": "Not ended or nothing to resume" + }, + "429": { + "description": "Session limit reached" + } + }, + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ] + } } }, "components": { diff --git a/src/Dockerfile b/src/Dockerfile index 20090bb380..8856da167e 100644 --- a/src/Dockerfile +++ b/src/Dockerfile @@ -246,9 +246,9 @@ RUN ARCH="${TARGETARCH:-$(dpkg --print-architecture)}" && \ chmod +x /usr/local/bin/ttyd # --- Layer 2b: GitHub CLI (off-PATH β€” only snapshot publisher uses it) --- -ARG GH_VERSION=2.103.3 -ARG GH_SHA256_AMD64=4c806978ffce1cec70853302a56d70ac6da4f19bae7adca4e1a8ab00633dd91d -ARG GH_SHA256_ARM64=ee32df2323c8847f5fac6cb6b4b69ca75828e9329a8f22f677021f7b3306ce63 +ARG GH_VERSION=2.104.0 +ARG GH_SHA256_AMD64=98cb101ad87dbbd3f6b2f2bfcbd43197ccd07643140121d8a9cfaa91eadbc24e +ARG GH_SHA256_ARM64=cb34cefc6f4e77b90731374a2b737516bb8f3033c6ca46aa949e5344f68069e3 RUN ARCH="${TARGETARCH:-$(dpkg --print-architecture)}" && \ case "$ARCH" in \ amd64) GH_ARCH=amd64; GH_SHA256="$GH_SHA256_AMD64" ;; \ @@ -325,7 +325,7 @@ RUN timeout 120s npm install -g --ignore-scripts --no-fund --no-audit \ # dashboard's codex model discovery (`codex app-server` model/list β€” pure # local stdio, no network, unaffected by the proxy CA) falls back to the # static list, and non-codex backends are unaffected. -ARG CODEX_VERSION=0.167.0 +ARG CODEX_VERSION=0.169.0 ARG CLINE_VERSION=3.0.74 # npm can briefly advertise AWS SDK versions before all registry replicas serve # them. Wait at most three minutes between attempts and revalidate metadata. @@ -343,7 +343,7 @@ RUN npm install -g --ignore-scripts --no-fund --no-audit @openai/codex@${CODEX_V echo "WARN: OpenAI Codex CLI install failed β€” skipping" # --- Layer 7: Claude Code (most frequent updates) --- -ARG CLAUDE_CODE_VERSION=2.1.322 +ARG CLAUDE_CODE_VERSION=2.1.325 # --ignore-scripts (kept, deliberately: no arbitrary postinstall runs during the # build) skips claude-code's install.cjs, which is what LINKS the platform # native binary into the package's bin/. The binary itself ships in the @@ -382,6 +382,33 @@ RUN ARCH="${TARGETARCH:-$(dpkg --print-architecture)}" && \ echo "WARN: Goose CLI download failed β€” skipping (backend: goose will be unavailable)"; \ fi +# --- Layer 8.1: spektacular CLI (Inbox -> RFC destination) --- +# Pinned hivecommons/spektacular release (includes `spektacular voice`, +# hivecommons/spektacular#123), SHA-256-verified before extraction, installed +# to /usr/local/bin/spektacular so inbox.spek.binary / runs.spektacular.binary +# resolve on PATH with no config. Download failures are tolerated (the Inbox +# falls back to inbox.spek.agent); checksum mismatches hard-fail the build. +# Bump note: see src/docs/cli-pins.md (spektacular row); update the version and +# both digests together from the release's checksums.txt. +ARG SPEKTACULAR_VERSION=0.31.0 +ARG SPEKTACULAR_SHA256_AMD64=21bd077b6723de41185fa657c0e54ea5726c1c2928794548ada4a8776a7b4746 +ARG SPEKTACULAR_SHA256_ARM64=81d3b92c907bd6d66e4e1db79df24be8b64803dd26e0de1d7147f1ff5fc9b8d0 +RUN ARCH="${TARGETARCH:-$(dpkg --print-architecture)}" && \ + case "$ARCH" in \ + amd64) SPEK_ARCH=x86_64; SPEK_SHA256="$SPEKTACULAR_SHA256_AMD64" ;; \ + arm64) SPEK_ARCH=arm64; SPEK_SHA256="$SPEKTACULAR_SHA256_ARM64" ;; \ + *) echo "unsupported arch for spektacular: $ARCH" >&2; exit 1 ;; \ + esac && \ + mkdir -p /tmp/spek-dl && \ + if curl -fsSL --retry 5 --retry-delay 5 --retry-connrefused "https://github.com/hivecommons/spektacular/releases/download/${SPEKTACULAR_VERSION}/spektacular_${SPEKTACULAR_VERSION}_linux_${SPEK_ARCH}.tar.gz" -o /tmp/spek-dl/spektacular.tar.gz; then \ + echo "${SPEK_SHA256} /tmp/spek-dl/spektacular.tar.gz" | sha256sum -c - && \ + tar xz -C /usr/local/bin -f /tmp/spek-dl/spektacular.tar.gz spektacular && \ + chmod +x /usr/local/bin/spektacular && \ + rm -rf /tmp/spek-dl; \ + else \ + echo "WARN: spektacular download failed - skipping (Inbox falls back to inbox.spek.agent)"; \ + fi + # --- Layer 8.25: Google Antigravity CLI (backend: agy) --- # The vendor distributes a self-contained Linux executable, not an npm package. # Pin the version, build ID, and SHA-512 from its public updater manifest; do @@ -412,9 +439,9 @@ RUN ARCH="${TARGETARCH:-$(dpkg --print-architecture)}" && \ # --- Layer 8.27: OMP CLI (Oh My Pi β€” backend: omp) --- # Mirrored from Dockerfile.contributor so hub agents can launch backend: omp. # Downloads are optional like Goose/agy, but checksum mismatches are fatal. -ARG OMP_VERSION=18.19.0 -ARG OMP_SHA256_AMD64=b44fda2ae5c9cce2bbe124d1672cba532a136d9e1eef68599b16efcd3bc5c626 -ARG OMP_SHA256_ARM64=61826bf89c2c0512417fee05fa2f486e60ee11b23ea95a17257a6c49edb6b8d5 +ARG OMP_VERSION=18.20.0 +ARG OMP_SHA256_AMD64=c8694be8c6e6a1f45e3636dd8a8e9eaa78878ad629a89000b403ec5e456d3f89 +ARG OMP_SHA256_ARM64=8b5e197db7c58b7ebabf83c6f03597f89bae3a766a84c6481b3a942a125f5e22 RUN ARCH="${TARGETARCH:-$(dpkg --print-architecture)}" \ && case "$ARCH" in \ amd64|x86_64) OMP_ASSET=omp-linux-x64; OMP_SHA256="$OMP_SHA256_AMD64" ;; \ diff --git a/src/Dockerfile.contributor b/src/Dockerfile.contributor index d45cd6f111..de9a2338d7 100644 --- a/src/Dockerfile.contributor +++ b/src/Dockerfile.contributor @@ -263,7 +263,7 @@ RUN mkdir -p /etc/apt/keyrings \ # npm cache is cleaned in the same layer it's populated (issue #2613, # Observation 2 "also minor") so the cache doesn't linger in this layer's # diff, mirroring the cleanup already done for the bobshell install below. -ARG CLAUDE_CODE_VERSION=2.1.322 +ARG CLAUDE_CODE_VERSION=2.1.325 ARG COPILOT_VERSION=1.0.88 # --ignore-scripts (kept, deliberately: no arbitrary postinstall runs during # the build) skips claude-code's install.cjs, which is what LINKS the platform @@ -286,7 +286,7 @@ ARG KILO_CLI_VERSION=7.5.6 # Same ARG name and value as src/Dockerfile so bin/test_backend_smoke.sh can # assert the two pins agree β€” this line was previously an inline hardcode that # could (and did) drift from the main image's CODEX_VERSION unnoticed. -ARG CODEX_VERSION=0.167.0 +ARG CODEX_VERSION=0.169.0 RUN npm install -g --ignore-scripts --no-fund --no-audit \ @anthropic-ai/claude-code@${CLAUDE_CODE_VERSION} \ @github/copilot@${COPILOT_VERSION} \ @@ -414,6 +414,33 @@ RUN ARCH="${TARGETARCH:-$(dpkg --print-architecture)}" \ echo "Goose install skipped"; \ fi +# spektacular CLI (Inbox -> RFC destination) --- +# Pinned hivecommons/spektacular release (includes `spektacular voice`, +# hivecommons/spektacular#123), SHA-256-verified before extraction, installed +# to /usr/local/bin/spektacular so inbox.spek.binary / runs.spektacular.binary +# resolve on PATH with no config. Download failures are tolerated (the Inbox +# falls back to inbox.spek.agent); checksum mismatches hard-fail the build. +# Bump note: see src/docs/cli-pins.md (spektacular row); update the version and +# both digests together from the release's checksums.txt. +ARG SPEKTACULAR_VERSION=0.31.0 +ARG SPEKTACULAR_SHA256_AMD64=21bd077b6723de41185fa657c0e54ea5726c1c2928794548ada4a8776a7b4746 +ARG SPEKTACULAR_SHA256_ARM64=81d3b92c907bd6d66e4e1db79df24be8b64803dd26e0de1d7147f1ff5fc9b8d0 +RUN ARCH="${TARGETARCH:-$(dpkg --print-architecture)}" && \ + case "$ARCH" in \ + amd64) SPEK_ARCH=x86_64; SPEK_SHA256="$SPEKTACULAR_SHA256_AMD64" ;; \ + arm64) SPEK_ARCH=arm64; SPEK_SHA256="$SPEKTACULAR_SHA256_ARM64" ;; \ + *) echo "unsupported arch for spektacular: $ARCH" >&2; exit 1 ;; \ + esac && \ + mkdir -p /tmp/spek-dl && \ + if curl -fsSL --retry 5 --retry-delay 5 --retry-connrefused "https://github.com/hivecommons/spektacular/releases/download/${SPEKTACULAR_VERSION}/spektacular_${SPEKTACULAR_VERSION}_linux_${SPEK_ARCH}.tar.gz" -o /tmp/spek-dl/spektacular.tar.gz; then \ + echo "${SPEK_SHA256} /tmp/spek-dl/spektacular.tar.gz" | sha256sum -c - && \ + tar xz -C /usr/local/bin -f /tmp/spek-dl/spektacular.tar.gz spektacular && \ + chmod +x /usr/local/bin/spektacular && \ + rm -rf /tmp/spek-dl; \ + else \ + echo "WARN: spektacular download failed - skipping (Inbox falls back to inbox.spek.agent)"; \ + fi + # Install agy (Google Antigravity CLI). Not an npm package β€” Google ships it # as a standalone binary from a versioned, checksummed GCS URL (the same URL # Homebrew's antigravity-cli cask resolves to, confirmed against @@ -489,9 +516,9 @@ RUN ARCH="${TARGETARCH:-$(dpkg --print-architecture)}" \ # No credential is baked in: omp reads the selected model's provider # credential from its documented environment variables (ANTHROPIC_API_KEY, # OPENAI_API_KEY, …), which the Justfile's container arm already forwards. -ARG OMP_VERSION=18.19.0 -ARG OMP_SHA256_AMD64=b44fda2ae5c9cce2bbe124d1672cba532a136d9e1eef68599b16efcd3bc5c626 -ARG OMP_SHA256_ARM64=61826bf89c2c0512417fee05fa2f486e60ee11b23ea95a17257a6c49edb6b8d5 +ARG OMP_VERSION=18.20.0 +ARG OMP_SHA256_AMD64=c8694be8c6e6a1f45e3636dd8a8e9eaa78878ad629a89000b403ec5e456d3f89 +ARG OMP_SHA256_ARM64=8b5e197db7c58b7ebabf83c6f03597f89bae3a766a84c6481b3a942a125f5e22 RUN ARCH="${TARGETARCH:-$(dpkg --print-architecture)}" \ && case "$ARCH" in \ amd64) OMP_ASSET=omp-linux-x64; OMP_SHA256="$OMP_SHA256_AMD64" ;; \ diff --git a/src/cmd/hive/boot.go b/src/cmd/hive/boot.go index fe4c8b2f07..7063af80d8 100644 --- a/src/cmd/hive/boot.go +++ b/src/cmd/hive/boot.go @@ -35,6 +35,7 @@ import ( "github.com/hivecommons/hive/pkg/mention" "github.com/hivecommons/hive/pkg/mergelane" "github.com/hivecommons/hive/pkg/notify" + "github.com/hivecommons/hive/pkg/perspsrc" "github.com/hivecommons/hive/pkg/planning" "github.com/hivecommons/hive/pkg/priorart" "github.com/hivecommons/hive/pkg/promptsrc" @@ -212,6 +213,7 @@ type boot struct { promptFetcher promptsrc.Fetcher defFetcher defsrc.Fetcher definitionResolver *defsrc.Resolver + perspectivesResolver *perspsrc.Resolver pendingTokenSeed []dashboard.TokenSparklineEntry pendingFactSeed []dashboard.FactHistoryEntry pendingCostSeed []dashboard.CostHistoryEntry @@ -236,14 +238,19 @@ type boot struct { saved *snapshot.PersistedState // bootKnowledge - gitSyncer *knowledge.GitSyncer - knowledgeConnectors *connector.Syncer - inboxSvc *inbox.Service - knowledgePublish *connector.Mirror - promotionScheduler *knowledge.PromotionScheduler - quotaAccount string - quotaPoolDir string - explicitQuotaPoolDir bool + gitSyncer *knowledge.GitSyncer + knowledgeConnectors *connector.Syncer + // knowledgeConnectorsMu guards knowledgeConnectors once the dashboard + // can hot-load it (#18091); startConnectorSyncer is the boot starter + // reused for a syncer first built after boot. + knowledgeConnectorsMu sync.Mutex + startConnectorSyncer func(ctx context.Context, s *connector.Syncer) + inboxSvc *inbox.Service + knowledgePublish *connector.Mirror + promotionScheduler *knowledge.PromotionScheduler + quotaAccount string + quotaPoolDir string + explicitQuotaPoolDir bool // knowledgeAPIFallback records that knowledgeAPI is the file-only API // bootKnowledge auto-enables when knowledge.enabled is false, not one // built from the configured layers/engine/curator (#9231). @@ -261,6 +268,10 @@ type boot struct { // bootProxy githubProxy *proxy.GitHubProxy + // referenceRepos keeps the derived reference-repo state between the + // boot pass, config reloads and the periodic re-derive + // (hivecommons/hive#18159); nil until wireReferenceRepos ran. + referenceRepos *referenceRepoRuntime // inferenceSink is the one per-agent inference usage ledger; the proxy // records into it in-process and the hive callback's usage capability // records into it for a sidecar runtime (#14138 stage 4). diff --git a/src/cmd/hive/boot_inbox.go b/src/cmd/hive/boot_inbox.go index d4a3e0808b..cd41274df9 100644 --- a/src/cmd/hive/boot_inbox.go +++ b/src/cmd/hive/boot_inbox.go @@ -6,8 +6,10 @@ import ( "path/filepath" "time" + "github.com/hivecommons/hive/pkg/config" "github.com/hivecommons/hive/pkg/dashboard" "github.com/hivecommons/hive/pkg/inbox" + "github.com/hivecommons/hive/pkg/knowledge" "github.com/hivecommons/hive/pkg/knowledge/connector" ) @@ -21,17 +23,7 @@ const inboxSweepTimeout = 2 * time.Minute // route state persists at /connector-state/_inbox.json (connector // names cannot start with "_", so it never collides with a connector). func (b *boot) bootInbox(baseDir string) { - sources := b.cfg.Inbox.EffectiveSources() - configured := map[string]bool{} - for _, c := range b.cfg.Knowledge.Connectors { - configured[c.Type] = true - } - var srcs []inbox.Source - for _, typ := range sources { - if configured[typ] { - srcs = append(srcs, b.inboxSource(baseDir, typ)) - } - } + srcs := b.inboxSources(baseDir, b.cfg.Knowledge.Connectors, b.knowledgeConnectors) token := func(ctx context.Context, _ string) (string, error) { if b.appAuth == nil { return "", nil @@ -58,18 +50,50 @@ func (b *boot) bootInbox(baseDir string) { b.logger.Info("inbox started", "sources", len(srcs), "rules", len(b.cfg.Inbox.Rules)) } +// inboxSources returns one source per configured voice type (inbox.sources) +// that entries provide. syncer, when non-nil, lets Plaud read recordings +// directly from its connector. +func (b *boot) inboxSources(baseDir string, entries []config.KnowledgeConnector, syncer *connector.Syncer) []inbox.Source { + configured := map[string]bool{} + for _, c := range entries { + configured[c.Type] = true + } + var srcs []inbox.Source + for _, typ := range b.cfg.Inbox.EffectiveSources() { + if configured[typ] { + srcs = append(srcs, b.inboxSource(baseDir, typ, entries, syncer)) + } + } + return srcs +} + +// refreshInboxSources rebuilds the Inbox sources from the live connector set +// (#18091). bootKnowledgeConnectors runs it once the boot syncer exists (the +// Inbox boots first, so its Plaud source would otherwise stay fact-only), and +// reloadKnowledgeConnectors runs it after every dashboard save that reached +// the runtime, so a voice connector added after boot feeds GET /api/inbox +// without a restart. A nil syncer keeps the fact-backed sources. +func (b *boot) refreshInboxSources(entries []config.KnowledgeConnector, syncer *connector.Syncer) { + if b.inboxSvc == nil { + return + } + srcs := b.inboxSources(knowledge.BaseDir(), entries, syncer) + b.inboxSvc.SetSources(srcs) + b.logger.Info("inbox sources refreshed", "sources", len(srcs)) +} + // inboxSource returns the fact-backed source for typ. For Plaud it prefers // reading recordings directly from the connector while it is signed in. -func (b *boot) inboxSource(baseDir, typ string) inbox.Source { +func (b *boot) inboxSource(baseDir, typ string, entries []config.KnowledgeConnector, syncer *connector.Syncer) inbox.Source { facts := &inbox.FactSource{ConnectorsDir: filepath.Join(baseDir, "connectors"), SourceType: typ} - if typ != inbox.PlaudType || b.knowledgeConnectors == nil { + if typ != inbox.PlaudType || syncer == nil { return facts } - for _, c := range b.cfg.Knowledge.Connectors { + for _, c := range entries { if c.Type != typ { continue } - conn, ok := b.knowledgeConnectors.Connector(c.Name) + conn, ok := syncer.Connector(c.Name) reader, isReader := conn.(connector.PlaudReader) if !ok || !isReader { continue diff --git a/src/cmd/hive/boot_inbox_sources_test.go b/src/cmd/hive/boot_inbox_sources_test.go new file mode 100644 index 0000000000..f941408b14 --- /dev/null +++ b/src/cmd/hive/boot_inbox_sources_test.go @@ -0,0 +1,84 @@ +package main + +import ( + "context" + "testing" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/inbox" + "github.com/hivecommons/hive/pkg/knowledge" + "github.com/hivecommons/hive/pkg/knowledge/connector" +) + +func plaudEntry() config.KnowledgeConnector { + return config.KnowledgeConnector{Name: "voice", Type: connector.TypePlaud, Layer: "personal"} +} + +// The Inbox follows the connector set the dashboard saves (#18091): a hive +// booted with no connectors lists no sources, a hot-loaded Plaud connector +// becomes the direct-read source on the same save, and removing every +// connector drops it again without a restart. +func TestReloadKnowledgeConnectorsRefreshesInboxSources(t *testing.T) { + knowledge.SetBaseDirForTest(t, t.TempDir()) + b, log := newDepsTestBoot(t, &config.Config{}) + b.bootKnowledgeConnectors(bootKnowledgeDeps{startConnectorSyncer: func(context.Context, *connector.Syncer) {}}) + if b.inboxSvc == nil { + t.Fatal("the Inbox must boot even with no connectors") + } + if b.inboxSvc.HasSource(inbox.PlaudType) { + t.Fatal("no connectors at boot must leave the Inbox without a plaud source") + } + + rt, err := b.reloadKnowledgeConnectors([]config.KnowledgeConnector{plaudEntry()}) + if err != nil || rt == nil { + t.Fatalf("reload = %v, %v\n%s", rt, err, log.String()) + } + if !b.inboxSvc.HasSource(inbox.PlaudType) { + t.Fatalf("hot-loaded plaud connector must become an Inbox source\n%s", log.String()) + } + srcs := b.inboxSources(knowledge.BaseDir(), []config.KnowledgeConnector{plaudEntry()}, b.knowledgeConnectors) + if len(srcs) != 1 { + t.Fatalf("sources = %d, want the one plaud source", len(srcs)) + } + if _, direct := srcs[0].(*inbox.PreferDirect); !direct { + t.Fatalf("plaud source with a live syncer = %T, want *inbox.PreferDirect", srcs[0]) + } + + if rt, err := b.reloadKnowledgeConnectors(nil); err != nil || rt == nil { + t.Fatalf("reload with none = %v, %v; want the same runtime kept", rt, err) + } + if b.inboxSvc.HasSource(inbox.PlaudType) { + t.Fatal("removing every connector must drop the Inbox plaud source") + } +} + +// A hive booted with a Plaud connector attaches the direct-read source once +// the syncer exists, not the fact-only one the Inbox had to settle for while +// booting ahead of the syncer (#18091). +func TestBootKnowledgeConnectorsAttachesDirectPlaudSource(t *testing.T) { + knowledge.SetBaseDirForTest(t, t.TempDir()) + cfg := &config.Config{} + cfg.Knowledge.Connectors = []config.KnowledgeConnector{plaudEntry()} + b, log := newDepsTestBoot(t, cfg) + + if srcs := b.inboxSources(knowledge.BaseDir(), cfg.Knowledge.Connectors, nil); len(srcs) != 1 { + t.Fatalf("sources without a syncer = %d, want one fact-backed plaud source", len(srcs)) + } else if _, facts := srcs[0].(*inbox.FactSource); !facts { + t.Fatalf("plaud source without a syncer = %T, want *inbox.FactSource", srcs[0]) + } + + b.bootKnowledgeConnectors(bootKnowledgeDeps{startConnectorSyncer: func(context.Context, *connector.Syncer) {}}) + if b.knowledgeConnectorRuntime() == nil { + t.Fatalf("syncer not built\n%s", log.String()) + } + if !b.inboxSvc.HasSource(inbox.PlaudType) { + t.Fatal("plaud source missing after boot") + } + srcs := b.inboxSources(knowledge.BaseDir(), cfg.Knowledge.Connectors, b.knowledgeConnectors) + if len(srcs) != 1 { + t.Fatalf("sources = %d, want one", len(srcs)) + } + if _, direct := srcs[0].(*inbox.PreferDirect); !direct { + t.Fatalf("plaud source after boot = %T, want *inbox.PreferDirect", srcs[0]) + } +} diff --git a/src/cmd/hive/boot_knowledge_connectors.go b/src/cmd/hive/boot_knowledge_connectors.go index 070f0f9b5e..9b71766598 100644 --- a/src/cmd/hive/boot_knowledge_connectors.go +++ b/src/cmd/hive/boot_knowledge_connectors.go @@ -93,6 +93,7 @@ func (b *boot) bootKnowledgeConnectors(deps bootKnowledgeDeps) { // answers 200 with an empty list and the panel renders its empty state // (#17918) instead of waiting forever on a 404. b.bootInbox(knowledge.BaseDir()) + b.startConnectorSyncer = deps.startConnectorSyncer s, err := newKnowledgeConnectorSyncer(b.cfg.Knowledge.Connectors, nil, b.knowledgeAPI, knowledge.BaseDir(), b.logger, b.inboxAfterSync) if err != nil { b.logger.Warn("knowledge connectors disabled", "error", err) @@ -101,17 +102,62 @@ func (b *boot) bootKnowledgeConnectors(deps bootKnowledgeDeps) { if s == nil { return } + b.knowledgeConnectorsMu.Lock() b.knowledgeConnectors = s + b.knowledgeConnectorsMu.Unlock() + // The Inbox booted before the syncer existed; give it the live + // connectors so Plaud can read recordings directly (#18091). + b.refreshInboxSources(b.cfg.Knowledge.Connectors, s) if deps.startConnectorSyncer != nil { deps.startConnectorSyncer(b.ctx, s) } b.logger.Info("knowledge connectors started", "count", len(b.cfg.Knowledge.Connectors)) } +// reloadKnowledgeConnectors applies a knowledge.connectors list saved from +// the dashboard to the running hive (#18091). With no syncer yet (the hive +// booted with zero connectors, or the boot build failed) it builds and +// starts one from the same inputs as bootKnowledgeConnectors; otherwise it +// reconfigures the running syncer in place, so unchanged connectors keep +// syncing undisturbed. The knowledge.publish mirror is not rebuilt: a +// changed publish connector still needs a restart. Once the runtime follows, +// the Inbox sources are rebuilt from entries so a voice connector added from +// the dashboard shows up in the Inbox at once. +func (b *boot) reloadKnowledgeConnectors(entries []config.KnowledgeConnector) (dashboard.KnowledgeConnectorRuntime, error) { + b.knowledgeConnectorsMu.Lock() + defer b.knowledgeConnectorsMu.Unlock() + if b.knowledgeConnectors != nil { + if err := b.knowledgeConnectors.Reconfigure(connectorConfigs(entries)); err != nil { + return nil, err + } + b.logger.Info("knowledge connectors reloaded", "count", len(entries)) + b.refreshInboxSources(entries, b.knowledgeConnectors) + return b.knowledgeConnectors, nil + } + s, err := newKnowledgeConnectorSyncer(entries, nil, b.knowledgeAPI, knowledge.BaseDir(), b.logger, b.inboxAfterSync) + if err != nil || s == nil { + // No runtime to follow: with no entries left the Inbox still lists + // no sources, and a build failure keeps the previous set. + if err == nil { + b.refreshInboxSources(entries, nil) + } + return nil, err + } + b.knowledgeConnectors = s + if b.startConnectorSyncer != nil { + b.startConnectorSyncer(b.ctx, s) + } + b.logger.Info("knowledge connectors started", "count", len(entries), "hot_loaded", true) + b.refreshInboxSources(entries, s) + return s, nil +} + // knowledgeConnectorRuntime exposes the syncer to the dashboard. A nil // *connector.Syncer must stay a nil interface so the dashboard reports // "no connectors configured" instead of calling methods on a nil pointer. func (b *boot) knowledgeConnectorRuntime() dashboard.KnowledgeConnectorRuntime { + b.knowledgeConnectorsMu.Lock() + defer b.knowledgeConnectorsMu.Unlock() if b.knowledgeConnectors == nil { return nil } diff --git a/src/cmd/hive/boot_knowledge_connectors_reload_test.go b/src/cmd/hive/boot_knowledge_connectors_reload_test.go new file mode 100644 index 0000000000..ae7e70aab6 --- /dev/null +++ b/src/cmd/hive/boot_knowledge_connectors_reload_test.go @@ -0,0 +1,52 @@ +package main + +import ( + "context" + "testing" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/knowledge" + "github.com/hivecommons/hive/pkg/knowledge/connector" +) + +// A hive booted with zero connectors builds and starts the syncer the first +// time the dashboard saves one, then reconfigures that same syncer (#18091). +func TestReloadKnowledgeConnectors(t *testing.T) { + knowledge.SetBaseDirForTest(t, t.TempDir()) + b, log := newDepsTestBoot(t, &config.Config{}) + var started []*connector.Syncer + b.bootKnowledgeConnectors(bootKnowledgeDeps{startConnectorSyncer: func(_ context.Context, s *connector.Syncer) { + started = append(started, s) + }}) + if b.knowledgeConnectorRuntime() != nil || len(started) != 0 { + t.Fatal("no connectors at boot must leave the syncer off") + } + + if rt, err := b.reloadKnowledgeConnectors(nil); rt != nil || err != nil { + t.Fatalf("reload with none = %v, %v; want nil, nil", rt, err) + } + + docs := config.KnowledgeConnector{Name: "docs", Type: connector.TypeDocument, Layer: "project", + Scope: map[string]string{"url": "https://example.org/guide.html"}} + rt, err := b.reloadKnowledgeConnectors([]config.KnowledgeConnector{docs}) + if err != nil || rt == nil { + t.Fatalf("reload = %v, %v\n%s", rt, err, log.String()) + } + if len(started) != 1 || started[0] != b.knowledgeConnectors { + t.Fatalf("hot-loaded syncer not started once: %d", len(started)) + } + if sts := rt.Statuses(); len(sts) != 1 || sts[0].Name != "docs" { + t.Fatalf("statuses = %+v", sts) + } + + rt2, err := b.reloadKnowledgeConnectors(nil) + if err != nil || rt2 != rt { + t.Fatalf("second reload = %v, %v; want the same runtime", rt2, err) + } + if sts := rt2.Statuses(); len(sts) != 0 { + t.Fatalf("removed connector still listed: %+v", sts) + } + if len(started) != 1 { + t.Fatalf("reconfigure must not start a second syncer: %d", len(started)) + } +} diff --git a/src/cmd/hive/discovery_appauth_forge_switch_test.go b/src/cmd/hive/discovery_appauth_forge_switch_test.go new file mode 100644 index 0000000000..c9a946d56f --- /dev/null +++ b/src/cmd/hive/discovery_appauth_forge_switch_test.go @@ -0,0 +1,54 @@ +package main + +import ( + "testing" + + "github.com/hivecommons/hive/pkg/config" +) + +// A placeholder booted against its pool's default forge (GHE App 5686) and +// then claimed onto github.com receives App 3568013 with installation_id 0. +// HasUsableApp is false for that identity, so the full client rebuild is +// (correctly) skipped β€” but installation discovery must still move to the new +// App/forge, or it polls the old one forever and the hive never adopts the +// installation its owner makes (live: `ash` β†’ thinkscientist on vllm-d). +func TestDiscoveryAppAuthStaleOnForgeSwitchWithoutInstallation(t *testing.T) { + gheCfg := &config.Config{} + gheCfg.GitHub.AppID = 5686 + gheCfg.GitHub.APIURL = "https://github.ibm.com/api/v3" + booted := testAppAuth(t, gheCfg) + + public := config.GitHubConfig{AppID: 3568013, InstallationID: 0, APIURL: "https://api.github.com"} + + if !discoveryAppAuthStale(booted, public, "/data/gh-app-key-3568013.pem") { + t.Fatal("GHE-booted AppAuth must be replaced when the hub delivers the github.com App with no installation") + } + if !discoveryAppAuthStale(nil, public, "/data/gh-app-key-3568013.pem") { + t.Fatal("a spoke that booted with no AppAuth must get one for discovery") + } +} + +func TestDiscoveryAppAuthStaleNoOpCases(t *testing.T) { + cfg := &config.Config{} + cfg.GitHub.AppID = 3568013 + cfg.GitHub.APIURL = "https://api.github.com" + current := testAppAuth(t, cfg) + + cases := []struct { + name string + gh config.GitHubConfig + key string + }{ + {"same App and forge already held", config.GitHubConfig{AppID: 3568013, APIURL: "https://api.github.com/"}, "/k.pem"}, + {"installation known β€” the usable-App rebuild owns this", config.GitHubConfig{AppID: 5686, InstallationID: 42, APIURL: "https://github.ibm.com/api/v3"}, "/k.pem"}, + {"no key resolved", config.GitHubConfig{AppID: 5686, APIURL: "https://github.ibm.com/api/v3"}, ""}, + {"placeholder App is not a real App", config.GitHubConfig{AppID: config.PlaceholderAppID}, "/k.pem"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + if discoveryAppAuthStale(current, tc.gh, tc.key) { + t.Errorf("expected no discovery AppAuth swap") + } + }) + } +} diff --git a/src/cmd/hive/eval_cycle_quota_hold.go b/src/cmd/hive/eval_cycle_quota_hold.go new file mode 100644 index 0000000000..f7b552c562 --- /dev/null +++ b/src/cmd/hive/eval_cycle_quota_hold.go @@ -0,0 +1,137 @@ +package main + +import ( + "strings" + "sync" + "time" + + "github.com/hivecommons/hive/crux" + "github.com/hivecommons/hive/pkg/scheduler" +) + +// PER-AGENT PROVIDER QUOTA HOLD (hivecommons/hive#18112). +// +// The provider spend gate (gateKickMessagesForProviderBudget) only fires for +// agents behind the hive's own inference gateway: its latch is set by the +// gateway proxy, which never sees a backend that calls its provider directly +// (GitHub Copilot, for one). Those agents still raise crux.Process.QuotaExhausted +// from the pane ("You have exceeded your monthly quota"), but nothing withheld +// their kicks, so the governor kept kicking them β€” and sending them review +// work β€” every cycle, each turn ending at once on the same quota error. +// +// The hold is PER AGENT, unlike the spend gate, because the limit is on that +// agent's provider account, not on a key every agent shares: one agent out of +// quota must never hold any other agent. A held agent still gets one probe +// kick per governor.provider_budget.probe_interval_s, so the hive notices when +// the quota resets (the flag clears once the pane stops showing the message). + +// agentQuotaHoldState remembers, per quota-exhausted agent, when the last +// probe kick was actually delivered. Package-level (agentQuotaHold) for the +// same reason as providerBudgetProbe: runEvalCycle has no state of its own. +type agentQuotaHoldState struct { + mu sync.Mutex + lastProbe map[string]time.Time +} + +// MarkProbeDelivered records that a probe kick reached agent at the given +// time. Call it only after a kick is delivered: a withheld, deferred or failed +// kick learned nothing about the quota and must not push the next probe out. +func (s *agentQuotaHoldState) MarkProbeDelivered(agent string, at time.Time) { + s.mu.Lock() + defer s.mu.Unlock() + if s.lastProbe == nil { + s.lastProbe = map[string]time.Time{} + } + s.lastProbe[agent] = at +} + +// Forget drops agent's probe time, so a later quota hit starts fresh. +func (s *agentQuotaHoldState) Forget(agent string) { + s.mu.Lock() + defer s.mu.Unlock() + delete(s.lastProbe, agent) +} + +// LastProbe reports when a probe kick was last delivered to agent; the zero +// time means never, which makes the agent due a probe at once. +func (s *agentQuotaHoldState) LastProbe(agent string) time.Time { + s.mu.Lock() + defer s.mu.Unlock() + return s.lastProbe[agent] +} + +// quotaExhaustedAgents reads the per-agent quota flag from one status snapshot +// and forgets the probe time of every agent whose flag is clear, so its kicks +// flow normally and its next quota hit starts a fresh interval. nil entries +// are skipped. The returned set holds only the exhausted agents. +func (s *agentQuotaHoldState) quotaExhaustedAgents(statuses map[string]*crux.Process) map[string]bool { + exhausted := map[string]bool{} + for name, proc := range statuses { + if proc == nil { + continue + } + if proc.QuotaExhausted { + exhausted[name] = true + continue + } + s.Forget(name) + } + return exhausted +} + +// gate applies the per-agent quota hold to one cycle's kicks. It always reads +// the flags from statuses (so cleared agents forget their probe time), then +// returns messages unchanged when no agent is held or there is nothing to +// gate; otherwise it defers to gateKickMessagesForAgentQuota. +func (s *agentQuotaHoldState) gate(messages []scheduler.KickMessage, statuses map[string]*crux.Process, now time.Time, probeInterval time.Duration) (kept []scheduler.KickMessage, withheld []string, probes []string) { + exhausted := s.quotaExhaustedAgents(statuses) + if len(exhausted) == 0 || len(messages) == 0 { + return messages, nil, nil + } + return gateKickMessagesForAgentQuota(messages, func(agent string) bool { return exhausted[agent] }, s.LastProbe, now, probeInterval) +} + +// gateKickMessagesForAgentQuota withholds kicks for agents whose provider +// quota is exhausted, except for one probe kick per held agent once +// probeInterval has passed since that agent's last delivered probe (a zero +// lastProbe is due at once). Only the FIRST message for a probing agent is +// kept; the rest of that agent's messages are withheld this cycle. Agents +// that are not exhausted pass through untouched. Message order is preserved. +// +// withheld and probes name agents in message order; an agent appears in +// withheld once per withheld message, and in probes at most once. +func gateKickMessagesForAgentQuota(messages []scheduler.KickMessage, quotaExhausted func(agent string) bool, lastProbe func(agent string) time.Time, now time.Time, probeInterval time.Duration) (kept []scheduler.KickMessage, withheld []string, probes []string) { + decided := map[string]bool{} + for _, msg := range messages { + if !quotaExhausted(msg.Agent) { + kept = append(kept, msg) + continue + } + if !decided[msg.Agent] { + decided[msg.Agent] = true + if last := lastProbe(msg.Agent); last.IsZero() || now.Sub(last) >= probeInterval { + probes = append(probes, msg.Agent) + kept = append(kept, msg) + continue + } + } + withheld = append(withheld, msg.Agent) + } + return kept, withheld, probes +} + +// compactDuration renders an operator-facing interval without the zero tails +// of time.Duration.String: 30m, 2h, 1h30m, 45s. +func compactDuration(d time.Duration) string { + if d <= 0 { + return "0s" + } + s := d.Round(time.Second).String() + if strings.HasSuffix(s, "m0s") { + s = strings.TrimSuffix(s, "0s") + } + if strings.HasSuffix(s, "h0m") { + s = strings.TrimSuffix(s, "0m") + } + return s +} diff --git a/src/cmd/hive/eval_cycle_quota_hold_test.go b/src/cmd/hive/eval_cycle_quota_hold_test.go new file mode 100644 index 0000000000..77c938fa8f --- /dev/null +++ b/src/cmd/hive/eval_cycle_quota_hold_test.go @@ -0,0 +1,272 @@ +package main + +import ( + "errors" + "log/slog" + "reflect" + "testing" + "time" + + "github.com/hivecommons/hive/crux" + "github.com/hivecommons/hive/pkg/scheduler" +) + +// Tests for the per-agent provider quota hold (hivecommons/hive#18112). + +var quotaHoldNow = time.Date(2026, 11, 13, 9, 0, 0, 0, time.UTC) + +func quotaSet(agents ...string) func(string) bool { + set := map[string]bool{} + for _, a := range agents { + set[a] = true + } + return func(agent string) bool { return set[agent] } +} + +func probesAt(at map[string]time.Time) func(string) time.Time { + return func(agent string) time.Time { return at[agent] } +} + +func kickAgents(msgs []scheduler.KickMessage) []string { + out := []string{} + for _, m := range msgs { + out = append(out, m.Agent+":"+m.Message) + } + return out +} + +func TestQuotaHoldGate_NoHeldAgentsPassesThrough(t *testing.T) { + msgs := []scheduler.KickMessage{{Agent: "a", Message: "1"}, {Agent: "b", Message: "2"}} + kept, withheld, probes := gateKickMessagesForAgentQuota(msgs, quotaSet(), probesAt(nil), quotaHoldNow, 30*time.Minute) + if !reflect.DeepEqual(kept, msgs) { + t.Fatalf("kept = %v, want every message unchanged", kickAgents(kept)) + } + if len(withheld) != 0 || len(probes) != 0 { + t.Fatalf("withheld=%v probes=%v, want none", withheld, probes) + } +} + +func TestQuotaHoldGate_HeldAgentNotDueIsWithheld(t *testing.T) { + msgs := []scheduler.KickMessage{{Agent: "copilot", Message: "gov"}} + last := map[string]time.Time{"copilot": quotaHoldNow.Add(-10 * time.Minute)} + kept, withheld, probes := gateKickMessagesForAgentQuota(msgs, quotaSet("copilot"), probesAt(last), quotaHoldNow, 30*time.Minute) + if len(kept) != 0 { + t.Fatalf("kept = %v, want the held agent's kick withheld", kickAgents(kept)) + } + if !reflect.DeepEqual(withheld, []string{"copilot"}) || len(probes) != 0 { + t.Fatalf("withheld=%v probes=%v, want [copilot] and no probe", withheld, probes) + } +} + +// A held agent that is due a probe gets exactly one kick; its other messages +// this cycle (for example a fix kick ahead of its governor kick) stay held. +func TestQuotaHoldGate_HeldAgentDueGetsOneProbe(t *testing.T) { + msgs := []scheduler.KickMessage{ + {Agent: "copilot", Message: "fix"}, + {Agent: "copilot", Message: "gov"}, + } + last := map[string]time.Time{"copilot": quotaHoldNow.Add(-30 * time.Minute)} + kept, withheld, probes := gateKickMessagesForAgentQuota(msgs, quotaSet("copilot"), probesAt(last), quotaHoldNow, 30*time.Minute) + if want := []string{"copilot:fix"}; !reflect.DeepEqual(kickAgents(kept), want) { + t.Fatalf("kept = %v, want %v (first message only)", kickAgents(kept), want) + } + if !reflect.DeepEqual(probes, []string{"copilot"}) || !reflect.DeepEqual(withheld, []string{"copilot"}) { + t.Fatalf("probes=%v withheld=%v, want one probe and one withheld", probes, withheld) + } +} + +// One agent out of quota must not hold any other agent; order is kept. +func TestQuotaHoldGate_MixedHeldAndHealthyKeepsOrder(t *testing.T) { + msgs := []scheduler.KickMessage{ + {Agent: "healthy-1", Message: "a"}, + {Agent: "held", Message: "review"}, + {Agent: "healthy-2", Message: "b"}, + {Agent: "held", Message: "gov"}, + } + last := map[string]time.Time{"held": quotaHoldNow.Add(-time.Minute)} + kept, withheld, probes := gateKickMessagesForAgentQuota(msgs, quotaSet("held"), probesAt(last), quotaHoldNow, 30*time.Minute) + if want := []string{"healthy-1:a", "healthy-2:b"}; !reflect.DeepEqual(kickAgents(kept), want) { + t.Fatalf("kept = %v, want %v", kickAgents(kept), want) + } + if !reflect.DeepEqual(withheld, []string{"held", "held"}) || len(probes) != 0 { + t.Fatalf("withheld=%v probes=%v, want [held held] and no probe", withheld, probes) + } +} + +func TestQuotaHoldGate_ZeroProbeTimeIsDueAtOnce(t *testing.T) { + msgs := []scheduler.KickMessage{ + {Agent: "healthy", Message: "a"}, + {Agent: "held", Message: "gov"}, + } + kept, withheld, probes := gateKickMessagesForAgentQuota(msgs, quotaSet("held"), probesAt(nil), quotaHoldNow, 30*time.Minute) + if want := []string{"healthy:a", "held:gov"}; !reflect.DeepEqual(kickAgents(kept), want) { + t.Fatalf("kept = %v, want %v", kickAgents(kept), want) + } + if !reflect.DeepEqual(probes, []string{"held"}) || len(withheld) != 0 { + t.Fatalf("probes=%v withheld=%v, want [held] probed at once", probes, withheld) + } +} + +func TestQuotaHoldState_MarkThenLastProbeThenForget(t *testing.T) { + var s agentQuotaHoldState + if got := s.LastProbe("a"); !got.IsZero() { + t.Fatalf("LastProbe on empty state = %v, want zero", got) + } + s.MarkProbeDelivered("a", quotaHoldNow) + if got := s.LastProbe("a"); !got.Equal(quotaHoldNow) { + t.Fatalf("LastProbe = %v, want %v", got, quotaHoldNow) + } + if got := s.LastProbe("b"); !got.IsZero() { + t.Fatalf("another agent's probe time = %v, want zero", got) + } + s.Forget("a") + if got := s.LastProbe("a"); !got.IsZero() { + t.Fatalf("LastProbe after Forget = %v, want zero", got) + } +} + +// When an agent's flag clears its probe time is forgotten, so its kicks flow +// normally and a later quota hit starts a fresh interval. +func TestQuotaHoldState_ClearedFlagForgetsProbe(t *testing.T) { + var s agentQuotaHoldState + s.MarkProbeDelivered("held", quotaHoldNow) + s.MarkProbeDelivered("recovered", quotaHoldNow) + got := s.quotaExhaustedAgents(map[string]*crux.Process{ + "held": {QuotaExhausted: true}, + "recovered": {QuotaExhausted: false}, + "gone": nil, + }) + if !reflect.DeepEqual(got, map[string]bool{"held": true}) { + t.Fatalf("exhausted = %v, want only held", got) + } + if !s.LastProbe("held").Equal(quotaHoldNow) { + t.Fatal("a still-held agent must keep its probe time") + } + if !s.LastProbe("recovered").IsZero() { + t.Fatal("a recovered agent's probe time must be forgotten") + } +} + +// gate is the eval cycle's entry point: it must return the cycle's messages +// untouched (same slice, no copy) when no agent is held or there is nothing to +// kick, still forget recovered agents, and gate only when both are non-empty. +func TestQuotaHoldState_GateShortCircuitsAndGates(t *testing.T) { + msgs := []scheduler.KickMessage{{Agent: "held", Message: "1"}, {Agent: "ok", Message: "2"}, {Agent: "held", Message: "3"}} + + t.Run("no held agent returns the same slice", func(t *testing.T) { + var s agentQuotaHoldState + s.MarkProbeDelivered("ok", quotaHoldNow) + kept, withheld, probes := s.gate(msgs, map[string]*crux.Process{"ok": {}}, quotaHoldNow, 30*time.Minute) + if len(kept) != len(msgs) || &kept[0] != &msgs[0] { + t.Fatalf("kept = %v, want the input slice itself", kickAgents(kept)) + } + if withheld != nil || probes != nil { + t.Fatalf("withheld=%v probes=%v, want nil", withheld, probes) + } + if !s.LastProbe("ok").IsZero() { + t.Fatal("a recovered agent's probe time must be forgotten even when nothing is gated") + } + }) + + t.Run("no messages returns the empty input", func(t *testing.T) { + var s agentQuotaHoldState + empty := []scheduler.KickMessage{} + kept, withheld, probes := s.gate(empty, map[string]*crux.Process{"held": {QuotaExhausted: true}}, quotaHoldNow, 30*time.Minute) + if kept == nil || len(kept) != 0 || withheld != nil || probes != nil { + t.Fatalf("kept=%v withheld=%v probes=%v, want the empty input and nothing else", kept, withheld, probes) + } + }) + + t.Run("one held agent is gated", func(t *testing.T) { + var s agentQuotaHoldState + s.MarkProbeDelivered("held", quotaHoldNow.Add(-time.Minute)) + kept, withheld, probes := s.gate(msgs, map[string]*crux.Process{"held": {QuotaExhausted: true}, "ok": {}}, quotaHoldNow, 30*time.Minute) + if got := kickAgents(kept); !reflect.DeepEqual(got, []string{"ok:2"}) { + t.Fatalf("kept = %v, want only ok:2", got) + } + if !reflect.DeepEqual(withheld, []string{"held", "held"}) || len(probes) != 0 { + t.Fatalf("withheld=%v probes=%v, want held twice and no probe", withheld, probes) + } + }) +} + +// The full cycle: withheld, then one probe after the interval, then withheld +// again on the next cycle once that probe was delivered. +func TestQuotaHoldGate_ProbeOncePerInterval(t *testing.T) { + var s agentQuotaHoldState + held := quotaSet("held") + msgs := []scheduler.KickMessage{{Agent: "held", Message: "gov"}} + interval := 30 * time.Minute + + s.MarkProbeDelivered("held", quotaHoldNow) + if kept, _, _ := gateKickMessagesForAgentQuota(msgs, held, s.LastProbe, quotaHoldNow.Add(5*time.Minute), interval); len(kept) != 0 { + t.Fatalf("inside the interval kept = %v, want none", kickAgents(kept)) + } + at := quotaHoldNow.Add(interval) + kept, _, probes := gateKickMessagesForAgentQuota(msgs, held, s.LastProbe, at, interval) + if len(kept) != 1 || !reflect.DeepEqual(probes, []string{"held"}) { + t.Fatalf("after the interval kept=%v probes=%v, want exactly one probe", kickAgents(kept), probes) + } + s.MarkProbeDelivered("held", at) + if kept, _, _ := gateKickMessagesForAgentQuota(msgs, held, s.LastProbe, at.Add(2*time.Minute), interval); len(kept) != 0 { + t.Fatalf("next cycle kept = %v, want the agent held again", kickAgents(kept)) + } +} + +// The probe time is recorded on delivery only: a probe whose send fails must +// not advance it, so the next cycle may probe again. +func TestQuotaHoldDispatch_ProbeRecordedOnDeliveryNotOnSendError(t *testing.T) { + for _, tc := range []struct { + name string + sendErr error + want bool + }{ + {"delivered", nil, true}, + {"send error", errors.New("agent CLI did not reach input prompt"), false}, + {"busy deferral", crux.ErrAgentBusy, false}, + } { + t.Run(tc.name, func(t *testing.T) { + var s agentQuotaHoldState + exhausted := map[string]bool{"held": true} + r := newKickDispatchRecorder() + r.sendErr["held"] = tc.sendErr + deps := r.deps() + inner := deps.onDelivered + deps.onDelivered = func(msg scheduler.KickMessage, deliveredAt time.Time) { + inner(msg, deliveredAt) + if exhausted[msg.Agent] { + s.MarkProbeDelivered(msg.Agent, deliveredAt) + } + } + msgs, _, probes := gateKickMessagesForAgentQuota( + []scheduler.KickMessage{{Agent: "held", Message: "gov"}, {Agent: "healthy", Message: "gov"}}, + func(a string) bool { return exhausted[a] }, s.LastProbe, quotaHoldNow, 30*time.Minute) + if !reflect.DeepEqual(probes, []string{"held"}) { + t.Fatalf("probes = %v, want [held]", probes) + } + dispatchAgentKicks(msgs, false, deps, slog.Default()) + if got := !s.LastProbe("held").IsZero(); got != tc.want { + t.Fatalf("probe recorded = %v, want %v", got, tc.want) + } + if !s.LastProbe("healthy").IsZero() { + t.Fatal("a healthy agent's delivery must never record a probe time") + } + }) + } +} + +func TestCompactDuration(t *testing.T) { + for d, want := range map[time.Duration]string{ + 30 * time.Minute: "30m", + 2 * time.Hour: "2h", + 90 * time.Minute: "1h30m", + 45 * time.Second: "45s", + 90 * time.Second: "1m30s", + 0: "0s", + time.Hour + 30*time.Second: "1h0m30s", + } { + if got := compactDuration(d); got != want { + t.Errorf("compactDuration(%v) = %q, want %q", d, got, want) + } + } +} diff --git a/src/cmd/hive/eval_cycle_seams.go b/src/cmd/hive/eval_cycle_seams.go index 43b792a7f6..98a5d8c93f 100644 --- a/src/cmd/hive/eval_cycle_seams.go +++ b/src/cmd/hive/eval_cycle_seams.go @@ -443,7 +443,11 @@ type providerBudgetAlert struct { // consulted the agent manager on the NOT-latched branch, and collecting agent // statuses every cycle while the provider is clipped would be work the old // code never did. Passing a thunk keeps that laziness exactly. -func decideProviderBudgetAlert(latched, suppress bool, cause string, since time.Time, rebuffs int, quotaReason func() string) providerBudgetAlert { +// +// quotaProbeInterval is how often an agent held for exhausted provider quota +// is probed (#18112); the quota banner names it so the operator knows the +// hive is holding those kicks and when it will retry them. +func decideProviderBudgetAlert(latched, suppress bool, cause string, since time.Time, rebuffs int, quotaProbeInterval time.Duration, quotaReason func() string) providerBudgetAlert { if latched { state := "agent kicks suspended" if !suppress { @@ -457,7 +461,8 @@ func decideProviderBudgetAlert(latched, suppress bool, cause string, since time. return providerBudgetAlert{Message: msg, Cause: msg} } if reason := quotaReason(); reason != "" { - return providerBudgetAlert{Message: "provider quota exhausted β€” " + reason} + return providerBudgetAlert{Message: fmt.Sprintf("provider quota exhausted β€” %s; their kicks are held and retried every %s", + reason, compactDuration(quotaProbeInterval))} } return providerBudgetAlert{Clear: true} } diff --git a/src/cmd/hive/knowledge_adviser_llm.go b/src/cmd/hive/knowledge_adviser_llm.go index 233a540260..c711084250 100644 --- a/src/cmd/hive/knowledge_adviser_llm.go +++ b/src/cmd/hive/knowledge_adviser_llm.go @@ -9,10 +9,15 @@ import ( "hash/fnv" "io" "log/slog" + "maps" + "math/rand/v2" "net" "net/http" + "slices" + "strconv" "strings" "sync" + "sync/atomic" "time" "github.com/hivecommons/hive/crux" @@ -39,16 +44,40 @@ const ( // knowledgeAdviserLLMBudgetWindow is the rolling per-agent window. knowledgeAdviserLLMBudgetWindow = time.Hour knowledgeAdviserLLMUserPrefix = "hive-knowledge-advisor:" + + // knowledgeAdviserLLMMaxInFlight caps model calls in flight across every + // agent on the hive (hivecommons/hive#18151). The advisor shares one + // upstream quota with the agents (the Copilot user token), so N agents + // fanning out N calls in the same second only buys 429s; the runtime's + // per-agent in-flight guard stays in front of this cap. + knowledgeAdviserLLMMaxInFlight = 1 + // knowledgeAdviserLLMRetryBackoff is the wait before the single retry of + // a 429 that carries no usable Retry-After, plus up to the same amount of + // jitter so agents released together do not collide again. + knowledgeAdviserLLMRetryBackoff = 2 * time.Second + // knowledgeAdviserLLMRetryHeadroom is how much of the decision timeout + // must remain after the Retry-After wait for the retry to be worth + // making; a shorter remainder records rate-limited without a second call. + knowledgeAdviserLLMRetryHeadroom = 3 * time.Second + // knowledgeAdviserLLMRateLimitWarnWindow: the first 429 in this window + // logs at WARN with the rate-limit headers, the rest at DEBUG. + knowledgeAdviserLLMRateLimitWarnWindow = 5 * time.Minute + // knowledgeAdviserLLMErrorBodyRunes caps the logged 429 response body. + knowledgeAdviserLLMErrorBodyRunes = 300 + // knowledgeAdviserLLMRateLimitHeaderPrefix selects the upstream's + // rate-limit headers for the WARN line. + knowledgeAdviserLLMRateLimitHeaderPrefix = "x-ratelimit-" ) // Model verdicts: what the model call produced. const ( - knowledgeVerdictPick = "pick" - knowledgeVerdictNull = "null" - knowledgeVerdictInvalid = "invalid" - knowledgeVerdictTimeout = "timeout" - knowledgeVerdictOverBudget = "over-budget" - knowledgeVerdictError = "error" + knowledgeVerdictPick = "pick" + knowledgeVerdictNull = "null" + knowledgeVerdictInvalid = "invalid" + knowledgeVerdictTimeout = "timeout" + knowledgeVerdictOverBudget = "over-budget" + knowledgeVerdictRateLimited = "rate-limited" + knowledgeVerdictError = "error" ) // Decision outcomes: what happened to the delivery. invalid, timeout, @@ -83,9 +112,39 @@ type knowledgeAdviserLLM struct { // decisions persists one compact record per decision for the retro // lane's arm comparison (#17211); nil records nothing. decisions knowledgeAdvisorDecisionRecorder + // slots is the hive-wide in-flight cap (knowledgeAdviserLLMMaxInFlight); + // nil means uncapped. + slots chan struct{} + // waiting counts decisions blocked on slots, for tests and the WARN line. + waiting atomic.Int32 + // wait blocks for d or until ctx ends and reports whether d elapsed; tests + // replace it so no test sleeps. + wait func(ctx context.Context, d time.Duration) bool mu sync.Mutex delivered map[string]*knowledgeAdviserLLMKick + // rateLimitWarnedAt is when the current WARN window opened. + rateLimitWarnedAt time.Time +} + +// newKnowledgeAdviserLLMSlots is the hive-wide in-flight semaphore. +func newKnowledgeAdviserLLMSlots() chan struct{} { + return make(chan struct{}, knowledgeAdviserLLMMaxInFlight) +} + +// knowledgeAdviserLLMWait is the production wait: a timer bounded by ctx. +func knowledgeAdviserLLMWait(ctx context.Context, d time.Duration) bool { + if d <= 0 { + return ctx.Err() == nil + } + t := time.NewTimer(d) + defer t.Stop() + select { + case <-t.C: + return true + case <-ctx.Done(): + return false + } } // knowledgeAdviserLLMKick is the slugs this decorator handed the runtime @@ -111,6 +170,8 @@ func newKnowledgeAdviserLLM(b *boot, inner *knowledgeAdviser) *knowledgeAdviserL logger: b.logger, budget: newKnowledgeAdviserLLMBudget(time.Now), decisions: b.knowledgeAdvisorDecisions(), + slots: newKnowledgeAdviserLLMSlots(), + wait: knowledgeAdviserLLMWait, delivered: make(map[string]*knowledgeAdviserLLMKick), } } @@ -253,17 +314,36 @@ type knowledgeAdviserLLMDecision struct { latency time.Duration promptTokens int completionTokens int + // retryAfter is the upstream's Retry-After on a rate-limited decision, + // zero when it sent none. + retryAfter time.Duration + // retried reports that the single 429 retry was made. + retried bool } -// decide spends one budgeted call on the model. There is no retry: a wrong -// answer costs one silent scope, a retry would double the spend. +// decide spends one budgeted call on the model. A wrong answer is never +// retried (it would double the spend); a 429 is retried once after the +// upstream's Retry-After, inside the same timeout, and a decision that still +// ends rate-limited hands its budget back (hivecommons/hive#18151). func (d *knowledgeAdviserLLM) decide(agent string, kick scheduler.KickSummary, scope crux.KnowledgeScope, candidates []crux.KnowledgeAdvice, r config.ResolvedAdvisorLLM) knowledgeAdviserLLMDecision { - if over := d.budget.take(agent, r.MaxCallsPerAgentHour, r.MaxCallsPerDay); over != "" { + over, spent := d.budget.take(agent, r.MaxCallsPerAgentHour, r.MaxCallsPerDay) + if over != "" { return knowledgeAdviserLLMDecision{verdict: knowledgeVerdictOverBudget, budget: over} } start := d.now() ctx, cancel := context.WithTimeout(context.Background(), time.Duration(r.TimeoutS)*time.Second) defer cancel() + if !d.acquireSlot(ctx) { + // The hive-wide slot never freed inside the timeout: nothing was + // sent, so nothing is charged. + d.budget.refund(agent, spent) + return knowledgeAdviserLLMDecision{ + verdict: knowledgeVerdictTimeout, + err: errors.New("knowledge advisor: timed out waiting for the hive-wide in-flight slot"), + latency: d.now().Sub(start), + } + } + defer d.releaseSlot() reqBody := knowledgeAdviserLLMRequest{ Model: r.Model, Messages: []knowledgeAdviserLLMMessage{ @@ -275,12 +355,33 @@ func (d *knowledgeAdviserLLM) decide(agent string, kick scheduler.KickSummary, s User: knowledgeAdviserLLMUserPrefix + agent, } resp, err := d.complete(ctx, r, reqBody) - dec := knowledgeAdviserLLMDecision{latency: d.now().Sub(start)} + dec := knowledgeAdviserLLMDecision{} + var rl *knowledgeAdviserLLMRateLimited + if errors.As(err, &rl) { + d.logRateLimited(agent, rl, false) + dec.retryAfter = rl.retryAfter + if wait, ok := knowledgeAdviserLLMRetryWait(ctx, rl.retryAfter, time.Now()); ok && d.wait(ctx, wait) { + dec.retried = true + resp, err = d.complete(ctx, r, reqBody) + if errors.As(err, &rl) { + d.logRateLimited(agent, rl, true) + dec.retryAfter = rl.retryAfter + } + } + } + dec.latency = d.now().Sub(start) if err != nil { dec.err = err dec.verdict = knowledgeVerdictError var ne net.Error - if errors.Is(err, context.DeadlineExceeded) || (errors.As(err, &ne) && ne.Timeout()) { + switch { + case errors.As(err, &rl): + // Nothing was answered: the call is not spent against the + // budgets, so a throttled hour does not also become an + // over-budget hour. + dec.verdict = knowledgeVerdictRateLimited + d.budget.refund(agent, spent) + case errors.Is(err, context.DeadlineExceeded) || (errors.As(err, &ne) && ne.Timeout()): dec.verdict = knowledgeVerdictTimeout } return dec @@ -375,6 +476,9 @@ func (d *knowledgeAdviserLLM) complete(ctx context.Context, r config.ResolvedAdv return cr, fmt.Errorf("knowledge advisor request failed: %w", err) } defer func() { _ = resp.Body.Close() }() + if resp.StatusCode == http.StatusTooManyRequests { + return cr, newKnowledgeAdviserLLMRateLimited(resp, time.Now()) + } if resp.StatusCode != http.StatusOK { return cr, fmt.Errorf("knowledge advisor model returned %d", resp.StatusCode) } @@ -421,12 +525,131 @@ func (d *knowledgeAdviserLLM) logDecision(agent, kickID string, scope crux.Knowl if dec.budget != "" { attrs = append(attrs, "budget", dec.budget) } + if dec.verdict == knowledgeVerdictRateLimited { + attrs = append(attrs, "retry_after_s", dec.retryAfter.Seconds(), "retried", dec.retried) + } if dec.err != nil { attrs = append(attrs, "error", dec.err.Error()) } d.logger.Info("knowledge advisor: decision", attrs...) } +// knowledgeAdviserLLMRateLimited is a 429 from the model endpoint with what +// the upstream said about it. +type knowledgeAdviserLLMRateLimited struct { + // retryAfter is the parsed Retry-After, zero when absent or malformed. + retryAfter time.Duration + // headers holds retry-after and the x-ratelimit-* headers, lower-cased. + headers map[string]string + // body is the response body, capped at knowledgeAdviserLLMErrorBodyRunes. + body string +} + +func (e *knowledgeAdviserLLMRateLimited) Error() string { + return fmt.Sprintf("knowledge advisor model returned %d", http.StatusTooManyRequests) +} + +// newKnowledgeAdviserLLMRateLimited reads the 429's headers and a bounded +// slice of its body; the body is drained no further. +func newKnowledgeAdviserLLMRateLimited(resp *http.Response, now time.Time) *knowledgeAdviserLLMRateLimited { + e := &knowledgeAdviserLLMRateLimited{headers: map[string]string{}} + for name, values := range resp.Header { + lower := strings.ToLower(name) + if (lower == "retry-after" || strings.HasPrefix(lower, knowledgeAdviserLLMRateLimitHeaderPrefix)) && len(values) > 0 { + e.headers[lower] = values[0] + } + } + e.retryAfter = parseRetryAfter(resp.Header.Get("Retry-After"), now) + raw, _ := io.ReadAll(io.LimitReader(resp.Body, int64(knowledgeAdviserLLMErrorBodyRunes)*4)) + e.body = truncateRunes(strings.Join(strings.Fields(string(raw)), " "), knowledgeAdviserLLMErrorBodyRunes) + return e +} + +// parseRetryAfter reads a Retry-After header: delay seconds or an HTTP date. +// Anything else, or a date already past, is zero. +func parseRetryAfter(v string, now time.Time) time.Duration { + v = strings.TrimSpace(v) + if v == "" { + return 0 + } + if secs, err := strconv.ParseFloat(v, 64); err == nil { + if secs <= 0 { + return 0 + } + return time.Duration(secs * float64(time.Second)) + } + if at, err := http.ParseTime(v); err == nil && at.After(now) { + return at.Sub(now) + } + return 0 +} + +// knowledgeAdviserLLMRetryWait is how long to wait before the single retry of +// a 429: the Retry-After when the upstream sent one, else the jittered +// backoff. ok is false when that wait would leave less than the retry +// headroom before ctx's deadline, so the retry is skipped and the decision +// records rate-limited without a second call. +func knowledgeAdviserLLMRetryWait(ctx context.Context, retryAfter time.Duration, now time.Time) (time.Duration, bool) { + wait := retryAfter + if wait <= 0 { + wait = knowledgeAdviserLLMRetryBackoff + rand.N(knowledgeAdviserLLMRetryBackoff) + } + if deadline, ok := ctx.Deadline(); ok && now.Add(wait+knowledgeAdviserLLMRetryHeadroom).After(deadline) { + return wait, false + } + return wait, true +} + +// logRateLimited logs a 429: the first in each WARN window at WARN with the +// upstream's rate-limit headers and body, the rest at DEBUG. The decision +// line itself still goes out at INFO. +func (d *knowledgeAdviserLLM) logRateLimited(agent string, rl *knowledgeAdviserLLMRateLimited, retried bool) { + now := d.now() + d.mu.Lock() + first := d.rateLimitWarnedAt.IsZero() || now.Sub(d.rateLimitWarnedAt) >= knowledgeAdviserLLMRateLimitWarnWindow + if first { + d.rateLimitWarnedAt = now + } + d.mu.Unlock() + attrs := []any{ + "agent", agent, + "retry_after_s", rl.retryAfter.Seconds(), + "retried", retried, + "waiting", d.waiting.Load(), + } + if first { + for _, k := range slices.Sorted(maps.Keys(rl.headers)) { + attrs = append(attrs, "header_"+strings.ReplaceAll(k, "-", "_"), rl.headers[k]) + } + attrs = append(attrs, "body", rl.body, "warn_window", knowledgeAdviserLLMRateLimitWarnWindow.String()) + d.logger.Warn("knowledge advisor: model endpoint rate limited (429)", attrs...) + return + } + d.logger.Debug("knowledge advisor: model endpoint rate limited (429)", attrs...) +} + +// acquireSlot takes the hive-wide in-flight slot, or reports false when ctx +// ends first. A nil slots channel is uncapped. +func (d *knowledgeAdviserLLM) acquireSlot(ctx context.Context) bool { + if d.slots == nil { + return true + } + d.waiting.Add(1) + defer d.waiting.Add(-1) + select { + case d.slots <- struct{}{}: + return true + case <-ctx.Done(): + return false + } +} + +func (d *knowledgeAdviserLLM) releaseSlot() { + if d.slots != nil { + <-d.slots + } +} + type knowledgeAdviserLLMMessage struct { Role string `json:"role"` Content string `json:"content"` @@ -470,9 +693,10 @@ func newKnowledgeAdviserLLMBudget(now func() time.Time) *knowledgeAdviserLLMBudg return &knowledgeAdviserLLMBudget{now: now, events: make(map[string][]time.Time)} } -// take spends one call for agent when both budgets allow it and returns ""; -// otherwise it spends nothing and names the budget that is exhausted. -func (b *knowledgeAdviserLLMBudget) take(agent string, perAgentHour, perDay int) string { +// take spends one call for agent when both budgets allow it and returns "" +// with the time the call was stamped (for refund); otherwise it spends +// nothing and names the budget that is exhausted. +func (b *knowledgeAdviserLLMBudget) take(agent string, perAgentHour, perDay int) (string, time.Time) { now := b.now() b.mu.Lock() defer b.mu.Unlock() @@ -494,14 +718,41 @@ func (b *knowledgeAdviserLLMBudget) take(agent string, perAgentHour, perDay int) switch { case perAgentHour > 0 && len(events) >= perAgentHour: b.events[agent] = events - return "agent-hour" + return "agent-hour", now case perDay > 0 && b.used >= perDay: if len(events) > 0 { b.events[agent] = events } - return "fleet-day" + return "fleet-day", now } b.events[agent] = append(events, now) b.used++ - return "" + return "", now +} + +// refund hands back the call take stamped at `at` for agent: the model never +// answered it (rate-limited, or the in-flight slot never freed), so it must +// not count against the agent's hour or the fleet's day. A stamp that has +// already rolled out of the window, or a day that has rolled over, refunds +// nothing. +func (b *knowledgeAdviserLLMBudget) refund(agent string, at time.Time) { + b.mu.Lock() + defer b.mu.Unlock() + events := b.events[agent] + removed := false + for i := len(events) - 1; i >= 0; i-- { + if events[i].Equal(at) { + events = append(events[:i:i], events[i+1:]...) + if len(events) == 0 { + delete(b.events, agent) + } else { + b.events[agent] = events + } + removed = true + break + } + } + if removed && b.used > 0 && at.UTC().Format(time.DateOnly) == b.day { + b.used-- + } } diff --git a/src/cmd/hive/knowledge_adviser_llm_test.go b/src/cmd/hive/knowledge_adviser_llm_test.go index 9a478e9cd7..21ec2e14ce 100644 --- a/src/cmd/hive/knowledge_adviser_llm_test.go +++ b/src/cmd/hive/knowledge_adviser_llm_test.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "context" "encoding/json" "fmt" "log/slog" @@ -14,6 +15,7 @@ import ( "github.com/hivecommons/hive/crux" "github.com/hivecommons/hive/crux/logscrub" + "github.com/hivecommons/hive/internal/testutil" "github.com/hivecommons/hive/pkg/config" "github.com/hivecommons/hive/pkg/knowledge" "github.com/hivecommons/hive/pkg/scheduler" @@ -70,6 +72,9 @@ type llmFixture struct { handler http.HandlerFunc lastReq knowledgeAdviserLLMRequest kicks map[string]scheduler.KickSummary + // waits records every wait the decorator asked for; the fixture's wait + // never sleeps. + waits []time.Duration } func (f *llmFixture) with(fn func()) { @@ -122,10 +127,7 @@ func newLLMFixture(t *testing.T, a config.AdvisorLLMConfig) *llmFixture { } return } - _ = json.NewEncoder(w).Encode(map[string]any{ - "choices": []any{map[string]any{"message": map[string]any{"content": reply}}}, - "usage": map[string]any{"prompt_tokens": 410, "completion_tokens": 17}, - }) + writeLLMReply(w, reply) })) t.Cleanup(func() { close(release); f.srv.Close() }) @@ -147,16 +149,84 @@ func newLLMFixture(t *testing.T, a config.AdvisorLLMConfig) *llmFixture { f.with(func() { k, ok = f.kicks[agent] }) return k, ok }, - http: &http.Client{}, - now: now, - logger: slog.New(slog.NewJSONHandler(f.logs, nil)), - budget: newKnowledgeAdviserLLMBudget(now), + http: &http.Client{}, + now: now, + logger: slog.New(slog.NewJSONHandler(f.logs, &slog.HandlerOptions{Level: slog.LevelDebug})), + budget: newKnowledgeAdviserLLMBudget(now), + slots: newKnowledgeAdviserLLMSlots(), + wait: func(ctx context.Context, d time.Duration) bool { + f.with(func() { f.waits = append(f.waits, d) }) + return ctx.Err() == nil + }, delivered: map[string]*knowledgeAdviserLLMKick{}, } f.kicks["developer"] = scheduler.KickSummary{ID: "developer#1", Text: "#42 Fix the tree pin [kind/bug]"} return f } +// writeLLMReply is the model server's 200 answer carrying reply. +func writeLLMReply(w http.ResponseWriter, reply string) { + _ = json.NewEncoder(w).Encode(map[string]any{ + "choices": []any{map[string]any{"message": map[string]any{"content": reply}}}, + "usage": map[string]any{"prompt_tokens": 410, "completion_tokens": 17}, + }) +} + +const llmTest429Body = `{"error":{"message":"rate limit exceeded","code":"rate_limited"}}` + +// rateLimit makes the model answer 429 (with the Copilot-style rate-limit +// headers and, when non-empty, Retry-After) to the first n requests and the +// fixture's reply afterwards. +func (f *llmFixture) rateLimit(n int, retryAfter string) { + served := 0 + f.with(func() { + f.handler = func(w http.ResponseWriter, _ *http.Request) { + var throttle bool + var reply string + f.with(func() { + served++ + throttle = served <= n + reply = f.reply + }) + if !throttle { + writeLLMReply(w, reply) + return + } + if retryAfter != "" { + w.Header().Set("Retry-After", retryAfter) + } + w.Header().Set("X-RateLimit-Remaining", "0") + w.Header().Set("X-RateLimit-Limit", "60") + w.WriteHeader(http.StatusTooManyRequests) + _, _ = w.Write([]byte(llmTest429Body)) + } + }) +} + +func (f *llmFixture) recordedWaits() (out []time.Duration) { + f.with(func() { out = append(out, f.waits...) }) + return out +} + +// logsAt returns the decoded log lines with msg at level. +func (f *llmFixture) logsAt(t *testing.T, msg, level string) []map[string]any { + t.Helper() + var out []map[string]any + for _, line := range strings.Split(strings.TrimSpace(f.logs.String()), "\n") { + if line == "" { + continue + } + var rec map[string]any + if err := json.Unmarshal([]byte(line), &rec); err != nil { + t.Fatalf("log line %q: %v", line, err) + } + if rec["msg"] == msg && rec["level"] == level { + out = append(out, rec) + } + } + return out +} + var llmTestScope = crux.KnowledgeScope{ Kind: "path", Key: "src/pkg/tree", Path: "src/pkg/tree/pin.go", Keywords: []string{"tree"}, Action: crux.KnowledgeActionEdit, PaneTail: "⏺ Edit(src/pkg/tree/pin.go)\n token [REDACTED]", @@ -573,3 +643,247 @@ func TestKnowledgeAdviserLLM_ReasonNeverDelivered(t *testing.T) { } } } + +// 429 handling (hivecommons/hive#18151): one retry after Retry-After inside +// the timeout, the outcome recorded as rate-limited, no budget spent, and one +// advisor call in flight across the hive. + +const llmTest429Msg = "knowledge advisor: model endpoint rate limited (429)" + +func TestKnowledgeAdviserLLM_RateLimitedRetriesOnceThenRecords(t *testing.T) { + f := newLLMFixture(t, config.AdvisorLLMConfig{Mode: config.AdvisorLLMModeRank}) + f.rateLimit(2, "1") + if got := f.d.Advise("developer", llmTestScope); len(got) != 0 { + t.Errorf("rate-limited delivered %v", slugsOf(got)) + } + if n := f.callCount(); n != 2 { + t.Errorf("calls = %d, want exactly one retry (2)", n) + } + if w := f.recordedWaits(); len(w) != 1 || w[0] != time.Second { + t.Errorf("waits = %v, want [1s] from Retry-After", w) + } + d := f.lastDecision(t) + if d["verdict"] != knowledgeVerdictRateLimited || d["outcome"] != knowledgeVerdictRateLimited { + t.Errorf("decision = %v, want verdict and outcome rate-limited", d) + } + if d["retried"] != true || d["retry_after_s"] != float64(1) { + t.Errorf("decision retried/retry_after_s = %v/%v", d["retried"], d["retry_after_s"]) + } + warns := f.logsAt(t, llmTest429Msg, "WARN") + if len(warns) != 1 { + t.Fatalf("WARN lines = %d, want 1 (first 429 in the window); logs:\n%s", len(warns), f.logs.String()) + } + w := warns[0] + if w["header_retry_after"] != "1" || w["header_x_ratelimit_remaining"] != "0" || w["header_x_ratelimit_limit"] != "60" { + t.Errorf("WARN line lacks the rate-limit headers: %v", w) + } + if body, _ := w["body"].(string); !strings.Contains(body, "rate limit exceeded") { + t.Errorf("WARN body = %q", body) + } + if debugs := f.logsAt(t, llmTest429Msg, "DEBUG"); len(debugs) != 1 || debugs[0]["retried"] != true { + t.Errorf("DEBUG lines = %v, want the retry's 429 only", debugs) + } +} + +func TestKnowledgeAdviserLLM_RateLimitedRetrySucceeds(t *testing.T) { + f := newLLMFixture(t, config.AdvisorLLMConfig{Mode: config.AdvisorLLMModeRank}) + f.setReply(`{"slug": "first", "reason": "editing the pin"}`) + f.rateLimit(1, "0.5") + if got := slugsOf(f.d.Advise("developer", llmTestScope)); strings.Join(got, ",") != "first" { + t.Errorf("advice after the retry = %v, want [first]", got) + } + if n := f.callCount(); n != 2 { + t.Errorf("calls = %d, want 2", n) + } + if d := f.lastDecision(t); d["outcome"] != "delivered" || d["verdict"] != "pick" { + t.Errorf("decision = %v", d) + } +} + +func TestKnowledgeAdviserLLM_RateLimitedWithoutRetryAfterBacksOffWithJitter(t *testing.T) { + f := newLLMFixture(t, config.AdvisorLLMConfig{Mode: config.AdvisorLLMModeRank}) + f.rateLimit(2, "") + f.d.Advise("developer", llmTestScope) + w := f.recordedWaits() + if len(w) != 1 || w[0] < knowledgeAdviserLLMRetryBackoff || w[0] >= 2*knowledgeAdviserLLMRetryBackoff { + t.Errorf("waits = %v, want one in [%s, %s)", w, knowledgeAdviserLLMRetryBackoff, 2*knowledgeAdviserLLMRetryBackoff) + } + if d := f.lastDecision(t); d["outcome"] != knowledgeVerdictRateLimited || d["retry_after_s"] != float64(0) { + t.Errorf("decision = %v", d) + } +} + +func TestKnowledgeAdviserLLM_RateLimitedSkipsRetryBeyondTimeout(t *testing.T) { + f := newLLMFixture(t, config.AdvisorLLMConfig{Mode: config.AdvisorLLMModeRank, TimeoutS: 5}) + f.rateLimit(2, "30") + f.d.Advise("developer", llmTestScope) + if n := f.callCount(); n != 1 { + t.Errorf("calls = %d, want 1: a 30s Retry-After cannot fit a 5s timeout", n) + } + if w := f.recordedWaits(); len(w) != 0 { + t.Errorf("waited %v for a retry that cannot fit", w) + } + if d := f.lastDecision(t); d["outcome"] != knowledgeVerdictRateLimited || d["retried"] != false || d["retry_after_s"] != float64(30) { + t.Errorf("decision = %v", d) + } +} + +func TestKnowledgeAdviserLLM_RateLimitedOnErrorMatcherFallsBack(t *testing.T) { + f := newLLMFixture(t, config.AdvisorLLMConfig{Mode: config.AdvisorLLMModeRank, OnError: config.AdvisorLLMOnErrorMatcher}) + f.rateLimit(2, "1") + if got := slugsOf(f.d.Advise("developer", llmTestScope)); strings.Join(got, ",") != "first" { + t.Errorf("on_error matcher = %v, want [first]", got) + } + if d := f.lastDecision(t); d["outcome"] != "fallback-matcher" || d["verdict"] != knowledgeVerdictRateLimited { + t.Errorf("decision = %v", d) + } +} + +func TestKnowledgeAdviserLLM_RateLimitedDoesNotConsumeBudget(t *testing.T) { + f := newLLMFixture(t, config.AdvisorLLMConfig{Mode: config.AdvisorLLMModeRank, MaxCallsPerAgentHour: 1}) + f.rateLimit(2, "1") + f.d.Advise("developer", llmTestScope) + if d := f.lastDecision(t); d["outcome"] != knowledgeVerdictRateLimited { + t.Fatalf("decision = %v", d) + } + f.d.budget.mu.Lock() + events, used := len(f.d.budget.events["developer"]), f.d.budget.used + f.d.budget.mu.Unlock() + if events != 0 || used != 0 { + t.Errorf("after a rate-limited decision agent events = %d fleet used = %d, want 0/0", events, used) + } + // The one slot of the hour is still free: the next decision reaches the + // model instead of being over-budget. + f.with(func() { f.handler = nil }) + f.d.Advise("developer", llmTestScope) + if n := f.callCount(); n != 3 { + t.Errorf("calls = %d, want 3 (the rate-limited pair plus one real call)", n) + } + if d := f.lastDecision(t); d["outcome"] != "null" { + t.Errorf("decision after the throttled one = %v, want a real null verdict, not over-budget", d) + } +} + +func TestKnowledgeAdviserLLM_GlobalCapSerializesAgents(t *testing.T) { + f := newLLMFixture(t, config.AdvisorLLMConfig{Mode: config.AdvisorLLMModeRank}) + f.setKick("scanner", scheduler.KickSummary{ID: "scanner#1", Text: "#7 Scan"}) + release := make(chan struct{}) + var seq []string + served := 0 + f.with(func() { + f.handler = func(w http.ResponseWriter, _ *http.Request) { + var n int + f.with(func() { + served++ + n = served + seq = append(seq, fmt.Sprintf("start%d", n)) + }) + if n == 1 { + <-release + } + f.with(func() { seq = append(seq, fmt.Sprintf("end%d", n)) }) + writeLLMReply(w, `{"slug": null, "reason": "reading"}`) + } + }) + var wg sync.WaitGroup + wg.Add(1) + go func() { + defer wg.Done() + f.d.Advise("developer", llmTestScope) + }() + testutil.Eventually(t, 5*time.Second, func() bool { return f.callCount() == 1 }, "first agent's call did not reach the model") + wg.Add(1) + go func() { + defer wg.Done() + f.d.Advise("scanner", llmTestScope) + }() + testutil.Eventually(t, 5*time.Second, func() bool { return f.d.waiting.Load() == 1 }, "second agent is not waiting on the hive-wide slot") + if n := f.callCount(); n != 1 { + t.Fatalf("calls = %d while the first is in flight, want 1", n) + } + close(release) + wg.Wait() + if n := f.callCount(); n != 2 { + t.Errorf("calls = %d, want 2", n) + } + var got string + f.with(func() { got = strings.Join(seq, ",") }) + if got != "start1,end1,start2,end2" { + t.Errorf("call sequence = %s, want start1,end1,start2,end2", got) + } + if n := len(f.decisions(t)); n != 2 { + t.Errorf("decisions logged = %d, want 2", n) + } + if f.d.waiting.Load() != 0 { + t.Errorf("waiting = %d after both calls returned", f.d.waiting.Load()) + } +} + +func TestKnowledgeAdviserLLMBudgetRefund(t *testing.T) { + clock := time.Date(2026, 11, 13, 20, 0, 0, 0, time.UTC) + b := newKnowledgeAdviserLLMBudget(func() time.Time { return clock }) + if over, _ := b.take("developer", 2, 10); over != "" { + t.Fatalf("first take over = %q", over) + } + clock = clock.Add(time.Minute) + over, stamp := b.take("developer", 2, 10) + if over != "" { + t.Fatalf("second take over = %q", over) + } + if over, _ := b.take("developer", 2, 10); over != "agent-hour" { + t.Fatalf("third take over = %q, want agent-hour", over) + } + b.refund("developer", stamp) + b.mu.Lock() + events, used := len(b.events["developer"]), b.used + b.mu.Unlock() + if events != 1 || used != 1 { + t.Errorf("after refund events = %d used = %d, want 1/1", events, used) + } + if over, _ := b.take("developer", 2, 10); over != "" { + t.Errorf("take after refund over = %q, want the slot back", over) + } + // A stamp that is not there refunds no event and never drives used negative. + b.refund("developer", stamp.Add(time.Hour)) + b.refund("other", stamp) + b.mu.Lock() + events, used = len(b.events["developer"]), b.used + b.mu.Unlock() + if events != 2 || used != 2 { + t.Errorf("after bogus refunds events = %d used = %d, want 2/2 (the take after the refund spent one again)", events, used) + } +} + +func TestParseRetryAfter(t *testing.T) { + now := time.Date(2026, 11, 13, 20, 0, 0, 0, time.UTC) + for in, want := range map[string]time.Duration{ + "": 0, + "3": 3 * time.Second, + " 1.5 ": 1500 * time.Millisecond, + "0": 0, + "-2": 0, + "soon": 0, + now.Add(7 * time.Second).UTC().Format(http.TimeFormat): 7 * time.Second, + now.Add(-7 * time.Second).UTC().Format(http.TimeFormat): 0, + } { + if got := parseRetryAfter(in, now); got != want { + t.Errorf("parseRetryAfter(%q) = %s, want %s", in, got, want) + } + } +} + +func TestKnowledgeAdviserLLMRetryWait(t *testing.T) { + now := time.Date(2026, 11, 13, 20, 0, 0, 0, time.UTC) + ctx, cancel := context.WithDeadline(context.Background(), now.Add(time.Duration(config.DefaultAdvisorLLMTimeoutS)*time.Second)) + defer cancel() + if wait, ok := knowledgeAdviserLLMRetryWait(ctx, time.Second, now); !ok || wait != time.Second { + t.Errorf("1s Retry-After inside 15s = %s/%v, want 1s/true", wait, ok) + } + tooLong := time.Duration(config.DefaultAdvisorLLMTimeoutS)*time.Second - knowledgeAdviserLLMRetryHeadroom + time.Second + if _, ok := knowledgeAdviserLLMRetryWait(ctx, tooLong, now); ok { + t.Errorf("%s Retry-After leaves no headroom inside 15s, want no retry", tooLong) + } + if wait, ok := knowledgeAdviserLLMRetryWait(context.Background(), 0, now); !ok || wait < knowledgeAdviserLLMRetryBackoff || wait >= 2*knowledgeAdviserLLMRetryBackoff { + t.Errorf("no Retry-After, no deadline = %s/%v, want jittered backoff/true", wait, ok) + } +} diff --git a/src/cmd/hive/main.go b/src/cmd/hive/main.go index 95a59c3843..a2de30eb57 100644 --- a/src/cmd/hive/main.go +++ b/src/cmd/hive/main.go @@ -72,6 +72,7 @@ import ( "github.com/hivecommons/hive/pkg/nousengine" "github.com/hivecommons/hive/pkg/nousstore" "github.com/hivecommons/hive/pkg/persona" + "github.com/hivecommons/hive/pkg/perspsrc" "github.com/hivecommons/hive/pkg/planning" "github.com/hivecommons/hive/pkg/policies" "github.com/hivecommons/hive/pkg/promptsrc" @@ -1104,6 +1105,7 @@ func (b *boot) wireBootClosures() { WorkflowClusterRunner: workflowClusterRunner(), WorkflowSpecLLM: newWorkflowSpecRunner(func() *config.Config { return b.cfg }), AgentMgr: b.agentMgr, + Sessions: cruxruntime.NewUserSessions(b.logger), CruxRuntimeHealth: cruxRuntimeHealthFunc(b.cruxSupervision), // Provider gateways (#5565 slice 3): concrete openrouter/watsonx/ // linearagent adapters behind the dashboard's consumer-defined @@ -1158,18 +1160,19 @@ func (b *boot) wireBootClosures() { // #3972: hand the ACMM advisor the SAME cached fleet-stats collector // the heartbeat reads, so its merge-success signal reuses the existing // 30-minute collect loop instead of issuing a second GitHub fetch. - FleetStats: b.fleetStatsCollector, - Activity: b.activityCollector, - RepoCost: b.repoCostCollector, - LaunchCost: b.launchCostCollector, - BeadSynthesizer: b.beadSynth, - KnowledgeConnectors: b.knowledgeConnectorRuntime(), - KnowledgePublish: b.knowledgePublishRuntime(), - Inbox: b.inboxSvc, - BeadStores: b.beadStores, - BeadStoreLoadFailures: b.beadStoreLoadFailures, - AuditLedger: auditLedger, - AuditJournal: auditJournal, + FleetStats: b.fleetStatsCollector, + Activity: b.activityCollector, + RepoCost: b.repoCostCollector, + LaunchCost: b.launchCostCollector, + BeadSynthesizer: b.beadSynth, + KnowledgeConnectors: b.knowledgeConnectorRuntime(), + ReloadKnowledgeConnectors: b.reloadKnowledgeConnectors, + KnowledgePublish: b.knowledgePublishRuntime(), + Inbox: b.inboxSvc, + BeadStores: b.beadStores, + BeadStoreLoadFailures: b.beadStoreLoadFailures, + AuditLedger: auditLedger, + AuditJournal: auditJournal, AuditPublisherFunc: func() convergenceaudit.FindingPublisher { return b.findingPublisher }, @@ -1325,12 +1328,16 @@ func (b *boot) wireBootClosures() { ReInitFunc: func() { initAgentConfigDrivenSystems(b.cfg) }, - IoscanConfigApplied: b.reinstallIoscanClassifier, + IoscanConfigApplied: b.reinstallIoscanClassifier, + ApplyReferenceRepos: b.applyEditedReferenceRepos, + RemergeDerivedReferenceRepos: b.remergeLastDerivedReferenceRepos, ReviewConfigApplied: func(rc config.ReviewConfig) { if b.ghClient == nil { return } - installReviewRelaySettings(b.ghClient, b.cfg, b.logger) + // LinkedPerspectives is runtime-only, so re-resolve it before + // the relay reinstall or a dashboard PUT would drop the link. + applyReviewPerspectivesSource(context.Background(), b.cfg, b.perspectivesResolver, b.ghClient, b.logger) // review_bots.min_priority and fix_human_prs are edited // through the same PUT; reinstall so the thread reconciler // applies them without a restart (#10481). @@ -1503,6 +1510,18 @@ func (b *boot) bootGitHubWith(deps bootGitHubDeps) { // The same function every rebuild path uses (#9614). Only the tiers whose // dependencies exist yet apply here; bootAdvisory, bootAgents and // bootDashboard apply the rest as those dependencies come up. + // Resolve review.perspectives_source before the first relay install so + // the relay never boots on a set the link has since replaced. + var persFetcher defsrc.Fetcher + if b.ghClient != nil { + persFetcher = b.ghClient + } + b.perspectivesResolver = perspsrc.NewResolver( + persFetcher, + func(slug string) bool { return b.cfg.GitHubPromptAllowed(slug) }, + b.logger, + ) + perspsrc.ApplyToConfig(b.ctx, b.cfg, b.perspectivesResolver, b.logger) b.configureGitHubClient(b.ghClient) // Long-lived consumers read the client through this provider, never a // captured b.ghClient, so every rebuild reaches them (#9621). @@ -3311,12 +3330,20 @@ func (b *boot) bootKnowledgeWith(deps bootKnowledgeDeps) { FilePath: doc.FilePath, Layer: knowledge.LayerType(doc.Layer), } - meta, err := b.knowledgeAPI.ImportDocument(b.ctx, docConfig) + // EnsureDocument keeps a document already reloaded from disk and + // re-imports one whose on-disk state is gone, so a configured + // document survives an upgrade the way git sources do (#18058). + meta, imported, err := b.knowledgeAPI.EnsureDocument(b.ctx, docConfig) if err != nil { b.logger.Warn("failed to import document source", "name", doc.Name, "error", err, ) + } else if !imported { + b.logger.Debug("document source already loaded", + "name", doc.Name, + "facts", meta.FactCount, + ) } else { b.logger.Info("document source imported", "name", doc.Name, @@ -4090,6 +4117,10 @@ func (b *boot) bootWatchersWith(deps bootWatchersDeps) { // Re-sync subsystems that cache config values b.ghClient.SetRepos(b.cfg.Project.Repos) + // project.reference_repos: the file carries only the hand-written + // entries, so the derived ones are merged back now and re-listed + // from the request issues in the background (hivecommons/hive#18159). + b.reconcileReferenceReposAfterReload() b.observeWritebackRepos() if b.metricsCollector != nil { b.metricsCollector.SetProjectScope(b.cfg.Project.Org, metricsPrimaryRepo(b.cfg.Project), b.cfg.Project.Repos, b.cfg.EffectiveAIAuthor()) @@ -4151,6 +4182,7 @@ func (b *boot) bootWatchersWith(deps bootWatchersDeps) { // fields; a fetch failure keeps each agent's baked definition. Runs before // initAgentConfigDrivenSystems so downstream systems see the merged config. defsrc.ApplyToConfig(context.Background(), b.cfg, b.definitionResolver, b.logger) + applyReviewPerspectivesSource(context.Background(), b.cfg, b.perspectivesResolver, b.ghClient, b.logger) if err := b.cfg.ExpandAgentReplicas(); err != nil { b.logger.Warn("failed to expand agent replicas after config reload", "error", err) } @@ -4386,6 +4418,10 @@ func (b *boot) bootProxyWith(deps bootProxyDeps) { // unconditionally: with HIVE_PROXY_INJECT_GH_AUTH unset (the default) // the proxy never consults the source and the registry stays empty. b.githubProxy.SetAgentTokenSource(github.AgentProxyToken) + // project.reference_repos (#18015): study-only repos outside the + // project, with a rare per-entry issues/prs opt-in served by a + // credential verified here (boot refuses otherwise). + b.wireReferenceRepos() // Durable attempted-vs-blocked write counters (#12069); observation only. if err := proxy.MigrateWriteCounters(proxy.LegacyWriteCountersPath, proxy.WriteCountersPath, time.Now()); err != nil { b.logger.Warn("write counters: legacy file not migrated", "error", err) @@ -5967,6 +6003,16 @@ func (b *boot) bootHeartbeatWith(deps bootHeartbeatDeps) { "app_id", b.cfg.GitHub.AppID, "installation_id", b.cfg.GitHub.InstallationID, ) + } else if discoveryAppAuthStale(b.appAuth, b.cfg.GitHub, rebuildKeyFile) { + // A real App with no installation yet (forge switch on claim): + // give installation discovery the NEW App's JWT so it polls + // the forge the repos actually live on. + newAppAuth, err := github.NewAppAuth(b.cfg.GitHub.AppID, 0, rebuildKeyFile, b.logger, b.cfg.GitHub.ResolvedAPIURL()) + if err != nil { + b.logger.Error("github app auth init for installation discovery failed", "error", err) + return + } + b.adoptDiscoveryAppAuth(newAppAuth) } }), diff --git a/src/cmd/hive/main_eval_cycle.go b/src/cmd/hive/main_eval_cycle.go index cb4eedeabf..08816e1a23 100644 --- a/src/cmd/hive/main_eval_cycle.go +++ b/src/cmd/hive/main_eval_cycle.go @@ -91,6 +91,11 @@ var providerBudgetNotify governor.ProviderBudgetNotifyState // providerBudgetNotify: runEvalCycle has no state of its own. var providerBudgetProbe governor.ProviderBudgetProbeState +// agentQuotaHold remembers, per agent out of provider quota, when its last +// probe kick was delivered (hivecommons/hive#18112). Package-level for the +// same reason as providerBudgetProbe. +var agentQuotaHold agentQuotaHoldState + // governorKickFairness remembers, per agent, how many cycles in a row the // governor kick was deferred behind a delivered review-fix kick // (hivecommons/hive#15166). Package-level for the same reason as @@ -996,7 +1001,7 @@ func runEvalCycle( suppressKicks := governor.ProviderBudgetSuppresses(providerBudgetLatched, providerBudgetProbe.Freshest(providerBudgetLastRebuff), time.Now(), providerBudgetProbeInterval) budgetAlert := decideProviderBudgetAlert(providerBudgetLatched, suppressKicks, - providerBudgetCause, providerBudgetSince, providerBudgetRebuffs, + providerBudgetCause, providerBudgetSince, providerBudgetRebuffs, providerBudgetProbeInterval, func() string { return hub.QuotaExhaustedAgentReason(hub.QuotaExhaustedProcessCount(agentMgr.AllStatuses())) }) @@ -1204,6 +1209,23 @@ func runEvalCycle( "last_rebuff", providerBudgetLastRebuff, "probe_interval", providerBudgetProbeInterval) } messages = kickGate.Kept + // PER-AGENT PROVIDER QUOTA HOLD (#18112). The spend gate above only sees + // agents behind the hive's inference gateway; an agent whose own provider + // refuses it for quota (Copilot's "monthly quota") is held here, per + // agent, from the QuotaExhausted flag every backend reports. Review-swarm + // kicks are in this list too, so a held reviewer gets no review work. + quotaProbeNow := map[string]bool{} + { + kept, quotaWithheld, quotaProbes := agentQuotaHold.gate(messages, agentMgr.AllStatuses(), time.Now(), providerBudgetProbeInterval) + if len(quotaWithheld) > 0 || len(quotaProbes) > 0 { + logger.Warn("provider quota exhausted: withholding agent kicks", + "withheld", quotaWithheld, "probe", quotaProbes, "probe_interval", providerBudgetProbeInterval) + } + for _, agent := range quotaProbes { + quotaProbeNow[agent] = true + } + messages = kept + } // Self-upgrade drain (#11876): a hub-instructed upgrade is waiting for // in-flight kicks to end, so starting new turns would only feed the // restart. Withheld kicks stay due and go out from the new process. @@ -1646,6 +1668,12 @@ func runEvalCycle( }, onDelivered: func(msg scheduler.KickMessage, deliveredAt time.Time) { deliveredKicks = append(deliveredKicks, msg) + if quotaProbeNow[msg.Agent] { + // The only kick a held agent can receive is its probe; + // stamp it on delivery so a failed or deferred probe + // is retried next cycle (#18112). + agentQuotaHold.MarkProbeDelivered(msg.Agent, deliveredAt) + } gov.RecordKickForRepoAt(msg.Agent, msg.Repo, deliveredAt) dashSrv.AuditLog("governor", "kick", "trigger=governor-eval", msg.Agent) diff --git a/src/cmd/hive/main_github_app.go b/src/cmd/hive/main_github_app.go index b747bdca20..a3a89f0f26 100644 --- a/src/cmd/hive/main_github_app.go +++ b/src/cmd/hive/main_github_app.go @@ -558,3 +558,57 @@ func advisoryIssueMissingError(repo string, cause error) string { } return base } + +// discoveryAppAuthStale reports whether the AppAuth the installation-discovery +// loop is polling with no longer names the App the config now claims. +// +// A hub-delivered identity with installation_id 0 (a placeholder claimed onto +// a DIFFERENT forge than its pool default: GHE slot β†’ github.com repo) is not +// "usable" β€” HasUsableApp is false β€” so the heartbeat rebuild skipped it and +// the boot-time AppAuth (old App, old api_url) kept serving auto-discovery. +// Discovery then polled the wrong forge forever: the installation the owner +// made on the right one could never be found, and the hive sat at "App has no installation" until a pod restart. Boot builds an AppAuth the +// moment a real app_id and key exist precisely so discovery has a JWT to use; +// this is the same rule applied to a hub delivery. +func discoveryAppAuthStale(current *github.AppAuth, gh config.GitHubConfig, keyFile string) bool { + if !gh.HasApp() || gh.InstallationID != 0 || keyFile == "" { + return false + } + if current == nil { + return true + } + return current.AppID() != gh.AppID || !sameAPIURL(current.APIURL(), gh.ResolvedAPIURL()) +} + +// sameAPIURL compares two GitHub API base URLs ignoring scheme-case and a +// trailing slash, the two spellings the config and the client disagree on. +func sameAPIURL(a, b string) bool { + norm := func(s string) string { return strings.TrimRight(strings.ToLower(strings.TrimSpace(s)), "/") } + return norm(a) == norm(b) +} + +// adoptDiscoveryAppAuth hands a not-yet-installed App's AppAuth to the +// installation-discovery loop and flips the banner/hub state to +// not-installed for the App it now names, then forces one discovery pass so +// an installation already made on the new forge is adopted without waiting +// for the next tick. No client is built: there is no installation to mint +// against yet β€” AutoDiscoverGitHubInstallationID builds it on success. +func (b *boot) adoptDiscoveryAppAuth(newAppAuth *github.AppAuth) { + if b == nil || newAppAuth == nil { + return + } + b.appAuth = newAppAuth + diag := "GitHub App " + strconv.FormatInt(b.cfg.GitHub.AppID, 10) + + " has no installation for this org β€” install it (the spoke adopts the installation automatically)" + b.appAuthFailure = "The GitHub App is configured but has no installation. Install the app on your org; this hive will discover the installation automatically." + if b.dashSrv != nil { + b.dashSrv.UpdateGitHubAppAuth(newAppAuth) + b.dashSrv.SetGitHubAppRequired(true) + b.dashSrv.SetGitHubAppState(github.AppStateNotInstalled.String()) + b.dashSrv.SetGitHubAppPermIssue(diag) + go func() { _, _ = b.dashSrv.AutoDiscoverGitHubInstallationID(b.ctx, true) }() + } + b.logger.Info("github app identity adopted for installation discovery", + "app_id", newAppAuth.AppID(), "api_url", newAppAuth.APIURL()) +} diff --git a/src/cmd/hive/main_sweeps.go b/src/cmd/hive/main_sweeps.go index b8e02989f4..6853892a9e 100644 --- a/src/cmd/hive/main_sweeps.go +++ b/src/cmd/hive/main_sweeps.go @@ -27,6 +27,7 @@ import ( "github.com/hivecommons/hive/pkg/governor" "github.com/hivecommons/hive/pkg/hooks" "github.com/hivecommons/hive/pkg/notify" + "github.com/hivecommons/hive/pkg/perspsrc" "github.com/hivecommons/hive/pkg/planning" "github.com/hivecommons/hive/pkg/review" "github.com/hivecommons/hive/pkg/rotation" @@ -2062,6 +2063,15 @@ func reviewAuthorBase(cfg *config.Config, agent string) string { return agent } +// applyReviewPerspectivesSource re-resolves review.perspectives_source and +// reinstalls the relay settings, so the relay validates verdicts against the +// set the link now names rather than the one it booted with. With no source +// set it fetches nothing and the inline set stands. +func applyReviewPerspectivesSource(ctx context.Context, cfg *config.Config, resolver *perspsrc.Resolver, client *github.Client, logger *slog.Logger) { + perspsrc.ApplyToConfig(ctx, cfg, resolver, logger) + installReviewRelaySettings(client, cfg, logger) +} + // installReviewRelaySettings gives a (possibly rebuilt) GitHub client the // review-relay knobs that live in cfg.Review: which repos may be revised in // place, which perspectives a verdict may name, and whether comments carry a @@ -2337,7 +2347,9 @@ func applyHumanDecisionLabels(ctx context.Context, cfg *config.Config, ghClient // silently becomes the current time would re-open every verdict in the // artifact at once β€” the opposite of the narrow, deliberate correction this // setting exists for. -// reviewPerspectiveSet resolves the hive's configured review perspectives. +// reviewPerspectiveSet resolves the hive's configured review perspectives: the +// set linked from review.perspectives_source when one has resolved, otherwise +// the inline review.perspectives (ReviewConfig.EffectivePerspectives). // // A bad configuration falls back to the built-in set and says so, rather than // disabling review. Silently reviewing nothing would look identical to a healthy @@ -2347,7 +2359,7 @@ func reviewPerspectiveSet(cfg *config.Config, logger *slog.Logger) review.Perspe if cfg == nil { return review.PerspectiveSet{} } - set, err := review.NewPerspectiveSet(cfg.Review.Perspectives, cfg.Review.PerspectivePrompts) + set, err := review.NewPerspectiveSet(cfg.Review.EffectivePerspectives()) if err != nil { if logger != nil { logger.Warn("review.perspectives is invalid; reviewing with the built-in set until it is fixed", diff --git a/src/cmd/hive/provider_budget_alert_test.go b/src/cmd/hive/provider_budget_alert_test.go index bcfb10685f..878d96bb04 100644 --- a/src/cmd/hive/provider_budget_alert_test.go +++ b/src/cmd/hive/provider_budget_alert_test.go @@ -23,7 +23,7 @@ func neverCalled(t *testing.T) func() string { } func TestDecideProviderBudgetAlertLatchedSuspended(t *testing.T) { - got := decideProviderBudgetAlert(true, true, "spend cap hit", time.Now(), 1, neverCalled(t)) + got := decideProviderBudgetAlert(true, true, "spend cap hit", time.Now(), 1, 30*time.Minute, neverCalled(t)) if got.Clear { t.Fatal("a held latch must not clear the banner") @@ -40,7 +40,7 @@ func TestDecideProviderBudgetAlertLatchedSuspended(t *testing.T) { } func TestDecideProviderBudgetAlertLatchedProbing(t *testing.T) { - got := decideProviderBudgetAlert(true, false, "spend cap hit", time.Now(), 1, neverCalled(t)) + got := decideProviderBudgetAlert(true, false, "spend cap hit", time.Now(), 1, 30*time.Minute, neverCalled(t)) if !strings.Contains(got.Message, "probing with a single agent kick") { t.Fatalf("a stale latch probes rather than staying muted, got %q", got.Message) @@ -54,7 +54,7 @@ func TestDecideProviderBudgetAlertLatchedProbing(t *testing.T) { // between "one call was refused" and "this has been refusing since Tuesday". func TestDecideProviderBudgetAlertRepeatedRebuffsAreQuantified(t *testing.T) { since := time.Date(2026, 9, 16, 14, 30, 0, 0, time.UTC) - got := decideProviderBudgetAlert(true, true, "spend cap hit", since, 47, neverCalled(t)) + got := decideProviderBudgetAlert(true, true, "spend cap hit", since, 47, 30*time.Minute, neverCalled(t)) if !strings.Contains(got.Message, "47 refused calls") { t.Fatalf("repeated rebuffs must be counted, got %q", got.Message) @@ -66,7 +66,7 @@ func TestDecideProviderBudgetAlertRepeatedRebuffsAreQuantified(t *testing.T) { func TestDecideProviderBudgetAlertSingleRebuffIsNotQuantified(t *testing.T) { since := time.Date(2026, 9, 16, 14, 30, 0, 0, time.UTC) - got := decideProviderBudgetAlert(true, true, "spend cap hit", since, 1, neverCalled(t)) + got := decideProviderBudgetAlert(true, true, "spend cap hit", since, 1, 30*time.Minute, neverCalled(t)) if strings.Contains(got.Message, "refused calls") { t.Fatalf("a single rebuff must not be reported as a run of them, got %q", got.Message) @@ -80,7 +80,7 @@ func TestDecideProviderBudgetAlertSingleRebuffIsNotQuantified(t *testing.T) { // branch: the provider is not refusing on money, but agents are individually // out of quota, which is a different cause and must not be silent. func TestDecideProviderBudgetAlertQuotaExhaustedFallback(t *testing.T) { - got := decideProviderBudgetAlert(false, false, "", time.Now(), 0, + got := decideProviderBudgetAlert(false, false, "", time.Now(), 0, 30*time.Minute, func() string { return "3 agents out of quota" }) if got.Clear { @@ -89,13 +89,18 @@ func TestDecideProviderBudgetAlertQuotaExhaustedFallback(t *testing.T) { if !strings.Contains(got.Message, "provider quota exhausted") || !strings.Contains(got.Message, "3 agents out of quota") { t.Fatalf("quota exhaustion must name itself and its reason, got %q", got.Message) } + // #18112: the banner says the hive is holding those agents' kicks and + // when it retries them, not just that the quota ran out. + if !strings.Contains(got.Message, "their kicks are held and retried every 30m") { + t.Fatalf("quota exhaustion must say kicks are held and the probe interval, got %q", got.Message) + } if got.Cause != "" { t.Fatalf("only the latched branch replaces the cause, got %q", got.Cause) } } func TestDecideProviderBudgetAlertHealthyClears(t *testing.T) { - got := decideProviderBudgetAlert(false, false, "", time.Now(), 0, func() string { return "" }) + got := decideProviderBudgetAlert(false, false, "", time.Now(), 0, 30*time.Minute, func() string { return "" }) if !got.Clear { t.Fatal("a healthy provider with no exhausted quota must clear the banner, or it sticks forever") @@ -121,7 +126,7 @@ func TestDecideProviderBudgetAlertNeverRaisesAndClears(t *testing.T) { } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { - got := decideProviderBudgetAlert(tc.latched, tc.suppress, "cause", time.Now(), 1, + got := decideProviderBudgetAlert(tc.latched, tc.suppress, "cause", time.Now(), 1, 30*time.Minute, func() string { return tc.quota }) if got.Message != "" && got.Clear { t.Fatalf("must never raise and clear in the same cycle: %+v", got) diff --git a/src/cmd/hive/reference_repos.go b/src/cmd/hive/reference_repos.go new file mode 100644 index 0000000000..f6a645fbf8 --- /dev/null +++ b/src/cmd/hive/reference_repos.go @@ -0,0 +1,230 @@ +package main + +import ( + "context" + "errors" + "fmt" + "log/slog" + "os" + "sort" + + "github.com/hivecommons/hive/crux/proxy" + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/github" + wfstore "github.com/hivecommons/hive/pkg/workflowmodel/store" +) + +// referenceRepoCredentialExitCode marks an exit caused by a +// project.reference_repos entry that opted in to writes without a credential +// GitHub would honour for that repo (hivecommons/hive#18015). Its own code so +// the termination state says "install the App on the target org or supply +// write_token_file", not "bug". +const referenceRepoCredentialExitCode = 22 + +// derivedReadOnlyPoster posts the one-shot read-only notice on a request +// issue (hivecommons/hive#18158). Production is +// github.Client.CommentDerivedReadOnly; tests stub it. +type derivedReadOnlyPoster func(ctx context.Context, issueRepo string, number int, readOnly []config.ReferenceRepo) (bool, error) + +// referenceRepoDeps are the seams buildReferenceRepoPolicies needs: App +// installation discovery on a foreign org and token-identity resolution. +// Either may be nil when the hive has no GitHub App auth, in which case a +// write-mode entry can only be served by write_token_file. +type referenceRepoDeps struct { + botLogin string + discover func(ctx context.Context, org string) (int64, error) + whoAmI func(ctx context.Context, token string) (string, error) + auth func() *github.AppAuth +} + +// referenceRepoResolution is what buildReferenceRepoPolicies produces: the +// proxy policies and the entries as resolved, with each derived entry's +// CredentialState filled in (and its Write cleared when it was downgraded), +// so the kick and the status payload describe what the proxy enforces. +type referenceRepoResolution struct { + policies []proxy.ReferenceRepoPolicy + repos []config.ReferenceRepo +} + +// buildReferenceRepoPolicies turns the config list into proxy policies. A +// read-only entry needs nothing; a hand-written write-mode entry is verified +// now so the failure is a boot-time config error naming the repo. A derived +// entry (hivecommons/hive#18158) is resolved instead: write tier when the App +// is installed on its org, read-only otherwise, never an error (a transient +// discovery failure keeps the entry read-only for this load and is logged). +func buildReferenceRepoPolicies(ctx context.Context, refs []config.ReferenceRepo, deps referenceRepoDeps, logger *slog.Logger) (referenceRepoResolution, error) { + res := referenceRepoResolution{ + policies: make([]proxy.ReferenceRepoPolicy, 0, len(refs)), + repos: make([]config.ReferenceRepo, 0, len(refs)), + } + for _, rr := range refs { + login := "" + if rr.Derived() { + resolved, derivedLogin, err := github.ResolveDerivedReferenceRepo(ctx, rr, deps.botLogin, deps.discover) + if err != nil { + // A blip, not a verdict: serve read-only this load but do not + // tell the request issue the App is missing when it may not be. + logger.Warn("derived reference repo: credential discovery failed, serving read-only this load", "repo", rr.Repo, "from", rr.DerivedFrom, "error", err.Error()) + resolved.Write = nil + resolved.CredentialState = config.ReferenceCredentialUnresolved + resolved.CredentialReason = err.Error() + } + rr, login = resolved, derivedLogin + } else if !rr.ReadOnly() { + l, err := github.VerifyReferenceRepoWriteCredential(ctx, rr, deps.botLogin, deps.discover, deps.whoAmI) + if err != nil { + return referenceRepoResolution{}, err + } + login = l + } + pol := proxy.ReferenceRepoPolicy{Repo: rr.Repo, Issues: rr.WritesIssues(), PRs: rr.WritesPRs()} + if !rr.ReadOnly() { + pol.Login = login + pol.Token = github.ReferenceRepoTokenSource(rr, deps.auth, logger) + } + res.policies = append(res.policies, pol) + res.repos = append(res.repos, rr) + } + return res, nil +} + +// derivedReadOnlyByRequest groups the downgraded derived entries by the +// request issue that granted them, in a stable order of issue refs. +func derivedReadOnlyByRequest(repos []config.ReferenceRepo) (refs []string, byRef map[string][]config.ReferenceRepo) { + byRef = map[string][]config.ReferenceRepo{} + for _, rr := range repos { + if !rr.DerivedReadOnly() { + continue + } + if _, ok := byRef[rr.DerivedFrom]; !ok { + refs = append(refs, rr.DerivedFrom) + } + byRef[rr.DerivedFrom] = append(byRef[rr.DerivedFrom], rr) + } + sort.Strings(refs) + return refs, byRef +} + +// notifyDerivedReadOnly posts the read-only notice for every request issue +// whose derived entries were downgraded. The poster is idempotent per +// (issue, repo set), so calling this on every reload posts at most once. +func notifyDerivedReadOnly(ctx context.Context, repos []config.ReferenceRepo, post derivedReadOnlyPoster, logger *slog.Logger) { + if post == nil { + return + } + refs, byRef := derivedReadOnlyByRequest(repos) + for _, ref := range refs { + readOnly := byRef[ref] + issueRepo, number, ok := readOnly[0].DerivedFromIssue() + if !ok { + logger.Warn("derived reference repo: malformed request issue ref, notice skipped", "from", ref) + continue + } + posted, err := post(ctx, issueRepo, number, readOnly) + if err != nil { + logger.Warn("derived reference repo: read-only notice not posted", "from", ref, "error", err.Error()) + continue + } + if posted { + logger.Info("derived reference repos served read-only, request issue notified", "from", ref, "count", len(readOnly)) + } + } +} + +// wireReferenceRepos installs project.reference_repos on the proxy, refusing +// to boot when a hand-written write-mode entry has no usable credential. A +// derived entry never refuses boot: it is downgraded to read-only and the +// request issue is told once (hivecommons/hive#18158). With a GitHub client +// the derived entries are listed from accepted request issues first +// (hivecommons/hive#18159) and kept current by every config reload and a +// periodic pass. +func (b *boot) wireReferenceRepos() { + if b.githubProxy == nil { + return + } + rt := b.newReferenceRepoRuntime() + if len(b.cfg.Project.ReferenceRepos) == 0 && rt.deriver == nil { + return + } + b.wireReferenceReposWith(rt) +} + +// newReferenceRepoRuntime builds the seams from the live GitHub client. The +// closures read b.ghClient on every call because the client is replaced on +// App key rotation. +func (b *boot) newReferenceRepoRuntime() *referenceRepoRuntime { + rt := &referenceRepoRuntime{ + deps: referenceRepoDeps{auth: func() *github.AppAuth { return nil }}, + interval: referenceRepoDeriveInterval(), + } + if b.ghClient == nil { + return rt + } + rt.post = func(ctx context.Context, issueRepo string, number int, readOnly []config.ReferenceRepo) (bool, error) { + return b.ghClient.CommentDerivedReadOnly(ctx, issueRepo, number, readOnly) + } + if auth := b.ghClient.AppAuth(); auth != nil { + apiURL := auth.APIURL() + rt.deps.botLogin = auth.BotLogin() + rt.deps.discover = auth.DiscoverInstallationID + rt.deps.whoAmI = func(ctx context.Context, token string) (string, error) { + return github.TokenUserLogin(ctx, token, apiURL) + } + // Re-fetched per call: the AppAuth is replaced on key rotation. + rt.deps.auth = func() *github.AppAuth { return b.ghClient.AppAuth() } + } else { + rt.deps.whoAmI = func(ctx context.Context, token string) (string, error) { + return github.TokenUserLogin(ctx, token, "") + } + } + rt.deriver = newReferenceRepoDeriver( + func(ctx context.Context, repo, label string, limit int) ([]github.RequestIssueCandidate, error) { + return b.ghClient.ListRequestIssueCandidates(ctx, repo, label, limit) + }, + func(ctx context.Context, repo string, number int, label string) (github.RequestIssueAccepter, error) { + return b.ghClient.RequestIssueAccepterFor(ctx, repo, number, label) + }, + workflowIntakeResolver(b.cfg, wfstore.Open(wfstore.Root(b.cfg.Data.WorkflowsDir))), + b.cfg.GitHub.HostLabel(), + ) + return rt +} + +// wireReferenceReposWith is wireReferenceRepos with the runtime injected +// (tests supply fake listers). It runs the boot pass, exits on a hand-written +// credential error and starts the periodic re-derive. +func (b *boot) wireReferenceReposWith(rt *referenceRepoRuntime) { + b.referenceRepos = rt + if err := b.applyReferenceRepos(b.ctx, rt); err != nil { + b.logger.Error("refusing to start: project.reference_repos write-mode entry has no usable credential (#18015)", "error", err.Error()) + if errors.Is(err, github.ErrReferenceRepoNoCredential) { + fmt.Fprintln(os.Stderr, err.Error()) + } + os.Exit(referenceRepoCredentialExitCode) + } + b.startReferenceRepoDerivePoller(b.ctx, rt) +} + +// applyEditedReferenceRepos is the dashboard editor's hook +// (hivecommons/hive#18136): it verifies the write credentials of the edited +// hand-written refs and, only when every entry passes, installs the rebuilt +// policies (derived entries from the last pass stay merged in). The error is +// returned to the owner verbatim and the previous list is kept. +func (b *boot) applyEditedReferenceRepos(ctx context.Context, refs []config.ReferenceRepo) error { + rt := b.referenceRepos + if rt == nil { + rt = b.newReferenceRepoRuntime() + } + if b.githubProxy == nil || b.cfg == nil { + return nil + } + rt.mu.Lock() + defer rt.mu.Unlock() + merged := mergedReferenceRepos(b.cfg, config.HandWrittenReferenceRepos(refs), rt.lastDerived, b.logger) + res, err := buildReferenceRepoPolicies(ctx, merged, rt.deps, b.logger) + if err != nil { + return err + } + b.githubProxy.SetReferenceRepos(res.policies) + return nil +} diff --git a/src/cmd/hive/reference_repos_derive.go b/src/cmd/hive/reference_repos_derive.go new file mode 100644 index 0000000000..f4914fa75c --- /dev/null +++ b/src/cmd/hive/reference_repos_derive.go @@ -0,0 +1,447 @@ +package main + +import ( + "context" + "log/slog" + "os" + "strings" + "sync" + "time" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/github" + "github.com/hivecommons/hive/pkg/refderive" + "github.com/hivecommons/hive/pkg/workflowmodel" + wfstore "github.com/hivecommons/hive/pkg/workflowmodel/store" +) + +// Derived reference repos (hivecommons/hive#18159, RFC #18139 step 3). +// +// An assessment hive's authorized repo holds request issues. Once a +// maintainer applies refderive.GrantLabel to one, the repos the request names +// become project.reference_repos entries at the issues+prs tier, in memory +// only: this file lists those issues at boot, on every config reload and on +// a timer, runs the pure refderive.Derive on each, merges the result under +// the operator's hand-written entries and installs the outcome on the proxy. +// Nothing here writes a config file; a closed issue or a removed label simply +// stops yielding entries and the next pass drops them. + +// referenceRepoDeriveIntervalEnv overrides how often the hive re-lists the +// request issues between config reloads. +const referenceRepoDeriveIntervalEnv = "HIVE_REFERENCE_REPOS_DERIVE_INTERVAL" + +// referenceRepoDeriveIntervalDefault is the re-derive cadence when the env +// var is unset or unusable: long enough to cost one list call per authorized +// repo per ten minutes, short enough that an ended grant is gone before an +// agent's next session. +const referenceRepoDeriveIntervalDefault = 10 * time.Minute + +// maxRequestIssuesPerRepoPerPass bounds how many labelled issues one pass +// reads per authorized repo: an assessment hive has a handful of open +// requests, and a repo with hundreds of triage/accepted issues is not one +// whose model should be deriving anything. +const maxRequestIssuesPerRepoPerPass = 50 + +// referenceRepoDeriveInterval reads the re-derive cadence from the +// environment, falling back to the default for an unset, unparseable or +// non-positive value. +func referenceRepoDeriveInterval() time.Duration { + raw := strings.TrimSpace(os.Getenv(referenceRepoDeriveIntervalEnv)) + if raw == "" { + return referenceRepoDeriveIntervalDefault + } + d, err := time.ParseDuration(raw) + if err != nil || d <= 0 { + return referenceRepoDeriveIntervalDefault + } + return d +} + +// requestIssueLister lists the open issues in an authorized repo carrying +// label. Production is github.Client.ListRequestIssueCandidates. +type requestIssueLister func(ctx context.Context, repo, label string, limit int) ([]github.RequestIssueCandidate, error) + +// requestIssueAccepterFunc reads who applied label to repo#number and their +// permission. Production is github.Client.RequestIssueAccepterFor. +type requestIssueAccepterFunc func(ctx context.Context, repo string, number int, label string) (github.RequestIssueAccepter, error) + +// intakeResolver returns the intake declaration of the workflow model bound +// to an authorized repo ("owner/name"), and false when the repo is unbound, +// the model is unusable or it declares no intake.referenceRepoFields. +type intakeResolver func(repo string) (workflowmodel.Intake, bool) + +// workflowIntakeResolver resolves bindings the same way the stage engine does +// (stageEngineWorkflowFor): cfg is read on every call so a reload or a +// dashboard binding edit takes effect on the next pass, and the model is read +// from the store rather than compiled because the intake declaration lives on +// the model's spec, not on the compiled profile. +func workflowIntakeResolver(cfg *config.Config, st *wfstore.Store) intakeResolver { + return func(repo string) (workflowmodel.Intake, bool) { + if cfg == nil || st == nil || !cfg.Workflow.Enabled { + return workflowmodel.Intake{}, false + } + b, ok := cfg.WorkflowBindingFor(repo) + if !ok { + return workflowmodel.Intake{}, false + } + m, _, err := st.Get(b.Name, b.Version) + if err != nil || m == nil { + return workflowmodel.Intake{}, false + } + return m.Spec.Intake, m.Spec.Intake.DerivesReferenceRepos() + } +} + +// accepterCacheEntry is the accepter read for one request issue, valid while +// the issue's updated_at has not moved. +type accepterCacheEntry struct { + updatedAt time.Time + accepter github.RequestIssueAccepter +} + +// referenceRepoDeriver runs derivation passes. It remembers the accepter per +// issue so an unchanged issue costs no timeline or permission read, and the +// last good entries per authorized repo so a listing error keeps the previous +// grant instead of dropping it. +type referenceRepoDeriver struct { + list requestIssueLister + accepter requestIssueAccepterFunc + intakeFor intakeResolver + // host is the forge host repository URLs must be on (refderive.Options). + host string + + mu sync.Mutex + // cache is keyed by the issue ref ("owner/repo#N"). + cache map[string]accepterCacheEntry + // lastByRepo holds the entries the previous pass derived from each + // authorized repo's issues, unresolved (as refderive produced them). + lastByRepo map[string][]config.ReferenceRepo +} + +func newReferenceRepoDeriver(list requestIssueLister, accepter requestIssueAccepterFunc, intakeFor intakeResolver, host string) *referenceRepoDeriver { + return &referenceRepoDeriver{ + list: list, + accepter: accepter, + intakeFor: intakeFor, + host: host, + cache: map[string]accepterCacheEntry{}, + lastByRepo: map[string][]config.ReferenceRepo{}, + } +} + +// authorizedRepoSlugs is project.repos as qualified "owner/name" slugs. +func authorizedRepoSlugs(cfg *config.Config) []string { + if cfg == nil { + return nil + } + out := make([]string, 0, len(cfg.Project.Repos)) + for _, repo := range cfg.Project.Repos { + out = append(out, config.QualifyRepo(cfg.Project.Org, repo)) + } + return out +} + +// bodyNamesIntakeField is the cheap pre-filter: an issue whose body carries +// none of the model's reference-repo headings was not filed from the request +// form, so its timeline and the accepter's permission are never fetched. +func bodyNamesIntakeField(body string, intake workflowmodel.Intake) bool { + lower := strings.ToLower(body) + for _, f := range intake.ReferenceRepoFields { + f = strings.ToLower(strings.TrimSpace(f)) + if f != "" && strings.Contains(lower, f) { + return true + } + } + return false +} + +// entriesFrom returns the entries in list derived from the issue ref. +func entriesFrom(list []config.ReferenceRepo, ref string) []config.ReferenceRepo { + var out []config.ReferenceRepo + for _, rr := range list { + if rr.DerivedFrom == ref { + out = append(out, rr) + } + } + return out +} + +// derive runs one pass over every authorized repo whose bound model derives +// reference repos and returns the derived entries, unresolved, in authorized +// repo order then issue order. A nil deriver derives nothing. Errors never +// propagate: a repo whose listing failed keeps its previous entries, an +// issue whose accepter could not be read keeps its previous entries, and a +// refused grant is logged at debug and yields nothing. +func (d *referenceRepoDeriver) derive(ctx context.Context, cfg *config.Config, logger *slog.Logger) []config.ReferenceRepo { + if d == nil || cfg == nil || d.list == nil || d.accepter == nil || d.intakeFor == nil { + return nil + } + if logger == nil { + logger = slog.Default() + } + d.mu.Lock() + defer d.mu.Unlock() + authorized := authorizedRepoSlugs(cfg) + opts := refderive.Options{Host: d.host, Authorized: authorized} + seenIssues := map[string]bool{} + nextByRepo := make(map[string][]config.ReferenceRepo, len(d.lastByRepo)) + var out []config.ReferenceRepo + for _, full := range authorized { + intake, ok := d.intakeFor(full) + if !ok { + continue + } + previous := d.lastByRepo[full] + cands, err := d.list(ctx, full, refderive.GrantLabel, maxRequestIssuesPerRepoPerPass) + if err != nil { + logger.Warn("derived reference repos: request issues not listed, keeping previous entries", "repo", full, "kept", len(previous), "error", err.Error()) + for _, rr := range previous { + seenIssues[rr.DerivedFrom] = true + } + nextByRepo[full] = previous + out = append(out, previous...) + continue + } + var entries []config.ReferenceRepo + for _, cand := range cands { + if cand.Closed || !bodyNamesIntakeField(cand.Body, intake) { + continue + } + issue := refderive.RequestIssue{ + Repo: cand.Repo, + Number: cand.Number, + Title: cand.Title, + Body: cand.Body, + Closed: cand.Closed, + Labels: cand.Labels, + } + ref := issue.Ref() + seenIssues[ref] = true + cached, hit := d.cache[ref] + if !hit || !cached.updatedAt.Equal(cand.UpdatedAt) { + acc, err := d.accepter(ctx, cand.Repo, cand.Number, refderive.GrantLabel) + if err != nil { + kept := entriesFrom(previous, ref) + logger.Warn("derived reference repos: accepter not read, keeping previous entries for the issue", "issue", ref, "kept", len(kept), "error", err.Error()) + entries = append(entries, kept...) + continue + } + cached = accepterCacheEntry{updatedAt: cand.UpdatedAt, accepter: acc} + d.cache[ref] = cached + } + issue.AcceptedBy = cached.accepter.Login + issue.AcceptedByIsBot = cached.accepter.IsBot + issue.AcceptedByPermission = cached.accepter.Permission + res, err := refderive.Derive(issue, intake, opts) + if err != nil { + logger.Debug("derived reference repos: request issue grants nothing", "issue", ref, "reason", err.Error()) + continue + } + for _, sk := range res.Skipped { + logger.Debug("derived reference repos: repository skipped", "issue", ref, "repo", sk.Repo, "reason", sk.Reason) + } + entries = append(entries, res.Repos...) + } + nextByRepo[full] = entries + out = append(out, entries...) + } + for ref := range d.cache { + if !seenIssues[ref] { + delete(d.cache, ref) + } + } + d.lastByRepo = nextByRepo + return out +} + +// clampDerivedWrite returns write restricted to the surfaces a derived entry +// may ever carry (refderive.DerivedWrite): the parser only emits that tier, +// the merge asserts it. +func clampDerivedWrite(write []string) []string { + var out []string + for _, w := range write { + for _, allowed := range refderive.DerivedWrite { + if strings.EqualFold(strings.TrimSpace(w), allowed) { + out = append(out, allowed) + break + } + } + } + return out +} + +// mergeReferenceRepos is the pure merge of derived entries under the +// operator's hand-written list. Hand-written entries come first and +// unchanged; a derived entry is dropped when a hand-written entry names the +// same repo (the operator's tier wins), when the repo is an authorized repo +// (never both), when it carries no provenance, or when an earlier derived +// entry already named the repo (first wins; the other is logged). Every kept +// derived entry's Write is clamped to issues+prs. Other derived fields +// (CredentialState, CredentialReason) pass through so a resolved entry can be +// re-merged after a reload without a new credential lookup. +func mergeReferenceRepos(handWritten, derived []config.ReferenceRepo, authorized []string, logger *slog.Logger) []config.ReferenceRepo { + if logger == nil { + logger = slog.Default() + } + key := func(repo string) string { return strings.ToLower(strings.TrimSpace(repo)) } + taken := make(map[string]string, len(handWritten)+len(derived)) + out := make([]config.ReferenceRepo, 0, len(handWritten)+len(derived)) + for _, rr := range handWritten { + taken[key(rr.Repo)] = "project.reference_repos" + out = append(out, rr) + } + authorizedKeys := make(map[string]bool, len(authorized)) + for _, a := range authorized { + authorizedKeys[key(a)] = true + } + for _, rr := range derived { + k := key(rr.Repo) + switch { + case k == "" || !rr.Derived(): + logger.Warn("derived reference repos: entry without provenance dropped", "repo", rr.Repo) + continue + case authorizedKeys[k]: + logger.Info("derived reference repos: authorized repo is never a reference repo", "repo", rr.Repo, "from", rr.DerivedFrom) + continue + } + if by, dup := taken[k]; dup { + logger.Info("derived reference repos: repo already listed, first wins", "repo", rr.Repo, "from", rr.DerivedFrom, "kept", by) + continue + } + taken[k] = rr.DerivedFrom + rr.Write = clampDerivedWrite(rr.Write) + rr.WriteTokenFile = "" + out = append(out, rr) + } + return out +} + +// referenceRepoRuntime is the state the lifecycle keeps between passes: the +// seams, the deriver and the derived entries as last resolved, so a config +// reload can put them back in memory before the next pass re-lists. +type referenceRepoRuntime struct { + deriver *referenceRepoDeriver + deps referenceRepoDeps + post derivedReadOnlyPoster + // interval is the periodic re-derive cadence; zero disables the poller. + interval time.Duration + + mu sync.Mutex + // lastDerived are the derived entries as buildReferenceRepoPolicies + // resolved them on the last successful pass. + lastDerived []config.ReferenceRepo +} + +// derivedEntries returns the derived entries of a resolved list. +func derivedEntries(refs []config.ReferenceRepo) []config.ReferenceRepo { + var out []config.ReferenceRepo + for _, rr := range refs { + if rr.Derived() { + out = append(out, rr) + } + } + return out +} + +// mergedReferenceRepos validates the merge against the live project; a merge +// that would not load from a file falls back to the hand-written list. +func mergedReferenceRepos(cfg *config.Config, handWritten, derived []config.ReferenceRepo, logger *slog.Logger) []config.ReferenceRepo { + merged := mergeReferenceRepos(handWritten, derived, authorizedRepoSlugs(cfg), logger) + check := &config.Config{Project: config.ProjectConfig{Org: cfg.Project.Org, Repos: cfg.Project.Repos, ReferenceRepos: merged}} + if err := check.ValidateReferenceRepos(); err != nil { + logger.Warn("derived reference repos: merged list invalid, serving hand-written entries only", "error", err.Error()) + return handWritten + } + return merged +} + +// applyReferenceRepos runs one full pass: derive, merge, resolve, install. +// On success the resolved list replaces cfg.Project.ReferenceRepos in memory, +// the proxy gets the rebuilt policies (so an entry that vanished loses its +// policy at once) and the read-only notices go out. A credential error on a +// hand-written write-mode entry is returned for the caller to judge: boot +// exits, a later pass keeps the previous installation. +func (b *boot) applyReferenceRepos(ctx context.Context, rt *referenceRepoRuntime) error { + if rt == nil || b.cfg == nil || b.githubProxy == nil { + return nil + } + rt.mu.Lock() + defer rt.mu.Unlock() + handWritten := config.HandWrittenReferenceRepos(b.cfg.Project.ReferenceRepos) + derived := rt.deriver.derive(ctx, b.cfg, b.logger) + merged := mergedReferenceRepos(b.cfg, handWritten, derived, b.logger) + res, err := buildReferenceRepoPolicies(ctx, merged, rt.deps, b.logger) + if err != nil { + return err + } + // The resolved entries replace the list in memory only: a derived entry + // is never written back to any config file (yaml:"-" on its provenance + // and the save test in reference_repos_derive_test.go guard that), and + // its credential state is what the kick and /api/status read. + b.cfg.Project.ReferenceRepos = res.repos + b.githubProxy.SetReferenceRepos(res.policies) + rt.lastDerived = derivedEntries(res.repos) + b.logger.Info("reference repos wired", "count", len(res.policies), "derived", len(rt.lastDerived), "repos", b.cfg.ReferenceRepoSlugs()) + // Off the caller's path: the notice is a GitHub write and must not delay + // the proxy coming up or a reload completing. + go notifyDerivedReadOnly(ctx, res.repos, rt.post, b.logger) + return nil +} + +// rederiveReferenceRepos is the pass a reload or the poller runs: never +// fatal, the previous installation stays when it fails. +func (b *boot) rederiveReferenceRepos(ctx context.Context) { + rt := b.referenceRepos + if rt == nil { + return + } + if err := b.applyReferenceRepos(ctx, rt); err != nil { + b.logger.Warn("reference repos: re-derive failed, previous policies kept", "error", err.Error()) + } +} + +// reconcileReferenceReposAfterReload runs inside the config reload hook, +// after the new config was swapped in: the reload rebuilt +// cfg.Project.ReferenceRepos from the file, so the derived entries the last +// pass resolved are merged back at once (no network, so the kick never loses +// them while the pass runs), then a full pass re-lists the request issues in +// the background so a grant that ended drops without an operator action. +func (b *boot) reconcileReferenceReposAfterReload() { + if b.referenceRepos == nil || b.cfg == nil { + return + } + b.remergeLastDerivedReferenceRepos() + go b.rederiveReferenceRepos(b.ctx) +} + +// remergeLastDerivedReferenceRepos puts the derived entries of the last +// successful pass back under the hand-written list now in cfg. +func (b *boot) remergeLastDerivedReferenceRepos() { + rt := b.referenceRepos + if rt == nil || b.cfg == nil { + return + } + rt.mu.Lock() + defer rt.mu.Unlock() + handWritten := config.HandWrittenReferenceRepos(b.cfg.Project.ReferenceRepos) + b.cfg.Project.ReferenceRepos = mergedReferenceRepos(b.cfg, handWritten, rt.lastDerived, b.logger) +} + +// startReferenceRepoDerivePoller re-derives on rt.interval until ctx ends. +func (b *boot) startReferenceRepoDerivePoller(ctx context.Context, rt *referenceRepoRuntime) { + if rt == nil || rt.deriver == nil || rt.interval <= 0 || ctx == nil { + return + } + go func() { + ticker := time.NewTicker(rt.interval) + defer ticker.Stop() + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + b.rederiveReferenceRepos(ctx) + } + } + }() +} diff --git a/src/cmd/hive/reference_repos_derive_test.go b/src/cmd/hive/reference_repos_derive_test.go new file mode 100644 index 0000000000..06491b8221 --- /dev/null +++ b/src/cmd/hive/reference_repos_derive_test.go @@ -0,0 +1,468 @@ +package main + +import ( + "context" + "errors" + "os" + "path/filepath" + "strings" + "sync" + "testing" + "time" + + "github.com/hivecommons/hive/crux/proxy" + "github.com/hivecommons/hive/internal/testutil" + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/github" + "github.com/hivecommons/hive/pkg/refderive" + "github.com/hivecommons/hive/pkg/workflowmodel" +) + +// Fixture: the CNCF TechDocs shape. cncf/techdocs is the authorized repo, +// bound to a model whose intake names the form headings; request issue #395 +// names acme/docs (App installed on acme: write tier) and zeta/site (no +// installation: read-only). +const ( + testIntakeRepoField = "Website or documentation repository" + testIntakeOutOfScope = "Out of scope" + testRequestIssueRepo = "cncf/techdocs" + testRequestIssueRef = "cncf/techdocs#395" + testDerivedWriteRepo = "acme/docs" + testDerivedReadOnly = "zeta/site" + testHandWrittenRepo = "hivecommons/docs" + testRequestIssueBody = "### Project\nAcme\n\n### " + testIntakeRepoField + "\nhttps://github.com/acme/docs\nhttps://github.com/zeta/site\nhttps://github.com/cncf/toc\n\n### " + testIntakeOutOfScope + "\n_No response_\n" + testRequestIssueTitle = "Assessment request: Acme" +) + +// fakeRequestIssues is the mutable issue store behind the lister and +// accepter seams; tests close issues, drop labels or move updated_at and +// run another pass. +type fakeRequestIssues struct { + mu sync.Mutex + issues map[int]github.RequestIssueCandidate + accepter github.RequestIssueAccepter + listErr error + accepterCalls int +} + +func newFakeRequestIssues() *fakeRequestIssues { + return &fakeRequestIssues{ + issues: map[int]github.RequestIssueCandidate{ + 395: { + Repo: testRequestIssueRepo, + Number: 395, + Title: testRequestIssueTitle, + Body: testRequestIssueBody, + Labels: []string{refderive.GrantLabel, "kind/assessment"}, + UpdatedAt: time.Date(2026, 11, 1, 10, 0, 0, 0, time.UTC), + }, + }, + accepter: github.RequestIssueAccepter{Login: "maintainer", Permission: "maintain"}, + } +} + +func (f *fakeRequestIssues) list(_ context.Context, repo, label string, limit int) ([]github.RequestIssueCandidate, error) { + f.mu.Lock() + defer f.mu.Unlock() + if f.listErr != nil { + return nil, f.listErr + } + var out []github.RequestIssueCandidate + for _, is := range f.issues { + if is.Repo != repo || is.Closed || len(out) >= limit { + continue + } + for _, l := range is.Labels { + if l == label { + out = append(out, is) + break + } + } + } + return out, nil +} + +func (f *fakeRequestIssues) accept(_ context.Context, _ string, _ int, _ string) (github.RequestIssueAccepter, error) { + f.mu.Lock() + defer f.mu.Unlock() + f.accepterCalls++ + return f.accepter, nil +} + +func (f *fakeRequestIssues) update(number int, fn func(*github.RequestIssueCandidate)) { + f.mu.Lock() + defer f.mu.Unlock() + is := f.issues[number] + fn(&is) + f.issues[number] = is +} + +func (f *fakeRequestIssues) accepterCallCount() int { + f.mu.Lock() + defer f.mu.Unlock() + return f.accepterCalls +} + +// testIntakeFor binds cncf/techdocs to a model with intake fields and +// nothing else. +func testIntakeFor(repo string) (workflowmodel.Intake, bool) { + if repo != testRequestIssueRepo { + return workflowmodel.Intake{}, false + } + return workflowmodel.Intake{ReferenceRepoFields: []string{testIntakeRepoField}, OutOfScopeFields: []string{testIntakeOutOfScope}}, true +} + +func derivedTestConfig(t *testing.T, handWritten ...config.ReferenceRepo) *config.Config { + t.Helper() + return &config.Config{ + SourcePath: filepath.Join(t.TempDir(), "hive.yaml"), + Project: config.ProjectConfig{Org: "cncf", Name: "techdocs", Repos: []string{"techdocs", "toc"}, ReferenceRepos: handWritten}, + AgentRuntimeConfig: config.AgentRuntimeConfig{ + Agents: map[string]config.AgentConfig{"scanner": {Role: "scanner"}}, + }, + } +} + +// derivedTestBoot wires a boot with a real (ephemeral) proxy and the fake +// issue store. deps install the App on acme and hivecommons only. +func derivedTestBoot(t *testing.T, fake *fakeRequestIssues, post derivedReadOnlyPoster, handWritten ...config.ReferenceRepo) (*boot, *referenceRepoRuntime) { + t.Helper() + cfg := derivedTestConfig(t, handWritten...) + px, err := proxy.NewGitHubProxyEphemeral(testLogger(), cfg.Project.Org, cfg.Project.Repos) + if err != nil { + t.Fatal(err) + } + deps := testReferenceRepoDeps() + deps.discover = func(_ context.Context, org string) (int64, error) { + switch org { + case "acme", "hivecommons": + return 1, nil + } + return 0, github.ErrNoInstallationForOrg + } + b := &boot{cfg: cfg, logger: testLogger(), ctx: context.Background(), githubProxy: px} + rt := &referenceRepoRuntime{ + deriver: newReferenceRepoDeriver(fake.list, fake.accept, testIntakeFor, refderive.DefaultHost), + deps: deps, + post: post, + } + return b, rt +} + +func referenceRepoByName(refs []config.ReferenceRepo, repo string) (config.ReferenceRepo, bool) { + for _, rr := range refs { + if strings.EqualFold(rr.Repo, repo) { + return rr, true + } + } + return config.ReferenceRepo{}, false +} + +// Boot derives from the accepted issue: the write-tier entry reaches the +// config and the proxy at issues+prs with its provenance, the uninstalled +// org's entry is read-only and the request issue is told once, and the +// authorized repo the form also named is never derived. +func TestWireReferenceReposDerivesAtBoot(t *testing.T) { + fake := newFakeRequestIssues() + rec := ¬ifyRecorder{posted: true} + b, rt := derivedTestBoot(t, fake, rec.post, config.ReferenceRepo{Repo: testHandWrittenRepo}) + b.wireReferenceReposWith(rt) + + refs := b.cfg.Project.ReferenceRepos + if len(refs) != 3 { + t.Fatalf("want hand-written + 2 derived, got %+v", refs) + } + if refs[0].Repo != testHandWrittenRepo || refs[0].Derived() { + t.Errorf("hand-written entry must come first and unchanged: %+v", refs[0]) + } + docs, ok := referenceRepoByName(refs, testDerivedWriteRepo) + if !ok || docs.DerivedFrom != testRequestIssueRef || !docs.WritesIssues() || !docs.WritesPRs() || docs.CredentialState != config.ReferenceCredentialWriteTier { + t.Errorf("derived write-tier entry = %+v", docs) + } + site, ok := referenceRepoByName(refs, testDerivedReadOnly) + if !ok || !site.DerivedReadOnly() || !site.ReadOnly() { + t.Errorf("derived entry for an uninstalled org must be read-only: %+v", site) + } + if _, derived := referenceRepoByName(refs, "cncf/toc"); derived { + t.Error("an authorized repo named by the form must never become a reference repo") + } + pol, ok := b.githubProxy.ReferenceRepoPolicyFor(testDerivedWriteRepo) + if !ok || !pol.Issues || !pol.PRs { + t.Errorf("proxy policy for the derived repo = %+v ok=%v", pol, ok) + } + if pol, ok := b.githubProxy.ReferenceRepoPolicyFor(testDerivedReadOnly); !ok || pol.Issues || pol.PRs { + t.Errorf("proxy policy for the read-only derived repo = %+v ok=%v", pol, ok) + } + testutil.Eventually(t, referenceRepoTestTimeout, func() bool { return rec.count() == 1 }, "read-only notice must be posted once") + rec.mu.Lock() + calls := rec.calls + rec.mu.Unlock() + if len(calls) != 1 || calls[0].repo != testRequestIssueRepo || calls[0].number != 395 || len(calls[0].repos) != 1 || calls[0].repos[0] != testDerivedReadOnly { + t.Errorf("notice calls = %+v", calls) + } +} + +// The grant ends with the label: the next pass drops the entry from the +// config and the proxy, which then refuses writes to the repo again. +func TestRederiveDropsEntryWhenLabelRemoved(t *testing.T) { + fake := newFakeRequestIssues() + b, rt := derivedTestBoot(t, fake, nil) + b.wireReferenceReposWith(rt) + if _, ok := b.githubProxy.ReferenceRepoPolicyFor(testDerivedWriteRepo); !ok { + t.Fatal("precondition: derived policy installed") + } + + fake.update(395, func(is *github.RequestIssueCandidate) { + is.Labels = []string{"kind/assessment"} + is.UpdatedAt = is.UpdatedAt.Add(time.Minute) + }) + b.rederiveReferenceRepos(context.Background()) + + if len(b.cfg.Project.ReferenceRepos) != 0 { + t.Fatalf("label removed: entries must drop, got %+v", b.cfg.Project.ReferenceRepos) + } + if _, ok := b.githubProxy.ReferenceRepoPolicyFor(testDerivedWriteRepo); ok { + t.Fatal("label removed: proxy must no longer hold a policy for the repo") + } +} + +func TestRederiveDropsEntryWhenIssueClosed(t *testing.T) { + fake := newFakeRequestIssues() + b, rt := derivedTestBoot(t, fake, nil) + b.wireReferenceReposWith(rt) + + fake.update(395, func(is *github.RequestIssueCandidate) { is.Closed = true }) + b.rederiveReferenceRepos(context.Background()) + + if len(b.cfg.Project.ReferenceRepos) != 0 { + t.Fatalf("closed issue: entries must drop, got %+v", b.cfg.Project.ReferenceRepos) + } + if _, ok := b.githubProxy.ReferenceRepoPolicyFor(testDerivedWriteRepo); ok { + t.Fatal("closed issue: proxy must no longer hold a policy for the repo") + } +} + +// A hand-written entry for the same repo wins: the operator's read-only +// acme/docs stays read-only, the derived issues+prs grant is dropped. +func TestWireReferenceReposHandWrittenWins(t *testing.T) { + fake := newFakeRequestIssues() + b, rt := derivedTestBoot(t, fake, nil, config.ReferenceRepo{Repo: testDerivedWriteRepo}) + b.wireReferenceReposWith(rt) + + var matches int + for _, rr := range b.cfg.Project.ReferenceRepos { + if strings.EqualFold(rr.Repo, testDerivedWriteRepo) { + matches++ + if rr.Derived() || !rr.ReadOnly() { + t.Errorf("hand-written entry must win unchanged, got %+v", rr) + } + } + } + if matches != 1 { + t.Fatalf("repo listed %d times, want once", matches) + } + if pol, ok := b.githubProxy.ReferenceRepoPolicyFor(testDerivedWriteRepo); !ok || pol.Issues || pol.PRs { + t.Errorf("proxy must enforce the hand-written read-only tier, got %+v ok=%v", pol, ok) + } +} + +// Refused grants: a bot applier and a triage-only applier derive nothing. +func TestDeriveRefusesBotAndTriageAccepters(t *testing.T) { + for name, acc := range map[string]github.RequestIssueAccepter{ + "bot": {Login: "hive-app[bot]", IsBot: true, Permission: "write"}, + "triage": {Login: "triager", Permission: "triage"}, + "none": {}, + } { + fake := newFakeRequestIssues() + fake.accepter = acc + d := newReferenceRepoDeriver(fake.list, fake.accept, testIntakeFor, refderive.DefaultHost) + if got := d.derive(context.Background(), derivedTestConfig(t), testLogger()); len(got) != 0 { + t.Errorf("%s accepter must grant nothing, got %+v", name, got) + } + } +} + +// The accepter is cached by (issue, updated_at): an unchanged issue costs no +// timeline read on the next pass, a moved one is re-read, and a listing +// error keeps the previous entries. +func TestDeriveCachesAccepterAndKeepsPreviousOnListError(t *testing.T) { + fake := newFakeRequestIssues() + cfg := derivedTestConfig(t) + d := newReferenceRepoDeriver(fake.list, fake.accept, testIntakeFor, refderive.DefaultHost) + ctx := context.Background() + + first := d.derive(ctx, cfg, testLogger()) + if len(first) != 2 || fake.accepterCallCount() != 1 { + t.Fatalf("first pass: %d entries, %d accepter calls", len(first), fake.accepterCallCount()) + } + d.derive(ctx, cfg, testLogger()) + if fake.accepterCallCount() != 1 { + t.Fatalf("unchanged issue must not re-read the accepter, calls = %d", fake.accepterCallCount()) + } + fake.update(395, func(is *github.RequestIssueCandidate) { is.UpdatedAt = is.UpdatedAt.Add(time.Hour) }) + d.derive(ctx, cfg, testLogger()) + if fake.accepterCallCount() != 2 { + t.Fatalf("moved updated_at must re-read the accepter, calls = %d", fake.accepterCallCount()) + } + + fake.mu.Lock() + fake.listErr = errors.New("502 from the forge") + fake.mu.Unlock() + kept := d.derive(ctx, cfg, testLogger()) + if len(kept) != 2 { + t.Fatalf("a listing error must keep the previous entries, got %+v", kept) + } +} + +func TestMergeReferenceRepos(t *testing.T) { + hand := []config.ReferenceRepo{{Repo: "Acme/Docs"}} + derived := []config.ReferenceRepo{ + {Repo: "acme/docs", Write: []string{"issues", "prs"}, DerivedFrom: "o/r#1"}, + {Repo: "zeta/site", Write: []string{"issues", "prs", "admin"}, WriteTokenFile: "/x", DerivedFrom: "o/r#1", CredentialState: config.ReferenceCredentialWriteTier}, + {Repo: "Zeta/Site", Write: []string{"issues"}, DerivedFrom: "o/r#2"}, + {Repo: "cncf/toc", Write: []string{"issues"}, DerivedFrom: "o/r#2"}, + {Repo: "no/provenance", Write: []string{"issues"}}, + } + got := mergeReferenceRepos(hand, derived, []string{"cncf/techdocs", "cncf/toc"}, testLogger()) + if len(got) != 2 { + t.Fatalf("got %+v", got) + } + if got[0].Repo != "Acme/Docs" || got[0].Derived() || !got[0].ReadOnly() { + t.Errorf("hand-written must win unchanged: %+v", got[0]) + } + z := got[1] + if z.Repo != "zeta/site" || z.DerivedFrom != "o/r#1" || z.WriteTokenFile != "" || z.CredentialState != config.ReferenceCredentialWriteTier { + t.Errorf("first derived entry must win with its state: %+v", z) + } + if len(z.Write) != 2 || !z.WritesIssues() || !z.WritesPRs() { + t.Errorf("derived write must be clamped to issues+prs: %v", z.Write) + } + if out := mergeReferenceRepos(nil, nil, nil, nil); len(out) != 0 { + t.Errorf("empty merge = %+v", out) + } +} + +// A derived entry never lands in a saved config: the overlay and runtime +// file writers both go through Config.Save, whose YAML must name the +// hand-written repo and none of the derived ones (hivecommons/hive#18137). +func TestSavedConfigHasNoDerivedReferenceRepos(t *testing.T) { + fake := newFakeRequestIssues() + b, rt := derivedTestBoot(t, fake, nil, config.ReferenceRepo{Repo: testHandWrittenRepo}) + b.wireReferenceReposWith(rt) + if len(b.cfg.Project.ReferenceRepos) != 3 { + t.Fatalf("precondition: derived entries in memory, got %+v", b.cfg.Project.ReferenceRepos) + } + if err := b.cfg.Save(); err != nil { + t.Fatal(err) + } + data, err := os.ReadFile(b.cfg.SourcePath) + if err != nil { + t.Fatal(err) + } + saved := string(data) + if !strings.Contains(saved, testHandWrittenRepo) { + t.Errorf("saved config must keep the hand-written entry:\n%s", saved) + } + for _, leak := range []string{testDerivedWriteRepo, testDerivedReadOnly, testRequestIssueRef, "derived_from", "credential_state"} { + if strings.Contains(saved, leak) { + t.Errorf("saved config leaks derived state %q:\n%s", leak, saved) + } + } + // The in-memory list is untouched by the save. + if len(b.cfg.Project.ReferenceRepos) != 3 { + t.Errorf("save must not strip the in-memory derived entries: %+v", b.cfg.Project.ReferenceRepos) + } +} + +// A config reload replaces cfg.Project.ReferenceRepos with the file's +// hand-written list; the hook merges the last derived entries back at once +// so the kick never loses them while the background pass runs. +func TestReconcileReferenceReposAfterReloadKeepsDerived(t *testing.T) { + fake := newFakeRequestIssues() + b, rt := derivedTestBoot(t, fake, nil, config.ReferenceRepo{Repo: testHandWrittenRepo}) + b.wireReferenceReposWith(rt) + + // Simulate the reload swap: the file carries only the hand-written entry. + b.cfg.Project.ReferenceRepos = []config.ReferenceRepo{{Repo: testHandWrittenRepo}} + b.remergeLastDerivedReferenceRepos() + refs := b.cfg.Project.ReferenceRepos + if len(refs) != 3 || refs[0].Repo != testHandWrittenRepo { + t.Fatalf("derived entries must be merged back under the hand-written one, got %+v", refs) + } + docs, ok := referenceRepoByName(refs, testDerivedWriteRepo) + if !ok || docs.CredentialState != config.ReferenceCredentialWriteTier || !docs.WritesPRs() { + t.Fatalf("re-merged entry must keep its resolved state, got %+v", docs) + } + + // The full hook also re-lists in the background; with the label gone the + // pass drops the entry from the proxy. + fake.update(395, func(is *github.RequestIssueCandidate) { is.Labels = nil }) + b.reconcileReferenceReposAfterReload() + testutil.Eventually(t, referenceRepoTestTimeout, func() bool { + _, ok := b.githubProxy.ReferenceRepoPolicyFor(testDerivedWriteRepo) + return !ok + }, "background pass after reload must drop the ended grant from the proxy") +} + +func TestReferenceRepoDeriveInterval(t *testing.T) { + t.Setenv(referenceRepoDeriveIntervalEnv, "") + if got := referenceRepoDeriveInterval(); got != referenceRepoDeriveIntervalDefault { + t.Errorf("unset = %v, want %v", got, referenceRepoDeriveIntervalDefault) + } + t.Setenv(referenceRepoDeriveIntervalEnv, "3m") + if got := referenceRepoDeriveInterval(); got != 3*time.Minute { + t.Errorf("3m = %v", got) + } + for _, bad := range []string{"soon", "-1m", "0"} { + t.Setenv(referenceRepoDeriveIntervalEnv, bad) + if got := referenceRepoDeriveInterval(); got != referenceRepoDeriveIntervalDefault { + t.Errorf("%q = %v, want default", bad, got) + } + } +} + +// The dashboard editor's hook (hivecommons/hive#18136) installs the edited +// hand-written list with the last derived set still merged in: a derived +// entry the editor echoes back is not re-verified as hand-written, a new +// hand-written entry gets its policy, and a removed one loses it. The +// remerge the handler runs after the save then restores the derived entries +// in memory under the new list. +func TestApplyEditedReferenceReposKeepsDerivedAndRemerges(t *testing.T) { + fake := newFakeRequestIssues() + b, rt := derivedTestBoot(t, fake, nil, config.ReferenceRepo{Repo: testHandWrittenRepo}) + b.wireReferenceReposWith(rt) + derivedBefore := len(rt.lastDerived) + if derivedBefore == 0 { + t.Fatal("precondition: boot derived entries") + } + + // Echo of the GET body (hand-written + derived) with the hand-written + // entry swapped for a new read-only one. + edited := append([]config.ReferenceRepo{{Repo: "other/ref"}}, rt.lastDerived...) + if err := b.applyEditedReferenceRepos(context.Background(), edited); err != nil { + t.Fatalf("applyEditedReferenceRepos: %v", err) + } + if _, ok := b.githubProxy.ReferenceRepoPolicyFor("other/ref"); !ok { + t.Error("new hand-written entry must get a proxy policy") + } + if _, ok := b.githubProxy.ReferenceRepoPolicyFor(testHandWrittenRepo); ok { + t.Error("removed hand-written entry must lose its proxy policy") + } + if pol, ok := b.githubProxy.ReferenceRepoPolicyFor(testDerivedWriteRepo); !ok || !pol.Issues || !pol.PRs { + t.Errorf("derived write-tier entry must stay enforced, got %+v ok=%v", pol, ok) + } + if len(rt.lastDerived) != derivedBefore { + t.Errorf("an edit must not change the derived set: %d -> %d", derivedBefore, len(rt.lastDerived)) + } + + // What the handler does after the file is written. + b.cfg.Project.ReferenceRepos = []config.ReferenceRepo{{Repo: "other/ref"}} + b.remergeLastDerivedReferenceRepos() + refs := b.cfg.Project.ReferenceRepos + if len(refs) != 1+derivedBefore || refs[0].Repo != "other/ref" || refs[0].Derived() { + t.Fatalf("derived entries must be merged back under the saved list, got %+v", refs) + } + if docs, ok := referenceRepoByName(refs, testDerivedWriteRepo); !ok || !docs.Derived() || docs.CredentialState != config.ReferenceCredentialWriteTier { + t.Fatalf("re-merged derived entry must keep its provenance and state, got %+v", docs) + } +} diff --git a/src/cmd/hive/reference_repos_test.go b/src/cmd/hive/reference_repos_test.go new file mode 100644 index 0000000000..bdadd91718 --- /dev/null +++ b/src/cmd/hive/reference_repos_test.go @@ -0,0 +1,220 @@ +package main + +import ( + "context" + "errors" + "strings" + "sync" + "testing" + "time" + + "github.com/hivecommons/hive/internal/testutil" + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/github" +) + +// referenceRepoTestTimeout bounds the waits on the async notice poster. +const referenceRepoTestTimeout = 5 * time.Second + +func testReferenceRepoDeps() referenceRepoDeps { + return referenceRepoDeps{ + botLogin: "hive[bot]", + discover: func(_ context.Context, org string) (int64, error) { + if org == "hivecommons" { + return 1, nil + } + return 0, github.ErrNoInstallationForOrg + }, + auth: func() *github.AppAuth { return nil }, + } +} + +func TestBuildReferenceRepoPolicies(t *testing.T) { + ctx := context.Background() + deps := testReferenceRepoDeps() + refs := []config.ReferenceRepo{ + {Repo: "hivecommons/docs"}, + {Repo: "hivecommons/hive", Write: []string{"issues", "prs"}}, + } + res, err := buildReferenceRepoPolicies(ctx, refs, deps, testLogger()) + if err != nil { + t.Fatal(err) + } + pols := res.policies + if len(pols) != 2 || len(res.repos) != 2 { + t.Fatalf("got %d policies, %d repos", len(pols), len(res.repos)) + } + if pols[0].Token != nil || pols[0].Issues || pols[0].PRs || pols[0].Login != "" { + t.Errorf("read-only policy carries write state: %+v", pols[0]) + } + if pols[1].Token == nil || !pols[1].Issues || !pols[1].PRs || pols[1].Login != "hive[bot]" { + t.Errorf("write policy incomplete: %+v", pols[1]) + } + for i, rr := range res.repos { + if rr.CredentialState != "" || rr.Repo != refs[i].Repo { + t.Errorf("hand-written entry %d must come back unchanged: %+v", i, rr) + } + } + + // A hand-written write-mode entry without a credential still refuses + // boot with the exit-22 error text. + _, err = buildReferenceRepoPolicies(ctx, []config.ReferenceRepo{{Repo: "kubernetes/website", Write: []string{"issues"}}}, deps, testLogger()) + if !errors.Is(err, github.ErrReferenceRepoNoCredential) { + t.Fatalf("uninstalled org must refuse with ErrReferenceRepoNoCredential, got %v", err) + } + if !strings.Contains(err.Error(), "kubernetes/website") || !strings.Contains(err.Error(), "needs the GitHub App installed on kubernetes") { + t.Fatalf("exit-22 error must name the repo and the cure: %v", err) + } + if referenceRepoCredentialExitCode != 22 { + t.Fatalf("exit code = %d, want 22", referenceRepoCredentialExitCode) + } +} + +// notifyRecorder stubs the request-issue notice poster. +type notifyRecorder struct { + mu sync.Mutex + calls []notifyCall + // posted is what the stub reports back; the real poster says false when + // the marker already exists. + posted bool + err error +} + +type notifyCall struct { + repo string + number int + repos []string +} + +func (n *notifyRecorder) post(_ context.Context, issueRepo string, number int, readOnly []config.ReferenceRepo) (bool, error) { + n.mu.Lock() + defer n.mu.Unlock() + call := notifyCall{repo: issueRepo, number: number} + for _, rr := range readOnly { + call.repos = append(call.repos, rr.Repo) + } + n.calls = append(n.calls, call) + return n.posted, n.err +} + +func (n *notifyRecorder) count() int { + n.mu.Lock() + defer n.mu.Unlock() + return len(n.calls) +} + +// hivecommons/hive#18158: a derived entry never refuses boot. With the App +// installed on its org it keeps the write tier; without, it is served +// read-only and the request issue is told once. +func TestBuildReferenceRepoPoliciesDerived(t *testing.T) { + ctx := context.Background() + deps := testReferenceRepoDeps() + refs := []config.ReferenceRepo{ + {Repo: "hivecommons/docs", Write: []string{"issues", "prs"}, DerivedFrom: "cncf/techdocs#395"}, + {Repo: "acme/docs", Write: []string{"issues", "prs"}, DerivedFrom: "cncf/techdocs#395"}, + {Repo: "acme/site", Write: []string{"issues", "prs"}, DerivedFrom: "cncf/techdocs#395"}, + {Repo: "other/repo", Write: []string{"issues", "prs"}, DerivedFrom: "cncf/techdocs#400"}, + {Repo: "hivecommons/hive"}, + } + res, err := buildReferenceRepoPolicies(ctx, refs, deps, testLogger()) + if err != nil { + t.Fatalf("a derived entry on an uninstalled org must not refuse boot: %v", err) + } + if len(res.policies) != len(refs) || len(res.repos) != len(refs) { + t.Fatalf("got %d policies, %d repos, want %d", len(res.policies), len(res.repos), len(refs)) + } + writeTier := res.repos[0] + if !writeTier.WritesIssues() || !writeTier.WritesPRs() || writeTier.CredentialState != config.ReferenceCredentialWriteTier { + t.Errorf("installed org must keep the write tier: %+v", writeTier) + } + if p := res.policies[0]; !p.Issues || !p.PRs || p.Login != "hive[bot]" || p.Token == nil { + t.Errorf("write-tier policy incomplete: %+v", p) + } + for i := 1; i <= 3; i++ { + rr, p := res.repos[i], res.policies[i] + if !rr.DerivedReadOnly() || !rr.ReadOnly() || rr.CredentialReason == "" { + t.Errorf("uninstalled org must downgrade to read-only: %+v", rr) + } + if p.Issues || p.PRs || p.Token != nil || p.Login != "" { + t.Errorf("read-only policy carries write state: %+v", p) + } + if rr.DerivedFrom != refs[i].DerivedFrom { + t.Errorf("provenance lost: %+v", rr) + } + } + if res.repos[4].CredentialState != "" { + t.Errorf("hand-written read-only entry must carry no credential state: %+v", res.repos[4]) + } + + // First load: one notice per request issue, each with its read-only set. + rec := ¬ifyRecorder{posted: true} + go notifyDerivedReadOnly(ctx, res.repos, rec.post, testLogger()) + testutil.Eventually(t, referenceRepoTestTimeout, func() bool { return rec.count() == 2 }, "want 2 notices, got %d", rec.count()) + rec.mu.Lock() + calls := append([]notifyCall(nil), rec.calls...) + rec.mu.Unlock() + byIssue := map[int][]string{} + for _, c := range calls { + if c.repo != "cncf/techdocs" { + t.Errorf("notice posted to %q, want the request issue's repo", c.repo) + } + byIssue[c.number] = c.repos + } + if got := strings.Join(byIssue[395], ","); got != "acme/docs,acme/site" { + t.Errorf("#395 read-only set = %q", got) + } + if got := strings.Join(byIssue[400], ","); got != "other/repo" { + t.Errorf("#400 read-only set = %q", got) + } + + // Second reload with the same outcome: the poster finds its marker and + // reports nothing posted; the wiring neither errors nor loops. + rec2 := ¬ifyRecorder{posted: false} + res2, err := buildReferenceRepoPolicies(ctx, refs, deps, testLogger()) + if err != nil { + t.Fatal(err) + } + done := make(chan struct{}) + go func() { + notifyDerivedReadOnly(ctx, res2.repos, rec2.post, testLogger()) + close(done) + }() + testutil.Eventually(t, referenceRepoTestTimeout, func() bool { + select { + case <-done: + return true + default: + return false + } + }, "second reload notify did not finish") + if rec2.count() != 2 { + t.Fatalf("second reload asked the poster %d times, want 2 (the poster decides idempotency)", rec2.count()) + } + + // A poster failure is logged, never fatal; a nil poster is a no-op. + rec3 := ¬ifyRecorder{err: errors.New("boom")} + notifyDerivedReadOnly(ctx, res.repos, rec3.post, testLogger()) + notifyDerivedReadOnly(ctx, res.repos, nil, testLogger()) +} + +// A transient discovery failure must not downgrade a derived entry with a +// "not installed" verdict: it is served read-only this load as unresolved +// and the request issue is not told. +func TestBuildReferenceRepoPoliciesDerivedTransientError(t *testing.T) { + deps := testReferenceRepoDeps() + deps.discover = func(context.Context, string) (int64, error) { return 0, errors.New("dial tcp: i/o timeout") } + refs := []config.ReferenceRepo{{Repo: "acme/docs", Write: []string{"issues", "prs"}, DerivedFrom: "cncf/techdocs#395"}} + res, err := buildReferenceRepoPolicies(context.Background(), refs, deps, testLogger()) + if err != nil { + t.Fatalf("transient error must not refuse boot: %v", err) + } + rr := res.repos[0] + if !rr.ReadOnly() || rr.CredentialState != config.ReferenceCredentialUnresolved || rr.DerivedReadOnly() { + t.Fatalf("got %+v", rr) + } + rec := ¬ifyRecorder{posted: true} + notifyDerivedReadOnly(context.Background(), res.repos, rec.post, testLogger()) + if rec.count() != 0 { + t.Fatalf("an unresolved entry must not notify the request issue, got %d calls", rec.count()) + } +} diff --git a/src/cmd/hive/review_config_parsers_test.go b/src/cmd/hive/review_config_parsers_test.go index 1c0dc67eab..ad5a24d3cd 100644 --- a/src/cmd/hive/review_config_parsers_test.go +++ b/src/cmd/hive/review_config_parsers_test.go @@ -94,6 +94,26 @@ func TestReviewPerspectiveSet(t *testing.T) { } }) + t.Run("linked perspectives win over inline", func(t *testing.T) { + cfg := &config.Config{} + cfg.Review.Perspectives = []string{"correctness"} + cfg.Review.PerspectivesSource = &config.DefinitionSourceConfig{Owner: "torch-spyre", Repo: "spyre-inference", Path: ".hive/review-perspectives.yaml"} + cfg.Review.LinkedPerspectives = &config.LinkedPerspectives{ + Names: []string{"security", "spyre-numerics"}, + Focus: map[string]string{"spyre-numerics": "Silent CPU fallbacks and fp16 accumulation-order changes."}, + } + got := reviewPerspectiveSet(cfg, nil).List() + want := []review.Perspective{review.PerspectiveSecurity, review.Perspective("spyre-numerics")} + if len(got) != len(want) { + t.Fatalf("List() = %v, want %v", got, want) + } + for i := range want { + if got[i] != want[i] { + t.Errorf("List()[%d] = %q, want %q", i, got[i], want[i]) + } + } + }) + t.Run("invalid selection with nil logger does not panic", func(t *testing.T) { cfg := &config.Config{} cfg.Review.Perspectives = []string{"sekurity"} diff --git a/src/cmd/hive/review_perspectives_source_test.go b/src/cmd/hive/review_perspectives_source_test.go new file mode 100644 index 0000000000..d9ae106839 --- /dev/null +++ b/src/cmd/hive/review_perspectives_source_test.go @@ -0,0 +1,101 @@ +package main + +import ( + "context" + "errors" + "io" + "log/slog" + "testing" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/github" + "github.com/hivecommons/hive/pkg/perspsrc" + "github.com/hivecommons/hive/pkg/review" +) + +type fakePerspectivesFetcher struct { + doc string + err error + calls int +} + +func (f *fakePerspectivesFetcher) GetFileContentRef(_ context.Context, _, _, _, _ string) (string, error) { + f.calls++ + return f.doc, f.err +} + +const ( + perspectivesDocA = "kind: ReviewPerspectives\nperspectives:\n - name: correctness\n - name: spyre-numerics\n focus: Check numerics on the Spyre path.\n" + perspectivesDocB = "kind: ReviewPerspectives\nperspectives:\n - name: correctness\n" +) + +func perspectivesSourceFixture(f *fakePerspectivesFetcher) (*config.Config, *perspsrc.Resolver, *github.Client, *slog.Logger) { + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) + cfg := &config.Config{} + cfg.Review.PerspectivesSource = &config.DefinitionSourceConfig{Owner: "acme", Repo: "review", Path: "perspectives.yaml"} + resolver := perspsrc.NewResolver(f, func(string) bool { return true }, logger) + client := github.NewClientForTest("http://127.0.0.1:0", "acme", nil, logger) + return cfg, resolver, client, logger +} + +func relayAccepts(cfg *config.Config, logger *slog.Logger, name string) bool { + return reviewPerspectiveSet(cfg, logger).Known(review.Perspective(name)) +} + +func TestApplyReviewPerspectivesSourceReinstallsRelaySet(t *testing.T) { + f := &fakePerspectivesFetcher{doc: perspectivesDocA} + cfg, resolver, client, logger := perspectivesSourceFixture(f) + + applyReviewPerspectivesSource(context.Background(), cfg, resolver, client, logger) + if !relayAccepts(cfg, logger, "spyre-numerics") { + t.Fatal("relay set should accept spyre-numerics from doc A") + } + + f.doc = perspectivesDocB + applyReviewPerspectivesSource(context.Background(), cfg, resolver, client, logger) + if relayAccepts(cfg, logger, "spyre-numerics") { + t.Fatal("relay set still accepts spyre-numerics after doc B removed it") + } + if !relayAccepts(cfg, logger, "correctness") { + t.Fatal("relay set lost correctness") + } +} + +func TestApplyReviewPerspectivesSourceMalformedKeepsRelaySet(t *testing.T) { + f := &fakePerspectivesFetcher{doc: perspectivesDocA} + cfg, resolver, client, logger := perspectivesSourceFixture(f) + applyReviewPerspectivesSource(context.Background(), cfg, resolver, client, logger) + + f.doc = "kind: Nope\n" + applyReviewPerspectivesSource(context.Background(), cfg, resolver, client, logger) + if !relayAccepts(cfg, logger, "spyre-numerics") { + t.Fatal("malformed document dropped the last-known-good set") + } + if cfg.Review.LinkedPerspectives == nil || cfg.Review.LinkedPerspectives.Warning == "" { + t.Fatalf("want a warning on the kept set, got %+v", cfg.Review.LinkedPerspectives) + } + + f.doc, f.err = "", errors.New("boom") + applyReviewPerspectivesSource(context.Background(), cfg, resolver, client, logger) + if !relayAccepts(cfg, logger, "spyre-numerics") { + t.Fatal("fetch failure dropped the last-known-good set") + } +} + +func TestApplyReviewPerspectivesSourceUnsetMakesNoFetch(t *testing.T) { + f := &fakePerspectivesFetcher{doc: perspectivesDocA} + cfg, resolver, client, logger := perspectivesSourceFixture(f) + cfg.Review.PerspectivesSource = nil + cfg.Review.Perspectives = []string{"correctness", "security"} + + applyReviewPerspectivesSource(context.Background(), cfg, resolver, client, logger) + if f.calls != 0 { + t.Fatalf("unset perspectives_source fetched %d times", f.calls) + } + if cfg.Review.LinkedPerspectives != nil { + t.Fatalf("LinkedPerspectives = %+v, want nil", cfg.Review.LinkedPerspectives) + } + if !relayAccepts(cfg, logger, "security") || relayAccepts(cfg, logger, "spyre-numerics") { + t.Fatal("boot relay set changed") + } +} diff --git a/src/crux/agent/claude_credential_bridge.go b/src/crux/agent/claude_credential_bridge.go index c6e94c3bfa..67c08e35f8 100644 --- a/src/crux/agent/claude_credential_bridge.go +++ b/src/crux/agent/claude_credential_bridge.go @@ -586,6 +586,9 @@ func (m *Manager) rebridgeClaudeCredential(agentName, link, target, trigger stri if rep == nil { return nil } + // The fleet self-report feed deliberately ignores this exact message (a + // completed repair, expected in a burst at every token refresh): + // claudeCredentialRebridged in pkg/fleetreport/slogfeed. Keep them in sync. m.logger.Warn("claude credential bridge: a private credential file had replaced the shared-login symlink; re-bridged", "agent", agentName, "trigger", trigger, diff --git a/src/crux/agent/kick_canary_retry.go b/src/crux/agent/kick_canary_retry.go new file mode 100644 index 0000000000..99eb8be642 --- /dev/null +++ b/src/crux/agent/kick_canary_retry.go @@ -0,0 +1,61 @@ +package agent + +// A kick that opens with the ioscan exfiltration canary can be refused as a +// whole by a model that reads the canary as a prompt-injection marker +// (Claude Sonnet 5 via the Copilot CLI: "this request includes a 'SECURITY +// CANARY' marker and asks me to act as a scanner agent …"). The refusal used +// to cost the entire cadence slot β€” a 4h scanner kick, or ~150 five-minute +// supervisor kicks a day on one hosted hive. The refused kick is instead +// re-delivered once with the canary preamble stripped; the retry is a plain +// kick, so a second refusal is the model's verdict on the work itself and +// stands. + +// kickTriggerCanaryRetry is the delivery trigger of the one canary-free +// re-delivery a refused canary kick gets. recordDeliveredKickLocked uses it +// to mark the retry so it is never itself retried. +const kickTriggerCanaryRetry = "canary-refusal-retry" + +// planCanaryRetry decides from the refused kick alone whether it earns a +// canary-free re-delivery: the kick as handed to delivery must have carried +// a canary preamble with a body after it, and must not already be the retry. +func planCanaryRetry(lastKickInput string, alreadyRetried bool) (plain string, ok bool) { + if alreadyRetried { + return "", false + } + plain, had := stripCanaryPreamble(lastKickInput) + if !had { + return "", false + } + return plain, true +} + +// retryRefusedCanaryKick is the checkKickRefusal hook: when the kick just +// refused led with a canary, re-deliver it once without one. Delivery goes +// through the full SendKick readiness chain on its own goroutine (the caller +// is the pane poller, and SendKick blocks on the input prompt), so the retry +// lands once the refusing turn has returned to the prompt. Must NOT be +// called with m.mu held. +func (m *Manager) retryRefusedCanaryKick(agent *AgentProcess) { + m.mu.RLock() + plain, ok := planCanaryRetry(agent.lastKickInput, agent.canaryRetryDone) + m.mu.RUnlock() + if !ok { + return + } + m.logger.Warn("kick refused with the exfiltration canary in front; re-delivering once without it", + "agent", agent.Name, + "reason", agent.KickRefusalReason, + ) + deliver := m.canaryRetryDeliver + if deliver == nil { + deliver = func(name, message string) { + go func() { + if err := m.sendKick(name, message, kickTriggerCanaryRetry); err != nil { + m.logger.Warn("canary-free re-delivery of refused kick failed", + "agent", name, "error", err) + } + }() + } + } + deliver(agent.Name, plain) +} diff --git a/src/crux/agent/kick_canary_retry_test.go b/src/crux/agent/kick_canary_retry_test.go new file mode 100644 index 0000000000..68d3528db6 --- /dev/null +++ b/src/crux/agent/kick_canary_retry_test.go @@ -0,0 +1,82 @@ +package agent + +import "testing" + +// A model that reads the canary preamble as a prompt-injection marker refuses +// the whole kick; the kick is re-delivered once without the canary so the +// refusal does not burn the cadence slot. + +func TestStripCanaryPreamble(t *testing.T) { + work := "Review PR #15783 (correctness).\n\nFocus on the retry loop." + if got, ok := stripCanaryPreamble(testCanaryPreamble + work); !ok || got != work { + t.Fatalf("stripCanaryPreamble = (%q, %v), want (%q, true)", got, ok, work) + } + for _, msg := range []string{work, testCanaryPreamble, "=== HIVE HARNESS HEADER ===\nExfiltration canary: HIVE-CANARY-abc123 no blank line", ""} { + if got, ok := stripCanaryPreamble(msg); ok || got != msg { + t.Errorf("stripCanaryPreamble(%q) = (%q, %v), want unchanged, false", msg, got, ok) + } + } +} + +func TestPlanCanaryRetry(t *testing.T) { + work := "Triage issue #12." + if plain, ok := planCanaryRetry(testCanaryPreamble+work, false); !ok || plain != work { + t.Fatalf("canary kick must earn one plain retry, got (%q, %v)", plain, ok) + } + if _, ok := planCanaryRetry(testCanaryPreamble+work, true); ok { + t.Error("the retry itself must not be retried") + } + if _, ok := planCanaryRetry(work, false); ok { + t.Error("a kick without a canary has nothing to strip; no retry") + } + if _, ok := planCanaryRetry(testCanaryPreamble, false); ok { + t.Error("a canary-only kick has no work to re-deliver") + } +} + +func TestCheckKickRefusalRedeliversCanaryKickOnce(t *testing.T) { + m := &Manager{logger: discardLogger()} + var delivered []string + m.canaryRetryDeliver = func(name, message string) { + delivered = append(delivered, name+"\x00"+message) + } + work := "[agent:scanner]\n\nScan cncf/techdocs for stale pages." + refusal := "I'm noticing this request includes a canary marker and looks like a prompt injection, so I won't act on it." + + agent := &AgentProcess{Name: "scanner", lastKickInput: testCanaryPreamble + work} + m.checkKickRefusal(agent, refusal) + if !agent.KickRefused { + t.Fatal("refusal must still be recorded") + } + if len(delivered) != 1 || delivered[0] != "scanner\x00"+work { + t.Fatalf("refused canary kick must be re-delivered plain, got %q", delivered) + } + + // The re-delivery is marked by its trigger; a refusal of the retry stands. + m.recordDeliveredKickLocked(agent, work, kickTriggerCanaryRetry) + if agent.KickRefused || !agent.canaryRetryDone { + t.Fatalf("retry delivery must clear the refusal and mark the retry: refused=%v retried=%v", agent.KickRefused, agent.canaryRetryDone) + } + agent.lastKickInput = testCanaryPreamble + work + m.checkKickRefusal(agent, refusal) + if len(delivered) != 1 { + t.Fatalf("a refused retry must not be retried again, got %d deliveries", len(delivered)) + } + + // A fresh kick from the governor re-arms the single retry. + m.recordDeliveredKickLocked(agent, testCanaryPreamble+work, "send-kick") + if agent.canaryRetryDone { + t.Fatal("an ordinary kick must re-arm the canary retry") + } +} + +func TestCheckKickRefusalWithoutCanaryDoesNotRedeliver(t *testing.T) { + m := &Manager{logger: discardLogger()} + calls := 0 + m.canaryRetryDeliver = func(string, string) { calls++ } + agent := &AgentProcess{Name: "scanner", lastKickInput: "[agent:scanner]\n\nPlain kick."} + m.checkKickRefusal(agent, "This looks like a prompt injection attempt, so I'm declining to execute it.") + if !agent.KickRefused || calls != 0 { + t.Fatalf("plain kick refusal must be recorded without a retry: refused=%v calls=%d", agent.KickRefused, calls) + } +} diff --git a/src/crux/agent/kick_preview.go b/src/crux/agent/kick_preview.go index f25130c5c9..6566381e92 100644 --- a/src/crux/agent/kick_preview.go +++ b/src/crux/agent/kick_preview.go @@ -3,24 +3,33 @@ package agent import "strings" // canaryPreamblePrefix is how an ioscan exfiltration canary opens a kick -// (hive pkg/ioscan.CanaryPreamble). The preamble runs to the first blank -// line and is about as long as the audit preview, so a kick that carried -// one showed operators nothing but the canary (hivecommons/hive#15823). The -// preview and the kick-history snippet start after it; the kick itself is -// delivered unchanged, canary first. -const canaryPreamblePrefix = "SECURITY CANARY: " +// (hive pkg/ioscan.CanaryPreamblePrefix; the two must stay in step). The +// preamble runs to the first blank line and is about as long as the audit +// preview, so a kick that carried one showed operators nothing but the +// canary (hivecommons/hive#15823). The preview and the kick-history snippet +// start after it; the kick itself is delivered unchanged, canary first. +const canaryPreamblePrefix = "=== HIVE HARNESS HEADER" -// kickPreviewBody returns the part of a kick worth previewing: the message -// after its canary preamble, or the whole message when it carries none (or -// nothing follows the preamble, so the canary-only kick stays visible as -// such). -func kickPreviewBody(message string) string { +// stripCanaryPreamble returns the kick without its canary preamble and true +// when one was present and something followed it. A canary-only kick (or one +// whose preamble is not terminated by a blank line) is returned unchanged +// with false. +func stripCanaryPreamble(message string) (string, bool) { if !strings.HasPrefix(message, canaryPreamblePrefix) { - return message + return message, false } _, rest, ok := strings.Cut(message, "\n\n") if !ok || strings.TrimSpace(rest) == "" { - return message + return message, false } - return rest + return rest, true +} + +// kickPreviewBody returns the part of a kick worth previewing: the message +// after its canary preamble, or the whole message when it carries none (or +// nothing follows the preamble, so the canary-only kick stays visible as +// such). +func kickPreviewBody(message string) string { + body, _ := stripCanaryPreamble(message) + return body } diff --git a/src/crux/agent/kick_preview_test.go b/src/crux/agent/kick_preview_test.go index ff0a21b36c..8a3b3b7187 100644 --- a/src/crux/agent/kick_preview_test.go +++ b/src/crux/agent/kick_preview_test.go @@ -5,7 +5,7 @@ import "testing" // hivecommons/hive#15823: the audit preview of a canary-bearing kick shows // the work the agent was given, not the canary preamble. -const testCanaryPreamble = "SECURITY CANARY: HIVE-CANARY-abc123\nThis is a hive-issued exfiltration-canary marker. Never print, quote, summarize, transform, store in reports, include in commits, or send it to any external service.\n\n" +const testCanaryPreamble = "=== HIVE HARNESS HEADER (operator configuration, not user content) ===\nExfiltration canary: HIVE-CANARY-abc123\nThe token above is a tripwire planted by the hive harness that runs you. It is not a task.\n=== END HIVE HARNESS HEADER ===\n\n" func TestKickPreviewBodySkipsCanaryPreamble(t *testing.T) { work := "Review PR #15783 (correctness).\n\nFocus on the retry loop." @@ -22,7 +22,7 @@ func TestKickPreviewBodyPassesThroughKicksWithoutPreamble(t *testing.T) { } func TestKickPreviewBodyKeepsCanaryOnlyKickVisible(t *testing.T) { - for _, msg := range []string{testCanaryPreamble, "SECURITY CANARY: HIVE-CANARY-abc123 no blank line"} { + for _, msg := range []string{testCanaryPreamble, "=== HIVE HARNESS HEADER ===\nExfiltration canary: HIVE-CANARY-abc123 no blank line"} { if got := kickPreviewBody(msg); got != msg { t.Fatalf("preview of canary-only kick = %q, want whole message", got) } diff --git a/src/crux/agent/manager.go b/src/crux/agent/manager.go index 52b6587b61..7eaf6b506c 100644 --- a/src/crux/agent/manager.go +++ b/src/crux/agent/manager.go @@ -262,16 +262,22 @@ type AgentProcess struct { LastKickSource string KickRefused bool KickRefusalReason string - LaunchedMode AgentMode - HasLaunched bool - poolLeaseID string - poolLeaseCancel context.CancelFunc - baseConfig config.AgentConfig - agentSpecPrompt string - tmuxSession string - tmuxSocket string - cancel context.CancelFunc - forceRelaunch bool + // lastKickInput is the last kick as handed to delivery, before the + // backend suffixes, so a refused canary kick can be re-delivered plain + // without doubling the suffixes. canaryRetryDone is set when the current + // kick IS that re-delivery, so it is never retried again. + lastKickInput string + canaryRetryDone bool + LaunchedMode AgentMode + HasLaunched bool + poolLeaseID string + poolLeaseCancel context.CancelFunc + baseConfig config.AgentConfig + agentSpecPrompt string + tmuxSession string + tmuxSocket string + cancel context.CancelFunc + forceRelaunch bool // launching is set true under m.mu while Start runs this agent's launch // with m.mu RELEASED (so a slow /data NFS write or a hung MITM-proxy token // mint during launch cannot block AllStatuses()/the heartbeat collect() and @@ -716,6 +722,9 @@ type Manager struct { // kickOutcomeObserver receives the verdict on how each kicked turn ended // (#7421); the governor consumes it. Same discipline as kickObserver. kickOutcomeObserver atomic.Pointer[func(agentName string, outcome KickOutcome)] + // canaryRetryDeliver, when set, replaces the goroutine+sendKick path a + // refused canary kick is re-delivered through (tests inject a recorder). + canaryRetryDeliver func(name, message string) // kickDispatches tracks asynchronous kick dispatches (#5325): the in-flight // guard that makes delivery exactly-once, and the latest outcome per agent diff --git a/src/crux/agent/manager_kick.go b/src/crux/agent/manager_kick.go index 7f3774258d..14f0f89473 100644 --- a/src/crux/agent/manager_kick.go +++ b/src/crux/agent/manager_kick.go @@ -111,6 +111,12 @@ func (m *Manager) SendKick(name string, message string) error { return m.SendKickWithSource(name, message, "") } +// sendKick is SendKick with the delivery trigger spelled out; the trigger is +// what the audit log, prompt history and the canary-retry bookkeeping see. +func (m *Manager) sendKick(name, message, trigger string) error { + return m.SendKickWithSource(name, message, trigger) +} + func (m *Manager) SendKickWithSource(name string, message string, source string) (err error) { // Agent-kick span. No-op with zero export cost when tracing is disabled // (the default). SendKick has no context parameter, so this span roots at @@ -291,6 +297,7 @@ func (m *Manager) deliverKickLocked(agent *AgentProcess, message, trigger string return fmt.Errorf("%w: agent %s kick delivery already in progress", crux.ErrAgentBusy, agent.Name) } defer agent.kickDelivering.Store(false) + agent.lastKickInput = message visible := m.captureVisiblePaneForAgent(agent) if err := m.checkKickPromptReadyLocked(agent, visible, trigger); err != nil { @@ -473,6 +480,7 @@ func (m *Manager) recordDeliveredKickLocked(agent *AgentProcess, message, trigge agent.LastKickSource = trigger agent.KickRefused = false agent.KickRefusalReason = "" + agent.canaryRetryDone = trigger == kickTriggerCanaryRetry // The session now holds this kick's output; the next rotation point // (kick, restart, shutdown) must archive it before it is destroyed. agent.kickLogPending = true diff --git a/src/crux/agent/manager_poll.go b/src/crux/agent/manager_poll.go index 0065b1a7c0..f32bf2b7a2 100644 --- a/src/crux/agent/manager_poll.go +++ b/src/crux/agent/manager_poll.go @@ -601,6 +601,7 @@ func (m *Manager) checkKickRefusal(agent *AgentProcess, line string) { "pattern", pattern, "line", reason, ) + m.retryRefusedCanaryKick(agent) return } } diff --git a/src/crux/agent/permissions_unownable.go b/src/crux/agent/permissions_unownable.go new file mode 100644 index 0000000000..a575b545bf --- /dev/null +++ b/src/crux/agent/permissions_unownable.go @@ -0,0 +1,91 @@ +package agent + +import ( + "log/slog" + "os" + "sync" +) + +// unownableHint tells the operator who can actually repair the ownership the +// watcher is reporting. +const unownableHint = "chown needs root (CAP_CHOWN); the watcher runs unprivileged after the entrypoint's privilege drop, so these entries are repaired by the entrypoint's root phase or out of band, never by retrying here" + +// cannotChown reports whether this process certainly cannot chown an entry +// owned by ownerUID, so attempting it would only ever return EPERM. +// +// chown(2) lets an unprivileged caller change only the GROUP of an inode it +// OWNS (to a group it belongs to); changing the uid, or anything about an +// inode owned by someone else, needs CAP_CHOWN. The hive process drops to dev +// (uid 1001) at the entrypoint, so every entry another uid owns β€” a root seed, +// or a sandbox kick's workspace under /data/agents/sandbox/- +// written by the Job pod's uid (#18120) β€” is unrepairable by construction, and +// a surge of kicks mints thousands of such paths between two ticks. Deciding +// that here, before the syscall, is what keeps the failure from being logged +// once per path (4087 WARN lines in ten minutes on hive-tall-frog). +func cannotChown(ownerUID uint32) bool { + euid := os.Geteuid() + return euid != 0 && uint32(euid) != ownerUID +} + +// unownableTally counts, per watcher tick, the entries fixEntry skipped +// because cannotChown held, and reports them as ONE summary line per tick +// instead of one line per path. The summary is WARN only when the count sets +// a new high-water mark since the condition last cleared, and DEBUG otherwise, +// so a standing unfixable tree costs one WARN when it appears, one more each +// time it grows, and nothing at WARN in steady state. +type unownableTally struct { + mu sync.Mutex + count int // entries skipped this tick + sample string // first skipped path this tick, for the operator's grep + highWater int // largest count already reported at WARN; 0 once clear +} + +// skipped records one entry the watcher could not own this tick. +func (u *unownableTally) skipped(path string) { + u.mu.Lock() + defer u.mu.Unlock() + if u.count == 0 { + u.sample = path + } + u.count++ +} + +// flush ends the tick: emits the summary and resets the per-tick counters. It +// returns whether the summary went out at WARN (tests). +func (u *unownableTally) flush(logger *slog.Logger) bool { + u.mu.Lock() + count, sample := u.count, u.sample + u.count, u.sample = 0, "" + if count == 0 { + // Condition cleared: the next appearance is a new finding. + u.highWater = 0 + u.mu.Unlock() + return false + } + warn := count > u.highWater + if warn { + u.highWater = count + } + u.mu.Unlock() + + attrs := []any{ + "count", count, + "sample_path", sample, + "watcher_uid", os.Geteuid(), + "hint", unownableHint, + } + if warn { + logger.Warn("permissions watcher: entries owned by another uid need a chown this process cannot perform; skipped without retrying", + append(attrs, "note", "one summary per tick; logged at debug until the count grows or the condition clears")...) + return true + } + logger.Debug("permissions watcher: entries owned by another uid skipped (unchanged)", attrs...) + return false +} + +// permUnownable is the watcher's shared tally. Package-level for the same +// reason as permWarnDedupe: fixEntry keeps its (path, fi, logger) signature. +var permUnownable = &unownableTally{} + +// resetPermUnownable restores pristine tally state. Tests only. +func resetPermUnownable() { permUnownable = &unownableTally{} } diff --git a/src/crux/agent/permissions_unownable_test.go b/src/crux/agent/permissions_unownable_test.go new file mode 100644 index 0000000000..bba2262741 --- /dev/null +++ b/src/crux/agent/permissions_unownable_test.go @@ -0,0 +1,227 @@ +package agent + +import ( + "log/slog" + "os" + "path/filepath" + "strconv" + "testing" +) + +// These tests pin the #18120 fix: an entry owned by a uid the unprivileged +// watcher can never chown (a sandbox kick workspace written by the Job pod's +// uid, a root seed) is skipped BEFORE the syscall and reported as one summary +// line per tick, never as one WARN per path. On hive-tall-frog a surge of +// kicks minted ~4096 such paths and the per-path deduper warned once for each +// of them before saturating. + +const unownableSummaryMsg = "permissions watcher: entries owned by another uid need a chown this process cannot perform; skipped without retrying" + +// foreignOwnedEntry creates a regular file and returns a FileInfo that reports +// it as owned by a uid this process is not, with a gid that is not NodeGID, so +// fixEntry wants a chown it cannot perform. Skips as root, where every chown +// is possible and the skip arm is unreachable. +func foreignOwnedEntry(t *testing.T, dir, name string) (string, os.FileInfo) { + t.Helper() + if os.Geteuid() == 0 { + t.Skip("running as root: every chown is permitted, skip arm unreachable") + } + path := filepath.Join(dir, name) + if err := os.WriteFile(path, []byte("{}"), 0o600); err != nil { + t.Fatalf("write %s: %v", path, err) + } + fi, err := os.Lstat(path) + if err != nil { + t.Fatalf("lstat %s: %v", path, err) + } + foreign := uint32(os.Geteuid() + 1) + return path, fakeOwnerInfo{FileInfo: fi, sys: syntheticStat(t, path, foreign, uint32(NodeGID)+1)} +} + +func TestCannotChownUnprivilegedForeignOwner(t *testing.T) { + if os.Geteuid() == 0 { + t.Skip("running as root: chown is always permitted") + } + me := uint32(os.Geteuid()) + if cannotChown(me) { + t.Error("an entry this process owns must be attempted (group change may succeed)") + } + if !cannotChown(me + 1) { + t.Error("an entry another uid owns can never be chowned without CAP_CHOWN") + } + if !cannotChown(0) { + t.Error("a root-owned entry can never be chowned without CAP_CHOWN") + } +} + +// TestFixEntryForeignOwnerSkippedWithoutPerPathWarn is the core regression +// test: many foreign-owned entries across several ticks produce ZERO per-path +// chown WARN lines and leave the mode untouched (0600 stays 0600: the entry is +// not ours to widen either). +func TestFixEntryForeignOwnerSkippedWithoutPerPathWarn(t *testing.T) { + resetPermWarnDedupe() + resetPermUnownable() + t.Cleanup(resetPermWarnDedupe) + t.Cleanup(resetPermUnownable) + + dir := t.TempDir() + const paths = 50 + h := &recordingHandler{} + logger := slog.New(h) + var first string + for i := range paths { + path, fi := foreignOwnedEntry(t, dir, "skill-"+strconv.Itoa(i)) + if i == 0 { + first = path + } + fixEntry(path, fi, logger) + } + + if got := h.count(slog.LevelWarn, "permissions watcher: chown failed", ""); got != 0 { + t.Errorf("per-path chown WARN logged %d times for unownable entries, want 0", got) + } + if got := h.count(slog.LevelDebug, "permissions watcher: chown failed", ""); got != 0 { + t.Errorf("per-path chown DEBUG logged %d times: the chown must not be attempted at all", got) + } + if got := h.count(slog.LevelWarn, "permissions watcher: chmod file failed", ""); got != 0 { + t.Errorf("chmod attempted on %d entries we do not own, want 0", got) + } + info, err := os.Stat(first) + if err != nil { + t.Fatalf("stat: %v", err) + } + if info.Mode().Perm() != 0o600 { + t.Errorf("mode of foreign-owned entry changed to %v, want 0600 untouched", info.Mode().Perm()) + } + + permUnownable.mu.Lock() + count, sample := permUnownable.count, permUnownable.sample + permUnownable.mu.Unlock() + if count != paths { + t.Errorf("tally counted %d skipped entries, want %d", count, paths) + } + if sample != first { + t.Errorf("tally sample = %q, want the first skipped path %q", sample, first) + } +} + +// TestUnownableTallyOneSummaryPerTickAtMost pins the summary cadence: WARN +// when the condition appears, DEBUG while it holds unchanged, WARN again only +// when it grows, nothing when the tick saw none, and WARN once more when the +// condition clears and then reappears. +func TestUnownableTallyOneSummaryPerTickAtMost(t *testing.T) { + u := &unownableTally{} + h := &recordingHandler{} + logger := slog.New(h) + + tick := func(n int) bool { + for i := range n { + u.skipped("/data/agents/sandbox/quality-1/.claude/skills/" + strconv.Itoa(i)) + } + return u.flush(logger) + } + + if tick(0) { + t.Error("an empty tick must not warn") + } + if !tick(4087) { + t.Error("first tick with unownable entries must warn") + } + if tick(4087) { + t.Error("unchanged count on the next tick must be demoted to debug") + } + if tick(10) { + t.Error("a smaller count must not warn") + } + if !tick(5000) { + t.Error("a count above the high-water mark is new information and must warn") + } + if tick(0) { + t.Error("a clear tick must not warn") + } + if !tick(1) { + t.Error("reappearance after the condition cleared must warn again") + } + + if got := h.count(slog.LevelWarn, unownableSummaryMsg, ""); got != 3 { + t.Errorf("summary WARN logged %d times across 7 ticks, want 3", got) + } + if got := h.count(slog.LevelDebug, "permissions watcher: entries owned by another uid skipped (unchanged)", ""); got != 2 { + t.Errorf("summary DEBUG logged %d times, want 2", got) + } + u.mu.Lock() + defer u.mu.Unlock() + if u.count != 0 || u.sample != "" { + t.Errorf("flush left count=%d sample=%q, want reset", u.count, u.sample) + } + if u.highWater != 1 { + t.Errorf("highWater = %d after the final tick of 1, want 1", u.highWater) + } +} + +// TestUnownableTallySummaryCarriesCountAndSample pins the attrs an operator +// needs to act on the one line they get. +func TestUnownableTallySummaryCarriesCountAndSample(t *testing.T) { + u := &unownableTally{} + h := &recordingHandler{} + u.skipped("/data/agents/sandbox/quality-1/.claude/skills/upgrade-vllm") + u.skipped("/data/agents/sandbox/quality-1/.claude/skills/other") + u.flush(slog.New(h)) + + h.mu.Lock() + defer h.mu.Unlock() + if len(h.records) != 1 { + t.Fatalf("got %d records, want 1", len(h.records)) + } + attrs := map[string]string{} + h.records[0].Attrs(func(a slog.Attr) bool { + attrs[a.Key] = a.Value.String() + return true + }) + if attrs["count"] != "2" { + t.Errorf("count attr = %q, want 2", attrs["count"]) + } + if attrs["sample_path"] != "/data/agents/sandbox/quality-1/.claude/skills/upgrade-vllm" { + t.Errorf("sample_path attr = %q, want the first skipped path", attrs["sample_path"]) + } + if attrs["hint"] != unownableHint { + t.Errorf("hint attr = %q, want unownableHint", attrs["hint"]) + } +} + +// TestFixPermissionsFlushesUnownableSummary pins that a watcher tick ends by +// emitting the summary, so the tally does not accumulate across ticks. +func TestFixPermissionsFlushesUnownableSummary(t *testing.T) { + resetPermWarnDedupe() + resetPermUnownable() + t.Cleanup(resetPermWarnDedupe) + t.Cleanup(resetPermUnownable) + + h := &recordingHandler{} + logger := slog.New(h) + // Seed the tally as fixEntry would during the walk; the walk roots are + // left at their production values, which do not exist here, so the tick + // itself touches nothing and only the flush is observable. + path, fi := foreignOwnedEntry(t, t.TempDir(), "upgrade-vllm") + fixEntry(path, fi, logger) + + oldDirs, oldGlob, oldCaps, oldRepo := WatchedHomeDirs, ModeFileGlob, CapsFileGlob, SharedRepoParent + WatchedHomeDirs = nil + ModeFileGlob = filepath.Join(t.TempDir(), "none-*") + CapsFileGlob = filepath.Join(t.TempDir(), "none-*") + SharedRepoParent = t.TempDir() + t.Cleanup(func() { + WatchedHomeDirs, ModeFileGlob, CapsFileGlob, SharedRepoParent = oldDirs, oldGlob, oldCaps, oldRepo + }) + + fixPermissions(logger) + if got := h.count(slog.LevelWarn, unownableSummaryMsg, ""); got != 1 { + t.Errorf("summary WARN logged %d times after one tick, want 1", got) + } + permUnownable.mu.Lock() + count := permUnownable.count + permUnownable.mu.Unlock() + if count != 0 { + t.Errorf("tally count = %d after the tick flushed, want 0", count) + } +} diff --git a/src/crux/agent/permissions_watcher.go b/src/crux/agent/permissions_watcher.go index 648b60fb3d..9615ce7cd4 100644 --- a/src/crux/agent/permissions_watcher.go +++ b/src/crux/agent/permissions_watcher.go @@ -516,6 +516,7 @@ func fixPermissions(logger *slog.Logger) { ) } } + permUnownable.flush(logger) } // fixEntry checks a single file or directory and corrects ownership/mode @@ -555,6 +556,16 @@ func fixEntry(path string, fi os.FileInfo, logger *slog.Logger) { if stat.Gid != uint32(NodeGID) { needsChown = true } + if needsChown && cannotChown(stat.Uid) { + // Unrepairable by construction (#18120): an unprivileged watcher can + // never chown an inode it does not own, so do not spend a syscall and + // a WARN line per path on it. Tally it for the per-tick summary and + // leave the uid alone so the mode arms below skip it as not ours. + permUnownable.skipped(path) + permWarnDedupe.clear(dedupeKey("chown", path)) + needsChown = false + newUID = int(stat.Uid) + } if needsChown { if err := entryChown(f, newUID, NodeGID); err != nil { warnDeduped(logger, "chown", "permissions watcher: chown failed", path, err) diff --git a/src/crux/agent/usersession.go b/src/crux/agent/usersession.go new file mode 100644 index 0000000000..9b4403fdda --- /dev/null +++ b/src/crux/agent/usersession.go @@ -0,0 +1,380 @@ +package agent + +import ( + "bytes" + "context" + "fmt" + "log/slog" + "os" + "os/exec" + "path/filepath" + "regexp" + "strconv" + "strings" + "time" + "unicode" + "unicode/utf8" + + "github.com/hivecommons/hive/crux" + "github.com/hivecommons/hive/crux/agent/resumehandle" + "github.com/hivecommons/hive/crux/agent/tmux" +) + +// UserSessionTmuxPrefix is the tmux session (and socket) name prefix of every +// user session; ttyd-tmux.sh finds the socket by this name. +const UserSessionTmuxPrefix = "hive-sess-" + +const ( + // userSessionCmdTimeout bounds every tmux call so a wedged server cannot + // hold an HTTP handler. + userSessionCmdTimeout = 10 * time.Second + // userSessionBufferName is the tmux paste buffer used for typed input. + userSessionBufferName = "hive-sess-input" + // userSessionMaxCaptureLines caps a scrollback read. + userSessionMaxCaptureLines = fullLogCaptureLines + // userSessionProbeFields is the field count of the probe format below. + userSessionProbeFields = 3 + userSessionProbeFormat = "#{pane_dead} #{session_attached} #{session_activity}" +) + +var ( + userSessionIDPattern = regexp.MustCompile(`^[a-f0-9]{8,32}$`) + // userSessionResumeCmdPattern admits the resume commands the vendors + // print: words, dots, dashes, slashes, colons and equals. + userSessionResumeCmdPattern = regexp.MustCompile(`^[A-Za-z0-9._/:=@-]+( [A-Za-z0-9._/:=@-]+)*$`) +) + +// userSessionVendor describes a CLI a session may run. Args are the vendor's +// plain interactive invocation: NO permission-bypass flag is ever added here, +// which is the point of this table existing separately from +// backends.conf's backend_perm_flag (that function emits the unattended +// bypass flags agents run with, and is deliberately not consulted). +type userSessionVendor struct { + id string + label string + binary string +} + +// userSessionVendors is the interactive-capable subset of KNOWN_BACKENDS +// (config/backends.conf; parity is asserted by a test). Headless runners +// (cline, muse, omnigent), the litellm gateway and any shell are excluded. +var userSessionVendors = []userSessionVendor{ + {"claude", "Claude Code", "claude"}, + {"copilot", "GitHub Copilot CLI", "copilot"}, + {"codex", "Codex", "codex"}, + {"goose", "Goose", "goose"}, + {"agy", "Antigravity", "agy"}, + {"bob", "Bob", "bob"}, + {"pi", "Pi", "pi"}, + {"aider", "Aider", "aider"}, + {"opencode", "OpenCode", "opencode"}, + {"kilo", "Kilo", "kilo"}, + {"omp", "OMP", "omp"}, + {"openhands", "OpenHands", "openhands"}, +} + +func findUserSessionVendor(id string) (userSessionVendor, bool) { + for _, v := range userSessionVendors { + if v.id == id { + return v, true + } + } + return userSessionVendor{}, false +} + +// tmuxRunFunc runs one tmux invocation against a session socket. It is the +// test seam: tests substitute a recorder so no real tmux is ever touched. +type tmuxRunFunc func(ctx context.Context, env []string, stdin, socket string, args ...string) ([]byte, error) + +// UserSessionRuntime starts, probes, types into and kills the tmux sessions +// behind the dashboard's Sessions feature. Each session gets its OWN tmux +// server (socket hive-sess-), its own HOME and a credential-stripped +// environment. It holds no per-session state: the dashboard's store is the +// source of truth for names, groups and ownership. +type UserSessionRuntime struct { + logger *slog.Logger + run tmuxRunFunc + lookPath func(string) (string, error) + environ func() []string + now func() time.Time +} + +// NewUserSessionRuntime returns a runtime that drives the real tmux binary. +func NewUserSessionRuntime(logger *slog.Logger) *UserSessionRuntime { + if logger == nil { + logger = slog.Default() + } + return &UserSessionRuntime{ + logger: logger, + run: execUserSessionTmux, + lookPath: exec.LookPath, + environ: os.Environ, + now: time.Now, + } +} + +func execUserSessionTmux(ctx context.Context, env []string, stdin, socket string, args ...string) ([]byte, error) { + argv := append([]string{"-L", socket}, args...) + cmd := exec.CommandContext(ctx, tmux.Binary, argv...) + cmd.Env = env + cmd.WaitDelay = tmuxCmdWaitDelay + if stdin != "" { + cmd.Stdin = strings.NewReader(stdin) + } + out, err := cmd.CombinedOutput() + if err != nil { + return out, outputErr("tmux "+args[0], err, out) + } + return out, nil +} + +// UserSessionTmuxName is the tmux session name for a session id. +func UserSessionTmuxName(id string) string { return UserSessionTmuxPrefix + id } + +func (r *UserSessionRuntime) tmuxEnv() []string { + return userSessionBaseEnv(r.environ(), os.Getenv(userSessionEnvPassthroughEnv)) +} + +func (r *UserSessionRuntime) tmux(id, stdin string, args ...string) ([]byte, error) { + ctx, cancel := context.WithTimeout(context.Background(), userSessionCmdTimeout) + defer cancel() + return r.run(ctx, r.tmuxEnv(), stdin, UserSessionTmuxName(id), args...) +} + +func checkUserSessionID(id string) error { + if !userSessionIDPattern.MatchString(id) { + return fmt.Errorf("invalid session id") + } + return nil +} + +// Vendors lists the vendors a session may run with binary presence. +func (r *UserSessionRuntime) Vendors() []crux.UserSessionVendor { + out := make([]crux.UserSessionVendor, 0, len(userSessionVendors)) + for _, v := range userSessionVendors { + _, err := r.lookPath(v.binary) + out = append(out, crux.UserSessionVendor{ID: v.id, Label: v.label, Available: err == nil}) + } + return out +} + +// SupportsResume reports whether the vendor keeps a capturable resume handle. +func (r *UserSessionRuntime) SupportsResume(vendor string) bool { + return BackendSupportsResumeHandle(vendor) +} + +// WorkDir is the session's working directory; the dashboard stores uploads +// beneath it. +func (r *UserSessionRuntime) WorkDir(id string) string { return userSessionWorkDir(id) } + +// Start creates the session's tmux server, strips and sets its environment and +// launches the vendor CLI as the pane's only process (there is no shell to +// fall back to when the CLI exits). +func (r *UserSessionRuntime) Start(spec crux.UserSessionSpec) error { + if err := checkUserSessionID(spec.ID); err != nil { + return err + } + argv, err := r.launchArgv(spec) + if err != nil { + return err + } + // An ended session's dead pane may still hold the tmux server open; a + // resume replaces it. + if p := r.Probe(spec.ID); p.Exists { + if err := r.Kill(spec.ID); err != nil { + return err + } + } + if err := setupUserSessionHome(spec.ID); err != nil { + return err + } + session := UserSessionTmuxName(spec.ID) + create := newSessionCommands(session, userSessionWorkDir(spec.ID)) + // Keep the pane (and its scrollback) after the CLI exits so a late viewer + // can still read what happened; Probe reports it as Dead. + create = append(create, ";", "set-option", "-w", "-t", session, "remain-on-exit", "on") + if out, err := r.tmux(spec.ID, "", create...); err != nil { + return outputErr("creating user session tmux server", err, out) + } + if _, err := r.tmux(spec.ID, "", r.environmentCommands(spec.ID, session)...); err != nil { + _ = r.Kill(spec.ID) + return fmt.Errorf("setting session environment: %w", err) + } + // respawn-pane -k replaces the pre-env shell with the CLI itself, forked + // after the environment was populated. + respawn := append([]string{"respawn-pane", "-k", "-t", session, "-c", userSessionWorkDir(spec.ID), "--"}, argv...) + if _, err := r.tmux(spec.ID, "", respawn...); err != nil { + _ = r.Kill(spec.ID) + return fmt.Errorf("launching %s: %w", spec.Vendor, err) + } + r.logger.Info("user session started", "id", spec.ID, "vendor", spec.Vendor) + return nil +} + +// environmentCommands removes every credential name from the session +// environment and then sets the per-session variables, as one tmux invocation. +func (r *UserSessionRuntime) environmentCommands(id, session string) []string { + var args []string + for _, k := range userSessionStripNames() { + args = append(args, "set-environment", "-t", session, "-r", k, ";") + } + for _, kv := range userSessionEnvPairs(id, r.scrubScript()) { + args = append(args, "set-environment", "-t", session, kv[0], kv[1], ";") + } + return args[:len(args)-1] +} + +func (r *UserSessionRuntime) scrubScript() string { + for _, p := range []string{"/usr/local/bin/agent-env-scrub.sh", filepath.Join(os.Getenv("HIVE_BIN_DIR"), "agent-env-scrub.sh")} { + if st, err := os.Stat(p); err == nil && !st.IsDir() { + return p + } + } + return "" +} + +// launchArgv resolves the pane command: the vendor binary alone, or the +// validated resume command for that vendor. Never a shell. +func (r *UserSessionRuntime) launchArgv(spec crux.UserSessionSpec) ([]string, error) { + v, ok := findUserSessionVendor(spec.Vendor) + if !ok { + return nil, fmt.Errorf("unsupported vendor %q", spec.Vendor) + } + bin, err := r.lookPath(v.binary) + if err != nil { + return nil, fmt.Errorf("%s is not installed on this spoke", v.binary) + } + if spec.ResumeCommand == "" { + return []string{bin}, nil + } + if !userSessionResumeCmdPattern.MatchString(spec.ResumeCommand) { + return nil, fmt.Errorf("resume command rejected") + } + fields := strings.Fields(spec.ResumeCommand) + if fields[0] != v.binary { + return nil, fmt.Errorf("resume command does not belong to %s", v.id) + } + return append([]string{bin}, fields[1:]...), nil +} + +// Probe reads the session's live tmux state in one call. +func (r *UserSessionRuntime) Probe(id string) crux.UserSessionProbe { + if checkUserSessionID(id) != nil { + return crux.UserSessionProbe{} + } + out, err := r.tmux(id, "", "display-message", "-p", "-t", UserSessionTmuxName(id), userSessionProbeFormat) + if err != nil { + return crux.UserSessionProbe{} + } + fields := strings.Fields(string(out)) + if len(fields) != userSessionProbeFields { + return crux.UserSessionProbe{Exists: true} + } + attached, _ := strconv.Atoi(fields[1]) + secs, _ := strconv.ParseInt(fields[2], 10, 64) + return crux.UserSessionProbe{ + Exists: true, + Dead: fields[0] == "1", + Attached: attached, + Activity: time.Unix(secs, 0), + } +} + +// Kill ends the session's tmux server. A missing session is not an error. +func (r *UserSessionRuntime) Kill(id string) error { + if err := checkUserSessionID(id); err != nil { + return err + } + if !r.Probe(id).Exists { + return nil + } + if out, err := r.tmux(id, "", "kill-session", "-t", UserSessionTmuxName(id)); err != nil { + return outputErr("killing user session", err, out) + } + return nil +} + +// Capture returns up to lines lines of the session's scrollback as text. +func (r *UserSessionRuntime) Capture(id string, lines int) (string, error) { + if err := checkUserSessionID(id); err != nil { + return "", err + } + if lines <= 0 || lines > userSessionMaxCaptureLines { + lines = userSessionMaxCaptureLines + } + out, err := r.tmux(id, "", "capture-pane", "-t", UserSessionTmuxName(id), "-p", "-J", + "-S", "-"+strconv.Itoa(lines), "-E", "-") + if err != nil { + return "", fmt.Errorf("capturing session: %w", err) + } + return string(out), nil +} + +// SendText types text into the session. It travels through a tmux paste +// buffer (bracketed paste when the CLI asked for it), so nothing is +// interpreted as a key name and a multi-line paste does not execute line by +// line. When submit is true a single Enter follows. The text is sanitised +// first: control and escape characters are refused, because an ESC could close +// the bracketed paste early and turn data into keystrokes. +func (r *UserSessionRuntime) SendText(id, text string, submit bool) error { + if err := checkUserSessionID(id); err != nil { + return err + } + clean, err := SanitizeSessionInput(text) + if err != nil { + return err + } + session := UserSessionTmuxName(id) + if p := r.Probe(id); !p.Exists || p.Dead { + return fmt.Errorf("session is not running") + } + if clean != "" { + if out, err := r.tmux(id, clean, "load-buffer", "-b", userSessionBufferName, "-"); err != nil { + return outputErr("loading input", err, out) + } + if out, err := r.tmux(id, "", "paste-buffer", "-p", "-r", "-d", "-b", userSessionBufferName, "-t", session); err != nil { + return outputErr("pasting input", err, out) + } + } + if submit { + if out, err := r.tmux(id, "", "send-keys", "-t", session, "Enter"); err != nil { + return outputErr("submitting input", err, out) + } + } + return nil +} + +// SanitizeSessionInput validates text for typing into a session: it must be +// valid UTF-8 and contain no control characters other than tab and newline +// (CRLF is folded to LF). It returns the text to paste. +func SanitizeSessionInput(text string) (string, error) { + if !utf8.ValidString(text) { + return "", fmt.Errorf("input is not valid UTF-8") + } + text = strings.ReplaceAll(text, "\r\n", "\n") + var b bytes.Buffer + for _, r := range text { + if r == '\n' || r == '\t' { + b.WriteRune(r) + continue + } + if unicode.IsControl(r) { + return "", fmt.Errorf("input contains a control character") + } + b.WriteRune(r) + } + return b.String(), nil +} + +// ResumeInfo reads the newest vendor-native conversation from the session's +// own HOME. +func (r *UserSessionRuntime) ResumeInfo(id, vendor string) (crux.UserSessionResume, bool) { + if checkUserSessionID(id) != nil || !r.SupportsResume(vendor) { + return crux.UserSessionResume{}, false + } + h, ok := resumehandle.CaptureResumeHandleIn(userSessionHome(id), userSessionCodexHome(id), vendor, userSessionWorkDir(id), r.now()) + if !ok || h.SessionID == "" || h.Command == "" { + return crux.UserSessionResume{}, false + } + return crux.UserSessionResume{SessionID: h.SessionID, Command: h.Command}, true +} diff --git a/src/crux/agent/usersession_env.go b/src/crux/agent/usersession_env.go new file mode 100644 index 0000000000..0a0000ad99 --- /dev/null +++ b/src/crux/agent/usersession_env.go @@ -0,0 +1,139 @@ +package agent + +import ( + "sort" + "strings" +) + +// Session environment policy. A user session is a terminal handed to a person +// through the dashboard, so its environment is built from an ALLOWLIST of the +// hive process env rather than by subtracting known secrets: a credential the +// hive later learns to hold is excluded by default instead of leaking until +// someone remembers to add it to a strip list. +// +// The allowlist covers only what a CLI needs to run and reach the network. +// Vendor API keys are NOT inherited; an operator who wants one in sessions +// names it in HIVE_SESSIONS_ENV_PASSTHROUGH, and even then every name that is +// (or looks like) a hive, dashboard, hub, terminal or GitHub credential is +// refused (sessionEnvForbidden). +const ( + // userSessionEnvPassthroughEnv lists extra env var names (comma separated) + // copied from the hive process into sessions. + userSessionEnvPassthroughEnv = "HIVE_SESSIONS_ENV_PASSTHROUGH" + // userSessionDefaultTerm is the TERM sessions run with; ttyd/xterm.js + // speaks xterm-256color. + userSessionDefaultTerm = "xterm-256color" +) + +// userSessionEnvAllow is the set of hive-process variables copied verbatim. +var userSessionEnvAllow = map[string]bool{ + "PATH": true, + "LANG": true, + "LC_ALL": true, + "LC_CTYPE": true, + "TZ": true, + "TMUX_TMPDIR": true, + "SSL_CERT_FILE": true, + "SSL_CERT_DIR": true, + "NODE_EXTRA_CA_CERTS": true, + "REQUESTS_CA_BUNDLE": true, + "CURL_CA_BUNDLE": true, +} + +// userSessionProxyVars are copied only when their value carries no userinfo +// (a proxy URL's user:password is a credential). +var userSessionProxyVars = map[string]bool{ + "HTTP_PROXY": true, "HTTPS_PROXY": true, "NO_PROXY": true, "ALL_PROXY": true, + "http_proxy": true, "https_proxy": true, "no_proxy": true, "all_proxy": true, +} + +// sessionEnvForbiddenNames are refused even through the operator passthrough. +func sessionEnvForbiddenNames() map[string]bool { + out := map[string]bool{ + "DASHBOARD_AUTH_TOKEN": true, + "HIVE_DASHBOARD_TOKEN": true, + } + for _, k := range inheritedCredentialEnvVars { + out[k] = true + } + for _, k := range githubTokenEnvVars { + out[k] = true + } + return out +} + +// sessionEnvForbidden reports whether name must never reach a session: a +// known hive credential, anything in the HIVE_ namespace (hub, heartbeat, +// terminal and ttyd secrets live there), or a GitHub token spelling. +func sessionEnvForbidden(name string) bool { + if sessionEnvForbiddenNames()[name] { + return true + } + upper := strings.ToUpper(name) + return strings.HasPrefix(upper, "HIVE_") || + strings.HasPrefix(upper, "GITHUB_") || + strings.HasPrefix(upper, "GH_") || + strings.Contains(upper, "GITHUB_TOKEN") +} + +// userSessionStripNames is the sorted list handed to `tmux set-environment -r` +// as a second line of defence: even if the tmux server somehow inherited one, +// the removal hides it from every process the session forks. +func userSessionStripNames() []string { + names := sessionEnvForbiddenNames() + out := make([]string, 0, len(names)) + for k := range names { + out = append(out, k) + } + sort.Strings(out) + return out +} + +// userSessionBaseEnv builds the environment a session's tmux server starts +// with from the hive process environ (KEY=VALUE entries) and the operator's +// passthrough list. +func userSessionBaseEnv(environ []string, passthrough string) []string { + extra := map[string]bool{} + for _, n := range strings.Split(passthrough, ",") { + n = strings.TrimSpace(n) + if n != "" && !sessionEnvForbidden(n) { + extra[n] = true + } + } + var out []string + for _, kv := range environ { + k, v, ok := strings.Cut(kv, "=") + if !ok || k == "" { + continue + } + switch { + case sessionEnvForbidden(k): + continue + case userSessionEnvAllow[k], extra[k]: + out = append(out, k+"="+v) + case userSessionProxyVars[k] && proxyValueHasNoUserinfo(v): + out = append(out, k+"="+v) + } + } + out = append(out, "TERM="+userSessionDefaultTerm) + return out +} + +// proxyValueHasNoUserinfo reports whether a proxy setting is safe to copy. +// NO_PROXY style lists have no "@" and pass. Anything else is judged on its +// authority component (after an optional scheme, up to the first path, query +// or fragment delimiter) so a schemeless "user:pw@host:port" is caught too; +// url.Parse would read "user" as a scheme and miss the credentials. +func proxyValueHasNoUserinfo(v string) bool { + if !strings.Contains(v, "@") { + return true + } + rest := v + if i := strings.Index(rest, "://"); i >= 0 { + rest = rest[i+len("://"):] + } + if end := strings.IndexAny(rest, "/?#"); end >= 0 { + rest = rest[:end] + } + return !strings.Contains(rest, "@") +} diff --git a/src/crux/agent/usersession_home.go b/src/crux/agent/usersession_home.go new file mode 100644 index 0000000000..d0c8d10e8b --- /dev/null +++ b/src/crux/agent/usersession_home.go @@ -0,0 +1,116 @@ +package agent + +import ( + "fmt" + "os" + "path/filepath" +) + +// Per-session filesystem layout. A session never shares a HOME with another +// session or with an agent, so a vendor CLI's transcripts, caches and memory +// stay per-session. Vendor LOGINS are shared fleet-wide by bridging only the +// credential files a vendor CLI needs (the perAgentCLIHomes bridge lists the +// fleet already trusts) from the shared home, never the whole shared +// ~/.config (which holds gh's hosts.yml). +const ( + userSessionHomeDirName = "sessions" + userSessionHomeMode = 0o700 + userSessionWorkDirName = "work" + // userSessionsDirEnv relocates the sessions data root (default + // /data/sessions), shared with the dashboard store. + userSessionsDirEnv = "HIVE_SESSIONS_DIR" + defaultUserSessionsDir = "/data/sessions" +) + +// sharedConfigBridges are the ~/.config subtrees bridged from the shared home +// because a vendor stores its provider login there. gh is deliberately absent. +var sharedConfigBridges = []string{"goose", "opencode", "aider"} + +// userSessionsDir is the data root holding sessions.json and per-session work +// dirs. +func userSessionsDir() string { + if v := os.Getenv(userSessionsDirEnv); v != "" { + return v + } + return defaultUserSessionsDir +} + +func userSessionHome(id string) string { + return filepath.Join(sharedAgentHome, userSessionHomeDirName, id) +} + +func userSessionCodexHome(id string) string { + return filepath.Join(userSessionHome(id), ".codex") +} + +func userSessionWorkDir(id string) string { + return filepath.Join(userSessionsDir(), id, userSessionWorkDirName) +} + +// setupUserSessionHome creates the per-session HOME, XDG dirs, CODEX_HOME and +// work dir and bridges the shared vendor login files into it. +func setupUserSessionHome(id string) error { + home := userSessionHome(id) + dirs := []string{ + home, + filepath.Join(home, ".config"), + agentXDGDataHome(home), + agentXDGStateHome(home), + userSessionCodexHome(id), + userSessionWorkDir(id), + } + for _, d := range dirs { + if err := os.MkdirAll(d, userSessionHomeMode); err != nil { + return fmt.Errorf("creating session dir %s: %w", d, err) + } + } + for _, spec := range perAgentCLIHomes { + dir := filepath.Join(home, spec.dir) + if err := os.MkdirAll(dir, userSessionHomeMode); err != nil { + return fmt.Errorf("creating %s: %w", dir, err) + } + for _, name := range spec.bridges { + bridgeSessionEntry(filepath.Join(dir, name), filepath.Join(sharedAgentHome, spec.dir, name)) + } + } + for _, name := range sharedConfigBridges { + bridgeSessionEntry(filepath.Join(home, ".config", name), filepath.Join(sharedAgentHome, ".config", name)) + } + // codex keeps its login in CODEX_HOME/auth.json. + bridgeSessionEntry(filepath.Join(userSessionCodexHome(id), "auth.json"), filepath.Join(sharedAgentHome, ".codex", "auth.json")) + return nil +} + +// bridgeSessionEntry links link -> target when the shared target exists and +// link is absent. A missing target is skipped (the vendor was never signed in +// fleet-wide); an existing link is left alone so a re-launch keeps whatever the +// CLI wrote. +func bridgeSessionEntry(link, target string) { + if _, err := os.Stat(target); err != nil { + return + } + if _, err := os.Lstat(link); err == nil { + return + } + _ = os.Symlink(target, link) +} + +// userSessionEnvPairs are the per-session variables set on the tmux session +// after the inherited credentials were stripped. +func userSessionEnvPairs(id, scrubScript string) [][2]string { + home := userSessionHome(id) + pairs := [][2]string{ + {"HOME", home}, + {"CODEX_HOME", userSessionCodexHome(id)}, + {"XDG_CONFIG_HOME", filepath.Join(home, ".config")}, + {"XDG_DATA_HOME", agentXDGDataHome(home)}, + {"XDG_STATE_HOME", agentXDGStateHome(home)}, + {"HIVE_SESSION_ID", id}, + } + if scrubScript != "" { + // Sourced by every non-interactive bash the CLI spawns; unsets any + // GitHub token the CLI re-exports into its tool shells (#4045). + pairs = append(pairs, [2]string{"BASH_ENV", scrubScript}, [2]string{"ENV", scrubScript}) + } + return pairs +} diff --git a/src/crux/agent/usersession_more_test.go b/src/crux/agent/usersession_more_test.go new file mode 100644 index 0000000000..611361e3c5 --- /dev/null +++ b/src/crux/agent/usersession_more_test.go @@ -0,0 +1,509 @@ +package agent + +import ( + "context" + "errors" + "log/slog" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/hivecommons/hive/crux" +) + +const ( + testResumeUUID = "123e4567-e89b-12d3-a456-426614174000" + testFakeTmuxScript = "#!/bin/sh\necho \"args=$*\"\ncat\ncase \"$3\" in fail) echo boom; exit 3;; esac\n" + testScrubScript = "agent-env-scrub.sh" +) + +// sessionLifecycleTmux fakes a tmux server that only "exists" once new-session +// ran, and fails any call whose argv contains failOn. +func sessionLifecycleTmux(failOn string) *fakeTmux { + created := false + f := &fakeTmux{} + f.handler = func(args []string) ([]byte, error) { + joined := strings.Join(args, " ") + if failOn != "" && strings.Contains(joined, failOn) { + return []byte("simulated failure"), errors.New("tmux failed") + } + switch { + case args[0] == "display-message": + if !created { + return nil, errors.New("no session") + } + return []byte("0 0 1\n"), nil + case strings.Contains(joined, "new-session"): + created = true + } + return nil, nil + } + return f +} + +func TestUserSessionStartStepFailuresAreReportedAndCleanedUp(t *testing.T) { + cases := []struct { + name, failOn, wantErr string + wantKill bool + }{ + {"create", "remain-on-exit", "creating user session tmux server", false}, + {"environment", "set-environment", "setting session environment", true}, + {"respawn", "respawn-pane", "launching claude", true}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + f := sessionLifecycleTmux(tc.failOn) + r := newTestSessionRuntime(t, f, nil) + err := r.Start(crux.UserSessionSpec{ID: testSessionID, Vendor: "claude"}) + if err == nil || !strings.Contains(err.Error(), tc.wantErr) { + t.Fatalf("Start error = %v, want it to mention %q", err, tc.wantErr) + } + killed := false + for _, v := range f.verbs() { + if v == "kill-session" { + killed = true + } + } + if killed != tc.wantKill { + t.Errorf("kill-session issued = %v, want %v (calls %v)", killed, tc.wantKill, f.verbs()) + } + }) + } +} + +func TestUserSessionStartReplacesExistingSession(t *testing.T) { + f := &fakeTmux{handler: func(args []string) ([]byte, error) { + if args[0] == "display-message" { + return []byte("1 0 1\n"), nil + } + return nil, nil + }} + r := newTestSessionRuntime(t, f, nil) + if err := r.Start(crux.UserSessionSpec{ID: testSessionID, Vendor: "claude"}); err != nil { + t.Fatal(err) + } + verbs := f.verbs() + killAt, respawnAt := -1, -1 + for i, v := range verbs { + switch v { + case "kill-session": + killAt = i + case "respawn-pane": + respawnAt = i + } + } + if killAt < 0 || respawnAt < 0 || killAt > respawnAt { + t.Errorf("dead session was not killed before relaunch: %v", verbs) + } +} + +func TestUserSessionStartSurfacesKillFailure(t *testing.T) { + f := &fakeTmux{handler: func(args []string) ([]byte, error) { + switch args[0] { + case "display-message": + return []byte("1 0 1\n"), nil + case "kill-session": + return []byte("server wedged"), errors.New("exit 1") + } + return nil, nil + }} + r := newTestSessionRuntime(t, f, nil) + err := r.Start(crux.UserSessionSpec{ID: testSessionID, Vendor: "claude"}) + if err == nil || !strings.Contains(err.Error(), "killing user session") || !strings.Contains(err.Error(), "server wedged") { + t.Fatalf("Start error = %v, want kill failure with tmux output", err) + } + for _, v := range f.verbs() { + if v == "respawn-pane" { + t.Error("CLI launched despite the old session not being killed") + } + } +} + +func TestUserSessionStartFailsWhenHomeCannotBeCreated(t *testing.T) { + f := sessionLifecycleTmux("") + r := newTestSessionRuntime(t, f, nil) + blocker := filepath.Join(t.TempDir(), "blocker") + if err := os.WriteFile(blocker, []byte("x"), 0o600); err != nil { + t.Fatal(err) + } + sharedAgentHome = filepath.Join(blocker, "home") + err := r.Start(crux.UserSessionSpec{ID: testSessionID, Vendor: "claude"}) + if err == nil || !strings.Contains(err.Error(), "creating session dir") { + t.Fatalf("Start error = %v, want session dir failure", err) + } + for _, v := range f.verbs() { + if v == "respawn-pane" || v == "set-option" { + t.Errorf("tmux %s ran after home setup failed", v) + } + } +} + +func TestUserSessionStartRequiresInstalledBinary(t *testing.T) { + f := &fakeTmux{} + r := newTestSessionRuntime(t, f, nil) + r.lookPath = func(string) (string, error) { return "", errors.New("not found") } + err := r.Start(crux.UserSessionSpec{ID: testSessionID, Vendor: "claude"}) + if err == nil || !strings.Contains(err.Error(), "claude is not installed") { + t.Fatalf("Start error = %v", err) + } + if len(f.calls) != 0 { + t.Errorf("tmux reached for a missing binary: %v", f.verbs()) + } +} + +func TestUserSessionLaunchArgv(t *testing.T) { + r := newTestSessionRuntime(t, &fakeTmux{}, nil) + cases := []struct { + name string + spec crux.UserSessionSpec + want []string + wantErr string + }{ + {"bare", crux.UserSessionSpec{Vendor: "codex"}, []string{"/usr/bin/codex"}, ""}, + {"resume", crux.UserSessionSpec{Vendor: "codex", ResumeCommand: "codex resume abc-1"}, []string{"/usr/bin/codex", "resume", "abc-1"}, ""}, + {"unknown vendor", crux.UserSessionSpec{Vendor: "nope"}, nil, `unsupported vendor "nope"`}, + {"metacharacters", crux.UserSessionSpec{Vendor: "claude", ResumeCommand: "claude --resume a|b"}, nil, "resume command rejected"}, + {"leading space", crux.UserSessionSpec{Vendor: "claude", ResumeCommand: " claude --resume a"}, nil, "resume command rejected"}, + {"wrong binary", crux.UserSessionSpec{Vendor: "claude", ResumeCommand: "copilot --resume a"}, nil, "does not belong to claude"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + got, err := r.launchArgv(tc.spec) + if tc.wantErr != "" { + if err == nil || !strings.Contains(err.Error(), tc.wantErr) { + t.Fatalf("err = %v, want %q", err, tc.wantErr) + } + return + } + if err != nil || strings.Join(got, " ") != strings.Join(tc.want, " ") { + t.Fatalf("argv = %v, %v; want %v", got, err, tc.want) + } + }) + } +} + +func TestUserSessionEnvironmentCommandsStripThenSet(t *testing.T) { + r := newTestSessionRuntime(t, &fakeTmux{}, nil) + session := UserSessionTmuxName(testSessionID) + args := r.environmentCommands(testSessionID, session) + if len(args) == 0 || args[len(args)-1] == ";" { + t.Fatalf("command list must not end with a separator: tail %v", args[len(args)-2:]) + } + joined := strings.Join(args, " ") + lastStrip := strings.LastIndex(joined, "-r ") + firstSet := strings.Index(joined, "set-environment -t "+session+" HOME ") + if firstSet < 0 || lastStrip > firstSet { + t.Errorf("credentials must be removed before per-session vars are set:\n%s", joined) + } + for _, name := range userSessionStripNames() { + if !strings.Contains(joined, "-r "+name+" ;") { + t.Errorf("strip of %s missing", name) + } + } + if !strings.Contains(joined, "HIVE_SESSION_ID "+testSessionID) { + t.Error("HIVE_SESSION_ID not set") + } +} + +func TestUserSessionScrubScriptDiscovery(t *testing.T) { + if _, err := os.Stat("/usr/local/bin/" + testScrubScript); err == nil { + t.Skip("host has a system-wide scrub script") + } + r := newTestSessionRuntime(t, &fakeTmux{}, nil) + binDir := t.TempDir() + t.Setenv("HIVE_BIN_DIR", binDir) + if got := r.scrubScript(); got != "" { + t.Fatalf("scrubScript() = %q with no script present", got) + } + // A directory of that name is not a script. + if err := os.Mkdir(filepath.Join(binDir, testScrubScript), 0o755); err != nil { + t.Fatal(err) + } + if got := r.scrubScript(); got != "" { + t.Fatalf("scrubScript() accepted a directory: %q", got) + } + if err := os.Remove(filepath.Join(binDir, testScrubScript)); err != nil { + t.Fatal(err) + } + script := filepath.Join(binDir, testScrubScript) + if err := os.WriteFile(script, []byte("#!/bin/sh\n"), 0o755); err != nil { + t.Fatal(err) + } + if got := r.scrubScript(); got != script { + t.Fatalf("scrubScript() = %q, want %q", got, script) + } + joined := strings.Join(r.environmentCommands(testSessionID, UserSessionTmuxName(testSessionID)), " ") + if !strings.Contains(joined, "BASH_ENV "+script) || !strings.Contains(joined, "ENV "+script) { + t.Errorf("scrub script not wired into BASH_ENV/ENV: %s", joined) + } +} + +func TestUserSessionEnvPairs(t *testing.T) { + newTestSessionRuntime(t, &fakeTmux{}, nil) + toMap := func(pairs [][2]string) map[string]string { + m := map[string]string{} + for _, p := range pairs { + m[p[0]] = p[1] + } + return m + } + plain := toMap(userSessionEnvPairs(testSessionID, "")) + if plain["HOME"] != userSessionHome(testSessionID) || plain["CODEX_HOME"] != userSessionCodexHome(testSessionID) || + plain["XDG_CONFIG_HOME"] != filepath.Join(userSessionHome(testSessionID), ".config") || + plain["HIVE_SESSION_ID"] != testSessionID { + t.Errorf("pairs = %v", plain) + } + if _, ok := plain["BASH_ENV"]; ok { + t.Error("BASH_ENV set without a scrub script") + } + scrubbed := toMap(userSessionEnvPairs(testSessionID, "/opt/scrub.sh")) + if scrubbed["BASH_ENV"] != "/opt/scrub.sh" || scrubbed["ENV"] != "/opt/scrub.sh" { + t.Errorf("scrub pairs = %v", scrubbed) + } +} + +func TestUserSessionPathHelpers(t *testing.T) { + newTestSessionRuntime(t, &fakeTmux{}, nil) + t.Setenv(userSessionsDirEnv, "") + if got := userSessionsDir(); got != defaultUserSessionsDir { + t.Errorf("default sessions dir = %q", got) + } + root := t.TempDir() + t.Setenv(userSessionsDirEnv, root) + if got := userSessionsDir(); got != root { + t.Errorf("sessions dir override = %q", got) + } + r := &UserSessionRuntime{} + want := filepath.Join(root, testSessionID, userSessionWorkDirName) + if got := r.WorkDir(testSessionID); got != want { + t.Errorf("WorkDir = %q, want %q", got, want) + } + if got := userSessionCodexHome(testSessionID); got != filepath.Join(userSessionHome(testSessionID), ".codex") { + t.Errorf("codex home = %q", got) + } + if !strings.HasPrefix(userSessionHome(testSessionID), sharedAgentHome) { + t.Errorf("session home %q outside shared home", userSessionHome(testSessionID)) + } +} + +func TestCheckUserSessionID(t *testing.T) { + for _, id := range []string{testSessionID, strings.Repeat("a", 8), strings.Repeat("f", 32)} { + if err := checkUserSessionID(id); err != nil { + t.Errorf("checkUserSessionID(%q) = %v", id, err) + } + } + for _, id := range []string{"", "abc", strings.Repeat("a", 33), "ABCDEF0123", "0123456789ab/..", "0123456789zz", "0123456789ab\n"} { + if err := checkUserSessionID(id); err == nil { + t.Errorf("checkUserSessionID(%q) accepted", id) + } + } +} + +func TestUserSessionSupportsResume(t *testing.T) { + r := &UserSessionRuntime{} + if !r.SupportsResume("claude") || !r.SupportsResume("codex") { + t.Error("claude and codex keep resume handles") + } + if r.SupportsResume("") || r.SupportsResume("bash") { + t.Error("unknown vendors must not report resume support") + } +} + +func TestUserSessionResumeInfo(t *testing.T) { + r := newTestSessionRuntime(t, &fakeTmux{}, nil) + if _, ok := r.ResumeInfo("../x", "claude"); ok { + t.Error("invalid id produced resume info") + } + if _, ok := r.ResumeInfo(testSessionID, "bash"); ok { + t.Error("unsupported vendor produced resume info") + } + if _, ok := r.ResumeInfo(testSessionID, "claude"); ok { + t.Error("session without transcripts produced resume info") + } + dir := filepath.Join(userSessionHome(testSessionID), ".claude", "projects", "proj") + if err := os.MkdirAll(dir, 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, testResumeUUID+".jsonl"), []byte("{}\n"), 0o600); err != nil { + t.Fatal(err) + } + got, ok := r.ResumeInfo(testSessionID, "claude") + if !ok || got.SessionID != testResumeUUID || got.Command != "claude --resume "+testResumeUUID { + t.Fatalf("ResumeInfo = %+v, %v", got, ok) + } + // Another session's transcripts are invisible: homes are per session. + if _, ok := r.ResumeInfo("fedcba987654", "claude"); ok { + t.Error("resume info leaked across session homes") + } +} + +func TestUserSessionBaseEnvProxyAndMalformedEntries(t *testing.T) { + env := userSessionBaseEnv([]string{ + "PATH=/usr/bin", + "NOEQUALS", + "=emptykey", + "https_proxy=http://proxy.example:3128", + "HTTP_PROXY=http://user:pw@proxy.example:3128", + "NO_PROXY=localhost,10.0.0.0/8", + "CUSTOM_VENDOR_KEY=abc", + "LANG=C.UTF-8", + }, " ,CUSTOM_VENDOR_KEY, ") + joined := strings.Join(env, "\n") + for _, want := range []string{"PATH=/usr/bin", "https_proxy=http://proxy.example:3128", "NO_PROXY=localhost,10.0.0.0/8", "CUSTOM_VENDOR_KEY=abc", "LANG=C.UTF-8", "TERM=" + userSessionDefaultTerm} { + if !strings.Contains(joined, want) { + t.Errorf("env lacks %q:\n%s", want, joined) + } + } + for _, bad := range []string{"NOEQUALS", "emptykey", "pw@"} { + if strings.Contains(joined, bad) { + t.Errorf("env contains %q:\n%s", bad, joined) + } + } + if env[len(env)-1] != "TERM="+userSessionDefaultTerm { + t.Errorf("TERM must be last, got %v", env) + } +} + +func TestProxyValueHasNoUserinfo(t *testing.T) { + cases := map[string]bool{ + "": true, + "localhost,10.0.0.0/8": true, + "http://proxy.example:3128": true, + "http://user@proxy.example": false, + "http://user:pw@proxy.example": false, + "user:pw@proxy.example:3128": false, + "http://u@[::1": false, + "http://proxy.example/path?a@b": true, + } + for in, want := range cases { + if got := proxyValueHasNoUserinfo(in); got != want { + t.Errorf("proxyValueHasNoUserinfo(%q) = %v, want %v", in, got, want) + } + } +} + +func TestBridgeSessionEntry(t *testing.T) { + dir := t.TempDir() + target := filepath.Join(dir, "shared") + link := filepath.Join(dir, "link") + + bridgeSessionEntry(link, target) + if _, err := os.Lstat(link); err == nil { + t.Fatal("link created for a missing target") + } + if err := os.WriteFile(target, []byte("login"), 0o600); err != nil { + t.Fatal(err) + } + bridgeSessionEntry(link, target) + if got, err := os.Readlink(link); err != nil || got != target { + t.Fatalf("Readlink = %q, %v", got, err) + } + // An existing entry is left alone, even when it points elsewhere. + other := filepath.Join(dir, "other") + if err := os.WriteFile(other, []byte("x"), 0o600); err != nil { + t.Fatal(err) + } + if err := os.Remove(link); err != nil { + t.Fatal(err) + } + if err := os.Symlink(other, link); err != nil { + t.Fatal(err) + } + bridgeSessionEntry(link, target) + if got, _ := os.Readlink(link); got != other { + t.Errorf("existing link overwritten: %q", got) + } +} + +func TestSetupUserSessionHomeLayoutAndIdempotence(t *testing.T) { + newTestSessionRuntime(t, &fakeTmux{}, nil) + shared := filepath.Join(sharedAgentHome, ".config", "goose") + if err := os.MkdirAll(shared, 0o755); err != nil { + t.Fatal(err) + } + for i := 0; i < 2; i++ { + if err := setupUserSessionHome(testSessionID); err != nil { + t.Fatalf("setup #%d: %v", i, err) + } + } + home := userSessionHome(testSessionID) + for _, d := range []string{home, filepath.Join(home, ".config"), agentXDGDataHome(home), agentXDGStateHome(home), userSessionCodexHome(testSessionID), userSessionWorkDir(testSessionID)} { + if st, err := os.Stat(d); err != nil || !st.IsDir() { + t.Errorf("%s not created: %v", d, err) + } + } + if got, err := os.Readlink(filepath.Join(home, ".config", "goose")); err != nil || got != shared { + t.Errorf("goose bridge = %q, %v", got, err) + } + if _, err := os.Lstat(filepath.Join(home, ".config", "opencode")); err == nil { + t.Error("bridge created for a vendor that was never signed in") + } +} + +func TestSetupUserSessionHomeFailsWhenVendorDirBlocked(t *testing.T) { + newTestSessionRuntime(t, &fakeTmux{}, nil) + home := userSessionHome(testSessionID) + if err := os.MkdirAll(home, 0o755); err != nil { + t.Fatal(err) + } + blocked := filepath.Join(home, perAgentCLIHomes[0].dir) + if err := os.WriteFile(blocked, []byte("x"), 0o600); err != nil { + t.Fatal(err) + } + err := setupUserSessionHome(testSessionID) + if err == nil || !strings.Contains(err.Error(), blocked) { + t.Fatalf("setup error = %v, want failure naming %s", err, blocked) + } +} + +func TestNewUserSessionRuntimeWiresRealDependencies(t *testing.T) { + r := NewUserSessionRuntime(nil) + if r.logger == nil || r.run == nil || r.lookPath == nil || r.environ == nil || r.now == nil { + t.Fatalf("runtime not fully wired: %+v", r) + } + logger := slog.Default() + if got := NewUserSessionRuntime(logger); got.logger != logger { + t.Error("supplied logger not used") + } + t.Setenv("HIVE_TEST_SESSION_ENVIRON_MARKER", "1") + found := false + for _, kv := range r.environ() { + if kv == "HIVE_TEST_SESSION_ENVIRON_MARKER=1" { + found = true + } + } + if !found { + t.Error("environ is not the process environment") + } +} + +func TestExecUserSessionTmux(t *testing.T) { + binDir := t.TempDir() + if err := os.WriteFile(filepath.Join(binDir, "tmux"), []byte(testFakeTmuxScript), 0o755); err != nil { + t.Fatal(err) + } + t.Setenv("PATH", binDir+":/bin:/usr/bin") + env := []string{"PATH=/bin:/usr/bin"} + + out, err := execUserSessionTmux(context.Background(), env, "typed text", "hive-sess-x", "load-buffer", "-") + if err != nil { + t.Fatalf("err = %v, out %q", err, out) + } + if !strings.Contains(string(out), "args=-L hive-sess-x load-buffer -") || !strings.Contains(string(out), "typed text") { + t.Errorf("socket flag or stdin not passed: %q", out) + } + + out, err = execUserSessionTmux(context.Background(), env, "", "hive-sess-x", "fail") + if err == nil || !strings.Contains(err.Error(), "tmux fail") || !strings.Contains(err.Error(), "boom") { + t.Fatalf("err = %v, want wrapped failure with tmux output", err) + } + if !strings.Contains(string(out), "boom") { + t.Errorf("output not returned on failure: %q", out) + } + + t.Setenv("PATH", t.TempDir()) + if _, err := execUserSessionTmux(context.Background(), env, "", "hive-sess-x", "display-message"); err == nil || !strings.Contains(err.Error(), "tmux display-message") { + t.Errorf("missing binary error = %v", err) + } +} diff --git a/src/crux/agent/usersession_test.go b/src/crux/agent/usersession_test.go new file mode 100644 index 0000000000..3d282a7c44 --- /dev/null +++ b/src/crux/agent/usersession_test.go @@ -0,0 +1,474 @@ +package agent + +import ( + "context" + "errors" + "io" + "log/slog" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/crux" +) + +const testSessionID = "0123456789ab" + +type fakeTmuxCall struct { + env []string + stdin string + socket string + args []string +} + +// fakeTmux is a deterministic stand-in for the tmux binary. handler may be +// nil (every call succeeds with empty output). +type fakeTmux struct { + calls []fakeTmuxCall + handler func(args []string) ([]byte, error) +} + +func (f *fakeTmux) run(_ context.Context, env []string, stdin, socket string, args ...string) ([]byte, error) { + f.calls = append(f.calls, fakeTmuxCall{env: env, stdin: stdin, socket: socket, args: args}) + if f.handler != nil { + return f.handler(args) + } + return nil, nil +} + +func (f *fakeTmux) verbs() []string { + var out []string + for _, c := range f.calls { + out = append(out, c.args[0]) + } + return out +} + +func newTestSessionRuntime(t *testing.T, f *fakeTmux, environ []string) *UserSessionRuntime { + t.Helper() + oldHome := sharedAgentHome + sharedAgentHome = filepath.Join(t.TempDir(), "home") + t.Cleanup(func() { sharedAgentHome = oldHome }) + t.Setenv(userSessionsDirEnv, filepath.Join(t.TempDir(), "sessions")) + t.Setenv(userSessionEnvPassthroughEnv, "") + return &UserSessionRuntime{ + logger: slog.New(slog.NewTextHandler(io.Discard, nil)), + run: f.run, + lookPath: func(name string) (string, error) { return "/usr/bin/" + name, nil }, + environ: func() []string { return environ }, + now: time.Now, + } +} + +var sessionSecretEnv = []string{ + "PATH=/usr/bin", + "DASHBOARD_AUTH_TOKEN=dash-secret", + "HIVE_DASHBOARD_TOKEN=dash-secret-2", + "HIVE_TERMINAL_KEY=term-secret", + "HIVE_HUB_SECRET=hub-secret", + "HIVE_HEARTBEAT_KEY=hb-secret", + "HIVE_TTYD_CREDENTIAL=ttyd-secret", + "HIVE_INVITE_KEY=invite-secret", + "HIVE_GITHUB_TOKEN=ghs_installtoken", + "GH_TOKEN=ghp_pat", + "GITHUB_TOKEN=ghs_other", + "GH_ENTERPRISE_TOKEN=ghe", + "ANTHROPIC_API_KEY=sk-ant-secret", + "HTTPS_PROXY=http://user:proxypass@proxy.example:3128", +} + +// TestUserSessionBaseEnvStripsEveryCredential asserts the invariant itself: +// whatever the hive process env holds, no session env entry carries a +// dashboard, terminal, hub, heartbeat, ttyd or GitHub credential, by name or by +// value. +func TestUserSessionBaseEnvStripsEveryCredential(t *testing.T) { + env := userSessionBaseEnv(sessionSecretEnv, "") + joined := strings.Join(env, "\n") + for _, secret := range []string{ + "dash-secret", "term-secret", "hub-secret", "hb-secret", "ttyd-secret", "invite-secret", + "ghs_installtoken", "ghp_pat", "ghs_other", "sk-ant-secret", "proxypass", + } { + if strings.Contains(joined, secret) { + t.Errorf("session env leaks %q:\n%s", secret, joined) + } + } + if !strings.Contains(joined, "PATH=/usr/bin") { + t.Errorf("session env lost PATH: %v", env) + } + if !strings.Contains(joined, "TERM="+userSessionDefaultTerm) { + t.Errorf("session env missing TERM: %v", env) + } +} + +func TestUserSessionBaseEnvPassthroughCannotReintroduceCredentials(t *testing.T) { + env := userSessionBaseEnv(sessionSecretEnv, "ANTHROPIC_API_KEY, HIVE_TERMINAL_KEY, GH_TOKEN, DASHBOARD_AUTH_TOKEN, HIVE_FUTURE_SECRET") + joined := strings.Join(env, "\n") + if !strings.Contains(joined, "ANTHROPIC_API_KEY=sk-ant-secret") { + t.Errorf("operator passthrough of a vendor key was dropped: %v", env) + } + for _, secret := range []string{"term-secret", "ghp_pat", "dash-secret"} { + if strings.Contains(joined, secret) { + t.Errorf("passthrough leaked %q", secret) + } + } +} + +func TestUserSessionForbiddenCoversEveryKnownCredentialVar(t *testing.T) { + strip := map[string]bool{} + for _, n := range userSessionStripNames() { + strip[n] = true + } + var all []string + all = append(all, inheritedCredentialEnvVars...) + all = append(all, githubTokenEnvVars...) + all = append(all, "DASHBOARD_AUTH_TOKEN", "HIVE_DASHBOARD_TOKEN", "HIVE_TERMINAL_KEY", "HIVE_HUB_SECRET", "HIVE_HEARTBEAT_KEY", "HIVE_TTYD_CREDENTIAL") + for _, name := range all { + if !sessionEnvForbidden(name) { + t.Errorf("%s is not forbidden in sessions", name) + } + if !strip[name] { + t.Errorf("%s is not in the set-environment -r strip list", name) + } + } +} + +func TestUserSessionStartLaunchesBareCLIWithoutBypassFlagsOrShell(t *testing.T) { + f := &fakeTmux{handler: func(args []string) ([]byte, error) { + if args[0] == "display-message" { + return []byte("can't find session"), errors.New("no session") + } + return nil, nil + }} + r := newTestSessionRuntime(t, f, sessionSecretEnv) + for _, v := range userSessionVendors { + f.calls = nil + if err := r.Start(crux.UserSessionSpec{ID: testSessionID, Vendor: v.id}); err != nil { + t.Fatalf("Start(%s): %v", v.id, err) + } + var respawn []string + var envCmd []string + for _, c := range f.calls { + if c.socket != UserSessionTmuxPrefix+testSessionID { + t.Fatalf("socket = %q, want per-session socket", c.socket) + } + joined := strings.Join(c.env, "\n") + for _, secret := range []string{"dash-secret", "term-secret", "hub-secret", "ghp_pat", "ghs_installtoken", "proxypass"} { + if strings.Contains(joined, secret) { + t.Fatalf("tmux client env leaks %q", secret) + } + } + if c.args[0] == "respawn-pane" { + respawn = c.args + } + for _, a := range c.args { + if a == "set-environment" { + envCmd = c.args + } + } + } + if respawn == nil { + t.Fatalf("%s: no respawn-pane call in %v", v.id, f.verbs()) + } + idx := -1 + for i, a := range respawn { + if a == "--" { + idx = i + } + } + if idx < 0 { + t.Fatalf("%s: respawn has no -- separator: %v", v.id, respawn) + } + argv := respawn[idx+1:] + if len(argv) != 1 || argv[0] != "/usr/bin/"+v.binary { + t.Errorf("%s: pane command = %v, want only the bare binary", v.id, argv) + } + for _, a := range respawn { + if strings.Contains(a, "allow-all") || strings.Contains(a, "bypass") || strings.Contains(a, "yolo") || + strings.Contains(a, "dangerously") || strings.Contains(a, "auto-approve") { + t.Errorf("%s: launch carries a permission-bypass flag: %v", v.id, respawn) + } + if a == "bash" || a == "sh" || a == "zsh" || strings.HasSuffix(a, "/bash") || strings.HasSuffix(a, "/sh") { + t.Errorf("%s: launch reaches a shell: %v", v.id, respawn) + } + } + if envCmd == nil { + t.Fatalf("%s: no set-environment call", v.id) + } + joined := strings.Join(envCmd, " ") + for _, name := range []string{"HIVE_TERMINAL_KEY", "DASHBOARD_AUTH_TOKEN", "GH_TOKEN", "HIVE_HUB_SECRET"} { + if !strings.Contains(joined, "-r "+name) { + t.Errorf("%s: session env does not remove %s", v.id, name) + } + } + if !strings.Contains(joined, "HOME "+userSessionHome(testSessionID)) { + t.Errorf("%s: session HOME not set to the per-session dir", v.id) + } + } + if _, err := os.Stat(userSessionWorkDir(testSessionID)); err != nil { + t.Errorf("work dir not created: %v", err) + } +} + +func TestUserSessionStartRejectsBadInput(t *testing.T) { + f := &fakeTmux{} + r := newTestSessionRuntime(t, f, nil) + cases := []crux.UserSessionSpec{ + {ID: "../etc", Vendor: "claude"}, + {ID: "XYZ", Vendor: "claude"}, + {ID: testSessionID, Vendor: "bash"}, + {ID: testSessionID, Vendor: "sh"}, + {ID: testSessionID, Vendor: "claude", ResumeCommand: "claude --resume abc; rm -rf /"}, + {ID: testSessionID, Vendor: "claude", ResumeCommand: "codex resume abc"}, + {ID: testSessionID, Vendor: "claude", ResumeCommand: "claude --resume $(id)"}, + } + for _, spec := range cases { + if err := r.Start(spec); err == nil { + t.Errorf("Start(%+v) succeeded, want error", spec) + } + } + if len(f.calls) != 0 { + t.Errorf("rejected starts reached tmux: %v", f.verbs()) + } +} + +func TestUserSessionStartResumeUsesValidatedCommand(t *testing.T) { + f := &fakeTmux{handler: func(args []string) ([]byte, error) { + if args[0] == "display-message" { + return nil, errors.New("no session") + } + return nil, nil + }} + r := newTestSessionRuntime(t, f, nil) + spec := crux.UserSessionSpec{ID: testSessionID, Vendor: "claude", ResumeCommand: "claude --resume 123e4567-e89b-12d3-a456-426614174000"} + if err := r.Start(spec); err != nil { + t.Fatal(err) + } + last := f.calls[len(f.calls)-1].args + joined := strings.Join(last, " ") + if !strings.HasSuffix(joined, "-- /usr/bin/claude --resume 123e4567-e89b-12d3-a456-426614174000") { + t.Errorf("resume launch = %q", joined) + } +} + +func TestUserSessionProbe(t *testing.T) { + f := &fakeTmux{handler: func(args []string) ([]byte, error) { + return []byte("0 2 1700000000\n"), nil + }} + r := newTestSessionRuntime(t, f, nil) + p := r.Probe(testSessionID) + if !p.Exists || p.Dead || p.Attached != 2 || p.Activity.Unix() != 1700000000 { + t.Errorf("probe = %+v", p) + } + f.handler = func(args []string) ([]byte, error) { return []byte("1 0 5\n"), nil } + if p := r.Probe(testSessionID); !p.Dead { + t.Errorf("dead pane not reported: %+v", p) + } + f.handler = func(args []string) ([]byte, error) { return nil, errors.New("can't find session") } + if p := r.Probe(testSessionID); p.Exists { + t.Errorf("missing session reported as existing: %+v", p) + } + if p := r.Probe("not-an-id"); p.Exists { + t.Errorf("invalid id probed as existing") + } +} + +func TestUserSessionSendTextUsesPasteBufferNotKeyNames(t *testing.T) { + f := &fakeTmux{handler: func(args []string) ([]byte, error) { + if args[0] == "display-message" { + return []byte("0 0 1700000000\n"), nil + } + return nil, nil + }} + r := newTestSessionRuntime(t, f, nil) + text := "Enter C-c\nline two; $(rm -rf /)" + if err := r.SendText(testSessionID, text, true); err != nil { + t.Fatal(err) + } + var load, paste, keys *fakeTmuxCall + for i := range f.calls { + switch f.calls[i].args[0] { + case "load-buffer": + load = &f.calls[i] + case "paste-buffer": + paste = &f.calls[i] + case "send-keys": + keys = &f.calls[i] + } + } + if load == nil || load.stdin != text { + t.Fatalf("text was not loaded verbatim via stdin: %+v", load) + } + if paste == nil || !strings.Contains(strings.Join(paste.args, " "), "-p") { + t.Fatalf("paste-buffer missing or not bracketed: %+v", paste) + } + for _, c := range f.calls { + if c.args[0] == "send-keys" && strings.Contains(strings.Join(c.args, " "), "C-c") { + t.Fatalf("user text reached send-keys as key names: %v", c.args) + } + } + if keys == nil || keys.args[len(keys.args)-1] != "Enter" { + t.Fatalf("submit did not press Enter: %+v", keys) + } + f.calls = nil + if err := r.SendText(testSessionID, "no submit", false); err != nil { + t.Fatal(err) + } + for _, v := range f.verbs() { + if v == "send-keys" { + t.Fatalf("Enter pressed without submit") + } + } +} + +func TestUserSessionSendTextRefusesDeadOrInvalid(t *testing.T) { + f := &fakeTmux{handler: func(args []string) ([]byte, error) { return []byte("1 0 1700000000\n"), nil }} + r := newTestSessionRuntime(t, f, nil) + if err := r.SendText(testSessionID, "hi", false); err == nil { + t.Error("typing into a dead session succeeded") + } + f.handler = func(args []string) ([]byte, error) { return []byte("0 0 1700000000\n"), nil } + f.calls = nil + if err := r.SendText(testSessionID, "bad \x1b[201~ escape", false); err == nil { + t.Error("escape sequence accepted") + } + for _, v := range f.verbs() { + if v == "load-buffer" || v == "paste-buffer" { + t.Errorf("rejected text reached tmux: %v", f.verbs()) + } + } +} + +func TestSanitizeSessionInput(t *testing.T) { + ok := map[string]string{ + "plain": "plain", + "tab\tsep": "tab\tsep", + "a\r\nb": "a\nb", + "unicode: hΓ©llo βœ“": "unicode: hΓ©llo βœ“", + } + for in, want := range ok { + got, err := SanitizeSessionInput(in) + if err != nil || got != want { + t.Errorf("Sanitize(%q) = %q, %v; want %q", in, got, err, want) + } + } + for _, in := range []string{"\x1b[31m", "nul\x00", "bell\a", "del\x7f", "c1\u0085", "bad\xff"} { + if _, err := SanitizeSessionInput(in); err == nil { + t.Errorf("Sanitize(%q) accepted", in) + } + } +} + +func TestUserSessionKillIsIdempotent(t *testing.T) { + f := &fakeTmux{handler: func(args []string) ([]byte, error) { return nil, errors.New("can't find session") }} + r := newTestSessionRuntime(t, f, nil) + if err := r.Kill(testSessionID); err != nil { + t.Errorf("kill of missing session = %v", err) + } + for _, v := range f.verbs() { + if v == "kill-session" { + t.Error("kill-session issued for a missing session") + } + } + f.handler = func(args []string) ([]byte, error) { return []byte("0 0 1\n"), nil } + f.calls = nil + if err := r.Kill(testSessionID); err != nil { + t.Fatal(err) + } + last := f.calls[len(f.calls)-1].args + if last[0] != "kill-session" || last[len(last)-1] != "hive-sess-"+testSessionID { + t.Errorf("kill args = %v", last) + } +} + +func TestUserSessionCaptureCapsLines(t *testing.T) { + f := &fakeTmux{handler: func(args []string) ([]byte, error) { return []byte("hello\n"), nil }} + r := newTestSessionRuntime(t, f, nil) + out, err := r.Capture(testSessionID, 0) + if err != nil || out != "hello\n" { + t.Fatalf("capture = %q, %v", out, err) + } + if !strings.Contains(strings.Join(f.calls[0].args, " "), "-S -50000") { + t.Errorf("uncapped capture args: %v", f.calls[0].args) + } + f.calls = nil + _, _ = r.Capture(testSessionID, 10_000_000) + if !strings.Contains(strings.Join(f.calls[0].args, " "), "-S -50000") { + t.Errorf("oversized capture not capped: %v", f.calls[0].args) + } + f.calls = nil + _, _ = r.Capture(testSessionID, 25) + if !strings.Contains(strings.Join(f.calls[0].args, " "), "-S -25 ") { + t.Errorf("requested line count not honoured: %v", f.calls[0].args) + } +} + +func TestUserSessionVendorsMatchKnownBackends(t *testing.T) { + const conf = "../../../config/backends.conf" + if _, err := os.Stat(conf); err != nil { + t.Skipf("backends.conf not reachable: %v", err) + } + out, err := exec.Command("bash", "-c", "source "+conf+" && printf '%s' \"$KNOWN_BACKENDS\"").Output() + if err != nil { + t.Fatalf("sourcing backends.conf: %v", err) + } + known := map[string]bool{} + for _, b := range strings.Fields(string(out)) { + known[b] = true + } + for _, v := range userSessionVendors { + if !known[v.id] { + t.Errorf("session vendor %q is not in KNOWN_BACKENDS", v.id) + } + if v.id == "bash" || v.id == "sh" || v.id == "shell" { + t.Errorf("shell vendor %q must never be offered", v.id) + } + } +} + +func TestUserSessionVendorsReportAvailability(t *testing.T) { + r := newTestSessionRuntime(t, &fakeTmux{}, nil) + r.lookPath = func(name string) (string, error) { + if name == "claude" { + return "/usr/bin/claude", nil + } + return "", errors.New("missing") + } + for _, v := range r.Vendors() { + if v.Available != (v.ID == "claude") { + t.Errorf("vendor %s available = %v", v.ID, v.Available) + } + } +} + +func TestUserSessionHomeBridgesVendorLoginsButNotGH(t *testing.T) { + newTestSessionRuntime(t, &fakeTmux{}, nil) + shared := sharedAgentHome + for _, p := range []string{ + filepath.Join(shared, ".claude", ".credentials.json"), + filepath.Join(shared, ".config", "gh", "hosts.yml"), + filepath.Join(shared, ".codex", "auth.json"), + } { + if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(p, []byte("x"), 0o600); err != nil { + t.Fatal(err) + } + } + if err := setupUserSessionHome(testSessionID); err != nil { + t.Fatal(err) + } + home := userSessionHome(testSessionID) + if _, err := os.Lstat(filepath.Join(home, ".claude", ".credentials.json")); err != nil { + t.Errorf("claude login not bridged: %v", err) + } + if _, err := os.Lstat(filepath.Join(userSessionCodexHome(testSessionID), "auth.json")); err != nil { + t.Errorf("codex login not bridged: %v", err) + } + if _, err := os.Lstat(filepath.Join(home, ".config", "gh")); err == nil { + t.Error("gh config (hosts.yml token) was bridged into a session") + } +} diff --git a/src/crux/proxy/github_proxy.go b/src/crux/proxy/github_proxy.go index c47abfedf8..008dfdd63c 100644 --- a/src/crux/proxy/github_proxy.go +++ b/src/crux/proxy/github_proxy.go @@ -84,8 +84,26 @@ type GitHubProxy struct { caCert tls.Certificate caX509 *x509.Certificate logger *slog.Logger - uidMap *agent.UIDMap - allowedRepos map[string]bool + // uidMap is read through loadedUIDMap and swapped only by refreshUIDMap + // under uidMapMu (#18184): agents added at runtime land in uid-map.json + // after construction. uidMapPath/uidMapInfo are what the refresh compares + // against; an empty path (no map at boot, or a test literal) never reloads. + uidMap *agent.UIDMap + uidMapMu sync.RWMutex + uidMapReloadMu sync.Mutex + uidMapPath string + uidMapInfo os.FileInfo + allowedRepos map[string]bool + // Reference repos (#18015): study-only repositories outside allowedRepos, + // read-only by default with a per-entry issues/prs opt-in. Reads to them + // are forwarded without the hive's credential; writes are gated by + // referenceRepoRefusal. referenceDefaults caches each repo's default + // branch for the push guard; referenceLookup is the GET those checks use + // (nil = defaultReferenceLookup; tests inject one). + referenceMu sync.RWMutex + referenceRepos map[string]ReferenceRepoPolicy + referenceDefaults map[string]referenceDefaultBranch + referenceLookup referenceLookupFunc // repoPaused reports whether a repo ("owner/repo", as ExtractRepo yields it) // is under an operator pause (#6203). Guarded because the dashboard can @@ -269,8 +287,14 @@ func NewGitHubProxyEphemeral(logger *slog.Logger, org string, repos []string) (* func newGitHubProxyWithCA(caCert tls.Certificate, caX509 *x509.Certificate, logger *slog.Logger, org string, repos []string) *GitHubProxy { var uidMap *agent.UIDMap + var uidMapPath string + var uidMapInfo os.FileInfo + // Stat before load: a write landing in between leaves an older stamp, so + // the next lookup miss reloads rather than missing the change. + statInfo, _ := os.Stat(agent.UIDMapPath) if loaded, loadErr := agent.LoadUIDMap(agent.UIDMapPath); loadErr == nil { uidMap = loaded + uidMapPath, uidMapInfo = agent.UIDMapPath, statInfo logger.Info("proxy loaded UID map", "agents", len(uidMap.Agents), "iptables", uidMap.IptablesActive) } @@ -313,6 +337,8 @@ func newGitHubProxyWithCA(caCert tls.Certificate, caX509 *x509.Certificate, logg caX509: caX509, logger: logger, uidMap: uidMap, + uidMapPath: uidMapPath, + uidMapInfo: uidMapInfo, proxyAdvisoryOK: advisoryOK, allowPrivateForward: allowPrivateForward, injectGHAuth: injectGHAuth, diff --git a/src/crux/proxy/github_proxy_connect.go b/src/crux/proxy/github_proxy_connect.go index aa7cd75310..d1365dfc66 100644 --- a/src/crux/proxy/github_proxy_connect.go +++ b/src/crux/proxy/github_proxy_connect.go @@ -237,7 +237,7 @@ func (p *GitHubProxy) handleTransparentTLS(conn net.Conn, peeked []byte) { // an allocated agent UID) is named internalCallerName so a hive-originated // write is attributed rather than mistaken for a UID-attribution failure. func (p *GitHubProxy) attributeTransparentConn(conn net.Conn) string { - if p.uidMap == nil { + if p.loadedUIDMap() == nil { return "" } _, portStr, splitErr := net.SplitHostPort(conn.RemoteAddr().String()) @@ -257,7 +257,7 @@ func (p *GitHubProxy) attributeTransparentConn(conn net.Conn) string { if lookupErr != nil { return "" } - if name := p.uidMap.LookupByUID(uid); name != "" { + if name := p.lookupAgentByUID(uid); name != "" { return name } // The hive's own control plane is not an agent, so the lookup above @@ -266,7 +266,7 @@ func (p *GitHubProxy) attributeTransparentConn(conn net.Conn) string { // like an unattributable agent, which the mode gate blocks. Name it // explicitly so it is attributed rather than mistaken for a // UID-attribution failure. - if p.uidMap.IsInternalUID(uid) { + if p.loadedUIDMap().IsInternalUID(uid) { return internalCallerName } return "" diff --git a/src/crux/proxy/github_proxy_identity.go b/src/crux/proxy/github_proxy_identity.go index 03d95e5366..a024304eac 100644 --- a/src/crux/proxy/github_proxy_identity.go +++ b/src/crux/proxy/github_proxy_identity.go @@ -16,7 +16,7 @@ import ( // including native binaries that don't send Proxy-Authorization); see // fallbackAgentName for what happens when that fails. func (p *GitHubProxy) identifyAgentFromReq(r *http.Request) string { - if p.uidMap != nil { + if p.loadedUIDMap() != nil { if name := p.identifyAgentByUID(r.RemoteAddr); name != "" { return name } @@ -33,7 +33,7 @@ func (p *GitHubProxy) identifyAgentFromReq(r *http.Request) string { // functional" mode worth that trade β€” an unidentified caller is refused. // Returns "" when no UID map was loaded or the connection's UID is unmapped. func (p *GitHubProxy) IdentifyAgentByUID(r *http.Request) string { - if p.uidMap == nil || r == nil { + if p.loadedUIDMap() == nil || r == nil { return "" } return p.identifyAgentByUID(r.RemoteAddr) @@ -47,7 +47,7 @@ func (p *GitHubProxy) IdentifyAgentByUID(r *http.Request) string { // falls back to ADVISORY β€” downgrading every write-capable agent to read-only. // Taking the peer address from the connection itself removes that trap. func (p *GitHubProxy) identifyAgentFromConn(conn net.Conn, r *http.Request) string { - if p.uidMap != nil && conn != nil { + if p.loadedUIDMap() != nil && conn != nil { if name := p.identifyAgentByUID(conn.RemoteAddr().String()); name != "" { return name } @@ -101,7 +101,7 @@ func (p *GitHubProxy) identifyAgentByUID(remoteAddr string) string { if err != nil { return "" } - return p.uidMap.LookupByUID(uid) + return p.lookupAgentByUID(uid) } // uidForRemoteAddr resolves the UID of the process that owns the local socket diff --git a/src/crux/proxy/github_proxy_inference.go b/src/crux/proxy/github_proxy_inference.go index 59a2ef23ec..c080039701 100644 --- a/src/crux/proxy/github_proxy_inference.go +++ b/src/crux/proxy/github_proxy_inference.go @@ -49,8 +49,22 @@ const inferenceAPIKeyPrefix = "sk-hive-" // - a UID mapped to an agent is authoritative; a header naming someone else // is logged and overridden; // - the root control plane keeps the header name β€” the hive is the party -// that issues these keys; the proxy UID cannot assert a name because its -// upstream sockets can relay agent-controlled requests; +// that issues these keys; +// - the proxy UID keeps the header name only while the private-forward +// guard is in place. On v5 pods the hive drops to `dev`, which IS the +// proxy UID, and every agent without its own UID runs as it, so refusing +// that UID outright refused the hive's own agents (#18061). The reason it +// was refused (#14448) is that the proxy's plain-forward and uninspected +// tunnel legs can relay an agent-controlled request to this loopback +// port from the proxy's own socket; those legs dial only globally +// routable addresses unless HIVE_PROXY_ALLOW_PRIVATE_FORWARD lifts the +// guard, so with the guard lifted the proxy UID is still refused. Even +// with the guard in place the proxy UID is a SHARED identity: every +// UID-less agent runs as it, so one such agent could claim another +// agent's key. A caller at that UID may therefore only assert a name +// that has no per-agent UID: an agent that owns a UID always arrives +// from that UID, so a proxy-UID socket claiming it is refused, and +// HIVE_PROXY_ADVISORY_OK does not lift that refusal (#18115); // - an unmapped UID, or no UID map at all, accepts the header only under // HIVE_PROXY_ADVISORY_OK, the same operator escape hatch the GitHub path // honours for deployments without per-agent UID separation. @@ -59,7 +73,7 @@ const inferenceAPIKeyPrefix = "sk-hive-" // request must be refused. func (p *GitHubProxy) translatorAgentName(r *http.Request) (string, bool) { claimed := strings.TrimPrefix(r.Header.Get("x-api-key"), inferenceAPIKeyPrefix) - if p.uidMap == nil { + if p.loadedUIDMap() == nil { if p.proxyAdvisoryOK { return claimed, true } @@ -67,18 +81,20 @@ func (p *GitHubProxy) translatorAgentName(r *http.Request) (string, bool) { } uid, err := uidForRemoteAddr(r.RemoteAddr) if err == nil { - if name := p.uidMap.LookupByUID(uid); name != "" { + if name := p.lookupAgentByUID(uid); name != "" { if claimed != "" && claimed != name { p.logger.Warn("inference identity: x-api-key names another agent; using the socket owner", "agent", name, "claimed", claimed, "uid", uid) } return name, true } - if uid == 0 && p.uidMap.IsInternalUID(uid) { + if p.internalTranslatorUID(uid) && !p.sharedUIDClaimsIsolatedAgent(uid, claimed) { return claimed, true } } - if p.proxyAdvisoryOK { + // Advisory-ok accepts self-asserted identity where the socket proves + // nothing; it does not override a socket that proves the claim false. + if p.proxyAdvisoryOK && !(err == nil && p.sharedUIDClaimsIsolatedAgent(uid, claimed)) { return claimed, true } return "", false diff --git a/src/crux/proxy/github_proxy_mitm.go b/src/crux/proxy/github_proxy_mitm.go index da81467ffe..d750bd976e 100644 --- a/src/crux/proxy/github_proxy_mitm.go +++ b/src/crux/proxy/github_proxy_mitm.go @@ -77,7 +77,29 @@ func (p *GitHubProxy) proxyHTTPHost(client net.Conn, upstream net.Conn, host str // refused and audited as agent_write_refused. Checked after pause and // scope so their more specific reasons win when both apply. writeSurfaceKind := "" + // Reference repos (#18015) sit outside every list below: not in + // allowedRepos (so the mode chain's repo filter would refuse the + // write with the wrong reason), not pausable, not agent-scoped. The + // gate runs first so its refusal β€” naming reference_repos and the + // tier β€” is what the agent reads, and an allowed opt-in write is + // remembered so the repo filter and the PR hard-deny below do not + // undo it (the credential it will carry is the reference repo's + // own, so App-bot authorship is already guaranteed). + referenceWrite := false if agentName != internalCallerName { + if pol, isRef := p.referenceRepoFor(repoScopePath(req)); isRef { + if reason, refused := p.referenceRepoRefusal(req); refused { + blocked = true + blockReason = reason + blockCode = DenyReasonReferenceRepo + } else { + referenceWrite = referenceWriteCredential(pol, req.Method, req.URL.Path, req.URL.RawQuery) + } + } + } + if blocked { + // Refused as a reference-repo write; nothing below can un-block it. + } else if agentName != internalCallerName { if reason, paused := p.repoPauseRefusal(req.Method, repoScopePath(req)); paused { blocked = true blockReason = reason @@ -165,6 +187,12 @@ func (p *GitHubProxy) proxyHTTPHost(client net.Conn, upstream net.Conn, host str // autonomy. Its control-plane calls (App token mint, heartbeat) // must not be gated as if they were agent writes. Repo-filter and // canary-egress checks below still apply. + } else if referenceWrite && !referenceWriteAllowedByMode(mode, req.Method, req.URL.Path) { + blocked = true + blockCode = DenyReasonMode + } else if referenceWrite { + // Opt-in reference write at a sufficient mode: skip the PR + // hard-deny and the repo filter, both of which assume a managed repo. } else if !AllowedByModeCaps(autonomyModeForRepo(agentName, ExtractRepo(repoScopePath(req)), mode), caps, req.Method, req.URL.Path) { blocked = true blockCode = DenyReasonMode diff --git a/src/crux/proxy/github_proxy_policy.go b/src/crux/proxy/github_proxy_policy.go index b61b8d03ab..2d60d115e4 100644 --- a/src/crux/proxy/github_proxy_policy.go +++ b/src/crux/proxy/github_proxy_policy.go @@ -180,6 +180,17 @@ const loginPathPrefix = "/login/" func (p *GitHubProxy) rewriteGitHubAuth(req *http.Request, agentName string) { // NOTE: callers must gate on injectsGitHubAuthForHost first; this function // does not know which host the request is bound for. + if agentName != internalCallerName { + if pol, isRef := p.referenceRepoFor(repoScopePath(req)); isRef { + // Reference repo (#18015): the agent's own credential belongs to + // the hive's installation and is never valid here, so it is + // stripped whatever the injection flag says. A read goes out + // unauthenticated, as the public internet would send it; an + // opt-in write carries the repo's own write credential. + p.rewriteReferenceRepoAuth(req, agentName, pol) + return + } + } if !p.injectGHAuth { // Flag off (the default): behavior byte-identical to before #1861 β€” // no header is touched on any path. @@ -372,3 +383,28 @@ func isGitPath(path string) bool { strings.HasSuffix(path, "/git-upload-pack") || strings.HasSuffix(path, "/info/refs") } + +func (p *GitHubProxy) rewriteReferenceRepoAuth(req *http.Request, agentName string, pol ReferenceRepoPolicy) { + req.Header.Del("Authorization") + if isGitPath(req.URL.Path) { + // Same reason as the managed-repo git branch above: each git request + // must re-enter this rewrite on its own connection. + req.Close = true + } + if !referenceWriteCredential(pol, req.Method, req.URL.Path, req.URL.RawQuery) { + p.logger.Debug("proxy auth: reference repo read forwarded without credentials", "agent", agentName, "repo", pol.Repo, "injected", false) + return + } + token := p.referenceToken(pol) + if token == "" { + p.logger.Warn("proxy auth: reference repo write has no credential β€” forwarding without one (check the write_token_file or the App installation on the target org)", "agent", agentName, "repo", pol.Repo, "injected", false) + return + } + if isGitPath(req.URL.Path) { + basic := base64.StdEncoding.EncodeToString([]byte(gitInjectBasicUser + ":" + token)) + req.Header.Set("Authorization", "Basic "+basic) + } else { + req.Header.Set("Authorization", "token "+token) + } + p.logger.Debug("proxy auth: reference repo write credential attached", "agent", agentName, "repo", pol.Repo, "injected", true) +} diff --git a/src/crux/proxy/github_proxy_uidmap.go b/src/crux/proxy/github_proxy_uidmap.go new file mode 100644 index 0000000000..7c934b5f6f --- /dev/null +++ b/src/crux/proxy/github_proxy_uidmap.go @@ -0,0 +1,102 @@ +package proxy + +import ( + "os" + + "github.com/hivecommons/hive/crux/agent" +) + +// loadedUIDMap returns the UID map the proxy currently attributes callers +// with, or nil when none was loaded at construction. Every reader goes through +// here because refreshUIDMap may swap the pointer while requests are in +// flight. +func (p *GitHubProxy) loadedUIDMap() *agent.UIDMap { + p.uidMapMu.RLock() + defer p.uidMapMu.RUnlock() + return p.uidMap +} + +// refreshUIDMap re-reads uid-map.json when it changed since the proxy last +// loaded it and returns the new map, or nil when nothing changed or no reload +// is possible. +// +// WHY (#18184). The manager allocates a UID for an agent added at runtime +// (AddAgent, ReconcileAgents) and saves the map, but the proxy used to read the +// file once at construction. Until a restart such an agent's own socket was +// refused as uid_not_mapped, and β€” worse β€” LookupByName returned 0 for it, so +// the #18133 check that stops a proxy-UID caller from claiming a UID-isolated +// agent's name accepted the claim. The file, not the manager's in-memory map, +// is the source here because in sidecar mode the manager runs in another +// process. +// +// The callers reload only on a lookup MISS, so a known agent costs nothing +// and an unknown UID or name costs one stat. A map absent at construction +// stays absent (uidMapPath is empty): that is the no-entrypoint deployment +// whose ADVISORY semantics must not flip mid-run. An unreadable or malformed +// file keeps the previous map; its stamp is still recorded so a bad file is +// warned about once per change rather than on every request. +func (p *GitHubProxy) refreshUIDMap() *agent.UIDMap { + if p.uidMapPath == "" { + return nil + } + p.uidMapReloadMu.Lock() + defer p.uidMapReloadMu.Unlock() + + info, err := os.Stat(p.uidMapPath) + if err != nil { + return nil + } + if prev := p.uidMapInfo; prev != nil && os.SameFile(prev, info) && + prev.ModTime().Equal(info.ModTime()) && prev.Size() == info.Size() { + return nil + } + p.uidMapInfo = info + loaded, err := agent.LoadUIDMap(p.uidMapPath) + if err != nil { + if p.logger != nil { + p.logger.Warn("proxy: UID map changed but could not be reloaded; keeping the previous map", "path", p.uidMapPath, "error", err) + } + return nil + } + p.uidMapMu.Lock() + p.uidMap = loaded + p.uidMapMu.Unlock() + if p.logger != nil { + p.logger.Info("proxy reloaded UID map", "path", p.uidMapPath, "agents", len(loaded.Agents)) + } + return loaded +} + +// lookupAgentByUID names the agent that owns uid, reloading the UID map once +// when uid is not in it so an agent added at runtime is attributed without a +// restart. Returns "" when no map is loaded or uid belongs to no agent. +func (p *GitHubProxy) lookupAgentByUID(uid int) string { + m := p.loadedUIDMap() + if m == nil { + return "" + } + if name := m.LookupByUID(uid); name != "" { + return name + } + if fresh := p.refreshUIDMap(); fresh != nil { + return fresh.LookupByUID(uid) + } + return "" +} + +// lookupUIDByName returns the per-agent UID allocated to name, reloading the +// UID map once on a miss (see lookupAgentByUID). Returns 0 when no map is +// loaded or name has no per-agent UID. +func (p *GitHubProxy) lookupUIDByName(name string) int { + m := p.loadedUIDMap() + if m == nil { + return 0 + } + if uid := m.LookupByName(name); uid > 0 { + return uid + } + if fresh := p.refreshUIDMap(); fresh != nil { + return fresh.LookupByName(name) + } + return 0 +} diff --git a/src/crux/proxy/github_proxy_uidmap_test.go b/src/crux/proxy/github_proxy_uidmap_test.go new file mode 100644 index 0000000000..9dd66db663 --- /dev/null +++ b/src/crux/proxy/github_proxy_uidmap_test.go @@ -0,0 +1,137 @@ +package proxy + +import ( + "io" + "log/slog" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/hivecommons/hive/crux/agent" +) + +// bootUIDMap writes a uid-map.json holding "quality" at 2002 with proxy UID +// 1001, points agent.UIDMapPath at it, and builds the proxy the way boot does, +// so the proxy's copy is the one loaded at construction. +func bootUIDMap(t *testing.T, logOut io.Writer) (*GitHubProxy, string) { + t.Helper() + path := filepath.Join(t.TempDir(), "uid-map.json") + m := agent.NewUIDMap() + m.Agents["quality"] = 2002 + if err := m.Save(path); err != nil { + t.Fatal(err) + } + old := agent.UIDMapPath + agent.UIDMapPath = path + t.Cleanup(func() { agent.UIDMapPath = old }) + + p, err := NewGitHubProxyEphemeral(slog.New(slog.NewTextHandler(logOut, nil)), "org", []string{"repo"}) + if err != nil { + t.Fatal(err) + } + if p.loadedUIDMap() == nil { + t.Fatal("proxy did not load the boot UID map") + } + return p, path +} + +// addAgentAtRuntime does what Manager.AddAgent / ReconcileAgents do for a new +// agent after boot: allocate the next UID and save the map to disk. +func addAgentAtRuntime(t *testing.T, path, name string) int { + t.Helper() + m, err := agent.LoadUIDMap(path) + if err != nil { + t.Fatal(err) + } + uid := m.AllocateUID(name) + if err := m.Save(path); err != nil { + t.Fatal(err) + } + return uid +} + +// TestUIDMap_RuntimeAddedAgentOwnSocketAccepted pins #18184: an agent the +// manager adds after the proxy was built is identified from its own socket +// without a restart, instead of being refused as uid_not_mapped. +func TestUIDMap_RuntimeAddedAgentOwnSocketAccepted(t *testing.T) { + var buf strings.Builder + p, path := bootUIDMap(t, &buf) + uid := addAgentAtRuntime(t, path, "scanner") + if uid != 2003 { + t.Fatalf("allocated uid = %d, want 2003", uid) + } + fakeProcNetTCPUID(t, uid) + + if name, ok := p.translatorAgentName(translatorReq("127.0.0.1:43210", "")); !ok || name != "scanner" { + t.Fatalf("runtime-added agent's own socket: got (%q,%v), want (scanner,true)", name, ok) + } + if got := p.identifyAgentByUID("127.0.0.1:43210"); got != "scanner" { + t.Fatalf("identifyAgentByUID = %q, want scanner", got) + } + if strings.Contains(buf.String(), "uid_not_mapped") { + t.Fatalf("runtime-added agent refused as uid_not_mapped: %s", buf.String()) + } +} + +// TestUIDMap_ProxyUIDClaimOnRuntimeAddedAgentRefused pins #18184 against the +// #18133 invariant: once an agent added at runtime owns a UID, a proxy-UID +// caller claiming its name is refused with proxy_uid_claims_isolated_agent +// rather than accepted because the proxy's map was stale. +func TestUIDMap_ProxyUIDClaimOnRuntimeAddedAgentRefused(t *testing.T) { + var buf strings.Builder + p, path := bootUIDMap(t, &buf) + p.inference = newInferenceRouter() + addAgentAtRuntime(t, path, "scanner") + p.inference.Set("scanner", &InferenceRoute{Backend: "vllm", Endpoint: "http://127.0.0.1:1", Model: "m", APIKey: "victim-secret"}) + + // The name check alone must see the new agent, independent of any UID + // lookup having refreshed the map first. + if !p.sharedUIDClaimsIsolatedAgent(1001, "scanner") { + t.Fatal("sharedUIDClaimsIsolatedAgent did not see the runtime-added agent") + } + + fakeProcNetTCPUID(t, 1001) + if name, ok := p.translatorAgentName(translatorReq("127.0.0.1:43210", "scanner")); ok || name != "" { + t.Fatalf("proxy uid claiming runtime-added agent: got (%q,%v), want refused", name, ok) + } + rec := httptest.NewRecorder() + p.inferenceTranslatorHandler().ServeHTTP(rec, translatorReq("127.0.0.1:43210", "scanner")) + if rec.Code != http.StatusUnauthorized { + t.Fatalf("status = %d, want 401; body %s", rec.Code, rec.Body.String()) + } + if !strings.Contains(buf.String(), "reason=proxy_uid_claims_isolated_agent") { + t.Errorf("refusal log %q missing reason=proxy_uid_claims_isolated_agent", buf.String()) + } + if strings.Contains(buf.String(), "victim-secret") { + t.Errorf("log %q leaked the victim's route key", buf.String()) + } +} + +// TestUIDMap_RefreshOnlyWhenFileChanges pins the reload discipline: an +// unchanged file is not re-read, a malformed rewrite keeps the previous map, +// and a map absent at construction never appears mid-run. +func TestUIDMap_RefreshOnlyWhenFileChanges(t *testing.T) { + p, path := bootUIDMap(t, io.Discard) + before := p.loadedUIDMap() + if p.refreshUIDMap() != nil || p.loadedUIDMap() != before { + t.Fatal("unchanged uid-map.json was reloaded") + } + + if err := os.WriteFile(path, []byte("{not json"), 0o644); err != nil { + t.Fatal(err) + } + if p.refreshUIDMap() != nil || p.loadedUIDMap() != before { + t.Fatal("malformed uid-map.json replaced the loaded map") + } + if got := p.lookupUIDByName("quality"); got != 2002 { + t.Fatalf("after malformed rewrite, quality uid = %d, want 2002", got) + } + + noMap := &GitHubProxy{logger: slog.New(slog.NewTextHandler(io.Discard, nil))} + if noMap.refreshUIDMap() != nil || noMap.lookupAgentByUID(2002) != "" || noMap.loadedUIDMap() != nil { + t.Fatal("proxy with no boot UID map loaded one at runtime") + } +} diff --git a/src/crux/proxy/inference_refusal.go b/src/crux/proxy/inference_refusal.go index 922db429e8..754b4c59eb 100644 --- a/src/crux/proxy/inference_refusal.go +++ b/src/crux/proxy/inference_refusal.go @@ -1,21 +1,53 @@ package proxy -import "net/http" +import ( + "net/http" + "strings" +) + +// internalTranslatorUID reports whether uid is the hive's own control plane +// for the translator's purposes: root always, and the proxy UID only when the +// proxy cannot relay to private addresses (see translatorAgentName). +func (p *GitHubProxy) internalTranslatorUID(uid int) bool { + if !p.loadedUIDMap().IsInternalUID(uid) { + return false + } + return uid == 0 || !p.allowPrivateForward +} + +// sharedUIDClaimsIsolatedAgent reports whether a caller at the proxy UID is +// asserting the name of an agent that has its own per-agent UID. The proxy +// UID is shared by the hive process and every UID-less agent, so the header +// is the only thing distinguishing those callers from each other; an agent +// that owns a UID never connects from the proxy UID, so such a claim can only +// be another process borrowing that agent's inference route (#18115). Root is +// the control plane and is not subject to this check, and other unmapped UIDs +// keep the HIVE_PROXY_ADVISORY_OK semantics the GitHub path documents. +func (p *GitHubProxy) sharedUIDClaimsIsolatedAgent(uid int, claimed string) bool { + return uid != 0 && uid == p.loadedUIDMap().ProxyUID && claimed != "" && p.lookupUIDByName(claimed) > 0 +} // translatorRefusalAttrs are the log attributes for a refused translator // request. Beyond remote/method/path they say why identification failed (no UID -// map, socket not resolvable, or UID not mapped to an agent), the socket owner's -// UID when known, and whether an x-api-key was sent, so the refusal is -// actionable without weakening it. The key value is never logged. +// map, socket not resolvable, proxy UID refused because private forwarding is +// enabled, proxy UID claiming a UID-isolated agent's name, or UID not mapped +// to an agent), the socket owner's UID when known, and whether an x-api-key +// was sent, so the refusal is actionable without weakening it. The key value +// is never logged; the claimed agent NAME is (it is public inside the pod). func (p *GitHubProxy) translatorRefusalAttrs(r *http.Request) []any { attrs := []any{"remote", r.RemoteAddr, "method", r.Method, "path", r.URL.Path} switch { - case p.uidMap == nil: + case p.loadedUIDMap() == nil: attrs = append(attrs, "reason", "no_uid_map") default: uid, err := uidForRemoteAddr(r.RemoteAddr) + claimed := strings.TrimPrefix(r.Header.Get("x-api-key"), inferenceAPIKeyPrefix) if err != nil { attrs = append(attrs, "reason", "socket_owner_unresolved", "error", err.Error()) + } else if p.loadedUIDMap().IsInternalUID(uid) && p.allowPrivateForward { + attrs = append(attrs, "reason", "proxy_uid_private_forward", "uid", uid) + } else if p.loadedUIDMap().IsInternalUID(uid) && p.sharedUIDClaimsIsolatedAgent(uid, claimed) { + attrs = append(attrs, "reason", "proxy_uid_claims_isolated_agent", "uid", uid, "claimed", claimed) } else { attrs = append(attrs, "reason", "uid_not_mapped", "uid", uid) } diff --git a/src/crux/proxy/inference_translator_identity_test.go b/src/crux/proxy/inference_translator_identity_test.go index 21cf3f4179..726b38dbdd 100644 --- a/src/crux/proxy/inference_translator_identity_test.go +++ b/src/crux/proxy/inference_translator_identity_test.go @@ -96,12 +96,106 @@ func TestTranslatorAgentName_UnmappedRefusedUnlessAdvisoryOK(t *testing.T) { func TestTranslatorAgentName_InternalUIDKeepsHeader(t *testing.T) { fakeProcNetTCPUID(t, 0) p := &GitHubProxy{logger: slog.New(slog.NewTextHandler(io.Discard, nil))} - // Root is the control plane; the separate proxy UID is not trusted. + // Root is the control plane whatever the private-forward setting. p.uidMap = &agent.UIDMap{BaseUID: 2001, ProxyUID: 2003, Agents: map[string]int{"quality": 2002}} if name, ok := p.translatorAgentName(translatorReq("127.0.0.1:43210", "supervisor")); !ok || name != "supervisor" { t.Fatalf("internal uid: got (%q,%v), want (supervisor,true)", name, ok) } + // Lifting the private-forward guard refuses only the proxy UID, never root. + p.allowPrivateForward = true + if name, ok := p.translatorAgentName(translatorReq("127.0.0.1:43210", "supervisor")); !ok || name != "supervisor" { + t.Fatalf("root, private forward allowed: got (%q,%v), want (supervisor,true)", name, ok) + } +} + +// TestTranslatorAgentName_ProxyUIDTrustedOnlyWhileForwardGuarded pins #18061: +// on v5 pods the hive drops to the proxy UID (dev, 1001) and agents without a +// per-agent UID run as it, so the proxy UID keeps its header name β€” but only +// while the private-forward guard stops the proxy relaying agent requests to +// this loopback port (#14448). With the guard lifted it is refused again. +func TestTranslatorAgentName_ProxyUIDTrustedOnlyWhileForwardGuarded(t *testing.T) { + fakeProcNetTCP(t) + var buf strings.Builder + p := &GitHubProxy{logger: slog.New(slog.NewTextHandler(&buf, nil))} + p.uidMap = &agent.UIDMap{BaseUID: 2001, ProxyUID: 2003, Agents: map[string]int{"quality": 2002}} + + if name, ok := p.translatorAgentName(translatorReq("127.0.0.1:43210", "supervisor")); !ok || name != "supervisor" { + t.Fatalf("proxy uid, forward guarded: got (%q,%v), want (supervisor,true)", name, ok) + } + + p.allowPrivateForward = true + if name, ok := p.translatorAgentName(translatorReq("127.0.0.1:43210", "supervisor")); ok || name != "" { + t.Fatalf("proxy uid, private forward allowed: got (%q,%v), want refused", name, ok) + } + p.inferenceTranslatorHandler().ServeHTTP(httptest.NewRecorder(), translatorReq("127.0.0.1:43210", "supervisor")) + if !strings.Contains(buf.String(), "reason=proxy_uid_private_forward") { + t.Errorf("refusal log %q lacks reason=proxy_uid_private_forward", buf.String()) + } + + // An agent allocated the proxy UID is never internal: its own name wins. + p.allowPrivateForward = false + p.uidMap = &agent.UIDMap{BaseUID: 2001, ProxyUID: 2003, Agents: map[string]int{"scanner": 2003}} + if name, ok := p.translatorAgentName(translatorReq("127.0.0.1:43210", "supervisor")); !ok || name != "scanner" { + t.Fatalf("agent at proxy uid: got (%q,%v), want (scanner,true)", name, ok) + } +} + +// TestTranslatorAgentName_ProxyUIDCannotClaimIsolatedAgent pins #18115: the +// proxy UID is shared by the hive process and every UID-less agent, so a +// caller at that UID may only assert a name that has NO per-agent UID. A +// claim on an agent that owns a UID (which would have arrived from that UID) +// is refused, whatever the private-forward setting; root is still trusted. +func TestTranslatorAgentName_ProxyUIDCannotClaimIsolatedAgent(t *testing.T) { + fakeProcNetTCP(t) + var buf strings.Builder + p := &GitHubProxy{logger: slog.New(slog.NewTextHandler(&buf, nil)), inference: newInferenceRouter()} + p.uidMap = &agent.UIDMap{BaseUID: 2001, ProxyUID: 2003, Agents: map[string]int{"quality": 2002}} + // The victim has a real route; the spoofed request must never reach it. + p.inference.Set("quality", &InferenceRoute{Backend: "vllm", Endpoint: "http://127.0.0.1:1", Model: "m", APIKey: "victim-secret"}) + + if name, ok := p.translatorAgentName(translatorReq("127.0.0.1:43210", "quality")); ok || name != "" { + t.Fatalf("proxy uid claiming isolated agent: got (%q,%v), want refused", name, ok) + } + + rec := httptest.NewRecorder() + p.inferenceTranslatorHandler().ServeHTTP(rec, translatorReq("127.0.0.1:43210", "quality")) + if rec.Code != http.StatusUnauthorized { + t.Fatalf("status = %d, want 401; body %s", rec.Code, rec.Body.String()) + } + for _, want := range []string{"reason=proxy_uid_claims_isolated_agent", "uid=2003", "claimed=quality"} { + if !strings.Contains(buf.String(), want) { + t.Errorf("refusal log %q missing %q", buf.String(), want) + } + } + for _, leak := range []string{"inference forward", "victim-secret", "sk-hive-"} { + if strings.Contains(buf.String(), leak) { + t.Errorf("log %q contains %q: the victim's route was used or the key leaked", buf.String(), leak) + } + } + + // A name with no per-agent UID is the legitimate shape of a UID-less + // agent (#18061) and is still accepted from the proxy UID. + if name, ok := p.translatorAgentName(translatorReq("127.0.0.1:43210", "supervisor")); !ok || name != "supervisor" { + t.Fatalf("proxy uid claiming uid-less agent: got (%q,%v), want (supervisor,true)", name, ok) + } + + // HIVE_PROXY_ADVISORY_OK accepts self-asserted identity only where the + // socket proves nothing; a socket that proves the claim false still wins. + p.proxyAdvisoryOK = true + if name, ok := p.translatorAgentName(translatorReq("127.0.0.1:43210", "quality")); ok || name != "" { + t.Fatalf("advisory-ok proxy uid claiming isolated agent: got (%q,%v), want refused", name, ok) + } + if name, ok := p.translatorAgentName(translatorReq("127.0.0.1:43210", "supervisor")); !ok || name != "supervisor" { + t.Fatalf("advisory-ok proxy uid claiming uid-less agent: got (%q,%v), want (supervisor,true)", name, ok) + } + p.proxyAdvisoryOK = false + + // Root is the control plane that issues the keys: it may still name any agent. + fakeProcNetTCPUID(t, 0) + if name, ok := p.translatorAgentName(translatorReq("127.0.0.1:43210", "quality")); !ok || name != "quality" { + t.Fatalf("root claiming isolated agent: got (%q,%v), want (quality,true)", name, ok) + } } // TestTranslatorAgentName_NoUIDMap pins the no-map deployments: the header is diff --git a/src/crux/proxy/reference_repos.go b/src/crux/proxy/reference_repos.go new file mode 100644 index 0000000000..11108646d6 --- /dev/null +++ b/src/crux/proxy/reference_repos.go @@ -0,0 +1,417 @@ +package proxy + +import ( + "bufio" + "bytes" + "encoding/hex" + "encoding/json" + "fmt" + "io" + "net/http" + "regexp" + "strconv" + "strings" + "time" + + "github.com/hivecommons/hive/crux/agent" +) + +// Reference repositories (hivecommons/hive#18015). +// +// A reference repo is one the hive's agents may STUDY without managing it: +// project.reference_repos. Reads β€” git clone/fetch and GitHub GETs β€” pass, and +// pass WITHOUT the hive's credential: the agent's injected token belongs to the +// hive's own installation and is not valid for a foreign repo, so it is +// stripped and the request goes out as the public internet would send it. +// +// Writes are refused unless the entry opted in, and even then only to the +// narrow surface an assessor needs: open an issue, open a PR from a topic +// branch, comment on the hive's OWN issue or PR. Merging, pushing the default +// branch, labels/assignees/closing on anyone's items and reviews are never +// allowed β€” there is no mode, capability or relay that widens this. The write +// credential is the policy's own Token source (an installation token minted +// on the TARGET org, or an operator-supplied file), never the hive's token. + +// DenyReasonReferenceRepo is the 403 reason code for a refused reference-repo +// write (see denial_message.go for the vocabulary). +const DenyReasonReferenceRepo = "reference_repo" + +// ReferenceRepoPolicy is the proxy's view of one project.reference_repos entry. +type ReferenceRepoPolicy struct { + // Repo is the "owner/name" slug. + Repo string + // Issues and PRs are the write opt-ins. + Issues, PRs bool + // Token yields the write credential for Repo. nil for a read-only entry. + Token func() (string, bool) + // Login is the GitHub login the Token acts as ("[bot]" or the PAT's + // user); own-item checks compare an item's author against it. + Login string +} + +func (r ReferenceRepoPolicy) readOnly() bool { return !r.Issues && !r.PRs } + +func (r ReferenceRepoPolicy) tier() string { + switch { + case r.Issues && r.PRs: + return "issues+prs" + case r.Issues: + return "issues" + case r.PRs: + return "prs" + } + return "read-only" +} + +// referenceDecision is the outcome of the path-only part of the write check. +type referenceDecision int + +const ( + referenceNotReference referenceDecision = iota // not a reference repo, or a read: nothing to do + referenceAllow + referenceDeny + referenceOwnItem // allowed only if item #N was authored by the policy's Login + referencePush // git push: allowed only when no ref targets the default branch +) + +var ( + refIssuesPath = regexp.MustCompile(`^/repos/[^/]+/[^/]+/issues$`) + refPullsPath = regexp.MustCompile(`^/repos/[^/]+/[^/]+/pulls$`) + refItemCommentsPath = regexp.MustCompile(`^/repos/[^/]+/[^/]+/(?:issues|pulls)/(\d+)/comments$`) + refGitReceivePackRe = regexp.MustCompile(`\.git/git-receive-pack$`) + refGitInfoRefsRe = regexp.MustCompile(`\.git/info/refs$`) + referenceLookupLimit = int64(1 << 20) +) + +// ReferenceRepoDecision classifies one request against a reference-repo +// policy without touching the network. number is the item for an +// referenceOwnItem decision. reason is the agent-facing refusal for +// referenceDeny. +func ReferenceRepoDecision(pol ReferenceRepoPolicy, method, path, rawQuery string) (d referenceDecision, number int, reason string) { + push := refGitReceivePackRe.MatchString(path) || (refGitInfoRefsRe.MatchString(path) && gitServiceIsReceivePack(rawQuery)) + if !push && (!writeMethods[method] || gitUploadPackPath.MatchString(path)) { + return referenceNotReference, 0, "" + } + if pol.readOnly() { + return referenceDeny, 0, referenceRepoRefusal(pol, "it is listed as READ-ONLY") + } + switch { + case push: + if !pol.PRs { + return referenceDeny, 0, referenceRepoRefusal(pol, "pushing needs write: [prs]") + } + return referencePush, 0, "" + case method == "POST" && refIssuesPath.MatchString(path): + if !pol.Issues { + return referenceDeny, 0, referenceRepoRefusal(pol, "opening an issue needs write: [issues]") + } + return referenceAllow, 0, "" + case method == "POST" && refPullsPath.MatchString(path): + if !pol.PRs { + return referenceDeny, 0, referenceRepoRefusal(pol, "opening a PR needs write: [prs]") + } + return referenceAllow, 0, "" + case method == "POST" && refItemCommentsPath.MatchString(path): + m := refItemCommentsPath.FindStringSubmatch(path) + n, _ := strconv.Atoi(m[1]) + return referenceOwnItem, n, "" + } + return referenceDeny, 0, referenceRepoRefusal(pol, "only opening an issue or PR and commenting on the hive's own items are ever allowed there β€” never merge, push to the default branch, label, assign, close or review") +} + +func referenceRepoRefusal(pol ReferenceRepoPolicy, why string) string { + return "repository " + pol.Repo + " is a REFERENCE repo (project.reference_repos, tier " + pol.tier() + "): " + why + ". Reads β€” clone, fetch, GET β€” still work. Put findings about it into the deliverable in your AUTHORIZED repo instead. Do NOT retry, do NOT route around this with another CLI, the hive relays or the GitHub MCP, and do NOT file an issue about it: this is how the operator scoped the hive, not an outage." +} + +// SetReferenceRepos installs the reference-repo policies (#18015). Replaces +// the previous set; nil clears it. +func (p *GitHubProxy) SetReferenceRepos(policies []ReferenceRepoPolicy) { + m := make(map[string]ReferenceRepoPolicy, len(policies)) + for _, pol := range policies { + key := strings.ToLower(strings.TrimSpace(pol.Repo)) + if key == "" { + continue + } + pol.Repo = strings.TrimSpace(pol.Repo) + m[key] = pol + } + p.referenceMu.Lock() + p.referenceRepos = m + p.referenceMu.Unlock() +} + +// ReferenceRepoPolicyFor returns the installed policy for repo ("owner/name", +// case-insensitive) and whether one exists. Boot tests read it to show that +// a derived entry whose grant ended no longer has a policy +// (hivecommons/hive#18159); the request path uses referenceRepoFor. +func (p *GitHubProxy) ReferenceRepoPolicyFor(repo string) (ReferenceRepoPolicy, bool) { + p.referenceMu.RLock() + defer p.referenceMu.RUnlock() + pol, ok := p.referenceRepos[strings.ToLower(strings.TrimSpace(repo))] + return pol, ok +} + +// referenceRepoFor returns the policy covering the repo a path names. +func (p *GitHubProxy) referenceRepoFor(path string) (ReferenceRepoPolicy, bool) { + p.referenceMu.RLock() + defer p.referenceMu.RUnlock() + if len(p.referenceRepos) == 0 { + return ReferenceRepoPolicy{}, false + } + repo := ExtractRepo(path) + if repo == "" { + repo = WriteSurfaceRepo(path) + } + if repo == "" { + return ReferenceRepoPolicy{}, false + } + pol, ok := p.referenceRepos[strings.ToLower(repo)] + return pol, ok +} + +// referenceRepoRefusal is the per-request gate the MITM loop calls. It returns +// the refusal reason and true when the request must be blocked. The own-item +// and default-branch checks consult GitHub through p.referenceLookup; a lookup +// that fails refuses (fail closed) and says so. +func (p *GitHubProxy) referenceRepoRefusal(req *http.Request) (string, bool) { + pol, ok := p.referenceRepoFor(repoScopePath(req)) + if !ok { + return "", false + } + d, number, reason := ReferenceRepoDecision(pol, req.Method, req.URL.Path, req.URL.RawQuery) + switch d { + case referenceNotReference, referenceAllow: + return "", false + case referenceDeny: + return reason, true + case referenceOwnItem: + author, err := p.referenceItemAuthor(req, pol, number) + if err != nil { + return referenceRepoRefusal(pol, fmt.Sprintf("could not confirm #%d is the hive's own item (%v)", number, err)), true + } + if pol.Login == "" || !strings.EqualFold(author, pol.Login) { + return referenceRepoRefusal(pol, fmt.Sprintf("#%d was opened by %s, not by the hive (%s); comments are allowed only on the hive's own issues and PRs", number, author, pol.Login)), true + } + return "", false + case referencePush: + if refGitInfoRefsRe.MatchString(req.URL.Path) { + return "", false // ref advertisement; the refs travel in the POST + } + def, err := p.referenceDefaultBranch(req, pol) + if err != nil { + return referenceRepoRefusal(pol, fmt.Sprintf("could not resolve its default branch to protect it (%v)", err)), true + } + refs, err := peekReceivePackRefs(req) + if err != nil { + return referenceRepoRefusal(pol, "push command list unreadable ("+err.Error()+")"), true + } + for _, ref := range refs { + if ref == "refs/heads/"+def { + return referenceRepoRefusal(pol, "pushing to its default branch ("+def+") is never allowed β€” push a topic branch and open a PR"), true + } + } + return "", false + } + return "", false +} + +// referenceWriteCredential says whether a request to a reference repo should +// carry the policy's write token (a write, or the push ref advertisement) β€” +// every other request to a reference repo goes out unauthenticated. +func referenceWriteCredential(pol ReferenceRepoPolicy, method, path, rawQuery string) bool { + if pol.Token == nil { + return false + } + d, _, _ := ReferenceRepoDecision(pol, method, path, rawQuery) + return d != referenceNotReference && d != referenceDeny +} + +// referenceAPIBase derives the REST base for the forge host a request is bound +// for: api.github.com for github.com, /api/v3 for GHE. +func referenceAPIBase(req *http.Request) string { + host := canonicalHost(req.Host) + if host == "" { + host = canonicalHost(req.URL.Host) + } + switch host { + case "", "github.com", "api.github.com": + return "https://api.github.com" + } + return "https://" + host + "/api/v3" +} + +// referenceLookupFunc is the GET the own-item and default-branch checks use. +// Tests swap it; production uses an ordinary client from this process. +type referenceLookupFunc func(url, token string) ([]byte, error) + +func defaultReferenceLookup(url, token string) ([]byte, error) { + client := &http.Client{Timeout: 10 * time.Second} + req, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + return nil, err + } + req.Header.Set("Accept", "application/vnd.github+json") + if token != "" { + req.Header.Set("Authorization", "token "+token) + } + resp, err := client.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + body, err := io.ReadAll(io.LimitReader(resp.Body, referenceLookupLimit)) + if err != nil { + return nil, err + } + if resp.StatusCode/100 != 2 { + return nil, fmt.Errorf("GitHub answered %d", resp.StatusCode) + } + return body, nil +} + +func (p *GitHubProxy) referenceLookupOrDefault() referenceLookupFunc { + if p.referenceLookup != nil { + return p.referenceLookup + } + return defaultReferenceLookup +} + +func (p *GitHubProxy) referenceToken(pol ReferenceRepoPolicy) string { + if pol.Token == nil { + return "" + } + tok, ok := pol.Token() + if !ok { + return "" + } + return tok +} + +func (p *GitHubProxy) referenceItemAuthor(req *http.Request, pol ReferenceRepoPolicy, number int) (string, error) { + body, err := p.referenceLookupOrDefault()(referenceAPIBase(req)+"/repos/"+pol.Repo+"/issues/"+strconv.Itoa(number), p.referenceToken(pol)) + if err != nil { + return "", err + } + var item struct { + User struct { + Login string `json:"login"` + } `json:"user"` + } + if err := json.Unmarshal(body, &item); err != nil { + return "", err + } + if item.User.Login == "" { + return "", fmt.Errorf("item has no author") + } + return item.User.Login, nil +} + +// referenceDefaultBranchTTL bounds how long a resolved default branch is +// trusted before it is looked up again. +const referenceDefaultBranchTTL = 10 * time.Minute + +func (p *GitHubProxy) referenceDefaultBranch(req *http.Request, pol ReferenceRepoPolicy) (string, error) { + key := strings.ToLower(pol.Repo) + p.referenceMu.RLock() + cached, ok := p.referenceDefaults[key] + p.referenceMu.RUnlock() + if ok && time.Now().Before(cached.until) { + return cached.branch, nil + } + body, err := p.referenceLookupOrDefault()(referenceAPIBase(req)+"/repos/"+pol.Repo, p.referenceToken(pol)) + if err != nil { + return "", err + } + var meta struct { + DefaultBranch string `json:"default_branch"` + } + if err := json.Unmarshal(body, &meta); err != nil { + return "", err + } + if meta.DefaultBranch == "" { + return "", fmt.Errorf("repository metadata carries no default_branch") + } + p.referenceMu.Lock() + if p.referenceDefaults == nil { + p.referenceDefaults = map[string]referenceDefaultBranch{} + } + p.referenceDefaults[key] = referenceDefaultBranch{branch: meta.DefaultBranch, until: time.Now().Add(referenceDefaultBranchTTL)} + p.referenceMu.Unlock() + return meta.DefaultBranch, nil +} + +type referenceDefaultBranch struct { + branch string + until time.Time +} + +// receivePackPeekLimit bounds the command section read from a push body. The +// command list precedes the packfile and is a handful of lines; anything +// larger is not a push shape this gate will reason about. +const receivePackPeekLimit = 64 * 1024 + +// peekReceivePackRefs reads the pkt-line command section of a git-receive-pack +// body β€” " [\0caps]" lines up to the flush β€” returns the refs +// it names, and puts every byte back so the request forwards unchanged. A +// content-encoded body cannot be read here and is an error (fail closed). +func peekReceivePackRefs(req *http.Request) ([]string, error) { + if req.Body == nil { + return nil, fmt.Errorf("no body") + } + if enc := req.Header.Get("Content-Encoding"); enc != "" && enc != "identity" { + return nil, fmt.Errorf("content-encoding %q", enc) + } + br := bufio.NewReader(io.LimitReader(req.Body, receivePackPeekLimit)) + var consumed bytes.Buffer + tee := io.TeeReader(br, &consumed) + var refs []string + for { + var hdr [4]byte + if _, err := io.ReadFull(tee, hdr[:]); err != nil { + return nil, fmt.Errorf("pkt-line header: %w", err) + } + n, err := hex.DecodeString(string(hdr[:])) + if err != nil { + return nil, fmt.Errorf("pkt-line length %q", hdr) + } + size := int(n[0])<<8 | int(n[1]) + if size == 0 { + break // flush: end of commands + } + if size < 4 { + return nil, fmt.Errorf("pkt-line length %d", size) + } + line := make([]byte, size-4) + if _, err := io.ReadFull(tee, line); err != nil { + return nil, fmt.Errorf("pkt-line body: %w", err) + } + if i := bytes.IndexByte(line, 0); i >= 0 { + line = line[:i] + } + fields := strings.Fields(string(line)) + if len(fields) != 3 { + return nil, fmt.Errorf("malformed command %q", line) + } + refs = append(refs, fields[2]) + } + // Whatever bufio read ahead past the flush belongs to the packfile; it is + // also in neither consumed nor req.Body any more, so splice it back too. + rest, _ := br.Peek(br.Buffered()) + req.Body = io.NopCloser(io.MultiReader(bytes.NewReader(consumed.Bytes()), bytes.NewReader(append([]byte(nil), rest...)), req.Body)) + return refs, nil +} + +// referenceWriteAllowedByMode applies the agent's ACMM mode to an opt-in +// reference write: the per-repo opt-in widens WHERE an agent may write, never +// WHAT a mode lets it do. Opening an issue or commenting needs ISSUES_ONLY; +// pushing a branch or opening a PR needs ISSUES_AND_PRS, exactly as on a +// managed repo (the PR hard-deny that routes managed-repo PRs through +// hive-open-pr does not apply β€” the relay cannot act on a foreign repo, and +// the reference credential is the App bot's own). +func referenceWriteAllowedByMode(mode agent.AgentMode, method, path string) bool { + switch { + case refGitReceivePackRe.MatchString(path), refGitInfoRefsRe.MatchString(path), refPullsPath.MatchString(path): + return mode >= agent.ModeIssuesAndPRs + } + return mode >= agent.ModeIssuesOnly +} diff --git a/src/crux/proxy/reference_repos_test.go b/src/crux/proxy/reference_repos_test.go new file mode 100644 index 0000000000..546ed31470 --- /dev/null +++ b/src/crux/proxy/reference_repos_test.go @@ -0,0 +1,237 @@ +package proxy + +import ( + "bytes" + "encoding/base64" + "fmt" + "io" + "log/slog" + "net/http" + "strings" + "testing" + + "github.com/hivecommons/hive/crux/agent" +) + +func newReferenceTestProxy(t *testing.T, pol ReferenceRepoPolicy, lookup referenceLookupFunc) *GitHubProxy { + t.Helper() + p := &GitHubProxy{logger: slog.New(slog.NewTextHandler(io.Discard, nil))} + p.SetReferenceRepos([]ReferenceRepoPolicy{pol}) + p.referenceLookup = lookup + return p +} + +func refReq(method, path, rawQuery string, body []byte) *http.Request { + req, _ := http.NewRequest(method, "https://api.github.com"+path, bytes.NewReader(body)) + req.URL.RawQuery = rawQuery + req.Host = "api.github.com" + return req +} + +// pktLine encodes one git pkt-line. +func pktLine(s string) string { return fmt.Sprintf("%04x%s", len(s)+4, s) } + +func TestReferenceRepoDecision_Matrix(t *testing.T) { + ro := ReferenceRepoPolicy{Repo: "hivecommons/docs"} + issues := ReferenceRepoPolicy{Repo: "hivecommons/docs", Issues: true} + prs := ReferenceRepoPolicy{Repo: "hivecommons/docs", PRs: true} + both := ReferenceRepoPolicy{Repo: "hivecommons/docs", Issues: true, PRs: true} + + cases := []struct { + name string + pol ReferenceRepoPolicy + method string + path string + query string + want referenceDecision + }{ + // Reads pass for every tier. + {"ro GET", ro, "GET", "/repos/hivecommons/docs/contents/README.md", "", referenceNotReference}, + {"ro fetch", ro, "POST", "/hivecommons/docs.git/git-upload-pack", "", referenceNotReference}, + {"ro info/refs upload", ro, "GET", "/hivecommons/docs.git/info/refs", "service=git-upload-pack", referenceNotReference}, + // Read-only refuses every write, including the push advertisement. + {"ro issue", ro, "POST", "/repos/hivecommons/docs/issues", "", referenceDeny}, + {"ro comment", ro, "POST", "/repos/hivecommons/docs/issues/3/comments", "", referenceDeny}, + {"ro push adv", ro, "GET", "/hivecommons/docs.git/info/refs", "service=git-receive-pack", referenceDeny}, + {"ro push", ro, "POST", "/hivecommons/docs.git/git-receive-pack", "", referenceDeny}, + {"ro fork", ro, "POST", "/repos/hivecommons/docs/forks", "", referenceDeny}, + // issues tier. + {"issues open", issues, "POST", "/repos/hivecommons/docs/issues", "", referenceAllow}, + {"issues comment own-check", issues, "POST", "/repos/hivecommons/docs/issues/3/comments", "", referenceOwnItem}, + {"issues pr comment own-check", issues, "POST", "/repos/hivecommons/docs/pulls/9/comments", "", referenceOwnItem}, + {"issues edit", issues, "PATCH", "/repos/hivecommons/docs/issues/3", "", referenceDeny}, + {"issues label", issues, "POST", "/repos/hivecommons/docs/issues/3/labels", "", referenceDeny}, + {"issues assign", issues, "POST", "/repos/hivecommons/docs/issues/3/assignees", "", referenceDeny}, + {"issues open pr", issues, "POST", "/repos/hivecommons/docs/pulls", "", referenceDeny}, + {"issues push", issues, "POST", "/hivecommons/docs.git/git-receive-pack", "", referenceDeny}, + // prs tier. + {"prs open pr", prs, "POST", "/repos/hivecommons/docs/pulls", "", referenceAllow}, + {"prs push adv", prs, "GET", "/hivecommons/docs.git/info/refs", "service=git-receive-pack", referencePush}, + {"prs push", prs, "POST", "/hivecommons/docs.git/git-receive-pack", "", referencePush}, + {"prs open issue", prs, "POST", "/repos/hivecommons/docs/issues", "", referenceDeny}, + {"prs merge", prs, "PUT", "/repos/hivecommons/docs/pulls/9/merge", "", referenceDeny}, + {"prs review", prs, "POST", "/repos/hivecommons/docs/pulls/9/reviews", "", referenceDeny}, + {"prs edit pr", prs, "PATCH", "/repos/hivecommons/docs/pulls/9", "", referenceDeny}, + {"prs api ref", prs, "POST", "/repos/hivecommons/docs/git/refs", "", referenceDeny}, + {"prs delete ref", prs, "DELETE", "/repos/hivecommons/docs/git/refs/heads/x", "", referenceDeny}, + {"prs contents", prs, "PUT", "/repos/hivecommons/docs/contents/x.md", "", referenceDeny}, + // both. + {"both issue", both, "POST", "/repos/hivecommons/docs/issues", "", referenceAllow}, + {"both pr", both, "POST", "/repos/hivecommons/docs/pulls", "", referenceAllow}, + {"both merge", both, "PUT", "/repos/hivecommons/docs/pulls/9/merge", "", referenceDeny}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + got, _, reason := ReferenceRepoDecision(tc.pol, tc.method, tc.path, tc.query) + if got != tc.want { + t.Fatalf("decision=%v want %v (reason %q)", got, tc.want, reason) + } + if got == referenceDeny && (!strings.Contains(reason, "reference_repos") || !strings.Contains(reason, tc.pol.tier())) { + t.Fatalf("refusal must name reference_repos and the tier: %q", reason) + } + }) + } +} + +func TestReferenceRepoRefusal_OwnItemCheck(t *testing.T) { + pol := ReferenceRepoPolicy{Repo: "hivecommons/docs", Issues: true, Login: "hivecommons-hive[bot]", Token: func() (string, bool) { return "ref-tok", true }} + var gotURL, gotToken string + author := "hivecommons-hive[bot]" + p := newReferenceTestProxy(t, pol, func(url, token string) ([]byte, error) { + gotURL, gotToken = url, token + return []byte(`{"user":{"login":"` + author + `"}}`), nil + }) + + if reason, refused := p.referenceRepoRefusal(refReq("POST", "/repos/hivecommons/docs/issues/12/comments", "", nil)); refused { + t.Fatalf("comment on own issue refused: %s", reason) + } + if gotURL != "https://api.github.com/repos/hivecommons/docs/issues/12" || gotToken != "ref-tok" { + t.Fatalf("lookup url=%q token=%q", gotURL, gotToken) + } + + author = "someone-else" + reason, refused := p.referenceRepoRefusal(refReq("POST", "/repos/hivecommons/docs/issues/12/comments", "", nil)) + if !refused || !strings.Contains(reason, "someone-else") { + t.Fatalf("comment on another's issue must be refused naming the author, got refused=%v %q", refused, reason) + } + + p.referenceLookup = func(string, string) ([]byte, error) { return nil, fmt.Errorf("boom") } + if _, refused := p.referenceRepoRefusal(refReq("POST", "/repos/hivecommons/docs/issues/12/comments", "", nil)); !refused { + t.Fatal("a failed author lookup must fail closed") + } + + // A different repo is not a reference repo at all. + if _, refused := p.referenceRepoRefusal(refReq("POST", "/repos/cncf/techdocs/issues/1/comments", "", nil)); refused { + t.Fatal("non-reference repo must not be gated here") + } +} + +func TestReferenceRepoRefusal_PushGuardsDefaultBranch(t *testing.T) { + pol := ReferenceRepoPolicy{Repo: "hivecommons/docs", PRs: true, Token: func() (string, bool) { return "ref-tok", true }} + lookups := 0 + p := newReferenceTestProxy(t, pol, func(url, token string) ([]byte, error) { + lookups++ + if url != "https://api.github.com/repos/hivecommons/docs" { + t.Fatalf("unexpected lookup %s", url) + } + return []byte(`{"default_branch":"main"}`), nil + }) + zero := strings.Repeat("0", 40) + sha := strings.Repeat("a", 40) + pack := "PACK-bytes-follow" + + topic := pktLine(zero+" "+sha+" refs/heads/hive/assessment\x00report-status side-band-64k") + "0000" + pack + req := refReq("POST", "/hivecommons/docs.git/git-receive-pack", "", []byte(topic)) + if reason, refused := p.referenceRepoRefusal(req); refused { + t.Fatalf("topic-branch push refused: %s", reason) + } + rest, _ := io.ReadAll(req.Body) + if string(rest) != topic { + t.Fatalf("push body not restored byte-for-byte:\n got %q\nwant %q", rest, topic) + } + + def := pktLine(zero+" "+sha+" refs/heads/hive/x\x00caps") + pktLine(sha+" "+sha+" refs/heads/main") + "0000" + pack + reason, refused := p.referenceRepoRefusal(refReq("POST", "/hivecommons/docs.git/git-receive-pack", "", []byte(def))) + if !refused || !strings.Contains(reason, "default branch (main)") { + t.Fatalf("default-branch push must be refused, got refused=%v %q", refused, reason) + } + if lookups != 1 { + t.Fatalf("default branch should be cached after one lookup, got %d", lookups) + } + + gz := refReq("POST", "/hivecommons/docs.git/git-receive-pack", "", []byte(topic)) + gz.Header.Set("Content-Encoding", "gzip") + if _, refused := p.referenceRepoRefusal(gz); !refused { + t.Fatal("an encoded push body the guard cannot read must fail closed") + } + + if _, refused := p.referenceRepoRefusal(refReq("GET", "/hivecommons/docs.git/info/refs", "service=git-receive-pack", nil)); refused { + t.Fatal("push ref advertisement must pass for a prs entry") + } +} + +func TestReferenceRepoAuth_StripsOnReadsInjectsOnWrites(t *testing.T) { + pol := ReferenceRepoPolicy{Repo: "hivecommons/docs", Issues: true, PRs: true, Token: func() (string, bool) { return "ref-tok", true }} + p := newReferenceTestProxy(t, pol, nil) + p.injectGHAuth = true + p.agentTokenSource = func(string) (string, bool) { return "hive-app-token", true } + + read := refReq("GET", "/repos/hivecommons/docs/contents/README.md", "", nil) + read.Header.Set("Authorization", "token agent-held") + p.rewriteGitHubAuth(read, "scanner") + if got := read.Header.Get("Authorization"); got != "" { + t.Fatalf("reference read must go out unauthenticated, got %q", got) + } + + fetch := refReq("POST", "/hivecommons/docs.git/git-upload-pack", "", nil) + fetch.Header.Set("Authorization", "Basic xyz") + p.rewriteGitHubAuth(fetch, "scanner") + if got := fetch.Header.Get("Authorization"); got != "" { + t.Fatalf("reference fetch must go out unauthenticated, got %q", got) + } + + write := refReq("POST", "/repos/hivecommons/docs/issues", "", nil) + write.Header.Set("Authorization", "token agent-held") + p.rewriteGitHubAuth(write, "scanner") + if got := write.Header.Get("Authorization"); got != "token ref-tok" { + t.Fatalf("reference write must carry the reference credential, got %q", got) + } + + push := refReq("GET", "/hivecommons/docs.git/info/refs", "service=git-receive-pack", nil) + p.rewriteGitHubAuth(push, "scanner") + want := "Basic " + base64.StdEncoding.EncodeToString([]byte(gitInjectBasicUser+":ref-tok")) + if got := push.Header.Get("Authorization"); got != want || !push.Close { + t.Fatalf("push advertisement must carry basic reference credential and close, got %q close=%v", got, push.Close) + } + + // A managed repo still takes the ordinary injection path. + managed := refReq("GET", "/repos/cncf/techdocs/issues", "", nil) + p.rewriteGitHubAuth(managed, "scanner") + if got := managed.Header.Get("Authorization"); got != "token hive-app-token" { + t.Fatalf("managed repo should get the agent token, got %q", got) + } + + // Read-only entries never carry anything, even on a (refused upstream) write. + p.SetReferenceRepos([]ReferenceRepoPolicy{{Repo: "hivecommons/docs"}}) + ro := refReq("POST", "/repos/hivecommons/docs/issues", "", nil) + ro.Header.Set("Authorization", "token agent-held") + p.rewriteGitHubAuth(ro, "scanner") + if got := ro.Header.Get("Authorization"); got != "" { + t.Fatalf("read-only reference repo must never carry a credential, got %q", got) + } +} + +func TestReferenceWriteAllowedByMode(t *testing.T) { + if referenceWriteAllowedByMode(agent.ModeAdvisory, "POST", "/repos/o/r/issues") { + t.Error("ADVISORY may not open issues on a reference repo") + } + if !referenceWriteAllowedByMode(agent.ModeIssuesOnly, "POST", "/repos/o/r/issues") { + t.Error("ISSUES_ONLY may open issues") + } + if referenceWriteAllowedByMode(agent.ModeIssuesOnly, "POST", "/repos/o/r/pulls") { + t.Error("ISSUES_ONLY may not open PRs") + } + if !referenceWriteAllowedByMode(agent.ModeIssuesAndPRs, "POST", "/o/r.git/git-receive-pack") { + t.Error("ISSUES_AND_PRS may push a topic branch") + } +} diff --git a/src/crux/runtime/code_work.go b/src/crux/runtime/code_work.go index dd18b21eb9..86a5e8d24d 100644 --- a/src/crux/runtime/code_work.go +++ b/src/crux/runtime/code_work.go @@ -2,6 +2,7 @@ package runtime import ( "io" + "log/slog" "time" "github.com/hivecommons/hive/crux" @@ -14,3 +15,13 @@ import ( func ObserveCodeWorkTranscript(r io.Reader, workDir string, since, now time.Time) *crux.CodeWork { return agent.ObserveCodeWorkTranscript(r, workDir, since, now) } + +// UserSessions is the runtime behind the dashboard's Sessions feature +// (interactive, named CLI terminals). It is exposed here so consumers do not +// name the agent package. +type UserSessions = agent.UserSessionRuntime + +// NewUserSessions returns a user-session runtime driving the real tmux binary. +func NewUserSessions(logger *slog.Logger) *UserSessions { + return agent.NewUserSessionRuntime(logger) +} diff --git a/src/crux/usersession_types.go b/src/crux/usersession_types.go new file mode 100644 index 0000000000..9de9738597 --- /dev/null +++ b/src/crux/usersession_types.go @@ -0,0 +1,42 @@ +package crux + +import "time" + +// UserSessionVendor is one CLI a user session may run, as offered to the +// dashboard's new-session picker. +type UserSessionVendor struct { + ID string `json:"id"` + Label string `json:"label"` + // Available is true when the vendor's binary is on PATH. + Available bool `json:"available"` +} + +// UserSessionSpec is what the runtime needs to start (or resume) one user +// session. ID is the dashboard-issued session id (lower-case hex). +type UserSessionSpec struct { + ID string + Vendor string + // ResumeCommand, when set, is a backend resume command captured from the + // vendor's own transcript store (for example "claude --resume "). The + // runtime re-validates it and runs it instead of the bare CLI. + ResumeCommand string +} + +// UserSessionProbe is one live read of a session's tmux state. +type UserSessionProbe struct { + // Exists is false when tmux has no such session. + Exists bool + // Dead is true when the session exists but its CLI has exited (the pane is + // kept so the scrollback stays readable). + Dead bool + // Attached is the number of tmux clients currently attached. + Attached int + // Activity is tmux's last-activity time for the session. + Activity time.Time +} + +// UserSessionResume names a vendor-native conversation a session can resume. +type UserSessionResume struct { + SessionID string + Command string +} diff --git a/src/deploy/ci-runners/Dockerfile b/src/deploy/ci-runners/Dockerfile index 817b9fa0f6..f3c0d780d2 100644 --- a/src/deploy/ci-runners/Dockerfile +++ b/src/deploy/ci-runners/Dockerfile @@ -48,7 +48,7 @@ ARG RUNNER_BASE_IMAGE=summerwind/actions-runner:v2.337.0-ubuntu-24.04 FROM ${RUNNER_BASE_IMAGE} -ARG GITHUB_CLI_VERSION=2.103.3 +ARG GITHUB_CLI_VERSION=2.104.0 ARG GITHUB_CLI_KEYRING_SHA256=6084d5d7bd8e288441e0e94fc6275570895da18e6751f70f057485dc2d1a811b # The base image runs as uid 1001 (`runner`), which does not own /etc/apt. diff --git a/src/deploy/ttyd-tmux.sh b/src/deploy/ttyd-tmux.sh index ac0c3abd6d..fd2b13beef 100644 --- a/src/deploy/ttyd-tmux.sh +++ b/src/deploy/ttyd-tmux.sh @@ -28,6 +28,19 @@ else SESSION_FROM_ARG=0 fi +# User sessions (dashboard Sessions feature): hive-sess- runs on its OWN +# tmux server whose socket is named after the session. Reject any other +# hive-sess-* shape so ?arg= cannot reach a glob or a path. +USER_SESSION_RE='^hive-sess-[a-f0-9]{8,32}$' +case "$SESSION" in + hive-sess-*) + if ! [[ "$SESSION" =~ $USER_SESSION_RE ]]; then + echo "error: invalid user session name" >&2 + exit 1 + fi + ;; +esac + TTYD_WAIT_SEC="${HIVE_TTYD_WAIT_SEC:-900}" TTYD_POLL_SEC="${HIVE_TTYD_POLL_SEC:-2}" if ! [[ "$TTYD_WAIT_SEC" =~ ^[0-9]+$ ]]; then diff --git a/src/docs/README.md b/src/docs/README.md index 3f4ad2bd8d..ed854cfbd7 100644 --- a/src/docs/README.md +++ b/src/docs/README.md @@ -13,7 +13,7 @@ Choose how to obtain a running hive **before** configuring its agents: | Your situation | Start here | Expected result | |---|---|---| | You want a hosted hive, without running a cluster | [Hosted onboarding](hosted-hub.md); request at [hive.hivecommons.dev/get-started](https://hive.hivecommons.dev/get-started) | After admin approval and provisioning, **My Hives** links to your dashboard. | -| You want to run one hive yourself | [Docker Compose](quickstart-docker-compose.md), [Podman](quickstart-podman.md), or [Kubernetes](kubernetes-deployment.md) | Your own running container and dashboard, with storage and authentication configured. | +| You want to run one hive yourself | [Docker Compose](quickstart-docker-compose.md), [Podman](quickstart-podman.md), or [Kubernetes](kubernetes-deployment.md) | Your own running container and dashboard, with storage and authentication configured. To list it on a hub afterwards, see [Register a self-hosted hive](register-self-hosted-hive.md). | | Someone has already given you dashboard access | [Getting started](getting-started.md#step-0--before-you-start-do-this-first) | Connect the GitHub App, authenticate a model backend, set a budget, then enable agents conservatively. | You need a GitHub identity and a repository on GitHub.com or GitHub Enterprise. diff --git a/src/docs/admin-mcp.md b/src/docs/admin-mcp.md index eb1d418fa5..d831c9b777 100644 --- a/src/docs/admin-mcp.md +++ b/src/docs/admin-mcp.md @@ -481,7 +481,8 @@ The sections: `question_autoclose`, `question_autoclose_schedule`, `fleet_report`, `work_source`, `project_observability`, `general_advanced`, `cadence_scope`, `threshold_scaling`, `backup`, `bob`, `inference_auth`, - `gateways`, `backends`, `variables`, `write_surface`, `stat_sources`. + `gateways`, `backends`, `variables`, `write_surface`, `stat_sources`, + `reference_repos`. - **Knowledge:** `knowledge_pipeline`, `knowledge_connectors`, `knowledge_connectors_status`, `knowledge_agent_scopes`, `knowledge_public`. - **Panels the tabs load:** `upstream_watch`, `linear_agent`, @@ -495,6 +496,9 @@ Not reachable: credentials (`config_effective_read` is the vetted view of the effective configuration and its provenance); - an agent's export bundle (the `agent` and `agent_prompt` sections hold it); +- the GitHub webhook setup card, which probes the GitHub App's hook + permissions live on every read (webhook verification and health are in + `/api/status`); - the theme and sidebar, which are purely presentational. A test fails when the dashboard serves a new `GET /api/config` route that is diff --git a/src/docs/adr/0025-central-stage-engine.md b/src/docs/adr/0025-central-stage-engine.md index 49bc9b633d..a20f20afd4 100644 --- a/src/docs/adr/0025-central-stage-engine.md +++ b/src/docs/adr/0025-central-stage-engine.md @@ -428,9 +428,11 @@ Leaving 017 for 010 or closed is never a regression. **Open for discussion (`stage/018-open-for-discussion`, #17150).** Added after the epic: an issue-only stage between 017 and 020 with no dwell. Unlike 017 it is derived, not written by a gate: `DeriveIssue` returns it -for an open issue labelled `proposal` or `kind/design` with no live claim -(`IssueFacts.DiscussionLabel`, resolved in `pkg/github`), reason -`open for discussion: