@peac/policy-kit 0.9.18

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/loader.js ADDED
@@ -0,0 +1,245 @@
1
+ "use strict";
2
+ /**
3
+ * PEAC Policy Kit Loader
4
+ *
5
+ * Loads and validates policy documents from YAML or JSON.
6
+ * No network calls - file system only.
7
+ *
8
+ * @packageDocumentation
9
+ */
10
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ var desc = Object.getOwnPropertyDescriptor(m, k);
13
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
14
+ desc = { enumerable: true, get: function() { return m[k]; } };
15
+ }
16
+ Object.defineProperty(o, k2, desc);
17
+ }) : (function(o, m, k, k2) {
18
+ if (k2 === undefined) k2 = k;
19
+ o[k2] = m[k];
20
+ }));
21
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
22
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
23
+ }) : function(o, v) {
24
+ o["default"] = v;
25
+ });
26
+ var __importStar = (this && this.__importStar) || (function () {
27
+ var ownKeys = function(o) {
28
+ ownKeys = Object.getOwnPropertyNames || function (o) {
29
+ var ar = [];
30
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
31
+ return ar;
32
+ };
33
+ return ownKeys(o);
34
+ };
35
+ return function (mod) {
36
+ if (mod && mod.__esModule) return mod;
37
+ var result = {};
38
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
39
+ __setModuleDefault(result, mod);
40
+ return result;
41
+ };
42
+ })();
43
+ Object.defineProperty(exports, "__esModule", { value: true });
44
+ exports.PolicyValidationError = exports.PolicyLoadError = void 0;
45
+ exports.parsePolicy = parsePolicy;
46
+ exports.validatePolicy = validatePolicy;
47
+ exports.loadPolicy = loadPolicy;
48
+ exports.policyFileExists = policyFileExists;
49
+ exports.createExamplePolicy = createExamplePolicy;
50
+ exports.serializePolicyYaml = serializePolicyYaml;
51
+ exports.serializePolicyJson = serializePolicyJson;
52
+ const fs = __importStar(require("fs"));
53
+ const path = __importStar(require("path"));
54
+ const yaml = __importStar(require("yaml"));
55
+ const zod_1 = require("zod");
56
+ const types_1 = require("./types");
57
+ /**
58
+ * Policy load error
59
+ */
60
+ class PolicyLoadError extends Error {
61
+ cause;
62
+ constructor(message, cause) {
63
+ super(message);
64
+ this.cause = cause;
65
+ this.name = 'PolicyLoadError';
66
+ }
67
+ }
68
+ exports.PolicyLoadError = PolicyLoadError;
69
+ /**
70
+ * Policy validation error with details
71
+ */
72
+ class PolicyValidationError extends PolicyLoadError {
73
+ issues;
74
+ constructor(message, issues) {
75
+ super(message);
76
+ this.issues = issues;
77
+ this.name = 'PolicyValidationError';
78
+ }
79
+ }
80
+ exports.PolicyValidationError = PolicyValidationError;
81
+ /**
82
+ * Parse policy from string content
83
+ *
84
+ * @param content - YAML or JSON string
85
+ * @param format - Optional format hint ('yaml' | 'json'), auto-detected if not provided
86
+ * @returns Validated policy document
87
+ * @throws PolicyLoadError on parse failure
88
+ * @throws PolicyValidationError on schema validation failure
89
+ */
90
+ function parsePolicy(content, format) {
91
+ let parsed;
92
+ try {
93
+ if (format === 'json') {
94
+ parsed = JSON.parse(content);
95
+ }
96
+ else if (format === 'yaml') {
97
+ parsed = yaml.parse(content);
98
+ }
99
+ else {
100
+ // Auto-detect: try JSON first (faster), fall back to YAML
101
+ try {
102
+ parsed = JSON.parse(content);
103
+ }
104
+ catch {
105
+ parsed = yaml.parse(content);
106
+ }
107
+ }
108
+ }
109
+ catch (err) {
110
+ throw new PolicyLoadError(`Failed to parse policy: ${err instanceof Error ? err.message : String(err)}`, err instanceof Error ? err : undefined);
111
+ }
112
+ return validatePolicy(parsed);
113
+ }
114
+ /**
115
+ * Validate a parsed policy object
116
+ *
117
+ * @param obj - Parsed policy object (from YAML/JSON)
118
+ * @returns Validated policy document
119
+ * @throws PolicyValidationError on schema validation failure
120
+ */
121
+ function validatePolicy(obj) {
122
+ try {
123
+ return types_1.PolicyDocumentSchema.parse(obj);
124
+ }
125
+ catch (err) {
126
+ if (err instanceof zod_1.ZodError) {
127
+ const issues = err.issues.map((i) => `${i.path.join('.')}: ${i.message}`).join('; ');
128
+ throw new PolicyValidationError(`Policy validation failed: ${issues}`, err.issues);
129
+ }
130
+ throw new PolicyLoadError(`Policy validation failed: ${err instanceof Error ? err.message : String(err)}`, err instanceof Error ? err : undefined);
131
+ }
132
+ }
133
+ /**
134
+ * Load policy from file
135
+ *
136
+ * @param filePath - Path to policy file (.yaml, .yml, or .json)
137
+ * @returns Validated policy document
138
+ * @throws PolicyLoadError on file read or parse failure
139
+ * @throws PolicyValidationError on schema validation failure
140
+ */
141
+ function loadPolicy(filePath) {
142
+ const ext = path.extname(filePath).toLowerCase();
143
+ let format;
144
+ if (ext === '.json') {
145
+ format = 'json';
146
+ }
147
+ else if (ext === '.yaml' || ext === '.yml') {
148
+ format = 'yaml';
149
+ }
150
+ let content;
151
+ try {
152
+ content = fs.readFileSync(filePath, 'utf-8');
153
+ }
154
+ catch (err) {
155
+ throw new PolicyLoadError(`Failed to read policy file: ${err instanceof Error ? err.message : String(err)}`, err instanceof Error ? err : undefined);
156
+ }
157
+ return parsePolicy(content, format);
158
+ }
159
+ /**
160
+ * Check if a policy file exists and is readable
161
+ *
162
+ * @param filePath - Path to policy file
163
+ * @returns true if file exists and is readable
164
+ */
165
+ function policyFileExists(filePath) {
166
+ try {
167
+ fs.accessSync(filePath, fs.constants.R_OK);
168
+ return true;
169
+ }
170
+ catch {
171
+ return false;
172
+ }
173
+ }
174
+ /**
175
+ * Create a minimal example policy document
176
+ *
177
+ * Useful for scaffolding new policy files.
178
+ */
179
+ function createExamplePolicy() {
180
+ return {
181
+ version: types_1.POLICY_VERSION,
182
+ name: 'Example Policy',
183
+ defaults: {
184
+ decision: 'deny',
185
+ reason: 'No matching rule found',
186
+ },
187
+ rules: [
188
+ {
189
+ name: 'allow-subscribed-crawl',
190
+ subject: {
191
+ type: 'human',
192
+ labels: ['subscribed'],
193
+ },
194
+ purpose: 'crawl',
195
+ licensing_mode: 'subscription',
196
+ decision: 'allow',
197
+ reason: 'Subscribed users can crawl',
198
+ },
199
+ {
200
+ name: 'allow-verified-agents-inference',
201
+ subject: {
202
+ type: 'agent',
203
+ labels: ['verified'],
204
+ },
205
+ purpose: ['inference', 'ai_input'],
206
+ licensing_mode: 'pay_per_inference',
207
+ decision: 'allow',
208
+ reason: 'Verified agents can run inference with payment',
209
+ },
210
+ {
211
+ name: 'review-org-train',
212
+ subject: {
213
+ type: 'org',
214
+ },
215
+ purpose: 'train',
216
+ decision: 'review',
217
+ reason: 'Training requests from organizations require review',
218
+ },
219
+ ],
220
+ };
221
+ }
222
+ /**
223
+ * Serialize policy to YAML string
224
+ *
225
+ * @param policy - Policy document to serialize
226
+ * @returns YAML string
227
+ */
228
+ function serializePolicyYaml(policy) {
229
+ return yaml.stringify(policy, {
230
+ lineWidth: 100,
231
+ defaultKeyType: 'PLAIN',
232
+ defaultStringType: 'QUOTE_DOUBLE',
233
+ });
234
+ }
235
+ /**
236
+ * Serialize policy to JSON string
237
+ *
238
+ * @param policy - Policy document to serialize
239
+ * @param pretty - Pretty-print with indentation (default: true)
240
+ * @returns JSON string
241
+ */
242
+ function serializePolicyJson(policy, pretty = true) {
243
+ return JSON.stringify(policy, null, pretty ? 2 : undefined);
244
+ }
245
+ //# sourceMappingURL=loader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loader.js","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CH,kCAwBC;AASD,wCAaC;AAUD,gCAqBC;AAQD,4CAOC;AAOD,kDA0CC;AAQD,kDAMC;AASD,kDAEC;AA/MD,uCAAy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AO,EAAE;oBACP,IAAI,EAAE,OAAO;oBACb,MAAM,EAAE,CAAC,UAAU,CAAC;iBACrB;gBACD,OAAO,EAAE,CAAC,WAAW,EAAE,UAAU,CAAC;gBAClC,cAAc,EAAE,mBAAmB;gBACnC,QAAQ,EAAE,OAAO;gBACjB,MAAM,EAAE,gDAAgD;aACzD;YACD;gBACE,IAAI,EAAE,kBAAkB;gBACxB,OAAO,EAAE;oBACP,IAAI,EAAE,KAAK;iBACZ;gBACD,OAAO,EAAE,OAAO;gBAChB,QAAQ,EAAE,QAAQ;gBAClB,MAAM,EAAE,qDAAqD;aAC9D;SACF;KACF,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,SAAgB,mBAAmB,CAAC,MAAsB;IACxD,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE;QAC5B,SAAS,EAAE,GAAG;QACd,cAAc,EAAE,OAAO;QACvB,iBAAiB,EAAE,cAAc;KAClC,CAAC,CAAC;AACL,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,mBAAmB,CAAC,MAAsB,EAAE,MAAM,GAAG,IAAI;IACvE,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;AAC9D,CAAC"}
@@ -0,0 +1,276 @@
1
+ /**
2
+ * PEAC Policy Kit Types
3
+ *
4
+ * Deterministic policy format for CAL semantics.
5
+ * Version: peac-policy/0.1
6
+ *
7
+ * Design principles:
8
+ * - No scripting, no dynamic code
9
+ * - Deterministic, auditable, side-effect free
10
+ * - First-match-wins rule semantics
11
+ *
12
+ * @packageDocumentation
13
+ */
14
+ import { z } from 'zod';
15
+ import { ControlPurposeSchema, ControlLicensingModeSchema, ControlDecisionSchema, SubjectTypeSchema } from '@peac/schema';
16
+ /**
17
+ * Policy format version
18
+ */
19
+ export declare const POLICY_VERSION = "peac-policy/0.1";
20
+ /**
21
+ * Subject type (re-export from schema)
22
+ */
23
+ export type SubjectType = z.infer<typeof SubjectTypeSchema>;
24
+ /**
25
+ * Control purpose (re-export from schema)
26
+ */
27
+ export type ControlPurpose = z.infer<typeof ControlPurposeSchema>;
28
+ /**
29
+ * Control licensing mode (re-export from schema)
30
+ */
31
+ export type ControlLicensingMode = z.infer<typeof ControlLicensingModeSchema>;
32
+ /**
33
+ * Control decision (re-export from schema)
34
+ */
35
+ export type ControlDecision = z.infer<typeof ControlDecisionSchema>;
36
+ /**
37
+ * Subject matcher - criteria for matching a subject
38
+ *
39
+ * All fields are optional (omitted = any/wildcard).
40
+ * When multiple fields are present, all must match (AND logic).
41
+ */
42
+ export declare const SubjectMatcherSchema: z.ZodObject<{
43
+ /** Match by subject type(s) - single type or array */
44
+ type: z.ZodOptional<z.ZodUnion<[z.ZodEnum<["human", "org", "agent"]>, z.ZodArray<z.ZodEnum<["human", "org", "agent"]>, "many">]>>;
45
+ /** Match by label(s) - subject must have ALL specified labels */
46
+ labels: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
47
+ /** Match by subject ID pattern (exact match or prefix with *) */
48
+ id: z.ZodOptional<z.ZodString>;
49
+ }, "strict", z.ZodTypeAny, {
50
+ type?: "human" | "org" | "agent" | ("human" | "org" | "agent")[] | undefined;
51
+ labels?: string[] | undefined;
52
+ id?: string | undefined;
53
+ }, {
54
+ type?: "human" | "org" | "agent" | ("human" | "org" | "agent")[] | undefined;
55
+ labels?: string[] | undefined;
56
+ id?: string | undefined;
57
+ }>;
58
+ export type SubjectMatcher = z.infer<typeof SubjectMatcherSchema>;
59
+ /**
60
+ * Policy rule - a single rule in the policy
61
+ *
62
+ * Evaluated in order; first match wins.
63
+ */
64
+ export declare const PolicyRuleSchema: z.ZodObject<{
65
+ /** Rule name (for debugging/auditing) */
66
+ name: z.ZodString;
67
+ /** Subject matcher (omit for any subject) */
68
+ subject: z.ZodOptional<z.ZodObject<{
69
+ /** Match by subject type(s) - single type or array */
70
+ type: z.ZodOptional<z.ZodUnion<[z.ZodEnum<["human", "org", "agent"]>, z.ZodArray<z.ZodEnum<["human", "org", "agent"]>, "many">]>>;
71
+ /** Match by label(s) - subject must have ALL specified labels */
72
+ labels: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
73
+ /** Match by subject ID pattern (exact match or prefix with *) */
74
+ id: z.ZodOptional<z.ZodString>;
75
+ }, "strict", z.ZodTypeAny, {
76
+ type?: "human" | "org" | "agent" | ("human" | "org" | "agent")[] | undefined;
77
+ labels?: string[] | undefined;
78
+ id?: string | undefined;
79
+ }, {
80
+ type?: "human" | "org" | "agent" | ("human" | "org" | "agent")[] | undefined;
81
+ labels?: string[] | undefined;
82
+ id?: string | undefined;
83
+ }>>;
84
+ /** Purpose(s) this rule applies to - single purpose or array */
85
+ purpose: z.ZodOptional<z.ZodUnion<[z.ZodEnum<["crawl", "index", "train", "inference", "ai_input", "ai_index", "search"]>, z.ZodArray<z.ZodEnum<["crawl", "index", "train", "inference", "ai_input", "ai_index", "search"]>, "many">]>>;
86
+ /** Licensing mode(s) this rule applies to */
87
+ licensing_mode: z.ZodOptional<z.ZodUnion<[z.ZodEnum<["subscription", "pay_per_crawl", "pay_per_inference"]>, z.ZodArray<z.ZodEnum<["subscription", "pay_per_crawl", "pay_per_inference"]>, "many">]>>;
88
+ /** Decision if rule matches */
89
+ decision: z.ZodEnum<["allow", "deny", "review"]>;
90
+ /** Reason for decision (for audit trail) */
91
+ reason: z.ZodOptional<z.ZodString>;
92
+ }, "strict", z.ZodTypeAny, {
93
+ name: string;
94
+ decision: "allow" | "deny" | "review";
95
+ subject?: {
96
+ type?: "human" | "org" | "agent" | ("human" | "org" | "agent")[] | undefined;
97
+ labels?: string[] | undefined;
98
+ id?: string | undefined;
99
+ } | undefined;
100
+ purpose?: "crawl" | "index" | "train" | "inference" | "ai_input" | "ai_index" | "search" | ("crawl" | "index" | "train" | "inference" | "ai_input" | "ai_index" | "search")[] | undefined;
101
+ licensing_mode?: "subscription" | "pay_per_crawl" | "pay_per_inference" | ("subscription" | "pay_per_crawl" | "pay_per_inference")[] | undefined;
102
+ reason?: string | undefined;
103
+ }, {
104
+ name: string;
105
+ decision: "allow" | "deny" | "review";
106
+ subject?: {
107
+ type?: "human" | "org" | "agent" | ("human" | "org" | "agent")[] | undefined;
108
+ labels?: string[] | undefined;
109
+ id?: string | undefined;
110
+ } | undefined;
111
+ purpose?: "crawl" | "index" | "train" | "inference" | "ai_input" | "ai_index" | "search" | ("crawl" | "index" | "train" | "inference" | "ai_input" | "ai_index" | "search")[] | undefined;
112
+ licensing_mode?: "subscription" | "pay_per_crawl" | "pay_per_inference" | ("subscription" | "pay_per_crawl" | "pay_per_inference")[] | undefined;
113
+ reason?: string | undefined;
114
+ }>;
115
+ export type PolicyRule = z.infer<typeof PolicyRuleSchema>;
116
+ /**
117
+ * Policy defaults - fallback values when no rule matches
118
+ */
119
+ export declare const PolicyDefaultsSchema: z.ZodObject<{
120
+ /** Default decision when no rule matches */
121
+ decision: z.ZodEnum<["allow", "deny", "review"]>;
122
+ /** Default reason for audit trail */
123
+ reason: z.ZodOptional<z.ZodString>;
124
+ }, "strict", z.ZodTypeAny, {
125
+ decision: "allow" | "deny" | "review";
126
+ reason?: string | undefined;
127
+ }, {
128
+ decision: "allow" | "deny" | "review";
129
+ reason?: string | undefined;
130
+ }>;
131
+ export type PolicyDefaults = z.infer<typeof PolicyDefaultsSchema>;
132
+ /**
133
+ * Complete policy document
134
+ */
135
+ export declare const PolicyDocumentSchema: z.ZodObject<{
136
+ /** Policy format version */
137
+ version: z.ZodLiteral<"peac-policy/0.1">;
138
+ /** Policy name/description (optional) */
139
+ name: z.ZodOptional<z.ZodString>;
140
+ /** Default decision (required) */
141
+ defaults: z.ZodObject<{
142
+ /** Default decision when no rule matches */
143
+ decision: z.ZodEnum<["allow", "deny", "review"]>;
144
+ /** Default reason for audit trail */
145
+ reason: z.ZodOptional<z.ZodString>;
146
+ }, "strict", z.ZodTypeAny, {
147
+ decision: "allow" | "deny" | "review";
148
+ reason?: string | undefined;
149
+ }, {
150
+ decision: "allow" | "deny" | "review";
151
+ reason?: string | undefined;
152
+ }>;
153
+ /** Rules evaluated in order (first match wins) */
154
+ rules: z.ZodArray<z.ZodObject<{
155
+ /** Rule name (for debugging/auditing) */
156
+ name: z.ZodString;
157
+ /** Subject matcher (omit for any subject) */
158
+ subject: z.ZodOptional<z.ZodObject<{
159
+ /** Match by subject type(s) - single type or array */
160
+ type: z.ZodOptional<z.ZodUnion<[z.ZodEnum<["human", "org", "agent"]>, z.ZodArray<z.ZodEnum<["human", "org", "agent"]>, "many">]>>;
161
+ /** Match by label(s) - subject must have ALL specified labels */
162
+ labels: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
163
+ /** Match by subject ID pattern (exact match or prefix with *) */
164
+ id: z.ZodOptional<z.ZodString>;
165
+ }, "strict", z.ZodTypeAny, {
166
+ type?: "human" | "org" | "agent" | ("human" | "org" | "agent")[] | undefined;
167
+ labels?: string[] | undefined;
168
+ id?: string | undefined;
169
+ }, {
170
+ type?: "human" | "org" | "agent" | ("human" | "org" | "agent")[] | undefined;
171
+ labels?: string[] | undefined;
172
+ id?: string | undefined;
173
+ }>>;
174
+ /** Purpose(s) this rule applies to - single purpose or array */
175
+ purpose: z.ZodOptional<z.ZodUnion<[z.ZodEnum<["crawl", "index", "train", "inference", "ai_input", "ai_index", "search"]>, z.ZodArray<z.ZodEnum<["crawl", "index", "train", "inference", "ai_input", "ai_index", "search"]>, "many">]>>;
176
+ /** Licensing mode(s) this rule applies to */
177
+ licensing_mode: z.ZodOptional<z.ZodUnion<[z.ZodEnum<["subscription", "pay_per_crawl", "pay_per_inference"]>, z.ZodArray<z.ZodEnum<["subscription", "pay_per_crawl", "pay_per_inference"]>, "many">]>>;
178
+ /** Decision if rule matches */
179
+ decision: z.ZodEnum<["allow", "deny", "review"]>;
180
+ /** Reason for decision (for audit trail) */
181
+ reason: z.ZodOptional<z.ZodString>;
182
+ }, "strict", z.ZodTypeAny, {
183
+ name: string;
184
+ decision: "allow" | "deny" | "review";
185
+ subject?: {
186
+ type?: "human" | "org" | "agent" | ("human" | "org" | "agent")[] | undefined;
187
+ labels?: string[] | undefined;
188
+ id?: string | undefined;
189
+ } | undefined;
190
+ purpose?: "crawl" | "index" | "train" | "inference" | "ai_input" | "ai_index" | "search" | ("crawl" | "index" | "train" | "inference" | "ai_input" | "ai_index" | "search")[] | undefined;
191
+ licensing_mode?: "subscription" | "pay_per_crawl" | "pay_per_inference" | ("subscription" | "pay_per_crawl" | "pay_per_inference")[] | undefined;
192
+ reason?: string | undefined;
193
+ }, {
194
+ name: string;
195
+ decision: "allow" | "deny" | "review";
196
+ subject?: {
197
+ type?: "human" | "org" | "agent" | ("human" | "org" | "agent")[] | undefined;
198
+ labels?: string[] | undefined;
199
+ id?: string | undefined;
200
+ } | undefined;
201
+ purpose?: "crawl" | "index" | "train" | "inference" | "ai_input" | "ai_index" | "search" | ("crawl" | "index" | "train" | "inference" | "ai_input" | "ai_index" | "search")[] | undefined;
202
+ licensing_mode?: "subscription" | "pay_per_crawl" | "pay_per_inference" | ("subscription" | "pay_per_crawl" | "pay_per_inference")[] | undefined;
203
+ reason?: string | undefined;
204
+ }>, "many">;
205
+ }, "strict", z.ZodTypeAny, {
206
+ version: "peac-policy/0.1";
207
+ defaults: {
208
+ decision: "allow" | "deny" | "review";
209
+ reason?: string | undefined;
210
+ };
211
+ rules: {
212
+ name: string;
213
+ decision: "allow" | "deny" | "review";
214
+ subject?: {
215
+ type?: "human" | "org" | "agent" | ("human" | "org" | "agent")[] | undefined;
216
+ labels?: string[] | undefined;
217
+ id?: string | undefined;
218
+ } | undefined;
219
+ purpose?: "crawl" | "index" | "train" | "inference" | "ai_input" | "ai_index" | "search" | ("crawl" | "index" | "train" | "inference" | "ai_input" | "ai_index" | "search")[] | undefined;
220
+ licensing_mode?: "subscription" | "pay_per_crawl" | "pay_per_inference" | ("subscription" | "pay_per_crawl" | "pay_per_inference")[] | undefined;
221
+ reason?: string | undefined;
222
+ }[];
223
+ name?: string | undefined;
224
+ }, {
225
+ version: "peac-policy/0.1";
226
+ defaults: {
227
+ decision: "allow" | "deny" | "review";
228
+ reason?: string | undefined;
229
+ };
230
+ rules: {
231
+ name: string;
232
+ decision: "allow" | "deny" | "review";
233
+ subject?: {
234
+ type?: "human" | "org" | "agent" | ("human" | "org" | "agent")[] | undefined;
235
+ labels?: string[] | undefined;
236
+ id?: string | undefined;
237
+ } | undefined;
238
+ purpose?: "crawl" | "index" | "train" | "inference" | "ai_input" | "ai_index" | "search" | ("crawl" | "index" | "train" | "inference" | "ai_input" | "ai_index" | "search")[] | undefined;
239
+ licensing_mode?: "subscription" | "pay_per_crawl" | "pay_per_inference" | ("subscription" | "pay_per_crawl" | "pay_per_inference")[] | undefined;
240
+ reason?: string | undefined;
241
+ }[];
242
+ name?: string | undefined;
243
+ }>;
244
+ export type PolicyDocument = z.infer<typeof PolicyDocumentSchema>;
245
+ /**
246
+ * Evaluation context - input to policy evaluation
247
+ */
248
+ export interface EvaluationContext {
249
+ /** Subject information */
250
+ subject?: {
251
+ /** Subject ID */
252
+ id?: string;
253
+ /** Subject type */
254
+ type?: SubjectType;
255
+ /** Subject labels */
256
+ labels?: string[];
257
+ };
258
+ /** Purpose of access */
259
+ purpose?: ControlPurpose;
260
+ /** Licensing mode */
261
+ licensing_mode?: ControlLicensingMode;
262
+ }
263
+ /**
264
+ * Evaluation result
265
+ */
266
+ export interface EvaluationResult {
267
+ /** Decision from evaluation */
268
+ decision: ControlDecision;
269
+ /** Name of matched rule (undefined if default applied) */
270
+ matched_rule?: string;
271
+ /** Reason for decision */
272
+ reason?: string;
273
+ /** Whether default was applied (no rule matched) */
274
+ is_default: boolean;
275
+ }
276
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,CAAC,EAAE,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
+ *
8
+ * Design principles:
9
+ * - No scripting, no dynamic code
10
+ * - Deterministic, auditable, side-effect free
11
+ * - First-match-wins rule semantics
12
+ *
13
+ * @packageDocumentation
14
+ */
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.PolicyDocumentSchema = exports.PolicyDefaultsSchema = exports.PolicyRuleSchema = exports.SubjectMatcherSchema = exports.POLICY_VERSION = void 0;
17
+ const zod_1 = require("zod");
18
+ const schema_1 = require("@peac/schema");
19
+ /**
20
+ * Policy format version
21
+ */
22
+ exports.POLICY_VERSION = 'peac-policy/0.1';
23
+ /**
24
+ * Subject matcher - criteria for matching a subject
25
+ *
26
+ * All fields are optional (omitted = any/wildcard).
27
+ * When multiple fields are present, all must match (AND logic).
28
+ */
29
+ exports.SubjectMatcherSchema = zod_1.z
30
+ .object({
31
+ /** Match by subject type(s) - single type or array */
32
+ type: zod_1.z.union([schema_1.SubjectTypeSchema, zod_1.z.array(schema_1.SubjectTypeSchema)]).optional(),
33
+ /** Match by label(s) - subject must have ALL specified labels */
34
+ labels: zod_1.z.array(zod_1.z.string().min(1)).optional(),
35
+ /** Match by subject ID pattern (exact match or prefix with *) */
36
+ id: zod_1.z.string().min(1).optional(),
37
+ })
38
+ .strict();
39
+ /**
40
+ * Policy rule - a single rule in the policy
41
+ *
42
+ * Evaluated in order; first match wins.
43
+ */
44
+ exports.PolicyRuleSchema = zod_1.z
45
+ .object({
46
+ /** Rule name (for debugging/auditing) */
47
+ name: zod_1.z.string().min(1),
48
+ /** Subject matcher (omit for any subject) */
49
+ subject: exports.SubjectMatcherSchema.optional(),
50
+ /** Purpose(s) this rule applies to - single purpose or array */
51
+ purpose: zod_1.z.union([schema_1.ControlPurposeSchema, zod_1.z.array(schema_1.ControlPurposeSchema)]).optional(),
52
+ /** Licensing mode(s) this rule applies to */
53
+ licensing_mode: zod_1.z
54
+ .union([schema_1.ControlLicensingModeSchema, zod_1.z.array(schema_1.ControlLicensingModeSchema)])
55
+ .optional(),
56
+ /** Decision if rule matches */
57
+ decision: schema_1.ControlDecisionSchema,
58
+ /** Reason for decision (for audit trail) */
59
+ reason: zod_1.z.string().optional(),
60
+ })
61
+ .strict();
62
+ /**
63
+ * Policy defaults - fallback values when no rule matches
64
+ */
65
+ exports.PolicyDefaultsSchema = zod_1.z
66
+ .object({
67
+ /** Default decision when no rule matches */
68
+ decision: schema_1.ControlDecisionSchema,
69
+ /** Default reason for audit trail */
70
+ reason: zod_1.z.string().optional(),
71
+ })
72
+ .strict();
73
+ /**
74
+ * Complete policy document
75
+ */
76
+ exports.PolicyDocumentSchema = zod_1.z
77
+ .object({
78
+ /** Policy format version */
79
+ version: zod_1.z.literal(exports.POLICY_VERSION),
80
+ /** Policy name/description (optional) */
81
+ name: zod_1.z.string().optional(),
82
+ /** Default decision (required) */
83
+ defaults: exports.PolicyDefaultsSchema,
84
+ /** Rules evaluated in order (first match wins) */
85
+ rules: zod_1.z.array(exports.PolicyRuleSchema),
86
+ })
87
+ .strict();
88
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;GAYG;;;AAEH,6BAAwB;AACxB,yCAKsB;AAEtB;;GAEG;AACU,QAAA,cAAc,GAAG,iBAAiB,CAAC;AAsBhD;;;;;GAKG;AACU,QAAA,oBAAoB,GAAG,OAAC;KAClC,MAAM,CAAC;IACN,sDAAsD;IACtD,IAAI,EAAE,OAAC,CAAC,KAAK,CAAC,CAAC,0BAAiB,EAAE,OAAC,CAAC,KAAK,CAAC,0BAAiB,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE;IAEzE,iEAAiE;IACjE,MAAM,EAAE,OAAC,CAAC,KAAK,CAAC,OAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE;IAE7C,iEAAiE;IACjE,EAAE,EAAE,OAAC,CAAC,MAAM,EAAE,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AAkD;IAClD,KAAK,EAAE,OAAC,CAAC,KAAK,CAAC,wBAAgB,CAAC;CACjC,CAAC;KACD,MAAM,EAAE,CAAC"}
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@peac/policy-kit",
3
+ "version": "0.9.18",
4
+ "description": "PEAC Policy Kit - deterministic policy evaluation for CAL semantics",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/peacprotocol/peac.git",
10
+ "directory": "packages/policy-kit"
11
+ },
12
+ "author": "jithinraj <7850727+jithinraj@users.noreply.github.com>",
13
+ "license": "Apache-2.0",
14
+ "bugs": {
15
+ "url": "https://github.com/peacprotocol/peac/issues"
16
+ },
17
+ "homepage": "https://github.com/peacprotocol/peac#readme",
18
+ "files": [
19
+ "dist",
20
+ "README.md"
21
+ ],
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "scripts": {
26
+ "build": "tsc",
27
+ "test": "vitest run",
28
+ "test:watch": "vitest",
29
+ "clean": "rm -rf dist"
30
+ },
31
+ "dependencies": {
32
+ "@peac/schema": "workspace:*",
33
+ "yaml": "^2.3.4",
34
+ "zod": "^3.22.4"
35
+ },
36
+ "devDependencies": {
37
+ "@types/node": "^20.10.0",
38
+ "typescript": "^5.3.3",
39
+ "vitest": "^1.1.0"
40
+ }
41
+ }