diff --git a/.github/workflows/integration-candidate.yml b/.github/workflows/integration-candidate.yml index cbb54fbc26..cbdb8aed8b 100644 --- a/.github/workflows/integration-candidate.yml +++ b/.github/workflows/integration-candidate.yml @@ -465,6 +465,9 @@ jobs: - name: hive-review helper tests working-directory: . run: bash bin/test_hive_review.sh + - name: hive-topic helper tests + working-directory: . + run: bash bin/test_hive_topic.sh - name: Shared CI baseline classifier tests working-directory: . run: bash bin/test_hive_baseline_check.sh diff --git a/.github/workflows/integration-technical-v2-ci.yml b/.github/workflows/integration-technical-v2-ci.yml index e1084f89a4..812a04dcf0 100644 --- a/.github/workflows/integration-technical-v2-ci.yml +++ b/.github/workflows/integration-technical-v2-ci.yml @@ -325,6 +325,9 @@ jobs: - name: hive-review helper tests working-directory: . run: bash bin/test_hive_review.sh + - name: hive-topic helper tests + working-directory: . + run: bash bin/test_hive_topic.sh - name: Shared CI baseline classifier tests working-directory: . run: bash bin/test_hive_baseline_check.sh diff --git a/.github/workflows/v2-ci.yml b/.github/workflows/v2-ci.yml index 669bb5f24a..3c006aa10f 100644 --- a/.github/workflows/v2-ci.yml +++ b/.github/workflows/v2-ci.yml @@ -892,6 +892,10 @@ jobs: working-directory: . run: bash bin/test_hive_review.sh + - name: hive-topic helper tests + working-directory: . + run: bash bin/test_hive_topic.sh + - name: Shared CI baseline classifier tests working-directory: . run: bash bin/test_hive_baseline_check.sh diff --git a/bin/hive-open-issue.sh b/bin/hive-open-issue.sh index 7f6490681a..0b7f9b7216 100755 --- a/bin/hive-open-issue.sh +++ b/bin/hive-open-issue.sh @@ -125,6 +125,19 @@ set -euo pipefail REQ_DIR="/var/run/hive-metrics/issue-requests" +# Inside a sandbox (Podman or a Kubernetes Job) the pod-local relay dir is not +# reachable. Hive sets HIVE_SANDBOX_REQUEST_DIR to the kick's request spool and +# HIVE_SANDBOX_SPOOL_KEY to a per-run key (hivecommons/hive#18573); the request +# is then written there as {"kind": "issue", "request": "", +# "mac": ""}. The relay runs it +# through the same issue handler and gates, attributed to the kick's own agent +# whatever "agent" says. The older HIVE_REVIEW_* names are accepted too. +SPOOL_DIR="${HIVE_SANDBOX_REQUEST_DIR:-${HIVE_REVIEW_REQUEST_DIR:-}}" +SPOOL_KEY="${HIVE_SANDBOX_SPOOL_KEY:-${HIVE_REVIEW_SPOOL_KEY:-}}" +if [ -n "$SPOOL_DIR" ] && [ -n "$SPOOL_KEY" ]; then + REQ_DIR="$SPOOL_DIR" + export HIVE_OPEN_ISSUE_SPOOL_KEY="$SPOOL_KEY" +fi KIND="issue" case "${1:-}" in @@ -456,8 +469,16 @@ if dry_run == "1": print("hive-open-issue: DRY RUN — no request written, nothing will be created. Would write %s:" % path) print(json.dumps(req, indent=2)) sys.exit(0) +data = json.dumps(req) +# Sandbox spool: sign for this run. Read from the environment, never argv: +# argv is visible to every user. +key = os.environ.get("HIVE_OPEN_ISSUE_SPOOL_KEY", "") +if key: + import hashlib, hmac + mac = hmac.new(key.encode(), ("issue\n" + data).encode(), hashlib.sha256).hexdigest() + data = json.dumps({"kind": "issue", "request": data, "mac": mac}) with open(temporary, "w") as fh: - json.dump(req, fh) + fh.write(data) os.replace(temporary, path) PY diff --git a/bin/hive-topic.sh b/bin/hive-topic.sh new file mode 100755 index 0000000000..92765526e2 --- /dev/null +++ b/bin/hive-topic.sh @@ -0,0 +1,184 @@ +#!/bin/bash +# hive-topic.sh — read and write your topic claims from anywhere, sandbox included. +# +# A topic claim is a durable "this topic is mine, through this PR" record in +# the agent's own bead store (`bd topic`, hivecommons/hive#18493). In the hive +# pod this script simply runs `bd topic` with the same arguments. Inside a +# sandbox (a Podman container or a Kubernetes Job) neither `bd` nor the bead +# store exists, so it writes a signed request into the kick's request spool +# instead, waits for hive's reply and prints it (hivecommons/hive#18572). Hive +# applies the request to the bead store of the agent the kick belongs to, +# whatever this script says about who is asking. +# +# Usage (the same as `bd topic`): +# hive-topic claim [--repo owner/name] [--pr N] [--branch B] +# hive-topic get [--json] +# hive-topic list [--json] +# hive-topic release +# +# In a sandbox, `claim ` WITHOUT --pr claims the pull request hive opens +# from this kick: a sandbox has no PR number yet, so hive records the claim as +# pending on the kick's branch and binds it to the PR once it is open. --repo +# defaults to the kick's repository. Pass --pr when the PR already exists. +# +# Sandbox-only flags: --no-wait returns as soon as the request is written +# (the result appears at .result.json); --timeout bounds the wait +# (default $HIVE_TOPIC_WAIT_SECONDS, else 120). +# +# Exit codes, as `bd topic`: 0 success, 1 usage or store error (or no reply in +# time), 2 refused (the topic is held through a different PR) or not found on +# get. + +set -euo pipefail + +# The kick's spool and its per-run key (hivecommons/hive#18572). The older +# HIVE_REVIEW_* names are accepted too. +SPOOL_DIR="${HIVE_SANDBOX_REQUEST_DIR:-${HIVE_REVIEW_REQUEST_DIR:-}}" +SPOOL_KEY="${HIVE_SANDBOX_SPOOL_KEY:-${HIVE_REVIEW_SPOOL_KEY:-}}" + +if [ -z "$SPOOL_DIR" ] || [ -z "$SPOOL_KEY" ]; then + # In-pod: the bead store is right here. + if ! command -v bd >/dev/null 2>&1; then + echo "hive-topic: no sandbox spool (HIVE_SANDBOX_REQUEST_DIR/HIVE_SANDBOX_SPOOL_KEY unset) and no bd on PATH" >&2 + exit 1 + fi + exec bd topic "$@" +fi + +if ! command -v python3 >/dev/null 2>&1; then + echo "hive-topic: python3 is required to encode the topic request" >&2 + exit 1 +fi + +AGENT="${HIVE_AGENT:-agent}" +mkdir -p "$SPOOL_DIR" 2>/dev/null || true +REQ_FILE="$SPOOL_DIR/${AGENT//[^A-Za-z0-9_.-]/_}-topic-$(date +%s%N).json" + +export HIVE_TOPIC_SPOOL_KEY="$SPOOL_KEY" +exec python3 - "$REQ_FILE" "$AGENT" "${HIVE_TOPIC_WAIT_SECONDS:-120}" "$@" <<'PY' +import hashlib, hmac, json, os, sys, time + +USAGE = """Usage: + hive-topic claim [--repo owner/name] [--pr N] [--branch B] + hive-topic get [--json] + hive-topic list [--json] + hive-topic release """ + +path, agent, wait = sys.argv[1], sys.argv[2], sys.argv[3] +args = sys.argv[4:] + +def die(msg, code=1): + sys.stderr.write("hive-topic: %s\n" % msg) + sys.exit(code) + +if not args: + die("a subcommand is required\n" + USAGE) +op, rest = args[0], args[1:] +if op not in ("claim", "get", "list", "release"): + die("unknown subcommand %r\n%s" % (op, USAGE)) + +req = {"op": op, "agent": agent} +if op != "list": + if not rest or not rest[0] or rest[0].startswith("-"): + die("%s requires a topic key" % op) + req["key"], rest = rest[0], rest[1:] + +as_json, no_wait = False, False +try: + wait_s = float(wait) +except ValueError: + die("HIVE_TOPIC_WAIT_SECONDS must be a number, got %r" % wait) +i = 0 +while i < len(rest): + a = rest[i] + name, _, inline = a.partition("=") + def value(): + global i + if inline: + return inline + i += 1 + if i >= len(rest): + die("%s needs a value" % name) + return rest[i] + if name in ("--repo", "-R") and op == "claim": + req["repo"] = value() + elif name == "--pr" and op == "claim": + v = value() + if not v.isdigit() or int(v) <= 0: + die("--pr expects a positive pull request number, got %r" % v) + req["pr"] = int(v) + elif name == "--branch" and op == "claim": + req["branch"] = value() + elif name == "--actor" and op == "claim": + value() # accepted for bd compatibility; hive attributes the claim itself + elif name == "--json" and op in ("get", "list"): + as_json = True + elif name == "--no-wait": + no_wait = True + elif name == "--timeout": + v = value() + try: + wait_s = float(v) + except ValueError: + die("--timeout expects seconds, got %r" % v) + else: + die("unknown flag %r for %s; nothing was written\n%s" % (a, op, USAGE)) + i += 1 + +data = json.dumps(req) +# Read from the environment, never argv: argv is visible to every user. +key = os.environ.get("HIVE_TOPIC_SPOOL_KEY", "") +mac = hmac.new(key.encode(), ("topic\n" + data).encode(), hashlib.sha256).hexdigest() +tmp = path + ".tmp" +with open(tmp, "w") as fh: + fh.write(json.dumps({"kind": "topic", "request": data, "mac": mac})) +os.replace(tmp, path) + +result = path[:-len(".json")] + ".result.json" +if no_wait: + print("hive-topic: request %s (result appears at %s)" % (path, result)) + sys.exit(0) + +deadline = time.time() + wait_s +resp = None +while time.time() < deadline: + try: + with open(result) as fh: + resp = json.load(fh) + break + except (OSError, ValueError): + time.sleep(1) +if resp is None: + die("no reply from hive within %gs; the request stays queued at %s and its result will appear at %s" % (wait_s, path, result)) + +def line(c): + pr = "#%d" % c["pr"] if c.get("pr") else " (PR not opened yet)" + s = "%s -> %s%s" % (c.get("key", ""), c.get("repo", ""), pr) + if c.get("branch"): + s += " branch %s" % c["branch"] + return s + +if not resp.get("ok"): + if resp.get("claim"): + sys.stderr.write("hive-topic: held: %s\n" % line(resp["claim"])) + die(resp.get("error") or "request refused", 2 if resp.get("refused") else 1) +if op == "list": + claims = resp.get("claims") or [] + if as_json: + print(json.dumps(claims, indent=2)) + elif not claims: + print("no open topic claims") + else: + for c in claims: + print(line(c)) +elif op == "release": + print("released %s" % req["key"] if resp.get("released") else "no open claim for %s" % req["key"]) +else: + c = resp.get("claim") or {} + if as_json: + print(json.dumps(c, indent=2)) + elif op == "claim": + print("claimed %s" % line(c)) + else: + print(line(c)) +PY diff --git a/bin/test_hive_open_issue.sh b/bin/test_hive_open_issue.sh index b0fdb46765..1af63bb4cd 100755 --- a/bin/test_hive_open_issue.sh +++ b/bin/test_hive_open_issue.sh @@ -640,6 +640,52 @@ check "repo inside HIVE_REPOS (case-insensitive) is written" "1" "$(find "$REQ_D env HIVE_AGENT=cfgbot HIVE_REPOS="org/repo" bash "$MODIFIED_SCRIPT" --repo org/repo --title t --body b >/dev/null 2>&1 || true check "configured placeholder-looking repo is accepted" "1" "$(find "$REQ_DIR" -name 'cfgbot-*.json' | wc -l | tr -d ' ')" +# --- sandbox spool (hivecommons/hive#18573) --- +# Runs the UNMODIFIED script: the spool redirect is the script's own behavior. +echo "" +echo "--- sandbox spool via HIVE_SANDBOX_REQUEST_DIR ---" +ISSUE_SPOOL="$TMPDIR_ROOT/workspace/.hive/review-requests" +ISSUE_KEY="0123456789abcdef0123456789abcdef" +env HIVE_AGENT="spoolbot" HIVE_SANDBOX_REQUEST_DIR="$ISSUE_SPOOL" HIVE_SANDBOX_SPOOL_KEY="$ISSUE_KEY" \ + bash "$SCRIPT" --repo hiveorg/widgets --title "dependency X breaks us" --body "detail" >/dev/null 2>&1 || true +ISSUE_FILES=("$ISSUE_SPOOL"/spoolbot-*.json) +if [ -f "${ISSUE_FILES[0]}" ]; then + check "spooled envelope kind is issue" "issue" "$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["kind"])' "${ISSUE_FILES[0]}")" + # The relay recomputes HMAC-SHA256(key, "issue\n" + request): the kind is bound. + check "envelope MAC binds the issue kind" "ok" "$(python3 -c ' +import hashlib, hmac, json, sys +env = json.load(open(sys.argv[1])) +want = hmac.new(sys.argv[2].encode(), ("issue\n" + env["request"]).encode(), hashlib.sha256).hexdigest() +print("ok" if hmac.compare_digest(env["mac"], want) else "bad")' "${ISSUE_FILES[0]}" "$ISSUE_KEY")" + check "spooled request carries the title" "dependency X breaks us" "$(python3 -c 'import json,sys; print(json.loads(json.load(open(sys.argv[1]))["request"])["title"])' "${ISSUE_FILES[0]}")" + if grep -q "$ISSUE_KEY" "${ISSUE_FILES[0]}"; then + echo " FAIL: the spool key itself was written into the spool"; FAIL=$((FAIL + 1)) + else + echo " PASS: the spool key is not in the spool file"; PASS=$((PASS + 1)) + fi +else + echo " FAIL: issue request not written to HIVE_SANDBOX_REQUEST_DIR"; FAIL=$((FAIL + 4)) +fi + +echo "" +echo "--- sandbox spool via the older HIVE_REVIEW_* names ---" +OLD_SPOOL="$TMPDIR_ROOT/old/.hive/review-requests" +env HIVE_AGENT="oldbot" HIVE_REVIEW_REQUEST_DIR="$OLD_SPOOL" HIVE_REVIEW_SPOOL_KEY="$ISSUE_KEY" \ + bash "$SCRIPT" comment --repo hiveorg/widgets 41 --body "note" >/dev/null 2>&1 || true +OLD_FILES=("$OLD_SPOOL"/oldbot-*.json) +if [ -f "${OLD_FILES[0]}" ]; then + check "older env names spool a signed comment" "issue comment" "$(python3 -c 'import json,sys; e=json.load(open(sys.argv[1])); print(e["kind"], json.loads(e["request"])["kind"])' "${OLD_FILES[0]}")" +else + echo " FAIL: comment not written to HIVE_REVIEW_REQUEST_DIR"; FAIL=$((FAIL + 1)) +fi + +echo "" +echo "--- a spool dir without a key is not a spool ---" +NOKEY_SPOOL="$TMPDIR_ROOT/nokey/.hive/review-requests" +env HIVE_AGENT="nokeybot" HIVE_SANDBOX_REQUEST_DIR="$NOKEY_SPOOL" bash "$MODIFIED_SCRIPT" --repo hiveorg/widgets --title t --body b >/dev/null 2>&1 || true +check "no key: nothing written to the spool dir" "0" "$(find "$TMPDIR_ROOT/nokey" -name 'nokeybot-*.json' 2>/dev/null | wc -l | tr -d ' ')" +check "no key: the pod-local request is plain JSON" "issue" "$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1])).get("kind",""))' "$(find "$REQ_DIR" -name 'nokeybot-*.json' | head -1)")" + echo "" echo "=== $PASS passed, $FAIL failed ===" [ "$FAIL" -eq 0 ] diff --git a/bin/test_hive_topic.sh b/bin/test_hive_topic.sh new file mode 100755 index 0000000000..420f4f93f8 --- /dev/null +++ b/bin/test_hive_topic.sh @@ -0,0 +1,119 @@ +#!/usr/bin/env bash +# Tests for bin/hive-topic.sh — topic claims from the hive pod or a sandbox +# (hivecommons/hive#18572). +# +# Run: bash bin/test_hive_topic.sh +set -euo pipefail + +PASS=0 +FAIL=0 + +SCRIPT="$(cd "$(dirname "$0")" && pwd)/hive-topic.sh" + +check() { + local label="$1" want="$2" got="$3" + if [ "$want" = "$got" ]; then + echo " PASS: $label"; PASS=$((PASS + 1)) + else + echo " FAIL: $label"; echo " want: '$want'"; echo " got: '$got'"; FAIL=$((FAIL + 1)) + fi +} + +echo "=== hive-topic.sh tests ===" + +TMPDIR_ROOT="$(mktemp -d)" +trap 'rm -rf "$TMPDIR_ROOT"' EXIT +KEY="0123456789abcdef0123456789abcdef" +SPOOL="$TMPDIR_ROOT/workspace/.hive/review-requests" + +# Run the relay as a sandbox would, with the spool and key in its env. +in_sandbox() { env HIVE_AGENT=deps HIVE_SANDBOX_REQUEST_DIR="$SPOOL" HIVE_SANDBOX_SPOOL_KEY="$KEY" bash "$SCRIPT" "$@"; } +newest() { { ls -t "$SPOOL"/deps-topic-*.json 2>/dev/null || true; } | { grep -v '\.result\.json$' || true; } | head -1; } +reqfield() { python3 -c 'import json,sys; print(json.loads(json.load(open(sys.argv[1]))["request"]).get(sys.argv[2], ""))' "$1" "$2"; } + +echo "" +echo "--- in-pod: runs bd topic ---" +FAKEBIN="$TMPDIR_ROOT/bin"; mkdir -p "$FAKEBIN" +printf '#!/bin/sh\necho "bd $*"\n' > "$FAKEBIN/bd"; chmod +x "$FAKEBIN/bd" +check "no spool env execs bd topic with the same args" "bd topic get deps-bump --json" \ + "$(env -u HIVE_SANDBOX_REQUEST_DIR -u HIVE_REVIEW_REQUEST_DIR PATH="$FAKEBIN:$PATH" bash "$SCRIPT" get deps-bump --json)" +check "a spool dir without a key is not a spool" "bd topic list" \ + "$(env HIVE_SANDBOX_REQUEST_DIR="$SPOOL" PATH="$FAKEBIN:$PATH" bash "$SCRIPT" list)" + +echo "" +echo "--- sandbox: claim without --pr is written signed, kind topic ---" +in_sandbox claim deps-bump --no-wait >/dev/null +REQ="$(newest)" +if [ -n "$REQ" ]; then + check "envelope kind is topic" "topic" "$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["kind"])' "$REQ")" + check "envelope MAC binds the topic kind" "ok" "$(python3 -c ' +import hashlib, hmac, json, sys +env = json.load(open(sys.argv[1])) +want = hmac.new(sys.argv[2].encode(), ("topic\n" + env["request"]).encode(), hashlib.sha256).hexdigest() +print("ok" if hmac.compare_digest(env["mac"], want) else "bad")' "$REQ" "$KEY")" + check "op is claim" "claim" "$(reqfield "$REQ" op)" + check "key is carried" "deps-bump" "$(reqfield "$REQ" key)" + check "no pr: hive binds the kick's PR" "" "$(reqfield "$REQ" pr)" + if grep -q "$KEY" "$REQ"; then + echo " FAIL: the spool key was written into the spool"; FAIL=$((FAIL + 1)) + else + echo " PASS: the spool key is not in the spool file"; PASS=$((PASS + 1)) + fi +else + echo " FAIL: no topic request in the spool"; FAIL=$((FAIL + 6)) +fi +rm -f "$SPOOL"/*.json + +in_sandbox claim deps-bump --repo acme/widget --pr 42 --branch bump --no-wait >/dev/null +REQ="$(newest)" +check "explicit pr, repo and branch" "42 acme/widget bump" "$(reqfield "$REQ" pr) $(reqfield "$REQ" repo) $(reqfield "$REQ" branch)" +rm -f "$SPOOL"/*.json + +echo "" +echo "--- sandbox: usage errors write nothing ---" +rc=0; in_sandbox claim >/dev/null 2>&1 || rc=$? +check "claim without a key exits 1" "1" "$rc" +rc=0; in_sandbox claim k --pr nope >/dev/null 2>&1 || rc=$? +check "non-numeric --pr exits 1" "1" "$rc" +rc=0; in_sandbox frobnicate >/dev/null 2>&1 || rc=$? +check "unknown subcommand exits 1" "1" "$rc" +rc=0; in_sandbox get k --bogus >/dev/null 2>&1 || rc=$? +check "unknown flag exits 1" "1" "$rc" +check "nothing was written" "0" "$(find "$SPOOL" -name '*.json' | wc -l | tr -d ' ')" + +# reply answers the newest request with $1 once it appears, as the relay does. +reply() { + ( for _ in $(seq 1 50); do + r="$(newest)" + if [ -n "$r" ]; then printf '%s' "$1" > "${r%.json}.result.json"; exit 0; fi + sleep 0.1 + done ) & +} + +echo "" +echo "--- sandbox: waits for hive's reply ---" +reply '{"ok":true,"pending":true,"claim":{"key":"deps-bump","repo":"acme/widget","pr":0,"branch":"hive/deps-1"}}' +OUT="$(in_sandbox claim deps-bump --timeout 10)"; wait +check "pending claim is reported" "claimed deps-bump -> acme/widget (PR not opened yet) branch hive/deps-1" "$OUT" +rm -f "$SPOOL"/*.json + +reply '{"ok":false,"refused":true,"error":"topic already claimed","claim":{"key":"deps-bump","repo":"acme/widget","pr":7}}' +rc=0; in_sandbox claim deps-bump --pr 9 --timeout 10 >/dev/null 2>&1 || rc=$?; wait +check "a refused claim exits 2" "2" "$rc" +rm -f "$SPOOL"/*.json + +reply '{"ok":false,"error":"authorization denied: no topic store configured (fail closed)"}' +rc=0; in_sandbox release deps-bump --timeout 10 >/dev/null 2>&1 || rc=$?; wait +check "an error exits 1" "1" "$rc" +rm -f "$SPOOL"/*.json + +reply '{"ok":true,"claims":[{"key":"a","repo":"acme/widget","pr":3},{"key":"b","repo":"acme/widget","pr":4}]}' +check "list prints every claim" "2" "$(in_sandbox list --json --timeout 10 | python3 -c 'import json,sys; print(len(json.load(sys.stdin)))')"; wait +rm -f "$SPOOL"/*.json + +rc=0; in_sandbox get deps-bump --timeout 1 >/dev/null 2>&1 || rc=$? +check "no reply in time exits 1" "1" "$rc" + +echo "" +echo "=== $PASS passed, $FAIL failed ===" +[ "$FAIL" -eq 0 ] diff --git a/changelog.d/added-18572-sandbox-issue-topic-spool.md b/changelog.d/added-18572-sandbox-issue-topic-spool.md new file mode 100644 index 0000000000..d11c13cb4a --- /dev/null +++ b/changelog.d/added-18572-sandbox-issue-topic-spool.md @@ -0,0 +1 @@ +- **Sandboxed agents can open issues and keep topic claims.** The sandbox request spool now carries `issue` and `topic` requests as well as reviews, signed with the same per-kick key and attributed to the kick's own agent. `hive-open-issue` and the new `hive-topic` relay ship on every sandbox's `PATH` and write to the spool when they run in a sandbox. Issue requests go through the issue relay with all its gates (ACMM mode, lane write allowlist, repo scope, dedupe by title, canary scan, audit); topic requests land in the agent's own bead store, and `hive-topic claim ` without `--pr` claims the PR hive opens from the kick. `hive-review` is unchanged. ([#18572](https://github.com/hivecommons/hive/issues/18572), [#18573](https://github.com/hivecommons/hive/issues/18573)) diff --git a/src/Dockerfile b/src/Dockerfile index 5d6892c7aa..63c0bf45f4 100644 --- a/src/Dockerfile +++ b/src/Dockerfile @@ -832,6 +832,7 @@ COPY bin/hive-open-pr.sh /usr/local/bin/hive-open-pr COPY bin/hive-open-issue.sh /usr/local/bin/hive-open-issue COPY bin/hive-merge.sh /usr/local/bin/hive-merge COPY bin/hive-review.sh /usr/local/bin/hive-review +COPY bin/hive-topic.sh /usr/local/bin/hive-topic COPY bin/hive-push-branch.sh /usr/local/bin/hive-push-branch COPY bin/hive-baseline-check.sh /usr/local/bin/hive-baseline-check.sh COPY bin/gh-wrapper.sh /usr/local/bin/gh @@ -845,7 +846,7 @@ COPY bin/copilot-models.mjs /usr/local/bin/copilot-models.mjs COPY config/backends.conf /usr/local/etc/hive/backends.conf RUN chmod +x /usr/local/bin/ttyd-tmux.sh /usr/local/bin/hive-panes /usr/local/bin/gh-app-token.sh \ /usr/local/bin/hive-config.sh /usr/local/bin/agent-launch.sh /usr/local/bin/gh /usr/local/go/bin/go \ - /usr/local/bin/git-credential-hive.sh /usr/local/bin/hive-open-pr /usr/local/bin/hive-open-issue /usr/local/bin/hive-merge /usr/local/bin/hive-review \ + /usr/local/bin/git-credential-hive.sh /usr/local/bin/hive-open-pr /usr/local/bin/hive-open-issue /usr/local/bin/hive-merge /usr/local/bin/hive-review /usr/local/bin/hive-topic \ /usr/local/bin/hive-push-branch /usr/local/bin/hive-gh \ /usr/local/bin/hive-baseline-check.sh diff --git a/src/cmd/hive/boot_agents_deps.go b/src/cmd/hive/boot_agents_deps.go index e5fdbda951..4a2d52040c 100644 --- a/src/cmd/hive/boot_agents_deps.go +++ b/src/cmd/hive/boot_agents_deps.go @@ -82,7 +82,10 @@ type requestRelays struct { // that forge's merger. nil keeps every repo on the GitHub path. forgeMerge github.ForgeMergeSelector pushBranch github.PushBranchRequestAuthorizer - logger *slog.Logger + // topic applies a sandboxed kick's spooled topic request to its agent's + // bead store (hivecommons/hive#18572). nil refuses topic requests. + topic github.SandboxTopicHandler + logger *slog.Logger } func defaultBootAgentsDeps() bootAgentsDeps { @@ -105,6 +108,10 @@ func defaultBootAgentsDeps() bootAgentsDeps { if r.forgeMerge != nil { c.SetForgeMergeSelector(r.forgeMerge) } + // The sandbox spool's issue and topic kinds (#18572, #18573): + // the same issue gate the issue relay uses, installed before + // any watcher reads it. + c.SetSandboxSpoolRelays(r.issueOpen, r.topic) return joinDone( startRequestWatchers(ctx, requestwatch.New(c, r.prOpen, r.issueOpen, r.holdLabel, nil), r.logger), c.StartReviewRequestWatcher(ctx, r.review, nil), diff --git a/src/cmd/hive/boot_agents_deps_test.go b/src/cmd/hive/boot_agents_deps_test.go index 6beb6d72c9..be9f494bb9 100644 --- a/src/cmd/hive/boot_agents_deps_test.go +++ b/src/cmd/hive/boot_agents_deps_test.go @@ -205,7 +205,7 @@ func TestBootAgentsWith_UsableAppArmsRelaysAfterClientIsConfigured(t *testing.T) if relays == nil || relayClient != b.ghClient { t.Fatal("request relays not started on the App client") } - if f.relays.prOpen == nil || f.relays.issueOpen == nil || f.relays.review == nil || f.relays.merge == nil || f.relays.pushBranch == nil || f.relays.holdLabel == nil { + if f.relays.prOpen == nil || f.relays.issueOpen == nil || f.relays.review == nil || f.relays.merge == nil || f.relays.pushBranch == nil || f.relays.holdLabel == nil || f.relays.topic == nil { t.Fatalf("a relay was armed without its authorizer: %+v", *f.relays) } // The PR relay's authorizer is the manager's own gate: an unknown agent diff --git a/src/cmd/hive/github_client_config_test.go b/src/cmd/hive/github_client_config_test.go index 4a4944a3e9..f39fe87356 100644 --- a/src/cmd/hive/github_client_config_test.go +++ b/src/cmd/hive/github_client_config_test.go @@ -67,6 +67,10 @@ var clientSetterAllowlist = map[string]string{ "startRequestRelays installs the per-repo forge router on whichever client the merge relay " + "runs on, and every rebuilt client re-arms the relays (armRequestRelays), so the selector is " + "reinstalled alongside the merge authorizer it routes (#14113)", + "SetSandboxSpoolRelays": "owned by the request-relay supervisor, like SetMergeRequestPolicyGate: " + + "startRequestRelays installs the sandbox spool's issue authorizer and topic handler on whichever " + + "client the review relay (which drains the spool) runs on, and every rebuilt client re-arms the " + + "relays (armRequestRelays), so they are reinstalled with the relays (#18572, #18573)", "SetStageFactSink": "test seam: production uses the client's own default stage FactSink " + "(enforce-mode reconciler over stagelabels.Shared()), which every rebuilt client " + "constructs lazily on first use (ADR-0025, #13009)", diff --git a/src/cmd/hive/main.go b/src/cmd/hive/main.go index 8a7967ecf3..99be699706 100644 --- a/src/cmd/hive/main.go +++ b/src/cmd/hive/main.go @@ -2395,7 +2395,10 @@ func (b *boot) bootAgentsWith(deps bootAgentsDeps) { // gate a direct `git push` is governed by, so the relay grants // no privilege — it only changes which process performs the push. pushBranch: b.agentMgr.AuthorizePushBranch, - logger: b.logger, + // Sandbox spool topic requests (#18572) land in the agent's + // own bead store, read at call time. + topic: sandboxTopicHandler(func() map[string]*beads.Store { return b.beadStores }), + logger: b.logger, }) // The ACMM verdict is re-read on every (re)start; the dashboard's // OnACMMLevelChanged hook restarts this generation when a runtime diff --git a/src/cmd/hive/sandbox_topic_relay.go b/src/cmd/hive/sandbox_topic_relay.go new file mode 100644 index 0000000000..268f037a43 --- /dev/null +++ b/src/cmd/hive/sandbox_topic_relay.go @@ -0,0 +1,58 @@ +package main + +import ( + "errors" + + "github.com/hivecommons/hive/pkg/beads" + "github.com/hivecommons/hive/pkg/github" +) + +// sandboxTopicHandler applies a sandboxed kick's spooled topic request to +// its agent's own bead store (hivecommons/hive#18572), the store `bd topic` +// writes in-pod. The relay has already attributed the call to the kick's +// manifest agent; this only looks that agent's store up. stores is read at +// call time, not at wire time. +func sandboxTopicHandler(stores func() map[string]*beads.Store) github.SandboxTopicHandler { + return func(call github.SandboxTopicCall) github.SandboxTopicResponse { + var store *beads.Store + if stores != nil { + store = stores()[call.Agent] + } + if store == nil { + return github.SandboxTopicResponse{Error: "agent " + call.Agent + " has no bead store on this hive"} + } + switch call.Op { + case github.SandboxTopicOpClaim: + var claim beads.TopicClaim + var err error + if call.Pending { + claim, err = store.ClaimTopicForBranch(call.Agent, call.Key, call.Repo, call.Branch) + } else { + claim, err = store.ClaimTopic(call.Agent, beads.TopicClaim{Key: call.Key, Repo: call.Repo, PR: call.PR, Branch: call.Branch}) + } + if err != nil { + resp := github.SandboxTopicResponse{Error: err.Error(), Refused: errors.Is(err, beads.ErrTopicClaimed)} + if resp.Refused { + resp.Claim = &claim + } + return resp + } + return github.SandboxTopicResponse{OK: true, Claim: &claim, Pending: claim.PR == 0} + case github.SandboxTopicOpGet: + claim, ok := store.Topic(call.Key) + if !ok { + return github.SandboxTopicResponse{Refused: true, Error: "no open claim for topic " + call.Key} + } + return github.SandboxTopicResponse{OK: true, Claim: &claim, Pending: claim.PR == 0} + case github.SandboxTopicOpList: + return github.SandboxTopicResponse{OK: true, Claims: store.OpenTopics()} + case github.SandboxTopicOpRelease: + released, err := store.ReleaseTopic(call.Key, beads.TopicStateReleased) + if err != nil { + return github.SandboxTopicResponse{Error: err.Error()} + } + return github.SandboxTopicResponse{OK: true, Released: released} + } + return github.SandboxTopicResponse{Error: "unknown topic op " + call.Op} + } +} diff --git a/src/cmd/hive/topic_claims.go b/src/cmd/hive/topic_claims.go index 8bd8f77552..8e472041cb 100644 --- a/src/cmd/hive/topic_claims.go +++ b/src/cmd/hive/topic_claims.go @@ -115,6 +115,10 @@ func syncTopicClaims(stores map[string]*beads.Store, actionable *github.Actionab continue } for _, claim := range store.OpenTopics() { + if claim.PR == 0 { + bindPendingTopicClaim(store, name, prs, claim, logger) + continue + } pr, ok := findTopicPR(prs, claim) if !ok { continue @@ -133,6 +137,39 @@ func syncTopicClaims(stores map[string]*beads.Store, actionable *github.Actionab } } +// bindPendingTopicClaim binds a pending claim (made from a sandboxed kick +// before hive opened its PR, hivecommons/hive#18572) to the PR whose head +// branch is the claim's branch. An open PR wins over a closed one; a PR that +// is already terminal still binds, and the next cycle releases the claim. +func bindPendingTopicClaim(store *beads.Store, agent string, prs []github.PullRequest, claim beads.TopicClaim, logger *slog.Logger) { + if claim.Branch == "" { + return + } + number := 0 + for _, pr := range prs { + if pr.HeadRef != claim.Branch || pr.Number <= 0 || !beads.SameTopicRepo(pr.Repo, claim.Repo) { + continue + } + if number == 0 || topicStateForPR(pr) == "" { + number = pr.Number + } + } + if number == 0 { + return + } + n, err := store.BindTopicBranch(claim.Repo, claim.Branch, number) + if logger == nil { + return + } + if err != nil { + logger.Warn("pending topic claim bind failed", "agent", agent, "topic", claim.Key, "error", err) + return + } + if n > 0 { + logger.Info("pending topic claim bound to its PR", "agent", agent, "topic", claim.Key, "repo", claim.Repo, "pr", number) + } +} + // findTopicPR returns the snapshot's copy of the claim's PR. A terminal copy // wins over an open one, since Attributed can carry a PR that left Items. func findTopicPR(prs []github.PullRequest, claim beads.TopicClaim) (github.PullRequest, bool) { diff --git a/src/cmd/hive/topic_claims_test.go b/src/cmd/hive/topic_claims_test.go index afdb8e1538..66fcef5156 100644 --- a/src/cmd/hive/topic_claims_test.go +++ b/src/cmd/hive/topic_claims_test.go @@ -120,3 +120,77 @@ func TestSyncTopicClaimsFollowsRenameAndReleases(t *testing.T) { syncTopicClaims(stores, nil, quietTopicLogger()) syncTopicClaims(nil, actionable, quietTopicLogger()) } + +// A pending claim from a sandboxed kick (hivecommons/hive#18572) binds to the +// PR opened from its branch, preferring an open PR, and stays pending until +// one appears. +func TestSyncTopicClaimsBindsPendingClaims(t *testing.T) { + stores := topicTestStores(t, "deps") + s := stores["deps"] + _, _ = s.ClaimTopicForBranch("deps", "bump", "acme/widget", "hive/deps-1") + _, _ = s.ClaimTopicForBranch("deps", "waiting", "acme/widget", "hive/deps-2") + + actionable := &github.ActionableResult{PRs: github.PRResult{ + Items: []github.PullRequest{{Repo: "widget", Number: 21, HeadRef: "hive/deps-1", State: "open"}}, + Attributed: []github.PullRequest{{Repo: "acme/widget", Number: 20, HeadRef: "hive/deps-1", State: "closed"}}, + }} + syncTopicClaims(stores, actionable, quietTopicLogger()) + + open := map[string]beads.TopicClaim{} + for _, c := range s.OpenTopics() { + open[c.Key] = c + } + if open["bump"].PR != 21 { + t.Errorf("pending claim bound to %d, want the open PR 21", open["bump"].PR) + } + if open["waiting"].PR != 0 { + t.Errorf("claim with no PR on its branch was bound: %+v", open["waiting"]) + } + bindPendingTopicClaim(s, "deps", nil, beads.TopicClaim{Key: "x"}, nil) // no branch: no-op +} + +func TestSandboxTopicHandler(t *testing.T) { + stores := topicTestStores(t, "deps") + h := sandboxTopicHandler(func() map[string]*beads.Store { return stores }) + + if r := h(github.SandboxTopicCall{Agent: "ghost", Op: github.SandboxTopicOpList}); r.OK || r.Error == "" { + t.Errorf("unknown agent = %+v, want an error", r) + } + if r := sandboxTopicHandler(nil)(github.SandboxTopicCall{Agent: "deps", Op: github.SandboxTopicOpList}); r.OK { + t.Errorf("no stores = %+v, want an error", r) + } + + r := h(github.SandboxTopicCall{Agent: "deps", Op: github.SandboxTopicOpClaim, Key: "bump", Repo: "acme/widget", Branch: "hive/deps-1", Pending: true}) + if !r.OK || !r.Pending || r.Claim == nil || r.Claim.Actor != "deps" { + t.Fatalf("pending claim = %+v", r) + } + r = h(github.SandboxTopicCall{Agent: "deps", Op: github.SandboxTopicOpClaim, Key: "rel", Repo: "acme/widget", PR: 4}) + if !r.OK || r.Pending || r.Claim.PR != 4 { + t.Fatalf("claim = %+v", r) + } + r = h(github.SandboxTopicCall{Agent: "deps", Op: github.SandboxTopicOpClaim, Key: "rel", Repo: "acme/widget", PR: 5}) + if r.OK || !r.Refused || r.Claim == nil || r.Claim.PR != 4 { + t.Fatalf("second claim = %+v, want refused naming #4", r) + } + if r := h(github.SandboxTopicCall{Agent: "deps", Op: github.SandboxTopicOpClaim, Key: "x"}); r.OK || r.Refused { + t.Errorf("invalid claim = %+v, want a plain error", r) + } + if r := h(github.SandboxTopicCall{Agent: "deps", Op: github.SandboxTopicOpGet, Key: "bump"}); !r.OK || !r.Pending { + t.Errorf("get = %+v", r) + } + if r := h(github.SandboxTopicCall{Agent: "deps", Op: github.SandboxTopicOpGet, Key: "none"}); r.OK || !r.Refused { + t.Errorf("get missing = %+v, want refused", r) + } + if r := h(github.SandboxTopicCall{Agent: "deps", Op: github.SandboxTopicOpList}); !r.OK || len(r.Claims) != 2 { + t.Errorf("list = %+v", r) + } + if r := h(github.SandboxTopicCall{Agent: "deps", Op: github.SandboxTopicOpRelease, Key: "bump"}); !r.OK || !r.Released { + t.Errorf("release = %+v", r) + } + if r := h(github.SandboxTopicCall{Agent: "deps", Op: github.SandboxTopicOpRelease, Key: ""}); r.OK { + t.Errorf("release with no key = %+v", r) + } + if r := h(github.SandboxTopicCall{Agent: "deps", Op: "steal"}); r.OK { + t.Errorf("unknown op = %+v", r) + } +} diff --git a/src/crux/agent/sandbox_executor.go b/src/crux/agent/sandbox_executor.go index 69d218e3d1..836edb2e57 100644 --- a/src/crux/agent/sandbox_executor.go +++ b/src/crux/agent/sandbox_executor.go @@ -218,7 +218,7 @@ func (e *SandboxExecutor) Run(ctx context.Context, spec SandboxKickSpec) (Sandbo } // A spool the relay cannot see is a lost review, not a failed kick: the // rest of the run is still worth doing, so log and go on without one. - spool, spoolErr := e.openReviewSpool(spec.ReviewSpoolDir, spec.Agent, workspace) + spool, spoolErr := e.openReviewSpool(spec.ReviewSpoolDir, spec.Agent, workspace, spoolTargetOf(spec)) if spoolErr != nil && e.Logger != nil { e.Logger.Warn("sandbox review spool unavailable; the sandbox cannot post reviews this kick", "agent", spec.Agent, "error", spoolErr) } diff --git a/src/crux/agent/sandbox_review_spool.go b/src/crux/agent/sandbox_review_spool.go index f45cfa2873..c0a3b797bc 100644 --- a/src/crux/agent/sandbox_review_spool.go +++ b/src/crux/agent/sandbox_review_spool.go @@ -33,9 +33,28 @@ type reviewSpoolHandle struct { entry crux.SandboxReviewSpool } +// spoolKickTarget is what the manifest records about the kick's PR: the +// target repo and the branch hive opens the PR from. A review-only kick +// opens none, so it records no branch and a topic claim cannot bind to one. +type spoolKickTarget struct { + repo string + branch string +} + +func spoolTargetOf(spec SandboxKickSpec) spoolKickTarget { + t := spoolKickTarget{branch: spec.Branch} + if spec.Org != "" && spec.Repo != "" { + t.repo = spec.Org + "/" + spec.Repo + } + if spec.reviewOnly() { + t.branch = "" + } + return t +} + // openReviewSpool creates the workspace spool and registers it. A nil handle // with a nil error means spooling is not configured for this kick. -func (e *SandboxExecutor) openReviewSpool(manifestDir, agentName, workspace string) (*reviewSpoolHandle, error) { +func (e *SandboxExecutor) openReviewSpool(manifestDir, agentName, workspace string, target spoolKickTarget) (*reviewSpoolHandle, error) { if strings.TrimSpace(manifestDir) == "" { return nil, nil } @@ -61,7 +80,10 @@ func (e *SandboxExecutor) openReviewSpool(manifestDir, agentName, workspace stri } h := &reviewSpoolHandle{ manifest: filepath.Join(manifestDir, fmt.Sprintf("%s-%d.json", sanitizeBranchPart(agentName), time.Now().UnixNano())), - entry: crux.SandboxReviewSpool{Agent: agentName, Spool: spool, Key: hex.EncodeToString(key), StartedAt: e.now().UTC()}, + entry: crux.SandboxReviewSpool{ + Agent: agentName, Repo: target.repo, Branch: target.branch, + Spool: spool, Key: hex.EncodeToString(key), StartedAt: e.now().UTC(), + }, } if err := h.write(); err != nil { return nil, err @@ -71,14 +93,19 @@ func (e *SandboxExecutor) openReviewSpool(manifestDir, agentName, workspace stri // sandboxEnv is what the sandbox needs to use the spool: hive-review writes // to HIVE_REVIEW_REQUEST_DIR when it is set, and MACs each request with -// HIVE_REVIEW_SPOOL_KEY. +// HIVE_REVIEW_SPOOL_KEY. hive-open-issue and hive-topic read the generic +// HIVE_SANDBOX_REQUEST_DIR and HIVE_SANDBOX_SPOOL_KEY (#18572, #18573), +// which carry the same values. func (h *reviewSpoolHandle) sandboxEnv() map[string]string { if h == nil { return nil } + dir := path.Join(sandbox.DefaultWorkspaceMount, crux.SandboxReviewSpoolRelPath) return map[string]string{ - crux.SandboxReviewSpoolEnv: path.Join(sandbox.DefaultWorkspaceMount, crux.SandboxReviewSpoolRelPath), + crux.SandboxReviewSpoolEnv: dir, crux.SandboxReviewSpoolKeyEnv: h.entry.Key, + crux.SandboxSpoolEnv: dir, + crux.SandboxSpoolKeyEnv: h.entry.Key, } } diff --git a/src/crux/agent/sandbox_review_spool_test.go b/src/crux/agent/sandbox_review_spool_test.go index dc928c3401..4a0fe69298 100644 --- a/src/crux/agent/sandbox_review_spool_test.go +++ b/src/crux/agent/sandbox_review_spool_test.go @@ -297,3 +297,47 @@ func TestJobRuntimeKickRegistersReviewSpoolUnderWorkDir(t *testing.T) { t.Errorf("manifests under %s during the job = %+v, want one for reviewer", manifestDir, *during) } } + +// The manifest records the kick's repo and the branch hive opens its PR +// from, so a topic claim without a PR number can bind to that PR +// (hivecommons/hive#18572); the sandbox gets the generic spool env names +// hive-open-issue and hive-topic read, carrying the same values. +func TestSandboxExecutorManifestRecordsKickTargetAndGenericEnv(t *testing.T) { + manifestDir := filepath.Join(t.TempDir(), "sandbox-review-spools") + launcher, env, during := spoolProbe(manifestDir) + exec := &SandboxExecutor{Runner: &sandboxFakeRunner{}, Launcher: launcher} + if _, err := exec.Run(context.Background(), SandboxKickSpec{ + Agent: "deps-bump", AgentConfig: configSnapshot{Backend: "claude"}, Message: "bump deps", + Org: "torch-spyre", Repo: "spyre-inference", Branch: "hive/deps-bump-1", WorkspaceDir: t.TempDir(), Image: "img", + ReviewSpoolDir: manifestDir, + }); err != nil { + t.Fatalf("Run: %v", err) + } + if len(*during) != 1 { + t.Fatalf("manifests = %d, want 1", len(*during)) + } + m := (*during)[0] + if m.Repo != "torch-spyre/spyre-inference" || m.Branch != "hive/deps-bump-1" { + t.Errorf("manifest target = %q %q, want the kick's repo and branch", m.Repo, m.Branch) + } + if (*env)[crux.SandboxSpoolEnv] != (*env)[crux.SandboxReviewSpoolEnv] || (*env)[crux.SandboxSpoolEnv] == "" { + t.Errorf("%s = %q, want the spool path %q", crux.SandboxSpoolEnv, (*env)[crux.SandboxSpoolEnv], (*env)[crux.SandboxReviewSpoolEnv]) + } + if (*env)[crux.SandboxSpoolKeyEnv] != m.Key { + t.Errorf("%s does not carry the run key", crux.SandboxSpoolKeyEnv) + } + if sandbox.IsCredentialName(crux.SandboxSpoolKeyEnv) { + t.Errorf("%s would be stripped by the launchers' credential filter", crux.SandboxSpoolKeyEnv) + } +} + +// A review-only kick opens no PR, so its manifest names no branch for a +// pending topic claim to bind to. +func TestSpoolTargetOfReviewOnlyKickHasNoBranch(t *testing.T) { + if got := spoolTargetOf(SandboxKickSpec{Org: "o", Repo: "r", Branch: "pr-7", ReviewPRs: []int{7}}); got.repo != "o/r" || got.branch != "" { + t.Errorf("review-only target = %+v", got) + } + if got := spoolTargetOf(SandboxKickSpec{Branch: "b"}); got.repo != "" || got.branch != "b" { + t.Errorf("target without org/repo = %+v", got) + } +} diff --git a/src/crux/kubejob/relays.go b/src/crux/kubejob/relays.go index 0bb8ed76b1..628968a494 100644 --- a/src/crux/kubejob/relays.go +++ b/src/crux/kubejob/relays.go @@ -117,7 +117,7 @@ func (l *Launcher) resolveRelays(ctx context.Context, ns string) (*relayPlan, er } pod, err := l.selfPod(ctx, ns) if err != nil { - return nil, fmt.Errorf("kubejob: reading hive's own pod to supply the sandbox relays (hive-review): %w; set job.relay_image and job.relay_run_as_user, or job.relay_image: %s to skip the relays", err, RelayImageNone) + return nil, fmt.Errorf("kubejob: reading hive's own pod to supply the sandbox relays (%s): %w; set job.relay_image and job.relay_run_as_user, or job.relay_image: %s to skip the relays", strings.Join(sandbox.RelayNames, ", "), err, RelayImageNone) } if plan.image == "" { if plan.image = pod.hiveImage(); plan.image == "" { diff --git a/src/crux/kubejob/relays_test.go b/src/crux/kubejob/relays_test.go index f0af8fea51..8947ceb434 100644 --- a/src/crux/kubejob/relays_test.go +++ b/src/crux/kubejob/relays_test.go @@ -69,7 +69,7 @@ func TestRelayJobSpecOnVanillaCluster(t *testing.T) { `"name":"hive-relays"`, `"image":"` + testHiveDigest + `"`, // the running digest, not the :stable tag `"imagePullPolicy":"IfNotPresent"`, - `"command":["cp","/usr/local/bin/hive-review","/opt/hive/relays/"]`, + `"command":["cp","/usr/local/bin/hive-review","/usr/local/bin/hive-open-issue","/usr/local/bin/hive-topic","/opt/hive/relays/"]`, `"runAsNonRoot":true`, `"readOnlyRootFilesystem":true`, `"allowPrivilegeEscalation":false`, diff --git a/src/crux/sandbox/relays.go b/src/crux/sandbox/relays.go index ad7064f179..5463d5c3c0 100644 --- a/src/crux/sandbox/relays.go +++ b/src/crux/sandbox/relays.go @@ -11,9 +11,9 @@ import ( // Sandbox relays (#18512). // // A sandboxed kick runs in the project's own image, which knows nothing about -// hive. The relays an agent needs to hand work back to hive (today only -// hive-review, which writes a signed request into the kick's review spool, -// #14986) are therefore supplied by hive itself on every launch rather than +// hive. The relays an agent needs to hand work back to hive (hive-review, +// hive-open-issue and hive-topic, each of which writes a signed request into +// the kick's request spool, #14986, #18572, #18573) are therefore supplied by hive itself on every launch rather than // baked into the project image: the launcher puts hive's own copy in a // read-only directory at RelayMountPath and prepends that directory to PATH. // Hive's copy always wins over one the image carries, because the relay and @@ -42,7 +42,7 @@ const ( ) // RelayNames are the sandbox-safe relays hive supplies to every sandbox. -var RelayNames = []string{"hive-review"} +var RelayNames = []string{"hive-review", "hive-open-issue", "hive-topic"} // RelayRequirements are the interpreters the relays need inside the image. var RelayRequirements = []string{"bash", "python3"} diff --git a/src/crux/sandbox/relays_test.go b/src/crux/sandbox/relays_test.go index 1bfba83bfa..7ae442bcd8 100644 --- a/src/crux/sandbox/relays_test.go +++ b/src/crux/sandbox/relays_test.go @@ -23,10 +23,10 @@ func writeRelaySource(t *testing.T) string { } func TestRelayNamesAreSandboxSafeOnly(t *testing.T) { - // hive-review is the only relay with a sandbox spool today (#14986); a + // Only relays that write the sandbox spool (#14986, #18572, #18573); a // relay that needs the pod-local drop boxes must not be added here. - if strings.Join(RelayNames, ",") != "hive-review" { - t.Errorf("RelayNames = %v, want only hive-review", RelayNames) + if strings.Join(RelayNames, ",") != "hive-review,hive-open-issue,hive-topic" { + t.Errorf("RelayNames = %v, want hive-review, hive-open-issue and hive-topic", RelayNames) } } @@ -116,9 +116,11 @@ func TestStageRelaysCopiesFromEnvDir(t *testing.T) { if err != nil { t.Fatal(err) } - info, err := os.Stat(filepath.Join(dir, "hive-review")) - if err != nil || info.Mode().Perm() != relayFileMode { - t.Fatalf("staged relay = %v, %v, want mode %o", info, err, relayFileMode) + for _, name := range []string{"hive-review", "hive-open-issue", "hive-topic"} { + info, err := os.Stat(filepath.Join(dir, name)) + if err != nil || info.Mode().Perm() != relayFileMode { + t.Fatalf("staged relay %s = %v, %v, want mode %o", name, info, err, relayFileMode) + } } cleanup() if _, err := os.Stat(dir); !os.IsNotExist(err) { @@ -191,6 +193,13 @@ func TestPodmanRunStagesAndMountsRelays(t *testing.T) { if mount == "" { t.Fatalf("podman args carry no relay mount:\n%s", raw) } + // Every relay is on the sandbox PATH: the wrapper prepends the mount, + // and the preflight names each one it supplies. + for _, want := range []string{RelayMountPath, `PATH="$relay_dir:$PATH"`, "hive-review, hive-open-issue, hive-topic"} { + if !strings.Contains(string(raw), want) { + t.Errorf("podman args missing %q:\n%s", want, raw) + } + } if _, err := os.Stat(mount); !os.IsNotExist(err) { t.Errorf("the staged relay dir %s must be removed after the run: %v", mount, err) } diff --git a/src/crux/sandbox_review_spool.go b/src/crux/sandbox_review_spool.go index 6fde6bcf1d..8bbb720118 100644 --- a/src/crux/sandbox_review_spool.go +++ b/src/crux/sandbox_review_spool.go @@ -28,9 +28,48 @@ const ( // sandbox. The name must not contain TOKEN or SECRET: the launchers // strip those (sandbox.IsCredentialName). SandboxReviewSpoolKeyEnv = "HIVE_REVIEW_SPOOL_KEY" - sandboxReviewSpoolsDir = "sandbox-review-spools" + // SandboxSpoolEnv and SandboxSpoolKeyEnv are the generic names of the + // same two values (hivecommons/hive#18572, #18573): the spool now carries + // issue and topic requests too, so hive-open-issue and hive-topic read + // these first. Hive sets both pairs; hive-review keeps reading the old + // names so an older copy of it still works. + SandboxSpoolEnv = "HIVE_SANDBOX_REQUEST_DIR" + SandboxSpoolKeyEnv = "HIVE_SANDBOX_SPOOL_KEY" + sandboxReviewSpoolsDir = "sandbox-review-spools" ) +// Sandbox request kinds (hivecommons/hive#18572, #18573). The spool started +// as hive-review's alone; an envelope with no kind is still a review. Each +// kind is staged apart and handed to the relay handler that already serves +// it in-pod, with every gate that handler applies. +const ( + // SandboxSpoolKindReview is a PR review (hive-review), the original kind. + SandboxSpoolKindReview = "review" + // SandboxSpoolKindIssue is a hive-open-issue request: open, comment, + // claim, close, label or request-review. + SandboxSpoolKindIssue = "issue" + // SandboxSpoolKindTopic is a hive-topic request against the agent's own + // topic claims (bd topic). + SandboxSpoolKindTopic = "topic" +) + +// SandboxSpoolKinds lists every kind the relay accepts, review first. +var SandboxSpoolKinds = []string{SandboxSpoolKindReview, SandboxSpoolKindIssue, SandboxSpoolKindTopic} + +// NormalizeSandboxSpoolKind maps an envelope's kind to a known kind: empty +// is a review. ok is false for a kind the relay does not serve. +func NormalizeSandboxSpoolKind(kind string) (string, bool) { + if kind == "" { + return SandboxSpoolKindReview, true + } + for _, k := range SandboxSpoolKinds { + if kind == k { + return k, true + } + } + return "", false +} + // SandboxReviewSpoolsDir is where the sandbox executor writes one manifest // per kick and the review relay reads them. Host and runtime share it // through /data, exactly as they share SandboxRunsDir. @@ -47,8 +86,15 @@ func SandboxReviewSpoolsDir(workDir string) string { // is group-writable, so knowing WHICH workspace a request came from does not // say WHO wrote it; the relay accepts only a request MACed with this key // (SandboxReviewMAC), which nothing outside this run's sandbox holds. +// +// Repo and Branch name the kick's target ("owner/name") and the branch hive +// opens the kick's PR from. A topic claim made without a PR number binds to +// that PR once it exists (hivecommons/hive#18572). Branch is empty for a +// kick that never opens a PR (a review-only kick). type SandboxReviewSpool struct { Agent string `json:"agent"` + Repo string `json:"repo,omitempty"` + Branch string `json:"branch,omitempty"` Spool string `json:"spool"` Key string `json:"key"` StartedAt time.Time `json:"started_at"` @@ -56,8 +102,10 @@ type SandboxReviewSpool struct { } // SandboxReviewEnvelope is what a sandbox writes to its spool: the request -// JSON, verbatim, and its MAC under the kick's spool key. +// JSON, verbatim, its kind, and its MAC under the kick's spool key +// (SandboxSpoolMAC). An envelope without a kind is a review. type SandboxReviewEnvelope struct { + Kind string `json:"kind,omitempty"` Request string `json:"request"` MAC string `json:"mac"` } @@ -69,3 +117,18 @@ func SandboxReviewMAC(key string, request []byte) string { m.Write(request) return hex.EncodeToString(m.Sum(nil)) } + +// SandboxSpoolMAC is the MAC an envelope of kind carries. A review (kind +// empty or "review") keeps SandboxReviewMAC, so an older hive-review still +// verifies. Every other kind MACs "\n", which binds the kind: +// a request signed as one kind cannot be replayed as another. The relays in +// bin/ compute the same value. +func SandboxSpoolMAC(key, kind string, request []byte) string { + if kind == "" || kind == SandboxSpoolKindReview { + return SandboxReviewMAC(key, request) + } + m := hmac.New(sha256.New, []byte(key)) + m.Write([]byte(kind + "\n")) + m.Write(request) + return hex.EncodeToString(m.Sum(nil)) +} diff --git a/src/crux/sandbox_review_spool_test.go b/src/crux/sandbox_review_spool_test.go new file mode 100644 index 0000000000..7eec0efcea --- /dev/null +++ b/src/crux/sandbox_review_spool_test.go @@ -0,0 +1,38 @@ +package crux + +import "testing" + +// The spool's kinds (hivecommons/hive#18572, #18573): a kindless envelope is +// a review whose MAC is unchanged, so an older hive-review still verifies; +// every other kind binds its name into the MAC. +func TestSandboxSpoolMACBindsKind(t *testing.T) { + req := []byte(`{"repo":"o/r"}`) + review := SandboxReviewMAC("k", req) + if SandboxSpoolMAC("k", "", req) != review || SandboxSpoolMAC("k", SandboxSpoolKindReview, req) != review { + t.Fatal("a review's MAC must stay SandboxReviewMAC") + } + issue := SandboxSpoolMAC("k", SandboxSpoolKindIssue, req) + topic := SandboxSpoolMAC("k", SandboxSpoolKindTopic, req) + if issue == review || topic == review || issue == topic { + t.Fatal("each kind must MAC differently") + } + if issue != SandboxReviewMAC("k", []byte("issue\n"+string(req))) { + t.Error("the issue MAC is HMAC(key, \"issue\\n\" + request), as bin/hive-open-issue.sh computes") + } + if SandboxSpoolMAC("other", SandboxSpoolKindIssue, req) == issue { + t.Error("the MAC must depend on the key") + } +} + +func TestNormalizeSandboxSpoolKind(t *testing.T) { + for in, want := range map[string]string{"": SandboxSpoolKindReview, "review": SandboxSpoolKindReview, "issue": SandboxSpoolKindIssue, "topic": SandboxSpoolKindTopic} { + if got, ok := NormalizeSandboxSpoolKind(in); !ok || got != want { + t.Errorf("NormalizeSandboxSpoolKind(%q) = %q, %v, want %q", in, got, ok, want) + } + } + for _, bad := range []string{"merge", "Issue", "pr"} { + if _, ok := NormalizeSandboxSpoolKind(bad); ok { + t.Errorf("NormalizeSandboxSpoolKind(%q) accepted", bad) + } + } +} diff --git a/src/docs/beads-cli.md b/src/docs/beads-cli.md index 34dea591c3..9a4d4038ce 100644 --- a/src/docs/beads-cli.md +++ b/src/docs/beads-cli.md @@ -48,6 +48,11 @@ bd topic release deps-bump terminal PR, and on each eval cycle's snapshot. The same eval-cycle pass updates `topic_branch` when the PR's head branch was renamed. `bd topic release` records `released`. `bd reset` closes open claims too. +- A sandboxed kick has no `bd`: it runs `hive-topic` with the same arguments, + which hive applies to the agent's store through the sandbox request spool. + `hive-topic claim ` without `--pr` claims the PR hive opens from the + kick; the claim is pending until that PR exists. See + [sandbox-isolation.md](sandbox-isolation.md#issue-and-topic-requests-through-the-spool). - The kick shows an agent its open claims in a `📌 Topic claims` block, also available to kick templates as `${TOPIC_CLAIMS}` (which renders the usage lines even when the agent holds no claim). The FIX-BEFORE-NEW and hold-gated diff --git a/src/docs/notifications.md b/src/docs/notifications.md index b8eef9e64f..2805a83c09 100644 --- a/src/docs/notifications.md +++ b/src/docs/notifications.md @@ -519,7 +519,7 @@ without a class. Review submissions and review requests are always counted as | `github.(*Client).bindProwLgtm` | `attention` | | | `github.(*Client).commentOnHumanFiledTwin` | `attention` | | | `github.(*Client).createIssue` | `attention` | | -| `github.(*Client).handleOneIssueRequest` | `attention` | | +| `github.(*Client).handleOneIssueRequestFrom` | `attention` | | | `github.(*Client).parkClankerRequestedPR` | `attention` | | | `github.(*Client).redirectToExistingPR` | `attention` | | | `github.(*Client).relayScannerNote` | `attention` | scanner PR note: an unchanged or edited note records a suppressed comment (#18451) | diff --git a/src/docs/sandbox-isolation.md b/src/docs/sandbox-isolation.md index 5a7b30eebe..7cece597da 100644 --- a/src/docs/sandbox-isolation.md +++ b/src/docs/sandbox-isolation.md @@ -217,7 +217,7 @@ agents: sandbox: enabled: true runtime: job - image: registry.example/project-dev:latest # needs bash and python3; hive supplies hive-review + image: registry.example/project-dev:latest # needs bash and python3; hive supplies its relays job: node_selector: { accelerator: spyre } resources: @@ -232,6 +232,17 @@ A sandbox cannot reach the pod-local review relay (`/var/run/hive-metrics/review 4. The manifest says which workspace a request came from, not who wrote it: the spool is group-writable, so any agent sharing hive's group could drop a file into it. So hive also mints a random 256-bit **spool key** for each kick, keeps it in the manifest, and hands it to the sandbox only through `HIVE_REVIEW_SPOOL_KEY` in its environment (the Podman launcher passes env by name, so the value never appears on a command line). `hive-review` writes `{"request": "", "mac": ""}`. The relay stages only a request whose MAC matches this run's key and that this spool has not already accepted. An unsigned request, one signed with another run's key, or a copy of an accepted one is set aside as `.rejected`, logged at WARN, and answered with a `rejected:` result file. This stands in for the file-owner check the pod-local relay does, which can't apply here because a Job's uid is not an agent's. 5. The result is copied back to `.result.json` in the spool, where the sandboxed agent reads it exactly as it would in-pod. A bad `--verdict-file` is refused with the validator's message, so the agent can fix the verdict and resubmit within the same kick. +#### Issue and topic requests through the spool + +The spool is typed ([#18572](https://github.com/hivecommons/hive/issues/18572), [#18573](https://github.com/hivecommons/hive/issues/18573)). Besides reviews, it carries two more kinds of request, each run through the handler that serves it in-pod: + +- **`issue`**: `hive-open-issue` (open, comment, claim, close, label, request-review). The request runs through the issue relay's own handler. The repository gate, the ACMM `CanCreateIssues` gate, the lane write allowlist, repo pause, repo scope, dedupe by exact title, consolidation, the canary scan and the `agent_issue_created` (or comment, claim, close, label) audit all apply unchanged, for the manifest's agent. +- **`topic`**: `hive-topic` (`claim`, `get`, `list`, `release`, the same arguments as `bd topic`). The request is applied to the manifest agent's own bead store, the one `bd topic` writes in-pod. A claim needs a repo the agent is scoped to. `hive-topic` waits for hive's reply and prints it, exiting 0 on success, 2 when the topic is held through another PR (or `get` finds none), and 1 on any other error or when no reply arrives in time (`--timeout`, default 120 seconds; `--no-wait` returns at once). + +Hive sets the spool path and key under two more names, `HIVE_SANDBOX_REQUEST_DIR` and `HIVE_SANDBOX_SPOOL_KEY`, which `hive-open-issue` and `hive-topic` read first. Both fall back to the `HIVE_REVIEW_*` names, and both write to the pod-local drop box (or, for `hive-topic`, run `bd topic`) when no spool key is set. The envelope names its kind, `{"kind": "issue", "request": "...", "mac": "..."}`, and every kind but `review` MACs `"\n" + request`. So a request signed as one kind is refused as any other, and the replay guard covers all kinds. An envelope with no `kind` is a review MACed over the request alone, so `hive-review` is unchanged and an older copy still works. An unknown kind is set aside as `.rejected`. + +**A topic claim for the PR hive opens.** A sandboxed kick has no PR number: hive opens the PR from the kick's branch after the sandbox exits. So the manifest also records the kick's repo and branch, and `hive-topic claim ` without `--pr` records a **pending** claim on that branch. On the next eval cycle the governor binds it to the PR whose head branch it is (an open PR wins over a closed one), and the claim then follows the PR like any other: hive releases it when the PR merges or closes. Until then the kick's topic block shows the claim as "PR not opened yet". A pending claim left by a kick that opened no PR moves to the next kick's branch when that kick claims the topic again. A review-only kick opens no PR, so it records no branch, and a claim from it needs `--pr`. + While any spool is registered, the watcher scans on its short poll interval: a Job writes its spool over a shared volume, often from another node, where no file-change notification reaches the hive pod. After the sandbox exits, the kick waits up to a minute for the relay to take any request still in the spool, because a run-stage worktree is removed when the kick returns. The manifest is dropped once the kick has ended and nothing is left to deliver, or after 24 hours. The relay touches the spool only through an `os.Root` opened at the workspace. A symlink the sandbox plants can't redirect a read, rename or write outside its own workspace, and a request file that is a symlink, FIFO or oversized is set aside as `.rejected`. The relay ignores a manifest directory or manifest that is not owned by its own uid or is group- or world-writable, and a manifest without a spool key. In the sidecar runtime (`CRUX_MODE=sidecar`), the runtime and hive must therefore share both `/data` and a uid. On `sandbox.runtime: job`, the key is in the Job's pod spec, so anything that can read Jobs or Pods in the namespace through the Kubernetes API can read it; agents must not hold that permission. A group member can still delete or alter a request in the spool, which loses the review but cannot post one. @@ -240,14 +251,14 @@ Out of scope: the quality and deps lanes are unchanged, and nothing inside a Job ### Hive's relays inside the sandbox -The project image does not need to carry `hive-review` or any other hive script ([#18512](https://github.com/hivecommons/hive/issues/18512)). On every sandboxed agent kick, hive supplies its **sandbox-safe relays** itself, read-only at `/opt/hive/relays`, and runs the kick command through a short `/bin/sh` preflight that prepends that directory to `PATH`. Today the only relay is `hive-review`, which works from a sandbox through the review spool above. Relays that need the pod-local drop boxes (`hive-open-pr`, `hive-open-issue`, `hive-merge`, `hive-push-branch`) are left out until they have a spool of their own. +The project image does not need to carry `hive-review` or any other hive script ([#18512](https://github.com/hivecommons/hive/issues/18512)). On every sandboxed agent kick, hive supplies its **sandbox-safe relays** itself, read-only at `/opt/hive/relays`, and runs the kick command through a short `/bin/sh` preflight that prepends that directory to `PATH`. The relays are `hive-review`, `hive-open-issue` and `hive-topic`, which work from a sandbox through the spool above. Relays that need the pod-local drop boxes (`hive-open-pr`, `hive-merge`, `hive-push-branch`) are left out until they have a spool of their own. -- **Job runtime.** The Job gets an init container, `hive-relays`, from the **running hive image by digest**. Hive reads its own pod (`$POD_NAME`, else `$HOSTNAME`) through the `pods` get grant the launcher already has, and takes the hive container's `imageID`. When the runtime reports no pullable digest, hive falls back to the image as written. Hive's own `imagePullSecrets` are copied onto the Job so the node can pull it. The init container runs `cp /usr/local/bin/hive-review /opt/hive/relays/` into a 16Mi `emptyDir`, and the kick container mounts that `emptyDir` read-only. The init container passes OpenShift's restricted SCC and the Kubernetes `restricted` Pod Security Standard: `runAsNonRoot`, `readOnlyRootFilesystem`, `allowPrivilegeEscalation: false`, every capability dropped, `RuntimeDefault` seccomp. On OpenShift (hive's pod carries the `openshift.io/scc` annotation) it sets no uid, and the SCC assigns one from the namespace range. Elsewhere the hive image's default user is root, which `runAsNonRoot` refuses, so it runs as uid 65534. Two `job` fields override this: +- **Job runtime.** The Job gets an init container, `hive-relays`, from the **running hive image by digest**. Hive reads its own pod (`$POD_NAME`, else `$HOSTNAME`) through the `pods` get grant the launcher already has, and takes the hive container's `imageID`. When the runtime reports no pullable digest, hive falls back to the image as written. Hive's own `imagePullSecrets` are copied onto the Job so the node can pull it. The init container runs `cp /usr/local/bin/hive-review /usr/local/bin/hive-open-issue /usr/local/bin/hive-topic /opt/hive/relays/` into a 16Mi `emptyDir`, and the kick container mounts that `emptyDir` read-only. The init container passes OpenShift's restricted SCC and the Kubernetes `restricted` Pod Security Standard: `runAsNonRoot`, `readOnlyRootFilesystem`, `allowPrivilegeEscalation: false`, every capability dropped, `RuntimeDefault` seccomp. On OpenShift (hive's pod carries the `openshift.io/scc` annotation) it sets no uid, and the SCC assigns one from the namespace range. Elsewhere the hive image's default user is root, which `runAsNonRoot` refuses, so it runs as uid 65534. Two `job` fields override this: - `relay_image` pins the init container's image, for example a mirror on an internal registry. `none` turns the relays off. - `relay_run_as_user` pins its uid. It must be positive. The first kick on a node pulls the hive image, which is large. Later kicks reuse it (`imagePullPolicy: IfNotPresent`). If hive cannot read its own pod, the kick fails with a message that names both fields, and no Job is created. -- **Podman.** Hive copies its own `hive-review` (found on its `PATH`, or in `$HIVE_SANDBOX_RELAY_DIR` when that is set) into a private temporary directory. It bind-mounts that directory with `-v :/opt/hive/relays:ro,Z` and removes it after the run. If the relay cannot be found, the kick fails with a message that says so. +- **Podman.** Hive copies its own relays (found on its `PATH`, or in `$HIVE_SANDBOX_RELAY_DIR` when that is set) into a private temporary directory. It bind-mounts that directory with `-v :/opt/hive/relays:ro,Z` and removes it after the run. If a relay cannot be found, the kick fails with a message that says so. - **Preflight.** The relays need `bash` and `python3` in the image. If either is missing, the kick exits with code 78 before the agent starts, and its transcript reads `hive: this sandbox image has no python3 on PATH ... add python3 to the image.` The image also needs `/bin/sh`. A kick that relies on the image's own entrypoint (no command) cannot be wrapped: the relays are mounted but not put on `PATH`. - **Precedence.** Hive's copy always comes first on `PATH`, and a copy in the image is ignored. The relay and the spool protocol it speaks ship together in the hive image, so an older or newer copy baked into a project image could sign or shape a request that this hive rejects. There is deliberately no "use the image's copy if newer" option. - **OpenShell and Substrate.** Not yet. On these runtimes the gateway or the actor API builds the sandbox pod, and getting hive's image into it needs runtime-specific work: an init container in the OpenShell template, and Landlock read access to `/opt/hive/relays`. Until that follow-up lands, the image still has to include `hive-review` on these runtimes. diff --git a/src/docs/scale-envelope.md b/src/docs/scale-envelope.md index 4a3216023a..3ace5f8388 100644 --- a/src/docs/scale-envelope.md +++ b/src/docs/scale-envelope.md @@ -34,7 +34,7 @@ ones to: 2. **Truncation must preserve the prioritization signal, not cut an arbitrary slice.** This holds today and is easy to assume backwards: - Issues are sorted by actionable rank tier, then **oldest first** within each tier before any cap is applied - (`RankActionableIssues`, `src/pkg/github/client.go:5336-5346`, descending `AgeMinutes` within each tier). + (`RankActionableIssues`, `src/pkg/github/client.go:5343-5353`, descending `AgeMinutes` within each tier). - PRs are sorted by **review class then oldest-first within class** (`SortPullRequestsForReview`, `src/pkg/github/review_priority.go:334`; fixes → refactors/docs → tests). diff --git a/src/pkg/beads/topic.go b/src/pkg/beads/topic.go index 8298be90ad..1178b25ac0 100644 --- a/src/pkg/beads/topic.go +++ b/src/pkg/beads/topic.go @@ -143,7 +143,9 @@ func (s *Store) openTopicBead(key string) *Bead { // ClaimTopic records that actor owns topic c.Key through pull request // c.Repo#c.PR. Claiming an open topic again for the same PR is idempotent and // refreshes its branch (a branch rename); claiming it for a different PR -// returns ErrTopicClaimed together with the existing claim. +// returns ErrTopicClaimed together with the existing claim. A pending claim +// (ClaimTopicForBranch) in the same repo holds no PR yet, so a claim with a +// PR number binds it. func (s *Store) ClaimTopic(actor string, c TopicClaim) (TopicClaim, error) { key, err := NormalizeTopicKey(c.Key) if err != nil { @@ -165,6 +167,13 @@ func (s *Store) ClaimTopic(actor string, c TopicClaim) (TopicClaim, error) { now := flexTime{time.Now().UTC()} if b := s.openTopicBead(key); b != nil { existing := topicFromBead(b) + if existing.PR == 0 && SameTopicRepo(existing.Repo, repo) { + bindTopicBead(b, c.PR, branch, now) + if err := s.persist(b); err != nil { + return existing, err + } + return topicFromBead(b), nil + } if existing.PR != c.PR || !SameTopicRepo(existing.Repo, repo) { return existing, fmt.Errorf("%w: %s is held by %s#%d", ErrTopicClaimed, key, existing.Repo, existing.PR) } @@ -181,17 +190,116 @@ func (s *Store) ClaimTopic(actor string, c TopicClaim) (TopicClaim, error) { return topicFromBead(b), nil } + b := s.newTopicBead(actor, key, repo, c.PR, branch, now) + s.beads[b.ID] = b + s.evictOldClosed() + if err := s.persist(b); err != nil { + return TopicClaim{}, err + } + return topicFromBead(b), nil +} + +// ClaimTopicForBranch records a PENDING claim (hivecommons/hive#18572): actor +// owns topic key through the pull request hive will open from branch in +// repo, whose number is not known yet. A sandboxed kick makes this claim; +// the governor binds it to the PR once one with that head branch appears +// (BindTopicBranch). Claiming again with the same branch is idempotent, and +// a pending claim on another branch (an earlier kick that opened no PR) is +// moved to this one. An open claim already bound to a PR is kept when its +// branch is this branch, since the kick is pushing onto that PR, and refused +// with ErrTopicClaimed otherwise. +func (s *Store) ClaimTopicForBranch(actor, key, repo, branch string) (TopicClaim, error) { + k, err := NormalizeTopicKey(key) + if err != nil { + return TopicClaim{}, err + } + repo, branch = strings.TrimSpace(repo), strings.TrimSpace(branch) + if repo == "" { + return TopicClaim{}, errors.New("topic claim needs a repo") + } + if branch == "" { + return TopicClaim{}, errors.New("a pending topic claim needs the branch its pull request is opened from") + } + + s.mu.Lock() + defer s.mu.Unlock() + defer s.lockAndRefresh()() + + now := flexTime{time.Now().UTC()} + if b := s.openTopicBead(k); b != nil { + existing := topicFromBead(b) + if !SameTopicRepo(existing.Repo, repo) { + return existing, fmt.Errorf("%w: %s is held in %s", ErrTopicClaimed, k, existing.Repo) + } + if existing.PR > 0 { + if existing.Branch == branch { + return existing, nil + } + return existing, fmt.Errorf("%w: %s is held by %s#%d", ErrTopicClaimed, k, existing.Repo, existing.PR) + } + if existing.Branch == branch { + return existing, nil + } + bindTopicBead(b, 0, branch, now) + if err := s.persist(b); err != nil { + return existing, err + } + return topicFromBead(b), nil + } + b := s.newTopicBead(actor, k, repo, 0, branch, now) + s.beads[b.ID] = b + s.evictOldClosed() + if err := s.persist(b); err != nil { + return TopicClaim{}, err + } + return topicFromBead(b), nil +} + +// BindTopicBranch binds every open pending claim on repo's branch to pull +// request pr, the PR hive opened from that branch. It returns how many +// claims it bound. +func (s *Store) BindTopicBranch(repo, branch string, pr int) (int, error) { + branch = strings.TrimSpace(branch) + if pr <= 0 || branch == "" { + return 0, nil + } + n := 0 + _, err := s.mutateOpenTopics(func(b *Bead) bool { + got, _ := strconv.Atoi(b.Meta(MetaTopicPR)) + return got == 0 && b.Meta(MetaTopicBranch) == branch && SameTopicRepo(b.Meta(MetaTopicRepo), repo) + }, func(b *Bead, now flexTime) { + bindTopicBead(b, pr, branch, now) + n++ + }) + return n, err +} + +// bindTopicBead sets a claim's PR (0 keeps it pending) and, when given, its +// branch. Caller holds s.mu. +func bindTopicBead(b *Bead, pr int, branch string, now flexTime) { + if b.Metadata == nil { + b.Metadata = make(map[string]interface{}) + } + b.Metadata[MetaTopicPR] = strconv.Itoa(pr) + if branch != "" { + b.Metadata[MetaTopicBranch] = branch + } + b.UpdatedAt = now +} + +// newTopicBead builds an open topic-claim bead. Caller holds s.mu. +func (s *Store) newTopicBead(actor, key, repo string, pr int, branch string, now flexTime) *Bead { metadata := map[string]interface{}{ MetaTopicKey: key, MetaTopicRepo: repo, - MetaTopicPR: strconv.Itoa(c.PR), + MetaTopicPR: strconv.Itoa(pr), MetaTopicBranch: branch, MetaTopicState: TopicStateOpen, } if s.hiveID != "" { metadata[hiveIDMetadataKey] = s.hiveID } - b := &Bead{ + return &Bead{ ID: uuid.New().String()[:12], Title: "topic: " + key, Type: TypeChore, @@ -203,12 +311,6 @@ func (s *Store) ClaimTopic(actor string, c TopicClaim) (TopicClaim, error) { CreatedAt: now, UpdatedAt: now, } - s.beads[b.ID] = b - s.evictOldClosed() - if err := s.persist(b); err != nil { - return TopicClaim{}, err - } - return topicFromBead(b), nil } // Topic returns the open claim for key. diff --git a/src/pkg/beads/topic_test.go b/src/pkg/beads/topic_test.go index 565a21fd81..6c8bb4ac85 100644 --- a/src/pkg/beads/topic_test.go +++ b/src/pkg/beads/topic_test.go @@ -156,3 +156,70 @@ func TestSameTopicRepoAndProjection(t *testing.T) { t.Fatal("non-topic bead counted as a topic") } } + +// A pending claim (hivecommons/hive#18572) is made before its PR exists, +// moves with a later kick's branch, is refused against a PR-bound claim on +// another branch, and is bound by the PR opened from its branch. +func TestClaimTopicForBranchPendingAndBind(t *testing.T) { + s := newTopicStore(t) + for name, args := range map[string][3]string{ + "bad key": {"a b", "acme/widget", "b"}, + "no repo": {"k", "", "b"}, + "no branch": {"k", "acme/widget", " "}, + } { + if _, err := s.ClaimTopicForBranch("deps", args[0], args[1], args[2]); err == nil { + t.Errorf("%s: pending claim accepted", name) + } + } + + c, err := s.ClaimTopicForBranch("deps", "K", "acme/widget", "hive/deps-1") + if err != nil || c.PR != 0 || c.Branch != "hive/deps-1" || c.Key != "k" || c.State != TopicStateOpen { + t.Fatalf("pending claim = %+v, %v", c, err) + } + if again, err := s.ClaimTopicForBranch("deps", "k", "acme/widget", "hive/deps-1"); err != nil || again.BeadID != c.BeadID { + t.Fatalf("idempotent pending claim = %+v, %v", again, err) + } + // An earlier kick opened no PR: the pending claim follows the new kick. + moved, err := s.ClaimTopicForBranch("deps", "k", "acme/widget", "hive/deps-2") + if err != nil || moved.BeadID != c.BeadID || moved.Branch != "hive/deps-2" { + t.Fatalf("moved pending claim = %+v, %v", moved, err) + } + if _, err := s.ClaimTopicForBranch("deps", "k", "other/widget", "hive/deps-2"); !errors.Is(err, ErrTopicClaimed) { + t.Fatalf("pending claim in another repo = %v, want ErrTopicClaimed", err) + } + + if n, err := s.BindTopicBranch("acme/widget", "hive/deps-1", 9); err != nil || n != 0 { + t.Fatalf("bind on a stale branch = %d, %v", n, err) + } + if n, _ := s.BindTopicBranch("acme/widget", "", 9); n != 0 { + t.Fatal("bind without a branch bound something") + } + if n, err := s.BindTopicBranch("widget", "hive/deps-2", 9); err != nil || n != 1 { + t.Fatalf("bind = %d, %v", n, err) + } + bound, ok := s.Topic("k") + if !ok || bound.PR != 9 || bound.Branch != "hive/deps-2" { + t.Fatalf("bound claim = %+v", bound) + } + // Bound: the same branch keeps it (the kick pushes onto that PR), any + // other branch is refused. + if same, err := s.ClaimTopicForBranch("deps", "k", "acme/widget", "hive/deps-2"); err != nil || same.PR != 9 { + t.Fatalf("same-branch pending claim on a bound topic = %+v, %v", same, err) + } + if _, err := s.ClaimTopicForBranch("deps", "k", "acme/widget", "hive/deps-3"); !errors.Is(err, ErrTopicClaimed) { + t.Fatalf("other-branch claim on a bound topic = %v, want ErrTopicClaimed", err) + } +} + +// A claim with a PR number binds a pending claim in the same repo. +func TestClaimTopicBindsPendingClaim(t *testing.T) { + s := newTopicStore(t) + pending, _ := s.ClaimTopicForBranch("deps", "k", "acme/widget", "hive/deps-1") + c, err := s.ClaimTopic("deps", TopicClaim{Key: "k", Repo: "acme/widget", PR: 12}) + if err != nil || c.BeadID != pending.BeadID || c.PR != 12 || c.Branch != "hive/deps-1" { + t.Fatalf("binding claim = %+v, %v", c, err) + } + if _, err := s.ClaimTopic("deps", TopicClaim{Key: "k", Repo: "acme/widget", PR: 13}); !errors.Is(err, ErrTopicClaimed) { + t.Fatalf("claim for another PR after binding = %v", err) + } +} diff --git a/src/pkg/github/client.go b/src/pkg/github/client.go index e12904495c..2d13c119e1 100644 --- a/src/pkg/github/client.go +++ b/src/pkg/github/client.go @@ -397,6 +397,12 @@ type Client struct { // the per-agent push-capability policy + forge-resistance. nil fails closed. // Set by StartReviewRequestWatcher. reviewAuthz ReviewRequestAuthorizer + // sandboxSpoolIssueAuthz and sandboxSpoolTopic serve the issue and topic + // kinds of the sandbox request spool (hivecommons/hive#18572, #18573). + // Set by SetSandboxSpoolRelays; nil refuses the kind. + sandboxSpoolMu sync.Mutex + sandboxSpoolIssueAuthz IssueRequestAuthorizer + sandboxSpoolTopic SandboxTopicHandler // reviewBots is classification.review_bots (hivecommons/hive#7360): the // external review-bot logins whose threads the hive may reply in and // resolve. Zero value = feature off: the monitor reports nothing and the diff --git a/src/pkg/github/issue_request_watcher.go b/src/pkg/github/issue_request_watcher.go index afad097b87..521338df66 100644 --- a/src/pkg/github/issue_request_watcher.go +++ b/src/pkg/github/issue_request_watcher.go @@ -409,6 +409,15 @@ func (c *Client) issueClearRetry(path string) { } func (c *Client) handleOneIssueRequest(ctx context.Context, path string, nowFn func() time.Time) { + c.handleOneIssueRequestFrom(ctx, path, reviewRequestSource{}, nowFn) +} + +// handleOneIssueRequestFrom is handleOneIssueRequest for a request from src. +// The zero source is the pod-local drop box. A sandbox spool's request +// (hivecommons/hive#18573) is attributed to the manifest's agent, skips the +// file-owner check its ingest already stood in for, and is authorized by the +// source's authorizer; every other gate is the same code. +func (c *Client) handleOneIssueRequestFrom(ctx context.Context, path string, src reviewRequestSource, nowFn func() time.Time) { if !c.issueBackoffAllows(path, nowFn) { return } @@ -437,6 +446,11 @@ func (c *Client) handleOneIssueRequest(ctx context.Context, path string, nowFn f slog.String("path", path), slog.String("error", err.Error())) return } + // A sandbox spool's requests belong to the agent its manifest names, + // whatever the file says. + if src.agent != "" { + req.Agent = src.agent + } kind := strings.TrimSpace(strings.ToLower(req.Kind)) if kind == "" { kind = "issue" @@ -505,11 +519,19 @@ func (c *Client) handleOneIssueRequest(ctx context.Context, path string, nowFn f // watcher. A nil authorizer fails closed. Denials quarantine (.denied) — // retrying can never change policy. fileUID := statUID(data, path) - if c.issueAuthz == nil { + authz := c.issueAuthz + if src.agent != "" { + // A staged spool file is owned by hive, so its UID proves nothing; + // the MAC check at ingest did the proving. 0 is the authorizer's "no + // owning UID" value. + fileUID = 0 + authz = src.issueAuthz + } + if authz == nil { c.denyIssueRequest(path, req, "no authorizer configured (fail closed)", nowFn) return } - if err := c.issueAuthz(req.Agent, fileUID, kind); err != nil { + if err := authz(req.Agent, fileUID, kind); err != nil { c.denyIssueRequest(path, req, err.Error(), nowFn) return } @@ -919,7 +941,7 @@ func (c *Client) handleOneIssueRequest(ctx context.Context, path string, nowFn f } } if req.Claim { - resp.Claimed, resp.ClaimError = c.claimCreatedIssue(ctx, req, fileUID, res, meta, nowFn) + resp.Claimed, resp.ClaimError = c.claimCreatedIssue(ctx, req, authz, fileUID, res, meta, nowFn) } } } @@ -1017,7 +1039,7 @@ func (c *Client) claimIssueForAgent(ctx context.Context, repo string, number int // issue its filer is working is not open for outside help. The create has // already happened, so a failure never fails or retries the request; it is // returned as the result's claim_error. -func (c *Client) claimCreatedIssue(ctx context.Context, req IssueRequest, fileUID int, res CreateIssueResult, meta InvocationMeta, nowFn func() time.Time) (bool, string) { +func (c *Client) claimCreatedIssue(ctx context.Context, req IssueRequest, authz IssueRequestAuthorizer, fileUID int, res CreateIssueResult, meta InvocationMeta, nowFn func() time.Time) (bool, string) { fail := func(why string) (bool, string) { c.logger.Warn("issue-request watcher: issue created but not claimed", slog.String("repo", req.Repo), slog.Int("number", res.Number), @@ -1033,10 +1055,10 @@ func (c *Client) claimCreatedIssue(ctx context.Context, req IssueRequest, fileUI if strings.TrimSpace(req.Agent) == "" { return fail("the request names no agent") } - if c.issueAuthz == nil { + if authz == nil { return fail("no authorizer configured (fail closed)") } - if err := c.issueAuthz(req.Agent, fileUID, "claim"); err != nil { + if err := authz(req.Agent, fileUID, "claim"); err != nil { return fail(err.Error()) } if reason, refused := c.refuseWrite(req.Agent, WriteOpClaim, req.Repo, res.Number); refused { diff --git a/src/pkg/github/notification_write_sites.go b/src/pkg/github/notification_write_sites.go index 798ab9a7d6..7d95a65879 100644 --- a/src/pkg/github/notification_write_sites.go +++ b/src/pkg/github/notification_write_sites.go @@ -84,7 +84,7 @@ var notificationWriteSites = map[string]NotificationWriteSite{ "github.(*Client).bindProwLgtm": {Class: NotificationClassAttention}, "github.(*Client).commentOnHumanFiledTwin": {Class: NotificationClassAttention}, "github.(*Client).createIssue": {Class: NotificationClassAttention}, - "github.(*Client).handleOneIssueRequest": {Class: NotificationClassAttention}, + "github.(*Client).handleOneIssueRequestFrom": {Class: NotificationClassAttention}, "github.(*Client).parkClankerRequestedPR": {Class: NotificationClassAttention}, "github.(*Client).redirectToExistingPR": {Class: NotificationClassAttention}, "github.(*Client).relayScannerNote": {Class: NotificationClassAttention}, diff --git a/src/pkg/github/review_request_sandbox_spool.go b/src/pkg/github/review_request_sandbox_spool.go index 02eaf07311..10f74d6f1d 100644 --- a/src/pkg/github/review_request_sandbox_spool.go +++ b/src/pkg/github/review_request_sandbox_spool.go @@ -55,6 +55,15 @@ import ( // accepted (a copied request), is set aside as .rejected and never staged. // That is what stands in for the file-owner forge check the pod-local path // relies on. +// +// The spool is typed (hivecommons/hive#18572, #18573). An envelope's kind +// says which relay a request is for: review (the default, hive-review), +// issue (hive-open-issue) or topic (hive-topic). Each kind is staged in its +// own dir and runs through the handler that serves it in-pod: +// handleOneReviewRequest, handleOneIssueRequest, or the topic handler cmd/hive +// installs over the agents' bead stores. The kind is bound into the MAC +// (crux.SandboxSpoolMAC), so a request signed as one kind is refused as any +// other, and the seen-MAC replay guard is shared across kinds. // sandboxReviewSpoolsDirForTest lets tests point the relay at a temp dir. var sandboxReviewSpoolsDirForTest string @@ -86,6 +95,19 @@ type reviewRequestSource struct { // agent, when set, is the manifest-attributed owner of a sandbox spool. // It replaces the request's agent field, and the UID check is skipped. agent string + // issueAuthz authorizes a spooled issue request. The spool is drained + // by the review watcher's loop, so it carries the authorizer installed + // with SetSandboxSpoolRelays rather than reading the issue watcher's. + issueAuthz IssueRequestAuthorizer + // repo and branch are the kick's target and PR branch from the manifest; + // a topic claim with no PR number binds to the PR opened from branch. + repo, branch string +} + +// spoolSource is how a manifest's spool is attributed. +func (c *Client) spoolSource(s sandboxReviewSpool) reviewRequestSource { + issueAuthz, _ := c.sandboxSpoolRelays() + return reviewRequestSource{agent: s.Agent, issueAuthz: issueAuthz, repo: s.Repo, branch: s.Branch} } type sandboxReviewSpool struct { @@ -94,10 +116,43 @@ type sandboxReviewSpool struct { } // staging is the hive-owned dir a spool's requests are processed in. +// Reviews are staged at its top level, as they always were; every other +// kind in a subdirectory named for it (stagingFor). func (s sandboxReviewSpool) staging() string { return strings.TrimSuffix(s.path, ".json") + ".d" } +// stagingFor is the staging dir of one request kind. +func stagingFor(staging, kind string) string { + if kind == crux.SandboxSpoolKindReview { + return staging + } + return filepath.Join(staging, kind) +} + +// stagedAnywhere reports whether name is already staged under any kind, so +// a request never replaces one still retrying and two kinds never share a +// result file name in the spool. +func stagedAnywhere(staging, name string) bool { + for _, kind := range crux.SandboxSpoolKinds { + if _, err := os.Lstat(filepath.Join(stagingFor(staging, kind), name)); err == nil { + return true + } + } + return false +} + +// stagingHasRequest reports whether any kind's staging dir still holds a +// request. +func stagingHasRequest(staging string) bool { + for _, kind := range crux.SandboxSpoolKinds { + if dirHasReviewRequest(stagingFor(staging, kind)) { + return true + } + } + return false +} + // workspace is the kick workspace the spool lives in, and rel the spool's // path inside it. func (s sandboxReviewSpool) workspace() (dir, rel string) { @@ -126,14 +181,19 @@ func (c *Client) processSandboxReviewSpools(ctx context.Context, nowFn func() ti if err == nil { c.ingestSandboxSpool(root, rel, staging, s.Key, nowFn()) } - c.processReviewRequestDir(ctx, staging, reviewRequestSource{agent: s.Agent}, nowFn) + src := c.spoolSource(s) + c.processReviewRequestDir(ctx, staging, src, nowFn) + c.processSandboxIssueDir(ctx, stagingFor(staging, crux.SandboxSpoolKindIssue), src, nowFn) + c.processSandboxTopicDir(ctx, stagingFor(staging, crux.SandboxSpoolKindTopic), src, nowFn) spoolPending := false if root != nil { - mirrorSandboxResults(root, rel, staging) + for _, kind := range crux.SandboxSpoolKinds { + mirrorSandboxResults(root, rel, stagingFor(staging, kind)) + } spoolPending = rootSpoolPending(root, rel) _ = root.Close() } - if s.EndedAt != nil && !spoolPending && !dirHasReviewRequest(staging) { + if s.EndedAt != nil && !spoolPending && !stagingHasRequest(staging) { _ = os.RemoveAll(staging) _ = os.Remove(s.path) } @@ -253,12 +313,13 @@ func sandboxSpoolRefusal(m crux.SandboxReviewSpool, now time.Time) string { return "" } -// ingestSandboxSpool moves each request out of the spool into staging. A -// name already staged (a request still retrying) waits in the spool for a -// later pass rather than replacing it. A file that is not yet valid JSON is -// left alone for inFlightGrace, in case the sandbox is still writing it. -// Only the request inside an envelope MACed with this run's key is staged; -// anything else is set aside (sandboxSpoolEnvelope). +// ingestSandboxSpool moves each request out of the spool into the staging +// dir of its kind. A name already staged (a request still retrying) waits in +// the spool for a later pass rather than replacing it. A file that is not +// yet valid JSON is left alone for inFlightGrace, in case the sandbox is +// still writing it. Only the request inside an envelope MACed with this +// run's key for its kind is staged; anything else is set aside +// (sandboxSpoolEnvelope). func (c *Client) ingestSandboxSpool(root *os.Root, rel, staging, key string, now time.Time) { d, err := root.Open(rel) if err != nil { @@ -274,8 +335,7 @@ func (c *Client) ingestSandboxSpool(root *os.Root, rel, staging, key string, now continue } name := e.Name() - dst := filepath.Join(staging, name) - if _, err := os.Lstat(dst); err == nil { + if stagedAnywhere(staging, name) { continue } src := filepath.Join(rel, name) @@ -291,11 +351,16 @@ func (c *Client) ingestSandboxSpool(root *os.Root, rel, staging, key string, now if !json.Valid(data) && now.Sub(fi.ModTime()) < inFlightGrace { continue } - request, mac, reason := sandboxSpoolEnvelope(data, key) + kind, request, mac, reason := sandboxSpoolEnvelope(data, key) if reason != "" { c.rejectSandboxSpoolFile(root, src, reason, now) continue } + dir := stagingFor(staging, kind) + if err := os.MkdirAll(dir, 0o700); err != nil { + continue // left in the spool for the next pass + } + dst := filepath.Join(dir, name) marker, err := markSpoolMACSeen(staging, mac) if errors.Is(err, fs.ErrExist) { c.rejectSandboxSpoolFile(root, src, "replay of a request this spool already accepted", now) @@ -312,20 +377,25 @@ func (c *Client) ingestSandboxSpool(root *os.Root, rel, staging, key string, now } } -// sandboxSpoolEnvelope returns the request inside data and its MAC, or why -// data is not a request this run's sandbox wrote. A request without the key -// is indistinguishable from one another agent planted, so it is refused. -func sandboxSpoolEnvelope(data []byte, key string) (request []byte, mac, reason string) { +// sandboxSpoolEnvelope returns the kind of the request inside data, the +// request and its MAC, or why data is not a request this run's sandbox +// wrote. A request without the key is indistinguishable from one another +// agent planted, so it is refused; so is a kind the relay does not serve. +func sandboxSpoolEnvelope(data []byte, key string) (kind string, request []byte, mac, reason string) { var env crux.SandboxReviewEnvelope if err := json.Unmarshal(data, &env); err != nil || env.Request == "" || env.MAC == "" { - return nil, "", "not a signed spool envelope" + return "", nil, "", "not a signed spool envelope" + } + kind, ok := crux.NormalizeSandboxSpoolKind(env.Kind) + if !ok { + return "", nil, "", fmt.Sprintf("unknown spool request kind %q", env.Kind) } request = []byte(env.Request) - want := crux.SandboxReviewMAC(key, request) + want := crux.SandboxSpoolMAC(key, env.Kind, request) if !hmac.Equal([]byte(env.MAC), []byte(want)) { - return nil, "", "MAC does not match this run's spool key" + return "", nil, "", "MAC does not match this run's spool key" } - return request, want, "" + return kind, request, want, "" } // markSpoolMACSeen records that this spool accepted a request with mac, so diff --git a/src/pkg/github/sandbox_spool_relays.go b/src/pkg/github/sandbox_spool_relays.go new file mode 100644 index 0000000000..78095bfaba --- /dev/null +++ b/src/pkg/github/sandbox_spool_relays.go @@ -0,0 +1,245 @@ +package github + +import ( + "context" + "encoding/json" + "errors" + "log/slog" + "os" + "path/filepath" + "strings" + "time" + + "github.com/hivecommons/hive/pkg/beads" +) + +// The issue and topic kinds of the sandbox request spool +// (hivecommons/hive#18572, #18573). review_request_sandbox_spool.go verifies +// and stages every spooled request; this file hands the issue and topic +// kinds to their handlers. A spooled issue request runs through +// handleOneIssueRequestFrom, so the repo gate, the ACMM mode gate, the lane +// write allowlist, repo pause, repo scope, dedupe by title, the canary scan +// and the audit trail all apply exactly as they do in-pod. A spooled topic +// request is applied to the manifest agent's own bead store by the handler +// cmd/hive installs. Both are attributed to the manifest's agent only. + +// Topic request operations, mirroring `bd topic`. +const ( + SandboxTopicOpClaim = "claim" + SandboxTopicOpGet = "get" + SandboxTopicOpList = "list" + SandboxTopicOpRelease = "release" +) + +// SandboxTopicRequest is what hive-topic writes into the spool. Agent is +// ignored: a spooled request belongs to the manifest's agent. A claim with +// no PR is a pending claim for the PR hive opens from this kick. +type SandboxTopicRequest struct { + Op string `json:"op"` + Key string `json:"key,omitempty"` + Repo string `json:"repo,omitempty"` + PR int `json:"pr,omitempty"` + Branch string `json:"branch,omitempty"` + Agent string `json:"agent,omitempty"` +} + +// SandboxTopicCall is a validated topic request with its attribution +// resolved. Pending marks a claim that binds to the PR hive opens from +// Branch once that PR exists. +type SandboxTopicCall struct { + Agent string + Op string + Key string + Repo string + PR int + Branch string + Pending bool +} + +// SandboxTopicResponse is the result file hive-topic reads back. Refused +// means the topic is held through a different PR (claim) or has no open +// claim (get); hive-topic exits 2 for it, as `bd topic` does. +type SandboxTopicResponse struct { + OK bool `json:"ok"` + Refused bool `json:"refused,omitempty"` + Error string `json:"error,omitempty"` + Claim *beads.TopicClaim `json:"claim,omitempty"` + Claims []beads.TopicClaim `json:"claims,omitempty"` + Released bool `json:"released,omitempty"` + Pending bool `json:"pending,omitempty"` + At string `json:"at"` +} + +// SandboxTopicHandler applies one topic call to the agent's bead store. +type SandboxTopicHandler func(SandboxTopicCall) SandboxTopicResponse + +// SetSandboxSpoolRelays installs the authorizer for spooled issue requests +// and the handler for spooled topic requests. Call it before the request +// watchers start. A nil authorizer or handler refuses its kind (fail +// closed): the request gets a result saying so and nothing is written. +func (c *Client) SetSandboxSpoolRelays(issueAuthz IssueRequestAuthorizer, topic SandboxTopicHandler) { + if c == nil { + return + } + c.sandboxSpoolMu.Lock() + defer c.sandboxSpoolMu.Unlock() + c.sandboxSpoolIssueAuthz = issueAuthz + c.sandboxSpoolTopic = topic +} + +func (c *Client) sandboxSpoolRelays() (IssueRequestAuthorizer, SandboxTopicHandler) { + c.sandboxSpoolMu.Lock() + defer c.sandboxSpoolMu.Unlock() + return c.sandboxSpoolIssueAuthz, c.sandboxSpoolTopic +} + +// spoolRequestPaths lists the requests waiting in a staging dir. +func spoolRequestPaths(dir string) []string { + entries, err := os.ReadDir(dir) + if err != nil { + return nil + } + var out []string + for _, e := range entries { + if isReviewRequestFile(e) { + out = append(out, filepath.Join(dir, e.Name())) + } + } + return out +} + +// processSandboxIssueDir runs a spool's staged issue requests through the +// pod-local issue relay's handler. +func (c *Client) processSandboxIssueDir(ctx context.Context, dir string, src reviewRequestSource, nowFn func() time.Time) { + for _, path := range spoolRequestPaths(dir) { + if ctx.Err() != nil { + return + } + c.handleOneIssueRequestFrom(ctx, path, src, nowFn) + } +} + +// processSandboxTopicDir applies a spool's staged topic requests. +func (c *Client) processSandboxTopicDir(ctx context.Context, dir string, src reviewRequestSource, nowFn func() time.Time) { + for _, path := range spoolRequestPaths(dir) { + if ctx.Err() != nil { + return + } + c.handleOneSandboxTopicRequest(path, src, nowFn) + } +} + +// handleOneSandboxTopicRequest applies one staged topic request and writes +// its result. A topic write is local to the agent's bead store, so there is +// nothing to retry: every request is final on its first pass. +func (c *Client) handleOneSandboxTopicRequest(path string, src reviewRequestSource, nowFn func() time.Time) { + data, _, err := readUntrustedFile(path, requestMaxBytes) + if err != nil { + if errors.Is(err, errDropBoxFileRejected) { + _ = os.Rename(path, path+".rejected") + } + return + } + var req SandboxTopicRequest + if err := json.Unmarshal(data, &req); err != nil { + if !quarantinable(path, nowFn()) { + return + } + c.finishSandboxTopicRequest(path, ".bad", SandboxTopicResponse{Error: "invalid JSON: " + err.Error()}, nowFn) + return + } + call, reason := sandboxTopicCall(req, src) + if reason != "" { + c.finishSandboxTopicRequest(path, ".bad", SandboxTopicResponse{Error: reason}, nowFn) + return + } + if call.Op == SandboxTopicOpClaim && !c.AgentServesRepo(call.Agent, call.Repo) { + c.finishSandboxTopicRequest(path, ".denied", SandboxTopicResponse{Error: "authorization denied: " + AgentRepoScopeReason(call.Agent, call.Repo)}, nowFn) + return + } + _, handler := c.sandboxSpoolRelays() + if handler == nil { + c.finishSandboxTopicRequest(path, ".denied", SandboxTopicResponse{Error: "authorization denied: no topic store configured (fail closed)"}, nowFn) + return + } + resp := handler(call) + suffix := "" + if !resp.OK { + suffix = ".denied" + } + c.logger.Info("review-request watcher: sandbox topic request applied", + slog.String("agent", call.Agent), slog.String("op", call.Op), slog.String("topic", call.Key), + slog.Bool("ok", resp.OK), slog.Bool("pending", call.Pending), slog.String("error", resp.Error)) + c.finishSandboxTopicRequest(path, suffix, resp, nowFn) +} + +// finishSandboxTopicRequest writes resp next to the staged request, then +// removes the request (suffix "") or sets it aside under suffix. +func (c *Client) finishSandboxTopicRequest(path, suffix string, resp SandboxTopicResponse, nowFn func() time.Time) { + if resp.At == "" { + resp.At = nowFn().UTC().Format(time.RFC3339) + } + if b, err := json.MarshalIndent(resp, "", " "); err == nil { + _ = writeRequestFile(strings.TrimSuffix(path, ".json")+".result.json", b) + } + if suffix == "" { + _ = os.Remove(path) + return + } + _ = os.Rename(path, path+suffix) +} + +// sandboxTopicCall validates req and resolves it against the manifest: the +// agent is the manifest's, a claim's repo defaults to the kick's target, and +// a claim with no PR number becomes a pending claim on the kick's branch. +func sandboxTopicCall(req SandboxTopicRequest, src reviewRequestSource) (SandboxTopicCall, string) { + call := SandboxTopicCall{ + Agent: src.agent, + Op: strings.ToLower(strings.TrimSpace(req.Op)), + Repo: strings.TrimSpace(req.Repo), + PR: req.PR, + Branch: strings.TrimSpace(req.Branch), + } + if call.Agent == "" { + return call, "topic request has no manifest agent" + } + switch call.Op { + case SandboxTopicOpList: + return call, "" + case SandboxTopicOpClaim, SandboxTopicOpGet, SandboxTopicOpRelease: + default: + return call, "unknown topic op " + `"` + req.Op + `"` + "; want claim, get, list or release" + } + key, err := beads.NormalizeTopicKey(req.Key) + if err != nil { + return call, err.Error() + } + call.Key = key + if call.Op != SandboxTopicOpClaim { + return call, "" + } + if call.Repo == "" { + call.Repo = src.repo + } + if call.Repo == "" { + return call, "topic claim needs --repo" + } + switch { + case call.PR < 0: + return call, "topic claim needs a positive --pr" + case call.PR > 0: + return call, "" + } + // No PR yet: the claim is for the PR hive opens from this kick. + if src.branch == "" { + return call, "this kick opens no pull request, so a topic claim needs --pr" + } + if !beads.SameTopicRepo(call.Repo, src.repo) { + return call, "a claim without --pr binds to the pull request this kick opens in " + src.repo + ", not " + call.Repo + } + if call.Branch != "" && call.Branch != src.branch { + return call, "a claim without --pr binds to this kick's branch " + src.branch + ", not " + call.Branch + } + call.Repo, call.Branch, call.Pending = src.repo, src.branch, true + return call, "" +} diff --git a/src/pkg/github/sandbox_spool_relays_test.go b/src/pkg/github/sandbox_spool_relays_test.go new file mode 100644 index 0000000000..6e01b0bb38 --- /dev/null +++ b/src/pkg/github/sandbox_spool_relays_test.go @@ -0,0 +1,484 @@ +package github + +import ( + "context" + "encoding/json" + "errors" + "os" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/crux" + "github.com/hivecommons/hive/pkg/beads" +) + +// writeSignedSpoolKind writes request into spool as hive-open-issue or +// hive-topic does inside a sandbox: an envelope of kind, MACed for that kind. +func writeSignedSpoolKind(t *testing.T, spool, name, key, kind string, request any) string { + t.Helper() + raw, err := json.Marshal(request) + if err != nil { + t.Fatal(err) + } + b, err := json.Marshal(crux.SandboxReviewEnvelope{Kind: kind, Request: string(raw), MAC: crux.SandboxSpoolMAC(key, kind, raw)}) + if err != nil { + t.Fatal(err) + } + path := filepath.Join(spool, name) + if err := os.WriteFile(path, b, 0o660); err != nil { + t.Fatal(err) + } + return path +} + +// newKickSpool is newSandboxSpool for a kick that opens a PR in repo from +// branch, which a pending topic claim binds to. +func newKickSpool(t *testing.T, manifests, agent, repo, branch string) string { + t.Helper() + workspace := t.TempDir() + spool := filepath.Join(workspace, filepath.FromSlash(crux.SandboxReviewSpoolRelPath)) + if err := os.MkdirAll(spool, 0o770); err != nil { + t.Fatal(err) + } + writeSandboxManifest(t, manifests, agent, crux.SandboxReviewSpool{ + Agent: agent, Repo: repo, Branch: branch, Spool: spool, Key: testSpoolKey(agent), StartedAt: time.Now().UTC(), + }) + return spool +} + +func readTopicResultFile(t *testing.T, reqPath string) SandboxTopicResponse { + t.Helper() + b, err := os.ReadFile(strings.TrimSuffix(reqPath, ".json") + ".result.json") + if err != nil { + t.Fatalf("result file missing: %v", err) + } + var r SandboxTopicResponse + if err := json.Unmarshal(b, &r); err != nil { + t.Fatal(err) + } + return r +} + +// A sandboxed agent's spooled issue is created through the issue relay's own +// handler, deduplicated by title, attributed and audited to the MANIFEST's +// agent (not the one the request names), and its result lands in the spool. +func TestSandboxSpoolIssue_CreatesAttributedToManifestAgent(t *testing.T) { + created := 0 + srv := newIssueMockServer(t, "", &created, nil, nil) + defer srv.Close() + c := testClient(t, srv.URL) + withReviewDir(t) + withIssueDir(t) + manifests := withSandboxSpools(t) + spool, _ := newSandboxSpool(t, manifests, "deps-bump", false) + + var authzAgent, authzKind string + authzUID := -1 + c.SetSandboxSpoolRelays(func(agent string, uid int, kind string) error { + authzAgent, authzUID, authzKind = agent, uid, kind + return nil + }, nil) + var auditAgent string + c.SetAttributionAudit(func(action, detail, agent string) { + if action == AuditActionAgentIssueCreated { + auditAgent = agent + } + }) + + reqPath := writeSignedSpoolKind(t, spool, "i.json", testSpoolKey("deps-bump"), crux.SandboxSpoolKindIssue, + IssueRequest{Repo: "o/r", Title: "dependency X breaks us", Body: "detail", Agent: "someone-else"}) + c.ProcessReviewRequestsOnce(context.Background()) + + if created != 1 { + t.Fatalf("issues created = %d, want 1", created) + } + if authzAgent != "deps-bump" || authzUID != 0 || authzKind != "issue" { + t.Errorf("authorized (%q, uid %d, %q), want the manifest's agent deps-bump, uid 0, kind issue", authzAgent, authzUID, authzKind) + } + if auditAgent != "deps-bump" { + t.Errorf("audited as %q, want deps-bump", auditAgent) + } + if _, err := os.Stat(reqPath); !errors.Is(err, os.ErrNotExist) { + t.Errorf("request still in the spool after delivery: %v", err) + } + if res := readIssueResultFile(t, reqPath); !res.OK || res.Number != 99 { + t.Errorf("mirrored result = %+v, want ok #99", res) + } +} + +// The title dedupe is the relay's: an open issue with the same title is +// reused, not duplicated, for a spooled request too. +func TestSandboxSpoolIssue_DedupesByTitle(t *testing.T) { + created := 0 + srv := newIssueMockServer(t, "dependency X breaks us", &created, nil, nil) + defer srv.Close() + c := testClient(t, srv.URL) + withReviewDir(t) + withIssueDir(t) + manifests := withSandboxSpools(t) + spool, _ := newSandboxSpool(t, manifests, "deps-bump", false) + c.SetSandboxSpoolRelays(func(string, int, string) error { return nil }, nil) + + reqPath := writeSignedSpoolKind(t, spool, "i.json", testSpoolKey("deps-bump"), crux.SandboxSpoolKindIssue, + IssueRequest{Repo: "o/r", Title: "dependency X breaks us", Body: "detail"}) + c.ProcessReviewRequestsOnce(context.Background()) + + if created != 0 { + t.Fatalf("issues created = %d, want 0 (dedupe)", created) + } + if res := readIssueResultFile(t, reqPath); !res.OK || !res.AlreadyExisted || res.Number != 7 { + t.Errorf("result = %+v, want reuse of #7", res) + } +} + +// A comment goes through too, as the same agent. +func TestSandboxSpoolIssue_PostsComment(t *testing.T) { + commented := 0 + srv := newIssueMockServer(t, "", nil, nil, &commented) + defer srv.Close() + c := testClient(t, srv.URL) + withReviewDir(t) + withIssueDir(t) + manifests := withSandboxSpools(t) + spool, _ := newSandboxSpool(t, manifests, "deps-bump", false) + c.SetSandboxSpoolRelays(func(string, int, string) error { return nil }, nil) + + writeSignedSpoolKind(t, spool, "c.json", testSpoolKey("deps-bump"), crux.SandboxSpoolKindIssue, + IssueRequest{Kind: "comment", Repo: "o/r", Number: 41, Body: "still broken on the card"}) + c.ProcessReviewRequestsOnce(context.Background()) + + if commented != 1 { + t.Fatalf("comments posted = %d, want 1", commented) + } +} + +// Every gate of the issue relay applies: the mode gate's denial, a missing +// authorizer (fail closed), the repo gate and the repo scope each refuse, +// the refusal reaches the sandbox and nothing is created. +func TestSandboxSpoolIssue_GatesRefuse(t *testing.T) { + for _, tc := range []struct { + name string + setup func(c *Client) + repo string + want string + }{ + {"mode gate denies", func(c *Client) { + c.SetSandboxSpoolRelays(func(string, int, string) error { return errors.New("advisory-only") }, nil) + }, "o/r", "advisory-only"}, + {"no authorizer fails closed", func(c *Client) { + // The pod-local issue authorizer is not borrowed by the spool. + c.issueAuthz = func(string, int, string) error { return nil } + c.SetSandboxSpoolRelays(nil, nil) + }, "o/r", "fail closed"}, + {"placeholder repo refused", func(c *Client) { + c.SetSandboxSpoolRelays(func(string, int, string) error { return nil }, nil) + }, "owner/repo", "placeholder"}, + {"repo scope refused", func(c *Client) { + c.SetSandboxSpoolRelays(func(string, int, string) error { return nil }, nil) + c.SetAgentRepoScopeFunc(func(agent, repo string) bool { return agent != "deps-bump" }) + }, "o/r", "not scoped"}, + } { + t.Run(tc.name, func(t *testing.T) { + created := 0 + srv := newIssueMockServer(t, "", &created, nil, nil) + defer srv.Close() + c := testClient(t, srv.URL) + withReviewDir(t) + withIssueDir(t) + manifests := withSandboxSpools(t) + spool, _ := newSandboxSpool(t, manifests, "deps-bump", false) + tc.setup(c) + + reqPath := writeSignedSpoolKind(t, spool, "i.json", testSpoolKey("deps-bump"), crux.SandboxSpoolKindIssue, + IssueRequest{Repo: tc.repo, Title: "t", Body: "b"}) + c.ProcessReviewRequestsOnce(context.Background()) + + if created != 0 { + t.Fatalf("refused request created %d issues", created) + } + if res := readIssueResultFile(t, reqPath); res.OK || !strings.Contains(res.Error, tc.want) { + t.Errorf("result = %+v, want an error containing %q", res, tc.want) + } + }) + } +} + +// The kind is bound into the MAC: an issue request signed as a review (the +// MAC hive-review computes) or under another run's key is rejected, and a +// copy of an accepted one is a replay whatever kind it now claims. +func TestSandboxSpoolIssue_MACBindsKindAndRefusesReplay(t *testing.T) { + created := 0 + srv := newIssueMockServer(t, "", &created, nil, nil) + defer srv.Close() + c := testClient(t, srv.URL) + withReviewDir(t) + withIssueDir(t) + manifests := withSandboxSpools(t) + spool, _ := newSandboxSpool(t, manifests, "deps-bump", false) + c.SetSandboxSpoolRelays(func(string, int, string) error { return nil }, nil) + key := testSpoolKey("deps-bump") + + raw, _ := json.Marshal(IssueRequest{Repo: "o/r", Title: "t", Body: "b"}) + crossKind, _ := json.Marshal(crux.SandboxReviewEnvelope{Kind: crux.SandboxSpoolKindIssue, Request: string(raw), MAC: crux.SandboxReviewMAC(key, raw)}) + if err := os.WriteFile(filepath.Join(spool, "cross.json"), crossKind, 0o660); err != nil { + t.Fatal(err) + } + otherRun := writeSignedSpoolKind(t, spool, "other.json", testSpoolKey("other-run"), crux.SandboxSpoolKindIssue, IssueRequest{Repo: "o/r", Title: "t2", Body: "b"}) + unknown := writeSignedSpoolKind(t, spool, "unknown.json", key, "merge", IssueRequest{Repo: "o/r", Title: "t3", Body: "b"}) + c.ProcessReviewRequestsOnce(context.Background()) + + if created != 0 { + t.Fatalf("mis-signed requests created %d issues", created) + } + for _, p := range []string{filepath.Join(spool, "cross.json"), otherRun, unknown} { + if _, err := os.Lstat(p + ".rejected"); err != nil { + t.Errorf("%s should be set aside as .rejected: %v", filepath.Base(p), err) + } + } + + first := writeSignedSpoolKind(t, spool, "first.json", key, crux.SandboxSpoolKindIssue, IssueRequest{Repo: "o/r", Title: "t", Body: "b"}) + copied, _ := os.ReadFile(first) + c.ProcessReviewRequestsOnce(context.Background()) + if created != 1 { + t.Fatalf("signed request: %d issues created, want 1", created) + } + replay := filepath.Join(spool, "replay.json") + if err := os.WriteFile(replay, copied, 0o660); err != nil { + t.Fatal(err) + } + c.ProcessReviewRequestsOnce(context.Background()) + if created != 1 { + t.Fatalf("replayed request created an issue: %d, want 1", created) + } + if _, err := os.Lstat(replay + ".rejected"); err != nil { + t.Errorf("replay should be set aside as .rejected: %v", err) + } +} + +// fakeTopicStore records the calls a topic handler receives and answers +// from a real bead store, as cmd/hive's handler does. +func fakeTopicHandler(t *testing.T, calls *[]SandboxTopicCall) SandboxTopicHandler { + t.Helper() + store, err := beads.NewStore(t.TempDir()) + if err != nil { + t.Fatal(err) + } + return func(call SandboxTopicCall) SandboxTopicResponse { + *calls = append(*calls, call) + switch call.Op { + case SandboxTopicOpClaim: + var claim beads.TopicClaim + var err error + if call.Pending { + claim, err = store.ClaimTopicForBranch(call.Agent, call.Key, call.Repo, call.Branch) + } else { + claim, err = store.ClaimTopic(call.Agent, beads.TopicClaim{Key: call.Key, Repo: call.Repo, PR: call.PR}) + } + if err != nil { + return SandboxTopicResponse{Error: err.Error(), Refused: errors.Is(err, beads.ErrTopicClaimed)} + } + return SandboxTopicResponse{OK: true, Claim: &claim, Pending: claim.PR == 0} + case SandboxTopicOpRelease: + released, err := store.ReleaseTopic(call.Key, beads.TopicStateReleased) + if err != nil { + return SandboxTopicResponse{Error: err.Error()} + } + return SandboxTopicResponse{OK: true, Released: released} + case SandboxTopicOpList: + return SandboxTopicResponse{OK: true, Claims: store.OpenTopics()} + } + claim, ok := store.Topic(call.Key) + if !ok { + return SandboxTopicResponse{Refused: true, Error: "no open claim"} + } + return SandboxTopicResponse{OK: true, Claim: &claim} + } +} + +// End to end: a sandboxed kick claims a topic for the PR hive will open from +// it (no PR number yet), reads it back, and releases it. Every call is the +// manifest's agent's, whatever the request says. +func TestSandboxSpoolTopic_ClaimGetReleaseAttributedToManifestAgent(t *testing.T) { + c := testClient(t, "http://127.0.0.1:1") + withReviewDir(t) + manifests := withSandboxSpools(t) + spool := newKickSpool(t, manifests, "deps-bump", "o/r", "hive/deps-bump-1") + var calls []SandboxTopicCall + c.SetSandboxSpoolRelays(nil, fakeTopicHandler(t, &calls)) + key := testSpoolKey("deps-bump") + + claim := writeSignedSpoolKind(t, spool, "claim.json", key, crux.SandboxSpoolKindTopic, SandboxTopicRequest{Op: "claim", Key: "Deps-Bump", Agent: "someone-else"}) + c.ProcessReviewRequestsOnce(context.Background()) + res := readTopicResultFile(t, claim) + if !res.OK || !res.Pending || res.Claim == nil || res.Claim.Repo != "o/r" || res.Claim.Branch != "hive/deps-bump-1" || res.Claim.PR != 0 || res.Claim.Actor != "deps-bump" { + t.Fatalf("claim result = %+v (%+v), want a pending claim by deps-bump on the kick's branch", res, res.Claim) + } + if _, err := os.Stat(claim); !errors.Is(err, os.ErrNotExist) { + t.Errorf("claim request still in the spool: %v", err) + } + + get := writeSignedSpoolKind(t, spool, "get.json", key, crux.SandboxSpoolKindTopic, SandboxTopicRequest{Op: "get", Key: "deps-bump"}) + list := writeSignedSpoolKind(t, spool, "list.json", key, crux.SandboxSpoolKindTopic, SandboxTopicRequest{Op: "list"}) + c.ProcessReviewRequestsOnce(context.Background()) + if r := readTopicResultFile(t, get); !r.OK || r.Claim == nil || r.Claim.Key != "deps-bump" { + t.Errorf("get = %+v, want the claim", r) + } + if r := readTopicResultFile(t, list); !r.OK || len(r.Claims) != 1 { + t.Errorf("list = %+v, want one claim", r) + } + + release := writeSignedSpoolKind(t, spool, "release.json", key, crux.SandboxSpoolKindTopic, SandboxTopicRequest{Op: "release", Key: "deps-bump"}) + c.ProcessReviewRequestsOnce(context.Background()) + if r := readTopicResultFile(t, release); !r.OK || !r.Released { + t.Errorf("release = %+v, want released", r) + } + for _, call := range calls { + if call.Agent != "deps-bump" { + t.Errorf("call %+v attributed to %q, want the manifest's agent", call, call.Agent) + } + } +} + +// A claim with a PR number is recorded as given; a second claim on the same +// open topic for another PR is refused and the refusal reaches the sandbox. +func TestSandboxSpoolTopic_ExplicitPRAndRefusal(t *testing.T) { + c := testClient(t, "http://127.0.0.1:1") + withReviewDir(t) + manifests := withSandboxSpools(t) + spool := newKickSpool(t, manifests, "deps-bump", "o/r", "hive/deps-bump-1") + var calls []SandboxTopicCall + c.SetSandboxSpoolRelays(nil, fakeTopicHandler(t, &calls)) + key := testSpoolKey("deps-bump") + + first := writeSignedSpoolKind(t, spool, "a.json", key, crux.SandboxSpoolKindTopic, SandboxTopicRequest{Op: "claim", Key: "k", Repo: "o/r", PR: 5}) + c.ProcessReviewRequestsOnce(context.Background()) + if r := readTopicResultFile(t, first); !r.OK || r.Claim == nil || r.Claim.PR != 5 { + t.Fatalf("claim = %+v, want #5", r) + } + second := writeSignedSpoolKind(t, spool, "b.json", key, crux.SandboxSpoolKindTopic, SandboxTopicRequest{Op: "claim", Key: "k", Repo: "o/r", PR: 6}) + c.ProcessReviewRequestsOnce(context.Background()) + if r := readTopicResultFile(t, second); r.OK || !r.Refused { + t.Errorf("second claim = %+v, want refused", r) + } + if _, err := os.Stat(second); !errors.Is(err, os.ErrNotExist) { + t.Errorf("refused request still in the spool: %v", err) + } +} + +// Topic gates: no handler fails closed, a repo the agent is not scoped to is +// refused, and malformed requests never reach the store. +func TestSandboxSpoolTopic_GatesRefuse(t *testing.T) { + for _, tc := range []struct { + name string + branch string + handler bool + scope func(agent, repo string) bool + req SandboxTopicRequest + want string + }{ + {"no handler fails closed", "b", false, nil, SandboxTopicRequest{Op: "claim", Key: "k", PR: 1}, "fail closed"}, + {"repo scope", "b", true, func(string, string) bool { return false }, SandboxTopicRequest{Op: "claim", Key: "k", PR: 1}, "not scoped"}, + {"unknown op", "b", true, nil, SandboxTopicRequest{Op: "steal", Key: "k"}, "unknown topic op"}, + {"bad key", "b", true, nil, SandboxTopicRequest{Op: "get", Key: "a b"}, "whitespace"}, + {"negative pr", "b", true, nil, SandboxTopicRequest{Op: "claim", Key: "k", PR: -1}, "positive"}, + {"no PR and the kick opens none", "", true, nil, SandboxTopicRequest{Op: "claim", Key: "k"}, "needs --pr"}, + {"no PR in another repo", "b", true, nil, SandboxTopicRequest{Op: "claim", Key: "k", Repo: "o/other"}, "binds to the pull request"}, + {"no PR on another branch", "b", true, nil, SandboxTopicRequest{Op: "claim", Key: "k", Branch: "elsewhere"}, "this kick's branch"}, + } { + t.Run(tc.name, func(t *testing.T) { + c := testClient(t, "http://127.0.0.1:1") + withReviewDir(t) + manifests := withSandboxSpools(t) + spool := newKickSpool(t, manifests, "deps-bump", "o/r", tc.branch) + var calls []SandboxTopicCall + if tc.handler { + c.SetSandboxSpoolRelays(nil, fakeTopicHandler(t, &calls)) + } + if tc.scope != nil { + c.SetAgentRepoScopeFunc(tc.scope) + } + p := writeSignedSpoolKind(t, spool, "t.json", testSpoolKey("deps-bump"), crux.SandboxSpoolKindTopic, tc.req) + c.ProcessReviewRequestsOnce(context.Background()) + + if len(calls) != 0 { + t.Fatalf("refused request reached the store: %+v", calls) + } + if r := readTopicResultFile(t, p); r.OK || !strings.Contains(r.Error, tc.want) { + t.Errorf("result = %+v, want an error containing %q", r, tc.want) + } + }) + } +} + +// A topic file that is not JSON past the in-flight grace is quarantined with +// a result; one still inside the grace waits. +func TestSandboxSpoolTopic_BadJSON(t *testing.T) { + c := testClient(t, "http://127.0.0.1:1") + dir := t.TempDir() + p := filepath.Join(dir, "t.json") + if err := os.WriteFile(p, []byte("{not json"), 0o600); err != nil { + t.Fatal(err) + } + src := reviewRequestSource{agent: "deps-bump"} + c.handleOneSandboxTopicRequest(p, src, time.Now) + if _, err := os.Stat(p); err != nil { + t.Fatalf("a fresh torn file must be left for the next pass: %v", err) + } + late := func() time.Time { return time.Now().Add(time.Hour) } + c.handleOneSandboxTopicRequest(p, src, late) + if _, err := os.Lstat(p + ".bad"); err != nil { + t.Errorf("bad JSON should be quarantined: %v", err) + } + if r := readTopicResultFile(t, p); r.OK || !strings.Contains(r.Error, "invalid JSON") { + t.Errorf("result = %+v", r) + } + // A vanished file is a no-op. + c.handleOneSandboxTopicRequest(filepath.Join(dir, "gone.json"), src, time.Now) +} + +func TestSandboxTopicCall_ResolvesAgainstManifest(t *testing.T) { + src := reviewRequestSource{agent: "deps", repo: "o/r", branch: "hive/deps-1"} + call, reason := sandboxTopicCall(SandboxTopicRequest{Op: " Claim ", Key: "K", Agent: "liar"}, src) + if reason != "" || call.Agent != "deps" || call.Key != "k" || call.Repo != "o/r" || call.Branch != "hive/deps-1" || !call.Pending { + t.Fatalf("call = %+v, %q", call, reason) + } + call, reason = sandboxTopicCall(SandboxTopicRequest{Op: "claim", Key: "k", Repo: "o/x", PR: 3, Branch: "b"}, src) + if reason != "" || call.Pending || call.Repo != "o/x" || call.PR != 3 { + t.Fatalf("explicit call = %+v, %q", call, reason) + } + if _, reason := sandboxTopicCall(SandboxTopicRequest{Op: "list"}, reviewRequestSource{}); !strings.Contains(reason, "manifest agent") { + t.Errorf("no agent: %q", reason) + } + if _, reason := sandboxTopicCall(SandboxTopicRequest{Op: "claim", Key: "k", PR: 1}, reviewRequestSource{agent: "deps"}); !strings.Contains(reason, "--repo") { + t.Errorf("no repo: %q", reason) + } +} + +func TestSetSandboxSpoolRelays_NilClient(t *testing.T) { + var c *Client + c.SetSandboxSpoolRelays(nil, nil) // must not panic +} + +// Two kinds never share a staged name, so their results cannot collide in +// the spool: a name staged as one kind waits. +func TestSandboxSpool_StagedNameIsUniqueAcrossKinds(t *testing.T) { + staging := t.TempDir() + if stagedAnywhere(staging, "x.json") || stagingHasRequest(staging) { + t.Fatal("empty staging reports a request") + } + dir := stagingFor(staging, crux.SandboxSpoolKindTopic) + if err := os.MkdirAll(dir, 0o700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, "x.json"), []byte("{}"), 0o600); err != nil { + t.Fatal(err) + } + if !stagedAnywhere(staging, "x.json") || !stagingHasRequest(staging) { + t.Error("a staged topic request is not seen") + } + if stagingFor(staging, crux.SandboxSpoolKindReview) != staging { + t.Error("reviews must stay at the staging root") + } +} diff --git a/src/pkg/scheduler/topic_claims.go b/src/pkg/scheduler/topic_claims.go index d40c110103..b665fe1e6c 100644 --- a/src/pkg/scheduler/topic_claims.go +++ b/src/pkg/scheduler/topic_claims.go @@ -93,7 +93,8 @@ const topicClaimsHeader = "## 📌 Topic claims (one PR per topic)" // topicClaimsUsage is how an agent claims, reads and releases a topic. const topicClaimsUsage = "Claim a new topic right after opening its PR: `bd topic claim --repo --pr --branch `.\n" + "Read one back with `bd topic get `; give one up with `bd topic release `. A second claim on an\n" + - "open topic for a different PR is refused. Hive releases a claim when its PR merges or closes.\n" + "open topic for a different PR is refused. Hive releases a claim when its PR merges or closes.\n" + + "In a sandbox, use `hive-topic` with the same arguments; `hive-topic claim ` without --pr claims the PR hive opens from this kick.\n" // topicClaimsSection renders the ${TOPIC_CLAIMS} block. It always carries the // usage lines, so a template that places the variable teaches the command @@ -111,6 +112,11 @@ func topicClaimsSection(claims []TopicClaim) string { if c.Branch != "" { line += " (branch " + c.Branch + ")" } + if c.PR == 0 { + // A pending claim from a sandboxed kick (#18572): hive binds + // it once the PR opened from the branch appears. + line = fmt.Sprintf(" %s → %s, PR not opened yet (pending on branch %s)", c.Key, c.Repo, c.Branch) + } b.WriteString(line + "\n") } } diff --git a/src/pkg/scheduler/topic_claims_test.go b/src/pkg/scheduler/topic_claims_test.go index d691a49f8f..f9b6754222 100644 --- a/src/pkg/scheduler/topic_claims_test.go +++ b/src/pkg/scheduler/topic_claims_test.go @@ -133,3 +133,17 @@ func TestTopicClaimHelpers(t *testing.T) { t.Errorf("seam repeated a placed block: %q", got) } } + +// A pending claim from a sandboxed kick (hivecommons/hive#18572) has no PR +// yet; the kick says so instead of printing #0, and teaches hive-topic. +func TestTopicClaimsSectionShowsPendingClaim(t *testing.T) { + got := topicClaimsSection([]TopicClaim{{Agent: "deps", Key: "bump", Repo: "acme/widget", Branch: "hive/deps-1"}}) + for _, want := range []string{"bump → acme/widget, PR not opened yet (pending on branch hive/deps-1)", "hive-topic"} { + if !strings.Contains(got, want) { + t.Errorf("section missing %q:\n%s", want, got) + } + } + if strings.Contains(got, "#0") { + t.Errorf("pending claim printed as #0:\n%s", got) + } +}