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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/integration-candidate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/integration-technical-v2-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/v2-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 22 additions & 1 deletion bin/hive-open-issue.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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": "<request JSON>",
# "mac": "<hex HMAC-SHA256(key, "issue\n" + request JSON)>"}. 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
Expand Down Expand Up @@ -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

Expand Down
184 changes: 184 additions & 0 deletions bin/hive-topic.sh
Original file line number Diff line number Diff line change
@@ -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 <key> [--repo owner/name] [--pr N] [--branch B]
# hive-topic get <key> [--json]
# hive-topic list [--json]
# hive-topic release <key>
#
# In a sandbox, `claim <key>` 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 <request>.result.json); --timeout <s> 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 <key> [--repo owner/name] [--pr N] [--branch B]
hive-topic get <key> [--json]
hive-topic list [--json]
hive-topic release <key>"""

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
46 changes: 46 additions & 0 deletions bin/test_hive_open_issue.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Loading
Loading