realontext 0.8.0 → 0.8.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,6 +1,6 @@
1
1
  {
2
2
  "name": "realontext",
3
- "version": "0.8.0",
3
+ "version": "0.8.2",
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
@@ -64,9 +64,11 @@ The binary keeps itself at the version the Relay names. In the background, at
64
64
  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
- (`%LOCALAPPDATA%\realontext` on Windows). The next start runs it; an agent's
68
- running MCP server is not interrupted. A newer `npm install -g realontext`
69
- runs in its place. `REALONTEXT_NO_AUTO_UPDATE=1` turns this off.
67
+ (`%LOCALAPPDATA%\realontext` on Windows). The next start runs it. An agent's
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
70
+ `npm install -g realontext` runs in its place. `REALONTEXT_NO_AUTO_UPDATE=1`
71
+ turns this off.
70
72
 
71
73
  ## From the shell
72
74
 
package/bin/realontext.js CHANGED
@@ -3,16 +3,22 @@
3
3
  // this process's arguments, stdio and exit status (D39). What is written here
4
4
  // instead is what Node does better: `setup` edits agents' configs (D74), and
5
5
  // with no command, in a terminal, a menu of everything (D75). Each start may
6
- // update the binary in the background (D94).
6
+ // update the binary in the background (D94); `mcp` passes the session through
7
+ // and switches to a newer binary in its middle (D95).
7
8
  "use strict";
8
9
 
9
10
  const { spawnSync } = require("child_process");
10
11
 
11
- require("../lib/update.js").start();
12
-
13
12
  const [command, ...rest] = process.argv.slice(2);
14
13
  const terminal = process.stdin.isTTY && process.stdout.isTTY;
15
14
 
15
+ if (command !== "mcp") require("../lib/update.js").start();
16
+
17
+ function exit(status) {
18
+ if (typeof status === "string") process.kill(process.pid, status);
19
+ process.exit(status);
20
+ }
21
+
16
22
  if (terminal && (command === undefined || (command === "setup" && rest.length === 0))) {
17
23
  import("../lib/tui.mjs")
18
24
  .then((tui) => tui.default(command === "setup" ? "setup" : "home"))
@@ -27,13 +33,14 @@ if (terminal && (command === undefined || (command === "setup" && rest.length ==
27
33
  console.error(`realontext: ${e.message}`);
28
34
  process.exit(1);
29
35
  }
30
- const run = spawnSync(path, process.argv.slice(2), { stdio: "inherit" });
31
- if (run.error) {
32
- console.error(`realontext: ${run.error.message}`);
33
- process.exit(1);
34
- }
35
- if (run.signal) {
36
- process.kill(process.pid, run.signal);
36
+ if (command === "mcp") {
37
+ require("../lib/mcp.js")(path, rest).then(exit);
38
+ } else {
39
+ const run = spawnSync(path, process.argv.slice(2), { stdio: "inherit" });
40
+ if (run.error) {
41
+ console.error(`realontext: ${run.error.message}`);
42
+ process.exit(1);
43
+ }
44
+ exit(run.signal ?? run.status);
37
45
  }
38
- process.exit(run.status);
39
46
  }
package/lib/mcp.js ADDED
@@ -0,0 +1,91 @@
1
+ // `realontext mcp` through the wrapper (D95): the agent's JSON-RPC lines go to
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.
7
+ "use strict";
8
+
9
+ const readline = require("readline");
10
+ const { spawn } = require("child_process");
11
+ const update = require("./update.js");
12
+
13
+ const replay = "realontext-switch"; // the id of the replayed initialize
14
+
15
+ function parse(line) {
16
+ try {
17
+ const m = JSON.parse(line);
18
+ return m && typeof m === "object" ? m : null;
19
+ } catch {
20
+ return null;
21
+ }
22
+ }
23
+
24
+ // Resolves to the exit status, or the signal, once the binary has exited.
25
+ module.exports = function mcp(path, args) {
26
+ return new Promise((resolve) => {
27
+ let child;
28
+ let initialize = null;
29
+ let busy = false;
30
+ let closed = false;
31
+ let status = null; // the current binary's, once it has exited
32
+ const alive = new Set(); // binaries not exited, the old ones answering what they were sent
33
+ const held = []; // the agent's lines not sent yet
34
+
35
+ function start() {
36
+ const c = spawn(path, ["mcp", ...args], { stdio: ["pipe", "pipe", "inherit"], windowsHide: true });
37
+ child = c;
38
+ c.stdin.on("error", () => {});
39
+ readline.createInterface({ input: c.stdout, crlfDelay: Infinity }).on("line", (line) => {
40
+ if (parse(line)?.id !== replay) process.stdout.write(line + "\n");
41
+ });
42
+ c.on("error", (e) => {
43
+ if (c !== child) return;
44
+ console.error(`realontext: ${e.message}`);
45
+ resolve(1);
46
+ });
47
+ alive.add(c);
48
+ c.on("exit", (code, signal) => {
49
+ alive.delete(c);
50
+ if (c === child) status = signal ?? code ?? 1;
51
+ // The status goes back once every binary has answered and every answer is out.
52
+ if (status !== null && !alive.size) process.stdout.write("", () => resolve(status));
53
+ });
54
+ }
55
+
56
+ async function pump() {
57
+ if (busy) return;
58
+ busy = true;
59
+ while (held.length) {
60
+ const line = held.shift();
61
+ const m = parse(line);
62
+ if (m?.method === "tools/call") {
63
+ const newer = await update.latest();
64
+ if (newer && newer !== path) {
65
+ const old = child;
66
+ path = newer;
67
+ start();
68
+ if (initialize) child.stdin.write(JSON.stringify({ ...parse(initialize), id: replay }) + "\n");
69
+ old.stdin.end();
70
+ }
71
+ }
72
+ if (m?.method === "initialize") initialize = line;
73
+ child.stdin.write(line + "\n");
74
+ }
75
+ busy = false;
76
+ if (closed) child.stdin.end();
77
+ }
78
+
79
+ start();
80
+ readline
81
+ .createInterface({ input: process.stdin, crlfDelay: Infinity })
82
+ .on("line", (line) => {
83
+ held.push(line);
84
+ pump();
85
+ })
86
+ .on("close", () => {
87
+ closed = true;
88
+ pump();
89
+ });
90
+ });
91
+ };
package/lib/update.js CHANGED
@@ -1,11 +1,14 @@
1
- // Keeps the binary at the version the Relay names (D94), in the background.
2
- // `start()` runs this file as a detached process, at most once an hour; the
3
- // command it was started for does not wait. The Relay's GET /v1/client names
4
- // the version; when it is newer than npm's, that version's platform package
5
- // comes from the registry, checked against the registry's integrity, into
6
- // versions/<version>/, and `current` names it: the next start runs it. A
7
- // version no newer than npm's lets npm's run. Nothing is printed; the last
8
- // failure is left in update.log.
1
+ // Keeps the binary at the version the Relay names (D94). The Relay's GET
2
+ // /v1/client names the version; when it is newer than npm's, that version's
3
+ // platform package comes from the registry, checked against the registry's
4
+ // integrity, into versions/<version>/, and `current` names it. A version no
5
+ // newer than npm's lets npm's run. Nothing is printed; the last failure is
6
+ // left in update.log.
7
+ //
8
+ // `start()` runs this file as a detached process, at most once an hour: the
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` before each
11
+ // call (D95), which switches to what it returns.
9
12
  "use strict";
10
13
 
11
14
  const crypto = require("crypto");
@@ -20,8 +23,10 @@ const relay = "https://api.context.revlm.com";
20
23
  const registry = (process.env.npm_config_registry || "https://registry.npmjs.org").replace(/\/+$/, "");
21
24
  const hour = 60 * 60 * 1000;
22
25
 
26
+ const off = () => Boolean(process.env.REALONTEXT_NO_AUTO_UPDATE) || !binary.packageName();
27
+
23
28
  function start() {
24
- if (process.env.REALONTEXT_NO_AUTO_UPDATE || !binary.packageName()) return;
29
+ if (off()) return;
25
30
  const checked = path.join(binary.home(), "checked");
26
31
  try {
27
32
  if (Date.now() - fs.statSync(checked).mtimeMs < hour) return;
@@ -33,10 +38,10 @@ function start() {
33
38
  } catch {}
34
39
  }
35
40
 
36
- async function get(url) {
41
+ async function get(url, ms = 60000) {
37
42
  const res = await fetch(url, {
38
43
  headers: { "User-Agent": `realontext/${version}` },
39
- signal: AbortSignal.timeout(60000),
44
+ signal: AbortSignal.timeout(ms),
40
45
  });
41
46
  if (!res.ok) throw new Error(`${url}: HTTP ${res.status}`);
42
47
  return res;
@@ -103,20 +108,33 @@ function choose(v) {
103
108
  }
104
109
 
105
110
  async function run() {
106
- const want = (await (await get(`${relay}/v1/client`)).json()).version;
111
+ // `mcp` asks before every call: an unreachable Relay costs it 2 s at most.
112
+ const want = (await (await get(`${relay}/v1/client`, 2000)).json()).version;
107
113
  if (!want) return;
108
114
  if (!binary.newer(want, version)) return choose(null);
109
115
  await install(want);
110
116
  choose(want);
111
117
  }
112
118
 
113
- if (require.main === module) {
114
- run().catch((e) => {
115
- try {
116
- fs.mkdirSync(binary.home(), { recursive: true });
117
- fs.writeFileSync(path.join(binary.home(), "update.log"), `${new Date().toISOString()} ${e.message}\n`);
118
- } catch {}
119
- });
119
+ function log(e) {
120
+ try {
121
+ fs.mkdirSync(binary.home(), { recursive: true });
122
+ fs.writeFileSync(path.join(binary.home(), "update.log"), `${new Date().toISOString()} ${e.message}\n`);
123
+ } catch {}
124
+ }
125
+
126
+ // The binary to run once versions/ is where the Relay says; null when updates
127
+ // are off or there is no binary.
128
+ async function latest() {
129
+ if (off()) return null;
130
+ await run().catch(log);
131
+ try {
132
+ return binary();
133
+ } catch {
134
+ return null;
135
+ }
120
136
  }
121
137
 
122
- module.exports = { start };
138
+ if (require.main === module) run().catch(log);
139
+
140
+ module.exports = { start, latest };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "realontext",
3
- "version": "0.8.0",
3
+ "version": "0.8.2",
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.0",
28
- "@realontext/linux-x64": "0.8.0",
29
- "@realontext/win32-x64": "0.8.0"
27
+ "@realontext/darwin-arm64": "0.8.2",
28
+ "@realontext/linux-x64": "0.8.2",
29
+ "@realontext/win32-x64": "0.8.2"
30
30
  }
31
31
  }