Skip to content

Commit 3d82da8

Browse files
committed
fix(mobile): prompt-last IPC layout so user | doesn't corrupt llm_gen
The old layout "generate|{prompt}|{max}|{temp}" + Kotlin split("|", limit = 3) truncated the prompt the moment a user typed `|`, and shoved whatever followed into max_tokens / temperature. On top of the reliability bug, this was a parsing-injection vector: a crafted prompt could silently coerce generation parameters. Switch to "generate|{max}|{temp}|{prompt}" — prompt is the last slot, so split keeps it intact as a single string (and the default 512 / 0.7 still apply if it's empty). Additionally strip NUL / CR / LF from the prompt on the Rust side, as the file IPC uses newline-agnostic read_to_string but the request parser does not. Closes #11/21.
1 parent 4ab6958 commit 3d82da8

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

‎packages/mobile/src-tauri/gen/android/app/src/main/java/ai/opencode/mobile/LlamaEngine.kt‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,16 @@ object LlamaEngine {
196196
"loaded" -> if (loaded()) "true" else "false"
197197
"stop" -> { stop(); "ok" }
198198
"generate" -> {
199+
// Protocol: "{max}|{temp}|{prompt}". Prompt is LAST so that
200+
// any `|` in user text is preserved (split with limit=3 keeps
201+
// the third slot as the uncut tail). Older layout put prompt
202+
// first and got corrupted the moment the user typed a `|`.
199203
val genParts = arg.split("|", limit = 3)
200-
val prompt = genParts.getOrElse(0) { "" }
204+
val maxTokens = genParts.getOrElse(0) { "512" }.toIntOrNull() ?: 512
205+
val temp = genParts.getOrElse(1) { "0.7" }.toFloatOrNull() ?: 0.7f
206+
val prompt = genParts.getOrElse(2) { "" }
201207
if (prompt.isEmpty()) "error:Empty prompt"
202208
else {
203-
val maxTokens = genParts.getOrElse(1) { "512" }.toIntOrNull() ?: 512
204-
val temp = genParts.getOrElse(2) { "0.7" }.toFloatOrNull() ?: 0.7f
205209
chat(prompt, maxTokens, temp)
206210
}
207211
}

‎packages/mobile/src-tauri/src/llm.rs‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,12 @@ pub async fn abort_llm(app: AppHandle) -> Result<(), String> {
270270
}
271271

272272
/// Generate text via Kotlin LlamaEngine (file IPC).
273+
///
274+
/// Protocol: `generate|{max}|{temp}|{prompt}`.
275+
/// `prompt` is placed LAST on purpose so a prompt containing `|` is kept
276+
/// intact — the Kotlin side calls `split("|", limit = 3)` on the argument
277+
/// tail, which stops after the third token. An older layout put prompt
278+
/// first and any `|` in user text would corrupt max / temp parsing.
273279
#[tauri::command]
274280
pub async fn generate_llm(
275281
app: AppHandle,
@@ -280,8 +286,14 @@ pub async fn generate_llm(
280286
let max = max_tokens.unwrap_or(512);
281287
let temp = temperature.unwrap_or(0.7);
282288

283-
// Send generate command — timeout 300s for generation
284-
let cmd = format!("generate|{}|{}|{}", prompt, max, temp);
289+
// Defensive: drop \0 / CR / LF that could break the request-file parser.
290+
let clean_prompt: String = prompt
291+
.chars()
292+
.filter(|c| *c != '\0' && *c != '\r' && *c != '\n')
293+
.collect();
294+
295+
// Send generate command — timeout 300s for generation.
296+
let cmd = format!("generate|{}|{}|{}", max, temp, clean_prompt);
285297
let result = llm_command(&app, &cmd, 300)?;
286298

287299
// Emit full result as token event

0 commit comments

Comments
 (0)