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
//! The definition a saved session reopens as: its agent's current
//! definition, whose tools, model and runtime it runs with, and the
//! instructions the session was created with, which its conversation was
//! had under. The session's header keeps them (`SavedAgent::prompt`); a
//! session saved before it did reads them back from its saved system
//! prompt.
use crate::{
agent::{AgentDefinition, PromptPolicy},
prompt::agent_instructions,
session::SavedAgent,
};
use super::subagent_messaging::CHILD_COMMUNICATION_CONTRACT;
/// Whether a saved agent's instructions are the session's, which its saved
/// system prompt renders.
pub(crate) enum Instructions<'a> {
/// They are; this reads the session's saved system prompt.
Rendered(&'a dyn Fn() -> anyhow::Result<Option<String>>),
/// They are not: a session `/agent:` rebound runs its child's, and its
/// root only caps the child's tools.
NotRendered,
}
/// The definition a session saved as `saved` reopens as, `current` being
/// that agent's definition now. Refused when `current` is another agent
/// (#1661). A session whose header kept no instructions runs `current`'s
/// when it was saved under `current`, else those its saved prompt holds,
/// else, with a warning, `current`'s.
pub(crate) fn reopened(
saved: &SavedAgent,
current: &AgentDefinition,
instructions: Instructions<'_>,
) -> anyhow::Result<AgentDefinition> {
saved.validate(current)?;
let prompt = match (&saved.prompt, instructions) {
(Some(prompt), _) => prompt.clone(),
(None, Instructions::NotRendered) => current.prompt.clone(),
(None, Instructions::Rendered(_))
if current.accepts_stored_fingerprint(&saved.fingerprint) =>
{
current.prompt.clone()
}
(None, Instructions::Rendered(read)) => {
let system = read()?;
match system
.as_deref()
.and_then(|system| agent_instructions::of(system, &[CHILD_COMMUNICATION_CONTRACT]))
{
Some(text) => PromptPolicy::Extend(text.to_owned()),
None => {
tracing::warn!(
agent = %saved.id,
"a session saved before its agent's instructions were kept, whose prompt holds none, runs the agent's current ones"
);
current.prompt.clone()
}
}
}
};
Ok(AgentDefinition {
prompt,
..current.clone()
})
}
#[cfg(test)]
#[path = "agent_reopen_tests.rs"]
mod tests;