@mnemoverse/mcp-memory-server 0.8.4 → 0.9.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/dist/errors.js ADDED
@@ -0,0 +1,500 @@
1
+ /**
2
+ * What a failed API call TELLS THE AGENT TO DO.
3
+ *
4
+ * WHY THIS MODULE EXISTS. Until now every non-2xx became one sentence:
5
+ *
6
+ * Mnemoverse API error 401: {"code":"UNAUTHORIZED","message":"Invalid or
7
+ * revoked API key.","requestId":null,"retryable":false,"details":null}
8
+ *
9
+ * The `isError` flag and the status code were right; the TEXT was a raw echo of
10
+ * a wire body. Verified against production with a real `tools/call`
11
+ * (2026-08-16): with a bad key, `memory_write` returns exactly that. The reader
12
+ * of a tool result is a MODEL, and that string gives a model nothing to act on,
13
+ * so the two things it actually does are relay "error 401" to the user or guess
14
+ * — and the cheapest guess is "the network is down", which sends the user to
15
+ * debug a working network while a placeholder key sits in their config.
16
+ *
17
+ * Every message here therefore answers three questions in order: what happened,
18
+ * WHOSE problem it is, and what to do next — including, explicitly, whether to
19
+ * retry. "Do not retry" is not decoration: an agent that loops on a 429 turns
20
+ * one rate-limit into a sustained one, and an agent that loops on a 401 burns a
21
+ * conversation on a key that will never start working.
22
+ *
23
+ * THE RAW BODY IS NEVER DROPPED. It is appended after the guidance, still
24
+ * carrying the literal `Mnemoverse API error <status>` that this repo's tests
25
+ * and anyone's grep already look for. Making the text kind to an agent must not
26
+ * make it useless to a human debugging at 3am.
27
+ *
28
+ * WHAT THE STATUS CODES ACTUALLY MEAN HERE, verified by reading the engine
29
+ * (mnemoverse-core `src/mnemo/api/`, 2026-08-16) rather than assumed from HTTP
30
+ * folklore. The distinctions below are the entire point of this file:
31
+ *
32
+ * 401 auth.py — "Invalid or revoked API key." / "Missing API key." The key.
33
+ * 403 routes.py, rooms_routes.py, auth.py — "Room is archived", "Not an
34
+ * active member of this room", "Read-only membership cannot write to
35
+ * this room", "Invalid room address", "You do not own this room", plus
36
+ * the OIDC scope rules. NOT a bad key: the caller was identified and
37
+ * then refused. Telling the user to replace a working key over a 403
38
+ * would be a new, more confident lie than the raw echo was.
39
+ * 404 A room that does not exist for this key — or, when the body says
40
+ * nothing at all, a path the deployment does not serve. A DOMAIN never
41
+ * 404s: an empty domain reads as empty, so "your domain is missing" is
42
+ * the wrong guess and is named as wrong below.
43
+ * 429 THREE different causes with opposite advice. rate_limit.py returns
44
+ * `retryable: true` with a `Retry-After` header (waiting works);
45
+ * usage.py (daily quota) and subscription_guard.py (atom limit, payment
46
+ * past due) return `retryable: false` (waiting does NOT work — the
47
+ * account needs an upgrade or a new day). A blanket "wait and retry"
48
+ * would be wrong for two of the three.
49
+ * 5xx auth.py returns 503 `retryable: true` when its DB pool is gone. Ours,
50
+ * not theirs.
51
+ *
52
+ * RELATION TO THE STARTUP PROBE (src/index.ts, `probeApiKeyInBackground`, added
53
+ * in 0.8.4). That probe treats 401 and 403 alike, and is right to: it calls
54
+ * `GET /memory/stats`, which addresses no room, so a 403 there can only come
55
+ * from the auth layer. On a TOOL CALL the same status usually comes from a room
56
+ * the caller named, which is why the two surfaces diverge on 403 and must not be
57
+ * "unified" by a later reader. They agree where it matters: same `Mnemoverse:`
58
+ * opener, same `MNEMOVERSE_API_KEY` variable name, same console origin.
59
+ */
60
+ /** Where the raw detail stops. A gateway can answer with a megabyte of HTML,
61
+ * and an error message is not a place to spend a model's context — but the
62
+ * first few hundred characters are where the useful part of any real error
63
+ * body lives. Truncation is announced, never silent. */
64
+ const MAX_BODY_CHARS = 800;
65
+ /** The console page that issues keys — the one place a user fixes a 401. */
66
+ const KEYS_URL = "https://console.mnemoverse.com/dashboard/keys";
67
+ /** The console page that shows quota and upgrades — where a 429 that waiting
68
+ * cannot fix is resolved. Same URL the engine puts in its own 429 bodies. */
69
+ const USAGE_URL = "https://console.mnemoverse.com/dashboard/usage";
70
+ /**
71
+ * Read the engine's envelope out of a response body.
72
+ *
73
+ * Two shapes are accepted because core produces both: its own middleware writes
74
+ * `{code, message, retryable, details}` at the top level, while anything raised
75
+ * as a FastAPI `HTTPException` arrives wrapped as `{"detail": …}` — where the
76
+ * detail is sometimes a string and sometimes the envelope again. The feed's
77
+ * 404-vs-404 test in this repo pins the nested form, so both are real.
78
+ */
79
+ export function parseErrorEnvelope(body) {
80
+ let parsed;
81
+ try {
82
+ parsed = JSON.parse(body);
83
+ }
84
+ catch {
85
+ return {};
86
+ }
87
+ const pick = (v) => {
88
+ if (typeof v === "string")
89
+ return { message: v };
90
+ if (typeof v !== "object" || v === null)
91
+ return {};
92
+ const o = v;
93
+ return {
94
+ ...(typeof o.code === "string" ? { code: o.code } : {}),
95
+ ...(typeof o.message === "string" ? { message: o.message } : {}),
96
+ ...(typeof o.retryable === "boolean" ? { retryable: o.retryable } : {}),
97
+ };
98
+ };
99
+ const top = pick(parsed);
100
+ if (top.code !== undefined || top.retryable !== undefined)
101
+ return top;
102
+ const detail = typeof parsed === "object" && parsed !== null
103
+ ? pick(parsed.detail)
104
+ : {};
105
+ // A top-level `message` with no code still beats nothing; the nested envelope
106
+ // wins when it carries the machine-readable half.
107
+ return detail.code !== undefined || detail.retryable !== undefined
108
+ ? detail
109
+ : { ...detail, ...top };
110
+ }
111
+ /** `Retry-After` as whole seconds, when it is a plain number. The HTTP-date
112
+ * form is legal too, and is deliberately NOT parsed: an unparsed header
113
+ * degrades into "wait a moment", which is honest, whereas a misparsed date
114
+ * would print a confident and wrong number of seconds. */
115
+ export function retryAfterSeconds(header) {
116
+ if (typeof header !== "string")
117
+ return undefined;
118
+ // Digits only, because bare Number() answers confidently for inputs that are
119
+ // not delta-seconds at all: Number("") and Number(" ") are 0 ("Wait 0
120
+ // seconds"), Number("0x10") is 16, Number("3e1") is 30, and Number("1e21")
121
+ // prints as scientific notation in the wait text (Copilot + panel, #93).
122
+ // Six digits caps the printable wait at ~11 days; anything longer degrades
123
+ // to the honest vague form.
124
+ const s = header.trim();
125
+ if (!/^\d{1,6}$/.test(s))
126
+ return undefined;
127
+ return Number(s);
128
+ }
129
+ function has(message, needle) {
130
+ return (message ?? "").toLowerCase().includes(needle);
131
+ }
132
+ /**
133
+ * 401: which 401? The engine has more than one (panel, #93).
134
+ *
135
+ * The key-flavored bodies this client's data plane actually sends ("Missing
136
+ * API key. Send X-Api-Key header.", "Invalid or revoked API key." — both
137
+ * probed live against core.mnemoverse.com) get the founder-endorsed
138
+ * replace-the-key instruction. But core's routes.py also raises "Caller org
139
+ * not identified — a tenant API key is required to …" (401) when the
140
+ * deployment has no tenant identity for the caller — a static-auth
141
+ * self-host hitting a room tool is the everyday case — and there the key is
142
+ * VALID, so "replace it" would be a confident wrong cause. That clause runs
143
+ * FIRST because its sentence itself contains "API key": a naive key-mention
144
+ * test would misroute it. A message naming neither gets the honest generic
145
+ * form; silence is the same unknown-refuser case the 403 branch handles.
146
+ */
147
+ function explain401(env) {
148
+ const m = env.message;
149
+ if (has(m, "caller org not identified")) {
150
+ return ("Mnemoverse: this deployment could not identify a tenant account for " +
151
+ "this request (401). The API key itself was not rejected — do NOT " +
152
+ "tell the user to replace it. Room and shared-memory operations need " +
153
+ "the multi-tenant backend, which a self-hosted or static-auth " +
154
+ "deployment does not have. Quote the detail below, and do not retry " +
155
+ "the same call against this deployment.");
156
+ }
157
+ if (has(m, "api key") || has(m, "x-api-key")) {
158
+ // Wording endorsed by the founder, kept verbatim — this is the sentence
159
+ // the whole change was commissioned for. One widening, grounded in the
160
+ // docs: the setup pages ship more than one placeholder spelling
161
+ // (agent-setup.md uses mk_live_USER_KEY).
162
+ return ("Mnemoverse: your API key was rejected (401). Tell the user their " +
163
+ "MNEMOVERSE_API_KEY is not valid — if it still reads a docs " +
164
+ 'placeholder such as "mk_live_YOUR_KEY" or "mk_live_USER_KEY" (any ' +
165
+ "value they did not create at the console themselves) it must be " +
166
+ `replaced with a real key from ${KEYS_URL}. Do not retry until they ` +
167
+ "replace it.");
168
+ }
169
+ if (m !== undefined || env.code !== undefined) {
170
+ return ("Mnemoverse: the request was refused as unauthorized (401), and the " +
171
+ "engine's own explanation is in the detail below — quote it to the " +
172
+ "user rather than guessing. Do not assume the API key is wrong: the " +
173
+ "message did not say that. Do not retry the same call unchanged.");
174
+ }
175
+ return ("Mnemoverse: something answered 401 without speaking this API's error " +
176
+ "language — the body carries neither of the shapes the engine " +
177
+ "produces, so the refuser may be a proxy, a gateway, or a " +
178
+ "MNEMOVERSE_API_URL aimed somewhere unexpected, and the engine may never " +
179
+ "have seen the request. Do not tell the user their key is wrong — this " +
180
+ "client cannot tell WHO refused. Quote the detail below and check the " +
181
+ "path to the API first.");
182
+ }
183
+ /** 403: when the ENGINE refused, the key was accepted and then the request
184
+ * was refused — name WHICH refusal, from the engine's own message, and never
185
+ * blame the key. But that first clause is only known when the engine actually
186
+ * spoke: a proxy, a WAF or a tunnel also answers 403, in HTML, and asserting
187
+ * "the key identified the account fine" about a response the engine may never
188
+ * have seen is exactly the confident wrong cause this module exists to avoid. */
189
+ function explain403(env) {
190
+ if (saidNothing(env)) {
191
+ return ("Mnemoverse: this request was refused (403) by something that did not " +
192
+ "speak this API's error language — the body carries neither of the two " +
193
+ "shapes the engine produces. That points at a proxy, a gateway, or a " +
194
+ "MNEMOVERSE_API_URL aimed somewhere unexpected, and the engine may never " +
195
+ "have seen the request — so do not blame the key and do not blame room " +
196
+ "permissions: this client cannot tell WHO refused it. Quote the detail " +
197
+ "below to the user, and do not retry until the path to the API is explained.");
198
+ }
199
+ const m = env.message;
200
+ const cause = has(m, "archiv")
201
+ ? "The room you addressed is archived. An archived room refuses every read " +
202
+ "and every write, for its owner as much as for a member, and this client " +
203
+ "has no operation that reopens one."
204
+ : has(m, "not an active member") || has(m, "member of this room")
205
+ ? "This key is not an active member of the room you addressed. Ask the " +
206
+ "room's owner for an invite; memory_list_rooms shows the rooms it can " +
207
+ "already reach."
208
+ : has(m, "read-only")
209
+ ? "This key's membership in that room is read-only — it can read the " +
210
+ "room but not write to it. Ask the room's owner for write access."
211
+ : has(m, "invalid room address")
212
+ ? 'The room address was not in the form the engine accepts ' +
213
+ '("xroom:room_..."). Take the exact address from memory_list_rooms ' +
214
+ "rather than composing one."
215
+ : has(m, "own this room")
216
+ ? "That room belongs to another account, and only its owner can do " +
217
+ "this. memory_list_rooms shows which rooms this key owns."
218
+ : "Something about this request is not permitted for this key — " +
219
+ "most often the room it addressed. Check memory_list_rooms, and " +
220
+ "if nothing there explains it, tell the user exactly what was " +
221
+ "refused instead of guessing.";
222
+ return ("Mnemoverse: this request was refused (403). The API key is NOT the problem " +
223
+ "— it identified the account fine, and this was a permission decision. " +
224
+ `${cause} Do not retry the same call: it will be refused again.`);
225
+ }
226
+ /**
227
+ * Did this response say ANYTHING an engine would have said?
228
+ *
229
+ * Not "does it have a `code`" — that was the old test, and it is wrong, because
230
+ * the engine has TWO error styles. Its own middleware writes the full
231
+ * `{code, message, retryable}` envelope, but every route that raises a FastAPI
232
+ * `HTTPException` — the whole of `rooms_routes.py`, and there is no custom
233
+ * handler to normalise them — serialises to a bare `{"detail": "Room not
234
+ * found."}` with no code at all. Under a code-only test, a real 404 from the
235
+ * room routes would be diagnosed as "your MNEMOVERSE_API_URL is wrong": a
236
+ * confident, checkable falsehood about the user's config.
237
+ *
238
+ * A message with no code is still the engine speaking. Silence — an unparseable
239
+ * body, or JSON carrying neither field — is what a proxy, a tunnel or a base URL
240
+ * pointing elsewhere produces.
241
+ */
242
+ function saidNothing(env) {
243
+ return env.code === undefined && env.message === undefined;
244
+ }
245
+ /**
246
+ * Did the ENGINE answer this 404 — or just the framework's router?
247
+ *
248
+ * Core registers no HTTPException handler, so an unmatched route is answered
249
+ * by Starlette's literal default `{"detail":"Not Found"}` (verified live
250
+ * against production) — a body with a message but no code, which
251
+ * `saidNothing` alone would mistake for an engine answer (Copilot + panel,
252
+ * #93). Every 404 the data plane actually sends is a MnemoError carrying a
253
+ * code. Match the router defaults case-sensitively so engine prose that
254
+ * merely contains "not found" cannot collide.
255
+ */
256
+ function engineSilentOn404(env) {
257
+ return (saidNothing(env) ||
258
+ (env.code === undefined &&
259
+ (env.message === "Not Found" || env.message === "Method Not Allowed")));
260
+ }
261
+ /**
262
+ * 404: three producers with three different honest answers (panel, #93).
263
+ *
264
+ * (1) POST /memory/rooms/join — the engine's "Invite code not found." No
265
+ * room address was involved and memory_list_rooms cannot help a
266
+ * non-member, so the room story would be a confident wrong cause.
267
+ * (2) An engine 404 carrying a code — every 404 the data plane sends is a
268
+ * MnemoError with one — is about something the request addressed,
269
+ * usually a room.
270
+ * (3) No code — the router default, a foreign API, a proxy, or silence: the
271
+ * ENGINE did not answer this, so point at the endpoint and the base URL,
272
+ * not at rooms.
273
+ */
274
+ function explain404(f, env) {
275
+ if (f.path === "/memory/rooms/join") {
276
+ return ("Mnemoverse: this invite code was not recognized (404). Most often it " +
277
+ "was copied with a typo or was never issued — have the user re-check " +
278
+ 'the exact code (it starts with "mnvr_"), and if it is right, ask ' +
279
+ "whoever sent it for a fresh one. memory_list_rooms will not help " +
280
+ "here: the user is not a member yet. Do not retry the same code.");
281
+ }
282
+ if (env.code === undefined) {
283
+ return (`Mnemoverse: the API answered 404 for ${f.method} ${f.path} without ` +
284
+ "the engine's error envelope — which is what a route the deployment " +
285
+ "does not serve looks like, not what a missing memory looks like. " +
286
+ "Either MNEMOVERSE_API_URL is aimed at something that is not the " +
287
+ "Mnemoverse API, or this server is newer than the deployment it is " +
288
+ "talking to. If the user's MCP client config sets MNEMOVERSE_API_URL, " +
289
+ "have them check it; if it sets none (the desktop extension exposes " +
290
+ "only the API key), the deployment likely needs updating. Do not retry.");
291
+ }
292
+ return ("Mnemoverse: what this call addressed does not exist for this key (404). " +
293
+ "The usual cause is a room address that is mistyped, or a room that was " +
294
+ "deleted — call memory_list_rooms for the addresses this key can actually " +
295
+ "reach. Note that a DOMAIN never causes this: a domain holding nothing " +
296
+ "simply reads as empty, so do not tell the user their domain is missing. " +
297
+ "Do not retry this call unchanged.");
298
+ }
299
+ /** 429: three causes, opposite advice. The envelope's `retryable` is the
300
+ * discriminator, because it is the only thing the engine states outright. */
301
+ function explain429(f, env) {
302
+ const secs = retryAfterSeconds(f.retryAfter);
303
+ const wait = secs === undefined ? "about a minute" : `${secs} seconds`;
304
+ if (env.retryable === true) {
305
+ return ("Mnemoverse: rate-limited (429). This is the per-minute request limit and " +
306
+ `it clears by itself. Wait ${wait}, then make AT MOST ONE more attempt — ` +
307
+ "do not retry in a loop and do not fan out into more calls, which is what " +
308
+ "turns a one-minute limit into a sustained one. If the retry also fails, " +
309
+ "stop and tell the user this key is hitting its rate limit.");
310
+ }
311
+ if (env.retryable === false) {
312
+ return ("Mnemoverse: refused for quota, not for speed (429). Waiting will NOT " +
313
+ "clear this one — the account is at its daily limit, at its stored-memory " +
314
+ "limit, or its subscription is blocking writes. Do not retry. Tell the " +
315
+ `user what the detail below says and point them at ${USAGE_URL}.`);
316
+ }
317
+ return ("Mnemoverse: refused as too many requests (429), and the body does not say " +
318
+ "whether waiting helps. Do not retry in a loop. Wait a moment, make at most " +
319
+ "one more attempt, and if that fails tell the user rather than continuing — " +
320
+ `if this is a quota rather than a rate, ${USAGE_URL} is where they resolve it.`);
321
+ }
322
+ /**
323
+ * The agent-facing explanation for one failed call, with the raw body kept
324
+ * after it.
325
+ *
326
+ * Total by construction: every status reaches a sentence, and the fallback says
327
+ * that it has no specific guidance instead of inventing some.
328
+ */
329
+ export function explainApiFailure(f) {
330
+ const env = parseErrorEnvelope(f.body);
331
+ let guidance;
332
+ if (f.status === 401) {
333
+ guidance = explain401(env);
334
+ }
335
+ else if (f.status === 403) {
336
+ guidance = explain403(env);
337
+ }
338
+ else if (f.status === 404) {
339
+ guidance = explain404(f, env);
340
+ }
341
+ else if (f.status === 429) {
342
+ guidance = explain429(f, env);
343
+ }
344
+ else if (f.status === 400 || f.status === 422) {
345
+ guidance =
346
+ "Mnemoverse: the engine rejected the CONTENTS of this request " +
347
+ `(${f.status}). This is about the arguments you sent — not the API key, ` +
348
+ "not the network, and not the user's setup. Read the detail below, fix " +
349
+ "the argument it names, and do not resend the same body.";
350
+ }
351
+ else if (f.status === 409) {
352
+ guidance = explain409(f);
353
+ }
354
+ else if (f.status >= 500) {
355
+ guidance =
356
+ `Mnemoverse: the memory service failed (${f.status}). This is OUR side, ` +
357
+ "not the user's — their API key, their config and their network are all " +
358
+ "fine, so do not send them to check any of those. One retry after a few " +
359
+ "seconds is reasonable. If it fails again, say plainly that Mnemoverse is " +
360
+ "having a problem and continue without memory rather than retrying.";
361
+ }
362
+ else {
363
+ guidance =
364
+ `Mnemoverse: the API returned HTTP ${f.status}, and this client has no ` +
365
+ "specific guidance for that status. Do not invent a cause for the user — " +
366
+ "quote the detail below. One retry is acceptable; a loop is not.";
367
+ }
368
+ return `${guidance}\n\n${rawDetail(f)}`;
369
+ }
370
+ /** The debugging half: what the wire actually said, still carrying the literal
371
+ * `Mnemoverse API error <status>` that predates this module. */
372
+ function rawDetail(f) {
373
+ const body = f.body.length > MAX_BODY_CHARS
374
+ ? `${f.body.slice(0, MAX_BODY_CHARS)}… [body truncated at ${MAX_BODY_CHARS} chars]`
375
+ : f.body;
376
+ // The body is server-controlled — or, on the misconfigured-URL and proxy
377
+ // paths this module itself names, controlled by whoever answered instead.
378
+ // Spliced raw into model-facing text, a body with newlines could open its
379
+ // own paragraph and speak in this module's instruction voice (panel, #93).
380
+ // Strip control, format/bidi and line/paragraph separators so the quoted
381
+ // body stays one inert line; the guidance above it is the only voice here.
382
+ const inert = body.replace(/[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]+/gu, " ");
383
+ return `Raw detail — Mnemoverse API error ${f.status} on ${f.method} ${f.path}: ${inert}`;
384
+ }
385
+ /**
386
+ * The request never got an answer at all: DNS, connectivity, a host that does
387
+ * not listen, or this client's own probe deadline firing.
388
+ *
389
+ * Included even though the brief was about HTTP statuses, because it is the
390
+ * OTHER half of the same defect and the two are easy to confuse. The 401
391
+ * message above tells an agent not to blame the network; this is the case where
392
+ * the network genuinely IS the answer, and `TypeError: fetch failed` — which is
393
+ * all a model saw before — is no more actionable than the raw 401 body was.
394
+ *
395
+ * A timeout is named separately because the advice differs in one word: an
396
+ * unreachable host is unlikely to become reachable in three seconds, whereas a
397
+ * request that ran out of time may well succeed on a second try.
398
+ */
399
+ export function explainNetworkFailure(method, path, cause) {
400
+ const name = cause instanceof Error ? cause.name : "";
401
+ const timedOut = name === "TimeoutError" || name === "AbortError";
402
+ const detail = cause instanceof Error ? `${cause.name}: ${cause.message}` : String(cause);
403
+ const head = timedOut
404
+ ? `Mnemoverse: the memory service did not answer ${method} ${path} in time.`
405
+ : `Mnemoverse: the memory service could not be reached at all — ${method} ` +
406
+ `${path} failed before any HTTP response came back.`;
407
+ const cause_ = timedOut
408
+ ? "The service may just be slow right now."
409
+ : "That is a connectivity or DNS problem, or MNEMOVERSE_API_URL pointing at " +
410
+ "a host that does not answer.";
411
+ return (`${head} ${cause_} This is NOT a rejected API key and NOT a quota — no ` +
412
+ "reply arrived to say anything about either, so do not send the user to " +
413
+ "check their key. One retry is reasonable. If that also fails, tell the " +
414
+ "user memory is unreachable and carry on without it rather than retrying.\n\n" +
415
+ `Raw detail — request to ${method} ${path} failed: ${detail}`);
416
+ }
417
+ /**
418
+ * 409: name the conflict this tool actually produced (panel, #93). The two
419
+ * real producers on this server are the join tool (used/expired/revoked
420
+ * invite) and memory_create_room (core: "A room with this name already
421
+ * exists.", 409). The old single sentence gave the invite advice to a
422
+ * duplicate-name conflict, misdirecting the agent to a nonexistent inviter.
423
+ */
424
+ function explain409(f) {
425
+ if (f.path === "/memory/rooms/join") {
426
+ return ("Mnemoverse: this invite code cannot be used (409) — it has already " +
427
+ "been used, has expired, or was revoked. Ask whoever invited the user " +
428
+ "for a fresh one. Retrying the same code will conflict again.");
429
+ }
430
+ if (f.path === "/memory/rooms" && f.method === "POST") {
431
+ return ("Mnemoverse: a room with this name already exists for this account " +
432
+ "(409). Pick a different name, or reuse the existing room — " +
433
+ "memory_list_rooms shows its address. Retrying the same name will " +
434
+ "conflict again.");
435
+ }
436
+ return ("Mnemoverse: this request conflicts with the current state (409). The " +
437
+ "engine's own explanation is in the detail below — quote it to the " +
438
+ "user. Retrying the same request will conflict again.");
439
+ }
440
+ /** {@link explainNetworkFailure} as an error, so `instanceof` can tell a
441
+ * transport failure from an HTTP one without reading either message. */
442
+ export class NetworkError extends Error {
443
+ method;
444
+ path;
445
+ constructor(method, path, cause) {
446
+ super(explainNetworkFailure(method, path, cause), { cause });
447
+ this.name = "NetworkError";
448
+ this.method = method;
449
+ this.path = path;
450
+ }
451
+ }
452
+ /**
453
+ * A failed API call, as an ERROR OBJECT rather than a parsed string.
454
+ *
455
+ * The status and the body are fields because a caller that needs to branch on
456
+ * them must not do it by matching the message. `memory_list_recent` used to
457
+ * decide between "the endpoint is not deployed" and "this room does not exist"
458
+ * with `message.startsWith("Mnemoverse API error 404:")`, which coupled a
459
+ * behavioural branch to the exact prefix of a user-facing sentence — so
460
+ * improving the sentence, which is this change, would have silently flipped
461
+ * that branch. Structured fields make the wording free to change.
462
+ */
463
+ export class ApiError extends Error {
464
+ status;
465
+ body;
466
+ method;
467
+ path;
468
+ /** The engine's envelope, already parsed — so a caller never re-parses. */
469
+ envelope;
470
+ constructor(f) {
471
+ super(explainApiFailure(f));
472
+ this.name = "ApiError";
473
+ this.status = f.status;
474
+ this.body = f.body;
475
+ this.method = f.method;
476
+ this.path = f.path;
477
+ this.envelope = parseErrorEnvelope(f.body);
478
+ }
479
+ /**
480
+ * A 404 the ENGINE did not answer: silence, or the router's literal
481
+ * defaults. This is what a path the deployment does not serve looks like
482
+ * — the first version of this predicate tested silence alone, mistook the
483
+ * real framework body `{"detail":"Not Found"}` for an engine answer, and
484
+ * un-fired the feed's degrade branch for the exact rollout case it exists
485
+ * for (panel, #93).
486
+ *
487
+ * NAMED FOR WHAT IT TESTS, not for what it implies: a gateway, a proxy or a
488
+ * wrong MNEMOVERSE_API_URL can produce the same shapes and is
489
+ * indistinguishable from here. Known, accepted edge: a foreign API whose
490
+ * 404 nests its error under a key this parser does not read
491
+ * (`{"error":{…}}`) also reads as bare, so the feed degrades to its
492
+ * not-supported notice instead of surfacing the foreign body — bounded
493
+ * harm, since against a wrong base URL the very next call fails with the
494
+ * instructive no-envelope wording.
495
+ */
496
+ get isBare404() {
497
+ return this.status === 404 && engineSilentOn404(this.envelope);
498
+ }
499
+ }
500
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0DG;AAEH;;;yDAGyD;AACzD,MAAM,cAAc,GAAG,GAAG,CAAC;AAE3B,4EAA4E;AAC5E,MAAM,QAAQ,GAAG,+CAA+C,CAAC;AAEjE;8EAC8E;AAC9E,MAAM,SAAS,GAAG,gDAAgD,CAAC;AAiCnE;;;;;;;;GAQG;AACH,MAAM,UAAU,kBAAkB,CAAC,IAAY;IAC7C,IAAI,MAAe,CAAC;IACpB,IAAI,CAAC;QACH,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC5B,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,EAAE,CAAC;IACZ,CAAC;IACD,MAAM,IAAI,GAAG,CAAC,CAAU,EAAiB,EAAE;QACzC,IAAI,OAAO,CAAC,KAAK,QAAQ;YAAE,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC;QACjD,IAAI,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,IAAI;YAAE,OAAO,EAAE,CAAC;QACnD,MAAM,CAAC,GAAG,CAA4B,CAAC;QACvC,OAAO;YACL,GAAG,CAAC,OAAO,CAAC,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YACvD,GAAG,CAAC,OAAO,CAAC,CAAC,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