feat: add usage snapshot event and enhance compaction logic

- Introduced `UsageSnapshot` event to track token usage during conversation runs.
- Updated `RunEngine` to emit usage snapshots, providing better visibility into token consumption.
- Refactored compaction logic to utilize a new `compaction_estimate` function for improved token budget management.
- Added tests to validate timeout constants for blob synchronization and ensure correct behavior of usage tracking during compaction.
This commit is contained in:
leokun committed 2026-09-01 20:04:35 +08:00
1 parent e768980dad
commit 76417e005b
6 files changed
+81 -27

No files matched your search

+8
View File
@@ -399,6 +399,14 @@ impl ConversationOutput {
.unwrap_or_else(|_| serde_json::json!({}))
};
}
RunEvent::UsageSnapshot(usage) => {
if !self.context.compacting {
if let Some(output_tokens) = usage.output_tokens {
self.handle.emit(&events::token_delta(output_tokens))?;
}
context_tokens = usage.context_input_tokens;
}
}
RunEvent::Usage(usage) => {
if !self.context.compacting {
if let Some(output_tokens) = usage.output_tokens {
+20 -2
View File
@@ -20,6 +20,9 @@ use crate::{
type BlobSetSender = oneshot::Sender<Result<()>>;
const SET_TIMEOUT: Duration = Duration::from_secs(30 * 60);
const GET_TIMEOUT: Duration = Duration::from_secs(10 * 60);
#[derive(Clone)]
pub struct BlobSynchronizer {
inner: Arc<Inner>,
@@ -130,7 +133,7 @@ impl BlobSynchronizer {
let result = tokio::select! {
result = receiver => result.map_err(|_| Error::Protocol("KV SET response channel closed".into()))?,
_ = cancellation.cancelled() => Err(Error::Cancelled),
_ = tokio::time::sleep(Duration::from_secs(60)) => Err(Error::Protocol(format!("KV SET timed out: {}", blob_id.to_base64()))),
_ = tokio::time::sleep(SET_TIMEOUT) => Err(Error::Protocol(format!("KV SET timed out: {}", blob_id.to_base64()))),
};
if result.is_err() {
self.inner.set_requests.lock().await.remove(&id);
@@ -183,7 +186,7 @@ impl BlobSynchronizer {
let result = tokio::select! {
result = receiver => result.map_err(|_| Error::Protocol("KV GET response channel closed".into()))?,
_ = cancellation.cancelled() => Err(Error::Cancelled),
_ = tokio::time::sleep(Duration::from_secs(60)) => Err(Error::Protocol(format!("KV GET timed out: {}", blob_id.to_base64()))),
_ = tokio::time::sleep(GET_TIMEOUT) => Err(Error::Protocol(format!("KV GET timed out: {}", blob_id.to_base64()))),
};
if result.is_err() {
self.inner.get_requests.lock().await.remove(&id);
@@ -318,3 +321,18 @@ impl BlobSynchronizer {
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn set_timeout_allows_slow_cursor_acknowledgements() {
assert_eq!(SET_TIMEOUT, Duration::from_secs(30 * 60));
}
#[test]
fn get_timeout_allows_slow_cursor_responses() {
assert_eq!(GET_TIMEOUT, Duration::from_secs(10 * 60));
}
}