@theoremjs/agents 0.3.4-next.831.g934609e → 0.3.4-next.841.g5f7aa38

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.
@@ -31,8 +31,10 @@ interface DetectScope extends LeakScope, Pick<GuardrailContext, 'givenUrls' | 'o
31
31
  note?: string;
32
32
  /** What `ungiven_images` and `ungiven_links` let through besides the given URLs. */
33
33
  allow?: ResolvedAllow;
34
- /** The tools the model can call this turn, for `tool_instructions`: a tool's text naming one is a match. */
34
+ /** The tools the model can call this turn, for `injection` in what a tool returns: naming one is a match. */
35
35
  callable?: readonly string[];
36
+ /** Set where a tool's output or error is read: `injection` also reads it for orders aimed at the agent. */
37
+ fromTool?: boolean;
36
38
  }
37
39
  /**
38
40
  * The {@linkcode LeakScope} of a turn under `detect`: each part only while its detector is above
@@ -33,6 +33,8 @@ function stronger(left, right) {
33
33
  /** Each sensitive group alone, for a scan that reads one detector at a time. */
34
34
  const ONLY = recordOf(SENSITIVE_GROUPS, (group) => recordOf(SENSITIVE_GROUPS, (other) => other === group));
35
35
  const NO_SCOPE = {};
36
+ /** A text read at no boundary may be what a tool returned, so `injection` reads it for orders to the agent too. */
37
+ const NO_BOUNDARY = { fromTool: true };
36
38
  /**
37
39
  * The {@linkcode LeakScope} of a turn under `detect`: each part only while its detector is above
38
40
  * `ignore` somewhere, so a profile that reads for neither binds nothing to read for.
@@ -86,7 +88,6 @@ function hostSpans(text, matchers, kind) {
86
88
  /** The placeholder a host's pattern leaves for a detector, where it is not the sensitive one. */
87
89
  const HOST_KIND = {
88
90
  injection: 'injection',
89
- tool_instructions: 'directive',
90
91
  tool_leak: 'tool',
91
92
  };
92
93
  /**
@@ -101,8 +102,15 @@ function spansOf(detector, text, scope, source, skipImages = false) {
101
102
  }
102
103
  /** With `skipImages`, `ungiven_links` leaves an image's own markup to `ungiven_images`. */
103
104
  function theoremSpans(detector, text, scope, skipImages = false) {
104
- if (detector === 'injection')
105
- return injectionSpans(text);
105
+ if (detector === 'injection') {
106
+ if (!scope.fromTool)
107
+ return injectionSpans(text);
108
+ const orders = directives(text, scope.callable).map((found) => ({
109
+ ...found,
110
+ kind: 'directive',
111
+ }));
112
+ return [...injectionSpans(text), ...orders];
113
+ }
106
114
  if (detector === 'marker_leak') {
107
115
  const patterns = [MARKERS, ...(scope.note ? [notePattern(scope.note)] : [])];
108
116
  return spansFromPatterns(text, patterns, 'prompt');
@@ -125,9 +133,6 @@ function theoremSpans(detector, text, scope, skipImages = false) {
125
133
  }
126
134
  if (detector === 'tool_leak')
127
135
  return toolLeakSpans(text, scope.ownTools);
128
- if (detector === 'tool_instructions') {
129
- return directives(text, scope.callable).map((found) => ({ ...found, kind: 'directive' }));
130
- }
131
136
  return sensitiveSpans(text, ONLY[detector]);
132
137
  }
133
138
  /** The hit for a match of `detector`. A canary leak never carries the token. */
@@ -137,7 +142,7 @@ function hitOf(detector, text, span) {
137
142
  return { ...CANARY_HIT, span: { start, end } };
138
143
  if (detector === 'prompt_leak')
139
144
  return promptEchoHit(text, start, end);
140
- if (detector === 'tool_instructions' && span.signal) {
145
+ if (detector === 'injection' && span.signal) {
141
146
  const severity = span.signal === 'order' || span.signal === 'authority' ? 'medium' : 'high';
142
147
  return { ...hitFromSpan(text, span, DETECT_RULES[detector], severity), signal: span.signal };
143
148
  }
@@ -203,6 +208,10 @@ function hostReader(host, boundary, chosen) {
203
208
  function hostAt(boundary, detect) {
204
209
  return (detect.host ?? []).filter(({ actions }) => actions[boundary] !== 'ignore');
205
210
  }
211
+ /** Whether `boundary` is where a tool's output or error reaches the model. */
212
+ function isToolReturn(boundary) {
213
+ return boundary.startsWith('tool_output_') || boundary.startsWith('tool_failure_');
214
+ }
206
215
  /** Every detector that reads `boundary`, Theorem's then the host's; among `keys` when given. */
207
216
  function readersAt(boundary, detect, scope, keys) {
208
217
  const readers = [];
@@ -211,11 +220,12 @@ function readersAt(boundary, detect, scope, keys) {
211
220
  if (chosen === 'ignore')
212
221
  continue;
213
222
  const skip = leavesImages(detector, boundary, detect);
223
+ const read = isToolReturn(boundary) ? { ...scope, fromTool: true } : scope;
214
224
  readers.push({
215
225
  key: detector,
216
226
  rule: DETECT_RULES[detector],
217
227
  chosen,
218
- spans: (text) => spansOf(detector, text, scope, detect.sources?.[detector], skip),
228
+ spans: (text) => spansOf(detector, text, read, detect.sources?.[detector], skip),
219
229
  hit: (text, span) => hitOf(detector, text, span),
220
230
  });
221
231
  }
@@ -365,7 +375,7 @@ function detectRelease(window, { from, to, settled }, boundary, detect, { detect
365
375
  }
366
376
  /** `text` with every match of `detectors` replaced by its placeholder, whatever a profile sets. */
367
377
  function redactDetectors(text, detectors) {
368
- return applySpans(text, detectors.flatMap((detector) => spansOf(detector, text, NO_SCOPE)));
378
+ return applySpans(text, detectors.flatMap((detector) => spansOf(detector, text, NO_BOUNDARY)));
369
379
  }
370
380
  /**
371
381
  * What `detectors` find in a text a trace stores, whatever action a profile sets. `detect` is
@@ -377,7 +387,7 @@ function storedSpans(text, detectors, sides, detect) {
377
387
  const source = sides === true
378
388
  ? turn
379
389
  : { theorem: sides.theorem, matchers: sides.host ? (turn?.matchers ?? []) : [] };
380
- return spansOf(detector, text, NO_SCOPE, source);
390
+ return spansOf(detector, text, NO_BOUNDARY, source);
381
391
  });
382
392
  }
383
393
  /** What a host's own detectors find in a text a trace stores. A `find` that fails covers it all. */
@@ -7,7 +7,7 @@ import { type CompiledPatterns, type HostMatcher, type HostPattern } from './hos
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", "email", "phone", "bank_account", "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", "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
13
  /** The detectors a text is cleaned with when no profile says how: every one but the working groups (`WORKING_GROUPS`). */
@@ -14,7 +14,6 @@ import { SENSITIVE_GROUPS, WORKING_GROUPS } from './sensitive-groups.js';
14
14
  const DETECTORS = [
15
15
  ...SENSITIVE_GROUPS,
16
16
  'injection',
17
- 'tool_instructions',
18
17
  'canary_leak',
19
18
  'prompt_leak',
20
19
  'marker_leak',
@@ -54,18 +53,21 @@ const DETECTOR_GROUP_META = {
54
53
  const TOOL_ARGUMENT_BOUNDARIES = TOOL_KINDS.map((kind) => toolBoundary('tool_arguments', kind));
55
54
  const TOOL_SHOWN_BOUNDARIES = TOOL_KINDS.map((kind) => toolBoundary('tool_shown', kind));
56
55
  /**
57
- * Text on its way to the model is flagged, a tool call takes `toTool`, and what the model writes
58
- * or a tool shows the person is not read.
56
+ * Text on its way to the model is flagged, a tool call takes `toTool`, and what a tool shows the
57
+ * person is not read. What the model says or thinks is left out: that text goes to the person.
59
58
  */
60
- function everywhere(toTool) {
59
+ function notToPerson(toTool) {
61
60
  return {
62
61
  ...recordOf(INBOUND_BOUNDARIES, () => 'flag'),
63
62
  ...recordOf(TOOL_BOUNDARIES, () => 'flag'),
64
63
  ...recordOf(TOOL_ARGUMENT_BOUNDARIES, () => toTool),
65
64
  ...recordOf(TOOL_SHOWN_BOUNDARIES, () => 'ignore'),
66
- ...recordOf(OUTBOUND_BOUNDARIES, () => 'ignore'),
67
65
  };
68
66
  }
67
+ /** As `notToPerson`, and what the model says or thinks is not read. */
68
+ function everywhere(toTool) {
69
+ return { ...notToPerson(toTool), ...recordOf(OUTBOUND_BOUNDARIES, () => 'ignore') };
70
+ }
69
71
  /** What the model writes: a tool call takes `toTool`, what it says takes `said`, a thought `thought`. */
70
72
  function leaving(toTool, said, thought) {
71
73
  return {
@@ -76,14 +78,6 @@ function leaving(toTool, said, thought) {
76
78
  thought,
77
79
  };
78
80
  }
79
- /** What a tool returns, its output, and its error text: `remote` from a tool the host did not write, `local` from a function. */
80
- function returned(remote, local) {
81
- const crossings = ['tool_output', 'tool_failure'];
82
- return Object.fromEntries(crossings.flatMap((crossing) => TOOL_KINDS.map((kind) => [
83
- toolBoundary(crossing, kind),
84
- kind === 'function' ? local : remote,
85
- ])));
86
- }
87
81
  /** What the model says or thinks: a reply of any kind takes `said`, a thought `thought`. */
88
82
  function shown(said, thought) {
89
83
  return { reply: said, reply_structured: said, live_reply: said, thought };
@@ -148,17 +142,9 @@ const DETECTOR_META = {
148
142
  },
149
143
  injection: {
150
144
  label: 'Injection',
151
- doc: 'Prompt-injection phrasing, plain or disguised.',
152
- group: 'manipulation',
153
- defaults: everywhere('ignore'),
154
- patterns: true,
155
- },
156
- tool_instructions: {
157
- label: 'Tool instructions',
158
- short: 'From tools',
159
- doc: 'Tool output that instructs the agent. A match raises taint.',
145
+ doc: 'Prompt-injection phrasing, plain or disguised, and orders to the agent in what a tool returns. A match there raises taint.',
160
146
  group: 'manipulation',
161
- defaults: returned('flag', 'ignore'),
147
+ defaults: notToPerson('ignore'),
162
148
  patterns: true,
163
149
  },
164
150
  canary_leak: {