Canonset
← Samples

Verified coding tasks

Tasks that prove their own tests

Each task is what a coding agent would receive: a problem statement and starting code, plus hidden tests and a reference solution. Our sandbox ran every one: the tests fail on the starting code, pass twice on the reference, and have no network access.

  • 12 tasks
  • Python 3.12, TypeScript, JavaScript
  • 33 fail→pass tests
  • Bug fixes, features, two security fixes

Parse compound durations like 1h30m

code-10 · what the agent sees is the problem statement and the starting code

easyTypeScript · Node.js 24 · node:test
Problem statement

duration.ts parses durations for our job scheduler config. Operators write "1h30m" or "2d 4h" and the scheduler refuses to start, because only single units such as "90s" are supported. Extend parseDuration so that it accepts one or more parts, each a whole number followed by ms, s, m, h or d, optionally separated by spaces, and returns the total in milliseconds. Anything else, including an empty string, throws RangeError.

Starting code
duration.ts
const UNITS: Record<string, number> = { ms: 1, s: 1000, m: 60_000, h: 3_600_000, d: 86_400_000 };

export function parseDuration(text: string): number {
  const match = /^(\d+)(ms|s|m|h|d)$/.exec(text.trim());
  if (!match) throw new RangeError(`Invalid duration: "${text}"`);
  return Number(match[1]) * UNITS[match[2]];
}
Reference solution
duration.ts
const UNITS: Record<string, number> = { ms: 1, s: 1000, m: 60_000, h: 3_600_000, d: 86_400_000 };
const PART = /^(\d+)(ms|s|m|h|d)/;

export function parseDuration(text: string): number {
  let rest = text.replace(/\s+/g, "");
  if (!rest) throw new RangeError(`Invalid duration: "${text}"`);
  let total = 0;
  while (rest) {
    const match = PART.exec(rest);
    if (!match) throw new RangeError(`Invalid duration: "${text}"`);
    total += Number(match[1]) * UNITS[match[2]];
    rest = rest.slice(match[0].length);
  }
  return total;
}
Tests (hidden from the agent)
duration.test.ts
import assert from "node:assert/strict";
import { test } from "node:test";
import { parseDuration } from "./duration.ts";

test("single units", () => {
  assert.equal(parseDuration("90s"), 90_000);
  assert.equal(parseDuration("250ms"), 250);
});

test("rejects invalid input", () => {
  for (const bad of ["", "5x", "h1", "1.5h"]) assert.throws(() => parseDuration(bad), RangeError, bad);
});

test("compound durations", () => {
  assert.equal(parseDuration("1h30m"), 5_400_000);
  assert.equal(parseDuration("1m30s"), 90_000);
});

test("spaces between parts", () => {
  assert.equal(parseDuration("2d 4h"), 187_200_000);
});
Notes

"1m30s" checks that "m" followed by digits is minutes, not the start of "ms".

Sandbox run

Recorded by pnpm samples:verify. Our tests fail the build if this stops matching the task.

Checks passed4 tests pass on the reference solution; 2 of them fail on the starting code.
TestStarting codeReferenceSecond run
duration.test.ts::single units
passpasspasspass → pass
duration.test.ts::rejects invalid input
passpasspasspass → pass
duration.test.ts::compound durations
Invalid duration: "1h30m"
failpasspassfail → pass
duration.test.ts::spaces between parts
Invalid duration: "2d 4h"
failpasspassfail → pass

TypeScript · Node.js 24 · node:test · canonset-sandbox-node:1 · 0.9 s · checked 2026-09-29 23:30 UTC

Output: Starting code (exit 1, 0.2 s)
✔ single units (0.98305ms)
✔ rejects invalid input (0.517047ms)
✖ compound durations (0.255693ms)
✖ spaces between parts (0.145072ms)
ℹ tests 4
ℹ suites 0
ℹ pass 2
ℹ fail 2
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 139.774372

✖ failing tests:

test at duration.test.ts:14:1
✖ compound durations (0.255693ms)
  RangeError: Invalid duration: "1h30m"
      at parseDuration (file:///work/0-starter/duration.ts:5:21)
      at TestContext.<anonymous> (file:///work/0-starter/duration.test.ts:15:16)
      at Test.runInAsyncScope (node:async_hooks:227:14)
      at Test.run (node:internal/test_runner/test:1402:25)
      at Test.processPendingSubtests (node:internal/test_runner/test:974:18)
      at Test.postRun (node:internal/test_runner/test:1542:19)
      at Test.run (node:internal/test_runner/test:1467:12)
      at async Test.processPendingSubtests (node:internal/test_runner/test:974:7)

test at duration.test.ts:19:1
✖ spaces between parts (0.145072ms)
  RangeError: Invalid duration: "2d 4h"
      at parseDuration (file:///work/0-starter/duration.ts:5:21)
      at TestContext.<anonymous> (file:///work/0-starter/duration.test.ts:20:16)
      at Test.runInAsyncScope (node:async_hooks:227:14)
      at Test.run (node:internal/test_runner/test:1402:25)
      at Test.processPendingSubtests (node:internal/test_runner/test:974:18)
      at Test.postRun (node:internal/test_runner/test:1542:19)
      at Test.run (node:internal/test_runner/test:1467:12)
      at async Test.processPendingSubtests (node:internal/test_runner/test:974:7)
Output: Reference solution (exit 0, 0.2 s)
✔ single units (1.045857ms)
✔ rejects invalid input (0.447295ms)
✔ compound durations (0.221193ms)
✔ spaces between parts (0.150015ms)
ℹ tests 4
ℹ suites 0
ℹ pass 4
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 160.90424
Output: Reference, second run (exit 0, 0.2 s)
✔ single units (0.976464ms)
✔ rejects invalid input (0.495332ms)
✔ compound durations (0.186332ms)
✔ spaces between parts (0.145616ms)
ℹ tests 4
ℹ suites 0
ℹ pass 4
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 139.243258