@join-pantheon/cli 0.33.10 → 0.33.11-rc.2
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/board.js +53 -6
- package/dist/claude-confine.js +9 -2
- package/dist/cli.js +674 -34
- package/dist/client.js +14 -1
- package/dist/continued.js +4 -0
- package/dist/declined.js +67 -0
- package/dist/doctor.js +19 -3
- package/dist/home-guard.js +256 -5
- package/dist/home-safety.js +7 -1
- package/dist/mcp-instructions.js +15 -7
- package/dist/notice-linux.js +36 -10
- package/dist/notice-windows.js +21 -2
- package/dist/notifier.js +16 -2
- package/dist/pending.js +90 -7
- package/dist/pickup.js +6 -0
- package/dist/protocol.js +3 -1
- package/dist/quickstart.js +6 -3
- package/dist/relay.js +33 -3
- package/dist/run-control.js +72 -0
- package/dist/service.js +47 -0
- package/dist/setup-changes.js +521 -0
- package/dist/setup-guide.js +119 -17
- package/dist/spend.js +17 -2
- package/dist/tray.js +77 -6
- package/dist/uninstall.js +23 -13
- package/dist/uri-handler.js +20 -3
- package/dist/worker-policy.js +28 -0
- package/dist/worker.js +670 -65
- package/helper/macos/main.swift +209 -18
- package/helper/macos/menubar.swift +87 -3
- package/helper/macos/prebuilt/manifest.json +5 -5
- package/helper/macos/prebuilt/pantheon-hud +0 -0
- package/helper/macos/prebuilt/pantheon-tray +0 -0
- package/helper/windows/pantheon-notice.ps1 +6 -2
- package/helper/windows/pantheon-tray.ps1 +68 -5
- package/installer/install.ps1 +55 -0
- package/package.json +1 -1
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { appendFileSync, closeSync, mkdirSync, openSync, readFileSync, readSync, renameSync, statSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { CONFIG_PATH } from "./config.js";
|
|
5
|
+
import { argvHash, trustWrite } from "./home-guard.js";
|
|
6
|
+
import { AUTOPILOT_PLAN_NOTE, describeIdleSeconds, inWokenRun } from "./worker-policy.js";
|
|
7
|
+
import { parseJsonText } from "./text-file.js";
|
|
8
|
+
export function unsandboxedRisk(who = "Codex, Cursor and Copilot") {
|
|
9
|
+
const either = who.replace(/ and ([^,]+)$/, " or $1");
|
|
10
|
+
const one = !/,| or /.test(either);
|
|
11
|
+
return `When ${either} ${one ? "runs" : "run"} outside the sandbox, ${one ? "it" : "they"} can do anything you can on this computer — including changing Pantheon's own settings. Pantheon's setup protection stops honest mistakes; only Claude Code's sandbox can stop a misbehaving agent.`;
|
|
12
|
+
}
|
|
13
|
+
const AGENT_VARS = [
|
|
14
|
+
[/^CLAUDECODE$/, "claude-code"], [/^CLAUDE_CODE_ENTRYPOINT$/, "claude-code"],
|
|
15
|
+
[/^CODEX_(SANDBOX|SANDBOX_NETWORK_DISABLED|THREAD_ID|SESSION_ID|CI)$/, "codex"],
|
|
16
|
+
[/^CURSOR_AGENT$/, "cursor"],
|
|
17
|
+
[/^COPILOT_(AGENT|CLI|SESSION_ID|AGENT_SESSION_ID)$/, "copilot"],
|
|
18
|
+
[/^KIRO_(SESSION_ID|CI|AGENT)$/, "kiro"],
|
|
19
|
+
[/^GEMINI_CLI$/, "gemini"],
|
|
20
|
+
];
|
|
21
|
+
const PERSON_VARS = /^(CODEX_HOME|COPILOT_HOME|COPILOT_GITHUB_TOKEN|COPILOT_MODEL|KIRO_HOME|KIRO_DATA_DIR|KIRO_API_KEY|KIRO_NO_AUTO_UPDATE)$/;
|
|
22
|
+
export function whoIsAsking(env = process.env, isTTY = !!process.stdin.isTTY && !!process.stdout.isTTY) {
|
|
23
|
+
if (inWokenRun(env))
|
|
24
|
+
return { kind: "worker" };
|
|
25
|
+
for (const k of Object.keys(env)) {
|
|
26
|
+
if (!env[k] || PERSON_VARS.test(k))
|
|
27
|
+
continue;
|
|
28
|
+
const hit = AGENT_VARS.find(([re]) => re.test(k));
|
|
29
|
+
if (hit)
|
|
30
|
+
return { kind: "agent", agent: hit[1] };
|
|
31
|
+
}
|
|
32
|
+
if (env.AI_AGENT)
|
|
33
|
+
return { kind: "agent", agent: String(env.AI_AGENT).split(/[_\s]/)[0].slice(0, 24) || "agent" };
|
|
34
|
+
if (!isTTY)
|
|
35
|
+
return { kind: "agent", agent: "no terminal" };
|
|
36
|
+
return { kind: "terminal" };
|
|
37
|
+
}
|
|
38
|
+
export function withoutAgentMarkers(env) {
|
|
39
|
+
const out = {};
|
|
40
|
+
for (const [k, v] of Object.entries(env)) {
|
|
41
|
+
if (k === "AI_AGENT" || (!PERSON_VARS.test(k) && AGENT_VARS.some(([re]) => re.test(k))))
|
|
42
|
+
continue;
|
|
43
|
+
out[k] = v;
|
|
44
|
+
}
|
|
45
|
+
return out;
|
|
46
|
+
}
|
|
47
|
+
export function askedByLabel(a) {
|
|
48
|
+
return a.kind === "agent" ? `agent:${a.agent.replace(/\s+/g, "-")}` : a.kind;
|
|
49
|
+
}
|
|
50
|
+
export function askedByWords(label) {
|
|
51
|
+
if (label === "terminal")
|
|
52
|
+
return "from a terminal";
|
|
53
|
+
if (label === "worker")
|
|
54
|
+
return "from a woken run";
|
|
55
|
+
if (label === "alert")
|
|
56
|
+
return "from an alert";
|
|
57
|
+
if (label === "board")
|
|
58
|
+
return "from the board";
|
|
59
|
+
const a = label.replace(/^agent:/, "");
|
|
60
|
+
const names = { "claude-code": "Claude Code", codex: "Codex", cursor: "Cursor", copilot: "Copilot", kiro: "Kiro", gemini: "Gemini CLI", "no-terminal": "a command with no terminal (an agent or a script)" };
|
|
61
|
+
return `from ${names[a] ?? `an agent (${a})`}`;
|
|
62
|
+
}
|
|
63
|
+
const WAKE_RANK = { no: 0, approved: 1, anyone: 2 };
|
|
64
|
+
const wakeNow = {
|
|
65
|
+
anyone: "any teammate can hand work to your agent while you're away — that's what keeps the team moving without you",
|
|
66
|
+
approved: "only teammates you approve can hand work to your agent while you're away",
|
|
67
|
+
no: "nobody can hand work to your agent while you're away; teammates' requests wait for you",
|
|
68
|
+
};
|
|
69
|
+
export const CONSEQUENCES = {
|
|
70
|
+
mode: (c) => c.to === "here"
|
|
71
|
+
? "Right now your agent finishes teammates' requests on its own — that's what keeps work moving while you're busy or away. After this change, requests wait for you when you're at your computer."
|
|
72
|
+
: `Right now teammates' requests wait for you while you're at your computer. After this change your agent takes every request at once and finishes it on its own, even while you work, so nothing waits on you. ${AUTOPILOT_PLAN_NOTE}`,
|
|
73
|
+
wake: (c) => (WAKE_RANK[c.to] ?? 2) < (WAKE_RANK[c.from] ?? 2)
|
|
74
|
+
? `Right now ${wakeNow[c.from] ?? wakeNow.anyone}. After this change ${c.to === "no" ? "nobody can, and every request waits until you're back at your computer" : "only teammates you approve can — you're asked the first time each one needs it, and the rest wait for you"}.`
|
|
75
|
+
: `Right now ${wakeNow[c.from] ?? wakeNow.no}. After this change ${c.to === "anyone" ? "any teammate can, so work keeps moving even when you're away" : "teammates you approve can, so their work keeps moving even when you're away"}.`,
|
|
76
|
+
waker: (c) => c.to === "allowed"
|
|
77
|
+
? `Right now ${c.about ?? "this teammate"} can't start work on your machine while you're away. After this change they can, so their requests get answered without waiting for you.`
|
|
78
|
+
: `Right now ${c.about ?? "this teammate"} can start work on your machine while you're away, so their requests get answered without waiting for you. After this change their requests wait until you're back.`,
|
|
79
|
+
commands: (c) => c.to === "on"
|
|
80
|
+
? "Right now work your agent does for teammates can edit files and answer, but can't run tests, commit or push. After this change it runs your tests and commits and pushes to its own branch, so work arrives tested and ready for your review."
|
|
81
|
+
: "Right now work your agent does for teammates runs your tests and commits and pushes to its own branch, so it arrives tested and ready for your review. After this change it can only edit files and answer — deliveries will wait for you to commit and push.",
|
|
82
|
+
unsandboxed: (c) => {
|
|
83
|
+
const who = c.about || "Codex, Cursor and Copilot";
|
|
84
|
+
const one = !who.includes(" and ") && !who.includes(",");
|
|
85
|
+
return c.to === "on"
|
|
86
|
+
? `Right now ${who} ${one ? "leaves" : "leave"} teammates' requests for you. After this change ${one ? "it answers" : "they answer"} them while you're away too, so nobody waits on you. ${unsandboxedRisk(who)}`
|
|
87
|
+
: `Right now ${who} ${one ? "answers" : "answer"} teammates' requests while you're away, so nobody waits on you. After this change those requests wait for you instead.`;
|
|
88
|
+
},
|
|
89
|
+
"self-upgrade": (c) => c.to === "on"
|
|
90
|
+
? "Right now you update Pantheon on this machine yourself. After this change your worker keeps it up to date with your team on its own."
|
|
91
|
+
: "Right now your worker keeps Pantheon up to date with your team on its own, so you're never left behind. After this change you update it yourself; until you do, new features and fixes wait.",
|
|
92
|
+
"run-minutes": (c) => Number(c.to) > Number(c.from)
|
|
93
|
+
? `Right now one piece of work may take up to ${c.from} minutes. After this change it may take up to ${c.to} minutes, so bigger requests can finish in one go (a longer run uses more of your agent plan).`
|
|
94
|
+
: `Right now one piece of work may take up to ${c.from} minutes, so bigger requests can finish in one go. After this change it stops after ${c.to} minutes, and bigger requests may come back to you unfinished.`,
|
|
95
|
+
"idle-seconds": (c) => {
|
|
96
|
+
const quiet = (x) => (Number(x) === 0 ? "as soon as you stop typing" : `after you've been quiet for ${describeIdleSeconds(Number(x))}`);
|
|
97
|
+
return Number(c.to) < Number(c.from)
|
|
98
|
+
? `Right now your worker takes a teammate's request ${quiet(c.from)}. After this change it takes it ${quiet(c.to)}, so teammates get answers sooner.`
|
|
99
|
+
: `Right now your worker takes a teammate's request ${quiet(c.from)}, so teammates get answers soon. After this change it waits until ${quiet(c.to).replace(/^after /, "")}.`;
|
|
100
|
+
},
|
|
101
|
+
alerts: (c) => {
|
|
102
|
+
if (c.to === "none")
|
|
103
|
+
return `Right now you get a desktop alert for ${c.from}, so nothing that needs you waits long. After this change you get no alerts; decisions wait in pantheon status and the board until you look.`;
|
|
104
|
+
if (c.from === "none")
|
|
105
|
+
return `Right now you get no desktop alerts. After this change you get one for ${c.to}, so you hear about what needs you right away.`;
|
|
106
|
+
const rank = (x) => ["decisions that need you", "decisions and deliveries", "decisions, deliveries and your worker's finished runs"].indexOf(x);
|
|
107
|
+
return rank(c.to) > rank(c.from)
|
|
108
|
+
? `Right now you get a desktop alert for ${c.from}. After this change you get one for ${c.to} too, so you can review work the moment it is ready.`
|
|
109
|
+
: `Right now you get a desktop alert for ${c.from}, so you can review work the moment it is ready. After this change you get one only for ${c.to}; the rest waits on the board.`;
|
|
110
|
+
},
|
|
111
|
+
decides: (c) => c.to === "yours"
|
|
112
|
+
? `Right now agents may decide ${c.about ?? "this"} without stopping. After this change every ${c.about ?? "such"} decision waits for you personally — nothing in it happens without you, and nothing in it moves while you're away.`
|
|
113
|
+
: `Right now every ${c.about ?? "such"} decision waits for you personally, so nothing in it happens without you. After this change agents may decide ${c.about ?? "it"} without asking you, so that work keeps moving.`,
|
|
114
|
+
persona: (c) => c.about === "playbook"
|
|
115
|
+
? "Right now your worker works from your setup alone. After this change it also follows a short starter playbook written into this repo, which says what it may do there; you can edit it any time."
|
|
116
|
+
: c.from === "any request"
|
|
117
|
+
? `Right now your worker takes any teammate's request it is allowed to. After this change it takes only what the persona "${c.about ?? c.to}" covers — requests no persona covers will wait for you.`
|
|
118
|
+
: `Right now your worker follows ${c.from}. After this change it also follows the persona "${c.about ?? c.to}" — requests none of them covers will wait for you.`,
|
|
119
|
+
agent: (c) => `Right now teammates' requests are answered with ${agentLabel(c.from)}. After this change they are answered with ${agentLabel(c.to)}.`,
|
|
120
|
+
hooks: (c) => c.to === "on"
|
|
121
|
+
? "Right now your agent windows don't tell Pantheon when you're working, and don't see teammates' replies when a turn ends. After this change they do, so your agent keeps going when a teammate is waiting on it."
|
|
122
|
+
: "Right now your agent windows tell Pantheon when you're working and pick up teammates' replies when a turn ends, so nobody waits on a window that went quiet. After this change they don't.",
|
|
123
|
+
worker: (c) => c.to === "off"
|
|
124
|
+
? "Right now your worker answers teammates while you're away, so nothing waits on you. After this change it stops, and every request waits until you're back at your computer."
|
|
125
|
+
: c.from === "off"
|
|
126
|
+
? "Right now nothing answers teammates while you're away. After this change your worker does, so their work keeps moving without you."
|
|
127
|
+
: `Right now your worker runs with: ${c.from}. After this change: ${c.to}.`,
|
|
128
|
+
folder: (c) => c.to === "unbound"
|
|
129
|
+
? `Right now ${c.about ?? "this folder"} is ${c.from === "bound" ? "part of your team" : c.from} on this machine, so your worker serves it and agents opened there speak for the team. After this change it isn't, and requests about it wait for you.`
|
|
130
|
+
: c.from.startsWith("bound to ")
|
|
131
|
+
? `Right now ${c.about ?? "this folder"} is ${c.from}: agents opened there speak for that team. After this change it is ${c.to}, and your worker serves it there instead.`
|
|
132
|
+
: `Right now ${c.about ?? "this folder"} isn't part of your team on this machine. After this change it is ${c.to}: agents opened there speak for the team, and your worker can answer teammates' requests about it while you're away.`,
|
|
133
|
+
base: (c) => /default|uses now/.test(c.from)
|
|
134
|
+
? `Right now your worker starts its work from the branch origin calls its default. After this change it starts from ${c.to}, and deliveries are checked against ${c.to}.`
|
|
135
|
+
: `Right now your worker starts its work from ${c.from}, and deliveries are checked against it. After this change it starts from ${c.to}, and deliveries are checked against ${c.to}.`,
|
|
136
|
+
};
|
|
137
|
+
export function describeWorkerFlags(args) {
|
|
138
|
+
if (args === null)
|
|
139
|
+
return "off";
|
|
140
|
+
const words = [];
|
|
141
|
+
for (let i = 0; i < args.length; i++) {
|
|
142
|
+
const a = args[i], v = args[i + 1];
|
|
143
|
+
const take = () => { i++; return v ?? ""; };
|
|
144
|
+
if (a === "--allow-bash")
|
|
145
|
+
words.push("runs your tests, commits and pushes");
|
|
146
|
+
else if (a === "--agent")
|
|
147
|
+
words.push(`answers with ${take()}`);
|
|
148
|
+
else if (a === "--persona")
|
|
149
|
+
words.push(`follows the persona ${take()}`);
|
|
150
|
+
else if (a === "--run-minutes")
|
|
151
|
+
words.push(`stops a run after ${take()} minutes`);
|
|
152
|
+
else if (a === "--project")
|
|
153
|
+
words.push(`serves only ${take()}`);
|
|
154
|
+
else if (a === "--max-runs")
|
|
155
|
+
words.push(`at most ${take()} runs an hour`);
|
|
156
|
+
else if (a === "--max-runs-day")
|
|
157
|
+
words.push(`at most ${take()} runs a day`);
|
|
158
|
+
else if (a === "--idle")
|
|
159
|
+
words.push(`waits ${take()} seconds of quiet`);
|
|
160
|
+
else if (a === "--no-continue")
|
|
161
|
+
words.push("does not pick up replies on its own");
|
|
162
|
+
else if (a.startsWith("--"))
|
|
163
|
+
words.push(`a setting of its own (${a}${v && !v.startsWith("--") ? ` ${take()}` : ""})`);
|
|
164
|
+
}
|
|
165
|
+
const set = [...new Set(words)].sort();
|
|
166
|
+
return set.length ? set.join("; ") : "its defaults (edits and answers only)";
|
|
167
|
+
}
|
|
168
|
+
export function consequence(c) {
|
|
169
|
+
return CONSEQUENCES[c.setting](c);
|
|
170
|
+
}
|
|
171
|
+
function valueWords(c, v) {
|
|
172
|
+
if (c.setting === "idle-seconds" && /^\d+$/.test(v))
|
|
173
|
+
return describeIdleSeconds(Number(v));
|
|
174
|
+
if (c.setting === "run-minutes" && /^\d+$/.test(v))
|
|
175
|
+
return `${v} minutes`;
|
|
176
|
+
if (c.setting === "agent")
|
|
177
|
+
return agentLabel(v);
|
|
178
|
+
return v;
|
|
179
|
+
}
|
|
180
|
+
export function agentLabel(id) {
|
|
181
|
+
const names = { "claude-code": "Claude Code", codex: "Codex", cursor: "Cursor", copilot: "Copilot", kiro: "Kiro" };
|
|
182
|
+
return names[id] ?? id;
|
|
183
|
+
}
|
|
184
|
+
const SETTING_NAMES = {
|
|
185
|
+
mode: "Mode", persona: "Playbook", decides: "Decided by", wake: "Who may hand you work", waker: "May hand you work", commands: "Commands in runs",
|
|
186
|
+
unsandboxed: "Codex, Cursor and Copilot runs", "self-upgrade": "Keeping Pantheon up to date", "run-minutes": "Longest run", "idle-seconds": "Wait while you type",
|
|
187
|
+
alerts: "Alerts", agent: "Agent for requests", hooks: "Agent window hooks", worker: "Worker", folder: "Folder", base: "Base branch",
|
|
188
|
+
};
|
|
189
|
+
export function changeLabel(c) {
|
|
190
|
+
return `${settingName(c)}: ${valueWords(c, c.from)} → ${valueWords(c, c.to)}`;
|
|
191
|
+
}
|
|
192
|
+
const homeOf = (home) => home ?? process.env.PANTHEON_HOME ?? dirname(CONFIG_PATH);
|
|
193
|
+
export const changesLogPath = (home) => join(homeOf(home), "setup-changes.log");
|
|
194
|
+
export const pendingChangesPath = (home) => join(homeOf(home), "setup-pending.json");
|
|
195
|
+
export const CHANGE_LOG_MAX = 1000;
|
|
196
|
+
const CHANGE_LOG_TRIM_BYTES = 512 * 1024;
|
|
197
|
+
export function logChanges(entries, home) {
|
|
198
|
+
if (!entries.length)
|
|
199
|
+
return;
|
|
200
|
+
try {
|
|
201
|
+
const f = changesLogPath(home);
|
|
202
|
+
mkdirSync(dirname(f), { recursive: true });
|
|
203
|
+
appendFileSync(f, entries.map((e) => JSON.stringify(e)).join("\n") + "\n", { mode: 0o600 });
|
|
204
|
+
if (statSync(f).size > CHANGE_LOG_TRIM_BYTES) {
|
|
205
|
+
const lines = readFileSync(f, "utf8").split("\n").filter((l) => l.trim());
|
|
206
|
+
const first = lines.find((l) => l.includes(`"setting":"${FIRST_SETUP}"`));
|
|
207
|
+
const rest = lines.filter((l) => l !== first).slice(-CHANGE_LOG_MAX);
|
|
208
|
+
const tmp = `${f}.${process.pid}.tmp`;
|
|
209
|
+
writeFileSync(tmp, [...(first ? [first] : []), ...rest].join("\n") + "\n", { mode: 0o600 });
|
|
210
|
+
renameSync(tmp, f);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
catch { }
|
|
214
|
+
}
|
|
215
|
+
export function readChangeLog(home, head) {
|
|
216
|
+
let text = "";
|
|
217
|
+
try {
|
|
218
|
+
if (head === undefined)
|
|
219
|
+
text = readFileSync(changesLogPath(home), "utf8");
|
|
220
|
+
else {
|
|
221
|
+
const fd = openSync(changesLogPath(home), "r");
|
|
222
|
+
try {
|
|
223
|
+
const buf = Buffer.alloc(head);
|
|
224
|
+
const n = readSync(fd, buf, 0, head, 0);
|
|
225
|
+
text = buf.subarray(0, n).toString("utf8");
|
|
226
|
+
}
|
|
227
|
+
finally {
|
|
228
|
+
closeSync(fd);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
catch {
|
|
233
|
+
return [];
|
|
234
|
+
}
|
|
235
|
+
const out = [];
|
|
236
|
+
for (const line of text.split("\n")) {
|
|
237
|
+
if (!line.trim())
|
|
238
|
+
continue;
|
|
239
|
+
try {
|
|
240
|
+
const e = JSON.parse(line);
|
|
241
|
+
if (typeof e.at === "number" && typeof e.setting === "string" && typeof e.outcome === "string")
|
|
242
|
+
out.push(e);
|
|
243
|
+
}
|
|
244
|
+
catch { }
|
|
245
|
+
}
|
|
246
|
+
return out;
|
|
247
|
+
}
|
|
248
|
+
export function lastChangeLine(home, now = Date.now()) {
|
|
249
|
+
const done = readChangeLog(home).filter((e) => (e.outcome === "applied" || e.outcome === "approved") && e.setting !== FIRST_SETUP);
|
|
250
|
+
const e = done[done.length - 1];
|
|
251
|
+
if (!e)
|
|
252
|
+
return null;
|
|
253
|
+
return `setup last changed ${ago(now - e.at)}: ${changeLabel({ setting: e.setting, from: e.from, to: e.to, ...(e.about ? { about: e.about } : {}) })}, ${askedByWords(e.asked_by)}${e.approved_by ? ` (approved ${askedByWords(e.approved_by)})` : ""}`;
|
|
254
|
+
}
|
|
255
|
+
export function ago(ms) {
|
|
256
|
+
const m = Math.round(ms / 60_000);
|
|
257
|
+
if (m < 1)
|
|
258
|
+
return "just now";
|
|
259
|
+
if (m < 60)
|
|
260
|
+
return `${m}m ago`;
|
|
261
|
+
const h = Math.round(m / 60);
|
|
262
|
+
return h < 48 ? `${h}h ago` : `${Math.round(h / 24)}d ago`;
|
|
263
|
+
}
|
|
264
|
+
const REPLAYABLE = {
|
|
265
|
+
setup: null, mode: null, use: null, persona: ["init"], repo: ["add"], profile: ["set"],
|
|
266
|
+
policy: ["set", "grant", "revoke"], wake: ["allow", "deny"], hook: ["install", "uninstall"], service: ["install", "uninstall"],
|
|
267
|
+
};
|
|
268
|
+
const REPLAY_FLAGS = {
|
|
269
|
+
setup: new Set(["--yes", "--mode", "--wake", "--commands", "--unsandboxed", "--decides", "--alerts", "--playbook", "--base"]),
|
|
270
|
+
profile: new Set(["--human-required", "--agent-can-decide", "--surface", "--space"]),
|
|
271
|
+
};
|
|
272
|
+
export function replayAllowed(argv) {
|
|
273
|
+
const [a, b] = argv;
|
|
274
|
+
if (!a || !Object.prototype.hasOwnProperty.call(REPLAYABLE, a))
|
|
275
|
+
return false;
|
|
276
|
+
const subs = REPLAYABLE[a];
|
|
277
|
+
if (subs && (!b || !subs.includes(b)))
|
|
278
|
+
return false;
|
|
279
|
+
const only = REPLAY_FLAGS[a];
|
|
280
|
+
if (only && argv.some((x) => x.startsWith("-") && !only.has(x.split("=")[0])))
|
|
281
|
+
return false;
|
|
282
|
+
return argv.every((x) => typeof x === "string" && !x.includes("\0"));
|
|
283
|
+
}
|
|
284
|
+
export { argvHash };
|
|
285
|
+
export function loadPending(home, now = Date.now()) {
|
|
286
|
+
try {
|
|
287
|
+
const raw = parseJsonText(readFileSync(pendingChangesPath(home), "utf8"));
|
|
288
|
+
const list = Array.isArray(raw?.pending) ? raw.pending : [];
|
|
289
|
+
return list.filter((p) => !!p && typeof p === "object" && typeof p.id === "string"
|
|
290
|
+
&& Array.isArray(p.changes) && Array.isArray(p.argv) && typeof p.cwd === "string"
|
|
291
|
+
&& typeof p.at === "number" && now - p.at < PENDING_MAX_MS);
|
|
292
|
+
}
|
|
293
|
+
catch {
|
|
294
|
+
return [];
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
export function savePending(list, home) {
|
|
298
|
+
const f = pendingChangesPath(home);
|
|
299
|
+
mkdirSync(dirname(f), { recursive: true });
|
|
300
|
+
let prev = null;
|
|
301
|
+
try {
|
|
302
|
+
prev = readFileSync(f);
|
|
303
|
+
}
|
|
304
|
+
catch { }
|
|
305
|
+
const tmp = `${f}.${process.pid}.tmp`;
|
|
306
|
+
writeFileSync(tmp, JSON.stringify({ pending: list }, null, 2) + "\n", { mode: 0o600 });
|
|
307
|
+
renameSync(tmp, f);
|
|
308
|
+
trustWrite(f, prev);
|
|
309
|
+
}
|
|
310
|
+
export const PENDING_MAX_MS = 7 * 24 * 3600_000;
|
|
311
|
+
export const PENDING_MAX = 50;
|
|
312
|
+
const changeKey = (c) => `${c.setting}|${c.about ?? ""}`;
|
|
313
|
+
export function addPending(p, home, now = Date.now()) {
|
|
314
|
+
if (!replayAllowed(p.argv))
|
|
315
|
+
throw new Error(`"pantheon ${p.argv.slice(0, 2).join(" ")}" is not a command a setup change can be approved for`);
|
|
316
|
+
const list = loadPending(home, now);
|
|
317
|
+
const same = list.find((x) => sameChanges(x.changes, p.changes) && x.cwd === p.cwd && argvHash(x.argv) === argvHash(p.argv));
|
|
318
|
+
if (same)
|
|
319
|
+
return { entry: same, fresh: false };
|
|
320
|
+
const keys = new Set(p.changes.map(changeKey));
|
|
321
|
+
const replaced = list.filter((x) => x.changes.some((c) => keys.has(changeKey(c))));
|
|
322
|
+
const kept = list.filter((x) => !replaced.includes(x));
|
|
323
|
+
if (kept.length >= PENDING_MAX)
|
|
324
|
+
return { full: true };
|
|
325
|
+
const entry = { id: `c_${randomBytes(4).toString("hex")}`, at: now, ...p };
|
|
326
|
+
savePending([...kept, entry], home);
|
|
327
|
+
logReplaced(replaced, home, now);
|
|
328
|
+
return { entry, fresh: true };
|
|
329
|
+
}
|
|
330
|
+
function logReplaced(replaced, home, now) {
|
|
331
|
+
logChanges(replaced.flatMap((x) => x.changes.map((c) => ({ at: now, setting: c.setting, from: c.from, to: c.to, ...(c.about ? { about: c.about } : {}), asked_by: x.asked_by, outcome: "replaced", id: x.id }))), home);
|
|
332
|
+
}
|
|
333
|
+
export function dropMoot(same, home, now = Date.now()) {
|
|
334
|
+
if (!same.length)
|
|
335
|
+
return 0;
|
|
336
|
+
const keys = new Set(same.map(changeKey));
|
|
337
|
+
const list = loadPending(home, now);
|
|
338
|
+
const moot = list.filter((x) => x.changes.some((c) => keys.has(changeKey(c))));
|
|
339
|
+
if (!moot.length)
|
|
340
|
+
return 0;
|
|
341
|
+
savePending(list.filter((x) => !moot.includes(x)), home);
|
|
342
|
+
logReplaced(moot, home, now);
|
|
343
|
+
return moot.length;
|
|
344
|
+
}
|
|
345
|
+
export function findPending(id, home) {
|
|
346
|
+
return loadPending(home).find((x) => x.id === id);
|
|
347
|
+
}
|
|
348
|
+
export function dropPending(id, home) {
|
|
349
|
+
const list = loadPending(home);
|
|
350
|
+
if (list.some((x) => x.id === id))
|
|
351
|
+
savePending(list.filter((x) => x.id !== id), home);
|
|
352
|
+
}
|
|
353
|
+
export const APPROVED_ENV = "PANTHEON_CHANGE_APPROVED";
|
|
354
|
+
export function beginApproval(id, by, home, now = Date.now()) {
|
|
355
|
+
const list = loadPending(home, now);
|
|
356
|
+
const e = list.find((x) => x.id === id);
|
|
357
|
+
if (!e || !replayAllowed(e.argv))
|
|
358
|
+
return null;
|
|
359
|
+
const nonce = randomBytes(12).toString("hex");
|
|
360
|
+
e.approving = { nonce, at: now, by, argv: argvHash(e.argv) };
|
|
361
|
+
savePending(list, home);
|
|
362
|
+
return `${id}:${nonce}`;
|
|
363
|
+
}
|
|
364
|
+
export function approvedRun(env = process.env, home, now = Date.now(), argv = process.argv.slice(2)) {
|
|
365
|
+
const v = env[APPROVED_ENV];
|
|
366
|
+
if (!v)
|
|
367
|
+
return null;
|
|
368
|
+
const [id, nonce] = v.split(":");
|
|
369
|
+
const e = id ? findPending(id, home) : undefined;
|
|
370
|
+
if (!e?.approving || e.approving.nonce !== nonce || now - e.approving.at > 120_000)
|
|
371
|
+
return null;
|
|
372
|
+
if (!replayAllowed(argv) || argvHash(argv) !== argvHash(e.argv) || e.approving.argv !== argvHash(argv))
|
|
373
|
+
return null;
|
|
374
|
+
return e;
|
|
375
|
+
}
|
|
376
|
+
export function approvalApplied(id, since, home) {
|
|
377
|
+
return readChangeLog(home).some((e) => e.id === id && e.outcome === "approved" && e.at >= since - 1000);
|
|
378
|
+
}
|
|
379
|
+
export function outOfDateLine(entry, since, home) {
|
|
380
|
+
const c = entry.changes[0];
|
|
381
|
+
const seen = readChangeLog(home).filter((e) => e.id === entry.id && e.outcome === "out of date" && e.at >= since - 1000);
|
|
382
|
+
const hit = seen.find((e) => e.setting === c.setting && (e.about ?? "") === (c.about ?? ""));
|
|
383
|
+
const now = hit ? hit.from : c.to;
|
|
384
|
+
return `That change is out of date — ${settingName(c)} is now ${valueWords(c, now)}. Nothing changed.`;
|
|
385
|
+
}
|
|
386
|
+
export function settingName(c) {
|
|
387
|
+
if (c.setting === "unsandboxed" && c.about)
|
|
388
|
+
return `${c.about} runs`;
|
|
389
|
+
return `${SETTING_NAMES[c.setting] ?? c.setting}${c.about ? ` (${c.about})` : ""}`;
|
|
390
|
+
}
|
|
391
|
+
export function sameChanges(a, b) {
|
|
392
|
+
const key = (x) => JSON.stringify([...x].map((c) => [c.setting, c.about ?? "", c.from, c.to]).sort());
|
|
393
|
+
return key(a) === key(b);
|
|
394
|
+
}
|
|
395
|
+
export async function gateChanges(g) {
|
|
396
|
+
const env = g.env ?? process.env;
|
|
397
|
+
if (!g.changes.length)
|
|
398
|
+
return { go: true };
|
|
399
|
+
const who = whoIsAsking(env, g.isTTY);
|
|
400
|
+
const asked = askedByLabel(who);
|
|
401
|
+
const same = g.changes.filter((c) => c.from === c.to);
|
|
402
|
+
const changes = g.changes.filter((c) => c.from !== c.to);
|
|
403
|
+
const log = (outcome, extra = {}) => logChanges(changes.map((c) => ({ at: Date.now(), setting: c.setting, from: c.from, to: c.to, ...(c.about ? { about: c.about } : {}), asked_by: asked, outcome, ...extra })), g.home);
|
|
404
|
+
if (who.kind === "worker") {
|
|
405
|
+
if (!changes.length)
|
|
406
|
+
return { go: true };
|
|
407
|
+
log("refused");
|
|
408
|
+
return { go: false, why: "refused" };
|
|
409
|
+
}
|
|
410
|
+
if (same.length && !env[APPROVED_ENV])
|
|
411
|
+
dropMoot(same, g.home);
|
|
412
|
+
if (!changes.length)
|
|
413
|
+
return { go: true };
|
|
414
|
+
const approved = approvedRun(env, g.home, Date.now(), g.processArgv ?? process.argv.slice(2));
|
|
415
|
+
if (approved) {
|
|
416
|
+
dropPending(approved.id, g.home);
|
|
417
|
+
if (!sameChanges(approved.changes, changes)) {
|
|
418
|
+
logChanges(changes.map((c) => ({ at: Date.now(), setting: c.setting, from: c.from, to: c.to, asked_by: approved.asked_by, outcome: "out of date", id: approved.id })), g.home);
|
|
419
|
+
return { go: false, why: "out of date" };
|
|
420
|
+
}
|
|
421
|
+
logChanges(changes.map((c) => ({ at: Date.now(), setting: c.setting, from: c.from, to: c.to, ...(c.about ? { about: c.about } : {}), asked_by: approved.asked_by, outcome: "approved", approved_by: approved.approving.by, id: approved.id })), g.home);
|
|
422
|
+
return { go: true, approvedBy: approved.approving.by, id: approved.id };
|
|
423
|
+
}
|
|
424
|
+
if (!g.setUp) {
|
|
425
|
+
log("applied");
|
|
426
|
+
return { go: true };
|
|
427
|
+
}
|
|
428
|
+
if (who.kind === "agent" && !replayAllowed(g.argv)) {
|
|
429
|
+
log("refused");
|
|
430
|
+
return { go: false, why: "not replayable" };
|
|
431
|
+
}
|
|
432
|
+
if (who.kind === "agent") {
|
|
433
|
+
const r = addPending({ changes, argv: [...g.argv], cwd: g.cwd, asked_by: asked }, g.home);
|
|
434
|
+
if ("full" in r) {
|
|
435
|
+
log("refused");
|
|
436
|
+
return { go: false, why: "full" };
|
|
437
|
+
}
|
|
438
|
+
const { entry, fresh } = r;
|
|
439
|
+
if (fresh) {
|
|
440
|
+
log("pending", { id: entry.id });
|
|
441
|
+
try {
|
|
442
|
+
g.alert?.(entry);
|
|
443
|
+
}
|
|
444
|
+
catch { }
|
|
445
|
+
}
|
|
446
|
+
return { go: false, why: "pending", entry, fresh };
|
|
447
|
+
}
|
|
448
|
+
if (!g.yes && g.confirm) {
|
|
449
|
+
const ok = await g.confirm(changes.map((c) => consequence(c)));
|
|
450
|
+
if (!ok) {
|
|
451
|
+
log("declined");
|
|
452
|
+
return { go: false, why: "declined" };
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
log("applied");
|
|
456
|
+
return { go: true };
|
|
457
|
+
}
|
|
458
|
+
export function pendingSaid(name, entry, alerted) {
|
|
459
|
+
const them = name === "you" ? "you" : name;
|
|
460
|
+
return [
|
|
461
|
+
`Asked ${name} to confirm — nothing changed yet.`,
|
|
462
|
+
...entry.changes.map((c) => ` ${changeLabel(c)}`),
|
|
463
|
+
alerted
|
|
464
|
+
? ` ${them === "you" ? "You see" : `${them} sees`} it as an alert on this computer, and in: pantheon changes (it is ${entry.id}).`
|
|
465
|
+
: ` ${them === "you" ? "You see" : `${them} sees`} it in: pantheon changes (it is ${entry.id}).`,
|
|
466
|
+
` Do not change Pantheon's setup any other way; ${them === "you" ? "you decide" : `${them} decides`} it.`,
|
|
467
|
+
];
|
|
468
|
+
}
|
|
469
|
+
export const FIRST_SETUP_GRACE_MS = 30 * 60_000;
|
|
470
|
+
export const FIRST_SETUP = "first-setup";
|
|
471
|
+
export function firstSetupAt(home) {
|
|
472
|
+
return readChangeLog(home, 64 * 1024).find((e) => e.setting === FIRST_SETUP)?.at ?? null;
|
|
473
|
+
}
|
|
474
|
+
export function markFirstSetup(askedBy, home, now = Date.now()) {
|
|
475
|
+
if (firstSetupAt(home) !== null)
|
|
476
|
+
return;
|
|
477
|
+
logChanges([{ at: now, setting: FIRST_SETUP, from: "not set up", to: "set up", asked_by: askedBy, outcome: "applied" }], home);
|
|
478
|
+
}
|
|
479
|
+
export function machineSetUp(o) {
|
|
480
|
+
try {
|
|
481
|
+
const p = JSON.parse(readFileSync(o.profileFile, "utf8"));
|
|
482
|
+
if (typeof p?.agent?.wake_default !== "string")
|
|
483
|
+
return false;
|
|
484
|
+
const first = firstSetupAt(o.home);
|
|
485
|
+
return first === null || (o.now ?? Date.now()) - first >= FIRST_SETUP_GRACE_MS;
|
|
486
|
+
}
|
|
487
|
+
catch {
|
|
488
|
+
return false;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
export const SETUP_ALERT_TAG = "pantheon_setup_changes";
|
|
492
|
+
export function askedByName(label) {
|
|
493
|
+
return askedByWords(label).replace(/^from /, "").replace(/^a command with no terminal.*$/, "A command with no terminal").replace(/^./, (c) => c.toUpperCase());
|
|
494
|
+
}
|
|
495
|
+
export function composeSetupAlert(pending, o) {
|
|
496
|
+
const all = pending.flatMap((p) => p.changes.map((c) => ({ p, c })));
|
|
497
|
+
if (!all.length)
|
|
498
|
+
return null;
|
|
499
|
+
const askers = [...new Set(pending.map((p) => askedByName(p.asked_by)))];
|
|
500
|
+
const who = askers.length === 1 ? `${askers[0]} asked` : "Your agents asked";
|
|
501
|
+
const base = { kind: "setup", who, ttl: 0, sound: true, tag: SETUP_ALERT_TAG };
|
|
502
|
+
const lead = o.buttons ? "" : "In a terminal: pantheon changes — then approve or keep each one.\n";
|
|
503
|
+
if (all.length === 1) {
|
|
504
|
+
const { p, c } = all[0];
|
|
505
|
+
const body = `${lead}${consequence(c)}`;
|
|
506
|
+
const fits = o.room === undefined || body.length <= o.room;
|
|
507
|
+
return { ...base, heading: "Confirm a change to your setup", context: changeLabel(c), body,
|
|
508
|
+
...(o.buttons ? (fits ? { change: { id: p.id } } : { review: { what: "changes" } }) : {}) };
|
|
509
|
+
}
|
|
510
|
+
const subject = askers.length === 1 ? (askers[0] === "A command with no terminal" ? "An agent" : askers[0]) : "Your agents";
|
|
511
|
+
const shown = all.slice(0, ALERT_LIST_MAX);
|
|
512
|
+
const more = all.length - shown.length;
|
|
513
|
+
return { ...base, heading: `${subject} ${askers.length === 1 && subject !== "Your agents" ? "wants" : "want"} to change ${all.length} settings`, context: "Nothing changes until you review them",
|
|
514
|
+
body: `${lead}${shown.map(({ c }) => `• ${changeLabel(c)}`).join("\n")}${more ? `\nand ${more} more` : ""}`,
|
|
515
|
+
...(o.buttons ? { review: { what: "changes" } } : {}) };
|
|
516
|
+
}
|
|
517
|
+
export const ALERT_LIST_MAX = 8;
|
|
518
|
+
export function composeSetupChange(entry, o) {
|
|
519
|
+
return composeSetupAlert([entry], o);
|
|
520
|
+
}
|
|
521
|
+
//# sourceMappingURL=setup-changes.js.map
|