realontext 0.8.1 → 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.1",
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
@@ -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/lib/mcp.js CHANGED
@@ -1,19 +1,15 @@
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");
13
11
  const update = require("./update.js");
14
12
 
15
- const hour = 60 * 60 * 1000;
16
- const wait = 15 * 1000;
17
13
  const replay = "realontext-switch"; // the id of the replayed initialize
18
14
 
19
15
  function parse(line) {
@@ -25,120 +21,62 @@ function parse(line) {
25
21
  }
26
22
  }
27
23
 
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.
24
+ // Resolves to the exit status, or the signal, once the binary has exited.
35
25
  module.exports = function mcp(path, args) {
36
26
  return new Promise((resolve) => {
37
27
  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
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
44
33
  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
34
 
61
35
  function start() {
62
36
  const c = spawn(path, ["mcp", ...args], { stdio: ["pipe", "pipe", "inherit"], windowsHide: true });
63
37
  child = c;
64
38
  c.stdin.on("error", () => {});
65
- readline.createInterface({ input: c.stdout, crlfDelay: Infinity }).on("line", answer);
39
+ readline.createInterface({ input: c.stdout, crlfDelay: Infinity }).on("line", (line) => {
40
+ if (parse(line)?.id !== replay) process.stdout.write(line + "\n");
41
+ });
66
42
  c.on("error", (e) => {
67
43
  if (c !== child) return;
68
44
  console.error(`realontext: ${e.message}`);
69
45
  resolve(1);
70
46
  });
47
+ alive.add(c);
71
48
  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)));
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));
75
53
  });
76
54
  }
77
55
 
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();
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
+ }
133
71
  }
72
+ if (m?.method === "initialize") initialize = line;
73
+ child.stdin.write(line + "\n");
134
74
  }
135
- process.stdout.write(line + "\n");
136
- pump();
75
+ busy = false;
76
+ if (closed) child.stdin.end();
137
77
  }
138
78
 
139
79
  start();
140
- ask();
141
- setInterval(ask, hour).unref();
142
80
  readline
143
81
  .createInterface({ input: process.stdin, crlfDelay: Infinity })
144
82
  .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;
@@ -108,7 +108,8 @@ function choose(v) {
108
108
  }
109
109
 
110
110
  async function run() {
111
- 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;
112
113
  if (!want) return;
113
114
  if (!binary.newer(want, version)) return choose(null);
114
115
  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.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.1",
28
- "@realontext/linux-x64": "0.8.1",
29
- "@realontext/win32-x64": "0.8.1"
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
  }