lakonai 1.2.3 → 1.2.5
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 +25 -5
- package/bin/lakonai.js +51 -1
- package/package.json +1 -1
- package/src/install/atomic.js +46 -0
- package/src/install/claude-hook.js +4 -1
- package/src/install/mcp.js +147 -13
- package/src/install/platforms.js +11 -4
- package/src/proxy/state.js +32 -15
package/README.md
CHANGED
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
<img src="https://img.shields.io/badge/node-%E2%89%A518-0F0F0F" alt="node ≥18" />
|
|
21
21
|
<img src="https://img.shields.io/badge/deps-0-0F0F0F" alt="zero dependencies" />
|
|
22
22
|
<img src="https://img.shields.io/badge/agents-6-0F0F0F" alt="6 AI agents" />
|
|
23
|
-
<img src="https://img.shields.io/badge/tests-
|
|
23
|
+
<img src="https://img.shields.io/badge/tests-832-0F0F0F" alt="832 tests" />
|
|
24
24
|
</p>
|
|
25
25
|
|
|
26
26
|
<p align="center">
|
|
@@ -89,7 +89,7 @@ All numbers from live projects using `lakonai@1.1.1`.
|
|
|
89
89
|
| **Proxy** | Every API request carries raw bloat | Local HTTP proxy compresses request bodies before they reach Anthropic |
|
|
90
90
|
| **Graph** | File reads load entire source files | AST knowledge graph: serves a compact subgraph instead of the raw file (-87%) |
|
|
91
91
|
| **Pixel** | Skill files are verbose markdown | Converts skill bodies to PNG — vision tokens cheaper than text tokens |
|
|
92
|
-
| **Context** | MCP catalogs + memory re-paid every turn | Auto-compresses MCP catalogs; `compress-memory` via local AI CLI |
|
|
92
|
+
| **Context** | MCP catalogs + memory re-paid every turn | Auto-compresses MCP catalogs (`lakonai mcp`); `compress-memory` via local AI CLI |
|
|
93
93
|
|
|
94
94
|
Auto-learning runs underneath all of this: lakonai detects new heavy commands and enables a safe filter automatically, no config. ([full reference →](docs/reference.md))
|
|
95
95
|
|
|
@@ -175,8 +175,9 @@ accumulate in `~/.lakon/proxy-stats.json`.
|
|
|
175
175
|
`ANTHROPIC_BASE_URL` directly — it sources `~/.lakon/proxy-env.sh`, which checks
|
|
176
176
|
that the proxy is actually listening before pointing anything at it. Proxy down
|
|
177
177
|
(reboot, crash, `lakonai proxy stop`) means no compression, and Claude talks to
|
|
178
|
-
the API directly as if lakonai were not installed.
|
|
179
|
-
|
|
178
|
+
the API directly as if lakonai were not installed. It also clears a dead lakonai
|
|
179
|
+
URL it finds already in the environment, so a shell that inherited one from an
|
|
180
|
+
older install heals itself. A base URL you set yourself is never touched.
|
|
180
181
|
|
|
181
182
|
Manage it with `lakonai proxy`:
|
|
182
183
|
|
|
@@ -187,6 +188,24 @@ lakonai proxy stop # stop it and unwire the shell
|
|
|
187
188
|
lakonai proxy restart
|
|
188
189
|
```
|
|
189
190
|
|
|
191
|
+
### MCP catalog compression and your Claude Code sessions
|
|
192
|
+
|
|
193
|
+
Wrapping MCP servers means editing `~/.claude.json` — the same file where Claude
|
|
194
|
+
Code keeps per-project session state (`lastSessionId`, trust dialogs,
|
|
195
|
+
`allowedTools`) and rewrites on every turn. lakonai therefore **never touches it
|
|
196
|
+
while a session is live**: `lakonai install` run from inside Claude Code defers
|
|
197
|
+
the wrap and says so. Apply it afterwards:
|
|
198
|
+
|
|
199
|
+
```bash
|
|
200
|
+
lakonai mcp # status: how many servers are wrapped
|
|
201
|
+
lakonai mcp wrap # wrap them (after quitting Claude Code)
|
|
202
|
+
lakonai mcp unwrap # undo
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
Every write to that file is atomic (temp file + rename, never a truncate in
|
|
206
|
+
place) and validated first: if the rewrite would drop a project or change a
|
|
207
|
+
session id, it is refused. `LAKON_NO_MCP=1` opts out of MCP wrapping entirely.
|
|
208
|
+
|
|
190
209
|
Upgrading migrates an old install by itself: `lakonai upgrade` replaces a daemon
|
|
191
210
|
running a previous version (a running process keeps executing the server code it
|
|
192
211
|
was started with), moves it off port 7474, and rewrites the old
|
|
@@ -266,6 +285,7 @@ After `lakonai install`, these are available in Claude Code:
|
|
|
266
285
|
| `lakonai peek [id]` | Read output parked in sandbox (`--grep/--offset/--limit`) |
|
|
267
286
|
| `lakonai gain` | Token savings across all measured fronts |
|
|
268
287
|
| `lakonai inspect <cmd>` | Debug what filter applies to a command |
|
|
288
|
+
| `lakonai mcp [wrap\|unwrap]` | MCP catalog compression status/lifecycle |
|
|
269
289
|
| `lakonai proxy [start\|stop\|restart]` | Compression proxy status/lifecycle |
|
|
270
290
|
| `lakonai doctor` | Health check: CLI on PATH, hooks, rule |
|
|
271
291
|
|
|
@@ -322,7 +342,7 @@ Input is measured and deterministic. Output is estimated by your local AI CLI (n
|
|
|
322
342
|
|
|
323
343
|
## Test suite
|
|
324
344
|
|
|
325
|
-
|
|
345
|
+
832 tests across 54 suites — all passing, no mocks on I/O boundaries.
|
|
326
346
|
|
|
327
347
|
| Type | Suites | Tests |
|
|
328
348
|
|------|--------|-------|
|
package/bin/lakonai.js
CHANGED
|
@@ -49,6 +49,11 @@ Usage:
|
|
|
49
49
|
lakonai gain Show token savings - INPUT (shell output, measured)
|
|
50
50
|
AND OUTPUT (how much terser the model writes; measured
|
|
51
51
|
weekly via your local AI CLI, no API key)
|
|
52
|
+
lakonai mcp [cmd] MCP catalog compression: status (default) | wrap |
|
|
53
|
+
unwrap. Wrapping edits ~/.claude.json, where Claude
|
|
54
|
+
Code keeps per-project session state, so it is
|
|
55
|
+
skipped while a session is live - run it after
|
|
56
|
+
quitting Claude Code (--force overrides).
|
|
52
57
|
lakonai proxy [cmd] Compression proxy: status (default) | start | stop
|
|
53
58
|
| restart. The proxy shrinks API request bodies;
|
|
54
59
|
while it is down lakonai simply stays out of the way
|
|
@@ -427,6 +432,47 @@ function runPixel(args) {
|
|
|
427
432
|
process.stdout.write(pixel.formatConvert(results));
|
|
428
433
|
}
|
|
429
434
|
|
|
435
|
+
function runMcp(args) {
|
|
436
|
+
const mcp = require('../src/install/mcp');
|
|
437
|
+
const { homedir } = require('../src/install/paths');
|
|
438
|
+
const home = homedir();
|
|
439
|
+
const sub = args[0] || 'status';
|
|
440
|
+
const force = args.includes('--force');
|
|
441
|
+
|
|
442
|
+
if (sub === 'status') {
|
|
443
|
+
const st = mcp.statusMcp(home);
|
|
444
|
+
if (!st.exists) {
|
|
445
|
+
process.stdout.write(`lakonai mcp: no Claude Code config at ${st.config}\n`);
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
process.stdout.write(`lakonai mcp: ${st.wrapped} wrapped, ${st.unwrapped} not wrapped (${st.config})\n`);
|
|
449
|
+
if (st.blockedBy) {
|
|
450
|
+
process.stdout.write(` wrapping is on hold — ${st.blockedBy}\n`);
|
|
451
|
+
process.stdout.write(' ~/.claude.json holds your session state; run `lakonai mcp wrap` after quitting Claude Code.\n');
|
|
452
|
+
}
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
if (sub === 'wrap' || sub === 'unwrap') {
|
|
457
|
+
const res = sub === 'wrap' ? mcp.wrapMcp(home, { force }) : mcp.unwrapMcp(home, { force });
|
|
458
|
+
if (res.skipped) {
|
|
459
|
+
process.stdout.write(`lakonai mcp: skipped — ${res.reason}\n`);
|
|
460
|
+
process.stdout.write(' ~/.claude.json carries your Claude Code sessions; rewriting it now could orphan them.\n');
|
|
461
|
+
process.stdout.write(` Quit Claude Code and run \`lakonai mcp ${sub}\` again (or pass --force).\n`);
|
|
462
|
+
process.exitCode = 1;
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
const verb = sub === 'wrap' ? 'wrapped' : 'unwrapped';
|
|
466
|
+
process.stdout.write(
|
|
467
|
+
res.count ? `lakonai mcp: ${verb} ${res.count} server${res.count > 1 ? 's' : ''}\n` : `lakonai mcp: nothing to ${sub}\n`
|
|
468
|
+
);
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
process.stdout.write(`lakonai mcp: unknown subcommand "${sub}" (use status|wrap|unwrap)\n`);
|
|
473
|
+
process.exitCode = 1;
|
|
474
|
+
}
|
|
475
|
+
|
|
430
476
|
async function runProxy(args) {
|
|
431
477
|
const daemon = require('../src/proxy/daemon');
|
|
432
478
|
const sub = args[0] || 'status';
|
|
@@ -534,6 +580,10 @@ async function main() {
|
|
|
534
580
|
await maybeOfferUpdate();
|
|
535
581
|
return;
|
|
536
582
|
}
|
|
583
|
+
if (first === 'mcp') {
|
|
584
|
+
runMcp(rest);
|
|
585
|
+
return;
|
|
586
|
+
}
|
|
537
587
|
if (first === 'proxy') {
|
|
538
588
|
await runProxy(rest);
|
|
539
589
|
return;
|
|
@@ -572,4 +622,4 @@ if (require.main === module) {
|
|
|
572
622
|
});
|
|
573
623
|
}
|
|
574
624
|
|
|
575
|
-
module.exports = { runAndFilter, printVersion, main, runProxy, HELP };
|
|
625
|
+
module.exports = { runAndFilter, printVersion, main, runProxy, runMcp, HELP };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lakonai",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.5",
|
|
4
4
|
"description": "lakonai — save AI context tokens. Filters & compresses CLI output (git, ls, grep, find, test runners, lint/build, docker/kubectl/aws) before it reaches Claude Code, Codex, Cursor, Windsurf, Cline or Gemini — and learns to filter new commands automatically the more you use it.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"claude",
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Atomic file writes.
|
|
4
|
+
//
|
|
5
|
+
// `fs.writeFileSync` truncates the target and then fills it, so a reader that
|
|
6
|
+
// looks in that window — or a crash mid-write — sees a half file. That is fine
|
|
7
|
+
// for a shim script and catastrophic for `~/.claude.json`, which is where Claude
|
|
8
|
+
// Code keeps per-project session state (`lastSessionId`, trust dialogs,
|
|
9
|
+
// `allowedTools`): a truncated parse makes it fall back to an empty config and
|
|
10
|
+
// the user loses their sessions.
|
|
11
|
+
//
|
|
12
|
+
// Writing a sibling temp file and renaming it over the target makes the swap
|
|
13
|
+
// atomic: a concurrent reader sees either the whole old file or the whole new
|
|
14
|
+
// one, never a partial write.
|
|
15
|
+
|
|
16
|
+
const fs = require('fs');
|
|
17
|
+
const path = require('path');
|
|
18
|
+
|
|
19
|
+
function writeFileAtomic(filePath, data, { encoding = 'utf8' } = {}) {
|
|
20
|
+
const dir = path.dirname(filePath);
|
|
21
|
+
const tmp = path.join(dir, `.${path.basename(filePath)}.lakonai-${process.pid}-${Date.now()}.tmp`);
|
|
22
|
+
|
|
23
|
+
// Keep the target's permissions — rename replaces the inode, so the temp
|
|
24
|
+
// file's mode is what survives.
|
|
25
|
+
let mode;
|
|
26
|
+
try { mode = fs.statSync(filePath).mode; } catch { /* new file: default mode */ }
|
|
27
|
+
|
|
28
|
+
let fd;
|
|
29
|
+
try {
|
|
30
|
+
fd = fs.openSync(tmp, 'w', mode);
|
|
31
|
+
fs.writeFileSync(fd, data, { encoding });
|
|
32
|
+
// Flush before the rename, so a crash cannot leave the renamed file empty.
|
|
33
|
+
fs.fsyncSync(fd);
|
|
34
|
+
fs.closeSync(fd);
|
|
35
|
+
fd = undefined;
|
|
36
|
+
fs.renameSync(tmp, filePath);
|
|
37
|
+
} catch (err) {
|
|
38
|
+
/* istanbul ignore next -- best-effort cleanup of a temp file we may not have opened */
|
|
39
|
+
if (fd !== undefined) { try { fs.closeSync(fd); } catch { /* ignore */ } }
|
|
40
|
+
try { fs.unlinkSync(tmp); } catch { /* never created */ }
|
|
41
|
+
throw err;
|
|
42
|
+
}
|
|
43
|
+
return filePath;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
module.exports = { writeFileAtomic };
|
|
@@ -4,6 +4,7 @@ const fs = require('fs');
|
|
|
4
4
|
const path = require('path');
|
|
5
5
|
const { backupFile } = require('./backup');
|
|
6
6
|
const { claudeConfigDir } = require('./paths');
|
|
7
|
+
const { writeFileAtomic } = require('./atomic');
|
|
7
8
|
|
|
8
9
|
const HOOKS = [
|
|
9
10
|
{
|
|
@@ -74,7 +75,9 @@ function readSettings(home) {
|
|
|
74
75
|
}
|
|
75
76
|
|
|
76
77
|
function writeSettings(home, data) {
|
|
77
|
-
|
|
78
|
+
// settings.json is read by a running Claude Code; a truncated read would drop
|
|
79
|
+
// the user's permissions and hooks.
|
|
80
|
+
writeFileAtomic(settingsPath(home), JSON.stringify(data, null, 2) + '\n');
|
|
78
81
|
}
|
|
79
82
|
|
|
80
83
|
function entryHasHook(entry, basename) {
|
package/src/install/mcp.js
CHANGED
|
@@ -6,15 +6,71 @@
|
|
|
6
6
|
// context (via `lakonai __mcp <original-cmd>`). Backed up first, fully reversible
|
|
7
7
|
// on uninstall. Only stdio servers (a string `command`) are touched; url/sse
|
|
8
8
|
// servers and tool-call results are never altered. Opt out with LAKON_NO_MCP=1.
|
|
9
|
+
//
|
|
10
|
+
// ~/.claude.json is NOT just MCP config — Claude Code keeps per-project session
|
|
11
|
+
// state there (`lastSessionId`, `lastSessionFirstPrompt`, `hasTrustDialogAccepted`,
|
|
12
|
+
// `allowedTools`) and rewrites it on every turn. A read-modify-write from here
|
|
13
|
+
// while a session is live silently discards whatever Claude Code wrote in
|
|
14
|
+
// between, which orphans the session (`claude --resume` no longer finds it).
|
|
15
|
+
// So this module: refuses to write while a session is active, writes atomically,
|
|
16
|
+
// and validates that no project survived the round trip missing.
|
|
9
17
|
|
|
10
18
|
const fs = require('fs');
|
|
11
19
|
const path = require('path');
|
|
12
20
|
const { backupFile } = require('./backup');
|
|
21
|
+
const { writeFileAtomic } = require('./atomic');
|
|
22
|
+
|
|
23
|
+
// A session that wrote the config this recently is treated as live.
|
|
24
|
+
const RECENT_WRITE_MS = 30_000;
|
|
25
|
+
|
|
26
|
+
// Where we record the mtime of our own last write, so we do not mistake it for
|
|
27
|
+
// a Claude Code session and refuse the very next command.
|
|
28
|
+
function lastWritePath() {
|
|
29
|
+
const home = process.env.LAKON_HOME || path.join(require('./paths').homedir(), '.lakon');
|
|
30
|
+
return path.join(home, 'mcp-last-write.json');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function recordOwnWrite(configPath) {
|
|
34
|
+
try {
|
|
35
|
+
const { mtimeMs } = fs.statSync(configPath);
|
|
36
|
+
fs.mkdirSync(path.dirname(lastWritePath()), { recursive: true });
|
|
37
|
+
fs.writeFileSync(lastWritePath(), JSON.stringify({ mtimeMs }));
|
|
38
|
+
} catch { /* best-effort: the worst case is one over-cautious refusal */ }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function isOwnWrite(mtimeMs) {
|
|
42
|
+
try {
|
|
43
|
+
return JSON.parse(fs.readFileSync(lastWritePath(), 'utf8')).mtimeMs === mtimeMs;
|
|
44
|
+
} catch {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
13
48
|
|
|
14
49
|
function mcpConfigPath(home) {
|
|
15
50
|
return path.join(home, '.claude.json');
|
|
16
51
|
}
|
|
17
52
|
|
|
53
|
+
// Why it is unsafe to write ~/.claude.json right now, or null when it is safe.
|
|
54
|
+
// The env check catches the common case by far: `lakonai install` typed inside a
|
|
55
|
+
// Claude Code session. The mtime check catches a session running in another
|
|
56
|
+
// terminal.
|
|
57
|
+
function activeSessionReason(home, { now = Date.now() } = {}) {
|
|
58
|
+
if (process.env.CLAUDE_CODE_ENTRYPOINT || process.env.CLAUDE_PID) {
|
|
59
|
+
return 'this shell is inside a running Claude Code session';
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
// A file timestamp can land a hair AHEAD of Date.now() (clock/fs
|
|
63
|
+
// granularity), so a negative age means "just written", not "stale" — and
|
|
64
|
+
// erring toward "a session is live" is the safe direction anyway.
|
|
65
|
+
const { mtimeMs } = fs.statSync(mcpConfigPath(home));
|
|
66
|
+
const age = now - mtimeMs;
|
|
67
|
+
if (age < RECENT_WRITE_MS && !isOwnWrite(mtimeMs)) {
|
|
68
|
+
return `a Claude Code session wrote ~/.claude.json ${Math.round(Math.max(age, 0) / 1000)}s ago`;
|
|
69
|
+
}
|
|
70
|
+
} catch { /* no config yet: nothing can be racing us */ }
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
|
|
18
74
|
// Collect every `mcpServers` map anywhere in the config (top-level + per-project).
|
|
19
75
|
function collectServerMaps(obj, out) {
|
|
20
76
|
if (!obj || typeof obj !== 'object') return out;
|
|
@@ -60,28 +116,106 @@ function unwrapServers(config) {
|
|
|
60
116
|
return n;
|
|
61
117
|
}
|
|
62
118
|
|
|
63
|
-
|
|
64
|
-
|
|
119
|
+
// Last line of defence before the rename: the bytes we are about to write must
|
|
120
|
+
// parse, and must still carry every project (with its session pointers) and
|
|
121
|
+
// every top-level key the file had. Anything less means a bug here would be
|
|
122
|
+
// destroying the user's Claude Code state.
|
|
123
|
+
function preservesState(before, serialized) {
|
|
124
|
+
let after;
|
|
125
|
+
try { after = JSON.parse(serialized); } catch { return false; }
|
|
126
|
+
|
|
127
|
+
for (const k of Object.keys(before)) {
|
|
128
|
+
if (!(k in after)) return false;
|
|
129
|
+
}
|
|
130
|
+
const oldProjects = before.projects && typeof before.projects === 'object' ? before.projects : null;
|
|
131
|
+
if (oldProjects) {
|
|
132
|
+
const newProjects = after.projects;
|
|
133
|
+
if (!newProjects || typeof newProjects !== 'object') return false;
|
|
134
|
+
for (const p of Object.keys(oldProjects)) {
|
|
135
|
+
if (!(p in newProjects)) return false;
|
|
136
|
+
if (oldProjects[p] && oldProjects[p].lastSessionId && newProjects[p].lastSessionId !== oldProjects[p].lastSessionId) {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Returns { count, skipped, reason }. `count` is how many servers changed;
|
|
145
|
+
// `skipped` says the config was deliberately left alone.
|
|
146
|
+
function applyToConfig(home, transform, { force = false } = {}) {
|
|
147
|
+
if (process.env.LAKON_NO_MCP === '1') return { count: 0, skipped: true, reason: 'LAKON_NO_MCP=1' };
|
|
148
|
+
|
|
65
149
|
const p = mcpConfigPath(home);
|
|
150
|
+
if (!force) {
|
|
151
|
+
const reason = activeSessionReason(home);
|
|
152
|
+
if (reason) return { count: 0, skipped: true, reason };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Back up before reading, so the copy on disk is never newer than what we read.
|
|
156
|
+
backupFile('claude-code-mcp', p);
|
|
157
|
+
|
|
158
|
+
let raw;
|
|
66
159
|
let config;
|
|
67
160
|
try {
|
|
68
|
-
|
|
161
|
+
raw = fs.readFileSync(p, 'utf8');
|
|
162
|
+
config = JSON.parse(raw);
|
|
69
163
|
} catch {
|
|
70
|
-
return 0; // no config / unreadable -> nothing to do
|
|
164
|
+
return { count: 0, skipped: false, reason: null }; // no config / unreadable -> nothing to do
|
|
71
165
|
}
|
|
166
|
+
|
|
167
|
+
// Read → transform → write with nothing slow in between: every millisecond
|
|
168
|
+
// here is a millisecond of Claude Code writes we would clobber.
|
|
169
|
+
const before = JSON.parse(raw);
|
|
72
170
|
const n = transform(config);
|
|
73
|
-
if (n
|
|
74
|
-
|
|
75
|
-
|
|
171
|
+
if (n === 0) return { count: 0, skipped: false, reason: null };
|
|
172
|
+
|
|
173
|
+
const serialized = JSON.stringify(config, null, 2) + '\n';
|
|
174
|
+
if (!preservesState(before, serialized)) {
|
|
175
|
+
return { count: 0, skipped: true, reason: 'refused: the rewrite would have dropped Claude Code state' };
|
|
76
176
|
}
|
|
77
|
-
|
|
177
|
+
|
|
178
|
+
writeFileAtomic(p, serialized);
|
|
179
|
+
recordOwnWrite(p);
|
|
180
|
+
return { count: n, skipped: false, reason: null };
|
|
78
181
|
}
|
|
79
182
|
|
|
80
|
-
function wrapMcp(home) {
|
|
81
|
-
return applyToConfig(home, wrapServers);
|
|
183
|
+
function wrapMcp(home, opts) {
|
|
184
|
+
return applyToConfig(home, wrapServers, opts);
|
|
82
185
|
}
|
|
83
|
-
function unwrapMcp(home) {
|
|
84
|
-
return applyToConfig(home, unwrapServers);
|
|
186
|
+
function unwrapMcp(home, opts) {
|
|
187
|
+
return applyToConfig(home, unwrapServers, opts);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// How many servers are currently wrapped / wrappable — for `lakonai mcp status`.
|
|
191
|
+
function statusMcp(home) {
|
|
192
|
+
const p = mcpConfigPath(home);
|
|
193
|
+
let config;
|
|
194
|
+
try { config = JSON.parse(fs.readFileSync(p, 'utf8')); } catch { return { config: p, exists: false, wrapped: 0, unwrapped: 0 }; }
|
|
195
|
+
let wrapped = 0;
|
|
196
|
+
let unwrapped = 0;
|
|
197
|
+
for (const map of collectServerMaps(config, [])) {
|
|
198
|
+
for (const name of Object.keys(map)) {
|
|
199
|
+
const s = map[name];
|
|
200
|
+
if (!s || typeof s !== 'object') continue;
|
|
201
|
+
if (s._lakonai) wrapped++;
|
|
202
|
+
else if (typeof s.command === 'string') unwrapped++;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return { config: p, exists: true, wrapped, unwrapped, blockedBy: activeSessionReason(home) };
|
|
85
206
|
}
|
|
86
207
|
|
|
87
|
-
module.exports = {
|
|
208
|
+
module.exports = {
|
|
209
|
+
applyToConfig,
|
|
210
|
+
wrapMcp,
|
|
211
|
+
unwrapMcp,
|
|
212
|
+
statusMcp,
|
|
213
|
+
wrapServers,
|
|
214
|
+
unwrapServers,
|
|
215
|
+
collectServerMaps,
|
|
216
|
+
mcpConfigPath,
|
|
217
|
+
activeSessionReason,
|
|
218
|
+
preservesState,
|
|
219
|
+
isOwnWrite,
|
|
220
|
+
recordOwnWrite,
|
|
221
|
+
};
|
package/src/install/platforms.js
CHANGED
|
@@ -6,6 +6,7 @@ const { backupFile, restoreAllBackups } = require('./backup');
|
|
|
6
6
|
const { installHook, uninstallHook } = require('./claude-hook');
|
|
7
7
|
const { installCommands, uninstallCommands } = require('./claude-commands');
|
|
8
8
|
const { wrapMcp, unwrapMcp } = require('./mcp');
|
|
9
|
+
const { writeFileAtomic } = require('./atomic');
|
|
9
10
|
const { claudeConfigDir } = require('./paths');
|
|
10
11
|
|
|
11
12
|
const MARK_BEGIN = '<!-- lakonai:begin -->';
|
|
@@ -46,7 +47,7 @@ function upsertBlock(platformId, filePath, rule) {
|
|
|
46
47
|
const block = wrap(rule);
|
|
47
48
|
const re = new RegExp(`${ANY_BEGIN}[\\s\\S]*?${ANY_END}\\n?`);
|
|
48
49
|
const next = re.test(existing) ? existing.replace(re, block) : (existing ? `${existing.trim()}\n\n${block}` : block);
|
|
49
|
-
|
|
50
|
+
writeFileAtomic(filePath, next);
|
|
50
51
|
return filePath;
|
|
51
52
|
}
|
|
52
53
|
|
|
@@ -56,7 +57,7 @@ function stripBlock(filePath) {
|
|
|
56
57
|
const re = new RegExp(`\\n*${ANY_BEGIN}[\\s\\S]*?${ANY_END}\\n?`);
|
|
57
58
|
if (!re.test(existing)) return null;
|
|
58
59
|
const next = existing.replace(re, '').trim();
|
|
59
|
-
if (next)
|
|
60
|
+
if (next) writeFileAtomic(filePath, next + '\n');
|
|
60
61
|
else fs.unlinkSync(filePath);
|
|
61
62
|
return filePath;
|
|
62
63
|
}
|
|
@@ -77,12 +78,18 @@ const PLATFORMS = [
|
|
|
77
78
|
const rulePath = upsertBlock(id, path.join(claudeConfigDir(home), 'CLAUDE.md'), rule);
|
|
78
79
|
const hookResult = installHook(home);
|
|
79
80
|
const cmds = installCommands(home);
|
|
80
|
-
wrapMcp(home);
|
|
81
|
+
const mcp = wrapMcp(home);
|
|
81
82
|
/* istanbul ignore next */
|
|
82
83
|
const suffixHook = hookResult.settingsMerged ? '+ PreToolUse hook' : `(hook: ${hookResult.note})`;
|
|
83
84
|
/* istanbul ignore next */
|
|
84
85
|
const suffixCmds = cmds.length ? `+ ${cmds.length} slash command${cmds.length > 1 ? 's' : ''}` : '';
|
|
85
|
-
|
|
86
|
+
// Never silent: writing ~/.claude.json under a live session can orphan it,
|
|
87
|
+
// so say that MCP compression was deferred and how to apply it later.
|
|
88
|
+
/* istanbul ignore next */
|
|
89
|
+
const suffixMcp = mcp.skipped && mcp.reason && !mcp.reason.startsWith('LAKON_NO_MCP')
|
|
90
|
+
? `(MCP compression deferred - ${mcp.reason}; run \`lakonai mcp wrap\` after quitting Claude Code)`
|
|
91
|
+
: '';
|
|
92
|
+
return [rulePath, suffixHook, suffixCmds, suffixMcp].filter(Boolean).join(' ');
|
|
86
93
|
},
|
|
87
94
|
uninstall: ({ home }) => {
|
|
88
95
|
uninstallHook(home);
|
package/src/proxy/state.js
CHANGED
|
@@ -124,30 +124,47 @@ function envScript(port) {
|
|
|
124
124
|
return `# lakonai proxy — generated by lakonai, safe to delete.
|
|
125
125
|
# Points ANTHROPIC_BASE_URL at the local compression proxy, but only while it is
|
|
126
126
|
# actually listening. Proxy down = plain direct API access, never a broken CLI.
|
|
127
|
+
|
|
128
|
+
# Is something listening on 127.0.0.1:$1? Shell-native where possible.
|
|
129
|
+
__lakon_probe() {
|
|
130
|
+
if [ -n "$ZSH_VERSION" ] && zmodload zsh/net/tcp 2>/dev/null; then
|
|
131
|
+
if ztcp 127.0.0.1 "$1" 2>/dev/null; then ztcp -c "$REPLY" 2>/dev/null; return 0; fi
|
|
132
|
+
return 1
|
|
133
|
+
elif [ -n "$BASH_VERSION" ]; then
|
|
134
|
+
(exec 3<>/dev/tcp/127.0.0.1/"$1") 2>/dev/null && return 0
|
|
135
|
+
return 1
|
|
136
|
+
elif command -v nc >/dev/null 2>&1; then
|
|
137
|
+
nc -z -w 1 127.0.0.1 "$1" >/dev/null 2>&1 && return 0
|
|
138
|
+
return 1
|
|
139
|
+
fi
|
|
140
|
+
return 1
|
|
141
|
+
}
|
|
142
|
+
|
|
127
143
|
__lakon_port=${port}
|
|
128
|
-
__lakon_up=0
|
|
129
144
|
|
|
130
145
|
# Never clobber a base URL the user set for another endpoint — only take over
|
|
131
|
-
# when nothing is set, or when what is set is
|
|
146
|
+
# when nothing is set, or when what is set is a local proxy.
|
|
132
147
|
case "$ANTHROPIC_BASE_URL" in
|
|
133
148
|
""|http://127.0.0.1:*)
|
|
134
|
-
if
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
149
|
+
if __lakon_probe "$__lakon_port"; then
|
|
150
|
+
export ANTHROPIC_BASE_URL=http://127.0.0.1:$__lakon_port
|
|
151
|
+
else
|
|
152
|
+
# A dead lakonai URL inherited from the environment (ours, or the
|
|
153
|
+
# pre-1.2.3 port 7474) is what broke the CLI in the first place. If
|
|
154
|
+
# nothing answers there, drop it and let Claude talk to the API directly.
|
|
155
|
+
case "$ANTHROPIC_BASE_URL" in
|
|
156
|
+
http://127.0.0.1:$__lakon_port|http://127.0.0.1:7474)
|
|
157
|
+
__lakon_stale=\${ANTHROPIC_BASE_URL##*:}
|
|
158
|
+
__lakon_probe "$__lakon_stale" || unset ANTHROPIC_BASE_URL
|
|
159
|
+
unset __lakon_stale
|
|
160
|
+
;;
|
|
161
|
+
esac
|
|
143
162
|
fi
|
|
144
163
|
;;
|
|
145
164
|
esac
|
|
146
165
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
fi
|
|
150
|
-
unset __lakon_port __lakon_up
|
|
166
|
+
unset __lakon_port
|
|
167
|
+
unset -f __lakon_probe 2>/dev/null
|
|
151
168
|
`;
|
|
152
169
|
}
|
|
153
170
|
|