@@ -290,6 +290,48 @@ fn substitute_strings(v: &Value, next: &mut std::vec::IntoIter<String>) -> Value
290290 }
291291}
292292
293+ /// Every placeholder-shaped token a model turn of this run produced — the
294+ /// set [`Runner::rehydrate_from_model`] is allowed to resolve or refuse.
295+ ///
296+ /// Read off the journaled outcomes, not accumulated from live calls, so a
297+ /// resumed run and a fresh one agree: `results` holds every settled effect
298+ /// the journal records, replayed or not. "Model turn" means an LLM effect,
299+ /// and — conservatively — a subgraph's result, which may carry a child
300+ /// model's output into the parent.
301+ #[ derive( Default ) ]
302+ struct ModelTokens {
303+ scanned : std:: collections:: BTreeSet < JournalKey > ,
304+ tokens : std:: collections:: BTreeSet < String > ,
305+ }
306+
307+ impl ModelTokens {
308+ fn refresh (
309+ & mut self ,
310+ results : & BTreeMap < JournalKey , EffectOutcome > ,
311+ model_origin : & dyn Fn ( & JournalKey ) -> bool ,
312+ template : & str ,
313+ ) -> Result < ( ) , String > {
314+ let none = BTreeMap :: new ( ) ;
315+ for ( key, outcome) in results {
316+ if self . scanned . contains ( key) || !model_origin ( key) {
317+ continue ;
318+ }
319+ self . scanned . insert ( key. clone ( ) ) ;
320+ let EffectOutcome :: Completed { result, .. } = outcome else { continue } ;
321+ let mut strings = Vec :: new ( ) ;
322+ collect_strings ( result, & mut strings) ;
323+ for s in strings {
324+ // Against an empty mapping every placeholder is a leftover,
325+ // in the policy's own shape.
326+ let r = areev_core:: anon:: rehydrate_with_template ( & s, & none, template)
327+ . map_err ( |e| e. to_string ( ) ) ?;
328+ self . tokens . extend ( r. unmatched ) ;
329+ }
330+ }
331+ Ok ( ( ) )
332+ }
333+ }
334+
293335/// The anonymization boundary between run state and a model.
294336///
295337/// ## Why this seam and not the tool seam
@@ -307,14 +349,24 @@ fn substitute_strings(v: &Value, next: &mut std::vec::IntoIter<String>) -> Value
307349/// ## The round trip
308350///
309351/// Out, in [`Runner::pseudonymize_for_model`]: every string in the LLM effect's input is
310- /// pseudonymized. Back, in [`Runner::rehydrate_from_model`]: the tool-call arguments the
311- /// model produced are rehydrated *before* dispatch, so the tool sees real
312- /// values again.
352+ /// pseudonymized. Back, in [`Runner::rehydrate_from_model`]: placeholders a
353+ /// MODEL produced are rehydrated *before* dispatch, so the tool sees real
354+ /// values again — whether they arrive as a model's tool-call arguments or as
355+ /// its output flowing into a downstream node's input.
356+ ///
357+ /// ## Only what a model produced (#350)
358+ ///
359+ /// A token is rehydrated — or refused — only when it appears in the output of
360+ /// a model turn in this run ([`ModelTokens`]). Text a host or code tool wrote
361+ /// itself is dispatched verbatim, even when it has the placeholder's shape: a
362+ /// deterministic pipeline that pseudonymizes a record before it leaves writes
363+ /// `[PERSON_1]` markers no model produced, and neither failing them as
364+ /// "unresolvable" nor splicing in an older mapping's value for them is right.
313365///
314366/// Rehydration **fails closed**. `rehydrate` leaves a token it cannot resolve
315367/// intact and reports it; dispatching a partially rehydrated call would post a
316- /// literal `[PERSON_A4F2]` to a vendor, so an unresolved token fails the node
317- /// instead.
368+ /// literal `[PERSON_A4F2]` to a vendor, so an unresolved model-produced token
369+ /// fails the node instead.
318370impl Runner {
319371 /// Is a model-facing anonymization policy live for this run's namespace?
320372 fn anon_boundary_active ( & self ) -> Result < bool , RunError > {
@@ -341,19 +393,27 @@ impl Runner {
341393 Ok ( substitute_strings ( input, & mut values. into_iter ( ) ) )
342394 }
343395
344- /// Rehydrate what came back, or say exactly which token could not be.
396+ /// Rehydrate the placeholders a model produced, or say exactly which one
397+ /// could not be.
398+ ///
399+ /// Scoped to [`ModelTokens`]: a placeholder-shaped string no model turn of
400+ /// this run emitted is host-written text and passes through verbatim —
401+ /// never failed as unresolvable, never rewritten to whatever an older
402+ /// mapping of the namespace holds under that key (#350).
345403 ///
346404 /// `Err` here is the fail-closed case: the caller turns it into a failed
347405 /// effect rather than dispatching a call with a placeholder still in it.
348- fn rehydrate_from_model ( & self , input : & Value ) -> Result < Value , String > {
406+ fn rehydrate_from_model (
407+ & self ,
408+ input : & Value ,
409+ model : & mut ModelTokens ,
410+ results : & BTreeMap < JournalKey , EffectOutcome > ,
411+ model_origin : & dyn Fn ( & JournalKey ) -> bool ,
412+ ) -> Result < Value , String > {
349413 let active = self . anon_boundary_active ( ) . map_err ( |e| e. to_string ( ) ) ?;
350414 if !active {
351415 return Ok ( input. clone ( ) ) ;
352416 }
353- let mapping = self
354- . facade
355- . with_store ( |m| m. anon_mapping_for ( & self . ns ) )
356- . map_err ( |e| e. to_string ( ) ) ?;
357417 // The policy's OWN template, not the default. Scanning for the default
358418 // silhouette while the memory mints another shape would report no
359419 // leftovers and fail open — the exact inversion of what this check is
@@ -368,12 +428,26 @@ impl Runner {
368428 if values. is_empty ( ) {
369429 return Ok ( input. clone ( ) ) ;
370430 }
431+ model. refresh ( results, model_origin, & template) ?;
432+ if model. tokens . is_empty ( ) {
433+ // No model turn produced a placeholder: nothing here is ours to
434+ // resolve, whatever its shape.
435+ return Ok ( input. clone ( ) ) ;
436+ }
437+ let mapping: BTreeMap < String , String > = self
438+ . facade
439+ . with_store ( |m| m. anon_mapping_for ( & self . ns ) )
440+ . map_err ( |e| e. to_string ( ) ) ?
441+ . into_iter ( )
442+ . filter ( |( k, _) | model. tokens . contains ( k) )
443+ . collect ( ) ;
371444 let mut out = Vec :: with_capacity ( values. len ( ) ) ;
372445 let mut unmatched: Vec < String > = Vec :: new ( ) ;
373446 for v in values {
374447 let r = areev_core:: anon:: rehydrate_with_template ( & v, & mapping, & template)
375448 . map_err ( |e| e. to_string ( ) ) ?;
376- for u in r. unmatched {
449+ // A leftover no model produced is host-written text, left as is.
450+ for u in r. unmatched . into_iter ( ) . filter ( |u| model. tokens . contains ( u) ) {
377451 if !unmatched. contains ( & u) {
378452 unmatched. push ( u) ;
379453 }
@@ -1788,6 +1862,19 @@ impl Runner {
17881862 . iter ( )
17891863 . filter_map ( |( k, e) | e. result . as_ref ( ) . map ( |( _, o) | ( k. clone ( ) , o. clone ( ) ) ) )
17901864 . collect ( ) ;
1865+ // What a model produced, for the egress round trip (#350).
1866+ let mut model_tokens = ModelTokens :: default ( ) ;
1867+ let model_origin = |k : & JournalKey | {
1868+ k. kind == areev_run_core:: EffectKind :: Llm
1869+ || plan
1870+ . nodes
1871+ . iter ( )
1872+ . position ( |n| * n == k. node )
1873+ . and_then ( |i| executors. get ( i) )
1874+ . is_some_and ( |e| {
1875+ matches ! ( e, areev_run_core:: NodeExecutor :: Subgraph { .. } )
1876+ } )
1877+ } ;
17911878 let mut prev_ckpt: Option < Hash > = view. checkpoints . last ( ) . map ( |c| c. hash ) ;
17921879 let mut input_cursor = view. cursor ;
17931880 // The host-pause state (#344): the standing request, if any, polled
@@ -2192,7 +2279,19 @@ impl Runner {
21922279 // Rehydrate for the tool, never for the record: the
21932280 // intent above journaled the pseudonymized input and
21942281 // `idem` derives from it, so verify replays identically.
2195- let input = match self . rehydrate_from_model ( & input) {
2282+ // An LLM turn is exempt: what it sends is `to_model`,
2283+ // prepared above from the unrehydrated input.
2284+ let rehydrated = if key. kind == areev_run_core:: EffectKind :: Llm {
2285+ Ok ( input)
2286+ } else {
2287+ self . rehydrate_from_model (
2288+ & input,
2289+ & mut model_tokens,
2290+ & results,
2291+ & model_origin,
2292+ )
2293+ } ;
2294+ let input = match rehydrated {
21962295 Ok ( v) => v,
21972296 Err ( why) => {
21982297 // Fail the node, not the run: a plan's retries
0 commit comments