import { resolveRunnerSession } from '../agent-events/events-runner.ts' const TERMINAL_STATES = new Set(['succeeded', 'failed', 'cancelled', 'timed_out']) /** * The runner-reported session (from `@kody/devin.session.*` events) for a * stored session id, which may still be `pending_`. Read-only and * local: the factory never calls Devin. Null until the first event arrives. */ async function getSessionStatus(input: { devinId: string }) { const s = await resolveRunnerSession(input.devinId) if (!s) return null return { status: TERMINAL_STATES.has(s.state) ? 'finished' : (s.rawStatus ?? s.state), pull_requests: (s.pullRequests ?? []).map((p) => ({ url: p.url, state: p.state ?? '' })), } } import { sessionHoldsIssues, sessionPrs } from './in-flight-rules.ts' import { mapWithConcurrency } from './issues.ts' import { denverDayKey } from './shared.ts' import { closeGroupSession, getOutcome, listLegacySpawnedSweeps, listOpenGroupSessions, } from './storage.ts' export type InFlightSession = { groupKey: string repo: string groupName: string sessionUrl: string | null issueNumbers: Array holds: boolean reason: string prUrls: Array } function parseNumbers(json: string) { try { const value = JSON.parse(json) return Array.isArray(value) ? value.map(Number).filter((n) => Number.isInteger(n) && n > 0) : [] } catch { return [] } } /** * Read every open group session from storage, ask Devin for its status * (read-only), and decide whether it still holds its issues. Sessions that no * longer hold are closed in storage unless `dryRun`. * * @returns per-session view, the held issue numbers per repo, and the total. */ export async function loadInFlight(input: { dryRun: boolean; now?: number }) { const open = await listOpenGroupSessions(input.dryRun) const sessions = await mapWithConcurrency(open, 4, async (row): Promise => { let status: string | null = null let statusError = false let prs: ReturnType = [] if (row.session_id) { try { const current = await getSessionStatus({ devinId: row.session_id }) status = String(current?.status ?? '') prs = sessionPrs(current?.pull_requests) } catch { statusError = true } } const outcomeRecorded = Boolean(await getOutcome(row.group_key, input.dryRun)) const decision = sessionHoldsIssues({ createdAt: row.created_at, sessionId: row.session_id, now: input.now, status, statusError, prs, outcomeRecorded, }) if (!decision.holds && !input.dryRun) await closeGroupSession(row.group_key, decision.reason) return { groupKey: row.group_key, repo: row.repo, groupName: row.group_name, sessionUrl: row.session_url, issueNumbers: parseNumbers(row.issue_numbers_json), holds: decision.holds, reason: decision.reason, prUrls: prs.map((pr) => pr.url), } }) // Cursor agents from the pre-Devin sweep (yesterday or today) that never // reported still hold their issues, so the first Devin runs do not // duplicate their work. They age out after one day. const now = input.now ?? Date.now() const sinceDay = denverDayKey(new Date(now - 86_400_000)) for (const legacy of await listLegacySpawnedSweeps(sinceDay, input.dryRun)) { const rec = legacy as Record const agentId = typeof rec.agent_id === 'string' ? rec.agent_id : null const repo = typeof rec.repo === 'string' ? rec.repo : '' const issueJson = typeof rec.issue_numbers_json === 'string' ? rec.issue_numbers_json : '[]' if (agentId && await getOutcome(agentId, input.dryRun)) continue if (!repo) continue sessions.push({ groupKey: `legacy:${legacy.day}:${repo}`, repo, groupName: 'legacy cursor sweep', sessionUrl: null, issueNumbers: parseNumbers(issueJson), holds: true, reason: 'legacy-cursor-agent', prUrls: [], }) } const heldByRepo = new Map>() let count = 0 for (const session of sessions) { if (!session.holds) continue const held = heldByRepo.get(session.repo.toLowerCase()) ?? new Set() for (const number of session.issueNumbers) { if (!held.has(number)) count += 1 held.add(number) } heldByRepo.set(session.repo.toLowerCase(), held) } return { sessions, heldByRepo, count } }