@kvbox/server 0.6.13 → 0.6.15

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.
@@ -1781,16 +1781,9 @@ function hasDiskRoom(d, sizeGib) {
1781
1781
  }
1782
1782
  var VCPU_OVERSUBSCRIPTION = 2;
1783
1783
 
1784
- // src/core/volumes.ts
1785
- import { existsSync as existsSync5, statSync } from "node:fs";
1786
- import { lstat, mkdir as mkdir2, readdir as readdir3, rm as rm3, statfs, symlink, truncate, writeFile as writeFile3 } from "node:fs/promises";
1787
-
1788
- // ../shared/src/api.ts
1789
- import { z as z6 } from "zod";
1790
-
1791
1784
  // ../shared/src/integrations.ts
1792
1785
  import { z as z5 } from "zod";
1793
- var INTEGRATION_IDS = ["github"];
1786
+ var INTEGRATION_IDS = ["github", "claude"];
1794
1787
  var IntegrationIdSchema = z5.enum(INTEGRATION_IDS);
1795
1788
  var INTEGRATION_KINDS = ["oauth", "app", "api-key", "subscription"];
1796
1789
  var IntegrationKindSchema = z5.enum(INTEGRATION_KINDS);
@@ -1806,8 +1799,11 @@ var IntegrationCatalogEntrySchema = z5.object({
1806
1799
  id: z5.string(),
1807
1800
  title: z5.string(),
1808
1801
  description: z5.string(),
1809
- /** How Connect works: `oauth` sends the browser to the provider's consent screen. */
1810
- connect: z5.enum(["oauth"]),
1802
+ /**
1803
+ * How Connect works: `oauth` sends the browser to the provider's consent screen; `paste` takes a credential
1804
+ * the user pastes (Claude Code's). A plain string, like `id`: a released CLI must still read a catalog with a way it does not know.
1805
+ */
1806
+ connect: z5.string(),
1811
1807
  /** What a user's slug and an org's may hold; an empty list: that kind of slug cannot connect it yet. */
1812
1808
  kinds: z5.object({ user: z5.array(IntegrationKindSchema), org: z5.array(IntegrationKindSchema) }),
1813
1809
  /** Whether this control plane is set up for it (a self-hosted one without a GitHub app is not). */
@@ -1840,6 +1836,9 @@ var IntegrationSchema = z5.object({
1840
1836
  });
1841
1837
  var IntegrationConnectSchema = z5.object({ url: z5.string() });
1842
1838
  var IntegrationCompleteRequestSchema = z5.object({ code: z5.string().min(1), state: z5.string().min(1) });
1839
+ var ClaudeConnectRequestSchema = z5.object({
1840
+ credential: z5.string().trim().min(1).max(4096).regex(/^[A-Za-z0-9_-]+$/, "a Claude Code token or an Anthropic API key has only letters, digits, - and _: paste it again on one line, with nothing inside it")
1841
+ });
1843
1842
  var IntegrationIdentitySchema = z5.object({ name: z5.string(), email: z5.string() });
1844
1843
  var CREDENTIAL_REFUSALS = ["off", "not-connected", "needs-reconnect", "unavailable"];
1845
1844
  var CredentialAnswerSchema = z5.discriminatedUnion("ok", [
@@ -1864,9 +1863,29 @@ var ConnectedAnswerSchema = z5.object({ connected: z5.boolean(), identity: Integ
1864
1863
  var GUEST_REQUEST_PORT = 53;
1865
1864
  var GuestRequestSchema = z5.object({ op: z5.literal("credential"), integration: IntegrationIdSchema });
1866
1865
  var INTEGRATIONS_BASE = "0.17.0";
1866
+ var INTEGRATION_BASES = { github: INTEGRATIONS_BASE, claude: "0.18.0" };
1867
+ function unusableOnBase(base, list) {
1868
+ const parts = (v) => v.replace(/^v/, "").split(".").map(Number);
1869
+ if (!/^v?\d+\.\d+\.\d+$/.test(base)) return [];
1870
+ const have = parts(base);
1871
+ const older = (need) => {
1872
+ const want = parts(need);
1873
+ for (let i = 0; i < 3; i++) if (have[i] !== want[i]) return have[i] < want[i];
1874
+ return false;
1875
+ };
1876
+ return list.filter((n) => {
1877
+ const need = INTEGRATION_BASES[n];
1878
+ return need !== void 0 && older(need);
1879
+ });
1880
+ }
1867
1881
  var GUEST_REQUESTS_PER_MINUTE = 60;
1868
1882
 
1883
+ // src/core/volumes.ts
1884
+ import { existsSync as existsSync5, statSync } from "node:fs";
1885
+ import { lstat, mkdir as mkdir2, readdir as readdir3, rm as rm3, statfs, symlink, truncate, writeFile as writeFile3 } from "node:fs/promises";
1886
+
1869
1887
  // ../shared/src/api.ts
1888
+ import { z as z6 } from "zod";
1870
1889
  var SizeSchema = z6.enum(SIZE_NAMES);
1871
1890
  var BoxStatusSchema = z6.enum(["running", "standby", "hibernated", "stopped", "unknown"]);
1872
1891
  var PortProtoSchema = z6.enum(["tcp", "udp"]);
@@ -2123,6 +2142,11 @@ var MachineSchema = z6.object({
2123
2142
  // version of the image the box's overlay stacks on
2124
2143
  /** The image (#87) when it is not the base one: `postgres`. `base` is then that image's version. */
2125
2144
  image: z6.string().optional(),
2145
+ /**
2146
+ * A newer version of the box's image on its host, the one `kvbox vm update` moves it onto. Only the control
2147
+ * plane fills it in (from the host's last `/capacity`); absent while the box is on the newest one, or the host did not say.
2148
+ */
2149
+ latestBase: z6.string().optional(),
2126
2150
  createdAt: z6.string(),
2127
2151
  source: z6.string(),
2128
2152
  // "base", "snapshot:<owner/name>", "fork:<owner/name>" (#79) or "backup:<id>" (#141)
@@ -2443,7 +2467,9 @@ var CapacitySchema = z6.object({
2443
2467
  /** True on a kvbox-server with `POST /machines/from-backup` (#141, `kvbox new --from-backup`); older ones omit it. */
2444
2468
  fromBackup: z6.boolean().optional(),
2445
2469
  /** True on a kvbox-server that keeps a box's `integrations` (#205); an older one drops the field without a word. */
2446
- integrations: z6.boolean().optional()
2470
+ integrations: z6.boolean().optional(),
2471
+ /** The integrations this kvbox-server knows (`hostKnowsIntegrations`). Absent with `integrations`: the first one's, `github` alone. */
2472
+ integrationIds: z6.array(z6.string()).optional()
2447
2473
  });
2448
2474
  var ImageSchema = z6.object({ name: z6.string(), version: z6.string().nullable() });
2449
2475
  var ImageIndexSchema = z6.object({
@@ -3575,7 +3601,9 @@ async function capacity(p = paths()) {
3575
3601
  // A new box on a backup's disk (#141): the control plane places `new --from-backup` only here.
3576
3602
  fromBackup: true,
3577
3603
  // A box's integrations are kept here (#205): the control plane sends a list only to such a host.
3578
- integrations: true
3604
+ integrations: true,
3605
+ // ...and which (the control plane places a box with `claude` only on a host that names it).
3606
+ integrationIds: [...INTEGRATION_IDS]
3579
3607
  };
3580
3608
  }
3581
3609
  function gb(mib) {
@@ -3960,8 +3988,10 @@ async function idle(udsPath, script, timeoutMs = 15e3) {
3960
3988
  async function runScript(udsPath, path, input, timeoutMs = 35e3) {
3961
3989
  return (await agentCall(udsPath, { op: "script", path, input: input ?? null }, timeoutMs))["output"];
3962
3990
  }
3963
- function setIntegrations(udsPath, list, identity, timeoutMs) {
3964
- return agentCall(udsPath, { op: "integrations", list: [...list], ...identity ? { identity } : {} }, timeoutMs);
3991
+ async function setIntegrations(udsPath, list, identity, timeoutMs) {
3992
+ const reply = await agentCall(udsPath, { op: "integrations", list: [...list], ...identity ? { identity } : {} }, timeoutMs);
3993
+ const known = reply["known"];
3994
+ return { known: Array.isArray(known) ? known.filter((n) => typeof n === "string") : ["github"] };
3965
3995
  }
3966
3996
  async function startRun(udsPath, id, command, timeoutSec, timeoutMs = 15e3) {
3967
3997
  return (await agentCall(udsPath, { op: "run", id, command, timeoutSec }, timeoutMs))["started"] === true;
@@ -4192,9 +4222,11 @@ async function answerGuest(ref, request7, p, now = Date.now()) {
4192
4222
  var told = /* @__PURE__ */ new Map();
4193
4223
  var sameIdentity = (a, b) => a?.name === b?.name && a?.email === b?.email;
4194
4224
  async function tellGuest(box, vsock, identity) {
4195
- await setIntegrations(vsock, box.integrations ?? [], identity, AGENT_REPLY_MS);
4225
+ const { known } = await setIntegrations(vsock, box.integrations ?? [], identity, AGENT_REPLY_MS);
4196
4226
  told.set(box.id, identity);
4227
+ return (box.integrations ?? []).filter((n) => !known.includes(n));
4197
4228
  }
4229
+ var tooOldFor = (box, names2) => `its image is from before base v${names2.map((n) => INTEGRATION_BASES[n] ?? INTEGRATIONS_BASE).sort().at(-1)}, which cannot use ${names2.join(", ")} (kvbox vm update ${shown(box)} moves it onto the current one)`;
4198
4230
  async function identityOf(box, p) {
4199
4231
  if (!(box.integrations ?? []).includes("github")) return void 0;
4200
4232
  const cp = (await loadConfig(p)).controlPlane;
@@ -4212,26 +4244,45 @@ async function tellCurrent(ref, vsock, p, identity) {
4212
4244
  const done = await withBoxLock(ref, async () => {
4213
4245
  const box = await readMachineMeta(ref, p);
4214
4246
  if (!sameList(box.integrations, asked2.integrations)) return false;
4215
- await tellGuest(box, vsock, who);
4247
+ const unusable = await tellGuest(box, vsock, who);
4248
+ if (unusable.length > 0) log.warn(`box ${shown(box)} has integrations it cannot use: ${tooOldFor(box, unusable)}`);
4216
4249
  return true;
4217
4250
  });
4218
4251
  if (done) return true;
4219
4252
  }
4220
4253
  }
4254
+ function assertImageCanUse(box, list, before) {
4255
+ if (box.image !== void 0) return;
4256
+ const unusable = unusableOnBase(box.base, (list ?? []).filter((n) => !(before ?? []).includes(n)));
4257
+ if (unusable.length > 0) throw new KvboxError(`box ${shown(box)} cannot use ${unusable.join(", ")} yet: ${tooOldFor(box, unusable)}`);
4258
+ }
4221
4259
  var outdated = (box) => `its image is from before base v${INTEGRATIONS_BASE}, which has no integrations (kvbox vm update ${shown(box)} moves it onto the current one)`;
4222
- async function applyIntegrations(box, vsock, p) {
4260
+ async function applyIntegrations(box, vsock, p, before) {
4223
4261
  if (!isCloudBox(box.owner)) return;
4262
+ let unusable;
4224
4263
  try {
4264
+ const prior = told.has(box.id) ? { who: told.get(box.id) } : null;
4225
4265
  const who = await identityOf(box, p);
4226
- await tellGuest(box, vsock, who ?? void 0);
4227
- if (who === null) {
4266
+ unusable = await tellGuest(box, vsock, who ?? void 0);
4267
+ let unsure = who === null;
4268
+ if (unusable.length > 0) {
4269
+ if (prior) told.set(box.id, prior.who);
4270
+ else told.delete(box.id);
4271
+ const kept = { ...box, integrations: before && before.length > 0 ? [...before] : void 0 };
4272
+ const keptWho = await identityOf(kept, p);
4273
+ unsure = keptWho === null;
4274
+ await tellGuest(kept, vsock, keptWho ?? void 0).catch(() => unsure = true);
4275
+ }
4276
+ if (unsure) {
4228
4277
  told.delete(box.id);
4229
4278
  if (box.pid !== null) void tellIntegrationsWhenUp(box, box.pid, vsock, p);
4230
4279
  }
4231
4280
  } catch (err) {
4232
4281
  if (!isUnknownOp(err)) return void log.warn(`box ${shown(box)} was not told its integrations: ${err.message}`);
4233
4282
  if ((box.integrations ?? []).length > 0) throw new KvboxError(`box ${shown(box)} cannot use integrations yet: ${outdated(box)}`);
4283
+ return;
4234
4284
  }
4285
+ if (unusable.length > 0) throw new KvboxError(`box ${shown(box)} cannot use ${unusable.join(", ")} yet: ${tooOldFor(box, unusable)}`);
4235
4286
  }
4236
4287
  var BOOT_WAIT_MS = 12e4;
4237
4288
  var WHO_RETRY_MS = 3e3;
@@ -4700,8 +4751,9 @@ function autoHibernateFor(req, cfg, manifest = {}) {
4700
4751
  return cfg.autoHibernateSec || void 0;
4701
4752
  }
4702
4753
  var integrationsOf = (list) => list && list.length > 0 ? [...list] : void 0;
4703
- function requestedIntegrations(list) {
4754
+ function requestedIntegrations(list, on) {
4704
4755
  assertKnownIntegrations(list, "this host's kvbox-server", "it needs updating first");
4756
+ if (on) assertImageCanUse(on, list);
4705
4757
  return integrationsOf(list);
4706
4758
  }
4707
4759
  var inheritedIntegrations = (list, sourceOwner, owner) => sourceOwner === owner ? integrationsOf(list) : void 0;
@@ -5011,7 +5063,8 @@ var setMachine = locked(async function setMachine2(ref, req, p = paths()) {
5011
5063
  if (req.idleCheck !== void 0) next.idleCheck = req.idleCheck || void 0;
5012
5064
  if (req.integrations !== void 0) {
5013
5065
  next.integrations = requestedIntegrations(req.integrations);
5014
- if (statusOf(meta, p) === "running") await applyIntegrations(next, p.machine(meta.owner, meta.name).vsock, p);
5066
+ if (statusOf(meta, p) === "running") await applyIntegrations(next, p.machine(meta.owner, meta.name).vsock, p, meta.integrations);
5067
+ else assertImageCanUse(meta, next.integrations, meta.integrations);
5015
5068
  }
5016
5069
  await writeMachineMeta(next, p);
5017
5070
  return toMachine(next, p);
@@ -5069,7 +5122,7 @@ async function restoreFrom(from, owner, req, cfg, p) {
5069
5122
  await placeholderDrives(m.dir, snap.volumes ?? []);
5070
5123
  return { vmstate: s.vmstate, mem: s.mem, label: shown(from), volumes: snap.volumes };
5071
5124
  },
5072
- { source: `snapshot:${refKey(from)}`, autoHibernateSec: autoHibernateFor(req, cfg, manifest), idleCheck: manifest.idle, url: maskedUrl(manifest, vars), integrations: req.integrations !== void 0 ? requestedIntegrations(req.integrations) : inheritedIntegrations(snap.integrations, from.owner, owner) },
5125
+ { source: `snapshot:${refKey(from)}`, autoHibernateSec: autoHibernateFor(req, cfg, manifest), idleCheck: manifest.idle, url: maskedUrl(manifest, vars), integrations: req.integrations !== void 0 ? requestedIntegrations(req.integrations, { owner, name, base: snap.base, image: snap.image }) : inheritedIntegrations(snap.integrations, from.owner, owner) },
5073
5126
  cfg,
5074
5127
  p
5075
5128
  );
@@ -5126,7 +5179,7 @@ async function createMachine(req, owner, p = paths(), from) {
5126
5179
  } else await createOverlay(m.overlay, size2.diskGib);
5127
5180
  if (vars.length > 0) await writeBoxEnv({ owner, name }, vars, p);
5128
5181
  },
5129
- { source: from ? `backup:${from.id}` : "base", autoHibernateSec, idleCheck: manifest.idle, url: maskedUrl(manifest, vars), integrations: requestedIntegrations(req.integrations) },
5182
+ { source: from ? `backup:${from.id}` : "base", autoHibernateSec, idleCheck: manifest.idle, url: maskedUrl(manifest, vars), integrations: requestedIntegrations(req.integrations, { owner, name, base: base.version, image }) },
5130
5183
  cfg,
5131
5184
  p
5132
5185
  );
@@ -6940,7 +6993,7 @@ function versionOf(srcFileUrl, injected) {
6940
6993
  }
6941
6994
 
6942
6995
  // src/version.ts
6943
- var VERSION = versionOf(import.meta.url, true ? "0.6.13" : void 0);
6996
+ var VERSION = versionOf(import.meta.url, true ? "0.6.15" : void 0);
6944
6997
 
6945
6998
  // src/caller.ts
6946
6999
  import { createHash as createHash3, timingSafeEqual } from "node:crypto";