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

main
ts 41 lines 1.47 KB
Raw
1 import type { ScratchStack } from "@alchemy.run/test/Alchemy";
2 import * as Drift from "alchemy/Drift";
3 import * as Effect from "effect/Effect";
4 import * as Schedule from "effect/Schedule";
5
6 const target = (stack: ScratchStack) => ({ name: stack.name, stage: stack.stage });
7
8 /** `alchemy drift` for one resource of a scratch stack. */
9 export const driftOf = (stack: ScratchStack, logicalId: string) =>
10 Drift.detect(target(stack)).pipe(
11 Effect.map((result) => result.resources[logicalId]),
12 Effect.provide(stack.state),
13 );
14
15 /** The drift action of every resource in a scratch stack, by logical id. */
16 export const driftActions = (stack: ScratchStack) =>
17 Drift.detect(target(stack)).pipe(
18 Effect.map((result) =>
19 Object.fromEntries(
20 Object.entries(result.resources).map(([id, resource]) => [id, resource?.action]),
21 ),
22 ),
23 Effect.provide(stack.state),
24 );
25
26 /**
27 * Re-run drift detection until the resource reads as `expected`, so an
28 * out-of-band change has time to become visible (or a repair to settle).
29 */
30 export const waitForDrift = (stack: ScratchStack, logicalId: string, expected: Drift.DriftAction) =>
31 driftOf(stack, logicalId).pipe(
32 Effect.repeat({
33 until: (result) => result?.action === expected,
34 schedule: Schedule.spaced("2 seconds"),
35 times: 15,
36 }),
37 );
38
39 /** `alchemy drift --repair` for a scratch stack. */
40 export const repairDrift = (stack: ScratchStack) =>
41 Drift.repair(target(stack)).pipe(Effect.provide(stack.state));