Skip to content

chore: optimize AI agent context and implement CI cost controls#5335

Merged
jamesarich merged 2 commits intomainfrom
chore/agent-efficiency
May 2, 2026
Merged

chore: optimize AI agent context and implement CI cost controls#5335
jamesarich merged 2 commits intomainfrom
chore/agent-efficiency

Conversation

@jamesarich
Copy link
Copy Markdown
Collaborator

  • Modularize AGENTS.md by relocating detailed rules to the .skills/ directory.
  • Add scripts/sort-strings.py and strings-index.txt to organize and index string resources.
  • Introduce CI cost control guidelines and a pre-commit ai-guardrail.sh to prevent binary and log leaks.
  • Expand .copilotignore, .aiexclude, and .gitattributes to reduce context window usage and indexing costs.
  • Establish .agent_memory/session_context.md for persistent task tracking and project state.
  • Update SKILL.md for code reviews with specific Git and PR hygiene requirements.

- Modularize `AGENTS.md` by relocating detailed rules to the `.skills/` directory.
- Add `scripts/sort-strings.py` and `strings-index.txt` to organize and index string resources.
- Introduce CI cost control guidelines and a pre-commit `ai-guardrail.sh` to prevent binary and log leaks.
- Expand `.copilotignore`, `.aiexclude`, and `.gitattributes` to reduce context window usage and indexing costs.
- Establish `.agent_memory/session_context.md` for persistent task tracking and project state.
- Update `SKILL.md` for code reviews with specific Git and PR hygiene requirements.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces repository-level “AI agent token/cost hygiene” improvements by modularizing agent guidance into .skills/, adding guardrails against committing high-cost artifacts, and formalizing a string-resource indexing workflow to reduce large-file churn.

Changes:

  • Adds tooling to keep strings.xml sorted and to regenerate a searchable strings-index.txt.
  • Introduces an AI commit guardrail script and CI cost-control guidance.
  • Expands AI indexing ignore/exclude rules and adds .gitattributes settings intended to reduce review/indexing costs.

Reviewed changes

Copilot reviewed 10 out of 11 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
scripts/sort-strings.py New Python helper to sort string resources and regenerate the strings index.
scripts/ai-guardrail.sh New guardrail script intended for use as a pre-commit check.
core/resources/src/commonMain/composeResources/values/strings.xml Large re-ordering/grouping of strings; content retained but reorganized.
AGENTS.md Condenses “unified guide” and points to .skills/ + session context.
.skills/compose-ui/strings-index.txt New generated index of string keys for quick lookup.
.skills/code-review/SKILL.md Adds Git/PR hygiene rules to the code review playbook.
.skills/ci-cost-control/SKILL.md Adds CI cost-control playbook (run-checking guidance, rerun strategy).
.gitattributes Adds linguist/binary settings intended to reduce PR summary/indexing costs.
.copilotignore Expands Copilot ignore rules for binaries, artifacts, and resource churn.
.aiexclude Mirrors .copilotignore for other AI tools.
.agent_memory/session_context.md Adds a persistent session handover/project-state document.
Comments suppressed due to low confidence (1)

.copilotignore:55

  • The comment says this section ignores translations, but the glob **/composeResources/**/values*/*.xml also matches the base values/strings.xml (no locale qualifier). If you only mean localized resources, consider narrowing this to values-*/ (or update the comment if excluding the base file is intentional).
# ── Resources ────────────────────────────────────────────────────────────────
# Ignore translations (reduces churn and indexing tokens)
**/values-*/strings.xml
**/composeResources/**/values*/*.xml

Comment thread scripts/sort-strings.py
Comment on lines +10 to +13
print(f"Reading {xml_path}...")
with open(xml_path, 'r') as f:
content = f.read()

Comment thread scripts/ai-guardrail.sh Outdated
Comment on lines +20 to +29
STAGED_FILES=$(git diff --cached --name-only)
VIOLATIONS=()

for file in $STAGED_FILES; do
for pattern in "${FORBIDDEN_PATTERNS[@]}"; do
if [[ $file =~ $pattern ]]; then
VIOLATIONS+=("$file (matched $pattern)")
fi
done
done
<!-- ADD -->
<string name="add">Add</string>
<string name="add_a_note">Add a private note…</string>
<string name="add_channels_description">The following channels were found in the QR code. Select the once you would like to add to your device. Existing channels will be preserved.</string>
Comment thread AGENTS.md Outdated
- **Memory Persistence:** Update `.agent_memory/session_context.md` at the end of every session or major task.
- **Bootstrap First:** Run the mandatory bootstrap steps in `.skills/project-overview/SKILL.md` before any build.
- **Plan Before Execution:** Use `.agent_plans/` (git-ignored) for complex refactors.
- **Baseline Verification:** Always run: `./gradlew spotlessCheck spotlessApply detekt assembleDebug test allTests`
Comment thread .aiexclude Outdated

# Resources (Indexing non-English strings is a token sink)
**/values-*/strings.xml
**/composeResources/**/values*/*.xml
Comment thread .gitattributes Outdated
**/src/main/res/**/*.xml linguist-generated=true
**/composeResources/**/*.xml linguist-generated=true
**/strings.xml linguist-generated=true
*.json linguist-generated=true
Comment thread .skills/ci-cost-control/SKILL.md Outdated

### 2. Local First
NEVER use CI as a "remote compiler."
- You must run `./gradlew spotlessCheck detekt test` locally before pushing.
Comment thread .agent_memory/session_context.md Outdated
Comment on lines +3 to +13
## Current Project State
- **Token Mitigation (Phase 1-3):** COMPLETE.
- **Ignore Rules:** Stricter `.copilotignore` and `.aiexclude` are active.
- **Instruction Optimization:** `AGENTS.md` is modularized (~3KB base).
- **Cleanup:** 1.5GB of stale build artifacts and logs purged.
- **Guardrails:** Pre-commit AI guardrail script installed in `scripts/`. CI Cost Control skill active.

## Active Task History
- **Task:** Mitigate astronomical Copilot token costs.
- **Outcome:** Significantly reduced context tax by moving detailed rules to `.skills/` and ignoring binary/log artifacts.
- **Next Steps:** None. Mitigation project finalized.
Comment thread scripts/ai-guardrail.sh
@@ -0,0 +1,41 @@
#!/bin/bash
# Meshtastic AI Guardrail - Prevent binary/log leaks in commits

@jamesarich
Copy link
Copy Markdown
Collaborator Author

@copilot apply changes based on the comments in this thread

- Use explicit encoding='utf-8'/newline='\n' in sort-strings.py
- Fix ai-guardrail.sh to use NUL-delimited staged-files loop
- Add installation instructions to ai-guardrail.sh
- Fix typo in strings.xml: 'once' -> 'ones'
- Fix AGENTS.md: spotlessApply before spotlessCheck
- Narrow .aiexclude pattern to values-*/ only
- Narrow .gitattributes linguist-generated rules
- Add allTests to ci-cost-control SKILL.md command
- Rewrite session_context.md as dated append-only handover log

Agent-Logs-Url: https://github.com/meshtastic/Meshtastic-Android/sessions/1ffb7a86-714e-42eb-b9ab-3b9d49349b20

Co-authored-by: jamesarich <2199651+jamesarich@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI commented May 2, 2026

@copilot apply changes based on the comments in this thread

Applied all review feedback in commit e3fdac6:

  • sort-strings.py: All open() calls now use encoding='utf-8', newline='\n' to handle non-ASCII characters safely across platforms.
  • ai-guardrail.sh: Replaced for file in $STAGED_FILES with while IFS= read -r -d '' file reading from git diff --cached --name-only -z (NUL-delimited), so paths with spaces/special characters are handled correctly. Also added installation instructions (via core.hooksPath or direct .git/hooks/pre-commit symlink).
  • strings.xml: Fixed typo "once" → "ones".
  • AGENTS.md: Swapped to spotlessApply spotlessCheck so formatting is applied before verification.
  • .aiexclude: Narrowed values*/*.xml to values-*/*.xml so the base values/ directory is no longer excluded.
  • .gitattributes: Removed the broad **/src/main/res/**/*.xml and *.json linguist-generated marks; now only localized string XMLs (values-*/) and the generated strings-index.txt are marked.
  • ci-cost-control/SKILL.md: Added allTests to the local validation command.
  • session_context.md: Rewritten as a dated, append-only handover log with entries prepended at the top.

@jamesarich jamesarich merged commit 9e396cd into main May 2, 2026
6 checks passed
@jamesarich jamesarich deleted the chore/agent-efficiency branch May 2, 2026 18:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants