lakonai 0.14.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
@@ -326,20 +326,23 @@ agent's own non-shell Read tool and its full command stream — guarding that to
326
326
  and auto-learning need a call-rewriting hook, which today only Claude Code
327
327
  exposes. Honest matrix:
328
328
 
329
- | Agent | Terse rule | Shell filter | Read/Grep guard | Auto-learning |
330
- | ----------- | :--------: | -------------------- | --------------------- | :-----------: |
331
- | Claude Code | ✅ | ✅ automatic (hook) | ✅ tool + shell | ✅ |
332
- | Codex CLI | ✅ | ✅ automatic (shim¹) | ✅ shell reads (shim²) | ❌³ |
333
- | Cursor | ✅ | ✅ automatic (shim¹) | ✅ shell reads (shim²) | ❌³ |
334
- | Windsurf | ✅ | ✅ automatic (shim¹) | ✅ shell reads (shim²) | ❌³ |
335
- | Cline | ✅ | ✅ automatic (shim¹) | ✅ shell reads (shim²) | ❌³ |
336
- | Gemini CLI | ✅ | ✅ automatic (shim¹) | ✅ shell reads (shim²) | ❌³ |
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³ |
337
337
 
338
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 ²).
339
342
 
340
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.
341
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).
342
- ³ Auto-learning needs to observe the agent's full command stream (an end-of-turn hook), so it's Claude-Code-only — and largely moot elsewhere, since the shim already filters the standard heavy commands without needing to learn them.
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`.
343
346
 
344
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.
345
348
 
package/bin/lakonai.js CHANGED
@@ -98,6 +98,15 @@ function runAndFilter(cmd, args) {
98
98
  filteredTokens: countTokensApprox(filtered),
99
99
  });
100
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
+
101
110
  /* istanbul ignore next */
102
111
  process.exit(child.status ?? 0);
103
112
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lakonai",
3
- "version": "0.14.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,