feat: lease-based Sprite keepalive with expiry and renewal

Replace forever-on heartbeat billing with a finite lease system:
callers request a window of Sprite time and renew as needed; on
expiry the heartbeat session stops and the Sprite auto-pauses.

- SpriteLeaseStore (server/sprite-lease-store.ts): refcounted
  leases with TTL clamping ([60s, 8h], default 15min) persisted
  to sprite-leases.json
- SpriteHostKeepalive reimplemented on leases; legacy
  keepalive-hosts manifest seeds one final window then clears
- Provider: session.open acquires, session.prompt renews,
  session.close releases
- sprite_* tools and RPCs acquire/renew/release via
  sprites.lease.acquire/renew/release/list
- destroy/archive release leases before deleting Sprites
- Settings: defaultLeaseMinutes (1-480, default 30), schema v2
This commit is contained in:
nandi committed 2026-11-20 14:25:36 -08:00
1 parent 4ae69f54ce
commit ec93b5a215
8 files changed
+591 -98

No files matched your search

+89
View File
@@ -188,6 +188,17 @@ export const statusRpc = defineRpc({
spriteHosts: z.array(SpriteHostSummarySchema),
spriteAgents: z.array(SpriteAgentSummarySchema),
agentSprites: z.record(z.string(), z.string()),
leases: z.array(
z.object({
spriteName: z.string(),
leaseId: z.string(),
holder: z.object({ kind: z.string(), id: z.string() }),
reason: z.string(),
createdAtMs: z.number(),
updatedAtMs: z.number(),
expiresAtMs: z.number(),
}),
),
}),
});
@@ -570,3 +581,81 @@ export const watchWorkersSub = defineSubscription({
workers: z.array(WorkerSummarySchema),
}),
});
// --- Sprite leases (finite keep-alive windows) ---
export const LeaseHolderSchema = z.object({
kind: z.string(),
id: z.string(),
});
export const SpriteLeaseSchema = z.object({
spriteName: z.string(),
leaseId: z.string(),
holder: LeaseHolderSchema,
reason: z.string(),
createdAtMs: z.number(),
updatedAtMs: z.number(),
expiresAtMs: z.number(),
});
export type SpriteLease = z.output<typeof SpriteLeaseSchema>;
export const acquireLeaseRpc = defineRpc({
name: "sprites.lease.acquire",
input: z.object({
spriteName: z.string(),
holder: LeaseHolderSchema,
ttlMs: z.number().int().positive().optional(),
reason: z.string().optional(),
}),
output: z.discriminatedUnion("ok", [
z.object({
ok: z.literal(true),
lease: SpriteLeaseSchema,
}),
z.object({
ok: z.literal(false),
error: z.string(),
}),
]),
});
export const renewLeaseRpc = defineRpc({
name: "sprites.lease.renew",
input: z.object({
leaseId: z.string(),
ttlMs: z.number().int().positive().optional(),
}),
output: z.discriminatedUnion("ok", [
z.object({
ok: z.literal(true),
lease: SpriteLeaseSchema,
}),
z.object({
ok: z.literal(false),
error: z.string(),
}),
]),
});
export const releaseLeaseRpc = defineRpc({
name: "sprites.lease.release",
input: z.object({
leaseId: z.string(),
}),
output: z.object({
ok: z.boolean(),
}),
});
export const listLeasesRpc = defineRpc({
name: "sprites.lease.list",
input: z.object({
spriteName: z.string().optional(),
}),
output: z.object({
leases: z.array(SpriteLeaseSchema),
}),
});
+3 -1
View File
@@ -29,6 +29,7 @@ export const flySpritesSettingsSchema = z.object({
injectAgentTools: z.boolean().default(true),
autoCleanupOnArchive: z.boolean().default(false),
keepSpriteHostsAwake: z.boolean().default(true),
defaultLeaseMinutes: z.number().int().min(1).max(480).default(30),
});
export type FlySpritesSettings = z.output<typeof flySpritesSettingsSchema>;
@@ -36,7 +37,7 @@ export type FlySpritesSettings = z.output<typeof flySpritesSettingsSchema>;
export const flySpritesSettings = defineSettings({
id: "fly-sprites",
scope: "host",
version: 1,
version: 2,
schema: flySpritesSettingsSchema,
});
@@ -68,4 +69,5 @@ export const defaultFlySpritesSettings: FlySpritesSettings = {
injectAgentTools: true,
autoCleanupOnArchive: false,
keepSpriteHostsAwake: true,
defaultLeaseMinutes: 30,
};