Skip to content

Commit f8defeb

Browse files
donislawdevclaude
andauthored
A damaged file is judged in every format, and no longer claims its label (#227)
* The structural checker refuses broken archives instead of stopping on them An archive whose first bytes are zeroed passed the ZIP check, because it read the end records and never followed them to the local headers. It now walks the central directory and checks that every entry leads to a local header with the same name, with its data before the directory. Office packages go through the same walk first, so bytes after their end record are refused too. A PNG cut inside its last checksum, a PNG that is only its signature and an Office part that cannot be read stopped the check with an exception. Each is now a refusal with its reason. An exception left anywhere is reported as a fault in the checker and never counted as a refusal of the file. A verdict is raised rather than printed, so one process can judge many files: strict.py --batch reads one request per line, and oracle.StrictBatch hands it a list. Two guards hold this: real files from this tool, broken nine ways, must be refused for the right reason, and planted checks that fall over must be answered as a crash. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Every damage is judged in every format, with the checker as the witness A damage is offered for a format only when a judge refuses the result. That was a sweep taken once, over seven formats in the test suite. A guard now writes, through the real engine, a healthy and a damaged file for every format and every registered damage, at its defaults and with each parameter at both ends, at the smallest size the pair can be given and at 20 kB, and judges them all in one run of the structural checker. A damaged file that is accepted is red, and so is a checker that falls over instead of answering. A format with no structural check is red too, because the offer would reach it by default. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * A damaged file no longer claims its label The label is written by the generator and the damage comes after it, so it can overwrite it: zeroing the head of a text file took the label with it while the manifest still said label_embedded: true. A damaged file now says false, at the top of its entry and in its properties, with a label_not_guaranteed note saying why. A guard holds this for every format, beside a healthy file that does claim its label. The witness guard now writes its files with the label, as both surfaces do by default. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * The witness checker runs under its caller's context, and the changelog says what a user notices StrictBatch takes the caller's context, so a guard that is stopped stops the checker with it. The changelog lists the one change a user of the tool sees: a damaged file no longer claims to carry its label. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Three more ways the structural checker stopped on broken bytes are refusals Random damage to healthy files of every format found three: a cross reference offset that is not a number stopped the PDF check, a label heading never closed stopped the HTML check, and an Office part packed with a compression method nobody knows stopped the package check. Each is now refused with its reason, and the guard of broken bytes holds all three. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * A request the checker cannot act on is one error, and a stopped caller stops the checker The batch mode stopped altogether on a line of JSON nested past what the decoder goes, and on a path holding a NUL. Each is now answered ERROR in its place, and the requests after it are still answered. Two guards hold the answers a batch gives to what it cannot act on, and that a caller whose context is stopped stops the checker. The sentence about a batch whose answers do not match its questions no longer writes file(s). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7716cf3 commit f8defeb

12 files changed

Lines changed: 1217 additions & 101 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ because it turns other people's test suites red.
9292

9393
### Fixed
9494

95+
- **A damaged file no longer claims to carry its label.** Its manifest entry
96+
said `label_embedded: true`, although the damage comes after the label is
97+
written and can overwrite it. On a text file, `zero-head` zeroes the start of
98+
the label itself. A damaged file now says `false`, in its `properties` too,
99+
with a `label_not_guaranteed` note saying why. An undamaged file is
100+
described as before, and `manifest_version` does not change.
95101
- **A recipe asking for more than a million files in all is refused before
96102
it takes the memory.** Targets were held to a million each and the run only
97103
once all their sizes were laid out, so `tfg validate` took 2.5 GB to refuse

‎internal/damage/damage.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
// What this package does NOT do is decide whether a damaged file is worth
2020
// writing. That is the witness rule - a pair with no judge able to refuse the
2121
// result is a pair this tool does not offer - and it belongs with the oracles,
22-
// because it is a measurement rather than a declaration.
22+
// because it is a measurement rather than a declaration. The guard
23+
// TestEveryDamageIsRefusedByTheStructuralCheckInEveryFormat takes that
24+
// measurement for every format and every damage registered here.
2325
package damage
2426

2527
import (

‎internal/engine/engine.go‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"errors"
88
"fmt"
99
"io/fs"
10+
"maps"
1011
"os"
1112
"path/filepath"
1213
"strings"
@@ -728,6 +729,21 @@ func (res *Result) entryFor(f PlannedFile, sha string, materialized bool, failur
728729
}
729730

730731
label, _ := f.Plan.Properties[format.PropertyLabelEmbedded].(bool)
732+
props := f.Plan.Properties
733+
if label && f.Damaged() {
734+
// The generator wrote the label and a damage came after it. Where the
735+
// label sits differs by format - the first line of a text, pixels in
736+
// a picture, a part inside a package - so whether a damage reached it
737+
// cannot be said once for all of them. Measured 2026-11-09: zero-head
738+
// on txt overwrote the label it sat beside, and the manifest still
739+
// claimed it. So a damaged file never claims its label, and the note
740+
// says why (docs/DAMAGE-ETAP-2-2026-11-09.md section 4.7).
741+
label = false
742+
props = maps.Clone(props)
743+
props[format.PropertyLabelEmbedded] = false
744+
notes = append(notes, res.note("label_not_guaranteed",
745+
core.Says("engine.LabelNotGuaranteed", "The label was written before the file was damaged, so the damage may have overwritten it.")))
746+
}
731747

732748
e := manifest.File{
733749
ID: f.ID,
@@ -741,7 +757,7 @@ func (res *Result) entryFor(f PlannedFile, sha string, materialized bool, failur
741757
Seed: core.SeedLabel(f.Seed),
742758
Generator: manifest.GeneratorRef{Name: f.Desc.ID, Version: f.Desc.GeneratorVersion},
743759
Determinism: string(f.Plan.Determinism),
744-
Properties: f.Plan.Properties,
760+
Properties: props,
745761
LabelEmbedded: label,
746762
Damage: damageFor(f),
747763
Notes: notes,

‎internal/guard/damagelabel_test.go‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package guard
2+
3+
import (
4+
"path/filepath"
5+
"slices"
6+
"testing"
7+
8+
"github.com/donislawdev/TestingFilesGenerator/internal/damage"
9+
"github.com/donislawdev/TestingFilesGenerator/internal/engine"
10+
"github.com/donislawdev/TestingFilesGenerator/internal/format"
11+
"github.com/donislawdev/TestingFilesGenerator/internal/manifest"
12+
)
13+
14+
// labelNotGuaranteed is the note a damaged file carries in place of a claim
15+
// to its label.
16+
const labelNotGuaranteed = "label_not_guaranteed"
17+
18+
// TestADamagedFileNeverClaimsItsLabel holds the manifest to what it can know
19+
// about a label after a damage.
20+
//
21+
// Measured 2026-11-09: zero-head on txt overwrote the label at the start of
22+
// the text, and the manifest still said label_embedded: true. Where a label
23+
// sits differs by format, so the rule is the same for all of them. A damaged
24+
// file says false, in both places the manifest says it, and a note says why.
25+
//
26+
// Every format, each beside a healthy file of the same format and size. The
27+
// healthy one is the control: a format that never claims its label would pass
28+
// this guard without the rule being there, so the guard also counts the
29+
// formats where the claim was really taken away.
30+
func TestADamagedFileNeverClaimsItsLabel(t *testing.T) {
31+
dir := t.TempDir()
32+
chain := damage.Chain{{ID: damage.ZeroHead}}
33+
34+
withdrawn := 0
35+
for _, f := range format.All() {
36+
out := filepath.Join(dir, f.ID)
37+
targets := []engine.Target{
38+
{ID: "good", Format: f.ID, Sizes: engine.Uniform(1, witnessSize), Label: true},
39+
{ID: "broken", Format: f.ID, Sizes: engine.Uniform(1, witnessSize), Damage: chain, Label: true},
40+
}
41+
opt := engine.Options{OutDir: out, Seed: 5, ManifestName: engine.DefaultManifestName}
42+
planned, err := engine.Plan(targets, opt)
43+
if err != nil {
44+
t.Fatalf("%s: planning: %v", f.ID, err)
45+
}
46+
res, err := engine.Run(t.Context(), planned, opt)
47+
if err != nil {
48+
t.Fatalf("%s: running: %v", f.ID, err)
49+
}
50+
entries := map[string]manifest.File{}
51+
for _, m := range res.Manifest.Files {
52+
entries[m.TargetID] = m
53+
}
54+
good, broken := entries["good"], entries["broken"]
55+
if good.Name == "" || broken.Name == "" {
56+
t.Fatalf("%s: the run did not write both files", f.ID)
57+
}
58+
59+
if broken.LabelEmbedded {
60+
t.Errorf("%s: a damaged file claims label_embedded: true.\n"+
61+
"Reason: the damage came after the label was written and may have overwritten it,\n"+
62+
"so the manifest cannot know the label is still there.", f.ID)
63+
}
64+
if claim, ok := broken.Properties[format.PropertyLabelEmbedded].(bool); ok && claim {
65+
t.Errorf("%s: a damaged file claims label_embedded: true in its properties, "+
66+
"while the field beside them says false", f.ID)
67+
}
68+
noted := slices.ContainsFunc(broken.Notes, func(n manifest.Note) bool { return n.Code == labelNotGuaranteed })
69+
if noted != good.LabelEmbedded {
70+
t.Errorf("%s: the healthy file claims its label: %t, and the damaged one carries the note %s: %t.\n"+
71+
"Reason: the note says why a label that was written is no longer claimed, so it\n"+
72+
"belongs exactly where a claim was taken away.", f.ID, good.LabelEmbedded, labelNotGuaranteed, noted)
73+
}
74+
if good.LabelEmbedded {
75+
withdrawn++
76+
}
77+
}
78+
if withdrawn == 0 {
79+
t.Fatal("no healthy file claimed its label, so this guard took nothing away and proved nothing")
80+
}
81+
t.Logf("the claim was taken away in %d format(s)", withdrawn)
82+
}

‎internal/guard/damagerefused_test.go‎

Lines changed: 3 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -13,53 +13,14 @@ import (
1313
"github.com/donislawdev/TestingFilesGenerator/internal/cli"
1414
"github.com/donislawdev/TestingFilesGenerator/internal/damage"
1515
"github.com/donislawdev/TestingFilesGenerator/internal/engine"
16-
"github.com/donislawdev/TestingFilesGenerator/internal/format"
1716
_ "github.com/donislawdev/TestingFilesGenerator/internal/format/all"
1817
"github.com/donislawdev/TestingFilesGenerator/internal/manifest"
19-
"github.com/donislawdev/TestingFilesGenerator/internal/oracle"
2018
"github.com/donislawdev/TestingFilesGenerator/internal/recipe"
2119
)
2220

23-
// A damaged file has to be REFUSED, which is the reverse of everything else in
24-
// this package.
25-
//
26-
// Every other oracle guard here asks "does a judge accept what we wrote". This
27-
// one asks the opposite, and it is the whole reason the damage axis is worth
28-
// having: a broken file no reader complains about is a file nobody can write a
29-
// test against, so it is a file this tool has no business producing.
30-
//
31-
// The formats are the text ones plus png, because those are the ones whose
32-
// judges run without an external tool being installed. That is a limit of what
33-
// can be asked on any machine rather than of the damage: the full sweep across
34-
// all twenty four lives in tools/probes/corruptwitness, which is where the
35-
// witness matrix is measured.
36-
func TestADamagedFileIsRefusedByAJudge(t *testing.T) {
37-
dir := t.TempDir()
38-
39-
for _, id := range []string{"txt", "md", "json", "xml", "csv", "html", "png"} {
40-
t.Run(id, func(t *testing.T) {
41-
good, broken := writeGoodAndBroken(t, dir, id)
42-
43-
// The control first. Without it this test passes for a build whose
44-
// judge refuses everything, which would be the loudest possible
45-
// way of proving nothing.
46-
if res := oracle.Strict(id, good); res.Available && res.Err != nil {
47-
t.Fatalf("the undamaged file was refused, so this row says nothing about damage: %v\n%s",
48-
res.Err, res.Output)
49-
}
50-
51-
res := oracle.Strict(id, broken)
52-
if !res.Available {
53-
t.Skipf("no structural check for %s on this machine", id)
54-
}
55-
if res.Err == nil {
56-
t.Errorf("the damaged file was ACCEPTED. A broken file every reader takes is one "+
57-
"nobody can write a test against, and the manifest calls it expected: reject.\n%s",
58-
res.Output)
59-
}
60-
})
61-
}
62-
}
21+
// Whether a damaged file is refused by a judge is asked of every format and
22+
// every damage in damagewitness_test.go, which replaced the seven formats
23+
// asked here until 2026-11-09.
6324

6425
// The manifest of a damaged run says what was broken and expects a rejection.
6526
//
@@ -157,26 +118,6 @@ type discard struct{}
157118

158119
func (discard) Write(p []byte) (int, error) { return len(p), nil }
159120

160-
// writeGoodAndBroken produces one file of this format twice, once whole and
161-
// once damaged, and returns both paths.
162-
func writeGoodAndBroken(t *testing.T, dir, id string) (good, broken string) {
163-
t.Helper()
164-
165-
d, err := format.Get(id)
166-
if err != nil {
167-
t.Fatal(err)
168-
}
169-
size := d.SmallestAccepted(format.Request{}) + 4096
170-
171-
goodDir := filepath.Join(dir, id+"-good")
172-
brokenDir := filepath.Join(dir, id+"-broken")
173-
goodMan := runOne(t, goodDir, id, size, nil)
174-
brokenMan := runOne(t, brokenDir, id, size, damage.Chain{{ID: damage.ZeroHead}})
175-
176-
return filepath.Join(goodDir, goodMan.Files[0].Name),
177-
filepath.Join(brokenDir, brokenMan.Files[0].Name)
178-
}
179-
180121
// runDamaged is one png of a comfortable size, with the chain given.
181122
func runDamaged(t *testing.T, dir, id string, chain damage.Chain) *manifest.Manifest {
182123
t.Helper()

0 commit comments

Comments
 (0)