Artifact 4590f2f5c84740c1b53fe4f802552352ae4aa1258e820dd7394052bb2b1763c9:
- File
internal/search/index.go
— part of check-in
[670ca0e75f]
at
2026-07-19 16:00:09
on branch trunk
— fix: separate blob existence from blob availability (#43) (#52)
* fix: separate blob existence from blob availability (#43)
A phantom is a real blob row with size = -1 and NULL content, so blob.Exists — "SELECT rid FROM blob WHERE uuid=?" — answers true for it. Every guard built on Exists therefore admitted artifacts whose content cannot be read, and content.Expand aborted with "rid N is a phantom".
Canonical Fossil keeps two predicates and never substitutes one for the other: rid_from_uuid (existence, xfer.c:70) and content_is_available (availability, content.c:163). The latter is transitive — it follows delta.srcid up the chain, so a delta whose base is a phantom is unavailable even though every row in the chain exists.
Add content.IsAvailable / content.AvailableByUUID, ported from content_is_available including the chain walk, with an explicit depth bound so a cyclic delta table terminates instead of hanging. The walk lives in exactly one place; no caller touches the delta table.
blob.Exists keeps its behavior and gains a doc comment stating that it answers existence only, returns true for phantoms, and naming the availability predicate as the alternative.
manifest.blobPresent is deleted. It was a local patch at one call site that tested size >= 0 without the chain walk, so it still reported a delta-on-phantom as present — the same class of bug it was written to work around.
Every blob.Exists call site was classified as existence or availability and converted where it guards a content read. Sites that want rid identity — plink/mlink/tagxref population, cluster phantom creation, private-status bookkeeping, the post-store sanity check in handleFile, and phantom filling in storeReceivedFile — keep existence, since delta-before-base is a normal steady state during clone.
Verified against a loopback fossil 2.28 server: the 23-artifact repro that previously failed roughly half the time now clones 6 of 6.
* fix: IsAvailable must fail closed when the delta lookup errors
Only sql.ErrNoRows means the chain is grounded. Any other failure — cancelled context, closed connection, NULL srcid — says nothing about groundedness, so returning true there claims readable content that was never verified. That is the same class of bug this predicate exists to prevent, and it runs on data arriving over the wire during clone.
The blob-size lookup above already fails closed on the same pattern; this restores the symmetry. (user: [email protected], size: 2919) [annotate] [blame] [check-ins using]
package search import ( "bytes" "fmt" "strconv" libfossil "github.com/danmestas/libfossil/internal/fsltype" "github.com/danmestas/libfossil/internal/content" "github.com/danmestas/libfossil/db" "github.com/danmestas/libfossil/internal/manifest" "github.com/danmestas/libfossil/internal/repo" ) // maxBinaryProbe is the number of bytes checked for null bytes to detect binary files. const maxBinaryProbe = 8192 // isBinary returns true if data contains a null byte in the first maxBinaryProbe bytes. func isBinary(data []byte) bool { if data == nil { panic("isBinary: nil data") } probe := data if len(probe) > maxBinaryProbe { probe = probe[:maxBinaryProbe] } return bytes.ContainsRune(probe, 0) } // RebuildIndex walks the trunk tip manifest, expands blob content, // skips binaries and phantoms, and populates fts_content. // No-ops if already current. // // Panics if idx is nil (TigerStyle precondition). func (idx *Index) RebuildIndex() error { if idx == nil { panic("search.RebuildIndex: nil *Index") } d := idx.repo.DB() tip, err := trunkTip(d) if err != nil { return fmt.Errorf("search.RebuildIndex: %w", err) } if tip == 0 { return nil // empty repo, nothing to index } current, err := indexedRID(d) if err != nil { return fmt.Errorf("search.RebuildIndex: %w", err) } if tip == current { return nil // already up to date } if err := rebuildPopulate(idx.repo, d, tip); err != nil { return err } // Postcondition: verify the meta update took hold. stored, err := indexedRID(d) if err != nil { return fmt.Errorf("search.RebuildIndex: verify meta: %w", err) } if stored != tip { panic("search.RebuildIndex: postcondition: indexed_rid != tip after update") } return nil } // rebuildPopulate clears fts_content, walks the manifest at tip, // expands each text blob, and inserts into the FTS index. func rebuildPopulate(r *repo.Repo, d db.Querier, tip libfossil.FslID) error { if _, err := d.Exec("DELETE FROM fts_content"); err != nil { return fmt.Errorf("search.RebuildIndex: clear: %w", err) } files, err := manifest.ListFiles(r, tip) if err != nil { return fmt.Errorf("search.RebuildIndex: list files: %w", err) } for _, f := range files { rid, ok := content.AvailableByUUID(d, f.UUID) if !ok { continue // phantom — blob not yet received } data, err := content.Expand(d, rid) if err != nil { continue // phantom or corrupt — skip } if isBinary(data) { continue } if _, err := d.Exec( "INSERT INTO fts_content(path, content) VALUES(?, ?)", f.Name, string(data), ); err != nil { return fmt.Errorf("search.RebuildIndex: insert %s: %w", f.Name, err) } } if _, err := d.Exec( "INSERT OR REPLACE INTO fts_meta(key, value) VALUES('indexed_rid', ?)", strconv.FormatInt(int64(tip), 10), ); err != nil { return fmt.Errorf("search.RebuildIndex: update meta: %w", err) } return nil }