Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(workspace): embedded database identity per repo, checked on the provider side (#4653)
Raghav Chamadiyaba2ccdab08ee
diff --git a/docs/reference/WORKSPACE_CONTRACTS.md b/docs/reference/WORKSPACE_CONTRACTS.mdindex 455c67d31..a5423ff82 100644--- a/docs/reference/WORKSPACE_CONTRACTS.md+++ b/docs/reference/WORKSPACE_CONTRACTS.md@@ -89,7 +89,7 @@ binding site itself is linked. A publish to the default exchange## Data contracts-Data/DB contracts use the id scheme `data::<table>` and render as a `db` edge in the [system graph](../scale/WORKSPACES.md#system-graph). An ORM model with no explicit table name takes its library's default: the Prisma model name, the TypeORM class name in snake case, the Sequelize model name pluralized, the Eloquent class name in snake case and plural. A service that only models a table (an ORM class, no migration) is linked to the service whose migration or DDL defines that table's schema, when exactly one service defines it; two services that each migrate a table of one name are read as separate databases and not linked. A reader in a file that opens an embedded database (SQLite) links only to tables declared in its own repo, since that file is its own database. The consumer side (SQL string matching) is heuristic and lower-confidence than the ORM-based providers; unlike HTTP and gRPC, there is no field-level breaking-change diffing for data contracts, only table/route-level removal.+Data/DB contracts use the id scheme `data::<table>` and render as a `db` edge in the [system graph](../scale/WORKSPACES.md#system-graph). An ORM model with no explicit table name takes its library's default: the Prisma model name, the TypeORM class name in snake case, the Sequelize model name pluralized, the Eloquent class name in snake case and plural. A service that only models a table (an ORM class, no migration) is linked to the service whose migration or DDL defines that table's schema, when exactly one service defines it; two services that each migrate a table of one name are read as separate databases and not linked. A table on an embedded database (SQLite) links only within its own repo, on either side: a reader or declaration in a file that opens one, and every table in a repo whose code names an embedded engine and no server database (Postgres, MySQL, SQL Server), since that file is the repo's own database. The consumer side (SQL string matching) is heuristic and lower-confidence than the ORM-based providers; unlike HTTP and gRPC, there is no field-level breaking-change diffing for data contracts, only table/route-level removal.## HTTP routes and callsdiff --git a/packages/core/src/repowise/core/workspace/contracts.py b/packages/core/src/repowise/core/workspace/contracts.pyindex c60a29194..645d5891e 100644--- a/packages/core/src/repowise/core/workspace/contracts.py+++ b/packages/core/src/repowise/core/workspace/contracts.py@@ -77,8 +77,9 @@ CONTRACTS_FILENAME = "contracts.json"# Go routes from chi, fiber, gorilla/mux and Go 1.22 method patterns are read.# Next.js route handlers exported from a brace list are read, and the probe# skips tooling and build files.-# A data consumer in a file that opens SQLite carries ``meta['embedded']`` and-# links only within its repo.+# A data contract in a file that opens SQLite, or in a repo that names SQLite+# and no server database, carries ``meta['embedded']`` and links only within+# its repo, on either side.# An HTTP or gRPC consumer whose id two or more repos serve, with nothing# placing it in one, carries ``meta['ambiguous_provider']`` and links to none.CONTRACTS_VERSION = 15diff --git a/packages/core/src/repowise/core/workspace/extractors/data/__init__.py b/packages/core/src/repowise/core/workspace/extractors/data/__init__.pyindex e8550830f..ca61d0912 100644--- a/packages/core/src/repowise/core/workspace/extractors/data/__init__.py+++ b/packages/core/src/repowise/core/workspace/extractors/data/__init__.py@@ -11,8 +11,12 @@ the Live System Map.from __future__ import annotations+from typing import TYPE_CHECKING++from ..base import select_filesfrom ..dialect import ContractDialect, DialectExtractorfrom .ddl import DdlDialect+from .dialect import repo_embedded_enginefrom .drizzle import DrizzleDialectfrom .knex import KnexDialectfrom .laravel import EloquentDialect, LaravelMigrationDialect, LaravelQueryDialect@@ -28,6 +32,14 @@ from .sequelize import SequelizeDialectfrom .sql_strings import SqlStringsDialectfrom .typeorm import TypeOrmDialect+if TYPE_CHECKING:+ from collections.abc import Callable, Sequence+ from pathlib import Path++ from repowise.core.workspace.contracts import Contract++ from ..base import SourceFile+# Table-ownership recognisers (DDL + one per ORM family).PROVIDER_DIALECTS: tuple[ContractDialect, ...] = (DdlDialect(),@@ -58,6 +70,27 @@ class DataExtractor(DialectExtractor):dialects = PROVIDER_DIALECTS + CONSUMER_DIALECTS+ def extract(+ self,+ repo_path: Path,+ repo_alias: str = "",+ exclude: Callable[[str], bool] | None = None,+ files: Sequence[SourceFile] | None = None,+ ) -> list[Contract]:+ """The repo's table contracts, each tagged with the repo's embedded engine.++ The engine test reads the files again only when the repo has a table.+ """+ contracts = super().extract(repo_path, repo_alias, exclude, files)+ if not contracts:+ return contracts+ selected = select_files(repo_path, self.source_extensions(), exclude, files)+ engine = repo_embedded_engine(content for _, _, content in selected)+ if engine:+ for c in contracts:+ c.meta.setdefault("embedded", engine)+ return contracts+__all__ = ["CONSUMER_DIALECTS",diff --git a/packages/core/src/repowise/core/workspace/extractors/data/dialect.py b/packages/core/src/repowise/core/workspace/extractors/data/dialect.pyindex b54338241..5556b4838 100644--- a/packages/core/src/repowise/core/workspace/extractors/data/dialect.py+++ b/packages/core/src/repowise/core/workspace/extractors/data/dialect.py@@ -10,7 +10,7 @@ scheme (``data::<normalized table>``) and the normalization rules stay infrom __future__ import annotationsimport re-from collections.abc import Callable+from collections.abc import Callable, Iterablefrom typing import TYPE_CHECKINGfrom repowise.core.workspace.origins import ContractOrigin@@ -53,7 +53,12 @@ def build_table_provider(symbol_name=f"{framework}:{table_raw}",origin=origin,line=line,- meta={"table": table, "framework": framework, **({"schema": schema} if schema else {})},+ meta={+ "table": table,+ "framework": framework,+ **({"schema": schema} if schema else {}),+ **_embedded_meta(ctx),+ },)@@ -82,6 +87,37 @@ _EMBEDDED_ENGINES: tuple[tuple[str, tuple[str, ...]], ...] = ()+#: Tokens naming a server database: a driver, a dialect or a URL scheme, in+#: lower case (matched against the lower-cased file). A repo that names one may+#: run its tables on a shared server, so only the file that opens an embedded+#: engine is read as embedded (see :func:`repo_embedded_engine`). A stray hit+#: (a comment) only keeps that per-file reading.+_SERVER_ENGINE_TOKENS: tuple[str, ...] = (+ "postgres",+ "pgsql",+ "psycopg",+ "asyncpg",+ "npgsql",+ "lib/pq",+ "jackc/pgx",+ "'pg'",+ '"pg"',+ "pg-promise",+ "pg::",+ "mysql",+ "mariadb",+ "sqlserver",+ "sqlsrv",+ "mssql",+ "tedious",+ "oracle",+ "cockroach",+ "redshift",+ "snowflake",+ "clickhouse",+)++def embedded_engine(ctx: ScanContext) -> str | None:"""The embedded engine *ctx*'s file opens (``sqlite``), or ``None``."""for engine, tokens in _EMBEDDED_ENGINES:@@ -90,6 +126,35 @@ def embedded_engine(ctx: ScanContext) -> str | None:return None+def repo_embedded_engine(contents: Iterable[str]) -> str | None:+ """The embedded engine a repo runs on, when it names one and no server database.++ Apps open SQLite in one module (``db.py``, ``config.py``) and query it in+ others, so the file-level tag misses most readers. A repo whose files name+ an embedded engine and no server driver keeps all its tables in that file.+ Deliberate ceiling: one engine per repo; a monorepo naming both a server+ and an embedded engine keeps the per-file tag. Upgrade path: the same test+ per package directory.+ """+ found = None+ for content in contents:+ lowered = content.lower()+ if any(token in lowered for token in _SERVER_ENGINE_TOKENS):+ return None+ if found is None:+ found = next(+ (e for e, tokens in _EMBEDDED_ENGINES if any(t in content for t in tokens)),+ None,+ )+ return found+++def _embedded_meta(ctx: ScanContext) -> dict[str, str]:+ """``{"embedded": engine}`` when *ctx*'s file opens an embedded engine."""+ engine = embedded_engine(ctx)+ return {"embedded": engine} if engine else {}++def build_table_consumer(ctx: ScanContext,*,@@ -109,7 +174,6 @@ def build_table_consumer(table = normalize_table_name(table_raw)if table is None or is_system_table(table):return None- engine = embedded_engine(ctx)return build_contract(ctx,contract_id=f"data::{table}",@@ -122,7 +186,7 @@ def build_table_consumer("table": table,"verb": verb,"client": client,- **({"embedded": engine} if engine else {}),+ **_embedded_meta(ctx),},)diff --git a/packages/core/src/repowise/core/workspace/matching/data.py b/packages/core/src/repowise/core/workspace/matching/data.pyindex 2b0a86519..afc52a13f 100644--- a/packages/core/src/repowise/core/workspace/matching/data.py+++ b/packages/core/src/repowise/core/workspace/matching/data.py@@ -33,11 +33,13 @@ def _rank(c: Contract) -> tuple[int, str, int]:def accepts(provider: Contract, consumer: Contract) -> bool:"""Whether *consumer* can read *provider*'s table.- A consumer in a file that opens an embedded database (SQLite) reads that- file's tables, which no other repo's schema declares: two repos sharing a- table name there have two databases. Within one repo the name still links.+ A table on an embedded database (SQLite) lives in a file its own repo+ opens, so no other repo reads it or declares it: when either side is+ embedded, two repos sharing a table name have two databases. Within one+ repo the name still links."""- return not consumer.meta.get("embedded") or provider.repo == consumer.repo+ embedded = consumer.meta.get("embedded") or provider.meta.get("embedded")+ return not embedded or provider.repo == consumer.repodef shared_table_pass(state: MatchState) -> None:@@ -64,5 +66,5 @@ def shared_table_pass(state: MatchState) -> None:continueowner = owners[0]for other in best:- if other is not owner and not internal(owner, other):+ if other is not owner and not internal(owner, other) and accepts(owner, other):state.add(other, owner, LinkStrategy.SHARED_TABLE)diff --git a/tests/unit/workspace/test_data_contracts.py b/tests/unit/workspace/test_data_contracts.pyindex e4031cafc..52d82aa2f 100644--- a/tests/unit/workspace/test_data_contracts.py+++ b/tests/unit/workspace/test_data_contracts.py@@ -415,6 +415,57 @@ class TestDataMatching:contracts += DataExtractor().extract(worker, "worker")assert len(match_contracts(contracts)) == 1+ def test_repo_that_opens_sqlite_in_one_module_keeps_its_tables(self, tmp_path: Path) -> None:+ # The Flask tutorial shape: db.py opens SQLite, auth.py queries it,+ # schema.sql declares it. No other repo reads or owns those tables.+ app = tmp_path / "app"+ server = tmp_path / "server"+ _write(app, "app/db.py", "import sqlite3\n")+ _write(app, "app/auth.py", 'q = "SELECT id FROM user WHERE x = 1"\n')+ _write(app, "app/schema.sql", "CREATE TABLE post (id INT);")+ _write(server, "db/database.sql", "CREATE TABLE user (id INT);")+ _write(server, "db/db.go", 'import _ "github.com/lib/pq"\n')+ _write(server, "models/post.go", 'const q = "SELECT id FROM post WHERE x = 1"\n')+ app_contracts = DataExtractor().extract(app, "app")+ assert {c.meta.get("embedded") for c in app_contracts} == {"sqlite"}+ server_contracts = DataExtractor().extract(server, "server")+ assert len(server_contracts) == 2+ assert all("embedded" not in c.meta for c in server_contracts)+ assert match_contracts(app_contracts + server_contracts) == []++ def test_repo_naming_a_server_database_keeps_the_file_tag(self, tmp_path: Path) -> None:+ # SQLite only as a fallback to DATABASE_URL: the app may run on a shared server.+ app = tmp_path / "app"+ backend = tmp_path / "backend"+ _write(+ app,+ "config.py",+ "URI = os.environ.get('DATABASE_URL', '').replace('postgres://', 'postgresql://')"+ " or 'sqlite:///app.db'\n",+ )+ _write(app, "routes.py", 'q = "SELECT id FROM repos WHERE x = 1"\n')+ _write(backend, "migrations/001.sql", "CREATE TABLE repos (id INT);")+ contracts = DataExtractor().extract(app, "app") + DataExtractor().extract(backend, "backend")+ assert len(match_contracts(contracts)) == 1++ def test_repo_engine_reads_server_tokens_in_any_case(self) -> None:+ from repowise.core.workspace.extractors.data.dialect import repo_embedded_engine++ db = "import sqlite3\n"+ assert repo_embedded_engine([db, "x = 1\n"]) == "sqlite"+ assert repo_embedded_engine([db, "'driver' => 'pgsql',\n"]) is None+ assert repo_embedded_engine([db, "const { Pool } = require('pg');\n"]) is None+ assert repo_embedded_engine([db, "# Works with MariaDB too\n"]) is None+ assert repo_embedded_engine(["x = 1\n"]) is None++ def test_embedded_model_not_shared_with_another_repos_schema(self, tmp_path: Path) -> None:+ _write(tmp_path / "api", "db/schema.sql", "CREATE TABLE refunds (id INT);")+ _write(tmp_path / "admin", "config.php", "<?php\n$db = new PDO('sqlite:app.db');\n")+ _write(tmp_path / "admin", "app/Models/Refund.php", _ELOQUENT_HEAD + "class Refund extends Model {}\n")+ contracts = DataExtractor().extract(tmp_path / "api", "api")+ contracts += DataExtractor().extract(tmp_path / "admin", "admin")+ assert match_contracts(contracts) == []+_ELOQUENT_HEAD = "<?php\nnamespace App\\Models;\nuse Illuminate\\Database\\Eloquent\\Model;\n"