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
This commit is contained in:
nandi committed 2026-11-20 13:41:25 -08:00
1 parent b2c8f4492d
commit 4ae69f54ce
3 files changed
+78

No files matched your search

+51
View File
@@ -10,6 +10,7 @@ import {
type Sprite,
type SpriteCommand,
type SpriteConfig,
type ResourcesPolicy,
type StreamMessage,
} from "@fly/sprites";
import type {
@@ -268,6 +269,43 @@ export class SpritesManager {
return Object.keys(config).length > 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<string> {
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 {