File size: 2,513 Bytes
518343a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// IO-budget receipts — FlashAttention's IO-aware discipline re-expressed as
// a typed claim. Every sparse/full attention execution MUST name its
// ioBudgetBytes claim AND its ioConsumedBytes measurement; a sparse plan
// that saves FLOPs but inflates IO is detectable at the receipt-ledger level
// rather than at a wall-clock anomaly downstream.

import type { SparseReceiptCommon } from "./receipts.js";

export interface SparseIoBudgetReceipt extends SparseReceiptCommon {
  readonly receiptClass: "sparse.io.budget.v1";
  readonly regimeRef: string;
  readonly executeReceiptRef: string;
  readonly ioBudgetBytes: number;
  readonly ioConsumedBytes: number;
  readonly withinBudget: true;
}

export interface SparseIoOverrunReceipt extends SparseReceiptCommon {
  readonly receiptClass: "sparse.io.overrun.v1";
  readonly regimeRef: string;
  readonly executeReceiptRef: string;
  readonly ioBudgetBytes: number;
  readonly ioConsumedBytes: number;
  readonly overrunRatio: number; // consumed / budget; > 1 by definition
}

export interface RecordIoInput {
  readonly regimeRef: string;
  readonly executeReceiptRef: string;
  readonly ioBudgetBytes: number;
  readonly ioConsumedBytes: number;
  readonly tenant: string;
  readonly nonce: string;
  readonly issuedAt?: string;
}

export type RecordIoOutput =
  | { overrun: false; receipt: SparseIoBudgetReceipt }
  | { overrun: true; receipt: SparseIoOverrunReceipt };

export function recordIo(input: RecordIoInput): RecordIoOutput {
  const issuedAt = input.issuedAt ?? new Date().toISOString();
  if (input.ioConsumedBytes <= input.ioBudgetBytes) {
    return {
      overrun: false,
      receipt: {
        receiptClass: "sparse.io.budget.v1",
        freshnessNonce: input.nonce,
        issuedAt,
        tenant: input.tenant,
        parentRef: input.regimeRef,
        regimeRef: input.regimeRef,
        executeReceiptRef: input.executeReceiptRef,
        ioBudgetBytes: input.ioBudgetBytes,
        ioConsumedBytes: input.ioConsumedBytes,
        withinBudget: true,
      },
    };
  }
  return {
    overrun: true,
    receipt: {
      receiptClass: "sparse.io.overrun.v1",
      freshnessNonce: input.nonce,
      issuedAt,
      tenant: input.tenant,
      parentRef: input.regimeRef,
      regimeRef: input.regimeRef,
      executeReceiptRef: input.executeReceiptRef,
      ioBudgetBytes: input.ioBudgetBytes,
      ioConsumedBytes: input.ioConsumedBytes,
      overrunRatio: input.ioConsumedBytes / input.ioBudgetBytes,
    },
  };
}