@yielded/agent-platform-cloudflare 0.1.0-beta.175 → 0.1.0-beta.176

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.
@@ -410,49 +410,48 @@ const makeAlarmOperation = Effect.gen(function* () {
410
410
  const queue = make(ctx.storage);
411
411
  const transaction = (body) => alarms.transaction((mutations) => Effect.withFiber((owner) => {
412
412
  let active = true;
413
- const requireActive = Effect.withFiber((fiber) => active && fiber === owner ? Effect.void : Effect.fail(DurableAlarmError.make({
414
- operation: "enroll maintenance source",
415
- message: "Source intent requires its active transaction callback's fiber"
416
- })));
417
- return body.pipe(Effect.provideService(CurrentCheckpoint, {
413
+ return Effect.onExitPrimitive(body(owner).pipe(Effect.provideService(CurrentCheckpoint, {
418
414
  mutations,
419
- requireActive
420
- }), Effect.ensuring(Effect.sync(() => {
415
+ isActive: (fiber) => active && fiber === owner
416
+ })), () => {
421
417
  active = false;
422
- })));
418
+ });
423
419
  }), { sqlClient: sql }).pipe(Effect.mapError((cause) => cause instanceof DurableObjectStorage.StorageOperationError ? SqlError.make({ reason: UnknownError.make({
424
420
  operation: "commit maintenance checkpoint",
425
421
  message: "Source facts and maintenance checkpoint could not commit",
426
422
  cause
427
423
  }) }) : cause));
428
- const withTransaction = (body) => Effect.flatMap(Effect.serviceOption(sql.transactionService), (ambient) => {
424
+ const withTransaction = (body) => Effect.withFiber((fiber) => {
425
+ const ambient = Context.getOption(fiber.context, sql.transactionService);
429
426
  if (ambient._tag === "Some" && !ownsTransaction(ctx.storage, ambient.value[1])) return Effect.fail(SqlError.make({ reason: UnknownError.make({
430
427
  operation: "begin maintenance source transaction",
431
428
  message: "Use ThreadMutationGate.withTransaction at the outermost source transaction",
432
429
  cause: "Unwrapped SQL transaction cannot own buffered maintenance intent"
433
430
  }) }));
434
- return Effect.uninterruptibleMask((restore) => Effect.suspend(() => {
435
- let frame;
436
- return transaction(Effect.flatMap(Effect.serviceOption(sql.transactionService), (transaction) => Effect.gen(function* () {
437
- if (transaction._tag === "None") return yield* SqlError.make({ reason: UnknownError.make({
438
- operation: "begin maintenance source transaction",
439
- message: "SQL transaction unavailable",
440
- cause: "SQL transaction unavailable"
441
- }) });
442
- frame = queue.begin(transaction.value[1]);
443
- })).pipe(Effect.andThen(restore(body)), Effect.tap(() => Effect.try({
431
+ let frame;
432
+ return Effect.uninterruptibleMask((restore) => Effect.onExitPrimitive(transaction((owner) => {
433
+ const current = Context.getOption(owner.context, sql.transactionService);
434
+ if (current._tag === "None") return SqlError.make({ reason: UnknownError.make({
435
+ operation: "begin maintenance source transaction",
436
+ message: "SQL transaction unavailable",
437
+ cause: "SQL transaction unavailable"
438
+ }) });
439
+ const started = queue.begin(current.value[1]);
440
+ frame = started;
441
+ return Effect.flatMap(restore(body), (value) => Effect.try({
444
442
  try: () => {
445
- if (frame !== void 0) queue.flush(frame);
443
+ queue.flush(started);
444
+ return value;
446
445
  },
447
446
  catch: (cause) => SqlError.make({ reason: UnknownError.make({
448
447
  cause,
449
448
  operation: "flush maintenance due queue",
450
449
  message: "Maintenance intent could not commit"
451
450
  }) })
452
- })))).pipe(Effect.onExit((exit) => Effect.sync(() => {
453
- if (frame !== void 0) queue.finish(frame, Exit.isSuccess(exit));
454
- })), Effect.provideService(Scheduler.PreventSchedulerYield, true));
455
- }));
451
+ }));
452
+ }), (exit) => {
453
+ if (frame !== void 0) queue.finish(frame, Exit.isSuccess(exit));
454
+ }).pipe(Effect.provideService(Scheduler.PreventSchedulerYield, true)));
456
455
  }).pipe(Effect.provideService(SqlClient, sql));
457
456
  const run = (operation, execute) => {
458
457
  const commit = Effect.withFiber((fiber) => {
@@ -461,28 +460,30 @@ const makeAlarmOperation = Effect.gen(function* () {
461
460
  operation: "checkpoint",
462
461
  message: "Maintenance checkpoint transaction unavailable"
463
462
  }));
464
- return tx.requireActive.pipe(Effect.andThen(Effect.suspend(() => {
465
- let checkpoint = Effect.void;
466
- const intent = {
467
- at: (at) => {
468
- checkpoint = tx.mutations.scheduleAlarm(maintenanceAlarmInput(at));
469
- },
470
- earlier: (at) => {
471
- checkpoint = tx.mutations.scheduleAlarmEarlier(maintenanceAlarmInput(at));
472
- },
473
- cancel: () => {
474
- checkpoint = tx.mutations.cancelAlarm(maintenanceAlarmRef);
475
- }
476
- };
477
- return Effect.tryPromise({
478
- try: () => execute(ctx.storage, intent),
479
- catch: alarmFailure(operation)
480
- }).pipe(Effect.tap(() => checkpoint));
481
- })));
463
+ if (!tx.isActive(fiber)) return Effect.fail(DurableAlarmError.make({
464
+ operation: "enroll maintenance source",
465
+ message: "Source intent requires its active transaction callback's fiber"
466
+ }));
467
+ let checkpoint = Effect.void;
468
+ const intent = {
469
+ at: (at) => {
470
+ checkpoint = tx.mutations.scheduleAlarm(maintenanceAlarmInput(at));
471
+ },
472
+ earlier: (at) => {
473
+ checkpoint = tx.mutations.scheduleAlarmEarlier(maintenanceAlarmInput(at));
474
+ },
475
+ cancel: () => {
476
+ checkpoint = tx.mutations.cancelAlarm(maintenanceAlarmRef);
477
+ }
478
+ };
479
+ return Effect.tryPromise({
480
+ try: () => execute(ctx.storage, intent),
481
+ catch: alarmFailure(operation)
482
+ }).pipe(Effect.tap(() => checkpoint));
482
483
  });
483
484
  return Effect.withFiber((fiber) => {
484
485
  if (fiber.getRef(CurrentCheckpoint) !== void 0) return commit;
485
- return Context.getOption(fiber.context, sql.transactionService)._tag === "Some" && !buffering(ctx.storage) ? transaction(commit).pipe(Effect.ensuring(Effect.sync(() => invalidate(ctx.storage)))) : withTransaction(commit);
486
+ return Context.getOption(fiber.context, sql.transactionService)._tag === "Some" && !buffering(ctx.storage) ? transaction(() => commit).pipe(Effect.ensuring(Effect.sync(() => invalidate(ctx.storage)))) : withTransaction(commit);
486
487
  }).pipe(Effect.uninterruptible, Effect.mapError(alarmFailure(operation)));
487
488
  };
488
489
  return {
@@ -1896,4 +1897,4 @@ var ThreadMaintenance = class ThreadMaintenance extends Context.Service()("@effe
1896
1897
  //#endregion
1897
1898
  export { Lifecycle as _, ThreadHostMaintenance as a, Projection as b, ThreadMaintenanceFailpoint as c, ThreadNativeMaintenance as d, ThreadPublication as f, HostLaneId as g, publishCommitted as h, MaintenancePassReport as i, ThreadMessageDelivery as l, ThreadRecoveryFaultEvent as m, DurableAlarmError as n, ThreadMaintenance as o, ThreadRecoveryEvents as p, DurableAlarmService as r, ThreadMaintenanceAlarm as s, Alarm_exports as t, ThreadMutationGate as u, Messages as v, Publication as x, Native as y };
1898
1899
 
1899
- //# sourceMappingURL=Alarm-B44efvB1.mjs.map
1900
+ //# sourceMappingURL=Alarm-7uipp6LW.mjs.map