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.
Files changed (76) hide show
  1. package/README.md +19 -3
  2. package/dist/approvals.d.ts +508 -0
  3. package/dist/approvals.js +1445 -0
  4. package/dist/argpath.d.ts +75 -0
  5. package/dist/argpath.js +189 -0
  6. package/dist/audit.d.ts +297 -0
  7. package/dist/audit.js +524 -0
  8. package/dist/canonical.d.ts +78 -0
  9. package/dist/canonical.js +146 -0
  10. package/dist/classify-held.d.ts +47 -0
  11. package/dist/classify-held.js +60 -0
  12. package/dist/classify-name.d.ts +55 -0
  13. package/dist/classify-name.js +255 -0
  14. package/dist/decide.d.ts +168 -0
  15. package/dist/decide.js +518 -0
  16. package/dist/egress.d.ts +123 -0
  17. package/dist/egress.js +544 -0
  18. package/dist/enforce.d.ts +78 -0
  19. package/dist/enforce.js +231 -0
  20. package/dist/exposure-apply.d.ts +51 -0
  21. package/dist/exposure-apply.js +69 -0
  22. package/dist/exposure.d.ts +81 -0
  23. package/dist/exposure.js +65 -0
  24. package/dist/file-lock.d.ts +70 -0
  25. package/dist/file-lock.js +165 -0
  26. package/dist/glob.d.ts +11 -0
  27. package/dist/glob.js +55 -0
  28. package/dist/grants.d.ts +204 -0
  29. package/dist/grants.js +372 -0
  30. package/dist/http-util.d.ts +52 -0
  31. package/dist/http-util.js +81 -0
  32. package/dist/index.d.ts +35 -0
  33. package/dist/index.js +36 -0
  34. package/dist/inventory.d.ts +100 -0
  35. package/dist/inventory.js +88 -0
  36. package/dist/ip.d.ts +15 -0
  37. package/dist/ip.js +186 -0
  38. package/dist/jwt.d.ts +57 -0
  39. package/dist/jwt.js +199 -0
  40. package/dist/live-state.d.ts +76 -0
  41. package/dist/live-state.js +57 -0
  42. package/dist/migrate.d.ts +22 -0
  43. package/dist/migrate.js +318 -0
  44. package/dist/pins.d.ts +119 -0
  45. package/dist/pins.js +125 -0
  46. package/dist/policy-batch.d.ts +89 -0
  47. package/dist/policy-batch.js +340 -0
  48. package/dist/policy-source.d.ts +161 -0
  49. package/dist/policy-source.js +299 -0
  50. package/dist/policy-write.d.ts +627 -0
  51. package/dist/policy-write.js +1697 -0
  52. package/dist/policy.d.ts +456 -0
  53. package/dist/policy.js +448 -0
  54. package/dist/redact.d.ts +65 -0
  55. package/dist/redact.js +191 -0
  56. package/dist/registry.d.ts +41 -0
  57. package/dist/registry.js +121 -0
  58. package/dist/remember-defaults.d.ts +9 -0
  59. package/dist/remember-defaults.js +9 -0
  60. package/dist/remember.d.ts +91 -0
  61. package/dist/remember.js +386 -0
  62. package/dist/resolve.d.ts +22 -0
  63. package/dist/resolve.js +34 -0
  64. package/dist/runtime-state.d.ts +65 -0
  65. package/dist/runtime-state.js +35 -0
  66. package/dist/secrets.d.ts +159 -0
  67. package/dist/secrets.js +361 -0
  68. package/dist/tenant.d.ts +83 -0
  69. package/dist/tenant.js +182 -0
  70. package/dist/unicode.d.ts +40 -0
  71. package/dist/unicode.js +103 -0
  72. package/dist/within-host.d.ts +33 -0
  73. package/dist/within-host.js +91 -0
  74. package/dist/within-real.d.ts +7 -0
  75. package/dist/within-real.js +28 -0
  76. package/package.json +33 -4
@@ -0,0 +1,627 @@
1
+ import { z } from "zod";
2
+ import { type Decision } from "./decide.js";
3
+ import { type ArgConstraint, type DecisionKind, type Effect, type Policy, type Rule } from "./policy.js";
4
+ /**
5
+ * The per-tool rule a reviewer writes from the console or the CLI: what one
6
+ * tool is, what happens when it is called, and the bounds that qualify it.
7
+ *
8
+ * Why this exists. The product's defaults are good — reads run, writes wait for
9
+ * a human, deletes and unclassified tools are refused — but they were only ever
10
+ * defaults, with no way to depart from them short of hand-writing YAML, and the
11
+ * console said so next to every refused tool. Hand-writing has a trap too: a
12
+ * rule matching an unclassified tool with a `decision` and no `effect` changes
13
+ * nothing, because `resolveEffect` reads the matching rule's own `effect` and
14
+ * `decide()`'s unclassified branch returns before any rule is consulted.
15
+ *
16
+ * Why it writes the *file* rather than rows in the approvals database. The
17
+ * exposure switch is allowed to live in that database because it can only
18
+ * subtract: "off, for this one tool" is the only state it stores. This is
19
+ * additive — it is what makes a refused call reachable — so that argument does
20
+ * not carry. And `policy.yaml` is the artifact the product tells people to
21
+ * review in git; a second, hidden store would mean the Policy page, `hmcp
22
+ * policy` and `git diff` no longer describe what is enforced. The reload
23
+ * machinery adopts a written rule within about a second, fails closed on a
24
+ * half-written file, and audits every outcome.
25
+ *
26
+ * ## What this is forbidden to do
27
+ *
28
+ * Enforced here and checked per write by `assertOnlyOwnRuleChanged` and
29
+ * `assertOnlyRulesChanged`, not promised in prose:
30
+ *
31
+ * - it adds, rewrites or removes exactly **one** rule, the one whose id is
32
+ * `console-<tool>` and whose `match` is the literal `<tool>`. Such a rule is
33
+ * by construction about one tool, so writing it cannot change another tool's
34
+ * verdict — which `assertNoCollateralChange` then proves against the live
35
+ * tool list rather than assuming;
36
+ * - every other rule keeps its value **and its position**. An update never
37
+ * reorders; if a human has inserted a rule above ours that also matches the
38
+ * tool, the update is refused rather than quietly moved up;
39
+ * - the reserved id is not a licence. A rule sitting at `console-<tool>` is
40
+ * rewritten only if it is also *shaped* like one of ours — see `ownsRule`.
41
+ * Anything else is refused, naming the field, so a hand-edited rule is
42
+ * neither clobbered nor silently ignored;
43
+ * - nothing outside `rules` changes, except `defaults.on_unclassified` through
44
+ * its own function below. That is checked structurally rather than by a
45
+ * denylist of `egress`/`tenant`/`audit`/`approvals`/`tool_budget`, so it
46
+ * cannot go stale when `PolicySchema` grows a field;
47
+ * - `pattern` and `coerce` are never written. See `REFUSED_CONSTRAINTS`;
48
+ * - a policy inlined in `gateway.yaml` is refused: that file configures child
49
+ * processes and transports an edit cannot touch and no reload can adopt.
50
+ *
51
+ * ## What it *can* do, said plainly
52
+ *
53
+ * This can widen a permission. A refused `delete_*` tool can be made to run
54
+ * unasked. What stands in the way is not a refusal but visibility: the change
55
+ * is previewed against the real `decide()` first, lands as a readable line in
56
+ * a reviewed file, carries the actor and the overridden rule's own words in
57
+ * the audit log, and trips HMCP001 on the console's own Findings page the
58
+ * moment it leaves a write or destructive tool reachable. A deployment that
59
+ * does not want that sets `policy_overrides: false` and keeps the narrow,
60
+ * classify-only behaviour.
61
+ */
62
+ /** A per-tool rule was written. */
63
+ export declare const TOOL_RULE_SET = "policy.tool-rule.set";
64
+ /** An existing per-tool rule was rewritten. */
65
+ export declare const TOOL_RULE_UPDATE = "policy.tool-rule.update";
66
+ /** A per-tool rule was removed, handing the tool back to the rest of the policy. */
67
+ export declare const TOOL_RULE_CLEAR = "policy.tool-rule.clear";
68
+ /**
69
+ * A write this module refused.
70
+ *
71
+ * Wired up, unlike its predecessor, which was exported and never emitted. A
72
+ * refused attempt to override a human's refusal is precisely the record a
73
+ * security log should carry; without it that attempt is a 422 and nothing else.
74
+ */
75
+ export declare const TOOL_RULE_REFUSED = "policy.tool-rule.refused";
76
+ /** A change to `defaults.on_unclassified`. */
77
+ export declare const UNCLASSIFIED_POSTURE_RULE = "policy.unclassified-posture";
78
+ /** The plain-language choices, so the file's comment says what was picked. */
79
+ export declare const EFFECT_PROSE: Record<Effect, string>;
80
+ export declare const DECISION_PROSE: Record<DecisionKind, string>;
81
+ /**
82
+ * Confirmations the caller states it obtained.
83
+ *
84
+ * Part of the draft rather than a route flag, so the console and the CLI cannot
85
+ * diverge on which are required, and so the audit record can say which were
86
+ * given and what the person was shown when they gave them.
87
+ */
88
+ export interface Acknowledged {
89
+ /** The id of the human-authored rule whose refusal or hold is being overridden. */
90
+ readonly overrideDeny?: string | undefined;
91
+ /** Bounds that govern this tool today are being dropped on purpose. */
92
+ readonly dropBounds?: boolean | undefined;
93
+ /** A destructive tool is being allowed to run with nobody asked. */
94
+ readonly destructiveAllow?: boolean | undefined;
95
+ }
96
+ export interface ToolRuleDraft {
97
+ /** The exact, literal tool name. Never a pattern. */
98
+ readonly tool: string;
99
+ readonly effect: Effect;
100
+ readonly decision: DecisionKind;
101
+ /**
102
+ * Argument bounds for this rule.
103
+ *
104
+ * **`undefined` means "carry whatever governs this tool today".** That
105
+ * default is the single most important line in this file. `decide()` takes
106
+ * the effect, the decision *and* the bounds from one rule — the first whose
107
+ * glob matches — so a per-tool rule placed above a bounding rule silently
108
+ * unbounds the tool unless its bounds come along. A caller that does not
109
+ * mention bounds must get the safe outcome.
110
+ *
111
+ * An explicit `{}` means "no bounds", which is a real choice and requires
112
+ * `acknowledged.dropBounds` when bounds exist to drop.
113
+ */
114
+ readonly args?: Record<string, ArgConstraint> | undefined;
115
+ /** `undefined` carries the governing rule's value, as with `args`. */
116
+ readonly on_violation?: "deny" | "approve" | undefined;
117
+ /** `undefined` carries the governing rule's value, as with `args`. */
118
+ readonly unknown_args?: "allow" | "deny" | "approve" | undefined;
119
+ /**
120
+ * Free text on the rule.
121
+ *
122
+ * Note this reaches the *model*: `decide()` returns `rule.reason` as the
123
+ * decision's reason and sets no `agentReason`, so a refusal hands this
124
+ * string to the caller. True of hand-written rules too, and not changed
125
+ * here — but anything presenting this field must say so rather than calling
126
+ * it an audit-log note.
127
+ */
128
+ readonly reason?: string | undefined;
129
+ readonly actor: string;
130
+ /** Where the rule was set from, for the comment above it. Defaults to the console. */
131
+ readonly via?: string | undefined;
132
+ readonly acknowledged?: Acknowledged | undefined;
133
+ }
134
+ export declare const ToolRuleDraftSchema: z.ZodObject<{
135
+ tool: z.ZodString;
136
+ effect: z.ZodEnum<{
137
+ read: "read";
138
+ write: "write";
139
+ destructive: "destructive";
140
+ }>;
141
+ decision: z.ZodEnum<{
142
+ allow: "allow";
143
+ approve: "approve";
144
+ deny: "deny";
145
+ }>;
146
+ args: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
147
+ max: z.ZodOptional<z.ZodNumber>;
148
+ min: z.ZodOptional<z.ZodNumber>;
149
+ enum: z.ZodOptional<z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean]>>>;
150
+ pattern: z.ZodOptional<z.ZodString>;
151
+ maxLength: z.ZodOptional<z.ZodNumber>;
152
+ maxItems: z.ZodOptional<z.ZodNumber>;
153
+ sum_max: z.ZodOptional<z.ZodNumber>;
154
+ count_max: z.ZodOptional<z.ZodNumber>;
155
+ within: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodArray<z.ZodString>]>>;
156
+ host: z.ZodOptional<z.ZodArray<z.ZodString>>;
157
+ required: z.ZodOptional<z.ZodBoolean>;
158
+ optional: z.ZodOptional<z.ZodBoolean>;
159
+ coerce: z.ZodOptional<z.ZodBoolean>;
160
+ const: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull]>>;
161
+ same_items: z.ZodOptional<z.ZodArray<z.ZodUnknown>>;
162
+ }, z.core.$strict>>>;
163
+ on_violation: z.ZodOptional<z.ZodEnum<{
164
+ approve: "approve";
165
+ deny: "deny";
166
+ }>>;
167
+ unknown_args: z.ZodOptional<z.ZodEnum<{
168
+ allow: "allow";
169
+ approve: "approve";
170
+ deny: "deny";
171
+ }>>;
172
+ reason: z.ZodOptional<z.ZodString>;
173
+ actor: z.ZodString;
174
+ via: z.ZodOptional<z.ZodString>;
175
+ acknowledged: z.ZodOptional<z.ZodObject<{
176
+ overrideDeny: z.ZodOptional<z.ZodString>;
177
+ dropBounds: z.ZodOptional<z.ZodBoolean>;
178
+ destructiveAllow: z.ZodOptional<z.ZodBoolean>;
179
+ }, z.core.$strict>>;
180
+ }, z.core.$strict>;
181
+ export declare class PolicyWriteError extends Error {
182
+ constructor(message: string);
183
+ }
184
+ export declare function parseToolRuleDraft(input: unknown): ToolRuleDraft;
185
+ /** The rule id for one tool. Stable, so a second change rewrites rather than duplicating. */
186
+ export declare function ruleIdFor(tool: string): string;
187
+ /**
188
+ * Whether this module may rewrite or remove the rule sitting at our reserved id.
189
+ *
190
+ * The id alone is forgeable — a person can type `console-notes__delete` by hand
191
+ * — so the id is not the authority. What makes a rewrite safe is a *shape*
192
+ * check: the rule must name exactly this one tool literally, and must carry
193
+ * only fields and values this writer can itself emit. The round trip is the
194
+ * test, rather than a field list, so it cannot drift from what the writer
195
+ * actually produces.
196
+ *
197
+ * The point is not to detect a forgery. It is to make the failure safe: a rule
198
+ * at our id that we did not shape is neither clobbered nor silently ignored —
199
+ * `writeToolRule` refuses and names the field, and a human who meant to own it
200
+ * can rename it.
201
+ */
202
+ export declare function ownsRule(rule: Rule, tool: string): {
203
+ ok: true;
204
+ } | {
205
+ ok: false;
206
+ why: string;
207
+ };
208
+ /** The rule a draft becomes, with anything it left unsaid carried from the policy. */
209
+ export declare function ruleFor(policy: Policy, draft: ToolRuleDraft): Rule;
210
+ /**
211
+ * What a rule's bounds do to a call, said separately from the verdict.
212
+ *
213
+ * This exists because of a trap in the preview. `decide({argsUnknown: true})`
214
+ * deliberately skips `boundsVerdict` — it is asking what a tool's *posture* is,
215
+ * not what happens to a call carrying no arguments. So for a rule with
216
+ * `on_violation: approve`, the verdict is `allow` and that is a true statement
217
+ * about the tool and a false one about any real call. A preview that printed
218
+ * only the verdict would tell someone their bounded tool "runs" when half its
219
+ * calls are held. The dialog builds its sentence from both.
220
+ */
221
+ export interface BoundsView {
222
+ readonly paths: readonly string[];
223
+ readonly constraints: Record<string, ArgConstraint>;
224
+ readonly onViolation: "deny" | "approve";
225
+ readonly unknownArgs: "allow" | "deny" | "approve";
226
+ }
227
+ export declare function boundsOf(rule: Rule | undefined): BoundsView | null;
228
+ export interface ToolRulePreview {
229
+ readonly policy: Policy;
230
+ readonly rule: Rule;
231
+ /** Where in `rules` it lands. The write uses this same number. */
232
+ readonly index: number;
233
+ /** The verdict afterwards — about the tool's posture; see `bounds`. */
234
+ readonly verdict: Decision;
235
+ /** The verdict now. */
236
+ readonly current: Decision;
237
+ /** What the rule's bounds will do to a real call. Null when it has none. */
238
+ readonly bounds: BoundsView | null;
239
+ /** What bounds do to a real call today. */
240
+ readonly currentBounds: BoundsView | null;
241
+ /** The existing rule at our id, when this rewrites rather than adds. */
242
+ readonly replaces: Rule | null;
243
+ /** The rule that decides the tool today. */
244
+ readonly governedBy: string | null;
245
+ /** Whether a person wrote that rule, as against this console. */
246
+ readonly governedByConsole: boolean;
247
+ /** The rule the bounds were copied from, when carried rather than chosen. */
248
+ readonly carriedFrom: string | null;
249
+ /** Bounds that exist today and would be gone. Requires `acknowledged.dropBounds`. */
250
+ readonly dropsBounds: string | null;
251
+ /** A rule no served tool will reach any more once this lands. */
252
+ readonly strandedRule: string | null;
253
+ /** Set when the chosen effect contradicts what the tool's own name says. */
254
+ readonly nameContradiction: string | null;
255
+ /** The exposure switch refuses this before any rule is weighed. */
256
+ readonly overridden: string | null;
257
+ /** A human-written refusal or hold being reversed, with their own words. */
258
+ readonly overriddenReason: {
259
+ readonly ruleId: string;
260
+ readonly decision: DecisionKind;
261
+ readonly reason: string;
262
+ } | null;
263
+ /** True when this makes the tool reachable in a way it was not. */
264
+ readonly loosens: boolean;
265
+ }
266
+ export declare function previewToolRule(args: {
267
+ policy: Policy;
268
+ draft: ToolRuleDraft;
269
+ /** Whether the console's exposure switch has this tool switched off. */
270
+ disabled?: boolean | undefined;
271
+ /** Every tool a live gateway serves, for the stranded-rule note. */
272
+ liveTools?: readonly string[] | undefined;
273
+ }): ToolRulePreview;
274
+ export interface ToolRuleClearPreview {
275
+ readonly policy: Policy;
276
+ readonly removed: Rule;
277
+ readonly verdict: Decision;
278
+ readonly current: Decision;
279
+ /**
280
+ * True when removing the rule leaves the tool unclassified, and therefore
281
+ * refused. "Revert to policy" sounds strictly safe and is not: if this rule
282
+ * was the only thing classifying the tool, reverting breaks it.
283
+ */
284
+ readonly becomesUnclassified: boolean;
285
+ }
286
+ export declare function previewClearToolRule(args: {
287
+ policy: Policy;
288
+ tool: string;
289
+ disabled?: boolean | undefined;
290
+ }): ToolRuleClearPreview | null;
291
+ export interface ToolRuleRefusal {
292
+ readonly kind: "refused";
293
+ readonly why: string;
294
+ /** Set when the refusal is one a confirmation would lift, for the caller to offer. */
295
+ readonly needs?: keyof Acknowledged | undefined;
296
+ }
297
+ export type ToolRuleWrite = {
298
+ readonly kind: "written";
299
+ readonly rule: Rule | null;
300
+ readonly action: "set" | "update" | "cleared";
301
+ readonly previous: Rule | null;
302
+ readonly digest: string;
303
+ readonly previousDigest: string;
304
+ readonly preview: ToolRulePreview | null;
305
+ } | ToolRuleRefusal | {
306
+ readonly kind: "stale";
307
+ readonly expected: string;
308
+ readonly actual: string;
309
+ readonly why: string;
310
+ };
311
+ export interface ToolRuleWriteOptions {
312
+ readonly policyPath: string;
313
+ readonly draft: ToolRuleDraft;
314
+ readonly expectedDigest: string;
315
+ /** Whether a live gateway reports serving this tool. */
316
+ readonly knownToLiveSurface: boolean;
317
+ /** Every tool that gateway serves, for the collateral-change proof. */
318
+ readonly liveTools?: readonly string[] | undefined;
319
+ /** `false` where `policy_writes` is off. */
320
+ readonly writesAllowed?: boolean | undefined;
321
+ /**
322
+ * `false` where `policy_overrides` is off: the console may then still
323
+ * classify a tool nothing classified, but not reverse a decision.
324
+ */
325
+ readonly overridesAllowed?: boolean | undefined;
326
+ readonly now?: Date | undefined;
327
+ }
328
+ export declare function writeToolRule(options: ToolRuleWriteOptions): ToolRuleWrite;
329
+ export declare function clearToolRule(options: {
330
+ readonly policyPath: string;
331
+ readonly tool: string;
332
+ readonly expectedDigest: string;
333
+ readonly liveTools?: readonly string[] | undefined;
334
+ readonly writesAllowed?: boolean | undefined;
335
+ }): ToolRuleWrite;
336
+ /**
337
+ * One source of truth for the rule as YAML text.
338
+ *
339
+ * The console and the CLI both show the rule before it is written. Two
340
+ * hand-rolled emitters that each knew four fields is how both come to omit a
341
+ * carried bound on the day bounds are added, so the displayed YAML is produced
342
+ * from the same object the file write serialises.
343
+ */
344
+ export declare function ruleToYaml(rule: Rule, indent?: string): string;
345
+ /**
346
+ * Temp file and a rename, matching `writeRegistry`.
347
+ *
348
+ * The rename matters more here. `PolicySource.watch` polls mtime, size *and*
349
+ * inode, precisely so an atomic replace is seen as a change — and a reader that
350
+ * caught a partial write would be a gateway parsing half a policy. Failing
351
+ * closed covers that; not writing half a file at all is better than relying on
352
+ * it.
353
+ *
354
+ * The file's mode is preserved rather than forced to 0o600 as the registry's
355
+ * is: a policy file is routinely committed and read by more than one account,
356
+ * and silently tightening its permissions on first edit would be a surprise.
357
+ * Also how the console writes gateway.yaml, for the same reasons.
358
+ */
359
+ export declare function writeAtomically(full: string, text: string): void;
360
+ /**
361
+ * The other half of the answer to "an unclassified tool is a wall": leave the
362
+ * tool unclassified and change what that *means*, from refused outright to held
363
+ * for a human.
364
+ *
365
+ * The one field outside `rules` this module may touch, and only between the two
366
+ * values the schema already has. A posture change rather than a permission
367
+ * grant — nothing runs unasked under either value.
368
+ */
369
+ export declare function writeUnclassifiedPosture(options: {
370
+ readonly policyPath: string;
371
+ readonly value: "deny" | "approve";
372
+ readonly expectedDigest: string;
373
+ readonly actor: string;
374
+ readonly writesAllowed?: boolean | undefined;
375
+ }): {
376
+ kind: "written";
377
+ digest: string;
378
+ previousDigest: string;
379
+ } | ToolRuleRefusal | {
380
+ kind: "stale";
381
+ expected: string;
382
+ actual: string;
383
+ why: string;
384
+ };
385
+ /** One policy setting changed. */
386
+ export declare const POLICY_SETTING_RULE = "policy.setting";
387
+ /** A policy rule rewritten in place. */
388
+ export declare const POLICY_RULE_SET = "policy.rule.set";
389
+ /** A policy rule added. */
390
+ export declare const POLICY_RULE_ADD = "policy.rule.add";
391
+ /** A policy rule removed. */
392
+ export declare const POLICY_RULE_DELETE = "policy.rule.delete";
393
+ /** The policy's rules reordered. */
394
+ export declare const POLICY_RULE_ORDER = "policy.rule.order";
395
+ /** The policy file replaced as text. */
396
+ export declare const POLICY_FILE_EDIT = "policy.file-edit";
397
+ /** What a caller says it confirmed before a change that widens what policy lets through. */
398
+ export interface LoosenAcknowledged {
399
+ readonly loosens?: boolean | undefined;
400
+ }
401
+ export declare const LoosenAcknowledgedSchema: z.ZodObject<{
402
+ loosens: z.ZodOptional<z.ZodBoolean>;
403
+ }, z.core.$strict>;
404
+ export type PolicyEditRefusal = {
405
+ readonly kind: "refused";
406
+ readonly why: string;
407
+ readonly needs?: keyof Acknowledged | "loosens" | undefined;
408
+ };
409
+ export type PolicyEditStale = {
410
+ readonly kind: "stale";
411
+ readonly expected: string;
412
+ readonly actual: string;
413
+ readonly why: string;
414
+ };
415
+ interface SettingSpec {
416
+ readonly schema: z.ZodType;
417
+ /** True when going from `before` to `after` widens what the gateway lets through. */
418
+ readonly loosens: (before: never, after: never) => boolean;
419
+ }
420
+ /**
421
+ * Every policy setting the console may change, with its own schema and the
422
+ * one place that says which direction is the loosening one.
423
+ *
424
+ * An allow-list rather than "any path that validates": a path not here is not
425
+ * writable from the console at all. Left out on purpose:
426
+ *
427
+ * - `audit.path` and `approvals.store_path`, which move where the record and
428
+ * the queue live -- pointing the log somewhere else is the quietest way to
429
+ * stop it being read;
430
+ * - `admin`, which decides who may administer the gateway over HTTP;
431
+ * - `tenant.source`, `tenant.field` and the rest of the tenant block's shape,
432
+ * which decide *which* customer a call belongs to. Only the two switches
433
+ * that make separation stricter or looser are here.
434
+ *
435
+ * Those stay edits to the file, and the raw-file writer grades any change to
436
+ * them as loosening, because it cannot say which way they point.
437
+ *
438
+ * Loosening, per path -- the direction in which more gets through:
439
+ *
440
+ * | path | loosening |
441
+ * |-------------------------------|---------------------------------------------|
442
+ * | enforcement | enforce → observe |
443
+ * | on_tool_change | deny → warn |
444
+ * | defaults.mode | locked → read-only → approve-writes (wider) |
445
+ * | defaults.on_unclassified | deny → approve |
446
+ * | results.on_injection | quarantine → label → allow |
447
+ * | results.on_secret | quarantine → warn → allow |
448
+ * | approvals.mode | deny → cli → elicit → both |
449
+ * | approvals.ttl_seconds | raised |
450
+ * | approvals.single_use | true → false |
451
+ * | tool_budget | raised |
452
+ * | egress.allow / egress.methods | any entry added |
453
+ * | egress.* byte, time, redirect | raised |
454
+ * | egress.block_private_ips | true → false |
455
+ * | egress.allow_http | false → true |
456
+ * | egress.allow_ip_literals | false → true |
457
+ * | audit.enabled / hash_chain | true → false |
458
+ * | audit.record_args | true → false |
459
+ * | audit.redact | any name removed |
460
+ * | audit.on_failure | refuse → continue |
461
+ * | tenant.on_mismatch | deny → override |
462
+ * | tenant.required | true → false |
463
+ *
464
+ * `defaults.mode`: `approve-writes` is the widest because it is the only
465
+ * posture under which a write is reachable at all (with a person's say-so);
466
+ * `read-only` refuses writes outright, and `locked` refuses reads too.
467
+ * `approvals.mode`: `deny` makes every hold a refusal, so moving *to* it
468
+ * tightens; `elicit` lets the person at the agent's own client answer, which
469
+ * is a channel the agent's host controls, so adding it widens.
470
+ */
471
+ export declare const POLICY_SETTINGS: Readonly<Record<string, SettingSpec>>;
472
+ export declare const POLICY_SETTING_PATHS: readonly string[];
473
+ /** A path and a value, checked against that path's own schema. Throws `PolicyWriteError`. */
474
+ export declare function parsePolicySetting(path: unknown, value: unknown): {
475
+ path: string;
476
+ value: unknown;
477
+ };
478
+ /** The value a parsed policy holds at a setting path; undefined where its block is absent. */
479
+ export declare function policySettingValue(policy: Policy, path: string): unknown;
480
+ /** Whether going from `before` to `after` at this path widens what policy lets through. */
481
+ export declare function settingLoosens(path: string, before: unknown, after: unknown): boolean;
482
+ export interface PolicySettingWrite {
483
+ readonly kind: "written";
484
+ readonly digest: string;
485
+ readonly previousDigest: string;
486
+ readonly path: string;
487
+ readonly before: unknown;
488
+ readonly after: unknown;
489
+ readonly loosens: boolean;
490
+ }
491
+ /**
492
+ * Change one policy setting, comment-preserving.
493
+ *
494
+ * The general form of `writeUnclassifiedPosture`: the same read, digest check,
495
+ * validation, canonical "only this changed" proof and atomic write, for any
496
+ * path in `POLICY_SETTINGS`.
497
+ */
498
+ export declare function writePolicySetting(options: {
499
+ readonly policyPath: string;
500
+ readonly path: string;
501
+ readonly value: unknown;
502
+ readonly expectedDigest: string;
503
+ readonly writesAllowed?: boolean | undefined;
504
+ readonly overridesAllowed?: boolean | undefined;
505
+ readonly acknowledged?: LoosenAcknowledged | undefined;
506
+ }): PolicySettingWrite | PolicyEditRefusal | PolicyEditStale;
507
+ /** A tool as a live gateway serves it, for comparing verdicts before and after an edit. */
508
+ export interface PolicyToolRef {
509
+ readonly name: string;
510
+ readonly effect: Effect | null;
511
+ }
512
+ export interface VerdictChange {
513
+ readonly tool: string;
514
+ readonly before: DecisionKind;
515
+ readonly after: DecisionKind;
516
+ readonly beforeRule: string;
517
+ readonly afterRule: string;
518
+ /** More gets through than before: a looser verdict, or the same verdict with its bounds changed. */
519
+ readonly loosens: boolean;
520
+ }
521
+ /**
522
+ * A rule as the console sends it, validated by the policy schema itself.
523
+ *
524
+ * `pattern` and `coerce` are refused unless they are exactly what the same
525
+ * rule already carries at the same path -- see `REFUSED_CONSTRAINTS`.
526
+ */
527
+ export declare function parseRuleInput(input: unknown, existing?: Rule | undefined): Rule;
528
+ export type PolicyRulesOp = {
529
+ readonly kind: "set";
530
+ readonly ruleId: string;
531
+ readonly rule: Rule;
532
+ } | {
533
+ readonly kind: "add";
534
+ readonly rule: Rule;
535
+ readonly index?: number | undefined;
536
+ } | {
537
+ readonly kind: "delete";
538
+ readonly ruleId: string;
539
+ } | {
540
+ readonly kind: "order";
541
+ readonly order: readonly string[];
542
+ };
543
+ /** Why an op cannot apply to this policy at all. `missing` is a rule id that is not there. */
544
+ export type RulesOpError = {
545
+ readonly ok: false;
546
+ readonly why: string;
547
+ readonly missing?: boolean;
548
+ };
549
+ /** The rule list an op produces, or why it cannot. */
550
+ export declare function applyRulesOp(policy: Policy, op: PolicyRulesOp): {
551
+ ok: true;
552
+ rules: Rule[];
553
+ } | RulesOpError;
554
+ /**
555
+ * What changes for each live tool, comparing `decide()` before and after.
556
+ *
557
+ * Asked the way the Tools page asks (`argsUnknown`, the effect the gateway
558
+ * published) and with the tenant block set aside: tenant separation is graded
559
+ * as a setting of its own, and with it in place every tool reads "refused,
560
+ * no tenant" in a process that has none, which would hide every rule change.
561
+ */
562
+ export declare function verdictChanges(before: Policy, after: Policy, tools: readonly PolicyToolRef[]): VerdictChange[];
563
+ export interface PolicyEditAssessment {
564
+ readonly changes: readonly VerdictChange[];
565
+ readonly loosens: boolean;
566
+ /** Plain reasons, one per thing that loosens. */
567
+ readonly loosenReasons: readonly string[];
568
+ }
569
+ /** How a change from one policy to another moves what gets through. */
570
+ export declare function assessPolicyChange(before: Policy, after: Policy, tools: readonly PolicyToolRef[]): PolicyEditAssessment;
571
+ export interface PolicyRulesPreview extends PolicyEditAssessment {
572
+ readonly rules: readonly Rule[];
573
+ readonly previous: Rule | null;
574
+ }
575
+ export declare function previewRulesOp(policy: Policy, op: PolicyRulesOp, tools: readonly PolicyToolRef[]): ({
576
+ ok: true;
577
+ } & PolicyRulesPreview) | RulesOpError;
578
+ export interface PolicyRulesWrite extends PolicyRulesPreview {
579
+ readonly kind: "written";
580
+ readonly digest: string;
581
+ readonly previousDigest: string;
582
+ }
583
+ /**
584
+ * Add, rewrite, remove or reorder any rule -- including hand-written globs.
585
+ *
586
+ * Unlike `writeToolRule` this is not confined to one literal tool, which is
587
+ * exactly why loosening is graded across every live tool before anything is
588
+ * written, and why the proof afterwards is that the rule list is precisely
589
+ * the one previewed and nothing outside `rules` moved.
590
+ */
591
+ export declare function writePolicyRules(options: {
592
+ readonly policyPath: string;
593
+ readonly op: PolicyRulesOp;
594
+ readonly expectedDigest: string;
595
+ readonly tools: readonly PolicyToolRef[];
596
+ readonly writesAllowed?: boolean | undefined;
597
+ readonly overridesAllowed?: boolean | undefined;
598
+ readonly acknowledged?: LoosenAcknowledged | undefined;
599
+ }): PolicyRulesWrite | PolicyEditRefusal | PolicyEditStale | RulesOpError;
600
+ export interface PolicyFilePreview extends PolicyEditAssessment {
601
+ readonly valid: boolean;
602
+ readonly error: string | null;
603
+ readonly policy: Policy | null;
604
+ }
605
+ /**
606
+ * What replacing the policy file with `text` would do. Writes nothing.
607
+ *
608
+ * Invalid YAML and an invalid policy are answers here, not errors: the editor
609
+ * shows them beside the text.
610
+ */
611
+ export declare function previewPolicyFile(before: Policy, text: string, tools: readonly PolicyToolRef[]): PolicyFilePreview;
612
+ export interface PolicyFileWrite extends PolicyFilePreview {
613
+ readonly kind: "written";
614
+ readonly digest: string;
615
+ readonly previousDigest: string;
616
+ }
617
+ /** Replace the policy file with `text`, after the same checks as every other write. */
618
+ export declare function writePolicyFile(options: {
619
+ readonly policyPath: string;
620
+ readonly text: string;
621
+ readonly expectedDigest: string;
622
+ readonly tools: readonly PolicyToolRef[];
623
+ readonly writesAllowed?: boolean | undefined;
624
+ readonly overridesAllowed?: boolean | undefined;
625
+ readonly acknowledged?: LoosenAcknowledged | undefined;
626
+ }): PolicyFileWrite | PolicyEditRefusal | PolicyEditStale;
627
+ export {};