general code and the edge-case tail · lesson 1 of 9 · sources checked 2026-11-30
count the special cases and measure the tail
This lesson counts the special cases of each general component and estimates the part of the edge-case tail that the log does not show yet.
status of this lesson: from a public sourcepurpose
general code must work for many sites, and each site can add a special case. A team that does not count its special cases cannot see when the work on them grows faster than the value. A count also shows where each special case lives, and how much of the failure set the team has not seen yet.
objectives
- List the general components of a system, and count the special cases of each component with their place.
- Compute the unseen share and the Chao1 bound from the class counts of a failure log.
- Read a discovery curve, and say if the tail still grows.
- Keep deliberate classes out of the estimators, with a separate count.
new terms
- general code
- Code that one team writes one time and that must work for many targets, many page forms, or many clients.
- special case
- A rule, an override, or a branch that handles one edge case, in code or in data.
- special-case inventory
- A table of each general component with the count and the place of its special cases.
- edge case
- An input or a target behavior that the general code does not handle correctly.
- edge-case class
- A group of edge cases with one cause and one signature.
- edge-case tail
- The rare edge-case classes and the classes that the team did not see yet.
- unseen share
- The estimated probability that the next failure belongs to a class that the log does not contain.
- discovery curve
- A plot of the count of distinct edge-case classes against the count of attempts.
- deliberate case
- An edge case that a target makes on purpose to break or to mark a collector.
how it works
The work has two outputs: a special-case inventory and a tail report. The inventory counts what the team built. The tail report estimates what the team did not see yet.
the inventory
For each general component, the inventory counts three kinds of special cases:
| kind | where it lives | what the inventory counts |
|---|---|---|
| Switch arm | Code | Each case that names a target or a target class |
| Data row | Data file | Each row of a data file that the component reads |
| override | Override table | Each entry that changes one value for one scope |
For example, a date reader has two switch arms, three format rows, and no overrides. The two arms ask about a target name. Thus they are candidates to move into data.
Count again at each release, and add one row for each component to a time series. One count shows little. The series shows which component grows.
classes of failures
An edge case is an input that the general code does not handle correctly. Give each failure an edge-case class: one cause and one signature. For example, "the price has a comma as the decimal mark" is one class, also when it occurs on ten targets.
The rare classes, and the classes that the log does not contain yet, are the edge-case tail. The estimators below use the counts of the rare classes:
N: the count of failures in the log;S: the count of distinct classes;f1: the count of classes with one failure;f2: the count of classes with two failures.
two public estimators
The Good-Turing estimate gives the probability that the next item belongs to a class that the sample does not contain. It is the count of classes with one item, divided by the count of all items Wikipedia, source 1. Alan Turing and I. J. Good developed it, and Good published it in 1953.
Chao published a second estimator in 1984. The SPECIES package of R gives its function the name "Lower-bound estimator for species richness" R Project (SPECIES package), source 2.
the forms of the estimators
The unseen share is f1 / N. The Chao1 bound is S + f1 * f1 / (2 * f2). When no class has two failures, the form is S + f1 * (f1 - 1) / 2.
For example, a log has 50 failures in 20 classes. Ten classes have one failure, and five classes have two. The unseen share is 10 / 50 = 0.2. The Chao1 bound is 20 + 100 / 10 = 30 classes.
The estimators come from the count of species in ecology. Their use on the failure classes of a collector is my model. In the mean, Chao1 is not more than the true count. In one log, it can be under or over the true count.
the discovery curve
A discovery curve plots the distinct classes against the attempts. For example, a curve that still rises at the end of the log shows that new classes still arrive. Thus the tail still grows. A curve that becomes flat shows that the log covers most of the classes that occur.
The team compares these results with its stop value: the unseen share under which more work on the tail stops. The stop value is a decision of the team, and this course gives no value for it.
deliberate classes
A deliberate case is a case that a target makes on purpose. For example, a target shows a decoy price only to a collector. Such a target can make new classes at any time. This breaks the assumption of a stable set of classes.
Count deliberate classes separately, and keep them out of the estimators. The decision on a deliberate case is in Triage a new edge case.
the two sides
| automation side | detection side |
|---|---|
| The count of special cases grows fast in the first period. Later, the work moves from new cases to corrections of cases that exist. An unseen share that does not go down means that the tail still grows. The estimators count only accidental classes, because a deliberate change breaks the assumption of a stable set of classes. | A detection rule set grows with exceptions in the same way. A detection team can count its exceptions for each rule and for each customer. It can also estimate the share of traffic that no rule class covers. |
design decisions
| option | gain | cost | when it is correct |
|---|---|---|---|
| Count special cases by hand | No tool to write | The count is old after the next release. | For a first look at a small system |
| An inventory script at each release | A time series for each component | The script must know each place of a special case. | When the team releases often |
| Good-Turing unseen share | One number from two counts | It says nothing about the count of missing classes. | To decide if the next failure is likely new |
| Chao1 bound | An estimate of the least count of classes | It is wide when few classes have two failures. | To estimate the size of the tail |
| Separate count of deliberate classes | The estimators stay valid. | The team must decide which classes are deliberate. | Always, when a target can change on purpose |
diagram
The first diagram shows the loop of the module for one edge case. The second diagram shows the steps of the tail report.
the edge-case loop: from one run to the retirement of a special case
the tail report: from the failure log to a comparison with the stop value
code example
The first example computes the tail report from a log file. Each line of the file has a class and a kind: accidental or deliberate. The data is synthetic.
Compute the unseen share, the Chao1 bound with the form for no doubletons, and a discovery curve from a classified failure log.
// Tail report from a classified failure log. All data is synthetic.
// Each line: class,kind (kind is accidental or deliberate).
import { readFileSync } from "node:fs";
const sample = [
"comma-decimal,accidental", "day-first-date,accidental", "comma-decimal,accidental",
"decoy-price,deliberate", "price-in-span,accidental", "comma-decimal,accidental",
"empty-list,accidental", "day-first-date,accidental", "title-in-image,accidental",
"comma-decimal,accidental", "price-in-span,accidental", "day-first-date,accidental",
"two-prices,accidental", "decoy-price,deliberate", "empty-list,accidental",
"comma-decimal,accidental", "price-in-span,accidental", "second-language,accidental",
"day-first-date,accidental", "currency-after,accidental", "lazy-images,accidental",
"empty-list,accidental",
];
function tailReport(classes: string[]) {
const counts = new Map<string, number>();
for (const c of classes) counts.set(c, (counts.get(c) ?? 0) + 1);
const n = classes.length;
const s = counts.size;
const values = [...counts.values()];
const f1 = values.filter((v) => v === 1).length;
const f2 = values.filter((v) => v === 2).length;
const unseen = n > 0 ? f1 / n : 0;
const chao1 = f2 > 0 ? s + (f1 * f1) / (2 * f2) : s + (f1 * (f1 - 1)) / 2;
return { n, s, f1, f2, unseen, chao1 };
}
function discoveryCurve(classes: string[], step: number): [number, number][] {
const seen = new Set<string>();
const points: [number, number][] = [];
classes.forEach((c, i) => {
seen.add(c);
if ((i + 1) % step === 0) points.push([i + 1, seen.size]);
});
return points;
}
const path = process.argv[2];
const lines = path ? readFileSync(path, "utf8").split("\n") : sample;
const accidental: string[] = [];
const deliberate = new Map<string, number>();
for (const line of lines) {
if (!line.trim()) continue;
const [cls, kind] = line.trim().split(",");
if (kind === "deliberate") deliberate.set(cls, (deliberate.get(cls) ?? 0) + 1);
else accidental.push(cls);
}
const r = tailReport(accidental);
const deliberateFailures = [...deliberate.values()].reduce((a, b) => a + b, 0);
console.log(`failures ${r.n}, classes ${r.s}, f1 ${r.f1}, f2 ${r.f2}`);
console.log(`unseen share ${r.unseen.toFixed(3)}, chao1 ${r.chao1.toFixed(1)}`);
console.log(`deliberate classes ${deliberate.size}, failures ${deliberateFailures}`);
for (const [attempt, seen] of discoveryCurve(accidental, 5)) console.log(`curve ${attempt} ${seen}`);output
The four counts, the unseen share, the Chao1 bound, the deliberate counts, and the discovery curve of the synthetic log.
failures 20, classes 9, f1 5, f2 0
unseen share 0.250, chao1 19.0
deliberate classes 1, failures 2
curve 5 3
curve 10 5
curve 15 6
curve 20 9With no file argument, the code uses its own synthetic log. No class has two failures, so the code uses the second form of Chao1. The curve still rises at the end.
The second example counts the special cases of two synthetic components and adds one row for each component to a time series. The component names are synthetic.
Count the switch arms, data rows, and override entries of each component, and add one row to a time series at a release.
// Special-case inventory of general components. All data is synthetic.
type Component = {
name: string;
source: string; // the code text of the component
dataRows: unknown[]; // the rows of its data file
overrides: Record<string, unknown>; // its override entries
};
type Row = { release: string; component: string; switchArms: number; dataRows: number; overrides: number };
// A switch arm is a line that starts with "case". Each arm lives in code.
const countArms = (src: string): number => (src.match(/^\s*case\s/gm) ?? []).length;
function inventory(release: string, parts: Component[]): Row[] {
return parts.map((p) => ({
release,
component: p.name,
switchArms: countArms(p.source),
dataRows: p.dataRows.length,
overrides: Object.keys(p.overrides).length,
}));
}
const parts: Component[] = [
{
name: "date-reader",
source: ["switch (target) {", ' case "target-a": return dayFirst(text);', ' case "target-b": return monthFirst(text);', " default: return iso(text);", "}"].join("\n"),
dataRows: [{ format: "dd.mm.yyyy" }, { format: "mm/dd/yyyy" }, { format: "yyyy-mm-dd" }],
overrides: {},
},
{
name: "price-reader",
source: "return readPrice(text, rules.forClass(targetClass));",
dataRows: [{ sign: "before" }, { sign: "after" }],
overrides: { "target-c": { decimalMark: "," }, "target-d": { decimalMark: "," } },
},
];
// The time series holds one row for each component at each release.
const series: Row[] = [
{ release: "r1", component: "date-reader", switchArms: 1, dataRows: 2, overrides: 0 },
{ release: "r1", component: "price-reader", switchArms: 0, dataRows: 2, overrides: 1 },
];
const release = process.argv[2] ?? "r2";
series.push(...inventory(release, parts));
console.log("release,component,switch_arms,data_rows,overrides");
for (const r of series) console.log(`${r.release},${r.component},${r.switchArms},${r.dataRows},${r.overrides}`);
for (const r of series.filter((x) => x.release === release)) {
const before = series.find((x) => x.release !== release && x.component === r.component);
const growth = before ? r.switchArms + r.dataRows + r.overrides - (before.switchArms + before.dataRows + before.overrides) : 0;
console.log(`${r.component}: code ${r.switchArms}, data ${r.dataRows}, overrides ${r.overrides}, growth ${growth}`);
}output
The time series as CSV, and the place and growth of the special cases of each component.
release,component,switch_arms,data_rows,overrides
r1,date-reader,1,2,0
r1,price-reader,0,2,1
r2,date-reader,2,3,0
r2,price-reader,0,2,2
date-reader: code 2, data 3, overrides 0, growth 2
price-reader: code 0, data 2, overrides 2, growth 1The output shows that the date reader grows in code, with arms that name targets. The price reader grows only in its override table.
data
The first three charts come from the code history of the benchmark platform, my test system. The history is short.
special cases in four components of general code, by period
status of this figure: documented in my test system
source: Benchmark platform, code (my test system): special cases of four components, three periods; date 2026-11-16; sample of 43; condition: Each value is the count at the end of a period. The n is the sum of the changes after the first build of the four components.
data table
| series | period | items |
|---|---|---|
| verdict rule files | early | 17 |
| verdict rule files | middle | 25 |
| verdict rule files | late | 25 |
| oracle definitions | early | 9 |
| oracle definitions | middle | 14 |
| oracle definitions | late | 17 |
| proxy dialect arms | early | 3 |
| proxy dialect arms | middle | 4 |
| proxy dialect arms | late | 5 |
| availability patterns | early | 8 |
| availability patterns | middle | 12 |
| availability patterns | late | 12 |
The first chart shows that the special cases of each component grow fast in the first period and then slow down.
kind of each change by component, classified by two agents
status of this figure: measured by me
- new case: verdict rules 14; oracle definitions 6; block signatures 3; proxy dialects 4; availability patterns 2; tool status overrides 3; verdict rule language 3
- correction: verdict rules 12; oracle definitions 3; block signatures 4; proxy dialects 2; availability patterns 1; tool status overrides 7; verdict rule language 0
- general refactor: verdict rules 1; oracle definitions 0; block signatures 0; proxy dialects 1; availability patterns 0; tool status overrides 0; verdict rule language 0
- other or the passes differ: verdict rules 3; oracle definitions 1; block signatures 0; proxy dialects 0; availability patterns 0; tool status overrides 1; verdict rule language 2
source: Benchmark platform, code (my test system): changes classified by two agents; date 2026-11-15; sample of 73; condition: The passes agree on 93.2% of the changes (kappa 0.88). No person checked the labels. Four components have fewer than 10 changes: read counts, not shares.
data table
| series | component | changes |
|---|---|---|
| new case | verdict rules | 14 |
| new case | oracle definitions | 6 |
| new case | block signatures | 3 |
| new case | proxy dialects | 4 |
| new case | availability patterns | 2 |
| new case | tool status overrides | 3 |
| new case | verdict rule language | 3 |
| correction | verdict rules | 12 |
| correction | oracle definitions | 3 |
| correction | block signatures | 4 |
| correction | proxy dialects | 2 |
| correction | availability patterns | 1 |
| correction | tool status overrides | 7 |
| correction | verdict rule language | 0 |
| general refactor | verdict rules | 1 |
| general refactor | oracle definitions | 0 |
| general refactor | block signatures | 0 |
| general refactor | proxy dialects | 1 |
| general refactor | availability patterns | 0 |
| general refactor | tool status overrides | 0 |
| general refactor | verdict rule language | 0 |
| other or the passes differ | verdict rules | 3 |
| other or the passes differ | oracle definitions | 1 |
| other or the passes differ | block signatures | 0 |
| other or the passes differ | proxy dialects | 0 |
| other or the passes differ | availability patterns | 0 |
| other or the passes differ | tool status overrides | 1 |
| other or the passes differ | verdict rule language | 2 |
The second chart shows the kind of each change for each component: a new case, a correction of a case, or a general change. Read it as counts.
share of changes that corrected an earlier case, verdict rule files
status of this figure: estimate, not measured
source: Benchmark platform, code (my test system): changes of the verdict rule files, manual reading; date 2026-11-16; condition: Changes after the first build. Estimate from a manual reading of each change, not a measured rate.
data table
| period | % |
|---|---|
| early period | 21 |
| late period | 63 |
The third chart shows that, for one component, the work moves from new cases to corrections of earlier cases.
classes seen and the chao1 estimate against attempts, one lab log
status of this figure: simulated
source: Lab result of this course: one simulated failure log of 150 classes, 2000 attempts; date 2026-11-13; sample of 594; condition: Zipf population, failure probability 0.3, one seed. The n is the failures at 2000 attempts. Teaching values for one host. The later part of the log is not in the chart.
data table
| series | attempts | classes |
|---|---|---|
| classes seen | 100 | 17 |
| classes seen | 200 | 32 |
| classes seen | 300 | 39 |
| classes seen | 400 | 48 |
| classes seen | 500 | 52 |
| classes seen | 600 | 57 |
| classes seen | 700 | 62 |
| classes seen | 800 | 69 |
| classes seen | 900 | 71 |
| classes seen | 1000 | 73 |
| classes seen | 1100 | 76 |
| classes seen | 1200 | 79 |
| classes seen | 1300 | 82 |
| classes seen | 1400 | 86 |
| classes seen | 1500 | 90 |
| classes seen | 1600 | 92 |
| classes seen | 1700 | 93 |
| classes seen | 1800 | 93 |
| classes seen | 1900 | 94 |
| classes seen | 2000 | 95 |
| Chao1 estimate | 100 | 33.5 |
| Chao1 estimate | 200 | 59.1429 |
| Chao1 estimate | 300 | 99 |
| Chao1 estimate | 400 | 118.8571 |
| Chao1 estimate | 500 | 85.8333 |
| Chao1 estimate | 600 | 80.625 |
| Chao1 estimate | 700 | 107.0909 |
| Chao1 estimate | 800 | 132.9091 |
| Chao1 estimate | 900 | 114.1538 |
| Chao1 estimate | 1000 | 113.0714 |
| Chao1 estimate | 1100 | 128.5 |
| Chao1 estimate | 1200 | 130.2308 |
| Chao1 estimate | 1300 | 124 |
| Chao1 estimate | 1400 | 135.4 |
| Chao1 estimate | 1500 | 139.4 |
| Chao1 estimate | 1600 | 133.625 |
| Chao1 estimate | 1700 | 124.3158 |
| Chao1 estimate | 1800 | 116.619 |
| Chao1 estimate | 1900 | 117.25 |
| Chao1 estimate | 2000 | 121.1053 |
| Chao1 lower bound, 95% | 100 | 21.2574 |
| Chao1 lower bound, 95% | 200 | 41.0383 |
| Chao1 lower bound, 95% | 300 | 59.6857 |
| Chao1 lower bound, 95% | 400 | 75.3685 |
| Chao1 lower bound, 95% | 500 | 65.4303 |
| Chao1 lower bound, 95% | 600 | 66.3738 |
| Chao1 lower bound, 95% | 700 | 80.3596 |
| Chao1 lower bound, 95% | 800 | 96.3418 |
| Chao1 lower bound, 95% | 900 | 89.1574 |
| Chao1 lower bound, 95% | 1000 | 89.9502 |
| Chao1 lower bound, 95% | 1100 | 98.3218 |
| Chao1 lower bound, 95% | 1200 | 101.1158 |
| Chao1 lower bound, 95% | 1300 | 100.1765 |
| Chao1 lower bound, 95% | 1400 | 107.9104 |
| Chao1 lower bound, 95% | 1500 | 111.9104 |
| Chao1 lower bound, 95% | 1600 | 110.2449 |
| Chao1 lower bound, 95% | 1700 | 106.5754 |
| Chao1 lower bound, 95% | 1800 | 102.9138 |
| Chao1 lower bound, 95% | 1900 | 103.6345 |
| Chao1 lower bound, 95% | 2000 | 105.9478 |
| Chao1 upper bound, 95% | 100 | 80.9478 |
| Chao1 upper bound, 95% | 200 | 113.5123 |
| Chao1 upper bound, 95% | 300 | 213.0337 |
| Chao1 upper bound, 95% | 400 | 231.4496 |
| Chao1 upper bound, 95% | 500 | 137.2322 |
| Chao1 upper bound, 95% | 600 | 116.5425 |
| Chao1 upper bound, 95% | 700 | 172.7428 |
| Chao1 upper bound, 95% | 800 | 218.3818 |
| Chao1 upper bound, 95% | 900 | 173.5616 |
| Chao1 upper bound, 95% | 1000 | 167.7313 |
| Chao1 upper bound, 95% | 1100 | 199.4779 |
| Chao1 upper bound, 95% | 1200 | 197.6751 |
| Chao1 upper bound, 95% | 1300 | 179.0482 |
| Chao1 upper bound, 95% | 1400 | 197.3791 |
| Chao1 upper bound, 95% | 1500 | 201.3791 |
| Chao1 upper bound, 95% | 1600 | 186.9657 |
| Chao1 upper bound, 95% | 1700 | 165.2396 |
| Chao1 upper bound, 95% | 1800 | 149.2711 |
| Chao1 upper bound, 95% | 1900 | 150.1068 |
| Chao1 upper bound, 95% | 2000 | 157.2484 |
The fourth chart is a lab result of this course. The curve of the classes seen still rises at the end. In this log, the Chao1 estimate stays under the true count.
entries of a public list of supported sites and the entries marked broken
status of this figure: from a public source
source: Public list of supported sites of an open-source downloader (public source, https://github.com/yt-dlp/yt-dlp/blob/master/supportedsites.md, read 2026-11-12); date 2026-11-12; condition: Approximate counts from a fetch of the page on the read date. The page says that not all listed sites are guaranteed to work.
data table
| entries | entries |
|---|---|
| all entries | 1700 |
| marked as broken | 135 |
The fifth chart shows that a large public collection of special cases also carries a part that does not work at a given time.
failure modes
| problem | cause | correction |
|---|---|---|
| Nobody knows which component grows. | The team counts its special cases one time, or never. | Run the inventory at each release, and keep the time series. |
| A switch arm that names a target stays in code. | The inventory counts only data rows and overrides. | Count switch arms too, and mark each arm that asks about a target. |
| The unseen share looks low, but new failures still arrive. | The log mixes failures of one cause under many classes, or one class under many causes. | Give each class one cause and one signature. |
| The Chao1 bound is very wide. | Few classes have two failures. | Collect a longer log before you use the bound. |
| The estimators give a tail that grows with no end. | Deliberate classes are in the log. | Count deliberate classes separately, and keep them out of the estimators. |
check questions
List the three kinds of special cases that an inventory counts for each general component, and give the place of each kind.
sample answer
A switch arm lives in code. A data row lives in a data file that the component reads. An override lives in an override table and changes one value for one scope. The inventory counts each kind again at each release and adds one row for each component to a time series.
A failure log has 40 failures in 15 classes. Eight classes have one failure, and four classes have two failures. What are the unseen share and the Chao1 bound?
- 0.2 and 23 classes
- 0.1 and 19 classes
- 0.2 and 19 classes
- 0.375 and 23 classes
answer
a. 0.2 and 23 classes
The unseen share is f1 / N = 8 / 40 = 0.2. The Chao1 bound is S + f1 * f1 / (2 * f2) = 15 + 64 / 8 = 23 classes.
The discovery curve of a collector still rises at the end of the log. What does it show?
- The log covers most of the classes that occur.
- New classes still arrive, and the tail still grows.
- The Chao1 bound is equal to the true count of classes.
answer
b. New classes still arrive, and the tail still grows.
A curve that still rises shows that new classes still arrive. Only a flat curve shows that the log covers most of the classes that occur.
Explain why deliberate classes stay out of the unseen share and the Chao1 bound.
sample answer
The estimators assume a stable set of classes. A target that makes cases on purpose can make new classes at any time. The estimators then give a tail that grows with no end. Thus the team counts deliberate classes separately.
exercise
exercise Do this task on paper with the synthetic failure log below. Each row is one class with its kind and its count of failures.
| class | kind | failures |
|---|---|---|
| comma as decimal mark | accidental | 6 |
| date with day first | accidental | 4 |
| price in a second element | accidental | 2 |
| empty list page | accidental | 2 |
| currency after the number | accidental | 1 |
| title in an image | accidental | 1 |
| two prices for one product | accidental | 1 |
| page in a second language | accidental | 1 |
| decoy price on each page | deliberate | 3 |
- Remove the deliberate class, and write its count separately.
- Write
N,S,f1, andf2for the other rows. - Compute the unseen share.
- Compute the Chao1 bound.
sample answer
The answer uses the nine rows of the task.
The deliberate class has 3 failures, and it goes to a separate count. The other rows give N = 18, S = 8, f1 = 4, and f2 = 2.
The unseen share is 4 / 18, about 0.22. Thus about one in five of the next failures can belong to a new class. The Chao1 bound is 8 + 16 / 4 = 12 classes. Thus the log can miss four or more classes.
sources
- Good-Turing frequency estimation, Wikipedia, article, read 2026-11-22
- chao1984: Lower-bound estimator for species richness, R Project (SPECIES package), documentation, read 2026-11-22
- My model of the special-case inventory, the tail report, and the use of the estimators on failure classes, 2026-11 (not public).