realontext 0.8.0 → 0.8.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.
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "realontext",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
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,12 @@ 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 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
71
+ `npm install -g realontext` runs in its place. `REALONTEXT_NO_AUTO_UPDATE=1`
72
+ turns this off.
70
73
 
71
74
  ## From the shell
72
75
 
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,153 @@
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.
9
+ "use strict";
10
+
11
+ const readline = require("readline");
12
+ const { spawn } = require("child_process");
13
+ const update = require("./update.js");
14
+
15
+ const hour = 60 * 60 * 1000;
16
+ const wait = 15 * 1000;
17
+ const replay = "realontext-switch"; // the id of the replayed initialize
18
+
19
+ function parse(line) {
20
+ try {
21
+ const m = JSON.parse(line);
22
+ return m && typeof m === "object" ? m : null;
23
+ } catch {
24
+ return null;
25
+ }
26
+ }
27
+
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.
35
+ module.exports = function mcp(path, args) {
36
+ return new Promise((resolve) => {
37
+ 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
+ let initialize = null;
44
+ 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
+
61
+ function start() {
62
+ const c = spawn(path, ["mcp", ...args], { stdio: ["pipe", "pipe", "inherit"], windowsHide: true });
63
+ child = c;
64
+ c.stdin.on("error", () => {});
65
+ readline.createInterface({ input: c.stdout, crlfDelay: Infinity }).on("line", answer);
66
+ c.on("error", (e) => {
67
+ if (c !== child) return;
68
+ console.error(`realontext: ${e.message}`);
69
+ resolve(1);
70
+ });
71
+ 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)));
75
+ });
76
+ }
77
+
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();
133
+ }
134
+ }
135
+ process.stdout.write(line + "\n");
136
+ pump();
137
+ }
138
+
139
+ start();
140
+ ask();
141
+ setInterval(ask, hour).unref();
142
+ readline
143
+ .createInterface({ input: process.stdin, crlfDelay: Infinity })
144
+ .on("line", (line) => {
145
+ held.push(line);
146
+ pump();
147
+ })
148
+ .on("close", () => {
149
+ closed = true;
150
+ pump();
151
+ });
152
+ });
153
+ };
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` (D95), which
11
+ // 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;
@@ -110,13 +115,25 @@ async function run() {
110
115
  choose(want);
111
116
  }
112
117
 
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
- });
118
+ function log(e) {
119
+ try {
120
+ fs.mkdirSync(binary.home(), { recursive: true });
121
+ fs.writeFileSync(path.join(binary.home(), "update.log"), `${new Date().toISOString()} ${e.message}\n`);
122
+ } catch {}
123
+ }
124
+
125
+ // The binary to run once versions/ is where the Relay says; null when updates
126
+ // are off or there is no binary.
127
+ async function latest() {
128
+ if (off()) return null;
129
+ await run().catch(log);
130
+ try {
131
+ return binary();
132
+ } catch {
133
+ return null;
134
+ }
120
135
  }
121
136
 
122
- module.exports = { start };
137
+ if (require.main === module) run().catch(log);
138
+
139
+ 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.1",
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.1",
28
+ "@realontext/linux-x64": "0.8.1",
29
+ "@realontext/win32-x64": "0.8.1"
30
30
  }
31
31
  }