inky-engine 3.0.2

A hand-fused Metal inference engine for Thinking Machines' Inkling 2 Mini, for Apple M5 Macs
---------------------------- MODULE KvPoolCap ----------------------------
\* TLA+ model of `INK_KV_POOL_BYTES` (`ServePoolBudget::compute`,
\* `Engine::checked_capped_serve_block_pool_counts_for`, `pooled_context`,
\* HTTP 400 over-pool refusal, and eviction-before-deferral under a fixed shared pool).
\*
\* The pool's budget is what the working set has left after the lanes' non-KV bytes and the host bytes the block
\* cache may fill with saved work (`INK_MAX_RETAINED_HOST_BYTES` when set, else the recommended carve). Host and GPU
\* memory are one physical memory, so `HostRetentionInsideBudget` requires lanes, pool and host budget to fit
\* `AvailBudget` together. A `PoolCapBytes` at or above `AvailBudget` never binds: the pool is the automatic budget.
\*
\* Reversed witnesses for the host budget:
\* - `"ignore_host_override"` (`KvPoolCapIgnoreHostOverride.cfg`, automatic pool) -> violates `HostRetentionInsideBudget`
\* - `"ignore_host_override"` (`KvPoolCapIgnoreHostOverrideCapped.cfg`, a cap above what the override leaves)
\*   -> violates `HostRetentionInsideBudget`
\* Before the fix the pool gave up only the recommended carve whatever the override said: on 2026-12-02 a 128 GB
\* M5 Max with a 28 GiB override wired a 97 GB pool beside a 20 GB model and went to swap.

EXTENDS Integers, FiniteSets, Sequences, TLC

CONSTANTS
    AvailBudget,
    RecommendedHost,
    HostOverride,
    NonKvPerLane,
    WinCost,
    FullCost,
    WindowBlocks,
    ConfiguredLanes,
    ConfiguredCtxBlocks,
    PoolCapBytes,
    MaxQueue,
    MaxSteps,
    Bug

ASSUME
    /\ AvailBudget >= 1
    /\ RecommendedHost >= 0
    /\ HostOverride >= 0
    /\ NonKvPerLane >= 1
    /\ WinCost >= 1
    /\ FullCost >= 1
    /\ WindowBlocks >= 1
    /\ ConfiguredLanes >= 1
    /\ ConfiguredCtxBlocks > WindowBlocks
    /\ PoolCapBytes >= WinCost + FullCost
    /\ MaxQueue >= 1
    /\ MaxSteps >= 1
    /\ Bug \in {
           "none",
           "lanes_times_ctx_clamp",
           "ignore_host_override",
           "starve_window",
           "admit_over_pool",
           "defer_without_evict",
           "sub_rollover_stranded"
       }

Min(a, b) == IF a <= b THEN a ELSE b
Max(a, b) == IF a >= b THEN a ELSE b

PerLaneReserve ==
    IF Bug = "lanes_times_ctx_clamp"
    THEN NonKvPerLane + ConfiguredCtxBlocks * FullCost
    ELSE NonKvPerLane + WinCost + FullCost

MaxBudgetLanes == Max(1, AvailBudget \div PerLaneReserve)
EffectiveLanes == Max(1, Min(ConfiguredLanes, MaxBudgetLanes))

AfterNonKvBudget ==
    IF AvailBudget > EffectiveLanes * NonKvPerLane
    THEN AvailBudget - EffectiveLanes * NonKvPerLane
    ELSE 0

\* Host bytes the block cache may fill: the override when set (`HostOverride > 0`), else the recommended carve
\* (`ServeBlockCache::recommended_host_budget_bytes`: 1/32 of `AfterNonKvBudget`, between 16 MiB and 512 MiB).
HostRetention == IF HostOverride > 0 THEN HostOverride ELSE RecommendedHost

\* What the pool gives up for it. The bug took out the recommended carve whatever the override said.
PoolHostCarve == IF Bug = "ignore_host_override" THEN RecommendedHost ELSE HostRetention

AutoPoolBudget ==
    IF AfterNonKvBudget > PoolHostCarve
    THEN AfterNonKvBudget - PoolHostCarve
    ELSE 0

EffectivePoolBudget == Min(PoolCapBytes, AutoPoolBudget)

SlidingWinFloor == WindowBlocks + 2
PairCost == WinCost + FullCost

\* A server whose host budget leaves no room for one window and one full block refuses to start
\* (`Engine::plan_serve_block_pools_for` on the budget `ServePoolBudget::compute` hands it).
ASSUME AfterNonKvBudget >= HostRetention + PairCost

WinSlots ==
    IF Bug = "starve_window"
    THEN Min(WindowBlocks + 1, Max(1, EffectivePoolBudget \div PairCost))
    ELSE IF EffectivePoolBudget >= SlidingWinFloor * PairCost
         THEN SlidingWinFloor
         ELSE Max(1, EffectivePoolBudget \div PairCost)

FullSlots ==
    IF WinSlots < SlidingWinFloor /\ Bug /= "sub_rollover_stranded"
    THEN WinSlots
    ELSE Max(1, (EffectivePoolBudget - WinSlots * WinCost) \div FullCost)

PooledCtxBlocks ==
    LET hardSlots ==
            IF Bug = "sub_rollover_stranded"
            THEN IF WinSlots <= WindowBlocks THEN Min(FullSlots, WinSlots) ELSE FullSlots
            ELSE IF WinSlots < SlidingWinFloor THEN Min(FullSlots, WinSlots) ELSE FullSlots
    IN Min(ConfiguredCtxBlocks, hardSlots)

Lanes == 1..EffectiveLanes
ReqSizes == 1..ConfiguredCtxBlocks

VARIABLES
    sized,
    active,
    coldBlocks,
    queued,
    falseDefer,
    steps

vars == <<sized, active, coldBlocks, queued, falseDefer, steps>>

RECURSIVE SumSeq(_)
SumSeq(s) == IF s = << >> THEN 0 ELSE Head(s) + SumSeq(Tail(s))

ActiveUsed ==
    LET F[i \in 0..EffectiveLanes] ==
            IF i = 0 THEN 0 ELSE F[i - 1] + active[i]
    IN F[EffectiveLanes]

ColdUsed == SumSeq(coldBlocks)

RECURSIVE EvictColdUntilFits(_, _)
EvictColdUntilFits(s, limit) ==
    IF s = << >> \/ SumSeq(s) <= limit
    THEN s
    ELSE EvictColdUntilFits(Tail(s), limit)

TypeOK ==
    /\ sized \in BOOLEAN
    /\ active \in [Lanes -> 0..FullSlots]
    /\ coldBlocks \in Seq(1..FullSlots)
    /\ queued \in Seq(ReqSizes)
    /\ Len(queued) <= MaxQueue
    /\ falseDefer \in BOOLEAN
    /\ steps \in 0..MaxSteps

Init ==
    /\ sized = FALSE
    /\ active = [l \in Lanes |-> 0]
    /\ coldBlocks = << >>
    /\ queued = << >>
    /\ falseDefer = FALSE
    /\ steps = 0

SizePool ==
    /\ ~sized
    /\ sized' = TRUE
    /\ UNCHANGED <<active, coldBlocks, queued, falseDefer, steps>>

Arrive(req) ==
    /\ sized
    /\ steps < MaxSteps
    /\ req \in ReqSizes
    /\ LET limit == IF Bug = "admit_over_pool" THEN ConfiguredCtxBlocks ELSE PooledCtxBlocks
       IN IF req > limit
          THEN /\ UNCHANGED <<sized, active, coldBlocks, queued, falseDefer>>
               /\ steps' = steps + 1
          ELSE /\ Len(queued) < MaxQueue
               /\ queued' = Append(queued, req)
               /\ UNCHANGED <<sized, active, coldBlocks, falseDefer>>
               /\ steps' = steps + 1

AdmitHead(l) ==
    /\ sized
    /\ steps < MaxSteps
    /\ Len(queued) > 0
    /\ l \in Lanes
    /\ active[l] = 0
    /\ LET req == Head(queued)
           canFit ==
               IF Bug = "defer_without_evict"
               THEN ActiveUsed + ColdUsed + req <= FullSlots
               ELSE ActiveUsed + req <= FullSlots
       IN /\ canFit
          /\ coldBlocks' =
                 IF Bug = "defer_without_evict"
                 THEN coldBlocks
                 ELSE EvictColdUntilFits(coldBlocks, FullSlots - ActiveUsed - req)
          /\ active' = [active EXCEPT ![l] = req]
          /\ queued' = Tail(queued)
          /\ UNCHANGED <<sized, falseDefer>>
          /\ steps' = steps + 1

DeferHead ==
    /\ sized
    /\ steps < MaxSteps
    /\ Len(queued) > 0
    /\ \E l \in Lanes : active[l] = 0
    /\ LET req == Head(queued)
           canFit ==
               IF Bug = "defer_without_evict"
               THEN ActiveUsed + ColdUsed + req <= FullSlots
               ELSE ActiveUsed + req <= FullSlots
       IN /\ ~canFit
          /\ falseDefer' = (falseDefer \/ (ActiveUsed + req <= FullSlots))
          /\ UNCHANGED <<sized, active, coldBlocks, queued>>
          /\ steps' = steps + 1

FinishLane(l) ==
    /\ sized
    /\ steps < MaxSteps
    /\ l \in Lanes
    /\ active[l] > 0
    /\ LET released == active[l]
           activeAfter == ActiveUsed - released
       IN /\ active' = [active EXCEPT ![l] = 0]
          /\ coldBlocks' =
                 IF activeAfter + ColdUsed + released <= FullSlots
                 THEN Append(coldBlocks, released)
                 ELSE coldBlocks
          /\ UNCHANGED <<sized, queued, falseDefer>>
          /\ steps' = steps + 1

Stutter ==
    /\ sized
    /\ steps = MaxSteps
    /\ UNCHANGED vars

Next ==
    \/ SizePool
    \/ \E req \in ReqSizes : Arrive(req)
    \/ \E l \in Lanes : AdmitHead(l)
    \/ DeferHead
    \/ \E l \in Lanes : FinishLane(l)
    \/ Stutter

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

\* ---- Invariants ----

PoolCapRespected ==
    sized =>
        (/\ WinSlots * WinCost + FullSlots * FullCost <= EffectivePoolBudget
         /\ EffectiveLanes * NonKvPerLane + WinSlots * WinCost + FullSlots * FullCost <= AvailBudget)

HostRetentionInsideBudget ==
    sized =>
        EffectiveLanes * NonKvPerLane + WinSlots * WinCost + FullSlots * FullCost + HostRetention <= AvailBudget

LanesUnclampedWhenCapFits ==
    (sized /\ ConfiguredLanes * (NonKvPerLane + WinCost + FullCost) <= AvailBudget)
        => (EffectiveLanes = ConfiguredLanes)

SlidingWindowUnlocked ==
    (sized /\ EffectivePoolBudget >= SlidingWinFloor * PairCost)
        => (WinSlots >= SlidingWinFloor /\ PooledCtxBlocks = Min(ConfiguredCtxBlocks, FullSlots))

SubRolloverPaired ==
    (sized /\ WinSlots < SlidingWinFloor)
        => (FullSlots = WinSlots /\ PooledCtxBlocks = Min(ConfiguredCtxBlocks, WinSlots))

PoolNeverExceeded ==
    ActiveUsed + ColdUsed <= FullSlots

QueuedAlwaysPoolSatisfiable ==
    \A i \in 1..Len(queued) : queued[i] <= FullSlots

EvictBeforeDefer ==
    ~falseDefer

=============================================================================