@theoremjs/agents 0.3.4-next.815.g7f5fa76 → 0.3.4-next.819.g62d1f4f
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/esm/src/guardrails/credential-scan.js +83 -4
- package/esm/src/guardrails/detectors.d.ts +5 -3
- package/esm/src/guardrails/detectors.js +26 -3
- package/esm/src/guardrails/egress-automata.js +20030 -19038
- package/esm/src/guardrails/labels.d.ts +1 -1
- package/esm/src/guardrails/labels.js +9 -0
- package/esm/src/guardrails/normalize.js +41 -6
- package/esm/src/guardrails/rules.d.ts +3 -0
- package/esm/src/guardrails/rules.js +3 -0
- package/esm/src/guardrails/sensitive-groups.d.ts +11 -3
- package/esm/src/guardrails/sensitive-groups.js +18 -2
- package/esm/src/guardrails/sensitive.d.ts +1 -1
- package/esm/src/guardrails/sensitive.js +138 -16
- package/esm/src/kernel/tools/model-text.d.ts +4 -4
- package/esm/src/kernel/tools/model-text.js +10 -7
- package/esm/src/observability/trace-catalog.js +12 -0
- package/esm/src/observability/trace-record.js +2 -2
- package/esm/src/surface/formats.js +2 -2
- package/package.json +1 -1
|
@@ -108,6 +108,75 @@ function allowed(list, read, scan) {
|
|
|
108
108
|
}
|
|
109
109
|
return answer;
|
|
110
110
|
}
|
|
111
|
+
function keywordNode() {
|
|
112
|
+
return { goto: new Map(), next: new Map(), fail: undefined, ends: [] };
|
|
113
|
+
}
|
|
114
|
+
const AUTOMATA = new WeakMap();
|
|
115
|
+
function keywordTrie(words) {
|
|
116
|
+
const root = keywordNode();
|
|
117
|
+
for (const word of words) {
|
|
118
|
+
let node = root;
|
|
119
|
+
for (let i = 0; i < word.length; i += 1) {
|
|
120
|
+
const code = word.charCodeAt(i);
|
|
121
|
+
const child = node.goto.get(code) ?? keywordNode();
|
|
122
|
+
node.goto.set(code, child);
|
|
123
|
+
node = child;
|
|
124
|
+
}
|
|
125
|
+
node.ends.push(word);
|
|
126
|
+
}
|
|
127
|
+
return root;
|
|
128
|
+
}
|
|
129
|
+
/** Links each node of `root`'s trie to its longest proper suffix, breadth first, and folds that in. */
|
|
130
|
+
function linkTrie(root) {
|
|
131
|
+
const queue = [root];
|
|
132
|
+
for (let head = 0; head < queue.length; head += 1) {
|
|
133
|
+
const node = queue[head] ?? root;
|
|
134
|
+
const back = node.fail;
|
|
135
|
+
if (back) {
|
|
136
|
+
node.ends.push(...back.ends);
|
|
137
|
+
for (const [code, target] of back.next)
|
|
138
|
+
node.next.set(code, target);
|
|
139
|
+
}
|
|
140
|
+
for (const [code, child] of node.goto) {
|
|
141
|
+
node.next.set(code, child);
|
|
142
|
+
child.fail = back?.next.get(code) ?? root;
|
|
143
|
+
queue.push(child);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
function keywordAutomaton(rules) {
|
|
148
|
+
const cached = AUTOMATA.get(rules);
|
|
149
|
+
if (cached)
|
|
150
|
+
return cached;
|
|
151
|
+
const words = [...new Set(rules.flatMap((rule) => rule.keywords))];
|
|
152
|
+
const root = keywordTrie(words);
|
|
153
|
+
linkTrie(root);
|
|
154
|
+
const automaton = { words: words.length, root };
|
|
155
|
+
AUTOMATA.set(rules, automaton);
|
|
156
|
+
return automaton;
|
|
157
|
+
}
|
|
158
|
+
/** Where each keyword of `rules` is first in `lower`, by keyword: what `indexOf` finds. */
|
|
159
|
+
function firstKeywords(lower, rules) {
|
|
160
|
+
const { words, root } = keywordAutomaton(rules);
|
|
161
|
+
const first = new Map();
|
|
162
|
+
let node = root;
|
|
163
|
+
for (let i = 0; i < lower.length && first.size < words; i += 1) {
|
|
164
|
+
node = node.next.get(lower.charCodeAt(i)) ?? root;
|
|
165
|
+
for (const word of node.ends) {
|
|
166
|
+
if (!first.has(word))
|
|
167
|
+
first.set(word, i + 1 - word.length);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return first;
|
|
171
|
+
}
|
|
172
|
+
/** Whether one of `rule`'s keywords is first in the text by `end`, as `firstKeywords` found them. */
|
|
173
|
+
function keywordsBy(rule, first, end) {
|
|
174
|
+
return (rule.keywords.length === 0 ||
|
|
175
|
+
rule.keywords.some((word) => {
|
|
176
|
+
const at = first.get(word);
|
|
177
|
+
return at !== undefined && at + word.length <= end;
|
|
178
|
+
}));
|
|
179
|
+
}
|
|
111
180
|
/** Whether one of `rule`'s keywords is in `lower`, the text in lower case, by `end`. */
|
|
112
181
|
function keywordsIn(rule, lower, end = lower.length) {
|
|
113
182
|
return (rule.keywords.length === 0 ||
|
|
@@ -117,6 +186,16 @@ function keywordsIn(rule, lower, end = lower.length) {
|
|
|
117
186
|
}));
|
|
118
187
|
}
|
|
119
188
|
const FINDERS = new WeakMap();
|
|
189
|
+
const GLOBALS = new WeakMap();
|
|
190
|
+
/** `pattern` reading every match. */
|
|
191
|
+
function everyMatch(pattern) {
|
|
192
|
+
let found = GLOBALS.get(pattern);
|
|
193
|
+
if (!found) {
|
|
194
|
+
found = new RegExp(pattern.source, `${pattern.flags.replace('g', '')}g`);
|
|
195
|
+
GLOBALS.set(pattern, found);
|
|
196
|
+
}
|
|
197
|
+
return found;
|
|
198
|
+
}
|
|
120
199
|
/** `pattern` with its indices, read from the start of a string. */
|
|
121
200
|
function finder(pattern) {
|
|
122
201
|
let found = FINDERS.get(pattern);
|
|
@@ -167,14 +246,14 @@ function credentialSecret(rule, global, text, match, at, scan = scanOf(text)) {
|
|
|
167
246
|
/** The secrets `rules` find in `text`, each one's span. */
|
|
168
247
|
function credentialSpans(text, rules, global) {
|
|
169
248
|
const lower = text.toLowerCase();
|
|
249
|
+
const first = firstKeywords(lower, rules);
|
|
170
250
|
const scan = scanOf(text);
|
|
171
251
|
const spans = [];
|
|
172
252
|
for (const rule of rules) {
|
|
173
|
-
if (!
|
|
253
|
+
if (!keywordsBy(rule, first, lower.length))
|
|
174
254
|
continue;
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
if (match[0] === '' || !keywordsIn(rule, lower, match.index + match[0].length))
|
|
255
|
+
for (const match of text.matchAll(everyMatch(rule.pattern))) {
|
|
256
|
+
if (match[0] === '' || !keywordsBy(rule, first, match.index + match[0].length))
|
|
178
257
|
continue;
|
|
179
258
|
const secret = credentialSecret(rule, global, text, match[0], match.index, scan);
|
|
180
259
|
if (secret)
|
|
@@ -2,14 +2,16 @@
|
|
|
2
2
|
import { type Boundary } from './boundaries.js';
|
|
3
3
|
import { type CompiledPatterns, type HostMatcher, type HostPattern } from './host-patterns.js';
|
|
4
4
|
/**
|
|
5
|
-
* What the kernel finds in text: the
|
|
5
|
+
* What the kernel finds in text: the families of sensitive data, prompt-injection phrasing,
|
|
6
6
|
* a tool's text that instructs the agent, and what is the profile's own on its way out (the canary token, the system instruction, the
|
|
7
7
|
* kernel's markers, an image or link to an address the model was not given, and the names of its
|
|
8
8
|
* tools).
|
|
9
9
|
*/
|
|
10
|
-
declare const DETECTORS: readonly ["ids", "financial", "network", "credentials", "injection", "tool_instructions", "canary_leak", "prompt_leak", "marker_leak", "ungiven_images", "ungiven_links", "tool_leak"];
|
|
10
|
+
declare const DETECTORS: readonly ["ids", "financial", "network", "credentials", "email", "phone", "bank_account", "injection", "tool_instructions", "canary_leak", "prompt_leak", "marker_leak", "ungiven_images", "ungiven_links", "tool_leak"];
|
|
11
11
|
/** A detector recognizes a particular kind of content in text. Its configured action determines what happens when it finds a match. */
|
|
12
12
|
type Detector = (typeof DETECTORS)[number];
|
|
13
|
+
/** The detectors a text is cleaned with when no profile says how: every one but the working groups (`WORKING_GROUPS`). */
|
|
14
|
+
declare const UNPROFILED_DETECTORS: readonly Detector[];
|
|
13
15
|
/**
|
|
14
16
|
* What the kernel does with a match, the same at every boundary:
|
|
15
17
|
* - `ignore` — the text is not checked.
|
|
@@ -213,4 +215,4 @@ declare function isDetector(value: string): value is Detector;
|
|
|
213
215
|
*/
|
|
214
216
|
declare function detectProblem(path: string, spec: unknown, boundaries?: readonly Boundary[]): string | undefined;
|
|
215
217
|
export type { DetectAction, DetectMatrix, DetectMeta, Detector, DetectorConfig, DetectorDeclaration, DetectorGroup, DetectorRule, DetectorSource, DetectSources, DetectSpec, HostDetector, HostDetectorConfig, HostDetectorId, HostFind, HostSpan, NameAllow, ResolvedAllow, ResolvedDetect, UrlAllow, UrlDetector, };
|
|
216
|
-
export { actionAt, DETECT_ACTION_META, DETECT_ACTIONS, DETECT_DEFAULTS, DETECTOR_BOUNDARIES, DETECTOR_GROUP_META, DETECTOR_GROUPS, DETECTOR_META, DETECTORS, detectProblem, detects, HOST_FIND_HOLD, HOST_FIND_HOLD_LIVE, hintProblem, isDetector, NO_ALLOW, PATTERN_DETECTORS, resolveAllow, resolveDetect, };
|
|
218
|
+
export { actionAt, DETECT_ACTION_META, DETECT_ACTIONS, DETECT_DEFAULTS, DETECTOR_BOUNDARIES, DETECTOR_GROUP_META, DETECTOR_GROUPS, DETECTOR_META, DETECTORS, detectProblem, detects, HOST_FIND_HOLD, HOST_FIND_HOLD_LIVE, hintProblem, isDetector, NO_ALLOW, PATTERN_DETECTORS, resolveAllow, resolveDetect, UNPROFILED_DETECTORS, };
|
|
@@ -4,9 +4,9 @@
|
|
|
4
4
|
/** labels-exempt-file: authoring labels, descriptions and profile-registration diagnostics for detectors — not runtime user or model copy (P2) */
|
|
5
5
|
import { BOUNDARIES, INBOUND_BOUNDARIES, OUTBOUND_BOUNDARIES, recordOf, TOOL_BOUNDARIES, TOOL_KINDS, toolBoundary, } from './boundaries.js';
|
|
6
6
|
import { matchersOf, patternsProblem, } from './host-patterns.js';
|
|
7
|
-
import { SENSITIVE_GROUPS } from './sensitive-groups.js';
|
|
7
|
+
import { SENSITIVE_GROUPS, WORKING_GROUPS } from './sensitive-groups.js';
|
|
8
8
|
/**
|
|
9
|
-
* What the kernel finds in text: the
|
|
9
|
+
* What the kernel finds in text: the families of sensitive data, prompt-injection phrasing,
|
|
10
10
|
* a tool's text that instructs the agent, and what is the profile's own on its way out (the canary token, the system instruction, the
|
|
11
11
|
* kernel's markers, an image or link to an address the model was not given, and the names of its
|
|
12
12
|
* tools).
|
|
@@ -22,6 +22,8 @@ const DETECTORS = [
|
|
|
22
22
|
'ungiven_links',
|
|
23
23
|
'tool_leak',
|
|
24
24
|
];
|
|
25
|
+
/** The detectors a text is cleaned with when no profile says how: every one but the working groups (`WORKING_GROUPS`). */
|
|
26
|
+
const UNPROFILED_DETECTORS = DETECTORS.filter((detector) => !WORKING_GROUPS.includes(detector));
|
|
25
27
|
/**
|
|
26
28
|
* What the kernel does with a match, the same at every boundary:
|
|
27
29
|
* - `ignore` — the text is not checked.
|
|
@@ -123,6 +125,27 @@ const DETECTOR_META = {
|
|
|
123
125
|
},
|
|
124
126
|
patterns: true,
|
|
125
127
|
},
|
|
128
|
+
email: {
|
|
129
|
+
label: 'Email',
|
|
130
|
+
doc: 'Email addresses.',
|
|
131
|
+
group: 'data',
|
|
132
|
+
defaults: everywhere('flag'),
|
|
133
|
+
patterns: true,
|
|
134
|
+
},
|
|
135
|
+
phone: {
|
|
136
|
+
label: 'Phone',
|
|
137
|
+
doc: 'Phone numbers: international, North American, and national, or any number after a phone label.',
|
|
138
|
+
group: 'data',
|
|
139
|
+
defaults: everywhere('flag'),
|
|
140
|
+
patterns: true,
|
|
141
|
+
},
|
|
142
|
+
bank_account: {
|
|
143
|
+
label: 'Bank account',
|
|
144
|
+
doc: 'Account, routing, and sort-code numbers after a label that names them. IBANs are `financial`.',
|
|
145
|
+
group: 'data',
|
|
146
|
+
defaults: everywhere('flag'),
|
|
147
|
+
patterns: true,
|
|
148
|
+
},
|
|
126
149
|
injection: {
|
|
127
150
|
label: 'Injection',
|
|
128
151
|
doc: 'Prompt-injection phrasing, plain or disguised.',
|
|
@@ -499,4 +522,4 @@ function hostProblem(path, id, rule, boundaries) {
|
|
|
499
522
|
hintProblem(`${path}.hint`, hint) ??
|
|
500
523
|
patternsProblem(path, patterns, compiled));
|
|
501
524
|
}
|
|
502
|
-
export { actionAt, DETECT_ACTION_META, DETECT_ACTIONS, DETECT_DEFAULTS, DETECTOR_BOUNDARIES, DETECTOR_GROUP_META, DETECTOR_GROUPS, DETECTOR_META, DETECTORS, detectProblem, detects, HOST_FIND_HOLD, HOST_FIND_HOLD_LIVE, hintProblem, isDetector, NO_ALLOW, PATTERN_DETECTORS, resolveAllow, resolveDetect, };
|
|
525
|
+
export { actionAt, DETECT_ACTION_META, DETECT_ACTIONS, DETECT_DEFAULTS, DETECTOR_BOUNDARIES, DETECTOR_GROUP_META, DETECTOR_GROUPS, DETECTOR_META, DETECTORS, detectProblem, detects, HOST_FIND_HOLD, HOST_FIND_HOLD_LIVE, hintProblem, isDetector, NO_ALLOW, PATTERN_DETECTORS, resolveAllow, resolveDetect, UNPROFILED_DETECTORS, };
|