Files
fleet/scripts/ci/shared-inventory.sh
CatoandClaude Sonnet 5.5 213d3f61b1 docs(experience): the experience belongs to the package, measured and enforced (#487)
* docs(experience): the experience belongs to the package, measured and enforced

The owner, 2026-11-06: every product installs the shared packages and the
screens people touch are still built per app — a text box with no microphone,
a header that is not ours, a comment list that works its own way. For a user
that is the same as no package at all. A non-technical person who says "make me
a website" must get the standard out of the door.

- AGENTS.md: "The experience belongs to the package". Packages ship behaviour
  and the layout skeleton; apps own words, backend and brand (tokens).
  Extending the package is the work, not a detour. A file that must be the
  exception says why on its first line.
- SHARED.md: "The experience standard" — what a person gets, from which
  package, measured per repo on 2026-11-06, how it is enforced.
- templates/agent-rules.md: one bullet, so the rule reaches every repo's
  AGENTS.md by the existing sync PRs and no future session can miss it.
- scripts/ci/experience-audit.mjs (+ tests): counts bare text areas, local
  chrome, composers, comment lists and article editors. package-composition
  runs it; a product whose package-plan.json sets experience.ceiling may not
  exceed it. The app template ships an empty ceiling, so a new product starts
  at zero.
- docs/experience-standard-plan.md: the living plan, status table and how to
  resume, for whoever picks this up next.
- shared-inventory.sh: its comment said UI markup was excluded so apps keep
  their own look; it now says behaviour is measured by the new audit.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmvpWNTLxXNfwUHS7MD

* docs(agents): regenerate the template rule blocks from templates/agent-rules.md

The audit job compares the three template AGENTS.md copies with the shipped
block; they had not been regenerated after the experience bullet was added.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmvpWNTLxXNfwUHS7MD

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-06 09:44:55 +01:00

277 lines
12 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# What has the fleet built more than once — and is it getting better or worse?
#
# The problem this solves is not "we don't know we duplicate things". Everyone
# knows. The problem is that knowing has never changed the number:
#
# - orangecat's ADR-0002 ("Unify Rate Limiting Modules", 2026-01-18) is still
# Status: Proposed. It names TWO implementations. There are now FOUR.
# - templates/ci/README.md says "deliberately ONE central script, not a copy
# per repo". auto-merge-sweep.sh exists in 21 repos in 8 distinct versions,
# 11,787 to 19,344 bytes. A fix in one reaches at most 9 of them.
#
# Both were written down. Writing it down is what failed. So this does not
# produce a document — it produces a NUMBER, and `--check` makes that number a
# ratchet: it may fall, it may hold, it may never rise. That is the whole
# mechanism. Nobody has to fix everything today; they only have to stop adding.
#
# Reads each repo's REMOTE default branch via the trees API, never a local
# checkout: clones drift, and a stale clone has already produced a redundant PR
# and a nearly-dropped gate in this fleet.
#
# Usage:
# shared-inventory.sh # report
# shared-inventory.sh --check # ratchet: exit 1 if any count ROSE
# shared-inventory.sh --update # rewrite the baseline (do this in a PR)
#
# Env: GH_OWNER (default bitbaum), GH_LIMIT (default 100)
set -uo pipefail
OWNER="${GH_OWNER:-bitbaum}"
LIMIT="${GH_LIMIT:-100}"
HERE="$(cd "$(dirname "$0")" && pwd)"
BASELINE="${SHARED_INVENTORY_BASELINE:-$HERE/shared-inventory.baseline}"
MODE=report
case "${1:-}" in
--check) MODE=check ;;
--update) MODE=update ;;
"") MODE=report ;;
*) echo "unknown argument: $1" >&2; exit 2 ;;
esac
command -v gh >/dev/null 2>&1 || { echo "gh CLI not found" >&2; exit 2; }
command -v jq >/dev/null 2>&1 || { echo "jq not found" >&2; exit 2; }
# ── The concerns we track ────────────────────────────────────────────────────
#
# Each line: name|regex over file paths. Deliberately conservative — a concern
# belongs here only if a shared implementation is PLAUSIBLE. Auth and DB schemas
# are excluded on purpose: they are coupled to a framework or a schema. UI
# markup is excluded from THIS file because its look is each app's own tokens;
# its BEHAVIOUR and layout skeleton are measured by experience-audit.mjs
# (SHARED.md "The experience standard"), which is where a bare text box or a
# home-made header is counted.
#
# chat-composer is the one UI entry, and it counts BEHAVIOUR, not looks: 13
# chats in 10 repos (2026-09-25) each re-learned — or never learned — the mic,
# 16px, the soft keyboard, stop and retry. SHARED.md "Chat — the standard"
# holds the checklist; this line only stops a new composer file appearing
# without anyone deciding it should.
# date-utils is anchored to a path segment: unanchored, `(dates)\.ts` matched
# ballot-updates.ts, auto-updates.ts and useActivityUpdates.ts (grep -i), and
# three files about UPDATES read as a rise in date helpers (2026-10-14).
CONCERNS='
automerge-script|scripts/ci/auto-merge-sweep\.sh$
rate-limit|(lib|src|app|apps|packages)/.*rate-?limit(er)?\.(ts|js)$
email-send|(lib|src)/.*(email|mailer|resend)/(index|client|send|config)\.(ts|js)$
logger|(lib|src)/.*logger\.(ts|js)$
api-envelope|(lib|src)/.*(route-helpers|api-response|apiResponse)\.(ts|js)$
health-route|.*api/health/route\.(ts|js)$
slug-util|(lib|src)/.*slug(ify)?\.(ts|js)$
date-utils|(lib|src)/(.*/)?(dates|date-utils)\.(ts|js)$
ai-provider-client|(lib|src|packages|apps)/.*(provider|call-provider)s?\.(ts|js)$
messaging-threads|(lib|src)/.*(message-utils|conversation-helpers|messages-view|thread-(utils|helpers))\.(ts|js)$
list-query|(lib|src|hooks)/.*([uU]se-?[fF]ilter(s|ing)?|[uU]se-?[dD]ebounce|filter-?state|pagination)\.(ts|js)$
chat-composer|(^|/)([cC]omposer|[cC]hat-?[iI]nput|chat-input)\.(tsx|ts|jsx|js)$
'
# ── Collect ──────────────────────────────────────────────────────────────────
#
# FORKS ARE EXCLUDED, and that exclusion is load-bearing. `openclaw` is a fork of
# openclaw/openclaw (1.5 GB of upstream), and counting it put 46 provider files,
# 7 loggers and 7 date utils into the totals — none of them written here, none
# of them ours to unify. A metric dominated by somebody else's codebase measures
# nothing and gets ignored, which is the failure mode every check in this repo
# exists to avoid.
repos=$(gh repo list "$OWNER" --limit "$LIMIT" --no-archived --source \
--json name,defaultBranchRef \
--jq '.[] | "\(.name)\t\(.defaultBranchRef.name // "")"' 2>/dev/null)
[ -n "$repos" ] || { echo "could not list repos for $OWNER" >&2; exit 2; }
# Paths that are never a copy somebody wrote: dependencies, build output and
# tests. dist/ is matched at the ROOT too — chatkit commits dist/composer.js,
# and `/dist/` alone counted it as a second chat composer (2026-10-14).
EXCLUDE_PATHS='node_modules|\.next/|(^|/)dist/|__tests__|\.test\.|\.spec\.|\.d\.ts'
# The fleet's packages are excluded the way the app starter is: a kit IS the
# one shared implementation that every copy should become. Counting chatkit's
# Composer as a chat composer, or bip-kit's slug.ts as a slug helper, made
# extracting a package read as MORE duplication — the ratchet punishing the
# exact move it exists to reward. The list is the derived register, so a new
# package is excluded the day it is registered, with no edit here.
REGISTER="$HERE/../../registers/packages.json"
KITS=""
if [ -f "$REGISTER" ]; then
KITS=$(jq -r --arg o "$OWNER" '.packages[].repo // empty
| select(startswith("https://github.com/" + $o + "/"))
| sub("^https://github.com/[^/]+/"; "")' "$REGISTER")
fi
kits_skipped=""
TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT
truncated=""
unreadable=""
inspected=0
# A repo we could not read contributes ZERO files, which is indistinguishable
# from a repo that deleted its duplicate. The fetch used to `|| continue` on any
# failure, so one TLS handshake timeout — common enough that a single sweep over
# 43 repos dropped NINE of them on 2026-09-15 — turned into an apparent fall in
# email-send, health-route and slug-util that nobody had earned. Retry, and then
# say the repo's name rather than quietly counting nothing.
fetch_tree() {
local name="$1" branch="$2" attempt body
for attempt in 1 2 3 4; do
if body=$(gh api "repos/$OWNER/$name/git/trees/$branch?recursive=1" 2>/dev/null) \
&& [ -n "$body" ]; then
printf '%s' "$body"
return 0
fi
sleep $((attempt * 3))
done
return 1
}
while IFS=$'\t' read -r name branch; do
[ -n "$name" ] || continue
[ -n "$branch" ] || continue
if grep -qx "$name" <<<"$KITS"; then
kits_skipped="$kits_skipped $name"
continue
fi
if ! body=$(fetch_tree "$name" "$branch"); then
unreadable="$unreadable $name"
continue
fi
# A truncated tree UNDERCOUNTS, which would read as progress. Say so.
if [ "$(printf '%s' "$body" | jq -r '.truncated // false')" = "true" ]; then
truncated="$truncated $name"
fi
# The app starter is excluded, in fleet (templates/app/) and in the copy
# Loki scaffolds from (scripts/site-template/): it is the ONE canonical source
# every product is made from, not a duplicate. Each product rendered from it
# is still counted, so a concern the starter carries shows up once per
# product — which is the honest number.
printf '%s' "$body" \
| jq -r '.tree[]? | select(.type=="blob") | .path' 2>/dev/null \
| grep -viE "$EXCLUDE_PATHS" \
| grep -vE '^templates/app/|^scripts/site-template/' \
> "$TMP/$name.files"
inspected=$((inspected + 1))
done <<< "$repos"
# ── Count ────────────────────────────────────────────────────────────────────
current="$TMP/current"
: > "$current"
for line in $CONCERNS; do
[ -n "$line" ] || continue
concern="${line%%|*}"
re="${line#*|}"
repos_with=0
copies=0
who=""
for f in "$TMP"/*.files; do
[ -e "$f" ] || continue
r="$(basename "$f" .files)"
n=$(grep -icE "$re" "$f" 2>/dev/null || true)
n=${n:-0}
if [ "$n" -gt 0 ]; then
grep -iE "$re" "$f" | sed "s|^| $r: |" >> "$TMP/$concern.where"
repos_with=$((repos_with + 1))
copies=$((copies + n))
who="$who $r($n)"
fi
done
printf '%s\t%s\t%s\n' "$concern" "$repos_with" "$copies" >> "$current"
if [ "$MODE" = report ]; then
printf '%-20s %2d repos %3d files %s\n' "$concern" "$repos_with" "$copies" \
"$(echo "$who" | cut -c1-80)"
fi
done
if [ "$MODE" = report ]; then
echo
echo "inspected $inspected repo(s) on their default branches"
[ -n "$kits_skipped" ] && echo "not counted (fleet packages — the shared implementation):$kits_skipped"
[ -n "$truncated" ] && echo "⚠ TRUNCATED trees (undercounted):$truncated"
[ -n "$unreadable" ] && echo "⚠ COULD NOT READ (undercounted):$unreadable"
echo
echo "A count is not a verdict — see SHARED.md for what is worth extracting."
exit 0
fi
if [ "$MODE" = update ]; then
# Writing a baseline from an undercount is the worst outcome available: it
# locks in a fall that never happened, and the ratchet then defends it.
if [ -n "$truncated$unreadable" ]; then
echo "✗ incomplete read (truncated:${truncated:- none}) (unreadable:${unreadable:- none})" >&2
echo " refusing to write a baseline from counts that are known to be low." >&2
exit 2
fi
cp "$current" "$BASELINE"
echo "baseline written: $BASELINE"
cat "$BASELINE"
exit 0
fi
# ── Ratchet ──────────────────────────────────────────────────────────────────
[ -f "$BASELINE" ] || { echo "no baseline at $BASELINE — run --update" >&2; exit 2; }
# A truncated tree undercounts, so a "pass" here could be an artefact. Refuse to
# certify rather than report a decrease that did not happen.
if [ -n "$truncated" ]; then
echo "✗ tree truncated for:$truncated — counts undercount, refusing to judge" >&2
exit 2
fi
# Same reasoning, different cause: a repo the API would not hand over is a repo
# whose duplication we did not see. Absence is not an answer.
if [ -n "$unreadable" ]; then
echo "✗ could not read:$unreadable — counts undercount, refusing to judge" >&2
exit 2
fi
fail=0
while IFS=$'\t' read -r concern was_repos was_copies; do
[ -n "$concern" ] || continue
now_line=$(grep -P "^${concern}\t" "$current" 2>/dev/null || grep "^${concern} " "$current" 2>/dev/null)
if [ -z "$now_line" ]; then
echo "· $concern — no longer measured (concern removed from the script?)"
continue
fi
now_copies=$(printf '%s' "$now_line" | cut -f3)
if [ "$now_copies" -gt "$was_copies" ]; then
echo "✗ $concern: $was_copies → $now_copies files. The ratchet only turns one way."
# The baseline holds counts, not paths, so name every file now counted:
# a red ratchet that cannot say WHERE sends someone off to re-derive it.
[ -f "$TMP/$concern.where" ] && cat "$TMP/$concern.where"
fail=1
elif [ "$now_copies" -lt "$was_copies" ]; then
echo "✓ $concern: $was_copies → $now_copies files — run --update to lock it in."
fi
done < "$BASELINE"
# A concern present now but absent from the baseline is new duplication that
# would otherwise slip in unmeasured.
while IFS=$'\t' read -r concern _ now_copies; do
[ -n "$concern" ] || continue
if ! cut -f1 "$BASELINE" | grep -qx "$concern"; then
echo "✗ $concern: not in the baseline ($now_copies files). Add it deliberately with --update."
fail=1
fi
done < "$current"
if [ "$fail" -ne 0 ]; then
echo
echo "Duplication rose. Either reuse what exists (SHARED.md) or, if this copy is"
echo "genuinely justified, run --update in the same PR so the increase is reviewed."
exit 1
fi
echo "✓ duplication did not increase (inspected $inspected repos)"