@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, 04:45 UTC

main
ts 104 lines 3.94 KB
Raw
1 import type { FlyMachineConfig } from "@distilled.cloud/fly-io/machines";
2 import { pinsFromConfig, sameImageSet, validObservedImageSet } from "./DeploymentImages.ts";
3 import { alchemyMetadataKeys as keys } from "./Metadata.ts";
4
5 type DeploymentState = { protocol: "legacy" | "1" | "2" } | { protocol: "invalid"; reason: string };
6
7 /** Observation and reconciliation must agree before treating a Machine as legacy. */
8 export const classifyDeploymentState = (machine: {
9 config?: FlyMachineConfig;
10 }): DeploymentState => {
11 const metadata = machine.config?.metadata;
12 const protocol = metadata?.[keys.protocol];
13 if (protocol !== undefined && protocol !== "1" && protocol !== "2")
14 return {
15 protocol: "invalid",
16 reason: `Unknown deployment protocol ${protocol}.`,
17 };
18 if (protocol !== "2" && metadata?.[keys.containerImageSet] !== undefined)
19 return {
20 protocol: "invalid",
21 reason: "Container image metadata requires deployment protocol 2.",
22 };
23 if (protocol === "2")
24 return validProtocol2RecoveryMetadata(machine)
25 ? { protocol }
26 : {
27 protocol: "invalid",
28 reason: "Invalid protocol-2 image set or recovery metadata.",
29 };
30 if (protocol === "1") {
31 const replica = metadata?.[keys.replica] ?? "";
32 if (
33 !/^(0|[1-9]\d*)$/.test(replica) ||
34 !Number.isSafeInteger(Number(replica)) ||
35 !["candidate", "promoting", "validating", "active", "retiring"].includes(
36 metadata?.[keys.phase] ?? "",
37 )
38 )
39 return {
40 protocol: "invalid",
41 reason: "Invalid protocol-1 replica index or deployment phase.",
42 };
43 return { protocol };
44 }
45 return { protocol: "legacy" };
46 };
47
48 /** Required recovery fields for a complete protocol-2 generation member. */
49 const validProtocol2RecoveryMetadata = (machine: { config?: FlyMachineConfig }): boolean => {
50 if (!validObservedImageSet(machine)) return false;
51 const recorded = machine.config?.metadata;
52 const countText = recorded?.[keys.count] ?? "";
53 const sequenceText = recorded?.[keys.sequence] ?? "";
54 const replicaText = recorded?.[keys.replica] ?? "";
55 const count = Number(countText);
56 const index = Number(replicaText);
57 const roles = (recorded?.[keys.roles] ?? "").split(",");
58 return (
59 Boolean(recorded?.[keys.generation]?.trim()) &&
60 Boolean(recorded?.[keys.workload]?.trim()) &&
61 /^[1-9]\d*$/.test(countText) &&
62 Number.isSafeInteger(count) &&
63 /^[1-9]\d*$/.test(sequenceText) &&
64 Number.isSafeInteger(Number(sequenceText)) &&
65 /^(0|[1-9]\d*)$/.test(replicaText) &&
66 Number.isSafeInteger(index) &&
67 index < count &&
68 roles.length === count &&
69 roles.includes("run") &&
70 roles.every((role) => role === "run" || role === "idle") &&
71 recorded?.[keys.role] === roles[index] &&
72 ["candidate", "promoting", "validating", "active", "retiring"].includes(
73 recorded?.[keys.phase] ?? "",
74 ) &&
75 ["true", "false"].includes(recorded?.[keys.restored] ?? "")
76 );
77 };
78
79 /** A partial protocol-2 generation is valid only when every member agrees on its identity. */
80 export const validProtocol2Generation = (
81 group: readonly { config?: FlyMachineConfig }[],
82 ): boolean => {
83 const first = group[0]?.config?.metadata;
84 if (!first || group.length === 0) return false;
85 const indices = new Set<string>();
86 for (const machine of group) {
87 const metadata = machine.config?.metadata;
88 const replica = metadata?.[keys.replica];
89 if (
90 !validProtocol2RecoveryMetadata(machine) ||
91 metadata?.[keys.generation] !== first[keys.generation] ||
92 metadata?.[keys.count] !== first[keys.count] ||
93 metadata?.[keys.sequence] !== first[keys.sequence] ||
94 metadata?.[keys.workload] !== first[keys.workload] ||
95 metadata?.[keys.roles] !== first[keys.roles] ||
96 replica === undefined ||
97 indices.has(replica) ||
98 !sameImageSet(pinsFromConfig(machine.config), pinsFromConfig(group[0]?.config))
99 )
100 return false;
101 indices.add(replica);
102 }
103 return true;
104 };