@abelxiaoxing/cadence 2.0.35 → 2.0.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/README.de.md +4 -4
  2. package/README.es.md +4 -4
  3. package/README.fr.md +4 -4
  4. package/README.ja.md +3 -4
  5. package/README.ko.md +4 -4
  6. package/README.md +4 -4
  7. package/README.pt-BR.md +4 -4
  8. package/README.ru.md +5 -4
  9. package/README.zh-CN.md +4 -4
  10. package/THIRD_PARTY_NOTICES.md +9 -1
  11. package/docs/conversation-mode.md +21 -2
  12. package/docs/reuse-and-distribution.md +1 -1
  13. package/docs/service-management.md +35 -9
  14. package/package.json +18 -18
  15. package/src/abort-scope.ts +47 -0
  16. package/src/agent-browser/labeled-rule.ts +74 -0
  17. package/src/agent-browser/session-navigator.ts +187 -0
  18. package/src/agent-browser/source.ts +170 -0
  19. package/src/agent-browser/transcript-content.ts +442 -0
  20. package/src/agent-browser/transcript-pane.ts +324 -0
  21. package/src/agent-panel-model.ts +4 -248
  22. package/src/agent-panel.ts +138 -432
  23. package/src/coordinator-tools.mjs +42 -14
  24. package/src/image-content.mjs +7 -28
  25. package/src/native-build.ts +1 -1
  26. package/src/research-scan.mjs +393 -0
  27. package/src/research-tools.mjs +406 -342
  28. package/src/sdk-worker.mjs +40 -55
  29. package/src/service/contracts/core.ts +0 -1
  30. package/src/service/contracts/workbench.ts +10 -16
  31. package/src/service-cli.mjs +328 -106
  32. package/src/service-host.mjs +545 -404
  33. package/src/service-http-client.ts +55 -50
  34. package/src/worker-public.mjs +1 -2
  35. package/src/worker-toolkit.mjs +110 -0
  36. package/src/worker-workspace.d.mts +13 -0
  37. package/src/worker-workspace.mjs +48 -0
  38. package/web/dist/assets/cadence.css +1 -1
  39. package/web/dist/assets/cadence.js +17 -17
  40. package/web/dist/manifest.json +7 -7
@@ -2,11 +2,11 @@
2
2
  // Generated by scripts/build-service.mjs; edit the entry and source modules.
3
3
 
4
4
  // src/service/cli.ts
5
- import { spawn as spawn2 } from "node:child_process";
5
+ import { spawn as spawn3 } from "node:child_process";
6
6
  import { mkdirSync as mkdirSync6 } from "node:fs";
7
7
  import { request } from "node:http";
8
8
  import { homedir as homedir7 } from "node:os";
9
- import { dirname as dirname13, join as join18, resolve as resolve7 } from "node:path";
9
+ import { dirname as dirname13, join as join19, resolve as resolve7 } from "node:path";
10
10
  import { fileURLToPath as fileURLToPath2 } from "node:url";
11
11
 
12
12
  // src/install-config.mjs
@@ -2668,6 +2668,20 @@ function serviceEnvironment(s) {
2668
2668
  out.push(["XDG_STATE_HOME", state]);
2669
2669
  if (s.env.PI_CODING_AGENT_DIR)
2670
2670
  out.push(["PI_CODING_AGENT_DIR", agentDirectory(s)]);
2671
+ for (const key of [
2672
+ "XDG_CONFIG_HOME",
2673
+ "BUN_INSTALL",
2674
+ "BUN_INSTALL_GLOBAL_DIR",
2675
+ "BUN_INSTALL_BIN",
2676
+ "npm_config_prefix",
2677
+ "NPM_CONFIG_PREFIX",
2678
+ "npm_config_userconfig",
2679
+ "NPM_CONFIG_USERCONFIG"
2680
+ ]) {
2681
+ const value = s.env[key];
2682
+ if (value && isAbsolute3(value))
2683
+ out.push([key, value]);
2684
+ }
2671
2685
  return out;
2672
2686
  }
2673
2687
  function checked(value) {
@@ -5924,14 +5938,224 @@ No project or default workspace was created.
5924
5938
  `);
5925
5939
  }
5926
5940
 
5941
+ // src/service/installation.ts
5942
+ import { spawn } from "node:child_process";
5943
+ import {
5944
+ accessSync,
5945
+ constants as constants4,
5946
+ existsSync as existsSync3,
5947
+ lstatSync as lstatSync8,
5948
+ mkdtempSync,
5949
+ readFileSync as readFileSync3,
5950
+ realpathSync as realpathSync5,
5951
+ rmSync as rmSync4,
5952
+ writeFileSync as writeFileSync4
5953
+ } from "node:fs";
5954
+ import { tmpdir } from "node:os";
5955
+ import { basename as basename2, dirname as dirname10, isAbsolute as isAbsolute7, join as join14 } from "node:path";
5956
+ var PACKAGE = "@abelxiaoxing/cadence";
5957
+ var VIEW_MS = 30000;
5958
+ var INSTALL_MS = 15 * 60000;
5959
+ function packageCommands(s) {
5960
+ const command = (name) => (args, show = false, options) => new Promise((resolve7) => {
5961
+ const child = spawn(name, args, {
5962
+ cwd: options?.cwd ?? s.home,
5963
+ env: { ...s.env, HOME: s.home, ...options?.env },
5964
+ stdio: ["ignore", show ? 2 : "pipe", show ? 2 : "ignore"],
5965
+ timeout: show ? INSTALL_MS : VIEW_MS,
5966
+ windowsHide: true
5967
+ });
5968
+ let stdout = "";
5969
+ child.stdout?.setEncoding("utf8").on("data", (chunk) => {
5970
+ if (stdout.length < 1 << 20)
5971
+ stdout += chunk;
5972
+ });
5973
+ child.once("error", () => resolve7({ status: null, stdout: "" }));
5974
+ child.once("close", (status) => resolve7({ status, stdout }));
5975
+ });
5976
+ return { npm: command("npm"), bun: command("bun") };
5977
+ }
5978
+ var unsupported = () => ({
5979
+ route: "unsupported-install"
5980
+ });
5981
+ function sameCopy(modules, root) {
5982
+ try {
5983
+ const path2 = join14(modules, ...PACKAGE.split("/"));
5984
+ return !lstatSync8(path2).isSymbolicLink() && realpathSync5(path2) === realpathSync5(root) && realpathSync5(path2) === join14(realpathSync5(modules), ...PACKAGE.split("/"));
5985
+ } catch {
5986
+ return false;
5987
+ }
5988
+ }
5989
+ function prefixManager(prefix) {
5990
+ const npm = existsSync3(join14(prefix, "package-lock.json")) || existsSync3(join14(prefix, "npm-shrinkwrap.json"));
5991
+ const bun = existsSync3(join14(prefix, "bun.lock")) || existsSync3(join14(prefix, "bun.lockb"));
5992
+ return npm === bun ? undefined : bun ? "bun" : "npm";
5993
+ }
5994
+ function writable(installation, modules, root) {
5995
+ if (installation.manager === "bun" && [".npmrc", "bunfig.toml", ".bunfig.toml"].some((name) => existsSync3(join14(installation.prefix, name))))
5996
+ return unsupported();
5997
+ try {
5998
+ for (const path2 of [installation.prefix, modules, dirname10(root), root])
5999
+ accessSync(path2, constants4.W_OK);
6000
+ for (const name of [
6001
+ "package.json",
6002
+ "package-lock.json",
6003
+ "npm-shrinkwrap.json",
6004
+ "bun.lock",
6005
+ "bun.lockb"
6006
+ ])
6007
+ if (existsSync3(join14(installation.prefix, name)))
6008
+ accessSync(join14(installation.prefix, name), constants4.W_OK);
6009
+ return installation;
6010
+ } catch {
6011
+ return { route: "not-writable" };
6012
+ }
6013
+ }
6014
+ async function installation(s, commands) {
6015
+ const agent = agentDirectory(s), piPrefix = join14(agent, "npm");
6016
+ if (sameCopy(join14(piPrefix, "node_modules"), s.root)) {
6017
+ try {
6018
+ const settings2 = JSON.parse(readFileSync3(join14(agent, "settings.json"), "utf8"));
6019
+ if (!settings2 || !Array.isArray(settings2.packages))
6020
+ return unsupported();
6021
+ const sources = settings2.packages.map((p) => typeof p === "string" ? p : p && typeof p === "object" && ("source" in p) ? p.source : undefined).filter((source) => typeof source === "string" && (source === `npm:${PACKAGE}` || source.startsWith(`npm:${PACKAGE}@`)));
6022
+ if (sources.length !== 1)
6023
+ return unsupported();
6024
+ if (sources[0] !== `npm:${PACKAGE}`)
6025
+ return { route: "pinned" };
6026
+ const command = settings2.npmCommand ?? ["npm"];
6027
+ if (!Array.isArray(command) || command.length !== 1 || !["npm", "bun"].includes(command[0]))
6028
+ return unsupported();
6029
+ const manager = command[0];
6030
+ return writable({
6031
+ route: manager === "npm" ? "pi-npm" : "pi-bun",
6032
+ manager,
6033
+ prefix: piPrefix
6034
+ }, join14(piPrefix, "node_modules"), s.root);
6035
+ } catch {
6036
+ return unsupported();
6037
+ }
6038
+ }
6039
+ const localModules = dirname10(dirname10(s.root)), local = dirname10(localModules);
6040
+ const manifest = (() => {
6041
+ try {
6042
+ return JSON.parse(readFileSync3(join14(local, "package.json"), "utf8"));
6043
+ } catch {
6044
+ return;
6045
+ }
6046
+ })();
6047
+ if (manifest?.name === "pi-extensions" || basename2(dirname10(local)) === ".pi" || basename2(local) === "npm" && existsSync3(join14(dirname10(local), "settings.json")))
6048
+ return unsupported();
6049
+ const bun = await commands.bun(["pm", "ls", "--global"]);
6050
+ const bunPrefix = bun.status === 0 ? /^(\/[^\r\n]+) node_modules(?: \(\d+(?: installed)?\))?\r?$/.exec(bun.stdout.split(`
6051
+ `)[0])?.[1] : undefined;
6052
+ if (bunPrefix && sameCopy(join14(bunPrefix, "node_modules"), s.root)) {
6053
+ const manager = prefixManager(bunPrefix);
6054
+ if (manager === "bun")
6055
+ return writable({ route: "bun", manager: "bun", prefix: bunPrefix }, join14(bunPrefix, "node_modules"), s.root);
6056
+ if (!manager)
6057
+ return unsupported();
6058
+ }
6059
+ const npm = await commands.npm(["root", "--global"]);
6060
+ const modules = npm.status === 0 ? npm.stdout.trim() : "";
6061
+ const prefix = dirname10(dirname10(modules));
6062
+ if (isAbsolute7(modules) && modules === join14(prefix, "lib", "node_modules") && sameCopy(modules, s.root))
6063
+ return writable({ route: "npm", manager: "npm", prefix }, modules, s.root);
6064
+ if (basename2(localModules) !== "node_modules" || !sameCopy(localModules, s.root))
6065
+ return unsupported();
6066
+ try {
6067
+ if (![
6068
+ manifest.dependencies,
6069
+ manifest.devDependencies,
6070
+ manifest.optionalDependencies
6071
+ ].some((deps) => deps && typeof deps[PACKAGE] === "string" && /^(?:[~^]?\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?|latest)$/.test(deps[PACKAGE])))
6072
+ return unsupported();
6073
+ const manager = prefixManager(local);
6074
+ if (!manager)
6075
+ return unsupported();
6076
+ if (manifest.packageManager !== undefined && (typeof manifest.packageManager !== "string" || !manifest.packageManager.startsWith(`${manager}@`)))
6077
+ return unsupported();
6078
+ return writable({
6079
+ route: manager === "bun" ? "bun-local" : "npm-local",
6080
+ manager,
6081
+ prefix: local
6082
+ }, localModules, s.root);
6083
+ } catch {
6084
+ return unsupported();
6085
+ }
6086
+ }
6087
+ function npmScope(i) {
6088
+ return [
6089
+ i.route === "npm" ? "--global" : "--global=false",
6090
+ "--prefix",
6091
+ i.prefix
6092
+ ];
6093
+ }
6094
+ function registryCommand(i, commands) {
6095
+ return (args, show) => commands[i.manager](i.manager === "npm" ? [args[0], ...npmScope(i), ...args.slice(1)] : args, show, { cwd: i.prefix });
6096
+ }
6097
+ function installVersion(i, version, commands) {
6098
+ const spec = `${PACKAGE}@${version}`;
6099
+ if (i.manager === "npm")
6100
+ return commands.npm([
6101
+ "install",
6102
+ ...npmScope(i),
6103
+ ...i.route === "pi-npm" ? ["--legacy-peer-deps"] : [],
6104
+ "--no-audit",
6105
+ "--no-fund",
6106
+ spec
6107
+ ], true, { cwd: i.prefix });
6108
+ return commands.bun([
6109
+ "add",
6110
+ ...i.route === "bun" ? ["--global"] : i.route === "pi-bun" ? ["--omit=peer"] : [],
6111
+ "--cwd",
6112
+ i.prefix,
6113
+ "--exact",
6114
+ spec
6115
+ ], true, { env: i.route === "bun" ? { BUN_INSTALL_GLOBAL_DIR: i.prefix } : {} });
6116
+ }
6117
+ async function fetchVersions(i, versions, commands) {
6118
+ try {
6119
+ for (const version of versions) {
6120
+ const spec = `${PACKAGE}@${version}`;
6121
+ if (i.manager === "npm") {
6122
+ if ((await commands.npm(["cache", "add", ...npmScope(i), spec], true, {
6123
+ cwd: i.prefix
6124
+ })).status !== 0)
6125
+ return false;
6126
+ } else {
6127
+ const stage = mkdtempSync(join14(tmpdir(), "cadence-upgrade-"));
6128
+ try {
6129
+ writeFileSync4(join14(stage, "package.json"), JSON.stringify({ private: true }));
6130
+ if ((await commands.bun([
6131
+ "add",
6132
+ "--cwd",
6133
+ stage,
6134
+ "--ignore-scripts",
6135
+ "--no-save",
6136
+ ...i.route === "pi-bun" ? ["--omit=peer"] : [],
6137
+ spec
6138
+ ], true)).status !== 0)
6139
+ return false;
6140
+ } finally {
6141
+ rmSync4(stage, { recursive: true, force: true });
6142
+ }
6143
+ }
6144
+ }
6145
+ return true;
6146
+ } catch {
6147
+ return false;
6148
+ }
6149
+ }
6150
+
5927
6151
  // src/service/launchd.ts
5928
- import { rmSync as rmSync4 } from "node:fs";
5929
- import { dirname as dirname10, join as join14 } from "node:path";
6152
+ import { rmSync as rmSync5 } from "node:fs";
6153
+ import { dirname as dirname11, join as join15 } from "node:path";
5930
6154
  var LABEL = "com.abelxiaoxing.cadence";
5931
6155
  var MARKER2 = `<?xml version="1.0" encoding="UTF-8"?>
5932
6156
  <!-- Generated by @abelxiaoxing/cadence`;
5933
6157
  function plistPath(s) {
5934
- return join14(s.home, "Library", "LaunchAgents", `${LABEL}.plist`);
6158
+ return join15(s.home, "Library", "LaunchAgents", `${LABEL}.plist`);
5935
6159
  }
5936
6160
  var xml = (value) => checked(value).replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
5937
6161
  function buildPlist(u) {
@@ -5956,7 +6180,7 @@ function buildPlist(u) {
5956
6180
  "\t</array>",
5957
6181
  ...u.workspace === null ? ["<!-- Cadence workbench gateway; no project workspace -->"] : [],
5958
6182
  "\t<key>WorkingDirectory</key>",
5959
- ` <string>${xml(u.workspace ?? dirname10(dirname10(u.cli)))}</string>`,
6183
+ ` <string>${xml(u.workspace ?? dirname11(dirname11(u.cli)))}</string>`,
5960
6184
  "\t<key>EnvironmentVariables</key>",
5961
6185
  "\t<dict>",
5962
6186
  ...u.environment.flatMap(([k, v]) => [
@@ -6011,11 +6235,11 @@ async function configureLaunchAgent(s, mode, requested) {
6011
6235
  s.launchctl(["bootout", target(s)]);
6012
6236
  write(path2, buildPlist({
6013
6237
  node: s.node,
6014
- cli: join14(s.root, "src", "service-cli.mjs"),
6238
+ cli: join15(s.root, "src", "service-cli.mjs"),
6015
6239
  workspace,
6016
6240
  serverConfig: serverConfigPath(s.home),
6017
6241
  environment: serviceEnvironment(s),
6018
- log: join14(s.home, "Library", "Logs", "cadence.log")
6242
+ log: join15(s.home, "Library", "Logs", "cadence.log")
6019
6243
  }));
6020
6244
  const result = {
6021
6245
  unit: path2,
@@ -6026,7 +6250,7 @@ async function configureLaunchAgent(s, mode, requested) {
6026
6250
  s.launchctl(["enable", target(s)]);
6027
6251
  else if (disabled(s))
6028
6252
  return { status: "disabled", ...result };
6029
- if (workspace !== null && !regularFile(join14(agentDirectory(s), "auth.json")))
6253
+ if (workspace !== null && !regularFile(join15(agentDirectory(s), "auth.json")))
6030
6254
  return { status: "waiting-for-pi-login", ...result };
6031
6255
  const started = s.launchctl(["bootstrap", domain(s), path2]).status === 0;
6032
6256
  return { status: started ? "started" : "failed", ...result };
@@ -6054,7 +6278,7 @@ async function removeLaunchAgent(s) {
6054
6278
  return { ...refused2, unit: path2 };
6055
6279
  if (s.uid !== undefined && loaded(s))
6056
6280
  s.launchctl(["bootout", target(s)]);
6057
- rmSync4(path2);
6281
+ rmSync5(path2);
6058
6282
  return { status: "removed", unit: path2 };
6059
6283
  }
6060
6284
  function launchAgentStatus(s) {
@@ -6074,15 +6298,15 @@ function launchAgentStatus(s) {
6074
6298
  }
6075
6299
 
6076
6300
  // src/service/login-run.ts
6077
- import { rmSync as rmSync5 } from "node:fs";
6078
- import { dirname as dirname11, isAbsolute as isAbsolute7, join as join15 } from "node:path";
6301
+ import { rmSync as rmSync6 } from "node:fs";
6302
+ import { dirname as dirname12, isAbsolute as isAbsolute8, join as join16 } from "node:path";
6079
6303
  var RUN_KEY = "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run";
6080
6304
  var APPROVED_KEY = "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\StartupApproved\\Run";
6081
6305
  var RUN_VALUE = "Cadence";
6082
6306
  var MARKER3 = "' Generated by @abelxiaoxing/cadence";
6083
6307
  function launcherPath(s) {
6084
6308
  const local = s.env.LOCALAPPDATA;
6085
- return join15(local && isAbsolute7(local) ? local : join15(s.home, "AppData", "Local"), "cadence-server", "cadence.vbs");
6309
+ return join16(local && isAbsolute8(local) ? local : join16(s.home, "AppData", "Local"), "cadence-server", "cadence.vbs");
6086
6310
  }
6087
6311
  var literal2 = (value) => `"${checked(value).replaceAll('"', '""')}"`;
6088
6312
  function argument2(value) {
@@ -6099,7 +6323,7 @@ function buildLauncher(u) {
6099
6323
  ...u.environment.length ? ['Set env = shell.Environment("Process")'] : [],
6100
6324
  ...u.environment.map(([k, v]) => `env(${literal2(k)}) = ${literal2(v)}`),
6101
6325
  ...u.workspace === null ? ["' Cadence workbench gateway; no project workspace"] : [],
6102
- `shell.CurrentDirectory = ${literal2(u.workspace ?? dirname11(dirname11(u.cli)))}`,
6326
+ `shell.CurrentDirectory = ${literal2(u.workspace ?? dirname12(dirname12(u.cli)))}`,
6103
6327
  `shell.Run ${literal2(command)}, 0, False`,
6104
6328
  ""
6105
6329
  ].join(`\r
@@ -6138,7 +6362,7 @@ async function configureLoginRun(s, mode, requested) {
6138
6362
  const environment = s.env.PI_CODING_AGENT_DIR ? [["PI_CODING_AGENT_DIR", agentDirectory(s)]] : [];
6139
6363
  write(path2, utf16(buildLauncher({
6140
6364
  node: s.node,
6141
- cli: join15(s.root, "src", "service-cli.mjs"),
6365
+ cli: join16(s.root, "src", "service-cli.mjs"),
6142
6366
  workspace,
6143
6367
  serverConfig: serverConfigPath(s.home),
6144
6368
  environment
@@ -6154,7 +6378,7 @@ async function configureLoginRun(s, mode, requested) {
6154
6378
  return { status: "failed", ...result };
6155
6379
  s.reg(["delete", APPROVED_KEY, "/v", RUN_VALUE, "/f"]);
6156
6380
  }
6157
- if (workspace !== null && !regularFile(join15(agentDirectory(s), "auth.json")))
6381
+ if (workspace !== null && !regularFile(join16(agentDirectory(s), "auth.json")))
6158
6382
  return { status: "waiting-for-pi-login", ...result };
6159
6383
  return { status: s.launch(path2) ? "started" : "failed", ...result };
6160
6384
  }
@@ -6182,7 +6406,7 @@ async function removeLoginRun(s) {
6182
6406
  for (const key of [RUN_KEY, APPROVED_KEY])
6183
6407
  if (present(s, key))
6184
6408
  s.reg(["delete", key, "/v", RUN_VALUE, "/f"]);
6185
- rmSync5(path2);
6409
+ rmSync6(path2);
6186
6410
  return { status: "removed", unit: path2 };
6187
6411
  }
6188
6412
  async function loginRunStatus(s) {
@@ -6203,12 +6427,9 @@ async function loginRunStatus(s) {
6203
6427
  }
6204
6428
 
6205
6429
  // src/service/maintenance.ts
6206
- import { spawn, spawnSync as spawnSync4 } from "node:child_process";
6207
- import { accessSync, constants as constants4, readFileSync as readFileSync3, realpathSync as realpathSync5 } from "node:fs";
6208
- import { dirname as dirname12, join as join16 } from "node:path";
6209
- var PACKAGE = "@abelxiaoxing/cadence";
6210
- var VIEW_MS = 30000;
6211
- var INSTALL_MS = 15 * 60000;
6430
+ import { spawn as spawn2, spawnSync as spawnSync4 } from "node:child_process";
6431
+ import { readFileSync as readFileSync4 } from "node:fs";
6432
+ import { join as join17 } from "node:path";
6212
6433
  var RESTART_MS = 5 * 60000;
6213
6434
  function cgroupPath(text2) {
6214
6435
  const lines = text2?.split(`
@@ -6226,7 +6447,7 @@ function insideUnit(s) {
6226
6447
  return unit !== "" && (own === unit || own.startsWith(`${unit}/`));
6227
6448
  }
6228
6449
  function installedVersion(root) {
6229
- const value = JSON.parse(readFileSync3(join16(root, "package.json"), "utf8"));
6450
+ const value = JSON.parse(readFileSync4(join17(root, "package.json"), "utf8"));
6230
6451
  if (!value || typeof value !== "object" || !("name" in value))
6231
6452
  throw new ContractError("invalid");
6232
6453
  literal(value.name, PACKAGE);
@@ -6264,11 +6485,10 @@ class MaintenanceRecord {
6264
6485
  this.write({ ...job2, state, reason, version, endedAt: Date.now() });
6265
6486
  }
6266
6487
  }
6267
- var npm = (args, show = false) => run("npm", args, show ? INSTALL_MS : VIEW_MS, show);
6268
- function run(command, args, timeout, show = false) {
6488
+ function run(command, args, timeout) {
6269
6489
  return new Promise((resolve7) => {
6270
- const child = spawn(command, args, {
6271
- stdio: ["ignore", show ? 2 : "pipe", show ? 2 : "ignore"],
6490
+ const child = spawn2(command, args, {
6491
+ stdio: ["ignore", "pipe", "ignore"],
6272
6492
  timeout,
6273
6493
  windowsHide: true
6274
6494
  });
@@ -6281,37 +6501,22 @@ function run(command, args, timeout, show = false) {
6281
6501
  child.once("close", (status) => resolve7({ status, stdout }));
6282
6502
  });
6283
6503
  }
6284
- async function upgradeRoute(root, npmRun = npm) {
6285
- const result = await npmRun(["root", "--global"]);
6286
- const global = result.status === 0 ? result.stdout.trim() : "";
6287
- let installed;
6288
- try {
6289
- installed = realpathSync5(join16(global, ...PACKAGE.split("/")));
6290
- if (!global || installed !== realpathSync5(root))
6291
- return "not-npm-global";
6292
- } catch {
6293
- return "not-npm-global";
6294
- }
6295
- try {
6296
- accessSync(dirname12(installed), constants4.W_OK);
6297
- accessSync(global, constants4.W_OK);
6298
- } catch {
6299
- return "not-writable";
6300
- }
6301
- return "npm";
6302
- }
6303
- async function release(spec = "latest", npmRun = npm) {
6304
- const result = await npmRun([
6305
- "view",
6306
- `${PACKAGE}@${spec}`,
6307
- "version",
6308
- "deprecated",
6309
- "--json"
6310
- ]);
6504
+ async function release(spec, command, manager = "npm") {
6505
+ const name = `${PACKAGE}@${spec ?? "latest"}`;
6506
+ const result = await command(manager === "bun" ? ["info", name, "--json"] : ["view", name, "version", "deprecated", "--json"]);
6311
6507
  if (result.status !== 0)
6312
6508
  return;
6313
6509
  try {
6314
6510
  let value = JSON.parse(result.stdout);
6511
+ if (manager === "bun") {
6512
+ if (!value || typeof value !== "object" || Array.isArray(value) || !("name" in value) || !("version" in value))
6513
+ return;
6514
+ literal(value.name, PACKAGE);
6515
+ return {
6516
+ version: decodeVersion(value.version),
6517
+ deprecated: "deprecated" in value && typeof value.deprecated === "string"
6518
+ };
6519
+ }
6315
6520
  if (Array.isArray(value)) {
6316
6521
  if (value.length !== 1)
6317
6522
  return;
@@ -6328,15 +6533,20 @@ async function release(spec = "latest", npmRun = npm) {
6328
6533
  return;
6329
6534
  }
6330
6535
  }
6536
+ function releaseForInstallation(selected, commands, spec) {
6537
+ return "manager" in selected ? release(spec, registryCommand(selected, commands), selected.manager) : Promise.resolve(undefined);
6538
+ }
6331
6539
  async function restartInstalled(s) {
6332
6540
  const name = serviceName(s);
6333
6541
  const result = await run(s.node, [
6334
- join16(s.root, "src", "service-cli.mjs"),
6542
+ join17(s.root, "src", "service-cli.mjs"),
6335
6543
  "server",
6336
6544
  "restart",
6337
6545
  s.scope === "system" ? "--system" : "--user",
6338
6546
  ...name ? ["--name", name] : []
6339
6547
  ], RESTART_MS);
6548
+ if (result.status !== 0)
6549
+ return false;
6340
6550
  try {
6341
6551
  const value = JSON.parse(result.stdout.trim().split(`
6342
6552
  `).at(-1) ?? "");
@@ -6346,46 +6556,54 @@ async function restartInstalled(s) {
6346
6556
  }
6347
6557
  }
6348
6558
  async function upgradeService(s, to, ports) {
6349
- const route = await upgradeRoute(s.root, ports.npm);
6350
- if (route !== "npm")
6351
- return { status: route };
6559
+ const selected = await installation(s, ports.commands);
6560
+ if (!("manager" in selected))
6561
+ return { status: selected.route };
6562
+ const currentVersion = () => {
6563
+ try {
6564
+ return installedVersion(s.root);
6565
+ } catch {
6566
+ return;
6567
+ }
6568
+ };
6352
6569
  const from = installedVersion(s.root);
6353
- const target2 = await release(to, ports.npm);
6354
- if (!target2)
6570
+ const target2 = await releaseForInstallation(selected, ports.commands, to);
6571
+ if (!target2 || to !== undefined && target2.version !== to)
6355
6572
  return { status: "failed", reason: "fetch-failed", from };
6356
6573
  const versions = { from, to: target2.version };
6357
6574
  if (target2.version === from)
6358
6575
  return { status: "current", version: from, ...versions };
6359
6576
  if (target2.deprecated)
6360
6577
  return { status: "deprecated", ...versions };
6361
- const spec = `${PACKAGE}@${target2.version}`;
6362
- if ((await ports.npm(["cache", "add", spec], true)).status !== 0)
6578
+ if (!await fetchVersions(selected, [target2.version, from], ports.commands))
6363
6579
  return { status: "failed", reason: "fetch-failed", ...versions };
6580
+ if (currentVersion() !== from || JSON.stringify(await installation(s, ports.commands)) !== JSON.stringify(selected))
6581
+ return { status: "failed", reason: "refused", ...versions };
6364
6582
  const stopped = await ports.stop();
6365
6583
  if (stopped.status !== "stopped")
6366
6584
  return { ...stopped, reason: "stop-unconfirmed", ...versions };
6367
- const install = (version) => ports.npm([
6368
- "install",
6369
- "--global",
6370
- "--no-audit",
6371
- "--no-fund",
6372
- `${PACKAGE}@${version}`
6373
- ], true);
6374
- const settled = (status, reason) => ({
6585
+ const install = (version) => installVersion(selected, version, ports.commands);
6586
+ const settled = (status, reason2) => ({
6375
6587
  status,
6376
- ...reason ? { reason } : {},
6588
+ ...reason2 ? { reason: reason2 } : {},
6377
6589
  ...versions,
6378
- version: installedVersion(s.root)
6590
+ version: currentVersion()
6379
6591
  });
6380
- if ((await install(target2.version)).status !== 0) {
6381
- await ports.restart();
6382
- return settled("failed", "install-failed");
6592
+ const installed = (await install(target2.version)).status === 0 && currentVersion() === target2.version;
6593
+ if (installed) {
6594
+ if (await ports.restart())
6595
+ return settled("upgraded");
6596
+ const stoppedNew = await ports.stop();
6597
+ if (stoppedNew.status !== "stopped")
6598
+ return {
6599
+ ...stoppedNew,
6600
+ ...settled(stoppedNew.status, "stop-unconfirmed")
6601
+ };
6383
6602
  }
6384
- if (await ports.restart())
6385
- return settled("upgraded");
6386
- if ((await install(from)).status === 0 && await ports.restart())
6387
- return settled("rolled-back", "not-ready");
6388
- return settled("failed", "not-ready");
6603
+ const reason = installed ? "not-ready" : "install-failed";
6604
+ if ((await install(from)).status === 0 && currentVersion() === from && await ports.restart())
6605
+ return settled(installed ? "rolled-back" : "failed", reason);
6606
+ return settled("failed", reason);
6389
6607
  }
6390
6608
  function jobOutcome(action, result) {
6391
6609
  if (action === "upgrade") {
@@ -6406,8 +6624,8 @@ function jobOutcome(action, result) {
6406
6624
  }
6407
6625
 
6408
6626
  // src/service/system-service.ts
6409
- import { existsSync as existsSync3, realpathSync as realpathSync6, rmSync as rmSync6 } from "node:fs";
6410
- import { isAbsolute as isAbsolute8, join as join17 } from "node:path";
6627
+ import { existsSync as existsSync4, realpathSync as realpathSync6, rmSync as rmSync7 } from "node:fs";
6628
+ import { isAbsolute as isAbsolute9, join as join18 } from "node:path";
6411
6629
  var MARKER4 = "# Generated by @abelxiaoxing/cadence";
6412
6630
  function boundary(s) {
6413
6631
  if (s.platform !== "linux" || s.scope !== "system")
@@ -6421,7 +6639,7 @@ function legacyPath(s) {
6421
6639
  function observed(s) {
6422
6640
  const path2 = unitPath(s);
6423
6641
  let previous = readGenerated(path2, MARKER4);
6424
- if (typeof previous === "string" && (unitWorkspace(previous) !== null && !isAbsolute8(unitWorkspace(previous) ?? "") || !previous.includes(`
6642
+ if (typeof previous === "string" && (unitWorkspace(previous) !== null && !isAbsolute9(unitWorkspace(previous) ?? "") || !previous.includes(`
6425
6643
  User=root
6426
6644
  `)))
6427
6645
  previous = null;
@@ -6438,7 +6656,7 @@ function sameStateEnvironment(s, previous) {
6438
6656
  if (values.size !== kept.length)
6439
6657
  return false;
6440
6658
  const state = s.env.XDG_STATE_HOME;
6441
- return values.get("HOME") === s.home && values.get("XDG_STATE_HOME") === (state && isAbsolute8(state) ? state : undefined);
6659
+ return values.get("HOME") === s.home && values.get("XDG_STATE_HOME") === (state && isAbsolute9(state) ? state : undefined);
6442
6660
  } catch {
6443
6661
  return false;
6444
6662
  }
@@ -6459,7 +6677,7 @@ async function configureSystemService(s, mode, requested) {
6459
6677
  const refused2 = boundary(s);
6460
6678
  if (refused2)
6461
6679
  return refused2;
6462
- if (existsSync3(legacyPath(s)))
6680
+ if (existsSync4(legacyPath(s)))
6463
6681
  return { status: "scope-conflict", unit: legacyPath(s), scope: "system" };
6464
6682
  if (s.systemctl(["show-environment"]).status !== 0)
6465
6683
  return { status: "unavailable", scope: "system" };
@@ -6471,17 +6689,17 @@ async function configureSystemService(s, mode, requested) {
6471
6689
  if (mode === "restart" && previous === undefined)
6472
6690
  return { status: "absent", scope: "system" };
6473
6691
  const sdk = sdkPackages(s.root);
6474
- if (previous && unitWorkspace(previous) && !existsSync3(unitWorkspace(previous)))
6692
+ if (previous && unitWorkspace(previous) && !existsSync4(unitWorkspace(previous)))
6475
6693
  return { status: "workspace-changed", ...result };
6476
6694
  const selected = selectWorkspace(s, requested, mode === "install" && requested === undefined ? null : unitWorkspace(previous), false);
6477
6695
  if (selected.workspace !== null && !sdk)
6478
6696
  return { status: "missing-pi-sdk", scope: "system" };
6479
- const selectedReal = selected.workspace && existsSync3(selected.workspace) ? realpathSync6(selected.workspace) : undefined;
6697
+ const selectedReal = selected.workspace && existsSync4(selected.workspace) ? realpathSync6(selected.workspace) : undefined;
6480
6698
  const agentDir = agentDirectory(s);
6481
6699
  const content = buildUnit({
6482
6700
  system: true,
6483
6701
  node: s.node,
6484
- cli: join17(s.root, "src", "service-cli.mjs"),
6702
+ cli: join18(s.root, "src", "service-cli.mjs"),
6485
6703
  workspace: selected.workspace,
6486
6704
  serverConfig: serviceConfigPath(s),
6487
6705
  agentDir,
@@ -6530,7 +6748,7 @@ async function configureSystemService(s, mode, requested) {
6530
6748
  return { status: "failed", ...result, ...selected };
6531
6749
  if (mode === "install" && s.systemctl(["enable", s.unit]).status !== 0)
6532
6750
  return { status: "failed", ...result, ...selected };
6533
- if (selected.workspace !== null && !regularFile(join17(agentDir, "auth.json")))
6751
+ if (selected.workspace !== null && !regularFile(join18(agentDir, "auth.json")))
6534
6752
  return { status: "waiting-for-pi-login", ...result, ...selected };
6535
6753
  s.systemctl(["reset-failed", s.unit]);
6536
6754
  return {
@@ -6572,7 +6790,7 @@ async function stopSystemService(s, remove = false) {
6572
6790
  return { status: "failed", workspace, ...result };
6573
6791
  if (readGenerated(path2, MARKER4) !== previous)
6574
6792
  return { status: "foreign", ...result };
6575
- rmSync6(path2);
6793
+ rmSync7(path2);
6576
6794
  if (s.systemctl(["daemon-reload"]).status !== 0)
6577
6795
  return { status: "failed", ...result };
6578
6796
  s.systemctl(["reset-failed", s.unit]);
@@ -6770,10 +6988,10 @@ function startupGuidance(result, input) {
6770
6988
  return `Pi's agent directory ${agent} must be an existing real directory (not a symlink) owned by you.
6771
6989
  `;
6772
6990
  case "pi-auth":
6773
- return `${join18(agent, "auth.json")} must be your own single-link regular file holding valid Pi credentials; log in to Pi first.
6991
+ return `${join19(agent, "auth.json")} must be your own single-link regular file holding valid Pi credentials; log in to Pi first.
6774
6992
  `;
6775
6993
  case "pi-models":
6776
- return `${join18(agent, "models.json")} must be your own single-link regular file using only fields Cadence's pinned Pi SDK supports; a field only newer Pi understands is refused.
6994
+ return `${join19(agent, "models.json")} must be your own single-link regular file using only fields Cadence's pinned Pi SDK supports; a field only newer Pi understands is refused.
6777
6995
  `;
6778
6996
  case "pi-runtime":
6779
6997
  return `Loading Pi's SDK model runtime failed (optional SDK packages missing, or sources the SDK refuses). Fix that and start again; the next start recovers the owner's registration automatically.
@@ -6833,7 +7051,7 @@ async function launchHost(input, c, foreground, hostUrl, cwd, env, network) {
6833
7051
  throw new ContractError("oversize");
6834
7052
  if (!sameNetwork(network, networkSnapshot(process.env)))
6835
7053
  throw new ContractError("stale");
6836
- const child = spawn2(process.execPath, [
7054
+ const child = spawn3(process.execPath, [
6837
7055
  ...env.NODE_USE_ENV_PROXY === "1" ? ["--use-env-proxy"] : [],
6838
7056
  fileURLToPath2(hostUrl)
6839
7057
  ], {
@@ -7004,13 +7222,13 @@ function upgrade(s, to) {
7004
7222
  if (s.platform !== "linux")
7005
7223
  return Promise.resolve({ status: "unsupported" });
7006
7224
  return upgradeService(s, to, {
7007
- npm,
7225
+ commands: packageCommands(s),
7008
7226
  stop: () => serverAction(s, "stop", undefined, false),
7009
7227
  restart: () => restartInstalled(s)
7010
7228
  });
7011
7229
  }
7012
7230
  function serverGuidance(s, action, result, link) {
7013
- const cli = piUserInstall(s) ? `node ${JSON.stringify(join18(s.root, "src", "service-cli.mjs"))}` : "cadence";
7231
+ const cli = piUserInstall(s) ? `node ${JSON.stringify(join19(s.root, "src", "service-cli.mjs"))}` : "cadence";
7014
7232
  const note = result.redirectedFrom ? `Cadence workspace: ${result.workspace} (${result.redirectedFrom} contains Cadence's private state)
7015
7233
  ` : "";
7016
7234
  return note + serverStatusGuidance(s, action, result, cli, link);
@@ -7043,7 +7261,7 @@ function managerDetail(s) {
7043
7261
  var REASONS = {
7044
7262
  "fetch-failed": "the release could not be fetched; the service kept running",
7045
7263
  "stop-unconfirmed": "the running service did not confirm its orderly Stop",
7046
- "install-failed": "npm could not install it",
7264
+ "install-failed": "the package manager could not install it",
7047
7265
  "not-ready": "the new version did not become ready"
7048
7266
  };
7049
7267
  function serverStatusGuidance(s, action, result, cli, link) {
@@ -7066,17 +7284,20 @@ function serverStatusGuidance(s, action, result, cli, link) {
7066
7284
  case "rolled-back":
7067
7285
  return `Cadence ${result.to} did not become ready, so ${result.from} was reinstalled and is running; ${managerDetail(s)}.
7068
7286
  `;
7069
- case "not-npm-global":
7070
- return `This copy of Cadence (${s.root}) was not installed with npm install -g, so it cannot upgrade itself. ${terminal}
7287
+ case "unsupported-install":
7288
+ return `The installation method for this copy of Cadence (${s.root}) could not be safely identified. ${terminal}
7289
+ `;
7290
+ case "pinned":
7291
+ return `Pi pins this package version; change its package declaration explicitly before upgrading. ${terminal}
7071
7292
  `;
7072
7293
  case "not-writable":
7073
- return `npm cannot write ${s.root} as this account. ${terminal}
7294
+ return `The package manager cannot write ${s.root} as this account. ${terminal}
7074
7295
  `;
7075
7296
  case "unsupported":
7076
- return `Cadence upgrades itself only on Linux. Run npm install -g ${PACKAGE}@latest, then: ${cli} server restart
7297
+ return `Cadence upgrades itself only on Linux. ${terminal}
7077
7298
  `;
7078
7299
  case "failed":
7079
- return `Cadence was not upgraded: ${REASONS[result.reason ?? ""] ?? result.reason}. Running now: ${result.version ?? result.from}; ${managerDetail(s)}.
7300
+ return `Cadence was not upgraded: ${REASONS[result.reason ?? ""] ?? result.reason}. Installed version: ${result.version ?? "not confirmed"}; ${managerDetail(s)}.
7080
7301
  `;
7081
7302
  }
7082
7303
  }
@@ -7114,7 +7335,7 @@ ${manage}
7114
7335
  ${linger}`;
7115
7336
  }
7116
7337
  case "waiting-for-pi-login":
7117
- return `Cadence is registered for ${result.workspace}; it starts once Pi has ${join18(agentDirectory(s), "auth.json")}.
7338
+ return `Cadence is registered for ${result.workspace}; it starts once Pi has ${join19(agentDirectory(s), "auth.json")}.
7118
7339
  Log in to Pi, then: ${cli} server restart${flags}
7119
7340
  ${config}
7120
7341
  ${lingerGuidance(result.linger)}`;
@@ -7306,7 +7527,7 @@ async function serverMain(argv, hostUrl) {
7306
7527
  if (job2 && (action === "restart" || action === "upgrade")) {
7307
7528
  const { state, reason } = jobOutcome(action, result);
7308
7529
  try {
7309
- new MaintenanceRecord(new GatewayStore(serviceConfigPath(s))).finish(job2, state, reason, installedVersion(s.root));
7530
+ new MaintenanceRecord(new GatewayStore(serviceConfigPath(s))).finish(job2, state, reason, action === "upgrade" ? result.version ?? null : installedVersion(s.root));
7310
7531
  } catch {}
7311
7532
  }
7312
7533
  process.stderr.write(vanished ?? serverGuidance(s, action, result, link));
@@ -7326,7 +7547,8 @@ async function serverMain(argv, hostUrl) {
7326
7547
  "project-running",
7327
7548
  "environment-changed",
7328
7549
  "inside-service",
7329
- "not-npm-global",
7550
+ "unsupported-install",
7551
+ "pinned",
7330
7552
  "not-writable",
7331
7553
  "deprecated",
7332
7554
  "rolled-back"
@@ -7494,7 +7716,7 @@ cadence workbench start|status|diagnose|stop|token-link --reveal|token-rotate [-
7494
7716
  cadence doctor [--fix] [--yes] [--workspace PATH] [--state-home PATH] [--server-config PATH]
7495
7717
  Start recovers automatically from an unclean shutdown or crash. Doctor shows the state readonly; --fix also stops an unresponsive Cadence or takes over state it cannot observe, after one confirmation (--yes skips it). recover inspect|resolve are aliases of doctor and doctor --fix.
7496
7718
  cadence server install [--workspace PATH] [--no-linger]|status|restart|stop|uninstall|upgrade [--to VERSION]
7497
- Upgrade (Linux, npm global installs) fetches the release while the service runs, then stops it, installs the release and restarts; a release that does not become ready is replaced by the previous version. Server commands that change the service refuse to run inside it; use the WebUI or a terminal.
7719
+ Upgrade (Linux, identified Pi/npm/Bun installs) fetches the release while the service runs, then stops it, installs the release and restarts; a release that does not become ready is replaced by the previous version. Server commands that change the service refuse to run inside it; use the WebUI or a terminal.
7498
7720
  Linux server options: --system (root, no linger) or --user; --name NAME selects an independent service; install also accepts --port PORT (required for a new named service). Root defaults to --system. No automatic sudo or profile migration.
7499
7721
  cadence project open [PATH] [--create] [--agent-dir PATH] [--state-home PATH]
7500
7722
  Project open explicitly starts/reuses that checkout owner, selecting a private free port for a new project. It never creates or starts a task.