Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
abde0d6
Refactor F1+F2: add get_jllama_context_impl and throw_invalid_request…
claude Apr 5, 2026
2fc8687
Refactor F3: extract collect_task_results helper, add tests
claude Apr 5, 2026
6b71495
Redesign F3: extract collect_task_results_impl to jni_server_helpers.hpp
claude Apr 5, 2026
8473d20
Fix double-include of server.hpp (no include guard)
claude Apr 5, 2026
a55693d
Fix include order: server.hpp before jni_server_helpers.hpp in jllama…
claude Apr 5, 2026
c85a14f
Fix uncaught exception in handleCompletionsOai crashing JVM
claude Apr 5, 2026
da8f314
Refactor F4: extract build_completion_tasks_impl, remove 6 duplicated…
claude Apr 5, 2026
2ab7a12
Finding 5: extract recv_slot_task_result_impl, replace 4 handleSlotAc…
claude Apr 5, 2026
ebf352d
Finding 6: add [[nodiscard]] to all _impl helpers and their wrappers
claude Apr 5, 2026
6ff2f54
Finding A: fix JNI_OnUnload double-delete of c_log_level and missing …
claude Apr 5, 2026
3b47cb7
Finding B: replace missed manual collect loop in handleChatCompletions
claude Apr 5, 2026
da3b530
Finding C: extract parse_json_params, replace 9 duplicate parse_jstri…
claude Apr 5, 2026
6166224
Finding D: extract dispatch_tasks, replace 9 duplicate add_waiting+ge…
claude Apr 5, 2026
6693162
Finding E: extract results_to_jstring_impl, replace 4 duplicate seria…
claude Apr 5, 2026
46fe065
Finding I: remove dead variables and redundant to_json() calls in embed
claude Apr 5, 2026
f46ec39
Finding H: extract require_single_task_id_impl, replace 2 duplicate g…
claude Apr 5, 2026
53bb4b5
Finding G: extract detokenize helper, replace is_vocab_only dispatch …
claude Apr 5, 2026
bc8bdb6
Finding F: extract dispatch_single_task, replace 4 duplicate register…
claude Apr 5, 2026
769f258
Finding J: extract parse_oai_chat_params, replace 2 duplicate try/cat…
claude Apr 5, 2026
1a62d86
Fix build error and warnings: const json& in parse_oai_chat_params, n…
claude Apr 5, 2026
06169a8
Finding K: remove dead commented-out duplicate log block in loadModel
claude Apr 5, 2026
6875e5f
Finding L: extract json_to_jstring_impl for repeated dump()+NewString…
claude Apr 5, 2026
f0a0e37
Finding M: extract exec_slot_file_task for SAVE/RESTORE slot actions
claude Apr 5, 2026
9b5e03e
Finding N: extract collect_and_serialize for the collect+results_to_j…
claude Apr 5, 2026
8df3894
Fix M: move exec_slot_file_task after parse_jstring/recv_slot_task_re…
claude Apr 5, 2026
339ae6b
Finding O: use json_to_jstring_impl in recv_slot_task_result_impl suc…
claude Apr 5, 2026
8aac5bb
Finding U: split results_to_json_impl out of results_to_jstring_impl
claude Apr 5, 2026
1b7fd3b
Finding P: extract jint_array_to_tokens_impl for jintArray→vector<lla…
claude Apr 5, 2026
2343053
Finding Q: extract get_result_error_message for repeated error-string…
claude Apr 5, 2026
b40ae3d
Finding R: introduce fail_load lambda in loadModel for repeated cleanup
claude Apr 5, 2026
df74180
Finding S: extract check_infill_support_impl for FIM token compatibil…
claude Apr 5, 2026
3817d75
Finding T: extract require_json_field_impl for repeated field-presenc…
claude Apr 5, 2026
16ef233
Fix ordering in jni_server_helpers.hpp: define helpers before their c…
claude Apr 5, 2026
e3dcf6b
Add /find-cpp-duplication Claude skill
claude Apr 5, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions .claude/commands/find-cpp-duplication.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
name: find-cpp-duplication
description: Scan C++ source files in src/main/cpp/ for all categories of duplication and boilerplate — short repeated expressions, copy-pasted blocks, pipeline compositions, near-identical switch arms, mixed JNI/logic concerns, inconsistent helper usage, local cleanup sequences, and dead code. Reports findings only, no modifications.
---

Review the C++ source files in src/main/cpp/ (jllama.cpp, jni_helpers.hpp,
jni_server_helpers.hpp, server.hpp, utils.hpp) and identify all duplication
opportunities. Cast a wider net than start/end boilerplate — include patterns
anywhere inside a function body.

Look for ALL of the following categories:

1. SHORT REPEATED EXPRESSIONS (1–2 lines, 3+ sites)
Any single expression or two-line sequence that appears verbatim in three or
more places, even if surrounded by different context.
Example: result->to_json()["message"].get<std::string>()

2. REPEATED MULTI-LINE BLOCKS (3+ lines, 2+ sites)
Any block of 3 or more consecutive lines that is copy-pasted with at most
minor variation (different variable names or one differing string literal).
Example: four-line jintArray → vector<llama_token> read pattern.

3. PIPELINE COMPOSITIONS
Any sequence of 2+ function calls that always appear chained together in the
same order at every call site. The chain itself is a candidate for wrapping.
Example: build_completion_tasks → dispatch_tasks → collect_and_serialize

4. NEAR-IDENTICAL SWITCH CASES OR IF-ELSE ARMS
Two or more switch cases / if-else branches whose bodies differ only by one
variable, constant, or string literal.

5. MIXED CONCERNS (JNI + LOGIC)
Functions where pure computation (no JNI calls) is interleaved with JNI
serialisation. The pure part is a candidate for extraction to a separately
testable _impl function.
Example: single-vs-array JSON construction inside results_to_jstring.

6. INCONSISTENT HELPER USAGE
Places where an already-extracted helper exists but is not used — either
because the helper was added after the call site was written, or the call
site is inside a header that the helper lives in.
Example: a header function still using dump()+NewStringUTF after
json_to_jstring_impl was extracted.

7. LOCAL CLEANUP SEQUENCES
Repeated tear-down sequences inside a single function (delete X; delete Y;
free(); ThrowNew()) that differ only in the error message — candidate for a
local lambda.

8. DEAD CODE
Commented-out blocks that duplicate active code immediately above or below.

For each finding report:
- Category (from the list above)
- Exact file names and line numbers of every occurrence
- The minimal signature of the helper that would eliminate the duplication
- Whether the extraction is unit-testable without a real JVM or llama model
(i.e., can all llama.h / server.hpp dependencies be passed as parameters)
- Estimated line savings across all call sites

Do not modify any files. Report only.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ if(BUILD_TESTING)
src/test/cpp/test_utils.cpp
src/test/cpp/test_server.cpp
src/test/cpp/test_jni_helpers.cpp
src/test/cpp/test_jni_server_helpers.cpp
)

target_include_directories(jllama_test PRIVATE
Expand Down
Loading
Loading