@join-pantheon/cli 0.37.3 → 0.38.1

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,4 +1,5 @@
1
- import { closeSync, fsyncSync, mkdirSync, openSync, renameSync, rmSync, writeFileSync, writeSync } from "node:fs";
1
+ import { closeSync, existsSync, fsyncSync, mkdirSync, openSync, readFileSync, renameSync, rmSync, writeFileSync, writeSync } from "node:fs";
2
+ import { homedir } from "node:os";
2
3
  import { dirname, join } from "node:path";
3
4
  import { CONFIG_PATH } from "./config.js";
4
5
  import { trustWrite } from "./home-guard.js";
@@ -78,6 +79,63 @@ export function describeRunMinutes(r, persona) {
78
79
  : r.from === "flag" ? "this worker's --run-minutes" : `lowered by persona ${persona ?? ""}`.trim();
79
80
  return `${r.minutes} minutes per run (${why})`;
80
81
  }
82
+ export const DEFAULT_RUN_MODEL = { "claude-code": "sonnet" };
83
+ export const OWN_MODEL = "own";
84
+ export const RUN_MODEL_AGENTS = ["claude-code", "codex", "cursor", "copilot", "kiro"];
85
+ export function parseModelName(raw) {
86
+ if (raw === undefined || raw === null)
87
+ return null;
88
+ const s = String(raw).trim();
89
+ return /^[A-Za-z0-9][A-Za-z0-9._:/\[\]-]{0,79}$/.test(s) ? s : null;
90
+ }
91
+ export function effectiveRunModel(agent, policy, routing = null) {
92
+ const set = agent ? policy?.models?.[agent] : undefined;
93
+ if (set === OWN_MODEL)
94
+ return { model: null, from: "own" };
95
+ if (set)
96
+ return { model: set, from: "policy" };
97
+ const d = agent ? DEFAULT_RUN_MODEL[agent] : undefined;
98
+ if (d && agent === "claude-code" && routing)
99
+ return { model: null, from: "provider" };
100
+ return d ? { model: d, from: "default" } : { model: null, from: "own" };
101
+ }
102
+ export const CLAUDE_ROUTING_VARS = ["CLAUDE_CODE_USE_BEDROCK", "CLAUDE_CODE_USE_VERTEX", "CLAUDE_CODE_USE_FOUNDRY", "ANTHROPIC_BASE_URL", "ANTHROPIC_MODEL"];
103
+ export function claudeRouting(env = process.env, home = homedir()) {
104
+ let fromSettings = {};
105
+ try {
106
+ const j = JSON.parse(readFileSync(join(home, ".claude", "settings.json"), "utf8"));
107
+ if (j && typeof j.env === "object" && j.env !== null)
108
+ fromSettings = j.env;
109
+ }
110
+ catch { }
111
+ for (const v of CLAUDE_ROUTING_VARS) {
112
+ const x = env[v] ?? fromSettings[v];
113
+ if (typeof x !== "string" || !x.trim())
114
+ continue;
115
+ if (v.startsWith("CLAUDE_CODE_USE_") && /^(0|false|no|off)$/i.test(x.trim()))
116
+ continue;
117
+ return v;
118
+ }
119
+ return null;
120
+ }
121
+ export function describeRunModel(r) {
122
+ if (!r.model)
123
+ return "the agent's own default model";
124
+ return `${r.model} (${r.from === "default" ? "the default" : "set on this machine"})`;
125
+ }
126
+ export const GOAL_RUNS = { default: 12, min: 1, max: 100 };
127
+ export function parseGoalRuns(raw) {
128
+ if (raw === undefined || raw === null)
129
+ return null;
130
+ const s = String(raw).trim();
131
+ if (!/^\d+$/.test(s))
132
+ return null;
133
+ const n = Number(s);
134
+ return n >= GOAL_RUNS.min && n <= GOAL_RUNS.max ? n : null;
135
+ }
136
+ export function effectiveGoalRuns(policy) {
137
+ return policy?.goalRuns !== undefined ? { runs: policy.goalRuns, from: "policy" } : { runs: GOAL_RUNS.default, from: "default" };
138
+ }
81
139
  export const finalKey = {
82
140
  cap: (cap) => `cap:${cap}`,
83
141
  wake: (team, member) => `wake:${wakeKey(team, member)}`,
@@ -170,7 +228,7 @@ export function loadPolicy(home) {
170
228
  }
171
229
  catch (e) {
172
230
  if (e.code === "ENOENT")
173
- return { grants: {}, asks: {}, denied: {}, attempts: {} };
231
+ return { grants: {}, asks: {}, denied: {}, attempts: {}, born: Date.now() };
174
232
  if (attempt < 3) {
175
233
  pause(15);
176
234
  continue;
@@ -226,9 +284,14 @@ function parsePolicy(text) {
226
284
  ? { upgradeHeld: { version: raw.upgradeHeld.version, node: raw.upgradeHeld.node, at: Number(raw.upgradeHeld.at) || 0, why: String(raw.upgradeHeld.why ?? "") } } : {}),
227
285
  ...(typeof raw.runMinutes === "number" && parseRunMinutes(raw.runMinutes) !== null ? { runMinutes: raw.runMinutes } : {}),
228
286
  ...(typeof raw.idleSeconds === "number" && parseIdleSeconds(raw.idleSeconds) !== null ? { idleSeconds: raw.idleSeconds } : {}),
287
+ ...(isRecord(raw.models) ? (() => { const m = Object.fromEntries(Object.entries(raw.models).filter(([k, v]) => RUN_MODEL_AGENTS.includes(k) && typeof v === "string" && parseModelName(v) !== null)); return Object.keys(m).length ? { models: m } : {}; })() : {}),
288
+ ...(typeof raw.goalRuns === "number" && parseGoalRuns(raw.goalRuns) !== null ? { goalRuns: raw.goalRuns } : {}),
229
289
  ...(typeof raw.mode === "string" && parseMode(raw.mode) ? { mode: raw.mode } : {}),
230
290
  ...(typeof raw.modeToldAt === "number" ? { modeToldAt: raw.modeToldAt } : {}),
231
291
  ...(isRecord(raw.stuckReports) ? { stuckReports: Object.fromEntries(Object.entries(raw.stuckReports).filter(([, v]) => isRecord(v) && typeof v.ref === "string" && typeof v.team === "string" && typeof v.at === "number")) } : {}),
292
+ ...(isRecord(raw.revoked) ? { revoked: Object.fromEntries(Object.entries(raw.revoked).filter(([, v]) => isTime(v))) } : {}),
293
+ ...(isRecord(raw.usedOnce) ? { usedOnce: Object.fromEntries(Object.entries(raw.usedOnce).filter(([, v]) => isTime(v))) } : {}),
294
+ ...(isTime(raw.born) ? { born: raw.born } : {}),
232
295
  ...(Array.isArray(raw.finalNo) ? { finalNo: raw.finalNo.filter((k) => typeof k === "string") } : {}),
233
296
  ...(isRecord(raw.safetyNotes) ? { safetyNotes: Object.fromEntries(Object.entries(raw.safetyNotes).filter(([, v]) => isRecord(v) && typeof v.says === "string" && typeof v.fix === "string" && typeof v.at === "number" && typeof v.team === "string")) } : {}),
234
297
  };
@@ -457,23 +520,62 @@ function merge3(base, ours, theirs) {
457
520
  }
458
521
  return ours;
459
522
  }
523
+ export function setupMayGrantSelfUpgrade(p, fileExists) {
524
+ if (policyUnreadable(p))
525
+ return false;
526
+ if (fileExists && !p.born)
527
+ return false;
528
+ return !p.grants.self_upgrade && p.denied.self_upgrade === undefined && p.revoked?.self_upgrade === undefined
529
+ && p.usedOnce?.self_upgrade === undefined && !p.asks.self_upgrade;
530
+ }
531
+ export function selfUpgradeSetupDue(home, env = process.env) {
532
+ if (inWokenRun(env))
533
+ return false;
534
+ try {
535
+ return setupMayGrantSelfUpgrade(loadPolicy(home), existsSync(policyPath(home)));
536
+ }
537
+ catch {
538
+ return false;
539
+ }
540
+ }
541
+ export function grantSelfUpgradeAtSetup(home, env = process.env) {
542
+ if (inWokenRun(env))
543
+ return null;
544
+ try {
545
+ const p = loadPolicy(home);
546
+ if (!setupMayGrantSelfUpgrade(p, existsSync(policyPath(home))))
547
+ return null;
548
+ grant(p, "self_upgrade", "always", "setup-default");
549
+ savePolicy(p, home);
550
+ return SELF_UPGRADE_DISCLOSURE;
551
+ }
552
+ catch {
553
+ return null;
554
+ }
555
+ }
556
+ export const SELF_UPGRADE_DISCLOSURE = "The worker keeps pantheon itself current with your team's relay (fast-forward pull + rebuild, reported on the ledger). Revoke: pantheon policy revoke self-upgrade";
460
557
  export function useGrant(p, cap) {
461
558
  const g = p.grants[cap];
462
559
  if (!g)
463
560
  return false;
464
- if (g.mode === "once")
561
+ if (g.mode === "once") {
465
562
  delete p.grants[cap];
563
+ p.usedOnce = { ...(p.usedOnce ?? {}), [cap]: Date.now() };
564
+ }
466
565
  return true;
467
566
  }
468
567
  export function grant(p, cap, mode, by) {
469
568
  p.grants[cap] = { mode, by, at: Date.now() };
470
569
  delete p.asks[cap];
471
570
  delete p.denied[cap];
571
+ if (p.revoked)
572
+ delete p.revoked[cap];
472
573
  if (p.finalNo)
473
574
  p.finalNo = p.finalNo.filter((k) => k !== finalKey.cap(cap));
474
575
  }
475
576
  export function revoke(p, cap) {
476
577
  delete p.grants[cap];
578
+ p.revoked = { ...(p.revoked ?? {}), [cap]: Date.now() };
477
579
  if (cap === "unsandboxed_runs")
478
580
  p.denied[cap] = Date.now();
479
581
  }