@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 209 lines 8.8 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as kv from "@distilled.cloud/cloudflare/kv";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9 import * as Stream from "effect/Stream";
10 import * as pathe from "pathe";
11 import { expectUrlContains } from "../Utils/Http.ts";
12 import { waitForWorkerToBeDeleted } from "../Utils/Worker.ts";
13
14 const { test } = Test.make({ providers: Cloudflare.providers() });
15
16 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
17
18 /**
19 * Producer: logs on every request so each invocation produces a streaming
20 * tail session (onset → log → outcome) for its streaming tail consumers.
21 */
22 const producerScript = `export default {
23 fetch() {
24 console.log("alchemy-streaming-tail-marker");
25 return new Response("streaming-producer-ok");
26 },
27 };`;
28
29 /** The recorded shape of one streamed `TailEvent` in a consumer session. */
30 interface RecordedTailEvent {
31 invocationId?: string;
32 event?: { type?: string; outcome?: string; message?: unknown };
33 }
34
35 /**
36 * Cloud-side `tailStream()` delivery is not yet available: Cloudflare's
37 * production API refuses the `streaming_tail_worker` compatibility flag —
38 * the flag that enables workerd's streaming tail model — with the typed
39 * `ScriptStartupError` (code 10021) "The compatibility flag
40 * streaming_tail_worker is experimental and cannot yet be used in Workers
41 * deployed to Cloudflare." A deployed consumer therefore cannot opt into
42 * the `tailStream()` handler, so no events are ever delivered to it. The
43 * ungated probe test below pins that exact rejection.
44 *
45 * Probed 2026-08-04 on the testing account: the `streaming_tail_consumers`
46 * metadata PUT succeeds and the producer serves, but producer invocations
47 * delivered ZERO streaming sessions across 5 minutes of polling (60 × 5s,
48 * each poll re-invoking the producer) — with and without
49 * `observability.traces.enabled` on the producer — while a plain `tail()`
50 * consumer on the same account delivers within seconds
51 * (TailConsumers.test.ts, green). Local workerd delivery is real and
52 * covered by StreamingTailConsumers.local.test.ts. Set this env var once
53 * Cloudflare ships production delivery (the probe test will fail then) to
54 * assert delivery end-to-end.
55 */
56 const STREAMING_TAIL_DELIVERY = !!process.env.CLOUDFLARE_TEST_STREAMING_TAIL_DELIVERY;
57
58 /**
59 * Ungated platform probe: production refuses the experimental
60 * `streaming_tail_worker` compatibility flag, which is the root cause of
61 * zero cloud-side `tailStream()` delivery (a deployed consumer cannot
62 * enable the streaming tail model). The day this test FAILS — the flag
63 * deploys, or the rejection changes shape — streaming tail workers have
64 * shipped (or changed): re-verify delivery with
65 * CLOUDFLARE_TEST_STREAMING_TAIL_DELIVERY=1 and drop the gate above.
66 */
67 test.provider(
68 "production refuses the streaming_tail_worker compatibility flag (probe)",
69 (stack) =>
70 Effect.gen(function* () {
71 yield* stack.destroy();
72
73 const error = yield* stack
74 .deploy(
75 Effect.gen(function* () {
76 return yield* Cloudflare.Worker("StreamingTailFlagProbe", {
77 script: producerScript,
78 compatibility: { flags: ["streaming_tail_worker"] },
79 });
80 }),
81 )
82 .pipe(Effect.flip);
83
84 expect(error._tag).toEqual("ScriptStartupError");
85 if (error._tag === "ScriptStartupError") {
86 expect(error.message).toContain(
87 "streaming_tail_worker is experimental and cannot yet be used",
88 );
89 }
90
91 yield* stack.destroy();
92 }).pipe(logLevel),
93 {
94 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
95 timeout: 240_000,
96 },
97 );
98
99 /**
100 * `streamingTailConsumers` uploads `streaming_tail_consumers` in the script
101 * metadata. Unlike plain `tail_consumers`, the script-settings read endpoint
102 * does not expose the field, so the attachment is asserted through the
103 * recorded attribute plus (when `CLOUDFLARE_TEST_STREAMING_TAIL_DELIVERY` is
104 * set) actual delivery: the consumer's `tailStream()` persists each completed
105 * session into KV, observed out-of-band via distilled KV reads.
106 */
107 test.provider(
108 "worker with streamingTailConsumers attaches and detaches the consumer (delivery gated)",
109 (stack) =>
110 Effect.gen(function* () {
111 const { accountId } = yield* yield* CloudflareEnvironment;
112
113 yield* stack.destroy();
114
115 const deployStack = (tails: "attached" | "detached") =>
116 Effect.gen(function* () {
117 const events = yield* Cloudflare.KV.Namespace("StreamTailEvents");
118 const consumer = yield* Cloudflare.Worker("StreamingTailConsumer", {
119 main: pathe.resolve(import.meta.dirname, "fixtures/tail/streaming-tail-consumer.ts"),
120 env: { EVENTS: events },
121 });
122 const producer = yield* Cloudflare.Worker("StreamingTailProducer", {
123 script: producerScript,
124 streamingTailConsumers: tails === "attached" ? [consumer] : [],
125 });
126 return { events, consumer, producer };
127 });
128
129 const v1 = yield* stack.deploy(deployStack("attached"));
130
131 // The upload carried `streaming_tail_consumers` (the PUT would have
132 // failed otherwise) and the attribute records the consumer by deployed
133 // script name. GET script-settings has no field for streaming
134 // consumers, so there is no out-of-band settings read to assert
135 // against — the gated delivery assertion below is the only cloud-side
136 // proof of attachment available.
137 expect(v1.producer.streamingTailConsumers).toEqual([{ service: v1.consumer.workerName }]);
138 expect(v1.producer.tailConsumers ?? []).toEqual([]);
139
140 // The producer serves with the streaming consumer attached — the
141 // attachment never breaks the producer's own deploy or execution.
142 yield* expectUrlContains(v1.producer.url!, "streaming-producer-ok", {
143 label: "streaming tail producer",
144 });
145
146 // Delivery assertion — gated (see STREAMING_TAIL_DELIVERY above).
147 if (STREAMING_TAIL_DELIVERY) {
148 // Poll KV until a completed streaming session lands. Each poll
149 // re-invokes the producer so delivery that begins slightly after the
150 // first request still surfaces.
151 const eventKeys = yield* Effect.gen(function* () {
152 yield* expectUrlContains(v1.producer.url!, "streaming-producer-ok", {
153 timeout: "15 seconds",
154 label: "streaming tail producer (poll)",
155 });
156 const keys = yield* kv.listNamespaceKeys({
157 accountId,
158 namespaceId: v1.events.namespaceId,
159 prefix: "evt:",
160 });
161 return keys.result.map((k) => k.name);
162 }).pipe(
163 Effect.repeat({
164 schedule: Schedule.spaced("5 seconds"),
165 until: (keys): boolean => keys.length > 0,
166 times: 18,
167 }),
168 );
169 expect(eventKeys.length).toBeGreaterThan(0);
170
171 // The recorded session is a full onset → log → outcome stream from
172 // one producer invocation, carrying the producer's log marker.
173 const session = yield* kv
174 .getNamespaceValue({
175 accountId,
176 namespaceId: v1.events.namespaceId,
177 keyName: eventKeys[0],
178 })
179 .pipe(
180 Effect.flatMap((res) =>
181 Effect.tryPromise(() =>
182 new Response(Stream.toReadableStream(res.body) as BodyInit).text(),
183 ),
184 ),
185 );
186 expect(session).toContain("alchemy-streaming-tail-marker");
187 const events = JSON.parse(session) as RecordedTailEvent[];
188 const types = events.map((tailEvent) => tailEvent.event?.type);
189 expect(types).toContain("onset");
190 expect(types).toContain("outcome");
191 const log = events.find((tailEvent) => tailEvent.event?.type === "log");
192 expect(log?.event?.message).toEqual(["alchemy-streaming-tail-marker"]);
193 }
194
195 // Detaching every consumer is an in-place update, never a replace.
196 const v2 = yield* stack.deploy(deployStack("detached"));
197 expect(v2.producer.workerId).toEqual(v1.producer.workerId);
198 expect(v2.producer.workerName).toEqual(v1.producer.workerName);
199 expect(v2.producer.streamingTailConsumers ?? []).toEqual([]);
200
201 yield* stack.destroy();
202 yield* waitForWorkerToBeDeleted(v1.producer.workerName, accountId);
203 yield* waitForWorkerToBeDeleted(v1.consumer.workerName, accountId);
204 }).pipe(logLevel),
205 {
206 tags: ["provider:cloudflare", "provider:cloudflare:kv", "provider:cloudflare:worker", "live"],
207 timeout: 240_000,
208 },
209 );