kodit 0.4.2__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 (100) hide show
  1. kodit/_version.py +2 -2
  2. kodit/app.py +59 -24
  3. kodit/application/factories/reporting_factory.py +16 -7
  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 -46
  8. kodit/application/services/queue_service.py +24 -3
  9. kodit/application/services/reporting.py +70 -54
  10. kodit/application/services/sync_scheduler.py +15 -31
  11. kodit/cli.py +2 -763
  12. kodit/cli_utils.py +2 -9
  13. kodit/config.py +3 -96
  14. kodit/database.py +38 -1
  15. kodit/domain/entities/__init__.py +276 -0
  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 +270 -46
  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 +19 -0
  25. kodit/domain/value_objects.py +113 -147
  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 +105 -44
  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 +41 -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 +10 -3
  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/enrichment/local_enrichment_provider.py +41 -30
  51. kodit/infrastructure/indexing/fusion_service.py +1 -1
  52. kodit/infrastructure/mappers/git_mapper.py +193 -0
  53. kodit/infrastructure/mappers/snippet_mapper.py +106 -0
  54. kodit/infrastructure/mappers/task_mapper.py +5 -44
  55. kodit/infrastructure/mappers/task_status_mapper.py +85 -0
  56. kodit/infrastructure/reporting/db_progress.py +23 -0
  57. kodit/infrastructure/reporting/log_progress.py +13 -38
  58. kodit/infrastructure/reporting/telemetry_progress.py +21 -0
  59. kodit/infrastructure/slicing/slicer.py +32 -31
  60. kodit/infrastructure/sqlalchemy/embedding_repository.py +43 -23
  61. kodit/infrastructure/sqlalchemy/entities.py +428 -131
  62. kodit/infrastructure/sqlalchemy/git_branch_repository.py +263 -0
  63. kodit/infrastructure/sqlalchemy/git_commit_repository.py +337 -0
  64. kodit/infrastructure/sqlalchemy/git_repository.py +252 -0
  65. kodit/infrastructure/sqlalchemy/git_tag_repository.py +257 -0
  66. kodit/infrastructure/sqlalchemy/snippet_v2_repository.py +484 -0
  67. kodit/infrastructure/sqlalchemy/task_repository.py +29 -23
  68. kodit/infrastructure/sqlalchemy/task_status_repository.py +91 -0
  69. kodit/infrastructure/sqlalchemy/unit_of_work.py +10 -14
  70. kodit/mcp.py +12 -26
  71. kodit/migrations/env.py +1 -1
  72. kodit/migrations/versions/04b80f802e0c_foreign_key_review.py +100 -0
  73. kodit/migrations/versions/7f15f878c3a1_add_new_git_entities.py +690 -0
  74. kodit/migrations/versions/b9cd1c3fd762_add_task_status.py +77 -0
  75. kodit/migrations/versions/f9e5ef5e688f_add_git_commits_number.py +43 -0
  76. kodit/py.typed +0 -0
  77. kodit/utils/dump_openapi.py +7 -4
  78. kodit/utils/path_utils.py +29 -0
  79. {kodit-0.4.2.dist-info → kodit-0.5.0.dist-info}/METADATA +3 -3
  80. kodit-0.5.0.dist-info/RECORD +137 -0
  81. kodit/application/factories/code_indexing_factory.py +0 -193
  82. kodit/application/services/auto_indexing_service.py +0 -103
  83. kodit/application/services/code_indexing_application_service.py +0 -393
  84. kodit/domain/entities.py +0 -323
  85. kodit/domain/services/index_query_service.py +0 -70
  86. kodit/domain/services/index_service.py +0 -267
  87. kodit/infrastructure/api/client/index_client.py +0 -57
  88. kodit/infrastructure/api/v1/routers/indexes.py +0 -119
  89. kodit/infrastructure/api/v1/schemas/index.py +0 -101
  90. kodit/infrastructure/bm25/bm25_factory.py +0 -28
  91. kodit/infrastructure/cloning/__init__.py +0 -1
  92. kodit/infrastructure/cloning/metadata.py +0 -98
  93. kodit/infrastructure/mappers/index_mapper.py +0 -345
  94. kodit/infrastructure/reporting/tdqm_progress.py +0 -73
  95. kodit/infrastructure/slicing/language_detection_service.py +0 -18
  96. kodit/infrastructure/sqlalchemy/index_repository.py +0 -646
  97. kodit-0.4.2.dist-info/RECORD +0 -119
  98. {kodit-0.4.2.dist-info → kodit-0.5.0.dist-info}/WHEEL +0 -0
  99. {kodit-0.4.2.dist-info → kodit-0.5.0.dist-info}/entry_points.txt +0 -0
  100. {kodit-0.4.2.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,32 +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
22
 
27
23
  # Global database connection for MCP server
28
24
  _mcp_db: Database | None = None
25
+ _mcp_server_factory: ServerFactory | None = None
29
26
 
30
27
 
31
28
  @dataclass
32
29
  class MCPContext:
33
30
  """Context for the MCP server."""
34
31
 
35
- session: AsyncSession
36
- session_factory: Callable[[], AsyncSession]
37
- app_context: AppContext
32
+ server_factory: ServerFactory
38
33
 
39
34
 
40
35
  @asynccontextmanager
@@ -52,16 +47,12 @@ async def mcp_lifespan(_: FastMCP) -> AsyncIterator[MCPContext]:
52
47
  Since they don't provide a good way to handle global state, we must use a
53
48
  global variable to store the database connection.
54
49
  """
55
- global _mcp_db # noqa: PLW0603
56
- app_context = AppContext()
57
- if _mcp_db is None:
58
- _mcp_db = await app_context.get_db()
59
- async with _mcp_db.session_factory() as session:
60
- yield MCPContext(
61
- session=session,
62
- app_context=app_context,
63
- session_factory=_mcp_db.session_factory,
64
- )
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)
65
56
 
66
57
 
67
58
  def create_mcp_server(name: str, instructions: str | None = None) -> FastMCP:
@@ -177,12 +168,7 @@ def register_mcp_tools(mcp_server: FastMCP) -> None:
177
168
  mcp_context: MCPContext = ctx.request_context.lifespan_context
178
169
 
179
170
  # Use the unified application service
180
- service = create_code_indexing_application_service(
181
- app_context=mcp_context.app_context,
182
- session=mcp_context.session,
183
- session_factory=mcp_context.session_factory,
184
- operation=create_server_operation(),
185
- )
171
+ service = mcp_context.server_factory.code_search_application_service()
186
172
 
187
173
  log.debug("Searching for snippets")
188
174
 
kodit/migrations/env.py CHANGED
@@ -1,4 +1,3 @@
1
- # ruff: noqa: F401
2
1
  """Alembic environment file for kodit."""
3
2
 
4
3
  import asyncio
@@ -42,6 +41,7 @@ def run_migrations_offline() -> None:
42
41
  target_metadata=target_metadata,
43
42
  literal_binds=True,
44
43
  dialect_opts={"paramstyle": "named"},
44
+ render_as_batch=True,
45
45
  )
46
46
 
47
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
+ )