lakonai 1.5.0 → 1.5.1

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-1165-0F0F0F" alt="1165 tests" />
24
24
  </p>
25
25
 
26
26
  <p align="center">
@@ -377,7 +377,7 @@ Input is measured and deterministic. Output is estimated by your local AI CLI (n
377
377
 
378
378
  ## Test suite
379
379
 
380
- 1075 tests across 68 suites — all passing, no mocks on I/O boundaries.
380
+ 1165 tests across 69 suites — all passing, no mocks on I/O boundaries.
381
381
 
382
382
  | Type | Suites | Tests |
383
383
  |------|--------|-------|
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lakonai",
3
- "version": "1.5.0",
3
+ "version": "1.5.1",
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 };