deepline 0.3.530 → 0.3.532
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.
- package/dist/bundling-sources/sdk/src/release.ts +1 -1
- package/dist/bundling-sources/shared_libs/monitors/fleets/limits.ts +6 -0
- package/dist/bundling-sources/shared_libs/play-data-plane/r2.ts +10 -8
- package/dist/bundling-sources/shared_libs/play-runtime/context.ts +95 -9
- package/dist/bundling-sources/shared_libs/play-runtime/ctx-types.ts +7 -0
- package/dist/bundling-sources/shared_libs/play-runtime/deployment/commands/roll-execution-relay.ts +139 -35
- package/dist/bundling-sources/shared_libs/play-runtime/deployment/commands/roll-receipt-gateway.ts +438 -38
- package/dist/bundling-sources/shared_libs/play-runtime/deployment/commands/runtime-topology.ts +17 -0
- package/dist/bundling-sources/shared_libs/play-runtime/deployment/fly-machine-lifecycle.ts +2 -0
- package/dist/bundling-sources/shared_libs/play-runtime/deployment/fly-transient-retry.ts +279 -20
- package/dist/bundling-sources/shared_libs/play-runtime/durable-summary-events.ts +615 -0
- package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/absurd-runtime-worker.ts +16 -1
- package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/postgres-progress.ts +26 -8
- package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/postgres.ts +27 -0
- package/dist/bundling-sources/shared_libs/play-runtime/tool-call-retry-facts.ts +317 -0
- package/dist/bundling-sources/shared_libs/play-runtime/tool-execute-retry-policy.ts +129 -39
- package/dist/cli/auth-7YWTPQSQ.js +43 -0
- package/dist/cli/{auth-LRWNDQFT.mjs → auth-ZB3NN4E7.mjs} +5 -5
- package/dist/cli/{billing-5FDPWGCG.js → billing-32EF4HJM.js} +82 -82
- package/dist/cli/{billing-7T73MRYF.mjs → billing-5GGO7DQS.mjs} +7 -7
- package/dist/cli/{chunk-7WGMCOM6.js → chunk-2T7YE5UH.js} +2 -2
- package/dist/cli/{chunk-L4QR7CYL.js → chunk-2ZGZBN6D.js} +14 -14
- package/dist/cli/{chunk-J6XSBTEX.mjs → chunk-3NUYO5QO.mjs} +1 -1
- package/dist/cli/{chunk-MVF7ZBJX.mjs → chunk-47UF3DDN.mjs} +1 -1
- package/dist/cli/{chunk-2FLODRM2.js → chunk-4S3ONSLF.js} +13 -13
- package/dist/cli/{chunk-4WPIORJX.js → chunk-64CEFPF4.js} +1 -1
- package/dist/cli/{chunk-QI2LKPW5.mjs → chunk-6LBD6G6U.mjs} +2 -2
- package/dist/cli/{chunk-WWLIDAVT.js → chunk-7S3DJFW5.js} +11 -11
- package/dist/cli/{chunk-BVY4QRVT.mjs → chunk-AOAXIJL2.mjs} +82 -31
- package/dist/cli/{chunk-KUHH7RW4.js → chunk-AQHMHYXX.js} +44 -44
- package/dist/cli/{chunk-WAWOMHWO.mjs → chunk-B77ZRM6M.mjs} +2 -2
- package/dist/cli/{chunk-5B37ER3C.js → chunk-BQ2KAFVQ.js} +3 -3
- package/dist/cli/{chunk-VQMGCNX2.mjs → chunk-C6VCDDDD.mjs} +5 -5
- package/dist/cli/{chunk-3MAHDV5C.mjs → chunk-CRWX26LR.mjs} +1 -1
- package/dist/cli/{chunk-7E7CP5KA.js → chunk-CSMLW5VE.js} +4 -4
- package/dist/cli/{chunk-HCWPX2UP.mjs → chunk-DSNTHXR7.mjs} +2 -2
- package/dist/cli/{chunk-4VQ63Y6S.mjs → chunk-EJLCIQA4.mjs} +5 -5
- package/dist/cli/{chunk-7FCD34YS.mjs → chunk-EVXRQXNV.mjs} +3 -2
- package/dist/cli/{chunk-K4Y6CNFV.mjs → chunk-F2YLC4ZG.mjs} +1 -1
- package/dist/cli/{chunk-YI7QOP6L.js → chunk-FU4MMUGM.js} +16 -16
- package/dist/cli/{chunk-6NG3FRJB.mjs → chunk-GABQIZJR.mjs} +5 -5
- package/dist/cli/{chunk-V4EK7ZZX.js → chunk-GBVTSWL3.js} +98 -98
- package/dist/cli/{chunk-WYI3VMTO.js → chunk-H6OYTU2Y.js} +6 -6
- package/dist/cli/{chunk-C44RZGM5.js → chunk-HR2RDDAC.js} +26 -26
- package/dist/cli/{chunk-XNWS2WJG.mjs → chunk-HREGFD2I.mjs} +1 -1
- package/dist/cli/{chunk-GW76IDID.js → chunk-JJSJ47CA.js} +22 -22
- package/dist/cli/{chunk-32NTC5UD.js → chunk-KEE57BF2.js} +7 -7
- package/dist/cli/{chunk-SJQ7Z3NI.js → chunk-KQW27FSR.js} +132 -81
- package/dist/cli/{chunk-D7Y7I4EE.js → chunk-L5V5SLI7.js} +2 -2
- package/dist/cli/{chunk-VKVLJTW5.js → chunk-MHTKWZM3.js} +9 -9
- package/dist/cli/{chunk-EADHDZW2.js → chunk-NBIDYAN6.js} +6 -6
- package/dist/cli/{chunk-E6L3NEFV.js → chunk-NQDUXVBK.js} +41 -41
- package/dist/cli/{chunk-QWEPCX6L.mjs → chunk-O2A4C4WP.mjs} +2 -2
- package/dist/cli/{chunk-KEXGBXUG.js → chunk-O7JU5GUR.js} +5 -5
- package/dist/cli/{chunk-MVFKZQ7E.js → chunk-QZINISXP.js} +3 -3
- package/dist/cli/{chunk-YVLPGYRS.mjs → chunk-RRCSFOF3.mjs} +1 -1
- package/dist/cli/{chunk-A5MYJM4K.mjs → chunk-S2HHY26V.mjs} +2 -2
- package/dist/cli/{chunk-M4Q4LEMM.js → chunk-SMDX4BM5.js} +2 -2
- package/dist/cli/{chunk-5KX3TWBK.mjs → chunk-T2QY36E3.mjs} +1 -1
- package/dist/cli/{chunk-OUM2V7R6.mjs → chunk-TFWREZYG.mjs} +6 -6
- package/dist/cli/{chunk-QQ4LNSY5.mjs → chunk-THQPLSJD.mjs} +1 -1
- package/dist/cli/{chunk-MHR3XVJD.js → chunk-TS6XF2QP.js} +5 -5
- package/dist/cli/{chunk-WL6GAZBD.js → chunk-V6LUMGVI.js} +3 -2
- package/dist/cli/{chunk-5DJUNUHN.mjs → chunk-VCRCMK7M.mjs} +5 -5
- package/dist/cli/{chunk-VYSQ76RP.js → chunk-VHHD5D44.js} +7 -7
- package/dist/cli/{chunk-KI63L5H3.mjs → chunk-VIC2MI4W.mjs} +2 -2
- package/dist/cli/{chunk-ERMDXXP5.mjs → chunk-VN432I4S.mjs} +1 -1
- package/dist/cli/{chunk-LNZILMVK.mjs → chunk-VWEDIFHT.mjs} +4 -4
- package/dist/cli/{chunk-I2YXEHDO.mjs → chunk-VZKCZO4J.mjs} +3 -3
- package/dist/cli/{chunk-2LKVNWRA.mjs → chunk-W5KSUNTE.mjs} +10 -10
- package/dist/cli/{chunk-X547OW3I.js → chunk-WU33X2QL.js} +16 -16
- package/dist/cli/{chunk-2FLRKNME.mjs → chunk-YM6JAKYP.mjs} +2 -2
- package/dist/cli/command-docs.js +2 -2
- package/dist/cli/command-docs.mjs +1 -1
- package/dist/cli/{csv-EFIEDJA2.js → csv-4JBCPMEH.js} +13 -13
- package/dist/cli/{csv-VY2S7KKV.mjs → csv-LM44ZFL3.mjs} +4 -4
- package/dist/cli/{db-D32CBDTN.mjs → db-IFJEGJ6M.mjs} +6 -6
- package/dist/cli/{db-3DLI5TYW.js → db-UVNXGK6L.js} +27 -27
- package/dist/cli/describe-AF73GGH6.js +50 -0
- package/dist/cli/{describe-6MYKK2P3.mjs → describe-TWTOPGII.mjs} +18 -18
- package/dist/cli/{executions-QG3DEEY3.js → executions-3I6RU7AO.js} +8 -8
- package/dist/cli/{executions-U6TPX4RX.mjs → executions-NEV2P5MX.mjs} +6 -6
- package/dist/cli/{feedback-YZ6H4VMQ.mjs → feedback-IO5ZAWKK.mjs} +6 -6
- package/dist/cli/{feedback-ZSPW6NXL.js → feedback-NHO56EWZ.js} +14 -14
- package/dist/cli/git-AOKC6WL3.js +25 -0
- package/dist/cli/{git-ENN72IK4.mjs → git-MQISNZ5F.mjs} +5 -5
- package/dist/cli/index.js +145 -145
- package/dist/cli/index.mjs +44 -44
- package/dist/cli/{legacy-noops-MNA6LWJU.mjs → legacy-noops-DNRJHNQB.mjs} +6 -6
- package/dist/cli/{legacy-noops-Q6EVYIGN.js → legacy-noops-SLAQE7HN.js} +9 -9
- package/dist/cli/{monitors-BUH54HXJ.js → monitors-JRNATYS2.js} +100 -100
- package/dist/cli/{monitors-KENWEEWC.mjs → monitors-NAX7SUAR.mjs} +7 -7
- package/dist/cli/{org-HWOHKYP3.mjs → org-34VCNPUD.mjs} +6 -6
- package/dist/cli/{org-AHWQJMV7.js → org-BHOREX5D.js} +86 -86
- package/dist/cli/{pages-Z5R7NYEF.js → pages-GB337AQG.js} +18 -18
- package/dist/cli/{pages-I7DYTCW6.mjs → pages-ZWVHNJOE.mjs} +6 -6
- package/dist/cli/{play-3LXZB4ED.js → play-DAUQ7OAO.js} +317 -317
- package/dist/cli/{play-ET7CRVWJ.mjs → play-GJ7OEYSB.mjs} +18 -18
- package/dist/cli/{providers-GZSAZVHZ.mjs → providers-KA2RKD6E.mjs} +6 -6
- package/dist/cli/{providers-ZV2E7NGX.js → providers-W7QY5FUK.js} +9 -9
- package/dist/cli/{quickstart-5746NBS2.mjs → quickstart-7KOAL3BD.mjs} +4 -4
- package/dist/cli/{quickstart-VJZAT4RY.js → quickstart-D4DPUR7R.js} +6 -6
- package/dist/cli/{run-WOQ6XMTT.mjs → run-AYJSQED7.mjs} +23 -23
- package/dist/cli/{run-WGUAVYZC.js → run-QFVN2WUQ.js} +41 -41
- package/dist/cli/search-JVCK5AB4.js +33 -0
- package/dist/cli/{search-NNPFJYEW.mjs → search-MEE7OV2B.mjs} +8 -8
- package/dist/cli/{search-output-65HDEEFS.mjs → search-output-G36JH6D6.mjs} +4 -4
- package/dist/cli/{search-output-K4ISMPKD.js → search-output-OW6UPOI6.js} +6 -6
- package/dist/cli/{secrets-NBKGWMF4.js → secrets-CJJQJUJC.js} +15 -15
- package/dist/cli/{secrets-RK44OV3X.mjs → secrets-OFNYIJT6.mjs} +6 -6
- package/dist/cli/{sessions-ULQJJN3J.mjs → sessions-B6VCVU4Z.mjs} +6 -6
- package/dist/cli/sessions-DXN6BQ7Q.js +22 -0
- package/dist/cli/{settings-IHNA7X3Y.js → settings-H4Q524LF.js} +86 -86
- package/dist/cli/{settings-V5ZRL2EF.mjs → settings-PPFLSXDP.mjs} +13 -13
- package/dist/cli/{setup-HTEBFMOQ.js → setup-SJAM7XHR.js} +78 -63
- package/dist/cli/{setup-54VZPJGM.mjs → setup-WI3WDRRE.mjs} +28 -13
- package/dist/cli/skills-6MNJK2VQ.js +37 -0
- package/dist/cli/{skills-3FCPWMHX.mjs → skills-QSRYGMG2.mjs} +8 -8
- package/dist/cli/tools-OT43MSZE.js +57 -0
- package/dist/cli/{tools-2NTDNUQT.mjs → tools-RUC4ULAD.mjs} +17 -17
- package/dist/cli/{update-25MTBTGU.mjs → update-DPWNL2UB.mjs} +10 -10
- package/dist/cli/update-PHOSO3TL.js +35 -0
- package/dist/cli/{views-37K4JNNS.js → views-CVDVQALA.js} +18 -18
- package/dist/cli/{views-OBE5LUSW.mjs → views-JP66QRA3.mjs} +6 -6
- package/dist/cli/{workflow-6YTIHHJH.js → workflow-JM6JUI7I.js} +17 -17
- package/dist/cli/{workflow-HAPCL4K6.mjs → workflow-MVTX2PY3.mjs} +7 -7
- package/dist/index.js +9 -9
- package/dist/index.mjs +6 -6
- package/dist/install-integrity.json +108 -106
- package/dist/release.d.mts +1 -1
- package/dist/release.d.ts +1 -1
- package/dist/release.js +2 -2
- package/dist/release.mjs +1 -1
- package/dist/server.js +5 -5
- package/dist/server.mjs +3 -3
- package/package.json +1 -1
- package/dist/cli/auth-QFIXECQV.js +0 -43
- package/dist/cli/describe-U6HM52X6.js +0 -50
- package/dist/cli/git-OBV73OVY.js +0 -25
- package/dist/cli/search-IGHNMXI4.js +0 -33
- package/dist/cli/sessions-ZJNV4SEL.js +0 -22
- package/dist/cli/skills-ZCJ6FOOK.js +0 -37
- package/dist/cli/tools-Q75XC2BS.js +0 -57
- package/dist/cli/update-3G3GO7IE.js +0 -35
|
@@ -12,8 +12,10 @@
|
|
|
12
12
|
* Rules:
|
|
13
13
|
* - Retry only an operation that converges when repeated: reads, cordon,
|
|
14
14
|
* uncordon, start, stop, metadata writes, and staged secret writes of the
|
|
15
|
-
* same values. A create is never repeated blindly
|
|
16
|
-
*
|
|
15
|
+
* same values. A create is never repeated blindly: every create is preceded
|
|
16
|
+
* by a read that shows its identity absent, and an ambiguous create is
|
|
17
|
+
* followed by a bounded read-back window before another one may be issued
|
|
18
|
+
* (see `createWithReadBeforeRetry`).
|
|
17
19
|
* - Retry only a failure classified as transient from structured data (HTTP
|
|
18
20
|
* status, Node/Bun error code, subprocess exit status) or from the stable
|
|
19
21
|
* transport text flyctl prints (`FLY_TRANSIENT_OUTPUT_PATTERNS`). Auth,
|
|
@@ -24,6 +26,7 @@
|
|
|
24
26
|
* - A subprocess killed by a signal or by its own deadline (exit 124 or
|
|
25
27
|
* >= 128) may have applied its effect; only a read may repeat it.
|
|
26
28
|
*/
|
|
29
|
+
import { randomBytes } from 'node:crypto';
|
|
27
30
|
|
|
28
31
|
export const FLY_TRANSIENT_RETRY_ATTEMPTS = 4;
|
|
29
32
|
export const FLY_TRANSIENT_RETRY_BASE_DELAY_MS = 1_000;
|
|
@@ -510,28 +513,284 @@ export async function runFlyctlWithTransientRetry(
|
|
|
510
513
|
}
|
|
511
514
|
|
|
512
515
|
/**
|
|
513
|
-
*
|
|
514
|
-
*
|
|
515
|
-
|
|
516
|
-
|
|
516
|
+
* Fly Machine metadata key that carries the nonce of the create request that
|
|
517
|
+
* made the Machine. Diagnostic identity only: nothing selects on it.
|
|
518
|
+
*/
|
|
519
|
+
export const FLY_CREATE_NONCE_METADATA_KEY = 'deepline_create_attempt_nonce';
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* One identity may see at most this many create requests end ambiguously:
|
|
523
|
+
* the first and one retry. Requests Fly answered with a rate limit are
|
|
524
|
+
* counted by `FLY_TRANSIENT_RETRY_ATTEMPTS` instead.
|
|
525
|
+
*/
|
|
526
|
+
export const FLY_CREATE_ATTEMPTS = 2;
|
|
527
|
+
export const FLY_CREATE_READBACK_POLL_MS = 5_000;
|
|
528
|
+
/**
|
|
529
|
+
* How long an identity must stay absent from provider state, after an
|
|
530
|
+
* ambiguous create, before another create may be issued for it.
|
|
531
|
+
*
|
|
532
|
+
* Aborting a request client-side does not cancel it at Fly. In Production
|
|
533
|
+
* release run 43649841489 (2026-11-14) `POST /machines` for
|
|
534
|
+
* `receipt-12f77d159ca8-2` hit the 30 s client timeout, one inventory read
|
|
535
|
+
* 1.1 s later did not contain a Machine with that name, a second create was
|
|
536
|
+
* issued, and within about 35 s of the abort Fly listed two Machines with
|
|
537
|
+
* that name. Whether the first create committed late or the list lagged, a
|
|
538
|
+
* single read moments after a timeout proves nothing.
|
|
539
|
+
*
|
|
540
|
+
* 90 s is about 2.5 times that observed worst case. Together with the 30 s
|
|
541
|
+
* request timeout, a create has two minutes from the moment it was issued to
|
|
542
|
+
* become visible before it is treated as never having happened. Fly records
|
|
543
|
+
* no upper bound for this, so a later arrival is still possible; the callers'
|
|
544
|
+
* readiness waits detect that surplus instead of assuming it away.
|
|
545
|
+
*/
|
|
546
|
+
export const FLY_CREATE_ABSENCE_WINDOW_MS = 90_000;
|
|
547
|
+
|
|
548
|
+
export type FlyCreateAttempt = Readonly<{
|
|
549
|
+
/** 1-based create attempt for this identity in this process. */
|
|
550
|
+
number: number;
|
|
551
|
+
/**
|
|
552
|
+
* Unique per create request. The caller persists it on the resource (Fly
|
|
553
|
+
* Machine metadata) so provider state names the request that made it.
|
|
554
|
+
*/
|
|
555
|
+
nonce: string;
|
|
556
|
+
}>;
|
|
557
|
+
|
|
558
|
+
export type FlyCreateResult<T> = Readonly<{
|
|
559
|
+
/** `adopted`: provider state already held the identity; nothing was created. */
|
|
560
|
+
outcome: 'created' | 'adopted';
|
|
561
|
+
/**
|
|
562
|
+
* The resource(s) carrying the identity. More than one adopted resource is
|
|
563
|
+
* a surplus an earlier ambiguous create left behind; the caller's
|
|
564
|
+
* readiness path must reconcile or refuse it.
|
|
565
|
+
*/
|
|
566
|
+
resources: readonly T[];
|
|
567
|
+
}>;
|
|
568
|
+
|
|
569
|
+
export type FlyCreateReadBackOptions = Readonly<{
|
|
570
|
+
/** Create requests that may end ambiguously. */
|
|
571
|
+
attempts?: number;
|
|
572
|
+
/** Create requests in total, rate-limited ones included. */
|
|
573
|
+
requests?: number;
|
|
574
|
+
/** Total time that may be spent waiting out rate limits. */
|
|
575
|
+
rateLimitDeadlineMs?: number;
|
|
576
|
+
random?: () => number;
|
|
577
|
+
absenceWindowMs?: number;
|
|
578
|
+
pollMs?: number;
|
|
579
|
+
now?: () => number;
|
|
580
|
+
sleep?: (ms: number) => Promise<void>;
|
|
581
|
+
log?: (line: string) => void;
|
|
582
|
+
nonce?: () => string;
|
|
583
|
+
}>;
|
|
584
|
+
|
|
585
|
+
/**
|
|
586
|
+
* An ambiguous create could be neither confirmed nor ruled out inside its
|
|
587
|
+
* bounds. Nothing further was created; the identity may or may not exist.
|
|
588
|
+
*/
|
|
589
|
+
export class FlyAmbiguousCreateError extends Error {
|
|
590
|
+
readonly identity: string;
|
|
591
|
+
readonly nonces: readonly string[];
|
|
592
|
+
constructor(input: {
|
|
593
|
+
identity: string;
|
|
594
|
+
nonces: readonly string[];
|
|
595
|
+
detail: string;
|
|
596
|
+
cause: unknown;
|
|
597
|
+
}) {
|
|
598
|
+
super(
|
|
599
|
+
`Create ${input.identity} is unresolved: ${input.detail} No further create was issued (attempt nonces: ${input.nonces.join(', ')}). Inspect provider state for this identity before retrying.`,
|
|
600
|
+
{ cause: input.cause },
|
|
601
|
+
);
|
|
602
|
+
this.name = 'FlyAmbiguousCreateError';
|
|
603
|
+
this.identity = input.identity;
|
|
604
|
+
this.nonces = input.nonces;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/**
|
|
609
|
+
* Fly answered `POST` with HTTP 429: the request was refused before it took
|
|
610
|
+
* effect. Unlike a timeout or a 5xx, this outcome is not ambiguous.
|
|
611
|
+
*/
|
|
612
|
+
export function isFlyRateLimitRefusal(error: unknown): boolean {
|
|
613
|
+
return error instanceof FlyTransientHttpError && error.status === 429;
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
function errorDetail(error: unknown): string {
|
|
617
|
+
return error instanceof Error ? error.message : String(error);
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/**
|
|
621
|
+
* Create a provider resource that Fly does not deduplicate for us. The
|
|
622
|
+
* Machines client sends `POST /machines` with no idempotency key, and the
|
|
623
|
+
* incident above shows Fly accepting a second Machine with the same name. So
|
|
624
|
+
* the identity travels inside the request (a deterministic name, the
|
|
625
|
+
* operation id, and this attempt's nonce in Machine metadata) and provider
|
|
626
|
+
* state is the only record of whether a request took effect.
|
|
627
|
+
*
|
|
628
|
+
* - Before the first create, read. An identity that already exists is adopted.
|
|
629
|
+
* - A failure Fly answered with a non-transient status is rethrown unchanged.
|
|
630
|
+
* - HTTP 429 is also an answer: Fly refused the request and created nothing.
|
|
631
|
+
* It is sent again after the shared backoff (or `Retry-After`, when
|
|
632
|
+
* longer) with no read-back window, within `requests` requests in total
|
|
633
|
+
* and `rateLimitDeadlineMs` of waiting.
|
|
634
|
+
* - Every other transient failure is ambiguous: a client timeout, a dropped
|
|
635
|
+
* connection, and also a 5xx. The client only sees the status
|
|
636
|
+
* (`FlyTransientHttpError`), and a 502/503/504 can come from a proxy after
|
|
637
|
+
* the request reached Fly, so it cannot tell whether a 5xx was applied.
|
|
638
|
+
* Poll provider state every `pollMs` for `absenceWindowMs`. The identity
|
|
639
|
+
* appearing ends the wait and is adopted. Only a full window of absence,
|
|
640
|
+
* closed by a successful read, permits the next create. A window whose
|
|
641
|
+
* closing read failed proves nothing and fails.
|
|
642
|
+
* - At most `attempts` requests may end ambiguously. After the last one the
|
|
643
|
+
* same window runs, so a late resource is still adopted; otherwise the
|
|
644
|
+
* failure names the identity and every nonce that was sent.
|
|
645
|
+
*
|
|
646
|
+
* Worst case per identity, at the defaults: 2 x (30 s request timeout + 90 s
|
|
647
|
+
* window + one 5 s poll) plus up to 150 s of rate-limit waiting, about
|
|
648
|
+
* 6 min 40 s plus read latency.
|
|
517
649
|
*/
|
|
518
650
|
export async function createWithReadBeforeRetry<T>(input: {
|
|
519
|
-
|
|
520
|
-
|
|
651
|
+
/** Names the intended resource in every log line and error. */
|
|
652
|
+
identity: string;
|
|
653
|
+
/** Issues exactly one create that persists `attempt.nonce` on the resource. */
|
|
654
|
+
create: (attempt: FlyCreateAttempt) => Promise<T>;
|
|
655
|
+
/** Every live provider resource that carries the identity right now. */
|
|
656
|
+
findExisting: () => Promise<readonly T[]>;
|
|
657
|
+
/** One short token per resource for logs, e.g. a Machine id. */
|
|
658
|
+
describe?: (resource: T) => string;
|
|
521
659
|
classify: (error: unknown) => FlyTransientClassification;
|
|
522
|
-
options
|
|
523
|
-
}): Promise<T
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
660
|
+
options?: FlyCreateReadBackOptions;
|
|
661
|
+
}): Promise<FlyCreateResult<T>> {
|
|
662
|
+
const options = input.options ?? {};
|
|
663
|
+
const attempts = options.attempts ?? FLY_CREATE_ATTEMPTS;
|
|
664
|
+
const requests = options.requests ?? FLY_TRANSIENT_RETRY_ATTEMPTS;
|
|
665
|
+
const rateLimitDeadlineMs =
|
|
666
|
+
options.rateLimitDeadlineMs ?? FLY_TRANSIENT_RETRY_DEADLINE_MS;
|
|
667
|
+
const absenceWindowMs =
|
|
668
|
+
options.absenceWindowMs ?? FLY_CREATE_ABSENCE_WINDOW_MS;
|
|
669
|
+
const pollMs = options.pollMs ?? FLY_CREATE_READBACK_POLL_MS;
|
|
670
|
+
const now = options.now ?? Date.now;
|
|
671
|
+
const sleep =
|
|
672
|
+
options.sleep ??
|
|
673
|
+
((ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms)));
|
|
674
|
+
const log = options.log ?? ((line: string) => console.warn(line));
|
|
675
|
+
const newNonce = options.nonce ?? (() => randomBytes(8).toString('hex'));
|
|
676
|
+
const describe = (resources: readonly T[]): string =>
|
|
677
|
+
input.describe
|
|
678
|
+
? resources.map(input.describe).join(', ')
|
|
679
|
+
: `${resources.length} resource(s)`;
|
|
680
|
+
const adopt = (
|
|
681
|
+
resources: readonly T[],
|
|
682
|
+
observed: string,
|
|
683
|
+
): FlyCreateResult<T> => {
|
|
684
|
+
log(
|
|
685
|
+
resources.length === 1
|
|
686
|
+
? `[fly-create] ${input.identity}: adopting ${describe(resources)}, ${observed}; no create issued.`
|
|
687
|
+
: `[fly-create] ${input.identity}: ${resources.length} resources carry this identity (${describe(resources)}), ${observed}; no create issued. The surplus must be reconciled before this identity is used.`,
|
|
688
|
+
);
|
|
689
|
+
return { outcome: 'adopted', resources };
|
|
690
|
+
};
|
|
691
|
+
|
|
692
|
+
const present = await input.findExisting();
|
|
693
|
+
if (present.length > 0) return adopt(present, 'already present');
|
|
694
|
+
|
|
695
|
+
const nonces: string[] = [];
|
|
696
|
+
let lastError: unknown;
|
|
697
|
+
let ambiguous = 0;
|
|
698
|
+
let rateLimited = 0;
|
|
699
|
+
let rateLimitWaitMs = 0;
|
|
700
|
+
for (let number = 1; ; number += 1) {
|
|
701
|
+
const nonce = newNonce();
|
|
702
|
+
nonces.push(nonce);
|
|
703
|
+
// The intent is on record before the effect: the nonce is logged here and
|
|
704
|
+
// travels inside the request, so a Machine this request makes names it.
|
|
705
|
+
log(
|
|
706
|
+
`[fly-create] ${input.identity}: issuing create request ${number}/${requests} (nonce ${nonce}).`,
|
|
707
|
+
);
|
|
708
|
+
try {
|
|
709
|
+
const resource = await input.create({ number, nonce });
|
|
710
|
+
return { outcome: 'created', resources: [resource] };
|
|
711
|
+
} catch (error) {
|
|
712
|
+
const classification = input.classify(error);
|
|
713
|
+
if (!classification.transient) throw error;
|
|
714
|
+
lastError = error;
|
|
715
|
+
if (isFlyRateLimitRefusal(error)) {
|
|
716
|
+
// Fly answered and created nothing: no read-back, no ambiguity spent.
|
|
717
|
+
rateLimited += 1;
|
|
718
|
+
const delayMs = Math.max(
|
|
719
|
+
flyTransientRetryDelayMs({
|
|
720
|
+
retry: rateLimited,
|
|
721
|
+
...(options.random ? { random: options.random } : {}),
|
|
722
|
+
}),
|
|
723
|
+
classification.retryAfterMs ?? 0,
|
|
724
|
+
);
|
|
725
|
+
if (
|
|
726
|
+
number >= requests ||
|
|
727
|
+
rateLimitWaitMs + delayMs > rateLimitDeadlineMs
|
|
728
|
+
) {
|
|
729
|
+
throw new FlyTransientRetryExhaustedError({
|
|
730
|
+
label: `Create ${input.identity}`,
|
|
731
|
+
attempts: number,
|
|
732
|
+
cause: error,
|
|
733
|
+
});
|
|
734
|
+
}
|
|
735
|
+
rateLimitWaitMs += delayMs;
|
|
736
|
+
log(
|
|
737
|
+
`[fly-create] ${input.identity}: create request ${number}/${requests} (nonce ${nonce}) was refused by Fly (${classification.reason}), so nothing was created; sending it again in ${delayMs}ms.`,
|
|
738
|
+
);
|
|
739
|
+
await sleep(delayMs);
|
|
740
|
+
continue;
|
|
529
741
|
}
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
742
|
+
ambiguous += 1;
|
|
743
|
+
log(
|
|
744
|
+
`[fly-create] ${input.identity}: create request ${number}/${requests} (nonce ${nonce}) ended ambiguously (${classification.reason}; ${ambiguous}/${attempts} allowed); reading provider state every ${pollMs}ms for ${absenceWindowMs}ms before any further create.`,
|
|
745
|
+
);
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
const windowStartedAtMs = now();
|
|
749
|
+
for (let read = 1; ; read += 1) {
|
|
750
|
+
await sleep(pollMs);
|
|
751
|
+
let observed: readonly T[] | undefined;
|
|
752
|
+
let readError: unknown;
|
|
753
|
+
try {
|
|
754
|
+
observed = await input.findExisting();
|
|
755
|
+
} catch (error) {
|
|
756
|
+
readError = error;
|
|
757
|
+
}
|
|
758
|
+
const elapsedMs = now() - windowStartedAtMs;
|
|
759
|
+
if (observed !== undefined && observed.length > 0) {
|
|
760
|
+
return adopt(
|
|
761
|
+
observed,
|
|
762
|
+
`observed on read ${read}, ${elapsedMs}ms after the ambiguous create`,
|
|
763
|
+
);
|
|
764
|
+
}
|
|
765
|
+
if (elapsedMs < absenceWindowMs) {
|
|
766
|
+
if (observed === undefined) {
|
|
767
|
+
log(
|
|
768
|
+
`[fly-create] ${input.identity}: read-back ${read} failed (${errorDetail(readError)}); still inside the ${absenceWindowMs}ms window.`,
|
|
769
|
+
);
|
|
770
|
+
}
|
|
771
|
+
continue;
|
|
772
|
+
}
|
|
773
|
+
if (observed === undefined) {
|
|
774
|
+
throw new FlyAmbiguousCreateError({
|
|
775
|
+
identity: input.identity,
|
|
776
|
+
nonces,
|
|
777
|
+
detail: `create request ${number}/${requests} ended ambiguously (${errorDetail(lastError)}) and the read that closes its ${absenceWindowMs}ms read-back window failed (${errorDetail(readError)}), so absence is not proven.`,
|
|
778
|
+
cause: readError,
|
|
779
|
+
});
|
|
780
|
+
}
|
|
781
|
+
log(
|
|
782
|
+
`[fly-create] ${input.identity}: absent on ${read} read(s) over ${elapsedMs}ms after create request ${number}/${requests}.`,
|
|
783
|
+
);
|
|
784
|
+
break;
|
|
785
|
+
}
|
|
786
|
+
if (ambiguous >= attempts || number >= requests) break;
|
|
787
|
+
}
|
|
788
|
+
throw new FlyAmbiguousCreateError({
|
|
789
|
+
identity: input.identity,
|
|
790
|
+
nonces,
|
|
791
|
+
detail: `${ambiguous} create request(s) ended ambiguously (last: ${errorDetail(lastError)}) and the identity stayed absent for ${absenceWindowMs}ms after each.`,
|
|
792
|
+
cause: lastError,
|
|
793
|
+
});
|
|
535
794
|
}
|
|
536
795
|
|
|
537
796
|
/**
|