Skip to content

Commit 594dbed

Browse files
authored
feat: migrate token optimizer prompts to gh-proxy mode with bash+jq efficiency guidance (#29415)
1 parent 0952fa8 commit 594dbed

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

.github/workflows/copilot-token-optimizer.lock.yml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.github/workflows/copilot-token-optimizer.md

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,33 @@ You are the Copilot Token Optimizer. Pick one high-cost workflow, audit recent r
126126
3. Propose safe, high-impact optimizations with evidence.
127127
4. Publish one issue and update optimization history.
128128

129+
## Data Access Guidelines
130+
131+
All GitHub API access goes through the `gh` CLI via the cli-proxy — there are **no GitHub MCP tools** available. Always filter API responses with `--jq` or pipe through `jq` to extract only the fields you need. Loading full JSON payloads into context wastes tokens; every extra field is overhead.
132+
133+
**Preferred patterns:**
134+
135+
```bash
136+
REPO="${{ github.repository }}"
137+
138+
# ✅ Extract only the fields you need from a file
139+
gh api "repos/$REPO/contents/.github/workflows/my-workflow.md" \
140+
--jq '.content' | base64 -d
141+
142+
# ✅ List workflow runs — keep only essential metadata
143+
gh api "repos/$REPO/actions/workflows/my-workflow.yml/runs?per_page=10" \
144+
--jq '.workflow_runs[] | {id, name, conclusion, run_started_at}'
145+
146+
# ✅ Combine multi-step reads into one bash block with pipes
147+
gh api "repos/$REPO/contents/.github/workflows/my-workflow.md" \
148+
--jq '.content' | base64 -d | sed -n '1,/^---$/{ /^---$/d; p }' | head -40
149+
150+
# ❌ Never load full unfiltered responses — drops everything into context
151+
gh api "repos/$REPO/actions/workflows/my-workflow.yml/runs"
152+
```
153+
154+
Prefer `--jq` on `gh api` calls over a separate `| jq` step when the filter is simple — it avoids piping the full response through the shell. Use `| jq` for multi-step transformations or when chaining with other commands.
155+
129156
## Data Inputs
130157

131158
- `/tmp/gh-aw/token-audit/all-runs.json`: full 7-day run data (`gh aw logs --json`).
@@ -157,7 +184,7 @@ Use this compact analysis matrix:
157184

158185
| Area | Required checks | Output |
159186
|---|---|---|
160-
| Tool usage | Compare configured tools from workflow source (read via `gh api` through cli-proxy) vs observed usage across multiple runs | Keep / Consider removing / Remove |
187+
| Tool usage | Compare configured tools from workflow source (read with `gh api … --jq '.content' \| base64 -d \| sed -n …` to extract only the frontmatter) vs observed usage across multiple runs | Keep / Consider removing / Remove |
161188
| Token efficiency | Evaluate token totals, effective tokens, cache efficiency, turns | Top token waste drivers |
162189
| Reliability | Repeated errors, warnings, retries, missing tools | Token waste from failures |
163190
| Prompt efficiency | Redundant instructions, overlong sections, avoidable iteration | Prompt reduction opportunities |
@@ -180,7 +207,26 @@ Rules:
180207

181208
## Phase 3 — Read Workflow Source
182209

183-
Use `gh` CLI requests (via cli-proxy) to read the target workflow `.md` source and validate. Run `gh` commands normally in bash steps; cli-proxy forwards them over its HTTP interface:
210+
Use `gh api` with `--jq` (via cli-proxy) to read the target workflow `.md` source. Extract only the sections you need — do not load the whole file if a targeted slice is sufficient.
211+
212+
```bash
213+
REPO="${{ github.repository }}"
214+
# Replace <workflow-name> with the actual .md filename, e.g. "copilot-agent-analysis"
215+
WF_PATH=".github/workflows/<workflow-name>.md"
216+
217+
# Read the full source (only when necessary — prefer targeted slices below)
218+
gh api "repos/$REPO/contents/$WF_PATH" --jq '.content' | base64 -d
219+
220+
# Extract frontmatter only (tools, features, network, permissions)
221+
gh api "repos/$REPO/contents/$WF_PATH" --jq '.content' | base64 -d \
222+
| awk '/^---$/{n++; if(n==2) exit} n==1'
223+
224+
# Extract the prompt body only (everything after the closing ---)
225+
gh api "repos/$REPO/contents/$WF_PATH" --jq '.content' | base64 -d \
226+
| awk 'f; /^---$/{f=1}'
227+
```
228+
229+
Validate from the source:
184230

185231
- configured tools and feature flags
186232
- imported shared components

0 commit comments

Comments
 (0)