@goodfish/jarvy 0.12.2 → 0.13.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,9 +1,9 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  // src/server/cli.ts
4
- import { existsSync as existsSync5 } from "node:fs";
4
+ import { existsSync as existsSync6 } from "node:fs";
5
5
  import { createInterface } from "node:readline/promises";
6
- import { dirname as dirname11, join as join19 } from "node:path";
6
+ import { dirname as dirname12, join as join20 } from "node:path";
7
7
  import { fileURLToPath as fileURLToPath2 } from "node:url";
8
8
 
9
9
  // src/server/api/server.ts
@@ -426,9 +426,13 @@ function botRoutes(bots) {
426
426
  return c.json(await bots.setComputerLimits(c.req.param("id"), { memory: input.memory, cpus: input.cpus }));
427
427
  });
428
428
  routes.get("/:id/computer/resources", async (c) => c.json(await bots.computerResources(c.req.param("id"))));
429
+ routes.put("/:id/computer/type", jsonBody, async (c) => {
430
+ const input = await readJson(c);
431
+ return c.json(await bots.setComputerType(c.req.param("id"), input.type));
432
+ });
429
433
  routes.post("/:id/computer/:action", async (c) => {
430
434
  const action = c.req.param("action");
431
- if (action !== "start" && action !== "stop" && action !== "reset") throw new HTTPException8(404, { message: "Not found" });
435
+ if (action !== "start" && action !== "stop" && action !== "reset" && action !== "restart") throw new HTTPException8(404, { message: "Not found" });
432
436
  return c.json(await bots.computer(c.req.param("id"), action));
433
437
  });
434
438
  routes.get("/:id", (c) => c.json(bots.describe(c.req.param("id"))));
@@ -957,7 +961,7 @@ function modelRoutes(models) {
957
961
  import { Hono as Hono8 } from "hono";
958
962
 
959
963
  // src/server/api/event-stream.ts
960
- function eventStream(c, authenticated, open2) {
964
+ function eventStream(c, authenticated, open3) {
961
965
  const signal = c.req.raw.signal;
962
966
  let disconnect;
963
967
  const body = new ReadableStream({
@@ -976,7 +980,7 @@ data: ${JSON.stringify(data)}
976
980
 
977
981
  `));
978
982
  };
979
- const { unsubscribe, ready } = open2({
983
+ const { unsubscribe, ready } = open3({
980
984
  send,
981
985
  close: () => disconnect(),
982
986
  signedIn: (listener) => (...args) => {
@@ -1238,6 +1242,8 @@ import { HTTPException as HTTPException13 } from "hono/http-exception";
1238
1242
  function environmentRoutes(bots) {
1239
1243
  const routes = new Hono13(), machines = bots.machines;
1240
1244
  routes.get("/", (c) => c.json({ machines: machines.list() }));
1245
+ routes.get("/vm", (c) => c.json(bots.vmSupport()));
1246
+ routes.post("/vm/check", (c) => c.json(bots.vmSupport(true)));
1241
1247
  routes.post("/scan", jsonBody, async (c) => {
1242
1248
  const input = await readJson(c);
1243
1249
  return c.json(await machines.scan({ host: input.host, port: input.port, user: input.user }));
@@ -4169,6 +4175,13 @@ var EnvironmentRepository = class {
4169
4175
  ...limits2.cpus !== null ? { cpus: String(limits2.cpus) } : {}
4170
4176
  }), id);
4171
4177
  }
4178
+ /** Keeps what a computer is (A85): a VM, or left out for a container. */
4179
+ setType(id, type) {
4180
+ const computer = this.get(id);
4181
+ if (computer?.kind !== "computer") return;
4182
+ const { type: _type, ...spec } = computer.spec;
4183
+ this.db.prepare("UPDATE environments SET connection=? WHERE id=?").run(JSON.stringify({ ...spec, ...type === "vm" ? { type } : {} }), id);
4184
+ }
4172
4185
  /** The Bot is deleted, and its computer with it. Its row stays its home, deleted, so what reads a deleted Bot's
4173
4186
  * placement still finds one. */
4174
4187
  dropComputer(botId) {
@@ -5530,7 +5543,7 @@ var User = class extends Member {
5530
5543
  // src/server/application.ts
5531
5544
  import { createHash as createHash9 } from "node:crypto";
5532
5545
  import { mkdir as mkdir8, realpath } from "node:fs/promises";
5533
- import { join as join17, resolve as resolve3 } from "node:path";
5546
+ import { join as join18, resolve as resolve3 } from "node:path";
5534
5547
  import lockfile from "proper-lockfile";
5535
5548
 
5536
5549
  // src/contract/mentions.ts
@@ -6141,12 +6154,12 @@ var ChatThreads = class {
6141
6154
  /** The open threads a member follows, most recently active first, up to `limit`, and how many there are (A73): what a
6142
6155
  * Bot is reminded of after a compaction. Finished threads are left out, as from a Bot's unread (A70). */
6143
6156
  openFollowed(actorId, limit2) {
6144
- const open2 = this.store.followedKeys(actorId).filter((row2) => row2.status === "open").sort((a, b) => b.lastReplyAt - a.lastReplyAt || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
6145
- const threads = open2.slice(0, limit2).flatMap((row2) => {
6157
+ const open3 = this.store.followedKeys(actorId).filter((row2) => row2.status === "open").sort((a, b) => b.lastReplyAt - a.lastReplyAt || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
6158
+ const threads = open3.slice(0, limit2).flatMap((row2) => {
6146
6159
  const chat = this.repository.getChat(actorId, row2.id), channel2 = chat?.parentId ? this.repository.getChat(actorId, chat.parentId) : null;
6147
6160
  return chat && channel2 ? [{ id: chat.id, title: chat.title, channel: { id: channel2.id, title: channel2.title }, lastReplyAt: row2.lastReplyAt, unread: row2.unread }] : [];
6148
6161
  });
6149
- return { total: open2.length, threads };
6162
+ return { total: open3.length, threads };
6150
6163
  }
6151
6164
  update(actorId, threadId, input) {
6152
6165
  const title = input.title;
@@ -6598,8 +6611,8 @@ function usageOverview(since2, perBot) {
6598
6611
  }
6599
6612
 
6600
6613
  // src/server/service/bot/service.ts
6601
- import { dirname as dirname10, join as join16 } from "node:path";
6602
- import { rm as rm6 } from "node:fs/promises";
6614
+ import { dirname as dirname11, join as join17 } from "node:path";
6615
+ import { rm as rm8 } from "node:fs/promises";
6603
6616
 
6604
6617
  // src/server/infra/pi/history.ts
6605
6618
  var value = (input) => JSON.parse(JSON.stringify(input));
@@ -7166,7 +7179,7 @@ function environmentTools(lookup, secrets) {
7166
7179
  const access = async (path) => {
7167
7180
  if (!await current().exists(path)) throw Object.assign(new Error(`ENOENT: no such file or directory, access '${path}'`), { code: "ENOENT" });
7168
7181
  };
7169
- const readFile4 = (path) => current().readFile(path), writeFile5 = (path, content2) => current().writeFile(path, content2);
7182
+ const readFile4 = (path) => current().readFile(path), writeFile4 = (path, content2) => current().writeFile(path, content2);
7170
7183
  const cwd = current().cwd;
7171
7184
  const definitions = [
7172
7185
  relocated(createBashToolDefinition(cwd, { operations: {
@@ -7190,8 +7203,8 @@ function environmentTools(lookup, secrets) {
7190
7203
  } }), current),
7191
7204
  // Pictures go to the model as pictures; the kind comes from a file's first bytes, as Pi's local read does.
7192
7205
  createReadToolDefinition(cwd, { operations: { readFile: readFile4, access, detectImageMimeType: async (path) => imageKind(await current().head(path, IMAGE_SNIFF_BYTES)) } }),
7193
- createWriteToolDefinition(cwd, { operations: { writeFile: writeFile5, mkdir: (path) => current().mkdir(path) } }),
7194
- createEditToolDefinition(cwd, { operations: { readFile: readFile4, writeFile: writeFile5, access } })
7206
+ createWriteToolDefinition(cwd, { operations: { writeFile: writeFile4, mkdir: (path) => current().mkdir(path) } }),
7207
+ createEditToolDefinition(cwd, { operations: { readFile: readFile4, writeFile: writeFile4, access } })
7195
7208
  ];
7196
7209
  return definitions.map((definition) => inside(definition, lookup, pinned));
7197
7210
  }
@@ -7578,6 +7591,35 @@ ENV NPM_CONFIG_PREFIX=/home/bot/.npm-global PATH=/home/bot/.npm-global/bin:$PATH
7578
7591
  USER bot
7579
7592
  WORKDIR /workspace
7580
7593
  `;
7594
+ var VM_DISK = "/opt/jarvy-disk/docker.img";
7595
+ var DOCKER_UP = `#!/bin/sh
7596
+ set -eu
7597
+ DISK=${VM_DISK}
7598
+ if ! mountpoint -q /var/lib/docker; then
7599
+ [ -e /dev/loop-control ] || mknod -m 660 /dev/loop-control c 10 237
7600
+ i=0; while [ $i -lt 8 ]; do [ -e /dev/loop$i ] || mknod -m 660 /dev/loop$i b 7 $i; i=$((i+1)); done
7601
+ dev=$(losetup -j "$DISK" | cut -d: -f1 | head -n1)
7602
+ [ -n "$dev" ] || dev=$(losetup --direct-io=on -f --show "$DISK")
7603
+ blkid "$dev" > /dev/null 2>&1 || mkfs.ext4 -q -F -E lazy_itable_init=1,lazy_journal_init=1 "$dev"
7604
+ mkdir -p /var/lib/docker && mount "$dev" /var/lib/docker
7605
+ fi
7606
+ grep -q ' /sys/fs/cgroup cgroup2 rw' /proc/mounts || mount -o remount,rw /sys/fs/cgroup 2>/dev/null || true
7607
+ if ! docker version > /dev/null 2>&1; then
7608
+ pkill -x dockerd 2>/dev/null || true; pkill -x containerd 2>/dev/null || true
7609
+ rm -rf /var/run/docker /var/run/docker.pid /var/run/docker.sock /run/containerd
7610
+ setsid nohup dockerd > /var/log/dockerd.log 2>&1 < /dev/null &
7611
+ i=0; until docker version > /dev/null 2>&1; do i=$((i+1)); [ $i -lt 120 ] || { tail -n 5 /var/log/dockerd.log >&2; exit 1; }; sleep 0.5; done
7612
+ fi
7613
+ `;
7614
+ var vmDockerfile = (base) => `FROM ${base}
7615
+ USER root
7616
+ RUN apt-get update \\
7617
+ && apt-get install -y --no-install-recommends docker.io docker-compose-v2 e2fsprogs iptables \\
7618
+ && rm -rf /var/lib/apt/lists/* \\
7619
+ && usermod -aG docker bot && mkdir -p /var/lib/docker /opt/jarvy-disk
7620
+ COPY jarvy-docker-up /usr/local/bin/jarvy-docker-up
7621
+ USER bot
7622
+ `;
7581
7623
 
7582
7624
  // src/server/infra/env/durable.ts
7583
7625
  import { BACKGROUND_CONTEXT, withAbortSignal } from "@earendil-works/chord/context";
@@ -7656,26 +7698,36 @@ var COMPUTER_CWD = "/workspace";
7656
7698
  var DAEMON_DIR = "/opt/jarvy";
7657
7699
  function docker(args, timeoutMs = 6e4) {
7658
7700
  return new Promise((resolve5, reject) => execFile("docker", args, { timeout: timeoutMs, maxBuffer: 8 << 20 }, (error, stdout, stderr) => {
7659
- if (error) reject(Object.assign(new Error((stderr || error.message).trim().split("\n").at(-1) ?? "docker failed"), { code: "docker_failed" }));
7660
- else resolve5(stdout.trim());
7701
+ if (!error) return resolve5(stdout.trim());
7702
+ const said = stderr.trim().split("\n").filter((line) => line.trim() && !/^Run 'docker .*--help'/.test(line)).at(-1);
7703
+ const ended = error.killed ? `docker ${args[0]} was stopped after ${timeoutMs / 1e3} seconds` : `docker ${args[0]} exited with ${error.code ?? error.signal}`;
7704
+ reject(Object.assign(new Error(said ?? (stdout.trim().split("\n").at(-1) || ended)), { code: "docker_failed" }));
7661
7705
  }));
7662
7706
  }
7663
- async function dockerAvailable() {
7664
- try {
7665
- await docker(["version", "--format", "{{.Server.Version}}"], 1e4);
7666
- return true;
7667
- } catch {
7668
- return false;
7669
- }
7707
+ function dockerProblem() {
7708
+ return new Promise((resolve5) => execFile("docker", ["version", "--format", "{{.Server.Version}}"], { timeout: 1e4 }, (error, _stdout, stderr) => {
7709
+ if (!error) return resolve5(null);
7710
+ const said = (stderr || error.message).trim(), detail = said.split("\n").filter(Boolean).at(-1) ?? said;
7711
+ if (error.code === "ENOENT") resolve5({ reason: "missing", detail: "no docker command" });
7712
+ else if (/permission denied/i.test(said)) resolve5({ reason: "denied", detail });
7713
+ else if (/cannot connect|failed to connect|daemon (is )?running|no such file|connection refused|error during connect/i.test(said)) resolve5({ reason: "stopped", detail });
7714
+ else resolve5({ reason: "failed", detail: error.killed ? "docker didn't answer within 10 seconds" : detail });
7715
+ }));
7670
7716
  }
7671
7717
  function computerImage(uid, gid) {
7672
7718
  return `jarvy-computer:${createHash6("sha256").update(`${COMPUTER_DOCKERFILE}
7673
7719
  ${uid}:${gid}`).digest("hex").slice(0, 12)}`;
7674
7720
  }
7721
+ function vmImage(base) {
7722
+ return `jarvy-computer:vm-${createHash6("sha256").update(`${vmDockerfile(base)}
7723
+ ${DOCKER_UP}`).digest("hex").slice(0, 12)}`;
7724
+ }
7725
+ var computerRecipe = (uid, gid) => ({ dockerfile: COMPUTER_DOCKERFILE, args: { UID: String(uid), GID: String(gid) } });
7726
+ var vmRecipe = (base) => ({ dockerfile: vmDockerfile(base), files: { "jarvy-docker-up": DOCKER_UP } });
7675
7727
  async function pruneImages(keep) {
7676
7728
  const tags = (await docker(["images", "--filter", "label=jarvy.computer=1", "--format", "{{.Repository}}:{{.Tag}}"])).split("\n").filter(Boolean);
7677
7729
  const removed = [];
7678
- for (const tag of tags) if (tag !== keep) {
7730
+ for (const tag of tags) if (!keep.includes(tag)) {
7679
7731
  try {
7680
7732
  await docker(["image", "rm", tag], 12e4);
7681
7733
  removed.push(tag);
@@ -7692,17 +7744,22 @@ async function imageExists(tag) {
7692
7744
  return false;
7693
7745
  }
7694
7746
  }
7695
- async function buildImage(tag, uid, gid, logFile) {
7747
+ async function buildImage(tag, recipe, logFile) {
7696
7748
  const dir = await mkdtemp(join9(tmpdir(), "jarvy-computer-"));
7697
7749
  try {
7698
- await writeFile2(join9(dir, "Dockerfile"), COMPUTER_DOCKERFILE);
7750
+ await writeFile2(join9(dir, "Dockerfile"), recipe.dockerfile);
7751
+ for (const [name3, content2] of Object.entries(recipe.files ?? {})) await writeFile2(join9(dir, name3), content2, { mode: 493 });
7699
7752
  const log = createWriteStream(logFile, { flags: "w", mode: 384 });
7700
7753
  const code = await new Promise((resolve5, reject) => {
7701
- const child = spawn(
7702
- "docker",
7703
- ["build", "-t", tag, "--label", "jarvy.computer=1", "--build-arg", `UID=${uid}`, "--build-arg", `GID=${gid}`, dir],
7704
- { stdio: ["ignore", "pipe", "pipe"] }
7705
- );
7754
+ const child = spawn("docker", [
7755
+ "build",
7756
+ "-t",
7757
+ tag,
7758
+ "--label",
7759
+ "jarvy.computer=1",
7760
+ ...Object.entries(recipe.args ?? {}).flatMap(([key, value2]) => ["--build-arg", `${key}=${value2}`]),
7761
+ dir
7762
+ ], { stdio: ["ignore", "pipe", "pipe"] });
7706
7763
  child.stdout.pipe(log, { end: false });
7707
7764
  child.stderr.pipe(log, { end: false });
7708
7765
  child.on("error", reject);
@@ -7746,6 +7803,7 @@ async function runContainer(spec) {
7746
7803
  ...Object.entries(spec.hosts ?? {}).flatMap(([name3, address2]) => ["--add-host", `${name3}:${address2}`]),
7747
7804
  ...spec.limits?.memory ? memoryFlags(spec.limits.memory, spec.limits.memory) : [],
7748
7805
  ...spec.limits?.cpus ? ["--cpus", String(spec.limits.cpus)] : [],
7806
+ ...spec.vm ? vmFlags(spec.vm.disk) : [],
7749
7807
  "-v",
7750
7808
  `${spec.workspace}:${COMPUTER_CWD}`,
7751
7809
  "-v",
@@ -7757,6 +7815,27 @@ async function runContainer(spec) {
7757
7815
  "infinity"
7758
7816
  ], 12e4);
7759
7817
  }
7818
+ var KATA_RUNTIME = "io.containerd.kata.v2";
7819
+ var vmFlags = (disk) => [
7820
+ "--runtime",
7821
+ KATA_RUNTIME,
7822
+ "--group-add",
7823
+ "docker",
7824
+ "--cap-add",
7825
+ "ALL",
7826
+ "--security-opt",
7827
+ "seccomp=unconfined",
7828
+ "--security-opt",
7829
+ "apparmor=unconfined",
7830
+ "--security-opt",
7831
+ "systempaths=unconfined",
7832
+ "--device-cgroup-rule",
7833
+ "b 7:* rmw",
7834
+ "--device-cgroup-rule",
7835
+ "c 10:237 rmw",
7836
+ "-v",
7837
+ `${disk}:${VM_DISK}`
7838
+ ];
7760
7839
  var memoryFlags = (memory, withSwap) => ["--memory", String(memory), "--memory-swap", String(withSwap)];
7761
7840
  async function updateLimits(name3, change, machine) {
7762
7841
  const args = [
@@ -7881,24 +7960,177 @@ function loopbackAddress(hex) {
7881
7960
 
7882
7961
  // src/server/service/bot/computers.ts
7883
7962
  import { createHash as createHash7 } from "node:crypto";
7884
- import { dirname as dirname5, join as join10 } from "node:path";
7963
+ import { dirname as dirname6, join as join11 } from "node:path";
7964
+
7965
+ // src/server/infra/env/kata.ts
7966
+ import { existsSync as existsSync4, readFileSync as readFileSync4 } from "node:fs";
7967
+ import { isIP } from "node:net";
7968
+ import { mkdir as mkdir5, open as open2, rm as rm5 } from "node:fs/promises";
7969
+ import { dirname as dirname5 } from "node:path";
7970
+ var VM_DISK_SIZE = 64 * 2 ** 30;
7971
+ async function vmProblem(image) {
7972
+ if (!existsSync4("/dev/kvm")) return { reason: "no_kvm", detail: "this machine has no /dev/kvm" };
7973
+ try {
7974
+ await docker(["run", "--rm", "--runtime", KATA_RUNTIME, "--memory", "512m", "--entrypoint", "true", image], 12e4);
7975
+ return null;
7976
+ } catch (error) {
7977
+ const said = error.message;
7978
+ if (/binary not installed|containerd-shim-kata|unknown runtime|executable file not found/i.test(said)) return { reason: "no_kata", detail: said };
7979
+ if (/resource busy|KVM_CREATE_VM/i.test(said) || /QMP ready/i.test(said) && otherHypervisor()) return { reason: "busy", detail: said };
7980
+ if (/\/dev\/kvm|kvm/i.test(said)) return { reason: "no_kvm", detail: said };
7981
+ return { reason: "failed", detail: said };
7982
+ }
7983
+ }
7984
+ function otherHypervisor() {
7985
+ try {
7986
+ return /^vboxdrv /m.test(readFileSync4("/proc/modules", "utf8"));
7987
+ } catch {
7988
+ return false;
7989
+ }
7990
+ }
7991
+ async function createDisk(path) {
7992
+ await mkdir5(dirname5(path), { recursive: true, mode: 448 });
7993
+ const file = await open2(path, "wx", 384).catch((error) => {
7994
+ if (error.code === "EEXIST") return null;
7995
+ throw error;
7996
+ });
7997
+ if (!file) return;
7998
+ try {
7999
+ await file.truncate(VM_DISK_SIZE);
8000
+ } finally {
8001
+ await file.close();
8002
+ }
8003
+ }
8004
+ async function removeDisk(path) {
8005
+ await rm5(path, { force: true });
8006
+ }
8007
+ async function dockerUp(container) {
8008
+ await docker(["exec", "-u", "root", container, "/usr/local/bin/jarvy-docker-up"], 9e4);
8009
+ }
8010
+ function hostResolvers() {
8011
+ for (const file of ["/run/systemd/resolve/resolv.conf", "/etc/resolv.conf"]) {
8012
+ let text5;
8013
+ try {
8014
+ text5 = readFileSync4(file, "utf8");
8015
+ } catch {
8016
+ continue;
8017
+ }
8018
+ const servers = [...text5.matchAll(/^\s*nameserver\s+(\S+)/gm)].map((match) => match[1]).filter((address2) => isIP(address2) && !address2.startsWith("127.") && address2 !== "::1" && !address2.toLowerCase().startsWith("fe80:"));
8019
+ if (servers.length) return servers.slice(0, 3);
8020
+ }
8021
+ return ["8.8.8.8", "8.8.4.4"];
8022
+ }
8023
+ async function useResolvers(container, servers) {
8024
+ await docker(["exec", "-u", "root", container, "sh", "-c", `printf 'nameserver %s\\n' ${servers.join(" ")} > /etc/resolv.conf`], 3e4);
8025
+ }
8026
+
8027
+ // src/server/service/bot/computer-vm.ts
8028
+ import { join as join10 } from "node:path";
8029
+ var VM_SIZE = { memory: 4 * 2 ** 30, cpus: 2 };
8030
+ var computerType = (record3) => record3.spec.type === "vm" ? "vm" : "container";
8031
+ var vmDisk = (dataDir, container) => join10(dataDir, "computers", "disks", `${container}.img`);
8032
+ var ComputerImages = class {
8033
+ constructor(dataDir, uid, gid, log) {
8034
+ this.dataDir = dataDir;
8035
+ this.uid = uid;
8036
+ this.gid = gid;
8037
+ this.log = log;
8038
+ }
8039
+ base = null;
8040
+ vm = null;
8041
+ /** The tag a sandbox of this type starts from, built or not. */
8042
+ tag(type) {
8043
+ const base = computerImage(this.uid, this.gid);
8044
+ return type === "vm" ? vmImage(base) : base;
8045
+ }
8046
+ /** The image for this type, built first when missing. */
8047
+ ready(type) {
8048
+ this.base ??= this.build(computerImage(this.uid, this.gid), computerRecipe(this.uid, this.gid)).catch((error) => {
8049
+ this.base = null;
8050
+ throw error;
8051
+ });
8052
+ if (type === "container") return this.base;
8053
+ this.vm ??= this.base.then((base) => this.build(vmImage(base), vmRecipe(base))).catch((error) => {
8054
+ this.vm = null;
8055
+ throw error;
8056
+ });
8057
+ return this.vm;
8058
+ }
8059
+ async build(tag, recipe) {
8060
+ if (await imageExists(tag)) return tag;
8061
+ this.log(`building ${tag}`);
8062
+ await buildImage(tag, recipe, join10(this.dataDir, "computers", "build.log"));
8063
+ const keep = [this.tag("container"), this.tag("vm")];
8064
+ void pruneImages(keep).then((removed) => {
8065
+ if (removed.length) this.log(`removed ${removed.join(", ")}`);
8066
+ }).catch(() => {
8067
+ });
8068
+ return tag;
8069
+ }
8070
+ };
8071
+ var VmSupportCheck = class {
8072
+ constructor(images, log) {
8073
+ this.images = images;
8074
+ this.log = log;
8075
+ }
8076
+ known = { state: "unknown" };
8077
+ running = null;
8078
+ get current() {
8079
+ return this.known;
8080
+ }
8081
+ /** The last answer, checking first when there is none yet or `again`. */
8082
+ check(again = false) {
8083
+ if (this.running) return this.running;
8084
+ if (!again && this.known.state !== "unknown") return Promise.resolve(this.known);
8085
+ this.known = { state: "checking" };
8086
+ this.running = (async () => {
8087
+ try {
8088
+ const problem = await vmProblem(await this.images.ready("container"));
8089
+ if (problem) this.log(`VM sandboxes can't be used (${problem.reason}): ${problem.detail}`);
8090
+ return problem ? { state: "unavailable", ...problem } : { state: "available" };
8091
+ } catch (error) {
8092
+ return { state: "unavailable", reason: "failed", detail: error.message };
8093
+ }
8094
+ })().then((answer) => {
8095
+ this.known = answer;
8096
+ return answer;
8097
+ }).finally(() => {
8098
+ this.running = null;
8099
+ });
8100
+ return this.running;
8101
+ }
8102
+ };
8103
+
8104
+ // src/server/service/bot/computers.ts
8105
+ import { userInfo } from "node:os";
7885
8106
  var SWEEP_MS = 6e4;
8107
+ var RECHECK_MS = 3e4;
7886
8108
  var HOST_NAME = "host.docker.internal";
7887
- var MIN_MEMORY = 256 * 2 ** 20;
7888
- var MIN_CPUS = 0.1;
8109
+ var MIN = { container: { memory: 256 * 2 ** 20, cpus: 0.1 }, vm: { memory: 2 ** 30, cpus: 1 } };
7889
8110
  function computerLimits(record3) {
7890
8111
  const read3 = (value2) => value2 && Number(value2) > 0 ? Number(value2) : null;
7891
- return { memory: read3(record3.spec.memory), cpus: read3(record3.spec.cpus) };
8112
+ const memory = read3(record3.spec.memory), cpus = read3(record3.spec.cpus);
8113
+ return computerType(record3) === "vm" ? { memory: memory ?? VM_SIZE.memory, cpus: cpus ?? VM_SIZE.cpus } : { memory, cpus };
7892
8114
  }
7893
8115
  var Computers = class {
7894
8116
  constructor(deps) {
7895
8117
  this.deps = deps;
7896
- this.dataDir = dirname5(deps.store.path);
8118
+ this.dataDir = dirname6(deps.store.path);
7897
8119
  this.tag = createHash7("sha256").update(this.dataDir).digest("hex").slice(0, 12);
8120
+ this.images = new ComputerImages(this.dataDir, this.uid, this.gid, (text5) => this.log(text5));
8121
+ this.vms = new VmSupportCheck(this.images, (text5) => this.log(text5));
7898
8122
  }
7899
8123
  docker = null;
8124
+ /** Why Docker can't be used, while it can't. */
8125
+ problem = null;
8126
+ recheck = null;
8127
+ closed = false;
7900
8128
  probing = null;
7901
- image = null;
8129
+ images;
8130
+ /** Whether VM sandboxes can be used here (A85). */
8131
+ vms;
8132
+ /** VM sandboxes resized while they ran: the new size comes with their next start. */
8133
+ resized = /* @__PURE__ */ new Set();
7902
8134
  timer = null;
7903
8135
  states = /* @__PURE__ */ new Map();
7904
8136
  preparing = /* @__PURE__ */ new Map();
@@ -7916,25 +8148,52 @@ var Computers = class {
7916
8148
  tag;
7917
8149
  uid = process.getuid?.() ?? 1e3;
7918
8150
  gid = process.getgid?.() ?? 1e3;
7919
- /** Looks for Docker and learns the state of every computer, once; every computer waits for it. */
8151
+ /** Looks for Docker and learns the state of every computer; every computer waits for the first look. While Docker
8152
+ * can't be used, it looks again every 30 seconds and takes it up once it can. */
7920
8153
  start() {
7921
8154
  this.probing ??= this.probe();
7922
8155
  return this.probing;
7923
8156
  }
7924
8157
  async probe() {
7925
- this.docker = await dockerAvailable();
7926
- if (!this.docker) return;
8158
+ const problem = await dockerProblem();
8159
+ if (problem) return this.unusable(problem);
7927
8160
  try {
7928
- await syncDaemon(join10(this.dataDir, "computers", "bin"));
8161
+ await syncDaemon(join11(this.dataDir, "computers", "bin"));
7929
8162
  } catch (error) {
7930
- this.docker = false;
7931
- this.log(`computers disabled: ${error.message}`);
7932
- return;
8163
+ return this.unusable({ reason: "failed", detail: `the tools' daemon could not be put in place: ${error.message}` });
7933
8164
  }
8165
+ const found = this.docker === false;
8166
+ this.docker = true;
8167
+ this.problem = null;
8168
+ if (found) this.log("Docker can be used now");
7934
8169
  await this.joinNetwork().catch((error) => this.log(`sandbox network unavailable, staying on the default bridge: ${error.message}`));
7935
8170
  await this.sweep().catch((error) => this.log(`computer sweep failed: ${error.message}`));
7936
8171
  this.timer = setInterval(() => void this.sweep().catch((error) => this.log(`computer sweep failed: ${error.message}`)), SWEEP_MS);
7937
8172
  this.timer.unref();
8173
+ if (found) this.deps.changed();
8174
+ }
8175
+ /** Docker can't be used: said once in the log per reason, and looked for again later. */
8176
+ unusable(problem) {
8177
+ const before = this.docker === null ? null : this.problem?.reason;
8178
+ if (before !== problem.reason) this.log(`Docker can't be used (${problem.reason}): ${problem.detail}`);
8179
+ this.docker = false;
8180
+ this.problem = problem;
8181
+ if (before && before !== problem.reason) this.deps.changed();
8182
+ if (this.closed) return;
8183
+ this.recheck = setTimeout(() => {
8184
+ this.recheck = null;
8185
+ void this.probe();
8186
+ }, RECHECK_MS);
8187
+ this.recheck.unref();
8188
+ }
8189
+ /** Why Docker can't be used, in a Bot's words; null when it can, or before Jarvy has looked. */
8190
+ get dockerProblem() {
8191
+ const problem = this.docker === false ? this.problem : null;
8192
+ if (!problem) return null;
8193
+ if (problem.reason === "missing") return "Docker isn't installed on the computer running Jarvy";
8194
+ if (problem.reason === "stopped") return "Docker is installed on the computer running Jarvy but isn't running";
8195
+ if (problem.reason === "denied") return `Docker on the computer running Jarvy refuses the user Jarvy runs as (${userInfo().username})`;
8196
+ return `Docker on the computer running Jarvy can't be used (${problem.detail})`;
7938
8197
  }
7939
8198
  /** Whether new computers can be made here; false until `start` found Docker. */
7940
8199
  get available() {
@@ -7942,40 +8201,74 @@ var Computers = class {
7942
8201
  }
7943
8202
  status(botId) {
7944
8203
  if (!this.deps.store.environments.computerOf(botId)) return null;
7945
- if (!this.available) return { state: "unavailable" };
8204
+ if (!this.available) return { state: "unavailable", ...this.problem ? { error: this.problem.reason } : {} };
7946
8205
  return this.states.get(botId) ?? { state: "stopped" };
7947
8206
  }
7948
8207
  /** The sandbox as Bot info shows it: its state, and the container, image and workspace folder it is made of. */
7949
8208
  info(botId) {
7950
8209
  const computer = this.deps.store.environments.computerOf(botId), status = this.status(botId);
7951
8210
  if (!computer || !status) return null;
8211
+ const type = computerType(computer);
7952
8212
  return {
7953
8213
  ...status,
8214
+ type,
7954
8215
  container: computer.spec.container,
7955
- image: computerImage(this.uid, this.gid),
8216
+ image: this.images.tag(type),
7956
8217
  workspace: this.workspace(botId),
7957
8218
  mount: COMPUTER_CWD,
7958
- limits: computerLimits(computer)
8219
+ limits: computerLimits(computer),
8220
+ ...this.resized.has(botId) ? { restartNeeded: true } : {}
7959
8221
  };
7960
8222
  }
7961
- /** What the sandbox uses now, when it runs, and what this machine has; null for what Docker doesn't answer. */
8223
+ /** Whether VM sandboxes can be used here, as last checked (A85). */
8224
+ get vmSupport() {
8225
+ return this.available ? this.vms.current : { state: "unavailable", reason: "failed", detail: this.dockerProblem ?? "Docker can't be used" };
8226
+ }
8227
+ /** Checks whether VM sandboxes can be used here: the first time when asked, or `again`. */
8228
+ checkVms(again = false) {
8229
+ return this.available ? this.vms.check(again) : Promise.resolve(this.vmSupport);
8230
+ }
8231
+ /** Makes the Bot's sandbox a container or a VM (A85). Like a reset, what was installed goes and the workspace stays;
8232
+ * a VM's Docker disk goes too. The next start makes the new one. */
8233
+ async setType(botId, type) {
8234
+ const computer = this.need(botId);
8235
+ if (computerType(computer) === type) return;
8236
+ if (type === "vm") {
8237
+ const support = await this.vms.check();
8238
+ if (support.state !== "available") throw new ValidationError("This machine can't run VM sandboxes", "vm_unavailable", { reason: support.reason ?? "failed" });
8239
+ }
8240
+ await this.settled(botId);
8241
+ await this.disconnect(botId);
8242
+ await removeContainer(computer.spec.container);
8243
+ await removeDisk(vmDisk(this.dataDir, computer.spec.container));
8244
+ this.homes.delete(computer.spec.container);
8245
+ this.resized.delete(botId);
8246
+ this.deps.store.environments.setType(computer.id, type);
8247
+ this.set(botId, { state: "stopped" });
8248
+ this.deps.changed();
8249
+ }
8250
+ /** What the sandbox uses now, when it runs, and what this machine has; null for what Docker doesn't answer. A VM's
8251
+ * use isn't known (A85): Docker counts only its container's own processes, not what runs in the VM besides. */
7962
8252
  async resources(botId) {
7963
8253
  const computer = this.need(botId);
7964
- const usage = this.status(botId)?.state === "running" ? await containerUsage(computer.spec.container).catch(() => null) : null;
8254
+ const usage = this.status(botId)?.state === "running" && computerType(computer) === "container" ? await containerUsage(computer.spec.container).catch(() => null) : null;
7965
8255
  return { usage, capacity: await this.capacity().catch(() => null) };
7966
8256
  }
7967
8257
  /** Sets what the sandbox may use at most (A75): a container that exists changes at once, running or not, and one made
7968
8258
  * later, after a reset too, starts with them. Each is null for no limit, or from a floor up to this machine's size. */
7969
8259
  async setLimits(botId, input) {
7970
- const computer = this.need(botId), machine = await this.capacity();
7971
- const memory = limit(input.memory, MIN_MEMORY, machine.memory, 2 ** 20), cpus = limit(input.cpus, MIN_CPUS, machine.cpus, 0.01);
8260
+ const computer = this.need(botId), machine = await this.capacity(), type = computerType(computer), least = MIN[type];
8261
+ const memory = limit(input.memory, least.memory, machine.memory, 2 ** 20), cpus = limit(input.cpus, least.cpus, machine.cpus, 0.01);
7972
8262
  if (memory === void 0 || cpus === void 0) throw new ValidationError(
7973
8263
  "Limits are null or a number within this machine's size",
7974
- "limits_invalid",
8264
+ type === "vm" ? "vm_size_invalid" : "limits_invalid",
7975
8265
  { memory: Math.floor(machine.memory / 2 ** 30 * 10) / 10, cpus: machine.cpus }
7976
8266
  );
7977
- const before = computerLimits(computer), change = { ...memory !== before.memory ? { memory } : {}, ...cpus !== before.cpus ? { cpus } : {} };
7978
- if (Object.keys(change).length && await containerState(computer.spec.container) !== "missing") await updateLimits(computer.spec.container, change, machine);
8267
+ const next = computerLimits({ ...computer, spec: { ...computer.spec, memory: memory === null ? "" : String(memory), cpus: cpus === null ? "" : String(cpus) } });
8268
+ const before = computerLimits(computer), change = { ...next.memory !== before.memory ? { memory: next.memory } : {}, ...next.cpus !== before.cpus ? { cpus: next.cpus } : {} };
8269
+ const state = Object.keys(change).length ? await containerState(computer.spec.container) : "missing";
8270
+ if (state !== "missing") await updateLimits(computer.spec.container, change, machine);
8271
+ if (type === "vm" && state === "running") this.resized.add(botId);
7979
8272
  this.deps.store.environments.setLimits(computer.id, { memory, cpus });
7980
8273
  this.deps.changed();
7981
8274
  }
@@ -8033,7 +8326,10 @@ var Computers = class {
8033
8326
  const container = await this.ensure(botId);
8034
8327
  let home = this.homes.get(container);
8035
8328
  if (!home) {
8036
- home = this.prepare(container).catch((error) => this.log(`sandbox of ${botId} not set up: ${error.message}`));
8329
+ home = this.prepare(container, computerType(record3) === "vm").catch((error) => {
8330
+ this.homes.delete(container);
8331
+ this.log(`sandbox of ${botId} not set up: ${error.message}`);
8332
+ });
8037
8333
  this.homes.set(container, home);
8038
8334
  }
8039
8335
  await home;
@@ -8044,11 +8340,15 @@ var Computers = class {
8044
8340
  }
8045
8341
  return env;
8046
8342
  }
8047
- /** Sets up a container after it starts: the workspace as its home, and this machine's name for one made before
8048
- * containers got it at creation. */
8049
- async prepare(container) {
8343
+ /** Sets up a container after it starts: the workspace as its home, this machine's name for one made before
8344
+ * containers got it at creation, and in a VM (A85) Docker on its disk. */
8345
+ async prepare(container, vm) {
8050
8346
  await homeAtWorkspace(container);
8051
8347
  if (this.network) await addHost(container, HOST_NAME, this.network.gateway);
8348
+ if (vm) {
8349
+ await useResolvers(container, hostResolvers());
8350
+ await dockerUp(container);
8351
+ }
8052
8352
  }
8053
8353
  /** Makes this Jarvy's network and moves the containers still on Docker's default bridge there, running or not. */
8054
8354
  async joinNetwork() {
@@ -8106,29 +8406,35 @@ var Computers = class {
8106
8406
  const task = (async () => {
8107
8407
  try {
8108
8408
  await this.start();
8109
- if (!this.available) throw new RuntimeFault("docker_unavailable", "Docker is not available on this machine.", 503);
8110
- const image = await this.imageReady();
8409
+ if (!this.available) throw this.unavailable(503);
8410
+ const type = computerType(computer), image = await this.images.ready(type), disk = vmDisk(this.dataDir, container);
8411
+ if (type === "vm") await createDisk(disk);
8111
8412
  const state = await containerState(container);
8112
8413
  if (state === "missing") await runContainer({
8113
8414
  name: container,
8114
8415
  image,
8115
8416
  workspace: this.workspace(botId),
8116
- daemonDir: join10(this.dataDir, "computers", "bin"),
8417
+ daemonDir: join11(this.dataDir, "computers", "bin"),
8117
8418
  uid: this.uid,
8118
8419
  gid: this.gid,
8119
8420
  labels: { "jarvy.data": this.tag, "jarvy.bot": botId },
8120
8421
  limits: computerLimits(computer),
8121
- ...this.network ? { network: this.network.name, hosts: { [HOST_NAME]: this.network.gateway } } : {}
8422
+ ...this.network ? { network: this.network.name, hosts: { [HOST_NAME]: this.network.gateway } } : {},
8423
+ ...type === "vm" ? { vm: { disk } } : {}
8122
8424
  });
8123
8425
  else if (state === "stopped") await startContainer(container);
8124
8426
  if (state !== "running") {
8125
8427
  this.homes.delete(container);
8126
8428
  this.book = null;
8429
+ this.resized.delete(botId);
8127
8430
  }
8128
8431
  this.set(botId, { state: "running" });
8129
8432
  return container;
8130
8433
  } catch (error) {
8131
- this.set(botId, { state: "failed", error: error instanceof RuntimeFault ? error.code : "computer_failed" });
8434
+ if (error instanceof RuntimeFault && error.code === "docker_unavailable") {
8435
+ this.states.delete(botId);
8436
+ this.deps.changed();
8437
+ } else this.set(botId, { state: "failed", error: error instanceof RuntimeFault ? error.code : "computer_failed" });
8132
8438
  this.log(`computer of ${botId} failed: ${error.message}`);
8133
8439
  if (error instanceof RuntimeFault) throw error;
8134
8440
  throw new RuntimeFault("computer_failed", `Your sandbox could not start: ${error.message}`, 503);
@@ -8141,15 +8447,19 @@ var Computers = class {
8141
8447
  const computer = this.need(botId);
8142
8448
  await this.disconnect(botId);
8143
8449
  await stopContainer(computer.spec.container);
8450
+ this.resized.delete(botId);
8144
8451
  this.set(botId, { state: "stopped" });
8145
8452
  }
8146
8453
  /** Removes the container, so the next start makes a fresh one from the image: what was installed goes, the
8147
8454
  * workspace stays. */
8148
8455
  async reset(botId) {
8149
8456
  const computer = this.need(botId);
8457
+ await this.settled(botId);
8150
8458
  await this.disconnect(botId);
8151
8459
  await removeContainer(computer.spec.container);
8460
+ await removeDisk(vmDisk(this.dataDir, computer.spec.container));
8152
8461
  this.homes.delete(computer.spec.container);
8462
+ this.resized.delete(botId);
8153
8463
  this.set(botId, { state: "stopped" });
8154
8464
  }
8155
8465
  /** The Bot is being deleted: its container goes with it. */
@@ -8158,41 +8468,36 @@ var Computers = class {
8158
8468
  if (!computer) return;
8159
8469
  await this.disconnect(botId);
8160
8470
  if (this.available) await removeContainer(computer.spec.container);
8471
+ await removeDisk(vmDisk(this.dataDir, computer.spec.container));
8161
8472
  this.deps.store.environments.dropComputer(botId);
8162
8473
  this.states.delete(botId);
8163
8474
  }
8164
8475
  /** Closes the connections; containers keep running. */
8165
8476
  async close() {
8477
+ this.closed = true;
8166
8478
  if (this.timer) clearInterval(this.timer);
8479
+ if (this.recheck) clearTimeout(this.recheck);
8167
8480
  this.timer = null;
8481
+ this.recheck = null;
8168
8482
  await Promise.all([...this.connections.keys()].map((botId) => this.disconnect(botId)));
8169
8483
  }
8484
+ unavailable(status) {
8485
+ const problem = this.dockerProblem;
8486
+ return new RuntimeFault("docker_unavailable", problem ? `${problem[0].toUpperCase()}${problem.slice(1)}.` : "Docker is not available on this machine.", status);
8487
+ }
8170
8488
  workspace(botId) {
8171
- return join10(this.dataDir, "bots", botId, "workspace");
8489
+ return join11(this.dataDir, "bots", botId, "workspace");
8172
8490
  }
8173
8491
  need(botId) {
8174
8492
  const computer = this.deps.store.environments.computerOf(botId);
8175
8493
  if (!computer) throw new RuntimeFault("computer_missing", "This Bot has no sandbox.", 404);
8176
- if (!this.available) throw new RuntimeFault("docker_unavailable", "Docker is not available on this machine.", 409);
8494
+ if (!this.available) throw this.unavailable(409);
8177
8495
  return computer;
8178
8496
  }
8179
- imageReady() {
8180
- this.image ??= (async () => {
8181
- const tag = computerImage(this.uid, this.gid);
8182
- if (!await imageExists(tag)) {
8183
- this.log(`building ${tag}`);
8184
- await buildImage(tag, this.uid, this.gid, join10(this.dataDir, "computers", "build.log"));
8185
- void pruneImages(tag).then((removed) => {
8186
- if (removed.length) this.log(`removed ${removed.join(", ")}`);
8187
- }).catch(() => {
8188
- });
8189
- }
8190
- return tag;
8191
- })().catch((error) => {
8192
- this.image = null;
8193
- throw error;
8497
+ /** Waits for a start under way, so a container it is making isn't made after the one replacing it is asked for. */
8498
+ async settled(botId) {
8499
+ await this.preparing.get(botId)?.catch(() => {
8194
8500
  });
8195
- return this.image;
8196
8501
  }
8197
8502
  async disconnect(botId) {
8198
8503
  const env = this.connections.get(botId);
@@ -8220,6 +8525,7 @@ var Computers = class {
8220
8525
  const running = states.get(computer.spec.container) === "running";
8221
8526
  if (!running) {
8222
8527
  if (this.states.get(botId)?.state !== "failed") this.set(botId, { state: "stopped" });
8528
+ this.resized.delete(botId);
8223
8529
  await this.disconnect(botId);
8224
8530
  continue;
8225
8531
  }
@@ -8244,6 +8550,9 @@ function limits(record3) {
8244
8550
  const parts = [memory ? `${Number((memory / 2 ** 30).toFixed(2))} GB of memory` : "", cpus ? `${cpus} ${cpus === 1 ? "CPU" : "CPUs"}` : ""].filter(Boolean);
8245
8551
  return parts.length ? parts.join(" and ") : null;
8246
8552
  }
8553
+ var sentence = (text5) => `${text5[0].toUpperCase()}${text5.slice(1)}`;
8554
+ var VM = "A Linux virtual machine only you use, with Node, Python, git, build tools, Playwright with its browsers, and Docker with Docker Compose: docker works there without sudo, and a port a container of yours publishes is a port of your sandbox. Your images, containers and volumes stay until the owner resets your sandbox.";
8555
+ var CONTAINER = "A Linux container only you use, with Node, Python, git, build tools, and Playwright with its browsers.";
8247
8556
  var sandbox = {
8248
8557
  kind: "computer",
8249
8558
  home: true,
@@ -8261,12 +8570,17 @@ var sandbox = {
8261
8570
  connects: false,
8262
8571
  summary: (record3, botId) => record3.botId === botId ? "own sandbox" : "another Bot's sandbox",
8263
8572
  describe: (record3, botId, deps) => {
8264
- const most = limits(record3);
8265
- return { state: deps.computers.status(record3.botId ?? botId)?.state ?? "stopped", ...most ? { limits: `at most ${most}` } : {} };
8573
+ const most = limits(record3), problem = deps.computers.dockerProblem;
8574
+ return {
8575
+ state: deps.computers.status(record3.botId ?? botId)?.state ?? "stopped",
8576
+ ...computerType(record3) === "vm" ? { type: "virtual machine with Docker" } : {},
8577
+ ...most ? { limits: `at most ${most}` } : {},
8578
+ ...problem ? { problem } : {}
8579
+ };
8266
8580
  },
8267
8581
  about: (record3, _botId, deps) => {
8268
8582
  const most = limits(record3);
8269
- return "A Linux container only you use, with Node, Python, git, build tools, and Playwright with its browsers. You can install software there with sudo; it stays until the owner resets your sandbox, and /workspace, your home there, is kept when it is reset. Other Bots' sandboxes can't be reached from it; services of this machine that listen on all its addresses are at host.docker.internal. To show a web service of yours to people or other Bots, publish its port with ports.publish." + (most ? ` It may use at most ${most}; a process that takes more memory than that is ended.` : "") + (deps.computers.available ? "" : " This machine has no Docker yet, so your file and command tools are unavailable here; your chat tools work as usual.");
8583
+ return (computerType(record3) === "vm" ? VM : CONTAINER) + " You can install software there with sudo; it stays until the owner resets your sandbox, and /workspace, your home there, is kept when it is reset. Other Bots' sandboxes can't be reached from it; services of this machine that listen on all its addresses are at host.docker.internal. To show a web service of yours to people or other Bots, publish its port with ports.publish." + (most ? ` It may use at most ${most}; a process that takes more memory than that is ended.` : "") + (deps.computers.dockerProblem ? ` ${sentence(deps.computers.dockerProblem)}, so your file and command tools don't work here; your chat tools work as usual. Jarvy looks for Docker again every 30 seconds.` : "");
8270
8584
  },
8271
8585
  signature: (record3) => record3.id
8272
8586
  };
@@ -8299,9 +8613,9 @@ var BotEnvironments = class {
8299
8613
  kinds;
8300
8614
  current(botId, workspacePath) {
8301
8615
  const target3 = this.store.environments.placement(botId).current, signature = this.signature(target3);
8302
- const open2 = this.open.get(botId);
8303
- if (open2?.id === signature) return open2.view;
8304
- if (open2) this.retire(open2);
8616
+ const open3 = this.open.get(botId);
8617
+ if (open3?.id === signature) return open3.view;
8618
+ if (open3) this.retire(open3);
8305
8619
  const opened = this.track(signature, KINDS[target3.kind].open(target3, botId, workspacePath, this.kinds));
8306
8620
  this.open.set(botId, opened);
8307
8621
  return opened.view;
@@ -8390,24 +8704,24 @@ var BotEnvironments = class {
8390
8704
  return { changed, moved: { from, to: this.here(botId, workspacePath) }, name: name3 };
8391
8705
  }
8392
8706
  async release(botId) {
8393
- const open2 = this.open.get(botId);
8707
+ const open3 = this.open.get(botId);
8394
8708
  this.open.delete(botId);
8395
- await open2?.env.close();
8709
+ await open3?.env.close();
8396
8710
  await this.computers.remove(botId);
8397
8711
  }
8398
8712
  async close() {
8399
8713
  const all = [...this.open.values()];
8400
8714
  this.open.clear();
8401
- await Promise.all(all.map((open2) => open2.env.close()));
8715
+ await Promise.all(all.map((open3) => open3.env.close()));
8402
8716
  await this.computers.close();
8403
8717
  }
8404
8718
  /** What an open environment is for: changing a machine's directory reopens it where the owner put it. */
8405
8719
  signature(record3) {
8406
8720
  return KINDS[record3.kind].signature(record3);
8407
8721
  }
8408
- retire(open2) {
8409
- open2.retired = true;
8410
- if (!open2.busy) void open2.env.close();
8722
+ retire(open3) {
8723
+ open3.retired = true;
8724
+ if (!open3.busy) void open3.env.close();
8411
8725
  }
8412
8726
  /** Counts the operations running on an environment, so one the Bot has left closes only when they are done. */
8413
8727
  track(id, env) {
@@ -8443,13 +8757,13 @@ var BotEnvironments = class {
8443
8757
 
8444
8758
  // src/server/service/bot/machines.ts
8445
8759
  import { randomUUID as randomUUID18 } from "node:crypto";
8446
- import { dirname as dirname6, join as join12, posix } from "node:path";
8760
+ import { dirname as dirname7, join as join13, posix } from "node:path";
8447
8761
 
8448
8762
  // src/server/infra/env/ssh.ts
8449
8763
  import { spawn as spawn2 } from "node:child_process";
8450
8764
  import { mkdirSync as mkdirSync6, rmSync as rmSync2, writeFileSync as writeFileSync3 } from "node:fs";
8451
- import { mkdir as mkdir5, mkdtemp as mkdtemp2, readFile as readFile3, rm as rm5 } from "node:fs/promises";
8452
- import { join as join11 } from "node:path";
8765
+ import { mkdir as mkdir6, mkdtemp as mkdtemp2, readFile as readFile3, rm as rm6 } from "node:fs/promises";
8766
+ import { join as join12 } from "node:path";
8453
8767
  import { HostKeyChangedError, HostKeyUnknownError, RemoteExecutionEnv as RemoteExecutionEnv2, detectPlatform, scanHostKey, sshArguments, sshConnection } from "@earendil-works/pi-env";
8454
8768
  var SshFault = class extends Error {
8455
8769
  constructor(code, message) {
@@ -8460,9 +8774,9 @@ var SshFault = class extends Error {
8460
8774
  var SCAN_ALIAS = "jarvy-scan";
8461
8775
  var TIMEOUT_MS2 = 3e4;
8462
8776
  async function scanHost(address2, runtime) {
8463
- await mkdir5(runtime, { recursive: true, mode: 448 });
8777
+ await mkdir6(runtime, { recursive: true, mode: 448 });
8464
8778
  try {
8465
- const scanned = await scanHostKey({ ...target(address2, SCAN_ALIAS), knownHostsFile: join11(runtime, "none") });
8779
+ const scanned = await scanHostKey({ ...target(address2, SCAN_ALIAS), knownHostsFile: join12(runtime, "none") });
8466
8780
  return {
8467
8781
  keys: scanned.lines.map((line) => line.trim().split(/\s+/).slice(1, 3).join(" ")),
8468
8782
  fingerprints: scanned.fingerprints.map((line) => line.replace(` ${SCAN_ALIAS} `, " ").replace(/^\d+\s+/, ""))
@@ -8472,13 +8786,13 @@ async function scanHost(address2, runtime) {
8472
8786
  }
8473
8787
  }
8474
8788
  async function generateKey(runtime, comment) {
8475
- await mkdir5(runtime, { recursive: true, mode: 448 });
8476
- const directory = await mkdtemp2(join11(runtime, "keygen-"));
8789
+ await mkdir6(runtime, { recursive: true, mode: 448 });
8790
+ const directory = await mkdtemp2(join12(runtime, "keygen-"));
8477
8791
  try {
8478
- await run("ssh-keygen", ["-q", "-t", "ed25519", "-N", "", "-C", comment, "-f", join11(directory, "key")]);
8479
- return { privateKey: await readFile3(join11(directory, "key"), "utf8"), publicKey: (await readFile3(join11(directory, "key.pub"), "utf8")).trim() };
8792
+ await run("ssh-keygen", ["-q", "-t", "ed25519", "-N", "", "-C", comment, "-f", join12(directory, "key")]);
8793
+ return { privateKey: await readFile3(join12(directory, "key"), "utf8"), publicKey: (await readFile3(join12(directory, "key.pub"), "utf8")).trim() };
8480
8794
  } finally {
8481
- await rm5(directory, { recursive: true, force: true });
8795
+ await rm6(directory, { recursive: true, force: true });
8482
8796
  }
8483
8797
  }
8484
8798
  function publicKeyOf(privateKey, comment) {
@@ -8545,7 +8859,7 @@ async function removeKey(access, comment) {
8545
8859
  }
8546
8860
  function sshEnvironment(id, access, directory, temp, onLog) {
8547
8861
  mkdirSync6(access.runtime, { recursive: true, mode: 448 });
8548
- const folder = join11(access.runtime, `open-${id}-${process.pid}-${Date.now().toString(36)}`);
8862
+ const folder = join12(access.runtime, `open-${id}-${process.pid}-${Date.now().toString(36)}`);
8549
8863
  const files = writeFilesSync(folder, access);
8550
8864
  const { connection } = sshConnection({ ...target(access, access.alias), ...files, ...onLog ? { onLog } : {} });
8551
8865
  return overDurable(id, "ssh", new RemoteExecutionEnv2({ connection, id: `ssh:${id}`, cwd: directory }), {
@@ -8564,21 +8878,21 @@ function target(address2, alias2) {
8564
8878
  return { host: address2.host, port: address2.port, user: address2.user, hostKeyAlias: alias2, configFile: "/dev/null" };
8565
8879
  }
8566
8880
  async function withFiles(access, work) {
8567
- await mkdir5(access.runtime, { recursive: true, mode: 448 });
8568
- const directory = await mkdtemp2(join11(access.runtime, "use-"));
8881
+ await mkdir6(access.runtime, { recursive: true, mode: 448 });
8882
+ const directory = await mkdtemp2(join12(access.runtime, "use-"));
8569
8883
  try {
8570
8884
  return await work(writeFilesSync(directory, access));
8571
8885
  } finally {
8572
- await rm5(directory, { recursive: true, force: true });
8886
+ await rm6(directory, { recursive: true, force: true });
8573
8887
  }
8574
8888
  }
8575
8889
  function writeFilesSync(directory, access) {
8576
8890
  mkdirSync6(directory, { recursive: true, mode: 448 });
8577
- const knownHostsFile = join11(directory, "known_hosts");
8891
+ const knownHostsFile = join12(directory, "known_hosts");
8578
8892
  writeFileSync3(knownHostsFile, access.hostKeys.map((key) => `${access.alias} ${key}
8579
8893
  `).join(""), { mode: 384 });
8580
8894
  if (access.password) {
8581
- const secret = join11(directory, "password"), askpass = join11(directory, "askpass"), ssh2 = join11(directory, "ssh");
8895
+ const secret = join12(directory, "password"), askpass = join12(directory, "askpass"), ssh2 = join12(directory, "ssh");
8582
8896
  writeFileSync3(secret, `${access.password}
8583
8897
  `, { mode: 384 });
8584
8898
  writeFileSync3(askpass, `#!/bin/sh
@@ -8587,7 +8901,7 @@ exec cat ${quote(secret)}
8587
8901
  writeFileSync3(ssh2, ["#!/bin/sh", `exec env -u SSH_AUTH_SOCK SSH_ASKPASS=${quote(askpass)} SSH_ASKPASS_REQUIRE=force DISPLAY=none ssh -o BatchMode=no -o PreferredAuthentications=keyboard-interactive,password -o PubkeyAuthentication=no -o NumberOfPasswordPrompts=1 -o ConnectTimeout=15 "$@"`, ""].join("\n"), { mode: 448 });
8588
8902
  return { knownHostsFile, ssh: ssh2 };
8589
8903
  }
8590
- const identityFile = join11(directory, "key");
8904
+ const identityFile = join12(directory, "key");
8591
8905
  writeFileSync3(identityFile, access.privateKey.endsWith("\n") ? access.privateKey : `${access.privateKey}
8592
8906
  `, { mode: 384 });
8593
8907
  return { knownHostsFile, identityFile };
@@ -8650,7 +8964,7 @@ var SshMachines = class {
8650
8964
  this.store = store;
8651
8965
  this.chats = chats;
8652
8966
  this.changed = changed;
8653
- this.runtime = join12(dirname6(store.path), "ssh");
8967
+ this.runtime = join13(dirname7(store.path), "ssh");
8654
8968
  clearRuntime(this.runtime);
8655
8969
  }
8656
8970
  runtime;
@@ -8934,7 +9248,7 @@ function directoryIn(home, value2) {
8934
9248
  // src/server/infra/storage/history-repository.ts
8935
9249
  import { DatabaseSync as DatabaseSync2 } from "node:sqlite";
8936
9250
  import { chmodSync as chmodSync3, mkdirSync as mkdirSync7 } from "node:fs";
8937
- import { dirname as dirname7 } from "node:path";
9251
+ import { dirname as dirname8 } from "node:path";
8938
9252
  var json = (value2) => value2 == null ? null : JSON.stringify(value2);
8939
9253
  var CALL = "EXISTS (SELECT 1 FROM json_each(history.message, '$.content') b WHERE json_extract(b.value, '$.type')=? AND json_extract(b.value, '$.tool_call_id')=?)";
8940
9254
  var BLOCKS = "FROM json_each(history.message,'$.content') b WHERE json_extract(b.value,'$.type')";
@@ -8946,7 +9260,7 @@ var SUMMARY = `SELECT id,timestamp,role,model,
8946
9260
  var HistoryRepository = class {
8947
9261
  db;
8948
9262
  constructor(path) {
8949
- mkdirSync7(dirname7(path), { recursive: true, mode: 448 });
9263
+ mkdirSync7(dirname8(path), { recursive: true, mode: 448 });
8950
9264
  this.db = new DatabaseSync2(path);
8951
9265
  chmodSync3(path, 384);
8952
9266
  const version = this.db.prepare("PRAGMA user_version").get().user_version;
@@ -9264,89 +9578,37 @@ var Model = class {
9264
9578
  import { posix as posix2 } from "node:path";
9265
9579
 
9266
9580
  // src/server/infra/files/workspace.ts
9267
- import { mkdir as mkdir6, writeFile as writeFile3 } from "node:fs/promises";
9268
- import { join as join13 } from "node:path";
9581
+ import { mkdir as mkdir7, writeFile as writeFile3 } from "node:fs/promises";
9582
+ import { join as join14 } from "node:path";
9269
9583
  var LocalWorkspace = class {
9270
9584
  constructor(cwd) {
9271
9585
  this.cwd = cwd;
9272
9586
  }
9273
9587
  get tempDir() {
9274
- return join13(this.cwd, "temp");
9588
+ return join14(this.cwd, "temp");
9275
9589
  }
9276
9590
  async prepare() {
9277
- await mkdir6(this.tempDir, { recursive: true, mode: 448 });
9591
+ await mkdir7(this.tempDir, { recursive: true, mode: 448 });
9278
9592
  }
9279
9593
  async writeTempFile(name3, content2, signal) {
9280
9594
  await this.prepare();
9281
- const path = join13(this.tempDir, name3);
9595
+ const path = join14(this.tempDir, name3);
9282
9596
  await writeFile3(path, content2, { encoding: "utf8", flag: "wx", mode: 384, signal });
9283
9597
  return path;
9284
9598
  }
9285
9599
  };
9286
9600
 
9287
- // src/server/infra/files/skills.ts
9288
- import { existsSync as existsSync4, readdirSync, readFileSync as readFileSync4 } from "node:fs";
9289
- import { mkdir as mkdir7, writeFile as writeFile4 } from "node:fs/promises";
9290
- import { dirname as dirname8, join as join14 } from "node:path";
9291
- import { fileURLToPath } from "node:url";
9292
-
9293
- // src/server/shared/skill.ts
9294
- var skillPath = (name3) => `skills/${name3}/SKILL.md`;
9295
-
9296
- // src/server/infra/files/skills.ts
9297
- function builtinSkills() {
9298
- const dir = skillsDir();
9299
- if (!dir) return [];
9300
- const skills = [];
9301
- for (const entry of readdirSync(dir, { withFileTypes: true })) {
9302
- if (!entry.isDirectory()) continue;
9303
- const file = join14(dir, entry.name, "SKILL.md");
9304
- if (!existsSync4(file)) continue;
9305
- const skill = parseSkill(entry.name, readFileSync4(file, "utf8"));
9306
- if (skill) skills.push(skill);
9307
- else console.warn(`Jarvy: skipped the skill in ${entry.name}: SKILL.md needs a name matching its folder and a description`);
9308
- }
9309
- return skills.sort((a, b) => a.name.localeCompare(b.name));
9310
- }
9311
- async function installSkills(workspace, skills) {
9312
- for (const skill of skills) {
9313
- const file = join14(workspace, skillPath(skill.name));
9314
- await mkdir7(dirname8(file), { recursive: true });
9315
- await writeFile4(file, skill.content, "utf8");
9316
- }
9317
- }
9318
- function skillsDir() {
9319
- let dir = dirname8(fileURLToPath(import.meta.url));
9320
- for (let depth = 0; depth < 6; depth++) {
9321
- if (existsSync4(join14(dir, "package.json"))) return existsSync4(join14(dir, "skills")) ? join14(dir, "skills") : null;
9322
- const parent = dirname8(dir);
9323
- if (parent === dir) break;
9324
- dir = parent;
9325
- }
9326
- return null;
9327
- }
9328
- function parseSkill(folder, content2) {
9329
- const head = /^---\r?\n([\s\S]*?)\r?\n---\r?\n/.exec(content2);
9330
- if (!head) return null;
9331
- const field2 = (key) => new RegExp(`^${key}:\\s*(.+)$`, "m").exec(head[1])?.[1]?.trim() ?? "";
9332
- const name3 = field2("name"), description = field2("description");
9333
- if (name3 !== folder || !/^[a-z0-9]+(-[a-z0-9]+)*$/.test(name3) || !description) return null;
9334
- return { name: name3, description, content: content2 };
9335
- }
9336
-
9337
9601
  // src/server/service/bot/workspace.ts
9338
9602
  var CHUNK = 4 * 1024 * 1024;
9339
9603
  var Workspace = class {
9340
- constructor(cwd, current, home, skills = []) {
9604
+ constructor(cwd, current, home) {
9341
9605
  this.current = current;
9342
9606
  this.home = home;
9343
- this.skills = skills;
9344
9607
  this.local = new LocalWorkspace(cwd);
9345
9608
  }
9346
9609
  local;
9347
9610
  async prepare() {
9348
9611
  await this.local.prepare();
9349
- await installSkills(this.local.cwd, this.skills);
9350
9612
  }
9351
9613
  /** A file in the temporary directory of the environment the Bot works in, by the path it sees there. */
9352
9614
  async writeTempFile(name3, content2, signal) {
@@ -10418,6 +10680,12 @@ function channel(context2, chatId) {
10418
10680
  return chat;
10419
10681
  }
10420
10682
  var agentTools = [
10683
+ tool(
10684
+ "skills.read",
10685
+ "Read one of the skills your prompt lists: its whole guide. Works wherever you work, also when your file tools can't.",
10686
+ Type11.Object({ name: Type11.String({ minLength: 1, maxLength: 80, description: "The skill's name, as your prompt lists it" }) }, { additionalProperties: false }),
10687
+ (input, context2) => ops(context2).skill(input.name)
10688
+ ),
10421
10689
  tool(
10422
10690
  "models.list",
10423
10691
  "List the models you can give a Bot: each connection with its models and their thinking levels. Only the owner adds connections, keys and logins, in Settings.",
@@ -10595,9 +10863,9 @@ function remember(map, id, definition) {
10595
10863
  var noticeName = (note) => note.type === "thread" ? `this thread's summary (version ${note.version})` : `the notice of \u300C${note.title}\u300D (${note.type}, version ${note.version})`;
10596
10864
  var unreadNoteLine = (notes) => notes.length ? `<notice_unread>${notes.map((note) => `<chat${attribute("id", note.chatId)}${attribute("title", note.title)}${attribute("type", note.type)}${attribute("version", String(note.version))}/>`).join("")}
10597
10865
  <note>You haven't read ${notes.map(noticeName).join(" or ")} yet. Read ${notes.length === 1 ? "it" : "them"} with chats.info (${notes.map((note) => `chatId: "${note.chatId}"`).join(", ")}), make sure your message follows ${notes.length === 1 ? "it" : "them"} before you send here.</note></notice_unread>` : "";
10598
- var environmentLine = (place) => place ? `<environment${attribute("name", place.name)}${attribute("directory", place.directory)}/>` : "";
10866
+ var environmentLine = (place) => place ? `<environment${attribute("name", place.name)}${attribute("directory", place.directory)}${place.problem ? attribute("problem", place.problem) : ""}/>` : "";
10599
10867
  var iso2 = (time2) => new Date(time2).toISOString().replace(".000Z", "Z");
10600
- var welcome = (owner, language) => `${owner} has just set up this chat platform and picked your model. You are their agent Bot. Write to them in a DM (messages.send with to: "${owner}") and welcome them in ${WELCOME_LANGUAGES[language]}, the language of their interface: say in a few sentences what it is and how you can help, such as building a team of Bots for their work and running the platform for them. Then ask what they want to get done: a software project, writing, research or something else.`;
10868
+ var welcome = (owner, language) => `${owner} has just set up this chat platform and picked your model. You are their agent Bot. Write to them in a DM (messages.send with to: "${owner}") and welcome them in ${WELCOME_LANGUAGES[language]}, the language of their interface: say in a few sentences what it is and how you can help, such as building a team of Bots for their work and running the platform for them. Then ask what they want to get done: a software project, writing, research or something else. If the environment at the end of this batch names a problem, also tell them in a sentence what it means for the Bots and offer to help set it up, as your setup skill describes.`;
10601
10869
  function encodeNoticeBatch(botId, notices, environment, prompt, unreadNotes) {
10602
10870
  if (!notices.length && !prompt) throw new Error("Empty notice batch");
10603
10871
  const chats = /* @__PURE__ */ new Map(), members = /* @__PURE__ */ new Map();
@@ -10745,8 +11013,8 @@ var BotInbox = class {
10745
11013
  }
10746
11014
  // Its conversation was compacted (A73): the open threads it follows, as they are now.
10747
11015
  case "followed": {
10748
- const open2 = this.chats.threads.openFollowed(this.botId, FOLLOWED_SHOWN);
10749
- return open2.total ? { kind: "followed", id, total: open2.total, threads: open2.threads, createdAt } : null;
11016
+ const open3 = this.chats.threads.openFollowed(this.botId, FOLLOWED_SHOWN);
11017
+ return open3.total ? { kind: "followed", id, total: open3.total, threads: open3.threads, createdAt } : null;
10750
11018
  }
10751
11019
  }
10752
11020
  }
@@ -11017,7 +11285,7 @@ var AGENT_ROLE = `
11017
11285
  ## Your role
11018
11286
  You are the owner's agent Bot. Besides working like any Bot, you help them set up and run Jarvy itself.
11019
11287
  Help a new owner get started: find out what they want to get done, suggest a team of Bots for it, then create and set up those Bots.
11020
- The chat gateway lists tools only you have: models.list, bots.create, bots.update, bots.start, bots.stop and bots.usage. bots.delete, chats.delete, chats.removeMembers and sections.delete first ask the owner on a card, and delete nothing unless they confirm.
11288
+ The chat gateway lists tools only you have: skills.read, models.list, bots.create, bots.update, bots.start, bots.stop and bots.usage. bots.delete, chats.delete, chats.removeMembers and sections.delete first ask the owner on a card, and delete nothing unless they confirm.
11021
11289
  Other Bots may ask you to do these things for them. Do the ordinary ones; deleting always waits for the owner's confirmation.
11022
11290
  You can't add model connections, keys or logins. The owner does that in Settings, so point them there when a model is missing.
11023
11291
  `;
@@ -11035,7 +11303,7 @@ You are in a group chat with people and other Bots. You take part in direct mess
11035
11303
  Here your name is ${bot.name}, and your description is: ${bot.description}
11036
11304
  ` },
11037
11305
  { name: "Your role", text: bot.agent ? AGENT_ROLE : "" },
11038
- { name: "Skills", text: skillsSection(setup, skills) },
11306
+ { name: "Skills", text: skillsSection(skills) },
11039
11307
  { name: "Rules", text: `
11040
11308
  ## Language
11041
11309
  Write to people in the language they write to you in. In a channel or thread, use the language its conversation is held in.
@@ -11079,16 +11347,63 @@ Your file and command tools run on your own computer: a Linux container only you
11079
11347
  You can also work in other, external workspaces. environments.list shows the ones your owner has given you; environments.switch moves you to one, and from then on your file and command tools run there until you switch back. Your chat tools are the same either way. To work on another machine over SSH, ask your owner to add it with environments.request; they type its password on that card, not in the chat. Each batch of messages ends with the environment you are in now.
11080
11348
  `;
11081
11349
  }
11082
- function skillsSection(setup, skills) {
11350
+ function skillsSection(skills) {
11083
11351
  if (!skills.length) return "";
11084
- const base = setup.home().cwd;
11085
11352
  return `
11086
11353
  ## Skills
11087
- Skills are guides that come with this chat platform for kinds of work you help with. When a task matches one, read its file before you start, and follow it. They are suggestions for the owner: propose a plan from them and act once the owner agrees. They are kept in your workspace and written again each time you start, so changes to them don't last.
11088
- ${skills.map((skill) => `- ${skill.name}: ${skill.description} File: ${base}/${skillPath(skill.name)}`).join("\n")}
11354
+ Skills are guides that come with this chat platform for kinds of work you help with. When a task matches one, read it with skills.read before you start, and follow it. They are suggestions for the owner: propose a plan from them and act once the owner agrees.
11355
+ ${skills.map((skill) => `- ${skill.name}: ${skill.description}`).join("\n")}
11089
11356
  `;
11090
11357
  }
11091
11358
 
11359
+ // src/server/infra/files/skills.ts
11360
+ import { existsSync as existsSync5, readdirSync, readFileSync as readFileSync5 } from "node:fs";
11361
+ import { rm as rm7, rmdir } from "node:fs/promises";
11362
+ import { dirname as dirname10, join as join16 } from "node:path";
11363
+ import { fileURLToPath } from "node:url";
11364
+ function builtinSkills() {
11365
+ const dir = skillsDir();
11366
+ if (!dir) return [];
11367
+ const skills = [];
11368
+ for (const entry of readdirSync(dir, { withFileTypes: true })) {
11369
+ if (!entry.isDirectory()) continue;
11370
+ const file = join16(dir, entry.name, "SKILL.md");
11371
+ if (!existsSync5(file)) continue;
11372
+ const skill = parseSkill(entry.name, readFileSync5(file, "utf8"));
11373
+ if (skill) skills.push(skill);
11374
+ else console.warn(`Jarvy: skipped the skill in ${entry.name}: SKILL.md needs a name matching its folder and a description`);
11375
+ }
11376
+ return skills.sort((a, b) => a.name.localeCompare(b.name));
11377
+ }
11378
+ async function removeSkillCopies(workspace, skills) {
11379
+ const root = join16(workspace, "skills");
11380
+ for (const skill of skills) {
11381
+ await rm7(join16(root, skill.name, "SKILL.md"), { force: true });
11382
+ await rmdir(join16(root, skill.name)).catch(() => {
11383
+ });
11384
+ }
11385
+ await rmdir(root).catch(() => {
11386
+ });
11387
+ }
11388
+ function skillsDir() {
11389
+ let dir = dirname10(fileURLToPath(import.meta.url));
11390
+ for (let depth = 0; depth < 6; depth++) {
11391
+ if (existsSync5(join16(dir, "package.json"))) return existsSync5(join16(dir, "skills")) ? join16(dir, "skills") : null;
11392
+ const parent = dirname10(dir);
11393
+ if (parent === dir) break;
11394
+ dir = parent;
11395
+ }
11396
+ return null;
11397
+ }
11398
+ function parseSkill(folder, content2) {
11399
+ const head = /^---\r?\n([\s\S]*?)\r?\n---\r?\n/.exec(content2);
11400
+ if (!head) return null;
11401
+ const field2 = (key) => new RegExp(`^${key}:\\s*(.+)$`, "m").exec(head[1])?.[1]?.trim() ?? "";
11402
+ const name3 = field2("name"), description = field2("description");
11403
+ if (name3 !== folder || !/^[a-z0-9]+(-[a-z0-9]+)*$/.test(name3) || !description) return null;
11404
+ return { name: name3, description, content: content2 };
11405
+ }
11406
+
11092
11407
  // src/server/shared/live.ts
11093
11408
  var LIVE_STEPS = 300;
11094
11409
  var LIVE_TEXT = 4e3;
@@ -11344,11 +11659,12 @@ var LiveTurns = class {
11344
11659
  // src/server/service/bot/agent.ts
11345
11660
  var same2 = (a, b) => a.trim().replace(/^@/, "").toLocaleLowerCase() === b.trim().toLocaleLowerCase();
11346
11661
  var AgentOps = class {
11347
- constructor(bots, store, chats, deliveries) {
11662
+ constructor(bots, store, chats, deliveries, skills = []) {
11348
11663
  this.bots = bots;
11349
11664
  this.store = store;
11350
11665
  this.chats = chats;
11351
11666
  this.deliveries = deliveries;
11667
+ this.skills = skills;
11352
11668
  }
11353
11669
  get owner() {
11354
11670
  return this.store.account.userMemberId();
@@ -11360,6 +11676,12 @@ var AgentOps = class {
11360
11676
  if (!bot) throw new ChatError(404, `No Bot named ${name3} (see bots.list)`);
11361
11677
  return bot;
11362
11678
  }
11679
+ /** A skill's guide (A55), read from Jarvy itself so it can be read wherever the agent works and when its tools can't. */
11680
+ skill(name3) {
11681
+ const skill = this.skills.find((item) => item.name === name3.trim().toLowerCase());
11682
+ if (!skill) throw new ChatError(404, `No skill named ${name3}; there are ${this.skills.map((item) => item.name).join(", ") || "none"}`);
11683
+ return { name: skill.name, description: skill.description, content: skill.content.replace(/^---\r?\n[\s\S]*?\r?\n---\r?\n/, "").trim() };
11684
+ }
11363
11685
  /** Connected models and their thinking levels; never credentials. */
11364
11686
  models() {
11365
11687
  return this.store.connections.list().filter((connection) => connection.models.length).map((connection) => ({
@@ -11679,7 +12001,7 @@ function text4(value2, label, max) {
11679
12001
  var BotService = class {
11680
12002
  constructor(deps) {
11681
12003
  this.deps = deps;
11682
- this.agent = new AgentOps(this, deps.store, deps.chats, deps.deliveries);
12004
+ this.agent = new AgentOps(this, deps.store, deps.chats, deps.deliveries, this.skills);
11683
12005
  this.computers = new Computers({ store: deps.store, changed: () => this.listChanged() });
11684
12006
  this.computers.adopt(deps.store.bots.list().map((record3) => record3.id));
11685
12007
  this.ports = new PublishedPorts(deps.store, this.computers, () => deps.listening?.() ?? null);
@@ -11710,9 +12032,11 @@ var BotService = class {
11710
12032
  let bot = this.bots.get(id);
11711
12033
  if (bot) return bot;
11712
12034
  const { store, chats, files, credentials } = this.deps, live = this.live(id), skills = id === AGENT_BOT_ID ? this.skills : [];
12035
+ if (skills.length) void removeSkillCopies(record3.workspacePath, skills).catch(() => {
12036
+ });
11713
12037
  const here = () => {
11714
- const place = this.environments.here(id, record3.workspacePath);
11715
- return { name: place.name, directory: place.directory };
12038
+ const place = this.environments.here(id, record3.workspacePath), problem = place.kind === "sandbox" ? this.computers.dockerProblem : null;
12039
+ return { name: place.name, directory: place.directory, ...problem ? { problem: `${problem}, so your file and command tools don't work here` } : {} };
11716
12040
  };
11717
12041
  const runtime = new AgentRuntime(new BotInbox(id, store.runtime, chats, files, store.tasks, here), (setup, model, reference, activity) => (this.deps.factory ?? PiSession.create)({
11718
12042
  store,
@@ -11737,8 +12061,8 @@ var BotService = class {
11737
12061
  const workspace = new Workspace(record3.workspacePath, () => this.environments.current(id, record3.workspacePath), () => {
11738
12062
  const place = this.environments.default(id, record3.workspacePath);
11739
12063
  return { kind: "computer", cwd: place.directory };
11740
- }, skills);
11741
- const memory = new Memory(join16(dirname10(record3.workspacePath), "history.db"));
12064
+ });
12065
+ const memory = new Memory(join17(dirname11(record3.workspacePath), "history.db"));
11742
12066
  bot = new Bot(id, {
11743
12067
  identity: new Identity(id, store),
11744
12068
  model: new Model(id, store, resolveThinking),
@@ -11870,11 +12194,12 @@ var BotService = class {
11870
12194
  if (!record3) throw new RuntimeFault("bot_missing", "Bot not found", 404);
11871
12195
  return record3;
11872
12196
  }
11873
- /** Starting (or resetting, which starts afresh) returns at once: the Bot's state follows as it changes. */
12197
+ /** Starting (or resetting, which starts afresh, or restarting, which a VM's new size needs) returns at once: the
12198
+ * Bot's state follows as it changes. */
11874
12199
  async computer(id, action) {
11875
12200
  this.describe(id);
11876
- if (action === "stop") await this.computers.stop(id);
11877
- else {
12201
+ if (action === "stop" || action === "restart") await this.computers.stop(id);
12202
+ if (action !== "stop") {
11878
12203
  if (action === "reset") {
11879
12204
  await this.computers.reset(id);
11880
12205
  this.ports.unpublishAll(id);
@@ -11884,6 +12209,24 @@ var BotService = class {
11884
12209
  }
11885
12210
  return this.describe(id);
11886
12211
  }
12212
+ /** Makes the sandbox a container or a VM (A85) and starts the new one; what ran there is gone, and so are the links
12213
+ * to it. */
12214
+ async setComputerType(id, type) {
12215
+ this.describe(id);
12216
+ if (type !== "container" && type !== "vm") throw new ValidationError("type is container or vm", "type_invalid");
12217
+ await this.computers.setType(id, type);
12218
+ this.ports.unpublishAll(id);
12219
+ void this.computers.ensure(id).catch(() => {
12220
+ });
12221
+ return this.describe(id);
12222
+ }
12223
+ /** Whether VM sandboxes can be used here (A85), as last checked; asking the first time, or `again`, starts a check,
12224
+ * `checking` until it is done. */
12225
+ vmSupport(again = false) {
12226
+ void this.computers.checkVms(again).catch(() => {
12227
+ });
12228
+ return this.computers.vmSupport;
12229
+ }
11887
12230
  /** What the sandbox may use at most (A75); returns the Bot with them. */
11888
12231
  async setComputerLimits(id, limits2) {
11889
12232
  this.describe(id);
@@ -12000,7 +12343,7 @@ var BotService = class {
12000
12343
  if (!this.deps.store.bots.delete(id)) throw new RuntimeFault("bot_missing", "Bot not found", 404);
12001
12344
  this.ports.changed();
12002
12345
  await this.deps.chats.attachments.clean();
12003
- if (!keepFiles) await rm6(dirname10(workspacePath), { recursive: true, force: true });
12346
+ if (!keepFiles) await rm8(dirname11(workspacePath), { recursive: true, force: true });
12004
12347
  }
12005
12348
  async close() {
12006
12349
  if (this.closed) return;
@@ -12368,7 +12711,7 @@ var Application = class _Application {
12368
12711
  retries: 0,
12369
12712
  stale: 3e4,
12370
12713
  update: 1e4,
12371
- lockfilePath: join17(root, ".runtime.lock"),
12714
+ lockfilePath: join18(root, ".runtime.lock"),
12372
12715
  onCompromised: () => {
12373
12716
  console.error("[runtime] data lock compromised");
12374
12717
  options.onCompromised?.();
@@ -12412,10 +12755,10 @@ var Application = class _Application {
12412
12755
 
12413
12756
  // src/server/daemon.ts
12414
12757
  import { spawn as spawn3 } from "node:child_process";
12415
- import { closeSync as closeSync2, mkdirSync as mkdirSync8, openSync as openSync2, readFileSync as readFileSync5, rmSync as rmSync3, writeFileSync as writeFileSync4 } from "node:fs";
12416
- import { join as join18, resolve as resolve4 } from "node:path";
12417
- var pidPath = (dir) => join18(resolve4(dir), "jarvy.pid");
12418
- var logPath = (dir) => join18(resolve4(dir), "jarvy.log");
12758
+ import { closeSync as closeSync2, mkdirSync as mkdirSync8, openSync as openSync2, readFileSync as readFileSync6, rmSync as rmSync3, writeFileSync as writeFileSync4 } from "node:fs";
12759
+ import { join as join19, resolve as resolve4 } from "node:path";
12760
+ var pidPath = (dir) => join19(resolve4(dir), "jarvy.pid");
12761
+ var logPath = (dir) => join19(resolve4(dir), "jarvy.log");
12419
12762
  function writePidFile(dir, url) {
12420
12763
  const record3 = { pid: process.pid, startToken: startToken(process.pid), url, startedAt: Date.now() };
12421
12764
  writeFileSync4(pidPath(dir), JSON.stringify(record3), { mode: 384 });
@@ -12425,14 +12768,14 @@ function removePidFile(dir) {
12425
12768
  }
12426
12769
  function readRecord(dir) {
12427
12770
  try {
12428
- return JSON.parse(readFileSync5(pidPath(dir), "utf8"));
12771
+ return JSON.parse(readFileSync6(pidPath(dir), "utf8"));
12429
12772
  } catch {
12430
12773
  return null;
12431
12774
  }
12432
12775
  }
12433
12776
  function startToken(pid) {
12434
12777
  try {
12435
- return readFileSync5(`/proc/${pid}/stat`, "utf8").split(") ").pop().split(" ")[19] ?? "";
12778
+ return readFileSync6(`/proc/${pid}/stat`, "utf8").split(") ").pop().split(" ")[19] ?? "";
12436
12779
  } catch {
12437
12780
  return "";
12438
12781
  }
@@ -12475,7 +12818,7 @@ function startBackground(script, runArgs, dir) {
12475
12818
  }
12476
12819
  function lastLogLine(dir) {
12477
12820
  try {
12478
- return readFileSync5(logPath(dir), "utf8").slice(-4e3).trim().split("\n").reverse().find((line) => line.startsWith("Jarvy: "))?.slice(7) ?? null;
12821
+ return readFileSync6(logPath(dir), "utf8").slice(-4e3).trim().split("\n").reverse().find((line) => line.startsWith("Jarvy: "))?.slice(7) ?? null;
12479
12822
  } catch {
12480
12823
  return null;
12481
12824
  }
@@ -12639,8 +12982,8 @@ async function main() {
12639
12982
  try {
12640
12983
  if (!store.account.isInitialized()) await initialize(store);
12641
12984
  const { host, port, url } = listenOptions(args);
12642
- const webRoot = join19(dirname11(fileURLToPath2(import.meta.url)), "../web");
12643
- if (!existsSync5(join19(webRoot, "index.html"))) throw new Error("Web build missing. Run npm run build first");
12985
+ const webRoot = join20(dirname12(fileURLToPath2(import.meta.url)), "../web");
12986
+ if (!existsSync6(join20(webRoot, "index.html"))) throw new Error("Web build missing. Run npm run build first");
12644
12987
  const tls = app.connection.prepare(host, port);
12645
12988
  const server = createJarvyServer(app, webRoot, tls);
12646
12989
  const published = new PublishedListeners(app, tls, host, port);