Repository navigation
Expand file tree
/
Copy patheffects.py
More file actions
177 lines (157 loc) · 6.47 KB
/
Copy patheffects.py
File metadata and controls
177 lines (157 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"""The effect dispatcher — the one and only place a registered tool callable is invoked.
This is the no-bypass *wiring* for registered tools, not a proof that no unauthorized effect
can occur:
- it is the sole holder of the registry's callables;
- it cannot be constructed without a ``Gate`` (the gate is a required constructor argument);
- ``dispatch`` calls ``gate.authorize`` and raises ``EffectBlocked`` *before* the callable is
reached.
So "registered effects go through the Verifier" reduces to "the only registered effect site
checks first." Side channels, host-owned callables outside the registry, and tools that exceed
their ``ToolSpec`` are out of scope. Witnessed in the dispatch trace when the Gate runs.
"""
from __future__ import annotations
import uuid
from reasoning_kernel.kernel.gate import Gate
from reasoning_kernel.kernel.taint import result_label
from reasoning_kernel.memory.trace import TraceWriter
from reasoning_kernel.schemas.capability import CapabilitySet
from reasoning_kernel.schemas.ids import StepId
from reasoning_kernel.schemas.policy import RunContext, VerifierVerdict
from reasoning_kernel.schemas.registry import ToolSpec
from reasoning_kernel.schemas.trace import (
EffectBlockedEvent,
EffectCommitted,
EffectFailed,
EffectStarted,
GateDecision,
digest,
)
from reasoning_kernel.schemas.values import TaintedValue
from reasoning_kernel.tools.registry import ToolRegistry
class EffectBlocked(Exception):
"""Raised when the Verifier denies a call. Carries the verdict; the callable did not run."""
def __init__(self, verdict: VerifierVerdict) -> None:
super().__init__(verdict.reason)
self.verdict = verdict
class ToolExecutionError(Exception):
"""A tool failed or returned invalid output; previous/external effects are not rolled back."""
def __init__(self, code: str) -> None:
super().__init__(code)
self.code = code
class EffectDispatcher:
def __init__(
self,
registry: ToolRegistry,
gate: Gate,
trace: TraceWriter,
ctx: RunContext,
) -> None:
self._registry = registry.snapshot()
self._gate = gate
self._trace = trace
self._ctx = ctx
def catalog(self) -> list[ToolSpec]:
return [
spec
for spec in self._registry.catalog()
if self._gate.grant.allows_all(spec.required_caps)
]
def matches(self, ctx: RunContext, trace: TraceWriter) -> bool:
return ctx == self._ctx and trace is self._trace and trace.owns(ctx.run_id)
def grant(self) -> CapabilitySet:
"""The capability grant the Gate enforces (the run's authority ceiling)."""
return self._gate.grant
def for_subkernel(self, grant: CapabilitySet, ctx: RunContext) -> EffectDispatcher:
"""A dispatcher over the SAME registry and shared trace, at a reduced grant (a sub-kernel).
Same registry means the inner kernel cannot reach a callable the outer one couldn't; the
clamped Gate means it can authorize strictly less. Every effect still routes through a Gate.
"""
return EffectDispatcher(self._registry, self._gate.for_grant(grant), self._trace, ctx)
def dispatch(
self, tool_name: str, named_args: dict[str, TaintedValue], *, step_id: StepId | None = None
) -> TaintedValue:
rtool = self._registry.get(tool_name)
spec = rtool.spec
invocation_id = uuid.uuid4().hex
checked = self._gate.authorize(spec, named_args, self._ctx)
model_in, named_args, verdict = checked.model, checked.args, checked.verdict
arg_labels = [v.label for v in named_args.values()]
self._trace.emit(
GateDecision(
run_id=self._ctx.run_id,
tool=spec.name,
verdict=verdict,
arg_labels=arg_labels,
step_id=step_id,
invocation_id=invocation_id,
)
)
if not verdict.allowed:
self._trace.emit(
EffectBlockedEvent(
run_id=self._ctx.run_id,
tool=spec.name,
verdict=verdict,
step_id=step_id,
invocation_id=invocation_id,
)
)
raise EffectBlocked(verdict)
# Past the gate: invoke with the same normalized model that was authorized.
assert model_in is not None
self._trace.emit(
EffectStarted(
run_id=self._ctx.run_id,
tool=spec.name,
step_id=step_id,
invocation_id=invocation_id,
)
)
try:
out = rtool.callable(model_in)
except Exception:
self._trace.emit(
EffectFailed(
run_id=self._ctx.run_id,
tool=spec.name,
step_id=step_id,
invocation_id=invocation_id,
code="tool_exception",
)
)
raise ToolExecutionError("tool_exception") from None
try:
# Revalidate even existing model instances; a callable may have mutated one.
if not isinstance(out, spec.output_schema):
raise TypeError("tool returned the wrong output model")
validated = spec.output_schema.model_validate(out.model_dump(by_alias=True))
except Exception:
self._trace.emit(
EffectCommitted(
run_id=self._ctx.run_id,
tool=spec.name,
step_id=step_id,
invocation_id=invocation_id,
output_digest="",
output_valid=False,
)
)
raise ToolExecutionError("invalid_output") from None
try:
output_digest = "" if self._trace.operational else digest(validated)
except (ValueError, TypeError):
output_digest = "" # custom payloads never prevent recording a completed call
self._trace.emit(
EffectCommitted(
run_id=self._ctx.run_id,
tool=spec.name,
output_digest=output_digest,
step_id=step_id,
invocation_id=invocation_id,
)
)
return TaintedValue(
value=validated,
label=result_label(spec, arg_labels),
produced_by=step_id or StepId(f"__effect__{spec.name}"),
)