refactor: split pod metadata store
This commit is contained in:
@@ -3,7 +3,6 @@
|
||||
//! Layout:
|
||||
//! - Segment log: `{root}/{session_id}/{segment_id}.jsonl`
|
||||
//! - Event trace: `{root}/{session_id}/{segment_id}.trace.jsonl`
|
||||
//! - Pod metadata: `{root}/pods/{pod_name}/metadata.json`
|
||||
//!
|
||||
//! The per-Session directory makes `list_segments(session_id)` an O(dir)
|
||||
//! scan and gives the fork tree a visible grouping in the filesystem.
|
||||
@@ -17,7 +16,6 @@
|
||||
//! enumerable by the picker.
|
||||
|
||||
use crate::event_trace::TraceEntry;
|
||||
use crate::pod_metadata::{PodMetadata, PodMetadataStore, validate_pod_name};
|
||||
use crate::segment_log::LogEntry;
|
||||
use crate::store::{Store, StoreError};
|
||||
use crate::{SegmentId, SessionId};
|
||||
@@ -57,19 +55,6 @@ impl FsStore {
|
||||
.join(format!("{segment_id}.trace.jsonl"))
|
||||
}
|
||||
|
||||
fn pods_dir(&self) -> PathBuf {
|
||||
self.root.join("pods")
|
||||
}
|
||||
|
||||
fn pod_dir(&self, pod_name: &str) -> Result<PathBuf, StoreError> {
|
||||
validate_pod_name(pod_name)?;
|
||||
Ok(self.pods_dir().join(pod_name))
|
||||
}
|
||||
|
||||
fn pod_metadata_path(&self, pod_name: &str) -> Result<PathBuf, StoreError> {
|
||||
Ok(self.pod_dir(pod_name)?.join("metadata.json"))
|
||||
}
|
||||
|
||||
fn append_line(&self, path: &Path, line: &str) -> Result<(), StoreError> {
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent)?;
|
||||
@@ -102,70 +87,6 @@ impl FsStore {
|
||||
}
|
||||
}
|
||||
|
||||
impl PodMetadataStore for FsStore {
|
||||
fn write(&self, metadata: &PodMetadata) -> Result<(), StoreError> {
|
||||
let path = self.pod_metadata_path(&metadata.pod_name)?;
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent)?;
|
||||
}
|
||||
let content = serde_json::to_vec_pretty(metadata)?;
|
||||
fs::write(path, content)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_by_name(&self, pod_name: &str) -> Result<Option<PodMetadata>, StoreError> {
|
||||
let path = self.pod_metadata_path(pod_name)?;
|
||||
let content = match fs::read_to_string(path) {
|
||||
Ok(content) => content,
|
||||
Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(None),
|
||||
Err(err) => return Err(StoreError::Io(err)),
|
||||
};
|
||||
Ok(Some(serde_json::from_str(&content)?))
|
||||
}
|
||||
|
||||
fn list_names(&self) -> Result<Vec<String>, StoreError> {
|
||||
let dir = self.pods_dir();
|
||||
let mut names = Vec::new();
|
||||
if !dir.exists() {
|
||||
return Ok(names);
|
||||
}
|
||||
for entry in fs::read_dir(dir)? {
|
||||
let entry = entry?;
|
||||
if !entry.file_type()?.is_dir() {
|
||||
continue;
|
||||
}
|
||||
if !entry.path().join("metadata.json").exists() {
|
||||
continue;
|
||||
}
|
||||
let Some(name) = entry.file_name().to_str().map(ToOwned::to_owned) else {
|
||||
continue;
|
||||
};
|
||||
if validate_pod_name(&name).is_ok() {
|
||||
names.push(name);
|
||||
}
|
||||
}
|
||||
names.sort();
|
||||
Ok(names)
|
||||
}
|
||||
|
||||
fn root_dir(&self) -> Option<PathBuf> {
|
||||
Some(self.root.clone())
|
||||
}
|
||||
|
||||
fn delete_by_name(&self, pod_name: &str) -> Result<(), StoreError> {
|
||||
let path = self.pod_metadata_path(pod_name)?;
|
||||
match fs::remove_file(&path) {
|
||||
Ok(()) => {}
|
||||
Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(()),
|
||||
Err(err) => return Err(StoreError::Io(err)),
|
||||
}
|
||||
if let Some(parent) = path.parent() {
|
||||
let _ = fs::remove_dir(parent);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Store for FsStore {
|
||||
fn append(
|
||||
&self,
|
||||
|
||||
@@ -33,7 +33,6 @@
|
||||
pub mod event_trace;
|
||||
pub mod fs_store;
|
||||
pub mod logged_item;
|
||||
pub mod pod_metadata;
|
||||
pub mod segment;
|
||||
pub mod segment_log;
|
||||
pub mod store;
|
||||
@@ -44,9 +43,6 @@ pub use fs_store::FsStore;
|
||||
pub use llm_worker::UsageRecord;
|
||||
pub use llm_worker::llm_client::types::{ContentPart, Item, Role};
|
||||
pub use logged_item::{LoggedContentPart, LoggedItem, LoggedRole, from_logged, to_logged};
|
||||
pub use pod_metadata::{
|
||||
PodActiveSegmentRef, PodMetadata, PodMetadataStore, PodSpawnedChild, PodSpawnedScopeRule,
|
||||
};
|
||||
pub use segment::{
|
||||
SegmentStartState, append_entry, append_system_item, classify_history_item,
|
||||
create_compacted_segment, create_segment, create_segment_with_ids, ensure_head_or_fork, fork,
|
||||
|
||||
@@ -1,150 +0,0 @@
|
||||
//! Pod metadata persistence API.
|
||||
//!
|
||||
//! Pod metadata is a lightweight name-keyed pointer to the Session/Segment
|
||||
//! currently active for a Pod. Conversation content remains in the segment log;
|
||||
//! this metadata only records references needed by Pod-name resume/attach flows.
|
||||
|
||||
use crate::store::StoreError;
|
||||
use crate::{SegmentId, SessionId};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// Active Session/Segment pointer for a Pod.
|
||||
///
|
||||
/// `segment_id` is optional so callers can persist a reserved Session before
|
||||
/// the first Segment ID is known. Once a segment exists, callers should rewrite
|
||||
/// the metadata with `Some(segment_id)`.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct PodActiveSegmentRef {
|
||||
pub session_id: SessionId,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub segment_id: Option<SegmentId>,
|
||||
}
|
||||
|
||||
impl PodActiveSegmentRef {
|
||||
/// Create a reference whose active Segment is not known yet.
|
||||
pub fn pending_segment(session_id: SessionId) -> Self {
|
||||
Self {
|
||||
session_id,
|
||||
segment_id: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a fully resolved active Session/Segment reference.
|
||||
pub fn active_segment(session_id: SessionId, segment_id: SegmentId) -> Self {
|
||||
Self {
|
||||
session_id,
|
||||
segment_id: Some(segment_id),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One delegated scope rule for a spawned child, kept local to
|
||||
/// `session-store` so the persistence crate does not depend on manifest
|
||||
/// scope types.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct PodSpawnedScopeRule {
|
||||
pub target: PathBuf,
|
||||
pub permission: String,
|
||||
pub recursive: bool,
|
||||
}
|
||||
|
||||
/// One child Pod spawned by this Pod and persisted with the spawner's
|
||||
/// name-keyed Pod state.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct PodSpawnedChild {
|
||||
pub pod_name: String,
|
||||
pub socket_path: PathBuf,
|
||||
pub scope_delegated: Vec<PodSpawnedScopeRule>,
|
||||
pub callback_address: PathBuf,
|
||||
}
|
||||
|
||||
/// Persistent metadata for a Pod name.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct PodMetadata {
|
||||
pub pod_name: String,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub active: Option<PodActiveSegmentRef>,
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
pub spawned_children: Vec<PodSpawnedChild>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub resolved_manifest_snapshot: Option<serde_json::Value>,
|
||||
}
|
||||
|
||||
impl PodMetadata {
|
||||
/// Create Pod metadata for `pod_name`.
|
||||
pub fn new(pod_name: impl Into<String>, active: Option<PodActiveSegmentRef>) -> Self {
|
||||
Self {
|
||||
pod_name: pod_name.into(),
|
||||
active,
|
||||
spawned_children: Vec::new(),
|
||||
resolved_manifest_snapshot: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Sync persistence backend for Pod metadata.
|
||||
///
|
||||
/// The key is the Pod name. Missing state is not an error: `read_by_name`
|
||||
/// returns `Ok(None)` for Pods that have never persisted metadata or whose
|
||||
/// metadata was deleted.
|
||||
pub trait PodMetadataStore: Send + Sync {
|
||||
/// Create or replace metadata for its `pod_name` key.
|
||||
fn write(&self, metadata: &PodMetadata) -> Result<(), StoreError>;
|
||||
|
||||
/// Read metadata by Pod name. Returns `None` when no metadata exists.
|
||||
fn read_by_name(&self, pod_name: &str) -> Result<Option<PodMetadata>, StoreError>;
|
||||
|
||||
/// List persisted Pod metadata keys. Implementations return names only;
|
||||
/// callers can then read each item independently so a corrupt metadata
|
||||
/// file does not make the whole discovery result fail.
|
||||
fn list_names(&self) -> Result<Vec<String>, StoreError>;
|
||||
|
||||
/// Return the metadata root directory when this backend is path-backed.
|
||||
fn root_dir(&self) -> Option<PathBuf> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Delete metadata by Pod name. Missing metadata is a successful no-op.
|
||||
fn delete_by_name(&self, pod_name: &str) -> Result<(), StoreError>;
|
||||
}
|
||||
|
||||
pub(crate) fn validate_pod_name(pod_name: &str) -> Result<(), StoreError> {
|
||||
if pod_name.is_empty()
|
||||
|| pod_name == "."
|
||||
|| pod_name == ".."
|
||||
|| pod_name.contains('/')
|
||||
|| pod_name.contains('\0')
|
||||
{
|
||||
return Err(StoreError::InvalidPodName(pod_name.to_string()));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn pod_metadata_manifest_snapshot_roundtrips() {
|
||||
let mut metadata = PodMetadata::new(
|
||||
"profile-pod",
|
||||
Some(PodActiveSegmentRef::pending_segment(crate::new_session_id())),
|
||||
);
|
||||
metadata.resolved_manifest_snapshot = Some(serde_json::json!({
|
||||
"pod": { "name": "profile-pod" },
|
||||
"profile": {
|
||||
"source": { "kind": "path", "path": "/profiles/coder.nix" }
|
||||
}
|
||||
}));
|
||||
|
||||
let json = serde_json::to_string(&metadata).unwrap();
|
||||
let restored: PodMetadata = serde_json::from_str(&json).unwrap();
|
||||
|
||||
assert_eq!(restored, metadata);
|
||||
assert_eq!(
|
||||
restored.resolved_manifest_snapshot.as_ref().unwrap()["profile"]["source"]["kind"],
|
||||
"path"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -29,9 +29,6 @@ pub enum StoreError {
|
||||
|
||||
#[error("log corrupted at line {line}: {message}")]
|
||||
Corrupt { line: usize, message: String },
|
||||
|
||||
#[error("invalid pod name: {0}")]
|
||||
InvalidPodName(String),
|
||||
}
|
||||
|
||||
/// Sync persistence backend for segment logs.
|
||||
|
||||
Reference in New Issue
Block a user