@gmitch215/folio 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. package/README.md +78 -810
  2. package/dist/boot.d.ts +1 -0
  3. package/dist/boot.js +1 -0
  4. package/dist/browser/index.d.ts +73 -0
  5. package/dist/browser/index.js +277 -0
  6. package/dist/browser.d.ts +1 -68
  7. package/dist/browser.js +1 -274
  8. package/dist/continuation.d.ts +1 -0
  9. package/dist/continuation.js +1 -0
  10. package/dist/{compiler.d.ts → execution/compiler.d.ts} +1 -1
  11. package/dist/{compiler.js → execution/compiler.js} +5 -12
  12. package/dist/{coordinator.d.ts → execution/coordinator.d.ts} +1 -1
  13. package/dist/{coordinator.js → execution/coordinator.js} +5 -5
  14. package/dist/{extract.d.ts → execution/extract.d.ts} +3 -13
  15. package/dist/{extract.js → execution/extract.js} +7 -8
  16. package/dist/{fold.d.ts → execution/fold.d.ts} +15 -3
  17. package/dist/{guards.d.ts → execution/guards.d.ts} +2 -1
  18. package/dist/execution/types.d.ts +221 -0
  19. package/dist/{affine-state.d.ts → folds/affine-state.d.ts} +18 -47
  20. package/dist/{affine-state.js → folds/affine-state.js} +18 -44
  21. package/dist/{affine.d.ts → folds/affine.d.ts} +12 -24
  22. package/dist/{affine.js → folds/affine.js} +8 -17
  23. package/dist/{boundaries.d.ts → folds/boundaries.d.ts} +7 -18
  24. package/dist/{boundaries.js → folds/boundaries.js} +5 -13
  25. package/dist/{certified.d.ts → folds/certified.d.ts} +13 -24
  26. package/dist/{certified.js → folds/certified.js} +12 -26
  27. package/dist/{certify.d.ts → folds/certify.d.ts} +11 -22
  28. package/dist/{certify.js → folds/certify.js} +24 -41
  29. package/dist/{memo.d.ts → footprints/memo.d.ts} +8 -33
  30. package/dist/{memo.js → footprints/memo.js} +8 -2
  31. package/dist/{ranges.js → footprints/ranges.js} +1 -1
  32. package/dist/{read-scan.js → footprints/read-scan.js} +1 -1
  33. package/dist/{read-windows.d.ts → footprints/read-windows.d.ts} +6 -32
  34. package/dist/{read-windows.js → footprints/read-windows.js} +8 -7
  35. package/dist/index.d.ts +16 -4
  36. package/dist/index.js +14 -2
  37. package/dist/{iteration-admission.d.ts → iteration/admission.d.ts} +2 -38
  38. package/dist/{iteration.d.ts → iteration/index.d.ts} +8 -98
  39. package/dist/{iteration.js → iteration/index.js} +28 -28
  40. package/dist/{inline.js → iteration/inline.js} +2 -9
  41. package/dist/{iteration-owner.d.ts → iteration/owner.d.ts} +3 -3
  42. package/dist/{iteration-owner.js → iteration/owner.js} +4 -3
  43. package/dist/{iteration-worker.js → iteration/worker.js} +3 -3
  44. package/dist/{managed.d.ts → managed/index.d.ts} +3 -3
  45. package/dist/{managed.js → managed/index.js} +7 -9
  46. package/dist/managed/outbox.d.ts +128 -0
  47. package/dist/managed/outbox.js +156 -0
  48. package/dist/managed/pool.d.ts +221 -0
  49. package/dist/managed/pool.js +310 -0
  50. package/dist/managed/tasks.d.ts +154 -0
  51. package/dist/managed/tasks.js +183 -0
  52. package/dist/{bake.d.ts → memory/bake.d.ts} +12 -6
  53. package/dist/{bake.js → memory/bake.js} +9 -7
  54. package/dist/memory/continuation.d.ts +141 -0
  55. package/dist/memory/continuation.js +495 -0
  56. package/dist/memory/elastic-peer.d.ts +20 -0
  57. package/dist/memory/elastic-peer.js +39 -0
  58. package/dist/memory/elastic-sql.d.ts +11 -0
  59. package/dist/memory/elastic-sql.js +35 -0
  60. package/dist/memory/elastic.d.ts +130 -0
  61. package/dist/memory/elastic.js +475 -0
  62. package/dist/memory/growth-governor.d.ts +16 -0
  63. package/dist/memory/growth-governor.js +42 -0
  64. package/dist/memory/imports.d.ts +10 -0
  65. package/dist/memory/imports.js +89 -0
  66. package/dist/{iteration-layout.d.ts → memory/iteration-layout.d.ts} +1 -1
  67. package/dist/{pages.d.ts → memory/pages.d.ts} +3 -3
  68. package/dist/memory.d.ts +5 -10
  69. package/dist/memory.js +5 -89
  70. package/dist/{context.d.ts → node/context.d.ts} +1 -1
  71. package/dist/{host.d.ts → node/host.d.ts} +1 -1
  72. package/dist/{host.js → node/host.js} +2 -2
  73. package/dist/{node.d.ts → node/index.d.ts} +4 -49
  74. package/dist/{node.js → node/index.js} +16 -17
  75. package/dist/{tasks.d.ts → node/tasks.d.ts} +2 -13
  76. package/dist/{worker.js → node/worker.js} +2 -2
  77. package/dist/{ownership.js → ownership/index.js} +7 -10
  78. package/dist/{ownership-redirect.d.ts → ownership/redirect.d.ts} +2 -2
  79. package/dist/{ownership-redirect.js → ownership/redirect.js} +2 -2
  80. package/dist/{ownership-slices.d.ts → ownership/slices.d.ts} +3 -3
  81. package/dist/{ownership-slices.js → ownership/slices.js} +3 -3
  82. package/dist/{frames.d.ts → remote/frames.d.ts} +1 -1
  83. package/dist/{frames.js → remote/frames.js} +2 -2
  84. package/dist/{remote.d.ts → remote/index.d.ts} +7 -2
  85. package/dist/{remote.js → remote/index.js} +7 -2
  86. package/dist/{router.d.ts → routing/index.d.ts} +3 -3
  87. package/dist/{router.js → routing/index.js} +19 -15
  88. package/dist/{router-worker.js → routing/worker.js} +1 -1
  89. package/dist/{slices.d.ts → slices/index.d.ts} +2 -2
  90. package/dist/{slices.js → slices/index.js} +5 -5
  91. package/dist/slices/worker.d.ts +1 -0
  92. package/dist/{slices-worker.js → slices/worker.js} +2 -2
  93. package/dist/workers.d.ts +5 -0
  94. package/dist/workers.js +5 -0
  95. package/package.json +30 -80
  96. /package/dist/{browser-worker.d.ts → browser/worker.d.ts} +0 -0
  97. /package/dist/{browser-worker.js → browser/worker.js} +0 -0
  98. /package/dist/{fold.js → execution/fold.js} +0 -0
  99. /package/dist/{guards.js → execution/guards.js} +0 -0
  100. /package/dist/{import-order.d.ts → execution/import-order.d.ts} +0 -0
  101. /package/dist/{import-order.js → execution/import-order.js} +0 -0
  102. /package/dist/{runs.d.ts → execution/runs.d.ts} +0 -0
  103. /package/dist/{runs.js → execution/runs.js} +0 -0
  104. /package/dist/{transaction.d.ts → execution/transaction.d.ts} +0 -0
  105. /package/dist/{transaction.js → execution/transaction.js} +0 -0
  106. /package/dist/{iteration-worker.d.ts → execution/types.js} +0 -0
  107. /package/dist/{ranges.d.ts → footprints/ranges.d.ts} +0 -0
  108. /package/dist/{read-scan.d.ts → footprints/read-scan.d.ts} +0 -0
  109. /package/dist/{iteration-admission.js → iteration/admission.js} +0 -0
  110. /package/dist/{inline.d.ts → iteration/inline.d.ts} +0 -0
  111. /package/dist/{worker.d.ts → iteration/worker.d.ts} +0 -0
  112. /package/dist/{iteration-layout.js → memory/iteration-layout.js} +0 -0
  113. /package/dist/{lease.d.ts → memory/lease.d.ts} +0 -0
  114. /package/dist/{lease.js → memory/lease.js} +0 -0
  115. /package/dist/{pages.js → memory/pages.js} +0 -0
  116. /package/dist/{context.js → node/context.js} +0 -0
  117. /package/dist/{tasks.js → node/tasks.js} +0 -0
  118. /package/dist/{router-worker.d.ts → node/worker.d.ts} +0 -0
  119. /package/dist/{ownership.d.ts → ownership/index.d.ts} +0 -0
  120. /package/dist/{values.d.ts → remote/values.d.ts} +0 -0
  121. /package/dist/{values.js → remote/values.js} +0 -0
  122. /package/dist/{slices-worker.d.ts → routing/worker.d.ts} +0 -0
@@ -0,0 +1,221 @@
1
+ /** Why the rest of a loop ran serially after the pooled path was tried or refused. */
2
+ export type IterationStop = 'no-admission' | 'log-exhausted' | 'frame-carried dependence' | 'dependency' | 'stalled';
3
+ /** What the admission decision saw and chose for one owner call's loop. */
4
+ export interface IterationAdmission {
5
+ /** Time of iteration 0 on the owner thread, on a plain instance and the real frame. */
6
+ plainIterationMs: number;
7
+ /** Serial time of the iterations after iteration 0, from `plainIterationMs`. */
8
+ remainingSerialMs: number;
9
+ /** The least remaining serial time worth dispatching: `fixedMs * margin`. */
10
+ floorMs: number;
11
+ /** The decision before the first dispatch. A later stop is `IterationReport.stopped`. */
12
+ admitted: boolean;
13
+ /** Wall of the last batch, dispatch to the end of its replays. */
14
+ batchWallMs?: number;
15
+ /** The serial time of the iterations of that batch, from the online estimate. */
16
+ batchSerialMs?: number;
17
+ /** The latest estimate of serial time per iteration, from iteration 0, replays or workers. */
18
+ estimateMs?: number;
19
+ /** Set when a refused loop was admitted after timed serial iterations; the iteration index. */
20
+ readmittedAt?: number;
21
+ /** Admitted as a probe: under the floor, over `probeFraction` of it, one timed batch decides. */
22
+ probe?: boolean;
23
+ /** The probe batch did not beat the serial time of its iterations, and the dispatch ended. */
24
+ probeLost?: boolean;
25
+ /** One entry per batch, only under `admissionSeam.trace`. */
26
+ trace?: AdmissionStep[];
27
+ }
28
+ /** What the estimate and the batch rule saw after one batch. @internal */
29
+ export interface AdmissionStep {
30
+ first: number;
31
+ count: number;
32
+ windows: boolean;
33
+ trackedMeanMs: number;
34
+ kappa?: number;
35
+ estimateMs: number;
36
+ wallMs: number;
37
+ serialMs: number;
38
+ }
39
+ /** Why the iteration route is not used, in `ExecutionReport.reason`. */
40
+ export type IterationReason = 'no-arena' | 'memory-size-observed' | 'import-order' | 'workers-unavailable' | 'trip-zero' | 'no-admission' | 'log-exhausted' | 'frame-carried dependence' | 'stalled';
41
+ /**
42
+ * The read windows of one loop. The strided window moves with the iteration index: iteration `k`
43
+ * reads from the granule boundary at or below `low + (k - index) * stride`. The fixed window is the
44
+ * same range in every iteration. Neither has to be right for a run to be correct: a load outside
45
+ * the window is tracked as usual, and a window only costs a false conflict when another iteration
46
+ * writes inside it.
47
+ */
48
+ export interface IterationReadWindows {
49
+ strided?: {
50
+ index: number;
51
+ low: number;
52
+ stride: number;
53
+ bytes: number;
54
+ };
55
+ fixed?: IterationReadWindow;
56
+ }
57
+ /**
58
+ * Loads of at most 8 bytes of one run on the module with read windows, by where they landed:
59
+ * inside a window, tracked outside every window, or exempt (the slot's own callee stack).
60
+ */
61
+ export interface ReadWindowCounts {
62
+ hits: number;
63
+ misses: number;
64
+ exempt: number;
65
+ }
66
+ /** A byte range an iteration reads without per-access marks. */
67
+ export interface IterationReadWindow {
68
+ low: number;
69
+ bytes: number;
70
+ }
71
+ /** What a run did. `regions`, `replayed`, `fallbacks` and `workers` parallel `ExecutionReport`. */
72
+ export interface IterationReport {
73
+ /** Workers that took part. */
74
+ workers: number;
75
+ /** Iterations run on workers, the regions of this mode. */
76
+ regions: number;
77
+ /** Dispatched iterations that were rolled back and rerun serially at the owner. */
78
+ replayed: number;
79
+ /** Iterations whose writes were undone before the rerun. */
80
+ rolledBack: number;
81
+ /** Batches whose first failure was a trap or an exhausted undo log, not a dependency. */
82
+ fallbacks: number;
83
+ /** Iterations that trapped on a worker. */
84
+ traps: number;
85
+ /** Iterations that exhausted their undo log. */
86
+ exhausted: number;
87
+ /** Iterations that trapped on an access to another iteration's stack slot. */
88
+ violations: number;
89
+ /** Batches in which the stall guard fired. */
90
+ stalls: number;
91
+ /** Iterations the stall word stopped, rolled back and rerun serially. */
92
+ stalledIterations: number;
93
+ batches: number;
94
+ failedBatches: number;
95
+ undoEntries: number;
96
+ /** The most undo entries one completed iteration logged. */
97
+ maxLogEntries: number;
98
+ /** Times a batch that exhausted its log was retried with the grown log. */
99
+ logGrowths: number;
100
+ /** Iterations rolled back and dispatched again by such a retry. They are not in `regions`. */
101
+ retried: number;
102
+ /** Ordered iteration pairs by hazard. */
103
+ conflicts: {
104
+ raw: number;
105
+ war: number;
106
+ waw: number;
107
+ frame: number;
108
+ };
109
+ /** From posting a batch to the last worker reply. */
110
+ dispatchMs: number;
111
+ validationMs: number;
112
+ rollbackMs: number;
113
+ replayMs: number;
114
+ /** Wasm time inside workers, summed. */
115
+ workerRunMs: number;
116
+ /** Bitmap scan and clear time inside workers, summed. */
117
+ workerScanMs: number;
118
+ /** Time the workers spent marking read windows before their iterations, summed. */
119
+ workerMarkMs: number;
120
+ /** Read bitmap words the workers scanned, summed. */
121
+ workerScanWords: number;
122
+ /** Read bitmap words they found non-zero. */
123
+ workerScanSet: number;
124
+ /**
125
+ * Iterations the owner ran as the serial program without dispatching them: iteration 0, the
126
+ * rest of a loop after a stop, or all of them when admission refused the loop.
127
+ */
128
+ serial: number;
129
+ /** What ended the dispatch early, or why admission refused the loop. */
130
+ stopped?: IterationStop;
131
+ /** What the admission callback saw and chose; set when the job had one. */
132
+ admission?: IterationAdmission;
133
+ /** Frame conflicts that fell in a window's bytes; they are also in `conflicts.frame`. */
134
+ windowConflicts: number;
135
+ /** The window the batches ran on, if any, and where it came from. */
136
+ window?: {
137
+ low: number;
138
+ end: number;
139
+ source: 'given' | 'discovered';
140
+ /** Batches dispatched on it. */
141
+ batches: number;
142
+ };
143
+ /** Why a window that was offered or found was not used. */
144
+ windowRefused?: string;
145
+ /** The read windows learned from the first clean batch, with the batches that ran on them. */
146
+ readWindows?: IterationReadWindows & {
147
+ batches: number;
148
+ learnedFrom: number;
149
+ };
150
+ /** Why read windows were not learned, or were dropped after a batch on them conflicted. */
151
+ readWindowRefused?: string;
152
+ /** What the counted batch on the read windows saw, and whether they were armed for the loop. */
153
+ readWindowGate?: {
154
+ iterations: number;
155
+ counts: ReadWindowCounts;
156
+ saved: number;
157
+ spent: number;
158
+ /** which check the armed module runs first for a load of at most 8 bytes */
159
+ order: 'window' | 'exempt';
160
+ armed: boolean;
161
+ };
162
+ }
163
+ /** Timing and size evidence for one joined task. Timestamps are epoch milliseconds. */
164
+ export interface TaskRecord {
165
+ id: string;
166
+ worker: number;
167
+ name: string;
168
+ submitMs: number;
169
+ startMs: number;
170
+ endMs: number;
171
+ waitMs: number;
172
+ cpuMs?: number;
173
+ inputBytes: number;
174
+ outputBytes: number;
175
+ }
176
+ /** What a run did: results, how it was routed and what the speculation cost. */
177
+ export interface ExecutionReport {
178
+ /** Return values of the call, or of each call in a batch. */
179
+ values: unknown[];
180
+ /** Workers that executed regions; zero for a serial run. */
181
+ workers: number;
182
+ /** Regions discovered and dispatched. */
183
+ regions: number;
184
+ /** Regions that conflicted and replayed against committed state. */
185
+ replayed: number;
186
+ /** Regions or batches that fell back to native execution at the owner. */
187
+ fallbacks: number;
188
+ /** `native-shared` ran proved kernels on the shared allocation without tracking. */
189
+ mode: 'serial' | 'speculative' | 'native-shared';
190
+ /** Set when the control-flow extractor or the iteration route produced the regions. */
191
+ extraction?: 'control-flow' | 'iteration';
192
+ /** `shared` regions read the physical allocation; `snapshot` ran on a copied arena. */
193
+ memoryMode?: 'shared' | 'snapshot';
194
+ /** Bytes copied into the worker group for snapshot execution. */
195
+ snapshotBytes?: number;
196
+ /** Why a run stayed serial or left the fast path. */
197
+ reason?: 'requested' | 'small-workload' | 'no-parallel-plan' | 'single-region' | 'unsupported-module' | 'dependency' | 'guard-failed' | 'snapshot-unavailable' | 'previous-conflicts' | 'opaque-input' | IterationReason;
198
+ /** The iteration executor's counts, summed over the owner calls of the run. */
199
+ iteration?: IterationReport;
200
+ /** Present when `FolioOptions.profile` is set. */
201
+ profile?: ExecutionProfile;
202
+ }
203
+ /** Opt-in timings. Process CPU is measured once at the coordinator, not summed from these. */
204
+ export interface ExecutionProfile {
205
+ /** Elapsed time of the whole run. */
206
+ wallMs: number;
207
+ /** Elapsed time of each region or batch observation on its worker. */
208
+ workerWallMs: number[];
209
+ /** Thread CPU time of each observation. */
210
+ workerCpuMs: number[];
211
+ /** Ordered dependency validation. */
212
+ validationMs: number;
213
+ /** Committing validated writes to canonical memory. */
214
+ commitMs: number;
215
+ /** Re-executing conflicted regions. */
216
+ replayMs: number;
217
+ /** Bytes tracked as read by speculative regions. */
218
+ readBytes: number;
219
+ /** Bytes tracked as written by speculative regions. */
220
+ writeBytes: number;
221
+ }
@@ -1,6 +1,6 @@
1
+ import { type FoldChunk } from '../execution/fold.ts';
2
+ import { type TaskLink, type TaskPool } from '../node/tasks.ts';
1
3
  import { type AffineCheck, type AffineRing } from './affine.ts';
2
- import { type FoldChunk } from './fold.ts';
3
- import { type TaskLink, type TaskPool } from './tasks.ts';
4
4
  /** Where the probe puts the context and the data in the kernel's linear memory. */
5
5
  export declare const CONTEXT_ADDRESS: number;
6
6
  export declare const DATA_ADDRESS: number;
@@ -13,11 +13,7 @@ export interface ContextLayout {
13
13
  /** Bytes of the context the probe examines and every task copies. At most 256. */
14
14
  window: number;
15
15
  }
16
- /**
17
- * What a worker needs to run a context kernel: the module, the parameter layout, the context image
18
- * the caller initialised, and which bytes of it are state. Every other byte of the image is held at
19
- * its value in `context`.
20
- */
16
+ /** Inputs a worker needs to run a context kernel: module, layout, image and state bytes. */
21
17
  export interface ContextSubject {
22
18
  module: WebAssembly.Module | Uint8Array;
23
19
  layout: ContextLayout;
@@ -36,7 +32,7 @@ export interface StateFactor {
36
32
  words: number;
37
33
  /** Bits per word. For `gf2` it is the width of the whole factor. */
38
34
  bits: number;
39
- /** `gf2`: the columns of the one-unit bit matrix. `z2w`: the words x words matrix over Z/2^bits. */
35
+ /** `gf2`: columns of the one-unit bit matrix. `z2w`: words x words matrix over Z/2^bits. */
40
36
  operator: bigint[] | bigint[][];
41
37
  }
42
38
  export interface StateCheck extends AffineCheck {
@@ -106,17 +102,10 @@ export declare class ContextKernel {
106
102
  raw(args: (number | bigint)[]): number | bigint;
107
103
  /** Copies of every byte of memory outside the given ranges, for the hidden state check. */
108
104
  outside(ranges: [number, number][]): Uint8Array;
109
- /**
110
- * One call: the context image at the context address, the data at the data address. Returns the
111
- * context window afterwards, or undefined when the kernel traps.
112
- */
105
+ /** One call. Returns the context window afterwards, or undefined when the kernel traps. */
113
106
  run(layout: ContextLayout, image: Uint8Array, data: Uint8Array): Uint8Array | undefined;
114
107
  }
115
- /**
116
- * The context image after one call of an init function, for a subject whose state starts from
117
- * something other than zeros. `args` gets the context address; `inputs` is placed at the data
118
- * address first, for init functions that take a key or an IV.
119
- */
108
+ /** The context image after one init call. `inputs` is placed at the data address first. */
120
109
  export declare function initialContext(module: WebAssembly.Module | Uint8Array, entry: string, args: (ctx: number, data: number) => (number | bigint)[], inputs?: Uint8Array, window?: number): Uint8Array;
121
110
  /** A subject whose state is the whole window, for chains with no model. */
122
111
  export declare function contextSubject(module: WebAssembly.Module | Uint8Array, entry: string, context: Uint8Array, layout?: Partial<ContextLayout>): ContextSubject;
@@ -126,46 +115,28 @@ export declare const pick: (image: Uint8Array, offsets: number[]) => Uint8Array;
126
115
  export declare function toHex(bytes: Uint8Array): string;
127
116
  export declare function fromHex(hex: string): Uint8Array;
128
117
  export declare const bytesEqual: (a: Uint8Array, b: Uint8Array) => boolean;
129
- /**
130
- * The state vector after a chunk of `length` bytes from `start`, given `offset`, the state vector
131
- * after the same chunk from zero: A(length) start + offset, factor by factor. `length` must be a
132
- * multiple of `model.align`.
133
- */
118
+ /** Next state after `length` bytes from `start`: A(length) start + offset, factor by factor. */
134
119
  export declare function predictState(model: Pick<StateModel, 'factors' | 'align' | 'state'>, start: Uint8Array, length: number, offset: Uint8Array): Uint8Array;
135
120
  /**
136
- * Probes `entry`, a function (context pointer, data pointer, length) that updates a context struct
137
- * in place, and decides by trial what its state is and whether the state transition is affine.
121
+ * Probes `entry`, a (context, data, length) function that updates a context in place, and returns
122
+ * the affine model of its state bytes or the reason there is none. State is the bytes a call both
123
+ * reads and writes; bytes only read are parameters. Each independent factor is checked over GF(2)
124
+ * and over Z/2^bits, and a factor affine in no ring refuses the whole context.
138
125
  *
139
- * It finds the parameter order under which the call composes over a split, then, from contexts the
140
- * function itself produced, flips each of the first `window` context bytes and records which bytes
141
- * the result depends on. State is the set of bytes a call both reads and writes. A byte that is read
142
- * and never written is a parameter and stays at its value in `options.context`. State bytes that
143
- * do not depend on each other form independent factors. Each factor is tested over GF(2) and over
144
- * Z/2^bits (as one little or big endian integer, or as 32-bit words coupled by a matrix) with the
145
- * five checks of `probeAffine`, with the other factors set to random bytes in every call, which
146
- * tests that they do not interact. A factor that is affine in no ring refuses the whole context.
147
- *
148
- * A pass is evidence on the sampled inputs only. It cannot see state outside the window, a byte
149
- * that is read only on rare inputs, or behaviour above the probed lengths. `foldState` in exact
150
- * mode does not trust the model, but its guarantee needs the state to be complete.
126
+ * Evidence is sampled: state outside the window is not seen, so `foldState` in exact mode is what
127
+ * makes a result trustworthy.
151
128
  */
152
129
  export declare function probeState(module: WebAssembly.Module | Uint8Array, entry: string, options?: StateProbeOptions): StateModel | StateRefusal;
153
130
  /**
154
- * Runs on the worker side. Answers `ctx:<hex state>` with the end state bytes, and
155
- * `ctxrec:<hex state>:<step>` with the state after every `step` bytes, concatenated, the last one
156
- * being the end state. The context image is rebuilt from `subject.context` for every task.
131
+ * Worker side: answers `ctx:<hex state>` with the end state, and `ctxrec:<hex state>:<step>` with
132
+ * the state every `step` bytes. Each task rebuilds the image from `subject.context`.
157
133
  */
158
134
  export declare function serveContext(link: TaskLink, subject: ContextSubject): void;
159
135
  /** The serial reference: one call over all the bytes on a private instance. */
160
136
  export declare function stateSerial(subject: ContextSubject): (input: Uint8Array, state?: Uint8Array) => Uint8Array;
161
137
  /**
162
- * `foldAffine` exact mode for a state vector in the context: pass 1 runs every chunk but the last
163
- * from the zero state, the model turns the offsets into predicted start vectors, pass 2 runs
164
- * chunks 1 to n-1 from them in workers started with `serveContext`, and each end vector is compared
165
- * with the next predicted start. All equal: the last end vector is the serial state, by induction,
166
- * whatever the model got wrong. The first difference hands the rest to one serial task.
167
- *
168
- * The result is the state bytes only. Bytes a call writes and does not read (`model.scratch`, and
169
- * the data a call changes in place) are not returned.
138
+ * Exact fold of a context state: pass 1 runs each chunk from zero, pass 2 from predicted starts,
139
+ * and each end is checked against the next start. The first mismatch runs the rest serially.
140
+ * Returns the state bytes only; scratch bytes a call writes and never reads are not returned.
170
141
  */
171
142
  export declare function foldState(pool: TaskPool, input: Uint8Array, model: StateModel, options?: StateFoldOptions): StateFoldResult;
@@ -1,6 +1,6 @@
1
- import { foldExact, gfApply, gfMul, mulberry } from "./affine.js";
2
- import { chunkRanges } from "./fold.js";
3
- import { serveTasks } from "./tasks.js";
1
+ import { chunkRanges } from "../execution/fold.js";
2
+ import { serveTasks } from "../node/tasks.js";
3
+ import { foldExact, gfApply, gfMul, mask, mulberry } from "./affine.js";
4
4
  /** Where the probe puts the context and the data in the kernel's linear memory. */
5
5
  export const CONTEXT_ADDRESS = 1 << 19;
6
6
  export const DATA_ADDRESS = 1 << 20;
@@ -60,10 +60,7 @@ export class ContextKernel {
60
60
  all.fill(0, from, Math.min(to, all.length));
61
61
  return all;
62
62
  }
63
- /**
64
- * One call: the context image at the context address, the data at the data address. Returns the
65
- * context window afterwards, or undefined when the kernel traps.
66
- */
63
+ /** One call. Returns the context window afterwards, or undefined when the kernel traps. */
67
64
  run(layout, image, data) {
68
65
  this.put(CONTEXT_ADDRESS, image);
69
66
  this.put(DATA_ADDRESS, data);
@@ -82,11 +79,7 @@ export class ContextKernel {
82
79
  return this.get(CONTEXT_ADDRESS, layout.window);
83
80
  }
84
81
  }
85
- /**
86
- * The context image after one call of an init function, for a subject whose state starts from
87
- * something other than zeros. `args` gets the context address; `inputs` is placed at the data
88
- * address first, for init functions that take a key or an IV.
89
- */
82
+ /** The context image after one init call. `inputs` is placed at the data address first. */
90
83
  export function initialContext(module, entry, args, inputs = new Uint8Array(0), window = MAX_WINDOW) {
91
84
  const kernel = new ContextKernel(module, entry);
92
85
  kernel.put(DATA_ADDRESS, inputs);
@@ -123,7 +116,6 @@ export function fromHex(hex) {
123
116
  return out;
124
117
  }
125
118
  export const bytesEqual = (a, b) => a.length === b.length && a.every((v, i) => v === b[i]);
126
- const wordMask = (f) => (1n << BigInt(f.bits)) - 1n;
127
119
  function toWords(f, bytes) {
128
120
  const out = [];
129
121
  for (let w = 0; w < f.words; w++) {
@@ -145,7 +137,7 @@ function fromWords(f, words) {
145
137
  }
146
138
  return out;
147
139
  }
148
- const plus = (f, a, b) => f.ring === 'gf2' ? [a[0] ^ b[0]] : a.map((x, i) => (x + b[i]) & wordMask(f));
140
+ const plus = (f, a, b) => f.ring === 'gf2' ? [a[0] ^ b[0]] : a.map((x, i) => (x + b[i]) & mask(f.bits));
149
141
  const minus = (f, a, b) => f.ring === 'gf2' ? [a[0] ^ b[0]] : a.map((x, i) => BigInt.asUintN(f.bits, x - b[i]));
150
142
  function applyOperator(f, op, v) {
151
143
  if (f.ring === 'gf2')
@@ -185,11 +177,7 @@ function powerOf(f, units) {
185
177
  cache.byUnits.set(units, result);
186
178
  return result;
187
179
  }
188
- /**
189
- * The state vector after a chunk of `length` bytes from `start`, given `offset`, the state vector
190
- * after the same chunk from zero: A(length) start + offset, factor by factor. `length` must be a
191
- * multiple of `model.align`.
192
- */
180
+ /** Next state after `length` bytes from `start`: A(length) start + offset, factor by factor. */
193
181
  export function predictState(model, start, length, offset) {
194
182
  if (!Number.isSafeInteger(length) || length < 0 || length % model.align)
195
183
  throw new RangeError(`length ${length} is not a multiple of ${model.align}`);
@@ -244,21 +232,13 @@ function shapesFor(offsets) {
244
232
  return shapes;
245
233
  }
246
234
  /**
247
- * Probes `entry`, a function (context pointer, data pointer, length) that updates a context struct
248
- * in place, and decides by trial what its state is and whether the state transition is affine.
249
- *
250
- * It finds the parameter order under which the call composes over a split, then, from contexts the
251
- * function itself produced, flips each of the first `window` context bytes and records which bytes
252
- * the result depends on. State is the set of bytes a call both reads and writes. A byte that is read
253
- * and never written is a parameter and stays at its value in `options.context`. State bytes that
254
- * do not depend on each other form independent factors. Each factor is tested over GF(2) and over
255
- * Z/2^bits (as one little or big endian integer, or as 32-bit words coupled by a matrix) with the
256
- * five checks of `probeAffine`, with the other factors set to random bytes in every call, which
257
- * tests that they do not interact. A factor that is affine in no ring refuses the whole context.
235
+ * Probes `entry`, a (context, data, length) function that updates a context in place, and returns
236
+ * the affine model of its state bytes or the reason there is none. State is the bytes a call both
237
+ * reads and writes; bytes only read are parameters. Each independent factor is checked over GF(2)
238
+ * and over Z/2^bits, and a factor affine in no ring refuses the whole context.
258
239
  *
259
- * A pass is evidence on the sampled inputs only. It cannot see state outside the window, a byte
260
- * that is read only on rare inputs, or behaviour above the probed lengths. `foldState` in exact
261
- * mode does not trust the model, but its guarantee needs the state to be complete.
240
+ * Evidence is sampled: state outside the window is not seen, so `foldState` in exact mode is what
241
+ * makes a result trustworthy.
262
242
  */
263
243
  export function probeState(module, entry, options = {}) {
264
244
  const trials = options.trials ?? 32;
@@ -647,9 +627,8 @@ function probe(kernel, template, checks, refuse, trials, align, seed) {
647
627
  };
648
628
  }
649
629
  /**
650
- * Runs on the worker side. Answers `ctx:<hex state>` with the end state bytes, and
651
- * `ctxrec:<hex state>:<step>` with the state after every `step` bytes, concatenated, the last one
652
- * being the end state. The context image is rebuilt from `subject.context` for every task.
630
+ * Worker side: answers `ctx:<hex state>` with the end state, and `ctxrec:<hex state>:<step>` with
631
+ * the state every `step` bytes. Each task rebuilds the image from `subject.context`.
653
632
  */
654
633
  export function serveContext(link, subject) {
655
634
  const kernel = new ContextKernel(subject.module, subject.layout.entry);
@@ -696,14 +675,9 @@ export function stateSerial(subject) {
696
675
  };
697
676
  }
698
677
  /**
699
- * `foldAffine` exact mode for a state vector in the context: pass 1 runs every chunk but the last
700
- * from the zero state, the model turns the offsets into predicted start vectors, pass 2 runs
701
- * chunks 1 to n-1 from them in workers started with `serveContext`, and each end vector is compared
702
- * with the next predicted start. All equal: the last end vector is the serial state, by induction,
703
- * whatever the model got wrong. The first difference hands the rest to one serial task.
704
- *
705
- * The result is the state bytes only. Bytes a call writes and does not read (`model.scratch`, and
706
- * the data a call changes in place) are not returned.
678
+ * Exact fold of a context state: pass 1 runs each chunk from zero, pass 2 from predicted starts,
679
+ * and each end is checked against the next start. The first mismatch runs the rest serially.
680
+ * Returns the state bytes only; scratch bytes a call writes and never reads are not returned.
707
681
  */
708
682
  export function foldState(pool, input, model, options = {}) {
709
683
  const initial = options.initial ?? pick(model.context, model.state);
@@ -1,8 +1,8 @@
1
- import { type FoldChunk } from './fold.ts';
2
- import { type TaskLink, type TaskPool } from './tasks.ts';
1
+ import { type FoldChunk } from '../execution/fold.ts';
2
+ import { type TaskLink, type TaskPool } from '../node/tasks.ts';
3
3
  /** `gf2` is affine over GF(2)^bits (xor), `z2w` is affine over Z/2^bits (add, multiply). */
4
4
  export type AffineRing = 'gf2' | 'z2w';
5
- /** The model of a certificate may also be `zp`: two 16-bit digits of the state, each a residue mod p. */
5
+ /** The model may also be `zp`: two 16-bit digits of the state, each a residue mod p. */
6
6
  export type AffineModelRing = AffineRing | 'zp';
7
7
  /** Where the state, the pointer and the length sit in the entry's parameter list. */
8
8
  export interface AffineLayout {
@@ -12,7 +12,7 @@ export interface AffineLayout {
12
12
  len: number;
13
13
  bits: 32 | 64;
14
14
  }
15
- /** One check the probe ran. `trials` counts the trials run, which is fewer than asked on a failure. */
15
+ /** One check the probe ran. `trials` is below the number asked when a check fails early. */
16
16
  export interface AffineCheck {
17
17
  ring: AffineRing;
18
18
  name: string;
@@ -20,11 +20,8 @@ export interface AffineCheck {
20
20
  passed: boolean;
21
21
  }
22
22
  /**
23
- * The recovered model: T(s, x) = A(|x|) s + T(0, x) over `ring`, with A(|x|) the `align`-byte
24
- * operator raised to the power |x| / align. `operator` is the columns of that operator over GF(2),
25
- * or its multiplier over Z/2^bits. Over `zp` the state is `digits` digits of `bits / digits` bits,
26
- * each reduced mod `modulus` after a call of at least one byte, and `operator` is the digits by
27
- * digits matrix mod `modulus` in row order.
23
+ * A recovered transition T(s, x) = A(|x|) s + T(0, x) over `ring`. `operator` is the one-`align`
24
+ * byte operator A: GF(2) columns, a Z/2^bits multiplier, or for `zp` a digits-by-digits matrix.
28
25
  */
29
26
  export interface AffineModel {
30
27
  kind: 'model';
@@ -90,30 +87,21 @@ export declare function mulberry(seed: number): () => number;
90
87
  export declare function gfApply(columns: bigint[], vector: bigint): bigint;
91
88
  export declare const gfMul: (a: bigint[], b: bigint[]) => bigint[];
92
89
  /**
93
- * The state-dependent part of T(state, x) for a chunk of `length` bytes: A(length) state. The
94
- * operator comes from repeated squaring and is cached per length. `length` must be a multiple of
95
- * `model.align`. Over `zp` a zero length leaves the state as it is and any other length reduces
96
- * every digit.
90
+ * The state-dependent part of T(state, x) for `length` bytes: A(length) state. `length` must be a
91
+ * multiple of `model.align`. Over `zp` a zero length leaves the state unchanged.
97
92
  */
98
93
  export declare function advance(model: AffineModel, state: bigint, length: number): bigint;
99
94
  /** T(start, x) predicted from `offset` = T(0, x): A(length) start combined with the offset. */
100
95
  export declare function predictEnd(model: AffineModel, start: bigint, length: number, offset: bigint): bigint;
101
96
  /**
102
- * Probes `entry`, a function (state, pointer, length) -> state, in a private instance of `module`
103
- * and decides by trial whether its state transition is affine over GF(2)^bits or over Z/2^bits.
104
- * It recovers the parameter roles, checks affinity, data independence, homogeneity (the operator
105
- * for L bytes is the L-th power of the one-unit operator), additivity and streaming composition,
106
- * and returns the model or the check that failed. Every call into the kernel is counted.
107
- * A pass is evidence on the sampled inputs only; `foldAffine` in `exact` mode is what makes the
108
- * result independent of the probe.
97
+ * Probes `entry`, a (state, pointer, length) -> state function, by trial. Returns the affine model
98
+ * or the check that failed. A pass is evidence on the sampled inputs only; `foldAffine` in `exact`
99
+ * mode is what makes the result independent of the probe.
109
100
  */
110
101
  export declare function probeAffine(module: WebAssembly.Module | Uint8Array, entry: string, options?: AffineProbeOptions): (AffineModel & {
111
102
  ring: AffineRing;
112
103
  }) | AffineRefusal;
113
- /**
114
- * Runs on the worker side. Instantiates the kernel privately and answers `affine:<hex state>`
115
- * tasks: the input is the chunk, the output is the 8-byte little-endian end state.
116
- */
104
+ /** Worker side: answers `affine:<hex state>` tasks with the 8-byte little-endian end state. */
117
105
  export declare function serveAffine(link: TaskLink, subject: AffineSubject): void;
118
106
  /** The serial reference: one call over all the bytes in a private instance. */
119
107
  export declare function affineSerial(subject: AffineSubject): (input: Uint8Array, state?: bigint) => bigint;
@@ -1,5 +1,5 @@
1
- import { chunkRanges } from "./fold.js";
2
- import { serveTasks } from "./tasks.js";
1
+ import { chunkRanges } from "../execution/fold.js";
2
+ import { serveTasks } from "../node/tasks.js";
3
3
  /** The task name `serveAffine` answers: `affine:<start state in hex>`. */
4
4
  export const AFFINE_TASK = 'affine';
5
5
  const DATA = 1 << 20;
@@ -153,10 +153,8 @@ function residuePower(model, units) {
153
153
  return result;
154
154
  }
155
155
  /**
156
- * The state-dependent part of T(state, x) for a chunk of `length` bytes: A(length) state. The
157
- * operator comes from repeated squaring and is cached per length. `length` must be a multiple of
158
- * `model.align`. Over `zp` a zero length leaves the state as it is and any other length reduces
159
- * every digit.
156
+ * The state-dependent part of T(state, x) for `length` bytes: A(length) state. `length` must be a
157
+ * multiple of `model.align`. Over `zp` a zero length leaves the state unchanged.
160
158
  */
161
159
  export function advance(model, state, length) {
162
160
  if (!Number.isSafeInteger(length) || length < 0 || length % model.align)
@@ -189,13 +187,9 @@ export function predictEnd(model, start, length, offset) {
189
187
  }
190
188
  // #endregion
191
189
  /**
192
- * Probes `entry`, a function (state, pointer, length) -> state, in a private instance of `module`
193
- * and decides by trial whether its state transition is affine over GF(2)^bits or over Z/2^bits.
194
- * It recovers the parameter roles, checks affinity, data independence, homogeneity (the operator
195
- * for L bytes is the L-th power of the one-unit operator), additivity and streaming composition,
196
- * and returns the model or the check that failed. Every call into the kernel is counted.
197
- * A pass is evidence on the sampled inputs only; `foldAffine` in `exact` mode is what makes the
198
- * result independent of the probe.
190
+ * Probes `entry`, a (state, pointer, length) -> state function, by trial. Returns the affine model
191
+ * or the check that failed. A pass is evidence on the sampled inputs only; `foldAffine` in `exact`
192
+ * mode is what makes the result independent of the probe.
199
193
  */
200
194
  export function probeAffine(module, entry, options = {}) {
201
195
  const trials = options.trials ?? 32;
@@ -407,10 +401,7 @@ function probe(kernel, checks, refuse, trials, align, seed) {
407
401
  }
408
402
  return refuse(`not affine over either ring: failed ${failures.join(' and ')}`);
409
403
  }
410
- /**
411
- * Runs on the worker side. Instantiates the kernel privately and answers `affine:<hex state>`
412
- * tasks: the input is the chunk, the output is the 8-byte little-endian end state.
413
- */
404
+ /** Worker side: answers `affine:<hex state>` tasks with the 8-byte little-endian end state. */
414
405
  export function serveAffine(link, subject) {
415
406
  const kernel = new Kernel(subject.module, subject.layout.entry);
416
407
  kernel.layout = subject.layout;
@@ -1,9 +1,6 @@
1
+ import type { TaskPool } from '../node/tasks.ts';
1
2
  import { type ContextSubject } from './affine-state.ts';
2
- import type { TaskPool } from './tasks.ts';
3
- /**
4
- * The state of a serial chain at every `step` bytes: `starts[k]` is the state before byte
5
- * `k * step`. The caller keeps it and passes it back; nothing is cached globally.
6
- */
3
+ /** States of a serial chain every `step` bytes: `starts[k]` is the state before byte `k * step`. */
7
4
  export interface BoundaryLog {
8
5
  step: number;
9
6
  starts: Uint8Array[];
@@ -36,24 +33,16 @@ export interface VerifyResult {
36
33
  stale: boolean;
37
34
  }
38
35
  /**
39
- * Runs the chain serially on a private instance in the calling thread and records the state at
40
- * every `step` bytes. The copy at each boundary costs a few hundred bytes. The state returned is
41
- * the one a single call over the whole input returns when the kernel composes over a split.
36
+ * Runs the chain serially on a private instance and records the state every `step` bytes. The
37
+ * returned state equals a single call over the whole input when the kernel composes over a split.
42
38
  */
43
39
  export declare function recordBoundaries(subject: ContextSubject, input: Uint8Array, options: RecordOptions): {
44
40
  state: Uint8Array;
45
41
  log: BoundaryLog;
46
42
  };
47
43
  /**
48
- * Runs every chunk of `input` in parallel from the start state the log recorded for it, on workers
49
- * started with `serveContext`, and compares each end state with the next recorded start. If all
50
- * agree, the last end state is the serial result, by induction: each chunk ran from a state that
51
- * the chunk before it ended in. Nothing about the bytes is trusted or hashed, so a log recorded for
52
- * other bytes, other lengths or another chain shows up as a difference and never changes the result.
53
- *
54
- * At the first difference the real end state of that chunk is a real state, and the rest of the
55
- * input runs serially from it in one task that records fresh boundaries; the recorded starts after
56
- * that point are dropped. An edit that changes a chunk's bytes but not its end state (a chain that
57
- * forgets) is not a difference, so the chunks after it stay valid.
44
+ * Runs each chunk in parallel from its recorded start and compares each end with the next start.
45
+ * Nothing about the bytes is hashed, so a stale log shows up as a difference and cannot change the
46
+ * result. At the first difference the rest runs serially and records fresh boundaries.
58
47
  */
59
48
  export declare function verifyBoundaries(pool: TaskPool, subject: ContextSubject, input: Uint8Array, log: BoundaryLog, options?: VerifyOptions): VerifyResult;