@agentguard-run/burn 0.6.2 → 0.7.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.
@@ -0,0 +1,232 @@
1
+ "use strict";
2
+ /**
3
+ * Removing AgentGuard's standalone Burn hook from a host's hooks file
4
+ * (~/.codex/hooks.json, ~/.claude/settings.json) once the AgentGuard plugin,
5
+ * which includes the same gate, is installed there.
6
+ *
7
+ * The file is the person's. Other hooks, other settings, their spacing and
8
+ * their key order stay exactly as written: instead of parsing and
9
+ * re-serializing, the file is parsed with byte positions and only the arrays
10
+ * and objects that lose an entry are rebuilt, from the original bytes of the
11
+ * entries they keep. A backup is taken first and the write is atomic.
12
+ */
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.isStandaloneBurnGate = isStandaloneBurnGate;
15
+ exports.removeStandaloneBurnHooks = removeStandaloneBurnHooks;
16
+ exports.countStandaloneBurnHooks = countStandaloneBurnHooks;
17
+ exports.removeStandaloneBurnHooksFromFile = removeStandaloneBurnHooksFromFile;
18
+ const node_fs_1 = require("node:fs");
19
+ /**
20
+ * A Burn gate command as `init claude|codex --write` writes it (or the npx and
21
+ * bare-command forms): Burn's CLI followed by `hook` or `codex-hook`. Other
22
+ * commands that mention Burn, such as `npx agentguard-burn why`, are not gates.
23
+ */
24
+ const BURN_GATE = /(?:agentguard-burn|agentguard-run\/burn)\S*\s+(?:hook|codex-hook)$/;
25
+ function isStandaloneBurnGate(command) {
26
+ return typeof command === 'string' && BURN_GATE.test(command.trim());
27
+ }
28
+ /** A JSON parser that keeps where every value starts and ends. Input is already known to be valid JSON. */
29
+ function parseWithPositions(text) {
30
+ let i = 0;
31
+ const ws = () => {
32
+ while (i < text.length && (text[i] === ' ' || text[i] === '\t' || text[i] === '\n' || text[i] === '\r'))
33
+ i++;
34
+ };
35
+ const fail = (what) => {
36
+ throw new Error(`not valid JSON: ${what} at offset ${i}`);
37
+ };
38
+ const string = () => {
39
+ const start = i;
40
+ if (text[i] !== '"')
41
+ fail('string expected');
42
+ i++;
43
+ while (i < text.length && text[i] !== '"')
44
+ i += text[i] === '\\' ? 2 : 1;
45
+ if (text[i] !== '"')
46
+ fail('unterminated string');
47
+ i++;
48
+ return JSON.parse(text.slice(start, i));
49
+ };
50
+ const value = () => {
51
+ ws();
52
+ const start = i;
53
+ if (text[i] === '{') {
54
+ i++;
55
+ const members = [];
56
+ ws();
57
+ if (text[i] === '}')
58
+ return { kind: 'object', start, end: ++i, members };
59
+ for (;;) {
60
+ ws();
61
+ const keyStart = i;
62
+ const key = string();
63
+ ws();
64
+ if (text[i] !== ':')
65
+ fail("':' expected");
66
+ i++;
67
+ members.push({ key, keyStart, value: value() });
68
+ ws();
69
+ if (text[i] === ',') {
70
+ i++;
71
+ continue;
72
+ }
73
+ if (text[i] === '}') {
74
+ i++;
75
+ break;
76
+ }
77
+ fail("',' or '}' expected");
78
+ }
79
+ return { kind: 'object', start, end: i, members };
80
+ }
81
+ if (text[i] === '[') {
82
+ i++;
83
+ const items = [];
84
+ ws();
85
+ if (text[i] === ']')
86
+ return { kind: 'array', start, end: ++i, items };
87
+ for (;;) {
88
+ items.push(value());
89
+ ws();
90
+ if (text[i] === ',') {
91
+ i++;
92
+ continue;
93
+ }
94
+ if (text[i] === ']') {
95
+ i++;
96
+ break;
97
+ }
98
+ fail("',' or ']' expected");
99
+ }
100
+ return { kind: 'array', start, end: i, items };
101
+ }
102
+ if (text[i] === '"') {
103
+ const parsed = string();
104
+ return { kind: 'value', start, end: i, value: parsed };
105
+ }
106
+ const literal = /^(?:-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?|true|false|null)/.exec(text.slice(i, i + 64));
107
+ if (!literal)
108
+ fail('value expected');
109
+ i += literal[0].length;
110
+ return { kind: 'value', start, end: i, value: JSON.parse(literal[0]) };
111
+ };
112
+ const root = value();
113
+ ws();
114
+ if (i !== text.length)
115
+ fail('trailing text');
116
+ return root;
117
+ }
118
+ /** The original text with every node in `drop` removed; untouched parts keep their bytes. */
119
+ function render(text, node, drop) {
120
+ if (node.kind === 'value')
121
+ return text.slice(node.start, node.end);
122
+ const children = node.kind === 'array'
123
+ ? node.items.map((item) => ({ start: item.start, node: item }))
124
+ : node.members.map((member) => ({ start: member.keyStart, node: member.value }));
125
+ if (!children.length)
126
+ return text.slice(node.start, node.end);
127
+ const kept = children.filter((child) => !drop.has(child.node));
128
+ const [open, close] = node.kind === 'array' ? ['[', ']'] : ['{', '}'];
129
+ if (!kept.length)
130
+ return open + close;
131
+ if (kept.length === children.length) {
132
+ let out = '';
133
+ let at = node.start;
134
+ for (const child of children) {
135
+ out += text.slice(at, child.node.start) + render(text, child.node, drop);
136
+ at = child.node.end;
137
+ }
138
+ return out + text.slice(at, node.end);
139
+ }
140
+ const first = children[0];
141
+ const last = children[children.length - 1];
142
+ const separator = children.length > 1 ? text.slice(first.node.end, children[1].start) : ', ';
143
+ return open + text.slice(node.start + 1, first.start)
144
+ + kept.map((child) => text.slice(child.start, child.node.start) + render(text, child.node, drop)).join(separator)
145
+ + text.slice(last.node.end, node.end - 1) + close;
146
+ }
147
+ const commandOf = (node) => {
148
+ if (node.kind !== 'object')
149
+ return undefined;
150
+ const command = node.members.find((m) => m.key === 'command')?.value;
151
+ return command?.kind === 'value' ? command.value : undefined;
152
+ };
153
+ /**
154
+ * Removes the standalone Burn gate entries under "hooks". A matcher group
155
+ * holding only Burn entries goes with them, and an event left with no group
156
+ * goes too; a group that also holds other hooks keeps them.
157
+ */
158
+ function removeStandaloneBurnHooks(text) {
159
+ JSON.parse(text); // refuse anything that is not plain JSON before touching it
160
+ const root = parseWithPositions(text);
161
+ if (root.kind !== 'object')
162
+ return { text, removed: 0 };
163
+ const hooks = root.members.find((m) => m.key === 'hooks')?.value;
164
+ if (!hooks || hooks.kind !== 'object')
165
+ return { text, removed: 0 };
166
+ const drop = new Set();
167
+ let removed = 0;
168
+ for (const event of hooks.members) {
169
+ if (event.value.kind !== 'array')
170
+ continue;
171
+ let kept = 0;
172
+ let dropped = 0;
173
+ for (const group of event.value.items) {
174
+ if (commandOf(group) !== undefined) {
175
+ if (isStandaloneBurnGate(commandOf(group))) {
176
+ drop.add(group);
177
+ removed++;
178
+ dropped++;
179
+ }
180
+ else
181
+ kept++;
182
+ continue;
183
+ }
184
+ const inner = group.kind === 'object' ? group.members.find((m) => m.key === 'hooks')?.value : undefined;
185
+ const burn = inner?.kind === 'array' ? inner.items.filter((h) => isStandaloneBurnGate(commandOf(h))) : [];
186
+ if (!inner || inner.kind !== 'array' || !burn.length) {
187
+ kept++;
188
+ continue;
189
+ }
190
+ removed += burn.length;
191
+ if (burn.length === inner.items.length) {
192
+ drop.add(group);
193
+ dropped++;
194
+ }
195
+ else {
196
+ for (const entry of burn)
197
+ drop.add(entry);
198
+ kept++;
199
+ }
200
+ }
201
+ if (!kept && dropped)
202
+ drop.add(event.value);
203
+ }
204
+ // Whitespace around the top-level object (a final newline, say) stays as it was.
205
+ return removed ? { text: text.slice(0, root.start) + render(text, root, drop) + text.slice(root.end), removed } : { text, removed: 0 };
206
+ }
207
+ /** How many standalone Burn gate entries a hooks file holds (0 when it is missing or not valid JSON). */
208
+ function countStandaloneBurnHooks(file) {
209
+ try {
210
+ return removeStandaloneBurnHooks((0, node_fs_1.readFileSync)(file, 'utf8')).removed;
211
+ }
212
+ catch {
213
+ return 0;
214
+ }
215
+ }
216
+ /** Removes the Burn gate entries from a file: a timestamped backup first, then an atomic write that keeps the file's mode. */
217
+ function removeStandaloneBurnHooksFromFile(file) {
218
+ const original = (0, node_fs_1.readFileSync)(file, 'utf8');
219
+ const result = removeStandaloneBurnHooks(original);
220
+ if (!result.removed)
221
+ return { removed: 0, backup: null };
222
+ JSON.parse(result.text); // the rebuilt file must still be valid JSON before it replaces the original
223
+ const stamp = new Date().toISOString().replace(/[-:.]/g, '').slice(0, 15); // 20261028T163812
224
+ let backup = `${file}.bak-${stamp}`;
225
+ for (let n = 1; (0, node_fs_1.existsSync)(backup); n++)
226
+ backup = `${file}.bak-${stamp}-${n}`;
227
+ (0, node_fs_1.copyFileSync)(file, backup, node_fs_1.constants.COPYFILE_EXCL);
228
+ const temporary = `${file}.agentguard.tmp`;
229
+ (0, node_fs_1.writeFileSync)(temporary, result.text, { mode: (0, node_fs_1.statSync)(file).mode & 0o777 });
230
+ (0, node_fs_1.renameSync)(temporary, file);
231
+ return { removed: result.removed, backup };
232
+ }
@@ -0,0 +1,209 @@
1
+ /**
2
+ * `agentguard-burn install`: AgentGuard on every agent host on this machine.
3
+ *
4
+ * It finds Claude Code, Codex, Cursor and Hermes by their command on PATH and
5
+ * their config folder, and decides what to do by reading files: nothing is
6
+ * started to look. Without --yes it prints the plan (each host, the exact
7
+ * commands, the files they change) and changes nothing, so an agent can show
8
+ * the plan to its person first. With --yes it runs each host's own plugin
9
+ * commands, reads the same files again, and reports one line per host.
10
+ *
11
+ * It never pipes a downloaded script to a shell, never uses sudo, never
12
+ * installs a host itself and never asks for or stores a key. It also never
13
+ * writes Burn's policy file: the plugin seeds enforce mode into a fresh one,
14
+ * and a file written first would keep a new install in shadow.
15
+ */
16
+ export type AgentHost = 'claude' | 'codex' | 'cursor' | 'hermes';
17
+ export declare const AGENT_HOSTS: readonly AgentHost[];
18
+ export declare const HOST_NAMES: Record<AgentHost, string>;
19
+ export declare const PLUGIN_REPO = "agentguard-run/agentguard-codex-plugin";
20
+ export declare const PLUGIN_ID = "agentguard@agentguard";
21
+ export declare const HERMES_PACKAGE = "agentguard-spend-hermes";
22
+ export declare const HERMES_PLUGIN = "agentguard-spend";
23
+ /** Where the plugin lived before it moved; GitHub forwards it to PLUGIN_REPO. */
24
+ export declare const OLD_PLUGIN_REPO = "MerchantGuard/agentguard-codex-plugin";
25
+ /**
26
+ * The oldest plugin release this Burn treats as current. A host's local
27
+ * marketplace copy can be weeks stale (it refreshes only when asked), so an
28
+ * install older than this is updated even when that copy offers nothing newer.
29
+ */
30
+ export declare const PLUGIN_FLOOR_VERSION = "0.5.0";
31
+ /** Negative when a is older than b. Versions that are not x.y.z compare equal. */
32
+ export declare function compareVersions(a: string, b: string): number;
33
+ export interface InstallContext {
34
+ home: string;
35
+ env: Record<string, string | undefined>;
36
+ platform: NodeJS.Platform;
37
+ /** This Burn's cli.js. The Cursor hook runs it. */
38
+ cliPath: string;
39
+ }
40
+ export interface InstallOptions {
41
+ yes: boolean;
42
+ json: boolean;
43
+ /** Hosts named with --only, or null for every host. */
44
+ only: AgentHost[] | null;
45
+ skip: AgentHost[];
46
+ }
47
+ export declare const INSTALL_USAGE = "Usage: agentguard-burn install [--yes] [--only claude,codex,cursor,hermes] [--skip <hosts>] [--json]";
48
+ export declare function parseInstallArgs(args: string[]): InstallOptions | {
49
+ error: string;
50
+ };
51
+ export declare function which(name: string, ctx: Pick<InstallContext, 'env' | 'platform'>): string | null;
52
+ /**
53
+ * The plugin's two locked runtime packages are on disk: in its folder, or in
54
+ * the durable copy its postinstall keeps in the plugin data folder, keyed by
55
+ * the lockfile digest (Codex can replace its install cache at session start).
56
+ */
57
+ export declare function pluginDependenciesPresent(root: string, data?: string | null): boolean;
58
+ /** Where the host keeps the plugin's data: `<plugins>/data/agentguard-<marketplace>`, the same for Claude Code and Codex. */
59
+ export declare function pluginDataDir(configDir: string): string;
60
+ /** The mode AgentGuard runs in on a host, read from local files. */
61
+ export interface HostMode {
62
+ mode: 'enforce' | 'shadow';
63
+ /** Why it is not enforcing, or what can still change it; null when nothing does. */
64
+ reason: string | null;
65
+ /** Where the mode was read. */
66
+ source: string;
67
+ /** The status command that reports the effective mode on that host. */
68
+ check: string;
69
+ }
70
+ /** One host as found on this machine. */
71
+ export interface HostState {
72
+ host: AgentHost;
73
+ /** The host's command on PATH. */
74
+ binary: string | null;
75
+ /** Where the host keeps its config, whether or not it exists. */
76
+ configDir: string;
77
+ configDirExists: boolean;
78
+ present: boolean;
79
+ /** AgentGuard is in place and complete for this host. */
80
+ installed: boolean;
81
+ /** The host has AgentGuard registered (its record exists), complete or not. */
82
+ registered: boolean;
83
+ version: string | null;
84
+ /** The plugin folder, the hook file or the Python environment AgentGuard lives in. */
85
+ root: string | null;
86
+ /** The plugin's locked dependencies are on disk (Claude Code and Codex). */
87
+ dependencies: boolean | null;
88
+ /** The configured AgentGuard marketplace source, if any. */
89
+ marketplace: string | null;
90
+ /** False when the person turned AgentGuard off in the host's own config. */
91
+ enabled: boolean | null;
92
+ /** A file holding a standalone Burn hook for this host. */
93
+ standaloneHook: string | null;
94
+ /** Something found while reading that stops a safe install. */
95
+ problem: string | null;
96
+ /** Hermes: the Python environment Hermes runs from. */
97
+ python: string | null;
98
+ /** Cursor: a hook entry exists but its command is gone from disk. */
99
+ broken: boolean;
100
+ /** The mode AgentGuard runs in, once it is registered. */
101
+ mode: HostMode | null;
102
+ /** The plugin version the host's local marketplace copy offers. */
103
+ offered: string | null;
104
+ /** The version an update would bring, when the installed one is older. */
105
+ updateTo: string | null;
106
+ /** The marketplace still points at the plugin's old repository. */
107
+ oldMarketplace: boolean;
108
+ /** Nothing left to do: installed, current, and no standalone Burn hook beside an enabled plugin. */
109
+ complete: boolean;
110
+ }
111
+ /** The data folder the plugin derives from its install folder: `<plugins>/data/agentguard-<marketplace>`. */
112
+ export declare function dataDirForRoot(root: string): string;
113
+ export declare function probeHost(host: AgentHost, ctx: InstallContext): HostState;
114
+ export declare function detectHosts(ctx: InstallContext): HostState[];
115
+ export interface PlannedCommand {
116
+ /** The command as a person would type it, or what Burn does itself. */
117
+ command: string;
118
+ /** The program and arguments, run without a shell; null when Burn makes the change itself. */
119
+ argv: string[] | null;
120
+ /** Where it runs; null is the current folder. */
121
+ cwd: string | null;
122
+ /** Runs in the plugin folder the previous command installed, which is known only then. */
123
+ cwdFromPluginRoot?: boolean;
124
+ /** Runs only when the plugin's dependencies are missing after the step before. */
125
+ onlyIfDependenciesMissing?: boolean;
126
+ /** A change Burn makes itself (argv is null): Cursor's hook, or removing a standalone Burn hook from `file`. */
127
+ builtin?: 'cursor-hook' | 'remove-burn-hook';
128
+ file?: string;
129
+ }
130
+ export type HostPlanState = 'absent' | 'excluded' | 'installed' | 'install' | 'blocked';
131
+ export interface HostPlan {
132
+ host: AgentHost;
133
+ name: string;
134
+ found: {
135
+ binary: string | null;
136
+ configDir: string | null;
137
+ };
138
+ /** absent: not on this machine. excluded: left out by --only or --skip. installed: nothing to do. install: the commands below. blocked: present, but something it needs is missing. */
139
+ state: HostPlanState;
140
+ detail: string;
141
+ commands: PlannedCommand[];
142
+ /** Files and folders the commands change. */
143
+ files: string[];
144
+ notes: string[];
145
+ /** Named with --only. */
146
+ requested: boolean;
147
+ /** The mode AgentGuard runs in on this host, when it is already registered. */
148
+ mode: HostMode | null;
149
+ /** Set when an installed plugin is older than what is available. */
150
+ update?: {
151
+ from: string;
152
+ to: string;
153
+ } | null;
154
+ }
155
+ export interface InstallPlan {
156
+ hosts: HostPlan[];
157
+ options: InstallOptions;
158
+ /** The command that carries out this plan. */
159
+ next: string | null;
160
+ }
161
+ export declare function planInstall(ctx: InstallContext, options: InstallOptions): InstallPlan;
162
+ /** "enforce (local policy)" or "shadow: <why> (<where read>)". */
163
+ export declare function modeText(mode: HostMode): string;
164
+ export declare function renderPlan(plan: InstallPlan, ctx: InstallContext): string;
165
+ export interface RunResult {
166
+ status: number | null;
167
+ stdout: string;
168
+ stderr: string;
169
+ /** Set when the program could not be started (missing, timed out). */
170
+ error?: string;
171
+ }
172
+ export type Runner = (argv: string[], options: {
173
+ cwd: string | null;
174
+ }) => RunResult;
175
+ /** Runs a host command without a shell and without stdin, so nothing can wait on a prompt. */
176
+ export declare function defaultRunner(ctx: InstallContext): Runner;
177
+ export type HostOutcome = 'installed' | 'already-installed' | 'skipped' | 'failed';
178
+ export interface HostResult {
179
+ host: AgentHost;
180
+ name: string;
181
+ outcome: HostOutcome;
182
+ reason: string;
183
+ /** Every command started, in order, with its exit code. */
184
+ ran: Array<{
185
+ command: string;
186
+ cwd: string | null;
187
+ exitCode: number | null;
188
+ }>;
189
+ /** The host as read again after the run. */
190
+ after: {
191
+ installed: boolean;
192
+ version: string | null;
193
+ root: string | null;
194
+ mode: HostMode | null;
195
+ };
196
+ notes: string[];
197
+ }
198
+ export interface InstallReport {
199
+ ok: boolean;
200
+ hosts: HostResult[];
201
+ }
202
+ /**
203
+ * Carries out a plan: each host in turn, continuing past a host that fails,
204
+ * then reads every host again (the same checks the plan used) for its line.
205
+ */
206
+ export declare function runInstall(plan: InstallPlan, ctx: InstallContext, runner: Runner, log?: (line: string) => void): InstallReport;
207
+ export declare function renderReport(report: InstallReport): string;
208
+ /** What --json prints: the plan, and after --yes the result for each host. `mode` is plan or install; each host's `agentguardMode` is the enforce or shadow mode AgentGuard runs in there. */
209
+ export declare function installJson(version: string, plan: InstallPlan, report: InstallReport | null): Record<string, unknown>;