@yielded/agent-platform-cloudflare 0.1.0-beta.185 → 0.1.0-beta.186
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.
- package/dist/{Alarm-C257DCAJ.mjs → Alarm-BN-ADYcu.mjs} +87 -47
- package/dist/Alarm-BN-ADYcu.mjs.map +1 -0
- package/dist/Alarm.d.mts +10 -3
- package/dist/Alarm.mjs +1 -1
- package/dist/BrowserRestCapture.d.mts +2 -2
- package/dist/BrowserRestCapture.mjs +2 -2
- package/dist/BrowserRestCrawl.d.mts +1 -1
- package/dist/BrowserRestCrawl.mjs +1 -1
- package/dist/{BrowserSession-CfivQ5JY.mjs → BrowserSession-CTgTFKPh.mjs} +2 -2
- package/dist/{BrowserSession-CfivQ5JY.mjs.map → BrowserSession-CTgTFKPh.mjs.map} +1 -1
- package/dist/{BrowserSession-mKnLSc3p.d.mts → BrowserSession-DJ1Ey4Zv.d.mts} +2 -2
- package/dist/BrowserSession.d.mts +2 -2
- package/dist/BrowserSession.mjs +2 -2
- package/dist/BrowserUse.d.mts +1 -1
- package/dist/CloudflareBindings.d.mts +4 -1
- package/dist/CloudflareBindings.mjs +5 -1
- package/dist/CloudflareBindings.mjs.map +1 -1
- package/dist/{CloudflareBrowser-gu7gma0Y.d.mts → CloudflareBrowser-DS6GodKz.d.mts} +2 -2
- package/dist/{CloudflareBrowser-Vt8qTdYW.mjs → CloudflareBrowser-DrZQwovV.mjs} +2 -2
- package/dist/{CloudflareBrowser-Vt8qTdYW.mjs.map → CloudflareBrowser-DrZQwovV.mjs.map} +1 -1
- package/dist/CloudflareBrowser.d.mts +1 -1
- package/dist/CloudflareBrowser.mjs +1 -1
- package/dist/CloudflareCodeMode.d.mts +1 -1
- package/dist/CloudflareCodeMode.mjs +2 -2
- package/dist/{CloudflareConfig-C9MvPbX8.d.mts → CloudflareConfig-D_-BMp4q.d.mts} +12 -6
- package/dist/CloudflareConfig.d.mts +2 -2
- package/dist/CloudflareConfig.mjs +12 -5
- package/dist/CloudflareConfig.mjs.map +1 -1
- package/dist/CloudflareMemory.d.mts +1 -1
- package/dist/CloudflareMemory.mjs +1 -1
- package/dist/CloudflareScheduling.d.mts +1 -1
- package/dist/CloudflareScheduling.mjs +3 -3
- package/dist/CloudflareSubscriptions.d.mts +1 -1
- package/dist/CloudflareSubscriptions.mjs +3 -3
- package/dist/{CloudflareThreadClient-CnvMXPmV.mjs → CloudflareThreadClient-DgV_RBtF.mjs} +11 -3
- package/dist/CloudflareThreadClient-DgV_RBtF.mjs.map +1 -0
- package/dist/{CloudflareThreadClient-CmlojpjC.d.mts → CloudflareThreadClient-_RWtjnqM.d.mts} +188 -183
- package/dist/CloudflareThreadClient.d.mts +1 -1
- package/dist/CloudflareThreadClient.mjs +1 -1
- package/dist/{InteractiveBrowser-wV2Bti5e.d.mts → InteractiveBrowser-BJX3d8cP.d.mts} +2 -2
- package/dist/{InteractiveBrowser-nukKxc87.mjs → InteractiveBrowser-M887Mt3D.mjs} +2 -2
- package/dist/{InteractiveBrowser-nukKxc87.mjs.map → InteractiveBrowser-M887Mt3D.mjs.map} +1 -1
- package/dist/InteractiveBrowser.d.mts +1 -1
- package/dist/InteractiveBrowser.mjs +1 -1
- package/dist/{ThreadObject-Cr1CFWXb.mjs → ThreadObject-AP5CFiOw.mjs} +30 -9
- package/dist/ThreadObject-AP5CFiOw.mjs.map +1 -0
- package/dist/{ThreadObject-Osn-FmcF.d.mts → ThreadObject-CXUcI8j5.d.mts} +14 -6
- package/dist/ThreadObject.d.mts +1 -1
- package/dist/ThreadObject.mjs +1 -1
- package/dist/WakeScheduler.d.mts +1 -1
- package/dist/WakeScheduler.mjs +7 -3
- package/dist/WakeScheduler.mjs.map +1 -1
- package/dist/{boundary-BguazVkh.mjs → boundary-BLbZMBl2.mjs} +2 -2
- package/dist/boundary-BLbZMBl2.mjs.map +1 -0
- package/dist/index.d.mts +5 -5
- package/dist/index.mjs +5 -5
- package/dist/{prepared-admission-w2Kq32_k.mjs → prepared-admission-DSuWZ5qK.mjs} +4 -3
- package/dist/prepared-admission-DSuWZ5qK.mjs.map +1 -0
- package/package.json +1 -1
- package/src/Alarm.ts +110 -64
- package/src/CloudflareBindings.ts +5 -1
- package/src/CloudflareConfig.ts +10 -3
- package/src/CloudflareThreadClient.ts +8 -1
- package/src/ThreadObject.ts +13 -8
- package/src/WakeScheduler.ts +5 -1
- package/src/internal/boundary.ts +1 -1
- package/src/internal/crypto.ts +4 -1
- package/src/internal/due-queue.ts +2 -1
- package/src/internal/layers.ts +29 -16
- package/src/internal/live-text-protocol.ts +1 -1
- package/src/internal/live-text.ts +4 -1
- package/src/internal/message-delivery.ts +7 -1
- package/src/internal/prepared-admission.ts +2 -1
- package/src/internal/progress-wait.ts +6 -1
- package/src/internal/transport.ts +2 -1
- package/dist/Alarm-C257DCAJ.mjs.map +0 -1
- package/dist/CloudflareThreadClient-CnvMXPmV.mjs.map +0 -1
- package/dist/ThreadObject-Cr1CFWXb.mjs.map +0 -1
- package/dist/boundary-BguazVkh.mjs.map +0 -1
- package/dist/prepared-admission-w2Kq32_k.mjs.map +0 -1
package/src/Alarm.ts
CHANGED
|
@@ -21,31 +21,29 @@ import {
|
|
|
21
21
|
type ThreadProjectionError,
|
|
22
22
|
} from "@yielded/agent/thread-projection-maintenance";
|
|
23
23
|
import { WakeScheduler } from "@yielded/agent/wake-scheduler";
|
|
24
|
-
import {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
Stream,
|
|
44
|
-
Struct,
|
|
45
|
-
} from "effect";
|
|
46
|
-
import { DurableObjectAlarm, DurableObjectStorage } from "effect-cf";
|
|
24
|
+
import { DurableObjectAlarm, DurableObjectState, DurableObjectStorage } from "effect-cf";
|
|
25
|
+
import * as Cause from "effect/Cause";
|
|
26
|
+
import * as Clock from "effect/Clock";
|
|
27
|
+
import * as Context from "effect/Context";
|
|
28
|
+
import * as DateTime from "effect/DateTime";
|
|
29
|
+
import * as Deferred from "effect/Deferred";
|
|
30
|
+
import * as Effect from "effect/Effect";
|
|
31
|
+
import * as ErrorReporter from "effect/ErrorReporter";
|
|
32
|
+
import * as Exit from "effect/Exit";
|
|
33
|
+
import * as Fiber from "effect/Fiber";
|
|
34
|
+
import * as Layer from "effect/Layer";
|
|
35
|
+
import * as Option from "effect/Option";
|
|
36
|
+
import * as Random from "effect/Random";
|
|
37
|
+
import * as Ref from "effect/Ref";
|
|
38
|
+
import * as Result from "effect/Result";
|
|
39
|
+
import * as Scheduler from "effect/Scheduler";
|
|
40
|
+
import * as Schema from "effect/Schema";
|
|
41
|
+
import * as Scope from "effect/Scope";
|
|
42
|
+
import * as Semaphore from "effect/Semaphore";
|
|
47
43
|
import { SqlClient } from "effect/sql/SqlClient";
|
|
48
44
|
import { SqlError, UnknownError } from "effect/sql/SqlError";
|
|
45
|
+
import * as Stream from "effect/Stream";
|
|
46
|
+
import * as Struct from "effect/Struct";
|
|
49
47
|
|
|
50
48
|
import {
|
|
51
49
|
DurableAlarmError,
|
|
@@ -979,6 +977,7 @@ export class ThreadMutationGate extends Context.Service<
|
|
|
979
977
|
});
|
|
980
978
|
|
|
981
979
|
const minimumAlarmDelay = Math.max(1, Math.ceil(config.alarmBackoffBase / 2));
|
|
980
|
+
const recoveryDelay = config.recoveryWindowMillis || minimumAlarmDelay;
|
|
982
981
|
|
|
983
982
|
const runTransaction = yield* makeStorageOperation;
|
|
984
983
|
const { run: runAlarmTransaction, withTransaction } = yield* makeAlarmOperation;
|
|
@@ -1010,6 +1009,7 @@ export class ThreadMutationGate extends Context.Service<
|
|
|
1010
1009
|
) {
|
|
1011
1010
|
yield* validateSourceBoundary;
|
|
1012
1011
|
const now = yield* Clock.currentTimeMillis;
|
|
1012
|
+
const scheduling = yield* DurableObjectAlarm.ScheduleConfiguration;
|
|
1013
1013
|
|
|
1014
1014
|
const enroll = async (transaction: MaintenanceStorage, checkpoint: CheckpointIntent) => {
|
|
1015
1015
|
if (progressCursor === undefined) dueQueue.dirty(id, dueAt);
|
|
@@ -1033,7 +1033,13 @@ export class ThreadMutationGate extends Context.Service<
|
|
|
1033
1033
|
}
|
|
1034
1034
|
const next = DueQueue.next(dueQueue.read());
|
|
1035
1035
|
|
|
1036
|
-
if (Number.isFinite(next))
|
|
1036
|
+
if (Number.isFinite(next))
|
|
1037
|
+
checkpoint.earlier(
|
|
1038
|
+
Math.max(
|
|
1039
|
+
next,
|
|
1040
|
+
now + (scheduling.dispatchAfterEvent === true ? 0 : minimumAlarmDelay),
|
|
1041
|
+
),
|
|
1042
|
+
);
|
|
1037
1043
|
};
|
|
1038
1044
|
|
|
1039
1045
|
yield* runAlarmTransaction("schedule maintenance lane", enroll);
|
|
@@ -1072,12 +1078,12 @@ export class ThreadMutationGate extends Context.Service<
|
|
|
1072
1078
|
}),
|
|
1073
1079
|
),
|
|
1074
1080
|
);
|
|
1075
|
-
//
|
|
1076
|
-
//
|
|
1081
|
+
// Commit recovery before the producer. Source progress separately starts work
|
|
1082
|
+
// promptly; a retained earlier wake already covers this recovery window.
|
|
1077
1083
|
const deadline = DueQueue.next(dueQueue.read());
|
|
1078
1084
|
|
|
1079
1085
|
if (Number.isFinite(deadline))
|
|
1080
|
-
checkpoint.earlier(Math.max(deadline, now +
|
|
1086
|
+
checkpoint.earlier(Math.max(deadline, now + recoveryDelay));
|
|
1081
1087
|
},
|
|
1082
1088
|
);
|
|
1083
1089
|
yield* failpoint.hit("maintenance:dirty:after");
|
|
@@ -1268,6 +1274,10 @@ export class ThreadMaintenance extends Context.Service<
|
|
|
1268
1274
|
{
|
|
1269
1275
|
/** One idempotent pass; failures propagate after durably scheduling bounded recovery. */
|
|
1270
1276
|
readonly pass: Effect.Effect<MaintenancePassReport, MaintenancePassFailure>;
|
|
1277
|
+
/** Start a committed admission under the same bounded processing and recovery guard. */
|
|
1278
|
+
readonly start: (
|
|
1279
|
+
admission: Pick<SubmissionWorkItem, "threadId" | "submissionId">,
|
|
1280
|
+
) => Effect.Effect<void>;
|
|
1271
1281
|
/**
|
|
1272
1282
|
* Constructor gate: initialize/inspect only the O(1) maintenance record and ensure a dirty
|
|
1273
1283
|
* generation has an alarm. It never scans the ledger or canonical history.
|
|
@@ -1310,6 +1320,7 @@ export class ThreadMaintenance extends Context.Service<
|
|
|
1310
1320
|
const alarms = yield* ThreadMaintenanceAlarm;
|
|
1311
1321
|
const config = yield* CloudflareDurableRuntimeConfig;
|
|
1312
1322
|
const { ctx } = yield* DurableObjectContext;
|
|
1323
|
+
const execution = DurableObjectState.fromDurableObjectState(ctx);
|
|
1313
1324
|
const dueQueue = DueQueue.make(ctx.storage);
|
|
1314
1325
|
const storage = DurableObjectStorage.fromDurableObjectStorage(ctx.storage);
|
|
1315
1326
|
const runStorage = yield* makeStorageEffect;
|
|
@@ -1982,12 +1993,13 @@ export class ThreadMaintenance extends Context.Service<
|
|
|
1982
1993
|
);
|
|
1983
1994
|
|
|
1984
1995
|
const attemptSelected = Effect.fnUntraced(function* (
|
|
1985
|
-
selected: SubmissionWorkItem,
|
|
1996
|
+
selected: Pick<SubmissionWorkItem, "threadId" | "submissionId">,
|
|
1986
1997
|
previousWaits: ReadonlyArray<BindingWait>,
|
|
1987
1998
|
yieldAfter: DateTime.Utc,
|
|
1988
1999
|
recovery: NativeRecovery,
|
|
2000
|
+
admitted = false,
|
|
1989
2001
|
): Effect.fn.Return<number, MaintenancePassFailure> {
|
|
1990
|
-
yield* recoverThread(selected.threadId, recovery);
|
|
2002
|
+
if (!admitted) yield* recoverThread(selected.threadId, recovery);
|
|
1991
2003
|
|
|
1992
2004
|
let bindingFailure: DurableBindingFailure | undefined;
|
|
1993
2005
|
|
|
@@ -1995,14 +2007,19 @@ export class ThreadMaintenance extends Context.Service<
|
|
|
1995
2007
|
// and committing its registry wait below.
|
|
1996
2008
|
const settlement = recovery.faults.has(selected.threadId)
|
|
1997
2009
|
? Option.none()
|
|
1998
|
-
: yield* runtime
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2010
|
+
: yield* runtime
|
|
2011
|
+
.processThreadHead(selected.threadId, {
|
|
2012
|
+
yieldAfter,
|
|
2013
|
+
...(admitted ? { admittedSubmissionId: selected.submissionId } : {}),
|
|
2014
|
+
})
|
|
2015
|
+
.pipe(
|
|
2016
|
+
Effect.provideService(CurrentNativeSource, true),
|
|
2017
|
+
Effect.catchTag("BindingUnavailable", (failure) => {
|
|
2018
|
+
bindingFailure = failure;
|
|
2002
2019
|
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2020
|
+
return Effect.succeed(Option.none());
|
|
2021
|
+
}),
|
|
2022
|
+
);
|
|
2006
2023
|
|
|
2007
2024
|
const previous = previousWaits.find((wait) => wait.submissionId === selected.submissionId);
|
|
2008
2025
|
|
|
@@ -2626,6 +2643,7 @@ export class ThreadMaintenance extends Context.Service<
|
|
|
2626
2643
|
yieldAfter: DateTime.Utc,
|
|
2627
2644
|
dispatchUntil: DateTime.Utc,
|
|
2628
2645
|
observed: MaintenanceObservation,
|
|
2646
|
+
admission?: Pick<SubmissionWorkItem, "threadId" | "submissionId">,
|
|
2629
2647
|
) {
|
|
2630
2648
|
const leanSelection =
|
|
2631
2649
|
framework.singleThreadRuntime === runtime &&
|
|
@@ -2713,7 +2731,21 @@ export class ThreadMaintenance extends Context.Service<
|
|
|
2713
2731
|
let result: NativePassResult;
|
|
2714
2732
|
let backlog: ReadonlyArray<ThreadId> | undefined;
|
|
2715
2733
|
|
|
2716
|
-
if (leanSelection) {
|
|
2734
|
+
if (leanSelection && admission !== undefined && started._tag === "Actionable") {
|
|
2735
|
+
// Admission already identified the work. Its exact FIFO claim and owner generation
|
|
2736
|
+
// remain authoritative; the normal loop discovers and acknowledges everything after it.
|
|
2737
|
+
dispatch.dispatched++;
|
|
2738
|
+
dispatch.active.set(
|
|
2739
|
+
admission.threadId,
|
|
2740
|
+
yield* fork(attemptSelected(admission, [], yieldAfter, recovery, true)),
|
|
2741
|
+
);
|
|
2742
|
+
result = {
|
|
2743
|
+
phase: "actionable",
|
|
2744
|
+
nonterminal: started.nonterminal,
|
|
2745
|
+
nextAttemptAt: undefined,
|
|
2746
|
+
dispatched: true,
|
|
2747
|
+
};
|
|
2748
|
+
} else if (leanSelection) {
|
|
2717
2749
|
result = yield* advance(started, yieldAfter, observed, recovery, dispatch);
|
|
2718
2750
|
recovery.started = true;
|
|
2719
2751
|
yield* Deferred.succeed(recovery.queue, []);
|
|
@@ -3061,9 +3093,10 @@ export class ThreadMaintenance extends Context.Service<
|
|
|
3061
3093
|
yieldAfter: DateTime.Utc,
|
|
3062
3094
|
dispatchUntil: DateTime.Utc,
|
|
3063
3095
|
observed: MaintenanceObservation,
|
|
3096
|
+
admission?: Pick<SubmissionWorkItem, "threadId" | "submissionId">,
|
|
3064
3097
|
): Effect.fn.Return<MaintenancePassReport, MaintenancePassFailure, Scope.Scope> {
|
|
3065
3098
|
const concurrent = yield* Effect.exit(
|
|
3066
|
-
Effect.scoped(dispatch(yieldAfter, dispatchUntil, observed)).pipe(
|
|
3099
|
+
Effect.scoped(dispatch(yieldAfter, dispatchUntil, observed, admission)).pipe(
|
|
3067
3100
|
Effect.provideService(ThreadMutationGate, mutations),
|
|
3068
3101
|
),
|
|
3069
3102
|
);
|
|
@@ -3245,6 +3278,13 @@ export class ThreadMaintenance extends Context.Service<
|
|
|
3245
3278
|
|
|
3246
3279
|
return "rearmed" as const;
|
|
3247
3280
|
}
|
|
3281
|
+
// A productive event retains one recovery opportunity for nearby ingress.
|
|
3282
|
+
// Its later empty delivery has no charged work and removes the checkpoint.
|
|
3283
|
+
if (config.recoveryWindowMillis > 0 && observed.charged.size > 0) {
|
|
3284
|
+
checkpoint.at(now + config.recoveryWindowMillis);
|
|
3285
|
+
|
|
3286
|
+
return "rearmed" as const;
|
|
3287
|
+
}
|
|
3248
3288
|
checkpoint.cancel();
|
|
3249
3289
|
|
|
3250
3290
|
return "cleared" as const;
|
|
@@ -3274,9 +3314,9 @@ export class ThreadMaintenance extends Context.Service<
|
|
|
3274
3314
|
return report;
|
|
3275
3315
|
});
|
|
3276
3316
|
|
|
3277
|
-
|
|
3278
|
-
|
|
3279
|
-
|
|
3317
|
+
// A mid-pass immediate hint is droppable; durable dirty state decides the final alarm.
|
|
3318
|
+
const runPass = Effect.fnUntraced(
|
|
3319
|
+
function* (admission: Pick<SubmissionWorkItem, "threadId" | "submissionId"> | undefined) {
|
|
3280
3320
|
const now = yield* Clock.currentTimeMillis;
|
|
3281
3321
|
const yieldAfter = DateTime.makeUnsafe(now + 10 * 60_000);
|
|
3282
3322
|
const dispatchUntil = DateTime.makeUnsafe(now + 14 * 60_000);
|
|
@@ -3291,14 +3331,16 @@ export class ThreadMaintenance extends Context.Service<
|
|
|
3291
3331
|
return yield* alarms
|
|
3292
3332
|
.deferWakes(
|
|
3293
3333
|
maintenancePassGate.withPermit(
|
|
3294
|
-
dueQueue
|
|
3295
|
-
|
|
3296
|
-
|
|
3297
|
-
|
|
3298
|
-
|
|
3299
|
-
(
|
|
3334
|
+
dueQueue
|
|
3335
|
+
.withView(Effect.scoped(pass(yieldAfter, dispatchUntil, observed, admission)))
|
|
3336
|
+
.pipe(
|
|
3337
|
+
// Close event-owned auxiliary work and release Attempt ownership before
|
|
3338
|
+
// failure rearming, while still holding the pass permit.
|
|
3339
|
+
Effect.onErrorIf(
|
|
3340
|
+
() => true,
|
|
3341
|
+
() => rearmFailure(observed),
|
|
3342
|
+
),
|
|
3300
3343
|
),
|
|
3301
|
-
),
|
|
3302
3344
|
),
|
|
3303
3345
|
)
|
|
3304
3346
|
.pipe(
|
|
@@ -3309,22 +3351,26 @@ export class ThreadMaintenance extends Context.Service<
|
|
|
3309
3351
|
alarmFailure("defer maintenance wakes")(error),
|
|
3310
3352
|
}),
|
|
3311
3353
|
);
|
|
3312
|
-
}
|
|
3313
|
-
|
|
3314
|
-
|
|
3315
|
-
|
|
3316
|
-
|
|
3317
|
-
|
|
3318
|
-
|
|
3319
|
-
|
|
3320
|
-
|
|
3321
|
-
|
|
3322
|
-
|
|
3323
|
-
|
|
3324
|
-
|
|
3325
|
-
|
|
3326
|
-
|
|
3327
|
-
|
|
3354
|
+
},
|
|
3355
|
+
// Include permit waiting, recovery and acknowledgement in the event deadline.
|
|
3356
|
+
// Interruption releases Attempt ownership, leaving the prearmed dirty generation
|
|
3357
|
+
// for recovery. It never changes the logical Run duration or settles a policy failure.
|
|
3358
|
+
// This cooperative timer cannot preempt synchronous CPU work or stuck finalizers.
|
|
3359
|
+
Effect.timeoutOrElse({
|
|
3360
|
+
duration: "14 minutes",
|
|
3361
|
+
orElse: () =>
|
|
3362
|
+
DurableAlarmError.make({
|
|
3363
|
+
operation: "maintenance pass deadline",
|
|
3364
|
+
message:
|
|
3365
|
+
"The maintenance event exceeded its 14 minute deadline; durable recovery remains pending",
|
|
3366
|
+
}),
|
|
3367
|
+
}),
|
|
3368
|
+
Effect.ensuring(reportParked),
|
|
3369
|
+
);
|
|
3370
|
+
|
|
3371
|
+
return ThreadMaintenance.of({
|
|
3372
|
+
pass: runPass(undefined),
|
|
3373
|
+
start: (admission) => execution.waitUntil(runPass(admission)),
|
|
3328
3374
|
ensureAlarm: mutations
|
|
3329
3375
|
.withSnapshot(() => ensureAlarm())
|
|
3330
3376
|
.pipe(Effect.tap(() => reportParked)),
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { type ThreadId } from "@yielded/agent/identifiers";
|
|
2
2
|
import { type ProducerId } from "@yielded/agent/records";
|
|
3
|
-
import { Context, Effect, Layer, Predicate, Schema } from "effect";
|
|
4
3
|
import { RpcTargets } from "effect-cf";
|
|
4
|
+
import * as Context from "effect/Context";
|
|
5
|
+
import * as Effect from "effect/Effect";
|
|
6
|
+
import * as Layer from "effect/Layer";
|
|
7
|
+
import * as Predicate from "effect/Predicate";
|
|
8
|
+
import * as Schema from "effect/Schema";
|
|
5
9
|
|
|
6
10
|
/**
|
|
7
11
|
* Cloudflare platform bindings as Effect services (DEPLOY-010: "Cloudflare platform bindings
|
package/src/CloudflareConfig.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { DEFAULT_MAX_STORED_VALUE_BYTES } from "@yielded/agent-storage-cloudflare/do-storage-config";
|
|
2
2
|
import { DeploymentId } from "@yielded/agent/records";
|
|
3
|
-
import
|
|
3
|
+
import * as Context from "effect/Context";
|
|
4
|
+
import * as Schema from "effect/Schema";
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Schema-validated configuration for the Cloudflare durable runtime (deployment spec §4:
|
|
@@ -14,6 +15,9 @@ const PositiveMillis = Schema.Int.check(Schema.isGreaterThan(0));
|
|
|
14
15
|
export const AuxiliaryDispatchMillis = PositiveMillis.check(Schema.isLessThanOrEqualTo(300_000));
|
|
15
16
|
const NonNegativeMillis = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0));
|
|
16
17
|
|
|
18
|
+
/** Recovery coverage retained after activity; independent of work-start and retry deadlines. */
|
|
19
|
+
export const DEFAULT_RECOVERY_WINDOW_MILLIS = 60_000;
|
|
20
|
+
|
|
17
21
|
/** The supplied Cloudflare durable runtime configuration failed validation (DEPLOY-003). */
|
|
18
22
|
export class CloudflarePlatformConfigError extends Schema.TaggedError<CloudflarePlatformConfigError>()(
|
|
19
23
|
"CloudflarePlatformConfigError",
|
|
@@ -87,10 +91,12 @@ export class CloudflareDurableRuntimeConfigValue extends Schema.Class<Cloudflare
|
|
|
87
91
|
deploymentId: DeploymentId,
|
|
88
92
|
/** Head of the minted producer identity `{producerPrefix}:{threadId}`. */
|
|
89
93
|
producerPrefix: Schema.NonEmptyString.check(Schema.isMaxLength(256)),
|
|
94
|
+
/** Recovery-only window. Zero restores immediate idle cancellation and the short guards. */
|
|
95
|
+
recoveryWindowMillis: NonNegativeMillis,
|
|
90
96
|
/**
|
|
91
97
|
* Base delay of exponential backoff when a pass fails or makes no progress.
|
|
92
|
-
* Half this delay (rounded up, at least 1ms)
|
|
93
|
-
*
|
|
98
|
+
* Half this delay (rounded up, at least 1ms) starts alarm-driven work and recovers
|
|
99
|
+
* startup work. It also pre-arms mutations when the recovery window is disabled.
|
|
94
100
|
*/
|
|
95
101
|
alarmBackoffBase: PositiveMillis,
|
|
96
102
|
/** Ceiling of exponential backoff after failed or no-progress maintenance passes. */
|
|
@@ -121,6 +127,7 @@ export class CloudflareDurableRuntimeConfig extends Context.Service<
|
|
|
121
127
|
|
|
122
128
|
/** Documented production defaults applied by `ThreadObject.layer`. */
|
|
123
129
|
export const CLOUDFLARE_RUNTIME_DEFAULTS = {
|
|
130
|
+
recoveryWindowMillis: DEFAULT_RECOVERY_WINDOW_MILLIS,
|
|
124
131
|
alarmBackoffBase: 100,
|
|
125
132
|
alarmBackoffCap: 5_000,
|
|
126
133
|
settlementPollInterval: 500,
|
|
@@ -44,8 +44,15 @@ import {
|
|
|
44
44
|
ThreadNotMaterialized,
|
|
45
45
|
ThreadStoreError,
|
|
46
46
|
} from "@yielded/agent/thread-store";
|
|
47
|
-
import { Context, Crypto, Duration, Effect, Layer, Predicate, Schema, Stream } from "effect";
|
|
48
47
|
import { RpcTargets, RpcTracing } from "effect-cf";
|
|
48
|
+
import * as Context from "effect/Context";
|
|
49
|
+
import * as Crypto from "effect/Crypto";
|
|
50
|
+
import * as Duration from "effect/Duration";
|
|
51
|
+
import * as Effect from "effect/Effect";
|
|
52
|
+
import * as Layer from "effect/Layer";
|
|
53
|
+
import * as Predicate from "effect/Predicate";
|
|
54
|
+
import * as Schema from "effect/Schema";
|
|
55
|
+
import * as Stream from "effect/Stream";
|
|
49
56
|
|
|
50
57
|
import {
|
|
51
58
|
callThreadObject,
|
package/src/ThreadObject.ts
CHANGED
|
@@ -52,13 +52,19 @@ import {
|
|
|
52
52
|
} from "@yielded/agent/thread-store";
|
|
53
53
|
import { WorkDiscoveryUnavailable } from "@yielded/agent/thread-work";
|
|
54
54
|
import { WakeScheduler } from "@yielded/agent/wake-scheduler";
|
|
55
|
-
import { Context, Effect, Layer, Option, Schema, Stream, type Scope } from "effect";
|
|
56
55
|
import {
|
|
57
56
|
DurableObject as EffectCfDurableObject,
|
|
58
57
|
DurableObjectAlarm,
|
|
59
58
|
DurableObjectState as EffectCfDurableObjectState,
|
|
60
59
|
WorkerEnvironment,
|
|
61
60
|
} from "effect-cf";
|
|
61
|
+
import * as Context from "effect/Context";
|
|
62
|
+
import * as Effect from "effect/Effect";
|
|
63
|
+
import * as Layer from "effect/Layer";
|
|
64
|
+
import * as Option from "effect/Option";
|
|
65
|
+
import * as Schema from "effect/Schema";
|
|
66
|
+
import type * as Scope from "effect/Scope";
|
|
67
|
+
import * as Stream from "effect/Stream";
|
|
62
68
|
|
|
63
69
|
import {
|
|
64
70
|
ThreadMaintenance,
|
|
@@ -523,14 +529,11 @@ const submitEndpoint = (encoded: unknown): Effect.Effect<unknown, never, Endpoin
|
|
|
523
529
|
const scheduling = yield* DurableObjectAlarm.ScheduleConfiguration;
|
|
524
530
|
|
|
525
531
|
if (scheduling.dispatchAfterEvent === true) {
|
|
526
|
-
const
|
|
532
|
+
const maintenance = yield* ThreadMaintenance;
|
|
527
533
|
|
|
528
|
-
//
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
Effect.logWarning("Post-admission maintenance hint failed", error),
|
|
532
|
-
),
|
|
533
|
-
);
|
|
534
|
+
// effect-cf owns the background scope and native event lifetime. Admission is
|
|
535
|
+
// already durable; neither the Attempt nor its model request delays this receipt.
|
|
536
|
+
yield* maintenance.start(receipt);
|
|
534
537
|
}
|
|
535
538
|
|
|
536
539
|
return SubmitSucceeded.make({ receipt });
|
|
@@ -1290,10 +1293,12 @@ export const make = <
|
|
|
1290
1293
|
| AlarmServices
|
|
1291
1294
|
> = Layer.flatMap(application, (applicationServices) => {
|
|
1292
1295
|
const scheduling = Context.get(applicationServices, DurableObjectAlarm.ScheduleConfiguration);
|
|
1296
|
+
const config = Context.get(applicationServices, CloudflareDurableRuntimeConfig);
|
|
1293
1297
|
|
|
1294
1298
|
const services = Context.add(applicationServices, DurableObjectAlarm.ScheduleConfiguration, {
|
|
1295
1299
|
...scheduling,
|
|
1296
1300
|
dispatchAfterEvent: scheduling.dispatchAfterEvent ?? true,
|
|
1301
|
+
inFlightRecovery: config.recoveryWindowMillis || (scheduling.inFlightRecovery ?? 1_000),
|
|
1297
1302
|
});
|
|
1298
1303
|
|
|
1299
1304
|
return Layer.effectContext(gateEndpoint.pipe(Effect.provide(services), Effect.as(services)));
|
package/src/WakeScheduler.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { CurrentNativeSource } from "@yielded/agent-storage-cloudflare/port-routing";
|
|
2
2
|
import { type ThreadId } from "@yielded/agent/identifiers";
|
|
3
3
|
import { makeWakeSubscriptionHub, WakeScheduler } from "@yielded/agent/wake-scheduler";
|
|
4
|
-
import
|
|
4
|
+
import * as Effect from "effect/Effect";
|
|
5
|
+
import * as Layer from "effect/Layer";
|
|
6
|
+
import * as PubSub from "effect/PubSub";
|
|
7
|
+
import * as Schema from "effect/Schema";
|
|
8
|
+
import * as Stream from "effect/Stream";
|
|
5
9
|
|
|
6
10
|
import { DurableAlarmService } from "./Alarm.ts";
|
|
7
11
|
import {
|
package/src/internal/boundary.ts
CHANGED
package/src/internal/crypto.ts
CHANGED
|
@@ -3,7 +3,10 @@
|
|
|
3
3
|
import { createHash } from "node:crypto";
|
|
4
4
|
|
|
5
5
|
import { BrowserCrypto } from "@effect/platform-browser";
|
|
6
|
-
import
|
|
6
|
+
import * as Crypto from "effect/Crypto";
|
|
7
|
+
import * as Effect from "effect/Effect";
|
|
8
|
+
import * as Layer from "effect/Layer";
|
|
9
|
+
import * as PlatformError from "effect/PlatformError";
|
|
7
10
|
|
|
8
11
|
/** Workers use native SHA-256 while retaining BrowserCrypto's services and overrides. */
|
|
9
12
|
export const cloudflareCryptoLayer: Layer.Layer<Crypto.Crypto> = Layer.effect(
|
package/src/internal/layers.ts
CHANGED
|
@@ -95,21 +95,20 @@ import {
|
|
|
95
95
|
} from "@yielded/agent/thread-store";
|
|
96
96
|
import { ToolReconciler } from "@yielded/agent/tool-reconciler";
|
|
97
97
|
import { type WakeScheduler } from "@yielded/agent/wake-scheduler";
|
|
98
|
-
import {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
} from "effect";
|
|
98
|
+
import { DurableObjectAlarm } from "effect-cf";
|
|
99
|
+
import * as Cause from "effect/Cause";
|
|
100
|
+
import * as Clock from "effect/Clock";
|
|
101
|
+
import * as Context from "effect/Context";
|
|
102
|
+
import type * as Crypto from "effect/Crypto";
|
|
103
|
+
import * as Duration from "effect/Duration";
|
|
104
|
+
import * as Effect from "effect/Effect";
|
|
105
|
+
import * as ErrorReporter from "effect/ErrorReporter";
|
|
106
|
+
import * as Exit from "effect/Exit";
|
|
107
|
+
import * as Layer from "effect/Layer";
|
|
108
|
+
import * as Match from "effect/Match";
|
|
109
|
+
import * as Option from "effect/Option";
|
|
110
|
+
import * as Schema from "effect/Schema";
|
|
111
|
+
import * as Semaphore from "effect/Semaphore";
|
|
113
112
|
import { SqlClient } from "effect/sql/SqlClient";
|
|
114
113
|
|
|
115
114
|
import {
|
|
@@ -158,6 +157,8 @@ export interface CloudflareDurableRuntimeOptions {
|
|
|
158
157
|
readonly deploymentId: string;
|
|
159
158
|
/** Head of the minted producer identity `{producerPrefix}:{threadId}`. */
|
|
160
159
|
readonly producerPrefix: string;
|
|
160
|
+
/** Recovery-only window in milliseconds; default 60000. Zero disables idle retention. */
|
|
161
|
+
readonly recoveryWindowMillis?: number | undefined;
|
|
161
162
|
/** Milliseconds; default 100. */
|
|
162
163
|
readonly alarmBackoffBase?: number | undefined;
|
|
163
164
|
/** Failed/no-progress exponential backoff ceiling in milliseconds; default 5000. */
|
|
@@ -284,6 +285,8 @@ const configFromOptions = (
|
|
|
284
285
|
decodeConfigValue({
|
|
285
286
|
deploymentId: options.deploymentId,
|
|
286
287
|
producerPrefix: options.producerPrefix,
|
|
288
|
+
recoveryWindowMillis:
|
|
289
|
+
options.recoveryWindowMillis ?? CLOUDFLARE_RUNTIME_DEFAULTS.recoveryWindowMillis,
|
|
287
290
|
alarmBackoffBase: options.alarmBackoffBase ?? CLOUDFLARE_RUNTIME_DEFAULTS.alarmBackoffBase,
|
|
288
291
|
alarmBackoffCap: options.alarmBackoffCap ?? CLOUDFLARE_RUNTIME_DEFAULTS.alarmBackoffCap,
|
|
289
292
|
settlementPollInterval:
|
|
@@ -624,6 +627,7 @@ const sharedLayer = <A, E, R, PE = never, PR = never>(
|
|
|
624
627
|
const { ctx } = yield* DurableObjectContext;
|
|
625
628
|
const config = yield* CloudflareDurableRuntimeConfig;
|
|
626
629
|
const placement = yield* ThreadObjectPlacement;
|
|
630
|
+
const scheduling = yield* DurableObjectAlarm.ScheduleConfiguration;
|
|
627
631
|
const { ownsThread } = placement;
|
|
628
632
|
|
|
629
633
|
const storageOptions: DoStorageOptions = {
|
|
@@ -1049,7 +1053,10 @@ const sharedLayer = <A, E, R, PE = never, PR = never>(
|
|
|
1049
1053
|
| MessageDeliveryStore
|
|
1050
1054
|
| WakeScheduler
|
|
1051
1055
|
| DurableRuntimeFailpoint
|
|
1052
|
-
>()
|
|
1056
|
+
>().pipe(
|
|
1057
|
+
// The request's effective scheduling policy is installed after the ports build.
|
|
1058
|
+
Effect.map(Context.omit(DurableObjectAlarm.ScheduleConfiguration)),
|
|
1059
|
+
);
|
|
1053
1060
|
|
|
1054
1061
|
const lookupSubmission = makeLocalSubmissionLookup({ ownsThread });
|
|
1055
1062
|
|
|
@@ -1192,6 +1199,12 @@ const sharedLayer = <A, E, R, PE = never, PR = never>(
|
|
|
1192
1199
|
Layer.provideMerge(sourceOwner),
|
|
1193
1200
|
Layer.provideMerge(ThreadMutationGate.layer),
|
|
1194
1201
|
Layer.provideMerge(infrastructure),
|
|
1202
|
+
Layer.provideMerge(
|
|
1203
|
+
Layer.succeed(DurableObjectAlarm.ScheduleConfiguration, {
|
|
1204
|
+
...scheduling,
|
|
1205
|
+
inFlightRecovery: config.recoveryWindowMillis || (scheduling.inFlightRecovery ?? 1_000),
|
|
1206
|
+
}),
|
|
1207
|
+
),
|
|
1195
1208
|
);
|
|
1196
1209
|
}),
|
|
1197
1210
|
);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ThreadId } from "@yielded/agent/identifiers";
|
|
2
2
|
import { Event } from "@yielded/agent/provisional-text";
|
|
3
|
-
import { Schema } from "effect";
|
|
4
3
|
import { Rpc } from "effect-cf";
|
|
4
|
+
import * as Schema from "effect/Schema";
|
|
5
5
|
|
|
6
6
|
export const MAX_FRAME_BYTES = 32 * 1_024;
|
|
7
7
|
export const MAX_ID_LENGTH = 256;
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { type ThreadId } from "@yielded/agent/identifiers";
|
|
2
2
|
import { Publisher, type Event } from "@yielded/agent/provisional-text";
|
|
3
3
|
import { RpcTarget } from "cloudflare:workers";
|
|
4
|
-
import { Context, Effect, Layer, Schema } from "effect";
|
|
5
4
|
import * as Response from "effect/ai/Response";
|
|
5
|
+
import * as Context from "effect/Context";
|
|
6
|
+
import * as Effect from "effect/Effect";
|
|
7
|
+
import * as Layer from "effect/Layer";
|
|
8
|
+
import * as Schema from "effect/Schema";
|
|
6
9
|
|
|
7
10
|
import { HostProtocolError } from "../CloudflareThreadClient.ts";
|
|
8
11
|
import {
|
|
@@ -5,7 +5,13 @@ import {
|
|
|
5
5
|
MessageDeliveryStore,
|
|
6
6
|
} from "@yielded/agent/message-delivery";
|
|
7
7
|
import { WakeScheduler } from "@yielded/agent/wake-scheduler";
|
|
8
|
-
import
|
|
8
|
+
import * as Clock from "effect/Clock";
|
|
9
|
+
import * as Context from "effect/Context";
|
|
10
|
+
import * as Duration from "effect/Duration";
|
|
11
|
+
import * as Effect from "effect/Effect";
|
|
12
|
+
import * as Exit from "effect/Exit";
|
|
13
|
+
import * as Layer from "effect/Layer";
|
|
14
|
+
import * as Option from "effect/Option";
|
|
9
15
|
|
|
10
16
|
import { DurableAlarmError, ThreadMessageDelivery, ThreadMutationGate } from "../Alarm.ts";
|
|
11
17
|
import { ThreadObjectPlacement } from "../CloudflareBindings.ts";
|
|
@@ -10,7 +10,8 @@ import {
|
|
|
10
10
|
ScheduleStorageError,
|
|
11
11
|
} from "@yielded/agent/schedule";
|
|
12
12
|
import { type PreparedInput } from "@yielded/agent/subscription";
|
|
13
|
-
import
|
|
13
|
+
import * as Effect from "effect/Effect";
|
|
14
|
+
import * as Layer from "effect/Layer";
|
|
14
15
|
|
|
15
16
|
import { CloudflareThreadClient, type ThreadClientError } from "../CloudflareThreadClient.ts";
|
|
16
17
|
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as Context from "effect/Context";
|
|
2
|
+
import * as Deferred from "effect/Deferred";
|
|
3
|
+
import * as Effect from "effect/Effect";
|
|
4
|
+
import * as Layer from "effect/Layer";
|
|
5
|
+
import * as Ref from "effect/Ref";
|
|
6
|
+
import type * as Scope from "effect/Scope";
|
|
2
7
|
|
|
3
8
|
/** Cancellation tombstones are bounded hints, never durable authority. */
|
|
4
9
|
const MAX_CANCELLATION_TOMBSTONES = 1_024;
|
|
@@ -3,8 +3,9 @@ import {
|
|
|
3
3
|
portTransportFailure,
|
|
4
4
|
} from "@yielded/agent-storage-cloudflare/port-routing";
|
|
5
5
|
import type { ThreadId } from "@yielded/agent/identifiers";
|
|
6
|
-
import { Effect, Layer } from "effect";
|
|
7
6
|
import { RpcTracing } from "effect-cf";
|
|
7
|
+
import * as Effect from "effect/Effect";
|
|
8
|
+
import * as Layer from "effect/Layer";
|
|
8
9
|
|
|
9
10
|
import { callThreadObject, ThreadObjectNamespace } from "../CloudflareBindings.ts";
|
|
10
11
|
|