diff --git a/modules/mesh-grants/grants_test.go b/modules/mesh-grants/grants_test.go index 8af29a737..7c47f24cf 100644 --- a/modules/mesh-grants/grants_test.go +++ b/modules/mesh-grants/grants_test.go @@ -5373,6 +5373,14 @@ func TestGrantsForComposesARevisionOnce(t *testing.T) { if strings.Contains(said, "composed") || strings.Count(said, "(kept)") != 2 || !reflect.DeepEqual(first, again) { t.Errorf("a second ask for the same revisions said %q and answered %+v, want both kept and %+v", said, again, first) } + // Inputs that differ compose both again (H-A-478 second addendum); the same inputs then keep them again. + b.put(grants.ReadResolved, "a.mod-x.greeting", map[string]any{"node": "a", "module": "mod-x", "key": "greeting", "value": "hello"}) + if _, said = ask(meshgrants.GrantsForInput{Branch: "main", Source: "7"}); strings.Count(said, "(composed)") != 2 { + t.Errorf("an ask whose inputs differ said %q, want both revisions' grants composed again", said) + } + if _, said = ask(meshgrants.GrantsForInput{Branch: "main", Source: "7"}); strings.Count(said, "(kept)") != 2 { + t.Errorf("an ask with the same inputs again said %q, want both kept", said) + } // A run's candidate is another revision: composed once, the newest's grants kept. _, said = ask(meshgrants.GrantsForInput{Branch: "main", Run: "r1"}) if !strings.Contains(said, "the newest's grants (kept)") || !strings.Contains(said, "the candidate's grants (composed)") { @@ -5410,8 +5418,11 @@ func TestGrantsForKeepsWhatItMay(t *testing.T) { perBranch int then func(t *testing.T, b *fakeBus, p *grants.Part, followed *[]string) pages int + // composed is how many of the two revisions' grants the grants-for after it composes again: a revision no longer + // kept has no grants kept either (H-A-478). + composed int }{ - {name: "the setting bounds a branch's revisions: with 2, the candidate is dropped once two newer are kept", pages: 2, + {name: "the setting bounds a branch's revisions: with 2, the candidate is dropped once two newer are kept", pages: 2, composed: 1, then: func(t *testing.T, b *fakeBus, p *grants.Part, _ *[]string) { p.RevisionsPerBranch = setting(t, "2\n") hear(t, p, 10, 0) @@ -5419,14 +5430,14 @@ func TestGrantsForKeepsWhatItMay(t *testing.T) { _, _ = p.GrantsFor(meshgrants.GrantsForInput{Branch: "main", Source: "7"}) }}, {name: "a run's candidate counts within its branch's bound: with 1, the candidate and the newest each drop the other", run: true, - perBranch: 1, pages: 4, then: func(*testing.T, *fakeBus, *grants.Part, *[]string) {}}, - {name: "a branch that leaves the watched branches frees its revisions", pages: 4, + perBranch: 1, pages: 4, composed: 2, then: func(*testing.T, *fakeBus, *grants.Part, *[]string) {}}, + {name: "a branch that leaves the watched branches frees its revisions", pages: 4, composed: 2, then: func(t *testing.T, _ *fakeBus, p *grants.Part, followed *[]string) { *followed = nil _, _ = p.GrantsFor(meshgrants.GrantsForInput{Branch: "main", Source: "7"}) *followed = []string{"main"} }}, - {name: "an event below the newest held drops what is kept of the branch", pages: 4, + {name: "an event below the newest held drops what is kept of the branch", pages: 4, composed: 2, then: func(t *testing.T, _ *fakeBus, p *grants.Part, _ *[]string) { hear(t, p, 3, 0) }}, } { t.Run(row.name, func(t *testing.T) { @@ -5449,10 +5460,15 @@ func TestGrantsForKeepsWhatItMay(t *testing.T) { } row.then(t, b, p, &followed) before := len(slices.DeleteFunc(slices.Clone(b.calls), func(c string) bool { return c != grants.CallRevision })) + var said []string + p.Logf = func(f string, a ...any) { said = append(said, fmt.Sprintf(f, a...)) } _, _ = p.GrantsFor(in) if got := len(slices.DeleteFunc(slices.Clone(b.calls), func(c string) bool { return c != grants.CallRevision })) - before; got != row.pages { t.Errorf("grants-for read %d revision pages, want %d", got, row.pages) } + if len(said) != 1 || strings.Count(said[0], "(composed)") != row.composed { + t.Errorf("grants-for said %q, want %d of the revisions' grants composed again", said, row.composed) + } }) } if n, err := grants.RevisionsPerBranchSetting(filepath.Join(t.TempDir(), "none")); n != grants.DefaultRevisionsPerBranch || err != nil { diff --git a/modules/mesh-grants/internal/grants/grantsfor.go b/modules/mesh-grants/internal/grants/grantsfor.go index f4d6e0c6c..f8db5685b 100644 --- a/modules/mesh-grants/internal/grants/grantsfor.go +++ b/modules/mesh-grants/internal/grants/grantsfor.go @@ -2,6 +2,8 @@ package grants import ( "context" + "crypto/sha256" + "encoding/hex" "encoding/json" "errors" "fmt" @@ -98,9 +100,10 @@ type revisionsKept struct { mu sync.Mutex order map[string][]string kept map[string]map[string]catalogue.Manifest - // grants are the grants each kept revision composes to, by module (grantsOfEntries), composed once per revision and kept as - // long as the revision is (design 47 H2 H-A-478). - grants map[string]map[string]map[string]bool + // grants are the grants each kept revision composes to, by module (grantsOfEntries), with the digest of the inputs they were + // composed with: composed once per revision and inputs, kept as long as the revision is (design 47 H2 H-A-478 and its + // addenda). + grants map[string]keptGrants newest map[string]int64 sources map[string]map[int64]int64 sourced map[string][]int64 @@ -117,7 +120,7 @@ func revisionKey(branch string, r meshcatalog.CatalogRevision) string { func (k *revisionsKept) init() { if k.kept == nil { k.kept, k.order, k.newest = map[string]map[string]catalogue.Manifest{}, map[string][]string{}, map[string]int64{} - k.grants = map[string]map[string]map[string]bool{} + k.grants = map[string]keptGrants{} k.sources, k.sourced = map[string]map[int64]int64{}, map[string][]int64{} } } @@ -129,24 +132,44 @@ func (k *revisionsKept) get(branch string, r meshcatalog.CatalogRevision) (map[s return m, ok } -// grantsOf is the grants kept of the revision key names; false when none is. -func (k *revisionsKept) grantsOf(key string) (map[string]map[string]bool, bool) { +// keptGrants are a revision's composed grants, by module, and the digest of the inputs they were composed with. +type keptGrants struct { + inputs string + grants map[string]map[string]bool +} + +// grantsOf is the grants kept of the revision key names, composed with the inputs of the digest given; false when none is. +func (k *revisionsKept) grantsOf(key, inputs string) (map[string]map[string]bool, bool) { k.mu.Lock() defer k.mu.Unlock() g, ok := k.grants[key] - return g, ok + if !ok || g.inputs != inputs { + return nil, false + } + return g.grants, true } -// keepGrants keeps the grants the revision key names composes to, while that revision is kept: so the grants are bounded as -// the revisions are (H-A-195's setting). -func (k *revisionsKept) keepGrants(key string, g map[string]map[string]bool) { +// keepGrants keeps the grants the revision key names composes to with the inputs of the digest given, in place of any kept +// before, while that revision is kept: so the grants are bounded as the revisions are (H-A-195's setting). +func (k *revisionsKept) keepGrants(key, inputs string, g map[string]map[string]bool) { k.mu.Lock() defer k.mu.Unlock() if _, ok := k.kept[key]; ok { - k.grants[key] = g + k.grants[key] = keptGrants{inputs: inputs, grants: g} } } +// inputsDigest is the sha256 of the inputs as JSON: what a revision's kept grants are composed with (H-A-478 second +// addendum). Empty when they do not marshal: nothing is kept then. +func inputsDigest(in Inputs) string { + raw, err := json.Marshal(in) + if err != nil { + return "" + } + sum := sha256.Sum256(raw) + return hex.EncodeToString(sum[:]) +} + func (k *revisionsKept) put(branch string, r meshcatalog.CatalogRevision, manifests map[string]catalogue.Manifest, max int) { k.mu.Lock() defer k.mu.Unlock() @@ -440,15 +463,16 @@ func (p *Part) GrantsFor(in meshgrants.GrantsForInput) (meshgrants.GrantsFor, er } start = time.Now() input := p.inputs() + digest := inputsDigest(input) stage("the inputs", start) start = time.Now() - was, composed, err := p.grantsOfRevision(input, newestKept, newest) + was, composed, err := p.grantsOfRevision(input, digest, newestKept, newest) stage("the newest's grants ("+composed+")", start) if err != nil { return meshgrants.GrantsFor{Error: "the branch's newest does not compose: " + err.Error()}, nil } start = time.Now() - will, composed, err := p.grantsOfRevision(input, candKept, cand) + will, composed, err := p.grantsOfRevision(input, digest, candKept, cand) stage("the candidate's grants ("+composed+")", start) if err != nil { return meshgrants.GrantsFor{Error: "the candidate does not compose: " + err.Error()}, nil @@ -484,11 +508,12 @@ func candidateName(c meshcatalog.RevisionInput) string { return c.Branch + " source " + strconv.FormatInt(c.Source, 10) } -// grantsOfRevision is the grants a revision's manifests compose to with in, by module: those kept of the revision kept names -// when it has them, else composed and kept with it (H-A-478: once per revision). how says which. -func (p *Part) grantsOfRevision(in Inputs, kept string, manifests map[string]catalogue.Manifest) (grants map[string]map[string]bool, how string, err error) { - if kept != "" { - if g, ok := p.revisions.grantsOf(kept); ok { +// grantsOfRevision is the grants a revision's manifests compose to with in (of the digest inputs), by module: those kept of the +// revision kept names when they were composed with the same inputs, else composed and kept with it (H-A-478: once per revision +// and inputs). how says which. +func (p *Part) grantsOfRevision(in Inputs, inputs, kept string, manifests map[string]catalogue.Manifest) (grants map[string]map[string]bool, how string, err error) { + if kept != "" && inputs != "" { + if g, ok := p.revisions.grantsOf(kept, inputs); ok { return g, "kept", nil } } @@ -496,8 +521,8 @@ func (p *Part) grantsOfRevision(in Inputs, kept string, manifests map[string]cat if err != nil { return nil, "composed", err } - if kept != "" { - p.revisions.keepGrants(kept, g) + if kept != "" && inputs != "" { + p.revisions.keepGrants(kept, inputs, g) } return g, "composed", nil } diff --git a/modules/mesh-grants/internal/grants/part.go b/modules/mesh-grants/internal/grants/part.go index 30c3ae625..c6aef6f16 100644 --- a/modules/mesh-grants/internal/grants/part.go +++ b/modules/mesh-grants/internal/grants/part.go @@ -170,7 +170,8 @@ type Part struct { Emit Emitter // Create creates a declared stream the bus lacks: the process's connection; nil in the tools, which ask the process. Create Creator - // Logf logs what no state or condition says (an error of acting on an answer): the process's; nil in the tools. + // Logf logs what no state or condition says (an error of acting on an answer): the process's journal, and in the tools + // program its standard error (grants-for's stage times, H-A-478). Logf func(string, ...any) // Objects is the part's object bucket of sealed lists: the process's connection; nil in the tools. Objects Objects