From b51d659ce79a6d7638fe0e22b31e7c11dd7da6fd Mon Sep 17 00:00:00 2001 From: Kyle Sexton <153232337+kyle-sexton@users.noreply.github.com> Date: Fri, 23 Oct 2026 17:15:03 -0400 Subject: [PATCH 1/3] feat(harness-ops): show per-role lane spend in the morning brief The Lanes section prints one SPEND line per credential role from the lane-outcome records' costUsd, the CLI's list-price estimate: the median cost of the role's successful API-key legs, credit stops left out, times the runs of the lanes those legs ran in, per day, with the key legs read and their sum. Token legs are ignored; a role whose key legs carry no cost prints "cost not recorded". No new API calls. Co-Authored-By: Claude Opus 5.5 --- .../feat-morning-brief-role-spend-1e1353d1.md | 7 +++ .../harness-ops/skills/morning-brief/SKILL.md | 7 +-- .../morning-brief/morning-brief.test.sh | 51 +++++++++++++++++++ .../morning-brief/scripts/morning-brief.sh | 31 ++++++++++- 4 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 .changes/harness-ops/feat-morning-brief-role-spend-1e1353d1.md diff --git a/.changes/harness-ops/feat-morning-brief-role-spend-1e1353d1.md b/.changes/harness-ops/feat-morning-brief-role-spend-1e1353d1.md new file mode 100644 index 00000000000..d455663ed46 --- /dev/null +++ b/.changes/harness-ops/feat-morning-brief-role-spend-1e1353d1.md @@ -0,0 +1,7 @@ +--- +bump: minor +--- + +### Added + +- `/harness-ops:morning-brief` prints a `SPEND:` line in the Lanes section for each credential role whose API-key legs it read, from the lane-outcome records' `costUsd` (the CLI's list-price estimate, not billing): a per-day estimate from the median cost of the role's successful key legs, credit stops left out, times the runs of the lanes those legs ran in, with the number of key legs read and their sum. Subscription-token legs are left out, and a role whose key legs carry no cost prints `cost not recorded`. It makes no new API calls. diff --git a/plugins/harness-ops/skills/morning-brief/SKILL.md b/plugins/harness-ops/skills/morning-brief/SKILL.md index 24a2bba9d2b..21f037396b5 100644 --- a/plugins/harness-ops/skills/morning-brief/SKILL.md +++ b/plugins/harness-ops/skills/morning-brief/SKILL.md @@ -199,7 +199,7 @@ Upstream facts the script restates, each with its verification record: | Parked decisions | open issues with the decision label (default `status: needs-decision`) | Surfaces each one's RECOMMENDED line, the uppercase marker wins over an incidental lowercase mention; a case-insensitive fallback catches lowercase markers; pass `--decision-label` to pin | | Lane telemetry | the loop-lane telemetry issue's per-lane comments | Each lane's `last-cycle` age (marked `STALE` past `--stale-hours`, default 6) and any `flags:` | | Stranded findings | merged PRs whose unresolved review threads were **created after the merge** | One line per PR at its worst severity, with a finding count. The header prints the window: merges since the older of `--stranded-days` ago (default 3) and the repo's bookmark. Paging stops at that cutoff | -| Lanes | the last 7 days of runs of each lane workflow, and the check-run annotations of their jobs | One row per lane: runs, failures by class (rate-limit, max-turns, auth, other), limit stops, restarts (a run on an item whose previous run was a limit stop), and items with 3+ limit stops. Then turns used (max, p90) against the cap, with `CHECK` and run links when a run hit max-turns or the p90 is at or above 80% of the cap. Class, turns and cap come from the lane record when it carries them, else from the SDK result and the action's turn-cap annotation; a turn-cap stop counts as max-turns whatever class the record gives. Lanes default to the Claude workflows the repo has; `--lane-workflows` pins them. Annotations are read for `--lane-read-limit` runs (default 200): the newest 3 of each lane whatever their conclusion, then failed runs; past it the row says `unread` and the section `PARTIAL`. Then one `KEY:` line per credential role whose API key needs the owner, from the runs' lane-outcome records: `auth` when a run fell back off a refused key or a key leg failed with class auth, naming the key secret to replace; `credit-stop` when a run fell back on a credit stop, naming the role to rotate a spare key into. Each counts the flagged runs against the runs read for that role, and a role with no key problem prints nothing. The fallback reason marks a credit stop, never the class. Then a model-usage table, the baseline before model routing changes anything: one row per activity and profile over the runs whose annotations carry a lane record, with runs, passes (run conclusion success), median turns, max-turns hits and the models the records name, never sizes or costs. The activity is a pipeline lane's activity (` / `), else the lane; the profile is the record's `profile=`, else `unknown`. A row whose max-turns hits exceed `--max-turns-hits` gets a `RECOMMENDED` line; with the flag unset (the default) the table says no threshold is set | +| Lanes | the last 7 days of runs of each lane workflow, and the check-run annotations of their jobs | One row per lane: runs, failures by class (rate-limit, max-turns, auth, other), limit stops, restarts (a run on an item whose previous run was a limit stop), and items with 3+ limit stops. Then turns used (max, p90) against the cap, with `CHECK` and run links when a run hit max-turns or the p90 is at or above 80% of the cap. Class, turns and cap come from the lane record when it carries them, else from the SDK result and the action's turn-cap annotation; a turn-cap stop counts as max-turns whatever class the record gives. Lanes default to the Claude workflows the repo has; `--lane-workflows` pins them. Annotations are read for `--lane-read-limit` runs (default 200): the newest 3 of each lane whatever their conclusion, then failed runs; past it the row says `unread` and the section `PARTIAL`. Then one `KEY:` line per credential role whose API key needs the owner, from the runs' lane-outcome records: `auth` when a run fell back off a refused key or a key leg failed with class auth, naming the key secret to replace; `credit-stop` when a run fell back on a credit stop, naming the role to rotate a spare key into. Each counts the flagged runs against the runs read for that role, and a role with no key problem prints nothing. The fallback reason marks a credit stop, never the class. Then one `SPEND:` line per credential role whose API-key legs were read, from the records' `costUsd`, the CLI's estimate at [list price](https://platform.claude.com/docs/en/about-claude/pricing) and never what was billed ([Manage costs](https://code.claude.com/docs/en/costs#track-your-costs)): a per-day estimate, the median cost of the role's successful key legs (credit stops left out) times the runs of the lanes those legs ran in, divided by the days read, with the key legs read as `n=` and their sum. Subscription-token legs are left out, and a role whose key legs carry no cost prints `cost not recorded`. Then a model-usage table, the baseline before model routing changes anything: one row per activity and profile over the runs whose annotations carry a lane record, with runs, passes (run conclusion success), median turns, max-turns hits and the models the records name; it shows no sizes. The activity is a pipeline lane's activity (` / `), else the lane; the profile is the record's `profile=`, else `unknown`. A row whose max-turns hits exceed `--max-turns-hits` gets a `RECOMMENDED` line; with the flag unset (the default) the table says no threshold is set | | Cost | GitHub: `gh api` org billing usage (this month) and budgets; the org's repos and admins, and per live repo its direct collaborators and default-branch workflow files. Azure, when `az account show` succeeds: budgets with their forecast and the Cost Management scheduled actions (`az rest` GETs), `az security pricing list`, a month-to-date Cost Management query (`az rest` POST), `az monitor log-analytics workspace list` and one Usage-table query per workspace, `az monitor activity-log list --offset 1d`, `az security secure-scores list`, and `az advisor recommendation list --category Security` and `--category Cost` | One line per check, each `OK`, `ALERT`, `INFO` or `UNREADABLE`. `ALERT`: a product whose net this month is above 0; private-repo Actions minutes or artifact storage at `--usage-alert-percent` (default 50) of `--actions-included-minutes` or `--actions-included-storage-gb` (unset: the line is `INFO` with the amount used); a GitHub budget above 0 or not stopping at its limit; a direct collaborator who is not an org admin or in `--allowed-collaborators`; a workflow on `pull_request_target` or `workflow_run` (the event key bare, quoted, or in a flow sequence or mapping) that references a secret whose name contains `_CLAUDE_`, or passes every secret (`secrets: inherit`, `toJSON(secrets)`, a computed `secrets[...]` index); an Azure budget whose spend reached its amount or whose forecast is above `--forecast-alert` (default: the budget's amount); a `--required-budgets` budget that is missing, or the `--anomaly-alert` alert (default: any cost anomaly alert) missing or disabled, or either ending within `--expiry-days` (default 30); a workspace whose peak billable day over 31 days is at `--log-cap-alert-percent` (default 50) of its daily cap, or that has no cap; a month-to-date actual cost above 0 on any cost row whose service or meter category names Defender or Security Center; a Defender plan, policy assignment or role assignment write in the last day. A repo's Actions usage counts as private unless the org's repo list says it is public; with no repo list read, the line says every repo was counted as private. Budget lines print the latest usage date in the month-to-date cost query (its daily `UsageDate` column) as the data date, beside the read time; with the query unread they say the data date is unread. The Defender plans line lists any plan not on Free as `INFO`, never `ALERT`. The secure score and Advisor counts are `INFO`. The org is `--org`, else the repo's owner, and must be a GitHub owner name; the subscription is `--subscription`, else az's current one. A fixture run reads neither live unless `--org` or `--subscription` is passed as a flag; a settings file never makes it live | The Stuck section depends on two GraphQL facts, the result cap of a search and the fields of a @@ -216,13 +216,14 @@ wrong, read the current shapes live: - **Pointer**: the lane record and its classes, ci-workflows [`report-lane-outcome/action.yml`](https://github.com/melodic-software/ci-workflows/blob/main/.github/actions/report-lane-outcome/action.yml) and [`classify.cjs`](https://github.com/melodic-software/ci-workflows/blob/main/.github/actions/report-lane-outcome/classify.cjs); - the lane-outcome record's fields (`credential`, `fallback`), ci-workflows + the lane-outcome record's fields (`credential`, `fallback`, `costUsd`), ci-workflows [`docs/lane-outcome.md` § Fields](https://github.com/melodic-software/ci-workflows/blob/main/docs/lane-outcome.md#fields); the turn-cap annotation, claude-code-action [`base-action/src/run-claude-sdk.ts`](https://github.com/anthropics/claude-code-action/blob/main/base-action/src/run-claude-sdk.ts). - **As of**: 2026-10-23 - **Recheck trigger**: a ci-workflows release changes the lane record's fields, class names, - fallback reasons or record version, or a claude-code-action release changes its turn-cap message. + fallback reasons, what `costUsd` measures, or the record version, or a claude-code-action + release changes its turn-cap message. A pipeline lane's activity is read from the name of the job that carries the record, which the pipeline lanes name after the activity, so it costs no read beyond the jobs read. The diff --git a/plugins/harness-ops/skills/morning-brief/morning-brief.test.sh b/plugins/harness-ops/skills/morning-brief/morning-brief.test.sh index 482439802d5..f985e9c2f8b 100755 --- a/plugins/harness-ops/skills/morning-brief/morning-brief.test.sh +++ b/plugins/harness-ops/skills/morning-brief/morning-brief.test.sh @@ -1679,6 +1679,57 @@ assert_not_contains "keys live: a later record in one message flags no role" "$K assert_contains "keys live: the cap's overflow is reported" "$KEYS_LIVE" \ " PARTIAL: annotations read for 200 runs, the newest 3 of each lane and then failed runs first; 3 more were not read (raise --lane-read-limit)" +# --- Lanes: per-role spend from the records' costUsd ---------------------------- +# Fixture amounts are synthetic round numbers, not rates. AUTOMATIONS spans two +# lanes: pr-verify runs 100 and 101 are successful key legs (1 and 3), run +# 102's key leg hit a credit stop (4, left out of the median) and its +# subscription leg carries a cost (9, a token leg, ignored), and run 103 was +# not read; intake-triage run 110 is a successful key leg (2) and run 111 was +# not read. Median 2 per key leg times the 6 runs of those lanes, over 7 days; +# the sum covers every key leg read, the credit stop's included (1+3+4+2). +# REVIEWS run 120's key leg has no costUsd. PR_UNBLOCK run 130 is a token leg. +cat >"$TMP/spend-runs.json" <<'EOF' +{ + "pr-verify.yml": [ + {"id": 100, "html_url": "http://x/runs/100", "event": "workflow_run", "display_title": "PR S1", "head_sha": "s1", "status": "completed", "conclusion": "success", "created_at": "2026-07-19T10:00:00Z"}, + {"id": 101, "html_url": "http://x/runs/101", "event": "workflow_run", "display_title": "PR S2", "head_sha": "s2", "status": "completed", "conclusion": "success", "created_at": "2026-07-19T11:00:00Z"}, + {"id": 102, "html_url": "http://x/runs/102", "event": "workflow_run", "display_title": "PR S3", "head_sha": "s3", "status": "completed", "conclusion": "success", "created_at": "2026-07-19T12:00:00Z"}, + {"id": 103, "html_url": "http://x/runs/103", "event": "workflow_run", "display_title": "PR S4", "head_sha": "s4", "status": "completed", "conclusion": "success", "created_at": "2026-07-19T13:00:00Z"} + ], + "intake-triage.yml": [ + {"id": 110, "html_url": "http://x/runs/110", "event": "issues", "display_title": "Issue S", "head_sha": "t1", "status": "completed", "conclusion": "success", "created_at": "2026-07-19T10:00:00Z"}, + {"id": 111, "html_url": "http://x/runs/111", "event": "issues", "display_title": "Issue T", "head_sha": "t2", "status": "completed", "conclusion": "success", "created_at": "2026-07-19T11:00:00Z"} + ], + "pr-review-hosted.yml": [ + {"id": 120, "html_url": "http://x/runs/120", "event": "pull_request", "display_title": "PR R", "head_sha": "r1", "status": "completed", "conclusion": "success", "created_at": "2026-07-19T10:00:00Z"} + ], + "pr-unblock.yml": [ + {"id": 130, "html_url": "http://x/runs/130", "event": "workflow_dispatch", "display_title": "Fix #9", "head_sha": "u1", "status": "completed", "conclusion": "success", "created_at": "2026-07-19T10:00:00Z"} + ] +} +EOF +cat >"$TMP/spend-annotations.json" <<'EOF' +{ + "100": "pr-verify completed: credential=AUTOMATIONS_CLAUDE_API_KEY turns=5. lane-outcome {\"v\":1,\"lane\":\"pr-verify\",\"outcome\":\"success\",\"credential\":{\"secret\":\"AUTOMATIONS_CLAUDE_API_KEY\",\"kind\":\"api-key\",\"role\":\"AUTOMATIONS\"},\"turns\":5,\"costUsd\":1}", + "101": "pr-verify completed: credential=AUTOMATIONS_CLAUDE_API_KEY turns=5. lane-outcome {\"v\":1,\"lane\":\"pr-verify\",\"outcome\":\"success\",\"credential\":{\"secret\":\"AUTOMATIONS_CLAUDE_API_KEY\",\"kind\":\"api-key\",\"role\":\"AUTOMATIONS\"},\"turns\":5,\"costUsd\":3}", + "102": "pr-verify exited with: failure class=credit-stop credential=AUTOMATIONS_CLAUDE_API_KEY. lane-outcome {\"v\":1,\"lane\":\"pr-verify\",\"outcome\":\"failure\",\"class\":\"credit-stop\",\"credential\":{\"secret\":\"AUTOMATIONS_CLAUDE_API_KEY\",\"kind\":\"api-key\",\"role\":\"AUTOMATIONS\"},\"handoff\":true,\"costUsd\":4}\npr-verify completed: credential=AUTOMATIONS_CLAUDE_CODE_OAUTH_TOKEN fallback=credit-stop turns=4. lane-outcome {\"v\":1,\"lane\":\"pr-verify\",\"outcome\":\"success\",\"credential\":{\"secret\":\"AUTOMATIONS_CLAUDE_CODE_OAUTH_TOKEN\",\"kind\":\"oauth-token\",\"role\":\"AUTOMATIONS\"},\"fallback\":{\"reason\":\"credit-stop\"},\"turns\":4,\"costUsd\":9}", + "110": "Claude intake triage completed: credential=AUTOMATIONS_CLAUDE_API_KEY turns=3. lane-outcome {\"v\":1,\"lane\":\"intake-triage\",\"outcome\":\"success\",\"credential\":{\"secret\":\"AUTOMATIONS_CLAUDE_API_KEY\",\"kind\":\"api-key\",\"role\":\"AUTOMATIONS\"},\"turns\":3,\"costUsd\":2}", + "120": "pr-review completed: credential=REVIEWS_CLAUDE_API_KEY turns=7. lane-outcome {\"v\":1,\"lane\":\"pr-review\",\"outcome\":\"success\",\"credential\":{\"secret\":\"REVIEWS_CLAUDE_API_KEY\",\"kind\":\"api-key\",\"role\":\"REVIEWS\"},\"turns\":7}", + "130": "pr-unblock completed: credential=PR_UNBLOCK_CLAUDE_CODE_OAUTH_TOKEN fallback=no-key turns=6. lane-outcome {\"v\":1,\"lane\":\"fix-ci\",\"outcome\":\"success\",\"credential\":{\"secret\":\"PR_UNBLOCK_CLAUDE_CODE_OAUTH_TOKEN\",\"kind\":\"oauth-token\",\"role\":\"PR_UNBLOCK\"},\"fallback\":{\"reason\":\"no-key\"},\"turns\":6,\"costUsd\":5}\nactivity=fix-ci" +} +EOF +SPEND="$(section "$(bash "$BRIEF" --now "$NOW" --counts-json "$TMP/counts.json" --pr-json "$TMP/pr.json" \ + --decisions-json "$TMP/decisions.json" --telemetry-json "$TMP/telemetry.json" --merged-json "$TMP/merged-clean.json" \ + --lane-runs-json "$TMP/spend-runs.json" --lane-annotations-json "$TMP/spend-annotations.json" "${CLEAR_COST_ARGS[@]}" 2>&1)" "== Lanes")" +assert_contains "spend: the median successful key leg, scaled by the role's lane runs per day, labeled a list-price estimate" "$SPEND" \ + " SPEND: AUTOMATIONS ~\$1.71/day (list price, n=4) — estimate: median \$2.00 per successful key leg × 6 runs over 7d; \$10.00 summed over the legs read" +assert_eq "spend: one line per role prefix, across lanes and secrets" "1" "$(grep -c "^ SPEND: AUTOMATIONS " <<<"$SPEND")" +assert_not_contains "spend: never grouped by lane id" "$SPEND" "SPEND: pr-verify" +assert_contains "spend: a key leg with no costUsd says so" "$SPEND" " SPEND: REVIEWS cost not recorded (n=1)" +assert_not_contains "spend: a role with only token legs prints no spend line" "$SPEND" "SPEND: PR_UNBLOCK" +assert_not_contains "spend: the section stays readable" "$SPEND" "UNREADABLE" +assert_contains "spend: legs without costUsd elsewhere print cost not recorded" "$KEYS" " SPEND: INTAKE cost not recorded (n=1)" + # --- Lanes: model usage by activity and profile --------------------------------- # pr-unblock runs 30-36: resolve-conflicts with no profile= runs 30, 31, 34 and # 36 (two pass, two max-turns failures, turns 8, 40, 40, 10, so the median is diff --git a/plugins/harness-ops/skills/morning-brief/scripts/morning-brief.sh b/plugins/harness-ops/skills/morning-brief/scripts/morning-brief.sh index fea605bb2c7..f6b61429a9a 100755 --- a/plugins/harness-ops/skills/morning-brief/scripts/morning-brief.sh +++ b/plugins/harness-ops/skills/morning-brief/scripts/morning-brief.sh @@ -2015,7 +2015,7 @@ MODELS_JQ="$LANE_DEFS"' hits: (map(select(.class == "max-turns")) | length), models: ([.[].models[]] | unique) })) as $cells - | " model usage by activity and profile (runs with a read lane record; no sizes or costs):", + | " model usage by activity and profile (runs with a read lane record; no sizes):", (if ($cells | length) == 0 then " no lane records read" else empty end), ($cells[] | " \(.name) profile=\(.profile) runs=\(.runs) pass=\(.pass)/\(.runs) median-turns=\(.median // "-") max-turns-hits=\(.hits) models=\(if (.models | length) > 0 then .models | join(",") else "none recorded" end)"), (if $unread > 0 then " \($unread) run(s) left out: no lane record was read" else empty end), @@ -2045,6 +2045,28 @@ KEYS_JQ="$LANE_DEFS"' | (if $a > 0 then " KEY: \(.[0].role) auth (fallback on \($a) of \($m) runs read) — replace \(.[0].role)_CLAUDE_API_KEY" else empty end), (if $c > 0 then " KEY: \(.[0].role) credit-stop (\($c) of \($m)) — rotate a spare key into \(.[0].role)" else empty end) ' +# The spend lines: per credential role, an estimate from the costUsd (the +# CLI's list-price estimate, not billing) of its API-key legs. Token legs are +# left out. The median over the successful key legs that carry a cost, credit +# stops left out, times the runs of every lane those legs ran in, per day, +# and the sum over every key leg read. +# shellcheck disable=SC2016 # jq variables, as above +SPEND_JQ="$LANE_DEFS"' + def median: if length == 0 then null elif length % 2 == 1 then .[length / 2 | floor] else (.[length / 2 - 1] + .[length / 2]) / 2 end; + def usd: (. * 100 | round) as $c | "\($c / 100 | floor).\($c % 100 | tostring | if length == 1 then "0" + . else . end)"; + [ $lanes[] as $wf | lane_runs($wf) as $r | $r[] + | ($ann[0][.id | tostring] // "" | records)[] + | select(.credential.kind == "api-key" and (.credential.role | strings | test("\\A[A-Z0-9_]+\\z"))) + | {wf: $wf, runs: ($r | length), role: .credential.role, + cost: (.costUsd | if type == "number" and . >= 0 then . else null end)} + {ok: (.outcome == "success" and .class != "credit-stop")} ] + | group_by(.role)[] + | length as $n + | (map(select(.ok) | .cost | numbers) | sort | median) as $med + | (unique_by(.wf) | map(.runs) | add) as $runs + | (map(.cost | numbers) | add) as $sum + | if $med == null then " SPEND: \(.[0].role) cost not recorded (n=\($n))" + else " SPEND: \(.[0].role) ~$\($med * $runs / $days | usd)/day (list price, n=\($n)) — estimate: median $\($med | usd) per successful key leg × \($runs) runs over \($days)d; $\($sum | usd) summed over the legs read" end +' LANES_PARTIAL=0 CALLER_JOB_RE='^([a-z0-9][a-z0-9-]*) / ' @@ -2172,6 +2194,13 @@ print_lanes() { rows=" UNREADABLE: could not parse the lane-outcome records: ${rows%%$'\n'*}" } [[ -n "$rows" ]] && echo "$rows" + rows="$(jq -n -r --argjson since "$since" --argjson days "$LANE_DAYS" --slurpfile runs "$runs" --slurpfile ann "$ann" \ + --argjson lanes "$(printf '%s\n' "${LANE_WORKFLOWS[@]}" | jq -R . | jq -s .)" "$SPEND_JQ" 2>&1)" || { + UNREADABLE=$((UNREADABLE + 1)) + UNREADABLE_NAMES+=(lanes) + rows=" UNREADABLE: could not read the lane spend: ${rows%%$'\n'*}" + } + [[ -n "$rows" ]] && echo "$rows" rows="$(jq -n -r --argjson since "$since" --slurpfile runs "$runs" --slurpfile ann "$ann" \ --argjson threshold "${MAX_TURNS_HITS:-null}" \ --argjson lanes "$(printf '%s\n' "${LANE_WORKFLOWS[@]}" | jq -R . | jq -s .)" "$MODELS_JQ" 2>&1)" || { From 2c2c73e9dd895ea625504301df1c1e1c36ddd301 Mon Sep 17 00:00:00 2001 From: Kyle Sexton <153232337+kyle-sexton@users.noreply.github.com> Date: Fri, 23 Oct 2026 17:42:58 -0400 Subject: [PATCH 2/3] fix(harness-ops): bound the spend line's cost and pin its branches in tests A costUsd past 1e6 or of -0 no longer garbles the line; the median is shared from LANE_DEFS. New fixtures cover an even leg count, a successful credit-stop leg, negative and string costs, a cost past the bound and a role outside the pattern. Co-Authored-By: Claude Opus 5.5 --- .../skills/morning-brief/morning-brief.test.sh | 16 ++++++++++++++-- .../morning-brief/scripts/morning-brief.sh | 8 ++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/plugins/harness-ops/skills/morning-brief/morning-brief.test.sh b/plugins/harness-ops/skills/morning-brief/morning-brief.test.sh index f985e9c2f8b..53117f3f152 100755 --- a/plugins/harness-ops/skills/morning-brief/morning-brief.test.sh +++ b/plugins/harness-ops/skills/morning-brief/morning-brief.test.sh @@ -1687,7 +1687,11 @@ assert_contains "keys live: the cap's overflow is reported" "$KEYS_LIVE" \ # not read; intake-triage run 110 is a successful key leg (2) and run 111 was # not read. Median 2 per key leg times the 6 runs of those lanes, over 7 days; # the sum covers every key leg read, the credit stop's included (1+3+4+2). -# REVIEWS run 120's key leg has no costUsd. PR_UNBLOCK run 130 is a token leg. +# REVIEWS run 120's key leg has no costUsd. EVEN's successful legs in runs 121 +# and 122 cost 1 and 4 (median 2.5 over pr-review-hosted's 3 runs); its +# credit-stop leg (100) counts only in the sum, and its -1 and "2" costs are not +# costs. HUGE's cost is past the bound; role "bad" is not a role. PR_UNBLOCK +# run 130 is a token leg. cat >"$TMP/spend-runs.json" <<'EOF' { "pr-verify.yml": [ @@ -1701,7 +1705,9 @@ cat >"$TMP/spend-runs.json" <<'EOF' {"id": 111, "html_url": "http://x/runs/111", "event": "issues", "display_title": "Issue T", "head_sha": "t2", "status": "completed", "conclusion": "success", "created_at": "2026-07-19T11:00:00Z"} ], "pr-review-hosted.yml": [ - {"id": 120, "html_url": "http://x/runs/120", "event": "pull_request", "display_title": "PR R", "head_sha": "r1", "status": "completed", "conclusion": "success", "created_at": "2026-07-19T10:00:00Z"} + {"id": 120, "html_url": "http://x/runs/120", "event": "pull_request", "display_title": "PR R", "head_sha": "r1", "status": "completed", "conclusion": "success", "created_at": "2026-07-19T10:00:00Z"}, + {"id": 121, "html_url": "http://x/runs/121", "event": "pull_request", "display_title": "PR R2", "head_sha": "r2", "status": "completed", "conclusion": "success", "created_at": "2026-07-19T11:00:00Z"}, + {"id": 122, "html_url": "http://x/runs/122", "event": "pull_request", "display_title": "PR R3", "head_sha": "r3", "status": "completed", "conclusion": "success", "created_at": "2026-07-19T12:00:00Z"} ], "pr-unblock.yml": [ {"id": 130, "html_url": "http://x/runs/130", "event": "workflow_dispatch", "display_title": "Fix #9", "head_sha": "u1", "status": "completed", "conclusion": "success", "created_at": "2026-07-19T10:00:00Z"} @@ -1715,6 +1721,8 @@ cat >"$TMP/spend-annotations.json" <<'EOF' "102": "pr-verify exited with: failure class=credit-stop credential=AUTOMATIONS_CLAUDE_API_KEY. lane-outcome {\"v\":1,\"lane\":\"pr-verify\",\"outcome\":\"failure\",\"class\":\"credit-stop\",\"credential\":{\"secret\":\"AUTOMATIONS_CLAUDE_API_KEY\",\"kind\":\"api-key\",\"role\":\"AUTOMATIONS\"},\"handoff\":true,\"costUsd\":4}\npr-verify completed: credential=AUTOMATIONS_CLAUDE_CODE_OAUTH_TOKEN fallback=credit-stop turns=4. lane-outcome {\"v\":1,\"lane\":\"pr-verify\",\"outcome\":\"success\",\"credential\":{\"secret\":\"AUTOMATIONS_CLAUDE_CODE_OAUTH_TOKEN\",\"kind\":\"oauth-token\",\"role\":\"AUTOMATIONS\"},\"fallback\":{\"reason\":\"credit-stop\"},\"turns\":4,\"costUsd\":9}", "110": "Claude intake triage completed: credential=AUTOMATIONS_CLAUDE_API_KEY turns=3. lane-outcome {\"v\":1,\"lane\":\"intake-triage\",\"outcome\":\"success\",\"credential\":{\"secret\":\"AUTOMATIONS_CLAUDE_API_KEY\",\"kind\":\"api-key\",\"role\":\"AUTOMATIONS\"},\"turns\":3,\"costUsd\":2}", "120": "pr-review completed: credential=REVIEWS_CLAUDE_API_KEY turns=7. lane-outcome {\"v\":1,\"lane\":\"pr-review\",\"outcome\":\"success\",\"credential\":{\"secret\":\"REVIEWS_CLAUDE_API_KEY\",\"kind\":\"api-key\",\"role\":\"REVIEWS\"},\"turns\":7}", + "121": "pr-review completed. lane-outcome {\"v\":1,\"lane\":\"pr-review\",\"outcome\":\"success\",\"credential\":{\"secret\":\"EVEN_CLAUDE_API_KEY\",\"kind\":\"api-key\",\"role\":\"EVEN\"},\"costUsd\":1}\nother leg. lane-outcome {\"v\":1,\"lane\":\"pr-review\",\"outcome\":\"success\",\"credential\":{\"secret\":\"bad_CLAUDE_API_KEY\",\"kind\":\"api-key\",\"role\":\"bad\"},\"costUsd\":1}\nother leg. lane-outcome {\"v\":1,\"lane\":\"pr-review\",\"outcome\":\"success\",\"credential\":{\"secret\":\"HUGE_CLAUDE_API_KEY\",\"kind\":\"api-key\",\"role\":\"HUGE\"},\"costUsd\":1e17}", + "122": "pr-review completed. lane-outcome {\"v\":1,\"lane\":\"pr-review\",\"outcome\":\"success\",\"credential\":{\"secret\":\"EVEN_CLAUDE_API_KEY\",\"kind\":\"api-key\",\"role\":\"EVEN\"},\"costUsd\":4}\nother leg. lane-outcome {\"v\":1,\"lane\":\"pr-review\",\"outcome\":\"success\",\"credential\":{\"secret\":\"EVEN_CLAUDE_API_KEY\",\"kind\":\"api-key\",\"role\":\"EVEN\"},\"class\":\"credit-stop\",\"costUsd\":100}\nother leg. lane-outcome {\"v\":1,\"lane\":\"pr-review\",\"outcome\":\"success\",\"credential\":{\"secret\":\"EVEN_CLAUDE_API_KEY\",\"kind\":\"api-key\",\"role\":\"EVEN\"},\"costUsd\":-1}\nother leg. lane-outcome {\"v\":1,\"lane\":\"pr-review\",\"outcome\":\"success\",\"credential\":{\"secret\":\"EVEN_CLAUDE_API_KEY\",\"kind\":\"api-key\",\"role\":\"EVEN\"},\"costUsd\":\"2\"}", "130": "pr-unblock completed: credential=PR_UNBLOCK_CLAUDE_CODE_OAUTH_TOKEN fallback=no-key turns=6. lane-outcome {\"v\":1,\"lane\":\"fix-ci\",\"outcome\":\"success\",\"credential\":{\"secret\":\"PR_UNBLOCK_CLAUDE_CODE_OAUTH_TOKEN\",\"kind\":\"oauth-token\",\"role\":\"PR_UNBLOCK\"},\"fallback\":{\"reason\":\"no-key\"},\"turns\":6,\"costUsd\":5}\nactivity=fix-ci" } EOF @@ -1727,6 +1735,10 @@ assert_eq "spend: one line per role prefix, across lanes and secrets" "1" "$(gre assert_not_contains "spend: never grouped by lane id" "$SPEND" "SPEND: pr-verify" assert_contains "spend: a key leg with no costUsd says so" "$SPEND" " SPEND: REVIEWS cost not recorded (n=1)" assert_not_contains "spend: a role with only token legs prints no spend line" "$SPEND" "SPEND: PR_UNBLOCK" +assert_contains "spend: an even count takes the mean of the middle two; a successful credit-stop leg and a negative or string cost are left out" "$SPEND" \ + " SPEND: EVEN ~\$1.07/day (list price, n=5) — estimate: median \$2.50 per successful key leg × 3 runs over 7d; \$105.00 summed over the legs read" +assert_contains "spend: a cost past the bound is not a cost" "$SPEND" " SPEND: HUGE cost not recorded (n=1)" +assert_not_contains "spend: a role outside the role pattern prints nothing" "$SPEND" "SPEND: bad" assert_not_contains "spend: the section stays readable" "$SPEND" "UNREADABLE" assert_contains "spend: legs without costUsd elsewhere print cost not recorded" "$KEYS" " SPEND: INTAKE cost not recorded (n=1)" diff --git a/plugins/harness-ops/skills/morning-brief/scripts/morning-brief.sh b/plugins/harness-ops/skills/morning-brief/scripts/morning-brief.sh index f6b61429a9a..41d5f964487 100755 --- a/plugins/harness-ops/skills/morning-brief/scripts/morning-brief.sh +++ b/plugins/harness-ops/skills/morning-brief/scripts/morning-brief.sh @@ -1936,6 +1936,7 @@ LANE_DEFS=' elif test("class=max-turns(?![a-z-])|error_max_turns|exceeding the configured maximum of [0-9]+|maximum number of turns"; "i") then "max-turns" else "other" end; def hot($cap): $cap != null and . != null and . * 100 >= 80 * $cap; + def median: if length == 0 then null elif length % 2 == 1 then .[length / 2 | floor] else (.[length / 2 - 1] + .[length / 2]) / 2 end; # The v1 lane-outcome records in a run text, at most 8: after each marker, # the shortest prefix ending at a "}" that parses, since free text may follow # the JSON. A record whose credential or fallback is not an object is dropped. @@ -2004,7 +2005,6 @@ LANES_JQ="$LANE_DEFS"' # concluded success; a max-turns hit is a failed run of class max-turns. # shellcheck disable=SC2016 # jq variables, as above MODELS_JQ="$LANE_DEFS"' - def median: if length == 0 then null elif length % 2 == 1 then .[length / 2 | floor] else (.[length / 2 - 1] + .[length / 2]) / 2 end; [ $lanes[] as $wf | lane_runs($wf)[] ] as $all | ($all | map(select(.record))) as $read | (($all | length) - ($read | length)) as $unread @@ -2047,18 +2047,18 @@ KEYS_JQ="$LANE_DEFS"' ' # The spend lines: per credential role, an estimate from the costUsd (the # CLI's list-price estimate, not billing) of its API-key legs. Token legs are -# left out. The median over the successful key legs that carry a cost, credit +# left out, and so is a cost that is not a number from 0 to under 1e6 (the +# records are annotation text). The median over the successful key legs that carry a cost, credit # stops left out, times the runs of every lane those legs ran in, per day, # and the sum over every key leg read. # shellcheck disable=SC2016 # jq variables, as above SPEND_JQ="$LANE_DEFS"' - def median: if length == 0 then null elif length % 2 == 1 then .[length / 2 | floor] else (.[length / 2 - 1] + .[length / 2]) / 2 end; def usd: (. * 100 | round) as $c | "\($c / 100 | floor).\($c % 100 | tostring | if length == 1 then "0" + . else . end)"; [ $lanes[] as $wf | lane_runs($wf) as $r | $r[] | ($ann[0][.id | tostring] // "" | records)[] | select(.credential.kind == "api-key" and (.credential.role | strings | test("\\A[A-Z0-9_]+\\z"))) | {wf: $wf, runs: ($r | length), role: .credential.role, - cost: (.costUsd | if type == "number" and . >= 0 then . else null end)} + {ok: (.outcome == "success" and .class != "credit-stop")} ] + cost: (.costUsd | if type == "number" and . >= 0 and . < 1e6 then (if . == 0 then 0 else . end) else null end)} + {ok: (.outcome == "success" and .class != "credit-stop")} ] | group_by(.role)[] | length as $n | (map(select(.ok) | .cost | numbers) | sort | median) as $med From 423203be90f821238619685e54344aaba34e945e Mon Sep 17 00:00:00 2001 From: Kyle Sexton <153232337+kyle-sexton@users.noreply.github.com> Date: Fri, 23 Oct 2026 18:03:37 -0400 Subject: [PATCH 3/3] docs(harness-ops): point at the lane-outcome record for costUsd's meaning The Lanes row no longer restates what costUsd measures; the existing lane-outcome pointer record names it. Co-Authored-By: Claude Opus 5.5 --- plugins/harness-ops/skills/morning-brief/SKILL.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/harness-ops/skills/morning-brief/SKILL.md b/plugins/harness-ops/skills/morning-brief/SKILL.md index 21f037396b5..2b340954fe5 100644 --- a/plugins/harness-ops/skills/morning-brief/SKILL.md +++ b/plugins/harness-ops/skills/morning-brief/SKILL.md @@ -199,7 +199,7 @@ Upstream facts the script restates, each with its verification record: | Parked decisions | open issues with the decision label (default `status: needs-decision`) | Surfaces each one's RECOMMENDED line, the uppercase marker wins over an incidental lowercase mention; a case-insensitive fallback catches lowercase markers; pass `--decision-label` to pin | | Lane telemetry | the loop-lane telemetry issue's per-lane comments | Each lane's `last-cycle` age (marked `STALE` past `--stale-hours`, default 6) and any `flags:` | | Stranded findings | merged PRs whose unresolved review threads were **created after the merge** | One line per PR at its worst severity, with a finding count. The header prints the window: merges since the older of `--stranded-days` ago (default 3) and the repo's bookmark. Paging stops at that cutoff | -| Lanes | the last 7 days of runs of each lane workflow, and the check-run annotations of their jobs | One row per lane: runs, failures by class (rate-limit, max-turns, auth, other), limit stops, restarts (a run on an item whose previous run was a limit stop), and items with 3+ limit stops. Then turns used (max, p90) against the cap, with `CHECK` and run links when a run hit max-turns or the p90 is at or above 80% of the cap. Class, turns and cap come from the lane record when it carries them, else from the SDK result and the action's turn-cap annotation; a turn-cap stop counts as max-turns whatever class the record gives. Lanes default to the Claude workflows the repo has; `--lane-workflows` pins them. Annotations are read for `--lane-read-limit` runs (default 200): the newest 3 of each lane whatever their conclusion, then failed runs; past it the row says `unread` and the section `PARTIAL`. Then one `KEY:` line per credential role whose API key needs the owner, from the runs' lane-outcome records: `auth` when a run fell back off a refused key or a key leg failed with class auth, naming the key secret to replace; `credit-stop` when a run fell back on a credit stop, naming the role to rotate a spare key into. Each counts the flagged runs against the runs read for that role, and a role with no key problem prints nothing. The fallback reason marks a credit stop, never the class. Then one `SPEND:` line per credential role whose API-key legs were read, from the records' `costUsd`, the CLI's estimate at [list price](https://platform.claude.com/docs/en/about-claude/pricing) and never what was billed ([Manage costs](https://code.claude.com/docs/en/costs#track-your-costs)): a per-day estimate, the median cost of the role's successful key legs (credit stops left out) times the runs of the lanes those legs ran in, divided by the days read, with the key legs read as `n=` and their sum. Subscription-token legs are left out, and a role whose key legs carry no cost prints `cost not recorded`. Then a model-usage table, the baseline before model routing changes anything: one row per activity and profile over the runs whose annotations carry a lane record, with runs, passes (run conclusion success), median turns, max-turns hits and the models the records name; it shows no sizes. The activity is a pipeline lane's activity (` / `), else the lane; the profile is the record's `profile=`, else `unknown`. A row whose max-turns hits exceed `--max-turns-hits` gets a `RECOMMENDED` line; with the flag unset (the default) the table says no threshold is set | +| Lanes | the last 7 days of runs of each lane workflow, and the check-run annotations of their jobs | One row per lane: runs, failures by class (rate-limit, max-turns, auth, other), limit stops, restarts (a run on an item whose previous run was a limit stop), and items with 3+ limit stops. Then turns used (max, p90) against the cap, with `CHECK` and run links when a run hit max-turns or the p90 is at or above 80% of the cap. Class, turns and cap come from the lane record when it carries them, else from the SDK result and the action's turn-cap annotation; a turn-cap stop counts as max-turns whatever class the record gives. Lanes default to the Claude workflows the repo has; `--lane-workflows` pins them. Annotations are read for `--lane-read-limit` runs (default 200): the newest 3 of each lane whatever their conclusion, then failed runs; past it the row says `unread` and the section `PARTIAL`. Then one `KEY:` line per credential role whose API key needs the owner, from the runs' lane-outcome records: `auth` when a run fell back off a refused key or a key leg failed with class auth, naming the key secret to replace; `credit-stop` when a run fell back on a credit stop, naming the role to rotate a spare key into. Each counts the flagged runs against the runs read for that role, and a role with no key problem prints nothing. The fallback reason marks a credit stop, never the class. Then one `SPEND:` line per credential role whose API-key legs were read, from the records' `costUsd`, whose meaning the lane-outcome pointer below names (for how cost is tracked, [Manage costs](https://code.claude.com/docs/en/costs#track-your-costs)): a per-day estimate, the median cost of the role's successful key legs (credit stops left out) times the runs of the lanes those legs ran in, divided by the days read, with the key legs read as `n=` and their sum. Subscription-token legs are left out, and a role whose key legs carry no cost prints `cost not recorded`. Then a model-usage table, the baseline before model routing changes anything: one row per activity and profile over the runs whose annotations carry a lane record, with runs, passes (run conclusion success), median turns, max-turns hits and the models the records name; it shows no sizes. The activity is a pipeline lane's activity (` / `), else the lane; the profile is the record's `profile=`, else `unknown`. A row whose max-turns hits exceed `--max-turns-hits` gets a `RECOMMENDED` line; with the flag unset (the default) the table says no threshold is set | | Cost | GitHub: `gh api` org billing usage (this month) and budgets; the org's repos and admins, and per live repo its direct collaborators and default-branch workflow files. Azure, when `az account show` succeeds: budgets with their forecast and the Cost Management scheduled actions (`az rest` GETs), `az security pricing list`, a month-to-date Cost Management query (`az rest` POST), `az monitor log-analytics workspace list` and one Usage-table query per workspace, `az monitor activity-log list --offset 1d`, `az security secure-scores list`, and `az advisor recommendation list --category Security` and `--category Cost` | One line per check, each `OK`, `ALERT`, `INFO` or `UNREADABLE`. `ALERT`: a product whose net this month is above 0; private-repo Actions minutes or artifact storage at `--usage-alert-percent` (default 50) of `--actions-included-minutes` or `--actions-included-storage-gb` (unset: the line is `INFO` with the amount used); a GitHub budget above 0 or not stopping at its limit; a direct collaborator who is not an org admin or in `--allowed-collaborators`; a workflow on `pull_request_target` or `workflow_run` (the event key bare, quoted, or in a flow sequence or mapping) that references a secret whose name contains `_CLAUDE_`, or passes every secret (`secrets: inherit`, `toJSON(secrets)`, a computed `secrets[...]` index); an Azure budget whose spend reached its amount or whose forecast is above `--forecast-alert` (default: the budget's amount); a `--required-budgets` budget that is missing, or the `--anomaly-alert` alert (default: any cost anomaly alert) missing or disabled, or either ending within `--expiry-days` (default 30); a workspace whose peak billable day over 31 days is at `--log-cap-alert-percent` (default 50) of its daily cap, or that has no cap; a month-to-date actual cost above 0 on any cost row whose service or meter category names Defender or Security Center; a Defender plan, policy assignment or role assignment write in the last day. A repo's Actions usage counts as private unless the org's repo list says it is public; with no repo list read, the line says every repo was counted as private. Budget lines print the latest usage date in the month-to-date cost query (its daily `UsageDate` column) as the data date, beside the read time; with the query unread they say the data date is unread. The Defender plans line lists any plan not on Free as `INFO`, never `ALERT`. The secure score and Advisor counts are `INFO`. The org is `--org`, else the repo's owner, and must be a GitHub owner name; the subscription is `--subscription`, else az's current one. A fixture run reads neither live unless `--org` or `--subscription` is passed as a flag; a settings file never makes it live | The Stuck section depends on two GraphQL facts, the result cap of a search and the fields of a