( agentApi: AgentListApi, rootSlug: string, nowMs: number = Date.now() )
| 476 | } |
| 477 | |
| 478 | export async function findActiveRootPlanner( |
| 479 | agentApi: AgentListApi, |
| 480 | rootSlug: string, |
| 481 | nowMs: number = Date.now() |
| 482 | ): Promise<ActiveRootPlanner | null> { |
| 483 | const list = await agentApi.list({ runtime: "cloud", limit: 50 }); |
| 484 | // Exact match, not startsWith: a prefix slug would otherwise adopt another |
| 485 | // goal's `<slug>-root` planner (e.g. "auth" adopting "auth-ui-root"). |
| 486 | const rootPlannerName = `${rootSlug}-root`; |
| 487 | for (const item of listItems(list)) { |
| 488 | const name = stringField(item, "name"); |
| 489 | if (!name) continue; |
| 490 | if (name !== rootPlannerName) continue; |
| 491 | const createdAt = timeMs(field(item, "createdAt")); |
| 492 | if (createdAt === null || nowMs - createdAt > MAX_BOOT_MS) continue; |
| 493 | const agentId = |
| 494 | stringField(item, "agentId") ?? stringField(item, "id") ?? null; |
| 495 | if (!agentId) continue; |
| 496 | const embeddedRun = latestRunFromItem(item); |
| 497 | const run = embeddedRun ?? (await latestRunForAgent(agentApi, agentId)); |
| 498 | const status = runStatus(run); |
| 499 | if (!ACTIVE_ROOT_RUN_STATUSES.has(status)) continue; |
| 500 | return { |
| 501 | agentId, |
| 502 | runId: stringField(run, "id") ?? null, |
| 503 | status, |
| 504 | name, |
| 505 | }; |
| 506 | } |
| 507 | return null; |
| 508 | } |
| 509 | |
| 510 | export function inferKickoffRootSlug(goal: string): string { |
| 511 | const firstLine = goal.split("\n")[0]?.trim() ?? ""; |
no test coverage detected