fix(memory-lancedb): validate CLI search limit to prevent NaN propagation#63413
fix(memory-lancedb): validate CLI search limit to prevent NaN propagation#63413PrincNL wants to merge 3 commits intoopenclaw:mainfrom
Conversation
…tion Add radix and NaN guard to the --limit CLI option in the ltm search command. Without validation, passing a non-numeric value like --limit abc causes NaN to propagate directly to the LanceDB .limit() call. Now falls back to the default of 5 when the input is invalid. Follows the established CLI option parsing pattern from src/cli/cron-cli/register.cron-simple.ts.
Greptile SummaryThis PR adds input validation to the Confidence Score: 5/5Safe to merge — the fix is correct, minimal, and consistent with the established pattern in the codebase. All findings are P2 (style suggestion). The validation logic is correct: NaN, zero, and negative values all fall back to 5, matching the commander default, and valid positive integers pass through unchanged. No files require special attention.
|
| const vector = await embeddings.embed(query); | ||
| const results = await db.search(vector, parseInt(opts.limit), 0.3); | ||
| const limitRaw = Number.parseInt(opts.limit, 10); | ||
| const limit = Number.isFinite(limitRaw) && limitRaw > 0 ? limitRaw : 5; |
There was a problem hiding this comment.
Silent fallback may surprise users
When an invalid --limit value is supplied, the fallback to 5 happens with no visible feedback, so a user who runs ltm search "query" --limit abc will receive results without any indication that their input was ignored. A brief console.warn (matching how other CLI commands surface misconfiguration) would make this easier to diagnose.
| const limit = Number.isFinite(limitRaw) && limitRaw > 0 ? limitRaw : 5; | |
| const limit = Number.isFinite(limitRaw) && limitRaw > 0 ? limitRaw : 5; | |
| if (limit !== limitRaw) { | |
| console.warn(`Invalid --limit value "${opts.limit}"; using default limit of ${limit}.`); | |
| } |
Prompt To Fix With AI
This is a comment left during a code review.
Path: extensions/memory-lancedb/index.ts
Line: 511
Comment:
**Silent fallback may surprise users**
When an invalid `--limit` value is supplied, the fallback to `5` happens with no visible feedback, so a user who runs `ltm search "query" --limit abc` will receive results without any indication that their input was ignored. A brief `console.warn` (matching how other CLI commands surface misconfiguration) would make this easier to diagnose.
```suggestion
const limit = Number.isFinite(limitRaw) && limitRaw > 0 ? limitRaw : 5;
if (limit !== limitRaw) {
console.warn(`Invalid --limit value "${opts.limit}"; using default limit of ${limit}.`);
}
```
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
Applied — added console.warn for invalid --limit values in 5edffa2.
Add console.warn when a non-numeric --limit value is silently replaced with the default, so users know their input was ignored.
Summary
ltm searchCLI command usesparseInt(opts.limit)without a radix parameter and without NaN validation. Passing a non-numeric value (e.g.--limit abc) causesNaNto propagate directly into the LanceDB.limit()call, leading to unpredictable query behavior.--limitvalues get a confusing LanceDB error instead of a graceful fallback.10) and aNumber.isFinite+ positive-integer guard that falls back to the default of5(matching the commander default on line 507).ltm search.Change Type (select all)
Scope (select all touched areas)
Linked Issue/PR
parseInt→Number.parseIntpattern, but forsrc/files — this PR coversextensions/memory-lancedb)Root Cause (if applicable)
parseInt(opts.limit)was used without a radix parameter and without validating the parsed result. JavaScript'sparseInt("abc")returnsNaN, which then flows unchecked intodb.search(vector, NaN, 0.3).src/cli/cron-cli/register.cron-simple.ts:72-73already usesNumber.parseIntwith radix andNumber.isFiniteguard — this extension predates that pattern.Regression Test Plan (if applicable)
extensions/memory-lancedb/index.test.tsltm search "query" --limit abcshould use the default limit (5) instead of propagating NaN.src/cli/cron-cli/register.cron-simple.ts. The commander default ("5") ensures the happy path always provides a valid string; only explicit invalid user input triggers the guard.User-visible / Behavior Changes
openclaw ltm search "query" --limit <invalid>now silently falls back to the default limit of 5 instead of producing unpredictable LanceDB behavior.Diagram (if applicable)
Security Impact (required)
NoNoNoNoNoRepro + Verification
Environment
Steps
openclaw ltm search "test query" --limit abcExpected
Actual (before fix)
NaNpropagates to LanceDB.limit()call, causing unpredictable query behaviorEvidence
Human Verification (required)
Number.parseInt("abc", 10)returnsNaN, and guard correctly falls back to5. Confirmed valid inputs ("1","10","100") parse correctly."-1"→ fallback), zero ("0"→ fallback), floats ("3.5"→ truncated to3, accepted).Review Conversations
Compatibility / Migration
YesNoNoRisks and Mitigations
5when0is passed (users cannot request zero results)."5".AI-assisted: yes