A test fails in CI. Is your change broken, or is the test flaky? Developers answer that by hitting “re-run” and hoping. Coding agents are worse at it: an agent that sees a red test will try to fix it, and if the test is flaky, it will “fix” code that was never broken, or declare victory when the rerun happens to pass.
A better answer is to measure: run the test many times in clean environments and look at the pattern. This use case does that in parallel Sandscale sandboxes, each started from the same snapshot.
Why several sandboxes, not one loop
Running a test 30 times in one environment finds some flakiness, but misses failures that depend on state the first run left behind, or on things that differ between machines. Spreading attempts across fresh sandboxes from the same snapshot gives you both: repeated attempts within a box, and independent boxes with identical starting files.
The code
Type-checked with tsc --strict against @sandscale/sdk 1.0. It assumes you already have a snapshot of the project with dependencies installed under /workspace; starting sandboxes from a prepared snapshot shows how to make one.
import { Sandscale } from '@sandscale/sdk';
const sandscale = new Sandscale(); // reads SANDSCALE_API_KEY
export interface Attempt {
sandbox: number;
attempt: number;
passed: boolean;
timedOut: boolean;
oomKilled: boolean;
durationMs?: number;
tail: string;
}
// Run one test command `attempts` times in each of `sandboxes` fresh boxes started from the same snapshot.
export async function checkFlaky(snapshotId: string, testCommand: string, sandboxes = 3, attempts = 10) {
const one = async (n: number): Promise<Attempt[]> => {
const sb = await sandscale.sandboxes.create({
snapshotId,
timeoutSeconds: 3600,
labels: { app: 'flaky-check', box: String(n) },
});
const out: Attempt[] = [];
try {
for (let i = 1; i <= attempts; i++) {
const r = await sandscale.sandboxes.run(sb.id, ['bash', '-lc', testCommand], {
region: sb.region,
workdir: '/workspace/app',
timeoutSeconds: 300,
});
out.push({
sandbox: n,
attempt: i,
passed: r.exitCode === 0 && !r.timedOut,
timedOut: r.timedOut,
oomKilled: r.oomKilled,
durationMs: r.durationMs,
tail: (r.stdout + r.stderr).slice(-1_500),
});
}
} finally {
await sandscale.sandboxes.stop(sb.id, { region: sb.region });
}
return out;
};
const all = (await Promise.all(Array.from({ length: sandboxes }, (_, n) => one(n)))).flat();
const failed = all.filter((a) => !a.passed);
const verdict =
failed.length === 0 ? 'stable: passed every time'
: failed.length === all.length ? 'broken: failed every time'
: `flaky: failed ${failed.length} of ${all.length}`;
return { verdict, failed, all };
}
Call it with the single failing test, not the whole suite:
const result = await checkFlaky(snapshotId, 'source /workspace/.venv/bin/activate && pytest -q tests/test_orders.py::test_refund', 3, 10);
console.log(result.verdict);
Reading the result
- “stable: passed every time” across 30 attempts: your failure was likely environmental, or the test depends on something in CI that the sandbox does not have. Look at the CI log, not the code.
- “broken: failed every time”: a real failure. Fix the code or the test.
- “flaky: failed 4 of 30”: the test is nondeterministic. Quarantine it or fix the nondeterminism; do not let an agent “fix” the code under test.
Each failed attempt keeps timedOut, oomKilled and the tail of its output. Patterns tell you a lot: failures that are all timeouts point at a race or a slow dependency; failures only in one sandbox point at something that differs per box, such as time or random seeds; memory kills point at a leak that builds up over attempts.
Design notes
Same snapshot, fresh boxes. Every sandbox starts with identical files and fresh processes, so differences in outcome come from the test, not from leftover state.
No network. The sandboxes are created without a network option, so the SDK’s default, none, applies. A test that only fails without network access was secretly an integration test, which is worth knowing.
Limits per attempt. Each run gets timeoutSeconds: 300; a hung attempt counts as a failure with timedOut: true instead of stalling the whole check.
Labels. Each box carries app: flaky-check and its index, which makes the runs easy to find when you list sandboxes.
Plan limits. Boxes at once are 3 on Free and 30 on Pro and Team, per the plans reference. Keep sandboxes within that.
Giving it to a coding agent
Expose checkFlaky as a tool with a clear description: “Before changing code to fix a failing test, check whether the test is flaky. Returns stable, broken or flaky with counts.” The tool’s verdict is a fact the agent can act on, instead of a guess based on one rerun. Pair it with test results a coding agent can act on for the failure details.
Cost
Attempts use awake vCPU-hours while their sandboxes run: three boxes for the length of ten attempts of one test. On Free that is part of the 100 hours a month; on Pro and Team, 1,500 and 2,000 per member pooled.