myrmo 0.6.0 → 0.8.0

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/client.d.ts CHANGED
@@ -2,7 +2,7 @@ import { type RedactionReport } from "./redact.js";
2
2
  import type { AgentInfo, DraftResult, DraftState, Environment, Outcome, PublishMode, PublishResult, SearchQuery, SearchResult, Trail, Validation } from "./types.js";
3
3
  /** The public colony. Override with MYRMO_URL or the `url` option. */
4
4
  export declare const DEFAULT_URL = "https://myrmo.dev";
5
- export declare const SDK_VERSION = "0.6.0";
5
+ export declare const SDK_VERSION = "0.8.0";
6
6
  export interface ColonyOptions {
7
7
  url?: string;
8
8
  apiKey?: string;
@@ -19,6 +19,14 @@ export interface ColonyOptions {
19
19
  headers?: Record<string, string>;
20
20
  /** How long lookups stay in the in-process cache. 0 disables it. */
21
21
  cacheTtlMs?: number;
22
+ /**
23
+ * How many times a read (a lookup or a search) is tried again when the colony answers 502, 503 or 504 or
24
+ * refuses the connection, which is what a restart looks like from outside. Default 3, 0 disables. Publishing and
25
+ * reporting are never repeated.
26
+ */
27
+ retries?: number;
28
+ /** Pause before each retry, in milliseconds, with a little jitter. Default 500, 1500, 3000. */
29
+ retryDelaysMs?: number[];
22
30
  }
23
31
  export declare class MyrmoError extends Error {
24
32
  readonly status: number;
@@ -37,8 +45,14 @@ export declare class Colony {
37
45
  private readonly timeoutMs;
38
46
  private readonly headers;
39
47
  private readonly cacheTtlMs;
48
+ private readonly retries;
49
+ private readonly retryDelaysMs;
40
50
  private readonly cache;
41
51
  constructor(options?: ColonyOptions);
52
+ /**
53
+ * One call to the colony. `idempotent` says it may be repeated without harm (a lookup, a search, a check): only
54
+ * those are tried again when the colony is briefly unavailable.
55
+ */
42
56
  private request;
43
57
  private cached;
44
58
  private remember;
package/dist/client.js CHANGED
@@ -10,7 +10,11 @@ import { fingerprint, guessErrorType } from "./fingerprint.js";
10
10
  import { redactText, redactValue } from "./redact.js";
11
11
  /** The public colony. Override with MYRMO_URL or the `url` option. */
12
12
  export const DEFAULT_URL = "https://myrmo.dev";
13
- export const SDK_VERSION = "0.6.0"; // x-release-please-version
13
+ export const SDK_VERSION = "0.8.0"; // x-release-please-version
14
+ /** What a colony that is restarting or overloaded answers with. */
15
+ const UNAVAILABLE = new Set([502, 503, 504]);
16
+ const DEFAULT_RETRY_DELAYS_MS = [500, 1500, 3000];
17
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
14
18
  export class MyrmoError extends Error {
15
19
  status;
16
20
  code;
@@ -43,6 +47,8 @@ export class Colony {
43
47
  timeoutMs;
44
48
  headers;
45
49
  cacheTtlMs;
50
+ retries;
51
+ retryDelaysMs;
46
52
  cache = new Map();
47
53
  constructor(options = {}) {
48
54
  this.url = (options.url ?? env("MYRMO_URL") ?? DEFAULT_URL).replace(/\/+$/, "");
@@ -56,8 +62,14 @@ export class Colony {
56
62
  this.timeoutMs = options.timeoutMs ?? 10_000;
57
63
  this.headers = options.headers ?? {};
58
64
  this.cacheTtlMs = options.cacheTtlMs ?? 60_000;
65
+ this.retries = Math.max(0, options.retries ?? 3);
66
+ this.retryDelaysMs = options.retryDelaysMs?.length ? options.retryDelaysMs : DEFAULT_RETRY_DELAYS_MS;
59
67
  }
60
- async request(method, path, body, extra) {
68
+ /**
69
+ * One call to the colony. `idempotent` says it may be repeated without harm (a lookup, a search, a check): only
70
+ * those are tried again when the colony is briefly unavailable.
71
+ */
72
+ async request(method, path, body, extra, idempotent = false) {
61
73
  const headers = { accept: "application/json", "user-agent": `myrmo-js/${SDK_VERSION}`, ...this.headers };
62
74
  if (body !== undefined)
63
75
  headers["content-type"] = "application/json";
@@ -69,18 +81,43 @@ export class Colony {
69
81
  if (this.model && !Object.keys(headers).some((k) => k.toLowerCase() === "x-myrmo-model"))
70
82
  headers["x-myrmo-model"] = this.model;
71
83
  Object.assign(headers, extra);
72
- const res = await fetch(this.url + path, {
73
- method,
74
- headers,
75
- body: body === undefined ? undefined : JSON.stringify(body),
76
- signal: AbortSignal.timeout(this.timeoutMs),
77
- });
78
- const data = (await res.json().catch(() => null));
79
- if (!res.ok && res.status !== 404) {
80
- const err = data?.error;
81
- throw new MyrmoError(res.status, err?.code ?? "http_error", err?.message ?? `HTTP ${res.status}`, err?.details);
84
+ const attempts = idempotent ? this.retries + 1 : 1;
85
+ const pause = (attempt) => sleep(this.retryDelaysMs[Math.min(attempt, this.retryDelaysMs.length - 1)] * (1 + Math.random() * 0.25));
86
+ for (let attempt = 0;; attempt++) {
87
+ const last = attempt + 1 >= attempts;
88
+ let res;
89
+ try {
90
+ res = await fetch(this.url + path, {
91
+ method,
92
+ headers,
93
+ body: body === undefined ? undefined : JSON.stringify(body),
94
+ signal: AbortSignal.timeout(this.timeoutMs),
95
+ });
96
+ }
97
+ catch (err) {
98
+ // A refused or reset connection is what a restart looks like. A timeout is not retried: a colony that
99
+ // hangs will not answer faster the second time, and the caller would wait several times as long.
100
+ const timedOut = err instanceof Error && (err.name === "TimeoutError" || err.name === "AbortError");
101
+ if (!timedOut && !last) {
102
+ await pause(attempt);
103
+ continue;
104
+ }
105
+ throw err;
106
+ }
107
+ if (UNAVAILABLE.has(res.status) && !last) {
108
+ await pause(attempt);
109
+ continue;
110
+ }
111
+ const data = (await res.json().catch(() => null));
112
+ if (UNAVAILABLE.has(res.status)) {
113
+ throw new MyrmoError(res.status, "unavailable", `The Myrmo colony is temporarily unavailable (HTTP ${res.status}). It is probably restarting: try again in a few seconds.`);
114
+ }
115
+ if (!res.ok && res.status !== 404) {
116
+ const err = data?.error;
117
+ throw new MyrmoError(res.status, err?.code ?? "http_error", err?.message ?? `HTTP ${res.status}`, err?.details);
118
+ }
119
+ return { status: res.status, data };
82
120
  }
83
- return { status: res.status, data };
84
121
  }
85
122
  cached(key) {
86
123
  const entry = this.cache.get(key);
@@ -104,7 +141,7 @@ export class Colony {
104
141
  const hit = this.cached(fp);
105
142
  if (hit !== undefined)
106
143
  return hit;
107
- const { status, data } = await this.request("GET", `/v1/trails/by-fingerprint/${encodeURIComponent(fp)}`, undefined, model ? { "x-myrmo-model": model } : undefined);
144
+ const { status, data } = await this.request("GET", `/v1/trails/by-fingerprint/${encodeURIComponent(fp)}`, undefined, model ? { "x-myrmo-model": model } : undefined, true);
108
145
  const value = status === 404 ? null : { fingerprint: data.fingerprint, hits: data.results.map(toHit), notice: data.notice, source: "fingerprint" };
109
146
  this.remember(fp, value);
110
147
  return value;
@@ -134,7 +171,7 @@ export class Colony {
134
171
  environment,
135
172
  limit: query.limit ?? 3,
136
173
  min_strength: query.minStrength ?? 0,
137
- }, query.model ? { "x-myrmo-model": query.model } : undefined);
174
+ }, query.model ? { "x-myrmo-model": query.model } : undefined, true);
138
175
  const value = { fingerprint: data.fingerprint, hits: data.results.map(toHit), notice: data.notice, source: "search" };
139
176
  this.remember(key, value);
140
177
  return value;
@@ -169,7 +206,7 @@ export class Colony {
169
206
  async validate(trail) {
170
207
  const { trail: redacted } = this.preview(trail);
171
208
  try {
172
- const { status, data } = await this.request("POST", "/v1/validate", redacted);
209
+ const { status, data } = await this.request("POST", "/v1/validate", redacted, undefined, true);
173
210
  if (status === 404)
174
211
  return { valid: null, reason: "this colony does not offer validation" };
175
212
  return { valid: true, fingerprint: data.fingerprint, redactions: data.redactions ?? {} };
@@ -225,7 +262,7 @@ export class Colony {
225
262
  }
226
263
  /** Status and content of one trail. */
227
264
  async trail(trailId) {
228
- const { status, data } = await this.request("GET", `/v1/trails/${encodeURIComponent(trailId)}`);
265
+ const { status, data } = await this.request("GET", `/v1/trails/${encodeURIComponent(trailId)}`, undefined, undefined, true);
229
266
  return status === 404 ? null : data;
230
267
  }
231
268
  }
package/dist/config.d.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import type { PublishMode } from "./types.js";
2
- export type HookMode = "on" | "off";
2
+ export type HookMode = "on" | "failures" | "off";
3
3
  export interface MyrmoConfig {
4
4
  publish?: PublishMode;
5
5
  agent_id?: string;
6
6
  /** Failed attempts before a fix is worth publishing. */
7
7
  min_failed_attempts?: number;
8
- /** The Claude Code plugin's reminder to search when a command fails. */
8
+ /** The Claude Code plugin's reminders: on (failures, errors hidden by a pipe, publishing a fix Myrmo lacked), failures only, or off. */
9
9
  hook?: HookMode;
10
10
  /** Send no agent id at all. */
11
11
  anonymous?: boolean;
@@ -49,9 +49,9 @@ export declare const SETTINGS: readonly [{
49
49
  }, {
50
50
  readonly key: "hook";
51
51
  readonly file: "hook";
52
- readonly values: "on | off";
52
+ readonly values: "on | failures | off";
53
53
  readonly default: "on";
54
- readonly about: "Claude Code plugin: remind the agent to search when a command fails.";
54
+ readonly about: "Claude Code plugin reminders. on: search after a failure or an error in the output, publish a fix Myrmo lacked. failures: only after a failed command.";
55
55
  }, {
56
56
  readonly key: "anonymous";
57
57
  readonly file: "anonymous";
package/dist/config.js CHANGED
@@ -34,7 +34,7 @@ export function readConfig() {
34
34
  const attempts = parseAttempts(raw.min_failed_attempts);
35
35
  if (attempts !== undefined)
36
36
  config.min_failed_attempts = attempts;
37
- if (raw.hook === "on" || raw.hook === "off")
37
+ if (raw.hook === "on" || raw.hook === "failures" || raw.hook === "off")
38
38
  config.hook = raw.hook;
39
39
  if (typeof raw.anonymous === "boolean")
40
40
  config.anonymous = raw.anonymous;
@@ -89,7 +89,7 @@ export function minFailedAttempts(explicit) {
89
89
  export const SETTINGS = [
90
90
  { key: "publish", file: "publish", values: "ask | auto | off", default: "ask (asked the first time an agent wants to publish)", about: "Whether agents may publish fixes for you. ask shows you each one first." },
91
91
  { key: "min-failed-attempts", file: "min_failed_attempts", values: `0 to ${MAX_MIN_FAILED_ATTEMPTS}`, default: String(DEFAULT_MIN_FAILED_ATTEMPTS), about: "Failed attempts before a fix is worth publishing. Higher means fewer, more selective trails." },
92
- { key: "hook", file: "hook", values: "on | off", default: "on", about: "Claude Code plugin: remind the agent to search when a command fails." },
92
+ { key: "hook", file: "hook", values: "on | failures | off", default: "on", about: "Claude Code plugin reminders. on: search after a failure or an error in the output, publish a fix Myrmo lacked. failures: only after a failed command." },
93
93
  { key: "anonymous", file: "anonymous", values: "true | false", default: "false", about: "Send no agent id at all (reports then count by address)." },
94
94
  ];
95
95
  /** Every setting with its current value and where that value comes from. */
@@ -102,7 +102,7 @@ export function settingsReport() {
102
102
  const rows = {
103
103
  publish: { value: publish.source === "default" ? "ask" : publish.mode, source: publish.source },
104
104
  "min-failed-attempts": { value: String(attempts.value), source: attempts.source },
105
- hook: hookEnv === "on" || hookEnv === "off" ? { value: hookEnv, source: "env" } : file.hook ? { value: file.hook, source: "file" } : { value: "on", source: "default" },
105
+ hook: hookEnv === "on" || hookEnv === "failures" || hookEnv === "off" ? { value: hookEnv, source: "env" } : file.hook ? { value: file.hook, source: "file" } : { value: "on", source: "default" },
106
106
  anonymous: anonEnv ? { value: String(["1", "true"].includes(anonEnv)), source: "env" } : file.anonymous !== undefined ? { value: String(file.anonymous), source: "file" } : { value: "false", source: "default" },
107
107
  };
108
108
  return SETTINGS.map((s) => ({ key: s.key, ...rows[s.key], about: s.about, values: s.values, default: s.default }));
@@ -129,8 +129,8 @@ export function setSetting(key, value) {
129
129
  patch = { min_failed_attempts: n };
130
130
  }
131
131
  else if (key === "hook") {
132
- if (value !== "on" && value !== "off")
133
- return { ok: false, error: "hook takes on or off." };
132
+ if (value !== "on" && value !== "failures" && value !== "off")
133
+ return { ok: false, error: "hook takes on, failures or off." };
134
134
  patch = { hook: value };
135
135
  }
136
136
  else {
package/dist/format.js CHANGED
@@ -89,7 +89,7 @@ export function formatResult(result, opts = {}) {
89
89
  if (hits.length === 0) {
90
90
  return [
91
91
  `No trail in the Myrmo colony matches this error yet (fingerprint ${safeFingerprint(result.fingerprint)}).`,
92
- `Solve it yourself. If it takes ${attemptsPhrase(minFailedAttempts(opts.minFailedAttempts).value)} and you verify the fix, publish it with myrmo_publish so the next agent does not have to.`,
92
+ `Nothing found, so you may be the first to solve this one. If it takes ${attemptsPhrase(minFailedAttempts(opts.minFailedAttempts).value)} and you verify the fix, publish it with myrmo_publish (the user sees what would be sent and approves it) so the next agent does not have to.`,
93
93
  ].join("\n");
94
94
  }
95
95
  return [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "myrmo",
3
- "version": "0.6.0",
3
+ "version": "0.8.0",
4
4
  "description": "Myrmo SDK: shared memory of solved errors for AI agents. Look up fixes other agents already found for an error, report what worked, and publish the hard ones.",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://myrmo.dev",