@effect-agent/platform-cloudflare 0.1.0-beta.9 → 0.1.0-beta.91
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.d.mts +188 -0
- package/dist/Alarm.mjs +451 -0
- package/dist/Alarm.mjs.map +1 -0
- package/dist/BrowserRestCapture.d.mts +35 -0
- package/dist/BrowserRestCapture.mjs +238 -0
- package/dist/BrowserRestCapture.mjs.map +1 -0
- package/dist/BrowserRestCrawl.d.mts +17 -0
- package/dist/BrowserRestCrawl.mjs +244 -0
- package/dist/BrowserRestCrawl.mjs.map +1 -0
- package/dist/CloudflareAiGateway.d.mts +64 -0
- package/dist/CloudflareAiGateway.mjs +70 -0
- package/dist/CloudflareAiGateway.mjs.map +1 -0
- package/dist/CloudflareBindings.d.mts +115 -0
- package/dist/CloudflareBindings.mjs +113 -0
- package/dist/CloudflareBindings.mjs.map +1 -0
- package/dist/CloudflareBrowser-DV6kya1H.mjs +495 -0
- package/dist/CloudflareBrowser-DV6kya1H.mjs.map +1 -0
- package/dist/CloudflareBrowser-Doe1M7R5.d.mts +80 -0
- package/dist/CloudflareBrowser.d.mts +2 -0
- package/dist/CloudflareBrowser.mjs +2 -0
- package/dist/CloudflareCodeMode.d.mts +44 -0
- package/dist/CloudflareCodeMode.mjs +616 -0
- package/dist/CloudflareCodeMode.mjs.map +1 -0
- package/dist/CloudflareConfig-f3CqTel1.d.mts +101 -0
- package/dist/CloudflareConfig.d.mts +2 -0
- package/dist/CloudflareConfig.mjs +122 -0
- package/dist/CloudflareConfig.mjs.map +1 -0
- package/dist/CloudflareMemory.d.mts +142 -0
- package/dist/CloudflareMemory.mjs +202 -0
- package/dist/CloudflareMemory.mjs.map +1 -0
- package/dist/CloudflareScheduling.d.mts +52 -0
- package/dist/CloudflareScheduling.mjs +396 -0
- package/dist/CloudflareScheduling.mjs.map +1 -0
- package/dist/CloudflareSubscriptions.d.mts +87 -0
- package/dist/CloudflareSubscriptions.mjs +530 -0
- package/dist/CloudflareSubscriptions.mjs.map +1 -0
- package/dist/CloudflareThreadClient.d.mts +1496 -0
- package/dist/CloudflareThreadClient.mjs +382 -0
- package/dist/CloudflareThreadClient.mjs.map +1 -0
- package/dist/InteractiveBrowser-DUE_m12-.d.mts +161 -0
- package/dist/InteractiveBrowser-DupDtfw2.mjs +1169 -0
- package/dist/InteractiveBrowser-DupDtfw2.mjs.map +1 -0
- package/dist/InteractiveBrowser.d.mts +2 -0
- package/dist/InteractiveBrowser.mjs +2 -0
- package/dist/ProtectedBrowser.d.mts +123 -0
- package/dist/ProtectedBrowser.mjs +1135 -0
- package/dist/ProtectedBrowser.mjs.map +1 -0
- package/dist/ThreadObject-CIbmXZCY.d.mts +812 -0
- package/dist/ThreadObject-DvRG0dDz.mjs +835 -0
- package/dist/ThreadObject-DvRG0dDz.mjs.map +1 -0
- package/dist/ThreadObject.d.mts +2 -0
- package/dist/ThreadObject.mjs +3 -0
- package/dist/WakeScheduler.d.mts +23 -0
- package/dist/WakeScheduler.mjs +57 -0
- package/dist/WakeScheduler.mjs.map +1 -0
- package/dist/boundary-BguazVkh.mjs +42 -0
- package/dist/boundary-BguazVkh.mjs.map +1 -0
- package/dist/index.d.mts +13 -1548
- package/dist/index.mjs +13 -1636
- package/dist/prepared-admission-DU0_T-ev.mjs +73 -0
- package/dist/prepared-admission-DU0_T-ev.mjs.map +1 -0
- package/dist/rolldown-runtime-D7D4PA-g.mjs +13 -0
- package/package.json +1 -53
- package/src/Alarm.ts +973 -0
- package/src/BrowserRestCapture.ts +477 -0
- package/src/BrowserRestCrawl.ts +540 -0
- package/src/CloudflareAiGateway.ts +170 -0
- package/src/CloudflareBindings.ts +199 -0
- package/src/CloudflareBrowser.ts +15 -0
- package/src/{code-mode-executor.ts → CloudflareCodeMode.ts} +372 -204
- package/src/{config.ts → CloudflareConfig.ts} +9 -8
- package/src/CloudflareMemory.ts +420 -0
- package/src/CloudflareScheduling.ts +747 -0
- package/src/CloudflareSubscriptions.ts +1041 -0
- package/src/CloudflareThreadClient.ts +809 -0
- package/src/InteractiveBrowser.ts +2354 -0
- package/src/ProtectedBrowser.ts +9 -0
- package/src/ThreadObject.ts +1179 -0
- package/src/{wake-scheduler.ts → WakeScheduler.ts} +32 -26
- package/src/index.ts +12 -23
- package/src/internal/boundary.ts +55 -0
- package/src/internal/browser-quick-action.ts +857 -0
- package/src/internal/browser-session-lifecycle.ts +160 -0
- package/src/internal/layers.ts +751 -0
- package/src/internal/message-delivery.ts +152 -0
- package/src/internal/prepared-admission.ts +116 -0
- package/src/internal/progress-wait.ts +121 -0
- package/src/internal/transport.ts +44 -0
- package/src/protected-browser/binding.ts +313 -0
- package/src/protected-browser/host.ts +344 -0
- package/src/protected-browser/inspect-frame.ts +125 -0
- package/src/protected-browser/native.ts +445 -0
- package/src/protected-browser/policy.ts +821 -0
- package/dist/index.mjs.map +0 -1
- package/src/alarm.ts +0 -334
- package/src/bindings.ts +0 -145
- package/src/client.ts +0 -646
- package/src/conversation-object.ts +0 -768
- package/src/layers.ts +0 -376
- package/src/transport.ts +0 -38
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { Clock, Context, Deferred, Effect, Layer, Option, Ref, Semaphore, Stream } from "effect";
|
|
2
|
+
import { type ThreadId } from "effect-agent/identifiers";
|
|
3
|
+
import {
|
|
4
|
+
MessageDeliveryDriver,
|
|
5
|
+
MessageDeliveryError,
|
|
6
|
+
MessageDeliveryStore,
|
|
7
|
+
} from "effect-agent/message-delivery";
|
|
8
|
+
import { WakeScheduler } from "effect-agent/wake-scheduler";
|
|
9
|
+
|
|
10
|
+
import { DurableAlarmError, ThreadMessageDelivery, ThreadMutationGate } from "../Alarm.ts";
|
|
11
|
+
import { ThreadObjectPlacement } from "../CloudflareBindings.ts";
|
|
12
|
+
import { CloudflareDurableRuntimeConfig } from "../CloudflareConfig.ts";
|
|
13
|
+
|
|
14
|
+
/** Every write prearms its owner; the delivery due index owns its recovery deadline. */
|
|
15
|
+
export const guardedMessageDeliveryStoreLayer = Layer.effect(
|
|
16
|
+
MessageDeliveryStore,
|
|
17
|
+
Effect.gen(function* () {
|
|
18
|
+
const store = yield* MessageDeliveryStore;
|
|
19
|
+
const mutations = yield* ThreadMutationGate;
|
|
20
|
+
const wakes = yield* WakeScheduler;
|
|
21
|
+
const { ownsThread } = yield* ThreadObjectPlacement;
|
|
22
|
+
// Reconstructed as unknown on every incarnation. The gate prevents a racing read from
|
|
23
|
+
// caching an empty deadline across a write; SQL remains the recovery authority.
|
|
24
|
+
const deadline = yield* Ref.make<number | null | undefined>(undefined);
|
|
25
|
+
const cacheGate = yield* Semaphore.make(1);
|
|
26
|
+
|
|
27
|
+
const local = <A, E>(
|
|
28
|
+
owner: ThreadId | undefined,
|
|
29
|
+
body: Effect.Effect<A, E>,
|
|
30
|
+
): Effect.Effect<A, E | MessageDeliveryError> =>
|
|
31
|
+
owner === undefined || ownsThread(owner)
|
|
32
|
+
? body
|
|
33
|
+
: Effect.fail(
|
|
34
|
+
MessageDeliveryError.make({ reason: "validation", operation: "message owner" }),
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
const mutate = <A, E>(body: Effect.Effect<A, E>) =>
|
|
38
|
+
mutations
|
|
39
|
+
.withMutation(
|
|
40
|
+
cacheGate.withPermit(Ref.set(deadline, undefined).pipe(Effect.andThen(body))),
|
|
41
|
+
// A foreign receipt changing does not make the source ledger actionable. Keep the
|
|
42
|
+
// prearm and producer gate so eviction and a racing pass cannot lose delivery work.
|
|
43
|
+
{ invalidatesRecovery: false },
|
|
44
|
+
)
|
|
45
|
+
.pipe(
|
|
46
|
+
Effect.catchTag("DurableAlarmError", () =>
|
|
47
|
+
MessageDeliveryError.make({ reason: "storage", operation: "prearm message delivery" }),
|
|
48
|
+
),
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
const nextDeadline = cacheGate.withPermit(
|
|
52
|
+
Effect.gen(function* () {
|
|
53
|
+
const cached = yield* Ref.get(deadline);
|
|
54
|
+
|
|
55
|
+
if (cached !== undefined) return cached;
|
|
56
|
+
const current = yield* store.nextDeadline();
|
|
57
|
+
|
|
58
|
+
yield* Ref.set(deadline, current);
|
|
59
|
+
|
|
60
|
+
return current;
|
|
61
|
+
}),
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
return MessageDeliveryStore.of({
|
|
65
|
+
limits: store.limits,
|
|
66
|
+
maxStoredValueBytes: store.maxStoredValueBytes,
|
|
67
|
+
insert: (record) =>
|
|
68
|
+
local(
|
|
69
|
+
record.key.ownerThreadId,
|
|
70
|
+
mutate(store.insert(record)).pipe(
|
|
71
|
+
Effect.tap(() => wakes.notify(record.key.ownerThreadId)),
|
|
72
|
+
),
|
|
73
|
+
),
|
|
74
|
+
get: (key) => local(key.ownerThreadId, store.get(key)),
|
|
75
|
+
list: (request) => local(request.ownerThreadId, store.list(request)),
|
|
76
|
+
change: (key, change) => local(key.ownerThreadId, mutate(store.change(key, change))),
|
|
77
|
+
// Only the trusted physical-owner pump omits an owner. All returned keys still
|
|
78
|
+
// pass placement validation before the driver may dispatch any of the wave.
|
|
79
|
+
due: (nowMillis, limit, owner) =>
|
|
80
|
+
local(owner, store.due(nowMillis, limit, owner)).pipe(
|
|
81
|
+
Effect.filterOrFail(
|
|
82
|
+
(keys) => keys.every((key) => ownsThread(key.ownerThreadId)),
|
|
83
|
+
() => MessageDeliveryError.make({ reason: "validation", operation: "message owner" }),
|
|
84
|
+
),
|
|
85
|
+
),
|
|
86
|
+
nextDeadline: (owner) =>
|
|
87
|
+
local(owner, owner === undefined ? nextDeadline : store.nextDeadline(owner)),
|
|
88
|
+
});
|
|
89
|
+
}),
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
/** Message progress shares the native alarm slot without blocking source runtime work. */
|
|
93
|
+
export const threadMessageDeliveryLayer = Layer.effectContext(
|
|
94
|
+
Effect.gen(function* () {
|
|
95
|
+
const driver = yield* MessageDeliveryDriver;
|
|
96
|
+
const store = yield* MessageDeliveryStore;
|
|
97
|
+
const wakes = yield* WakeScheduler;
|
|
98
|
+
const config = yield* CloudflareDurableRuntimeConfig;
|
|
99
|
+
|
|
100
|
+
const failure = (operation: string) => () =>
|
|
101
|
+
DurableAlarmError.make({
|
|
102
|
+
operation,
|
|
103
|
+
message: "Durable message recovery remains pending",
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const drain = Effect.gen(function* () {
|
|
107
|
+
const deadline = yield* store.nextDeadline();
|
|
108
|
+
|
|
109
|
+
if (deadline !== null && deadline <= (yield* Clock.currentTimeMillis)) {
|
|
110
|
+
yield* driver.runDue();
|
|
111
|
+
}
|
|
112
|
+
}).pipe(Effect.mapError(failure("drain message delivery")));
|
|
113
|
+
|
|
114
|
+
return Context.make(ThreadMessageDelivery, {
|
|
115
|
+
drain,
|
|
116
|
+
drainUntil: Effect.fn("ThreadMessageDelivery.drainUntil")(function* (
|
|
117
|
+
finished: Deferred.Deferred<void>,
|
|
118
|
+
) {
|
|
119
|
+
// One scoped subscription covers every logical lane in this physical owner.
|
|
120
|
+
// Acquire it before reading durable deadlines; hints remain droppable.
|
|
121
|
+
const notified = (yield* Stream.toPull(wakes.wakes)).pipe(Effect.catch(() => Effect.never));
|
|
122
|
+
|
|
123
|
+
// Always finish one wave; source completion prevents starting subsequent waves.
|
|
124
|
+
yield* Effect.gen(function* () {
|
|
125
|
+
yield* drain;
|
|
126
|
+
|
|
127
|
+
const deadline = yield* store
|
|
128
|
+
.nextDeadline()
|
|
129
|
+
.pipe(Effect.mapError(failure("read message deadline")));
|
|
130
|
+
|
|
131
|
+
// The index includes unfinished waves, lease expiry, retry and settlement polls.
|
|
132
|
+
// Yield at least one millisecond for an already-due deadline instead of spinning.
|
|
133
|
+
const delay =
|
|
134
|
+
deadline === null
|
|
135
|
+
? config.wakeScanInterval
|
|
136
|
+
: Math.min(
|
|
137
|
+
config.wakeScanInterval,
|
|
138
|
+
Math.max(1, deadline - (yield* Clock.currentTimeMillis)),
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
yield* Effect.raceFirst(
|
|
142
|
+
Deferred.await(finished),
|
|
143
|
+
Effect.raceFirst(notified, Effect.sleep(delay)),
|
|
144
|
+
);
|
|
145
|
+
}).pipe(Effect.repeat({ until: () => Deferred.isDone(finished) }));
|
|
146
|
+
}, Effect.scoped),
|
|
147
|
+
pendingDeadline: store
|
|
148
|
+
.nextDeadline()
|
|
149
|
+
.pipe(Effect.map(Option.fromNullishOr), Effect.mapError(failure("read message deadline"))),
|
|
150
|
+
});
|
|
151
|
+
}),
|
|
152
|
+
);
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { Effect, Layer } from "effect";
|
|
2
|
+
import { type DurableSubmitAgent } from "effect-agent/durable-agent-runtime";
|
|
3
|
+
import { type AgentId } from "effect-agent/identifiers";
|
|
4
|
+
import { PreparedInputAdmission } from "effect-agent/prepared-input-admission";
|
|
5
|
+
import { PersistedJson } from "effect-agent/records";
|
|
6
|
+
import {
|
|
7
|
+
type ScheduledEnvelope,
|
|
8
|
+
ScheduledInputAdmission,
|
|
9
|
+
ScheduledInputRetryable,
|
|
10
|
+
ScheduledInputRefused,
|
|
11
|
+
ScheduleStorageError,
|
|
12
|
+
} from "effect-agent/schedule";
|
|
13
|
+
import { type PreparedInput } from "effect-agent/subscription";
|
|
14
|
+
|
|
15
|
+
import { CloudflareThreadClient, type ThreadClientError } from "../CloudflareThreadClient.ts";
|
|
16
|
+
|
|
17
|
+
const passthroughAgent = (agentId: AgentId): DurableSubmitAgent<typeof PersistedJson> => ({
|
|
18
|
+
definition: { id: agentId, input: PersistedJson },
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
/** Ordinary prepared admission through one freshly addressed Thread Object call. */
|
|
22
|
+
export const cloudflarePreparedInputAdmissionLayer: Layer.Layer<
|
|
23
|
+
PreparedInputAdmission,
|
|
24
|
+
never,
|
|
25
|
+
CloudflareThreadClient
|
|
26
|
+
> = Layer.effect(
|
|
27
|
+
PreparedInputAdmission,
|
|
28
|
+
Effect.gen(function* () {
|
|
29
|
+
const client = yield* CloudflareThreadClient;
|
|
30
|
+
|
|
31
|
+
return PreparedInputAdmission.of({
|
|
32
|
+
submissionStatus: (receipt) =>
|
|
33
|
+
client
|
|
34
|
+
.submissionStatus(receipt)
|
|
35
|
+
.pipe(Effect.mapError(() => ScheduledInputRetryable.make({ reason: "storage" }))),
|
|
36
|
+
submit: (envelope) =>
|
|
37
|
+
client
|
|
38
|
+
.submit(passthroughAgent(envelope.agentId), envelope.input, {
|
|
39
|
+
threadId: envelope.threadId,
|
|
40
|
+
principal: envelope.deliveryPrincipal,
|
|
41
|
+
idempotencyKey: envelope.admissionKey,
|
|
42
|
+
...(envelope.admissionGroup === undefined
|
|
43
|
+
? {}
|
|
44
|
+
: { admissionGroup: envelope.admissionGroup }),
|
|
45
|
+
...(envelope.admissionFence === undefined
|
|
46
|
+
? {}
|
|
47
|
+
: { admissionFence: envelope.admissionFence }),
|
|
48
|
+
...(envelope.workerAdmission === undefined
|
|
49
|
+
? {}
|
|
50
|
+
: { workerAdmission: envelope.workerAdmission }),
|
|
51
|
+
...(envelope.messageAdmission === undefined
|
|
52
|
+
? {}
|
|
53
|
+
: { messageAdmission: envelope.messageAdmission }),
|
|
54
|
+
definitions: envelope.definitions,
|
|
55
|
+
})
|
|
56
|
+
.pipe(
|
|
57
|
+
Effect.catchTags({
|
|
58
|
+
AdmissionConflict: () =>
|
|
59
|
+
ScheduleStorageError.make({ operation: "prepared admission", reason: "corrupt" }),
|
|
60
|
+
AdmissionLimitExceeded: () => ScheduledInputRetryable.make({ reason: "capacity" }),
|
|
61
|
+
ThreadClientError: (error: ThreadClientError) =>
|
|
62
|
+
ScheduledInputRetryable.make({
|
|
63
|
+
reason: error.overloaded === true ? "capacity" : "transport",
|
|
64
|
+
}),
|
|
65
|
+
HostProtocolError: () => ScheduledInputRetryable.make({ reason: "ambiguous" }),
|
|
66
|
+
AdmissionPolicyError: (error) =>
|
|
67
|
+
error.reason === "refused"
|
|
68
|
+
? ScheduledInputRefused.make({ code: error.code })
|
|
69
|
+
: ScheduledInputRetryable.make({
|
|
70
|
+
reason: error.reason === "occupied" ? "capacity" : "storage",
|
|
71
|
+
}),
|
|
72
|
+
LedgerError: () => ScheduledInputRetryable.make({ reason: "storage" }),
|
|
73
|
+
ThreadStoreError: () => ScheduledInputRetryable.make({ reason: "storage" }),
|
|
74
|
+
DurableAlarmError: () => ScheduledInputRetryable.make({ reason: "storage" }),
|
|
75
|
+
AgentInputError: () =>
|
|
76
|
+
ScheduleStorageError.make({ operation: "prepared admission", reason: "corrupt" }),
|
|
77
|
+
DigestError: () =>
|
|
78
|
+
ScheduleStorageError.make({ operation: "prepared admission", reason: "corrupt" }),
|
|
79
|
+
ThreadNotMaterialized: () => ScheduledInputRetryable.make({ reason: "storage" }),
|
|
80
|
+
AppendConflict: () => ScheduledInputRetryable.make({ reason: "ambiguous" }),
|
|
81
|
+
FenceRejected: () => ScheduledInputRetryable.make({ reason: "ambiguous" }),
|
|
82
|
+
DurableRuntimeFailpointError: () =>
|
|
83
|
+
ScheduledInputRetryable.make({ reason: "ambiguous" }),
|
|
84
|
+
}),
|
|
85
|
+
),
|
|
86
|
+
});
|
|
87
|
+
}),
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
const preparedFromSchedule = (envelope: ScheduledEnvelope): PreparedInput => ({
|
|
91
|
+
schemaVersion: 1,
|
|
92
|
+
threadId: envelope.threadId,
|
|
93
|
+
deliveryPrincipal: envelope.deliveryPrincipal,
|
|
94
|
+
...(envelope.admissionGroup === undefined ? {} : { admissionGroup: envelope.admissionGroup }),
|
|
95
|
+
...(envelope.admissionFence === undefined ? {} : { admissionFence: envelope.admissionFence }),
|
|
96
|
+
agentId: envelope.agentId,
|
|
97
|
+
definitions: envelope.definitions,
|
|
98
|
+
input: envelope.input,
|
|
99
|
+
inputDigest: envelope.inputDigest,
|
|
100
|
+
admissionKey: envelope.admissionKey,
|
|
101
|
+
authorization: envelope.authorization,
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
/** Compatibility adapter retaining Scheduling's public admission port. */
|
|
105
|
+
export const cloudflareScheduledInputAdmissionLayer: Layer.Layer<
|
|
106
|
+
ScheduledInputAdmission,
|
|
107
|
+
never,
|
|
108
|
+
PreparedInputAdmission
|
|
109
|
+
> = Layer.effect(
|
|
110
|
+
ScheduledInputAdmission,
|
|
111
|
+
Effect.map(PreparedInputAdmission, (admission) =>
|
|
112
|
+
ScheduledInputAdmission.of({
|
|
113
|
+
submit: (envelope) => admission.submit(preparedFromSchedule(envelope)),
|
|
114
|
+
}),
|
|
115
|
+
),
|
|
116
|
+
);
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { Context, Deferred, Effect, Layer, Ref, type Scope } from "effect";
|
|
2
|
+
|
|
3
|
+
/** Cancellation tombstones are bounded hints, never durable authority. */
|
|
4
|
+
const MAX_CANCELLATION_TOMBSTONES = 1_024;
|
|
5
|
+
|
|
6
|
+
type ActiveRegistration = ReadonlySet<Deferred.Deferred<void>>;
|
|
7
|
+
type Registration = ActiveRegistration | "cancelled";
|
|
8
|
+
type Registrations = ReadonlyMap<string, Registration>;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Per-incarnation cancellation registry for long-lived progress RPCs. The public runtime owns
|
|
12
|
+
* the actual wake registration; this host-only registry lets an interrupted Worker Effect ask
|
|
13
|
+
* the Object to interrupt its scoped wait before the Worker execution context itself ends.
|
|
14
|
+
*/
|
|
15
|
+
export class ProgressWaitRegistry extends Context.Service<
|
|
16
|
+
ProgressWaitRegistry,
|
|
17
|
+
{
|
|
18
|
+
/** Register a Scope-owned cancellation signal, observing any early cancel tombstone. */
|
|
19
|
+
readonly subscribe: (
|
|
20
|
+
waiterId: string,
|
|
21
|
+
) => Effect.Effect<Effect.Effect<void>, never, Scope.Scope>;
|
|
22
|
+
/** Cancel every attempt and retain a bounded tombstone for later transport attempts. */
|
|
23
|
+
readonly cancel: (waiterId: string) => Effect.Effect<void>;
|
|
24
|
+
}
|
|
25
|
+
>()("@effect-agent/platform-cloudflare/ProgressWaitRegistry") {
|
|
26
|
+
static readonly layer: Layer.Layer<ProgressWaitRegistry> = Layer.effect(
|
|
27
|
+
ProgressWaitRegistry,
|
|
28
|
+
Effect.gen(function* () {
|
|
29
|
+
const registrations = yield* Ref.make<Registrations>(new Map());
|
|
30
|
+
|
|
31
|
+
const remove = (waiterId: string, deferred: Deferred.Deferred<void>) =>
|
|
32
|
+
Ref.update(registrations, (current) => {
|
|
33
|
+
const existing = current.get(waiterId);
|
|
34
|
+
|
|
35
|
+
if (existing === undefined || existing === "cancelled" || !existing.has(deferred)) {
|
|
36
|
+
return current;
|
|
37
|
+
}
|
|
38
|
+
const next = new Map(current);
|
|
39
|
+
const active = new Set(existing);
|
|
40
|
+
|
|
41
|
+
active.delete(deferred);
|
|
42
|
+
if (active.size === 0) {
|
|
43
|
+
next.delete(waiterId);
|
|
44
|
+
} else {
|
|
45
|
+
next.set(waiterId, active);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return next;
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const subscribe = Effect.fn("ProgressWaitRegistry.subscribe")(
|
|
52
|
+
(waiterId: string): Effect.Effect<Effect.Effect<void>, never, Scope.Scope> =>
|
|
53
|
+
Effect.gen(function* () {
|
|
54
|
+
const deferred = yield* Deferred.make<void>();
|
|
55
|
+
|
|
56
|
+
yield* Effect.addFinalizer(() => remove(waiterId, deferred));
|
|
57
|
+
|
|
58
|
+
const cancelled = yield* Ref.modify(registrations, (current) => {
|
|
59
|
+
const existing = current.get(waiterId);
|
|
60
|
+
const next = new Map(current);
|
|
61
|
+
|
|
62
|
+
if (existing === "cancelled") {
|
|
63
|
+
return [true, current] as const;
|
|
64
|
+
}
|
|
65
|
+
const active = new Set(existing ?? []);
|
|
66
|
+
|
|
67
|
+
active.add(deferred);
|
|
68
|
+
next.set(waiterId, active);
|
|
69
|
+
|
|
70
|
+
return [false, next] as const;
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
return { cancelled, deferred };
|
|
74
|
+
}).pipe(
|
|
75
|
+
Effect.map(({ cancelled, deferred }) =>
|
|
76
|
+
cancelled ? Effect.void : Deferred.await(deferred),
|
|
77
|
+
),
|
|
78
|
+
),
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
// Updating the registry and completing captured signals form one nonblocking operation;
|
|
82
|
+
// interruption between them must not strand attempts removed from the active registry.
|
|
83
|
+
const cancel = Effect.fn("ProgressWaitRegistry.cancel")(function* (waiterId: string) {
|
|
84
|
+
const waiters = yield* Ref.modify(
|
|
85
|
+
registrations,
|
|
86
|
+
(current): readonly [ReadonlyArray<Deferred.Deferred<void>>, Registrations] => {
|
|
87
|
+
const existing = current.get(waiterId);
|
|
88
|
+
|
|
89
|
+
if (existing === "cancelled") return [[], current] as const;
|
|
90
|
+
const next = new Map(current);
|
|
91
|
+
|
|
92
|
+
// A retry may arrive after an active attempt was cancelled. Retain the same
|
|
93
|
+
// tombstone used for early cancellation, ordered by cancellation time.
|
|
94
|
+
next.delete(waiterId);
|
|
95
|
+
next.set(waiterId, "cancelled");
|
|
96
|
+
let tombstones = 0;
|
|
97
|
+
|
|
98
|
+
for (const registration of next.values()) {
|
|
99
|
+
if (registration === "cancelled") tombstones += 1;
|
|
100
|
+
}
|
|
101
|
+
if (tombstones > MAX_CANCELLATION_TOMBSTONES) {
|
|
102
|
+
for (const [id, registration] of next) {
|
|
103
|
+
if (registration !== "cancelled") continue;
|
|
104
|
+
next.delete(id);
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return [existing === undefined ? [] : [...existing], next] as const;
|
|
110
|
+
},
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
yield* Effect.forEach(waiters, (waiter) => Deferred.succeed(waiter, undefined), {
|
|
114
|
+
discard: true,
|
|
115
|
+
});
|
|
116
|
+
}, Effect.uninterruptible);
|
|
117
|
+
|
|
118
|
+
return ProgressWaitRegistry.of({ subscribe, cancel });
|
|
119
|
+
}),
|
|
120
|
+
);
|
|
121
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ThreadPortTransport,
|
|
3
|
+
portTransportFailure,
|
|
4
|
+
} from "@effect-agent/storage-cloudflare/port-routing";
|
|
5
|
+
import { Effect, Layer } from "effect";
|
|
6
|
+
|
|
7
|
+
import { callThreadObject, ThreadObjectNamespace } from "../CloudflareBindings.ts";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* `ThreadPortTransport` over native Durable Object JS RPC (decision D-P6-3): one
|
|
11
|
+
* `portCall(envelope)` on the stub of the Object that owns the addressed Thread
|
|
12
|
+
* (`namespace.idFromName(threadId)` — the identity rule, plan §1.2). The envelopes are
|
|
13
|
+
* already Schema-encoded JSON, so the RPC boundary carries only structured-cloneable values;
|
|
14
|
+
* the protocol module stays transport-agnostic and fetch-with-JSON remains the documented
|
|
15
|
+
* fallback carrier.
|
|
16
|
+
*
|
|
17
|
+
* Every delivery problem — stub construction, RPC rejection, overload, deploy-in-progress —
|
|
18
|
+
* surfaces as `PortTransportError` (preserving the platform stub's own `retryable` signal
|
|
19
|
+
* when present) and NEVER as a fabricated answer: on `resolveAdmission` the routing layer
|
|
20
|
+
* turns exactly this error into `AdmissionIndeterminate` (SUB-031).
|
|
21
|
+
*/
|
|
22
|
+
export const threadPortTransportLayer: Layer.Layer<
|
|
23
|
+
ThreadPortTransport,
|
|
24
|
+
never,
|
|
25
|
+
ThreadObjectNamespace
|
|
26
|
+
> = Layer.effect(ThreadPortTransport)(
|
|
27
|
+
Effect.gen(function* () {
|
|
28
|
+
const namespace = yield* ThreadObjectNamespace;
|
|
29
|
+
|
|
30
|
+
return ThreadPortTransport.of({
|
|
31
|
+
call: (threadId, request) =>
|
|
32
|
+
callThreadObject(
|
|
33
|
+
threadId,
|
|
34
|
+
(target) => target.portCall(request),
|
|
35
|
+
(cause) => portTransportFailure(threadId, cause),
|
|
36
|
+
).pipe(
|
|
37
|
+
Effect.provideService(ThreadObjectNamespace, namespace),
|
|
38
|
+
Effect.withSpan("CloudflarePortTransport.call", {
|
|
39
|
+
attributes: { threadId },
|
|
40
|
+
}),
|
|
41
|
+
),
|
|
42
|
+
});
|
|
43
|
+
}),
|
|
44
|
+
);
|