mastra-ai/mastra · error
Worker resourceLimits.${name} must be a positive safe intege
Error message
Worker resourceLimits.${name} must be a positive safe integer. What it means
Each provided worker resource limit value must be a positive JavaScript safe integer (Number.isSafeInteger and > 0). validateOptions rejects values that are floats, zero, negative, NaN, Infinity, or beyond Number.MAX_SAFE_INTEGER, because they become ulimit arguments in the sandbox shell and must map to exact integer kernel limits.
Source
Thrown at deployers/sandbox/src/worker.ts:198
['terminationGraceMs', options.terminationGraceMs],
] as const) {
if (value !== undefined && (!Number.isFinite(value) || value <= 0))
throw new Error(`${name} must be greater than zero.`);
}
const resourceLimits = options.resourceLimits;
if (resourceLimits) {
const knownLimits = new Set(['cpuTimeSeconds', 'addressSpaceBytes', 'fileSizeBytes', 'openFiles']);
for (const name of Object.keys(resourceLimits)) {
if (!knownLimits.has(name)) throw new Error(`Unknown worker resource limit: ${name}.`);
}
for (const [name, value] of [
['cpuTimeSeconds', resourceLimits.cpuTimeSeconds],
['addressSpaceBytes', resourceLimits.addressSpaceBytes],
['fileSizeBytes', resourceLimits.fileSizeBytes],
['openFiles', resourceLimits.openFiles],
] as const) {
if (value !== undefined && (!Number.isSafeInteger(value) || value <= 0)) {
throw new Error(`Worker resourceLimits.${name} must be a positive safe integer.`);
}
}
}
}
function validateRelativePath(value: string, label: string): void {
if (!value || posix.isAbsolute(value) || posix.normalize(value).startsWith('..')) {
throw new Error(`Worker ${label} must stay within the deployed artifact root.`);
}
}
function validateInput(input: SandboxWorkerInput | undefined): void {
if (input?.type === 'file') validateRelativePath(input.path, 'input file path');
}
function normalizeResourceLimits(
limits: SandboxWorkerResourceLimits | undefined,
): NormalizedResourceLimits | undefined {View on GitHub (pinned to 75dd419e61)
Solutions
- Round the value to a positive safe integer: Math.max(1, Math.floor(value)) and verify Number.isSafeInteger.
- Convert human-readable units to whole bytes/seconds yourself (e.g. '512mb' -> 512 * 1024 * 1024).
- Remove the key entirely if you don't want to enforce that limit (undefined values are skipped by validation).
- Parse env/config inputs with strict validation before constructing the options object.
Example fix
// before
const limits = { cpuTimeSeconds: process.env.CPU_SECONDS, fileSizeBytes: 0.5 * 1024 ** 3 };
await deployWorkerToSandbox({ sandbox, command: 'node', resourceLimits: limits });
// after
const cpu = Number(process.env.CPU_SECONDS);
const limits = {
...(Number.isSafeInteger(cpu) && cpu > 0 ? { cpuTimeSeconds: cpu } : {}),
fileSizeBytes: Math.floor(0.5 * 1024 ** 3),
};
await deployWorkerToSandbox({ sandbox, command: 'node', resourceLimits: limits }); Defensive patterns
Strategy: validation
Validate before calling
function assertPositiveSafeIntLimits(limits) {
for (const [name, value] of Object.entries(limits ?? {})) {
if (value === undefined) continue;
if (!Number.isSafeInteger(value) || value <= 0) {
throw new Error(`resourceLimits.${name} must be a positive safe integer, got: ${String(value)}`);
}
}
}
assertPositiveSafeIntLimits(options.resourceLimits); Type guard
function isPositiveSafeInteger(v) {
return Number.isSafeInteger(v) && v > 0;
} Try / catch
try {
await deployWorkerToSandbox(options);
} catch (error) {
if (error instanceof Error && error.message.includes('must be a positive safe integer')) {
const field = error.message.match(/resourceLimits\.(\w+)/)?.[1];
console.error(`Fix resourceLimits.${field}: coerce with Math.floor(Number(x)) and ensure > 0`);
} else throw error;
} Prevention
- Always derive limits with Math.floor/Math.round from parsed values, never pass raw env strings.
- Prefer undefined (omit the key) over 0 or null when a limit is unset.
- Guard byte conversions (MB/GB) with Math.floor to avoid float results.
- Add a schema (zod/valibot) validation for deploy options parsed from config files.
When it happens
Trigger: Passing resourceLimits.cpuTimeSeconds: 1.5, openFiles: 0, addressSpaceBytes: -1024, fileSizeBytes: Number.MAX_SAFE_INTEGER + 1, or a string/NaN value (e.g. parsed from env as '30s' without conversion) in options.resourceLimits of deployWorkerToSandbox.
Common situations: Parsing limits from environment variables or CLI flags without Number() conversion; computing bytes with float math (e.g. 0.5 * 1024 ** 3); passing human strings like '512mb'; copying seconds-based values into byte fields producing tiny/fractional numbers; JSON config with null or 0 defaults.
Understand the failure class
Background: Invalid option value errors: "must be one of", "is not a valid", and "only allows" failures explained — this error's family across 23 libraries.
Related errors
- Unknown worker resource limit: ${name}.
- Invalid environment variable name: "${key}"
- Sandbox provider "${options.sandbox.provider}" does not supp
- terminationGraceMs must be greater than zero.
- Worker command must be a non-empty executable path.
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/8106860f61553a49.
Report an issue: GitHub.