Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions docs/reference/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,9 @@ The code index's network git (clone, fetch, ls-remote, submodule update) through
| `LOOM_CODE_UNIT_RISK_MAX_AGE_S` | `int` | `864000` | | Age (seconds) past which a risk row is stale and annotated live; must exceed the weekly pass interval. |
| `LOOM_CODE_SEARCH_BODIES_DEFAULT` | `'auto' \| 'none'` | `'auto'` | | code.search `bodies` when the request omits it: "auto" fills bodies into the budget; "none" serves handles only. |
| `LOOM_CODE_SEARCH_TOKEN_BUDGET` | `int` | `0` | | code.search response budget in tokens when the request omits token_budget; 0 = unlimited. An explicit request budget overrides this default. |
| `LOOM_CODE_SEARCH_PLACES_TOKEN_BUDGET` | `int` | `600` | | Token target for MCP search places when the request omits token_budget. A lone place or cursor page can exceed it. 0 = unlimited. |
| `LOOM_CODE_SEARCH_PLACES_SOURCE_MAX_LINES` | `int` | `30` | | Maximum lines in the full body of the leading search place. 0 = omit the full body. |
| `LOOM_CODE_SEARCH_PLACES_QUERY_MAX_WORDS` | `int` | `32` | | Maximum distinct query words used to select matching lines in search places. |
| `LOOM_CODE_SEARCH_MEMO_ENABLED` | `bool` | `True` | | Memoise served code.search responses per (org, namespace, request shape) in loom.code_search_memo and replay within the TTL (route "+memo"); `fresh=true` bypasses it. |
| `LOOM_CODE_SEARCH_MEMO_TTL_S` | `int` | `1800` | | Seconds a memoised response, and a cursor's stored pool, stay replayable. |
| `LOOM_CODE_SEARCH_MEMO_MAX_PAYLOAD_BYTES` | `int` | `200000` | | Max bytes of a response the memo stores; a larger one is not memoised. 0 = no cap. |
Expand Down
12 changes: 12 additions & 0 deletions loom.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2045,6 +2045,18 @@ settings:
# unlimited. An explicit request budget overrides this default. [restart]
# code_search_token_budget: 0

# Token target for MCP search places when the request omits token_budget. A lone place
# or cursor page can exceed it. 0 = unlimited. [restart]
# code_search_places_token_budget: 600

# Maximum lines in the full body of the leading search place. 0 = omit the full body.
# [restart]
# code_search_places_source_max_lines: 30

# Maximum distinct query words used to select matching lines in search places.
# [restart]
# code_search_places_query_max_words: 32

# Memoise served code.search responses per (org, namespace, request shape) in
# loom.code_search_memo and replay within the TTL (route "+memo"); `fresh=true`
# bypasses it. [restart]
Expand Down
18 changes: 18 additions & 0 deletions schemas/loom-settings.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2189,6 +2189,24 @@
"type": "string",
"description": "code.search's listwise pick (a request's `rerank: \"builtin:pick\"`): for an issue-shaped query it reads one line for each of the first 100 ranked units and names the definitions a fix changes, with how sure it is. The provider's smallest model is the one measured. \"\" = off: such a request keeps the plain ranking."
},
"code_search_places_query_max_words": {
"default": 32,
"minimum": 1,
"type": "integer",
"description": "Maximum distinct query words used to select matching lines in search places."
},
"code_search_places_source_max_lines": {
"default": 30,
"minimum": 0,
"type": "integer",
"description": "Maximum lines in the full body of the leading search place. 0 = omit the full body."
},
"code_search_places_token_budget": {
"default": 600,
"minimum": 0,
"type": "integer",
"description": "Token target for MCP search places when the request omits token_budget. A lone place or cursor page can exceed it. 0 = unlimited."
},
"code_search_pr_leg": {
"default": "issue",
"enum": [
Expand Down
8 changes: 8 additions & 0 deletions server/settings/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -1692,6 +1692,14 @@ def _check_git_proxy(self) -> CodeSettings:
# code.search response budget in tokens when the request omits token_budget;
# 0 = unlimited. An explicit request budget overrides this default.
code_search_token_budget: int = Field(default=0, ge=0)
# Token target for MCP search places when the request omits token_budget.
# A lone place or cursor page can exceed it. 0 = unlimited.
code_search_places_token_budget: int = Field(default=600, ge=0)
# Maximum lines in the full body of the leading search place.
# 0 = omit the full body.
code_search_places_source_max_lines: int = Field(default=30, ge=0)
# Maximum distinct query words used to select matching lines in search places.
code_search_places_query_max_words: int = Field(default=32, ge=1)
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
# Memoise served code.search responses per (org, namespace, request shape)
# in loom.code_search_memo and replay within the TTL (route "+memo");
# `fresh=true` bypasses it.
Expand Down
14 changes: 5 additions & 9 deletions server/transport/code_lean.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,14 +631,10 @@ def _lean_search(out: dict[str, Any]) -> None:
)
PLACE_MATCHING_LINES = 2
PLACE_LINE_CHARS = 120
# What the places of one reply may cost by `estimate_json_tokens`; a call's own
# `token_budget` replaces it (0 = no limit).
PLACES_TOKEN_BUDGET = 600
# The first place carries its source when no other result competes with it (it is
# the only one, or its score is this many times the second's) and the source is
# short: a body costs its lines on every later request, a missing one a read.
TOP_SOURCE_LEAD = 2.0
TOP_SOURCE_MAX_LINES = 30
_QUERY_WORD = re.compile(r"\w{3,}")
# A three-letter word is common inside longer ones (`set` in `offset`, `for` in
# `format`): it matches a whole word or one part of an identifier (`set_quota`,
Expand All @@ -648,8 +644,6 @@ def _lean_search(out: dict[str, Any]) -> None:
# A line the chunker wrote, not source: a continued piece's header, an elided body,
# a window's pointer to the next, after the language's comment leader if it has one.
_CHUNK_MARKER = re.compile(r"(?:\S+ )?… (?:continued |body elided )")
# A pasted issue has thousands of words: the longest are the identifiers.
_QUERY_WORDS_MAX = 32
# A leg that proposes a unit neither content leg ranked.
_VIA_REASON = {"symbol": "exact name", "definition": "named in the query"}
# What `_compact_as_of` keeps of a fresh index.
Expand All @@ -669,7 +663,9 @@ def _source_lines(row: dict[str, Any]) -> tuple[list[str], int]:

def _query_words(query: Any) -> list[str]:
words = {word.lower() for word in _QUERY_WORD.findall(str(query or ""))}
return sorted(words, key=lambda word: (-len(word), word))[:_QUERY_WORDS_MAX]
return sorted(words, key=lambda word: (-len(word), word))[
: settings.code_search_places_query_max_words
]


def _matching_lines(row: dict[str, Any], words: list[str]) -> list[str]:
Expand Down Expand Up @@ -703,7 +699,7 @@ def _place_reason(row: dict[str, Any]) -> str:

def _top_source(rows: list[dict[str, Any]]) -> bool:
lines, first = _source_lines(rows[0])
if not first or len(lines) > TOP_SOURCE_MAX_LINES:
if not first or len(lines) > settings.code_search_places_source_max_lines:
return False
if len(rows) == 1:
return True
Expand All @@ -727,7 +723,7 @@ def _places_budget(arguments: dict[str, Any]) -> int:
try:
return max(int(arguments["token_budget"]), 0)
except (KeyError, TypeError, ValueError):
return PLACES_TOKEN_BUDGET
return settings.code_search_places_token_budget


def _search_places(out: dict[str, Any], served: Any, arguments: dict[str, Any]) -> None:
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/snapshots/settings_defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,9 @@
"code_search_memo_ttl_s": 1800,
"code_search_pattern_max_files": 1500,
"code_search_pick_model": "",
"code_search_places_query_max_words": 32,
"code_search_places_source_max_lines": 30,
"code_search_places_token_budget": 600,
"code_search_pr_leg": "issue",
"code_search_pr_leg_cap": 0.5,
"code_search_pr_leg_max_files": 10,
Expand Down
20 changes: 18 additions & 2 deletions tests/unit/test_code_search_places.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def test_places_fit_the_budget_and_a_call_can_set_its_own() -> None:
query = {"query": "quota usage limit of an account", "top_k": 25}
out = lean_code("code.search", {**_reply(rows), "has_more": False}, query)
places = out["results"]
assert estimate_json_tokens(places) <= code_lean.PLACES_TOKEN_BUDGET
assert estimate_json_tokens(places) <= settings.code_search_places_token_budget
assert 1 < len(places) < 25 and out["has_more"] is True
assert [row["path"] for row in places] == [row["path"] for row in rows[: len(places)]]
assert not any("lines" in row for row in places), "lines leave before places do"
Expand Down Expand Up @@ -244,6 +244,22 @@ def test_a_short_word_matches_a_whole_word_or_identifier_part_not_inside_a_word(
assert longer["results"][0]["lines"] == ["14: free_symbols = offset"]


def test_search_places_limit_the_query_words_used_for_matching(
monkeypatch: pytest.MonkeyPatch,
) -> None:
rows = [_unit("a.py", "cache = {}\nreturn relationship\n"), _unit("b.py", "x = 1\n")]
query = {"query": "cache relationship"}
assert lean_code("code.search", _reply(rows), query)["results"][0]["lines"] == [
"10: cache = {}",
"11: return relationship",
]

monkeypatch.setattr(settings, "code_search_places_query_max_words", 1)
assert lean_code("code.search", _reply(rows), query)["results"][0]["lines"] == [
"11: return relationship",
]


def test_the_first_place_carries_short_source_only_when_nothing_competes() -> None:
query = {"query": "save"}
alone = lean_code("code.search", _reply([_unit("a.py", SAVE, via="symbol")]), query)
Expand All @@ -264,7 +280,7 @@ def test_the_first_place_carries_short_source_only_when_nothing_competes() -> No
first, runner_up = lean_code("code.search", _reply(rows), query)["results"]
assert ("body" in first) is has_source and ("lines" in first) is not has_source
assert "body" not in runner_up
long = _unit("a.py", SAVE * (code_lean.TOP_SOURCE_MAX_LINES // 5 + 1))
long = _unit("a.py", SAVE * (settings.code_search_places_source_max_lines // 5 + 1))
assert "body" not in lean_code("code.search", _reply([long]), query)["results"][0]
# two namespaces rank on separate scales: a score lead across them is no lead
rows = [_unit("a.py", SAVE, namespace="code-a"), _unit("b.py", SAVE, score=0.01)]
Expand Down
Loading