@theoremjs/agents 0.3.4-next.731.g189a95d → 0.3.4-next.735.g8d504c6
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/destinations.d.ts +6 -1
- package/esm/src/guardrails/destinations.js +16 -2
- package/esm/src/guardrails/egress-urls.js +3 -1
- package/esm/src/guardrails/labels.d.ts +1 -1
- package/esm/src/guardrails/labels.js +4 -0
- package/esm/src/guardrails/mod.d.ts +2 -1
- package/esm/src/guardrails/mod.js +2 -1
- package/esm/src/guardrails/tool-fence.d.ts +23 -0
- package/esm/src/guardrails/tool-fence.js +38 -0
- package/esm/src/guardrails/tool-result.d.ts +1 -3
- package/esm/src/guardrails/tool-result.js +2 -3
- package/esm/src/kernel/api-catalog.js +5 -0
- package/esm/src/kernel/engine/runner/mod.js +1 -0
- package/esm/src/kernel/engine/runner/state.d.ts +2 -0
- package/esm/src/kernel/engine/runner/state.js +69 -2
- package/esm/src/kernel/protocols/runtime.js +8 -1
- package/esm/src/kernel/tools/coordinate.js +4 -2
- package/esm/src/kernel/tools/execute.d.ts +3 -1
- package/esm/src/kernel/tools/execute.js +2 -2
- package/esm/src/kernel/types.d.ts +3 -1
- package/package.json +1 -1
|
@@ -30,6 +30,11 @@ declare function addResultDestinations(turn: TurnDestinations, result: {
|
|
|
30
30
|
finding: string;
|
|
31
31
|
data?: unknown;
|
|
32
32
|
}, provenance: Provenance): void;
|
|
33
|
+
/**
|
|
34
|
+
* Notes a remote result read back from its fence. `composeToolText` put any `data` on the last
|
|
35
|
+
* line, and without it a field reads as running text, which is the stricter way.
|
|
36
|
+
*/
|
|
37
|
+
declare function addFencedDestinations(turn: TurnDestinations, text: string, provenance: Provenance): void;
|
|
33
38
|
/** A call's verdict, and what an approval card says when the profile asks the user first. */
|
|
34
39
|
interface DestinationVerdict {
|
|
35
40
|
verdict: Verdict;
|
|
@@ -42,4 +47,4 @@ interface DestinationVerdict {
|
|
|
42
47
|
*/
|
|
43
48
|
declare function checkDestinationGate(turn: TurnDestinations | undefined, args: unknown, policy: ResolvedGuardrailPolicy, labels?: LabelOverrides): DestinationVerdict;
|
|
44
49
|
export type { DestinationVerdict, TurnDestinations };
|
|
45
|
-
export { addGivenDestinations, addHistoryDestinations, addRequestDestinations, addResultDestinations, checkDestinationGate, turnDestinations, };
|
|
50
|
+
export { addFencedDestinations, addGivenDestinations, addHistoryDestinations, addRequestDestinations, addResultDestinations, checkDestinationGate, turnDestinations, };
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
import { labelText } from './labels.js';
|
|
8
8
|
import { TOOL_RULES } from './rules.js';
|
|
9
9
|
import { textForScan } from './serialize.js';
|
|
10
|
+
import { isLateResult } from './tool-fence.js';
|
|
10
11
|
import { isRemoteOrigin } from './tool-result.js';
|
|
11
12
|
function turnDestinations() {
|
|
12
13
|
return { given: new Set(), remote: new Map() };
|
|
@@ -165,8 +166,12 @@ function addGivenDestinations(turn, value) {
|
|
|
165
166
|
function addHistoryDestinations(turn, messages, own) {
|
|
166
167
|
for (const message of messages) {
|
|
167
168
|
// why: An earlier turn's tool result is neither the user's word nor this turn's read.
|
|
168
|
-
if (message.role === 'assistant' ||
|
|
169
|
+
if (message.role === 'assistant' ||
|
|
170
|
+
message.role === 'tool' ||
|
|
171
|
+
own.has(message) ||
|
|
172
|
+
isLateResult(message)) {
|
|
169
173
|
continue;
|
|
174
|
+
}
|
|
170
175
|
addGivenDestinations(turn, textForScan(message).text);
|
|
171
176
|
}
|
|
172
177
|
}
|
|
@@ -198,6 +203,15 @@ function addResultDestinations(turn, result, provenance) {
|
|
|
198
203
|
turn.remote.set(key, provenance.tool);
|
|
199
204
|
}
|
|
200
205
|
}
|
|
206
|
+
/**
|
|
207
|
+
* Notes a remote result read back from its fence. `composeToolText` put any `data` on the last
|
|
208
|
+
* line, and without it a field reads as running text, which is the stricter way.
|
|
209
|
+
*/
|
|
210
|
+
function addFencedDestinations(turn, text, provenance) {
|
|
211
|
+
const cut = text.lastIndexOf('\n');
|
|
212
|
+
const data = cut < 0 ? undefined : parsedJson(text.slice(cut + 1));
|
|
213
|
+
addResultDestinations(turn, data === undefined ? { finding: text } : { finding: text.slice(0, cut), data }, provenance);
|
|
214
|
+
}
|
|
201
215
|
/**
|
|
202
216
|
* Reported whether or not the profile acts on it, so a host sees how often the
|
|
203
217
|
* rule would hold a call before it turns that on.
|
|
@@ -235,4 +249,4 @@ function checkDestinationGate(turn, args, policy, labels) {
|
|
|
235
249
|
: {}),
|
|
236
250
|
};
|
|
237
251
|
}
|
|
238
|
-
export { addGivenDestinations, addHistoryDestinations, addRequestDestinations, addResultDestinations, checkDestinationGate, turnDestinations, };
|
|
252
|
+
export { addFencedDestinations, addGivenDestinations, addHistoryDestinations, addRequestDestinations, addResultDestinations, checkDestinationGate, turnDestinations, };
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
* @module
|
|
16
16
|
*/
|
|
17
17
|
import { textForScan } from './serialize.js';
|
|
18
|
+
import { isLateResult } from './tool-fence.js';
|
|
18
19
|
function givenUrlSets() {
|
|
19
20
|
return { request: new Set(), tools: new Set(), own: new WeakSet() };
|
|
20
21
|
}
|
|
@@ -156,7 +157,8 @@ function addHistoryUrls(given, messages) {
|
|
|
156
157
|
for (const message of messages) {
|
|
157
158
|
if (message.role === 'assistant' || given.own.has(message))
|
|
158
159
|
continue;
|
|
159
|
-
|
|
160
|
+
const read = message.role === 'tool' || isLateResult(message);
|
|
161
|
+
addSeenUrls(read ? given.tools : given.request, textForScan(message).text);
|
|
160
162
|
}
|
|
161
163
|
}
|
|
162
164
|
function addRequestUrls(given, request) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/** Values that fill a label template's `{param}` placeholders. */
|
|
2
2
|
export type LabelParams = Record<string, string | number>;
|
|
3
3
|
/** Keys are stable API. */
|
|
4
|
-
export declare const LABEL_KEYS: readonly ["provider.decision_failed", "provider.decision_unavailable", "provider.decision_model_missing", "provider.decision_ended", "provider.decision_images_off", "provider.decision_box_model", "provider.budget_exhausted", "provider.adapter_version", "provider.adapter_methods", "provider.adapter_schemas", "provider.fallback_slot", "provider.model_fallback_slot", "provider.host_model", "provider.turn_profile", "provider.model_binding_missing", "provider.session_closed", "provider.settled_call", "provider.session_unavailable", "provider.live_input", "provider.tool_cancel_missing", "provider.kernel_gate", "provider.event_invalid", "provider.response_identity", "provider.credential_resolution", "provider.credential_shape", "provider.stored_continuation", "provider.continuation_mismatch", "provider.continuation_contract", "provider.stateless_history", "provider.events_after_completion", "provider.duplicate_call", "provider.tool_cancel_live", "provider.complete_unavailable", "provider.parallel_tools", "provider.binding_identity", "provider.profile_type", "provider.event_type", "provider.feature", "provider.input_kind", "provider.output_kind", "provider.builtin", "provider.credential_missing", "provider.profile_configuration", "continue.instruction", "canary.bind_note", "user_data.note", "context.note", "taint.blocked", "taint.reason_steered", "taint.reason_tainted", "taint.destination_blocked", "taint.destination_confirm", "advisory.notice_elevated", "advisory.notice_high", "advisory.guidance", "attachments.too_many_files", "attachments.too_many_of_kind", "attachments.file_too_large", "attachments.turn_too_large", "attachments.not_accepted", "attachments.mime_not_allowed", "attachments.limits_unconfigured", "quota.exhausted", "quota.busy", "error.config", "error.request", "error.conflict", "error.not_found", "error.input", "error.action", "error.auth", "error.rate_limit", "error.busy", "error.unsupported", "error.unavailable", "error.bad_response", "error.network", "error.timeout", "error.safety", "error.blocked", "error.declined", "error.failed", "error.cancelled", "error.internal", "repair.default_guidance", "repair.items_guidance", "repair.prompt_header", "repair.prompt_intro", "repair.prompt_instructions", "repair.history_heading", "repair.section_previous_output", "repair.section_validator_rejection", "repair.section_repair_guidance", "repair.section_instructions", "structured.answer_tool", "structured.answer_missing", "structured.cut_off", "structured.not_json", "compaction.request", "compaction.tool_call", "compaction.tool_result", "compaction.tool_files", "decision.box_image", "detect.blocked", "detect.call_blocked", "detect.output_blocked", "detect.shown_blocked", "detect.hint.ids", "detect.hint.financial", "detect.hint.network", "detect.hint.credentials", "detect.hint.injection", "detect.hint.tool_instructions", "detect.hint.canary_leak", "detect.hint.prompt_leak", "detect.hint.marker_leak", "detect.hint.ungiven_images", "detect.hint.ungiven_links", "detect.hint.tool_leak", "detect.hint.own", "egress.default_repair_guidance", "egress.refusal", "egress.refusal_cut", "egress.rejection", "egress.rejection_found", "egress.hint_unscannable", "egress.hint_provider_tool_leak", "thought.omitted_image", "thought.omitted_link", "thought.omitted_instructions", "session.abandon_gated", "session.tool_denied", "session.tool_aborted", "session.sign_in", "sign_in.link", "sign_in.pending", "sign_in.done", "sign_in.declined", "sign_in.expired", "sign_in.out_of_scope", "session.gate_expired", "session.provider_state_unowned", "session.conversation_unknown", "session.full", "session.busy", "session.turn_ended", "session.gate_pending", "session.part_skipped", "live.session_ended", "voice.unsupported", "voice.permission", "voice.unavailable", "voice.failed", "voice.empty", "stop.cancelled", "stop.interrupted", "stop.length", "stop.filtered", "stop.provider_error", "stop.stream_incomplete", "stop.step_limit", "composer.model", "composer.effort", "tool.awaiting_user", "tool.completed_hidden", "tool.t2_loader_needs_snapshot", "tool.t2_loader_shape", "tool.t2_loader_output_invalid", "tool.execution_uncertain", "tool.input_invalid", "tool.input_invalid_after_mutate", "tool.output_invalid_after_mutate", "tool.handler_no_output", "tool.did_not_run", "tool.page_no_answer", "tool.page_timed_out", "tool.output_invalid", "tool.not_loaded_t2", "tool.wait_unknown", "tool.wait_cycle", "tool.after_field", "tool.background_field", "tool.background_started", "tool.not_visible", "tool.builtin_not_enabled", "tool.provider_native", "tool.not_registered", "tool.builtin_needs_snapshot", "tool.not_allowed", "tool.not_eligible", "tool.unsupported_type", "tool.agent_call_limit", "tool.agent_failed", "tool.activity_failed", "protocol.not_served", "protocol.profile_type", "protocol.options", "protocol.option_clash", "protocol.base_options", "protocol.id_clash", "protocol.ws_headers", "protocol.builtin_unwritten", "protocol.provider_missing", "protocol.connection", "protocol.part_schema", "protocol.part_invalid", "protocol.key_invalid", "protocol.no_key", "protocol.reply_shape", "protocol.endpoint_url", "protocol.endpoint_host", "protocol.http", "protocol.no_operation", "protocol.definition", "protocol.provider_id_clash", "protocol.history_needed"];
|
|
4
|
+
export declare const LABEL_KEYS: readonly ["provider.decision_failed", "provider.decision_unavailable", "provider.decision_model_missing", "provider.decision_ended", "provider.decision_images_off", "provider.decision_box_model", "provider.budget_exhausted", "provider.adapter_version", "provider.adapter_methods", "provider.adapter_schemas", "provider.fallback_slot", "provider.model_fallback_slot", "provider.host_model", "provider.turn_profile", "provider.model_binding_missing", "provider.session_closed", "provider.settled_call", "provider.session_unavailable", "provider.live_input", "provider.tool_cancel_missing", "provider.kernel_gate", "provider.event_invalid", "provider.response_identity", "provider.credential_resolution", "provider.credential_shape", "provider.stored_continuation", "provider.continuation_mismatch", "provider.continuation_contract", "provider.stateless_history", "provider.events_after_completion", "provider.duplicate_call", "provider.tool_cancel_live", "provider.complete_unavailable", "provider.parallel_tools", "provider.binding_identity", "provider.profile_type", "provider.event_type", "provider.feature", "provider.input_kind", "provider.output_kind", "provider.builtin", "provider.credential_missing", "provider.profile_configuration", "continue.instruction", "canary.bind_note", "user_data.note", "context.note", "taint.blocked", "taint.reason_steered", "taint.reason_tainted", "taint.destination_blocked", "taint.destination_confirm", "advisory.notice_elevated", "advisory.notice_high", "advisory.guidance", "attachments.too_many_files", "attachments.too_many_of_kind", "attachments.file_too_large", "attachments.turn_too_large", "attachments.not_accepted", "attachments.mime_not_allowed", "attachments.limits_unconfigured", "quota.exhausted", "quota.busy", "error.config", "error.request", "error.conflict", "error.not_found", "error.input", "error.action", "error.auth", "error.rate_limit", "error.busy", "error.unsupported", "error.unavailable", "error.bad_response", "error.network", "error.timeout", "error.safety", "error.blocked", "error.declined", "error.failed", "error.cancelled", "error.internal", "repair.default_guidance", "repair.items_guidance", "repair.prompt_header", "repair.prompt_intro", "repair.prompt_instructions", "repair.history_heading", "repair.section_previous_output", "repair.section_validator_rejection", "repair.section_repair_guidance", "repair.section_instructions", "structured.answer_tool", "structured.answer_missing", "structured.cut_off", "structured.not_json", "compaction.request", "compaction.tool_call", "compaction.tool_result", "compaction.tool_files", "decision.box_image", "detect.blocked", "detect.call_blocked", "detect.output_blocked", "detect.shown_blocked", "detect.hint.ids", "detect.hint.financial", "detect.hint.network", "detect.hint.credentials", "detect.hint.injection", "detect.hint.tool_instructions", "detect.hint.canary_leak", "detect.hint.prompt_leak", "detect.hint.marker_leak", "detect.hint.ungiven_images", "detect.hint.ungiven_links", "detect.hint.tool_leak", "detect.hint.own", "egress.default_repair_guidance", "egress.refusal", "egress.refusal_cut", "egress.rejection", "egress.rejection_found", "egress.hint_unscannable", "egress.hint_provider_tool_leak", "thought.omitted_image", "thought.omitted_link", "thought.omitted_instructions", "session.abandon_gated", "session.tool_denied", "session.tool_aborted", "session.sign_in", "sign_in.link", "sign_in.pending", "sign_in.done", "sign_in.declined", "sign_in.expired", "sign_in.out_of_scope", "session.gate_expired", "session.provider_state_unowned", "session.conversation_unknown", "session.full", "session.busy", "session.turn_ended", "session.gate_pending", "session.part_skipped", "live.session_ended", "voice.unsupported", "voice.permission", "voice.unavailable", "voice.failed", "voice.empty", "stop.cancelled", "stop.interrupted", "stop.length", "stop.filtered", "stop.provider_error", "stop.stream_incomplete", "stop.step_limit", "composer.model", "composer.effort", "tool.awaiting_user", "tool.completed_hidden", "tool.t2_loader_needs_snapshot", "tool.t2_loader_shape", "tool.t2_loader_output_invalid", "tool.execution_uncertain", "tool.input_invalid", "tool.input_invalid_after_mutate", "tool.output_invalid_after_mutate", "tool.handler_no_output", "tool.did_not_run", "tool.page_no_answer", "tool.page_timed_out", "tool.output_invalid", "tool.not_loaded_t2", "tool.wait_unknown", "tool.wait_cycle", "tool.after_field", "tool.background_field", "tool.background_started", "tool.late_result", "tool.not_visible", "tool.builtin_not_enabled", "tool.provider_native", "tool.not_registered", "tool.builtin_needs_snapshot", "tool.not_allowed", "tool.not_eligible", "tool.unsupported_type", "tool.agent_call_limit", "tool.agent_failed", "tool.activity_failed", "protocol.not_served", "protocol.profile_type", "protocol.options", "protocol.option_clash", "protocol.base_options", "protocol.id_clash", "protocol.ws_headers", "protocol.builtin_unwritten", "protocol.provider_missing", "protocol.connection", "protocol.part_schema", "protocol.part_invalid", "protocol.key_invalid", "protocol.no_key", "protocol.reply_shape", "protocol.endpoint_url", "protocol.endpoint_host", "protocol.http", "protocol.no_operation", "protocol.definition", "protocol.provider_id_clash", "protocol.history_needed"];
|
|
5
5
|
/** One of {@linkcode LABEL_KEYS}. */
|
|
6
6
|
export type LabelKey = (typeof LABEL_KEYS)[number];
|
|
7
7
|
/**
|
|
@@ -186,6 +186,7 @@ export const LABEL_KEYS = [
|
|
|
186
186
|
'tool.after_field',
|
|
187
187
|
'tool.background_field',
|
|
188
188
|
'tool.background_started',
|
|
189
|
+
'tool.late_result',
|
|
189
190
|
'tool.not_visible',
|
|
190
191
|
'tool.builtin_not_enabled',
|
|
191
192
|
'tool.provider_native',
|
|
@@ -521,6 +522,7 @@ const DEFAULTS = {
|
|
|
521
522
|
'tool.wait_cycle': "Tool '{tool}' waits for calls that cannot finish before it: {calls}",
|
|
522
523
|
'tool.background_field': 'Set to true to get a reply now and let this call finish after the turn. Leave out to wait for its result.',
|
|
523
524
|
'tool.background_started': '{tool} has started and is still running. You do not have its result, so do not state or guess one, and do not call {tool} again. Tell the person it is under way and that the result comes later.',
|
|
525
|
+
'tool.late_result': 'The {tool} call you started earlier has finished. This is what it returned:',
|
|
524
526
|
'tool.after_field': 'Tools you are calling in this same step that must finish before this call starts. Leave out to set no order.',
|
|
525
527
|
'tool.not_visible': "Tool '{tool}' is not visible this turn",
|
|
526
528
|
'tool.builtin_not_enabled': "Builtin '{tool}' is not enabled this turn",
|
|
@@ -743,6 +745,7 @@ export const LABEL_NOTES = {
|
|
|
743
745
|
'tool.wait_cycle': "Told to the model when a call's pre_tool waitFor names calls that cannot settle before it, because they wait for it or run after it. Takes {tool} and {calls}.",
|
|
744
746
|
'tool.background_field': "What the model reads on the background field of a tool that sets registerTool.background to ask. Read when the tool registers, so a profile's labels do not change it.",
|
|
745
747
|
'tool.background_started': 'Told to the model when a call goes on after its reply: the call runs in the background and its result comes later.',
|
|
748
|
+
'tool.late_result': 'Put in front of a tool call result that a host passes in input.lateResults. Takes {tool}.',
|
|
746
749
|
'tool.after_field': "What the model reads on the after field of a tool that sets registerTool.after. Read when the tool registers, so a profile's labels do not change it.",
|
|
747
750
|
'tool.not_visible': 'Told to the model when it calls a tool it cannot see this turn. Takes {tool}.',
|
|
748
751
|
'tool.builtin_not_enabled': 'Told to the model when it calls a provider builtin not turned on this turn. Takes {tool}.',
|
|
@@ -817,6 +820,7 @@ const LABEL_PLACEHOLDERS = {
|
|
|
817
820
|
'sign_in.declined': SERVICE,
|
|
818
821
|
'sign_in.expired': SERVICE,
|
|
819
822
|
'sign_in.out_of_scope': SERVICE,
|
|
823
|
+
'tool.late_result': ['tool'],
|
|
820
824
|
'tool.awaiting_user': ['kind', 'prompt'],
|
|
821
825
|
'tool.t2_loader_needs_snapshot': TOOL,
|
|
822
826
|
'tool.t2_loader_shape': TOOL,
|
|
@@ -48,7 +48,8 @@ export type { ScanText } from './serialize.js';
|
|
|
48
48
|
export { scanTextOf, textForScan } from './serialize.js';
|
|
49
49
|
export type { Directive, DirectiveSignal } from './tool-directives.js';
|
|
50
50
|
export { advisoryLevel, DIRECTIVE_SIGNALS, directives } from './tool-directives.js';
|
|
51
|
+
export { TOOL_CLOSE } from './tool-fence.js';
|
|
51
52
|
export type { GuardedToolText, InspectedToolArguments } from './tool-result.js';
|
|
52
|
-
export { checkTaintGate, composeToolText, guardToolFailureText, guardToolResult, inspectToolArguments, isRemoteOrigin, isSuspicious, isTainted, recordTaint,
|
|
53
|
+
export { checkTaintGate, composeToolText, guardToolFailureText, guardToolResult, inspectToolArguments, isRemoteOrigin, isSuspicious, isTainted, recordTaint, toolCallEvent, wrapToolData, } from './tool-result.js';
|
|
53
54
|
export type { AdvisoryLevel, BlockedReplyOnBlock, BlockedReplySpec, DecisionDisclosureEnforcer, DecisionDisclosureVerdict, DecisionGuardrailsSpec, DestinationGate, GuardrailAction, GuardrailContext, GuardrailEvent, GuardrailHit, GuardrailStage, HostGuardrailsSpec, NetworkGuardrailSpec, OutboundPayload, ProfileGuardrailsSpec, Provenance, QuotaGuardrailSpec, ResolvedBlockedReply, ResolvedGuardrailPolicy, Severity, TaintGate, TaintGuardrailSpec, ToolOrigin, TrustLevel, TurnTaint, Verdict, } from './types.js';
|
|
54
55
|
export { ADVISORY_LEVELS, BLOCKED_REPLY_ON_BLOCK, DESTINATION_GATES, GUARDRAIL_STAGES, SEVERITIES, TAINT_GATES, TOOL_ORIGINS, TRUST_LEVELS, } from './types.js';
|
|
@@ -30,5 +30,6 @@ export { sensitiveSpans } from './sensitive.js';
|
|
|
30
30
|
export { SENSITIVE_GROUPS } from './sensitive-groups.js';
|
|
31
31
|
export { scanTextOf, textForScan } from './serialize.js';
|
|
32
32
|
export { advisoryLevel, DIRECTIVE_SIGNALS, directives } from './tool-directives.js';
|
|
33
|
-
export {
|
|
33
|
+
export { TOOL_CLOSE } from './tool-fence.js';
|
|
34
|
+
export { checkTaintGate, composeToolText, guardToolFailureText, guardToolResult, inspectToolArguments, isRemoteOrigin, isSuspicious, isTainted, recordTaint, toolCallEvent, wrapToolData, } from './tool-result.js';
|
|
34
35
|
export { ADVISORY_LEVELS, BLOCKED_REPLY_ON_BLOCK, DESTINATION_GATES, GUARDRAIL_STAGES, SEVERITIES, TAINT_GATES, TOOL_ORIGINS, TRUST_LEVELS, } from './types.js';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { TurnHistoryMessage } from '../kernel/turn-events.js';
|
|
2
|
+
import type { GuardrailHit, Provenance } from './types.js';
|
|
3
|
+
/** The closing tag of the fence a tool result is wrapped in. */
|
|
4
|
+
declare const TOOL_CLOSE = "</tool_data>";
|
|
5
|
+
/** What a fence from `wrapToolData` says of the text inside it. */
|
|
6
|
+
export interface ToolDataRead {
|
|
7
|
+
provenance: Provenance;
|
|
8
|
+
suspicious: GuardrailHit[];
|
|
9
|
+
/** The result as `composeToolText` joined it. */
|
|
10
|
+
text: string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Reads each whole fence in a message. A sign-in note can stand before one, and a late-result
|
|
14
|
+
* message holds one for each result. `stripToolFences` has taken any forged tag out of a body, so
|
|
15
|
+
* what a tag says is the kernel's.
|
|
16
|
+
*/
|
|
17
|
+
declare function readToolData(content: string): ToolDataRead[];
|
|
18
|
+
/**
|
|
19
|
+
* Whether a role `user` message holds tool results that `input.lateResults` opened. It is no one's
|
|
20
|
+
* word to the model. A person who types a fence only makes their own reply stricter.
|
|
21
|
+
*/
|
|
22
|
+
declare function isLateResult(message: TurnHistoryMessage): boolean;
|
|
23
|
+
export { isLateResult, readToolData, TOOL_CLOSE };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { DETECT_RULES } from './rules.js';
|
|
2
|
+
/** The closing tag of the fence a tool result is wrapped in. */
|
|
3
|
+
const TOOL_CLOSE = '</tool_data>';
|
|
4
|
+
const FENCE_OPEN = /^<tool_data tool="([^"]*)" origin="([^"]*)"( advisory="[^"]*")?>\n/gm;
|
|
5
|
+
/**
|
|
6
|
+
* Reads each whole fence in a message. A sign-in note can stand before one, and a late-result
|
|
7
|
+
* message holds one for each result. `stripToolFences` has taken any forged tag out of a body, so
|
|
8
|
+
* what a tag says is the kernel's.
|
|
9
|
+
*/
|
|
10
|
+
function readToolData(content) {
|
|
11
|
+
const reads = [];
|
|
12
|
+
for (const open of content.matchAll(FENCE_OPEN)) {
|
|
13
|
+
const [opening, tool = '', origin = '', advised] = open;
|
|
14
|
+
const start = open.index + opening.length;
|
|
15
|
+
const end = content.indexOf(`\n${TOOL_CLOSE}`, start);
|
|
16
|
+
if (end < 0)
|
|
17
|
+
continue;
|
|
18
|
+
const body = content.slice(start, end);
|
|
19
|
+
reads.push({
|
|
20
|
+
provenance: { origin: origin, tool, depth: origin === 'delegated' ? 2 : 1 },
|
|
21
|
+
suspicious: advised
|
|
22
|
+
? [{ rule: DETECT_RULES.tool_instructions, severity: 'high' }]
|
|
23
|
+
: [],
|
|
24
|
+
text: advised ? body.slice(body.indexOf('\n') + 1) : body,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
return reads;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Whether a role `user` message holds tool results that `input.lateResults` opened. It is no one's
|
|
31
|
+
* word to the model. A person who types a fence only makes their own reply stricter.
|
|
32
|
+
*/
|
|
33
|
+
function isLateResult(message) {
|
|
34
|
+
return (message.role === 'user' &&
|
|
35
|
+
message.content !== undefined &&
|
|
36
|
+
readToolData(message.content).length > 0);
|
|
37
|
+
}
|
|
38
|
+
export { isLateResult, readToolData, TOOL_CLOSE };
|
|
@@ -2,8 +2,6 @@ import type { ToolBoundary } from './boundaries.js';
|
|
|
2
2
|
import { type DetectScope } from './detect-at.js';
|
|
3
3
|
import { type LabelOverrides } from './labels.js';
|
|
4
4
|
import type { AdvisoryLevel, GuardrailEvent, GuardrailHit, Provenance, ResolvedGuardrailPolicy, ToolOrigin, TurnTaint, Verdict } from './types.js';
|
|
5
|
-
/** The closing tag of the fence a tool result is wrapped in. */
|
|
6
|
-
declare const TOOL_CLOSE = "</tool_data>";
|
|
7
5
|
/** True when the tool's bytes come from outside the host. */
|
|
8
6
|
declare function isRemoteOrigin(origin: ToolOrigin): boolean;
|
|
9
7
|
/**
|
|
@@ -89,4 +87,4 @@ declare function isSuspicious(taint: TurnTaint | undefined): boolean;
|
|
|
89
87
|
* or not enforcement is configured, so the risk is visible before a host opts in.
|
|
90
88
|
*/
|
|
91
89
|
declare function checkTaintGate(taint: TurnTaint | undefined, access: string, policy: ResolvedGuardrailPolicy, labels?: LabelOverrides): Verdict;
|
|
92
|
-
export { checkTaintGate, composeToolText, guardShownValue, guardToolFailureText, guardToolResult, inspectToolArguments, isRemoteOrigin, isSuspicious, isTainted, recordTaint,
|
|
90
|
+
export { checkTaintGate, composeToolText, guardShownValue, guardToolFailureText, guardToolResult, inspectToolArguments, isRemoteOrigin, isSuspicious, isTainted, recordTaint, toolCallEvent, wrapToolData, };
|
|
@@ -5,8 +5,7 @@ import { labelText } from './labels.js';
|
|
|
5
5
|
import { DETECT_RULES, EGRESS_RULES, TOOL_RULES } from './rules.js';
|
|
6
6
|
import { textForScan } from './serialize.js';
|
|
7
7
|
import { advisoryLevel } from './tool-directives.js';
|
|
8
|
-
|
|
9
|
-
const TOOL_CLOSE = '</tool_data>';
|
|
8
|
+
import { TOOL_CLOSE } from './tool-fence.js';
|
|
10
9
|
const TOOL_OPEN = '<tool_data';
|
|
11
10
|
/** Origins whose bytes the host does not author and cannot vouch for. */
|
|
12
11
|
const REMOTE_ORIGINS = new Set(['http', 'mcp', 'delegated']);
|
|
@@ -269,4 +268,4 @@ function checkTaintGate(taint, access, policy, labels) {
|
|
|
269
268
|
rejection: labelText('taint.blocked', { access, sources: read, reason }, labels),
|
|
270
269
|
};
|
|
271
270
|
}
|
|
272
|
-
export { checkTaintGate, composeToolText, guardShownValue, guardToolFailureText, guardToolResult, inspectToolArguments, isRemoteOrigin, isSuspicious, isTainted, recordTaint,
|
|
271
|
+
export { checkTaintGate, composeToolText, guardShownValue, guardToolFailureText, guardToolResult, inspectToolArguments, isRemoteOrigin, isSuspicious, isTainted, recordTaint, toolCallEvent, wrapToolData, };
|
|
@@ -33,6 +33,11 @@ export const REQUEST_FIELDS = {
|
|
|
33
33
|
doc: "What the page (client) or the host (server) wants the model to know, as any JSON or text; a sender the profile's inputs.context does not list, or a package over its maxChars, is refused.",
|
|
34
34
|
unset: 'No context',
|
|
35
35
|
},
|
|
36
|
+
'runTurn.input.lateResults': {
|
|
37
|
+
type: 'ToolOperationOutcome[]',
|
|
38
|
+
doc: 'Background tool calls that settled after their turn, as the host stored them. The model reads each as a tool result, and this reply counts it as a remote read when the call was remote. Pass them on each run of the reply.',
|
|
39
|
+
unset: 'No late results',
|
|
40
|
+
},
|
|
36
41
|
'runTurn.input.attachments': {
|
|
37
42
|
type: 'Array<TurnBlob | TurnMediaRef>',
|
|
38
43
|
doc: "Files sent with the turn, as bytes or as a provider file reference; each must pass the profile's inputs.attachments.",
|
|
@@ -109,6 +109,8 @@ declare function openTurnState(args: {
|
|
|
109
109
|
tools: ToolRegistry;
|
|
110
110
|
profile: Profile;
|
|
111
111
|
generation: ResolvedGeneration;
|
|
112
|
+
/** The request, on the run that opens the turn. */
|
|
113
|
+
request?: TurnRequest;
|
|
112
114
|
trace: TurnTraceState;
|
|
113
115
|
allEmittedEvents?: TurnEvent[];
|
|
114
116
|
agents?: AgentCaller;
|
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
import { turnDestinations } from '../../../guardrails/destinations.js';
|
|
2
|
-
import { givenUrlSets } from '../../../guardrails/egress-urls.js';
|
|
1
|
+
import { addFencedDestinations, addGivenDestinations, addResultDestinations, turnDestinations, } from '../../../guardrails/destinations.js';
|
|
2
|
+
import { addSeenUrls, givenUrlSets } from '../../../guardrails/egress-urls.js';
|
|
3
|
+
import { labelText } from '../../../guardrails/labels.js';
|
|
4
|
+
import { isLateResult, readToolData } from '../../../guardrails/tool-fence.js';
|
|
5
|
+
import { isRemoteOrigin, recordTaint, wrapToolData } from '../../../guardrails/tool-result.js';
|
|
6
|
+
import { originOfTool } from '../../tools/execute.js';
|
|
3
7
|
import { ownToolsOf } from '../../tools/project.js';
|
|
4
8
|
function recordStepEvent(event, state) {
|
|
5
9
|
state.allEmittedEvents.push(event);
|
|
@@ -14,6 +18,65 @@ function appendUserInput(state, parts) {
|
|
|
14
18
|
? { role: 'user', content: parts.map((p) => (p.type === 'text' ? p.text : '')).join('') }
|
|
15
19
|
: { role: 'user', parts: [...parts] });
|
|
16
20
|
}
|
|
21
|
+
/** Whether this run goes on with a reply that an earlier run began, not with a new message. */
|
|
22
|
+
function continuesReply({ continueFrom, input }) {
|
|
23
|
+
if (continueFrom)
|
|
24
|
+
return true;
|
|
25
|
+
if (input?.text || input?.attachments?.length || input?.voice?.length)
|
|
26
|
+
return false;
|
|
27
|
+
return input?.history?.at(-1)?.role === 'tool';
|
|
28
|
+
}
|
|
29
|
+
/** The reply's part of the history. Late results that opened it stand before its first message. */
|
|
30
|
+
function replyTail(history) {
|
|
31
|
+
let start = history.findLastIndex((message) => message.role === 'user');
|
|
32
|
+
while (start > 0 && isLateResult(history[start - 1]))
|
|
33
|
+
start -= 1;
|
|
34
|
+
return history.slice(Math.max(start, 0));
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* invariant: Taint lasts for the reply that first read the text, and a pause does not end a reply.
|
|
38
|
+
* The run after a pause reads the marks back from the tool results of that reply, in the history the
|
|
39
|
+
* request sent, because compaction can rewrite those results.
|
|
40
|
+
*/
|
|
41
|
+
function carryReplyReads(state, history) {
|
|
42
|
+
for (const message of replyTail(history)) {
|
|
43
|
+
if (message.content === undefined)
|
|
44
|
+
continue;
|
|
45
|
+
if (message.role !== 'tool' && !isLateResult(message))
|
|
46
|
+
continue;
|
|
47
|
+
const reads = readToolData(message.content);
|
|
48
|
+
for (const read of reads) {
|
|
49
|
+
state.taint = recordTaint(state.taint, read.provenance, read.suspicious);
|
|
50
|
+
addFencedDestinations(state.destinations, read.text, read.provenance);
|
|
51
|
+
}
|
|
52
|
+
if (reads.length > 0)
|
|
53
|
+
continue;
|
|
54
|
+
const tool = message.name ? state.tools.get(message.name) : undefined;
|
|
55
|
+
if (tool && !isRemoteOrigin(originOfTool(tool, state.tools))) {
|
|
56
|
+
addGivenDestinations(state.destinations, message.content);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/** A late result is read as a tool's result, so its message is no one's word to the model. */
|
|
61
|
+
function openLateResults(state, outcomes, labels) {
|
|
62
|
+
if (outcomes.length === 0)
|
|
63
|
+
return;
|
|
64
|
+
const blocks = outcomes.map(({ event, replay }) => {
|
|
65
|
+
const { name, readBack = '' } = event.tool;
|
|
66
|
+
const result = replay.modelResult;
|
|
67
|
+
const provenance = result?.provenance ?? { origin: 'local', tool: name, depth: 1 };
|
|
68
|
+
if (result?.provenance) {
|
|
69
|
+
state.taint = recordTaint(state.taint, result.provenance, result.suspicious);
|
|
70
|
+
addResultDestinations(state.destinations, result, result.provenance);
|
|
71
|
+
}
|
|
72
|
+
addSeenUrls(state.givenUrls.tools, readBack);
|
|
73
|
+
const fenced = readToolData(readBack).length > 0 ? readBack : wrapToolData(readBack, provenance);
|
|
74
|
+
return `${labelText('tool.late_result', { tool: name }, labels)}\n${fenced}`;
|
|
75
|
+
});
|
|
76
|
+
const message = { role: 'user', content: blocks.join('\n\n') };
|
|
77
|
+
state.givenUrls.own.add(message);
|
|
78
|
+
state.currentHistory.push(message);
|
|
79
|
+
}
|
|
17
80
|
/**
|
|
18
81
|
* Step state for one turn. A text turn's history grows inside the turn (tool
|
|
19
82
|
* steps, stage injects, repair retries), so its opening input moves into turn
|
|
@@ -46,6 +109,10 @@ function openTurnState(args) {
|
|
|
46
109
|
...(args.background ? { background: args.background } : {}),
|
|
47
110
|
};
|
|
48
111
|
if (profile.type === 'text') {
|
|
112
|
+
if (args.request && continuesReply(args.request)) {
|
|
113
|
+
carryReplyReads(state, args.request.input?.history ?? []);
|
|
114
|
+
}
|
|
115
|
+
openLateResults(state, args.request?.input?.lateResults ?? [], profile.labels);
|
|
49
116
|
appendUserInput(state, generation.input);
|
|
50
117
|
generation.input = [];
|
|
51
118
|
}
|
|
@@ -163,10 +163,17 @@ export async function checkKey(provider, options = {}) {
|
|
|
163
163
|
...(said ? { said } : {}),
|
|
164
164
|
};
|
|
165
165
|
}
|
|
166
|
+
// why: A `/\/+$/` scan is quadratic on a long run of slashes.
|
|
167
|
+
function withoutTrailingSlashes(url) {
|
|
168
|
+
let end = url.length;
|
|
169
|
+
while (url[end - 1] === '/')
|
|
170
|
+
end--;
|
|
171
|
+
return url.slice(0, end);
|
|
172
|
+
}
|
|
166
173
|
function urlFor(serve, path, apiId, stream) {
|
|
167
174
|
if (serve.url)
|
|
168
175
|
return new URL(serve.url({ path, apiId, stream }));
|
|
169
|
-
return new URL(serve.baseUrl
|
|
176
|
+
return new URL(withoutTrailingSlashes(serve.baseUrl) + (path.startsWith('/') ? path : `/${path}`));
|
|
170
177
|
}
|
|
171
178
|
async function* defaultFraming(response, stream) {
|
|
172
179
|
const type = response.headers.get('content-type') ?? '';
|
|
@@ -235,7 +235,9 @@ class CoordinatedExecution {
|
|
|
235
235
|
finally {
|
|
236
236
|
clearTimeout(this.graceTimer);
|
|
237
237
|
await this.close().catch(() => { });
|
|
238
|
-
await Promise.resolve(
|
|
238
|
+
await Promise.resolve()
|
|
239
|
+
.then(() => detach.trace.end())
|
|
240
|
+
.catch(() => { });
|
|
239
241
|
}
|
|
240
242
|
}
|
|
241
243
|
async drain(iterator, first, emit) {
|
|
@@ -312,7 +314,7 @@ class CoordinatedExecution {
|
|
|
312
314
|
message: `The result of ${name} is ${bytes} bytes, more than the ${limit} bytes kept for one tool result`, // labels-exempt: developer contract / internal diagnostic — not end-user or model copy (P2)
|
|
313
315
|
details: { bytes, maxOutcomeBytes: limit },
|
|
314
316
|
};
|
|
315
|
-
return failedOutcome(outcome.event.tool, outcome.effects, failure);
|
|
317
|
+
return failedOutcome(outcome.event.tool, outcome.effects, failure, outcome.event.tool.tokens);
|
|
316
318
|
}
|
|
317
319
|
replay(error) {
|
|
318
320
|
const saved = toolOperationOutcomeSchema.parse(operationWireValue(error.outcome));
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type ToolBoundary } from '../../guardrails/boundaries.js';
|
|
2
2
|
import { type DetectScope } from '../../guardrails/detect-at.js';
|
|
3
3
|
import { type LabelOverrides } from '../../guardrails/labels.js';
|
|
4
|
+
import type { ToolOrigin } from '../../guardrails/types.js';
|
|
4
5
|
import type { SpanHandle, TraceAttributes } from '../../observability/trace-span.js';
|
|
5
6
|
import { type ToolCallEnd } from '../engine/tool-trace.js';
|
|
6
7
|
import type { InjectUnit } from '../stages.js';
|
|
@@ -11,8 +12,9 @@ import { startToolExecution, type ToolCallBase } from './events.js';
|
|
|
11
12
|
import type { ToolExecutionCoordinator, ToolOperationClaim, ToolOperationOutcome } from './operation.js';
|
|
12
13
|
import type { ToolRegistry } from './registry.js';
|
|
13
14
|
import { type ToolStageSupport } from './stage-run.js';
|
|
14
|
-
import type { FunctionToolDef, ModelToolResult, RegisteredAgentTool, ToolContext, ToolFailure, ToolGate, TurnToolSnapshot } from './types.js';
|
|
15
|
+
import type { FunctionToolDef, ModelToolResult, RegisteredAgentTool, RegisteredTool, ToolContext, ToolFailure, ToolGate, TurnToolSnapshot } from './types.js';
|
|
15
16
|
export { checkPermission, isGateResumeGranted, isResumeContinuation, permissionGranted, } from './permission.js';
|
|
17
|
+
export declare function originOfTool(tool: RegisteredTool, tools: ToolRegistry): ToolOrigin;
|
|
16
18
|
/**
|
|
17
19
|
* The boundary a call of `name` is read at before its tool gets it: none for a provider builtin,
|
|
18
20
|
* whose text the kernel never reads. A call no tool takes is read as a function tool's.
|
|
@@ -27,7 +27,7 @@ import { CredentialRefusedError, signedInFetch } from './signed-in-fetch.js';
|
|
|
27
27
|
import { emitGateSettlement, runPostToolStages, runPreToolPipeline, } from './stage-run.js';
|
|
28
28
|
export { checkPermission, isGateResumeGranted, isResumeContinuation, permissionGranted, } from './permission.js';
|
|
29
29
|
import { UncheckedOutput } from './unchecked-output.js';
|
|
30
|
-
function originOfTool(tool, tools) {
|
|
30
|
+
export function originOfTool(tool, tools) {
|
|
31
31
|
if (tool.type === 'agent')
|
|
32
32
|
return agentToolOrigin(tools.findProfile(tool.profile));
|
|
33
33
|
if (tool.type === 'http')
|
|
@@ -1103,8 +1103,8 @@ async function* executeAgentTool(args) {
|
|
|
1103
1103
|
return;
|
|
1104
1104
|
}
|
|
1105
1105
|
const event = next.value;
|
|
1106
|
-
seen.push(event);
|
|
1107
1106
|
if (event.type === 'tokens' || event.type === 'tool') {
|
|
1107
|
+
seen.push(event);
|
|
1108
1108
|
const spent = sumTurnTokens(seen);
|
|
1109
1109
|
// why: A stopped agent throws before it returns its usage.
|
|
1110
1110
|
if (spent)
|
|
@@ -4,7 +4,7 @@ import type { CacheMode, CacheTtl, CompactionMeter, CompactionOutcome, Compactio
|
|
|
4
4
|
import type { StageHandler } from './stages.js';
|
|
5
5
|
import type { ToolBackgroundService } from './tools/background.js';
|
|
6
6
|
import type { GateDecision } from './tools/gate-answer.js';
|
|
7
|
-
import type { PageToolExecution, ToolExecutionCoordinator } from './tools/operation.js';
|
|
7
|
+
import type { PageToolExecution, ToolExecutionCoordinator, ToolOperationOutcome } from './tools/operation.js';
|
|
8
8
|
import type { AgentCall, AgentCallHook, AgentCallRequest, AgentToolDef, AgentToolInput, AgentToolOutput, HostProfileToolsSpec, InvokeToolRequest, InvokeToolResume, InvokeToolStepCall, InvokeToolStepRequest, LiveProfileToolsSpec, ModelToolResult, PageAnswer, ProfileToolsSpec, RegisteredTool, ToolCallEvent, ToolFailure, ToolGate, ToolLoadContext, ToolPolicy, TurnToolSnapshot, WireFunctionTool } from './tools/types.js';
|
|
9
9
|
import type { AttemptReason, CallDone, CompactionFailure, CompactionResult, CompactionSignal, DoneFields, GroundingEvent, InjectedMessages, InteractionMediaPart, InteractionMediaRefPart, InteractionPart, InteractionTextPart, InvalidField, ModelEvent, ProviderEvidence, RemovedItem, SessionEnded, SessionEvent, SessionEventKind, SessionEventOf, Source, TurnCost, TurnEvent, TurnEventOf, TurnEventType, TurnGroundingCount, TurnHistoryMessage, TurnResponse, TurnTokenSide, TurnTokens } from './turn-events.js';
|
|
10
10
|
export type { AgentCall, AgentCallHook, AgentCallRequest, AgentToolDef, AgentToolInput, AgentToolOutput, AttemptReason, CacheMode, CacheTtl, CallDone, CompactionFailure, CompactionMeter, CompactionOutcome, CompactionResult, CompactionSignal, CompactionTiming, ContinueStopKind, DoneFields, FieldMeta, GroundingEvent, HostProfileToolsSpec, InjectedMessages, InteractionMediaPart, InteractionMediaRefPart, InteractionPart, InteractionTextPart, InvalidField, InvokeToolRequest, InvokeToolResume, InvokeToolStepCall, InvokeToolStepRequest, KeySlot, KeyVault, LiveActivityHandling, LiveEndSensitivity, LiveProfileToolsSpec, LiveStartSensitivity, MediaInputKind, ModelEvent, ProfileToolsSpec, ProfileType, ProviderEvidence, RegisteredTool, RemovedItem, SessionEnded, SessionEvent, SessionEventKind, SessionEventOf, Source, SpeechAudioFormat, StreamMode, SummaryMode, ThinkingLevel, ToolCallEvent, ToolFailure, ToolGate, ToolLoadContext, ToolLoadTier, ToolPolicy, TurnCost, TurnEvent, TurnEventOf, TurnEventType, TurnGroundingCount, TurnHistoryMessage, TurnResponse, TurnStage, TurnStopKind, TurnTokenSide, TurnTokens, TurnToolSnapshot, WireFunctionTool, };
|
|
@@ -737,6 +737,8 @@ export interface TurnInput {
|
|
|
737
737
|
attachments?: Array<TurnBlob | TurnMediaRef>;
|
|
738
738
|
voice?: TurnBlob[];
|
|
739
739
|
history?: TurnHistoryMessage[];
|
|
740
|
+
/** Tool calls that settled after their turn, as the host stored them. Each counts as read in this reply. */
|
|
741
|
+
lateResults?: ToolOperationOutcome[];
|
|
740
742
|
repair?: TurnRepairRequest;
|
|
741
743
|
/**
|
|
742
744
|
* Optional host-supplied history token count for `meter: 'history'`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@theoremjs/agents",
|
|
3
|
-
"version": "0.3.4-next.
|
|
3
|
+
"version": "0.3.4-next.735.g8d504c6",
|
|
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",
|