Skip to content

Commit 99d0c75

Browse files
authored
Merge branch 'main' into copilot/package-repo-as-described
2 parents 49cf6b2 + ea1f98e commit 99d0c75

11 files changed

Lines changed: 185 additions & 3831 deletions

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

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

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

Lines changed: 75 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
description: Daily optimizer that identifies a high-token-usage agentic workflow, audits its runs, and recommends efficiency improvements
2+
description: Daily optimizer that identifies a high-token-usage agentic workflow, audits its runs, and recommends efficiency improvements including inline sub-agent refactors when warranted
33
on:
44
schedule:
55
- cron: "daily around 14:00 on weekdays"
@@ -111,13 +111,13 @@ steps:
111111

112112
# Agentic Workflow Token Usage Optimizer
113113

114-
You are the Agentic Workflow Token Optimizer. Pick one high-cost workflow, audit recent runs, and create a conservative optimization issue with measurable savings.
114+
You are the Agentic Workflow Token Optimizer. Pick one high-cost workflow, audit recent runs, and create a conservative optimization issue with measurable savings. Your recommendations may include prompt, tool, reliability, setup-prefix, and inline sub-agent improvements when the evidence supports them.
115115

116116
## Objectives
117117

118118
1. Select one workflow using repo-memory and pre-aggregated data.
119-
2. Analyze tokens, turns, errors, and tool usage patterns across multiple runs.
120-
3. Propose safe, high-impact optimizations with evidence.
119+
2. Analyze tokens, turns, errors, tool usage patterns, and prompt structure across multiple runs.
120+
3. Propose safe, high-impact optimizations with evidence, including inline sub-agent refactors only when they are a clear fit.
121121
4. Publish one issue and update optimization history.
122122

123123
## Data Access Guidelines
@@ -172,31 +172,33 @@ Then collect run-level data for the selected workflow:
172172
- total and average turns
173173
- conclusions/error patterns
174174

175-
## Phase 2 — Analyze
175+
## Phase 2 — Analyze Runtime Behavior
176176

177177
Use this compact analysis matrix:
178178

179179
| Area | Required checks | Output |
180180
|---|---|---|
181-
| 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 |
181+
| Tool usage | Compare configured tools from workflow source vs observed usage across multiple runs | Keep / Consider removing / Remove |
182182
| Token efficiency | Evaluate token totals, effective tokens, cache efficiency, turns | Top token waste drivers |
183183
| Reliability | Repeated errors, warnings, retries, missing tools | Token waste from failures |
184184
| Prompt efficiency | Redundant instructions, overlong sections, avoidable iteration | Prompt reduction opportunities |
185+
| Structural optimization | Repeated setup/tool-call prefixes and sections suited for inline sub-agents | Extract setup / Add sub-agent / Keep in main agent |
185186

186187
### Tool-Usage Efficiency Patterns
187188

188189
When auditing runs, check for these common anti-patterns that waste tokens:
189190

190-
- **Batch independent reads**: look for sequential file reads or API calls that could be requested in a single tool-use block — each extra turn repeats the full context
191-
- **Chain bash commands**: look for separate bash tool calls that could be combined with `&&` — each call adds a full context echo
192-
- **Prefer typed tools**: look for `bash cat`, `bash grep`, `bash find -name` when `view`, `grep`, `glob` would return more concise output
193-
- **Consolidate GitHub API sequences**: look for multiple sequential `gh api` calls that could be combined into fewer round-trips with `jq` filtering
194-
- **Don't retry without diagnosing**: look for blind retries of the same failing operation without error analysis — each retry wastes a full turn
191+
- **Batch independent reads**: sequential file reads or API calls that could be requested in a single block
192+
- **Chain bash commands**: separate bash calls that could be combined with `&&`
193+
- **Prefer typed tools**: `bash cat`, `bash grep`, `bash find -name` where a more concise tool exists
194+
- **Consolidate GitHub API sequences**: multiple `gh api` calls that could be reduced with `jq` filtering
195+
- **Don't retry without diagnosing**: blind retries of the same failing operation without error analysis
195196

196197
Rules:
197198

198199
- Audit at least 5 runs when available before removal recommendations.
199200
- Never recommend removing a tool used in any successful run unless there is strong contrary evidence.
201+
- Only recommend inline sub-agents when the target workflow has no existing `## agent:` blocks and at least 3 major prompt sections.
200202
- Prioritize highest expected savings first.
201203

202204
## Phase 3 — Read Workflow Source
@@ -205,17 +207,16 @@ Use `gh api` with `--jq` (via cli-proxy) to read the target workflow `.md` sourc
205207

206208
```bash
207209
REPO="${{ github.repository }}"
208-
# Replace <workflow-name> with the actual .md filename, e.g. "copilot-agent-analysis"
209210
WF_PATH=".github/workflows/<workflow-name>.md"
210211

211-
# Read the full source (only when necessary — prefer targeted slices below)
212+
# Read the full source only when necessary
212213
gh api "repos/$REPO/contents/$WF_PATH" --jq '.content' | base64 -d
213214

214-
# Extract frontmatter only (tools, features, network, permissions)
215+
# Extract frontmatter only
215216
gh api "repos/$REPO/contents/$WF_PATH" --jq '.content' | base64 -d \
216217
| awk '/^---$/{n++; if(n==2) exit} n==1'
217218

218-
# Extract the prompt body only (everything after the closing ---)
219+
# Extract the prompt body only
219220
gh api "repos/$REPO/contents/$WF_PATH" --jq '.content' | base64 -d \
220221
| awk 'f; /^---$/{f=1}'
221222
```
@@ -225,9 +226,57 @@ Validate from the source:
225226
- configured tools and feature flags
226227
- imported shared components
227228
- prompt structure and verbosity
229+
- whether the prompt already uses inline sub-agents
228230
- network/sandbox constraints relevant to recommendations
229231

230-
## Phase 4 — Publish Optimization Issue
232+
## Phase 4 — Structural Optimization Checks
233+
234+
### Common Setup Prefix Analysis
235+
236+
Split the prompt body into major sections (`##` and `###`). For each section, inspect the first 10 lines and note explicit setup instructions, tool invocations, file reads, or repeated shell snippets.
237+
238+
A setup extraction recommendation is warranted only when:
239+
240+
- at least 2 sections repeat the same opening tool calls or setup instructions, and
241+
- moving them into a shared `## Setup` section would not change later section behavior.
242+
243+
If you recommend this optimization, capture:
244+
245+
- the shared setup text (quote the exact calls)
246+
- the affected sections
247+
- the proposed `## Setup` section text
248+
- a conservative savings estimate (5–15% per duplicated call removed)
249+
250+
### Inline Sub-Agent Opportunity Analysis
251+
252+
If the workflow has no inline sub-agents yet, score major sections using these dimensions:
253+
254+
| Dimension | Meaning | Max |
255+
|---|---|---|
256+
| Independence | Can the section run without outputs from other sections? | 3 |
257+
| Small-model adequacy | Is the work mostly extractive, classificatory, or formatting? | 3 |
258+
| Parallelism | Could it run concurrently with other sections? | 2 |
259+
| Size | Is the task substantial enough to justify an agent call? | 2 |
260+
261+
Scoring guidance:
262+
263+
- `6+`: strong candidate
264+
- `4–5`: moderate candidate
265+
- `<4`: keep in the main agent
266+
267+
Smaller models are a good fit for:
268+
269+
- summarizing one file or one code section
270+
- extracting specific fields from structured text
271+
- classifying items into a fixed set of categories
272+
- checking whether something meets a stated criterion
273+
- formatting already-derived data into tables or templates
274+
275+
Keep with the main agent when the section requires cross-referencing multiple heterogeneous sources, strategic synthesis, or writing the final authoritative issue body.
276+
277+
Recommend at most 3 inline sub-agents, and only when the combined opportunity is clearly material. Keep any proposed agent prompt concise and imperative.
278+
279+
## Phase 5 — Publish Optimization Issue
231280

232281
Create one issue with:
233282

@@ -239,27 +288,31 @@ Create one issue with:
239288
- estimated token savings per run
240289
- concrete action
241290
- evidence from observed runs
291+
- **Optional structural optimizations** for shared setup prefixes and inline sub-agents when supported by the analysis
242292
- **Caveats** (sampling limits, edge cases)
243293

244-
Use `<details>` blocks for long supporting tables.
245-
246294
### Report Formatting Requirements
247295

248-
- Use `###` for main sections and `####` for subsections inside the issue body.
296+
- Use `###` for main sections and `####` for subsections.
249297
- Keep the selected workflow, token profile summary, and ranked recommendations visible without collapsible sections.
250298
- Use `<details><summary>...</summary>` blocks for long supporting tables, raw run evidence, and lower-priority context.
251299
- If you cite specific workflow runs, format them as links like `[§12345](https://github.com/${{ github.repository }}/actions/runs/12345)` and include up to 3 under `**References:**`.
300+
- If you recommend inline sub-agents, include each candidate's task, why a smaller model fits, score breakdown, and the exact invocation change you want made in the main prompt.
252301

253-
## Phase 5 — Update Optimization Log
302+
## Phase 6 — Update Optimization Log
254303

255304
Append one entry to `/tmp/gh-aw/repo-memory/default/optimization-log.json`:
256305

257-
`{"date":"YYYY-MM-DD","workflow_name":"...","total_tokens_analyzed":N,"runs_audited":N,"recommendations_count":N,"estimated_savings_per_run":N}`
306+
`{"date":"YYYY-MM-DD","workflow_name":"...","total_tokens_analyzed":N,"runs_audited":N,"recommendations_count":N,"subagent_candidates":N,"estimated_savings_per_run":N}`
307+
308+
Use `subagent_candidates` for the count of inline sub-agent candidates you actually recommend in the issue body.
258309

259-
Load existing array if present, append, keep only last 30 entries, and save.
310+
Load the existing array if present, append, keep only the last 30 entries, and save.
260311

261312
## Guardrails
262313

263314
- Use pre-downloaded data; do not re-download logs.
264315
- Keep recommendations evidence-based and low-risk.
265316
- Do not modify audit snapshots; only update `optimization-log.json`.
317+
- If the target workflow already has inline sub-agents, do not recommend adding more unless there is a clearly separate, still-extractive task.
318+
- If no structural optimization is warranted, omit that section rather than padding the issue.

.github/workflows/ci.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
TARGET_REPO=$(mktemp -d)
4545
git init "$TARGET_REPO"
4646
cd "$TARGET_REPO"
47-
gh aw add "${{ github.workspace }}/workflows/agentic-token-audit.md" "${{ github.workspace }}/workflows/agentic-token-optimizer.md" "${{ github.workspace }}/workflows/daily-subagent-optimizer.md"
47+
gh aw add "${{ github.workspace }}/workflows/agentic-token-audit.md" "${{ github.workspace }}/workflows/agentic-token-optimizer.md"
4848
gh aw compile --validate --no-emit
4949
env:
5050
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -68,7 +68,7 @@ jobs:
6868
TARGET_REPO=$(mktemp -d)
6969
git init "$TARGET_REPO"
7070
cd "$TARGET_REPO"
71-
gh aw add "${{ github.repository }}/agentic-token-audit" "${{ github.repository }}/agentic-token-optimizer" "${{ github.repository }}/daily-subagent-optimizer"
71+
gh aw add "${{ github.repository }}/agentic-token-audit" "${{ github.repository }}/agentic-token-optimizer"
7272
gh aw compile --validate --no-emit
7373
env:
7474
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -101,9 +101,7 @@ jobs:
101101
.github/workflows/agentic-token-audit.md \
102102
.github/workflows/agentic-token-audit.lock.yml \
103103
.github/workflows/agentic-token-optimizer.md \
104-
.github/workflows/agentic-token-optimizer.lock.yml \
105-
.github/workflows/daily-subagent-optimizer.md \
106-
.github/workflows/daily-subagent-optimizer.lock.yml; do
104+
.github/workflows/agentic-token-optimizer.lock.yml; do
107105
if [[ ! -f "$path" ]]; then
108106
echo "Missing installed workflow file: $path"
109107
installed=false
@@ -125,6 +123,6 @@ jobs:
125123
if: steps.detect-workflows.outputs.installed == 'true'
126124
run: |
127125
set -euo pipefail
128-
gh aw run agentic-token-audit agentic-token-optimizer daily-subagent-optimizer --ref main
126+
gh aw run agentic-token-audit agentic-token-optimizer --ref main
129127
env:
130128
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)