File size: 9,639 Bytes
9368cc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/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")