@@ -26,6 +26,35 @@ async function getArtifactClient() {
2626 return new DefaultArtifactClient ( ) ;
2727}
2828
29+ /**
30+ * @param {string } message
31+ * @param {Record<string, unknown> } [details]
32+ * @returns {string }
33+ */
34+ function formatDailyGuardrailLogMessage ( message , details ) {
35+ if ( ! details || Object . keys ( details ) . length === 0 ) {
36+ return `[daily-workflow-et] ${ message } ` ;
37+ }
38+ let serializedDetails = "" ;
39+ try {
40+ serializedDetails = JSON . stringify ( details ) ;
41+ } catch {
42+ serializedDetails = JSON . stringify ( { error : "failed to serialize log details" } ) ;
43+ }
44+ return `[daily-workflow-et] ${ message } : ${ serializedDetails } ` ;
45+ }
46+
47+ /**
48+ * Emit a consistently prefixed daily workflow ET diagnostic log line.
49+ *
50+ * @param {string } message
51+ * @param {Record<string, unknown> } [details]
52+ * @returns {void }
53+ */
54+ function logDailyGuardrail ( message , details ) {
55+ core . info ( formatDailyGuardrailLogMessage ( message , details ) ) ;
56+ }
57+
2958/**
3059 * @returns {boolean }
3160 */
@@ -66,12 +95,34 @@ async function getRunEffectiveTokens(artifactClient, runId, token, owner, repo)
6695 repositoryName : repo ,
6796 } ,
6897 } ) ;
98+ const artifactSummaries = artifacts . map ( item => ( { id : item ?. id ?? null , name : item ?. name || "" } ) ) ;
99+ logDailyGuardrail ( "Listed workflow artifacts" , {
100+ runId,
101+ artifactCount : artifacts . length ,
102+ artifacts : artifactSummaries ,
103+ } ) ;
69104
70- const artifact = artifacts . find ( item => matchesGuardrailArtifactName ( item . name ) ) ;
105+ const artifact = artifacts . find ( item => item ?. name && matchesGuardrailArtifactName ( item . name ) ) ;
71106 if ( ! artifact ) {
107+ logDailyGuardrail ( "No matching guardrail artifact found" , {
108+ runId,
109+ availableArtifacts : artifactSummaries ,
110+ } ) ;
111+ return 0 ;
112+ }
113+ if ( ! artifact . id ) {
114+ logDailyGuardrail ( "Skipping guardrail artifact without an id" , {
115+ runId,
116+ artifactName : artifact . name ,
117+ } ) ;
72118 return 0 ;
73119 }
74120
121+ logDailyGuardrail ( "Selected guardrail artifact" , {
122+ runId,
123+ artifactId : artifact . id ,
124+ artifactName : artifact . name ,
125+ } ) ;
75126 const downloadRoot = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , `gh-aw-daily-guardrail-${ runId } -` ) ) ;
76127 const download = await artifactClient . downloadArtifact ( artifact . id , {
77128 path : downloadRoot ,
@@ -84,7 +135,20 @@ async function getRunEffectiveTokens(artifactClient, runId, token, owner, repo)
84135 } ) ;
85136
86137 const tokenUsageFile = findTokenUsageFile ( download . downloadPath || downloadRoot ) ;
87- return sumEffectiveTokensFromTokenUsageFile ( tokenUsageFile ) ;
138+ logDailyGuardrail ( "Downloaded guardrail artifact" , {
139+ runId,
140+ artifactId : artifact . id ,
141+ artifactName : artifact . name ,
142+ downloadPath : download . downloadPath || downloadRoot ,
143+ tokenUsageFile,
144+ } ) ;
145+ const effectiveTokens = sumEffectiveTokensFromTokenUsageFile ( tokenUsageFile ) ;
146+ logDailyGuardrail ( "Computed run ET from artifact" , {
147+ runId,
148+ artifactId : artifact . id ,
149+ effectiveTokens,
150+ } ) ;
151+ return effectiveTokens ;
88152}
89153
90154/**
@@ -251,6 +315,17 @@ async function main() {
251315 return ;
252316 }
253317
318+ logDailyGuardrail ( "Resolved current workflow ET guardrail context" , {
319+ owner,
320+ repo,
321+ currentRunId : context . runId ,
322+ workflowId : currentRun . data . workflow_id ,
323+ workflowName,
324+ actorLogin,
325+ threshold,
326+ rateLimitRemaining : rateLimit . remaining ,
327+ rateLimitLimit : rateLimit . limit ,
328+ } ) ;
254329 const maxInspectableRuns = computeMaxInspectableRuns ( rateLimit . remaining ) ;
255330 if ( maxInspectableRuns <= 0 ) {
256331 core . warning ( `Skipping daily workflow ET guardrail because the GitHub API rate limit is too low (${ rateLimit . remaining } remaining, reserve ${ RATE_LIMIT_RESERVE } ).` ) ;
@@ -265,6 +340,13 @@ async function main() {
265340 let page = 1 ;
266341 let truncatedByRateLimit = false ;
267342 while ( page <= MAX_WORKFLOW_RUN_PAGES ) {
343+ logDailyGuardrail ( "Querying completed workflow runs" , {
344+ workflowId : currentRun . data . workflow_id ,
345+ actorLogin,
346+ page,
347+ perPage : 100 ,
348+ cutoff : new Date ( cutoffMs ) . toISOString ( ) ,
349+ } ) ;
268350 const response = await githubClient . rest . actions . listWorkflowRuns ( {
269351 owner,
270352 repo,
@@ -275,6 +357,11 @@ async function main() {
275357 page,
276358 } ) ;
277359 runs = response . data . workflow_runs || [ ] ;
360+ logDailyGuardrail ( "Received workflow runs page" , {
361+ page,
362+ runCount : runs . length ,
363+ runIds : runs . map ( run => run ?. id ) . filter ( Boolean ) ,
364+ } ) ;
278365 if ( runs . length === 0 ) {
279366 break ;
280367 }
@@ -297,6 +384,12 @@ async function main() {
297384 }
298385 page += 1 ;
299386 }
387+ logDailyGuardrail ( "Prepared candidate workflow runs for artifact inspection" , {
388+ candidateRunsCount : candidateRuns . length ,
389+ candidateRunIds : candidateRuns . map ( run => run . id ) ,
390+ maxInspectableRuns,
391+ truncatedByRateLimit,
392+ } ) ;
300393
301394 const artifactClient = await getArtifactClient ( ) ;
302395 let totalEffectiveTokens = 0 ;
@@ -310,6 +403,11 @@ async function main() {
310403 try {
311404 const runEffectiveTokens = await getRunEffectiveTokens ( artifactClient , run . id , token , owner , repo ) ;
312405 if ( runEffectiveTokens <= 0 ) {
406+ logDailyGuardrail ( "Skipping run without ET usage artifact data" , {
407+ runId : run . id ,
408+ currentEffectiveTokens : totalEffectiveTokens ,
409+ threshold,
410+ } ) ;
313411 continue ;
314412 }
315413 totalEffectiveTokens += runEffectiveTokens ;
@@ -320,6 +418,13 @@ async function main() {
320418 conclusion : run . conclusion || "" ,
321419 effective_tokens : runEffectiveTokens ,
322420 } ) ;
421+ logDailyGuardrail ( "Updated current ET state" , {
422+ runId : run . id ,
423+ runEffectiveTokens,
424+ currentEffectiveTokens : totalEffectiveTokens ,
425+ threshold,
426+ countedRunIds : countedRuns . map ( item => item . id ) ,
427+ } ) ;
323428 } catch ( error ) {
324429 core . warning ( `Failed to inspect token usage for run ${ run . id } : ${ getErrorMessage ( error ) } ` ) ;
325430 }
@@ -334,6 +439,14 @@ async function main() {
334439 inspectedRunsCount : countedRuns . length ,
335440 truncatedByRateLimit,
336441 } ;
442+ logDailyGuardrail ( "Completed ET inspection window" , {
443+ candidateRunsCount : summaryMeta . candidateRunsCount ,
444+ inspectedRunsCount : summaryMeta . inspectedRunsCount ,
445+ countedRunIds : countedRuns . map ( run => run . id ) ,
446+ currentEffectiveTokens : totalEffectiveTokens ,
447+ threshold,
448+ exceeded : totalEffectiveTokens > threshold ,
449+ } ) ;
337450
338451 if ( totalEffectiveTokens <= threshold ) {
339452 await appendDailyEffectiveWorkflowSummary ( workflowName , actorLogin , threshold , countedRuns , rateLimit , summaryMeta ) ;
@@ -355,4 +468,5 @@ module.exports = {
355468 calculateDailyEffectiveWorkflowStats,
356469 computeMaxInspectableRuns,
357470 renderDailyEffectiveWorkflowSummary,
471+ formatDailyGuardrailLogMessage,
358472} ;
0 commit comments