Skip to content

LLM Wiki is a cross-platform desktop application that turns your documents into an organized, interlinked knowledge base — automatically. Instead of traditional RAG (retrieve-and-answer from scratch every time), the LLM incrementally builds and maintains a persistent wiki from your sources。

TypeScript

Harden Codex CLI stream completion

nash_sud0cf416efa78
diff --git a/src-tauri/src/commands/codex_cli.rs b/src-tauri/src/commands/codex_cli.rs
index fa9276e7..a8be6215 100644
--- a/src-tauri/src/commands/codex_cli.rs
+++ b/src-tauri/src/commands/codex_cli.rs
@@ -36,6 +36,21 @@ const CODEX_SPAWN_TIMEOUT: Duration = Duration::from_secs(10 * 60);
const STDERR_LIMIT_BYTES: usize = 1024 * 1024;
const STDOUT_LIMIT_BYTES: usize = 1024 * 1024;
+fn append_capped_line(collected: &mut String, line: &str, limit_bytes: usize) {
+ if collected.len() >= limit_bytes {
+ return;
+ }
+ for ch in line.chars() {
+ if collected.len() + ch.len_utf8() > limit_bytes {
+ break;
+ }
+ collected.push(ch);
+ }
+ if collected.len() < limit_bytes {
+ collected.push('\n');
+ }
+}
+
fn find_codex_command() -> Result<PathBuf, String> {
#[cfg(windows)]
{
@@ -196,17 +211,7 @@ pub async fn codex_cli_spawn(
let mut collected = String::new();
while let Ok(Some(line)) = stderr_reader.next_line().await {
eprintln!("[codex-cli stderr] {line}");
- if collected.len() < STDERR_LIMIT_BYTES {
- for ch in line.chars() {
- if collected.len() + ch.len_utf8() > STDERR_LIMIT_BYTES {
- break;
- }
- collected.push(ch);
- }
- if collected.len() < STDERR_LIMIT_BYTES {
- collected.push('\n');
- }
- }
+ append_capped_line(&mut collected, &line, STDERR_LIMIT_BYTES);
}
collected
});
@@ -215,17 +220,7 @@ pub async fn codex_cli_spawn(
loop {
match reader.next_line().await {
Ok(Some(line)) => {
- if stdout_text.len() < STDOUT_LIMIT_BYTES {
- for ch in line.chars() {
- if stdout_text.len() + ch.len_utf8() > STDOUT_LIMIT_BYTES {
- break;
- }
- stdout_text.push(ch);
- }
- if stdout_text.len() < STDOUT_LIMIT_BYTES {
- stdout_text.push('\n');
- }
- }
+ append_capped_line(&mut stdout_text, &line, STDOUT_LIMIT_BYTES);
if app.emit(&topic, line).is_err() {
break;
}
@@ -290,3 +285,34 @@ pub async fn codex_cli_kill(
}
Ok(())
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn append_capped_line_appends_newline_when_space_remains() {
+ let mut out = String::new();
+ append_capped_line(&mut out, "hello", 16);
+ assert_eq!(out, "hello\n");
+ }
+
+ #[test]
+ fn append_capped_line_never_exceeds_limit() {
+ let mut out = String::new();
+ append_capped_line(&mut out, "abcdef", 4);
+ assert_eq!(out, "abcd");
+ assert_eq!(out.len(), 4);
+ append_capped_line(&mut out, "ignored", 4);
+ assert_eq!(out, "abcd");
+ }
+
+ #[test]
+ fn append_capped_line_preserves_utf8_boundaries() {
+ let mut out = String::new();
+ append_capped_line(&mut out, "é水x", 5);
+ assert_eq!(out, "é水");
+ assert_eq!(out.len(), 5);
+ assert!(std::str::from_utf8(out.as_bytes()).is_ok());
+ }
+}
diff --git a/src/lib/codex-cli-transport.test.ts b/src/lib/codex-cli-transport.test.ts
index 7d53a2c0..39ec0e5f 100644
--- a/src/lib/codex-cli-transport.test.ts
+++ b/src/lib/codex-cli-transport.test.ts
@@ -239,6 +239,49 @@ describe("streamCodexCli", () => {
expect(callbacks.onError).not.toHaveBeenCalled()
})
+ it("surfaces a clear error when completion has no agent message", async () => {
+ const callbacks = {
+ onToken: vi.fn(),
+ onDone: vi.fn(),
+ onError: vi.fn(),
+ }
+
+ const stream = streamCodexCli(
+ {
+ provider: "codex-cli",
+ apiKey: "",
+ model: "gpt-5.1-codex-mini",
+ ollamaUrl: "",
+ customEndpoint: "",
+ maxContextSize: 128000,
+ },
+ [{ role: "user", content: "Analyze this source." }],
+ callbacks,
+ )
+
+ await vi.waitFor(() => {
+ expect(tauriMocks.invoke).toHaveBeenCalledTimes(1)
+ })
+
+ const payload = tauriMocks.invoke.mock.calls[0]?.[1] as { streamId: string }
+ tauriMocks.emit(`codex-cli:${payload.streamId}:done`, {
+ code: 0,
+ stderr: "",
+ stdout: JSON.stringify({ type: "turn.completed" }),
+ })
+
+ await stream
+
+ expect(callbacks.onToken).not.toHaveBeenCalled()
+ expect(callbacks.onDone).not.toHaveBeenCalled()
+ expect(callbacks.onError).toHaveBeenCalledTimes(1)
+ expect(callbacks.onError.mock.calls[0]?.[0]).toEqual(
+ expect.objectContaining({
+ message: expect.stringContaining("completed but did not emit an agent_message"),
+ }),
+ )
+ })
+
it("does not spawn when the signal is already aborted", async () => {
const controller = new AbortController()
controller.abort()