findTokenUsageFile searches for token-usage.jsonl in the run directory
(runDir string)
| 286 | |
| 287 | // findTokenUsageFile searches for token-usage.jsonl in the run directory |
| 288 | func findTokenUsageFile(runDir string) string { |
| 289 | usageArtifactCandidate := filepath.Join(runDir, "usage", "agent", "token_usage.jsonl") |
| 290 | if fileutil.FileExists(usageArtifactCandidate) { |
| 291 | tokenUsageLog.Printf("Found token usage file in usage artifact: %s", usageArtifactCandidate) |
| 292 | return usageArtifactCandidate |
| 293 | } |
| 294 | |
| 295 | // Primary path: sandbox/firewall/logs/api-proxy-logs/token-usage.jsonl |
| 296 | primary := filepath.Join(runDir, "sandbox", "firewall", "logs", tokenUsageJSONLPath) |
| 297 | if fileutil.FileExists(primary) { |
| 298 | tokenUsageLog.Printf("Found token usage file at primary path: %s", primary) |
| 299 | return primary |
| 300 | } |
| 301 | |
| 302 | // Check legacy firewall-audit-logs artifact directory (backward compat for older runs) |
| 303 | entries, err := os.ReadDir(runDir) |
| 304 | if err != nil { |
| 305 | return "" |
| 306 | } |
| 307 | |
| 308 | for _, entry := range entries { |
| 309 | if !entry.IsDir() { |
| 310 | continue |
| 311 | } |
| 312 | name := entry.Name() |
| 313 | if strings.HasPrefix(name, "firewall-audit-logs") || strings.HasPrefix(name, "firewall-logs") { |
| 314 | candidate := filepath.Join(runDir, name, tokenUsageJSONLPath) |
| 315 | if fileutil.FileExists(candidate) { |
| 316 | tokenUsageLog.Printf("Found token usage file in %s: %s", name, candidate) |
| 317 | return candidate |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | // Walk sandbox directory for any token-usage.jsonl |
| 323 | if walkErr := filepath.Walk(runDir, func(path string, info os.FileInfo, err error) error { |
| 324 | if err != nil { |
| 325 | tokenUsageLog.Printf("walk error at %s: %v", path, err) |
| 326 | return nil |
| 327 | } |
| 328 | if info == nil || info.IsDir() { |
| 329 | return nil |
| 330 | } |
| 331 | if info.Name() == "token-usage.jsonl" || info.Name() == "token_usage.jsonl" { |
| 332 | primary = path |
| 333 | return filepath.SkipAll |
| 334 | } |
| 335 | return nil |
| 336 | }); walkErr != nil && !errors.Is(walkErr, filepath.SkipAll) { |
| 337 | fmt.Fprintln(os.Stderr, console.FormatWarningMessage(fmt.Sprintf("filesystem error walking %s: %v", runDir, walkErr))) |
| 338 | } |
| 339 | if primary != filepath.Join(runDir, "sandbox", "firewall", "logs", tokenUsageJSONLPath) { |
| 340 | tokenUsageLog.Printf("Found token usage file via walk: %s", primary) |
| 341 | return primary |
| 342 | } |
| 343 | |
| 344 | tokenUsageLog.Print("No token usage file found") |
| 345 | return "" |