@fozikio/reflex 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,202 @@
1
+ # Reflex
2
+
3
+ **Portable guardrails for any agent runtime.**
4
+
5
+ Safety rules as data, not code. Define what your agent can and can't do in YAML, and Reflex enforces it -- across Claude Code, MCP servers, Cursor, or any runtime with an adapter.
6
+
7
+ ## Why Reflex
8
+
9
+ Agent safety shouldn't be hardcoded into a single platform's hook system. Reflex separates **what** the rules are from **where** they run:
10
+
11
+ - **Rules are YAML files.** Readable, versionable, shareable.
12
+ - **Core rules can't be disabled.** Tier enforcement prevents agents from weakening their own guardrails.
13
+ - **One rule set, many runtimes.** Write rules once, enforce them everywhere your agent operates.
14
+
15
+ ## Install
16
+
17
+ ```bash
18
+ npm install @fozikio/reflex
19
+ ```
20
+
21
+ ## Quick Start
22
+
23
+ ```typescript
24
+ import { RuleEngine, CORE_RULES, loadRuleDirectory } from '@fozikio/reflex';
25
+
26
+ // Initialize with core safety rules
27
+ const engine = new RuleEngine({ allowed_roots: 'D:/projects, /home/user' });
28
+ engine.addRules(CORE_RULES);
29
+
30
+ // Load custom rules from YAML files
31
+ const customRules = await loadRuleDirectory('./rules');
32
+ engine.addRules(customRules);
33
+
34
+ // Evaluate an event
35
+ const results = engine.evaluate({
36
+ event: 'file_write',
37
+ file_path: '/etc/passwd',
38
+ content: 'something dangerous',
39
+ });
40
+
41
+ // Check if an action is allowed
42
+ if (!engine.isAllowed({ event: 'bash_command', command: 'rm -rf /' })) {
43
+ console.log('Blocked by reflex');
44
+ }
45
+ ```
46
+
47
+ ## Rule Format
48
+
49
+ Rules are YAML files with a flat, declarative structure:
50
+
51
+ ```yaml
52
+ name: block-dangerous-rm
53
+ version: 1
54
+ tier: core
55
+ category: safety
56
+ description: Block catastrophic shell commands
57
+ events: [bash_command]
58
+ conditions:
59
+ - field: command
60
+ op: regex
61
+ pattern: 'rm\s+-rf\s+[/~.]'
62
+ action: block
63
+ severity: critical
64
+ message: "Destructive command blocked: {{command}}"
65
+ override:
66
+ allow_disable: false
67
+ allow_downgrade: false
68
+ ```
69
+
70
+ ### Rule Fields
71
+
72
+ | Field | Type | Description |
73
+ |-------|------|-------------|
74
+ | `name` | string | Unique rule identifier |
75
+ | `version` | number | Rule version (for evolution tracking) |
76
+ | `tier` | `core` \| `recommended` \| `custom` | Enforcement tier |
77
+ | `category` | `safety` \| `quality` \| `style` \| `cognitive` | Rule category |
78
+ | `description` | string | Human-readable description |
79
+ | `events` | array | Events that trigger evaluation |
80
+ | `conditions` | array | All must match for rule to trigger |
81
+ | `action` | `block` \| `warn` \| `log` | What happens when triggered |
82
+ | `severity` | `critical` \| `high` \| `medium` \| `low` \| `info` | Severity level |
83
+ | `message` | string | Message shown when triggered (supports `{{var}}` templates) |
84
+ | `override` | object | Whether the rule can be disabled or downgraded |
85
+
86
+ ### Events
87
+
88
+ | Event | Triggers on |
89
+ |-------|-------------|
90
+ | `bash_command` | Shell command execution |
91
+ | `file_write` | New file creation |
92
+ | `file_edit` | Existing file modification |
93
+ | `file_delete` | File deletion |
94
+ | `prompt_submit` | User prompt submission |
95
+ | `session_start` | Agent session starting |
96
+ | `session_stop` | Agent session ending |
97
+ | `tool_call` | Any tool invocation |
98
+ | `post_tool` | After a tool completes |
99
+ | `any` | All events |
100
+
101
+ ### Condition Operators
102
+
103
+ | Operator | Behavior |
104
+ |----------|----------|
105
+ | `matches` | Glob-style matching (comma-separated patterns, `*` wildcards) |
106
+ | `not_matches` | Inverse of matches |
107
+ | `contains` | Substring check |
108
+ | `not_contains` | Inverse of contains |
109
+ | `equals` | Exact string match |
110
+ | `not_equals` | Inverse of equals |
111
+ | `starts_with` | Prefix check |
112
+ | `ends_with` | Suffix check |
113
+ | `regex` | Full regex match |
114
+
115
+ ### Template Variables
116
+
117
+ Rule messages and condition patterns support `{{variable}}` templates. Variables are set at engine initialization and can reference event data:
118
+
119
+ ```typescript
120
+ const engine = new RuleEngine({
121
+ allowed_roots: 'D:/projects, /home/user',
122
+ owner: 'idapixl',
123
+ });
124
+ ```
125
+
126
+ ```yaml
127
+ message: "Write blocked: {{file_path}} is outside {{allowed_roots}}"
128
+ ```
129
+
130
+ ## Tier Enforcement
131
+
132
+ Rules have three tiers with different enforcement guarantees:
133
+
134
+ | Tier | Can disable? | Can downgrade? | Use case |
135
+ |------|-------------|----------------|----------|
136
+ | `core` | No | No | Safety invariants (write boundaries, destructive command blocking) |
137
+ | `recommended` | Yes | Yes | Best practices (credential checks, style rules) |
138
+ | `custom` | Yes | Yes | User-defined rules |
139
+
140
+ Core rules ship with the package and cannot be weakened by configuration:
141
+
142
+ ```typescript
143
+ import { CORE_RULES } from '@fozikio/reflex';
144
+ // write-boundary, destructive-commands, path-traversal, credential-protection
145
+ ```
146
+
147
+ ## API
148
+
149
+ ### `RuleEngine`
150
+
151
+ ```typescript
152
+ const engine = new RuleEngine(variables?: Record<string, string | string[]>);
153
+
154
+ engine.addRule(rule: ReflexRule): void;
155
+ engine.addRules(rules: ReflexRule[]): void;
156
+ engine.setVariable(key: string, value: string | string[]): void;
157
+
158
+ // Returns all triggered rules
159
+ engine.evaluate(event: ReflexEventData): EvaluationResult[];
160
+
161
+ // Returns only blocking results
162
+ engine.getBlocks(event: ReflexEventData): EvaluationResult[];
163
+
164
+ // Returns true if no blocking rules trigger
165
+ engine.isAllowed(event: ReflexEventData): boolean;
166
+ ```
167
+
168
+ ### `loadRuleFile` / `loadRuleDirectory`
169
+
170
+ ```typescript
171
+ // Load a single YAML rule
172
+ const rule = await loadRuleFile('./rules/my-rule.yaml');
173
+
174
+ // Load all .yaml/.yml files from a directory (recursive)
175
+ const rules = await loadRuleDirectory('./rules', config?);
176
+ ```
177
+
178
+ The optional `ReflexConfig` lets you disable specific rules by name (respecting tier enforcement):
179
+
180
+ ```typescript
181
+ const config: ReflexConfig = {
182
+ allowed_roots: ['D:/projects'],
183
+ disabled_rules: ['warn-debug-code'], // only works for recommended/custom tier
184
+ rule_dirs: ['./rules', './extra-rules'],
185
+ };
186
+ ```
187
+
188
+ ## Related Projects
189
+
190
+ - [cortex-engine](https://github.com/Fozikio/cortex-engine) — Cognitive memory layer for AI agents (ships with Reflex rules)
191
+ - [sigil](https://github.com/Fozikio/sigil) — Agent control surface
192
+ - [fozikio.com](https://www.fozikio.com) — Documentation and guides
193
+
194
+ ## Background
195
+
196
+ Reflex is the successor to [Hookify](https://github.com/anthropics/claude-code-plugins), a Claude Code plugin that used markdown files with YAML frontmatter for hook rules. Reflex takes the same idea -- rules as data -- and makes it portable, typed, and enforceable across any agent runtime.
197
+
198
+ Part of the [Fozikio](https://github.com/Fozikio) agent toolkit.
199
+
200
+ ## License
201
+
202
+ MIT
@@ -0,0 +1,4 @@
1
+ import type { ReflexRule } from './types.js';
2
+ /** Core safety rules — hardcoded, non-disableable, ship with every agent. */
3
+ export declare const CORE_RULES: ReflexRule[];
4
+ //# sourceMappingURL=defaults.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defaults.d.ts","sourceRoot":"","sources":["../../src/core/defaults.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,6EAA6E;AAC7E,eAAO,MAAM,UAAU,EAAE,UAAU,EAyElC,CAAC"}
@@ -0,0 +1,76 @@
1
+ /** Core safety rules — hardcoded, non-disableable, ship with every agent. */
2
+ export const CORE_RULES = [
3
+ {
4
+ name: 'write-boundary',
5
+ version: 1,
6
+ tier: 'core',
7
+ category: 'safety',
8
+ description: 'Block file writes outside allowed locations',
9
+ events: ['file_write', 'file_edit'],
10
+ conditions: [
11
+ { field: 'file_path', op: 'not_matches', pattern: '{{allowed_roots}}' },
12
+ ],
13
+ action: 'block',
14
+ severity: 'critical',
15
+ message: 'Write boundary violation: {{file_path}} is outside allowed roots ({{allowed_roots}})',
16
+ override: { allow_disable: false, allow_downgrade: false },
17
+ enabled: true,
18
+ },
19
+ {
20
+ name: 'destructive-commands',
21
+ version: 1,
22
+ tier: 'core',
23
+ category: 'safety',
24
+ description: 'Block catastrophic shell commands',
25
+ events: ['bash_command'],
26
+ conditions: [
27
+ {
28
+ field: 'command',
29
+ op: 'regex',
30
+ pattern: '(rm\\s+-rf\\s+[/~.]|format\\s|diskpart|fdisk|mkfs|del\\s+/s\\s+/q)',
31
+ },
32
+ ],
33
+ action: 'block',
34
+ severity: 'critical',
35
+ message: 'Destructive command blocked: {{command}}',
36
+ override: { allow_disable: false, allow_downgrade: false },
37
+ enabled: true,
38
+ },
39
+ {
40
+ name: 'path-traversal',
41
+ version: 1,
42
+ tier: 'core',
43
+ category: 'safety',
44
+ description: 'Block delete commands with tilde or parent traversal in paths',
45
+ events: ['bash_command', 'file_delete'],
46
+ conditions: [
47
+ { field: 'command', op: 'regex', pattern: '(rm|del|Remove-Item).*[~]|\\.\\.' },
48
+ ],
49
+ action: 'block',
50
+ severity: 'critical',
51
+ message: 'Path traversal in delete command blocked: {{command}}',
52
+ override: { allow_disable: false, allow_downgrade: false },
53
+ enabled: true,
54
+ },
55
+ {
56
+ name: 'credential-protection',
57
+ version: 1,
58
+ tier: 'core',
59
+ category: 'safety',
60
+ description: 'Block hardcoded secrets in source files',
61
+ events: ['file_write', 'file_edit'],
62
+ conditions: [
63
+ {
64
+ field: 'content',
65
+ op: 'regex',
66
+ pattern: '(API_KEY|SECRET|TOKEN|PASSWORD)\\s*=\\s*["\'][A-Za-z0-9]{20,}',
67
+ },
68
+ ],
69
+ action: 'block',
70
+ severity: 'critical',
71
+ message: 'Potential hardcoded credential detected in {{file_path}}',
72
+ override: { allow_disable: false, allow_downgrade: false },
73
+ enabled: true,
74
+ },
75
+ ];
76
+ //# sourceMappingURL=defaults.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defaults.js","source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EAAE,SAAS,EAAE,EAAE,EAAE,OAAO,EAAE,OAAO,EAAE,kCAAkC,EAAE;SAC/E;QACD,MAAM,EAAE,OAAO;QACf,QAAQ,EAAE,UAAU;QACpB,OAAO,EAAE,uDAAuD;QAChE,QAAQ,EAAE,EAAE,aAAa,EAAE,KAAK,EAAE,eAAe,EAAE,KAAK,EAAE;QAC1D,OAAO,EAAE,IAAI;KACd;IACD;QACE,IAAI,EAAE,uBAAuB;QAC7B,OAAO,EAAE,CAAC;QACV,IAAI,EAAE,MAAM;QACZ,QAAQ,EAAE,QAAQ;QAClB,WAAW,EAAE,yCAAyC;QACtD,MAAM,EAAE,CAAC,YAAY,EAAE,WAAW,CAAC;QACnC,UAAU,EAAE;YACV;gBACE,KAAK,EAAE,SAAS;gBAChB,EAAE,EAAE,OAAO;gBACX,OAAO,EAAE,+DAA+D;aACzE;SACF;QACD,MAAM,EAAE,OAAO;QACf,QAAQ,EAAE,UAAU;QACpB,OAAO,EAAE,0DAA0D;QACnE,QAAQ,EAAE,EAAE,aAAa,EAAE,KAAK,EAAE,eAAe,EAAE,KAAK,EAAE;QAC1D,OAAO,EAAE,IAAI;KACd;CACF,CAAC"}
@@ -0,0 +1,18 @@
1
+ import type { ReflexRule, ReflexEventData, EvaluationResult } from './types.js';
2
+ export declare class RuleEngine {
3
+ private rules;
4
+ private variables;
5
+ constructor(variables?: Record<string, string | string[]>);
6
+ addRule(rule: ReflexRule): void;
7
+ addRules(rules: ReflexRule[]): void;
8
+ setVariable(key: string, value: string | string[]): void;
9
+ evaluate(event: ReflexEventData): EvaluationResult[];
10
+ /** Get blocking results only */
11
+ getBlocks(event: ReflexEventData): EvaluationResult[];
12
+ /** Check if an event is allowed (no blocking rules trigger) */
13
+ isAllowed(event: ReflexEventData): boolean;
14
+ private resolveField;
15
+ private resolveTemplate;
16
+ private evaluateCondition;
17
+ }
18
+ //# sourceMappingURL=rule-engine.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rule-engine.d.ts","sourceRoot":"","sources":["../../src/core/rule-engine.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,eAAe,EAAE,gBAAgB,EAAe,MAAM,YAAY,CAAC;AAE7F,qBAAa,UAAU;IACrB,OAAO,CAAC,KAAK,CAAoB;IACjC,OAAO,CAAC,SAAS,CAAyC;gBAE9C,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAIzD,OAAO,CAAC,IAAI,EAAE,UAAU,GAAG,IAAI;IAI/B,QAAQ,CAAC,KAAK,EAAE,UAAU,EAAE,GAAG,IAAI;IAInC,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,IAAI;IAIxD,QAAQ,CAAC,KAAK,EAAE,eAAe,GAAG,gBAAgB,EAAE;IAsCpD,gCAAgC;IAChC,SAAS,CAAC,KAAK,EAAE,eAAe,GAAG,gBAAgB,EAAE;IAIrD,+DAA+D;IAC/D,SAAS,CAAC,KAAK,EAAE,eAAe,GAAG,OAAO;IAI1C,OAAO,CAAC,YAAY;IAMpB,OAAO,CAAC,eAAe;IAWvB,OAAO,CAAC,iBAAiB;CAgC1B"}
@@ -0,0 +1,109 @@
1
+ export class RuleEngine {
2
+ rules = [];
3
+ variables = {};
4
+ constructor(variables) {
5
+ this.variables = variables ?? {};
6
+ }
7
+ addRule(rule) {
8
+ this.rules.push(rule);
9
+ }
10
+ addRules(rules) {
11
+ this.rules.push(...rules);
12
+ }
13
+ setVariable(key, value) {
14
+ this.variables[key] = value;
15
+ }
16
+ evaluate(event) {
17
+ const results = [];
18
+ for (const rule of this.rules) {
19
+ if (!rule.enabled)
20
+ continue;
21
+ if (!rule.events.includes(event.event) && !rule.events.includes('any'))
22
+ continue;
23
+ const matchedConditions = [];
24
+ let allMatch = true;
25
+ for (const condition of rule.conditions) {
26
+ const fieldValue = this.resolveField(event, condition.field);
27
+ const pattern = this.resolveTemplate(condition.pattern);
28
+ if (this.evaluateCondition(fieldValue, condition.op, pattern)) {
29
+ matchedConditions.push(`${condition.field} ${condition.op} ${pattern}`);
30
+ }
31
+ else {
32
+ allMatch = false;
33
+ break;
34
+ }
35
+ }
36
+ if (allMatch && rule.conditions.length > 0) {
37
+ const message = this.resolveTemplate(rule.message, event);
38
+ results.push({
39
+ rule: rule.name,
40
+ triggered: true,
41
+ action: rule.action,
42
+ severity: rule.severity,
43
+ message,
44
+ matched_conditions: matchedConditions,
45
+ });
46
+ }
47
+ }
48
+ return results;
49
+ }
50
+ /** Get blocking results only */
51
+ getBlocks(event) {
52
+ return this.evaluate(event).filter(r => r.action === 'block');
53
+ }
54
+ /** Check if an event is allowed (no blocking rules trigger) */
55
+ isAllowed(event) {
56
+ return this.getBlocks(event).length === 0;
57
+ }
58
+ resolveField(event, field) {
59
+ const value = event[field];
60
+ if (value === undefined || value === null)
61
+ return '';
62
+ return String(value);
63
+ }
64
+ resolveTemplate(template, event) {
65
+ return template.replace(/\{\{(\w+)\}\}/g, (_, key) => {
66
+ // Check event data first
67
+ if (event && key in event)
68
+ return String(event[key]);
69
+ // Then check variables
70
+ const value = this.variables[key];
71
+ if (Array.isArray(value))
72
+ return value.join(', ');
73
+ return value ?? `{{${key}}}`;
74
+ });
75
+ }
76
+ evaluateCondition(value, op, pattern) {
77
+ switch (op) {
78
+ case 'equals': return value === pattern;
79
+ case 'not_equals': return value !== pattern;
80
+ case 'contains': return value.includes(pattern);
81
+ case 'not_contains': return !value.includes(pattern);
82
+ case 'starts_with': return value.startsWith(pattern);
83
+ case 'ends_with': return value.endsWith(pattern);
84
+ case 'regex': return new RegExp(pattern).test(value);
85
+ case 'matches': {
86
+ const patterns = pattern.split(',').map(p => p.trim());
87
+ return patterns.some(p => {
88
+ if (p.includes('*')) {
89
+ const regex = new RegExp('^' + p.replace(/\*/g, '.*') + '$');
90
+ return regex.test(value);
91
+ }
92
+ return value.startsWith(p);
93
+ });
94
+ }
95
+ case 'not_matches': {
96
+ const patterns = pattern.split(',').map(p => p.trim());
97
+ return !patterns.some(p => {
98
+ if (p.includes('*')) {
99
+ const regex = new RegExp('^' + p.replace(/\*/g, '.*') + '$');
100
+ return regex.test(value);
101
+ }
102
+ return value.startsWith(p);
103
+ });
104
+ }
105
+ default: return false;
106
+ }
107
+ }
108
+ }
109
+ //# sourceMappingURL=rule-engine.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rule-engine.js","sourceRoot":"","sources":["../../src/core/rule-engine.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,UAAU;IACb,KAAK,GAAiB,EAAE,CAAC;IACzB,SAAS,GAAsC,EAAE,CAAC;IAE1D,YAAY,SAA6C;QACvD,IAAI,CAAC,SAAS,GAAG,SAAS,IAAI,EAAE,CAAC;IACnC,CAAC;IAED,OAAO,CAAC,IAAgB;QACtB,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACxB,CAAC;IAED,QAAQ,CAAC,KAAmB;QAC1B,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,CAAC;IAC5B,CAAC;IAED,WAAW,CAAC,GAAW,EAAE,KAAwB;QAC/C,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;IAC9B,CAAC;IAED,QAAQ,CAAC,KAAsB;QAC7B,MAAM,OAAO,GAAuB,EAAE,CAAC;QAEvC,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;YAC9B,IAAI,CAAC,IAAI,CAAC,OAAO;gBAAE,SAAS;YAC5B,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC;gBAAE,SAAS;YAEjF,MAAM,iBAAiB,GAAa,EAAE,CAAC;YACvC,IAAI,QAAQ,GAAG,IAAI,CAAC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C;wBAC7D,OAAO,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;oBAC3B,CAAC;oBACD,OAAO,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;gBAC7B,CAAC,CAAC,CAAC;YACL,CAAC;YACD,OAAO,CAAC,CAAC,OAAO,KAAK,CAAC;QACxB,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,4 @@
1
+ import type { ReflexRule, ReflexConfig } from './types.js';
2
+ export declare function loadRuleFile(path: string): Promise<ReflexRule>;
3
+ export declare function loadRuleDirectory(dir: string, config?: ReflexConfig): Promise<ReflexRule[]>;
4
+ //# sourceMappingURL=rule-loader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rule-loader.d.ts","sourceRoot":"","sources":["../../src/core/rule-loader.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAQ3D,wBAAsB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAkCpE;AAED,wBAAsB,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC,CA4BjG"}
@@ -0,0 +1,61 @@
1
+ import { readFile, readdir } from 'node:fs/promises';
2
+ import { join, extname } from 'node:path';
3
+ import { parse as parseYaml } from 'yaml';
4
+ const TIER_ENFORCEMENT = {
5
+ core: { allow_disable: false, allow_downgrade: false },
6
+ recommended: { allow_disable: true, allow_downgrade: true },
7
+ custom: { allow_disable: true, allow_downgrade: true },
8
+ };
9
+ export async function loadRuleFile(path) {
10
+ const content = await readFile(path, 'utf-8');
11
+ const raw = parseYaml(content);
12
+ const tier = raw['tier'] ?? 'custom';
13
+ const enforcement = TIER_ENFORCEMENT[tier] ?? TIER_ENFORCEMENT['custom'];
14
+ return {
15
+ name: raw['name'] ?? '',
16
+ version: raw['version'] ?? 1,
17
+ tier: tier,
18
+ category: (raw['category'] ?? 'safety'),
19
+ description: raw['description'] ?? '',
20
+ events: raw['events'] ?? [],
21
+ conditions: (raw['conditions'] ?? []).map(c => ({
22
+ field: c.field,
23
+ op: c.op,
24
+ pattern: c.pattern,
25
+ })),
26
+ action: (raw['action'] ?? 'warn'),
27
+ severity: (raw['severity'] ?? 'medium'),
28
+ message: raw['message'] ?? '',
29
+ override: {
30
+ allow_disable: raw['override']?.allow_disable ??
31
+ enforcement.allow_disable,
32
+ allow_downgrade: raw['override']?.allow_downgrade ??
33
+ enforcement.allow_downgrade,
34
+ },
35
+ enabled: true,
36
+ };
37
+ }
38
+ export async function loadRuleDirectory(dir, config) {
39
+ const rules = [];
40
+ const entries = await readdir(dir, { withFileTypes: true });
41
+ for (const entry of entries) {
42
+ const fullPath = join(dir, entry.name);
43
+ if (entry.isDirectory()) {
44
+ const subRules = await loadRuleDirectory(fullPath, config);
45
+ rules.push(...subRules);
46
+ }
47
+ else if (entry.isFile() &&
48
+ (extname(entry.name) === '.yaml' || extname(entry.name) === '.yml')) {
49
+ const rule = await loadRuleFile(fullPath);
50
+ // Apply config overrides — silently ignore disable attempts on core rules
51
+ if (config?.disabled_rules?.includes(rule.name)) {
52
+ if (rule.override.allow_disable) {
53
+ rule.enabled = false;
54
+ }
55
+ }
56
+ rules.push(rule);
57
+ }
58
+ }
59
+ return rules;
60
+ }
61
+ //# sourceMappingURL=rule-loader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rule-loader.js","sourceRoot":"","sources":["../../src/core/rule-loader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,KAAK,IAAI,SAAS,EAAE,MAAM,MAAM,CAAC;AAG1C,MAAM,gBAAgB,GAAyE;IAC7F,IAAI,EAAE,EAAE,aAAa,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AO,GAAG,KAAK,CAAC;gBACvB,CAAC;YACH,CAAC;YAED,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACnB,CAAC;IACH,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -0,0 +1,53 @@
1
+ export type ReflexEvent = 'bash_command' | 'file_write' | 'file_edit' | 'file_delete' | 'prompt_submit' | 'session_start' | 'session_stop' | 'tool_call' | 'post_tool' | 'any';
2
+ export type RuleTier = 'core' | 'recommended' | 'custom';
3
+ export type RuleCategory = 'safety' | 'quality' | 'style' | 'cognitive';
4
+ export type RuleAction = 'block' | 'warn' | 'log';
5
+ export type RuleSeverity = 'critical' | 'high' | 'medium' | 'low' | 'info';
6
+ export type ConditionOp = 'matches' | 'not_matches' | 'contains' | 'not_contains' | 'equals' | 'not_equals' | 'starts_with' | 'ends_with' | 'regex';
7
+ export interface ReflexCondition {
8
+ field: string;
9
+ op: ConditionOp;
10
+ pattern: string;
11
+ }
12
+ export interface RuleOverride {
13
+ allow_disable: boolean;
14
+ allow_downgrade: boolean;
15
+ }
16
+ export interface ReflexRule {
17
+ name: string;
18
+ version: number;
19
+ tier: RuleTier;
20
+ category: RuleCategory;
21
+ description: string;
22
+ events: ReflexEvent[];
23
+ conditions: ReflexCondition[];
24
+ action: RuleAction;
25
+ severity: RuleSeverity;
26
+ message: string;
27
+ override: RuleOverride;
28
+ enabled: boolean;
29
+ }
30
+ export interface ReflexEventData {
31
+ event: ReflexEvent;
32
+ tool_name?: string;
33
+ file_path?: string;
34
+ command?: string;
35
+ content?: string;
36
+ prompt?: string;
37
+ [key: string]: unknown;
38
+ }
39
+ export interface EvaluationResult {
40
+ rule: string;
41
+ triggered: boolean;
42
+ action: RuleAction;
43
+ severity: RuleSeverity;
44
+ message: string;
45
+ matched_conditions: string[];
46
+ }
47
+ export interface ReflexConfig {
48
+ allowed_roots?: string[];
49
+ variables?: Record<string, string | string[]>;
50
+ disabled_rules?: string[];
51
+ rule_dirs?: string[];
52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EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,YAAY,CAAC;IACvB,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,MAAM,WAAW,eAAe;IAC9B,KAAK,EAAE,WAAW,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,OAAO,CAAC;IACnB,MAAM,EAAE,UAAU,CAAC;IACnB,QAAQ,EAAE,YAAY,CAAC;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,kBAAkB,EAAE,MAAM,EAAE,CAAC;CAC9B;AAED,MAAM,WAAW,YAAY;IAC3B,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,CAAC;IAC9C,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;CACtB"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/core/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,5 @@
1
+ export { RuleEngine } from './core/rule-engine.js';
2
+ export { loadRuleFile, loadRuleDirectory } from './core/rule-loader.js';
3
+ export { CORE_RULES } from './core/defaults.js';
4
+ export type { ReflexRule, ReflexEventData, EvaluationResult, ReflexCondition, ReflexConfig, ReflexEvent, RuleTier, RuleCategory, RuleAction, RuleSeverity, ConditionOp, RuleOverride, } from './core/types.js';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AACxE,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,YAAY,EACV,UAAU,EACV,eAAe,EACf,gBAAgB,EAChB,eAAe,EACf,YAAY,EACZ,WAAW,EACX,QAAQ,EACR,YAAY,EACZ,UAAU,EACV,YAAY,EACZ,WAAW,EACX,YAAY,GACb,MAAM,iBAAiB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export { RuleEngine } from './core/rule-engine.js';
2
+ export { loadRuleFile, loadRuleDirectory } from './core/rule-loader.js';
3
+ export { CORE_RULES } from './core/defaults.js';
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AACxE,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC"}
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@fozikio/reflex",
3
+ "version": "0.1.1",
4
+ "description": "Portable guardrails for any agent runtime — safety rules as data, not code",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "bin": {
9
+ "reflex": "dist/cli/index.js"
10
+ },
11
+ "scripts": {
12
+ "build": "tsc",
13
+ "dev": "tsc --watch",
14
+ "test": "vitest run"
15
+ },
16
+ "engines": {
17
+ "node": ">=20"
18
+ },
19
+ "dependencies": {
20
+ "yaml": "^2.7.0"
21
+ },
22
+ "devDependencies": {
23
+ "@types/node": "^22.10.0",
24
+ "typescript": "^5.7.0",
25
+ "vitest": "^3.0.0"
26
+ },
27
+ "files": [
28
+ "dist",
29
+ "rules",
30
+ "LICENSE",
31
+ "README.md"
32
+ ],
33
+ "exports": {
34
+ ".": {
35
+ "import": "./dist/index.js",
36
+ "types": "./dist/index.d.ts"
37
+ }
38
+ },
39
+ "keywords": [
40
+ "reflex",
41
+ "guardrails",
42
+ "safety",
43
+ "ai-agent",
44
+ "hooks",
45
+ "mcp"
46
+ ],
47
+ "license": "MIT",
48
+ "author": "Fozikio <hello@fozikio.com>",
49
+ "repository": {
50
+ "type": "git",
51
+ "url": "https://github.com/Fozikio/reflex.git"
52
+ },
53
+ "homepage": "https://github.com/Fozikio/reflex#readme",
54
+ "funding": {
55
+ "type": "github",
56
+ "url": "https://github.com/sponsors/Fozikio"
57
+ }
58
+ }
@@ -0,0 +1,16 @@
1
+ name: credential-protection
2
+ version: 1
3
+ tier: core
4
+ category: safety
5
+ description: Block hardcoded secrets in source files
6
+ events: [file_write, file_edit]
7
+ conditions:
8
+ - field: content
9
+ op: regex
10
+ pattern: "(API_KEY|SECRET|TOKEN|PASSWORD)\\s*=\\s*[\"'][A-Za-z0-9]{20,}"
11
+ action: block
12
+ severity: critical
13
+ message: "Potential hardcoded credential detected in {{file_path}}"
14
+ override:
15
+ allow_disable: false
16
+ allow_downgrade: false
@@ -0,0 +1,16 @@
1
+ name: destructive-commands
2
+ version: 1
3
+ tier: core
4
+ category: safety
5
+ description: Block catastrophic shell commands
6
+ events: [bash_command]
7
+ conditions:
8
+ - field: command
9
+ op: regex
10
+ pattern: "(rm\\s+-rf\\s+[/~.]|format\\s|diskpart|fdisk|mkfs|del\\s+/s\\s+/q)"
11
+ action: block
12
+ severity: critical
13
+ message: "Destructive command blocked: {{command}}"
14
+ override:
15
+ allow_disable: false
16
+ allow_downgrade: false
@@ -0,0 +1,16 @@
1
+ name: path-traversal
2
+ version: 1
3
+ tier: core
4
+ category: safety
5
+ description: Block delete commands with tilde or parent traversal in paths
6
+ events: [bash_command, file_delete]
7
+ conditions:
8
+ - field: command
9
+ op: regex
10
+ pattern: "(rm|del|Remove-Item).*[~]|\\.\\."
11
+ action: block
12
+ severity: critical
13
+ message: "Path traversal in delete command blocked: {{command}}"
14
+ override:
15
+ allow_disable: false
16
+ allow_downgrade: false
@@ -0,0 +1,19 @@
1
+ name: write-boundary
2
+ version: 1
3
+ tier: core
4
+ category: safety
5
+ description: Block file writes outside allowed locations
6
+ events: [file_write, file_edit]
7
+ conditions:
8
+ - field: file_path
9
+ op: not_matches
10
+ pattern: "{{allowed_roots}}"
11
+ action: block
12
+ severity: critical
13
+ message: |
14
+ Write boundary violation.
15
+ Attempted to modify: {{file_path}}
16
+ Allowed roots: {{allowed_roots}}
17
+ override:
18
+ allow_disable: false
19
+ allow_downgrade: false