@@ -13,10 +13,10 @@ const { parsePositiveCompactNumber } = require("./numeric_limits.cjs");
1313const { getErrorMessage } = require ( "./error_helpers.cjs" ) ;
1414const { createRateLimitAwareGithub } = require ( "./github_rate_limit_logger.cjs" ) ;
1515const { scanDailyAIC } = require ( "./daily_aic_scan.cjs" ) ;
16- const { DAILY_AIC_QUOTA_BLOCKED_ENV , API_READ_OPERATIONS , createAPIBudget, retryNotBefore, safeResponseHeaders, isQuotaError, getAPIReadFailure } = require ( "./daily_aic_api_budget.cjs" ) ;
16+ const { DAILY_AIC_QUOTA_BLOCKED_ENV , API_READ_OPERATIONS , createAPIBudget, retryNotBefore, safeResponseHeaders, isQuotaError, getAPIReadFailure, getErrorStatus } = require ( "./daily_aic_api_budget.cjs" ) ;
1717const { GITHUB_API_QUOTA_EXHAUSTED , getGitHubAPIQuotaExhaustion, formatGitHubAPIQuotaDetails, writeGitHubAPIQuotaSummary } = require ( "./github_api_quota.cjs" ) ;
1818const { getGitHubAPIDiagnostic } = require ( "./github_api_diagnostics.cjs" ) ;
19- const { loadBillableJobs, allBillableJobsNotStarted, allBillableJobsSkipped, sumCoveredComponents } = require ( "./daily_aic_component_coverage.cjs" ) ;
19+ const { loadBillableJobs, allBillableJobsNotStarted, allBillableJobsSkipped, invocationUsageArtifactName , sumCoveredComponents } = require ( "./daily_aic_component_coverage.cjs" ) ;
2020const { readLedgerEntries, readLedgerCoverageStart, WINDOW_MS } = require ( "./daily_aic_repo_memory_ledger.cjs" ) ;
2121
2222const PRIMARY_GUARDRAIL_ARTIFACT_NAMES = [ "usage" ] ;
@@ -241,7 +241,7 @@ function inspectLegacyAgentLog(logText) {
241241 } ;
242242}
243243
244- async function inspectLegacyAgentArtifact ( artifactClient , artifacts , downloadRoot , token , owner , repo , run , components ) {
244+ async function inspectLegacyAgentArtifact ( artifactClient , artifacts , downloadRoot , token , owner , repo , run , components , usageArtifactName ) {
245245 const job = components . get ( "agent" ) ;
246246 const noEvidence = { artifactInspected : false , preHarnessFailure : false , sampleReplay : false } ;
247247 if ( ! job ) return noEvidence ;
@@ -255,7 +255,7 @@ async function inspectLegacyAgentArtifact(artifactClient, artifacts, downloadRoo
255255 }
256256 }
257257
258- const artifact = artifacts . find ( item => item ?. name === "agent" ) ;
258+ const artifact = artifacts . find ( item => item ?. name === usageArtifactName . slice ( 0 , - "usage" . length ) + "agent" ) ;
259259 const createdAt = artifact ?. createdAt ?. getTime ( ) ;
260260 const startedAt = Date . parse ( job . started_at ) ;
261261 const completedAt = Date . parse ( job . completed_at ) ;
@@ -301,26 +301,30 @@ async function inspectLegacyAgentArtifact(artifactClient, artifacts, downloadRoo
301301 * @returns {Promise<number> }
302302 */
303303async function getRunAIC ( artifactClient , runId , token , owner , repo , run , inspection ) {
304- const { components, priorJobs } = run ? await loadBillableJobs ( inspection , owner , repo , run ) : { components : null , priorJobs : null } ;
305- if ( components && allBillableJobsNotStarted ( components ) ) {
304+ const invocations = run ? await loadBillableJobs ( inspection , owner , repo , run ) : null ;
305+ const executed = invocations ?. filter ( ( { components } ) => ! allBillableJobsNotStarted ( components ) ) ;
306+ if ( invocations && executed ?. length === 0 ) {
306307 inspection . accountingReason = "confirmed_zero" ;
307- for ( const [ component , job ] of components ) {
308- core . info (
309- `[daily-workflow-aic] Computed component AIC: ${ JSON . stringify ( {
310- runId,
311- component,
312- jobId : job . id ,
313- runAttempt : job . run_attempt ,
314- conclusion : job . conclusion ,
315- aic : 0 ,
316- reason : job . conclusion === "skipped" ? "job_skipped" : "runner_not_assigned" ,
317- } ) } `
318- ) ;
308+ for ( const { components, label } of invocations ) {
309+ for ( const [ component , job ] of components ) {
310+ core . info (
311+ `[daily-workflow-aic] Computed component AIC: ${ JSON . stringify ( {
312+ runId,
313+ component,
314+ invocation : label ,
315+ jobId : job . id ,
316+ runAttempt : job . run_attempt ,
317+ conclusion : job . conclusion ,
318+ aic : 0 ,
319+ reason : job . conclusion === "skipped" ? "job_skipped" : "runner_not_assigned" ,
320+ } ) } `
321+ ) ;
322+ }
319323 }
320324 logDailyGuardrail ( "Computed run AIC" , {
321325 runId,
322326 aic : 0 ,
323- reason : components . size === 0 ? "no_billable_jobs" : allBillableJobsSkipped ( components ) ? "all_billable_jobs_skipped" : "all_billable_jobs_not_started" ,
327+ reason : invocations . length === 0 ? "no_billable_jobs" : invocations . every ( ( { components } ) => allBillableJobsSkipped ( components ) ) ? "all_billable_jobs_skipped" : "all_billable_jobs_not_started" ,
324328 } ) ;
325329 return 0 ;
326330 }
@@ -346,7 +350,36 @@ async function getRunAIC(artifactClient, runId, token, owner, repo, run, inspect
346350 artifacts : artifactSummaries ,
347351 } ) ;
348352
349- const artifact = artifacts . find ( item => item ?. name && matchesGuardrailArtifactName ( item . name ) ) ;
353+ if ( ! invocations ) {
354+ const artifact = artifacts . find ( item => item ?. name && matchesGuardrailArtifactName ( item . name ) ) ;
355+ return readUsageArtifactAIC ( artifactClient , artifact , artifacts , runId , token , owner , repo , null , null , artifactSummaries ) ;
356+ }
357+
358+ let total = 0 ;
359+ const usedArtifacts = new Set ( ) ;
360+ // An old hash-based artifact has no invocation identity. Only use that
361+ // format with a single recognized invocation, never to fill a missing call.
362+ const legacyArtifact = invocations . length === 1 ? artifacts . find ( item => item ?. name && ! / ^ c a l l - [ 1 - 9 ] \d * - u s a g e $ / . test ( item . name ) && matchesGuardrailArtifactName ( item . name ) ) : null ;
363+ for ( const invocation of executed || [ ] ) {
364+ try {
365+ const name = invocationUsageArtifactName ( invocation ) ;
366+ const artifact = ( name && artifacts . find ( item => item ?. name === name ) ) || ( invocation . prefix ? legacyArtifact : artifacts . find ( item => item ?. name === "usage" ) || legacyArtifact ) ;
367+ if ( artifact ?. id && usedArtifacts . has ( artifact . id ) ) throw new Error ( `Usage artifact ${ artifact . id } is already assigned to another call` ) ;
368+ total += await readUsageArtifactAIC ( artifactClient , artifact , artifacts , runId , token , owner , repo , run , invocation , artifactSummaries ) ;
369+ if ( artifact ?. id ) usedArtifacts . add ( artifact . id ) ;
370+ } catch ( error ) {
371+ if ( getErrorStatus ( error ) || isQuotaError ( error ) || getAPIReadFailure ( error ) || safeResponseHeaders ( error ?. response ?. headers ) [ "retry-after" ] != null ) {
372+ logDailyGuardrail ( "Invocation accounting unresolved" , { runId, invocation : invocation . label , error : getErrorMessage ( error ) } ) ;
373+ throw error ;
374+ }
375+ throw new Error ( `Daily AIC unresolved for ${ invocation . label } : ${ getErrorMessage ( error ) } ` , { cause : error } ) ;
376+ }
377+ }
378+ if ( ! Number . isFinite ( total ) ) throw new Error ( "Daily AIC invocation total is not finite" ) ;
379+ return total ;
380+ }
381+
382+ async function readUsageArtifactAIC ( artifactClient , artifact , artifacts , runId , token , owner , repo , run , invocation , artifactSummaries ) {
350383 if ( ! artifact ) {
351384 if ( run ) {
352385 throw new Error ( `No usage artifact proves AIC for completed run ${ runId } ; check artifact upload and retention` ) ;
@@ -394,9 +427,6 @@ async function getRunAIC(artifactClient, runId, token, owner, repo, run, inspect
394427 } ) ;
395428
396429 const usageJSONLFiles = findJSONLFiles ( download . downloadPath || downloadRoot ) ;
397- if ( run && ! components && usageJSONLFiles . length === 0 ) {
398- throw new Error ( `Usage artifact contains no accounting records for run ${ runId } ` ) ;
399- }
400430 logDailyGuardrail ( "Downloaded guardrail artifact" , {
401431 runId,
402432 artifactId : artifact . id ,
@@ -405,13 +435,15 @@ async function getRunAIC(artifactClient, runId, token, owner, repo, run, inspect
405435 usageJSONLFiles,
406436 } ) ;
407437 const artifactRoot = download . downloadPath || downloadRoot ;
408- const legacyAgentEvidence = components ? await inspectLegacyAgentArtifact ( artifactClient , artifacts , artifactRoot , token , owner , repo , run , components ) : null ;
409- const aic = components ? sumCoveredComponents ( artifactRoot , components , artifact . createdAt . getTime ( ) , artifacts , artifact . name , run . run_attempt , run . id , legacyAgentEvidence , priorJobs ) : sumAICFromUsageJSONLFiles ( usageJSONLFiles ) ;
438+ const legacyAgentEvidence = invocation ? await inspectLegacyAgentArtifact ( artifactClient , artifacts , artifactRoot , token , owner , repo , run , invocation . components , artifact . name ) : null ;
439+ const aic = invocation
440+ ? sumCoveredComponents ( artifactRoot , invocation . components , artifact . createdAt . getTime ( ) , artifacts , artifact . name , run . run_attempt , run . id , legacyAgentEvidence , invocation . priorJobs )
441+ : sumAICFromUsageJSONLFiles ( usageJSONLFiles ) ;
410442 logDailyGuardrail ( "Computed run AIC from artifact" , {
411443 runId,
412444 artifactId : artifact . id ,
413445 aic,
414- reason : components ? "covered_components" : "usage_files" ,
446+ reason : invocation ? "covered_components" : "usage_files" ,
415447 } ) ;
416448 return aic ;
417449 } catch ( error ) {
0 commit comments