@agst/cli 0.1.0 → 0.2.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.
Files changed (3) hide show
  1. package/README.md +1 -0
  2. package/bin/agst.js +112 -3
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -12,6 +12,7 @@ npx @agst/cli serve # on the machine; prints a pairing link
12
12
  npx @agst/cli serve --install # the same, as a service that survives reboots (Linux)
13
13
  npx @agst/cli pair # a fresh pairing link
14
14
  npx @agst/cli link # optional: reach it through August Cloud too
15
+ npx @agst/cli update # move to the newest release, restart the service
15
16
  ```
16
17
 
17
18
  Open the pairing link on your Mac (or type its address and code under
package/bin/agst.js CHANGED
@@ -8,7 +8,9 @@
8
8
  *
9
9
  * Binaries live at <releases>/runner/<version>/august-runner-<platform>,
10
10
  * cached in ~/.august/bin. ~/.august/bin/agst always points at the current
11
- * one, so a systemd unit (`agst serve --install`) survives upgrades.
11
+ * one, so a systemd unit (`agst serve --install`) survives upgrades, and
12
+ * `agst update` (handled here, not by the runner) moves a box to the newest
13
+ * release and restarts that unit.
12
14
  *
13
15
  * Environment:
14
16
  * AGST_HOME cache dir (default ~/.august/bin)
@@ -23,7 +25,7 @@ import os from "node:os";
23
25
  import path from "node:path";
24
26
 
25
27
  /** The runner this launcher ships with. Bumped with every agst release. */
26
- const RUNNER_VERSION = "0.2.0";
28
+ const RUNNER_VERSION = "0.3.0";
27
29
  const RELEASES =
28
30
  process.env.AGST_RELEASES_URL ?? "https://releases.agst.me/runner";
29
31
  /** The runner signing key (SPKI, base64): the same one sandboxes verify. */
@@ -35,9 +37,17 @@ const PLATFORMS = new Set(["linux-x64", "linux-arm64", "darwin-arm64"]);
35
37
  * pair`…"). From our own package name, so the scoped and bare packages
36
38
  * both get it right.
37
39
  */
38
- const { name: PACKAGE } = JSON.parse(
40
+ const { name: PACKAGE, version: VERSION } = JSON.parse(
39
41
  fs.readFileSync(new URL("../package.json", import.meta.url), "utf8"),
40
42
  );
43
+ /** The systemd user unit `agst serve --install` writes (Linux). */
44
+ const UNIT_FILE = path.join(
45
+ os.homedir(),
46
+ ".config",
47
+ "systemd",
48
+ "user",
49
+ "agst.service",
50
+ );
41
51
 
42
52
  function fail(message) {
43
53
  console.error(`agst: ${message}`);
@@ -107,11 +117,110 @@ function linkCurrent(binDir, target) {
107
117
  return link;
108
118
  }
109
119
 
120
+ /** The runner version a cached binary's file name carries, if any. */
121
+ function versionOf(file) {
122
+ return path
123
+ .basename(file)
124
+ .match(/^august-runner-(.+)-[a-z]+-[a-z0-9]+$/)?.[1];
125
+ }
126
+
127
+ /** "0.3.0" > "0.2.10"? Plain numeric compare, enough for our tags. */
128
+ function newer(a, b) {
129
+ const [x, y] = [a, b].map((v) => v.split(".").map(Number));
130
+ for (let i = 0; i < 3; i++) {
131
+ if ((x[i] ?? 0) !== (y[i] ?? 0)) return (x[i] ?? 0) > (y[i] ?? 0);
132
+ }
133
+ return false;
134
+ }
135
+
136
+ /**
137
+ * `agst update`: bring this box to the newest release and restart the
138
+ * service onto it. Runs in the newest launcher: an older one (a stale npx
139
+ * cache) hands off to `npx <package>@<latest> update` first, since the
140
+ * launcher is what pins the runner. Then the usual fetch + verify + link,
141
+ * a restart of the systemd unit if one is installed, and a prune of runner
142
+ * binaries older than the previous one. Threads, settings and paired
143
+ * devices live outside ~/.august/bin and are never touched.
144
+ */
145
+ async function update(binDir, platform) {
146
+ if (!process.env.AGST_UPDATE_HANDOFF) {
147
+ let latest;
148
+ try {
149
+ const response = await fetch(
150
+ `https://registry.npmjs.org/${PACKAGE.replace("/", "%2f")}/latest`,
151
+ );
152
+ latest = (await response.json()).version;
153
+ } catch {
154
+ // Offline registry: update to what this launcher pins.
155
+ }
156
+ if (latest && newer(latest, VERSION)) {
157
+ const child = spawn("npx", ["-y", `${PACKAGE}@${latest}`, "update"], {
158
+ stdio: "inherit",
159
+ env: { ...process.env, AGST_UPDATE_HANDOFF: "1" },
160
+ });
161
+ child.on("exit", (code) => process.exit(code ?? 1));
162
+ child.on("error", (cause) =>
163
+ fail(`couldn't run npx (${cause.message}).`),
164
+ );
165
+ return;
166
+ }
167
+ }
168
+
169
+ const link = path.join(binDir, "agst");
170
+ let previous;
171
+ try {
172
+ previous = versionOf(fs.readlinkSync(link));
173
+ } catch {
174
+ // First install on this box.
175
+ }
176
+ const runner = await ensureBinary(binDir, platform);
177
+ linkCurrent(binDir, runner);
178
+
179
+ if (previous === RUNNER_VERSION) {
180
+ console.log(`agst: already on the newest runner (${RUNNER_VERSION}).`);
181
+ return;
182
+ }
183
+ console.log(
184
+ `agst: runner ${previous ? `${previous} → ` : ""}${RUNNER_VERSION}.`,
185
+ );
186
+
187
+ // Keep the current binary and the one before it (a quick way back).
188
+ for (const name of fs.readdirSync(binDir)) {
189
+ const version = versionOf(name);
190
+ if (!version || version === RUNNER_VERSION || version === previous)
191
+ continue;
192
+ fs.rmSync(path.join(binDir, name), { force: true });
193
+ }
194
+
195
+ if (process.platform === "linux" && fs.existsSync(UNIT_FILE)) {
196
+ try {
197
+ execFileSync("systemctl", ["--user", "restart", "agst.service"], {
198
+ stdio: "inherit",
199
+ });
200
+ console.log("agst: restarted the agst service on the new runner.");
201
+ } catch {
202
+ fail(
203
+ "updated, but couldn't restart the service. Run: systemctl --user restart agst",
204
+ );
205
+ }
206
+ } else {
207
+ console.log("agst: restart `agst serve` wherever it runs to pick it up.");
208
+ }
209
+ }
210
+
110
211
  async function main() {
111
212
  const platform = `${process.platform}-${process.arch}`;
112
213
  const binDir =
113
214
  process.env.AGST_HOME ?? path.join(os.homedir(), ".august", "bin");
114
215
 
216
+ if (process.argv[2] === "update" && !process.env.AGST_RUNNER) {
217
+ if (!PLATFORMS.has(platform)) {
218
+ fail(`no runner build for ${platform} yet.`);
219
+ }
220
+ await update(binDir, platform);
221
+ return;
222
+ }
223
+
115
224
  let runner = process.env.AGST_RUNNER;
116
225
  let stable = runner;
117
226
  if (!runner) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agst/cli",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "August agent orchestrator. Run Claude Code and OpenCode agents on any machine and drive them from the August app on your Mac or phone.",
5
5
  "keywords": [
6
6
  "august",