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 .agents/skills/firstmate-coding-guidelines/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Apply the decision tree above to every line you are about to add to `AGENTS.md`.
If an addition needs more than a few lines of conditional detail (detail that matters only in a specific situation) or reference detail (a wire format, an exact schema, historical rationale), you are almost certainly adding it to the wrong file.
`AGENTS.md`'s token cost is paid by every session of every fleet member, every time, whether or not that session ever hits the situation the new lines describe.
A skill's cost is paid only by the sessions that actually load it.
`AGENTS.md` also has a hard ceiling: Codex silently truncates project instructions past its byte budget, and `bin/fm-agents-md-budget.sh`, run by its colocated test, fails once the file crosses that budget minus a margin.
`AGENTS.md` also has a hard ceiling: Codex silently truncates project instructions past its byte budget, and `bin/fm-agents-md-budget.sh`, run by its colocated test, fails once the root `AGENTS.md` plus every nested one git does not ignore crosses that shared budget minus a margin, so a nested `AGENTS.md` spends the root file's headroom.
When in doubt, write the fact into the skill or doc first by patching that owner's existing language, and add only the one-line trigger to `AGENTS.md`.

## Trigger hygiene
Expand Down
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 or would track 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 --cached --others --exclude-standard -- AGENTS.md '*/AGENTS.md') \
|| 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 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 does not ignore, 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), `.agents/skills/firstmate-coding-guidelines/SKILL.md` | `docs/scripts.md`, `bin/fm-test-run.sh`, that skill | 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 non-ignored `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
29 changes: 29 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,34 @@ 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 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"
mkdir -p "$repo/new"
printf 'a' > "$repo/new/AGENTS.md"
out=$("$CHECK" --repo "$repo" --budget 1000 --margin 100 2>&1)
assert_contains "$out" "is 902 bytes" "an unstaged nested AGENTS.md was not counted"
pass "every non-ignored AGENTS.md shares the budget, staged or not; ignored ones do not count"
}

test_invalid_arguments_refuse() {
local rc
"$CHECK" --budget 12k >/dev/null 2>&1; rc=$?
Expand All @@ -60,4 +88,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