Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
40 changes: 31 additions & 9 deletions bin/fm-agents-md-budget.sh
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
#!/usr/bin/env bash
# fm-agents-md-budget.sh - fail when AGENTS.md nears Codex's project-doc byte budget.
# Usage:
# fm-agents-md-budget.sh [--file <path>] [--budget <bytes>] [--margin <bytes>]
# fm-agents-md-budget.sh [--file <path> | --repo <dir>] [--budget <bytes>] [--margin <bytes>]
#
# Codex reads at most project_doc_max_bytes of project instructions and silently
# truncates the rest, so a Codex primary, secondmate, or worker in this repo would
# lose the tail of the contract with no error. The default budget is Codex's
# DEFAULT_PROJECT_DOC_MAX_BYTES (32 * 1024 = 32768, codex-rs/config/src/config_toml.rs).
# The check passes only while the file stays at or under budget minus margin
# (default margin 2048 bytes), so growth is caught before it reaches the cut.
# --file defaults to this repository's AGENTS.md; sizes are counted in bytes.
# The budget is shared by every AGENTS.md from the repository root down to the
# working folder, so by default the check sums the root AGENTS.md and every nested
# AGENTS.md git tracks in --repo (default this repository; ignored paths such as
# projects/ are excluded). --file measures one file instead; sizes are in bytes.
# Prints one "ok" line on success; prints the overrun to stderr and exits 1 otherwise.
set -eu

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
FILE="$ROOT/AGENTS.md"
FILE=
REPO=$ROOT
BUDGET=32768
MARGIN=2048

usage() {
sed -n '2,13{s/^# \{0,1\}//;p;}' "$0"
sed -n '2,16{s/^# \{0,1\}//;p;}' "$0"
}

die() {
Expand All @@ -35,10 +39,11 @@ is_count() {

while [ "$#" -gt 0 ]; do
case "$1" in
--file|--budget|--margin)
--file|--repo|--budget|--margin)
[ "$#" -ge 2 ] || die "$1 needs a value" 2
case "$1" in
--file) FILE=$2 ;;
--repo) REPO=$2 ;;
--budget) BUDGET=$2 ;;
--margin) MARGIN=$2 ;;
esac
Expand All @@ -52,12 +57,29 @@ done
is_count "$BUDGET" || die "--budget must be a whole number of bytes: $BUDGET" 2
is_count "$MARGIN" || die "--margin must be a whole number of bytes: $MARGIN" 2
[ "$MARGIN" -lt "$BUDGET" ] || die "--margin $MARGIN must be smaller than --budget $BUDGET" 2
[ -f "$FILE" ] || die "not a readable file: $FILE" 2

bytes=$(wc -c < "$FILE" | tr -d ' ')
if [ -n "$FILE" ]; then
[ -f "$FILE" ] || die "not a readable file: $FILE" 2
bytes=$(wc -c < "$FILE" | tr -d ' ')
label=$FILE
else
[ -f "$REPO/AGENTS.md" ] || die "no root AGENTS.md in $REPO" 2
# quotePath=false keeps non-ASCII paths literal so each one is found on disk.
tracked=$(git -C "$REPO" -c core.quotePath=false ls-files -- AGENTS.md '*/AGENTS.md') \

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new nested AGENTS.md that has not been staged yet is not counted, so the local check passes while CI fails once the file is committed. The chain comes from git ls-files, which lists only indexed paths. Codex itself reads any AGENTS.md on disk in the chain, whether or not git tracks it. Fix: also list untracked files that are not ignored (git ls-files --cached --others --exclude-standard -- AGENTS.md '*/AGENTS.md'). That keeps projects/ out and counts a file a worker just created.

Failure scenario: A worker creates docs/AGENTS.md (3 KB) in a worktree whose root AGENTS.md is 28,234 bytes. It runs bin/fm-test-run.sh --near before git add, and the budget test reports ok at bytes=28234. The worker commits and opens the PR, and CI fails at 31,234 bytes, over the 30,720 limit. The local check missed what it exists to catch.

|| die "cannot list tracked AGENTS.md files in $REPO" 2
bytes=0
count=0
while IFS= read -r rel; do
[ -n "$rel" ] && [ -f "$REPO/$rel" ] || continue
bytes=$((bytes + $(wc -c < "$REPO/$rel" | tr -d ' ')))
count=$((count + 1))
done <<EOF_TRACKED
$tracked
EOF_TRACKED
label="the AGENTS.md chain ($count tracked files under $REPO)"
fi
limit=$((BUDGET - MARGIN))
if [ "$bytes" -gt "$limit" ]; then
die "$FILE is $bytes bytes, $((bytes - limit)) over the $limit-byte limit (Codex budget $BUDGET minus margin $MARGIN); move conditional detail to its owning skill or doc behind a pointer"
die "$label is $bytes bytes, $((bytes - limit)) over the $limit-byte limit (Codex budget $BUDGET minus margin $MARGIN); prune it as AGENTS.md \"Maintaining this file\" says, moving conditional detail to its owning skill or doc behind a pointer; do not raise the limit"
fi
printf 'fm-agents-md-budget: ok bytes=%s limit=%s budget=%s margin=%s headroom=%s\n' \
"$bytes" "$limit" "$BUDGET" "$MARGIN" "$((limit - bytes))"
6 changes: 6 additions & 0 deletions bin/fm-test-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1636,6 +1636,12 @@ families_for_changed_path() {
printf '%s\n' __script__:fm-documentation-audiences.test.sh
;;
esac
case "$path" in
# Every nested AGENTS.md shares Codex's budget, whatever arm owns its folder.
*/AGENTS.md)
printf '%s\n' "__script__:fm-agents-md-budget.test.sh"
;;
esac
case "$path" in
tests/fm-backend-herdr-eventwait.test.py)
printf '%s\n' real-herdr-gated
Expand Down
3 changes: 3 additions & 0 deletions docs/fork-decisions/2026-11-23-agents-md-chain-budget.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
| What | Files or functions | Upstream code touched | Status | Last checked sync |
| --- | --- | --- | --- | --- |
| The `AGENTS.md` byte-budget check sums the root and every nested `AGENTS.md` git tracks, because Codex shares one `project_doc_max_bytes` budget across the chain, and its overrun points at "Maintaining this file" and says to prune, not raise the limit | `bin/fm-agents-md-budget.sh`, `tests/fm-agents-md-budget.test.sh`, `docs/scripts.md`, `bin/fm-test-run.sh` (the `*/AGENTS.md` selection) | `docs/scripts.md`, `bin/fm-test-run.sh` | carried | 427056c4 |
2 changes: 1 addition & 1 deletion docs/scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ The shared no-mistakes gate lifecycle boundary is summarized in [architecture.md
| `fm_voice_records.py` | What a spoken answer may read, and the handover that queues real work |
| `backends/herdr-eventwait.py` | Subscribe to a Herdr session's native agent-status stream and print one projected line per event |
| `backends/herdr-workspace-move.py` | Send one presentation-only `workspace.move` request to a Herdr control socket |
| `fm-agents-md-budget.sh` | Fail when `AGENTS.md` exceeds Codex's project-doc byte budget minus a safety margin |
| `fm-agents-md-budget.sh` | Fail when the tracked `AGENTS.md` chain (root plus every nested one) exceeds Codex's shared project-doc byte budget minus a safety margin |
| `fm-agy-trust.sh` | Pre-register Antigravity CLI workspace trust for a task worktree before an agy spawn |
| `fm-allocation-lib.sh` | Own the task allocation-ownership contract for reused worktree and home pool slots |
| `fm-backlog-overdue.sh` | Wake firstmate, weekly while it lasts, for a queued backlog item whose date gate lapsed without a dispatch or answer |
Expand Down
25 changes: 25 additions & 0 deletions tests/fm-agents-md-budget.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,30 @@ test_counts_bytes_not_characters() {
pass "size is measured in bytes, as Codex truncates"
}

test_nested_tracked_agents_md_counts_toward_budget() {
local repo="$TMP_ROOT/repo" out rc
mkdir -p "$repo/sub" "$repo/projects/x"
git -C "$repo" init -q
printf 'projects/\n' > "$repo/.gitignore"
write_bytes "$repo/AGENTS.md" 600
write_bytes "$repo/sub/AGENTS.md" 300
write_bytes "$repo/projects/x/AGENTS.md" 5000
git -C "$repo" add .gitignore AGENTS.md sub/AGENTS.md
out=$("$CHECK" --repo "$repo" --budget 1000 --margin 100 2>&1) \
|| fail "root plus nested tracked AGENTS.md at the limit was refused: $out"
assert_contains "$out" "bytes=900 limit=900" "nested AGENTS.md was not summed, or an ignored one was"

printf 'a' >> "$repo/sub/AGENTS.md"
out=$("$CHECK" --repo "$repo" --budget 1000 --margin 100 2>&1)
rc=$?
[ "$rc" -eq 1 ] || fail "chain one byte over the limit exited $rc, want 1"
assert_contains "$out" "2 tracked files" "overrun did not name the summed chain"
assert_contains "$out" "is 901 bytes, 1 over the 900-byte limit" "chain overrun was not explained"
assert_contains "$out" '"Maintaining this file"' "overrun did not point at the maintenance section"
assert_contains "$out" "do not raise the limit" "overrun did not say to prune instead of raising the limit"
pass "every tracked AGENTS.md shares the budget; ignored ones do not count"
}

test_invalid_arguments_refuse() {
local rc
"$CHECK" --budget 12k >/dev/null 2>&1; rc=$?
Expand All @@ -60,4 +84,5 @@ test_invalid_arguments_refuse() {
test_repository_agents_md_fits_codex_budget
test_limit_boundary_is_budget_minus_margin
test_counts_bytes_not_characters
test_nested_tracked_agents_md_counts_toward_budget
test_invalid_arguments_refuse
Loading