lakonai 1.2.6 → 1.2.7

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-875-0F0F0F" alt="875 tests" />
23
+ <img src="https://img.shields.io/badge/tests-895-0F0F0F" alt="895 tests" />
24
24
  </p>
25
25
 
26
26
  <p align="center">
@@ -342,7 +342,7 @@ Input is measured and deterministic. Output is estimated by your local AI CLI (n
342
342
 
343
343
  ## Test suite
344
344
 
345
- 875 tests across 57 suites — all passing, no mocks on I/O boundaries.
345
+ 895 tests across 58 suites — all passing, no mocks on I/O boundaries.
346
346
 
347
347
  | Type | Suites | Tests |
348
348
  |------|--------|-------|
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lakonai",
3
- "version": "1.2.6",
3
+ "version": "1.2.7",
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",
@@ -54,6 +54,16 @@ async function main() {
54
54
  const update = await checkForUpdate();
55
55
  if (update) parts.push(formatNotice(update));
56
56
 
57
+ // An upgrade leaves the old daemon running its old code; this is where a
58
+ // new session notices and replaces it.
59
+ if (!process.env.LAKON_NO_PROXY_REFRESH) {
60
+ try {
61
+ const { refreshStaleDaemon, formatRefreshNotice } = require('../proxy/refresh');
62
+ const notice = formatRefreshNotice(await refreshStaleDaemon());
63
+ if (notice) parts.push(notice);
64
+ } catch { /* best-effort */ }
65
+ }
66
+
57
67
  try {
58
68
  const learnReport = require('../learn-report');
59
69
  const summary = learnReport.maybeGetUnseen();
@@ -166,7 +166,7 @@ function reapRetired() {
166
166
  // Start the daemon and wait until it actually serves. Returns
167
167
  // `{running: false, error}` when it does not — callers must check before
168
168
  // touching the user's shell rc.
169
- async function start({ allowRestart = true } = {}) {
169
+ async function start({ allowRestart = true, port: forcedPort = null } = {}) {
170
170
  reapRetired();
171
171
  const current = await status();
172
172
  if (current.running) {
@@ -177,8 +177,13 @@ async function start({ allowRestart = true } = {}) {
177
177
  // Retire, do not kill: an upgrade run from inside a Claude Code session
178
178
  // would otherwise drop that session's own connection to the proxy.
179
179
  const how = await retire({ pid: current.pid, port: current.port, version: current.version });
180
+ const held = current.port;
180
181
  clearState();
181
- const next = await start({ allowRestart: false });
182
+ // Carry the port across the clear. Sessions already running have that
183
+ // port baked into ANTHROPIC_BASE_URL for life, so the replacement has to
184
+ // land on it; re-reading preferredPort() here would find the state we
185
+ // just cleared and fall back to DEFAULT_PORT, stranding every one of them.
186
+ const next = await start({ allowRestart: false, port: held });
182
187
  return { ...next, replaced: how };
183
188
  }
184
189
  // The env script is what the shell actually reads; an upgrade that adopts a
@@ -192,7 +197,7 @@ async function start({ allowRestart = true } = {}) {
192
197
  // cannot be told to look elsewhere. Rebinding the same port is the only thing
193
198
  // that brings those sessions back; clearing first would send us to the
194
199
  // default and strand every one of them on ECONNREFUSED.
195
- const port = preferredPort();
200
+ const port = forcedPort || preferredPort();
196
201
  clearState(); // drop stale pid/port so we don't read the dead daemon's values
197
202
  const child = spawn(process.execPath, [serverScript], {
198
203
  detached: true,
@@ -0,0 +1,86 @@
1
+ 'use strict';
2
+
3
+ // Replacing a daemon left behind by an upgrade.
4
+ //
5
+ // `npm i -g lakonai` only swaps files on disk. The daemon already running keeps
6
+ // executing the `server.js` it was started with, so after an upgrade the CLI is
7
+ // new and the process serving every session is old code — the fix the user just
8
+ // installed is not the one answering their requests.
9
+ //
10
+ // `daemon.start()` already knows how to replace a version-mismatched daemon
11
+ // (retire the old one, rebind the same port). Nothing was calling it after an
12
+ // upgrade: npm runs no postinstall, and the only callers are `lakonai install`
13
+ // and `lakonai proxy start|restart`. This module is the missing trigger, run
14
+ // from the SessionStart hook — the moment a new session is about to depend on
15
+ // the proxy, and the cheapest place to notice the mismatch.
16
+
17
+ const daemon = require('./daemon');
18
+
19
+ const VERSION = require('../../package.json').version;
20
+
21
+ // Deps are injected so the unit tests can drive every branch without spawning
22
+ // a real daemon; production callers pass nothing.
23
+ async function refreshStaleDaemon({
24
+ status = daemon.status,
25
+ start = daemon.start,
26
+ version = VERSION,
27
+ } = {}) {
28
+ let current;
29
+ try {
30
+ current = await status();
31
+ } catch {
32
+ return null; // a status probe that throws is not a reason to fail a session
33
+ }
34
+
35
+ // Nothing running: starting one here is not this hook's job. A session that
36
+ // needs the proxy gets it from `lakonai install` / `lakonai proxy start`.
37
+ if (!current || !current.running) return null;
38
+
39
+ // Same comparison daemon.start() makes: an unstamped daemon (pre-1.2.3) is
40
+ // stale by definition, so `null !== version` correctly counts as a mismatch.
41
+ if (current.version === version) return null;
42
+
43
+ const from = current.version;
44
+ const fromPort = current.port;
45
+
46
+ let res;
47
+ try {
48
+ res = await start();
49
+ } catch (err) {
50
+ return { refreshed: false, from, to: version, error: err.message };
51
+ }
52
+
53
+ if (!res || !res.running) {
54
+ return { refreshed: false, from, to: version, error: (res && res.error) || 'the proxy did not come back up' };
55
+ }
56
+
57
+ return {
58
+ refreshed: true,
59
+ from,
60
+ to: version,
61
+ port: res.port,
62
+ // The old daemon held the port and was too old to hand it over, so the new
63
+ // one landed somewhere else. Sessions already pinned to `fromPort` keep
64
+ // talking to the old process until it goes idle and gets reaped.
65
+ movedPort: res.port !== fromPort ? fromPort : null,
66
+ replaced: res.replaced || null,
67
+ };
68
+ }
69
+
70
+ function formatRefreshNotice(result) {
71
+ if (!result) return null;
72
+
73
+ const from = result.from || 'an unstamped build';
74
+
75
+ if (!result.refreshed) {
76
+ return `lakonai proxy: the running daemon is still ${from} after the upgrade to ${result.to}, and restarting it failed (${result.error}).\nRun \`lakonai proxy restart\` to pick up the new code.`;
77
+ }
78
+
79
+ const lines = [`lakonai proxy: replaced the ${from} daemon left over by the upgrade — now serving ${result.to} on 127.0.0.1:${result.port}.`];
80
+ if (result.movedPort) {
81
+ lines.push(`The old daemon was too old to release port ${result.movedPort} and is still draining there; sessions pinned to it keep working and it is reaped once idle.`);
82
+ }
83
+ return lines.join('\n');
84
+ }
85
+
86
+ module.exports = { refreshStaleDaemon, formatRefreshNotice, VERSION };