realontext 0.7.0 → 0.8.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.7.0",
3
+ "version": "0.8.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
@@ -58,6 +58,16 @@ Without a Token nothing leaves the machine: the results come back in lexical
58
58
  order, with a notice saying so. `REALONTEXT_TOKEN` overrides the saved Token;
59
59
  set to an empty string, it turns jev off for that process.
60
60
 
61
+ ## Updates
62
+
63
+ The binary keeps itself at the version the Relay names. In the background, at
64
+ most once an hour, `realontext` asks the Relay for it; a newer one is
65
+ downloaded from the npm registry (`npm_config_registry` if set), checked
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.
70
+
61
71
  ## From the shell
62
72
 
63
73
  ```bash
package/bin/realontext.js CHANGED
@@ -2,11 +2,14 @@
2
2
  // The `realontext` command npm installs: runs the binary of this platform with
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
- // with no command, in a terminal, a menu of everything (D75).
5
+ // with no command, in a terminal, a menu of everything (D75). Each start may
6
+ // update the binary in the background (D94).
6
7
  "use strict";
7
8
 
8
9
  const { spawnSync } = require("child_process");
9
10
 
11
+ require("../lib/update.js").start();
12
+
10
13
  const [command, ...rest] = process.argv.slice(2);
11
14
  const terminal = process.stdin.isTTY && process.stdout.isTTY;
12
15
 
package/lib/binary.js CHANGED
@@ -1,15 +1,62 @@
1
- // The binary of the platform package npm picked through optionalDependencies
2
- // (D39). Throws when there is none for this machine.
1
+ // The binary this machine runs: the one the platform package npm picked
2
+ // through optionalDependencies (D39), or the newer one the updater put in
3
+ // versions/ (D94). Throws when there is neither.
3
4
  "use strict";
4
5
 
6
+ const fs = require("fs");
7
+ const os = require("os");
8
+ const path = require("path");
9
+ const { version } = require("../package.json");
10
+
5
11
  const packages = {
6
12
  "darwin arm64": "@realontext/darwin-arm64",
7
13
  "linux x64": "@realontext/linux-x64",
8
14
  "win32 x64": "@realontext/win32-x64",
9
15
  };
10
16
 
11
- module.exports = function binary() {
12
- const name = packages[`${process.platform} ${process.arch}`];
17
+ const exe = process.platform === "win32" ? "realontext.exe" : "realontext";
18
+
19
+ function packageName() {
20
+ return packages[`${process.platform} ${process.arch}`];
21
+ }
22
+
23
+ // Where the updater keeps versions/<version>/ and `current`, the version that runs.
24
+ function home() {
25
+ if (process.platform === "win32") {
26
+ return path.join(process.env.LOCALAPPDATA || path.join(os.homedir(), "AppData", "Local"), "realontext");
27
+ }
28
+ return path.join(os.homedir(), ".local", "share", "realontext");
29
+ }
30
+
31
+ function parse(v) {
32
+ const m = /^(\d+)\.(\d+)\.(\d+)$/.exec(v || "");
33
+ return m && m.slice(1).map(Number);
34
+ }
35
+
36
+ // Whether version `a` is later than `b`; false when either is not a version.
37
+ function newer(a, b) {
38
+ const x = parse(a);
39
+ const y = parse(b);
40
+ if (!x || !y) return false;
41
+ const i = x.findIndex((n, k) => n !== y[k]);
42
+ return i >= 0 && x[i] > y[i];
43
+ }
44
+
45
+ // The updater's version, when it is newer than npm's: {version, path}.
46
+ function installed() {
47
+ try {
48
+ const v = fs.readFileSync(path.join(home(), "current"), "utf8").trim();
49
+ const p = path.join(home(), "versions", v, exe);
50
+ return newer(v, version) && fs.existsSync(p) ? { version: v, path: p } : null;
51
+ } catch {
52
+ return null;
53
+ }
54
+ }
55
+
56
+ function binary() {
57
+ const ours = installed();
58
+ if (ours) return ours.path;
59
+ const name = packageName();
13
60
  if (!name) {
14
61
  throw new Error(
15
62
  `no build for ${process.platform}-${process.arch}; ` +
@@ -17,11 +64,16 @@ module.exports = function binary() {
17
64
  );
18
65
  }
19
66
  try {
20
- return require.resolve(`${name}/bin/realontext${process.platform === "win32" ? ".exe" : ""}`);
67
+ return require.resolve(`${name}/bin/${exe}`);
21
68
  } catch {
22
69
  throw new Error(
23
70
  `${name} is not installed. It is an optional dependency of realontext; ` +
24
71
  `reinstall without --no-optional / --omit=optional.`
25
72
  );
26
73
  }
27
- };
74
+ }
75
+
76
+ // The version `binary()` runs.
77
+ binary.version = () => installed()?.version ?? version;
78
+
79
+ module.exports = Object.assign(binary, { packageName, home, exe, newer });
package/lib/tui.mjs CHANGED
@@ -13,7 +13,6 @@ import { Box, Text, render, useApp, useInput } from "ink";
13
13
  const require = createRequire(import.meta.url);
14
14
  const { agents, setup } = require("./setup.js");
15
15
  const binary = require("./binary.js");
16
- const { version } = require("../package.json");
17
16
  const h = React.createElement;
18
17
 
19
18
  // `realontext usage`: {account, queries}, or {error} with what went wrong.
@@ -144,7 +143,7 @@ function App({ start, onDone }) {
144
143
  h(
145
144
  Box,
146
145
  { flexDirection: "column" },
147
- h(Text, { bold: true }, `realontext ${version} `, h(Text, { dimColor: true }, process.cwd())),
146
+ h(Text, { bold: true }, `realontext ${binary.version()} `, h(Text, { dimColor: true }, process.cwd())),
148
147
  h(Account, { account })
149
148
  ),
150
149
  h(SCREENS[screen], { account, go, done })
package/lib/update.js ADDED
@@ -0,0 +1,122 @@
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.
9
+ "use strict";
10
+
11
+ const crypto = require("crypto");
12
+ const fs = require("fs");
13
+ const path = require("path");
14
+ const zlib = require("zlib");
15
+ const { spawn } = require("child_process");
16
+ const binary = require("./binary.js");
17
+ const { version } = require("../package.json");
18
+
19
+ const relay = "https://api.context.revlm.com";
20
+ const registry = (process.env.npm_config_registry || "https://registry.npmjs.org").replace(/\/+$/, "");
21
+ const hour = 60 * 60 * 1000;
22
+
23
+ function start() {
24
+ if (process.env.REALONTEXT_NO_AUTO_UPDATE || !binary.packageName()) return;
25
+ const checked = path.join(binary.home(), "checked");
26
+ try {
27
+ if (Date.now() - fs.statSync(checked).mtimeMs < hour) return;
28
+ } catch {}
29
+ try {
30
+ fs.mkdirSync(binary.home(), { recursive: true });
31
+ fs.writeFileSync(checked, "");
32
+ spawn(process.execPath, [__filename], { detached: true, stdio: "ignore", windowsHide: true }).unref();
33
+ } catch {}
34
+ }
35
+
36
+ async function get(url) {
37
+ const res = await fetch(url, {
38
+ headers: { "User-Agent": `realontext/${version}` },
39
+ signal: AbortSignal.timeout(60000),
40
+ });
41
+ if (!res.ok) throw new Error(`${url}: HTTP ${res.status}`);
42
+ return res;
43
+ }
44
+
45
+ // The file `want` in a tar archive.
46
+ function untar(tar, want) {
47
+ for (let at = 0; at + 512 <= tar.length && tar[at] !== 0; ) {
48
+ const field = (from, to) => tar.toString("utf8", at + from, at + to).replace(/\0[\s\S]*$/, "");
49
+ const prefix = field(345, 500);
50
+ const name = (prefix ? prefix + "/" : "") + field(0, 100);
51
+ const size = parseInt(field(124, 136).trim(), 8);
52
+ if (name === want) return tar.subarray(at + 512, at + 512 + size);
53
+ at += 512 + Math.ceil(size / 512) * 512;
54
+ }
55
+ throw new Error(`${want} is not in the package`);
56
+ }
57
+
58
+ // Puts `v`'s binary in versions/<v>/.
59
+ async function install(v) {
60
+ const dir = path.join(binary.home(), "versions", v);
61
+ if (fs.existsSync(path.join(dir, binary.exe))) return;
62
+ const name = binary.packageName();
63
+ const meta = await (await get(`${registry}/${name.replace("/", "%2f")}/${v}`)).json();
64
+ const tgz = Buffer.from(await (await get(meta.dist.tarball)).arrayBuffer());
65
+ const [algorithm, digest] = meta.dist.integrity.split(/-(.*)/s);
66
+ if (crypto.createHash(algorithm).update(tgz).digest("base64") !== digest) {
67
+ throw new Error(`${name}@${v}: the download does not match the registry's integrity`);
68
+ }
69
+ const file = untar(zlib.gunzipSync(tgz), `package/bin/${binary.exe}`);
70
+ // Written beside, then renamed: a start never finds half a binary.
71
+ const tmp = `${dir}.${process.pid}`;
72
+ fs.mkdirSync(tmp, { recursive: true });
73
+ fs.writeFileSync(path.join(tmp, binary.exe), file, { mode: 0o755 });
74
+ try {
75
+ fs.renameSync(tmp, dir);
76
+ } catch (e) {
77
+ fs.rmSync(tmp, { recursive: true, force: true });
78
+ if (!fs.existsSync(path.join(dir, binary.exe))) throw e;
79
+ }
80
+ }
81
+
82
+ // Points `current` at `v`, or at nothing; versions/ keeps it and the one before.
83
+ function choose(v) {
84
+ const current = path.join(binary.home(), "current");
85
+ let was = null;
86
+ try {
87
+ was = fs.readFileSync(current, "utf8").trim();
88
+ } catch {}
89
+ if (v) {
90
+ fs.writeFileSync(`${current}.${process.pid}`, v);
91
+ fs.renameSync(`${current}.${process.pid}`, current);
92
+ } else {
93
+ fs.rmSync(current, { force: true });
94
+ }
95
+ const versions = path.join(binary.home(), "versions");
96
+ for (const d of fs.existsSync(versions) ? fs.readdirSync(versions) : []) {
97
+ if (d === v || d === was) continue;
98
+ // A version still running on Windows cannot go: it goes next time.
99
+ try {
100
+ fs.rmSync(path.join(versions, d), { recursive: true, force: true });
101
+ } catch {}
102
+ }
103
+ }
104
+
105
+ async function run() {
106
+ const want = (await (await get(`${relay}/v1/client`)).json()).version;
107
+ if (!want) return;
108
+ if (!binary.newer(want, version)) return choose(null);
109
+ await install(want);
110
+ choose(want);
111
+ }
112
+
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
+ });
120
+ }
121
+
122
+ module.exports = { start };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "realontext",
3
- "version": "0.7.0",
3
+ "version": "0.8.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.7.0",
28
- "@realontext/linux-x64": "0.7.0",
29
- "@realontext/win32-x64": "0.7.0"
27
+ "@realontext/darwin-arm64": "0.8.0",
28
+ "@realontext/linux-x64": "0.8.0",
29
+ "@realontext/win32-x64": "0.8.0"
30
30
  }
31
31
  }