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

main
ts 299 lines 11.5 KB
Raw
1 import { describe, expect } from "@alchemy.run/test";
2 import * as Test from "@alchemy.run/test/Alchemy";
3 import * as Duration from "effect/Duration";
4 import * as Effect from "effect/Effect";
5 import * as Layer from "effect/Layer";
6 import * as Redacted from "effect/Redacted";
7 /**
8 * Binding-diff stability: a deploy with NO changes must produce an all-noop
9 * plan for resources that carry bindings — regardless of the binding data's
10 * shape and regardless of the state store kind.
11 *
12 * The engine's default diff marks a resource "update" whenever any of its
13 * binding rows compares non-noop (`diffBindings` in Diff.ts), so any
14 * asymmetry between what apply persists and what the next plan resolves
15 * re-updates every bound resource on every deploy, forever.
16 *
17 * Two store kinds are exercised:
18 * - the in-memory store used by the engine tests (retains live values), and
19 * - a JSON round-tripping store mirroring every durable store (local file,
20 * S3/R2, HTTP), which serializes state through
21 * `JSON.stringify(encodeState(...))` + `reviveState`.
22 */
23 import { apply } from "@/Apply";
24 import { provideFreshArtifactStore } from "@/Artifacts";
25 import * as Output from "@/Output";
26 import * as Plan from "@/Plan";
27 import type { ResourceBinding } from "@/Resource";
28 import * as Stack from "@/Stack";
29 import { Stage } from "@/Stage";
30 import { encodeState, InMemoryService, reviveState, State } from "@/State";
31 import { BindingTarget, TestLayers, TestResource, TestResourceHooks } from "./test.resources.ts";
32
33 const { test } = Test.make({
34 providers: TestLayers(),
35 state: Layer.effect(
36 State,
37 Effect.sync(() => InMemoryService({})),
38 ),
39 });
40
41 /**
42 * In-memory state that JSON round-trips every write, mirroring what durable
43 * state stores do to persisted state (function-valued leaves are dropped,
44 * `toJSON`-carrying objects — e.g. Effects — are replaced by their JSON
45 * form, `undefined` keys vanish).
46 */
47 const jsonRoundTripState = () => {
48 const roundTrip = <T>(v: T): T => JSON.parse(JSON.stringify(encodeState(v)), reviveState);
49 const store: Record<string, Record<string, Record<string, any>>> = {};
50 return Layer.effect(
51 State,
52 Effect.sync(() =>
53 State.of(
54 (InMemoryService(store) as Effect.Effect<any>).pipe(
55 Effect.map((svc: any) => ({
56 ...svc,
57 set: (req: any) => svc.set({ ...req, value: roundTrip(req.value) }),
58 })),
59 ) as any,
60 ),
61 ),
62 );
63 };
64
65 type StoreKind = "in-memory" | "json";
66
67 /** Minimal deploy/plan harness over a private state store. */
68 const makeHarness = (name: string, kind: StoreKind) => {
69 // One store per harness, shared across the deploy and the follow-up plan
70 // (the layer itself is rebuilt for every Stack.make).
71 const store: Record<string, Record<string, Record<string, any>>> = {};
72 const stateLayer =
73 kind === "json"
74 ? jsonRoundTripState()
75 : Layer.effect(
76 State,
77 Effect.sync(() => InMemoryService(store)),
78 );
79 const compile = (effect: Effect.Effect<any, any, any>) =>
80 (effect as Effect.Effect<any, any, never>).pipe(
81 Stack.make({
82 name,
83 providers: TestLayers() as Layer.Layer<any, never, any>,
84 state: stateLayer,
85 }),
86 );
87 const deploy = (effect: Effect.Effect<any, any, any>): Effect.Effect<any, any, never> =>
88 compile(effect).pipe(
89 Effect.flatMap((compiled: any) =>
90 Plan.make(compiled).pipe(Effect.flatMap(apply), Effect.provide(compiled.services)),
91 ),
92 Effect.provide(Layer.succeed(Stage, "test")),
93 provideFreshArtifactStore,
94 ) as unknown as Effect.Effect<any, any, never>;
95 const plan = (effect: Effect.Effect<any, any, any>): Effect.Effect<any, any, never> =>
96 compile(effect).pipe(
97 Effect.flatMap((compiled: any) =>
98 Plan.make(compiled).pipe(Effect.provide(compiled.services)),
99 ),
100 Effect.provide(Layer.succeed(Stage, "test")),
101 provideFreshArtifactStore,
102 ) as unknown as Effect.Effect<any, any, never>;
103 return { deploy, plan };
104 };
105
106 const expectAllNoop = (plan: any) => {
107 for (const [fqn, node] of Object.entries<any>(plan.resources)) {
108 expect(`${fqn}:${node.action}`).toBe(`${fqn}:noop`);
109 for (const row of node.bindings ?? []) {
110 expect(`${fqn}:${row.sid}:${row.action}`).toBe(`${fqn}:${row.sid}:noop`);
111 }
112 }
113 };
114
115 /** Each shape deploys once, then asserts the second plan is entirely noop. */
116 const shapes: Record<string, () => Effect.Effect<any, any, any>> = {
117 "PropExpr data": () =>
118 Effect.gen(function* () {
119 const upstream = yield* TestResource("Upstream", { string: "up-value" });
120 const target = yield* BindingTarget("Host", { name: "host" });
121 yield* target.bind("Cap", { env: { UPSTREAM: upstream.string } });
122 return target;
123 }),
124 "whole-resource data": () =>
125 Effect.gen(function* () {
126 const upstream = yield* TestResource("Upstream", { string: "up-value" });
127 const target = yield* BindingTarget("Host", { name: "host" });
128 yield* target.bind("Cap", { env: { NAME: upstream.string }, resource: upstream } as any);
129 return target;
130 }),
131 "self-referential data": () =>
132 Effect.gen(function* () {
133 const target = yield* BindingTarget("Host", { name: "host" });
134 yield* target.bind("Self", { env: { ME: target.string } });
135 return target;
136 }),
137 "mapped Output data": () =>
138 Effect.gen(function* () {
139 const upstream = yield* TestResource("Upstream", { string: "up-value" });
140 const target = yield* BindingTarget("Host", { name: "host" });
141 yield* target.bind("Cap", {
142 env: { MAPPED: upstream.string.pipe(Output.map((s) => `${s}-sfx`)) },
143 });
144 return target;
145 }),
146 "Effect-valued data": () =>
147 Effect.gen(function* () {
148 const target = yield* BindingTarget("Host", { name: "host" });
149 yield* target.bind("Cap", { env: { NAME: Effect.succeed("x") } } as any);
150 return target;
151 }),
152 // A tagged Resource class is a function-typed Effect — the standard
153 // circular-binding pattern (`env: { WORKER: WorkerClass }`). It passes
154 // through plan/apply resolution untouched, and a JSON store persists it
155 // via its `toJSON` as an `{"_id":"Effect",...}` relic. Without stripping
156 // at the commit boundary, `diffBindings` compares that relic against the
157 // live class (stripped to `undefined`) — a phantom "update" on every
158 // deploy, forever.
159 "class-valued (Effectable) data": () =>
160 Effect.gen(function* () {
161 const target = yield* BindingTarget("Host", { name: "host" });
162 yield* target.bind("Cap", { env: { PEER: TestResource } } as any);
163 return target;
164 }),
165 "Redacted data": () =>
166 Effect.gen(function* () {
167 const target = yield* BindingTarget("Host", { name: "host" });
168 yield* target.bind("Cap", { env: { SECRET: Redacted.make("shh") } } as any);
169 return target;
170 }),
171 "Duration data": () =>
172 Effect.gen(function* () {
173 const target = yield* BindingTarget("Host", { name: "host" });
174 yield* target.bind("Cap", { env: { TIMEOUT: Duration.seconds(30) } } as any);
175 return target;
176 }),
177 "mutual bindings": () =>
178 Effect.gen(function* () {
179 const A = yield* BindingTarget("A", { name: "a" });
180 const B = yield* BindingTarget("B", { name: "b" });
181 yield* A.bind("FromB", { env: { PEER: B.string } });
182 yield* B.bind("FromA", { env: { PEER: A.string } });
183 return { A, B };
184 }),
185 };
186
187 for (const kind of ["in-memory", "json"] as StoreKind[]) {
188 describe(`no-change redeploy is all-noop (${kind} state)`, { tags: ["unit", "local"] }, () => {
189 for (const [shape, program] of Object.entries(shapes)) {
190 test(
191 shape,
192 Effect.gen(function* () {
193 const { deploy, plan } = makeHarness(
194 `bind-${kind}-${shape.replaceAll(/[^a-zA-Z0-9]/g, "-")}`,
195 kind,
196 );
197 yield* deploy(program());
198 expectAllNoop(yield* plan(program()));
199 }),
200 );
201 }
202 });
203 }
204
205 describe("binding rows are deterministically ordered", { tags: ["unit", "local"] }, () => {
206 // Bindings are registered by concurrently-built layers doing real IO before
207 // `host.bind`, so registration order is not stable across deploys. The
208 // engine sorts rows by sid at every boundary (dedupeBindings/diffBindings)
209 // so provider diff/reconcile inputs and persisted state never churn on a
210 // registration-order flip.
211 test(
212 "a registration-order flip stays noop and providers observe sorted rows",
213 Effect.gen(function* () {
214 const { deploy, plan } = makeHarness("bind-order", "in-memory");
215
216 const program = (flipped: boolean) =>
217 Effect.gen(function* () {
218 const target = yield* BindingTarget("Host", { name: "host" });
219 const bindA = target.bind("CapA", { env: { A: "1" } });
220 const bindB = target.bind("CapB", { env: { B: "2" } });
221 if (flipped) {
222 yield* bindB;
223 yield* bindA;
224 } else {
225 yield* bindA;
226 yield* bindB;
227 }
228 return target;
229 });
230
231 yield* deploy(program(false));
232
233 const observed: ResourceBinding[][] = [];
234 const p: any = yield* plan(program(true)).pipe(
235 Effect.provide(
236 Layer.succeed(TestResourceHooks, {
237 diff: (_id, newBindings) => Effect.sync(() => void observed.push(newBindings)),
238 }),
239 ),
240 );
241
242 expectAllNoop(p);
243 // The provider's diff observed the sid-sorted row order even though
244 // registration order was flipped between deploys.
245 expect(observed.length).toBeGreaterThan(0);
246 for (const rows of observed) {
247 expect(rows.map((r) => r.sid)).toEqual(["CapA", "CapB"]);
248 }
249 // The plan node's rows are sorted too.
250 expect(p.resources.Host.bindings.map((r: any) => r.sid)).toEqual(["CapA", "CapB"]);
251 }),
252 );
253 });
254
255 describe("persisted binding rows are plain data", { tags: ["unit", "local"] }, () => {
256 // Mirror of the "interrupted create persists no unresolved Output exprs"
257 // apply test, for bindings: even non-terminal commits must not persist live
258 // Output proxies or Effect leaves into the state store.
259 test(
260 "terminal state holds no live Effect leaves in binding data",
261 Effect.gen(function* () {
262 const store: Record<string, any> = {};
263 const stateLayer = Layer.effect(
264 State,
265 Effect.sync(() => InMemoryService(store)),
266 );
267 const program = Effect.gen(function* () {
268 const target = yield* BindingTarget("Host", { name: "host" });
269 yield* target.bind("Cap", {
270 env: { PEER: TestResource, NAME: Effect.succeed("x") },
271 } as any);
272 return target;
273 });
274 yield* (program as unknown as Effect.Effect<any, any, never>).pipe(
275 Stack.make({
276 name: "bind-plain",
277 providers: TestLayers() as Layer.Layer<any, never, any>,
278 state: stateLayer,
279 }),
280 Effect.flatMap((compiled: any) =>
281 Plan.make(compiled).pipe(Effect.flatMap(apply), Effect.provide(compiled.services)),
282 ),
283 Effect.provide(Layer.succeed(Stage, "test")),
284 provideFreshArtifactStore,
285 ) as unknown as Effect.Effect<any, any, never>;
286
287 const persisted = store["bind-plain"].test.Host;
288 const hasLiveEffect = (value: unknown): boolean => {
289 if (Effect.isEffect(value)) return true;
290 if (Array.isArray(value)) return value.some(hasLiveEffect);
291 if (value && typeof value === "object") {
292 return Object.values(value).some(hasLiveEffect);
293 }
294 return false;
295 };
296 expect(hasLiveEffect(persisted.bindings)).toBe(false);
297 }),
298 );
299 });