Reuse canonical chat context

This commit is contained in:
Georg Bauer
2026-07-27 18:35:52 +02:00
parent ee7e5696a5
commit 4adb49a4d1
10 changed files with 567 additions and 121 deletions

View File

@@ -19,7 +19,6 @@ pub(crate) struct GenerationService {
pub(crate) struct ActiveGeneration {
pub(crate) events: Receiver<GenerationEvent>,
pub(crate) cancel: Arc<AtomicBool>,
pub(crate) started_at: Instant,
}
impl Drop for ActiveGeneration {
@@ -29,7 +28,11 @@ impl Drop for ActiveGeneration {
}
pub(crate) enum CheckpointTarget {
Local(PathBuf),
Local {
checkpoint: PathBuf,
/// Shared cache for the deterministic rendered system prompt.
bootstrap: Option<PathBuf>,
},
Transient(PathBuf),
/// Same transient KV handling as [`CheckpointTarget::Transient`], but asked
/// for by the app itself (session titling) rather than by an HTTP client.
@@ -39,7 +42,7 @@ pub(crate) enum CheckpointTarget {
impl CheckpointTarget {
fn source(&self) -> WorkSource {
match self {
Self::Local(_) | Self::OneShot(_) => WorkSource::LocalChat,
Self::Local { .. } | Self::OneShot(_) => WorkSource::LocalChat,
Self::Transient(_) => WorkSource::Http,
}
}
@@ -108,7 +111,6 @@ impl GenerationService {
checkpoint: CheckpointTarget,
idle_timeout: Duration,
) -> Result<ActiveGeneration, String> {
let started_at = Instant::now();
let source = checkpoint.source();
let cancel = Arc::new(AtomicBool::new(false));
let (events, receiver) = mpsc::channel();
@@ -133,7 +135,6 @@ impl GenerationService {
Ok(ActiveGeneration {
events: receiver,
cancel,
started_at,
})
}
@@ -148,7 +149,6 @@ impl GenerationService {
checkpoint: PathBuf,
idle_timeout: Duration,
) -> Result<ActiveGeneration, String> {
let started_at = Instant::now();
let cancel = Arc::new(AtomicBool::new(false));
let (events, receiver) = mpsc::channel();
self.commands
@@ -156,7 +156,10 @@ impl GenerationService {
engine,
turn,
messages,
checkpoint: CheckpointTarget::Local(checkpoint),
checkpoint: CheckpointTarget::Local {
checkpoint,
bootstrap: None,
},
operation: Operation::Compact {
reason: reason.to_owned(),
rebuild_system_prompt,
@@ -169,7 +172,6 @@ impl GenerationService {
Ok(ActiveGeneration {
events: receiver,
cancel,
started_at,
})
}
@@ -180,7 +182,6 @@ impl GenerationService {
messages: Vec<ChatTurn>,
idle_timeout: Duration,
) -> Result<ActiveGeneration, String> {
let started_at = Instant::now();
let cancel = Arc::new(AtomicBool::new(false));
let (events, receiver) = mpsc::channel();
self.metrics.request_queued(WorkSource::LocalChat);
@@ -199,7 +200,6 @@ impl GenerationService {
Ok(ActiveGeneration {
events: receiver,
cancel,
started_at,
})
}
}
@@ -335,7 +335,7 @@ fn run_command(
rebuild_system_prompt,
} = &command.operation
{
let CheckpointTarget::Local(checkpoint) = &command.checkpoint else {
let CheckpointTarget::Local { checkpoint, .. } = &command.checkpoint else {
unreachable!("compaction checkpoints are local")
};
let result = generator.compact(
@@ -367,8 +367,12 @@ fn run_command(
return;
}
let result = match command.checkpoint {
CheckpointTarget::Local(path) => generator.generate(
&path,
CheckpointTarget::Local {
checkpoint,
bootstrap,
} => generator.generate(
&checkpoint,
bootstrap.as_deref(),
&command.messages,
&command.turn,
&command.cancel,
@@ -440,7 +444,6 @@ mod tests {
drop(ActiveGeneration {
events,
cancel: Arc::clone(&cancel),
started_at: Instant::now(),
});
assert!(cancel.load(Ordering::Relaxed));