Hmbown/CodeWhale · error · ExecError
region must be integer [x,y,width,height] with positive size
Error message
region must be integer [x,y,width,height] with positive size
What it means
screenshot validates the region argument: it must be an array of exactly 4 integers [x,y,width,height] with positive width and height. It throws this ExecError otherwise, since the region is interpolated directly into a System.Drawing.Rectangle in the PowerShell capture script.
Solutions
- Pass exactly [x, y, width, height] as integers with width > 0 and height > 0
- Convert corner-format rects: [x1, y1, x2-x1, y2-y1]
- Round or Math.trunc computed coordinates to integers before calling
- Validate array length and integrality on the caller side
Example fix
// before
await screenshot({ region: { x: 0, y: 0, w: 800, h: 600 } });
// after
await screenshot({ region: [0, 0, 800, 600] }); Defensive patterns
Strategy: validation
Validate before calling
function toRegionInts(rect) {
const [x1, y1, x2, y2] = Array.isArray(rect) && rect.length === 4
? rect
: [rect.x, rect.y, rect.x + rect.w, rect.y + rect.h];
const region = [x1, y1, x2 - x1, y2 - y1].map(Math.round);
if (region.length !== 4 || region.some(n => !Number.isInteger(n)) || region[2] <= 0 || region[3] <= 0) {
throw new TypeError("region must resolve to integer [x,y,width,height] with positive size");
}
return region;
} Type guard
const isValidRegion = (r) => Array.isArray(r) && r.length === 4 && r.every(Number.isInteger) && r[2] > 0 && r[3] > 0;
Try / catch
try {
return await backend.screenshot({ region });
} catch (e) {
if (String(e.message).startsWith("region must be integer")) {
return await backend.screenshot(); // full-screen fallback
}
throw e;
} Prevention
- Always pass [x, y, width, height], not rect objects or corner pairs
- Round computed coordinates to integers
- Reject zero/negative width or height before calling
- Clamp regions to the display bounds
When it happens
Trigger: Passing region as {x,y,w,h} object instead of array; floats like [10.5, 0, 100, 100]; negative or zero width/height; fewer/more than 4 elements; null when a region was expected.
Common situations: Porting from APIs that take rect objects; computed coordinates producing NaN or floats from scaling; an agent emitting [x1,y1,x2,y2] corner format instead of [x,y,w,h].
Understand the failure class
Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.
Related errors
- choose app_ref or region, not both
- display index must be a positive integer
- Invalid native whale body.
- Invalid native whale body.
- region must be [x, y, w, h] in screen points
AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22).
Data as JSON: /api/errors/624936deb35ff01b.
Report an issue: GitHub.
Appendix: source
Thrown at crates/tui/plugins/computer-use/src/backends/win32.mjs:407
$value = ''; $vp = $null;
if (-not $cur.Current.IsPassword -and $cur.TryGetCurrentPattern([System.Windows.Automation.ValuePattern]::Pattern, [ref]$vp)) { $value = [string]$vp.Current.Value }
[void]$els.Add([pscustomobject]@{ index = $els.Count; path = @($path); runtime_id = @($cur.GetRuntimeId()); window_runtime_id = $windowId; role = [string]$cur.Current.ControlType.ProgrammaticName; label = [string]$cur.Current.Name; value = $value.Substring(0, [Math]::Min(120, $value.Length)); enabled = $cur.Current.IsEnabled;
x = [int]$rect.X; y = [int]$rect.Y; w = [int]$rect.Width; h = [int]$rect.Height; actions = $acts });
$kids = $cur.FindAll([System.Windows.Automation.TreeScope]::Children, [System.Windows.Automation.Condition]::TrueCondition);
for ($i = $kids.Count - 1; $i -ge 0; $i--) { $stack.Push(@($kids[$i], ($path + $i))) }
}
break;
}
$result = [pscustomobject]@{ found = $found; name = $appName; truncated = $truncated; elements = @($els | ForEach-Object { [pscustomobject]@{ index = $_.index; path = @($_.path); runtime_id = $_.runtime_id; window_runtime_id = $_.window_runtime_id; role = ($_.role -replace 'ControlType.',''); label = $_.label; value = $_.value; enabled = $_.enabled; position = [pscustomobject]@{ x = $_.x; y = $_.y }; size = [pscustomobject]@{ w = $_.w; h = $_.h }; actions = $_.actions } }) };
Write-Output ($result | ConvertTo-Json -Depth 6 -Compress);`, { timeoutMs: 60_000 });
if (!j.found) throw new ExecError("application window not found in UIA tree — pass app_ref.name as the exact window title from list_windows or list_apps.title");
return j;
},
screenshot: async (args = {}) => {
if (Object.hasOwn(args, "app_ref") || Object.hasOwn(args, "window_id")) throw unsupportedSelector("Windows screenshot does not support app_ref or window_id; omit them for a desktop screenshot");
const { display = activeDisplay, region, path: outPath } = args;
if (display != null && (!Number.isInteger(display) || display < 1)) throw new ExecError("display index must be a positive integer");
if (region != null && (!Array.isArray(region) || region.length !== 4 || !region.every(Number.isInteger) || region[2] <= 0 || region[3] <= 0)) throw new ExecError("region must be integer [x,y,width,height] with positive size");
const dir = recordingsDir();
fs.mkdirSync(dir, { recursive: true });
const file = path.resolve(outPath || path.join(dir, `shot-${crypto.randomBytes(6).toString("hex")}.png`));
const meta = await psJson(`Add-Type -AssemblyName System.Windows.Forms; Add-Type -AssemblyName System.Drawing;
$bounds = [System.Windows.Forms.SystemInformation]::VirtualScreen;
${display == null ? "" : `$screens = [System.Windows.Forms.Screen]::AllScreens; if (${display} -gt $screens.Count) { throw 'display index is out of range' }; $bounds = $screens[${display - 1}].Bounds;`}
${region == null ? "" : `$crop = New-Object System.Drawing.Rectangle(${region.join(",")}); if (-not $bounds.Contains($crop)) { throw 'region is outside capture bounds' }; $bounds = $crop;`}
$bmp = New-Object System.Drawing.Bitmap($bounds.Width, $bounds.Height);
try {
$g = [System.Drawing.Graphics]::FromImage($bmp);
try { $g.CopyFromScreen($bounds.X, $bounds.Y, 0, 0, $bounds.Size); } finally { $g.Dispose(); }
$bmp.Save('${file.replace(/'/g, "''")}', [System.Drawing.Imaging.ImageFormat]::Png);
} finally { $bmp.Dispose(); }
@{ x = $bounds.X; y = $bounds.Y; w = $bounds.Width; h = $bounds.Height } | ConvertTo-Json -Compress;`, { timeoutMs: 30_000 });
if (!fs.existsSync(file) || ![meta.x, meta.y, meta.w, meta.h].every(Number.isFinite) || meta.w <= 0 || meta.h <= 0) throw new ExecError("screenshot did not return a valid raster");
lastRaster = { file, bytes: fs.statSync(file).size, points: { x: meta.x, y: meta.y, w: meta.w, h: meta.h }, pixels: { w: meta.w, h: meta.h }, scale: 1, capturedAt: new Date().toISOString() };
return { ...lastRaster };
},View on GitHub (pinned to 73e0f67d83)