From 4ae69f54ce4f8953df9183299aa2fc5d2d72a200 Mon Sep 17 00:00:00 2001 From: nandithebull Date: Fri, 20 Nov 2026 13:41:25 -0800 Subject: [PATCH] feat: autoscale Sprite memory via Resources Policy Sprites have no size presets and CPU/storage are fixed (8 vCPU / 100 GB), so memory policy (limit + autoscale) is the sizing lever for bursty unknown workloads. ensureSprite now applies idempotently, non-failing: - memoryAutoscale (default true) lets the platform grow memory under pressure instead of thrashing at a fixed cap - memoryLimitMB (0 = Fly default ceiling 8192) is the peak ceiling - Settings UI exposes both controls --- client/settings-screen.tsx | 23 +++++++++++++++++ server/sprites-manager.ts | 51 ++++++++++++++++++++++++++++++++++++++ shared/settings.ts | 4 +++ 3 files changed, 78 insertions(+) diff --git a/client/settings-screen.tsx b/client/settings-screen.tsx index 0e4685b..2b57aa7 100644 --- a/client/settings-screen.tsx +++ b/client/settings-screen.tsx @@ -440,6 +440,18 @@ export function FlySpritesSettingsScreen({ theme, layout, host }: PluginSurfaceP setDraft((prev) => ({ ...prev, defaultCpus: Number.isFinite(parsed) && parsed >= 0 ? parsed : 0 })); }} /> + { + const parsed = Number.parseInt(val.trim() || "0", 10); + setStatusMessage(null); + setDraft((prev) => ({ ...prev, memoryLimitMB: Number.isFinite(parsed) && parsed >= 0 ? parsed : 0 })); + }} + /> @@ -456,6 +468,17 @@ export function FlySpritesSettingsScreen({ theme, layout, host }: PluginSurfaceP void saveDraft(next); }} /> + { + const next = { ...draft, memoryAutoscale }; + setDraft(next); + void saveDraft(next); + }} + /> 0 ? config : undefined; } + /** + * Ensure the Sprite's Resources Policy memory ceiling/autoscale match + * settings. Idempotent: only calls `updateResourcesPolicy` when the + * effective values differ, so repeated `ensureSprite` passes are cheap. + * Sprites have no size presets and no CPU/storage scaling — memory + * policy (limit + autoscale) is the sizing lever. `memoryLimitMB` of 0 + * means the Fly default ceiling (8192 MB, matching sprite defaults). + */ + async applyMemoryResourcesPolicy( + sprite: Sprite, + settings?: FlySpritesSettings, + ): Promise<{ limitMB: number; autoscale: boolean; changed: boolean }> { + const resolved = settings ?? (await this.getSettings()); + const limitMB = + resolved.memoryLimitMB && resolved.memoryLimitMB > 0 + ? resolved.memoryLimitMB + : 8192; + // Missing key (settings stored before this field existed) means default: on. + const autoscale = resolved.memoryAutoscale !== false; + let current: ResourcesPolicy | null = null; + try { + current = await sprite.getResourcesPolicy(); + } catch { + current = null; + } + const already = + current?.memory?.limitMB === limitMB && + (current?.memory?.autoscale ?? false) === autoscale; + if (already) { + return { limitMB, autoscale, changed: false }; + } + await sprite.updateResourcesPolicy({ + memory: { limitMB, autoscale }, + }); + return { limitMB, autoscale, changed: true }; + } + async generateSpriteName(prefixOverride?: string): Promise { const settings = await this.getSettings(); const prefix = prefixOverride ?? settings.spritePrefix ?? "paseo-worker-"; @@ -313,6 +351,19 @@ export class SpritesManager { this.remoteSpritesCache = null; } + try { + const policyResult = await this.applyMemoryResourcesPolicy(sprite, settings); + options.onProgress?.( + `Memory policy ${policyResult.limitMB} MB (autoscale ${ + policyResult.autoscale ? "on" : "off" + }) ${policyResult.changed ? "applied to" : "already on"} ${sprite.name}`, + sprite.name, + ); + } catch (error) { + const msg = error instanceof Error ? error.message : String(error); + console.warn(`[fly-sprites] memory policy skipped for ${sprite.name}: ${msg}`); + } + let antigravityAuthChanged = false; if (settings.syncAntigravityAuth) { try { diff --git a/shared/settings.ts b/shared/settings.ts index e58d489..a0c99bb 100644 --- a/shared/settings.ts +++ b/shared/settings.ts @@ -19,6 +19,8 @@ export const flySpritesSettingsSchema = z.object({ defaultRegion: z.string().default(""), defaultRamMB: z.number().int().nonnegative().default(0), defaultCpus: z.number().int().nonnegative().default(0), + memoryLimitMB: z.number().int().nonnegative().default(0), + memoryAutoscale: z.boolean().default(true), defaultRemoteCwd: z.string().default("/workspace"), defaultProviderMode: z.enum(["shell", "acp"]).default("acp"), acpCommand: z.string().default("claude-code-acp"), @@ -56,6 +58,8 @@ export const defaultFlySpritesSettings: FlySpritesSettings = { defaultRegion: "", defaultRamMB: 0, defaultCpus: 0, + memoryLimitMB: 0, + memoryAutoscale: true, defaultRemoteCwd: "/workspace", defaultProviderMode: "acp", acpCommand: "claude-code-acp",