podのモジュール分割

This commit is contained in:
2026-04-24 11:48:27 +09:00
parent 30f9abacb8
commit 4763173f36
35 changed files with 238 additions and 94 deletions
+5
View File
@@ -0,0 +1,5 @@
pub(crate) mod prune;
pub(crate) mod state;
pub(crate) mod token_counter;
pub(crate) mod usage_tracker;
pub(crate) mod worker;
@@ -12,7 +12,7 @@ use llm_worker::prune::{PruneConfig, SavingsEstimator};
use session_store::Store;
use crate::Pod;
use crate::token_counter::{EstimateSource, savings_for_prune_impl};
use crate::compact::token_counter::{EstimateSource, savings_for_prune_impl};
impl<C: LlmClient, St: Store> Pod<C, St> {
/// Enable prune projection on the underlying Worker.
+14 -14
View File
@@ -6,17 +6,17 @@ use llm_worker::llm_client::client::LlmClient;
use session_store::Store;
use tokio::sync::{broadcast, mpsc, oneshot};
use crate::notification_buffer::NotificationBuffer;
use crate::notifier::Notifier;
use crate::ipc::notification_buffer::NotificationBuffer;
use crate::ipc::notifier::Notifier;
use crate::pod::{Pod, PodError, PodRunResult};
use crate::pod_comm_tools::{
use crate::spawn::comm_tools::{
list_pods_tool, read_pod_output_tool, send_to_pod_tool, stop_pod_tool,
};
use crate::runtime_dir::RuntimeDir;
use crate::runtime::dir::RuntimeDir;
use crate::shared_state::{PodSharedState, PodStatus};
use crate::socket_server::SocketServer;
use crate::spawn_pod::spawn_pod_tool;
use crate::spawned_pod_registry::SpawnedPodRegistry;
use crate::ipc::server::SocketServer;
use crate::spawn::tool::spawn_pod_tool;
use crate::spawn::registry::SpawnedPodRegistry;
use protocol::{ErrorCode, Event, Method, NotificationLevel, NotificationSource, RunResult, TurnResult};
// ---------------------------------------------------------------------------
@@ -463,7 +463,7 @@ impl PodController {
// (1) system side effects — idempotent and
// tolerant of out-of-order delivery (e.g.
// `TurnEnded` arriving after `ShutDown`).
crate::pod_events::apply_event_side_effects(
crate::ipc::event::apply_event_side_effects(
&event,
&spawned_registry,
&spawner_name,
@@ -474,7 +474,7 @@ impl PodController {
// into the notification buffer; the next LLM
// request will inject it as a system message
// via `PodInterceptor::pre_llm_request`.
let text = crate::pod_events::render_event(&event);
let text = crate::ipc::event::render_event(&event);
pod.push_notification(text);
// Auto-kick a turn if the Pod is idle so the
// notification is not stranded. Matches the
@@ -529,7 +529,7 @@ impl PodController {
// `connect_and_send` helper enforces a 5 s timeout so a
// stuck parent cannot block process exit indefinitely.
if let Some(parent) = self_parent_socket.as_ref() {
if let Err(e) = crate::pod_events::send_pod_event(
if let Err(e) = crate::ipc::event::send_pod_event(
parent,
protocol::PodEvent::ShutDown {
pod_name: spawner_name.clone(),
@@ -587,7 +587,7 @@ where
};
let _ = event_tx.send(Event::RunEnd { result: run_result });
if matches!(run_result, RunResult::Finished) {
crate::pod_events::fire_and_forget(
crate::ipc::event::fire_and_forget(
parent_socket.cloned(),
protocol::PodEvent::TurnEnded {
pod_name: self_name.to_string(),
@@ -612,7 +612,7 @@ where
code,
message: message.clone(),
});
crate::pod_events::fire_and_forget(
crate::ipc::event::fire_and_forget(
parent_socket.cloned(),
protocol::PodEvent::Errored {
pod_name: self_name.to_string(),
@@ -657,14 +657,14 @@ where
// notification buffer so the in-flight turn's
// next `pre_llm_request` surfaces it.
let self_parent_socket = parent_socket.cloned();
crate::pod_events::apply_event_side_effects(
crate::ipc::event::apply_event_side_effects(
&event,
spawned_registry,
self_name,
&self_parent_socket,
)
.await;
notification_buffer.push(crate::pod_events::render_event(&event));
notification_buffer.push(crate::ipc::event::render_event(&event));
}
None => {
let _ = cancel_tx.try_send(());
+3 -3
View File
@@ -29,7 +29,7 @@ use std::path::{Path, PathBuf};
use manifest::{PodManifest, PodManifestConfig, ResolveError};
use crate::prompt_loader::PromptLoader;
use crate::prompt::loader::PromptLoader;
/// Errors raised while building a [`PodManifest`] from cascade layers.
#[derive(Debug, thiserror::Error)]
@@ -614,7 +614,7 @@ permission = "write"
#[test]
fn resolve_produces_loader_with_workspace_prompts_dir() {
use crate::system_prompt::{SystemPromptContext, SystemPromptTemplate};
use crate::prompt::system::{SystemPromptContext, SystemPromptTemplate};
use manifest::{Permission, Scope, ScopeConfig, ScopeRule};
let tmp = TempDir::new().unwrap();
@@ -664,7 +664,7 @@ permission = "write"
deny: Vec::new(),
};
let scope = Scope::from_config(&scope_cfg).unwrap();
let catalog = crate::prompts::PromptCatalog::builtins_only().unwrap();
let catalog = crate::prompt::catalog::PromptCatalog::builtins_only().unwrap();
let ctx = SystemPromptContext {
now: chrono::Utc::now(),
cwd: &root,
+1 -1
View File
@@ -15,7 +15,7 @@ use session_store::Store;
use crate::pod::{Pod, PodError, PodRunResult};
#[cfg(test)]
use crate::prompts::PromptCatalog;
use crate::prompt::catalog::PromptCatalog;
impl<C: LlmClient, St: Store> Pod<C, St> {
/// Close out the current (paused) turn and start a new one with `input`.
@@ -26,10 +26,10 @@ use std::sync::Arc;
use protocol::{Method, PodEvent, ScopeRule};
use crate::pod_comm_tools::connect_and_send;
use crate::runtime_dir::SpawnedPodRecord;
use crate::scope_lock::{self, ScopeLockError};
use crate::spawned_pod_registry::SpawnedPodRegistry;
use crate::spawn::comm_tools::connect_and_send;
use crate::runtime::dir::SpawnedPodRecord;
use crate::runtime::scope_lock::{self, ScopeLockError};
use crate::spawn::registry::SpawnedPodRegistry;
/// Connect to `socket`, send a single `Method::PodEvent(event)`, and
/// return. Used by children to report up to their parent.
@@ -20,14 +20,14 @@ use llm_worker::tool::ToolOutput;
use session_store::UsageRecord;
use tracing::info;
use crate::compact_state::CompactState;
use crate::compact::state::CompactState;
use crate::hook::{
AbortInfo, HookRegistry, PreRequestInfo, PromptSubmitInfo, ToolCallSummary, ToolResultSummary,
TurnEndInfo,
};
use crate::notification_buffer::{NotificationBuffer, format_notification};
use crate::prompts::PromptCatalog;
use crate::token_counter::total_tokens_impl;
use crate::ipc::notification_buffer::{NotificationBuffer, format_notification};
use crate::prompt::catalog::PromptCatalog;
use crate::compact::token_counter::total_tokens_impl;
use tracing::warn;
/// Maximum number of bytes copied into `TurnEndInfo::final_text_preview`.
+6
View File
@@ -0,0 +1,6 @@
pub mod event;
pub mod notifier;
pub mod server;
pub(crate) mod interceptor;
pub(crate) mod notification_buffer;
@@ -11,7 +11,7 @@ use std::sync::{Arc, Mutex};
use llm_worker::Item;
use tracing::warn;
use crate::prompts::{CatalogError, PromptCatalog};
use crate::prompt::catalog::{CatalogError, PromptCatalog};
/// Maximum queued notifications. Oldest entries are dropped beyond this.
const CAPACITY: usize = 128;
+12 -27
View File
@@ -1,45 +1,30 @@
pub mod compact;
pub mod controller;
pub mod hook;
pub mod notifier;
pub mod runtime_dir;
pub mod scope_lock;
pub mod ipc;
pub mod prompt;
pub mod runtime;
pub mod shared_state;
pub mod pod_comm_tools;
pub mod pod_events;
pub mod socket_server;
pub mod spawn_pod;
pub mod spawned_pod_registry;
pub mod spawn;
mod agents_md;
mod compact_state;
mod compact_worker;
mod factory;
mod interrupt_and_run;
mod notification_buffer;
mod pod;
mod pod_interceptor;
mod prompt_loader;
mod prompts;
mod prune;
mod system_prompt;
mod token_counter;
mod usage_tracker;
pub use token_counter::{EstimateSource, SplitPoint, TokenEstimate};
pub use compact::token_counter::{EstimateSource, SplitPoint, TokenEstimate};
pub use controller::{PodController, PodHandle, ShutdownReceiver};
pub use factory::{FactoryError, PodFactory};
pub use notifier::Notifier;
pub use hook::{Hook, HookEventKind, HookRegistryBuilder};
pub use ipc::notifier::Notifier;
pub use ipc::server::SocketServer;
pub use manifest::{
AuthRef, ModelManifest, PodManifest, PodManifestConfig, PodMetaConfig, Scope, SchemeKind,
};
pub use pod::{Pod, PodError, PodRunResult, apply_worker_manifest};
pub use prompt_loader::PromptLoader;
pub use prompts::{CatalogError, PodPrompt, PromptCatalog};
pub use prompt::catalog::{CatalogError, PodPrompt, PromptCatalog};
pub use prompt::loader::PromptLoader;
pub use prompt::system::{SystemPromptContext, SystemPromptError, SystemPromptTemplate};
pub use protocol::{ErrorCode, Event, Method, TurnResult};
pub use provider::{ProviderError, build_client};
pub use runtime_dir::RuntimeDir;
pub use runtime::dir::RuntimeDir;
pub use shared_state::{PodSharedState, PodStatus};
pub use socket_server::SocketServer;
pub use system_prompt::{SystemPromptContext, SystemPromptError, SystemPromptTemplate};
+13 -13
View File
@@ -13,21 +13,21 @@ use tracing::{info, warn};
use manifest::{PodManifest, PodManifestConfig, ResolveError, Scope, ScopeError, WorkerManifest};
use crate::agents_md::read_agents_md;
use crate::compact_state::CompactState;
use crate::prompt::agents_md::read_agents_md;
use crate::compact::state::CompactState;
use crate::hook::{
Hook, HookRegistryBuilder, OnAbort, OnPromptSubmit, OnTurnEnd, PostToolCall, PreLlmRequest,
PreRequestInfo, PreToolCall,
};
use crate::notification_buffer::NotificationBuffer;
use crate::notifier::Notifier;
use crate::pod_interceptor::PodInterceptor;
use crate::prompt_loader::PromptLoader;
use crate::prompts::{CatalogError, PromptCatalog};
use crate::runtime_dir;
use crate::scope_lock::{self, ScopeAllocationGuard, ScopeLockError};
use crate::system_prompt::{SystemPromptContext, SystemPromptError, SystemPromptTemplate};
use crate::usage_tracker::UsageTracker;
use crate::ipc::notification_buffer::NotificationBuffer;
use crate::ipc::notifier::Notifier;
use crate::ipc::interceptor::PodInterceptor;
use crate::prompt::loader::PromptLoader;
use crate::prompt::catalog::{CatalogError, PromptCatalog};
use crate::runtime::dir;
use crate::runtime::scope_lock::{self, ScopeAllocationGuard, ScopeLockError};
use crate::prompt::system::{SystemPromptContext, SystemPromptError, SystemPromptTemplate};
use crate::compact::usage_tracker::UsageTracker;
use protocol::{Event, NotificationLevel, NotificationSource};
use tokio::sync::broadcast;
use async_trait::async_trait;
@@ -848,7 +848,7 @@ impl<C: LlmClient, St: Store> Pod<C, St> {
pub async fn compact(&mut self, retained_tokens: u64) -> Result<SessionId, PodError> {
use std::sync::atomic::{AtomicU64, Ordering};
use crate::compact_worker::{
use crate::compact::worker::{
CompactWorkerContext, CompactWorkerInterceptor, add_reference_tool,
mark_read_required_tool, slice_lines, write_summary_tool,
};
@@ -1125,7 +1125,7 @@ impl<St: Store> Pod<Box<dyn LlmClient>, St> {
// Register this Pod in the machine-wide scope-lock registry
// before building anything else, so a spawn that conflicts on
// scope fails fast (and without having paid for client setup).
let socket_path = runtime_dir::default_base()
let socket_path = dir::default_base()
.map_err(ScopeLockError::from)?
.join(&manifest.pod.name)
.join("sock");
@@ -45,13 +45,13 @@ use serde::Deserialize;
use thiserror::Error;
use tracing::warn;
use crate::prompt_loader::PromptLoader;
use crate::prompt::loader::PromptLoader;
// Generated by build.rs from `resources/prompts/internal.toml`.
include!(concat!(env!("OUT_DIR"), "/internal_keys.rs"));
/// Source of the builtin pack. Baked in at compile time.
const INTERNAL_TOML: &str = include_str!("../../../resources/prompts/internal.toml");
const INTERNAL_TOML: &str = include_str!("../../../../resources/prompts/internal.toml");
/// Pod-level prompt injection point.
///
@@ -104,7 +104,7 @@ pub enum LoaderError {
/// libraries. Cheap to clone.
///
/// Also carries the auto-discovered `prompts.toml` pack file paths so
/// [`crate::prompts::PromptCatalog`] can read the same user/workspace
/// [`crate::prompt::catalog::PromptCatalog`] can read the same user/workspace
/// layers without a separate plumbing channel. These fields do not
/// affect `$prefix` asset resolution — they are purely metadata
/// consulted by the catalog loader.
+4
View File
@@ -0,0 +1,4 @@
pub(crate) mod agents_md;
pub(crate) mod catalog;
pub(crate) mod loader;
pub(crate) mod system;
@@ -22,8 +22,8 @@ use minijinja::value::Value;
use minijinja::{Environment, ErrorKind, UndefinedBehavior};
use thiserror::Error;
use crate::prompt_loader::{LoaderError, PromptLoader, PromptRef};
use crate::prompts::{CatalogError, PromptCatalog};
use crate::prompt::loader::{LoaderError, PromptLoader, PromptRef};
use crate::prompt::catalog::{CatalogError, PromptCatalog};
#[derive(Debug, Error)]
pub enum SystemPromptError {
+2
View File
@@ -0,0 +1,2 @@
pub mod dir;
pub mod scope_lock;
@@ -21,9 +21,9 @@ use protocol::{ErrorCode, Event, Method};
use serde::Deserialize;
use tokio::net::UnixStream;
use crate::runtime_dir::SpawnedPodRecord;
use crate::scope_lock::{self, LockFileGuard};
use crate::spawned_pod_registry::SpawnedPodRegistry;
use crate::runtime::dir::SpawnedPodRecord;
use crate::runtime::scope_lock::{self, LockFileGuard};
use crate::spawn::registry::SpawnedPodRegistry;
/// Timeout applied to each socket-level operation — connect, write,
/// read. Kept short so a stuck child doesn't block the spawner's turn.
+3
View File
@@ -0,0 +1,3 @@
pub mod comm_tools;
pub mod registry;
pub mod tool;
@@ -20,7 +20,7 @@ use std::sync::Arc;
use tokio::sync::Mutex;
use crate::runtime_dir::{RuntimeDir, SpawnedPodRecord};
use crate::runtime::dir::{RuntimeDir, SpawnedPodRecord};
pub struct SpawnedPodRegistry {
records: Mutex<Vec<SpawnedPodRecord>>,
@@ -24,10 +24,10 @@ use tokio::net::UnixStream;
use tokio::process::Command;
use tokio::time::sleep;
use crate::pod_events;
use crate::runtime_dir::SpawnedPodRecord;
use crate::scope_lock::{self, LockFileGuard, ScopeLockError};
use crate::spawned_pod_registry::SpawnedPodRegistry;
use crate::ipc::event;
use crate::runtime::dir::SpawnedPodRecord;
use crate::runtime::scope_lock::{self, LockFileGuard, ScopeLockError};
use crate::spawn::registry::SpawnedPodRegistry;
use protocol::PodEvent;
const DESCRIPTION: &str = "Spawn a new Pod process to work on a delegated task. \
@@ -233,7 +233,7 @@ impl Tool for SpawnPodTool {
// Notify this Pod's own parent so the grandparent can register
// the new grandchild directly. Fire-and-forget; top-level Pods
// (with no parent) skip the send inside `fire_and_forget`.
pod_events::fire_and_forget(
event::fire_and_forget(
self.parent_socket.clone(),
PodEvent::ScopeSubDelegated {
parent_pod: self.spawner_name.clone(),
+4 -4
View File
@@ -14,12 +14,12 @@ use std::sync::{Arc, LazyLock, Mutex};
use llm_worker::llm_client::types::{ContentPart, Item, Role};
use llm_worker::tool::ToolOutput;
use manifest::{Permission, ScopeRule};
use pod::pod_comm_tools::{
use pod::spawn::comm_tools::{
list_pods_tool, read_pod_output_tool, send_to_pod_tool, stop_pod_tool,
};
use pod::runtime_dir::{RuntimeDir, SpawnedPodRecord};
use pod::scope_lock::{self, LockFileGuard};
use pod::spawned_pod_registry::SpawnedPodRegistry;
use pod::runtime::dir::{RuntimeDir, SpawnedPodRecord};
use pod::runtime::scope_lock::{self, LockFileGuard};
use pod::spawn::registry::SpawnedPodRegistry;
use protocol::stream::{JsonLineReader, JsonLineWriter};
use protocol::{ErrorCode, Event, Greeting, Method};
use serde_json::json;
+4 -4
View File
@@ -9,10 +9,10 @@ use std::path::PathBuf;
use std::sync::{Arc, LazyLock, Mutex};
use std::time::Duration;
use pod::pod_events::{apply_event_side_effects, fire_and_forget, render_event};
use pod::runtime_dir::{RuntimeDir, SpawnedPodRecord};
use pod::scope_lock::{self, LockFileGuard};
use pod::spawned_pod_registry::SpawnedPodRegistry;
use pod::ipc::event::{apply_event_side_effects, fire_and_forget, render_event};
use pod::runtime::dir::{RuntimeDir, SpawnedPodRecord};
use pod::runtime::scope_lock::{self, LockFileGuard};
use pod::spawn::registry::SpawnedPodRegistry;
use protocol::stream::JsonLineReader;
use protocol::{Method, Permission, PodEvent, ScopeRule};
use tempfile::TempDir;
+4 -4
View File
@@ -12,10 +12,10 @@ use std::sync::{LazyLock, Mutex};
use llm_worker::tool::{ToolError, ToolOutput};
use manifest::{AuthRef, ModelManifest, Permission, SchemeKind, ScopeRule};
use pod::runtime_dir::{RuntimeDir, SpawnedPodRecord};
use pod::scope_lock::{self, LockFileGuard};
use pod::spawn_pod::spawn_pod_tool;
use pod::spawned_pod_registry::SpawnedPodRegistry;
use pod::runtime::dir::{RuntimeDir, SpawnedPodRecord};
use pod::runtime::scope_lock::{self, LockFileGuard};
use pod::spawn::tool::spawn_pod_tool;
use pod::spawn::registry::SpawnedPodRegistry;
use protocol::Method;
use protocol::stream::JsonLineReader;
use serde_json::json;