Files
buzz/desktop/src/features/agents/managedAgentReconciliationPlan.ts
Tyler 61cc738ee8 feat(desktop+acp): spawn a harness per (agent, community) pair at GUI startup — warm sockets, lazy LLM pool (#2122)
Signed-off-by: Tyler Longwell <tlongwell@block.xyz>
Signed-off-by: Matt Toohey <contact@matttoohey.com>
Signed-off-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co>
Signed-off-by: Tyler <109685178+tlongwell-block@users.noreply.github.com>
Co-authored-by: npub1jh9wn95s0472h86ahapupaf7m6kx4v9sx2n0atj2hltcfer8k06s5n3pyf <95cae996907d7cab9f5dbf43c0f53edeac6ab0b032a6feae4abfd784e467b3f5@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: Tyler Longwell <tlongwell@block.xyz>
Co-authored-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: npub1mprnacetjua2xx3p5eddmhxyk6wv929ymm5py8kd2xfxurxahspqqlgyta <d8473ee32b973aa31a21a65adddcc4b69cc2a8a4dee8121ecd51926e0cddbc02@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: npub1t2tgm7d8f995uqvmnm8h88sg3wnpp9a5xysjf6dg3tjmgt3ltulqdp8ehr <5a968df9a7494b4e019b9ecf739e088ba61097b4312124e9a88ae5b42e3f5f3e@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: Dawn <c6237ef84fa537c78dcee78efd2d4e59f728859c7f194da42ac51ededfa0be05@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: Matt Toohey <contact@matttoohey.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Co-authored-by: npub1hwqy0rnujtl25dzmlhn8qwux4kr8sjhas3ugltx9j5dm5dwkp2dsqjhytw <bb80478e7c92feaa345bfde6703b86ad86784afd84788facc5951bba35d60a9b@buzz.block.builderlab.xyz>
2026-07-22 13:24:46 -07:00

96 lines
3.7 KiB
TypeScript

import type { ManagedAgentRuntimeStatus } from "@/shared/api/types";
/**
* Pure planning core for incremental, retrying runtime reconciliation.
*
* `useManagedAgentRuntimeReconciliation` reconciles the configured community
* relays against the running harness pairs. Unlike the old one-shot-per-mount
* hook (which only re-ran on a community switch or relaunch) it must:
* - reconcile a newly configured relay as soon as it appears, without
* depending on the add flow also switching communities, and
* - retry a relay whose reconcile failed (relay unreachable at launch, laptop
* waking from sleep) with a capped backoff instead of leaving it un-spawned
* until the next switch or relaunch.
*
* These decisions live here with no React/timer/IPC dependencies so they can be
* unit-tested directly; the hook only owns the refs, timer, and IPC calls.
*/
/** Capped retry backoff: 5s, then 30s, then 2m, then give up. */
const RETRY_BACKOFF_MS = [5_000, 30_000, 120_000] as const;
/**
* Delay before the next retry for a relay that has failed `failureCount` times
* (1-based: 1 = first failure). Returns null once the cap is exhausted, meaning
* stop retrying until the community set changes again.
*/
export function reconcileRetryDelayMs(failureCount: number): number | null {
if (failureCount < 1) return null;
return RETRY_BACKOFF_MS[failureCount - 1] ?? null;
}
/**
* Canonicalize the configured community relays, dropping duplicates and
* unparsable entries. Maps canonical URL -> the raw `relayUrl` to submit to the
* backend (first occurrence wins), so the reconcile call still speaks the
* community's stored spelling while all bookkeeping is keyed canonically.
*/
export function canonicalCommunityRelays(
communities: readonly { relayUrl: string }[],
canonicalize: (url: string) => string | null,
): Map<string, string> {
const byCanonical = new Map<string, string>();
for (const community of communities) {
const canonical = canonicalize(community.relayUrl);
if (canonical === null || byCanonical.has(canonical)) continue;
byCanonical.set(canonical, community.relayUrl);
}
return byCanonical;
}
/**
* Configured relays that still need a reconcile attempt: not yet reconciled
* cleanly and not currently in flight. Returns canonical URLs.
*/
export function pendingReconcileRelays(
canonicalToRequested: ReadonlyMap<string, string>,
reconciled: ReadonlySet<string>,
inFlight: ReadonlySet<string>,
): string[] {
const pending: string[] = [];
for (const canonical of canonicalToRequested.keys()) {
if (reconciled.has(canonical) || inFlight.has(canonical)) continue;
pending.push(canonical);
}
return pending;
}
/**
* Split an attempted batch into relays that reconciled cleanly vs. those that
* still failed. A relay failed if the call threw (`rows === null` marks the
* whole batch failed) or it produced a `failed` lifecycle row. A relay that
* produced no rows (no eligible agents there) counts as reconciled.
*/
export function classifyReconcileResult(
attempted: readonly string[],
rows: readonly ManagedAgentRuntimeStatus[] | null,
canonicalize: (url: string) => string | null,
): { succeeded: string[]; failed: string[] } {
if (rows === null) {
return { succeeded: [], failed: [...attempted] };
}
const failedRelays = new Set<string>();
for (const row of rows) {
if (row.lifecycle !== "failed") continue;
const canonical = canonicalize(row.requestedRelayUrl ?? row.relayUrl);
if (canonical !== null) failedRelays.add(canonical);
}
const succeeded: string[] = [];
const failed: string[] = [];
for (const relay of attempted) {
if (failedRelays.has(relay)) failed.push(relay);
else succeeded.push(relay);
}
return { succeeded, failed };
}