lakonai 1.5.0 → 1.6.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
@@ -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-1075-0F0F0F" alt="1075 tests" />
23
+ <img src="https://img.shields.io/badge/tests-1178-0F0F0F" alt="1178 tests" />
24
24
  </p>
25
25
 
26
26
  <p align="center">
@@ -91,6 +91,7 @@ All numbers from live projects using `lakonai@1.1.1`.
91
91
  **Typical coding session:** ~53% savings from proxy alone, ~75% with graph read-guard.
92
92
 
93
93
  `lakonai gain` shows your own numbers. `/lakonai:stats` shows all layers at once.
94
+ `lakonai gain --share` turns them into a short card you can post.
94
95
 
95
96
  ---
96
97
 
@@ -318,7 +319,7 @@ After `lakonai install`, these are available in Claude Code:
318
319
  | `lakonai compress-memory <file>` | Shrink a memory file via your local AI CLI |
319
320
  | `lakonai peek [id]` | Read output parked in sandbox (`--grep/--offset/--limit`) |
320
321
  | `lakonai depth [on\|off\|status]` | Answer budget: `on` buys one long reply, `off` cancels, `status` reports |
321
- | `lakonai gain` | Token savings across all measured fronts |
322
+ | `lakonai gain [--share]` | Token savings across all measured fronts; `--share` prints a plain-text card to post (totals and command names only — no args or paths) |
322
323
  | `lakonai inspect <cmd>` | Debug what filter applies to a command |
323
324
  | `lakonai mcp [wrap\|unwrap]` | MCP catalog compression status/lifecycle |
324
325
  | `lakonai proxy [start\|stop\|restart]` | Compression proxy status/lifecycle |
@@ -377,7 +378,7 @@ Input is measured and deterministic. Output is estimated by your local AI CLI (n
377
378
 
378
379
  ## Test suite
379
380
 
380
- 1075 tests across 68 suites — all passing, no mocks on I/O boundaries.
381
+ 1178 tests across 70 suites — all passing, no mocks on I/O boundaries.
381
382
 
382
383
  | Type | Suites | Tests |
383
384
  |------|--------|-------|
package/bin/lakonai.js CHANGED
@@ -55,7 +55,9 @@ Usage:
55
55
 
56
56
  lakonai gain Show token savings - INPUT (shell output, measured)
57
57
  AND OUTPUT (how much terser the model writes; measured
58
- weekly via your local AI CLI, no API key)
58
+ weekly via your local AI CLI, no API key).
59
+ --share prints a plain-text card of your totals to
60
+ post anywhere (no paths or args included)
59
61
  lakonai mcp [cmd] MCP catalog compression: status (default) | wrap |
60
62
  unwrap. Wrapping edits ~/.claude.json, where Claude
61
63
  Code keeps per-project session state, so it is
@@ -628,6 +630,10 @@ async function main() {
628
630
  return;
629
631
  }
630
632
  if (first === 'gain' || first === 'stats') {
633
+ if (rest.includes('--share')) {
634
+ process.stdout.write(tracking.shareReport());
635
+ return;
636
+ }
631
637
  process.stdout.write(tracking.report());
632
638
  // Output side: measure how much terser the model writes (weekly, TTY-only,
633
639
  // via the local AI CLI), then show it alongside the input savings.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lakonai",
3
- "version": "1.5.0",
3
+ "version": "1.6.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",
@@ -19,16 +19,43 @@ const { countTokensApprox } = require('../filters');
19
19
  // results are short, and TodoWrite & friends are structural.
20
20
  const SPILLABLE_TOOLS = new Set(['Bash', 'Read', 'Grep', 'Glob', 'WebFetch', 'Task']);
21
21
 
22
- // The result arrives as a string or as a structured object depending on the tool.
22
+ // The result arrives as a string or as a structured object depending on the tool:
23
+ // Bash {stdout, stderr, interrupted, ...}, Read {file: {content}}, Grep {content},
24
+ // WebFetch {result}. Each entry reads the payload and rebuilds the SAME shape
25
+ // around a replacement; Claude Code drops an updatedToolOutput whose shape differs
26
+ // from the tool's own ("does not match Bash's output shape; using original output").
27
+ const TEXT_FIELDS = [
28
+ { get: (r) => r.content, set: (r, v) => ({ ...r, content: v }) },
29
+ { get: (r) => r.file?.content, set: (r, v) => ({ ...r, file: { ...r.file, content: v } }) },
30
+ { get: (r) => r.output, set: (r, v) => ({ ...r, output: v }) },
31
+ { get: (r) => r.stdout, set: (r, v) => ({ ...r, stdout: v }) },
32
+ { get: (r) => r.result, set: (r, v) => ({ ...r, result: v }) },
33
+ ];
34
+
35
+ function rawResponse(data) {
36
+ return data.tool_response !== undefined ? data.tool_response : data.tool_output;
37
+ }
38
+
39
+ // First field holding a string. Mirrors the old `??` chain: a present but
40
+ // non-string field (Task's content array) stops the search, so the caller falls
41
+ // back to stringifying instead of picking a later, unrelated field.
42
+ function textField(r) {
43
+ for (const f of TEXT_FIELDS) {
44
+ const v = f.get(r);
45
+ if (v != null) return typeof v === 'string' ? f : null;
46
+ }
47
+ return null;
48
+ }
49
+
23
50
  function extractText(data) {
24
- const r = data.tool_response !== undefined ? data.tool_response : data.tool_output;
51
+ const r = rawResponse(data);
25
52
  if (r == null) return null;
26
53
  if (typeof r === 'string') return r;
27
- // Read returns {file: {content}}; others vary. Prefer a real content field over
28
- // stringifying the wrapper, which would park JSON noise instead of the payload.
54
+ // Prefer a real content field over stringifying the wrapper, which would park
55
+ // JSON noise instead of the payload.
29
56
  if (typeof r === 'object') {
30
- const content = r.content ?? r.file?.content ?? r.output ?? r.stdout;
31
- if (typeof content === 'string') return content;
57
+ const f = textField(r);
58
+ if (f) return f.get(r);
32
59
  try {
33
60
  return JSON.stringify(r);
34
61
  } catch {
@@ -38,6 +65,19 @@ function extractText(data) {
38
65
  return null;
39
66
  }
40
67
 
68
+ // The tool result with its payload swapped for `digest`, in the tool's own shape.
69
+ // null when the payload has no single text field to swap (stringified fallback):
70
+ // leaving the result alone beats a replacement Claude Code will reject.
71
+ function replaceText(data, digest) {
72
+ const r = rawResponse(data);
73
+ if (typeof r === 'string') return digest;
74
+ if (r && typeof r === 'object') {
75
+ const f = textField(r);
76
+ if (f) return f.set(r, digest);
77
+ }
78
+ return null;
79
+ }
80
+
41
81
  // bin/lakonai.js may already have parked this very output; re-parking a digest
42
82
  // would nest one spill inside another and lose the original id.
43
83
  function isAlreadyDigest(text) {
@@ -72,6 +112,9 @@ async function main() {
72
112
  const text = extractText(data);
73
113
  if (!decide({ toolName, text, threshold: sandbox.spillThreshold() })) process.exit(0);
74
114
 
115
+ // Check before parking, so an unreplaceable result leaves no orphan spill.
116
+ if (replaceText(data, '') === null) process.exit(0);
117
+
75
118
  const label = toolName === 'Bash' ? data.tool_input?.command || 'Bash' : toolName;
76
119
  const parked = sandbox.spill({ cmd: label, args: [], exitCode: null, text });
77
120
  // Disk failure must leave the original result untouched, not blank it.
@@ -81,7 +124,7 @@ async function main() {
81
124
  JSON.stringify({
82
125
  hookSpecificOutput: {
83
126
  hookEventName: 'PostToolUse',
84
- updatedToolOutput: parked.digest,
127
+ updatedToolOutput: replaceText(data, parked.digest),
85
128
  },
86
129
  })
87
130
  );
@@ -95,4 +138,4 @@ async function main() {
95
138
  /* istanbul ignore next */
96
139
  if (require.main === module) main();
97
140
 
98
- module.exports = { extractText, isAlreadyDigest, decide, SPILLABLE_TOOLS };
141
+ module.exports = { extractText, replaceText, isAlreadyDigest, decide, SPILLABLE_TOOLS };
package/src/tracking.js CHANGED
@@ -221,9 +221,39 @@ function report() {
221
221
  return lines.join('\n') + '\n';
222
222
  }
223
223
 
224
+ // A plain-text card for posting your savings (`lakonai gain --share`). No
225
+ // color, no args, no paths: only totals, the command names and the agent -
226
+ // nothing from the user's projects leaves the log. Short enough for one post.
227
+ const AGENT_NAMES = { claude: 'Claude Code', codex: 'Codex', gemini: 'Gemini CLI' };
228
+ const REPO_URL = 'https://github.com/bargadev/lakonai';
229
+
230
+ function shareText(entries, now = Date.now()) {
231
+ const all = aggregate(entries);
232
+ if (!all.calls) return null;
233
+ const first = entries.reduce((m, e) => (!isSessionEntry(e) && e.t && e.t < m ? e.t : m), now);
234
+ const days = Math.max(1, Math.ceil((now - first) / DAY_MS));
235
+ const agents = byAgent(entries)
236
+ .map((a) => AGENT_NAMES[a.agent])
237
+ .filter(Boolean);
238
+ const where = agents.length ? ` in ${agents.join(' + ')}` : '';
239
+ const top = byCommand(entries).filter((c) => c.saved > 0).slice(0, 3);
240
+ const lines = [
241
+ `lakonai saved me ${tok(all.saved)} (${pct(all.saved, all.raw)}% smaller) across ` +
242
+ `${all.calls.toLocaleString('en-US')} command${all.calls === 1 ? '' : 's'}${where}, ${days} day${days === 1 ? '' : 's'}.`,
243
+ ];
244
+ if (top.length) lines.push('Top: ' + top.map((c) => `${c.cmd} ${fmt(c.saved)}`).join(' · '));
245
+ lines.push(`npm i -g lakonai → ${REPO_URL}`);
246
+ return lines.join('\n') + '\n';
247
+ }
248
+
249
+ function shareReport() {
250
+ return shareText(readEntries()) ||
251
+ 'lakonai: no usage recorded yet. Run a few commands through `lakonai` first.\n';
252
+ }
253
+
224
254
  function reset() {
225
255
  try { fs.unlinkSync(logPath()); return true; }
226
256
  catch { return false; }
227
257
  }
228
258
 
229
- module.exports = { record, report, reset, readEntries, logPath, responseStats, detectAgent, byAgent };
259
+ module.exports = { record, report, shareText, shareReport, reset, readEntries, logPath, responseStats, detectAgent, byAgent };