@waffle-os/runtime 0.0.149 → 0.0.150

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.
@@ -1,7 +1,12 @@
1
1
  import { atomicsWaitAsync } from "./intrinsics.js";
2
2
  import type { DescriptorTable, Resource } from "../core/descriptors.js";
3
- import { claimFileOffset, withFileOffset, shareFileResource } from "../core/open-file.js";
4
- import { FilesystemClient } from "../fs/client.js";
3
+ import {
4
+ claimFileOffset,
5
+ withFileOffset,
6
+ shareFileResource,
7
+ type AsyncFileOffsetLease,
8
+ } from "../core/open-file.js";
9
+ import { FilesystemClient, type FilesystemCompletion } from "../fs/client.js";
5
10
  import { errno, type FilesystemConnection } from "../fs/protocol.js";
6
11
  import { CommandStdioError, type CommandInput, type CommandOutput } from "./command.js";
7
12
 
@@ -116,42 +121,101 @@ export class CommandStdio {
116
121
  }
117
122
  }
118
123
 
119
- private async runAsyncFileLease(
124
+ private commitAsyncWrite(
125
+ lease: AsyncFileOffsetLease,
126
+ result: { written: number; offset: number } | number,
127
+ ): number {
128
+ if (lease.append) {
129
+ const appended = result as { written: number; offset: number };
130
+ lease.commit(appended.offset);
131
+ return appended.written;
132
+ }
133
+ const written = result as number;
134
+ if (written > 0) lease.commit(lease.position + written);
135
+ return written;
136
+ }
137
+
138
+ private deferAsyncWrite(
139
+ filesystem: FilesystemClient,
140
+ completion: FilesystemCompletion,
141
+ lease: AsyncFileOffsetLease,
142
+ ): void {
143
+ filesystem.trackDeferredWrite(completion, () => {
144
+ try {
145
+ const settled = completion.tryConsumeCommitted();
146
+ if (settled !== undefined) this.commitAsyncWrite(lease, settled);
147
+ } catch {
148
+ // Aborted or failed write: leave offset unchanged before releasing the lease.
149
+ } finally {
150
+ completion.release();
151
+ lease.release();
152
+ }
153
+ });
154
+ }
155
+
156
+ private async writeAsyncFileLease(
120
157
  filesystem: FilesystemClient,
121
158
  resource: Extract<Resource, { kind: "file" }>,
122
- lease: { readonly position: number; readonly append: boolean; commit(next: number): void },
159
+ lease: AsyncFileOffsetLease,
123
160
  bytes: Uint8Array,
124
- writing: boolean,
161
+ onDefer: () => void,
125
162
  ): Promise<number> {
126
- if (writing && lease.append) {
127
- const result: { written: number; offset: number } = await filesystem.asyncCall(
128
- "append",
129
- [resource.handle],
130
- bytes,
131
- );
132
- lease.commit(result.offset);
133
- this.check();
134
- return result.written;
163
+ const completion = lease.append
164
+ ? filesystem.submit("append", [resource.handle], bytes)
165
+ : filesystem.submit("write", [resource.handle, lease.position], bytes);
166
+ let handedOff = false;
167
+ try {
168
+ const result = await completion.value();
169
+ return this.commitAsyncWrite(lease, result);
170
+ } catch (error) {
171
+ const committed = completion.tryConsumeCommitted();
172
+ if (committed !== undefined) {
173
+ this.commitAsyncWrite(lease, committed);
174
+ } else {
175
+ handedOff = true;
176
+ onDefer();
177
+ this.deferAsyncWrite(filesystem, completion, lease);
178
+ }
179
+ throw error;
180
+ } finally {
181
+ if (!handedOff) completion.release();
135
182
  }
136
- if (writing) {
137
- const written: number = await filesystem.asyncCall(
138
- "write",
139
- [resource.handle, lease.position],
140
- bytes,
141
- );
142
- if (written > 0) lease.commit(lease.position + written);
183
+ }
184
+
185
+ private async runLeasedTransfer(
186
+ filesystem: FilesystemClient,
187
+ resource: Extract<Resource, { kind: "file" }>,
188
+ lease: AsyncFileOffsetLease,
189
+ bytes: Uint8Array,
190
+ writing: boolean,
191
+ ): Promise<number> {
192
+ let releaseLease = true;
193
+ const defer = () => {
194
+ releaseLease = false;
195
+ };
196
+ try {
197
+ if (writing) {
198
+ const written = await this.writeAsyncFileLease(filesystem, resource, lease, bytes, defer);
199
+ lease.release();
200
+ releaseLease = false;
201
+ this.check();
202
+ return written;
203
+ }
204
+ const read: Uint8Array = await filesystem.asyncCall("read", [
205
+ resource.handle,
206
+ bytes.length,
207
+ lease.position,
208
+ ]);
209
+ bytes.set(read, 0);
210
+ if (read.length > 0) lease.commit(lease.position + read.length);
143
211
  this.check();
144
- return written;
212
+ return read.length;
213
+ } catch (error) {
214
+ if (error instanceof CommandStdioError) throw error;
215
+ throw stdioError(-errno(error));
216
+ } finally {
217
+ if (releaseLease) lease.release();
145
218
  }
146
- const read: Uint8Array = await filesystem.asyncCall("read", [
147
- resource.handle,
148
- bytes.length,
149
- lease.position,
150
- ]);
151
- bytes.set(read, 0);
152
- if (read.length > 0) lease.commit(lease.position + read.length);
153
- this.check();
154
- return read.length;
155
219
  }
156
220
 
157
221
  private async transferAsyncFile(
@@ -165,21 +229,13 @@ export class CommandStdio {
165
229
  shareFileResource(resource, filesystem.connection);
166
230
  for (;;) {
167
231
  this.check();
168
- const claim = claimFileOffset(resource);
232
+ const claim = claimFileOffset(resource, writing);
169
233
  if (claim.kind === "failed") throw stdioError(-claim.errno);
170
234
  if (claim.kind === "busy") {
171
235
  await this.wait([claim.word], [claim.observed]);
172
236
  continue;
173
237
  }
174
- const { lease } = claim;
175
- try {
176
- return await this.runAsyncFileLease(filesystem, resource, lease, bytes, writing);
177
- } catch (error) {
178
- if (error instanceof CommandStdioError) throw error;
179
- throw stdioError(-errno(error));
180
- } finally {
181
- lease.release();
182
- }
238
+ return await this.runLeasedTransfer(filesystem, resource, claim.lease, bytes, writing);
183
239
  }
184
240
  }
185
241
 
@@ -250,6 +306,7 @@ export class CommandStdio {
250
306
 
251
307
  async settle(): Promise<void> {
252
308
  while (this.pending.size) await Promise.allSettled(Array.from(this.pending));
309
+ if (this.filesystem?.hasDeferredWrites()) await this.filesystem.settleDeferredWrites();
253
310
  await this.filesystem?.stopResidentPublication();
254
311
  this.filesystem?.dispose();
255
312
  }
package/thread/service.ts CHANGED
@@ -272,12 +272,28 @@ export class ThreadService {
272
272
  await Promise.race([previous?.catch(() => {}), this.cancelled.promise]);
273
273
  this.live();
274
274
  const lease = await this.offset(resource);
275
+ const leaseState: { current: "held" | "settled" | "deferred" } = { current: "held" };
275
276
  try {
276
- return await this.file(operation, resource, lease.position, lease);
277
+ return await this.file(operation, resource, lease.position, lease, (next) => {
278
+ leaseState.current = next;
279
+ });
277
280
  } finally {
278
281
  // Revocation can reject value() while remote preparation still owns this operation.
279
- if (this.phase !== "open") await this.retire(operation);
280
- lease.release();
282
+ if (this.phase !== "open") await this.retireSequential(operation, lease, leaseState.current);
283
+ if (leaseState.current !== "deferred") lease.release();
284
+ }
285
+ }
286
+
287
+ private async retireSequential(
288
+ operation: Operation,
289
+ lease: AsyncFileOffsetLease,
290
+ leaseState: "held" | "settled" | "deferred",
291
+ ): Promise<void> {
292
+ try {
293
+ await this.retire(operation);
294
+ } catch (error) {
295
+ if (leaseState === "settled") lease.release();
296
+ throw error;
281
297
  }
282
298
  }
283
299
 
@@ -296,6 +312,7 @@ export class ThreadService {
296
312
  resource: FileResource,
297
313
  position: number,
298
314
  lease?: AsyncFileOffsetLease,
315
+ onLeaseState?: (state: "settled" | "deferred") => void,
299
316
  ): Promise<ThreadResult> {
300
317
  this.live();
301
318
  const request = operation.request;
@@ -311,6 +328,7 @@ export class ThreadService {
311
328
  this.position(next);
312
329
  this.live();
313
330
  lease!.commit(next);
331
+ onLeaseState?.("settled");
314
332
  return { kind: "ok", value: next };
315
333
  }
316
334
  if (request.kind !== "read" && request.kind !== "write") throw new FilesystemError(58);
@@ -325,6 +343,7 @@ export class ThreadService {
325
343
  position,
326
344
  ]);
327
345
  lease?.commit(position + bytes.length);
346
+ if (lease) onLeaseState?.("settled");
328
347
  this.live();
329
348
  this.scatter(request.spans, bytes);
330
349
  return { kind: "ok", value: bytes.length };
@@ -336,19 +355,45 @@ export class ThreadService {
336
355
  append ? "append" : "write",
337
356
  [resource.handle, position],
338
357
  data,
358
+ lease
359
+ ? {
360
+ lease,
361
+ commit: (committed) => {
362
+ const committedWritten: number = append ? committed.written : committed;
363
+ lease.commit(append ? committed.offset : position + committedWritten);
364
+ },
365
+ onState: onLeaseState,
366
+ }
367
+ : undefined,
339
368
  );
340
369
  const written: number = append ? result.written : result;
341
- lease?.commit(append ? result.offset : position + written);
342
370
  this.live();
343
371
  return { kind: "ok", value: written };
344
372
  }
345
373
 
346
- private async request(operation: Operation, name: string, args: unknown[], data?: Uint8Array) {
374
+ private async request(
375
+ operation: Operation,
376
+ name: string,
377
+ args: unknown[],
378
+ data?: Uint8Array,
379
+ writeLease?: {
380
+ lease: AsyncFileOffsetLease;
381
+ commit: (value: any) => void;
382
+ onState?: (state: "settled" | "deferred") => void;
383
+ },
384
+ ) {
347
385
  this.live();
348
386
  const completion = this.options.filesystem.submit(name, args, data);
349
387
  operation.completions.add(completion);
388
+ let committedApplied = false;
350
389
  try {
351
- return await completion.value();
390
+ const value = await completion.value();
391
+ if (writeLease) {
392
+ committedApplied = true;
393
+ writeLease.commit(value);
394
+ writeLease.onState?.("settled");
395
+ }
396
+ return value;
352
397
  } catch (error) {
353
398
  if (
354
399
  error instanceof FilesystemTransportError ||
@@ -357,10 +402,83 @@ export class ThreadService {
357
402
  this.cancelAll();
358
403
  throw error;
359
404
  } finally {
360
- if (this.phase !== "open") await this.retire(operation);
405
+ await this.finishRequest(operation, completion, committedApplied, writeLease);
406
+ }
407
+ }
408
+
409
+ private async finishRequest(
410
+ operation: Operation,
411
+ completion: FilesystemCompletion,
412
+ committedApplied: boolean,
413
+ writeLease?: {
414
+ lease: AsyncFileOffsetLease;
415
+ commit: (value: any) => void;
416
+ onState?: (state: "settled" | "deferred") => void;
417
+ },
418
+ ): Promise<void> {
419
+ const retiredError = this.phase !== "open" ? await this.tryRetire(operation) : undefined;
420
+ if (writeLease && !committedApplied) {
421
+ this.settleWriteLease(operation, completion, writeLease, retiredError === undefined);
422
+ } else if (retiredError === undefined || committedApplied || completion.backendSettled) {
361
423
  completion.release();
362
424
  operation.completions.delete(completion);
363
425
  }
426
+ if (retiredError !== undefined) throw retiredError;
427
+ }
428
+
429
+ private settleWriteLease(
430
+ operation: Operation,
431
+ completion: FilesystemCompletion,
432
+ writeLease: {
433
+ lease: AsyncFileOffsetLease;
434
+ commit: (value: any) => void;
435
+ onState?: (state: "settled" | "deferred") => void;
436
+ },
437
+ retiredCleanly: boolean,
438
+ ): void {
439
+ // Check monotonic backendSettled BEFORE tryConsumeCommitted so a Pending->Complete
440
+ // transition can never release the completion without applying the committed offset.
441
+ if (retiredCleanly || completion.backendSettled) {
442
+ this.consumeCommitted(completion, writeLease.commit);
443
+ writeLease.onState?.("settled");
444
+ completion.release();
445
+ operation.completions.delete(completion);
446
+ return;
447
+ }
448
+ writeLease.onState?.("deferred");
449
+ this.options.filesystem.trackDeferredWrite(completion, () => {
450
+ try {
451
+ this.consumeCommitted(completion, writeLease.commit);
452
+ } finally {
453
+ completion.release();
454
+ operation.completions.delete(completion);
455
+ writeLease.lease.release();
456
+ }
457
+ });
458
+ }
459
+
460
+ private async tryRetire(operation: Operation): Promise<unknown> {
461
+ try {
462
+ await this.retire(operation);
463
+ return undefined;
464
+ } catch (error) {
465
+ return error;
466
+ }
467
+ }
468
+
469
+ private consumeCommitted(
470
+ completion: FilesystemCompletion,
471
+ onCommitted: (value: any) => void,
472
+ ): boolean {
473
+ try {
474
+ const committed = completion.tryConsumeCommitted();
475
+ if (committed === undefined) return false;
476
+ onCommitted(committed);
477
+ return true;
478
+ } catch {
479
+ // Aborted or failed write: leave offset unchanged before releasing the completion.
480
+ return false;
481
+ }
364
482
  }
365
483
 
366
484
  private async stream(operation: Operation): Promise<ThreadResult> {
@@ -1 +1 @@
1
- {"commit":"7b0a762d210309fb18e4772f1a1cb50d96602dc1","buildToolsSha256":"b617652c1601f8722bb85c3a688a1fca4985b53f2c2066c10c76c2e693081137","optimizerSourceSha256":"34c4a040f3cf639f039823880822c6541c384c9b4eaa116d27dda8d9cb5c9f9e"}
1
+ {"commit":"f2b1143915c41d86a9ef930b7f9d50e5228b1a05","buildToolsSha256":"b617652c1601f8722bb85c3a688a1fca4985b53f2c2066c10c76c2e693081137","optimizerSourceSha256":"34c4a040f3cf639f039823880822c6541c384c9b4eaa116d27dda8d9cb5c9f9e"}