Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(threads): tag permanent refusals with a stable PERMANENT: code

Akarshan Biswas4ab2a3e17c0b
diff --git a/src-tauri/src/core/threads/commands.rs b/src-tauri/src/core/threads/commands.rs
index d4a2d92dc..03dd6be71 100644
--- a/src-tauri/src/core/threads/commands.rs
+++ b/src-tauri/src/core/threads/commands.rs
@@ -13,7 +13,7 @@ use super::{
constants::THREADS_FILE,
utils::{
ensure_data_dirs, ensure_thread_dir_exists, get_data_dir, get_messages_path,
- get_thread_dir, get_thread_metadata_path, validate_thread_id,
+ get_thread_dir, get_thread_metadata_path, validate_thread_id, PERMANENT_PREFIX,
},
};
use crate::core::app::commands::get_jan_data_folder_path;
@@ -132,7 +132,7 @@ fn thread_extra_path(
name: &str,
) -> Result<std::path::PathBuf, String> {
if !THREAD_EXTRA_NAMES.contains(&name) {
- return Err(format!("Unsupported thread extra: {name:?}"));
+ return Err(format!("{PERMANENT_PREFIX}Unsupported thread extra: {name:?}"));
}
Ok(get_thread_dir(data_folder, thread_id)?.join(format!("{name}.json")))
}
@@ -169,7 +169,7 @@ fn ensure_uncommitted_dir_is_ours(
}
}
Err(format!(
- "Thread '{thread_id}' has messages but no thread.json; refusing to overwrite its messages"
+ "{PERMANENT_PREFIX}Thread '{thread_id}' has messages but no thread.json; refusing to overwrite its messages"
))
}
@@ -250,12 +250,14 @@ pub async fn replace_thread_messages<R: Runtime>(
let id = message
.get("id")
.and_then(|v| v.as_str())
- .ok_or("Every message needs a string id")?;
+ .ok_or_else(|| format!("{PERMANENT_PREFIX}Every message needs a string id"))?;
if !seen.insert(id) {
- return Err(format!("Duplicate message id {id:?}"));
+ return Err(format!("{PERMANENT_PREFIX}Duplicate message id {id:?}"));
}
if message.get("thread_id").and_then(|v| v.as_str()) != Some(thread_id.as_str()) {
- return Err(format!("Message {id:?} does not belong to thread {thread_id:?}"));
+ return Err(format!(
+ "{PERMANENT_PREFIX}Message {id:?} does not belong to thread {thread_id:?}"
+ ));
}
}
let data_folder = get_jan_data_folder_path(app_handle);
diff --git a/src-tauri/src/core/threads/tests.rs b/src-tauri/src/core/threads/tests.rs
index 233e5eb3c..06a2ee728 100644
--- a/src-tauri/src/core/threads/tests.rs
+++ b/src-tauri/src/core/threads/tests.rs
@@ -1398,3 +1398,54 @@ async fn test_write_thread_extra_create_refuses_a_foreign_log_without_thread_jso
.await
.unwrap();
}
+
+/// Refusals that retrying can never fix carry the stable `PERMANENT: ` prefix,
+/// so the Cowork migration classifies them by code, not by message text.
+#[tokio::test]
+async fn test_permanent_refusals_carry_the_stable_prefix() {
+ use super::utils::PERMANENT_PREFIX;
+ assert_eq!(PERMANENT_PREFIX, "PERMANENT: ");
+ let (app, _data_dir) = mock_app_with_temp_data_dir();
+ let handle = || app.handle().clone();
+
+ // Invalid id, through the validator and through a command.
+ assert!(validate_thread_id("a/b").unwrap_err().starts_with(PERMANENT_PREFIX));
+ assert!(validate_thread_id("").unwrap_err().starts_with(PERMANENT_PREFIX));
+ let err = write_thread_extra(handle(), "../x".into(), "cowork".into(), json!({}), Some(true))
+ .await
+ .unwrap_err();
+ assert!(err.starts_with(PERMANENT_PREFIX), "{err}");
+
+ // An unknown sidecar name.
+ let err = read_thread_extra(handle(), "ok-id".into(), "other".into())
+ .await
+ .unwrap_err();
+ assert!(err.starts_with(PERMANENT_PREFIX), "{err}");
+
+ // A directory holding someone's messages with no thread.json.
+ create_thread(handle(), json!({"id": "other-thread", "title": "t"}))
+ .await
+ .unwrap();
+ create_message(handle(), create_test_message("clash-code", "keep me"))
+ .await
+ .unwrap();
+ let batch = vec![migrated_message("clash-code", "clash-code-msg-0", "x")];
+ let err = replace_thread_messages(handle(), "clash-code".into(), batch, Some(true))
+ .await
+ .unwrap_err();
+ assert!(err.starts_with(PERMANENT_PREFIX), "{err}");
+
+ // A batch that can never be valid.
+ let bad = vec![migrated_message("someone-else", "m-0", "x")];
+ let err = replace_thread_messages(handle(), "mine".into(), bad, Some(true))
+ .await
+ .unwrap_err();
+ assert!(err.starts_with(PERMANENT_PREFIX), "{err}");
+
+ // An I/O style failure is not permanent: a write to a missing thread
+ // without `create` stays a plain message (retry after the thread exists).
+ let err = write_thread_extra(handle(), "ghost-2".into(), "cowork".into(), json!({}), None)
+ .await
+ .unwrap_err();
+ assert!(!err.starts_with(PERMANENT_PREFIX), "{err}");
+}
diff --git a/src-tauri/src/core/threads/utils.rs b/src-tauri/src/core/threads/utils.rs
index b3ea257a0..166eeffc0 100644
--- a/src-tauri/src/core/threads/utils.rs
+++ b/src-tauri/src/core/threads/utils.rs
@@ -3,6 +3,11 @@ use std::path::{Path, PathBuf};
use super::constants::{MESSAGES_FILE, THREADS_DIR, THREADS_FILE};
+/// Prefix of an error that retrying can never fix (an id the store refuses, a
+/// directory that is someone else's, an unsupported sidecar). Stable: the web
+/// app's Cowork migration classifies failures by it, not by message text.
+pub const PERMANENT_PREFIX: &str = "PERMANENT: ";
+
/// Longest thread id accepted. Real ids are UUIDs (36 chars) or ULIDs (26).
const MAX_THREAD_ID_LEN: usize = 128;
@@ -29,17 +34,17 @@ fn is_windows_device_name(name: &str) -> bool {
/// empty names, so the id can never resolve outside its own directory.
pub fn validate_thread_id(thread_id: &str) -> Result<(), String> {
if thread_id.is_empty() {
- return Err("Invalid thread id: empty".to_string());
+ return Err(format!("{PERMANENT_PREFIX}Invalid thread id: empty"));
}
if thread_id.len() > MAX_THREAD_ID_LEN {
- return Err("Invalid thread id: too long".to_string());
+ return Err(format!("{PERMANENT_PREFIX}Invalid thread id: too long"));
}
if !thread_id
.bytes()
.all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_')
|| is_windows_device_name(thread_id)
{
- return Err(format!("Invalid thread id: {thread_id:?}"));
+ return Err(format!("{PERMANENT_PREFIX}Invalid thread id: {thread_id:?}"));
}
Ok(())
}