Skip to content

Commit 50aea4b

Browse files
authored
nit: memory storage (#11924)
1 parent e415369 commit 50aea4b

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

codex-rs/core/src/memories/storage.rs

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use codex_state::Stage1Output;
2-
use std::collections::BTreeSet;
2+
use std::collections::HashSet;
33
use std::fmt::Write as _;
44
use std::path::Path;
55
use tracing::warn;
@@ -8,8 +8,6 @@ use crate::memories::ensure_layout;
88
use crate::memories::raw_memories_file;
99
use crate::memories::rollout_summaries_dir;
1010

11-
//TODO(jif) clean.
12-
1311
/// Rebuild `raw_memories.md` from DB-backed stage-1 outputs.
1412
pub(super) async fn rebuild_raw_memories_file_from_memories(
1513
root: &Path,
@@ -28,17 +26,14 @@ pub(super) async fn sync_rollout_summaries_from_memories(
2826
) -> std::io::Result<()> {
2927
ensure_layout(root).await?;
3028

31-
let retained = memories
32-
.iter()
33-
.take(max_raw_memories_for_global)
34-
.collect::<Vec<_>>();
29+
let retained = retained_memories(memories, max_raw_memories_for_global);
3530
let keep = retained
3631
.iter()
37-
.map(|memory| rollout_summary_file_stem(memory))
38-
.collect::<BTreeSet<_>>();
32+
.map(rollout_summary_file_stem)
33+
.collect::<HashSet<_>>();
3934
prune_rollout_summaries(root, &keep).await?;
4035

41-
for memory in &retained {
36+
for memory in retained {
4237
write_rollout_summary_for_thread(root, memory).await?;
4338
}
4439

@@ -68,10 +63,7 @@ async fn rebuild_raw_memories_file(
6863
memories: &[Stage1Output],
6964
max_raw_memories_for_global: usize,
7065
) -> std::io::Result<()> {
71-
let retained = memories
72-
.iter()
73-
.take(max_raw_memories_for_global)
74-
.collect::<Vec<_>>();
66+
let retained = retained_memories(memories, max_raw_memories_for_global);
7567
let mut body = String::from("# Raw Memories\n\n");
7668

7769
if retained.is_empty() {
@@ -81,26 +73,23 @@ async fn rebuild_raw_memories_file(
8173

8274
body.push_str("Merged stage-1 raw memories (latest first):\n\n");
8375
for memory in retained {
84-
writeln!(body, "## Thread `{}`", memory.thread_id)
85-
.map_err(|err| std::io::Error::other(format!("format raw memories: {err}")))?;
76+
writeln!(body, "## Thread `{}`", memory.thread_id).map_err(raw_memories_format_error)?;
8677
writeln!(
8778
body,
8879
"updated_at: {}",
8980
memory.source_updated_at.to_rfc3339()
9081
)
91-
.map_err(|err| std::io::Error::other(format!("format raw memories: {err}")))?;
92-
writeln!(body, "cwd: {}", memory.cwd.display())
93-
.map_err(|err| std::io::Error::other(format!("format raw memories: {err}")))?;
94-
writeln!(body)
95-
.map_err(|err| std::io::Error::other(format!("format raw memories: {err}")))?;
82+
.map_err(raw_memories_format_error)?;
83+
writeln!(body, "cwd: {}", memory.cwd.display()).map_err(raw_memories_format_error)?;
84+
writeln!(body).map_err(raw_memories_format_error)?;
9685
body.push_str(memory.raw_memory.trim());
9786
body.push_str("\n\n");
9887
}
9988

10089
tokio::fs::write(raw_memories_file(root), body).await
10190
}
10291

103-
async fn prune_rollout_summaries(root: &Path, keep: &BTreeSet<String>) -> std::io::Result<()> {
92+
async fn prune_rollout_summaries(root: &Path, keep: &HashSet<String>) -> std::io::Result<()> {
10493
let dir_path = rollout_summaries_dir(root);
10594
let mut dir = match tokio::fs::read_dir(&dir_path).await {
10695
Ok(dir) => dir,
@@ -138,24 +127,36 @@ async fn write_rollout_summary_for_thread(
138127
let path = rollout_summaries_dir(root).join(format!("{file_stem}.md"));
139128

140129
let mut body = String::new();
141-
writeln!(body, "thread_id: {}", memory.thread_id)
142-
.map_err(|err| std::io::Error::other(format!("format rollout summary: {err}")))?;
130+
writeln!(body, "thread_id: {}", memory.thread_id).map_err(rollout_summary_format_error)?;
143131
writeln!(
144132
body,
145133
"updated_at: {}",
146134
memory.source_updated_at.to_rfc3339()
147135
)
148-
.map_err(|err| std::io::Error::other(format!("format rollout summary: {err}")))?;
149-
writeln!(body, "cwd: {}", memory.cwd.display())
150-
.map_err(|err| std::io::Error::other(format!("format rollout summary: {err}")))?;
151-
writeln!(body)
152-
.map_err(|err| std::io::Error::other(format!("format rollout summary: {err}")))?;
136+
.map_err(rollout_summary_format_error)?;
137+
writeln!(body, "cwd: {}", memory.cwd.display()).map_err(rollout_summary_format_error)?;
138+
writeln!(body).map_err(rollout_summary_format_error)?;
153139
body.push_str(&memory.rollout_summary);
154140
body.push('\n');
155141

156142
tokio::fs::write(path, body).await
157143
}
158144

145+
fn retained_memories(
146+
memories: &[Stage1Output],
147+
max_raw_memories_for_global: usize,
148+
) -> &[Stage1Output] {
149+
&memories[..memories.len().min(max_raw_memories_for_global)]
150+
}
151+
152+
fn raw_memories_format_error(err: std::fmt::Error) -> std::io::Error {
153+
std::io::Error::other(format!("format raw memories: {err}"))
154+
}
155+
156+
fn rollout_summary_format_error(err: std::fmt::Error) -> std::io::Error {
157+
std::io::Error::other(format!("format rollout summary: {err}"))
158+
}
159+
159160
fn rollout_summary_file_stem(memory: &Stage1Output) -> String {
160161
const ROLLOUT_SLUG_MAX_LEN: usize = 20;
161162

0 commit comments

Comments
 (0)