lakonai 0.7.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/README.md +33 -12
- package/bin/lakonai.js +33 -24
- package/package.json +17 -13
- package/src/filters/auto.js +22 -0
- package/src/filters/defs.js +103 -0
- package/src/filters/engine.js +117 -0
- package/src/filters/find.js +18 -0
- package/src/filters/git.js +2 -2
- package/src/filters/index.js +58 -5
- package/src/filters/ls.js +1 -1
- package/src/filters/test.js +92 -0
- package/src/filters/utils.js +53 -1
- package/src/hooks/bash-rewrite.js +10 -5
- package/src/hooks/grep-guard.js +7 -6
- package/src/hooks/read-guard.js +18 -9
- package/src/hooks/session-start.js +6 -2
- package/src/hooks/stop-hook.js +13 -8
- package/src/hooks/throttle.js +2 -3
- package/src/hooks/version-check.js +11 -11
- package/src/install/claude-commands.js +1 -0
- package/src/install/claude-hook.js +8 -7
- package/src/install/index.js +8 -8
- package/src/install/paths.js +9 -1
- package/src/install/platforms.js +3 -3
- package/src/learn.js +157 -0
- package/src/rules/lakonai.md +8 -2
- package/src/tracking.js +3 -3
|
@@ -68,13 +68,13 @@ function writeSettings(home, data) {
|
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
function entryHasHook(entry, basename) {
|
|
71
|
-
/*
|
|
71
|
+
/* istanbul ignore next */
|
|
72
72
|
if (!entry || !Array.isArray(entry.hooks)) return false;
|
|
73
73
|
return entry.hooks.some((h) => h && typeof h.command === 'string' && h.command.includes(basename));
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
function mergeHook(data, hookDef, dest) {
|
|
77
|
-
/*
|
|
77
|
+
/* istanbul ignore next */
|
|
78
78
|
const eventKey = hookDef.event || 'PreToolUse';
|
|
79
79
|
data.hooks = data.hooks || {};
|
|
80
80
|
data.hooks[eventKey] = data.hooks[eventKey] || [];
|
|
@@ -83,7 +83,7 @@ function mergeHook(data, hookDef, dest) {
|
|
|
83
83
|
const existing = data.hooks[eventKey].find((e) => e.matcher === hookDef.matcher);
|
|
84
84
|
if (existing) {
|
|
85
85
|
if (!entryHasHook(existing, hookDef.basename)) {
|
|
86
|
-
/*
|
|
86
|
+
/* istanbul ignore next */
|
|
87
87
|
existing.hooks = existing.hooks || [];
|
|
88
88
|
existing.hooks.push({ type: 'command', command: dest });
|
|
89
89
|
}
|
|
@@ -135,7 +135,7 @@ function installHook(home) {
|
|
|
135
135
|
try {
|
|
136
136
|
const { writeInstalledVersionMarker } = require('../hooks/version-check');
|
|
137
137
|
writeInstalledVersionMarker(require('../../package.json').version);
|
|
138
|
-
/*
|
|
138
|
+
/* istanbul ignore next */
|
|
139
139
|
} catch {
|
|
140
140
|
// never let marker write break install
|
|
141
141
|
}
|
|
@@ -147,11 +147,11 @@ function uninstallHook(home) {
|
|
|
147
147
|
const { ok, data } = readSettings(home);
|
|
148
148
|
if (ok && data.hooks && typeof data.hooks === 'object') {
|
|
149
149
|
for (const eventKey of Object.keys(data.hooks)) {
|
|
150
|
-
/*
|
|
150
|
+
/* istanbul ignore next */
|
|
151
151
|
if (!Array.isArray(data.hooks[eventKey])) continue;
|
|
152
152
|
data.hooks[eventKey] = data.hooks[eventKey]
|
|
153
153
|
.map((entry) => {
|
|
154
|
-
/*
|
|
154
|
+
/* istanbul ignore next */
|
|
155
155
|
if (!Array.isArray(entry.hooks)) return entry;
|
|
156
156
|
const remaining = entry.hooks.filter(
|
|
157
157
|
(h) => !(h.command && ALL_BASENAMES.some((b) => h.command.includes(b)))
|
|
@@ -163,13 +163,14 @@ function uninstallHook(home) {
|
|
|
163
163
|
if (data.hooks[eventKey].length === 0) delete data.hooks[eventKey];
|
|
164
164
|
}
|
|
165
165
|
if (Object.keys(data.hooks).length === 0) delete data.hooks;
|
|
166
|
+
/* istanbul ignore next -- settings file is always present on this path */
|
|
166
167
|
if (fs.existsSync(settingsPath(home))) writeSettings(home, data);
|
|
167
168
|
}
|
|
168
169
|
|
|
169
170
|
for (const h of [...HOOKS, ...SUPPORT_FILES]) {
|
|
170
171
|
const dest = hookDest(home, h.basename);
|
|
171
172
|
if (fs.existsSync(dest)) {
|
|
172
|
-
try { fs.unlinkSync(dest); /*
|
|
173
|
+
try { fs.unlinkSync(dest); /* istanbul ignore next */ } catch {}
|
|
173
174
|
}
|
|
174
175
|
}
|
|
175
176
|
}
|
package/src/install/index.js
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
const fs = require('fs');
|
|
4
4
|
const path = require('path');
|
|
5
|
-
const os = require('os');
|
|
6
5
|
const platforms = require('./platforms');
|
|
7
6
|
const { listBackups } = require('./backup');
|
|
7
|
+
const { homedir } = require('./paths');
|
|
8
8
|
|
|
9
9
|
const RULE_PATH = path.join(__dirname, '..', 'rules', 'lakonai.md');
|
|
10
10
|
|
|
@@ -18,26 +18,26 @@ function readRule() {
|
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
function shortenPath(p) {
|
|
21
|
-
/*
|
|
21
|
+
/* istanbul ignore next */
|
|
22
22
|
if (!p) return p;
|
|
23
|
-
const home =
|
|
23
|
+
const home = homedir();
|
|
24
24
|
if (p.startsWith(home)) return '~' + p.slice(home.length);
|
|
25
25
|
return p;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
function padLabel(label, width = 26) {
|
|
29
|
-
/*
|
|
29
|
+
/* istanbul ignore next */
|
|
30
30
|
return label.length >= width ? label : label + ' '.repeat(width - label.length);
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
function detectPlatforms() {
|
|
34
|
-
const home =
|
|
34
|
+
const home = homedir();
|
|
35
35
|
return platforms.list().filter((p) => p.detect(home));
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
async function install({ only, here = false } = {}) {
|
|
39
39
|
const rule = readRule();
|
|
40
|
-
const home =
|
|
40
|
+
const home = homedir();
|
|
41
41
|
const all = platforms.list();
|
|
42
42
|
|
|
43
43
|
let targets;
|
|
@@ -85,7 +85,7 @@ async function install({ only, here = false } = {}) {
|
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
async function uninstall() {
|
|
88
|
-
const home =
|
|
88
|
+
const home = homedir();
|
|
89
89
|
process.stdout.write('\nlakonai uninstall\n');
|
|
90
90
|
process.stdout.write('─────────────────\n');
|
|
91
91
|
let any = false;
|
|
@@ -149,7 +149,7 @@ function backupsReport() {
|
|
|
149
149
|
|
|
150
150
|
function listPlatforms() {
|
|
151
151
|
return platforms.list().map((p) => {
|
|
152
|
-
const detected = p.detect(
|
|
152
|
+
const detected = p.detect(homedir());
|
|
153
153
|
const mark = detected ? OK : ' ';
|
|
154
154
|
const scope = p.scope === 'project' ? '[project]' : '[global] ';
|
|
155
155
|
return `${mark} ${p.id.padEnd(14)} ${scope} ${p.label}`;
|
package/src/install/paths.js
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const path = require('path');
|
|
4
|
+
const os = require('os');
|
|
4
5
|
|
|
5
6
|
function claudeConfigDir(home) {
|
|
6
7
|
return process.env.CLAUDE_CONFIG_DIR || path.join(home, '.claude');
|
|
7
8
|
}
|
|
8
9
|
|
|
9
|
-
|
|
10
|
+
// Prefer $HOME (overridable in tests / respected on POSIX) and fall back to the
|
|
11
|
+
// OS-level home. os.homedir() alone ignores a runtime-set process.env.HOME under
|
|
12
|
+
// sandboxed runners like Jest, which breaks home isolation in tests.
|
|
13
|
+
function homedir() {
|
|
14
|
+
return process.env.HOME || os.homedir();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
module.exports = { claudeConfigDir, homedir };
|
package/src/install/platforms.js
CHANGED
|
@@ -30,7 +30,7 @@ function readSafe(p) {
|
|
|
30
30
|
|
|
31
31
|
function hasBlock(filePath) {
|
|
32
32
|
const existing = readSafe(filePath);
|
|
33
|
-
/*
|
|
33
|
+
/* istanbul ignore next */
|
|
34
34
|
if (!existing) return false;
|
|
35
35
|
const re = new RegExp(`${ANY_BEGIN}[\\s\\S]*?${ANY_END}`);
|
|
36
36
|
return re.test(existing);
|
|
@@ -74,9 +74,9 @@ const PLATFORMS = [
|
|
|
74
74
|
const rulePath = upsertBlock(id, path.join(claudeConfigDir(home), 'CLAUDE.md'), rule);
|
|
75
75
|
const hookResult = installHook(home);
|
|
76
76
|
const cmds = installCommands(home);
|
|
77
|
-
/*
|
|
77
|
+
/* istanbul ignore next */
|
|
78
78
|
const suffixHook = hookResult.settingsMerged ? '+ PreToolUse hook' : `(hook: ${hookResult.note})`;
|
|
79
|
-
/*
|
|
79
|
+
/* istanbul ignore next */
|
|
80
80
|
const suffixCmds = cmds.length ? `+ ${cmds.join(' ')}` : '';
|
|
81
81
|
return [rulePath, suffixHook, suffixCmds].filter(Boolean).join(' ');
|
|
82
82
|
},
|
package/src/learn.js
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Automatic filter learning.
|
|
4
|
+
//
|
|
5
|
+
// Reads the Claude Code session transcript (same data source rtk's `discover`
|
|
6
|
+
// uses) at session end, counts how often each *unfiltered* shell command shows
|
|
7
|
+
// up and how much output it produces, and — once a command crosses a confidence
|
|
8
|
+
// floor — promotes it to the "learned" list so it gets a conservative auto-filter
|
|
9
|
+
// from then on. No user action required: the more you use it, the more it covers.
|
|
10
|
+
//
|
|
11
|
+
// Activation goes BEYOND rtk (which only reports/suggests). Safety is preserved
|
|
12
|
+
// by only ever applying the near-lossless filter in src/filters/auto.js.
|
|
13
|
+
|
|
14
|
+
const fs = require('fs');
|
|
15
|
+
const path = require('path');
|
|
16
|
+
const os = require('os');
|
|
17
|
+
const { countTokensApprox } = require('./filters/utils');
|
|
18
|
+
|
|
19
|
+
const MIN_CALLS = Number(process.env.LAKON_LEARN_MIN_CALLS) || 3;
|
|
20
|
+
const MIN_AVG_TOKENS = Number(process.env.LAKON_LEARN_MIN_TOKENS) || 300;
|
|
21
|
+
|
|
22
|
+
/* istanbul ignore next */
|
|
23
|
+
function lakonHome() {
|
|
24
|
+
return process.env.LAKON_HOME || path.join(os.homedir(), '.lakon');
|
|
25
|
+
}
|
|
26
|
+
const statsPath = () => path.join(lakonHome(), 'learn-stats.json');
|
|
27
|
+
const learnedPath = () => path.join(lakonHome(), 'learned.json');
|
|
28
|
+
|
|
29
|
+
function readJson(file, fallback) {
|
|
30
|
+
try {
|
|
31
|
+
return JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
32
|
+
} catch {
|
|
33
|
+
return fallback;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function writeJson(file, data) {
|
|
38
|
+
try {
|
|
39
|
+
fs.mkdirSync(lakonHome(), { recursive: true });
|
|
40
|
+
fs.writeFileSync(file, JSON.stringify(data));
|
|
41
|
+
/* istanbul ignore next */
|
|
42
|
+
} catch {
|
|
43
|
+
// never let learning break the hook
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Base command of a shell string, skipping env assignments / sudo / env.
|
|
48
|
+
function baseCommand(command) {
|
|
49
|
+
const tokens = String(command).trim().split(/\s+/);
|
|
50
|
+
let i = 0;
|
|
51
|
+
while (i < tokens.length && (/^\w+=/.test(tokens[i]) || tokens[i] === 'sudo' || tokens[i] === 'env')) i++;
|
|
52
|
+
return tokens[i] || '';
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Parse a Claude Code transcript (JSONL) into [{ cmd, tokens }] for every Bash
|
|
56
|
+
// tool call, where tokens approximates the size of that call's output.
|
|
57
|
+
function extractBashUsage(transcript) {
|
|
58
|
+
const cmds = new Map(); // tool_use_id -> base command
|
|
59
|
+
const results = []; // { id, tokens }
|
|
60
|
+
for (const line of transcript.split('\n')) {
|
|
61
|
+
if (!line.trim()) continue;
|
|
62
|
+
let obj;
|
|
63
|
+
try {
|
|
64
|
+
obj = JSON.parse(line);
|
|
65
|
+
} catch {
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
const msg = obj.message || obj;
|
|
69
|
+
const content = msg && msg.content;
|
|
70
|
+
if (!Array.isArray(content)) continue;
|
|
71
|
+
for (const item of content) {
|
|
72
|
+
if (item.type === 'tool_use' && item.name === 'Bash' && item.input && typeof item.input.command === 'string') {
|
|
73
|
+
cmds.set(item.id, baseCommand(item.input.command));
|
|
74
|
+
} else if (item.type === 'tool_result') {
|
|
75
|
+
const c = item.content;
|
|
76
|
+
const text =
|
|
77
|
+
typeof c === 'string'
|
|
78
|
+
? c
|
|
79
|
+
: Array.isArray(c)
|
|
80
|
+
? c.map((x) => (typeof x === 'string' ? x : x.text || '')).join('\n')
|
|
81
|
+
: '';
|
|
82
|
+
results.push({ id: item.tool_use_id, tokens: countTokensApprox(text) });
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
const out = [];
|
|
87
|
+
for (const r of results) {
|
|
88
|
+
const cmd = cmds.get(r.id);
|
|
89
|
+
if (cmd) out.push({ cmd, tokens: r.tokens });
|
|
90
|
+
}
|
|
91
|
+
return out;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Promote stats entries that cross the floor (and aren't already built-in or
|
|
95
|
+
// learned) into the learned list. Returns the (possibly grown) learned array.
|
|
96
|
+
function promote(stats, isBuiltin) {
|
|
97
|
+
const learned = new Set(readJson(learnedPath(), []));
|
|
98
|
+
let changed = false;
|
|
99
|
+
for (const [cmd, s] of Object.entries(stats)) {
|
|
100
|
+
if (learned.has(cmd) || isBuiltin(cmd)) continue;
|
|
101
|
+
if (s.count >= MIN_CALLS && s.tokens / s.count >= MIN_AVG_TOKENS) {
|
|
102
|
+
learned.add(cmd);
|
|
103
|
+
changed = true;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
const arr = [...learned];
|
|
107
|
+
if (changed) writeJson(learnedPath(), arr);
|
|
108
|
+
return arr;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Orchestrator called at session end by the stop hook.
|
|
112
|
+
function analyzeTranscript(transcriptPath, isBuiltin) {
|
|
113
|
+
if (process.env.LAKON_NO_LEARN === '1' || process.env.LAKON_NO_TRACK === '1') return;
|
|
114
|
+
let text;
|
|
115
|
+
try {
|
|
116
|
+
text = fs.readFileSync(transcriptPath, 'utf8');
|
|
117
|
+
} catch {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const usage = extractBashUsage(text);
|
|
121
|
+
if (!usage.length) return;
|
|
122
|
+
const stats = readJson(statsPath(), {});
|
|
123
|
+
for (const u of usage) {
|
|
124
|
+
if (!u.cmd || isBuiltin(u.cmd)) continue;
|
|
125
|
+
const s = stats[u.cmd] || { count: 0, tokens: 0 };
|
|
126
|
+
s.count += 1;
|
|
127
|
+
s.tokens += u.tokens;
|
|
128
|
+
stats[u.cmd] = s;
|
|
129
|
+
}
|
|
130
|
+
writeJson(statsPath(), stats);
|
|
131
|
+
promote(stats, isBuiltin);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
let _learnedCache;
|
|
135
|
+
function learnedCommands() {
|
|
136
|
+
if (_learnedCache === undefined) _learnedCache = new Set(readJson(learnedPath(), []));
|
|
137
|
+
return _learnedCache;
|
|
138
|
+
}
|
|
139
|
+
function isLearned(cmd) {
|
|
140
|
+
return learnedCommands().has(cmd);
|
|
141
|
+
}
|
|
142
|
+
/* istanbul ignore next -- test helper to reset the per-process cache */
|
|
143
|
+
function _resetCache() {
|
|
144
|
+
_learnedCache = undefined;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
module.exports = {
|
|
148
|
+
baseCommand,
|
|
149
|
+
extractBashUsage,
|
|
150
|
+
promote,
|
|
151
|
+
analyzeTranscript,
|
|
152
|
+
learnedCommands,
|
|
153
|
+
isLearned,
|
|
154
|
+
_resetCache,
|
|
155
|
+
statsPath,
|
|
156
|
+
learnedPath,
|
|
157
|
+
};
|
package/src/rules/lakonai.md
CHANGED
|
@@ -52,8 +52,14 @@ For any shell command whose output you read into context, prefix it with `lakona
|
|
|
52
52
|
| `lakonai ls -la` | `ls -la` |
|
|
53
53
|
| `lakonai cat <file>` | `cat <file>` |
|
|
54
54
|
| `lakonai grep -r …` | `grep -r …` |
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
| `lakonai find …` | `find …` |
|
|
56
|
+
| `lakonai npm test` | `npm test` |
|
|
57
|
+
| `lakonai pytest` | `pytest` |
|
|
58
|
+
| `lakonai tsc` | `tsc` |
|
|
59
|
+
| `lakonai make` | `make` |
|
|
60
|
+
|
|
61
|
+
It also compresses test runners (jest/vitest/pytest/go test/cargo test), linters
|
|
62
|
+
and builds (eslint/ruff/clippy/tsc/make), package installs, and docker/kubectl/aws.
|
|
57
63
|
|
|
58
64
|
Unsupported commands run unchanged through `lakonai`, so when in doubt, prefix it.
|
|
59
65
|
|
package/src/tracking.js
CHANGED
|
@@ -9,7 +9,7 @@ const DAY_MS = 24 * HOUR_MS;
|
|
|
9
9
|
const WEEK_MS = 7 * DAY_MS;
|
|
10
10
|
|
|
11
11
|
function dataDir() {
|
|
12
|
-
/*
|
|
12
|
+
/* istanbul ignore next */
|
|
13
13
|
return process.env.LAKON_HOME || path.join(os.homedir(), '.lakon');
|
|
14
14
|
}
|
|
15
15
|
|
|
@@ -30,7 +30,7 @@ function record({ cmd, args, rawTokens, filteredTokens }) {
|
|
|
30
30
|
saved: Math.max(0, rawTokens - filteredTokens),
|
|
31
31
|
};
|
|
32
32
|
fs.appendFileSync(logPath(), JSON.stringify(entry) + '\n');
|
|
33
|
-
/*
|
|
33
|
+
/* istanbul ignore next */
|
|
34
34
|
} catch {
|
|
35
35
|
// never let tracking break a user command
|
|
36
36
|
}
|
|
@@ -147,7 +147,7 @@ const cyan = (s) => paint(s, '36');
|
|
|
147
147
|
|
|
148
148
|
function pad(s, n) {
|
|
149
149
|
s = String(s);
|
|
150
|
-
/*
|
|
150
|
+
/* istanbul ignore next */
|
|
151
151
|
if (s.length >= n) return s;
|
|
152
152
|
return s + ' '.repeat(n - s.length);
|
|
153
153
|
}
|