${paste.content}diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index cb0c531fd..a2f3ae61f 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -20,7 +20,7 @@ jobs:
test:
name: Test (${{ matrix.os }}, Node ${{ matrix.node }}, ${{ matrix.pm }})
runs-on: ${{ matrix.os }}
- timeout-minutes: 20
+ timeout-minutes: 30
strategy:
fail-fast: false
diff --git a/.github/workflows/maintenance.yml b/.github/workflows/maintenance.yml
index 0a0db2f7f..ca724b328 100644
--- a/.github/workflows/maintenance.yml
+++ b/.github/workflows/maintenance.yml
@@ -48,7 +48,7 @@ jobs:
name: Stale Issues/PRs
runs-on: ubuntu-latest
steps:
- - uses: actions/stale@1e223db275d687790206a7acac4d1a11bd6fe629 # v10.4.0
+ - uses: actions/stale@4391f3da665fdf50b6810c1a66712fb9ba21aa93 # v11.0.0
with:
stale-issue-message: 'This issue is stale due to inactivity.'
stale-pr-message: 'This PR is stale due to inactivity.'
diff --git a/docker/context-profiles/Dockerfile b/docker/context-profiles/Dockerfile
new file mode 100644
index 000000000..f93da49bb
--- /dev/null
+++ b/docker/context-profiles/Dockerfile
@@ -0,0 +1,19 @@
+ARG NODE_IMAGE=node:22-bookworm-slim
+FROM ${NODE_IMAGE}
+ARG CODEX_VERSION=0.154.0
+WORKDIR /consumer
+COPY package.tgz /tmp/ecc-context-package.tgz
+RUN npm install --ignore-scripts --omit=dev --no-audit --no-fund --fetch-timeout=30000 --fetch-retries=1 /tmp/ecc-context-package.tgz \
+ && task_arch=$(node -p process.arch) \
+ && npm install --global --ignore-scripts --no-audit --no-fund --fetch-timeout=30000 --fetch-retries=1 \
+ @openai/codex@${CODEX_VERSION} "@openai/codex-linux-${task_arch}@npm:@openai/codex@${CODEX_VERSION}-linux-${task_arch}" \
+ && codex --version
+COPY native-probe.js /consumer/node_modules/ecc-universal/docker/context-profiles/native-probe.js
+COPY native-switch-probe.js /consumer/node_modules/ecc-universal/docker/context-profiles/native-switch-probe.js
+COPY packed-smoke.js /consumer/node_modules/ecc-universal/docker/context-profiles/packed-smoke.js
+COPY context-carrier-fixture.js /consumer/node_modules/ecc-universal/tests/lib/helpers/context-carrier-fixture.js
+COPY expected-carriers.json /tmp/ecc-expected-carriers.json
+ENV ECC_EXPECTED_CARRIERS=/tmp/ecc-expected-carriers.json
+ENV PATH="/consumer/node_modules/.bin:${PATH}"
+USER node
+CMD ["node", "/consumer/node_modules/ecc-universal/docker/context-profiles/packed-smoke.js"]
diff --git a/docker/context-profiles/README.md b/docker/context-profiles/README.md
new file mode 100644
index 000000000..ad3f1ee61
--- /dev/null
+++ b/docker/context-profiles/README.md
@@ -0,0 +1,69 @@
+# Context profile native and fresh install checks
+
+These opt-in probes exercise real native discovery without creating a model
+thread or copying credentials. They are separate from the default unit suite.
+
+```sh
+node docker/context-profiles/native-probe.js
+node docker/context-profiles/native-probe.js --claude
+node docker/context-profiles/native-switch-probe.js
+node docker/context-profiles/run-podman.js
+```
+
+The first command uses the locally installed Codex executable, a new private
+temporary home for each case, a local marketplace, and the native plugin cache.
+It starts a new app-server process and calls only `initialize` and `skills/list`.
+Lean, Lean with Angular's bundled resources, and Full excluding Python patterns
+must expose exactly their selected plugin skill names. Provider-owned system
+skills are reported separately. Every installed resource is checked against its
+source digest after removing the local marketplace's carrier source.
+
+The Claude command uses the locally installed Claude executable, a private
+temporary home, empty setting sources, `plugin validate`, and `plugin details`
+with an inline plugin directory. It checks exact Lean/Full-with-exclusion skill
+inventories and zero agent, hook, MCP, and LSP components. Reported token costs
+are the provider's projections, not measured usage. Manifest attribution and
+version warnings remain visible.
+
+The switch probe uses the product's managed store and isolated native adapter for
+Full, Lean, and rollback to Full. Preparation creates a separate provider home
+and registers the selected carrier, then opens a fresh app-server to verify
+discovery. Rollback first restores managed authority, then re-verifies the prior
+native home and selects it. The Full Python exclusion and unrelated bytes in the
+prior home must survive every transition. Each native pointer binds its managed
+store revision, carrier digest, exact provider version, and native executable
+SHA-256. Read-only status rechecks receipts, native configuration, cached resource
+bytes, and the pinned executable. Existing sessions and host registration remain
+unchanged.
+
+The Podman runner runs the normal `npm pack` lifecycle, reports its archive
+SHA-256, and builds an isolated consumer from that archive. It installs runtime
+dependencies and pinned Codex 0.154.0 during the image build. The final container
+runs as the image's unprivileged `node` user, with networking disabled, all Linux
+capabilities dropped, no added host mounts, and no copied credentials. It checks
+all ten target/profile combinations through the packed public CLI and independent
+structural oracle, including exact carrier equality with the source checkout.
+It also checks the packed CLI's Full/Lean/rollback lifecycle, idempotency, stale
+revision rejection, Auto context loading, Suggest/Manual/dry-run boundaries,
+pinned receipt reuse, and no-workflow reset. It then repeats native Codex discovery
+and product native preparation/rollback. The packed CLI also prepares a native
+generation and verifies an isolated launch dry-run with no provider on PATH.
+Test helpers are
+copied separately into the image; they are not part of the published package.
+
+An existing compatible Node image can be selected with
+`ECC_CONTEXT_NODE_IMAGE= ' + body + ' ' + escapeHtml(body) + '${paste.content}`; `expiresAt` is an ISO
+ timestamp. Default TTL is 7 days; `ttlSeconds` must be an integer between
+ 1 and 2592000 (30 days).
+ - Missing/invalid `url` or out-of-range `ttlSeconds` → `400`.
+- `GET /` — `302` with `Location` set to the original URL.
+ Unknown code → `404`. Expired link → `410`.
+- `DELETE /links/` — `204`. Unknown code → `404`.
+
+All error responses follow the envelope in `CONTRIBUTING.md`.
diff --git a/docker/context-profiles/complex-eval/cases3/chained-tickets/files/CONTRIBUTING.md b/docker/context-profiles/complex-eval/cases3/chained-tickets/files/CONTRIBUTING.md
new file mode 100644
index 000000000..7c45e4af2
--- /dev/null
+++ b/docker/context-profiles/complex-eval/cases3/chained-tickets/files/CONTRIBUTING.md
@@ -0,0 +1,13 @@
+# Engineering conventions
+
+These conventions apply to every ticket, every route, every change:
+
+- **Errors**: every error response is JSON with the envelope
+ `{ "error": { "code": "`; `expiresAt` is an ISO
+ timestamp. Default TTL is 7 days; `ttlSeconds` must be an integer between
+ 1 and 2592000 (30 days).
+ - Missing/invalid `url` or out-of-range `ttlSeconds` → `400`.
+- `GET /` — `302` with `Location` set to the original URL.
+ Unknown code → `404`. Expired link → `410`.
+- `DELETE /links/` — `204`. Unknown code → `404`.
+
+All error responses follow the envelope in `CONTRIBUTING.md`.
diff --git a/docker/context-profiles/complex-eval/cases4/chained-tickets/files/CONTRIBUTING.md b/docker/context-profiles/complex-eval/cases4/chained-tickets/files/CONTRIBUTING.md
new file mode 100644
index 000000000..7c45e4af2
--- /dev/null
+++ b/docker/context-profiles/complex-eval/cases4/chained-tickets/files/CONTRIBUTING.md
@@ -0,0 +1,13 @@
+# Engineering conventions
+
+These conventions apply to every ticket, every route, every change:
+
+- **Errors**: every error response is JSON with the envelope
+ `{ "error": { "code": "${escapeHtml(paste.content)}/stats` returns `{ "code", "hits", "expiresAt" }` —
+ `hits` counts redirects.
+- The API is rate limited per client and validates URLs (http/https only).
+- Run the tests with `npm test`.
diff --git a/docker/context-profiles/complex-eval/reference3/chained-tickets/src/app.js b/docker/context-profiles/complex-eval/reference3/chained-tickets/src/app.js
new file mode 100644
index 000000000..c802a64fd
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference3/chained-tickets/src/app.js
@@ -0,0 +1,15 @@
+'use strict';
+const http = require('node:http');
+const path = require('node:path');
+const { createStore } = require('./store');
+const { createService } = require('./service');
+const { createRouter } = require('./routes');
+
+function createApp() {
+ const file = process.env.DATA_FILE || path.join(process.cwd(), 'data', 'links.json');
+ const store = createStore(file);
+ const service = createService(store);
+ return http.createServer(createRouter(service));
+}
+
+module.exports = { createApp };
diff --git a/docker/context-profiles/complex-eval/reference3/chained-tickets/src/index.js b/docker/context-profiles/complex-eval/reference3/chained-tickets/src/index.js
new file mode 100644
index 000000000..d37872b76
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference3/chained-tickets/src/index.js
@@ -0,0 +1,7 @@
+'use strict';
+const { createApp } = require('./app');
+
+const port = Number(process.env.PORT || process.argv[2] || 8080);
+createApp().listen(port, () => {
+ console.log(`shortlink listening on ${port}`);
+});
diff --git a/docker/context-profiles/complex-eval/reference3/chained-tickets/src/routes.js b/docker/context-profiles/complex-eval/reference3/chained-tickets/src/routes.js
new file mode 100644
index 000000000..7344146c6
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference3/chained-tickets/src/routes.js
@@ -0,0 +1,86 @@
+'use strict';
+const { HttpError } = require('./service');
+
+const MAX_BODY_BYTES = 64 * 1024;
+
+function sendJson(res, status, value) {
+ res.writeHead(status, { 'content-type': 'application/json' });
+ res.end(JSON.stringify(value));
+}
+
+function sendError(res, error) {
+ const known = error instanceof HttpError;
+ sendJson(res, known ? error.status : 500, {
+ error: { code: known ? error.code : 'INTERNAL', message: known ? error.message : 'internal error' },
+ });
+}
+
+function readBody(req) {
+ return new Promise((resolve, reject) => {
+ let body = '';
+ let bytes = 0;
+ let settled = false;
+ req.on('data', chunk => {
+ if (settled) return;
+ bytes += chunk.length;
+ if (bytes > MAX_BODY_BYTES) {
+ settled = true;
+ reject(new HttpError(413, 'PAYLOAD_TOO_LARGE', 'request body too large'));
+ // Drain rather than destroy: the socket must live long enough to send the 413.
+ req.resume();
+ return;
+ }
+ body += chunk;
+ });
+ req.on('end', () => {
+ if (settled) return;
+ settled = true;
+ if (!body) { resolve({}); return; }
+ try { resolve(JSON.parse(body)); } catch { reject(new HttpError(400, 'INVALID_JSON', 'body must be valid JSON')); }
+ });
+ req.on('error', reject);
+ });
+}
+
+function createRouter(service) {
+ return async (req, res) => {
+ try {
+ const url = new URL(req.url, 'http://localhost');
+
+ if (req.method === 'POST' && url.pathname === '/links') {
+ service.assertRateLimit(req.socket.remoteAddress || 'unknown');
+ const link = service.createLink(await readBody(req));
+ sendJson(res, 201, { code: link.code, shortUrl: `/${link.code}`, expiresAt: link.expiresAt });
+ return;
+ }
+
+ const statsMatch = /^\/links\/([A-Za-z0-9]{1,20})\/stats$/.exec(url.pathname);
+ if (req.method === 'GET' && statsMatch) {
+ sendJson(res, 200, service.stats(statsMatch[1]));
+ return;
+ }
+
+ const linkMatch = /^\/links\/([A-Za-z0-9]{1,20})$/.exec(url.pathname);
+ if (req.method === 'DELETE' && linkMatch) {
+ service.deleteLink(linkMatch[1]);
+ res.writeHead(204);
+ res.end();
+ return;
+ }
+
+ const redirectMatch = /^\/([A-Za-z0-9]{1,20})$/.exec(url.pathname);
+ if (req.method === 'GET' && redirectMatch) {
+ const link = service.resolveLink(redirectMatch[1]);
+ res.writeHead(302, { location: link.url });
+ res.end();
+ return;
+ }
+
+ throw new HttpError(404, 'NOT_FOUND', 'not found');
+ } catch (error) {
+ sendError(res, error);
+ }
+ };
+}
+
+module.exports = { createRouter };
diff --git a/docker/context-profiles/complex-eval/reference3/chained-tickets/src/service.js b/docker/context-profiles/complex-eval/reference3/chained-tickets/src/service.js
new file mode 100644
index 000000000..f28167d8a
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference3/chained-tickets/src/service.js
@@ -0,0 +1,82 @@
+'use strict';
+const crypto = require('node:crypto');
+
+const MAX_URL_LENGTH = 2048;
+const DEFAULT_TTL_SECONDS = 604800;
+const MAX_TTL_SECONDS = 2592000;
+const RATE_LIMIT_WINDOW_MS = 60000;
+const RATE_LIMIT_MAX = 20;
+
+class HttpError extends Error {
+ constructor(status, code, message) {
+ super(message);
+ this.status = status;
+ this.code = code;
+ }
+}
+
+function validateUrl(url) {
+ if (typeof url !== 'string' || !url) throw new HttpError(400, 'INVALID_URL', 'url is required');
+ if (url.length > MAX_URL_LENGTH) throw new HttpError(400, 'INVALID_URL', 'url exceeds 2048 characters');
+ let parsed;
+ try { parsed = new URL(url); } catch { throw new HttpError(400, 'INVALID_URL', 'url must be a valid absolute URL'); }
+ if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
+ throw new HttpError(400, 'INVALID_URL', 'only http and https URLs are allowed');
+ }
+ return url;
+}
+
+function validateTtl(ttlSeconds) {
+ if (ttlSeconds === undefined || ttlSeconds === null) return DEFAULT_TTL_SECONDS;
+ if (!Number.isInteger(ttlSeconds) || ttlSeconds < 1 || ttlSeconds > MAX_TTL_SECONDS) {
+ throw new HttpError(400, 'INVALID_TTL', 'ttlSeconds must be an integer between 1 and 2592000');
+ }
+ return ttlSeconds;
+}
+
+function createService(store) {
+ const buckets = new Map();
+
+ function assertRateLimit(key) {
+ const now = Date.now();
+ const windowHits = (buckets.get(key) || []).filter(at => now - at < RATE_LIMIT_WINDOW_MS);
+ if (windowHits.length >= RATE_LIMIT_MAX) throw new HttpError(429, 'RATE_LIMITED', 'too many requests, slow down');
+ windowHits.push(now);
+ buckets.set(key, windowHits);
+ }
+
+ function freshCode() {
+ let code = crypto.randomBytes(4).toString('hex');
+ while (store.get(code)) code = crypto.randomBytes(4).toString('hex');
+ return code;
+ }
+
+ return {
+ assertRateLimit,
+ createLink({ url, ttlSeconds } = {}) {
+ const validUrl = validateUrl(url);
+ const ttl = validateTtl(ttlSeconds);
+ const link = { code: freshCode(), url: validUrl,
+ expiresAt: new Date(Date.now() + ttl * 1000).toISOString(), hits: 0 };
+ store.set(link.code, link);
+ return link;
+ },
+ resolveLink(code) {
+ const link = store.get(code);
+ if (!link) throw new HttpError(404, 'NOT_FOUND', 'no link with that code');
+ if (Date.parse(link.expiresAt) <= Date.now()) throw new HttpError(410, 'GONE', 'link has expired');
+ store.incrementHits(code);
+ return link;
+ },
+ deleteLink(code) {
+ if (!store.delete(code)) throw new HttpError(404, 'NOT_FOUND', 'no link with that code');
+ },
+ stats(code) {
+ const link = store.get(code);
+ if (!link) throw new HttpError(404, 'NOT_FOUND', 'no link with that code');
+ return { code, hits: link.hits || 0, expiresAt: link.expiresAt };
+ },
+ };
+}
+
+module.exports = { createService, HttpError };
diff --git a/docker/context-profiles/complex-eval/reference3/chained-tickets/src/store.js b/docker/context-profiles/complex-eval/reference3/chained-tickets/src/store.js
new file mode 100644
index 000000000..7d5aa091b
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference3/chained-tickets/src/store.js
@@ -0,0 +1,28 @@
+'use strict';
+const fs = require('node:fs');
+const path = require('node:path');
+
+// JSON-file-backed link store. Missing or corrupt files start clean; every
+// mutation is flushed synchronously so a restart never loses a committed link.
+function createStore(file) {
+ let links = new Map();
+ try {
+ const raw = JSON.parse(fs.readFileSync(file, 'utf8'));
+ for (const [code, value] of Object.entries(raw.links || {})) links.set(code, value);
+ } catch { /* missing or corrupt: start empty */ }
+ const save = () => {
+ fs.mkdirSync(path.dirname(file), { recursive: true });
+ fs.writeFileSync(file, `${JSON.stringify({ links: Object.fromEntries(links) }, null, 1)}\n`);
+ };
+ return {
+ get: code => links.get(code) || null,
+ set(code, value) { links.set(code, value); save(); },
+ delete(code) { const had = links.delete(code); if (had) save(); return had; },
+ incrementHits(code) {
+ const link = links.get(code);
+ if (link) { link.hits = (link.hits || 0) + 1; save(); }
+ },
+ };
+}
+
+module.exports = { createStore };
diff --git a/docker/context-profiles/complex-eval/reference3/chained-tickets/test/links.test.js b/docker/context-profiles/complex-eval/reference3/chained-tickets/test/links.test.js
new file mode 100644
index 000000000..1a358d43d
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference3/chained-tickets/test/links.test.js
@@ -0,0 +1,106 @@
+'use strict';
+const test = require('node:test');
+const assert = require('node:assert/strict');
+const { createApp } = require('../src/app');
+
+process.env.DATA_FILE = require('node:path').join(require('node:os').tmpdir(),
+ `shortlink-test-${process.pid}.json`);
+
+let server;
+let port;
+test.before(async () => {
+ server = createApp();
+ await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
+ port = server.address().port;
+});
+test.after(() => server.close());
+
+const post = body => fetch(`http://127.0.0.1:${port}/links`, {
+ method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) });
+const get = p => fetch(`http://127.0.0.1:${port}${p}`, { redirect: 'manual' });
+
+test('creates a link with default expiry', async () => {
+ const res = await post({ url: 'https://example.com/a' });
+ assert.equal(res.status, 201);
+ const body = await res.json();
+ assert.match(body.code, /^[A-Za-z0-9]{6,10}$/);
+ assert.ok(Date.parse(body.expiresAt) > Date.now());
+});
+
+test('redirects with 302 and location', async () => {
+ const { code } = await (await post({ url: 'https://example.com/b' })).json();
+ const res = await get(`/${code}`);
+ assert.equal(res.status, 302);
+ assert.equal(res.headers.get('location'), 'https://example.com/b');
+});
+
+test('unknown code is a 404 envelope', async () => {
+ const res = await get('/zzzzzz');
+ assert.equal(res.status, 404);
+ assert.equal((await res.json()).error.code, 'NOT_FOUND');
+});
+
+test('invalid url is a 400 envelope', async () => {
+ const res = await post({ url: 'notaurl' });
+ assert.equal(res.status, 400);
+ assert.equal((await res.json()).error.code, 'INVALID_URL');
+});
+
+test('javascript scheme rejected', async () => {
+ const res = await post({ url: 'javascript:alert(1)' });
+ assert.equal(res.status, 400);
+});
+
+test('ttl bounds enforced', async () => {
+ const res = await post({ url: 'https://example.com', ttlSeconds: 99999999 });
+ assert.equal(res.status, 400);
+ assert.equal((await res.json()).error.code, 'INVALID_TTL');
+});
+
+test('delete flow', async () => {
+ const { code } = await (await post({ url: 'https://example.com/c' })).json();
+ const del = await fetch(`http://127.0.0.1:${port}/links/${code}`, { method: 'DELETE' });
+ assert.equal(del.status, 204);
+ assert.equal((await get(`/${code}`)).status, 404);
+});
+
+test('stats start at zero and count redirects', async () => {
+ const { code } = await (await post({ url: 'https://example.com/d' })).json();
+ const zero = await (await fetch(`http://127.0.0.1:${port}/links/${code}/stats`)).json();
+ assert.equal(zero.hits, 0);
+ await get(`/${code}`);
+ await get(`/${code}`);
+ const two = await (await fetch(`http://127.0.0.1:${port}/links/${code}/stats`)).json();
+ assert.equal(two.hits, 2);
+});
+
+test('stats for unknown code are a 404 envelope', async () => {
+ const res = await fetch(`http://127.0.0.1:${port}/links/zzzzzz/stats`);
+ assert.equal(res.status, 404);
+ assert.equal((await res.json()).error.code, 'NOT_FOUND');
+});
+
+test('expired links are 410', async () => {
+ const { code } = await (await post({ url: 'https://example.com/e', ttlSeconds: 1 })).json();
+ await new Promise(resolve => setTimeout(resolve, 1200));
+ assert.equal((await get(`/${code}`)).status, 410);
+});
+
+test('malformed json is a 400 envelope', async () => {
+ const res = await fetch(`http://127.0.0.1:${port}/links`, {
+ method: 'POST', headers: { 'content-type': 'application/json' }, body: '{nope' });
+ assert.equal(res.status, 400);
+ assert.equal((await res.json()).error.code, 'INVALID_JSON');
+});
+
+test('error responses never leak html', async () => {
+ const res = await get('/zzzzzz');
+ assert.match(res.headers.get('content-type'), /application\/json/);
+});
+
+// Last: the flood exhausts the per-client rate-limit bucket.
+test('rate limiting kicks in under a flood', async () => {
+ const responses = await Promise.all(Array.from({ length: 30 }, (_, i) =>
+ post({ url: `https://example.com/flood-${i}` })));
+ assert.ok(responses.some(r => r.status === 429));
+});
diff --git a/docker/context-profiles/complex-eval/reference3/idempotent-webhooks/CHANGELOG.md b/docker/context-profiles/complex-eval/reference3/idempotent-webhooks/CHANGELOG.md
new file mode 100644
index 000000000..e0560c4a5
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference3/idempotent-webhooks/CHANGELOG.md
@@ -0,0 +1,6 @@
+# Changelog
+
+- 2026-09-25: Fixed INC-104 — the receiver now claims each event id and applies
+ the payment synchronously in one event-loop turn, so concurrent duplicate
+ deliveries can never both pass the seen-check. Added idempotency regression
+ tests for concurrent duplicates, retries, and already-paid orders.
diff --git a/docker/context-profiles/complex-eval/reference3/idempotent-webhooks/src/app.js b/docker/context-profiles/complex-eval/reference3/idempotent-webhooks/src/app.js
new file mode 100644
index 000000000..57f29c250
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference3/idempotent-webhooks/src/app.js
@@ -0,0 +1,87 @@
+'use strict';
+const http = require('node:http');
+const { store } = require('./store');
+
+// Fixed after INC-104: all state checks and mutations happen synchronously in
+// one turn of the event loop — an event is claimed the instant its body is
+// parsed, before any await, so concurrent duplicates can never both pass.
+class HttpError extends Error {
+ constructor(status, code, message) {
+ super(message);
+ this.status = status;
+ this.code = code;
+ }
+}
+
+function sendJson(res, status, value) {
+ res.writeHead(status, { 'content-type': 'application/json' });
+ res.end(JSON.stringify(value));
+}
+
+function sendError(res, error) {
+ const known = error instanceof HttpError;
+ sendJson(res, known ? error.status : 500, {
+ error: { code: known ? error.code : 'INTERNAL', message: known ? error.message : 'internal error' },
+ });
+}
+
+function readBody(req) {
+ return new Promise((resolve, reject) => {
+ let body = '';
+ req.on('data', chunk => { body += chunk; });
+ req.on('end', () => {
+ try { resolve(JSON.parse(body)); } catch { reject(new HttpError(400, 'INVALID_JSON', 'body must be valid JSON')); }
+ });
+ req.on('error', reject);
+ });
+}
+
+function validateEvent(parsed) {
+ if (!parsed || typeof parsed.eventId !== 'string' || !parsed.eventId
+ || typeof parsed.orderId !== 'string' || !parsed.orderId
+ || !Number.isInteger(parsed.amountCents) || parsed.amountCents <= 0
+ || parsed.type !== 'payment.succeeded') {
+ throw new HttpError(400, 'INVALID_EVENT', 'body must be a valid payment.succeeded event');
+ }
+ return parsed;
+}
+
+// Synchronous claim-and-apply: no awaits inside, so it is atomic.
+function applyEvent({ eventId, orderId, amountCents }) {
+ if (store.processedEvents.has(eventId)) return { status: 'duplicate', orderId };
+ const order = store.orders.get(orderId);
+ if (!order) throw new HttpError(404, 'NOT_FOUND', 'no such order');
+ if (order.amountCents !== amountCents) throw new HttpError(422, 'AMOUNT_MISMATCH', 'amountCents does not match the order');
+ if (order.status === 'paid') return { status: 'already_paid', orderId };
+ store.processedEvents.add(eventId);
+ order.status = 'paid';
+ order.paidAt = new Date().toISOString();
+ order.paymentsApplied++;
+ store.paymentLog.push({ eventId, orderId, amountCents });
+ return { status: 'processed', orderId };
+}
+
+function createApp() {
+ return http.createServer(async (req, res) => {
+ const url = new URL(req.url, 'http://localhost');
+ try {
+ if (req.method === 'POST' && url.pathname === '/webhooks/payments') {
+ const parsed = validateEvent(await readBody(req));
+ sendJson(res, 200, applyEvent(parsed));
+ return;
+ }
+ const match = /^\/orders\/([\w-]+)$/.exec(url.pathname);
+ if (req.method === 'GET' && match) {
+ const order = store.orders.get(match[1]);
+ if (!order) throw new HttpError(404, 'NOT_FOUND', 'no such order');
+ sendJson(res, 200, order);
+ return;
+ }
+ throw new HttpError(404, 'NOT_FOUND', 'not found');
+ } catch (error) {
+ sendError(res, error);
+ }
+ });
+}
+
+module.exports = { createApp };
diff --git a/docker/context-profiles/complex-eval/reference3/idempotent-webhooks/test/webhooks.test.js b/docker/context-profiles/complex-eval/reference3/idempotent-webhooks/test/webhooks.test.js
new file mode 100644
index 000000000..cdd102f49
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference3/idempotent-webhooks/test/webhooks.test.js
@@ -0,0 +1,60 @@
+'use strict';
+const test = require('node:test');
+const assert = require('node:assert/strict');
+const { createApp } = require('../src/app');
+const { store } = require('../src/store');
+
+let server;
+let port;
+test.before(async () => {
+ server = createApp();
+ await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
+ port = server.address().port;
+});
+test.after(() => server.close());
+
+const send = (eventId, orderId, amountCents) => fetch(`http://127.0.0.1:${port}/webhooks/payments`, {
+ method: 'POST', headers: { 'content-type': 'application/json' },
+ body: JSON.stringify({ eventId, orderId, amountCents, type: 'payment.succeeded' }) });
+
+test('a single payment event processes', async () => {
+ const res = await send('ev-t-1', 'o1', 5000);
+ assert.equal(res.status, 200);
+ assert.equal((await res.json()).status, 'processed');
+ assert.equal(store.orders.get('o1').status, 'paid');
+});
+
+test('a sequential retry is an inert duplicate', async () => {
+ await send('ev-t-2', 'o3', 800);
+ const before = store.paymentLog.filter(p => p.orderId === 'o3').length;
+ const res = await send('ev-t-2', 'o3', 800);
+ assert.equal((await res.json()).status, 'duplicate');
+ assert.equal(store.paymentLog.filter(p => p.orderId === 'o3').length, before);
+});
+
+test('fifty concurrent duplicates apply exactly once (INC-104 regression)', async () => {
+ const storm = await Promise.all(Array.from({ length: 50 }, () => send('ev-t-storm', 'o4', 9999)));
+ const bodies = [];
+ for (const r of storm) bodies.push(await r.json());
+ assert.equal(bodies.filter(b => b.status === 'processed').length, 1);
+ assert.equal(bodies.filter(b => b.status === 'duplicate').length, 49);
+ assert.equal(store.orders.get('o4').paymentsApplied, 1);
+});
+
+test('a second event for a paid order is already_paid', async () => {
+ const res = await send('ev-t-3', 'o4', 9999);
+ assert.equal((await res.json()).status, 'already_paid');
+ assert.equal(store.orders.get('o4').paymentsApplied, 1);
+});
+
+test('amount mismatch is 422 and inert', async () => {
+ const res = await send('ev-t-4', 'o5', 1);
+ assert.equal(res.status, 422);
+ assert.equal(store.orders.get('o5').status, 'pending');
+});
+
+test('unknown order is a 404 envelope', async () => {
+ const res = await send('ev-t-5', 'nope', 100);
+ assert.equal(res.status, 404);
+ assert.equal((await res.json()).error.code, 'NOT_FOUND');
+});
diff --git a/docker/context-profiles/complex-eval/reference3/production-ready/CHANGELOG.md b/docker/context-profiles/complex-eval/reference3/production-ready/CHANGELOG.md
new file mode 100644
index 000000000..e0b0f3c6a
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference3/production-ready/CHANGELOG.md
@@ -0,0 +1,6 @@
+# Changelog
+
+- 2026-09-25: Production hardening — request validation with structured JSON
+ error envelopes, 64 KB body limit with 413, /health endpoint, structured
+ JSON request logging, PORT from the environment, graceful SIGTERM shutdown,
+ nosniff headers, and error-path test coverage.
diff --git a/docker/context-profiles/complex-eval/reference3/production-ready/src/app.js b/docker/context-profiles/complex-eval/reference3/production-ready/src/app.js
new file mode 100644
index 000000000..ccdecd16e
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference3/production-ready/src/app.js
@@ -0,0 +1,100 @@
+'use strict';
+const http = require('node:http');
+
+const MAX_BODY_BYTES = Number(process.env.MAX_BODY_BYTES || 64 * 1024);
+
+class HttpError extends Error {
+ constructor(status, code, message) {
+ super(message);
+ this.status = status;
+ this.code = code;
+ }
+}
+
+function sendJson(res, status, value) {
+ res.writeHead(status, { 'content-type': 'application/json', 'x-content-type-options': 'nosniff' });
+ res.end(JSON.stringify(value));
+}
+
+function sendError(res, error) {
+ const known = error instanceof HttpError;
+ sendJson(res, known ? error.status : 500, {
+ error: { code: known ? error.code : 'INTERNAL', message: known ? error.message : 'internal error' },
+ });
+}
+
+function readBody(req) {
+ return new Promise((resolve, reject) => {
+ let body = '';
+ let bytes = 0;
+ let settled = false;
+ req.on('data', chunk => {
+ if (settled) return;
+ bytes += chunk.length;
+ if (bytes > MAX_BODY_BYTES) {
+ settled = true;
+ reject(new HttpError(413, 'PAYLOAD_TOO_LARGE', 'request body exceeds 64 KB'));
+ // Drain rather than destroy: the socket must live long enough to send the 413.
+ req.resume();
+ return;
+ }
+ body += chunk;
+ });
+ req.on('end', () => {
+ if (settled) return;
+ settled = true;
+ try { resolve(JSON.parse(body)); } catch { reject(new HttpError(400, 'INVALID_JSON', 'body must be valid JSON')); }
+ });
+ req.on('error', reject);
+ });
+}
+
+function validateNote(input) {
+ if (!input || typeof input.title !== 'string' || !input.title.trim()) {
+ throw new HttpError(400, 'INVALID_TITLE', 'title must be a non-empty string');
+ }
+ if (typeof input.body !== 'string') throw new HttpError(400, 'INVALID_BODY', 'body must be a string');
+ return { title: input.title, body: input.body };
+}
+
+function createApp() {
+ const notes = new Map();
+ let nextId = 1;
+
+ const server = http.createServer(async (req, res) => {
+ const url = new URL(req.url, 'http://localhost');
+ try {
+ if (req.method === 'GET' && url.pathname === '/health') {
+ sendJson(res, 200, { status: 'ok' });
+ return;
+ }
+ if (req.method === 'POST' && url.pathname === '/notes') {
+ const fields = validateNote(await readBody(req));
+ const id = `n_${nextId++}`;
+ notes.set(id, { id, ...fields });
+ sendJson(res, 201, notes.get(id));
+ return;
+ }
+ const match = /^\/notes\/([\w-]+)$/.exec(url.pathname);
+ if (req.method === 'GET' && match) {
+ const note = notes.get(match[1]);
+ if (!note) throw new HttpError(404, 'NOT_FOUND', 'no note with that id');
+ sendJson(res, 200, note);
+ return;
+ }
+ if (req.method === 'GET' && url.pathname === '/notes') {
+ sendJson(res, 200, { notes: [...notes.values()] });
+ return;
+ }
+ throw new HttpError(404, 'NOT_FOUND', 'not found');
+ } catch (error) {
+ sendError(res, error);
+ } finally {
+ console.log(JSON.stringify({ method: req.method, path: url.pathname,
+ status: res.statusCode, at: new Date().toISOString() }));
+ }
+ });
+ return server;
+}
+
+module.exports = { createApp };
diff --git a/docker/context-profiles/complex-eval/reference3/production-ready/src/index.js b/docker/context-profiles/complex-eval/reference3/production-ready/src/index.js
new file mode 100644
index 000000000..9b1d0a0d7
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference3/production-ready/src/index.js
@@ -0,0 +1,13 @@
+'use strict';
+const { createApp } = require('./app');
+
+const port = Number(process.env.PORT || 8080);
+const server = createApp();
+server.listen(port, () => {
+ console.log(JSON.stringify({ event: 'listening', port }));
+});
+
+process.on('SIGTERM', () => {
+ server.close(() => process.exit(0));
+ setTimeout(() => process.exit(1), 5000).unref();
+});
diff --git a/docker/context-profiles/complex-eval/reference3/production-ready/test/notes.test.js b/docker/context-profiles/complex-eval/reference3/production-ready/test/notes.test.js
new file mode 100644
index 000000000..65e4ca200
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference3/production-ready/test/notes.test.js
@@ -0,0 +1,58 @@
+'use strict';
+const test = require('node:test');
+const assert = require('node:assert/strict');
+const { createApp } = require('../src/app');
+
+let server;
+let port;
+test.before(async () => {
+ server = createApp();
+ await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
+ port = server.address().port;
+});
+test.after(() => server.close());
+
+const post = body => fetch(`http://127.0.0.1:${port}/notes`, {
+ method: 'POST', headers: { 'content-type': 'application/json' }, body });
+
+test('create and read a note', async () => {
+ const created = await post(JSON.stringify({ title: 'first', body: 'hello' }));
+ assert.equal(created.status, 201);
+ const { id } = await created.json();
+ const read = await fetch(`http://127.0.0.1:${port}/notes/${id}`);
+ assert.equal((await read.json()).title, 'first');
+});
+
+test('malformed json is a 400 envelope', async () => {
+ const res = await post('{oops');
+ assert.equal(res.status, 400);
+ assert.equal((await res.json()).error.code, 'INVALID_JSON');
+});
+
+test('missing title is a 400 envelope', async () => {
+ const res = await post(JSON.stringify({ body: 'x' }));
+ assert.equal(res.status, 400);
+ assert.equal((await res.json()).error.code, 'INVALID_TITLE');
+});
+
+test('unknown note is a 404 envelope', async () => {
+ const res = await fetch(`http://127.0.0.1:${port}/notes/n_9999`);
+ assert.equal(res.status, 404);
+ assert.equal((await res.json()).error.code, 'NOT_FOUND');
+});
+
+test('oversize body is a 413 envelope', async () => {
+ const res = await post(JSON.stringify({ title: 'x', body: 'y'.repeat(100 * 1024) }));
+ assert.equal(res.status, 413);
+});
+
+test('health endpoint', async () => {
+ const res = await fetch(`http://127.0.0.1:${port}/health`);
+ assert.equal(res.status, 200);
+ assert.equal((await res.json()).status, 'ok');
+});
+
+test('nosniff header present', async () => {
+ const res = await fetch(`http://127.0.0.1:${port}/notes`);
+ assert.equal(res.headers.get('x-content-type-options'), 'nosniff');
+});
diff --git a/docker/context-profiles/complex-eval/reference4/chained-tickets/CHANGELOG.md b/docker/context-profiles/complex-eval/reference4/chained-tickets/CHANGELOG.md
new file mode 100644
index 000000000..e8cad2f0c
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference4/chained-tickets/CHANGELOG.md
@@ -0,0 +1,6 @@
+# Changelog
+
+- 2026-09-25: Initial shortlink core — create, redirect, expiry, and delete per API.md.
+- 2026-09-25: Persistence — links survive restarts via the DATA_FILE JSON store; missing or corrupt data files start clean.
+- 2026-09-25: Abuse protection — URL validation (http/https only, length cap), request body limits, and per-client rate limiting with 429 responses.
+- 2026-09-25: Analytics — per-link redirect hit counts exposed at GET /links/:code/stats.
diff --git a/docker/context-profiles/complex-eval/reference4/chained-tickets/README.md b/docker/context-profiles/complex-eval/reference4/chained-tickets/README.md
new file mode 100644
index 000000000..3420482fe
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference4/chained-tickets/README.md
@@ -0,0 +1,14 @@
+# shortlink
+
+Internal link shortener service. Node.js standard library only, CommonJS.
+
+- `API.md` — the HTTP contract.
+- `CONTRIBUTING.md` — engineering conventions. Every ticket follows them.
+- `src/app.js` exports `createApp()` returning an `http.Server` that is not yet
+ listening; `node src/index.js /stats` returns `{ "code", "hits", "expiresAt" }` —
+ `hits` counts redirects.
+- The API is rate limited per client and validates URLs (http/https only).
+- Run the tests with `npm test`.
diff --git a/docker/context-profiles/complex-eval/reference4/chained-tickets/src/app.js b/docker/context-profiles/complex-eval/reference4/chained-tickets/src/app.js
new file mode 100644
index 000000000..c802a64fd
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference4/chained-tickets/src/app.js
@@ -0,0 +1,15 @@
+'use strict';
+const http = require('node:http');
+const path = require('node:path');
+const { createStore } = require('./store');
+const { createService } = require('./service');
+const { createRouter } = require('./routes');
+
+function createApp() {
+ const file = process.env.DATA_FILE || path.join(process.cwd(), 'data', 'links.json');
+ const store = createStore(file);
+ const service = createService(store);
+ return http.createServer(createRouter(service));
+}
+
+module.exports = { createApp };
diff --git a/docker/context-profiles/complex-eval/reference4/chained-tickets/src/index.js b/docker/context-profiles/complex-eval/reference4/chained-tickets/src/index.js
new file mode 100644
index 000000000..d37872b76
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference4/chained-tickets/src/index.js
@@ -0,0 +1,7 @@
+'use strict';
+const { createApp } = require('./app');
+
+const port = Number(process.env.PORT || process.argv[2] || 8080);
+createApp().listen(port, () => {
+ console.log(`shortlink listening on ${port}`);
+});
diff --git a/docker/context-profiles/complex-eval/reference4/chained-tickets/src/routes.js b/docker/context-profiles/complex-eval/reference4/chained-tickets/src/routes.js
new file mode 100644
index 000000000..7344146c6
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference4/chained-tickets/src/routes.js
@@ -0,0 +1,86 @@
+'use strict';
+const { HttpError } = require('./service');
+
+const MAX_BODY_BYTES = 64 * 1024;
+
+function sendJson(res, status, value) {
+ res.writeHead(status, { 'content-type': 'application/json' });
+ res.end(JSON.stringify(value));
+}
+
+function sendError(res, error) {
+ const known = error instanceof HttpError;
+ sendJson(res, known ? error.status : 500, {
+ error: { code: known ? error.code : 'INTERNAL', message: known ? error.message : 'internal error' },
+ });
+}
+
+function readBody(req) {
+ return new Promise((resolve, reject) => {
+ let body = '';
+ let bytes = 0;
+ let settled = false;
+ req.on('data', chunk => {
+ if (settled) return;
+ bytes += chunk.length;
+ if (bytes > MAX_BODY_BYTES) {
+ settled = true;
+ reject(new HttpError(413, 'PAYLOAD_TOO_LARGE', 'request body too large'));
+ // Drain rather than destroy: the socket must live long enough to send the 413.
+ req.resume();
+ return;
+ }
+ body += chunk;
+ });
+ req.on('end', () => {
+ if (settled) return;
+ settled = true;
+ if (!body) { resolve({}); return; }
+ try { resolve(JSON.parse(body)); } catch { reject(new HttpError(400, 'INVALID_JSON', 'body must be valid JSON')); }
+ });
+ req.on('error', reject);
+ });
+}
+
+function createRouter(service) {
+ return async (req, res) => {
+ try {
+ const url = new URL(req.url, 'http://localhost');
+
+ if (req.method === 'POST' && url.pathname === '/links') {
+ service.assertRateLimit(req.socket.remoteAddress || 'unknown');
+ const link = service.createLink(await readBody(req));
+ sendJson(res, 201, { code: link.code, shortUrl: `/${link.code}`, expiresAt: link.expiresAt });
+ return;
+ }
+
+ const statsMatch = /^\/links\/([A-Za-z0-9]{1,20})\/stats$/.exec(url.pathname);
+ if (req.method === 'GET' && statsMatch) {
+ sendJson(res, 200, service.stats(statsMatch[1]));
+ return;
+ }
+
+ const linkMatch = /^\/links\/([A-Za-z0-9]{1,20})$/.exec(url.pathname);
+ if (req.method === 'DELETE' && linkMatch) {
+ service.deleteLink(linkMatch[1]);
+ res.writeHead(204);
+ res.end();
+ return;
+ }
+
+ const redirectMatch = /^\/([A-Za-z0-9]{1,20})$/.exec(url.pathname);
+ if (req.method === 'GET' && redirectMatch) {
+ const link = service.resolveLink(redirectMatch[1]);
+ res.writeHead(302, { location: link.url });
+ res.end();
+ return;
+ }
+
+ throw new HttpError(404, 'NOT_FOUND', 'not found');
+ } catch (error) {
+ sendError(res, error);
+ }
+ };
+}
+
+module.exports = { createRouter };
diff --git a/docker/context-profiles/complex-eval/reference4/chained-tickets/src/service.js b/docker/context-profiles/complex-eval/reference4/chained-tickets/src/service.js
new file mode 100644
index 000000000..f28167d8a
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference4/chained-tickets/src/service.js
@@ -0,0 +1,82 @@
+'use strict';
+const crypto = require('node:crypto');
+
+const MAX_URL_LENGTH = 2048;
+const DEFAULT_TTL_SECONDS = 604800;
+const MAX_TTL_SECONDS = 2592000;
+const RATE_LIMIT_WINDOW_MS = 60000;
+const RATE_LIMIT_MAX = 20;
+
+class HttpError extends Error {
+ constructor(status, code, message) {
+ super(message);
+ this.status = status;
+ this.code = code;
+ }
+}
+
+function validateUrl(url) {
+ if (typeof url !== 'string' || !url) throw new HttpError(400, 'INVALID_URL', 'url is required');
+ if (url.length > MAX_URL_LENGTH) throw new HttpError(400, 'INVALID_URL', 'url exceeds 2048 characters');
+ let parsed;
+ try { parsed = new URL(url); } catch { throw new HttpError(400, 'INVALID_URL', 'url must be a valid absolute URL'); }
+ if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
+ throw new HttpError(400, 'INVALID_URL', 'only http and https URLs are allowed');
+ }
+ return url;
+}
+
+function validateTtl(ttlSeconds) {
+ if (ttlSeconds === undefined || ttlSeconds === null) return DEFAULT_TTL_SECONDS;
+ if (!Number.isInteger(ttlSeconds) || ttlSeconds < 1 || ttlSeconds > MAX_TTL_SECONDS) {
+ throw new HttpError(400, 'INVALID_TTL', 'ttlSeconds must be an integer between 1 and 2592000');
+ }
+ return ttlSeconds;
+}
+
+function createService(store) {
+ const buckets = new Map();
+
+ function assertRateLimit(key) {
+ const now = Date.now();
+ const windowHits = (buckets.get(key) || []).filter(at => now - at < RATE_LIMIT_WINDOW_MS);
+ if (windowHits.length >= RATE_LIMIT_MAX) throw new HttpError(429, 'RATE_LIMITED', 'too many requests, slow down');
+ windowHits.push(now);
+ buckets.set(key, windowHits);
+ }
+
+ function freshCode() {
+ let code = crypto.randomBytes(4).toString('hex');
+ while (store.get(code)) code = crypto.randomBytes(4).toString('hex');
+ return code;
+ }
+
+ return {
+ assertRateLimit,
+ createLink({ url, ttlSeconds } = {}) {
+ const validUrl = validateUrl(url);
+ const ttl = validateTtl(ttlSeconds);
+ const link = { code: freshCode(), url: validUrl,
+ expiresAt: new Date(Date.now() + ttl * 1000).toISOString(), hits: 0 };
+ store.set(link.code, link);
+ return link;
+ },
+ resolveLink(code) {
+ const link = store.get(code);
+ if (!link) throw new HttpError(404, 'NOT_FOUND', 'no link with that code');
+ if (Date.parse(link.expiresAt) <= Date.now()) throw new HttpError(410, 'GONE', 'link has expired');
+ store.incrementHits(code);
+ return link;
+ },
+ deleteLink(code) {
+ if (!store.delete(code)) throw new HttpError(404, 'NOT_FOUND', 'no link with that code');
+ },
+ stats(code) {
+ const link = store.get(code);
+ if (!link) throw new HttpError(404, 'NOT_FOUND', 'no link with that code');
+ return { code, hits: link.hits || 0, expiresAt: link.expiresAt };
+ },
+ };
+}
+
+module.exports = { createService, HttpError };
diff --git a/docker/context-profiles/complex-eval/reference4/chained-tickets/src/store.js b/docker/context-profiles/complex-eval/reference4/chained-tickets/src/store.js
new file mode 100644
index 000000000..7d5aa091b
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference4/chained-tickets/src/store.js
@@ -0,0 +1,28 @@
+'use strict';
+const fs = require('node:fs');
+const path = require('node:path');
+
+// JSON-file-backed link store. Missing or corrupt files start clean; every
+// mutation is flushed synchronously so a restart never loses a committed link.
+function createStore(file) {
+ let links = new Map();
+ try {
+ const raw = JSON.parse(fs.readFileSync(file, 'utf8'));
+ for (const [code, value] of Object.entries(raw.links || {})) links.set(code, value);
+ } catch { /* missing or corrupt: start empty */ }
+ const save = () => {
+ fs.mkdirSync(path.dirname(file), { recursive: true });
+ fs.writeFileSync(file, `${JSON.stringify({ links: Object.fromEntries(links) }, null, 1)}\n`);
+ };
+ return {
+ get: code => links.get(code) || null,
+ set(code, value) { links.set(code, value); save(); },
+ delete(code) { const had = links.delete(code); if (had) save(); return had; },
+ incrementHits(code) {
+ const link = links.get(code);
+ if (link) { link.hits = (link.hits || 0) + 1; save(); }
+ },
+ };
+}
+
+module.exports = { createStore };
diff --git a/docker/context-profiles/complex-eval/reference4/chained-tickets/test/links.test.js b/docker/context-profiles/complex-eval/reference4/chained-tickets/test/links.test.js
new file mode 100644
index 000000000..1a358d43d
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference4/chained-tickets/test/links.test.js
@@ -0,0 +1,106 @@
+'use strict';
+const test = require('node:test');
+const assert = require('node:assert/strict');
+const { createApp } = require('../src/app');
+
+process.env.DATA_FILE = require('node:path').join(require('node:os').tmpdir(),
+ `shortlink-test-${process.pid}.json`);
+
+let server;
+let port;
+test.before(async () => {
+ server = createApp();
+ await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
+ port = server.address().port;
+});
+test.after(() => server.close());
+
+const post = body => fetch(`http://127.0.0.1:${port}/links`, {
+ method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) });
+const get = p => fetch(`http://127.0.0.1:${port}${p}`, { redirect: 'manual' });
+
+test('creates a link with default expiry', async () => {
+ const res = await post({ url: 'https://example.com/a' });
+ assert.equal(res.status, 201);
+ const body = await res.json();
+ assert.match(body.code, /^[A-Za-z0-9]{6,10}$/);
+ assert.ok(Date.parse(body.expiresAt) > Date.now());
+});
+
+test('redirects with 302 and location', async () => {
+ const { code } = await (await post({ url: 'https://example.com/b' })).json();
+ const res = await get(`/${code}`);
+ assert.equal(res.status, 302);
+ assert.equal(res.headers.get('location'), 'https://example.com/b');
+});
+
+test('unknown code is a 404 envelope', async () => {
+ const res = await get('/zzzzzz');
+ assert.equal(res.status, 404);
+ assert.equal((await res.json()).error.code, 'NOT_FOUND');
+});
+
+test('invalid url is a 400 envelope', async () => {
+ const res = await post({ url: 'notaurl' });
+ assert.equal(res.status, 400);
+ assert.equal((await res.json()).error.code, 'INVALID_URL');
+});
+
+test('javascript scheme rejected', async () => {
+ const res = await post({ url: 'javascript:alert(1)' });
+ assert.equal(res.status, 400);
+});
+
+test('ttl bounds enforced', async () => {
+ const res = await post({ url: 'https://example.com', ttlSeconds: 99999999 });
+ assert.equal(res.status, 400);
+ assert.equal((await res.json()).error.code, 'INVALID_TTL');
+});
+
+test('delete flow', async () => {
+ const { code } = await (await post({ url: 'https://example.com/c' })).json();
+ const del = await fetch(`http://127.0.0.1:${port}/links/${code}`, { method: 'DELETE' });
+ assert.equal(del.status, 204);
+ assert.equal((await get(`/${code}`)).status, 404);
+});
+
+test('stats start at zero and count redirects', async () => {
+ const { code } = await (await post({ url: 'https://example.com/d' })).json();
+ const zero = await (await fetch(`http://127.0.0.1:${port}/links/${code}/stats`)).json();
+ assert.equal(zero.hits, 0);
+ await get(`/${code}`);
+ await get(`/${code}`);
+ const two = await (await fetch(`http://127.0.0.1:${port}/links/${code}/stats`)).json();
+ assert.equal(two.hits, 2);
+});
+
+test('stats for unknown code are a 404 envelope', async () => {
+ const res = await fetch(`http://127.0.0.1:${port}/links/zzzzzz/stats`);
+ assert.equal(res.status, 404);
+ assert.equal((await res.json()).error.code, 'NOT_FOUND');
+});
+
+test('expired links are 410', async () => {
+ const { code } = await (await post({ url: 'https://example.com/e', ttlSeconds: 1 })).json();
+ await new Promise(resolve => setTimeout(resolve, 1200));
+ assert.equal((await get(`/${code}`)).status, 410);
+});
+
+test('malformed json is a 400 envelope', async () => {
+ const res = await fetch(`http://127.0.0.1:${port}/links`, {
+ method: 'POST', headers: { 'content-type': 'application/json' }, body: '{nope' });
+ assert.equal(res.status, 400);
+ assert.equal((await res.json()).error.code, 'INVALID_JSON');
+});
+
+test('error responses never leak html', async () => {
+ const res = await get('/zzzzzz');
+ assert.match(res.headers.get('content-type'), /application\/json/);
+});
+
+// Last: the flood exhausts the per-client rate-limit bucket.
+test('rate limiting kicks in under a flood', async () => {
+ const responses = await Promise.all(Array.from({ length: 30 }, (_, i) =>
+ post({ url: `https://example.com/flood-${i}` })));
+ assert.ok(responses.some(r => r.status === 429));
+});
diff --git a/docker/context-profiles/complex-eval/reference4/idempotent-webhooks/CHANGELOG.md b/docker/context-profiles/complex-eval/reference4/idempotent-webhooks/CHANGELOG.md
new file mode 100644
index 000000000..e0560c4a5
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference4/idempotent-webhooks/CHANGELOG.md
@@ -0,0 +1,6 @@
+# Changelog
+
+- 2026-09-25: Fixed INC-104 — the receiver now claims each event id and applies
+ the payment synchronously in one event-loop turn, so concurrent duplicate
+ deliveries can never both pass the seen-check. Added idempotency regression
+ tests for concurrent duplicates, retries, and already-paid orders.
diff --git a/docker/context-profiles/complex-eval/reference4/idempotent-webhooks/src/app.js b/docker/context-profiles/complex-eval/reference4/idempotent-webhooks/src/app.js
new file mode 100644
index 000000000..57f29c250
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference4/idempotent-webhooks/src/app.js
@@ -0,0 +1,87 @@
+'use strict';
+const http = require('node:http');
+const { store } = require('./store');
+
+// Fixed after INC-104: all state checks and mutations happen synchronously in
+// one turn of the event loop — an event is claimed the instant its body is
+// parsed, before any await, so concurrent duplicates can never both pass.
+class HttpError extends Error {
+ constructor(status, code, message) {
+ super(message);
+ this.status = status;
+ this.code = code;
+ }
+}
+
+function sendJson(res, status, value) {
+ res.writeHead(status, { 'content-type': 'application/json' });
+ res.end(JSON.stringify(value));
+}
+
+function sendError(res, error) {
+ const known = error instanceof HttpError;
+ sendJson(res, known ? error.status : 500, {
+ error: { code: known ? error.code : 'INTERNAL', message: known ? error.message : 'internal error' },
+ });
+}
+
+function readBody(req) {
+ return new Promise((resolve, reject) => {
+ let body = '';
+ req.on('data', chunk => { body += chunk; });
+ req.on('end', () => {
+ try { resolve(JSON.parse(body)); } catch { reject(new HttpError(400, 'INVALID_JSON', 'body must be valid JSON')); }
+ });
+ req.on('error', reject);
+ });
+}
+
+function validateEvent(parsed) {
+ if (!parsed || typeof parsed.eventId !== 'string' || !parsed.eventId
+ || typeof parsed.orderId !== 'string' || !parsed.orderId
+ || !Number.isInteger(parsed.amountCents) || parsed.amountCents <= 0
+ || parsed.type !== 'payment.succeeded') {
+ throw new HttpError(400, 'INVALID_EVENT', 'body must be a valid payment.succeeded event');
+ }
+ return parsed;
+}
+
+// Synchronous claim-and-apply: no awaits inside, so it is atomic.
+function applyEvent({ eventId, orderId, amountCents }) {
+ if (store.processedEvents.has(eventId)) return { status: 'duplicate', orderId };
+ const order = store.orders.get(orderId);
+ if (!order) throw new HttpError(404, 'NOT_FOUND', 'no such order');
+ if (order.amountCents !== amountCents) throw new HttpError(422, 'AMOUNT_MISMATCH', 'amountCents does not match the order');
+ if (order.status === 'paid') return { status: 'already_paid', orderId };
+ store.processedEvents.add(eventId);
+ order.status = 'paid';
+ order.paidAt = new Date().toISOString();
+ order.paymentsApplied++;
+ store.paymentLog.push({ eventId, orderId, amountCents });
+ return { status: 'processed', orderId };
+}
+
+function createApp() {
+ return http.createServer(async (req, res) => {
+ const url = new URL(req.url, 'http://localhost');
+ try {
+ if (req.method === 'POST' && url.pathname === '/webhooks/payments') {
+ const parsed = validateEvent(await readBody(req));
+ sendJson(res, 200, applyEvent(parsed));
+ return;
+ }
+ const match = /^\/orders\/([\w-]+)$/.exec(url.pathname);
+ if (req.method === 'GET' && match) {
+ const order = store.orders.get(match[1]);
+ if (!order) throw new HttpError(404, 'NOT_FOUND', 'no such order');
+ sendJson(res, 200, order);
+ return;
+ }
+ throw new HttpError(404, 'NOT_FOUND', 'not found');
+ } catch (error) {
+ sendError(res, error);
+ }
+ });
+}
+
+module.exports = { createApp };
diff --git a/docker/context-profiles/complex-eval/reference4/idempotent-webhooks/test/webhooks.test.js b/docker/context-profiles/complex-eval/reference4/idempotent-webhooks/test/webhooks.test.js
new file mode 100644
index 000000000..cdd102f49
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference4/idempotent-webhooks/test/webhooks.test.js
@@ -0,0 +1,60 @@
+'use strict';
+const test = require('node:test');
+const assert = require('node:assert/strict');
+const { createApp } = require('../src/app');
+const { store } = require('../src/store');
+
+let server;
+let port;
+test.before(async () => {
+ server = createApp();
+ await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
+ port = server.address().port;
+});
+test.after(() => server.close());
+
+const send = (eventId, orderId, amountCents) => fetch(`http://127.0.0.1:${port}/webhooks/payments`, {
+ method: 'POST', headers: { 'content-type': 'application/json' },
+ body: JSON.stringify({ eventId, orderId, amountCents, type: 'payment.succeeded' }) });
+
+test('a single payment event processes', async () => {
+ const res = await send('ev-t-1', 'o1', 5000);
+ assert.equal(res.status, 200);
+ assert.equal((await res.json()).status, 'processed');
+ assert.equal(store.orders.get('o1').status, 'paid');
+});
+
+test('a sequential retry is an inert duplicate', async () => {
+ await send('ev-t-2', 'o3', 800);
+ const before = store.paymentLog.filter(p => p.orderId === 'o3').length;
+ const res = await send('ev-t-2', 'o3', 800);
+ assert.equal((await res.json()).status, 'duplicate');
+ assert.equal(store.paymentLog.filter(p => p.orderId === 'o3').length, before);
+});
+
+test('fifty concurrent duplicates apply exactly once (INC-104 regression)', async () => {
+ const storm = await Promise.all(Array.from({ length: 50 }, () => send('ev-t-storm', 'o4', 9999)));
+ const bodies = [];
+ for (const r of storm) bodies.push(await r.json());
+ assert.equal(bodies.filter(b => b.status === 'processed').length, 1);
+ assert.equal(bodies.filter(b => b.status === 'duplicate').length, 49);
+ assert.equal(store.orders.get('o4').paymentsApplied, 1);
+});
+
+test('a second event for a paid order is already_paid', async () => {
+ const res = await send('ev-t-3', 'o4', 9999);
+ assert.equal((await res.json()).status, 'already_paid');
+ assert.equal(store.orders.get('o4').paymentsApplied, 1);
+});
+
+test('amount mismatch is 422 and inert', async () => {
+ const res = await send('ev-t-4', 'o5', 1);
+ assert.equal(res.status, 422);
+ assert.equal(store.orders.get('o5').status, 'pending');
+});
+
+test('unknown order is a 404 envelope', async () => {
+ const res = await send('ev-t-5', 'nope', 100);
+ assert.equal(res.status, 404);
+ assert.equal((await res.json()).error.code, 'NOT_FOUND');
+});
diff --git a/docker/context-profiles/complex-eval/reference4/production-ready/CHANGELOG.md b/docker/context-profiles/complex-eval/reference4/production-ready/CHANGELOG.md
new file mode 100644
index 000000000..e0b0f3c6a
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference4/production-ready/CHANGELOG.md
@@ -0,0 +1,6 @@
+# Changelog
+
+- 2026-09-25: Production hardening — request validation with structured JSON
+ error envelopes, 64 KB body limit with 413, /health endpoint, structured
+ JSON request logging, PORT from the environment, graceful SIGTERM shutdown,
+ nosniff headers, and error-path test coverage.
diff --git a/docker/context-profiles/complex-eval/reference4/production-ready/src/app.js b/docker/context-profiles/complex-eval/reference4/production-ready/src/app.js
new file mode 100644
index 000000000..ccdecd16e
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference4/production-ready/src/app.js
@@ -0,0 +1,100 @@
+'use strict';
+const http = require('node:http');
+
+const MAX_BODY_BYTES = Number(process.env.MAX_BODY_BYTES || 64 * 1024);
+
+class HttpError extends Error {
+ constructor(status, code, message) {
+ super(message);
+ this.status = status;
+ this.code = code;
+ }
+}
+
+function sendJson(res, status, value) {
+ res.writeHead(status, { 'content-type': 'application/json', 'x-content-type-options': 'nosniff' });
+ res.end(JSON.stringify(value));
+}
+
+function sendError(res, error) {
+ const known = error instanceof HttpError;
+ sendJson(res, known ? error.status : 500, {
+ error: { code: known ? error.code : 'INTERNAL', message: known ? error.message : 'internal error' },
+ });
+}
+
+function readBody(req) {
+ return new Promise((resolve, reject) => {
+ let body = '';
+ let bytes = 0;
+ let settled = false;
+ req.on('data', chunk => {
+ if (settled) return;
+ bytes += chunk.length;
+ if (bytes > MAX_BODY_BYTES) {
+ settled = true;
+ reject(new HttpError(413, 'PAYLOAD_TOO_LARGE', 'request body exceeds 64 KB'));
+ // Drain rather than destroy: the socket must live long enough to send the 413.
+ req.resume();
+ return;
+ }
+ body += chunk;
+ });
+ req.on('end', () => {
+ if (settled) return;
+ settled = true;
+ try { resolve(JSON.parse(body)); } catch { reject(new HttpError(400, 'INVALID_JSON', 'body must be valid JSON')); }
+ });
+ req.on('error', reject);
+ });
+}
+
+function validateNote(input) {
+ if (!input || typeof input.title !== 'string' || !input.title.trim()) {
+ throw new HttpError(400, 'INVALID_TITLE', 'title must be a non-empty string');
+ }
+ if (typeof input.body !== 'string') throw new HttpError(400, 'INVALID_BODY', 'body must be a string');
+ return { title: input.title, body: input.body };
+}
+
+function createApp() {
+ const notes = new Map();
+ let nextId = 1;
+
+ const server = http.createServer(async (req, res) => {
+ const url = new URL(req.url, 'http://localhost');
+ try {
+ if (req.method === 'GET' && url.pathname === '/health') {
+ sendJson(res, 200, { status: 'ok' });
+ return;
+ }
+ if (req.method === 'POST' && url.pathname === '/notes') {
+ const fields = validateNote(await readBody(req));
+ const id = `n_${nextId++}`;
+ notes.set(id, { id, ...fields });
+ sendJson(res, 201, notes.get(id));
+ return;
+ }
+ const match = /^\/notes\/([\w-]+)$/.exec(url.pathname);
+ if (req.method === 'GET' && match) {
+ const note = notes.get(match[1]);
+ if (!note) throw new HttpError(404, 'NOT_FOUND', 'no note with that id');
+ sendJson(res, 200, note);
+ return;
+ }
+ if (req.method === 'GET' && url.pathname === '/notes') {
+ sendJson(res, 200, { notes: [...notes.values()] });
+ return;
+ }
+ throw new HttpError(404, 'NOT_FOUND', 'not found');
+ } catch (error) {
+ sendError(res, error);
+ } finally {
+ console.log(JSON.stringify({ method: req.method, path: url.pathname,
+ status: res.statusCode, at: new Date().toISOString() }));
+ }
+ });
+ return server;
+}
+
+module.exports = { createApp };
diff --git a/docker/context-profiles/complex-eval/reference4/production-ready/src/index.js b/docker/context-profiles/complex-eval/reference4/production-ready/src/index.js
new file mode 100644
index 000000000..9b1d0a0d7
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference4/production-ready/src/index.js
@@ -0,0 +1,13 @@
+'use strict';
+const { createApp } = require('./app');
+
+const port = Number(process.env.PORT || 8080);
+const server = createApp();
+server.listen(port, () => {
+ console.log(JSON.stringify({ event: 'listening', port }));
+});
+
+process.on('SIGTERM', () => {
+ server.close(() => process.exit(0));
+ setTimeout(() => process.exit(1), 5000).unref();
+});
diff --git a/docker/context-profiles/complex-eval/reference4/production-ready/test/notes.test.js b/docker/context-profiles/complex-eval/reference4/production-ready/test/notes.test.js
new file mode 100644
index 000000000..65e4ca200
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference4/production-ready/test/notes.test.js
@@ -0,0 +1,58 @@
+'use strict';
+const test = require('node:test');
+const assert = require('node:assert/strict');
+const { createApp } = require('../src/app');
+
+let server;
+let port;
+test.before(async () => {
+ server = createApp();
+ await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
+ port = server.address().port;
+});
+test.after(() => server.close());
+
+const post = body => fetch(`http://127.0.0.1:${port}/notes`, {
+ method: 'POST', headers: { 'content-type': 'application/json' }, body });
+
+test('create and read a note', async () => {
+ const created = await post(JSON.stringify({ title: 'first', body: 'hello' }));
+ assert.equal(created.status, 201);
+ const { id } = await created.json();
+ const read = await fetch(`http://127.0.0.1:${port}/notes/${id}`);
+ assert.equal((await read.json()).title, 'first');
+});
+
+test('malformed json is a 400 envelope', async () => {
+ const res = await post('{oops');
+ assert.equal(res.status, 400);
+ assert.equal((await res.json()).error.code, 'INVALID_JSON');
+});
+
+test('missing title is a 400 envelope', async () => {
+ const res = await post(JSON.stringify({ body: 'x' }));
+ assert.equal(res.status, 400);
+ assert.equal((await res.json()).error.code, 'INVALID_TITLE');
+});
+
+test('unknown note is a 404 envelope', async () => {
+ const res = await fetch(`http://127.0.0.1:${port}/notes/n_9999`);
+ assert.equal(res.status, 404);
+ assert.equal((await res.json()).error.code, 'NOT_FOUND');
+});
+
+test('oversize body is a 413 envelope', async () => {
+ const res = await post(JSON.stringify({ title: 'x', body: 'y'.repeat(100 * 1024) }));
+ assert.equal(res.status, 413);
+});
+
+test('health endpoint', async () => {
+ const res = await fetch(`http://127.0.0.1:${port}/health`);
+ assert.equal(res.status, 200);
+ assert.equal((await res.json()).status, 'ok');
+});
+
+test('nosniff header present', async () => {
+ const res = await fetch(`http://127.0.0.1:${port}/notes`);
+ assert.equal(res.headers.get('x-content-type-options'), 'nosniff');
+});
diff --git a/docker/context-profiles/complex-eval/reference4/recurring-incident/docs/handoff.md b/docker/context-profiles/complex-eval/reference4/recurring-incident/docs/handoff.md
new file mode 100644
index 000000000..91d68d69f
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference4/recurring-incident/docs/handoff.md
@@ -0,0 +1,35 @@
+# Handoff: refunds & payouts idempotency
+
+## What happened
+
+Two incidents, one root cause family:
+
+- **Refunds** (INC-201, INC-214, INC-227 in docs/incidents.md): refund requests
+ arriving without an idempotency key were double-processed whenever the
+ storefront retried, refunding customers twice.
+- **Payouts**: finance's batch job is about to start retrying on timeouts, and
+ keyless payout retries would double-pay vendors the same way.
+
+## The fix
+
+Both entry points now route through a single shared helper,
+`src/idempotency.js` (`deriveKey` + `once`). `src/refunds.js` and
+`src/payouts.js` derive a stable key from the request payload when the caller
+sends none, claim it synchronously so concurrent retries share one execution,
+and persist the receipt in `src/store.js` so retries after a restart return the
+stored receipt. Gateway side effects all go through `src/charge.js`, so the
+ledger is the source of truth for "did this actually happen".
+
+## Regression coverage
+
+`test/idempotency.test.js` covers keyless refund retries, restart durability,
+and a 20-way concurrent payout storm. The pre-existing `test/refunds.test.js`
+and `test/payouts.test.js` still cover the keyed contract. Everything is wired
+into `npm test`; run it before touching any of this.
+
+## Prevention
+
+`docs/runbooks/idempotency.md` is the runbook: any new money-moving operation
+must go through `src/idempotency.js`, ship with a retry regression test, and
+log recurrences in `docs/incidents.md`. Do not bolt a second inline key-check
+into a new module — extend the helper instead.
diff --git a/docker/context-profiles/complex-eval/reference4/recurring-incident/docs/runbooks/idempotency.md b/docker/context-profiles/complex-eval/reference4/recurring-incident/docs/runbooks/idempotency.md
new file mode 100644
index 000000000..00c32cfa9
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference4/recurring-incident/docs/runbooks/idempotency.md
@@ -0,0 +1,35 @@
+# Runbook: idempotency for money-moving operations
+
+## The incident class
+
+INC-201, INC-214, INC-227 (refunds) and the payout double-pay risk flagged by
+finance are one class of bug: a caller retries a money-moving request that
+carries no idempotency key, and the service executes it again. Asking clients
+to retry less has failed three times; prevention must live in the service.
+
+## The pattern
+
+Every money-moving entry point routes through the shared helper in
+`src/idempotency.js`:
+
+- `deriveKey(scope, parts)` builds a stable key from the request payload when
+ the caller did not supply one.
+- `once(store, key, produce)` claims the key synchronously (concurrent retries
+ share one execution) and persists the receipt (retries after a restart get
+ the stored receipt back).
+
+`src/refunds.js` and `src/payouts.js` both use it. Do not add a second inline
+implementation of key derivation or seen-tracking in another module.
+
+## Prevention procedure
+
+For any new operation that moves money (charges, refunds, payouts, credits,
+adjustments):
+
+1. Route the side effect through `once()` from `src/idempotency.js` — never
+ call the gateway directly from the entry point.
+2. Add a regression test that retries the operation without a key (including
+ a concurrent retry storm) and asserts the ledger shows exactly one effect.
+3. Run `npm test` before merging.
+4. If this class of bug recurs anywhere, log it in `docs/incidents.md` and
+ extend this runbook instead of fixing silently.
diff --git a/docker/context-profiles/complex-eval/reference4/recurring-incident/src/idempotency.js b/docker/context-profiles/complex-eval/reference4/recurring-incident/src/idempotency.js
new file mode 100644
index 000000000..7f5eb0fc8
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference4/recurring-incident/src/idempotency.js
@@ -0,0 +1,31 @@
+// Shared idempotency helper for money-moving entry points. Any operation that
+// must not happen twice derives a stable key (from the caller's idempotencyKey
+// or from the request payload) and routes through once().
+import crypto from 'node:crypto';
+
+const inflight = new Map();
+
+export function deriveKey(scope, parts) {
+ const hash = crypto.createHash('sha256').update(JSON.stringify(parts)).digest('hex').slice(0, 24);
+ return `${scope}:${hash}`;
+}
+
+// Runs produce() at most once per key. The key is claimed synchronously, so
+// concurrent callers share one execution, and the receipt is persisted, so a
+// retry after a restart returns the stored receipt instead of re-running.
+export async function once(store, key, produce) {
+ const existing = store.get(key);
+ if (existing) return { ...existing, duplicate: true };
+ if (inflight.has(key)) return { ...(await inflight.get(key)), duplicate: true };
+ const pending = (async () => {
+ const receipt = await produce();
+ store.set(key, receipt);
+ return receipt;
+ })();
+ inflight.set(key, pending);
+ try {
+ return await pending;
+ } finally {
+ inflight.delete(key);
+ }
+}
diff --git a/docker/context-profiles/complex-eval/reference4/recurring-incident/src/payouts.js b/docker/context-profiles/complex-eval/reference4/recurring-incident/src/payouts.js
new file mode 100644
index 000000000..fffb428ae
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference4/recurring-incident/src/payouts.js
@@ -0,0 +1,12 @@
+import { payout } from './charge.js';
+import * as store from './store.js';
+import { deriveKey, once } from './idempotency.js';
+
+// Processes a vendor payout through the same shared idempotency helper as
+// refunds, so a retry storm can never double-pay a vendor.
+export async function processPayout(req) {
+ const key = req.idempotencyKey
+ ? `payout:${req.idempotencyKey}`
+ : deriveKey('payout', { vendorId: req.vendorId, amount: req.amount });
+ return once(store, key, () => payout({ vendorId: req.vendorId, amount: req.amount }));
+}
diff --git a/docker/context-profiles/complex-eval/reference4/recurring-incident/src/refunds.js b/docker/context-profiles/complex-eval/reference4/recurring-incident/src/refunds.js
new file mode 100644
index 000000000..a756f09bc
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference4/recurring-incident/src/refunds.js
@@ -0,0 +1,13 @@
+import { refund } from './charge.js';
+import * as store from './store.js';
+import { deriveKey, once } from './idempotency.js';
+
+// Processes a customer refund. Requests without an idempotencyKey get a key
+// derived from the payload, so a retried call can never refund twice — see
+// docs/runbooks/idempotency.md.
+export async function processRefund(req) {
+ const key = req.idempotencyKey
+ ? `refund:${req.idempotencyKey}`
+ : deriveKey('refund', { orderId: req.orderId, amount: req.amount });
+ return once(store, key, () => refund({ orderId: req.orderId, amount: req.amount }));
+}
diff --git a/docker/context-profiles/complex-eval/reference4/recurring-incident/test/idempotency.test.js b/docker/context-profiles/complex-eval/reference4/recurring-incident/test/idempotency.test.js
new file mode 100644
index 000000000..20135eb69
--- /dev/null
+++ b/docker/context-profiles/complex-eval/reference4/recurring-incident/test/idempotency.test.js
@@ -0,0 +1,53 @@
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import fs from 'node:fs';
+import os from 'node:os';
+import path from 'node:path';
+import { readLedger } from '../src/charge.js';
+
+function freshEnv(t) {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'payments-idem-'));
+ process.env.LEDGER_FILE = path.join(dir, 'ledger.jsonl');
+ process.env.STORE_FILE = path.join(dir, 'store.json');
+ t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
+ return dir;
+}
+
+test('a refund retried without an idempotency key refunds exactly once', async (t) => {
+ const dir = freshEnv(t);
+ const { processRefund } = await import('../src/refunds.js');
+ await processRefund({ orderId: 'ord-retry', amount: 2500 });
+ await processRefund({ orderId: 'ord-retry', amount: 2500 });
+ const refunds = readLedger().filter(e => e.type === 'refund' && e.orderId === 'ord-retry');
+ assert.equal(refunds.length, 1);
+ assert.equal(fs.readdirSync(dir).includes('ledger.jsonl'), true);
+});
+
+test('refund idempotency survives a restart (fresh module, same store)', async (t) => {
+ freshEnv(t);
+ const first = await import('../src/refunds.js');
+ await first.processRefund({ orderId: 'ord-restart', amount: 3100 });
+ const reloaded = await import(`../src/refunds.js?restart=${Date.now()}`);
+ await reloaded.processRefund({ orderId: 'ord-restart', amount: 3100 });
+ const refunds = readLedger().filter(e => e.type === 'refund' && e.orderId === 'ord-restart');
+ assert.equal(refunds.length, 1);
+});
+
+test('a concurrent keyless payout retry storm pays exactly once', async (t) => {
+ freshEnv(t);
+ const { processPayout } = await import('../src/payouts.js');
+ await Promise.all(Array.from({ length: 20 },
+ () => processPayout({ vendorId: 'ven-storm', amount: 9000 })));
+ const payouts = readLedger().filter(e => e.type === 'payout' && e.vendorId === 'ven-storm');
+ assert.equal(payouts.length, 1);
+});
+
+test('payout idempotency survives a restart (fresh module, same store)', async (t) => {
+ freshEnv(t);
+ const first = await import('../src/payouts.js');
+ await first.processPayout({ vendorId: 'ven-restart', amount: 4000 });
+ const reloaded = await import(`../src/payouts.js?restart=${Date.now()}`);
+ await reloaded.processPayout({ vendorId: 'ven-restart', amount: 4000 });
+ const payouts = readLedger().filter(e => e.type === 'payout' && e.vendorId === 'ven-restart');
+ assert.equal(payouts.length, 1);
+});
diff --git a/docker/context-profiles/complex-eval/verify-checks.js b/docker/context-profiles/complex-eval/verify-checks.js
new file mode 100644
index 000000000..8c69d8d5e
--- /dev/null
+++ b/docker/context-profiles/complex-eval/verify-checks.js
@@ -0,0 +1,64 @@
+'use strict';
+// Development tool: validates the hidden graders end to end. For every task the
+// reference solution (referenceDir/
hi