hmcp-core 0.0.1 → 0.2.0
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/README.md +19 -3
- package/dist/approvals.d.ts +508 -0
- package/dist/approvals.js +1445 -0
- package/dist/argpath.d.ts +75 -0
- package/dist/argpath.js +189 -0
- package/dist/audit.d.ts +297 -0
- package/dist/audit.js +524 -0
- package/dist/canonical.d.ts +78 -0
- package/dist/canonical.js +146 -0
- package/dist/classify-held.d.ts +47 -0
- package/dist/classify-held.js +60 -0
- package/dist/classify-name.d.ts +55 -0
- package/dist/classify-name.js +255 -0
- package/dist/decide.d.ts +168 -0
- package/dist/decide.js +518 -0
- package/dist/egress.d.ts +123 -0
- package/dist/egress.js +544 -0
- package/dist/enforce.d.ts +78 -0
- package/dist/enforce.js +231 -0
- package/dist/exposure-apply.d.ts +51 -0
- package/dist/exposure-apply.js +69 -0
- package/dist/exposure.d.ts +81 -0
- package/dist/exposure.js +65 -0
- package/dist/file-lock.d.ts +70 -0
- package/dist/file-lock.js +165 -0
- package/dist/glob.d.ts +11 -0
- package/dist/glob.js +55 -0
- package/dist/grants.d.ts +204 -0
- package/dist/grants.js +372 -0
- package/dist/http-util.d.ts +52 -0
- package/dist/http-util.js +81 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +36 -0
- package/dist/inventory.d.ts +100 -0
- package/dist/inventory.js +88 -0
- package/dist/ip.d.ts +15 -0
- package/dist/ip.js +186 -0
- package/dist/jwt.d.ts +57 -0
- package/dist/jwt.js +199 -0
- package/dist/live-state.d.ts +76 -0
- package/dist/live-state.js +57 -0
- package/dist/migrate.d.ts +22 -0
- package/dist/migrate.js +318 -0
- package/dist/pins.d.ts +119 -0
- package/dist/pins.js +125 -0
- package/dist/policy-batch.d.ts +89 -0
- package/dist/policy-batch.js +340 -0
- package/dist/policy-source.d.ts +161 -0
- package/dist/policy-source.js +299 -0
- package/dist/policy-write.d.ts +627 -0
- package/dist/policy-write.js +1697 -0
- package/dist/policy.d.ts +456 -0
- package/dist/policy.js +448 -0
- package/dist/redact.d.ts +65 -0
- package/dist/redact.js +191 -0
- package/dist/registry.d.ts +41 -0
- package/dist/registry.js +121 -0
- package/dist/remember-defaults.d.ts +9 -0
- package/dist/remember-defaults.js +9 -0
- package/dist/remember.d.ts +91 -0
- package/dist/remember.js +386 -0
- package/dist/resolve.d.ts +22 -0
- package/dist/resolve.js +34 -0
- package/dist/runtime-state.d.ts +65 -0
- package/dist/runtime-state.js +35 -0
- package/dist/secrets.d.ts +159 -0
- package/dist/secrets.js +361 -0
- package/dist/tenant.d.ts +83 -0
- package/dist/tenant.js +182 -0
- package/dist/unicode.d.ts +40 -0
- package/dist/unicode.js +103 -0
- package/dist/within-host.d.ts +33 -0
- package/dist/within-host.js +91 -0
- package/dist/within-real.d.ts +7 -0
- package/dist/within-real.js +28 -0
- package/package.json +33 -4
package/dist/remember.js
ADDED
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
import { formatPath, parseArgPath, resolveArgPath } from "./argpath.js";
|
|
2
|
+
import { checkArgConstraints } from "./decide.js";
|
|
3
|
+
import { addedArgs, hasProtoKey, parseGrantDraft } from "./grants.js";
|
|
4
|
+
/**
|
|
5
|
+
* "Approve, and allow calls like this for a while."
|
|
6
|
+
*
|
|
7
|
+
* An approval covers one exact call. That is the right default and it is also
|
|
8
|
+
* why the same question comes back a minute later, and a person asked the same
|
|
9
|
+
* thing ten times stops reading it. Remembering turns their answer into a
|
|
10
|
+
* standing grant -- but one built from the call they were just shown rather
|
|
11
|
+
* than from a pattern they had to write:
|
|
12
|
+
*
|
|
13
|
+
* - this tool, by its exact name;
|
|
14
|
+
* - for a stated time and a stated number of calls;
|
|
15
|
+
* - inside every bound the governing rule already sets;
|
|
16
|
+
* - and where the call broke one of those bounds, relaxed only as far as the
|
|
17
|
+
* call that was approved. Someone who approved a transfer of 3,000 against
|
|
18
|
+
* a limit of 500 has pre-approved transfers up to 3,000, not transfers.
|
|
19
|
+
*
|
|
20
|
+
* Two things cannot be remembered at all. A tool nothing has classified has
|
|
21
|
+
* to be classified first, because a grant is checked against an effect. And
|
|
22
|
+
* a destructive tool stays in front of a person every time: making deletes
|
|
23
|
+
* routine is the one thing this should never be a shortcut to.
|
|
24
|
+
*/
|
|
25
|
+
export { REMEMBER_DEFAULT_TTL_SECONDS, REMEMBER_DEFAULT_USES } from "./remember-defaults.js";
|
|
26
|
+
export class RememberError extends Error {
|
|
27
|
+
constructor(message) {
|
|
28
|
+
super(message);
|
|
29
|
+
this.name = "RememberError";
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/** A value the record does not actually hold: redacted, or cut short. */
|
|
33
|
+
function notInRecord(value) {
|
|
34
|
+
return typeof value === "string" && (value.startsWith("[redacted") || value.includes("…[truncated"));
|
|
35
|
+
}
|
|
36
|
+
/** `items[3].qty` as the pattern a bound is written against: `items[].qty`. */
|
|
37
|
+
function patternOf(held) {
|
|
38
|
+
return held.replace(/\[\d+\]/g, "[]");
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* One rule bound, relaxed just far enough to admit the values that were
|
|
42
|
+
* approved. Whatever the rule said that the call did not break is kept.
|
|
43
|
+
*/
|
|
44
|
+
function relax(spec, existing, values) {
|
|
45
|
+
const c = { ...(existing ?? {}) };
|
|
46
|
+
const hadBound = c.max !== undefined ||
|
|
47
|
+
c.min !== undefined ||
|
|
48
|
+
c.enum !== undefined ||
|
|
49
|
+
c.const !== undefined ||
|
|
50
|
+
c.pattern !== undefined ||
|
|
51
|
+
c.maxLength !== undefined ||
|
|
52
|
+
c.maxItems !== undefined ||
|
|
53
|
+
c.within !== undefined ||
|
|
54
|
+
c.host !== undefined;
|
|
55
|
+
if (values.every((v) => typeof v === "number")) {
|
|
56
|
+
const numbers = values;
|
|
57
|
+
const highest = Math.max(...numbers);
|
|
58
|
+
const lowest = Math.min(...numbers);
|
|
59
|
+
if (c.max !== undefined && highest > c.max)
|
|
60
|
+
c.max = highest;
|
|
61
|
+
if (c.min !== undefined && lowest < c.min)
|
|
62
|
+
c.min = lowest;
|
|
63
|
+
/*
|
|
64
|
+
* An argument the rule said nothing about: no more than what was shown,
|
|
65
|
+
* and not the other side of zero from it either. A floor matters as much
|
|
66
|
+
* as a ceiling here, because this grant is the only thing bounding the
|
|
67
|
+
* argument at all -- a ceiling alone would read an approved adjustment of
|
|
68
|
+
* 5 as approving one of -1000, which for a quantity or an amount is the
|
|
69
|
+
* opposite operation. Anything between zero and what was approved is the
|
|
70
|
+
* same operation, smaller, so the floor sits at zero rather than at the
|
|
71
|
+
* lowest value seen.
|
|
72
|
+
*/
|
|
73
|
+
if (!hadBound) {
|
|
74
|
+
c.max = highest;
|
|
75
|
+
c.min = Math.min(0, lowest);
|
|
76
|
+
}
|
|
77
|
+
if (c.sum_max !== undefined)
|
|
78
|
+
c.sum_max = Math.max(c.sum_max, numbers.reduce((a, b) => a + b, 0));
|
|
79
|
+
}
|
|
80
|
+
else if (values.every((v) => typeof v === "string" || typeof v === "boolean")) {
|
|
81
|
+
const seen = [...new Set(values)];
|
|
82
|
+
const strings = seen.filter((v) => typeof v === "string");
|
|
83
|
+
const allowed = c.enum ?? (c.const !== undefined && c.const !== null ? [c.const] : undefined);
|
|
84
|
+
const patternBroken = c.pattern !== undefined && strings.some((v) => !new RegExp(c.pattern).test(v));
|
|
85
|
+
if (allowed !== undefined || patternBroken || !hadBound) {
|
|
86
|
+
// A choice: the values the rule allowed, plus the ones just approved.
|
|
87
|
+
// A broken pattern cannot be widened by a little, so it becomes the
|
|
88
|
+
// exact values too.
|
|
89
|
+
delete c.const;
|
|
90
|
+
if (patternBroken)
|
|
91
|
+
delete c.pattern;
|
|
92
|
+
c.enum = [...new Set([...(allowed ?? []), ...seen])];
|
|
93
|
+
}
|
|
94
|
+
const longest = Math.max(0, ...strings.map((v) => v.length));
|
|
95
|
+
if (c.maxLength !== undefined && longest > c.maxLength)
|
|
96
|
+
c.maxLength = longest;
|
|
97
|
+
}
|
|
98
|
+
else if (values.every((v) => Array.isArray(v))) {
|
|
99
|
+
const longest = Math.max(...values.map((v) => v.length));
|
|
100
|
+
if (c.maxItems !== undefined && longest > c.maxItems)
|
|
101
|
+
c.maxItems = longest;
|
|
102
|
+
if (c.maxLength !== undefined && longest > c.maxLength)
|
|
103
|
+
c.maxLength = longest;
|
|
104
|
+
if (!hadBound)
|
|
105
|
+
c.maxItems = longest;
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
throw new RememberError(`${spec} is not a number, a string, a yes/no or a list, so there is no bound to write for it. ` +
|
|
109
|
+
`Approve this call on its own, or write a pre-approval by hand in the console.`);
|
|
110
|
+
}
|
|
111
|
+
if (c.count_max !== undefined)
|
|
112
|
+
c.count_max = Math.max(c.count_max, values.length);
|
|
113
|
+
return c;
|
|
114
|
+
}
|
|
115
|
+
/** Whether a constraint limits the value itself (not only that it is sent). */
|
|
116
|
+
function boundsValueOf(c) {
|
|
117
|
+
return (c.max !== undefined ||
|
|
118
|
+
c.min !== undefined ||
|
|
119
|
+
c.enum !== undefined ||
|
|
120
|
+
c.const !== undefined ||
|
|
121
|
+
c.pattern !== undefined ||
|
|
122
|
+
c.maxLength !== undefined ||
|
|
123
|
+
c.maxItems !== undefined ||
|
|
124
|
+
c.sum_max !== undefined ||
|
|
125
|
+
c.count_max !== undefined ||
|
|
126
|
+
c.same_items !== undefined ||
|
|
127
|
+
c.within !== undefined ||
|
|
128
|
+
c.host !== undefined);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Every place in the arguments a value sits, as the pattern a bound is
|
|
132
|
+
* written against, with the value found there. A list is one place: its
|
|
133
|
+
* items are pinned whole (`same_items`), not field by field, because the
|
|
134
|
+
* fields of one item belong together -- approving 5 to alice and 1 to bob
|
|
135
|
+
* must not release 5 to bob (R2 round 4, R4-1). So no pattern returned here
|
|
136
|
+
* reaches inside a list, and each holds exactly one value.
|
|
137
|
+
*/
|
|
138
|
+
function leafPatterns(args) {
|
|
139
|
+
const out = new Map();
|
|
140
|
+
const walk = (pattern, value) => {
|
|
141
|
+
if (Array.isArray(value)) {
|
|
142
|
+
out.set(pattern, value);
|
|
143
|
+
}
|
|
144
|
+
else if (value !== null && typeof value === "object") {
|
|
145
|
+
const entries = Object.entries(value);
|
|
146
|
+
if (entries.length === 0)
|
|
147
|
+
out.set(pattern, value);
|
|
148
|
+
for (const [k, v] of entries)
|
|
149
|
+
walk(`${pattern}.${k}`, v);
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
out.set(pattern, value);
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
for (const [k, v] of Object.entries(args))
|
|
156
|
+
walk(k, v);
|
|
157
|
+
return out;
|
|
158
|
+
}
|
|
159
|
+
/** A value anywhere inside this one that the record does not actually hold. */
|
|
160
|
+
function hiddenInside(value) {
|
|
161
|
+
if (Array.isArray(value))
|
|
162
|
+
return value.some(hiddenInside);
|
|
163
|
+
if (value !== null && typeof value === "object")
|
|
164
|
+
return Object.values(value).some(hiddenInside);
|
|
165
|
+
return notInRecord(value);
|
|
166
|
+
}
|
|
167
|
+
/** The grant an approved call would be remembered as, or a `RememberError` saying why not. */
|
|
168
|
+
export function rememberDraft(input) {
|
|
169
|
+
const now = input.now ?? Date.now();
|
|
170
|
+
if (input.effect === null) {
|
|
171
|
+
throw new RememberError(`nothing has classified ${input.tool}, and a pre-approval is checked against what a tool does. ` +
|
|
172
|
+
`Say what it is first (hmcp approve <id> --classify read|write|destructive), then remember a later call.`);
|
|
173
|
+
}
|
|
174
|
+
if (input.effect === "destructive") {
|
|
175
|
+
throw new RememberError(`${input.tool} is destructive, and a destructive call is approved one at a time. ` +
|
|
176
|
+
`A pre-approval for one can still be written deliberately, in the console.`);
|
|
177
|
+
}
|
|
178
|
+
if (/[*?{},]/.test(input.tool)) {
|
|
179
|
+
throw new RememberError(`"${input.tool}" contains pattern characters, so a pre-approval naming exactly it cannot be written.`);
|
|
180
|
+
}
|
|
181
|
+
if (hasProtoKey(input.args)) {
|
|
182
|
+
throw new RememberError(`this call sends an argument named __proto__, so "don't ask again" can't be limited to it. Approve this call on its own.`);
|
|
183
|
+
}
|
|
184
|
+
const constraints = {};
|
|
185
|
+
for (const [spec, c] of Object.entries(input.ruleArgs ?? {}))
|
|
186
|
+
constraints[spec] = { ...c };
|
|
187
|
+
for (const spec of new Set(input.heldArgs.map(patternOf))) {
|
|
188
|
+
const parsed = parseArgPath(spec);
|
|
189
|
+
if (!parsed.ok) {
|
|
190
|
+
throw new RememberError(`the held argument "${spec}" cannot be written as a bound: ${parsed.error}`);
|
|
191
|
+
}
|
|
192
|
+
const matches = resolveArgPath(input.args, parsed.segments);
|
|
193
|
+
if (matches.length === 0) {
|
|
194
|
+
throw new RememberError(`this call was held because ${spec} was left out, and a pre-approval cannot be bounded by a value ` +
|
|
195
|
+
`that was not sent. Approve this call on its own.`);
|
|
196
|
+
}
|
|
197
|
+
const hidden = matches.find((m) => notInRecord(m.value));
|
|
198
|
+
if (hidden) {
|
|
199
|
+
throw new RememberError(`${formatPath(hidden.path)} is redacted or cut short in the record, so its value is not in the record ` +
|
|
200
|
+
`to bound a pre-approval by. Approve this call on its own.`);
|
|
201
|
+
}
|
|
202
|
+
constraints[spec] = relax(spec, constraints[spec], matches.map((m) => m.value));
|
|
203
|
+
}
|
|
204
|
+
/*
|
|
205
|
+
* Every argument the rule does not bound is bounded by what was approved.
|
|
206
|
+
* The person was shown one call and told that only calls "with inputs that
|
|
207
|
+
* stay within what you are approving now" skip the question. A grant
|
|
208
|
+
* carrying only the rule's bounds -- often none -- would release any
|
|
209
|
+
* arguments at all, so each value nothing else limits is held to the one
|
|
210
|
+
* that was shown: the same text, no larger a number, no longer a list.
|
|
211
|
+
* Arguments a rule bounds keep the rule's bound, as before.
|
|
212
|
+
*/
|
|
213
|
+
const valueBounded = Object.entries(constraints)
|
|
214
|
+
.filter(([, c]) => boundsValueOf(c))
|
|
215
|
+
.map(([spec]) => spec);
|
|
216
|
+
// Only a bound on that very place: a rule's `items: { maxItems }` limits the list's length, not what its items hold.
|
|
217
|
+
const coveredByRule = (pattern) => valueBounded.includes(pattern);
|
|
218
|
+
for (const [pattern, value] of leafPatterns(input.args)) {
|
|
219
|
+
if (!parseArgPath(pattern).ok) {
|
|
220
|
+
throw new RememberError(`the argument "${pattern}" has a name that cannot be written as a bound, so "don't ask again" cannot be ` +
|
|
221
|
+
`limited to this call. Approve this call on its own.`);
|
|
222
|
+
}
|
|
223
|
+
if (hiddenInside(value)) {
|
|
224
|
+
throw new RememberError(`${pattern} is too long or too private to keep in the record, so "don't ask again" cannot be limited to ` +
|
|
225
|
+
`what was approved. Approve this call on its own.`);
|
|
226
|
+
}
|
|
227
|
+
/*
|
|
228
|
+
* A list: exactly the approved items, each compared whole and used no
|
|
229
|
+
* more often than it was approved. A different order of the same records
|
|
230
|
+
* is the same batch and is released; anything else -- an item re-paired,
|
|
231
|
+
* repeated, left out or added -- asks again. This applies even where a
|
|
232
|
+
* rule bounds the list or a field inside it: the rule's bound is kept as
|
|
233
|
+
* well, but it does not stand in for the person's answer about the items.
|
|
234
|
+
*/
|
|
235
|
+
if (Array.isArray(value)) {
|
|
236
|
+
constraints[pattern] = { ...(constraints[pattern] ?? {}), same_items: value };
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
if (coveredByRule(pattern))
|
|
240
|
+
continue;
|
|
241
|
+
// Sent as null: the server receives it, so the grant names it, and only as null.
|
|
242
|
+
if (value === null) {
|
|
243
|
+
constraints[pattern] = { const: null };
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
if (typeof value === "object") {
|
|
247
|
+
throw new RememberError(`${pattern} was sent as an empty object, and there is no bound to write for one, so "don't ask again" cannot be ` +
|
|
248
|
+
`limited to this call. Approve this call on its own.`);
|
|
249
|
+
}
|
|
250
|
+
constraints[pattern] = relax(pattern, undefined, [value]);
|
|
251
|
+
}
|
|
252
|
+
/*
|
|
253
|
+
* The call that was approved has to be inside the grant written from it.
|
|
254
|
+
* If it is not, the derivation above is wrong for this shape of bound, and
|
|
255
|
+
* a grant that does not even cover its own example is refused rather than
|
|
256
|
+
* created. Only the held arguments are checked: the rest may be redacted in
|
|
257
|
+
* the copy this was given, and were inside the rule's bounds already.
|
|
258
|
+
*/
|
|
259
|
+
const held = new Set(input.heldArgs.map(patternOf));
|
|
260
|
+
const pinned = new Set([...leafPatterns(input.args)].filter(([p, v]) => Array.isArray(v) || !coveredByRule(p)).map(([p]) => p));
|
|
261
|
+
const stillBroken = checkArgConstraints(input.args, Object.fromEntries(Object.entries(constraints).filter(([spec]) => held.has(spec) || pinned.has(spec))));
|
|
262
|
+
if (stillBroken.length > 0) {
|
|
263
|
+
throw new RememberError(`could not write a bound that covers this call (${stillBroken.map((v) => v.message).join("; ")}). ` +
|
|
264
|
+
`Approve this call on its own.`);
|
|
265
|
+
}
|
|
266
|
+
// And every place it sends a value is named, or the grant would not release even this call.
|
|
267
|
+
const unnamed = addedArgs(input.args, constraints);
|
|
268
|
+
if (unnamed.length > 0) {
|
|
269
|
+
throw new RememberError(`could not write a bound for ${unnamed.join(", ")}, so "don't ask again" cannot be limited to this call. ` +
|
|
270
|
+
`Approve this call on its own.`);
|
|
271
|
+
}
|
|
272
|
+
/*
|
|
273
|
+
* A terminal command is the one argument where "a bit like the one I
|
|
274
|
+
* approved" means anything at all: `ls` and `ls; curl … | sh` are the same
|
|
275
|
+
* length. So a pre-approval for Claude Code's terminal (the guard calls it
|
|
276
|
+
* `claude-code__Bash`) has to pin the command itself -- to the exact text,
|
|
277
|
+
* a list of texts, or a pattern -- and one bounded any looser is refused.
|
|
278
|
+
*/
|
|
279
|
+
if (input.tool === "claude-code__Bash") {
|
|
280
|
+
const command = constraints["command"];
|
|
281
|
+
if (!command || (command.enum === undefined && command.const === undefined && command.pattern === undefined)) {
|
|
282
|
+
throw new RememberError(`a pre-approval for Claude Code's terminal has to name the exact command (or a pattern for it), and this one ` +
|
|
283
|
+
`would only limit it loosely, so any command could run under it. Approve this call on its own, or write a ` +
|
|
284
|
+
`rule for claude-code__Bash that bounds "command" with a pattern.`);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
try {
|
|
288
|
+
return parseGrantDraft({
|
|
289
|
+
tool_match: input.tool,
|
|
290
|
+
effect: input.effect,
|
|
291
|
+
constraints,
|
|
292
|
+
expires_at: now + Math.round(input.ttlSeconds * 1000),
|
|
293
|
+
max_uses: input.maxUses,
|
|
294
|
+
reason: input.approvalId
|
|
295
|
+
? `remembered while approving ${input.approvalId}`
|
|
296
|
+
: "remembered while approving a held call",
|
|
297
|
+
created_by: input.createdBy,
|
|
298
|
+
// Only the arguments that were approved: a call adding one is asked about again (R2 N4).
|
|
299
|
+
closed: true
|
|
300
|
+
}, now);
|
|
301
|
+
}
|
|
302
|
+
catch (err) {
|
|
303
|
+
throw new RememberError(err.message);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Creates the grant and records it. No pre-approval exists unaudited: if the
|
|
308
|
+
* record cannot be written the grant is withdrawn, the same rule the console
|
|
309
|
+
* applies to a grant made by hand.
|
|
310
|
+
*/
|
|
311
|
+
export function createRememberedGrant(options) {
|
|
312
|
+
const { scoped, audit, draft, approvalId } = options;
|
|
313
|
+
const grant = scoped.createGrant(draft);
|
|
314
|
+
const uses = grant.max_uses === null ? "unlimited uses" : `${grant.max_uses} use(s)`;
|
|
315
|
+
try {
|
|
316
|
+
audit.appendStrict({
|
|
317
|
+
tool: grant.tool_match,
|
|
318
|
+
effect: grant.effect ?? null,
|
|
319
|
+
decision: "approve",
|
|
320
|
+
tenant: scoped.scope.tenant || null,
|
|
321
|
+
rule_id: "standing_grant.create",
|
|
322
|
+
reason: `standing grant ${grant.id} created by ${grant.created_by} via ${options.via ?? "an approval"}, ` +
|
|
323
|
+
`remembering ${approvalId} (matches ${grant.tool_match}, ${uses}, ` +
|
|
324
|
+
`expires ${new Date(grant.expires_at).toISOString()})`,
|
|
325
|
+
outcome: "completed",
|
|
326
|
+
approval_id: approvalId,
|
|
327
|
+
grant_id: grant.id,
|
|
328
|
+
args_redacted: {
|
|
329
|
+
tool_match: grant.tool_match,
|
|
330
|
+
effect: grant.effect,
|
|
331
|
+
constraints: draft.constraints ?? {},
|
|
332
|
+
max_uses: grant.max_uses,
|
|
333
|
+
expires_at: grant.expires_at
|
|
334
|
+
}
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
catch (err) {
|
|
338
|
+
scoped.revokeGrant(grant.id, "system");
|
|
339
|
+
throw new RememberError(`the pre-approval was withdrawn because it could not be recorded: ${err.message}`);
|
|
340
|
+
}
|
|
341
|
+
return grant;
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Remembers an approval: builds the grant from the held call and creates it.
|
|
345
|
+
*
|
|
346
|
+
* Shared by `hmcp approve --remember` and the console, so the two cannot come
|
|
347
|
+
* to disagree about what remembering a call may release. It does not decide
|
|
348
|
+
* the approval: the caller does that next, so a grant that cannot be written
|
|
349
|
+
* stops before anything changes. Throws `RememberError` for every refusal.
|
|
350
|
+
*/
|
|
351
|
+
export function rememberOnApprove(options) {
|
|
352
|
+
const { row } = options;
|
|
353
|
+
if (row.held_args === null) {
|
|
354
|
+
throw new RememberError(`this request was recorded by an older version, which did not note which arguments it was held over. ` +
|
|
355
|
+
`Approve this call on its own, or write the pre-approval in the console.`);
|
|
356
|
+
}
|
|
357
|
+
// The bounds of the rule that held the call, as the policy has them now.
|
|
358
|
+
const rule = options.policy.rules.find((r) => r.id === row.rule_id);
|
|
359
|
+
let args;
|
|
360
|
+
let heldArgs;
|
|
361
|
+
try {
|
|
362
|
+
args = JSON.parse(row.args_redacted);
|
|
363
|
+
heldArgs = JSON.parse(row.held_args);
|
|
364
|
+
}
|
|
365
|
+
catch {
|
|
366
|
+
throw new RememberError(`the stored arguments of ${row.id} are not readable, so no bound can be written from them.`);
|
|
367
|
+
}
|
|
368
|
+
const draft = rememberDraft({
|
|
369
|
+
tool: row.tool,
|
|
370
|
+
effect: row.effect,
|
|
371
|
+
args,
|
|
372
|
+
heldArgs,
|
|
373
|
+
ruleArgs: rule?.args,
|
|
374
|
+
ttlSeconds: options.ttlSeconds,
|
|
375
|
+
maxUses: options.maxUses,
|
|
376
|
+
createdBy: options.actor,
|
|
377
|
+
approvalId: row.id
|
|
378
|
+
});
|
|
379
|
+
return createRememberedGrant({
|
|
380
|
+
scoped: options.scoped,
|
|
381
|
+
audit: options.audit,
|
|
382
|
+
draft,
|
|
383
|
+
approvalId: row.id,
|
|
384
|
+
via: options.via
|
|
385
|
+
});
|
|
386
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type Policy } from "./policy.js";
|
|
2
|
+
/**
|
|
3
|
+
* How every entry point finds its policy: an explicit path, else a
|
|
4
|
+
* `policy.yaml` beside the caller, else the built-in read-only default.
|
|
5
|
+
*
|
|
6
|
+
* This lived inside the CLI, which meant the web console would have had to
|
|
7
|
+
* reimplement it and could have drifted into a different default. The default
|
|
8
|
+
* mattering here is the whole point: falling back to anything other than
|
|
9
|
+
* read-only would make a missing file a way to widen the posture.
|
|
10
|
+
*/
|
|
11
|
+
export declare function resolvePolicy(path?: string, cwd?: string): Policy;
|
|
12
|
+
/** Where the approval database lives, given an optional override. */
|
|
13
|
+
export declare function resolveStorePath(override: string | undefined, policy: Policy): string;
|
|
14
|
+
/** Where the audit log lives, given an optional override. */
|
|
15
|
+
export declare function resolveAuditPath(override: string | undefined, policy: Policy, cwd?: string): string;
|
|
16
|
+
/**
|
|
17
|
+
* The records `hmcp audit tail --denied` shows: anything refused, plus anything
|
|
18
|
+
* that did not run to completion.
|
|
19
|
+
*/
|
|
20
|
+
export declare const DENIED_QUERY: {
|
|
21
|
+
outcome: readonly ["denied", "pending-approval", "error"];
|
|
22
|
+
};
|
package/dist/resolve.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { defaultPolicy, expandPath, loadPolicy } from "./policy.js";
|
|
3
|
+
/**
|
|
4
|
+
* How every entry point finds its policy: an explicit path, else a
|
|
5
|
+
* `policy.yaml` beside the caller, else the built-in read-only default.
|
|
6
|
+
*
|
|
7
|
+
* This lived inside the CLI, which meant the web console would have had to
|
|
8
|
+
* reimplement it and could have drifted into a different default. The default
|
|
9
|
+
* mattering here is the whole point: falling back to anything other than
|
|
10
|
+
* read-only would make a missing file a way to widen the posture.
|
|
11
|
+
*/
|
|
12
|
+
export function resolvePolicy(path, cwd = process.cwd()) {
|
|
13
|
+
if (path)
|
|
14
|
+
return loadPolicy(path);
|
|
15
|
+
const local = expandPath("policy.yaml", cwd);
|
|
16
|
+
if (existsSync(local))
|
|
17
|
+
return loadPolicy(local);
|
|
18
|
+
return defaultPolicy();
|
|
19
|
+
}
|
|
20
|
+
/** Where the approval database lives, given an optional override. */
|
|
21
|
+
export function resolveStorePath(override, policy) {
|
|
22
|
+
return override ?? policy.approvals.store_path;
|
|
23
|
+
}
|
|
24
|
+
/** Where the audit log lives, given an optional override. */
|
|
25
|
+
export function resolveAuditPath(override, policy, cwd = process.cwd()) {
|
|
26
|
+
return expandPath(override ?? policy.audit.path, cwd);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The records `hmcp audit tail --denied` shows: anything refused, plus anything
|
|
30
|
+
* that did not run to completion.
|
|
31
|
+
*/
|
|
32
|
+
export const DENIED_QUERY = {
|
|
33
|
+
outcome: ["denied", "pending-approval", "error"]
|
|
34
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { Policy } from "./policy.js";
|
|
2
|
+
/**
|
|
3
|
+
* What a running server last told the world about itself.
|
|
4
|
+
*
|
|
5
|
+
* The console reads `policy.yaml` from disk on every request, which is what
|
|
6
|
+
* makes its numbers live. A running server does not: it parses the policy once
|
|
7
|
+
* at startup and enforces that copy until it is restarted. So the two can
|
|
8
|
+
* disagree, and the console was presenting its own fresher read as though it
|
|
9
|
+
* were what the server is enforcing — confidently reporting a timeout that
|
|
10
|
+
* nothing has applied yet.
|
|
11
|
+
*
|
|
12
|
+
* Nothing on disk could settle that. The audit log only records decisions about
|
|
13
|
+
* calls, so it says a server was alive at some point, never which policy it
|
|
14
|
+
* loaded. The policy file's mtime is no better: an edit after the last recorded
|
|
15
|
+
* call could mean the server has not seen it, or that the server restarted
|
|
16
|
+
* since and has simply had nothing to do.
|
|
17
|
+
*
|
|
18
|
+
* So a server now states it outright. A row here carries the digest of the
|
|
19
|
+
* policy the process actually parsed, and the console compares that against the
|
|
20
|
+
* digest of what it just read. Equal means the displayed configuration is the
|
|
21
|
+
* enforced one; different means it is not, and the console says so rather than
|
|
22
|
+
* implying otherwise by silence.
|
|
23
|
+
*
|
|
24
|
+
* Rows live in the approvals database for the same reasons exposure overrides
|
|
25
|
+
* do: both processes already open it, it is WAL-mode and lock-safe, and it
|
|
26
|
+
* needs no new transport between them.
|
|
27
|
+
*
|
|
28
|
+
* This is advisory, and only ever subtracts confidence. A missing or stale row
|
|
29
|
+
* means the console does not know what is running, never that a call will be
|
|
30
|
+
* treated differently — enforcement reads none of this.
|
|
31
|
+
*/
|
|
32
|
+
export interface RuntimeStateRow {
|
|
33
|
+
/** The audit `component` of the server this describes. */
|
|
34
|
+
readonly component: string;
|
|
35
|
+
readonly pid: number;
|
|
36
|
+
/** When the process parsed its policy, which is when it started. */
|
|
37
|
+
readonly started_at: number;
|
|
38
|
+
/** Refreshed on the exposure poll; how a dead process is told from a live one. */
|
|
39
|
+
readonly last_seen: number;
|
|
40
|
+
/** `policyDigest` of the policy this process is enforcing. */
|
|
41
|
+
readonly policy_digest: string;
|
|
42
|
+
/** Where it read that policy from, to name the file in a warning. */
|
|
43
|
+
readonly policy_path: string;
|
|
44
|
+
}
|
|
45
|
+
export declare const RUNTIME_STATE_SCHEMA = "\nCREATE TABLE IF NOT EXISTS runtime_state (\n component TEXT PRIMARY KEY,\n pid INTEGER NOT NULL,\n started_at INTEGER NOT NULL,\n last_seen INTEGER NOT NULL,\n policy_digest TEXT NOT NULL,\n policy_path TEXT NOT NULL\n);\n";
|
|
46
|
+
/**
|
|
47
|
+
* A digest of the policy as *parsed*, not as written.
|
|
48
|
+
*
|
|
49
|
+
* Hashing the file bytes would report a change for a reformatted comment and
|
|
50
|
+
* miss that two differently-written files mean the same thing. Hashing the
|
|
51
|
+
* parsed object compares what is actually enforced: defaults are already
|
|
52
|
+
* filled in, key order does not matter, and a whitespace edit is correctly
|
|
53
|
+
* silent.
|
|
54
|
+
*/
|
|
55
|
+
export declare function policyDigest(policy: Policy): string;
|
|
56
|
+
/**
|
|
57
|
+
* How long a row stays believable without a refresh.
|
|
58
|
+
*
|
|
59
|
+
* The runtime refreshes on its exposure poll, which defaults to every 2s. Three
|
|
60
|
+
* missed polls plus a second of slack is long enough that a loaded machine does
|
|
61
|
+
* not make a healthy server look dead, and short enough that a console opened
|
|
62
|
+
* after a server exits does not keep quoting it.
|
|
63
|
+
*/
|
|
64
|
+
export declare const RUNTIME_STATE_TTL_MS = 7000;
|
|
65
|
+
export declare function runtimeStateIsLive(row: RuntimeStateRow, now?: number): boolean;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { canonicalJson, sha256 } from "./canonical.js";
|
|
2
|
+
export const RUNTIME_STATE_SCHEMA = `
|
|
3
|
+
CREATE TABLE IF NOT EXISTS runtime_state (
|
|
4
|
+
component TEXT PRIMARY KEY,
|
|
5
|
+
pid INTEGER NOT NULL,
|
|
6
|
+
started_at INTEGER NOT NULL,
|
|
7
|
+
last_seen INTEGER NOT NULL,
|
|
8
|
+
policy_digest TEXT NOT NULL,
|
|
9
|
+
policy_path TEXT NOT NULL
|
|
10
|
+
);
|
|
11
|
+
`;
|
|
12
|
+
/**
|
|
13
|
+
* A digest of the policy as *parsed*, not as written.
|
|
14
|
+
*
|
|
15
|
+
* Hashing the file bytes would report a change for a reformatted comment and
|
|
16
|
+
* miss that two differently-written files mean the same thing. Hashing the
|
|
17
|
+
* parsed object compares what is actually enforced: defaults are already
|
|
18
|
+
* filled in, key order does not matter, and a whitespace edit is correctly
|
|
19
|
+
* silent.
|
|
20
|
+
*/
|
|
21
|
+
export function policyDigest(policy) {
|
|
22
|
+
return sha256(canonicalJson(policy));
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* How long a row stays believable without a refresh.
|
|
26
|
+
*
|
|
27
|
+
* The runtime refreshes on its exposure poll, which defaults to every 2s. Three
|
|
28
|
+
* missed polls plus a second of slack is long enough that a loaded machine does
|
|
29
|
+
* not make a healthy server look dead, and short enough that a console opened
|
|
30
|
+
* after a server exits does not keep quoting it.
|
|
31
|
+
*/
|
|
32
|
+
export const RUNTIME_STATE_TTL_MS = 7_000;
|
|
33
|
+
export function runtimeStateIsLive(row, now = Date.now()) {
|
|
34
|
+
return now - row.last_seen <= RUNTIME_STATE_TTL_MS;
|
|
35
|
+
}
|