Skip to content

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

Rust

fix(llm): drain the cursor agent's pipes together and keep its workspace private

AkitaOnRailsf29e92a840f0
diff --git a/crates/ai-memory-llm/src/cursor_agent.rs b/crates/ai-memory-llm/src/cursor_agent.rs
index 2e9be51d..e6b3c1a4 100644
--- a/crates/ai-memory-llm/src/cursor_agent.rs
+++ b/crates/ai-memory-llm/src/cursor_agent.rs
@@ -108,8 +108,15 @@ impl CursorAgentProvider {
.ok_or_else(|| LlmError::UnexpectedShape("cursor agent stderr missing".into()))?;
let collected = timeout(self.timeout, async {
- let stdout = read_capped(stdout, MAX_STDOUT_BYTES).await?;
- let stderr = read_capped(stderr, MAX_STDERR_BYTES).await?;
+ // Both pipes drain at once: reading stdout to EOF first would
+ // leave a chatty child blocked on a full stderr pipe until the
+ // timeout. `try_join!` also stops at the first capped stream
+ // instead of waiting on the other one (the child is killed on
+ // drop).
+ let (stdout, stderr) = tokio::try_join!(
+ read_capped(stdout, MAX_STDOUT_BYTES),
+ read_capped(stderr, MAX_STDERR_BYTES)
+ )?;
let status = child.wait().await.map_err(|err| {
LlmError::UnexpectedShape(format!("waiting for cursor agent: {err}"))
})?;
@@ -176,7 +183,12 @@ struct TempWorkspace {
impl TempWorkspace {
fn create() -> LlmResult<Self> {
let path = std::env::temp_dir().join(format!("ai-memory-cursor-{}", Uuid::now_v7()));
- std::fs::create_dir(&path).map_err(|err| {
+ // The prompt is captured session text: keep the workspace private
+ // to the server's user on a shared temp directory.
+ let mut builder = std::fs::DirBuilder::new();
+ #[cfg(unix)]
+ std::os::unix::fs::DirBuilderExt::mode(&mut builder, 0o700);
+ builder.create(&path).map_err(|err| {
LlmError::UnexpectedShape(format!("creating cursor workspace: {err}"))
})?;
Ok(Self { path })
diff --git a/crates/ai-memory-llm/tests/suite/cursor_provider.rs b/crates/ai-memory-llm/tests/suite/cursor_provider.rs
index 99c54e66..fddfd1e3 100644
--- a/crates/ai-memory-llm/tests/suite/cursor_provider.rs
+++ b/crates/ai-memory-llm/tests/suite/cursor_provider.rs
@@ -105,3 +105,69 @@ async fn fenced_json_is_parsed() {
.unwrap();
assert_eq!(value["answer"], 1);
}
+
+fn script_agent(dir: &Path, body: &str) -> PathBuf {
+ let path = dir.join("agent");
+ std::fs::write(&path, format!("#!/bin/sh\n{body}\n")).unwrap();
+ let mut perms = std::fs::metadata(&path).unwrap().permissions();
+ perms.set_mode(0o755);
+ std::fs::set_permissions(&path, perms).unwrap();
+ path
+}
+
+/// A child that floods stderr past the pipe buffer before writing stdout
+/// must not hang the call: both pipes drain together, and a stream over its
+/// cap ends the call at once instead of waiting out the timeout.
+#[tokio::test]
+async fn a_stderr_flood_neither_hangs_nor_blocks_stdout() {
+ let dir = TempDir::new();
+ // 40 KiB of stderr, then the answer: within the cap, so it succeeds.
+ let ok = script_agent(
+ &dir.0,
+ "head -c 40960 /dev/zero | tr '\\0' e >&2\nprintf 'OK\\n'",
+ );
+ let response = tokio::time::timeout(
+ std::time::Duration::from_secs(20),
+ provider(ok, "default").complete(ChatRequest::user_prompt("hi")),
+ )
+ .await
+ .expect("a 40 KiB stderr must not hang the call")
+ .unwrap();
+ assert_eq!(response.text, "OK");
+
+ // 200 KiB of stderr while stdout stays open: over the cap, so it fails
+ // fast instead of blocking on the full stderr pipe.
+ let flood = script_agent(
+ &dir.0,
+ "head -c 204800 /dev/zero | tr '\\0' e >&2\nsleep 30\nprintf 'late\\n'",
+ );
+ let err = tokio::time::timeout(
+ std::time::Duration::from_secs(20),
+ provider(flood, "default").complete(ChatRequest::user_prompt("hi")),
+ )
+ .await
+ .expect("a stderr flood must not hang the call")
+ .unwrap_err();
+ assert!(err.to_string().contains("size cap"), "{err}");
+}
+
+/// The per-call workspace holds the prompt (captured session text), so it is
+/// created private to the server's user.
+#[tokio::test]
+async fn the_prompt_workspace_is_private() {
+ let dir = TempDir::new();
+ let sink = dir.0.join("mode");
+ let agent = script_agent(
+ &dir.0,
+ &format!(
+ "ls -ld . > {}\nprintf 'OK\\n'",
+ sh_single(&sink.display().to_string())
+ ),
+ );
+ provider(agent, "default")
+ .complete(ChatRequest::user_prompt("hi"))
+ .await
+ .unwrap();
+ let listing = std::fs::read_to_string(&sink).unwrap();
+ assert!(listing.starts_with("drwx------"), "{listing}");
+}
diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md
index 9664eb36..7afbd317 100644
--- a/docs/ARCHITECTURE.md
+++ b/docs/ARCHITECTURE.md
@@ -1067,7 +1067,7 @@ stopwords or adding a ranking signal.
**LLM provider env** (opt-in):
```
AI_MEMORY_LLM_PROVIDER anthropic | anthropic-oauth | openai | openai-oauth | codex | copilot |
- gemini | openai-compat | opencode
+ cursor | gemini | openai-compat | opencode
AI_MEMORY_LLM_MODEL optional when the provider has a default; e.g. claude-haiku-4-5, gpt-5.4-mini
ANTHROPIC_API_KEY / OPENAI_API_KEY / GEMINI_API_KEY / LLM_API_KEY
AI_MEMORY_LLM_BASE_URL required for openai-compat (Ollama, vLLM); optional override for
@@ -1108,6 +1108,7 @@ AI_MEMORY_LLM_HEADERS optional extra HTTP headers on every chat request, as
AI_MEMORY_RERANKER optional `llm`; reranks project/scopes query candidates
COPILOT_GITHUB_TOKEN optional GitHub token for copilot
AI_MEMORY_CODEX_EXECUTABLE optional Codex executable; defaults to codex on PATH
+AI_MEMORY_CURSOR_AGENT optional Cursor Agent CLI executable for cursor; defaults to agent on PATH
GITHUB_COPILOT_API_TOKEN optional pre-minted Copilot API token
COPILOT_API_URL optional Copilot API base URL override
```