lakonai 0.13.0 → 0.15.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 CHANGED
@@ -317,28 +317,32 @@ Each profile gets its own independent install. `lakonai uninstall` / `lakonai re
317
317
 
318
318
  ### What each agent actually gets (honest capability matrix)
319
319
 
320
- Shell-output **filtering is automatic on every agent** once you run `lakonai
321
- shim` (see below) — it routes commands through lakonai at the PATH level, so no
322
- hook API and no model cooperation is needed. The other input-side features
323
- (Read/Grep guards, auto-learning) act on the agent's *own* tool calls, so they
324
- still require a tool-interception hook — which today only Claude Code exposes
325
- with the ability to rewrite a call. Honest matrix:
326
-
327
- | Agent | Terse rule | Shell filter | Read/Grep guard | Auto-learning |
328
- | ----------- | :--------: | ----------------------- | :-------------: | :-----------: |
329
- | Claude Code | ✅ | ✅ automatic (hook) | ✅ | ✅ |
330
- | Codex CLI | ✅ | ✅ automatic (shim¹) | ❌ (upstream²) | ❌ |
331
- | Cursor | ✅ | ✅ automatic (shim¹) | ⚠️ block-only³ | ❌ |
332
- | Windsurf | ✅ | ✅ automatic (shim¹) | ❌ | ❌ |
333
- | Cline | ✅ | ✅ automatic (shim¹) | ❌ | ❌ |
334
- | Gemini CLI | ✅ | ✅ automatic (shim¹) | ❌ (buildable⁴)| ❌ |
320
+ Once you run `lakonai shim` (see above), the **shell-mediated** features are
321
+ automatic on every agent — they work at the PATH level, no hook API and no model
322
+ cooperation needed. **Shell filtering** routes every wrapped command through
323
+ lakonai, and the **Read-guard** refuses junk reads done via the shell
324
+ (`cat pnpm-lock.yaml`, `head node_modules/…`). What the shim *can't* see is an
325
+ agent's own non-shell Read tool and its full command stream — guarding that tool
326
+ and auto-learning need a call-rewriting hook, which today only Claude Code
327
+ exposes. Honest matrix:
328
+
329
+ | Agent | Terse rule | Shell filter | Read/Grep guard | Auto-learning |
330
+ | ----------- | :--------: | -------------------- | --------------------- | --------------- |
331
+ | Claude Code | ✅ | ✅ automatic (hook) | ✅ tool + shell | ✅ transcript+log |
332
+ | Codex CLI | ✅ | ✅ automatic (shim¹) | ✅ shell reads (shim²) | ✅ from log³ |
333
+ | Cursor | ✅ | ✅ automatic (shim¹) | ✅ shell reads (shim²) | ✅ from log³ |
334
+ | Windsurf | ✅ | ✅ automatic (shim¹) | ✅ shell reads (shim²) | ✅ from log³ |
335
+ | Cline | ✅ | ✅ automatic (shim¹) | ✅ shell reads (shim²) | ✅ from log³ |
336
+ | Gemini CLI | ✅ | ✅ automatic (shim¹) | ✅ shell reads (shim²) | ✅ from log³ |
335
337
 
336
338
  Run `lakonai doctor` to see, per platform, what's actually active on your machine.
339
+ The only thing **not** automatic everywhere is guarding the agent's *own non-shell
340
+ Read tool* — that needs a call-rewriting hook, which today only Claude Code exposes
341
+ (see ²).
337
342
 
338
343
  ¹ `lakonai shim` prepends `~/.lakon/shim` to PATH so `ls`/`grep`/`rg`/`ag`/`find`/`cat`/`tree`/`head` filter automatically for any agent that runs them through a shell (opt-in — it edits your shell rc; `git` is excluded for safety). Without it, those commands filter only when the model prefixes `lakonai` itself.
339
- ² Codex's hook can block a tool call but [can't yet rewrite its input](https://github.com/openai/codex/issues/18491), so the Read/Grep guard can't be made automatic there yet.
340
- ³ Cursor's `beforeReadFile` hook can deny a read but not filter/cap its contents.
341
- ⁴ Gemini's `BeforeTool` hook *can* rewrite/cap a tool call; a Gemini guard installer is planned but not shipped (needs validation against the real CLI schema).
344
+ ² The shim refuses junk reads (lockfiles, `node_modules/…`, build artifacts) done through the shell with `cat`/`head`/`tail`/`less` — same deny rules as the Claude Read hook. An agent's *own* internal Read tool (not via the shell) still needs a hook, so capping/blocking it is automatic only on Claude Code. Codex's hook [can't yet rewrite a tool call](https://github.com/openai/codex/issues/18491); Cursor's can deny but not cap; Gemini's `BeforeTool` could (installer planned, pending schema validation against the real CLI).
345
+ ³ Auto-learning runs off `~/.lakon/log.jsonl`, which every `lakonai` call (shim wrappers + rule-prefixed commands) appends to on any agent — so it promotes new heavy commands automatically everywhere, throttled hourly. Claude Code additionally learns from the full session transcript (a wider window). Disable with `LAKON_NO_LEARN=1`.
342
346
 
343
347
  ¹ "Claude Code" covers **every** Claude Code frontend — terminal CLI, VS Code extension, JetBrains plugin, desktop app. All read the same `~/.claude/CLAUDE.md` + `~/.claude/settings.json`, so one install lights up all of them.
344
348
 
package/bin/lakonai.js CHANGED
@@ -63,6 +63,16 @@ Update notifications:
63
63
  `;
64
64
 
65
65
  function runAndFilter(cmd, args) {
66
+ // Universal Read-guard: refuse junk reads (lockfiles/node_modules) done via the
67
+ // shell (`cat pnpm-lock.yaml`) on ANY agent that uses the shim — same deny rules
68
+ // as the Claude Read hook, no platform hook required.
69
+ const denied = require('../src/shim-guard').check(cmd, args);
70
+ if (denied) {
71
+ process.stdout.write(`lakonai: skipped ${denied.path} — ${denied.reason}\n`);
72
+ tracking.record({ cmd, args, rawTokens: 0, filteredTokens: 0 });
73
+ process.exit(0);
74
+ }
75
+
66
76
  const merge = needsStderr(cmd, args);
67
77
  const stdio = merge ? ['inherit', 'pipe', 'pipe'] : ['inherit', 'pipe', 'inherit'];
68
78
  // Strip the shim dir from PATH so spawning `cmd` resolves the real system
@@ -88,6 +98,15 @@ function runAndFilter(cmd, args) {
88
98
  filteredTokens: countTokensApprox(filtered),
89
99
  });
90
100
 
101
+ // Auto-learning off the universal log (throttled hourly) — runs on EVERY agent,
102
+ // not just Claude Code's transcript. Never let it break the command.
103
+ try {
104
+ require('../src/learn').maybeLearnFromLog((c) => isSupported(c));
105
+ /* istanbul ignore next */
106
+ } catch {
107
+ // learning is best-effort
108
+ }
109
+
91
110
  /* istanbul ignore next */
92
111
  process.exit(child.status ?? 0);
93
112
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lakonai",
3
- "version": "0.13.0",
3
+ "version": "0.15.0",
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",
@@ -81,7 +81,7 @@ const PLATFORMS = [
81
81
  /* istanbul ignore next */
82
82
  const suffixHook = hookResult.settingsMerged ? '+ PreToolUse hook' : `(hook: ${hookResult.note})`;
83
83
  /* istanbul ignore next */
84
- const suffixCmds = cmds.length ? `+ ${cmds.join(' ')}` : '';
84
+ const suffixCmds = cmds.length ? `+ ${cmds.length} slash command${cmds.length > 1 ? 's' : ''}` : '';
85
85
  return [rulePath, suffixHook, suffixCmds].filter(Boolean).join(' ');
86
86
  },
87
87
  uninstall: ({ home }) => {
package/src/learn.js CHANGED
@@ -131,6 +131,52 @@ function analyzeTranscript(transcriptPath, isBuiltin) {
131
131
  promote(stats, isBuiltin);
132
132
  }
133
133
 
134
+ // Platform-agnostic learning. The transcript reader above is Claude-Code-only;
135
+ // this learns from `~/.lakon/log.jsonl` instead — populated by EVERY `lakonai`
136
+ // invocation (shim wrappers + rule-prefixed calls) on ANY agent. So auto-learning
137
+ // runs automatically everywhere, not just on Claude. Smaller observation window
138
+ // (only commands routed through lakonai), but no hook required.
139
+ function analyzeLog(isBuiltin) {
140
+ if (process.env.LAKON_NO_LEARN === '1' || process.env.LAKON_NO_TRACK === '1') return;
141
+ let text;
142
+ try {
143
+ text = fs.readFileSync(path.join(lakonHome(), 'log.jsonl'), 'utf8');
144
+ } catch {
145
+ return;
146
+ }
147
+ const stats = {};
148
+ for (const line of text.split('\n')) {
149
+ if (!line.trim()) continue;
150
+ let e;
151
+ try {
152
+ e = JSON.parse(line);
153
+ } catch {
154
+ continue;
155
+ }
156
+ const cmd = e && typeof e.cmd === 'string' ? e.cmd : null;
157
+ if (!cmd || cmd === 'session' || isBuiltin(cmd)) continue;
158
+ const s = stats[cmd] || { count: 0, tokens: 0 };
159
+ s.count += 1;
160
+ s.tokens += e.raw || 0;
161
+ stats[cmd] = s;
162
+ }
163
+ if (Object.keys(stats).length) promote(stats, isBuiltin);
164
+ }
165
+
166
+ const LEARN_LOG_TTL_MS = 60 * 60 * 1000; // scan the log at most once per hour
167
+
168
+ // Throttled entry point for the per-call path (runAndFilter): scans the log at
169
+ // most hourly, so learning is automatic on every agent but costs nothing hot.
170
+ function maybeLearnFromLog(isBuiltin, now = Date.now()) {
171
+ if (process.env.LAKON_NO_LEARN === '1' || process.env.LAKON_NO_TRACK === '1') return false;
172
+ const stamp = path.join(lakonHome(), '.learn-log-stamp');
173
+ const last = Number(readJson(stamp, { t: 0 }).t) || 0;
174
+ if (last && now - last < LEARN_LOG_TTL_MS) return false; // last===0 ⇒ never run ⇒ run now
175
+ writeJson(stamp, { t: now });
176
+ analyzeLog(isBuiltin);
177
+ return true;
178
+ }
179
+
134
180
  let _learnedCache;
135
181
  function learnedCommands() {
136
182
  if (_learnedCache === undefined) _learnedCache = new Set(readJson(learnedPath(), []));
@@ -149,6 +195,8 @@ module.exports = {
149
195
  extractBashUsage,
150
196
  promote,
151
197
  analyzeTranscript,
198
+ analyzeLog,
199
+ maybeLearnFromLog,
152
200
  learnedCommands,
153
201
  isLearned,
154
202
  _resetCache,
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ // Universal Read-guard for the shell path. The Read-guard hook only fires on
4
+ // Claude Code's Read tool, but EVERY agent reads files through the shell too
5
+ // (`cat pnpm-lock.yaml`, `head node_modules/...`). Those go through the shim →
6
+ // `lakonai cat ...`, so we can refuse junk reads here and the protection becomes
7
+ // automatic on every agent that uses the shim — no per-platform hook needed.
8
+ //
9
+ // Reuses the exact deny rules from the Claude Read-guard so behaviour matches.
10
+
11
+ const { isDeniedPath } = require('./hooks/read-guard');
12
+
13
+ // Commands whose non-flag arguments are file paths we should guard.
14
+ const READ_CMDS = new Set(['cat', 'head', 'tail', 'less', 'more', 'bat']);
15
+
16
+ // Return { path, reason } for the first denied path argument, or null. Args that
17
+ // start with `-` are flags; `--` ends option parsing.
18
+ function check(cmd, args) {
19
+ if (!READ_CMDS.has(cmd) || !Array.isArray(args)) return null;
20
+ let optsEnded = false;
21
+ for (const a of args) {
22
+ if (typeof a !== 'string') continue;
23
+ if (!optsEnded && a === '--') {
24
+ optsEnded = true;
25
+ continue;
26
+ }
27
+ if (!optsEnded && a.startsWith('-')) continue; // flag (e.g. -n, -20)
28
+ const reason = isDeniedPath(a);
29
+ if (reason) return { path: a, reason };
30
+ }
31
+ return null;
32
+ }
33
+
34
+ module.exports = { check, READ_CMDS };