@theoremjs/agents 0.3.4-next.841.g5f7aa38 → 0.3.4-next.843.ga3bad83

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.
@@ -16,9 +16,9 @@ export interface GuardedToolText {
16
16
  /** Emitted when the guard did anything worth recording. */
17
17
  event?: GuardrailEvent;
18
18
  /**
19
- * What `injection` found in a remote tool's text. Legitimate tool output is frequently
20
- * instruction-shaped, so the detector starts at `flag`; whatever its action,
21
- * these raise the turn's taint.
19
+ * What `injection` found in a remote tool's text, a result or a failure's message. Legitimate
20
+ * tool output is frequently instruction-shaped, so the detector starts at `flag`; whatever its
21
+ * action, these raise the turn's taint.
22
22
  */
23
23
  suspicious?: GuardrailHit[];
24
24
  }
@@ -32,9 +32,9 @@ declare function composeToolText(finding: string, data: unknown): string;
32
32
  * data the host did not write — but only remote origins are fenced, because
33
33
  * fencing a local tool's output would change prompts hosts have already tuned.
34
34
  * `boundary` is absent for text that crosses none: a provider builtin's, or a
35
- * failure already read at its own boundary.
35
+ * failure already read at its own boundary, whose findings come in as `carried`.
36
36
  */
37
- declare function guardToolResult(finding: string, data: unknown, provenance: Provenance, policy: ResolvedGuardrailPolicy, boundary: ToolBoundary | undefined, callableTools?: readonly string[], labels?: LabelOverrides): GuardedToolText;
37
+ declare function guardToolResult(finding: string, data: unknown, provenance: Provenance, policy: ResolvedGuardrailPolicy, boundary: ToolBoundary | undefined, callableTools?: readonly string[], labels?: LabelOverrides, carried?: GuardrailHit[]): GuardedToolText;
38
38
  /**
39
39
  * A remote server authors its own error strings, so an unguarded failure message
40
40
  * is the cleanest injection path across this boundary: it reaches the model
@@ -94,20 +94,24 @@ const NOTHING_FOUND = { action: 'allow', hits: [] };
94
94
  function readAt(text, boundary, detect, scope) {
95
95
  return boundary ? detectAt(text, boundary, detect, scope) : { ...NOTHING_FOUND, text };
96
96
  }
97
+ /** What `injection` found in `hits`, when a remote tool wrote the text; a local tool's raise nothing. */
98
+ function suspiciousHits(hits, provenance) {
99
+ if (!isRemoteOrigin(provenance.origin))
100
+ return [];
101
+ return hits.filter(({ rule }) => rule === DETECT_RULES.injection);
102
+ }
97
103
  /**
98
104
  * Local host tools are still detected — a host tool reading a database returns
99
105
  * data the host did not write — but only remote origins are fenced, because
100
106
  * fencing a local tool's output would change prompts hosts have already tuned.
101
107
  * `boundary` is absent for text that crosses none: a provider builtin's, or a
102
- * failure already read at its own boundary.
108
+ * failure already read at its own boundary, whose findings come in as `carried`.
103
109
  */
104
- function guardToolResult(finding, data, provenance, policy, boundary, callableTools = [], labels) {
110
+ function guardToolResult(finding, data, provenance, policy, boundary, callableTools = [], labels, carried = []) {
105
111
  const composed = composeToolText(finding, data);
106
112
  const detected = readAt(composed, boundary, policy.detect, { callable: callableTools });
107
113
  const remote = isRemoteOrigin(provenance.origin);
108
- const suspicious = remote
109
- ? detected.hits.filter(({ rule }) => rule === DETECT_RULES.injection)
110
- : [];
114
+ const suspicious = [...carried, ...suspiciousHits(detected.hits, provenance)];
111
115
  const event = detected.action === 'allow'
112
116
  ? undefined
113
117
  : {
@@ -137,9 +141,11 @@ function guardToolResult(finding, data, provenance, policy, boundary, callableTo
137
141
  function guardToolFailureText(message, provenance, policy, boundary) {
138
142
  const detected = readAt(stripToolFences(message), boundary, policy.detect);
139
143
  const event = boundary ? detectEvent(boundary, detected, provenance) : undefined;
144
+ const suspicious = detected.text === undefined ? [] : suspiciousHits(detected.hits, provenance);
140
145
  return {
141
146
  ...(detected.text === undefined ? {} : { text: detected.text }),
142
147
  ...(event ? { event } : {}),
148
+ ...(suspicious.length > 0 ? { suspicious } : {}),
143
149
  };
144
150
  }
145
151
  /**
@@ -1194,12 +1194,18 @@ function* guardFailure(failure, guard) {
1194
1194
  yield { type: 'guardrail', guardrail: checked.event };
1195
1195
  }
1196
1196
  const message = checked.text ?? labelText('detect.output_blocked', {}, guard.labels);
1197
- return { modelResult: yield* reportFailure({ ...failure, message }, guard) };
1197
+ return {
1198
+ modelResult: yield* reportFailure({ ...failure, message }, guard, checked.suspicious),
1199
+ };
1198
1200
  }
1199
- /** A failure framed for the model in the kernel's own words, fenced and labelled like any result. */
1200
- function* reportFailure(failure, guard) {
1201
+ /**
1202
+ * A failure framed for the model in the kernel's own words, fenced and labelled like any result.
1203
+ * `suspicious` is what `injection` found in a remote failure's message, which raises taint as a
1204
+ * result's does.
1205
+ */
1206
+ function* reportFailure(failure, guard, suspicious) {
1201
1207
  const framed = frameToolFailure(failure);
1202
- return (yield* readBack(framed, guard, undefined)) ?? framed;
1208
+ return (yield* readBack(framed, guard, undefined, suspicious)) ?? framed;
1203
1209
  }
1204
1210
  /** A result read at `tool_output_<kind>`. A blocked one settles as a failed call the model is told about. */
1205
1211
  function* guardResult(result, guard) {
@@ -1222,10 +1228,10 @@ function* showValue(value, guard) {
1222
1228
  return 'value' in shown ? shown.value : labelText('detect.shown_blocked', {}, guard.labels);
1223
1229
  }
1224
1230
  /** What the model reads of `result`, or `undefined` when a match at `boundary` blocks it. */
1225
- function* readBack(result, guard, boundary) {
1231
+ function* readBack(result, guard, boundary, carried) {
1226
1232
  const { provenance, span } = guard;
1227
1233
  const start = performance.now();
1228
- const guarded = guardToolResult(result.finding, result.data, provenance, guard.policy, boundary, guard.callableTools, guard.labels);
1234
+ const guarded = guardToolResult(result.finding, result.data, provenance, guard.policy, boundary, guard.callableTools, guard.labels, carried);
1229
1235
  recordToolCheck(span, 'tool_result', performance.now() - start, guarded.event);
1230
1236
  if (guarded.event) {
1231
1237
  yield { type: 'guardrail', guardrail: guarded.event };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theoremjs/agents",
3
- "version": "0.3.4-next.841.g5f7aa38",
3
+ "version": "0.3.4-next.843.ga3bad83",
4
4
  "description": "A flat TypeScript agent kernel for typed profiles, deterministic turn execution, registered tools, provider adapters, guardrails, and host-injected traces.",
5
5
  "keywords": [
6
6
  "agent",