vexp-mcp 2.2.0 → 2.2.2

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.
@@ -1,9 +1,72 @@
1
1
  import * as net from "net";
2
2
  import * as path from "path";
3
+ import * as os from "os";
3
4
  import * as fs from "fs";
4
5
  import { spawn } from "child_process";
5
6
  import { fileURLToPath } from "url";
6
7
  import { randomUUID } from "crypto";
8
+ /**
9
+ * The quota marker for `workspaceRoot`, or null when absent or no longer binding.
10
+ *
11
+ * This is our side of the contract the VS Code extension gets for free from the
12
+ * daemon's exit code: the MCP client spawns the daemon detached (it is a
13
+ * grandchild via `daemon-cmd start`), so it never observes how the daemon exited
14
+ * and cannot otherwise tell "stopped on purpose, quota spent" from "crashed,
15
+ * please restart".
16
+ *
17
+ * Two ways a marker stops binding, both of which also delete it:
18
+ * - `until` has passed — the quota reset.
19
+ * - a license file is newer than the marker — the user upgraded mid-day, and
20
+ * must not stay locked out until midnight over a stale free-tier marker.
21
+ */
22
+ export function readDailyLimitMarker(workspaceRoot) {
23
+ const markerPath = path.join(workspaceRoot, ".vexp", "daily-limit.json");
24
+ const drop = () => { try {
25
+ fs.unlinkSync(markerPath);
26
+ }
27
+ catch { /* raced */ } };
28
+ try {
29
+ const stat = fs.statSync(markerPath);
30
+ const marker = JSON.parse(fs.readFileSync(markerPath, "utf-8"));
31
+ if (!marker?.until || !(Date.parse(marker.until) > Date.now())) {
32
+ drop();
33
+ return null;
34
+ }
35
+ const home = process.env.VEXP_HOME || os.homedir();
36
+ for (const name of ["license.jwt", "fresh.jwt"]) {
37
+ try {
38
+ if (fs.statSync(path.join(home, ".vexp", name)).mtimeMs > stat.mtimeMs) {
39
+ drop();
40
+ return null;
41
+ }
42
+ }
43
+ catch { /* no such license file — still on the free tier */ }
44
+ }
45
+ return marker;
46
+ }
47
+ catch {
48
+ // Missing or unparseable marker: never gate on what we can't read.
49
+ return null;
50
+ }
51
+ }
52
+ /**
53
+ * Terminal error for a daemon that is down on purpose.
54
+ *
55
+ * Written at the model, not at a human: an agent that gets a bare
56
+ * `connect ENOENT` reads it as a transient glitch and retries forever (one user
57
+ * burned 200k tokens that way). It has to be told, in words, that no retry can
58
+ * succeed and that this is not a broken install.
59
+ */
60
+ export function dailyLimitError(marker) {
61
+ return new Error(`vexp daily free limit reached (${marker.used}/${marker.limit} calls used today). ` +
62
+ `The vexp daemon stopped on purpose and will not restart until the quota ` +
63
+ `resets at ${marker.until} (midnight UTC).\n\n` +
64
+ `AGENT INSTRUCTION: do NOT retry this or any other vexp tool call — every ` +
65
+ `one will fail until the reset, and nothing is broken or misconfigured. ` +
66
+ `Carry on with your own tools (file reads, search) and tell the user their ` +
67
+ `vexp free daily limit is spent.\n\n` +
68
+ `To lift the limit now, upgrade to Pro or Team: https://vexp.dev/#pricing`);
69
+ }
7
70
  /**
8
71
  * Client per comunicare con il vexp-core daemon via Unix socket / Named pipe.
9
72
  * Protocol: JSON lines (newline-delimited JSON).
@@ -52,6 +115,11 @@ export class DaemonClient {
52
115
  if (process.env.VEXP_NO_AUTOSTART === "1")
53
116
  return;
54
117
  const workspaceRoot = resolveWorkspaceRoot(this.spawnWorkspaceRoot);
118
+ // Never resurrect a daemon that stopped on purpose: it would re-exit on its
119
+ // first quota check. `call()` already answers this case before reaching us;
120
+ // the guard keeps the contract true for any other caller.
121
+ if (readDailyLimitMarker(workspaceRoot))
122
+ return;
55
123
  if (!fs.existsSync(path.join(workspaceRoot, ".vexp", "manifest.json")))
56
124
  return;
57
125
  const binary = resolveVexpCoreBinary();
@@ -124,6 +192,15 @@ export class DaemonClient {
124
192
  }
125
193
  catch (err) {
126
194
  lastError = err instanceof Error ? err : new Error(String(err));
195
+ // An unreachable daemon may be down BY DESIGN (free-tier quota spent).
196
+ // Answer that in words and stop here: retrying would respawn a daemon
197
+ // that re-exits on its next quota check, and the agent would only ever
198
+ // see connection errors it reads as "transient, try again".
199
+ if (DaemonClient.isRetryableError(lastError)) {
200
+ const quota = readDailyLimitMarker(resolveWorkspaceRoot(this.spawnWorkspaceRoot));
201
+ if (quota)
202
+ throw dailyLimitError(quota);
203
+ }
127
204
  if (attempt < DaemonClient.MAX_RETRIES && DaemonClient.isRetryableError(lastError)) {
128
205
  // On the first failure, (re)start the daemon (cooldown-gated so a
129
206
  // long-lived supervisor can resurrect it whenever it dies).
@@ -150,6 +227,15 @@ export class DaemonClient {
150
227
  if (tail && !err.message.includes("--- daemon.log") && !err.message.includes("--- vexp.log")) {
151
228
  err.message += tail;
152
229
  }
230
+ // Targeting diagnostics apply only to the stdio path that RESOLVES the target
231
+ // (env/cwd/registry). The HTTP supervisor (spawnWorkspaceRoot) and explicit-
232
+ // socket callers (tests, one-shot CLI) already know their target, so leave
233
+ // those error paths byte-for-byte unchanged.
234
+ if (!this.explicitSocketPath &&
235
+ !this.spawnWorkspaceRoot &&
236
+ !err.message.includes("--- vexp targeting diagnostics ---")) {
237
+ err.message += targetingDiagnostic();
238
+ }
153
239
  return err;
154
240
  }
155
241
  /**
@@ -547,6 +633,33 @@ function pickFromDaemonRegistry(workspaceHint) {
547
633
  return bestSock;
548
634
  return null;
549
635
  }
636
+ /** All live `[workspace, socket]` entries in `~/.vexp/daemons.json`, dropping
637
+ * stale ones whose socket no longer exists (same liveness filter as
638
+ * pickFromDaemonRegistry; Windows named pipes are always kernel-exposed, so no
639
+ * file check there). Diagnostics only — never used to pick a target. */
640
+ function liveDaemonEntries() {
641
+ const home = process.env.HOME ?? process.env.USERPROFILE ?? "";
642
+ if (!home)
643
+ return [];
644
+ let registry;
645
+ try {
646
+ registry = JSON.parse(fs.readFileSync(path.join(home, ".vexp", "daemons.json"), "utf-8"));
647
+ }
648
+ catch {
649
+ return [];
650
+ }
651
+ const all = Object.entries(registry);
652
+ if (process.platform === "win32")
653
+ return all;
654
+ return all.filter(([, sock]) => {
655
+ try {
656
+ return fs.statSync(sock).isSocket();
657
+ }
658
+ catch {
659
+ return false;
660
+ }
661
+ });
662
+ }
550
663
  /**
551
664
  * Resolve the daemon socket/pipe AND record which signal decided the target.
552
665
  * Single source of truth for `resolveDaemonSocket()` (which discards the
@@ -603,6 +716,73 @@ export function resolveTargeting() {
603
716
  export function resolveDaemonSocket() {
604
717
  return resolveTargeting().socket;
605
718
  }
719
+ /**
720
+ * A human-readable "why can't vexp find the daemon" block, appended to a
721
+ * connection error so a targeting failure explains itself instead of surfacing
722
+ * a bare `connect ENOENT /.vexp/daemon.sock`. Shows which signals we checked,
723
+ * the workspace we resolved and how, the socket we tried, and the daemons that
724
+ * ARE alive — so a user can see e.g. that their project's daemon is running but
725
+ * the host never told us which project this session is. Read-only and
726
+ * defensive: it must never throw over the real error.
727
+ */
728
+ export function targetingDiagnostic() {
729
+ let t;
730
+ try {
731
+ t = resolveTargeting();
732
+ }
733
+ catch {
734
+ return "";
735
+ }
736
+ const vw = process.env.VEXP_WORKSPACE;
737
+ const cpd = process.env.CLAUDE_PROJECT_DIR;
738
+ const sockTried = t.socket ?? path.join(t.workspaceRoot, ".vexp", "daemon.sock");
739
+ const sockExists = (() => { try {
740
+ return fs.existsSync(sockTried);
741
+ }
742
+ catch {
743
+ return false;
744
+ } })();
745
+ const indexed = fs.existsSync(path.join(t.workspaceRoot, ".vexp", "manifest.json")) ||
746
+ fs.existsSync(path.join(t.workspaceRoot, ".vexp", "index.db"));
747
+ // "Unresolved" = we had no targeting signal AND walking up from cwd hit no
748
+ // indexed workspace, so the root is a meaningless cwd fallback (the Devin /
749
+ // GUI-launched `cwd=/` case). This is distinct from "resolved fine but the
750
+ // daemon is down".
751
+ const unresolved = t.source === "cwd-discovery" && !indexed;
752
+ const live = liveDaemonEntries();
753
+ const lines = [
754
+ "",
755
+ "--- vexp targeting diagnostics ---",
756
+ "The MCP server could not reach a vexp daemon for this session.",
757
+ "",
758
+ "Workspace resolution:",
759
+ ` VEXP_WORKSPACE ${vw || "(not set)"}`,
760
+ ` CLAUDE_PROJECT_DIR ${cpd || "(not set)"}`,
761
+ ` resolved workspace ${t.workspaceRoot} (via ${t.source}` +
762
+ `${unresolved ? " — no indexed .vexp/ found walking up from cwd" : ""})`,
763
+ ` socket tried ${sockTried}${sockExists ? "" : " (missing)"}`,
764
+ "",
765
+ ];
766
+ if (live.length) {
767
+ lines.push(`Live daemons in ~/.vexp/daemons.json (${live.length}):`);
768
+ for (const [ws, sock] of live.slice(0, 8))
769
+ lines.push(` ${ws} -> ${sock}`);
770
+ if (live.length > 8)
771
+ lines.push(` ... and ${live.length - 8} more`);
772
+ }
773
+ else {
774
+ lines.push("No live daemons are registered in ~/.vexp/daemons.json.");
775
+ }
776
+ lines.push("");
777
+ if (unresolved) {
778
+ lines.push("This host launched the MCP server outside your project and passed no", "VEXP_WORKSPACE, so vexp cannot tell which project you mean. Set VEXP_WORKSPACE", "to your project root in the MCP server's env" +
779
+ (live.length === 1 ? `, e.g. VEXP_WORKSPACE=${live[0][0]}` : "") + ".");
780
+ }
781
+ else if (!live.some(([ws]) => ws.toLowerCase() === t.workspaceRoot.toLowerCase())) {
782
+ lines.push(`No live daemon is registered for ${t.workspaceRoot}.`, "Start it with `vexp daemon-cmd start` in that directory, or `vexp index` to (re)index.");
783
+ }
784
+ return lines.join("\n");
785
+ }
606
786
  /** Back-compat wrapper: some callers expect a string (old behavior).
607
787
  * Returns the resolved path or the nominal workspace path so the caller's
608
788
  * connect attempt produces a meaningful ENOENT with the real path. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vexp-mcp",
3
- "version": "2.2.0",
3
+ "version": "2.2.2",
4
4
  "description": "vexp MCP server — AI context tools for coding agents",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",