P4 take-in-carries: an unfinished mover's entry is read at its kept revision, else at the head's when the fingerprint is the same; place-unfinished reads it so too; a drop names the fingerprint and the revisions looked at

Builds: design 47 H2 H-A-608, H-A-594 (3)
This commit is contained in:
2026-11-25 19:21:55 +01:00
parent b96c3f1bd8
commit c2d2877fc4
4 changed files with 113 additions and 21 deletions
@@ -288,6 +288,13 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro
return half.Entries, err == nil
}
unfinished.Pipeline = func(key string) (seat.Pipeline, bool) { p, _, err := store.Get(key); return p, err == nil }
unfinished.Kept = func(key string) map[string]int64 {
kept, err := store.KeptRevisions(key)
if err != nil {
logger.Printf("the kept-revisions of %s are not read, so its movers' entries are read at the revision its key names: %v", key, err)
}
return kept
}
unfinished.Registered = func(module string) bool { _, ok := rv.Registrations.Registration(module); return ok }
unfinished.Add = func(from seat.Pipeline, group []seat.Mover) (string, bool) {
half, ok := rv.Catalog.View(from.Branch)
@@ -387,22 +387,40 @@ func (v Verbs) planUnfinished(key string) (handPlan, error) {
if err != nil || !Moves(from, module) {
return handPlan{}, fmt.Errorf("its pipeline %s is not read or moves %s no more", e.Pipeline, module)
}
var half Half
switch {
case s.Revision != nil:
if half, err = s.Revision(branch, e.Revision); err != nil {
return handPlan{}, fmt.Errorf("the revision %d of %s is not read: %w", e.Revision, branch, err)
}
case v.View != nil:
var ok bool
if half, ok = v.View(branch); !ok || half.Revision != e.Revision {
return handPlan{}, fmt.Errorf("the revision %d of %s is not held", e.Revision, branch)
}
default:
if s.Revision == nil && v.View == nil {
return handPlan{}, errors.New("the runner reads no catalog revision")
}
if entry, ok := half.Entries[module]; !ok || entry.Fp != e.Fingerprint || half.Source <= 0 {
return handPlan{}, fmt.Errorf("the revision %d of %s holds %s at %q, not its unfinished fingerprint %s", e.Revision, branch, module, entry.Fp, e.Fingerprint)
// The entry is read at its kept revision, else at the head's when the entry there has the same fingerprint (design 47 H2 H-A-608).
readAt := func(rev int64) (Half, error) {
if s.Revision != nil {
h, err := s.Revision(branch, rev)
if err != nil {
return Half{}, fmt.Errorf("the revision %d of %s is not read: %w", rev, branch, err)
}
return h, nil
}
if h, ok := v.View(branch); ok && h.Revision == rev {
return h, nil
}
return Half{}, fmt.Errorf("the revision %d of %s is not held", rev, branch)
}
kept := s.Unfinished.keptRevision(e, module)
half, rerr := readAt(kept)
entry, has := half.Entries[module]
if rerr != nil || !has || entry.Fp != e.Fingerprint || half.Source <= 0 {
looked := fmt.Sprintf("its kept revision %d holds it at %q", kept, entry.Fp)
if rerr != nil {
looked = rerr.Error()
}
head, hok := Half{}, false
if v.View != nil {
head, hok = v.View(branch)
}
he, hhas := head.Entries[module]
if !hok || !hhas || he.Fp != e.Fingerprint || head.Source <= 0 {
return handPlan{}, fmt.Errorf("%s at fingerprint %s is read at neither revision: %s; the head's revision %d of %s holds it at %q", module, e.Fingerprint, looked, head.Revision, branch, he.Fp)
}
half = head
}
group := v.closedGroup(from, module, half.Entries)
plan := handPlan{from: from, group: group, entries: half.Entries}
@@ -534,6 +534,36 @@ func TestTakenInStartsOnlyItsEntries(t *testing.T) {
if p, _, _ := mem.Get("main.14"); p.Outcome != seat.PipelineOutcomeRunning || p.Ended != nil {
t.Errorf("main.14 is %s ended %v, want the cancelled pipeline reopened", p.Outcome, p.Ended)
}
// design 47 H2 H-A-608 (AC-P4): a key whose kept revision is gone is read at the head's revision when the fingerprint is the same,
// by the hand verb as by the carry; a key whose kept revision is still read is read there, beyond the take-in's revision.
c, rev, _ = mem.Get("main.14")
c.Outcome, c.Ended = seat.PipelineOutcomeFailed, &end
if _, err := mem.Update("main.14", c, rev); err != nil {
t.Fatal(err)
}
u["main.batched"] = `{"fingerprint":"src2:bb","revision":9,"pipeline":"main.14","outcome":"failed"}`
if a, err = v.PlaceUnfinished("agent.dev", seat.PlaceUnfinishedInput{Key: "main.batched", Why: "kept revision gone"}); err != nil || len(a.Placed) != 1 {
t.Fatalf("place-unfinished of a key whose kept revision 9 is gone = %+v (%v), want it read at the head's revision and placed", a, err)
}
u["main.batched"] = `{"fingerprint":"src2:zz","revision":9,"pipeline":"main.14","outcome":"failed"}`
if _, err = v.PlaceUnfinished("agent.dev", seat.PlaceUnfinishedInput{Key: "main.batched", Why: "neither"}); err == nil || !strings.Contains(err.Error(), "src2:zz") || !strings.Contains(err.Error(), "revision 9") || !strings.Contains(err.Error(), "revision 14") {
t.Errorf("place-unfinished of a key found at neither revision = %v, want it naming the fingerprint and both revisions", err)
}
var said []string
held := map[int64]map[string]catalog.CatalogEntry{3: {"old": {Commit: "c3", Fp: "src2:o1"}}}
cu := &Unfinished{Store: memUnfinished{
"main.old": `{"fingerprint":"src2:o1","revision":3}`,
"main.far": `{"fingerprint":"src2:f1","revision":2}`,
"main.back": `{"fingerprint":"src2:b1","revision":2}`,
}, Logf: func(f string, a ...any) { said = append(said, fmt.Sprintf(f, a...)) },
Entries: func(_ string, r int64) (map[string]catalog.CatalogEntry, bool) { e, ok := held[r]; return e, ok }}
got := cu.Carry("main", 20, map[string]catalog.CatalogEntry{"back": {Commit: "c20", Fp: "src2:b1"}}, nil)
if len(got) != 2 || got[0].Module != "back" || got[0].Commit != "c20" || got[1].Module != "old" || got[1].Commit != "c3" {
t.Errorf("the carry = %+v, want back read at the head's revision (its kept revision 2 gone) and old at its kept revision 3, which the head does not hold", got)
}
if j := strings.Join(said, "\n"); !strings.Contains(j, "far is no longer unfinished at fingerprint src2:f1") || !strings.Contains(j, "kept revision 2 is gone") || !strings.Contains(j, "head's revision 20") {
t.Errorf("the journal says %q, want each drop naming its fingerprint and the revisions looked at", said)
}
}
// R16/AC1 (design 47 H §765, H-A-58, H-A-98): a take-in.taken of the old form, with no call (or no branch or source), is
@@ -110,6 +110,9 @@ type Unfinished struct {
Pipeline func(key string) (seat.Pipeline, bool)
Registered func(module string) bool
Add func(from seat.Pipeline, group []seat.Mover) (string, bool)
// Kept reads a pipeline's kept-revisions, the revision each re-pinned mover's entry comes from (H-A-285); nil, or a pipeline
// that keeps none, says the revision the key names (H-A-608).
Kept func(pipeline string) map[string]int64
}
func (u *Unfinished) logf(format string, args ...any) {
@@ -417,13 +420,13 @@ func (u *Unfinished) Carry(branch string, revision int64, entries map[string]cat
}
if i := slices.IndexFunc(movers, func(m seat.Mover) bool { return m.Module == module }); i >= 0 {
if movers[i].Fingerprint != e.Fingerprint {
u.dropMoved(key, module, fmt.Sprintf("revision %d of %s moves it at %s", revision, branch, movers[i].Fingerprint))
u.dropMoved(key, module, e.Fingerprint, fmt.Sprintf("revision %d of %s, the revision looked at, moves it at %s", revision, branch, movers[i].Fingerprint))
}
continue
}
entry, ok := entries[module]
if !ok || entry.Fp != e.Fingerprint {
u.dropMoved(key, module, fmt.Sprintf("revision %d of %s holds it at %q", revision, branch, entry.Fp))
entry, why, ok := u.entryOf(branch, revision, entries, module, e)
if !ok {
u.dropMoved(key, module, e.Fingerprint, why)
continue
}
if by := u.heldBack(branch, module, e); len(by) > 0 && !slices.ContainsFunc(by, func(f string) bool {
@@ -438,13 +441,47 @@ func (u *Unfinished) Carry(branch string, revision int64, entries map[string]cat
return movers
}
// dropMoved deletes the key of an unfinished mover a newer fingerprint moves (§786).
func (u *Unfinished) dropMoved(key, module, why string) {
// keptRevision is the revision the entry of module's key comes from: its kept revision in the pipeline the key names (H-A-285),
// else the revision the key names (design 47 H2 H-A-608).
func (u *Unfinished) keptRevision(e UnfinishedEntry, module string) int64 {
if u.Kept != nil {
if r := u.Kept(e.Pipeline)[module]; r > 0 {
return r
}
}
return e.Revision
}
// entryOf reads module's entry for the carry (design 47 H2 H-A-608): at its kept revision when that is still read and holds it
// at the key's fingerprint, else at the head's revision (entries) when the entry there has the same fingerprint. Neither says why,
// naming the fingerprint and each revision looked at.
func (u *Unfinished) entryOf(branch string, head int64, entries map[string]catalog.CatalogEntry, module string, e UnfinishedEntry) (catalog.CatalogEntry, string, bool) {
var looked []string
if kept := u.keptRevision(e, module); kept > 0 && kept != head && u.Entries != nil {
if kents, ok := u.Entries(branch, kept); !ok {
looked = append(looked, fmt.Sprintf("its kept revision %d is gone", kept))
} else if entry, ok := kents[module]; ok && entry.Fp == e.Fingerprint {
return entry, "", true
} else {
looked = append(looked, fmt.Sprintf("its kept revision %d holds it at %q", kept, entry.Fp))
}
}
entry, ok := entries[module]
if ok && entry.Fp == e.Fingerprint {
return entry, "", true
}
looked = append(looked, fmt.Sprintf("the head's revision %d of %s holds it at %q", head, branch, entry.Fp))
return catalog.CatalogEntry{}, strings.Join(looked, ", ") + ", so a newer revision moved it or its entry is gone", false
}
// dropMoved deletes the key of an unfinished mover a newer fingerprint moves, or whose entry no revision looked at holds at its
// fingerprint (§786, H-A-608): one journal line names the fingerprint and the revisions looked at.
func (u *Unfinished) dropMoved(key, module, fingerprint, why string) {
if err := u.Store.Delete(key); err != nil {
u.logf("the unfinished entry of %s is not deleted: %v", module, err)
return
}
u.logf("%s is no longer unfinished: %s", module, why)
u.logf("%s is no longer unfinished at fingerprint %s: %s", module, fingerprint, why)
}
// Entry is one key's entry; false when the state holds none.