realontext 0.8.1 → 0.9.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.
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "realontext",
3
- "version": "0.8.1",
3
+ "version": "0.9.0",
4
4
  "description": "Code search for coding agents, over the directory you are in. Needs `npm install -g realontext`.",
5
5
  "author": {
6
6
  "name": "FlowerRealm",
package/README.md CHANGED
@@ -65,9 +65,8 @@ most once an hour, `realontext` asks the Relay for it; a newer one is
65
65
  downloaded from the npm registry (`npm_config_registry` if set), checked
66
66
  against the registry's integrity, and kept in `~/.local/share/realontext`
67
67
  (`%LOCALAPPDATA%\realontext` on Windows). The next start runs it. An agent's
68
- `realontext mcp` asks when it starts and every hour, and switches to the new
69
- binary between two requests, without the agent seeing it; a query the Relay
70
- refuses as too old waits for the new binary and goes again. A newer
68
+ `realontext mcp` asks before every query and, when there is a newer binary,
69
+ runs the query on it, without the agent seeing the switch. A newer
71
70
  `npm install -g realontext` runs in its place. `REALONTEXT_NO_AUTO_UPDATE=1`
72
71
  turns this off.
73
72
 
package/bin/realontext.js CHANGED
@@ -26,9 +26,10 @@ if (terminal && (command === undefined || (command === "setup" && rest.length ==
26
26
  } else if (command === "setup") {
27
27
  process.exit(require("../lib/setup.js").setup(rest));
28
28
  } else {
29
+ const binary = require("../lib/binary.js");
29
30
  let path;
30
31
  try {
31
- path = require("../lib/binary.js")();
32
+ path = binary();
32
33
  } catch (e) {
33
34
  console.error(`realontext: ${e.message}`);
34
35
  process.exit(1);
@@ -36,7 +37,7 @@ if (terminal && (command === undefined || (command === "setup" && rest.length ==
36
37
  if (command === "mcp") {
37
38
  require("../lib/mcp.js")(path, rest).then(exit);
38
39
  } else {
39
- const run = spawnSync(path, process.argv.slice(2), { stdio: "inherit" });
40
+ const run = spawnSync(path, process.argv.slice(2), { stdio: "inherit", env: binary.env(path) });
40
41
  if (run.error) {
41
42
  console.error(`realontext: ${run.error.message}`);
42
43
  process.exit(1);
package/lib/binary.js CHANGED
@@ -76,4 +76,19 @@ function binary() {
76
76
  // The version `binary()` runs.
77
77
  binary.version = () => installed()?.version ?? version;
78
78
 
79
- module.exports = Object.assign(binary, { packageName, home, exe, newer });
79
+ // The environment to run the binary at `file` in: the rg shipped beside it
80
+ // first on PATH (D97). Windows spells the key as it likes, and one key it stays.
81
+ // Windows also looks for a bare `rg` in the current directory, the repository
82
+ // being searched, before PATH; NoDefaultCurrentDirectoryInExePath stops that.
83
+ // Nothing else reads it.
84
+ function env(file) {
85
+ const key = Object.keys(process.env).find((k) => k.toUpperCase() === "PATH") ?? "PATH";
86
+ const rest = process.env[key];
87
+ return {
88
+ ...process.env,
89
+ [key]: rest ? path.dirname(file) + path.delimiter + rest : path.dirname(file),
90
+ NoDefaultCurrentDirectoryInExePath: "1",
91
+ };
92
+ }
93
+
94
+ module.exports = Object.assign(binary, { packageName, home, exe, newer, env });
package/lib/mcp.js CHANGED
@@ -1,19 +1,16 @@
1
1
  // `realontext mcp` through the wrapper (D95): the agent's JSON-RPC lines go to
2
- // the binary and its lines come back, so the binary can be switched for a
3
- // newer one in the middle of a session. The version is asked for when the
4
- // session starts, every hour, and when a query comes back 426; a tools/call
5
- // waits up to 15 s for an ask in progress. The switch happens between
6
- // requests, when the binary owes no answer: the new one is started, the
7
- // session's initialize is replayed to it and its answer dropped, and the old
8
- // one is closed. A call refused with 426 goes again once, after an ask.
2
+ // the binary in order and its lines come back. Before each tools/call the
3
+ // version is asked for (D94); when that gives a newer binary, it is started,
4
+ // the session's initialize is replayed to it and its answer dropped, and the
5
+ // call goes to it. The old binary's stdin is closed: it answers what it was
6
+ // sent and exits.
9
7
  "use strict";
10
8
 
11
9
  const readline = require("readline");
12
10
  const { spawn } = require("child_process");
11
+ const binary = require("./binary.js");
13
12
  const update = require("./update.js");
14
13
 
15
- const hour = 60 * 60 * 1000;
16
- const wait = 15 * 1000;
17
14
  const replay = "realontext-switch"; // the id of the replayed initialize
18
15
 
19
16
  function parse(line) {
@@ -25,120 +22,66 @@ function parse(line) {
25
22
  }
26
23
  }
27
24
 
28
- const key = (id) => JSON.stringify(id);
29
-
30
- function tooOld(m) {
31
- return m.result?.isError === true && JSON.stringify(m.result.content ?? "").includes("HTTP 426");
32
- }
33
-
34
- // Resolves to the exit status once the binary has exited.
25
+ // Resolves to the exit status, or the signal, once the binary has exited.
35
26
  module.exports = function mcp(path, args) {
36
27
  return new Promise((resolve) => {
37
28
  let child;
38
- let next = null; // a binary to switch to
39
- let asking = null; // the ask in progress
40
- let waiting = false; // a tools/call waits for `asking`
41
- let switching = false; // the replayed initialize is not answered yet
42
- let closed = false; // the agent closed stdin
43
29
  let initialize = null;
30
+ let busy = false;
31
+ let closed = false;
32
+ let status = null; // the current binary's, once it has exited
33
+ const alive = new Set(); // binaries not exited, the old ones answering what they were sent
44
34
  const held = []; // the agent's lines not sent yet
45
- const owed = new Map(); // key(id) → the request line the binary has not answered
46
- const retried = new Set();
47
-
48
- function ask() {
49
- if (asking) return;
50
- asking = update
51
- .latest()
52
- .then((p) => {
53
- if (p && p !== path) next = p;
54
- })
55
- .finally(() => {
56
- asking = null;
57
- pump();
58
- });
59
- }
60
35
 
61
36
  function start() {
62
- const c = spawn(path, ["mcp", ...args], { stdio: ["pipe", "pipe", "inherit"], windowsHide: true });
37
+ const c = spawn(path, ["mcp", ...args], {
38
+ stdio: ["pipe", "pipe", "inherit"],
39
+ windowsHide: true,
40
+ env: binary.env(path),
41
+ });
63
42
  child = c;
64
43
  c.stdin.on("error", () => {});
65
- readline.createInterface({ input: c.stdout, crlfDelay: Infinity }).on("line", answer);
44
+ readline.createInterface({ input: c.stdout, crlfDelay: Infinity }).on("line", (line) => {
45
+ if (parse(line)?.id !== replay) process.stdout.write(line + "\n");
46
+ });
66
47
  c.on("error", (e) => {
67
48
  if (c !== child) return;
68
49
  console.error(`realontext: ${e.message}`);
69
50
  resolve(1);
70
51
  });
52
+ alive.add(c);
71
53
  c.on("exit", (code, signal) => {
72
- if (c !== child) return;
73
- // Every answer is out before the status goes back.
74
- process.stdout.write("", () => resolve(signal ? signal : (code ?? 1)));
54
+ alive.delete(c);
55
+ if (c === child) status = signal ?? code ?? 1;
56
+ // The status goes back once every binary has answered and every answer is out.
57
+ if (status !== null && !alive.size) process.stdout.write("", () => resolve(status));
75
58
  });
76
59
  }
77
60
 
78
- function change() {
79
- const old = child;
80
- path = next;
81
- next = null;
82
- start();
83
- if (initialize) {
84
- child.stdin.write(JSON.stringify({ ...parse(initialize), id: replay }) + "\n");
85
- switching = true;
86
- }
87
- old.stdin.end();
88
- }
89
-
90
- function send(line, m) {
91
- if (m?.method === "initialize") initialize = line;
92
- if (m?.method && "id" in m) owed.set(key(m.id), line);
93
- child.stdin.write(line + "\n");
94
- }
95
-
96
- function pump() {
97
- while (held.length && !switching && !waiting) {
98
- const m = parse(held[0]);
99
- if (m?.method === "tools/call" && asking) {
100
- waiting = true;
101
- Promise.race([asking, new Promise((r) => setTimeout(r, wait).unref())]).then(() => {
102
- waiting = false;
103
- pump();
104
- });
105
- return;
106
- }
107
- // Nothing more goes to a binary being left: it answers what it owes first.
108
- if (next) {
109
- if (owed.size) return;
110
- change();
111
- continue;
112
- }
113
- send(held.shift(), m);
114
- }
115
- if (closed && !held.length && !switching && !waiting) child.stdin.end();
116
- }
117
-
118
- function answer(line) {
119
- const m = parse(line);
120
- if (m && m.id === replay && !m.method) {
121
- switching = false;
122
- return pump();
123
- }
124
- const k = m && "id" in m && !m.method ? key(m.id) : null;
125
- if (k && owed.has(k)) {
126
- const request = owed.get(k);
127
- owed.delete(k);
128
- if (tooOld(m) && !retried.has(k)) {
129
- retried.add(k);
130
- held.unshift(request);
131
- ask();
132
- return pump();
61
+ async function pump() {
62
+ if (busy) return;
63
+ busy = true;
64
+ while (held.length) {
65
+ const line = held.shift();
66
+ const m = parse(line);
67
+ if (m?.method === "tools/call") {
68
+ const newer = await update.latest();
69
+ if (newer && newer !== path) {
70
+ const old = child;
71
+ path = newer;
72
+ start();
73
+ if (initialize) child.stdin.write(JSON.stringify({ ...parse(initialize), id: replay }) + "\n");
74
+ old.stdin.end();
75
+ }
133
76
  }
77
+ if (m?.method === "initialize") initialize = line;
78
+ child.stdin.write(line + "\n");
134
79
  }
135
- process.stdout.write(line + "\n");
136
- pump();
80
+ busy = false;
81
+ if (closed) child.stdin.end();
137
82
  }
138
83
 
139
84
  start();
140
- ask();
141
- setInterval(ask, hour).unref();
142
85
  readline
143
86
  .createInterface({ input: process.stdin, crlfDelay: Infinity })
144
87
  .on("line", (line) => {
package/lib/update.js CHANGED
@@ -7,8 +7,8 @@
7
7
  //
8
8
  // `start()` runs this file as a detached process, at most once an hour: the
9
9
  // command it was started for does not wait, the next start runs the new
10
- // version. `latest()` does the same in this process, for `mcp` (D95), which
11
- // switches to what it returns.
10
+ // version. `latest()` does the same in this process, for `mcp` before each
11
+ // call (D95), which switches to what it returns.
12
12
  "use strict";
13
13
 
14
14
  const crypto = require("crypto");
@@ -38,10 +38,10 @@ function start() {
38
38
  } catch {}
39
39
  }
40
40
 
41
- async function get(url) {
41
+ async function get(url, ms = 60000) {
42
42
  const res = await fetch(url, {
43
43
  headers: { "User-Agent": `realontext/${version}` },
44
- signal: AbortSignal.timeout(60000),
44
+ signal: AbortSignal.timeout(ms),
45
45
  });
46
46
  if (!res.ok) throw new Error(`${url}: HTTP ${res.status}`);
47
47
  return res;
@@ -71,11 +71,18 @@ async function install(v) {
71
71
  if (crypto.createHash(algorithm).update(tgz).digest("base64") !== digest) {
72
72
  throw new Error(`${name}@${v}: the download does not match the registry's integrity`);
73
73
  }
74
- const file = untar(zlib.gunzipSync(tgz), `package/bin/${binary.exe}`);
74
+ const tar = zlib.gunzipSync(tgz);
75
+ const file = untar(tar, `package/bin/${binary.exe}`);
75
76
  // Written beside, then renamed: a start never finds half a binary.
76
77
  const tmp = `${dir}.${process.pid}`;
77
78
  fs.mkdirSync(tmp, { recursive: true });
78
79
  fs.writeFileSync(path.join(tmp, binary.exe), file, { mode: 0o755 });
80
+ // ripgrep, which the binary runs from PATH with its own directory first
81
+ // (D97); a version from before it has none.
82
+ const rg = process.platform === "win32" ? "rg.exe" : "rg";
83
+ try {
84
+ fs.writeFileSync(path.join(tmp, rg), untar(tar, `package/bin/${rg}`), { mode: 0o755 });
85
+ } catch {}
79
86
  try {
80
87
  fs.renameSync(tmp, dir);
81
88
  } catch (e) {
@@ -108,7 +115,8 @@ function choose(v) {
108
115
  }
109
116
 
110
117
  async function run() {
111
- const want = (await (await get(`${relay}/v1/client`)).json()).version;
118
+ // `mcp` asks before every call: an unreachable Relay costs it 2 s at most.
119
+ const want = (await (await get(`${relay}/v1/client`, 2000)).json()).version;
112
120
  if (!want) return;
113
121
  if (!binary.newer(want, version)) return choose(null);
114
122
  await install(want);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "realontext",
3
- "version": "0.8.1",
3
+ "version": "0.9.0",
4
4
  "description": "Code search for coding agents, over the directory you are in. CLI and MCP server.",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "repository": {
@@ -24,8 +24,8 @@
24
24
  "react": "^18.3.1"
25
25
  },
26
26
  "optionalDependencies": {
27
- "@realontext/darwin-arm64": "0.8.1",
28
- "@realontext/linux-x64": "0.8.1",
29
- "@realontext/win32-x64": "0.8.1"
27
+ "@realontext/darwin-arm64": "0.9.0",
28
+ "@realontext/linux-x64": "0.9.0",
29
+ "@realontext/win32-x64": "0.9.0"
30
30
  }
31
31
  }