Skip to content

Commit f4056d4

Browse files
lpcoxCopilot
andcommitted
fix: address PR review feedback for token optimizer workflows
- Replace schedule: daily with workflow_run trigger (runs after analyzer completes, not before) - Add branches: [main] restriction for security - Fix no-report path to avoid creating blocking labeled issues (prevents skip-if-match from permanently blocking future runs) - Add workflow name resolution from display names to file paths (handles mismatch between report display names and file stems) - Add || true to grep commands to prevent false failures on no matches - Use -h flag on grep for cleaner multi-file output Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent c3e29c3 commit f4056d4

File tree

4 files changed

+128
-80
lines changed

4 files changed

+128
-80
lines changed

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

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

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

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
---
22
description: Daily Claude token optimization advisor — reads the latest token usage report and creates actionable recommendations to reduce token consumption for the most expensive workflow
33
on:
4-
schedule: daily
4+
workflow_run:
5+
workflows: ["Daily Claude Token Usage Analyzer"]
6+
types: [completed]
7+
branches: [main]
58
workflow_dispatch:
69
skip-if-match:
710
query: 'is:issue is:open label:claude-token-optimization'
@@ -42,7 +45,7 @@ gh issue list --repo "$GITHUB_REPOSITORY" \
4245
--json number,title,body,createdAt,url
4346
```
4447

45-
If no report exists, create a brief issue stating that no token usage report was found and that the `claude-token-usage-analyzer` workflow should run first. Then stop.
48+
If no report exists, do **not** create an issue. Simply log a message noting that no token usage report was found and that the `claude-token-usage-analyzer` workflow should run first. Then stop without calling any safe-output tools.
4649

4750
Read the full issue body to extract per-workflow statistics.
4851

@@ -63,10 +66,18 @@ Extract these key metrics for the target workflow:
6366

6467
## Step 3: Analyze the Workflow Definition
6568

66-
Read the workflow's `.md` source file to understand its configuration:
69+
Resolve the workflow file name from the display name in the report. The report table uses display names (e.g., "Smoke Claude") but the files use kebab-case (e.g., `smoke-claude.md`). Map the name by searching for a matching `name:` field:
6770

6871
```bash
69-
cat ".github/workflows/<workflow-name>.md"
72+
# Find workflow file by display name
73+
DISPLAY_NAME="Smoke Claude" # from report
74+
WORKFLOW_FILE=$(grep -rl "^name: ${DISPLAY_NAME}$" .github/workflows/*.md 2>/dev/null | head -1)
75+
# Fallback: try kebab-case conversion
76+
if [ -z "$WORKFLOW_FILE" ]; then
77+
KEBAB=$(echo "$DISPLAY_NAME" | tr '[:upper:]' '[:lower:]' | tr ' ' '-')
78+
WORKFLOW_FILE=".github/workflows/${KEBAB}.md"
79+
fi
80+
cat "$WORKFLOW_FILE"
7081
```
7182

7283
Analyze:
@@ -81,27 +92,32 @@ Analyze:
8192
Download the most recent successful run's artifacts to understand actual tool usage:
8293

8394
```bash
84-
# Find the latest successful run
95+
# Find the latest successful run using the resolved workflow file
96+
LOCK_FILE="$(basename "$WORKFLOW_FILE" .md).lock.yml"
8597
RUN_ID=$(gh run list --repo "$GITHUB_REPOSITORY" \
86-
--workflow "<workflow-name>.lock.yml" \
98+
--workflow "$LOCK_FILE" \
8799
--status success --limit 1 \
88100
--json databaseId --jq '.[0].databaseId')
89101

90-
# Download artifacts
91-
TMPDIR=$(mktemp -d)
92-
gh run download "$RUN_ID" --repo "$GITHUB_REPOSITORY" \
93-
--name agent-artifacts --dir "$TMPDIR" 2>/dev/null || \
94-
gh run download "$RUN_ID" --repo "$GITHUB_REPOSITORY" \
95-
--name agent --dir "$TMPDIR" 2>/dev/null
96-
97-
# Check token usage
98-
find "$TMPDIR" -name "token-usage.jsonl" -exec cat {} \;
99-
100-
# Check agent stdio log for tool calls
101-
find "$TMPDIR" -name "agent-stdio.log" -exec grep "^●" {} \;
102-
103-
# Check prompt size
104-
find "$TMPDIR" -name "prompt.txt" -exec wc -c {} \;
102+
if [ -z "$RUN_ID" ] || [ "$RUN_ID" = "null" ]; then
103+
echo "No successful runs found for $LOCK_FILE — skipping artifact analysis"
104+
else
105+
# Download artifacts
106+
TMPDIR=$(mktemp -d)
107+
gh run download "$RUN_ID" --repo "$GITHUB_REPOSITORY" \
108+
--name agent-artifacts --dir "$TMPDIR" 2>/dev/null || \
109+
gh run download "$RUN_ID" --repo "$GITHUB_REPOSITORY" \
110+
--name agent --dir "$TMPDIR" 2>/dev/null
111+
112+
# Check token usage
113+
find "$TMPDIR" -name "token-usage.jsonl" -exec cat {} \;
114+
115+
# Check agent stdio log for tool calls (|| true to handle no matches)
116+
find "$TMPDIR" -name "agent-stdio.log" -exec grep -h "^●" {} \; || true
117+
118+
# Check prompt size
119+
find "$TMPDIR" -name "prompt.txt" -exec wc -c {} \;
120+
fi
105121
```
106122

107123
From the artifacts, determine:

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

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

0 commit comments

Comments
 (0)