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
2 changes: 1 addition & 1 deletion scripts/compliance-audit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1708,7 +1708,7 @@ check_centralized_workflow_stubs() {
# (major-agnostic on tier, #657 F5); non-ring keeps the fixed canonical ref.
local esc_reusable why expected_pin="$canonical"
esc_reusable=$(escape_ere "$reusable")
[ "$is_ring" = 1 ] && expected_pin="${chan}/v<M>-$(ring_tier_for_repo "$repo")"
[ "$is_ring" = 1 ] && expected_pin="${chan}/v<M>-$(ring_tier_for_repo "$chan" "$repo")"
if echo "$decoded" | grep -qE "^[[:space:]]*uses:[[:space:]]*petry-projects/\\.github/\\.github/workflows/${esc_reusable}\\.yml@"; then
why="references the reusable but is not pinned to the major-scoped channel \`@${expected_pin}\` (org standard — a bare \`@${canonical}\` tier pin is drift)"
elif echo "$decoded" | grep -qF "petry-projects/.github/.github/workflows/${reusable}"; then
Expand Down
93 changes: 78 additions & 15 deletions scripts/lib/ring-pins.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,83 @@ readonly RING_REUSABLES=(
pr-review-mention
)

# ring_tier_for_repo <repo> -> next|ring0|ring1|stable
# Map a repo to its canary-ring tier (epic #495 topology):
# next — .github-private (candidate / first soak)
# ring0 — .github (dogfood; self-hosts via @main, so the stub check
# skips it — this mapping is informational there)
# ring1 — TalkTerm, bmad-bgreat-suite (early fleet canary)
# stable — everything else (broad fleet)
# Path to the ring registry (single source of truth). Overridable via CANARY_RINGS
# for tests, mirroring canary-rollout.sh / migrate-major-channels.sh; defaults to
# standards/canary-rings.json relative to this lib. Resolved at source time so it
# is correct no matter which script sources ring-pins.sh (#1092).
RING_PINS_REGISTRY="${CANARY_RINGS:-$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." 2>/dev/null && pwd)/standards/canary-rings.json}"

# ring_tier_for_repo <agent> <repo> -> next|ring0|ring1|stable
# Resolve a repo's canary-ring tier FOR A GIVEN AGENT (epic #495 topology),
# derived from that agent's rings[] in standards/canary-rings.json — the single
# source of truth (#1092). Ring membership is per-agent: the tier a repo sits in
# can differ between agents (e.g. `markets` is ring1 for apply-repo-settings but
# stable for every other agent), which a single agent-agnostic map cannot express.
#
# The repo is matched by basename, so callers may pass a bare name (`markets`) or a
# full `petry-projects/<repo>`. The `$host` and `$org_infra` member tokens are
# expanded (host = the agent's own host repo → next; org_infra = the other infra
# repo → ring0), and the first ring (registry order = tier order) whose members
# contain the repo wins. Repos in no explicit ring fall back to the ring whose
# members contain the `"*"` fleet wildcard (today: stable). An unknown agent (not
# in the registry) also falls back to `stable`.
ring_tier_for_repo() {
case "$1" in
.github-private) printf 'next' ;;
.github) printf 'ring0' ;;
TalkTerm | bmad-bgreat-suite) printf 'ring1' ;;
*) printf 'stable' ;;
esac
local agent="$1" repo="$2"
if [ -z "$agent" ]; then
echo "ring_tier_for_repo: missing agent argument (signature is <agent> <repo>)" >&2
return 2
fi
local repo_name="${repo##*/}"
# Self-initializing per-process cache: within one run the registry is immutable, so
# (agent, repo-basename) -> tier is a pure function. Cache it to avoid re-parsing the
# registry with jq on every call — the audit (pinned-version-report.sh) and deploy
# sweeps invoke this inside nested loops over every repo × agent (#1096). Same
# pattern as _RING_TAG_EXISTS_CACHE below.
declare -g -A _RING_TIER_CACHE 2>/dev/null || true
local cache_key="${agent},${repo_name}"
if [[ -n "${_RING_TIER_CACHE[$cache_key]+isset}" ]]; then
printf '%s' "${_RING_TIER_CACHE[$cache_key]}"
return 0
fi
local tier status
# Resolve tiers in the registry's `order` sequence (not array position) so a
# reordered registry can never make pin resolution disagree with the rollout
# promotion order that canary-rollout.sh drives off the same `order` field (#1096).
tier="$(jq -r --arg a "$agent" --arg repo "$repo_name" '
.agents[$a] as $ag
| if $ag == null then "" else
($ag.host | sub(".*/";"")) as $host
| ([.org_infra_repos[]? | sub(".*/";"")] - [$host]) as $orginfra
| ( $ag.rings
| sort_by(.order)
| map({ channel,
m: (reduce (.members[]?) as $x ([];
if $x == "$host" then . + [$host]
elif $x == "$org_infra" then . + $orginfra
else . + [($x | sub(".*/";""))] end)) }) ) as $r
| ( ([$r[] | select(.m | index($repo)) | .channel][0])
// ([$r[] | select(.m | index("*")) | .channel][0])
// "" )
Comment thread
don-petry marked this conversation as resolved.
Resolved
end
' "$RING_PINS_REGISTRY" 2>/dev/null)"
status=$?
# Fail CLOSED on a registry read/parse error (missing or corrupt file, or a jq
# failure): silently returning `stable` when the source of truth is unavailable
# would let the audit accept — and the deploy emit — incorrect stable pins,
# masking real ring drift (#1096). Mirrors ring_host_current_channel_major's
# fail-closed probe (#870). A read error is NOT cached, so a transient failure
# does not poison later lookups.
if [ "$status" -ne 0 ]; then
echo "ring_tier_for_repo: failed to read ring registry '$RING_PINS_REGISTRY' (agent=$agent repo=$repo_name)" >&2
return 3
fi
# jq succeeded: an empty result is a genuine no-match — an unknown agent (not in
# the registry) or a repo in no explicit ring — so fall back to the broad-fleet
# tier (the `*` ring is `stable` fleet-wide today). This is distinct from the
# infra read error above and IS a valid, cacheable answer.
[ -n "$tier" ] || tier="stable"
_RING_TIER_CACHE[$cache_key]="$tier"
printf '%s' "$tier"
Comment thread
don-petry marked this conversation as resolved.
Resolved
return 0
}
Comment thread
don-petry marked this conversation as resolved.
Resolved

Expand All @@ -85,7 +148,7 @@ ring_is_ring_reusable() {
# F5, so the no-major call site behavior is unchanged.
ring_canonical_ref() {
local name="$1" repo="$2" major="${3:-}"
local tier; tier="$(ring_tier_for_repo "$repo")"
local tier; tier="$(ring_tier_for_repo "$name" "$repo")"
if [ -n "$major" ]; then
printf '%s/v%s-%s' "$name" "$major" "$tier"
else
Expand Down Expand Up @@ -268,7 +331,7 @@ ring_repin_uses() {
# aligned. Pure.
ring_vform_tier_aligned() {
local ref="$1" base="$2" repo="$3" tier
tier="$(ring_tier_for_repo "$repo")"
tier="$(ring_tier_for_repo "$base" "$repo")"
[[ "$ref" =~ ^${base}/v[0-9]+-${tier}$ ]]
}

Expand Down
5 changes: 4 additions & 1 deletion scripts/pinned-version-report.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ drift_count=0
declare -A SEEN_VERSION # "agent\tversion" -> 1 (for the fan-out summary)

for repo in "${REPOS[@]}"; do
tier="$(ring_tier_for_repo "$repo")"
for agent in "${REUSABLES[@]}"; do
# Ring membership is per-agent (#1092): the same repo can sit in different tiers
# for different agents (e.g. markets is ring1 for apply-repo-settings, stable
# elsewhere), so resolve the tier per (agent, repo), not once per repo.
tier="$(ring_tier_for_repo "$agent" "$repo")"
# Caller stubs are conventionally named after the reusable (minus -reusable).
stub=".github/workflows/${agent}.yml"
content="$(gh api "repos/$ORG/$repo/contents/$stub" --jq '.content // empty' 2>/dev/null | base64 -d 2>/dev/null || true)"
Expand Down
28 changes: 19 additions & 9 deletions test/scripts/compliance-audit/centralized-stub-pins.bats
Original file line number Diff line number Diff line change
Expand Up @@ -209,33 +209,43 @@ stub_check() { # <workflow-yaml> — run the check for a stable-tier repo (mark
}

# ---------------------------------------------------------------------------
# ring_tier_for_repo() — maps a repo to its canary-ring tier (epic #495).
# ring_tier_for_repo() — maps a (repo) to its canary-ring tier for a given AGENT
# (epic #495; agent-aware since #1092). The tier is derived from the agent's
# rings[] in canary-rings.json, so it takes the agent as its first argument.
# ---------------------------------------------------------------------------
tier() {
run bash -c 'source "$1" >/dev/null 2>&1; ring_tier_for_repo "$2"' _ "$SCRIPT" "$1"
tier() { # <agent> <repo>
run bash -c 'source "$1" >/dev/null 2>&1; ring_tier_for_repo "$2" "$3"' _ "$SCRIPT" "$1" "$2"
}

@test "ring tier: .github-private is next" {
tier ".github-private"
tier "agent-shield" ".github-private"
[ "$status" -eq 0 ]
[ "$output" = "next" ]
}

@test "ring tier: .github is ring0 (dogfood)" {
tier ".github"
tier "agent-shield" ".github"
[ "$output" = "ring0" ]
}

@test "ring tier: TalkTerm and bmad-bgreat-suite are ring1" {
tier "TalkTerm"
tier "agent-shield" "TalkTerm"
[ "$output" = "ring1" ]
tier "bmad-bgreat-suite"
tier "agent-shield" "bmad-bgreat-suite"
[ "$output" = "ring1" ]
}

@test "ring tier: any other repo defaults to stable" {
tier "markets"
tier "agent-shield" "markets"
[ "$output" = "stable" ]
tier "broodly"
tier "agent-shield" "broodly"
[ "$output" = "stable" ]
}

# #1092: markets is ring1 for apply-repo-settings ONLY, stable for everyone else.
@test "ring tier: markets is ring1 for apply-repo-settings, stable otherwise (#1092)" {
tier "apply-repo-settings" "markets"
[ "$output" = "ring1" ]
tier "dev-lead" "markets"
[ "$output" = "stable" ]
}
133 changes: 126 additions & 7 deletions test/scripts/lib/ring-pins.bats
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,123 @@ setup() {
source "${REPO_ROOT}/scripts/lib/ring-pins.sh"
}

@test "ring_tier_for_repo maps each tier" {
[ "$(ring_tier_for_repo .github-private)" = "next" ]
[ "$(ring_tier_for_repo .github)" = "ring0" ]
[ "$(ring_tier_for_repo TalkTerm)" = "ring1" ]
[ "$(ring_tier_for_repo bmad-bgreat-suite)" = "ring1" ]
[ "$(ring_tier_for_repo markets)" = "stable" ]
[ "$(ring_tier_for_repo anything-else)" = "stable" ]
# #1092: ring_tier_for_repo is now agent-aware and derived from canary-rings.json.
# The tier is resolved from the given agent's rings[], falling back to the ring
# whose members contain "*" (today: stable). The meta-repo tiers are identical for
# every agent (next/ring0 are host-relative in the registry), so a representative
# agent exercises them.
@test "ring_tier_for_repo maps each tier (agent-aware, #1092)" {
[ "$(ring_tier_for_repo agent-shield .github-private)" = "next" ]
[ "$(ring_tier_for_repo agent-shield .github)" = "ring0" ]
[ "$(ring_tier_for_repo agent-shield TalkTerm)" = "ring1" ]
[ "$(ring_tier_for_repo agent-shield bmad-bgreat-suite)" = "ring1" ]
[ "$(ring_tier_for_repo agent-shield markets)" = "stable" ]
[ "$(ring_tier_for_repo agent-shield anything-else)" = "stable" ]
}

# #1092 AC4 — the point cases the issue enumerates verbatim.
@test "ring_tier_for_repo: markets is ring1 ONLY for apply-repo-settings (#1092 AC4)" {
[ "$(ring_tier_for_repo apply-repo-settings markets)" = "ring1" ]
[ "$(ring_tier_for_repo dev-lead markets)" = "stable" ]
# markets is stable for every other agent, too.
[ "$(ring_tier_for_repo auto-rebase markets)" = "stable" ]
[ "$(ring_tier_for_repo pr-auto-review markets)" = "stable" ]
}

@test "ring_tier_for_repo: TalkTerm/.github/.github-private are agent-invariant (#1092 AC4)" {
local a
for a in $(jq -r '(.agents // {}) | keys[]?' "${REPO_ROOT}/standards/canary-rings.json"); do
[ "$(ring_tier_for_repo "$a" TalkTerm)" = "ring1" ]
[ "$(ring_tier_for_repo "$a" .github)" = "ring0" ]
[ "$(ring_tier_for_repo "$a" .github-private)" = "next" ]
done
}

# #1092 AC2 — the whole risk of the change: every agent EXCEPT apply-repo-settings
# must resolve identically to the old agent-agnostic hardcoded logic, for every
# repo. The old logic is inlined here as an independent oracle (NOT the impl).
@test "ring_tier_for_repo: no behaviour change for the other 15 agents (#1092 AC2)" {
old_tier() { # the pre-#1092 hardcoded case statement, verbatim
case "$1" in
.github-private) printf 'next' ;;
.github) printf 'ring0' ;;
TalkTerm | bmad-bgreat-suite) printf 'ring1' ;;
*) printf 'stable' ;;
esac
}
local a repo
local -a repos=(.github-private .github TalkTerm bmad-bgreat-suite markets
broodly ContentTwin google-app-scripts some-unlisted-repo)
for a in $(jq -r '(.agents // {}) | keys[]?' "${REPO_ROOT}/standards/canary-rings.json"); do
[ "$a" = "apply-repo-settings" ] && continue
for repo in "${repos[@]}"; do
[ "$(ring_tier_for_repo "$a" "$repo")" = "$(old_tier "$repo")" ] \
|| { echo "drift: $a $repo -> $(ring_tier_for_repo "$a" "$repo") (old: $(old_tier "$repo"))"; false; }
done
done
# apply-repo-settings is the ONE intended difference: markets moves to ring1.
[ "$(ring_tier_for_repo apply-repo-settings markets)" != "$(old_tier markets)" ]
[ "$(ring_tier_for_repo apply-repo-settings markets)" = "ring1" ]
}

# #1092 AC4 — the registry-conformance regression (analogue of #1088's
# "RING_REUSABLES equals the registry"): for every (agent, repo) pair the resolved
# tier equals an INDEPENDENT derivation straight from canary-rings.json.
@test "ring_tier_for_repo equals the canary-rings.json derivation for every pair (#1092 AC4)" {
local rings="${REPO_ROOT}/standards/canary-rings.json"
# Independent oracle: expand $host/$org_infra tokens, return the first ring
# (registry order = tier order) whose members contain the repo basename, else
# the ring whose members contain "*".
expected_tier() {
jq -r --arg a "$1" --arg repo "${2##*/}" '
.agents[$a] as $ag
| ($ag.host | sub(".*/";"")) as $host
| ([.org_infra_repos[]? | sub(".*/";"")] - [$host]) as $orginfra
| ( $ag.rings
| map({ channel,
m: (reduce (.members[]?) as $x ([];
if $x == "$host" then . + [$host]
elif $x == "$org_infra" then . + $orginfra
else . + [($x | sub(".*/";""))] end)) }) ) as $r
| ( ([$r[] | select(.m | index($repo)) | .channel][0])
// ([$r[] | select(.m | index("*")) | .channel][0]) )' "$rings"
}
# The repo universe: every explicitly-named member plus the meta-repos and a
# couple of fleet repos not named in any ring.
local -a repos
mapfile -t repos < <(
{ jq -r '.agents[].rings[].members[] | select(startswith("$")|not) | select(. != "*") | sub(".*/";"")' "$rings"
printf '%s\n' .github .github-private markets broodly some-unlisted-repo
} | sort -u)
local a repo want got
for a in $(jq -r '(.agents // {}) | keys[]?' "$rings"); do
for repo in "${repos[@]}"; do
want="$(expected_tier "$a" "$repo")"
got="$(ring_tier_for_repo "$a" "$repo")"
[ "$got" = "$want" ] || { echo "mismatch: $a $repo -> got=$got want=$want"; false; }
done
done
}

# #1096 — fail CLOSED on an unreadable/corrupt registry rather than silently
# returning `stable`. A missing or unparseable source of truth must surface as an
# error (non-zero) so the audit/deploy cannot accept or emit incorrect stable pins
# during an infra outage — mirroring ring_host_current_channel_major's fail-closed
# probe (#870). A genuine no-match on a READABLE registry still falls back to stable.
@test "ring_tier_for_repo: fails closed on an unreadable/corrupt registry (#1096)" {
RING_PINS_REGISTRY="${BATS_TEST_TMPDIR}/does-not-exist.json" \
run ring_tier_for_repo agent-shield markets
[ "$status" -eq 3 ]
[[ "$output" != "stable" ]]
printf 'not-json{' > "${BATS_TEST_TMPDIR}/corrupt.json"
RING_PINS_REGISTRY="${BATS_TEST_TMPDIR}/corrupt.json" \
run ring_tier_for_repo agent-shield markets
[ "$status" -eq 3 ]
[[ "$output" != "stable" ]]
# a readable registry with a genuine no-match (unknown agent) still returns stable
run ring_tier_for_repo not-a-real-agent markets
[ "$status" -eq 0 ]
[ "$output" = "stable" ]
}

@test "ring_is_ring_reusable recognises the ring set (incl. dev-lead)" {
Expand Down Expand Up @@ -66,6 +176,15 @@ setup() {
[ "$(ring_canonical_ref dev-lead .github-private)" = "dev-lead/next" ]
}

# #1092 — end-to-end: because the tier is now agent-aware, ring_canonical_ref (and
# thus the deploy/audit that consume it) computes markets as apply-repo-settings's
# ring1 channel, but every other agent still gets markets/stable.
@test "ring_canonical_ref: apply-repo-settings markets resolves to ring1, others to stable (#1092)" {
[ "$(ring_canonical_ref apply-repo-settings markets)" = "apply-repo-settings/ring1" ]
[ "$(ring_canonical_ref apply-repo-settings markets 1)" = "apply-repo-settings/v1-ring1" ]
[ "$(ring_canonical_ref auto-rebase markets)" = "auto-rebase/stable" ]
}

@test "ring_canonical_ref: major-aware form yields <agent>/v<major>-<tier> (#657 F3)" {
# A major argument opts the ref into the major-scoped channel line; the tier
# is still the repo's ring tier.
Expand Down
Loading