Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(workstream): match Claude project folder encoding

AkitaOnRailsadd6e54b256e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2b0625d3..c05534db 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,9 @@ version in [`docs/changelog/`](docs/changelog/):
### Fixed
+- Matched Claude Code's UTF-16 project-folder encoding for astral characters
+ and its hashed 200-character limit, so transcript discovery and token-usage
+ backfill find sessions from emoji and very long checkout paths (#1864).
- Claude Code backfill-close now finds transcripts for project paths that
contain dots, underscores, spaces, or non-ASCII characters, so an abandoned
session's token usage is retained when its synthetic `session-end` is
diff --git a/crates/ai-memory-cli/src/commands/session_usage.rs b/crates/ai-memory-cli/src/commands/session_usage.rs
index f8103c3a..ad6441b0 100644
--- a/crates/ai-memory-cli/src/commands/session_usage.rs
+++ b/crates/ai-memory-cli/src/commands/session_usage.rs
@@ -1016,6 +1016,7 @@ mod tests {
("/home/me/my_app", "-home-me-my-app"),
("/home/me/my repo", "-home-me-my-repo"),
("/home/me/café", "-home-me-caf-"),
+ ("/home/me/😀proj", "-home-me---proj"),
(r"C:\Users\me\my.repo", "C--Users-me-my-repo"),
] {
let project_dir = tmp.path().join("projects").join(folder);
diff --git a/crates/ai-memory-workstream/src/transcript.rs b/crates/ai-memory-workstream/src/transcript.rs
index 4c33d95a..0d226231 100644
--- a/crates/ai-memory-workstream/src/transcript.rs
+++ b/crates/ai-memory-workstream/src/transcript.rs
@@ -3871,15 +3871,44 @@ fn names_session(path: &Path, id: &str) -> bool {
.any(|component| component.as_os_str().to_string_lossy().contains(id))
}
-/// The folder Claude Code keeps a project's transcripts in: the cwd with
-/// every character outside `[A-Za-z0-9]` replaced by `-`, so `/a/b.c_d` is
-/// `-a-b-c-d` and `C:\\Users\\me` is `C--Users-me`.
+/// The folder Claude Code keeps a project's transcripts in.
+///
+/// Claude replaces each non-ASCII-alphanumeric UTF-16 code unit with `-`.
+/// Names longer than 200 units are truncated and suffixed with the base-36
+/// absolute value of Claude's wrapping 31-based hash of the original cwd.
#[must_use]
pub fn claude_project_dir_name(cwd: &Path) -> String {
- cwd.to_string_lossy()
- .chars()
- .map(|c| if c.is_ascii_alphanumeric() { c } else { '-' })
- .collect()
+ let cwd = cwd.to_string_lossy();
+ let mut name = String::with_capacity(cwd.len());
+ for character in cwd.chars() {
+ if character.is_ascii_alphanumeric() {
+ name.push(character);
+ } else {
+ name.extend(std::iter::repeat_n('-', character.len_utf16()));
+ }
+ }
+
+ if name.len() <= 200 {
+ return name;
+ }
+
+ let hash = cwd.encode_utf16().fold(0_i32, |hash, unit| {
+ hash.wrapping_mul(31).wrapping_add(i32::from(unit))
+ });
+ format!("{}-{}", &name[..200], base36(hash.unsigned_abs()))
+}
+
+fn base36(mut value: u32) -> String {
+ const DIGITS: &[u8; 36] = b"0123456789abcdefghijklmnopqrstuvwxyz";
+ let mut reversed = Vec::with_capacity(7);
+ loop {
+ reversed.push(char::from(DIGITS[(value % 36) as usize]));
+ value /= 36;
+ if value == 0 {
+ break;
+ }
+ }
+ reversed.into_iter().rev().collect()
}
fn session_path_matches(
@@ -7837,6 +7866,42 @@ mod tests {
}
}
+ #[test]
+ fn claude_project_dir_name_matches_utf16_and_long_path_rules() {
+ let long_ascii = format!(
+ "/home/me/{}/{}/{}",
+ "a".repeat(70),
+ "b".repeat(70),
+ "c".repeat(70)
+ );
+ let long_unicode = format!(
+ "/home/me/café/{}/{}/{}/😀",
+ "a".repeat(70),
+ "b".repeat(70),
+ "c".repeat(70)
+ );
+ let min_hash = format!(
+ "/home/me/{}/{}/{}/xhzwmkfb",
+ "a".repeat(70),
+ "b".repeat(70),
+ "c".repeat(70)
+ );
+
+ for (cwd, suffix) in [
+ (long_ascii.as_str(), "gbinw6"),
+ (long_unicode.as_str(), "11e44m"),
+ (min_hash.as_str(), "zik0zk"),
+ ] {
+ let folder = claude_project_dir_name(Path::new(cwd));
+ assert_eq!(folder.len(), 207, "{cwd}");
+ assert_eq!(&folder[200..], format!("-{suffix}"), "{cwd}");
+ }
+ assert_eq!(
+ claude_project_dir_name(Path::new("/home/me/😀proj")),
+ "-home-me---proj"
+ );
+ }
+
/// #1167: a cwd with `.` or `_` maps to a folder the old `/`-only rule
/// missed; the transcript is found there, a folder whose name does not
/// derive from the cwd is still probed, and neither a subagent sidechain