Skip to content

Commit df9c939

Browse files
sakirrcursoragent
andcommitted
fix: address bot review comments - null guard, decode failure flag, error details, and JNI exception fallback
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 3fb52db commit df9c939

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

sdk/runanywhere-commons/src/backends/llamacpp/llamacpp_backend.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,9 @@ TextGenerationResult LlamaCppTextGeneration::generate(const TextGenerationReques
529529
result.prompt_tokens = prompt_tokens;
530530
result.inference_time_ms = duration.count();
531531

532-
if (cancel_requested_.load()) {
532+
if (decode_failed_) {
533+
result.finish_reason = "error";
534+
} else if (cancel_requested_.load()) {
533535
result.finish_reason = "cancelled";
534536
} else if (success) {
535537
result.finish_reason = tokens_generated >= request.max_tokens ? "length" : "stop";
@@ -556,6 +558,7 @@ bool LlamaCppTextGeneration::generate_stream(const TextGenerationRequest& reques
556558
}
557559

558560
cancel_requested_.store(false);
561+
decode_failed_ = false;
559562

560563
std::string prompt = build_prompt(request);
561564
LOGI("Generating with prompt length: %zu", prompt.length());
@@ -724,6 +727,7 @@ bool LlamaCppTextGeneration::generate_stream(const TextGenerationRequest& reques
724727

725728
if (llama_decode(context_, batch) != 0) {
726729
LOGE("llama_decode failed during generation");
730+
decode_failed_ = true;
727731
break;
728732
}
729733
}
@@ -732,7 +736,9 @@ bool LlamaCppTextGeneration::generate_stream(const TextGenerationRequest& reques
732736
callback(stop_window);
733737
}
734738

735-
llama_memory_clear(llama_get_memory(context_), true);
739+
if (llama_memory_t post_mem = llama_get_memory(context_)) {
740+
llama_memory_clear(post_mem, true);
741+
}
736742

737743
llama_batch_free(batch);
738744

sdk/runanywhere-commons/src/backends/llamacpp/llamacpp_backend.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ class LlamaCppTextGeneration {
134134

135135
bool model_loaded_ = false;
136136
std::atomic<bool> cancel_requested_{false};
137+
bool decode_failed_ = false;
137138

138139
std::string model_path_;
139140
nlohmann::json model_config_;

sdk/runanywhere-commons/src/backends/llamacpp/rac_llm_llamacpp.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,10 @@ rac_result_t rac_llm_llamacpp_generate(rac_handle_t handle, const char* prompt,
207207
}
208208
RAC_LOG_INFO("LLM.LlamaCpp", "rac_llm_llamacpp_generate: generate() returned, tokens=%d", result.tokens_generated);
209209

210+
// finish_reason is std::string; TODO: migrate to enum if TextGenerationResult gains one
210211
if (result.finish_reason == "error") {
211212
RAC_LOG_ERROR("LLM.LlamaCpp", "rac_llm_llamacpp_generate: generation failed (e.g. llama_decode error)");
213+
rac_error_set_details("Generation failed: llama_decode returned non-zero");
212214
return RAC_ERROR_GENERATION_FAILED;
213215
}
214216

sdk/runanywhere-commons/src/jni/runanywhere_commons_jni.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <jni.h>
1818

1919
#include <condition_variable>
20+
#include <cstdio>
2021
#include <cstring>
2122
#include <mutex>
2223
#include <string>
@@ -567,13 +568,17 @@ Java_com_runanywhere_sdk_native_bridge_RunAnywhereBridge_racLlmComponentGenerate
567568

568569
if (status != RAC_SUCCESS) {
569570
LOGe("racLlmComponentGenerate failed with status=%d", status);
571+
rac_llm_result_free(&result);
570572
const char* msg = rac_error_message(status);
571-
if (msg && *msg) {
572-
jclass exClass = env->FindClass("java/lang/RuntimeException");
573-
if (exClass) {
574-
env->ThrowNew(exClass, msg);
575-
env->DeleteLocalRef(exClass);
573+
jclass exClass = env->FindClass("java/lang/RuntimeException");
574+
if (exClass) {
575+
char fallback[64];
576+
if (!msg || !*msg) {
577+
snprintf(fallback, sizeof(fallback), "LLM generation failed (status=%d)", status);
578+
msg = fallback;
576579
}
580+
env->ThrowNew(exClass, msg);
581+
env->DeleteLocalRef(exClass);
577582
}
578583
return nullptr;
579584
}
@@ -849,6 +854,17 @@ Java_com_runanywhere_sdk_native_bridge_RunAnywhereBridge_racLlmComponentGenerate
849854

850855
if (status != RAC_SUCCESS) {
851856
LOGe("rac_llm_component_generate_stream failed with status=%d", status);
857+
const char* msg = rac_error_message(status);
858+
jclass exClass = env->FindClass("java/lang/RuntimeException");
859+
if (exClass) {
860+
char fallback[64];
861+
if (!msg || !*msg) {
862+
snprintf(fallback, sizeof(fallback), "LLM stream generation failed (status=%d)", status);
863+
msg = fallback;
864+
}
865+
env->ThrowNew(exClass, msg);
866+
env->DeleteLocalRef(exClass);
867+
}
852868
return nullptr;
853869
}
854870

0 commit comments

Comments
 (0)