@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 103 lines 3.61 KB
Raw
1 import type * as Context from "effect/Context";
2 import * as Effect from "effect/Effect";
3 import * as EngineDrift from "../Drift.ts";
4 import { emit } from "../Events.ts";
5 import type { Plan } from "../Plan.ts";
6 import type { ConfiguredStack } from "../Stack.ts";
7 import type { State } from "../State/State.ts";
8 import {
9 buildWithoutProgram,
10 summarize,
11 type BuildOptions,
12 type PlanSummary,
13 } from "./Operations.ts";
14
15 export interface DriftedResource {
16 readonly fqn: string;
17 readonly logicalId: string;
18 readonly resourceType: string;
19 /** `skipped`: not checked (no `read`, or an interrupted deploy); see `reason`. */
20 readonly status: "in-sync" | "drifted" | "missing" | "skipped";
21 readonly reason?: string;
22 readonly actual?: unknown;
23 }
24
25 /** A drift check and the repair it proposes; only {@link execute} runs it. */
26 export interface DriftScan {
27 readonly stack: { readonly name: string; readonly stage: string };
28 readonly resources: ReadonlyArray<DriftedResource>;
29 /** What the repair would change. */
30 readonly summary: PlanSummary;
31 /**
32 * The repair as an engine plan, for renderers.
33 * @internal its shape may change.
34 */
35 readonly native: Plan;
36 /**
37 * The stack services drift was checked under.
38 * @internal {@link execute} runs in them.
39 */
40 readonly context: Context.Context<State>;
41 }
42
43 export interface ScanOptions extends BuildOptions {
44 readonly stack: ConfiguredStack<unknown, unknown>;
45 }
46
47 /** Whether the drift check found anything worth repairing. */
48 export const hasDrift = (scan: DriftScan): boolean =>
49 scan.resources.some((resource) => resource.status === "drifted" || resource.status === "missing");
50
51 const status = (action: string): DriftedResource["status"] =>
52 action === "unchanged"
53 ? "in-sync"
54 : action === "missing" || action === "skipped"
55 ? action
56 : "drifted";
57
58 /**
59 * Compare deployed state against the real cloud and plan the repair. Drift
60 * reads state and providers only, so the stack's program never runs.
61 */
62 export const scan = Effect.fn("Alchemist.Drift.scan")(function* (input: ScanOptions) {
63 const built = yield* buildWithoutProgram(input.stack, input);
64 const identity = {
65 name: built.stack.name,
66 stage: built.stack.stage,
67 };
68 // The engine's phase and per-resource observation events flow through the
69 // log as events; drift-flavored wording is the renderer's job
70 // (renderPlanning's computingLabel).
71 const { result, plan } = yield* EngineDrift.plan(identity).pipe(Effect.provide(built.context));
72 yield* emit({ type: "plan.phase", phase: "plan-ready" });
73 return {
74 stack: identity,
75 resources: Object.values(result.resources).map((resource) => ({
76 fqn: resource.fqn,
77 logicalId: resource.logicalId,
78 resourceType: resource.resourceType,
79 status: status(resource.action),
80 reason: resource.reason,
81 actual: resource.attr,
82 })),
83 summary: summarize(plan),
84 native: plan,
85 context: built.context,
86 } satisfies DriftScan;
87 });
88
89 /**
90 * Repair the drift `scan` found: reconcile each drifted or missing resource
91 * back to its last-deployed desired state. Only those resources are touched;
92 * they are re-read first, so one that is back in sync is left alone, and
93 * drift that appeared after the scan waits for the next one.
94 */
95 export const execute = Effect.fn("Alchemist.Drift.execute")(function* (scan: DriftScan) {
96 const only = scan.resources
97 .filter((resource) => resource.status === "drifted" || resource.status === "missing")
98 .map((resource) => resource.fqn);
99 return yield* Effect.provide(
100 EngineDrift.repair({ name: scan.stack.name, stage: scan.stack.stage }, { only }),
101 scan.context,
102 );
103 });