#!/usr/bin/env python3 """Batch 6: more compound tasks. Sub-defects are reused from tasks already validated individually, so each is known to be findable and known to be covered by its own tests. Combining them changes only how many must be found in one episode. """ import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).parent)) from make_tasks import make, spec, CORPUS # noqa: E402 FLOW = "python/scheduler" ROUTER = "typescript/router" LEDGER = "python/ledger" def tests_from(repo, task, *names): base = CORPUS / repo / "tasks" / task / "tests" return {n: (base / n).read_text() for n in names} # ============================================== ledger, 3 defects make(LEDGER, "audit-findings", spec("python", "logic", 5, """ Three findings from the stock audit. They are unrelated; all three need fixing. 1. Valuation and cost of goods sold are wrong for any SKU received at more than one unit cost. The ledger is documented as valuing inventory first-in, first-out. 2. A SKU sitting exactly at its configured reorder point produces no replenishment suggestion, and we ran out during the supplier lead time. The reorder point is meant to be inclusive. 3. Catching a projection up a second time raises rather than applying only what is new. A projection is supposed to be resumable, and catching up with nothing new appended must apply zero events. """), [("ledger/projections.py", " oldest = lots[0]", " oldest = lots[-1]"), ("ledger/projections.py", " lots.pop(0)", " lots.pop()"), ("ledger/policies.py", " if level <= threshold:", " if level < threshold:"), ("ledger/store.py", " return [e for e in self._events if e.seq > seq]", " return [e for e in self._events if e.seq >= seq]")], {**tests_from(LEDGER, "fifo-lifo", "test_valuation.py"), **tests_from(LEDGER, "reorder-boundary", "test_reorder.py"), **tests_from(LEDGER, "since-inclusive", "test_catch_up.py")}) # ============================================== flow, 3 defects make(FLOW, "scheduler-regressions", spec("python", "logic", 5, """ Three regressions reported against the scheduler this sprint. They have separate causes; please fix all three. 1. Reading the graph can corrupt it. A reporting tool that collects the dependents of each task into a set and annotates that set finds the graph's own structure changed afterwards, and subsequent scheduling is wrong. 2. A task that fails to be admitted keeps hold of whatever it did manage to take. A task needing two different pools, refused the second, leaves the first held for the remainder of the run. 3. Runs containing a failure never finish. When a task in the middle of a chain fails, its immediate dependents are skipped but everything below them stays pending forever, so the run never reports itself complete. """), [("flow/graph.py", """ if task_id not in self._tasks: raise UnknownTask(task_id) return set(self._dependents.get(task_id, ()))""", """ if task_id not in self._tasks: raise UnknownTask(task_id) return self._dependents.setdefault(task_id, set())"""), ("flow/resources.py", """ requests = list(requests) for request in requests: if request.name not in self.capacity: raise UnknownResource(request.name) if self.free(request.name) < request.amount: raise ResourceExhausted(request.name, request.amount, self.free(request.name)) holding = self._held.setdefault(task_id, {}) for request in requests: holding[request.name] = holding.get(request.name, 0) + request.amount""", """ holding = self._held.setdefault(task_id, {}) for request in requests: if request.name not in self.capacity: raise UnknownResource(request.name) if self.free(request.name) < request.amount: raise ResourceExhausted(request.name, request.amount, self.free(request.name)) holding[request.name] = holding.get(request.name, 0) + request.amount"""), ("flow/scheduler.py", """ skipped: List[str] = [] for downstream in sorted(self.graph.descendants_of(task_id)):""", """ skipped: List[str] = [] for downstream in sorted(self.graph.dependents_of(task_id)):""")], {**tests_from(FLOW, "dependents-aliasing", "test_graph_isolation.py"), **tests_from(FLOW, "acquire-not-atomic", "test_atomic_acquire.py"), **tests_from(FLOW, "cascade-shallow", "test_cascade.py")}) # ============================================== router, 3 defects make(ROUTER, "routing-regressions", spec("typescript", "logic", 5, """ Three regressions in the router, from separate reports. All three need fixing. 1. Our static file route /assets/*path no longer serves anything in a subdirectory, and when a single-segment request does match, the captured parameter holds only that one segment. A wildcard should swallow the entire remainder of the path. 2. Multi-select filters apply only the last value. `?tag=red&tag=blue` behaves as though only blue were sent. Repeated keys should collect every value in order. 3. Popular endpoints keep falling out of the route cache while paths hit once at start-up survive indefinitely. Eviction is meant to remove whatever has gone longest without being used. """), [("src/trie.ts", """ if (segment.kind === 'wildcard') { params[segment.value] = parts.slice(i).join('/'); return params; }""", """ if (segment.kind === 'wildcard') { if (i >= parts.length) return undefined; params[segment.value] = parts[i]; continue; }"""), ("src/query.ts", """ if (out[key]) out[key].push(value); else out[key] = [value];""", """ out[key] = [value];"""), ("src/cache.ts", """ const value = this.store.get(key) as V; // reinsert so this key becomes the newest in iteration order this.store.delete(key); this.store.set(key, value); this.hits++; return value;""", """ const value = this.store.get(key) as V; this.hits++; return value;""")], {**tests_from(ROUTER, "wildcard-remainder", "wildcard.test.ts"), **tests_from(ROUTER, "query-repeats-lost", "query.test.ts"), **tests_from(ROUTER, "lru-recency-on-read", "cache.test.ts")}) # ============================================== flow, 2 defects make(FLOW, "retry-subsystem-broken", spec("python", "logic", 5, """ Two problems with retries, reported together. 1. A task that has failed and is waiting to be retried never becomes eligible again, no matter how far the clock is advanced. The run simply stalls. 2. Separately, capacity is not returned when an attempt fails, so a pool drains over the life of a run until nothing can be admitted. """), [("flow/scheduler.py", """ # RETRYING counts as a candidate: its backoff is enforced below by # ready_at, and leaving it out would mean a retry never fires. if state.get(task_id) not in (st.PENDING, st.READY, st.RETRYING): continue""", """ if state.get(task_id) not in (st.PENDING, st.READY): continue"""), ("flow/scheduler.py", """ waits out its backoff. \"\"\" self.pool.release(task_id) task = self.graph.get(task_id)""", """ waits out its backoff. \"\"\" task = self.graph.get(task_id)""")], {**tests_from(FLOW, "retry-never-fires", "test_retry.py"), **tests_from(FLOW, "resource-leak-on-retry", "test_resource_lifecycle.py")}) # ============================================== router, 2 defects make(ROUTER, "negotiation-and-headers", spec("typescript", "logic", 4, """ Two content-negotiation problems, likely separate causes. 1. Clients sending `Accept: text/html, application/json` with no explicit qualities get JSON. When two acceptable types carry equal quality, the client's own ordering is the tie-break. 2. Clients that spell the header `Content-Type` rather than `content-type` are treated as though it were absent. HTTP field names are case-insensitive. """), [("src/negotiate.ts", """ const candidates = parseAccept(header).filter((c) => c.quality > 0); let best: Candidate | undefined; for (const candidate of candidates) { if (!matchesAny(candidate.type, offered)) continue; if (best === undefined) { best = candidate; continue; } if (candidate.quality > best.quality) best = candidate; }""", """ const candidates = parseAccept(header) .filter((c) => c.quality > 0) .sort((a, b) => b.quality - a.quality || a.type.localeCompare(b.type)); let best: Candidate | undefined; for (const candidate of candidates) { if (!matchesAny(candidate.type, offered)) continue; if (best === undefined) best = candidate; }"""), ("src/headers.ts", """ private static key(name: string): string { return name.toLowerCase(); }""", """ private static key(name: string): string { return name; }""")], {**tests_from(ROUTER, "negotiate-tiebreak", "negotiate.test.ts"), **tests_from(ROUTER, "header-case-sensitive", "headers.test.ts")}) print("done")