inky-engine 0.2.6

A hand-fused Metal inference engine for Thinking Machines' Inkling 2 Mini, for Apple Silicon M5 Macs (Metal 4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
----------------------------- MODULE PrefixTree -----------------------------
\* The Layer 3 prefix matcher (src/prefix.rs, PrefixTree and LiveTree):
\* a request matches the deepest retained start point in a tree of
\* sealed blocks.
\*
\* Unlike the first version of this model, blocks have token content:
\* a block is BlockLen tokens over Alphabet, so a block that differs
\* in its second token is a different block and MatchExact is a real
\* property, not a construction. A prefix is a token sequence whose
\* length is a whole number of blocks.
\*
\* Each sealed block also carries the pool facts the live matcher
\* checks: the slots it was sealed into and those slots' generations
\* at seal time (slotOf, genOf for the Full entry; winSlotOf, winGenOf
\* for the Window entry), against the slots' current generation
\* (poolGen). Reuse bumps a slot's generation; a block sealed at an
\* older generation is stale and must never be returned. refs is the
\* pin: a cached block sits at refs = 0, evictable, and lookup
\* validates it by generation; a lane pins a block it steps from
\* (refs = 1), and only then does the pool hold a ref for it. starts
\* counts the holders of a start point (a lane head and a prompt end
\* are two).
\*
\* A mutation after a seek consumes that seek's result (sought and
\* result reset): the checked properties speak about the tree as the
\* caller sees it at the moment of a seek, and the code's tree keeps
\* changing after one, as does the model's.
\*
\* Bug values are the reversed witnesses, one per config:
\*   "shallow"      Seek returns the shallowest live start (DeepestStart)
\*   "stale"        Seek ignores generations (ResultLive)
\*   "first-token"  a block matches on its first token only (MatchExact)
\*   "any-parent"   blocks match anywhere, not on one chain (ResultIsStart)
\*   "partial"      the result may end mid-block (StartOnBoundary)
\*   "leak"         Seek evicts stale blocks but drops no refs
\*                  (RefsSettledAfterSeek)
\*   "reuse-held"   a slot is reused while a block on it is pinned
\*                  (HeldNeverStale)
\*   "stale-insert" an insert is accepted at a generation its slot has
\*                  moved past (NoBadInsert; SealClaimed below)
\*   "evict"        not a bug: the Reuse guard is lifted (the pool's
\*                  bump_generation has no such guard) but Seek stays
\*                  correct, so a passing config exercises eviction
\*                  itself (PrefixTreeEvictFast, NoStaleAfterSeek)
\*
\* A seek also evicts: every stale sealed block (sealed at an older
\* generation of either of its slots) leaves the tree with its
\* subtree, its start holds released and any pin dropped. The code
\* does the same: LiveTree's seek evicts every stale block before
\* matching. A seek never returns the whole request: the final token
\* must remain to prefill, so an exact full match steps back one
\* block (ExpectedResult).
\* The request being matched is fixed in the module because a TLC
\* config file assigns only literals and cannot write a sequence.

EXTENDS Integers, Sequences, FiniteSets

CONSTANTS Alphabet, BlockLen, MaxBlocks, Slots, MaxGen, MaxHolders, MaxSealed, Bug

ASSUME /\ BlockLen \in Nat \ {0}
       /\ MaxBlocks \in Nat \ {0}
       /\ MaxGen \in Nat
       /\ MaxHolders \in Nat \ {0}
       /\ MaxSealed \in Nat

\* Two blocks of two tokens; must satisfy Len(Req) = MaxBlocks * BlockLen.
Req == <<1, 2, 1, 2>>

VARIABLES sealed, starts, slotOf, genOf, winSlotOf, winGenOf, poolGen,
          refs, result, sought, badInsert
vars == <<sealed, starts, slotOf, genOf, winSlotOf, winGenOf, poolGen,
          refs, result, sought, badInsert>>

L0 == {<<>>}
L1 == {<<a>> : a \in Alphabet}
L2 == {<<a, b>> : a, b \in Alphabet}
L3 == {<<a, b, c>> : a, b, c \in Alphabet}
L4 == {<<a, b, c, d>> : a, b, c, d \in Alphabet}
\* Every token sequence the model can name (results may end mid-block).
AllTok == L0 \cup L1 \cup L2 \cup L3 \cup L4
\* Sequences at block boundaries: the only things ever sealed or held.
BlockSeqs == L0 \cup L2 \cup L4

IsPrefix(p, s) ==
    /\ Len(p) <= Len(s)
    /\ \A i \in 1..Len(p) : p[i] = s[i]

DropBlock(s) == SubSeq(s, 1, Len(s) - BlockLen)

BlockAt(s, k) == SubSeq(s, (k - 1) * BlockLen + 1, k * BlockLen)

WinLive(s) == IF winSlotOf[s] = 0 THEN TRUE ELSE winGenOf[s] = poolGen[winSlotOf[s]]

EntriesLive(s) == genOf[s] = poolGen[slotOf[s]] /\ WinLive(s)

\* Every block-boundary prefix of s is sealed at its slots' current
\* generations. The empty prefix needs nothing and is always live.
Live(s) ==
    \A q \in BlockSeqs :
        (IsPrefix(q, s) /\ Len(q) > 0) =>
            q \in sealed /\ EntriesLive(q)

Held == {s \in BlockSeqs : starts[s] > 0}

Longest(cands, r) ==
    CHOOSE p \in cands : \A q \in cands : Len(q) <= Len(p)

DeepestLive(r) ==
    Longest({p \in Held \cup {<<>>} : IsPrefix(p, r) /\ Live(p)}, r)

\* The correct seek result: the deepest live held start, stepped back
\* one block when it is the whole request, so a token remains to
\* prefill.
ExpectedResult ==
    LET d == DeepestLive(Req)
    IN  IF d = Req THEN DeepestLive(DropBlock(Req)) ELSE d

Init ==
    /\ sealed = {<<>>}
    /\ starts = [s \in BlockSeqs |-> 0]
    /\ slotOf = [s \in BlockSeqs |-> 0]
    /\ genOf = [s \in BlockSeqs |-> 0]
    /\ winSlotOf = [s \in BlockSeqs |-> 0]
    /\ winGenOf = [s \in BlockSeqs |-> 0]
    /\ poolGen = [s \in Slots |-> 0]
    /\ refs = [s \in BlockSeqs |-> 0]
    /\ result = <<>>
    /\ sought = FALSE
    /\ badInsert = FALSE

\* A block is published only sealed, only onto a sealed parent, and
\* only within the modelled depth. It records its slots' generations
\* at seal time and takes no ref: cached, it is evictable until a
\* lane pins it. wslot = 0 seals a block with no Window entry.
Seal(s, slot, wslot) ==
    /\ s \in BlockSeqs
    /\ Len(s) \in {BlockLen, 2 * BlockLen}
    /\ Len(s) <= MaxBlocks * BlockLen
    /\ DropBlock(s) \in sealed
    /\ s \notin sealed
    /\ slot \in Slots
    /\ wslot \in Slots \cup {0}
    /\ sealed' = sealed \cup {s}
    /\ slotOf' = [slotOf EXCEPT ![s] = slot]
    /\ genOf' = [genOf EXCEPT ![s] = poolGen[slot]]
    /\ winSlotOf' = [winSlotOf EXCEPT ![s] = wslot]
    /\ winGenOf' = [winGenOf EXCEPT ![s] = IF wslot = 0 THEN 0 ELSE poolGen[wslot]]
    /\ refs' = [refs EXCEPT ![s] = 0]
    /\ UNCHANGED <<starts, poolGen, badInsert>>
    /\ sought' = FALSE
    /\ result' = <<>>

\* The stale-insert bug, expressible only as its own action: an insert
\* arrives claiming a generation its slot has moved past, and is
\* accepted anyway — for fresh tokens, or for a block already in the
\* tree (the stale twin: the name is re-sealed at the claimed, older
\* generation). Seal cannot write it, because Seal records the slot's
\* current generation and refuses a block already sealed. The code
\* refuses this insert (LiveTree, StaleEntry); under Bug =
\* "stale-insert" the model accepts it and NoBadInsert falls.
SealClaimed(s, slot, gen) ==
    /\ Bug = "stale-insert"
    /\ s \in BlockSeqs
    /\ Len(s) \in {BlockLen, 2 * BlockLen}
    /\ Len(s) <= MaxBlocks * BlockLen
    /\ DropBlock(s) \in sealed
    /\ slot \in Slots
    /\ gen \in 0..MaxGen
    /\ gen # poolGen[slot]
    /\ sealed' = sealed \cup {s}
    /\ slotOf' = [slotOf EXCEPT ![s] = slot]
    /\ genOf' = [genOf EXCEPT ![s] = gen]
    /\ winSlotOf' = [winSlotOf EXCEPT ![s] = 0]
    /\ winGenOf' = [winGenOf EXCEPT ![s] = 0]
    /\ refs' = [refs EXCEPT ![s] = 0]
    /\ badInsert' = TRUE
    /\ UNCHANGED <<starts, poolGen>>
    /\ sought' = FALSE
    /\ result' = <<>>

\* Inserting a block that is already in the tree, exactly: the code
\* returns the block it has and takes no second ref. The tree keeps
\* its own entries, whichever slots the caller named.
Dedup(s) ==
    /\ s \in sealed \ {<<>>}
    /\ EntriesLive(s)
    /\ UNCHANGED <<sealed, starts, slotOf, genOf, winSlotOf, winGenOf,
                    poolGen, refs, badInsert>>
    /\ sought' = FALSE
    /\ result' = <<>>

\* Inserting a fresh entry for a block whose twin in the tree is
\* stale: the code evicts the twin (its pin dropped, everything below
\* it gone, its start holds released) and seals the fresh entries in
\* its place, at their slots' current generations, unpinned.
ReSeal(s, slot, wslot) ==
    /\ s \in sealed \ {<<>>}
    /\ ~EntriesLive(s)
    /\ slot \in Slots
    /\ wslot \in Slots \cup {0}
    /\ sealed' = {u \in sealed : ~IsPrefix(s, u)} \cup {s}
    /\ starts' = [x \in BlockSeqs |-> IF IsPrefix(s, x) THEN 0 ELSE starts[x]]
    /\ slotOf' = [x \in BlockSeqs |->
            IF x = s THEN slot ELSE IF IsPrefix(s, x) THEN 0 ELSE slotOf[x]]
    /\ genOf' = [x \in BlockSeqs |->
            IF x = s THEN poolGen[slot] ELSE IF IsPrefix(s, x) THEN 0 ELSE genOf[x]]
    /\ winSlotOf' = [x \in BlockSeqs |->
            IF x = s THEN wslot ELSE IF IsPrefix(s, x) THEN 0 ELSE winSlotOf[x]]
    /\ winGenOf' = [x \in BlockSeqs |->
            IF x = s THEN (IF wslot = 0 THEN 0 ELSE poolGen[wslot])
            ELSE IF IsPrefix(s, x) THEN 0 ELSE winGenOf[x]]
    /\ refs' = [x \in BlockSeqs |-> IF IsPrefix(s, x) THEN 0 ELSE refs[x]]
    /\ UNCHANGED <<poolGen, badInsert>>
    /\ sought' = FALSE
    /\ result' = <<>>

\* A lane pins a live block it is stepping from; only then does the
\* pool hold a ref for it. A stale block cannot be pinned.
Pin(s) ==
    /\ s \in sealed \ {<<>>}
    /\ refs[s] = 0
    /\ EntriesLive(s)
    /\ refs' = [refs EXCEPT ![s] = 1]
    /\ UNCHANGED <<sealed, starts, slotOf, genOf, winSlotOf, winGenOf,
                    poolGen, badInsert>>
    /\ sought' = FALSE
    /\ result' = <<>>

Unpin(s) ==
    /\ s \in sealed \ {<<>>}
    /\ refs[s] = 1
    /\ refs' = [refs EXCEPT ![s] = 0]
    /\ UNCHANGED <<sealed, starts, slotOf, genOf, winSlotOf, winGenOf,
                    poolGen, badInsert>>
    /\ sought' = FALSE
    /\ result' = <<>>

Retain(s) ==
    /\ s \in sealed \ {<<>>}
    /\ starts[s] < MaxHolders
    /\ starts' = [starts EXCEPT ![s] = starts[s] + 1]
    /\ UNCHANGED <<sealed, slotOf, genOf, winSlotOf, winGenOf, poolGen, refs, badInsert>>
    /\ sought' = FALSE
    /\ result' = <<>>

Release(s) ==
    /\ starts[s] > 0
    /\ starts' = [starts EXCEPT ![s] = starts[s] - 1]
    /\ UNCHANGED <<sealed, slotOf, genOf, winSlotOf, winGenOf, poolGen, refs, badInsert>>
    /\ sought' = FALSE
    /\ result' = <<>>

\* The pool reuses a slot only once no pinned block names it (on
\* either entry): the generation moves, and any block still naming
\* the slot at the old generation is stale. Three witness configs
\* lift that guard (stale, leak, reuse-held) so staleness can arise
\* at all, and the "evict" config lifts it with a correct Seek so
\* eviction is exercised in a passing config.
Reuse(slot) ==
    /\ poolGen[slot] < MaxGen
    /\ Bug \in {"stale", "leak", "reuse-held", "evict"}
        \/ \A s \in sealed : (slotOf[s] = slot \/ winSlotOf[s] = slot) => refs[s] = 0
    /\ poolGen' = [poolGen EXCEPT ![slot] = poolGen[slot] + 1]
    /\ UNCHANGED <<sealed, starts, slotOf, genOf, winSlotOf, winGenOf, refs, badInsert>>
    /\ sought' = FALSE
    /\ result' = <<>>

\* Removal takes a block and everything below it out of the tree and
\* drops their pins and start holds.
Evict(s) ==
    /\ s \in sealed \ {<<>>}
    /\ sealed' = {t \in sealed : ~IsPrefix(s, t)}
    /\ starts' = [x \in BlockSeqs |-> IF IsPrefix(s, x) THEN 0 ELSE starts[x]]
    /\ refs' = [x \in BlockSeqs |-> IF IsPrefix(s, x) THEN 0 ELSE refs[x]]
    /\ slotOf' = [x \in BlockSeqs |-> IF IsPrefix(s, x) THEN 0 ELSE slotOf[x]]
    /\ genOf' = [x \in BlockSeqs |-> IF IsPrefix(s, x) THEN 0 ELSE genOf[x]]
    /\ winSlotOf' = [x \in BlockSeqs |-> IF IsPrefix(s, x) THEN 0 ELSE winSlotOf[x]]
    /\ winGenOf' = [x \in BlockSeqs |-> IF IsPrefix(s, x) THEN 0 ELSE winGenOf[x]]
    /\ UNCHANGED <<poolGen, badInsert>>
    /\ sought' = FALSE
    /\ result' = <<>>

FirstTokenMatch(p, r) ==
    /\ Len(p) <= Len(r)
    /\ \A k \in 1..(Len(p) \div BlockLen) :
           p[(k - 1) * BlockLen + 1] = r[(k - 1) * BlockLen + 1]

\* The longest request prefix whose blocks each appear as some sealed
\* block's last block, anywhere in the tree (the parent chain ignored).
AnyParentResult ==
    LET ok(n) == \A k \in 1..(n \div BlockLen) :
            \E t \in sealed :
                Len(t) >= BlockLen /\ BlockAt(t, Len(t) \div BlockLen) = BlockAt(Req, k)
        ns == {n \in {0, BlockLen, 2 * BlockLen} : n <= Len(Req) /\ ok(n)}
    IN  SubSeq(Req, 1, CHOOSE n \in ns : \A m \in ns : m <= n)

SeekResult ==
    CASE Bug = "shallow" ->
            LET c == {p \in Held \cup {<<>>} : IsPrefix(p, Req) /\ Live(p)}
            IN  CHOOSE p \in c : \A q \in c : Len(p) <= Len(q)
      [] Bug = "stale" ->
            Longest({p \in Held \cup {<<>>} : IsPrefix(p, Req)}, Req)
      [] Bug = "first-token" ->
            Longest({p \in Held \cup {<<>>} : FirstTokenMatch(p, Req)}, Req)
      [] Bug = "any-parent" -> AnyParentResult
      [] Bug = "partial" ->
            LET d == DeepestLive(Req)
            IN  IF Len(d) < Len(Req) THEN SubSeq(Req, 1, Len(d) + 1) ELSE d
      [] OTHER -> ExpectedResult

\* Stale blocks and everything below them: what a seek evicts.
StaleSet == {s \in sealed : Len(s) > 0 /\ ~EntriesLive(s)}
Evicted == {t \in sealed : \E s \in StaleSet : IsPrefix(s, t)}

Seek ==
    /\ result' = SeekResult
    /\ sought' = TRUE
    /\ sealed' = sealed \ Evicted
    /\ starts' = [x \in BlockSeqs |-> IF x \in Evicted THEN 0 ELSE starts[x]]
    /\ refs' = [x \in BlockSeqs |->
           IF Bug = "leak" \/ x \notin Evicted THEN refs[x] ELSE 0]
    /\ slotOf' = [x \in BlockSeqs |-> IF x \in Evicted THEN 0 ELSE slotOf[x]]
    /\ genOf' = [x \in BlockSeqs |-> IF x \in Evicted THEN 0 ELSE genOf[x]]
    /\ winSlotOf' = [x \in BlockSeqs |-> IF x \in Evicted THEN 0 ELSE winSlotOf[x]]
    /\ winGenOf' = [x \in BlockSeqs |-> IF x \in Evicted THEN 0 ELSE winGenOf[x]]
    /\ UNCHANGED <<poolGen, badInsert>>

Next ==
    \/ \E s \in BlockSeqs, slot \in Slots, wslot \in Slots \cup {0} : Seal(s, slot, wslot)
    \/ \E s \in BlockSeqs, slot \in Slots, gen \in 0..MaxGen : SealClaimed(s, slot, gen)
    \/ \E s \in BlockSeqs : Dedup(s)
    \/ \E s \in BlockSeqs, slot \in Slots, wslot \in Slots \cup {0} : ReSeal(s, slot, wslot)
    \/ \E s \in BlockSeqs : Pin(s) \/ Unpin(s)
    \/ \E s \in BlockSeqs : Retain(s) \/ Release(s) \/ Evict(s)
    \/ \E slot \in Slots : Reuse(slot)
    \/ Seek

\* A state bound for the small configs: the per-PR depth-2 check keeps
\* every rule but only explores trees of at most MaxSealed blocks.
SealedBound == Cardinality(sealed) <= MaxSealed

\* The per-PR bound further: one chain only. A match walks a single
\* chain from the root, so the deepest-start properties live there;
\* the nightly bound also explores siblings.
ChainBound ==
    \A a, b \in sealed : IsPrefix(a, b) \/ IsPrefix(b, a)

FastBound == SealedBound /\ ChainBound

Spec == Init /\ [][Next]_vars

TypeOK ==
    /\ sealed \subseteq BlockSeqs
    /\ starts \in [BlockSeqs -> 0..MaxHolders]
    /\ slotOf \in [BlockSeqs -> Slots \cup {0}]
    /\ genOf \in [BlockSeqs -> 0..MaxGen]
    /\ winSlotOf \in [BlockSeqs -> Slots \cup {0}]
    /\ winGenOf \in [BlockSeqs -> 0..MaxGen]
    /\ poolGen \in [Slots -> 0..MaxGen]
    /\ refs \in [BlockSeqs -> 0..1]
    /\ result \in AllTok
    /\ sought \in BOOLEAN
    /\ badInsert \in BOOLEAN

\* The tree is prefix-closed at block granularity.
TreeClosed ==
    \A s \in sealed : Len(s) >= BlockLen => DropBlock(s) \in sealed

\* Only sealed blocks are ever held as start points.
StartsSealed == \A s \in BlockSeqs : starts[s] > 0 => s \in sealed

\* A pin is only ever held on a sealed block.
RefsOnSealed == \A s \in BlockSeqs : refs[s] = 1 => s \in sealed \ {<<>>}

\* After a seek no pin survives on an evicted block: pins live only
\* on sealed blocks, and a seek has evicted every stale one. The
\* "leak" witness breaks exactly this.
RefsSettledAfterSeek ==
    sought => \A s \in BlockSeqs : refs[s] = 1 => s \in sealed \ {<<>>}

\* A pinned block is never stale: its slots have not been reused
\* under it. The "reuse-held" witness breaks exactly this.
HeldNeverStale ==
    \A s \in sealed \ {<<>>} :
        refs[s] = 1 => EntriesLive(s)

\* A pin covers one node's entries. LiveTree::pin does not recursively
\* pin its ancestors. The serving lane binds the entire matched chain.
\* A completely pinned chain must remain suitable for reuse.
PinnedPathLive ==
    \A s \in sealed :
        (\A q \in sealed \ {<<>>} : IsPrefix(q, s) => refs[q] = 1) => Live(s)

\* No insert was ever accepted at a generation its slot had moved
\* past. The "stale-insert" witness breaks exactly this.
NoBadInsert == ~badInsert

\* After a seek no stale block remains sealed: the seek evicted every
\* one of them, wherever they were in the tree.
NoStaleAfterSeek == sought => StaleSet = {}

\* The result is a held start point (or position 0).
ResultIsStart == sought => result \in Held \cup {<<>>}

\* The result is an exact token prefix of the request.
MatchExact == sought => IsPrefix(result, Req)

\* The result never ends in the middle of a block.
StartOnBoundary == sought => Len(result) = BlockLen * (Len(result) \div BlockLen)

\* No block on the returned path is stale or unsealed.
ResultLive == sought => Live(result)

\* The result is the deepest live held start point, stepped back one
\* block when that is the whole request.
DeepestStart == sought => result = ExpectedResult
=============================================================================