directionallylocal 0.6.0 → 0.7.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.
package/README.md CHANGED
@@ -1,59 +1,125 @@
1
1
  # Directionally Local installer
2
2
 
3
- Rent a server, paste one line, and send coding tasks to it from your laptop. Each task runs in its own [Hotcell](https://github.com/directionally-local/hotcell) sandbox, driven by [Autopilot](https://github.com/directionally-local/autopilot), and comes back as a pull request, even with your laptop closed.
3
+ Coding agents that run on your own computer or on your own server. Send a task; it runs in its own [Hotcell](https://github.com/directionally-local/hotcell) sandbox, driven by [Autopilot](https://github.com/directionally-local/autopilot), and comes back as a pull request, even with your laptop closed.
4
+
5
+ ## Start on your laptop
4
6
 
5
7
  ```sh
6
- curl -fsSL https://directionallylocal.run | sh
8
+ npm install -g directionallylocal
9
+ dl
7
10
  ```
8
11
 
9
- It asks three things:
12
+ `dl` asks where your agents should run:
13
+
14
+ - **On this computer** — start now, nothing to rent, pauses when this computer sleeps.
15
+ - **On my own server** — keeps going when your laptop is closed; needs SSH access.
16
+
17
+ For a server, `dl` lists the hosts in `~/.ssh/config` (or takes `user@host[:port]`), checks it over SSH, installs itself there, and pairs this laptop. Nothing is exposed on the server: `dl` reaches it through an SSH tunnel.
18
+
19
+ ### The keys it asks for
20
+
21
+ Two keys, kept on the server only. `dl` looks for them on your laptop first and offers each one it finds:
10
22
 
11
- 1. **Tailscale auth key.** The server joins your tailnet and serves Autopilot on its private `https://<name>.<tailnet>.ts.net` address, with no public ports. Turn on MagicDNS and HTTPS certificates for the tailnet first. Own-domain and instant-address options are coming.
12
- 2. **OpenRouter API key.** This is stored in Hotcell's keystore on the server. Sandboxes get spend-capped gateway tokens instead, and your laptop never sees the key.
13
- 3. **GitHub token,** so runs can clone, push, and open PRs. A fine-grained token with Contents and Pull requests read/write on the repositories you'll use is enough. It stays on the server; sandboxes push through Autopilot's repository-scoped proxy.
23
+ - **GitHub** — your GitHub CLI login (`gh auth token`), offered as *"Use your GitHub CLI login (you)? It stays on your server."* Otherwise it asks for a fine-grained token with Contents and Pull requests read/write.
24
+ - **OpenRouter** — the key in `OPENROUTER_API_KEY`, or the one Autopilot already has, offered the same way. Otherwise it asks for one.
14
25
 
15
- It ends with:
26
+ The OpenRouter key goes into Hotcell's keystore on the server; sandboxes get spend-capped gateway tokens instead, and your laptop never sees it. The GitHub token stays on the server too; sandboxes push through Autopilot's repository-scoped proxy.
27
+
28
+ ### The model
29
+
30
+ Setup asks once which model tasks and sessions use. The default is `deepseek/deepseek-v4.1-flash-1017`; if Autopilot already has a default model, that one is preselected instead. Change it any time with `dl model`, or per run with `-m`.
31
+
32
+ ### It ends with
16
33
 
17
34
  ```
18
- ✓ Ready. Fits ~6 agents at once (estimate).
19
- Open once to approve laptops in your browser: https://box.tail1234.ts.net/api/v1/auth/claim?token=…
20
- On your laptop (signed in to the same tailnet): autopilot remote add https://box.tail1234.ts.net
35
+ ✓ Ready · fits ~6 agents at once (estimate)
36
+ 1 Start a session: cd into a GitHub repository, then type
37
+ dl
38
+ 2 Or hand off a one-off task
39
+ dl run "fix the flaky checkout test"
40
+
41
+ dl status shows where agents run and what they're doing.
21
42
  ```
22
43
 
23
- Then, on your laptop:
44
+ Then, from inside a GitHub repository:
24
45
 
25
46
  ```sh
26
- npm install -g directionallylocal
27
- dl connect https://box.tail1234.ts.net # approve the code in the browser you opened the link in
28
- dl run "fix the flaky checkout test" # from inside a GitHub repository; local changes go along
29
- dl runs # tasks and their pull requests
30
- dl pull <id> # check one out here
47
+ dl # talk to the repository's agent; it keeps running when you leave
48
+ dl run "fix the flaky checkout test" # one-off task; your uncommitted changes go along
49
+ dl runs # tasks and their pull requests
50
+ dl pull <id> # check one out here
31
51
  ```
32
52
 
33
- Typing `dl` on its own explains what it is and what's going on, on the server and on the laptop.
53
+ `dl status` says when a server is behind npm, once a day: `! box is behind: Autopilot 2.7.0 → 2.7.2 dl update box`.
34
54
 
35
- The "fits ~N agents" figure is a conservative estimate from memory and CPUs (1 GB per sandbox). Measured numbers will replace it.
55
+ The "fits ~N agents" figure is a conservative estimate: memory left after the host's own share (at least 1.5 GB or 15%), divided by 1 GB per sandbox, capped at two per CPU. Measured numbers will replace it.
36
56
 
37
57
  ## Commands
38
58
 
39
- Run these as root on the server. `dl` is short for `directionallylocal`; both work.
59
+ `dl` is short for `directionallylocal`; both work.
60
+
61
+ ### On your laptop
62
+
63
+ | Command | What it does |
64
+ | --- | --- |
65
+ | `dl` | Set up the first time; afterwards, an overview. Inside a GitHub repository, that repository's agent session. |
66
+ | `dl setup` | Set up or repair where your agents run. Safe to re-run. `--identity FILE` names the SSH key to use. |
67
+ | `dl new` | In a GitHub repository: start a fresh conversation for it. |
68
+ | `dl status` | Where your agents run and what they're doing. Works anywhere. |
69
+ | `dl connect URL` | Connect to a server someone else set up; approve the code it shows. |
70
+ | `dl run "TASK"` | Run a task, from inside a GitHub repository. Ctrl-C stops watching, not the task. |
71
+ | `dl runs` | Your tasks and their pull requests. `--json` for scripts. |
72
+ | `dl watch ID` / `dl cancel ID` | Follow a task, or stop one. |
73
+ | `dl pull ID` | Check out a task's branch here. |
74
+ | `dl servers` | Where agents run. `dl servers rename OLD NEW`, `dl servers remove NAME`. |
75
+ | `dl use [NAME]` | Where tasks go without `--server`. With no name, shows it. |
76
+ | `dl keys [openrouter\|github]` | Which keys are set, or replace one. `--server NAME` picks the server. |
77
+ | `dl model [ID]` | The model for tasks and sessions. With no ID, picks from a list. |
78
+ | `dl update [NAME]` | Update a server's Autopilot, Hotcell and sandbox image over SSH. `--wait` waits for running tasks instead of asking; with no terminal it waits anyway. |
79
+ | `dl local status` | Agents running on this computer: running?, where, how many fit. |
80
+ | `dl local uninstall [--purge]` | Stop and remove them. `--purge` also deletes keys and data. |
81
+
82
+ Run options: `--server NAME`, `-m/--model MODEL`, `--repo OWNER/NAME`, `--detach`.
83
+
84
+ ### On the server
85
+
86
+ Run these as root. `dl` on its own shows what this is, whether it's healthy, the connected laptops, and the runs.
40
87
 
41
88
  | Command | What it does |
42
89
  | --- | --- |
43
90
  | `dl up` | Install or repair everything. Safe to re-run: it keeps your keys and generated secrets and skips work that's already done. |
44
91
  | `dl up --dry-run` | Print what `up` would change, without changing anything. |
92
+ | `dl connect-laptop` | How to connect a laptop, and a link to approve it in a browser. `dl connect-laptop CODE` approves the code a laptop shows. |
93
+ | `dl laptops` | Connected laptops. `dl laptops remove NAME` disconnects one. |
94
+ | `dl runs` | Tasks running or done, with their pull requests. |
95
+ | `dl logs [hotcell\|autopilot] [-f]` | The services' logs. |
45
96
  | `dl doctor` | Check every part, with a fix for each failure. |
97
+ | `dl keys [set openrouter\|github]` | Which keys are set, or replace one (hidden input). |
46
98
  | `dl status` | Show the installed versions and where to reach the server. |
47
99
  | `dl uninstall` | Remove the services and software but keep configuration and data. Add `--purge` to delete those too, plus the service accounts and the sandbox image. |
48
100
 
49
- For unattended installs, set the answers as environment variables: `DL_TAILSCALE_AUTH_KEY`, `DL_OPENROUTER_API_KEY` and `DL_GITHUB_TOKEN`. `DL_ACCESS=loopback` skips Tailscale; you then reach the server over an SSH tunnel.
101
+ ### Unattended installs
102
+
103
+ Set the answers as environment variables: `DL_TAILSCALE_AUTH_KEY`, `DL_OPENROUTER_API_KEY` and `DL_GITHUB_TOKEN`. `DL_ACCESS` picks how the server is reached: `tailscale` (the default), `loopback` (this machine only), or `ssh` (what the laptop's setup uses). `DL_SKIP_CREDENTIAL_CHECK=1` skips the online key checks for offline installs.
50
104
 
51
- ## What it sets up
105
+ The laptop's setup drives the server over SSH with `dl up --access ssh --answers-stdin --events json`, which takes the keys as one JSON object on stdin and reports progress as JSON lines. `dl up --needs` reports which keys it would still ask for, without changing anything.
106
+
107
+ ## The one-line server install
108
+
109
+ The curl path is still there for a server you set up by hand, as root on Ubuntu or Debian:
110
+
111
+ ```sh
112
+ curl -fsSL https://directionallylocal.run | sh
113
+ ```
114
+
115
+ It installs Node.js if needed, installs `directionallylocal`, and runs `dl up`. The laptop's setup uses the same script to bootstrap a server over SSH.
116
+
117
+ ## What it sets up on a server
52
118
 
53
119
  - **Node.js 24 LTS,** if Node is missing or lacks the fix for CVE-2026-111497 (22.23.4+, 24.22.1+ and 26.12.1+ are accepted). Node security releases from NodeSource are added to Ubuntu's automatic updates, which otherwise skip them. Delete `/etc/apt/apt.conf.d/52directionallylocal-nodejs` to opt out.
54
120
  - **Docker,** if it's missing, plus a rule that keeps sandboxes away from the cloud metadata endpoint. Docker isn't updated automatically, because its repo ships major upgrades and restarting the Docker daemon can stop running sandboxes.
55
121
  - **Hotcell and Autopilot from npm,** under `/opt/directionallylocal/releases/<versions>`, with `current` pointing at the active release.
56
- - **Two systemd services, each under its own unprivileged account:** `hotcell.service` (sandboxes and the credential gateway, on loopback) and `autopilot.service` (the API that laptops pair with, on `127.0.0.1:4096`). Only Tailscale Serve exposes it.
122
+ - **Three systemd units:** `hotcell.service` (sandboxes and the credential gateway, on loopback) and `autopilot.service` (the API that laptops pair with, on `127.0.0.1:4096`), each under its own unprivileged account, plus `directionallylocal-metadata-block.service`, the firewall rule for sandboxes. Only Tailscale Serve exposes the API, and only when you use Tailscale access.
57
123
  - **A sandbox image** with Node, git, Python, and the Autopilot runtime baked in.
58
124
  - **Configuration** in `/etc/hotcell`, `/etc/autopilot` and `/etc/directionallylocal`, readable only by root and the matching service account. Data lives in `/var/lib/hotcell` and `/var/lib/autopilot`.
59
125
 
@@ -63,6 +129,8 @@ Supported: Ubuntu 24.04 and 22.04 and Debian 12, on x86_64 or arm64, with at lea
63
129
 
64
130
  `site/` is the Cloudflare Worker for `https://directionallylocal.run`. It serves `install.sh` to `curl` at the bare domain (and `/install.sh`), sends browsers to directionallylocal.com, and serves nothing else. Subdomains of `directionallylocal.run` are users' instant addresses, and the domain is on the Public Suffix List, so the Worker never sets cookies. Deploy it with `npx wrangler deploy -c site/wrangler.toml`.
65
131
 
132
+ `site/wrangler.jsonc` is the portable config that celld and Serverless Objects read; `site/wrangler.toml` keeps the Cloudflare-only `routes` and `workers_dev`. The install script is checked in as `site/install-script.ts` so the Worker has no non-JavaScript imports; regenerate it with `npm run sync:site-script` after editing `install.sh`. `sobjects build --strict site` checks that the Worker stays portable. The design for the name service and approvals page is in [`docs/plans/portable-services.md`](docs/plans/portable-services.md).
133
+
66
134
  ## Development
67
135
 
68
136
  ```sh
@@ -70,6 +138,7 @@ npm install
70
138
  npm test # node:test through tsx; a simulated server exercises up, doctor, and uninstall
71
139
  npm run typecheck
72
140
  npm run build
141
+ npm run sync:site-script # after editing install.sh
73
142
  ```
74
143
 
75
144
  ## License
package/dist/cli.js CHANGED
@@ -10,6 +10,8 @@ import { keys } from "./laptop/keys.js";
10
10
  import { model } from "./laptop/settings.js";
11
11
  import { projectSession } from "./laptop/session.js";
12
12
  import { setup } from "./laptop/setup.js";
13
+ import { realSshExec } from "./laptop/ssh.js";
14
+ import { update } from "./laptop/update.js";
13
15
  import { localCommand } from "./local/cli.js";
14
16
  import { setUpOnThisComputer } from "./local/setup.js";
15
17
  import { TtyPrompter } from "./prompt.js";
@@ -38,6 +40,8 @@ On your laptop:
38
40
  dl use [NAME] where tasks go without --server (no NAME: show it)
39
41
  dl keys [openrouter|github] which keys are set, or replace one (--server NAME)
40
42
  dl model [ID] the model for tasks and sessions (no ID: pick from a list)
43
+ dl update [NAME] update a server's Autopilot, Hotcell and sandbox image
44
+ (--wait: wait for running tasks instead of asking)
41
45
  run options: --server NAME, -m/--model MODEL, --repo OWNER/NAME, --detach
42
46
  dl local status agents running on this computer: running?, where, how many fit
43
47
  dl local uninstall [--purge] stop and remove them (--purge also deletes keys and data)
@@ -106,6 +110,7 @@ export async function main(argv, io = {
106
110
  needs: { type: "boolean" },
107
111
  identity: { type: "string" },
108
112
  stdin: { type: "boolean" },
113
+ wait: { type: "boolean" },
109
114
  },
110
115
  });
111
116
  }
@@ -220,7 +225,7 @@ export async function main(argv, io = {
220
225
  }
221
226
  // A laptop connected to servers gets its own home (`dl status` shows it from anywhere);
222
227
  // otherwise, what this is and how to start.
223
- if ((!command || command === "status") && await laptopHome(laptopIO))
228
+ if ((!command || command === "status") && await laptopHome(laptopIO, undefined, undefined, { ssh: realSshExec }))
224
229
  return 0;
225
230
  // A laptop with nothing set up yet: the wizard (it needs someone to answer it).
226
231
  if (!command && terminal && !isRoot() && (await loadRemotes()).length === 0)
@@ -242,6 +247,15 @@ export async function main(argv, io = {
242
247
  }
243
248
  case "setup":
244
249
  return await setup({ io: laptopIO, terminal, localSetup: (input) => setUpOnThisComputer(input), ...(values.identity ? { identityFile: values.identity } : {}) });
250
+ case "update":
251
+ return await update(parsed.positionals.slice(1), {
252
+ ssh: realSshExec,
253
+ prompter: new TtyPrompter(),
254
+ newUi: () => terminal ? new TerminalUi(io.out, undefined, colorOn) : new PlainUi(io.out),
255
+ io: laptopIO,
256
+ interactive: terminal,
257
+ wait: Boolean(values.wait),
258
+ });
245
259
  case "connect": {
246
260
  const url = parsed.positionals[1];
247
261
  if (!url) {
@@ -77,6 +77,25 @@ export class RemoteClient {
77
77
  cancel(conversationId) {
78
78
  return this.request("POST", `/conversations/${encodeURIComponent(conversationId)}/cancel`);
79
79
  }
80
+ /**
81
+ * The newest event of a finished conversation: one request, then the stream is dropped.
82
+ * `dl runs` uses it to say why a task failed without replaying the whole run.
83
+ */
84
+ async lastEvent(conversationId, lastEventId) {
85
+ const controller = new AbortController();
86
+ try {
87
+ for await (const event of this.events(conversationId, Math.max(0, lastEventId - 1), controller.signal)) {
88
+ return event;
89
+ }
90
+ }
91
+ catch {
92
+ return undefined;
93
+ }
94
+ finally {
95
+ controller.abort();
96
+ }
97
+ return undefined;
98
+ }
80
99
  /**
81
100
  * Events after `cursor`, reconnecting with the last seen ID when the stream drops,
82
101
  * until the caller stops iterating or the signal aborts.
@@ -12,6 +12,7 @@ import { DEFAULT_MODEL, laptopModel } from "./settings.js";
12
12
  import { sshCommandLine } from "./ssh.js";
13
13
  import { openBrowser } from "./open-browser.js";
14
14
  import { withConnections } from "./tunnel.js";
15
+ import { outdatedServers } from "./update.js";
15
16
  export { DEFAULT_MODEL, readAutopilotModel } from "./settings.js";
16
17
  /**
17
18
  * `dl` on a laptop: send tasks to where agents run (this computer, or your own server),
@@ -129,12 +130,16 @@ async function listRuns(remotes, connections, json, io) {
129
130
  io.out(`\n No tasks yet. From inside a GitHub repository: ${c.bold('dl run "fix the flaky checkout test"')}\n\n`);
130
131
  return;
131
132
  }
133
+ const reasons = await failureReasons(rows, remotes, connections);
132
134
  io.out("\n");
133
135
  for (const entry of rows) {
134
136
  const running = entry.activeRunId !== null;
135
137
  const glyph = running ? c.cyan("◐") : entry.status === "completed" || entry.status === "merged" ? c.green("✓") : ["failed", "interrupted"].includes(entry.status) ? c.red("✗") : c.dim("○");
136
138
  const multi = remotes.length > 1 ? `${entry.server} ` : "";
137
139
  io.out(` ${glyph} ${entry.conversationId.slice(0, 8)} ${multi}${(running ? "running" : entry.status).padEnd(11)} ${(entry.repository ?? "").padEnd(26)} ${c.dim(entry.pullRequestURL ?? entry.branch ?? "")}\n`);
140
+ const reason = reasons.get(entry.conversationId);
141
+ if (reason)
142
+ io.out(` ${c.dim(oneLine(reason, 100))}\n`);
138
143
  }
139
144
  io.out(`\n ${c.dim("Follow one:")} dl watch <id> ${c.dim("Check one out:")} dl pull <id>\n\n`);
140
145
  }
@@ -262,7 +267,7 @@ export async function renameServer(args, io = processIO, path) {
262
267
  return 0;
263
268
  }
264
269
  /** `dl` on a laptop: what this is, which servers it's connected to, what to type next. */
265
- export async function laptopHome(io = processIO, open, model = laptopModel) {
270
+ export async function laptopHome(io = processIO, open, model = laptopModel, versions) {
266
271
  const c = palette(io.color === true);
267
272
  const remotes = await loadRemotes();
268
273
  if (remotes.length === 0)
@@ -276,12 +281,19 @@ export async function laptopHome(io = processIO, open, model = laptopModel) {
276
281
  total += list?.length ?? 0;
277
282
  }
278
283
  });
279
- const [current, chosen] = await Promise.all([defaultName(), model()]);
284
+ const [current, chosen, outdated] = await Promise.all([
285
+ defaultName(),
286
+ model(),
287
+ versions ? outdatedServers(remotes, versions).catch(() => []) : Promise.resolve([]),
288
+ ]);
280
289
  io.out(`\n ${c.bold("Directionally Local")} ${c.dim("on this laptop")}\n\n`);
281
290
  io.out(" Coding agents that run on this computer or your own server: send a task, get a pull request back.\n\n");
282
291
  io.out(` ${c.dim("Runs on".padEnd(11))}${remotes.map((remote) => `${mark(remote, current, c)} ${remote.name} ${c.dim(where(remote))}`).join(", ")} ${c.dim(remotes.length > 1 ? "dl servers · dl use NAME" : "dl servers")}\n`);
283
292
  io.out(` ${c.dim("Tasks".padEnd(11))}${running} running · ${total} in all ${c.dim("dl runs")}\n`);
284
- io.out(` ${c.dim("Model".padEnd(11))}${chosen ?? DEFAULT_MODEL} ${c.dim("dl model")}\n\n`);
293
+ io.out(` ${c.dim("Model".padEnd(11))}${chosen ?? DEFAULT_MODEL} ${c.dim("dl model")}\n`);
294
+ for (const entry of outdated)
295
+ io.out(` ${c.yellow("!")} ${entry.name} is behind: ${entry.notice} ${c.dim(`dl update ${entry.name}`)}\n`);
296
+ io.out("\n");
285
297
  io.out(` ${c.dim("Start a session: cd into a GitHub repository, then type")} ${c.bold("dl")}\n`);
286
298
  io.out(` ${c.dim("Or hand off a one-off task:")} ${c.bold('dl run "fix the flaky checkout test"')}\n\n`);
287
299
  return true;
@@ -293,12 +305,15 @@ async function follow(client, remote, conversationId, cursor, signal, io, replay
293
305
  signal?.addEventListener("abort", stop, { once: true });
294
306
  const renderer = new EventRenderer(io, c);
295
307
  let outcome;
308
+ let finalEvent;
296
309
  try {
297
310
  if (replayUntil === undefined || replayUntil > cursor) {
298
311
  for await (const event of client.events(conversationId, cursor, controller.signal)) {
299
312
  renderer.render(event);
300
- if (RUN_END_EVENTS.has(event.type))
313
+ if (RUN_END_EVENTS.has(event.type)) {
301
314
  outcome = event.type;
315
+ finalEvent = event;
316
+ }
302
317
  if (replayUntil === undefined ? RUN_END_EVENTS.has(event.type) : event.id >= replayUntil)
303
318
  break;
304
319
  }
@@ -325,9 +340,18 @@ async function follow(client, remote, conversationId, cursor, signal, io, replay
325
340
  return 0;
326
341
  }
327
342
  io.err(`\n ${c.red("✗")} ${id} ${outcome}\n`);
343
+ const reason = failureReason(finalEvent);
344
+ if (reason)
345
+ for (const line of wrap(reason, 100, 3))
346
+ io.err(` ${c.dim(line)}\n`);
328
347
  if (renderer.unpriced || isModelNotPriced(JSON.stringify(final))) {
329
348
  io.err(` This model isn't in your server's price list yet. Run ${c.bold("dl setup")} to refresh it, or pick another model with -m.\n`);
330
349
  }
350
+ else {
351
+ const hint = reason ? failureHint(reason) : undefined;
352
+ if (hint)
353
+ io.err(` ${c.dim(hint)}\n`);
354
+ }
331
355
  io.err("\n");
332
356
  return 1;
333
357
  }
@@ -439,6 +463,78 @@ function oneLine(text, max) {
439
463
  const flat = text.replace(/\s+/g, " ").trim();
440
464
  return flat.length > max ? `${flat.slice(0, max - 1)}…` : flat;
441
465
  }
466
+ /** The reason a run ended, from its final event. Servers have used `reason`, `error` and `message`. */
467
+ export function failureReason(event) {
468
+ if (!event)
469
+ return undefined;
470
+ for (const key of ["reason", "error", "message", "detail"]) {
471
+ const value = event.data[key];
472
+ if (typeof value === "string" && value.trim())
473
+ return value.trim();
474
+ }
475
+ return undefined;
476
+ }
477
+ /** A next step for the reasons we recognise, or undefined. */
478
+ export function failureHint(reason) {
479
+ const text = reason.toLowerCase();
480
+ if (/context window|token limit|too many tokens|maximum context/.test(text))
481
+ return "pick a model with a bigger window: dl model";
482
+ if (/not priced|no such model|model not found|not offered|unknown model/.test(text))
483
+ return "pick another model: dl model";
484
+ if (/rate limit|429|quota/.test(text))
485
+ return "wait a moment, or check the OpenRouter key: dl keys openrouter";
486
+ if (/git_push_failed|push failed|permission denied|bad credentials|401/.test(text))
487
+ return "check the GitHub token: dl keys github";
488
+ if (/out of memory|oom|killed/.test(text))
489
+ return "the sandbox ran out of memory; try a smaller task";
490
+ return undefined;
491
+ }
492
+ /** `text` broken into at most `maxLines` lines of `width`, with an ellipsis when it was cut. */
493
+ export function wrap(text, width, maxLines) {
494
+ const words = text.replace(/\s+/g, " ").trim().split(" ").filter(Boolean);
495
+ const lines = [];
496
+ let line = "";
497
+ for (const word of words) {
498
+ if (line && line.length + 1 + word.length > width) {
499
+ lines.push(line);
500
+ line = word;
501
+ }
502
+ else {
503
+ line = line ? `${line} ${word}` : word;
504
+ }
505
+ }
506
+ if (line)
507
+ lines.push(line);
508
+ if (lines.length <= maxLines)
509
+ return lines;
510
+ const kept = lines.slice(0, maxLines);
511
+ kept[maxLines - 1] = `${kept[maxLines - 1].slice(0, width - 1)}…`;
512
+ return kept;
513
+ }
514
+ /** How many failed tasks `dl runs` asks about; each one is a request. */
515
+ const REASON_LOOKUPS = 10;
516
+ /** One line per failed task, from its final event. */
517
+ async function failureReasons(rows, remotes, connections) {
518
+ const reasons = new Map();
519
+ let lookups = 0;
520
+ for (const entry of rows) {
521
+ if (lookups >= REASON_LOOKUPS)
522
+ break;
523
+ if (entry.activeRunId !== null || !["failed", "interrupted"].includes(entry.status))
524
+ continue;
525
+ const remote = remotes.find((candidate) => candidate.name === entry.server);
526
+ if (!remote)
527
+ continue;
528
+ lookups++;
529
+ const event = await connections.client(remote)
530
+ .then((client) => client.lastEvent(entry.conversationId, entry.lastEventId))
531
+ .catch(() => undefined);
532
+ const reason = failureReason(event);
533
+ if (reason)
534
+ reasons.set(entry.conversationId, reason);
535
+ }
536
+ return reasons;
537
+ }
442
538
  export function describeError(error) {
443
539
  if (error instanceof RemoteApiError)
444
540
  return `${error.message} (${error.code})`;
@@ -0,0 +1,206 @@
1
+ import { execFile } from "node:child_process";
2
+ import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
3
+ import { homedir } from "node:os";
4
+ import { dirname, join } from "node:path";
5
+ import { promisify } from "node:util";
6
+ import { palette } from "../ui.js";
7
+ import { resolveRemote } from "./config.js";
8
+ import { EventReplayer } from "./remote-install.js";
9
+ import { checkServer } from "./ssh.js";
10
+ import { withConnections } from "./tunnel.js";
11
+ const execFileAsync = promisify(execFile);
12
+ const DAY_MS = 24 * 60 * 60 * 1000;
13
+ const STATE_FILE = "/etc/directionallylocal/state.json";
14
+ /** Non-root users run the server commands with sudo; checkServer made sure it needs no password. */
15
+ function sudo(facts) {
16
+ return facts.root ? "" : "sudo -n ";
17
+ }
18
+ /** The versions a server has installed, from the state file `dl up` writes (world-readable). */
19
+ export async function serverVersions(access, ssh) {
20
+ const result = await ssh(access, `cat ${STATE_FILE}`);
21
+ if (result.code !== 0)
22
+ return undefined;
23
+ try {
24
+ const state = JSON.parse(result.stdout);
25
+ return typeof state.autopilotVersion === "string" && typeof state.hotcellVersion === "string"
26
+ ? { autopilot: state.autopilotVersion, hotcell: state.hotcellVersion }
27
+ : undefined;
28
+ }
29
+ catch {
30
+ return undefined;
31
+ }
32
+ }
33
+ /** `1.2.10` is newer than `1.2.9`; a pre-release suffix is ignored, since npm's `latest` has none. */
34
+ export function compareVersions(a, b) {
35
+ const parse = (version) => version.replace(/^v/, "").split(/[.+-]/).slice(0, 3).map((part) => Number(part) || 0);
36
+ const left = parse(a);
37
+ const right = parse(b);
38
+ for (let index = 0; index < 3; index++) {
39
+ const difference = (left[index] ?? 0) - (right[index] ?? 0);
40
+ if (difference !== 0)
41
+ return difference < 0 ? -1 : 1;
42
+ }
43
+ return 0;
44
+ }
45
+ /** "Autopilot 2.7.0 → 2.7.2 · Hotcell 1.4.0 → 1.4.1", or undefined when the server is current. */
46
+ export function behindNotice(server, latest) {
47
+ const parts = [];
48
+ if (compareVersions(server.autopilot, latest.autopilot) < 0)
49
+ parts.push(`Autopilot ${server.autopilot} → ${latest.autopilot}`);
50
+ if (compareVersions(server.hotcell, latest.hotcell) < 0)
51
+ parts.push(`Hotcell ${server.hotcell} → ${latest.hotcell}`);
52
+ return parts.length > 0 ? parts.join(" · ") : undefined;
53
+ }
54
+ export function versionsCachePath(env = process.env) {
55
+ const cacheHome = env.XDG_CACHE_HOME?.trim() || join(homedir(), ".cache");
56
+ return join(cacheHome, "directionallylocal", "versions.json");
57
+ }
58
+ async function npmVersion(pkg) {
59
+ try {
60
+ const { stdout } = await execFileAsync("npm", ["view", pkg, "version"], { timeout: 15_000 });
61
+ const version = stdout.trim();
62
+ return /^\d+\.\d+\.\d+/.test(version) ? version : undefined;
63
+ }
64
+ catch {
65
+ return undefined;
66
+ }
67
+ }
68
+ /**
69
+ * npm's newest Autopilot and Hotcell, looked up at most once a day. Offline keeps the last
70
+ * answer rather than pretending the server is current.
71
+ */
72
+ export async function latestVersions(options = {}) {
73
+ const path = options.cacheFile ?? versionsCachePath();
74
+ const now = (options.now ?? Date.now)();
75
+ const maxAge = options.maxAgeMs ?? DAY_MS;
76
+ const cached = await readCache(path);
77
+ if (cached && now - cached.checkedAt < maxAge)
78
+ return cached;
79
+ const npm = options.npm ?? npmVersion;
80
+ const [autopilot, hotcell] = await Promise.all([npm("autopilot-ai"), npm("hotcell")]);
81
+ if (!autopilot || !hotcell)
82
+ return cached;
83
+ const fresh = { autopilot, hotcell, checkedAt: now };
84
+ await writeCache(path, fresh);
85
+ return fresh;
86
+ }
87
+ async function readCache(path) {
88
+ try {
89
+ const parsed = JSON.parse(await readFile(path, "utf8"));
90
+ return typeof parsed.autopilot === "string" && typeof parsed.hotcell === "string" && typeof parsed.checkedAt === "number"
91
+ ? { autopilot: parsed.autopilot, hotcell: parsed.hotcell, checkedAt: parsed.checkedAt }
92
+ : undefined;
93
+ }
94
+ catch {
95
+ return undefined;
96
+ }
97
+ }
98
+ async function writeCache(path, versions) {
99
+ try {
100
+ await mkdir(dirname(path), { recursive: true });
101
+ const temp = `${path}.${process.pid}.tmp`;
102
+ await writeFile(temp, `${JSON.stringify(versions, null, 2)}\n`, { mode: 0o644 });
103
+ await rename(temp, path);
104
+ }
105
+ catch {
106
+ // A cache that can't be written only costs another lookup next time.
107
+ }
108
+ }
109
+ /** How many tasks are running on a server, or undefined when it can't be asked. */
110
+ export async function runningTasks(remote, open) {
111
+ try {
112
+ return await withConnections(open, async (connections) => {
113
+ const list = await (await connections.client(remote)).listConversations(200);
114
+ return list.filter((entry) => entry.activeRunId !== null).length;
115
+ });
116
+ }
117
+ catch {
118
+ return undefined;
119
+ }
120
+ }
121
+ /** Servers that are behind npm, for `dl status`. One SSH round trip each, and a cached lookup. */
122
+ export async function outdatedServers(remotes, deps) {
123
+ const latest = await (deps.latest ?? (() => latestVersions()))();
124
+ if (!latest)
125
+ return [];
126
+ const outdated = [];
127
+ for (const remote of remotes) {
128
+ if (!remote.ssh)
129
+ continue;
130
+ const server = await serverVersions(remote.ssh, deps.ssh);
131
+ if (!server)
132
+ continue;
133
+ const notice = behindNotice(server, latest);
134
+ if (notice)
135
+ outdated.push({ name: remote.name, notice });
136
+ }
137
+ return outdated;
138
+ }
139
+ /** `dl update [NAME] [--wait]`: update a server over SSH, without interrupting its tasks. */
140
+ export async function update(args, deps) {
141
+ const c = palette(deps.io.color === true);
142
+ const name = args.find((arg) => !arg.startsWith("-"));
143
+ const remote = await resolveRemote(name);
144
+ if (!remote.ssh) {
145
+ deps.io.err(`\n ${c.red("✗")} ${remote.name} wasn't set up over SSH, so this laptop can't update it.\n On the server: ${c.bold("sudo dl up")}\n\n`);
146
+ return 1;
147
+ }
148
+ const facts = await checkServer(remote.ssh, deps.ssh, remote.name);
149
+ const before = await serverVersions(remote.ssh, deps.ssh);
150
+ const running = deps.running ?? ((target) => runningTasks(target, deps.open));
151
+ const count = await running(remote);
152
+ if (count === undefined) {
153
+ deps.io.err(` ${c.yellow("!")} Couldn't ask ${remote.name} what's running; updating anyway.\n`);
154
+ }
155
+ else if (count > 0) {
156
+ if (!(await settleRunning(remote, count, deps, running, c)))
157
+ return 1;
158
+ }
159
+ deps.io.out(`\n ${c.bold("Updating")} ${remote.name}${before ? c.dim(` (Autopilot ${before.autopilot} · Hotcell ${before.hotcell})`) : ""}\n\n`);
160
+ const replay = new EventReplayer(deps.newUi);
161
+ // No --access: the server keeps the way it's reached. `{}` means "no new answers", so a
162
+ // missing key fails at once instead of waiting for a prompt nobody can see.
163
+ const result = await deps.ssh(remote.ssh, `${sudo(facts)}dl up --answers-stdin --events json`, {
164
+ input: "{}\n",
165
+ onStdoutLine: (line) => replay.line(line),
166
+ });
167
+ replay.finish();
168
+ if (result.code !== 0 || replay.error) {
169
+ deps.io.err(`\n ${c.red("✗")} Updating ${remote.name} failed: ${replay.error ?? `dl up exited with ${result.code}`}\n`);
170
+ for (const line of replay.tail())
171
+ deps.io.err(` ${c.dim(line)}\n`);
172
+ deps.io.err("\n");
173
+ return 1;
174
+ }
175
+ const after = await serverVersions(remote.ssh, deps.ssh);
176
+ const changed = before && after && (before.autopilot !== after.autopilot || before.hotcell !== after.hotcell);
177
+ const versions = after ? ` ${c.dim(`Autopilot ${after.autopilot} · Hotcell ${after.hotcell}`)}` : "";
178
+ deps.io.out(`\n ${c.green("✓")} ${remote.name} is up to date${changed ? "" : c.dim(" (already was)")}${versions}\n\n`);
179
+ return 0;
180
+ }
181
+ /**
182
+ * Decides what to do about running tasks: wait when asked or when nobody can answer, and
183
+ * otherwise ask. Returns whether to go ahead.
184
+ */
185
+ async function settleRunning(remote, count, deps, running, c) {
186
+ const tasks = `${count} task${count === 1 ? "" : "s"}`;
187
+ if (deps.wait || !deps.interactive) {
188
+ deps.io.out(` ${c.cyan("·")} ${tasks} running on ${remote.name}; waiting for ${count === 1 ? "it" : "them"} to finish (Ctrl-C stops waiting).\n`);
189
+ await waitForIdle(remote, deps, running);
190
+ return true;
191
+ }
192
+ const answer = (await deps.prompter.text(` ${tasks} running on ${remote.name}. Update now and interrupt ${count === 1 ? "it" : "them"}? [y/N] `)).trim().toLowerCase();
193
+ if (answer === "y" || answer === "yes")
194
+ return true;
195
+ deps.io.out(` ${c.dim("Not updating. Wait for them, then run")} dl update ${remote.name}${c.dim(", or add --wait.")}\n\n`);
196
+ return false;
197
+ }
198
+ async function waitForIdle(remote, deps, running) {
199
+ const sleep = deps.sleep ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
200
+ for (;;) {
201
+ const count = await running(remote);
202
+ if (count === undefined || count === 0)
203
+ return;
204
+ await sleep(15_000);
205
+ }
206
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "directionallylocal",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Directionally Local: background coding agents on your own server. One-line server install, and the dl command for your server and laptop.",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://directionallylocal.com",
@@ -27,8 +27,10 @@
27
27
  },
28
28
  "scripts": {
29
29
  "build": "tsc -p tsconfig.build.json",
30
- "typecheck": "tsc --noEmit && tsc -p site/tsconfig.json",
31
- "test": "tsx --test \"src/**/*.test.ts\" \"site/**/*.test.ts\"",
30
+ "typecheck": "tsc --noEmit && tsc -p site/tsconfig.json && tsc -p services/tsconfig.json",
31
+ "test": "tsx --test \"src/**/*.test.ts\" \"site/**/*.test.ts\" \"services/**/*.test.ts\"",
32
+ "sync:site-script": "node site/generate-install-script.mjs",
33
+ "check:portable": "node scripts/check-portable-apis.mjs services/name services/shared",
32
34
  "prepublishOnly": "npm run typecheck && npm test && npm run build"
33
35
  },
34
36
  "devDependencies": {