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
//! A prompt's lifecycle. A host starts each prompt with
//! [`SessionRuntime::begin_prompt`] and holds the returned [`PromptScope`]
//! until the prompt ends. Every in-prompt call (a turn, `/compact`,
//! `/workflow:`, an `/agent:` rebind, the plan handoff) takes the scope, so
//! the type, not a convention, keeps them within a prompt. Everything else a
//! prompt's end needs is the runtime's own: a turn notes a credential
//! failure for the next prompt to renew, and a turn that reached the model's
//! answer asks to name the session (see `title.rs`).
use rho_sdk::CancellationToken;
use super::{
prompt_gate::{ActiveWork, PromptGeneration},
SessionHandle, SessionRuntime,
};
use crate::config::Config;
/// One prompt the runtime started, held for the whole prompt. Dropping it
/// ends the prompt, however the prompt returns, so a cancel aimed at it does
/// not reach the next one.
///
/// Each scope belongs to one prompt generation: a scope dropped only after
/// a newer prompt began never ends that prompt, and reads its own as
/// cancelled.
#[must_use = "dropping a PromptScope ends the prompt at once"]
pub(crate) struct PromptScope {
cancellation: CancellationToken,
handle: SessionHandle,
generation: PromptGeneration,
/// Whether the prompt's start attached the session's deferred tool
/// sources, which changed its tool list.
attached_tool_sources: bool,
}
impl PromptScope {
/// Opens the next prompt on `handle`'s gate.
pub(super) fn begin(handle: &SessionHandle) -> Self {
let (cancellation, generation) = handle.gate().begin();
Self {
cancellation,
handle: handle.clone(),
generation,
attached_tool_sources: false,
}
}
/// Whether the session's tool list changed as the prompt started,
/// because its deferred tool sources attached then.
pub(crate) fn attached_tool_sources(&self) -> bool {
self.attached_tool_sources
}
/// Trips when the prompt is cancelled before its first run starts, and
/// is already cancelled when the session is closing, so work the host
/// does first, such as expanding a command, can stop.
pub(crate) fn cancellation(&self) -> &CancellationToken {
&self.cancellation
}
/// Whether this prompt was cancelled, at any point so far, or the
/// session is closing.
pub(super) fn is_cancelled(&self) -> bool {
self.handle.gate().is_cancelled(self.generation)
}
/// Makes `work` what this prompt runs now, so a cancel reaches it, and
/// one that already landed cancels it at once; a model run's steering
/// goes to [`SessionHandle::steer`].
pub(super) fn activate(&self, work: ActiveWork) {
self.handle.gate().activate(self.generation, work);
}
}
impl Drop for PromptScope {
fn drop(&mut self) {
self.handle.gate().finish(self.generation);
}
}
impl SessionRuntime {
/// Starts a prompt: refuses it while a compaction's rewrite is unsaved,
/// opens the window in which a cancel reaches it, rebuilds what the
/// session built on providers `process_config` changed (see
/// `providers.rs`), renews credentials a logout or a refused request
/// left stale, and waits for tool sources
/// the session still loads. Hold the returned scope
/// for the whole prompt and pass it to each of the prompt's calls.
pub(crate) async fn begin_prompt(
&mut self,
process_config: &Config,
) -> anyhow::Result<PromptScope> {
if self.unsaved_compaction {
return Err(super::UnsavedCompaction.into());
}
let mut scope = PromptScope::begin(&self.handle);
// Providers first: a changed host leaves the provider stale for the
// renewal to rebuild.
let renewed = match self.refresh_providers(process_config).await {
Ok(()) => self.renew_stale_credentials(process_config).await,
Err(error) => Err(error),
};
// With nothing to renew from, as after a logout, the renewal's read
// cached the miss in the OS store, and the login that fixes it may
// run in another process: the next prompt rereads.
self.note_credential_failure(&renewed);
renewed?;
// The prompt's tools must be there before it runs. A cancel stops
// the wait, and the prompt then ends as cancelled. Last, so a
// prompt whose start attached them returns the scope that says so.
if let Some(loaded) = self.tool_sources_loaded() {
tokio::select! {
() = scope.cancellation().cancelled() => {}
() = loaded => {}
}
}
scope.attached_tool_sources = self.attach_loaded_tool_sources(process_config).await?;
Ok(scope)
}
}