benignswarm 0.12.87 → 0.12.89

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/install.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { spawnSync } from "node:child_process";
2
- import { existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
2
+ import { existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, realpathSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs";
3
3
  import { homedir, userInfo } from "node:os";
4
4
  import { basename, delimiter, dirname, join, resolve } from "node:path";
5
5
  import { configRoot } from "./config.js";
@@ -34,6 +34,57 @@ export function requireInstall() {
34
34
  }
35
35
  return r;
36
36
  }
37
+ /** Build the environment installed for the machine daemon and used by its harness checks. */
38
+ export function composeMachinePath(options) {
39
+ const isDirectory = options.exists ?? ((path) => {
40
+ try {
41
+ return statSync(path).isDirectory();
42
+ }
43
+ catch {
44
+ return false;
45
+ }
46
+ });
47
+ const preferred = [
48
+ join(options.home, ".local", "bin"),
49
+ dirname(options.installBin),
50
+ dirname(options.npmBin),
51
+ ...(options.platform === "darwin" ? ["/opt/homebrew/bin", "/usr/local/bin"] : []),
52
+ ];
53
+ const entries = [];
54
+ const seen = new Set();
55
+ for (const entry of [...preferred.filter(isDirectory), ...options.currentPath.split(delimiter).filter(Boolean)]) {
56
+ if (seen.has(entry))
57
+ continue;
58
+ seen.add(entry);
59
+ entries.push(entry);
60
+ }
61
+ return entries.join(delimiter);
62
+ }
63
+ /** The machine daemon must not inherit PATH from whichever shell happened to install it. */
64
+ export function machineDaemonPath(currentPath = process.env.PATH ?? "", platformName = process.platform, home = homedir()) {
65
+ const install = requireInstall();
66
+ // npm is commonly a #!/usr/bin/env node script. Give it the known runtime locations
67
+ // before asking it for the prefix, since the shell that provisioned this machine may
68
+ // not have ~/.local/bin (or the recorded install's bin) on PATH yet.
69
+ const discoveryPath = composeMachinePath({
70
+ platform: platformName,
71
+ home,
72
+ installBin: install.bin,
73
+ npmBin: install.npm,
74
+ currentPath,
75
+ });
76
+ const prefix = spawnSync(install.npm, ["prefix", "-g"], {
77
+ encoding: "utf8",
78
+ timeout: 30_000,
79
+ env: { ...process.env, PATH: discoveryPath },
80
+ });
81
+ if (prefix.status !== 0)
82
+ throw new CliError("npm_prefix_failed", `${install.npm} prefix -g failed while preparing the machine daemon PATH`, (prefix.stderr ?? "").trim().slice(0, 300));
83
+ const globalPrefix = (prefix.stdout ?? "").trim();
84
+ if (!globalPrefix || !globalPrefix.startsWith("/"))
85
+ throw new CliError("npm_prefix_invalid", `${install.npm} prefix -g did not return an absolute path`, (prefix.stdout ?? "").trim().slice(0, 300));
86
+ return composeMachinePath({ platform: platformName, home, installBin: install.bin, npmBin: join(globalPrefix, "bin", "npm"), currentPath });
87
+ }
37
88
  /** "b9s 0.12.38" -> "0.12.38"; null when the binary cannot run. */
38
89
  export function binVersion(bin) {
39
90
  const r = spawnSync(bin, ["--version"], { encoding: "utf8", timeout: 20_000 });
@@ -0,0 +1,84 @@
1
+ import { createHash } from "node:crypto";
2
+ import { spawnSync } from "node:child_process";
3
+ import { readFileSync, statSync } from "node:fs";
4
+ import { homedir } from "node:os";
5
+ import { join } from "node:path";
6
+ import { CliError } from "./errors.js";
7
+ import { claudeResultError } from "./harnesses.js";
8
+ const MAX_AGE_MS = 6 * 60 * 60_000;
9
+ const checks = new Map();
10
+ export const deepSeekKeyRoot = () => join(homedir(), ".b9s");
11
+ export const deepSeekKeyPath = (root = deepSeekKeyRoot()) => join(root, "providers", "deepseek.key");
12
+ /** The key never appears in argv, watcher settings, or diagnostics. */
13
+ export function claudeProviderEnv(provider, root = deepSeekKeyRoot(), model = "deepseek-flash") {
14
+ if (provider === "anthropic")
15
+ return {};
16
+ const path = deepSeekKeyPath(root);
17
+ let key;
18
+ try {
19
+ const mode = statSync(path).mode & 0o777;
20
+ if (mode !== 0o600)
21
+ throw new CliError("provider_key_permissions", `${path} must have mode 600 (currently ${mode.toString(8).padStart(3, "0")})`, `Run: chmod 600 ${path}`);
22
+ key = readFileSync(path, "utf8").trim();
23
+ }
24
+ catch (error) {
25
+ if (error.code === "ENOENT")
26
+ throw new CliError("provider_key_missing", `DeepSeek provider key file is missing: ${path}`, `Create ${path} with your DeepSeek API key, then run: chmod 600 ${path}`);
27
+ throw error;
28
+ }
29
+ if (!key)
30
+ throw new CliError("provider_key_empty", `DeepSeek provider key file is empty: ${path}`);
31
+ return {
32
+ ANTHROPIC_BASE_URL: "https://api.deepseek.com/anthropic",
33
+ ANTHROPIC_AUTH_TOKEN: key,
34
+ ANTHROPIC_MODEL: model,
35
+ ANTHROPIC_SMALL_FAST_MODEL: model,
36
+ CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: "1",
37
+ };
38
+ }
39
+ /** The credentials and selected model for one Claude Code turn; Anthropic keeps its own env. */
40
+ export function claudeTurnEnv(provider, model, root) {
41
+ return provider ? claudeProviderEnv(provider, root, model) : {};
42
+ }
43
+ function keyFingerprint(provider, root) {
44
+ if (provider === "anthropic")
45
+ return "anthropic-session-auth";
46
+ const path = deepSeekKeyPath(root);
47
+ try {
48
+ const mode = statSync(path).mode & 0o777;
49
+ if (mode !== 0o600)
50
+ return `mode:${mode}`;
51
+ return createHash("sha256").update(readFileSync(path)).digest("hex");
52
+ }
53
+ catch (error) {
54
+ return error.code === "ENOENT" ? "missing" : `unreadable:${error.code ?? "error"}`;
55
+ }
56
+ }
57
+ /** Make one tiny provider call when first used, after its key changes, or every six hours. */
58
+ export function verifyClaudeProvider(provider, root = deepSeekKeyRoot(), nowMs = Date.now(), run = spawnSync) {
59
+ const fingerprint = keyFingerprint(provider, root);
60
+ const cacheKey = `${root}\0${provider}`;
61
+ const cached = checks.get(cacheKey);
62
+ if (cached?.fingerprint === fingerprint && nowMs - cached.at < MAX_AGE_MS)
63
+ return cached.result;
64
+ let result;
65
+ let secret;
66
+ const safeReason = (reason) => secret ? reason.replaceAll(secret, "[redacted]") : reason;
67
+ try {
68
+ const env = claudeProviderEnv(provider, root, "deepseek-flash");
69
+ secret = env.ANTHROPIC_AUTH_TOKEN;
70
+ const checked = run("claude", ["-p", "--output-format", "json", ...(env.ANTHROPIC_MODEL ? ["--model", env.ANTHROPIC_MODEL] : []), "Reply with exactly OK."], {
71
+ encoding: "utf8", timeout: 60_000, cwd: homedir(), env: { ...process.env, ...env, CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: "1" }, maxBuffer: 64 * 1024,
72
+ });
73
+ const outputError = claudeResultError(checked.stdout ?? "");
74
+ result = checked.status === 0 && !outputError
75
+ ? { logged_in: true }
76
+ : { logged_in: false, reason: safeReason(outputError ?? `Claude Code provider verification failed (exit ${checked.status ?? "unknown"})`).slice(0, 400) };
77
+ }
78
+ catch (error) {
79
+ result = { logged_in: false, reason: safeReason(error.message).slice(0, 400) };
80
+ }
81
+ checks.set(cacheKey, { fingerprint, at: nowMs, result });
82
+ return result;
83
+ }
84
+ export function clearProviderChecksForTests() { checks.clear(); }
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const CLI_VERSION = "0.12.87";
1
+ export const CLI_VERSION = "0.12.89";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "benignswarm",
3
- "version": "0.12.87",
3
+ "version": "0.12.89",
4
4
  "description": "b9s: chat, status board and evidence store for coding agents (benignswarm.org)",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {