@yielded/agent-platform-cloudflare 0.1.0-beta.185 → 0.1.0-beta.187

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 (128) hide show
  1. package/dist/{Alarm-C257DCAJ.mjs → Alarm-4qf-iCXC.mjs} +94 -54
  2. package/dist/Alarm-4qf-iCXC.mjs.map +1 -0
  3. package/dist/Alarm.d.mts +16 -9
  4. package/dist/Alarm.mjs +1 -1
  5. package/dist/{BrowserCredentials-C2rBhlUI.d.mts → BrowserCredentials-CysbK6UA.d.mts} +4 -2
  6. package/dist/BrowserCredentials.d.mts +1 -1
  7. package/dist/BrowserCredentials.mjs +2 -1
  8. package/dist/BrowserCredentials.mjs.map +1 -1
  9. package/dist/BrowserRestCapture.d.mts +4 -3
  10. package/dist/BrowserRestCapture.mjs +9 -3
  11. package/dist/BrowserRestCapture.mjs.map +1 -1
  12. package/dist/BrowserRestCrawl.d.mts +3 -2
  13. package/dist/BrowserRestCrawl.mjs +12 -2
  14. package/dist/BrowserRestCrawl.mjs.map +1 -1
  15. package/dist/{BrowserSession-mKnLSc3p.d.mts → BrowserSession-BLEG79zz.d.mts} +12 -5
  16. package/dist/{BrowserSession-CfivQ5JY.mjs → BrowserSession-CITvpByq.mjs} +17 -5
  17. package/dist/BrowserSession-CITvpByq.mjs.map +1 -0
  18. package/dist/BrowserSession.d.mts +2 -2
  19. package/dist/BrowserSession.mjs +2 -2
  20. package/dist/BrowserUse.d.mts +4 -2
  21. package/dist/BrowserUse.mjs +6 -2
  22. package/dist/BrowserUse.mjs.map +1 -1
  23. package/dist/CloudflareAiGateway.d.mts +3 -2
  24. package/dist/CloudflareAiGateway.mjs +12 -2
  25. package/dist/CloudflareAiGateway.mjs.map +1 -1
  26. package/dist/CloudflareBindings.d.mts +4 -1
  27. package/dist/CloudflareBindings.mjs +5 -1
  28. package/dist/CloudflareBindings.mjs.map +1 -1
  29. package/dist/{CloudflareBrowser-gu7gma0Y.d.mts → CloudflareBrowser-CZoXu_oS.d.mts} +5 -2
  30. package/dist/{CloudflareBrowser-Vt8qTdYW.mjs → CloudflareBrowser-Wfs-zRuk.mjs} +6 -2
  31. package/dist/CloudflareBrowser-Wfs-zRuk.mjs.map +1 -0
  32. package/dist/CloudflareBrowser.d.mts +1 -1
  33. package/dist/CloudflareBrowser.mjs +1 -1
  34. package/dist/CloudflareCodeMode.d.mts +2 -1
  35. package/dist/CloudflareCodeMode.mjs +10 -2
  36. package/dist/CloudflareCodeMode.mjs.map +1 -1
  37. package/dist/{CloudflareConfig-C9MvPbX8.d.mts → CloudflareConfig-D_-BMp4q.d.mts} +12 -6
  38. package/dist/CloudflareConfig.d.mts +2 -2
  39. package/dist/CloudflareConfig.mjs +12 -5
  40. package/dist/CloudflareConfig.mjs.map +1 -1
  41. package/dist/CloudflareMemory.d.mts +3 -1
  42. package/dist/CloudflareMemory.mjs +5 -1
  43. package/dist/CloudflareMemory.mjs.map +1 -1
  44. package/dist/CloudflareScheduling.d.mts +4 -1
  45. package/dist/CloudflareScheduling.mjs +9 -4
  46. package/dist/CloudflareScheduling.mjs.map +1 -1
  47. package/dist/CloudflareSubscriptions.d.mts +6 -1
  48. package/dist/CloudflareSubscriptions.mjs +10 -4
  49. package/dist/CloudflareSubscriptions.mjs.map +1 -1
  50. package/dist/{CloudflareThreadClient-CnvMXPmV.mjs → CloudflareThreadClient-DiV2xHn0.mjs} +16 -135
  51. package/dist/CloudflareThreadClient-DiV2xHn0.mjs.map +1 -0
  52. package/dist/CloudflareThreadClient.d.mts +2144 -2
  53. package/dist/CloudflareThreadClient.mjs +2 -1
  54. package/dist/{InteractiveBrowser-nukKxc87.mjs → InteractiveBrowser-Bh9wisQI.mjs} +18 -5
  55. package/dist/InteractiveBrowser-Bh9wisQI.mjs.map +1 -0
  56. package/dist/{InteractiveBrowser-wV2Bti5e.d.mts → InteractiveBrowser-DbYpKPA3.d.mts} +8 -3
  57. package/dist/InteractiveBrowser.d.mts +1 -1
  58. package/dist/InteractiveBrowser.mjs +1 -1
  59. package/dist/LiveText-D7WxSY-h.d.mts +96 -0
  60. package/dist/LiveText-DRzUabZi.mjs +151 -0
  61. package/dist/LiveText-DRzUabZi.mjs.map +1 -0
  62. package/dist/LiveText.d.mts +2 -0
  63. package/dist/LiveText.mjs +2 -0
  64. package/dist/{ThreadObject-Osn-FmcF.d.mts → ThreadObject-BcoRabnQ.d.mts} +27 -11
  65. package/dist/{ThreadObject-Cr1CFWXb.mjs → ThreadObject-EChaDQBT.mjs} +47 -18
  66. package/dist/ThreadObject-EChaDQBT.mjs.map +1 -0
  67. package/dist/ThreadObject.d.mts +1 -1
  68. package/dist/ThreadObject.mjs +1 -1
  69. package/dist/WakeScheduler.d.mts +1 -1
  70. package/dist/WakeScheduler.mjs +7 -3
  71. package/dist/WakeScheduler.mjs.map +1 -1
  72. package/dist/{boundary-BguazVkh.mjs → boundary-BLbZMBl2.mjs} +2 -2
  73. package/dist/boundary-BLbZMBl2.mjs.map +1 -0
  74. package/dist/{browser-failure-DECzRY3H.mjs → browser-failure-BrL9H7IG.mjs} +6 -2
  75. package/dist/browser-failure-BrL9H7IG.mjs.map +1 -0
  76. package/dist/index.d.mts +8 -7
  77. package/dist/index.mjs +7 -6
  78. package/dist/{prepared-admission-w2Kq32_k.mjs → prepared-admission-mGUYbSH_.mjs} +4 -3
  79. package/dist/prepared-admission-mGUYbSH_.mjs.map +1 -0
  80. package/package.json +1 -1
  81. package/src/Alarm.ts +128 -82
  82. package/src/BrowserCredentials.ts +3 -1
  83. package/src/BrowserRestCapture.ts +9 -2
  84. package/src/BrowserRestCrawl.ts +14 -18
  85. package/src/BrowserSession.ts +10 -12
  86. package/src/BrowserUse.ts +5 -1
  87. package/src/CloudflareAiGateway.ts +12 -8
  88. package/src/CloudflareBindings.ts +5 -1
  89. package/src/CloudflareCodeMode.ts +9 -1
  90. package/src/CloudflareConfig.ts +10 -3
  91. package/src/CloudflareMemory.ts +5 -1
  92. package/src/CloudflareScheduling.ts +7 -2
  93. package/src/CloudflareSubscriptions.ts +9 -2
  94. package/src/CloudflareThreadClient.ts +15 -148
  95. package/src/InteractiveBrowser.ts +10 -12
  96. package/src/LiveText.ts +141 -0
  97. package/src/ThreadObject.ts +13 -8
  98. package/src/WakeScheduler.ts +5 -1
  99. package/src/index.ts +1 -0
  100. package/src/internal/boundary.ts +1 -1
  101. package/src/internal/browser-binding.ts +5 -1
  102. package/src/internal/browser-credentials.ts +5 -1
  103. package/src/internal/browser-failure.ts +6 -2
  104. package/src/internal/browser-file-selection.ts +1 -1
  105. package/src/internal/browser-quick-action.ts +5 -1
  106. package/src/internal/browser-readonly-live-view.ts +9 -2
  107. package/src/internal/browser-session-lifecycle.ts +10 -2
  108. package/src/internal/crypto.ts +5 -2
  109. package/src/internal/due-queue.ts +2 -1
  110. package/src/internal/host-protocol-error.ts +23 -0
  111. package/src/internal/layers.ts +45 -22
  112. package/src/internal/live-text-observation.ts +8 -0
  113. package/src/internal/live-text-protocol.ts +10 -9
  114. package/src/internal/live-text.ts +28 -10
  115. package/src/internal/message-delivery.ts +7 -1
  116. package/src/internal/prepared-admission.ts +2 -1
  117. package/src/internal/progress-wait.ts +6 -1
  118. package/src/internal/transport.ts +2 -1
  119. package/dist/Alarm-C257DCAJ.mjs.map +0 -1
  120. package/dist/BrowserSession-CfivQ5JY.mjs.map +0 -1
  121. package/dist/CloudflareBrowser-Vt8qTdYW.mjs.map +0 -1
  122. package/dist/CloudflareThreadClient-CmlojpjC.d.mts +0 -2211
  123. package/dist/CloudflareThreadClient-CnvMXPmV.mjs.map +0 -1
  124. package/dist/InteractiveBrowser-nukKxc87.mjs.map +0 -1
  125. package/dist/ThreadObject-Cr1CFWXb.mjs.map +0 -1
  126. package/dist/boundary-BguazVkh.mjs.map +0 -1
  127. package/dist/browser-failure-DECzRY3H.mjs.map +0 -1
  128. package/dist/prepared-admission-w2Kq32_k.mjs.map +0 -1
@@ -1,4 +1,4 @@
1
- import { SqliteClient } from "@effect/sql-sqlite-do";
1
+ import * as SqliteClient from "@effect/sql-sqlite-do/SqliteClient";
2
2
  import {
3
3
  DoScheduleAlarmControl,
4
4
  DoScheduleTransaction,
@@ -35,7 +35,6 @@ import {
35
35
  ScheduleWakeNoop,
36
36
  } from "@yielded/agent/scheduling";
37
37
  import { AdmissionFence } from "@yielded/agent/submission-ledger";
38
- import { Clock, Context, DateTime, Effect, Layer, Schema } from "effect";
39
38
  import {
40
39
  DurableObject as EffectCfDurableObject,
41
40
  DurableObjectAlarm,
@@ -43,6 +42,12 @@ import {
43
42
  DurableObjectState as EffectCfDurableObjectState,
44
43
  type WorkerEnvironment,
45
44
  } from "effect-cf";
45
+ import * as Clock from "effect/Clock";
46
+ import * as Context from "effect/Context";
47
+ import * as DateTime from "effect/DateTime";
48
+ import * as Effect from "effect/Effect";
49
+ import * as Layer from "effect/Layer";
50
+ import * as Schema from "effect/Schema";
46
51
 
47
52
  import type { ThreadObjectNamespace } from "./CloudflareBindings.ts";
48
53
  import { CloudflareThreadClient } from "./CloudflareThreadClient.ts";
@@ -1,4 +1,4 @@
1
- import { SqliteClient } from "@effect/sql-sqlite-do";
1
+ import * as SqliteClient from "@effect/sql-sqlite-do/SqliteClient";
2
2
  import {
3
3
  DoSubscriptionAlarmControl,
4
4
  DoSubscriptionTransaction,
@@ -30,13 +30,20 @@ import {
30
30
  SubscriptionIntake,
31
31
  Subscriptions,
32
32
  } from "@yielded/agent/subscriptions";
33
- import { Cause, Clock, Context, DateTime, Effect, Layer, Schema, type Scope } from "effect";
34
33
  import {
35
34
  DurableObject as EffectCfDurableObject,
36
35
  DurableObjectAlarm,
37
36
  DurableObjectState as EffectCfDurableObjectState,
38
37
  type WorkerEnvironment,
39
38
  } from "effect-cf";
39
+ import * as Cause from "effect/Cause";
40
+ import * as Clock from "effect/Clock";
41
+ import * as Context from "effect/Context";
42
+ import * as DateTime from "effect/DateTime";
43
+ import * as Effect from "effect/Effect";
44
+ import * as Layer from "effect/Layer";
45
+ import * as Schema from "effect/Schema";
46
+ import type * as Scope from "effect/Scope";
40
47
 
41
48
  import { type ThreadObjectNamespace } from "./CloudflareBindings.ts";
42
49
  import { CloudflareThreadClient } from "./CloudflareThreadClient.ts";
@@ -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,
@@ -56,16 +63,16 @@ import {
56
63
  import { AdmissionLimitExceeded } from "./CloudflareConfig.ts";
57
64
  import { cloudflareFailureSignals, safeCauseMessage } from "./internal/boundary.ts";
58
65
  import { cloudflareCryptoLayer } from "./internal/crypto.ts";
66
+ import { boundHostDiagnostic, HostProtocolError } from "./internal/host-protocol-error.ts";
67
+ import { Observation as LiveTextObservation } from "./internal/live-text-observation.ts";
59
68
  import {
60
- Frame as LiveTextFrame,
61
- isBoundedEvent,
62
- MAX_DRAFT_PARTS,
63
- MAX_FRAME_BYTES,
64
- Observation as LiveTextObservation,
69
+ liveTextProtocolFailure,
65
70
  Request as WatchTextRequest,
66
71
  } from "./internal/live-text-protocol.ts";
72
+ import { decodeLiveText, type LiveTextFrame } from "./LiveText.ts";
67
73
 
68
- export { Frame as LiveTextFrame } from "./internal/live-text-protocol.ts";
74
+ export { boundHostDiagnostic, HostProtocolError } from "./internal/host-protocol-error.ts";
75
+ export { decodeLiveText, LiveTextFrame } from "./LiveText.ts";
69
76
 
70
77
  /**
71
78
  * The Worker↔Thread-Object host protocol (plan §1.4): Schema envelopes for the host
@@ -76,29 +83,8 @@ export { Frame as LiveTextFrame } from "./internal/live-text-protocol.ts";
76
83
  * (error-tag fidelity), and protocol anomalies are answered typed instead of thrown.
77
84
  */
78
85
 
79
- /** Ceiling for host protocol diagnostic strings. */
80
- const MAX_HOST_DIAGNOSTIC_LENGTH = 4_096;
81
86
  const PROGRESS_CANCELLATION_TIMEOUT = "1 second";
82
87
 
83
- const BoundedDiagnostic = Schema.String.check(Schema.isMaxLength(MAX_HOST_DIAGNOSTIC_LENGTH));
84
-
85
- /** Truncate a diagnostic string to the host protocol's bounded length. */
86
- export const boundHostDiagnostic = (value: string): string =>
87
- value.length > MAX_HOST_DIAGNOSTIC_LENGTH
88
- ? `${value.slice(0, MAX_HOST_DIAGNOSTIC_LENGTH - 3)}...`
89
- : value;
90
-
91
- /**
92
- * The envelope itself could not be honored: the Object could not decode the request, or a
93
- * response could not be encoded/decoded. Never carries operation semantics.
94
- */
95
- export class HostProtocolError extends Schema.TaggedError<HostProtocolError>()(
96
- "HostProtocolError",
97
- {
98
- message: BoundedDiagnostic,
99
- },
100
- ) {}
101
-
102
88
  /** The Worker-side stub call itself failed (RPC rejection, overload, eviction mid-call). */
103
89
  export class ThreadClientError extends Schema.TaggedError<ThreadClientError>()(
104
90
  "ThreadClientError",
@@ -384,126 +370,6 @@ export type ClientAbortFailure = typeof ClientAbortHostFailure.Type | ThreadClie
384
370
  export type ClientApprovalFailure = typeof ClientApprovalHostFailure.Type | ThreadClientError;
385
371
  export type ClientUnknownFailure = typeof ClientUnknownHostFailure.Type | ThreadClientError;
386
372
 
387
- const liveTextProtocolFailure = () =>
388
- HostProtocolError.make({
389
- message: "Provisional text is incomplete or invalid; discard drafts and read canonical records",
390
- });
391
-
392
- const decodeLiveTextFrame = Schema.decodeUnknownSync(Schema.fromJsonString(LiveTextFrame));
393
-
394
- /**
395
- * Validate untrusted NDJSON at the final consumer, including frame bounds, thread,
396
- * sequence and text lifecycle. Stream loss invalidates all provisional drafts.
397
- */
398
- export const decodeLiveText = <E, R>(
399
- threadId: ThreadId,
400
- source: Stream.Stream<Uint8Array, E, R>,
401
- ): Stream.Stream<LiveTextFrame, E | HostProtocolError, R> =>
402
- Stream.suspend(() => {
403
- const decoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: false });
404
- let pending = "";
405
- let pendingBytes = 0;
406
- let expected = 0;
407
- let streamId: string | undefined;
408
- const models = new Map<string, { attemptId: string; parts: Set<string> }>();
409
-
410
- const accept = Effect.fnUntraced(function* (frame: LiveTextFrame) {
411
- if (frame._tag === "Reset") {
412
- if (frame.streamId === streamId || frame.threadId !== threadId)
413
- return yield* liveTextProtocolFailure();
414
- streamId = frame.streamId;
415
- expected = 1;
416
- models.clear();
417
-
418
- return true;
419
- }
420
- if (
421
- streamId === undefined ||
422
- frame.streamId !== streamId ||
423
- frame.sequence !== expected++ ||
424
- frame.event.threadId !== threadId ||
425
- !isBoundedEvent(frame.event)
426
- )
427
- return yield* liveTextProtocolFailure();
428
- const event = frame.event;
429
-
430
- if (event._tag === "AttemptEnded") {
431
- for (const [key, model] of models)
432
- if (model.attemptId === event.attemptId) models.delete(key);
433
-
434
- return true;
435
- }
436
- const key = JSON.stringify([event.attemptId, event.runId, event.turnId, event.generation]);
437
-
438
- if (event._tag === "Discard") {
439
- models.delete(key);
440
-
441
- return true;
442
- }
443
- let model = models.get(key);
444
-
445
- if (event.part.type === "text-start") {
446
- if (model?.parts.has(event.part.id)) return yield* liveTextProtocolFailure();
447
- if (
448
- [...models.values()].reduce((size, value) => size + value.parts.size, 0) >=
449
- MAX_DRAFT_PARTS
450
- )
451
- return yield* liveTextProtocolFailure();
452
- if (model === undefined) {
453
- model = { attemptId: event.attemptId, parts: new Set() };
454
- models.set(key, model);
455
- }
456
- model.parts.add(event.part.id);
457
-
458
- return true;
459
- }
460
- if (!model?.parts.has(event.part.id)) return yield* liveTextProtocolFailure();
461
- if (event.part.type === "text-end") {
462
- model.parts.delete(event.part.id);
463
- if (model.parts.size === 0) models.delete(key);
464
- }
465
-
466
- return true;
467
- });
468
-
469
- return source.pipe(
470
- Stream.mapEffect(
471
- Effect.fnUntraced(function* (chunk) {
472
- if (!(chunk instanceof Uint8Array)) return yield* liveTextProtocolFailure();
473
- const frames: LiveTextFrame[] = [];
474
-
475
- for (let offset = 0; offset < chunk.byteLength;) {
476
- const newline = chunk.indexOf(10, offset);
477
- const end = newline < 0 ? chunk.byteLength : newline;
478
-
479
- pendingBytes += end - offset;
480
- if (pendingBytes > MAX_FRAME_BYTES) return yield* liveTextProtocolFailure();
481
- pending += yield* Effect.try({
482
- try: () => decoder.decode(chunk.subarray(offset, end), { stream: newline < 0 }),
483
- catch: liveTextProtocolFailure,
484
- });
485
- offset = end + 1;
486
- if (newline < 0) break;
487
-
488
- const frame = yield* Effect.try({
489
- try: () => decodeLiveTextFrame(pending),
490
- catch: liveTextProtocolFailure,
491
- });
492
-
493
- pending = "";
494
- pendingBytes = 0;
495
- if (yield* accept(frame)) frames.push(frame);
496
- }
497
-
498
- return frames;
499
- }),
500
- ),
501
- Stream.flatMap(Stream.fromArray),
502
- // This connection is open-ended. Clean EOF is also loss of provisional state.
503
- Stream.concat(Stream.fail(liveTextProtocolFailure())),
504
- );
505
- });
506
-
507
373
  const outOfContract = (threadId: string, operation: string, observed: string): ThreadClientError =>
508
374
  ThreadClientError.make({
509
375
  threadId,
@@ -572,6 +438,7 @@ export class CloudflareThreadClient extends Context.Service<
572
438
  readonly watchText: (threadId: ThreadId) => Stream.Stream<LiveTextFrame, ClientObserveFailure>;
573
439
  /**
574
440
  * Forward producer NDJSON bytes without decoding or re-encoding frames.
441
+ * Blank keepalive lines every 15 seconds expose idle transport disconnects.
575
442
  * The final consumer must use `decodeLiveText` to validate untrusted frames.
576
443
  * Interruption cancels the remote subscription; buffers remain producer-bounded.
577
444
  */
@@ -28,18 +28,16 @@ import {
28
28
  } from "@yielded/agent/interactive-browser";
29
29
  import { PageScreenshotResult } from "@yielded/agent/page-screenshot";
30
30
  import { SandboxImplementation } from "@yielded/agent/sandbox";
31
- import {
32
- Cause,
33
- Context,
34
- Clock,
35
- Duration,
36
- Effect,
37
- Layer,
38
- Redacted,
39
- Schema,
40
- Semaphore,
41
- Scope,
42
- } from "effect";
31
+ import * as Cause from "effect/Cause";
32
+ import * as Clock from "effect/Clock";
33
+ import * as Context from "effect/Context";
34
+ import * as Duration from "effect/Duration";
35
+ import * as Effect from "effect/Effect";
36
+ import * as Layer from "effect/Layer";
37
+ import * as Redacted from "effect/Redacted";
38
+ import * as Schema from "effect/Schema";
39
+ import * as Scope from "effect/Scope";
40
+ import * as Semaphore from "effect/Semaphore";
43
41
  import {
44
42
  type Browser,
45
43
  type BrowserContext,
@@ -0,0 +1,141 @@
1
+ import { type ThreadId } from "@yielded/agent/identifiers";
2
+ import * as Effect from "effect/Effect";
3
+ import * as Schema from "effect/Schema";
4
+ import * as Stream from "effect/Stream";
5
+
6
+ import type { HostProtocolError } from "./internal/host-protocol-error.ts";
7
+ import {
8
+ Frame as LiveTextFrame,
9
+ isBoundedEvent,
10
+ liveTextProtocolFailure,
11
+ MAX_DRAFT_PARTS,
12
+ MAX_FRAME_BYTES,
13
+ } from "./internal/live-text-protocol.ts";
14
+
15
+ export { HostProtocolError } from "./internal/host-protocol-error.ts";
16
+ export { Frame as LiveTextFrame } from "./internal/live-text-protocol.ts";
17
+
18
+ const decodeLiveTextFrame = Schema.decodeUnknownSync(Schema.fromJsonString(LiveTextFrame));
19
+
20
+ /**
21
+ * Validate untrusted NDJSON at the final consumer, including frame bounds, thread,
22
+ * sequence and text lifecycle. Bounded blank keepalive lines are ignored.
23
+ * Stream loss invalidates all provisional drafts.
24
+ */
25
+ export const decodeLiveText = <E, R>(
26
+ threadId: ThreadId,
27
+ source: Stream.Stream<Uint8Array, E, R>,
28
+ ): Stream.Stream<LiveTextFrame, E | HostProtocolError, R> =>
29
+ Stream.suspend(() => {
30
+ const decoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: false });
31
+ let pending = "";
32
+ let pendingBytes = 0;
33
+ let expected = 0;
34
+ let streamId: string | undefined;
35
+ const models = new Map<string, { attemptId: string; parts: Set<string> }>();
36
+
37
+ const accept = Effect.fnUntraced(function* (frame: LiveTextFrame) {
38
+ if (frame._tag === "Reset") {
39
+ if (frame.streamId === streamId || frame.threadId !== threadId)
40
+ return yield* liveTextProtocolFailure();
41
+ streamId = frame.streamId;
42
+ expected = 1;
43
+ models.clear();
44
+
45
+ return true;
46
+ }
47
+ if (
48
+ streamId === undefined ||
49
+ frame.streamId !== streamId ||
50
+ frame.sequence !== expected++ ||
51
+ frame.event.threadId !== threadId ||
52
+ !isBoundedEvent(frame.event)
53
+ )
54
+ return yield* liveTextProtocolFailure();
55
+ const event = frame.event;
56
+
57
+ if (event._tag === "AttemptEnded") {
58
+ for (const [key, model] of models)
59
+ if (model.attemptId === event.attemptId) models.delete(key);
60
+
61
+ return true;
62
+ }
63
+ const key = JSON.stringify([event.attemptId, event.runId, event.turnId, event.generation]);
64
+
65
+ if (event._tag === "Discard") {
66
+ models.delete(key);
67
+
68
+ return true;
69
+ }
70
+ let model = models.get(key);
71
+
72
+ const partId = JSON.stringify([
73
+ event.part.type.startsWith("reasoning-") ? "reasoning" : "text",
74
+ event.part.id,
75
+ ]);
76
+
77
+ if (event.part.type === "text-start" || event.part.type === "reasoning-start") {
78
+ if (model?.parts.has(partId)) return yield* liveTextProtocolFailure();
79
+ if (
80
+ [...models.values()].reduce((size, value) => size + value.parts.size, 0) >=
81
+ MAX_DRAFT_PARTS
82
+ )
83
+ return yield* liveTextProtocolFailure();
84
+ if (model === undefined) {
85
+ model = { attemptId: event.attemptId, parts: new Set() };
86
+ models.set(key, model);
87
+ }
88
+ model.parts.add(partId);
89
+
90
+ return true;
91
+ }
92
+ if (!model?.parts.has(partId)) return yield* liveTextProtocolFailure();
93
+ if (event.part.type === "text-end" || event.part.type === "reasoning-end") {
94
+ model.parts.delete(partId);
95
+ if (model.parts.size === 0) models.delete(key);
96
+ }
97
+
98
+ return true;
99
+ });
100
+
101
+ return source.pipe(
102
+ Stream.mapEffect(
103
+ Effect.fnUntraced(function* (chunk) {
104
+ if (!(chunk instanceof Uint8Array)) return yield* liveTextProtocolFailure();
105
+ const frames: LiveTextFrame[] = [];
106
+
107
+ for (let offset = 0; offset < chunk.byteLength;) {
108
+ const newline = chunk.indexOf(10, offset);
109
+ const end = newline < 0 ? chunk.byteLength : newline;
110
+
111
+ pendingBytes += end - offset;
112
+ if (pendingBytes > MAX_FRAME_BYTES) return yield* liveTextProtocolFailure();
113
+ pending += yield* Effect.try({
114
+ try: () => decoder.decode(chunk.subarray(offset, end), { stream: newline < 0 }),
115
+ catch: liveTextProtocolFailure,
116
+ });
117
+ offset = end + 1;
118
+ if (newline < 0) break;
119
+
120
+ const line = pending;
121
+
122
+ pending = "";
123
+ pendingBytes = 0;
124
+ if (line.trim().length === 0) continue;
125
+
126
+ const frame = yield* Effect.try({
127
+ try: () => decodeLiveTextFrame(line),
128
+ catch: liveTextProtocolFailure,
129
+ });
130
+
131
+ if (yield* accept(frame)) frames.push(frame);
132
+ }
133
+
134
+ return frames;
135
+ }),
136
+ ),
137
+ Stream.flatMap(Stream.fromArray),
138
+ // This connection is open-ended. Clean EOF is also loss of provisional state.
139
+ Stream.concat(Stream.fail(liveTextProtocolFailure())),
140
+ );
141
+ });
@@ -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 alarms = yield* DurableAlarmService;
532
+ const maintenance = yield* ThreadMaintenance;
527
533
 
528
- // Make accepted work eligible for post-response dispatch without changing queue retry floors.
529
- yield* alarms.scheduleNow.pipe(
530
- Effect.catch((error) =>
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)));
@@ -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 { Effect, Layer, PubSub, Schema, Stream } from "effect";
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/index.ts CHANGED
@@ -7,6 +7,7 @@ export * as CloudflareMemory from "./CloudflareMemory.ts";
7
7
  export * as CloudflareScheduling from "./CloudflareScheduling.ts";
8
8
  export * as CloudflareSubscriptions from "./CloudflareSubscriptions.ts";
9
9
  export * as CloudflareThreadClient from "./CloudflareThreadClient.ts";
10
+ export * as LiveText from "./LiveText.ts";
10
11
  export * as ThreadObject from "./ThreadObject.ts";
11
12
  export * as WakeScheduler from "./WakeScheduler.ts";
12
13
  export * as CloudflareAiGateway from "./CloudflareAiGateway.ts";
@@ -1,4 +1,4 @@
1
- import { Predicate } from "effect";
1
+ import * as Predicate from "effect/Predicate";
2
2
 
3
3
  const MAX_FOREIGN_DIAGNOSTIC_LENGTH = 8_192;
4
4
 
@@ -1,5 +1,9 @@
1
1
  /// <reference types="@cloudflare/workers-types" />
2
- import { Cause, Context, Effect, Layer, Schema } from "effect";
2
+ import * as Cause from "effect/Cause";
3
+ import * as Context from "effect/Context";
4
+ import * as Effect from "effect/Effect";
5
+ import * as Layer from "effect/Layer";
6
+ import * as Schema from "effect/Schema";
3
7
  import puppeteer, {
4
8
  type Browser,
5
9
  type ConnectionTransport,
@@ -1,4 +1,8 @@
1
- import { Cause, Context, Effect, Redacted, Schema } from "effect";
1
+ import * as Cause from "effect/Cause";
2
+ import * as Context from "effect/Context";
3
+ import * as Effect from "effect/Effect";
4
+ import * as Redacted from "effect/Redacted";
5
+ import * as Schema from "effect/Schema";
2
6
  import type { Frame, Page } from "puppeteer-core/lib/esm/puppeteer/puppeteer-core-browser.js";
3
7
 
4
8
  import {
@@ -1,5 +1,9 @@
1
- import type { Effect } from "effect";
2
- import { Cause, Data, ErrorReporter, Predicate, Schema } from "effect";
1
+ import * as Cause from "effect/Cause";
2
+ import * as Data from "effect/Data";
3
+ import type * as Effect from "effect/Effect";
4
+ import * as ErrorReporter from "effect/ErrorReporter";
5
+ import * as Predicate from "effect/Predicate";
6
+ import * as Schema from "effect/Schema";
3
7
 
4
8
  /** Source-authored stages and classifications only; never retain SDK text or session capabilities. */
5
9
  export class BrowserRunFailure extends Data.TaggedError("BrowserRunFailure")<{
@@ -1,5 +1,5 @@
1
1
  import type { BrowserSelectFileRequest } from "@yielded/agent/interactive-browser";
2
- import { Base64 } from "effect/encoding";
2
+ import * as Base64 from "effect/encoding/Base64";
3
3
  import type { Page } from "puppeteer-core/lib/esm/puppeteer/puppeteer-core-browser.js";
4
4
 
5
5
  import { pageFunction } from "./page-function.ts";
@@ -31,7 +31,11 @@ import {
31
31
  type PageScreenshotRequest,
32
32
  } from "@yielded/agent/page-screenshot";
33
33
  import { SandboxImplementation } from "@yielded/agent/sandbox";
34
- import { Context, Effect, Layer, Option, Schema } from "effect";
34
+ import * as Context from "effect/Context";
35
+ import * as Effect from "effect/Effect";
36
+ import * as Layer from "effect/Layer";
37
+ import * as Option from "effect/Option";
38
+ import * as Schema from "effect/Schema";
35
39
 
36
40
  /**
37
41
  * The Cloudflare Browser Run Quick Action `PageCapture` adapter (capability
@@ -1,5 +1,12 @@
1
- import { Context, Effect, Layer, Redacted, Schema, Stream } from "effect";
2
- import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/http";
1
+ import * as Context from "effect/Context";
2
+ import * as Effect from "effect/Effect";
3
+ import * as FetchHttpClient from "effect/http/FetchHttpClient";
4
+ import * as HttpClient from "effect/http/HttpClient";
5
+ import * as HttpClientRequest from "effect/http/HttpClientRequest";
6
+ import * as Layer from "effect/Layer";
7
+ import * as Redacted from "effect/Redacted";
8
+ import * as Schema from "effect/Schema";
9
+ import * as Stream from "effect/Stream";
3
10
 
4
11
  import { BrowserRunLiveViewRequest, BrowserRunLiveViewResult } from "../InteractiveBrowser.ts";
5
12
  import { browserFailure, BrowserRunFailure } from "./browser-failure.ts";
@@ -1,5 +1,13 @@
1
- import { Context, Effect, Layer, Option, Redacted, Schema, Stream } from "effect";
2
- import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/http";
1
+ import * as Context from "effect/Context";
2
+ import * as Effect from "effect/Effect";
3
+ import * as FetchHttpClient from "effect/http/FetchHttpClient";
4
+ import * as HttpClient from "effect/http/HttpClient";
5
+ import * as HttpClientRequest from "effect/http/HttpClientRequest";
6
+ import * as Layer from "effect/Layer";
7
+ import * as Option from "effect/Option";
8
+ import * as Redacted from "effect/Redacted";
9
+ import * as Schema from "effect/Schema";
10
+ import * as Stream from "effect/Stream";
3
11
 
4
12
  import { browserFailure } from "./browser-failure.ts";
5
13
 
@@ -2,8 +2,11 @@
2
2
  // @effect-diagnostics-next-line nodeBuiltinImport:off
3
3
  import { createHash } from "node:crypto";
4
4
 
5
- import { BrowserCrypto } from "@effect/platform-browser";
6
- import { Crypto, Effect, Layer, PlatformError } from "effect";
5
+ import * as BrowserCrypto from "@effect/platform-browser/BrowserCrypto";
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(
@@ -1,4 +1,5 @@
1
- import { Effect, Schema } from "effect";
1
+ import * as Effect from "effect/Effect";
2
+ import * as Schema from "effect/Schema";
2
3
 
3
4
  export const LaneId = Schema.NonEmptyString.check(Schema.isMaxLength(256));
4
5
 
@@ -0,0 +1,23 @@
1
+ import * as Schema from "effect/Schema";
2
+
3
+ /** Ceiling for host protocol diagnostic strings. */
4
+ const MAX_HOST_DIAGNOSTIC_LENGTH = 4_096;
5
+
6
+ const BoundedDiagnostic = Schema.String.check(Schema.isMaxLength(MAX_HOST_DIAGNOSTIC_LENGTH));
7
+
8
+ /** Truncate a diagnostic string to the host protocol's bounded length. */
9
+ export const boundHostDiagnostic = (value: string): string =>
10
+ value.length > MAX_HOST_DIAGNOSTIC_LENGTH
11
+ ? `${value.slice(0, MAX_HOST_DIAGNOSTIC_LENGTH - 3)}...`
12
+ : value;
13
+
14
+ /**
15
+ * The envelope itself could not be honored: the Object could not decode the request, or a
16
+ * response could not be encoded/decoded. Never carries operation semantics.
17
+ */
18
+ export class HostProtocolError extends Schema.TaggedError<HostProtocolError>()(
19
+ "HostProtocolError",
20
+ {
21
+ message: BoundedDiagnostic,
22
+ },
23
+ ) {}