skip to content

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 source

purpose

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

  1. List the general components of a system, and count the special cases of each component with their place.
  2. Compute the unseen share and the Chao1 bound from the class counts of a failure log.
  3. Read a discovery curve, and say if the tail still grows.
  4. 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:

kindwhere it liveswhat the inventory counts
Switch armCodeEach case that names a target or a target class
Data rowData fileEach row of a data file that the component reads
overrideOverride tableEach 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 sidedetection 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

optiongaincostwhen it is correct
Count special cases by handNo tool to writeThe count is old after the next release.For a first look at a small system
An inventory script at each releaseA time series for each componentThe script must know each place of a special case.When the team releases often
Good-Turing unseen shareOne number from two countsIt says nothing about the count of missing classes.To decide if the next failure is likely new
Chao1 boundAn estimate of the least count of classesIt is wide when few classes have two failures.To estimate the size of the tail
Separate count of deliberate classesThe 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

Each edge case goes around one loop, from the run to the retirement of its special case.source: My model of the edge-case loop

the tail report: from the failure log to a comparison with the stop value

The tail report uses only the class of each failure.source: My model of the tail report

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.

m53-x01-tail.tstypescript

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 9

With 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.

m53-x02-inventory.tstypescript

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 1

The 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

special cases in four components of general code, by period1725259141734581212earlymiddlelateitems1. verdict rule files2. oracle definitions3. proxy dialect arms4. availability patterns

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
special cases in four components of general code, by period
seriesperioditems
verdict rule filesearly17
verdict rule filesmiddle25
verdict rule fileslate25
oracle definitionsearly9
oracle definitionsmiddle14
oracle definitionslate17
proxy dialect armsearly3
proxy dialect armsmiddle4
proxy dialect armslate5
availability patternsearly8
availability patternsmiddle12
availability patternslate12
Special cases at the end of three periods of the history of one platform. The changes after the first build were 29, 9, 3, and 2, in the order of the lines.

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

kind of each change by component, classified by two agentsverdict rulesoracle definitionsblock signaturesproxy dialectsavailability patternstool status overridesverdict rule language1463433123473unit: changes1. new case2. correction3. general refactor4. other or the passes differ
  1. 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
  2. correction: verdict rules 12; oracle definitions 3; block signatures 4; proxy dialects 2; availability patterns 1; tool status overrides 7; verdict rule language 0
  3. 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
  4. 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
kind of each change by component, classified by two agents
seriescomponentchanges
new caseverdict rules14
new caseoracle definitions6
new caseblock signatures3
new caseproxy dialects4
new caseavailability patterns2
new casetool status overrides3
new caseverdict rule language3
correctionverdict rules12
correctionoracle definitions3
correctionblock signatures4
correctionproxy dialects2
correctionavailability patterns1
correctiontool status overrides7
correctionverdict rule language0
general refactorverdict rules1
general refactororacle definitions0
general refactorblock signatures0
general refactorproxy dialects1
general refactoravailability patterns0
general refactortool status overrides0
general refactorverdict rule language0
other or the passes differverdict rules3
other or the passes differoracle definitions1
other or the passes differblock signatures0
other or the passes differproxy dialects0
other or the passes differavailability patterns0
other or the passes differtool status overrides1
other or the passes differverdict rule language2
Each bar counts the changes to one component. Two agents classified each change, blind to each other. One segment holds the other changes and the changes where the two passes differ.

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

share of changes that corrected an earlier case, verdict rule filesearly period21%late period63%unit: %

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
share of changes that corrected an earlier case, verdict rule files
period%
early period21
late period63
Early period: 3 of 14 changes. Late period: 10 of 16 changes. The shares are approximate, and a manual reading gave the kind of each change.

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

classes seen and the chao1 estimate against attempts, one lab log179533.5121.105321.2574105.947880.9478157.2484ref 110020030040050060070080090010001100120013001400150016001700180019002000classes1. classes seen2. Chao1 estimate3. Chao1 lower bound, 95%4. Chao1 upper bound, 95%ref 1: true classes in the log 150

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
classes seen and the chao1 estimate against attempts, one lab log
seriesattemptsclasses
classes seen10017
classes seen20032
classes seen30039
classes seen40048
classes seen50052
classes seen60057
classes seen70062
classes seen80069
classes seen90071
classes seen100073
classes seen110076
classes seen120079
classes seen130082
classes seen140086
classes seen150090
classes seen160092
classes seen170093
classes seen180093
classes seen190094
classes seen200095
Chao1 estimate10033.5
Chao1 estimate20059.1429
Chao1 estimate30099
Chao1 estimate400118.8571
Chao1 estimate50085.8333
Chao1 estimate60080.625
Chao1 estimate700107.0909
Chao1 estimate800132.9091
Chao1 estimate900114.1538
Chao1 estimate1000113.0714
Chao1 estimate1100128.5
Chao1 estimate1200130.2308
Chao1 estimate1300124
Chao1 estimate1400135.4
Chao1 estimate1500139.4
Chao1 estimate1600133.625
Chao1 estimate1700124.3158
Chao1 estimate1800116.619
Chao1 estimate1900117.25
Chao1 estimate2000121.1053
Chao1 lower bound, 95%10021.2574
Chao1 lower bound, 95%20041.0383
Chao1 lower bound, 95%30059.6857
Chao1 lower bound, 95%40075.3685
Chao1 lower bound, 95%50065.4303
Chao1 lower bound, 95%60066.3738
Chao1 lower bound, 95%70080.3596
Chao1 lower bound, 95%80096.3418
Chao1 lower bound, 95%90089.1574
Chao1 lower bound, 95%100089.9502
Chao1 lower bound, 95%110098.3218
Chao1 lower bound, 95%1200101.1158
Chao1 lower bound, 95%1300100.1765
Chao1 lower bound, 95%1400107.9104
Chao1 lower bound, 95%1500111.9104
Chao1 lower bound, 95%1600110.2449
Chao1 lower bound, 95%1700106.5754
Chao1 lower bound, 95%1800102.9138
Chao1 lower bound, 95%1900103.6345
Chao1 lower bound, 95%2000105.9478
Chao1 upper bound, 95%10080.9478
Chao1 upper bound, 95%200113.5123
Chao1 upper bound, 95%300213.0337
Chao1 upper bound, 95%400231.4496
Chao1 upper bound, 95%500137.2322
Chao1 upper bound, 95%600116.5425
Chao1 upper bound, 95%700172.7428
Chao1 upper bound, 95%800218.3818
Chao1 upper bound, 95%900173.5616
Chao1 upper bound, 95%1000167.7313
Chao1 upper bound, 95%1100199.4779
Chao1 upper bound, 95%1200197.6751
Chao1 upper bound, 95%1300179.0482
Chao1 upper bound, 95%1400197.3791
Chao1 upper bound, 95%1500201.3791
Chao1 upper bound, 95%1600186.9657
Chao1 upper bound, 95%1700165.2396
Chao1 upper bound, 95%1800149.2711
Chao1 upper bound, 95%1900150.1068
Chao1 upper bound, 95%2000157.2484
One simulated failure log with 150 classes. The estimate and its 95% bounds use the log up to each attempt count. The reference line is the true class count.

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

entries of a public list of supported sites and the entries marked brokenall entries1700marked as broken135unit: entries

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 of a public list of supported sites and the entries marked broken
entriesentries
all entries1700
marked as broken135
A snapshot of the public list of supported sites of an open-source downloader. Both counts are approximate. About 8% of the entries carry the broken mark.

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

problemcausecorrection
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

  1. 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.

  2. 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?

    1. 0.2 and 23 classes
    2. 0.1 and 19 classes
    3. 0.2 and 19 classes
    4. 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.

  3. The discovery curve of a collector still rises at the end of the log. What does it show?

    1. The log covers most of the classes that occur.
    2. New classes still arrive, and the tail still grows.
    3. 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.

  4. 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.

classkindfailures
comma as decimal markaccidental6
date with day firstaccidental4
price in a second elementaccidental2
empty list pageaccidental2
currency after the numberaccidental1
title in an imageaccidental1
two prices for one productaccidental1
page in a second languageaccidental1
decoy price on each pagedeliberate3
  1. Remove the deliberate class, and write its count separately.
  2. Write N, S, f1, and f2 for the other rows.
  3. Compute the unseen share.
  4. 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

  1. Good-Turing frequency estimation, Wikipedia, article, read 2026-11-22
  2. chao1984: Lower-bound estimator for species richness, R Project (SPECIES package), documentation, read 2026-11-22
  3. My model of the special-case inventory, the tail report, and the use of the estimators on failure classes, 2026-11 (not public).