Skip to content
Open
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
5 changes: 5 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@
## 2026-04-29 - Python Dictionary Lookup Optimization for Lists
**Learning:** O(N) linear scans on static lists inside frequently called functions (e.g., `get_tool`) are a performance hazard. A `lru_cache(maxsize=1)` dictionary lookup wrapped in `MappingProxyType` to prevent modification is a simple and extremely effective optimization technique that reduces complexity from O(N) to O(1) and eliminates repeated loop and string allocations. We must be mindful when mapping non-unique elements. Preserving first-match behavior requires `if key not in lookup:` during the dictionary's initial lazy-loading map phase.
**Action:** Always favor lazily initialized cached dictionary lookups over list iteration when retrieving elements from static datasets by string keys.
## 2024-05-18 - Avoid Redundant String Allocations in Search Filters

When filtering lists of `PortingModule` objects (like tools and commands) based on string containment, avoid calling `.lower()` and concatenating strings inside loops.
Choose the narrowest cached search surface that preserves the caller's semantics: `search_text` is appropriate for free-text ranking across name, source hint, and responsibility, while `name_source_search_text` should be used for command/tool name-and-path lookup and `source_search_text` should be used for source-path classification filters such as plugin, skill, or MCP inclusion.
Using the right cached property inside list comprehensions prevents redundant string allocations without broadening field-specific filters into responsibility boilerplate matches.
Comment on lines +10 to +11
14 changes: 3 additions & 11 deletions src/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,15 @@ def get_commands(
) -> tuple[PortingModule, ...]:
commands = list(PORTED_COMMANDS)
if not include_plugin_commands:
commands = [
module for module in commands if "plugin" not in module.source_hint.lower()
]
commands = [module for module in commands if "plugin" not in module.source_search_text]
if not include_skill_commands:
commands = [
module for module in commands if "skills" not in module.source_hint.lower()
]
commands = [module for module in commands if "skills" not in module.source_search_text]
return tuple(commands)


def find_commands(query: str, limit: int = 20) -> list[PortingModule]:
needle = query.lower()
matches = [
module
for module in PORTED_COMMANDS
if needle in module.name.lower() or needle in module.source_hint.lower()
]
matches = [module for module in PORTED_COMMANDS if needle in module.name_source_search_text]
return matches[:limit]


Expand Down
8 changes: 8 additions & 0 deletions src/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ class PortingModule:
def search_text(self) -> str:
return f"{self.name} {self.source_hint} {self.responsibility}".lower()

@functools.cached_property
def name_source_search_text(self) -> str:
return f"{self.name} {self.source_hint}".lower()

@functools.cached_property
def source_search_text(self) -> str:
return self.source_hint.lower()


@dataclass(frozen=True)
class PermissionDenial:
Expand Down
13 changes: 2 additions & 11 deletions src/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,13 @@ def get_tools(
if module.name in {"BashTool", "FileReadTool", "FileEditTool"}
]
if not include_mcp:
tools = [
module
for module in tools
if "mcp" not in module.name.lower()
and "mcp" not in module.source_hint.lower()
]
tools = [module for module in tools if "mcp" not in module.name_source_search_text]
return filter_tools_by_permission_context(tuple(tools), permission_context)


def find_tools(query: str, limit: int = 20) -> list[PortingModule]:
needle = query.lower()
matches = [
module
for module in PORTED_TOOLS
if needle in module.name.lower() or needle in module.source_hint.lower()
]
matches = [module for module in PORTED_TOOLS if needle in module.name_source_search_text]
return matches[:limit]


Expand Down
83 changes: 83 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from __future__ import annotations

import unittest
from unittest.mock import patch

from src.commands import PORTED_COMMANDS, find_commands, get_commands
from src.models import PortingModule


class TestCommands(unittest.TestCase):
def test_get_commands(self) -> None:
commands = get_commands()
self.assertEqual(commands, PORTED_COMMANDS)

plugin_commands = [
command for command in PORTED_COMMANDS if 'plugin' in command.source_hint.lower()
]
if plugin_commands:
without_plugin_commands = get_commands(include_plugin_commands=False)
self.assertLess(len(without_plugin_commands), len(PORTED_COMMANDS))
for command in without_plugin_commands:
self.assertNotIn('plugin', command.source_hint.lower())

skill_commands = [
command for command in PORTED_COMMANDS if 'skills' in command.source_hint.lower()
]
if skill_commands:
without_skill_commands = get_commands(include_skill_commands=False)
self.assertLess(len(without_skill_commands), len(PORTED_COMMANDS))
for command in without_skill_commands:
self.assertNotIn('skills', command.source_hint.lower())

def test_get_commands_filters_only_source_hint_classifiers(self) -> None:
ordinary_command = PortingModule(
name='ordinary',
source_hint='commands/ordinary.ts',
responsibility='mentions plugin and skills boilerplate but is not sourced there',
status='mirrored',
)
plugin_command = PortingModule(
name='ordinary-plugin',
source_hint='plugins/ordinary-plugin.ts',
responsibility='ordinary command',
status='mirrored',
)
skill_command = PortingModule(
name='ordinary-skill',
source_hint='commands/skills/ordinary-skill.ts',
responsibility='ordinary command',
status='mirrored',
)

with patch(
'src.commands.PORTED_COMMANDS',
(ordinary_command, plugin_command, skill_command),
):
self.assertEqual(
get_commands(
include_plugin_commands=False,
include_skill_commands=False,
),
(ordinary_command,),
)

def test_find_commands(self) -> None:
if not PORTED_COMMANDS:
self.skipTest('No commands available in snapshot')

command = PORTED_COMMANDS[0]
self.assertIn(command, find_commands(command.name))
self.assertIn(command, find_commands(command.source_hint))

limit = 2
matches = find_commands('', limit=limit)
self.assertLessEqual(len(matches), limit)

def test_find_commands_excludes_responsibility_boilerplate(self) -> None:
matches = find_commands('typescript', limit=len(PORTED_COMMANDS))
self.assertEqual(matches, [])


Comment on lines +78 to +81
if __name__ == '__main__':
unittest.main()
23 changes: 23 additions & 0 deletions tests/test_tools.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

import unittest
from unittest.mock import patch

from src.tools import (
load_tool_snapshot,
build_tool_backlog,
Expand Down Expand Up @@ -93,6 +95,23 @@ def test_get_tools(self) -> None:
filtered = get_tools(permission_context=context)
self.assertNotIn(PORTED_TOOLS[0], filtered)

def test_get_tools_filters_mcp_by_name_or_source_hint_only(self) -> None:
ordinary_tool = PortingModule(
name='OrdinaryTool',
source_hint='tools/OrdinaryTool.ts',
responsibility='mentions mcp boilerplate but is not sourced there',
status='mirrored',
)
mcp_tool = PortingModule(
name='RemoteTool',
source_hint='tools/mcp/RemoteTool.ts',
responsibility='ordinary tool',
status='mirrored',
)

with patch('src.tools.PORTED_TOOLS', (ordinary_tool, mcp_tool)):
self.assertEqual(get_tools(include_mcp=False), (ordinary_tool,))

def test_find_tools(self) -> None:
if not PORTED_TOOLS:
self.skipTest("No tools available in snapshot")
Expand All @@ -111,6 +130,10 @@ def test_find_tools(self) -> None:
matches = find_tools("", limit=limit)
self.assertLessEqual(len(matches), limit)

def test_find_tools_excludes_responsibility_boilerplate(self) -> None:
matches = find_tools("typescript", limit=len(PORTED_TOOLS))
self.assertEqual(matches, [])

Comment on lines +134 to +136
def test_execute_tool(self) -> None:
if not PORTED_TOOLS:
self.skipTest("No tools available in snapshot")
Expand Down
Loading