@tokenoftrust/cli 7.4.0 → 7.4.1
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/package.json +1 -1
- package/src/commands/accept.mjs +65 -16
- package/src/viewer-session.mjs +47 -14
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tokenoftrust/cli",
|
|
3
|
-
"version": "7.4.
|
|
3
|
+
"version": "7.4.1",
|
|
4
4
|
"description": "Token of Trust developer CLI — clone a tenant store, run it locally with save→reload, and submit it for preview. Installs the `tot` command.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Token of Trust",
|
package/src/commands/accept.mjs
CHANGED
|
@@ -57,6 +57,7 @@ import { autoSurfaceDoctor } from "../merge-doctor-report.mjs";
|
|
|
57
57
|
import { isJobAccepted, waitForPipelineJob } from "../pipeline-job.mjs";
|
|
58
58
|
|
|
59
59
|
const DEFAULT_STOREFRONT_URL = "https://storefront.tokenoftrust.store";
|
|
60
|
+
const defaultSleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
60
61
|
const DEFAULT_MCP_URL = "https://mcp.tokenoftrust.com";
|
|
61
62
|
|
|
62
63
|
// The three refresh strategies `candidate_refresh` accepts, mirroring the admin
|
|
@@ -392,16 +393,54 @@ async function handleNotMergeable(
|
|
|
392
393
|
return { integrate: true, pr: rr.prNumber ?? pr, changeId };
|
|
393
394
|
}
|
|
394
395
|
|
|
396
|
+
/**
|
|
397
|
+
* The waits before asking again about an integrate request whose answer never arrived (the
|
|
398
|
+
* connection dropped, or the store answered 502/503/504). The request may already have created
|
|
399
|
+
* the job, so it is repeated with the SAME `Idempotency-Key`: the store answers the job the
|
|
400
|
+
* first request created, or creates it if that request never landed.
|
|
401
|
+
*/
|
|
402
|
+
export const INTEGRATE_REPEAT_DELAYS_MS = Object.freeze([2_000, 5_000, 10_000, 20_000]);
|
|
403
|
+
const STORE_UNAVAILABLE_STATUSES = new Set([502, 503, 504]);
|
|
404
|
+
|
|
405
|
+
/** An integrate request whose outcome is unknown: it may or may not have queued the job. */
|
|
406
|
+
export class IntegrateUnconfirmedError extends Error {
|
|
407
|
+
/** @param {string} reason */
|
|
408
|
+
constructor(reason) {
|
|
409
|
+
super(reason);
|
|
410
|
+
this.name = "IntegrateUnconfirmedError";
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
395
414
|
/**
|
|
396
415
|
* POST `/api/changes/integrate`, wait on the job it submitted, and normalise the outcome.
|
|
397
|
-
* Returns { res, result } — `res.ok` is the submit's transport verdict.
|
|
416
|
+
* Returns { res, result } — `res.ok` is the submit's transport verdict. A request whose answer
|
|
417
|
+
* was lost is repeated with the same idempotency key (see INTEGRATE_REPEAT_DELAYS_MS), and
|
|
418
|
+
* throws IntegrateUnconfirmedError only when no answer ever arrives.
|
|
398
419
|
*/
|
|
399
|
-
async function postIntegrate({ base, authHeaders, body }, fetchImpl, waitOpts = {}) {
|
|
400
|
-
const
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
420
|
+
async function postIntegrate({ base, authHeaders, body }, fetchImpl, waitOpts = {}, sleep = defaultSleep) {
|
|
421
|
+
const idempotencyKey = globalThis.crypto.randomUUID();
|
|
422
|
+
const send = async () => {
|
|
423
|
+
try {
|
|
424
|
+
const answer = await fetchImpl(`${base}/api/changes/integrate`, {
|
|
425
|
+
method: "POST",
|
|
426
|
+
headers: { "content-type": "application/json", "idempotency-key": idempotencyKey, ...authHeaders },
|
|
427
|
+
body: JSON.stringify(body),
|
|
428
|
+
});
|
|
429
|
+
return STORE_UNAVAILABLE_STATUSES.has(answer.status)
|
|
430
|
+
? { res: null, reason: `the store answered HTTP ${answer.status}` }
|
|
431
|
+
: { res: answer, reason: null };
|
|
432
|
+
} catch (e) {
|
|
433
|
+
return { res: null, reason: String(e?.message || e) };
|
|
434
|
+
}
|
|
435
|
+
};
|
|
436
|
+
let { res, reason } = await send();
|
|
437
|
+
for (const delay of INTEGRATE_REPEAT_DELAYS_MS) {
|
|
438
|
+
if (res) break;
|
|
439
|
+
console.log(` no answer from the integration queue (${reason}) — it may already have the request; asking again in ${delay / 1000}s…`);
|
|
440
|
+
await sleep(delay);
|
|
441
|
+
({ res, reason } = await send());
|
|
442
|
+
}
|
|
443
|
+
if (!res) throw new IntegrateUnconfirmedError(/** @type {string} */ (reason));
|
|
405
444
|
let data = {};
|
|
406
445
|
try {
|
|
407
446
|
data = await res.json();
|
|
@@ -477,7 +516,7 @@ export function reportIntegrated(result, { tenant, label }) {
|
|
|
477
516
|
* strategy?:string, mcpUrl?:string|null, identity?:string|null, env?:NodeJS.ProcessEnv }} params
|
|
478
517
|
* @param {{ fetch?:typeof fetch, confirmPlan?:typeof printPlanAndConfirm,
|
|
479
518
|
* resolveViewerTransport?:typeof resolveViewerTransport,
|
|
480
|
-
* wait?:Parameters<typeof waitForPipelineJob>[2] }} [deps]
|
|
519
|
+
* wait?:Parameters<typeof waitForPipelineJob>[2], sleep?:(ms:number)=>Promise<void> }} [deps]
|
|
481
520
|
* @returns {Promise<number>} process exit code
|
|
482
521
|
*/
|
|
483
522
|
export async function runIntegrate(
|
|
@@ -595,12 +634,10 @@ export async function runIntegrate(
|
|
|
595
634
|
{ base, authHeaders, body: integrateBody({ tenant, pr: curPr, changeId: curChangeId, headSha: curHead }) },
|
|
596
635
|
fetchImpl,
|
|
597
636
|
deps.wait,
|
|
637
|
+
deps.sleep,
|
|
598
638
|
));
|
|
599
639
|
} catch (e) {
|
|
600
|
-
|
|
601
|
-
fail(`couldn't reach the integration queue at ${base}: ${String(e?.message || e)}`, "check --url / your network, then re-run"),
|
|
602
|
-
);
|
|
603
|
-
return 1;
|
|
640
|
+
return reportIntegrateUnconfirmed(e, { base, tenant });
|
|
604
641
|
}
|
|
605
642
|
|
|
606
643
|
// A pre-flight error (auth, unknown tenant, candidate not found) is an HTTP 4xx
|
|
@@ -633,12 +670,10 @@ export async function runIntegrate(
|
|
|
633
670
|
},
|
|
634
671
|
fetchImpl,
|
|
635
672
|
deps.wait,
|
|
673
|
+
deps.sleep,
|
|
636
674
|
));
|
|
637
675
|
} catch (e) {
|
|
638
|
-
|
|
639
|
-
fail(`couldn't reach the integration queue at ${base}: ${String(e?.message || e)}`, "check --url / your network, then re-run"),
|
|
640
|
-
);
|
|
641
|
-
return 1;
|
|
676
|
+
return reportIntegrateUnconfirmed(e, { base, tenant });
|
|
642
677
|
}
|
|
643
678
|
if (!res.ok) {
|
|
644
679
|
return reportIntegrateHttpError(result, res, { tenant, label });
|
|
@@ -682,6 +717,20 @@ async function preflightMergeable({ base, authHeaders, pr, changeId }, fetchImpl
|
|
|
682
717
|
}
|
|
683
718
|
}
|
|
684
719
|
|
|
720
|
+
/**
|
|
721
|
+
* Surface an integrate request that never got an answer. It may have queued the job, so this
|
|
722
|
+
* never says the queue was unreachable. Returns exit 1.
|
|
723
|
+
*/
|
|
724
|
+
function reportIntegrateUnconfirmed(e, { base, tenant }) {
|
|
725
|
+
console.error(
|
|
726
|
+
fail(
|
|
727
|
+
`couldn't confirm the integration request reached ${base} (${String(e?.message || e)}) — it may already be queued.`,
|
|
728
|
+
`check \`tot pr list --tenant ${tenant}\` before re-running; a change already added reports as such.`,
|
|
729
|
+
),
|
|
730
|
+
);
|
|
731
|
+
return 1;
|
|
732
|
+
}
|
|
733
|
+
|
|
685
734
|
/** Surface an integrate HTTP pre-flight error (4xx, no queue verdict). Returns exit 1. */
|
|
686
735
|
function reportIntegrateHttpError(result, res, { tenant, label }) {
|
|
687
736
|
const msg = result.error || `HTTP ${res.status}`;
|
package/src/viewer-session.mjs
CHANGED
|
@@ -69,8 +69,29 @@ function originOf(url) {
|
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
-
/**
|
|
73
|
-
|
|
72
|
+
/**
|
|
73
|
+
* The waits before each retry of a session mint the store refused because Token of Trust's
|
|
74
|
+
* identity service (MCP) was briefly unavailable. An MCP restart is unreachable for 40-90 s,
|
|
75
|
+
* so the schedule spans about 90 s; the store retries each attempt itself as well.
|
|
76
|
+
*/
|
|
77
|
+
export const MCP_OUTAGE_RETRY_DELAYS_MS = Object.freeze([2_000, 4_000, 8_000, 15_000, 30_000, 30_000]);
|
|
78
|
+
|
|
79
|
+
/** The cli-session codes that mean the MCP was briefly unavailable, not misconfigured. */
|
|
80
|
+
const MCP_OUTAGE_CODES = new Set(["mcp_unreachable", "mcp_unavailable"]);
|
|
81
|
+
const MCP_OUTAGE_STATUSES = new Set([502, 503, 504]);
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Is a cli-session refusal an MCP outage worth waiting out? The store names every seam
|
|
85
|
+
* fault `error: "mcp_unreachable"` and says which in `code`: a lost connection
|
|
86
|
+
* (`mcp_unreachable`), an MCP answering 502/503/504 (`mcp_unavailable`, or `mcp_http_error`
|
|
87
|
+
* with that `upstreamStatus`). A misconfigured or unconfigured seam is never retried.
|
|
88
|
+
* @param {number} status @param {Record<string, unknown>|null} body
|
|
89
|
+
*/
|
|
90
|
+
export function isMcpOutageRefusal(status, body) {
|
|
91
|
+
if ((status !== 502 && status !== 503) || body?.error !== "mcp_unreachable") return false;
|
|
92
|
+
if (typeof body.code === "string" && MCP_OUTAGE_CODES.has(body.code)) return true;
|
|
93
|
+
return body.code === "mcp_http_error" && MCP_OUTAGE_STATUSES.has(Number(body.upstreamStatus));
|
|
94
|
+
}
|
|
74
95
|
|
|
75
96
|
/** A response whose body was already read, re-exposed so the caller can read it again. */
|
|
76
97
|
const replayed = (res, body) => ({
|
|
@@ -108,6 +129,12 @@ export function describeCliSessionRefusal(status, body, signedInMcpUrl) {
|
|
|
108
129
|
const code = body && typeof body.code === "string" ? body.code : null;
|
|
109
130
|
|
|
110
131
|
if (status === 502 || status === 503) {
|
|
132
|
+
if (isMcpOutageRefusal(status, body)) {
|
|
133
|
+
return {
|
|
134
|
+
message: `couldn't verify your session — Token of Trust's identity service (MCP) is unavailable${code ? ` (${code})` : ""}.`,
|
|
135
|
+
hint: "it is usually back within a few minutes; re-run then. if it persists, report it with `tot feedback`.",
|
|
136
|
+
};
|
|
137
|
+
}
|
|
111
138
|
return {
|
|
112
139
|
message: `couldn't verify your session — the store's identity check failed${code ? ` (${code})` : ""}.`,
|
|
113
140
|
hint: "retry shortly; if it persists, report it with `tot feedback`.",
|
|
@@ -136,7 +163,8 @@ export function describeCliSessionRefusal(status, body, signedInMcpUrl) {
|
|
|
136
163
|
*
|
|
137
164
|
* @param {{ tenant:string, env?:NodeJS.ProcessEnv, fetchImpl?:typeof fetch,
|
|
138
165
|
* storefrontUrl?:string|null, resolveDev?:typeof resolveDeveloperSession,
|
|
139
|
-
* verbose?:boolean, log?:(s:string)=>void,
|
|
166
|
+
* verbose?:boolean, log?:(s:string)=>void, notify?:(s:string)=>void,
|
|
167
|
+
* sleep?:(ms:number)=>Promise<void> }} params
|
|
140
168
|
* @returns {Promise<
|
|
141
169
|
* { ok:true, base:string, identity:{ account:string|null, mcp:string|null },
|
|
142
170
|
* authHeaders:Record<string,string> } |
|
|
@@ -151,6 +179,7 @@ export async function resolveViewerTransport({
|
|
|
151
179
|
resolveDev = resolveDeveloperSession,
|
|
152
180
|
verbose = false,
|
|
153
181
|
log = (s) => console.error(`[verbose] ${s}`),
|
|
182
|
+
notify = (s) => console.error(s),
|
|
154
183
|
sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
|
|
155
184
|
}) {
|
|
156
185
|
// --verbose: request URL + HTTP status + the server's error code only — NEVER
|
|
@@ -204,18 +233,22 @@ export async function resolveViewerTransport({
|
|
|
204
233
|
return { ok: false, message: "your selected MCP issuer isn't trusted by this store.",
|
|
205
234
|
hint: "report it with `tot feedback`; this command keeps your selected issuer." };
|
|
206
235
|
}
|
|
236
|
+
// An MCP outage (a restart leaves it unavailable for 40-90 s) is waited out on the
|
|
237
|
+
// schedule above, then reported naming the MCP. No other refusal is retried.
|
|
207
238
|
let mintedSession = await mintSession(base, dev.token, selectedMcp, fetchImpl, vlog);
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
const
|
|
212
|
-
if (
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
}
|
|
239
|
+
let notified = false;
|
|
240
|
+
for (const delay of MCP_OUTAGE_RETRY_DELAYS_MS) {
|
|
241
|
+
if (!mintedSession.ok) break;
|
|
242
|
+
const { status } = mintedSession.response;
|
|
243
|
+
if (status !== 502 && status !== 503) break;
|
|
244
|
+
const refusal = await readJson(mintedSession.response);
|
|
245
|
+
mintedSession = { ok: true, response: replayed(mintedSession.response, refusal) };
|
|
246
|
+
if (!isMcpOutageRefusal(status, refusal)) break;
|
|
247
|
+
if (!notified) notify("Token of Trust's identity service (MCP) is unavailable — retrying for up to 90s…");
|
|
248
|
+
notified = true;
|
|
249
|
+
vlog(`cli-session ${refusal.code} — retrying in ${delay}ms`);
|
|
250
|
+
await sleep(delay);
|
|
251
|
+
mintedSession = await mintSession(base, dev.token, selectedMcp, fetchImpl, vlog);
|
|
219
252
|
}
|
|
220
253
|
if (!mintedSession.ok) return mintedSession.refusal;
|
|
221
254
|
const res = mintedSession.response;
|