jackline 0.0.0-stage → 0.1.0-alpha.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/policy.js ADDED
@@ -0,0 +1,129 @@
1
+ /**
2
+ * Labels, policies and capability constraints (spec G3, G4).
3
+ *
4
+ * Everything here builds plain data that the Rust kernel enforces. Policies
5
+ * are declarative, so they can be exported for review and loaded back
6
+ * identically.
7
+ */
8
+ import { readFileSync, writeFileSync } from "node:fs";
9
+ export const readers = {
10
+ /** Anyone may read it. */
11
+ public: { kind: "public" },
12
+ /** Nobody outside this run may read it. */
13
+ private: { kind: "only", principals: [] },
14
+ /** Only these principals may read it. */
15
+ only(...principals) {
16
+ return { kind: "only", principals: [...new Set(principals)].sort() };
17
+ },
18
+ };
19
+ function label(integrity, readers_, tags) {
20
+ return { integrity, readers: readers_, tags: [...new Set(tags)].sort(), sources: [] };
21
+ }
22
+ /** What a tool declares about its output. Trusted configuration (spec N10). */
23
+ export class LabelSpec {
24
+ base;
25
+ fields;
26
+ independentOfArgs;
27
+ constructor(base, fields = {}, independentOfArgs = false) {
28
+ this.base = base;
29
+ this.fields = fields;
30
+ this.independentOfArgs = independentOfArgs;
31
+ }
32
+ /** The kernel's form. */
33
+ toKernel() {
34
+ return { base: this.base, fields: this.fields, independent_of_args: this.independentOfArgs };
35
+ }
36
+ }
37
+ /**
38
+ * Output the agent may act on: from your own systems, or a verifier.
39
+ *
40
+ * A result is never more trusted than the call that produced it: if the tool
41
+ * is called with untrusted arguments, its result is untrusted too, unless
42
+ * `independentOfArgs` is set.
43
+ */
44
+ export function trusted(options = {}) {
45
+ return new LabelSpec(label("trusted", options.readers ?? readers.public, options.tags ?? []), {}, options.independentOfArgs ?? false);
46
+ }
47
+ /** Output from the outside world. The default for every tool. */
48
+ export function untrusted(options = {}) {
49
+ return new LabelSpec(label("untrusted", options.readers ?? readers.private, options.tags ?? []));
50
+ }
51
+ /**
52
+ * Different labels for different top-level fields of a structured result,
53
+ * e.g. `fields({ customer_id: trusted(), body: untrusted() })`.
54
+ */
55
+ export function fields(perField, options = {}) {
56
+ const base = options.default ?? untrusted();
57
+ const out = {};
58
+ for (const [k, v] of Object.entries(perField))
59
+ out[k] = v.base;
60
+ return new LabelSpec(base.base, out, base.independentOfArgs);
61
+ }
62
+ /**
63
+ * These tools may only be called from a trusted context. With `args`, those
64
+ * arguments must come from trusted data instead.
65
+ *
66
+ * `otherwise: "approve"` asks a human instead of refusing. Tool names may use
67
+ * `*` wildcards.
68
+ */
69
+ export function requiresTrusted(tools, options = {}) {
70
+ return {
71
+ rule: "requires_trusted",
72
+ tools: typeof tools === "string" ? [tools] : [...tools],
73
+ args: options.args ?? null,
74
+ otherwise: options.otherwise ?? "refuse",
75
+ };
76
+ }
77
+ /**
78
+ * Values tagged `tags` (wildcards allowed, e.g. `secret:*`) may not flow into
79
+ * these `tool.arg` selectors, unless the value of the `destination` argument
80
+ * is one of the value's permitted readers.
81
+ */
82
+ export function noFlow(tags, options) {
83
+ return { rule: "no_flow", tags, into: [...options.into], destination_arg: options.destination ?? null };
84
+ }
85
+ /** Calls to these tools from an untrusted context need a human's approval. */
86
+ export function approveIfUntrusted(...tools) {
87
+ return { rule: "approve_if_untrusted", tools };
88
+ }
89
+ /**
90
+ * A list of rules, evaluated by the kernel on every tool call.
91
+ *
92
+ * Every rule is checked and the strictest outcome wins: refuse, then ask for
93
+ * approval, then allow.
94
+ */
95
+ export class Policy {
96
+ rules;
97
+ constructor(rules = []) {
98
+ this.rules = rules;
99
+ }
100
+ /** The kernel's form. */
101
+ toJSON() {
102
+ return { rules: this.rules };
103
+ }
104
+ /** Write the policy for review, as JSON (which is also valid YAML). */
105
+ export(path) {
106
+ writeFileSync(path, JSON.stringify(this.toJSON(), null, 2) + "\n");
107
+ }
108
+ /** Load a policy written by `export` (here or in Python, as JSON). */
109
+ static load(path) {
110
+ const data = JSON.parse(readFileSync(path, "utf8"));
111
+ return new Policy([...(data.rules ?? [])]);
112
+ }
113
+ }
114
+ export function atMost(value) {
115
+ return { op: "at_most", value };
116
+ }
117
+ export function atLeast(value) {
118
+ return { op: "at_least", value };
119
+ }
120
+ export function oneOf(values) {
121
+ return { op: "one_of", values: [...values] };
122
+ }
123
+ export function equals(value) {
124
+ return { op: "equals", value };
125
+ }
126
+ export function prefix(value) {
127
+ return { op: "prefix", value };
128
+ }
129
+ //# sourceMappingURL=policy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"policy.js","sourceRoot":"","sources":["../src/policy.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAetD,MAAM,CAAC,MAAM,OAAO,GAAG;IACrB,0BAA0B;IAC1B,MAAM,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAa;IACrC,2CAA2C;IAC3C,OAAO,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,EAAE,EAAa;IACpD,yCAAyC;IACzC,IAAI,CAAC,GAAG,UAAoB;QAC1B,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC,GAAG,IAAI,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC;IACvE,CAAC;CACF,CAAC;AASF,SAAS,KAAK,CAAC,SAAkC,EAAE,QAAiB,EAAE,IAAsB;IAC1F,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC;AACxF,CAAC;AAED,+EAA+E;AAC/E,MAAM,OAAO,SAAS;IAET;IACA;IACA;IAHX,YACW,IAAiB,EACjB,SAAsC,EAAE,EACxC,oBAAoB,KAAK;QAFzB,SAAI,GAAJ,IAAI,CAAa;QACjB,WAAM,GAAN,MAAM,CAAkC;QACx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