UNPKG

naisys

Version:

NAISYS - Autonomous AI agent runner with built-in context management and cost tracking

61 lines 2.15 kB
/** * Host-level cooperative claim on the shared desktop — one display, multiple * agents. Auto-expires after STALE_AFTER_MS of inactivity so a crashed agent * can't lock the desktop forever if cleanup didn't run. */ const STALE_AFTER_MS = 10 * 60 * 1000; export function createDesktopClaimService() { let claim = null; function isStale(c, now) { return now.getTime() - c.lastUsedAt.getTime() > STALE_AFTER_MS; } return { /** Succeeds when free, held by this agent, or held by a stale agent. */ tryClaim(agentUsername) { const now = new Date(); if (claim && claim.agentUsername !== agentUsername && !isStale(claim, now)) { return { ok: false, holderUsername: claim.agentUsername, lastUsedAt: claim.lastUsedAt, }; } let took; if (!claim) { took = "fresh"; claim = { agentUsername, claimedAt: now, lastUsedAt: now }; } else if (claim.agentUsername === agentUsername) { took = "refreshed"; claim.lastUsedAt = now; } else { took = "stolen-stale"; claim = { agentUsername, claimedAt: now, lastUsedAt: now }; } return { ok: true, took }; }, // Scoped to the agent so a late cleanup from a stale-then-stolen-from // holder can't clear the new owner's claim. release(agentUsername) { if (claim && claim.agentUsername === agentUsername) { claim = null; return true; } return false; }, getStatus() { if (!claim) return null; return { agentUsername: claim.agentUsername, claimedAt: claim.claimedAt, lastUsedAt: claim.lastUsedAt, isStale: isStale(claim, new Date()), }; }, }; } //# sourceMappingURL=desktopClaimService.js.map