@gmitch215/folio 1.0.0 → 1.1.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 (71) hide show
  1. package/README.md +455 -26
  2. package/dist/affine-state.d.ts +171 -0
  3. package/dist/affine-state.js +763 -0
  4. package/dist/affine.d.ts +160 -0
  5. package/dist/affine.js +571 -0
  6. package/dist/bake.d.ts +8 -3
  7. package/dist/bake.js +5 -2
  8. package/dist/boundaries.d.ts +59 -0
  9. package/dist/boundaries.js +127 -0
  10. package/dist/certified.d.ts +66 -0
  11. package/dist/certified.js +192 -0
  12. package/dist/certify.d.ts +98 -0
  13. package/dist/certify.js +2628 -0
  14. package/dist/compiler.d.ts +37 -1
  15. package/dist/compiler.js +116 -1
  16. package/dist/context.d.ts +47 -0
  17. package/dist/context.js +139 -0
  18. package/dist/coordinator.d.ts +1 -1
  19. package/dist/coordinator.js +54 -5
  20. package/dist/extract.d.ts +114 -1
  21. package/dist/extract.js +1011 -242
  22. package/dist/fold.d.ts +32 -0
  23. package/dist/fold.js +105 -0
  24. package/dist/frames.d.ts +20 -2
  25. package/dist/frames.js +205 -10
  26. package/dist/inline.d.ts +182 -0
  27. package/dist/inline.js +766 -0
  28. package/dist/iteration-admission.d.ts +185 -0
  29. package/dist/iteration-admission.js +201 -0
  30. package/dist/iteration-layout.d.ts +135 -0
  31. package/dist/iteration-layout.js +169 -0
  32. package/dist/iteration-owner.d.ts +45 -0
  33. package/dist/iteration-owner.js +223 -0
  34. package/dist/iteration-worker.d.ts +1 -0
  35. package/dist/iteration-worker.js +216 -0
  36. package/dist/iteration.d.ts +329 -0
  37. package/dist/iteration.js +1210 -0
  38. package/dist/lease.d.ts +45 -0
  39. package/dist/lease.js +274 -0
  40. package/dist/managed.d.ts +78 -0
  41. package/dist/managed.js +448 -27
  42. package/dist/memo.d.ts +99 -0
  43. package/dist/memo.js +250 -0
  44. package/dist/node.d.ts +61 -5
  45. package/dist/node.js +421 -23
  46. package/dist/ownership-redirect.d.ts +30 -0
  47. package/dist/ownership-redirect.js +179 -0
  48. package/dist/ownership-slices.d.ts +44 -0
  49. package/dist/ownership-slices.js +72 -0
  50. package/dist/ownership.d.ts +161 -0
  51. package/dist/ownership.js +6568 -0
  52. package/dist/ranges.d.ts +78 -0
  53. package/dist/ranges.js +263 -0
  54. package/dist/read-scan.d.ts +18 -0
  55. package/dist/read-scan.js +94 -0
  56. package/dist/read-windows.d.ts +103 -0
  57. package/dist/read-windows.js +244 -0
  58. package/dist/router-worker.d.ts +1 -0
  59. package/dist/router-worker.js +4 -0
  60. package/dist/router.d.ts +203 -0
  61. package/dist/router.js +667 -0
  62. package/dist/runs.d.ts +44 -0
  63. package/dist/runs.js +182 -0
  64. package/dist/slices-worker.d.ts +1 -0
  65. package/dist/slices-worker.js +38 -0
  66. package/dist/slices.d.ts +126 -0
  67. package/dist/slices.js +441 -0
  68. package/dist/tasks.d.ts +14 -4
  69. package/dist/tasks.js +23 -9
  70. package/dist/transaction.d.ts +10 -0
  71. package/package.json +44 -4
@@ -0,0 +1,45 @@
1
+ /** byte size, page size, and one lowercase hex sha256 per page; the last page may be short. */
2
+ export interface LeaseManifest {
3
+ size: number;
4
+ pageSize: number;
5
+ hashes: string[];
6
+ }
7
+ /** peek found pages that are not resident. */
8
+ export declare class PageAbsent extends Error {
9
+ readonly pages: readonly number[];
10
+ constructor(pages: readonly number[]);
11
+ }
12
+ /** a page failed its manifest hash on every attempt; nothing was inserted. */
13
+ export declare class LeaseMismatch extends Error {
14
+ readonly page: number;
15
+ constructor(page: number);
16
+ }
17
+ export interface LeaseOptions {
18
+ manifest: LeaseManifest;
19
+ /** the remote read; returns fewer bytes than asked only at the end of the object. */
20
+ fetchRange(offset: number, length: number): Promise<Uint8Array>;
21
+ /** lru budget in bytes, raised to one page when smaller. default 4 MiB. */
22
+ residentBytes?: number;
23
+ /** refetches of a page whose bytes failed verification. default 1. */
24
+ retries?: number;
25
+ }
26
+ export interface LeaseStats {
27
+ hits: number;
28
+ misses: number;
29
+ fetches: number;
30
+ fetchedBytes: number;
31
+ mismatches: number;
32
+ evictions: number;
33
+ residentBytes: number;
34
+ }
35
+ /** paged read-through cache over a remote immutable object; every page is verified. */
36
+ export declare class Lease {
37
+ #private;
38
+ constructor(options: LeaseOptions);
39
+ get stats(): LeaseStats;
40
+ peek(offset: number, length: number): Uint8Array;
41
+ read(offset: number, length: number): Promise<Uint8Array>;
42
+ prefetch(offset: number, length: number): Promise<void>;
43
+ }
44
+ /** producer side: pages of bytes and their hashes; the last page may be short. */
45
+ export declare function buildManifest(bytes: Uint8Array, pageSize: number): Promise<LeaseManifest>;
package/dist/lease.js ADDED
@@ -0,0 +1,274 @@
1
+ /** peek found pages that are not resident. */
2
+ export class PageAbsent extends Error {
3
+ pages;
4
+ constructor(pages) {
5
+ super(`lease pages not resident: ${pages.join(', ')}`);
6
+ this.name = 'PageAbsent';
7
+ this.pages = pages;
8
+ }
9
+ }
10
+ /** a page failed its manifest hash on every attempt; nothing was inserted. */
11
+ export class LeaseMismatch extends Error {
12
+ page;
13
+ constructor(page) {
14
+ super(`lease page ${page} failed its hash`);
15
+ this.name = 'LeaseMismatch';
16
+ this.page = page;
17
+ }
18
+ }
19
+ /** paged read-through cache over a remote immutable object; every page is verified. */
20
+ export class Lease {
21
+ #options;
22
+ #manifest;
23
+ #budget;
24
+ #retries;
25
+ // insertion order is recency order, oldest first
26
+ #resident = new Map();
27
+ #pins = new Map();
28
+ #inflight = new Map();
29
+ #waiting = [];
30
+ // a page another read is fetching counts as a miss
31
+ #stats = {
32
+ hits: 0,
33
+ misses: 0,
34
+ fetches: 0,
35
+ fetchedBytes: 0,
36
+ mismatches: 0,
37
+ evictions: 0,
38
+ residentBytes: 0
39
+ };
40
+ constructor(options) {
41
+ const { size, pageSize, hashes } = options.manifest;
42
+ if (!Number.isSafeInteger(size) ||
43
+ size < 0 ||
44
+ !Number.isSafeInteger(pageSize) ||
45
+ pageSize < 1 ||
46
+ hashes.length !== Math.ceil(size / pageSize)) {
47
+ throw new RangeError('lease manifest hashes do not cover its size');
48
+ }
49
+ this.#options = options;
50
+ this.#manifest = options.manifest;
51
+ this.#budget = Math.max(options.residentBytes ?? 4 * 2 ** 20, pageSize);
52
+ this.#retries = options.retries ?? 1;
53
+ }
54
+ get stats() {
55
+ return { ...this.#stats };
56
+ }
57
+ peek(offset, length) {
58
+ this.#check(offset, length);
59
+ if (length === 0)
60
+ return new Uint8Array(0);
61
+ const first = this.#pageOf(offset);
62
+ const last = this.#pageOf(offset + length - 1);
63
+ const pages = [];
64
+ const absent = [];
65
+ for (let page = first; page <= last; page++) {
66
+ const bytes = this.#resident.get(page);
67
+ if (bytes) {
68
+ this.#hit(page, bytes);
69
+ pages.push(bytes);
70
+ }
71
+ else {
72
+ this.#stats.misses++;
73
+ absent.push(page);
74
+ }
75
+ }
76
+ if (absent.length)
77
+ throw new PageAbsent(absent);
78
+ return this.#assemble(offset, length, first, pages);
79
+ }
80
+ async read(offset, length) {
81
+ this.#check(offset, length);
82
+ if (length === 0)
83
+ return new Uint8Array(0);
84
+ const first = this.#pageOf(offset);
85
+ const pages = await this.#gather(first, this.#pageOf(offset + length - 1));
86
+ return this.#assemble(offset, length, first, pages);
87
+ }
88
+ async prefetch(offset, length) {
89
+ this.#check(offset, length);
90
+ if (length === 0)
91
+ return;
92
+ await this.#gather(this.#pageOf(offset), this.#pageOf(offset + length - 1));
93
+ }
94
+ // pins the range once the pages it newly pins fit beside the pinned ones, or it runs alone
95
+ async #turn(first, last) {
96
+ for (;;) {
97
+ let pinned = 0;
98
+ for (const page of this.#pins.keys())
99
+ pinned += this.#pageLength(page);
100
+ let need = 0;
101
+ for (let page = first; page <= last; page++)
102
+ if (!this.#pins.has(page))
103
+ need += this.#pageLength(page);
104
+ if (pinned === 0 || pinned + need <= this.#budget) {
105
+ this.#pin(first, last, 1);
106
+ return;
107
+ }
108
+ await new Promise((resolve) => this.#waiting.push(resolve));
109
+ }
110
+ }
111
+ // a pinned page is never evicted while a read still needs it
112
+ async #gather(first, last) {
113
+ await this.#turn(first, last);
114
+ try {
115
+ const parts = await Promise.all(this.#claim(first, last));
116
+ // digests finish in any order; recency follows page order so the tail of a range stays
117
+ for (let page = first; page <= last; page++) {
118
+ const bytes = this.#resident.get(page);
119
+ if (!bytes)
120
+ continue;
121
+ this.#resident.delete(page);
122
+ this.#resident.set(page, bytes);
123
+ }
124
+ return parts;
125
+ }
126
+ finally {
127
+ this.#pin(first, last, -1);
128
+ this.#evict();
129
+ for (const wake of this.#waiting.splice(0))
130
+ wake();
131
+ }
132
+ }
133
+ // starts one fetch per run of adjacent missing pages and returns a promise per page
134
+ #claim(first, last) {
135
+ const missing = [];
136
+ for (let page = first; page <= last; page++) {
137
+ const bytes = this.#resident.get(page);
138
+ if (bytes) {
139
+ this.#hit(page, bytes);
140
+ }
141
+ else {
142
+ this.#stats.misses++;
143
+ if (!this.#inflight.has(page))
144
+ missing.push(page);
145
+ }
146
+ }
147
+ for (let i = 0; i < missing.length; i++) {
148
+ let end = i;
149
+ while (end + 1 < missing.length && missing[end + 1] === missing[end] + 1)
150
+ end++;
151
+ this.#fetch(missing[i], missing[end]);
152
+ i = end;
153
+ }
154
+ const parts = [];
155
+ for (let page = first; page <= last; page++) {
156
+ const bytes = this.#resident.get(page);
157
+ parts.push(bytes ? Promise.resolve(bytes) : this.#inflight.get(page));
158
+ }
159
+ return parts;
160
+ }
161
+ #fetch(first, last) {
162
+ const { pageSize, size } = this.#manifest;
163
+ const offset = first * pageSize;
164
+ const source = this.#transfer(offset, Math.min((last + 1) * pageSize, size) - offset);
165
+ for (let page = first; page <= last; page++) {
166
+ this.#inflight.set(page, this.#admit(page, first, source));
167
+ }
168
+ }
169
+ async #transfer(offset, length) {
170
+ this.#stats.fetches++;
171
+ const bytes = await this.#options.fetchRange(offset, length);
172
+ this.#stats.fetchedBytes += bytes.length;
173
+ return bytes;
174
+ }
175
+ // source holds bytes from page first onward; a retry replaces it with the one page
176
+ async #admit(page, first, source) {
177
+ const { pageSize } = this.#manifest;
178
+ const length = this.#pageLength(page);
179
+ try {
180
+ let origin = first * pageSize;
181
+ let bytes = await source;
182
+ for (let attempt = 0;; attempt++) {
183
+ const start = page * pageSize - origin;
184
+ const copy = bytes.slice(start, start + length);
185
+ if (copy.length === length &&
186
+ (await digest(copy)) === this.#manifest.hashes[page]) {
187
+ this.#insert(page, copy);
188
+ return copy;
189
+ }
190
+ this.#stats.mismatches++;
191
+ if (attempt >= this.#retries)
192
+ throw new LeaseMismatch(page);
193
+ origin = page * pageSize;
194
+ bytes = await this.#transfer(origin, length);
195
+ }
196
+ }
197
+ finally {
198
+ this.#inflight.delete(page);
199
+ }
200
+ }
201
+ #insert(page, bytes) {
202
+ this.#resident.set(page, bytes);
203
+ this.#stats.residentBytes += bytes.length;
204
+ this.#evict();
205
+ }
206
+ #hit(page, bytes) {
207
+ this.#stats.hits++;
208
+ this.#resident.delete(page);
209
+ this.#resident.set(page, bytes);
210
+ }
211
+ #evict() {
212
+ for (const [page, bytes] of this.#resident) {
213
+ if (this.#stats.residentBytes <= this.#budget)
214
+ return;
215
+ if (this.#pins.has(page))
216
+ continue;
217
+ this.#resident.delete(page);
218
+ this.#stats.residentBytes -= bytes.length;
219
+ this.#stats.evictions++;
220
+ }
221
+ }
222
+ #pin(first, last, delta) {
223
+ for (let page = first; page <= last; page++) {
224
+ const count = (this.#pins.get(page) ?? 0) + delta;
225
+ if (count)
226
+ this.#pins.set(page, count);
227
+ else
228
+ this.#pins.delete(page);
229
+ }
230
+ }
231
+ #assemble(offset, length, first, pages) {
232
+ const { pageSize } = this.#manifest;
233
+ const out = new Uint8Array(length);
234
+ pages.forEach((bytes, i) => {
235
+ const start = (first + i) * pageSize;
236
+ const from = Math.max(offset - start, 0);
237
+ const to = Math.min(offset + length - start, bytes.length);
238
+ out.set(bytes.subarray(from, to), start + from - offset);
239
+ });
240
+ return out;
241
+ }
242
+ #pageOf(byte) {
243
+ return Math.floor(byte / this.#manifest.pageSize);
244
+ }
245
+ #pageLength(page) {
246
+ const { pageSize, size } = this.#manifest;
247
+ return Math.min(pageSize, size - page * pageSize);
248
+ }
249
+ #check(offset, length) {
250
+ const { size } = this.#manifest;
251
+ if (!Number.isSafeInteger(offset) ||
252
+ !Number.isSafeInteger(length) ||
253
+ offset < 0 ||
254
+ length < 0 ||
255
+ offset + length > size) {
256
+ throw new RangeError(`lease range ${offset}+${length} is outside 0..${size}`);
257
+ }
258
+ }
259
+ }
260
+ /** producer side: pages of bytes and their hashes; the last page may be short. */
261
+ export async function buildManifest(bytes, pageSize) {
262
+ if (!Number.isSafeInteger(pageSize) || pageSize < 1) {
263
+ throw new RangeError(`page size ${pageSize} is not a positive integer`);
264
+ }
265
+ const hashes = [];
266
+ for (let at = 0; at < bytes.length; at += pageSize) {
267
+ hashes.push(await digest(bytes.subarray(at, at + pageSize)));
268
+ }
269
+ return { size: bytes.length, pageSize, hashes };
270
+ }
271
+ async function digest(bytes) {
272
+ const hash = new Uint8Array(await crypto.subtle.digest('SHA-256', bytes));
273
+ return Array.from(hash, (byte) => byte.toString(16).padStart(2, '0')).join('');
274
+ }
package/dist/managed.d.ts CHANGED
@@ -9,6 +9,8 @@ export interface ManagedArtifact {
9
9
  coordinator?: WebAssembly.Module;
10
10
  /** The tracked region module each lane runs. */
11
11
  transactional?: WebAssembly.Module;
12
+ /** The same regions with inline tracking, from `inlineLane` at build time; used by a lane with `inline` set. */
13
+ inline?: WebAssembly.Module;
12
14
  /** The control plan from `prepare`, minus the module bytes carried above. */
13
15
  control?: Omit<ControlPlan, 'coordinator' | 'workerOriginal' | 'transactional'>;
14
16
  /** Imported memory limits of the source module, from `memoryImports`. */
@@ -23,6 +25,8 @@ export interface ManagedLaneTransport {
23
25
  }
24
26
  /** An `ExecutionReport` for the managed backend, which always copies snapshots. */
25
27
  export interface ManagedReport extends Omit<ExecutionReport, 'memoryMode'> {
28
+ /** Logical snapshot bytes represented across lanes; encoded traffic is in transportBytes. */
29
+ snapshotBytes?: number;
26
30
  /** Always `managed-coherent`. */
27
31
  backend: 'managed-coherent';
28
32
  /** Lanes read copied snapshots, never shared memory. */
@@ -34,6 +38,36 @@ export interface ManagedReport extends Omit<ExecutionReport, 'memoryMode'> {
34
38
  };
35
39
  /** Batches that ran natively at the owner after a transport failure or malformed reply. */
36
40
  transportFallbacks: number;
41
+ /** Ranges mode only: payload bytes sent in ranges frames. */
42
+ rangeBytes?: number;
43
+ /** Ranges mode only: ranges frames sent, summed over `batches` lane dispatches; `max` is the most one dispatch needed. */
44
+ rounds?: {
45
+ batches: number;
46
+ total: number;
47
+ max: number;
48
+ };
49
+ /** Ranges mode only: ranges frames whose results had escapes and were used only to learn. */
50
+ escapeRounds?: number;
51
+ /** Ranges mode only: dispatches that ended in a full snapshot or in native execution at the owner. */
52
+ rangeFallbacks?: {
53
+ snapshot: number;
54
+ serial: number;
55
+ };
56
+ /** Ranges mode only: calls whose footprint was already cached when their dispatch began. */
57
+ footprintHits?: number;
58
+ /** Run encoding only: response frames that came back in runs, and in addresses from a lane that predates runs. */
59
+ runReplies?: {
60
+ runs: number;
61
+ addresses: number;
62
+ };
63
+ /** Memo mode only: calls answered from a recorded result instead of a lane. */
64
+ memoHits?: number;
65
+ /** Memo mode only: lane time the hits avoided, estimated from the recorded execution time. */
66
+ memoSavedMs?: number;
67
+ /** Memo mode only: bytes hashed by lookups during the run. */
68
+ memoHashedBytes?: number;
69
+ /** Memo mode only: results recorded during the run. */
70
+ memoRecords?: number;
37
71
  }
38
72
  /**
39
73
  * The lane runtime: runs tracked regions from a binary frame against a private copy of the
@@ -46,6 +80,19 @@ export declare class FolioLane {
46
80
  maximum: number;
47
81
  maximumMemoryBytes?: number;
48
82
  maximumFrameBytes?: number;
83
+ /** Start and length of each active data segment of `module`; omitted means unknown. */
84
+ dataSegments?: readonly (readonly [start: number, length: number])[];
85
+ /**
86
+ * Runs the inline-tracked module instead of `module`: the marks and the undo log live in the
87
+ * lane's own memory above the program memory, a call's writes are read back from the log
88
+ * and rolled back, and its reads are reported at 64-byte granules. `logEntries` bounds the
89
+ * 32-byte write granules one call may log (default 8192, which is 262,144 bytes); a call
90
+ * that passes it is exhausted. Off unless set.
91
+ */
92
+ inline?: {
93
+ module: WebAssembly.Module;
94
+ logEntries?: number;
95
+ };
49
96
  });
50
97
  /** Validates the frame, runs its calls in order and returns the encoded results. */
51
98
  execute(frame: Uint8Array): Uint8Array;
@@ -68,9 +115,40 @@ export declare class ManagedFolio {
68
115
  memory?: WebAssembly.Memory;
69
116
  fuel?: number;
70
117
  maxTransactionBytes?: number;
118
+ /** Largest arena a run may snapshot; the default is 31 MiB. */
71
119
  maximumSnapshotBytes?: number;
120
+ /** Largest request frame a lane transport accepts. A larger frame is not sent and the call falls back to the owner. The default is the snapshot limit plus 1 MiB, capped below the 32 MiB Workers RPC argument limit. */
72
121
  maximumFrameBytes?: number;
73
122
  promising?: readonly string[];
123
+ /** `dense` (default) sends the whole snapshot; `auto` elides zero pages, which lanes from 1.0.0 reject. */
124
+ snapshotEncoding?: 'auto' | 'dense';
125
+ /**
126
+ * Ships learned byte ranges instead of a snapshot. Off unless set, and the lanes must come
127
+ * from a release that reads ranges frames. A batch whose lane reads unshipped bytes is
128
+ * retried with a wider footprint, at most `maxRounds` frames (default 3), then falls back
129
+ * to a full snapshot, or to native execution at the owner when none fits the limits.
130
+ */
131
+ ranges?: {
132
+ maxRounds?: number;
133
+ cacheEntries?: number;
134
+ };
135
+ /**
136
+ * Asks lanes to answer in runs: sorted `[start, length]` read and write runs with the
137
+ * written bytes once, which the owner validates and commits as intervals instead of one
138
+ * address at a time. Off unless set. A lane that predates runs ignores the request and
139
+ * answers in addresses, which the owner still decodes and validates exactly; the report's
140
+ * `runReplies` counts both. A lane that answers in a version the owner does not know is
141
+ * refused.
142
+ */
143
+ runs?: boolean;
144
+ /**
145
+ * Reuses recorded results of regions whose read bytes are unchanged; see `RegionMemo`.
146
+ * Off unless set. Hits go through the same ordered validation as lane results.
147
+ */
148
+ memo?: {
149
+ bytes?: number;
150
+ entries?: number;
151
+ };
74
152
  });
75
153
  /** The canonical memory, which must fit the owner runtime. Throws when there is no unique one. */
76
154
  get memory(): WebAssembly.Memory;