alchemy 2.0.0-beta.45 → 2.0.0-beta.46

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/lib/AlchemyContext.d.ts +0 -1
  2. package/lib/AlchemyContext.d.ts.map +1 -1
  3. package/lib/AlchemyContext.js.map +1 -1
  4. package/lib/Cli/commands/cloudflare.d.ts.map +1 -1
  5. package/lib/Cli/commands/cloudflare.js +5 -1
  6. package/lib/Cli/commands/cloudflare.js.map +1 -1
  7. package/lib/Cloudflare/Providers.d.ts.map +1 -1
  8. package/lib/Cloudflare/Providers.js +5 -1
  9. package/lib/Cloudflare/Providers.js.map +1 -1
  10. package/lib/Cloudflare/Queue/QueueConsumer.d.ts.map +1 -1
  11. package/lib/Cloudflare/Queue/QueueConsumer.js +1 -2
  12. package/lib/Cloudflare/Queue/QueueConsumer.js.map +1 -1
  13. package/lib/Cloudflare/StateStore/Api.d.ts +1 -1
  14. package/lib/Cloudflare/StateStore/Api.js +1 -1
  15. package/lib/Cloudflare/StateStore/State.d.ts +11 -65
  16. package/lib/Cloudflare/StateStore/State.d.ts.map +1 -1
  17. package/lib/Cloudflare/StateStore/State.js +307 -366
  18. package/lib/Cloudflare/StateStore/State.js.map +1 -1
  19. package/lib/Cloudflare/StateStore/Store.d.ts.map +1 -1
  20. package/lib/Cloudflare/StateStore/Store.js +16 -29
  21. package/lib/Cloudflare/StateStore/Store.js.map +1 -1
  22. package/lib/Cloudflare/Vectorize/VectorizeIndex.d.ts +114 -0
  23. package/lib/Cloudflare/Vectorize/VectorizeIndex.d.ts.map +1 -0
  24. package/lib/Cloudflare/Vectorize/VectorizeIndex.js +157 -0
  25. package/lib/Cloudflare/Vectorize/VectorizeIndex.js.map +1 -0
  26. package/lib/Cloudflare/Vectorize/VectorizeIndexBinding.d.ts +38 -0
  27. package/lib/Cloudflare/Vectorize/VectorizeIndexBinding.d.ts.map +1 -0
  28. package/lib/Cloudflare/Vectorize/VectorizeIndexBinding.js +44 -0
  29. package/lib/Cloudflare/Vectorize/VectorizeIndexBinding.js.map +1 -0
  30. package/lib/Cloudflare/Vectorize/VectorizeMetadataIndex.d.ts +72 -0
  31. package/lib/Cloudflare/Vectorize/VectorizeMetadataIndex.d.ts.map +1 -0
  32. package/lib/Cloudflare/Vectorize/VectorizeMetadataIndex.js +131 -0
  33. package/lib/Cloudflare/Vectorize/VectorizeMetadataIndex.js.map +1 -0
  34. package/lib/Cloudflare/Vectorize/index.d.ts +4 -0
  35. package/lib/Cloudflare/Vectorize/index.d.ts.map +1 -0
  36. package/lib/Cloudflare/Vectorize/index.js +4 -0
  37. package/lib/Cloudflare/Vectorize/index.js.map +1 -0
  38. package/lib/Cloudflare/Workers/WorkerAsyncBindings.d.ts.map +1 -1
  39. package/lib/Cloudflare/Workers/WorkerAsyncBindings.js +8 -0
  40. package/lib/Cloudflare/Workers/WorkerAsyncBindings.js.map +1 -1
  41. package/lib/Cloudflare/Workers/WorkerBinding.d.ts +2 -1
  42. package/lib/Cloudflare/Workers/WorkerBinding.d.ts.map +1 -1
  43. package/lib/Cloudflare/Workers/WorkerBinding.js.map +1 -1
  44. package/lib/Cloudflare/index.d.ts +1 -0
  45. package/lib/Cloudflare/index.d.ts.map +1 -1
  46. package/lib/Cloudflare/index.js +1 -0
  47. package/lib/Cloudflare/index.js.map +1 -1
  48. package/lib/State/HttpStateStore.d.ts +4 -0
  49. package/lib/State/HttpStateStore.d.ts.map +1 -1
  50. package/lib/State/HttpStateStore.js +13 -0
  51. package/lib/State/HttpStateStore.js.map +1 -1
  52. package/lib/Util/poll.d.ts +30 -0
  53. package/lib/Util/poll.d.ts.map +1 -0
  54. package/lib/Util/poll.js +26 -0
  55. package/lib/Util/poll.js.map +1 -0
  56. package/lib/tsconfig.test.tsbuildinfo +1 -1
  57. package/package.json +7 -7
  58. package/src/AlchemyContext.ts +0 -1
  59. package/src/Cli/commands/cloudflare.ts +5 -3
  60. package/src/Cloudflare/Providers.ts +7 -0
  61. package/src/Cloudflare/Queue/QueueConsumer.ts +0 -2
  62. package/src/Cloudflare/StateStore/Api.ts +1 -1
  63. package/src/Cloudflare/StateStore/State.ts +445 -507
  64. package/src/Cloudflare/StateStore/Store.ts +22 -35
  65. package/src/Cloudflare/Vectorize/VectorizeIndex.ts +261 -0
  66. package/src/Cloudflare/Vectorize/VectorizeIndexBinding.ts +103 -0
  67. package/src/Cloudflare/Vectorize/VectorizeMetadataIndex.ts +208 -0
  68. package/src/Cloudflare/Vectorize/index.ts +3 -0
  69. package/src/Cloudflare/Workers/WorkerAsyncBindings.ts +7 -0
  70. package/src/Cloudflare/Workers/WorkerBinding.ts +2 -0
  71. package/src/Cloudflare/index.ts +1 -0
  72. package/src/State/HttpStateStore.ts +28 -0
  73. package/src/Util/poll.ts +47 -0
@@ -8,9 +8,9 @@ import * as HttpClient from "effect/unstable/http/HttpClient";
8
8
  import * as HttpClientRequest from "effect/unstable/http/HttpClientRequest";
9
9
  import * as HttpApiClient from "effect/unstable/httpapi/HttpApiClient";
10
10
  import crypto from "node:crypto";
11
- import * as Output from "../../Output.ts";
12
11
 
13
12
  import * as Config from "effect/Config";
13
+ import { isHttpClientError } from "effect/unstable/http/HttpClientError";
14
14
  import { adopt } from "../../AdoptPolicy.ts";
15
15
  import { AuthError } from "../../Auth/AuthProvider.ts";
16
16
  import {
@@ -20,16 +20,14 @@ import {
20
20
  import { ALCHEMY_PROFILE, ProfileLive } from "../../Auth/Profile.ts";
21
21
  import * as Cloudflare from "../../Cloudflare/Providers.ts";
22
22
  import { deploy } from "../../Deploy.ts";
23
+ import * as Output from "../../Output.ts";
23
24
  import * as Alchemy from "../../Stack.ts";
24
25
  import { StateApi } from "../../State/HttpStateApi.ts";
25
26
  import {
27
+ checkHttpStateStoreAuth,
26
28
  makeHttpStateStore,
27
29
  type HttpStateStoreCredentials,
28
30
  } from "../../State/HttpStateStore.ts";
29
- import * as CloudflareEnvironment from "../CloudflareEnvironment.ts";
30
- import * as Credentials from "../Credentials.ts";
31
-
32
- import { AlchemyContext } from "../../AlchemyContext.ts";
33
31
  import { makeLocalState } from "../../State/LocalState.ts";
34
32
  import { State, type StateService } from "../../State/State.ts";
35
33
  import {
@@ -39,94 +37,170 @@ import {
39
37
  import * as Clank from "../../Util/Clank.ts";
40
38
  import * as Access from "../Access.ts";
41
39
  import { CloudflareAuth } from "../Auth/AuthProvider.ts";
40
+ import * as CloudflareEnvironment from "../CloudflareEnvironment.ts";
41
+ import * as Credentials from "../Credentials.ts";
42
42
  import { EdgeSessionError, createEdgeSession } from "../EdgeSession.ts";
43
43
  import Api, { STATE_STORE_SCRIPT_NAME, STATE_STORE_VERSION } from "./Api.ts";
44
44
  import { AuthToken, AuthTokenSecretName, TokenValue } from "./Token.ts";
45
45
 
46
+ const CI = Config.boolean("CI").pipe(Config.withDefault(false));
47
+
46
48
  /** Filename used for stored credentials under the profile directory. */
47
49
  const CREDENTIALS_FILE = "cloudflare-state-store";
48
50
 
49
- /**
50
- * SHA-256 hex digest of the Cloudflare account ID. Used as a stable
51
- * pseudonymous identifier on telemetry spans so the dashboard can
52
- * count distinct state-store deployments without leaking the raw
53
- * accountId. Mirrors the `alchemy.git.origin_hash` pattern in
54
- * `Telemetry/Attributes.ts`.
55
- */
56
- const hashAccountId = (accountId: string) =>
57
- Effect.sync(() =>
58
- crypto.createHash("sha256").update(accountId).digest("hex"),
59
- );
51
+ export const state = () =>
52
+ Layer.effect(
53
+ State,
54
+ Effect.gen(function* () {
55
+ const isCI = yield* CI;
56
+ const scriptName = STATE_STORE_SCRIPT_NAME;
57
+ const profileName = yield* ALCHEMY_PROFILE;
58
+ const localStage = `${profileName}_${scriptName}`;
59
+ const credStore = yield* CredentialsStore;
60
+ if (yield* hasLocalStack(localStage)) {
61
+ // if there's still a local stack, then we need to finish the bootstrap
62
+ // TODO(sam): what if the local stack was
63
+ return yield* deployWithLocalState({
64
+ scriptName,
65
+ profileName,
66
+ isCI,
67
+ force: false,
68
+ });
69
+ }
60
70
 
61
- /**
62
- * Best-effort Cloudflare-account-hash annotation on the current span.
63
- * Resolves the accountId from {@link CloudflareEnvironment} and
64
- * attaches `alchemy.cloudflare.account_hash` to whichever span is
65
- * active. Silently no-ops if the environment isn't resolvable so
66
- * State-store layer construction still succeeds in degraded paths.
67
- *
68
- * `noTrack` controls whether the hash is attached:
69
- * - `true` — never annotate (caller-level opt-out).
70
- * - `false` — always annotate, regardless of env.
71
- * - `undefined` — fall back to the `NO_TRACK` env var; default off.
72
- */
73
- const annotateAccountHash = (noTrack?: boolean) =>
74
- Effect.gen(function* () {
75
- if (noTrack === true) return;
76
- if (noTrack === undefined) {
77
- const fromEnv = yield* Config.boolean("NO_TRACK").pipe(
78
- Config.withDefault(false),
71
+ const ensureLatest = ({
72
+ url,
73
+ authToken,
74
+ }: {
75
+ url: string;
76
+ authToken: string;
77
+ }) =>
78
+ Effect.gen(function* () {
79
+ const { matches, expected, observed } =
80
+ yield* checkStateStoreVersion(url);
81
+
82
+ if (observed === undefined) {
83
+ const shouldDeploy = yield* Clank.confirm({
84
+ message: `Cloudflare State Store '${scriptName}' is not available. Do you want to deploy it?`,
85
+ });
86
+ if (shouldDeploy) {
87
+ return yield* bootstrap({
88
+ workerName: scriptName,
89
+ profile: profileName,
90
+ });
91
+ } else {
92
+ return yield* Effect.die(new Clank.PromptCancelled());
93
+ }
94
+ }
95
+
96
+ const httpState = yield* ensureAccess({ url, authToken });
97
+ if (matches) {
98
+ return httpState;
99
+ } else if (isCI) {
100
+ return yield* Effect.die(
101
+ new AuthError({
102
+ message: `Cloudflare State store not found. Run 'alchemy bootstrap cloudflare --profile <your-ci-profile>' to deploy it first.`,
103
+ }),
104
+ );
105
+ } else {
106
+ const shouldDeploy = yield* Clank.confirm({
107
+ message:
108
+ `Cloudflare State Store '${scriptName}' is out of date ` +
109
+ `(expected v${expected}, observed v${observed ?? "unknown"})`,
110
+ });
111
+ if (shouldDeploy) {
112
+ const stateStoreOptions = yield* deployStateStore({
113
+ stage: scriptName,
114
+ state: httpState,
115
+ force: false,
116
+ });
117
+ return yield* makeCloudflareStateStore(stateStoreOptions);
118
+ } else {
119
+ return yield* Effect.die(new Clank.PromptCancelled());
120
+ }
121
+ }
122
+ });
123
+
124
+ const ensureAccess = (credentials: HttpStateStoreCredentials) =>
125
+ Effect.gen(function* () {
126
+ const isAuth = yield* checkHttpStateStoreAuth(credentials);
127
+ if (!isAuth) {
128
+ // our token is wrong, force a refresh
129
+ yield* Clank.info(
130
+ `Cloudflare State store authentication failed, refreshing credentials...`,
131
+ );
132
+ const credentials = yield* loginWithCloudflare(profileName, true);
133
+ if (!(yield* checkHttpStateStoreAuth(credentials))) {
134
+ return yield* Effect.die(
135
+ new AuthError({
136
+ message: `Cloudflare State store authentication failed, after refreshing credentials.`,
137
+ }),
138
+ );
139
+ }
140
+ return yield* makeCloudflareStateStore(credentials);
141
+ }
142
+ return yield* makeCloudflareStateStore(credentials);
143
+ });
144
+
145
+ const credentials = yield* credStore.read<HttpStateStoreCredentials>(
146
+ profileName,
147
+ CREDENTIALS_FILE,
79
148
  );
80
- if (fromEnv) return;
81
- }
82
- const env = yield* Effect.serviceOption(
83
- CloudflareEnvironment.CloudflareEnvironment,
84
- );
85
- if (env._tag !== "Some") return;
86
- const hash = yield* hashAccountId(env.value.accountId);
87
- yield* Effect.annotateCurrentSpan("alchemy.cloudflare.account_hash", hash);
88
- }).pipe(Effect.catch(() => Effect.void));
149
+ if (credentials) {
150
+ return yield* ensureLatest(credentials);
151
+ }
152
+ const workerExists = yield* isStateStoreAvailable(scriptName);
153
+ if (workerExists) {
154
+ return yield* ensureLatest(
155
+ yield* loginWithCloudflare(profileName, false),
156
+ );
157
+ } else if (isCI) {
158
+ // TODO(sam): do we want to support bootstrapping the state store from CI?
159
+ // for now - just die here
160
+ return yield* Effect.die(
161
+ new AuthError({
162
+ message: `Cloudflare State store not found. Run 'alchemy bootstrap cloudflare --profile <your-ci-profile>' to deploy it first.`,
163
+ }),
164
+ );
165
+ } else {
166
+ return yield* Clank.confirm({
167
+ message:
168
+ "Cloudflare State Store not found. Do you want to deploy it?",
169
+ }).pipe(
170
+ Effect.flatMap((shouldDeploy) =>
171
+ shouldDeploy
172
+ ? bootstrap()
173
+ : Effect.die(new Clank.PromptCancelled()),
174
+ ),
175
+ );
176
+ }
177
+ }).pipe(recordStateStoreInit, Effect.orDie),
178
+ ).pipe(
179
+ Layer.provideMerge(Credentials.fromAuthProvider()),
180
+ Layer.provideMerge(CloudflareEnvironment.fromProfile()),
181
+ Layer.provideMerge(CloudflareAuth),
182
+ Layer.provideMerge(Access.AccessLive),
183
+ Layer.provideMerge(ProfileLive),
184
+ Layer.provideMerge(CredentialsStoreLive),
185
+ Layer.orDie,
186
+ );
89
187
 
90
188
  export interface BootstrapOptions {
91
- /**
92
- * The name of the script to use for the state store.
93
- * @default "alchemy-state-store"
94
- */
189
+ /** @default "alchemy-state-store" */
95
190
  workerName?: string;
96
- /**
97
- * Re-run the deploy even if the worker already exists in Cloudflare.
98
- *
99
- * Without this flag, an already-deployed worker is **adopted**: the
100
- * auth-token secret is re-fetched via an edge probe and the local
101
- * credentials file is refreshed, but no new code is uploaded. With
102
- * `force: true` the full deploy runs again, redeploying the worker
103
- * code and reconciling every resource in place.
104
- */
191
+ /** @default false */
105
192
  force?: boolean;
193
+ /** @default "default" */
194
+ profile?: string;
106
195
  }
107
196
 
108
- /**
109
- * Manually bootstrap (or repair) the Cloudflare-hosted HTTP State
110
- * Store. Used by `alchemy bootstrap cloudflare`.
111
- *
112
- * The flow mirrors what {@link state} does on first use, but is
113
- * exposed as a standalone effect so users can trigger it (or recover
114
- * from a partially-failed previous deploy) explicitly:
115
- *
116
- * 1. Resume any in-progress local `CloudflareStateStore` stack.
117
- * 2. If the worker already exists in Cloudflare and `force` is not
118
- * set, adopt it: read the auth token via an edge-preview probe
119
- * and persist credentials.
120
- * 3. Otherwise run the full deploy with adoption enabled, sync local
121
- * state into the deployed store, persist credentials, and delete
122
- * the local stack.
123
- */
124
197
  export const bootstrap = (options: BootstrapOptions = {}) =>
125
198
  Effect.gen(function* () {
126
- const isCI = yield* Config.boolean("CI").pipe(Config.withDefault(false));
127
- const profileName = yield* ALCHEMY_PROFILE;
199
+ const isCI = yield* CI;
200
+ const profileName = options.profile ?? (yield* ALCHEMY_PROFILE);
128
201
  const scriptName = options.workerName ?? STATE_STORE_SCRIPT_NAME;
129
202
  const force = options.force ?? false;
203
+ const localStage = `${profileName}_${scriptName}`;
130
204
  yield* Effect.annotateCurrentSpan({
131
205
  "alchemy.state_store.script_name": scriptName,
132
206
  "alchemy.state_store.profile": profileName,
@@ -135,42 +209,41 @@ export const bootstrap = (options: BootstrapOptions = {}) =>
135
209
  });
136
210
  yield* annotateAccountHash();
137
211
 
138
- const localState = yield* makeLocalState();
139
- const hasLocalStack = yield* Effect.map(
140
- localState.listStages("CloudflareStateStore"),
141
- (stages) => stages.includes(scriptName),
142
- );
143
-
144
- if (hasLocalStack) {
212
+ if (yield* hasLocalStack(localStage)) {
213
+ // if there's a local stack still, we can assume we did not finish hoisting it, so finish that
145
214
  yield* Clank.info(
146
215
  `Resuming Cloudflare State Store '${scriptName}' deployment...`,
147
216
  );
148
- yield* finishBootstrap({
217
+ // resume deployment
218
+ return yield* deployWithLocalState({
149
219
  scriptName,
150
220
  profileName,
151
- localState,
152
221
  isCI,
153
222
  force,
154
- });
155
- yield* Clank.success(`Cloudflare State Store '${scriptName}' is ready.`);
156
- return;
157
- }
158
-
159
- const { accountId } = yield* CloudflareEnvironment.CloudflareEnvironment;
160
- const workerExists = yield* workers
161
- .getScriptSetting({ accountId, scriptName })
162
- .pipe(
163
- Effect.map((setting) => setting !== undefined),
164
- Effect.catchTag("WorkerNotFound", () => Effect.succeed(false)),
223
+ }).pipe(
224
+ Effect.tap(() =>
225
+ Clank.success(`Cloudflare State Store '${scriptName}' is ready.`),
226
+ ),
165
227
  );
228
+ }
166
229
 
167
- if (workerExists && !force) {
168
- yield* Clank.info(
169
- `Worker '${scriptName}' already exists; adopting and refreshing credentials. ` +
170
- `Use --force to redeploy.`,
230
+ const workerExists = yield* isStateStoreAvailable(scriptName);
231
+ if (workerExists) {
232
+ // this is a regular update, let's check if it needs an update and refresh credentials
233
+ if (!force) {
234
+ yield* Clank.info(
235
+ `Worker '${scriptName}' already exists; adopting and refreshing credentials. ` +
236
+ `Use --force to redeploy.`,
237
+ );
238
+ }
239
+ const credentials = yield* loginWithCloudflare(
240
+ profileName,
241
+ // force refresh during
242
+ true,
171
243
  );
172
- const credentials = yield* loginWithCloudflare();
244
+ const { url, authToken } = credentials;
173
245
  if (!isCI) {
246
+ // we don't write credentials in CI because the file system is ephemeral
174
247
  const store = yield* CredentialsStore;
175
248
  yield* store.write<HttpStateStoreCredentials>(
176
249
  profileName,
@@ -178,34 +251,44 @@ export const bootstrap = (options: BootstrapOptions = {}) =>
178
251
  credentials,
179
252
  );
180
253
  }
181
- yield* redeployIfStale({
254
+ const { matches, expected, observed } =
255
+ yield* checkStateStoreVersion(url);
256
+ const httpState = yield* makeCloudflareStateStore({ url, authToken });
257
+ if (!matches || force) {
258
+ if (matches && force) {
259
+ yield* Clank.info(
260
+ `Cloudflare State Store '${scriptName}' is up to date; force redeploying...`,
261
+ );
262
+ } else {
263
+ yield* Clank.info(
264
+ `Cloudflare State Store '${scriptName}' is out of date ` +
265
+ `(expected v${expected}, observed v${observed ?? "unknown"}); redeploying...`,
266
+ );
267
+ }
268
+ return yield* makeCloudflareStateStore(
269
+ yield* deployStateStore({
270
+ stage: scriptName,
271
+ state: httpState,
272
+ force,
273
+ }),
274
+ );
275
+ } else {
276
+ return httpState;
277
+ }
278
+ } else {
279
+ // fresh deploy - deploy from local for the first time
280
+ yield* Clank.info(`Deploying Cloudflare State Store '${scriptName}'...`);
281
+ return yield* deployWithLocalState({
182
282
  scriptName,
183
283
  profileName,
184
- localState,
185
284
  isCI,
186
- url: credentials.url,
187
285
  force,
188
- });
189
- yield* Clank.success(`Cloudflare State Store '${scriptName}' is ready.`);
190
- return;
191
- }
192
-
193
- if (workerExists) {
194
- yield* Clank.info(
195
- `Forcing redeploy of Cloudflare State Store '${scriptName}'...`,
286
+ }).pipe(
287
+ Effect.tap(() =>
288
+ Clank.success(`Cloudflare State Store '${scriptName}' is ready.`),
289
+ ),
196
290
  );
197
- } else {
198
- yield* Clank.info(`Deploying Cloudflare State Store '${scriptName}'...`);
199
291
  }
200
-
201
- yield* finishBootstrap({
202
- scriptName,
203
- profileName,
204
- localState,
205
- isCI,
206
- force,
207
- });
208
- yield* Clank.success(`Cloudflare State Store '${scriptName}' is ready.`);
209
292
  }).pipe(
210
293
  Effect.withSpan("state_store.bootstrap", {
211
294
  attributes: {
@@ -216,303 +299,22 @@ export const bootstrap = (options: BootstrapOptions = {}) =>
216
299
  }),
217
300
  );
218
301
 
219
- export const state = () =>
220
- Layer.effect(
221
- State,
222
- Effect.gen(function* () {
223
- const isCI = yield* Config.boolean("CI").pipe(Config.withDefault(false));
224
- const alchemyContext = yield* AlchemyContext;
225
- const profileName = yield* ALCHEMY_PROFILE;
226
- const store = yield* CredentialsStore;
227
- const credentials = yield* store.read<HttpStateStoreCredentials>(
228
- profileName,
229
- CREDENTIALS_FILE,
230
- );
231
-
232
- const scriptName = STATE_STORE_SCRIPT_NAME;
233
- yield* Effect.annotateCurrentSpan({
234
- "alchemy.state_store.script_name": scriptName,
235
- "alchemy.state_store.profile": profileName,
236
- "alchemy.state_store.ci": isCI,
237
- "alchemy.state_store.force": false,
238
- });
239
- yield* annotateAccountHash(
240
- yield* Config.boolean("NO_TRACK").pipe(Config.withDefault(false)),
241
- );
242
-
243
- // The bootstrap of the Cloudflare State Store is only considered
244
- // successful once two invariants hold:
245
- // 1. the worker has been deployed and the credentials persisted
246
- // 2. the local CloudflareStateStore stack has been synced into
247
- // the HTTP store and removed from disk
248
- //
249
- // If a previous run failed anywhere between deploying the worker
250
- // and deleting the local stack (e.g. the worker got created but
251
- // sync/credential-write/delete never ran) we will detect the
252
- // leftover local stack here and resume the bootstrap. This loop
253
- // re-runs deploy (idempotent over already-applied resources) and
254
- // finishes the sync/credential/cleanup steps until both
255
- // invariants are satisfied.
256
- const localState = yield* makeLocalState();
257
-
258
- const hasLocalStack = yield* Effect.map(
259
- localState.listStages("CloudflareStateStore"),
260
- (stages) => stages.includes(scriptName),
261
- );
262
-
263
- if (hasLocalStack) {
264
- yield* Clank.info(
265
- `Resuming Cloudflare State Store '${scriptName}' deployment...`,
266
- );
267
- return yield* finishBootstrap({
268
- scriptName,
269
- profileName,
270
- localState,
271
- isCI,
272
- force: false,
273
- });
274
- }
275
-
276
- if (credentials) {
277
- // We have local credentials. Before trusting them, verify
278
- // the deployed worker is on the version this CLI was built
279
- // against. If it isn't, `redeployIfStale` runs the idempotent
280
- // bootstrap flow and short-circuits with the freshly-deployed
281
- // store.
282
- const fresh = yield* redeployIfStale({
283
- scriptName,
284
- profileName,
285
- localState,
286
- isCI,
287
- url: credentials.url,
288
- force: false,
289
- });
290
- if (fresh) return fresh;
291
-
292
- const httpState = yield* makeCloudflareStateStore(credentials);
293
-
294
- if (alchemyContext.updateStateStore) {
295
- yield* deployStateStore(scriptName, httpState);
296
- }
297
-
298
- return httpState;
299
- }
300
-
301
- // our profile does not contain a reference to the state store, let's try and resolve it
302
- const { accountId } = yield* CloudflareEnvironment.CloudflareEnvironment;
303
- const workerExists = yield* workers
304
- .getScriptSetting({
305
- accountId,
306
- scriptName,
307
- })
308
- .pipe(
309
- Effect.map((setting) => setting !== undefined),
310
- Effect.catchTag("WorkerNotFound", () => Effect.succeed(false)),
311
- );
312
-
313
- if (workerExists) {
314
- // it exists, so fetch the secret token
315
- const credentials = yield* loginWithCloudflare();
316
- if (!isCI) {
317
- yield* store.write<HttpStateStoreCredentials>(
318
- profileName,
319
- CREDENTIALS_FILE,
320
- credentials,
321
- );
322
- }
323
-
324
- const fresh = yield* redeployIfStale({
325
- scriptName,
326
- profileName,
327
- localState,
328
- isCI,
329
- url: credentials.url,
330
- force: false,
331
- });
332
- if (fresh) return fresh;
333
-
334
- const httpState = yield* makeCloudflareStateStore(credentials);
335
-
336
- if (alchemyContext.updateStateStore) {
337
- yield* deployStateStore(scriptName, httpState);
338
- }
339
-
340
- return httpState;
341
- }
342
-
343
- if (isCI) {
344
- // TODO(sam): do we want to support bootstrapping the state store from CI?
345
- // for now - just die here
346
- return yield* Effect.die(
347
- new AuthError({
348
- message: `State store not found for script ${scriptName}. Deploy the state store first.`,
349
- }),
350
- );
351
- }
352
-
353
- const shouldDeploy = yield* Clank.confirm({
354
- message: "Cloudflare State Store not found. Do you want to deploy it?",
355
- });
356
-
357
- if (!shouldDeploy) {
358
- return yield* Effect.die(new Clank.PromptCancelled());
359
- }
360
-
361
- return yield* finishBootstrap({
362
- scriptName,
363
- profileName,
364
- localState,
365
- isCI,
366
- force: false,
367
- });
368
- }).pipe(recordStateStoreInit, Effect.orDie),
369
- ).pipe(
370
- Layer.provideMerge(Credentials.fromAuthProvider()),
371
- Layer.provideMerge(CloudflareEnvironment.fromProfile()),
372
- Layer.provideMerge(CloudflareAuth),
373
- Layer.provideMerge(Access.AccessLive),
374
- Layer.provideMerge(ProfileLive),
375
- Layer.provideMerge(CredentialsStoreLive),
376
- Layer.orDie,
377
- );
378
-
379
- const makeCloudflareStateStore = Effect.fnUntraced(function* ({
380
- url,
381
- authToken,
382
- }: {
383
- url: string;
384
- authToken: string;
385
- }) {
386
- const access = yield* Access.Access;
387
- const accessHeaders = yield* access.getAccessHeaders(new URL(url).host);
388
- return yield* makeHttpStateStore({
389
- url,
390
- authToken,
391
- transformClient: HttpClientRequest.setHeaders(accessHeaders),
392
- id: "cloudflare-http",
393
- });
394
- });
395
-
396
- /**
397
- * Non-destructively copy every resource in the
398
- * `CloudflareStateStore/<scriptName>` stack from `source` into
399
- * `destination`, leaving every other stack in `destination` untouched.
400
- *
401
- * This intentionally does not delete anything from `destination`: at
402
- * bootstrap time the destination is the user's live remote state
403
- * store, and removing entries that happen to be missing locally would
404
- * be catastrophic.
405
- */
406
- const hoistBootstrapStack = Effect.fnUntraced(function* (
407
- source: StateService,
408
- destination: StateService,
409
- scriptName: string,
410
- ) {
411
- const stack = "CloudflareStateStore";
412
- const stage = scriptName;
413
- const fqns = yield* source.list({ stack, stage });
414
- yield* Effect.annotateCurrentSpan({
415
- "alchemy.state_store.stack": stack,
416
- "alchemy.state_store.stage": stage,
417
- "alchemy.state_store.resources.count": fqns.length,
418
- });
419
- yield* Effect.forEach(
420
- fqns,
421
- Effect.fnUntraced(function* (fqn) {
422
- const value = yield* source.get({ stack, stage, fqn });
423
- if (value) {
424
- yield* destination.set({ stack, stage, fqn, value });
425
- }
426
- }),
427
- { concurrency: "unbounded" },
428
- );
429
- }, Effect.withSpan("state_store.hoist_bootstrap_stack"));
430
-
431
- /**
432
- * Finish (or resume) the bootstrap of the Cloudflare State Store using
433
- * the provided local state as the source of truth. This is idempotent
434
- * and safe to re-run: any resources already applied during a previous
435
- * partial run will be reconciled, the local stack will be synced into
436
- * the deployed HTTP store, credentials persisted, and finally the
437
- * local stack removed - which is what marks the bootstrap as complete.
438
- */
439
- const finishBootstrap = ({
440
- scriptName,
441
- profileName,
442
- localState,
443
- isCI,
302
+ const deployStateStore = ({
303
+ stage,
304
+ state,
444
305
  force,
445
306
  }: {
446
- scriptName: string;
447
- profileName: string;
448
- localState: StateService;
449
- isCI: boolean;
450
- force: boolean;
307
+ stage: string;
308
+ state: StateService;
309
+ force?: boolean;
451
310
  }) =>
452
- Effect.gen(function* () {
453
- const { authToken } = yield* deployStateStore(
454
- scriptName,
455
- localState,
456
- force,
457
- );
458
-
459
- // Don't trust the `authToken` returned by `deploy(...)`: when
460
- // adoption kicks in (the Secrets Store secret already existed),
461
- // the locally-generated `Random` value won't match the value
462
- // actually persisted in Cloudflare, and any HTTP call to the
463
- // worker would 401. Re-read the live token from the deployed
464
- // worker via the same edge-preview probe `loginWithCloudflare`
465
- // uses, so the credentials we persist always reflect what is
466
- // actually deployed. `loginWithCloudflare` also persists the
467
- // credentials file (skipping the write in CI), so we don't need
468
- // to do that explicitly here.
469
- const { url } = yield* loginWithCloudflare();
470
- const httpState = yield* makeCloudflareStateStore({ url, authToken });
471
- // `profileName` is intentionally unused here — `loginWithCloudflare`
472
- // resolves it itself. Reference it to keep the surrounding API
473
- // explicit and to avoid an unused-parameter lint.
474
- void profileName;
475
- void isCI;
476
-
477
- // Copy the freshly-deployed CloudflareStateStore stack into the
478
- // remote store. We deliberately do NOT call `syncState` here:
479
- // syncState is a *mirror* that deletes everything in the
480
- // destination that is not in the source, which would wipe every
481
- // user stack in the remote store on any subsequent bootstrap
482
- // (resume / --force / partial-failure recovery), since by that
483
- // point local state only contains the bootstrap stack itself.
484
- yield* hoistBootstrapStack(localState, httpState, scriptName);
485
-
486
- yield* localState.deleteStack({
487
- stack: "CloudflareStateStore",
488
- stage: scriptName,
489
- });
490
-
491
- return httpState;
492
- }).pipe(
493
- Effect.withSpan("state_store.finish_bootstrap", {
494
- attributes: {
495
- "alchemy.state_store.op": "finish_bootstrap",
496
- "alchemy.state_store.script_name": scriptName,
497
- "alchemy.state_store.profile": profileName,
498
- "alchemy.state_store.ci": isCI,
499
- },
500
- }),
501
- );
502
-
503
- const deployStateStore = (
504
- scriptName: string,
505
- state?: StateService,
506
- force?: boolean,
507
- ) =>
508
311
  Effect.gen(function* () {
509
312
  yield* annotateAccountHash();
510
- const localState = state ?? (yield* makeLocalState());
511
313
  // deploy it with local state (which we will then hoist into the Cloudflare state store)
512
- const stateLayer = Layer.succeed(State, localState);
314
+ const stateLayer = Layer.succeed(State, state);
513
315
  const { url, authToken } = yield* deploy({
514
316
  // use the script name as the stage name (so the user can have multiple state stores)
515
- stage: scriptName,
317
+ stage,
516
318
  force,
517
319
  stack: Alchemy.Stack(
518
320
  "CloudflareStateStore",
@@ -557,17 +359,138 @@ const deployStateStore = (
557
359
  // observe stale data or trip the staleness check and recurse into
558
360
  // another redeploy.
559
361
  yield* waitForStateStoreVersion(url);
560
- return { url, authToken, localState };
362
+ return { url, authToken };
561
363
  }).pipe(
562
364
  Effect.withSpan("state_store.deploy", {
563
365
  attributes: {
564
- "alchemy.state_store.script_name": scriptName,
565
366
  "alchemy.state_store.op": "deploy",
566
367
  },
567
368
  }),
568
369
  recordStateStoreOp("deploy"),
569
370
  );
570
371
 
372
+ const deployWithLocalState = ({
373
+ scriptName,
374
+ isCI,
375
+ force,
376
+ profileName,
377
+ }: {
378
+ scriptName: string;
379
+ isCI: boolean;
380
+ force: boolean;
381
+ profileName: string;
382
+ }) =>
383
+ Effect.gen(function* () {
384
+ const localState = yield* makeLocalState();
385
+ const localStage = `${profileName}_${scriptName}`;
386
+ const remoteStage = scriptName;
387
+ const { authToken } = yield* deployStateStore({
388
+ stage: localStage,
389
+ state: localState,
390
+ force,
391
+ });
392
+
393
+ const { url } = yield* loginWithCloudflare(profileName, force);
394
+ const httpState = yield* makeCloudflareStateStore({ url, authToken });
395
+
396
+ yield* hoistBootstrapStack({
397
+ source: {
398
+ state: localState,
399
+ stage: localStage,
400
+ },
401
+ destination: {
402
+ state: httpState,
403
+ stage: remoteStage,
404
+ },
405
+ });
406
+
407
+ yield* localState.deleteStack({
408
+ stack: "CloudflareStateStore",
409
+ stage: localStage,
410
+ });
411
+
412
+ return httpState;
413
+ }).pipe(
414
+ Effect.withSpan("state_store.finish_bootstrap", {
415
+ attributes: {
416
+ "alchemy.state_store.op": "finish_bootstrap",
417
+ "alchemy.state_store.ci": isCI,
418
+ },
419
+ }),
420
+ );
421
+
422
+ // check if there's a local stack that wasn't properly hoisted
423
+ const hasLocalStack = (stage: string) =>
424
+ Effect.gen(function* () {
425
+ const localState = yield* makeLocalState();
426
+ return yield* Effect.map(
427
+ localState.listStages("CloudflareStateStore"),
428
+ // key off the profile name to avoid conflicts with other profiles
429
+ (stages) => stages.includes(stage),
430
+ );
431
+ });
432
+
433
+ /**
434
+ * Non-destructively copy every resource in the
435
+ * `CloudflareStateStore/<scriptName>` stack from `source` into
436
+ * `destination`, leaving every other stack in `destination` untouched.
437
+ *
438
+ * This intentionally does not delete anything from `destination`: at
439
+ * bootstrap time the destination is the user's live remote state
440
+ * store, and removing entries that happen to be missing locally would
441
+ * be catastrophic.
442
+ */
443
+ const hoistBootstrapStack = Effect.fnUntraced(function* ({
444
+ source,
445
+ destination,
446
+ }: {
447
+ source: {
448
+ state: StateService;
449
+ stage: string;
450
+ };
451
+ destination: {
452
+ state: StateService;
453
+ stage: string;
454
+ };
455
+ }) {
456
+ const stack = "CloudflareStateStore";
457
+ const fqns = yield* source.state.list({ stack, stage: source.stage });
458
+ yield* Effect.annotateCurrentSpan({
459
+ "alchemy.state_store.stack": stack,
460
+ "alchemy.state_store.stage": source.stage,
461
+ "alchemy.state_store.resources.count": fqns.length,
462
+ });
463
+ yield* Effect.forEach(
464
+ fqns,
465
+ Effect.fnUntraced(function* (fqn) {
466
+ const value = yield* source.state.get({
467
+ stack,
468
+ stage: source.stage,
469
+ fqn,
470
+ });
471
+ if (value) {
472
+ yield* destination.state
473
+ .set({
474
+ stack,
475
+ stage: destination.stage,
476
+ fqn,
477
+ value,
478
+ })
479
+ .pipe(
480
+ Effect.retry({
481
+ while: (error) =>
482
+ isHttpClientError(error.cause) &&
483
+ // worker can 404 for a bit on first deploy, retry these
484
+ error.cause.response?.status === 404,
485
+ schedule: Schedule.fixed(200),
486
+ }),
487
+ );
488
+ }
489
+ }),
490
+ { concurrency: "unbounded" },
491
+ );
492
+ }, Effect.withSpan("state_store.hoist_bootstrap_stack"));
493
+
571
494
  /**
572
495
  * Log in to a Cloudflare-deployed HTTP state-store.
573
496
  *
@@ -584,12 +507,23 @@ const deployStateStore = (
584
507
  * `CloudflareEnvironment`, `Credentials`, `HttpClient`, and
585
508
  * `FileSystem`.
586
509
  */
587
- export const loginWithCloudflare = () =>
510
+ export const loginWithCloudflare = (profileName: string, force: boolean) =>
588
511
  Effect.gen(function* () {
589
- const isCI = yield* Config.boolean("CI").pipe(Config.withDefault(false));
590
- const profileName = yield* ALCHEMY_PROFILE;
512
+ const credStore = yield* CredentialsStore;
513
+ const isCI = yield* CI;
591
514
  const { accountId } = yield* CloudflareEnvironment.CloudflareEnvironment;
592
515
 
516
+ if (!force) {
517
+ // try and read from the cached credentials first if not forcing (force will always refresh)
518
+ const credentials = yield* credStore.read<HttpStateStoreCredentials>(
519
+ profileName,
520
+ CREDENTIALS_FILE,
521
+ );
522
+ if (credentials) {
523
+ return credentials;
524
+ }
525
+ }
526
+
593
527
  // 1. Locate the single Secrets Store on the account.
594
528
  const stores = yield* SecretsStore.listStores({ accountId });
595
529
  const store = stores.result[0];
@@ -602,15 +536,7 @@ export const loginWithCloudflare = () =>
602
536
  );
603
537
  }
604
538
 
605
- // 2. Fetch the auth-token value via an edge-preview worker.
606
- // We piggy-back on the already-deployed state-store script so the
607
- // `cf-workers-preview-token` header has a real workers.dev route to
608
- // swap onto. Uploading the probe under a brand-new script name fails
609
- // on accounts where that script has never been deployed (or where
610
- // workers.dev preview URLs are off by default — the post-2024
611
- // Cloudflare default for new accounts) because the host doesn't
612
- // resolve and Cloudflare's edge serves a 400 HTML error page
613
- // instead of routing to the preview.
539
+ // 2. Fetch the auth-token from Secrets Store with a temporary edge-preview worker
614
540
  const authToken = yield* readSecretViaEdge(
615
541
  STATE_STORE_SCRIPT_NAME,
616
542
  store.id,
@@ -631,7 +557,6 @@ export const loginWithCloudflare = () =>
631
557
  if (!isCI) {
632
558
  // 4. Persist credentials. The profile entry is managed by
633
559
  // `loadOrConfigure` when this is invoked through `configure`.
634
- const credStore = yield* CredentialsStore;
635
560
  yield* credStore
636
561
  .write<HttpStateStoreCredentials>(profileName, CREDENTIALS_FILE, {
637
562
  url,
@@ -674,96 +599,33 @@ export const loginWithCloudflare = () =>
674
599
  }),
675
600
  );
676
601
 
677
- const writeCredentials = (url: string, authToken: string) =>
602
+ const isStateStoreAvailable = (scriptName: string = "alchemy-state-store") =>
678
603
  Effect.gen(function* () {
679
- const profileName = yield* ALCHEMY_PROFILE;
680
- const credStore = yield* CredentialsStore;
681
- yield* credStore.write<HttpStateStoreCredentials>(
682
- profileName,
683
- CREDENTIALS_FILE,
684
- {
685
- url,
686
- authToken,
687
- },
604
+ // otherwise, the remote one might exist
605
+ const { accountId } = yield* CloudflareEnvironment.CloudflareEnvironment;
606
+ return yield* workers.getScriptSetting({ accountId, scriptName }).pipe(
607
+ Effect.map((setting) => setting !== undefined),
608
+ Effect.catchTag("WorkerNotFound", () => Effect.succeed(false)),
688
609
  );
689
610
  });
690
611
 
691
- const isWorkersPreviewConfigurationError = (error: unknown) =>
692
- error instanceof EdgeSessionError &&
693
- (error.message.includes("Invalid Workers Preview configuration") ||
694
- error.message.includes("Error 1031"));
695
-
696
- /**
697
- * Run {@link finishBootstrap} iff the worker at `url` fails the
698
- * version probe; otherwise resolve to `undefined`. Centralises the
699
- * "is the deployed worker still compatible?" guard used by every
700
- * code path that already has a presumably-valid worker URL.
701
- */
702
- const redeployIfStale = ({
612
+ const makeCloudflareStateStore = Effect.fnUntraced(function* ({
703
613
  url,
704
- scriptName,
705
- profileName,
706
- localState,
707
- isCI,
708
- force,
614
+ authToken,
709
615
  }: {
710
616
  url: string;
711
- scriptName: string;
712
- profileName: string;
713
- localState: StateService;
714
- isCI: boolean;
715
- force: boolean;
716
- }) =>
717
- Effect.gen(function* () {
718
- const { matches, expected, observed } = yield* checkStateStoreVersion(url);
719
- if (matches) return undefined;
720
- yield* Clank.info(
721
- `Cloudflare State Store '${scriptName}' is out of date ` +
722
- `(expected v${expected}, observed v${observed ?? "unknown"}); redeploying...`,
723
- );
724
- return yield* finishBootstrap({
725
- scriptName,
726
- profileName,
727
- localState,
728
- isCI,
729
- force,
730
- });
731
- }).pipe(
732
- Effect.withSpan("state_store.redeploy_if_stale", {
733
- attributes: {
734
- "alchemy.state_store.op": "redeploy_if_stale",
735
- "alchemy.state_store.script_name": scriptName,
736
- "alchemy.state_store.url": url,
737
- },
738
- }),
739
- );
617
+ authToken: string;
618
+ }) {
619
+ const access = yield* Access.Access;
620
+ const accessHeaders = yield* access.getAccessHeaders(new URL(url).host);
621
+ return yield* makeHttpStateStore({
622
+ url,
623
+ authToken,
624
+ transformClient: HttpClientRequest.setHeaders(accessHeaders),
625
+ id: "cloudflare-http",
626
+ });
627
+ });
740
628
 
741
- /**
742
- * Probe the deployed worker's `/version` endpoint and decide whether
743
- * it satisfies the current {@link STATE_STORE_VERSION} contract.
744
- *
745
- * Reuses the same typed {@link StateApi} client the rest of the store
746
- * speaks, just without the bearer-token transform — the `/version`
747
- * route is intentionally unauthenticated so we can probe it before
748
- * trusting any locally-cached credentials.
749
- *
750
- * Returns `true` only on a successful response carrying the matching
751
- * version. Any failure (404 from a pre-`/version` deploy, transport
752
- * error, schema mismatch, version mismatch) collapses to `false`,
753
- * since the caller's response in every case is the same: fall
754
- * through to the idempotent bootstrap flow.
755
- */
756
- /**
757
- * Block until the worker at `url` reports the {@link STATE_STORE_VERSION}
758
- * this CLI was built against, with bounded exponential retry. Used
759
- * post-deploy to wait out Cloudflare's edge cache so any subsequent
760
- * read or write goes to the new worker.
761
- *
762
- * Failures (transport errors, 404 from a pre-`/version` build, schema
763
- * mismatch, version mismatch) all collapse to "not ready yet" and are
764
- * retried; once the budget is exhausted we surface a hard failure
765
- * since continuing would talk to the wrong worker.
766
- */
767
629
  class StateStoreVersionNotReady extends Error {
768
630
  readonly _tag = "StateStoreVersionNotReady";
769
631
  constructor(
@@ -806,6 +668,9 @@ const waitForStateStoreVersion = (url: string) =>
806
668
  const checkStateStoreVersion = (url: string) =>
807
669
  Effect.gen(function* () {
808
670
  const client = yield* HttpApiClient.make(StateApi, { baseUrl: url });
671
+ const isAvailable = yield* Effect.cached(
672
+ isStateStoreAvailable(STATE_STORE_SCRIPT_NAME),
673
+ );
809
674
  // The /version route may 404 transiently after a fresh deploy
810
675
  // while Cloudflare propagates the new script to the edge, and may
811
676
  // also surface transport-level blips on cold workers.dev hosts.
@@ -813,6 +678,19 @@ const checkStateStoreVersion = (url: string) =>
813
678
  // exhausting that budget do we collapse to `undefined` and let
814
679
  // the caller treat it as a version mismatch.
815
680
  const result = yield* client.version.getVersion().pipe(
681
+ Effect.catchTag("HttpClientError", (e) =>
682
+ // if we get a 404 here, it means we assumed the worker shoudl exist, but it does not
683
+ // we should do a check to see if it does
684
+ e.response?.status === 404
685
+ ? isAvailable.pipe(
686
+ Effect.flatMap((isAvailable) =>
687
+ // if the worker is available, then we should assume it was recently created and retry by propagating the error
688
+ // otherwise, return undefined (we don't know the version, there is no worker)
689
+ isAvailable ? Effect.fail(e) : Effect.succeed(undefined),
690
+ ),
691
+ )
692
+ : Effect.fail(e),
693
+ ),
816
694
  Effect.retry({
817
695
  schedule: Schedule.spaced("250 millis").pipe(
818
696
  Schedule.both(Schedule.recurs(40)),
@@ -920,3 +798,63 @@ const readSecretViaEdge = (
920
798
  },
921
799
  }),
922
800
  );
801
+
802
+ const writeCredentials = (url: string, authToken: string) =>
803
+ Effect.gen(function* () {
804
+ const profileName = yield* ALCHEMY_PROFILE;
805
+ const credStore = yield* CredentialsStore;
806
+ yield* credStore.write<HttpStateStoreCredentials>(
807
+ profileName,
808
+ CREDENTIALS_FILE,
809
+ {
810
+ url,
811
+ authToken,
812
+ },
813
+ );
814
+ });
815
+
816
+ const isWorkersPreviewConfigurationError = (error: unknown) =>
817
+ error instanceof EdgeSessionError &&
818
+ (error.message.includes("Invalid Workers Preview configuration") ||
819
+ error.message.includes("Error 1031"));
820
+
821
+ /**
822
+ * SHA-256 hex digest of the Cloudflare account ID. Used as a stable
823
+ * pseudonymous identifier on telemetry spans so the dashboard can
824
+ * count distinct state-store deployments without leaking the raw
825
+ * accountId. Mirrors the `alchemy.git.origin_hash` pattern in
826
+ * `Telemetry/Attributes.ts`.
827
+ */
828
+ const hashAccountId = (accountId: string) =>
829
+ Effect.sync(() =>
830
+ crypto.createHash("sha256").update(accountId).digest("hex"),
831
+ );
832
+
833
+ /**
834
+ * Best-effort Cloudflare-account-hash annotation on the current span.
835
+ * Resolves the accountId from {@link CloudflareEnvironment} and
836
+ * attaches `alchemy.cloudflare.account_hash` to whichever span is
837
+ * active. Silently no-ops if the environment isn't resolvable so
838
+ * State-store layer construction still succeeds in degraded paths.
839
+ *
840
+ * `noTrack` controls whether the hash is attached:
841
+ * - `true` — never annotate (caller-level opt-out).
842
+ * - `false` — always annotate, regardless of env.
843
+ * - `undefined` — fall back to the `NO_TRACK` env var; default off.
844
+ */
845
+ const annotateAccountHash = (noTrack?: boolean) =>
846
+ Effect.gen(function* () {
847
+ if (noTrack === true) return;
848
+ if (noTrack === undefined) {
849
+ const fromEnv = yield* Config.boolean("NO_TRACK").pipe(
850
+ Config.withDefault(false),
851
+ );
852
+ if (fromEnv) return;
853
+ }
854
+ const env = yield* Effect.serviceOption(
855
+ CloudflareEnvironment.CloudflareEnvironment,
856
+ );
857
+ if (env._tag !== "Some") return;
858
+ const hash = yield* hashAccountId(env.value.accountId);
859
+ yield* Effect.annotateCurrentSpan("alchemy.cloudflare.account_hash", hash);
860
+ }).pipe(Effect.catch(() => Effect.void));