pub struct MemoryWriter { /* private fields */ }Expand description
Extracts durable facts from the latest user turn once a run completes. Failed, cancelled and interrupted runs write nothing. The extraction runs on a spawned task and can neither block nor change the run, and because it fires at run end, hook order does not matter: an answer another hook rejected is never extracted.
Outcomes (including model usage) are logged with tracing and kept in
a small buffer; MemoryWriter::drain waits for pending work and
returns them, which is useful at shutdown and in tests.
Requires a Memory built with a crate::FactExtractor; without one
the hook does nothing.
Implementations§
Source§impl MemoryWriter
impl MemoryWriter
pub fn new(memory: Memory) -> Self
Sourcepub fn honoring(self, learning: TurnLearning) -> Self
pub fn honoring(self, learning: TurnLearning) -> Self
Extract only while learning allows it: a turn opted out of
learning (see aidaemon_policies::Learning) is never extracted.
Sourcepub async fn drain(&self) -> Vec<WriterReport>
pub async fn drain(&self) -> Vec<WriterReport>
Wait for every spawned extraction, then return (and clear) the buffered reports.
Trait Implementations§
Source§impl Hook for MemoryWriter
impl Hook for MemoryWriter
Source§fn on_run_end<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
ctx: &'life1 HookContext<'life2>,
outcome: &'life3 RunOutcome,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
fn on_run_end<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
ctx: &'life1 HookContext<'life2>,
outcome: &'life3 RunOutcome,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
run or resume that
returns (a pause returns too), after the
outcome is final and every on_stop check has had its say, so it is
the place for after-the-fact work such as writing memory. It cannot
change the outcome. A run_resolving call is one run: the
interruptions it settles are not run ends.Source§fn safeguards(&self) -> &'static [Safeguard]
fn safeguards(&self) -> &'static [Safeguard]
crate::AgentBuilder::build
requires every Safeguard::REQUIRED one to be covered by a hook
or an attestation.Source§fn name(&self) -> &'static str
fn name(&self) -> &'static str
Source§fn redactor(&self) -> Option<&dyn Redact>
fn redactor(&self) -> Option<&dyn Redact>
crate::redaction, to model requests, events, receipts, the
question put to the owner and the saved state. Default: nothing.Source§fn timeout(&self) -> Option<Duration>
fn timeout(&self) -> Option<Duration>
crate::Limits::hook_timeout. The wall-clock budget still applies.Source§fn on_run_start<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_ctx: &'life1 HookContext<'life2>,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait,
fn on_run_start<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_ctx: &'life1 HookContext<'life2>,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait,
Source§fn edit_transcript<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_ctx: &'life1 HookContext<'life2>,
) -> Pin<Box<dyn Future<Output = Option<Vec<Message>>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait,
fn edit_transcript<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_ctx: &'life1 HookContext<'life2>,
) -> Pin<Box<dyn Future<Output = Option<Vec<Message>>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait,
before_model edit, which changes one
request and loses nothing; use this when the shortened history should
be what later turns and resumes start from. Read moreSource§fn before_model<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
_ctx: &'life1 HookContext<'life2>,
_request: &'life3 mut ModelRequest,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
Self: 'async_trait,
fn before_model<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
_ctx: &'life1 HookContext<'life2>,
_request: &'life3 mut ModelRequest,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
Self: 'async_trait,
Source§fn answer_model<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
_ctx: &'life1 HookContext<'life2>,
_request: &'life3 ModelRequest,
) -> Pin<Box<dyn Future<Output = Option<ModelResponse>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
Self: 'async_trait,
fn answer_model<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
_ctx: &'life1 HookContext<'life2>,
_request: &'life3 ModelRequest,
) -> Pin<Box<dyn Future<Output = Option<ModelResponse>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
Self: 'async_trait,
before_model hook, so it sees the final request. The first
answer wins; the response is treated as the model’s, after_model
included, and the turn counts.Source§fn after_model<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
_ctx: &'life1 HookContext<'life2>,
_response: &'life3 mut ModelResponse,
) -> Pin<Box<dyn Future<Output = ModelDecision> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
Self: 'async_trait,
fn after_model<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
_ctx: &'life1 HookContext<'life2>,
_response: &'life3 mut ModelResponse,
) -> Pin<Box<dyn Future<Output = ModelDecision> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
Self: 'async_trait,
Reject or Stop wins. Calls a hook adds or
changes face the same validation and before_tool hooks as any
other. Streamed text has already gone out as events, and the tokens
are already counted.Source§fn on_model_error<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
_ctx: &'life1 HookContext<'life2>,
_error: &'life3 ProviderError,
) -> Pin<Box<dyn Future<Output = ModelErrorDecision> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
Self: 'async_trait,
fn on_model_error<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
_ctx: &'life1 HookContext<'life2>,
_error: &'life3 ProviderError,
) -> Pin<Box<dyn Future<Output = ModelErrorDecision> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
Self: 'async_trait,
Abort beats everything, then the first
Recover or Retry wins. With none, the run ends.Source§fn on_limit<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_ctx: &'life1 HookContext<'life2>,
_limit: LimitKind,
) -> Pin<Box<dyn Future<Output = LimitDecision> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait,
fn on_limit<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_ctx: &'life1 HookContext<'life2>,
_limit: LimitKind,
) -> Pin<Box<dyn Future<Output = LimitDecision> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait,
crate::Limits. Every hook is asked: a
Forbid beats any grant, then the first Extend wins, is recorded in crate::RunState::limit_grants, and lasts for
this run. crate::HARD_TURN_CAP cannot be extended and is never
put to a hook. A model call cut off by the wall clock is lost and
runs again if the limit is extended.Source§fn before_tool<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait>(
&'life0 self,
_ctx: &'life1 HookContext<'life2>,
_call: &'life3 ToolCall,
_tool: &'life4 ToolInfo,
) -> Pin<Box<dyn Future<Output = ToolDecision> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
'life4: 'async_trait,
Self: 'async_trait,
fn before_tool<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait>(
&'life0 self,
_ctx: &'life1 HookContext<'life2>,
_call: &'life3 ToolCall,
_tool: &'life4 ToolInfo,
) -> Pin<Box<dyn Future<Output = ToolDecision> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
'life4: 'async_trait,
Self: 'async_trait,
Deny, Interrupt or
Answer wins;
Rewrite changes the arguments seen by later hooks and the tool.Source§fn after_tool<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait>(
&'life0 self,
_ctx: &'life1 HookContext<'life2>,
_receipt: &'life3 Receipt,
_output: &'life4 mut ToolOutput,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
'life4: 'async_trait,
Self: 'async_trait,
fn after_tool<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait>(
&'life0 self,
_ctx: &'life1 HookContext<'life2>,
_receipt: &'life3 Receipt,
_output: &'life4 mut ToolOutput,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
'life4: 'async_trait,
Self: 'async_trait,
Source§fn on_stop<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
_ctx: &'life1 HookContext<'life2>,
_final_text: &'life3 str,
) -> Pin<Box<dyn Future<Output = StopDecision> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
Self: 'async_trait,
fn on_stop<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
_ctx: &'life1 HookContext<'life2>,
_final_text: &'life3 str,
) -> Pin<Box<dyn Future<Output = StopDecision> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
Self: 'async_trait,
Halt beats
everything, then the first Continue wins. More than
crate::Limits::max_idle_continuations in a row with no new
successful call ends the run unmet.Source§fn on_stuck<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
_ctx: &'life1 HookContext<'life2>,
_signal: &'life3 StuckSignal,
) -> Pin<Box<dyn Future<Output = Option<StuckDecision>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
Self: 'async_trait,
fn on_stuck<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
_ctx: &'life1 HookContext<'life2>,
_signal: &'life3 StuckSignal,
) -> Pin<Box<dyn Future<Output = Option<StuckDecision>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
Self: 'async_trait,
None means “no opinion”; if every hook
abstains, the default policy nudges and then stops.Auto Trait Implementations§
impl !Freeze for MemoryWriter
impl !RefUnwindSafe for MemoryWriter
impl !UnwindSafe for MemoryWriter
impl Forget for MemoryWriter
impl Move for MemoryWriter
impl Send for MemoryWriter
impl Sync for MemoryWriter
impl Unpin for MemoryWriter
impl UnsafeUnpin for MemoryWriter
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
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