myrmo-mcp 0.25.1 → 0.27.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/README.md +23 -0
- package/dist/doctor.d.ts +7 -0
- package/dist/doctor.js +23 -4
- package/dist/hooks/effort.mjs +124 -0
- package/dist/hooks/lookup.mjs +72 -11
- package/dist/hooks/on-failure.mjs +25 -5
- package/dist/index.js +203 -20
- package/dist/init.d.ts +4 -2
- package/dist/init.js +25 -15
- package/dist/instructions.d.ts +6 -0
- package/dist/instructions.js +10 -0
- package/dist/profile.d.ts +13 -0
- package/dist/profile.js +60 -0
- package/dist/server.d.ts +26 -3
- package/dist/server.js +40 -10
- package/dist/wizard.d.ts +79 -0
- package/dist/wizard.js +220 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -35,6 +35,16 @@ One command for every client on the machine, Claude Code included (shows what it
|
|
|
35
35
|
npx myrmo-mcp init
|
|
36
36
|
```
|
|
37
37
|
|
|
38
|
+
Your organisation's private colony (the address and key you were given):
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
npx myrmo-mcp@latest init --url http://<colony>:8080 --key-file <file with your key> --publish auto
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
A colony since protocol 1.7 says itself that it is an organisation's, so the key is enough; add `--private` for an older
|
|
45
|
+
one. There the agent also asks what colleagues already know when a task starts (`myrmo_brief`) and keeps internal
|
|
46
|
+
tool, service and machine names in what it shares.
|
|
47
|
+
|
|
38
48
|
Agents learn how to use Myrmo from the server itself: it sends the rules (when to search, how to read a
|
|
39
49
|
trail, how to report, when to publish) as soon as it connects.
|
|
40
50
|
|
|
@@ -52,12 +62,25 @@ Other clients (Cursor, Windsurf, Claude Desktop, Gemini CLI; `npx myrmo-mcp init
|
|
|
52
62
|
| `myrmo_report` | Records whether a trail worked. Successes reinforce it, failures weaken it. |
|
|
53
63
|
| `myrmo_publish` | Publishes a verified fix that took at least one failed attempt and that no existing trail solved. The user decides how: the first time they are asked once (always, ask each time, never) and the answer is saved; the hosted server gives them an approval link. Reports the colony's verdict. |
|
|
54
64
|
| `myrmo_publish_status` | Whether the user approved a draft yet, and whether the colony indexed, merged or rejected the trail. |
|
|
65
|
+
| `myrmo_brief` | Private colonies: what the organisation already knows about a task (procedures, facts, fixes). |
|
|
66
|
+
| `myrmo_status` | Which colony this is (public or private), who the server acts as, how publishing is set, and what the colony keeps about you. Sends nothing. |
|
|
67
|
+
|
|
68
|
+
## Commands
|
|
69
|
+
|
|
70
|
+
| Command | What it does |
|
|
71
|
+
|---|---|
|
|
72
|
+
| `npx myrmo-mcp init [--url --key/--key-file --publish --dry-run]` | Sets up every agent client on this machine. |
|
|
73
|
+
| `npx myrmo-mcp doctor` | Checks that this machine can use the colony, and says what to fix. |
|
|
74
|
+
| `npx myrmo-mcp config [setting value]` | Shows or changes a setting: `publish`, `min-failed-attempts`, `hook`, `anonymous`, `effort`. |
|
|
75
|
+
| `npx myrmo-mcp export <dir> [--skill]` | Writes the colony's trails as Markdown notes (an Obsidian vault, or a skill). |
|
|
55
76
|
|
|
56
77
|
## Configuration
|
|
57
78
|
|
|
58
79
|
| Variable | Default | Purpose |
|
|
59
80
|
|---|---|---|
|
|
60
81
|
| `MYRMO_URL` | public colony | Your own colony. |
|
|
82
|
+
| `MYRMO_API_KEY` | none | The access key of a colony that requires one. |
|
|
83
|
+
| `MYRMO_PRIVATE` | unset | `1` for an organisation's colony older than protocol 1.7 (newer ones say so themselves). Never applies to the public colony. |
|
|
61
84
|
| `MYRMO_AGENT_ID` | created by itself | Pseudonymous id that separates your reports from other agents behind the same address. The first run creates a random one in the settings file; this overrides it. |
|
|
62
85
|
| `MYRMO_ANONYMOUS` | unset | `1` sends no agent id. |
|
|
63
86
|
| `MYRMO_PUBLISH` | not chosen yet | `auto`, `ask` or `off`. Overrides the saved choice (`npx myrmo-mcp config publish auto`). |
|
package/dist/doctor.d.ts
CHANGED
|
@@ -12,6 +12,13 @@ export interface DoctorInput {
|
|
|
12
12
|
saved: boolean;
|
|
13
13
|
apiKey?: string;
|
|
14
14
|
privateMode: boolean;
|
|
15
|
+
/** What the colony says it is (GET /v1/version), when it said it. */
|
|
16
|
+
declared?: {
|
|
17
|
+
profile?: string;
|
|
18
|
+
people_detail?: string | null;
|
|
19
|
+
holdout_percent?: number;
|
|
20
|
+
effort?: boolean;
|
|
21
|
+
};
|
|
15
22
|
publish: string;
|
|
16
23
|
nodeVersion?: string;
|
|
17
24
|
home: string;
|
package/dist/doctor.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
import { existsSync, readFileSync } from "node:fs";
|
|
5
5
|
import { join } from "node:path";
|
|
6
6
|
import { claudeCodeSetUp } from "./init.js";
|
|
7
|
-
import { compareVersions, VERSION } from "./server.js";
|
|
7
|
+
import { compareVersions, describeColony, VERSION, VERSION_SHAPE } from "./server.js";
|
|
8
8
|
const PUBLIC_HOSTS = new Set(["myrmo.dev", "www.myrmo.dev"]);
|
|
9
9
|
async function call(fetchImpl, url, key) {
|
|
10
10
|
try {
|
|
@@ -90,7 +90,7 @@ export async function diagnose(input) {
|
|
|
90
90
|
else if (open.status === 401) {
|
|
91
91
|
checks.push(input.apiKey
|
|
92
92
|
? { level: "fail", name: "Your access key", detail: "the colony does not accept it (401)", fix: "The key was copied wrong or has been revoked. Ask for a new one and run the setup command again." }
|
|
93
|
-
: { level: "fail", name: "Your access key", detail: "this colony needs a key and none is set", fix: "Run the setup command you were sent: npx myrmo-mcp init --url <colony> --key <your key> --
|
|
93
|
+
: { level: "fail", name: "Your access key", detail: "this colony needs a key and none is set", fix: "Run the setup command you were sent: npx myrmo-mcp init --url <colony> --key <your key> --publish auto." });
|
|
94
94
|
}
|
|
95
95
|
else if (open.status === 200) {
|
|
96
96
|
checks.push(input.apiKey ? { level: "ok", name: "Your access key", detail: "accepted" } : { level: "warn", name: "Your access key", detail: "this colony does not ask for a key", fix: "If it should be private, tell whoever runs it." });
|
|
@@ -98,9 +98,26 @@ export async function diagnose(input) {
|
|
|
98
98
|
else {
|
|
99
99
|
checks.push({ level: "fail", name: "Your access key", detail: `the colony answered ${open.status}`, fix: "Tell whoever runs the colony." });
|
|
100
100
|
}
|
|
101
|
+
// A key travels in every request: on plain HTTP anyone on the network path can read it.
|
|
102
|
+
try {
|
|
103
|
+
const u = new URL(input.url);
|
|
104
|
+
const local = ["localhost", "127.0.0.1", "::1", "[::1]"].includes(u.hostname);
|
|
105
|
+
if (u.protocol === "http:" && !local && input.apiKey) {
|
|
106
|
+
checks.push({ level: "warn", name: "Encryption", detail: "plain HTTP: your key and what you share cross the network unencrypted", fix: "Fine on a network you trust; elsewhere ask whoever runs the colony to put TLS (HTTPS) in front of it." });
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
/* the URL was checked above */
|
|
111
|
+
}
|
|
112
|
+
const declared = input.declared?.profile;
|
|
101
113
|
checks.push(input.privateMode
|
|
102
|
-
? { level: "ok", name: "Private mode", detail:
|
|
103
|
-
:
|
|
114
|
+
? { level: "ok", name: "Private mode", detail: `agents keep your internal names in what they publish${declared === "organisation" ? " (the colony declares itself your organisation's)" : ""}` }
|
|
115
|
+
: declared === "public"
|
|
116
|
+
? { level: "ok", name: "Private mode", detail: "off: the colony declares itself public, so internal names are removed" }
|
|
117
|
+
: { level: "warn", name: "Private mode", detail: "off: agents would strip internal names from what they publish", fix: declared === "organisation" ? "Set your access key (npx myrmo-mcp init --url <colony> --key <your key>)." : "This colony does not say what it is (an older server): run the setup command again with --private (or set MYRMO_PRIVATE=1)." });
|
|
118
|
+
if (input.declared?.profile === "organisation") {
|
|
119
|
+
checks.push({ level: "ok", name: "What the colony keeps", detail: describeColony(input.declared) });
|
|
120
|
+
}
|
|
104
121
|
checks.push(input.publish === "auto"
|
|
105
122
|
? { level: "ok", name: "Publishing", detail: "agents publish what they solve, to this colony only" }
|
|
106
123
|
: input.publish === "ask"
|
|
@@ -116,6 +133,8 @@ export async function diagnose(input) {
|
|
|
116
133
|
catch {
|
|
117
134
|
latest = undefined;
|
|
118
135
|
}
|
|
136
|
+
if (latest && !VERSION_SHAPE.test(latest))
|
|
137
|
+
latest = undefined;
|
|
119
138
|
if (latest && compareVersions(VERSION, latest) < 0) {
|
|
120
139
|
checks.push({ level: "warn", name: "Myrmo version", detail: `myrmo-mcp ${VERSION} here, ${latest} released with this colony`, fix: "Claude Code: claude plugin update myrmo@myrmo (or /plugin > Marketplaces > myrmo > Enable auto-update). Other clients: npx myrmo-mcp@latest init with the same options as the first time." });
|
|
121
140
|
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// Measured effort: what solving a problem cost this session, read from Claude Code's own transcript, sent to an
|
|
2
|
+
// organisation's colony so it can show what it saves (the Value tab of its panel).
|
|
3
|
+
//
|
|
4
|
+
// Claude Code writes every model response to the session's transcript with its usage (input, output, cache reads and
|
|
5
|
+
// writes). When a problem is solved, the hook adds up the responses between the first failed command and the fix, and
|
|
6
|
+
// sends the sums with the error line the problem started with: `unaided` when the colony had no trail, `aided` when a
|
|
7
|
+
// trail was followed and the agent reported it worked. Only numbers and the model leave the machine besides the error
|
|
8
|
+
// line, which the colony turns into a fingerprint and does not keep.
|
|
9
|
+
//
|
|
10
|
+
// Only to a colony that declares it collects them (an organisation's, `colony.effort` in GET /v1/version). MYRMO_EFFORT=off
|
|
11
|
+
// turns it off for a session, `"effort": "off"` in the settings file for good.
|
|
12
|
+
|
|
13
|
+
import { closeSync, fstatSync, openSync, readSync } from "node:fs";
|
|
14
|
+
import { clientHeader, colonyOf } from "./lookup.mjs";
|
|
15
|
+
|
|
16
|
+
/** Whether this machine sends effort records to its colony. */
|
|
17
|
+
export function effortEnabled(env = process.env, settings = {}) {
|
|
18
|
+
if (env.MYRMO_EFFORT === "off" || settings?.effort === "off") return false;
|
|
19
|
+
const colony = colonyOf(env, settings);
|
|
20
|
+
return colony.private && colony.declared?.effort === true;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The usage of the model responses written between `from` and `to` (milliseconds), from a transcript's JSON lines.
|
|
25
|
+
* A response is written once per content block with the same id and usage, so each id counts once.
|
|
26
|
+
*/
|
|
27
|
+
export function usageBetween(text, from, to) {
|
|
28
|
+
const byId = new Map();
|
|
29
|
+
for (const line of String(text ?? "").split("\n")) {
|
|
30
|
+
if (!line.includes('"usage"')) continue;
|
|
31
|
+
let entry;
|
|
32
|
+
try {
|
|
33
|
+
entry = JSON.parse(line);
|
|
34
|
+
} catch {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
if (entry?.type !== "assistant") continue;
|
|
38
|
+
const at = Date.parse(entry.timestamp);
|
|
39
|
+
if (!Number.isFinite(at) || at < from || at > to) continue;
|
|
40
|
+
const message = entry.message ?? {};
|
|
41
|
+
const usage = message.usage;
|
|
42
|
+
if (!usage || typeof usage !== "object") continue;
|
|
43
|
+
byId.set(message.id ?? entry.uuid ?? `${at}`, { usage, model: typeof message.model === "string" ? message.model : "" });
|
|
44
|
+
}
|
|
45
|
+
const n = (v) => (Number.isFinite(Number(v)) && Number(v) > 0 ? Math.floor(Number(v)) : 0);
|
|
46
|
+
const sums = { input_tokens: 0, output_tokens: 0, cache_read_tokens: 0, cache_write_tokens: 0, turns: 0 };
|
|
47
|
+
const models = new Map();
|
|
48
|
+
for (const { usage, model } of byId.values()) {
|
|
49
|
+
sums.input_tokens += n(usage.input_tokens);
|
|
50
|
+
sums.output_tokens += n(usage.output_tokens);
|
|
51
|
+
sums.cache_read_tokens += n(usage.cache_read_input_tokens);
|
|
52
|
+
sums.cache_write_tokens += n(usage.cache_creation_input_tokens);
|
|
53
|
+
sums.turns += 1;
|
|
54
|
+
if (model) models.set(model, (models.get(model) ?? 0) + 1);
|
|
55
|
+
}
|
|
56
|
+
const model = [...models.entries()].sort((a, b) => b[1] - a[1])[0]?.[0];
|
|
57
|
+
return { ...sums, model };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const CAP = 10_000_000;
|
|
61
|
+
|
|
62
|
+
/** The record for POST /v1/effort, or null when there is nothing worth sending. */
|
|
63
|
+
export function effortRecord({ role, line, trailId, from, to, usage }) {
|
|
64
|
+
if (!line || String(line).length < 8 || !usage || usage.turns === 0) return null;
|
|
65
|
+
if (role === "aided" && !trailId) return null;
|
|
66
|
+
const clip = (v) => Math.min(CAP, v);
|
|
67
|
+
return {
|
|
68
|
+
protocol_version: "1.7",
|
|
69
|
+
role,
|
|
70
|
+
error: String(line).slice(0, 2000),
|
|
71
|
+
...(role === "aided" ? { trail_id: trailId } : {}),
|
|
72
|
+
effort: {
|
|
73
|
+
input_tokens: clip(usage.input_tokens),
|
|
74
|
+
output_tokens: clip(usage.output_tokens),
|
|
75
|
+
cache_read_tokens: clip(usage.cache_read_tokens),
|
|
76
|
+
cache_write_tokens: clip(usage.cache_write_tokens),
|
|
77
|
+
turns: Math.min(10_000, usage.turns),
|
|
78
|
+
wall_time_seconds: Math.max(0, Math.min(86_400, Math.round((to - from) / 1000))),
|
|
79
|
+
},
|
|
80
|
+
agent_info: { model: usage.model || "unknown", framework: "claude-code" },
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Sends one record; true when the colony took it. Never throws. */
|
|
85
|
+
export async function sendEffort(record, { env = process.env, settings = {}, fetcher = fetch, timeoutMs = 4000 } = {}) {
|
|
86
|
+
const colony = colonyOf(env, settings);
|
|
87
|
+
const headers = { "content-type": "application/json", "user-agent": "myrmo-claude-code-hook/1", ...clientHeader() };
|
|
88
|
+
if (env.MYRMO_AGENT_ID) headers["x-myrmo-agent"] = env.MYRMO_AGENT_ID;
|
|
89
|
+
if (colony.apiKey) headers.authorization = `Bearer ${colony.apiKey}`;
|
|
90
|
+
try {
|
|
91
|
+
const res = await fetcher(`${colony.url}/v1/effort`, { method: "POST", headers, body: JSON.stringify(record), signal: AbortSignal.timeout(timeoutMs) });
|
|
92
|
+
return res.ok;
|
|
93
|
+
} catch {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** The last `max` bytes of a file: a problem is solved within the hour, and a long session's transcript can be huge. */
|
|
99
|
+
export function readTail(path, max = 32 * 1024 * 1024) {
|
|
100
|
+
const fd = openSync(path, "r");
|
|
101
|
+
try {
|
|
102
|
+
const size = fstatSync(fd).size;
|
|
103
|
+
const length = Math.min(size, max);
|
|
104
|
+
const buf = Buffer.alloc(length);
|
|
105
|
+
readSync(fd, buf, 0, length, size - length);
|
|
106
|
+
return buf.toString("utf8");
|
|
107
|
+
} finally {
|
|
108
|
+
closeSync(fd);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Reads the transcript and sends the record for a solved problem, when this machine sends them. */
|
|
113
|
+
export async function measureAndSend(event, transcriptPath, { env = process.env, settings = {}, fetcher = fetch, now = Date.now() } = {}) {
|
|
114
|
+
if (!event || !transcriptPath || !effortEnabled(env, settings)) return false;
|
|
115
|
+
let text = "";
|
|
116
|
+
try {
|
|
117
|
+
text = readTail(transcriptPath);
|
|
118
|
+
} catch {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
const to = event.to ?? now;
|
|
122
|
+
const record = effortRecord({ ...event, to, usage: usageBetween(text, event.from, to) });
|
|
123
|
+
return record ? sendEffort(record, { env, settings, fetcher }) : false;
|
|
124
|
+
}
|
package/dist/hooks/lookup.mjs
CHANGED
|
@@ -7,14 +7,69 @@
|
|
|
7
7
|
// --url` writes, whose key and private mode apply only to that very URL. For another colony of one's own it is opt-in
|
|
8
8
|
// with MYRMO_HOOK_LOOKUP=1; it never runs against the public colony, and MYRMO_HOOK_LOOKUP=0 turns it off everywhere.
|
|
9
9
|
|
|
10
|
-
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
11
|
-
import {
|
|
10
|
+
import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
11
|
+
import { homedir } from "node:os";
|
|
12
|
+
import { dirname, join } from "node:path";
|
|
12
13
|
|
|
13
14
|
const PUBLIC_HOSTS = new Set(["myrmo.dev", "www.myrmo.dev"]);
|
|
14
15
|
const trim = (url) => String(url ?? "").trim().replace(/\/+$/, "");
|
|
15
16
|
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
// --- what the colony says it is (GET /v1/version, `colony`, protocol 1.7) -------------------------------------------
|
|
18
|
+
//
|
|
19
|
+
// Remembered per URL in colonies.json next to the settings file, the same file the MCP server writes.
|
|
20
|
+
|
|
21
|
+
/** Where the colonies' declarations are kept. */
|
|
22
|
+
export function profilesPath(env = process.env) {
|
|
23
|
+
return join(dirname(env.MYRMO_CONFIG?.trim() || join(homedir(), ".myrmo", "config.json")), "colonies.json");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Only the fields the hooks act on, with the types they expect: the file and the colony's answer are untrusted. */
|
|
27
|
+
export function cleanDeclared(value) {
|
|
28
|
+
if (!value || typeof value !== "object" || (value.profile !== "public" && value.profile !== "organisation")) return undefined;
|
|
29
|
+
return {
|
|
30
|
+
profile: value.profile,
|
|
31
|
+
effort: value.effort === true,
|
|
32
|
+
task_search: value.task_search === true,
|
|
33
|
+
people_detail: value.people_detail === "named" || value.people_detail === "aggregate" ? value.people_detail : null,
|
|
34
|
+
holdout_percent: Number.isInteger(value.holdout_percent) && value.holdout_percent >= 0 && value.holdout_percent <= 20 ? value.holdout_percent : 0,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function readDeclared(url, env = process.env) {
|
|
39
|
+
try {
|
|
40
|
+
return cleanDeclared(JSON.parse(readFileSync(profilesPath(env), "utf8"))?.[trim(url)]?.colony);
|
|
41
|
+
} catch {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function rememberDeclared(url, colony, env = process.env, now = Date.now()) {
|
|
47
|
+
const clean = cleanDeclared(colony);
|
|
48
|
+
if (!clean) return;
|
|
49
|
+
const file = profilesPath(env);
|
|
50
|
+
let all = {};
|
|
51
|
+
try {
|
|
52
|
+
all = JSON.parse(readFileSync(file, "utf8")) ?? {};
|
|
53
|
+
} catch {
|
|
54
|
+
all = {};
|
|
55
|
+
}
|
|
56
|
+
all[trim(url)] = { colony: clean, at: new Date(now).toISOString() };
|
|
57
|
+
try {
|
|
58
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
59
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
60
|
+
writeFileSync(tmp, JSON.stringify(all, null, 2), { mode: 0o600 });
|
|
61
|
+
renameSync(tmp, file);
|
|
62
|
+
} catch {
|
|
63
|
+
/* asked again next time */
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The colony the hooks talk to: url, key and private mode, by the same rules as the MCP server. A colony that declares
|
|
69
|
+
* itself an organisation's is private for a person with a key for it; one that declares itself public never is,
|
|
70
|
+
* whatever the flags say; without a declaration the person's flag decides (MYRMO_PRIVATE=1, or `--private` saved by init).
|
|
71
|
+
*/
|
|
72
|
+
export function colonyOf(env = process.env, settings = {}, declared) {
|
|
18
73
|
const saved = settings?.colony && typeof settings.colony.url === "string" ? settings.colony : null;
|
|
19
74
|
const url = trim(env.MYRMO_URL || saved?.url || "https://myrmo.dev");
|
|
20
75
|
const same = Boolean(saved) && trim(saved.url) === url;
|
|
@@ -24,12 +79,11 @@ export function colonyOf(env = process.env, settings = {}) {
|
|
|
24
79
|
} catch {
|
|
25
80
|
own = false;
|
|
26
81
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
};
|
|
82
|
+
const apiKey = env.MYRMO_API_KEY || (same && typeof saved.api_key === "string" ? saved.api_key : undefined);
|
|
83
|
+
const explicit = env.MYRMO_PRIVATE === "1" || (same && saved.private === true);
|
|
84
|
+
const said = declared === undefined && own ? readDeclared(url, env) : declared;
|
|
85
|
+
const isPrivate = !own ? false : said?.profile === "public" ? false : said?.profile === "organisation" ? explicit || Boolean(apiKey) : explicit;
|
|
86
|
+
return { url, apiKey, own, private: isPrivate, declared: said };
|
|
33
87
|
}
|
|
34
88
|
|
|
35
89
|
/** Whether the colony is an organisation's own (the MCP server's private mode, same rule). */
|
|
@@ -248,13 +302,20 @@ export async function updateNotice({ env = process.env, settings = {}, now = Dat
|
|
|
248
302
|
cache = {};
|
|
249
303
|
}
|
|
250
304
|
const day = 24 * 3600_000;
|
|
305
|
+
const VERSION = /^\d{1,4}\.\d{1,4}\.\d{1,6}(?:-[0-9A-Za-z.]{1,20})?$/;
|
|
251
306
|
let latest = cache.url === colony.url && now - Number(cache.at ?? 0) < day ? cache.latest : undefined;
|
|
307
|
+
if (typeof latest === "string" && !VERSION.test(latest)) latest = undefined;
|
|
252
308
|
if (latest === undefined) {
|
|
253
309
|
const headers = { accept: "application/json", "user-agent": "myrmo-claude-code-hook/1", ...clientHeader(current) };
|
|
254
310
|
if (colony.apiKey) headers.authorization = `Bearer ${colony.apiKey}`;
|
|
255
311
|
try {
|
|
256
312
|
const res = await fetcher(`${colony.url}/v1/version`, { headers, signal: AbortSignal.timeout(timeoutMs) });
|
|
257
|
-
|
|
313
|
+
const body = res.ok ? await res.json() : null;
|
|
314
|
+
// Shown to the person: only something shaped like a version, never words a colony chose.
|
|
315
|
+
const said = body?.clients?.plugin;
|
|
316
|
+
latest = typeof said === "string" && /^\d{1,4}\.\d{1,4}\.\d{1,6}(?:-[0-9A-Za-z.]{1,20})?$/.test(said) ? said : null;
|
|
317
|
+
// What the colony is, for the hooks and the MCP server's next start.
|
|
318
|
+
if (body?.colony) rememberDeclared(colony.url, body.colony, env);
|
|
258
319
|
} catch {
|
|
259
320
|
latest = null;
|
|
260
321
|
}
|
|
@@ -25,6 +25,9 @@
|
|
|
25
25
|
// it relied on and to share what the next person's agent would need (Stop hook): what is learned in a session
|
|
26
26
|
// otherwise leaves with it.
|
|
27
27
|
//
|
|
28
|
+
// 9. When a problem is solved, what it cost (tokens, time, model, from the session's transcript) is sent to a colony
|
|
29
|
+
// that collects it, to measure what the colony saves (effort.mjs). MYRMO_EFFORT=off turns it off.
|
|
30
|
+
//
|
|
28
31
|
// And on any colony:
|
|
29
32
|
// 8. When a session starts, the person is told once a day if their colony was released with a newer Myrmo plugin
|
|
30
33
|
// (SessionStart; lookup.mjs `updateNotice`). MYRMO_UPDATE_CHECK=off turns it off.
|
|
@@ -36,6 +39,7 @@
|
|
|
36
39
|
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
37
40
|
import { homedir, tmpdir } from "node:os";
|
|
38
41
|
import { dirname, join } from "node:path";
|
|
42
|
+
import { measureAndSend } from "./effort.mjs";
|
|
39
43
|
import { briefEnabled, briefNote, lookup, lookupEnabled, lookupNote, lookupTask, privateColony, taskOf, updateNotice } from "./lookup.mjs";
|
|
40
44
|
|
|
41
45
|
// Commands that fail or print errors as part of normal work: a probe that found nothing, a diff that differs, a log
|
|
@@ -246,6 +250,10 @@ function quiet(state) {
|
|
|
246
250
|
return { note: null, state };
|
|
247
251
|
}
|
|
248
252
|
|
|
253
|
+
/** A problem stays open for 45 minutes after its first failed command: what was spent since then is what it cost. */
|
|
254
|
+
const PROBLEM_MS = 45 * 60_000;
|
|
255
|
+
const openProblem = (state, now) => (state.problem && now - state.problem.at < PROBLEM_MS ? state.problem : null);
|
|
256
|
+
|
|
249
257
|
export function decide(input, { now = Date.now(), state = {}, env = process.env, settings = {} } = {}) {
|
|
250
258
|
const mode = hookMode(env, settings);
|
|
251
259
|
if (mode === "off") return quiet(state);
|
|
@@ -264,10 +272,16 @@ export function decide(input, { now = Date.now(), state = {}, env = process.env,
|
|
|
264
272
|
}
|
|
265
273
|
return quiet(state);
|
|
266
274
|
}
|
|
267
|
-
// 4b. The agent reported: nothing left to remind.
|
|
275
|
+
// 4b. The agent reported: nothing left to remind. A trail that worked for the open problem: what it cost with help.
|
|
268
276
|
if (REPORT_TOOL.test(String(tool))) {
|
|
269
277
|
const brief = state.brief ? { ...state.brief, reported: true } : state.brief;
|
|
270
|
-
|
|
278
|
+
const outcome = String(input.tool_input?.outcome ?? "");
|
|
279
|
+
const trailId = String(input.tool_input?.trail_id ?? "").toLowerCase();
|
|
280
|
+
const problem = openProblem(state, now);
|
|
281
|
+
const helped = problem && /^[0-9a-f-]{36}$/.test(trailId) && (outcome === "worked" || outcome === "partially_worked");
|
|
282
|
+
const effort = helped ? { role: "aided", line: problem.line, trailId, from: problem.at, to: state.found?.solvedAt ?? now } : null;
|
|
283
|
+
const next = state.found || state.brief || helped ? { ...state, found: null, brief, ...(helped ? { problem: null } : {}) } : state;
|
|
284
|
+
return { note: null, state: next, effort };
|
|
271
285
|
}
|
|
272
286
|
if (!SHELLS.has(tool)) return quiet(state);
|
|
273
287
|
|
|
@@ -292,7 +306,8 @@ export function decide(input, { now = Date.now(), state = {}, env = process.env,
|
|
|
292
306
|
// A company's own tools print errors in no known shape (`migrate: lock 'migrate' is held by ci-runner-17`), and those
|
|
293
307
|
// are what a private colony is for: for a command that failed, its last line is the best guess.
|
|
294
308
|
const line = lastErrorLine(output, { loose: true }) || lastLine(output);
|
|
295
|
-
const
|
|
309
|
+
const problem = openProblem(state, now) ?? (line ? { at: now, line } : null);
|
|
310
|
+
const next = { ...state, failed: { key: commandKey(command), at: now, line }, failures: (state.failures ?? 0) + 1, problem };
|
|
296
311
|
const sig = signature(line);
|
|
297
312
|
if (spent || tooSoon || (sig && seen.includes(sig))) return quiet(next);
|
|
298
313
|
const exit = Number.isFinite(code) ? ` (exit ${code})` : "";
|
|
@@ -321,7 +336,10 @@ export function decide(input, { now = Date.now(), state = {}, env = process.env,
|
|
|
321
336
|
`(not in this project's own code), publish it with myrmo_publish: the user sees exactly what would be sent and approves it. ` +
|
|
322
337
|
`Describe where the error happened (say so if it was inside a container) and keep out names of people, companies, customers and internal systems. ` +
|
|
323
338
|
`Skip it if the fix was obvious or specific to this project.`;
|
|
324
|
-
|
|
339
|
+
// Solved without a trail: what it cost without help.
|
|
340
|
+
const problem = openProblem(state, now);
|
|
341
|
+
const effort = problem ? { role: "unaided", line: problem.line, from: problem.at, to: now } : null;
|
|
342
|
+
return { note, state: { ...state, nomatch: { ...nomatch, nudged: true }, count: (state.count ?? 0) + 1, problem: null }, effort };
|
|
325
343
|
}
|
|
326
344
|
|
|
327
345
|
// 4c. A command that failed earlier now works, and Myrmo had returned trails for it: ask for the outcome.
|
|
@@ -332,7 +350,7 @@ export function decide(input, { now = Date.now(), state = {}, env = process.env,
|
|
|
332
350
|
`Call myrmo_report once for the trail you followed: worked if it gave you the fix, partially_worked if it helped only in part, ` +
|
|
333
351
|
`failed if you tried it and it did not help, not_applicable if it was about something else. ` +
|
|
334
352
|
`It takes one call and is how good trails rise and stale ones fade. Skip it if you already reported.`;
|
|
335
|
-
return { note, state: { ...state, found: { ...found, nudged: true }, count: (state.count ?? 0) + 1 } };
|
|
353
|
+
return { note, state: { ...state, found: { ...found, nudged: true, solvedAt: now }, count: (state.count ?? 0) + 1 } };
|
|
336
354
|
}
|
|
337
355
|
|
|
338
356
|
// 2. Exit 0, but the output looks like an error.
|
|
@@ -463,6 +481,8 @@ async function main() {
|
|
|
463
481
|
if (body) Object.assign(out, lookupNote(out.state.failed.line, body, out.state));
|
|
464
482
|
}
|
|
465
483
|
save(file, state, out.state);
|
|
484
|
+
// 9. A problem was solved: what it cost, to a colony that collects it.
|
|
485
|
+
if (out.effort) await measureAndSend(out.effort, input.transcript_path, { settings });
|
|
466
486
|
if (!out.note) return;
|
|
467
487
|
process.stdout.write(JSON.stringify({ hookSpecificOutput: { hookEventName: input.hook_event_name ?? "PostToolUseFailure", additionalContext: out.note } }));
|
|
468
488
|
}
|