cargo fmt
This commit is contained in:
@@ -88,8 +88,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
// Wait for completion
|
||||
tokio::time::sleep(std::time::Duration::from_secs(15)).await;
|
||||
println!("\n[shared_state] final: {}", handle.shared_state.status_json());
|
||||
println!("[history] {} bytes", handle.shared_state.history_json().len());
|
||||
println!(
|
||||
"\n[shared_state] final: {}",
|
||||
handle.shared_state.status_json()
|
||||
);
|
||||
println!(
|
||||
"[history] {} bytes",
|
||||
handle.shared_state.history_json().len()
|
||||
);
|
||||
|
||||
drop(handle);
|
||||
let _ = listener.await;
|
||||
|
||||
@@ -9,11 +9,11 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use llm_worker::Item;
|
||||
use llm_worker::interceptor::{
|
||||
Interceptor, PostToolAction, PreRequestAction, PreToolAction, PromptAction, ToolCallInfo,
|
||||
ToolResultInfo, TurnEndAction,
|
||||
};
|
||||
use llm_worker::Item;
|
||||
use tracing::info;
|
||||
|
||||
use crate::compact_state::CompactState;
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
||||
use llm_worker::llm_client::client::LlmClient;
|
||||
use llm_worker::WorkerError;
|
||||
use llm_worker::llm_client::client::LlmClient;
|
||||
use session_store::Store;
|
||||
use tokio::sync::{broadcast, mpsc};
|
||||
|
||||
use crate::pod::{Pod, PodRunResult, PodError};
|
||||
use protocol::{ErrorCode, Event, Method, RunResult, TurnResult};
|
||||
use crate::pod::{Pod, PodError, PodRunResult};
|
||||
use crate::runtime_dir::RuntimeDir;
|
||||
use crate::shared_state::{PodSharedState, PodStatus};
|
||||
use crate::socket_server::SocketServer;
|
||||
use protocol::{ErrorCode, Event, Method, RunResult, TurnResult};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// PodHandle — client-facing, Clone-able
|
||||
|
||||
@@ -8,11 +8,11 @@
|
||||
//! concerns belong.
|
||||
|
||||
use async_trait::async_trait;
|
||||
use llm_worker::Item;
|
||||
use llm_worker::interceptor::{
|
||||
PostToolAction, PreRequestAction, PreToolAction, PromptAction, ToolCallInfo, ToolResultInfo,
|
||||
TurnEndAction,
|
||||
};
|
||||
use llm_worker::Item;
|
||||
|
||||
// =============================================================================
|
||||
// Hook Event Kinds
|
||||
|
||||
@@ -3,11 +3,11 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use llm_worker::Item;
|
||||
use llm_worker::interceptor::{
|
||||
Interceptor, PostToolAction, PreRequestAction, PreToolAction, PromptAction, ToolCallInfo,
|
||||
ToolResultInfo, TurnEndAction,
|
||||
};
|
||||
use llm_worker::Item;
|
||||
|
||||
use crate::hook::HookRegistry;
|
||||
|
||||
|
||||
@@ -15,8 +15,8 @@ mod usage_tracker;
|
||||
pub use token_counter::{EstimateSource, SplitPoint, TokenEstimate};
|
||||
|
||||
pub use controller::{PodController, PodHandle};
|
||||
pub use manifest::{PodManifest, ProviderConfig, ProviderKind, Scope};
|
||||
pub use hook::{Hook, HookEventKind, HookRegistryBuilder};
|
||||
pub use manifest::{PodManifest, ProviderConfig, ProviderKind, Scope};
|
||||
pub use pod::{Pod, PodError, PodRunResult, apply_worker_manifest};
|
||||
pub use protocol::{ErrorCode, Event, Method, TurnResult};
|
||||
pub use provider::{ProviderError, build_client};
|
||||
|
||||
@@ -2,8 +2,8 @@ use std::path::{Path, PathBuf};
|
||||
use std::process::ExitCode;
|
||||
|
||||
use clap::Parser;
|
||||
use session_store::FsStore;
|
||||
use pod::{Pod, PodController};
|
||||
use session_store::FsStore;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(name = "pod", about = "Run a Pod process from a manifest file")]
|
||||
@@ -18,9 +18,8 @@ struct Cli {
|
||||
}
|
||||
|
||||
fn default_store_dir() -> Result<PathBuf, std::io::Error> {
|
||||
let home = std::env::var("HOME").map_err(|_| {
|
||||
std::io::Error::new(std::io::ErrorKind::NotFound, "HOME is not set")
|
||||
})?;
|
||||
let home = std::env::var("HOME")
|
||||
.map_err(|_| std::io::Error::new(std::io::ErrorKind::NotFound, "HOME is not set"))?;
|
||||
Ok(PathBuf::from(home).join(".insomnia").join("sessions"))
|
||||
}
|
||||
|
||||
@@ -111,7 +110,10 @@ async fn main() -> ExitCode {
|
||||
}
|
||||
};
|
||||
|
||||
eprintln!("pod: {pod_name} listening on {:?}", handle.runtime_dir.socket_path());
|
||||
eprintln!(
|
||||
"pod: {pod_name} listening on {:?}",
|
||||
handle.runtime_dir.socket_path()
|
||||
);
|
||||
|
||||
// Wait for shutdown signal
|
||||
match tokio::signal::ctrl_c().await {
|
||||
|
||||
+51
-38
@@ -2,8 +2,8 @@ use std::path::PathBuf;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use llm_worker::Item;
|
||||
use llm_worker::llm_client::client::LlmClient;
|
||||
use llm_worker::llm_client::RequestConfig;
|
||||
use llm_worker::llm_client::client::LlmClient;
|
||||
use llm_worker::state::Mutable;
|
||||
use llm_worker::{Worker, WorkerError, WorkerResult};
|
||||
use session_store::{
|
||||
@@ -21,8 +21,8 @@ use crate::hook::{
|
||||
};
|
||||
use crate::hook_interceptor::HookInterceptor;
|
||||
use crate::usage_tracker::UsageTracker;
|
||||
use llm_worker::interceptor::PreRequestAction;
|
||||
use async_trait::async_trait;
|
||||
use llm_worker::interceptor::PreRequestAction;
|
||||
|
||||
/// Pre-LLM-request hook that records `history.len()` at send time into a
|
||||
/// shared `UsageTracker`. The on_usage callback later pairs this with the
|
||||
@@ -205,7 +205,10 @@ impl<C: LlmClient, St: Store> Pod<C, St> {
|
||||
/// Returns a clone since the underlying vector is shared with hooks
|
||||
/// running on the Worker.
|
||||
pub fn usage_history(&self) -> Vec<UsageRecord> {
|
||||
self.usage_history.lock().expect("usage_history poisoned").clone()
|
||||
self.usage_history
|
||||
.lock()
|
||||
.expect("usage_history poisoned")
|
||||
.clone()
|
||||
}
|
||||
|
||||
/// Shared handle to the cumulative Usage history.
|
||||
@@ -292,10 +295,9 @@ impl<C: LlmClient, St: Store> Pod<C, St> {
|
||||
// Pre-LLM-request hook: capture history.len() into the
|
||||
// UsageTracker so the upcoming on_usage callback can pair
|
||||
// it with the measured input_tokens.
|
||||
self.hook_builder
|
||||
.add_pre_llm_request(UsageTrackingHook {
|
||||
tracker: self.usage_tracker.clone(),
|
||||
});
|
||||
self.hook_builder.add_pre_llm_request(UsageTrackingHook {
|
||||
tracker: self.usage_tracker.clone(),
|
||||
});
|
||||
|
||||
let builder = std::mem::take(&mut self.hook_builder);
|
||||
let registry = Arc::new(builder.build());
|
||||
@@ -430,8 +432,9 @@ impl<C: LlmClient, St: Store> Pod<C, St> {
|
||||
/// async layout cycle (`run → handle_worker_result → do_compact_and_resume → resume`).
|
||||
fn do_compact_and_resume(
|
||||
&mut self,
|
||||
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<PodRunResult, PodError>> + Send + '_>>
|
||||
{
|
||||
) -> std::pin::Pin<
|
||||
Box<dyn std::future::Future<Output = Result<PodRunResult, PodError>> + Send + '_>,
|
||||
> {
|
||||
Box::pin(async move {
|
||||
// Thrash detection: if we just compacted and hit the threshold again,
|
||||
// something is wrong.
|
||||
@@ -475,9 +478,7 @@ impl<C: LlmClient, St: Store> Pod<C, St> {
|
||||
/// Best-effort: failures are logged but do not propagate.
|
||||
pub async fn try_post_run_compact(&mut self) -> Result<(), PodError> {
|
||||
let state = match self.compact_state.as_ref() {
|
||||
Some(s) if !s.is_disabled() && s.exceeds_post_run() && !s.just_compacted() => {
|
||||
s.clone()
|
||||
}
|
||||
Some(s) if !s.is_disabled() && s.exceeds_post_run() && !s.just_compacted() => s.clone(),
|
||||
_ => return Ok(()),
|
||||
};
|
||||
|
||||
@@ -509,13 +510,8 @@ impl<C: LlmClient, St: Store> Pod<C, St> {
|
||||
// head_hash mutable).
|
||||
let w = self.worker.as_ref().unwrap();
|
||||
let new_items = &w.history()[history_before..];
|
||||
session_store::save_delta(
|
||||
&self.store,
|
||||
self.session_id,
|
||||
&mut self.head_hash,
|
||||
new_items,
|
||||
)
|
||||
.await?;
|
||||
session_store::save_delta(&self.store, self.session_id, &mut self.head_hash, new_items)
|
||||
.await?;
|
||||
|
||||
let turn_count = self.worker.as_ref().unwrap().turn_count();
|
||||
session_store::save_turn_end(
|
||||
@@ -544,7 +540,10 @@ impl<C: LlmClient, St: Store> Pod<C, St> {
|
||||
record.output_tokens,
|
||||
)
|
||||
.await?;
|
||||
self.usage_history.lock().expect("usage_history poisoned").push(record);
|
||||
self.usage_history
|
||||
.lock()
|
||||
.expect("usage_history poisoned")
|
||||
.push(record);
|
||||
}
|
||||
|
||||
let interrupted = self.worker.as_ref().unwrap().last_run_interrupted();
|
||||
@@ -578,10 +577,7 @@ impl<C: LlmClient, St: Store> Pod<C, St> {
|
||||
/// - a clone of the main LlmClient via `clone_boxed()`.
|
||||
///
|
||||
/// Returns the new session ID.
|
||||
pub async fn compact(
|
||||
&mut self,
|
||||
retained_turns: usize,
|
||||
) -> Result<SessionId, PodError> {
|
||||
pub async fn compact(&mut self, retained_turns: usize) -> Result<SessionId, PodError> {
|
||||
let worker = self.worker.as_ref().expect("worker taken during run");
|
||||
let history = worker.history();
|
||||
|
||||
@@ -612,13 +608,20 @@ impl<C: LlmClient, St: Store> Pod<C, St> {
|
||||
.temperature(0.0);
|
||||
summary_worker.set_max_tokens(2048);
|
||||
|
||||
let out = summary_worker.run(summary_prompt).await
|
||||
let out = summary_worker
|
||||
.run(summary_prompt)
|
||||
.await
|
||||
.map_err(PodError::Worker)?;
|
||||
let summary_text = out.worker
|
||||
let summary_text = out
|
||||
.worker
|
||||
.history()
|
||||
.iter()
|
||||
.filter_map(|item| {
|
||||
if item.is_assistant_message() { item.as_text().map(String::from) } else { None }
|
||||
if item.is_assistant_message() {
|
||||
item.as_text().map(String::from)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
@@ -632,7 +635,9 @@ impl<C: LlmClient, St: Store> Pod<C, St> {
|
||||
|
||||
// Persist as a new compacted session.
|
||||
let old_session_id = self.session_id;
|
||||
let old_head_hash = self.head_hash.clone()
|
||||
let old_head_hash = self
|
||||
.head_hash
|
||||
.clone()
|
||||
.expect("head_hash should be set after at least one entry");
|
||||
|
||||
let w = self.worker.as_ref().unwrap();
|
||||
@@ -655,7 +660,10 @@ impl<C: LlmClient, St: Store> Pod<C, St> {
|
||||
self.session_id = new_session_id;
|
||||
self.head_hash = Some(new_head_hash);
|
||||
self.worker.as_mut().unwrap().set_history(new_history);
|
||||
self.usage_history.lock().expect("usage_history poisoned").clear();
|
||||
self.usage_history
|
||||
.lock()
|
||||
.expect("usage_history poisoned")
|
||||
.clear();
|
||||
|
||||
Ok(new_session_id)
|
||||
}
|
||||
@@ -715,7 +723,6 @@ impl<St: Store> Pod<Box<dyn LlmClient>, St> {
|
||||
pod.apply_prune_from_manifest();
|
||||
Ok(pod)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// Apply worker-level manifest settings to a Worker.
|
||||
@@ -769,18 +776,24 @@ fn build_summary_prompt(items: &[Item]) -> String {
|
||||
llm_worker::Role::Assistant => "Assistant",
|
||||
llm_worker::Role::System => "System",
|
||||
};
|
||||
let text: String = content.iter().map(|p| p.as_text()).collect::<Vec<_>>().join("");
|
||||
let text: String = content
|
||||
.iter()
|
||||
.map(|p| p.as_text())
|
||||
.collect::<Vec<_>>()
|
||||
.join("");
|
||||
lines.push(format!("[{role_label}] {text}"));
|
||||
}
|
||||
Item::ToolCall { name, arguments, .. } => {
|
||||
Item::ToolCall {
|
||||
name, arguments, ..
|
||||
} => {
|
||||
lines.push(format!("[ToolCall] {name}({arguments})"));
|
||||
}
|
||||
Item::ToolResult { summary, content, .. } => {
|
||||
match content {
|
||||
Some(c) => lines.push(format!("[ToolResult] {summary}\n{c}")),
|
||||
None => lines.push(format!("[ToolResult] {summary}")),
|
||||
}
|
||||
}
|
||||
Item::ToolResult {
|
||||
summary, content, ..
|
||||
} => match content {
|
||||
Some(c) => lines.push(format!("[ToolResult] {summary}\n{c}")),
|
||||
None => lines.push(format!("[ToolResult] {summary}")),
|
||||
},
|
||||
Item::Reasoning { text, .. } => {
|
||||
lines.push(format!("[Reasoning] {text}"));
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use std::sync::RwLock;
|
||||
|
||||
use llm_worker::llm_client::types::Item;
|
||||
use session_store::SessionId;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use session_store::SessionId;
|
||||
|
||||
/// Shared state between PodController and runtime directory.
|
||||
///
|
||||
@@ -25,11 +25,7 @@ pub enum PodStatus {
|
||||
}
|
||||
|
||||
impl PodSharedState {
|
||||
pub fn new(
|
||||
pod_name: String,
|
||||
session_id: SessionId,
|
||||
manifest_toml: String,
|
||||
) -> Self {
|
||||
pub fn new(pod_name: String, session_id: SessionId, manifest_toml: String) -> Self {
|
||||
Self {
|
||||
pod_name,
|
||||
session_id,
|
||||
|
||||
@@ -42,10 +42,7 @@ impl SocketServer {
|
||||
}
|
||||
});
|
||||
|
||||
Ok(Self {
|
||||
_accept_task,
|
||||
path,
|
||||
})
|
||||
Ok(Self { _accept_task, path })
|
||||
}
|
||||
|
||||
/// The socket file path.
|
||||
|
||||
@@ -140,9 +140,7 @@ fn tokens_at(
|
||||
let up_bytes = prefix[up.history_len.min(cap)];
|
||||
let at_bytes = prefix[index];
|
||||
let span_bytes = up_bytes.saturating_sub(lo_bytes);
|
||||
let span_tokens = up
|
||||
.input_total_tokens
|
||||
.saturating_sub(lo.input_total_tokens);
|
||||
let span_tokens = up.input_total_tokens.saturating_sub(lo.input_total_tokens);
|
||||
if span_bytes == 0 || span_tokens == 0 {
|
||||
return TokenEstimate {
|
||||
tokens: lo.input_total_tokens,
|
||||
@@ -198,11 +196,7 @@ fn total_tokens_impl(history: &[Item], records: &[UsageRecord]) -> TokenEstimate
|
||||
tokens_at(history, records, history.len(), &prefix)
|
||||
}
|
||||
|
||||
fn split_for_retained_impl(
|
||||
history: &[Item],
|
||||
records: &[UsageRecord],
|
||||
retained: u64,
|
||||
) -> SplitPoint {
|
||||
fn split_for_retained_impl(history: &[Item], records: &[UsageRecord], retained: u64) -> SplitPoint {
|
||||
let prefix = prefix_bytes(history);
|
||||
let current = tokens_at(history, records, history.len(), &prefix);
|
||||
if current.tokens <= retained {
|
||||
@@ -351,12 +345,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn split_interpolated_between_measurements() {
|
||||
let history = vec![
|
||||
msg("aaaaaa"),
|
||||
msg("bbbbbb"),
|
||||
msg("cccccc"),
|
||||
msg("dddddd"),
|
||||
];
|
||||
let history = vec![msg("aaaaaa"), msg("bbbbbb"), msg("cccccc"), msg("dddddd")];
|
||||
let records = vec![record(1, 50), record(4, 400)];
|
||||
let cut = split_for_retained_impl(&history, &records, 250);
|
||||
assert!(cut.index > 1 && cut.index <= 4);
|
||||
|
||||
@@ -1,17 +1,15 @@
|
||||
use std::pin::Pin;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use futures::Stream;
|
||||
use llm_worker::Worker;
|
||||
use llm_worker::llm_client::event::{Event as LlmEvent, ResponseStatus, StatusEvent};
|
||||
use llm_worker::llm_client::{ClientError, LlmClient, Request};
|
||||
use llm_worker::Worker;
|
||||
use session_store::FsStore;
|
||||
|
||||
use pod::{
|
||||
Event, Method, Pod, PodController, PodManifest, PodStatus,
|
||||
};
|
||||
use pod::{Event, Method, Pod, PodController, PodManifest, PodStatus};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Mock LLM Client
|
||||
|
||||
Reference in New Issue
Block a user