pub struct UsageLearner { /* private fields */ }Expand description
A TraceSink decorator that grows an IntentGraph from the events
passing through it, then forwards them unchanged.
Install it in place of the sink you would otherwise use, and hand the same graph handle to the registries so searches read what invocations write:
use std::sync::{Arc, RwLock};
use ratel_ai_core::{IntentGraph, NoopSink, Tool, ToolRegistry, UsageLearner};
let graph = Arc::new(RwLock::new(IntentGraph::empty()));
let learner = Arc::new(UsageLearner::new(graph.clone(), Arc::new(NoopSink)));
let mut registry = ToolRegistry::new();
registry.set_trace_sink(learner); // writes the graph
registry.set_intent_graph(Some(graph.clone())); // reads it
registry.register(Tool {
id: "gh_run_list".into(),
name: "gh_run_list".into(),
description: "List CI runs".into(),
experimental_searchable_description: None,
input_schema: serde_json::json!({}),
output_schema: serde_json::json!({}),
});
registry.search("why is the build broken", 5);
registry.record_event(ratel_ai_core::TraceEvent::InvokeStart {
tool_id: "gh_run_list".into(),
args_size_bytes: 0,
});
assert_eq!(graph.read().unwrap().len(), 1); // learnedImplementations§
Source§impl UsageLearner
impl UsageLearner
Sourcepub fn new(graph: Arc<RwLock<IntentGraph>>, inner: Arc<dyn TraceSink>) -> Self
pub fn new(graph: Arc<RwLock<IntentGraph>>, inner: Arc<dyn TraceSink>) -> Self
Wrap inner, learning into graph under ObservationPolicy::default.
Pass crate::NoopSink for inner when the only thing you want is the
learning.
Sourcepub fn with_policy(
graph: Arc<RwLock<IntentGraph>>,
inner: Arc<dyn TraceSink>,
policy: ObservationPolicy,
) -> Self
pub fn with_policy( graph: Arc<RwLock<IntentGraph>>, inner: Arc<dyn TraceSink>, policy: ObservationPolicy, ) -> Self
Wrap inner, learning into graph under policy — the entry point a
baseline capture or a replay uses. Self::new is this at the default
policy.
Sourcepub fn policy(&self) -> ObservationPolicy
pub fn policy(&self) -> ObservationPolicy
The policy in force.
Sourcepub fn graph(&self) -> Arc<RwLock<IntentGraph>> ⓘ
pub fn graph(&self) -> Arc<RwLock<IntentGraph>> ⓘ
The graph this learner writes — hand it to a registry to read.
Sourcepub fn replay(&self, envelope: &TraceEnvelope)
pub fn replay(&self, envelope: &TraceEnvelope)
Learn from a historical envelope instead of a live event.
Identical to TraceSink::record except that the observation is stamped
with the envelope’s own ts rather than the wall clock, so decay
reflects when the work actually happened. Replaying a trace log through
this therefore reproduces the graph the live path would have grown —
which is what makes a JSONL replay a faithful reconstruction rather than
an approximation.
Does not forward to the inner sink: replaying an old log must not re-emit its events into a live stream.
One learner covers one session. Feed envelopes from different
session_ids through separate learners, or their searches and invokes
cross-pair into edges nobody produced.
Trait Implementations§
Source§impl TraceSink for UsageLearner
impl TraceSink for UsageLearner
Source§fn record(&self, event: TraceEvent)
fn record(&self, event: TraceEvent)
Source§fn record_with_context(&self, event: TraceEvent, context: TraceEventContext)
fn record_with_context(&self, event: TraceEvent, context: TraceEventContext)
Source§fn record_envelope(&self, envelope: TraceEnvelope)
fn record_envelope(&self, envelope: TraceEnvelope)
Source§fn sample_rate(&self) -> f64
fn sample_rate(&self) -> f64
Auto Trait Implementations§
impl !Freeze for UsageLearner
impl !RefUnwindSafe for UsageLearner
impl !UnwindSafe for UsageLearner
impl Send for UsageLearner
impl Sync for UsageLearner
impl Unpin for UsageLearner
impl UnsafeUnpin for UsageLearner
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more