#!/usr/bin/env node import { readdir } from 'node:fs/promises'; import { join } from 'node:path'; import { execFile as execFileCb } from 'node:child_process'; import { promisify } from 'node:util'; const VALID_CADENCES = new Set(['daily', 'weekly']); const DAY_MS = 24 * 60 * 60 * 1000; function parseArgs(argv) { const args = { cadence: null }; for (let i = 0; i < argv.length; i += 1) { const value = argv[i]; if (value === '--cadence' || value === '-c') { args.cadence = argv[i + 1] ?? null; i += 1; continue; } if (value.startsWith('--cadence=')) { args.cadence = value.split('=')[1] ?? null; } } return args; } function isClaimCandidate(pr, cadence) { const headRef = String(pr?.head?.ref ?? '').toLowerCase(); const title = String(pr?.title ?? '').toLowerCase(); const branchHints = [ `agents/${cadence}/`, `${cadence}/`, `agent/${cadence}/`, ]; if (branchHints.some((hint) => headRef.includes(hint))) { return true; } if (title.includes(cadence) && title.includes('-agent')) { return true; } return false; } function deriveAgentFromPr(pr, cadence) { const headRef = String(pr?.head?.ref ?? ''); const title = String(pr?.title ?? ''); const branchMatcher = new RegExp(`^agents/${cadence}/(?[^/]+)/`); const branchMatch = headRef.match(branchMatcher); if (branchMatch?.groups?.agent) { return { agent: branchMatch.groups.agent, source: 'branch' }; } const legacyBranchMatcher = new RegExp(`^(?:agent/)?${cadence}/(?[^/]+)$`); const legacyMatch = headRef.match(legacyBranchMatcher); if (legacyMatch?.groups?.tail) { const tail = legacyMatch.groups.tail; const agentMatch = tail.match(/^(?(?:[A-Za-z0-9-]+-agent|docs-code-investigator))(?:[-_/].*)?$/); if (agentMatch?.groups?.agent) { return { agent: agentMatch.groups.agent, source: 'branch-legacy' }; } } const titleMatch = title.match(/(?[A-Za-z0-9-]+-agent|docs-code-investigator)/); if (titleMatch?.groups?.agent) { return { agent: titleMatch.groups.agent, source: 'title' }; } return { agent: null, source: 'none' }; } function parseTaskLogFilename(fileName) { const match = fileName.match( /^(?\d{4}-\d{2}-\d{2})_(?