feat: drive chunk kernel from published-state model table

This commit is contained in:
pingqiu committed 2026-11-30 05:39:37 -08:00
1 parent 60bb7c8fed
commit bab1cf39ad
4 files changed
+235 -199

No files matched your search

+85 -111
View File
@@ -1,22 +1,23 @@
// Package chunkkernel implements the pure chunk-kernel/1 transition table.
// It performs no storage I/O and is not connected to the serving path.
// Package chunkkernel implements the pure chunk-kernel/2 transition table.
// It performs no storage I/O and has no production callers.
package chunkkernel
import (
_ "embed"
"fmt"
"slices"
"strings"
)
const Version = "chunk-kernel/1"
const Version = "chunk-kernel/2"
type Kind string
const (
Absent Kind = "A"
Partial Kind = "P"
Unknown Kind = "U"
Hashed Kind = "H"
Ignored Kind = "ignored"
Absent Kind = "A"
Partial Kind = "P"
Unknown Kind = "U"
HashUnpublished Kind = "HU"
HashPublished Kind = "HP"
)
type Event string
@@ -25,6 +26,7 @@ const (
Write Event = "Write"
Fill Event = "Fill"
Hash Event = "Hash"
Commit Event = "Commit"
Evict Event = "Evict"
Move Event = "Move"
Changed Event = "Changed"
@@ -41,21 +43,27 @@ const (
CursorBack Effect = "cursor_back"
)
// State describes local coverage. Blocks is the partial chunk's local-block
// bitmap represented as block indices; other kinds do not retain this bitmap.
// The shell owns bytes, hashes, persistence and the meaning of block indices.
type State struct {
Kind Kind
Blocks []uint64
}
// State contains only the model state. The shell owns the durable bitmap,
// bytes, hashes and geometry; there is no second copy of those facts here.
type State struct{ Kind Kind }
// Arg.Value is the model's coverage (none/some/all), comparison (equal/differs)
// or '-' argument. WrittenBlocks names only this write's blocks. Changed and
// Rebuilt receive the store's complete local bitmap in LocalBlocks.
// Coverage is supplied by the shell; this kernel has no volume geometry.
type Arg struct {
Value string
WrittenBlocks, LocalBlocks []uint64
// Arg.Value is coverage (none/some/all), comparison (equal/differs), or '-'.
// For Write, coverage describes the resulting chunk, not just this write.
type Arg struct{ Value string }
type Outcome string
const (
Applied Outcome = "applied"
Ignored Outcome = "ignored"
Refused Outcome = "refused"
)
// Result separates the state a caller can store from the transition outcome.
// Ignored and refused results preserve the exact input state.
type Result struct {
State State
Outcome Outcome
}
// CallerDefect is a refused table edge or an input outside the table domain.
@@ -70,98 +78,64 @@ func (e *CallerDefect) Error() string {
return fmt.Sprintf("chunk kernel: refused %s/%s/%s", e.State, e.Event, e.Arg)
}
// Step returns the model result and the effect the shell must carry out.
// Ignored means no change and no record. Refusal is a typed caller defect.
// All returned bitmaps are fresh; neither input nor a previous result is mutated.
func Step(s State, e Event, a Arg) (State, Effect, error) {
refuse := func() (State, Effect, error) { return State{}, None, &CallerDefect{s.Kind, e, a.Value} }
if s.Kind != Absent && s.Kind != Partial && s.Kind != Unknown && s.Kind != Hashed {
return refuse()
}
valid := false
switch e {
case Write:
valid = a.Value == "some" || a.Value == "all"
case Fill, Hash:
valid = a.Value == "-"
case Evict, Move:
valid = a.Value == "equal" || a.Value == "differs"
case Changed, Rebuilt:
valid = a.Value == "none" || a.Value == "some" || a.Value == "all"
}
if !valid {
return refuse()
}
result, effect := State{}, None
switch e {
case Write:
if a.Value == "all" {
result.Kind = Unknown
} else if s.Kind == Absent || s.Kind == Partial {
result.Kind = Partial
// An absent chunk may carry stale caller memory: never merge that bitmap.
if s.Kind == Partial {
result.Blocks = append(result.Blocks, s.Blocks...)
}
result.Blocks = append(result.Blocks, a.WrittenBlocks...)
slices.Sort(result.Blocks)
result.Blocks = slices.Compact(result.Blocks)
} else {
return refuse()
}
case Fill:
switch s.Kind {
case Absent:
result.Kind = Hashed
case Partial:
result.Kind = Unknown
default:
result.Kind = Ignored
type edge struct {
state Kind
event Event
arg string
}
type transition struct {
result string
effect Effect
}
//go:embed testdata/chunk-kernel-2.tsv
var exportedTable string
var transitions = loadTable()
func loadTable() map[edge]transition {
table := map[edge]transition{}
version := ""
for _, line := range strings.Split(exportedTable, "\n") {
line = strings.TrimSuffix(line, "\r")
if line == "" {
continue
}
case Hash:
switch s.Kind {
case Unknown:
result.Kind = Hashed
case Hashed:
result.Kind = Ignored
default:
return refuse()
}
case Evict:
switch {
case s.Kind == Absent:
result.Kind = Ignored
case s.Kind == Hashed && a.Value == "equal":
result.Kind = Absent
effect = PageBeforeBytes
default:
return refuse()
}
case Move:
result.Kind = Absent
switch s.Kind {
case Partial, Unknown:
effect = DeclaredLoss
case Hashed:
if a.Value == "equal" {
result.Kind = Hashed
} else {
effect = Drop
if strings.HasPrefix(line, "#") {
if _, v, ok := strings.Cut(line, "version: "); ok {
version = v
}
continue
}
case Changed, Rebuilt:
switch a.Value {
case "none":
result.Kind = Absent
case "some":
result.Kind = Partial
result.Blocks = append([]uint64(nil), a.LocalBlocks...)
case "all":
result.Kind = Unknown
c := strings.Split(line, "\t")
if len(c) != 5 {
panic("chunk kernel: malformed exported table row")
}
if e == Rebuilt {
effect = CursorBack
key := edge{Kind(c[0]), Event(c[1]), c[2]}
if _, exists := table[key]; exists {
panic("chunk kernel: duplicate exported edge")
}
table[key] = transition{c[3], Effect(c[4])}
}
if version != Version {
panic("chunk kernel: exported table version mismatch")
}
return table
}
// Step reads the exported model table. Refused edges and inputs outside its
// domain return a typed caller defect, the input state and no effect.
//
// Commit is a shell premise: the known hash must equal the hash the committed
// HEAD names, at the captured version. State HU alone cannot establish this;
// another write and hash during a cut may have replaced the known hash.
// The shell also owns persistence and the ordering required by each Effect.
func Step(s State, e Event, a Arg) (Result, Effect, error) {
t, ok := transitions[edge{s.Kind, e, a.Value}]
if !ok || t.result == "refused" {
return Result{s, Refused}, None, &CallerDefect{s.Kind, e, a.Value}
}
if t.result == "ignored" {
return Result{s, Ignored}, t.effect, nil
}
return result, effect, nil
return Result{State{Kind(t.result)}, Applied}, t.effect, nil
}
+78 -30
View File
@@ -9,16 +9,20 @@ import (
)
func TestExportedModelEdges(t *testing.T) {
f, err := os.Open("testdata/chunk-kernel-1.tsv")
f, err := os.Open("testdata/chunk-kernel-2.tsv")
if err != nil {
t.Fatal(err)
}
defer f.Close()
scan := bufio.NewScanner(f)
seen := map[string]bool{}
version := ""
for scan.Scan() {
line := scan.Text()
if strings.HasPrefix(line, "#") {
if _, v, ok := strings.Cut(line, "version: "); ok {
version = v
}
continue
}
cols := strings.Split(line, "\t")
@@ -31,55 +35,99 @@ func TestExportedModelEdges(t *testing.T) {
}
seen[key] = true
t.Run(key, func(t *testing.T) {
result, effect, err := Step(State{Kind: Kind(cols[0])}, Event(cols[1]), Arg{Value: cols[2]})
input := State{Kind: Kind(cols[0])}
result, effect, err := Step(input, Event(cols[1]), Arg{Value: cols[2]})
if string(effect) != cols[4] {
t.Fatalf("effect=%s want%s", effect, cols[4])
}
if cols[3] == "refused" {
var defect *CallerDefect
if !errors.As(err, &defect) {
t.Fatalf("refusal error=%v", err)
}
if effect != None {
t.Fatal(effect)
if !errors.As(err, &defect) || result.State != input || result.Outcome != Refused || effect != None {
t.Fatalf("refused edge changed input: %+v/%s/%v", result, effect, err)
}
return
}
if err != nil {
t.Fatal(err)
}
if string(result.Kind) != cols[3] || string(effect) != cols[4] {
t.Fatalf("got %s/%s want %s/%s", result.Kind, effect, cols[3], cols[4])
if cols[3] == "ignored" {
if result.State != input || result.Outcome != Ignored {
t.Fatalf("ignored edge changed input: %+v", result)
}
} else if string(result.State.Kind) != cols[3] || result.Outcome != Applied {
t.Fatalf("got%+v want%s", result, cols[3])
}
})
}
if err := scan.Err(); err != nil {
t.Fatal(err)
}
if len(seen) != 56 {
t.Fatalf("edges=%d want56", len(seen))
if version == "" || version != Version {
t.Fatalf("version=%q want%q", version, Version)
}
// Every input in the exported model's domain must appear exactly once.
domains := map[Event][]string{Write: {"some", "all"}, Fill: {"-"}, Hash: {"-"}, Commit: {"-"}, Evict: {"-"}, Move: {"equal", "differs"}, Changed: {"some", "all", "none"}, Rebuilt: {"some", "all", "none"}}
total := 0
for _, s := range []Kind{Absent, Partial, Unknown, HashUnpublished, HashPublished} {
for e, args := range domains {
for _, a := range args {
total++
if !seen[string(s)+"/"+string(e)+"/"+a] {
t.Errorf("missing %s/%s/%s", s, e, a)
}
}
}
}
if len(seen) != total {
t.Fatalf("edges=%d domain=%d", len(seen), total)
}
}
func TestAbsentWriteStartsBitmapAfresh(t *testing.T) {
stale := []uint64{1, 2}
written := []uint64{7}
got, _, err := Step(State{Kind: Absent, Blocks: stale}, Write, Arg{Value: "some", WrittenBlocks: written})
if err != nil {
t.Fatal(err)
}
if got.Kind != Partial || len(got.Blocks) != 1 || got.Blocks[0] != 7 {
t.Fatalf("stale bitmap survived: %+v", got)
}
got.Blocks[0] = 9
if stale[0] != 1 || written[0] != 7 {
t.Fatal("Step mutated its inputs")
func TestOutsideDomainPreservesInput(t *testing.T) {
for _, tc := range []struct {
s Kind
e Event
a string
}{
{"not-a-state", Write, "all"}, {Absent, "not-an-event", "-"},
{Partial, Fill, "equal"}, {HashPublished, Evict, "equal"},
{Unknown, Write, "none"}, {HashUnpublished, Commit, "some"},
{Absent, Write, ""}, {"", Fill, "-"},
} {
t.Run(string(tc.s)+"/"+string(tc.e)+"/"+tc.a, func(t *testing.T) {
input := State{Kind: tc.s}
got, effect, err := Step(input, tc.e, Arg{Value: tc.a})
var defect *CallerDefect
if !errors.As(err, &defect) {
t.Fatalf("error=%v want CallerDefect", err)
}
if got.State != input || got.Outcome != Refused || effect != None {
t.Fatalf("refusal changed input: %+v/%s", got, effect)
}
if defect.State != tc.s || defect.Event != tc.e || defect.Arg != tc.a {
t.Fatalf("wrong defect: %+v", defect)
}
})
}
}
func TestPartialWriteKeepsExistingBlocks(t *testing.T) {
got, _, err := Step(State{Kind: Partial, Blocks: []uint64{2, 4}}, Write, Arg{Value: "some", WrittenBlocks: []uint64{4, 6}})
if err != nil {
t.Fatal(err)
func TestHashedChunkNeedsPublicationBeforeEviction(t *testing.T) {
got, _, err := Step(State{Kind: Unknown}, Hash, Arg{Value: "-"})
if err != nil || got.State.Kind != HashUnpublished {
t.Fatalf("hash: %+v %v", got, err)
}
if len(got.Blocks) != 3 || got.Blocks[0] != 2 || got.Blocks[1] != 4 || got.Blocks[2] != 6 {
t.Fatalf("bitmap=%v", got.Blocks)
held, effect, err := Step(got.State, Evict, Arg{Value: "-"})
var defect *CallerDefect
if !errors.As(err, &defect) || held.State != got.State || effect != None {
t.Fatalf("unpublished eviction: %+v/%s/%v", held, effect, err)
}
// The caller has independently established the committed HEAD names this hash.
published, _, err := Step(got.State, Commit, Arg{Value: "-"})
if err != nil || published.State.Kind != HashPublished {
t.Fatalf("commit: %+v %v", published, err)
}
evicted, effect, err := Step(published.State, Evict, Arg{Value: "-"})
if err != nil || evicted.State.Kind != Absent || effect != PageBeforeBytes {
t.Fatalf("published eviction: %+v/%s/%v", evicted, effect, err)
}
}
@@ -1,58 +0,0 @@
# kernel table of the per-chunk state. version: chunk-kernel/1
# state event arg result effect
A Write some P none
A Write all U none
A Fill - H none
A Hash - refused none
A Evict equal ignored none
A Evict differs ignored none
A Move equal A none
A Move differs A none
A Changed some P none
A Changed all U none
A Changed none A none
A Rebuilt some P cursor_back
A Rebuilt all U cursor_back
A Rebuilt none A cursor_back
P Write some P none
P Write all U none
P Fill - U none
P Hash - refused none
P Evict equal refused none
P Evict differs refused none
P Move equal A declared_loss
P Move differs A declared_loss
P Changed some P none
P Changed all U none
P Changed none A none
P Rebuilt some P cursor_back
P Rebuilt all U cursor_back
P Rebuilt none A cursor_back
U Write some refused none
U Write all U none
U Fill - ignored none
U Hash - H none
U Evict equal refused none
U Evict differs refused none
U Move equal A declared_loss
U Move differs A declared_loss
U Changed some P none
U Changed all U none
U Changed none A none
U Rebuilt some P cursor_back
U Rebuilt all U cursor_back
U Rebuilt none A cursor_back
H Write some refused none
H Write all U none
H Fill - ignored none
H Hash - ignored none
H Evict equal A page_before_bytes
H Evict differs refused none
H Move equal H none
H Move differs A drop
H Changed some P none
H Changed all U none
H Changed none A none
H Rebuilt some P cursor_back
H Rebuilt all U cursor_back
H Rebuilt none A cursor_back
1 # kernel table of the per-chunk state. version: chunk-kernel/1
2 # state event arg result effect
3 A Write some P none
4 A Write all U none
5 A Fill - H none
6 A Hash - refused none
7 A Evict equal ignored none
8 A Evict differs ignored none
9 A Move equal A none
10 A Move differs A none
11 A Changed some P none
12 A Changed all U none
13 A Changed none A none
14 A Rebuilt some P cursor_back
15 A Rebuilt all U cursor_back
16 A Rebuilt none A cursor_back
17 P Write some P none
18 P Write all U none
19 P Fill - U none
20 P Hash - refused none
21 P Evict equal refused none
22 P Evict differs refused none
23 P Move equal A declared_loss
24 P Move differs A declared_loss
25 P Changed some P none
26 P Changed all U none
27 P Changed none A none
28 P Rebuilt some P cursor_back
29 P Rebuilt all U cursor_back
30 P Rebuilt none A cursor_back
31 U Write some refused none
32 U Write all U none
33 U Fill - ignored none
34 U Hash - H none
35 U Evict equal refused none
36 U Evict differs refused none
37 U Move equal A declared_loss
38 U Move differs A declared_loss
39 U Changed some P none
40 U Changed all U none
41 U Changed none A none
42 U Rebuilt some P cursor_back
43 U Rebuilt all U cursor_back
44 U Rebuilt none A cursor_back
45 H Write some refused none
46 H Write all U none
47 H Fill - ignored none
48 H Hash - ignored none
49 H Evict equal A page_before_bytes
50 H Evict differs refused none
51 H Move equal H none
52 H Move differs A drop
53 H Changed some P none
54 H Changed all U none
55 H Changed none A none
56 H Rebuilt some P cursor_back
57 H Rebuilt all U cursor_back
58 H Rebuilt none A cursor_back
@@ -0,0 +1,72 @@
# kernel table of the per-chunk state. version: chunk-kernel/2
# state event arg result effect
A Write some P none
A Write all U none
A Fill - HP none
A Hash - refused none
A Commit - refused none
A Evict - ignored none
A Move equal A none
A Move differs A none
A Changed some P none
A Changed all U none
A Changed none A none
A Rebuilt some P cursor_back
A Rebuilt all U cursor_back
A Rebuilt none A cursor_back
P Write some P none
P Write all U none
P Fill - U none
P Hash - refused none
P Commit - refused none
P Evict - refused none
P Move equal A declared_loss
P Move differs A declared_loss
P Changed some P none
P Changed all U none
P Changed none A none
P Rebuilt some P cursor_back
P Rebuilt all U cursor_back
P Rebuilt none A cursor_back
U Write some refused none
U Write all U none
U Fill - ignored none
U Hash - HU none
U Commit - refused none
U Evict - refused none
U Move equal A declared_loss
U Move differs A declared_loss
U Changed some P none
U Changed all U none
U Changed none A none
U Rebuilt some P cursor_back
U Rebuilt all U cursor_back
U Rebuilt none A cursor_back
HU Write some refused none
HU Write all U none
HU Fill - ignored none
HU Hash - ignored none
HU Commit - HP none
HU Evict - refused none
HU Move equal HP none
HU Move differs A declared_loss
HU Changed some P none
HU Changed all U none
HU Changed none A none
HU Rebuilt some P cursor_back
HU Rebuilt all U cursor_back
HU Rebuilt none A cursor_back
HP Write some refused none
HP Write all U none
HP Fill - ignored none
HP Hash - ignored none
HP Commit - ignored none
HP Evict - A page_before_bytes
HP Move equal HP none
HP Move differs A drop
HP Changed some P none
HP Changed all U none
HP Changed none A none
HP Rebuilt some P cursor_back
HP Rebuilt all U cursor_back
HP Rebuilt none A cursor_back
1 # kernel table of the per-chunk state. version: chunk-kernel/2
2 # state event arg result effect
3 A Write some P none
4 A Write all U none
5 A Fill - HP none
6 A Hash - refused none
7 A Commit - refused none
8 A Evict - ignored none
9 A Move equal A none
10 A Move differs A none
11 A Changed some P none
12 A Changed all U none
13 A Changed none A none
14 A Rebuilt some P cursor_back
15 A Rebuilt all U cursor_back
16 A Rebuilt none A cursor_back
17 P Write some P none
18 P Write all U none
19 P Fill - U none
20 P Hash - refused none
21 P Commit - refused none
22 P Evict - refused none
23 P Move equal A declared_loss
24 P Move differs A declared_loss
25 P Changed some P none
26 P Changed all U none
27 P Changed none A none
28 P Rebuilt some P cursor_back
29 P Rebuilt all U cursor_back
30 P Rebuilt none A cursor_back
31 U Write some refused none
32 U Write all U none
33 U Fill - ignored none
34 U Hash - HU none
35 U Commit - refused none
36 U Evict - refused none
37 U Move equal A declared_loss
38 U Move differs A declared_loss
39 U Changed some P none
40 U Changed all U none
41 U Changed none A none
42 U Rebuilt some P cursor_back
43 U Rebuilt all U cursor_back
44 U Rebuilt none A cursor_back
45 HU Write some refused none
46 HU Write all U none
47 HU Fill - ignored none
48 HU Hash - ignored none
49 HU Commit - HP none
50 HU Evict - refused none
51 HU Move equal HP none
52 HU Move differs A declared_loss
53 HU Changed some P none
54 HU Changed all U none
55 HU Changed none A none
56 HU Rebuilt some P cursor_back
57 HU Rebuilt all U cursor_back
58 HU Rebuilt none A cursor_back
59 HP Write some refused none
60 HP Write all U none
61 HP Fill - ignored none
62 HP Hash - ignored none
63 HP Commit - ignored none
64 HP Evict - A page_before_bytes
65 HP Move equal HP none
66 HP Move differs A drop
67 HP Changed some P none
68 HP Changed all U none
69 HP Changed none A none
70 HP Rebuilt some P cursor_back
71 HP Rebuilt all U cursor_back
72 HP Rebuilt none A cursor_back