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
//! `/compact`: compact history between turns and save the result, for the
//! host to report however it shows compactions (ACP, for one, as a
//! compaction card).
use rho_sdk::{CancellationToken, CompactionOutcome};
use super::{prompt_gate::ActiveWork, PromptScope, SessionRuntime};
impl SessionRuntime {
/// Compacts within `prompt`, with optional `instructions` on what to
/// keep, and saves the result; `None` when it was cancelled first. A
/// failure on the provider's credentials has the next prompt renew them.
pub(crate) async fn compact(
&mut self,
prompt: &PromptScope,
instructions: Option<String>,
) -> anyhow::Result<Option<CompactionOutcome>> {
let result = self.compact_and_save(prompt, instructions).await;
self.note_credential_failure(&result);
result
}
/// The SDK commits replacement history before `compact` returns, so the
/// work runs as a task: a cancel aborts it only while it is still running.
/// A task that already finished has committed, and its outcome is saved
/// like any other, as the TUI's abort does.
async fn compact_and_save(
&mut self,
prompt: &PromptScope,
instructions: Option<String>,
) -> anyhow::Result<Option<CompactionOutcome>> {
let cancel = CancellationToken::new();
prompt.activate(ActiveWork::command(cancel.clone()));
let session = self.built.session.clone();
let mut task = tokio::spawn(async move {
match instructions {
Some(instructions) => session.compact_with_instructions(instructions).await,
None => session.compact().await,
}
});
let joined = tokio::select! {
biased;
() = cancel.cancelled() => {
task.abort();
(&mut task).await
}
joined = &mut task => joined,
};
let outcome = match joined {
// Keep the typed error in the chain, so a refused credential
// still asks the host to sign in.
Ok(outcome) => outcome.map_err(|error| {
let message = format!("could not compact context: {error}");
anyhow::Error::new(error).context(message)
})?,
Err(error) if error.is_cancelled() => return Ok(None),
Err(error) => return Err(anyhow::anyhow!("could not compact context: {error}")),
};
let snapshot = self.durable_snapshot();
// An ephemeral session has nothing to save.
let saved = self.stored().and_then(|stored| {
stored.map_or(Ok(()), |stored| {
stored.save_compaction_snapshot(&snapshot, &[], &outcome)
})
});
if let Err(error) = saved {
// Live history, compaction accounting, and metadata already
// moved on; refuse later prompts rather than run against
// state a reload would not see.
self.unsaved_compaction = true;
return Err(error.context("could not save compacted context"));
}
// Saved, so the follow-up starts. A failed save refuses later
// prompts above, so nothing would follow it.
self.built.diagnostics.compaction_committed();
Ok(Some(outcome))
}
}