Skip to content

Prevent skipped threat detection from reporting inherited agent tokens - #71933

Merged
pelikhan merged 3 commits into
mainfrom
copilot/fix-threat-detection-reports
Oct 25, 2026
Merged

pelikhan merged 3 commits into
mainfrom
copilot/fix-threat-detection-reports

Conversation

Copilot AI commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Skipped threat scans retained the downloaded agent’s usage counters and reported them on detection conclusion spans, double-counting tokens.

  • Cleanup: Move agent_usage.json removal from gated preparation into an always() step immediately after artifact download.
  • Attribution: Suppress gen_ai.usage.* for skipped detection, including counters recovered from logs; preserve successful-scan reporting.
  • Regression coverage: Assert cleanup ordering and guard independence, plus skipped/successful attribution for file and log counters.
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix skipped threat detection reports token usage Oct 25, 2026
Copilot AI requested a review from pelikhan October 25, 2026 18:24
@pelikhan
pelikhan marked this pull request as ready for review October 25, 2026 18:29
Copilot AI max review requested due to automatic review settings October 25, 2026 18:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The default detector's skipped-scan path can still report inherited agent tokens.

2 open issues
What changed in this PR

This PR targets double-counted agent tokens on skipped threat-detection spans by updating workflow generation and setup telemetry.

Changes:

  • Move inherited usage-file cleanup into an unconditional post-download step.
  • Suppress token attributes when detection explicitly concludes as skipped.
  • Add cleanup-ordering and token-attribution regression coverage.
File Description
pkg/​workflow/​threat_detection_job.go Adds unconditional cleanup after agent artifact download.
pkg/​workflow/​threat_detection_job_test.go Checks cleanup ordering and independence from the detection guard.
actions/​setup/​sh/​prepare_threat_detection_files.sh Removes cleanup from gated file preparation.
actions/​setup/​sh/​prepare_threat_detection_files_test.sh Removes obsolete cleanup assertions.
actions/​setup/​js/​send_otlp_span.test.cjs Tests skipped and successful attribution using file and log counters.
actions/​setup/​js/​send_otlp_span.cjs Suppresses token counters for explicitly skipped detection.

Review effort: Max

Comment on lines +59 to +61
" - name: Clear agent token usage\n",
" if: always()\n",
" run: rm -f /tmp/gh-aw/agent_usage.json\n",

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated smoke-copilot.golden with the generated cleanup step and regenerated workflow locks. Commit 1b62477.

Comment thread actions/setup/js/send_otlp_span.cjs Outdated
const agentUsageNormalized = normalizeRuntimeTokenUsage(agentUsageRaw);
const agentUsage = agentUsageNormalized || runtimeMetrics.tokenUsage || {};
// A skipped scan has no token usage, even if downloaded agent artifacts contain counters.
const agentUsage = jobName === "detection" && detectionConclusion === "skipped" ? {} : agentUsageNormalized || runtimeMetrics.tokenUsage || {};

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Detection spans now use only threat-detection/detection_usage.json, with no inherited agent file/log fallback even when the conclusion is unset. Tests cover skipped and successful scans with distinct counters. Commit 1b62477.

@pelikhan

Copy link
Copy Markdown
Collaborator

@copilot Fix the code for all comments in this review thread.

When a review comment includes a suggested change, apply the suggestion exactly.

Do not make changes beyond what is described in the linked review thread.

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
@pelikhan
pelikhan merged commit a81e497 into main Oct 25, 2026
47 checks passed
@pelikhan
pelikhan deleted the copilot/fix-threat-detection-reports branch October 25, 2026 20:52
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This pull request is included in a new release.

Release: v0.93.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants