kodit 0.4.3__py3-none-any.whl → 0.5.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of kodit might be problematic. Click here for more details.

Files changed (95) hide show
  1. kodit/_version.py +2 -2
  2. kodit/app.py +53 -23
  3. kodit/application/factories/reporting_factory.py +6 -2
  4. kodit/application/factories/server_factory.py +311 -0
  5. kodit/application/services/code_search_application_service.py +144 -0
  6. kodit/application/services/commit_indexing_application_service.py +543 -0
  7. kodit/application/services/indexing_worker_service.py +13 -44
  8. kodit/application/services/queue_service.py +24 -3
  9. kodit/application/services/reporting.py +0 -2
  10. kodit/application/services/sync_scheduler.py +15 -31
  11. kodit/cli.py +2 -753
  12. kodit/cli_utils.py +2 -9
  13. kodit/config.py +1 -94
  14. kodit/database.py +38 -1
  15. kodit/domain/{entities.py → entities/__init__.py} +50 -195
  16. kodit/domain/entities/git.py +190 -0
  17. kodit/domain/factories/__init__.py +1 -0
  18. kodit/domain/factories/git_repo_factory.py +76 -0
  19. kodit/domain/protocols.py +263 -64
  20. kodit/domain/services/bm25_service.py +5 -1
  21. kodit/domain/services/embedding_service.py +3 -0
  22. kodit/domain/services/git_repository_service.py +429 -0
  23. kodit/domain/services/git_service.py +300 -0
  24. kodit/domain/services/task_status_query_service.py +2 -2
  25. kodit/domain/value_objects.py +83 -114
  26. kodit/infrastructure/api/client/__init__.py +0 -2
  27. kodit/infrastructure/api/v1/__init__.py +0 -4
  28. kodit/infrastructure/api/v1/dependencies.py +92 -46
  29. kodit/infrastructure/api/v1/routers/__init__.py +0 -6
  30. kodit/infrastructure/api/v1/routers/commits.py +271 -0
  31. kodit/infrastructure/api/v1/routers/queue.py +2 -2
  32. kodit/infrastructure/api/v1/routers/repositories.py +282 -0
  33. kodit/infrastructure/api/v1/routers/search.py +31 -14
  34. kodit/infrastructure/api/v1/schemas/__init__.py +0 -24
  35. kodit/infrastructure/api/v1/schemas/commit.py +96 -0
  36. kodit/infrastructure/api/v1/schemas/context.py +2 -0
  37. kodit/infrastructure/api/v1/schemas/repository.py +128 -0
  38. kodit/infrastructure/api/v1/schemas/search.py +12 -9
  39. kodit/infrastructure/api/v1/schemas/snippet.py +58 -0
  40. kodit/infrastructure/api/v1/schemas/tag.py +31 -0
  41. kodit/infrastructure/api/v1/schemas/task_status.py +2 -0
  42. kodit/infrastructure/bm25/local_bm25_repository.py +16 -4
  43. kodit/infrastructure/bm25/vectorchord_bm25_repository.py +68 -52
  44. kodit/infrastructure/cloning/git/git_python_adaptor.py +467 -0
  45. kodit/infrastructure/cloning/git/working_copy.py +1 -1
  46. kodit/infrastructure/embedding/embedding_factory.py +3 -2
  47. kodit/infrastructure/embedding/local_vector_search_repository.py +1 -1
  48. kodit/infrastructure/embedding/vectorchord_vector_search_repository.py +111 -84
  49. kodit/infrastructure/enrichment/litellm_enrichment_provider.py +19 -26
  50. kodit/infrastructure/indexing/fusion_service.py +1 -1
  51. kodit/infrastructure/mappers/git_mapper.py +193 -0
  52. kodit/infrastructure/mappers/snippet_mapper.py +106 -0
  53. kodit/infrastructure/mappers/task_mapper.py +5 -44
  54. kodit/infrastructure/reporting/log_progress.py +8 -5
  55. kodit/infrastructure/reporting/telemetry_progress.py +21 -0
  56. kodit/infrastructure/slicing/slicer.py +32 -31
  57. kodit/infrastructure/sqlalchemy/embedding_repository.py +43 -23
  58. kodit/infrastructure/sqlalchemy/entities.py +394 -158
  59. kodit/infrastructure/sqlalchemy/git_branch_repository.py +263 -0
  60. kodit/infrastructure/sqlalchemy/git_commit_repository.py +337 -0
  61. kodit/infrastructure/sqlalchemy/git_repository.py +252 -0
  62. kodit/infrastructure/sqlalchemy/git_tag_repository.py +257 -0
  63. kodit/infrastructure/sqlalchemy/snippet_v2_repository.py +484 -0
  64. kodit/infrastructure/sqlalchemy/task_repository.py +29 -23
  65. kodit/infrastructure/sqlalchemy/task_status_repository.py +24 -12
  66. kodit/infrastructure/sqlalchemy/unit_of_work.py +10 -14
  67. kodit/mcp.py +12 -30
  68. kodit/migrations/env.py +1 -0
  69. kodit/migrations/versions/04b80f802e0c_foreign_key_review.py +100 -0
  70. kodit/migrations/versions/7f15f878c3a1_add_new_git_entities.py +690 -0
  71. kodit/migrations/versions/f9e5ef5e688f_add_git_commits_number.py +43 -0
  72. kodit/py.typed +0 -0
  73. kodit/utils/dump_openapi.py +7 -4
  74. kodit/utils/path_utils.py +29 -0
  75. {kodit-0.4.3.dist-info → kodit-0.5.0.dist-info}/METADATA +3 -3
  76. kodit-0.5.0.dist-info/RECORD +137 -0
  77. kodit/application/factories/code_indexing_factory.py +0 -195
  78. kodit/application/services/auto_indexing_service.py +0 -99
  79. kodit/application/services/code_indexing_application_service.py +0 -410
  80. kodit/domain/services/index_query_service.py +0 -70
  81. kodit/domain/services/index_service.py +0 -269
  82. kodit/infrastructure/api/client/index_client.py +0 -57
  83. kodit/infrastructure/api/v1/routers/indexes.py +0 -164
  84. kodit/infrastructure/api/v1/schemas/index.py +0 -101
  85. kodit/infrastructure/bm25/bm25_factory.py +0 -28
  86. kodit/infrastructure/cloning/__init__.py +0 -1
  87. kodit/infrastructure/cloning/metadata.py +0 -98
  88. kodit/infrastructure/mappers/index_mapper.py +0 -345
  89. kodit/infrastructure/reporting/tdqm_progress.py +0 -38
  90. kodit/infrastructure/slicing/language_detection_service.py +0 -18
  91. kodit/infrastructure/sqlalchemy/index_repository.py +0 -646
  92. kodit-0.4.3.dist-info/RECORD +0 -125
  93. {kodit-0.4.3.dist-info → kodit-0.5.0.dist-info}/WHEEL +0 -0
  94. {kodit-0.4.3.dist-info → kodit-0.5.0.dist-info}/entry_points.txt +0 -0
  95. {kodit-0.4.3.dist-info → kodit-0.5.0.dist-info}/licenses/LICENSE +0 -0
@@ -14,17 +14,10 @@ class SqlAlchemyUnitOfWork:
14
14
  self._session_factory = session_factory
15
15
  self._session: AsyncSession | None = None
16
16
 
17
- @property
18
- def session(self) -> AsyncSession:
19
- """Get the current session."""
20
- if self._session is None:
21
- raise RuntimeError("UnitOfWork must be used within async context")
22
- return self._session
23
-
24
- async def __aenter__(self) -> "SqlAlchemyUnitOfWork":
17
+ async def __aenter__(self) -> "AsyncSession":
25
18
  """Enter the unit of work context."""
26
19
  self._session = self._session_factory()
27
- return self
20
+ return self._session
28
21
 
29
22
  async def __aexit__(
30
23
  self,
@@ -34,11 +27,14 @@ class SqlAlchemyUnitOfWork:
34
27
  ) -> None:
35
28
  """Exit the unit of work context."""
36
29
  if self._session:
37
- if exc_type is not None:
38
- await self._session.rollback()
39
- await self._session.commit()
40
- await self._session.close()
41
- self._session = None
30
+ try:
31
+ if exc_type is None: # Only commit if no exception
32
+ await self._session.commit()
33
+ else:
34
+ await self._session.rollback()
35
+ finally:
36
+ await self._session.close()
37
+ self._session = None
42
38
 
43
39
  async def commit(self) -> None:
44
40
  """Commit the current transaction."""
kodit/mcp.py CHANGED
@@ -1,6 +1,6 @@
1
1
  """MCP server for kodit."""
2
2
 
3
- from collections.abc import AsyncIterator, Callable
3
+ from collections.abc import AsyncIterator
4
4
  from contextlib import asynccontextmanager
5
5
  from dataclasses import dataclass
6
6
  from pathlib import Path
@@ -9,35 +9,27 @@ from typing import Annotated
9
9
  import structlog
10
10
  from fastmcp import Context, FastMCP
11
11
  from pydantic import Field
12
- from sqlalchemy.ext.asyncio import AsyncSession
13
12
 
14
13
  from kodit._version import version
15
- from kodit.application.factories.code_indexing_factory import (
16
- create_code_indexing_application_service,
17
- )
18
- from kodit.application.factories.reporting_factory import create_server_operation
14
+ from kodit.application.factories.server_factory import ServerFactory
15
+ from kodit.application.services.code_search_application_service import MultiSearchResult
19
16
  from kodit.config import AppContext
20
17
  from kodit.database import Database
21
18
  from kodit.domain.value_objects import (
22
19
  MultiSearchRequest,
23
- MultiSearchResult,
24
20
  SnippetSearchFilters,
25
21
  )
26
- from kodit.infrastructure.sqlalchemy.task_status_repository import (
27
- create_task_status_repository,
28
- )
29
22
 
30
23
  # Global database connection for MCP server
31
24
  _mcp_db: Database | None = None
25
+ _mcp_server_factory: ServerFactory | None = None
32
26
 
33
27
 
34
28
  @dataclass
35
29
  class MCPContext:
36
30
  """Context for the MCP server."""
37
31
 
38
- session: AsyncSession
39
- session_factory: Callable[[], AsyncSession]
40
- app_context: AppContext
32
+ server_factory: ServerFactory
41
33
 
42
34
 
43
35
  @asynccontextmanager
@@ -55,16 +47,12 @@ async def mcp_lifespan(_: FastMCP) -> AsyncIterator[MCPContext]:
55
47
  Since they don't provide a good way to handle global state, we must use a
56
48
  global variable to store the database connection.
57
49
  """
58
- global _mcp_db # noqa: PLW0603
59
- app_context = AppContext()
60
- if _mcp_db is None:
61
- _mcp_db = await app_context.get_db()
62
- async with _mcp_db.session_factory() as session:
63
- yield MCPContext(
64
- session=session,
65
- app_context=app_context,
66
- session_factory=_mcp_db.session_factory,
67
- )
50
+ global _mcp_server_factory # noqa: PLW0603
51
+ if _mcp_server_factory is None:
52
+ app_context = AppContext()
53
+ db = await app_context.get_db()
54
+ _mcp_server_factory = ServerFactory(app_context, db.session_factory)
55
+ yield MCPContext(_mcp_server_factory)
68
56
 
69
57
 
70
58
  def create_mcp_server(name: str, instructions: str | None = None) -> FastMCP:
@@ -180,13 +168,7 @@ def register_mcp_tools(mcp_server: FastMCP) -> None:
180
168
  mcp_context: MCPContext = ctx.request_context.lifespan_context
181
169
 
182
170
  # Use the unified application service
183
- service = create_code_indexing_application_service(
184
- app_context=mcp_context.app_context,
185
- session_factory=mcp_context.session_factory,
186
- operation=create_server_operation(
187
- create_task_status_repository(mcp_context.session_factory)
188
- ),
189
- )
171
+ service = mcp_context.server_factory.code_search_application_service()
190
172
 
191
173
  log.debug("Searching for snippets")
192
174
 
kodit/migrations/env.py CHANGED
@@ -41,6 +41,7 @@ def run_migrations_offline() -> None:
41
41
  target_metadata=target_metadata,
42
42
  literal_binds=True,
43
43
  dialect_opts={"paramstyle": "named"},
44
+ render_as_batch=True,
44
45
  )
45
46
 
46
47
  with context.begin_transaction():
@@ -0,0 +1,100 @@
1
+ # ruff: noqa
2
+ """foreign key review
3
+
4
+ Revision ID: 04b80f802e0c
5
+ Revises: 7f15f878c3a1
6
+ Create Date: 2025-09-22 11:21:43.432880
7
+
8
+ """
9
+
10
+ from typing import Sequence, Union
11
+
12
+ from alembic import op
13
+ import sqlalchemy as sa
14
+
15
+
16
+ # revision identifiers, used by Alembic.
17
+ revision: str = "04b80f802e0c"
18
+ down_revision: Union[str, None] = "7f15f878c3a1"
19
+ branch_labels: Union[str, Sequence[str], None] = None
20
+ depends_on: Union[str, Sequence[str], None] = None
21
+
22
+
23
+ def upgrade() -> None:
24
+ """Upgrade schema."""
25
+ # SQLite doesn't support complex constraint alterations, so we'll drop and recreate tables
26
+
27
+ # Drop and recreate commit_indexes table with commit_sha as primary key
28
+ op.drop_table("commit_indexes")
29
+ op.create_table(
30
+ "commit_indexes",
31
+ sa.Column("commit_sha", sa.String(64), nullable=False),
32
+ sa.Column("status", sa.String(255), nullable=False),
33
+ sa.Column("indexed_at", sa.DateTime(), nullable=True),
34
+ sa.Column("error_message", sa.UnicodeText(), nullable=True),
35
+ sa.Column("files_processed", sa.Integer(), nullable=False, default=0),
36
+ sa.Column("processing_time_seconds", sa.Float(), nullable=False, default=0.0),
37
+ sa.Column("created_at", sa.DateTime(), nullable=False),
38
+ sa.Column("updated_at", sa.DateTime(), nullable=False),
39
+ sa.PrimaryKeyConstraint("commit_sha", name="pk_commit_indexes"),
40
+ )
41
+ op.create_index("ix_commit_indexes_status", "commit_indexes", ["status"])
42
+
43
+ # Drop and recreate git_tracking_branches table with proper constraints
44
+ op.drop_table("git_tracking_branches")
45
+ op.create_table(
46
+ "git_tracking_branches",
47
+ sa.Column("repo_id", sa.Integer(), nullable=False),
48
+ sa.Column("name", sa.String(255), nullable=False),
49
+ sa.Column("created_at", sa.DateTime(), nullable=False),
50
+ sa.Column("updated_at", sa.DateTime(), nullable=False),
51
+ sa.ForeignKeyConstraint(
52
+ ["repo_id"], ["git_repos.id"], name="fk_tracking_branch_repo"
53
+ ),
54
+ sa.PrimaryKeyConstraint("repo_id", "name", name="pk_git_tracking_branches"),
55
+ )
56
+ op.create_index("ix_git_tracking_branches_name", "git_tracking_branches", ["name"])
57
+ op.create_index(
58
+ "ix_git_tracking_branches_repo_id", "git_tracking_branches", ["repo_id"]
59
+ )
60
+
61
+
62
+ def downgrade() -> None:
63
+ """Downgrade schema."""
64
+ # Recreate the original tables
65
+
66
+ # Recreate commit_indexes table with id-based primary key (original structure)
67
+ op.drop_table("commit_indexes")
68
+ op.create_table(
69
+ "commit_indexes",
70
+ sa.Column("id", sa.Integer(), nullable=False),
71
+ sa.Column("commit_sha", sa.String(64), nullable=False),
72
+ sa.Column("status", sa.String(255), nullable=False),
73
+ sa.Column("indexed_at", sa.DateTime(), nullable=True),
74
+ sa.Column("error_message", sa.UnicodeText(), nullable=True),
75
+ sa.Column("files_processed", sa.Integer(), nullable=False, default=0),
76
+ sa.Column("processing_time_seconds", sa.Float(), nullable=False, default=0.0),
77
+ sa.Column("created_at", sa.DateTime(), nullable=False),
78
+ sa.Column("updated_at", sa.DateTime(), nullable=False),
79
+ sa.PrimaryKeyConstraint("id"),
80
+ )
81
+ op.create_index("ix_commit_indexes_status", "commit_indexes", ["status"])
82
+
83
+ # Recreate git_tracking_branches table with original structure
84
+ op.drop_table("git_tracking_branches")
85
+ op.create_table(
86
+ "git_tracking_branches",
87
+ sa.Column("repo_id", sa.Integer(), nullable=False),
88
+ sa.Column("name", sa.String(255), nullable=False),
89
+ sa.Column("created_at", sa.DateTime(), nullable=False),
90
+ sa.Column("updated_at", sa.DateTime(), nullable=False),
91
+ sa.ForeignKeyConstraint(
92
+ ["repo_id", "name"], ["git_branches.repo_id", "git_branches.name"]
93
+ ),
94
+ sa.PrimaryKeyConstraint("repo_id"),
95
+ sa.UniqueConstraint("repo_id", "name", name="uix_repo_tracking_branch"),
96
+ )
97
+ op.create_index("ix_git_tracking_branches_name", "git_tracking_branches", ["name"])
98
+ op.create_index(
99
+ "ix_git_tracking_branches_repo_id", "git_tracking_branches", ["repo_id"]
100
+ )