Artifact 0901d9becd678a37b5dccb10cc10d79245a5d9c2391a23284e11db341a66df67:
- File
internal/sync/horizon_deepen.go
— part of check-in
[c11b3055fb]
at
2026-10-22 17:36:26
on branch feat/horizon-cones
— feat(horizon): deepen as a verb, discarding the seed for the old C
Section 5.8 makes deepen the universal remedy of the draft: name a C' that is an ancestor of C and synchronize at it. sync.Deepen finds C' as zit's deepenPoint does, the target itself when it is C's ancestor, else a common ancestor over the P cards the store holds or asked of the origin with the merge-base query; it refuses rather than move C off its own ancestry when none can be established. Repo.DeepenHorizon and 'repo cone deepen' expose it, and the crossing diagnostic now names that command.
Moving a structural horizon through FetchCone now discards the held seed before the transfer, so the request offers none and the seed for C, applied at a later date, cannot shadow propagation from the seed for C'. A transfer that fails puts the old cone and seed back, as zit's deepen does: a store left structural without a seed derives no branch state for K. (user: [email protected], size: 4135) [annotate] [blame] [check-ins using] [more...]
package sync import ( "context" "database/sql" "errors" "fmt" "strings" "github.com/danmestas/go-libfossil/internal/cone" "github.com/danmestas/go-libfossil/internal/content" libfossil "github.com/danmestas/go-libfossil/internal/fsltype" "github.com/danmestas/go-libfossil/internal/hash" "github.com/danmestas/go-libfossil/internal/merge" "github.com/danmestas/go-libfossil/internal/repo" ) // ErrNoDeepenPoint is returned by Deepen when no common ancestor of the // store's C and the target can be established, here or by the origin: no // ancestor of C reaches the target, so no deepen does. var ErrNoDeepenPoint = errors.New("horizon: no common ancestor of the horizon and the target to deepen to") // Deepen moves the store's horizon toward target, the full hash of a // check-in the store needs history down to (draft-fossil-horizon-00 §5.8), // and synchronizes at the new C' through peer. C' is an ancestor of C, so K // only grows: target itself when it is one, else a common ancestor of C and // target, found over the graph the store holds or asked of the origin // (§6.2.4). The rule, the depth and the path members stay. FetchCone does // the rest: a structural store discards the seed for C before it takes the // one for C', and a failed transfer puts both back. func Deepen(ctx context.Context, r *repo.Repo, peer Transport, target string, opts SyncOpts) (*SyncResult, error) { if ctx == nil || r == nil || peer == nil { panic("sync.Deepen: nil context, repo or peer") } target = strings.ToLower(target) if !hash.IsValidHash(target) { return nil, fmt.Errorf("horizon: deepen target %q is not a full artifact hash", target) } sc, err := content.StoreCone(r.DB()) if err != nil { return nil, err } h, ok := horizonOf(sc) if !ok { return nil, errors.New("horizon: the store has no horizon to deepen") } point, err := deepenPoint(ctx, r, peer, h.Hash(), target, opts) if err != nil { return nil, err } nh, err := cone.NewHorizon(point, h.Rule(), h.Depth()) if err != nil { return nil, err } next, err := cone.NewWithHorizon(sc.Include(), sc.Exclude(), nh) if err != nil { return nil, err } return FetchCone(ctx, r, peer, next, opts) } // deepenPoint is where a deepen that must reach target moves C (zit's // deepenPoint, src/cmd/sync.zig): a common ancestor of the two, which is // target itself when target is C's ancestor and C when target descends from // it. The store's own graph answers when P cards it holds join the two, the // boundary's included; otherwise the origin is asked. func deepenPoint(ctx context.Context, r *repo.Repo, peer Transport, c, target string, opts SyncOpts) (string, error) { if c == target { return c, nil } rc, okC, err := blobRID(r, c) if err != nil { return "", err } rt, okT, err := blobRID(r, target) if err != nil { return "", err } if okC && okT { base, err := merge.FindCommonAncestorWithin(ctx, r, rc, rt, mergeBaseNodes) switch { case err == nil: var uuid string if err := r.DB().QueryRow("SELECT uuid FROM blob WHERE rid=?", base).Scan(&uuid); err != nil { return "", fmt.Errorf("horizon: deepen point: %w", err) } return uuid, nil case !errors.Is(err, merge.ErrNoCommonAncestor) && !errors.Is(err, merge.ErrBound): return "", err } } opts.Pull, opts.Push = true, false opts.MergeBaseOf = [2]string{c, target} res, err := Sync(ctx, r, peer, opts) if err != nil { return "", err } if !res.MergeBaseAnswered { return "", fmt.Errorf("%w: %s and %s; the origin did not answer", ErrNoDeepenPoint, c, target) } if res.MergeBase == "" { return "", fmt.Errorf("%w: %s and %s; the origin names none", ErrNoDeepenPoint, c, target) } return res.MergeBase, nil } // blobRID is the rid of the artifact named uuid, held or phantom. func blobRID(r *repo.Repo, uuid string) (libfossil.FslID, bool, error) { var rid libfossil.FslID err := r.DB().QueryRow("SELECT rid FROM blob WHERE uuid=?", uuid).Scan(&rid) if errors.Is(err, sql.ErrNoRows) { return 0, false, nil } if err != nil { return 0, false, fmt.Errorf("horizon: deepen point: %w", err) } return rid, true, nil }